libstdc++: Fix -Wsign-compare warning in std::string::resize_for_overwrite
[official-gcc.git] / gcc / ada / sem_ch3.adb
blob4dac4eec108985586ab11aa35e25facea377e844
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ C H 3 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2024, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Accessibility; use Accessibility;
27 with Aspects; use Aspects;
28 with Atree; use Atree;
29 with Checks; use Checks;
30 with Contracts; use Contracts;
31 with Debug; use Debug;
32 with Elists; use Elists;
33 with Einfo; use Einfo;
34 with Einfo.Entities; use Einfo.Entities;
35 with Einfo.Utils; use Einfo.Utils;
36 with Errout; use Errout;
37 with Eval_Fat; use Eval_Fat;
38 with Exp_Ch3; use Exp_Ch3;
39 with Exp_Ch9; use Exp_Ch9;
40 with Exp_Disp; use Exp_Disp;
41 with Exp_Dist; use Exp_Dist;
42 with Exp_Tss; use Exp_Tss;
43 with Exp_Util; use Exp_Util;
44 with Expander; use Expander;
45 with Freeze; use Freeze;
46 with Ghost; use Ghost;
47 with Itypes; use Itypes;
48 with Layout; use Layout;
49 with Lib; use Lib;
50 with Lib.Xref; use Lib.Xref;
51 with Mutably_Tagged; use Mutably_Tagged;
52 with Namet; use Namet;
53 with Nlists; use Nlists;
54 with Nmake; use Nmake;
55 with Opt; use Opt;
56 with Restrict; use Restrict;
57 with Rident; use Rident;
58 with Rtsfind; use Rtsfind;
59 with Sem; use Sem;
60 with Sem_Aux; use Sem_Aux;
61 with Sem_Case; use Sem_Case;
62 with Sem_Cat; use Sem_Cat;
63 with Sem_Ch6; use Sem_Ch6;
64 with Sem_Ch7; use Sem_Ch7;
65 with Sem_Ch8; use Sem_Ch8;
66 with Sem_Ch10; use Sem_Ch10;
67 with Sem_Ch13; use Sem_Ch13;
68 with Sem_Dim; use Sem_Dim;
69 with Sem_Disp; use Sem_Disp;
70 with Sem_Dist; use Sem_Dist;
71 with Sem_Elab; use Sem_Elab;
72 with Sem_Elim; use Sem_Elim;
73 with Sem_Eval; use Sem_Eval;
74 with Sem_Mech; use Sem_Mech;
75 with Sem_Res; use Sem_Res;
76 with Sem_Smem; use Sem_Smem;
77 with Sem_Type; use Sem_Type;
78 with Sem_Util; use Sem_Util;
79 with Sem_Warn; use Sem_Warn;
80 with Stand; use Stand;
81 with Sinfo; use Sinfo;
82 with Sinfo.Nodes; use Sinfo.Nodes;
83 with Sinfo.Utils; use Sinfo.Utils;
84 with Sinput; use Sinput;
85 with Snames; use Snames;
86 with Strub; use Strub;
87 with Targparm; use Targparm;
88 with Tbuild; use Tbuild;
89 with Ttypes; use Ttypes;
90 with Uintp; use Uintp;
91 with Urealp; use Urealp;
92 with Warnsw; use Warnsw;
94 package body Sem_Ch3 is
96 -----------------------
97 -- Local Subprograms --
98 -----------------------
100 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id);
101 -- Ada 2005 (AI-251): Add the tag components corresponding to all the
102 -- abstract interface types implemented by a record type or a derived
103 -- record type.
105 procedure Build_Access_Subprogram_Wrapper (Decl : Node_Id);
106 -- When an access-to-subprogram type has pre/postconditions, we build a
107 -- subprogram that includes these contracts and is invoked by an indirect
108 -- call through the corresponding access type.
110 procedure Build_Derived_Type
111 (N : Node_Id;
112 Parent_Type : Entity_Id;
113 Derived_Type : Entity_Id;
114 Is_Completion : Boolean;
115 Derive_Subps : Boolean := True);
116 -- Create and decorate a Derived_Type given the Parent_Type entity. N is
117 -- the N_Full_Type_Declaration node containing the derived type definition.
118 -- Parent_Type is the entity for the parent type in the derived type
119 -- definition and Derived_Type the actual derived type. Is_Completion must
120 -- be set to False if Derived_Type is the N_Defining_Identifier node in N
121 -- (i.e. Derived_Type = Defining_Identifier (N)). In this case N is not the
122 -- completion of a private type declaration. If Is_Completion is set to
123 -- True, N is the completion of a private type declaration and Derived_Type
124 -- is different from the defining identifier inside N (i.e. Derived_Type /=
125 -- Defining_Identifier (N)). Derive_Subps indicates whether the parent
126 -- subprograms should be derived. The only case where this parameter is
127 -- False is when Build_Derived_Type is recursively called to process an
128 -- implicit derived full type for a type derived from a private type (in
129 -- that case the subprograms must only be derived for the private view of
130 -- the type).
132 -- ??? These flags need a bit of re-examination and re-documentation:
133 -- ??? are they both necessary (both seem related to the recursion)?
135 procedure Build_Derived_Access_Type
136 (N : Node_Id;
137 Parent_Type : Entity_Id;
138 Derived_Type : Entity_Id);
139 -- Subsidiary procedure to Build_Derived_Type. For a derived access type,
140 -- create an implicit base if the parent type is constrained or if the
141 -- subtype indication has a constraint.
143 procedure Build_Derived_Array_Type
144 (N : Node_Id;
145 Parent_Type : Entity_Id;
146 Derived_Type : Entity_Id);
147 -- Subsidiary procedure to Build_Derived_Type. For a derived array type,
148 -- create an implicit base if the parent type is constrained or if the
149 -- subtype indication has a constraint.
151 procedure Build_Derived_Concurrent_Type
152 (N : Node_Id;
153 Parent_Type : Entity_Id;
154 Derived_Type : Entity_Id);
155 -- Subsidiary procedure to Build_Derived_Type. For a derived task or
156 -- protected type, inherit entries and protected subprograms, check
157 -- legality of discriminant constraints if any.
159 procedure Build_Derived_Enumeration_Type
160 (N : Node_Id;
161 Parent_Type : Entity_Id;
162 Derived_Type : Entity_Id);
163 -- Subsidiary procedure to Build_Derived_Type. For a derived enumeration
164 -- type, we must create a new list of literals. Types derived from
165 -- Character and [Wide_]Wide_Character are special-cased.
167 procedure Build_Derived_Numeric_Type
168 (N : Node_Id;
169 Parent_Type : Entity_Id;
170 Derived_Type : Entity_Id);
171 -- Subsidiary procedure to Build_Derived_Type. For numeric types, create
172 -- an anonymous base type, and propagate constraint to subtype if needed.
174 procedure Build_Derived_Private_Type
175 (N : Node_Id;
176 Parent_Type : Entity_Id;
177 Derived_Type : Entity_Id;
178 Is_Completion : Boolean;
179 Derive_Subps : Boolean := True);
180 -- Subsidiary procedure to Build_Derived_Type. This procedure is complex
181 -- because the parent may or may not have a completion, and the derivation
182 -- may itself be a completion.
184 procedure Build_Derived_Record_Type
185 (N : Node_Id;
186 Parent_Type : Entity_Id;
187 Derived_Type : Entity_Id;
188 Derive_Subps : Boolean := True);
189 -- Subsidiary procedure used for tagged and untagged record types
190 -- by Build_Derived_Type and Analyze_Private_Extension_Declaration.
191 -- All parameters are as in Build_Derived_Type except that N, in
192 -- addition to being an N_Full_Type_Declaration node, can also be an
193 -- N_Private_Extension_Declaration node. See the definition of this routine
194 -- for much more info. Derive_Subps indicates whether subprograms should be
195 -- derived from the parent type. The only case where Derive_Subps is False
196 -- is for an implicit derived full type for a type derived from a private
197 -- type (see Build_Derived_Type).
199 procedure Build_Discriminal (Discrim : Entity_Id);
200 -- Create the discriminal corresponding to discriminant Discrim, that is
201 -- the parameter corresponding to Discrim to be used in initialization
202 -- procedures for the type where Discrim is a discriminant. Discriminals
203 -- are not used during semantic analysis, and are not fully defined
204 -- entities until expansion. Thus they are not given a scope until
205 -- initialization procedures are built.
207 function Build_Discriminant_Constraints
208 (T : Entity_Id;
209 Def : Node_Id;
210 Derived_Def : Boolean := False) return Elist_Id;
211 -- Validate discriminant constraints and return the list of the constraints
212 -- in order of discriminant declarations, where T is the discriminated
213 -- unconstrained type. Def is the N_Subtype_Indication node where the
214 -- discriminants constraints for T are specified. Derived_Def is True
215 -- when building the discriminant constraints in a derived type definition
216 -- of the form "type D (...) is new T (xxx)". In this case T is the parent
217 -- type and Def is the constraint "(xxx)" on T and this routine sets the
218 -- Corresponding_Discriminant field of the discriminants in the derived
219 -- type D to point to the corresponding discriminants in the parent type T.
221 procedure Build_Discriminated_Subtype
222 (T : Entity_Id;
223 Def_Id : Entity_Id;
224 Elist : Elist_Id;
225 Related_Nod : Node_Id;
226 For_Access : Boolean := False);
227 -- Subsidiary procedure to Constrain_Discriminated_Type and to
228 -- Process_Incomplete_Dependents. Given
230 -- T (a possibly discriminated base type)
231 -- Def_Id (a very partially built subtype for T),
233 -- the call completes Def_Id to be the appropriate E_*_Subtype.
235 -- The Elist is the list of discriminant constraints if any (it is set
236 -- to No_Elist if T is not a discriminated type, and to an empty list if
237 -- T has discriminants but there are no discriminant constraints). The
238 -- Related_Nod is the same as Decl_Node in Create_Constrained_Components.
239 -- The For_Access says whether or not this subtype is really constraining
240 -- an access type.
242 function Build_Scalar_Bound
243 (Bound : Node_Id;
244 Par_T : Entity_Id;
245 Der_T : Entity_Id) return Node_Id;
246 -- The bounds of a derived scalar type are conversions of the bounds of
247 -- the parent type. Optimize the representation if the bounds are literals.
248 -- Needs a more complete spec--what are the parameters exactly, and what
249 -- exactly is the returned value, and how is Bound affected???
251 procedure Check_Access_Discriminant_Requires_Limited
252 (D : Node_Id;
253 Loc : Node_Id);
254 -- Check the restriction that the type to which an access discriminant
255 -- belongs must be a concurrent type or a descendant of a type with
256 -- the reserved word 'limited' in its declaration.
258 procedure Check_Anonymous_Access_Component
259 (Typ_Decl : Node_Id;
260 Typ : Entity_Id;
261 Prev : Entity_Id;
262 Comp_Def : Node_Id;
263 Access_Def : Node_Id);
264 -- Ada 2005 AI-382: an access component in a record definition can refer to
265 -- the enclosing record, in which case it denotes the type itself, and not
266 -- the current instance of the type. We create an anonymous access type for
267 -- the component, and flag it as an access to a component, so accessibility
268 -- checks are properly performed on it. The declaration of the access type
269 -- is placed ahead of that of the record to prevent order-of-elaboration
270 -- circularity issues in Gigi. We create an incomplete type for the record
271 -- declaration, which is the designated type of the anonymous access.
273 procedure Check_Anonymous_Access_Components
274 (Typ_Decl : Node_Id;
275 Typ : Entity_Id;
276 Prev : Entity_Id;
277 Comp_List : Node_Id);
278 -- Call Check_Anonymous_Access_Component on Comp_List
280 procedure Check_Constraining_Discriminant (New_Disc, Old_Disc : Entity_Id);
281 -- Check that, if a new discriminant is used in a constraint defining the
282 -- parent subtype of a derivation, its subtype is statically compatible
283 -- with the subtype of the corresponding parent discriminant (RM 3.7(15)).
285 procedure Check_Delta_Expression (E : Node_Id);
286 -- Check that the expression represented by E is suitable for use as a
287 -- delta expression, i.e. it is of real type and is static.
289 procedure Check_Digits_Expression (E : Node_Id);
290 -- Check that the expression represented by E is suitable for use as a
291 -- digits expression, i.e. it is of integer type, positive and static.
293 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id);
294 -- Validate the initialization of an object declaration. T is the required
295 -- type, and Exp is the initialization expression.
297 procedure Check_Interfaces (N : Node_Id; Def : Node_Id);
298 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
300 procedure Check_Or_Process_Discriminants
301 (N : Node_Id;
302 T : Entity_Id;
303 Prev : Entity_Id := Empty);
304 -- If N is the full declaration of the completion T of an incomplete or
305 -- private type, check its discriminants (which are already known to be
306 -- conformant with those of the partial view, see Find_Type_Name),
307 -- otherwise process them. Prev is the entity of the partial declaration,
308 -- if any.
310 procedure Check_Real_Bound (Bound : Node_Id);
311 -- Check given bound for being of real type and static. If not, post an
312 -- appropriate message, and rewrite the bound with the real literal zero.
314 procedure Constant_Redeclaration
315 (Id : Entity_Id;
316 N : Node_Id;
317 T : out Entity_Id);
318 -- Various checks on legality of full declaration of deferred constant.
319 -- Id is the entity for the redeclaration, N is the N_Object_Declaration,
320 -- node. The caller has not yet set any attributes of this entity.
322 function Contain_Interface
323 (Iface : Entity_Id;
324 Ifaces : Elist_Id) return Boolean;
325 -- Ada 2005: Determine whether Iface is present in the list Ifaces
327 procedure Convert_Scalar_Bounds
328 (N : Node_Id;
329 Parent_Type : Entity_Id;
330 Derived_Type : Entity_Id;
331 Loc : Source_Ptr);
332 -- For derived scalar types, convert the bounds in the type definition to
333 -- the derived type, and complete their analysis. Given a constraint of the
334 -- form ".. new T range Lo .. Hi", Lo and Hi are analyzed and resolved with
335 -- T'Base, the parent_type. The bounds of the derived type (the anonymous
336 -- base) are copies of Lo and Hi. Finally, the bounds of the derived
337 -- subtype are conversions of those bounds to the derived_type, so that
338 -- their typing is consistent.
340 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id);
341 -- Copies attributes from array base type T2 to array base type T1. Copies
342 -- only attributes that apply to base types, but not subtypes.
344 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id);
345 -- Copies attributes from array subtype T2 to array subtype T1. Copies
346 -- attributes that apply to both subtypes and base types.
348 procedure Create_Constrained_Components
349 (Subt : Entity_Id;
350 Decl_Node : Node_Id;
351 Typ : Entity_Id;
352 Constraints : Elist_Id);
353 -- Build the list of entities for a constrained discriminated record
354 -- subtype. If a component depends on a discriminant, replace its subtype
355 -- using the discriminant values in the discriminant constraint. Subt
356 -- is the defining identifier for the subtype whose list of constrained
357 -- entities we will create. Decl_Node is the type declaration node where
358 -- we will attach all the itypes created. Typ is the base discriminated
359 -- type for the subtype Subt. Constraints is the list of discriminant
360 -- constraints for Typ.
362 function Constrain_Component_Type
363 (Comp : Entity_Id;
364 Constrained_Typ : Entity_Id;
365 Related_Node : Node_Id;
366 Typ : Entity_Id;
367 Constraints : Elist_Id) return Entity_Id;
368 -- Given a discriminated base type Typ, a list of discriminant constraints,
369 -- Constraints, for Typ and a component Comp of Typ, create and return the
370 -- type corresponding to Etype (Comp) where all discriminant references
371 -- are replaced with the corresponding constraint. If Etype (Comp) contains
372 -- no discriminant references then it is returned as-is. Constrained_Typ
373 -- is the final constrained subtype to which the constrained component
374 -- belongs. Related_Node is the node where we attach all created itypes.
376 procedure Constrain_Access
377 (Def_Id : in out Entity_Id;
378 S : Node_Id;
379 Related_Nod : Node_Id);
380 -- Apply a list of constraints to an access type. If Def_Id is empty, it is
381 -- an anonymous type created for a subtype indication. In that case it is
382 -- created in the procedure and attached to Related_Nod.
384 procedure Constrain_Array
385 (Def_Id : in out Entity_Id;
386 SI : Node_Id;
387 Related_Nod : Node_Id;
388 Related_Id : Entity_Id;
389 Suffix : Character);
390 -- Apply a list of index constraints to an unconstrained array type. The
391 -- first parameter is the entity for the resulting subtype. A value of
392 -- Empty for Def_Id indicates that an implicit type must be created, but
393 -- creation is delayed (and must be done by this procedure) because other
394 -- subsidiary implicit types must be created first (which is why Def_Id
395 -- is an in/out parameter). The second parameter is a subtype indication
396 -- node for the constrained array to be created (e.g. something of the
397 -- form string (1 .. 10)). Related_Nod gives the place where this type
398 -- has to be inserted in the tree. The Related_Id and Suffix parameters
399 -- are used to build the associated Implicit type name.
401 procedure Constrain_Concurrent
402 (Def_Id : in out Entity_Id;
403 SI : Node_Id;
404 Related_Nod : Node_Id;
405 Related_Id : Entity_Id;
406 Suffix : Character);
407 -- Apply list of discriminant constraints to an unconstrained concurrent
408 -- type.
410 -- SI is the N_Subtype_Indication node containing the constraint and
411 -- the unconstrained type to constrain.
413 -- Def_Id is the entity for the resulting constrained subtype. A value
414 -- of Empty for Def_Id indicates that an implicit type must be created,
415 -- but creation is delayed (and must be done by this procedure) because
416 -- other subsidiary implicit types must be created first (which is why
417 -- Def_Id is an in/out parameter).
419 -- Related_Nod gives the place where this type has to be inserted
420 -- in the tree.
422 -- The last two arguments are used to create its external name if needed.
424 function Constrain_Corresponding_Record
425 (Prot_Subt : Entity_Id;
426 Corr_Rec : Entity_Id;
427 Related_Nod : Node_Id) return Entity_Id;
428 -- When constraining a protected type or task type with discriminants,
429 -- constrain the corresponding record with the same discriminant values.
431 procedure Constrain_Decimal (Def_Id : Entity_Id; S : Node_Id);
432 -- Constrain a decimal fixed point type with a digits constraint and/or a
433 -- range constraint, and build E_Decimal_Fixed_Point_Subtype entity.
435 procedure Constrain_Discriminated_Type
436 (Def_Id : Entity_Id;
437 S : Node_Id;
438 Related_Nod : Node_Id;
439 For_Access : Boolean := False);
440 -- Process discriminant constraints of composite type. Verify that values
441 -- have been provided for all discriminants, that the original type is
442 -- unconstrained, and that the types of the supplied expressions match
443 -- the discriminant types. The first three parameters are like in routine
444 -- Constrain_Concurrent. See Build_Discriminated_Subtype for an explanation
445 -- of For_Access.
447 procedure Constrain_Enumeration (Def_Id : Entity_Id; S : Node_Id);
448 -- Constrain an enumeration type with a range constraint. This is identical
449 -- to Constrain_Integer, but for the Ekind of the resulting subtype.
451 procedure Constrain_Float (Def_Id : Entity_Id; S : Node_Id);
452 -- Constrain a floating point type with either a digits constraint
453 -- and/or a range constraint, building a E_Floating_Point_Subtype.
455 procedure Constrain_Index
456 (Index : Node_Id;
457 S : Node_Id;
458 Related_Nod : Node_Id;
459 Related_Id : Entity_Id;
460 Suffix : Character;
461 Suffix_Index : Pos);
462 -- Process an index constraint S in a constrained array declaration. The
463 -- constraint can be a subtype name, or a range with or without an explicit
464 -- subtype mark. The index is the corresponding index of the unconstrained
465 -- array. The Related_Id and Suffix parameters are used to build the
466 -- associated Implicit type name.
468 procedure Constrain_Integer (Def_Id : Entity_Id; S : Node_Id);
469 -- Build subtype of a signed or modular integer type
471 procedure Constrain_Ordinary_Fixed (Def_Id : Entity_Id; S : Node_Id);
472 -- Constrain an ordinary fixed point type with a range constraint, and
473 -- build an E_Ordinary_Fixed_Point_Subtype entity.
475 procedure Copy_And_Swap (Priv, Full : Entity_Id);
476 -- Copy the Priv entity into the entity of its full declaration then swap
477 -- the two entities in such a manner that the former private type is now
478 -- seen as a full type.
480 procedure Decimal_Fixed_Point_Type_Declaration
481 (T : Entity_Id;
482 Def : Node_Id);
483 -- Create a new decimal fixed point type, and apply the constraint to
484 -- obtain a subtype of this new type.
486 procedure Complete_Private_Subtype
487 (Priv : Entity_Id;
488 Full : Entity_Id;
489 Full_Base : Entity_Id;
490 Related_Nod : Node_Id);
491 -- Complete the implicit full view of a private subtype by setting the
492 -- appropriate semantic fields. If the full view of the parent is a record
493 -- type, build constrained components of subtype.
495 procedure Derive_Progenitor_Subprograms
496 (Parent_Type : Entity_Id;
497 Tagged_Type : Entity_Id);
498 -- Ada 2005 (AI-251): To complete type derivation, collect the primitive
499 -- operations of progenitors of Tagged_Type, and replace the subsidiary
500 -- subtypes with Tagged_Type, to build the specs of the inherited interface
501 -- primitives. The derived primitives are aliased to those of the
502 -- interface. This routine takes care also of transferring to the full view
503 -- subprograms associated with the partial view of Tagged_Type that cover
504 -- interface primitives.
506 procedure Derived_Standard_Character
507 (N : Node_Id;
508 Parent_Type : Entity_Id;
509 Derived_Type : Entity_Id);
510 -- Subsidiary procedure to Build_Derived_Enumeration_Type which handles
511 -- derivations from types Standard.Character and Standard.Wide_Character.
513 procedure Derived_Type_Declaration
514 (T : Entity_Id;
515 N : Node_Id;
516 Is_Completion : Boolean);
517 -- Process a derived type declaration. Build_Derived_Type is invoked
518 -- to process the actual derived type definition. Parameters N and
519 -- Is_Completion have the same meaning as in Build_Derived_Type.
520 -- T is the N_Defining_Identifier for the entity defined in the
521 -- N_Full_Type_Declaration node N, that is T is the derived type.
523 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id);
524 -- Insert each literal in symbol table, as an overloadable identifier. Each
525 -- enumeration type is mapped into a sequence of integers, and each literal
526 -- is defined as a constant with integer value. If any of the literals are
527 -- character literals, the type is a character type, which means that
528 -- strings are legal aggregates for arrays of components of the type.
530 function Expand_To_Stored_Constraint
531 (Typ : Entity_Id;
532 Constraint : Elist_Id) return Elist_Id;
533 -- Given a constraint (i.e. a list of expressions) on the discriminants of
534 -- Typ, expand it into a constraint on the stored discriminants and return
535 -- the new list of expressions constraining the stored discriminants.
537 function Find_Type_Of_Object
538 (Obj_Def : Node_Id;
539 Related_Nod : Node_Id) return Entity_Id;
540 -- Get type entity for object referenced by Obj_Def, attaching the implicit
541 -- types generated to Related_Nod.
543 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id);
544 -- Create a new float and apply the constraint to obtain subtype of it
546 function Has_Range_Constraint (N : Node_Id) return Boolean;
547 -- Given an N_Subtype_Indication node N, return True if a range constraint
548 -- is present, either directly, or as part of a digits or delta constraint.
549 -- In addition, a digits constraint in the decimal case returns True, since
550 -- it establishes a default range if no explicit range is present.
552 function Inherit_Components
553 (N : Node_Id;
554 Parent_Base : Entity_Id;
555 Derived_Base : Entity_Id;
556 Is_Tagged : Boolean;
557 Inherit_Discr : Boolean;
558 Discs : Elist_Id) return Elist_Id;
559 -- Called from Build_Derived_Record_Type to inherit the components of
560 -- Parent_Base (a base type) into the Derived_Base (the derived base type).
561 -- For more information on derived types and component inheritance please
562 -- consult the comment above the body of Build_Derived_Record_Type.
564 -- N is the original derived type declaration
566 -- Is_Tagged is set if we are dealing with tagged types
568 -- If Inherit_Discr is set, Derived_Base inherits its discriminants from
569 -- Parent_Base, otherwise no discriminants are inherited.
571 -- Discs gives the list of constraints that apply to Parent_Base in the
572 -- derived type declaration. If Discs is set to No_Elist, then we have
573 -- the following situation:
575 -- type Parent (D1..Dn : ..) is [tagged] record ...;
576 -- type Derived is new Parent [with ...];
578 -- which gets treated as
580 -- type Derived (D1..Dn : ..) is new Parent (D1,..,Dn) [with ...];
582 -- For untagged types the returned value is an association list. The list
583 -- starts from the association (Parent_Base => Derived_Base), and then it
584 -- contains a sequence of the associations of the form
586 -- (Old_Component => New_Component),
588 -- where Old_Component is the Entity_Id of a component in Parent_Base and
589 -- New_Component is the Entity_Id of the corresponding component in
590 -- Derived_Base. For untagged records, this association list is needed when
591 -- copying the record declaration for the derived base. In the tagged case
592 -- the value returned is irrelevant.
594 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean;
595 -- Subsidiary to Check_Abstract_Overriding and Derive_Subprogram.
596 -- Determine whether subprogram Subp is a procedure subject to pragma
597 -- Extensions_Visible with value False and has at least one controlling
598 -- parameter of mode OUT.
600 function Is_Private_Primitive (Prim : Entity_Id) return Boolean;
601 -- Subsidiary to Check_Abstract_Overriding and Derive_Subprogram.
602 -- When applied to a primitive subprogram Prim, returns True if Prim is
603 -- declared as a private operation within a package or generic package,
604 -- and returns False otherwise.
606 function Is_Valid_Constraint_Kind
607 (T_Kind : Type_Kind;
608 Constraint_Kind : Node_Kind) return Boolean;
609 -- Returns True if it is legal to apply the given kind of constraint to the
610 -- given kind of type (index constraint to an array type, for example).
612 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id);
613 -- Create new modular type. Verify that modulus is in bounds
615 procedure New_Concatenation_Op (Typ : Entity_Id);
616 -- Create an abbreviated declaration for an operator in order to
617 -- materialize concatenation on array types.
619 procedure Ordinary_Fixed_Point_Type_Declaration
620 (T : Entity_Id;
621 Def : Node_Id);
622 -- Create a new ordinary fixed point type, and apply the constraint to
623 -- obtain subtype of it.
625 procedure Preanalyze_Default_Expression (N : Node_Id; T : Entity_Id);
626 -- Wrapper on Preanalyze_Spec_Expression for default expressions, so that
627 -- In_Default_Expr can be properly adjusted.
629 procedure Prepare_Private_Subtype_Completion
630 (Id : Entity_Id;
631 Related_Nod : Node_Id);
632 -- Id is a subtype of some private type. Creates the full declaration
633 -- associated with Id whenever possible, i.e. when the full declaration
634 -- of the base type is already known. Records each subtype into
635 -- Private_Dependents of the base type.
637 procedure Process_Incomplete_Dependents
638 (N : Node_Id;
639 Full_T : Entity_Id;
640 Inc_T : Entity_Id);
641 -- Process all entities that depend on an incomplete type. There include
642 -- subtypes, subprogram types that mention the incomplete type in their
643 -- profiles, and subprogram with access parameters that designate the
644 -- incomplete type.
646 -- Inc_T is the defining identifier of an incomplete type declaration, its
647 -- Ekind is E_Incomplete_Type.
649 -- N is the corresponding N_Full_Type_Declaration for Inc_T.
651 -- Full_T is N's defining identifier.
653 -- Subtypes of incomplete types with discriminants are completed when the
654 -- parent type is. This is simpler than private subtypes, because they can
655 -- only appear in the same scope, and there is no need to exchange views.
656 -- Similarly, access_to_subprogram types may have a parameter or a return
657 -- type that is an incomplete type, and that must be replaced with the
658 -- full type.
660 -- If the full type is tagged, subprogram with access parameters that
661 -- designated the incomplete may be primitive operations of the full type,
662 -- and have to be processed accordingly.
664 procedure Process_Real_Range_Specification (Def : Node_Id);
665 -- Given the type definition for a real type, this procedure processes and
666 -- checks the real range specification of this type definition if one is
667 -- present. If errors are found, error messages are posted, and the
668 -- Real_Range_Specification of Def is reset to Empty.
670 procedure Record_Type_Declaration
671 (T : Entity_Id;
672 N : Node_Id;
673 Prev : Entity_Id);
674 -- Process a record type declaration (for both untagged and tagged
675 -- records). Parameters T and N are exactly like in procedure
676 -- Derived_Type_Declaration, except that no flag Is_Completion is needed
677 -- for this routine. If this is the completion of an incomplete type
678 -- declaration, Prev is the entity of the incomplete declaration, used for
679 -- cross-referencing. Otherwise Prev = T.
681 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id);
682 -- This routine is used to process the actual record type definition (both
683 -- for untagged and tagged records). Def is a record type definition node.
684 -- This procedure analyzes the components in this record type definition.
685 -- Prev_T is the entity for the enclosing record type. It is provided so
686 -- that its Has_Task flag can be set if any of the component have Has_Task
687 -- set. If the declaration is the completion of an incomplete type
688 -- declaration, Prev_T is the original incomplete type, whose full view is
689 -- the record type.
691 procedure Replace_Discriminants (Typ : Entity_Id; Decl : Node_Id);
692 -- Subsidiary to Build_Derived_Record_Type. For untagged record types, we
693 -- first create the list of components for the derived type from that of
694 -- the parent by means of Inherit_Components and then build a copy of the
695 -- declaration tree of the parent with the help of the mapping returned by
696 -- Inherit_Components, which will for example be used to validate record
697 -- representation clauses given for the derived type. If the parent type
698 -- is private and has discriminants, the ancestor discriminants used in the
699 -- inheritance are that of the private declaration, whereas the ancestor
700 -- discriminants present in the declaration tree of the parent are that of
701 -- the full declaration; as a consequence, the remapping done during the
702 -- copy will leave the references to the ancestor discriminants unchanged
703 -- in the declaration tree and they need to be fixed up. If the derived
704 -- type has a known discriminant part, then the remapping done during the
705 -- copy will only create references to the stored discriminants and they
706 -- need to be replaced with references to the non-stored discriminants.
708 procedure Set_Fixed_Range
709 (E : Entity_Id;
710 Loc : Source_Ptr;
711 Lo : Ureal;
712 Hi : Ureal);
713 -- Build a range node with the given bounds and set it as the Scalar_Range
714 -- of the given fixed-point type entity. Loc is the source location used
715 -- for the constructed range. See body for further details.
717 procedure Set_Scalar_Range_For_Subtype
718 (Def_Id : Entity_Id;
719 R : Node_Id;
720 Subt : Entity_Id);
721 -- This routine is used to set the scalar range field for a subtype given
722 -- Def_Id, the entity for the subtype, and R, the range expression for the
723 -- scalar range. Subt provides the parent subtype to be used to analyze,
724 -- resolve, and check the given range.
726 procedure Set_Default_SSO (T : Entity_Id);
727 -- T is the entity for an array or record being declared. This procedure
728 -- sets the flags SSO_Set_Low_By_Default/SSO_Set_High_By_Default according
729 -- to the setting of Opt.Default_SSO.
731 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id);
732 -- Create a new signed integer entity, and apply the constraint to obtain
733 -- the required first named subtype of this type.
735 procedure Set_Stored_Constraint_From_Discriminant_Constraint
736 (E : Entity_Id);
737 -- E is some record type. This routine computes E's Stored_Constraint
738 -- from its Discriminant_Constraint.
740 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id);
741 -- Check that an entity in a list of progenitors is an interface,
742 -- emit error otherwise.
744 procedure Warn_On_Inherently_Limited_Type (E : Entity_Id);
745 -- Emit a warning if a record type that does not have a limited keyword in
746 -- its definition has any components that are limited (which implicitly
747 -- make the type limited).
749 -----------------------
750 -- Access_Definition --
751 -----------------------
753 function Access_Definition
754 (Related_Nod : Node_Id;
755 N : Node_Id) return Entity_Id
757 Anon_Type : Entity_Id;
758 Anon_Scope : Entity_Id;
759 Desig_Type : Entity_Id;
760 Enclosing_Prot_Type : Entity_Id := Empty;
762 begin
763 if Is_Entry (Current_Scope)
764 and then Is_Task_Type (Etype (Scope (Current_Scope)))
765 then
766 Error_Msg_N ("task entries cannot have access parameters", N);
767 return Empty;
768 end if;
770 -- Ada 2005: For an object declaration the corresponding anonymous
771 -- type is declared in the current scope.
773 -- If the access definition is the return type of another access to
774 -- function, scope is the current one, because it is the one of the
775 -- current type declaration, except for the pathological case below.
777 if Nkind (Related_Nod) in
778 N_Object_Declaration | N_Access_Function_Definition
779 then
780 Anon_Scope := Current_Scope;
782 -- A pathological case: function returning access functions that
783 -- return access functions, etc. Each anonymous access type created
784 -- is in the enclosing scope of the outermost function.
786 declare
787 Par : Node_Id;
789 begin
790 Par := Related_Nod;
791 while Nkind (Par) in
792 N_Access_Function_Definition | N_Access_Definition
793 loop
794 Par := Parent (Par);
795 end loop;
797 if Nkind (Par) = N_Function_Specification then
798 Anon_Scope := Scope (Defining_Entity (Par));
799 end if;
800 end;
802 -- For the anonymous function result case, retrieve the scope of the
803 -- function specification's associated entity rather than using the
804 -- current scope. The current scope will be the function itself if the
805 -- formal part is currently being analyzed, but will be the parent scope
806 -- in the case of a parameterless function, and we always want to use
807 -- the function's parent scope. Finally, if the function is a child
808 -- unit, we must traverse the tree to retrieve the proper entity.
810 elsif Nkind (Related_Nod) = N_Function_Specification
811 and then Nkind (Parent (N)) /= N_Parameter_Specification
812 then
813 -- If the current scope is a protected type, the anonymous access
814 -- is associated with one of the protected operations, and must
815 -- be available in the scope that encloses the protected declaration.
816 -- Otherwise the type is in the scope enclosing the subprogram.
818 -- If the function has formals, the return type of a subprogram
819 -- declaration is analyzed in the scope of the subprogram (see
820 -- Process_Formals) and thus the protected type, if present, is
821 -- the scope of the current function scope.
823 if Ekind (Current_Scope) = E_Protected_Type then
824 Enclosing_Prot_Type := Current_Scope;
826 elsif Ekind (Current_Scope) = E_Function
827 and then Ekind (Scope (Current_Scope)) = E_Protected_Type
828 then
829 Enclosing_Prot_Type := Scope (Current_Scope);
830 end if;
832 if Present (Enclosing_Prot_Type) then
833 Anon_Scope := Scope (Enclosing_Prot_Type);
835 else
836 Anon_Scope := Scope (Defining_Entity (Related_Nod));
837 end if;
839 -- For an access type definition, if the current scope is a child
840 -- unit it is the scope of the type.
842 elsif Is_Compilation_Unit (Current_Scope) then
843 Anon_Scope := Current_Scope;
845 -- For access formals, access components, and access discriminants, the
846 -- scope is that of the enclosing declaration,
848 else
849 Anon_Scope := Scope (Current_Scope);
850 end if;
852 Anon_Type :=
853 Create_Itype
854 (E_Anonymous_Access_Type, Related_Nod, Scope_Id => Anon_Scope);
856 if All_Present (N)
857 and then Ada_Version >= Ada_2005
858 then
859 Error_Msg_N ("ALL not permitted for anonymous access types", N);
860 end if;
862 -- Ada 2005 (AI-254): In case of anonymous access to subprograms call
863 -- the corresponding semantic routine
865 if Present (Access_To_Subprogram_Definition (N)) then
866 Access_Subprogram_Declaration
867 (T_Name => Anon_Type,
868 T_Def => Access_To_Subprogram_Definition (N));
870 if Ekind (Anon_Type) = E_Access_Protected_Subprogram_Type then
871 Mutate_Ekind
872 (Anon_Type, E_Anonymous_Access_Protected_Subprogram_Type);
873 else
874 Mutate_Ekind (Anon_Type, E_Anonymous_Access_Subprogram_Type);
875 end if;
877 -- If the anonymous access is associated with a protected operation,
878 -- create a reference to it after the enclosing protected definition
879 -- because the itype will be used in the subsequent bodies.
881 -- If the anonymous access itself is protected, a full type
882 -- declaratiton will be created for it, so that the equivalent
883 -- record type can be constructed. For further details, see
884 -- Replace_Anonymous_Access_To_Protected-Subprogram.
886 if Ekind (Current_Scope) = E_Protected_Type
887 and then not Protected_Present (Access_To_Subprogram_Definition (N))
888 then
889 Build_Itype_Reference (Anon_Type, Parent (Current_Scope));
890 end if;
892 return Anon_Type;
893 end if;
895 Find_Type (Subtype_Mark (N));
896 Desig_Type := Entity (Subtype_Mark (N));
898 Set_Directly_Designated_Type (Anon_Type, Desig_Type);
899 Set_Etype (Anon_Type, Anon_Type);
901 -- Make sure the anonymous access type has size and alignment fields
902 -- set, as required by gigi. This is necessary in the case of the
903 -- Task_Body_Procedure.
905 if not Has_Private_Component (Desig_Type) then
906 Layout_Type (Anon_Type);
907 end if;
909 -- Ada 2005 (AI-231): Ada 2005 semantics for anonymous access differs
910 -- from Ada 95 semantics. In Ada 2005, anonymous access must specify if
911 -- the null value is allowed. In Ada 95 the null value is never allowed.
913 if Ada_Version >= Ada_2005 then
914 Set_Can_Never_Be_Null (Anon_Type, Null_Exclusion_Present (N));
915 else
916 Set_Can_Never_Be_Null (Anon_Type, True);
917 end if;
919 -- The anonymous access type is as public as the discriminated type or
920 -- subprogram that defines it. It is imported (for back-end purposes)
921 -- if the designated type is.
923 Set_Is_Public (Anon_Type, Is_Public (Scope (Anon_Type)));
925 -- Ada 2005 (AI-231): Propagate the access-constant attribute
927 Set_Is_Access_Constant (Anon_Type, Constant_Present (N));
929 -- The context is either a subprogram declaration, object declaration,
930 -- or an access discriminant, in a private or a full type declaration.
931 -- In the case of a subprogram, if the designated type is incomplete,
932 -- the operation will be a primitive operation of the full type, to be
933 -- updated subsequently. If the type is imported through a limited_with
934 -- clause, the subprogram is not a primitive operation of the type
935 -- (which is declared elsewhere in some other scope).
937 if Ekind (Desig_Type) = E_Incomplete_Type
938 and then not From_Limited_With (Desig_Type)
939 and then Is_Overloadable (Current_Scope)
940 then
941 Append_Elmt (Current_Scope, Private_Dependents (Desig_Type));
942 Set_Has_Delayed_Freeze (Current_Scope);
943 end if;
945 -- If the designated type is limited and class-wide, the object might
946 -- contain tasks, so we create a Master entity for the declaration. This
947 -- must be done before expansion of the full declaration, because the
948 -- declaration may include an expression that is an allocator, whose
949 -- expansion needs the proper Master for the created tasks.
951 if Expander_Active
952 and then Nkind (Related_Nod) = N_Object_Declaration
953 then
954 if Is_Limited_Record (Desig_Type)
955 and then Is_Class_Wide_Type (Desig_Type)
956 then
957 Build_Class_Wide_Master (Anon_Type);
959 -- Similarly, if the type is an anonymous access that designates
960 -- tasks, create a master entity for it in the current context.
962 elsif Has_Task (Desig_Type)
963 and then Comes_From_Source (Related_Nod)
964 then
965 Build_Master_Entity (Defining_Identifier (Related_Nod));
966 Build_Master_Renaming (Anon_Type);
967 end if;
968 end if;
970 -- For a private component of a protected type, it is imperative that
971 -- the back-end elaborate the type immediately after the protected
972 -- declaration, because this type will be used in the declarations
973 -- created for the component within each protected body, so we must
974 -- create an itype reference for it now.
976 if Nkind (Parent (Related_Nod)) = N_Protected_Definition then
977 Build_Itype_Reference (Anon_Type, Parent (Parent (Related_Nod)));
979 -- Similarly, if the access definition is the return result of a
980 -- function, create an itype reference for it because it will be used
981 -- within the function body. For a regular function that is not a
982 -- compilation unit, insert reference after the declaration. For a
983 -- protected operation, insert it after the enclosing protected type
984 -- declaration. In either case, do not create a reference for a type
985 -- obtained through a limited_with clause, because this would introduce
986 -- semantic dependencies.
988 -- Similarly, do not create a reference if the designated type is a
989 -- generic formal, because no use of it will reach the backend.
991 elsif Nkind (Related_Nod) = N_Function_Specification
992 and then not From_Limited_With (Desig_Type)
993 and then not Is_Generic_Type (Desig_Type)
994 then
995 if Present (Enclosing_Prot_Type) then
996 Build_Itype_Reference (Anon_Type, Parent (Enclosing_Prot_Type));
998 elsif Is_List_Member (Parent (Related_Nod))
999 and then Nkind (Parent (N)) /= N_Parameter_Specification
1000 then
1001 Build_Itype_Reference (Anon_Type, Parent (Related_Nod));
1002 end if;
1004 -- Finally, create an itype reference for an object declaration of an
1005 -- anonymous access type. This is strictly necessary only for deferred
1006 -- constants, but in any case will avoid out-of-scope problems in the
1007 -- back-end.
1009 elsif Nkind (Related_Nod) = N_Object_Declaration then
1010 Build_Itype_Reference (Anon_Type, Related_Nod);
1011 end if;
1013 return Anon_Type;
1014 end Access_Definition;
1016 -----------------------------------
1017 -- Access_Subprogram_Declaration --
1018 -----------------------------------
1020 procedure Access_Subprogram_Declaration
1021 (T_Name : Entity_Id;
1022 T_Def : Node_Id)
1024 procedure Check_For_Premature_Usage (Def : Node_Id);
1025 -- Check that type T_Name is not used, directly or recursively, as a
1026 -- parameter or a return type in Def. Def is either a subtype, an
1027 -- access_definition, or an access_to_subprogram_definition.
1029 -------------------------------
1030 -- Check_For_Premature_Usage --
1031 -------------------------------
1033 procedure Check_For_Premature_Usage (Def : Node_Id) is
1034 Param : Node_Id;
1036 begin
1037 -- Check for a subtype mark
1039 if Nkind (Def) in N_Has_Etype then
1040 if Etype (Def) = T_Name then
1041 Error_Msg_N
1042 ("type& cannot be used before the end of its declaration",
1043 Def);
1044 end if;
1046 -- If this is not a subtype, then this is an access_definition
1048 elsif Nkind (Def) = N_Access_Definition then
1049 if Present (Access_To_Subprogram_Definition (Def)) then
1050 Check_For_Premature_Usage
1051 (Access_To_Subprogram_Definition (Def));
1052 else
1053 Check_For_Premature_Usage (Subtype_Mark (Def));
1054 end if;
1056 -- The only cases left are N_Access_Function_Definition and
1057 -- N_Access_Procedure_Definition.
1059 else
1060 if Present (Parameter_Specifications (Def)) then
1061 Param := First (Parameter_Specifications (Def));
1062 while Present (Param) loop
1063 Check_For_Premature_Usage (Parameter_Type (Param));
1064 Next (Param);
1065 end loop;
1066 end if;
1068 if Nkind (Def) = N_Access_Function_Definition then
1069 Check_For_Premature_Usage (Result_Definition (Def));
1070 end if;
1071 end if;
1072 end Check_For_Premature_Usage;
1074 -- Local variables
1076 Formals : constant List_Id := Parameter_Specifications (T_Def);
1077 Formal : Entity_Id;
1078 D_Ityp : Node_Id;
1079 Desig_Type : constant Entity_Id :=
1080 Create_Itype (E_Subprogram_Type, Parent (T_Def));
1082 -- Start of processing for Access_Subprogram_Declaration
1084 begin
1085 -- Associate the Itype node with the inner full-type declaration or
1086 -- subprogram spec or entry body. This is required to handle nested
1087 -- anonymous declarations. For example:
1089 -- procedure P
1090 -- (X : access procedure
1091 -- (Y : access procedure
1092 -- (Z : access T)))
1094 D_Ityp := Associated_Node_For_Itype (Desig_Type);
1095 while Nkind (D_Ityp) not in N_Full_Type_Declaration
1096 | N_Private_Type_Declaration
1097 | N_Private_Extension_Declaration
1098 | N_Procedure_Specification
1099 | N_Function_Specification
1100 | N_Entry_Body
1101 | N_Object_Declaration
1102 | N_Object_Renaming_Declaration
1103 | N_Formal_Object_Declaration
1104 | N_Formal_Type_Declaration
1105 | N_Task_Type_Declaration
1106 | N_Protected_Type_Declaration
1107 loop
1108 D_Ityp := Parent (D_Ityp);
1109 pragma Assert (D_Ityp /= Empty);
1110 end loop;
1112 Set_Associated_Node_For_Itype (Desig_Type, D_Ityp);
1114 if Nkind (D_Ityp) in N_Procedure_Specification | N_Function_Specification
1115 then
1116 Set_Scope (Desig_Type, Scope (Defining_Entity (D_Ityp)));
1118 elsif Nkind (D_Ityp) in N_Full_Type_Declaration
1119 | N_Object_Declaration
1120 | N_Object_Renaming_Declaration
1121 | N_Formal_Type_Declaration
1122 then
1123 Set_Scope (Desig_Type, Scope (Defining_Identifier (D_Ityp)));
1124 end if;
1126 if Nkind (T_Def) = N_Access_Function_Definition then
1127 if Nkind (Result_Definition (T_Def)) = N_Access_Definition then
1128 declare
1129 Acc : constant Node_Id := Result_Definition (T_Def);
1131 begin
1132 if Present (Access_To_Subprogram_Definition (Acc))
1133 and then
1134 Protected_Present (Access_To_Subprogram_Definition (Acc))
1135 then
1136 Set_Etype
1137 (Desig_Type,
1138 Replace_Anonymous_Access_To_Protected_Subprogram
1139 (T_Def));
1141 else
1142 Set_Etype
1143 (Desig_Type,
1144 Access_Definition (T_Def, Result_Definition (T_Def)));
1145 end if;
1146 end;
1148 else
1149 Analyze (Result_Definition (T_Def));
1151 declare
1152 Typ : constant Entity_Id := Entity (Result_Definition (T_Def));
1154 begin
1155 -- If a null exclusion is imposed on the result type, then
1156 -- create a null-excluding itype (an access subtype) and use
1157 -- it as the function's Etype.
1159 if Is_Access_Type (Typ)
1160 and then Null_Exclusion_In_Return_Present (T_Def)
1161 then
1162 Set_Etype (Desig_Type,
1163 Create_Null_Excluding_Itype
1164 (T => Typ,
1165 Related_Nod => T_Def,
1166 Scope_Id => Current_Scope));
1168 else
1169 if From_Limited_With (Typ) then
1171 -- AI05-151: Incomplete types are allowed in all basic
1172 -- declarations, including access to subprograms.
1174 if Ada_Version >= Ada_2012 then
1175 null;
1177 else
1178 Error_Msg_NE
1179 ("illegal use of incomplete type&",
1180 Result_Definition (T_Def), Typ);
1181 end if;
1183 elsif Ekind (Current_Scope) = E_Package
1184 and then In_Private_Part (Current_Scope)
1185 then
1186 if Ekind (Typ) = E_Incomplete_Type then
1187 Append_Elmt (Desig_Type, Private_Dependents (Typ));
1189 elsif Is_Class_Wide_Type (Typ)
1190 and then Ekind (Etype (Typ)) = E_Incomplete_Type
1191 then
1192 Append_Elmt
1193 (Desig_Type, Private_Dependents (Etype (Typ)));
1194 end if;
1195 end if;
1197 Set_Etype (Desig_Type, Typ);
1198 end if;
1199 end;
1200 end if;
1202 if not Is_Type (Etype (Desig_Type)) then
1203 Error_Msg_N
1204 ("expect type in function specification",
1205 Result_Definition (T_Def));
1206 end if;
1208 else
1209 Set_Etype (Desig_Type, Standard_Void_Type);
1210 end if;
1212 if Present (Formals) then
1213 Push_Scope (Desig_Type);
1215 -- Some special tests here. These special tests can be removed
1216 -- if and when Itypes always have proper parent pointers to their
1217 -- declarations???
1219 -- Special test 1) Link defining_identifier of formals. Required by
1220 -- First_Formal to provide its functionality.
1222 declare
1223 F : Node_Id;
1225 begin
1226 F := First (Formals);
1228 while Present (F) loop
1229 if No (Parent (Defining_Identifier (F))) then
1230 Set_Parent (Defining_Identifier (F), F);
1231 end if;
1233 Next (F);
1234 end loop;
1235 end;
1237 Process_Formals (Formals, Parent (T_Def));
1239 -- Special test 2) End_Scope requires that the parent pointer be set
1240 -- to something reasonable, but Itypes don't have parent pointers. So
1241 -- we set it and then unset it ???
1243 Set_Parent (Desig_Type, T_Name);
1244 End_Scope;
1245 Set_Parent (Desig_Type, Empty);
1246 end if;
1248 -- Check for premature usage of the type being defined
1250 Check_For_Premature_Usage (T_Def);
1252 -- The return type and/or any parameter type may be incomplete. Mark the
1253 -- subprogram_type as depending on the incomplete type, so that it can
1254 -- be updated when the full type declaration is seen. This only applies
1255 -- to incomplete types declared in some enclosing scope, not to limited
1256 -- views from other packages.
1258 -- Prior to Ada 2012, all parameters of an access-to-function type must
1259 -- be of mode 'in'.
1261 if Present (Formals) then
1262 Formal := First_Formal (Desig_Type);
1263 while Present (Formal) loop
1264 if Ekind (Formal) /= E_In_Parameter
1265 and then Nkind (T_Def) = N_Access_Function_Definition
1266 and then Ada_Version < Ada_2012
1267 then
1268 Error_Msg_N ("functions can only have IN parameters", Formal);
1269 end if;
1271 if Ekind (Etype (Formal)) = E_Incomplete_Type
1272 and then In_Open_Scopes (Scope (Etype (Formal)))
1273 then
1274 Append_Elmt (Desig_Type, Private_Dependents (Etype (Formal)));
1275 Set_Has_Delayed_Freeze (Desig_Type);
1276 end if;
1278 Next_Formal (Formal);
1279 end loop;
1280 end if;
1282 -- Check whether an indirect call without actuals may be possible. This
1283 -- is used when resolving calls whose result is then indexed.
1285 May_Need_Actuals (Desig_Type);
1287 -- If the return type is incomplete, this is legal as long as the type
1288 -- is declared in the current scope and will be completed in it (rather
1289 -- than being part of limited view).
1291 if Ekind (Etype (Desig_Type)) = E_Incomplete_Type
1292 and then not Has_Delayed_Freeze (Desig_Type)
1293 and then In_Open_Scopes (Scope (Etype (Desig_Type)))
1294 then
1295 Append_Elmt (Desig_Type, Private_Dependents (Etype (Desig_Type)));
1296 Set_Has_Delayed_Freeze (Desig_Type);
1297 end if;
1299 Check_Delayed_Subprogram (Desig_Type);
1301 if Protected_Present (T_Def) then
1302 Mutate_Ekind (T_Name, E_Access_Protected_Subprogram_Type);
1303 Set_Convention (Desig_Type, Convention_Protected);
1304 else
1305 Mutate_Ekind (T_Name, E_Access_Subprogram_Type);
1306 end if;
1308 Set_Can_Use_Internal_Rep (T_Name,
1309 not Always_Compatible_Rep_On_Target);
1310 Set_Etype (T_Name, T_Name);
1311 Reinit_Size_Align (T_Name);
1312 Set_Directly_Designated_Type (T_Name, Desig_Type);
1314 -- If the access_to_subprogram is not declared at the library level,
1315 -- it can only point to subprograms that are at the same or deeper
1316 -- accessibility level. The corresponding subprogram type might
1317 -- require an activation record when compiling for C.
1319 Set_Needs_Activation_Record (Desig_Type,
1320 not Is_Library_Level_Entity (T_Name));
1322 Generate_Reference_To_Formals (T_Name);
1324 -- Ada 2005 (AI-231): Propagate the null-excluding attribute
1326 Set_Can_Never_Be_Null (T_Name, Null_Exclusion_Present (T_Def));
1328 Check_Restriction (No_Access_Subprograms, T_Def);
1330 -- Addition of extra formals must be delayed till the freeze point so
1331 -- that we know the convention.
1332 end Access_Subprogram_Declaration;
1334 ----------------------------
1335 -- Access_Type_Declaration --
1336 ----------------------------
1338 procedure Access_Type_Declaration (T : Entity_Id; Def : Node_Id) is
1340 procedure Setup_Access_Type (Desig_Typ : Entity_Id);
1341 -- After type declaration is analysed with T being an incomplete type,
1342 -- this routine will mutate the kind of T to the appropriate access type
1343 -- and set its directly designated type to Desig_Typ.
1345 -----------------------
1346 -- Setup_Access_Type --
1347 -----------------------
1349 procedure Setup_Access_Type (Desig_Typ : Entity_Id) is
1350 begin
1351 if All_Present (Def) or else Constant_Present (Def) then
1352 Mutate_Ekind (T, E_General_Access_Type);
1353 else
1354 Mutate_Ekind (T, E_Access_Type);
1355 end if;
1357 Set_Directly_Designated_Type (T, Desig_Typ);
1358 end Setup_Access_Type;
1360 -- Local variables
1362 P : constant Node_Id := Parent (Def);
1363 S : constant Node_Id := Subtype_Indication (Def);
1365 Full_Desig : Entity_Id;
1367 -- Start of processing for Access_Type_Declaration
1369 begin
1370 -- Check for permissible use of incomplete type
1372 if Nkind (S) /= N_Subtype_Indication then
1374 Analyze (S);
1376 if Nkind (S) in N_Has_Entity
1377 and then Present (Entity (S))
1378 and then Ekind (Root_Type (Entity (S))) = E_Incomplete_Type
1379 then
1380 Setup_Access_Type (Desig_Typ => Entity (S));
1382 -- If the designated type is a limited view, we cannot tell if
1383 -- the full view contains tasks, and there is no way to handle
1384 -- that full view in a client. We create a master entity for the
1385 -- scope, which will be used when a client determines that one
1386 -- is needed.
1388 if From_Limited_With (Entity (S))
1389 and then not Is_Class_Wide_Type (Entity (S))
1390 then
1391 Build_Master_Entity (T);
1392 Build_Master_Renaming (T);
1393 end if;
1395 else
1396 Setup_Access_Type (Desig_Typ => Process_Subtype (S, P, T, 'P'));
1397 end if;
1399 -- If the access definition is of the form: ACCESS NOT NULL ..
1400 -- the subtype indication must be of an access type. Create
1401 -- a null-excluding subtype of it.
1403 if Null_Excluding_Subtype (Def) then
1404 if not Is_Access_Type (Entity (S)) then
1405 Error_Msg_N ("null exclusion must apply to access type", Def);
1407 else
1408 declare
1409 Loc : constant Source_Ptr := Sloc (S);
1410 Decl : Node_Id;
1411 Nam : constant Entity_Id := Make_Temporary (Loc, 'S');
1413 begin
1414 Decl :=
1415 Make_Subtype_Declaration (Loc,
1416 Defining_Identifier => Nam,
1417 Subtype_Indication =>
1418 New_Occurrence_Of (Entity (S), Loc));
1419 Set_Null_Exclusion_Present (Decl);
1420 Insert_Before (Parent (Def), Decl);
1421 Analyze (Decl);
1422 Set_Entity (S, Nam);
1423 end;
1424 end if;
1425 end if;
1427 else
1428 Setup_Access_Type (Desig_Typ => Process_Subtype (S, P, T, 'P'));
1429 end if;
1431 if not Error_Posted (T) then
1432 Full_Desig := Designated_Type (T);
1434 if Base_Type (Full_Desig) = T then
1435 Error_Msg_N ("access type cannot designate itself", S);
1437 -- In Ada 2005, the type may have a limited view through some unit in
1438 -- its own context, allowing the following circularity that cannot be
1439 -- detected earlier.
1441 elsif Is_Class_Wide_Type (Full_Desig) and then Etype (Full_Desig) = T
1442 then
1443 Error_Msg_N
1444 ("access type cannot designate its own class-wide type", S);
1446 -- Clean up indication of tagged status to prevent cascaded errors
1448 Set_Is_Tagged_Type (T, False);
1449 end if;
1451 Set_Etype (T, T);
1452 end if;
1454 -- If the type has appeared already in a with_type clause, it is frozen
1455 -- and the pointer size is already set. Else, initialize.
1457 if not From_Limited_With (T) then
1458 Reinit_Size_Align (T);
1459 end if;
1461 -- Note that Has_Task is always false, since the access type itself
1462 -- is not a task type. See Einfo for more description on this point.
1463 -- Exactly the same consideration applies to Has_Controlled_Component
1464 -- and to Has_Protected.
1466 Set_Has_Task (T, False);
1467 Set_Has_Protected (T, False);
1468 Set_Has_Timing_Event (T, False);
1469 Set_Has_Controlled_Component (T, False);
1471 -- Initialize field Finalization_Collection explicitly to Empty to avoid
1472 -- problems where an incomplete view of this entity has been previously
1473 -- established by a limited with and an overlaid version of this field
1474 -- (Stored_Constraint) was initialized for the incomplete view.
1476 -- This reset is performed in most cases except where the access type
1477 -- has been created for the purposes of allocating or deallocating a
1478 -- build-in-place object. Such access types have explicitly set pools
1479 -- and finalization collections.
1481 if No (Associated_Storage_Pool (T)) then
1482 Set_Finalization_Collection (T, Empty);
1483 end if;
1485 -- Ada 2005 (AI-231): Propagate the null-excluding and access-constant
1486 -- attributes
1488 Set_Can_Never_Be_Null (T, Null_Exclusion_Present (Def));
1489 Set_Is_Access_Constant (T, Constant_Present (Def));
1490 end Access_Type_Declaration;
1492 ----------------------------------
1493 -- Add_Interface_Tag_Components --
1494 ----------------------------------
1496 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id) is
1497 Loc : constant Source_Ptr := Sloc (N);
1498 L : List_Id;
1499 Last_Tag : Node_Id;
1501 procedure Add_Tag (Iface : Entity_Id);
1502 -- Add tag for one of the progenitor interfaces
1504 -------------
1505 -- Add_Tag --
1506 -------------
1508 procedure Add_Tag (Iface : Entity_Id) is
1509 Decl : Node_Id;
1510 Def : Node_Id;
1511 Tag : Entity_Id;
1512 Offset : Entity_Id;
1514 begin
1515 pragma Assert (Is_Tagged_Type (Iface) and then Is_Interface (Iface));
1517 -- This is a reasonable place to propagate predicates
1519 if Has_Predicates (Iface) then
1520 Set_Has_Predicates (Typ);
1521 end if;
1523 Def :=
1524 Make_Component_Definition (Loc,
1525 Aliased_Present => True,
1526 Subtype_Indication =>
1527 New_Occurrence_Of (RTE (RE_Interface_Tag), Loc));
1529 Tag := Make_Temporary (Loc, 'V');
1531 Decl :=
1532 Make_Component_Declaration (Loc,
1533 Defining_Identifier => Tag,
1534 Component_Definition => Def);
1536 Analyze_Component_Declaration (Decl);
1538 Set_Analyzed (Decl);
1539 Mutate_Ekind (Tag, E_Component);
1540 Set_Is_Tag (Tag);
1541 Set_Is_Aliased (Tag);
1542 Set_Is_Independent (Tag);
1543 Set_Related_Type (Tag, Iface);
1544 Reinit_Component_Location (Tag);
1546 pragma Assert (Is_Frozen (Iface));
1548 Set_DT_Entry_Count (Tag,
1549 DT_Entry_Count (First_Entity (Iface)));
1551 if No (Last_Tag) then
1552 Prepend (Decl, L);
1553 else
1554 Insert_After (Last_Tag, Decl);
1555 end if;
1557 Last_Tag := Decl;
1559 -- If the ancestor has discriminants we need to give special support
1560 -- to store the offset_to_top value of the secondary dispatch tables.
1561 -- For this purpose we add a supplementary component just after the
1562 -- field that contains the tag associated with each secondary DT.
1564 if Typ /= Etype (Typ) and then Has_Discriminants (Etype (Typ)) then
1565 Def :=
1566 Make_Component_Definition (Loc,
1567 Subtype_Indication =>
1568 New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
1570 Offset := Make_Temporary (Loc, 'V');
1572 Decl :=
1573 Make_Component_Declaration (Loc,
1574 Defining_Identifier => Offset,
1575 Component_Definition => Def);
1577 Analyze_Component_Declaration (Decl);
1579 Set_Analyzed (Decl);
1580 Mutate_Ekind (Offset, E_Component);
1581 Set_Is_Aliased (Offset);
1582 Set_Is_Independent (Offset);
1583 Set_Related_Type (Offset, Iface);
1584 Reinit_Component_Location (Offset);
1585 Insert_After (Last_Tag, Decl);
1586 Last_Tag := Decl;
1587 end if;
1588 end Add_Tag;
1590 -- Local variables
1592 Elmt : Elmt_Id;
1593 Ext : Node_Id;
1594 Comp : Node_Id;
1596 -- Start of processing for Add_Interface_Tag_Components
1598 begin
1599 if not RTE_Available (RE_Interface_Tag) then
1600 Error_Msg_N
1601 ("(Ada 2005) interface types not supported by this run-time!", N);
1602 return;
1603 end if;
1605 if Ekind (Typ) /= E_Record_Type
1606 or else (Is_Concurrent_Record_Type (Typ)
1607 and then Is_Empty_List (Abstract_Interface_List (Typ)))
1608 or else (not Is_Concurrent_Record_Type (Typ)
1609 and then No (Interfaces (Typ))
1610 and then Is_Empty_Elmt_List (Interfaces (Typ)))
1611 then
1612 return;
1613 end if;
1615 -- Find the current last tag
1617 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1618 Ext := Record_Extension_Part (Type_Definition (N));
1619 else
1620 pragma Assert (Nkind (Type_Definition (N)) = N_Record_Definition);
1621 Ext := Type_Definition (N);
1622 end if;
1624 Last_Tag := Empty;
1626 if No (Component_List (Ext)) then
1627 Set_Null_Present (Ext, False);
1628 L := New_List;
1629 Set_Component_List (Ext,
1630 Make_Component_List (Loc,
1631 Component_Items => L,
1632 Null_Present => False));
1633 else
1634 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1635 L := Component_Items
1636 (Component_List
1637 (Record_Extension_Part
1638 (Type_Definition (N))));
1639 else
1640 L := Component_Items
1641 (Component_List
1642 (Type_Definition (N)));
1643 end if;
1645 -- Find the last tag component
1647 Comp := First (L);
1648 while Present (Comp) loop
1649 if Nkind (Comp) = N_Component_Declaration
1650 and then Is_Tag (Defining_Identifier (Comp))
1651 then
1652 Last_Tag := Comp;
1653 end if;
1655 Next (Comp);
1656 end loop;
1657 end if;
1659 -- At this point L references the list of components and Last_Tag
1660 -- references the current last tag (if any). Now we add the tag
1661 -- corresponding with all the interfaces that are not implemented
1662 -- by the parent.
1664 if Present (Interfaces (Typ)) then
1665 Elmt := First_Elmt (Interfaces (Typ));
1666 while Present (Elmt) loop
1667 Add_Tag (Node (Elmt));
1668 Next_Elmt (Elmt);
1669 end loop;
1670 end if;
1671 end Add_Interface_Tag_Components;
1673 -------------------------------------
1674 -- Add_Internal_Interface_Entities --
1675 -------------------------------------
1677 procedure Add_Internal_Interface_Entities (Tagged_Type : Entity_Id) is
1679 function Error_Posted_In_Formals (Subp : Entity_Id) return Boolean;
1680 -- Determine if an error has been posted in some formal of Subp.
1682 -----------------------------
1683 -- Error_Posted_In_Formals --
1684 -----------------------------
1686 function Error_Posted_In_Formals (Subp : Entity_Id) return Boolean is
1687 Formal : Entity_Id := First_Formal (Subp);
1689 begin
1690 while Present (Formal) loop
1691 if Error_Posted (Formal) then
1692 return True;
1693 end if;
1695 Next_Formal (Formal);
1696 end loop;
1698 return False;
1699 end Error_Posted_In_Formals;
1701 -- Local variables
1703 Elmt : Elmt_Id;
1704 Iface : Entity_Id;
1705 Iface_Elmt : Elmt_Id;
1706 Iface_Prim : Entity_Id;
1707 Ifaces_List : Elist_Id;
1708 New_Subp : Entity_Id := Empty;
1709 Prim : Entity_Id;
1710 Restore_Scope : Boolean := False;
1712 begin
1713 pragma Assert (Ada_Version >= Ada_2005
1714 and then Is_Record_Type (Tagged_Type)
1715 and then Is_Tagged_Type (Tagged_Type)
1716 and then Has_Interfaces (Tagged_Type)
1717 and then not Is_Interface (Tagged_Type));
1719 -- Ensure that the internal entities are added to the scope of the type
1721 if Scope (Tagged_Type) /= Current_Scope then
1722 Push_Scope (Scope (Tagged_Type));
1723 Restore_Scope := True;
1724 end if;
1726 Collect_Interfaces (Tagged_Type, Ifaces_List);
1728 Iface_Elmt := First_Elmt (Ifaces_List);
1729 while Present (Iface_Elmt) loop
1730 Iface := Node (Iface_Elmt);
1732 -- Originally we excluded here from this processing interfaces that
1733 -- are parents of Tagged_Type because their primitives are located
1734 -- in the primary dispatch table (and hence no auxiliary internal
1735 -- entities are required to handle secondary dispatch tables in such
1736 -- case). However, these auxiliary entities are also required to
1737 -- handle derivations of interfaces in formals of generics (see
1738 -- Derive_Subprograms).
1740 Elmt := First_Elmt (Primitive_Operations (Iface));
1741 while Present (Elmt) loop
1742 Iface_Prim := Node (Elmt);
1744 if not Is_Predefined_Dispatching_Operation (Iface_Prim) then
1745 Prim :=
1746 Find_Primitive_Covering_Interface
1747 (Tagged_Type => Tagged_Type,
1748 Iface_Prim => Iface_Prim);
1750 if No (Prim) and then Serious_Errors_Detected > 0 then
1751 goto Continue;
1752 end if;
1754 pragma Assert (Present (Prim));
1756 -- Check subtype conformance; we skip this check if errors have
1757 -- been reported in the primitive (or in the formals of the
1758 -- primitive) because Find_Primitive_Covering_Interface relies
1759 -- on the subprogram Type_Conformant to locate the primitive,
1760 -- and reports errors if the formals don't match.
1762 if not Error_Posted (Prim)
1763 and then not Error_Posted_In_Formals (Prim)
1764 then
1765 declare
1766 Alias_Prim : Entity_Id;
1767 Alias_Typ : Entity_Id;
1768 Err_Loc : Node_Id := Empty;
1769 Ret_Type : Entity_Id;
1771 begin
1772 -- For inherited primitives, in case of reporting an
1773 -- error, the error must be reported on this primitive
1774 -- (i.e. in the name of its type declaration); otherwise
1775 -- the error would be reported in the formal of the
1776 -- alias primitive defined on its parent type.
1778 if Nkind (Parent (Prim)) = N_Full_Type_Declaration then
1779 Err_Loc := Prim;
1780 end if;
1782 -- Check subtype conformance of procedures, functions
1783 -- with matching return type, or functions not returning
1784 -- interface types.
1786 if Ekind (Prim) = E_Procedure
1787 or else Etype (Iface_Prim) = Etype (Prim)
1788 or else not Is_Interface (Etype (Iface_Prim))
1789 then
1790 Check_Subtype_Conformant
1791 (New_Id => Prim,
1792 Old_Id => Iface_Prim,
1793 Err_Loc => Err_Loc,
1794 Skip_Controlling_Formals => True);
1796 -- Check subtype conformance of functions returning an
1797 -- interface type; temporarily force both entities to
1798 -- return the same type. Required because subprogram
1799 -- Subtype_Conformant does not handle this case.
1801 else
1802 Ret_Type := Etype (Iface_Prim);
1803 Set_Etype (Iface_Prim, Etype (Prim));
1805 Check_Subtype_Conformant
1806 (New_Id => Prim,
1807 Old_Id => Iface_Prim,
1808 Err_Loc => Err_Loc,
1809 Skip_Controlling_Formals => True);
1811 Set_Etype (Iface_Prim, Ret_Type);
1812 end if;
1814 -- Complete the error when reported on inherited
1815 -- primitives.
1817 if Nkind (Parent (Prim)) = N_Full_Type_Declaration
1818 and then (Error_Posted (Prim)
1819 or else Error_Posted_In_Formals (Prim))
1820 and then Present (Alias (Prim))
1821 then
1822 Alias_Prim := Ultimate_Alias (Prim);
1823 Alias_Typ := Find_Dispatching_Type (Alias_Prim);
1825 if Alias_Typ /= Tagged_Type
1826 and then Is_Ancestor (Alias_Typ, Tagged_Type)
1827 then
1828 Error_Msg_Sloc := Sloc (Alias_Prim);
1829 Error_Msg_N
1830 ("in primitive inherited from #!", Prim);
1831 end if;
1832 end if;
1833 end;
1834 end if;
1836 -- Ada 2012 (AI05-0197): If the name of the covering primitive
1837 -- differs from the name of the interface primitive then it is
1838 -- a private primitive inherited from a parent type. In such
1839 -- case, given that Tagged_Type covers the interface, the
1840 -- inherited private primitive becomes visible. For such
1841 -- purpose we add a new entity that renames the inherited
1842 -- private primitive.
1844 if Chars (Prim) /= Chars (Iface_Prim) then
1845 pragma Assert (Has_Suffix (Prim, 'P'));
1846 Derive_Subprogram
1847 (New_Subp => New_Subp,
1848 Parent_Subp => Iface_Prim,
1849 Derived_Type => Tagged_Type,
1850 Parent_Type => Iface);
1851 Set_Alias (New_Subp, Prim);
1852 Set_Is_Abstract_Subprogram
1853 (New_Subp, Is_Abstract_Subprogram (Prim));
1854 end if;
1856 Derive_Subprogram
1857 (New_Subp => New_Subp,
1858 Parent_Subp => Iface_Prim,
1859 Derived_Type => Tagged_Type,
1860 Parent_Type => Iface);
1862 declare
1863 Anc : Entity_Id;
1864 begin
1865 if Is_Inherited_Operation (Prim)
1866 and then Present (Alias (Prim))
1867 then
1868 Anc := Alias (Prim);
1869 else
1870 Anc := Overridden_Operation (Prim);
1871 end if;
1873 -- Apply legality checks in RM 6.1.1 (10-13) concerning
1874 -- nonconforming preconditions in both an ancestor and
1875 -- a progenitor operation.
1877 -- If the operation is a primitive wrapper it is an explicit
1878 -- (overriding) operqtion and all is fine.
1880 if Present (Anc)
1881 and then Has_Non_Trivial_Precondition (Anc)
1882 and then Has_Non_Trivial_Precondition (Iface_Prim)
1883 then
1884 if Is_Abstract_Subprogram (Prim)
1885 or else
1886 (Ekind (Prim) = E_Procedure
1887 and then Nkind (Parent (Prim)) =
1888 N_Procedure_Specification
1889 and then Null_Present (Parent (Prim)))
1890 or else Is_Primitive_Wrapper (Prim)
1891 then
1892 null;
1894 -- The operation is inherited and must be overridden
1896 elsif not Comes_From_Source (Prim) then
1897 Error_Msg_NE
1898 ("&inherits non-conforming preconditions and must "
1899 & "be overridden (RM 6.1.1 (10-16))",
1900 Parent (Tagged_Type), Prim);
1901 end if;
1902 end if;
1903 end;
1905 -- Ada 2005 (AI-251): Decorate internal entity Iface_Subp
1906 -- associated with interface types. These entities are
1907 -- only registered in the list of primitives of its
1908 -- corresponding tagged type because they are only used
1909 -- to fill the contents of the secondary dispatch tables.
1910 -- Therefore they are removed from the homonym chains.
1912 Set_Is_Hidden (New_Subp);
1913 Set_Is_Internal (New_Subp);
1914 Set_Alias (New_Subp, Prim);
1915 Set_Is_Abstract_Subprogram
1916 (New_Subp, Is_Abstract_Subprogram (Prim));
1917 Set_Interface_Alias (New_Subp, Iface_Prim);
1919 -- If the returned type is an interface then propagate it to
1920 -- the returned type. Needed by the thunk to generate the code
1921 -- which displaces "this" to reference the corresponding
1922 -- secondary dispatch table in the returned object.
1924 if Is_Interface (Etype (Iface_Prim)) then
1925 Set_Etype (New_Subp, Etype (Iface_Prim));
1926 end if;
1928 -- Internal entities associated with interface types are only
1929 -- registered in the list of primitives of the tagged type.
1930 -- They are only used to fill the contents of the secondary
1931 -- dispatch tables. Therefore they are not needed in the
1932 -- homonym chains.
1934 Remove_Homonym (New_Subp);
1936 -- Hidden entities associated with interfaces must have set
1937 -- the Has_Delay_Freeze attribute to ensure that, in case
1938 -- of locally defined tagged types (or compiling with static
1939 -- dispatch tables generation disabled) the corresponding
1940 -- entry of the secondary dispatch table is filled when such
1941 -- an entity is frozen.
1943 Set_Has_Delayed_Freeze (New_Subp);
1944 end if;
1946 <<Continue>>
1947 Next_Elmt (Elmt);
1948 end loop;
1950 Next_Elmt (Iface_Elmt);
1951 end loop;
1953 if Restore_Scope then
1954 Pop_Scope;
1955 end if;
1956 end Add_Internal_Interface_Entities;
1958 -----------------------------------
1959 -- Analyze_Component_Declaration --
1960 -----------------------------------
1962 procedure Analyze_Component_Declaration (N : Node_Id) is
1963 Id : constant Entity_Id := Defining_Identifier (N);
1964 E : constant Node_Id := Expression (N);
1965 Typ : constant Node_Id :=
1966 Subtype_Indication (Component_Definition (N));
1967 T : Entity_Id;
1968 P : Entity_Id;
1970 function Is_Known_Limited (Typ : Entity_Id) return Boolean;
1971 -- Typ is the type of the current component, check whether this type is
1972 -- a limited type. Used to validate declaration against that of
1973 -- enclosing record.
1975 procedure Add_Range_Checks (Subt_Indic : Node_Id);
1976 -- Adds range constraint checks for a subtype indication
1978 ----------------------
1979 -- Is_Known_Limited --
1980 ----------------------
1982 function Is_Known_Limited (Typ : Entity_Id) return Boolean is
1983 P : constant Entity_Id := Etype (Typ);
1984 R : constant Entity_Id := Root_Type (Typ);
1986 begin
1987 if Is_Limited_Record (Typ) then
1988 return True;
1990 -- If the root type is limited (and not a limited interface) so is
1991 -- the current type.
1993 elsif Is_Limited_Record (R)
1994 and then (not Is_Interface (R) or else not Is_Limited_Interface (R))
1995 then
1996 return True;
1998 -- Else the type may have a limited interface progenitor, but a
1999 -- limited record parent that is not an interface.
2001 elsif R /= P
2002 and then Is_Limited_Record (P)
2003 and then not Is_Interface (P)
2004 then
2005 return True;
2007 else
2008 return False;
2009 end if;
2010 end Is_Known_Limited;
2012 ----------------------
2013 -- Add_Range_Checks --
2014 ----------------------
2016 procedure Add_Range_Checks (Subt_Indic : Node_Id)
2019 begin
2020 if Present (Subt_Indic) and then
2021 Nkind (Subt_Indic) = N_Subtype_Indication and then
2022 Nkind (Constraint (Subt_Indic)) = N_Index_Or_Discriminant_Constraint
2023 then
2025 declare
2026 Typ : constant Entity_Id := Entity (Subtype_Mark (Subt_Indic));
2027 Indic_Typ : constant Entity_Id := Underlying_Type (Typ);
2028 Subt_Index : Node_Id;
2029 Target_Index : Node_Id;
2030 begin
2032 if Present (Indic_Typ) and then Is_Array_Type (Indic_Typ) then
2034 Target_Index := First_Index (Indic_Typ);
2035 Subt_Index := First (Constraints (Constraint (Subt_Indic)));
2037 while Present (Target_Index) loop
2038 if Nkind (Subt_Index) in N_Expanded_Name | N_Identifier
2039 and then Is_Scalar_Type (Entity (Subt_Index))
2040 and then
2041 Nkind (Scalar_Range (Entity (Subt_Index))) = N_Range
2042 then
2043 Apply_Range_Check
2044 (Expr => Scalar_Range (Entity (Subt_Index)),
2045 Target_Typ => Etype (Target_Index),
2046 Insert_Node => Subt_Indic);
2047 end if;
2049 Next (Subt_Index);
2050 Next_Index (Target_Index);
2051 end loop;
2052 end if;
2053 end;
2054 end if;
2055 end Add_Range_Checks;
2057 -- Start of processing for Analyze_Component_Declaration
2059 begin
2060 Generate_Definition (Id);
2061 Enter_Name (Id);
2063 if Present (Typ) then
2064 T := Find_Type_Of_Object
2065 (Subtype_Indication (Component_Definition (N)), N);
2067 -- Ada 2005 (AI-230): Access Definition case
2069 else
2070 pragma Assert (Present
2071 (Access_Definition (Component_Definition (N))));
2073 T := Access_Definition
2074 (Related_Nod => N,
2075 N => Access_Definition (Component_Definition (N)));
2076 Set_Is_Local_Anonymous_Access (T);
2078 -- Ada 2005 (AI-254)
2080 if Present (Access_To_Subprogram_Definition
2081 (Access_Definition (Component_Definition (N))))
2082 and then Protected_Present (Access_To_Subprogram_Definition
2083 (Access_Definition
2084 (Component_Definition (N))))
2085 then
2086 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
2087 end if;
2088 end if;
2090 -- If the subtype is a constrained subtype of the enclosing record,
2091 -- (which must have a partial view) the back-end does not properly
2092 -- handle the recursion. Rewrite the component declaration with an
2093 -- explicit subtype indication, which is acceptable to Gigi. We can copy
2094 -- the tree directly because side effects have already been removed from
2095 -- discriminant constraints.
2097 if Ekind (T) = E_Access_Subtype
2098 and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
2099 and then Comes_From_Source (T)
2100 and then Nkind (Parent (T)) = N_Subtype_Declaration
2101 and then Etype (Directly_Designated_Type (T)) = Current_Scope
2102 then
2103 Rewrite
2104 (Subtype_Indication (Component_Definition (N)),
2105 New_Copy_Tree (Subtype_Indication (Parent (T))));
2106 T := Find_Type_Of_Object
2107 (Subtype_Indication (Component_Definition (N)), N);
2108 end if;
2110 -- If the component declaration includes a default expression, then we
2111 -- check that the component is not of a limited type (RM 3.7(5)),
2112 -- and do the special preanalysis of the expression (see section on
2113 -- "Handling of Default and Per-Object Expressions" in the spec of
2114 -- package Sem).
2116 if Present (E) then
2117 Preanalyze_Default_Expression (E, T);
2118 Check_Initialization (T, E);
2120 if Ada_Version >= Ada_2005
2121 and then Ekind (T) = E_Anonymous_Access_Type
2122 and then Etype (E) /= Any_Type
2123 then
2124 -- Check RM 3.9.2(9): "if the expected type for an expression is
2125 -- an anonymous access-to-specific tagged type, then the object
2126 -- designated by the expression shall not be dynamically tagged
2127 -- unless it is a controlling operand in a call on a dispatching
2128 -- operation"
2130 if Is_Tagged_Type (Directly_Designated_Type (T))
2131 and then
2132 Ekind (Directly_Designated_Type (T)) /= E_Class_Wide_Type
2133 and then
2134 Ekind (Directly_Designated_Type (Etype (E))) =
2135 E_Class_Wide_Type
2136 then
2137 Error_Msg_N
2138 ("access to specific tagged type required (RM 3.9.2(9))", E);
2139 end if;
2141 -- (Ada 2005: AI-230): Accessibility check for anonymous
2142 -- components
2144 if Type_Access_Level (Etype (E)) >
2145 Deepest_Type_Access_Level (T)
2146 then
2147 Error_Msg_N
2148 ("expression has deeper access level than component " &
2149 "(RM 3.10.2 (12.2))", E);
2150 end if;
2152 -- The initialization expression is a reference to an access
2153 -- discriminant. The type of the discriminant is always deeper
2154 -- than any access type.
2156 if Ekind (Etype (E)) = E_Anonymous_Access_Type
2157 and then Is_Entity_Name (E)
2158 and then Ekind (Entity (E)) = E_In_Parameter
2159 and then Present (Discriminal_Link (Entity (E)))
2160 then
2161 Error_Msg_N
2162 ("discriminant has deeper accessibility level than target",
2164 end if;
2165 end if;
2166 end if;
2168 -- The parent type may be a private view with unknown discriminants,
2169 -- and thus unconstrained. Regular components must be constrained.
2171 if not Is_Definite_Subtype (T)
2172 and then not Is_Mutably_Tagged_Type (T)
2173 and then Chars (Id) /= Name_uParent
2174 then
2175 if Is_Class_Wide_Type (T) then
2176 Error_Msg_N
2177 ("class-wide subtype with unknown discriminants" &
2178 " in component declaration",
2179 Subtype_Indication (Component_Definition (N)));
2180 else
2181 Error_Msg_N
2182 ("unconstrained subtype in component declaration",
2183 Subtype_Indication (Component_Definition (N)));
2184 end if;
2186 -- Components cannot be abstract, except for the special case of
2187 -- the _Parent field (case of extending an abstract tagged type)
2189 elsif Is_Abstract_Type (T) and then Chars (Id) /= Name_uParent then
2190 Error_Msg_N ("type of a component cannot be abstract", N);
2191 end if;
2193 Set_Etype (Id, T);
2195 if Aliased_Present (Component_Definition (N)) then
2196 Set_Is_Aliased (Id);
2198 -- AI12-001: All aliased objects are considered to be specified as
2199 -- independently addressable (RM C.6(8.1/4)).
2201 Set_Is_Independent (Id);
2202 end if;
2204 -- The component declaration may have a per-object constraint, set
2205 -- the appropriate flag in the defining identifier of the subtype.
2207 if Has_Discriminant_Dependent_Constraint (Id) then
2208 Set_Has_Per_Object_Constraint (Id);
2209 end if;
2211 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
2212 -- out some static checks.
2214 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (T) then
2215 Null_Exclusion_Static_Checks (N);
2216 end if;
2218 -- If this component is private (or depends on a private type), flag the
2219 -- record type to indicate that some operations are not available.
2221 P := Private_Component (T);
2223 if Present (P) then
2225 -- Check for circular definitions
2227 if P = Any_Type then
2228 Set_Etype (Id, Any_Type);
2230 -- There is a gap in the visibility of operations only if the
2231 -- component type is not defined in the scope of the record type.
2233 elsif Scope (P) = Scope (Current_Scope) then
2234 null;
2236 elsif Is_Limited_Type (P) then
2237 Set_Is_Limited_Composite (Current_Scope);
2239 else
2240 Set_Is_Private_Composite (Current_Scope);
2241 end if;
2242 end if;
2244 if P /= Any_Type
2245 and then Is_Limited_Type (T)
2246 and then Chars (Id) /= Name_uParent
2247 and then Is_Tagged_Type (Current_Scope)
2248 then
2249 if Is_Derived_Type (Current_Scope)
2250 and then not Is_Known_Limited (Current_Scope)
2251 then
2252 Error_Msg_N
2253 ("extension of nonlimited type cannot have limited components",
2256 if Is_Interface (Root_Type (Current_Scope)) then
2257 Error_Msg_N
2258 ("\limitedness is not inherited from limited interface", N);
2259 Error_Msg_N ("\add LIMITED to type indication", N);
2260 end if;
2262 Explain_Limited_Type (T, N);
2263 Set_Etype (Id, Any_Type);
2264 Set_Is_Limited_Composite (Current_Scope, False);
2266 elsif not Is_Derived_Type (Current_Scope)
2267 and then not Is_Limited_Record (Current_Scope)
2268 and then not Is_Concurrent_Type (Current_Scope)
2269 then
2270 Error_Msg_N
2271 ("nonlimited tagged type cannot have limited components", N);
2272 Explain_Limited_Type (T, N);
2273 Set_Etype (Id, Any_Type);
2274 Set_Is_Limited_Composite (Current_Scope, False);
2275 end if;
2276 end if;
2278 Set_Original_Record_Component (Id, Id);
2280 Analyze_Aspect_Specifications (N, Id);
2282 Analyze_Dimension (N);
2284 Add_Range_Checks (Subtype_Indication (Component_Definition (N)));
2286 end Analyze_Component_Declaration;
2288 --------------------------
2289 -- Analyze_Declarations --
2290 --------------------------
2292 procedure Analyze_Declarations (L : List_Id) is
2293 Decl : Node_Id;
2295 procedure Adjust_Decl;
2296 -- Adjust Decl not to include implicit label declarations, since these
2297 -- have strange Sloc values that result in elaboration check problems.
2298 -- (They have the sloc of the label as found in the source, and that
2299 -- is ahead of the current declarative part).
2301 procedure Build_Assertion_Bodies (Decls : List_Id; Context : Node_Id);
2302 -- Create the subprogram bodies which verify the run-time semantics of
2303 -- the pragmas listed below for each elibigle type found in declarative
2304 -- list Decls. The pragmas are:
2306 -- Default_Initial_Condition
2307 -- Invariant
2308 -- Type_Invariant
2310 -- Context denotes the owner of the declarative list.
2312 procedure Check_Entry_Contracts;
2313 -- Perform a preanalysis of the pre- and postconditions of an entry
2314 -- declaration. This must be done before full resolution and creation
2315 -- of the parameter block, etc. to catch illegal uses within the
2316 -- contract expression. Full analysis of the expression is done when
2317 -- the contract is processed.
2319 function Contains_Lib_Incomplete_Type (Pkg : Entity_Id) return Boolean;
2320 -- Check if a nested package has entities within it that rely on library
2321 -- level private types where the full view has not been completed for
2322 -- the purposes of checking if it is acceptable to freeze an expression
2323 -- function at the point of declaration.
2325 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id);
2326 -- Determine whether Body_Decl denotes the body of a late controlled
2327 -- primitive (either Initialize, Adjust or Finalize). If this is the
2328 -- case, add a proper spec if the body lacks one. The spec is inserted
2329 -- before Body_Decl and immediately analyzed.
2331 procedure Remove_Partial_Visible_Refinements (Spec_Id : Entity_Id);
2332 -- Spec_Id is the entity of a package that may define abstract states,
2333 -- and in the case of a child unit, whose ancestors may define abstract
2334 -- states. If the states have partial visible refinement, remove the
2335 -- partial visibility of each constituent at the end of the package
2336 -- spec and body declarations.
2338 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id);
2339 -- Spec_Id is the entity of a package that may define abstract states.
2340 -- If the states have visible refinement, remove the visibility of each
2341 -- constituent at the end of the package body declaration.
2343 procedure Resolve_Aspects;
2344 -- Utility to resolve the expressions of aspects at the end of a list of
2345 -- declarations, or before a declaration that freezes previous entities,
2346 -- such as in a subprogram body.
2348 -----------------
2349 -- Adjust_Decl --
2350 -----------------
2352 procedure Adjust_Decl is
2353 begin
2354 while Present (Prev (Decl))
2355 and then Nkind (Decl) = N_Implicit_Label_Declaration
2356 loop
2357 Prev (Decl);
2358 end loop;
2359 end Adjust_Decl;
2361 ----------------------------
2362 -- Build_Assertion_Bodies --
2363 ----------------------------
2365 procedure Build_Assertion_Bodies (Decls : List_Id; Context : Node_Id) is
2366 procedure Build_Assertion_Bodies_For_Type (Typ : Entity_Id);
2367 -- Create the subprogram bodies which verify the run-time semantics
2368 -- of the pragmas listed below for type Typ. The pragmas are:
2370 -- Default_Initial_Condition
2371 -- Invariant
2372 -- Type_Invariant
2374 -------------------------------------
2375 -- Build_Assertion_Bodies_For_Type --
2376 -------------------------------------
2378 procedure Build_Assertion_Bodies_For_Type (Typ : Entity_Id) is
2379 begin
2380 if Nkind (Context) = N_Package_Specification then
2382 -- Preanalyze and resolve the class-wide invariants of an
2383 -- interface at the end of whichever declarative part has the
2384 -- interface type. Note that an interface may be declared in
2385 -- any non-package declarative part, but reaching the end of
2386 -- such a declarative part will always freeze the type and
2387 -- generate the invariant procedure (see Freeze_Type).
2389 if Is_Interface (Typ) then
2391 -- Interfaces are treated as the partial view of a private
2392 -- type, in order to achieve uniformity with the general
2393 -- case. As a result, an interface receives only a "partial"
2394 -- invariant procedure, which is never called.
2396 if Has_Own_Invariants (Typ) then
2397 Build_Invariant_Procedure_Body
2398 (Typ => Typ,
2399 Partial_Invariant => True);
2400 end if;
2402 elsif Decls = Visible_Declarations (Context) then
2403 -- Preanalyze and resolve the invariants of a private type
2404 -- at the end of the visible declarations to catch potential
2405 -- errors. Inherited class-wide invariants are not included
2406 -- because they have already been resolved.
2408 if Ekind (Typ) in E_Limited_Private_Type
2409 | E_Private_Type
2410 | E_Record_Type_With_Private
2411 and then Has_Own_Invariants (Typ)
2412 then
2413 Build_Invariant_Procedure_Body
2414 (Typ => Typ,
2415 Partial_Invariant => True);
2416 end if;
2418 -- Preanalyze and resolve the Default_Initial_Condition
2419 -- assertion expression at the end of the declarations to
2420 -- catch any errors.
2422 if Ekind (Typ) in E_Limited_Private_Type
2423 | E_Private_Type
2424 | E_Record_Type_With_Private
2425 and then Has_Own_DIC (Typ)
2426 then
2427 Build_DIC_Procedure_Body
2428 (Typ => Typ,
2429 Partial_DIC => True);
2430 end if;
2432 elsif Decls = Private_Declarations (Context) then
2434 -- Preanalyze and resolve the invariants of a private type's
2435 -- full view at the end of the private declarations to catch
2436 -- potential errors.
2438 if (not Is_Private_Type (Typ)
2439 or else Present (Underlying_Full_View (Typ)))
2440 and then Has_Private_Declaration (Typ)
2441 and then Has_Invariants (Typ)
2442 then
2443 Build_Invariant_Procedure_Body (Typ);
2444 end if;
2446 if (not Is_Private_Type (Typ)
2447 or else Present (Underlying_Full_View (Typ)))
2448 and then Has_Private_Declaration (Typ)
2449 and then Has_DIC (Typ)
2450 then
2451 Build_DIC_Procedure_Body (Typ);
2452 end if;
2453 end if;
2454 end if;
2455 end Build_Assertion_Bodies_For_Type;
2457 -- Local variables
2459 Decl : Node_Id;
2460 Decl_Id : Entity_Id;
2462 -- Start of processing for Build_Assertion_Bodies
2464 begin
2465 Decl := First (Decls);
2466 while Present (Decl) loop
2467 if Is_Declaration (Decl) then
2468 Decl_Id := Defining_Entity (Decl);
2470 if Is_Type (Decl_Id) then
2471 Build_Assertion_Bodies_For_Type (Decl_Id);
2472 end if;
2473 end if;
2475 Next (Decl);
2476 end loop;
2477 end Build_Assertion_Bodies;
2479 ---------------------------
2480 -- Check_Entry_Contracts --
2481 ---------------------------
2483 procedure Check_Entry_Contracts is
2484 ASN : Node_Id;
2485 Ent : Entity_Id;
2486 Exp : Node_Id;
2488 begin
2489 Ent := First_Entity (Current_Scope);
2490 while Present (Ent) loop
2492 -- This only concerns entries with pre/postconditions
2494 if Ekind (Ent) = E_Entry
2495 and then Present (Contract (Ent))
2496 and then Present (Pre_Post_Conditions (Contract (Ent)))
2497 then
2498 ASN := Pre_Post_Conditions (Contract (Ent));
2499 Push_Scope (Ent);
2500 Install_Formals (Ent);
2502 -- Pre/postconditions are rewritten as Check pragmas. Analysis
2503 -- is performed on a copy of the pragma expression, to prevent
2504 -- modifying the original expression.
2506 while Present (ASN) loop
2507 if Nkind (ASN) = N_Pragma then
2508 Exp :=
2509 New_Copy_Tree
2510 (Expression
2511 (First (Pragma_Argument_Associations (ASN))));
2512 Set_Parent (Exp, ASN);
2514 Preanalyze_Assert_Expression (Exp, Standard_Boolean);
2515 end if;
2517 ASN := Next_Pragma (ASN);
2518 end loop;
2520 End_Scope;
2521 end if;
2523 Next_Entity (Ent);
2524 end loop;
2525 end Check_Entry_Contracts;
2527 ----------------------------------
2528 -- Contains_Lib_Incomplete_Type --
2529 ----------------------------------
2531 function Contains_Lib_Incomplete_Type (Pkg : Entity_Id) return Boolean is
2532 Curr : Entity_Id;
2534 begin
2535 -- Avoid looking through scopes that do not meet the precondition of
2536 -- Pkg not being within a library unit spec.
2538 if not Is_Compilation_Unit (Pkg)
2539 and then not Is_Generic_Instance (Pkg)
2540 and then not In_Package_Body (Enclosing_Lib_Unit_Entity (Pkg))
2541 then
2542 -- Loop through all entities in the current scope to identify
2543 -- an entity that depends on a private type.
2545 Curr := First_Entity (Pkg);
2546 loop
2547 if Nkind (Curr) in N_Entity
2548 and then Depends_On_Private (Curr)
2549 then
2550 return True;
2551 end if;
2553 exit when Last_Entity (Current_Scope) = Curr;
2554 Next_Entity (Curr);
2555 end loop;
2556 end if;
2558 return False;
2559 end Contains_Lib_Incomplete_Type;
2561 --------------------------------------
2562 -- Handle_Late_Controlled_Primitive --
2563 --------------------------------------
2565 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id) is
2566 Body_Spec : constant Node_Id := Specification (Body_Decl);
2567 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2568 Loc : constant Source_Ptr := Sloc (Body_Id);
2569 Params : constant List_Id :=
2570 Parameter_Specifications (Body_Spec);
2571 Spec : Node_Id;
2572 Spec_Id : Entity_Id;
2573 Typ : Node_Id;
2575 begin
2576 -- Consider only procedure bodies whose name matches one of the three
2577 -- controlled primitives.
2579 if Nkind (Body_Spec) /= N_Procedure_Specification
2580 or else Chars (Body_Id) not in Name_Adjust
2581 | Name_Finalize
2582 | Name_Initialize
2583 then
2584 return;
2586 -- A controlled primitive must have exactly one formal which is not
2587 -- an anonymous access type.
2589 elsif List_Length (Params) /= 1 then
2590 return;
2591 end if;
2593 Typ := Parameter_Type (First (Params));
2595 if Nkind (Typ) = N_Access_Definition then
2596 return;
2597 end if;
2599 Find_Type (Typ);
2601 -- The type of the formal must be derived from [Limited_]Controlled
2603 if not Is_Controlled (Entity (Typ)) then
2604 return;
2605 end if;
2607 -- Check whether a specification exists for this body. We do not
2608 -- analyze the spec of the body in full, because it will be analyzed
2609 -- again when the body is properly analyzed, and we cannot create
2610 -- duplicate entries in the formals chain. We look for an explicit
2611 -- specification because the body may be an overriding operation and
2612 -- an inherited spec may be present.
2614 Spec_Id := Current_Entity (Body_Id);
2616 while Present (Spec_Id) loop
2617 if Ekind (Spec_Id) in E_Procedure | E_Generic_Procedure
2618 and then Scope (Spec_Id) = Current_Scope
2619 and then Present (First_Formal (Spec_Id))
2620 and then No (Next_Formal (First_Formal (Spec_Id)))
2621 and then Etype (First_Formal (Spec_Id)) = Entity (Typ)
2622 and then Comes_From_Source (Spec_Id)
2623 then
2624 return;
2625 end if;
2627 Spec_Id := Homonym (Spec_Id);
2628 end loop;
2630 -- At this point the body is known to be a late controlled primitive.
2631 -- Generate a matching spec and insert it before the body. Note the
2632 -- use of Copy_Separate_Tree - we want an entirely separate semantic
2633 -- tree in this case.
2635 Spec := Copy_Separate_Tree (Body_Spec);
2637 -- Ensure that the subprogram declaration does not inherit the null
2638 -- indicator from the body as we now have a proper spec/body pair.
2640 Set_Null_Present (Spec, False);
2642 -- Ensure that the freeze node is inserted after the declaration of
2643 -- the primitive since its expansion will freeze the primitive.
2645 Decl := Make_Subprogram_Declaration (Loc, Specification => Spec);
2647 Insert_Before_And_Analyze (Body_Decl, Decl);
2648 end Handle_Late_Controlled_Primitive;
2650 ----------------------------------------
2651 -- Remove_Partial_Visible_Refinements --
2652 ----------------------------------------
2654 procedure Remove_Partial_Visible_Refinements (Spec_Id : Entity_Id) is
2655 State_Elmt : Elmt_Id;
2656 begin
2657 if Present (Abstract_States (Spec_Id)) then
2658 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2659 while Present (State_Elmt) loop
2660 Set_Has_Partial_Visible_Refinement (Node (State_Elmt), False);
2661 Next_Elmt (State_Elmt);
2662 end loop;
2663 end if;
2665 -- For a child unit, also hide the partial state refinement from
2666 -- ancestor packages.
2668 if Is_Child_Unit (Spec_Id) then
2669 Remove_Partial_Visible_Refinements (Scope (Spec_Id));
2670 end if;
2671 end Remove_Partial_Visible_Refinements;
2673 --------------------------------
2674 -- Remove_Visible_Refinements --
2675 --------------------------------
2677 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id) is
2678 State_Elmt : Elmt_Id;
2679 begin
2680 if Present (Abstract_States (Spec_Id)) then
2681 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2682 while Present (State_Elmt) loop
2683 Set_Has_Visible_Refinement (Node (State_Elmt), False);
2684 Next_Elmt (State_Elmt);
2685 end loop;
2686 end if;
2687 end Remove_Visible_Refinements;
2689 ---------------------
2690 -- Resolve_Aspects --
2691 ---------------------
2693 procedure Resolve_Aspects is
2694 E : Entity_Id;
2696 begin
2697 E := First_Entity (Current_Scope);
2698 while Present (E) loop
2699 Resolve_Aspect_Expressions (E);
2701 -- Now that the aspect expressions have been resolved, if this is
2702 -- at the end of the visible declarations, we can set the flag
2703 -- Known_To_Have_Preelab_Init properly on types declared in the
2704 -- visible part, which is needed for checking whether full types
2705 -- in the private part satisfy the Preelaborable_Initialization
2706 -- aspect of the partial view. We can't wait for the creation of
2707 -- the pragma by Analyze_Aspects_At_Freeze_Point, because the
2708 -- freeze point may occur after the end of the package declaration
2709 -- (in the case of nested packages).
2711 if Is_Type (E)
2712 and then L = Visible_Declarations (Parent (L))
2713 and then Has_Aspect (E, Aspect_Preelaborable_Initialization)
2714 then
2715 declare
2716 ASN : constant Node_Id :=
2717 Find_Aspect (E, Aspect_Preelaborable_Initialization);
2718 Expr : constant Node_Id := Expression (ASN);
2719 begin
2720 -- Set Known_To_Have_Preelab_Init to True if aspect has no
2721 -- expression, or if the expression is True (or was folded
2722 -- to True), or if the expression is a conjunction of one or
2723 -- more Preelaborable_Initialization attributes applied to
2724 -- formal types and wasn't folded to False. (Note that
2725 -- Is_Conjunction_Of_Formal_Preelab_Init_Attributes goes to
2726 -- Original_Node if needed, hence test for Standard_False.)
2728 if No (Expr)
2729 or else (Is_Entity_Name (Expr)
2730 and then Entity (Expr) = Standard_True)
2731 or else
2732 (Is_Conjunction_Of_Formal_Preelab_Init_Attributes (Expr)
2733 and then
2734 not (Is_Entity_Name (Expr)
2735 and then Entity (Expr) = Standard_False))
2736 then
2737 Set_Known_To_Have_Preelab_Init (E);
2738 end if;
2739 end;
2740 end if;
2742 Next_Entity (E);
2743 end loop;
2744 end Resolve_Aspects;
2746 -- Local variables
2748 Context : Node_Id := Empty;
2749 Ctrl_Typ : Entity_Id := Empty;
2750 Freeze_From : Entity_Id := Empty;
2751 Next_Decl : Node_Id;
2753 -- Start of processing for Analyze_Declarations
2755 begin
2756 Decl := First (L);
2757 while Present (Decl) loop
2759 -- Complete analysis of declaration
2761 Analyze (Decl);
2762 Next_Decl := Next (Decl);
2764 if No (Freeze_From) then
2765 Freeze_From := First_Entity (Current_Scope);
2766 end if;
2768 -- Remember if the declaration we just processed is the full type
2769 -- declaration of a controlled type (to handle late overriding of
2770 -- initialize, adjust or finalize).
2772 if Nkind (Decl) = N_Full_Type_Declaration
2773 and then Is_Controlled (Defining_Identifier (Decl))
2774 then
2775 Ctrl_Typ := Defining_Identifier (Decl);
2776 end if;
2778 -- At the end of a declarative part, freeze remaining entities
2779 -- declared in it. The end of the visible declarations of package
2780 -- specification is not the end of a declarative part if private
2781 -- declarations are present. The end of a package declaration is a
2782 -- freezing point only if it a library package. A task definition or
2783 -- protected type definition is not a freeze point either. Finally,
2784 -- we do not freeze entities in generic scopes, because there is no
2785 -- code generated for them and freeze nodes will be generated for
2786 -- the instance.
2788 -- The end of a package instantiation is not a freeze point, but
2789 -- for now we make it one, because the generic body is inserted
2790 -- (currently) immediately after. Generic instantiations will not
2791 -- be a freeze point once delayed freezing of bodies is implemented.
2792 -- (This is needed in any case for early instantiations ???).
2794 if No (Next_Decl) then
2795 if Nkind (Parent (L)) = N_Component_List then
2796 null;
2798 elsif Nkind (Parent (L)) in
2799 N_Protected_Definition | N_Task_Definition
2800 then
2801 Check_Entry_Contracts;
2803 elsif Nkind (Parent (L)) /= N_Package_Specification then
2804 if Nkind (Parent (L)) = N_Package_Body then
2805 Freeze_From := First_Entity (Current_Scope);
2806 end if;
2808 -- There may have been several freezing points previously,
2809 -- for example object declarations or subprogram bodies, but
2810 -- at the end of a declarative part we check freezing from
2811 -- the beginning, even though entities may already be frozen,
2812 -- in order to perform visibility checks on delayed aspects.
2814 Adjust_Decl;
2816 -- If the current scope is a generic subprogram body. Skip the
2817 -- generic formal parameters that are not frozen here.
2819 if Is_Subprogram (Current_Scope)
2820 and then Nkind (Unit_Declaration_Node (Current_Scope)) =
2821 N_Generic_Subprogram_Declaration
2822 and then Present (First_Entity (Current_Scope))
2823 then
2824 while Is_Generic_Formal (Freeze_From) loop
2825 Next_Entity (Freeze_From);
2826 end loop;
2828 Freeze_All (Freeze_From, Decl);
2829 Freeze_From := Last_Entity (Current_Scope);
2831 else
2832 -- For declarations in a subprogram body there is no issue
2833 -- with name resolution in aspect specifications.
2835 Freeze_All (First_Entity (Current_Scope), Decl);
2836 Freeze_From := Last_Entity (Current_Scope);
2837 end if;
2839 -- Current scope is a package specification
2841 elsif Scope (Current_Scope) /= Standard_Standard
2842 and then not Is_Child_Unit (Current_Scope)
2843 and then No (Generic_Parent (Parent (L)))
2844 then
2845 -- ARM rule 13.1.1(11/3): usage names in aspect definitions are
2846 -- resolved at the end of the immediately enclosing declaration
2847 -- list (AI05-0183-1).
2849 Resolve_Aspects;
2851 elsif L /= Visible_Declarations (Parent (L))
2852 or else Is_Empty_List (Private_Declarations (Parent (L)))
2853 then
2854 Adjust_Decl;
2856 -- End of a package declaration
2858 -- This is a freeze point because it is the end of a
2859 -- compilation unit.
2861 Freeze_All (First_Entity (Current_Scope), Decl);
2862 Freeze_From := Last_Entity (Current_Scope);
2864 -- At the end of the visible declarations the expressions in
2865 -- aspects of all entities declared so far must be resolved.
2866 -- The entities themselves might be frozen later, and the
2867 -- generated pragmas and attribute definition clauses analyzed
2868 -- in full at that point, but name resolution must take place
2869 -- now.
2870 -- In addition to being the proper semantics, this is mandatory
2871 -- within generic units, because global name capture requires
2872 -- those expressions to be analyzed, given that the generated
2873 -- pragmas do not appear in the original generic tree.
2875 elsif Serious_Errors_Detected = 0 then
2876 Resolve_Aspects;
2877 end if;
2879 -- If next node is a body then freeze all types before the body.
2880 -- An exception occurs for some expander-generated bodies. If these
2881 -- are generated at places where in general language rules would not
2882 -- allow a freeze point, then we assume that the expander has
2883 -- explicitly checked that all required types are properly frozen,
2884 -- and we do not cause general freezing here. This special circuit
2885 -- is used when the encountered body is marked as having already
2886 -- been analyzed.
2888 -- In all other cases (bodies that come from source, and expander
2889 -- generated bodies that have not been analyzed yet), freeze all
2890 -- types now. Note that in the latter case, the expander must take
2891 -- care to attach the bodies at a proper place in the tree so as to
2892 -- not cause unwanted freezing at that point.
2894 -- It is also necessary to check for a case where both an expression
2895 -- function is used and the current scope depends on an incomplete
2896 -- private type from a library unit, otherwise premature freezing of
2897 -- the private type will occur.
2899 elsif not Analyzed (Next_Decl) and then Is_Body (Next_Decl)
2900 and then ((Nkind (Next_Decl) /= N_Subprogram_Body
2901 or else not Was_Expression_Function (Next_Decl))
2902 or else (not Is_Ignored_Ghost_Entity (Current_Scope)
2903 and then not Contains_Lib_Incomplete_Type
2904 (Current_Scope)))
2905 then
2906 -- When a controlled type is frozen, the expander generates stream
2907 -- and controlled-type support routines. If the freeze is caused
2908 -- by the stand-alone body of Initialize, Adjust, or Finalize, the
2909 -- expander will end up using the wrong version of these routines,
2910 -- as the body has not been processed yet. To remedy this, detect
2911 -- a late controlled primitive and create a proper spec for it.
2912 -- This ensures that the primitive will override its inherited
2913 -- counterpart before the freeze takes place.
2915 -- If the declaration we just processed is a body, do not attempt
2916 -- to examine Next_Decl as the late primitive idiom can only apply
2917 -- to the first encountered body.
2919 -- ??? A cleaner approach may be possible and/or this solution
2920 -- could be extended to general-purpose late primitives.
2922 if Present (Ctrl_Typ) then
2924 -- No need to continue searching for late body overriding if
2925 -- the controlled type is already frozen.
2927 if Is_Frozen (Ctrl_Typ) then
2928 Ctrl_Typ := Empty;
2930 elsif Nkind (Next_Decl) = N_Subprogram_Body then
2931 Handle_Late_Controlled_Primitive (Next_Decl);
2932 end if;
2933 end if;
2935 Adjust_Decl;
2937 -- The generated body of an expression function does not freeze,
2938 -- unless it is a completion, in which case only the expression
2939 -- itself freezes. This is handled when the body itself is
2940 -- analyzed (see Freeze_Expr_Types, sem_ch6.adb).
2942 Freeze_All (Freeze_From, Decl);
2943 Freeze_From := Last_Entity (Current_Scope);
2944 end if;
2946 Decl := Next_Decl;
2947 end loop;
2949 -- Post-freezing actions
2951 if Present (L) then
2952 Context := Parent (L);
2954 -- Certain contract annotations have forward visibility semantics and
2955 -- must be analyzed after all declarative items have been processed.
2956 -- This timing ensures that entities referenced by such contracts are
2957 -- visible.
2959 -- Analyze the contract of an immediately enclosing package spec or
2960 -- body first because other contracts may depend on its information.
2962 if Nkind (Context) = N_Package_Body then
2963 Analyze_Package_Body_Contract (Defining_Entity (Context));
2965 elsif Nkind (Context) = N_Package_Specification then
2966 Analyze_Package_Contract (Defining_Entity (Context));
2967 end if;
2969 -- Analyze the contracts of various constructs in the declarative
2970 -- list.
2972 Analyze_Contracts (L);
2974 if Nkind (Context) = N_Package_Body then
2976 -- Ensure that all abstract states and objects declared in the
2977 -- state space of a package body are utilized as constituents.
2979 Check_Unused_Body_States (Defining_Entity (Context));
2981 -- State refinements are visible up to the end of the package body
2982 -- declarations. Hide the state refinements from visibility to
2983 -- restore the original state conditions.
2985 Remove_Visible_Refinements (Corresponding_Spec (Context));
2986 Remove_Partial_Visible_Refinements (Corresponding_Spec (Context));
2988 elsif Nkind (Context) = N_Package_Specification then
2990 -- Partial state refinements are visible up to the end of the
2991 -- package spec declarations. Hide the partial state refinements
2992 -- from visibility to restore the original state conditions.
2994 Remove_Partial_Visible_Refinements (Defining_Entity (Context));
2995 end if;
2997 -- Verify that all abstract states found in any package declared in
2998 -- the input declarative list have proper refinements. The check is
2999 -- performed only when the context denotes a block, entry, package,
3000 -- protected, subprogram, or task body (SPARK RM 7.1.4(4) and SPARK
3001 -- RM 7.2.2(3)).
3003 Check_State_Refinements (Context);
3005 -- Create the subprogram bodies which verify the run-time semantics
3006 -- of pragmas Default_Initial_Condition and [Type_]Invariant for all
3007 -- types within the current declarative list. This ensures that all
3008 -- assertion expressions are preanalyzed and resolved at the end of
3009 -- the declarative part. Note that the resolution happens even when
3010 -- freezing does not take place.
3012 Build_Assertion_Bodies (L, Context);
3013 end if;
3014 end Analyze_Declarations;
3016 -----------------------------------
3017 -- Analyze_Full_Type_Declaration --
3018 -----------------------------------
3020 procedure Analyze_Full_Type_Declaration (N : Node_Id) is
3021 Def : constant Node_Id := Type_Definition (N);
3022 Def_Id : constant Entity_Id := Defining_Identifier (N);
3023 T : Entity_Id;
3024 Prev : Entity_Id;
3026 Is_Remote : constant Boolean :=
3027 (Is_Remote_Types (Current_Scope)
3028 or else Is_Remote_Call_Interface (Current_Scope))
3029 and then not (In_Private_Part (Current_Scope)
3030 or else In_Package_Body (Current_Scope));
3032 procedure Check_Nonoverridable_Aspects;
3033 -- Apply the rule in RM 13.1.1(18.4/4) on iterator aspects that cannot
3034 -- be overridden, and can only be confirmed on derivation.
3036 procedure Check_Ops_From_Incomplete_Type;
3037 -- If there is a tagged incomplete partial view of the type, traverse
3038 -- the primitives of the incomplete view and change the type of any
3039 -- controlling formals and result to indicate the full view. The
3040 -- primitives will be added to the full type's primitive operations
3041 -- list later in Sem_Disp.Check_Operation_From_Incomplete_Type (which
3042 -- is called from Process_Incomplete_Dependents).
3044 ----------------------------------
3045 -- Check_Nonoverridable_Aspects --
3046 ----------------------------------
3048 procedure Check_Nonoverridable_Aspects is
3049 function Get_Aspect_Spec
3050 (Specs : List_Id;
3051 Aspect_Name : Name_Id) return Node_Id;
3052 -- Check whether a list of aspect specifications includes an entry
3053 -- for a specific aspect. The list is either that of a partial or
3054 -- a full view.
3056 ---------------------
3057 -- Get_Aspect_Spec --
3058 ---------------------
3060 function Get_Aspect_Spec
3061 (Specs : List_Id;
3062 Aspect_Name : Name_Id) return Node_Id
3064 Spec : Node_Id;
3066 begin
3067 Spec := First (Specs);
3068 while Present (Spec) loop
3069 if Chars (Identifier (Spec)) = Aspect_Name then
3070 return Spec;
3071 end if;
3072 Next (Spec);
3073 end loop;
3075 return Empty;
3076 end Get_Aspect_Spec;
3078 -- Local variables
3080 Prev_Aspects : constant List_Id :=
3081 Aspect_Specifications (Parent (Def_Id));
3082 Par_Type : Entity_Id;
3083 Prev_Aspect : Node_Id;
3085 -- Start of processing for Check_Nonoverridable_Aspects
3087 begin
3088 -- Get parent type of derived type. Note that Prev is the entity in
3089 -- the partial declaration, but its contents are now those of full
3090 -- view, while Def_Id reflects the partial view.
3092 if Is_Private_Type (Def_Id) then
3093 Par_Type := Etype (Full_View (Def_Id));
3094 else
3095 Par_Type := Etype (Def_Id);
3096 end if;
3098 -- If there is an inherited Implicit_Dereference, verify that it is
3099 -- made explicit in the partial view.
3101 if Has_Discriminants (Base_Type (Par_Type))
3102 and then Nkind (Parent (Prev)) = N_Full_Type_Declaration
3103 and then Present (Discriminant_Specifications (Parent (Prev)))
3104 and then Present (Get_Reference_Discriminant (Par_Type))
3105 then
3106 Prev_Aspect :=
3107 Get_Aspect_Spec (Prev_Aspects, Name_Implicit_Dereference);
3109 if No (Prev_Aspect)
3110 and then Present
3111 (Discriminant_Specifications
3112 (Original_Node (Parent (Prev))))
3113 then
3114 Error_Msg_N
3115 ("type does not inherit implicit dereference", Prev);
3117 else
3118 -- If one of the views has the aspect specified, verify that it
3119 -- is consistent with that of the parent.
3121 declare
3122 Cur_Discr : constant Entity_Id :=
3123 Get_Reference_Discriminant (Prev);
3124 Par_Discr : constant Entity_Id :=
3125 Get_Reference_Discriminant (Par_Type);
3127 begin
3128 if Corresponding_Discriminant (Cur_Discr) /= Par_Discr then
3129 Error_Msg_N
3130 ("aspect inconsistent with that of parent", N);
3131 end if;
3133 -- Check that specification in partial view matches the
3134 -- inherited aspect. Compare names directly because aspect
3135 -- expression may not be analyzed.
3137 if Present (Prev_Aspect)
3138 and then Nkind (Expression (Prev_Aspect)) = N_Identifier
3139 and then Chars (Expression (Prev_Aspect)) /=
3140 Chars (Cur_Discr)
3141 then
3142 Error_Msg_N
3143 ("aspect inconsistent with that of parent", N);
3144 end if;
3145 end;
3146 end if;
3147 end if;
3149 -- What about other nonoverridable aspects???
3150 end Check_Nonoverridable_Aspects;
3152 ------------------------------------
3153 -- Check_Ops_From_Incomplete_Type --
3154 ------------------------------------
3156 procedure Check_Ops_From_Incomplete_Type is
3157 Elmt : Elmt_Id;
3158 Formal : Entity_Id;
3159 Op : Entity_Id;
3161 begin
3162 if Prev /= T
3163 and then Ekind (Prev) = E_Incomplete_Type
3164 and then Is_Tagged_Type (Prev)
3165 and then Is_Tagged_Type (T)
3166 and then Present (Primitive_Operations (Prev))
3167 then
3168 Elmt := First_Elmt (Primitive_Operations (Prev));
3169 while Present (Elmt) loop
3170 Op := Node (Elmt);
3172 Formal := First_Formal (Op);
3173 while Present (Formal) loop
3174 if Etype (Formal) = Prev then
3175 Set_Etype (Formal, T);
3176 end if;
3178 Next_Formal (Formal);
3179 end loop;
3181 if Etype (Op) = Prev then
3182 Set_Etype (Op, T);
3183 end if;
3185 Next_Elmt (Elmt);
3186 end loop;
3187 end if;
3188 end Check_Ops_From_Incomplete_Type;
3190 -- Start of processing for Analyze_Full_Type_Declaration
3192 begin
3193 Prev := Find_Type_Name (N);
3195 -- The full view, if present, now points to the current type. If there
3196 -- is an incomplete partial view, set a link to it, to simplify the
3197 -- retrieval of primitive operations of the type.
3199 -- Ada 2005 (AI-50217): If the type was previously decorated when
3200 -- imported through a LIMITED WITH clause, it appears as incomplete
3201 -- but has no full view.
3203 if Ekind (Prev) = E_Incomplete_Type
3204 and then Present (Full_View (Prev))
3205 then
3206 T := Full_View (Prev);
3207 Set_Incomplete_View (N, Prev);
3208 else
3209 T := Prev;
3210 end if;
3212 Set_Is_Pure (T, Is_Pure (Current_Scope));
3214 -- We set the flag Is_First_Subtype here. It is needed to set the
3215 -- corresponding flag for the Implicit class-wide-type created
3216 -- during tagged types processing.
3218 Set_Is_First_Subtype (T, True);
3220 -- Only composite types other than array types are allowed to have
3221 -- discriminants.
3223 case Nkind (Def) is
3225 -- For derived types, the rule will be checked once we've figured
3226 -- out the parent type.
3228 when N_Derived_Type_Definition =>
3229 null;
3231 -- For record types, discriminants are allowed.
3233 when N_Record_Definition =>
3234 null;
3236 when others =>
3237 if Present (Discriminant_Specifications (N)) then
3238 Error_Msg_N
3239 ("elementary or array type cannot have discriminants",
3240 Defining_Identifier
3241 (First (Discriminant_Specifications (N))));
3242 end if;
3243 end case;
3245 -- Elaborate the type definition according to kind, and generate
3246 -- subsidiary (implicit) subtypes where needed. We skip this if it was
3247 -- already done (this happens during the reanalysis that follows a call
3248 -- to the high level optimizer).
3250 if not Analyzed (T) then
3251 Set_Analyzed (T);
3253 -- Set the SPARK mode from the current context
3255 Set_SPARK_Pragma (T, SPARK_Mode_Pragma);
3256 Set_SPARK_Pragma_Inherited (T);
3258 case Nkind (Def) is
3259 when N_Access_To_Subprogram_Definition =>
3260 Access_Subprogram_Declaration (T, Def);
3262 -- If this is a remote access to subprogram, we must create the
3263 -- equivalent fat pointer type, and related subprograms.
3265 if Is_Remote then
3266 Process_Remote_AST_Declaration (N);
3267 end if;
3269 -- Validate categorization rule against access type declaration
3270 -- usually a violation in Pure unit, Shared_Passive unit.
3272 Validate_Access_Type_Declaration (T, N);
3274 -- If the type has contracts, we create the corresponding
3275 -- wrapper at once, before analyzing the aspect specifications,
3276 -- so that pre/postconditions can be handled directly on the
3277 -- generated wrapper.
3279 if Ada_Version >= Ada_2022
3280 and then Present (Aspect_Specifications (N))
3281 and then Expander_Active
3282 then
3283 Build_Access_Subprogram_Wrapper (N);
3284 end if;
3286 when N_Access_To_Object_Definition =>
3287 Access_Type_Declaration (T, Def);
3289 -- Validate categorization rule against access type declaration
3290 -- usually a violation in Pure unit, Shared_Passive unit.
3292 Validate_Access_Type_Declaration (T, N);
3294 -- If we are in a Remote_Call_Interface package and define a
3295 -- RACW, then calling stubs and specific stream attributes
3296 -- must be added.
3298 if Is_Remote
3299 and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
3300 then
3301 Add_RACW_Features (Def_Id);
3302 end if;
3304 when N_Array_Type_Definition =>
3305 Array_Type_Declaration (T, Def);
3307 when N_Derived_Type_Definition =>
3308 Derived_Type_Declaration (T, N, T /= Def_Id);
3310 -- Save the scenario for examination by the ABE Processing
3311 -- phase.
3313 Record_Elaboration_Scenario (N);
3315 when N_Enumeration_Type_Definition =>
3316 Enumeration_Type_Declaration (T, Def);
3318 when N_Floating_Point_Definition =>
3319 Floating_Point_Type_Declaration (T, Def);
3321 when N_Decimal_Fixed_Point_Definition =>
3322 Decimal_Fixed_Point_Type_Declaration (T, Def);
3324 when N_Ordinary_Fixed_Point_Definition =>
3325 Ordinary_Fixed_Point_Type_Declaration (T, Def);
3327 when N_Signed_Integer_Type_Definition =>
3328 Signed_Integer_Type_Declaration (T, Def);
3330 when N_Modular_Type_Definition =>
3331 Modular_Type_Declaration (T, Def);
3333 when N_Record_Definition =>
3334 Record_Type_Declaration (T, N, Prev);
3336 -- If declaration has a parse error, nothing to elaborate.
3338 when N_Error =>
3339 null;
3341 when others =>
3342 raise Program_Error;
3343 end case;
3344 end if;
3346 if Etype (T) = Any_Type then
3347 return;
3348 end if;
3350 -- Set the primitives list of the full type and its base type when
3351 -- needed. T may be E_Void in cases of earlier errors, and in that
3352 -- case we bypass this.
3354 if Ekind (T) /= E_Void then
3355 if No (Direct_Primitive_Operations (T)) then
3356 if Etype (T) = T then
3357 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3359 -- If Etype of T is the base type (as opposed to a parent type)
3360 -- and already has an associated list of primitive operations,
3361 -- then set T's primitive list to the base type's list. Otherwise,
3362 -- create a new empty primitives list and share the list between
3363 -- T and its base type. The lists need to be shared in common.
3365 elsif Etype (T) = Base_Type (T) then
3367 if No (Direct_Primitive_Operations (Base_Type (T))) then
3368 Set_Direct_Primitive_Operations
3369 (Base_Type (T), New_Elmt_List);
3370 end if;
3372 Set_Direct_Primitive_Operations
3373 (T, Direct_Primitive_Operations (Base_Type (T)));
3375 -- Case where the Etype is a parent type, so we need a new
3376 -- primitives list for T.
3378 else
3379 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3380 end if;
3382 -- If T already has a Direct_Primitive_Operations list but its
3383 -- base type doesn't then set the base type's list to T's list.
3385 elsif No (Direct_Primitive_Operations (Base_Type (T))) then
3386 Set_Direct_Primitive_Operations
3387 (Base_Type (T), Direct_Primitive_Operations (T));
3388 end if;
3389 end if;
3391 -- Some common processing for all types
3393 Set_Depends_On_Private (T, Has_Private_Component (T));
3394 Check_Ops_From_Incomplete_Type;
3396 -- Both the declared entity, and its anonymous base type if one was
3397 -- created, need freeze nodes allocated.
3399 declare
3400 B : constant Entity_Id := Base_Type (T);
3402 begin
3403 -- In the case where the base type differs from the first subtype, we
3404 -- pre-allocate a freeze node, and set the proper link to the first
3405 -- subtype. Freeze_Entity will use this preallocated freeze node when
3406 -- it freezes the entity.
3408 -- This does not apply if the base type is a generic type, whose
3409 -- declaration is independent of the current derived definition.
3411 if B /= T and then not Is_Generic_Type (B) then
3412 Ensure_Freeze_Node (B);
3413 Set_First_Subtype_Link (Freeze_Node (B), T);
3414 end if;
3416 -- A type that is imported through a limited_with clause cannot
3417 -- generate any code, and thus need not be frozen. However, an access
3418 -- type with an imported designated type needs a finalization list,
3419 -- which may be referenced in some other package that has non-limited
3420 -- visibility on the designated type. Thus we must create the
3421 -- finalization list at the point the access type is frozen, to
3422 -- prevent unsatisfied references at link time.
3424 if not From_Limited_With (T) or else Is_Access_Type (T) then
3425 Set_Has_Delayed_Freeze (T);
3426 end if;
3427 end;
3429 -- Case where T is the full declaration of some private type which has
3430 -- been swapped in Defining_Identifier (N).
3432 if T /= Def_Id and then Is_Private_Type (Def_Id) then
3433 Process_Full_View (N, T, Def_Id);
3435 -- Record the reference. The form of this is a little strange, since
3436 -- the full declaration has been swapped in. So the first parameter
3437 -- here represents the entity to which a reference is made which is
3438 -- the "real" entity, i.e. the one swapped in, and the second
3439 -- parameter provides the reference location.
3441 -- Also, we want to kill Has_Pragma_Unreferenced temporarily here
3442 -- since we don't want a complaint about the full type being an
3443 -- unwanted reference to the private type
3445 declare
3446 B : constant Boolean := Has_Pragma_Unreferenced (T);
3447 begin
3448 Set_Has_Pragma_Unreferenced (T, False);
3449 Generate_Reference (T, T, 'c');
3450 Set_Has_Pragma_Unreferenced (T, B);
3451 end;
3453 Set_Completion_Referenced (Def_Id);
3455 -- For completion of incomplete type, process incomplete dependents
3456 -- and always mark the full type as referenced (it is the incomplete
3457 -- type that we get for any real reference).
3459 elsif Ekind (Prev) = E_Incomplete_Type then
3460 Process_Incomplete_Dependents (N, T, Prev);
3461 Generate_Reference (Prev, Def_Id, 'c');
3462 Set_Completion_Referenced (Def_Id);
3464 -- If not private type or incomplete type completion, this is a real
3465 -- definition of a new entity, so record it.
3467 else
3468 Generate_Definition (Def_Id);
3469 end if;
3471 if Chars (Scope (Def_Id)) = Name_System
3472 and then Chars (Def_Id) = Name_Address
3473 and then In_Predefined_Unit (N)
3474 then
3475 Set_Is_Descendant_Of_Address (Def_Id);
3476 Set_Is_Descendant_Of_Address (Base_Type (Def_Id));
3477 Set_Is_Descendant_Of_Address (Prev);
3478 end if;
3480 Set_Optimize_Alignment_Flags (Def_Id);
3481 Check_Eliminated (Def_Id);
3483 -- If the declaration is a completion and aspects are present, apply
3484 -- them to the entity for the type which is currently the partial
3485 -- view, but which is the one that will be frozen.
3487 -- In most cases the partial view is a private type, and both views
3488 -- appear in different declarative parts. In the unusual case where
3489 -- the partial view is incomplete, perform the analysis on the
3490 -- full view, to prevent freezing anomalies with the corresponding
3491 -- class-wide type, which otherwise might be frozen before the
3492 -- dispatch table is built.
3494 if Prev /= Def_Id
3495 and then Ekind (Prev) /= E_Incomplete_Type
3496 then
3497 Analyze_Aspect_Specifications (N, Prev);
3499 -- Normal case
3501 else
3502 Analyze_Aspect_Specifications (N, Def_Id);
3503 end if;
3505 if Is_Derived_Type (Prev)
3506 and then Def_Id /= Prev
3507 then
3508 Check_Nonoverridable_Aspects;
3509 end if;
3511 -- Check for tagged type declaration at library level
3513 if Is_Tagged_Type (T)
3514 and then not Is_Library_Level_Entity (T)
3515 then
3516 Check_Restriction (No_Local_Tagged_Types, T);
3517 end if;
3519 -- Derived tagged types inherit aspect First_Controlling_Parameter
3520 -- from their parent type and also from implemented interface types.
3521 -- We implicitly perform inheritance here and will check for the
3522 -- explicit confirming pragma or aspect in the sources when this type
3523 -- is frozen (required for pragmas since they are placed at any place
3524 -- after the type declaration; otherwise, when the pragma is used after
3525 -- some non-first-controlling-parameter primitive, the reported errors
3526 -- and warning would differ when the pragma is used).
3528 if Is_Tagged_Type (T)
3529 and then Is_Derived_Type (T)
3530 and then not Has_First_Controlling_Parameter_Aspect (T)
3531 then
3532 pragma Assert (Etype (T) /= T);
3534 if Has_First_Controlling_Parameter_Aspect (Etype (T)) then
3535 Set_Has_First_Controlling_Parameter_Aspect (T);
3537 elsif Present (Interfaces (T))
3538 and then not Is_Empty_Elmt_List (Interfaces (T))
3539 then
3540 declare
3541 Elmt : Elmt_Id := First_Elmt (Interfaces (T));
3542 Iface : Entity_Id;
3544 begin
3545 while Present (Elmt) loop
3546 Iface := Node (Elmt);
3548 if Has_First_Controlling_Parameter_Aspect (Iface) then
3549 Set_Has_First_Controlling_Parameter_Aspect (T);
3550 exit;
3551 end if;
3553 Next_Elmt (Elmt);
3554 end loop;
3555 end;
3556 end if;
3557 end if;
3558 end Analyze_Full_Type_Declaration;
3560 ----------------------------------
3561 -- Analyze_Incomplete_Type_Decl --
3562 ----------------------------------
3564 procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
3565 F : constant Boolean := Is_Pure (Current_Scope);
3566 T : Entity_Id;
3568 begin
3569 Generate_Definition (Defining_Identifier (N));
3571 -- Process an incomplete declaration. The identifier must not have been
3572 -- declared already in the scope. However, an incomplete declaration may
3573 -- appear in the private part of a package, for a private type that has
3574 -- already been declared.
3576 -- In this case, the discriminants (if any) must match
3578 T := Find_Type_Name (N);
3580 Mutate_Ekind (T, E_Incomplete_Type);
3581 Set_Etype (T, T);
3582 Set_Is_First_Subtype (T);
3583 Reinit_Size_Align (T);
3585 -- Set the SPARK mode from the current context
3587 Set_SPARK_Pragma (T, SPARK_Mode_Pragma);
3588 Set_SPARK_Pragma_Inherited (T);
3590 -- Ada 2005 (AI-326): Minimum decoration to give support to tagged
3591 -- incomplete types.
3593 if Tagged_Present (N) then
3594 Set_Is_Tagged_Type (T, True);
3595 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
3596 Make_Class_Wide_Type (T);
3597 end if;
3599 -- Initialize the list of primitive operations to an empty list,
3600 -- to cover tagged types as well as untagged types. For untagged
3601 -- types this is used either to analyze the call as legal when
3602 -- GNAT extensions are allowed, or to give better error messages.
3604 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3606 Set_Stored_Constraint (T, No_Elist);
3608 if Present (Discriminant_Specifications (N)) then
3609 Push_Scope (T);
3610 Process_Discriminants (N);
3611 End_Scope;
3612 end if;
3614 -- If the type has discriminants, nontrivial subtypes may be declared
3615 -- before the full view of the type. The full views of those subtypes
3616 -- will be built after the full view of the type.
3618 Set_Private_Dependents (T, New_Elmt_List);
3619 Set_Is_Pure (T, F);
3620 end Analyze_Incomplete_Type_Decl;
3622 -----------------------------------
3623 -- Analyze_Interface_Declaration --
3624 -----------------------------------
3626 procedure Analyze_Interface_Declaration (T : Entity_Id; Def : Node_Id) is
3627 CW : constant Entity_Id := Class_Wide_Type (T);
3629 begin
3630 Set_Is_Tagged_Type (T);
3631 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
3633 Set_Is_Limited_Record (T, Limited_Present (Def)
3634 or else Task_Present (Def)
3635 or else Protected_Present (Def)
3636 or else Synchronized_Present (Def));
3638 -- Type is abstract if full declaration carries keyword, or if previous
3639 -- partial view did.
3641 Set_Is_Abstract_Type (T);
3642 Set_Is_Interface (T);
3644 -- Type is a limited interface if it includes the keyword limited, task,
3645 -- protected, or synchronized.
3647 Set_Is_Limited_Interface
3648 (T, Limited_Present (Def)
3649 or else Protected_Present (Def)
3650 or else Synchronized_Present (Def)
3651 or else Task_Present (Def));
3653 Set_Interfaces (T, New_Elmt_List);
3654 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3656 -- Complete the decoration of the class-wide entity if it was already
3657 -- built (i.e. during the creation of the limited view)
3659 if Present (CW) then
3660 Set_Is_Interface (CW);
3661 Set_Is_Limited_Interface (CW, Is_Limited_Interface (T));
3662 end if;
3664 -- Check runtime support for synchronized interfaces
3666 if Is_Concurrent_Interface (T)
3667 and then not RTE_Available (RE_Select_Specific_Data)
3668 then
3669 Error_Msg_CRT ("synchronized interfaces", T);
3670 end if;
3671 end Analyze_Interface_Declaration;
3673 -----------------------------
3674 -- Analyze_Itype_Reference --
3675 -----------------------------
3677 -- Nothing to do. This node is placed in the tree only for the benefit of
3678 -- back end processing, and has no effect on the semantic processing.
3680 procedure Analyze_Itype_Reference (N : Node_Id) is
3681 begin
3682 pragma Assert (Is_Itype (Itype (N)));
3683 null;
3684 end Analyze_Itype_Reference;
3686 --------------------------------
3687 -- Analyze_Number_Declaration --
3688 --------------------------------
3690 procedure Analyze_Number_Declaration (N : Node_Id) is
3691 E : Node_Id := Expression (N);
3692 Id : constant Entity_Id := Defining_Identifier (N);
3693 Index : Interp_Index;
3694 It : Interp;
3695 T : Entity_Id;
3697 begin
3698 Generate_Definition (Id);
3699 Enter_Name (Id);
3701 -- This is an optimization of a common case of an integer literal
3703 if Nkind (E) = N_Integer_Literal then
3704 Set_Is_Static_Expression (E, True);
3705 Set_Etype (E, Universal_Integer);
3707 Set_Etype (Id, Universal_Integer);
3708 Mutate_Ekind (Id, E_Named_Integer);
3709 Set_Is_Frozen (Id, True);
3711 Set_Debug_Info_Needed (Id);
3712 return;
3713 end if;
3715 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3717 -- Replace Error by integer zero, which seems least likely to cause
3718 -- cascaded errors.
3720 if E = Error then
3721 pragma Assert (Serious_Errors_Detected > 0);
3722 E := Make_Integer_Literal (Sloc (N), Uint_0);
3723 Set_Expression (N, E);
3724 Set_Error_Posted (E);
3725 end if;
3727 Analyze (E);
3729 -- Verify that the expression is static and numeric. If
3730 -- the expression is overloaded, we apply the preference
3731 -- rule that favors root numeric types.
3733 if not Is_Overloaded (E) then
3734 T := Etype (E);
3735 if Has_Dynamic_Predicate_Aspect (T)
3736 or else Has_Ghost_Predicate_Aspect (T)
3737 then
3738 Error_Msg_N
3739 ("subtype has non-static predicate, "
3740 & "not allowed in number declaration", N);
3741 end if;
3743 else
3744 T := Any_Type;
3746 Get_First_Interp (E, Index, It);
3747 while Present (It.Typ) loop
3748 if (Is_Integer_Type (It.Typ) or else Is_Real_Type (It.Typ))
3749 and then (Scope (Base_Type (It.Typ))) = Standard_Standard
3750 then
3751 if T = Any_Type then
3752 T := It.Typ;
3754 elsif Is_Universal_Numeric_Type (It.Typ) then
3755 -- Choose universal interpretation over any other
3757 T := It.Typ;
3758 exit;
3759 end if;
3760 end if;
3762 Get_Next_Interp (Index, It);
3763 end loop;
3764 end if;
3766 if Is_Integer_Type (T) then
3767 Resolve (E, T);
3768 Set_Etype (Id, Universal_Integer);
3769 Mutate_Ekind (Id, E_Named_Integer);
3771 elsif Is_Real_Type (T) then
3773 -- Because the real value is converted to universal_real, this is a
3774 -- legal context for a universal fixed expression.
3776 if T = Universal_Fixed then
3777 declare
3778 Loc : constant Source_Ptr := Sloc (N);
3779 Conv : constant Node_Id := Make_Type_Conversion (Loc,
3780 Subtype_Mark =>
3781 New_Occurrence_Of (Universal_Real, Loc),
3782 Expression => Relocate_Node (E));
3784 begin
3785 Rewrite (E, Conv);
3786 Analyze (E);
3787 end;
3789 elsif T = Any_Fixed then
3790 Error_Msg_N ("illegal context for mixed mode operation", E);
3792 -- Expression is of the form : universal_fixed * integer. Try to
3793 -- resolve as universal_real.
3795 T := Universal_Real;
3796 Set_Etype (E, T);
3797 end if;
3799 Resolve (E, T);
3800 Set_Etype (Id, Universal_Real);
3801 Mutate_Ekind (Id, E_Named_Real);
3803 else
3804 Wrong_Type (E, Any_Numeric);
3805 Resolve (E, T);
3807 Set_Etype (Id, T);
3808 Mutate_Ekind (Id, E_Constant);
3809 Set_Never_Set_In_Source (Id, True);
3810 Set_Is_True_Constant (Id, True);
3811 return;
3812 end if;
3814 if Nkind (E) in N_Integer_Literal | N_Real_Literal then
3815 Set_Etype (E, Etype (Id));
3816 end if;
3818 if not Is_OK_Static_Expression (E) then
3819 Flag_Non_Static_Expr
3820 ("non-static expression used in number declaration!", E);
3821 Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
3822 Set_Etype (E, Any_Type);
3823 end if;
3825 Analyze_Dimension (N);
3826 end Analyze_Number_Declaration;
3828 --------------------------------
3829 -- Analyze_Object_Declaration --
3830 --------------------------------
3832 -- WARNING: This routine manages Ghost regions. Return statements must be
3833 -- replaced by gotos which jump to the end of the routine and restore the
3834 -- Ghost mode.
3836 procedure Analyze_Object_Declaration (N : Node_Id) is
3837 Loc : constant Source_Ptr := Sloc (N);
3838 Id : constant Entity_Id := Defining_Identifier (N);
3839 Next_Decl : constant Node_Id := Next (N);
3841 Act_T : Entity_Id;
3842 T : Entity_Id;
3844 E : Node_Id := Expression (N);
3845 -- E is set to Expression (N) throughout this routine. When Expression
3846 -- (N) is modified, E is changed accordingly.
3848 procedure Check_Dynamic_Object (Typ : Entity_Id);
3849 -- A library-level object with nonstatic discriminant constraints may
3850 -- require dynamic allocation. The declaration is illegal if the
3851 -- profile includes the restriction No_Implicit_Heap_Allocations.
3853 procedure Check_For_Null_Excluding_Components
3854 (Obj_Typ : Entity_Id;
3855 Obj_Decl : Node_Id);
3856 -- Verify that each null-excluding component of object declaration
3857 -- Obj_Decl carrying type Obj_Typ has explicit initialization. Emit
3858 -- a compile-time warning if this is not the case.
3860 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id);
3861 -- Check that the return subtype indication properly matches the result
3862 -- subtype of the function in an extended return object declaration, as
3863 -- required by RM 6.5(5.1/2-5.3/2).
3865 function Count_Tasks (T : Entity_Id) return Uint;
3866 -- This function is called when a non-generic library level object of a
3867 -- task type is declared. Its function is to count the static number of
3868 -- tasks declared within the type (it is only called if Has_Task is set
3869 -- for T). As a side effect, if an array of tasks with nonstatic bounds
3870 -- or a variant record type is encountered, Check_Restriction is called
3871 -- indicating the count is unknown.
3873 function Delayed_Aspect_Present return Boolean;
3874 -- If the declaration has an expression that is an aggregate, and it
3875 -- has aspects that require delayed analysis, the resolution of the
3876 -- aggregate must be deferred to the freeze point of the object. This
3877 -- special processing was created for address clauses, but it must
3878 -- also apply to address aspects. This must be done before the aspect
3879 -- specifications are analyzed because we must handle the aggregate
3880 -- before the analysis of the object declaration is complete.
3882 -- Any other relevant delayed aspects on object declarations ???
3884 --------------------------
3885 -- Check_Dynamic_Object --
3886 --------------------------
3888 procedure Check_Dynamic_Object (Typ : Entity_Id) is
3889 Comp : Entity_Id;
3890 Obj_Type : Entity_Id;
3892 begin
3893 Obj_Type := Typ;
3895 if Is_Private_Type (Obj_Type)
3896 and then Present (Full_View (Obj_Type))
3897 then
3898 Obj_Type := Full_View (Obj_Type);
3899 end if;
3901 if Known_Static_Esize (Obj_Type) then
3902 return;
3903 end if;
3905 if Restriction_Active (No_Implicit_Heap_Allocations)
3906 and then Expander_Active
3907 and then Has_Discriminants (Obj_Type)
3908 then
3909 Comp := First_Component (Obj_Type);
3910 while Present (Comp) loop
3911 if Known_Static_Esize (Etype (Comp))
3912 or else Size_Known_At_Compile_Time (Etype (Comp))
3913 then
3914 null;
3916 elsif Is_Record_Type (Etype (Comp)) then
3917 Check_Dynamic_Object (Etype (Comp));
3919 elsif not Discriminated_Size (Comp)
3920 and then Comes_From_Source (Comp)
3921 then
3922 Error_Msg_NE
3923 ("component& of non-static size will violate restriction "
3924 & "No_Implicit_Heap_Allocation?", N, Comp);
3926 end if;
3928 Next_Component (Comp);
3929 end loop;
3930 end if;
3931 end Check_Dynamic_Object;
3933 -----------------------------------------
3934 -- Check_For_Null_Excluding_Components --
3935 -----------------------------------------
3937 procedure Check_For_Null_Excluding_Components
3938 (Obj_Typ : Entity_Id;
3939 Obj_Decl : Node_Id)
3941 procedure Check_Component
3942 (Comp_Typ : Entity_Id;
3943 Comp_Decl : Node_Id := Empty;
3944 Array_Comp : Boolean := False);
3945 -- Apply a compile-time null-exclusion check on a component denoted
3946 -- by its declaration Comp_Decl and type Comp_Typ, and all of its
3947 -- subcomponents (if any).
3949 ---------------------
3950 -- Check_Component --
3951 ---------------------
3953 procedure Check_Component
3954 (Comp_Typ : Entity_Id;
3955 Comp_Decl : Node_Id := Empty;
3956 Array_Comp : Boolean := False)
3958 Comp : Entity_Id;
3959 T : Entity_Id;
3961 begin
3962 -- Do not consider internally-generated components or those that
3963 -- are already initialized.
3965 if Present (Comp_Decl)
3966 and then (not Comes_From_Source (Comp_Decl)
3967 or else Present (Expression (Comp_Decl)))
3968 then
3969 return;
3970 end if;
3972 if Is_Incomplete_Or_Private_Type (Comp_Typ)
3973 and then Present (Full_View (Comp_Typ))
3974 then
3975 T := Full_View (Comp_Typ);
3976 else
3977 T := Comp_Typ;
3978 end if;
3980 -- Verify a component of a null-excluding access type
3982 if Is_Access_Type (T)
3983 and then Can_Never_Be_Null (T)
3984 then
3985 if Comp_Decl = Obj_Decl then
3986 Null_Exclusion_Static_Checks
3987 (N => Obj_Decl,
3988 Comp => Empty,
3989 Array_Comp => Array_Comp);
3991 else
3992 Null_Exclusion_Static_Checks
3993 (N => Obj_Decl,
3994 Comp => Comp_Decl,
3995 Array_Comp => Array_Comp);
3996 end if;
3998 -- Check array components
4000 elsif Is_Array_Type (T) then
4002 -- There is no suitable component when the object is of an
4003 -- array type. However, a namable component may appear at some
4004 -- point during the recursive inspection, but not at the top
4005 -- level. At the top level just indicate array component case.
4007 if Comp_Decl = Obj_Decl then
4008 Check_Component (Component_Type (T), Array_Comp => True);
4009 else
4010 Check_Component (Component_Type (T), Comp_Decl);
4011 end if;
4013 -- Verify all components of type T
4015 -- Note: No checks are performed on types with discriminants due
4016 -- to complexities involving variants. ???
4018 elsif (Is_Concurrent_Type (T)
4019 or else Is_Incomplete_Or_Private_Type (T)
4020 or else Is_Record_Type (T))
4021 and then not Has_Discriminants (T)
4022 then
4023 Comp := First_Component (T);
4024 while Present (Comp) loop
4025 Check_Component (Etype (Comp), Parent (Comp));
4027 Next_Component (Comp);
4028 end loop;
4029 end if;
4030 end Check_Component;
4032 -- Start processing for Check_For_Null_Excluding_Components
4034 begin
4035 Check_Component (Obj_Typ, Obj_Decl);
4036 end Check_For_Null_Excluding_Components;
4038 -------------------------------------
4039 -- Check_Return_Subtype_Indication --
4040 -------------------------------------
4042 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id) is
4043 Obj_Id : constant Entity_Id := Defining_Identifier (Obj_Decl);
4044 Obj_Typ : constant Entity_Id := Etype (Obj_Id);
4045 Func_Id : constant Entity_Id := Return_Applies_To (Scope (Obj_Id));
4046 R_Typ : constant Entity_Id := Etype (Func_Id);
4047 Indic : constant Node_Id :=
4048 Object_Definition (Original_Node (Obj_Decl));
4050 procedure Error_No_Match (N : Node_Id);
4051 -- Output error messages for case where types do not statically
4052 -- match. N is the location for the messages.
4054 --------------------
4055 -- Error_No_Match --
4056 --------------------
4058 procedure Error_No_Match (N : Node_Id) is
4059 begin
4060 Error_Msg_N
4061 ("subtype must statically match function result subtype", N);
4063 if not Predicates_Match (Obj_Typ, R_Typ) then
4064 Error_Msg_Node_2 := R_Typ;
4065 Error_Msg_NE
4066 ("\predicate of& does not match predicate of&",
4067 N, Obj_Typ);
4068 end if;
4069 end Error_No_Match;
4071 -- Start of processing for Check_Return_Subtype_Indication
4073 begin
4074 -- First, avoid cascaded errors
4076 if Error_Posted (Obj_Decl) or else Error_Posted (Indic) then
4077 return;
4078 end if;
4080 -- "return access T" case; check that the return statement also has
4081 -- "access T", and that the subtypes statically match:
4082 -- if this is an access to subprogram the signatures must match.
4084 if Is_Anonymous_Access_Type (R_Typ) then
4085 if Is_Anonymous_Access_Type (Obj_Typ) then
4086 if Ekind (Designated_Type (Obj_Typ)) /= E_Subprogram_Type
4087 then
4088 if Base_Type (Designated_Type (Obj_Typ)) /=
4089 Base_Type (Designated_Type (R_Typ))
4090 or else not Subtypes_Statically_Match (Obj_Typ, R_Typ)
4091 then
4092 Error_No_Match (Subtype_Mark (Indic));
4093 end if;
4095 else
4096 -- For two anonymous access to subprogram types, the types
4097 -- themselves must be type conformant.
4099 if not Conforming_Types
4100 (Obj_Typ, R_Typ, Fully_Conformant)
4101 then
4102 Error_No_Match (Indic);
4103 end if;
4104 end if;
4106 else
4107 Error_Msg_N ("must use anonymous access type", Indic);
4108 end if;
4110 -- If the return object is of an anonymous access type, then report
4111 -- an error if the function's result type is not also anonymous.
4113 elsif Is_Anonymous_Access_Type (Obj_Typ) then
4114 pragma Assert (not Is_Anonymous_Access_Type (R_Typ));
4115 Error_Msg_N
4116 ("anonymous access not allowed for function with named access "
4117 & "result", Indic);
4119 -- Subtype indication case: check that the return object's type is
4120 -- covered by the result type, and that the subtypes statically match
4121 -- when the result subtype is constrained. Also handle record types
4122 -- with unknown discriminants for which we have built the underlying
4123 -- record view. Coverage is needed to allow specific-type return
4124 -- objects when the result type is class-wide (see AI05-32).
4126 elsif Covers (Base_Type (R_Typ), Base_Type (Obj_Typ))
4127 or else (Is_Underlying_Record_View (Base_Type (Obj_Typ))
4128 and then
4129 Covers
4130 (Base_Type (R_Typ),
4131 Underlying_Record_View (Base_Type (Obj_Typ))))
4132 then
4133 -- A null exclusion may be present on the return type, on the
4134 -- function specification, on the object declaration or on the
4135 -- subtype itself.
4137 if Is_Access_Type (R_Typ)
4138 and then
4139 (Can_Never_Be_Null (R_Typ)
4140 or else Null_Exclusion_Present (Parent (Func_Id))) /=
4141 Can_Never_Be_Null (Obj_Typ)
4142 then
4143 Error_No_Match (Indic);
4144 end if;
4146 -- AI05-103: for elementary types, subtypes must statically match
4148 if Is_Constrained (R_Typ) or else Is_Access_Type (R_Typ) then
4149 if not Subtypes_Statically_Match (Obj_Typ, R_Typ) then
4150 Error_No_Match (Indic);
4151 end if;
4153 -- If the result subtype of the function is defined by a
4154 -- subtype_mark, the return_subtype_indication shall be a
4155 -- subtype_indication. The subtype defined by the subtype_
4156 -- indication shall be statically compatible with the result
4157 -- subtype of the function (RM 6.5(5.3/5)).
4159 -- We exclude the extended return statement of the predefined
4160 -- stream input to avoid reporting spurious errors, because its
4161 -- code is expanded on the basis of the base type (see subprogram
4162 -- Stream_Base_Type).
4164 elsif Nkind (Indic) = N_Subtype_Indication
4165 and then not Subtypes_Statically_Compatible (Obj_Typ, R_Typ)
4166 and then not Is_TSS (Func_Id, TSS_Stream_Input)
4167 then
4168 Error_Msg_N
4169 ("result subtype must be statically compatible with the " &
4170 "function result type", Indic);
4172 if not Predicates_Compatible (Obj_Typ, R_Typ) then
4173 Error_Msg_NE
4174 ("\predicate on result subtype is not compatible with &",
4175 Indic, R_Typ);
4176 end if;
4177 end if;
4179 -- All remaining cases are illegal
4181 -- Note: previous versions of this subprogram allowed the return
4182 -- value to be the ancestor of the return type if the return type
4183 -- was a null extension. This was plainly incorrect.
4185 else
4186 Error_Msg_N
4187 ("wrong type for return_subtype_indication", Indic);
4188 end if;
4189 end Check_Return_Subtype_Indication;
4191 -----------------
4192 -- Count_Tasks --
4193 -----------------
4195 function Count_Tasks (T : Entity_Id) return Uint is
4196 C : Entity_Id;
4197 X : Node_Id;
4198 V : Uint;
4200 begin
4201 if Is_Task_Type (T) then
4202 return Uint_1;
4204 elsif Is_Record_Type (T) then
4205 if Has_Discriminants (T) then
4206 Check_Restriction (Max_Tasks, N);
4207 return Uint_0;
4209 else
4210 V := Uint_0;
4211 C := First_Component (T);
4212 while Present (C) loop
4213 V := V + Count_Tasks (Etype (C));
4214 Next_Component (C);
4215 end loop;
4217 return V;
4218 end if;
4220 elsif Is_Array_Type (T) then
4221 X := First_Index (T);
4222 V := Count_Tasks (Component_Type (T));
4223 while Present (X) loop
4224 C := Etype (X);
4226 if not Is_OK_Static_Subtype (C) then
4227 Check_Restriction (Max_Tasks, N);
4228 return Uint_0;
4229 else
4230 V := V * (UI_Max (Uint_0,
4231 Expr_Value (Type_High_Bound (C)) -
4232 Expr_Value (Type_Low_Bound (C)) + Uint_1));
4233 end if;
4235 Next_Index (X);
4236 end loop;
4238 return V;
4240 else
4241 return Uint_0;
4242 end if;
4243 end Count_Tasks;
4245 ----------------------------
4246 -- Delayed_Aspect_Present --
4247 ----------------------------
4249 function Delayed_Aspect_Present return Boolean is
4250 A : Node_Id;
4251 A_Id : Aspect_Id;
4253 begin
4254 A := First (Aspect_Specifications (N));
4256 while Present (A) loop
4257 A_Id := Get_Aspect_Id (Chars (Identifier (A)));
4259 if A_Id = Aspect_Address then
4261 -- Set flag on object entity, for later processing at the
4262 -- freeze point.
4264 Set_Has_Delayed_Aspects (Id);
4265 return True;
4266 end if;
4268 Next (A);
4269 end loop;
4271 return False;
4272 end Delayed_Aspect_Present;
4274 -- Local variables
4276 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
4277 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
4278 -- Save the Ghost-related attributes to restore on exit
4280 Prev_Entity : Entity_Id := Empty;
4281 Related_Id : Entity_Id;
4283 -- Start of processing for Analyze_Object_Declaration
4285 begin
4286 -- There are three kinds of implicit types generated by an
4287 -- object declaration:
4289 -- 1. Those generated by the original Object Definition
4291 -- 2. Those generated by the Expression
4293 -- 3. Those used to constrain the Object Definition with the
4294 -- expression constraints when the definition is unconstrained.
4296 -- They must be generated in this order to avoid order of elaboration
4297 -- issues. Thus the first step (after entering the name) is to analyze
4298 -- the object definition.
4300 if Constant_Present (N) then
4301 Prev_Entity := Current_Entity_In_Scope (Id);
4303 if Present (Prev_Entity)
4304 and then
4305 -- If the homograph is an implicit subprogram, it is overridden
4306 -- by the current declaration.
4308 ((Is_Overloadable (Prev_Entity)
4309 and then Is_Inherited_Operation (Prev_Entity))
4311 -- The current object is a discriminal generated for an entry
4312 -- family index. Even though the index is a constant, in this
4313 -- particular context there is no true constant redeclaration.
4314 -- Enter_Name will handle the visibility.
4316 or else
4317 (Is_Discriminal (Id)
4318 and then Ekind (Discriminal_Link (Id)) =
4319 E_Entry_Index_Parameter)
4321 -- The current object is the renaming for a generic declared
4322 -- within the instance.
4324 or else
4325 (Ekind (Prev_Entity) = E_Package
4326 and then Nkind (Parent (Prev_Entity)) =
4327 N_Package_Renaming_Declaration
4328 and then not Comes_From_Source (Prev_Entity)
4329 and then
4330 Is_Generic_Instance (Renamed_Entity (Prev_Entity)))
4332 -- The entity may be a homonym of a private component of the
4333 -- enclosing protected object, for which we create a local
4334 -- renaming declaration. The declaration is legal, even if
4335 -- useless when it just captures that component.
4337 or else
4338 (Ekind (Scope (Current_Scope)) = E_Protected_Type
4339 and then Nkind (Parent (Prev_Entity)) =
4340 N_Object_Renaming_Declaration))
4341 then
4342 Prev_Entity := Empty;
4343 end if;
4344 end if;
4346 if Present (Prev_Entity) then
4348 -- The object declaration is Ghost when it completes a deferred Ghost
4349 -- constant.
4351 Mark_And_Set_Ghost_Completion (N, Prev_Entity);
4353 Constant_Redeclaration (Id, N, T);
4355 Generate_Reference (Prev_Entity, Id, 'c');
4356 Set_Completion_Referenced (Id);
4358 if Error_Posted (N) then
4360 -- Type mismatch or illegal redeclaration; do not analyze
4361 -- expression to avoid cascaded errors.
4363 T := Find_Type_Of_Object (Object_Definition (N), N);
4364 Set_Etype (Id, T);
4365 Mutate_Ekind (Id, E_Variable);
4366 goto Leave;
4367 end if;
4369 -- In the normal case, enter identifier at the start to catch premature
4370 -- usage in the initialization expression.
4372 else
4373 Generate_Definition (Id);
4374 Enter_Name (Id);
4376 Mark_Coextensions (N, Object_Definition (N));
4378 T := Find_Type_Of_Object (Object_Definition (N), N);
4380 if Nkind (Object_Definition (N)) = N_Access_Definition
4381 and then Present
4382 (Access_To_Subprogram_Definition (Object_Definition (N)))
4383 and then Protected_Present
4384 (Access_To_Subprogram_Definition (Object_Definition (N)))
4385 then
4386 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
4387 end if;
4389 if Error_Posted (Id) then
4390 Set_Etype (Id, T);
4391 Mutate_Ekind (Id, E_Variable);
4392 goto Leave;
4393 end if;
4394 end if;
4396 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
4397 -- out some static checks.
4399 if Ada_Version >= Ada_2005 then
4401 -- In case of aggregates we must also take care of the correct
4402 -- initialization of nested aggregates bug this is done at the
4403 -- point of the analysis of the aggregate (see sem_aggr.adb) ???
4405 if Can_Never_Be_Null (T) then
4406 if Present (Expression (N))
4407 and then Nkind (Expression (N)) = N_Aggregate
4408 then
4409 null;
4411 elsif Comes_From_Source (Id) then
4412 declare
4413 Save_Typ : constant Entity_Id := Etype (Id);
4414 begin
4415 Set_Etype (Id, T); -- Temp. decoration for static checks
4416 Null_Exclusion_Static_Checks (N);
4417 Set_Etype (Id, Save_Typ);
4418 end;
4419 end if;
4421 -- We might be dealing with an object of a composite type containing
4422 -- null-excluding components without an aggregate, so we must verify
4423 -- that such components have default initialization.
4425 else
4426 Check_For_Null_Excluding_Components (T, N);
4427 end if;
4428 end if;
4430 -- Object is marked pure if it is in a pure scope
4432 Set_Is_Pure (Id, Is_Pure (Current_Scope));
4434 -- If deferred constant, make sure context is appropriate. We detect
4435 -- a deferred constant as a constant declaration with no expression.
4436 -- A deferred constant can appear in a package body if its completion
4437 -- is by means of an interface pragma.
4439 if Constant_Present (N) and then No (E) then
4441 -- A deferred constant may appear in the declarative part of the
4442 -- following constructs:
4444 -- blocks
4445 -- entry bodies
4446 -- extended return statements
4447 -- package specs
4448 -- package bodies
4449 -- subprogram bodies
4450 -- task bodies
4452 -- When declared inside a package spec, a deferred constant must be
4453 -- completed by a full constant declaration or pragma Import. In all
4454 -- other cases, the only proper completion is pragma Import. Extended
4455 -- return statements are flagged as invalid contexts because they do
4456 -- not have a declarative part and so cannot accommodate the pragma.
4458 if Ekind (Current_Scope) = E_Return_Statement then
4459 Error_Msg_N
4460 ("invalid context for deferred constant declaration (RM 7.4)",
4462 Error_Msg_N
4463 ("\declaration requires an initialization expression",
4465 Set_Constant_Present (N, False);
4467 -- In Ada 83, deferred constant must be of private type
4469 elsif not Is_Private_Type (T) then
4470 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
4471 Error_Msg_N
4472 ("(Ada 83) deferred constant must be private type", N);
4473 end if;
4474 end if;
4476 -- If not a deferred constant, then the object declaration freezes
4477 -- its type, unless the object is of an anonymous type and has delayed
4478 -- aspects (in that case the type is frozen when the object itself is)
4479 -- or the context is a spec expression.
4481 else
4482 Check_Fully_Declared (T, N);
4484 if Has_Delayed_Aspects (Id)
4485 and then Is_Array_Type (T)
4486 and then Is_Itype (T)
4487 then
4488 Set_Has_Delayed_Freeze (T);
4489 elsif not In_Spec_Expression then
4490 Freeze_Before (N, T);
4491 end if;
4492 end if;
4494 -- If the object was created by a constrained array definition, then
4495 -- set the link in both the anonymous base type and anonymous subtype
4496 -- that are built to represent the array type to point to the object.
4498 if Nkind (Object_Definition (Declaration_Node (Id))) =
4499 N_Constrained_Array_Definition
4500 then
4501 Set_Related_Array_Object (T, Id);
4502 Set_Related_Array_Object (Base_Type (T), Id);
4503 end if;
4505 -- Check for protected objects not at library level
4507 if Has_Protected (T) and then not Is_Library_Level_Entity (Id) then
4508 Check_Restriction (No_Local_Protected_Objects, Id);
4509 end if;
4511 -- Check for violation of No_Local_Timing_Events
4513 if Has_Timing_Event (T) and then not Is_Library_Level_Entity (Id) then
4514 Check_Restriction (No_Local_Timing_Events, Id);
4515 end if;
4517 -- The actual subtype of the object is the nominal subtype, unless
4518 -- the nominal one is unconstrained and obtained from the expression.
4520 Act_T := T;
4522 if Is_Library_Level_Entity (Id) then
4523 Check_Dynamic_Object (T);
4524 end if;
4526 -- Process initialization expression if present and not in error
4528 if Present (E) and then E /= Error then
4530 -- Generate an error in case of CPP class-wide object initialization.
4531 -- Required because otherwise the expansion of the class-wide
4532 -- assignment would try to use 'size to initialize the object
4533 -- (primitive that is not available in CPP tagged types).
4535 if Is_Class_Wide_Type (Act_T)
4536 and then
4537 (Is_CPP_Class (Root_Type (Etype (Act_T)))
4538 or else
4539 (Present (Full_View (Root_Type (Etype (Act_T))))
4540 and then
4541 Is_CPP_Class (Full_View (Root_Type (Etype (Act_T))))))
4542 then
4543 Error_Msg_N
4544 ("predefined assignment not available for 'C'P'P tagged types",
4546 end if;
4548 Mark_Coextensions (N, E);
4549 Analyze (E);
4551 -- In case of errors detected in the analysis of the expression,
4552 -- decorate it with the expected type to avoid cascaded errors.
4554 if No (Etype (E)) then
4555 Set_Etype (E, T);
4556 end if;
4558 -- If an initialization expression is present, then we set the
4559 -- Is_True_Constant flag. It will be reset if this is a variable
4560 -- and it is indeed modified.
4562 Set_Is_True_Constant (Id, True);
4564 -- If we are analyzing a constant declaration, set its completion
4565 -- flag after analyzing and resolving the expression.
4567 if Constant_Present (N) then
4568 Set_Has_Completion (Id);
4569 end if;
4571 -- Set type and resolve (type may be overridden later on). Note:
4572 -- Ekind (Id) must still be E_Void at this point so that incorrect
4573 -- early usage within E is properly diagnosed.
4575 Set_Etype (Id, T);
4577 -- If the expression is an aggregate we must look ahead to detect
4578 -- the possible presence of an address clause, and defer resolution
4579 -- and expansion of the aggregate to the freeze point of the entity.
4581 -- This is not always legal because the aggregate may contain other
4582 -- references that need freezing, e.g. references to other entities
4583 -- with address clauses. In any case, when compiling with -gnatI the
4584 -- presence of the address clause must be ignored.
4586 if Comes_From_Source (N)
4587 and then Expander_Active
4588 and then Nkind (E) = N_Aggregate
4589 and then
4590 ((Present (Following_Address_Clause (N))
4591 and then not Ignore_Rep_Clauses)
4592 or else Delayed_Aspect_Present)
4593 then
4594 Set_Etype (E, T);
4596 -- If the aggregate is limited it will be built in place, and its
4597 -- expansion is deferred until the object declaration is expanded.
4599 if Is_Limited_Type (T) then
4600 Set_Expansion_Delayed (E);
4601 end if;
4603 else
4604 -- If the expression is a formal that is a "subprogram pointer"
4605 -- this is illegal in accessibility terms (see RM 3.10.2 (13.1/2)
4606 -- and AARM 3.10.2 (13.b/2)). Add an explicit conversion to force
4607 -- the corresponding check, as is done for assignments.
4609 if Is_Entity_Name (E)
4610 and then Present (Entity (E))
4611 and then Is_Formal (Entity (E))
4612 and then
4613 Ekind (Etype (Entity (E))) = E_Anonymous_Access_Subprogram_Type
4614 and then Ekind (T) /= E_Anonymous_Access_Subprogram_Type
4615 then
4616 Rewrite (E, Convert_To (T, Relocate_Node (E)));
4617 end if;
4619 Resolve (E, T);
4620 end if;
4622 -- No further action needed if E is a call to an inlined function
4623 -- which returns an unconstrained type and it has been expanded into
4624 -- a procedure call. In that case N has been replaced by an object
4625 -- declaration without initializing expression and it has been
4626 -- analyzed (see Expand_Inlined_Call).
4628 if Back_End_Inlining
4629 and then Expander_Active
4630 and then Nkind (E) = N_Function_Call
4631 and then Nkind (Name (E)) in N_Has_Entity
4632 and then Is_Inlined (Entity (Name (E)))
4633 and then not Is_Constrained (Etype (E))
4634 and then Analyzed (N)
4635 and then No (Expression (N))
4636 then
4637 goto Leave;
4638 end if;
4640 -- If E is null and has been replaced by an N_Raise_Constraint_Error
4641 -- node (which was marked already-analyzed), we need to set the type
4642 -- to something else than Universal_Access to keep gigi happy.
4644 if Etype (E) = Universal_Access then
4645 Set_Etype (E, T);
4646 end if;
4648 -- If the object is an access to variable, the initialization
4649 -- expression cannot be an access to constant.
4651 if Is_Access_Type (T)
4652 and then not Is_Access_Constant (T)
4653 and then Is_Access_Type (Etype (E))
4654 and then Is_Access_Constant (Etype (E))
4655 then
4656 Error_Msg_N
4657 ("access to variable cannot be initialized with an "
4658 & "access-to-constant expression", E);
4659 end if;
4661 if not Assignment_OK (N) then
4662 Check_Initialization (T, E);
4663 end if;
4665 Check_Unset_Reference (E);
4667 -- If this is a variable, then set current value. If this is a
4668 -- declared constant of a scalar type with a static expression,
4669 -- indicate that it is always valid.
4671 if not Constant_Present (N) then
4672 if Compile_Time_Known_Value (E) then
4673 Set_Current_Value (Id, E);
4674 end if;
4676 elsif Is_Scalar_Type (T) and then Is_OK_Static_Expression (E) then
4677 Set_Is_Known_Valid (Id);
4679 -- If it is a constant initialized with a valid nonstatic entity,
4680 -- the constant is known valid as well, and can inherit the subtype
4681 -- of the entity if it is a subtype of the given type. This info
4682 -- is preserved on the actual subtype of the constant.
4684 elsif Is_Scalar_Type (T)
4685 and then Is_Entity_Name (E)
4686 and then Is_Known_Valid (Entity (E))
4687 and then In_Subrange_Of (Etype (Entity (E)), T)
4688 then
4689 Set_Is_Known_Valid (Id);
4690 Mutate_Ekind (Id, E_Constant);
4691 Set_Actual_Subtype (Id, Etype (Entity (E)));
4692 end if;
4694 -- Deal with setting of null flags
4696 if Is_Access_Type (T) then
4697 if Known_Non_Null (E) then
4698 Set_Is_Known_Non_Null (Id, True);
4699 elsif Known_Null (E) and then not Can_Never_Be_Null (Id) then
4700 Set_Is_Known_Null (Id, True);
4701 end if;
4702 end if;
4704 -- Check incorrect use of dynamically tagged expressions
4706 if Is_Tagged_Type (T) then
4707 Check_Dynamically_Tagged_Expression
4708 (Expr => E,
4709 Typ => T,
4710 Related_Nod => N);
4711 end if;
4713 Apply_Scalar_Range_Check (E, T);
4714 Apply_Static_Length_Check (E, T);
4716 -- A formal parameter of a specific tagged type whose related
4717 -- subprogram is subject to pragma Extensions_Visible with value
4718 -- "False" cannot be implicitly converted to a class-wide type by
4719 -- means of an initialization expression (SPARK RM 6.1.7(3)). Do
4720 -- not consider internally generated expressions.
4722 if Is_Class_Wide_Type (T)
4723 and then Comes_From_Source (E)
4724 and then Is_EVF_Expression (E)
4725 then
4726 Error_Msg_N
4727 ("formal parameter cannot be implicitly converted to "
4728 & "class-wide type when Extensions_Visible is False", E);
4729 end if;
4730 end if;
4732 -- If the No_Streams restriction is set, check that the type of the
4733 -- object is not, and does not contain, any subtype derived from
4734 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
4735 -- Has_Stream just for efficiency reasons. There is no point in
4736 -- spending time on a Has_Stream check if the restriction is not set.
4738 if Restriction_Check_Required (No_Streams) then
4739 if Has_Stream (T) then
4740 Check_Restriction (No_Streams, N);
4741 end if;
4742 end if;
4744 -- Deal with predicate check before we start to do major rewriting. It
4745 -- is OK to initialize and then check the initialized value, since the
4746 -- object goes out of scope if we get a predicate failure. Note that we
4747 -- do this in the analyzer and not the expander because the analyzer
4748 -- does some substantial rewriting in some cases.
4750 -- We need a predicate check if the type has predicates that are not
4751 -- ignored, and if either there is an initializing expression, or for
4752 -- default initialization when we have at least one case of an explicit
4753 -- default initial value (including via a Default_Value or
4754 -- Default_Component_Value aspect, see AI12-0301) and then this is not
4755 -- an internal declaration whose initialization comes later (as for an
4756 -- aggregate expansion) or a deferred constant.
4757 -- If expression is an aggregate it may be expanded into assignments
4758 -- and the declaration itself is marked with No_Initialization, but
4759 -- the predicate still applies.
4761 if not Suppress_Assignment_Checks (N)
4762 and then (Predicate_Enabled (T) or else Has_Static_Predicate (T))
4763 and then
4764 (not No_Initialization (N)
4765 or else (Present (E) and then Nkind (E) = N_Aggregate))
4766 and then
4767 (Present (E)
4768 or else
4769 Is_Partially_Initialized_Type (T, Include_Implicit => False))
4770 and then not (Constant_Present (N) and then No (E))
4771 then
4772 -- If the type has a static predicate and the expression is known at
4773 -- compile time, see if the expression satisfies the predicate.
4774 -- In the case of a static expression, this must be done even if
4775 -- the predicate is not enabled (as per static expression rules).
4777 if Present (E) then
4778 Check_Expression_Against_Static_Predicate (E, T);
4779 end if;
4781 -- Do not perform further predicate-related checks unless
4782 -- predicates are enabled for the subtype.
4784 if not Predicate_Enabled (T) then
4785 null;
4787 -- If the type is a null record and there is no explicit initial
4788 -- expression, no predicate check applies.
4790 elsif No (E) and then Is_Null_Record_Type (T) then
4791 null;
4793 -- If there is an address clause for this object, do not generate a
4794 -- predicate check here. It will be generated later, at the freezng
4795 -- point. It would be wrong to generate references to the object
4796 -- here, before the address has been determined.
4798 elsif Has_Aspect (Id, Aspect_Address)
4799 or else Present (Following_Address_Clause (N))
4800 then
4801 null;
4803 -- Do not generate a predicate check if the initialization expression
4804 -- is a type conversion whose target subtype statically matches the
4805 -- object's subtype because the conversion has been subjected to the
4806 -- same check. This is a small optimization which avoids redundant
4807 -- checks.
4809 elsif Present (E)
4810 and then Nkind (E) in N_Type_Conversion
4811 and then Subtypes_Statically_Match (Etype (Subtype_Mark (E)), T)
4812 then
4813 null;
4815 else
4816 -- The check must be inserted after the expanded aggregate
4817 -- expansion code, if any.
4819 declare
4820 Check : constant Node_Id :=
4821 Make_Predicate_Check (T, New_Occurrence_Of (Id, Loc));
4822 begin
4823 if No (Next_Decl) then
4824 Append_To (List_Containing (N), Check);
4825 else
4826 Insert_Before (Next_Decl, Check);
4827 end if;
4828 end;
4829 end if;
4830 end if;
4832 -- Case of unconstrained type
4834 if not Is_Definite_Subtype (T) then
4836 -- Nothing to do in deferred constant case
4838 if Constant_Present (N) and then No (E) then
4839 null;
4841 -- Case of no initialization present
4843 elsif No (E) then
4844 if No_Initialization (N) then
4845 null;
4847 elsif Is_Class_Wide_Type (T) then
4849 -- Case of a mutably tagged type
4851 if Is_Mutably_Tagged_Type (T) then
4852 Act_T := Class_Wide_Equivalent_Type (T);
4854 Rewrite (Object_Definition (N),
4855 New_Occurrence_Of (Act_T, Loc));
4857 Insert_After (N,
4858 Make_Procedure_Call_Statement (Loc,
4859 Name => New_Occurrence_Of (Init_Proc (Etype (T)), Loc),
4860 Parameter_Associations => New_List (
4861 Unchecked_Convert_To
4862 (Etype (T), New_Occurrence_Of (Id, Loc)))));
4864 Freeze_Before (N, Act_T);
4866 -- Otherwise an initial expression is required
4868 else
4869 Error_Msg_N
4870 ("initialization required in class-wide declaration", N);
4871 end if;
4873 else
4874 Error_Msg_N
4875 ("unconstrained subtype not allowed (need initialization)",
4876 Object_Definition (N));
4878 if Is_Record_Type (T) and then Has_Discriminants (T) then
4879 Error_Msg_N
4880 ("\provide initial value or explicit discriminant values",
4881 Object_Definition (N));
4883 Error_Msg_NE
4884 ("\or give default discriminant values for type&",
4885 Object_Definition (N), T);
4887 elsif Is_Array_Type (T) then
4888 Error_Msg_N
4889 ("\provide initial value or explicit array bounds",
4890 Object_Definition (N));
4891 end if;
4892 end if;
4894 -- Case of initialization present but in error. Set initial
4895 -- expression as absent (but do not make above complaints).
4897 elsif E = Error then
4898 Set_Expression (N, Empty);
4899 E := Empty;
4901 -- Case of initialization present
4903 else
4904 -- Unconstrained variables not allowed in Ada 83
4906 if Ada_Version = Ada_83
4907 and then not Constant_Present (N)
4908 and then Comes_From_Source (Object_Definition (N))
4909 then
4910 Error_Msg_N
4911 ("(Ada 83) unconstrained variable not allowed",
4912 Object_Definition (N));
4913 end if;
4915 -- Now we constrain the variable from the initializing expression
4917 -- If the expression is an aggregate, it has been expanded into
4918 -- individual assignments. Retrieve the actual type from the
4919 -- expanded construct.
4921 if Is_Array_Type (T)
4922 and then No_Initialization (N)
4923 and then Nkind (Original_Node (E)) = N_Aggregate
4924 then
4925 Act_T := Etype (E);
4927 -- In case of class-wide interface object declarations we delay
4928 -- the generation of the equivalent record type declarations until
4929 -- its expansion because there are cases in they are not required.
4931 elsif Is_Interface (T) then
4932 null;
4934 -- If the type is an unchecked union, no subtype can be built from
4935 -- the expression. Rewrite declaration as a renaming, which the
4936 -- back-end can handle properly. This is a rather unusual case,
4937 -- because most unchecked_union declarations have default values
4938 -- for discriminants and are thus not indefinite.
4940 elsif Is_Unchecked_Union (T) then
4941 if Constant_Present (N) or else Nkind (E) = N_Function_Call then
4942 Mutate_Ekind (Id, E_Constant);
4943 else
4944 Mutate_Ekind (Id, E_Variable);
4945 end if;
4947 -- If the expression is an aggregate it contains the required
4948 -- discriminant values but it has not been resolved yet, so do
4949 -- it now, and treat it as the initial expression of an object
4950 -- declaration, rather than a renaming.
4952 if Nkind (E) = N_Aggregate then
4953 Analyze_And_Resolve (E, T);
4955 else
4956 Rewrite (N,
4957 Make_Object_Renaming_Declaration (Loc,
4958 Defining_Identifier => Id,
4959 Subtype_Mark => New_Occurrence_Of (T, Loc),
4960 Name => E));
4962 Set_Renamed_Object (Id, E);
4963 Freeze_Before (N, T);
4964 Set_Is_Frozen (Id);
4965 goto Leave;
4966 end if;
4968 -- Rewrite mutably tagged class-wide type declarations to be that
4969 -- of the corresponding class-wide equivalent type.
4971 elsif Is_Mutably_Tagged_Type (T) then
4972 Act_T := Class_Wide_Equivalent_Type (T);
4974 Rewrite (Object_Definition (N),
4975 New_Occurrence_Of (Act_T, Loc));
4977 Freeze_Before (N, Act_T);
4979 else
4980 -- Ensure that the generated subtype has a unique external name
4981 -- when the related object is public. This guarantees that the
4982 -- subtype and its bounds will not be affected by switches or
4983 -- pragmas that may offset the internal counter due to extra
4984 -- generated code.
4986 if Is_Public (Id) then
4987 Related_Id := Id;
4988 else
4989 Related_Id := Empty;
4990 end if;
4992 -- If the object has an unconstrained array subtype with fixed
4993 -- lower bound, then sliding to that bound may be needed.
4995 if Is_Fixed_Lower_Bound_Array_Subtype (T) then
4996 Expand_Sliding_Conversion (E, T);
4997 end if;
4999 if In_Spec_Expression and then In_Declare_Expr > 0 then
5000 -- It is too early to be doing expansion-ish things,
5001 -- so exit early. But we have to set Ekind (Id) now so
5002 -- that subsequent uses of this entity are not rejected
5003 -- via the same mechanism that (correctly) rejects
5004 -- "X : Integer := X;".
5006 if Constant_Present (N) then
5007 Mutate_Ekind (Id, E_Constant);
5008 Set_Is_True_Constant (Id);
5009 else
5010 Mutate_Ekind (Id, E_Variable);
5011 if Present (E) then
5012 Set_Has_Initial_Value (Id);
5013 end if;
5014 end if;
5016 goto Leave;
5017 end if;
5019 Expand_Subtype_From_Expr
5020 (N => N,
5021 Unc_Type => T,
5022 Subtype_Indic => Object_Definition (N),
5023 Exp => E,
5024 Related_Id => Related_Id);
5026 Act_T := Find_Type_Of_Object (Object_Definition (N), N);
5027 end if;
5029 if Act_T /= T then
5030 declare
5031 Full_Act_T : constant Entity_Id :=
5032 (if Is_Private_Type (Act_T)
5033 then Full_View (Act_T)
5034 else Empty);
5035 -- Propagate attributes to full view when needed
5037 begin
5038 Set_Is_Constr_Subt_For_U_Nominal (Act_T);
5040 if Present (Full_Act_T) then
5041 Set_Is_Constr_Subt_For_U_Nominal (Full_Act_T);
5042 end if;
5044 -- If the object is aliased, then it may be pointed to by an
5045 -- access-to-unconstrained-array value, which means that it
5046 -- must be allocated with its bounds.
5048 if Aliased_Present (N)
5049 and then (Is_Array_Type (Act_T)
5050 or else (Present (Full_Act_T)
5051 and then Is_Array_Type (Full_Act_T)))
5052 then
5053 Set_Is_Constr_Array_Subt_With_Bounds (Act_T);
5055 if Present (Full_Act_T) then
5056 Set_Is_Constr_Array_Subt_With_Bounds (Full_Act_T);
5057 end if;
5058 end if;
5060 Freeze_Before (N, Act_T);
5061 end;
5062 end if;
5064 Freeze_Before (N, T);
5065 end if;
5067 elsif Is_Array_Type (T)
5068 and then No_Initialization (N)
5069 and then (Nkind (Original_Node (E)) = N_Aggregate
5070 or else (Nkind (Original_Node (E)) = N_Qualified_Expression
5071 and then Nkind (Original_Node (Expression
5072 (Original_Node (E)))) = N_Aggregate))
5073 then
5074 if not Is_Entity_Name (Object_Definition (N)) then
5075 Act_T := Etype (E);
5076 Check_Compile_Time_Size (Act_T);
5077 end if;
5079 -- When the given object definition and the aggregate are specified
5080 -- independently, and their lengths might differ do a length check.
5081 -- This cannot happen if the aggregate is of the form (others =>...)
5083 if Nkind (E) = N_Raise_Constraint_Error then
5085 -- Aggregate is statically illegal. Place back in declaration
5087 Set_Expression (N, E);
5088 Set_No_Initialization (N, False);
5090 elsif T = Etype (E) then
5091 null;
5093 elsif Nkind (E) = N_Aggregate
5094 and then Present (Component_Associations (E))
5095 and then Present (Choice_List (First (Component_Associations (E))))
5096 and then
5097 Nkind (First (Choice_List (First (Component_Associations (E))))) =
5098 N_Others_Choice
5099 then
5100 null;
5102 else
5103 Apply_Length_Check (E, T);
5104 end if;
5106 -- When possible, and not a deferred constant, build the default subtype
5108 elsif Build_Default_Subtype_OK (T)
5109 and then (not Constant_Present (N) or else Present (E))
5110 then
5111 if No (E) then
5112 Act_T := Build_Default_Subtype (T, N);
5113 else
5114 -- Ada 2005: A limited object may be initialized by means of an
5115 -- aggregate. If the type has default discriminants it has an
5116 -- unconstrained nominal type, Its actual subtype will be obtained
5117 -- from the aggregate, and not from the default discriminants.
5119 Act_T := Etype (E);
5120 end if;
5122 Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
5123 Freeze_Before (N, Act_T);
5125 elsif Nkind (E) = N_Function_Call
5126 and then Constant_Present (N)
5127 and then Has_Unconstrained_Elements (Etype (E))
5128 then
5129 -- The back-end has problems with constants of a discriminated type
5130 -- with defaults, if the initial value is a function call. We
5131 -- generate an intermediate temporary that will receive a reference
5132 -- to the result of the call. The initialization expression then
5133 -- becomes a dereference of that temporary.
5135 Remove_Side_Effects (E);
5137 -- If this is a constant declaration of an unconstrained type and
5138 -- the initialization is an aggregate, we can use the subtype of the
5139 -- aggregate for the declared entity because it is immutable.
5141 elsif not Is_Constrained (T)
5142 and then Has_Discriminants (T)
5143 and then Constant_Present (N)
5144 and then not Has_Unchecked_Union (T)
5145 and then Nkind (E) = N_Aggregate
5146 then
5147 Act_T := Etype (E);
5148 end if;
5150 -- Check No_Wide_Characters restriction
5152 Check_Wide_Character_Restriction (T, Object_Definition (N));
5154 -- Indicate this is not set in source. Certainly true for constants, and
5155 -- true for variables so far (will be reset for a variable if and when
5156 -- we encounter a modification in the source).
5158 Set_Never_Set_In_Source (Id);
5160 -- Now establish the proper kind and type of the object
5162 if Ekind (Id) = E_Void then
5163 Reinit_Field_To_Zero (Id, F_Next_Inlined_Subprogram);
5164 end if;
5166 if Constant_Present (N) then
5167 Mutate_Ekind (Id, E_Constant);
5168 Set_Is_True_Constant (Id);
5170 else
5171 Mutate_Ekind (Id, E_Variable);
5173 -- A variable is set as shared passive if it appears in a shared
5174 -- passive package, and is at the outer level. This is not done for
5175 -- entities generated during expansion, because those are always
5176 -- manipulated locally.
5178 if Is_Shared_Passive (Current_Scope)
5179 and then Is_Library_Level_Entity (Id)
5180 and then Comes_From_Source (Id)
5181 then
5182 Set_Is_Shared_Passive (Id);
5183 Check_Shared_Var (Id, T, N);
5184 end if;
5186 -- Set Has_Initial_Value if initializing expression present. Note
5187 -- that if there is no initializing expression, we leave the state
5188 -- of this flag unchanged (usually it will be False, but notably in
5189 -- the case of exception choice variables, it will already be true).
5191 if Present (E) then
5192 Set_Has_Initial_Value (Id);
5193 end if;
5194 end if;
5196 -- Set the SPARK mode from the current context (may be overwritten later
5197 -- with explicit pragma).
5199 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
5200 Set_SPARK_Pragma_Inherited (Id);
5202 -- Preserve relevant elaboration-related attributes of the context which
5203 -- are no longer available or very expensive to recompute once analysis,
5204 -- resolution, and expansion are over.
5206 Mark_Elaboration_Attributes
5207 (N_Id => Id,
5208 Checks => True,
5209 Warnings => True);
5211 -- Initialize alignment and size and capture alignment setting
5213 Reinit_Alignment (Id);
5214 Reinit_Esize (Id);
5215 Set_Optimize_Alignment_Flags (Id);
5217 -- Deal with aliased case
5219 if Aliased_Present (N) then
5220 Set_Is_Aliased (Id);
5222 -- AI12-001: All aliased objects are considered to be specified as
5223 -- independently addressable (RM C.6(8.1/4)).
5225 Set_Is_Independent (Id);
5227 -- If the object is aliased and the type is unconstrained with
5228 -- defaulted discriminants and there is no expression, then the
5229 -- object is constrained by the defaults, so it is worthwhile
5230 -- building the corresponding subtype.
5232 -- Ada 2005 (AI-363): If the aliased object is discriminated and
5233 -- unconstrained, then only establish an actual subtype if the
5234 -- nominal subtype is indefinite. In definite cases the object is
5235 -- unconstrained in Ada 2005.
5237 if No (E)
5238 and then Is_Record_Type (T)
5239 and then not Is_Constrained (T)
5240 and then Has_Discriminants (T)
5241 and then (Ada_Version < Ada_2005
5242 or else not Is_Definite_Subtype (T))
5243 then
5244 Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
5245 end if;
5246 end if;
5248 -- Now we can set the type of the object
5250 Set_Etype (Id, Act_T);
5252 -- Non-constant object is marked to be treated as volatile if type is
5253 -- volatile and we clear the Current_Value setting that may have been
5254 -- set above. Doing so for constants isn't required and might interfere
5255 -- with possible uses of the object as a static expression in contexts
5256 -- incompatible with volatility (e.g. as a case-statement alternative).
5258 if Ekind (Id) /= E_Constant and then Treat_As_Volatile (Etype (Id)) then
5259 Set_Treat_As_Volatile (Id);
5260 Set_Current_Value (Id, Empty);
5261 end if;
5263 -- Deal with controlled types
5265 if Has_Controlled_Component (Etype (Id))
5266 or else Is_Controlled (Etype (Id))
5267 then
5268 if not Is_Library_Level_Entity (Id) then
5269 Check_Restriction (No_Nested_Finalization, N);
5270 else
5271 Validate_Controlled_Object (Id);
5272 end if;
5274 -- If the type of a constrained array has an unconstrained first
5275 -- subtype, its Finalize_Address primitive expects the address of
5276 -- an object with a dope vector (see Make_Finalize_Address_Stmts).
5278 if Is_Array_Type (Etype (Id))
5279 and then Is_Constrained (Etype (Id))
5280 and then not Is_Constrained (First_Subtype (Etype (Id)))
5281 then
5282 Set_Is_Constr_Array_Subt_With_Bounds (Etype (Id));
5283 end if;
5284 end if;
5286 if Has_Task (Etype (Id)) then
5287 Check_Restriction (No_Tasking, N);
5289 -- Deal with counting max tasks
5291 -- Nothing to do if inside a generic
5293 if Inside_A_Generic then
5294 null;
5296 -- If library level entity, then count tasks
5298 elsif Is_Library_Level_Entity (Id) then
5299 Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
5301 -- If not library level entity, then indicate we don't know max
5302 -- tasks and also check task hierarchy restriction and blocking
5303 -- operation (since starting a task is definitely blocking).
5305 else
5306 Check_Restriction (Max_Tasks, N);
5307 Check_Restriction (No_Task_Hierarchy, N);
5308 Check_Potentially_Blocking_Operation (N);
5309 end if;
5311 -- A rather specialized test. If we see two tasks being declared
5312 -- of the same type in the same object declaration, and the task
5313 -- has an entry with an address clause, we know that program error
5314 -- will be raised at run time since we can't have two tasks with
5315 -- entries at the same address.
5317 if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
5318 declare
5319 E : Entity_Id;
5321 begin
5322 E := First_Entity (Etype (Id));
5323 while Present (E) loop
5324 if Ekind (E) = E_Entry
5325 and then Present (Address_Clause (E))
5326 then
5327 Error_Msg_Warn := SPARK_Mode /= On;
5328 Error_Msg_N
5329 ("more than one task with same entry address<<", N);
5330 Error_Msg_N ("\Program_Error [<<", N);
5331 Insert_Action (N,
5332 Make_Raise_Program_Error (Loc,
5333 Reason => PE_Duplicated_Entry_Address));
5334 exit;
5335 end if;
5337 Next_Entity (E);
5338 end loop;
5339 end;
5340 end if;
5341 end if;
5343 -- Check specific legality rules for a return object
5345 if Is_Return_Object (Id) then
5346 Check_Return_Subtype_Indication (N);
5347 end if;
5349 -- Some simple constant-propagation: if the expression is a constant
5350 -- string initialized with a literal, share the literal. This avoids
5351 -- a run-time copy.
5353 if Present (E)
5354 and then Is_Entity_Name (E)
5355 and then Ekind (Entity (E)) = E_Constant
5356 and then Base_Type (Etype (E)) = Standard_String
5357 then
5358 declare
5359 Val : constant Node_Id := Constant_Value (Entity (E));
5360 begin
5361 if Present (Val) and then Nkind (Val) = N_String_Literal then
5362 Rewrite (E, New_Copy (Val));
5363 end if;
5364 end;
5365 end if;
5367 if Present (Prev_Entity)
5368 and then Is_Frozen (Prev_Entity)
5369 and then not Error_Posted (Id)
5370 then
5371 Error_Msg_N ("full constant declaration appears too late", N);
5372 end if;
5374 Check_Eliminated (Id);
5376 -- Deal with setting In_Private_Part flag if in private part
5378 if Ekind (Scope (Id)) = E_Package
5379 and then In_Private_Part (Scope (Id))
5380 then
5381 Set_In_Private_Part (Id);
5382 end if;
5384 <<Leave>>
5385 -- Initialize the refined state of a variable here because this is a
5386 -- common destination for legal and illegal object declarations.
5388 if Ekind (Id) = E_Variable then
5389 Set_Encapsulating_State (Id, Empty);
5390 end if;
5392 Analyze_Aspect_Specifications (N, Id);
5394 Analyze_Dimension (N);
5396 -- Verify whether the object declaration introduces an illegal hidden
5397 -- state within a package subject to a null abstract state.
5399 if Ekind (Id) = E_Variable then
5400 Check_No_Hidden_State (Id);
5401 end if;
5403 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5404 end Analyze_Object_Declaration;
5406 ---------------------------
5407 -- Analyze_Others_Choice --
5408 ---------------------------
5410 -- Nothing to do for the others choice node itself, the semantic analysis
5411 -- of the others choice will occur as part of the processing of the parent
5413 procedure Analyze_Others_Choice (N : Node_Id) is
5414 pragma Warnings (Off, N);
5415 begin
5416 null;
5417 end Analyze_Others_Choice;
5419 -------------------------------------------
5420 -- Analyze_Private_Extension_Declaration --
5421 -------------------------------------------
5423 procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
5424 Indic : constant Node_Id := Subtype_Indication (N);
5425 T : constant Entity_Id := Defining_Identifier (N);
5426 Iface : Entity_Id;
5427 Iface_Elmt : Elmt_Id;
5428 Parent_Base : Entity_Id;
5429 Parent_Type : Entity_Id;
5431 begin
5432 -- Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
5434 if Is_Non_Empty_List (Interface_List (N)) then
5435 declare
5436 Intf : Node_Id;
5437 T : Entity_Id;
5439 begin
5440 Intf := First (Interface_List (N));
5441 while Present (Intf) loop
5442 T := Find_Type_Of_Subtype_Indic (Intf);
5444 Diagnose_Interface (Intf, T);
5445 Next (Intf);
5446 end loop;
5447 end;
5448 end if;
5450 Generate_Definition (T);
5452 -- For other than Ada 2012, just enter the name in the current scope
5454 if Ada_Version < Ada_2012 then
5455 Enter_Name (T);
5457 -- Ada 2012 (AI05-0162): Enter the name in the current scope handling
5458 -- case of private type that completes an incomplete type.
5460 else
5461 declare
5462 Prev : Entity_Id;
5464 begin
5465 Prev := Find_Type_Name (N);
5467 pragma Assert (Prev = T
5468 or else (Ekind (Prev) = E_Incomplete_Type
5469 and then Present (Full_View (Prev))
5470 and then Full_View (Prev) = T));
5471 end;
5472 end if;
5474 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
5475 Parent_Base := Base_Type (Parent_Type);
5477 if Parent_Type = Any_Type or else Etype (Parent_Type) = Any_Type then
5478 Mutate_Ekind (T, Ekind (Parent_Type));
5479 Set_Etype (T, Any_Type);
5480 goto Leave;
5482 elsif not Is_Tagged_Type (Parent_Type) then
5483 Error_Msg_N
5484 ("parent of type extension must be a tagged type", Indic);
5485 goto Leave;
5487 elsif Ekind (Parent_Type) in E_Void | E_Incomplete_Type then
5488 Error_Msg_N ("premature derivation of incomplete type", Indic);
5489 goto Leave;
5491 elsif Is_Concurrent_Type (Parent_Type) then
5492 Error_Msg_N
5493 ("parent type of a private extension cannot be a synchronized "
5494 & "tagged type (RM 3.9.1 (3/1))", N);
5496 Set_Etype (T, Any_Type);
5497 Mutate_Ekind (T, E_Limited_Private_Type);
5498 Set_Private_Dependents (T, New_Elmt_List);
5499 Set_Error_Posted (T);
5500 goto Leave;
5501 end if;
5503 Check_Wide_Character_Restriction (Parent_Type, Indic);
5505 -- Perhaps the parent type should be changed to the class-wide type's
5506 -- specific type in this case to prevent cascading errors ???
5508 if Is_Class_Wide_Type (Parent_Type) then
5509 Error_Msg_N
5510 ("parent of type extension must not be a class-wide type", Indic);
5511 goto Leave;
5512 end if;
5514 if (not Is_Package_Or_Generic_Package (Current_Scope)
5515 and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
5516 or else In_Private_Part (Current_Scope)
5517 then
5518 Error_Msg_N ("invalid context for private extension", N);
5519 end if;
5521 -- Set common attributes
5523 Set_Is_Pure (T, Is_Pure (Current_Scope));
5524 Set_Scope (T, Current_Scope);
5525 Mutate_Ekind (T, E_Record_Type_With_Private);
5526 Reinit_Size_Align (T);
5527 Set_Default_SSO (T);
5528 Set_No_Reordering (T, No_Component_Reordering);
5529 Set_Etype (T, Parent_Base);
5530 Set_Convention (T, Convention (Parent_Type));
5531 Set_First_Rep_Item (T, First_Rep_Item (Parent_Type));
5532 Set_Is_First_Subtype (T);
5534 -- Set the SPARK mode from the current context
5536 Set_SPARK_Pragma (T, SPARK_Mode_Pragma);
5537 Set_SPARK_Pragma_Inherited (T);
5539 if Unknown_Discriminants_Present (N) then
5540 Set_Discriminant_Constraint (T, No_Elist);
5541 end if;
5543 Build_Derived_Record_Type (N, Parent_Type, T);
5545 -- A private extension inherits the Default_Initial_Condition pragma
5546 -- coming from any parent type within the derivation chain.
5548 if Has_DIC (Parent_Type) then
5549 Set_Has_Inherited_DIC (T);
5550 end if;
5552 -- A private extension inherits any class-wide invariants coming from a
5553 -- parent type or an interface. Note that the invariant procedure of the
5554 -- parent type should not be inherited because the private extension may
5555 -- define invariants of its own.
5557 if Has_Inherited_Invariants (Parent_Type)
5558 or else Has_Inheritable_Invariants (Parent_Type)
5559 then
5560 Set_Has_Inherited_Invariants (T);
5562 elsif Present (Interfaces (T)) then
5563 Iface_Elmt := First_Elmt (Interfaces (T));
5564 while Present (Iface_Elmt) loop
5565 Iface := Node (Iface_Elmt);
5567 if Has_Inheritable_Invariants (Iface) then
5568 Set_Has_Inherited_Invariants (T);
5569 exit;
5570 end if;
5572 Next_Elmt (Iface_Elmt);
5573 end loop;
5574 end if;
5576 -- Ada 2005 (AI-443): Synchronized private extension or a rewritten
5577 -- synchronized formal derived type.
5579 if Ada_Version >= Ada_2005 and then Synchronized_Present (N) then
5580 Set_Is_Limited_Record (T);
5582 -- Formal derived type case
5584 if Is_Generic_Type (T) then
5586 -- The parent must be a tagged limited type or a synchronized
5587 -- interface.
5589 if (not Is_Tagged_Type (Parent_Type)
5590 or else not Is_Limited_Type (Parent_Type))
5591 and then
5592 (not Is_Interface (Parent_Type)
5593 or else not Is_Synchronized_Interface (Parent_Type))
5594 then
5595 Error_Msg_NE
5596 ("parent type of & must be tagged limited or synchronized",
5597 N, T);
5598 end if;
5600 -- The progenitors (if any) must be limited or synchronized
5601 -- interfaces.
5603 if Present (Interfaces (T)) then
5604 Iface_Elmt := First_Elmt (Interfaces (T));
5605 while Present (Iface_Elmt) loop
5606 Iface := Node (Iface_Elmt);
5608 if not Is_Limited_Interface (Iface)
5609 and then not Is_Synchronized_Interface (Iface)
5610 then
5611 Error_Msg_NE
5612 ("progenitor & must be limited or synchronized",
5613 N, Iface);
5614 end if;
5616 Next_Elmt (Iface_Elmt);
5617 end loop;
5618 end if;
5620 -- Regular derived extension, the parent must be a limited or
5621 -- synchronized interface.
5623 else
5624 if not Is_Interface (Parent_Type)
5625 or else (not Is_Limited_Interface (Parent_Type)
5626 and then not Is_Synchronized_Interface (Parent_Type))
5627 then
5628 Error_Msg_NE
5629 ("parent type of & must be limited interface", N, T);
5630 end if;
5631 end if;
5633 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
5634 -- extension with a synchronized parent must be explicitly declared
5635 -- synchronized, because the full view will be a synchronized type.
5636 -- This must be checked before the check for limited types below,
5637 -- to ensure that types declared limited are not allowed to extend
5638 -- synchronized interfaces.
5640 elsif Is_Interface (Parent_Type)
5641 and then Is_Synchronized_Interface (Parent_Type)
5642 and then not Synchronized_Present (N)
5643 then
5644 Error_Msg_NE
5645 ("private extension of& must be explicitly synchronized",
5646 N, Parent_Type);
5648 elsif Limited_Present (N) then
5649 Set_Is_Limited_Record (T);
5651 if not Is_Limited_Type (Parent_Type)
5652 and then
5653 (not Is_Interface (Parent_Type)
5654 or else not Is_Limited_Interface (Parent_Type))
5655 then
5656 Error_Msg_NE ("parent type& of limited extension must be limited",
5657 N, Parent_Type);
5658 end if;
5659 end if;
5661 -- Remember that its parent type has a private extension. Used to warn
5662 -- on public primitives of the parent type defined after its private
5663 -- extensions (see Check_Dispatching_Operation).
5665 Set_Has_Private_Extension (Parent_Type);
5667 <<Leave>>
5668 Analyze_Aspect_Specifications (N, T);
5669 end Analyze_Private_Extension_Declaration;
5671 ---------------------------------
5672 -- Analyze_Subtype_Declaration --
5673 ---------------------------------
5675 procedure Analyze_Subtype_Declaration
5676 (N : Node_Id;
5677 Skip : Boolean := False)
5679 Id : constant Entity_Id := Defining_Identifier (N);
5680 T : Entity_Id;
5682 begin
5683 Generate_Definition (Id);
5684 Set_Is_Pure (Id, Is_Pure (Current_Scope));
5685 Reinit_Size_Align (Id);
5687 -- The following guard condition on Enter_Name is to handle cases where
5688 -- the defining identifier has already been entered into the scope but
5689 -- the declaration as a whole needs to be analyzed.
5691 -- This case in particular happens for derived enumeration types. The
5692 -- derived enumeration type is processed as an inserted enumeration type
5693 -- declaration followed by a rewritten subtype declaration. The defining
5694 -- identifier, however, is entered into the name scope very early in the
5695 -- processing of the original type declaration and therefore needs to be
5696 -- avoided here, when the created subtype declaration is analyzed. (See
5697 -- Build_Derived_Types)
5699 -- This also happens when the full view of a private type is a derived
5700 -- type with constraints. In this case the entity has been introduced
5701 -- in the private declaration.
5703 -- Finally this happens in some complex cases when validity checks are
5704 -- enabled, where the same subtype declaration may be analyzed twice.
5705 -- This can happen if the subtype is created by the preanalysis of
5706 -- an attribute that gives the range of a loop statement, and the loop
5707 -- itself appears within an if_statement that will be rewritten during
5708 -- expansion.
5710 if Skip
5711 or else (Present (Etype (Id))
5712 and then (Is_Private_Type (Etype (Id))
5713 or else Is_Task_Type (Etype (Id))
5714 or else Is_Rewrite_Substitution (N)))
5715 then
5716 null;
5718 elsif Current_Entity (Id) = Id then
5719 null;
5721 else
5722 Enter_Name (Id);
5723 end if;
5725 T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
5727 -- Class-wide equivalent types of records with unknown discriminants
5728 -- involve the generation of an itype which serves as the private view
5729 -- of a constrained record subtype. In such cases the base type of the
5730 -- current subtype we are processing is the private itype. Use the full
5731 -- of the private itype when decorating various attributes.
5733 if Is_Itype (T)
5734 and then Is_Private_Type (T)
5735 and then Present (Full_View (T))
5736 then
5737 T := Full_View (T);
5738 end if;
5740 -- Inherit common attributes
5742 Set_Is_Volatile (Id, Is_Volatile (T));
5743 Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
5744 Set_Is_Generic_Type (Id, Is_Generic_Type (Base_Type (T)));
5745 Set_Convention (Id, Convention (T));
5747 -- If ancestor has predicates then so does the subtype, and in addition
5748 -- we must delay the freeze to properly arrange predicate inheritance.
5750 -- The Ancestor_Type test is really unpleasant, there seem to be cases
5751 -- in which T = ID, so the above tests and assignments do nothing???
5753 if Has_Predicates (T)
5754 or else (Present (Ancestor_Subtype (T))
5755 and then Has_Predicates (Ancestor_Subtype (T)))
5756 then
5757 Set_Has_Predicates (Id);
5758 Set_Has_Delayed_Freeze (Id);
5760 -- Generated subtypes inherit the predicate function from the parent
5761 -- (no aspects to examine on the generated declaration).
5763 if not Comes_From_Source (N) then
5764 Mutate_Ekind (Id, Ekind (T));
5766 if Present (Predicate_Function (Id)) then
5767 null;
5769 elsif Present (Predicate_Function (T)) then
5770 Set_Predicate_Function (Id, Predicate_Function (T));
5772 elsif Present (Ancestor_Subtype (T))
5773 and then Present (Predicate_Function (Ancestor_Subtype (T)))
5774 then
5775 Set_Predicate_Function (Id,
5776 Predicate_Function (Ancestor_Subtype (T)));
5777 end if;
5778 end if;
5779 end if;
5781 -- In the case where there is no constraint given in the subtype
5782 -- indication, Process_Subtype just returns the Subtype_Mark, so its
5783 -- semantic attributes must be established here.
5785 if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
5786 Set_Etype (Id, Base_Type (T));
5788 case Ekind (T) is
5789 when Array_Kind =>
5790 Mutate_Ekind (Id, E_Array_Subtype);
5791 Copy_Array_Subtype_Attributes (Id, T);
5792 Set_Packed_Array_Impl_Type (Id, Packed_Array_Impl_Type (T));
5794 when Decimal_Fixed_Point_Kind =>
5795 Mutate_Ekind (Id, E_Decimal_Fixed_Point_Subtype);
5796 Set_Digits_Value (Id, Digits_Value (T));
5797 Set_Delta_Value (Id, Delta_Value (T));
5798 Set_Scale_Value (Id, Scale_Value (T));
5799 Set_Small_Value (Id, Small_Value (T));
5800 Set_Scalar_Range (Id, Scalar_Range (T));
5801 Set_Machine_Radix_10 (Id, Machine_Radix_10 (T));
5802 Set_Is_Constrained (Id, Is_Constrained (T));
5803 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5804 Copy_RM_Size (To => Id, From => T);
5806 when Enumeration_Kind =>
5807 Mutate_Ekind (Id, E_Enumeration_Subtype);
5808 Set_First_Literal (Id, First_Literal (Base_Type (T)));
5809 Set_Scalar_Range (Id, Scalar_Range (T));
5810 Set_Is_Character_Type (Id, Is_Character_Type (T));
5811 Set_Is_Constrained (Id, Is_Constrained (T));
5812 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5813 Copy_RM_Size (To => Id, From => T);
5815 when Ordinary_Fixed_Point_Kind =>
5816 Mutate_Ekind (Id, E_Ordinary_Fixed_Point_Subtype);
5817 Set_Scalar_Range (Id, Scalar_Range (T));
5818 Set_Small_Value (Id, Small_Value (T));
5819 Set_Delta_Value (Id, Delta_Value (T));
5820 Set_Is_Constrained (Id, Is_Constrained (T));
5821 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5822 Copy_RM_Size (To => Id, From => T);
5824 when Float_Kind =>
5825 Mutate_Ekind (Id, E_Floating_Point_Subtype);
5826 Set_Scalar_Range (Id, Scalar_Range (T));
5827 Set_Digits_Value (Id, Digits_Value (T));
5828 Set_Is_Constrained (Id, Is_Constrained (T));
5830 -- If the floating point type has dimensions, these will be
5831 -- inherited subsequently when Analyze_Dimensions is called.
5833 when Signed_Integer_Kind =>
5834 Mutate_Ekind (Id, E_Signed_Integer_Subtype);
5835 Set_Scalar_Range (Id, Scalar_Range (T));
5836 Set_Is_Constrained (Id, Is_Constrained (T));
5837 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5838 Copy_RM_Size (To => Id, From => T);
5840 when Modular_Integer_Kind =>
5841 Mutate_Ekind (Id, E_Modular_Integer_Subtype);
5842 Set_Scalar_Range (Id, Scalar_Range (T));
5843 Set_Is_Constrained (Id, Is_Constrained (T));
5844 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5845 Copy_RM_Size (To => Id, From => T);
5847 when Class_Wide_Kind =>
5848 Mutate_Ekind (Id, E_Class_Wide_Subtype);
5849 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5850 Set_Cloned_Subtype (Id, T);
5851 Set_Is_Tagged_Type (Id, True);
5852 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5853 Set_Has_Unknown_Discriminants
5854 (Id, True);
5855 Set_No_Tagged_Streams_Pragma
5856 (Id, No_Tagged_Streams_Pragma (T));
5858 if Ekind (T) = E_Class_Wide_Subtype then
5859 Set_Equivalent_Type (Id, Equivalent_Type (T));
5860 end if;
5862 when E_Record_Subtype
5863 | E_Record_Type
5865 Mutate_Ekind (Id, E_Record_Subtype);
5867 -- Subtype declarations introduced for formal type parameters
5868 -- in generic instantiations should inherit the Size value of
5869 -- the type they rename.
5871 if Present (Generic_Parent_Type (N)) then
5872 Copy_RM_Size (To => Id, From => T);
5873 end if;
5875 if Ekind (T) = E_Record_Subtype
5876 and then Present (Cloned_Subtype (T))
5877 then
5878 Set_Cloned_Subtype (Id, Cloned_Subtype (T));
5879 else
5880 Set_Cloned_Subtype (Id, T);
5881 end if;
5883 Set_First_Entity (Id, First_Entity (T));
5884 Set_Last_Entity (Id, Last_Entity (T));
5885 Set_Has_Discriminants (Id, Has_Discriminants (T));
5886 Set_Is_Constrained (Id, Is_Constrained (T));
5887 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5888 Set_Has_Implicit_Dereference
5889 (Id, Has_Implicit_Dereference (T));
5890 Set_Has_Unknown_Discriminants
5891 (Id, Has_Unknown_Discriminants (T));
5893 if Has_Discriminants (T) then
5894 Set_Discriminant_Constraint
5895 (Id, Discriminant_Constraint (T));
5896 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5898 elsif Has_Unknown_Discriminants (Id) then
5899 Set_Discriminant_Constraint (Id, No_Elist);
5900 end if;
5902 if Is_Tagged_Type (T) then
5903 Set_Is_Tagged_Type (Id, True);
5904 Set_No_Tagged_Streams_Pragma
5905 (Id, No_Tagged_Streams_Pragma (T));
5906 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5907 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5909 if Is_Interface (T) then
5910 Set_Is_Interface (Id);
5911 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
5912 end if;
5913 end if;
5915 when Private_Kind =>
5916 Mutate_Ekind (Id, Subtype_Kind (Ekind (T)));
5917 Set_Has_Discriminants (Id, Has_Discriminants (T));
5918 Set_Is_Constrained (Id, Is_Constrained (T));
5919 Set_First_Entity (Id, First_Entity (T));
5920 Set_Last_Entity (Id, Last_Entity (T));
5921 Set_Private_Dependents (Id, New_Elmt_List);
5922 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5923 Set_Has_Implicit_Dereference
5924 (Id, Has_Implicit_Dereference (T));
5925 Set_Has_Unknown_Discriminants
5926 (Id, Has_Unknown_Discriminants (T));
5927 Set_Known_To_Have_Preelab_Init
5928 (Id, Known_To_Have_Preelab_Init (T));
5930 if Is_Tagged_Type (T) then
5931 Set_Is_Tagged_Type (Id);
5932 Set_No_Tagged_Streams_Pragma (Id,
5933 No_Tagged_Streams_Pragma (T));
5934 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5935 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5936 end if;
5938 -- In general the attributes of the subtype of a private type
5939 -- are the attributes of the partial view of parent. However,
5940 -- the full view may be a discriminated type, and the subtype
5941 -- must share the discriminant constraint to generate correct
5942 -- calls to initialization procedures.
5944 if Has_Discriminants (T) then
5945 Set_Discriminant_Constraint
5946 (Id, Discriminant_Constraint (T));
5947 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5949 elsif Present (Full_View (T))
5950 and then Has_Discriminants (Full_View (T))
5951 then
5952 Set_Discriminant_Constraint
5953 (Id, Discriminant_Constraint (Full_View (T)));
5954 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5956 -- This would seem semantically correct, but apparently
5957 -- generates spurious errors about missing components ???
5959 -- Set_Has_Discriminants (Id);
5960 end if;
5962 Prepare_Private_Subtype_Completion (Id, N);
5964 -- If this is the subtype of a constrained private type with
5965 -- discriminants that has got a full view and we also have
5966 -- built a completion just above, show that the completion
5967 -- is a clone of the full view to the back-end.
5969 if Has_Discriminants (T)
5970 and then not Has_Unknown_Discriminants (T)
5971 and then not Is_Empty_Elmt_List (Discriminant_Constraint (T))
5972 and then Present (Full_View (T))
5973 and then Present (Full_View (Id))
5974 then
5975 Set_Cloned_Subtype (Full_View (Id), Full_View (T));
5976 end if;
5978 when Access_Kind =>
5979 Mutate_Ekind (Id, E_Access_Subtype);
5980 Set_Is_Constrained (Id, Is_Constrained (T));
5981 Set_Is_Access_Constant
5982 (Id, Is_Access_Constant (T));
5983 Set_Directly_Designated_Type
5984 (Id, Designated_Type (T));
5985 Set_Can_Never_Be_Null (Id, Can_Never_Be_Null (T));
5987 -- A Pure library_item must not contain the declaration of a
5988 -- named access type, except within a subprogram, generic
5989 -- subprogram, task unit, or protected unit, or if it has
5990 -- a specified Storage_Size of zero (RM05-10.2.1(15.4-15.5)).
5992 if Comes_From_Source (Id)
5993 and then In_Pure_Unit
5994 and then not In_Subprogram_Task_Protected_Unit
5995 and then not No_Pool_Assigned (Id)
5996 then
5997 Error_Msg_N
5998 ("named access types not allowed in pure unit", N);
5999 end if;
6001 when Concurrent_Kind =>
6002 Mutate_Ekind (Id, Subtype_Kind (Ekind (T)));
6003 Set_Corresponding_Record_Type (Id,
6004 Corresponding_Record_Type (T));
6005 Set_First_Entity (Id, First_Entity (T));
6006 Set_First_Private_Entity (Id, First_Private_Entity (T));
6007 Set_Has_Discriminants (Id, Has_Discriminants (T));
6008 Set_Is_Constrained (Id, Is_Constrained (T));
6009 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
6010 Set_Last_Entity (Id, Last_Entity (T));
6012 if Is_Tagged_Type (T) then
6013 Set_No_Tagged_Streams_Pragma
6014 (Id, No_Tagged_Streams_Pragma (T));
6015 end if;
6017 if Has_Discriminants (T) then
6018 Set_Discriminant_Constraint
6019 (Id, Discriminant_Constraint (T));
6020 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
6021 end if;
6023 when Incomplete_Kind =>
6024 if Ada_Version >= Ada_2005 then
6026 -- In Ada 2005 an incomplete type can be explicitly tagged:
6027 -- propagate indication. Note that we also have to include
6028 -- subtypes for Ada 2012 extended use of incomplete types.
6030 Mutate_Ekind (Id, E_Incomplete_Subtype);
6031 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
6032 Set_Private_Dependents (Id, New_Elmt_List);
6034 if Is_Tagged_Type (Id) then
6035 Set_No_Tagged_Streams_Pragma
6036 (Id, No_Tagged_Streams_Pragma (T));
6037 end if;
6039 -- Ada 2005 (AI-412): Decorate an incomplete subtype of an
6040 -- incomplete type visible through a limited with clause.
6042 if From_Limited_With (T)
6043 and then Present (Non_Limited_View (T))
6044 then
6045 Set_From_Limited_With (Id);
6046 Set_Non_Limited_View (Id, Non_Limited_View (T));
6048 -- Ada 2005 (AI-412): Add the regular incomplete subtype
6049 -- to the private dependents of the original incomplete
6050 -- type for future transformation.
6052 else
6053 Append_Elmt (Id, Private_Dependents (T));
6054 end if;
6056 -- If the subtype name denotes an incomplete type an error
6057 -- was already reported by Process_Subtype.
6059 else
6060 Set_Etype (Id, Any_Type);
6061 end if;
6063 when others =>
6064 raise Program_Error;
6065 end case;
6067 -- If there is no constraint in the subtype indication, the
6068 -- declared entity inherits predicates from the parent.
6070 Inherit_Predicate_Flags (Id, T);
6071 end if;
6073 if Etype (Id) = Any_Type then
6074 goto Leave;
6075 end if;
6077 -- When prefixed calls are enabled for untagged types, the subtype
6078 -- shares the primitive operations of its base type. Do this even
6079 -- when GNAT extensions are not allowed, in order to give better
6080 -- error messages.
6082 Set_Direct_Primitive_Operations
6083 (Id, Direct_Primitive_Operations (Base_Type (T)));
6085 -- Some common processing on all types
6087 Set_Size_Info (Id, T);
6088 Set_First_Rep_Item (Id, First_Rep_Item (T));
6090 -- If the parent type is a generic actual, so is the subtype. This may
6091 -- happen in a nested instance. Why Comes_From_Source test???
6093 if not Comes_From_Source (N) then
6094 Set_Is_Generic_Actual_Type (Id, Is_Generic_Actual_Type (T));
6095 end if;
6097 -- If this is a subtype declaration for an actual in an instance,
6098 -- inherit static and dynamic predicates if any.
6100 if Has_Predicates (T)
6101 and then Present (Predicate_Function (T))
6102 and then In_Instance
6103 and then not Comes_From_Source (N)
6104 then
6105 -- Inherit Subprograms_For_Type from the full view, if present
6107 if Present (Full_View (T))
6108 and then Present (Subprograms_For_Type (Full_View (T)))
6109 then
6110 Set_Subprograms_For_Type
6111 (Id, Subprograms_For_Type (Full_View (T)));
6112 else
6113 Set_Subprograms_For_Type (Id, Subprograms_For_Type (T));
6114 end if;
6116 -- If the current declaration created both a private and a full view,
6117 -- then propagate Predicate_Function to the latter as well.
6119 if Present (Full_View (Id))
6120 and then No (Predicate_Function (Full_View (Id)))
6121 then
6122 Set_Subprograms_For_Type
6123 (Full_View (Id), Subprograms_For_Type (Id));
6124 end if;
6126 if Has_Static_Predicate (T) then
6127 Set_Has_Static_Predicate (Id);
6128 Set_Static_Discrete_Predicate (Id, Static_Discrete_Predicate (T));
6129 end if;
6130 end if;
6132 -- If the base type is a scalar type, or else if there is no
6133 -- constraint, the atomic flag is inherited by the subtype.
6134 -- Ditto for the Independent aspect.
6136 if Is_Scalar_Type (Id)
6137 or else Is_Entity_Name (Subtype_Indication (N))
6138 then
6139 Set_Is_Atomic (Id, Is_Atomic (T));
6140 Set_Is_Independent (Id, Is_Independent (T));
6141 end if;
6143 -- Remaining processing depends on characteristics of base type
6145 T := Etype (Id);
6147 Set_Is_Immediately_Visible (Id, True);
6148 Set_Depends_On_Private (Id, Has_Private_Component (T));
6149 Set_Is_Descendant_Of_Address (Id, Is_Descendant_Of_Address (T));
6151 if Is_Interface (T) then
6152 Set_Is_Interface (Id);
6153 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
6154 end if;
6156 if Present (Generic_Parent_Type (N))
6157 and then
6158 (Nkind (Parent (Generic_Parent_Type (N))) /=
6159 N_Formal_Type_Declaration
6160 or else Nkind (Formal_Type_Definition
6161 (Parent (Generic_Parent_Type (N)))) /=
6162 N_Formal_Private_Type_Definition)
6163 then
6164 if Is_Tagged_Type (Id) then
6166 -- If this is a generic actual subtype for a synchronized type,
6167 -- the primitive operations are those of the corresponding record
6168 -- for which there is a separate subtype declaration.
6170 if Is_Concurrent_Type (Id) then
6171 null;
6172 elsif Is_Class_Wide_Type (Id) then
6173 Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
6174 else
6175 Derive_Subprograms (Generic_Parent_Type (N), Id, T);
6176 end if;
6178 elsif Scope (Etype (Id)) /= Standard_Standard then
6179 Derive_Subprograms (Generic_Parent_Type (N), Id);
6180 end if;
6181 end if;
6183 if Is_Private_Type (T) and then Present (Full_View (T)) then
6184 Conditional_Delay (Id, Full_View (T));
6186 -- The subtypes of components or subcomponents of protected types
6187 -- do not need freeze nodes, which would otherwise appear in the
6188 -- wrong scope (before the freeze node for the protected type). The
6189 -- proper subtypes are those of the subcomponents of the corresponding
6190 -- record.
6192 elsif Ekind (Scope (Id)) /= E_Protected_Type
6193 and then Present (Scope (Scope (Id))) -- error defense
6194 and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
6195 then
6196 Conditional_Delay (Id, T);
6197 end if;
6199 -- If we have a subtype of an incomplete type whose full type is a
6200 -- derived numeric type, we need to have a freeze node for the subtype.
6201 -- Otherwise gigi will complain while computing the (static) bounds of
6202 -- the subtype.
6204 if Is_Itype (T)
6205 and then Is_Elementary_Type (Id)
6206 and then Etype (Id) /= Id
6207 then
6208 declare
6209 Partial : constant Entity_Id :=
6210 Incomplete_Or_Partial_View (First_Subtype (Id));
6211 begin
6212 if Present (Partial)
6213 and then Ekind (Partial) = E_Incomplete_Type
6214 then
6215 Set_Has_Delayed_Freeze (Id);
6216 end if;
6217 end;
6218 end if;
6220 -- Check that Constraint_Error is raised for a scalar subtype indication
6221 -- when the lower or upper bound of a non-null range lies outside the
6222 -- range of the type mark. Likewise for an array subtype, but check the
6223 -- compatibility for each index.
6225 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
6226 declare
6227 Indic_Typ : constant Entity_Id :=
6228 Underlying_Type (Etype (Subtype_Mark (Subtype_Indication (N))));
6229 Subt_Index : Node_Id;
6230 Target_Index : Node_Id;
6232 begin
6233 if Is_Scalar_Type (Etype (Id))
6234 and then Scalar_Range (Id) /= Scalar_Range (Indic_Typ)
6235 then
6236 Apply_Range_Check (Scalar_Range (Id), Indic_Typ);
6238 elsif Is_Array_Type (Etype (Id))
6239 and then Present (First_Index (Id))
6240 then
6241 Subt_Index := First_Index (Id);
6242 Target_Index := First_Index (Indic_Typ);
6244 while Present (Subt_Index) loop
6245 if ((Nkind (Subt_Index) in N_Expanded_Name | N_Identifier
6246 and then Is_Scalar_Type (Entity (Subt_Index)))
6247 or else Nkind (Subt_Index) = N_Subtype_Indication)
6248 and then
6249 Nkind (Scalar_Range (Etype (Subt_Index))) = N_Range
6250 then
6251 Apply_Range_Check
6252 (Scalar_Range (Etype (Subt_Index)),
6253 Etype (Target_Index),
6254 Insert_Node => N);
6255 end if;
6257 Next_Index (Subt_Index);
6258 Next_Index (Target_Index);
6259 end loop;
6260 end if;
6261 end;
6262 end if;
6264 Set_Optimize_Alignment_Flags (Id);
6265 Check_Eliminated (Id);
6267 <<Leave>>
6268 Analyze_Aspect_Specifications (N, Id);
6270 Analyze_Dimension (N);
6272 -- Check No_Dynamic_Sized_Objects restriction, which disallows subtype
6273 -- indications on composite types where the constraints are dynamic.
6274 -- Note that object declarations and aggregates generate implicit
6275 -- subtype declarations, which this covers. One special case is that the
6276 -- implicitly generated "=" for discriminated types includes an
6277 -- offending subtype declaration, which is harmless, so we ignore it
6278 -- here.
6280 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
6281 declare
6282 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
6283 begin
6284 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint
6285 and then not (Is_Internal (Id)
6286 and then Is_TSS (Scope (Id),
6287 TSS_Composite_Equality))
6288 and then not Within_Init_Proc
6289 and then not All_Composite_Constraints_Static (Cstr)
6290 then
6291 Check_Restriction (No_Dynamic_Sized_Objects, Cstr);
6292 end if;
6293 end;
6294 end if;
6295 end Analyze_Subtype_Declaration;
6297 --------------------------------
6298 -- Analyze_Subtype_Indication --
6299 --------------------------------
6301 procedure Analyze_Subtype_Indication (N : Node_Id) is
6302 T : constant Entity_Id := Subtype_Mark (N);
6303 R : constant Node_Id := Range_Expression (Constraint (N));
6305 begin
6306 Analyze (T);
6308 if R = Error then
6309 Set_Error_Posted (R);
6310 Set_Error_Posted (T);
6311 else
6312 Analyze (R);
6313 Set_Etype (N, Etype (R));
6314 Resolve (R, Entity (T));
6315 end if;
6316 end Analyze_Subtype_Indication;
6318 --------------------------
6319 -- Analyze_Variant_Part --
6320 --------------------------
6322 procedure Analyze_Variant_Part (N : Node_Id) is
6323 Discr_Name : Node_Id;
6324 Discr_Type : Entity_Id;
6326 procedure Process_Variant (A : Node_Id);
6327 -- Analyze declarations for a single variant
6329 package Analyze_Variant_Choices is
6330 new Generic_Analyze_Choices (Process_Variant);
6331 use Analyze_Variant_Choices;
6333 ---------------------
6334 -- Process_Variant --
6335 ---------------------
6337 procedure Process_Variant (A : Node_Id) is
6338 CL : constant Node_Id := Component_List (A);
6339 begin
6340 if not Null_Present (CL) then
6341 Analyze_Declarations (Component_Items (CL));
6343 if Present (Variant_Part (CL)) then
6344 Analyze (Variant_Part (CL));
6345 end if;
6346 end if;
6347 end Process_Variant;
6349 -- Start of processing for Analyze_Variant_Part
6351 begin
6352 Discr_Name := Name (N);
6353 Analyze (Discr_Name);
6355 -- If Discr_Name bad, get out (prevent cascaded errors)
6357 if Etype (Discr_Name) = Any_Type then
6358 return;
6359 end if;
6361 -- Check invalid discriminant in variant part
6363 if Ekind (Entity (Discr_Name)) /= E_Discriminant then
6364 Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
6365 end if;
6367 Discr_Type := Etype (Entity (Discr_Name));
6369 if not Is_Discrete_Type (Discr_Type) then
6370 Error_Msg_N
6371 ("discriminant in a variant part must be of a discrete type",
6372 Name (N));
6373 return;
6374 end if;
6376 -- Now analyze the choices, which also analyzes the declarations that
6377 -- are associated with each choice.
6379 Analyze_Choices (Variants (N), Discr_Type);
6381 -- Note: we used to instantiate and call Check_Choices here to check
6382 -- that the choices covered the discriminant, but it's too early to do
6383 -- that because of statically predicated subtypes, whose analysis may
6384 -- be deferred to their freeze point which may be as late as the freeze
6385 -- point of the containing record. So this call is now to be found in
6386 -- Freeze_Record_Declaration.
6388 end Analyze_Variant_Part;
6390 ----------------------------
6391 -- Array_Type_Declaration --
6392 ----------------------------
6394 procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
6395 Component_Def : constant Node_Id := Component_Definition (Def);
6396 Component_Typ : constant Node_Id := Subtype_Indication (Component_Def);
6397 P : constant Node_Id := Parent (Def);
6398 Element_Type : Entity_Id;
6399 Implicit_Base : Entity_Id;
6400 Index : Node_Id;
6401 Nb_Index : Pos;
6402 Priv : Entity_Id;
6403 Related_Id : Entity_Id;
6404 Has_FLB_Index : Boolean := False;
6406 begin
6407 if Nkind (Def) = N_Constrained_Array_Definition then
6408 Index := First (Discrete_Subtype_Definitions (Def));
6409 else
6410 Index := First (Subtype_Marks (Def));
6411 end if;
6413 -- Find proper names for the implicit types which may be public. In case
6414 -- of anonymous arrays we use the name of the first object of that type
6415 -- as prefix.
6417 if No (T) then
6418 Related_Id := Defining_Identifier (P);
6419 else
6420 Related_Id := T;
6421 end if;
6423 Nb_Index := 1;
6424 while Present (Index) loop
6425 Analyze (Index);
6427 -- Test for odd case of trying to index a type by the type itself
6429 if Is_Entity_Name (Index) and then Entity (Index) = T then
6430 Error_Msg_N ("type& cannot be indexed by itself", Index);
6431 Set_Entity (Index, Standard_Boolean);
6432 Set_Etype (Index, Standard_Boolean);
6433 end if;
6435 -- Add a subtype declaration for each index of private array type
6436 -- declaration whose type is also private. For example:
6438 -- package Pkg is
6439 -- type Index is private;
6440 -- private
6441 -- type Table is array (Index) of ...
6442 -- end;
6444 -- This is currently required by the expander for the internally
6445 -- generated equality subprogram of records with variant parts in
6446 -- which the type of some component is such a private type. And it
6447 -- also helps semantic analysis in peculiar cases where the array
6448 -- type is referenced from an instance but not the index directly.
6450 if Is_Package_Or_Generic_Package (Current_Scope)
6451 and then In_Private_Part (Current_Scope)
6452 and then Has_Private_Declaration (Etype (Index))
6453 and then Scope (Etype (Index)) = Current_Scope
6454 then
6455 declare
6456 Loc : constant Source_Ptr := Sloc (Def);
6457 Decl : Node_Id;
6458 New_E : Entity_Id;
6460 begin
6461 New_E := Make_Temporary (Loc, 'T');
6462 Set_Is_Internal (New_E);
6464 Decl :=
6465 Make_Subtype_Declaration (Loc,
6466 Defining_Identifier => New_E,
6467 Subtype_Indication =>
6468 New_Occurrence_Of (Etype (Index), Loc));
6470 Insert_Before (Parent (Def), Decl);
6471 Analyze (Decl);
6472 Set_Etype (Index, New_E);
6474 -- If the index is a range or a subtype indication it carries
6475 -- no entity. Example:
6477 -- package Pkg is
6478 -- type T is private;
6479 -- private
6480 -- type T is new Natural;
6481 -- Table : array (T(1) .. T(10)) of Boolean;
6482 -- end Pkg;
6484 -- Otherwise the type of the reference is its entity.
6486 if Is_Entity_Name (Index) then
6487 Set_Entity (Index, New_E);
6488 end if;
6489 end;
6490 end if;
6492 Make_Index (Index, P, Related_Id, Nb_Index);
6494 -- In the case where we have an unconstrained array with an index
6495 -- given by a subtype_indication, this is necessarily a "fixed lower
6496 -- bound" index. We change the upper bound of that index to the upper
6497 -- bound of the index's subtype (denoted by the subtype_mark), since
6498 -- that upper bound was originally set by the parser to be the same
6499 -- as the lower bound. In truth, that upper bound corresponds to
6500 -- a box ("<>"), and could be set to Empty, but it's convenient to
6501 -- set it to the upper bound to avoid needing to add special tests
6502 -- in various places for an Empty upper bound, and in any case that
6503 -- accurately characterizes the index's range of values.
6505 if Nkind (Def) = N_Unconstrained_Array_Definition
6506 and then Nkind (Index) = N_Subtype_Indication
6507 then
6508 declare
6509 Index_Subtype_High_Bound : constant Entity_Id :=
6510 Type_High_Bound (Entity (Subtype_Mark (Index)));
6511 begin
6512 Set_High_Bound (Range_Expression (Constraint (Index)),
6513 Index_Subtype_High_Bound);
6515 -- Record that the array type has one or more indexes with
6516 -- a fixed lower bound.
6518 Has_FLB_Index := True;
6520 -- Mark the index as belonging to an array type with a fixed
6521 -- lower bound.
6523 Set_Is_Fixed_Lower_Bound_Index_Subtype (Etype (Index));
6524 end;
6525 end if;
6527 -- Check error of subtype with predicate for index type
6529 Bad_Predicated_Subtype_Use
6530 ("subtype& has predicate, not allowed as index subtype",
6531 Index, Etype (Index));
6533 -- Move to next index
6535 Next (Index);
6536 Nb_Index := Nb_Index + 1;
6537 end loop;
6539 -- Process subtype indication if one is present
6541 if Present (Component_Typ) then
6542 Element_Type := Process_Subtype (Component_Typ, P, Related_Id, 'C');
6543 Set_Etype (Component_Typ, Element_Type);
6545 -- Ada 2005 (AI-230): Access Definition case
6547 else pragma Assert (Present (Access_Definition (Component_Def)));
6549 -- Indicate that the anonymous access type is created by the
6550 -- array type declaration.
6552 Element_Type := Access_Definition
6553 (Related_Nod => P,
6554 N => Access_Definition (Component_Def));
6555 Set_Is_Local_Anonymous_Access (Element_Type);
6557 -- Propagate the parent. This field is needed if we have to generate
6558 -- the master_id associated with an anonymous access to task type
6559 -- component (see Expand_N_Full_Type_Declaration.Build_Master)
6561 Copy_Parent (To => Element_Type, From => T);
6563 -- Ada 2005 (AI-230): In case of components that are anonymous access
6564 -- types the level of accessibility depends on the enclosing type
6565 -- declaration
6567 Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
6569 -- Ada 2005 (AI-254)
6571 declare
6572 CD : constant Node_Id :=
6573 Access_To_Subprogram_Definition
6574 (Access_Definition (Component_Def));
6575 begin
6576 if Present (CD) and then Protected_Present (CD) then
6577 Element_Type :=
6578 Replace_Anonymous_Access_To_Protected_Subprogram (Def);
6579 end if;
6580 end;
6581 end if;
6583 -- Constrained array case
6585 if No (T) then
6586 -- We might be creating more than one itype with the same Related_Id,
6587 -- e.g. for an array object definition and its initial value. Give
6588 -- them unique suffixes, because GNATprove require distinct types to
6589 -- have different names.
6591 T := Create_Itype (E_Void, P, Related_Id, 'T', Suffix_Index => -1);
6592 end if;
6594 if Nkind (Def) = N_Constrained_Array_Definition then
6595 Index := First (Discrete_Subtype_Definitions (Def));
6597 -- Establish Implicit_Base as unconstrained base type
6599 Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
6601 Set_Etype (Implicit_Base, Implicit_Base);
6602 Set_Scope (Implicit_Base, Current_Scope);
6603 Set_First_Index (Implicit_Base, Index);
6604 Set_Has_Delayed_Freeze (Implicit_Base);
6606 -- The constrained array type is a subtype of the unconstrained one
6608 Mutate_Ekind (T, E_Array_Subtype);
6609 Reinit_Size_Align (T);
6610 Set_Etype (T, Implicit_Base);
6611 Set_Scope (T, Current_Scope);
6612 Set_First_Index (T, Index);
6613 Set_Has_Delayed_Freeze (T);
6614 Set_Is_Constrained (T);
6616 -- Unconstrained array case
6618 else pragma Assert (Nkind (Def) = N_Unconstrained_Array_Definition);
6619 Mutate_Ekind (T, E_Array_Type);
6620 Reinit_Size_Align (T);
6621 Set_Etype (T, T);
6622 Set_Scope (T, Current_Scope);
6623 Set_First_Index (T, First (Subtype_Marks (Def)));
6624 Set_Has_Delayed_Freeze (T);
6625 Set_Is_Fixed_Lower_Bound_Array_Subtype
6626 (T, Has_FLB_Index);
6627 end if;
6629 -- Common attributes for both cases
6631 Set_Component_Type (Etype (T), Element_Type);
6633 if Aliased_Present (Component_Definition (Def)) then
6634 Set_Has_Aliased_Components (Etype (T));
6636 -- AI12-001: All aliased objects are considered to be specified as
6637 -- independently addressable (RM C.6(8.1/4)).
6639 Set_Has_Independent_Components (Etype (T));
6640 end if;
6642 pragma Assert (not Known_Component_Size (Etype (T)));
6644 Propagate_Concurrent_Flags (Etype (T), Element_Type);
6645 Propagate_Controlled_Flags (Etype (T), Element_Type, Comp => True);
6647 Set_Default_SSO (Etype (T));
6649 -- Ada 2005 (AI-231): Propagate the null-excluding attribute to the
6650 -- array type to ensure that objects of this type are initialized.
6652 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (Element_Type) then
6653 Set_Can_Never_Be_Null (T);
6655 if Null_Exclusion_Present (Component_Definition (Def))
6657 -- No need to check itypes because in their case this check was
6658 -- done at their point of creation
6660 and then not Is_Itype (Element_Type)
6661 then
6662 Error_Msg_N
6663 ("`NOT NULL` not allowed (null already excluded)",
6664 Subtype_Indication (Component_Definition (Def)));
6665 end if;
6666 end if;
6668 Priv := Private_Component (Element_Type);
6670 if Present (Priv) then
6672 -- Check for circular definitions
6674 if Priv = Any_Type then
6675 Set_Component_Type (Etype (T), Any_Type);
6677 -- There is a gap in the visibility of operations on the composite
6678 -- type only if the component type is defined in a different scope.
6680 elsif Scope (Priv) = Current_Scope then
6681 null;
6683 elsif Is_Limited_Type (Priv) then
6684 Set_Is_Limited_Composite (Etype (T));
6685 Set_Is_Limited_Composite (T);
6686 else
6687 Set_Is_Private_Composite (Etype (T));
6688 Set_Is_Private_Composite (T);
6689 end if;
6690 end if;
6692 -- A syntax error in the declaration itself may lead to an empty index
6693 -- list, in which case do a minimal patch.
6695 if No (First_Index (T)) then
6696 Error_Msg_N ("missing index definition in array type declaration", T);
6698 declare
6699 Indexes : constant List_Id :=
6700 New_List (New_Occurrence_Of (Any_Id, Sloc (T)));
6701 begin
6702 Set_Discrete_Subtype_Definitions (Def, Indexes);
6703 Set_First_Index (T, First (Indexes));
6704 return;
6705 end;
6706 end if;
6708 -- Create a concatenation operator for the new type. Internal array
6709 -- types created for packed entities do not need such, they are
6710 -- compatible with the user-defined type.
6712 if Number_Dimensions (T) = 1
6713 and then not Is_Packed_Array_Impl_Type (T)
6714 then
6715 New_Concatenation_Op (T);
6716 end if;
6718 -- In the case of an unconstrained array the parser has already verified
6719 -- that all the indexes are unconstrained but we still need to make sure
6720 -- that the element type is constrained.
6722 if Is_Mutably_Tagged_Type (Element_Type) then
6723 Set_Component_Type (T,
6724 Class_Wide_Equivalent_Type (Element_Type));
6726 elsif not Is_Definite_Subtype (Element_Type) then
6727 Error_Msg_N
6728 ("unconstrained element type in array declaration",
6729 Subtype_Indication (Component_Def));
6731 elsif Is_Abstract_Type (Element_Type) then
6732 Error_Msg_N
6733 ("the type of a component cannot be abstract",
6734 Subtype_Indication (Component_Def));
6735 end if;
6737 -- There may be an invariant declared for the component type, but
6738 -- the construction of the component invariant checking procedure
6739 -- takes place during expansion.
6740 end Array_Type_Declaration;
6742 ------------------------------------------------------
6743 -- Replace_Anonymous_Access_To_Protected_Subprogram --
6744 ------------------------------------------------------
6746 function Replace_Anonymous_Access_To_Protected_Subprogram
6747 (N : Node_Id) return Entity_Id
6749 Loc : constant Source_Ptr := Sloc (N);
6751 Curr_Scope : constant Scope_Stack_Entry :=
6752 Scope_Stack.Table (Scope_Stack.Last);
6754 Anon : constant Entity_Id := Make_Temporary (Loc, 'S');
6756 Acc : Node_Id;
6757 -- Access definition in declaration
6759 Comp : Node_Id;
6760 -- Object definition or formal definition with an access definition
6762 Decl : Node_Id;
6763 -- Declaration of anonymous access to subprogram type
6765 Spec : Node_Id;
6766 -- Original specification in access to subprogram
6768 P : Node_Id;
6770 begin
6771 Set_Is_Internal (Anon);
6773 case Nkind (N) is
6774 when N_Constrained_Array_Definition
6775 | N_Component_Declaration
6776 | N_Unconstrained_Array_Definition
6778 Comp := Component_Definition (N);
6779 Acc := Access_Definition (Comp);
6781 when N_Discriminant_Specification =>
6782 Comp := Discriminant_Type (N);
6783 Acc := Comp;
6785 when N_Parameter_Specification =>
6786 Comp := Parameter_Type (N);
6787 Acc := Comp;
6789 when N_Access_Function_Definition =>
6790 Comp := Result_Definition (N);
6791 Acc := Comp;
6793 when N_Object_Declaration =>
6794 Comp := Object_Definition (N);
6795 Acc := Comp;
6797 when N_Function_Specification =>
6798 Comp := Result_Definition (N);
6799 Acc := Comp;
6801 when others =>
6802 raise Program_Error;
6803 end case;
6805 Spec := Access_To_Subprogram_Definition (Acc);
6807 Decl :=
6808 Make_Full_Type_Declaration (Loc,
6809 Defining_Identifier => Anon,
6810 Type_Definition => Copy_Separate_Tree (Spec));
6812 Mark_Rewrite_Insertion (Decl);
6814 -- Insert the new declaration in the nearest enclosing scope. If the
6815 -- parent is a body and N is its return type, the declaration belongs
6816 -- in the enclosing scope. Likewise if N is the type of a parameter.
6818 P := Parent (N);
6820 if Nkind (N) = N_Function_Specification
6821 and then Nkind (P) = N_Subprogram_Body
6822 then
6823 P := Parent (P);
6824 elsif Nkind (N) = N_Parameter_Specification
6825 and then Nkind (P) in N_Subprogram_Specification
6826 and then Nkind (Parent (P)) = N_Subprogram_Body
6827 then
6828 P := Parent (Parent (P));
6829 end if;
6831 while Present (P) and then not Has_Declarations (P) loop
6832 P := Parent (P);
6833 end loop;
6835 pragma Assert (Present (P));
6837 if Nkind (P) = N_Package_Specification then
6838 Prepend (Decl, Visible_Declarations (P));
6839 else
6840 Prepend (Decl, Declarations (P));
6841 end if;
6843 -- Replace the anonymous type with an occurrence of the new declaration.
6844 -- In all cases the rewritten node does not have the null-exclusion
6845 -- attribute because (if present) it was already inherited by the
6846 -- anonymous entity (Anon). Thus, in case of components we do not
6847 -- inherit this attribute.
6849 if Nkind (N) = N_Parameter_Specification then
6850 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6851 Set_Etype (Defining_Identifier (N), Anon);
6852 Set_Null_Exclusion_Present (N, False);
6854 elsif Nkind (N) = N_Object_Declaration then
6855 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6856 Set_Etype (Defining_Identifier (N), Anon);
6858 elsif Nkind (N) = N_Access_Function_Definition then
6859 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6861 elsif Nkind (N) = N_Function_Specification then
6862 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6863 Set_Etype (Defining_Unit_Name (N), Anon);
6865 else
6866 Rewrite (Comp,
6867 Make_Component_Definition (Loc,
6868 Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
6869 end if;
6871 Mark_Rewrite_Insertion (Comp);
6873 if Nkind (N) in N_Object_Declaration | N_Access_Function_Definition
6874 or else (Nkind (Parent (N)) = N_Full_Type_Declaration
6875 and then not Is_Type (Current_Scope))
6876 then
6878 -- Declaration can be analyzed in the current scope.
6880 Analyze (Decl);
6882 else
6883 -- Temporarily remove the current scope (record or subprogram) from
6884 -- the stack to add the new declarations to the enclosing scope.
6885 -- The anonymous entity is an Itype with the proper attributes.
6887 Scope_Stack.Decrement_Last;
6888 Analyze (Decl);
6889 Set_Is_Itype (Anon);
6890 Set_Associated_Node_For_Itype (Anon, N);
6891 Scope_Stack.Append (Curr_Scope);
6892 end if;
6894 Mutate_Ekind (Anon, E_Anonymous_Access_Protected_Subprogram_Type);
6895 Set_Can_Use_Internal_Rep (Anon, not Always_Compatible_Rep_On_Target);
6896 return Anon;
6897 end Replace_Anonymous_Access_To_Protected_Subprogram;
6899 -------------------------------------
6900 -- Build_Access_Subprogram_Wrapper --
6901 -------------------------------------
6903 procedure Build_Access_Subprogram_Wrapper (Decl : Node_Id) is
6904 Loc : constant Source_Ptr := Sloc (Decl);
6905 Id : constant Entity_Id := Defining_Identifier (Decl);
6906 Type_Def : constant Node_Id := Type_Definition (Decl);
6907 Specs : constant List_Id :=
6908 Parameter_Specifications (Type_Def);
6909 Profile : constant List_Id := New_List;
6910 Subp : constant Entity_Id := Make_Temporary (Loc, 'A');
6912 Contracts : constant List_Id := New_List;
6913 Form_P : Node_Id;
6914 New_P : Node_Id;
6915 New_Decl : Node_Id;
6916 Spec : Node_Id;
6918 procedure Replace_Type_Name (Expr : Node_Id);
6919 -- In the expressions for contract aspects, replace occurrences of the
6920 -- access type with the name of the subprogram entity, as needed, e.g.
6921 -- for 'Result. Aspects that are not contracts, e.g. Size or Alignment)
6922 -- remain on the original access type declaration. What about expanded
6923 -- names denoting formals, whose prefix in source is the type name ???
6925 -----------------------
6926 -- Replace_Type_Name --
6927 -----------------------
6929 procedure Replace_Type_Name (Expr : Node_Id) is
6930 function Process (N : Node_Id) return Traverse_Result;
6931 function Process (N : Node_Id) return Traverse_Result is
6932 begin
6933 if Nkind (N) = N_Attribute_Reference
6934 and then Is_Entity_Name (Prefix (N))
6935 and then Chars (Prefix (N)) = Chars (Id)
6936 then
6937 Set_Prefix (N, Make_Identifier (Sloc (N), Chars (Subp)));
6938 end if;
6940 return OK;
6941 end Process;
6943 procedure Traverse is new Traverse_Proc (Process);
6944 begin
6945 Traverse (Expr);
6946 end Replace_Type_Name;
6948 begin
6949 if Ekind (Id) in E_Access_Subprogram_Type
6950 | E_Access_Protected_Subprogram_Type
6951 | E_Anonymous_Access_Protected_Subprogram_Type
6952 | E_Anonymous_Access_Subprogram_Type
6953 then
6954 null;
6956 else
6957 Error_Msg_N
6958 ("illegal pre/postcondition on access type", Decl);
6959 return;
6960 end if;
6962 declare
6963 Asp : Node_Id;
6964 A_Id : Aspect_Id;
6966 begin
6967 Asp := First (Aspect_Specifications (Decl));
6968 while Present (Asp) loop
6969 A_Id := Get_Aspect_Id (Chars (Identifier (Asp)));
6970 if A_Id = Aspect_Pre or else A_Id = Aspect_Post then
6971 Append (New_Copy_Tree (Asp), Contracts);
6972 Replace_Type_Name (Expression (Last (Contracts)));
6973 end if;
6974 Next (Asp);
6975 end loop;
6976 end;
6978 -- If there are no contract aspects, no need for a wrapper.
6980 if Is_Empty_List (Contracts) then
6981 return;
6982 end if;
6984 Form_P := First (Specs);
6986 while Present (Form_P) loop
6987 New_P := New_Copy_Tree (Form_P);
6988 Set_Defining_Identifier (New_P,
6989 Make_Defining_Identifier
6990 (Loc, Chars (Defining_Identifier (Form_P))));
6991 Append (New_P, Profile);
6992 Next (Form_P);
6993 end loop;
6995 -- Add to parameter specifications the access parameter that is passed
6996 -- in from an indirect call.
6998 Append (
6999 Make_Parameter_Specification (Loc,
7000 Defining_Identifier => Make_Temporary (Loc, 'P'),
7001 Parameter_Type => New_Occurrence_Of (Id, Loc)),
7002 Profile);
7004 if Nkind (Type_Def) = N_Access_Procedure_Definition then
7005 Spec :=
7006 Make_Procedure_Specification (Loc,
7007 Defining_Unit_Name => Subp,
7008 Parameter_Specifications => Profile);
7009 Mutate_Ekind (Subp, E_Procedure);
7010 else
7011 Spec :=
7012 Make_Function_Specification (Loc,
7013 Defining_Unit_Name => Subp,
7014 Parameter_Specifications => Profile,
7015 Result_Definition =>
7016 New_Copy_Tree
7017 (Result_Definition (Type_Definition (Decl))));
7018 Mutate_Ekind (Subp, E_Function);
7019 end if;
7021 New_Decl :=
7022 Make_Subprogram_Declaration (Loc, Specification => Spec);
7023 Set_Aspect_Specifications (New_Decl, Contracts);
7024 Set_Is_Wrapper (Subp);
7026 -- The wrapper is declared in the freezing actions to facilitate its
7027 -- identification and thus avoid handling it as a primitive operation
7028 -- of a tagged type (see Is_Access_To_Subprogram_Wrapper); otherwise it
7029 -- may be handled as a dispatching operation and erroneously registered
7030 -- in a dispatch table.
7032 Append_Freeze_Action (Id, New_Decl);
7034 Set_Access_Subprogram_Wrapper (Designated_Type (Id), Subp);
7035 Build_Access_Subprogram_Wrapper_Body (Decl, New_Decl);
7036 end Build_Access_Subprogram_Wrapper;
7038 -------------------------------
7039 -- Build_Derived_Access_Type --
7040 -------------------------------
7042 procedure Build_Derived_Access_Type
7043 (N : Node_Id;
7044 Parent_Type : Entity_Id;
7045 Derived_Type : Entity_Id)
7047 S : constant Node_Id := Subtype_Indication (Type_Definition (N));
7049 Desig_Type : Entity_Id;
7050 Discr : Entity_Id;
7051 Discr_Con_Elist : Elist_Id;
7052 Discr_Con_El : Elmt_Id;
7053 Subt : Entity_Id;
7055 begin
7056 -- Set the designated type so it is available in case this is an access
7057 -- to a self-referential type, e.g. a standard list type with a next
7058 -- pointer. Will be reset after subtype is built.
7060 Set_Directly_Designated_Type
7061 (Derived_Type, Designated_Type (Parent_Type));
7063 Subt := Process_Subtype (S, N);
7065 if Nkind (S) /= N_Subtype_Indication
7066 and then Subt /= Base_Type (Subt)
7067 then
7068 Mutate_Ekind (Derived_Type, E_Access_Subtype);
7069 end if;
7071 if Ekind (Derived_Type) = E_Access_Subtype then
7072 declare
7073 Pbase : constant Entity_Id := Base_Type (Parent_Type);
7074 Ibase : constant Entity_Id :=
7075 Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
7076 Svg_Chars : constant Name_Id := Chars (Ibase);
7077 Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
7078 Svg_Prev_E : constant Entity_Id := Prev_Entity (Ibase);
7080 begin
7081 Copy_Node (Pbase, Ibase);
7083 -- Restore Itype status after Copy_Node
7085 Set_Is_Itype (Ibase);
7086 Set_Associated_Node_For_Itype (Ibase, N);
7088 Set_Chars (Ibase, Svg_Chars);
7089 Set_Prev_Entity (Ibase, Svg_Prev_E);
7090 Set_Next_Entity (Ibase, Svg_Next_E);
7091 Set_Sloc (Ibase, Sloc (Derived_Type));
7092 Set_Scope (Ibase, Scope (Derived_Type));
7093 Set_Freeze_Node (Ibase, Empty);
7094 Set_Is_Frozen (Ibase, False);
7095 Set_Comes_From_Source (Ibase, False);
7096 Set_Is_First_Subtype (Ibase, False);
7098 Set_Etype (Ibase, Pbase);
7099 Set_Etype (Derived_Type, Ibase);
7100 end;
7101 end if;
7103 Set_Directly_Designated_Type
7104 (Derived_Type, Designated_Type (Subt));
7106 Set_Is_Constrained (Derived_Type, Is_Constrained (Subt));
7107 Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
7108 Set_Size_Info (Derived_Type, Parent_Type);
7109 Copy_RM_Size (To => Derived_Type, From => Parent_Type);
7110 Set_Depends_On_Private (Derived_Type,
7111 Has_Private_Component (Derived_Type));
7112 Conditional_Delay (Derived_Type, Subt);
7114 if Is_Access_Subprogram_Type (Derived_Type)
7115 and then Is_Base_Type (Derived_Type)
7116 then
7117 Set_Can_Use_Internal_Rep
7118 (Derived_Type, Can_Use_Internal_Rep (Parent_Type));
7119 end if;
7121 -- Ada 2005 (AI-231): Set the null-exclusion attribute, and verify
7122 -- that it is not redundant.
7124 if Null_Exclusion_Present (Type_Definition (N)) then
7125 Set_Can_Never_Be_Null (Derived_Type);
7127 elsif Can_Never_Be_Null (Parent_Type) then
7128 Set_Can_Never_Be_Null (Derived_Type);
7129 end if;
7131 -- Note: we do not copy the Storage_Size_Variable, since we always go to
7132 -- the root type for this information.
7134 -- Apply range checks to discriminants for derived record case
7135 -- ??? THIS CODE SHOULD NOT BE HERE REALLY.
7137 Desig_Type := Designated_Type (Derived_Type);
7139 if Is_Composite_Type (Desig_Type)
7140 and then not Is_Array_Type (Desig_Type)
7141 and then Has_Discriminants (Desig_Type)
7142 and then Base_Type (Desig_Type) /= Desig_Type
7143 then
7144 Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
7145 Discr_Con_El := First_Elmt (Discr_Con_Elist);
7147 Discr := First_Discriminant (Base_Type (Desig_Type));
7148 while Present (Discr_Con_El) loop
7149 Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
7150 Next_Elmt (Discr_Con_El);
7151 Next_Discriminant (Discr);
7152 end loop;
7153 end if;
7154 end Build_Derived_Access_Type;
7156 ------------------------------
7157 -- Build_Derived_Array_Type --
7158 ------------------------------
7160 procedure Build_Derived_Array_Type
7161 (N : Node_Id;
7162 Parent_Type : Entity_Id;
7163 Derived_Type : Entity_Id)
7165 Loc : constant Source_Ptr := Sloc (N);
7166 Tdef : constant Node_Id := Type_Definition (N);
7167 Indic : constant Node_Id := Subtype_Indication (Tdef);
7168 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
7169 Implicit_Base : Entity_Id := Empty;
7170 New_Indic : Node_Id;
7172 procedure Make_Implicit_Base;
7173 -- If the parent subtype is constrained, the derived type is a subtype
7174 -- of an implicit base type derived from the parent base.
7176 ------------------------
7177 -- Make_Implicit_Base --
7178 ------------------------
7180 procedure Make_Implicit_Base is
7181 begin
7182 Implicit_Base :=
7183 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
7185 Mutate_Ekind (Implicit_Base, Ekind (Parent_Base));
7186 Set_Etype (Implicit_Base, Parent_Base);
7188 Copy_Array_Subtype_Attributes (Implicit_Base, Parent_Base);
7189 Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
7191 Set_Has_Delayed_Freeze (Implicit_Base, True);
7192 end Make_Implicit_Base;
7194 -- Start of processing for Build_Derived_Array_Type
7196 begin
7197 if not Is_Constrained (Parent_Type) then
7198 if Nkind (Indic) /= N_Subtype_Indication then
7199 Mutate_Ekind (Derived_Type, E_Array_Type);
7201 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
7202 Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
7204 Set_Has_Delayed_Freeze (Derived_Type, True);
7206 else
7207 Make_Implicit_Base;
7208 Set_Etype (Derived_Type, Implicit_Base);
7210 New_Indic :=
7211 Make_Subtype_Declaration (Loc,
7212 Defining_Identifier => Derived_Type,
7213 Subtype_Indication =>
7214 Make_Subtype_Indication (Loc,
7215 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
7216 Constraint => Constraint (Indic)));
7218 Rewrite (N, New_Indic);
7220 -- Keep the aspects from the original node
7222 Move_Aspects (Original_Node (N), N);
7224 Analyze (N);
7225 end if;
7227 else
7228 if Nkind (Indic) /= N_Subtype_Indication then
7229 Make_Implicit_Base;
7231 Mutate_Ekind (Derived_Type, Ekind (Parent_Type));
7232 Set_Etype (Derived_Type, Implicit_Base);
7233 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
7235 else
7236 Error_Msg_N ("illegal constraint on constrained type", Indic);
7237 end if;
7238 end if;
7240 -- If parent type is not a derived type itself, and is declared in
7241 -- closed scope (e.g. a subprogram), then we must explicitly introduce
7242 -- the new type's concatenation operator since Derive_Subprograms
7243 -- will not inherit the parent's operator. If the parent type is
7244 -- unconstrained, the operator is of the unconstrained base type.
7246 if Number_Dimensions (Parent_Type) = 1
7247 and then not Is_Limited_Type (Parent_Type)
7248 and then not Is_Derived_Type (Parent_Type)
7249 and then not Is_Package_Or_Generic_Package
7250 (Scope (Base_Type (Parent_Type)))
7251 then
7252 if not Is_Constrained (Parent_Type)
7253 and then Is_Constrained (Derived_Type)
7254 then
7255 New_Concatenation_Op (Implicit_Base);
7256 else
7257 New_Concatenation_Op (Derived_Type);
7258 end if;
7259 end if;
7260 end Build_Derived_Array_Type;
7262 -----------------------------------
7263 -- Build_Derived_Concurrent_Type --
7264 -----------------------------------
7266 procedure Build_Derived_Concurrent_Type
7267 (N : Node_Id;
7268 Parent_Type : Entity_Id;
7269 Derived_Type : Entity_Id)
7271 Loc : constant Source_Ptr := Sloc (N);
7272 Def : constant Node_Id := Type_Definition (N);
7273 Indic : constant Node_Id := Subtype_Indication (Def);
7275 Corr_Record : constant Entity_Id := Make_Temporary (Loc, 'C');
7276 Corr_Decl : Node_Id := Empty;
7277 Corr_Decl_Needed : Boolean;
7278 -- If the derived type has fewer discriminants than its parent, the
7279 -- corresponding record is also a derived type, in order to account for
7280 -- the bound discriminants. We create a full type declaration for it in
7281 -- this case.
7283 Constraint_Present : constant Boolean :=
7284 Nkind (Indic) = N_Subtype_Indication;
7286 D_Constraint : Node_Id;
7287 New_Constraint : Elist_Id := No_Elist;
7288 Old_Disc : Entity_Id;
7289 New_Disc : Entity_Id;
7290 New_N : Node_Id;
7292 begin
7293 Set_Stored_Constraint (Derived_Type, No_Elist);
7294 Corr_Decl_Needed := False;
7295 Old_Disc := Empty;
7297 if Present (Discriminant_Specifications (N))
7298 and then Constraint_Present
7299 then
7300 Old_Disc := First_Discriminant (Parent_Type);
7301 New_Disc := First (Discriminant_Specifications (N));
7302 while Present (New_Disc) and then Present (Old_Disc) loop
7303 Next_Discriminant (Old_Disc);
7304 Next (New_Disc);
7305 end loop;
7306 end if;
7308 if Present (Old_Disc) and then Expander_Active then
7310 -- The new type has fewer discriminants, so we need to create a new
7311 -- corresponding record, which is derived from the corresponding
7312 -- record of the parent, and has a stored constraint that captures
7313 -- the values of the discriminant constraints. The corresponding
7314 -- record is needed only if expander is active and code generation is
7315 -- enabled.
7317 -- The type declaration for the derived corresponding record has the
7318 -- same discriminant part and constraints as the current declaration.
7319 -- Copy the unanalyzed tree to build declaration.
7321 Corr_Decl_Needed := True;
7322 New_N := Copy_Separate_Tree (N);
7324 Corr_Decl :=
7325 Make_Full_Type_Declaration (Loc,
7326 Defining_Identifier => Corr_Record,
7327 Discriminant_Specifications =>
7328 Discriminant_Specifications (New_N),
7329 Type_Definition =>
7330 Make_Derived_Type_Definition (Loc,
7331 Subtype_Indication =>
7332 Make_Subtype_Indication (Loc,
7333 Subtype_Mark =>
7334 New_Occurrence_Of
7335 (Corresponding_Record_Type (Parent_Type), Loc),
7336 Constraint =>
7337 Constraint
7338 (Subtype_Indication (Type_Definition (New_N))))));
7339 end if;
7341 -- Copy Storage_Size and Relative_Deadline variables if task case
7343 if Is_Task_Type (Parent_Type) then
7344 Set_Storage_Size_Variable (Derived_Type,
7345 Storage_Size_Variable (Parent_Type));
7346 Set_Relative_Deadline_Variable (Derived_Type,
7347 Relative_Deadline_Variable (Parent_Type));
7348 end if;
7350 if Present (Discriminant_Specifications (N)) then
7351 Push_Scope (Derived_Type);
7352 Check_Or_Process_Discriminants (N, Derived_Type);
7354 if Constraint_Present then
7355 New_Constraint :=
7356 Expand_To_Stored_Constraint
7357 (Parent_Type,
7358 Build_Discriminant_Constraints
7359 (Parent_Type, Indic, True));
7360 end if;
7362 End_Scope;
7364 elsif Constraint_Present then
7366 -- Build an unconstrained derived type and rewrite the derived type
7367 -- as a subtype of this new base type.
7369 declare
7370 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
7371 New_Base : Entity_Id;
7372 New_Decl : Node_Id;
7373 New_Indic : Node_Id;
7375 begin
7376 New_Base :=
7377 Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
7379 New_Decl :=
7380 Make_Full_Type_Declaration (Loc,
7381 Defining_Identifier => New_Base,
7382 Type_Definition =>
7383 Make_Derived_Type_Definition (Loc,
7384 Abstract_Present => Abstract_Present (Def),
7385 Limited_Present => Limited_Present (Def),
7386 Subtype_Indication =>
7387 New_Occurrence_Of (Parent_Base, Loc)));
7389 Mark_Rewrite_Insertion (New_Decl);
7390 Insert_Before (N, New_Decl);
7391 Analyze (New_Decl);
7393 New_Indic :=
7394 Make_Subtype_Indication (Loc,
7395 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
7396 Constraint => Relocate_Node (Constraint (Indic)));
7398 Rewrite (N,
7399 Make_Subtype_Declaration (Loc,
7400 Defining_Identifier => Derived_Type,
7401 Subtype_Indication => New_Indic));
7403 -- Keep the aspects from the original node
7405 Move_Aspects (Original_Node (N), N);
7407 Analyze (N);
7408 return;
7409 end;
7410 end if;
7412 -- By default, operations and private data are inherited from parent.
7413 -- However, in the presence of bound discriminants, a new corresponding
7414 -- record will be created, see below.
7416 Set_Has_Discriminants
7417 (Derived_Type, Has_Discriminants (Parent_Type));
7418 Set_Corresponding_Record_Type
7419 (Derived_Type, Corresponding_Record_Type (Parent_Type));
7421 -- Is_Constrained is set according the parent subtype, but is set to
7422 -- False if the derived type is declared with new discriminants.
7424 Set_Is_Constrained
7425 (Derived_Type,
7426 (Is_Constrained (Parent_Type) or else Constraint_Present)
7427 and then No (Discriminant_Specifications (N)));
7429 if Constraint_Present then
7430 if not Has_Discriminants (Parent_Type) then
7431 Error_Msg_N ("untagged parent must have discriminants", N);
7433 elsif Present (Discriminant_Specifications (N)) then
7435 -- Verify that new discriminants are used to constrain old ones
7437 D_Constraint := First (Constraints (Constraint (Indic)));
7439 Old_Disc := First_Discriminant (Parent_Type);
7441 while Present (D_Constraint) loop
7442 if Nkind (D_Constraint) /= N_Discriminant_Association then
7444 -- Positional constraint. If it is a reference to a new
7445 -- discriminant, it constrains the corresponding old one.
7447 if Nkind (D_Constraint) = N_Identifier then
7448 New_Disc := First_Discriminant (Derived_Type);
7449 while Present (New_Disc) loop
7450 exit when Chars (New_Disc) = Chars (D_Constraint);
7451 Next_Discriminant (New_Disc);
7452 end loop;
7454 if Present (New_Disc) then
7455 Set_Corresponding_Discriminant (New_Disc, Old_Disc);
7456 end if;
7457 end if;
7459 Next_Discriminant (Old_Disc);
7461 -- if this is a named constraint, search by name for the old
7462 -- discriminants constrained by the new one.
7464 elsif Nkind (Expression (D_Constraint)) = N_Identifier then
7466 -- Find new discriminant with that name
7468 New_Disc := First_Discriminant (Derived_Type);
7469 while Present (New_Disc) loop
7470 exit when
7471 Chars (New_Disc) = Chars (Expression (D_Constraint));
7472 Next_Discriminant (New_Disc);
7473 end loop;
7475 if Present (New_Disc) then
7477 -- Verify that new discriminant renames some discriminant
7478 -- of the parent type, and associate the new discriminant
7479 -- with one or more old ones that it renames.
7481 declare
7482 Selector : Node_Id;
7484 begin
7485 Selector := First (Selector_Names (D_Constraint));
7486 while Present (Selector) loop
7487 Old_Disc := First_Discriminant (Parent_Type);
7488 while Present (Old_Disc) loop
7489 exit when Chars (Old_Disc) = Chars (Selector);
7490 Next_Discriminant (Old_Disc);
7491 end loop;
7493 if Present (Old_Disc) then
7494 Set_Corresponding_Discriminant
7495 (New_Disc, Old_Disc);
7496 end if;
7498 Next (Selector);
7499 end loop;
7500 end;
7501 end if;
7502 end if;
7504 Next (D_Constraint);
7505 end loop;
7507 New_Disc := First_Discriminant (Derived_Type);
7508 while Present (New_Disc) loop
7509 if No (Corresponding_Discriminant (New_Disc)) then
7510 Error_Msg_NE
7511 ("new discriminant& must constrain old one", N, New_Disc);
7513 -- If a new discriminant is used in the constraint, then its
7514 -- subtype must be statically compatible with the subtype of
7515 -- the parent discriminant (RM 3.7(15)).
7517 else
7518 Check_Constraining_Discriminant
7519 (New_Disc, Corresponding_Discriminant (New_Disc));
7520 end if;
7522 Next_Discriminant (New_Disc);
7523 end loop;
7524 end if;
7526 elsif Present (Discriminant_Specifications (N)) then
7527 Error_Msg_N
7528 ("missing discriminant constraint in untagged derivation", N);
7529 end if;
7531 -- The entity chain of the derived type includes the new discriminants
7532 -- but shares operations with the parent.
7534 if Present (Discriminant_Specifications (N)) then
7535 Old_Disc := First_Discriminant (Parent_Type);
7536 while Present (Old_Disc) loop
7537 if No (Next_Entity (Old_Disc))
7538 or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
7539 then
7540 Link_Entities
7541 (Last_Entity (Derived_Type), Next_Entity (Old_Disc));
7542 exit;
7543 end if;
7545 Next_Discriminant (Old_Disc);
7546 end loop;
7548 else
7549 Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
7550 if Has_Discriminants (Parent_Type) then
7551 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7552 Set_Discriminant_Constraint (
7553 Derived_Type, Discriminant_Constraint (Parent_Type));
7554 end if;
7555 end if;
7557 Set_Last_Entity (Derived_Type, Last_Entity (Parent_Type));
7559 Set_Has_Completion (Derived_Type);
7561 if Corr_Decl_Needed then
7562 Set_Stored_Constraint (Derived_Type, New_Constraint);
7563 Insert_After (N, Corr_Decl);
7564 Analyze (Corr_Decl);
7565 Set_Corresponding_Record_Type (Derived_Type, Corr_Record);
7566 end if;
7567 end Build_Derived_Concurrent_Type;
7569 ------------------------------------
7570 -- Build_Derived_Enumeration_Type --
7571 ------------------------------------
7573 procedure Build_Derived_Enumeration_Type
7574 (N : Node_Id;
7575 Parent_Type : Entity_Id;
7576 Derived_Type : Entity_Id)
7578 function Bound_Belongs_To_Type (B : Node_Id) return Boolean;
7579 -- When the type declaration includes a constraint, we generate
7580 -- a subtype declaration of an anonymous base type, with the constraint
7581 -- given in the original type declaration. Conceptually, the bounds
7582 -- are converted to the new base type, and this conversion freezes
7583 -- (prematurely) that base type, when the bounds are simply literals.
7584 -- As a result, a representation clause for the derived type is then
7585 -- rejected or ignored. This procedure recognizes the simple case of
7586 -- literal bounds, which allows us to indicate that the conversions
7587 -- are not freeze points, and the subsequent representation clause
7588 -- can be accepted.
7589 -- A similar approach might be used to resolve the long-standing
7590 -- problem of premature freezing of derived numeric types ???
7592 function Bound_Belongs_To_Type (B : Node_Id) return Boolean is
7593 begin
7594 return Nkind (B) = N_Type_Conversion
7595 and then Is_Entity_Name (Expression (B))
7596 and then Ekind (Entity (Expression (B))) = E_Enumeration_Literal;
7597 end Bound_Belongs_To_Type;
7599 Loc : constant Source_Ptr := Sloc (N);
7600 Def : constant Node_Id := Type_Definition (N);
7601 Indic : constant Node_Id := Subtype_Indication (Def);
7602 Implicit_Base : Entity_Id;
7603 Literal : Entity_Id;
7604 New_Lit : Entity_Id;
7605 Literals_List : List_Id;
7606 Type_Decl : Node_Id;
7607 Hi, Lo : Node_Id;
7608 Rang_Expr : Node_Id;
7610 begin
7611 -- Since types Standard.Character and Standard.[Wide_]Wide_Character do
7612 -- not have explicit literals lists we need to process types derived
7613 -- from them specially. This is handled by Derived_Standard_Character.
7614 -- If the parent type is a generic type, there are no literals either,
7615 -- and we construct the same skeletal representation as for the generic
7616 -- parent type.
7618 if Is_Standard_Character_Type (Parent_Type) then
7619 Derived_Standard_Character (N, Parent_Type, Derived_Type);
7621 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
7622 declare
7623 Lo : Node_Id;
7624 Hi : Node_Id;
7626 begin
7627 if Nkind (Indic) /= N_Subtype_Indication then
7628 Lo :=
7629 Make_Attribute_Reference (Loc,
7630 Attribute_Name => Name_First,
7631 Prefix => New_Occurrence_Of (Derived_Type, Loc));
7632 Set_Etype (Lo, Derived_Type);
7634 Hi :=
7635 Make_Attribute_Reference (Loc,
7636 Attribute_Name => Name_Last,
7637 Prefix => New_Occurrence_Of (Derived_Type, Loc));
7638 Set_Etype (Hi, Derived_Type);
7640 Set_Scalar_Range (Derived_Type,
7641 Make_Range (Loc,
7642 Low_Bound => Lo,
7643 High_Bound => Hi));
7644 else
7646 -- Analyze subtype indication and verify compatibility
7647 -- with parent type.
7649 if Base_Type (Process_Subtype (Indic, N)) /=
7650 Base_Type (Parent_Type)
7651 then
7652 Error_Msg_N
7653 ("illegal constraint for formal discrete type", N);
7654 end if;
7655 end if;
7656 end;
7658 else
7659 -- If a constraint is present, analyze the bounds to catch
7660 -- premature usage of the derived literals.
7662 if Nkind (Indic) = N_Subtype_Indication
7663 and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
7664 then
7665 Analyze (Low_Bound (Range_Expression (Constraint (Indic))));
7666 Analyze (High_Bound (Range_Expression (Constraint (Indic))));
7667 end if;
7669 -- Create an implicit base type for the derived type even if there
7670 -- is no constraint attached to it, since this seems closer to the
7671 -- Ada semantics. Use an Itype like for the implicit base type of
7672 -- other kinds of derived type, but build a full type declaration
7673 -- for it so as to analyze the new literals properly. Then build a
7674 -- subtype declaration tree which applies the constraint (if any)
7675 -- and have it replace the derived type declaration.
7677 Literal := First_Literal (Parent_Type);
7678 Literals_List := New_List;
7679 while Present (Literal)
7680 and then Ekind (Literal) = E_Enumeration_Literal
7681 loop
7682 -- Literals of the derived type have the same representation as
7683 -- those of the parent type, but this representation can be
7684 -- overridden by an explicit representation clause. Indicate
7685 -- that there is no explicit representation given yet. These
7686 -- derived literals are implicit operations of the new type,
7687 -- and can be overridden by explicit ones.
7689 if Nkind (Literal) = N_Defining_Character_Literal then
7690 New_Lit :=
7691 Make_Defining_Character_Literal (Loc, Chars (Literal));
7692 else
7693 New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
7694 end if;
7696 Mutate_Ekind (New_Lit, E_Enumeration_Literal);
7697 Set_Is_Not_Self_Hidden (New_Lit);
7698 Set_Enumeration_Pos (New_Lit, Enumeration_Pos (Literal));
7699 Set_Enumeration_Rep (New_Lit, Enumeration_Rep (Literal));
7700 Set_Enumeration_Rep_Expr (New_Lit, Empty);
7701 Set_Alias (New_Lit, Literal);
7702 Set_Is_Known_Valid (New_Lit, True);
7704 Append (New_Lit, Literals_List);
7705 Next_Literal (Literal);
7706 end loop;
7708 Implicit_Base :=
7709 Create_Itype (E_Enumeration_Type, N, Derived_Type, 'B');
7711 -- Indicate the proper nature of the derived type. This must be done
7712 -- before analysis of the literals, to recognize cases when a literal
7713 -- may be hidden by a previous explicit function definition (cf.
7714 -- c83031a).
7716 Mutate_Ekind (Derived_Type, E_Enumeration_Subtype);
7717 Set_Etype (Derived_Type, Implicit_Base);
7719 Type_Decl :=
7720 Make_Full_Type_Declaration (Loc,
7721 Defining_Identifier => Implicit_Base,
7722 Type_Definition =>
7723 Make_Enumeration_Type_Definition (Loc, Literals_List));
7725 -- Do not insert the declarationn, just analyze it in the context
7727 Set_Parent (Type_Decl, Parent (N));
7728 Analyze (Type_Decl);
7730 -- The anonymous base now has a full declaration, but this base
7731 -- is not a first subtype.
7733 Set_Is_First_Subtype (Implicit_Base, False);
7735 -- After the implicit base is analyzed its Etype needs to be changed
7736 -- to reflect the fact that it is derived from the parent type which
7737 -- was ignored during analysis. We also set the size at this point.
7739 Set_Etype (Implicit_Base, Parent_Type);
7741 Set_Size_Info (Implicit_Base, Parent_Type);
7742 Set_RM_Size (Implicit_Base, RM_Size (Parent_Type));
7743 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
7745 -- Copy other flags from parent type
7747 Set_Has_Non_Standard_Rep
7748 (Implicit_Base, Has_Non_Standard_Rep
7749 (Parent_Type));
7750 Set_Has_Pragma_Ordered
7751 (Implicit_Base, Has_Pragma_Ordered
7752 (Parent_Type));
7753 Set_Has_Delayed_Freeze (Implicit_Base);
7755 -- Process the subtype indication including a validation check on the
7756 -- constraint, if any. If a constraint is given, its bounds must be
7757 -- implicitly converted to the new type.
7759 if Nkind (Indic) = N_Subtype_Indication then
7760 declare
7761 R : constant Node_Id :=
7762 Range_Expression (Constraint (Indic));
7764 begin
7765 if Nkind (R) = N_Range then
7766 Hi := Build_Scalar_Bound
7767 (High_Bound (R), Parent_Type, Implicit_Base);
7768 Lo := Build_Scalar_Bound
7769 (Low_Bound (R), Parent_Type, Implicit_Base);
7771 else
7772 -- Constraint is a Range attribute. Replace with explicit
7773 -- mention of the bounds of the prefix, which must be a
7774 -- subtype.
7776 Analyze (Prefix (R));
7777 Hi :=
7778 Convert_To (Implicit_Base,
7779 Make_Attribute_Reference (Loc,
7780 Attribute_Name => Name_Last,
7781 Prefix =>
7782 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
7784 Lo :=
7785 Convert_To (Implicit_Base,
7786 Make_Attribute_Reference (Loc,
7787 Attribute_Name => Name_First,
7788 Prefix =>
7789 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
7790 end if;
7791 end;
7793 else
7794 Hi :=
7795 Build_Scalar_Bound
7796 (Type_High_Bound (Parent_Type),
7797 Parent_Type, Implicit_Base);
7798 Lo :=
7799 Build_Scalar_Bound
7800 (Type_Low_Bound (Parent_Type),
7801 Parent_Type, Implicit_Base);
7802 end if;
7804 Rang_Expr :=
7805 Make_Range (Loc,
7806 Low_Bound => Lo,
7807 High_Bound => Hi);
7809 -- If we constructed a default range for the case where no range
7810 -- was given, then the expressions in the range must not freeze
7811 -- since they do not correspond to expressions in the source.
7812 -- However, if the type inherits predicates the expressions will
7813 -- be elaborated earlier and must freeze.
7815 if (Nkind (Indic) /= N_Subtype_Indication
7816 or else
7817 (Bound_Belongs_To_Type (Lo) and then Bound_Belongs_To_Type (Hi)))
7818 and then not Has_Predicates (Derived_Type)
7819 then
7820 Set_Must_Not_Freeze (Lo);
7821 Set_Must_Not_Freeze (Hi);
7822 Set_Must_Not_Freeze (Rang_Expr);
7823 end if;
7825 Rewrite (N,
7826 Make_Subtype_Declaration (Loc,
7827 Defining_Identifier => Derived_Type,
7828 Subtype_Indication =>
7829 Make_Subtype_Indication (Loc,
7830 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
7831 Constraint =>
7832 Make_Range_Constraint (Loc,
7833 Range_Expression => Rang_Expr))));
7835 -- Keep the aspects from the orignal node
7837 Move_Aspects (Original_Node (N), N);
7839 Analyze (N);
7841 -- Propagate the aspects from the original type declaration to the
7842 -- declaration of the implicit base.
7844 Copy_Aspects (From => N, To => Type_Decl);
7846 -- Apply a range check. Since this range expression doesn't have an
7847 -- Etype, we have to specifically pass the Source_Typ parameter. Is
7848 -- this right???
7850 if Nkind (Indic) = N_Subtype_Indication then
7851 Apply_Range_Check
7852 (Range_Expression (Constraint (Indic)), Parent_Type,
7853 Source_Typ => Entity (Subtype_Mark (Indic)));
7854 end if;
7855 end if;
7856 end Build_Derived_Enumeration_Type;
7858 --------------------------------
7859 -- Build_Derived_Numeric_Type --
7860 --------------------------------
7862 procedure Build_Derived_Numeric_Type
7863 (N : Node_Id;
7864 Parent_Type : Entity_Id;
7865 Derived_Type : Entity_Id)
7867 Loc : constant Source_Ptr := Sloc (N);
7868 Tdef : constant Node_Id := Type_Definition (N);
7869 Indic : constant Node_Id := Subtype_Indication (Tdef);
7870 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
7871 No_Constraint : constant Boolean := Nkind (Indic) /=
7872 N_Subtype_Indication;
7873 Implicit_Base : Entity_Id;
7875 Lo : Node_Id;
7876 Hi : Node_Id;
7878 begin
7879 -- Process the subtype indication including a validation check on
7880 -- the constraint if any.
7882 Discard_Node (Process_Subtype (Indic, N));
7884 -- Introduce an implicit base type for the derived type even if there
7885 -- is no constraint attached to it, since this seems closer to the Ada
7886 -- semantics.
7888 Implicit_Base :=
7889 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
7891 Set_Etype (Implicit_Base, Parent_Base);
7892 Mutate_Ekind (Implicit_Base, Ekind (Parent_Base));
7893 Set_Size_Info (Implicit_Base, Parent_Base);
7894 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
7895 Set_Parent (Implicit_Base, Parent (Derived_Type));
7896 Set_Is_Known_Valid (Implicit_Base, Is_Known_Valid (Parent_Base));
7897 Set_Is_Volatile (Implicit_Base, Is_Volatile (Parent_Base));
7899 -- Set RM Size for discrete type or decimal fixed-point type
7900 -- Ordinary fixed-point is excluded, why???
7902 if Is_Discrete_Type (Parent_Base)
7903 or else Is_Decimal_Fixed_Point_Type (Parent_Base)
7904 then
7905 Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
7906 end if;
7908 Set_Has_Delayed_Freeze (Implicit_Base);
7910 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
7911 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
7913 Set_Scalar_Range (Implicit_Base,
7914 Make_Range (Loc,
7915 Low_Bound => Lo,
7916 High_Bound => Hi));
7918 if Has_Infinities (Parent_Base) then
7919 Set_Includes_Infinities (Scalar_Range (Implicit_Base));
7920 end if;
7922 -- The Derived_Type, which is the entity of the declaration, is a
7923 -- subtype of the implicit base. Its Ekind is a subtype, even in the
7924 -- absence of an explicit constraint.
7926 Set_Etype (Derived_Type, Implicit_Base);
7928 -- If we did not have a constraint, then the Ekind is set from the
7929 -- parent type (otherwise Process_Subtype has set the bounds)
7931 if No_Constraint then
7932 Mutate_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
7933 end if;
7935 -- If we did not have a range constraint, then set the range from the
7936 -- parent type. Otherwise, the Process_Subtype call has set the bounds.
7938 if No_Constraint or else not Has_Range_Constraint (Indic) then
7939 Set_Scalar_Range (Derived_Type,
7940 Make_Range (Loc,
7941 Low_Bound => New_Copy_Tree (Type_Low_Bound (Parent_Type)),
7942 High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
7943 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7945 if Has_Infinities (Parent_Type) then
7946 Set_Includes_Infinities (Scalar_Range (Derived_Type));
7947 end if;
7949 Set_Is_Known_Valid (Derived_Type, Is_Known_Valid (Parent_Type));
7950 end if;
7952 Set_Is_Descendant_Of_Address (Derived_Type,
7953 Is_Descendant_Of_Address (Parent_Type));
7954 Set_Is_Descendant_Of_Address (Implicit_Base,
7955 Is_Descendant_Of_Address (Parent_Type));
7957 -- Set remaining type-specific fields, depending on numeric type
7959 if Is_Modular_Integer_Type (Parent_Type) then
7960 Set_Modulus (Implicit_Base, Modulus (Parent_Base));
7962 Set_Non_Binary_Modulus
7963 (Implicit_Base, Non_Binary_Modulus (Parent_Base));
7965 Set_Is_Known_Valid
7966 (Implicit_Base, Is_Known_Valid (Parent_Base));
7968 elsif Is_Floating_Point_Type (Parent_Type) then
7970 -- Digits of base type is always copied from the digits value of
7971 -- the parent base type, but the digits of the derived type will
7972 -- already have been set if there was a constraint present.
7974 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
7975 Set_Float_Rep (Implicit_Base, Float_Rep (Parent_Base));
7977 if No_Constraint then
7978 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
7979 end if;
7981 elsif Is_Fixed_Point_Type (Parent_Type) then
7983 -- Small of base type and derived type are always copied from the
7984 -- parent base type, since smalls never change. The delta of the
7985 -- base type is also copied from the parent base type. However the
7986 -- delta of the derived type will have been set already if a
7987 -- constraint was present.
7989 Set_Small_Value (Derived_Type, Small_Value (Parent_Base));
7990 Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
7991 Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
7993 if No_Constraint then
7994 Set_Delta_Value (Derived_Type, Delta_Value (Parent_Type));
7995 end if;
7997 -- The scale and machine radix in the decimal case are always
7998 -- copied from the parent base type.
8000 if Is_Decimal_Fixed_Point_Type (Parent_Type) then
8001 Set_Scale_Value (Derived_Type, Scale_Value (Parent_Base));
8002 Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
8004 Set_Machine_Radix_10
8005 (Derived_Type, Machine_Radix_10 (Parent_Base));
8006 Set_Machine_Radix_10
8007 (Implicit_Base, Machine_Radix_10 (Parent_Base));
8009 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
8011 if No_Constraint then
8012 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
8014 else
8015 -- the analysis of the subtype_indication sets the
8016 -- digits value of the derived type.
8018 null;
8019 end if;
8020 end if;
8021 end if;
8023 if Is_Integer_Type (Parent_Type) then
8024 Set_Has_Shift_Operator
8025 (Implicit_Base, Has_Shift_Operator (Parent_Type));
8026 end if;
8028 -- The type of the bounds is that of the parent type, and they
8029 -- must be converted to the derived type.
8031 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
8032 end Build_Derived_Numeric_Type;
8034 --------------------------------
8035 -- Build_Derived_Private_Type --
8036 --------------------------------
8038 procedure Build_Derived_Private_Type
8039 (N : Node_Id;
8040 Parent_Type : Entity_Id;
8041 Derived_Type : Entity_Id;
8042 Is_Completion : Boolean;
8043 Derive_Subps : Boolean := True)
8045 Loc : constant Source_Ptr := Sloc (N);
8046 Par_Base : constant Entity_Id := Base_Type (Parent_Type);
8047 Par_Scope : constant Entity_Id := Scope (Par_Base);
8048 Full_N : constant Node_Id := New_Copy_Tree (N);
8049 Full_Der : Entity_Id := New_Copy (Derived_Type);
8050 Full_P : Entity_Id;
8052 function Available_Full_View (Typ : Entity_Id) return Entity_Id;
8053 -- Return the Full_View or Underlying_Full_View of Typ, whichever is
8054 -- present (they cannot be both present for the same type), or Empty.
8056 procedure Build_Full_Derivation;
8057 -- Build full derivation, i.e. derive from the full view
8059 procedure Copy_And_Build;
8060 -- Copy derived type declaration, replace parent with its full view,
8061 -- and build derivation
8063 -------------------------
8064 -- Available_Full_View --
8065 -------------------------
8067 function Available_Full_View (Typ : Entity_Id) return Entity_Id is
8068 begin
8069 if Present (Full_View (Typ)) then
8070 return Full_View (Typ);
8072 elsif Present (Underlying_Full_View (Typ)) then
8074 -- We should be called on a type with an underlying full view
8075 -- only by means of the recursive call made in Copy_And_Build
8076 -- through the first call to Build_Derived_Type, or else if
8077 -- the parent scope is being analyzed because we are deriving
8078 -- a completion.
8080 pragma Assert (Is_Completion or else In_Private_Part (Par_Scope));
8082 return Underlying_Full_View (Typ);
8084 else
8085 return Empty;
8086 end if;
8087 end Available_Full_View;
8089 ---------------------------
8090 -- Build_Full_Derivation --
8091 ---------------------------
8093 procedure Build_Full_Derivation is
8094 begin
8095 -- If parent scope is not open, install the declarations
8097 if not In_Open_Scopes (Par_Scope) then
8098 Install_Private_Declarations (Par_Scope);
8099 Install_Visible_Declarations (Par_Scope);
8100 Copy_And_Build;
8101 Uninstall_Declarations (Par_Scope);
8103 -- If parent scope is open and in another unit, and parent has a
8104 -- completion, then the derivation is taking place in the visible
8105 -- part of a child unit. In that case retrieve the full view of
8106 -- the parent momentarily.
8108 elsif not In_Same_Source_Unit (N, Parent_Type)
8109 and then Present (Full_View (Parent_Type))
8110 then
8111 Full_P := Full_View (Parent_Type);
8112 Exchange_Declarations (Parent_Type);
8113 Copy_And_Build;
8114 Exchange_Declarations (Full_P);
8116 -- Otherwise it is a local derivation
8118 else
8119 Copy_And_Build;
8120 end if;
8121 end Build_Full_Derivation;
8123 --------------------
8124 -- Copy_And_Build --
8125 --------------------
8127 procedure Copy_And_Build is
8128 Full_Parent : Entity_Id := Parent_Type;
8130 begin
8131 -- If the parent is itself derived from another private type,
8132 -- installing the private declarations has not affected its
8133 -- privacy status, so use its own full view explicitly.
8135 if Is_Private_Type (Full_Parent)
8136 and then Present (Full_View (Full_Parent))
8137 then
8138 Full_Parent := Full_View (Full_Parent);
8139 end if;
8141 -- If the full view is itself derived from another private type
8142 -- and has got an underlying full view, and this is done for a
8143 -- completion, i.e. to build the underlying full view of the type,
8144 -- then use this underlying full view. We cannot do that if this
8145 -- is not a completion, i.e. to build the full view of the type,
8146 -- because this would break the privacy of the parent type, except
8147 -- if the parent scope is being analyzed because we are deriving a
8148 -- completion.
8150 if Is_Private_Type (Full_Parent)
8151 and then Present (Underlying_Full_View (Full_Parent))
8152 and then (Is_Completion or else In_Private_Part (Par_Scope))
8153 then
8154 Full_Parent := Underlying_Full_View (Full_Parent);
8155 end if;
8157 -- For private, record, concurrent, access and almost all enumeration
8158 -- types, the derivation from the full view requires a fully-fledged
8159 -- declaration. In the other cases, just use an itype.
8161 if Is_Private_Type (Full_Parent)
8162 or else Is_Record_Type (Full_Parent)
8163 or else Is_Concurrent_Type (Full_Parent)
8164 or else Is_Access_Type (Full_Parent)
8165 or else
8166 (Is_Enumeration_Type (Full_Parent)
8167 and then not Is_Standard_Character_Type (Full_Parent)
8168 and then not Is_Generic_Type (Root_Type (Full_Parent)))
8169 then
8170 -- Copy and adjust declaration to provide a completion for what
8171 -- is originally a private declaration. Indicate that full view
8172 -- is internally generated.
8174 Set_Comes_From_Source (Full_N, False);
8175 Set_Comes_From_Source (Full_Der, False);
8176 Set_Parent (Full_Der, Full_N);
8177 Set_Defining_Identifier (Full_N, Full_Der);
8179 -- If there are no constraints, adjust the subtype mark
8181 if Nkind (Subtype_Indication (Type_Definition (Full_N))) /=
8182 N_Subtype_Indication
8183 then
8184 Set_Subtype_Indication
8185 (Type_Definition (Full_N),
8186 New_Occurrence_Of (Full_Parent, Sloc (Full_N)));
8187 end if;
8189 Insert_After (N, Full_N);
8191 -- Build full view of derived type from full view of parent which
8192 -- is now installed. Subprograms have been derived on the partial
8193 -- view, the completion does not derive them anew.
8195 if Is_Record_Type (Full_Parent) then
8197 -- If parent type is tagged, the completion inherits the proper
8198 -- primitive operations.
8200 if Is_Tagged_Type (Parent_Type) then
8201 Build_Derived_Record_Type
8202 (Full_N, Full_Parent, Full_Der, Derive_Subps);
8203 else
8204 Build_Derived_Record_Type
8205 (Full_N, Full_Parent, Full_Der, Derive_Subps => False);
8206 end if;
8208 else
8209 -- If the parent type is private, this is not a completion and
8210 -- we build the full derivation recursively as a completion.
8212 Build_Derived_Type
8213 (Full_N, Full_Parent, Full_Der,
8214 Is_Completion => Is_Private_Type (Full_Parent),
8215 Derive_Subps => False);
8216 end if;
8218 -- The full declaration has been introduced into the tree and
8219 -- processed in the step above. It should not be analyzed again
8220 -- (when encountered later in the current list of declarations)
8221 -- to prevent spurious name conflicts. The full entity remains
8222 -- invisible.
8224 Set_Analyzed (Full_N);
8226 else
8227 Full_Der :=
8228 Make_Defining_Identifier (Sloc (Derived_Type),
8229 Chars => Chars (Derived_Type));
8230 Set_Is_Itype (Full_Der);
8231 Set_Associated_Node_For_Itype (Full_Der, N);
8232 Set_Parent (Full_Der, N);
8233 Build_Derived_Type
8234 (N, Full_Parent, Full_Der,
8235 Is_Completion => False, Derive_Subps => False);
8236 Set_Is_Not_Self_Hidden (Full_Der);
8237 end if;
8239 Set_Has_Private_Declaration (Full_Der);
8240 Set_Has_Private_Declaration (Derived_Type);
8242 Set_Scope (Full_Der, Scope (Derived_Type));
8243 Set_Is_First_Subtype (Full_Der, Is_First_Subtype (Derived_Type));
8244 Set_Has_Size_Clause (Full_Der, False);
8245 Set_Has_Alignment_Clause (Full_Der, False);
8246 Set_Has_Delayed_Freeze (Full_Der);
8247 Set_Is_Frozen (Full_Der, False);
8248 Set_Freeze_Node (Full_Der, Empty);
8249 Set_Depends_On_Private (Full_Der, Has_Private_Component (Full_Der));
8250 Set_Is_Public (Full_Der, Is_Public (Derived_Type));
8252 -- The convention on the base type may be set in the private part
8253 -- and not propagated to the subtype until later, so we obtain the
8254 -- convention from the base type of the parent.
8256 Set_Convention (Full_Der, Convention (Base_Type (Full_Parent)));
8257 end Copy_And_Build;
8259 -- Start of processing for Build_Derived_Private_Type
8261 begin
8262 if Is_Tagged_Type (Parent_Type) then
8263 Full_P := Full_View (Parent_Type);
8265 -- A type extension of a type with unknown discriminants is an
8266 -- indefinite type that the back-end cannot handle directly.
8267 -- We treat it as a private type, and build a completion that is
8268 -- derived from the full view of the parent, and hopefully has
8269 -- known discriminants.
8271 -- If the full view of the parent type has an underlying record view,
8272 -- use it to generate the underlying record view of this derived type
8273 -- (required for chains of derivations with unknown discriminants).
8275 -- Minor optimization: we avoid the generation of useless underlying
8276 -- record view entities if the private type declaration has unknown
8277 -- discriminants but its corresponding full view has no
8278 -- discriminants.
8280 if Has_Unknown_Discriminants (Parent_Type)
8281 and then Present (Full_P)
8282 and then (Has_Discriminants (Full_P)
8283 or else Present (Underlying_Record_View (Full_P)))
8284 and then not In_Open_Scopes (Par_Scope)
8285 and then Expander_Active
8286 then
8287 declare
8288 Full_Der : constant Entity_Id := Make_Temporary (Loc, 'T');
8289 New_Ext : constant Node_Id :=
8290 Copy_Separate_Tree
8291 (Record_Extension_Part (Type_Definition (N)));
8292 Decl : Node_Id;
8294 begin
8295 Build_Derived_Record_Type
8296 (N, Parent_Type, Derived_Type, Derive_Subps);
8298 -- Build anonymous completion, as a derivation from the full
8299 -- view of the parent. This is not a completion in the usual
8300 -- sense, because the current type is not private.
8302 Decl :=
8303 Make_Full_Type_Declaration (Loc,
8304 Defining_Identifier => Full_Der,
8305 Type_Definition =>
8306 Make_Derived_Type_Definition (Loc,
8307 Subtype_Indication =>
8308 New_Copy_Tree
8309 (Subtype_Indication (Type_Definition (N))),
8310 Record_Extension_Part => New_Ext));
8312 -- If the parent type has an underlying record view, use it
8313 -- here to build the new underlying record view.
8315 if Present (Underlying_Record_View (Full_P)) then
8316 pragma Assert
8317 (Nkind (Subtype_Indication (Type_Definition (Decl)))
8318 = N_Identifier);
8319 Set_Entity (Subtype_Indication (Type_Definition (Decl)),
8320 Underlying_Record_View (Full_P));
8321 end if;
8323 Install_Private_Declarations (Par_Scope);
8324 Install_Visible_Declarations (Par_Scope);
8325 Insert_Before (N, Decl);
8327 -- Mark entity as an underlying record view before analysis,
8328 -- to avoid generating the list of its primitive operations
8329 -- (which is not really required for this entity) and thus
8330 -- prevent spurious errors associated with missing overriding
8331 -- of abstract primitives (overridden only for Derived_Type).
8333 Mutate_Ekind (Full_Der, E_Record_Type);
8334 Set_Is_Underlying_Record_View (Full_Der);
8335 Set_Default_SSO (Full_Der);
8336 Set_No_Reordering (Full_Der, No_Component_Reordering);
8338 Analyze (Decl);
8340 pragma Assert (Has_Discriminants (Full_Der)
8341 and then not Has_Unknown_Discriminants (Full_Der));
8343 Uninstall_Declarations (Par_Scope);
8345 -- Freeze the underlying record view, to prevent generation of
8346 -- useless dispatching information, which is simply shared with
8347 -- the real derived type.
8349 Set_Is_Frozen (Full_Der);
8351 -- If the derived type has access discriminants, create
8352 -- references to their anonymous types now, to prevent
8353 -- back-end problems when their first use is in generated
8354 -- bodies of primitives.
8356 declare
8357 E : Entity_Id;
8359 begin
8360 E := First_Entity (Full_Der);
8362 while Present (E) loop
8363 if Ekind (E) = E_Discriminant
8364 and then Ekind (Etype (E)) = E_Anonymous_Access_Type
8365 then
8366 Build_Itype_Reference (Etype (E), Decl);
8367 end if;
8369 Next_Entity (E);
8370 end loop;
8371 end;
8373 -- Set up links between real entity and underlying record view
8375 Set_Underlying_Record_View (Derived_Type, Base_Type (Full_Der));
8376 Set_Underlying_Record_View (Base_Type (Full_Der), Derived_Type);
8377 end;
8379 -- If discriminants are known, build derived record
8381 else
8382 Build_Derived_Record_Type
8383 (N, Parent_Type, Derived_Type, Derive_Subps);
8384 end if;
8386 return;
8388 elsif Has_Discriminants (Parent_Type) then
8390 -- Build partial view of derived type from partial view of parent.
8391 -- This must be done before building the full derivation because the
8392 -- second derivation will modify the discriminants of the first and
8393 -- the discriminants are chained with the rest of the components in
8394 -- the full derivation.
8396 Build_Derived_Record_Type
8397 (N, Parent_Type, Derived_Type, Derive_Subps);
8399 -- Build the full derivation if this is not the anonymous derived
8400 -- base type created by Build_Derived_Record_Type in the constrained
8401 -- case (see point 5. of its head comment) since we build it for the
8402 -- derived subtype.
8404 if Present (Available_Full_View (Parent_Type))
8405 and then not Is_Itype (Derived_Type)
8406 then
8407 declare
8408 Der_Base : constant Entity_Id := Base_Type (Derived_Type);
8409 Discr : Entity_Id;
8410 Last_Discr : Entity_Id;
8412 begin
8413 -- If this is not a completion, construct the implicit full
8414 -- view by deriving from the full view of the parent type.
8415 -- But if this is a completion, the derived private type
8416 -- being built is a full view and the full derivation can
8417 -- only be its underlying full view.
8419 Build_Full_Derivation;
8421 if not Is_Completion then
8422 Set_Full_View (Derived_Type, Full_Der);
8423 else
8424 Set_Underlying_Full_View (Derived_Type, Full_Der);
8425 Set_Is_Underlying_Full_View (Full_Der);
8426 end if;
8428 if not Is_Base_Type (Derived_Type) then
8429 Set_Full_View (Der_Base, Base_Type (Full_Der));
8430 end if;
8432 -- Copy the discriminant list from full view to the partial
8433 -- view (base type and its subtype). Gigi requires that the
8434 -- partial and full views have the same discriminants.
8436 -- Note that since the partial view points to discriminants
8437 -- in the full view, their scope will be that of the full
8438 -- view. This might cause some front end problems and need
8439 -- adjustment???
8441 Discr := First_Discriminant (Base_Type (Full_Der));
8442 Set_First_Entity (Der_Base, Discr);
8444 loop
8445 Last_Discr := Discr;
8446 Next_Discriminant (Discr);
8447 exit when No (Discr);
8448 end loop;
8450 Set_Last_Entity (Der_Base, Last_Discr);
8451 Set_First_Entity (Derived_Type, First_Entity (Der_Base));
8452 Set_Last_Entity (Derived_Type, Last_Entity (Der_Base));
8453 end;
8454 end if;
8456 elsif Present (Available_Full_View (Parent_Type))
8457 and then Has_Discriminants (Available_Full_View (Parent_Type))
8458 then
8459 if Has_Unknown_Discriminants (Parent_Type)
8460 and then Nkind (Subtype_Indication (Type_Definition (N))) =
8461 N_Subtype_Indication
8462 then
8463 Error_Msg_N
8464 ("cannot constrain type with unknown discriminants",
8465 Subtype_Indication (Type_Definition (N)));
8466 return;
8467 end if;
8469 -- If this is not a completion, construct the implicit full view by
8470 -- deriving from the full view of the parent type. But if this is a
8471 -- completion, the derived private type being built is a full view
8472 -- and the full derivation can only be its underlying full view.
8474 Build_Full_Derivation;
8476 if not Is_Completion then
8477 Set_Full_View (Derived_Type, Full_Der);
8478 else
8479 Set_Underlying_Full_View (Derived_Type, Full_Der);
8480 Set_Is_Underlying_Full_View (Full_Der);
8481 end if;
8483 -- In any case, the primitive operations are inherited from the
8484 -- parent type, not from the internal full view.
8486 Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
8488 if Derive_Subps then
8489 -- Initialize the list of primitive operations to an empty list,
8490 -- to cover tagged types as well as untagged types. For untagged
8491 -- types this is used either to analyze the call as legal when
8492 -- GNAT extensions are allowed, or to give better error messages.
8494 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
8496 Derive_Subprograms (Parent_Type, Derived_Type);
8497 end if;
8499 Set_Stored_Constraint (Derived_Type, No_Elist);
8500 Set_Is_Constrained
8501 (Derived_Type, Is_Constrained (Available_Full_View (Parent_Type)));
8503 else
8504 -- Untagged type, No discriminants on either view
8506 if Nkind (Subtype_Indication (Type_Definition (N))) =
8507 N_Subtype_Indication
8508 then
8509 Error_Msg_N
8510 ("illegal constraint on type without discriminants", N);
8511 end if;
8513 if Present (Discriminant_Specifications (N))
8514 and then Present (Available_Full_View (Parent_Type))
8515 and then not Is_Tagged_Type (Available_Full_View (Parent_Type))
8516 then
8517 Error_Msg_N ("cannot add discriminants to untagged type", N);
8518 end if;
8520 Set_Stored_Constraint (Derived_Type, No_Elist);
8521 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
8523 -- If this is not a completion, construct the implicit full view by
8524 -- deriving from the full view of the parent type. But if this is a
8525 -- completion, the derived private type being built is a full view
8526 -- and the full derivation can only be its underlying full view.
8528 -- ??? If the parent type is untagged private and its completion is
8529 -- tagged, this mechanism will not work because we cannot derive from
8530 -- the tagged full view unless we have an extension.
8532 if Present (Available_Full_View (Parent_Type))
8533 and then not Is_Tagged_Type (Available_Full_View (Parent_Type))
8534 and then not Error_Posted (N)
8535 then
8536 Build_Full_Derivation;
8538 if not Is_Completion then
8539 Set_Full_View (Derived_Type, Full_Der);
8540 else
8541 Set_Underlying_Full_View (Derived_Type, Full_Der);
8542 Set_Is_Underlying_Full_View (Full_Der);
8543 end if;
8544 end if;
8545 end if;
8547 Set_Has_Unknown_Discriminants (Derived_Type,
8548 Has_Unknown_Discriminants (Parent_Type));
8550 if Is_Private_Type (Derived_Type) then
8551 Set_Private_Dependents (Derived_Type, New_Elmt_List);
8552 end if;
8554 -- If the parent base type is in scope, add the derived type to its
8555 -- list of private dependents, because its full view may become
8556 -- visible subsequently (in a nested private part, a body, or in a
8557 -- further child unit).
8559 if Is_Private_Type (Par_Base) and then In_Open_Scopes (Par_Scope) then
8560 Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
8562 -- Check for unusual case where a type completed by a private
8563 -- derivation occurs within a package nested in a child unit, and
8564 -- the parent is declared in an ancestor.
8566 if Is_Child_Unit (Scope (Current_Scope))
8567 and then Is_Completion
8568 and then In_Private_Part (Current_Scope)
8569 and then Scope (Parent_Type) /= Current_Scope
8571 -- Note that if the parent has a completion in the private part,
8572 -- (which is itself a derivation from some other private type)
8573 -- it is that completion that is visible, there is no full view
8574 -- available, and no special processing is needed.
8576 and then Present (Full_View (Parent_Type))
8577 then
8578 -- In this case, the full view of the parent type will become
8579 -- visible in the body of the enclosing child, and only then will
8580 -- the current type be possibly non-private. Build an underlying
8581 -- full view that will be installed when the enclosing child body
8582 -- is compiled.
8584 if Present (Underlying_Full_View (Derived_Type)) then
8585 Full_Der := Underlying_Full_View (Derived_Type);
8586 else
8587 Build_Full_Derivation;
8588 Set_Underlying_Full_View (Derived_Type, Full_Der);
8589 Set_Is_Underlying_Full_View (Full_Der);
8590 end if;
8592 -- The full view will be used to swap entities on entry/exit to
8593 -- the body, and must appear in the entity list for the package.
8595 Append_Entity (Full_Der, Scope (Derived_Type));
8596 end if;
8597 end if;
8598 end Build_Derived_Private_Type;
8600 -------------------------------
8601 -- Build_Derived_Record_Type --
8602 -------------------------------
8604 -- 1. INTRODUCTION
8606 -- Ideally we would like to use the same model of type derivation for
8607 -- tagged and untagged record types. Unfortunately this is not quite
8608 -- possible because the semantics of representation clauses is different
8609 -- for tagged and untagged records under inheritance. Consider the
8610 -- following:
8612 -- type R (...) is [tagged] record ... end record;
8613 -- type T (...) is new R (...) [with ...];
8615 -- The representation clauses for T can specify a completely different
8616 -- record layout from R's. Hence the same component can be placed in two
8617 -- very different positions in objects of type T and R. If R and T are
8618 -- tagged types, representation clauses for T can only specify the layout
8619 -- of non inherited components, thus components that are common in R and T
8620 -- have the same position in objects of type R and T.
8622 -- This has two implications. The first is that the entire tree for R's
8623 -- declaration needs to be copied for T in the untagged case, so that T
8624 -- can be viewed as a record type of its own with its own representation
8625 -- clauses. The second implication is the way we handle discriminants.
8626 -- Specifically, in the untagged case we need a way to communicate to Gigi
8627 -- what are the real discriminants in the record, while for the semantics
8628 -- we need to consider those introduced by the user to rename the
8629 -- discriminants in the parent type. This is handled by introducing the
8630 -- notion of stored discriminants. See below for more.
8632 -- Fortunately the way regular components are inherited can be handled in
8633 -- the same way in tagged and untagged types.
8635 -- To complicate things a bit more the private view of a private extension
8636 -- cannot be handled in the same way as the full view (for one thing the
8637 -- semantic rules are somewhat different). We will explain what differs
8638 -- below.
8640 -- 2. DISCRIMINANTS UNDER INHERITANCE
8642 -- The semantic rules governing the discriminants of derived types are
8643 -- quite subtle.
8645 -- type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
8646 -- [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
8648 -- If parent type has discriminants, then the discriminants that are
8649 -- declared in the derived type are [3.4 (11)]:
8651 -- o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
8652 -- there is one;
8654 -- o Otherwise, each discriminant of the parent type (implicitly declared
8655 -- in the same order with the same specifications). In this case, the
8656 -- discriminants are said to be "inherited", or if unknown in the parent
8657 -- are also unknown in the derived type.
8659 -- Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
8661 -- o The parent subtype must be constrained;
8663 -- o If the parent type is not a tagged type, then each discriminant of
8664 -- the derived type must be used in the constraint defining a parent
8665 -- subtype. [Implementation note: This ensures that the new discriminant
8666 -- can share storage with an existing discriminant.]
8668 -- For the derived type each discriminant of the parent type is either
8669 -- inherited, constrained to equal some new discriminant of the derived
8670 -- type, or constrained to the value of an expression.
8672 -- When inherited or constrained to equal some new discriminant, the
8673 -- parent discriminant and the discriminant of the derived type are said
8674 -- to "correspond".
8676 -- If a discriminant of the parent type is constrained to a specific value
8677 -- in the derived type definition, then the discriminant is said to be
8678 -- "specified" by that derived type definition.
8680 -- 3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
8682 -- We have spoken about stored discriminants in point 1 (introduction)
8683 -- above. There are two sorts of stored discriminants: implicit and
8684 -- explicit. As long as the derived type inherits the same discriminants as
8685 -- the root record type, stored discriminants are the same as regular
8686 -- discriminants, and are said to be implicit. However, if any discriminant
8687 -- in the root type was renamed in the derived type, then the derived
8688 -- type will contain explicit stored discriminants. Explicit stored
8689 -- discriminants are discriminants in addition to the semantically visible
8690 -- discriminants defined for the derived type. Stored discriminants are
8691 -- used by Gigi to figure out what are the physical discriminants in
8692 -- objects of the derived type (see precise definition in einfo.ads).
8693 -- As an example, consider the following:
8695 -- type R (D1, D2, D3 : Int) is record ... end record;
8696 -- type T1 is new R;
8697 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
8698 -- type T3 is new T2;
8699 -- type T4 (Y : Int) is new T3 (Y, 99);
8701 -- The following table summarizes the discriminants and stored
8702 -- discriminants in R and T1 through T4:
8704 -- Type Discrim Stored Discrim Comment
8705 -- R (D1, D2, D3) (D1, D2, D3) Stored discrims implicit in R
8706 -- T1 (D1, D2, D3) (D1, D2, D3) Stored discrims implicit in T1
8707 -- T2 (X1, X2) (D1, D2, D3) Stored discrims EXPLICIT in T2
8708 -- T3 (X1, X2) (D1, D2, D3) Stored discrims EXPLICIT in T3
8709 -- T4 (Y) (D1, D2, D3) Stored discrims EXPLICIT in T4
8711 -- Field Corresponding_Discriminant (abbreviated CD below) allows us to
8712 -- find the corresponding discriminant in the parent type, while
8713 -- Original_Record_Component (abbreviated ORC below) the actual physical
8714 -- component that is renamed. Finally the field Is_Completely_Hidden
8715 -- (abbreviated ICH below) is set for all explicit stored discriminants
8716 -- (see einfo.ads for more info). For the above example this gives:
8718 -- Discrim CD ORC ICH
8719 -- ^^^^^^^ ^^ ^^^ ^^^
8720 -- D1 in R empty itself no
8721 -- D2 in R empty itself no
8722 -- D3 in R empty itself no
8724 -- D1 in T1 D1 in R itself no
8725 -- D2 in T1 D2 in R itself no
8726 -- D3 in T1 D3 in R itself no
8728 -- X1 in T2 D3 in T1 D3 in T2 no
8729 -- X2 in T2 D1 in T1 D1 in T2 no
8730 -- D1 in T2 empty itself yes
8731 -- D2 in T2 empty itself yes
8732 -- D3 in T2 empty itself yes
8734 -- X1 in T3 X1 in T2 D3 in T3 no
8735 -- X2 in T3 X2 in T2 D1 in T3 no
8736 -- D1 in T3 empty itself yes
8737 -- D2 in T3 empty itself yes
8738 -- D3 in T3 empty itself yes
8740 -- Y in T4 X1 in T3 D3 in T4 no
8741 -- D1 in T4 empty itself yes
8742 -- D2 in T4 empty itself yes
8743 -- D3 in T4 empty itself yes
8745 -- 4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
8747 -- Type derivation for tagged types is fairly straightforward. If no
8748 -- discriminants are specified by the derived type, these are inherited
8749 -- from the parent. No explicit stored discriminants are ever necessary.
8750 -- The only manipulation that is done to the tree is that of adding a
8751 -- _parent field with parent type and constrained to the same constraint
8752 -- specified for the parent in the derived type definition. For instance:
8754 -- type R (D1, D2, D3 : Int) is tagged record ... end record;
8755 -- type T1 is new R with null record;
8756 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
8758 -- are changed into:
8760 -- type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
8761 -- _parent : R (D1, D2, D3);
8762 -- end record;
8764 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
8765 -- _parent : T1 (X2, 88, X1);
8766 -- end record;
8768 -- The discriminants actually present in R, T1 and T2 as well as their CD,
8769 -- ORC and ICH fields are:
8771 -- Discrim CD ORC ICH
8772 -- ^^^^^^^ ^^ ^^^ ^^^
8773 -- D1 in R empty itself no
8774 -- D2 in R empty itself no
8775 -- D3 in R empty itself no
8777 -- D1 in T1 D1 in R D1 in R no
8778 -- D2 in T1 D2 in R D2 in R no
8779 -- D3 in T1 D3 in R D3 in R no
8781 -- X1 in T2 D3 in T1 D3 in R no
8782 -- X2 in T2 D1 in T1 D1 in R no
8784 -- 5. FIRST TRANSFORMATION FOR DERIVED RECORDS
8786 -- Regardless of whether we are dealing with a tagged or untagged type
8787 -- we will transform all derived type declarations of the form
8789 -- type T is new R (...) [with ...];
8790 -- or
8791 -- subtype S is R (...);
8792 -- type T is new S [with ...];
8793 -- into
8794 -- type BT is new R [with ...];
8795 -- subtype T is BT (...);
8797 -- That is, the base derived type is constrained only if it has no
8798 -- discriminants. The reason for doing this is that GNAT's semantic model
8799 -- assumes that a base type with discriminants is unconstrained.
8801 -- Note that, strictly speaking, the above transformation is not always
8802 -- correct. Consider for instance the following excerpt from ACVC b34011a:
8804 -- procedure B34011A is
8805 -- type REC (D : integer := 0) is record
8806 -- I : Integer;
8807 -- end record;
8809 -- package P is
8810 -- type T6 is new Rec;
8811 -- function F return T6;
8812 -- end P;
8814 -- use P;
8815 -- package Q6 is
8816 -- type U is new T6 (Q6.F.I); -- ERROR: Q6.F.
8817 -- end Q6;
8819 -- The definition of Q6.U is illegal. However transforming Q6.U into
8821 -- type BaseU is new T6;
8822 -- subtype U is BaseU (Q6.F.I)
8824 -- turns U into a legal subtype, which is incorrect. To avoid this problem
8825 -- we always analyze the constraint (in this case (Q6.F.I)) before applying
8826 -- the transformation described above.
8828 -- There is another instance where the above transformation is incorrect.
8829 -- Consider:
8831 -- package Pack is
8832 -- type Base (D : Integer) is tagged null record;
8833 -- procedure P (X : Base);
8835 -- type Der is new Base (2) with null record;
8836 -- procedure P (X : Der);
8837 -- end Pack;
8839 -- Then the above transformation turns this into
8841 -- type Der_Base is new Base with null record;
8842 -- -- procedure P (X : Base) is implicitly inherited here
8843 -- -- as procedure P (X : Der_Base).
8845 -- subtype Der is Der_Base (2);
8846 -- procedure P (X : Der);
8847 -- -- The overriding of P (X : Der_Base) is illegal since we
8848 -- -- have a parameter conformance problem.
8850 -- To get around this problem, after having semantically processed Der_Base
8851 -- and the rewritten subtype declaration for Der, we copy Der_Base field
8852 -- Discriminant_Constraint from Der so that when parameter conformance is
8853 -- checked when P is overridden, no semantic errors are flagged.
8855 -- 6. SECOND TRANSFORMATION FOR DERIVED RECORDS
8857 -- Regardless of whether we are dealing with a tagged or untagged type
8858 -- we will transform all derived type declarations of the form
8860 -- type R (D1, .., Dn : ...) is [tagged] record ...;
8861 -- type T is new R [with ...];
8862 -- into
8863 -- type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
8865 -- The reason for such transformation is that it allows us to implement a
8866 -- very clean form of component inheritance as explained below.
8868 -- Note that this transformation is not achieved by direct tree rewriting
8869 -- and manipulation, but rather by redoing the semantic actions that the
8870 -- above transformation will entail. This is done directly in routine
8871 -- Inherit_Components.
8873 -- 7. TYPE DERIVATION AND COMPONENT INHERITANCE
8875 -- In both tagged and untagged derived types, regular non discriminant
8876 -- components are inherited in the derived type from the parent type. In
8877 -- the absence of discriminants component, inheritance is straightforward
8878 -- as components can simply be copied from the parent.
8880 -- If the parent has discriminants, inheriting components constrained with
8881 -- these discriminants requires caution. Consider the following example:
8883 -- type R (D1, D2 : Positive) is [tagged] record
8884 -- S : String (D1 .. D2);
8885 -- end record;
8887 -- type T1 is new R [with null record];
8888 -- type T2 (X : positive) is new R (1, X) [with null record];
8890 -- As explained in 6. above, T1 is rewritten as
8891 -- type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
8892 -- which makes the treatment for T1 and T2 identical.
8894 -- What we want when inheriting S, is that references to D1 and D2 in R are
8895 -- replaced with references to their correct constraints, i.e. D1 and D2 in
8896 -- T1 and 1 and X in T2. So all R's discriminant references are replaced
8897 -- with either discriminant references in the derived type or expressions.
8898 -- This replacement is achieved as follows: before inheriting R's
8899 -- components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
8900 -- created in the scope of T1 (resp. scope of T2) so that discriminants D1
8901 -- and D2 of T1 are visible (resp. discriminant X of T2 is visible).
8902 -- For T2, for instance, this has the effect of replacing String (D1 .. D2)
8903 -- by String (1 .. X).
8905 -- 8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
8907 -- We explain here the rules governing private type extensions relevant to
8908 -- type derivation. These rules are explained on the following example:
8910 -- type D [(...)] is new A [(...)] with private; <-- partial view
8911 -- type D [(...)] is new P [(...)] with null record; <-- full view
8913 -- Type A is called the ancestor subtype of the private extension.
8914 -- Type P is the parent type of the full view of the private extension. It
8915 -- must be A or a type derived from A.
8917 -- The rules concerning the discriminants of private type extensions are
8918 -- [7.3(10-13)]:
8920 -- o If a private extension inherits known discriminants from the ancestor
8921 -- subtype, then the full view must also inherit its discriminants from
8922 -- the ancestor subtype and the parent subtype of the full view must be
8923 -- constrained if and only if the ancestor subtype is constrained.
8925 -- o If a partial view has unknown discriminants, then the full view may
8926 -- define a definite or an indefinite subtype, with or without
8927 -- discriminants.
8929 -- o If a partial view has neither known nor unknown discriminants, then
8930 -- the full view must define a definite subtype.
8932 -- o If the ancestor subtype of a private extension has constrained
8933 -- discriminants, then the parent subtype of the full view must impose a
8934 -- statically matching constraint on those discriminants.
8936 -- This means that only the following forms of private extensions are
8937 -- allowed:
8939 -- type D is new A with private; <-- partial view
8940 -- type D is new P with null record; <-- full view
8942 -- If A has no discriminants than P has no discriminants, otherwise P must
8943 -- inherit A's discriminants.
8945 -- type D is new A (...) with private; <-- partial view
8946 -- type D is new P (:::) with null record; <-- full view
8948 -- P must inherit A's discriminants and (...) and (:::) must statically
8949 -- match.
8951 -- subtype A is R (...);
8952 -- type D is new A with private; <-- partial view
8953 -- type D is new P with null record; <-- full view
8955 -- P must have inherited R's discriminants and must be derived from A or
8956 -- any of its subtypes.
8958 -- type D (..) is new A with private; <-- partial view
8959 -- type D (..) is new P [(:::)] with null record; <-- full view
8961 -- No specific constraints on P's discriminants or constraint (:::).
8962 -- Note that A can be unconstrained, but the parent subtype P must either
8963 -- be constrained or (:::) must be present.
8965 -- type D (..) is new A [(...)] with private; <-- partial view
8966 -- type D (..) is new P [(:::)] with null record; <-- full view
8968 -- P's constraints on A's discriminants must statically match those
8969 -- imposed by (...).
8971 -- 9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
8973 -- The full view of a private extension is handled exactly as described
8974 -- above. The model chose for the private view of a private extension is
8975 -- the same for what concerns discriminants (i.e. they receive the same
8976 -- treatment as in the tagged case). However, the private view of the
8977 -- private extension always inherits the components of the parent base,
8978 -- without replacing any discriminant reference. Strictly speaking this is
8979 -- incorrect. However, Gigi never uses this view to generate code so this
8980 -- is a purely semantic issue. In theory, a set of transformations similar
8981 -- to those given in 5. and 6. above could be applied to private views of
8982 -- private extensions to have the same model of component inheritance as
8983 -- for non private extensions. However, this is not done because it would
8984 -- further complicate private type processing. Semantically speaking, this
8985 -- leaves us in an uncomfortable situation. As an example consider:
8987 -- package Pack is
8988 -- type R (D : integer) is tagged record
8989 -- S : String (1 .. D);
8990 -- end record;
8991 -- procedure P (X : R);
8992 -- type T is new R (1) with private;
8993 -- private
8994 -- type T is new R (1) with null record;
8995 -- end;
8997 -- This is transformed into:
8999 -- package Pack is
9000 -- type R (D : integer) is tagged record
9001 -- S : String (1 .. D);
9002 -- end record;
9003 -- procedure P (X : R);
9004 -- type T is new R (1) with private;
9005 -- private
9006 -- type BaseT is new R with null record;
9007 -- subtype T is BaseT (1);
9008 -- end;
9010 -- (strictly speaking the above is incorrect Ada)
9012 -- From the semantic standpoint the private view of private extension T
9013 -- should be flagged as constrained since one can clearly have
9015 -- Obj : T;
9017 -- in a unit withing Pack. However, when deriving subprograms for the
9018 -- private view of private extension T, T must be seen as unconstrained
9019 -- since T has discriminants (this is a constraint of the current
9020 -- subprogram derivation model). Thus, when processing the private view of
9021 -- a private extension such as T, we first mark T as unconstrained, we
9022 -- process it, we perform program derivation and just before returning from
9023 -- Build_Derived_Record_Type we mark T as constrained.
9025 -- ??? Are there are other uncomfortable cases that we will have to
9026 -- deal with.
9028 -- 10. RECORD_TYPE_WITH_PRIVATE complications
9030 -- Types that are derived from a visible record type and have a private
9031 -- extension present other peculiarities. They behave mostly like private
9032 -- types, but if they have primitive operations defined, these will not
9033 -- have the proper signatures for further inheritance, because other
9034 -- primitive operations will use the implicit base that we define for
9035 -- private derivations below. This affect subprogram inheritance (see
9036 -- Derive_Subprograms for details). We also derive the implicit base from
9037 -- the base type of the full view, so that the implicit base is a record
9038 -- type and not another private type, This avoids infinite loops.
9040 procedure Build_Derived_Record_Type
9041 (N : Node_Id;
9042 Parent_Type : Entity_Id;
9043 Derived_Type : Entity_Id;
9044 Derive_Subps : Boolean := True)
9046 Discriminant_Specs : constant Boolean :=
9047 Present (Discriminant_Specifications (N));
9048 Is_Tagged : constant Boolean := Is_Tagged_Type (Parent_Type);
9049 Loc : constant Source_Ptr := Sloc (N);
9050 Private_Extension : constant Boolean :=
9051 Nkind (N) = N_Private_Extension_Declaration;
9052 Assoc_List : Elist_Id;
9053 Constraint_Present : Boolean;
9054 Constrs : Elist_Id;
9055 Discrim : Entity_Id;
9056 Indic : Node_Id;
9057 Inherit_Discrims : Boolean := False;
9058 Last_Discrim : Entity_Id;
9059 New_Base : Entity_Id;
9060 New_Decl : Node_Id;
9061 New_Discrs : Elist_Id;
9062 New_Indic : Node_Id;
9063 Parent_Base : Entity_Id;
9064 Save_Etype : Entity_Id;
9065 Save_Discr_Constr : Elist_Id;
9066 Save_Next_Entity : Entity_Id;
9067 Type_Def : Node_Id;
9069 Discs : Elist_Id := New_Elmt_List;
9070 -- An empty Discs list means that there were no constraints in the
9071 -- subtype indication or that there was an error processing it.
9073 procedure Check_Generic_Ancestors;
9074 -- In Ada 2005 (AI-344), the restriction that a derived tagged type
9075 -- cannot be declared at a deeper level than its parent type is
9076 -- removed. The check on derivation within a generic body is also
9077 -- relaxed, but there's a restriction that a derived tagged type
9078 -- cannot be declared in a generic body if it's derived directly
9079 -- or indirectly from a formal type of that generic. This applies
9080 -- to progenitors as well.
9082 -----------------------------
9083 -- Check_Generic_Ancestors --
9084 -----------------------------
9086 procedure Check_Generic_Ancestors is
9087 Ancestor_Type : Entity_Id;
9088 Intf_List : List_Id;
9089 Intf_Name : Node_Id;
9091 procedure Check_Ancestor;
9092 -- For parent and progenitors.
9094 --------------------
9095 -- Check_Ancestor --
9096 --------------------
9098 procedure Check_Ancestor is
9099 begin
9100 -- If the derived type does have a formal type as an ancestor
9101 -- then it's an error if the derived type is declared within
9102 -- the body of the generic unit that declares the formal type
9103 -- in its generic formal part. It's sufficient to check whether
9104 -- the ancestor type is declared inside the same generic body
9105 -- as the derived type (such as within a nested generic spec),
9106 -- in which case the derivation is legal. If the formal type is
9107 -- declared outside of that generic body, then it's certain
9108 -- that the derived type is declared within the generic body
9109 -- of the generic unit declaring the formal type.
9111 if Is_Generic_Type (Ancestor_Type)
9112 and then Enclosing_Generic_Body (Ancestor_Type) /=
9113 Enclosing_Generic_Body (Derived_Type)
9114 then
9115 Error_Msg_NE
9116 ("ancestor type& is formal type of enclosing"
9117 & " generic unit (RM 3.9.1 (4/2))",
9118 Indic, Ancestor_Type);
9119 end if;
9120 end Check_Ancestor;
9122 begin
9123 if Nkind (N) = N_Private_Extension_Declaration then
9124 Intf_List := Interface_List (N);
9125 else
9126 Intf_List := Interface_List (Type_Definition (N));
9127 end if;
9129 if Present (Enclosing_Generic_Body (Derived_Type)) then
9130 Ancestor_Type := Parent_Type;
9132 while not Is_Generic_Type (Ancestor_Type)
9133 and then Etype (Ancestor_Type) /= Ancestor_Type
9134 loop
9135 Ancestor_Type := Etype (Ancestor_Type);
9136 end loop;
9138 Check_Ancestor;
9140 if Present (Intf_List) then
9141 Intf_Name := First (Intf_List);
9142 while Present (Intf_Name) loop
9143 Ancestor_Type := Entity (Intf_Name);
9144 Check_Ancestor;
9145 Next (Intf_Name);
9146 end loop;
9147 end if;
9148 end if;
9149 end Check_Generic_Ancestors;
9151 -- Start of processing for Build_Derived_Record_Type
9153 begin
9154 -- If the parent type is a private extension with discriminants, we
9155 -- need to have an unconstrained type on which to apply the inherited
9156 -- constraint, so we get to the full view. However, this means that the
9157 -- derived type and its implicit base type created below will not point
9158 -- to the same view of their respective parent type and, thus, special
9159 -- glue code like Exp_Ch7.Convert_View is needed to bridge this gap.
9161 if Ekind (Parent_Type) = E_Record_Type_With_Private
9162 and then Has_Discriminants (Parent_Type)
9163 and then Present (Full_View (Parent_Type))
9164 then
9165 Parent_Base := Base_Type (Full_View (Parent_Type));
9166 else
9167 Parent_Base := Base_Type (Parent_Type);
9168 end if;
9170 -- If the parent type is declared as a subtype of another private
9171 -- type with inherited discriminants, its generated base type is
9172 -- itself a record subtype. To further inherit the constraint we
9173 -- need to use its own base to have an unconstrained type on which
9174 -- to apply the inherited constraint.
9176 if Ekind (Parent_Base) = E_Record_Subtype then
9177 Parent_Base := Base_Type (Parent_Base);
9178 end if;
9180 -- If the parent base is a private type and only its full view has
9181 -- discriminants, use the full view's base type.
9183 -- This can happen when we are deriving from a subtype of a derived type
9184 -- of a private type derived from a discriminated type with known
9185 -- discriminant:
9187 -- package Pkg;
9188 -- type Root_Type(I: Positive) is record
9189 -- ...
9190 -- end record;
9191 -- type Bounded_Root_Type is private;
9192 -- private
9193 -- type Bounded_Root_Type is new Root_Type(10);
9194 -- end Pkg;
9196 -- package Pkg2 is
9197 -- type Constrained_Root_Type is new Pkg.Bounded_Root_Type;
9198 -- end Pkg2;
9199 -- subtype Sub_Base is Pkg2.Constrained_Root_Type;
9200 -- type New_Der_Type is new Sub_Base;
9202 if Is_Private_Type (Parent_Base)
9203 and then Present (Full_View (Parent_Base))
9204 and then not Has_Discriminants (Parent_Base)
9205 and then Has_Discriminants (Full_View (Parent_Base))
9206 then
9207 Parent_Base := Base_Type (Full_View (Parent_Base));
9208 end if;
9210 -- AI05-0115: if this is a derivation from a private type in some
9211 -- other scope that may lead to invisible components for the derived
9212 -- type, mark it accordingly.
9214 if Is_Private_Type (Parent_Type) then
9215 if Scope (Parent_Base) = Scope (Derived_Type) then
9216 null;
9218 elsif In_Open_Scopes (Scope (Parent_Base))
9219 and then In_Private_Part (Scope (Parent_Base))
9220 then
9221 null;
9223 else
9224 Set_Has_Private_Ancestor (Derived_Type);
9225 end if;
9227 else
9228 Set_Has_Private_Ancestor
9229 (Derived_Type, Has_Private_Ancestor (Parent_Type));
9230 end if;
9232 -- Before we start the previously documented transformations, here is
9233 -- little fix for size and alignment of tagged types. Normally when we
9234 -- derive type D from type P, we copy the size and alignment of P as the
9235 -- default for D, and in the absence of explicit representation clauses
9236 -- for D, the size and alignment are indeed the same as the parent.
9238 -- But this is wrong for tagged types, since fields may be added, and
9239 -- the default size may need to be larger, and the default alignment may
9240 -- need to be larger.
9242 -- We therefore reset the size and alignment fields in the tagged case.
9243 -- Note that the size and alignment will in any case be at least as
9244 -- large as the parent type (since the derived type has a copy of the
9245 -- parent type in the _parent field)
9247 -- The type is also marked as being tagged here, which is needed when
9248 -- processing components with a self-referential anonymous access type
9249 -- in the call to Check_Anonymous_Access_Components below. Note that
9250 -- this flag is also set later on for completeness.
9252 if Is_Tagged then
9253 Set_Is_Tagged_Type (Derived_Type);
9254 Reinit_Size_Align (Derived_Type);
9255 end if;
9257 -- STEP 0a: figure out what kind of derived type declaration we have
9259 if Private_Extension then
9260 Type_Def := N;
9261 Mutate_Ekind (Derived_Type, E_Record_Type_With_Private);
9262 Set_Default_SSO (Derived_Type);
9263 Set_No_Reordering (Derived_Type, No_Component_Reordering);
9265 else
9266 Type_Def := Type_Definition (N);
9268 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
9269 -- Parent_Base can be a private type or private extension. However,
9270 -- for tagged types with an extension the newly added fields are
9271 -- visible and hence the Derived_Type is always an E_Record_Type.
9272 -- (except that the parent may have its own private fields).
9273 -- For untagged types we preserve the Ekind of the Parent_Base.
9275 if Present (Record_Extension_Part (Type_Def)) then
9276 Mutate_Ekind (Derived_Type, E_Record_Type);
9277 Set_Default_SSO (Derived_Type);
9278 Set_No_Reordering (Derived_Type, No_Component_Reordering);
9280 -- Create internal access types for components with anonymous
9281 -- access types.
9283 if Ada_Version >= Ada_2005 then
9284 Check_Anonymous_Access_Components
9285 (N, Derived_Type, Derived_Type,
9286 Component_List (Record_Extension_Part (Type_Def)));
9287 end if;
9289 else
9290 Mutate_Ekind (Derived_Type, Ekind (Parent_Base));
9291 end if;
9292 end if;
9294 -- Indic can either be an N_Identifier if the subtype indication
9295 -- contains no constraint or an N_Subtype_Indication if the subtype
9296 -- indication has a constraint. In either case it can include an
9297 -- interface list.
9299 Indic := Subtype_Indication (Type_Def);
9300 Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
9302 -- Check that the type has visible discriminants. The type may be
9303 -- a private type with unknown discriminants whose full view has
9304 -- discriminants which are invisible.
9306 if Constraint_Present then
9307 if not Has_Discriminants (Parent_Base)
9308 or else
9309 (Has_Unknown_Discriminants (Parent_Base)
9310 and then Is_Private_Type (Parent_Base))
9311 then
9312 Error_Msg_N
9313 ("invalid constraint: type has no discriminant",
9314 Constraint (Indic));
9316 Constraint_Present := False;
9317 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
9319 elsif Is_Constrained (Parent_Type) then
9320 Error_Msg_N
9321 ("invalid constraint: parent type is already constrained",
9322 Constraint (Indic));
9324 Constraint_Present := False;
9325 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
9326 end if;
9327 end if;
9329 -- STEP 0b: If needed, apply transformation given in point 5. above
9331 if not Private_Extension
9332 and then Has_Discriminants (Parent_Type)
9333 and then not Discriminant_Specs
9334 and then (Is_Constrained (Parent_Type) or else Constraint_Present)
9335 then
9336 -- First, we must analyze the constraint (see comment in point 5.)
9337 -- The constraint may come from the subtype indication of the full
9338 -- declaration. Temporarily set the state of the Derived_Type to
9339 -- "self-hidden" (see RM-8.3(17)).
9341 if Constraint_Present then
9342 pragma Assert (Is_Not_Self_Hidden (Derived_Type));
9343 Set_Is_Not_Self_Hidden (Derived_Type, False);
9344 New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
9345 Set_Is_Not_Self_Hidden (Derived_Type);
9347 -- If there is no explicit constraint, there might be one that is
9348 -- inherited from a constrained parent type. In that case verify that
9349 -- it conforms to the constraint in the partial view. In perverse
9350 -- cases the parent subtypes of the partial and full view can have
9351 -- different constraints.
9353 elsif Present (Stored_Constraint (Parent_Type)) then
9354 New_Discrs := Stored_Constraint (Parent_Type);
9356 else
9357 New_Discrs := No_Elist;
9358 end if;
9360 if Has_Discriminants (Derived_Type)
9361 and then Has_Private_Declaration (Derived_Type)
9362 and then Present (Discriminant_Constraint (Derived_Type))
9363 and then Present (New_Discrs)
9364 then
9365 -- Verify that constraints of the full view statically match
9366 -- those given in the partial view.
9368 declare
9369 C1, C2 : Elmt_Id;
9371 begin
9372 C1 := First_Elmt (New_Discrs);
9373 C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
9374 while Present (C1) and then Present (C2) loop
9375 if Fully_Conformant_Expressions (Node (C1), Node (C2))
9376 or else
9377 (Is_OK_Static_Expression (Node (C1))
9378 and then Is_OK_Static_Expression (Node (C2))
9379 and then
9380 Expr_Value (Node (C1)) = Expr_Value (Node (C2)))
9381 then
9382 null;
9384 else
9385 if Constraint_Present then
9386 Error_Msg_N
9387 ("constraint not conformant to previous declaration",
9388 Node (C1));
9389 else
9390 Error_Msg_N
9391 ("constraint of full view is incompatible "
9392 & "with partial view", N);
9393 end if;
9394 end if;
9396 Next_Elmt (C1);
9397 Next_Elmt (C2);
9398 end loop;
9399 end;
9400 end if;
9402 -- Insert and analyze the declaration for the unconstrained base type
9404 New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
9406 New_Decl :=
9407 Make_Full_Type_Declaration (Loc,
9408 Defining_Identifier => New_Base,
9409 Type_Definition =>
9410 Make_Derived_Type_Definition (Loc,
9411 Abstract_Present => Abstract_Present (Type_Def),
9412 Limited_Present => Limited_Present (Type_Def),
9413 Subtype_Indication =>
9414 New_Occurrence_Of (Parent_Base, Loc),
9415 Record_Extension_Part =>
9416 Relocate_Node (Record_Extension_Part (Type_Def)),
9417 Interface_List => Interface_List (Type_Def)));
9419 Set_Parent (New_Decl, Parent (N));
9420 Mark_Rewrite_Insertion (New_Decl);
9421 Insert_Before (N, New_Decl);
9423 -- In the extension case, make sure ancestor is frozen appropriately
9424 -- (see also non-discriminated case below).
9426 if Present (Record_Extension_Part (Type_Def))
9427 or else Is_Interface (Parent_Base)
9428 then
9429 Freeze_Before (New_Decl, Parent_Type);
9430 end if;
9432 -- Note that this call passes False for the Derive_Subps parameter
9433 -- because subprogram derivation is deferred until after creating
9434 -- the subtype (see below).
9436 Build_Derived_Type
9437 (New_Decl, Parent_Base, New_Base,
9438 Is_Completion => False, Derive_Subps => False);
9440 -- ??? This needs re-examination to determine whether the
9441 -- following call can simply be replaced by a call to Analyze.
9443 Set_Analyzed (New_Decl);
9445 -- Insert and analyze the declaration for the constrained subtype
9447 if Constraint_Present then
9448 New_Indic :=
9449 Make_Subtype_Indication (Loc,
9450 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
9451 Constraint => Relocate_Node (Constraint (Indic)));
9453 else
9454 declare
9455 Constr_List : constant List_Id := New_List;
9456 C : Elmt_Id;
9457 Expr : Node_Id;
9459 begin
9460 C := First_Elmt (Discriminant_Constraint (Parent_Type));
9461 while Present (C) loop
9462 Expr := Node (C);
9464 -- It is safe here to call New_Copy_Tree since we called
9465 -- Force_Evaluation on each constraint previously
9466 -- in Build_Discriminant_Constraints.
9468 Append (New_Copy_Tree (Expr), To => Constr_List);
9470 Next_Elmt (C);
9471 end loop;
9473 New_Indic :=
9474 Make_Subtype_Indication (Loc,
9475 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
9476 Constraint =>
9477 Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
9478 end;
9479 end if;
9481 Rewrite (N,
9482 Make_Subtype_Declaration (Loc,
9483 Defining_Identifier => Derived_Type,
9484 Subtype_Indication => New_Indic));
9486 -- Keep the aspects from the original node
9488 Move_Aspects (Original_Node (N), N);
9490 Analyze (N);
9492 -- Derivation of subprograms must be delayed until the full subtype
9493 -- has been established, to ensure proper overriding of subprograms
9494 -- inherited by full types. If the derivations occurred as part of
9495 -- the call to Build_Derived_Type above, then the check for type
9496 -- conformance would fail because earlier primitive subprograms
9497 -- could still refer to the full type prior the change to the new
9498 -- subtype and hence would not match the new base type created here.
9499 -- Subprograms are not derived, however, when Derive_Subps is False
9500 -- (since otherwise there could be redundant derivations).
9502 if Derive_Subps then
9503 Derive_Subprograms (Parent_Type, Derived_Type);
9504 end if;
9506 -- For tagged types the Discriminant_Constraint of the new base itype
9507 -- is inherited from the first subtype so that no subtype conformance
9508 -- problem arise when the first subtype overrides primitive
9509 -- operations inherited by the implicit base type.
9511 if Is_Tagged then
9512 Set_Discriminant_Constraint
9513 (New_Base, Discriminant_Constraint (Derived_Type));
9514 end if;
9516 return;
9517 end if;
9519 -- If we get here Derived_Type will have no discriminants or it will be
9520 -- a discriminated unconstrained base type.
9522 -- STEP 1a: perform preliminary actions/checks for derived tagged types
9524 if Is_Tagged then
9526 -- The parent type is frozen for non-private extensions (RM 13.14(7))
9527 -- The declaration of a specific descendant of an interface type
9528 -- freezes the interface type (RM 13.14).
9530 if not Private_Extension or else Is_Interface (Parent_Base) then
9531 Freeze_Before (N, Parent_Type);
9532 end if;
9534 if Ada_Version >= Ada_2005 then
9535 Check_Generic_Ancestors;
9537 elsif Type_Access_Level (Derived_Type) /=
9538 Type_Access_Level (Parent_Type)
9539 and then not Is_Generic_Type (Derived_Type)
9540 then
9541 if Is_Controlled (Parent_Type) then
9542 Error_Msg_N
9543 ("controlled type must be declared at the library level",
9544 Indic);
9545 else
9546 Error_Msg_N
9547 ("type extension at deeper accessibility level than parent",
9548 Indic);
9549 end if;
9551 else
9552 declare
9553 GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
9554 begin
9555 if Present (GB)
9556 and then GB /= Enclosing_Generic_Body (Parent_Base)
9557 then
9558 Error_Msg_NE
9559 ("parent type of& must not be outside generic body"
9560 & " (RM 3.9.1(4))",
9561 Indic, Derived_Type);
9562 end if;
9563 end;
9564 end if;
9565 end if;
9567 -- Ada 2005 (AI-251)
9569 if Ada_Version >= Ada_2005 and then Is_Tagged then
9571 -- "The declaration of a specific descendant of an interface type
9572 -- freezes the interface type" (RM 13.14).
9574 declare
9575 Iface : Node_Id;
9576 begin
9577 Iface := First (Interface_List (Type_Def));
9578 while Present (Iface) loop
9579 Freeze_Before (N, Etype (Iface));
9580 Next (Iface);
9581 end loop;
9582 end;
9583 end if;
9585 -- STEP 1b : preliminary cleanup of the full view of private types
9587 -- If the type is already marked as having discriminants, then it's the
9588 -- completion of a private type or private extension and we need to
9589 -- retain the discriminants from the partial view if the current
9590 -- declaration has Discriminant_Specifications so that we can verify
9591 -- conformance. However, we must remove any existing components that
9592 -- were inherited from the parent (and attached in Copy_And_Swap)
9593 -- because the full type inherits all appropriate components anyway, and
9594 -- we do not want the partial view's components interfering.
9596 if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
9597 Discrim := First_Discriminant (Derived_Type);
9598 loop
9599 Last_Discrim := Discrim;
9600 Next_Discriminant (Discrim);
9601 exit when No (Discrim);
9602 end loop;
9604 Set_Last_Entity (Derived_Type, Last_Discrim);
9606 -- In all other cases wipe out the list of inherited components (even
9607 -- inherited discriminants), it will be properly rebuilt here.
9609 else
9610 Set_First_Entity (Derived_Type, Empty);
9611 Set_Last_Entity (Derived_Type, Empty);
9612 end if;
9614 -- STEP 1c: Initialize some flags for the Derived_Type
9616 -- The following flags must be initialized here so that
9617 -- Process_Discriminants can check that discriminants of tagged types do
9618 -- not have a default initial value and that access discriminants are
9619 -- only specified for limited records. For completeness, these flags are
9620 -- also initialized along with all the other flags below.
9622 -- AI-419: Limitedness is not inherited from an interface parent, so to
9623 -- be limited in that case the type must be explicitly declared as
9624 -- limited, or synchronized. While task and protected interfaces are
9625 -- always limited, a synchronized private extension might not inherit
9626 -- from such interfaces, and so we also need to recognize the
9627 -- explicit limitedness implied by a synchronized private extension
9628 -- that does not derive from a synchronized interface (see RM-7.3(6/2)).
9630 if Limited_Present (Type_Def)
9631 or else Synchronized_Present (Type_Def)
9632 then
9633 Set_Is_Limited_Record (Derived_Type);
9635 elsif Is_Limited_Record (Parent_Type)
9636 or else (Present (Full_View (Parent_Type))
9637 and then Is_Limited_Record (Full_View (Parent_Type)))
9638 then
9639 if not Is_Interface (Parent_Type)
9640 or else Is_Concurrent_Interface (Parent_Type)
9641 then
9642 Set_Is_Limited_Record (Derived_Type);
9643 end if;
9644 end if;
9646 -- STEP 2a: process discriminants of derived type if any
9648 Push_Scope (Derived_Type);
9650 if Discriminant_Specs then
9651 Set_Has_Unknown_Discriminants (Derived_Type, False);
9653 -- The following call to Check_Or_Process_Discriminants initializes
9654 -- fields Has_Discriminants and Discriminant_Constraint, unless we
9655 -- are processing the completion of a private type declaration.
9656 -- Temporarily set the state of the Derived_Type to "self-hidden"
9657 -- (see RM-8.3(17)), unless it is already the case.
9659 if Is_Not_Self_Hidden (Derived_Type) then
9660 Set_Is_Not_Self_Hidden (Derived_Type, False);
9661 Check_Or_Process_Discriminants (N, Derived_Type);
9662 Set_Is_Not_Self_Hidden (Derived_Type);
9663 else
9664 Check_Or_Process_Discriminants (N, Derived_Type);
9665 end if;
9667 -- For untagged types, the constraint on the Parent_Type must be
9668 -- present and is used to rename the discriminants.
9670 if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
9671 Error_Msg_N ("untagged parent must have discriminants", Indic);
9673 elsif not Is_Tagged and then not Constraint_Present then
9674 Error_Msg_N
9675 ("discriminant constraint needed for derived untagged records",
9676 Indic);
9678 -- Otherwise the parent subtype must be constrained unless we have a
9679 -- private extension.
9681 elsif not Constraint_Present
9682 and then not Private_Extension
9683 and then not Is_Constrained (Parent_Type)
9684 then
9685 Error_Msg_N
9686 ("unconstrained type not allowed in this context", Indic);
9688 elsif Constraint_Present then
9689 -- The following call sets the field Corresponding_Discriminant
9690 -- for the discriminants in the Derived_Type.
9692 Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
9694 -- For untagged types all new discriminants must rename
9695 -- discriminants in the parent. For private extensions new
9696 -- discriminants cannot rename old ones (implied by [7.3(13)]).
9698 Discrim := First_Discriminant (Derived_Type);
9699 while Present (Discrim) loop
9700 if not Is_Tagged
9701 and then No (Corresponding_Discriminant (Discrim))
9702 then
9703 Error_Msg_N
9704 ("new discriminants must constrain old ones", Discrim);
9706 elsif Private_Extension
9707 and then Present (Corresponding_Discriminant (Discrim))
9708 then
9709 Error_Msg_N
9710 ("only static constraints allowed for parent"
9711 & " discriminants in the partial view", Indic);
9712 exit;
9713 end if;
9715 -- If a new discriminant is used in the constraint, then its
9716 -- subtype must be statically compatible with the subtype of
9717 -- the parent discriminant (RM 3.7(15)).
9719 if Present (Corresponding_Discriminant (Discrim)) then
9720 Check_Constraining_Discriminant
9721 (Discrim, Corresponding_Discriminant (Discrim));
9722 end if;
9724 Next_Discriminant (Discrim);
9725 end loop;
9727 -- Check whether the constraints of the full view statically
9728 -- match those imposed by the parent subtype [7.3(13)].
9730 if Present (Stored_Constraint (Derived_Type)) then
9731 declare
9732 C1, C2 : Elmt_Id;
9734 begin
9735 C1 := First_Elmt (Discs);
9736 C2 := First_Elmt (Stored_Constraint (Derived_Type));
9737 while Present (C1) and then Present (C2) loop
9738 if not
9739 Fully_Conformant_Expressions (Node (C1), Node (C2))
9740 then
9741 Error_Msg_N
9742 ("not conformant with previous declaration",
9743 Node (C1));
9744 end if;
9746 Next_Elmt (C1);
9747 Next_Elmt (C2);
9748 end loop;
9749 end;
9750 end if;
9751 end if;
9753 -- STEP 2b: No new discriminants, inherit discriminants if any
9755 else
9756 if Private_Extension then
9757 Set_Has_Unknown_Discriminants
9758 (Derived_Type,
9759 Has_Unknown_Discriminants (Parent_Type)
9760 or else Unknown_Discriminants_Present (N));
9762 -- The partial view of the parent may have unknown discriminants,
9763 -- but if the full view has discriminants and the parent type is
9764 -- in scope they must be inherited.
9766 elsif Has_Unknown_Discriminants (Parent_Type)
9767 and then
9768 (not Has_Discriminants (Parent_Type)
9769 or else not In_Open_Scopes (Scope (Parent_Base)))
9770 then
9771 Set_Has_Unknown_Discriminants (Derived_Type);
9772 end if;
9774 if not Has_Unknown_Discriminants (Derived_Type)
9775 and then not Has_Unknown_Discriminants (Parent_Base)
9776 and then Has_Discriminants (Parent_Type)
9777 then
9778 Inherit_Discrims := True;
9779 Set_Has_Discriminants
9780 (Derived_Type, True);
9781 Set_Discriminant_Constraint
9782 (Derived_Type, Discriminant_Constraint (Parent_Base));
9783 end if;
9785 -- The following test is true for private types (remember
9786 -- transformation 5. is not applied to those) and in an error
9787 -- situation.
9789 if Constraint_Present then
9790 Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
9791 end if;
9793 -- For now mark a new derived type as constrained only if it has no
9794 -- discriminants. At the end of Build_Derived_Record_Type we properly
9795 -- set this flag in the case of private extensions. See comments in
9796 -- point 9. just before body of Build_Derived_Record_Type.
9798 Set_Is_Constrained
9799 (Derived_Type,
9800 not (Inherit_Discrims
9801 or else Has_Unknown_Discriminants (Derived_Type)));
9802 end if;
9804 -- STEP 3: initialize fields of derived type
9806 Set_Is_Tagged_Type (Derived_Type, Is_Tagged);
9807 Set_Stored_Constraint (Derived_Type, No_Elist);
9809 -- Ada 2005 (AI-251): Private type-declarations can implement interfaces
9810 -- but cannot be interfaces
9812 if not Private_Extension
9813 and then Ekind (Derived_Type) /= E_Private_Type
9814 and then Ekind (Derived_Type) /= E_Limited_Private_Type
9815 then
9816 if Interface_Present (Type_Def) then
9817 Analyze_Interface_Declaration (Derived_Type, Type_Def);
9818 end if;
9820 Set_Interfaces (Derived_Type, No_Elist);
9821 end if;
9823 -- Fields inherited from the Parent_Type
9825 Set_Has_Specified_Layout
9826 (Derived_Type, Has_Specified_Layout (Parent_Type));
9827 Set_Is_Limited_Composite
9828 (Derived_Type, Is_Limited_Composite (Parent_Type));
9829 Set_Is_Private_Composite
9830 (Derived_Type, Is_Private_Composite (Parent_Type));
9832 if Is_Tagged_Type (Parent_Type) then
9833 Set_No_Tagged_Streams_Pragma
9834 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
9835 end if;
9837 -- Fields inherited from the Parent_Base
9839 Propagate_Concurrent_Flags (Derived_Type, Parent_Base);
9840 Propagate_Controlled_Flags (Derived_Type, Parent_Base, Deriv => True);
9842 Set_Has_Non_Standard_Rep
9843 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
9844 Set_Has_Primitive_Operations
9845 (Derived_Type, Has_Primitive_Operations (Parent_Base));
9847 -- Set fields for private derived types
9849 if Is_Private_Type (Derived_Type) then
9850 Set_Depends_On_Private (Derived_Type, True);
9851 Set_Private_Dependents (Derived_Type, New_Elmt_List);
9852 end if;
9854 -- Inherit fields for non-private types. If this is the completion of a
9855 -- derivation from a private type, the parent itself is private and the
9856 -- attributes come from its full view, which must be present.
9858 if Is_Record_Type (Derived_Type) then
9859 declare
9860 Parent_Full : Entity_Id;
9862 begin
9863 if Is_Private_Type (Parent_Base)
9864 and then not Is_Record_Type (Parent_Base)
9865 then
9866 Parent_Full := Full_View (Parent_Base);
9867 else
9868 Parent_Full := Parent_Base;
9869 end if;
9871 Set_Component_Alignment
9872 (Derived_Type, Component_Alignment (Parent_Full));
9873 Set_C_Pass_By_Copy
9874 (Derived_Type, C_Pass_By_Copy (Parent_Full));
9875 Set_Has_Complex_Representation
9876 (Derived_Type, Has_Complex_Representation (Parent_Full));
9878 -- For untagged types, inherit the layout by default to avoid
9879 -- costly changes of representation for type conversions.
9881 if not Is_Tagged then
9882 Set_Is_Packed (Derived_Type, Is_Packed (Parent_Full));
9883 Set_No_Reordering (Derived_Type, No_Reordering (Parent_Full));
9884 end if;
9885 end;
9886 end if;
9888 -- Initialize the list of primitive operations to an empty list,
9889 -- to cover tagged types as well as untagged types. For untagged
9890 -- types this is used either to analyze the call as legal when
9891 -- GNAT extensions are allowed, or to give better error messages.
9893 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
9895 -- Set fields for tagged types
9897 if Is_Tagged then
9898 -- Minor optimization: there is no need to generate the class-wide
9899 -- entity associated with an underlying record view.
9901 if not Is_Underlying_Record_View (Derived_Type) then
9902 Make_Class_Wide_Type (Derived_Type);
9903 end if;
9905 Set_Is_Abstract_Type (Derived_Type, Abstract_Present (Type_Def));
9907 if Has_Discriminants (Derived_Type)
9908 and then Constraint_Present
9909 then
9910 Set_Stored_Constraint
9911 (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
9912 end if;
9914 if Ada_Version >= Ada_2005 then
9915 declare
9916 Ifaces_List : Elist_Id;
9918 begin
9919 -- Checks rules 3.9.4 (13/2 and 14/2)
9921 if Comes_From_Source (Derived_Type)
9922 and then not Is_Private_Type (Derived_Type)
9923 and then Is_Interface (Parent_Type)
9924 and then not Is_Interface (Derived_Type)
9925 then
9926 if Is_Task_Interface (Parent_Type) then
9927 Error_Msg_N
9928 ("(Ada 2005) task type required (RM 3.9.4 (13.2))",
9929 Derived_Type);
9931 elsif Is_Protected_Interface (Parent_Type) then
9932 Error_Msg_N
9933 ("(Ada 2005) protected type required (RM 3.9.4 (14.2))",
9934 Derived_Type);
9935 end if;
9936 end if;
9938 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
9940 Check_Interfaces (N, Type_Def);
9942 -- Ada 2005 (AI-251): Collect the list of progenitors that are
9943 -- not already in the parents.
9945 Collect_Interfaces
9946 (T => Derived_Type,
9947 Ifaces_List => Ifaces_List,
9948 Exclude_Parents => True);
9950 Set_Interfaces (Derived_Type, Ifaces_List);
9952 -- If the derived type is the anonymous type created for
9953 -- a declaration whose parent has a constraint, propagate
9954 -- the interface list to the source type. This must be done
9955 -- prior to the completion of the analysis of the source type
9956 -- because the components in the extension may contain current
9957 -- instances whose legality depends on some ancestor.
9959 if Is_Itype (Derived_Type) then
9960 declare
9961 Def : constant Node_Id :=
9962 Associated_Node_For_Itype (Derived_Type);
9963 begin
9964 if Present (Def)
9965 and then Nkind (Def) = N_Full_Type_Declaration
9966 then
9967 Set_Interfaces
9968 (Defining_Identifier (Def), Ifaces_List);
9969 end if;
9970 end;
9971 end if;
9973 -- A type extension is automatically Ghost when one of its
9974 -- progenitors is Ghost (SPARK RM 6.9(9)). This property is
9975 -- also inherited when the parent type is Ghost, but this is
9976 -- done in Build_Derived_Type as the mechanism also handles
9977 -- untagged derivations.
9979 if Implements_Ghost_Interface (Derived_Type) then
9980 Set_Is_Ghost_Entity (Derived_Type);
9981 end if;
9982 end;
9983 end if;
9984 end if;
9986 -- STEP 4: Inherit components from the parent base and constrain them.
9987 -- Apply the second transformation described in point 6. above.
9989 if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
9990 or else not Has_Discriminants (Parent_Type)
9991 or else not Is_Constrained (Parent_Type)
9992 then
9993 Constrs := Discs;
9994 else
9995 Constrs := Discriminant_Constraint (Parent_Type);
9996 end if;
9998 Assoc_List :=
9999 Inherit_Components
10000 (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
10002 -- STEP 5a: Copy the parent record declaration for untagged types
10004 Set_Has_Implicit_Dereference
10005 (Derived_Type, Has_Implicit_Dereference (Parent_Type));
10007 if not Is_Tagged then
10009 -- Discriminant_Constraint (Derived_Type) has been properly
10010 -- constructed. Save it and temporarily set it to Empty because we
10011 -- do not want the call to New_Copy_Tree below to mess this list.
10013 if Has_Discriminants (Derived_Type) then
10014 Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
10015 Set_Discriminant_Constraint (Derived_Type, No_Elist);
10016 else
10017 Save_Discr_Constr := No_Elist;
10018 end if;
10020 -- Save the Etype field of Derived_Type. It is correctly set now,
10021 -- but the call to New_Copy tree may remap it to point to itself,
10022 -- which is not what we want. Ditto for the Next_Entity field.
10024 Save_Etype := Etype (Derived_Type);
10025 Save_Next_Entity := Next_Entity (Derived_Type);
10027 -- Assoc_List maps all stored discriminants in the Parent_Base to
10028 -- stored discriminants in the Derived_Type. It is fundamental that
10029 -- no types or itypes with discriminants other than the stored
10030 -- discriminants appear in the entities declared inside
10031 -- Derived_Type, since the back end cannot deal with it.
10033 New_Decl :=
10034 New_Copy_Tree
10035 (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
10036 Copy_Dimensions_Of_Components (Derived_Type);
10038 -- Restore the fields saved prior to the New_Copy_Tree call
10039 -- and compute the stored constraint.
10041 Set_Etype (Derived_Type, Save_Etype);
10042 Link_Entities (Derived_Type, Save_Next_Entity);
10044 if Has_Discriminants (Derived_Type) then
10045 Set_Discriminant_Constraint
10046 (Derived_Type, Save_Discr_Constr);
10047 Set_Stored_Constraint
10048 (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
10050 Replace_Discriminants (Derived_Type, New_Decl);
10051 end if;
10053 -- Relocate the aspects from the original type
10055 Remove_Aspects (New_Decl);
10056 Move_Aspects (N, New_Decl);
10058 -- Insert the new derived type declaration
10060 Rewrite (N, New_Decl);
10062 -- STEP 5b: Complete the processing for record extensions in generics
10064 -- There is no completion for record extensions declared in the
10065 -- parameter part of a generic, so we need to complete processing for
10066 -- these generic record extensions here. Record_Type_Definition will
10067 -- set the Is_Not_Self_Hidden flag.
10069 elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
10070 Record_Type_Definition (Empty, Derived_Type);
10072 -- STEP 5c: Process the record extension for non private tagged types
10074 elsif not Private_Extension then
10075 Expand_Record_Extension (Derived_Type, Type_Def);
10077 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
10078 -- implemented interfaces if we are in expansion mode
10080 if Expander_Active
10081 and then Has_Interfaces (Derived_Type)
10082 then
10083 Add_Interface_Tag_Components (N, Derived_Type);
10084 end if;
10086 -- Analyze the record extension
10088 Record_Type_Definition
10089 (Record_Extension_Part (Type_Def), Derived_Type);
10090 end if;
10092 End_Scope;
10094 -- Nothing else to do if there is an error in the derivation.
10095 -- An unusual case: the full view may be derived from a type in an
10096 -- instance, when the partial view was used illegally as an actual
10097 -- in that instance, leading to a circular definition.
10099 if Etype (Derived_Type) = Any_Type
10100 or else Etype (Parent_Type) = Derived_Type
10101 then
10102 return;
10103 end if;
10105 -- Set delayed freeze and then derive subprograms, we need to do
10106 -- this in this order so that derived subprograms inherit the
10107 -- derived freeze if necessary.
10109 Set_Has_Delayed_Freeze (Derived_Type);
10111 if Derive_Subps then
10112 Derive_Subprograms (Parent_Type, Derived_Type);
10113 end if;
10115 -- If we have a private extension which defines a constrained derived
10116 -- type mark as constrained here after we have derived subprograms. See
10117 -- comment on point 9. just above the body of Build_Derived_Record_Type.
10119 if Private_Extension and then Inherit_Discrims then
10120 if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
10121 Set_Is_Constrained (Derived_Type, True);
10122 Set_Discriminant_Constraint (Derived_Type, Discs);
10124 elsif Is_Constrained (Parent_Type) then
10125 Set_Is_Constrained
10126 (Derived_Type, True);
10127 Set_Discriminant_Constraint
10128 (Derived_Type, Discriminant_Constraint (Parent_Type));
10129 end if;
10130 end if;
10132 -- Update the class-wide type, which shares the now-completed entity
10133 -- list with its specific type. In case of underlying record views,
10134 -- we do not generate the corresponding class wide entity.
10136 if Is_Tagged
10137 and then not Is_Underlying_Record_View (Derived_Type)
10138 then
10139 Set_First_Entity
10140 (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
10141 Set_Last_Entity
10142 (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
10143 end if;
10145 Check_Function_Writable_Actuals (N);
10146 end Build_Derived_Record_Type;
10148 ------------------------
10149 -- Build_Derived_Type --
10150 ------------------------
10152 procedure Build_Derived_Type
10153 (N : Node_Id;
10154 Parent_Type : Entity_Id;
10155 Derived_Type : Entity_Id;
10156 Is_Completion : Boolean;
10157 Derive_Subps : Boolean := True)
10159 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
10161 begin
10162 -- Set common attributes
10164 if Ekind (Derived_Type) in Incomplete_Or_Private_Kind
10165 and then Ekind (Parent_Base) in Elementary_Kind
10166 then
10167 Reinit_Field_To_Zero (Derived_Type, F_Discriminant_Constraint);
10168 end if;
10170 Set_Scope (Derived_Type, Current_Scope);
10171 Set_Etype (Derived_Type, Parent_Base);
10172 Mutate_Ekind (Derived_Type, Ekind (Parent_Base));
10174 Propagate_Concurrent_Flags (Derived_Type, Parent_Base);
10175 Propagate_Controlled_Flags (Derived_Type, Parent_Base, Deriv => True);
10177 Set_Size_Info (Derived_Type, Parent_Type);
10178 Copy_RM_Size (To => Derived_Type, From => Parent_Type);
10180 Set_Is_Tagged_Type (Derived_Type, Is_Tagged_Type (Parent_Type));
10181 Set_Is_Volatile (Derived_Type, Is_Volatile (Parent_Type));
10183 if Is_Tagged_Type (Derived_Type) then
10184 Set_No_Tagged_Streams_Pragma
10185 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
10186 end if;
10188 -- If the parent has primitive routines and may have not-seen-yet aspect
10189 -- specifications (e.g., a Pack pragma), then set the derived type link
10190 -- in order to later diagnose "early derivation" issues. If in different
10191 -- compilation units, then "early derivation" cannot be an issue (and we
10192 -- don't like interunit references that go in the opposite direction of
10193 -- semantic dependencies).
10195 if Has_Primitive_Operations (Parent_Type)
10196 and then Enclosing_Comp_Unit_Node (Parent_Type) =
10197 Enclosing_Comp_Unit_Node (Derived_Type)
10198 then
10199 Set_Derived_Type_Link (Parent_Base, Derived_Type);
10200 end if;
10202 -- If the parent type is a private subtype, the convention on the base
10203 -- type may be set in the private part, and not propagated to the
10204 -- subtype until later, so we obtain the convention from the base type.
10206 Set_Convention (Derived_Type, Convention (Parent_Base));
10208 if Is_Tagged_Type (Derived_Type)
10209 and then Present (Class_Wide_Type (Derived_Type))
10210 then
10211 Set_Convention (Class_Wide_Type (Derived_Type),
10212 Convention (Class_Wide_Type (Parent_Base)));
10213 end if;
10215 -- Set SSO default for record or array type
10217 if (Is_Array_Type (Derived_Type) or else Is_Record_Type (Derived_Type))
10218 and then Is_Base_Type (Derived_Type)
10219 then
10220 Set_Default_SSO (Derived_Type);
10221 end if;
10223 -- A derived type inherits the Default_Initial_Condition pragma coming
10224 -- from any parent type within the derivation chain.
10226 if Has_DIC (Parent_Type) then
10227 Set_Has_Inherited_DIC (Derived_Type);
10228 end if;
10230 -- A derived type inherits any class-wide invariants coming from a
10231 -- parent type or an interface. Note that the invariant procedure of
10232 -- the parent type should not be inherited because the derived type may
10233 -- define invariants of its own.
10235 if not Is_Interface (Derived_Type) then
10236 if Has_Inherited_Invariants (Parent_Type)
10237 or else Has_Inheritable_Invariants (Parent_Type)
10238 then
10239 Set_Has_Inherited_Invariants (Derived_Type);
10241 elsif Is_Concurrent_Type (Derived_Type)
10242 or else Is_Tagged_Type (Derived_Type)
10243 then
10244 declare
10245 Iface : Entity_Id;
10246 Ifaces : Elist_Id;
10247 Iface_Elmt : Elmt_Id;
10249 begin
10250 Collect_Interfaces
10251 (T => Derived_Type,
10252 Ifaces_List => Ifaces,
10253 Exclude_Parents => True);
10255 if Present (Ifaces) then
10256 Iface_Elmt := First_Elmt (Ifaces);
10257 while Present (Iface_Elmt) loop
10258 Iface := Node (Iface_Elmt);
10260 if Has_Inheritable_Invariants (Iface) then
10261 Set_Has_Inherited_Invariants (Derived_Type);
10262 exit;
10263 end if;
10265 Next_Elmt (Iface_Elmt);
10266 end loop;
10267 end if;
10268 end;
10269 end if;
10270 end if;
10272 -- We similarly inherit predicates
10274 Inherit_Predicate_Flags (Derived_Type, Parent_Type, Only_Flags => True);
10276 -- The derived type inherits representation clauses from the parent
10277 -- type, and from any interfaces.
10279 Inherit_Rep_Item_Chain (Derived_Type, Parent_Type);
10281 declare
10282 Iface : Node_Id := First (Abstract_Interface_List (Derived_Type));
10283 begin
10284 while Present (Iface) loop
10285 Inherit_Rep_Item_Chain (Derived_Type, Entity (Iface));
10286 Next (Iface);
10287 end loop;
10288 end;
10290 -- If the parent type has delayed rep aspects, then mark the derived
10291 -- type as possibly inheriting a delayed rep aspect.
10293 if Has_Delayed_Rep_Aspects (Parent_Type) then
10294 Set_May_Inherit_Delayed_Rep_Aspects (Derived_Type);
10295 end if;
10297 -- A derived type becomes Ghost when its parent type is also Ghost
10298 -- (SPARK RM 6.9(9)). Note that the Ghost-related attributes are not
10299 -- directly inherited because the Ghost policy in effect may differ.
10301 if Is_Ghost_Entity (Parent_Type) then
10302 Set_Is_Ghost_Entity (Derived_Type);
10303 end if;
10305 -- Type dependent processing
10307 case Ekind (Parent_Type) is
10308 when Numeric_Kind =>
10309 Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
10311 when Array_Kind =>
10312 Build_Derived_Array_Type (N, Parent_Type, Derived_Type);
10314 when Class_Wide_Kind
10315 | E_Record_Subtype
10316 | E_Record_Type
10318 Build_Derived_Record_Type
10319 (N, Parent_Type, Derived_Type, Derive_Subps);
10320 return;
10322 when Enumeration_Kind =>
10323 Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
10325 when Access_Kind =>
10326 Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
10328 when Incomplete_Or_Private_Kind =>
10329 Build_Derived_Private_Type
10330 (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
10332 -- For discriminated types, the derivation includes deriving
10333 -- primitive operations. For others it is done below.
10335 if Is_Tagged_Type (Parent_Type)
10336 or else Has_Discriminants (Parent_Type)
10337 or else (Present (Full_View (Parent_Type))
10338 and then Has_Discriminants (Full_View (Parent_Type)))
10339 then
10340 return;
10341 end if;
10343 when Concurrent_Kind =>
10344 Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
10346 when others =>
10347 raise Program_Error;
10348 end case;
10350 -- Nothing more to do if some error occurred
10352 if Etype (Derived_Type) = Any_Type then
10353 return;
10354 end if;
10356 -- If not already set, initialize the derived type's list of primitive
10357 -- operations to an empty element list.
10359 if No (Direct_Primitive_Operations (Derived_Type)) then
10360 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
10362 -- If Etype of the derived type is the base type (as opposed to
10363 -- a parent type) and doesn't have an associated list of primitive
10364 -- operations, then set the base type's primitive list to the
10365 -- derived type's list. The lists need to be shared in common
10366 -- between the two.
10368 if Etype (Derived_Type) = Base_Type (Derived_Type)
10369 and then No (Direct_Primitive_Operations (Etype (Derived_Type)))
10370 then
10371 Set_Direct_Primitive_Operations
10372 (Etype (Derived_Type),
10373 Direct_Primitive_Operations (Derived_Type));
10374 end if;
10375 end if;
10377 -- Set delayed freeze and then derive subprograms, we need to do this
10378 -- in this order so that derived subprograms inherit the derived freeze
10379 -- if necessary.
10381 Set_Has_Delayed_Freeze (Derived_Type);
10383 if Derive_Subps then
10384 Derive_Subprograms (Parent_Type, Derived_Type);
10385 end if;
10387 Set_Has_Primitive_Operations
10388 (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
10389 end Build_Derived_Type;
10391 -----------------------
10392 -- Build_Discriminal --
10393 -----------------------
10395 procedure Build_Discriminal (Discrim : Entity_Id) is
10396 D_Minal : Entity_Id;
10397 CR_Disc : Entity_Id;
10399 begin
10400 -- A discriminal has the same name as the discriminant
10402 D_Minal := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
10404 Mutate_Ekind (D_Minal, E_In_Parameter);
10405 Set_Mechanism (D_Minal, Default_Mechanism);
10406 Set_Etype (D_Minal, Etype (Discrim));
10407 Set_Scope (D_Minal, Current_Scope);
10408 Set_Parent (D_Minal, Parent (Discrim));
10410 Set_Discriminal (Discrim, D_Minal);
10411 Set_Discriminal_Link (D_Minal, Discrim);
10413 -- For task types, build at once the discriminants of the corresponding
10414 -- record, which are needed if discriminants are used in entry defaults
10415 -- and in family bounds.
10417 if Is_Concurrent_Type (Current_Scope)
10418 or else
10419 Is_Limited_Type (Current_Scope)
10420 then
10421 CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
10423 Mutate_Ekind (CR_Disc, E_In_Parameter);
10424 Set_Mechanism (CR_Disc, Default_Mechanism);
10425 Set_Etype (CR_Disc, Etype (Discrim));
10426 Set_Scope (CR_Disc, Current_Scope);
10427 Set_Discriminal_Link (CR_Disc, Discrim);
10428 Set_CR_Discriminant (Discrim, CR_Disc);
10429 end if;
10430 end Build_Discriminal;
10432 ------------------------------------
10433 -- Build_Discriminant_Constraints --
10434 ------------------------------------
10436 function Build_Discriminant_Constraints
10437 (T : Entity_Id;
10438 Def : Node_Id;
10439 Derived_Def : Boolean := False) return Elist_Id
10441 C : constant Node_Id := Constraint (Def);
10442 Nb_Discr : constant Nat := Number_Discriminants (T);
10444 Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
10445 -- Saves the expression corresponding to a given discriminant in T
10447 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
10448 -- Return the Position number within array Discr_Expr of a discriminant
10449 -- D within the discriminant list of the discriminated type T.
10451 procedure Process_Discriminant_Expression
10452 (Expr : Node_Id;
10453 D : Entity_Id);
10454 -- If this is a discriminant constraint on a partial view, do not
10455 -- generate an overflow check on the discriminant expression. The check
10456 -- will be generated when constraining the full view. Otherwise the
10457 -- backend creates duplicate symbols for the temporaries corresponding
10458 -- to the expressions to be checked, causing spurious assembler errors.
10460 ------------------
10461 -- Pos_Of_Discr --
10462 ------------------
10464 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
10465 Disc : Entity_Id;
10467 begin
10468 Disc := First_Discriminant (T);
10469 for J in Discr_Expr'Range loop
10470 if Disc = D then
10471 return J;
10472 end if;
10474 Next_Discriminant (Disc);
10475 end loop;
10477 -- Note: Since this function is called on discriminants that are
10478 -- known to belong to the discriminated type, falling through the
10479 -- loop with no match signals an internal compiler error.
10481 raise Program_Error;
10482 end Pos_Of_Discr;
10484 -------------------------------------
10485 -- Process_Discriminant_Expression --
10486 -------------------------------------
10488 procedure Process_Discriminant_Expression
10489 (Expr : Node_Id;
10490 D : Entity_Id)
10492 BDT : constant Entity_Id := Base_Type (Etype (D));
10494 begin
10495 -- If this is a discriminant constraint on a partial view, do
10496 -- not generate an overflow on the discriminant expression. The
10497 -- check will be generated when constraining the full view.
10499 if Is_Private_Type (T)
10500 and then Present (Full_View (T))
10501 then
10502 Analyze_And_Resolve (Expr, BDT, Suppress => Overflow_Check);
10503 else
10504 Analyze_And_Resolve (Expr, BDT);
10505 end if;
10506 end Process_Discriminant_Expression;
10508 -- Declarations local to Build_Discriminant_Constraints
10510 Discr : Entity_Id;
10511 E : Entity_Id;
10512 Elist : constant Elist_Id := New_Elmt_List;
10514 Constr : Node_Id;
10515 Expr : Node_Id;
10516 Id : Node_Id;
10517 Position : Nat;
10518 Found : Boolean;
10520 Discrim_Present : Boolean := False;
10522 -- Start of processing for Build_Discriminant_Constraints
10524 begin
10525 -- The following loop will process positional associations only.
10526 -- For a positional association, the (single) discriminant is
10527 -- implicitly specified by position, in textual order (RM 3.7.2).
10529 Discr := First_Discriminant (T);
10530 Constr := First (Constraints (C));
10531 for D in Discr_Expr'Range loop
10532 exit when Nkind (Constr) = N_Discriminant_Association;
10534 if No (Constr) then
10535 Error_Msg_N ("too few discriminants given in constraint", C);
10536 return New_Elmt_List;
10538 elsif Nkind (Constr) = N_Range
10539 or else (Nkind (Constr) = N_Attribute_Reference
10540 and then Attribute_Name (Constr) = Name_Range)
10541 then
10542 Error_Msg_N
10543 ("a range is not a valid discriminant constraint", Constr);
10544 Discr_Expr (D) := Error;
10546 elsif Nkind (Constr) = N_Subtype_Indication then
10547 Error_Msg_N
10548 ("a subtype indication is not a valid discriminant constraint",
10549 Constr);
10550 Discr_Expr (D) := Error;
10552 else
10553 Process_Discriminant_Expression (Constr, Discr);
10554 Discr_Expr (D) := Constr;
10555 end if;
10557 Next_Discriminant (Discr);
10558 Next (Constr);
10559 end loop;
10561 if No (Discr) and then Present (Constr) then
10562 Error_Msg_N ("too many discriminants given in constraint", Constr);
10563 return New_Elmt_List;
10564 end if;
10566 -- Named associations can be given in any order, but if both positional
10567 -- and named associations are used in the same discriminant constraint,
10568 -- then positional associations must occur first, at their normal
10569 -- position. Hence once a named association is used, the rest of the
10570 -- discriminant constraint must use only named associations.
10572 while Present (Constr) loop
10574 -- Positional association forbidden after a named association
10576 if Nkind (Constr) /= N_Discriminant_Association then
10577 Error_Msg_N ("positional association follows named one", Constr);
10578 return New_Elmt_List;
10580 -- Otherwise it is a named association
10582 else
10583 -- E records the type of the discriminants in the named
10584 -- association. All the discriminants specified in the same name
10585 -- association must have the same type.
10587 E := Empty;
10589 -- Search the list of discriminants in T to see if the simple name
10590 -- given in the constraint matches any of them.
10592 Id := First (Selector_Names (Constr));
10593 while Present (Id) loop
10594 Found := False;
10596 -- If Original_Discriminant is present, we are processing a
10597 -- generic instantiation and this is an instance node. We need
10598 -- to find the name of the corresponding discriminant in the
10599 -- actual record type T and not the name of the discriminant in
10600 -- the generic formal. Example:
10602 -- generic
10603 -- type G (D : int) is private;
10604 -- package P is
10605 -- subtype W is G (D => 1);
10606 -- end package;
10607 -- type Rec (X : int) is record ... end record;
10608 -- package Q is new P (G => Rec);
10610 -- At the point of the instantiation, formal type G is Rec
10611 -- and therefore when reanalyzing "subtype W is G (D => 1);"
10612 -- which really looks like "subtype W is Rec (D => 1);" at
10613 -- the point of instantiation, we want to find the discriminant
10614 -- that corresponds to D in Rec, i.e. X.
10616 if Present (Original_Discriminant (Id))
10617 and then In_Instance
10618 then
10619 Discr := Find_Corresponding_Discriminant (Id, T);
10620 Found := True;
10622 else
10623 Discr := First_Discriminant (T);
10624 while Present (Discr) loop
10625 if Chars (Discr) = Chars (Id) then
10626 Found := True;
10627 exit;
10628 end if;
10630 Next_Discriminant (Discr);
10631 end loop;
10633 if not Found then
10634 Error_Msg_N ("& does not match any discriminant", Id);
10635 return New_Elmt_List;
10637 -- If the parent type is a generic formal, preserve the
10638 -- name of the discriminant for subsequent instances.
10639 -- see comment at the beginning of this if statement.
10641 elsif Is_Generic_Type (Root_Type (T)) then
10642 Set_Original_Discriminant (Id, Discr);
10643 end if;
10644 end if;
10646 Position := Pos_Of_Discr (T, Discr);
10648 if Present (Discr_Expr (Position)) then
10649 Error_Msg_N ("duplicate constraint for discriminant&", Id);
10651 else
10652 -- Each discriminant specified in the same named association
10653 -- must be associated with a separate copy of the
10654 -- corresponding expression.
10656 if Present (Next (Id)) then
10657 Expr := New_Copy_Tree (Expression (Constr));
10658 Set_Parent (Expr, Parent (Expression (Constr)));
10659 else
10660 Expr := Expression (Constr);
10661 end if;
10663 Discr_Expr (Position) := Expr;
10664 Process_Discriminant_Expression (Expr, Discr);
10665 end if;
10667 -- A discriminant association with more than one discriminant
10668 -- name is only allowed if the named discriminants are all of
10669 -- the same type (RM 3.7.1(8)).
10671 if E = Empty then
10672 E := Base_Type (Etype (Discr));
10674 elsif Base_Type (Etype (Discr)) /= E then
10675 Error_Msg_N
10676 ("all discriminants in an association " &
10677 "must have the same type", Id);
10678 end if;
10680 Next (Id);
10681 end loop;
10682 end if;
10684 Next (Constr);
10685 end loop;
10687 -- A discriminant constraint must provide exactly one value for each
10688 -- discriminant of the type (RM 3.7.1(8)).
10690 for J in Discr_Expr'Range loop
10691 if No (Discr_Expr (J)) then
10692 Error_Msg_N ("too few discriminants given in constraint", C);
10693 return New_Elmt_List;
10694 end if;
10695 end loop;
10697 -- Determine if there are discriminant expressions in the constraint
10699 for J in Discr_Expr'Range loop
10700 if Denotes_Discriminant
10701 (Discr_Expr (J), Check_Concurrent => True)
10702 then
10703 Discrim_Present := True;
10704 exit;
10705 end if;
10706 end loop;
10708 -- Build an element list consisting of the expressions given in the
10709 -- discriminant constraint and apply the appropriate checks. The list
10710 -- is constructed after resolving any named discriminant associations
10711 -- and therefore the expressions appear in the textual order of the
10712 -- discriminants.
10714 Discr := First_Discriminant (T);
10715 for J in Discr_Expr'Range loop
10716 if Discr_Expr (J) /= Error then
10717 Append_Elmt (Discr_Expr (J), Elist);
10719 -- If any of the discriminant constraints is given by a
10720 -- discriminant and we are in a derived type declaration we
10721 -- have a discriminant renaming. Establish link between new
10722 -- and old discriminant. The new discriminant has an implicit
10723 -- dereference if the old one does.
10725 if Denotes_Discriminant (Discr_Expr (J)) then
10726 if Derived_Def then
10727 declare
10728 New_Discr : constant Entity_Id := Entity (Discr_Expr (J));
10730 begin
10731 Set_Corresponding_Discriminant (New_Discr, Discr);
10732 Set_Has_Implicit_Dereference (New_Discr,
10733 Has_Implicit_Dereference (Discr));
10734 end;
10735 end if;
10737 -- Force the evaluation of non-discriminant expressions.
10738 -- If we have found a discriminant in the constraint 3.4(26)
10739 -- and 3.8(18) demand that no range checks are performed are
10740 -- after evaluation. If the constraint is for a component
10741 -- definition that has a per-object constraint, expressions are
10742 -- evaluated but not checked either. In all other cases perform
10743 -- a range check.
10745 else
10746 if Discrim_Present then
10747 null;
10749 elsif Parent_Kind (Parent (Def)) = N_Component_Declaration
10750 and then Has_Per_Object_Constraint
10751 (Defining_Identifier (Parent (Parent (Def))))
10752 then
10753 null;
10755 elsif Is_Access_Type (Etype (Discr)) then
10756 Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
10758 else
10759 Apply_Range_Check (Discr_Expr (J), Etype (Discr));
10760 end if;
10762 -- If the value of the discriminant may be visible in
10763 -- another unit or child unit, create an external name
10764 -- for it. We use the name of the object or component
10765 -- that carries the discriminated subtype. The code
10766 -- below may generate external symbols for the discriminant
10767 -- expression when not strictly needed, which is harmless.
10769 if Expander_Active
10770 and then Comes_From_Source (Def)
10771 and then not Is_Subprogram (Current_Scope)
10772 then
10773 declare
10774 Id : Entity_Id := Empty;
10775 begin
10776 if Nkind (Parent (Def)) = N_Object_Declaration then
10777 Id := Defining_Identifier (Parent (Def));
10779 elsif Nkind (Parent (Def)) = N_Component_Definition
10780 and then
10781 Nkind (Parent (Parent (Def)))
10782 = N_Component_Declaration
10783 then
10784 Id := Defining_Identifier (Parent (Parent (Def)));
10785 end if;
10787 if Present (Id) then
10788 Force_Evaluation (
10789 Discr_Expr (J),
10790 Related_Id => Id,
10791 Discr_Number => J);
10792 else
10793 Force_Evaluation (Discr_Expr (J));
10794 end if;
10795 end;
10796 else
10797 Force_Evaluation (Discr_Expr (J));
10798 end if;
10799 end if;
10801 -- Check that the designated type of an access discriminant's
10802 -- expression is not a class-wide type unless the discriminant's
10803 -- designated type is also class-wide.
10805 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
10806 and then not Is_Class_Wide_Type
10807 (Designated_Type (Etype (Discr)))
10808 and then Etype (Discr_Expr (J)) /= Any_Type
10809 and then Is_Class_Wide_Type
10810 (Designated_Type (Etype (Discr_Expr (J))))
10811 then
10812 Wrong_Type (Discr_Expr (J), Etype (Discr));
10814 elsif Is_Access_Type (Etype (Discr))
10815 and then not Is_Access_Constant (Etype (Discr))
10816 and then Is_Access_Type (Etype (Discr_Expr (J)))
10817 and then Is_Access_Constant (Etype (Discr_Expr (J)))
10818 then
10819 Error_Msg_NE
10820 ("constraint for discriminant& must be access to variable",
10821 Def, Discr);
10822 end if;
10823 end if;
10825 Next_Discriminant (Discr);
10826 end loop;
10828 return Elist;
10829 end Build_Discriminant_Constraints;
10831 ---------------------------------
10832 -- Build_Discriminated_Subtype --
10833 ---------------------------------
10835 procedure Build_Discriminated_Subtype
10836 (T : Entity_Id;
10837 Def_Id : Entity_Id;
10838 Elist : Elist_Id;
10839 Related_Nod : Node_Id;
10840 For_Access : Boolean := False)
10842 Has_Discrs : constant Boolean := Has_Discriminants (T);
10843 Constrained : constant Boolean :=
10844 (Has_Discrs
10845 and then not Is_Empty_Elmt_List (Elist)
10846 and then not Is_Class_Wide_Type (T))
10847 or else Is_Constrained (T);
10849 begin
10850 if Ekind (T) = E_Record_Type then
10851 Mutate_Ekind (Def_Id, E_Record_Subtype);
10853 -- Inherit preelaboration flag from base, for types for which it
10854 -- may have been set: records, private types, protected types.
10856 Set_Known_To_Have_Preelab_Init
10857 (Def_Id, Known_To_Have_Preelab_Init (T));
10859 elsif Ekind (T) = E_Task_Type then
10860 Mutate_Ekind (Def_Id, E_Task_Subtype);
10862 elsif Ekind (T) = E_Protected_Type then
10863 Mutate_Ekind (Def_Id, E_Protected_Subtype);
10864 Set_Known_To_Have_Preelab_Init
10865 (Def_Id, Known_To_Have_Preelab_Init (T));
10867 elsif Is_Private_Type (T) then
10868 Mutate_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
10869 Set_Known_To_Have_Preelab_Init
10870 (Def_Id, Known_To_Have_Preelab_Init (T));
10872 -- Private subtypes may have private dependents
10874 Set_Private_Dependents (Def_Id, New_Elmt_List);
10876 elsif Is_Class_Wide_Type (T) then
10877 Mutate_Ekind (Def_Id, E_Class_Wide_Subtype);
10879 else
10880 -- Incomplete type. Attach subtype to list of dependents, to be
10881 -- completed with full view of parent type, unless is it the
10882 -- designated subtype of a record component within an init_proc.
10883 -- This last case arises for a component of an access type whose
10884 -- designated type is incomplete (e.g. a Taft Amendment type).
10885 -- The designated subtype is within an inner scope, and needs no
10886 -- elaboration, because only the access type is needed in the
10887 -- initialization procedure.
10889 if Ekind (T) = E_Incomplete_Type then
10890 Mutate_Ekind (Def_Id, E_Incomplete_Subtype);
10891 else
10892 Mutate_Ekind (Def_Id, Ekind (T));
10893 end if;
10895 if For_Access and then Within_Init_Proc then
10896 null;
10897 else
10898 Append_Elmt (Def_Id, Private_Dependents (T));
10899 end if;
10900 end if;
10902 Set_Etype (Def_Id, T);
10903 Reinit_Size_Align (Def_Id);
10904 Set_Has_Discriminants (Def_Id, Has_Discrs);
10905 Set_Is_Constrained (Def_Id, Constrained);
10907 Set_First_Entity (Def_Id, First_Entity (T));
10908 Set_Last_Entity (Def_Id, Last_Entity (T));
10909 Set_Has_Implicit_Dereference
10910 (Def_Id, Has_Implicit_Dereference (T));
10911 Set_Has_Pragma_Unreferenced_Objects
10912 (Def_Id, Has_Pragma_Unreferenced_Objects (T));
10914 -- If the subtype is the completion of a private declaration, there may
10915 -- have been representation clauses for the partial view, and they must
10916 -- be preserved. Build_Derived_Type chains the inherited clauses with
10917 -- the ones appearing on the extension. If this comes from a subtype
10918 -- declaration, all clauses are inherited.
10920 if No (First_Rep_Item (Def_Id)) then
10921 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10922 end if;
10924 if Is_Tagged_Type (T) then
10925 Set_Is_Tagged_Type (Def_Id);
10926 Set_No_Tagged_Streams_Pragma (Def_Id, No_Tagged_Streams_Pragma (T));
10927 Make_Class_Wide_Type (Def_Id);
10928 end if;
10930 -- When prefixed calls are enabled for untagged types, the subtype
10931 -- shares the primitive operations of its base type. Do this even
10932 -- when GNAT extensions are not allowed, in order to give better
10933 -- error messages.
10935 Set_Direct_Primitive_Operations
10936 (Def_Id, Direct_Primitive_Operations (T));
10938 Set_Stored_Constraint (Def_Id, No_Elist);
10940 if Has_Discrs then
10941 Set_Discriminant_Constraint (Def_Id, Elist);
10942 Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
10943 end if;
10945 if Is_Tagged_Type (T) then
10947 -- Ada 2005 (AI-251): In case of concurrent types we inherit the
10948 -- concurrent record type.
10950 if Ada_Version >= Ada_2005 and then Is_Concurrent_Type (T) then
10951 Set_Corresponding_Record_Type
10952 (Def_Id, Corresponding_Record_Type (T));
10953 end if;
10955 Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
10956 end if;
10958 -- Subtypes introduced by component declarations do not need to be
10959 -- marked as delayed, and do not get freeze nodes, because the semantics
10960 -- verifies that the parents of the subtypes are frozen before the
10961 -- enclosing record is frozen.
10963 if not Is_Type (Scope (Def_Id)) then
10964 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
10966 if Is_Private_Type (T)
10967 and then Present (Full_View (T))
10968 then
10969 Conditional_Delay (Def_Id, Full_View (T));
10970 else
10971 Conditional_Delay (Def_Id, T);
10972 end if;
10973 end if;
10975 if Is_Record_Type (T) then
10976 Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
10978 if Has_Discrs
10979 and then not Is_Empty_Elmt_List (Elist)
10980 and then not For_Access
10981 then
10982 Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
10984 elsif not Is_Private_Type (T) then
10985 Set_Cloned_Subtype (Def_Id, T);
10986 end if;
10987 end if;
10988 end Build_Discriminated_Subtype;
10990 ---------------------------
10991 -- Build_Itype_Reference --
10992 ---------------------------
10994 procedure Build_Itype_Reference
10995 (Ityp : Entity_Id;
10996 Nod : Node_Id)
10998 IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
10999 begin
11000 -- Itype references are only created for use by the back-end
11002 if Inside_A_Generic then
11003 return;
11004 else
11005 Set_Itype (IR, Ityp);
11007 -- If Nod is a library unit entity, then Insert_After won't work,
11008 -- because Nod is not a member of any list. Therefore, we use
11009 -- Add_Global_Declaration in this case. This can happen if we have a
11010 -- build-in-place library function, child unit or not.
11012 if (Nkind (Nod) in N_Entity and then Is_Compilation_Unit (Nod))
11013 or else (Nkind (Nod) in
11014 N_Defining_Program_Unit_Name | N_Subprogram_Declaration
11015 and then Is_Compilation_Unit (Defining_Entity (Nod)))
11016 then
11017 Add_Global_Declaration (IR);
11018 else
11019 Insert_After (Nod, IR);
11020 end if;
11021 end if;
11022 end Build_Itype_Reference;
11024 ------------------------
11025 -- Build_Scalar_Bound --
11026 ------------------------
11028 function Build_Scalar_Bound
11029 (Bound : Node_Id;
11030 Par_T : Entity_Id;
11031 Der_T : Entity_Id) return Node_Id
11033 New_Bound : Entity_Id;
11035 begin
11036 -- Note: not clear why this is needed, how can the original bound
11037 -- be unanalyzed at this point? and if it is, what business do we
11038 -- have messing around with it? and why is the base type of the
11039 -- parent type the right type for the resolution. It probably is
11040 -- not. It is OK for the new bound we are creating, but not for
11041 -- the old one??? Still if it never happens, no problem.
11043 Analyze_And_Resolve (Bound, Base_Type (Par_T));
11045 if Nkind (Bound) in N_Integer_Literal | N_Real_Literal then
11046 New_Bound := New_Copy (Bound);
11047 Set_Etype (New_Bound, Der_T);
11048 Set_Analyzed (New_Bound);
11050 elsif Is_Entity_Name (Bound) then
11051 New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
11053 -- The following is almost certainly wrong. What business do we have
11054 -- relocating a node (Bound) that is presumably still attached to
11055 -- the tree elsewhere???
11057 else
11058 New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
11059 end if;
11061 Set_Etype (New_Bound, Der_T);
11062 return New_Bound;
11063 end Build_Scalar_Bound;
11065 -------------------------------
11066 -- Check_Abstract_Overriding --
11067 -------------------------------
11069 procedure Check_Abstract_Overriding (T : Entity_Id) is
11070 Alias_Subp : Entity_Id;
11071 Elmt : Elmt_Id;
11072 Op_List : Elist_Id;
11073 Subp : Entity_Id;
11074 Type_Def : Node_Id;
11076 procedure Check_Pragma_Implemented (Subp : Entity_Id);
11077 -- Ada 2012 (AI05-0030): Subprogram Subp overrides an interface routine
11078 -- which has pragma Implemented already set. Check whether Subp's entity
11079 -- kind conforms to the implementation kind of the overridden routine.
11081 procedure Check_Pragma_Implemented
11082 (Subp : Entity_Id;
11083 Iface_Subp : Entity_Id);
11084 -- Ada 2012 (AI05-0030): Subprogram Subp overrides interface routine
11085 -- Iface_Subp and both entities have pragma Implemented already set on
11086 -- them. Check whether the two implementation kinds are conforming.
11088 procedure Inherit_Pragma_Implemented
11089 (Subp : Entity_Id;
11090 Iface_Subp : Entity_Id);
11091 -- Ada 2012 (AI05-0030): Interface primitive Subp overrides interface
11092 -- subprogram Iface_Subp which has been marked by pragma Implemented.
11093 -- Propagate the implementation kind of Iface_Subp to Subp.
11095 ------------------------------
11096 -- Check_Pragma_Implemented --
11097 ------------------------------
11099 procedure Check_Pragma_Implemented (Subp : Entity_Id) is
11100 Iface_Alias : constant Entity_Id := Interface_Alias (Subp);
11101 Impl_Kind : constant Name_Id := Implementation_Kind (Iface_Alias);
11102 Subp_Alias : constant Entity_Id := Alias (Subp);
11103 Contr_Typ : Entity_Id;
11104 Impl_Subp : Entity_Id;
11106 begin
11107 -- Subp must have an alias since it is a hidden entity used to link
11108 -- an interface subprogram to its overriding counterpart.
11110 pragma Assert (Present (Subp_Alias));
11112 -- Handle aliases to synchronized wrappers
11114 Impl_Subp := Subp_Alias;
11116 if Is_Primitive_Wrapper (Impl_Subp) then
11117 Impl_Subp := Wrapped_Entity (Impl_Subp);
11118 end if;
11120 -- Extract the type of the controlling formal
11122 Contr_Typ := Etype (First_Formal (Subp_Alias));
11124 if Is_Concurrent_Record_Type (Contr_Typ) then
11125 Contr_Typ := Corresponding_Concurrent_Type (Contr_Typ);
11126 end if;
11128 -- An interface subprogram whose implementation kind is By_Entry must
11129 -- be implemented by an entry.
11131 if Impl_Kind = Name_By_Entry
11132 and then Ekind (Impl_Subp) /= E_Entry
11133 then
11134 Error_Msg_Node_2 := Iface_Alias;
11135 Error_Msg_NE
11136 ("type & must implement abstract subprogram & with an entry",
11137 Subp_Alias, Contr_Typ);
11139 elsif Impl_Kind = Name_By_Protected_Procedure then
11141 -- An interface subprogram whose implementation kind is By_
11142 -- Protected_Procedure cannot be implemented by a primitive
11143 -- procedure of a task type.
11145 if Ekind (Contr_Typ) /= E_Protected_Type then
11146 Error_Msg_Node_2 := Contr_Typ;
11147 Error_Msg_NE
11148 ("interface subprogram & cannot be implemented by a "
11149 & "primitive procedure of task type &",
11150 Subp_Alias, Iface_Alias);
11152 -- An interface subprogram whose implementation kind is By_
11153 -- Protected_Procedure must be implemented by a procedure.
11155 elsif Ekind (Impl_Subp) /= E_Procedure then
11156 Error_Msg_Node_2 := Iface_Alias;
11157 Error_Msg_NE
11158 ("type & must implement abstract subprogram & with a "
11159 & "procedure", Subp_Alias, Contr_Typ);
11161 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
11162 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
11163 then
11164 Error_Msg_Name_1 := Impl_Kind;
11165 Error_Msg_N
11166 ("overriding operation& must have synchronization%",
11167 Subp_Alias);
11168 end if;
11170 -- If primitive has Optional synchronization, overriding operation
11171 -- must match if it has an explicit synchronization.
11173 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
11174 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
11175 then
11176 Error_Msg_Name_1 := Impl_Kind;
11177 Error_Msg_N
11178 ("overriding operation& must have synchronization%", Subp_Alias);
11179 end if;
11180 end Check_Pragma_Implemented;
11182 ------------------------------
11183 -- Check_Pragma_Implemented --
11184 ------------------------------
11186 procedure Check_Pragma_Implemented
11187 (Subp : Entity_Id;
11188 Iface_Subp : Entity_Id)
11190 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
11191 Subp_Kind : constant Name_Id := Implementation_Kind (Subp);
11193 begin
11194 -- Ada 2012 (AI05-0030): The implementation kinds of an overridden
11195 -- and overriding subprogram are different. In general this is an
11196 -- error except when the implementation kind of the overridden
11197 -- subprograms is By_Any or Optional.
11199 if Iface_Kind /= Subp_Kind
11200 and then Iface_Kind /= Name_By_Any
11201 and then Iface_Kind /= Name_Optional
11202 then
11203 if Iface_Kind = Name_By_Entry then
11204 Error_Msg_N
11205 ("incompatible implementation kind, overridden subprogram " &
11206 "is marked By_Entry", Subp);
11207 else
11208 Error_Msg_N
11209 ("incompatible implementation kind, overridden subprogram " &
11210 "is marked By_Protected_Procedure", Subp);
11211 end if;
11212 end if;
11213 end Check_Pragma_Implemented;
11215 --------------------------------
11216 -- Inherit_Pragma_Implemented --
11217 --------------------------------
11219 procedure Inherit_Pragma_Implemented
11220 (Subp : Entity_Id;
11221 Iface_Subp : Entity_Id)
11223 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
11224 Loc : constant Source_Ptr := Sloc (Subp);
11225 Impl_Prag : Node_Id;
11227 begin
11228 -- Since the implementation kind is stored as a representation item
11229 -- rather than a flag, create a pragma node.
11231 Impl_Prag :=
11232 Make_Pragma (Loc,
11233 Chars => Name_Implemented,
11234 Pragma_Argument_Associations => New_List (
11235 Make_Pragma_Argument_Association (Loc,
11236 Expression => New_Occurrence_Of (Subp, Loc)),
11238 Make_Pragma_Argument_Association (Loc,
11239 Expression => Make_Identifier (Loc, Iface_Kind))));
11241 -- The pragma doesn't need to be analyzed because it is internally
11242 -- built. It is safe to directly register it as a rep item since we
11243 -- are only interested in the characters of the implementation kind.
11245 Record_Rep_Item (Subp, Impl_Prag);
11246 end Inherit_Pragma_Implemented;
11248 -- Start of processing for Check_Abstract_Overriding
11250 begin
11251 Op_List := Primitive_Operations (T);
11253 -- Loop to check primitive operations
11255 Elmt := First_Elmt (Op_List);
11256 while Present (Elmt) loop
11257 Subp := Node (Elmt);
11258 Alias_Subp := Alias (Subp);
11260 -- If the parent type is untagged, then no overriding error checks
11261 -- are needed (such as in the case of an implicit full type for
11262 -- a derived type whose parent is an untagged private type with
11263 -- a tagged full type).
11265 if not Is_Tagged_Type (Etype (T)) then
11266 null;
11268 -- Inherited subprograms are identified by the fact that they do not
11269 -- come from source, and the associated source location is the
11270 -- location of the first subtype of the derived type.
11272 -- Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
11273 -- subprograms that "require overriding".
11275 -- Special exception, do not complain about failure to override the
11276 -- stream routines _Input and _Output, as well as the primitive
11277 -- operations used in dispatching selects since we always provide
11278 -- automatic overridings for these subprograms.
11280 -- The partial view of T may have been a private extension, for
11281 -- which inherited functions dispatching on result are abstract.
11282 -- If the full view is a null extension, there is no need for
11283 -- overriding in Ada 2005, but wrappers need to be built for them
11284 -- (see exp_ch3, Build_Controlling_Function_Wrappers).
11286 elsif Is_Null_Extension (T)
11287 and then Has_Controlling_Result (Subp)
11288 and then Ada_Version >= Ada_2005
11289 and then Present (Alias_Subp)
11290 and then not Comes_From_Source (Subp)
11291 and then not Is_Abstract_Subprogram (Alias_Subp)
11292 and then not Is_Access_Type (Etype (Subp))
11293 then
11294 null;
11296 -- Ada 2005 (AI-251): Internal entities of interfaces need no
11297 -- processing because this check is done with the aliased
11298 -- entity
11300 elsif Present (Interface_Alias (Subp)) then
11301 null;
11303 -- AI12-0042: Test for rule in 7.3.2(6.1/4), that requires overriding
11304 -- of a visible private primitive inherited from an ancestor with
11305 -- the aspect Type_Invariant'Class, unless the inherited primitive
11306 -- is abstract.
11308 elsif not Is_Abstract_Subprogram (Subp)
11309 and then not Comes_From_Source (Subp) -- An inherited subprogram
11310 and then Requires_Overriding (Subp)
11311 and then Present (Alias_Subp)
11312 and then Has_Invariants (Etype (T))
11313 and then Present (Get_Pragma (Etype (T), Pragma_Invariant))
11314 and then Class_Present (Get_Pragma (Etype (T), Pragma_Invariant))
11315 and then Is_Private_Primitive (Alias_Subp)
11316 then
11317 Error_Msg_NE
11318 ("inherited private primitive & must be overridden", T, Subp);
11319 Error_Msg_N
11320 ("\because ancestor type has 'Type_'Invariant''Class " &
11321 "(RM 7.3.2(6.1))", T);
11323 elsif (Is_Abstract_Subprogram (Subp)
11324 or else Requires_Overriding (Subp)
11325 or else
11326 (Has_Controlling_Result (Subp)
11327 and then Present (Alias_Subp)
11328 and then not Comes_From_Source (Subp)
11329 and then Sloc (Subp) = Sloc (First_Subtype (T))))
11330 and then not Is_TSS (Subp, TSS_Stream_Input)
11331 and then not Is_TSS (Subp, TSS_Stream_Output)
11332 and then not Is_Abstract_Type (T)
11333 and then not Is_Predefined_Interface_Primitive (Subp)
11335 -- Ada 2005 (AI-251): Do not consider hidden entities associated
11336 -- with abstract interface types because the check will be done
11337 -- with the aliased entity (otherwise we generate a duplicated
11338 -- error message).
11340 and then No (Interface_Alias (Subp))
11341 then
11342 if Present (Alias_Subp) then
11344 -- Only perform the check for a derived subprogram when the
11345 -- type has an explicit record extension. This avoids incorrect
11346 -- flagging of abstract subprograms for the case of a type
11347 -- without an extension that is derived from a formal type
11348 -- with a tagged actual (can occur within a private part).
11350 -- Ada 2005 (AI-391): In the case of an inherited function with
11351 -- a controlling result of the type, the rule does not apply if
11352 -- the type is a null extension (unless the parent function
11353 -- itself is abstract, in which case the function must still be
11354 -- be overridden). The expander will generate an overriding
11355 -- wrapper function calling the parent subprogram (see
11356 -- Exp_Ch3.Make_Controlling_Wrapper_Functions).
11358 Type_Def := Type_Definition (Parent (T));
11360 if Nkind (Type_Def) = N_Derived_Type_Definition
11361 and then Present (Record_Extension_Part (Type_Def))
11362 and then
11363 (Ada_Version < Ada_2005
11364 or else not Is_Null_Extension (T)
11365 or else Ekind (Subp) = E_Procedure
11366 or else not Has_Controlling_Result (Subp)
11367 or else Is_Abstract_Subprogram (Alias_Subp)
11368 or else Requires_Overriding (Subp)
11369 or else Is_Access_Type (Etype (Subp)))
11370 then
11371 -- Avoid reporting error in case of abstract predefined
11372 -- primitive inherited from interface type because the
11373 -- body of internally generated predefined primitives
11374 -- of tagged types are generated later by Freeze_Type
11376 if Is_Interface (Root_Type (T))
11377 and then Is_Abstract_Subprogram (Subp)
11378 and then Is_Predefined_Dispatching_Operation (Subp)
11379 and then not Comes_From_Source (Ultimate_Alias (Subp))
11380 then
11381 null;
11383 -- A null extension is not obliged to override an inherited
11384 -- procedure subject to pragma Extensions_Visible with value
11385 -- False and at least one controlling OUT parameter
11386 -- (SPARK RM 6.1.7(6)).
11388 elsif Is_Null_Extension (T)
11389 and then Is_EVF_Procedure (Subp)
11390 then
11391 null;
11393 -- Subprogram renamings cannot be overridden
11395 elsif Comes_From_Source (Subp)
11396 and then Present (Alias (Subp))
11397 then
11398 null;
11400 -- Skip reporting the error on Ada 2022 only subprograms
11401 -- that require overriding if we are not in Ada 2022 mode.
11403 elsif Ada_Version < Ada_2022
11404 and then Requires_Overriding (Subp)
11405 and then Is_Ada_2022_Only (Ultimate_Alias (Subp))
11406 then
11407 null;
11409 else
11410 Error_Msg_NE
11411 ("type must be declared abstract or & overridden",
11412 T, Subp);
11414 -- Traverse the whole chain of aliased subprograms to
11415 -- complete the error notification. This is especially
11416 -- useful for traceability of the chain of entities when
11417 -- the subprogram corresponds with an interface
11418 -- subprogram (which may be defined in another package).
11420 if Present (Alias_Subp) then
11421 declare
11422 E : Entity_Id;
11424 begin
11425 E := Subp;
11426 while Present (Alias (E)) loop
11428 -- Avoid reporting redundant errors on entities
11429 -- inherited from interfaces
11431 if Sloc (E) /= Sloc (T) then
11432 Error_Msg_Sloc := Sloc (E);
11433 Error_Msg_NE
11434 ("\& has been inherited #", T, Subp);
11435 end if;
11437 E := Alias (E);
11438 end loop;
11440 Error_Msg_Sloc := Sloc (E);
11442 -- AI05-0068: report if there is an overriding
11443 -- non-abstract subprogram that is invisible.
11445 if Is_Hidden (E)
11446 and then not Is_Abstract_Subprogram (E)
11447 then
11448 Error_Msg_NE
11449 ("\& subprogram# is not visible",
11450 T, Subp);
11452 -- Clarify the case where a non-null extension must
11453 -- override inherited procedure subject to pragma
11454 -- Extensions_Visible with value False and at least
11455 -- one controlling OUT param.
11457 elsif Is_EVF_Procedure (E) then
11458 Error_Msg_NE
11459 ("\& # is subject to Extensions_Visible False",
11460 T, Subp);
11462 else
11463 Error_Msg_NE
11464 ("\& has been inherited from subprogram #",
11465 T, Subp);
11466 end if;
11467 end;
11468 end if;
11469 end if;
11471 -- Ada 2005 (AI-345): Protected or task type implementing
11472 -- abstract interfaces.
11474 elsif Is_Concurrent_Record_Type (T)
11475 and then Present (Interfaces (T))
11476 then
11477 -- There is no need to check here RM 9.4(11.9/3) since we
11478 -- are processing the corresponding record type and the
11479 -- mode of the overriding subprograms was verified by
11480 -- Check_Conformance when the corresponding concurrent
11481 -- type declaration was analyzed.
11483 Error_Msg_NE
11484 ("interface subprogram & must be overridden", T, Subp);
11486 -- Examine primitive operations of synchronized type to find
11487 -- homonyms that have the wrong profile.
11489 declare
11490 Prim : Entity_Id;
11492 begin
11493 Prim := First_Entity (Corresponding_Concurrent_Type (T));
11494 while Present (Prim) loop
11495 if Chars (Prim) = Chars (Subp) then
11496 Error_Msg_NE
11497 ("profile is not type conformant with prefixed "
11498 & "view profile of inherited operation&",
11499 Prim, Subp);
11500 end if;
11502 Next_Entity (Prim);
11503 end loop;
11504 end;
11505 end if;
11507 else
11508 Error_Msg_Node_2 := T;
11509 Error_Msg_N
11510 ("abstract subprogram& not allowed for type&", Subp);
11512 -- Also post unconditional warning on the type (unconditional
11513 -- so that if there are more than one of these cases, we get
11514 -- them all, and not just the first one).
11516 Error_Msg_Node_2 := Subp;
11517 Error_Msg_N ("nonabstract type& has abstract subprogram&!", T);
11518 end if;
11520 -- A subprogram subject to pragma Extensions_Visible with value
11521 -- "True" cannot override a subprogram subject to the same pragma
11522 -- with value "False" (SPARK RM 6.1.7(5)).
11524 elsif Extensions_Visible_Status (Subp) = Extensions_Visible_True
11525 and then Present (Overridden_Operation (Subp))
11526 and then Extensions_Visible_Status (Overridden_Operation (Subp)) =
11527 Extensions_Visible_False
11528 then
11529 Error_Msg_Sloc := Sloc (Overridden_Operation (Subp));
11530 Error_Msg_N
11531 ("subprogram & with Extensions_Visible True cannot override "
11532 & "subprogram # with Extensions_Visible False", Subp);
11533 end if;
11535 -- Ada 2012 (AI05-0030): Perform checks related to pragma Implemented
11537 -- Subp is an expander-generated procedure which maps an interface
11538 -- alias to a protected wrapper. The interface alias is flagged by
11539 -- pragma Implemented. Ensure that Subp is a procedure when the
11540 -- implementation kind is By_Protected_Procedure or an entry when
11541 -- By_Entry.
11543 if Ada_Version >= Ada_2012
11544 and then Is_Hidden (Subp)
11545 and then Present (Interface_Alias (Subp))
11546 and then Has_Rep_Pragma (Interface_Alias (Subp), Name_Implemented)
11547 then
11548 Check_Pragma_Implemented (Subp);
11549 end if;
11551 -- Subp is an interface primitive which overrides another interface
11552 -- primitive marked with pragma Implemented.
11554 if Ada_Version >= Ada_2012
11555 and then Present (Overridden_Operation (Subp))
11556 and then Has_Rep_Pragma
11557 (Overridden_Operation (Subp), Name_Implemented)
11558 then
11559 -- If the overriding routine is also marked by Implemented, check
11560 -- that the two implementation kinds are conforming.
11562 if Has_Rep_Pragma (Subp, Name_Implemented) then
11563 Check_Pragma_Implemented
11564 (Subp => Subp,
11565 Iface_Subp => Overridden_Operation (Subp));
11567 -- Otherwise the overriding routine inherits the implementation
11568 -- kind from the overridden subprogram.
11570 else
11571 Inherit_Pragma_Implemented
11572 (Subp => Subp,
11573 Iface_Subp => Overridden_Operation (Subp));
11574 end if;
11575 end if;
11577 -- Ada 2005 (AI95-0414) and Ada 2022 (AI12-0269): Diagnose failure to
11578 -- match No_Return in parent, but do it unconditionally in Ada 95 too
11579 -- for procedures, since this is our pragma.
11581 if Present (Overridden_Operation (Subp))
11582 and then No_Return (Overridden_Operation (Subp))
11583 then
11585 -- If the subprogram is a renaming, check that the renamed
11586 -- subprogram is No_Return.
11588 if Present (Renamed_Or_Alias (Subp)) then
11589 if not No_Return (Renamed_Or_Alias (Subp)) then
11590 Error_Msg_NE ("subprogram & must be No_Return",
11591 Subp,
11592 Renamed_Or_Alias (Subp));
11593 Error_Msg_N ("\since renaming & overrides No_Return "
11594 & "subprogram (RM 6.5.1(6/2))",
11595 Subp);
11596 end if;
11598 -- Make sure that the subprogram itself is No_Return.
11600 elsif not No_Return (Subp) then
11601 Error_Msg_N ("overriding subprogram & must be No_Return", Subp);
11602 Error_Msg_N
11603 ("\since overridden subprogram is No_Return (RM 6.5.1(6/2))",
11604 Subp);
11605 end if;
11606 end if;
11608 -- If the operation is a wrapper for a synchronized primitive, it
11609 -- may be called indirectly through a dispatching select. We assume
11610 -- that it will be referenced elsewhere indirectly, and suppress
11611 -- warnings about an unused entity.
11613 if Is_Primitive_Wrapper (Subp)
11614 and then Present (Wrapped_Entity (Subp))
11615 then
11616 Set_Referenced (Wrapped_Entity (Subp));
11617 end if;
11619 Next_Elmt (Elmt);
11620 end loop;
11621 end Check_Abstract_Overriding;
11623 ------------------------------------------------
11624 -- Check_Access_Discriminant_Requires_Limited --
11625 ------------------------------------------------
11627 procedure Check_Access_Discriminant_Requires_Limited
11628 (D : Node_Id;
11629 Loc : Node_Id)
11631 begin
11632 -- A discriminant_specification for an access discriminant shall appear
11633 -- only in the declaration for a task or protected type, or for a type
11634 -- with the reserved word 'limited' in its definition or in one of its
11635 -- ancestors (RM 3.7(10)).
11637 -- AI-0063: The proper condition is that type must be immutably limited,
11638 -- or else be a partial view.
11640 if Nkind (Discriminant_Type (D)) = N_Access_Definition then
11641 if Is_Inherently_Limited_Type (Current_Scope)
11642 or else
11643 (Nkind (Parent (Current_Scope)) = N_Private_Type_Declaration
11644 and then Limited_Present (Parent (Current_Scope)))
11645 then
11646 null;
11648 else
11649 Error_Msg_N
11650 ("access discriminants allowed only for limited types", Loc);
11651 end if;
11652 end if;
11653 end Check_Access_Discriminant_Requires_Limited;
11655 -----------------------------------
11656 -- Check_Aliased_Component_Types --
11657 -----------------------------------
11659 procedure Check_Aliased_Component_Types (T : Entity_Id) is
11660 C : Entity_Id;
11662 begin
11663 -- ??? Also need to check components of record extensions, but not
11664 -- components of protected types (which are always limited).
11666 -- Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
11667 -- types to be unconstrained. This is safe because it is illegal to
11668 -- create access subtypes to such types with explicit discriminant
11669 -- constraints.
11671 if not Is_Limited_Type (T) then
11672 if Ekind (T) = E_Record_Type then
11673 C := First_Component (T);
11674 while Present (C) loop
11675 if Is_Aliased (C)
11676 and then Has_Discriminants (Etype (C))
11677 and then not Is_Constrained (Etype (C))
11678 and then not In_Instance_Body
11679 and then Ada_Version < Ada_2005
11680 then
11681 Error_Msg_N
11682 ("aliased component must be constrained (RM 3.6(11))",
11684 end if;
11686 Next_Component (C);
11687 end loop;
11689 elsif Ekind (T) = E_Array_Type then
11690 if Has_Aliased_Components (T)
11691 and then Has_Discriminants (Component_Type (T))
11692 and then not Is_Constrained (Component_Type (T))
11693 and then not In_Instance_Body
11694 and then Ada_Version < Ada_2005
11695 then
11696 Error_Msg_N
11697 ("aliased component type must be constrained (RM 3.6(11))",
11699 end if;
11700 end if;
11701 end if;
11702 end Check_Aliased_Component_Types;
11704 --------------------------------------
11705 -- Check_Anonymous_Access_Component --
11706 --------------------------------------
11708 procedure Check_Anonymous_Access_Component
11709 (Typ_Decl : Node_Id;
11710 Typ : Entity_Id;
11711 Prev : Entity_Id;
11712 Comp_Def : Node_Id;
11713 Access_Def : Node_Id)
11715 Loc : constant Source_Ptr := Sloc (Comp_Def);
11716 Anon_Access : Entity_Id;
11717 Acc_Def : Node_Id;
11718 Decl : Node_Id;
11719 Type_Def : Node_Id;
11721 procedure Build_Incomplete_Type_Declaration;
11722 -- If the record type contains components that include an access to the
11723 -- current record, then create an incomplete type declaration for the
11724 -- record, to be used as the designated type of the anonymous access.
11725 -- This is done only once, and only if there is no previous partial
11726 -- view of the type.
11728 function Designates_T (Subt : Node_Id) return Boolean;
11729 -- Check whether a node designates the enclosing record type, or 'Class
11730 -- of that type
11732 function Mentions_T (Acc_Def : Node_Id) return Boolean;
11733 -- Check whether an access definition includes a reference to
11734 -- the enclosing record type. The reference can be a subtype mark
11735 -- in the access definition itself, a 'Class attribute reference, or
11736 -- recursively a reference appearing in a parameter specification
11737 -- or result definition of an access_to_subprogram definition.
11739 --------------------------------------
11740 -- Build_Incomplete_Type_Declaration --
11741 --------------------------------------
11743 procedure Build_Incomplete_Type_Declaration is
11744 Decl : Node_Id;
11745 Inc_T : Entity_Id;
11746 H : Entity_Id;
11748 -- Is_Tagged indicates whether the type is tagged. It is tagged if
11749 -- it's "is new ... with record" or else "is tagged record ...".
11751 Typ_Def : constant Node_Id :=
11752 (if Nkind (Typ_Decl) = N_Full_Type_Declaration
11753 then Type_Definition (Typ_Decl) else Empty);
11754 Is_Tagged : constant Boolean :=
11755 Present (Typ_Def)
11756 and then
11757 ((Nkind (Typ_Def) = N_Derived_Type_Definition
11758 and then
11759 Present (Record_Extension_Part (Typ_Def)))
11760 or else
11761 (Nkind (Typ_Def) = N_Record_Definition
11762 and then Tagged_Present (Typ_Def)));
11764 begin
11765 -- If there is a previous partial view, no need to create a new one
11766 -- If the partial view, given by Prev, is incomplete, If Prev is
11767 -- a private declaration, full declaration is flagged accordingly.
11769 if Prev /= Typ then
11770 if Is_Tagged then
11771 Make_Class_Wide_Type (Prev);
11772 Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
11773 Set_Etype (Class_Wide_Type (Typ), Typ);
11774 end if;
11776 return;
11778 elsif Has_Private_Declaration (Typ) then
11780 -- If we refer to T'Class inside T, and T is the completion of a
11781 -- private type, then make sure the class-wide type exists.
11783 if Is_Tagged then
11784 Make_Class_Wide_Type (Typ);
11785 end if;
11787 return;
11789 -- If there was a previous anonymous access type, the incomplete
11790 -- type declaration will have been created already.
11792 elsif Present (Current_Entity (Typ))
11793 and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
11794 and then Full_View (Current_Entity (Typ)) = Typ
11795 then
11796 if Is_Tagged
11797 and then Comes_From_Source (Current_Entity (Typ))
11798 and then not Is_Tagged_Type (Current_Entity (Typ))
11799 then
11800 Make_Class_Wide_Type (Typ);
11801 Error_Msg_N
11802 ("incomplete view of tagged type should be declared tagged??",
11803 Parent (Current_Entity (Typ)));
11804 end if;
11805 return;
11807 else
11808 Inc_T := Make_Defining_Identifier (Loc, Chars (Typ));
11809 Decl := Make_Incomplete_Type_Declaration (Loc, Inc_T);
11811 -- Type has already been inserted into the current scope. Remove
11812 -- it, and add incomplete declaration for type, so that subsequent
11813 -- anonymous access types can use it. The entity is unchained from
11814 -- the homonym list and from immediate visibility. After analysis,
11815 -- the entity in the incomplete declaration becomes immediately
11816 -- visible in the record declaration that follows.
11818 H := Current_Entity (Typ);
11820 if H = Typ then
11821 Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
11823 else
11824 while Present (Homonym (H)) and then Homonym (H) /= Typ loop
11825 H := Homonym (Typ);
11826 end loop;
11828 Set_Homonym (H, Homonym (Typ));
11829 end if;
11831 Insert_Before (Typ_Decl, Decl);
11832 Analyze (Decl);
11833 Set_Full_View (Inc_T, Typ);
11834 Set_Incomplete_View (Typ_Decl, Inc_T);
11836 -- If the type is tagged, create a common class-wide type for
11837 -- both views, and set the Etype of the class-wide type to the
11838 -- full view.
11840 if Is_Tagged then
11841 Make_Class_Wide_Type (Inc_T);
11842 Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
11843 Set_Etype (Class_Wide_Type (Typ), Typ);
11844 end if;
11846 -- If the scope is a package with a limited view, create a shadow
11847 -- entity for the incomplete type like Build_Limited_Views, so as
11848 -- to make it possible for Remove_Limited_With_Unit to reinstall
11849 -- this incomplete type as the visible entity.
11851 if Ekind (Scope (Inc_T)) = E_Package
11852 and then Present (Limited_View (Scope (Inc_T)))
11853 then
11854 declare
11855 Shadow : constant Entity_Id := Make_Temporary (Loc, 'Z');
11857 begin
11858 -- This is modeled on Build_Shadow_Entity
11860 Set_Chars (Shadow, Chars (Inc_T));
11861 Set_Parent (Shadow, Decl);
11862 Decorate_Type (Shadow, Scope (Inc_T), Is_Tagged);
11863 Set_Is_Internal (Shadow);
11864 Set_From_Limited_With (Shadow);
11865 Set_Non_Limited_View (Shadow, Inc_T);
11866 Set_Private_Dependents (Shadow, New_Elmt_List);
11868 if Is_Tagged then
11869 Set_Non_Limited_View
11870 (Class_Wide_Type (Shadow), Class_Wide_Type (Inc_T));
11871 end if;
11873 Append_Entity (Shadow, Limited_View (Scope (Inc_T)));
11874 end;
11875 end if;
11876 end if;
11877 end Build_Incomplete_Type_Declaration;
11879 ------------------
11880 -- Designates_T --
11881 ------------------
11883 function Designates_T (Subt : Node_Id) return Boolean is
11884 Type_Id : constant Name_Id := Chars (Typ);
11886 function Names_T (Nam : Node_Id) return Boolean;
11887 -- The record type has not been introduced in the current scope
11888 -- yet, so we must examine the name of the type itself, either
11889 -- an identifier T, or an expanded name of the form P.T, where
11890 -- P denotes the current scope.
11892 -------------
11893 -- Names_T --
11894 -------------
11896 function Names_T (Nam : Node_Id) return Boolean is
11897 begin
11898 if Nkind (Nam) = N_Identifier then
11899 return Chars (Nam) = Type_Id;
11901 elsif Nkind (Nam) = N_Selected_Component then
11902 if Chars (Selector_Name (Nam)) = Type_Id then
11903 if Nkind (Prefix (Nam)) = N_Identifier then
11904 return Chars (Prefix (Nam)) = Chars (Current_Scope);
11906 elsif Nkind (Prefix (Nam)) = N_Selected_Component then
11907 return Chars (Selector_Name (Prefix (Nam))) =
11908 Chars (Current_Scope);
11909 else
11910 return False;
11911 end if;
11913 else
11914 return False;
11915 end if;
11917 else
11918 return False;
11919 end if;
11920 end Names_T;
11922 -- Start of processing for Designates_T
11924 begin
11925 if Nkind (Subt) = N_Identifier then
11926 return Chars (Subt) = Type_Id;
11928 -- Reference can be through an expanded name which has not been
11929 -- analyzed yet, and which designates enclosing scopes.
11931 elsif Nkind (Subt) = N_Selected_Component then
11932 if Names_T (Subt) then
11933 return True;
11935 -- Otherwise it must denote an entity that is already visible.
11936 -- The access definition may name a subtype of the enclosing
11937 -- type, if there is a previous incomplete declaration for it.
11939 else
11940 Find_Selected_Component (Subt);
11941 return
11942 Is_Entity_Name (Subt)
11943 and then Scope (Entity (Subt)) = Current_Scope
11944 and then
11945 (Chars (Base_Type (Entity (Subt))) = Type_Id
11946 or else
11947 (Is_Class_Wide_Type (Entity (Subt))
11948 and then
11949 Chars (Etype (Base_Type (Entity (Subt)))) =
11950 Type_Id));
11951 end if;
11953 -- A reference to the current type may appear as the prefix of
11954 -- a 'Class attribute.
11956 elsif Nkind (Subt) = N_Attribute_Reference
11957 and then Attribute_Name (Subt) = Name_Class
11958 then
11959 return Names_T (Prefix (Subt));
11961 else
11962 return False;
11963 end if;
11964 end Designates_T;
11966 ----------------
11967 -- Mentions_T --
11968 ----------------
11970 function Mentions_T (Acc_Def : Node_Id) return Boolean is
11971 Param_Spec : Node_Id;
11973 Acc_Subprg : constant Node_Id :=
11974 Access_To_Subprogram_Definition (Acc_Def);
11976 begin
11977 if No (Acc_Subprg) then
11978 return Designates_T (Subtype_Mark (Acc_Def));
11979 end if;
11981 -- Component is an access_to_subprogram: examine its formals,
11982 -- and result definition in the case of an access_to_function.
11984 Param_Spec := First (Parameter_Specifications (Acc_Subprg));
11985 while Present (Param_Spec) loop
11986 if Nkind (Parameter_Type (Param_Spec)) = N_Access_Definition
11987 and then Mentions_T (Parameter_Type (Param_Spec))
11988 then
11989 return True;
11991 elsif Designates_T (Parameter_Type (Param_Spec)) then
11992 return True;
11993 end if;
11995 Next (Param_Spec);
11996 end loop;
11998 if Nkind (Acc_Subprg) = N_Access_Function_Definition then
11999 if Nkind (Result_Definition (Acc_Subprg)) =
12000 N_Access_Definition
12001 then
12002 return Mentions_T (Result_Definition (Acc_Subprg));
12003 else
12004 return Designates_T (Result_Definition (Acc_Subprg));
12005 end if;
12006 end if;
12008 return False;
12009 end Mentions_T;
12011 -- Start of processing for Check_Anonymous_Access_Component
12013 begin
12014 if Present (Access_Def) and then Mentions_T (Access_Def) then
12015 Acc_Def := Access_To_Subprogram_Definition (Access_Def);
12017 Build_Incomplete_Type_Declaration;
12018 Anon_Access := Make_Temporary (Loc, 'S');
12020 -- Create a declaration for the anonymous access type: either
12021 -- an access_to_object or an access_to_subprogram.
12023 if Present (Acc_Def) then
12024 if Nkind (Acc_Def) = N_Access_Function_Definition then
12025 Type_Def :=
12026 Make_Access_Function_Definition (Loc,
12027 Parameter_Specifications =>
12028 Parameter_Specifications (Acc_Def),
12029 Result_Definition => Result_Definition (Acc_Def));
12030 else
12031 Type_Def :=
12032 Make_Access_Procedure_Definition (Loc,
12033 Parameter_Specifications =>
12034 Parameter_Specifications (Acc_Def));
12035 end if;
12037 else
12038 Type_Def :=
12039 Make_Access_To_Object_Definition (Loc,
12040 Subtype_Indication =>
12041 Relocate_Node (Subtype_Mark (Access_Def)));
12043 Set_Constant_Present (Type_Def, Constant_Present (Access_Def));
12044 Set_All_Present (Type_Def, All_Present (Access_Def));
12045 end if;
12047 Set_Null_Exclusion_Present
12048 (Type_Def, Null_Exclusion_Present (Access_Def));
12050 Decl :=
12051 Make_Full_Type_Declaration (Loc,
12052 Defining_Identifier => Anon_Access,
12053 Type_Definition => Type_Def);
12055 Insert_Before (Typ_Decl, Decl);
12056 Analyze (Decl);
12058 -- At first sight we could add here the extra formals of an access to
12059 -- subprogram; however, it must delayed till the freeze point so that
12060 -- we know the convention.
12062 if Nkind (Comp_Def) = N_Component_Definition then
12063 Rewrite (Comp_Def,
12064 Make_Component_Definition (Loc,
12065 Subtype_Indication => New_Occurrence_Of (Anon_Access, Loc)));
12066 else
12067 pragma Assert (Nkind (Comp_Def) = N_Discriminant_Specification);
12068 Rewrite (Comp_Def,
12069 Make_Discriminant_Specification (Loc,
12070 Defining_Identifier => Defining_Identifier (Comp_Def),
12071 Discriminant_Type => New_Occurrence_Of (Anon_Access, Loc)));
12072 end if;
12074 if Ekind (Designated_Type (Anon_Access)) = E_Subprogram_Type then
12075 Mutate_Ekind (Anon_Access, E_Anonymous_Access_Subprogram_Type);
12076 else
12077 Mutate_Ekind (Anon_Access, E_Anonymous_Access_Type);
12078 end if;
12080 Set_Is_Local_Anonymous_Access (Anon_Access);
12081 end if;
12082 end Check_Anonymous_Access_Component;
12084 ---------------------------------------
12085 -- Check_Anonymous_Access_Components --
12086 ---------------------------------------
12088 procedure Check_Anonymous_Access_Components
12089 (Typ_Decl : Node_Id;
12090 Typ : Entity_Id;
12091 Prev : Entity_Id;
12092 Comp_List : Node_Id)
12094 Comp : Node_Id;
12095 begin
12096 if No (Comp_List) then
12097 return;
12098 end if;
12100 Set_Is_Not_Self_Hidden (Typ);
12102 Comp := First (Component_Items (Comp_List));
12103 while Present (Comp) loop
12104 if Nkind (Comp) = N_Component_Declaration then
12105 Check_Anonymous_Access_Component
12106 (Typ_Decl, Typ, Prev,
12107 Component_Definition (Comp),
12108 Access_Definition (Component_Definition (Comp)));
12109 end if;
12111 Next (Comp);
12112 end loop;
12114 if Present (Variant_Part (Comp_List)) then
12115 declare
12116 V : Node_Id;
12117 begin
12118 V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
12119 while Present (V) loop
12120 Check_Anonymous_Access_Components
12121 (Typ_Decl, Typ, Prev, Component_List (V));
12122 Next_Non_Pragma (V);
12123 end loop;
12124 end;
12125 end if;
12126 end Check_Anonymous_Access_Components;
12128 ----------------------
12129 -- Check_Completion --
12130 ----------------------
12132 procedure Check_Completion (Body_Id : Node_Id := Empty) is
12133 E : Entity_Id;
12135 procedure Post_Error;
12136 -- Post error message for lack of completion for entity E
12138 ----------------
12139 -- Post_Error --
12140 ----------------
12142 procedure Post_Error is
12143 procedure Missing_Body;
12144 -- Output missing body message
12146 ------------------
12147 -- Missing_Body --
12148 ------------------
12150 procedure Missing_Body is
12151 begin
12152 -- Spec is in same unit, so we can post on spec
12154 if In_Same_Source_Unit (Body_Id, E) then
12155 Error_Msg_N ("missing body for &", E);
12157 -- Spec is in a separate unit, so we have to post on the body
12159 else
12160 Error_Msg_NE ("missing body for & declared#!", Body_Id, E);
12161 end if;
12162 end Missing_Body;
12164 -- Start of processing for Post_Error
12166 begin
12167 if not Comes_From_Source (E) then
12168 if Ekind (E) in E_Task_Type | E_Protected_Type then
12170 -- It may be an anonymous protected type created for a
12171 -- single variable. Post error on variable, if present.
12173 declare
12174 Var : Entity_Id;
12176 begin
12177 Var := First_Entity (Current_Scope);
12178 while Present (Var) loop
12179 exit when Etype (Var) = E
12180 and then Comes_From_Source (Var);
12182 Next_Entity (Var);
12183 end loop;
12185 if Present (Var) then
12186 E := Var;
12187 end if;
12188 end;
12189 end if;
12190 end if;
12192 -- If a generated entity has no completion, then either previous
12193 -- semantic errors have disabled the expansion phase, or else we had
12194 -- missing subunits, or else we are compiling without expansion,
12195 -- or else something is very wrong.
12197 if not Comes_From_Source (E) then
12198 pragma Assert
12199 (Serious_Errors_Detected > 0
12200 or else Configurable_Run_Time_Violations > 0
12201 or else Subunits_Missing
12202 or else not Expander_Active);
12203 return;
12205 -- Here for source entity
12207 else
12208 -- Here if no body to post the error message, so we post the error
12209 -- on the declaration that has no completion. This is not really
12210 -- the right place to post it, think about this later ???
12212 if No (Body_Id) then
12213 if Is_Type (E) then
12214 Error_Msg_NE
12215 ("missing full declaration for }", Parent (E), E);
12216 else
12217 Error_Msg_NE ("missing body for &", Parent (E), E);
12218 end if;
12220 -- Package body has no completion for a declaration that appears
12221 -- in the corresponding spec. Post error on the body, with a
12222 -- reference to the non-completed declaration.
12224 else
12225 Error_Msg_Sloc := Sloc (E);
12227 if Is_Type (E) then
12228 Error_Msg_NE ("missing full declaration for }!", Body_Id, E);
12230 elsif Is_Overloadable (E)
12231 and then Current_Entity_In_Scope (E) /= E
12232 then
12233 -- It may be that the completion is mistyped and appears as
12234 -- a distinct overloading of the entity.
12236 declare
12237 Candidate : constant Entity_Id :=
12238 Current_Entity_In_Scope (E);
12239 Decl : constant Node_Id :=
12240 Unit_Declaration_Node (Candidate);
12242 begin
12243 if Is_Overloadable (Candidate)
12244 and then Ekind (Candidate) = Ekind (E)
12245 and then Nkind (Decl) = N_Subprogram_Body
12246 and then Acts_As_Spec (Decl)
12247 then
12248 Check_Type_Conformant (Candidate, E);
12250 else
12251 Missing_Body;
12252 end if;
12253 end;
12255 else
12256 Missing_Body;
12257 end if;
12258 end if;
12259 end if;
12260 end Post_Error;
12262 -- Local variables
12264 Pack_Id : constant Entity_Id := Current_Scope;
12266 -- Start of processing for Check_Completion
12268 begin
12269 E := First_Entity (Pack_Id);
12270 while Present (E) loop
12271 if Is_Intrinsic_Subprogram (E) then
12272 null;
12274 -- The following situation requires special handling: a child unit
12275 -- that appears in the context clause of the body of its parent:
12277 -- procedure Parent.Child (...);
12279 -- with Parent.Child;
12280 -- package body Parent is
12282 -- Here Parent.Child appears as a local entity, but should not be
12283 -- flagged as requiring completion, because it is a compilation
12284 -- unit.
12286 -- Ignore missing completion for a subprogram that does not come from
12287 -- source (including the _Call primitive operation of RAS types,
12288 -- which has to have the flag Comes_From_Source for other purposes):
12289 -- we assume that the expander will provide the missing completion.
12290 -- In case of previous errors, other expansion actions that provide
12291 -- bodies for null procedures with not be invoked, so inhibit message
12292 -- in those cases.
12294 -- Note that E_Operator is not in the list that follows, because
12295 -- this kind is reserved for predefined operators, that are
12296 -- intrinsic and do not need completion.
12298 elsif Ekind (E) in E_Function
12299 | E_Procedure
12300 | E_Generic_Function
12301 | E_Generic_Procedure
12302 then
12303 if Has_Completion (E) then
12304 null;
12306 elsif Is_Subprogram (E) and then Is_Abstract_Subprogram (E) then
12307 null;
12309 elsif Is_Subprogram (E)
12310 and then (not Comes_From_Source (E)
12311 or else Chars (E) = Name_uCall)
12312 then
12313 null;
12315 elsif
12316 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
12317 then
12318 null;
12320 elsif Nkind (Parent (E)) = N_Procedure_Specification
12321 and then Null_Present (Parent (E))
12322 and then Serious_Errors_Detected > 0
12323 then
12324 null;
12326 else
12327 Post_Error;
12328 end if;
12330 elsif Is_Entry (E) then
12331 if not Has_Completion (E)
12332 and then Ekind (Scope (E)) = E_Protected_Type
12333 then
12334 Post_Error;
12335 end if;
12337 elsif Is_Package_Or_Generic_Package (E) then
12338 if Unit_Requires_Body (E) then
12339 if not Has_Completion (E)
12340 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
12341 N_Compilation_Unit
12342 then
12343 Post_Error;
12344 end if;
12346 elsif not Is_Child_Unit (E) then
12347 May_Need_Implicit_Body (E);
12348 end if;
12350 -- A formal incomplete type (Ada 2012) does not require a completion;
12351 -- other incomplete type declarations do.
12353 elsif Ekind (E) = E_Incomplete_Type then
12354 if No (Underlying_Type (E))
12355 and then not Is_Generic_Type (E)
12356 then
12357 Post_Error;
12358 end if;
12360 elsif Ekind (E) in E_Task_Type | E_Protected_Type then
12361 if not Has_Completion (E) then
12362 Post_Error;
12363 end if;
12365 -- A single task declared in the current scope is a constant, verify
12366 -- that the body of its anonymous type is in the same scope. If the
12367 -- task is defined elsewhere, this may be a renaming declaration for
12368 -- which no completion is needed.
12370 elsif Ekind (E) = E_Constant then
12371 if Ekind (Etype (E)) = E_Task_Type
12372 and then not Has_Completion (Etype (E))
12373 and then Scope (Etype (E)) = Current_Scope
12374 then
12375 Post_Error;
12376 end if;
12378 elsif Ekind (E) = E_Record_Type then
12379 if Is_Tagged_Type (E) then
12380 Check_Abstract_Overriding (E);
12381 Check_Conventions (E);
12382 end if;
12384 Check_Aliased_Component_Types (E);
12386 elsif Ekind (E) = E_Array_Type then
12387 Check_Aliased_Component_Types (E);
12389 end if;
12391 Next_Entity (E);
12392 end loop;
12393 end Check_Completion;
12395 -------------------------------------
12396 -- Check_Constraining_Discriminant --
12397 -------------------------------------
12399 procedure Check_Constraining_Discriminant (New_Disc, Old_Disc : Entity_Id)
12401 New_Type : constant Entity_Id := Etype (New_Disc);
12402 Old_Type : Entity_Id;
12404 begin
12405 -- If the record type contains an array constrained by the discriminant
12406 -- but with some different bound, the compiler tries to create a smaller
12407 -- range for the discriminant type (see exp_ch3.Adjust_Discriminants).
12408 -- In this case, where the discriminant type is a scalar type, the check
12409 -- must use the original discriminant type in the parent declaration.
12411 if Is_Scalar_Type (New_Type) then
12412 Old_Type := Entity (Discriminant_Type (Parent (Old_Disc)));
12413 else
12414 Old_Type := Etype (Old_Disc);
12415 end if;
12417 if not Subtypes_Statically_Compatible (New_Type, Old_Type) then
12418 Error_Msg_N
12419 ("subtype must be statically compatible with parent discriminant",
12420 New_Disc);
12422 if not Predicates_Compatible (New_Type, Old_Type) then
12423 Error_Msg_N
12424 ("\subtype predicate is not compatible with parent discriminant",
12425 New_Disc);
12426 end if;
12427 end if;
12428 end Check_Constraining_Discriminant;
12430 ------------------------------------
12431 -- Check_CPP_Type_Has_No_Defaults --
12432 ------------------------------------
12434 procedure Check_CPP_Type_Has_No_Defaults (T : Entity_Id) is
12435 Tdef : constant Node_Id := Type_Definition (Declaration_Node (T));
12436 Clist : Node_Id;
12437 Comp : Node_Id;
12439 begin
12440 -- Obtain the component list
12442 if Nkind (Tdef) = N_Record_Definition then
12443 Clist := Component_List (Tdef);
12444 else pragma Assert (Nkind (Tdef) = N_Derived_Type_Definition);
12445 Clist := Component_List (Record_Extension_Part (Tdef));
12446 end if;
12448 -- Check all components to ensure no default expressions
12450 if Present (Clist) then
12451 Comp := First_Non_Pragma (Component_Items (Clist));
12452 while Present (Comp) loop
12453 if Present (Expression (Comp)) then
12454 Error_Msg_N
12455 ("component of imported 'C'P'P type cannot have "
12456 & "default expression", Expression (Comp));
12457 end if;
12459 Next_Non_Pragma (Comp);
12460 end loop;
12461 end if;
12462 end Check_CPP_Type_Has_No_Defaults;
12464 ----------------------------
12465 -- Check_Delta_Expression --
12466 ----------------------------
12468 procedure Check_Delta_Expression (E : Node_Id) is
12469 begin
12470 if not Is_Real_Type (Etype (E)) then
12471 Wrong_Type (E, Any_Real);
12473 elsif not Is_OK_Static_Expression (E) then
12474 Flag_Non_Static_Expr
12475 ("non-static expression used for delta value!", E);
12477 elsif not UR_Is_Positive (Expr_Value_R (E)) then
12478 Error_Msg_N ("delta expression must be positive", E);
12480 else
12481 return;
12482 end if;
12484 -- If any of above errors occurred, then replace the incorrect
12485 -- expression by the real 0.1, which should prevent further errors.
12487 Rewrite (E,
12488 Make_Real_Literal (Sloc (E), Ureal_Tenth));
12489 Analyze_And_Resolve (E, Standard_Float);
12490 end Check_Delta_Expression;
12492 -----------------------------
12493 -- Check_Digits_Expression --
12494 -----------------------------
12496 procedure Check_Digits_Expression (E : Node_Id) is
12497 begin
12498 if not Is_Integer_Type (Etype (E)) then
12499 Wrong_Type (E, Any_Integer);
12501 elsif not Is_OK_Static_Expression (E) then
12502 Flag_Non_Static_Expr
12503 ("non-static expression used for digits value!", E);
12505 elsif Expr_Value (E) <= 0 then
12506 Error_Msg_N ("digits value must be greater than zero", E);
12508 else
12509 return;
12510 end if;
12512 -- If any of above errors occurred, then replace the incorrect
12513 -- expression by the integer 1, which should prevent further errors.
12515 Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
12516 Analyze_And_Resolve (E, Standard_Integer);
12518 end Check_Digits_Expression;
12520 --------------------------
12521 -- Check_Initialization --
12522 --------------------------
12524 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
12525 begin
12526 -- Special processing for limited types
12528 if Is_Limited_Type (T)
12529 and then not In_Instance
12530 and then not In_Inlined_Body
12531 then
12532 if not OK_For_Limited_Init (T, Exp) then
12534 -- In GNAT mode, this is just a warning, to allow it to be evilly
12535 -- turned off. Otherwise it is a real error.
12537 if GNAT_Mode then
12538 Error_Msg_N
12539 ("??cannot initialize entities of limited type!", Exp);
12541 elsif Ada_Version < Ada_2005 then
12543 -- The side effect removal machinery may generate illegal Ada
12544 -- code to avoid the usage of access types and 'reference in
12545 -- SPARK mode. Since this is legal code with respect to theorem
12546 -- proving, do not emit the error.
12548 if GNATprove_Mode
12549 and then Nkind (Exp) = N_Function_Call
12550 and then Nkind (Parent (Exp)) = N_Object_Declaration
12551 and then not Comes_From_Source
12552 (Defining_Identifier (Parent (Exp)))
12553 then
12554 null;
12556 else
12557 Error_Msg_N
12558 ("cannot initialize entities of limited type", Exp);
12559 Explain_Limited_Type (T, Exp);
12560 end if;
12562 else
12563 -- Specialize error message according to kind of illegal
12564 -- initial expression. We check the Original_Node to cover
12565 -- cases where the initialization expression of an object
12566 -- declaration generated by the compiler has been rewritten
12567 -- (such as for dispatching calls).
12569 if Nkind (Original_Node (Exp)) = N_Type_Conversion
12570 and then
12571 Nkind (Expression (Original_Node (Exp))) = N_Function_Call
12572 then
12573 -- No error for internally-generated object declarations,
12574 -- which can come from build-in-place assignment statements.
12576 if Nkind (Parent (Exp)) = N_Object_Declaration
12577 and then not Comes_From_Source
12578 (Defining_Identifier (Parent (Exp)))
12579 then
12580 null;
12582 else
12583 Error_Msg_N
12584 ("illegal context for call to function with limited "
12585 & "result", Exp);
12586 end if;
12588 else
12589 Error_Msg_N
12590 ("initialization of limited object requires aggregate or "
12591 & "function call", Exp);
12592 end if;
12593 end if;
12594 end if;
12595 end if;
12597 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag gets
12598 -- set unless we can be sure that no range check is required.
12600 if not Expander_Active
12601 and then Is_Scalar_Type (T)
12602 and then not Is_In_Range (Exp, T, Assume_Valid => True)
12603 then
12604 Set_Do_Range_Check (Exp);
12605 end if;
12606 end Check_Initialization;
12608 ----------------------
12609 -- Check_Interfaces --
12610 ----------------------
12612 procedure Check_Interfaces (N : Node_Id; Def : Node_Id) is
12613 Parent_Type : constant Entity_Id := Etype (Defining_Identifier (N));
12615 Iface : Node_Id;
12616 Iface_Def : Node_Id;
12617 Iface_Typ : Entity_Id;
12618 Parent_Node : Node_Id;
12620 Is_Task : Boolean := False;
12621 -- Set True if parent type or any progenitor is a task interface
12623 Is_Protected : Boolean := False;
12624 -- Set True if parent type or any progenitor is a protected interface
12626 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
12627 -- Check that a progenitor is compatible with declaration. If an error
12628 -- message is output, it is posted on Error_Node.
12630 ------------------
12631 -- Check_Ifaces --
12632 ------------------
12634 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
12635 Iface_Id : constant Entity_Id :=
12636 Defining_Identifier (Parent (Iface_Def));
12637 Type_Def : Node_Id;
12639 begin
12640 if Nkind (N) = N_Private_Extension_Declaration then
12641 Type_Def := N;
12642 else
12643 Type_Def := Type_Definition (N);
12644 end if;
12646 if Is_Task_Interface (Iface_Id) then
12647 Is_Task := True;
12649 elsif Is_Protected_Interface (Iface_Id) then
12650 Is_Protected := True;
12651 end if;
12653 if Is_Synchronized_Interface (Iface_Id) then
12655 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
12656 -- extension derived from a synchronized interface must explicitly
12657 -- be declared synchronized, because the full view will be a
12658 -- synchronized type.
12660 if Nkind (N) = N_Private_Extension_Declaration then
12661 if not Synchronized_Present (N) then
12662 Error_Msg_NE
12663 ("private extension of& must be explicitly synchronized",
12664 N, Iface_Id);
12665 end if;
12667 -- However, by 3.9.4(16/2), a full type that is a record extension
12668 -- is never allowed to derive from a synchronized interface (note
12669 -- that interfaces must be excluded from this check, because those
12670 -- are represented by derived type definitions in some cases).
12672 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition
12673 and then not Interface_Present (Type_Definition (N))
12674 then
12675 Error_Msg_N ("record extension cannot derive from synchronized "
12676 & "interface", Error_Node);
12677 end if;
12678 end if;
12680 -- Check that the characteristics of the progenitor are compatible
12681 -- with the explicit qualifier in the declaration.
12682 -- The check only applies to qualifiers that come from source.
12683 -- Limited_Present also appears in the declaration of corresponding
12684 -- records, and the check does not apply to them.
12686 if Limited_Present (Type_Def)
12687 and then not
12688 Is_Concurrent_Record_Type (Defining_Identifier (N))
12689 then
12690 if Is_Limited_Interface (Parent_Type)
12691 and then not Is_Limited_Interface (Iface_Id)
12692 then
12693 Error_Msg_NE
12694 ("progenitor & must be limited interface",
12695 Error_Node, Iface_Id);
12697 elsif
12698 (Task_Present (Iface_Def)
12699 or else Protected_Present (Iface_Def)
12700 or else Synchronized_Present (Iface_Def))
12701 and then Nkind (N) /= N_Private_Extension_Declaration
12702 and then not Error_Posted (N)
12703 then
12704 Error_Msg_NE
12705 ("progenitor & must be limited interface",
12706 Error_Node, Iface_Id);
12707 end if;
12709 -- Protected interfaces can only inherit from limited, synchronized
12710 -- or protected interfaces.
12712 elsif Nkind (N) = N_Full_Type_Declaration
12713 and then Protected_Present (Type_Def)
12714 then
12715 if Limited_Present (Iface_Def)
12716 or else Synchronized_Present (Iface_Def)
12717 or else Protected_Present (Iface_Def)
12718 then
12719 null;
12721 elsif Task_Present (Iface_Def) then
12722 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
12723 & "from task interface", Error_Node);
12725 else
12726 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
12727 & "from non-limited interface", Error_Node);
12728 end if;
12730 -- Ada 2005 (AI-345): Synchronized interfaces can only inherit from
12731 -- limited and synchronized.
12733 elsif Synchronized_Present (Type_Def) then
12734 if Limited_Present (Iface_Def)
12735 or else Synchronized_Present (Iface_Def)
12736 then
12737 null;
12739 elsif Protected_Present (Iface_Def)
12740 and then Nkind (N) /= N_Private_Extension_Declaration
12741 then
12742 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
12743 & "from protected interface", Error_Node);
12745 elsif Task_Present (Iface_Def)
12746 and then Nkind (N) /= N_Private_Extension_Declaration
12747 then
12748 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
12749 & "from task interface", Error_Node);
12751 elsif not Is_Limited_Interface (Iface_Id) then
12752 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
12753 & "from non-limited interface", Error_Node);
12754 end if;
12756 -- Ada 2005 (AI-345): Task interfaces can only inherit from limited,
12757 -- synchronized or task interfaces.
12759 elsif Nkind (N) = N_Full_Type_Declaration
12760 and then Task_Present (Type_Def)
12761 then
12762 if Limited_Present (Iface_Def)
12763 or else Synchronized_Present (Iface_Def)
12764 or else Task_Present (Iface_Def)
12765 then
12766 null;
12768 elsif Protected_Present (Iface_Def) then
12769 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
12770 & "protected interface", Error_Node);
12772 else
12773 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
12774 & "non-limited interface", Error_Node);
12775 end if;
12776 end if;
12777 end Check_Ifaces;
12779 -- Start of processing for Check_Interfaces
12781 begin
12782 if Is_Interface (Parent_Type) then
12783 if Is_Task_Interface (Parent_Type) then
12784 Is_Task := True;
12786 elsif Is_Protected_Interface (Parent_Type) then
12787 Is_Protected := True;
12788 end if;
12789 end if;
12791 if Nkind (N) = N_Private_Extension_Declaration then
12793 -- Check that progenitors are compatible with declaration
12795 Iface := First (Interface_List (Def));
12796 while Present (Iface) loop
12797 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
12799 Parent_Node := Parent (Base_Type (Iface_Typ));
12800 Iface_Def := Type_Definition (Parent_Node);
12802 if not Is_Interface (Iface_Typ) then
12803 Diagnose_Interface (Iface, Iface_Typ);
12804 else
12805 Check_Ifaces (Iface_Def, Iface);
12806 end if;
12808 Next (Iface);
12809 end loop;
12811 if Is_Task and Is_Protected then
12812 Error_Msg_N
12813 ("type cannot derive from task and protected interface", N);
12814 end if;
12816 return;
12817 end if;
12819 -- Full type declaration of derived type.
12820 -- Check compatibility with parent if it is interface type
12822 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
12823 and then Is_Interface (Parent_Type)
12824 then
12825 Parent_Node := Parent (Parent_Type);
12827 -- More detailed checks for interface varieties
12829 Check_Ifaces
12830 (Iface_Def => Type_Definition (Parent_Node),
12831 Error_Node => Subtype_Indication (Type_Definition (N)));
12832 end if;
12834 Iface := First (Interface_List (Def));
12835 while Present (Iface) loop
12836 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
12838 Parent_Node := Parent (Base_Type (Iface_Typ));
12839 Iface_Def := Type_Definition (Parent_Node);
12841 if not Is_Interface (Iface_Typ) then
12842 Diagnose_Interface (Iface, Iface_Typ);
12844 else
12845 -- "The declaration of a specific descendant of an interface
12846 -- type freezes the interface type" RM 13.14
12848 Freeze_Before (N, Iface_Typ);
12849 Check_Ifaces (Iface_Def, Error_Node => Iface);
12850 end if;
12852 Next (Iface);
12853 end loop;
12855 if Is_Task and Is_Protected then
12856 Error_Msg_N
12857 ("type cannot derive from task and protected interface", N);
12858 end if;
12859 end Check_Interfaces;
12861 ------------------------------------
12862 -- Check_Or_Process_Discriminants --
12863 ------------------------------------
12865 -- If an incomplete or private type declaration was already given for the
12866 -- type, the discriminants may have already been processed if they were
12867 -- present on the incomplete declaration. In this case a full conformance
12868 -- check has been performed in Find_Type_Name, and we then recheck here
12869 -- some properties that can't be checked on the partial view alone.
12870 -- Otherwise we call Process_Discriminants.
12872 procedure Check_Or_Process_Discriminants
12873 (N : Node_Id;
12874 T : Entity_Id;
12875 Prev : Entity_Id := Empty)
12877 begin
12878 if Has_Discriminants (T) then
12880 -- Discriminants are already set on T if they were already present
12881 -- on the partial view. Make them visible to component declarations.
12883 declare
12884 D : Entity_Id;
12885 -- Discriminant on T (full view) referencing expr on partial view
12887 Prev_D : Entity_Id;
12888 -- Entity of corresponding discriminant on partial view
12890 New_D : Node_Id;
12891 -- Discriminant specification for full view, expression is
12892 -- the syntactic copy on full view (which has been checked for
12893 -- conformance with partial view), only used here to post error
12894 -- message.
12896 begin
12897 D := First_Discriminant (T);
12898 New_D := First (Discriminant_Specifications (N));
12899 while Present (D) loop
12900 Prev_D := Current_Entity (D);
12901 Set_Current_Entity (D);
12902 Set_Is_Immediately_Visible (D);
12903 Set_Homonym (D, Prev_D);
12905 -- Handle the case where there is an untagged partial view and
12906 -- the full view is tagged: must disallow discriminants with
12907 -- defaults, unless compiling for Ada 2012, which allows a
12908 -- limited tagged type to have defaulted discriminants (see
12909 -- AI05-0214). However, suppress error here if it was already
12910 -- reported on the default expression of the partial view.
12912 if Is_Tagged_Type (T)
12913 and then Present (Expression (Parent (D)))
12914 and then (not Is_Limited_Type (Current_Scope)
12915 or else Ada_Version < Ada_2012)
12916 and then not Error_Posted (Expression (Parent (D)))
12917 then
12918 if Ada_Version >= Ada_2012 then
12919 Error_Msg_N
12920 ("discriminants of nonlimited tagged type cannot have "
12921 & "defaults",
12922 Expression (New_D));
12923 else
12924 Error_Msg_N
12925 ("discriminants of tagged type cannot have defaults",
12926 Expression (New_D));
12927 end if;
12928 end if;
12930 -- Ada 2005 (AI-230): Access discriminant allowed in
12931 -- non-limited record types.
12933 if Ada_Version < Ada_2005 then
12935 -- This restriction gets applied to the full type here. It
12936 -- has already been applied earlier to the partial view.
12938 Check_Access_Discriminant_Requires_Limited (Parent (D), N);
12939 end if;
12941 Next_Discriminant (D);
12942 Next (New_D);
12943 end loop;
12944 end;
12946 elsif Present (Discriminant_Specifications (N)) then
12947 Process_Discriminants (N, Prev);
12948 end if;
12949 end Check_Or_Process_Discriminants;
12951 ----------------------
12952 -- Check_Real_Bound --
12953 ----------------------
12955 procedure Check_Real_Bound (Bound : Node_Id) is
12956 begin
12957 if not Is_Real_Type (Etype (Bound)) then
12958 Error_Msg_N
12959 ("bound in real type definition must be of real type", Bound);
12961 elsif not Is_OK_Static_Expression (Bound) then
12962 Flag_Non_Static_Expr
12963 ("non-static expression used for real type bound!", Bound);
12965 else
12966 return;
12967 end if;
12969 Rewrite
12970 (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
12971 Analyze (Bound);
12972 Resolve (Bound, Standard_Float);
12973 end Check_Real_Bound;
12975 ------------------------------
12976 -- Complete_Private_Subtype --
12977 ------------------------------
12979 procedure Complete_Private_Subtype
12980 (Priv : Entity_Id;
12981 Full : Entity_Id;
12982 Full_Base : Entity_Id;
12983 Related_Nod : Node_Id)
12985 Save_Next_Entity : Entity_Id;
12986 Save_Homonym : Entity_Id;
12988 begin
12989 -- Set semantic attributes for (implicit) private subtype completion.
12990 -- If the full type has no discriminants, then it is a copy of the
12991 -- full view of the base. Otherwise, it is a subtype of the base with
12992 -- a possible discriminant constraint. Save and restore the original
12993 -- Next_Entity field of full to ensure that the calls to Copy_Node do
12994 -- not corrupt the entity chain.
12996 Save_Next_Entity := Next_Entity (Full);
12997 Save_Homonym := Homonym (Priv);
12999 if Is_Private_Type (Full_Base)
13000 or else Is_Record_Type (Full_Base)
13001 or else Is_Concurrent_Type (Full_Base)
13002 then
13003 Copy_Node (Priv, Full);
13005 -- Note that the Etype of the full view is the same as the Etype of
13006 -- the partial view. In this fashion, the subtype has access to the
13007 -- correct view of the parent.
13009 Set_Has_Discriminants (Full, Has_Discriminants (Full_Base));
13010 Set_Has_Unknown_Discriminants
13011 (Full, Has_Unknown_Discriminants (Full_Base));
13012 Set_First_Entity (Full, First_Entity (Full_Base));
13013 Set_Last_Entity (Full, Last_Entity (Full_Base));
13015 -- If the underlying base type is constrained, we know that the
13016 -- full view of the subtype is constrained as well (the converse
13017 -- is not necessarily true).
13019 if Is_Constrained (Full_Base) then
13020 Set_Is_Constrained (Full);
13021 end if;
13023 else
13024 Copy_Node (Full_Base, Full);
13026 -- The following subtlety with the Etype of the full view needs to be
13027 -- taken into account here. One could think that it must naturally be
13028 -- set to the base type of the full base:
13030 -- Set_Etype (Full, Base_Type (Full_Base));
13032 -- so that the full view becomes a subtype of the full base when the
13033 -- latter is a base type, which must for example happen when the full
13034 -- base is declared as derived type. That's also correct if the full
13035 -- base is declared as an array type, or a floating-point type, or a
13036 -- fixed-point type, or a signed integer type, as these declarations
13037 -- create an implicit base type and a first subtype so the Etype of
13038 -- the full views must be the implicit base type. But that's wrong
13039 -- if the full base is declared as an access type, or an enumeration
13040 -- type, or a modular integer type, as these declarations directly
13041 -- create a base type, i.e. with Etype pointing to itself. Moreover
13042 -- the full base being declared in the private part, i.e. when the
13043 -- views are swapped, the end result is that the Etype of the full
13044 -- base is set to its private view in this case and that we need to
13045 -- propagate this setting to the full view in order for the subtype
13046 -- to be compatible with the base type.
13048 if Is_Base_Type (Full_Base)
13049 and then (Is_Derived_Type (Full_Base)
13050 or else Ekind (Full_Base) in Array_Kind
13051 or else Ekind (Full_Base) in Fixed_Point_Kind
13052 or else Ekind (Full_Base) in Float_Kind
13053 or else Ekind (Full_Base) in Signed_Integer_Kind)
13054 then
13055 Set_Etype (Full, Full_Base);
13056 end if;
13058 Set_Chars (Full, Chars (Priv));
13059 Set_Sloc (Full, Sloc (Priv));
13060 Conditional_Delay (Full, Priv);
13061 end if;
13063 Link_Entities (Full, Save_Next_Entity);
13064 Set_Homonym (Full, Save_Homonym);
13065 Set_Associated_Node_For_Itype (Full, Related_Nod);
13067 if Ekind (Full) in Incomplete_Or_Private_Kind then
13068 Reinit_Field_To_Zero (Full, F_Private_Dependents);
13069 end if;
13071 -- Set common attributes for all subtypes: kind, convention, etc.
13073 Mutate_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
13074 Set_Is_Not_Self_Hidden (Full);
13075 Set_Convention (Full, Convention (Full_Base));
13076 Set_Is_First_Subtype (Full, False);
13077 Set_Scope (Full, Scope (Priv));
13078 Set_Size_Info (Full, Full_Base);
13079 Copy_RM_Size (To => Full, From => Full_Base);
13080 Set_Is_Itype (Full);
13082 -- A subtype of a private-type-without-discriminants, whose full-view
13083 -- has discriminants with default expressions, is not constrained.
13085 if not Has_Discriminants (Priv) then
13086 Set_Is_Constrained (Full, Is_Constrained (Full_Base));
13088 if Has_Discriminants (Full_Base) then
13089 Set_Discriminant_Constraint
13090 (Full, Discriminant_Constraint (Full_Base));
13092 -- The partial view may have been indefinite, the full view
13093 -- might not be.
13095 Set_Has_Unknown_Discriminants
13096 (Full, Has_Unknown_Discriminants (Full_Base));
13097 end if;
13098 end if;
13100 Set_First_Rep_Item (Full, First_Rep_Item (Full_Base));
13101 Set_Depends_On_Private (Full, Has_Private_Component (Full));
13103 -- When prefixed calls are enabled for untagged types, the subtype
13104 -- shares the primitive operations of its base type. Do this even
13105 -- when GNAT extensions are not allowed, in order to give better
13106 -- error messages.
13108 Set_Direct_Primitive_Operations
13109 (Full, Direct_Primitive_Operations (Full_Base));
13111 -- Freeze the private subtype entity if its parent is delayed, and not
13112 -- already frozen. We skip this processing if the type is an anonymous
13113 -- subtype of a record component, or is the corresponding record of a
13114 -- protected type, since these are processed when the enclosing type
13115 -- is frozen. If the parent type is declared in a nested package then
13116 -- the freezing of the private and full views also happens later.
13118 if not Is_Type (Scope (Full)) then
13119 if Is_Itype (Priv)
13120 and then In_Same_Source_Unit (Full, Full_Base)
13121 and then Scope (Full_Base) /= Scope (Full)
13122 then
13123 Set_Has_Delayed_Freeze (Full);
13124 Set_Has_Delayed_Freeze (Priv);
13126 else
13127 Set_Has_Delayed_Freeze (Full,
13128 Has_Delayed_Freeze (Full_Base)
13129 and then not Is_Frozen (Full_Base));
13130 end if;
13131 end if;
13133 Set_Freeze_Node (Full, Empty);
13134 Set_Is_Frozen (Full, False);
13136 if Has_Discriminants (Full) then
13137 Set_Stored_Constraint_From_Discriminant_Constraint (Full);
13138 Set_Stored_Constraint (Priv, Stored_Constraint (Full));
13140 if Has_Unknown_Discriminants (Full) then
13141 Set_Discriminant_Constraint (Full, No_Elist);
13142 end if;
13143 end if;
13145 if Ekind (Full_Base) = E_Record_Type
13146 and then Has_Discriminants (Full_Base)
13147 and then Has_Discriminants (Priv) -- might not, if errors
13148 and then not Has_Unknown_Discriminants (Priv)
13149 and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
13150 then
13151 Create_Constrained_Components
13152 (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
13154 -- If the full base is itself derived from private, build a congruent
13155 -- subtype of its underlying full view, for use by the back end.
13157 elsif Is_Private_Type (Full_Base)
13158 and then Present (Underlying_Full_View (Full_Base))
13159 then
13160 declare
13161 Underlying_Full_Base : constant Entity_Id :=
13162 Underlying_Full_View (Full_Base);
13163 Underlying_Full : constant Entity_Id :=
13164 Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
13165 begin
13166 Set_Is_Itype (Underlying_Full);
13167 Set_Associated_Node_For_Itype (Underlying_Full, Related_Nod);
13168 Complete_Private_Subtype
13169 (Priv, Underlying_Full, Underlying_Full_Base, Related_Nod);
13170 Set_Underlying_Full_View (Full, Underlying_Full);
13171 Set_Is_Underlying_Full_View (Underlying_Full);
13172 end;
13174 elsif Is_Record_Type (Full_Base) then
13176 -- Show Full is simply a renaming of Full_Base
13178 Set_Cloned_Subtype (Full, Full_Base);
13179 Set_Is_Limited_Record (Full, Is_Limited_Record (Full_Base));
13181 -- Propagate predicates
13183 Propagate_Predicate_Attributes (Full, Full_Base);
13184 end if;
13186 -- It is unsafe to share the bounds of a scalar type, because the Itype
13187 -- is elaborated on demand, and if a bound is nonstatic, then different
13188 -- orders of elaboration in different units will lead to different
13189 -- external symbols.
13191 if Is_Scalar_Type (Full_Base) then
13192 Set_Scalar_Range (Full,
13193 Make_Range (Sloc (Related_Nod),
13194 Low_Bound =>
13195 Duplicate_Subexpr_No_Checks (Type_Low_Bound (Full_Base)),
13196 High_Bound =>
13197 Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
13199 -- This completion inherits the bounds of the full parent, but if
13200 -- the parent is an unconstrained floating point type, so is the
13201 -- completion.
13203 if Is_Floating_Point_Type (Full_Base) then
13204 Set_Includes_Infinities
13205 (Scalar_Range (Full), Has_Infinities (Full_Base));
13206 end if;
13207 end if;
13209 -- ??? It seems that a lot of fields are missing that should be copied
13210 -- from Full_Base to Full. Here are some that are introduced in a
13211 -- non-disruptive way but a cleanup is necessary.
13213 if Is_Tagged_Type (Full_Base) then
13214 Set_Is_Tagged_Type (Full);
13215 Set_Is_Limited_Record (Full, Is_Limited_Record (Full_Base));
13217 Set_No_Tagged_Streams_Pragma
13218 (Full, No_Tagged_Streams_Pragma (Full_Base));
13220 if Is_Interface (Full_Base) then
13221 Set_Is_Interface (Full);
13222 Set_Is_Limited_Interface (Full, Is_Limited_Interface (Full_Base));
13223 end if;
13225 -- Inherit class_wide type of full_base in case the partial view was
13226 -- not tagged. Otherwise it has already been created when the private
13227 -- subtype was analyzed.
13229 if No (Class_Wide_Type (Full)) then
13230 Set_Class_Wide_Type (Full, Class_Wide_Type (Full_Base));
13231 end if;
13233 -- If this is a subtype of a protected or task type, constrain its
13234 -- corresponding record, unless this is a subtype without constraints,
13235 -- i.e. a simple renaming as with an actual subtype in an instance.
13237 elsif Is_Concurrent_Type (Full_Base) then
13238 if Has_Discriminants (Full)
13239 and then Present (Corresponding_Record_Type (Full_Base))
13240 and then
13241 not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
13242 then
13243 Set_Corresponding_Record_Type (Full,
13244 Constrain_Corresponding_Record
13245 (Full, Corresponding_Record_Type (Full_Base), Related_Nod));
13247 else
13248 Set_Corresponding_Record_Type (Full,
13249 Corresponding_Record_Type (Full_Base));
13250 end if;
13251 end if;
13253 -- Link rep item chain, and also setting of Has_Predicates from private
13254 -- subtype to full subtype, since we will need these on the full subtype
13255 -- to create the predicate function. Note that the full subtype may
13256 -- already have rep items, inherited from the full view of the base
13257 -- type, so we must be sure not to overwrite these entries.
13259 declare
13260 Append : Boolean;
13261 Item : Node_Id;
13262 Next_Item : Node_Id;
13263 Priv_Item : Node_Id;
13265 begin
13266 Item := First_Rep_Item (Full);
13267 Priv_Item := First_Rep_Item (Priv);
13269 -- If no existing rep items on full type, we can just link directly
13270 -- to the list of items on the private type, if any exist.. Same if
13271 -- the rep items are only those inherited from the base
13273 if (No (Item)
13274 or else Nkind (Item) /= N_Aspect_Specification
13275 or else Entity (Item) = Full_Base)
13276 and then Present (First_Rep_Item (Priv))
13277 then
13278 Set_First_Rep_Item (Full, Priv_Item);
13280 -- Otherwise, search to the end of items currently linked to the full
13281 -- subtype and append the private items to the end. However, if Priv
13282 -- and Full already have the same list of rep items, then the append
13283 -- is not done, as that would create a circularity.
13285 -- The partial view may have a predicate and the rep item lists of
13286 -- both views agree when inherited from the same ancestor. In that
13287 -- case, simply propagate the list from one view to the other.
13288 -- A more complex analysis needed here ???
13290 elsif Present (Priv_Item)
13291 and then Item = Next_Rep_Item (Priv_Item)
13292 then
13293 Set_First_Rep_Item (Full, Priv_Item);
13295 elsif Item /= Priv_Item then
13296 Append := True;
13297 loop
13298 Next_Item := Next_Rep_Item (Item);
13299 exit when No (Next_Item);
13300 Item := Next_Item;
13302 -- If the private view has aspect specifications, the full view
13303 -- inherits them. Since these aspects may already have been
13304 -- attached to the full view during derivation, do not append
13305 -- them if already present.
13307 if Item = First_Rep_Item (Priv) then
13308 Append := False;
13309 exit;
13310 end if;
13311 end loop;
13313 -- And link the private type items at the end of the chain
13315 if Append then
13316 Set_Next_Rep_Item (Item, First_Rep_Item (Priv));
13317 end if;
13318 end if;
13319 end;
13321 -- Make sure Has_Predicates is set on full type if it is set on the
13322 -- private type. Note that it may already be set on the full type and
13323 -- if so, we don't want to unset it. Similarly, propagate information
13324 -- about delayed aspects, because the corresponding pragmas must be
13325 -- analyzed when one of the views is frozen. This last step is needed
13326 -- in particular when the full type is a scalar type for which an
13327 -- anonymous base type is constructed.
13329 -- The predicate functions are generated either at the freeze point
13330 -- of the type or at the end of the visible part, and we must avoid
13331 -- generating them twice.
13333 Propagate_Predicate_Attributes (Full, Priv);
13335 if Has_Delayed_Aspects (Priv) then
13336 Set_Has_Delayed_Aspects (Full);
13337 end if;
13338 end Complete_Private_Subtype;
13340 ----------------------------
13341 -- Constant_Redeclaration --
13342 ----------------------------
13344 procedure Constant_Redeclaration
13345 (Id : Entity_Id;
13346 N : Node_Id;
13347 T : out Entity_Id)
13349 Prev : constant Entity_Id := Current_Entity_In_Scope (Id);
13350 Obj_Def : constant Node_Id := Object_Definition (N);
13351 New_T : Entity_Id;
13353 procedure Check_Possible_Deferred_Completion
13354 (Prev_Id : Entity_Id;
13355 Curr_Obj_Def : Node_Id);
13356 -- Determine whether the two object definitions describe the partial
13357 -- and the full view of a constrained deferred constant. Generate
13358 -- a subtype for the full view and verify that it statically matches
13359 -- the subtype of the partial view.
13361 procedure Check_Recursive_Declaration (Typ : Entity_Id);
13362 -- If deferred constant is an access type initialized with an allocator,
13363 -- check whether there is an illegal recursion in the definition,
13364 -- through a default value of some record subcomponent. This is normally
13365 -- detected when generating init procs, but requires this additional
13366 -- mechanism when expansion is disabled.
13368 ----------------------------------------
13369 -- Check_Possible_Deferred_Completion --
13370 ----------------------------------------
13372 procedure Check_Possible_Deferred_Completion
13373 (Prev_Id : Entity_Id;
13374 Curr_Obj_Def : Node_Id)
13376 Curr_Typ : Entity_Id;
13377 Prev_Typ : constant Entity_Id := Etype (Prev_Id);
13378 Anon_Acc : constant Boolean := Is_Anonymous_Access_Type (Prev_Typ);
13379 Mismatch : Boolean := False;
13380 begin
13381 if Anon_Acc then
13382 null;
13383 elsif Nkind (Curr_Obj_Def) = N_Subtype_Indication then
13384 declare
13385 Loc : constant Source_Ptr := Sloc (N);
13386 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
13387 Decl : constant Node_Id :=
13388 Make_Subtype_Declaration (Loc,
13389 Defining_Identifier => Def_Id,
13390 Subtype_Indication =>
13391 Relocate_Node (Curr_Obj_Def));
13393 begin
13394 Insert_Before_And_Analyze (N, Decl);
13395 Set_Etype (Id, Def_Id);
13396 Curr_Typ := Def_Id;
13397 end;
13398 else
13399 Curr_Typ := Etype (Curr_Obj_Def);
13400 end if;
13402 if Anon_Acc then
13403 if Nkind (Curr_Obj_Def) /= N_Access_Definition then
13404 Mismatch := True;
13405 elsif Has_Null_Exclusion (Prev_Typ)
13406 and then not Null_Exclusion_Present (Curr_Obj_Def)
13407 then
13408 Mismatch := True;
13409 end if;
13410 -- ??? Another check needed: mismatch if disagreement
13411 -- between designated types/profiles .
13412 else
13413 Mismatch :=
13414 Is_Constrained (Prev_Typ)
13415 and then not Subtypes_Statically_Match (Prev_Typ, Curr_Typ);
13416 end if;
13418 if Mismatch then
13419 Error_Msg_Sloc := Sloc (Prev_Id);
13420 Error_Msg_N ("subtype does not statically match deferred "
13421 & "declaration #", N);
13422 end if;
13423 end Check_Possible_Deferred_Completion;
13425 ---------------------------------
13426 -- Check_Recursive_Declaration --
13427 ---------------------------------
13429 procedure Check_Recursive_Declaration (Typ : Entity_Id) is
13430 Comp : Entity_Id;
13432 begin
13433 if Is_Record_Type (Typ) then
13434 Comp := First_Component (Typ);
13435 while Present (Comp) loop
13436 if Comes_From_Source (Comp) then
13437 if Present (Expression (Parent (Comp)))
13438 and then Is_Entity_Name (Expression (Parent (Comp)))
13439 and then Entity (Expression (Parent (Comp))) = Prev
13440 then
13441 Error_Msg_Sloc := Sloc (Parent (Comp));
13442 Error_Msg_NE
13443 ("illegal circularity with declaration for & #",
13444 N, Comp);
13445 return;
13447 elsif Is_Record_Type (Etype (Comp)) then
13448 Check_Recursive_Declaration (Etype (Comp));
13449 end if;
13450 end if;
13452 Next_Component (Comp);
13453 end loop;
13454 end if;
13455 end Check_Recursive_Declaration;
13457 -- Start of processing for Constant_Redeclaration
13459 begin
13460 if Nkind (Parent (Prev)) = N_Object_Declaration then
13461 if Nkind (Object_Definition
13462 (Parent (Prev))) = N_Subtype_Indication
13463 then
13464 -- Find type of new declaration. The constraints of the two
13465 -- views must match statically, but there is no point in
13466 -- creating an itype for the full view.
13468 if Nkind (Obj_Def) = N_Subtype_Indication then
13469 Find_Type (Subtype_Mark (Obj_Def));
13470 New_T := Entity (Subtype_Mark (Obj_Def));
13472 else
13473 Find_Type (Obj_Def);
13474 New_T := Entity (Obj_Def);
13475 end if;
13477 T := Etype (Prev);
13479 else
13480 -- The full view may impose a constraint, even if the partial
13481 -- view does not, so construct the subtype.
13483 New_T := Find_Type_Of_Object (Obj_Def, N);
13484 T := New_T;
13485 end if;
13487 else
13488 -- Current declaration is illegal, diagnosed below in Enter_Name
13490 T := Empty;
13491 New_T := Any_Type;
13492 end if;
13494 -- If previous full declaration or a renaming declaration exists, or if
13495 -- a homograph is present, let Enter_Name handle it, either with an
13496 -- error or with the removal of an overridden implicit subprogram.
13497 -- The previous one is a full declaration if it has an expression
13498 -- (which in the case of an aggregate is indicated by the Init flag).
13500 if Ekind (Prev) /= E_Constant
13501 or else Nkind (Parent (Prev)) = N_Object_Renaming_Declaration
13502 or else Present (Expression (Parent (Prev)))
13503 or else Has_Init_Expression (Parent (Prev))
13504 or else Present (Full_View (Prev))
13505 then
13506 Enter_Name (Id);
13508 -- Verify that types of both declarations match, or else that both types
13509 -- are anonymous access types whose designated subtypes statically match
13510 -- (as allowed in Ada 2005 by AI-385).
13512 elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
13513 and then
13514 (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
13515 or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
13516 or else Is_Access_Constant (Etype (New_T)) /=
13517 Is_Access_Constant (Etype (Prev))
13518 or else Can_Never_Be_Null (Etype (New_T)) /=
13519 Can_Never_Be_Null (Etype (Prev))
13520 or else Null_Exclusion_Present (Parent (Prev)) /=
13521 Null_Exclusion_Present (Parent (Id))
13522 or else not Subtypes_Statically_Match
13523 (Designated_Type (Etype (Prev)),
13524 Designated_Type (Etype (New_T))))
13525 then
13526 Error_Msg_Sloc := Sloc (Prev);
13527 Error_Msg_N ("type does not match declaration#", N);
13528 Set_Full_View (Prev, Id);
13529 Set_Etype (Id, Any_Type);
13531 -- A deferred constant whose type is an anonymous array is always
13532 -- illegal (unless imported). A detailed error message might be
13533 -- helpful for Ada beginners.
13535 if Nkind (Object_Definition (Parent (Prev)))
13536 = N_Constrained_Array_Definition
13537 and then Nkind (Object_Definition (N))
13538 = N_Constrained_Array_Definition
13539 then
13540 Error_Msg_N ("\each anonymous array is a distinct type", N);
13541 Error_Msg_N ("a deferred constant must have a named type",
13542 Object_Definition (Parent (Prev)));
13543 end if;
13545 elsif
13546 Null_Exclusion_Present (Parent (Prev))
13547 and then not Null_Exclusion_Present (N)
13548 then
13549 Error_Msg_Sloc := Sloc (Prev);
13550 Error_Msg_N ("null-exclusion does not match declaration#", N);
13551 Set_Full_View (Prev, Id);
13552 Set_Etype (Id, Any_Type);
13554 -- If so, process the full constant declaration
13556 else
13557 -- RM 7.4 (6): If the subtype defined by the subtype_indication in
13558 -- the deferred declaration is constrained, then the subtype defined
13559 -- by the subtype_indication in the full declaration shall match it
13560 -- statically.
13562 Check_Possible_Deferred_Completion
13563 (Prev_Id => Prev,
13564 Curr_Obj_Def => Obj_Def);
13566 Set_Full_View (Prev, Id);
13567 Set_Is_Public (Id, Is_Public (Prev));
13568 Set_Is_Internal (Id);
13569 Append_Entity (Id, Current_Scope);
13571 -- Check ALIASED present if present before (RM 7.4(7))
13573 if Is_Aliased (Prev)
13574 and then not Aliased_Present (N)
13575 then
13576 Error_Msg_Sloc := Sloc (Prev);
13577 Error_Msg_N ("ALIASED required (see declaration #)", N);
13578 end if;
13580 -- Check that placement is in private part and that the incomplete
13581 -- declaration appeared in the visible part.
13583 if Ekind (Current_Scope) = E_Package
13584 and then not In_Private_Part (Current_Scope)
13585 then
13586 Error_Msg_Sloc := Sloc (Prev);
13587 Error_Msg_N
13588 ("full constant for declaration # must be in private part", N);
13590 elsif Ekind (Current_Scope) = E_Package
13591 and then
13592 List_Containing (Parent (Prev)) /=
13593 Visible_Declarations (Package_Specification (Current_Scope))
13594 then
13595 Error_Msg_N
13596 ("deferred constant must be declared in visible part",
13597 Parent (Prev));
13598 end if;
13600 if Is_Access_Type (T)
13601 and then Nkind (Expression (N)) = N_Allocator
13602 then
13603 Check_Recursive_Declaration (Designated_Type (T));
13604 end if;
13606 -- A deferred constant is a visible entity. If type has invariants,
13607 -- verify that the initial value satisfies them. This is not done in
13608 -- GNATprove mode, as GNATprove handles invariant checks itself.
13610 if Has_Invariants (T)
13611 and then Present (Invariant_Procedure (T))
13612 and then not GNATprove_Mode
13613 then
13614 Insert_After (N,
13615 Make_Invariant_Call (New_Occurrence_Of (Prev, Sloc (N))));
13616 end if;
13617 end if;
13618 end Constant_Redeclaration;
13620 ----------------------
13621 -- Constrain_Access --
13622 ----------------------
13624 procedure Constrain_Access
13625 (Def_Id : in out Entity_Id;
13626 S : Node_Id;
13627 Related_Nod : Node_Id)
13629 T : constant Entity_Id := Entity (Subtype_Mark (S));
13630 Desig_Type : constant Entity_Id := Designated_Type (T);
13631 Desig_Subtype : Entity_Id;
13632 Constraint_OK : Boolean := True;
13634 begin
13635 if Is_Array_Type (Desig_Type) then
13636 Desig_Subtype := Create_Itype (E_Void, Related_Nod);
13637 Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
13639 elsif (Is_Record_Type (Desig_Type)
13640 or else Is_Incomplete_Or_Private_Type (Desig_Type))
13641 and then not Is_Constrained (Desig_Type)
13642 then
13643 -- If this is a constrained access definition for a record
13644 -- component, we leave the type as an unconstrained access,
13645 -- and mark the component so that its actual type is built
13646 -- at a point of use (e.g., an assignment statement). This
13647 -- is handled in Sem_Util.Build_Actual_Subtype_Of_Component.
13649 if Desig_Type = Current_Scope
13650 and then No (Def_Id)
13651 then
13652 Desig_Subtype :=
13653 Create_Itype
13654 (E_Void, Related_Nod, Scope_Id => Scope (Desig_Type));
13655 Mutate_Ekind (Desig_Subtype, E_Record_Subtype);
13656 Def_Id := Entity (Subtype_Mark (S));
13658 -- We indicate that the component has a per-object constraint
13659 -- for treatment at a point of use, even though the constraint
13660 -- may be independent of discriminants of the enclosing type.
13662 if Nkind (Related_Nod) = N_Component_Declaration then
13663 Set_Has_Per_Object_Constraint
13664 (Defining_Identifier (Related_Nod));
13665 end if;
13667 -- This call added to ensure that the constraint is analyzed
13668 -- (needed for a B test). Note that we still return early from
13669 -- this procedure to avoid recursive processing.
13671 Constrain_Discriminated_Type
13672 (Desig_Subtype, S, Related_Nod, For_Access => True);
13673 return;
13674 end if;
13676 -- Enforce rule that the constraint is illegal if there is an
13677 -- unconstrained view of the designated type. This means that the
13678 -- partial view (either a private type declaration or a derivation
13679 -- from a private type) has no discriminants. (Defect Report
13680 -- 8652/0008, Technical Corrigendum 1, checked by ACATS B371001).
13682 -- Rule updated for Ada 2005: The private type is said to have
13683 -- a constrained partial view, given that objects of the type
13684 -- can be declared. Furthermore, the rule applies to all access
13685 -- types, unlike the rule concerning default discriminants (see
13686 -- RM 3.7.1(7/3))
13688 if (Ekind (T) = E_General_Access_Type or else Ada_Version >= Ada_2005)
13689 and then Has_Private_Declaration (Desig_Type)
13690 and then In_Open_Scopes (Scope (Desig_Type))
13691 and then Has_Discriminants (Desig_Type)
13692 then
13693 declare
13694 Pack : constant Node_Id :=
13695 Unit_Declaration_Node (Scope (Desig_Type));
13696 Decls : List_Id;
13697 Decl : Node_Id;
13699 begin
13700 if Nkind (Pack) = N_Package_Declaration then
13701 Decls := Visible_Declarations (Specification (Pack));
13702 Decl := First (Decls);
13703 while Present (Decl) loop
13704 if (Nkind (Decl) = N_Private_Type_Declaration
13705 and then Chars (Defining_Identifier (Decl)) =
13706 Chars (Desig_Type))
13708 or else
13709 (Nkind (Decl) = N_Full_Type_Declaration
13710 and then
13711 Chars (Defining_Identifier (Decl)) =
13712 Chars (Desig_Type)
13713 and then Is_Derived_Type (Desig_Type)
13714 and then
13715 Has_Private_Declaration (Etype (Desig_Type)))
13716 then
13717 if No (Discriminant_Specifications (Decl)) then
13718 Error_Msg_N
13719 ("cannot constrain access type if designated "
13720 & "type has constrained partial view", S);
13721 end if;
13723 exit;
13724 end if;
13726 Next (Decl);
13727 end loop;
13728 end if;
13729 end;
13730 end if;
13732 Desig_Subtype := Create_Itype (E_Void, Related_Nod);
13733 Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
13734 For_Access => True);
13736 elsif Is_Concurrent_Type (Desig_Type)
13737 and then not Is_Constrained (Desig_Type)
13738 then
13739 Desig_Subtype := Create_Itype (E_Void, Related_Nod);
13740 Constrain_Concurrent (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
13742 else
13743 Error_Msg_N ("invalid constraint on access type", S);
13745 -- We simply ignore an invalid constraint
13747 Desig_Subtype := Desig_Type;
13748 Constraint_OK := False;
13749 end if;
13751 if No (Def_Id) then
13752 Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
13753 else
13754 Mutate_Ekind (Def_Id, E_Access_Subtype);
13755 end if;
13757 if Constraint_OK then
13758 Set_Etype (Def_Id, Base_Type (T));
13760 if Is_Private_Type (Desig_Type) then
13761 Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
13762 end if;
13763 else
13764 Set_Etype (Def_Id, Any_Type);
13765 end if;
13767 Set_Size_Info (Def_Id, T);
13768 Set_Is_Constrained (Def_Id, Constraint_OK);
13769 Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
13770 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
13771 Set_Is_Access_Constant (Def_Id, Is_Access_Constant (T));
13772 Set_Can_Never_Be_Null (Def_Id, Can_Never_Be_Null (T));
13774 Conditional_Delay (Def_Id, T);
13776 -- AI-363 : Subtypes of general access types whose designated types have
13777 -- default discriminants are disallowed. In instances, the rule has to
13778 -- be checked against the actual, of which T is the subtype. In a
13779 -- generic body, the rule is checked assuming that the actual type has
13780 -- defaulted discriminants.
13782 if Ada_Version >= Ada_2005 or else Warn_On_Ada_2005_Compatibility then
13783 if Ekind (Base_Type (T)) = E_General_Access_Type
13784 and then Has_Defaulted_Discriminants (Desig_Type)
13785 then
13786 if Ada_Version < Ada_2005 then
13787 Error_Msg_N
13788 ("access subtype of general access type would not " &
13789 "be allowed in Ada 2005?y?", S);
13790 else
13791 Error_Msg_N
13792 ("access subtype of general access type not allowed", S);
13793 end if;
13795 Error_Msg_N ("\discriminants have defaults", S);
13797 elsif Is_Access_Type (T)
13798 and then Is_Generic_Type (Desig_Type)
13799 and then Has_Discriminants (Desig_Type)
13800 and then In_Package_Body (Current_Scope)
13801 then
13802 if Ada_Version < Ada_2005 then
13803 Error_Msg_N
13804 ("access subtype would not be allowed in generic body "
13805 & "in Ada 2005?y?", S);
13806 else
13807 Error_Msg_N
13808 ("access subtype not allowed in generic body", S);
13809 end if;
13811 Error_Msg_N
13812 ("\designated type is a discriminated formal", S);
13813 end if;
13814 end if;
13815 end Constrain_Access;
13817 ---------------------
13818 -- Constrain_Array --
13819 ---------------------
13821 procedure Constrain_Array
13822 (Def_Id : in out Entity_Id;
13823 SI : Node_Id;
13824 Related_Nod : Node_Id;
13825 Related_Id : Entity_Id;
13826 Suffix : Character)
13828 C : constant Node_Id := Constraint (SI);
13829 Number_Of_Constraints : constant Nat := List_Length (Constraints (C));
13830 Index : Node_Id;
13831 S, T : Entity_Id;
13832 Constraint_OK : Boolean := True;
13833 Is_FLB_Array_Subtype : Boolean := False;
13835 begin
13836 T := Entity (Subtype_Mark (SI));
13838 if Is_Access_Type (T) then
13839 T := Designated_Type (T);
13840 end if;
13842 T := Underlying_Type (T);
13844 -- If an index constraint follows a subtype mark in a subtype indication
13845 -- then the type or subtype denoted by the subtype mark must not already
13846 -- impose an index constraint. The subtype mark must denote either an
13847 -- unconstrained array type or an access type whose designated type
13848 -- is such an array type... (RM 3.6.1)
13850 if Is_Constrained (T) then
13851 Error_Msg_N ("array type is already constrained", Subtype_Mark (SI));
13852 Constraint_OK := False;
13854 else
13855 -- In either case, the index constraint must provide a discrete
13856 -- range for each index of the array type and the type of each
13857 -- discrete range must be the same as that of the corresponding
13858 -- index. (RM 3.6.1)
13860 if Number_Of_Constraints /= Number_Dimensions (T) then
13861 Error_Msg_NE ("incorrect number of index constraints for }", C, T);
13862 Constraint_OK := False;
13864 else
13865 S := First (Constraints (C));
13866 Index := First_Index (T);
13867 Analyze (Index);
13869 -- Apply constraints to each index type
13871 for J in 1 .. Number_Of_Constraints loop
13872 Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
13874 -- If the subtype of the index has been set to indicate that
13875 -- it has a fixed lower bound, then record that the subtype's
13876 -- entity will need to be marked as being a fixed-lower-bound
13877 -- array subtype.
13879 if S = First (Constraints (C)) then
13880 Is_FLB_Array_Subtype :=
13881 Is_Fixed_Lower_Bound_Index_Subtype (Etype (S));
13883 -- If the parent subtype (or should this be Etype of that?)
13884 -- is an FLB array subtype, we flag an error, because we
13885 -- don't currently allow subtypes of such subtypes to
13886 -- specify a fixed lower bound for any of their indexes,
13887 -- even if the index of the parent subtype is a "range <>"
13888 -- index.
13890 if Is_FLB_Array_Subtype
13891 and then Is_Fixed_Lower_Bound_Array_Subtype (T)
13892 then
13893 Error_Msg_NE
13894 ("index with fixed lower bound not allowed for subtype "
13895 & "of fixed-lower-bound }", S, T);
13897 Is_FLB_Array_Subtype := False;
13898 end if;
13900 elsif Is_FLB_Array_Subtype
13901 and then not Is_Fixed_Lower_Bound_Index_Subtype (Etype (S))
13902 then
13903 Error_Msg_NE
13904 ("constrained index not allowed for fixed-lower-bound "
13905 & "subtype of}", S, T);
13907 elsif not Is_FLB_Array_Subtype
13908 and then Is_Fixed_Lower_Bound_Index_Subtype (Etype (S))
13909 then
13910 Error_Msg_NE
13911 ("index with fixed lower bound not allowed for "
13912 & "constrained subtype of}", S, T);
13913 end if;
13915 Next (Index);
13916 Next (S);
13917 end loop;
13919 end if;
13920 end if;
13922 if No (Def_Id) then
13923 Def_Id :=
13924 Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
13925 Set_Parent (Def_Id, Related_Nod);
13927 else
13928 Mutate_Ekind (Def_Id, E_Array_Subtype);
13929 end if;
13931 Set_Size_Info (Def_Id, (T));
13932 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13933 Set_Etype (Def_Id, Base_Type (T));
13935 if Constraint_OK then
13936 Set_First_Index (Def_Id, First (Constraints (C)));
13937 else
13938 Set_First_Index (Def_Id, First_Index (T));
13939 end if;
13941 Set_Is_Constrained (Def_Id, not Is_FLB_Array_Subtype);
13942 Set_Is_Fixed_Lower_Bound_Array_Subtype
13943 (Def_Id, Is_FLB_Array_Subtype);
13944 Set_Is_Aliased (Def_Id, Is_Aliased (T));
13945 Set_Is_Independent (Def_Id, Is_Independent (T));
13946 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
13948 Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
13949 Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
13951 -- A subtype does not inherit the Packed_Array_Impl_Type of is parent.
13952 -- We need to initialize the attribute because if Def_Id is previously
13953 -- analyzed through a limited_with clause, it will have the attributes
13954 -- of an incomplete type, one of which is an Elist that overlaps the
13955 -- Packed_Array_Impl_Type field.
13957 Set_Packed_Array_Impl_Type (Def_Id, Empty);
13959 -- Build a freeze node if parent still needs one. Also make sure that
13960 -- the Depends_On_Private status is set because the subtype will need
13961 -- reprocessing at the time the base type does, and also we must set a
13962 -- conditional delay.
13964 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
13965 Conditional_Delay (Def_Id, T);
13966 end Constrain_Array;
13968 ------------------------------
13969 -- Constrain_Component_Type --
13970 ------------------------------
13972 function Constrain_Component_Type
13973 (Comp : Entity_Id;
13974 Constrained_Typ : Entity_Id;
13975 Related_Node : Node_Id;
13976 Typ : Entity_Id;
13977 Constraints : Elist_Id) return Entity_Id
13979 Loc : constant Source_Ptr := Sloc (Constrained_Typ);
13980 Compon_Type : constant Entity_Id := Etype (Comp);
13982 function Build_Constrained_Array_Type
13983 (Old_Type : Entity_Id) return Entity_Id;
13984 -- If Old_Type is an array type, one of whose indexes is constrained
13985 -- by a discriminant, build an Itype whose constraint replaces the
13986 -- discriminant with its value in the constraint.
13988 function Build_Constrained_Discriminated_Type
13989 (Old_Type : Entity_Id) return Entity_Id;
13990 -- Ditto for record components. Handle the case where the constraint
13991 -- is a conversion of the discriminant value, introduced during
13992 -- expansion.
13994 function Build_Constrained_Access_Type
13995 (Old_Type : Entity_Id) return Entity_Id;
13996 -- Ditto for access types. Makes use of previous two functions, to
13997 -- constrain designated type.
13999 function Is_Discriminant (Expr : Node_Id) return Boolean;
14000 -- Returns True if Expr is a discriminant
14002 function Get_Discr_Value (Discr_Expr : Node_Id) return Node_Id;
14003 -- Find the value of a discriminant named by Discr_Expr in Constraints
14005 -----------------------------------
14006 -- Build_Constrained_Access_Type --
14007 -----------------------------------
14009 function Build_Constrained_Access_Type
14010 (Old_Type : Entity_Id) return Entity_Id
14012 Desig_Type : constant Entity_Id := Designated_Type (Old_Type);
14013 Itype : Entity_Id;
14014 Desig_Subtype : Entity_Id;
14015 Scop : Entity_Id;
14017 begin
14018 -- If the original access type was not embedded in the enclosing
14019 -- type definition, there is no need to produce a new access
14020 -- subtype. In fact every access type with an explicit constraint
14021 -- generates an itype whose scope is the enclosing record.
14023 if not Is_Type (Scope (Old_Type)) then
14024 return Old_Type;
14026 elsif Is_Array_Type (Desig_Type) then
14027 Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
14029 elsif Has_Discriminants (Desig_Type) then
14031 -- This may be an access type to an enclosing record type for
14032 -- which we are constructing the constrained components. Return
14033 -- the enclosing record subtype. This is not always correct,
14034 -- but avoids infinite recursion. ???
14036 Desig_Subtype := Any_Type;
14038 for J in reverse 0 .. Scope_Stack.Last loop
14039 Scop := Scope_Stack.Table (J).Entity;
14041 if Is_Type (Scop)
14042 and then Base_Type (Scop) = Base_Type (Desig_Type)
14043 then
14044 Desig_Subtype := Scop;
14045 end if;
14047 exit when not Is_Type (Scop);
14048 end loop;
14050 if Desig_Subtype = Any_Type then
14051 Desig_Subtype :=
14052 Build_Constrained_Discriminated_Type (Desig_Type);
14053 end if;
14055 else
14056 return Old_Type;
14057 end if;
14059 if Desig_Subtype /= Desig_Type then
14061 -- The Related_Node better be here or else we won't be able
14062 -- to attach new itypes to a node in the tree.
14064 pragma Assert (Present (Related_Node));
14066 Itype := Create_Itype (E_Access_Subtype, Related_Node);
14068 Set_Etype (Itype, Base_Type (Old_Type));
14069 Set_Size_Info (Itype, (Old_Type));
14070 Set_Directly_Designated_Type (Itype, Desig_Subtype);
14071 Set_Depends_On_Private (Itype, Has_Private_Component
14072 (Old_Type));
14073 Set_Is_Access_Constant (Itype, Is_Access_Constant
14074 (Old_Type));
14076 -- The new itype needs freezing when it depends on a not frozen
14077 -- type and the enclosing subtype needs freezing.
14079 if Has_Delayed_Freeze (Constrained_Typ)
14080 and then not Is_Frozen (Constrained_Typ)
14081 then
14082 Conditional_Delay (Itype, Base_Type (Old_Type));
14083 end if;
14085 return Itype;
14087 else
14088 return Old_Type;
14089 end if;
14090 end Build_Constrained_Access_Type;
14092 ----------------------------------
14093 -- Build_Constrained_Array_Type --
14094 ----------------------------------
14096 function Build_Constrained_Array_Type
14097 (Old_Type : Entity_Id) return Entity_Id
14099 Lo_Expr : Node_Id;
14100 Hi_Expr : Node_Id;
14101 Old_Index : Node_Id;
14102 Range_Node : Node_Id;
14103 Constr_List : List_Id;
14105 Need_To_Create_Itype : Boolean := False;
14107 begin
14108 Old_Index := First_Index (Old_Type);
14109 while Present (Old_Index) loop
14110 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
14112 if Is_Discriminant (Lo_Expr)
14113 or else
14114 Is_Discriminant (Hi_Expr)
14115 then
14116 Need_To_Create_Itype := True;
14117 exit;
14118 end if;
14120 Next_Index (Old_Index);
14121 end loop;
14123 if Need_To_Create_Itype then
14124 Constr_List := New_List;
14126 Old_Index := First_Index (Old_Type);
14127 while Present (Old_Index) loop
14128 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
14130 if Is_Discriminant (Lo_Expr) then
14131 Lo_Expr := Get_Discr_Value (Lo_Expr);
14132 end if;
14134 if Is_Discriminant (Hi_Expr) then
14135 Hi_Expr := Get_Discr_Value (Hi_Expr);
14136 end if;
14138 Range_Node :=
14139 Make_Range
14140 (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
14142 Append (Range_Node, To => Constr_List);
14144 Next_Index (Old_Index);
14145 end loop;
14147 return Build_Subtype (Related_Node, Loc, Old_Type, Constr_List);
14149 else
14150 return Old_Type;
14151 end if;
14152 end Build_Constrained_Array_Type;
14154 ------------------------------------------
14155 -- Build_Constrained_Discriminated_Type --
14156 ------------------------------------------
14158 function Build_Constrained_Discriminated_Type
14159 (Old_Type : Entity_Id) return Entity_Id
14161 Expr : Node_Id;
14162 Constr_List : List_Id;
14163 Old_Constraint : Elmt_Id;
14165 Need_To_Create_Itype : Boolean := False;
14167 begin
14168 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
14169 while Present (Old_Constraint) loop
14170 Expr := Node (Old_Constraint);
14172 if Is_Discriminant (Expr) then
14173 Need_To_Create_Itype := True;
14174 exit;
14176 -- After expansion of discriminated task types, the value
14177 -- of the discriminant may be converted to a run-time type
14178 -- for restricted run-times. Propagate the value of the
14179 -- discriminant as well, so that e.g. the secondary stack
14180 -- component has a static constraint. Necessary for LLVM.
14182 elsif Nkind (Expr) = N_Type_Conversion
14183 and then Is_Discriminant (Expression (Expr))
14184 then
14185 Need_To_Create_Itype := True;
14186 exit;
14187 end if;
14189 Next_Elmt (Old_Constraint);
14190 end loop;
14192 if Need_To_Create_Itype then
14193 Constr_List := New_List;
14195 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
14196 while Present (Old_Constraint) loop
14197 Expr := Node (Old_Constraint);
14199 if Is_Discriminant (Expr) then
14200 Expr := Get_Discr_Value (Expr);
14202 elsif Nkind (Expr) = N_Type_Conversion
14203 and then Is_Discriminant (Expression (Expr))
14204 then
14205 Expr := New_Copy_Tree (Expr);
14206 Set_Expression (Expr, Get_Discr_Value (Expression (Expr)));
14207 end if;
14209 Append (New_Copy_Tree (Expr), To => Constr_List);
14211 Next_Elmt (Old_Constraint);
14212 end loop;
14214 return Build_Subtype (Related_Node, Loc, Old_Type, Constr_List);
14216 else
14217 return Old_Type;
14218 end if;
14219 end Build_Constrained_Discriminated_Type;
14221 ---------------------
14222 -- Get_Discr_Value --
14223 ---------------------
14225 function Get_Discr_Value (Discr_Expr : Node_Id) return Node_Id is
14226 Discr_Id : constant Entity_Id := Entity (Discr_Expr);
14227 -- Entity of a discriminant that appear as a standalone expression in
14228 -- the constraint of a component.
14230 D : Entity_Id;
14231 E : Elmt_Id;
14233 begin
14234 -- The discriminant may be declared for the type, in which case we
14235 -- find it by iterating over the list of discriminants. If the
14236 -- discriminant is inherited from a parent type, it appears as the
14237 -- corresponding discriminant of the current type. This will be the
14238 -- case when constraining an inherited component whose constraint is
14239 -- given by a discriminant of the parent.
14241 D := First_Discriminant (Typ);
14242 E := First_Elmt (Constraints);
14244 while Present (D) loop
14245 if D = Discr_Id
14246 or else D = CR_Discriminant (Discr_Id)
14247 or else Corresponding_Discriminant (D) = Discr_Id
14248 then
14249 return New_Copy_Tree (Node (E));
14250 end if;
14252 Next_Discriminant (D);
14253 Next_Elmt (E);
14254 end loop;
14256 -- The Corresponding_Discriminant mechanism is incomplete, because
14257 -- the correspondence between new and old discriminants is not one
14258 -- to one: one new discriminant can constrain several old ones. In
14259 -- that case, scan sequentially the stored_constraint, the list of
14260 -- discriminants of the parents, and the constraints.
14262 -- Previous code checked for the present of the Stored_Constraint
14263 -- list for the derived type, but did not use it at all. Should it
14264 -- be present when the component is a discriminated task type?
14266 if Is_Derived_Type (Typ)
14267 and then Scope (Discr_Id) = Etype (Typ)
14268 then
14269 D := First_Discriminant (Etype (Typ));
14270 E := First_Elmt (Constraints);
14271 while Present (D) loop
14272 if D = Discr_Id then
14273 return New_Copy_Tree (Node (E));
14274 end if;
14276 Next_Discriminant (D);
14277 Next_Elmt (E);
14278 end loop;
14279 end if;
14281 -- Something is wrong if we did not find the value
14283 raise Program_Error;
14284 end Get_Discr_Value;
14286 ---------------------
14287 -- Is_Discriminant --
14288 ---------------------
14290 function Is_Discriminant (Expr : Node_Id) return Boolean is
14291 Discrim_Scope : Entity_Id;
14293 begin
14294 if Denotes_Discriminant (Expr) then
14295 Discrim_Scope := Scope (Entity (Expr));
14297 -- Either we have a reference to one of Typ's discriminants,
14299 pragma Assert (Discrim_Scope = Typ
14301 -- or to the discriminants of the parent type, in the case
14302 -- of a derivation of a tagged type with variants.
14304 or else Discrim_Scope = Etype (Typ)
14305 or else Full_View (Discrim_Scope) = Etype (Typ)
14307 -- or same as above for the case where the discriminants
14308 -- were declared in Typ's private view.
14310 or else (Is_Private_Type (Discrim_Scope)
14311 and then Chars (Discrim_Scope) = Chars (Typ))
14313 -- or else we are deriving from the full view and the
14314 -- discriminant is declared in the private entity.
14316 or else (Is_Private_Type (Typ)
14317 and then Chars (Discrim_Scope) = Chars (Typ))
14319 -- Or we are constrained the corresponding record of a
14320 -- synchronized type that completes a private declaration.
14322 or else (Is_Concurrent_Record_Type (Typ)
14323 and then
14324 Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
14326 -- or we have a class-wide type, in which case make sure the
14327 -- discriminant found belongs to the root type.
14329 or else (Is_Class_Wide_Type (Typ)
14330 and then Etype (Typ) = Discrim_Scope));
14332 return True;
14333 end if;
14335 -- In all other cases we have something wrong
14337 return False;
14338 end Is_Discriminant;
14340 -- Start of processing for Constrain_Component_Type
14342 begin
14343 if Nkind (Parent (Comp)) = N_Component_Declaration
14344 and then Comes_From_Source (Parent (Comp))
14345 and then Comes_From_Source
14346 (Subtype_Indication (Component_Definition (Parent (Comp))))
14347 and then
14348 Is_Entity_Name
14349 (Subtype_Indication (Component_Definition (Parent (Comp))))
14350 then
14351 return Compon_Type;
14353 elsif Is_Array_Type (Compon_Type) then
14354 return Build_Constrained_Array_Type (Compon_Type);
14356 elsif Has_Discriminants (Compon_Type) then
14357 return Build_Constrained_Discriminated_Type (Compon_Type);
14359 elsif Is_Access_Type (Compon_Type) then
14360 return Build_Constrained_Access_Type (Compon_Type);
14362 else
14363 return Compon_Type;
14364 end if;
14365 end Constrain_Component_Type;
14367 --------------------------
14368 -- Constrain_Concurrent --
14369 --------------------------
14371 -- For concurrent types, the associated record value type carries the same
14372 -- discriminants, so when we constrain a concurrent type, we must constrain
14373 -- the corresponding record type as well.
14375 procedure Constrain_Concurrent
14376 (Def_Id : in out Entity_Id;
14377 SI : Node_Id;
14378 Related_Nod : Node_Id;
14379 Related_Id : Entity_Id;
14380 Suffix : Character)
14382 -- Retrieve Base_Type to ensure getting to the concurrent type in the
14383 -- case of a private subtype (needed when only doing semantic analysis).
14385 T_Ent : Entity_Id := Base_Type (Entity (Subtype_Mark (SI)));
14386 T_Val : Entity_Id;
14388 begin
14389 if Is_Access_Type (T_Ent) then
14390 T_Ent := Designated_Type (T_Ent);
14391 end if;
14393 T_Val := Corresponding_Record_Type (T_Ent);
14395 if Present (T_Val) then
14397 if No (Def_Id) then
14398 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
14400 -- Elaborate itype now, as it may be used in a subsequent
14401 -- synchronized operation in another scope.
14403 if Nkind (Related_Nod) = N_Full_Type_Declaration then
14404 Build_Itype_Reference (Def_Id, Related_Nod);
14405 end if;
14406 end if;
14408 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
14409 Set_First_Private_Entity (Def_Id, First_Private_Entity (T_Ent));
14411 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
14412 Set_Corresponding_Record_Type (Def_Id,
14413 Constrain_Corresponding_Record (Def_Id, T_Val, Related_Nod));
14415 else
14416 -- If there is no associated record, expansion is disabled and this
14417 -- is a generic context. Create a subtype in any case, so that
14418 -- semantic analysis can proceed.
14420 if No (Def_Id) then
14421 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
14422 end if;
14424 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
14425 end if;
14426 end Constrain_Concurrent;
14428 ------------------------------------
14429 -- Constrain_Corresponding_Record --
14430 ------------------------------------
14432 function Constrain_Corresponding_Record
14433 (Prot_Subt : Entity_Id;
14434 Corr_Rec : Entity_Id;
14435 Related_Nod : Node_Id) return Entity_Id
14437 T_Sub : constant Entity_Id :=
14438 Create_Itype
14439 (Ekind => E_Record_Subtype,
14440 Related_Nod => Related_Nod,
14441 Related_Id => Corr_Rec,
14442 Suffix => 'C',
14443 Suffix_Index => -1);
14445 begin
14446 Set_Etype (T_Sub, Corr_Rec);
14447 Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
14448 Set_Is_Tagged_Type (T_Sub, Is_Tagged_Type (Corr_Rec));
14449 Set_Is_Constrained (T_Sub, True);
14450 Set_First_Entity (T_Sub, First_Entity (Corr_Rec));
14451 Set_Last_Entity (T_Sub, Last_Entity (Corr_Rec));
14452 Set_Direct_Primitive_Operations
14453 (T_Sub, Direct_Primitive_Operations (Corr_Rec));
14455 if Has_Discriminants (Prot_Subt) then -- False only if errors.
14456 Set_Discriminant_Constraint
14457 (T_Sub, Discriminant_Constraint (Prot_Subt));
14458 Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
14459 Create_Constrained_Components
14460 (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
14461 end if;
14463 Set_Depends_On_Private (T_Sub, Has_Private_Component (T_Sub));
14465 if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
14466 Conditional_Delay (T_Sub, Corr_Rec);
14468 else
14469 -- This is a component subtype: it will be frozen in the context of
14470 -- the enclosing record's init_proc, so that discriminant references
14471 -- are resolved to discriminals. (Note: we used to skip freezing
14472 -- altogether in that case, which caused errors downstream for
14473 -- components of a bit packed array type).
14475 Set_Has_Delayed_Freeze (T_Sub);
14476 end if;
14478 return T_Sub;
14479 end Constrain_Corresponding_Record;
14481 -----------------------
14482 -- Constrain_Decimal --
14483 -----------------------
14485 procedure Constrain_Decimal (Def_Id : Entity_Id; S : Node_Id) is
14486 T : constant Entity_Id := Entity (Subtype_Mark (S));
14487 C : constant Node_Id := Constraint (S);
14488 Loc : constant Source_Ptr := Sloc (C);
14489 Range_Expr : Node_Id;
14490 Digits_Expr : Node_Id;
14491 Digits_Val : Uint;
14492 Bound_Val : Ureal;
14494 begin
14495 Mutate_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
14497 if Nkind (C) = N_Range_Constraint then
14498 Range_Expr := Range_Expression (C);
14499 Digits_Val := Digits_Value (T);
14501 else
14502 pragma Assert (Nkind (C) = N_Digits_Constraint);
14504 Digits_Expr := Digits_Expression (C);
14505 Analyze_And_Resolve (Digits_Expr, Any_Integer);
14507 Check_Digits_Expression (Digits_Expr);
14508 Digits_Val := Expr_Value (Digits_Expr);
14510 if Digits_Val > Digits_Value (T) then
14511 Error_Msg_N
14512 ("digits expression is incompatible with subtype", C);
14513 Digits_Val := Digits_Value (T);
14514 end if;
14516 if Present (Range_Constraint (C)) then
14517 Range_Expr := Range_Expression (Range_Constraint (C));
14518 else
14519 Range_Expr := Empty;
14520 end if;
14521 end if;
14523 Set_Etype (Def_Id, Base_Type (T));
14524 Set_Size_Info (Def_Id, (T));
14525 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14526 Set_Delta_Value (Def_Id, Delta_Value (T));
14527 Set_Scale_Value (Def_Id, Scale_Value (T));
14528 Set_Small_Value (Def_Id, Small_Value (T));
14529 Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
14530 Set_Digits_Value (Def_Id, Digits_Val);
14532 -- Manufacture range from given digits value if no range present
14534 if No (Range_Expr) then
14535 Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
14536 Range_Expr :=
14537 Make_Range (Loc,
14538 Low_Bound =>
14539 Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
14540 High_Bound =>
14541 Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
14542 end if;
14544 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
14545 Set_Discrete_RM_Size (Def_Id);
14547 -- Unconditionally delay the freeze, since we cannot set size
14548 -- information in all cases correctly until the freeze point.
14550 Set_Has_Delayed_Freeze (Def_Id);
14551 end Constrain_Decimal;
14553 ----------------------------------
14554 -- Constrain_Discriminated_Type --
14555 ----------------------------------
14557 procedure Constrain_Discriminated_Type
14558 (Def_Id : Entity_Id;
14559 S : Node_Id;
14560 Related_Nod : Node_Id;
14561 For_Access : Boolean := False)
14563 E : Entity_Id := Entity (Subtype_Mark (S));
14564 T : Entity_Id;
14566 procedure Fixup_Bad_Constraint;
14567 -- Called after finding a bad constraint, and after having posted an
14568 -- appropriate error message. The goal is to leave type Def_Id in as
14569 -- reasonable state as possible.
14571 --------------------------
14572 -- Fixup_Bad_Constraint --
14573 --------------------------
14575 procedure Fixup_Bad_Constraint is
14576 begin
14577 -- Set a reasonable Ekind for the entity, including incomplete types.
14579 Mutate_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
14581 -- Set Etype to the known type, to reduce chances of cascaded errors
14583 Set_Etype (Def_Id, E);
14584 Set_Error_Posted (Def_Id);
14585 end Fixup_Bad_Constraint;
14587 -- Local variables
14589 C : Node_Id;
14590 Constr : Elist_Id := New_Elmt_List;
14592 -- Start of processing for Constrain_Discriminated_Type
14594 begin
14595 C := Constraint (S);
14597 -- A discriminant constraint is only allowed in a subtype indication,
14598 -- after a subtype mark. This subtype mark must denote either a type
14599 -- with discriminants, or an access type whose designated type is a
14600 -- type with discriminants. A discriminant constraint specifies the
14601 -- values of these discriminants (RM 3.7.2(5)).
14603 T := Base_Type (Entity (Subtype_Mark (S)));
14605 if Is_Access_Type (T) then
14606 T := Designated_Type (T);
14607 end if;
14609 -- In an instance it may be necessary to retrieve the full view of a
14610 -- type with unknown discriminants, or a full view with defaulted
14611 -- discriminants. In other contexts the constraint is illegal.
14612 -- This relaxation of legality checking may also be needed in
14613 -- compiler-generated Put_Image or streaming subprograms (hence
14614 -- the Comes_From_Source test).
14616 if (In_Instance or not Comes_From_Source (S))
14617 and then Is_Private_Type (T)
14618 and then Present (Full_View (T))
14619 and then
14620 (Has_Unknown_Discriminants (T)
14621 or else
14622 (not Has_Discriminants (T)
14623 and then Has_Defaulted_Discriminants (Full_View (T))))
14624 then
14625 T := Full_View (T);
14626 E := Full_View (E);
14627 end if;
14629 -- Ada 2005 (AI-412): Constrained incomplete subtypes are illegal. Avoid
14630 -- generating an error for access-to-incomplete subtypes.
14632 if Ada_Version >= Ada_2005
14633 and then Ekind (T) = E_Incomplete_Type
14634 and then Nkind (Parent (S)) = N_Subtype_Declaration
14635 and then not Is_Itype (Def_Id)
14636 then
14637 -- A little sanity check: emit an error message if the type has
14638 -- discriminants to begin with. Type T may be a regular incomplete
14639 -- type or imported via a limited with clause.
14641 if Has_Discriminants (T)
14642 or else (From_Limited_With (T)
14643 and then Present (Non_Limited_View (T))
14644 and then Nkind (Parent (Non_Limited_View (T))) =
14645 N_Full_Type_Declaration
14646 and then Present (Discriminant_Specifications
14647 (Parent (Non_Limited_View (T)))))
14648 then
14649 Error_Msg_N
14650 ("(Ada 2005) incomplete subtype may not be constrained", C);
14651 else
14652 Error_Msg_N ("invalid constraint: type has no discriminant", C);
14653 end if;
14655 Fixup_Bad_Constraint;
14656 return;
14658 -- Check that the type has visible discriminants. The type may be
14659 -- a private type with unknown discriminants whose full view has
14660 -- discriminants which are invisible.
14662 elsif not Has_Discriminants (T)
14663 or else
14664 (Has_Unknown_Discriminants (T)
14665 and then Is_Private_Type (T))
14666 then
14667 Error_Msg_N ("invalid constraint: type has no discriminant", C);
14668 Fixup_Bad_Constraint;
14669 return;
14671 elsif Is_Constrained (E)
14672 or else (Ekind (E) = E_Class_Wide_Subtype
14673 and then Present (Discriminant_Constraint (E)))
14674 then
14675 Error_Msg_N ("type is already constrained", Subtype_Mark (S));
14676 Fixup_Bad_Constraint;
14677 return;
14678 end if;
14680 -- T may be an unconstrained subtype (e.g. a generic actual). Constraint
14681 -- applies to the base type.
14683 T := Base_Type (T);
14685 Constr := Build_Discriminant_Constraints (T, S);
14687 -- If the list returned was empty we had an error in building the
14688 -- discriminant constraint. We have also already signalled an error
14689 -- in the incomplete type case
14691 if Is_Empty_Elmt_List (Constr) then
14692 Fixup_Bad_Constraint;
14693 return;
14694 end if;
14696 Build_Discriminated_Subtype (T, Def_Id, Constr, Related_Nod, For_Access);
14697 end Constrain_Discriminated_Type;
14699 ---------------------------
14700 -- Constrain_Enumeration --
14701 ---------------------------
14703 procedure Constrain_Enumeration (Def_Id : Entity_Id; S : Node_Id) is
14704 T : constant Entity_Id := Entity (Subtype_Mark (S));
14705 C : constant Node_Id := Constraint (S);
14707 begin
14708 Mutate_Ekind (Def_Id, E_Enumeration_Subtype);
14710 Set_First_Literal (Def_Id, First_Literal (Base_Type (T)));
14711 Set_Etype (Def_Id, Base_Type (T));
14712 Set_Size_Info (Def_Id, (T));
14713 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
14714 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14716 -- Inherit the chain of representation items instead of replacing it
14717 -- because Build_Derived_Enumeration_Type rewrites the declaration of
14718 -- the derived type as a subtype declaration and the former needs to
14719 -- preserve existing representation items (see Build_Derived_Type).
14721 Inherit_Rep_Item_Chain (Def_Id, T);
14723 Set_Discrete_RM_Size (Def_Id);
14724 end Constrain_Enumeration;
14726 ----------------------
14727 -- Constrain_Float --
14728 ----------------------
14730 procedure Constrain_Float (Def_Id : Entity_Id; S : Node_Id) is
14731 T : constant Entity_Id := Entity (Subtype_Mark (S));
14732 C : Node_Id;
14733 D : Node_Id;
14734 Rais : Node_Id;
14736 begin
14737 Mutate_Ekind (Def_Id, E_Floating_Point_Subtype);
14739 Set_Etype (Def_Id, Base_Type (T));
14740 Set_Size_Info (Def_Id, (T));
14741 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14743 -- Process the constraint
14745 C := Constraint (S);
14747 -- Digits constraint present
14749 if Nkind (C) = N_Digits_Constraint then
14750 Check_Restriction (No_Obsolescent_Features, C);
14752 if Warn_On_Obsolescent_Feature then
14753 Error_Msg_N
14754 ("subtype digits constraint is an " &
14755 "obsolescent feature (RM J.3(8))?j?", C);
14756 end if;
14758 D := Digits_Expression (C);
14759 Analyze_And_Resolve (D, Any_Integer);
14760 Check_Digits_Expression (D);
14761 Set_Digits_Value (Def_Id, Expr_Value (D));
14763 -- Check that digits value is in range. Obviously we can do this
14764 -- at compile time, but it is strictly a runtime check, and of
14765 -- course there is an ACVC test that checks this.
14767 if Digits_Value (Def_Id) > Digits_Value (T) then
14768 Error_Msg_Uint_1 := Digits_Value (T);
14769 Error_Msg_N ("??digits value is too large, maximum is ^", D);
14770 Rais :=
14771 Make_Raise_Constraint_Error (Sloc (D),
14772 Reason => CE_Range_Check_Failed);
14773 Insert_Action (Declaration_Node (Def_Id), Rais);
14774 end if;
14776 C := Range_Constraint (C);
14778 -- No digits constraint present
14780 else
14781 Set_Digits_Value (Def_Id, Digits_Value (T));
14782 end if;
14784 -- Range constraint present
14786 if Nkind (C) = N_Range_Constraint then
14787 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14789 -- No range constraint present
14791 else
14792 pragma Assert (No (C));
14793 Set_Scalar_Range (Def_Id, Scalar_Range (T));
14794 end if;
14796 Set_Is_Constrained (Def_Id);
14797 end Constrain_Float;
14799 ---------------------
14800 -- Constrain_Index --
14801 ---------------------
14803 procedure Constrain_Index
14804 (Index : Node_Id;
14805 S : Node_Id;
14806 Related_Nod : Node_Id;
14807 Related_Id : Entity_Id;
14808 Suffix : Character;
14809 Suffix_Index : Pos)
14811 Def_Id : Entity_Id;
14812 R : Node_Id := Empty;
14813 T : constant Entity_Id := Etype (Index);
14814 Is_FLB_Index : Boolean := False;
14816 begin
14817 Def_Id :=
14818 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
14819 Set_Etype (Def_Id, Base_Type (T));
14821 if Nkind (S) = N_Range
14822 or else
14823 (Nkind (S) = N_Attribute_Reference
14824 and then Attribute_Name (S) = Name_Range)
14825 then
14826 -- A Range attribute will be transformed into N_Range by Resolve
14828 -- If a range has an Empty upper bound, then remember that for later
14829 -- setting of the index subtype's Is_Fixed_Lower_Bound_Index_Subtype
14830 -- flag, and also set the upper bound of the range to the index
14831 -- subtype's upper bound rather than leaving it Empty. In truth,
14832 -- that upper bound corresponds to a box ("<>"), but it's convenient
14833 -- to set it to the upper bound to avoid needing to add special tests
14834 -- in various places for an Empty upper bound, and in any case it
14835 -- accurately characterizes the index's range of values.
14837 if Nkind (S) = N_Range and then No (High_Bound (S)) then
14838 Is_FLB_Index := True;
14839 Set_High_Bound (S, Type_High_Bound (T));
14840 end if;
14842 R := S;
14844 Process_Range_Expr_In_Decl (R, T);
14846 if not Error_Posted (S)
14847 and then
14848 (Nkind (S) /= N_Range
14849 or else not Covers (T, (Etype (Low_Bound (S))))
14850 or else not Covers (T, (Etype (High_Bound (S)))))
14851 then
14852 if Base_Type (T) /= Any_Type
14853 and then Etype (Low_Bound (S)) /= Any_Type
14854 and then Etype (High_Bound (S)) /= Any_Type
14855 then
14856 Error_Msg_N ("range expected", S);
14857 end if;
14858 end if;
14860 elsif Nkind (S) = N_Subtype_Indication then
14862 -- The parser has verified that this is a discrete indication
14864 Resolve_Discrete_Subtype_Indication (S, T);
14865 Bad_Predicated_Subtype_Use
14866 ("subtype& has predicate, not allowed in index constraint",
14867 S, Entity (Subtype_Mark (S)));
14869 R := Range_Expression (Constraint (S));
14871 -- Capture values of bounds and generate temporaries for them if
14872 -- needed, since checks may cause duplication of the expressions
14873 -- which must not be reevaluated.
14875 -- The forced evaluation removes side effects from expressions, which
14876 -- should occur also in GNATprove mode. Otherwise, we end up with
14877 -- unexpected insertions of actions at places where this is not
14878 -- supposed to occur, e.g. on default parameters of a call.
14880 if Expander_Active or GNATprove_Mode then
14881 Force_Evaluation
14882 (Low_Bound (R), Related_Id => Def_Id, Is_Low_Bound => True);
14883 Force_Evaluation
14884 (High_Bound (R), Related_Id => Def_Id, Is_High_Bound => True);
14885 end if;
14887 elsif Nkind (S) = N_Discriminant_Association then
14889 -- Syntactically valid in subtype indication
14891 Error_Msg_N ("invalid index constraint", S);
14892 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
14893 return;
14895 -- Subtype_Mark case, no anonymous subtypes to construct
14897 else
14898 Analyze (S);
14900 if Is_Entity_Name (S) then
14901 if not Is_Type (Entity (S)) then
14902 Error_Msg_N ("expect subtype mark for index constraint", S);
14904 elsif Base_Type (Entity (S)) /= Base_Type (T) then
14905 Wrong_Type (S, Base_Type (T));
14907 -- Check error of subtype with predicate in index constraint
14909 else
14910 Bad_Predicated_Subtype_Use
14911 ("subtype& has predicate, not allowed in index constraint",
14912 S, Entity (S));
14913 end if;
14915 return;
14917 else
14918 Error_Msg_N ("invalid index constraint", S);
14919 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
14920 return;
14921 end if;
14922 end if;
14924 -- Complete construction of the Itype
14926 if Is_Modular_Integer_Type (T) then
14927 Mutate_Ekind (Def_Id, E_Modular_Integer_Subtype);
14929 elsif Is_Integer_Type (T) then
14930 Mutate_Ekind (Def_Id, E_Signed_Integer_Subtype);
14932 else
14933 Mutate_Ekind (Def_Id, E_Enumeration_Subtype);
14934 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
14935 Set_First_Literal (Def_Id, First_Literal (T));
14936 end if;
14938 Set_Size_Info (Def_Id, (T));
14939 Copy_RM_Size (To => Def_Id, From => T);
14940 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14942 -- If this is a range for a fixed-lower-bound subtype, then set the
14943 -- index itype's low bound to the FLB and the index itype's upper bound
14944 -- to the high bound of the parent array type's index subtype. Also,
14945 -- mark the itype as an FLB index subtype.
14947 if Nkind (S) = N_Range and then Is_FLB_Index then
14948 Set_Scalar_Range
14949 (Def_Id,
14950 Make_Range (Sloc (S),
14951 Low_Bound => Low_Bound (S),
14952 High_Bound => Type_High_Bound (T)));
14953 Set_Is_Fixed_Lower_Bound_Index_Subtype (Def_Id);
14955 else
14956 Set_Scalar_Range (Def_Id, R);
14957 end if;
14959 Set_Etype (S, Def_Id);
14960 Set_Discrete_RM_Size (Def_Id);
14961 end Constrain_Index;
14963 -----------------------
14964 -- Constrain_Integer --
14965 -----------------------
14967 procedure Constrain_Integer (Def_Id : Entity_Id; S : Node_Id) is
14968 T : constant Entity_Id := Entity (Subtype_Mark (S));
14969 C : constant Node_Id := Constraint (S);
14971 begin
14972 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14974 if Is_Modular_Integer_Type (T) then
14975 Mutate_Ekind (Def_Id, E_Modular_Integer_Subtype);
14976 else
14977 Mutate_Ekind (Def_Id, E_Signed_Integer_Subtype);
14978 end if;
14980 Set_Etype (Def_Id, Base_Type (T));
14981 Set_Size_Info (Def_Id, (T));
14982 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14983 Set_Discrete_RM_Size (Def_Id);
14984 end Constrain_Integer;
14986 ------------------------------
14987 -- Constrain_Ordinary_Fixed --
14988 ------------------------------
14990 procedure Constrain_Ordinary_Fixed (Def_Id : Entity_Id; S : Node_Id) is
14991 T : constant Entity_Id := Entity (Subtype_Mark (S));
14992 C : Node_Id;
14993 D : Node_Id;
14994 Rais : Node_Id;
14996 begin
14997 Mutate_Ekind (Def_Id, E_Ordinary_Fixed_Point_Subtype);
14998 Set_Etype (Def_Id, Base_Type (T));
14999 Set_Size_Info (Def_Id, (T));
15000 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
15001 Set_Small_Value (Def_Id, Small_Value (T));
15003 -- Process the constraint
15005 C := Constraint (S);
15007 -- Delta constraint present
15009 if Nkind (C) = N_Delta_Constraint then
15010 Check_Restriction (No_Obsolescent_Features, C);
15012 if Warn_On_Obsolescent_Feature then
15013 Error_Msg_S
15014 ("subtype delta constraint is an " &
15015 "obsolescent feature (RM J.3(7))?j?");
15016 end if;
15018 D := Delta_Expression (C);
15019 Analyze_And_Resolve (D, Any_Real);
15020 Check_Delta_Expression (D);
15021 Set_Delta_Value (Def_Id, Expr_Value_R (D));
15023 -- Check that delta value is in range. Obviously we can do this
15024 -- at compile time, but it is strictly a runtime check, and of
15025 -- course there is an ACVC test that checks this.
15027 if Delta_Value (Def_Id) < Delta_Value (T) then
15028 Error_Msg_N ("??delta value is too small", D);
15029 Rais :=
15030 Make_Raise_Constraint_Error (Sloc (D),
15031 Reason => CE_Range_Check_Failed);
15032 Insert_Action (Declaration_Node (Def_Id), Rais);
15033 end if;
15035 C := Range_Constraint (C);
15037 -- No delta constraint present
15039 else
15040 Set_Delta_Value (Def_Id, Delta_Value (T));
15041 end if;
15043 -- Range constraint present
15045 if Nkind (C) = N_Range_Constraint then
15046 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
15048 -- No range constraint present
15050 else
15051 pragma Assert (No (C));
15052 Set_Scalar_Range (Def_Id, Scalar_Range (T));
15053 end if;
15055 Set_Discrete_RM_Size (Def_Id);
15057 -- Unconditionally delay the freeze, since we cannot set size
15058 -- information in all cases correctly until the freeze point.
15060 Set_Has_Delayed_Freeze (Def_Id);
15061 end Constrain_Ordinary_Fixed;
15063 -----------------------
15064 -- Contain_Interface --
15065 -----------------------
15067 function Contain_Interface
15068 (Iface : Entity_Id;
15069 Ifaces : Elist_Id) return Boolean
15071 Iface_Elmt : Elmt_Id;
15073 begin
15074 if Present (Ifaces) then
15075 Iface_Elmt := First_Elmt (Ifaces);
15076 while Present (Iface_Elmt) loop
15077 if Node (Iface_Elmt) = Iface then
15078 return True;
15079 end if;
15081 Next_Elmt (Iface_Elmt);
15082 end loop;
15083 end if;
15085 return False;
15086 end Contain_Interface;
15088 ---------------------------
15089 -- Convert_Scalar_Bounds --
15090 ---------------------------
15092 procedure Convert_Scalar_Bounds
15093 (N : Node_Id;
15094 Parent_Type : Entity_Id;
15095 Derived_Type : Entity_Id;
15096 Loc : Source_Ptr)
15098 Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
15100 Lo : Node_Id;
15101 Hi : Node_Id;
15102 Rng : Node_Id;
15104 begin
15105 -- Defend against previous errors
15107 if No (Scalar_Range (Derived_Type)) then
15108 Check_Error_Detected;
15109 return;
15110 end if;
15112 Lo := Build_Scalar_Bound
15113 (Type_Low_Bound (Derived_Type),
15114 Parent_Type, Implicit_Base);
15116 Hi := Build_Scalar_Bound
15117 (Type_High_Bound (Derived_Type),
15118 Parent_Type, Implicit_Base);
15120 Rng :=
15121 Make_Range (Loc,
15122 Low_Bound => Lo,
15123 High_Bound => Hi);
15125 Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
15127 Set_Parent (Rng, N);
15128 Set_Scalar_Range (Derived_Type, Rng);
15130 -- Analyze the bounds
15132 Analyze_And_Resolve (Lo, Implicit_Base);
15133 Analyze_And_Resolve (Hi, Implicit_Base);
15135 -- Analyze the range itself, except that we do not analyze it if
15136 -- the bounds are real literals, and we have a fixed-point type.
15137 -- The reason for this is that we delay setting the bounds in this
15138 -- case till we know the final Small and Size values (see circuit
15139 -- in Freeze.Freeze_Fixed_Point_Type for further details).
15141 if Is_Fixed_Point_Type (Parent_Type)
15142 and then Nkind (Lo) = N_Real_Literal
15143 and then Nkind (Hi) = N_Real_Literal
15144 then
15145 return;
15147 -- Here we do the analysis of the range
15149 -- Note: we do this manually, since if we do a normal Analyze and
15150 -- Resolve call, there are problems with the conversions used for
15151 -- the derived type range.
15153 else
15154 Set_Etype (Rng, Implicit_Base);
15155 Set_Analyzed (Rng, True);
15156 end if;
15157 end Convert_Scalar_Bounds;
15159 -------------------
15160 -- Copy_And_Swap --
15161 -------------------
15163 procedure Copy_And_Swap (Priv, Full : Entity_Id) is
15164 begin
15165 -- Initialize new full declaration entity by copying the pertinent
15166 -- fields of the corresponding private declaration entity.
15168 -- We temporarily set Ekind to a value appropriate for a type to
15169 -- avoid assert failures in Einfo from checking for setting type
15170 -- attributes on something that is not a type. Ekind (Priv) is an
15171 -- appropriate choice, since it allowed the attributes to be set
15172 -- in the first place. This Ekind value will be modified later.
15174 Mutate_Ekind (Full, Ekind (Priv));
15176 -- Also set Etype temporarily to Any_Type, again, in the absence
15177 -- of errors, it will be properly reset, and if there are errors,
15178 -- then we want a value of Any_Type to remain.
15180 Set_Etype (Full, Any_Type);
15182 -- Now start copying attributes
15184 Set_Has_Discriminants (Full, Has_Discriminants (Priv));
15186 if Has_Discriminants (Full) then
15187 Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
15188 Set_Stored_Constraint (Full, Stored_Constraint (Priv));
15189 end if;
15191 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
15192 Set_Homonym (Full, Homonym (Priv));
15193 Set_Is_Immediately_Visible (Full, Is_Immediately_Visible (Priv));
15194 Set_Is_Public (Full, Is_Public (Priv));
15195 Set_Is_Pure (Full, Is_Pure (Priv));
15196 Set_Is_Tagged_Type (Full, Is_Tagged_Type (Priv));
15197 Set_Has_Pragma_Unmodified (Full, Has_Pragma_Unmodified (Priv));
15198 Set_Has_Pragma_Unreferenced (Full, Has_Pragma_Unreferenced (Priv));
15199 Set_Has_Pragma_Unreferenced_Objects
15200 (Full, Has_Pragma_Unreferenced_Objects
15201 (Priv));
15203 Conditional_Delay (Full, Priv);
15205 if Is_Tagged_Type (Full) then
15206 Set_Direct_Primitive_Operations
15207 (Full, Direct_Primitive_Operations (Priv));
15208 Set_No_Tagged_Streams_Pragma
15209 (Full, No_Tagged_Streams_Pragma (Priv));
15211 if Is_Base_Type (Priv) then
15212 Set_Class_Wide_Type (Full, Class_Wide_Type (Priv));
15213 end if;
15214 end if;
15216 Set_Is_Volatile (Full, Is_Volatile (Priv));
15217 Set_Treat_As_Volatile (Full, Treat_As_Volatile (Priv));
15218 Set_Scope (Full, Scope (Priv));
15219 Set_Prev_Entity (Full, Prev_Entity (Priv));
15220 Set_Next_Entity (Full, Next_Entity (Priv));
15221 Set_First_Entity (Full, First_Entity (Priv));
15222 Set_Last_Entity (Full, Last_Entity (Priv));
15224 -- If access types have been recorded for later handling, keep them in
15225 -- the full view so that they get handled when the full view freeze
15226 -- node is expanded.
15228 if Present (Freeze_Node (Priv))
15229 and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
15230 then
15231 Ensure_Freeze_Node (Full);
15232 Set_Access_Types_To_Process
15233 (Freeze_Node (Full),
15234 Access_Types_To_Process (Freeze_Node (Priv)));
15235 end if;
15237 -- Swap the two entities. Now Private is the full type entity and Full
15238 -- is the private one. They will be swapped back at the end of the
15239 -- private part. This swapping ensures that the entity that is visible
15240 -- in the private part is the full declaration.
15242 Exchange_Entities (Priv, Full);
15243 Set_Is_Not_Self_Hidden (Priv);
15244 Append_Entity (Full, Scope (Full));
15245 end Copy_And_Swap;
15247 -------------------------------------
15248 -- Copy_Array_Base_Type_Attributes --
15249 -------------------------------------
15251 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
15252 begin
15253 Set_Component_Alignment (T1, Component_Alignment (T2));
15254 Set_Component_Type (T1, Component_Type (T2));
15255 Set_Component_Size (T1, Component_Size (T2));
15256 Set_Has_Non_Standard_Rep (T1, Has_Non_Standard_Rep (T2));
15257 Propagate_Concurrent_Flags (T1, T2);
15258 Propagate_Controlled_Flags (T1, T2);
15259 Set_Is_Packed (T1, Is_Packed (T2));
15260 Set_Has_Aliased_Components (T1, Has_Aliased_Components (T2));
15261 Set_Has_Atomic_Components (T1, Has_Atomic_Components (T2));
15262 Set_Has_Independent_Components (T1, Has_Independent_Components (T2));
15263 Set_Has_Volatile_Components (T1, Has_Volatile_Components (T2));
15264 end Copy_Array_Base_Type_Attributes;
15266 -----------------------------------
15267 -- Copy_Array_Subtype_Attributes --
15268 -----------------------------------
15270 -- Note that we used to copy Packed_Array_Impl_Type too here, but we now
15271 -- let it be recreated during freezing for the sake of better debug info.
15273 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
15274 begin
15275 Set_Size_Info (T1, T2);
15277 Set_First_Index (T1, First_Index (T2));
15278 Set_Is_Aliased (T1, Is_Aliased (T2));
15279 Set_Is_Atomic (T1, Is_Atomic (T2));
15280 Set_Is_Independent (T1, Is_Independent (T2));
15281 Set_Is_Volatile (T1, Is_Volatile (T2));
15282 Set_Is_Volatile_Full_Access (T1, Is_Volatile_Full_Access (T2));
15283 Set_Treat_As_Volatile (T1, Treat_As_Volatile (T2));
15284 Set_Is_Constrained (T1, Is_Constrained (T2));
15285 Set_Depends_On_Private (T1, Has_Private_Component (T2));
15286 Inherit_Rep_Item_Chain (T1, T2);
15287 Set_Convention (T1, Convention (T2));
15288 Set_Is_Limited_Composite (T1, Is_Limited_Composite (T2));
15289 Set_Is_Private_Composite (T1, Is_Private_Composite (T2));
15290 end Copy_Array_Subtype_Attributes;
15292 -----------------------------------
15293 -- Create_Constrained_Components --
15294 -----------------------------------
15296 procedure Create_Constrained_Components
15297 (Subt : Entity_Id;
15298 Decl_Node : Node_Id;
15299 Typ : Entity_Id;
15300 Constraints : Elist_Id)
15302 Loc : constant Source_Ptr := Sloc (Subt);
15303 Comp_List : constant Elist_Id := New_Elmt_List;
15304 Parent_Type : constant Entity_Id := Etype (Typ);
15306 Assoc_List : List_Id;
15307 Discr_Val : Elmt_Id;
15308 Errors : Boolean;
15309 New_C : Entity_Id;
15310 Old_C : Entity_Id;
15311 Is_Static : Boolean := True;
15312 Is_Compile_Time_Known : Boolean := True;
15314 procedure Collect_Fixed_Components (Typ : Entity_Id);
15315 -- Collect parent type components that do not appear in a variant part
15317 procedure Create_All_Components;
15318 -- Iterate over Comp_List to create the components of the subtype
15320 function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
15321 -- Creates a new component from Old_Compon, copying all the fields from
15322 -- it, including its Etype, inserts the new component in the Subt entity
15323 -- chain and returns the new component.
15325 function Is_Variant_Record (T : Entity_Id) return Boolean;
15326 -- If true, and discriminants are static, collect only components from
15327 -- variants selected by discriminant values.
15329 ------------------------------
15330 -- Collect_Fixed_Components --
15331 ------------------------------
15333 procedure Collect_Fixed_Components (Typ : Entity_Id) is
15334 begin
15335 -- Build association list for discriminants, and find components of
15336 -- the variant part selected by the values of the discriminants.
15338 Assoc_List := New_List;
15340 Old_C := First_Discriminant (Typ);
15341 Discr_Val := First_Elmt (Constraints);
15342 while Present (Old_C) loop
15343 Append_To (Assoc_List,
15344 Make_Component_Association (Loc,
15345 Choices => New_List (New_Occurrence_Of (Old_C, Loc)),
15346 Expression => New_Copy (Node (Discr_Val))));
15348 Next_Elmt (Discr_Val);
15349 Next_Discriminant (Old_C);
15350 end loop;
15352 -- The tag and the possible parent component are unconditionally in
15353 -- the subtype.
15355 if Is_Tagged_Type (Typ) or else Has_Controlled_Component (Typ) then
15356 Old_C := First_Component (Typ);
15357 while Present (Old_C) loop
15358 if Chars (Old_C) in Name_uTag | Name_uParent then
15359 Append_Elmt (Old_C, Comp_List);
15360 end if;
15362 Next_Component (Old_C);
15363 end loop;
15364 end if;
15365 end Collect_Fixed_Components;
15367 ---------------------------
15368 -- Create_All_Components --
15369 ---------------------------
15371 procedure Create_All_Components is
15372 Comp : Elmt_Id;
15374 begin
15375 Comp := First_Elmt (Comp_List);
15376 while Present (Comp) loop
15377 Old_C := Node (Comp);
15378 New_C := Create_Component (Old_C);
15380 Set_Etype
15381 (New_C,
15382 Constrain_Component_Type
15383 (Old_C, Subt, Decl_Node, Typ, Constraints));
15384 Set_Is_Public (New_C, Is_Public (Subt));
15386 Next_Elmt (Comp);
15387 end loop;
15388 end Create_All_Components;
15390 ----------------------
15391 -- Create_Component --
15392 ----------------------
15394 function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
15395 New_Compon : constant Entity_Id := New_Copy (Old_Compon);
15397 begin
15398 if Ekind (Old_Compon) = E_Discriminant
15399 and then Is_Completely_Hidden (Old_Compon)
15400 then
15401 -- This is a shadow discriminant created for a discriminant of
15402 -- the parent type, which needs to be present in the subtype.
15403 -- Give the shadow discriminant an internal name that cannot
15404 -- conflict with that of visible components.
15406 Set_Chars (New_Compon, New_Internal_Name ('C'));
15407 end if;
15409 -- Set the parent so we have a proper link for freezing etc. This is
15410 -- not a real parent pointer, since of course our parent does not own
15411 -- up to us and reference us, we are an illegitimate child of the
15412 -- original parent.
15414 Set_Parent (New_Compon, Parent (Old_Compon));
15416 -- We do not want this node marked as Comes_From_Source, since
15417 -- otherwise it would get first class status and a separate cross-
15418 -- reference line would be generated. Illegitimate children do not
15419 -- rate such recognition.
15421 Set_Comes_From_Source (New_Compon, False);
15423 -- But it is a real entity, and a birth certificate must be properly
15424 -- registered by entering it into the entity list, and setting its
15425 -- scope to the given subtype. This turns out to be useful for the
15426 -- LLVM code generator, but that scope is not used otherwise.
15428 Enter_Name (New_Compon);
15429 Set_Scope (New_Compon, Subt);
15431 return New_Compon;
15432 end Create_Component;
15434 -----------------------
15435 -- Is_Variant_Record --
15436 -----------------------
15438 function Is_Variant_Record (T : Entity_Id) return Boolean is
15439 Decl : constant Node_Id := Parent (T);
15440 begin
15441 return Nkind (Decl) = N_Full_Type_Declaration
15442 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
15443 and then Present (Component_List (Type_Definition (Decl)))
15444 and then
15445 Present (Variant_Part (Component_List (Type_Definition (Decl))));
15446 end Is_Variant_Record;
15448 -- Start of processing for Create_Constrained_Components
15450 begin
15451 pragma Assert (Subt /= Base_Type (Subt));
15452 pragma Assert (Typ = Base_Type (Typ));
15454 Set_First_Entity (Subt, Empty);
15455 Set_Last_Entity (Subt, Empty);
15457 -- Check whether constraint is fully static, in which case we can
15458 -- optimize the list of components.
15460 Discr_Val := First_Elmt (Constraints);
15461 while Present (Discr_Val) loop
15462 if not Is_OK_Static_Expression (Node (Discr_Val)) then
15463 Is_Static := False;
15465 if not Compile_Time_Known_Value (Node (Discr_Val)) then
15466 Is_Compile_Time_Known := False;
15467 exit;
15468 end if;
15469 end if;
15471 Next_Elmt (Discr_Val);
15472 end loop;
15474 Set_Has_Static_Discriminants (Subt, Is_Static);
15476 Push_Scope (Subt);
15478 -- Inherit the discriminants of the parent type
15480 Add_Discriminants : declare
15481 Num_Disc : Nat;
15482 Num_Stor : Nat;
15484 begin
15485 Num_Disc := 0;
15486 Old_C := First_Discriminant (Typ);
15488 while Present (Old_C) loop
15489 Num_Disc := Num_Disc + 1;
15490 New_C := Create_Component (Old_C);
15491 Set_Is_Public (New_C, Is_Public (Subt));
15492 Next_Discriminant (Old_C);
15493 end loop;
15495 -- For an untagged derived subtype, the number of discriminants may
15496 -- be smaller than the number of inherited discriminants, because
15497 -- several of them may be renamed by a single new discriminant or
15498 -- constrained. In this case, add the hidden discriminants back into
15499 -- the subtype, because they need to be present if the optimizer of
15500 -- the GCC 4.x back-end decides to break apart assignments between
15501 -- objects using the parent view into member-wise assignments.
15503 Num_Stor := 0;
15505 if Is_Derived_Type (Typ)
15506 and then not Is_Tagged_Type (Typ)
15507 then
15508 Old_C := First_Stored_Discriminant (Typ);
15510 while Present (Old_C) loop
15511 Num_Stor := Num_Stor + 1;
15512 Next_Stored_Discriminant (Old_C);
15513 end loop;
15514 end if;
15516 if Num_Stor > Num_Disc then
15518 -- Find out multiple uses of new discriminants, and add hidden
15519 -- components for the extra renamed discriminants. We recognize
15520 -- multiple uses through the Corresponding_Discriminant of a
15521 -- new discriminant: if it constrains several old discriminants,
15522 -- this field points to the last one in the parent type. The
15523 -- stored discriminants of the derived type have the same name
15524 -- as those of the parent.
15526 declare
15527 Constr : Elmt_Id;
15528 New_Discr : Entity_Id;
15529 Old_Discr : Entity_Id;
15531 begin
15532 Constr := First_Elmt (Stored_Constraint (Typ));
15533 Old_Discr := First_Stored_Discriminant (Typ);
15534 while Present (Constr) loop
15535 if Is_Entity_Name (Node (Constr))
15536 and then Ekind (Entity (Node (Constr))) = E_Discriminant
15537 then
15538 New_Discr := Entity (Node (Constr));
15540 if Chars (Corresponding_Discriminant (New_Discr)) /=
15541 Chars (Old_Discr)
15542 then
15543 -- The new discriminant has been used to rename a
15544 -- subsequent old discriminant. Introduce a shadow
15545 -- component for the current old discriminant.
15547 New_C := Create_Component (Old_Discr);
15548 Set_Original_Record_Component (New_C, Old_Discr);
15549 end if;
15551 else
15552 -- The constraint has eliminated the old discriminant.
15553 -- Introduce a shadow component.
15555 New_C := Create_Component (Old_Discr);
15556 Set_Original_Record_Component (New_C, Old_Discr);
15557 end if;
15559 Next_Elmt (Constr);
15560 Next_Stored_Discriminant (Old_Discr);
15561 end loop;
15562 end;
15563 end if;
15564 end Add_Discriminants;
15566 if Is_Compile_Time_Known
15567 and then Is_Variant_Record (Typ)
15568 then
15569 Collect_Fixed_Components (Typ);
15570 Gather_Components
15571 (Typ,
15572 Component_List (Type_Definition (Parent (Typ))),
15573 Governed_By => Assoc_List,
15574 Into => Comp_List,
15575 Report_Errors => Errors,
15576 Allow_Compile_Time => True);
15577 pragma Assert (not Errors or else Serious_Errors_Detected > 0);
15579 Create_All_Components;
15581 -- If the subtype declaration is created for a tagged type derivation
15582 -- with constraints, we retrieve the record definition of the parent
15583 -- type to select the components of the proper variant.
15585 elsif Is_Compile_Time_Known
15586 and then Is_Tagged_Type (Typ)
15587 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
15588 and then
15589 Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
15590 and then Is_Variant_Record (Parent_Type)
15591 then
15592 Collect_Fixed_Components (Typ);
15593 Gather_Components
15594 (Typ,
15595 Component_List (Type_Definition (Parent (Parent_Type))),
15596 Governed_By => Assoc_List,
15597 Into => Comp_List,
15598 Report_Errors => Errors,
15599 Allow_Compile_Time => True);
15601 -- Note: previously there was a check at this point that no errors
15602 -- were detected. As a consequence of AI05-220 there may be an error
15603 -- if an inherited discriminant that controls a variant has a non-
15604 -- static constraint.
15606 -- If the tagged derivation has a type extension, collect all the
15607 -- new relevant components therein via Gather_Components.
15609 if Present (Record_Extension_Part (Type_Definition (Parent (Typ))))
15610 then
15611 Gather_Components
15612 (Typ,
15613 Component_List
15614 (Record_Extension_Part (Type_Definition (Parent (Typ)))),
15615 Governed_By => Assoc_List,
15616 Into => Comp_List,
15617 Report_Errors => Errors,
15618 Allow_Compile_Time => True,
15619 Include_Interface_Tag => True);
15620 end if;
15622 Create_All_Components;
15624 else
15625 -- If discriminants are not static, or if this is a multi-level type
15626 -- extension, we have to include all components of the parent type.
15628 Old_C := First_Component (Typ);
15629 while Present (Old_C) loop
15630 New_C := Create_Component (Old_C);
15632 Set_Etype
15633 (New_C,
15634 Constrain_Component_Type
15635 (Old_C, Subt, Decl_Node, Typ, Constraints));
15636 Set_Is_Public (New_C, Is_Public (Subt));
15638 Next_Component (Old_C);
15639 end loop;
15640 end if;
15642 End_Scope;
15643 end Create_Constrained_Components;
15645 ------------------------------------------
15646 -- Decimal_Fixed_Point_Type_Declaration --
15647 ------------------------------------------
15649 procedure Decimal_Fixed_Point_Type_Declaration
15650 (T : Entity_Id;
15651 Def : Node_Id)
15653 Loc : constant Source_Ptr := Sloc (Def);
15654 Digs_Expr : constant Node_Id := Digits_Expression (Def);
15655 Delta_Expr : constant Node_Id := Delta_Expression (Def);
15656 Max_Digits : constant Nat :=
15657 (if System_Max_Integer_Size = 128 then 38 else 18);
15658 -- Maximum number of digits that can be represented in an integer
15660 Implicit_Base : Entity_Id;
15661 Digs_Val : Uint;
15662 Delta_Val : Ureal;
15663 Scale_Val : Uint;
15664 Bound_Val : Ureal;
15666 begin
15667 Check_Restriction (No_Fixed_Point, Def);
15669 -- Create implicit base type
15671 Implicit_Base :=
15672 Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
15673 Set_Etype (Implicit_Base, Implicit_Base);
15675 -- Analyze and process delta expression
15677 Analyze_And_Resolve (Delta_Expr, Universal_Real);
15679 Check_Delta_Expression (Delta_Expr);
15680 Delta_Val := Expr_Value_R (Delta_Expr);
15682 -- Check delta is power of 10, and determine scale value from it
15684 declare
15685 Val : Ureal;
15687 begin
15688 Scale_Val := Uint_0;
15689 Val := Delta_Val;
15691 if Val < Ureal_1 then
15692 while Val < Ureal_1 loop
15693 Val := Val * Ureal_10;
15694 Scale_Val := Scale_Val + 1;
15695 end loop;
15697 if Scale_Val > Max_Digits then
15698 Error_Msg_Uint_1 := UI_From_Int (Max_Digits);
15699 Error_Msg_N ("scale exceeds maximum value of ^", Def);
15700 Scale_Val := UI_From_Int (Max_Digits);
15701 end if;
15703 else
15704 while Val > Ureal_1 loop
15705 Val := Val / Ureal_10;
15706 Scale_Val := Scale_Val - 1;
15707 end loop;
15709 if Scale_Val < -Max_Digits then
15710 Error_Msg_Uint_1 := UI_From_Int (-Max_Digits);
15711 Error_Msg_N ("scale is less than minimum value of ^", Def);
15712 Scale_Val := UI_From_Int (-Max_Digits);
15713 end if;
15714 end if;
15716 if Val /= Ureal_1 then
15717 Error_Msg_N ("delta expression must be a power of 10", Def);
15718 Delta_Val := Ureal_10 ** (-Scale_Val);
15719 end if;
15720 end;
15722 -- Set delta, scale and small (small = delta for decimal type)
15724 Set_Delta_Value (Implicit_Base, Delta_Val);
15725 Set_Scale_Value (Implicit_Base, Scale_Val);
15726 Set_Small_Value (Implicit_Base, Delta_Val);
15728 -- Analyze and process digits expression
15730 Analyze_And_Resolve (Digs_Expr, Any_Integer);
15731 Check_Digits_Expression (Digs_Expr);
15732 Digs_Val := Expr_Value (Digs_Expr);
15734 if Digs_Val > Max_Digits then
15735 Error_Msg_Uint_1 := UI_From_Int (Max_Digits);
15736 Error_Msg_N ("digits value out of range, maximum is ^", Digs_Expr);
15737 Digs_Val := UI_From_Int (Max_Digits);
15738 end if;
15740 Set_Digits_Value (Implicit_Base, Digs_Val);
15741 Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
15743 -- Set range of base type from digits value for now. This will be
15744 -- expanded to represent the true underlying base range by Freeze.
15746 Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
15748 -- Note: We leave Esize unset for now, size will be set at freeze
15749 -- time. We have to do this for ordinary fixed-point, because the size
15750 -- depends on the specified small, and we might as well do the same for
15751 -- decimal fixed-point.
15753 pragma Assert (not Known_Esize (Implicit_Base));
15755 -- If there are bounds given in the declaration use them as the
15756 -- bounds of the first named subtype.
15758 if Present (Real_Range_Specification (Def)) then
15759 declare
15760 RRS : constant Node_Id := Real_Range_Specification (Def);
15761 Low : constant Node_Id := Low_Bound (RRS);
15762 High : constant Node_Id := High_Bound (RRS);
15763 Low_Val : Ureal;
15764 High_Val : Ureal;
15766 begin
15767 Analyze_And_Resolve (Low, Any_Real);
15768 Analyze_And_Resolve (High, Any_Real);
15769 Check_Real_Bound (Low);
15770 Check_Real_Bound (High);
15771 Low_Val := Expr_Value_R (Low);
15772 High_Val := Expr_Value_R (High);
15774 if Low_Val < (-Bound_Val) then
15775 Error_Msg_N
15776 ("range low bound too small for digits value", Low);
15777 Low_Val := -Bound_Val;
15778 end if;
15780 if High_Val > Bound_Val then
15781 Error_Msg_N
15782 ("range high bound too large for digits value", High);
15783 High_Val := Bound_Val;
15784 end if;
15786 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
15787 end;
15789 -- If no explicit range, use range that corresponds to given
15790 -- digits value. This will end up as the final range for the
15791 -- first subtype.
15793 else
15794 Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
15795 end if;
15797 -- Complete entity for first subtype. The inheritance of the rep item
15798 -- chain ensures that SPARK-related pragmas are not clobbered when the
15799 -- decimal fixed point type acts as a full view of a private type.
15801 Mutate_Ekind (T, E_Decimal_Fixed_Point_Subtype);
15802 Set_Etype (T, Implicit_Base);
15803 Set_Size_Info (T, Implicit_Base);
15804 Inherit_Rep_Item_Chain (T, Implicit_Base);
15805 Set_Digits_Value (T, Digs_Val);
15806 Set_Delta_Value (T, Delta_Val);
15807 Set_Small_Value (T, Delta_Val);
15808 Set_Scale_Value (T, Scale_Val);
15809 Set_Is_Constrained (T);
15810 end Decimal_Fixed_Point_Type_Declaration;
15812 -----------------------------------
15813 -- Derive_Progenitor_Subprograms --
15814 -----------------------------------
15816 procedure Derive_Progenitor_Subprograms
15817 (Parent_Type : Entity_Id;
15818 Tagged_Type : Entity_Id)
15820 E : Entity_Id;
15821 Elmt : Elmt_Id;
15822 Iface : Entity_Id;
15823 Iface_Alias : Entity_Id;
15824 Iface_Elmt : Elmt_Id;
15825 Iface_Subp : Entity_Id;
15826 New_Subp : Entity_Id := Empty;
15827 Prim_Elmt : Elmt_Id;
15828 Subp : Entity_Id;
15829 Typ : Entity_Id;
15831 begin
15832 pragma Assert (Ada_Version >= Ada_2005
15833 and then Is_Record_Type (Tagged_Type)
15834 and then Is_Tagged_Type (Tagged_Type)
15835 and then Has_Interfaces (Tagged_Type));
15837 -- Step 1: Transfer to the full-view primitives associated with the
15838 -- partial-view that cover interface primitives. Conceptually this
15839 -- work should be done later by Process_Full_View; done here to
15840 -- simplify its implementation at later stages. It can be safely
15841 -- done here because interfaces must be visible in the partial and
15842 -- private view (RM 7.3(7.3/2)).
15844 -- Small optimization: This work is only required if the parent may
15845 -- have entities whose Alias attribute reference an interface primitive.
15846 -- Such a situation may occur if the parent is an abstract type and the
15847 -- primitive has not been yet overridden or if the parent is a generic
15848 -- formal type covering interfaces.
15850 -- If the tagged type is not abstract, it cannot have abstract
15851 -- primitives (the only entities in the list of primitives of
15852 -- non-abstract tagged types that can reference abstract primitives
15853 -- through its Alias attribute are the internal entities that have
15854 -- attribute Interface_Alias, and these entities are generated later
15855 -- by Add_Internal_Interface_Entities).
15857 if In_Private_Part (Current_Scope)
15858 and then (Is_Abstract_Type (Parent_Type)
15859 or else
15860 Is_Generic_Type (Parent_Type))
15861 then
15862 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
15863 while Present (Elmt) loop
15864 Subp := Node (Elmt);
15866 -- At this stage it is not possible to have entities in the list
15867 -- of primitives that have attribute Interface_Alias.
15869 pragma Assert (No (Interface_Alias (Subp)));
15871 Typ := Find_Dispatching_Type (Ultimate_Alias (Subp));
15873 if Is_Interface (Typ) then
15874 E := Find_Primitive_Covering_Interface
15875 (Tagged_Type => Tagged_Type,
15876 Iface_Prim => Subp);
15878 if Present (E)
15879 and then Find_Dispatching_Type (Ultimate_Alias (E)) /= Typ
15880 then
15881 Replace_Elmt (Elmt, E);
15882 Remove_Homonym (Subp);
15883 end if;
15884 end if;
15886 Next_Elmt (Elmt);
15887 end loop;
15888 end if;
15890 -- Step 2: Add primitives of progenitors that are not implemented by
15891 -- parents of Tagged_Type.
15893 if Present (Interfaces (Base_Type (Tagged_Type))) then
15894 Iface_Elmt := First_Elmt (Interfaces (Base_Type (Tagged_Type)));
15895 while Present (Iface_Elmt) loop
15896 Iface := Node (Iface_Elmt);
15898 Prim_Elmt := First_Elmt (Primitive_Operations (Iface));
15899 while Present (Prim_Elmt) loop
15900 Iface_Subp := Node (Prim_Elmt);
15901 Iface_Alias := Ultimate_Alias (Iface_Subp);
15903 -- Exclude derivation of predefined primitives except those
15904 -- that come from source, or are inherited from one that comes
15905 -- from source. Required to catch declarations of equality
15906 -- operators of interfaces. For example:
15908 -- type Iface is interface;
15909 -- function "=" (Left, Right : Iface) return Boolean;
15911 if not Is_Predefined_Dispatching_Operation (Iface_Subp)
15912 or else Comes_From_Source (Iface_Alias)
15913 then
15914 E :=
15915 Find_Primitive_Covering_Interface
15916 (Tagged_Type => Tagged_Type,
15917 Iface_Prim => Iface_Subp);
15919 -- If not found we derive a new primitive leaving its alias
15920 -- attribute referencing the interface primitive.
15922 if No (E) then
15923 Derive_Subprogram
15924 (New_Subp, Iface_Subp, Tagged_Type, Iface);
15926 -- Ada 2012 (AI05-0197): If the covering primitive's name
15927 -- differs from the name of the interface primitive then it
15928 -- is a private primitive inherited from a parent type. In
15929 -- such case, given that Tagged_Type covers the interface,
15930 -- the inherited private primitive becomes visible. For such
15931 -- purpose we add a new entity that renames the inherited
15932 -- private primitive.
15934 elsif Chars (E) /= Chars (Iface_Subp) then
15935 pragma Assert (Has_Suffix (E, 'P'));
15936 Derive_Subprogram
15937 (New_Subp, Iface_Subp, Tagged_Type, Iface);
15938 Set_Alias (New_Subp, E);
15939 Set_Is_Abstract_Subprogram (New_Subp,
15940 Is_Abstract_Subprogram (E));
15942 -- Propagate to the full view interface entities associated
15943 -- with the partial view.
15945 elsif In_Private_Part (Current_Scope)
15946 and then Present (Alias (E))
15947 and then Alias (E) = Iface_Subp
15948 and then
15949 List_Containing (Parent (E)) /=
15950 Private_Declarations
15951 (Specification
15952 (Unit_Declaration_Node (Current_Scope)))
15953 then
15954 Append_Elmt (E, Primitive_Operations (Tagged_Type));
15955 end if;
15956 end if;
15958 Next_Elmt (Prim_Elmt);
15959 end loop;
15961 Next_Elmt (Iface_Elmt);
15962 end loop;
15963 end if;
15964 end Derive_Progenitor_Subprograms;
15966 -----------------------
15967 -- Derive_Subprogram --
15968 -----------------------
15970 procedure Derive_Subprogram
15971 (New_Subp : out Entity_Id;
15972 Parent_Subp : Entity_Id;
15973 Derived_Type : Entity_Id;
15974 Parent_Type : Entity_Id;
15975 Actual_Subp : Entity_Id := Empty)
15977 Formal : Entity_Id;
15978 -- Formal parameter of parent primitive operation
15980 Formal_Of_Actual : Entity_Id;
15981 -- Formal parameter of actual operation, when the derivation is to
15982 -- create a renaming for a primitive operation of an actual in an
15983 -- instantiation.
15985 New_Formal : Entity_Id;
15986 -- Formal of inherited operation
15988 Visible_Subp : Entity_Id := Parent_Subp;
15990 function Is_Private_Overriding return Boolean;
15991 -- If Subp is a private overriding of a visible operation, the inherited
15992 -- operation derives from the overridden op (even though its body is the
15993 -- overriding one) and the inherited operation is visible now. See
15994 -- sem_disp to see the full details of the handling of the overridden
15995 -- subprogram, which is removed from the list of primitive operations of
15996 -- the type. The overridden subprogram is saved locally in Visible_Subp,
15997 -- and used to diagnose abstract operations that need overriding in the
15998 -- derived type.
16000 procedure Replace_Type (Id, New_Id : Entity_Id);
16001 -- Set the Etype of New_Id to the appropriate subtype determined from
16002 -- the Etype of Id, following (RM 3.4 (18, 19, 20, 21)). Id is either
16003 -- the parent type's primitive subprogram or one of its formals, and
16004 -- New_Id is the corresponding entity for the derived type. When the
16005 -- Etype of Id is an anonymous access type, create a new access type
16006 -- designating the derived type.
16008 procedure Set_Derived_Name;
16009 -- This procedure sets the appropriate Chars name for New_Subp. This
16010 -- is normally just a copy of the parent name. An exception arises for
16011 -- type support subprograms, where the name is changed to reflect the
16012 -- name of the derived type, e.g. if type foo is derived from type bar,
16013 -- then a procedure barDA is derived with a name fooDA.
16015 ---------------------------
16016 -- Is_Private_Overriding --
16017 ---------------------------
16019 function Is_Private_Overriding return Boolean is
16020 Prev : Entity_Id;
16022 begin
16023 -- If the parent is not a dispatching operation there is no
16024 -- need to investigate overridings
16026 if not Is_Dispatching_Operation (Parent_Subp) then
16027 return False;
16028 end if;
16030 -- The visible operation that is overridden is a homonym of the
16031 -- parent subprogram. We scan the homonym chain to find the one
16032 -- whose alias is the subprogram we are deriving.
16034 Prev := Current_Entity (Parent_Subp);
16035 while Present (Prev) loop
16036 if Ekind (Prev) = Ekind (Parent_Subp)
16037 and then Alias (Prev) = Parent_Subp
16038 and then Scope (Parent_Subp) = Scope (Prev)
16039 and then not Is_Hidden (Prev)
16040 then
16041 Visible_Subp := Prev;
16042 return True;
16043 end if;
16045 Prev := Homonym (Prev);
16046 end loop;
16048 return False;
16049 end Is_Private_Overriding;
16051 ------------------
16052 -- Replace_Type --
16053 ------------------
16055 procedure Replace_Type (Id, New_Id : Entity_Id) is
16056 Id_Type : constant Entity_Id := Etype (Id);
16057 Par : constant Node_Id := Parent (Derived_Type);
16059 begin
16060 -- When the type is an anonymous access type, create a new access
16061 -- type designating the derived type. This itype must be elaborated
16062 -- at the point of the derivation, not on subsequent calls that may
16063 -- be out of the proper scope for Gigi, so we insert a reference to
16064 -- it after the derivation.
16066 if Ekind (Id_Type) = E_Anonymous_Access_Type then
16067 declare
16068 Acc_Type : Entity_Id;
16069 Desig_Typ : Entity_Id := Designated_Type (Id_Type);
16071 begin
16072 if Ekind (Desig_Typ) = E_Record_Type_With_Private
16073 and then Present (Full_View (Desig_Typ))
16074 and then not Is_Private_Type (Parent_Type)
16075 then
16076 Desig_Typ := Full_View (Desig_Typ);
16077 end if;
16079 if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
16081 -- Ada 2005 (AI-251): Handle also derivations of abstract
16082 -- interface primitives.
16084 or else (Is_Interface (Desig_Typ)
16085 and then not Is_Class_Wide_Type (Desig_Typ))
16086 then
16087 Acc_Type := New_Copy (Id_Type);
16088 Set_Etype (Acc_Type, Acc_Type);
16089 Set_Scope (Acc_Type, New_Subp);
16091 -- Set size of anonymous access type. If we have an access
16092 -- to an unconstrained array, this is a fat pointer, so it
16093 -- is sizes at twice addtress size.
16095 if Is_Array_Type (Desig_Typ)
16096 and then not Is_Constrained (Desig_Typ)
16097 then
16098 Init_Size (Acc_Type, 2 * System_Address_Size);
16100 -- Other cases use a thin pointer
16102 else
16103 Init_Size (Acc_Type, System_Address_Size);
16104 end if;
16106 -- Set remaining characterstics of anonymous access type
16108 Reinit_Alignment (Acc_Type);
16109 Set_Directly_Designated_Type (Acc_Type, Derived_Type);
16111 Set_Etype (New_Id, Acc_Type);
16112 Set_Scope (New_Id, New_Subp);
16114 -- Create a reference to it
16116 Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
16118 else
16119 Set_Etype (New_Id, Id_Type);
16120 end if;
16121 end;
16123 -- In Ada2012, a formal may have an incomplete type but the type
16124 -- derivation that inherits the primitive follows the full view.
16126 elsif Base_Type (Id_Type) = Base_Type (Parent_Type)
16127 or else
16128 (Ekind (Id_Type) = E_Record_Type_With_Private
16129 and then Present (Full_View (Id_Type))
16130 and then
16131 Base_Type (Full_View (Id_Type)) = Base_Type (Parent_Type))
16132 or else
16133 (Ada_Version >= Ada_2012
16134 and then Ekind (Id_Type) = E_Incomplete_Type
16135 and then Full_View (Id_Type) = Parent_Type)
16136 then
16137 -- Constraint checks on formals are generated during expansion,
16138 -- based on the signature of the original subprogram. The bounds
16139 -- of the derived type are not relevant, and thus we can use
16140 -- the base type for the formals. However, the return type may be
16141 -- used in a context that requires that the proper static bounds
16142 -- be used (a case statement, for example) and for those cases
16143 -- we must use the derived type (first subtype), not its base.
16145 -- If the derived_type_definition has no constraints, we know that
16146 -- the derived type has the same constraints as the first subtype
16147 -- of the parent, and we can also use it rather than its base,
16148 -- which can lead to more efficient code.
16150 if Id_Type = Parent_Type then
16151 if Is_Scalar_Type (Parent_Type)
16152 and then
16153 Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
16154 then
16155 Set_Etype (New_Id, Derived_Type);
16157 elsif Nkind (Par) = N_Full_Type_Declaration
16158 and then
16159 Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
16160 and then
16161 Is_Entity_Name
16162 (Subtype_Indication (Type_Definition (Par)))
16163 then
16164 Set_Etype (New_Id, Derived_Type);
16166 else
16167 Set_Etype (New_Id, Base_Type (Derived_Type));
16168 end if;
16170 else
16171 Set_Etype (New_Id, Base_Type (Derived_Type));
16172 end if;
16174 else
16175 Set_Etype (New_Id, Id_Type);
16176 end if;
16177 end Replace_Type;
16179 ----------------------
16180 -- Set_Derived_Name --
16181 ----------------------
16183 procedure Set_Derived_Name is
16184 Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
16185 begin
16186 if Nm = TSS_Null then
16187 Set_Chars (New_Subp, Chars (Parent_Subp));
16188 else
16189 Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
16190 end if;
16191 end Set_Derived_Name;
16193 -- Start of processing for Derive_Subprogram
16195 begin
16196 New_Subp := New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
16197 Mutate_Ekind (New_Subp, Ekind (Parent_Subp));
16198 Set_Is_Not_Self_Hidden (New_Subp);
16200 -- Check whether the inherited subprogram is a private operation that
16201 -- should be inherited but not yet made visible. Such subprograms can
16202 -- become visible at a later point (e.g., the private part of a public
16203 -- child unit) via Declare_Inherited_Private_Subprograms. If the
16204 -- following predicate is true, then this is not such a private
16205 -- operation and the subprogram simply inherits the name of the parent
16206 -- subprogram. Note the special check for the names of controlled
16207 -- operations, which are currently exempted from being inherited with
16208 -- a hidden name because they must be findable for generation of
16209 -- implicit run-time calls.
16211 if not Is_Hidden (Parent_Subp)
16212 or else Is_Internal (Parent_Subp)
16213 or else Is_Private_Overriding
16214 or else Is_Internal_Name (Chars (Parent_Subp))
16215 or else (Is_Controlled (Parent_Type)
16216 and then Chars (Parent_Subp) in Name_Adjust
16217 | Name_Finalize
16218 | Name_Initialize)
16219 then
16220 Set_Derived_Name;
16222 -- An inherited dispatching equality will be overridden by an internally
16223 -- generated one, or by an explicit one, so preserve its name and thus
16224 -- its entry in the dispatch table. Otherwise, if Parent_Subp is a
16225 -- private operation it may become invisible if the full view has
16226 -- progenitors, and the dispatch table will be malformed.
16227 -- We check that the type is limited to handle the anomalous declaration
16228 -- of Limited_Controlled, which is derived from a non-limited type, and
16229 -- which is handled specially elsewhere as well.
16231 elsif Chars (Parent_Subp) = Name_Op_Eq
16232 and then Is_Dispatching_Operation (Parent_Subp)
16233 and then Etype (Parent_Subp) = Standard_Boolean
16234 and then not Is_Limited_Type (Etype (First_Formal (Parent_Subp)))
16235 and then
16236 Etype (First_Formal (Parent_Subp)) =
16237 Etype (Next_Formal (First_Formal (Parent_Subp)))
16238 then
16239 Set_Derived_Name;
16241 -- If parent is hidden, this can be a regular derivation if the
16242 -- parent is immediately visible in a non-instantiating context,
16243 -- or if we are in the private part of an instance. This test
16244 -- should still be refined ???
16246 -- The test for In_Instance_Not_Visible avoids inheriting the derived
16247 -- operation as a non-visible operation in cases where the parent
16248 -- subprogram might not be visible now, but was visible within the
16249 -- original generic, so it would be wrong to make the inherited
16250 -- subprogram non-visible now. (Not clear if this test is fully
16251 -- correct; are there any cases where we should declare the inherited
16252 -- operation as not visible to avoid it being overridden, e.g., when
16253 -- the parent type is a generic actual with private primitives ???)
16255 -- (they should be treated the same as other private inherited
16256 -- subprograms, but it's not clear how to do this cleanly). ???
16258 elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
16259 and then Is_Immediately_Visible (Parent_Subp)
16260 and then not In_Instance)
16261 or else In_Instance_Not_Visible
16262 then
16263 Set_Derived_Name;
16265 -- Ada 2005 (AI-251): Regular derivation if the parent subprogram
16266 -- overrides an interface primitive because interface primitives
16267 -- must be visible in the partial view of the parent (RM 7.3 (7.3/2))
16269 elsif Ada_Version >= Ada_2005
16270 and then Is_Dispatching_Operation (Parent_Subp)
16271 and then Present (Covered_Interface_Op (Parent_Subp))
16272 then
16273 Set_Derived_Name;
16275 -- Otherwise, the type is inheriting a private operation, so enter it
16276 -- with a special name so it can't be overridden. See also below, where
16277 -- we check for this case, and if so avoid setting Requires_Overriding.
16279 else
16280 Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
16281 end if;
16283 Set_Parent (New_Subp, Parent (Derived_Type));
16285 if Present (Actual_Subp) then
16286 Replace_Type (Actual_Subp, New_Subp);
16287 else
16288 Replace_Type (Parent_Subp, New_Subp);
16289 end if;
16291 Conditional_Delay (New_Subp, Parent_Subp);
16293 -- If we are creating a renaming for a primitive operation of an
16294 -- actual of a generic derived type, we must examine the signature
16295 -- of the actual primitive, not that of the generic formal, which for
16296 -- example may be an interface. However the name and initial value
16297 -- of the inherited operation are those of the formal primitive.
16299 Formal := First_Formal (Parent_Subp);
16301 if Present (Actual_Subp) then
16302 Formal_Of_Actual := First_Formal (Actual_Subp);
16303 else
16304 Formal_Of_Actual := Empty;
16305 end if;
16307 while Present (Formal) loop
16308 New_Formal := New_Copy (Formal);
16310 -- Extra formals are not inherited from a limited interface parent
16311 -- since limitedness is not inherited in such case (AI-419) and this
16312 -- affects the extra formals.
16314 if Is_Limited_Interface (Parent_Type) then
16315 Set_Extra_Formal (New_Formal, Empty);
16316 Set_Extra_Accessibility (New_Formal, Empty);
16317 end if;
16319 -- Normally we do not go copying parents, but in the case of
16320 -- formals, we need to link up to the declaration (which is the
16321 -- parameter specification), and it is fine to link up to the
16322 -- original formal's parameter specification in this case.
16324 Set_Parent (New_Formal, Parent (Formal));
16325 Append_Entity (New_Formal, New_Subp);
16327 if Present (Formal_Of_Actual) then
16328 Replace_Type (Formal_Of_Actual, New_Formal);
16329 Next_Formal (Formal_Of_Actual);
16330 else
16331 Replace_Type (Formal, New_Formal);
16332 end if;
16334 Next_Formal (Formal);
16335 end loop;
16337 -- Extra formals are shared between the parent subprogram and this
16338 -- internal entity built by Derive_Subprogram (implicit in the above
16339 -- copy of formals), unless the parent type is a limited interface type;
16340 -- hence we must inherit also the reference to the first extra formal.
16341 -- When the parent type is an interface, the extra formals will be added
16342 -- when the tagged type is frozen (see Expand_Freeze_Record_Type).
16344 if not Is_Limited_Interface (Parent_Type) then
16345 Set_Extra_Formals (New_Subp, Extra_Formals (Parent_Subp));
16347 if Ekind (New_Subp) = E_Function then
16348 Set_Extra_Accessibility_Of_Result (New_Subp,
16349 Extra_Accessibility_Of_Result (Parent_Subp));
16350 end if;
16351 end if;
16353 -- If this derivation corresponds to a tagged generic actual, then
16354 -- primitive operations rename those of the actual. Otherwise the
16355 -- primitive operations rename those of the parent type, If the parent
16356 -- renames an intrinsic operator, so does the new subprogram. We except
16357 -- concatenation, which is always properly typed, and does not get
16358 -- expanded as other intrinsic operations.
16360 if No (Actual_Subp) then
16361 if Is_Intrinsic_Subprogram (Parent_Subp) then
16362 Set_Convention (New_Subp, Convention_Intrinsic);
16363 Set_Is_Intrinsic_Subprogram (New_Subp);
16365 if Present (Alias (Parent_Subp))
16366 and then Chars (Parent_Subp) /= Name_Op_Concat
16367 then
16368 Set_Alias (New_Subp, Alias (Parent_Subp));
16369 else
16370 Set_Alias (New_Subp, Parent_Subp);
16371 end if;
16373 else
16374 Set_Alias (New_Subp, Parent_Subp);
16375 end if;
16377 else
16378 Set_Alias (New_Subp, Actual_Subp);
16379 end if;
16381 Copy_Strub_Mode (New_Subp, Alias (New_Subp));
16383 -- Derived subprograms of a tagged type must inherit the convention
16384 -- of the parent subprogram (a requirement of AI95-117). Derived
16385 -- subprograms of untagged types simply get convention Ada by default.
16387 -- If the derived type is a tagged generic formal type with unknown
16388 -- discriminants, its convention is intrinsic (RM 6.3.1 (8)).
16390 -- However, if the type is derived from a generic formal, the further
16391 -- inherited subprogram has the convention of the non-generic ancestor.
16392 -- Otherwise there would be no way to override the operation.
16393 -- (This is subject to forthcoming ARG discussions).
16395 if Is_Tagged_Type (Derived_Type) then
16396 if Is_Generic_Type (Derived_Type)
16397 and then Has_Unknown_Discriminants (Derived_Type)
16398 then
16399 Set_Convention (New_Subp, Convention_Intrinsic);
16401 else
16402 if Is_Generic_Type (Parent_Type)
16403 and then Has_Unknown_Discriminants (Parent_Type)
16404 then
16405 Set_Convention (New_Subp, Convention (Alias (Parent_Subp)));
16406 else
16407 Set_Convention (New_Subp, Convention (Parent_Subp));
16408 end if;
16409 end if;
16410 end if;
16412 -- Predefined controlled operations retain their name even if the parent
16413 -- is hidden (see above), but they are not primitive operations if the
16414 -- ancestor is not visible, for example if the parent is a private
16415 -- extension completed with a controlled extension. Note that a full
16416 -- type that is controlled can break privacy: the flag Is_Controlled is
16417 -- set on both views of the type.
16419 if Is_Controlled (Parent_Type)
16420 and then Chars (Parent_Subp) in Name_Initialize
16421 | Name_Adjust
16422 | Name_Finalize
16423 and then Is_Hidden (Parent_Subp)
16424 and then not Is_Visibly_Controlled (Parent_Type)
16425 then
16426 Set_Is_Hidden (New_Subp);
16427 end if;
16429 Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
16430 Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
16432 if Ekind (Parent_Subp) = E_Procedure then
16433 Set_Is_Valued_Procedure
16434 (New_Subp, Is_Valued_Procedure (Parent_Subp));
16435 else
16436 Set_Has_Controlling_Result
16437 (New_Subp, Has_Controlling_Result (Parent_Subp));
16438 end if;
16440 -- No_Return must be inherited properly. If this is overridden in the
16441 -- case of a dispatching operation, then the check is made later in
16442 -- Check_Abstract_Overriding that the overriding operation is also
16443 -- No_Return (no such check is required for the nondispatching case).
16445 Set_No_Return (New_Subp, No_Return (Parent_Subp));
16447 -- If the parent subprogram is marked as Ghost, then so is the derived
16448 -- subprogram. The ghost policy for the derived subprogram is set from
16449 -- the effective ghost policy at the point of derived type declaration.
16451 if Is_Ghost_Entity (Parent_Subp) then
16452 Set_Is_Ghost_Entity (New_Subp);
16453 end if;
16455 -- A derived function with a controlling result is abstract. If the
16456 -- Derived_Type is a nonabstract formal generic derived type, then
16457 -- inherited operations are not abstract: the required check is done at
16458 -- instantiation time. If the derivation is for a generic actual, the
16459 -- function is not abstract unless the actual is.
16461 if Is_Generic_Type (Derived_Type)
16462 and then not Is_Abstract_Type (Derived_Type)
16463 then
16464 null;
16466 -- Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
16467 -- properties of the subprogram, as defined in RM-3.9.3(4/2-6/2). Note
16468 -- that functions with controlling access results of record extensions
16469 -- with a null extension part require overriding (AI95-00391/06).
16471 -- Ada 2022 (AI12-0042): Similarly, set those properties for
16472 -- implementing the rule of RM 7.3.2(6.1/4).
16474 -- A subprogram subject to pragma Extensions_Visible with value False
16475 -- requires overriding if the subprogram has at least one controlling
16476 -- OUT parameter (SPARK RM 6.1.7(6)).
16478 elsif Ada_Version >= Ada_2005
16479 and then (Is_Abstract_Subprogram (Alias (New_Subp))
16480 or else (Is_Tagged_Type (Derived_Type)
16481 and then Etype (New_Subp) = Derived_Type
16482 and then not Is_Null_Extension (Derived_Type))
16483 or else (Is_Tagged_Type (Derived_Type)
16484 and then Ekind (Etype (New_Subp)) =
16485 E_Anonymous_Access_Type
16486 and then Designated_Type (Etype (New_Subp)) =
16487 Derived_Type)
16488 or else (Comes_From_Source (Alias (New_Subp))
16489 and then Is_EVF_Procedure (Alias (New_Subp)))
16491 -- AI12-0042: Set Requires_Overriding when a type extension
16492 -- inherits a private operation that is visible at the
16493 -- point of extension (Has_Private_Ancestor is False) from
16494 -- an ancestor that has Type_Invariant'Class, and when the
16495 -- type extension is in a visible part (the latter as
16496 -- clarified by AI12-0382).
16498 or else
16499 (not Has_Private_Ancestor (Derived_Type)
16500 and then Has_Invariants (Parent_Type)
16501 and then
16502 Present (Get_Pragma (Parent_Type, Pragma_Invariant))
16503 and then
16504 Class_Present
16505 (Get_Pragma (Parent_Type, Pragma_Invariant))
16506 and then Is_Private_Primitive (Parent_Subp)
16507 and then In_Visible_Part (Scope (Derived_Type))))
16509 and then No (Actual_Subp)
16510 then
16511 if not Is_Tagged_Type (Derived_Type)
16512 or else Is_Abstract_Type (Derived_Type)
16513 or else Is_Abstract_Subprogram (Alias (New_Subp))
16514 then
16515 Set_Is_Abstract_Subprogram (New_Subp);
16517 -- If the Chars of the new subprogram is different from that of the
16518 -- parent's one, it means that we entered it with a special name so
16519 -- it can't be overridden (see above). In that case we had better not
16520 -- *require* it to be overridden. This is the case where the parent
16521 -- type inherited the operation privately, so there's no danger of
16522 -- dangling dispatching.
16524 elsif Chars (New_Subp) = Chars (Alias (New_Subp)) then
16525 Set_Requires_Overriding (New_Subp);
16526 end if;
16528 elsif Ada_Version < Ada_2005
16529 and then (Is_Abstract_Subprogram (Alias (New_Subp))
16530 or else (Is_Tagged_Type (Derived_Type)
16531 and then Etype (New_Subp) = Derived_Type
16532 and then No (Actual_Subp)))
16533 then
16534 Set_Is_Abstract_Subprogram (New_Subp);
16536 -- AI05-0097 : an inherited operation that dispatches on result is
16537 -- abstract if the derived type is abstract, even if the parent type
16538 -- is concrete and the derived type is a null extension.
16540 elsif Has_Controlling_Result (Alias (New_Subp))
16541 and then Is_Abstract_Type (Etype (New_Subp))
16542 then
16543 Set_Is_Abstract_Subprogram (New_Subp);
16545 -- Finally, if the parent type is abstract we must verify that all
16546 -- inherited operations are either non-abstract or overridden, or that
16547 -- the derived type itself is abstract (this check is performed at the
16548 -- end of a package declaration, in Check_Abstract_Overriding). A
16549 -- private overriding in the parent type will not be visible in the
16550 -- derivation if we are not in an inner package or in a child unit of
16551 -- the parent type, in which case the abstractness of the inherited
16552 -- operation is carried to the new subprogram.
16554 elsif Is_Abstract_Type (Parent_Type)
16555 and then not In_Open_Scopes (Scope (Parent_Type))
16556 and then Is_Private_Overriding
16557 and then Is_Abstract_Subprogram (Visible_Subp)
16558 then
16559 if No (Actual_Subp) then
16560 Set_Alias (New_Subp, Visible_Subp);
16561 Set_Is_Abstract_Subprogram (New_Subp, True);
16563 else
16564 -- If this is a derivation for an instance of a formal derived
16565 -- type, abstractness comes from the primitive operation of the
16566 -- actual, not from the operation inherited from the ancestor.
16568 Set_Is_Abstract_Subprogram
16569 (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
16570 end if;
16571 end if;
16573 New_Overloaded_Entity (New_Subp, Derived_Type);
16575 -- RM 6.1.1(15): If a subprogram inherits nonconforming class-wide
16576 -- preconditions and the derived type is abstract, the derived operation
16577 -- is abstract as well if parent subprogram is not abstract or null.
16579 if Is_Abstract_Type (Derived_Type)
16580 and then Has_Non_Trivial_Precondition (Parent_Subp)
16581 and then Present (Interfaces (Derived_Type))
16582 then
16584 -- Add useful attributes of subprogram before the freeze point,
16585 -- in case freezing is delayed or there are previous errors.
16587 Set_Is_Dispatching_Operation (New_Subp);
16589 declare
16590 Iface_Prim : constant Entity_Id := Covered_Interface_Op (New_Subp);
16592 begin
16593 if Present (Iface_Prim)
16594 and then Has_Non_Trivial_Precondition (Iface_Prim)
16595 then
16596 Set_Is_Abstract_Subprogram (New_Subp);
16597 end if;
16598 end;
16599 end if;
16601 -- Check for case of a derived subprogram for the instantiation of a
16602 -- formal derived tagged type, if so mark the subprogram as dispatching
16603 -- and inherit the dispatching attributes of the actual subprogram. The
16604 -- derived subprogram is effectively renaming of the actual subprogram,
16605 -- so it needs to have the same attributes as the actual.
16607 if Present (Actual_Subp)
16608 and then Is_Dispatching_Operation (Actual_Subp)
16609 then
16610 Set_Is_Dispatching_Operation (New_Subp);
16612 if Present (DTC_Entity (Actual_Subp)) then
16613 Set_DTC_Entity (New_Subp, DTC_Entity (Actual_Subp));
16614 Set_DT_Position_Value (New_Subp, DT_Position (Actual_Subp));
16615 end if;
16616 end if;
16618 -- Indicate that a derived subprogram does not require a body and that
16619 -- it does not require processing of default expressions.
16621 Set_Has_Completion (New_Subp);
16622 Set_Default_Expressions_Processed (New_Subp);
16624 if Ekind (New_Subp) = E_Function then
16625 Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
16626 Set_Returns_By_Ref (New_Subp, Returns_By_Ref (Parent_Subp));
16627 end if;
16629 -- Ada 2022 (AI12-0279): If a Yield aspect is specified True for a
16630 -- primitive subprogram S of a type T, then the aspect is inherited
16631 -- by the corresponding primitive subprogram of each descendant of T.
16633 if Is_Tagged_Type (Derived_Type)
16634 and then Is_Dispatching_Operation (New_Subp)
16635 and then Has_Yield_Aspect (Alias (New_Subp))
16636 then
16637 Set_Has_Yield_Aspect (New_Subp, Has_Yield_Aspect (Alias (New_Subp)));
16638 end if;
16640 Set_Is_Ada_2022_Only (New_Subp, Is_Ada_2022_Only (Parent_Subp));
16641 end Derive_Subprogram;
16643 ------------------------
16644 -- Derive_Subprograms --
16645 ------------------------
16647 procedure Derive_Subprograms
16648 (Parent_Type : Entity_Id;
16649 Derived_Type : Entity_Id;
16650 Generic_Actual : Entity_Id := Empty)
16652 Op_List : constant Elist_Id :=
16653 Collect_Primitive_Operations (Parent_Type);
16655 function Check_Derived_Type return Boolean;
16656 -- Check that all the entities derived from Parent_Type are found in
16657 -- the list of primitives of Derived_Type exactly in the same order.
16659 procedure Derive_Interface_Subprogram
16660 (New_Subp : out Entity_Id;
16661 Subp : Entity_Id;
16662 Actual_Subp : Entity_Id);
16663 -- Derive New_Subp from the ultimate alias of the parent subprogram Subp
16664 -- (which is an interface primitive). If Generic_Actual is present then
16665 -- Actual_Subp is the actual subprogram corresponding with the generic
16666 -- subprogram Subp.
16668 ------------------------
16669 -- Check_Derived_Type --
16670 ------------------------
16672 function Check_Derived_Type return Boolean is
16673 E : Entity_Id;
16674 Derived_Elmt : Elmt_Id;
16675 Derived_Op : Entity_Id;
16676 Derived_Ops : Elist_Id;
16677 Parent_Elmt : Elmt_Id;
16678 Parent_Op : Entity_Id;
16680 begin
16681 -- Traverse list of entities in the current scope searching for
16682 -- an incomplete type whose full-view is derived type.
16684 E := First_Entity (Scope (Derived_Type));
16685 while Present (E) and then E /= Derived_Type loop
16686 if Ekind (E) = E_Incomplete_Type
16687 and then Present (Full_View (E))
16688 and then Full_View (E) = Derived_Type
16689 then
16690 -- Disable this test if Derived_Type completes an incomplete
16691 -- type because in such case more primitives can be added
16692 -- later to the list of primitives of Derived_Type by routine
16693 -- Process_Incomplete_Dependents.
16695 return True;
16696 end if;
16698 Next_Entity (E);
16699 end loop;
16701 Derived_Ops := Collect_Primitive_Operations (Derived_Type);
16703 Derived_Elmt := First_Elmt (Derived_Ops);
16704 Parent_Elmt := First_Elmt (Op_List);
16705 while Present (Parent_Elmt) loop
16706 Parent_Op := Node (Parent_Elmt);
16707 Derived_Op := Node (Derived_Elmt);
16709 -- At this early stage Derived_Type has no entities with attribute
16710 -- Interface_Alias. In addition, such primitives are always
16711 -- located at the end of the list of primitives of Parent_Type.
16712 -- Therefore, if found we can safely stop processing pending
16713 -- entities.
16715 exit when Present (Interface_Alias (Parent_Op));
16717 -- Handle hidden entities
16719 if not Is_Predefined_Dispatching_Operation (Parent_Op)
16720 and then Is_Hidden (Parent_Op)
16721 then
16722 if Present (Derived_Op)
16723 and then Primitive_Names_Match (Parent_Op, Derived_Op)
16724 then
16725 Next_Elmt (Derived_Elmt);
16726 end if;
16728 else
16729 if No (Derived_Op)
16730 or else Ekind (Parent_Op) /= Ekind (Derived_Op)
16731 or else not Primitive_Names_Match (Parent_Op, Derived_Op)
16732 then
16733 return False;
16734 end if;
16736 Next_Elmt (Derived_Elmt);
16737 end if;
16739 Next_Elmt (Parent_Elmt);
16740 end loop;
16742 return True;
16743 end Check_Derived_Type;
16745 ---------------------------------
16746 -- Derive_Interface_Subprogram --
16747 ---------------------------------
16749 procedure Derive_Interface_Subprogram
16750 (New_Subp : out Entity_Id;
16751 Subp : Entity_Id;
16752 Actual_Subp : Entity_Id)
16754 Iface_Subp : constant Entity_Id := Ultimate_Alias (Subp);
16755 Iface_Type : constant Entity_Id := Find_Dispatching_Type (Iface_Subp);
16757 begin
16758 pragma Assert (Is_Interface (Iface_Type));
16760 Derive_Subprogram
16761 (New_Subp => New_Subp,
16762 Parent_Subp => Iface_Subp,
16763 Derived_Type => Derived_Type,
16764 Parent_Type => Iface_Type,
16765 Actual_Subp => Actual_Subp);
16767 -- Given that this new interface entity corresponds with a primitive
16768 -- of the parent that was not overridden we must leave it associated
16769 -- with its parent primitive to ensure that it will share the same
16770 -- dispatch table slot when overridden. We must set the Alias to Subp
16771 -- (instead of Iface_Subp), and we must fix Is_Abstract_Subprogram
16772 -- (in case we inherited Subp from Iface_Type via a nonabstract
16773 -- generic formal type).
16775 if No (Actual_Subp) then
16776 Set_Alias (New_Subp, Subp);
16778 declare
16779 T : Entity_Id := Find_Dispatching_Type (Subp);
16780 begin
16781 while Etype (T) /= T loop
16782 if Is_Generic_Type (T) and then not Is_Abstract_Type (T) then
16783 Set_Is_Abstract_Subprogram (New_Subp, False);
16784 exit;
16785 end if;
16787 T := Etype (T);
16788 end loop;
16789 end;
16791 -- For instantiations this is not needed since the previous call to
16792 -- Derive_Subprogram leaves the entity well decorated.
16794 else
16795 pragma Assert (Alias (New_Subp) = Actual_Subp);
16796 null;
16797 end if;
16798 end Derive_Interface_Subprogram;
16800 -- Local variables
16802 Alias_Subp : Entity_Id;
16803 Act_List : Elist_Id;
16804 Act_Elmt : Elmt_Id;
16805 Act_Subp : Entity_Id := Empty;
16806 Elmt : Elmt_Id;
16807 Need_Search : Boolean := False;
16808 New_Subp : Entity_Id;
16809 Parent_Base : Entity_Id;
16810 Subp : Entity_Id;
16812 -- Start of processing for Derive_Subprograms
16814 begin
16815 if Ekind (Parent_Type) = E_Record_Type_With_Private
16816 and then Has_Discriminants (Parent_Type)
16817 and then Present (Full_View (Parent_Type))
16818 then
16819 Parent_Base := Full_View (Parent_Type);
16820 else
16821 Parent_Base := Parent_Type;
16822 end if;
16824 if Present (Generic_Actual) then
16825 Act_List := Collect_Primitive_Operations (Generic_Actual);
16826 Act_Elmt := First_Elmt (Act_List);
16827 else
16828 Act_List := No_Elist;
16829 Act_Elmt := No_Elmt;
16830 end if;
16832 -- Derive primitives inherited from the parent. Note that if the generic
16833 -- actual is present, this is not really a type derivation, it is a
16834 -- completion within an instance.
16836 -- Case 1: Derived_Type does not implement interfaces
16838 if not Is_Tagged_Type (Derived_Type)
16839 or else (not Has_Interfaces (Derived_Type)
16840 and then not (Present (Generic_Actual)
16841 and then Has_Interfaces (Generic_Actual)))
16842 then
16843 Elmt := First_Elmt (Op_List);
16844 while Present (Elmt) loop
16845 Subp := Node (Elmt);
16847 -- Literals are derived earlier in the process of building the
16848 -- derived type, and are skipped here.
16850 if Ekind (Subp) = E_Enumeration_Literal then
16851 null;
16853 -- The actual is a direct descendant and the common primitive
16854 -- operations appear in the same order.
16856 -- If the generic parent type is present, the derived type is an
16857 -- instance of a formal derived type, and within the instance its
16858 -- operations are those of the actual. We derive from the formal
16859 -- type but make the inherited operations aliases of the
16860 -- corresponding operations of the actual.
16862 else
16863 pragma Assert (No (Node (Act_Elmt))
16864 or else (Primitive_Names_Match (Subp, Node (Act_Elmt))
16865 and then
16866 Type_Conformant
16867 (Subp, Node (Act_Elmt),
16868 Skip_Controlling_Formals => True)));
16870 Derive_Subprogram
16871 (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
16873 if Present (Act_Elmt) then
16874 Next_Elmt (Act_Elmt);
16875 end if;
16876 end if;
16878 Next_Elmt (Elmt);
16879 end loop;
16881 -- Case 2: Derived_Type implements interfaces
16883 else
16884 -- If the parent type has no predefined primitives we remove
16885 -- predefined primitives from the list of primitives of generic
16886 -- actual to simplify the complexity of this algorithm.
16888 if Present (Generic_Actual) then
16889 declare
16890 Has_Predefined_Primitives : Boolean := False;
16892 begin
16893 -- Check if the parent type has predefined primitives
16895 Elmt := First_Elmt (Op_List);
16896 while Present (Elmt) loop
16897 Subp := Node (Elmt);
16899 if Is_Predefined_Dispatching_Operation (Subp)
16900 and then not Comes_From_Source (Ultimate_Alias (Subp))
16901 then
16902 Has_Predefined_Primitives := True;
16903 exit;
16904 end if;
16906 Next_Elmt (Elmt);
16907 end loop;
16909 -- Remove predefined primitives of Generic_Actual. We must use
16910 -- an auxiliary list because in case of tagged types the value
16911 -- returned by Collect_Primitive_Operations is the value stored
16912 -- in its Primitive_Operations attribute (and we don't want to
16913 -- modify its current contents).
16915 if not Has_Predefined_Primitives then
16916 declare
16917 Aux_List : constant Elist_Id := New_Elmt_List;
16919 begin
16920 Elmt := First_Elmt (Act_List);
16921 while Present (Elmt) loop
16922 Subp := Node (Elmt);
16924 if not Is_Predefined_Dispatching_Operation (Subp)
16925 or else Comes_From_Source (Subp)
16926 then
16927 Append_Elmt (Subp, Aux_List);
16928 end if;
16930 Next_Elmt (Elmt);
16931 end loop;
16933 Act_List := Aux_List;
16934 end;
16935 end if;
16937 Act_Elmt := First_Elmt (Act_List);
16938 Act_Subp := Node (Act_Elmt);
16939 end;
16940 end if;
16942 -- Stage 1: If the generic actual is not present we derive the
16943 -- primitives inherited from the parent type. If the generic parent
16944 -- type is present, the derived type is an instance of a formal
16945 -- derived type, and within the instance its operations are those of
16946 -- the actual. We derive from the formal type but make the inherited
16947 -- operations aliases of the corresponding operations of the actual.
16949 Elmt := First_Elmt (Op_List);
16950 while Present (Elmt) loop
16951 Subp := Node (Elmt);
16952 Alias_Subp := Ultimate_Alias (Subp);
16954 -- Do not derive internal entities of the parent that link
16955 -- interface primitives with their covering primitive. These
16956 -- entities will be added to this type when frozen.
16958 if Present (Interface_Alias (Subp)) then
16959 goto Continue;
16960 end if;
16962 -- If the generic actual is present find the corresponding
16963 -- operation in the generic actual. If the parent type is a
16964 -- direct ancestor of the derived type then, even if it is an
16965 -- interface, the operations are inherited from the primary
16966 -- dispatch table and are in the proper order. If we detect here
16967 -- that primitives are not in the same order we traverse the list
16968 -- of primitive operations of the actual to find the one that
16969 -- implements the interface primitive.
16971 if Need_Search
16972 or else
16973 (Present (Generic_Actual)
16974 and then Present (Act_Subp)
16975 and then not
16976 (Primitive_Names_Match (Subp, Act_Subp)
16977 and then
16978 Type_Conformant (Subp, Act_Subp,
16979 Skip_Controlling_Formals => True)))
16980 then
16981 pragma Assert (not Is_Ancestor (Parent_Base, Generic_Actual,
16982 Use_Full_View => True));
16984 -- Remember that we need searching for all pending primitives
16986 Need_Search := True;
16988 -- Handle entities associated with interface primitives
16990 if Present (Alias_Subp)
16991 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
16992 and then not Is_Predefined_Dispatching_Operation (Subp)
16993 then
16994 -- Search for the primitive in the homonym chain
16996 Act_Subp :=
16997 Find_Primitive_Covering_Interface
16998 (Tagged_Type => Generic_Actual,
16999 Iface_Prim => Alias_Subp);
17001 -- Previous search may not locate primitives covering
17002 -- interfaces defined in generics units or instantiations.
17003 -- (it fails if the covering primitive has formals whose
17004 -- type is also defined in generics or instantiations).
17005 -- In such case we search in the list of primitives of the
17006 -- generic actual for the internal entity that links the
17007 -- interface primitive and the covering primitive.
17009 if No (Act_Subp)
17010 and then Is_Generic_Type (Parent_Type)
17011 then
17012 -- This code has been designed to handle only generic
17013 -- formals that implement interfaces that are defined
17014 -- in a generic unit or instantiation. If this code is
17015 -- needed for other cases we must review it because
17016 -- (given that it relies on Original_Location to locate
17017 -- the primitive of Generic_Actual that covers the
17018 -- interface) it could leave linked through attribute
17019 -- Alias entities of unrelated instantiations).
17021 pragma Assert
17022 (Is_Generic_Unit
17023 (Scope (Find_Dispatching_Type (Alias_Subp)))
17024 or else
17025 Instantiation_Location
17026 (Sloc (Find_Dispatching_Type (Alias_Subp)))
17027 /= No_Location);
17028 declare
17029 Iface_Prim_Loc : constant Source_Ptr :=
17030 Original_Location (Sloc (Alias_Subp));
17032 Elmt : Elmt_Id;
17033 Prim : Entity_Id;
17035 begin
17036 Elmt :=
17037 First_Elmt (Primitive_Operations (Generic_Actual));
17039 Search : while Present (Elmt) loop
17040 Prim := Node (Elmt);
17042 if Present (Interface_Alias (Prim))
17043 and then Original_Location
17044 (Sloc (Interface_Alias (Prim))) =
17045 Iface_Prim_Loc
17046 then
17047 Act_Subp := Alias (Prim);
17048 exit Search;
17049 end if;
17051 Next_Elmt (Elmt);
17052 end loop Search;
17053 end;
17054 end if;
17056 pragma Assert (Present (Act_Subp)
17057 or else Is_Abstract_Type (Generic_Actual)
17058 or else Serious_Errors_Detected > 0);
17060 -- Handle predefined primitives plus the rest of user-defined
17061 -- primitives
17063 else
17064 Act_Elmt := First_Elmt (Act_List);
17065 while Present (Act_Elmt) loop
17066 Act_Subp := Node (Act_Elmt);
17068 exit when Primitive_Names_Match (Subp, Act_Subp)
17069 and then Type_Conformant
17070 (Subp, Act_Subp,
17071 Skip_Controlling_Formals => True)
17072 and then No (Interface_Alias (Act_Subp));
17074 Next_Elmt (Act_Elmt);
17075 end loop;
17077 if No (Act_Elmt) then
17078 Act_Subp := Empty;
17079 end if;
17080 end if;
17081 end if;
17083 -- Case 1: If the parent is a limited interface then it has the
17084 -- predefined primitives of synchronized interfaces. However, the
17085 -- actual type may be a non-limited type and hence it does not
17086 -- have such primitives.
17088 if Present (Generic_Actual)
17089 and then No (Act_Subp)
17090 and then Is_Limited_Interface (Parent_Base)
17091 and then Is_Predefined_Interface_Primitive (Subp)
17092 then
17093 null;
17095 -- Case 2: Inherit entities associated with interfaces that were
17096 -- not covered by the parent type. We exclude here null interface
17097 -- primitives because they do not need special management.
17099 -- We also exclude interface operations that are renamings. If the
17100 -- subprogram is an explicit renaming of an interface primitive,
17101 -- it is a regular primitive operation, and the presence of its
17102 -- alias is not relevant: it has to be derived like any other
17103 -- primitive.
17105 elsif Present (Alias (Subp))
17106 and then Nkind (Unit_Declaration_Node (Subp)) /=
17107 N_Subprogram_Renaming_Declaration
17108 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
17109 and then not
17110 (Nkind (Parent (Alias_Subp)) = N_Procedure_Specification
17111 and then Null_Present (Parent (Alias_Subp)))
17112 then
17113 -- If this is an abstract private type then we transfer the
17114 -- derivation of the interface primitive from the partial view
17115 -- to the full view. This is safe because all the interfaces
17116 -- must be visible in the partial view. Done to avoid adding
17117 -- a new interface derivation to the private part of the
17118 -- enclosing package; otherwise this new derivation would be
17119 -- decorated as hidden when the analysis of the enclosing
17120 -- package completes.
17122 if Is_Abstract_Type (Derived_Type)
17123 and then In_Private_Part (Current_Scope)
17124 and then Has_Private_Declaration (Derived_Type)
17125 then
17126 declare
17127 Partial_View : Entity_Id;
17128 Elmt : Elmt_Id;
17129 Ent : Entity_Id;
17131 begin
17132 Partial_View := First_Entity (Current_Scope);
17133 loop
17134 exit when No (Partial_View)
17135 or else (Has_Private_Declaration (Partial_View)
17136 and then
17137 Full_View (Partial_View) = Derived_Type);
17139 Next_Entity (Partial_View);
17140 end loop;
17142 -- If the partial view was not found then the source code
17143 -- has errors and the derivation is not needed.
17145 if Present (Partial_View) then
17146 Elmt :=
17147 First_Elmt (Primitive_Operations (Partial_View));
17148 while Present (Elmt) loop
17149 Ent := Node (Elmt);
17151 if Present (Alias (Ent))
17152 and then Ultimate_Alias (Ent) = Alias (Subp)
17153 then
17154 Append_Elmt
17155 (Ent, Primitive_Operations (Derived_Type));
17156 exit;
17157 end if;
17159 Next_Elmt (Elmt);
17160 end loop;
17162 -- If the interface primitive was not found in the
17163 -- partial view then this interface primitive was
17164 -- overridden. We add a derivation to activate in
17165 -- Derive_Progenitor_Subprograms the machinery to
17166 -- search for it.
17168 if No (Elmt) then
17169 Derive_Interface_Subprogram
17170 (New_Subp => New_Subp,
17171 Subp => Subp,
17172 Actual_Subp => Act_Subp);
17173 end if;
17174 end if;
17175 end;
17176 else
17177 Derive_Interface_Subprogram
17178 (New_Subp => New_Subp,
17179 Subp => Subp,
17180 Actual_Subp => Act_Subp);
17181 end if;
17183 -- Case 3: Common derivation
17185 else
17186 Derive_Subprogram
17187 (New_Subp => New_Subp,
17188 Parent_Subp => Subp,
17189 Derived_Type => Derived_Type,
17190 Parent_Type => Parent_Base,
17191 Actual_Subp => Act_Subp);
17192 end if;
17194 -- No need to update Act_Elm if we must search for the
17195 -- corresponding operation in the generic actual
17197 if not Need_Search
17198 and then Present (Act_Elmt)
17199 then
17200 Next_Elmt (Act_Elmt);
17201 Act_Subp := Node (Act_Elmt);
17202 end if;
17204 <<Continue>>
17205 Next_Elmt (Elmt);
17206 end loop;
17208 -- Inherit additional operations from progenitors. If the derived
17209 -- type is a generic actual, there are not new primitive operations
17210 -- for the type because it has those of the actual, and therefore
17211 -- nothing needs to be done. The renamings generated above are not
17212 -- primitive operations, and their purpose is simply to make the
17213 -- proper operations visible within an instantiation.
17215 if No (Generic_Actual) then
17216 Derive_Progenitor_Subprograms (Parent_Base, Derived_Type);
17217 end if;
17218 end if;
17220 -- Final check: Direct descendants must have their primitives in the
17221 -- same order. We exclude from this test untagged types and instances
17222 -- of formal derived types. We skip this test if we have already
17223 -- reported serious errors in the sources.
17225 pragma Assert (not Is_Tagged_Type (Derived_Type)
17226 or else Present (Generic_Actual)
17227 or else Serious_Errors_Detected > 0
17228 or else Check_Derived_Type);
17229 end Derive_Subprograms;
17231 --------------------------------
17232 -- Derived_Standard_Character --
17233 --------------------------------
17235 procedure Derived_Standard_Character
17236 (N : Node_Id;
17237 Parent_Type : Entity_Id;
17238 Derived_Type : Entity_Id)
17240 Loc : constant Source_Ptr := Sloc (N);
17241 Def : constant Node_Id := Type_Definition (N);
17242 Indic : constant Node_Id := Subtype_Indication (Def);
17243 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
17244 Implicit_Base : constant Entity_Id :=
17245 Create_Itype
17246 (E_Enumeration_Type, N, Derived_Type, 'B');
17248 Lo : Node_Id;
17249 Hi : Node_Id;
17251 begin
17252 Discard_Node (Process_Subtype (Indic, N));
17254 Set_Etype (Implicit_Base, Parent_Base);
17255 Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
17256 Set_RM_Size (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
17258 Set_Is_Character_Type (Implicit_Base, True);
17259 Set_Has_Delayed_Freeze (Implicit_Base);
17261 -- The bounds of the implicit base are the bounds of the parent base.
17262 -- Note that their type is the parent base.
17264 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
17265 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
17267 Set_Scalar_Range (Implicit_Base,
17268 Make_Range (Loc,
17269 Low_Bound => Lo,
17270 High_Bound => Hi));
17272 Mutate_Ekind (Derived_Type, E_Enumeration_Subtype);
17273 Set_Etype (Derived_Type, Implicit_Base);
17274 Set_Size_Info (Derived_Type, Parent_Type);
17276 if not Known_RM_Size (Derived_Type) then
17277 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
17278 end if;
17280 Set_Is_Character_Type (Derived_Type, True);
17282 if Nkind (Indic) /= N_Subtype_Indication then
17284 -- If no explicit constraint, the bounds are those
17285 -- of the parent type.
17287 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Type));
17288 Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
17289 Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
17290 end if;
17292 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
17293 end Derived_Standard_Character;
17295 ------------------------------
17296 -- Derived_Type_Declaration --
17297 ------------------------------
17299 procedure Derived_Type_Declaration
17300 (T : Entity_Id;
17301 N : Node_Id;
17302 Is_Completion : Boolean)
17304 Parent_Type : Entity_Id;
17306 function Comes_From_Generic (Typ : Entity_Id) return Boolean;
17307 -- Check whether the parent type is a generic formal, or derives
17308 -- directly or indirectly from one.
17310 ------------------------
17311 -- Comes_From_Generic --
17312 ------------------------
17314 function Comes_From_Generic (Typ : Entity_Id) return Boolean is
17315 begin
17316 if Is_Generic_Type (Typ) then
17317 return True;
17319 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
17320 return True;
17322 elsif Is_Private_Type (Typ)
17323 and then Present (Full_View (Typ))
17324 and then Is_Generic_Type (Root_Type (Full_View (Typ)))
17325 then
17326 return True;
17328 elsif Is_Generic_Actual_Type (Typ) then
17329 return True;
17331 else
17332 return False;
17333 end if;
17334 end Comes_From_Generic;
17336 -- Local variables
17338 Def : constant Node_Id := Type_Definition (N);
17339 Iface_Def : Node_Id;
17340 Indic : constant Node_Id := Subtype_Indication (Def);
17341 Extension : constant Node_Id := Record_Extension_Part (Def);
17342 Parent_Node : Node_Id;
17343 Taggd : Boolean;
17345 -- Start of processing for Derived_Type_Declaration
17347 begin
17348 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
17350 -- Ada 2005 (AI-251): In case of interface derivation check that the
17351 -- parent is also an interface.
17353 if Interface_Present (Def) then
17354 if not Is_Interface (Parent_Type) then
17355 Diagnose_Interface (Indic, Parent_Type);
17357 else
17358 Parent_Node := Parent (Base_Type (Parent_Type));
17359 Iface_Def := Type_Definition (Parent_Node);
17361 -- Ada 2005 (AI-251): Limited interfaces can only inherit from
17362 -- other limited interfaces.
17364 if Limited_Present (Def) then
17365 if Limited_Present (Iface_Def) then
17366 null;
17368 elsif Protected_Present (Iface_Def) then
17369 Error_Msg_NE
17370 ("descendant of & must be declared as a protected "
17371 & "interface", N, Parent_Type);
17373 elsif Synchronized_Present (Iface_Def) then
17374 Error_Msg_NE
17375 ("descendant of & must be declared as a synchronized "
17376 & "interface", N, Parent_Type);
17378 elsif Task_Present (Iface_Def) then
17379 Error_Msg_NE
17380 ("descendant of & must be declared as a task interface",
17381 N, Parent_Type);
17383 else
17384 Error_Msg_N
17385 ("(Ada 2005) limited interface cannot inherit from "
17386 & "non-limited interface", Indic);
17387 end if;
17389 -- Ada 2005 (AI-345): Non-limited interfaces can only inherit
17390 -- from non-limited or limited interfaces.
17392 elsif not Protected_Present (Def)
17393 and then not Synchronized_Present (Def)
17394 and then not Task_Present (Def)
17395 then
17396 if Limited_Present (Iface_Def) then
17397 null;
17399 elsif Protected_Present (Iface_Def) then
17400 Error_Msg_NE
17401 ("descendant of & must be declared as a protected "
17402 & "interface", N, Parent_Type);
17404 elsif Synchronized_Present (Iface_Def) then
17405 Error_Msg_NE
17406 ("descendant of & must be declared as a synchronized "
17407 & "interface", N, Parent_Type);
17409 elsif Task_Present (Iface_Def) then
17410 Error_Msg_NE
17411 ("descendant of & must be declared as a task interface",
17412 N, Parent_Type);
17413 else
17414 null;
17415 end if;
17416 end if;
17417 end if;
17418 end if;
17420 if Is_Tagged_Type (Parent_Type)
17421 and then Is_Concurrent_Type (Parent_Type)
17422 and then not Is_Interface (Parent_Type)
17423 then
17424 Error_Msg_N
17425 ("parent type of a record extension cannot be a synchronized "
17426 & "tagged type (RM 3.9.1 (3/1))", N);
17427 Set_Etype (T, Any_Type);
17428 return;
17429 end if;
17431 -- Ada 2005 (AI-251): Decorate all the names in the list of ancestor
17432 -- interfaces
17434 if Is_Tagged_Type (Parent_Type)
17435 and then Is_Non_Empty_List (Interface_List (Def))
17436 then
17437 declare
17438 Intf : Node_Id;
17439 T : Entity_Id;
17441 begin
17442 Intf := First (Interface_List (Def));
17443 while Present (Intf) loop
17444 T := Find_Type_Of_Subtype_Indic (Intf);
17446 if not Is_Interface (T) then
17447 Diagnose_Interface (Intf, T);
17449 -- Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
17450 -- a limited type from having a nonlimited progenitor.
17452 elsif (Limited_Present (Def)
17453 or else (not Is_Interface (Parent_Type)
17454 and then Is_Limited_Type (Parent_Type)))
17455 and then not Is_Limited_Interface (T)
17456 then
17457 Error_Msg_NE
17458 ("progenitor interface& of limited type must be limited",
17459 N, T);
17460 end if;
17462 Next (Intf);
17463 end loop;
17464 end;
17466 -- Check consistency of any nonoverridable aspects that are
17467 -- inherited from multiple sources.
17469 Check_Inherited_Nonoverridable_Aspects
17470 (Inheritor => T,
17471 Interface_List => Interface_List (Def),
17472 Parent_Type => Parent_Type);
17473 end if;
17475 if Parent_Type = Any_Type
17476 or else Etype (Parent_Type) = Any_Type
17477 or else (Is_Class_Wide_Type (Parent_Type)
17478 and then Etype (Parent_Type) = T)
17479 then
17480 -- If Parent_Type is undefined or illegal, make new type into a
17481 -- subtype of Any_Type, and set a few attributes to prevent cascaded
17482 -- errors. If this is a self-definition, emit error now.
17484 if T = Parent_Type or else T = Etype (Parent_Type) then
17485 Error_Msg_N ("type cannot be used in its own definition", Indic);
17486 end if;
17488 Mutate_Ekind (T, Ekind (Parent_Type));
17489 Set_Etype (T, Any_Type);
17490 Set_Scalar_Range (T, Scalar_Range (Any_Type));
17492 -- Initialize the list of primitive operations to an empty list,
17493 -- to cover tagged types as well as untagged types. For untagged
17494 -- types this is used either to analyze the call as legal when
17495 -- GNAT extensions are allowed, or to give better error messages.
17497 Set_Direct_Primitive_Operations (T, New_Elmt_List);
17499 return;
17500 end if;
17502 -- Ada 2005 (AI-251): The case in which the parent of the full-view is
17503 -- an interface is special because the list of interfaces in the full
17504 -- view can be given in any order. For example:
17506 -- type A is interface;
17507 -- type B is interface and A;
17508 -- type D is new B with private;
17509 -- private
17510 -- type D is new A and B with null record; -- 1 --
17512 -- In this case we perform the following transformation of -1-:
17514 -- type D is new B and A with null record;
17516 -- If the parent of the full-view covers the parent of the partial-view
17517 -- we have two possible cases:
17519 -- 1) They have the same parent
17520 -- 2) The parent of the full-view implements some further interfaces
17522 -- In both cases we do not need to perform the transformation. In the
17523 -- first case the source program is correct and the transformation is
17524 -- not needed; in the second case the source program does not fulfill
17525 -- the no-hidden interfaces rule (AI-396) and the error will be reported
17526 -- later.
17528 -- This transformation not only simplifies the rest of the analysis of
17529 -- this type declaration but also simplifies the correct generation of
17530 -- the object layout to the expander.
17532 if In_Private_Part (Current_Scope)
17533 and then Is_Interface (Parent_Type)
17534 then
17535 declare
17536 Partial_View : Entity_Id;
17537 Partial_View_Parent : Entity_Id;
17539 function Reorder_Interfaces return Boolean;
17540 -- Look for an interface in the full view's interface list that
17541 -- matches the parent type of the partial view, and when found,
17542 -- rewrite the full view's parent with the partial view's parent,
17543 -- append the full view's original parent to the interface list,
17544 -- recursively call Derived_Type_Definition on the full type, and
17545 -- return True. If a match is not found, return False.
17547 ------------------------
17548 -- Reorder_Interfaces --
17549 ------------------------
17551 function Reorder_Interfaces return Boolean is
17552 Iface : Node_Id;
17553 New_Iface : Node_Id;
17555 begin
17556 Iface := First (Interface_List (Def));
17557 while Present (Iface) loop
17558 if Etype (Iface) = Etype (Partial_View) then
17559 Rewrite (Subtype_Indication (Def),
17560 New_Copy (Subtype_Indication (Parent (Partial_View))));
17562 New_Iface :=
17563 Make_Identifier (Sloc (N), Chars (Parent_Type));
17564 Rewrite (Iface, New_Iface);
17566 -- Analyze the transformed code
17568 Derived_Type_Declaration (T, N, Is_Completion);
17569 return True;
17570 end if;
17572 Next (Iface);
17573 end loop;
17574 return False;
17575 end Reorder_Interfaces;
17577 begin
17578 -- Look for the associated private type declaration
17580 Partial_View := Incomplete_Or_Partial_View (T);
17582 -- If the partial view was not found then the source code has
17583 -- errors and the transformation is not needed.
17585 if Present (Partial_View) then
17586 Partial_View_Parent := Etype (Partial_View);
17588 -- If the parent of the full-view covers the parent of the
17589 -- partial-view we have nothing else to do.
17591 if Interface_Present_In_Ancestor
17592 (Parent_Type, Partial_View_Parent)
17593 then
17594 null;
17596 -- Traverse the list of interfaces of the full view to look
17597 -- for the parent of the partial view and reorder the
17598 -- interfaces to match the order in the partial view,
17599 -- if needed.
17601 else
17603 if Reorder_Interfaces then
17604 -- Having the interfaces listed in any order is legal.
17605 -- However, the compiler does not properly handle
17606 -- different orders between partial and full views in
17607 -- generic units. We give a warning about the order
17608 -- mismatch, so the user can work around this problem.
17610 Error_Msg_N ("??full declaration does not respect " &
17611 "partial declaration order", T);
17612 Error_Msg_N ("\??consider reordering", T);
17614 return;
17615 end if;
17616 end if;
17617 end if;
17618 end;
17619 end if;
17621 -- Only composite types other than array types are allowed to have
17622 -- discriminants.
17624 if Present (Discriminant_Specifications (N)) then
17625 if (Is_Elementary_Type (Parent_Type)
17626 or else
17627 Is_Array_Type (Parent_Type))
17628 and then not Error_Posted (N)
17629 then
17630 Error_Msg_N
17631 ("elementary or array type cannot have discriminants",
17632 Defining_Identifier (First (Discriminant_Specifications (N))));
17634 -- Unset Has_Discriminants flag to prevent cascaded errors, but
17635 -- only if we are not already processing a malformed syntax tree.
17637 if Is_Type (T) then
17638 Set_Has_Discriminants (T, False);
17639 end if;
17640 end if;
17641 end if;
17643 -- In Ada 83, a derived type defined in a package specification cannot
17644 -- be used for further derivation until the end of its visible part.
17645 -- Note that derivation in the private part of the package is allowed.
17647 if Ada_Version = Ada_83
17648 and then Is_Derived_Type (Parent_Type)
17649 and then In_Visible_Part (Scope (Parent_Type))
17650 then
17651 if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
17652 Error_Msg_N
17653 ("(Ada 83) premature use of type for derivation", Indic);
17654 end if;
17655 end if;
17657 -- Check for early use of incomplete or private type
17659 if Ekind (Parent_Type) in E_Void | E_Incomplete_Type then
17660 Error_Msg_N ("premature derivation of incomplete type", Indic);
17661 return;
17663 elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
17664 and then not Comes_From_Generic (Parent_Type))
17665 or else Has_Private_Component (Parent_Type)
17666 then
17667 -- The ancestor type of a formal type can be incomplete, in which
17668 -- case only the operations of the partial view are available in the
17669 -- generic. Subsequent checks may be required when the full view is
17670 -- analyzed to verify that a derivation from a tagged type has an
17671 -- extension.
17673 if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
17674 null;
17676 elsif No (Underlying_Type (Parent_Type))
17677 or else Has_Private_Component (Parent_Type)
17678 then
17679 Error_Msg_N
17680 ("premature derivation of derived or private type", Indic);
17682 -- Flag the type itself as being in error, this prevents some
17683 -- nasty problems with subsequent uses of the malformed type.
17685 Set_Error_Posted (T);
17687 -- Check that within the immediate scope of an untagged partial
17688 -- view it's illegal to derive from the partial view if the
17689 -- full view is tagged. (7.3(7))
17691 -- We verify that the Parent_Type is a partial view by checking
17692 -- that it is not a Full_Type_Declaration (i.e. a private type or
17693 -- private extension declaration), to distinguish a partial view
17694 -- from a derivation from a private type which also appears as
17695 -- E_Private_Type. If the parent base type is not declared in an
17696 -- enclosing scope there is no need to check.
17698 elsif Present (Full_View (Parent_Type))
17699 and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
17700 and then not Is_Tagged_Type (Parent_Type)
17701 and then Is_Tagged_Type (Full_View (Parent_Type))
17702 and then In_Open_Scopes (Scope (Base_Type (Parent_Type)))
17703 then
17704 Error_Msg_N
17705 ("premature derivation from type with tagged full view",
17706 Indic);
17707 end if;
17708 end if;
17710 -- Check that form of derivation is appropriate
17712 Taggd := Is_Tagged_Type (Parent_Type);
17714 -- Set the parent type to the class-wide type's specific type in this
17715 -- case to prevent cascading errors
17717 if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
17718 Error_Msg_N ("parent type must not be a class-wide type", Indic);
17719 Set_Etype (T, Etype (Parent_Type));
17720 return;
17721 end if;
17723 if Present (Extension) and then not Taggd then
17724 Error_Msg_N
17725 ("type derived from untagged type cannot have extension", Indic);
17727 elsif No (Extension) and then Taggd then
17729 -- If this declaration is within a private part (or body) of a
17730 -- generic instantiation then the derivation is allowed (the parent
17731 -- type can only appear tagged in this case if it's a generic actual
17732 -- type, since it would otherwise have been rejected in the analysis
17733 -- of the generic template).
17735 if not Is_Generic_Actual_Type (Parent_Type)
17736 or else In_Visible_Part (Scope (Parent_Type))
17737 then
17738 if Is_Class_Wide_Type (Parent_Type) then
17739 Error_Msg_N
17740 ("parent type must not be a class-wide type", Indic);
17742 -- Use specific type to prevent cascaded errors.
17744 Parent_Type := Etype (Parent_Type);
17746 else
17747 Error_Msg_N
17748 ("type derived from tagged type must have extension", Indic);
17749 end if;
17750 end if;
17751 end if;
17753 -- AI-443: Synchronized formal derived types require a private
17754 -- extension. There is no point in checking the ancestor type or
17755 -- the progenitors since the construct is wrong to begin with.
17757 if Ada_Version >= Ada_2005
17758 and then Is_Generic_Type (T)
17759 and then Present (Original_Node (N))
17760 then
17761 declare
17762 Decl : constant Node_Id := Original_Node (N);
17764 begin
17765 if Nkind (Decl) = N_Formal_Type_Declaration
17766 and then Nkind (Formal_Type_Definition (Decl)) =
17767 N_Formal_Derived_Type_Definition
17768 and then Synchronized_Present (Formal_Type_Definition (Decl))
17769 and then No (Extension)
17771 -- Avoid emitting a duplicate error message
17773 and then not Error_Posted (Indic)
17774 then
17775 Error_Msg_N
17776 ("synchronized derived type must have extension", N);
17777 end if;
17778 end;
17779 end if;
17781 if Null_Exclusion_Present (Def)
17782 and then not Is_Access_Type (Parent_Type)
17783 then
17784 Error_Msg_N ("null exclusion can only apply to an access type", N);
17785 end if;
17787 Check_Wide_Character_Restriction (Parent_Type, Indic);
17789 -- Avoid deriving parent primitives of underlying record views
17791 Set_Is_Not_Self_Hidden (T);
17793 Build_Derived_Type (N, Parent_Type, T, Is_Completion,
17794 Derive_Subps => not Is_Underlying_Record_View (T));
17796 -- Check for special mutably tagged type declarations
17798 if Is_Tagged_Type (Parent_Type)
17799 and then not Error_Posted (T)
17800 then
17801 declare
17802 Actions : List_Id;
17803 CW_Typ : constant Entity_Id := Class_Wide_Type (T);
17804 Root_Class_Typ : constant Entity_Id :=
17805 Class_Wide_Type (Root_Type (Parent_Type));
17806 begin
17807 -- Perform various checks when we are indeed looking at a
17808 -- mutably tagged declaration.
17810 if Present (Root_Class_Typ)
17811 and then Is_Mutably_Tagged_Type (Root_Class_Typ)
17812 then
17813 -- Verify the level of the descendant's declaration is not
17814 -- deeper than the root type since this could cause leaking
17815 -- of the type.
17817 if Scope (Root_Class_Typ) /= Scope (T)
17818 and then Deepest_Type_Access_Level (Root_Class_Typ)
17819 < Deepest_Type_Access_Level (T)
17820 then
17821 Error_Msg_NE
17822 ("descendant of mutably tagged type cannot be deeper than"
17823 & " its root", N, Root_Type (T));
17825 elsif Present (Incomplete_Or_Partial_View (T))
17826 and then Is_Tagged_Type (Incomplete_Or_Partial_View (T))
17827 then
17828 Error_Msg_N
17829 ("descendant of mutably tagged type cannot a have partial"
17830 & " view which is tagged", N);
17832 -- Mutably tagged types cannot have discriminants
17834 elsif Present (Discriminant_Specifications (N)) then
17835 Error_Msg_N
17836 ("descendant of mutably tagged type cannot have"
17837 & " discriminates", N);
17839 elsif Present (Interfaces (T))
17840 and then not Is_Empty_Elmt_List (Interfaces (T))
17841 then
17842 Error_Msg_N
17843 ("descendant of mutably tagged type cannot implement"
17844 & " an interface", N);
17846 -- We have a valid descendant type
17848 else
17849 -- Set inherited attributes
17851 Set_Has_Size_Clause (CW_Typ);
17852 Set_RM_Size (CW_Typ, RM_Size (Root_Class_Typ));
17853 Set_Is_Mutably_Tagged_Type (CW_Typ);
17855 -- Generate a new class-wide equivalent type
17857 Set_Class_Wide_Equivalent_Type (CW_Typ,
17858 Make_CW_Equivalent_Type (CW_Typ, Empty, Actions));
17860 Insert_List_After_And_Analyze (N, Actions);
17862 -- Add a Compile_Time_Error sizing check as a hint
17863 -- to the backend since we don't know the true size of
17864 -- anything at this point.
17866 Insert_After_And_Analyze (N,
17867 Make_CW_Size_Compile_Check (T, Root_Class_Typ));
17868 end if;
17869 end if;
17870 end;
17871 end if;
17873 -- AI-419: The parent type of an explicitly limited derived type must
17874 -- be a limited type or a limited interface.
17876 if Limited_Present (Def) then
17877 Set_Is_Limited_Record (T);
17879 if Is_Interface (T) then
17880 Set_Is_Limited_Interface (T);
17881 end if;
17883 if not Is_Limited_Type (Parent_Type)
17884 and then
17885 (not Is_Interface (Parent_Type)
17886 or else not Is_Limited_Interface (Parent_Type))
17887 then
17888 -- AI05-0096: a derivation in the private part of an instance is
17889 -- legal if the generic formal is untagged limited, and the actual
17890 -- is non-limited.
17892 if Is_Generic_Actual_Type (Parent_Type)
17893 and then In_Private_Part (Current_Scope)
17894 and then
17895 not Is_Tagged_Type
17896 (Generic_Parent_Type (Parent (Parent_Type)))
17897 then
17898 null;
17900 else
17901 Error_Msg_NE
17902 ("parent type& of limited type must be limited",
17903 N, Parent_Type);
17904 end if;
17905 end if;
17906 end if;
17907 end Derived_Type_Declaration;
17909 ------------------------
17910 -- Diagnose_Interface --
17911 ------------------------
17913 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id) is
17914 begin
17915 if not Is_Interface (E) and then E /= Any_Type then
17916 Error_Msg_NE ("(Ada 2005) & must be an interface", N, E);
17917 end if;
17918 end Diagnose_Interface;
17920 ----------------------------------
17921 -- Enumeration_Type_Declaration --
17922 ----------------------------------
17924 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
17925 Ev : Uint;
17926 L : Node_Id;
17927 R_Node : Node_Id;
17928 B_Node : Node_Id;
17930 begin
17931 -- Create identifier node representing lower bound
17933 B_Node := New_Node (N_Identifier, Sloc (Def));
17934 L := First (Literals (Def));
17935 Set_Chars (B_Node, Chars (L));
17936 Set_Entity (B_Node, L);
17937 Set_Etype (B_Node, T);
17938 Set_Is_Static_Expression (B_Node, True);
17940 R_Node := New_Node (N_Range, Sloc (Def));
17941 Set_Low_Bound (R_Node, B_Node);
17943 Mutate_Ekind (T, E_Enumeration_Type);
17944 Set_First_Literal (T, L);
17945 Set_Etype (T, T);
17946 Set_Is_Constrained (T);
17948 Ev := Uint_0;
17950 -- Loop through literals of enumeration type setting pos and rep values
17951 -- except that if the Ekind is already set, then it means the literal
17952 -- was already constructed (case of a derived type declaration and we
17953 -- should not disturb the Pos and Rep values.
17955 while Present (L) loop
17956 if Ekind (L) /= E_Enumeration_Literal then
17957 Mutate_Ekind (L, E_Enumeration_Literal);
17958 Set_Is_Not_Self_Hidden (L);
17959 Set_Enumeration_Pos (L, Ev);
17960 Set_Enumeration_Rep (L, Ev);
17961 Set_Is_Known_Valid (L, True);
17962 end if;
17964 Set_Etype (L, T);
17965 New_Overloaded_Entity (L);
17966 Generate_Definition (L);
17967 Set_Convention (L, Convention_Intrinsic);
17969 -- Case of character literal
17971 if Nkind (L) = N_Defining_Character_Literal then
17972 Set_Is_Character_Type (T, True);
17974 -- Check violation of No_Wide_Characters
17976 if Restriction_Check_Required (No_Wide_Characters) then
17977 Get_Name_String (Chars (L));
17979 if Name_Len >= 3 and then Name_Buffer (1 .. 2) = "QW" then
17980 Check_Restriction (No_Wide_Characters, L);
17981 end if;
17982 end if;
17983 end if;
17985 Ev := Ev + 1;
17986 Next (L);
17987 end loop;
17989 -- Now create a node representing upper bound
17991 B_Node := New_Node (N_Identifier, Sloc (Def));
17992 Set_Chars (B_Node, Chars (Last (Literals (Def))));
17993 Set_Entity (B_Node, Last (Literals (Def)));
17994 Set_Etype (B_Node, T);
17995 Set_Is_Static_Expression (B_Node, True);
17997 Set_High_Bound (R_Node, B_Node);
17999 -- Initialize various fields of the type. Some of this information
18000 -- may be overwritten later through rep. clauses.
18002 Set_Scalar_Range (T, R_Node);
18003 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
18004 Set_Enum_Esize (T);
18005 Set_Enum_Pos_To_Rep (T, Empty);
18007 -- Set Discard_Names if configuration pragma set, or if there is
18008 -- a parameterless pragma in the current declarative region
18010 if Global_Discard_Names or else Discard_Names (Scope (T)) then
18011 Set_Discard_Names (T);
18012 end if;
18014 -- Process end label if there is one
18016 if Present (Def) then
18017 Process_End_Label (Def, 'e', T);
18018 end if;
18019 end Enumeration_Type_Declaration;
18021 ---------------------------------
18022 -- Expand_To_Stored_Constraint --
18023 ---------------------------------
18025 function Expand_To_Stored_Constraint
18026 (Typ : Entity_Id;
18027 Constraint : Elist_Id) return Elist_Id
18029 Explicitly_Discriminated_Type : Entity_Id;
18030 Expansion : Elist_Id;
18031 Discriminant : Entity_Id;
18033 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
18034 -- Find the nearest type that actually specifies discriminants
18036 ---------------------------------
18037 -- Type_With_Explicit_Discrims --
18038 ---------------------------------
18040 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
18041 Typ : constant E := Base_Type (Id);
18043 begin
18044 if Ekind (Typ) in Incomplete_Or_Private_Kind then
18045 if Present (Full_View (Typ)) then
18046 return Type_With_Explicit_Discrims (Full_View (Typ));
18047 end if;
18049 else
18050 if Has_Discriminants (Typ) then
18051 return Typ;
18052 end if;
18053 end if;
18055 if Etype (Typ) = Typ then
18056 return Empty;
18057 elsif Has_Discriminants (Typ) then
18058 return Typ;
18059 else
18060 return Type_With_Explicit_Discrims (Etype (Typ));
18061 end if;
18063 end Type_With_Explicit_Discrims;
18065 -- Start of processing for Expand_To_Stored_Constraint
18067 begin
18068 if No (Constraint) or else Is_Empty_Elmt_List (Constraint) then
18069 return No_Elist;
18070 end if;
18072 Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
18074 if No (Explicitly_Discriminated_Type) then
18075 return No_Elist;
18076 end if;
18078 Expansion := New_Elmt_List;
18080 Discriminant :=
18081 First_Stored_Discriminant (Explicitly_Discriminated_Type);
18082 while Present (Discriminant) loop
18083 Append_Elmt
18084 (Get_Discriminant_Value
18085 (Discriminant, Explicitly_Discriminated_Type, Constraint),
18086 To => Expansion);
18087 Next_Stored_Discriminant (Discriminant);
18088 end loop;
18090 return Expansion;
18091 end Expand_To_Stored_Constraint;
18093 ---------------------------
18094 -- Find_Hidden_Interface --
18095 ---------------------------
18097 function Find_Hidden_Interface
18098 (Src : Elist_Id;
18099 Dest : Elist_Id) return Entity_Id
18101 Iface : Entity_Id;
18102 Iface_Elmt : Elmt_Id;
18104 begin
18105 if Present (Src) and then Present (Dest) then
18106 Iface_Elmt := First_Elmt (Src);
18107 while Present (Iface_Elmt) loop
18108 Iface := Node (Iface_Elmt);
18110 if Is_Interface (Iface)
18111 and then not Contain_Interface (Iface, Dest)
18112 then
18113 return Iface;
18114 end if;
18116 Next_Elmt (Iface_Elmt);
18117 end loop;
18118 end if;
18120 return Empty;
18121 end Find_Hidden_Interface;
18123 --------------------
18124 -- Find_Type_Name --
18125 --------------------
18127 function Find_Type_Name (N : Node_Id) return Entity_Id is
18128 Id : constant Entity_Id := Defining_Identifier (N);
18129 New_Id : Entity_Id;
18130 Prev : Entity_Id;
18131 Prev_Par : Node_Id;
18133 procedure Check_Duplicate_Aspects;
18134 -- Check that aspects specified in a completion have not been specified
18135 -- already in the partial view.
18137 procedure Tag_Mismatch;
18138 -- Diagnose a tagged partial view whose full view is untagged. We post
18139 -- the message on the full view, with a reference to the previous
18140 -- partial view. The partial view can be private or incomplete, and
18141 -- these are handled in a different manner, so we determine the position
18142 -- of the error message from the respective slocs of both.
18144 -----------------------------
18145 -- Check_Duplicate_Aspects --
18146 -----------------------------
18148 procedure Check_Duplicate_Aspects is
18149 function Get_Partial_View_Aspect (Asp : Node_Id) return Node_Id;
18150 -- Return the corresponding aspect of the partial view which matches
18151 -- the aspect id of Asp. Return Empty is no such aspect exists.
18153 -----------------------------
18154 -- Get_Partial_View_Aspect --
18155 -----------------------------
18157 function Get_Partial_View_Aspect (Asp : Node_Id) return Node_Id is
18158 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp);
18159 Prev_Asps : constant List_Id := Aspect_Specifications (Prev_Par);
18160 Prev_Asp : Node_Id;
18162 begin
18163 Prev_Asp := First (Prev_Asps);
18164 while Present (Prev_Asp) loop
18165 if Get_Aspect_Id (Prev_Asp) = Asp_Id then
18166 return Prev_Asp;
18167 end if;
18169 Next (Prev_Asp);
18170 end loop;
18172 return Empty;
18173 end Get_Partial_View_Aspect;
18175 -- Local variables
18177 Full_Asps : constant List_Id := Aspect_Specifications (N);
18178 Full_Asp : Node_Id;
18179 Part_Asp : Node_Id;
18181 -- Start of processing for Check_Duplicate_Aspects
18183 begin
18184 Full_Asp := First (Full_Asps);
18185 while Present (Full_Asp) loop
18186 Part_Asp := Get_Partial_View_Aspect (Full_Asp);
18188 -- An aspect and its class-wide counterpart are two distinct
18189 -- aspects and may apply to both views of an entity.
18191 if Present (Part_Asp)
18192 and then Class_Present (Part_Asp) = Class_Present (Full_Asp)
18193 then
18194 Error_Msg_N
18195 ("aspect already specified in private declaration", Full_Asp);
18197 Remove (Full_Asp);
18198 return;
18199 end if;
18201 if Has_Discriminants (Prev)
18202 and then not Has_Unknown_Discriminants (Prev)
18203 and then Get_Aspect_Id (Full_Asp) =
18204 Aspect_Implicit_Dereference
18205 then
18206 Error_Msg_N
18207 ("cannot specify aspect if partial view has known "
18208 & "discriminants", Full_Asp);
18209 end if;
18211 Next (Full_Asp);
18212 end loop;
18213 end Check_Duplicate_Aspects;
18215 ------------------
18216 -- Tag_Mismatch --
18217 ------------------
18219 procedure Tag_Mismatch is
18220 begin
18221 if Sloc (Prev) < Sloc (Id) then
18222 if Ada_Version >= Ada_2012
18223 and then Nkind (N) = N_Private_Type_Declaration
18224 then
18225 Error_Msg_NE
18226 ("declaration of private } must be a tagged type", Id, Prev);
18227 else
18228 Error_Msg_NE
18229 ("full declaration of } must be a tagged type", Id, Prev);
18230 end if;
18232 else
18233 if Ada_Version >= Ada_2012
18234 and then Nkind (N) = N_Private_Type_Declaration
18235 then
18236 Error_Msg_NE
18237 ("declaration of private } must be a tagged type", Prev, Id);
18238 else
18239 Error_Msg_NE
18240 ("full declaration of } must be a tagged type", Prev, Id);
18241 end if;
18242 end if;
18243 end Tag_Mismatch;
18245 -- Start of processing for Find_Type_Name
18247 begin
18248 -- Find incomplete declaration, if one was given
18250 Prev := Current_Entity_In_Scope (Id);
18252 -- New type declaration
18254 if No (Prev) then
18255 Enter_Name (Id);
18256 return Id;
18258 -- Previous declaration exists
18260 else
18261 Prev_Par := Parent (Prev);
18263 -- Error if not incomplete/private case except if previous
18264 -- declaration is implicit, etc. Enter_Name will emit error if
18265 -- appropriate.
18267 if not Is_Incomplete_Or_Private_Type (Prev) then
18268 Enter_Name (Id);
18269 New_Id := Id;
18271 -- Check invalid completion of private or incomplete type
18273 elsif Nkind (N) not in N_Full_Type_Declaration
18274 | N_Task_Type_Declaration
18275 | N_Protected_Type_Declaration
18276 and then
18277 (Ada_Version < Ada_2012
18278 or else not Is_Incomplete_Type (Prev)
18279 or else Nkind (N) not in N_Private_Type_Declaration
18280 | N_Private_Extension_Declaration)
18281 then
18282 -- Completion must be a full type declarations (RM 7.3(4))
18284 Error_Msg_Sloc := Sloc (Prev);
18285 Error_Msg_NE ("invalid completion of }", Id, Prev);
18287 -- Set scope of Id to avoid cascaded errors. Entity is never
18288 -- examined again, except when saving globals in generics.
18290 Set_Scope (Id, Current_Scope);
18291 New_Id := Id;
18293 -- If this is a repeated incomplete declaration, no further
18294 -- checks are possible.
18296 if Nkind (N) = N_Incomplete_Type_Declaration then
18297 return Prev;
18298 end if;
18300 -- Case of full declaration of incomplete type
18302 elsif Ekind (Prev) = E_Incomplete_Type
18303 and then (Ada_Version < Ada_2012
18304 or else No (Full_View (Prev))
18305 or else not Is_Private_Type (Full_View (Prev)))
18306 then
18307 -- Indicate that the incomplete declaration has a matching full
18308 -- declaration. The defining occurrence of the incomplete
18309 -- declaration remains the visible one, and the procedure
18310 -- Get_Full_View dereferences it whenever the type is used.
18312 if Present (Full_View (Prev)) then
18313 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
18314 end if;
18316 Set_Full_View (Prev, Id);
18317 Append_Entity (Id, Current_Scope);
18318 Set_Is_Public (Id, Is_Public (Prev));
18319 Set_Is_Internal (Id);
18320 New_Id := Prev;
18322 -- If the incomplete view is tagged, a class_wide type has been
18323 -- created already. Use it for the private type as well, in order
18324 -- to prevent multiple incompatible class-wide types that may be
18325 -- created for self-referential anonymous access components.
18327 if Is_Tagged_Type (Prev)
18328 and then Present (Class_Wide_Type (Prev))
18329 then
18330 Mutate_Ekind (Id, Ekind (Prev)); -- will be reset later
18331 Set_Class_Wide_Type (Id, Class_Wide_Type (Prev));
18333 -- Type of the class-wide type is the current Id. Previously
18334 -- this was not done for private declarations because of order-
18335 -- of-elaboration issues in the back end, but gigi now handles
18336 -- this properly.
18338 Set_Etype (Class_Wide_Type (Id), Id);
18339 end if;
18341 -- Case of full declaration of private type
18343 else
18344 -- If the private type was a completion of an incomplete type then
18345 -- update Prev to reference the private type
18347 if Ada_Version >= Ada_2012
18348 and then Ekind (Prev) = E_Incomplete_Type
18349 and then Present (Full_View (Prev))
18350 and then Is_Private_Type (Full_View (Prev))
18351 then
18352 Prev := Full_View (Prev);
18353 Prev_Par := Parent (Prev);
18354 end if;
18356 if Nkind (N) = N_Full_Type_Declaration
18357 and then Nkind (Type_Definition (N)) in
18358 N_Record_Definition | N_Derived_Type_Definition
18359 and then Interface_Present (Type_Definition (N))
18360 then
18361 Error_Msg_N
18362 ("completion of private type cannot be an interface", N);
18363 end if;
18365 if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
18366 if Etype (Prev) /= Prev then
18368 -- Prev is a private subtype or a derived type, and needs
18369 -- no completion.
18371 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
18372 New_Id := Id;
18374 elsif Ekind (Prev) = E_Private_Type
18375 and then Nkind (N) in N_Task_Type_Declaration
18376 | N_Protected_Type_Declaration
18377 then
18378 Error_Msg_N
18379 ("completion of nonlimited type cannot be limited", N);
18381 elsif Ekind (Prev) = E_Record_Type_With_Private
18382 and then Nkind (N) in N_Task_Type_Declaration
18383 | N_Protected_Type_Declaration
18384 then
18385 if not Is_Limited_Record (Prev) then
18386 Error_Msg_N
18387 ("completion of nonlimited type cannot be limited", N);
18389 elsif No (Interface_List (N)) then
18390 Error_Msg_N
18391 ("completion of tagged private type must be tagged",
18393 end if;
18394 end if;
18396 -- Ada 2005 (AI-251): Private extension declaration of a task
18397 -- type or a protected type. This case arises when covering
18398 -- interface types.
18400 elsif Nkind (N) in N_Task_Type_Declaration
18401 | N_Protected_Type_Declaration
18402 then
18403 null;
18405 elsif Nkind (N) /= N_Full_Type_Declaration
18406 or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
18407 then
18408 Error_Msg_N
18409 ("full view of private extension must be an extension", N);
18411 elsif not (Abstract_Present (Parent (Prev)))
18412 and then Abstract_Present (Type_Definition (N))
18413 then
18414 Error_Msg_N
18415 ("full view of non-abstract extension cannot be abstract", N);
18416 end if;
18418 if not In_Private_Part (Current_Scope) then
18419 Error_Msg_N
18420 ("declaration of full view must appear in private part", N);
18421 end if;
18423 if Ada_Version >= Ada_2012 then
18424 Check_Duplicate_Aspects;
18425 end if;
18427 Copy_And_Swap (Prev, Id);
18428 Set_Has_Private_Declaration (Prev);
18429 Set_Has_Private_Declaration (Id);
18431 -- AI12-0133: Indicate whether we have a partial view with
18432 -- unknown discriminants, in which case initialization of objects
18433 -- of the type do not receive an invariant check.
18435 Set_Partial_View_Has_Unknown_Discr
18436 (Prev, Has_Unknown_Discriminants (Id));
18438 -- Preserve aspect and iterator flags that may have been set on
18439 -- the partial view.
18441 Set_Has_Delayed_Aspects (Prev, Has_Delayed_Aspects (Id));
18442 Set_Has_Implicit_Dereference (Prev, Has_Implicit_Dereference (Id));
18444 -- If no error, propagate freeze_node from private to full view.
18445 -- It may have been generated for an early operational item.
18447 if Present (Freeze_Node (Id))
18448 and then Serious_Errors_Detected = 0
18449 and then No (Full_View (Id))
18450 then
18451 Set_Freeze_Node (Prev, Freeze_Node (Id));
18452 Set_Freeze_Node (Id, Empty);
18453 Set_First_Rep_Item (Prev, First_Rep_Item (Id));
18454 end if;
18456 Set_Full_View (Id, Prev);
18457 New_Id := Prev;
18458 end if;
18460 -- Verify that full declaration conforms to partial one
18462 if Is_Incomplete_Or_Private_Type (Prev)
18463 and then Present (Discriminant_Specifications (Prev_Par))
18464 then
18465 if Present (Discriminant_Specifications (N)) then
18466 if Ekind (Prev) = E_Incomplete_Type then
18467 Check_Discriminant_Conformance (N, Prev, Prev);
18468 else
18469 Check_Discriminant_Conformance (N, Prev, Id);
18470 end if;
18472 else
18473 Error_Msg_N
18474 ("missing discriminants in full type declaration", N);
18476 -- To avoid cascaded errors on subsequent use, share the
18477 -- discriminants of the partial view.
18479 Set_Discriminant_Specifications (N,
18480 Discriminant_Specifications (Prev_Par));
18481 end if;
18482 end if;
18484 -- A prior untagged partial view can have an associated class-wide
18485 -- type due to use of the class attribute, and in this case the full
18486 -- type must also be tagged. This Ada 95 usage is deprecated in favor
18487 -- of incomplete tagged declarations, but we check for it.
18489 if Is_Type (Prev)
18490 and then (Is_Tagged_Type (Prev)
18491 or else Present (Class_Wide_Type (Prev)))
18492 then
18493 -- Ada 2012 (AI05-0162): A private type may be the completion of
18494 -- an incomplete type.
18496 if Ada_Version >= Ada_2012
18497 and then Is_Incomplete_Type (Prev)
18498 and then Nkind (N) in N_Private_Type_Declaration
18499 | N_Private_Extension_Declaration
18500 then
18501 -- No need to check private extensions since they are tagged
18503 if Nkind (N) = N_Private_Type_Declaration
18504 and then not Tagged_Present (N)
18505 then
18506 Tag_Mismatch;
18507 end if;
18509 -- The full declaration is either a tagged type (including
18510 -- a synchronized type that implements interfaces) or a
18511 -- type extension, otherwise this is an error.
18513 elsif Nkind (N) in N_Task_Type_Declaration
18514 | N_Protected_Type_Declaration
18515 then
18516 if No (Interface_List (N)) and then not Error_Posted (N) then
18517 Tag_Mismatch;
18518 end if;
18520 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
18522 -- Indicate that the previous declaration (tagged incomplete
18523 -- or private declaration) requires the same on the full one.
18525 if not Tagged_Present (Type_Definition (N)) then
18526 Tag_Mismatch;
18527 Set_Is_Tagged_Type (Id);
18528 end if;
18530 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
18531 if No (Record_Extension_Part (Type_Definition (N))) then
18532 Error_Msg_NE
18533 ("full declaration of } must be a record extension",
18534 Prev, Id);
18536 -- Set some attributes to produce a usable full view
18538 Set_Is_Tagged_Type (Id);
18539 end if;
18541 else
18542 Tag_Mismatch;
18543 end if;
18544 end if;
18546 if Present (Prev)
18547 and then Nkind (Parent (Prev)) = N_Incomplete_Type_Declaration
18548 and then Present (Premature_Use (Parent (Prev)))
18549 then
18550 Error_Msg_Sloc := Sloc (N);
18551 Error_Msg_N
18552 ("\full declaration #", Premature_Use (Parent (Prev)));
18553 end if;
18555 return New_Id;
18556 end if;
18557 end Find_Type_Name;
18559 -------------------------
18560 -- Find_Type_Of_Object --
18561 -------------------------
18563 function Find_Type_Of_Object
18564 (Obj_Def : Node_Id;
18565 Related_Nod : Node_Id) return Entity_Id
18567 Def_Kind : constant Node_Kind := Nkind (Obj_Def);
18568 P : Node_Id := Parent (Obj_Def);
18569 T : Entity_Id;
18570 Nam : Name_Id;
18572 begin
18573 -- If the parent is a component_definition node we climb to the
18574 -- component_declaration node.
18576 if Nkind (P) = N_Component_Definition then
18577 P := Parent (P);
18578 end if;
18580 -- Case of an anonymous array subtype
18582 if Def_Kind in N_Array_Type_Definition then
18583 T := Empty;
18584 Array_Type_Declaration (T, Obj_Def);
18586 -- Create an explicit subtype whenever possible
18588 elsif Nkind (P) /= N_Component_Declaration
18589 and then Def_Kind = N_Subtype_Indication
18590 then
18591 -- Base name of subtype on object name, which will be unique in
18592 -- the current scope.
18594 -- If this is a duplicate declaration, return base type, to avoid
18595 -- generating duplicate anonymous types.
18597 if Error_Posted (P) then
18598 Analyze (Subtype_Mark (Obj_Def));
18599 return Entity (Subtype_Mark (Obj_Def));
18600 end if;
18602 Nam :=
18603 New_External_Name
18604 (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
18606 T := Make_Defining_Identifier (Sloc (P), Nam);
18608 -- If In_Spec_Expression, for example within a pre/postcondition,
18609 -- provide enough information for use of the subtype without
18610 -- depending on full analysis and freezing, which will happen when
18611 -- building the corresponding subprogram.
18613 if In_Spec_Expression then
18614 Analyze (Subtype_Mark (Obj_Def));
18616 declare
18617 Base_T : constant Entity_Id := Entity (Subtype_Mark (Obj_Def));
18618 New_Def : constant Node_Id := New_Copy_Tree (Obj_Def);
18619 Decl : constant Node_Id :=
18620 Make_Subtype_Declaration (Sloc (P),
18621 Defining_Identifier => T,
18622 Subtype_Indication => New_Def);
18624 begin
18625 Set_Etype (T, Base_T);
18626 Mutate_Ekind (T, Subtype_Kind (Ekind (Base_T)));
18627 Set_Parent (T, Decl);
18628 Set_Scope (T, Current_Scope);
18630 if Ekind (T) = E_Array_Subtype then
18631 Constrain_Array (T, New_Def, Related_Nod, T, 'P');
18633 elsif Ekind (T) = E_Record_Subtype then
18634 Set_First_Entity (T, First_Entity (Base_T));
18635 Set_Has_Discriminants (T, Has_Discriminants (Base_T));
18636 Set_Is_Constrained (T);
18637 end if;
18639 Insert_Before (Related_Nod, Decl);
18640 end;
18642 return T;
18643 end if;
18645 -- When generating code, insert subtype declaration ahead of
18646 -- declaration that generated it.
18648 Insert_Action (Obj_Def,
18649 Make_Subtype_Declaration (Sloc (P),
18650 Defining_Identifier => T,
18651 Subtype_Indication => Relocate_Node (Obj_Def)));
18653 -- This subtype may need freezing, and this will not be done
18654 -- automatically if the object declaration is not in declarative
18655 -- part. Since this is an object declaration, the type cannot always
18656 -- be frozen here. Deferred constants do not freeze their type
18657 -- (which often enough will be private).
18659 if Nkind (P) = N_Object_Declaration
18660 and then Constant_Present (P)
18661 and then No (Expression (P))
18662 then
18663 null;
18665 -- Here we freeze the base type of object type to catch premature use
18666 -- of discriminated private type without a full view.
18668 else
18669 Insert_Actions (Obj_Def, Freeze_Entity (Base_Type (T), P));
18670 end if;
18672 -- Ada 2005 AI-406: the object definition in an object declaration
18673 -- can be an access definition.
18675 elsif Def_Kind = N_Access_Definition then
18676 T := Access_Definition (Related_Nod, Obj_Def);
18678 Set_Is_Local_Anonymous_Access
18679 (T, Ada_Version < Ada_2012
18680 or else Nkind (P) /= N_Object_Declaration
18681 or else Is_Library_Level_Entity (Defining_Identifier (P)));
18683 -- Otherwise, the object definition is just a subtype_mark
18685 else
18686 T := Process_Subtype (Obj_Def, Related_Nod);
18687 end if;
18689 return T;
18690 end Find_Type_Of_Object;
18692 --------------------------------
18693 -- Find_Type_Of_Subtype_Indic --
18694 --------------------------------
18696 function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
18697 Typ : Entity_Id;
18699 begin
18700 -- Case of subtype mark with a constraint
18702 if Nkind (S) = N_Subtype_Indication then
18703 Find_Type (Subtype_Mark (S));
18704 Typ := Entity (Subtype_Mark (S));
18706 if not
18707 Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
18708 then
18709 Error_Msg_N
18710 ("incorrect constraint for this kind of type", Constraint (S));
18711 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
18712 end if;
18714 -- Otherwise we have a subtype mark without a constraint
18716 elsif Error_Posted (S) then
18717 -- Don't rewrite if S is Empty or Error
18718 if S > Empty_Or_Error then
18719 Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
18720 end if;
18721 return Any_Type;
18723 else
18724 Find_Type (S);
18725 Typ := Entity (S);
18726 end if;
18728 return Typ;
18729 end Find_Type_Of_Subtype_Indic;
18731 -------------------------------------
18732 -- Floating_Point_Type_Declaration --
18733 -------------------------------------
18735 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
18736 Digs : constant Node_Id := Digits_Expression (Def);
18737 Max_Digs_Val : constant Uint := Digits_Value (Standard_Long_Long_Float);
18738 Digs_Val : Uint;
18739 Base_Typ : Entity_Id;
18740 Implicit_Base : Entity_Id;
18742 function Can_Derive_From (E : Entity_Id) return Boolean;
18743 -- Find if given digits value, and possibly a specified range, allows
18744 -- derivation from specified type
18746 procedure Convert_Bound (B : Node_Id);
18747 -- If specified, the bounds must be static but may be of different
18748 -- types. They must be converted into machine numbers of the base type,
18749 -- in accordance with RM 4.9(38).
18751 function Find_Base_Type return Entity_Id;
18752 -- Find a predefined base type that Def can derive from, or generate
18753 -- an error and substitute Long_Long_Float if none exists.
18755 ---------------------
18756 -- Can_Derive_From --
18757 ---------------------
18759 function Can_Derive_From (E : Entity_Id) return Boolean is
18760 Spec : constant Entity_Id := Real_Range_Specification (Def);
18762 begin
18763 -- Check specified "digits" constraint
18765 if Digs_Val > Digits_Value (E) then
18766 return False;
18767 end if;
18769 -- Check for matching range, if specified
18771 if Present (Spec) then
18772 if Expr_Value_R (Type_Low_Bound (E)) >
18773 Expr_Value_R (Low_Bound (Spec))
18774 then
18775 return False;
18776 end if;
18778 if Expr_Value_R (Type_High_Bound (E)) <
18779 Expr_Value_R (High_Bound (Spec))
18780 then
18781 return False;
18782 end if;
18783 end if;
18785 return True;
18786 end Can_Derive_From;
18788 -------------------
18789 -- Convert_Bound --
18790 --------------------
18792 procedure Convert_Bound (B : Node_Id) is
18793 begin
18794 -- If the bound is not a literal it can only be static if it is
18795 -- a static constant, possibly of a specified type.
18797 if Is_Entity_Name (B)
18798 and then Ekind (Entity (B)) = E_Constant
18799 then
18800 Rewrite (B, Constant_Value (Entity (B)));
18801 end if;
18803 if Nkind (B) = N_Real_Literal then
18804 Set_Realval (B, Machine (Base_Typ, Realval (B), Round, B));
18805 Set_Is_Machine_Number (B);
18806 Set_Etype (B, Base_Typ);
18807 end if;
18808 end Convert_Bound;
18810 --------------------
18811 -- Find_Base_Type --
18812 --------------------
18814 function Find_Base_Type return Entity_Id is
18815 Choice : Elmt_Id := First_Elmt (Predefined_Float_Types);
18817 begin
18818 -- Iterate over the predefined types in order, returning the first
18819 -- one that Def can derive from.
18821 while Present (Choice) loop
18822 if Can_Derive_From (Node (Choice)) then
18823 return Node (Choice);
18824 end if;
18826 Next_Elmt (Choice);
18827 end loop;
18829 -- If we can't derive from any existing type, use Long_Long_Float
18830 -- and give appropriate message explaining the problem.
18832 if Digs_Val > Max_Digs_Val then
18833 -- It might be the case that there is a type with the requested
18834 -- range, just not the combination of digits and range.
18836 Error_Msg_N
18837 ("no predefined type has requested range and precision",
18838 Real_Range_Specification (Def));
18840 else
18841 Error_Msg_N
18842 ("range too large for any predefined type",
18843 Real_Range_Specification (Def));
18844 end if;
18846 return Standard_Long_Long_Float;
18847 end Find_Base_Type;
18849 -- Start of processing for Floating_Point_Type_Declaration
18851 begin
18852 Check_Restriction (No_Floating_Point, Def);
18854 -- Create an implicit base type
18856 Implicit_Base :=
18857 Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
18859 -- Analyze and verify digits value
18861 Analyze_And_Resolve (Digs, Any_Integer);
18862 Check_Digits_Expression (Digs);
18863 Digs_Val := Expr_Value (Digs);
18865 -- Process possible range spec and find correct type to derive from
18867 Process_Real_Range_Specification (Def);
18869 -- Check that requested number of digits is not too high.
18871 if Digs_Val > Max_Digs_Val then
18873 -- The check for Max_Base_Digits may be somewhat expensive, as it
18874 -- requires reading System, so only do it when necessary.
18876 declare
18877 Max_Base_Digits : constant Uint :=
18878 Expr_Value
18879 (Expression
18880 (Parent (RTE (RE_Max_Base_Digits))));
18882 begin
18883 if Digs_Val > Max_Base_Digits then
18884 Error_Msg_Uint_1 := Max_Base_Digits;
18885 Error_Msg_N ("digits value out of range, maximum is ^", Digs);
18887 elsif No (Real_Range_Specification (Def)) then
18888 Error_Msg_Uint_1 := Max_Digs_Val;
18889 Error_Msg_N ("types with more than ^ digits need range spec "
18890 & "(RM 3.5.7(6))", Digs);
18891 end if;
18892 end;
18893 end if;
18895 -- Find a suitable type to derive from or complain and use a substitute
18897 Base_Typ := Find_Base_Type;
18899 -- If there are bounds given in the declaration use them as the bounds
18900 -- of the type, otherwise use the bounds of the predefined base type
18901 -- that was chosen based on the Digits value.
18903 if Present (Real_Range_Specification (Def)) then
18904 Set_Scalar_Range (T, Real_Range_Specification (Def));
18905 Set_Is_Constrained (T);
18907 Convert_Bound (Type_Low_Bound (T));
18908 Convert_Bound (Type_High_Bound (T));
18910 else
18911 Set_Scalar_Range (T, Scalar_Range (Base_Typ));
18912 end if;
18914 -- Complete definition of implicit base and declared first subtype. The
18915 -- inheritance of the rep item chain ensures that SPARK-related pragmas
18916 -- are not clobbered when the floating point type acts as a full view of
18917 -- a private type.
18919 Set_Etype (Implicit_Base, Base_Typ);
18920 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
18921 Set_Size_Info (Implicit_Base, Base_Typ);
18922 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
18923 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
18924 Set_Digits_Value (Implicit_Base, Digits_Value (Base_Typ));
18925 Set_Float_Rep (Implicit_Base, Float_Rep (Base_Typ));
18927 Mutate_Ekind (T, E_Floating_Point_Subtype);
18928 Set_Etype (T, Implicit_Base);
18929 Set_Size_Info (T, Implicit_Base);
18930 Set_RM_Size (T, RM_Size (Implicit_Base));
18931 Inherit_Rep_Item_Chain (T, Implicit_Base);
18933 if Digs_Val >= Uint_1 then
18934 Set_Digits_Value (T, Digs_Val);
18935 else
18936 pragma Assert (Serious_Errors_Detected > 0); null;
18937 end if;
18938 end Floating_Point_Type_Declaration;
18940 ----------------------------
18941 -- Get_Discriminant_Value --
18942 ----------------------------
18944 -- This is the situation:
18946 -- There is a non-derived type
18948 -- type T0 (Dx, Dy, Dz...)
18950 -- There are zero or more levels of derivation, with each derivation
18951 -- either purely inheriting the discriminants, or defining its own.
18953 -- type Ti is new Ti-1
18954 -- or
18955 -- type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
18956 -- or
18957 -- subtype Ti is ...
18959 -- The subtype issue is avoided by the use of Original_Record_Component,
18960 -- and the fact that derived subtypes also derive the constraints.
18962 -- This chain leads back from
18964 -- Typ_For_Constraint
18966 -- Typ_For_Constraint has discriminants, and the value for each
18967 -- discriminant is given by its corresponding Elmt of Constraints.
18969 -- Discriminant is some discriminant in this hierarchy
18971 -- We need to return its value
18973 -- We do this by recursively searching each level, and looking for
18974 -- Discriminant. Once we get to the bottom, we start backing up
18975 -- returning the value for it which may in turn be a discriminant
18976 -- further up, so on the backup we continue the substitution.
18978 function Get_Discriminant_Value
18979 (Discriminant : Entity_Id;
18980 Typ_For_Constraint : Entity_Id;
18981 Constraint : Elist_Id) return Node_Id
18983 function Root_Corresponding_Discriminant
18984 (Discr : Entity_Id) return Entity_Id;
18985 -- Given a discriminant, traverse the chain of inherited discriminants
18986 -- and return the topmost discriminant.
18988 function Search_Derivation_Levels
18989 (Ti : Entity_Id;
18990 Discrim_Values : Elist_Id;
18991 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
18992 -- This is the routine that performs the recursive search of levels
18993 -- as described above.
18995 -------------------------------------
18996 -- Root_Corresponding_Discriminant --
18997 -------------------------------------
18999 function Root_Corresponding_Discriminant
19000 (Discr : Entity_Id) return Entity_Id
19002 D : Entity_Id;
19004 begin
19005 D := Discr;
19006 while Present (Corresponding_Discriminant (D)) loop
19007 D := Corresponding_Discriminant (D);
19008 end loop;
19010 return D;
19011 end Root_Corresponding_Discriminant;
19013 ------------------------------
19014 -- Search_Derivation_Levels --
19015 ------------------------------
19017 function Search_Derivation_Levels
19018 (Ti : Entity_Id;
19019 Discrim_Values : Elist_Id;
19020 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
19022 Assoc : Elmt_Id;
19023 Disc : Entity_Id;
19024 Result : Node_Or_Entity_Id;
19025 Result_Entity : Node_Id;
19027 begin
19028 -- If inappropriate type, return Error, this happens only in
19029 -- cascaded error situations, and we want to avoid a blow up.
19031 if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
19032 return Error;
19033 end if;
19035 -- Look deeper if possible. Use Stored_Constraints only for
19036 -- untagged types. For tagged types use the given constraint.
19037 -- This asymmetry needs explanation???
19039 if not Stored_Discrim_Values
19040 and then Present (Stored_Constraint (Ti))
19041 and then not Is_Tagged_Type (Ti)
19042 then
19043 Result :=
19044 Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
19046 else
19047 declare
19048 Td : Entity_Id := Etype (Ti);
19050 begin
19051 -- If the parent type is private, the full view may include
19052 -- renamed discriminants, and it is those stored values that
19053 -- may be needed (the partial view never has more information
19054 -- than the full view).
19056 if Is_Private_Type (Td) and then Present (Full_View (Td)) then
19057 Td := Full_View (Td);
19058 end if;
19060 if Td = Ti then
19061 Result := Discriminant;
19063 else
19064 if Present (Stored_Constraint (Ti)) then
19065 Result :=
19066 Search_Derivation_Levels
19067 (Td, Stored_Constraint (Ti), True);
19068 else
19069 Result :=
19070 Search_Derivation_Levels
19071 (Td, Discrim_Values, Stored_Discrim_Values);
19072 end if;
19073 end if;
19074 end;
19075 end if;
19077 -- Extra underlying places to search, if not found above. For
19078 -- concurrent types, the relevant discriminant appears in the
19079 -- corresponding record. For a type derived from a private type
19080 -- without discriminant, the full view inherits the discriminants
19081 -- of the full view of the parent.
19083 if Result = Discriminant then
19084 if Is_Concurrent_Type (Ti)
19085 and then Present (Corresponding_Record_Type (Ti))
19086 then
19087 Result :=
19088 Search_Derivation_Levels (
19089 Corresponding_Record_Type (Ti),
19090 Discrim_Values,
19091 Stored_Discrim_Values);
19093 elsif Is_Private_Type (Ti)
19094 and then not Has_Discriminants (Ti)
19095 and then Present (Full_View (Ti))
19096 and then Etype (Full_View (Ti)) /= Ti
19097 then
19098 Result :=
19099 Search_Derivation_Levels (
19100 Full_View (Ti),
19101 Discrim_Values,
19102 Stored_Discrim_Values);
19103 end if;
19104 end if;
19106 -- If Result is not a (reference to a) discriminant, return it,
19107 -- otherwise set Result_Entity to the discriminant.
19109 if Nkind (Result) = N_Defining_Identifier then
19110 pragma Assert (Result = Discriminant);
19111 Result_Entity := Result;
19113 else
19114 if not Denotes_Discriminant (Result) then
19115 return Result;
19116 end if;
19118 Result_Entity := Entity (Result);
19119 end if;
19121 -- See if this level of derivation actually has discriminants because
19122 -- tagged derivations can add them, hence the lower levels need not
19123 -- have any.
19125 if not Has_Discriminants (Ti) then
19126 return Result;
19127 end if;
19129 -- Scan Ti's discriminants for Result_Entity, and return its
19130 -- corresponding value, if any.
19132 Result_Entity := Original_Record_Component (Result_Entity);
19134 Assoc := First_Elmt (Discrim_Values);
19136 if Stored_Discrim_Values then
19137 Disc := First_Stored_Discriminant (Ti);
19138 else
19139 Disc := First_Discriminant (Ti);
19140 end if;
19142 while Present (Disc) loop
19144 -- If no further associations return the discriminant, value will
19145 -- be found on the second pass.
19147 if No (Assoc) then
19148 return Result;
19149 end if;
19151 if Original_Record_Component (Disc) = Result_Entity then
19152 return Node (Assoc);
19153 end if;
19155 Next_Elmt (Assoc);
19157 if Stored_Discrim_Values then
19158 Next_Stored_Discriminant (Disc);
19159 else
19160 Next_Discriminant (Disc);
19161 end if;
19162 end loop;
19164 -- Could not find it
19166 return Result;
19167 end Search_Derivation_Levels;
19169 -- Local Variables
19171 Result : Node_Or_Entity_Id;
19173 -- Start of processing for Get_Discriminant_Value
19175 begin
19176 -- ??? This routine is a gigantic mess and will be deleted. For the
19177 -- time being just test for the trivial case before calling recurse.
19179 -- We are now celebrating the 20th anniversary of this comment!
19181 if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
19182 declare
19183 D : Entity_Id;
19184 E : Elmt_Id;
19186 begin
19187 D := First_Discriminant (Typ_For_Constraint);
19188 E := First_Elmt (Constraint);
19189 while Present (D) loop
19190 if Chars (D) = Chars (Discriminant) then
19191 return Node (E);
19192 end if;
19194 Next_Discriminant (D);
19195 Next_Elmt (E);
19196 end loop;
19197 end;
19198 end if;
19200 Result := Search_Derivation_Levels
19201 (Typ_For_Constraint, Constraint, False);
19203 -- ??? hack to disappear when this routine is gone
19205 if Nkind (Result) = N_Defining_Identifier then
19206 declare
19207 D : Entity_Id;
19208 E : Elmt_Id;
19210 begin
19211 D := First_Discriminant (Typ_For_Constraint);
19212 E := First_Elmt (Constraint);
19213 while Present (D) loop
19214 if Root_Corresponding_Discriminant (D) = Discriminant then
19215 return Node (E);
19216 end if;
19218 Next_Discriminant (D);
19219 Next_Elmt (E);
19220 end loop;
19221 end;
19222 end if;
19224 pragma Assert (Nkind (Result) /= N_Defining_Identifier);
19225 return Result;
19226 end Get_Discriminant_Value;
19228 --------------------------
19229 -- Has_Range_Constraint --
19230 --------------------------
19232 function Has_Range_Constraint (N : Node_Id) return Boolean is
19233 C : constant Node_Id := Constraint (N);
19235 begin
19236 if Nkind (C) = N_Range_Constraint then
19237 return True;
19239 elsif Nkind (C) = N_Digits_Constraint then
19240 return
19241 Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
19242 or else Present (Range_Constraint (C));
19244 elsif Nkind (C) = N_Delta_Constraint then
19245 return Present (Range_Constraint (C));
19247 else
19248 return False;
19249 end if;
19250 end Has_Range_Constraint;
19252 ------------------------
19253 -- Inherit_Components --
19254 ------------------------
19256 function Inherit_Components
19257 (N : Node_Id;
19258 Parent_Base : Entity_Id;
19259 Derived_Base : Entity_Id;
19260 Is_Tagged : Boolean;
19261 Inherit_Discr : Boolean;
19262 Discs : Elist_Id) return Elist_Id
19264 Assoc_List : constant Elist_Id := New_Elmt_List;
19266 procedure Inherit_Component
19267 (Old_C : Entity_Id;
19268 Plain_Discrim : Boolean := False;
19269 Stored_Discrim : Boolean := False);
19270 -- Inherits component Old_C from Parent_Base to the Derived_Base. If
19271 -- Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
19272 -- True, Old_C is a stored discriminant. If they are both false then
19273 -- Old_C is a regular component.
19275 -----------------------
19276 -- Inherit_Component --
19277 -----------------------
19279 procedure Inherit_Component
19280 (Old_C : Entity_Id;
19281 Plain_Discrim : Boolean := False;
19282 Stored_Discrim : Boolean := False)
19284 procedure Set_Anonymous_Type (Id : Entity_Id);
19285 -- Id denotes the entity of an access discriminant or anonymous
19286 -- access component. Set the type of Id to either the same type of
19287 -- Old_C or create a new one depending on whether the parent and
19288 -- the child types are in the same scope.
19290 ------------------------
19291 -- Set_Anonymous_Type --
19292 ------------------------
19294 procedure Set_Anonymous_Type (Id : Entity_Id) is
19295 Old_Typ : constant Entity_Id := Etype (Old_C);
19297 begin
19298 if Scope (Parent_Base) = Scope (Derived_Base) then
19299 Set_Etype (Id, Old_Typ);
19301 -- The parent and the derived type are in two different scopes.
19302 -- Reuse the type of the original discriminant / component by
19303 -- copying it in order to preserve all attributes.
19305 else
19306 declare
19307 Typ : constant Entity_Id := New_Copy (Old_Typ);
19309 begin
19310 Set_Etype (Id, Typ);
19312 -- Since we do not generate component declarations for
19313 -- inherited components, associate the itype with the
19314 -- derived type.
19316 Set_Associated_Node_For_Itype (Typ, Parent (Derived_Base));
19317 Set_Scope (Typ, Derived_Base);
19318 end;
19319 end if;
19320 end Set_Anonymous_Type;
19322 -- Local variables and constants
19324 New_C : constant Entity_Id := New_Copy (Old_C);
19326 Corr_Discrim : Entity_Id;
19327 Discrim : Entity_Id;
19329 -- Start of processing for Inherit_Component
19331 begin
19332 pragma Assert (not Is_Tagged or not Stored_Discrim);
19334 Set_Parent (New_C, Parent (Old_C));
19336 -- Regular discriminants and components must be inserted in the scope
19337 -- of the Derived_Base. Do it here.
19339 if not Stored_Discrim then
19340 Enter_Name (New_C);
19341 end if;
19343 -- For tagged types the Original_Record_Component must point to
19344 -- whatever this field was pointing to in the parent type. This has
19345 -- already been achieved by the call to New_Copy above.
19347 if not Is_Tagged then
19348 Set_Original_Record_Component (New_C, New_C);
19349 Set_Corresponding_Record_Component (New_C, Old_C);
19350 end if;
19352 -- Set the proper type of an access discriminant
19354 if Ekind (New_C) = E_Discriminant
19355 and then Ekind (Etype (New_C)) = E_Anonymous_Access_Type
19356 then
19357 Set_Anonymous_Type (New_C);
19358 end if;
19360 -- If we have inherited a component then see if its Etype contains
19361 -- references to Parent_Base discriminants. In this case, replace
19362 -- these references with the constraints given in Discs. We do not
19363 -- do this for the partial view of private types because this is
19364 -- not needed (only the components of the full view will be used
19365 -- for code generation) and cause problem. We also avoid this
19366 -- transformation in some error situations.
19368 if Ekind (New_C) = E_Component then
19370 -- Set the proper type of an anonymous access component
19372 if Ekind (Etype (New_C)) = E_Anonymous_Access_Type then
19373 Set_Anonymous_Type (New_C);
19375 elsif (Is_Private_Type (Derived_Base)
19376 and then not Is_Generic_Type (Derived_Base))
19377 or else (Is_Empty_Elmt_List (Discs)
19378 and then not Expander_Active)
19379 then
19380 Set_Etype (New_C, Etype (Old_C));
19382 else
19383 -- The current component introduces a circularity of the
19384 -- following kind:
19386 -- limited with Pack_2;
19387 -- package Pack_1 is
19388 -- type T_1 is tagged record
19389 -- Comp : access Pack_2.T_2;
19390 -- ...
19391 -- end record;
19392 -- end Pack_1;
19394 -- with Pack_1;
19395 -- package Pack_2 is
19396 -- type T_2 is new Pack_1.T_1 with ...;
19397 -- end Pack_2;
19399 Set_Etype
19400 (New_C,
19401 Constrain_Component_Type
19402 (Old_C, Derived_Base, N, Parent_Base, Discs));
19403 end if;
19404 end if;
19406 if Plain_Discrim then
19407 Set_Corresponding_Discriminant (New_C, Old_C);
19408 Build_Discriminal (New_C);
19410 -- If we are explicitly inheriting a stored discriminant it will be
19411 -- completely hidden.
19413 elsif Stored_Discrim then
19414 Set_Corresponding_Discriminant (New_C, Empty);
19415 Set_Discriminal (New_C, Empty);
19416 Set_Is_Completely_Hidden (New_C);
19418 -- Set the Original_Record_Component of each discriminant in the
19419 -- derived base to point to the corresponding stored that we just
19420 -- created.
19422 Discrim := First_Discriminant (Derived_Base);
19423 while Present (Discrim) loop
19424 Corr_Discrim := Corresponding_Discriminant (Discrim);
19426 -- Corr_Discrim could be missing in an error situation
19428 if Present (Corr_Discrim)
19429 and then Original_Record_Component (Corr_Discrim) = Old_C
19430 then
19431 Set_Original_Record_Component (Discrim, New_C);
19432 Set_Corresponding_Record_Component (Discrim, Empty);
19433 end if;
19435 Next_Discriminant (Discrim);
19436 end loop;
19438 Append_Entity (New_C, Derived_Base);
19439 end if;
19441 if not Is_Tagged then
19442 Append_Elmt (Old_C, Assoc_List);
19443 Append_Elmt (New_C, Assoc_List);
19444 end if;
19445 end Inherit_Component;
19447 -- Variables local to Inherit_Component
19449 Loc : constant Source_Ptr := Sloc (N);
19451 Parent_Discrim : Entity_Id;
19452 Stored_Discrim : Entity_Id;
19453 D : Entity_Id;
19454 Component : Entity_Id;
19456 -- Start of processing for Inherit_Components
19458 begin
19459 if not Is_Tagged then
19460 Append_Elmt (Parent_Base, Assoc_List);
19461 Append_Elmt (Derived_Base, Assoc_List);
19462 end if;
19464 -- Inherit parent discriminants if needed
19466 if Inherit_Discr then
19467 Parent_Discrim := First_Discriminant (Parent_Base);
19468 while Present (Parent_Discrim) loop
19469 Inherit_Component (Parent_Discrim, Plain_Discrim => True);
19470 Next_Discriminant (Parent_Discrim);
19471 end loop;
19472 end if;
19474 -- Create explicit stored discrims for untagged types when necessary
19476 if not Has_Unknown_Discriminants (Derived_Base)
19477 and then Has_Discriminants (Parent_Base)
19478 and then not Is_Tagged
19479 and then
19480 (not Inherit_Discr
19481 or else First_Discriminant (Parent_Base) /=
19482 First_Stored_Discriminant (Parent_Base))
19483 then
19484 Stored_Discrim := First_Stored_Discriminant (Parent_Base);
19485 while Present (Stored_Discrim) loop
19486 Inherit_Component (Stored_Discrim, Stored_Discrim => True);
19487 Next_Stored_Discriminant (Stored_Discrim);
19488 end loop;
19489 end if;
19491 -- See if we can apply the second transformation for derived types, as
19492 -- explained in point 6. in the comments above Build_Derived_Record_Type
19493 -- This is achieved by appending Derived_Base discriminants into Discs,
19494 -- which has the side effect of returning a non empty Discs list to the
19495 -- caller of Inherit_Components, which is what we want. This must be
19496 -- done for private derived types if there are explicit stored
19497 -- discriminants, to ensure that we can retrieve the values of the
19498 -- constraints provided in the ancestors.
19500 if Inherit_Discr
19501 and then Is_Empty_Elmt_List (Discs)
19502 and then Present (First_Discriminant (Derived_Base))
19503 and then
19504 (not Is_Private_Type (Derived_Base)
19505 or else Is_Completely_Hidden
19506 (First_Stored_Discriminant (Derived_Base))
19507 or else Is_Generic_Type (Derived_Base))
19508 then
19509 D := First_Discriminant (Derived_Base);
19510 while Present (D) loop
19511 Append_Elmt (New_Occurrence_Of (D, Loc), Discs);
19512 Next_Discriminant (D);
19513 end loop;
19514 end if;
19516 -- Finally, inherit non-discriminant components unless they are not
19517 -- visible because defined or inherited from the full view of the
19518 -- parent. Don't inherit the _parent field of the parent type.
19520 Component := First_Entity (Parent_Base);
19521 while Present (Component) loop
19523 -- Ada 2005 (AI-251): Do not inherit components associated with
19524 -- secondary tags of the parent.
19526 if Ekind (Component) = E_Component
19527 and then Present (Related_Type (Component))
19528 then
19529 null;
19531 elsif Ekind (Component) /= E_Component
19532 or else Chars (Component) = Name_uParent
19533 then
19534 null;
19536 -- If the derived type is within the parent type's declarative
19537 -- region, then the components can still be inherited even though
19538 -- they aren't visible at this point. This can occur for cases
19539 -- such as within public child units where the components must
19540 -- become visible upon entering the child unit's private part.
19542 elsif not Is_Visible_Component (Component)
19543 and then not In_Open_Scopes (Scope (Parent_Base))
19544 then
19545 null;
19547 elsif Ekind (Derived_Base) in E_Private_Type | E_Limited_Private_Type
19548 then
19549 null;
19551 else
19552 Inherit_Component (Component);
19553 end if;
19555 Next_Entity (Component);
19556 end loop;
19558 -- For tagged derived types, inherited discriminants cannot be used in
19559 -- component declarations of the record extension part. To achieve this
19560 -- we mark the inherited discriminants as not visible.
19562 if Is_Tagged and then Inherit_Discr then
19563 D := First_Discriminant (Derived_Base);
19564 while Present (D) loop
19565 Set_Is_Immediately_Visible (D, False);
19566 Next_Discriminant (D);
19567 end loop;
19568 end if;
19570 return Assoc_List;
19571 end Inherit_Components;
19573 ----------------------
19574 -- Is_EVF_Procedure --
19575 ----------------------
19577 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean is
19578 Formal : Entity_Id;
19580 begin
19581 -- Examine the formals of an Extensions_Visible False procedure looking
19582 -- for a controlling OUT parameter.
19584 if Ekind (Subp) = E_Procedure
19585 and then Extensions_Visible_Status (Subp) = Extensions_Visible_False
19586 then
19587 Formal := First_Formal (Subp);
19588 while Present (Formal) loop
19589 if Ekind (Formal) = E_Out_Parameter
19590 and then Is_Controlling_Formal (Formal)
19591 then
19592 return True;
19593 end if;
19595 Next_Formal (Formal);
19596 end loop;
19597 end if;
19599 return False;
19600 end Is_EVF_Procedure;
19602 --------------------------
19603 -- Is_Private_Primitive --
19604 --------------------------
19606 function Is_Private_Primitive (Prim : Entity_Id) return Boolean is
19607 Prim_Scope : constant Entity_Id := Scope (Prim);
19608 Priv_Entity : Entity_Id;
19609 begin
19610 if Is_Package_Or_Generic_Package (Prim_Scope) then
19611 Priv_Entity := First_Private_Entity (Prim_Scope);
19613 while Present (Priv_Entity) loop
19614 if Priv_Entity = Prim then
19615 return True;
19616 end if;
19618 Next_Entity (Priv_Entity);
19619 end loop;
19620 end if;
19622 return False;
19623 end Is_Private_Primitive;
19625 ------------------------------
19626 -- Is_Valid_Constraint_Kind --
19627 ------------------------------
19629 function Is_Valid_Constraint_Kind
19630 (T_Kind : Type_Kind;
19631 Constraint_Kind : Node_Kind) return Boolean
19633 begin
19634 case T_Kind is
19635 when Enumeration_Kind
19636 | Integer_Kind
19638 return Constraint_Kind = N_Range_Constraint;
19640 when Decimal_Fixed_Point_Kind =>
19641 return Constraint_Kind in N_Digits_Constraint | N_Range_Constraint;
19643 when Ordinary_Fixed_Point_Kind =>
19644 return Constraint_Kind in N_Delta_Constraint | N_Range_Constraint;
19646 when Float_Kind =>
19647 return Constraint_Kind in N_Digits_Constraint | N_Range_Constraint;
19649 when Access_Kind
19650 | Array_Kind
19651 | Class_Wide_Kind
19652 | Concurrent_Kind
19653 | Private_Kind
19654 | E_Incomplete_Type
19655 | E_Record_Subtype
19656 | E_Record_Type
19658 return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
19660 when others =>
19661 return True; -- Error will be detected later
19662 end case;
19663 end Is_Valid_Constraint_Kind;
19665 --------------------------
19666 -- Is_Visible_Component --
19667 --------------------------
19669 function Is_Visible_Component
19670 (C : Entity_Id;
19671 N : Node_Id := Empty) return Boolean
19673 Original_Comp : Entity_Id := Empty;
19674 Original_Type : Entity_Id;
19675 Type_Scope : Entity_Id;
19677 function Is_Local_Type (Typ : Entity_Id) return Boolean;
19678 -- Check whether parent type of inherited component is declared locally,
19679 -- possibly within a nested package or instance. The current scope is
19680 -- the derived record itself.
19682 -------------------
19683 -- Is_Local_Type --
19684 -------------------
19686 function Is_Local_Type (Typ : Entity_Id) return Boolean is
19687 begin
19688 return Scope_Within (Inner => Typ, Outer => Scope (Current_Scope));
19689 end Is_Local_Type;
19691 -- Start of processing for Is_Visible_Component
19693 begin
19694 if Ekind (C) in E_Component | E_Discriminant then
19695 Original_Comp := Original_Record_Component (C);
19696 end if;
19698 if No (Original_Comp) then
19700 -- Premature usage, or previous error
19702 return False;
19704 else
19705 Original_Type := Scope (Original_Comp);
19706 Type_Scope := Scope (Base_Type (Scope (C)));
19707 end if;
19709 -- This test only concerns tagged types
19711 if not Is_Tagged_Type (Original_Type) then
19713 -- Check if this is a renamed discriminant (hidden either by the
19714 -- derived type or by some ancestor), unless we are analyzing code
19715 -- generated by the expander since it may reference such components
19716 -- (for example see the expansion of Deep_Adjust).
19718 if Ekind (C) = E_Discriminant and then Present (N) then
19719 return
19720 not Comes_From_Source (N)
19721 or else not Is_Completely_Hidden (C);
19722 else
19723 return True;
19724 end if;
19726 -- If it is _Parent or _Tag, there is no visibility issue
19728 elsif not Comes_From_Source (Original_Comp) then
19729 return True;
19731 -- Discriminants are visible unless the (private) type has unknown
19732 -- discriminants. If the discriminant reference is inserted for a
19733 -- discriminant check on a full view it is also visible.
19735 elsif Ekind (Original_Comp) = E_Discriminant
19736 and then
19737 (not Has_Unknown_Discriminants (Original_Type)
19738 or else (Present (N)
19739 and then Nkind (N) = N_Selected_Component
19740 and then Nkind (Prefix (N)) = N_Type_Conversion
19741 and then not Comes_From_Source (Prefix (N))))
19742 then
19743 return True;
19745 -- If the component has been declared in an ancestor which is currently
19746 -- a private type, then it is not visible. The same applies if the
19747 -- component's containing type is not in an open scope and the original
19748 -- component's enclosing type is a visible full view of a private type
19749 -- (which can occur in cases where an attempt is being made to reference
19750 -- a component in a sibling package that is inherited from a visible
19751 -- component of a type in an ancestor package; the component in the
19752 -- sibling package should not be visible even though the component it
19753 -- inherited from is visible), but instance bodies are not subject to
19754 -- this second case since they have the Has_Private_View mechanism to
19755 -- ensure proper visibility. This does not apply however in the case
19756 -- where the scope of the type is a private child unit, or when the
19757 -- parent comes from a local package in which the ancestor is currently
19758 -- visible. The latter suppression of visibility is needed for cases
19759 -- that are tested in B730006.
19761 elsif Is_Private_Type (Original_Type)
19762 or else
19763 (not Is_Private_Descendant (Type_Scope)
19764 and then not In_Open_Scopes (Type_Scope)
19765 and then Has_Private_Declaration (Original_Type)
19766 and then not In_Instance_Body)
19767 then
19768 -- If the type derives from an entity in a formal package, there
19769 -- are no additional visible components.
19771 if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
19772 N_Formal_Package_Declaration
19773 then
19774 return False;
19776 -- if we are not in the private part of the current package, there
19777 -- are no additional visible components.
19779 elsif Ekind (Scope (Current_Scope)) = E_Package
19780 and then not In_Private_Part (Scope (Current_Scope))
19781 then
19782 return False;
19783 else
19784 return
19785 Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
19786 and then In_Open_Scopes (Scope (Original_Type))
19787 and then Is_Local_Type (Type_Scope);
19788 end if;
19790 -- There is another weird way in which a component may be invisible when
19791 -- the private and the full view are not derived from the same ancestor.
19792 -- Here is an example :
19794 -- type A1 is tagged record F1 : integer; end record;
19795 -- type A2 is new A1 with record F2 : integer; end record;
19796 -- type T is new A1 with private;
19797 -- private
19798 -- type T is new A2 with null record;
19800 -- In this case, the full view of T inherits F1 and F2 but the private
19801 -- view inherits only F1
19803 else
19804 declare
19805 Ancestor : Entity_Id := Scope (C);
19807 begin
19808 loop
19809 if Ancestor = Original_Type then
19810 return True;
19812 -- The ancestor may have a partial view of the original type,
19813 -- but if the full view is in scope, as in a child body, the
19814 -- component is visible.
19816 elsif In_Private_Part (Scope (Original_Type))
19817 and then Full_View (Ancestor) = Original_Type
19818 then
19819 return True;
19821 elsif Ancestor = Etype (Ancestor) then
19823 -- No further ancestors to examine
19825 return False;
19826 end if;
19828 Ancestor := Etype (Ancestor);
19829 end loop;
19830 end;
19831 end if;
19832 end Is_Visible_Component;
19834 --------------------------
19835 -- Make_Class_Wide_Type --
19836 --------------------------
19838 procedure Make_Class_Wide_Type (T : Entity_Id) is
19839 CW_Type : Entity_Id;
19840 CW_Name : Name_Id;
19841 Next_E : Entity_Id;
19842 Prev_E : Entity_Id;
19844 begin
19845 if Present (Class_Wide_Type (T)) then
19847 -- The class-wide type is a partially decorated entity created for a
19848 -- unanalyzed tagged type referenced through a limited with clause.
19849 -- When the tagged type is analyzed, its class-wide type needs to be
19850 -- redecorated. Note that we reuse the entity created by Decorate_
19851 -- Tagged_Type in order to preserve all links.
19853 if Materialize_Entity (Class_Wide_Type (T)) then
19854 CW_Type := Class_Wide_Type (T);
19855 Set_Materialize_Entity (CW_Type, False);
19857 -- The class wide type can have been defined by the partial view, in
19858 -- which case everything is already done.
19860 else
19861 return;
19862 end if;
19864 -- Default case, we need to create a new class-wide type
19866 else
19867 CW_Type :=
19868 New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
19869 end if;
19871 -- Inherit root type characteristics
19873 CW_Name := Chars (CW_Type);
19874 Next_E := Next_Entity (CW_Type);
19875 Prev_E := Prev_Entity (CW_Type);
19876 Copy_Node (T, CW_Type);
19877 Set_Comes_From_Source (CW_Type, False);
19878 Set_Chars (CW_Type, CW_Name);
19879 Set_Parent (CW_Type, Parent (T));
19880 Set_Prev_Entity (CW_Type, Prev_E);
19881 Set_Next_Entity (CW_Type, Next_E);
19883 -- Ensure we have a new freeze node for the class-wide type. The partial
19884 -- view may have freeze action of its own, requiring a proper freeze
19885 -- node, and the same freeze node cannot be shared between the two
19886 -- types.
19888 Set_Has_Delayed_Freeze (CW_Type);
19889 Set_Freeze_Node (CW_Type, Empty);
19891 -- Customize the class-wide type: It has no prim. op., it cannot be
19892 -- abstract, its Etype points back to the specific root type, and it
19893 -- cannot have any invariants.
19895 if Ekind (CW_Type) in Incomplete_Or_Private_Kind then
19896 Reinit_Field_To_Zero (CW_Type, F_Private_Dependents);
19898 elsif Ekind (CW_Type) in Concurrent_Kind then
19899 Reinit_Field_To_Zero (CW_Type, F_First_Private_Entity);
19900 Reinit_Field_To_Zero (CW_Type, F_Scope_Depth_Value);
19902 if Ekind (CW_Type) in Task_Kind then
19903 Reinit_Field_To_Zero (CW_Type, F_Is_Elaboration_Checks_OK_Id);
19904 Reinit_Field_To_Zero (CW_Type, F_Is_Elaboration_Warnings_OK_Id);
19905 end if;
19907 if Ekind (CW_Type) in E_Task_Type | E_Protected_Type then
19908 Reinit_Field_To_Zero (CW_Type, F_SPARK_Aux_Pragma_Inherited);
19909 end if;
19911 elsif Ekind (CW_Type) = E_Record_Type then
19912 Reinit_Field_To_Zero (CW_Type, F_Corresponding_Concurrent_Type);
19913 end if;
19915 Mutate_Ekind (CW_Type, E_Class_Wide_Type);
19916 Set_Is_Tagged_Type (CW_Type, True);
19917 Set_Direct_Primitive_Operations (CW_Type, New_Elmt_List);
19918 Set_Is_Abstract_Type (CW_Type, False);
19919 Set_Is_Constrained (CW_Type, False);
19920 Set_Is_First_Subtype (CW_Type, Is_First_Subtype (T));
19921 Set_Default_SSO (CW_Type);
19922 Set_Has_Inheritable_Invariants (CW_Type, False);
19923 Set_Has_Inherited_Invariants (CW_Type, False);
19924 Set_Has_Own_Invariants (CW_Type, False);
19926 if Ekind (T) = E_Class_Wide_Subtype then
19927 Set_Etype (CW_Type, Etype (Base_Type (T)));
19928 else
19929 Set_Etype (CW_Type, T);
19930 end if;
19932 Set_No_Tagged_Streams_Pragma (CW_Type, No_Tagged_Streams);
19934 -- If this is the class_wide type of a constrained subtype, it does
19935 -- not have discriminants.
19937 Set_Has_Discriminants (CW_Type,
19938 Has_Discriminants (T) and then not Is_Constrained (T));
19940 Set_Has_Unknown_Discriminants (CW_Type, True);
19941 Set_Class_Wide_Type (T, CW_Type);
19942 Set_Equivalent_Type (CW_Type, Empty);
19944 -- The class-wide type of a class-wide type is itself (RM 3.9(14))
19946 Set_Class_Wide_Type (CW_Type, CW_Type);
19947 end Make_Class_Wide_Type;
19949 ----------------
19950 -- Make_Index --
19951 ----------------
19953 procedure Make_Index
19954 (N : Node_Id;
19955 Related_Nod : Node_Id;
19956 Related_Id : Entity_Id := Empty;
19957 Suffix_Index : Pos := 1)
19959 R : Node_Id;
19960 T : Entity_Id;
19961 Def_Id : Entity_Id := Empty;
19962 Found : Boolean := False;
19964 begin
19965 -- For a discrete range used in a constrained array definition and
19966 -- defined by a range, an implicit conversion to the predefined type
19967 -- INTEGER is assumed if each bound is either a numeric literal, a named
19968 -- number, or an attribute, and the type of both bounds (prior to the
19969 -- implicit conversion) is the type universal_integer. Otherwise, both
19970 -- bounds must be of the same discrete type, other than universal
19971 -- integer; this type must be determinable independently of the
19972 -- context, but using the fact that the type must be discrete and that
19973 -- both bounds must have the same type.
19975 -- Character literals also have a universal type in the absence of
19976 -- of additional context, and are resolved to Standard_Character.
19978 if Nkind (N) = N_Range then
19980 -- The index is given by a range constraint. The bounds are known
19981 -- to be of a consistent type.
19983 if not Is_Overloaded (N) then
19984 T := Etype (N);
19986 -- For universal bounds, choose the specific predefined type
19988 if T = Universal_Integer then
19989 T := Standard_Integer;
19991 elsif T = Any_Character then
19992 Ambiguous_Character (Low_Bound (N));
19994 T := Standard_Character;
19995 end if;
19997 -- The node may be overloaded because some user-defined operators
19998 -- are available, but if a universal interpretation exists it is
19999 -- also the selected one.
20001 elsif Universal_Interpretation (N) = Universal_Integer then
20002 T := Standard_Integer;
20004 else
20005 T := Any_Type;
20007 declare
20008 Ind : Interp_Index;
20009 It : Interp;
20011 begin
20012 Get_First_Interp (N, Ind, It);
20013 while Present (It.Typ) loop
20014 if Is_Discrete_Type (It.Typ) then
20016 if Found
20017 and then not Covers (It.Typ, T)
20018 and then not Covers (T, It.Typ)
20019 then
20020 Error_Msg_N ("ambiguous bounds in discrete range", N);
20021 exit;
20022 else
20023 T := It.Typ;
20024 Found := True;
20025 end if;
20026 end if;
20028 Get_Next_Interp (Ind, It);
20029 end loop;
20031 if T = Any_Type then
20032 Error_Msg_N ("discrete type required for range", N);
20033 Set_Etype (N, Any_Type);
20034 return;
20036 elsif T = Universal_Integer then
20037 T := Standard_Integer;
20038 end if;
20039 end;
20040 end if;
20042 if not Is_Discrete_Type (T) then
20043 Error_Msg_N ("discrete type required for range", N);
20044 Set_Etype (N, Any_Type);
20045 return;
20046 end if;
20048 -- If the range bounds are "T'First .. T'Last" where T is a name of a
20049 -- discrete type, then use T as the type of the index.
20051 if Nkind (Low_Bound (N)) = N_Attribute_Reference
20052 and then Attribute_Name (Low_Bound (N)) = Name_First
20053 and then Is_Entity_Name (Prefix (Low_Bound (N)))
20054 and then Is_Discrete_Type (Entity (Prefix (Low_Bound (N))))
20056 and then Nkind (High_Bound (N)) = N_Attribute_Reference
20057 and then Attribute_Name (High_Bound (N)) = Name_Last
20058 and then Is_Entity_Name (Prefix (High_Bound (N)))
20059 and then Entity (Prefix (High_Bound (N))) = Def_Id
20060 then
20061 Def_Id := Entity (Prefix (Low_Bound (N)));
20062 end if;
20064 R := N;
20065 Process_Range_Expr_In_Decl (R, T);
20067 elsif Nkind (N) = N_Subtype_Indication then
20069 -- The index is given by a subtype with a range constraint
20071 T := Base_Type (Entity (Subtype_Mark (N)));
20073 if not Is_Discrete_Type (T) then
20074 Error_Msg_N ("discrete type required for range", N);
20075 Set_Etype (N, Any_Type);
20076 return;
20077 end if;
20079 R := Range_Expression (Constraint (N));
20081 Resolve (R, T);
20082 Process_Range_Expr_In_Decl (R, Entity (Subtype_Mark (N)));
20084 elsif Nkind (N) = N_Attribute_Reference then
20086 -- Catch beginner's error (use of attribute other than 'Range)
20088 if Attribute_Name (N) /= Name_Range then
20089 Error_Msg_N ("expect attribute ''Range", N);
20090 Set_Etype (N, Any_Type);
20091 return;
20092 end if;
20094 -- If the node denotes the range of a type mark, that is also the
20095 -- resulting type, and we do not need to create an Itype for it.
20097 if Is_Entity_Name (Prefix (N))
20098 and then Comes_From_Source (N)
20099 and then Is_Discrete_Type (Entity (Prefix (N)))
20100 then
20101 Def_Id := Entity (Prefix (N));
20102 end if;
20104 Analyze_And_Resolve (N);
20105 T := Etype (N);
20106 R := N;
20108 -- If none of the above, must be a subtype. We convert this to a
20109 -- range attribute reference because in the case of declared first
20110 -- named subtypes, the types in the range reference can be different
20111 -- from the type of the entity. A range attribute normalizes the
20112 -- reference and obtains the correct types for the bounds.
20114 -- This transformation is in the nature of an expansion, is only
20115 -- done if expansion is active. In particular, it is not done on
20116 -- formal generic types, because we need to retain the name of the
20117 -- original index for instantiation purposes.
20119 else
20120 if not Is_Entity_Name (N) or else not Is_Type (Entity (N)) then
20121 Error_Msg_N ("invalid subtype mark in discrete range", N);
20122 Set_Etype (N, Any_Integer);
20123 return;
20125 else
20126 -- The type mark may be that of an incomplete type. It is only
20127 -- now that we can get the full view, previous analysis does
20128 -- not look specifically for a type mark.
20130 Set_Entity (N, Get_Full_View (Entity (N)));
20131 Set_Etype (N, Entity (N));
20132 Def_Id := Entity (N);
20134 if not Is_Discrete_Type (Def_Id) then
20135 Error_Msg_N ("discrete type required for index", N);
20136 Set_Etype (N, Any_Type);
20137 return;
20138 end if;
20139 end if;
20141 if Expander_Active then
20142 Rewrite (N,
20143 Make_Attribute_Reference (Sloc (N),
20144 Attribute_Name => Name_Range,
20145 Prefix => Relocate_Node (N)));
20147 -- The original was a subtype mark that does not freeze. This
20148 -- means that the rewritten version must not freeze either.
20150 Set_Must_Not_Freeze (N);
20151 Set_Must_Not_Freeze (Prefix (N));
20152 Analyze_And_Resolve (N);
20153 T := Etype (N);
20154 R := N;
20156 -- If expander is inactive, type is legal, nothing else to construct
20158 else
20159 return;
20160 end if;
20161 end if;
20163 if not Is_Discrete_Type (T) then
20164 Error_Msg_N ("discrete type required for range", N);
20165 Set_Etype (N, Any_Type);
20166 return;
20168 elsif T = Any_Type then
20169 Set_Etype (N, Any_Type);
20170 return;
20171 end if;
20173 -- We will now create the appropriate Itype to describe the range, but
20174 -- first a check. If we originally had a subtype, then we just label
20175 -- the range with this subtype. Not only is there no need to construct
20176 -- a new subtype, but it is wrong to do so for two reasons:
20178 -- 1. A legality concern, if we have a subtype, it must not freeze,
20179 -- and the Itype would cause freezing incorrectly
20181 -- 2. An efficiency concern, if we created an Itype, it would not be
20182 -- recognized as the same type for the purposes of eliminating
20183 -- checks in some circumstances.
20185 -- We signal this case by setting the subtype entity in Def_Id
20187 if No (Def_Id) then
20188 Def_Id :=
20189 Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
20190 Set_Etype (Def_Id, Base_Type (T));
20192 if Is_Signed_Integer_Type (T) then
20193 Mutate_Ekind (Def_Id, E_Signed_Integer_Subtype);
20195 elsif Is_Modular_Integer_Type (T) then
20196 Mutate_Ekind (Def_Id, E_Modular_Integer_Subtype);
20198 else
20199 Mutate_Ekind (Def_Id, E_Enumeration_Subtype);
20200 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
20201 Set_First_Literal (Def_Id, First_Literal (T));
20202 end if;
20204 Set_Size_Info (Def_Id, (T));
20205 Set_RM_Size (Def_Id, RM_Size (T));
20206 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
20208 Set_Scalar_Range (Def_Id, R);
20209 Conditional_Delay (Def_Id, T);
20211 -- In the subtype indication case inherit properties of the parent
20213 if Nkind (N) = N_Subtype_Indication then
20215 -- It is enough to inherit predicate flags and not the predicate
20216 -- functions, because predicates on an index type are illegal
20217 -- anyway and the flags are enough to detect them.
20219 Inherit_Predicate_Flags (Def_Id, Entity (Subtype_Mark (N)));
20221 -- If the immediate parent of the new subtype is nonstatic, then
20222 -- the subtype we create is nonstatic as well, even if its bounds
20223 -- are static.
20225 if not Is_OK_Static_Subtype (Entity (Subtype_Mark (N))) then
20226 Set_Is_Non_Static_Subtype (Def_Id);
20227 end if;
20228 end if;
20230 Set_Parent (Def_Id, N);
20231 end if;
20233 -- Final step is to label the index with this constructed type
20235 Set_Etype (N, Def_Id);
20236 end Make_Index;
20238 ------------------------------
20239 -- Modular_Type_Declaration --
20240 ------------------------------
20242 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
20243 Mod_Expr : constant Node_Id := Expression (Def);
20244 M_Val : Uint;
20246 procedure Set_Modular_Size (Bits : Int);
20247 -- Sets RM_Size to Bits, and Esize to normal word size above this
20249 ----------------------
20250 -- Set_Modular_Size --
20251 ----------------------
20253 procedure Set_Modular_Size (Bits : Int) is
20254 Siz : Int;
20256 begin
20257 Set_RM_Size (T, UI_From_Int (Bits));
20259 if Bits < System_Max_Binary_Modulus_Power then
20260 Siz := 8;
20262 while Siz < 128 loop
20263 exit when Bits <= Siz;
20264 Siz := Siz * 2;
20265 end loop;
20267 Set_Esize (T, UI_From_Int (Siz));
20269 else
20270 Set_Esize (T, UI_From_Int (System_Max_Binary_Modulus_Power));
20271 end if;
20273 if not Non_Binary_Modulus (T) and then Esize (T) = RM_Size (T) then
20274 Set_Is_Known_Valid (T);
20275 end if;
20276 end Set_Modular_Size;
20278 -- Start of processing for Modular_Type_Declaration
20280 begin
20281 -- If the mod expression is (exactly) 2 * literal, where literal is
20282 -- 128 or less, then almost certainly the * was meant to be **. Warn.
20284 if Warn_On_Suspicious_Modulus_Value
20285 and then Nkind (Mod_Expr) = N_Op_Multiply
20286 and then Nkind (Left_Opnd (Mod_Expr)) = N_Integer_Literal
20287 and then Intval (Left_Opnd (Mod_Expr)) = Uint_2
20288 and then Nkind (Right_Opnd (Mod_Expr)) = N_Integer_Literal
20289 and then Intval (Right_Opnd (Mod_Expr)) <= Uint_128
20290 then
20291 Error_Msg_N
20292 ("suspicious MOD value, was '*'* intended'??.m?", Mod_Expr);
20293 end if;
20295 -- Proceed with analysis of mod expression
20297 Analyze_And_Resolve (Mod_Expr, Any_Integer);
20299 Set_Etype (T, T);
20300 Mutate_Ekind (T, E_Modular_Integer_Type);
20301 Reinit_Alignment (T);
20302 Set_Is_Constrained (T);
20304 if not Is_OK_Static_Expression (Mod_Expr) then
20305 Flag_Non_Static_Expr
20306 ("non-static expression used for modular type bound!", Mod_Expr);
20307 M_Val := 2 ** System_Max_Binary_Modulus_Power;
20308 else
20309 M_Val := Expr_Value (Mod_Expr);
20310 end if;
20312 if M_Val < 1 then
20313 Error_Msg_N ("modulus value must be positive", Mod_Expr);
20314 M_Val := 2 ** System_Max_Binary_Modulus_Power;
20315 end if;
20317 if M_Val > 2 ** Standard_Long_Integer_Size then
20318 Check_Restriction (No_Long_Long_Integers, Mod_Expr);
20319 end if;
20321 Set_Modulus (T, M_Val);
20323 -- Create bounds for the modular type based on the modulus given in
20324 -- the type declaration and then analyze and resolve those bounds.
20326 Set_Scalar_Range (T,
20327 Make_Range (Sloc (Mod_Expr),
20328 Low_Bound => Make_Integer_Literal (Sloc (Mod_Expr), 0),
20329 High_Bound => Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
20331 -- Properly analyze the literals for the range. We do this manually
20332 -- because we can't go calling Resolve, since we are resolving these
20333 -- bounds with the type, and this type is certainly not complete yet.
20335 Set_Etype (Low_Bound (Scalar_Range (T)), T);
20336 Set_Etype (High_Bound (Scalar_Range (T)), T);
20337 Set_Is_Static_Expression (Low_Bound (Scalar_Range (T)));
20338 Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
20340 -- Loop through powers of two to find number of bits required
20342 for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
20344 -- Binary case
20346 if M_Val = 2 ** Bits then
20347 Set_Modular_Size (Bits);
20348 return;
20350 -- Nonbinary case
20352 elsif M_Val < 2 ** Bits then
20353 Set_Non_Binary_Modulus (T);
20355 if Bits > System_Max_Nonbinary_Modulus_Power then
20356 Error_Msg_Uint_1 :=
20357 UI_From_Int (System_Max_Nonbinary_Modulus_Power);
20358 Error_Msg_F
20359 ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
20360 Set_Modular_Size (System_Max_Binary_Modulus_Power);
20361 return;
20363 else
20364 -- In the nonbinary case, set size as per RM 13.3(55)
20366 Set_Modular_Size (Bits);
20367 return;
20368 end if;
20369 end if;
20371 end loop;
20373 -- If we fall through, then the size exceed System.Max_Binary_Modulus
20374 -- so we just signal an error and set the maximum size.
20376 Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
20377 Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
20379 Set_Modular_Size (System_Max_Binary_Modulus_Power);
20380 Reinit_Alignment (T);
20382 end Modular_Type_Declaration;
20384 --------------------------
20385 -- New_Concatenation_Op --
20386 --------------------------
20388 procedure New_Concatenation_Op (Typ : Entity_Id) is
20389 Loc : constant Source_Ptr := Sloc (Typ);
20390 Op : Entity_Id;
20392 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
20393 -- Create abbreviated declaration for the formal of a predefined
20394 -- Operator 'Op' of type 'Typ'
20396 --------------------
20397 -- Make_Op_Formal --
20398 --------------------
20400 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
20401 Formal : Entity_Id;
20402 begin
20403 Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
20404 Set_Etype (Formal, Typ);
20405 Set_Mechanism (Formal, Default_Mechanism);
20406 return Formal;
20407 end Make_Op_Formal;
20409 -- Start of processing for New_Concatenation_Op
20411 begin
20412 Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
20414 Mutate_Ekind (Op, E_Operator);
20415 Set_Is_Not_Self_Hidden (Op);
20416 Set_Scope (Op, Current_Scope);
20417 Set_Etype (Op, Typ);
20418 Set_Homonym (Op, Get_Name_Entity_Id (Name_Op_Concat));
20419 Set_Is_Immediately_Visible (Op);
20420 Set_Is_Intrinsic_Subprogram (Op);
20421 Set_Has_Completion (Op);
20422 Append_Entity (Op, Current_Scope);
20424 Set_Name_Entity_Id (Name_Op_Concat, Op);
20426 Append_Entity (Make_Op_Formal (Typ, Op), Op);
20427 Append_Entity (Make_Op_Formal (Typ, Op), Op);
20428 end New_Concatenation_Op;
20430 -------------------------
20431 -- OK_For_Limited_Init --
20432 -------------------------
20434 -- ???Check all calls of this, and compare the conditions under which it's
20435 -- called.
20437 function OK_For_Limited_Init
20438 (Typ : Entity_Id;
20439 Exp : Node_Id) return Boolean
20441 begin
20442 return Is_CPP_Constructor_Call (Exp)
20443 or else (Ada_Version >= Ada_2005
20444 and then not Debug_Flag_Dot_L
20445 and then OK_For_Limited_Init_In_05 (Typ, Exp));
20446 end OK_For_Limited_Init;
20448 -------------------------------
20449 -- OK_For_Limited_Init_In_05 --
20450 -------------------------------
20452 function OK_For_Limited_Init_In_05
20453 (Typ : Entity_Id;
20454 Exp : Node_Id) return Boolean
20456 begin
20457 -- An object of a limited interface type can be initialized with any
20458 -- expression of a nonlimited descendant type. However this does not
20459 -- apply if this is a view conversion of some other expression. This
20460 -- is checked below.
20462 if Is_Class_Wide_Type (Typ)
20463 and then Is_Limited_Interface (Typ)
20464 and then not Is_Limited_Type (Etype (Exp))
20465 and then Nkind (Exp) /= N_Type_Conversion
20466 then
20467 return True;
20468 end if;
20470 -- Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
20471 -- case of limited aggregates (including extension aggregates), and
20472 -- function calls. The function call may have been given in prefixed
20473 -- notation, in which case the original node is an indexed component.
20474 -- If the function is parameterless, the original node was an explicit
20475 -- dereference. The function may also be parameterless, in which case
20476 -- the source node is just an identifier.
20478 -- A branch of a conditional expression may have been removed if the
20479 -- condition is statically known. This happens during expansion, and
20480 -- thus will not happen if previous errors were encountered. The check
20481 -- will have been performed on the chosen branch, which replaces the
20482 -- original conditional expression.
20484 if No (Exp) then
20485 return True;
20486 end if;
20488 case Nkind (Original_Node (Exp)) is
20489 when N_Aggregate
20490 | N_Delta_Aggregate
20491 | N_Extension_Aggregate
20492 | N_Function_Call
20493 | N_Op
20495 return True;
20497 when N_Identifier =>
20498 return Present (Entity (Original_Node (Exp)))
20499 and then Ekind (Entity (Original_Node (Exp))) = E_Function;
20501 when N_Qualified_Expression =>
20502 return
20503 OK_For_Limited_Init_In_05
20504 (Typ, Expression (Original_Node (Exp)));
20506 -- Ada 2005 (AI-251): If a class-wide interface object is initialized
20507 -- with a function call, the expander has rewritten the call into an
20508 -- N_Type_Conversion node to force displacement of the pointer to
20509 -- reference the component containing the secondary dispatch table.
20510 -- Otherwise a type conversion is not a legal context.
20511 -- A return statement for a build-in-place function returning a
20512 -- synchronized type also introduces an unchecked conversion.
20514 when N_Type_Conversion
20515 | N_Unchecked_Type_Conversion
20517 return not Comes_From_Source (Exp)
20518 and then
20519 -- If the conversion has been rewritten, check Original_Node;
20520 -- otherwise, check the expression of the compiler-generated
20521 -- conversion (which is a conversion that we want to ignore
20522 -- for purposes of the limited-initialization restrictions).
20524 (if Is_Rewrite_Substitution (Exp)
20525 then OK_For_Limited_Init_In_05 (Typ, Original_Node (Exp))
20526 else OK_For_Limited_Init_In_05 (Typ, Expression (Exp)));
20528 when N_Explicit_Dereference
20529 | N_Indexed_Component
20530 | N_Selected_Component
20532 return Nkind (Exp) = N_Function_Call;
20534 -- A use of 'Input is a function call, hence allowed. Normally the
20535 -- attribute will be changed to a call, but the attribute by itself
20536 -- can occur with -gnatc.
20538 when N_Attribute_Reference =>
20539 return Attribute_Name (Original_Node (Exp)) = Name_Input;
20541 -- "return raise ..." is OK
20543 when N_Raise_Expression =>
20544 return True;
20546 -- For a case expression, all dependent expressions must be legal
20548 when N_Case_Expression =>
20549 declare
20550 Alt : Node_Id;
20552 begin
20553 Alt := First (Alternatives (Original_Node (Exp)));
20554 while Present (Alt) loop
20555 if not OK_For_Limited_Init_In_05 (Typ, Expression (Alt)) then
20556 return False;
20557 end if;
20559 Next (Alt);
20560 end loop;
20562 return True;
20563 end;
20565 -- For an if expression, all dependent expressions must be legal
20567 when N_If_Expression =>
20568 declare
20569 Then_Expr : constant Node_Id :=
20570 Next (First (Expressions (Original_Node (Exp))));
20571 Else_Expr : constant Node_Id := Next (Then_Expr);
20572 begin
20573 return OK_For_Limited_Init_In_05 (Typ, Then_Expr)
20574 and then
20575 OK_For_Limited_Init_In_05 (Typ, Else_Expr);
20576 end;
20578 when others =>
20579 return False;
20580 end case;
20581 end OK_For_Limited_Init_In_05;
20583 -------------------------------------------
20584 -- Ordinary_Fixed_Point_Type_Declaration --
20585 -------------------------------------------
20587 procedure Ordinary_Fixed_Point_Type_Declaration
20588 (T : Entity_Id;
20589 Def : Node_Id)
20591 Loc : constant Source_Ptr := Sloc (Def);
20592 Delta_Expr : constant Node_Id := Delta_Expression (Def);
20593 RRS : constant Node_Id := Real_Range_Specification (Def);
20594 Implicit_Base : Entity_Id;
20595 Delta_Val : Ureal;
20596 Small_Val : Ureal;
20597 Low_Val : Ureal;
20598 High_Val : Ureal;
20600 begin
20601 Check_Restriction (No_Fixed_Point, Def);
20603 -- Create implicit base type
20605 Implicit_Base :=
20606 Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
20607 Set_Etype (Implicit_Base, Implicit_Base);
20609 -- Analyze and process delta expression
20611 Analyze_And_Resolve (Delta_Expr, Any_Real);
20613 Check_Delta_Expression (Delta_Expr);
20614 Delta_Val := Expr_Value_R (Delta_Expr);
20616 Set_Delta_Value (Implicit_Base, Delta_Val);
20618 -- Compute default small from given delta, which is the largest power
20619 -- of two that does not exceed the given delta value.
20621 declare
20622 Tmp : Ureal;
20623 Scale : Int;
20625 begin
20626 Tmp := Ureal_1;
20627 Scale := 0;
20629 if Delta_Val < Ureal_1 then
20630 while Delta_Val < Tmp loop
20631 Tmp := Tmp / Ureal_2;
20632 Scale := Scale + 1;
20633 end loop;
20635 else
20636 loop
20637 Tmp := Tmp * Ureal_2;
20638 exit when Tmp > Delta_Val;
20639 Scale := Scale - 1;
20640 end loop;
20641 end if;
20643 Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
20644 end;
20646 Set_Small_Value (Implicit_Base, Small_Val);
20648 -- If no range was given, set a dummy range
20650 if RRS <= Empty_Or_Error then
20651 Low_Val := -Small_Val;
20652 High_Val := Small_Val;
20654 -- Otherwise analyze and process given range
20656 else
20657 declare
20658 Low : constant Node_Id := Low_Bound (RRS);
20659 High : constant Node_Id := High_Bound (RRS);
20661 begin
20662 Analyze_And_Resolve (Low, Any_Real);
20663 Analyze_And_Resolve (High, Any_Real);
20664 Check_Real_Bound (Low);
20665 Check_Real_Bound (High);
20667 -- Obtain and set the range
20669 Low_Val := Expr_Value_R (Low);
20670 High_Val := Expr_Value_R (High);
20672 if Low_Val > High_Val then
20673 Error_Msg_NE ("??fixed point type& has null range", Def, T);
20674 end if;
20675 end;
20676 end if;
20678 -- The range for both the implicit base and the declared first subtype
20679 -- cannot be set yet, so we use the special routine Set_Fixed_Range to
20680 -- set a temporary range in place. Note that the bounds of the base
20681 -- type will be widened to be symmetrical and to fill the available
20682 -- bits when the type is frozen.
20684 -- We could do this with all discrete types, and probably should, but
20685 -- we absolutely have to do it for fixed-point, since the end-points
20686 -- of the range and the size are determined by the small value, which
20687 -- could be reset before the freeze point.
20689 Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
20690 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
20692 -- Complete definition of first subtype. The inheritance of the rep item
20693 -- chain ensures that SPARK-related pragmas are not clobbered when the
20694 -- ordinary fixed point type acts as a full view of a private type.
20696 Mutate_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
20697 Set_Etype (T, Implicit_Base);
20698 Reinit_Size_Align (T);
20699 Inherit_Rep_Item_Chain (T, Implicit_Base);
20700 Set_Small_Value (T, Small_Val);
20701 Set_Delta_Value (T, Delta_Val);
20702 Set_Is_Constrained (T);
20703 end Ordinary_Fixed_Point_Type_Declaration;
20705 ----------------------------------
20706 -- Preanalyze_Assert_Expression --
20707 ----------------------------------
20709 procedure Preanalyze_Assert_Expression (N : Node_Id; T : Entity_Id) is
20710 begin
20711 In_Assertion_Expr := In_Assertion_Expr + 1;
20712 Preanalyze_Spec_Expression (N, T);
20713 In_Assertion_Expr := In_Assertion_Expr - 1;
20714 end Preanalyze_Assert_Expression;
20716 -- ??? The variant below explicitly saves and restores all the flags,
20717 -- because it is impossible to compose the existing variety of
20718 -- Analyze/Resolve (and their wrappers, e.g. Preanalyze_Spec_Expression)
20719 -- to achieve the desired semantics.
20721 procedure Preanalyze_Assert_Expression (N : Node_Id) is
20722 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
20723 Save_Must_Not_Freeze : constant Boolean := Must_Not_Freeze (N);
20724 Save_Full_Analysis : constant Boolean := Full_Analysis;
20726 begin
20727 In_Assertion_Expr := In_Assertion_Expr + 1;
20728 In_Spec_Expression := True;
20729 Set_Must_Not_Freeze (N);
20730 Inside_Preanalysis_Without_Freezing :=
20731 Inside_Preanalysis_Without_Freezing + 1;
20732 Full_Analysis := False;
20733 Expander_Mode_Save_And_Set (False);
20735 if GNATprove_Mode then
20736 Analyze_And_Resolve (N);
20737 else
20738 Analyze_And_Resolve (N, Suppress => All_Checks);
20739 end if;
20741 Expander_Mode_Restore;
20742 Full_Analysis := Save_Full_Analysis;
20743 Inside_Preanalysis_Without_Freezing :=
20744 Inside_Preanalysis_Without_Freezing - 1;
20745 Set_Must_Not_Freeze (N, Save_Must_Not_Freeze);
20746 In_Spec_Expression := Save_In_Spec_Expression;
20747 In_Assertion_Expr := In_Assertion_Expr - 1;
20748 end Preanalyze_Assert_Expression;
20750 -----------------------------------
20751 -- Preanalyze_Default_Expression --
20752 -----------------------------------
20754 procedure Preanalyze_Default_Expression (N : Node_Id; T : Entity_Id) is
20755 Save_In_Default_Expr : constant Boolean := In_Default_Expr;
20756 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
20758 begin
20759 In_Default_Expr := True;
20760 In_Spec_Expression := True;
20762 Preanalyze_With_Freezing_And_Resolve (N, T);
20764 In_Default_Expr := Save_In_Default_Expr;
20765 In_Spec_Expression := Save_In_Spec_Expression;
20766 end Preanalyze_Default_Expression;
20768 --------------------------------
20769 -- Preanalyze_Spec_Expression --
20770 --------------------------------
20772 procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
20773 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
20774 begin
20775 In_Spec_Expression := True;
20776 Preanalyze_And_Resolve (N, T);
20777 In_Spec_Expression := Save_In_Spec_Expression;
20778 end Preanalyze_Spec_Expression;
20780 ----------------------------------------
20781 -- Prepare_Private_Subtype_Completion --
20782 ----------------------------------------
20784 procedure Prepare_Private_Subtype_Completion
20785 (Id : Entity_Id;
20786 Related_Nod : Node_Id)
20788 Id_B : constant Entity_Id := Base_Type (Id);
20789 Full_B : constant Entity_Id := Full_View (Id_B);
20790 Full : Entity_Id;
20792 begin
20793 if Present (Full_B) then
20795 -- The Base_Type is already completed, we can complete the subtype
20796 -- now. We have to create a new entity with the same name, Thus we
20797 -- can't use Create_Itype.
20799 Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
20800 Set_Is_Itype (Full);
20801 Set_Associated_Node_For_Itype (Full, Related_Nod);
20802 Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
20803 Set_Full_View (Id, Full);
20804 end if;
20806 -- The parent subtype may be private, but the base might not, in some
20807 -- nested instances. In that case, the subtype does not need to be
20808 -- exchanged. It would still be nice to make private subtypes and their
20809 -- bases consistent at all times ???
20811 if Is_Private_Type (Id_B) then
20812 Append_Elmt (Id, Private_Dependents (Id_B));
20813 end if;
20814 end Prepare_Private_Subtype_Completion;
20816 ---------------------------
20817 -- Process_Discriminants --
20818 ---------------------------
20820 procedure Process_Discriminants
20821 (N : Node_Id;
20822 Prev : Entity_Id := Empty)
20824 Elist : constant Elist_Id := New_Elmt_List;
20825 Id : Node_Id;
20826 Discr : Node_Id;
20827 Discr_Number : Uint;
20828 Discr_Type : Entity_Id;
20829 Default_Present : Boolean := False;
20830 Default_Not_Present : Boolean := False;
20832 begin
20833 -- A composite type other than an array type can have discriminants.
20834 -- On entry, the current scope is the composite type.
20836 -- The discriminants are initially entered into the scope of the type
20837 -- via Enter_Name with the default Ekind of E_Void to prevent premature
20838 -- use, as explained at the end of this procedure.
20840 Discr := First (Discriminant_Specifications (N));
20841 while Present (Discr) loop
20842 Enter_Name (Defining_Identifier (Discr));
20844 -- For navigation purposes we add a reference to the discriminant
20845 -- in the entity for the type. If the current declaration is a
20846 -- completion, place references on the partial view. Otherwise the
20847 -- type is the current scope.
20849 if Present (Prev) then
20851 -- The references go on the partial view, if present. If the
20852 -- partial view has discriminants, the references have been
20853 -- generated already.
20855 if not Has_Discriminants (Prev) then
20856 Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
20857 end if;
20858 else
20859 Generate_Reference
20860 (Current_Scope, Defining_Identifier (Discr), 'd');
20861 end if;
20863 if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
20864 Check_Anonymous_Access_Component
20865 (Typ_Decl => N,
20866 Typ => Defining_Identifier (N),
20867 Prev => Prev,
20868 Comp_Def => Discr,
20869 Access_Def => Discriminant_Type (Discr));
20871 -- if Check_Anonymous_Access_Component replaced Discr then
20872 -- its Original_Node points to the old Discr and the access type
20873 -- for Discr_Type has already been created.
20875 if Is_Rewrite_Substitution (Discr) then
20876 Discr_Type := Etype (Discriminant_Type (Discr));
20877 else
20878 Discr_Type :=
20879 Access_Definition (Discr, Discriminant_Type (Discr));
20881 -- Ada 2005 (AI-254)
20883 if Present (Access_To_Subprogram_Definition
20884 (Discriminant_Type (Discr)))
20885 and then Protected_Present (Access_To_Subprogram_Definition
20886 (Discriminant_Type (Discr)))
20887 then
20888 Discr_Type :=
20889 Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
20890 end if;
20891 end if;
20892 else
20893 Find_Type (Discriminant_Type (Discr));
20894 Discr_Type := Etype (Discriminant_Type (Discr));
20896 if Error_Posted (Discriminant_Type (Discr)) then
20897 Discr_Type := Any_Type;
20898 end if;
20899 end if;
20901 -- Handling of discriminants that are access types
20903 if Is_Access_Type (Discr_Type) then
20905 -- Ada 2005 (AI-230): Access discriminant allowed in non-
20906 -- limited record types
20908 if Ada_Version < Ada_2005 then
20909 Check_Access_Discriminant_Requires_Limited
20910 (Discr, Discriminant_Type (Discr));
20911 end if;
20913 if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
20914 Error_Msg_N
20915 ("(Ada 83) access discriminant not allowed", Discr);
20916 end if;
20918 -- If not access type, must be a discrete type
20920 elsif not Is_Discrete_Type (Discr_Type) then
20921 Error_Msg_N
20922 ("discriminants must have a discrete or access type",
20923 Discriminant_Type (Discr));
20924 end if;
20926 Set_Etype (Defining_Identifier (Discr), Discr_Type);
20928 -- If a discriminant specification includes the assignment compound
20929 -- delimiter followed by an expression, the expression is the default
20930 -- expression of the discriminant; the default expression must be of
20931 -- the type of the discriminant. (RM 3.7.1) Since this expression is
20932 -- a default expression, we do the special preanalysis, since this
20933 -- expression does not freeze (see section "Handling of Default and
20934 -- Per-Object Expressions" in spec of package Sem).
20936 if Present (Expression (Discr)) then
20937 Preanalyze_Default_Expression (Expression (Discr), Discr_Type);
20939 -- Legaity checks
20941 if Nkind (N) = N_Formal_Type_Declaration then
20942 Error_Msg_N
20943 ("discriminant defaults not allowed for formal type",
20944 Expression (Discr));
20946 -- Flag an error for a tagged type with defaulted discriminants,
20947 -- excluding limited tagged types when compiling for Ada 2012
20948 -- (see AI05-0214).
20950 elsif Is_Tagged_Type (Current_Scope)
20951 and then (not Is_Limited_Type (Current_Scope)
20952 or else Ada_Version < Ada_2012)
20953 and then Comes_From_Source (N)
20954 then
20955 -- Note: see similar test in Check_Or_Process_Discriminants, to
20956 -- handle the (illegal) case of the completion of an untagged
20957 -- view with discriminants with defaults by a tagged full view.
20958 -- We skip the check if Discr does not come from source, to
20959 -- account for the case of an untagged derived type providing
20960 -- defaults for a renamed discriminant from a private untagged
20961 -- ancestor with a tagged full view (ACATS B460006).
20963 if Ada_Version >= Ada_2012 then
20964 Error_Msg_N
20965 ("discriminants of nonlimited tagged type cannot have"
20966 & " defaults",
20967 Expression (Discr));
20968 else
20969 Error_Msg_N
20970 ("discriminants of tagged type cannot have defaults",
20971 Expression (Discr));
20972 end if;
20974 else
20975 Default_Present := True;
20976 Append_Elmt (Expression (Discr), Elist);
20978 -- Tag the defining identifiers for the discriminants with
20979 -- their corresponding default expressions from the tree.
20981 Set_Discriminant_Default_Value
20982 (Defining_Identifier (Discr), Expression (Discr));
20983 end if;
20985 -- In gnatc or GNATprove mode, make sure set Do_Range_Check flag
20986 -- gets set unless we can be sure that no range check is required.
20988 if not Expander_Active
20989 and then not
20990 Is_In_Range
20991 (Expression (Discr), Discr_Type, Assume_Valid => True)
20992 then
20993 Set_Do_Range_Check (Expression (Discr));
20994 end if;
20996 -- No default discriminant value given
20998 else
20999 Default_Not_Present := True;
21000 end if;
21002 -- Ada 2005 (AI-231): Create an Itype that is a duplicate of
21003 -- Discr_Type but with the null-exclusion attribute
21005 if Ada_Version >= Ada_2005 then
21007 -- Ada 2005 (AI-231): Static checks
21009 if Can_Never_Be_Null (Discr_Type) then
21010 Null_Exclusion_Static_Checks (Discr);
21012 elsif Is_Access_Type (Discr_Type)
21013 and then Null_Exclusion_Present (Discr)
21015 -- No need to check itypes because in their case this check
21016 -- was done at their point of creation
21018 and then not Is_Itype (Discr_Type)
21019 then
21020 if Can_Never_Be_Null (Discr_Type) then
21021 Error_Msg_NE
21022 ("`NOT NULL` not allowed (& already excludes null)",
21023 Discr,
21024 Discr_Type);
21025 end if;
21027 Set_Etype (Defining_Identifier (Discr),
21028 Create_Null_Excluding_Itype
21029 (T => Discr_Type,
21030 Related_Nod => Discr));
21032 -- Check for improper null exclusion if the type is otherwise
21033 -- legal for a discriminant.
21035 elsif Null_Exclusion_Present (Discr)
21036 and then Is_Discrete_Type (Discr_Type)
21037 then
21038 Error_Msg_N
21039 ("null exclusion can only apply to an access type", Discr);
21040 end if;
21042 -- Ada 2005 (AI-402): access discriminants of nonlimited types
21043 -- can't have defaults. Synchronized types, or types that are
21044 -- explicitly limited are fine, but special tests apply to derived
21045 -- types in generics: in a generic body we have to assume the
21046 -- worst, and therefore defaults are not allowed if the parent is
21047 -- a generic formal private type (see ACATS B370001).
21049 if Is_Access_Type (Discr_Type) and then Default_Present then
21050 if Ekind (Discr_Type) /= E_Anonymous_Access_Type
21051 or else Is_Limited_Record (Current_Scope)
21052 or else Is_Concurrent_Type (Current_Scope)
21053 or else Is_Concurrent_Record_Type (Current_Scope)
21054 or else Ekind (Current_Scope) = E_Limited_Private_Type
21055 then
21056 if not Is_Derived_Type (Current_Scope)
21057 or else not Is_Generic_Type (Etype (Current_Scope))
21058 or else not In_Package_Body (Scope (Etype (Current_Scope)))
21059 or else Limited_Present
21060 (Type_Definition (Parent (Current_Scope)))
21061 then
21062 null;
21064 else
21065 Error_Msg_N
21066 ("access discriminants of nonlimited types cannot "
21067 & "have defaults", Expression (Discr));
21068 end if;
21070 elsif Present (Expression (Discr)) then
21071 Error_Msg_N
21072 ("(Ada 2005) access discriminants of nonlimited types "
21073 & "cannot have defaults", Expression (Discr));
21074 end if;
21075 end if;
21076 end if;
21078 Next (Discr);
21079 end loop;
21081 -- An element list consisting of the default expressions of the
21082 -- discriminants is constructed in the above loop and used to set
21083 -- the Discriminant_Constraint attribute for the type. If an object
21084 -- is declared of this (record or task) type without any explicit
21085 -- discriminant constraint given, this element list will form the
21086 -- actual parameters for the corresponding initialization procedure
21087 -- for the type.
21089 Set_Discriminant_Constraint (Current_Scope, Elist);
21090 Set_Stored_Constraint (Current_Scope, No_Elist);
21092 -- Default expressions must be provided either for all or for none
21093 -- of the discriminants of a discriminant part. (RM 3.7.1)
21095 if Default_Present and then Default_Not_Present then
21096 Error_Msg_N
21097 ("incomplete specification of defaults for discriminants", N);
21098 end if;
21100 -- The use of the name of a discriminant is not allowed in default
21101 -- expressions of a discriminant part if the specification of the
21102 -- discriminant is itself given in the discriminant part. (RM 3.7.1)
21104 -- To detect this, the discriminant names are entered initially with an
21105 -- Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
21106 -- attempt to use a void entity (for example in an expression that is
21107 -- type-checked) produces the error message: premature usage. Now after
21108 -- completing the semantic analysis of the discriminant part, we can set
21109 -- the Ekind of all the discriminants appropriately.
21111 Discr := First (Discriminant_Specifications (N));
21112 Discr_Number := Uint_1;
21113 while Present (Discr) loop
21114 Id := Defining_Identifier (Discr);
21116 if Ekind (Id) = E_In_Parameter then
21117 Reinit_Field_To_Zero (Id, F_Discriminal_Link);
21118 end if;
21120 Mutate_Ekind (Id, E_Discriminant);
21121 Set_Is_Not_Self_Hidden (Id);
21122 Reinit_Component_Location (Id);
21123 Reinit_Esize (Id);
21124 Set_Discriminant_Number (Id, Discr_Number);
21126 -- Make sure this is always set, even in illegal programs
21128 Set_Corresponding_Discriminant (Id, Empty);
21130 -- Initialize the Original_Record_Component to the entity itself.
21131 -- Inherit_Components will propagate the right value to
21132 -- discriminants in derived record types.
21134 Set_Original_Record_Component (Id, Id);
21136 -- Create the discriminal for the discriminant
21138 Build_Discriminal (Id);
21140 Next (Discr);
21141 Discr_Number := Discr_Number + 1;
21142 end loop;
21144 Set_Has_Discriminants (Current_Scope);
21145 end Process_Discriminants;
21147 -----------------------
21148 -- Process_Full_View --
21149 -----------------------
21151 -- WARNING: This routine manages Ghost regions. Return statements must be
21152 -- replaced by gotos which jump to the end of the routine and restore the
21153 -- Ghost mode.
21155 procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
21156 procedure Collect_Implemented_Interfaces
21157 (Typ : Entity_Id;
21158 Ifaces : Elist_Id);
21159 -- Ada 2005: Gather all the interfaces that Typ directly or
21160 -- inherently implements. Duplicate entries are not added to
21161 -- the list Ifaces.
21163 ------------------------------------
21164 -- Collect_Implemented_Interfaces --
21165 ------------------------------------
21167 procedure Collect_Implemented_Interfaces
21168 (Typ : Entity_Id;
21169 Ifaces : Elist_Id)
21171 Iface : Entity_Id;
21172 Iface_Elmt : Elmt_Id;
21174 begin
21175 -- Abstract interfaces are only associated with tagged record types
21177 if not Is_Tagged_Type (Typ) or else not Is_Record_Type (Typ) then
21178 return;
21179 end if;
21181 -- Recursively climb to the ancestors
21183 if Etype (Typ) /= Typ
21185 -- Protect the frontend against wrong cyclic declarations like:
21187 -- type B is new A with private;
21188 -- type C is new A with private;
21189 -- private
21190 -- type B is new C with null record;
21191 -- type C is new B with null record;
21193 and then Etype (Typ) /= Priv_T
21194 and then Etype (Typ) /= Full_T
21195 then
21196 -- Keep separate the management of private type declarations
21198 if Ekind (Typ) = E_Record_Type_With_Private then
21200 -- Handle the following illegal usage:
21201 -- type Private_Type is tagged private;
21202 -- private
21203 -- type Private_Type is new Type_Implementing_Iface;
21205 if Present (Full_View (Typ))
21206 and then Etype (Typ) /= Full_View (Typ)
21207 then
21208 if Is_Interface (Etype (Typ)) then
21209 Append_Unique_Elmt (Etype (Typ), Ifaces);
21210 end if;
21212 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
21213 end if;
21215 -- Non-private types
21217 else
21218 if Is_Interface (Etype (Typ)) then
21219 Append_Unique_Elmt (Etype (Typ), Ifaces);
21220 end if;
21222 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
21223 end if;
21224 end if;
21226 -- Handle entities in the list of abstract interfaces
21228 if Present (Interfaces (Typ)) then
21229 Iface_Elmt := First_Elmt (Interfaces (Typ));
21230 while Present (Iface_Elmt) loop
21231 Iface := Node (Iface_Elmt);
21233 pragma Assert (Is_Interface (Iface));
21235 if not Contain_Interface (Iface, Ifaces) then
21236 Append_Elmt (Iface, Ifaces);
21237 Collect_Implemented_Interfaces (Iface, Ifaces);
21238 end if;
21240 Next_Elmt (Iface_Elmt);
21241 end loop;
21242 end if;
21243 end Collect_Implemented_Interfaces;
21245 -- Local variables
21247 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
21248 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
21249 -- Save the Ghost-related attributes to restore on exit
21251 Full_Indic : Node_Id;
21252 Full_Parent : Entity_Id;
21253 Priv_Parent : Entity_Id;
21255 -- Start of processing for Process_Full_View
21257 begin
21258 Mark_And_Set_Ghost_Completion (N, Priv_T);
21260 -- First some sanity checks that must be done after semantic
21261 -- decoration of the full view and thus cannot be placed with other
21262 -- similar checks in Find_Type_Name
21264 if not Is_Limited_Type (Priv_T)
21265 and then (Is_Limited_Type (Full_T)
21266 or else Is_Limited_Composite (Full_T))
21267 then
21268 if In_Instance then
21269 null;
21270 else
21271 Error_Msg_N
21272 ("completion of nonlimited type cannot be limited", Full_T);
21273 Explain_Limited_Type (Full_T, Full_T);
21274 end if;
21276 elsif Is_Abstract_Type (Full_T)
21277 and then not Is_Abstract_Type (Priv_T)
21278 then
21279 Error_Msg_N
21280 ("completion of nonabstract type cannot be abstract", Full_T);
21282 elsif Is_Tagged_Type (Priv_T)
21283 and then Is_Limited_Type (Priv_T)
21284 and then not Is_Limited_Type (Full_T)
21285 then
21286 -- If pragma CPP_Class was applied to the private declaration
21287 -- propagate the limitedness to the full-view
21289 if Is_CPP_Class (Priv_T) then
21290 Set_Is_Limited_Record (Full_T);
21292 -- GNAT allow its own definition of Limited_Controlled to disobey
21293 -- this rule in order in ease the implementation. This test is safe
21294 -- because Root_Controlled is defined in a child of System that
21295 -- normal programs are not supposed to use.
21297 elsif Is_RTE (Etype (Full_T), RE_Root_Controlled) then
21298 Set_Is_Limited_Composite (Full_T);
21299 else
21300 Error_Msg_N
21301 ("completion of limited tagged type must be limited", Full_T);
21302 end if;
21304 elsif Is_Generic_Type (Priv_T) then
21305 Error_Msg_N ("generic type cannot have a completion", Full_T);
21306 end if;
21308 -- Check that ancestor interfaces of private and full views are
21309 -- consistent. We omit this check for synchronized types because
21310 -- they are performed on the corresponding record type when frozen.
21312 if Ada_Version >= Ada_2005
21313 and then Is_Tagged_Type (Priv_T)
21314 and then Is_Tagged_Type (Full_T)
21315 and then not Is_Concurrent_Type (Full_T)
21316 then
21317 declare
21318 Iface : Entity_Id;
21319 Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
21320 Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
21322 begin
21323 Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
21324 Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
21326 -- Ada 2005 (AI-251): The partial view shall be a descendant of
21327 -- an interface type if and only if the full type is descendant
21328 -- of the interface type (AARM 7.3 (7.3/2)).
21330 Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
21332 if Present (Iface) then
21333 Error_Msg_NE
21334 ("interface in partial view& not implemented by full type "
21335 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
21336 end if;
21338 Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
21340 if Present (Iface) then
21341 Error_Msg_NE
21342 ("interface & not implemented by partial view "
21343 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
21344 end if;
21345 end;
21346 end if;
21348 if Is_Tagged_Type (Priv_T)
21349 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
21350 and then Is_Derived_Type (Full_T)
21351 then
21352 Priv_Parent := Etype (Priv_T);
21354 -- The full view of a private extension may have been transformed
21355 -- into an unconstrained derived type declaration and a subtype
21356 -- declaration (see build_derived_record_type for details).
21358 if Nkind (N) = N_Subtype_Declaration then
21359 Full_Indic := Subtype_Indication (N);
21360 Full_Parent := Etype (Base_Type (Full_T));
21361 else
21362 Full_Indic := Subtype_Indication (Type_Definition (N));
21363 Full_Parent := Etype (Full_T);
21364 end if;
21366 -- Check that the parent type of the full type is a descendant of
21367 -- the ancestor subtype given in the private extension. If either
21368 -- entity has an Etype equal to Any_Type then we had some previous
21369 -- error situation [7.3(8)].
21371 if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
21372 goto Leave;
21374 -- Ada 2005 (AI-251): Interfaces in the full type can be given in
21375 -- any order. Therefore we don't have to check that its parent must
21376 -- be a descendant of the parent of the private type declaration.
21378 elsif Is_Interface (Priv_Parent)
21379 and then Is_Interface (Full_Parent)
21380 then
21381 null;
21383 -- Ada 2005 (AI-251): If the parent of the private type declaration
21384 -- is an interface there is no need to check that it is an ancestor
21385 -- of the associated full type declaration. The required tests for
21386 -- this case are performed by Build_Derived_Record_Type.
21388 elsif not Is_Interface (Base_Type (Priv_Parent))
21389 and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
21390 then
21391 Error_Msg_N
21392 ("parent of full type must descend from parent of private "
21393 & "extension", Full_Indic);
21395 -- First check a formal restriction, and then proceed with checking
21396 -- Ada rules. Since the formal restriction is not a serious error, we
21397 -- don't prevent further error detection for this check, hence the
21398 -- ELSE.
21400 else
21401 -- Check the rules of 7.3(10): if the private extension inherits
21402 -- known discriminants, then the full type must also inherit those
21403 -- discriminants from the same (ancestor) type, and the parent
21404 -- subtype of the full type must be constrained if and only if
21405 -- the ancestor subtype of the private extension is constrained.
21407 if No (Discriminant_Specifications (Parent (Priv_T)))
21408 and then not Has_Unknown_Discriminants (Priv_T)
21409 and then Has_Discriminants (Base_Type (Priv_Parent))
21410 then
21411 declare
21412 Priv_Indic : constant Node_Id :=
21413 Subtype_Indication (Parent (Priv_T));
21415 Priv_Constr : constant Boolean :=
21416 Is_Constrained (Priv_Parent)
21417 or else
21418 Nkind (Priv_Indic) = N_Subtype_Indication
21419 or else
21420 Is_Constrained (Entity (Priv_Indic));
21422 Full_Constr : constant Boolean :=
21423 Is_Constrained (Full_Parent)
21424 or else
21425 Nkind (Full_Indic) = N_Subtype_Indication
21426 or else
21427 Is_Constrained (Entity (Full_Indic));
21429 Priv_Discr : Entity_Id;
21430 Full_Discr : Entity_Id;
21432 begin
21433 Priv_Discr := First_Discriminant (Priv_Parent);
21434 Full_Discr := First_Discriminant (Full_Parent);
21435 while Present (Priv_Discr) and then Present (Full_Discr) loop
21436 if Original_Record_Component (Priv_Discr) =
21437 Original_Record_Component (Full_Discr)
21438 or else
21439 Corresponding_Discriminant (Priv_Discr) =
21440 Corresponding_Discriminant (Full_Discr)
21441 then
21442 null;
21443 else
21444 exit;
21445 end if;
21447 Next_Discriminant (Priv_Discr);
21448 Next_Discriminant (Full_Discr);
21449 end loop;
21451 if Present (Priv_Discr) or else Present (Full_Discr) then
21452 Error_Msg_N
21453 ("full view must inherit discriminants of the parent "
21454 & "type used in the private extension", Full_Indic);
21456 elsif Priv_Constr and then not Full_Constr then
21457 Error_Msg_N
21458 ("parent subtype of full type must be constrained",
21459 Full_Indic);
21461 elsif Full_Constr and then not Priv_Constr then
21462 Error_Msg_N
21463 ("parent subtype of full type must be unconstrained",
21464 Full_Indic);
21465 end if;
21466 end;
21468 -- Check the rules of 7.3(12): if a partial view has neither
21469 -- known or unknown discriminants, then the full type
21470 -- declaration shall define a definite subtype.
21472 elsif not Has_Unknown_Discriminants (Priv_T)
21473 and then not Has_Discriminants (Priv_T)
21474 and then not Is_Constrained (Full_T)
21475 then
21476 Error_Msg_N
21477 ("full view must define a constrained type if partial view "
21478 & "has no discriminants", Full_T);
21479 end if;
21481 -- Do we implement the following properly???
21482 -- If the ancestor subtype of a private extension has constrained
21483 -- discriminants, then the parent subtype of the full view shall
21484 -- impose a statically matching constraint on those discriminants
21485 -- [7.3(13)].
21486 end if;
21488 else
21489 -- For untagged types, verify that a type without discriminants is
21490 -- not completed with an unconstrained type. A separate error message
21491 -- is produced if the full type has defaulted discriminants.
21493 if Is_Definite_Subtype (Priv_T)
21494 and then not Is_Definite_Subtype (Full_T)
21495 then
21496 Error_Msg_Sloc := Sloc (Parent (Priv_T));
21497 Error_Msg_NE
21498 ("full view of& not compatible with declaration#",
21499 Full_T, Priv_T);
21501 if not Is_Tagged_Type (Full_T) then
21502 Error_Msg_N
21503 ("\one is constrained, the other unconstrained", Full_T);
21504 end if;
21505 end if;
21506 end if;
21508 -- AI-419: verify that the use of "limited" is consistent
21510 declare
21511 Orig_Decl : constant Node_Id := Original_Node (N);
21513 begin
21514 if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
21515 and then Nkind (Orig_Decl) = N_Full_Type_Declaration
21516 and then Nkind
21517 (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
21518 then
21519 if not Limited_Present (Parent (Priv_T))
21520 and then not Synchronized_Present (Parent (Priv_T))
21521 and then Limited_Present (Type_Definition (Orig_Decl))
21522 then
21523 Error_Msg_N
21524 ("full view of non-limited extension cannot be limited", N);
21526 -- Conversely, if the partial view carries the limited keyword,
21527 -- the full view must as well, even if it may be redundant.
21529 elsif Limited_Present (Parent (Priv_T))
21530 and then not Limited_Present (Type_Definition (Orig_Decl))
21531 then
21532 Error_Msg_N
21533 ("full view of limited extension must be explicitly limited",
21535 end if;
21536 end if;
21537 end;
21539 -- Ada 2005 (AI-443): A synchronized private extension must be
21540 -- completed by a task or protected type.
21542 if Ada_Version >= Ada_2005
21543 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
21544 and then Synchronized_Present (Parent (Priv_T))
21545 and then not Is_Concurrent_Type (Full_T)
21546 then
21547 Error_Msg_N ("full view of synchronized extension must " &
21548 "be synchronized type", N);
21549 end if;
21551 -- Ada 2005 AI-363: if the full view has discriminants with
21552 -- defaults, it is illegal to declare constrained access subtypes
21553 -- whose designated type is the current type. This allows objects
21554 -- of the type that are declared in the heap to be unconstrained.
21556 if not Has_Unknown_Discriminants (Priv_T)
21557 and then not Has_Discriminants (Priv_T)
21558 and then Has_Defaulted_Discriminants (Full_T)
21559 then
21560 Set_Has_Constrained_Partial_View (Base_Type (Full_T));
21561 Set_Has_Constrained_Partial_View (Priv_T);
21562 end if;
21564 -- Create a full declaration for all its subtypes recorded in
21565 -- Private_Dependents and swap them similarly to the base type. These
21566 -- are subtypes that have been define before the full declaration of
21567 -- the private type. We also swap the entry in Private_Dependents list
21568 -- so we can properly restore the private view on exit from the scope.
21570 declare
21571 Priv_Elmt : Elmt_Id;
21572 Priv_Scop : Entity_Id;
21573 Priv : Entity_Id;
21574 Full : Entity_Id;
21576 begin
21577 Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
21578 while Present (Priv_Elmt) loop
21579 Priv := Node (Priv_Elmt);
21580 Priv_Scop := Scope (Priv);
21582 if Ekind (Priv) in E_Private_Subtype
21583 | E_Limited_Private_Subtype
21584 | E_Record_Subtype_With_Private
21585 then
21586 Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
21587 Set_Is_Itype (Full);
21588 Set_Parent (Full, Parent (Priv));
21589 Set_Associated_Node_For_Itype (Full, N);
21591 -- Now we need to complete the private subtype, but since the
21592 -- base type has already been swapped, we must also swap the
21593 -- subtypes (and thus, reverse the arguments in the call to
21594 -- Complete_Private_Subtype). Also note that we may need to
21595 -- re-establish the scope of the private subtype.
21597 Copy_And_Swap (Priv, Full);
21599 if not In_Open_Scopes (Priv_Scop) then
21600 Push_Scope (Priv_Scop);
21602 else
21603 -- Reset Priv_Scop to Empty to indicate no scope was pushed
21605 Priv_Scop := Empty;
21606 end if;
21608 Complete_Private_Subtype (Full, Priv, Full_T, N);
21609 Set_Full_View (Full, Priv);
21611 if Present (Priv_Scop) then
21612 Pop_Scope;
21613 end if;
21615 Replace_Elmt (Priv_Elmt, Full);
21616 end if;
21618 Next_Elmt (Priv_Elmt);
21619 end loop;
21620 end;
21622 declare
21623 Disp_Typ : Entity_Id;
21624 Full_List : Elist_Id;
21625 Prim : Entity_Id;
21626 Prim_Elmt : Elmt_Id;
21627 Priv_List : Elist_Id;
21629 function Contains
21630 (E : Entity_Id;
21631 L : Elist_Id) return Boolean;
21632 -- Determine whether list L contains element E
21634 --------------
21635 -- Contains --
21636 --------------
21638 function Contains
21639 (E : Entity_Id;
21640 L : Elist_Id) return Boolean
21642 List_Elmt : Elmt_Id;
21644 begin
21645 List_Elmt := First_Elmt (L);
21646 while Present (List_Elmt) loop
21647 if Node (List_Elmt) = E then
21648 return True;
21649 end if;
21651 Next_Elmt (List_Elmt);
21652 end loop;
21654 return False;
21655 end Contains;
21657 -- Start of processing
21659 begin
21660 -- If the private view was tagged, copy the new primitive operations
21661 -- from the private view to the full view.
21663 if Is_Tagged_Type (Full_T) then
21664 if Is_Tagged_Type (Priv_T) then
21665 Priv_List := Primitive_Operations (Priv_T);
21666 Prim_Elmt := First_Elmt (Priv_List);
21668 -- In the case of a concurrent type completing a private tagged
21669 -- type, primitives may have been declared in between the two
21670 -- views. These subprograms need to be wrapped the same way
21671 -- entries and protected procedures are handled because they
21672 -- cannot be directly shared by the two views.
21674 if Is_Concurrent_Type (Full_T) then
21675 declare
21676 Conc_Typ : constant Entity_Id :=
21677 Corresponding_Record_Type (Full_T);
21678 Curr_Nod : Node_Id := Parent (Conc_Typ);
21679 Wrap_Spec : Node_Id;
21681 begin
21682 while Present (Prim_Elmt) loop
21683 Prim := Node (Prim_Elmt);
21685 if Comes_From_Source (Prim)
21686 and then not Is_Abstract_Subprogram (Prim)
21687 then
21688 Wrap_Spec :=
21689 Make_Subprogram_Declaration (Sloc (Prim),
21690 Specification =>
21691 Build_Wrapper_Spec
21692 (Subp_Id => Prim,
21693 Obj_Typ => Conc_Typ,
21694 Formals =>
21695 Parameter_Specifications
21696 (Parent (Prim))));
21698 Insert_After (Curr_Nod, Wrap_Spec);
21699 Curr_Nod := Wrap_Spec;
21701 Analyze (Wrap_Spec);
21703 -- Remove the wrapper from visibility to avoid
21704 -- spurious conflict with the wrapped entity.
21706 Set_Is_Immediately_Visible
21707 (Defining_Entity (Specification (Wrap_Spec)),
21708 False);
21709 end if;
21711 Next_Elmt (Prim_Elmt);
21712 end loop;
21714 goto Leave;
21715 end;
21717 -- For nonconcurrent types, transfer explicit primitives, but
21718 -- omit those inherited from the parent of the private view
21719 -- since they will be re-inherited later on.
21721 else
21722 Full_List := Primitive_Operations (Full_T);
21723 while Present (Prim_Elmt) loop
21724 Prim := Node (Prim_Elmt);
21726 if Comes_From_Source (Prim)
21727 and then not Contains (Prim, Full_List)
21728 then
21729 Append_Elmt (Prim, Full_List);
21730 end if;
21732 Next_Elmt (Prim_Elmt);
21733 end loop;
21734 end if;
21736 -- Untagged private view
21738 else
21739 Full_List := Primitive_Operations (Full_T);
21741 -- In this case the partial view is untagged, so here we locate
21742 -- all of the earlier primitives that need to be treated as
21743 -- dispatching (those that appear between the two views). Note
21744 -- that these additional operations must all be new operations
21745 -- (any earlier operations that override inherited operations
21746 -- of the full view will already have been inserted in the
21747 -- primitives list, marked by Check_Operation_From_Private_View
21748 -- as dispatching. Note that implicit "/=" operators are
21749 -- excluded from being added to the primitives list since they
21750 -- shouldn't be treated as dispatching (tagged "/=" is handled
21751 -- specially).
21753 Prim := Next_Entity (Full_T);
21754 while Present (Prim) and then Prim /= Priv_T loop
21755 if Ekind (Prim) in E_Procedure | E_Function then
21756 Disp_Typ := Find_Dispatching_Type (Prim);
21758 if Disp_Typ = Full_T
21759 and then (Chars (Prim) /= Name_Op_Ne
21760 or else Comes_From_Source (Prim))
21761 then
21762 Check_Controlling_Formals (Full_T, Prim);
21764 if Is_Suitable_Primitive (Prim)
21765 and then not Is_Dispatching_Operation (Prim)
21766 then
21767 Append_Elmt (Prim, Full_List);
21768 Set_Is_Dispatching_Operation (Prim);
21769 Set_DT_Position_Value (Prim, No_Uint);
21770 end if;
21772 elsif Is_Dispatching_Operation (Prim)
21773 and then Disp_Typ /= Full_T
21774 then
21775 -- Verify that it is not otherwise controlled by a
21776 -- formal or a return value of type T.
21778 Check_Controlling_Formals (Disp_Typ, Prim);
21779 end if;
21780 end if;
21782 Next_Entity (Prim);
21783 end loop;
21784 end if;
21786 -- For the tagged case, the two views can share the same primitive
21787 -- operations list and the same class-wide type. Update attributes
21788 -- of the class-wide type which depend on the full declaration.
21790 if Is_Tagged_Type (Priv_T) then
21791 Set_Direct_Primitive_Operations (Priv_T, Full_List);
21792 Set_Class_Wide_Type
21793 (Base_Type (Full_T), Class_Wide_Type (Priv_T));
21795 Propagate_Concurrent_Flags (Class_Wide_Type (Priv_T), Full_T);
21796 end if;
21798 -- For untagged types, copy the primitives across from the private
21799 -- view to the full view, for support of prefixed calls when
21800 -- extensions are enabled, and better error messages otherwise.
21802 else
21803 Priv_List := Primitive_Operations (Priv_T);
21804 Prim_Elmt := First_Elmt (Priv_List);
21806 Full_List := Primitive_Operations (Full_T);
21807 while Present (Prim_Elmt) loop
21808 Prim := Node (Prim_Elmt);
21809 Append_Elmt (Prim, Full_List);
21810 Next_Elmt (Prim_Elmt);
21811 end loop;
21812 end if;
21813 end;
21815 -- Ada 2005 AI 161: Check preelaborable initialization consistency
21817 if Known_To_Have_Preelab_Init (Priv_T) then
21819 -- Case where there is a pragma Preelaborable_Initialization. We
21820 -- always allow this in predefined units, which is cheating a bit,
21821 -- but it means we don't have to struggle to meet the requirements in
21822 -- the RM for having Preelaborable Initialization. Otherwise we
21823 -- require that the type meets the RM rules. But we can't check that
21824 -- yet, because of the rule about overriding Initialize, so we simply
21825 -- set a flag that will be checked at freeze time.
21827 if not In_Predefined_Unit (Full_T) then
21828 Set_Must_Have_Preelab_Init (Full_T);
21829 end if;
21830 end if;
21832 -- If pragma CPP_Class was applied to the private type declaration,
21833 -- propagate it now to the full type declaration.
21835 if Is_CPP_Class (Priv_T) then
21836 Set_Is_CPP_Class (Full_T);
21837 Set_Convention (Full_T, Convention_CPP);
21839 -- Check that components of imported CPP types do not have default
21840 -- expressions.
21842 Check_CPP_Type_Has_No_Defaults (Full_T);
21843 end if;
21845 -- If the private view has user specified stream attributes, then so has
21846 -- the full view.
21848 -- Why the test, how could these flags be already set in Full_T ???
21850 if Has_Specified_Stream_Read (Priv_T) then
21851 Set_Has_Specified_Stream_Read (Full_T);
21852 end if;
21854 if Has_Specified_Stream_Write (Priv_T) then
21855 Set_Has_Specified_Stream_Write (Full_T);
21856 end if;
21858 if Has_Specified_Stream_Input (Priv_T) then
21859 Set_Has_Specified_Stream_Input (Full_T);
21860 end if;
21862 if Has_Specified_Stream_Output (Priv_T) then
21863 Set_Has_Specified_Stream_Output (Full_T);
21864 end if;
21866 -- Propagate Default_Initial_Condition-related attributes from the
21867 -- partial view to the full view.
21869 Propagate_DIC_Attributes (Full_T, From_Typ => Priv_T);
21871 -- And to the underlying full view, if any
21873 if Is_Private_Type (Full_T)
21874 and then Present (Underlying_Full_View (Full_T))
21875 then
21876 Propagate_DIC_Attributes
21877 (Underlying_Full_View (Full_T), From_Typ => Priv_T);
21878 end if;
21880 -- Propagate invariant-related attributes from the partial view to the
21881 -- full view.
21883 Propagate_Invariant_Attributes (Full_T, From_Typ => Priv_T);
21885 -- And to the underlying full view, if any
21887 if Is_Private_Type (Full_T)
21888 and then Present (Underlying_Full_View (Full_T))
21889 then
21890 Propagate_Invariant_Attributes
21891 (Underlying_Full_View (Full_T), From_Typ => Priv_T);
21892 end if;
21894 -- AI12-0041: Detect an attempt to inherit a class-wide type invariant
21895 -- in the full view without advertising the inheritance in the partial
21896 -- view. This can only occur when the partial view has no parent type
21897 -- and the full view has an interface as a parent. Any other scenarios
21898 -- are illegal because implemented interfaces must match between the
21899 -- two views.
21901 if Is_Tagged_Type (Priv_T) and then Is_Tagged_Type (Full_T) then
21902 declare
21903 Full_Par : constant Entity_Id := Etype (Full_T);
21904 Priv_Par : constant Entity_Id := Etype (Priv_T);
21906 begin
21907 if not Is_Interface (Priv_Par)
21908 and then Is_Interface (Full_Par)
21909 and then Has_Inheritable_Invariants (Full_Par)
21910 then
21911 Error_Msg_N
21912 ("hidden inheritance of class-wide type invariants not "
21913 & "allowed", N);
21914 end if;
21915 end;
21916 end if;
21918 -- Propagate First_Controlling_Parameter aspect to the full type
21920 if Is_Tagged_Type (Priv_T)
21921 and then Has_First_Controlling_Parameter_Aspect (Priv_T)
21922 then
21923 Set_Has_First_Controlling_Parameter_Aspect (Full_T);
21924 end if;
21926 -- Propagate predicates to full type, and predicate function if already
21927 -- defined. It is not clear that this can actually happen? the partial
21928 -- view cannot be frozen yet, and the predicate function has not been
21929 -- built. Still it is a cheap check and seems safer to make it.
21931 Propagate_Predicate_Attributes (Full_T, Priv_T);
21933 if Is_Private_Type (Full_T)
21934 and then Present (Underlying_Full_View (Full_T))
21935 then
21936 Propagate_Predicate_Attributes
21937 (Underlying_Full_View (Full_T), Priv_T);
21938 end if;
21940 <<Leave>>
21941 Restore_Ghost_Region (Saved_GM, Saved_IGR);
21942 end Process_Full_View;
21944 -----------------------------------
21945 -- Process_Incomplete_Dependents --
21946 -----------------------------------
21948 procedure Process_Incomplete_Dependents
21949 (N : Node_Id;
21950 Full_T : Entity_Id;
21951 Inc_T : Entity_Id)
21953 Inc_Elmt : Elmt_Id;
21954 Priv_Dep : Entity_Id;
21955 New_Subt : Entity_Id;
21957 Disc_Constraint : Elist_Id;
21959 begin
21960 if No (Private_Dependents (Inc_T)) then
21961 return;
21962 end if;
21964 -- Itypes that may be generated by the completion of an incomplete
21965 -- subtype are not used by the back-end and not attached to the tree.
21966 -- They are created only for constraint-checking purposes.
21968 Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
21969 while Present (Inc_Elmt) loop
21970 Priv_Dep := Node (Inc_Elmt);
21972 if Ekind (Priv_Dep) = E_Subprogram_Type then
21974 -- An Access_To_Subprogram type may have a return type or a
21975 -- parameter type that is incomplete. Replace with the full view.
21977 if Etype (Priv_Dep) = Inc_T then
21978 Set_Etype (Priv_Dep, Full_T);
21979 end if;
21981 declare
21982 Formal : Entity_Id;
21984 begin
21985 Formal := First_Formal (Priv_Dep);
21986 while Present (Formal) loop
21987 if Etype (Formal) = Inc_T then
21988 Set_Etype (Formal, Full_T);
21989 end if;
21991 Next_Formal (Formal);
21992 end loop;
21993 end;
21995 elsif Is_Overloadable (Priv_Dep) then
21997 -- If a subprogram in the incomplete dependents list is primitive
21998 -- for a tagged full type then mark it as a dispatching operation,
21999 -- check whether it overrides an inherited subprogram, and check
22000 -- restrictions on its controlling formals. Note that a protected
22001 -- operation is never dispatching: only its wrapper operation
22002 -- (which has convention Ada) is.
22004 if Is_Tagged_Type (Full_T)
22005 and then Is_Primitive (Priv_Dep)
22006 and then Convention (Priv_Dep) /= Convention_Protected
22007 then
22008 Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
22009 Set_Is_Dispatching_Operation (Priv_Dep);
22010 Check_Controlling_Formals (Full_T, Priv_Dep);
22011 end if;
22013 elsif Ekind (Priv_Dep) = E_Subprogram_Body then
22015 -- Can happen during processing of a body before the completion
22016 -- of a TA type. Ignore, because spec is also on dependent list.
22018 return;
22020 -- Ada 2005 (AI-412): Transform a regular incomplete subtype into a
22021 -- corresponding subtype of the full view.
22023 elsif Ekind (Priv_Dep) = E_Incomplete_Subtype
22024 and then Comes_From_Source (Priv_Dep)
22025 then
22026 Set_Subtype_Indication
22027 (Parent (Priv_Dep), New_Occurrence_Of (Full_T, Sloc (Priv_Dep)));
22028 Reinit_Field_To_Zero
22029 (Priv_Dep, F_Private_Dependents,
22030 Old_Ekind => E_Incomplete_Subtype);
22031 Mutate_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
22032 Set_Etype (Priv_Dep, Full_T);
22033 Set_Analyzed (Parent (Priv_Dep), False);
22035 -- Reanalyze the declaration, suppressing the call to Enter_Name
22036 -- to avoid duplicate names.
22038 Analyze_Subtype_Declaration
22039 (N => Parent (Priv_Dep),
22040 Skip => True);
22042 -- Dependent is a subtype
22044 else
22045 -- We build a new subtype indication using the full view of the
22046 -- incomplete parent. The discriminant constraints have been
22047 -- elaborated already at the point of the subtype declaration.
22049 New_Subt := Create_Itype (E_Void, N);
22051 if Has_Discriminants (Full_T) then
22052 Disc_Constraint := Discriminant_Constraint (Priv_Dep);
22053 else
22054 Disc_Constraint := No_Elist;
22055 end if;
22057 Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
22058 Set_Full_View (Priv_Dep, New_Subt);
22059 end if;
22061 Next_Elmt (Inc_Elmt);
22062 end loop;
22063 end Process_Incomplete_Dependents;
22065 --------------------------------
22066 -- Process_Range_Expr_In_Decl --
22067 --------------------------------
22069 procedure Process_Range_Expr_In_Decl
22070 (R : Node_Id;
22071 T : Entity_Id;
22072 Subtyp : Entity_Id := Empty;
22073 Check_List : List_Id := No_List)
22075 Lo, Hi : Node_Id;
22076 R_Checks : Check_Result;
22077 Insert_Node : Node_Id;
22078 Def_Id : Entity_Id;
22080 begin
22081 Analyze_And_Resolve (R, Base_Type (T));
22083 if Nkind (R) = N_Range then
22084 Lo := Low_Bound (R);
22085 Hi := High_Bound (R);
22087 -- Validity checks on the range of a quantified expression are
22088 -- delayed until the construct is transformed into a loop.
22090 if Nkind (Parent (R)) = N_Loop_Parameter_Specification
22091 and then Nkind (Parent (Parent (R))) = N_Quantified_Expression
22092 then
22093 null;
22095 -- We need to ensure validity of the bounds here, because if we
22096 -- go ahead and do the expansion, then the expanded code will get
22097 -- analyzed with range checks suppressed and we miss the check.
22099 -- WARNING: The capture of the range bounds with xxx_FIRST/_LAST and
22100 -- the temporaries generated by routine Remove_Side_Effects by means
22101 -- of validity checks must use the same names. When a range appears
22102 -- in the parent of a generic, the range is processed with checks
22103 -- disabled as part of the generic context and with checks enabled
22104 -- for code generation purposes. This leads to link issues as the
22105 -- generic contains references to xxx_FIRST/_LAST, but the inlined
22106 -- template sees the temporaries generated by Remove_Side_Effects.
22108 else
22109 Validity_Check_Range (R, Subtyp);
22110 end if;
22112 -- If there were errors in the declaration, try and patch up some
22113 -- common mistakes in the bounds. The cases handled are literals
22114 -- which are Integer where the expected type is Real and vice versa.
22115 -- These corrections allow the compilation process to proceed further
22116 -- along since some basic assumptions of the format of the bounds
22117 -- are guaranteed.
22119 if Etype (R) = Any_Type then
22120 if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
22121 Rewrite (Lo,
22122 Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
22124 elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
22125 Rewrite (Hi,
22126 Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
22128 elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
22129 Rewrite (Lo,
22130 Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
22132 elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
22133 Rewrite (Hi,
22134 Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
22135 end if;
22137 Set_Etype (Lo, T);
22138 Set_Etype (Hi, T);
22139 end if;
22141 -- If the bounds of the range have been mistakenly given as string
22142 -- literals (perhaps in place of character literals), then an error
22143 -- has already been reported, but we rewrite the string literal as a
22144 -- bound of the range's type to avoid blowups in later processing
22145 -- that looks at static values.
22147 if Nkind (Lo) = N_String_Literal then
22148 Rewrite (Lo,
22149 Make_Attribute_Reference (Sloc (Lo),
22150 Prefix => New_Occurrence_Of (T, Sloc (Lo)),
22151 Attribute_Name => Name_First));
22152 Analyze_And_Resolve (Lo);
22153 end if;
22155 if Nkind (Hi) = N_String_Literal then
22156 Rewrite (Hi,
22157 Make_Attribute_Reference (Sloc (Hi),
22158 Prefix => New_Occurrence_Of (T, Sloc (Hi)),
22159 Attribute_Name => Name_First));
22160 Analyze_And_Resolve (Hi);
22161 end if;
22163 -- If bounds aren't scalar at this point then exit, avoiding
22164 -- problems with further processing of the range in this procedure.
22166 if not Is_Scalar_Type (Etype (Lo)) then
22167 return;
22168 end if;
22170 -- Resolve (actually Sem_Eval) has checked that the bounds are in
22171 -- then range of the base type. Here we check whether the bounds
22172 -- are in the range of the subtype itself. Note that if the bounds
22173 -- represent the null range the Constraint_Error exception should
22174 -- not be raised.
22176 -- Capture values of bounds and generate temporaries for them
22177 -- if needed, before applying checks, since checks may cause
22178 -- duplication of the expression without forcing evaluation.
22180 -- The forced evaluation removes side effects from expressions,
22181 -- which should occur also in GNATprove mode. Otherwise, we end up
22182 -- with unexpected insertions of actions at places where this is
22183 -- not supposed to occur, e.g. on default parameters of a call.
22185 if Expander_Active or GNATprove_Mode then
22187 -- Call Force_Evaluation to create declarations as needed
22188 -- to deal with side effects, and also create typ_FIRST/LAST
22189 -- entities for bounds if we have a subtype name.
22191 -- Note: we do this transformation even if expansion is not
22192 -- active if we are in GNATprove_Mode since the transformation
22193 -- is in general required to ensure that the resulting tree has
22194 -- proper Ada semantics.
22196 Force_Evaluation
22197 (Lo, Related_Id => Subtyp, Is_Low_Bound => True);
22198 Force_Evaluation
22199 (Hi, Related_Id => Subtyp, Is_High_Bound => True);
22200 end if;
22202 -- We use a flag here instead of suppressing checks on the type
22203 -- because the type we check against isn't necessarily the place
22204 -- where we put the check.
22206 R_Checks := Get_Range_Checks (R, T);
22208 -- Look up tree to find an appropriate insertion point. We can't
22209 -- just use insert_actions because later processing depends on
22210 -- the insertion node. Prior to Ada 2012 the insertion point could
22211 -- only be a declaration or a loop, but quantified expressions can
22212 -- appear within any context in an expression, and the insertion
22213 -- point can be any statement, pragma, or declaration.
22215 Insert_Node := Parent (R);
22216 while Present (Insert_Node) loop
22217 exit when
22218 Nkind (Insert_Node) in N_Declaration
22219 and then
22220 Nkind (Insert_Node) not in N_Component_Declaration
22221 | N_Loop_Parameter_Specification
22222 | N_Function_Specification
22223 | N_Procedure_Specification;
22225 exit when Nkind (Insert_Node) in
22226 N_Later_Decl_Item |
22227 N_Statement_Other_Than_Procedure_Call |
22228 N_Procedure_Call_Statement |
22229 N_Pragma;
22231 Insert_Node := Parent (Insert_Node);
22232 end loop;
22234 if Present (Insert_Node) then
22236 -- Case of loop statement. Verify that the range is part of the
22237 -- subtype indication of the iteration scheme.
22239 if Nkind (Insert_Node) = N_Loop_Statement then
22240 declare
22241 Indic : Node_Id;
22243 begin
22244 Indic := Parent (R);
22245 while Present (Indic)
22246 and then Nkind (Indic) /= N_Subtype_Indication
22247 loop
22248 Indic := Parent (Indic);
22249 end loop;
22251 if Present (Indic) then
22252 Def_Id := Etype (Subtype_Mark (Indic));
22254 Insert_Range_Checks
22255 (R_Checks,
22256 Insert_Node,
22257 Def_Id,
22258 Sloc (Insert_Node),
22259 Do_Before => True);
22260 end if;
22261 end;
22263 -- Case of declarations. If the declaration is for a type and
22264 -- involves discriminants, the checks are premature at the
22265 -- declaration point and need to wait for the expansion of the
22266 -- initialization procedure, which will pass in the list to put
22267 -- them on; otherwise, the checks are done at the declaration
22268 -- point and there is no need to do them again in the
22269 -- initialization procedure.
22271 elsif Nkind (Insert_Node) in N_Declaration then
22272 Def_Id := Defining_Identifier (Insert_Node);
22274 if (Ekind (Def_Id) = E_Record_Type
22275 and then Depends_On_Discriminant (R))
22276 or else
22277 (Ekind (Def_Id) = E_Protected_Type
22278 and then Has_Discriminants (Def_Id))
22279 then
22280 if Present (Check_List) then
22281 Append_Range_Checks
22282 (R_Checks,
22283 Check_List, Def_Id, Sloc (Insert_Node));
22284 end if;
22286 else
22287 if No (Check_List) then
22288 Insert_Range_Checks
22289 (R_Checks,
22290 Insert_Node, Def_Id, Sloc (Insert_Node));
22291 end if;
22292 end if;
22294 -- Case of statements. Drop the checks, as the range appears in
22295 -- the context of a quantified expression. Insertion will take
22296 -- place when expression is expanded.
22298 else
22299 null;
22300 end if;
22301 end if;
22303 -- Case of other than an explicit N_Range node
22305 -- The forced evaluation removes side effects from expressions, which
22306 -- should occur also in GNATprove mode. Otherwise, we end up with
22307 -- unexpected insertions of actions at places where this is not
22308 -- supposed to occur, e.g. on default parameters of a call.
22310 elsif Expander_Active or GNATprove_Mode then
22311 Get_Index_Bounds (R, Lo, Hi);
22312 Force_Evaluation (Lo);
22313 Force_Evaluation (Hi);
22314 end if;
22315 end Process_Range_Expr_In_Decl;
22317 --------------------------------------
22318 -- Process_Real_Range_Specification --
22319 --------------------------------------
22321 procedure Process_Real_Range_Specification (Def : Node_Id) is
22322 Spec : constant Node_Id := Real_Range_Specification (Def);
22323 Lo : Node_Id;
22324 Hi : Node_Id;
22325 Err : Boolean := False;
22327 procedure Analyze_Bound (N : Node_Id);
22328 -- Analyze and check one bound
22330 -------------------
22331 -- Analyze_Bound --
22332 -------------------
22334 procedure Analyze_Bound (N : Node_Id) is
22335 begin
22336 Analyze_And_Resolve (N, Any_Real);
22338 if not Is_OK_Static_Expression (N) then
22339 Flag_Non_Static_Expr
22340 ("bound in real type definition is not static!", N);
22341 Err := True;
22342 end if;
22343 end Analyze_Bound;
22345 -- Start of processing for Process_Real_Range_Specification
22347 begin
22348 if Present (Spec) then
22349 Lo := Low_Bound (Spec);
22350 Hi := High_Bound (Spec);
22351 Analyze_Bound (Lo);
22352 Analyze_Bound (Hi);
22354 -- If error, clear away junk range specification
22356 if Err then
22357 Set_Real_Range_Specification (Def, Empty);
22358 end if;
22359 end if;
22360 end Process_Real_Range_Specification;
22362 ---------------------
22363 -- Process_Subtype --
22364 ---------------------
22366 function Process_Subtype
22367 (S : Node_Id;
22368 Related_Nod : Node_Id;
22369 Related_Id : Entity_Id := Empty;
22370 Suffix : Character := ' ') return Entity_Id
22372 procedure Check_Incomplete (T : Node_Id);
22373 -- Called to verify that an incomplete type is not used prematurely
22375 ----------------------
22376 -- Check_Incomplete --
22377 ----------------------
22379 procedure Check_Incomplete (T : Node_Id) is
22380 begin
22381 -- Ada 2005 (AI-412): Incomplete subtypes are legal
22383 if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
22384 and then
22385 not (Ada_Version >= Ada_2005
22386 and then
22387 (Nkind (Parent (T)) = N_Subtype_Declaration
22388 or else (Nkind (Parent (T)) = N_Subtype_Indication
22389 and then Nkind (Parent (Parent (T))) =
22390 N_Subtype_Declaration)))
22391 then
22392 Error_Msg_N ("invalid use of type before its full declaration", T);
22393 end if;
22394 end Check_Incomplete;
22396 -- Local variables
22398 P : Node_Id;
22399 Def_Id : Entity_Id;
22400 Error_Node : Node_Id;
22401 Full_View_Id : Entity_Id;
22402 Subtype_Mark_Id : Entity_Id;
22404 May_Have_Null_Exclusion : Boolean;
22406 -- Start of processing for Process_Subtype
22408 begin
22409 -- Case of no constraints present
22411 if Nkind (S) /= N_Subtype_Indication then
22412 Find_Type (S);
22414 -- No way to proceed if the subtype indication is malformed. This
22415 -- will happen for example when the subtype indication in an object
22416 -- declaration is missing altogether and the expression is analyzed
22417 -- as if it were that indication.
22419 if not Is_Entity_Name (S) then
22420 return Any_Type;
22421 end if;
22423 Check_Incomplete (S);
22424 P := Parent (S);
22426 -- The following mirroring of assertion in Null_Exclusion_Present is
22427 -- ugly, can't we have a range, a static predicate or even a flag???
22429 May_Have_Null_Exclusion :=
22430 Present (P)
22431 and then
22432 Nkind (P) in N_Access_Definition
22433 | N_Access_Function_Definition
22434 | N_Access_Procedure_Definition
22435 | N_Access_To_Object_Definition
22436 | N_Allocator
22437 | N_Component_Definition
22438 | N_Derived_Type_Definition
22439 | N_Discriminant_Specification
22440 | N_Formal_Object_Declaration
22441 | N_Function_Specification
22442 | N_Object_Declaration
22443 | N_Object_Renaming_Declaration
22444 | N_Parameter_Specification
22445 | N_Subtype_Declaration;
22447 -- Ada 2005 (AI-231): Static check
22449 if Ada_Version >= Ada_2005
22450 and then May_Have_Null_Exclusion
22451 and then Null_Exclusion_Present (P)
22452 and then Nkind (P) /= N_Access_To_Object_Definition
22453 and then not Is_Access_Type (Entity (S))
22454 then
22455 Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
22456 end if;
22458 -- Create an Itype that is a duplicate of Entity (S) but with the
22459 -- null-exclusion attribute.
22461 if May_Have_Null_Exclusion
22462 and then Is_Access_Type (Entity (S))
22463 and then Null_Exclusion_Present (P)
22465 -- No need to check the case of an access to object definition.
22466 -- It is correct to define double not-null pointers.
22468 -- Example:
22469 -- type Not_Null_Int_Ptr is not null access Integer;
22470 -- type Acc is not null access Not_Null_Int_Ptr;
22472 and then Nkind (P) /= N_Access_To_Object_Definition
22473 then
22474 if Can_Never_Be_Null (Entity (S)) then
22475 case Nkind (Related_Nod) is
22476 when N_Full_Type_Declaration =>
22477 if Nkind (Type_Definition (Related_Nod))
22478 in N_Array_Type_Definition
22479 then
22480 Error_Node :=
22481 Subtype_Indication
22482 (Component_Definition
22483 (Type_Definition (Related_Nod)));
22484 else
22485 Error_Node :=
22486 Subtype_Indication (Type_Definition (Related_Nod));
22487 end if;
22489 when N_Subtype_Declaration =>
22490 Error_Node := Subtype_Indication (Related_Nod);
22492 when N_Object_Declaration =>
22493 Error_Node := Object_Definition (Related_Nod);
22495 when N_Component_Declaration =>
22496 Error_Node :=
22497 Subtype_Indication (Component_Definition (Related_Nod));
22499 when N_Allocator =>
22500 Error_Node := Expression (Related_Nod);
22502 when others =>
22503 pragma Assert (False);
22504 Error_Node := Related_Nod;
22505 end case;
22507 Error_Msg_NE
22508 ("`NOT NULL` not allowed (& already excludes null)",
22509 Error_Node,
22510 Entity (S));
22511 end if;
22513 Set_Etype (S,
22514 Create_Null_Excluding_Itype
22515 (T => Entity (S),
22516 Related_Nod => P));
22517 Set_Entity (S, Etype (S));
22518 end if;
22520 return Entity (S);
22522 -- Case of constraint present, so that we have an N_Subtype_Indication
22523 -- node (this node is created only if constraints are present).
22525 else
22526 Find_Type (Subtype_Mark (S));
22528 if Nkind (Parent (S)) /= N_Access_To_Object_Definition
22529 and then not
22530 (Nkind (Parent (S)) = N_Subtype_Declaration
22531 and then Is_Itype (Defining_Identifier (Parent (S))))
22532 then
22533 Check_Incomplete (Subtype_Mark (S));
22534 end if;
22536 P := Parent (S);
22537 Subtype_Mark_Id := Entity (Subtype_Mark (S));
22539 -- Explicit subtype declaration case
22541 if Nkind (P) = N_Subtype_Declaration then
22542 Def_Id := Defining_Identifier (P);
22544 -- Explicit derived type definition case
22546 elsif Nkind (P) = N_Derived_Type_Definition then
22547 Def_Id := Defining_Identifier (Parent (P));
22549 -- Implicit case, the Def_Id must be created as an implicit type.
22550 -- The one exception arises in the case of concurrent types, array
22551 -- and access types, where other subsidiary implicit types may be
22552 -- created and must appear before the main implicit type. In these
22553 -- cases we leave Def_Id set to Empty as a signal that Create_Itype
22554 -- has not yet been called to create Def_Id.
22556 else
22557 if Is_Array_Type (Subtype_Mark_Id)
22558 or else Is_Concurrent_Type (Subtype_Mark_Id)
22559 or else Is_Access_Type (Subtype_Mark_Id)
22560 then
22561 Def_Id := Empty;
22563 -- For the other cases, we create a new unattached Itype,
22564 -- and set the indication to ensure it gets attached later.
22566 else
22567 Def_Id :=
22568 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
22569 end if;
22570 end if;
22572 -- If the kind of constraint is invalid for this kind of type,
22573 -- then give an error, and then pretend no constraint was given.
22575 if not Is_Valid_Constraint_Kind
22576 (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
22577 then
22578 Error_Msg_N
22579 ("incorrect constraint for this kind of type", Constraint (S));
22581 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
22583 -- Set Ekind of orphan itype, to prevent cascaded errors
22585 if Present (Def_Id) then
22586 Mutate_Ekind (Def_Id, Ekind (Any_Type));
22587 end if;
22589 -- Make recursive call, having got rid of the bogus constraint
22591 return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
22592 end if;
22594 -- Remaining processing depends on type. Select on Base_Type kind to
22595 -- ensure getting to the concrete type kind in the case of a private
22596 -- subtype (needed when only doing semantic analysis).
22598 case Ekind (Base_Type (Subtype_Mark_Id)) is
22599 when Access_Kind =>
22601 -- If this is a constraint on a class-wide type, discard it.
22602 -- There is currently no way to express a partial discriminant
22603 -- constraint on a type with unknown discriminants. This is
22604 -- a pathology that the ACATS wisely decides not to test.
22606 if Is_Class_Wide_Type (Designated_Type (Subtype_Mark_Id)) then
22607 if Comes_From_Source (S) then
22608 Error_Msg_N
22609 ("constraint on class-wide type ignored??",
22610 Constraint (S));
22611 end if;
22613 if Nkind (P) = N_Subtype_Declaration then
22614 Set_Subtype_Indication (P,
22615 New_Occurrence_Of (Subtype_Mark_Id, Sloc (S)));
22616 end if;
22618 return Subtype_Mark_Id;
22619 end if;
22621 Constrain_Access (Def_Id, S, Related_Nod);
22623 if Expander_Active
22624 and then Is_Itype (Designated_Type (Def_Id))
22625 and then Nkind (Related_Nod) = N_Subtype_Declaration
22626 and then not Is_Incomplete_Type (Designated_Type (Def_Id))
22627 then
22628 Build_Itype_Reference
22629 (Designated_Type (Def_Id), Related_Nod);
22630 end if;
22632 when Array_Kind =>
22633 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
22635 when Decimal_Fixed_Point_Kind =>
22636 Constrain_Decimal (Def_Id, S);
22638 when Enumeration_Kind =>
22639 Constrain_Enumeration (Def_Id, S);
22641 when Ordinary_Fixed_Point_Kind =>
22642 Constrain_Ordinary_Fixed (Def_Id, S);
22644 when Float_Kind =>
22645 Constrain_Float (Def_Id, S);
22647 when Integer_Kind =>
22648 Constrain_Integer (Def_Id, S);
22650 when Class_Wide_Kind
22651 | E_Incomplete_Type
22652 | E_Record_Subtype
22653 | E_Record_Type
22655 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
22657 if Ekind (Def_Id) = E_Incomplete_Type then
22658 Set_Private_Dependents (Def_Id, New_Elmt_List);
22659 end if;
22661 when Private_Kind =>
22663 -- A private type with unknown discriminants may be completed
22664 -- by an unconstrained array type.
22666 if Has_Unknown_Discriminants (Subtype_Mark_Id)
22667 and then Present (Full_View (Subtype_Mark_Id))
22668 and then Is_Array_Type (Full_View (Subtype_Mark_Id))
22669 then
22670 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
22672 -- ... but more commonly is completed by a discriminated record
22673 -- type.
22675 else
22676 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
22677 end if;
22679 -- The base type may be private but Def_Id may be a full view
22680 -- in an instance.
22682 if Is_Private_Type (Def_Id) then
22683 Set_Private_Dependents (Def_Id, New_Elmt_List);
22684 end if;
22686 -- In case of an invalid constraint prevent further processing
22687 -- since the type constructed is missing expected fields.
22689 if Etype (Def_Id) = Any_Type then
22690 return Def_Id;
22691 end if;
22693 -- If the full view is that of a task with discriminants,
22694 -- we must constrain both the concurrent type and its
22695 -- corresponding record type. Otherwise we will just propagate
22696 -- the constraint to the full view, if available.
22698 if Present (Full_View (Subtype_Mark_Id))
22699 and then Has_Discriminants (Subtype_Mark_Id)
22700 and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
22701 then
22702 Full_View_Id :=
22703 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
22705 Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
22706 Constrain_Concurrent (Full_View_Id, S,
22707 Related_Nod, Related_Id, Suffix);
22708 Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
22709 Set_Full_View (Def_Id, Full_View_Id);
22711 -- Introduce an explicit reference to the private subtype,
22712 -- to prevent scope anomalies in gigi if first use appears
22713 -- in a nested context, e.g. a later function body.
22714 -- Should this be generated in other contexts than a full
22715 -- type declaration?
22717 if Is_Itype (Def_Id)
22718 and then
22719 Nkind (Parent (P)) = N_Full_Type_Declaration
22720 then
22721 Build_Itype_Reference (Def_Id, Parent (P));
22722 end if;
22724 else
22725 Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
22726 end if;
22728 when Concurrent_Kind =>
22729 Constrain_Concurrent (Def_Id, S,
22730 Related_Nod, Related_Id, Suffix);
22732 when others =>
22733 Error_Msg_N ("invalid subtype mark in subtype indication", S);
22734 end case;
22736 -- Size, Alignment, Representation aspects and Convention are always
22737 -- inherited from the base type.
22739 Set_Size_Info (Def_Id, (Subtype_Mark_Id));
22740 Set_Rep_Info (Def_Id, (Subtype_Mark_Id));
22741 Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
22743 -- The anonymous subtype created for the subtype indication
22744 -- inherits the predicates of the parent.
22746 if Has_Predicates (Subtype_Mark_Id) then
22747 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
22749 -- Indicate where the predicate function may be found
22751 if No (Predicate_Function (Def_Id)) and then Is_Itype (Def_Id) then
22752 Set_Predicated_Parent (Def_Id, Subtype_Mark_Id);
22753 end if;
22754 end if;
22756 return Def_Id;
22757 end if;
22758 end Process_Subtype;
22760 -----------------------------
22761 -- Record_Type_Declaration --
22762 -----------------------------
22764 procedure Record_Type_Declaration
22765 (T : Entity_Id;
22766 N : Node_Id;
22767 Prev : Entity_Id)
22769 Def : constant Node_Id := Type_Definition (N);
22770 Is_Tagged : Boolean;
22771 Tag_Comp : Entity_Id;
22773 begin
22774 -- These flags must be initialized before calling Process_Discriminants
22775 -- because this routine makes use of them.
22777 Mutate_Ekind (T, E_Record_Type);
22778 Set_Etype (T, T);
22779 Reinit_Size_Align (T);
22780 Set_Interfaces (T, No_Elist);
22781 Set_Stored_Constraint (T, No_Elist);
22782 Set_Default_SSO (T);
22783 Set_No_Reordering (T, No_Component_Reordering);
22785 -- Normal case
22787 if Ada_Version < Ada_2005 or else not Interface_Present (Def) then
22788 -- The flag Is_Tagged_Type might have already been set by
22789 -- Find_Type_Name if it detected an error for declaration T. This
22790 -- arises in the case of private tagged types where the full view
22791 -- omits the word tagged.
22793 Is_Tagged :=
22794 Tagged_Present (Def)
22795 or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
22797 Set_Is_Limited_Record (T, Limited_Present (Def));
22799 if Is_Tagged then
22800 Set_Is_Tagged_Type (T, True);
22801 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
22802 end if;
22804 -- Type is abstract if full declaration carries keyword, or if
22805 -- previous partial view did.
22807 Set_Is_Abstract_Type (T, Is_Abstract_Type (T)
22808 or else Abstract_Present (Def));
22810 else
22811 Is_Tagged := True;
22812 Analyze_Interface_Declaration (T, Def);
22814 if Present (Discriminant_Specifications (N)) then
22815 Error_Msg_N
22816 ("interface types cannot have discriminants",
22817 Defining_Identifier
22818 (First (Discriminant_Specifications (N))));
22819 end if;
22820 end if;
22822 -- First pass: if there are self-referential access components,
22823 -- create the required anonymous access type declarations, and if
22824 -- need be an incomplete type declaration for T itself.
22826 Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
22828 if Ada_Version >= Ada_2005
22829 and then Present (Interface_List (Def))
22830 then
22831 Check_Interfaces (N, Def);
22833 declare
22834 Ifaces_List : Elist_Id;
22836 begin
22837 -- Ada 2005 (AI-251): Collect the list of progenitors that are not
22838 -- already in the parents.
22840 Collect_Interfaces
22841 (T => T,
22842 Ifaces_List => Ifaces_List,
22843 Exclude_Parents => True);
22845 Set_Interfaces (T, Ifaces_List);
22846 end;
22847 end if;
22849 -- Records constitute a scope for the component declarations within.
22850 -- The scope is created prior to the processing of these declarations.
22851 -- Discriminants are processed first, so that they are visible when
22852 -- processing the other components. The Ekind of the record type itself
22853 -- is set to E_Record_Type (subtypes appear as E_Record_Subtype).
22855 -- Enter record scope
22857 Push_Scope (T);
22859 -- If an incomplete or private type declaration was already given for
22860 -- the type, then this scope already exists, and the discriminants have
22861 -- been declared within. We must verify that the full declaration
22862 -- matches the incomplete one.
22864 Check_Or_Process_Discriminants (N, T, Prev);
22866 Set_Is_Constrained (T, not Has_Discriminants (T));
22867 Set_Has_Delayed_Freeze (T, True);
22869 -- For tagged types add a manually analyzed component corresponding
22870 -- to the component _tag, the corresponding piece of tree will be
22871 -- expanded as part of the freezing actions if it is not a CPP_Class.
22873 if Is_Tagged then
22875 -- Do not add the tag unless we are in expansion mode
22877 if Expander_Active then
22878 Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
22879 Enter_Name (Tag_Comp);
22881 Mutate_Ekind (Tag_Comp, E_Component);
22882 Set_Is_Tag (Tag_Comp);
22883 Set_Is_Aliased (Tag_Comp);
22884 Set_Is_Independent (Tag_Comp);
22885 Set_Etype (Tag_Comp, RTE (RE_Tag));
22886 Set_DT_Entry_Count (Tag_Comp, No_Uint);
22887 Set_Original_Record_Component (Tag_Comp, Tag_Comp);
22888 Reinit_Component_Location (Tag_Comp);
22890 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
22891 -- implemented interfaces.
22893 if Has_Interfaces (T) then
22894 Add_Interface_Tag_Components (N, T);
22895 end if;
22896 end if;
22898 Make_Class_Wide_Type (T);
22899 Set_Direct_Primitive_Operations (T, New_Elmt_List);
22900 end if;
22902 -- We must suppress range checks when processing record components in
22903 -- the presence of discriminants, since we don't want spurious checks to
22904 -- be generated during their analysis, but Suppress_Range_Checks flags
22905 -- must be reset the after processing the record definition.
22907 -- Note: this is the only use of Kill_Range_Checks, and is a bit odd,
22908 -- couldn't we just use the normal range check suppression method here.
22909 -- That would seem cleaner ???
22911 if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
22912 Set_Kill_Range_Checks (T, True);
22913 Record_Type_Definition (Def, Prev);
22914 Set_Kill_Range_Checks (T, False);
22915 else
22916 Record_Type_Definition (Def, Prev);
22917 end if;
22919 -- Exit from record scope
22921 End_Scope;
22923 -- Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
22924 -- the implemented interfaces and associate them an aliased entity.
22926 if Is_Tagged
22927 and then not Is_Empty_List (Interface_List (Def))
22928 then
22929 Derive_Progenitor_Subprograms (T, T);
22930 end if;
22932 Warn_On_Inherently_Limited_Type (T);
22934 Check_Function_Writable_Actuals (N);
22935 end Record_Type_Declaration;
22937 ----------------------------
22938 -- Record_Type_Definition --
22939 ----------------------------
22941 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
22942 Component : Entity_Id;
22943 Final_Storage_Only : Boolean := True;
22944 Relaxed_Finalization : Boolean := True;
22945 T : Entity_Id;
22947 begin
22948 if Ekind (Prev_T) = E_Incomplete_Type then
22949 T := Full_View (Prev_T);
22950 else
22951 T := Prev_T;
22952 end if;
22954 Set_Is_Not_Self_Hidden (T);
22956 -- Ada 2005: Check whether an explicit "limited" is present in a derived
22957 -- type declaration.
22959 if Parent_Kind (Def) = N_Derived_Type_Definition
22960 and then Limited_Present (Parent (Def))
22961 then
22962 Set_Is_Limited_Record (T);
22963 end if;
22965 -- If the component list of a record type is defined by the reserved
22966 -- word null and there is no discriminant part, then the record type has
22967 -- no components and all records of the type are null records (RM 3.7)
22968 -- This procedure is also called to process the extension part of a
22969 -- record extension, in which case the current scope may have inherited
22970 -- components.
22972 if Present (Def)
22973 and then Present (Component_List (Def))
22974 and then not Null_Present (Component_List (Def))
22975 then
22976 Analyze_Declarations (Component_Items (Component_List (Def)));
22978 if Present (Variant_Part (Component_List (Def))) then
22979 Analyze (Variant_Part (Component_List (Def)));
22980 end if;
22981 end if;
22983 -- After completing the semantic analysis of the record definition,
22984 -- record components, both new and inherited, are accessible. Set their
22985 -- kind accordingly. Exclude malformed itypes from illegal declarations,
22986 -- whose Ekind may be void.
22988 Component := First_Entity (Current_Scope);
22989 while Present (Component) loop
22990 if Ekind (Component) = E_Void
22991 and then not Is_Itype (Component)
22992 then
22993 Mutate_Ekind (Component, E_Component);
22994 Reinit_Component_Location (Component);
22995 Set_Is_Not_Self_Hidden (Component);
22996 end if;
22998 Propagate_Concurrent_Flags (T, Etype (Component));
23000 if Ekind (Component) /= E_Component then
23001 null;
23003 -- Do not set Has_Controlled_Component on a class-wide equivalent
23004 -- type. See Make_CW_Equivalent_Type.
23006 elsif not Is_Class_Wide_Equivalent_Type (T)
23007 and then (Has_Controlled_Component (Etype (Component))
23008 or else (Chars (Component) /= Name_uParent
23009 and then Is_Controlled (Etype (Component))))
23010 then
23011 Set_Has_Controlled_Component (T);
23012 Final_Storage_Only :=
23013 Final_Storage_Only
23014 and then Finalize_Storage_Only (Etype (Component));
23015 Relaxed_Finalization :=
23016 Relaxed_Finalization
23017 and then Has_Relaxed_Finalization (Etype (Component));
23018 end if;
23020 Next_Entity (Component);
23021 end loop;
23023 -- For a type that is not directly controlled but has controlled
23024 -- components, Finalize_Storage_Only is set if all the controlled
23025 -- components are Finalize_Storage_Only. The same processing is
23026 -- appled to Has_Relaxed_Finalization.
23028 if not Is_Controlled (T) and then Has_Controlled_Component (T) then
23029 Set_Finalize_Storage_Only (T, Final_Storage_Only);
23030 Set_Has_Relaxed_Finalization (T, Relaxed_Finalization);
23031 end if;
23033 -- Place reference to end record on the proper entity, which may
23034 -- be a partial view.
23036 if Present (Def) then
23037 Process_End_Label (Def, 'e', Prev_T);
23038 end if;
23039 end Record_Type_Definition;
23041 ---------------------------
23042 -- Replace_Discriminants --
23043 ---------------------------
23045 procedure Replace_Discriminants (Typ : Entity_Id; Decl : Node_Id) is
23046 function Process (N : Node_Id) return Traverse_Result;
23048 -------------
23049 -- Process --
23050 -------------
23052 function Process (N : Node_Id) return Traverse_Result is
23053 Comp : Entity_Id;
23055 begin
23056 if Nkind (N) = N_Discriminant_Specification then
23057 Comp := First_Discriminant (Typ);
23058 while Present (Comp) loop
23059 if Original_Record_Component (Comp) = Defining_Identifier (N)
23060 or else Chars (Comp) = Chars (Defining_Identifier (N))
23061 then
23062 Set_Defining_Identifier (N, Comp);
23063 exit;
23064 end if;
23066 Next_Discriminant (Comp);
23067 end loop;
23069 elsif Nkind (N) = N_Variant_Part then
23070 Comp := First_Discriminant (Typ);
23071 while Present (Comp) loop
23072 if Original_Record_Component (Comp) = Entity (Name (N))
23073 or else Chars (Comp) = Chars (Name (N))
23074 then
23075 -- Make sure to preserve the type coming from the parent on
23076 -- the Name, even if the subtype of the discriminant can be
23077 -- constrained, so that discrete choices inherited from the
23078 -- parent in the variant part are not flagged as violating
23079 -- the constraints of the subtype.
23081 declare
23082 Typ : constant Entity_Id := Etype (Name (N));
23083 begin
23084 Rewrite (Name (N), New_Occurrence_Of (Comp, Sloc (N)));
23085 Set_Etype (Name (N), Typ);
23086 end;
23087 exit;
23088 end if;
23090 Next_Discriminant (Comp);
23091 end loop;
23092 end if;
23094 return OK;
23095 end Process;
23097 procedure Replace is new Traverse_Proc (Process);
23099 -- Start of processing for Replace_Discriminants
23101 begin
23102 Replace (Decl);
23103 end Replace_Discriminants;
23105 -------------------------------
23106 -- Set_Completion_Referenced --
23107 -------------------------------
23109 procedure Set_Completion_Referenced (E : Entity_Id) is
23110 begin
23111 -- If in main unit, mark entity that is a completion as referenced,
23112 -- warnings go on the partial view when needed.
23114 if In_Extended_Main_Source_Unit (E) then
23115 Set_Referenced (E);
23116 end if;
23117 end Set_Completion_Referenced;
23119 ---------------------
23120 -- Set_Default_SSO --
23121 ---------------------
23123 procedure Set_Default_SSO (T : Entity_Id) is
23124 begin
23125 case Opt.Default_SSO is
23126 when ' ' =>
23127 null;
23128 when 'L' =>
23129 Set_SSO_Set_Low_By_Default (T, True);
23130 when 'H' =>
23131 Set_SSO_Set_High_By_Default (T, True);
23132 when others =>
23133 raise Program_Error;
23134 end case;
23135 end Set_Default_SSO;
23137 ---------------------
23138 -- Set_Fixed_Range --
23139 ---------------------
23141 -- The range for fixed-point types is complicated by the fact that we
23142 -- do not know the exact end points at the time of the declaration. This
23143 -- is true for three reasons:
23145 -- A size clause may affect the fudging of the end-points.
23146 -- A small clause may affect the values of the end-points.
23147 -- We try to include the end-points if it does not affect the size.
23149 -- This means that the actual end-points must be established at the
23150 -- point when the type is frozen. Meanwhile, we first narrow the range
23151 -- as permitted (so that it will fit if necessary in a small specified
23152 -- size), and then build a range subtree with these narrowed bounds.
23153 -- Set_Fixed_Range constructs the range from real literal values, and
23154 -- sets the range as the Scalar_Range of the given fixed-point type entity.
23156 -- The parent of this range is set to point to the entity so that it is
23157 -- properly hooked into the tree (unlike normal Scalar_Range entries for
23158 -- other scalar types, which are just pointers to the range in the
23159 -- original tree, this would otherwise be an orphan).
23161 -- The tree is left unanalyzed. When the type is frozen, the processing
23162 -- in Freeze.Freeze_Fixed_Point_Type notices that the range is not
23163 -- analyzed, and uses this as an indication that it should complete
23164 -- work on the range (it will know the final small and size values).
23166 procedure Set_Fixed_Range
23167 (E : Entity_Id;
23168 Loc : Source_Ptr;
23169 Lo : Ureal;
23170 Hi : Ureal)
23172 S : constant Node_Id :=
23173 Make_Range (Loc,
23174 Low_Bound => Make_Real_Literal (Loc, Lo),
23175 High_Bound => Make_Real_Literal (Loc, Hi));
23176 begin
23177 Set_Scalar_Range (E, S);
23178 Set_Parent (S, E);
23180 -- Before the freeze point, the bounds of a fixed point are universal
23181 -- and carry the corresponding type.
23183 Set_Etype (Low_Bound (S), Universal_Real);
23184 Set_Etype (High_Bound (S), Universal_Real);
23185 end Set_Fixed_Range;
23187 ----------------------------------
23188 -- Set_Scalar_Range_For_Subtype --
23189 ----------------------------------
23191 procedure Set_Scalar_Range_For_Subtype
23192 (Def_Id : Entity_Id;
23193 R : Node_Id;
23194 Subt : Entity_Id)
23196 Kind : constant Entity_Kind := Ekind (Def_Id);
23198 begin
23199 -- Defend against previous error
23201 if Nkind (R) = N_Error then
23202 return;
23203 end if;
23205 Set_Scalar_Range (Def_Id, R);
23207 -- We need to link the range into the tree before resolving it so
23208 -- that types that are referenced, including importantly the subtype
23209 -- itself, are properly frozen (Freeze_Expression requires that the
23210 -- expression be properly linked into the tree). Of course if it is
23211 -- already linked in, then we do not disturb the current link.
23213 if No (Parent (R)) then
23214 Set_Parent (R, Def_Id);
23215 end if;
23217 -- Reset the kind of the subtype during analysis of the range, to
23218 -- catch possible premature use in the bounds themselves.
23220 Process_Range_Expr_In_Decl (R, Subt, Subtyp => Def_Id);
23221 pragma Assert (Ekind (Def_Id) = Kind);
23222 end Set_Scalar_Range_For_Subtype;
23224 --------------------------------------------------------
23225 -- Set_Stored_Constraint_From_Discriminant_Constraint --
23226 --------------------------------------------------------
23228 procedure Set_Stored_Constraint_From_Discriminant_Constraint
23229 (E : Entity_Id)
23231 begin
23232 -- Make sure set if encountered during Expand_To_Stored_Constraint
23234 Set_Stored_Constraint (E, No_Elist);
23236 -- Give it the right value
23238 if Is_Constrained (E) and then Has_Discriminants (E) then
23239 Set_Stored_Constraint (E,
23240 Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
23241 end if;
23242 end Set_Stored_Constraint_From_Discriminant_Constraint;
23244 -------------------------------------
23245 -- Signed_Integer_Type_Declaration --
23246 -------------------------------------
23248 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
23249 Implicit_Base : Entity_Id;
23250 Base_Typ : Entity_Id;
23251 Lo_Val : Uint;
23252 Hi_Val : Uint;
23253 Errs : Boolean := False;
23254 Lo : Node_Id;
23255 Hi : Node_Id;
23257 function Can_Derive_From (E : Entity_Id) return Boolean;
23258 -- Determine whether given bounds allow derivation from specified type
23260 procedure Check_Bound (Expr : Node_Id);
23261 -- Check bound to make sure it is integral and static. If not, post
23262 -- appropriate error message and set Errs flag
23264 ---------------------
23265 -- Can_Derive_From --
23266 ---------------------
23268 -- Note we check both bounds against both end values, to deal with
23269 -- strange types like ones with a range of 0 .. -12341234.
23271 function Can_Derive_From (E : Entity_Id) return Boolean is
23272 Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
23273 Hi : constant Uint := Expr_Value (Type_High_Bound (E));
23274 begin
23275 return Lo <= Lo_Val and then Lo_Val <= Hi
23276 and then
23277 Lo <= Hi_Val and then Hi_Val <= Hi;
23278 end Can_Derive_From;
23280 -----------------
23281 -- Check_Bound --
23282 -----------------
23284 procedure Check_Bound (Expr : Node_Id) is
23285 begin
23286 -- If a range constraint is used as an integer type definition, each
23287 -- bound of the range must be defined by a static expression of some
23288 -- integer type, but the two bounds need not have the same integer
23289 -- type (Negative bounds are allowed.) (RM 3.5.4)
23291 if not Is_Integer_Type (Etype (Expr)) then
23292 Error_Msg_N
23293 ("integer type definition bounds must be of integer type", Expr);
23294 Errs := True;
23296 elsif not Is_OK_Static_Expression (Expr) then
23297 Flag_Non_Static_Expr
23298 ("non-static expression used for integer type bound!", Expr);
23299 Errs := True;
23301 -- Otherwise the bounds are folded into literals
23303 elsif Is_Entity_Name (Expr) then
23304 Fold_Uint (Expr, Expr_Value (Expr), True);
23305 end if;
23306 end Check_Bound;
23308 -- Start of processing for Signed_Integer_Type_Declaration
23310 begin
23311 -- Create an anonymous base type
23313 Implicit_Base :=
23314 Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
23316 -- Analyze and check the bounds, they can be of any integer type
23318 Lo := Low_Bound (Def);
23319 Hi := High_Bound (Def);
23321 -- Arbitrarily use Integer as the type if either bound had an error
23323 if Hi = Error or else Lo = Error then
23324 Base_Typ := Any_Integer;
23325 Set_Error_Posted (T, True);
23326 Errs := True;
23328 -- Here both bounds are OK expressions
23330 else
23331 Analyze_And_Resolve (Lo, Any_Integer);
23332 Analyze_And_Resolve (Hi, Any_Integer);
23334 Check_Bound (Lo);
23335 Check_Bound (Hi);
23337 if Errs then
23338 Hi := Type_High_Bound (Standard_Long_Long_Long_Integer);
23339 Lo := Type_Low_Bound (Standard_Long_Long_Long_Integer);
23340 end if;
23342 -- Find type to derive from
23344 Lo_Val := Expr_Value (Lo);
23345 Hi_Val := Expr_Value (Hi);
23347 if Can_Derive_From (Standard_Short_Short_Integer) then
23348 Base_Typ := Base_Type (Standard_Short_Short_Integer);
23350 elsif Can_Derive_From (Standard_Short_Integer) then
23351 Base_Typ := Base_Type (Standard_Short_Integer);
23353 elsif Can_Derive_From (Standard_Integer) then
23354 Base_Typ := Base_Type (Standard_Integer);
23356 elsif Can_Derive_From (Standard_Long_Integer) then
23357 Base_Typ := Base_Type (Standard_Long_Integer);
23359 elsif Can_Derive_From (Standard_Long_Long_Integer) then
23360 Check_Restriction (No_Long_Long_Integers, Def);
23361 Base_Typ := Base_Type (Standard_Long_Long_Integer);
23363 elsif Can_Derive_From (Standard_Long_Long_Long_Integer) then
23364 Check_Restriction (No_Long_Long_Integers, Def);
23365 Base_Typ := Base_Type (Standard_Long_Long_Long_Integer);
23367 else
23368 Base_Typ := Base_Type (Standard_Long_Long_Long_Integer);
23369 Error_Msg_N ("integer type definition bounds out of range", Def);
23370 Hi := Type_High_Bound (Standard_Long_Long_Long_Integer);
23371 Lo := Type_Low_Bound (Standard_Long_Long_Long_Integer);
23372 end if;
23373 end if;
23375 -- Set the type of the bounds to the implicit base: we cannot set it to
23376 -- the new type, because this would be a forward reference for the code
23377 -- generator and, if the original type is user-defined, this could even
23378 -- lead to spurious semantic errors. Furthermore we do not set it to be
23379 -- universal, because this could make it much larger than needed here.
23381 if not Errs then
23382 Set_Etype (Lo, Implicit_Base);
23383 Set_Etype (Hi, Implicit_Base);
23384 end if;
23386 -- Complete both implicit base and declared first subtype entities. The
23387 -- inheritance of the rep item chain ensures that SPARK-related pragmas
23388 -- are not clobbered when the signed integer type acts as a full view of
23389 -- a private type.
23391 Set_Etype (Implicit_Base, Base_Typ);
23392 Set_Size_Info (Implicit_Base, Base_Typ);
23393 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
23394 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
23395 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
23397 Mutate_Ekind (T, E_Signed_Integer_Subtype);
23398 Set_Etype (T, Implicit_Base);
23399 Set_Size_Info (T, Implicit_Base);
23400 Inherit_Rep_Item_Chain (T, Implicit_Base);
23401 Set_Scalar_Range (T, Def);
23402 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
23403 Set_Is_Constrained (T);
23404 end Signed_Integer_Type_Declaration;
23406 -------------------------------------
23407 -- Warn_On_Inherently_Limited_Type --
23408 -------------------------------------
23410 procedure Warn_On_Inherently_Limited_Type (E : Entity_Id) is
23411 C : Entity_Id;
23412 begin
23413 if Warnsw.Warn_On_Inherently_Limited_Type
23414 and then not Is_Limited_Record (E)
23415 then
23416 C := First_Component (Base_Type (E));
23417 while Present (C) loop
23418 if Is_Inherently_Limited_Type (Etype (C)) then
23419 Error_Msg_Node_2 := E;
23420 Error_Msg_NE
23421 ("?_l?limited component & makes & limited", E, C);
23422 Error_Msg_N
23423 ("\\?_l?consider annotating the record type "
23424 & "with a LIMITED keyword", E);
23425 exit;
23426 end if;
23428 Next_Component (C);
23429 end loop;
23430 end if;
23431 end Warn_On_Inherently_Limited_Type;
23433 end Sem_Ch3;