Implement -mmemcpy-strategy= and -mmemset-strategy= options
[official-gcc.git] / gcc / ada / sem_ch3.adb
blobf07f083463a8ea7ba05f2977a3cd35b9517f2ccd
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-2013, 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 Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Debug; use Debug;
30 with Elists; use Elists;
31 with Einfo; use Einfo;
32 with Errout; use Errout;
33 with Eval_Fat; use Eval_Fat;
34 with Exp_Ch3; use Exp_Ch3;
35 with Exp_Ch9; use Exp_Ch9;
36 with Exp_Disp; use Exp_Disp;
37 with Exp_Dist; use Exp_Dist;
38 with Exp_Pakd; use Exp_Pakd;
39 with Exp_Tss; use Exp_Tss;
40 with Exp_Util; use Exp_Util;
41 with Fname; use Fname;
42 with Freeze; use Freeze;
43 with Itypes; use Itypes;
44 with Layout; use Layout;
45 with Lib; use Lib;
46 with Lib.Xref; use Lib.Xref;
47 with Namet; use Namet;
48 with Nmake; use Nmake;
49 with Opt; use Opt;
50 with Restrict; use Restrict;
51 with Rident; use Rident;
52 with Rtsfind; use Rtsfind;
53 with Sem; use Sem;
54 with Sem_Aux; use Sem_Aux;
55 with Sem_Case; use Sem_Case;
56 with Sem_Cat; use Sem_Cat;
57 with Sem_Ch6; use Sem_Ch6;
58 with Sem_Ch7; use Sem_Ch7;
59 with Sem_Ch8; use Sem_Ch8;
60 with Sem_Ch13; use Sem_Ch13;
61 with Sem_Dim; use Sem_Dim;
62 with Sem_Disp; use Sem_Disp;
63 with Sem_Dist; use Sem_Dist;
64 with Sem_Elim; use Sem_Elim;
65 with Sem_Eval; use Sem_Eval;
66 with Sem_Mech; use Sem_Mech;
67 with Sem_Res; use Sem_Res;
68 with Sem_Smem; use Sem_Smem;
69 with Sem_Type; use Sem_Type;
70 with Sem_Util; use Sem_Util;
71 with Sem_Warn; use Sem_Warn;
72 with Stand; use Stand;
73 with Sinfo; use Sinfo;
74 with Sinput; use Sinput;
75 with Snames; use Snames;
76 with Targparm; use Targparm;
77 with Tbuild; use Tbuild;
78 with Ttypes; use Ttypes;
79 with Uintp; use Uintp;
80 with Urealp; use Urealp;
82 package body Sem_Ch3 is
84 -----------------------
85 -- Local Subprograms --
86 -----------------------
88 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id);
89 -- Ada 2005 (AI-251): Add the tag components corresponding to all the
90 -- abstract interface types implemented by a record type or a derived
91 -- record type.
93 procedure Build_Derived_Type
94 (N : Node_Id;
95 Parent_Type : Entity_Id;
96 Derived_Type : Entity_Id;
97 Is_Completion : Boolean;
98 Derive_Subps : Boolean := True);
99 -- Create and decorate a Derived_Type given the Parent_Type entity. N is
100 -- the N_Full_Type_Declaration node containing the derived type definition.
101 -- Parent_Type is the entity for the parent type in the derived type
102 -- definition and Derived_Type the actual derived type. Is_Completion must
103 -- be set to False if Derived_Type is the N_Defining_Identifier node in N
104 -- (i.e. Derived_Type = Defining_Identifier (N)). In this case N is not the
105 -- completion of a private type declaration. If Is_Completion is set to
106 -- True, N is the completion of a private type declaration and Derived_Type
107 -- is different from the defining identifier inside N (i.e. Derived_Type /=
108 -- Defining_Identifier (N)). Derive_Subps indicates whether the parent
109 -- subprograms should be derived. The only case where this parameter is
110 -- False is when Build_Derived_Type is recursively called to process an
111 -- implicit derived full type for a type derived from a private type (in
112 -- that case the subprograms must only be derived for the private view of
113 -- the type).
115 -- ??? These flags need a bit of re-examination and re-documentation:
116 -- ??? are they both necessary (both seem related to the recursion)?
118 procedure Build_Derived_Access_Type
119 (N : Node_Id;
120 Parent_Type : Entity_Id;
121 Derived_Type : Entity_Id);
122 -- Subsidiary procedure to Build_Derived_Type. For a derived access type,
123 -- create an implicit base if the parent type is constrained or if the
124 -- subtype indication has a constraint.
126 procedure Build_Derived_Array_Type
127 (N : Node_Id;
128 Parent_Type : Entity_Id;
129 Derived_Type : Entity_Id);
130 -- Subsidiary procedure to Build_Derived_Type. For a derived array type,
131 -- create an implicit base if the parent type is constrained or if the
132 -- subtype indication has a constraint.
134 procedure Build_Derived_Concurrent_Type
135 (N : Node_Id;
136 Parent_Type : Entity_Id;
137 Derived_Type : Entity_Id);
138 -- Subsidiary procedure to Build_Derived_Type. For a derived task or
139 -- protected type, inherit entries and protected subprograms, check
140 -- legality of discriminant constraints if any.
142 procedure Build_Derived_Enumeration_Type
143 (N : Node_Id;
144 Parent_Type : Entity_Id;
145 Derived_Type : Entity_Id);
146 -- Subsidiary procedure to Build_Derived_Type. For a derived enumeration
147 -- type, we must create a new list of literals. Types derived from
148 -- Character and [Wide_]Wide_Character are special-cased.
150 procedure Build_Derived_Numeric_Type
151 (N : Node_Id;
152 Parent_Type : Entity_Id;
153 Derived_Type : Entity_Id);
154 -- Subsidiary procedure to Build_Derived_Type. For numeric types, create
155 -- an anonymous base type, and propagate constraint to subtype if needed.
157 procedure Build_Derived_Private_Type
158 (N : Node_Id;
159 Parent_Type : Entity_Id;
160 Derived_Type : Entity_Id;
161 Is_Completion : Boolean;
162 Derive_Subps : Boolean := True);
163 -- Subsidiary procedure to Build_Derived_Type. This procedure is complex
164 -- because the parent may or may not have a completion, and the derivation
165 -- may itself be a completion.
167 procedure Build_Derived_Record_Type
168 (N : Node_Id;
169 Parent_Type : Entity_Id;
170 Derived_Type : Entity_Id;
171 Derive_Subps : Boolean := True);
172 -- Subsidiary procedure for Build_Derived_Type and
173 -- Analyze_Private_Extension_Declaration used for tagged and untagged
174 -- record types. All parameters are as in Build_Derived_Type except that
175 -- N, in addition to being an N_Full_Type_Declaration node, can also be an
176 -- N_Private_Extension_Declaration node. See the definition of this routine
177 -- for much more info. Derive_Subps indicates whether subprograms should
178 -- be derived from the parent type. The only case where Derive_Subps is
179 -- False is for an implicit derived full type for a type derived from a
180 -- private type (see Build_Derived_Type).
182 procedure Build_Discriminal (Discrim : Entity_Id);
183 -- Create the discriminal corresponding to discriminant Discrim, that is
184 -- the parameter corresponding to Discrim to be used in initialization
185 -- procedures for the type where Discrim is a discriminant. Discriminals
186 -- are not used during semantic analysis, and are not fully defined
187 -- entities until expansion. Thus they are not given a scope until
188 -- initialization procedures are built.
190 function Build_Discriminant_Constraints
191 (T : Entity_Id;
192 Def : Node_Id;
193 Derived_Def : Boolean := False) return Elist_Id;
194 -- Validate discriminant constraints and return the list of the constraints
195 -- in order of discriminant declarations, where T is the discriminated
196 -- unconstrained type. Def is the N_Subtype_Indication node where the
197 -- discriminants constraints for T are specified. Derived_Def is True
198 -- when building the discriminant constraints in a derived type definition
199 -- of the form "type D (...) is new T (xxx)". In this case T is the parent
200 -- type and Def is the constraint "(xxx)" on T and this routine sets the
201 -- Corresponding_Discriminant field of the discriminants in the derived
202 -- type D to point to the corresponding discriminants in the parent type T.
204 procedure Build_Discriminated_Subtype
205 (T : Entity_Id;
206 Def_Id : Entity_Id;
207 Elist : Elist_Id;
208 Related_Nod : Node_Id;
209 For_Access : Boolean := False);
210 -- Subsidiary procedure to Constrain_Discriminated_Type and to
211 -- Process_Incomplete_Dependents. Given
213 -- T (a possibly discriminated base type)
214 -- Def_Id (a very partially built subtype for T),
216 -- the call completes Def_Id to be the appropriate E_*_Subtype.
218 -- The Elist is the list of discriminant constraints if any (it is set
219 -- to No_Elist if T is not a discriminated type, and to an empty list if
220 -- T has discriminants but there are no discriminant constraints). The
221 -- Related_Nod is the same as Decl_Node in Create_Constrained_Components.
222 -- The For_Access says whether or not this subtype is really constraining
223 -- an access type. That is its sole purpose is the designated type of an
224 -- access type -- in which case a Private_Subtype Is_For_Access_Subtype
225 -- is built to avoid freezing T when the access subtype is frozen.
227 function Build_Scalar_Bound
228 (Bound : Node_Id;
229 Par_T : Entity_Id;
230 Der_T : Entity_Id) return Node_Id;
231 -- The bounds of a derived scalar type are conversions of the bounds of
232 -- the parent type. Optimize the representation if the bounds are literals.
233 -- Needs a more complete spec--what are the parameters exactly, and what
234 -- exactly is the returned value, and how is Bound affected???
236 procedure Build_Underlying_Full_View
237 (N : Node_Id;
238 Typ : Entity_Id;
239 Par : Entity_Id);
240 -- If the completion of a private type is itself derived from a private
241 -- type, or if the full view of a private subtype is itself private, the
242 -- back-end has no way to compute the actual size of this type. We build
243 -- an internal subtype declaration of the proper parent type to convey
244 -- this information. This extra mechanism is needed because a full
245 -- view cannot itself have a full view (it would get clobbered during
246 -- view exchanges).
248 procedure Check_Access_Discriminant_Requires_Limited
249 (D : Node_Id;
250 Loc : Node_Id);
251 -- Check the restriction that the type to which an access discriminant
252 -- belongs must be a concurrent type or a descendant of a type with
253 -- the reserved word 'limited' in its declaration.
255 procedure Check_Anonymous_Access_Components
256 (Typ_Decl : Node_Id;
257 Typ : Entity_Id;
258 Prev : Entity_Id;
259 Comp_List : Node_Id);
260 -- Ada 2005 AI-382: an access component in a record definition can refer to
261 -- the enclosing record, in which case it denotes the type itself, and not
262 -- the current instance of the type. We create an anonymous access type for
263 -- the component, and flag it as an access to a component, so accessibility
264 -- checks are properly performed on it. The declaration of the access type
265 -- is placed ahead of that of the record to prevent order-of-elaboration
266 -- circularity issues in Gigi. We create an incomplete type for the record
267 -- declaration, which is the designated type of the anonymous access.
269 procedure Check_Delta_Expression (E : Node_Id);
270 -- Check that the expression represented by E is suitable for use as a
271 -- delta expression, i.e. it is of real type and is static.
273 procedure Check_Digits_Expression (E : Node_Id);
274 -- Check that the expression represented by E is suitable for use as a
275 -- digits expression, i.e. it is of integer type, positive and static.
277 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id);
278 -- Validate the initialization of an object declaration. T is the required
279 -- type, and Exp is the initialization expression.
281 procedure Check_Interfaces (N : Node_Id; Def : Node_Id);
282 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
284 procedure Check_Or_Process_Discriminants
285 (N : Node_Id;
286 T : Entity_Id;
287 Prev : Entity_Id := Empty);
288 -- If N is the full declaration of the completion T of an incomplete or
289 -- private type, check its discriminants (which are already known to be
290 -- conformant with those of the partial view, see Find_Type_Name),
291 -- otherwise process them. Prev is the entity of the partial declaration,
292 -- if any.
294 procedure Check_Real_Bound (Bound : Node_Id);
295 -- Check given bound for being of real type and static. If not, post an
296 -- appropriate message, and rewrite the bound with the real literal zero.
298 procedure Constant_Redeclaration
299 (Id : Entity_Id;
300 N : Node_Id;
301 T : out Entity_Id);
302 -- Various checks on legality of full declaration of deferred constant.
303 -- Id is the entity for the redeclaration, N is the N_Object_Declaration,
304 -- node. The caller has not yet set any attributes of this entity.
306 function Contain_Interface
307 (Iface : Entity_Id;
308 Ifaces : Elist_Id) return Boolean;
309 -- Ada 2005: Determine whether Iface is present in the list Ifaces
311 procedure Convert_Scalar_Bounds
312 (N : Node_Id;
313 Parent_Type : Entity_Id;
314 Derived_Type : Entity_Id;
315 Loc : Source_Ptr);
316 -- For derived scalar types, convert the bounds in the type definition to
317 -- the derived type, and complete their analysis. Given a constraint of the
318 -- form ".. new T range Lo .. Hi", Lo and Hi are analyzed and resolved with
319 -- T'Base, the parent_type. The bounds of the derived type (the anonymous
320 -- base) are copies of Lo and Hi. Finally, the bounds of the derived
321 -- subtype are conversions of those bounds to the derived_type, so that
322 -- their typing is consistent.
324 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id);
325 -- Copies attributes from array base type T2 to array base type T1. Copies
326 -- only attributes that apply to base types, but not subtypes.
328 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id);
329 -- Copies attributes from array subtype T2 to array subtype T1. Copies
330 -- attributes that apply to both subtypes and base types.
332 procedure Create_Constrained_Components
333 (Subt : Entity_Id;
334 Decl_Node : Node_Id;
335 Typ : Entity_Id;
336 Constraints : Elist_Id);
337 -- Build the list of entities for a constrained discriminated record
338 -- subtype. If a component depends on a discriminant, replace its subtype
339 -- using the discriminant values in the discriminant constraint. Subt
340 -- is the defining identifier for the subtype whose list of constrained
341 -- entities we will create. Decl_Node is the type declaration node where
342 -- we will attach all the itypes created. Typ is the base discriminated
343 -- type for the subtype Subt. Constraints is the list of discriminant
344 -- constraints for Typ.
346 function Constrain_Component_Type
347 (Comp : Entity_Id;
348 Constrained_Typ : Entity_Id;
349 Related_Node : Node_Id;
350 Typ : Entity_Id;
351 Constraints : Elist_Id) return Entity_Id;
352 -- Given a discriminated base type Typ, a list of discriminant constraint
353 -- Constraints for Typ and a component of Typ, with type Compon_Type,
354 -- create and return the type corresponding to Compon_type where all
355 -- discriminant references are replaced with the corresponding constraint.
356 -- If no discriminant references occur in Compon_Typ then return it as is.
357 -- Constrained_Typ is the final constrained subtype to which the
358 -- constrained Compon_Type belongs. Related_Node is the node where we will
359 -- attach all the itypes created.
361 -- Above description is confused, what is Compon_Type???
363 procedure Constrain_Access
364 (Def_Id : in out Entity_Id;
365 S : Node_Id;
366 Related_Nod : Node_Id);
367 -- Apply a list of constraints to an access type. If Def_Id is empty, it is
368 -- an anonymous type created for a subtype indication. In that case it is
369 -- created in the procedure and attached to Related_Nod.
371 procedure Constrain_Array
372 (Def_Id : in out Entity_Id;
373 SI : Node_Id;
374 Related_Nod : Node_Id;
375 Related_Id : Entity_Id;
376 Suffix : Character);
377 -- Apply a list of index constraints to an unconstrained array type. The
378 -- first parameter is the entity for the resulting subtype. A value of
379 -- Empty for Def_Id indicates that an implicit type must be created, but
380 -- creation is delayed (and must be done by this procedure) because other
381 -- subsidiary implicit types must be created first (which is why Def_Id
382 -- is an in/out parameter). The second parameter is a subtype indication
383 -- node for the constrained array to be created (e.g. something of the
384 -- form string (1 .. 10)). Related_Nod gives the place where this type
385 -- has to be inserted in the tree. The Related_Id and Suffix parameters
386 -- are used to build the associated Implicit type name.
388 procedure Constrain_Concurrent
389 (Def_Id : in out Entity_Id;
390 SI : Node_Id;
391 Related_Nod : Node_Id;
392 Related_Id : Entity_Id;
393 Suffix : Character);
394 -- Apply list of discriminant constraints to an unconstrained concurrent
395 -- type.
397 -- SI is the N_Subtype_Indication node containing the constraint and
398 -- the unconstrained type to constrain.
400 -- Def_Id is the entity for the resulting constrained subtype. A value
401 -- of Empty for Def_Id indicates that an implicit type must be created,
402 -- but creation is delayed (and must be done by this procedure) because
403 -- other subsidiary implicit types must be created first (which is why
404 -- Def_Id is an in/out parameter).
406 -- Related_Nod gives the place where this type has to be inserted
407 -- in the tree
409 -- The last two arguments are used to create its external name if needed.
411 function Constrain_Corresponding_Record
412 (Prot_Subt : Entity_Id;
413 Corr_Rec : Entity_Id;
414 Related_Nod : Node_Id;
415 Related_Id : Entity_Id) return Entity_Id;
416 -- When constraining a protected type or task type with discriminants,
417 -- constrain the corresponding record with the same discriminant values.
419 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id);
420 -- Constrain a decimal fixed point type with a digits constraint and/or a
421 -- range constraint, and build E_Decimal_Fixed_Point_Subtype entity.
423 procedure Constrain_Discriminated_Type
424 (Def_Id : Entity_Id;
425 S : Node_Id;
426 Related_Nod : Node_Id;
427 For_Access : Boolean := False);
428 -- Process discriminant constraints of composite type. Verify that values
429 -- have been provided for all discriminants, that the original type is
430 -- unconstrained, and that the types of the supplied expressions match
431 -- the discriminant types. The first three parameters are like in routine
432 -- Constrain_Concurrent. See Build_Discriminated_Subtype for an explanation
433 -- of For_Access.
435 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id);
436 -- Constrain an enumeration type with a range constraint. This is identical
437 -- to Constrain_Integer, but for the Ekind of the resulting subtype.
439 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id);
440 -- Constrain a floating point type with either a digits constraint
441 -- and/or a range constraint, building a E_Floating_Point_Subtype.
443 procedure Constrain_Index
444 (Index : Node_Id;
445 S : Node_Id;
446 Related_Nod : Node_Id;
447 Related_Id : Entity_Id;
448 Suffix : Character;
449 Suffix_Index : Nat);
450 -- Process an index constraint S in a constrained array declaration. The
451 -- constraint can be a subtype name, or a range with or without an explicit
452 -- subtype mark. The index is the corresponding index of the unconstrained
453 -- array. The Related_Id and Suffix parameters are used to build the
454 -- associated Implicit type name.
456 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id);
457 -- Build subtype of a signed or modular integer type
459 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id);
460 -- Constrain an ordinary fixed point type with a range constraint, and
461 -- build an E_Ordinary_Fixed_Point_Subtype entity.
463 procedure Copy_And_Swap (Priv, Full : Entity_Id);
464 -- Copy the Priv entity into the entity of its full declaration then swap
465 -- the two entities in such a manner that the former private type is now
466 -- seen as a full type.
468 procedure Decimal_Fixed_Point_Type_Declaration
469 (T : Entity_Id;
470 Def : Node_Id);
471 -- Create a new decimal fixed point type, and apply the constraint to
472 -- obtain a subtype of this new type.
474 procedure Complete_Private_Subtype
475 (Priv : Entity_Id;
476 Full : Entity_Id;
477 Full_Base : Entity_Id;
478 Related_Nod : Node_Id);
479 -- Complete the implicit full view of a private subtype by setting the
480 -- appropriate semantic fields. If the full view of the parent is a record
481 -- type, build constrained components of subtype.
483 procedure Derive_Progenitor_Subprograms
484 (Parent_Type : Entity_Id;
485 Tagged_Type : Entity_Id);
486 -- Ada 2005 (AI-251): To complete type derivation, collect the primitive
487 -- operations of progenitors of Tagged_Type, and replace the subsidiary
488 -- subtypes with Tagged_Type, to build the specs of the inherited interface
489 -- primitives. The derived primitives are aliased to those of the
490 -- interface. This routine takes care also of transferring to the full view
491 -- subprograms associated with the partial view of Tagged_Type that cover
492 -- interface primitives.
494 procedure Derived_Standard_Character
495 (N : Node_Id;
496 Parent_Type : Entity_Id;
497 Derived_Type : Entity_Id);
498 -- Subsidiary procedure to Build_Derived_Enumeration_Type which handles
499 -- derivations from types Standard.Character and Standard.Wide_Character.
501 procedure Derived_Type_Declaration
502 (T : Entity_Id;
503 N : Node_Id;
504 Is_Completion : Boolean);
505 -- Process a derived type declaration. Build_Derived_Type is invoked
506 -- to process the actual derived type definition. Parameters N and
507 -- Is_Completion have the same meaning as in Build_Derived_Type.
508 -- T is the N_Defining_Identifier for the entity defined in the
509 -- N_Full_Type_Declaration node N, that is T is the derived type.
511 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id);
512 -- Insert each literal in symbol table, as an overloadable identifier. Each
513 -- enumeration type is mapped into a sequence of integers, and each literal
514 -- is defined as a constant with integer value. If any of the literals are
515 -- character literals, the type is a character type, which means that
516 -- strings are legal aggregates for arrays of components of the type.
518 function Expand_To_Stored_Constraint
519 (Typ : Entity_Id;
520 Constraint : Elist_Id) return Elist_Id;
521 -- Given a constraint (i.e. a list of expressions) on the discriminants of
522 -- Typ, expand it into a constraint on the stored discriminants and return
523 -- the new list of expressions constraining the stored discriminants.
525 function Find_Type_Of_Object
526 (Obj_Def : Node_Id;
527 Related_Nod : Node_Id) return Entity_Id;
528 -- Get type entity for object referenced by Obj_Def, attaching the
529 -- implicit types generated to Related_Nod
531 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id);
532 -- Create a new float and apply the constraint to obtain subtype of it
534 function Has_Range_Constraint (N : Node_Id) return Boolean;
535 -- Given an N_Subtype_Indication node N, return True if a range constraint
536 -- is present, either directly, or as part of a digits or delta constraint.
537 -- In addition, a digits constraint in the decimal case returns True, since
538 -- it establishes a default range if no explicit range is present.
540 function Inherit_Components
541 (N : Node_Id;
542 Parent_Base : Entity_Id;
543 Derived_Base : Entity_Id;
544 Is_Tagged : Boolean;
545 Inherit_Discr : Boolean;
546 Discs : Elist_Id) return Elist_Id;
547 -- Called from Build_Derived_Record_Type to inherit the components of
548 -- Parent_Base (a base type) into the Derived_Base (the derived base type).
549 -- For more information on derived types and component inheritance please
550 -- consult the comment above the body of Build_Derived_Record_Type.
552 -- N is the original derived type declaration
554 -- Is_Tagged is set if we are dealing with tagged types
556 -- If Inherit_Discr is set, Derived_Base inherits its discriminants from
557 -- Parent_Base, otherwise no discriminants are inherited.
559 -- Discs gives the list of constraints that apply to Parent_Base in the
560 -- derived type declaration. If Discs is set to No_Elist, then we have
561 -- the following situation:
563 -- type Parent (D1..Dn : ..) is [tagged] record ...;
564 -- type Derived is new Parent [with ...];
566 -- which gets treated as
568 -- type Derived (D1..Dn : ..) is new Parent (D1,..,Dn) [with ...];
570 -- For untagged types the returned value is an association list. The list
571 -- starts from the association (Parent_Base => Derived_Base), and then it
572 -- contains a sequence of the associations of the form
574 -- (Old_Component => New_Component),
576 -- where Old_Component is the Entity_Id of a component in Parent_Base and
577 -- New_Component is the Entity_Id of the corresponding component in
578 -- Derived_Base. For untagged records, this association list is needed when
579 -- copying the record declaration for the derived base. In the tagged case
580 -- the value returned is irrelevant.
582 function Is_Valid_Constraint_Kind
583 (T_Kind : Type_Kind;
584 Constraint_Kind : Node_Kind) return Boolean;
585 -- Returns True if it is legal to apply the given kind of constraint to the
586 -- given kind of type (index constraint to an array type, for example).
588 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id);
589 -- Create new modular type. Verify that modulus is in bounds
591 procedure New_Concatenation_Op (Typ : Entity_Id);
592 -- Create an abbreviated declaration for an operator in order to
593 -- materialize concatenation on array types.
595 procedure Ordinary_Fixed_Point_Type_Declaration
596 (T : Entity_Id;
597 Def : Node_Id);
598 -- Create a new ordinary fixed point type, and apply the constraint to
599 -- obtain subtype of it.
601 procedure Prepare_Private_Subtype_Completion
602 (Id : Entity_Id;
603 Related_Nod : Node_Id);
604 -- Id is a subtype of some private type. Creates the full declaration
605 -- associated with Id whenever possible, i.e. when the full declaration
606 -- of the base type is already known. Records each subtype into
607 -- Private_Dependents of the base type.
609 procedure Process_Incomplete_Dependents
610 (N : Node_Id;
611 Full_T : Entity_Id;
612 Inc_T : Entity_Id);
613 -- Process all entities that depend on an incomplete type. There include
614 -- subtypes, subprogram types that mention the incomplete type in their
615 -- profiles, and subprogram with access parameters that designate the
616 -- incomplete type.
618 -- Inc_T is the defining identifier of an incomplete type declaration, its
619 -- Ekind is E_Incomplete_Type.
621 -- N is the corresponding N_Full_Type_Declaration for Inc_T.
623 -- Full_T is N's defining identifier.
625 -- Subtypes of incomplete types with discriminants are completed when the
626 -- parent type is. This is simpler than private subtypes, because they can
627 -- only appear in the same scope, and there is no need to exchange views.
628 -- Similarly, access_to_subprogram types may have a parameter or a return
629 -- type that is an incomplete type, and that must be replaced with the
630 -- full type.
632 -- If the full type is tagged, subprogram with access parameters that
633 -- designated the incomplete may be primitive operations of the full type,
634 -- and have to be processed accordingly.
636 procedure Process_Real_Range_Specification (Def : Node_Id);
637 -- Given the type definition for a real type, this procedure processes and
638 -- checks the real range specification of this type definition if one is
639 -- present. If errors are found, error messages are posted, and the
640 -- Real_Range_Specification of Def is reset to Empty.
642 procedure Record_Type_Declaration
643 (T : Entity_Id;
644 N : Node_Id;
645 Prev : Entity_Id);
646 -- Process a record type declaration (for both untagged and tagged
647 -- records). Parameters T and N are exactly like in procedure
648 -- Derived_Type_Declaration, except that no flag Is_Completion is needed
649 -- for this routine. If this is the completion of an incomplete type
650 -- declaration, Prev is the entity of the incomplete declaration, used for
651 -- cross-referencing. Otherwise Prev = T.
653 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id);
654 -- This routine is used to process the actual record type definition (both
655 -- for untagged and tagged records). Def is a record type definition node.
656 -- This procedure analyzes the components in this record type definition.
657 -- Prev_T is the entity for the enclosing record type. It is provided so
658 -- that its Has_Task flag can be set if any of the component have Has_Task
659 -- set. If the declaration is the completion of an incomplete type
660 -- declaration, Prev_T is the original incomplete type, whose full view is
661 -- the record type.
663 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id);
664 -- Subsidiary to Build_Derived_Record_Type. For untagged records, we
665 -- build a copy of the declaration tree of the parent, and we create
666 -- independently the list of components for the derived type. Semantic
667 -- information uses the component entities, but record representation
668 -- clauses are validated on the declaration tree. This procedure replaces
669 -- discriminants and components in the declaration with those that have
670 -- been created by Inherit_Components.
672 procedure Set_Fixed_Range
673 (E : Entity_Id;
674 Loc : Source_Ptr;
675 Lo : Ureal;
676 Hi : Ureal);
677 -- Build a range node with the given bounds and set it as the Scalar_Range
678 -- of the given fixed-point type entity. Loc is the source location used
679 -- for the constructed range. See body for further details.
681 procedure Set_Scalar_Range_For_Subtype
682 (Def_Id : Entity_Id;
683 R : Node_Id;
684 Subt : Entity_Id);
685 -- This routine is used to set the scalar range field for a subtype given
686 -- Def_Id, the entity for the subtype, and R, the range expression for the
687 -- scalar range. Subt provides the parent subtype to be used to analyze,
688 -- resolve, and check the given range.
690 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id);
691 -- Create a new signed integer entity, and apply the constraint to obtain
692 -- the required first named subtype of this type.
694 procedure Set_Stored_Constraint_From_Discriminant_Constraint
695 (E : Entity_Id);
696 -- E is some record type. This routine computes E's Stored_Constraint
697 -- from its Discriminant_Constraint.
699 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id);
700 -- Check that an entity in a list of progenitors is an interface,
701 -- emit error otherwise.
703 -----------------------
704 -- Access_Definition --
705 -----------------------
707 function Access_Definition
708 (Related_Nod : Node_Id;
709 N : Node_Id) return Entity_Id
711 Anon_Type : Entity_Id;
712 Anon_Scope : Entity_Id;
713 Desig_Type : Entity_Id;
714 Enclosing_Prot_Type : Entity_Id := Empty;
716 begin
717 Check_SPARK_Restriction ("access type is not allowed", N);
719 if Is_Entry (Current_Scope)
720 and then Is_Task_Type (Etype (Scope (Current_Scope)))
721 then
722 Error_Msg_N ("task entries cannot have access parameters", N);
723 return Empty;
724 end if;
726 -- Ada 2005: for an object declaration the corresponding anonymous
727 -- type is declared in the current scope.
729 -- If the access definition is the return type of another access to
730 -- function, scope is the current one, because it is the one of the
731 -- current type declaration, except for the pathological case below.
733 if Nkind_In (Related_Nod, N_Object_Declaration,
734 N_Access_Function_Definition)
735 then
736 Anon_Scope := Current_Scope;
738 -- A pathological case: function returning access functions that
739 -- return access functions, etc. Each anonymous access type created
740 -- is in the enclosing scope of the outermost function.
742 declare
743 Par : Node_Id;
745 begin
746 Par := Related_Nod;
747 while Nkind_In (Par, N_Access_Function_Definition,
748 N_Access_Definition)
749 loop
750 Par := Parent (Par);
751 end loop;
753 if Nkind (Par) = N_Function_Specification then
754 Anon_Scope := Scope (Defining_Entity (Par));
755 end if;
756 end;
758 -- For the anonymous function result case, retrieve the scope of the
759 -- function specification's associated entity rather than using the
760 -- current scope. The current scope will be the function itself if the
761 -- formal part is currently being analyzed, but will be the parent scope
762 -- in the case of a parameterless function, and we always want to use
763 -- the function's parent scope. Finally, if the function is a child
764 -- unit, we must traverse the tree to retrieve the proper entity.
766 elsif Nkind (Related_Nod) = N_Function_Specification
767 and then Nkind (Parent (N)) /= N_Parameter_Specification
768 then
769 -- If the current scope is a protected type, the anonymous access
770 -- is associated with one of the protected operations, and must
771 -- be available in the scope that encloses the protected declaration.
772 -- Otherwise the type is in the scope enclosing the subprogram.
774 -- If the function has formals, The return type of a subprogram
775 -- declaration is analyzed in the scope of the subprogram (see
776 -- Process_Formals) and thus the protected type, if present, is
777 -- the scope of the current function scope.
779 if Ekind (Current_Scope) = E_Protected_Type then
780 Enclosing_Prot_Type := Current_Scope;
782 elsif Ekind (Current_Scope) = E_Function
783 and then Ekind (Scope (Current_Scope)) = E_Protected_Type
784 then
785 Enclosing_Prot_Type := Scope (Current_Scope);
786 end if;
788 if Present (Enclosing_Prot_Type) then
789 Anon_Scope := Scope (Enclosing_Prot_Type);
791 else
792 Anon_Scope := Scope (Defining_Entity (Related_Nod));
793 end if;
795 -- For an access type definition, if the current scope is a child
796 -- unit it is the scope of the type.
798 elsif Is_Compilation_Unit (Current_Scope) then
799 Anon_Scope := Current_Scope;
801 -- For access formals, access components, and access discriminants, the
802 -- scope is that of the enclosing declaration,
804 else
805 Anon_Scope := Scope (Current_Scope);
806 end if;
808 Anon_Type :=
809 Create_Itype
810 (E_Anonymous_Access_Type, Related_Nod, Scope_Id => Anon_Scope);
812 if All_Present (N)
813 and then Ada_Version >= Ada_2005
814 then
815 Error_Msg_N ("ALL is not permitted for anonymous access types", N);
816 end if;
818 -- Ada 2005 (AI-254): In case of anonymous access to subprograms call
819 -- the corresponding semantic routine
821 if Present (Access_To_Subprogram_Definition (N)) then
823 -- Compiler runtime units are compiled in Ada 2005 mode when building
824 -- the runtime library but must also be compilable in Ada 95 mode
825 -- (when bootstrapping the compiler).
827 Check_Compiler_Unit (N);
829 Access_Subprogram_Declaration
830 (T_Name => Anon_Type,
831 T_Def => Access_To_Subprogram_Definition (N));
833 if Ekind (Anon_Type) = E_Access_Protected_Subprogram_Type then
834 Set_Ekind
835 (Anon_Type, E_Anonymous_Access_Protected_Subprogram_Type);
836 else
837 Set_Ekind
838 (Anon_Type, E_Anonymous_Access_Subprogram_Type);
839 end if;
841 Set_Can_Use_Internal_Rep
842 (Anon_Type, not Always_Compatible_Rep_On_Target);
844 -- If the anonymous access is associated with a protected operation,
845 -- create a reference to it after the enclosing protected definition
846 -- because the itype will be used in the subsequent bodies.
848 if Ekind (Current_Scope) = E_Protected_Type then
849 Build_Itype_Reference (Anon_Type, Parent (Current_Scope));
850 end if;
852 return Anon_Type;
853 end if;
855 Find_Type (Subtype_Mark (N));
856 Desig_Type := Entity (Subtype_Mark (N));
858 Set_Directly_Designated_Type (Anon_Type, Desig_Type);
859 Set_Etype (Anon_Type, Anon_Type);
861 -- Make sure the anonymous access type has size and alignment fields
862 -- set, as required by gigi. This is necessary in the case of the
863 -- Task_Body_Procedure.
865 if not Has_Private_Component (Desig_Type) then
866 Layout_Type (Anon_Type);
867 end if;
869 -- Ada 2005 (AI-231): Ada 2005 semantics for anonymous access differs
870 -- from Ada 95 semantics. In Ada 2005, anonymous access must specify if
871 -- the null value is allowed. In Ada 95 the null value is never allowed.
873 if Ada_Version >= Ada_2005 then
874 Set_Can_Never_Be_Null (Anon_Type, Null_Exclusion_Present (N));
875 else
876 Set_Can_Never_Be_Null (Anon_Type, True);
877 end if;
879 -- The anonymous access type is as public as the discriminated type or
880 -- subprogram that defines it. It is imported (for back-end purposes)
881 -- if the designated type is.
883 Set_Is_Public (Anon_Type, Is_Public (Scope (Anon_Type)));
885 -- Ada 2005 (AI-231): Propagate the access-constant attribute
887 Set_Is_Access_Constant (Anon_Type, Constant_Present (N));
889 -- The context is either a subprogram declaration, object declaration,
890 -- or an access discriminant, in a private or a full type declaration.
891 -- In the case of a subprogram, if the designated type is incomplete,
892 -- the operation will be a primitive operation of the full type, to be
893 -- updated subsequently. If the type is imported through a limited_with
894 -- clause, the subprogram is not a primitive operation of the type
895 -- (which is declared elsewhere in some other scope).
897 if Ekind (Desig_Type) = E_Incomplete_Type
898 and then not From_With_Type (Desig_Type)
899 and then Is_Overloadable (Current_Scope)
900 then
901 Append_Elmt (Current_Scope, Private_Dependents (Desig_Type));
902 Set_Has_Delayed_Freeze (Current_Scope);
903 end if;
905 -- Ada 2005: if the designated type is an interface that may contain
906 -- tasks, create a Master entity for the declaration. This must be done
907 -- before expansion of the full declaration, because the declaration may
908 -- include an expression that is an allocator, whose expansion needs the
909 -- proper Master for the created tasks.
911 if Nkind (Related_Nod) = N_Object_Declaration
912 and then Expander_Active
913 then
914 if Is_Interface (Desig_Type)
915 and then Is_Limited_Record (Desig_Type)
916 then
917 Build_Class_Wide_Master (Anon_Type);
919 -- Similarly, if the type is an anonymous access that designates
920 -- tasks, create a master entity for it in the current context.
922 elsif Has_Task (Desig_Type)
923 and then Comes_From_Source (Related_Nod)
924 then
925 Build_Master_Entity (Defining_Identifier (Related_Nod));
926 Build_Master_Renaming (Anon_Type);
927 end if;
928 end if;
930 -- For a private component of a protected type, it is imperative that
931 -- the back-end elaborate the type immediately after the protected
932 -- declaration, because this type will be used in the declarations
933 -- created for the component within each protected body, so we must
934 -- create an itype reference for it now.
936 if Nkind (Parent (Related_Nod)) = N_Protected_Definition then
937 Build_Itype_Reference (Anon_Type, Parent (Parent (Related_Nod)));
939 -- Similarly, if the access definition is the return result of a
940 -- function, create an itype reference for it because it will be used
941 -- within the function body. For a regular function that is not a
942 -- compilation unit, insert reference after the declaration. For a
943 -- protected operation, insert it after the enclosing protected type
944 -- declaration. In either case, do not create a reference for a type
945 -- obtained through a limited_with clause, because this would introduce
946 -- semantic dependencies.
948 -- Similarly, do not create a reference if the designated type is a
949 -- generic formal, because no use of it will reach the backend.
951 elsif Nkind (Related_Nod) = N_Function_Specification
952 and then not From_With_Type (Desig_Type)
953 and then not Is_Generic_Type (Desig_Type)
954 then
955 if Present (Enclosing_Prot_Type) then
956 Build_Itype_Reference (Anon_Type, Parent (Enclosing_Prot_Type));
958 elsif Is_List_Member (Parent (Related_Nod))
959 and then Nkind (Parent (N)) /= N_Parameter_Specification
960 then
961 Build_Itype_Reference (Anon_Type, Parent (Related_Nod));
962 end if;
964 -- Finally, create an itype reference for an object declaration of an
965 -- anonymous access type. This is strictly necessary only for deferred
966 -- constants, but in any case will avoid out-of-scope problems in the
967 -- back-end.
969 elsif Nkind (Related_Nod) = N_Object_Declaration then
970 Build_Itype_Reference (Anon_Type, Related_Nod);
971 end if;
973 return Anon_Type;
974 end Access_Definition;
976 -----------------------------------
977 -- Access_Subprogram_Declaration --
978 -----------------------------------
980 procedure Access_Subprogram_Declaration
981 (T_Name : Entity_Id;
982 T_Def : Node_Id)
985 procedure Check_For_Premature_Usage (Def : Node_Id);
986 -- Check that type T_Name is not used, directly or recursively, as a
987 -- parameter or a return type in Def. Def is either a subtype, an
988 -- access_definition, or an access_to_subprogram_definition.
990 -------------------------------
991 -- Check_For_Premature_Usage --
992 -------------------------------
994 procedure Check_For_Premature_Usage (Def : Node_Id) is
995 Param : Node_Id;
997 begin
998 -- Check for a subtype mark
1000 if Nkind (Def) in N_Has_Etype then
1001 if Etype (Def) = T_Name then
1002 Error_Msg_N
1003 ("type& cannot be used before end of its declaration", Def);
1004 end if;
1006 -- If this is not a subtype, then this is an access_definition
1008 elsif Nkind (Def) = N_Access_Definition then
1009 if Present (Access_To_Subprogram_Definition (Def)) then
1010 Check_For_Premature_Usage
1011 (Access_To_Subprogram_Definition (Def));
1012 else
1013 Check_For_Premature_Usage (Subtype_Mark (Def));
1014 end if;
1016 -- The only cases left are N_Access_Function_Definition and
1017 -- N_Access_Procedure_Definition.
1019 else
1020 if Present (Parameter_Specifications (Def)) then
1021 Param := First (Parameter_Specifications (Def));
1022 while Present (Param) loop
1023 Check_For_Premature_Usage (Parameter_Type (Param));
1024 Param := Next (Param);
1025 end loop;
1026 end if;
1028 if Nkind (Def) = N_Access_Function_Definition then
1029 Check_For_Premature_Usage (Result_Definition (Def));
1030 end if;
1031 end if;
1032 end Check_For_Premature_Usage;
1034 -- Local variables
1036 Formals : constant List_Id := Parameter_Specifications (T_Def);
1037 Formal : Entity_Id;
1038 D_Ityp : Node_Id;
1039 Desig_Type : constant Entity_Id :=
1040 Create_Itype (E_Subprogram_Type, Parent (T_Def));
1042 -- Start of processing for Access_Subprogram_Declaration
1044 begin
1045 Check_SPARK_Restriction ("access type is not allowed", T_Def);
1047 -- Associate the Itype node with the inner full-type declaration or
1048 -- subprogram spec or entry body. This is required to handle nested
1049 -- anonymous declarations. For example:
1051 -- procedure P
1052 -- (X : access procedure
1053 -- (Y : access procedure
1054 -- (Z : access T)))
1056 D_Ityp := Associated_Node_For_Itype (Desig_Type);
1057 while not (Nkind_In (D_Ityp, N_Full_Type_Declaration,
1058 N_Private_Type_Declaration,
1059 N_Private_Extension_Declaration,
1060 N_Procedure_Specification,
1061 N_Function_Specification,
1062 N_Entry_Body)
1064 or else
1065 Nkind_In (D_Ityp, N_Object_Declaration,
1066 N_Object_Renaming_Declaration,
1067 N_Formal_Object_Declaration,
1068 N_Formal_Type_Declaration,
1069 N_Task_Type_Declaration,
1070 N_Protected_Type_Declaration))
1071 loop
1072 D_Ityp := Parent (D_Ityp);
1073 pragma Assert (D_Ityp /= Empty);
1074 end loop;
1076 Set_Associated_Node_For_Itype (Desig_Type, D_Ityp);
1078 if Nkind_In (D_Ityp, N_Procedure_Specification,
1079 N_Function_Specification)
1080 then
1081 Set_Scope (Desig_Type, Scope (Defining_Entity (D_Ityp)));
1083 elsif Nkind_In (D_Ityp, N_Full_Type_Declaration,
1084 N_Object_Declaration,
1085 N_Object_Renaming_Declaration,
1086 N_Formal_Type_Declaration)
1087 then
1088 Set_Scope (Desig_Type, Scope (Defining_Identifier (D_Ityp)));
1089 end if;
1091 if Nkind (T_Def) = N_Access_Function_Definition then
1092 if Nkind (Result_Definition (T_Def)) = N_Access_Definition then
1093 declare
1094 Acc : constant Node_Id := Result_Definition (T_Def);
1096 begin
1097 if Present (Access_To_Subprogram_Definition (Acc))
1098 and then
1099 Protected_Present (Access_To_Subprogram_Definition (Acc))
1100 then
1101 Set_Etype
1102 (Desig_Type,
1103 Replace_Anonymous_Access_To_Protected_Subprogram
1104 (T_Def));
1106 else
1107 Set_Etype
1108 (Desig_Type,
1109 Access_Definition (T_Def, Result_Definition (T_Def)));
1110 end if;
1111 end;
1113 else
1114 Analyze (Result_Definition (T_Def));
1116 declare
1117 Typ : constant Entity_Id := Entity (Result_Definition (T_Def));
1119 begin
1120 -- If a null exclusion is imposed on the result type, then
1121 -- create a null-excluding itype (an access subtype) and use
1122 -- it as the function's Etype.
1124 if Is_Access_Type (Typ)
1125 and then Null_Exclusion_In_Return_Present (T_Def)
1126 then
1127 Set_Etype (Desig_Type,
1128 Create_Null_Excluding_Itype
1129 (T => Typ,
1130 Related_Nod => T_Def,
1131 Scope_Id => Current_Scope));
1133 else
1134 if From_With_Type (Typ) then
1136 -- AI05-151: Incomplete types are allowed in all basic
1137 -- declarations, including access to subprograms.
1139 if Ada_Version >= Ada_2012 then
1140 null;
1142 else
1143 Error_Msg_NE
1144 ("illegal use of incomplete type&",
1145 Result_Definition (T_Def), Typ);
1146 end if;
1148 elsif Ekind (Current_Scope) = E_Package
1149 and then In_Private_Part (Current_Scope)
1150 then
1151 if Ekind (Typ) = E_Incomplete_Type then
1152 Append_Elmt (Desig_Type, Private_Dependents (Typ));
1154 elsif Is_Class_Wide_Type (Typ)
1155 and then Ekind (Etype (Typ)) = E_Incomplete_Type
1156 then
1157 Append_Elmt
1158 (Desig_Type, Private_Dependents (Etype (Typ)));
1159 end if;
1160 end if;
1162 Set_Etype (Desig_Type, Typ);
1163 end if;
1164 end;
1165 end if;
1167 if not (Is_Type (Etype (Desig_Type))) then
1168 Error_Msg_N
1169 ("expect type in function specification",
1170 Result_Definition (T_Def));
1171 end if;
1173 else
1174 Set_Etype (Desig_Type, Standard_Void_Type);
1175 end if;
1177 if Present (Formals) then
1178 Push_Scope (Desig_Type);
1180 -- A bit of a kludge here. These kludges will be removed when Itypes
1181 -- have proper parent pointers to their declarations???
1183 -- Kludge 1) Link defining_identifier of formals. Required by
1184 -- First_Formal to provide its functionality.
1186 declare
1187 F : Node_Id;
1189 begin
1190 F := First (Formals);
1192 -- In ASIS mode, the access_to_subprogram may be analyzed twice,
1193 -- when it is part of an unconstrained type and subtype expansion
1194 -- is disabled. To avoid back-end problems with shared profiles,
1195 -- use previous subprogram type as the designated type, and then
1196 -- remove scope added above.
1198 if ASIS_Mode
1199 and then Present (Scope (Defining_Identifier (F)))
1200 then
1201 Set_Etype (T_Name, T_Name);
1202 Init_Size_Align (T_Name);
1203 Set_Directly_Designated_Type (T_Name,
1204 Scope (Defining_Identifier (F)));
1205 End_Scope;
1206 return;
1207 end if;
1209 while Present (F) loop
1210 if No (Parent (Defining_Identifier (F))) then
1211 Set_Parent (Defining_Identifier (F), F);
1212 end if;
1214 Next (F);
1215 end loop;
1216 end;
1218 Process_Formals (Formals, Parent (T_Def));
1220 -- Kludge 2) End_Scope requires that the parent pointer be set to
1221 -- something reasonable, but Itypes don't have parent pointers. So
1222 -- we set it and then unset it ???
1224 Set_Parent (Desig_Type, T_Name);
1225 End_Scope;
1226 Set_Parent (Desig_Type, Empty);
1227 end if;
1229 -- Check for premature usage of the type being defined
1231 Check_For_Premature_Usage (T_Def);
1233 -- The return type and/or any parameter type may be incomplete. Mark the
1234 -- subprogram_type as depending on the incomplete type, so that it can
1235 -- be updated when the full type declaration is seen. This only applies
1236 -- to incomplete types declared in some enclosing scope, not to limited
1237 -- views from other packages.
1239 if Present (Formals) then
1240 Formal := First_Formal (Desig_Type);
1241 while Present (Formal) loop
1242 if Ekind (Formal) /= E_In_Parameter
1243 and then Nkind (T_Def) = N_Access_Function_Definition
1244 then
1245 Error_Msg_N ("functions can only have IN parameters", Formal);
1246 end if;
1248 if Ekind (Etype (Formal)) = E_Incomplete_Type
1249 and then In_Open_Scopes (Scope (Etype (Formal)))
1250 then
1251 Append_Elmt (Desig_Type, Private_Dependents (Etype (Formal)));
1252 Set_Has_Delayed_Freeze (Desig_Type);
1253 end if;
1255 Next_Formal (Formal);
1256 end loop;
1257 end if;
1259 -- If the return type is incomplete, this is legal as long as the type
1260 -- is declared in the current scope and will be completed in it (rather
1261 -- than being part of limited view).
1263 if Ekind (Etype (Desig_Type)) = E_Incomplete_Type
1264 and then not Has_Delayed_Freeze (Desig_Type)
1265 and then In_Open_Scopes (Scope (Etype (Desig_Type)))
1266 then
1267 Append_Elmt (Desig_Type, Private_Dependents (Etype (Desig_Type)));
1268 Set_Has_Delayed_Freeze (Desig_Type);
1269 end if;
1271 Check_Delayed_Subprogram (Desig_Type);
1273 if Protected_Present (T_Def) then
1274 Set_Ekind (T_Name, E_Access_Protected_Subprogram_Type);
1275 Set_Convention (Desig_Type, Convention_Protected);
1276 else
1277 Set_Ekind (T_Name, E_Access_Subprogram_Type);
1278 end if;
1280 Set_Can_Use_Internal_Rep (T_Name, not Always_Compatible_Rep_On_Target);
1282 Set_Etype (T_Name, T_Name);
1283 Init_Size_Align (T_Name);
1284 Set_Directly_Designated_Type (T_Name, Desig_Type);
1286 Generate_Reference_To_Formals (T_Name);
1288 -- Ada 2005 (AI-231): Propagate the null-excluding attribute
1290 Set_Can_Never_Be_Null (T_Name, Null_Exclusion_Present (T_Def));
1292 Check_Restriction (No_Access_Subprograms, T_Def);
1293 end Access_Subprogram_Declaration;
1295 ----------------------------
1296 -- Access_Type_Declaration --
1297 ----------------------------
1299 procedure Access_Type_Declaration (T : Entity_Id; Def : Node_Id) is
1300 P : constant Node_Id := Parent (Def);
1301 S : constant Node_Id := Subtype_Indication (Def);
1303 Full_Desig : Entity_Id;
1305 begin
1306 Check_SPARK_Restriction ("access type is not allowed", Def);
1308 -- Check for permissible use of incomplete type
1310 if Nkind (S) /= N_Subtype_Indication then
1311 Analyze (S);
1313 if Ekind (Root_Type (Entity (S))) = E_Incomplete_Type then
1314 Set_Directly_Designated_Type (T, Entity (S));
1315 else
1316 Set_Directly_Designated_Type (T,
1317 Process_Subtype (S, P, T, 'P'));
1318 end if;
1320 else
1321 Set_Directly_Designated_Type (T,
1322 Process_Subtype (S, P, T, 'P'));
1323 end if;
1325 if All_Present (Def) or Constant_Present (Def) then
1326 Set_Ekind (T, E_General_Access_Type);
1327 else
1328 Set_Ekind (T, E_Access_Type);
1329 end if;
1331 Full_Desig := Designated_Type (T);
1333 if Base_Type (Full_Desig) = T then
1334 Error_Msg_N ("access type cannot designate itself", S);
1336 -- In Ada 2005, the type may have a limited view through some unit in
1337 -- its own context, allowing the following circularity that cannot be
1338 -- detected earlier
1340 elsif Is_Class_Wide_Type (Full_Desig)
1341 and then Etype (Full_Desig) = T
1342 then
1343 Error_Msg_N
1344 ("access type cannot designate its own classwide type", S);
1346 -- Clean up indication of tagged status to prevent cascaded errors
1348 Set_Is_Tagged_Type (T, False);
1349 end if;
1351 Set_Etype (T, T);
1353 -- If the type has appeared already in a with_type clause, it is frozen
1354 -- and the pointer size is already set. Else, initialize.
1356 if not From_With_Type (T) then
1357 Init_Size_Align (T);
1358 end if;
1360 -- Note that Has_Task is always false, since the access type itself
1361 -- is not a task type. See Einfo for more description on this point.
1362 -- Exactly the same consideration applies to Has_Controlled_Component.
1364 Set_Has_Task (T, False);
1365 Set_Has_Controlled_Component (T, False);
1367 -- Initialize field Finalization_Master explicitly to Empty, to avoid
1368 -- problems where an incomplete view of this entity has been previously
1369 -- established by a limited with and an overlaid version of this field
1370 -- (Stored_Constraint) was initialized for the incomplete view.
1372 -- This reset is performed in most cases except where the access type
1373 -- has been created for the purposes of allocating or deallocating a
1374 -- build-in-place object. Such access types have explicitly set pools
1375 -- and finalization masters.
1377 if No (Associated_Storage_Pool (T)) then
1378 Set_Finalization_Master (T, Empty);
1379 end if;
1381 -- Ada 2005 (AI-231): Propagate the null-excluding and access-constant
1382 -- attributes
1384 Set_Can_Never_Be_Null (T, Null_Exclusion_Present (Def));
1385 Set_Is_Access_Constant (T, Constant_Present (Def));
1386 end Access_Type_Declaration;
1388 ----------------------------------
1389 -- Add_Interface_Tag_Components --
1390 ----------------------------------
1392 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id) is
1393 Loc : constant Source_Ptr := Sloc (N);
1394 L : List_Id;
1395 Last_Tag : Node_Id;
1397 procedure Add_Tag (Iface : Entity_Id);
1398 -- Add tag for one of the progenitor interfaces
1400 -------------
1401 -- Add_Tag --
1402 -------------
1404 procedure Add_Tag (Iface : Entity_Id) is
1405 Decl : Node_Id;
1406 Def : Node_Id;
1407 Tag : Entity_Id;
1408 Offset : Entity_Id;
1410 begin
1411 pragma Assert (Is_Tagged_Type (Iface) and then Is_Interface (Iface));
1413 -- This is a reasonable place to propagate predicates
1415 if Has_Predicates (Iface) then
1416 Set_Has_Predicates (Typ);
1417 end if;
1419 Def :=
1420 Make_Component_Definition (Loc,
1421 Aliased_Present => True,
1422 Subtype_Indication =>
1423 New_Occurrence_Of (RTE (RE_Interface_Tag), Loc));
1425 Tag := Make_Temporary (Loc, 'V');
1427 Decl :=
1428 Make_Component_Declaration (Loc,
1429 Defining_Identifier => Tag,
1430 Component_Definition => Def);
1432 Analyze_Component_Declaration (Decl);
1434 Set_Analyzed (Decl);
1435 Set_Ekind (Tag, E_Component);
1436 Set_Is_Tag (Tag);
1437 Set_Is_Aliased (Tag);
1438 Set_Related_Type (Tag, Iface);
1439 Init_Component_Location (Tag);
1441 pragma Assert (Is_Frozen (Iface));
1443 Set_DT_Entry_Count (Tag,
1444 DT_Entry_Count (First_Entity (Iface)));
1446 if No (Last_Tag) then
1447 Prepend (Decl, L);
1448 else
1449 Insert_After (Last_Tag, Decl);
1450 end if;
1452 Last_Tag := Decl;
1454 -- If the ancestor has discriminants we need to give special support
1455 -- to store the offset_to_top value of the secondary dispatch tables.
1456 -- For this purpose we add a supplementary component just after the
1457 -- field that contains the tag associated with each secondary DT.
1459 if Typ /= Etype (Typ) and then Has_Discriminants (Etype (Typ)) then
1460 Def :=
1461 Make_Component_Definition (Loc,
1462 Subtype_Indication =>
1463 New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
1465 Offset := Make_Temporary (Loc, 'V');
1467 Decl :=
1468 Make_Component_Declaration (Loc,
1469 Defining_Identifier => Offset,
1470 Component_Definition => Def);
1472 Analyze_Component_Declaration (Decl);
1474 Set_Analyzed (Decl);
1475 Set_Ekind (Offset, E_Component);
1476 Set_Is_Aliased (Offset);
1477 Set_Related_Type (Offset, Iface);
1478 Init_Component_Location (Offset);
1479 Insert_After (Last_Tag, Decl);
1480 Last_Tag := Decl;
1481 end if;
1482 end Add_Tag;
1484 -- Local variables
1486 Elmt : Elmt_Id;
1487 Ext : Node_Id;
1488 Comp : Node_Id;
1490 -- Start of processing for Add_Interface_Tag_Components
1492 begin
1493 if not RTE_Available (RE_Interface_Tag) then
1494 Error_Msg
1495 ("(Ada 2005) interface types not supported by this run-time!",
1496 Sloc (N));
1497 return;
1498 end if;
1500 if Ekind (Typ) /= E_Record_Type
1501 or else (Is_Concurrent_Record_Type (Typ)
1502 and then Is_Empty_List (Abstract_Interface_List (Typ)))
1503 or else (not Is_Concurrent_Record_Type (Typ)
1504 and then No (Interfaces (Typ))
1505 and then Is_Empty_Elmt_List (Interfaces (Typ)))
1506 then
1507 return;
1508 end if;
1510 -- Find the current last tag
1512 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1513 Ext := Record_Extension_Part (Type_Definition (N));
1514 else
1515 pragma Assert (Nkind (Type_Definition (N)) = N_Record_Definition);
1516 Ext := Type_Definition (N);
1517 end if;
1519 Last_Tag := Empty;
1521 if not (Present (Component_List (Ext))) then
1522 Set_Null_Present (Ext, False);
1523 L := New_List;
1524 Set_Component_List (Ext,
1525 Make_Component_List (Loc,
1526 Component_Items => L,
1527 Null_Present => False));
1528 else
1529 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1530 L := Component_Items
1531 (Component_List
1532 (Record_Extension_Part
1533 (Type_Definition (N))));
1534 else
1535 L := Component_Items
1536 (Component_List
1537 (Type_Definition (N)));
1538 end if;
1540 -- Find the last tag component
1542 Comp := First (L);
1543 while Present (Comp) loop
1544 if Nkind (Comp) = N_Component_Declaration
1545 and then Is_Tag (Defining_Identifier (Comp))
1546 then
1547 Last_Tag := Comp;
1548 end if;
1550 Next (Comp);
1551 end loop;
1552 end if;
1554 -- At this point L references the list of components and Last_Tag
1555 -- references the current last tag (if any). Now we add the tag
1556 -- corresponding with all the interfaces that are not implemented
1557 -- by the parent.
1559 if Present (Interfaces (Typ)) then
1560 Elmt := First_Elmt (Interfaces (Typ));
1561 while Present (Elmt) loop
1562 Add_Tag (Node (Elmt));
1563 Next_Elmt (Elmt);
1564 end loop;
1565 end if;
1566 end Add_Interface_Tag_Components;
1568 -------------------------------------
1569 -- Add_Internal_Interface_Entities --
1570 -------------------------------------
1572 procedure Add_Internal_Interface_Entities (Tagged_Type : Entity_Id) is
1573 Elmt : Elmt_Id;
1574 Iface : Entity_Id;
1575 Iface_Elmt : Elmt_Id;
1576 Iface_Prim : Entity_Id;
1577 Ifaces_List : Elist_Id;
1578 New_Subp : Entity_Id := Empty;
1579 Prim : Entity_Id;
1580 Restore_Scope : Boolean := False;
1582 begin
1583 pragma Assert (Ada_Version >= Ada_2005
1584 and then Is_Record_Type (Tagged_Type)
1585 and then Is_Tagged_Type (Tagged_Type)
1586 and then Has_Interfaces (Tagged_Type)
1587 and then not Is_Interface (Tagged_Type));
1589 -- Ensure that the internal entities are added to the scope of the type
1591 if Scope (Tagged_Type) /= Current_Scope then
1592 Push_Scope (Scope (Tagged_Type));
1593 Restore_Scope := True;
1594 end if;
1596 Collect_Interfaces (Tagged_Type, Ifaces_List);
1598 Iface_Elmt := First_Elmt (Ifaces_List);
1599 while Present (Iface_Elmt) loop
1600 Iface := Node (Iface_Elmt);
1602 -- Originally we excluded here from this processing interfaces that
1603 -- are parents of Tagged_Type because their primitives are located
1604 -- in the primary dispatch table (and hence no auxiliary internal
1605 -- entities are required to handle secondary dispatch tables in such
1606 -- case). However, these auxiliary entities are also required to
1607 -- handle derivations of interfaces in formals of generics (see
1608 -- Derive_Subprograms).
1610 Elmt := First_Elmt (Primitive_Operations (Iface));
1611 while Present (Elmt) loop
1612 Iface_Prim := Node (Elmt);
1614 if not Is_Predefined_Dispatching_Operation (Iface_Prim) then
1615 Prim :=
1616 Find_Primitive_Covering_Interface
1617 (Tagged_Type => Tagged_Type,
1618 Iface_Prim => Iface_Prim);
1620 if No (Prim) and then Serious_Errors_Detected > 0 then
1621 goto Continue;
1622 end if;
1624 pragma Assert (Present (Prim));
1626 -- Ada 2012 (AI05-0197): If the name of the covering primitive
1627 -- differs from the name of the interface primitive then it is
1628 -- a private primitive inherited from a parent type. In such
1629 -- case, given that Tagged_Type covers the interface, the
1630 -- inherited private primitive becomes visible. For such
1631 -- purpose we add a new entity that renames the inherited
1632 -- private primitive.
1634 if Chars (Prim) /= Chars (Iface_Prim) then
1635 pragma Assert (Has_Suffix (Prim, 'P'));
1636 Derive_Subprogram
1637 (New_Subp => New_Subp,
1638 Parent_Subp => Iface_Prim,
1639 Derived_Type => Tagged_Type,
1640 Parent_Type => Iface);
1641 Set_Alias (New_Subp, Prim);
1642 Set_Is_Abstract_Subprogram
1643 (New_Subp, Is_Abstract_Subprogram (Prim));
1644 end if;
1646 Derive_Subprogram
1647 (New_Subp => New_Subp,
1648 Parent_Subp => Iface_Prim,
1649 Derived_Type => Tagged_Type,
1650 Parent_Type => Iface);
1652 -- Ada 2005 (AI-251): Decorate internal entity Iface_Subp
1653 -- associated with interface types. These entities are
1654 -- only registered in the list of primitives of its
1655 -- corresponding tagged type because they are only used
1656 -- to fill the contents of the secondary dispatch tables.
1657 -- Therefore they are removed from the homonym chains.
1659 Set_Is_Hidden (New_Subp);
1660 Set_Is_Internal (New_Subp);
1661 Set_Alias (New_Subp, Prim);
1662 Set_Is_Abstract_Subprogram
1663 (New_Subp, Is_Abstract_Subprogram (Prim));
1664 Set_Interface_Alias (New_Subp, Iface_Prim);
1666 -- If the returned type is an interface then propagate it to
1667 -- the returned type. Needed by the thunk to generate the code
1668 -- which displaces "this" to reference the corresponding
1669 -- secondary dispatch table in the returned object.
1671 if Is_Interface (Etype (Iface_Prim)) then
1672 Set_Etype (New_Subp, Etype (Iface_Prim));
1673 end if;
1675 -- Internal entities associated with interface types are
1676 -- only registered in the list of primitives of the tagged
1677 -- type. They are only used to fill the contents of the
1678 -- secondary dispatch tables. Therefore they are not needed
1679 -- in the homonym chains.
1681 Remove_Homonym (New_Subp);
1683 -- Hidden entities associated with interfaces must have set
1684 -- the Has_Delay_Freeze attribute to ensure that, in case of
1685 -- locally defined tagged types (or compiling with static
1686 -- dispatch tables generation disabled) the corresponding
1687 -- entry of the secondary dispatch table is filled when
1688 -- such an entity is frozen.
1690 Set_Has_Delayed_Freeze (New_Subp);
1691 end if;
1693 <<Continue>>
1694 Next_Elmt (Elmt);
1695 end loop;
1697 Next_Elmt (Iface_Elmt);
1698 end loop;
1700 if Restore_Scope then
1701 Pop_Scope;
1702 end if;
1703 end Add_Internal_Interface_Entities;
1705 -----------------------------------
1706 -- Analyze_Component_Declaration --
1707 -----------------------------------
1709 procedure Analyze_Component_Declaration (N : Node_Id) is
1710 Id : constant Entity_Id := Defining_Identifier (N);
1711 E : constant Node_Id := Expression (N);
1712 Typ : constant Node_Id :=
1713 Subtype_Indication (Component_Definition (N));
1714 T : Entity_Id;
1715 P : Entity_Id;
1717 function Contains_POC (Constr : Node_Id) return Boolean;
1718 -- Determines whether a constraint uses the discriminant of a record
1719 -- type thus becoming a per-object constraint (POC).
1721 function Is_Known_Limited (Typ : Entity_Id) return Boolean;
1722 -- Typ is the type of the current component, check whether this type is
1723 -- a limited type. Used to validate declaration against that of
1724 -- enclosing record.
1726 ------------------
1727 -- Contains_POC --
1728 ------------------
1730 function Contains_POC (Constr : Node_Id) return Boolean is
1731 begin
1732 -- Prevent cascaded errors
1734 if Error_Posted (Constr) then
1735 return False;
1736 end if;
1738 case Nkind (Constr) is
1739 when N_Attribute_Reference =>
1740 return
1741 Attribute_Name (Constr) = Name_Access
1742 and then Prefix (Constr) = Scope (Entity (Prefix (Constr)));
1744 when N_Discriminant_Association =>
1745 return Denotes_Discriminant (Expression (Constr));
1747 when N_Identifier =>
1748 return Denotes_Discriminant (Constr);
1750 when N_Index_Or_Discriminant_Constraint =>
1751 declare
1752 IDC : Node_Id;
1754 begin
1755 IDC := First (Constraints (Constr));
1756 while Present (IDC) loop
1758 -- One per-object constraint is sufficient
1760 if Contains_POC (IDC) then
1761 return True;
1762 end if;
1764 Next (IDC);
1765 end loop;
1767 return False;
1768 end;
1770 when N_Range =>
1771 return Denotes_Discriminant (Low_Bound (Constr))
1772 or else
1773 Denotes_Discriminant (High_Bound (Constr));
1775 when N_Range_Constraint =>
1776 return Denotes_Discriminant (Range_Expression (Constr));
1778 when others =>
1779 return False;
1781 end case;
1782 end Contains_POC;
1784 ----------------------
1785 -- Is_Known_Limited --
1786 ----------------------
1788 function Is_Known_Limited (Typ : Entity_Id) return Boolean is
1789 P : constant Entity_Id := Etype (Typ);
1790 R : constant Entity_Id := Root_Type (Typ);
1792 begin
1793 if Is_Limited_Record (Typ) then
1794 return True;
1796 -- If the root type is limited (and not a limited interface)
1797 -- so is the current type
1799 elsif Is_Limited_Record (R)
1800 and then (not Is_Interface (R) or else not Is_Limited_Interface (R))
1801 then
1802 return True;
1804 -- Else the type may have a limited interface progenitor, but a
1805 -- limited record parent.
1807 elsif R /= P and then Is_Limited_Record (P) then
1808 return True;
1810 else
1811 return False;
1812 end if;
1813 end Is_Known_Limited;
1815 -- Start of processing for Analyze_Component_Declaration
1817 begin
1818 Generate_Definition (Id);
1819 Enter_Name (Id);
1821 if Present (Typ) then
1822 T := Find_Type_Of_Object
1823 (Subtype_Indication (Component_Definition (N)), N);
1825 if not Nkind_In (Typ, N_Identifier, N_Expanded_Name) then
1826 Check_SPARK_Restriction ("subtype mark required", Typ);
1827 end if;
1829 -- Ada 2005 (AI-230): Access Definition case
1831 else
1832 pragma Assert (Present
1833 (Access_Definition (Component_Definition (N))));
1835 T := Access_Definition
1836 (Related_Nod => N,
1837 N => Access_Definition (Component_Definition (N)));
1838 Set_Is_Local_Anonymous_Access (T);
1840 -- Ada 2005 (AI-254)
1842 if Present (Access_To_Subprogram_Definition
1843 (Access_Definition (Component_Definition (N))))
1844 and then Protected_Present (Access_To_Subprogram_Definition
1845 (Access_Definition
1846 (Component_Definition (N))))
1847 then
1848 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1849 end if;
1850 end if;
1852 -- If the subtype is a constrained subtype of the enclosing record,
1853 -- (which must have a partial view) the back-end does not properly
1854 -- handle the recursion. Rewrite the component declaration with an
1855 -- explicit subtype indication, which is acceptable to Gigi. We can copy
1856 -- the tree directly because side effects have already been removed from
1857 -- discriminant constraints.
1859 if Ekind (T) = E_Access_Subtype
1860 and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
1861 and then Comes_From_Source (T)
1862 and then Nkind (Parent (T)) = N_Subtype_Declaration
1863 and then Etype (Directly_Designated_Type (T)) = Current_Scope
1864 then
1865 Rewrite
1866 (Subtype_Indication (Component_Definition (N)),
1867 New_Copy_Tree (Subtype_Indication (Parent (T))));
1868 T := Find_Type_Of_Object
1869 (Subtype_Indication (Component_Definition (N)), N);
1870 end if;
1872 -- If the component declaration includes a default expression, then we
1873 -- check that the component is not of a limited type (RM 3.7(5)),
1874 -- and do the special preanalysis of the expression (see section on
1875 -- "Handling of Default and Per-Object Expressions" in the spec of
1876 -- package Sem).
1878 if Present (E) then
1879 Check_SPARK_Restriction ("default expression is not allowed", E);
1880 Preanalyze_Spec_Expression (E, T);
1881 Check_Initialization (T, E);
1883 if Ada_Version >= Ada_2005
1884 and then Ekind (T) = E_Anonymous_Access_Type
1885 and then Etype (E) /= Any_Type
1886 then
1887 -- Check RM 3.9.2(9): "if the expected type for an expression is
1888 -- an anonymous access-to-specific tagged type, then the object
1889 -- designated by the expression shall not be dynamically tagged
1890 -- unless it is a controlling operand in a call on a dispatching
1891 -- operation"
1893 if Is_Tagged_Type (Directly_Designated_Type (T))
1894 and then
1895 Ekind (Directly_Designated_Type (T)) /= E_Class_Wide_Type
1896 and then
1897 Ekind (Directly_Designated_Type (Etype (E))) =
1898 E_Class_Wide_Type
1899 then
1900 Error_Msg_N
1901 ("access to specific tagged type required (RM 3.9.2(9))", E);
1902 end if;
1904 -- (Ada 2005: AI-230): Accessibility check for anonymous
1905 -- components
1907 if Type_Access_Level (Etype (E)) >
1908 Deepest_Type_Access_Level (T)
1909 then
1910 Error_Msg_N
1911 ("expression has deeper access level than component " &
1912 "(RM 3.10.2 (12.2))", E);
1913 end if;
1915 -- The initialization expression is a reference to an access
1916 -- discriminant. The type of the discriminant is always deeper
1917 -- than any access type.
1919 if Ekind (Etype (E)) = E_Anonymous_Access_Type
1920 and then Is_Entity_Name (E)
1921 and then Ekind (Entity (E)) = E_In_Parameter
1922 and then Present (Discriminal_Link (Entity (E)))
1923 then
1924 Error_Msg_N
1925 ("discriminant has deeper accessibility level than target",
1927 end if;
1928 end if;
1929 end if;
1931 -- The parent type may be a private view with unknown discriminants,
1932 -- and thus unconstrained. Regular components must be constrained.
1934 if Is_Indefinite_Subtype (T) and then Chars (Id) /= Name_uParent then
1935 if Is_Class_Wide_Type (T) then
1936 Error_Msg_N
1937 ("class-wide subtype with unknown discriminants" &
1938 " in component declaration",
1939 Subtype_Indication (Component_Definition (N)));
1940 else
1941 Error_Msg_N
1942 ("unconstrained subtype in component declaration",
1943 Subtype_Indication (Component_Definition (N)));
1944 end if;
1946 -- Components cannot be abstract, except for the special case of
1947 -- the _Parent field (case of extending an abstract tagged type)
1949 elsif Is_Abstract_Type (T) and then Chars (Id) /= Name_uParent then
1950 Error_Msg_N ("type of a component cannot be abstract", N);
1951 end if;
1953 Set_Etype (Id, T);
1954 Set_Is_Aliased (Id, Aliased_Present (Component_Definition (N)));
1956 -- The component declaration may have a per-object constraint, set
1957 -- the appropriate flag in the defining identifier of the subtype.
1959 if Present (Subtype_Indication (Component_Definition (N))) then
1960 declare
1961 Sindic : constant Node_Id :=
1962 Subtype_Indication (Component_Definition (N));
1963 begin
1964 if Nkind (Sindic) = N_Subtype_Indication
1965 and then Present (Constraint (Sindic))
1966 and then Contains_POC (Constraint (Sindic))
1967 then
1968 Set_Has_Per_Object_Constraint (Id);
1969 end if;
1970 end;
1971 end if;
1973 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
1974 -- out some static checks.
1976 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (T) then
1977 Null_Exclusion_Static_Checks (N);
1978 end if;
1980 -- If this component is private (or depends on a private type), flag the
1981 -- record type to indicate that some operations are not available.
1983 P := Private_Component (T);
1985 if Present (P) then
1987 -- Check for circular definitions
1989 if P = Any_Type then
1990 Set_Etype (Id, Any_Type);
1992 -- There is a gap in the visibility of operations only if the
1993 -- component type is not defined in the scope of the record type.
1995 elsif Scope (P) = Scope (Current_Scope) then
1996 null;
1998 elsif Is_Limited_Type (P) then
1999 Set_Is_Limited_Composite (Current_Scope);
2001 else
2002 Set_Is_Private_Composite (Current_Scope);
2003 end if;
2004 end if;
2006 if P /= Any_Type
2007 and then Is_Limited_Type (T)
2008 and then Chars (Id) /= Name_uParent
2009 and then Is_Tagged_Type (Current_Scope)
2010 then
2011 if Is_Derived_Type (Current_Scope)
2012 and then not Is_Known_Limited (Current_Scope)
2013 then
2014 Error_Msg_N
2015 ("extension of nonlimited type cannot have limited components",
2018 if Is_Interface (Root_Type (Current_Scope)) then
2019 Error_Msg_N
2020 ("\limitedness is not inherited from limited interface", N);
2021 Error_Msg_N ("\add LIMITED to type indication", N);
2022 end if;
2024 Explain_Limited_Type (T, N);
2025 Set_Etype (Id, Any_Type);
2026 Set_Is_Limited_Composite (Current_Scope, False);
2028 elsif not Is_Derived_Type (Current_Scope)
2029 and then not Is_Limited_Record (Current_Scope)
2030 and then not Is_Concurrent_Type (Current_Scope)
2031 then
2032 Error_Msg_N
2033 ("nonlimited tagged type cannot have limited components", N);
2034 Explain_Limited_Type (T, N);
2035 Set_Etype (Id, Any_Type);
2036 Set_Is_Limited_Composite (Current_Scope, False);
2037 end if;
2038 end if;
2040 Set_Original_Record_Component (Id, Id);
2042 if Has_Aspects (N) then
2043 Analyze_Aspect_Specifications (N, Id);
2044 end if;
2046 Analyze_Dimension (N);
2047 end Analyze_Component_Declaration;
2049 --------------------------
2050 -- Analyze_Declarations --
2051 --------------------------
2053 procedure Analyze_Declarations (L : List_Id) is
2054 D : Node_Id;
2055 Freeze_From : Entity_Id := Empty;
2056 Next_Node : Node_Id;
2058 procedure Adjust_D;
2059 -- Adjust D not to include implicit label declarations, since these
2060 -- have strange Sloc values that result in elaboration check problems.
2061 -- (They have the sloc of the label as found in the source, and that
2062 -- is ahead of the current declarative part).
2064 --------------
2065 -- Adjust_D --
2066 --------------
2068 procedure Adjust_D is
2069 begin
2070 while Present (Prev (D))
2071 and then Nkind (D) = N_Implicit_Label_Declaration
2072 loop
2073 Prev (D);
2074 end loop;
2075 end Adjust_D;
2077 -- Start of processing for Analyze_Declarations
2079 begin
2080 if Restriction_Check_Required (SPARK_05) then
2081 Check_Later_Vs_Basic_Declarations (L, During_Parsing => False);
2082 end if;
2084 D := First (L);
2085 while Present (D) loop
2087 -- Package spec cannot contain a package declaration in SPARK
2089 if Nkind (D) = N_Package_Declaration
2090 and then Nkind (Parent (L)) = N_Package_Specification
2091 then
2092 Check_SPARK_Restriction
2093 ("package specification cannot contain a package declaration",
2095 end if;
2097 -- Complete analysis of declaration
2099 Analyze (D);
2100 Next_Node := Next (D);
2102 if No (Freeze_From) then
2103 Freeze_From := First_Entity (Current_Scope);
2104 end if;
2106 -- At the end of a declarative part, freeze remaining entities
2107 -- declared in it. The end of the visible declarations of package
2108 -- specification is not the end of a declarative part if private
2109 -- declarations are present. The end of a package declaration is a
2110 -- freezing point only if it a library package. A task definition or
2111 -- protected type definition is not a freeze point either. Finally,
2112 -- we do not freeze entities in generic scopes, because there is no
2113 -- code generated for them and freeze nodes will be generated for
2114 -- the instance.
2116 -- The end of a package instantiation is not a freeze point, but
2117 -- for now we make it one, because the generic body is inserted
2118 -- (currently) immediately after. Generic instantiations will not
2119 -- be a freeze point once delayed freezing of bodies is implemented.
2120 -- (This is needed in any case for early instantiations ???).
2122 if No (Next_Node) then
2123 if Nkind_In (Parent (L), N_Component_List,
2124 N_Task_Definition,
2125 N_Protected_Definition)
2126 then
2127 null;
2129 elsif Nkind (Parent (L)) /= N_Package_Specification then
2130 if Nkind (Parent (L)) = N_Package_Body then
2131 Freeze_From := First_Entity (Current_Scope);
2132 end if;
2134 Adjust_D;
2135 Freeze_All (Freeze_From, D);
2136 Freeze_From := Last_Entity (Current_Scope);
2138 elsif Scope (Current_Scope) /= Standard_Standard
2139 and then not Is_Child_Unit (Current_Scope)
2140 and then No (Generic_Parent (Parent (L)))
2141 then
2142 null;
2144 elsif L /= Visible_Declarations (Parent (L))
2145 or else No (Private_Declarations (Parent (L)))
2146 or else Is_Empty_List (Private_Declarations (Parent (L)))
2147 then
2148 Adjust_D;
2149 Freeze_All (Freeze_From, D);
2150 Freeze_From := Last_Entity (Current_Scope);
2151 end if;
2153 -- If next node is a body then freeze all types before the body.
2154 -- An exception occurs for some expander-generated bodies. If these
2155 -- are generated at places where in general language rules would not
2156 -- allow a freeze point, then we assume that the expander has
2157 -- explicitly checked that all required types are properly frozen,
2158 -- and we do not cause general freezing here. This special circuit
2159 -- is used when the encountered body is marked as having already
2160 -- been analyzed.
2162 -- In all other cases (bodies that come from source, and expander
2163 -- generated bodies that have not been analyzed yet), freeze all
2164 -- types now. Note that in the latter case, the expander must take
2165 -- care to attach the bodies at a proper place in the tree so as to
2166 -- not cause unwanted freezing at that point.
2168 elsif not Analyzed (Next_Node)
2169 and then (Nkind_In (Next_Node, N_Subprogram_Body,
2170 N_Entry_Body,
2171 N_Package_Body,
2172 N_Protected_Body,
2173 N_Task_Body)
2174 or else
2175 Nkind (Next_Node) in N_Body_Stub)
2176 then
2177 Adjust_D;
2178 Freeze_All (Freeze_From, D);
2179 Freeze_From := Last_Entity (Current_Scope);
2180 end if;
2182 D := Next_Node;
2183 end loop;
2185 -- One more thing to do, we need to scan the declarations to check for
2186 -- any precondition/postcondition pragmas (Pre/Post aspects have by this
2187 -- stage been converted into corresponding pragmas). It is at this point
2188 -- that we analyze the expressions in such pragmas, to implement the
2189 -- delayed visibility requirement.
2191 declare
2192 Decl : Node_Id;
2193 Subp_Id : Entity_Id;
2195 begin
2196 Decl := First (L);
2197 while Present (Decl) loop
2198 if Nkind (Decl) = N_Subprogram_Declaration then
2199 Subp_Id := Defining_Unit_Name (Specification (Decl));
2200 Analyze_Subprogram_Contract (Subp_Id);
2201 end if;
2203 Next (Decl);
2204 end loop;
2205 end;
2206 end Analyze_Declarations;
2208 -----------------------------------
2209 -- Analyze_Full_Type_Declaration --
2210 -----------------------------------
2212 procedure Analyze_Full_Type_Declaration (N : Node_Id) is
2213 Def : constant Node_Id := Type_Definition (N);
2214 Def_Id : constant Entity_Id := Defining_Identifier (N);
2215 T : Entity_Id;
2216 Prev : Entity_Id;
2218 Is_Remote : constant Boolean :=
2219 (Is_Remote_Types (Current_Scope)
2220 or else Is_Remote_Call_Interface (Current_Scope))
2221 and then not (In_Private_Part (Current_Scope)
2222 or else In_Package_Body (Current_Scope));
2224 procedure Check_Ops_From_Incomplete_Type;
2225 -- If there is a tagged incomplete partial view of the type, traverse
2226 -- the primitives of the incomplete view and change the type of any
2227 -- controlling formals and result to indicate the full view. The
2228 -- primitives will be added to the full type's primitive operations
2229 -- list later in Sem_Disp.Check_Operation_From_Incomplete_Type (which
2230 -- is called from Process_Incomplete_Dependents).
2232 ------------------------------------
2233 -- Check_Ops_From_Incomplete_Type --
2234 ------------------------------------
2236 procedure Check_Ops_From_Incomplete_Type is
2237 Elmt : Elmt_Id;
2238 Formal : Entity_Id;
2239 Op : Entity_Id;
2241 begin
2242 if Prev /= T
2243 and then Ekind (Prev) = E_Incomplete_Type
2244 and then Is_Tagged_Type (Prev)
2245 and then Is_Tagged_Type (T)
2246 then
2247 Elmt := First_Elmt (Primitive_Operations (Prev));
2248 while Present (Elmt) loop
2249 Op := Node (Elmt);
2251 Formal := First_Formal (Op);
2252 while Present (Formal) loop
2253 if Etype (Formal) = Prev then
2254 Set_Etype (Formal, T);
2255 end if;
2257 Next_Formal (Formal);
2258 end loop;
2260 if Etype (Op) = Prev then
2261 Set_Etype (Op, T);
2262 end if;
2264 Next_Elmt (Elmt);
2265 end loop;
2266 end if;
2267 end Check_Ops_From_Incomplete_Type;
2269 -- Start of processing for Analyze_Full_Type_Declaration
2271 begin
2272 Prev := Find_Type_Name (N);
2274 -- The full view, if present, now points to the current type
2276 -- Ada 2005 (AI-50217): If the type was previously decorated when
2277 -- imported through a LIMITED WITH clause, it appears as incomplete
2278 -- but has no full view.
2280 if Ekind (Prev) = E_Incomplete_Type
2281 and then Present (Full_View (Prev))
2282 then
2283 T := Full_View (Prev);
2284 else
2285 T := Prev;
2286 end if;
2288 Set_Is_Pure (T, Is_Pure (Current_Scope));
2290 -- We set the flag Is_First_Subtype here. It is needed to set the
2291 -- corresponding flag for the Implicit class-wide-type created
2292 -- during tagged types processing.
2294 Set_Is_First_Subtype (T, True);
2296 -- Only composite types other than array types are allowed to have
2297 -- discriminants.
2299 case Nkind (Def) is
2301 -- For derived types, the rule will be checked once we've figured
2302 -- out the parent type.
2304 when N_Derived_Type_Definition =>
2305 null;
2307 -- For record types, discriminants are allowed, unless we are in
2308 -- SPARK.
2310 when N_Record_Definition =>
2311 if Present (Discriminant_Specifications (N)) then
2312 Check_SPARK_Restriction
2313 ("discriminant type is not allowed",
2314 Defining_Identifier
2315 (First (Discriminant_Specifications (N))));
2316 end if;
2318 when others =>
2319 if Present (Discriminant_Specifications (N)) then
2320 Error_Msg_N
2321 ("elementary or array type cannot have discriminants",
2322 Defining_Identifier
2323 (First (Discriminant_Specifications (N))));
2324 end if;
2325 end case;
2327 -- Elaborate the type definition according to kind, and generate
2328 -- subsidiary (implicit) subtypes where needed. We skip this if it was
2329 -- already done (this happens during the reanalysis that follows a call
2330 -- to the high level optimizer).
2332 if not Analyzed (T) then
2333 Set_Analyzed (T);
2335 case Nkind (Def) is
2337 when N_Access_To_Subprogram_Definition =>
2338 Access_Subprogram_Declaration (T, Def);
2340 -- If this is a remote access to subprogram, we must create the
2341 -- equivalent fat pointer type, and related subprograms.
2343 if Is_Remote then
2344 Process_Remote_AST_Declaration (N);
2345 end if;
2347 -- Validate categorization rule against access type declaration
2348 -- usually a violation in Pure unit, Shared_Passive unit.
2350 Validate_Access_Type_Declaration (T, N);
2352 when N_Access_To_Object_Definition =>
2353 Access_Type_Declaration (T, Def);
2355 -- Validate categorization rule against access type declaration
2356 -- usually a violation in Pure unit, Shared_Passive unit.
2358 Validate_Access_Type_Declaration (T, N);
2360 -- If we are in a Remote_Call_Interface package and define a
2361 -- RACW, then calling stubs and specific stream attributes
2362 -- must be added.
2364 if Is_Remote
2365 and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
2366 then
2367 Add_RACW_Features (Def_Id);
2368 end if;
2370 -- Set no strict aliasing flag if config pragma seen
2372 if Opt.No_Strict_Aliasing then
2373 Set_No_Strict_Aliasing (Base_Type (Def_Id));
2374 end if;
2376 when N_Array_Type_Definition =>
2377 Array_Type_Declaration (T, Def);
2379 when N_Derived_Type_Definition =>
2380 Derived_Type_Declaration (T, N, T /= Def_Id);
2382 when N_Enumeration_Type_Definition =>
2383 Enumeration_Type_Declaration (T, Def);
2385 when N_Floating_Point_Definition =>
2386 Floating_Point_Type_Declaration (T, Def);
2388 when N_Decimal_Fixed_Point_Definition =>
2389 Decimal_Fixed_Point_Type_Declaration (T, Def);
2391 when N_Ordinary_Fixed_Point_Definition =>
2392 Ordinary_Fixed_Point_Type_Declaration (T, Def);
2394 when N_Signed_Integer_Type_Definition =>
2395 Signed_Integer_Type_Declaration (T, Def);
2397 when N_Modular_Type_Definition =>
2398 Modular_Type_Declaration (T, Def);
2400 when N_Record_Definition =>
2401 Record_Type_Declaration (T, N, Prev);
2403 -- If declaration has a parse error, nothing to elaborate.
2405 when N_Error =>
2406 null;
2408 when others =>
2409 raise Program_Error;
2411 end case;
2412 end if;
2414 if Etype (T) = Any_Type then
2415 return;
2416 end if;
2418 -- Controlled type is not allowed in SPARK
2420 if Is_Visibly_Controlled (T) then
2421 Check_SPARK_Restriction ("controlled type is not allowed", N);
2422 end if;
2424 -- Some common processing for all types
2426 Set_Depends_On_Private (T, Has_Private_Component (T));
2427 Check_Ops_From_Incomplete_Type;
2429 -- Both the declared entity, and its anonymous base type if one
2430 -- was created, need freeze nodes allocated.
2432 declare
2433 B : constant Entity_Id := Base_Type (T);
2435 begin
2436 -- In the case where the base type differs from the first subtype, we
2437 -- pre-allocate a freeze node, and set the proper link to the first
2438 -- subtype. Freeze_Entity will use this preallocated freeze node when
2439 -- it freezes the entity.
2441 -- This does not apply if the base type is a generic type, whose
2442 -- declaration is independent of the current derived definition.
2444 if B /= T and then not Is_Generic_Type (B) then
2445 Ensure_Freeze_Node (B);
2446 Set_First_Subtype_Link (Freeze_Node (B), T);
2447 end if;
2449 -- A type that is imported through a limited_with clause cannot
2450 -- generate any code, and thus need not be frozen. However, an access
2451 -- type with an imported designated type needs a finalization list,
2452 -- which may be referenced in some other package that has non-limited
2453 -- visibility on the designated type. Thus we must create the
2454 -- finalization list at the point the access type is frozen, to
2455 -- prevent unsatisfied references at link time.
2457 if not From_With_Type (T) or else Is_Access_Type (T) then
2458 Set_Has_Delayed_Freeze (T);
2459 end if;
2460 end;
2462 -- Case where T is the full declaration of some private type which has
2463 -- been swapped in Defining_Identifier (N).
2465 if T /= Def_Id and then Is_Private_Type (Def_Id) then
2466 Process_Full_View (N, T, Def_Id);
2468 -- Record the reference. The form of this is a little strange, since
2469 -- the full declaration has been swapped in. So the first parameter
2470 -- here represents the entity to which a reference is made which is
2471 -- the "real" entity, i.e. the one swapped in, and the second
2472 -- parameter provides the reference location.
2474 -- Also, we want to kill Has_Pragma_Unreferenced temporarily here
2475 -- since we don't want a complaint about the full type being an
2476 -- unwanted reference to the private type
2478 declare
2479 B : constant Boolean := Has_Pragma_Unreferenced (T);
2480 begin
2481 Set_Has_Pragma_Unreferenced (T, False);
2482 Generate_Reference (T, T, 'c');
2483 Set_Has_Pragma_Unreferenced (T, B);
2484 end;
2486 Set_Completion_Referenced (Def_Id);
2488 -- For completion of incomplete type, process incomplete dependents
2489 -- and always mark the full type as referenced (it is the incomplete
2490 -- type that we get for any real reference).
2492 elsif Ekind (Prev) = E_Incomplete_Type then
2493 Process_Incomplete_Dependents (N, T, Prev);
2494 Generate_Reference (Prev, Def_Id, 'c');
2495 Set_Completion_Referenced (Def_Id);
2497 -- If not private type or incomplete type completion, this is a real
2498 -- definition of a new entity, so record it.
2500 else
2501 Generate_Definition (Def_Id);
2502 end if;
2504 if Chars (Scope (Def_Id)) = Name_System
2505 and then Chars (Def_Id) = Name_Address
2506 and then Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (N)))
2507 then
2508 Set_Is_Descendent_Of_Address (Def_Id);
2509 Set_Is_Descendent_Of_Address (Base_Type (Def_Id));
2510 Set_Is_Descendent_Of_Address (Prev);
2511 end if;
2513 Set_Optimize_Alignment_Flags (Def_Id);
2514 Check_Eliminated (Def_Id);
2516 -- If the declaration is a completion and aspects are present, apply
2517 -- them to the entity for the type which is currently the partial
2518 -- view, but which is the one that will be frozen.
2520 if Has_Aspects (N) then
2521 if Prev /= Def_Id then
2522 Analyze_Aspect_Specifications (N, Prev);
2523 else
2524 Analyze_Aspect_Specifications (N, Def_Id);
2525 end if;
2526 end if;
2527 end Analyze_Full_Type_Declaration;
2529 ----------------------------------
2530 -- Analyze_Incomplete_Type_Decl --
2531 ----------------------------------
2533 procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
2534 F : constant Boolean := Is_Pure (Current_Scope);
2535 T : Entity_Id;
2537 begin
2538 Check_SPARK_Restriction ("incomplete type is not allowed", N);
2540 Generate_Definition (Defining_Identifier (N));
2542 -- Process an incomplete declaration. The identifier must not have been
2543 -- declared already in the scope. However, an incomplete declaration may
2544 -- appear in the private part of a package, for a private type that has
2545 -- already been declared.
2547 -- In this case, the discriminants (if any) must match
2549 T := Find_Type_Name (N);
2551 Set_Ekind (T, E_Incomplete_Type);
2552 Init_Size_Align (T);
2553 Set_Is_First_Subtype (T, True);
2554 Set_Etype (T, T);
2556 -- Ada 2005 (AI-326): Minimum decoration to give support to tagged
2557 -- incomplete types.
2559 if Tagged_Present (N) then
2560 Set_Is_Tagged_Type (T);
2561 Make_Class_Wide_Type (T);
2562 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2563 end if;
2565 Push_Scope (T);
2567 Set_Stored_Constraint (T, No_Elist);
2569 if Present (Discriminant_Specifications (N)) then
2570 Process_Discriminants (N);
2571 end if;
2573 End_Scope;
2575 -- If the type has discriminants, non-trivial subtypes may be
2576 -- declared before the full view of the type. The full views of those
2577 -- subtypes will be built after the full view of the type.
2579 Set_Private_Dependents (T, New_Elmt_List);
2580 Set_Is_Pure (T, F);
2581 end Analyze_Incomplete_Type_Decl;
2583 -----------------------------------
2584 -- Analyze_Interface_Declaration --
2585 -----------------------------------
2587 procedure Analyze_Interface_Declaration (T : Entity_Id; Def : Node_Id) is
2588 CW : constant Entity_Id := Class_Wide_Type (T);
2590 begin
2591 Set_Is_Tagged_Type (T);
2593 Set_Is_Limited_Record (T, Limited_Present (Def)
2594 or else Task_Present (Def)
2595 or else Protected_Present (Def)
2596 or else Synchronized_Present (Def));
2598 -- Type is abstract if full declaration carries keyword, or if previous
2599 -- partial view did.
2601 Set_Is_Abstract_Type (T);
2602 Set_Is_Interface (T);
2604 -- Type is a limited interface if it includes the keyword limited, task,
2605 -- protected, or synchronized.
2607 Set_Is_Limited_Interface
2608 (T, Limited_Present (Def)
2609 or else Protected_Present (Def)
2610 or else Synchronized_Present (Def)
2611 or else Task_Present (Def));
2613 Set_Interfaces (T, New_Elmt_List);
2614 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2616 -- Complete the decoration of the class-wide entity if it was already
2617 -- built (i.e. during the creation of the limited view)
2619 if Present (CW) then
2620 Set_Is_Interface (CW);
2621 Set_Is_Limited_Interface (CW, Is_Limited_Interface (T));
2622 end if;
2624 -- Check runtime support for synchronized interfaces
2626 if VM_Target = No_VM
2627 and then (Is_Task_Interface (T)
2628 or else Is_Protected_Interface (T)
2629 or else Is_Synchronized_Interface (T))
2630 and then not RTE_Available (RE_Select_Specific_Data)
2631 then
2632 Error_Msg_CRT ("synchronized interfaces", T);
2633 end if;
2634 end Analyze_Interface_Declaration;
2636 -----------------------------
2637 -- Analyze_Itype_Reference --
2638 -----------------------------
2640 -- Nothing to do. This node is placed in the tree only for the benefit of
2641 -- back end processing, and has no effect on the semantic processing.
2643 procedure Analyze_Itype_Reference (N : Node_Id) is
2644 begin
2645 pragma Assert (Is_Itype (Itype (N)));
2646 null;
2647 end Analyze_Itype_Reference;
2649 --------------------------------
2650 -- Analyze_Number_Declaration --
2651 --------------------------------
2653 procedure Analyze_Number_Declaration (N : Node_Id) is
2654 Id : constant Entity_Id := Defining_Identifier (N);
2655 E : constant Node_Id := Expression (N);
2656 T : Entity_Id;
2657 Index : Interp_Index;
2658 It : Interp;
2660 begin
2661 Generate_Definition (Id);
2662 Enter_Name (Id);
2664 -- This is an optimization of a common case of an integer literal
2666 if Nkind (E) = N_Integer_Literal then
2667 Set_Is_Static_Expression (E, True);
2668 Set_Etype (E, Universal_Integer);
2670 Set_Etype (Id, Universal_Integer);
2671 Set_Ekind (Id, E_Named_Integer);
2672 Set_Is_Frozen (Id, True);
2673 return;
2674 end if;
2676 Set_Is_Pure (Id, Is_Pure (Current_Scope));
2678 -- Process expression, replacing error by integer zero, to avoid
2679 -- cascaded errors or aborts further along in the processing
2681 -- Replace Error by integer zero, which seems least likely to cause
2682 -- cascaded errors.
2684 if E = Error then
2685 Rewrite (E, Make_Integer_Literal (Sloc (E), Uint_0));
2686 Set_Error_Posted (E);
2687 end if;
2689 Analyze (E);
2691 -- Verify that the expression is static and numeric. If
2692 -- the expression is overloaded, we apply the preference
2693 -- rule that favors root numeric types.
2695 if not Is_Overloaded (E) then
2696 T := Etype (E);
2698 else
2699 T := Any_Type;
2701 Get_First_Interp (E, Index, It);
2702 while Present (It.Typ) loop
2703 if (Is_Integer_Type (It.Typ) or else Is_Real_Type (It.Typ))
2704 and then (Scope (Base_Type (It.Typ))) = Standard_Standard
2705 then
2706 if T = Any_Type then
2707 T := It.Typ;
2709 elsif It.Typ = Universal_Real
2710 or else It.Typ = Universal_Integer
2711 then
2712 -- Choose universal interpretation over any other
2714 T := It.Typ;
2715 exit;
2716 end if;
2717 end if;
2719 Get_Next_Interp (Index, It);
2720 end loop;
2721 end if;
2723 if Is_Integer_Type (T) then
2724 Resolve (E, T);
2725 Set_Etype (Id, Universal_Integer);
2726 Set_Ekind (Id, E_Named_Integer);
2728 elsif Is_Real_Type (T) then
2730 -- Because the real value is converted to universal_real, this is a
2731 -- legal context for a universal fixed expression.
2733 if T = Universal_Fixed then
2734 declare
2735 Loc : constant Source_Ptr := Sloc (N);
2736 Conv : constant Node_Id := Make_Type_Conversion (Loc,
2737 Subtype_Mark =>
2738 New_Occurrence_Of (Universal_Real, Loc),
2739 Expression => Relocate_Node (E));
2741 begin
2742 Rewrite (E, Conv);
2743 Analyze (E);
2744 end;
2746 elsif T = Any_Fixed then
2747 Error_Msg_N ("illegal context for mixed mode operation", E);
2749 -- Expression is of the form : universal_fixed * integer. Try to
2750 -- resolve as universal_real.
2752 T := Universal_Real;
2753 Set_Etype (E, T);
2754 end if;
2756 Resolve (E, T);
2757 Set_Etype (Id, Universal_Real);
2758 Set_Ekind (Id, E_Named_Real);
2760 else
2761 Wrong_Type (E, Any_Numeric);
2762 Resolve (E, T);
2764 Set_Etype (Id, T);
2765 Set_Ekind (Id, E_Constant);
2766 Set_Never_Set_In_Source (Id, True);
2767 Set_Is_True_Constant (Id, True);
2768 return;
2769 end if;
2771 if Nkind_In (E, N_Integer_Literal, N_Real_Literal) then
2772 Set_Etype (E, Etype (Id));
2773 end if;
2775 if not Is_OK_Static_Expression (E) then
2776 Flag_Non_Static_Expr
2777 ("non-static expression used in number declaration!", E);
2778 Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
2779 Set_Etype (E, Any_Type);
2780 end if;
2781 end Analyze_Number_Declaration;
2783 --------------------------------
2784 -- Analyze_Object_Declaration --
2785 --------------------------------
2787 procedure Analyze_Object_Declaration (N : Node_Id) is
2788 Loc : constant Source_Ptr := Sloc (N);
2789 Id : constant Entity_Id := Defining_Identifier (N);
2790 T : Entity_Id;
2791 Act_T : Entity_Id;
2793 E : Node_Id := Expression (N);
2794 -- E is set to Expression (N) throughout this routine. When
2795 -- Expression (N) is modified, E is changed accordingly.
2797 Prev_Entity : Entity_Id := Empty;
2799 function Count_Tasks (T : Entity_Id) return Uint;
2800 -- This function is called when a non-generic library level object of a
2801 -- task type is declared. Its function is to count the static number of
2802 -- tasks declared within the type (it is only called if Has_Tasks is set
2803 -- for T). As a side effect, if an array of tasks with non-static bounds
2804 -- or a variant record type is encountered, Check_Restrictions is called
2805 -- indicating the count is unknown.
2807 -----------------
2808 -- Count_Tasks --
2809 -----------------
2811 function Count_Tasks (T : Entity_Id) return Uint is
2812 C : Entity_Id;
2813 X : Node_Id;
2814 V : Uint;
2816 begin
2817 if Is_Task_Type (T) then
2818 return Uint_1;
2820 elsif Is_Record_Type (T) then
2821 if Has_Discriminants (T) then
2822 Check_Restriction (Max_Tasks, N);
2823 return Uint_0;
2825 else
2826 V := Uint_0;
2827 C := First_Component (T);
2828 while Present (C) loop
2829 V := V + Count_Tasks (Etype (C));
2830 Next_Component (C);
2831 end loop;
2833 return V;
2834 end if;
2836 elsif Is_Array_Type (T) then
2837 X := First_Index (T);
2838 V := Count_Tasks (Component_Type (T));
2839 while Present (X) loop
2840 C := Etype (X);
2842 if not Is_Static_Subtype (C) then
2843 Check_Restriction (Max_Tasks, N);
2844 return Uint_0;
2845 else
2846 V := V * (UI_Max (Uint_0,
2847 Expr_Value (Type_High_Bound (C)) -
2848 Expr_Value (Type_Low_Bound (C)) + Uint_1));
2849 end if;
2851 Next_Index (X);
2852 end loop;
2854 return V;
2856 else
2857 return Uint_0;
2858 end if;
2859 end Count_Tasks;
2861 -- Start of processing for Analyze_Object_Declaration
2863 begin
2864 -- There are three kinds of implicit types generated by an
2865 -- object declaration:
2867 -- 1. Those generated by the original Object Definition
2869 -- 2. Those generated by the Expression
2871 -- 3. Those used to constrain the Object Definition with the
2872 -- expression constraints when the definition is unconstrained.
2874 -- They must be generated in this order to avoid order of elaboration
2875 -- issues. Thus the first step (after entering the name) is to analyze
2876 -- the object definition.
2878 if Constant_Present (N) then
2879 Prev_Entity := Current_Entity_In_Scope (Id);
2881 if Present (Prev_Entity)
2882 and then
2884 -- If the homograph is an implicit subprogram, it is overridden
2885 -- by the current declaration.
2887 ((Is_Overloadable (Prev_Entity)
2888 and then Is_Inherited_Operation (Prev_Entity))
2890 -- The current object is a discriminal generated for an entry
2891 -- family index. Even though the index is a constant, in this
2892 -- particular context there is no true constant redeclaration.
2893 -- Enter_Name will handle the visibility.
2895 or else
2896 (Is_Discriminal (Id)
2897 and then Ekind (Discriminal_Link (Id)) =
2898 E_Entry_Index_Parameter)
2900 -- The current object is the renaming for a generic declared
2901 -- within the instance.
2903 or else
2904 (Ekind (Prev_Entity) = E_Package
2905 and then Nkind (Parent (Prev_Entity)) =
2906 N_Package_Renaming_Declaration
2907 and then not Comes_From_Source (Prev_Entity)
2908 and then Is_Generic_Instance (Renamed_Entity (Prev_Entity))))
2909 then
2910 Prev_Entity := Empty;
2911 end if;
2912 end if;
2914 if Present (Prev_Entity) then
2915 Constant_Redeclaration (Id, N, T);
2917 Generate_Reference (Prev_Entity, Id, 'c');
2918 Set_Completion_Referenced (Id);
2920 if Error_Posted (N) then
2922 -- Type mismatch or illegal redeclaration, Do not analyze
2923 -- expression to avoid cascaded errors.
2925 T := Find_Type_Of_Object (Object_Definition (N), N);
2926 Set_Etype (Id, T);
2927 Set_Ekind (Id, E_Variable);
2928 goto Leave;
2929 end if;
2931 -- In the normal case, enter identifier at the start to catch premature
2932 -- usage in the initialization expression.
2934 else
2935 Generate_Definition (Id);
2936 Enter_Name (Id);
2938 Mark_Coextensions (N, Object_Definition (N));
2940 T := Find_Type_Of_Object (Object_Definition (N), N);
2942 if Nkind (Object_Definition (N)) = N_Access_Definition
2943 and then Present
2944 (Access_To_Subprogram_Definition (Object_Definition (N)))
2945 and then Protected_Present
2946 (Access_To_Subprogram_Definition (Object_Definition (N)))
2947 then
2948 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
2949 end if;
2951 if Error_Posted (Id) then
2952 Set_Etype (Id, T);
2953 Set_Ekind (Id, E_Variable);
2954 goto Leave;
2955 end if;
2956 end if;
2958 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
2959 -- out some static checks
2961 if Ada_Version >= Ada_2005
2962 and then Can_Never_Be_Null (T)
2963 then
2964 -- In case of aggregates we must also take care of the correct
2965 -- initialization of nested aggregates bug this is done at the
2966 -- point of the analysis of the aggregate (see sem_aggr.adb)
2968 if Present (Expression (N))
2969 and then Nkind (Expression (N)) = N_Aggregate
2970 then
2971 null;
2973 else
2974 declare
2975 Save_Typ : constant Entity_Id := Etype (Id);
2976 begin
2977 Set_Etype (Id, T); -- Temp. decoration for static checks
2978 Null_Exclusion_Static_Checks (N);
2979 Set_Etype (Id, Save_Typ);
2980 end;
2981 end if;
2982 end if;
2984 -- Object is marked pure if it is in a pure scope
2986 Set_Is_Pure (Id, Is_Pure (Current_Scope));
2988 -- If deferred constant, make sure context is appropriate. We detect
2989 -- a deferred constant as a constant declaration with no expression.
2990 -- A deferred constant can appear in a package body if its completion
2991 -- is by means of an interface pragma.
2993 if Constant_Present (N) and then No (E) then
2995 -- A deferred constant may appear in the declarative part of the
2996 -- following constructs:
2998 -- blocks
2999 -- entry bodies
3000 -- extended return statements
3001 -- package specs
3002 -- package bodies
3003 -- subprogram bodies
3004 -- task bodies
3006 -- When declared inside a package spec, a deferred constant must be
3007 -- completed by a full constant declaration or pragma Import. In all
3008 -- other cases, the only proper completion is pragma Import. Extended
3009 -- return statements are flagged as invalid contexts because they do
3010 -- not have a declarative part and so cannot accommodate the pragma.
3012 if Ekind (Current_Scope) = E_Return_Statement then
3013 Error_Msg_N
3014 ("invalid context for deferred constant declaration (RM 7.4)",
3016 Error_Msg_N
3017 ("\declaration requires an initialization expression",
3019 Set_Constant_Present (N, False);
3021 -- In Ada 83, deferred constant must be of private type
3023 elsif not Is_Private_Type (T) then
3024 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
3025 Error_Msg_N
3026 ("(Ada 83) deferred constant must be private type", N);
3027 end if;
3028 end if;
3030 -- If not a deferred constant, then object declaration freezes its type
3032 else
3033 Check_Fully_Declared (T, N);
3034 Freeze_Before (N, T);
3035 end if;
3037 -- If the object was created by a constrained array definition, then
3038 -- set the link in both the anonymous base type and anonymous subtype
3039 -- that are built to represent the array type to point to the object.
3041 if Nkind (Object_Definition (Declaration_Node (Id))) =
3042 N_Constrained_Array_Definition
3043 then
3044 Set_Related_Array_Object (T, Id);
3045 Set_Related_Array_Object (Base_Type (T), Id);
3046 end if;
3048 -- Special checks for protected objects not at library level
3050 if Is_Protected_Type (T)
3051 and then not Is_Library_Level_Entity (Id)
3052 then
3053 Check_Restriction (No_Local_Protected_Objects, Id);
3055 -- Protected objects with interrupt handlers must be at library level
3057 -- Ada 2005: this test is not needed (and the corresponding clause
3058 -- in the RM is removed) because accessibility checks are sufficient
3059 -- to make handlers not at the library level illegal.
3061 -- AI05-0303: the AI is in fact a binding interpretation, and thus
3062 -- applies to the '95 version of the language as well.
3064 if Has_Interrupt_Handler (T) and then Ada_Version < Ada_95 then
3065 Error_Msg_N
3066 ("interrupt object can only be declared at library level", Id);
3067 end if;
3068 end if;
3070 -- The actual subtype of the object is the nominal subtype, unless
3071 -- the nominal one is unconstrained and obtained from the expression.
3073 Act_T := T;
3075 -- These checks should be performed before the initialization expression
3076 -- is considered, so that the Object_Definition node is still the same
3077 -- as in source code.
3079 -- In SPARK, the nominal subtype shall be given by a subtype mark and
3080 -- shall not be unconstrained. (The only exception to this is the
3081 -- admission of declarations of constants of type String.)
3083 if not
3084 Nkind_In (Object_Definition (N), N_Identifier, N_Expanded_Name)
3085 then
3086 Check_SPARK_Restriction
3087 ("subtype mark required", Object_Definition (N));
3089 elsif Is_Array_Type (T)
3090 and then not Is_Constrained (T)
3091 and then T /= Standard_String
3092 then
3093 Check_SPARK_Restriction
3094 ("subtype mark of constrained type expected",
3095 Object_Definition (N));
3096 end if;
3098 -- There are no aliased objects in SPARK
3100 if Aliased_Present (N) then
3101 Check_SPARK_Restriction ("aliased object is not allowed", N);
3102 end if;
3104 -- Process initialization expression if present and not in error
3106 if Present (E) and then E /= Error then
3108 -- Generate an error in case of CPP class-wide object initialization.
3109 -- Required because otherwise the expansion of the class-wide
3110 -- assignment would try to use 'size to initialize the object
3111 -- (primitive that is not available in CPP tagged types).
3113 if Is_Class_Wide_Type (Act_T)
3114 and then
3115 (Is_CPP_Class (Root_Type (Etype (Act_T)))
3116 or else
3117 (Present (Full_View (Root_Type (Etype (Act_T))))
3118 and then
3119 Is_CPP_Class (Full_View (Root_Type (Etype (Act_T))))))
3120 then
3121 Error_Msg_N
3122 ("predefined assignment not available for 'C'P'P tagged types",
3124 end if;
3126 Mark_Coextensions (N, E);
3127 Analyze (E);
3129 -- In case of errors detected in the analysis of the expression,
3130 -- decorate it with the expected type to avoid cascaded errors
3132 if No (Etype (E)) then
3133 Set_Etype (E, T);
3134 end if;
3136 -- If an initialization expression is present, then we set the
3137 -- Is_True_Constant flag. It will be reset if this is a variable
3138 -- and it is indeed modified.
3140 Set_Is_True_Constant (Id, True);
3142 -- If we are analyzing a constant declaration, set its completion
3143 -- flag after analyzing and resolving the expression.
3145 if Constant_Present (N) then
3146 Set_Has_Completion (Id);
3147 end if;
3149 -- Set type and resolve (type may be overridden later on). Note:
3150 -- Ekind (Id) must still be E_Void at this point so that incorrect
3151 -- early usage within E is properly diagnosed.
3153 Set_Etype (Id, T);
3154 Resolve (E, T);
3156 -- No further action needed if E is a call to an inlined function
3157 -- which returns an unconstrained type and it has been expanded into
3158 -- a procedure call. In that case N has been replaced by an object
3159 -- declaration without initializing expression and it has been
3160 -- analyzed (see Expand_Inlined_Call).
3162 if Debug_Flag_Dot_K
3163 and then Expander_Active
3164 and then Nkind (E) = N_Function_Call
3165 and then Nkind (Name (E)) in N_Has_Entity
3166 and then Is_Inlined (Entity (Name (E)))
3167 and then not Is_Constrained (Etype (E))
3168 and then Analyzed (N)
3169 and then No (Expression (N))
3170 then
3171 return;
3172 end if;
3174 -- If E is null and has been replaced by an N_Raise_Constraint_Error
3175 -- node (which was marked already-analyzed), we need to set the type
3176 -- to something other than Any_Access in order to keep gigi happy.
3178 if Etype (E) = Any_Access then
3179 Set_Etype (E, T);
3180 end if;
3182 -- If the object is an access to variable, the initialization
3183 -- expression cannot be an access to constant.
3185 if Is_Access_Type (T)
3186 and then not Is_Access_Constant (T)
3187 and then Is_Access_Type (Etype (E))
3188 and then Is_Access_Constant (Etype (E))
3189 then
3190 Error_Msg_N
3191 ("access to variable cannot be initialized "
3192 & "with an access-to-constant expression", E);
3193 end if;
3195 if not Assignment_OK (N) then
3196 Check_Initialization (T, E);
3197 end if;
3199 Check_Unset_Reference (E);
3201 -- If this is a variable, then set current value. If this is a
3202 -- declared constant of a scalar type with a static expression,
3203 -- indicate that it is always valid.
3205 if not Constant_Present (N) then
3206 if Compile_Time_Known_Value (E) then
3207 Set_Current_Value (Id, E);
3208 end if;
3210 elsif Is_Scalar_Type (T)
3211 and then Is_OK_Static_Expression (E)
3212 then
3213 Set_Is_Known_Valid (Id);
3214 end if;
3216 -- Deal with setting of null flags
3218 if Is_Access_Type (T) then
3219 if Known_Non_Null (E) then
3220 Set_Is_Known_Non_Null (Id, True);
3221 elsif Known_Null (E)
3222 and then not Can_Never_Be_Null (Id)
3223 then
3224 Set_Is_Known_Null (Id, True);
3225 end if;
3226 end if;
3228 -- Check incorrect use of dynamically tagged expressions.
3230 if Is_Tagged_Type (T) then
3231 Check_Dynamically_Tagged_Expression
3232 (Expr => E,
3233 Typ => T,
3234 Related_Nod => N);
3235 end if;
3237 Apply_Scalar_Range_Check (E, T);
3238 Apply_Static_Length_Check (E, T);
3240 if Nkind (Original_Node (N)) = N_Object_Declaration
3241 and then Comes_From_Source (Original_Node (N))
3243 -- Only call test if needed
3245 and then Restriction_Check_Required (SPARK_05)
3246 and then not Is_SPARK_Initialization_Expr (E)
3247 then
3248 Check_SPARK_Restriction
3249 ("initialization expression is not appropriate", E);
3250 end if;
3251 end if;
3253 -- If the No_Streams restriction is set, check that the type of the
3254 -- object is not, and does not contain, any subtype derived from
3255 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
3256 -- Has_Stream just for efficiency reasons. There is no point in
3257 -- spending time on a Has_Stream check if the restriction is not set.
3259 if Restriction_Check_Required (No_Streams) then
3260 if Has_Stream (T) then
3261 Check_Restriction (No_Streams, N);
3262 end if;
3263 end if;
3265 -- Deal with predicate check before we start to do major rewriting. It
3266 -- is OK to initialize and then check the initialized value, since the
3267 -- object goes out of scope if we get a predicate failure. Note that we
3268 -- do this in the analyzer and not the expander because the analyzer
3269 -- does some substantial rewriting in some cases.
3271 -- We need a predicate check if the type has predicates, and if either
3272 -- there is an initializing expression, or for default initialization
3273 -- when we have at least one case of an explicit default initial value.
3275 if not Suppress_Assignment_Checks (N)
3276 and then Present (Predicate_Function (T))
3277 and then
3278 (Present (E)
3279 or else
3280 Is_Partially_Initialized_Type (T, Include_Implicit => False))
3281 then
3282 -- If the type has a static predicate and the expression is known at
3283 -- compile time, see if the expression satisfies the predicate.
3285 if Present (E) then
3286 Check_Expression_Against_Static_Predicate (E, T);
3287 end if;
3289 Insert_After (N,
3290 Make_Predicate_Check (T, New_Occurrence_Of (Id, Loc)));
3291 end if;
3293 -- Case of unconstrained type
3295 if Is_Indefinite_Subtype (T) then
3297 -- In SPARK, a declaration of unconstrained type is allowed
3298 -- only for constants of type string.
3300 if Is_String_Type (T) and then not Constant_Present (N) then
3301 Check_SPARK_Restriction
3302 ("declaration of object of unconstrained type not allowed", N);
3303 end if;
3305 -- Nothing to do in deferred constant case
3307 if Constant_Present (N) and then No (E) then
3308 null;
3310 -- Case of no initialization present
3312 elsif No (E) then
3313 if No_Initialization (N) then
3314 null;
3316 elsif Is_Class_Wide_Type (T) then
3317 Error_Msg_N
3318 ("initialization required in class-wide declaration ", N);
3320 else
3321 Error_Msg_N
3322 ("unconstrained subtype not allowed (need initialization)",
3323 Object_Definition (N));
3325 if Is_Record_Type (T) and then Has_Discriminants (T) then
3326 Error_Msg_N
3327 ("\provide initial value or explicit discriminant values",
3328 Object_Definition (N));
3330 Error_Msg_NE
3331 ("\or give default discriminant values for type&",
3332 Object_Definition (N), T);
3334 elsif Is_Array_Type (T) then
3335 Error_Msg_N
3336 ("\provide initial value or explicit array bounds",
3337 Object_Definition (N));
3338 end if;
3339 end if;
3341 -- Case of initialization present but in error. Set initial
3342 -- expression as absent (but do not make above complaints)
3344 elsif E = Error then
3345 Set_Expression (N, Empty);
3346 E := Empty;
3348 -- Case of initialization present
3350 else
3351 -- Check restrictions in Ada 83
3353 if not Constant_Present (N) then
3355 -- Unconstrained variables not allowed in Ada 83 mode
3357 if Ada_Version = Ada_83
3358 and then Comes_From_Source (Object_Definition (N))
3359 then
3360 Error_Msg_N
3361 ("(Ada 83) unconstrained variable not allowed",
3362 Object_Definition (N));
3363 end if;
3364 end if;
3366 -- Now we constrain the variable from the initializing expression
3368 -- If the expression is an aggregate, it has been expanded into
3369 -- individual assignments. Retrieve the actual type from the
3370 -- expanded construct.
3372 if Is_Array_Type (T)
3373 and then No_Initialization (N)
3374 and then Nkind (Original_Node (E)) = N_Aggregate
3375 then
3376 Act_T := Etype (E);
3378 -- In case of class-wide interface object declarations we delay
3379 -- the generation of the equivalent record type declarations until
3380 -- its expansion because there are cases in they are not required.
3382 elsif Is_Interface (T) then
3383 null;
3385 else
3386 Expand_Subtype_From_Expr (N, T, Object_Definition (N), E);
3387 Act_T := Find_Type_Of_Object (Object_Definition (N), N);
3388 end if;
3390 Set_Is_Constr_Subt_For_U_Nominal (Act_T);
3392 if Aliased_Present (N) then
3393 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
3394 end if;
3396 Freeze_Before (N, Act_T);
3397 Freeze_Before (N, T);
3398 end if;
3400 elsif Is_Array_Type (T)
3401 and then No_Initialization (N)
3402 and then Nkind (Original_Node (E)) = N_Aggregate
3403 then
3404 if not Is_Entity_Name (Object_Definition (N)) then
3405 Act_T := Etype (E);
3406 Check_Compile_Time_Size (Act_T);
3408 if Aliased_Present (N) then
3409 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
3410 end if;
3411 end if;
3413 -- When the given object definition and the aggregate are specified
3414 -- independently, and their lengths might differ do a length check.
3415 -- This cannot happen if the aggregate is of the form (others =>...)
3417 if not Is_Constrained (T) then
3418 null;
3420 elsif Nkind (E) = N_Raise_Constraint_Error then
3422 -- Aggregate is statically illegal. Place back in declaration
3424 Set_Expression (N, E);
3425 Set_No_Initialization (N, False);
3427 elsif T = Etype (E) then
3428 null;
3430 elsif Nkind (E) = N_Aggregate
3431 and then Present (Component_Associations (E))
3432 and then Present (Choices (First (Component_Associations (E))))
3433 and then Nkind (First
3434 (Choices (First (Component_Associations (E))))) = N_Others_Choice
3435 then
3436 null;
3438 else
3439 Apply_Length_Check (E, T);
3440 end if;
3442 -- If the type is limited unconstrained with defaulted discriminants and
3443 -- there is no expression, then the object is constrained by the
3444 -- defaults, so it is worthwhile building the corresponding subtype.
3446 elsif (Is_Limited_Record (T) or else Is_Concurrent_Type (T))
3447 and then not Is_Constrained (T)
3448 and then Has_Discriminants (T)
3449 then
3450 if No (E) then
3451 Act_T := Build_Default_Subtype (T, N);
3452 else
3453 -- Ada 2005: a limited object may be initialized by means of an
3454 -- aggregate. If the type has default discriminants it has an
3455 -- unconstrained nominal type, Its actual subtype will be obtained
3456 -- from the aggregate, and not from the default discriminants.
3458 Act_T := Etype (E);
3459 end if;
3461 Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
3463 elsif Present (Underlying_Type (T))
3464 and then not Is_Constrained (Underlying_Type (T))
3465 and then Has_Discriminants (Underlying_Type (T))
3466 and then Nkind (E) = N_Function_Call
3467 and then Constant_Present (N)
3468 then
3469 -- The back-end has problems with constants of a discriminated type
3470 -- with defaults, if the initial value is a function call. We
3471 -- generate an intermediate temporary for the result of the call.
3472 -- It is unclear why this should make it acceptable to gcc. ???
3474 Remove_Side_Effects (E);
3476 -- If this is a constant declaration of an unconstrained type and
3477 -- the initialization is an aggregate, we can use the subtype of the
3478 -- aggregate for the declared entity because it is immutable.
3480 elsif not Is_Constrained (T)
3481 and then Has_Discriminants (T)
3482 and then Constant_Present (N)
3483 and then not Has_Unchecked_Union (T)
3484 and then Nkind (E) = N_Aggregate
3485 then
3486 Act_T := Etype (E);
3487 end if;
3489 -- Check No_Wide_Characters restriction
3491 Check_Wide_Character_Restriction (T, Object_Definition (N));
3493 -- Indicate this is not set in source. Certainly true for constants, and
3494 -- true for variables so far (will be reset for a variable if and when
3495 -- we encounter a modification in the source).
3497 Set_Never_Set_In_Source (Id, True);
3499 -- Now establish the proper kind and type of the object
3501 if Constant_Present (N) then
3502 Set_Ekind (Id, E_Constant);
3503 Set_Is_True_Constant (Id, True);
3505 else
3506 Set_Ekind (Id, E_Variable);
3508 -- A variable is set as shared passive if it appears in a shared
3509 -- passive package, and is at the outer level. This is not done for
3510 -- entities generated during expansion, because those are always
3511 -- manipulated locally.
3513 if Is_Shared_Passive (Current_Scope)
3514 and then Is_Library_Level_Entity (Id)
3515 and then Comes_From_Source (Id)
3516 then
3517 Set_Is_Shared_Passive (Id);
3518 Check_Shared_Var (Id, T, N);
3519 end if;
3521 -- Set Has_Initial_Value if initializing expression present. Note
3522 -- that if there is no initializing expression, we leave the state
3523 -- of this flag unchanged (usually it will be False, but notably in
3524 -- the case of exception choice variables, it will already be true).
3526 if Present (E) then
3527 Set_Has_Initial_Value (Id, True);
3528 end if;
3529 end if;
3531 -- Initialize alignment and size and capture alignment setting
3533 Init_Alignment (Id);
3534 Init_Esize (Id);
3535 Set_Optimize_Alignment_Flags (Id);
3537 -- Deal with aliased case
3539 if Aliased_Present (N) then
3540 Set_Is_Aliased (Id);
3542 -- If the object is aliased and the type is unconstrained with
3543 -- defaulted discriminants and there is no expression, then the
3544 -- object is constrained by the defaults, so it is worthwhile
3545 -- building the corresponding subtype.
3547 -- Ada 2005 (AI-363): If the aliased object is discriminated and
3548 -- unconstrained, then only establish an actual subtype if the
3549 -- nominal subtype is indefinite. In definite cases the object is
3550 -- unconstrained in Ada 2005.
3552 if No (E)
3553 and then Is_Record_Type (T)
3554 and then not Is_Constrained (T)
3555 and then Has_Discriminants (T)
3556 and then (Ada_Version < Ada_2005 or else Is_Indefinite_Subtype (T))
3557 then
3558 Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
3559 end if;
3560 end if;
3562 -- Now we can set the type of the object
3564 Set_Etype (Id, Act_T);
3566 -- Object is marked to be treated as volatile if type is volatile and
3567 -- we clear the Current_Value setting that may have been set above.
3569 if Treat_As_Volatile (Etype (Id)) then
3570 Set_Treat_As_Volatile (Id);
3571 Set_Current_Value (Id, Empty);
3572 end if;
3574 -- Deal with controlled types
3576 if Has_Controlled_Component (Etype (Id))
3577 or else Is_Controlled (Etype (Id))
3578 then
3579 if not Is_Library_Level_Entity (Id) then
3580 Check_Restriction (No_Nested_Finalization, N);
3581 else
3582 Validate_Controlled_Object (Id);
3583 end if;
3584 end if;
3586 if Has_Task (Etype (Id)) then
3587 Check_Restriction (No_Tasking, N);
3589 -- Deal with counting max tasks
3591 -- Nothing to do if inside a generic
3593 if Inside_A_Generic then
3594 null;
3596 -- If library level entity, then count tasks
3598 elsif Is_Library_Level_Entity (Id) then
3599 Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
3601 -- If not library level entity, then indicate we don't know max
3602 -- tasks and also check task hierarchy restriction and blocking
3603 -- operation (since starting a task is definitely blocking!)
3605 else
3606 Check_Restriction (Max_Tasks, N);
3607 Check_Restriction (No_Task_Hierarchy, N);
3608 Check_Potentially_Blocking_Operation (N);
3609 end if;
3611 -- A rather specialized test. If we see two tasks being declared
3612 -- of the same type in the same object declaration, and the task
3613 -- has an entry with an address clause, we know that program error
3614 -- will be raised at run time since we can't have two tasks with
3615 -- entries at the same address.
3617 if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
3618 declare
3619 E : Entity_Id;
3621 begin
3622 E := First_Entity (Etype (Id));
3623 while Present (E) loop
3624 if Ekind (E) = E_Entry
3625 and then Present (Get_Attribute_Definition_Clause
3626 (E, Attribute_Address))
3627 then
3628 Error_Msg_N
3629 ("??more than one task with same entry address", N);
3630 Error_Msg_N
3631 ("\??Program_Error will be raised at run time", N);
3632 Insert_Action (N,
3633 Make_Raise_Program_Error (Loc,
3634 Reason => PE_Duplicated_Entry_Address));
3635 exit;
3636 end if;
3638 Next_Entity (E);
3639 end loop;
3640 end;
3641 end if;
3642 end if;
3644 -- Some simple constant-propagation: if the expression is a constant
3645 -- string initialized with a literal, share the literal. This avoids
3646 -- a run-time copy.
3648 if Present (E)
3649 and then Is_Entity_Name (E)
3650 and then Ekind (Entity (E)) = E_Constant
3651 and then Base_Type (Etype (E)) = Standard_String
3652 then
3653 declare
3654 Val : constant Node_Id := Constant_Value (Entity (E));
3655 begin
3656 if Present (Val)
3657 and then Nkind (Val) = N_String_Literal
3658 then
3659 Rewrite (E, New_Copy (Val));
3660 end if;
3661 end;
3662 end if;
3664 -- Another optimization: if the nominal subtype is unconstrained and
3665 -- the expression is a function call that returns an unconstrained
3666 -- type, rewrite the declaration as a renaming of the result of the
3667 -- call. The exceptions below are cases where the copy is expected,
3668 -- either by the back end (Aliased case) or by the semantics, as for
3669 -- initializing controlled types or copying tags for classwide types.
3671 if Present (E)
3672 and then Nkind (E) = N_Explicit_Dereference
3673 and then Nkind (Original_Node (E)) = N_Function_Call
3674 and then not Is_Library_Level_Entity (Id)
3675 and then not Is_Constrained (Underlying_Type (T))
3676 and then not Is_Aliased (Id)
3677 and then not Is_Class_Wide_Type (T)
3678 and then not Is_Controlled (T)
3679 and then not Has_Controlled_Component (Base_Type (T))
3680 and then Expander_Active
3681 then
3682 Rewrite (N,
3683 Make_Object_Renaming_Declaration (Loc,
3684 Defining_Identifier => Id,
3685 Access_Definition => Empty,
3686 Subtype_Mark => New_Occurrence_Of
3687 (Base_Type (Etype (Id)), Loc),
3688 Name => E));
3690 Set_Renamed_Object (Id, E);
3692 -- Force generation of debugging information for the constant and for
3693 -- the renamed function call.
3695 Set_Debug_Info_Needed (Id);
3696 Set_Debug_Info_Needed (Entity (Prefix (E)));
3697 end if;
3699 if Present (Prev_Entity)
3700 and then Is_Frozen (Prev_Entity)
3701 and then not Error_Posted (Id)
3702 then
3703 Error_Msg_N ("full constant declaration appears too late", N);
3704 end if;
3706 Check_Eliminated (Id);
3708 -- Deal with setting In_Private_Part flag if in private part
3710 if Ekind (Scope (Id)) = E_Package
3711 and then In_Private_Part (Scope (Id))
3712 then
3713 Set_In_Private_Part (Id);
3714 end if;
3716 -- Check for violation of No_Local_Timing_Events
3718 if Restriction_Check_Required (No_Local_Timing_Events)
3719 and then not Is_Library_Level_Entity (Id)
3720 and then Is_RTE (Etype (Id), RE_Timing_Event)
3721 then
3722 Check_Restriction (No_Local_Timing_Events, N);
3723 end if;
3725 <<Leave>>
3726 if Has_Aspects (N) then
3727 Analyze_Aspect_Specifications (N, Id);
3728 end if;
3730 Analyze_Dimension (N);
3732 -- Verify whether the object declaration introduces an illegal hidden
3733 -- state within a package subject to a null abstract state.
3735 if Formal_Extensions and then Ekind (Id) = E_Variable then
3736 Check_No_Hidden_State (Id);
3737 end if;
3738 end Analyze_Object_Declaration;
3740 ---------------------------
3741 -- Analyze_Others_Choice --
3742 ---------------------------
3744 -- Nothing to do for the others choice node itself, the semantic analysis
3745 -- of the others choice will occur as part of the processing of the parent
3747 procedure Analyze_Others_Choice (N : Node_Id) is
3748 pragma Warnings (Off, N);
3749 begin
3750 null;
3751 end Analyze_Others_Choice;
3753 -------------------------------------------
3754 -- Analyze_Private_Extension_Declaration --
3755 -------------------------------------------
3757 procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
3758 T : constant Entity_Id := Defining_Identifier (N);
3759 Indic : constant Node_Id := Subtype_Indication (N);
3760 Parent_Type : Entity_Id;
3761 Parent_Base : Entity_Id;
3763 begin
3764 -- Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
3766 if Is_Non_Empty_List (Interface_List (N)) then
3767 declare
3768 Intf : Node_Id;
3769 T : Entity_Id;
3771 begin
3772 Intf := First (Interface_List (N));
3773 while Present (Intf) loop
3774 T := Find_Type_Of_Subtype_Indic (Intf);
3776 Diagnose_Interface (Intf, T);
3777 Next (Intf);
3778 end loop;
3779 end;
3780 end if;
3782 Generate_Definition (T);
3784 -- For other than Ada 2012, just enter the name in the current scope
3786 if Ada_Version < Ada_2012 then
3787 Enter_Name (T);
3789 -- Ada 2012 (AI05-0162): Enter the name in the current scope handling
3790 -- case of private type that completes an incomplete type.
3792 else
3793 declare
3794 Prev : Entity_Id;
3796 begin
3797 Prev := Find_Type_Name (N);
3799 pragma Assert (Prev = T
3800 or else (Ekind (Prev) = E_Incomplete_Type
3801 and then Present (Full_View (Prev))
3802 and then Full_View (Prev) = T));
3803 end;
3804 end if;
3806 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
3807 Parent_Base := Base_Type (Parent_Type);
3809 if Parent_Type = Any_Type
3810 or else Etype (Parent_Type) = Any_Type
3811 then
3812 Set_Ekind (T, Ekind (Parent_Type));
3813 Set_Etype (T, Any_Type);
3814 goto Leave;
3816 elsif not Is_Tagged_Type (Parent_Type) then
3817 Error_Msg_N
3818 ("parent of type extension must be a tagged type ", Indic);
3819 goto Leave;
3821 elsif Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
3822 Error_Msg_N ("premature derivation of incomplete type", Indic);
3823 goto Leave;
3825 elsif Is_Concurrent_Type (Parent_Type) then
3826 Error_Msg_N
3827 ("parent type of a private extension cannot be "
3828 & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
3830 Set_Etype (T, Any_Type);
3831 Set_Ekind (T, E_Limited_Private_Type);
3832 Set_Private_Dependents (T, New_Elmt_List);
3833 Set_Error_Posted (T);
3834 goto Leave;
3835 end if;
3837 -- Perhaps the parent type should be changed to the class-wide type's
3838 -- specific type in this case to prevent cascading errors ???
3840 if Is_Class_Wide_Type (Parent_Type) then
3841 Error_Msg_N
3842 ("parent of type extension must not be a class-wide type", Indic);
3843 goto Leave;
3844 end if;
3846 if (not Is_Package_Or_Generic_Package (Current_Scope)
3847 and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
3848 or else In_Private_Part (Current_Scope)
3850 then
3851 Error_Msg_N ("invalid context for private extension", N);
3852 end if;
3854 -- Set common attributes
3856 Set_Is_Pure (T, Is_Pure (Current_Scope));
3857 Set_Scope (T, Current_Scope);
3858 Set_Ekind (T, E_Record_Type_With_Private);
3859 Init_Size_Align (T);
3861 Set_Etype (T, Parent_Base);
3862 Set_Has_Task (T, Has_Task (Parent_Base));
3864 Set_Convention (T, Convention (Parent_Type));
3865 Set_First_Rep_Item (T, First_Rep_Item (Parent_Type));
3866 Set_Is_First_Subtype (T);
3867 Make_Class_Wide_Type (T);
3869 if Unknown_Discriminants_Present (N) then
3870 Set_Discriminant_Constraint (T, No_Elist);
3871 end if;
3873 Build_Derived_Record_Type (N, Parent_Type, T);
3875 -- Propagate inherited invariant information. The new type has
3876 -- invariants, if the parent type has inheritable invariants,
3877 -- and these invariants can in turn be inherited.
3879 if Has_Inheritable_Invariants (Parent_Type) then
3880 Set_Has_Inheritable_Invariants (T);
3881 Set_Has_Invariants (T);
3882 end if;
3884 -- Ada 2005 (AI-443): Synchronized private extension or a rewritten
3885 -- synchronized formal derived type.
3887 if Ada_Version >= Ada_2005
3888 and then Synchronized_Present (N)
3889 then
3890 Set_Is_Limited_Record (T);
3892 -- Formal derived type case
3894 if Is_Generic_Type (T) then
3896 -- The parent must be a tagged limited type or a synchronized
3897 -- interface.
3899 if (not Is_Tagged_Type (Parent_Type)
3900 or else not Is_Limited_Type (Parent_Type))
3901 and then
3902 (not Is_Interface (Parent_Type)
3903 or else not Is_Synchronized_Interface (Parent_Type))
3904 then
3905 Error_Msg_NE ("parent type of & must be tagged limited " &
3906 "or synchronized", N, T);
3907 end if;
3909 -- The progenitors (if any) must be limited or synchronized
3910 -- interfaces.
3912 if Present (Interfaces (T)) then
3913 declare
3914 Iface : Entity_Id;
3915 Iface_Elmt : Elmt_Id;
3917 begin
3918 Iface_Elmt := First_Elmt (Interfaces (T));
3919 while Present (Iface_Elmt) loop
3920 Iface := Node (Iface_Elmt);
3922 if not Is_Limited_Interface (Iface)
3923 and then not Is_Synchronized_Interface (Iface)
3924 then
3925 Error_Msg_NE ("progenitor & must be limited " &
3926 "or synchronized", N, Iface);
3927 end if;
3929 Next_Elmt (Iface_Elmt);
3930 end loop;
3931 end;
3932 end if;
3934 -- Regular derived extension, the parent must be a limited or
3935 -- synchronized interface.
3937 else
3938 if not Is_Interface (Parent_Type)
3939 or else (not Is_Limited_Interface (Parent_Type)
3940 and then
3941 not Is_Synchronized_Interface (Parent_Type))
3942 then
3943 Error_Msg_NE
3944 ("parent type of & must be limited interface", N, T);
3945 end if;
3946 end if;
3948 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
3949 -- extension with a synchronized parent must be explicitly declared
3950 -- synchronized, because the full view will be a synchronized type.
3951 -- This must be checked before the check for limited types below,
3952 -- to ensure that types declared limited are not allowed to extend
3953 -- synchronized interfaces.
3955 elsif Is_Interface (Parent_Type)
3956 and then Is_Synchronized_Interface (Parent_Type)
3957 and then not Synchronized_Present (N)
3958 then
3959 Error_Msg_NE
3960 ("private extension of& must be explicitly synchronized",
3961 N, Parent_Type);
3963 elsif Limited_Present (N) then
3964 Set_Is_Limited_Record (T);
3966 if not Is_Limited_Type (Parent_Type)
3967 and then
3968 (not Is_Interface (Parent_Type)
3969 or else not Is_Limited_Interface (Parent_Type))
3970 then
3971 Error_Msg_NE ("parent type& of limited extension must be limited",
3972 N, Parent_Type);
3973 end if;
3974 end if;
3976 <<Leave>>
3977 if Has_Aspects (N) then
3978 Analyze_Aspect_Specifications (N, T);
3979 end if;
3980 end Analyze_Private_Extension_Declaration;
3982 ---------------------------------
3983 -- Analyze_Subtype_Declaration --
3984 ---------------------------------
3986 procedure Analyze_Subtype_Declaration
3987 (N : Node_Id;
3988 Skip : Boolean := False)
3990 Id : constant Entity_Id := Defining_Identifier (N);
3991 T : Entity_Id;
3992 R_Checks : Check_Result;
3994 begin
3995 Generate_Definition (Id);
3996 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3997 Init_Size_Align (Id);
3999 -- The following guard condition on Enter_Name is to handle cases where
4000 -- the defining identifier has already been entered into the scope but
4001 -- the declaration as a whole needs to be analyzed.
4003 -- This case in particular happens for derived enumeration types. The
4004 -- derived enumeration type is processed as an inserted enumeration type
4005 -- declaration followed by a rewritten subtype declaration. The defining
4006 -- identifier, however, is entered into the name scope very early in the
4007 -- processing of the original type declaration and therefore needs to be
4008 -- avoided here, when the created subtype declaration is analyzed. (See
4009 -- Build_Derived_Types)
4011 -- This also happens when the full view of a private type is derived
4012 -- type with constraints. In this case the entity has been introduced
4013 -- in the private declaration.
4015 if Skip
4016 or else (Present (Etype (Id))
4017 and then (Is_Private_Type (Etype (Id))
4018 or else Is_Task_Type (Etype (Id))
4019 or else Is_Rewrite_Substitution (N)))
4020 then
4021 null;
4023 else
4024 Enter_Name (Id);
4025 end if;
4027 T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
4029 -- Class-wide equivalent types of records with unknown discriminants
4030 -- involve the generation of an itype which serves as the private view
4031 -- of a constrained record subtype. In such cases the base type of the
4032 -- current subtype we are processing is the private itype. Use the full
4033 -- of the private itype when decorating various attributes.
4035 if Is_Itype (T)
4036 and then Is_Private_Type (T)
4037 and then Present (Full_View (T))
4038 then
4039 T := Full_View (T);
4040 end if;
4042 -- Inherit common attributes
4044 Set_Is_Volatile (Id, Is_Volatile (T));
4045 Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
4046 Set_Is_Generic_Type (Id, Is_Generic_Type (Base_Type (T)));
4047 Set_Convention (Id, Convention (T));
4049 -- If ancestor has predicates then so does the subtype, and in addition
4050 -- we must delay the freeze to properly arrange predicate inheritance.
4052 -- The Ancestor_Type test is a big kludge, there seem to be cases in
4053 -- which T = ID, so the above tests and assignments do nothing???
4055 if Has_Predicates (T)
4056 or else (Present (Ancestor_Subtype (T))
4057 and then Has_Predicates (Ancestor_Subtype (T)))
4058 then
4059 Set_Has_Predicates (Id);
4060 Set_Has_Delayed_Freeze (Id);
4061 end if;
4063 -- Subtype of Boolean cannot have a constraint in SPARK
4065 if Is_Boolean_Type (T)
4066 and then Nkind (Subtype_Indication (N)) = N_Subtype_Indication
4067 then
4068 Check_SPARK_Restriction
4069 ("subtype of Boolean cannot have constraint", N);
4070 end if;
4072 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
4073 declare
4074 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
4075 One_Cstr : Node_Id;
4076 Low : Node_Id;
4077 High : Node_Id;
4079 begin
4080 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint then
4081 One_Cstr := First (Constraints (Cstr));
4082 while Present (One_Cstr) loop
4084 -- Index or discriminant constraint in SPARK must be a
4085 -- subtype mark.
4087 if not
4088 Nkind_In (One_Cstr, N_Identifier, N_Expanded_Name)
4089 then
4090 Check_SPARK_Restriction
4091 ("subtype mark required", One_Cstr);
4093 -- String subtype must have a lower bound of 1 in SPARK.
4094 -- Note that we do not need to test for the non-static case
4095 -- here, since that was already taken care of in
4096 -- Process_Range_Expr_In_Decl.
4098 elsif Base_Type (T) = Standard_String then
4099 Get_Index_Bounds (One_Cstr, Low, High);
4101 if Is_OK_Static_Expression (Low)
4102 and then Expr_Value (Low) /= 1
4103 then
4104 Check_SPARK_Restriction
4105 ("String subtype must have lower bound of 1", N);
4106 end if;
4107 end if;
4109 Next (One_Cstr);
4110 end loop;
4111 end if;
4112 end;
4113 end if;
4115 -- In the case where there is no constraint given in the subtype
4116 -- indication, Process_Subtype just returns the Subtype_Mark, so its
4117 -- semantic attributes must be established here.
4119 if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
4120 Set_Etype (Id, Base_Type (T));
4122 -- Subtype of unconstrained array without constraint is not allowed
4123 -- in SPARK.
4125 if Is_Array_Type (T)
4126 and then not Is_Constrained (T)
4127 then
4128 Check_SPARK_Restriction
4129 ("subtype of unconstrained array must have constraint", N);
4130 end if;
4132 case Ekind (T) is
4133 when Array_Kind =>
4134 Set_Ekind (Id, E_Array_Subtype);
4135 Copy_Array_Subtype_Attributes (Id, T);
4137 when Decimal_Fixed_Point_Kind =>
4138 Set_Ekind (Id, E_Decimal_Fixed_Point_Subtype);
4139 Set_Digits_Value (Id, Digits_Value (T));
4140 Set_Delta_Value (Id, Delta_Value (T));
4141 Set_Scale_Value (Id, Scale_Value (T));
4142 Set_Small_Value (Id, Small_Value (T));
4143 Set_Scalar_Range (Id, Scalar_Range (T));
4144 Set_Machine_Radix_10 (Id, Machine_Radix_10 (T));
4145 Set_Is_Constrained (Id, Is_Constrained (T));
4146 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4147 Set_RM_Size (Id, RM_Size (T));
4149 when Enumeration_Kind =>
4150 Set_Ekind (Id, E_Enumeration_Subtype);
4151 Set_First_Literal (Id, First_Literal (Base_Type (T)));
4152 Set_Scalar_Range (Id, Scalar_Range (T));
4153 Set_Is_Character_Type (Id, Is_Character_Type (T));
4154 Set_Is_Constrained (Id, Is_Constrained (T));
4155 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4156 Set_RM_Size (Id, RM_Size (T));
4158 when Ordinary_Fixed_Point_Kind =>
4159 Set_Ekind (Id, E_Ordinary_Fixed_Point_Subtype);
4160 Set_Scalar_Range (Id, Scalar_Range (T));
4161 Set_Small_Value (Id, Small_Value (T));
4162 Set_Delta_Value (Id, Delta_Value (T));
4163 Set_Is_Constrained (Id, Is_Constrained (T));
4164 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4165 Set_RM_Size (Id, RM_Size (T));
4167 when Float_Kind =>
4168 Set_Ekind (Id, E_Floating_Point_Subtype);
4169 Set_Scalar_Range (Id, Scalar_Range (T));
4170 Set_Digits_Value (Id, Digits_Value (T));
4171 Set_Is_Constrained (Id, Is_Constrained (T));
4173 when Signed_Integer_Kind =>
4174 Set_Ekind (Id, E_Signed_Integer_Subtype);
4175 Set_Scalar_Range (Id, Scalar_Range (T));
4176 Set_Is_Constrained (Id, Is_Constrained (T));
4177 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4178 Set_RM_Size (Id, RM_Size (T));
4180 when Modular_Integer_Kind =>
4181 Set_Ekind (Id, E_Modular_Integer_Subtype);
4182 Set_Scalar_Range (Id, Scalar_Range (T));
4183 Set_Is_Constrained (Id, Is_Constrained (T));
4184 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4185 Set_RM_Size (Id, RM_Size (T));
4187 when Class_Wide_Kind =>
4188 Set_Ekind (Id, E_Class_Wide_Subtype);
4189 Set_First_Entity (Id, First_Entity (T));
4190 Set_Last_Entity (Id, Last_Entity (T));
4191 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
4192 Set_Cloned_Subtype (Id, T);
4193 Set_Is_Tagged_Type (Id, True);
4194 Set_Has_Unknown_Discriminants
4195 (Id, True);
4197 if Ekind (T) = E_Class_Wide_Subtype then
4198 Set_Equivalent_Type (Id, Equivalent_Type (T));
4199 end if;
4201 when E_Record_Type | E_Record_Subtype =>
4202 Set_Ekind (Id, E_Record_Subtype);
4204 if Ekind (T) = E_Record_Subtype
4205 and then Present (Cloned_Subtype (T))
4206 then
4207 Set_Cloned_Subtype (Id, Cloned_Subtype (T));
4208 else
4209 Set_Cloned_Subtype (Id, T);
4210 end if;
4212 Set_First_Entity (Id, First_Entity (T));
4213 Set_Last_Entity (Id, Last_Entity (T));
4214 Set_Has_Discriminants (Id, Has_Discriminants (T));
4215 Set_Is_Constrained (Id, Is_Constrained (T));
4216 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
4217 Set_Has_Implicit_Dereference
4218 (Id, Has_Implicit_Dereference (T));
4219 Set_Has_Unknown_Discriminants
4220 (Id, Has_Unknown_Discriminants (T));
4222 if Has_Discriminants (T) then
4223 Set_Discriminant_Constraint
4224 (Id, Discriminant_Constraint (T));
4225 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
4227 elsif Has_Unknown_Discriminants (Id) then
4228 Set_Discriminant_Constraint (Id, No_Elist);
4229 end if;
4231 if Is_Tagged_Type (T) then
4232 Set_Is_Tagged_Type (Id);
4233 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
4234 Set_Direct_Primitive_Operations
4235 (Id, Direct_Primitive_Operations (T));
4236 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
4238 if Is_Interface (T) then
4239 Set_Is_Interface (Id);
4240 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
4241 end if;
4242 end if;
4244 when Private_Kind =>
4245 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
4246 Set_Has_Discriminants (Id, Has_Discriminants (T));
4247 Set_Is_Constrained (Id, Is_Constrained (T));
4248 Set_First_Entity (Id, First_Entity (T));
4249 Set_Last_Entity (Id, Last_Entity (T));
4250 Set_Private_Dependents (Id, New_Elmt_List);
4251 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
4252 Set_Has_Implicit_Dereference
4253 (Id, Has_Implicit_Dereference (T));
4254 Set_Has_Unknown_Discriminants
4255 (Id, Has_Unknown_Discriminants (T));
4256 Set_Known_To_Have_Preelab_Init
4257 (Id, Known_To_Have_Preelab_Init (T));
4259 if Is_Tagged_Type (T) then
4260 Set_Is_Tagged_Type (Id);
4261 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
4262 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
4263 Set_Direct_Primitive_Operations (Id,
4264 Direct_Primitive_Operations (T));
4265 end if;
4267 -- In general the attributes of the subtype of a private type
4268 -- are the attributes of the partial view of parent. However,
4269 -- the full view may be a discriminated type, and the subtype
4270 -- must share the discriminant constraint to generate correct
4271 -- calls to initialization procedures.
4273 if Has_Discriminants (T) then
4274 Set_Discriminant_Constraint
4275 (Id, Discriminant_Constraint (T));
4276 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
4278 elsif Present (Full_View (T))
4279 and then Has_Discriminants (Full_View (T))
4280 then
4281 Set_Discriminant_Constraint
4282 (Id, Discriminant_Constraint (Full_View (T)));
4283 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
4285 -- This would seem semantically correct, but apparently
4286 -- generates spurious errors about missing components ???
4288 -- Set_Has_Discriminants (Id);
4289 end if;
4291 Prepare_Private_Subtype_Completion (Id, N);
4293 -- If this is the subtype of a constrained private type with
4294 -- discriminants that has got a full view and we also have
4295 -- built a completion just above, show that the completion
4296 -- is a clone of the full view to the back-end.
4298 if Has_Discriminants (T)
4299 and then not Has_Unknown_Discriminants (T)
4300 and then not Is_Empty_Elmt_List (Discriminant_Constraint (T))
4301 and then Present (Full_View (T))
4302 and then Present (Full_View (Id))
4303 then
4304 Set_Cloned_Subtype (Full_View (Id), Full_View (T));
4305 end if;
4307 when Access_Kind =>
4308 Set_Ekind (Id, E_Access_Subtype);
4309 Set_Is_Constrained (Id, Is_Constrained (T));
4310 Set_Is_Access_Constant
4311 (Id, Is_Access_Constant (T));
4312 Set_Directly_Designated_Type
4313 (Id, Designated_Type (T));
4314 Set_Can_Never_Be_Null (Id, Can_Never_Be_Null (T));
4316 -- A Pure library_item must not contain the declaration of a
4317 -- named access type, except within a subprogram, generic
4318 -- subprogram, task unit, or protected unit, or if it has
4319 -- a specified Storage_Size of zero (RM05-10.2.1(15.4-15.5)).
4321 if Comes_From_Source (Id)
4322 and then In_Pure_Unit
4323 and then not In_Subprogram_Task_Protected_Unit
4324 and then not No_Pool_Assigned (Id)
4325 then
4326 Error_Msg_N
4327 ("named access types not allowed in pure unit", N);
4328 end if;
4330 when Concurrent_Kind =>
4331 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
4332 Set_Corresponding_Record_Type (Id,
4333 Corresponding_Record_Type (T));
4334 Set_First_Entity (Id, First_Entity (T));
4335 Set_First_Private_Entity (Id, First_Private_Entity (T));
4336 Set_Has_Discriminants (Id, Has_Discriminants (T));
4337 Set_Is_Constrained (Id, Is_Constrained (T));
4338 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
4339 Set_Last_Entity (Id, Last_Entity (T));
4341 if Has_Discriminants (T) then
4342 Set_Discriminant_Constraint (Id,
4343 Discriminant_Constraint (T));
4344 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
4345 end if;
4347 when E_Incomplete_Type =>
4348 if Ada_Version >= Ada_2005 then
4350 -- In Ada 2005 an incomplete type can be explicitly tagged:
4351 -- propagate indication.
4353 Set_Ekind (Id, E_Incomplete_Subtype);
4354 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
4355 Set_Private_Dependents (Id, New_Elmt_List);
4357 -- Ada 2005 (AI-412): Decorate an incomplete subtype of an
4358 -- incomplete type visible through a limited with clause.
4360 if From_With_Type (T)
4361 and then Present (Non_Limited_View (T))
4362 then
4363 Set_From_With_Type (Id);
4364 Set_Non_Limited_View (Id, Non_Limited_View (T));
4366 -- Ada 2005 (AI-412): Add the regular incomplete subtype
4367 -- to the private dependents of the original incomplete
4368 -- type for future transformation.
4370 else
4371 Append_Elmt (Id, Private_Dependents (T));
4372 end if;
4374 -- If the subtype name denotes an incomplete type an error
4375 -- was already reported by Process_Subtype.
4377 else
4378 Set_Etype (Id, Any_Type);
4379 end if;
4381 when others =>
4382 raise Program_Error;
4383 end case;
4384 end if;
4386 if Etype (Id) = Any_Type then
4387 goto Leave;
4388 end if;
4390 -- Some common processing on all types
4392 Set_Size_Info (Id, T);
4393 Set_First_Rep_Item (Id, First_Rep_Item (T));
4395 -- If the parent type is a generic actual, so is the subtype. This may
4396 -- happen in a nested instance. Why Comes_From_Source test???
4398 if not Comes_From_Source (N) then
4399 Set_Is_Generic_Actual_Type (Id, Is_Generic_Actual_Type (T));
4400 end if;
4402 T := Etype (Id);
4404 Set_Is_Immediately_Visible (Id, True);
4405 Set_Depends_On_Private (Id, Has_Private_Component (T));
4406 Set_Is_Descendent_Of_Address (Id, Is_Descendent_Of_Address (T));
4408 if Is_Interface (T) then
4409 Set_Is_Interface (Id);
4410 end if;
4412 if Present (Generic_Parent_Type (N))
4413 and then
4414 (Nkind
4415 (Parent (Generic_Parent_Type (N))) /= N_Formal_Type_Declaration
4416 or else Nkind
4417 (Formal_Type_Definition (Parent (Generic_Parent_Type (N))))
4418 /= N_Formal_Private_Type_Definition)
4419 then
4420 if Is_Tagged_Type (Id) then
4422 -- If this is a generic actual subtype for a synchronized type,
4423 -- the primitive operations are those of the corresponding record
4424 -- for which there is a separate subtype declaration.
4426 if Is_Concurrent_Type (Id) then
4427 null;
4428 elsif Is_Class_Wide_Type (Id) then
4429 Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
4430 else
4431 Derive_Subprograms (Generic_Parent_Type (N), Id, T);
4432 end if;
4434 elsif Scope (Etype (Id)) /= Standard_Standard then
4435 Derive_Subprograms (Generic_Parent_Type (N), Id);
4436 end if;
4437 end if;
4439 if Is_Private_Type (T)
4440 and then Present (Full_View (T))
4441 then
4442 Conditional_Delay (Id, Full_View (T));
4444 -- The subtypes of components or subcomponents of protected types
4445 -- do not need freeze nodes, which would otherwise appear in the
4446 -- wrong scope (before the freeze node for the protected type). The
4447 -- proper subtypes are those of the subcomponents of the corresponding
4448 -- record.
4450 elsif Ekind (Scope (Id)) /= E_Protected_Type
4451 and then Present (Scope (Scope (Id))) -- error defense!
4452 and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
4453 then
4454 Conditional_Delay (Id, T);
4455 end if;
4457 -- Check that Constraint_Error is raised for a scalar subtype indication
4458 -- when the lower or upper bound of a non-null range lies outside the
4459 -- range of the type mark.
4461 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
4462 if Is_Scalar_Type (Etype (Id))
4463 and then Scalar_Range (Id) /=
4464 Scalar_Range (Etype (Subtype_Mark
4465 (Subtype_Indication (N))))
4466 then
4467 Apply_Range_Check
4468 (Scalar_Range (Id),
4469 Etype (Subtype_Mark (Subtype_Indication (N))));
4471 -- In the array case, check compatibility for each index
4473 elsif Is_Array_Type (Etype (Id))
4474 and then Present (First_Index (Id))
4475 then
4476 -- This really should be a subprogram that finds the indications
4477 -- to check???
4479 declare
4480 Subt_Index : Node_Id := First_Index (Id);
4481 Target_Index : Node_Id :=
4482 First_Index (Etype
4483 (Subtype_Mark (Subtype_Indication (N))));
4484 Has_Dyn_Chk : Boolean := Has_Dynamic_Range_Check (N);
4486 begin
4487 while Present (Subt_Index) loop
4488 if ((Nkind (Subt_Index) = N_Identifier
4489 and then Ekind (Entity (Subt_Index)) in Scalar_Kind)
4490 or else Nkind (Subt_Index) = N_Subtype_Indication)
4491 and then
4492 Nkind (Scalar_Range (Etype (Subt_Index))) = N_Range
4493 then
4494 declare
4495 Target_Typ : constant Entity_Id :=
4496 Etype (Target_Index);
4497 begin
4498 R_Checks :=
4499 Get_Range_Checks
4500 (Scalar_Range (Etype (Subt_Index)),
4501 Target_Typ,
4502 Etype (Subt_Index),
4503 Defining_Identifier (N));
4505 -- Reset Has_Dynamic_Range_Check on the subtype to
4506 -- prevent elision of the index check due to a dynamic
4507 -- check generated for a preceding index (needed since
4508 -- Insert_Range_Checks tries to avoid generating
4509 -- redundant checks on a given declaration).
4511 Set_Has_Dynamic_Range_Check (N, False);
4513 Insert_Range_Checks
4514 (R_Checks,
4516 Target_Typ,
4517 Sloc (Defining_Identifier (N)));
4519 -- Record whether this index involved a dynamic check
4521 Has_Dyn_Chk :=
4522 Has_Dyn_Chk or else Has_Dynamic_Range_Check (N);
4523 end;
4524 end if;
4526 Next_Index (Subt_Index);
4527 Next_Index (Target_Index);
4528 end loop;
4530 -- Finally, mark whether the subtype involves dynamic checks
4532 Set_Has_Dynamic_Range_Check (N, Has_Dyn_Chk);
4533 end;
4534 end if;
4535 end if;
4537 -- Make sure that generic actual types are properly frozen. The subtype
4538 -- is marked as a generic actual type when the enclosing instance is
4539 -- analyzed, so here we identify the subtype from the tree structure.
4541 if Expander_Active
4542 and then Is_Generic_Actual_Type (Id)
4543 and then In_Instance
4544 and then not Comes_From_Source (N)
4545 and then Nkind (Subtype_Indication (N)) /= N_Subtype_Indication
4546 and then Is_Frozen (T)
4547 then
4548 Freeze_Before (N, Id);
4549 end if;
4551 Set_Optimize_Alignment_Flags (Id);
4552 Check_Eliminated (Id);
4554 <<Leave>>
4555 if Has_Aspects (N) then
4556 Analyze_Aspect_Specifications (N, Id);
4557 end if;
4559 Analyze_Dimension (N);
4560 end Analyze_Subtype_Declaration;
4562 --------------------------------
4563 -- Analyze_Subtype_Indication --
4564 --------------------------------
4566 procedure Analyze_Subtype_Indication (N : Node_Id) is
4567 T : constant Entity_Id := Subtype_Mark (N);
4568 R : constant Node_Id := Range_Expression (Constraint (N));
4570 begin
4571 Analyze (T);
4573 if R /= Error then
4574 Analyze (R);
4575 Set_Etype (N, Etype (R));
4576 Resolve (R, Entity (T));
4577 else
4578 Set_Error_Posted (R);
4579 Set_Error_Posted (T);
4580 end if;
4581 end Analyze_Subtype_Indication;
4583 --------------------------
4584 -- Analyze_Variant_Part --
4585 --------------------------
4587 procedure Analyze_Variant_Part (N : Node_Id) is
4589 procedure Non_Static_Choice_Error (Choice : Node_Id);
4590 -- Error routine invoked by the generic instantiation below when the
4591 -- variant part has a non static choice.
4593 procedure Process_Declarations (Variant : Node_Id);
4594 -- Analyzes all the declarations associated with a Variant. Needed by
4595 -- the generic instantiation below.
4597 package Variant_Choices_Processing is new
4598 Generic_Choices_Processing
4599 (Get_Alternatives => Variants,
4600 Get_Choices => Discrete_Choices,
4601 Process_Empty_Choice => No_OP,
4602 Process_Non_Static_Choice => Non_Static_Choice_Error,
4603 Process_Associated_Node => Process_Declarations);
4604 use Variant_Choices_Processing;
4605 -- Instantiation of the generic choice processing package
4607 -----------------------------
4608 -- Non_Static_Choice_Error --
4609 -----------------------------
4611 procedure Non_Static_Choice_Error (Choice : Node_Id) is
4612 begin
4613 Flag_Non_Static_Expr
4614 ("choice given in variant part is not static!", Choice);
4615 end Non_Static_Choice_Error;
4617 --------------------------
4618 -- Process_Declarations --
4619 --------------------------
4621 procedure Process_Declarations (Variant : Node_Id) is
4622 begin
4623 if not Null_Present (Component_List (Variant)) then
4624 Analyze_Declarations (Component_Items (Component_List (Variant)));
4626 if Present (Variant_Part (Component_List (Variant))) then
4627 Analyze (Variant_Part (Component_List (Variant)));
4628 end if;
4629 end if;
4630 end Process_Declarations;
4632 -- Local Variables
4634 Discr_Name : Node_Id;
4635 Discr_Type : Entity_Id;
4637 Dont_Care : Boolean;
4638 Others_Present : Boolean := False;
4640 pragma Warnings (Off, Dont_Care);
4641 pragma Warnings (Off, Others_Present);
4642 -- We don't care about the assigned values of any of these
4644 -- Start of processing for Analyze_Variant_Part
4646 begin
4647 Discr_Name := Name (N);
4648 Analyze (Discr_Name);
4650 -- If Discr_Name bad, get out (prevent cascaded errors)
4652 if Etype (Discr_Name) = Any_Type then
4653 return;
4654 end if;
4656 -- Check invalid discriminant in variant part
4658 if Ekind (Entity (Discr_Name)) /= E_Discriminant then
4659 Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
4660 end if;
4662 Discr_Type := Etype (Entity (Discr_Name));
4664 if not Is_Discrete_Type (Discr_Type) then
4665 Error_Msg_N
4666 ("discriminant in a variant part must be of a discrete type",
4667 Name (N));
4668 return;
4669 end if;
4671 -- Call the instantiated Analyze_Choices which does the rest of the work
4673 Analyze_Choices (N, Discr_Type, Dont_Care, Others_Present);
4674 end Analyze_Variant_Part;
4676 ----------------------------
4677 -- Array_Type_Declaration --
4678 ----------------------------
4680 procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
4681 Component_Def : constant Node_Id := Component_Definition (Def);
4682 Component_Typ : constant Node_Id := Subtype_Indication (Component_Def);
4683 Element_Type : Entity_Id;
4684 Implicit_Base : Entity_Id;
4685 Index : Node_Id;
4686 Related_Id : Entity_Id := Empty;
4687 Nb_Index : Nat;
4688 P : constant Node_Id := Parent (Def);
4689 Priv : Entity_Id;
4691 begin
4692 if Nkind (Def) = N_Constrained_Array_Definition then
4693 Index := First (Discrete_Subtype_Definitions (Def));
4694 else
4695 Index := First (Subtype_Marks (Def));
4696 end if;
4698 -- Find proper names for the implicit types which may be public. In case
4699 -- of anonymous arrays we use the name of the first object of that type
4700 -- as prefix.
4702 if No (T) then
4703 Related_Id := Defining_Identifier (P);
4704 else
4705 Related_Id := T;
4706 end if;
4708 Nb_Index := 1;
4709 while Present (Index) loop
4710 Analyze (Index);
4712 if not Nkind_In (Index, N_Identifier, N_Expanded_Name) then
4713 Check_SPARK_Restriction ("subtype mark required", Index);
4714 end if;
4716 -- Add a subtype declaration for each index of private array type
4717 -- declaration whose etype is also private. For example:
4719 -- package Pkg is
4720 -- type Index is private;
4721 -- private
4722 -- type Table is array (Index) of ...
4723 -- end;
4725 -- This is currently required by the expander for the internally
4726 -- generated equality subprogram of records with variant parts in
4727 -- which the etype of some component is such private type.
4729 if Ekind (Current_Scope) = E_Package
4730 and then In_Private_Part (Current_Scope)
4731 and then Has_Private_Declaration (Etype (Index))
4732 then
4733 declare
4734 Loc : constant Source_Ptr := Sloc (Def);
4735 New_E : Entity_Id;
4736 Decl : Entity_Id;
4738 begin
4739 New_E := Make_Temporary (Loc, 'T');
4740 Set_Is_Internal (New_E);
4742 Decl :=
4743 Make_Subtype_Declaration (Loc,
4744 Defining_Identifier => New_E,
4745 Subtype_Indication =>
4746 New_Occurrence_Of (Etype (Index), Loc));
4748 Insert_Before (Parent (Def), Decl);
4749 Analyze (Decl);
4750 Set_Etype (Index, New_E);
4752 -- If the index is a range the Entity attribute is not
4753 -- available. Example:
4755 -- package Pkg is
4756 -- type T is private;
4757 -- private
4758 -- type T is new Natural;
4759 -- Table : array (T(1) .. T(10)) of Boolean;
4760 -- end Pkg;
4762 if Nkind (Index) /= N_Range then
4763 Set_Entity (Index, New_E);
4764 end if;
4765 end;
4766 end if;
4768 Make_Index (Index, P, Related_Id, Nb_Index);
4770 -- Check error of subtype with predicate for index type
4772 Bad_Predicated_Subtype_Use
4773 ("subtype& has predicate, not allowed as index subtype",
4774 Index, Etype (Index));
4776 -- Move to next index
4778 Next_Index (Index);
4779 Nb_Index := Nb_Index + 1;
4780 end loop;
4782 -- Process subtype indication if one is present
4784 if Present (Component_Typ) then
4785 Element_Type := Process_Subtype (Component_Typ, P, Related_Id, 'C');
4787 Set_Etype (Component_Typ, Element_Type);
4789 if not Nkind_In (Component_Typ, N_Identifier, N_Expanded_Name) then
4790 Check_SPARK_Restriction ("subtype mark required", Component_Typ);
4791 end if;
4793 -- Ada 2005 (AI-230): Access Definition case
4795 else pragma Assert (Present (Access_Definition (Component_Def)));
4797 -- Indicate that the anonymous access type is created by the
4798 -- array type declaration.
4800 Element_Type := Access_Definition
4801 (Related_Nod => P,
4802 N => Access_Definition (Component_Def));
4803 Set_Is_Local_Anonymous_Access (Element_Type);
4805 -- Propagate the parent. This field is needed if we have to generate
4806 -- the master_id associated with an anonymous access to task type
4807 -- component (see Expand_N_Full_Type_Declaration.Build_Master)
4809 Set_Parent (Element_Type, Parent (T));
4811 -- Ada 2005 (AI-230): In case of components that are anonymous access
4812 -- types the level of accessibility depends on the enclosing type
4813 -- declaration
4815 Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
4817 -- Ada 2005 (AI-254)
4819 declare
4820 CD : constant Node_Id :=
4821 Access_To_Subprogram_Definition
4822 (Access_Definition (Component_Def));
4823 begin
4824 if Present (CD) and then Protected_Present (CD) then
4825 Element_Type :=
4826 Replace_Anonymous_Access_To_Protected_Subprogram (Def);
4827 end if;
4828 end;
4829 end if;
4831 -- Constrained array case
4833 if No (T) then
4834 T := Create_Itype (E_Void, P, Related_Id, 'T');
4835 end if;
4837 if Nkind (Def) = N_Constrained_Array_Definition then
4839 -- Establish Implicit_Base as unconstrained base type
4841 Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
4843 Set_Etype (Implicit_Base, Implicit_Base);
4844 Set_Scope (Implicit_Base, Current_Scope);
4845 Set_Has_Delayed_Freeze (Implicit_Base);
4847 -- The constrained array type is a subtype of the unconstrained one
4849 Set_Ekind (T, E_Array_Subtype);
4850 Init_Size_Align (T);
4851 Set_Etype (T, Implicit_Base);
4852 Set_Scope (T, Current_Scope);
4853 Set_Is_Constrained (T, True);
4854 Set_First_Index (T, First (Discrete_Subtype_Definitions (Def)));
4855 Set_Has_Delayed_Freeze (T);
4857 -- Complete setup of implicit base type
4859 Set_First_Index (Implicit_Base, First_Index (T));
4860 Set_Component_Type (Implicit_Base, Element_Type);
4861 Set_Has_Task (Implicit_Base, Has_Task (Element_Type));
4862 Set_Component_Size (Implicit_Base, Uint_0);
4863 Set_Packed_Array_Type (Implicit_Base, Empty);
4864 Set_Has_Controlled_Component
4865 (Implicit_Base, Has_Controlled_Component
4866 (Element_Type)
4867 or else Is_Controlled
4868 (Element_Type));
4869 Set_Finalize_Storage_Only
4870 (Implicit_Base, Finalize_Storage_Only
4871 (Element_Type));
4873 -- Unconstrained array case
4875 else
4876 Set_Ekind (T, E_Array_Type);
4877 Init_Size_Align (T);
4878 Set_Etype (T, T);
4879 Set_Scope (T, Current_Scope);
4880 Set_Component_Size (T, Uint_0);
4881 Set_Is_Constrained (T, False);
4882 Set_First_Index (T, First (Subtype_Marks (Def)));
4883 Set_Has_Delayed_Freeze (T, True);
4884 Set_Has_Task (T, Has_Task (Element_Type));
4885 Set_Has_Controlled_Component (T, Has_Controlled_Component
4886 (Element_Type)
4887 or else
4888 Is_Controlled (Element_Type));
4889 Set_Finalize_Storage_Only (T, Finalize_Storage_Only
4890 (Element_Type));
4891 end if;
4893 -- Common attributes for both cases
4895 Set_Component_Type (Base_Type (T), Element_Type);
4896 Set_Packed_Array_Type (T, Empty);
4898 if Aliased_Present (Component_Definition (Def)) then
4899 Check_SPARK_Restriction
4900 ("aliased is not allowed", Component_Definition (Def));
4901 Set_Has_Aliased_Components (Etype (T));
4902 end if;
4904 -- Ada 2005 (AI-231): Propagate the null-excluding attribute to the
4905 -- array type to ensure that objects of this type are initialized.
4907 if Ada_Version >= Ada_2005
4908 and then Can_Never_Be_Null (Element_Type)
4909 then
4910 Set_Can_Never_Be_Null (T);
4912 if Null_Exclusion_Present (Component_Definition (Def))
4914 -- No need to check itypes because in their case this check was
4915 -- done at their point of creation
4917 and then not Is_Itype (Element_Type)
4918 then
4919 Error_Msg_N
4920 ("`NOT NULL` not allowed (null already excluded)",
4921 Subtype_Indication (Component_Definition (Def)));
4922 end if;
4923 end if;
4925 Priv := Private_Component (Element_Type);
4927 if Present (Priv) then
4929 -- Check for circular definitions
4931 if Priv = Any_Type then
4932 Set_Component_Type (Etype (T), Any_Type);
4934 -- There is a gap in the visibility of operations on the composite
4935 -- type only if the component type is defined in a different scope.
4937 elsif Scope (Priv) = Current_Scope then
4938 null;
4940 elsif Is_Limited_Type (Priv) then
4941 Set_Is_Limited_Composite (Etype (T));
4942 Set_Is_Limited_Composite (T);
4943 else
4944 Set_Is_Private_Composite (Etype (T));
4945 Set_Is_Private_Composite (T);
4946 end if;
4947 end if;
4949 -- A syntax error in the declaration itself may lead to an empty index
4950 -- list, in which case do a minimal patch.
4952 if No (First_Index (T)) then
4953 Error_Msg_N ("missing index definition in array type declaration", T);
4955 declare
4956 Indexes : constant List_Id :=
4957 New_List (New_Occurrence_Of (Any_Id, Sloc (T)));
4958 begin
4959 Set_Discrete_Subtype_Definitions (Def, Indexes);
4960 Set_First_Index (T, First (Indexes));
4961 return;
4962 end;
4963 end if;
4965 -- Create a concatenation operator for the new type. Internal array
4966 -- types created for packed entities do not need such, they are
4967 -- compatible with the user-defined type.
4969 if Number_Dimensions (T) = 1
4970 and then not Is_Packed_Array_Type (T)
4971 then
4972 New_Concatenation_Op (T);
4973 end if;
4975 -- In the case of an unconstrained array the parser has already verified
4976 -- that all the indexes are unconstrained but we still need to make sure
4977 -- that the element type is constrained.
4979 if Is_Indefinite_Subtype (Element_Type) then
4980 Error_Msg_N
4981 ("unconstrained element type in array declaration",
4982 Subtype_Indication (Component_Def));
4984 elsif Is_Abstract_Type (Element_Type) then
4985 Error_Msg_N
4986 ("the type of a component cannot be abstract",
4987 Subtype_Indication (Component_Def));
4988 end if;
4990 -- There may be an invariant declared for the component type, but
4991 -- the construction of the component invariant checking procedure
4992 -- takes place during expansion.
4993 end Array_Type_Declaration;
4995 ------------------------------------------------------
4996 -- Replace_Anonymous_Access_To_Protected_Subprogram --
4997 ------------------------------------------------------
4999 function Replace_Anonymous_Access_To_Protected_Subprogram
5000 (N : Node_Id) return Entity_Id
5002 Loc : constant Source_Ptr := Sloc (N);
5004 Curr_Scope : constant Scope_Stack_Entry :=
5005 Scope_Stack.Table (Scope_Stack.Last);
5007 Anon : constant Entity_Id := Make_Temporary (Loc, 'S');
5009 Acc : Node_Id;
5010 -- Access definition in declaration
5012 Comp : Node_Id;
5013 -- Object definition or formal definition with an access definition
5015 Decl : Node_Id;
5016 -- Declaration of anonymous access to subprogram type
5018 Spec : Node_Id;
5019 -- Original specification in access to subprogram
5021 P : Node_Id;
5023 begin
5024 Set_Is_Internal (Anon);
5026 case Nkind (N) is
5027 when N_Component_Declaration |
5028 N_Unconstrained_Array_Definition |
5029 N_Constrained_Array_Definition =>
5030 Comp := Component_Definition (N);
5031 Acc := Access_Definition (Comp);
5033 when N_Discriminant_Specification =>
5034 Comp := Discriminant_Type (N);
5035 Acc := Comp;
5037 when N_Parameter_Specification =>
5038 Comp := Parameter_Type (N);
5039 Acc := Comp;
5041 when N_Access_Function_Definition =>
5042 Comp := Result_Definition (N);
5043 Acc := Comp;
5045 when N_Object_Declaration =>
5046 Comp := Object_Definition (N);
5047 Acc := Comp;
5049 when N_Function_Specification =>
5050 Comp := Result_Definition (N);
5051 Acc := Comp;
5053 when others =>
5054 raise Program_Error;
5055 end case;
5057 Spec := Access_To_Subprogram_Definition (Acc);
5059 Decl :=
5060 Make_Full_Type_Declaration (Loc,
5061 Defining_Identifier => Anon,
5062 Type_Definition => Copy_Separate_Tree (Spec));
5064 Mark_Rewrite_Insertion (Decl);
5066 -- In ASIS mode, analyze the profile on the original node, because
5067 -- the separate copy does not provide enough links to recover the
5068 -- original tree. Analysis is limited to type annotations, within
5069 -- a temporary scope that serves as an anonymous subprogram to collect
5070 -- otherwise useless temporaries and itypes.
5072 if ASIS_Mode then
5073 declare
5074 Typ : constant Entity_Id := Make_Temporary (Loc, 'S');
5076 begin
5077 if Nkind (Spec) = N_Access_Function_Definition then
5078 Set_Ekind (Typ, E_Function);
5079 else
5080 Set_Ekind (Typ, E_Procedure);
5081 end if;
5083 Set_Parent (Typ, N);
5084 Set_Scope (Typ, Current_Scope);
5085 Push_Scope (Typ);
5087 Process_Formals (Parameter_Specifications (Spec), Spec);
5089 if Nkind (Spec) = N_Access_Function_Definition then
5090 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
5091 Find_Type (Subtype_Mark (Result_Definition (Spec)));
5092 else
5093 Find_Type (Result_Definition (Spec));
5094 end if;
5095 end if;
5097 End_Scope;
5098 end;
5099 end if;
5101 -- Insert the new declaration in the nearest enclosing scope. If the
5102 -- node is a body and N is its return type, the declaration belongs in
5103 -- the enclosing scope.
5105 P := Parent (N);
5107 if Nkind (P) = N_Subprogram_Body
5108 and then Nkind (N) = N_Function_Specification
5109 then
5110 P := Parent (P);
5111 end if;
5113 while Present (P) and then not Has_Declarations (P) loop
5114 P := Parent (P);
5115 end loop;
5117 pragma Assert (Present (P));
5119 if Nkind (P) = N_Package_Specification then
5120 Prepend (Decl, Visible_Declarations (P));
5121 else
5122 Prepend (Decl, Declarations (P));
5123 end if;
5125 -- Replace the anonymous type with an occurrence of the new declaration.
5126 -- In all cases the rewritten node does not have the null-exclusion
5127 -- attribute because (if present) it was already inherited by the
5128 -- anonymous entity (Anon). Thus, in case of components we do not
5129 -- inherit this attribute.
5131 if Nkind (N) = N_Parameter_Specification then
5132 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5133 Set_Etype (Defining_Identifier (N), Anon);
5134 Set_Null_Exclusion_Present (N, False);
5136 elsif Nkind (N) = N_Object_Declaration then
5137 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5138 Set_Etype (Defining_Identifier (N), Anon);
5140 elsif Nkind (N) = N_Access_Function_Definition then
5141 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5143 elsif Nkind (N) = N_Function_Specification then
5144 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5145 Set_Etype (Defining_Unit_Name (N), Anon);
5147 else
5148 Rewrite (Comp,
5149 Make_Component_Definition (Loc,
5150 Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
5151 end if;
5153 Mark_Rewrite_Insertion (Comp);
5155 if Nkind_In (N, N_Object_Declaration, N_Access_Function_Definition) then
5156 Analyze (Decl);
5158 else
5159 -- Temporarily remove the current scope (record or subprogram) from
5160 -- the stack to add the new declarations to the enclosing scope.
5162 Scope_Stack.Decrement_Last;
5163 Analyze (Decl);
5164 Set_Is_Itype (Anon);
5165 Scope_Stack.Append (Curr_Scope);
5166 end if;
5168 Set_Ekind (Anon, E_Anonymous_Access_Protected_Subprogram_Type);
5169 Set_Can_Use_Internal_Rep (Anon, not Always_Compatible_Rep_On_Target);
5170 return Anon;
5171 end Replace_Anonymous_Access_To_Protected_Subprogram;
5173 -------------------------------
5174 -- Build_Derived_Access_Type --
5175 -------------------------------
5177 procedure Build_Derived_Access_Type
5178 (N : Node_Id;
5179 Parent_Type : Entity_Id;
5180 Derived_Type : Entity_Id)
5182 S : constant Node_Id := Subtype_Indication (Type_Definition (N));
5184 Desig_Type : Entity_Id;
5185 Discr : Entity_Id;
5186 Discr_Con_Elist : Elist_Id;
5187 Discr_Con_El : Elmt_Id;
5188 Subt : Entity_Id;
5190 begin
5191 -- Set the designated type so it is available in case this is an access
5192 -- to a self-referential type, e.g. a standard list type with a next
5193 -- pointer. Will be reset after subtype is built.
5195 Set_Directly_Designated_Type
5196 (Derived_Type, Designated_Type (Parent_Type));
5198 Subt := Process_Subtype (S, N);
5200 if Nkind (S) /= N_Subtype_Indication
5201 and then Subt /= Base_Type (Subt)
5202 then
5203 Set_Ekind (Derived_Type, E_Access_Subtype);
5204 end if;
5206 if Ekind (Derived_Type) = E_Access_Subtype then
5207 declare
5208 Pbase : constant Entity_Id := Base_Type (Parent_Type);
5209 Ibase : constant Entity_Id :=
5210 Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
5211 Svg_Chars : constant Name_Id := Chars (Ibase);
5212 Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
5214 begin
5215 Copy_Node (Pbase, Ibase);
5217 Set_Chars (Ibase, Svg_Chars);
5218 Set_Next_Entity (Ibase, Svg_Next_E);
5219 Set_Sloc (Ibase, Sloc (Derived_Type));
5220 Set_Scope (Ibase, Scope (Derived_Type));
5221 Set_Freeze_Node (Ibase, Empty);
5222 Set_Is_Frozen (Ibase, False);
5223 Set_Comes_From_Source (Ibase, False);
5224 Set_Is_First_Subtype (Ibase, False);
5226 Set_Etype (Ibase, Pbase);
5227 Set_Etype (Derived_Type, Ibase);
5228 end;
5229 end if;
5231 Set_Directly_Designated_Type
5232 (Derived_Type, Designated_Type (Subt));
5234 Set_Is_Constrained (Derived_Type, Is_Constrained (Subt));
5235 Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
5236 Set_Size_Info (Derived_Type, Parent_Type);
5237 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
5238 Set_Depends_On_Private (Derived_Type,
5239 Has_Private_Component (Derived_Type));
5240 Conditional_Delay (Derived_Type, Subt);
5242 -- Ada 2005 (AI-231): Set the null-exclusion attribute, and verify
5243 -- that it is not redundant.
5245 if Null_Exclusion_Present (Type_Definition (N)) then
5246 Set_Can_Never_Be_Null (Derived_Type);
5248 if Can_Never_Be_Null (Parent_Type)
5249 and then False
5250 then
5251 Error_Msg_NE
5252 ("`NOT NULL` not allowed (& already excludes null)",
5253 N, Parent_Type);
5254 end if;
5256 elsif Can_Never_Be_Null (Parent_Type) then
5257 Set_Can_Never_Be_Null (Derived_Type);
5258 end if;
5260 -- Note: we do not copy the Storage_Size_Variable, since we always go to
5261 -- the root type for this information.
5263 -- Apply range checks to discriminants for derived record case
5264 -- ??? THIS CODE SHOULD NOT BE HERE REALLY.
5266 Desig_Type := Designated_Type (Derived_Type);
5267 if Is_Composite_Type (Desig_Type)
5268 and then (not Is_Array_Type (Desig_Type))
5269 and then Has_Discriminants (Desig_Type)
5270 and then Base_Type (Desig_Type) /= Desig_Type
5271 then
5272 Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
5273 Discr_Con_El := First_Elmt (Discr_Con_Elist);
5275 Discr := First_Discriminant (Base_Type (Desig_Type));
5276 while Present (Discr_Con_El) loop
5277 Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
5278 Next_Elmt (Discr_Con_El);
5279 Next_Discriminant (Discr);
5280 end loop;
5281 end if;
5282 end Build_Derived_Access_Type;
5284 ------------------------------
5285 -- Build_Derived_Array_Type --
5286 ------------------------------
5288 procedure Build_Derived_Array_Type
5289 (N : Node_Id;
5290 Parent_Type : Entity_Id;
5291 Derived_Type : Entity_Id)
5293 Loc : constant Source_Ptr := Sloc (N);
5294 Tdef : constant Node_Id := Type_Definition (N);
5295 Indic : constant Node_Id := Subtype_Indication (Tdef);
5296 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
5297 Implicit_Base : Entity_Id;
5298 New_Indic : Node_Id;
5300 procedure Make_Implicit_Base;
5301 -- If the parent subtype is constrained, the derived type is a subtype
5302 -- of an implicit base type derived from the parent base.
5304 ------------------------
5305 -- Make_Implicit_Base --
5306 ------------------------
5308 procedure Make_Implicit_Base is
5309 begin
5310 Implicit_Base :=
5311 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
5313 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
5314 Set_Etype (Implicit_Base, Parent_Base);
5316 Copy_Array_Subtype_Attributes (Implicit_Base, Parent_Base);
5317 Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
5319 Set_Has_Delayed_Freeze (Implicit_Base, True);
5320 end Make_Implicit_Base;
5322 -- Start of processing for Build_Derived_Array_Type
5324 begin
5325 if not Is_Constrained (Parent_Type) then
5326 if Nkind (Indic) /= N_Subtype_Indication then
5327 Set_Ekind (Derived_Type, E_Array_Type);
5329 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
5330 Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
5332 Set_Has_Delayed_Freeze (Derived_Type, True);
5334 else
5335 Make_Implicit_Base;
5336 Set_Etype (Derived_Type, Implicit_Base);
5338 New_Indic :=
5339 Make_Subtype_Declaration (Loc,
5340 Defining_Identifier => Derived_Type,
5341 Subtype_Indication =>
5342 Make_Subtype_Indication (Loc,
5343 Subtype_Mark => New_Reference_To (Implicit_Base, Loc),
5344 Constraint => Constraint (Indic)));
5346 Rewrite (N, New_Indic);
5347 Analyze (N);
5348 end if;
5350 else
5351 if Nkind (Indic) /= N_Subtype_Indication then
5352 Make_Implicit_Base;
5354 Set_Ekind (Derived_Type, Ekind (Parent_Type));
5355 Set_Etype (Derived_Type, Implicit_Base);
5356 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
5358 else
5359 Error_Msg_N ("illegal constraint on constrained type", Indic);
5360 end if;
5361 end if;
5363 -- If parent type is not a derived type itself, and is declared in
5364 -- closed scope (e.g. a subprogram), then we must explicitly introduce
5365 -- the new type's concatenation operator since Derive_Subprograms
5366 -- will not inherit the parent's operator. If the parent type is
5367 -- unconstrained, the operator is of the unconstrained base type.
5369 if Number_Dimensions (Parent_Type) = 1
5370 and then not Is_Limited_Type (Parent_Type)
5371 and then not Is_Derived_Type (Parent_Type)
5372 and then not Is_Package_Or_Generic_Package
5373 (Scope (Base_Type (Parent_Type)))
5374 then
5375 if not Is_Constrained (Parent_Type)
5376 and then Is_Constrained (Derived_Type)
5377 then
5378 New_Concatenation_Op (Implicit_Base);
5379 else
5380 New_Concatenation_Op (Derived_Type);
5381 end if;
5382 end if;
5383 end Build_Derived_Array_Type;
5385 -----------------------------------
5386 -- Build_Derived_Concurrent_Type --
5387 -----------------------------------
5389 procedure Build_Derived_Concurrent_Type
5390 (N : Node_Id;
5391 Parent_Type : Entity_Id;
5392 Derived_Type : Entity_Id)
5394 Loc : constant Source_Ptr := Sloc (N);
5396 Corr_Record : constant Entity_Id := Make_Temporary (Loc, 'C');
5397 Corr_Decl : Node_Id;
5398 Corr_Decl_Needed : Boolean;
5399 -- If the derived type has fewer discriminants than its parent, the
5400 -- corresponding record is also a derived type, in order to account for
5401 -- the bound discriminants. We create a full type declaration for it in
5402 -- this case.
5404 Constraint_Present : constant Boolean :=
5405 Nkind (Subtype_Indication (Type_Definition (N))) =
5406 N_Subtype_Indication;
5408 D_Constraint : Node_Id;
5409 New_Constraint : Elist_Id;
5410 Old_Disc : Entity_Id;
5411 New_Disc : Entity_Id;
5412 New_N : Node_Id;
5414 begin
5415 Set_Stored_Constraint (Derived_Type, No_Elist);
5416 Corr_Decl_Needed := False;
5417 Old_Disc := Empty;
5419 if Present (Discriminant_Specifications (N))
5420 and then Constraint_Present
5421 then
5422 Old_Disc := First_Discriminant (Parent_Type);
5423 New_Disc := First (Discriminant_Specifications (N));
5424 while Present (New_Disc) and then Present (Old_Disc) loop
5425 Next_Discriminant (Old_Disc);
5426 Next (New_Disc);
5427 end loop;
5428 end if;
5430 if Present (Old_Disc) and then Expander_Active then
5432 -- The new type has fewer discriminants, so we need to create a new
5433 -- corresponding record, which is derived from the corresponding
5434 -- record of the parent, and has a stored constraint that captures
5435 -- the values of the discriminant constraints. The corresponding
5436 -- record is needed only if expander is active and code generation is
5437 -- enabled.
5439 -- The type declaration for the derived corresponding record has the
5440 -- same discriminant part and constraints as the current declaration.
5441 -- Copy the unanalyzed tree to build declaration.
5443 Corr_Decl_Needed := True;
5444 New_N := Copy_Separate_Tree (N);
5446 Corr_Decl :=
5447 Make_Full_Type_Declaration (Loc,
5448 Defining_Identifier => Corr_Record,
5449 Discriminant_Specifications =>
5450 Discriminant_Specifications (New_N),
5451 Type_Definition =>
5452 Make_Derived_Type_Definition (Loc,
5453 Subtype_Indication =>
5454 Make_Subtype_Indication (Loc,
5455 Subtype_Mark =>
5456 New_Occurrence_Of
5457 (Corresponding_Record_Type (Parent_Type), Loc),
5458 Constraint =>
5459 Constraint
5460 (Subtype_Indication (Type_Definition (New_N))))));
5461 end if;
5463 -- Copy Storage_Size and Relative_Deadline variables if task case
5465 if Is_Task_Type (Parent_Type) then
5466 Set_Storage_Size_Variable (Derived_Type,
5467 Storage_Size_Variable (Parent_Type));
5468 Set_Relative_Deadline_Variable (Derived_Type,
5469 Relative_Deadline_Variable (Parent_Type));
5470 end if;
5472 if Present (Discriminant_Specifications (N)) then
5473 Push_Scope (Derived_Type);
5474 Check_Or_Process_Discriminants (N, Derived_Type);
5476 if Constraint_Present then
5477 New_Constraint :=
5478 Expand_To_Stored_Constraint
5479 (Parent_Type,
5480 Build_Discriminant_Constraints
5481 (Parent_Type,
5482 Subtype_Indication (Type_Definition (N)), True));
5483 end if;
5485 End_Scope;
5487 elsif Constraint_Present then
5489 -- Build constrained subtype, copying the constraint, and derive
5490 -- from it to create a derived constrained type.
5492 declare
5493 Loc : constant Source_Ptr := Sloc (N);
5494 Anon : constant Entity_Id :=
5495 Make_Defining_Identifier (Loc,
5496 Chars => New_External_Name (Chars (Derived_Type), 'T'));
5497 Decl : Node_Id;
5499 begin
5500 Decl :=
5501 Make_Subtype_Declaration (Loc,
5502 Defining_Identifier => Anon,
5503 Subtype_Indication =>
5504 New_Copy_Tree (Subtype_Indication (Type_Definition (N))));
5505 Insert_Before (N, Decl);
5506 Analyze (Decl);
5508 Rewrite (Subtype_Indication (Type_Definition (N)),
5509 New_Occurrence_Of (Anon, Loc));
5510 Set_Analyzed (Derived_Type, False);
5511 Analyze (N);
5512 return;
5513 end;
5514 end if;
5516 -- By default, operations and private data are inherited from parent.
5517 -- However, in the presence of bound discriminants, a new corresponding
5518 -- record will be created, see below.
5520 Set_Has_Discriminants
5521 (Derived_Type, Has_Discriminants (Parent_Type));
5522 Set_Corresponding_Record_Type
5523 (Derived_Type, Corresponding_Record_Type (Parent_Type));
5525 -- Is_Constrained is set according the parent subtype, but is set to
5526 -- False if the derived type is declared with new discriminants.
5528 Set_Is_Constrained
5529 (Derived_Type,
5530 (Is_Constrained (Parent_Type) or else Constraint_Present)
5531 and then not Present (Discriminant_Specifications (N)));
5533 if Constraint_Present then
5534 if not Has_Discriminants (Parent_Type) then
5535 Error_Msg_N ("untagged parent must have discriminants", N);
5537 elsif Present (Discriminant_Specifications (N)) then
5539 -- Verify that new discriminants are used to constrain old ones
5541 D_Constraint :=
5542 First
5543 (Constraints
5544 (Constraint (Subtype_Indication (Type_Definition (N)))));
5546 Old_Disc := First_Discriminant (Parent_Type);
5548 while Present (D_Constraint) loop
5549 if Nkind (D_Constraint) /= N_Discriminant_Association then
5551 -- Positional constraint. If it is a reference to a new
5552 -- discriminant, it constrains the corresponding old one.
5554 if Nkind (D_Constraint) = N_Identifier then
5555 New_Disc := First_Discriminant (Derived_Type);
5556 while Present (New_Disc) loop
5557 exit when Chars (New_Disc) = Chars (D_Constraint);
5558 Next_Discriminant (New_Disc);
5559 end loop;
5561 if Present (New_Disc) then
5562 Set_Corresponding_Discriminant (New_Disc, Old_Disc);
5563 end if;
5564 end if;
5566 Next_Discriminant (Old_Disc);
5568 -- if this is a named constraint, search by name for the old
5569 -- discriminants constrained by the new one.
5571 elsif Nkind (Expression (D_Constraint)) = N_Identifier then
5573 -- Find new discriminant with that name
5575 New_Disc := First_Discriminant (Derived_Type);
5576 while Present (New_Disc) loop
5577 exit when
5578 Chars (New_Disc) = Chars (Expression (D_Constraint));
5579 Next_Discriminant (New_Disc);
5580 end loop;
5582 if Present (New_Disc) then
5584 -- Verify that new discriminant renames some discriminant
5585 -- of the parent type, and associate the new discriminant
5586 -- with one or more old ones that it renames.
5588 declare
5589 Selector : Node_Id;
5591 begin
5592 Selector := First (Selector_Names (D_Constraint));
5593 while Present (Selector) loop
5594 Old_Disc := First_Discriminant (Parent_Type);
5595 while Present (Old_Disc) loop
5596 exit when Chars (Old_Disc) = Chars (Selector);
5597 Next_Discriminant (Old_Disc);
5598 end loop;
5600 if Present (Old_Disc) then
5601 Set_Corresponding_Discriminant
5602 (New_Disc, Old_Disc);
5603 end if;
5605 Next (Selector);
5606 end loop;
5607 end;
5608 end if;
5609 end if;
5611 Next (D_Constraint);
5612 end loop;
5614 New_Disc := First_Discriminant (Derived_Type);
5615 while Present (New_Disc) loop
5616 if No (Corresponding_Discriminant (New_Disc)) then
5617 Error_Msg_NE
5618 ("new discriminant& must constrain old one", N, New_Disc);
5620 elsif not
5621 Subtypes_Statically_Compatible
5622 (Etype (New_Disc),
5623 Etype (Corresponding_Discriminant (New_Disc)))
5624 then
5625 Error_Msg_NE
5626 ("& not statically compatible with parent discriminant",
5627 N, New_Disc);
5628 end if;
5630 Next_Discriminant (New_Disc);
5631 end loop;
5632 end if;
5634 elsif Present (Discriminant_Specifications (N)) then
5635 Error_Msg_N
5636 ("missing discriminant constraint in untagged derivation", N);
5637 end if;
5639 -- The entity chain of the derived type includes the new discriminants
5640 -- but shares operations with the parent.
5642 if Present (Discriminant_Specifications (N)) then
5643 Old_Disc := First_Discriminant (Parent_Type);
5644 while Present (Old_Disc) loop
5645 if No (Next_Entity (Old_Disc))
5646 or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
5647 then
5648 Set_Next_Entity
5649 (Last_Entity (Derived_Type), Next_Entity (Old_Disc));
5650 exit;
5651 end if;
5653 Next_Discriminant (Old_Disc);
5654 end loop;
5656 else
5657 Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
5658 if Has_Discriminants (Parent_Type) then
5659 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
5660 Set_Discriminant_Constraint (
5661 Derived_Type, Discriminant_Constraint (Parent_Type));
5662 end if;
5663 end if;
5665 Set_Last_Entity (Derived_Type, Last_Entity (Parent_Type));
5667 Set_Has_Completion (Derived_Type);
5669 if Corr_Decl_Needed then
5670 Set_Stored_Constraint (Derived_Type, New_Constraint);
5671 Insert_After (N, Corr_Decl);
5672 Analyze (Corr_Decl);
5673 Set_Corresponding_Record_Type (Derived_Type, Corr_Record);
5674 end if;
5675 end Build_Derived_Concurrent_Type;
5677 ------------------------------------
5678 -- Build_Derived_Enumeration_Type --
5679 ------------------------------------
5681 procedure Build_Derived_Enumeration_Type
5682 (N : Node_Id;
5683 Parent_Type : Entity_Id;
5684 Derived_Type : Entity_Id)
5686 Loc : constant Source_Ptr := Sloc (N);
5687 Def : constant Node_Id := Type_Definition (N);
5688 Indic : constant Node_Id := Subtype_Indication (Def);
5689 Implicit_Base : Entity_Id;
5690 Literal : Entity_Id;
5691 New_Lit : Entity_Id;
5692 Literals_List : List_Id;
5693 Type_Decl : Node_Id;
5694 Hi, Lo : Node_Id;
5695 Rang_Expr : Node_Id;
5697 begin
5698 -- Since types Standard.Character and Standard.[Wide_]Wide_Character do
5699 -- not have explicit literals lists we need to process types derived
5700 -- from them specially. This is handled by Derived_Standard_Character.
5701 -- If the parent type is a generic type, there are no literals either,
5702 -- and we construct the same skeletal representation as for the generic
5703 -- parent type.
5705 if Is_Standard_Character_Type (Parent_Type) then
5706 Derived_Standard_Character (N, Parent_Type, Derived_Type);
5708 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
5709 declare
5710 Lo : Node_Id;
5711 Hi : Node_Id;
5713 begin
5714 if Nkind (Indic) /= N_Subtype_Indication then
5715 Lo :=
5716 Make_Attribute_Reference (Loc,
5717 Attribute_Name => Name_First,
5718 Prefix => New_Reference_To (Derived_Type, Loc));
5719 Set_Etype (Lo, Derived_Type);
5721 Hi :=
5722 Make_Attribute_Reference (Loc,
5723 Attribute_Name => Name_Last,
5724 Prefix => New_Reference_To (Derived_Type, Loc));
5725 Set_Etype (Hi, Derived_Type);
5727 Set_Scalar_Range (Derived_Type,
5728 Make_Range (Loc,
5729 Low_Bound => Lo,
5730 High_Bound => Hi));
5731 else
5733 -- Analyze subtype indication and verify compatibility
5734 -- with parent type.
5736 if Base_Type (Process_Subtype (Indic, N)) /=
5737 Base_Type (Parent_Type)
5738 then
5739 Error_Msg_N
5740 ("illegal constraint for formal discrete type", N);
5741 end if;
5742 end if;
5743 end;
5745 else
5746 -- If a constraint is present, analyze the bounds to catch
5747 -- premature usage of the derived literals.
5749 if Nkind (Indic) = N_Subtype_Indication
5750 and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
5751 then
5752 Analyze (Low_Bound (Range_Expression (Constraint (Indic))));
5753 Analyze (High_Bound (Range_Expression (Constraint (Indic))));
5754 end if;
5756 -- Introduce an implicit base type for the derived type even if there
5757 -- is no constraint attached to it, since this seems closer to the
5758 -- Ada semantics. Build a full type declaration tree for the derived
5759 -- type using the implicit base type as the defining identifier. The
5760 -- build a subtype declaration tree which applies the constraint (if
5761 -- any) have it replace the derived type declaration.
5763 Literal := First_Literal (Parent_Type);
5764 Literals_List := New_List;
5765 while Present (Literal)
5766 and then Ekind (Literal) = E_Enumeration_Literal
5767 loop
5768 -- Literals of the derived type have the same representation as
5769 -- those of the parent type, but this representation can be
5770 -- overridden by an explicit representation clause. Indicate
5771 -- that there is no explicit representation given yet. These
5772 -- derived literals are implicit operations of the new type,
5773 -- and can be overridden by explicit ones.
5775 if Nkind (Literal) = N_Defining_Character_Literal then
5776 New_Lit :=
5777 Make_Defining_Character_Literal (Loc, Chars (Literal));
5778 else
5779 New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
5780 end if;
5782 Set_Ekind (New_Lit, E_Enumeration_Literal);
5783 Set_Enumeration_Pos (New_Lit, Enumeration_Pos (Literal));
5784 Set_Enumeration_Rep (New_Lit, Enumeration_Rep (Literal));
5785 Set_Enumeration_Rep_Expr (New_Lit, Empty);
5786 Set_Alias (New_Lit, Literal);
5787 Set_Is_Known_Valid (New_Lit, True);
5789 Append (New_Lit, Literals_List);
5790 Next_Literal (Literal);
5791 end loop;
5793 Implicit_Base :=
5794 Make_Defining_Identifier (Sloc (Derived_Type),
5795 Chars => New_External_Name (Chars (Derived_Type), 'B'));
5797 -- Indicate the proper nature of the derived type. This must be done
5798 -- before analysis of the literals, to recognize cases when a literal
5799 -- may be hidden by a previous explicit function definition (cf.
5800 -- c83031a).
5802 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
5803 Set_Etype (Derived_Type, Implicit_Base);
5805 Type_Decl :=
5806 Make_Full_Type_Declaration (Loc,
5807 Defining_Identifier => Implicit_Base,
5808 Discriminant_Specifications => No_List,
5809 Type_Definition =>
5810 Make_Enumeration_Type_Definition (Loc, Literals_List));
5812 Mark_Rewrite_Insertion (Type_Decl);
5813 Insert_Before (N, Type_Decl);
5814 Analyze (Type_Decl);
5816 -- After the implicit base is analyzed its Etype needs to be changed
5817 -- to reflect the fact that it is derived from the parent type which
5818 -- was ignored during analysis. We also set the size at this point.
5820 Set_Etype (Implicit_Base, Parent_Type);
5822 Set_Size_Info (Implicit_Base, Parent_Type);
5823 Set_RM_Size (Implicit_Base, RM_Size (Parent_Type));
5824 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
5826 -- Copy other flags from parent type
5828 Set_Has_Non_Standard_Rep
5829 (Implicit_Base, Has_Non_Standard_Rep
5830 (Parent_Type));
5831 Set_Has_Pragma_Ordered
5832 (Implicit_Base, Has_Pragma_Ordered
5833 (Parent_Type));
5834 Set_Has_Delayed_Freeze (Implicit_Base);
5836 -- Process the subtype indication including a validation check on the
5837 -- constraint, if any. If a constraint is given, its bounds must be
5838 -- implicitly converted to the new type.
5840 if Nkind (Indic) = N_Subtype_Indication then
5841 declare
5842 R : constant Node_Id :=
5843 Range_Expression (Constraint (Indic));
5845 begin
5846 if Nkind (R) = N_Range then
5847 Hi := Build_Scalar_Bound
5848 (High_Bound (R), Parent_Type, Implicit_Base);
5849 Lo := Build_Scalar_Bound
5850 (Low_Bound (R), Parent_Type, Implicit_Base);
5852 else
5853 -- Constraint is a Range attribute. Replace with explicit
5854 -- mention of the bounds of the prefix, which must be a
5855 -- subtype.
5857 Analyze (Prefix (R));
5858 Hi :=
5859 Convert_To (Implicit_Base,
5860 Make_Attribute_Reference (Loc,
5861 Attribute_Name => Name_Last,
5862 Prefix =>
5863 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
5865 Lo :=
5866 Convert_To (Implicit_Base,
5867 Make_Attribute_Reference (Loc,
5868 Attribute_Name => Name_First,
5869 Prefix =>
5870 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
5871 end if;
5872 end;
5874 else
5875 Hi :=
5876 Build_Scalar_Bound
5877 (Type_High_Bound (Parent_Type),
5878 Parent_Type, Implicit_Base);
5879 Lo :=
5880 Build_Scalar_Bound
5881 (Type_Low_Bound (Parent_Type),
5882 Parent_Type, Implicit_Base);
5883 end if;
5885 Rang_Expr :=
5886 Make_Range (Loc,
5887 Low_Bound => Lo,
5888 High_Bound => Hi);
5890 -- If we constructed a default range for the case where no range
5891 -- was given, then the expressions in the range must not freeze
5892 -- since they do not correspond to expressions in the source.
5894 if Nkind (Indic) /= N_Subtype_Indication then
5895 Set_Must_Not_Freeze (Lo);
5896 Set_Must_Not_Freeze (Hi);
5897 Set_Must_Not_Freeze (Rang_Expr);
5898 end if;
5900 Rewrite (N,
5901 Make_Subtype_Declaration (Loc,
5902 Defining_Identifier => Derived_Type,
5903 Subtype_Indication =>
5904 Make_Subtype_Indication (Loc,
5905 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
5906 Constraint =>
5907 Make_Range_Constraint (Loc,
5908 Range_Expression => Rang_Expr))));
5910 Analyze (N);
5912 -- Apply a range check. Since this range expression doesn't have an
5913 -- Etype, we have to specifically pass the Source_Typ parameter. Is
5914 -- this right???
5916 if Nkind (Indic) = N_Subtype_Indication then
5917 Apply_Range_Check (Range_Expression (Constraint (Indic)),
5918 Parent_Type,
5919 Source_Typ => Entity (Subtype_Mark (Indic)));
5920 end if;
5921 end if;
5922 end Build_Derived_Enumeration_Type;
5924 --------------------------------
5925 -- Build_Derived_Numeric_Type --
5926 --------------------------------
5928 procedure Build_Derived_Numeric_Type
5929 (N : Node_Id;
5930 Parent_Type : Entity_Id;
5931 Derived_Type : Entity_Id)
5933 Loc : constant Source_Ptr := Sloc (N);
5934 Tdef : constant Node_Id := Type_Definition (N);
5935 Indic : constant Node_Id := Subtype_Indication (Tdef);
5936 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
5937 No_Constraint : constant Boolean := Nkind (Indic) /=
5938 N_Subtype_Indication;
5939 Implicit_Base : Entity_Id;
5941 Lo : Node_Id;
5942 Hi : Node_Id;
5944 begin
5945 -- Process the subtype indication including a validation check on
5946 -- the constraint if any.
5948 Discard_Node (Process_Subtype (Indic, N));
5950 -- Introduce an implicit base type for the derived type even if there
5951 -- is no constraint attached to it, since this seems closer to the Ada
5952 -- semantics.
5954 Implicit_Base :=
5955 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
5957 Set_Etype (Implicit_Base, Parent_Base);
5958 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
5959 Set_Size_Info (Implicit_Base, Parent_Base);
5960 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
5961 Set_Parent (Implicit_Base, Parent (Derived_Type));
5962 Set_Is_Known_Valid (Implicit_Base, Is_Known_Valid (Parent_Base));
5964 -- Set RM Size for discrete type or decimal fixed-point type
5965 -- Ordinary fixed-point is excluded, why???
5967 if Is_Discrete_Type (Parent_Base)
5968 or else Is_Decimal_Fixed_Point_Type (Parent_Base)
5969 then
5970 Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
5971 end if;
5973 Set_Has_Delayed_Freeze (Implicit_Base);
5975 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
5976 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
5978 Set_Scalar_Range (Implicit_Base,
5979 Make_Range (Loc,
5980 Low_Bound => Lo,
5981 High_Bound => Hi));
5983 if Has_Infinities (Parent_Base) then
5984 Set_Includes_Infinities (Scalar_Range (Implicit_Base));
5985 end if;
5987 -- The Derived_Type, which is the entity of the declaration, is a
5988 -- subtype of the implicit base. Its Ekind is a subtype, even in the
5989 -- absence of an explicit constraint.
5991 Set_Etype (Derived_Type, Implicit_Base);
5993 -- If we did not have a constraint, then the Ekind is set from the
5994 -- parent type (otherwise Process_Subtype has set the bounds)
5996 if No_Constraint then
5997 Set_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
5998 end if;
6000 -- If we did not have a range constraint, then set the range from the
6001 -- parent type. Otherwise, the Process_Subtype call has set the bounds.
6003 if No_Constraint
6004 or else not Has_Range_Constraint (Indic)
6005 then
6006 Set_Scalar_Range (Derived_Type,
6007 Make_Range (Loc,
6008 Low_Bound => New_Copy_Tree (Type_Low_Bound (Parent_Type)),
6009 High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
6010 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
6012 if Has_Infinities (Parent_Type) then
6013 Set_Includes_Infinities (Scalar_Range (Derived_Type));
6014 end if;
6016 Set_Is_Known_Valid (Derived_Type, Is_Known_Valid (Parent_Type));
6017 end if;
6019 Set_Is_Descendent_Of_Address (Derived_Type,
6020 Is_Descendent_Of_Address (Parent_Type));
6021 Set_Is_Descendent_Of_Address (Implicit_Base,
6022 Is_Descendent_Of_Address (Parent_Type));
6024 -- Set remaining type-specific fields, depending on numeric type
6026 if Is_Modular_Integer_Type (Parent_Type) then
6027 Set_Modulus (Implicit_Base, Modulus (Parent_Base));
6029 Set_Non_Binary_Modulus
6030 (Implicit_Base, Non_Binary_Modulus (Parent_Base));
6032 Set_Is_Known_Valid
6033 (Implicit_Base, Is_Known_Valid (Parent_Base));
6035 elsif Is_Floating_Point_Type (Parent_Type) then
6037 -- Digits of base type is always copied from the digits value of
6038 -- the parent base type, but the digits of the derived type will
6039 -- already have been set if there was a constraint present.
6041 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
6042 Set_Float_Rep (Implicit_Base, Float_Rep (Parent_Base));
6044 if No_Constraint then
6045 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
6046 end if;
6048 elsif Is_Fixed_Point_Type (Parent_Type) then
6050 -- Small of base type and derived type are always copied from the
6051 -- parent base type, since smalls never change. The delta of the
6052 -- base type is also copied from the parent base type. However the
6053 -- delta of the derived type will have been set already if a
6054 -- constraint was present.
6056 Set_Small_Value (Derived_Type, Small_Value (Parent_Base));
6057 Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
6058 Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
6060 if No_Constraint then
6061 Set_Delta_Value (Derived_Type, Delta_Value (Parent_Type));
6062 end if;
6064 -- The scale and machine radix in the decimal case are always
6065 -- copied from the parent base type.
6067 if Is_Decimal_Fixed_Point_Type (Parent_Type) then
6068 Set_Scale_Value (Derived_Type, Scale_Value (Parent_Base));
6069 Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
6071 Set_Machine_Radix_10
6072 (Derived_Type, Machine_Radix_10 (Parent_Base));
6073 Set_Machine_Radix_10
6074 (Implicit_Base, Machine_Radix_10 (Parent_Base));
6076 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
6078 if No_Constraint then
6079 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
6081 else
6082 -- the analysis of the subtype_indication sets the
6083 -- digits value of the derived type.
6085 null;
6086 end if;
6087 end if;
6088 end if;
6090 -- The type of the bounds is that of the parent type, and they
6091 -- must be converted to the derived type.
6093 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
6095 -- The implicit_base should be frozen when the derived type is frozen,
6096 -- but note that it is used in the conversions of the bounds. For fixed
6097 -- types we delay the determination of the bounds until the proper
6098 -- freezing point. For other numeric types this is rejected by GCC, for
6099 -- reasons that are currently unclear (???), so we choose to freeze the
6100 -- implicit base now. In the case of integers and floating point types
6101 -- this is harmless because subsequent representation clauses cannot
6102 -- affect anything, but it is still baffling that we cannot use the
6103 -- same mechanism for all derived numeric types.
6105 -- There is a further complication: actually *some* representation
6106 -- clauses can affect the implicit base type. Namely, attribute
6107 -- definition clauses for stream-oriented attributes need to set the
6108 -- corresponding TSS entries on the base type, and this normally cannot
6109 -- be done after the base type is frozen, so the circuitry in
6110 -- Sem_Ch13.New_Stream_Subprogram must account for this possibility and
6111 -- not use Set_TSS in this case.
6113 if Is_Fixed_Point_Type (Parent_Type) then
6114 Conditional_Delay (Implicit_Base, Parent_Type);
6115 else
6116 Freeze_Before (N, Implicit_Base);
6117 end if;
6118 end Build_Derived_Numeric_Type;
6120 --------------------------------
6121 -- Build_Derived_Private_Type --
6122 --------------------------------
6124 procedure Build_Derived_Private_Type
6125 (N : Node_Id;
6126 Parent_Type : Entity_Id;
6127 Derived_Type : Entity_Id;
6128 Is_Completion : Boolean;
6129 Derive_Subps : Boolean := True)
6131 Loc : constant Source_Ptr := Sloc (N);
6132 Der_Base : Entity_Id;
6133 Discr : Entity_Id;
6134 Full_Decl : Node_Id := Empty;
6135 Full_Der : Entity_Id;
6136 Full_P : Entity_Id;
6137 Last_Discr : Entity_Id;
6138 Par_Scope : constant Entity_Id := Scope (Base_Type (Parent_Type));
6139 Swapped : Boolean := False;
6141 procedure Copy_And_Build;
6142 -- Copy derived type declaration, replace parent with its full view,
6143 -- and analyze new declaration.
6145 --------------------
6146 -- Copy_And_Build --
6147 --------------------
6149 procedure Copy_And_Build is
6150 Full_N : Node_Id;
6152 begin
6153 if Ekind (Parent_Type) in Record_Kind
6154 or else
6155 (Ekind (Parent_Type) in Enumeration_Kind
6156 and then not Is_Standard_Character_Type (Parent_Type)
6157 and then not Is_Generic_Type (Root_Type (Parent_Type)))
6158 then
6159 Full_N := New_Copy_Tree (N);
6160 Insert_After (N, Full_N);
6161 Build_Derived_Type (
6162 Full_N, Parent_Type, Full_Der, True, Derive_Subps => False);
6164 else
6165 Build_Derived_Type (
6166 N, Parent_Type, Full_Der, True, Derive_Subps => False);
6167 end if;
6168 end Copy_And_Build;
6170 -- Start of processing for Build_Derived_Private_Type
6172 begin
6173 if Is_Tagged_Type (Parent_Type) then
6174 Full_P := Full_View (Parent_Type);
6176 -- A type extension of a type with unknown discriminants is an
6177 -- indefinite type that the back-end cannot handle directly.
6178 -- We treat it as a private type, and build a completion that is
6179 -- derived from the full view of the parent, and hopefully has
6180 -- known discriminants.
6182 -- If the full view of the parent type has an underlying record view,
6183 -- use it to generate the underlying record view of this derived type
6184 -- (required for chains of derivations with unknown discriminants).
6186 -- Minor optimization: we avoid the generation of useless underlying
6187 -- record view entities if the private type declaration has unknown
6188 -- discriminants but its corresponding full view has no
6189 -- discriminants.
6191 if Has_Unknown_Discriminants (Parent_Type)
6192 and then Present (Full_P)
6193 and then (Has_Discriminants (Full_P)
6194 or else Present (Underlying_Record_View (Full_P)))
6195 and then not In_Open_Scopes (Par_Scope)
6196 and then Expander_Active
6197 then
6198 declare
6199 Full_Der : constant Entity_Id := Make_Temporary (Loc, 'T');
6200 New_Ext : constant Node_Id :=
6201 Copy_Separate_Tree
6202 (Record_Extension_Part (Type_Definition (N)));
6203 Decl : Node_Id;
6205 begin
6206 Build_Derived_Record_Type
6207 (N, Parent_Type, Derived_Type, Derive_Subps);
6209 -- Build anonymous completion, as a derivation from the full
6210 -- view of the parent. This is not a completion in the usual
6211 -- sense, because the current type is not private.
6213 Decl :=
6214 Make_Full_Type_Declaration (Loc,
6215 Defining_Identifier => Full_Der,
6216 Type_Definition =>
6217 Make_Derived_Type_Definition (Loc,
6218 Subtype_Indication =>
6219 New_Copy_Tree
6220 (Subtype_Indication (Type_Definition (N))),
6221 Record_Extension_Part => New_Ext));
6223 -- If the parent type has an underlying record view, use it
6224 -- here to build the new underlying record view.
6226 if Present (Underlying_Record_View (Full_P)) then
6227 pragma Assert
6228 (Nkind (Subtype_Indication (Type_Definition (Decl)))
6229 = N_Identifier);
6230 Set_Entity (Subtype_Indication (Type_Definition (Decl)),
6231 Underlying_Record_View (Full_P));
6232 end if;
6234 Install_Private_Declarations (Par_Scope);
6235 Install_Visible_Declarations (Par_Scope);
6236 Insert_Before (N, Decl);
6238 -- Mark entity as an underlying record view before analysis,
6239 -- to avoid generating the list of its primitive operations
6240 -- (which is not really required for this entity) and thus
6241 -- prevent spurious errors associated with missing overriding
6242 -- of abstract primitives (overridden only for Derived_Type).
6244 Set_Ekind (Full_Der, E_Record_Type);
6245 Set_Is_Underlying_Record_View (Full_Der);
6247 Analyze (Decl);
6249 pragma Assert (Has_Discriminants (Full_Der)
6250 and then not Has_Unknown_Discriminants (Full_Der));
6252 Uninstall_Declarations (Par_Scope);
6254 -- Freeze the underlying record view, to prevent generation of
6255 -- useless dispatching information, which is simply shared with
6256 -- the real derived type.
6258 Set_Is_Frozen (Full_Der);
6260 -- Set up links between real entity and underlying record view
6262 Set_Underlying_Record_View (Derived_Type, Base_Type (Full_Der));
6263 Set_Underlying_Record_View (Base_Type (Full_Der), Derived_Type);
6264 end;
6266 -- If discriminants are known, build derived record
6268 else
6269 Build_Derived_Record_Type
6270 (N, Parent_Type, Derived_Type, Derive_Subps);
6271 end if;
6273 return;
6275 elsif Has_Discriminants (Parent_Type) then
6276 if Present (Full_View (Parent_Type)) then
6277 if not Is_Completion then
6279 -- Copy declaration for subsequent analysis, to provide a
6280 -- completion for what is a private declaration. Indicate that
6281 -- the full type is internally generated.
6283 Full_Decl := New_Copy_Tree (N);
6284 Full_Der := New_Copy (Derived_Type);
6285 Set_Comes_From_Source (Full_Decl, False);
6286 Set_Comes_From_Source (Full_Der, False);
6287 Set_Parent (Full_Der, Full_Decl);
6289 Insert_After (N, Full_Decl);
6291 else
6292 -- If this is a completion, the full view being built is itself
6293 -- private. We build a subtype of the parent with the same
6294 -- constraints as this full view, to convey to the back end the
6295 -- constrained components and the size of this subtype. If the
6296 -- parent is constrained, its full view can serve as the
6297 -- underlying full view of the derived type.
6299 if No (Discriminant_Specifications (N)) then
6300 if Nkind (Subtype_Indication (Type_Definition (N))) =
6301 N_Subtype_Indication
6302 then
6303 Build_Underlying_Full_View (N, Derived_Type, Parent_Type);
6305 elsif Is_Constrained (Full_View (Parent_Type)) then
6306 Set_Underlying_Full_View
6307 (Derived_Type, Full_View (Parent_Type));
6308 end if;
6310 else
6311 -- If there are new discriminants, the parent subtype is
6312 -- constrained by them, but it is not clear how to build
6313 -- the Underlying_Full_View in this case???
6315 null;
6316 end if;
6317 end if;
6318 end if;
6320 -- Build partial view of derived type from partial view of parent
6322 Build_Derived_Record_Type
6323 (N, Parent_Type, Derived_Type, Derive_Subps);
6325 if Present (Full_View (Parent_Type)) and then not Is_Completion then
6326 if not In_Open_Scopes (Par_Scope)
6327 or else not In_Same_Source_Unit (N, Parent_Type)
6328 then
6329 -- Swap partial and full views temporarily
6331 Install_Private_Declarations (Par_Scope);
6332 Install_Visible_Declarations (Par_Scope);
6333 Swapped := True;
6334 end if;
6336 -- Build full view of derived type from full view of parent which
6337 -- is now installed. Subprograms have been derived on the partial
6338 -- view, the completion does not derive them anew.
6340 if not Is_Tagged_Type (Parent_Type) then
6342 -- If the parent is itself derived from another private type,
6343 -- installing the private declarations has not affected its
6344 -- privacy status, so use its own full view explicitly.
6346 if Is_Private_Type (Parent_Type) then
6347 Build_Derived_Record_Type
6348 (Full_Decl, Full_View (Parent_Type), Full_Der, False);
6349 else
6350 Build_Derived_Record_Type
6351 (Full_Decl, Parent_Type, Full_Der, False);
6352 end if;
6354 else
6355 -- If full view of parent is tagged, the completion inherits
6356 -- the proper primitive operations.
6358 Set_Defining_Identifier (Full_Decl, Full_Der);
6359 Build_Derived_Record_Type
6360 (Full_Decl, Parent_Type, Full_Der, Derive_Subps);
6361 end if;
6363 -- The full declaration has been introduced into the tree and
6364 -- processed in the step above. It should not be analyzed again
6365 -- (when encountered later in the current list of declarations)
6366 -- to prevent spurious name conflicts. The full entity remains
6367 -- invisible.
6369 Set_Analyzed (Full_Decl);
6371 if Swapped then
6372 Uninstall_Declarations (Par_Scope);
6374 if In_Open_Scopes (Par_Scope) then
6375 Install_Visible_Declarations (Par_Scope);
6376 end if;
6377 end if;
6379 Der_Base := Base_Type (Derived_Type);
6380 Set_Full_View (Derived_Type, Full_Der);
6381 Set_Full_View (Der_Base, Base_Type (Full_Der));
6383 -- Copy the discriminant list from full view to the partial views
6384 -- (base type and its subtype). Gigi requires that the partial and
6385 -- full views have the same discriminants.
6387 -- Note that since the partial view is pointing to discriminants
6388 -- in the full view, their scope will be that of the full view.
6389 -- This might cause some front end problems and need adjustment???
6391 Discr := First_Discriminant (Base_Type (Full_Der));
6392 Set_First_Entity (Der_Base, Discr);
6394 loop
6395 Last_Discr := Discr;
6396 Next_Discriminant (Discr);
6397 exit when No (Discr);
6398 end loop;
6400 Set_Last_Entity (Der_Base, Last_Discr);
6402 Set_First_Entity (Derived_Type, First_Entity (Der_Base));
6403 Set_Last_Entity (Derived_Type, Last_Entity (Der_Base));
6404 Set_Stored_Constraint (Full_Der, Stored_Constraint (Derived_Type));
6406 else
6407 -- If this is a completion, the derived type stays private and
6408 -- there is no need to create a further full view, except in the
6409 -- unusual case when the derivation is nested within a child unit,
6410 -- see below.
6412 null;
6413 end if;
6415 elsif Present (Full_View (Parent_Type))
6416 and then Has_Discriminants (Full_View (Parent_Type))
6417 then
6418 if Has_Unknown_Discriminants (Parent_Type)
6419 and then Nkind (Subtype_Indication (Type_Definition (N))) =
6420 N_Subtype_Indication
6421 then
6422 Error_Msg_N
6423 ("cannot constrain type with unknown discriminants",
6424 Subtype_Indication (Type_Definition (N)));
6425 return;
6426 end if;
6428 -- If full view of parent is a record type, build full view as a
6429 -- derivation from the parent's full view. Partial view remains
6430 -- private. For code generation and linking, the full view must have
6431 -- the same public status as the partial one. This full view is only
6432 -- needed if the parent type is in an enclosing scope, so that the
6433 -- full view may actually become visible, e.g. in a child unit. This
6434 -- is both more efficient, and avoids order of freezing problems with
6435 -- the added entities.
6437 if not Is_Private_Type (Full_View (Parent_Type))
6438 and then (In_Open_Scopes (Scope (Parent_Type)))
6439 then
6440 Full_Der :=
6441 Make_Defining_Identifier (Sloc (Derived_Type),
6442 Chars => Chars (Derived_Type));
6444 Set_Is_Itype (Full_Der);
6445 Set_Has_Private_Declaration (Full_Der);
6446 Set_Has_Private_Declaration (Derived_Type);
6447 Set_Associated_Node_For_Itype (Full_Der, N);
6448 Set_Parent (Full_Der, Parent (Derived_Type));
6449 Set_Full_View (Derived_Type, Full_Der);
6450 Set_Is_Public (Full_Der, Is_Public (Derived_Type));
6451 Full_P := Full_View (Parent_Type);
6452 Exchange_Declarations (Parent_Type);
6453 Copy_And_Build;
6454 Exchange_Declarations (Full_P);
6456 else
6457 Build_Derived_Record_Type
6458 (N, Full_View (Parent_Type), Derived_Type,
6459 Derive_Subps => False);
6461 -- Except in the context of the full view of the parent, there
6462 -- are no non-extension aggregates for the derived type.
6464 Set_Has_Private_Ancestor (Derived_Type);
6465 end if;
6467 -- In any case, the primitive operations are inherited from the
6468 -- parent type, not from the internal full view.
6470 Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
6472 if Derive_Subps then
6473 Derive_Subprograms (Parent_Type, Derived_Type);
6474 end if;
6476 else
6477 -- Untagged type, No discriminants on either view
6479 if Nkind (Subtype_Indication (Type_Definition (N))) =
6480 N_Subtype_Indication
6481 then
6482 Error_Msg_N
6483 ("illegal constraint on type without discriminants", N);
6484 end if;
6486 if Present (Discriminant_Specifications (N))
6487 and then Present (Full_View (Parent_Type))
6488 and then not Is_Tagged_Type (Full_View (Parent_Type))
6489 then
6490 Error_Msg_N ("cannot add discriminants to untagged type", N);
6491 end if;
6493 Set_Stored_Constraint (Derived_Type, No_Elist);
6494 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
6495 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
6496 Set_Has_Controlled_Component
6497 (Derived_Type, Has_Controlled_Component
6498 (Parent_Type));
6500 -- Direct controlled types do not inherit Finalize_Storage_Only flag
6502 if not Is_Controlled (Parent_Type) then
6503 Set_Finalize_Storage_Only
6504 (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
6505 end if;
6507 -- Construct the implicit full view by deriving from full view of the
6508 -- parent type. In order to get proper visibility, we install the
6509 -- parent scope and its declarations.
6511 -- ??? If the parent is untagged private and its completion is
6512 -- tagged, this mechanism will not work because we cannot derive from
6513 -- the tagged full view unless we have an extension.
6515 if Present (Full_View (Parent_Type))
6516 and then not Is_Tagged_Type (Full_View (Parent_Type))
6517 and then not Is_Completion
6518 then
6519 Full_Der :=
6520 Make_Defining_Identifier
6521 (Sloc (Derived_Type), Chars (Derived_Type));
6522 Set_Is_Itype (Full_Der);
6523 Set_Has_Private_Declaration (Full_Der);
6524 Set_Has_Private_Declaration (Derived_Type);
6525 Set_Associated_Node_For_Itype (Full_Der, N);
6526 Set_Parent (Full_Der, Parent (Derived_Type));
6527 Set_Full_View (Derived_Type, Full_Der);
6529 if not In_Open_Scopes (Par_Scope) then
6530 Install_Private_Declarations (Par_Scope);
6531 Install_Visible_Declarations (Par_Scope);
6532 Copy_And_Build;
6533 Uninstall_Declarations (Par_Scope);
6535 -- If parent scope is open and in another unit, and parent has a
6536 -- completion, then the derivation is taking place in the visible
6537 -- part of a child unit. In that case retrieve the full view of
6538 -- the parent momentarily.
6540 elsif not In_Same_Source_Unit (N, Parent_Type) then
6541 Full_P := Full_View (Parent_Type);
6542 Exchange_Declarations (Parent_Type);
6543 Copy_And_Build;
6544 Exchange_Declarations (Full_P);
6546 -- Otherwise it is a local derivation
6548 else
6549 Copy_And_Build;
6550 end if;
6552 Set_Scope (Full_Der, Current_Scope);
6553 Set_Is_First_Subtype (Full_Der,
6554 Is_First_Subtype (Derived_Type));
6555 Set_Has_Size_Clause (Full_Der, False);
6556 Set_Has_Alignment_Clause (Full_Der, False);
6557 Set_Next_Entity (Full_Der, Empty);
6558 Set_Has_Delayed_Freeze (Full_Der);
6559 Set_Is_Frozen (Full_Der, False);
6560 Set_Freeze_Node (Full_Der, Empty);
6561 Set_Depends_On_Private (Full_Der,
6562 Has_Private_Component (Full_Der));
6563 Set_Public_Status (Full_Der);
6564 end if;
6565 end if;
6567 Set_Has_Unknown_Discriminants (Derived_Type,
6568 Has_Unknown_Discriminants (Parent_Type));
6570 if Is_Private_Type (Derived_Type) then
6571 Set_Private_Dependents (Derived_Type, New_Elmt_List);
6572 end if;
6574 if Is_Private_Type (Parent_Type)
6575 and then Base_Type (Parent_Type) = Parent_Type
6576 and then In_Open_Scopes (Scope (Parent_Type))
6577 then
6578 Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
6580 -- Check for unusual case where a type completed by a private
6581 -- derivation occurs within a package nested in a child unit, and
6582 -- the parent is declared in an ancestor.
6584 if Is_Child_Unit (Scope (Current_Scope))
6585 and then Is_Completion
6586 and then In_Private_Part (Current_Scope)
6587 and then Scope (Parent_Type) /= Current_Scope
6589 -- Note that if the parent has a completion in the private part,
6590 -- (which is itself a derivation from some other private type)
6591 -- it is that completion that is visible, there is no full view
6592 -- available, and no special processing is needed.
6594 and then Present (Full_View (Parent_Type))
6595 then
6596 -- In this case, the full view of the parent type will become
6597 -- visible in the body of the enclosing child, and only then will
6598 -- the current type be possibly non-private. We build an
6599 -- underlying full view that will be installed when the enclosing
6600 -- child body is compiled.
6602 Full_Der :=
6603 Make_Defining_Identifier
6604 (Sloc (Derived_Type), Chars (Derived_Type));
6605 Set_Is_Itype (Full_Der);
6606 Build_Itype_Reference (Full_Der, N);
6608 -- The full view will be used to swap entities on entry/exit to
6609 -- the body, and must appear in the entity list for the package.
6611 Append_Entity (Full_Der, Scope (Derived_Type));
6612 Set_Has_Private_Declaration (Full_Der);
6613 Set_Has_Private_Declaration (Derived_Type);
6614 Set_Associated_Node_For_Itype (Full_Der, N);
6615 Set_Parent (Full_Der, Parent (Derived_Type));
6616 Full_P := Full_View (Parent_Type);
6617 Exchange_Declarations (Parent_Type);
6618 Copy_And_Build;
6619 Exchange_Declarations (Full_P);
6620 Set_Underlying_Full_View (Derived_Type, Full_Der);
6621 end if;
6622 end if;
6623 end Build_Derived_Private_Type;
6625 -------------------------------
6626 -- Build_Derived_Record_Type --
6627 -------------------------------
6629 -- 1. INTRODUCTION
6631 -- Ideally we would like to use the same model of type derivation for
6632 -- tagged and untagged record types. Unfortunately this is not quite
6633 -- possible because the semantics of representation clauses is different
6634 -- for tagged and untagged records under inheritance. Consider the
6635 -- following:
6637 -- type R (...) is [tagged] record ... end record;
6638 -- type T (...) is new R (...) [with ...];
6640 -- The representation clauses for T can specify a completely different
6641 -- record layout from R's. Hence the same component can be placed in two
6642 -- very different positions in objects of type T and R. If R and T are
6643 -- tagged types, representation clauses for T can only specify the layout
6644 -- of non inherited components, thus components that are common in R and T
6645 -- have the same position in objects of type R and T.
6647 -- This has two implications. The first is that the entire tree for R's
6648 -- declaration needs to be copied for T in the untagged case, so that T
6649 -- can be viewed as a record type of its own with its own representation
6650 -- clauses. The second implication is the way we handle discriminants.
6651 -- Specifically, in the untagged case we need a way to communicate to Gigi
6652 -- what are the real discriminants in the record, while for the semantics
6653 -- we need to consider those introduced by the user to rename the
6654 -- discriminants in the parent type. This is handled by introducing the
6655 -- notion of stored discriminants. See below for more.
6657 -- Fortunately the way regular components are inherited can be handled in
6658 -- the same way in tagged and untagged types.
6660 -- To complicate things a bit more the private view of a private extension
6661 -- cannot be handled in the same way as the full view (for one thing the
6662 -- semantic rules are somewhat different). We will explain what differs
6663 -- below.
6665 -- 2. DISCRIMINANTS UNDER INHERITANCE
6667 -- The semantic rules governing the discriminants of derived types are
6668 -- quite subtle.
6670 -- type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
6671 -- [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
6673 -- If parent type has discriminants, then the discriminants that are
6674 -- declared in the derived type are [3.4 (11)]:
6676 -- o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
6677 -- there is one;
6679 -- o Otherwise, each discriminant of the parent type (implicitly declared
6680 -- in the same order with the same specifications). In this case, the
6681 -- discriminants are said to be "inherited", or if unknown in the parent
6682 -- are also unknown in the derived type.
6684 -- Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
6686 -- o The parent subtype shall be constrained;
6688 -- o If the parent type is not a tagged type, then each discriminant of
6689 -- the derived type shall be used in the constraint defining a parent
6690 -- subtype. [Implementation note: This ensures that the new discriminant
6691 -- can share storage with an existing discriminant.]
6693 -- For the derived type each discriminant of the parent type is either
6694 -- inherited, constrained to equal some new discriminant of the derived
6695 -- type, or constrained to the value of an expression.
6697 -- When inherited or constrained to equal some new discriminant, the
6698 -- parent discriminant and the discriminant of the derived type are said
6699 -- to "correspond".
6701 -- If a discriminant of the parent type is constrained to a specific value
6702 -- in the derived type definition, then the discriminant is said to be
6703 -- "specified" by that derived type definition.
6705 -- 3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
6707 -- We have spoken about stored discriminants in point 1 (introduction)
6708 -- above. There are two sort of stored discriminants: implicit and
6709 -- explicit. As long as the derived type inherits the same discriminants as
6710 -- the root record type, stored discriminants are the same as regular
6711 -- discriminants, and are said to be implicit. However, if any discriminant
6712 -- in the root type was renamed in the derived type, then the derived
6713 -- type will contain explicit stored discriminants. Explicit stored
6714 -- discriminants are discriminants in addition to the semantically visible
6715 -- discriminants defined for the derived type. Stored discriminants are
6716 -- used by Gigi to figure out what are the physical discriminants in
6717 -- objects of the derived type (see precise definition in einfo.ads).
6718 -- As an example, consider the following:
6720 -- type R (D1, D2, D3 : Int) is record ... end record;
6721 -- type T1 is new R;
6722 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
6723 -- type T3 is new T2;
6724 -- type T4 (Y : Int) is new T3 (Y, 99);
6726 -- The following table summarizes the discriminants and stored
6727 -- discriminants in R and T1 through T4.
6729 -- Type Discrim Stored Discrim Comment
6730 -- R (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in R
6731 -- T1 (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in T1
6732 -- T2 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T2
6733 -- T3 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T3
6734 -- T4 (Y) (D1, D2, D3) Girder discrims EXPLICIT in T4
6736 -- Field Corresponding_Discriminant (abbreviated CD below) allows us to
6737 -- find the corresponding discriminant in the parent type, while
6738 -- Original_Record_Component (abbreviated ORC below), the actual physical
6739 -- component that is renamed. Finally the field Is_Completely_Hidden
6740 -- (abbreviated ICH below) is set for all explicit stored discriminants
6741 -- (see einfo.ads for more info). For the above example this gives:
6743 -- Discrim CD ORC ICH
6744 -- ^^^^^^^ ^^ ^^^ ^^^
6745 -- D1 in R empty itself no
6746 -- D2 in R empty itself no
6747 -- D3 in R empty itself no
6749 -- D1 in T1 D1 in R itself no
6750 -- D2 in T1 D2 in R itself no
6751 -- D3 in T1 D3 in R itself no
6753 -- X1 in T2 D3 in T1 D3 in T2 no
6754 -- X2 in T2 D1 in T1 D1 in T2 no
6755 -- D1 in T2 empty itself yes
6756 -- D2 in T2 empty itself yes
6757 -- D3 in T2 empty itself yes
6759 -- X1 in T3 X1 in T2 D3 in T3 no
6760 -- X2 in T3 X2 in T2 D1 in T3 no
6761 -- D1 in T3 empty itself yes
6762 -- D2 in T3 empty itself yes
6763 -- D3 in T3 empty itself yes
6765 -- Y in T4 X1 in T3 D3 in T3 no
6766 -- D1 in T3 empty itself yes
6767 -- D2 in T3 empty itself yes
6768 -- D3 in T3 empty itself yes
6770 -- 4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
6772 -- Type derivation for tagged types is fairly straightforward. If no
6773 -- discriminants are specified by the derived type, these are inherited
6774 -- from the parent. No explicit stored discriminants are ever necessary.
6775 -- The only manipulation that is done to the tree is that of adding a
6776 -- _parent field with parent type and constrained to the same constraint
6777 -- specified for the parent in the derived type definition. For instance:
6779 -- type R (D1, D2, D3 : Int) is tagged record ... end record;
6780 -- type T1 is new R with null record;
6781 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
6783 -- are changed into:
6785 -- type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
6786 -- _parent : R (D1, D2, D3);
6787 -- end record;
6789 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
6790 -- _parent : T1 (X2, 88, X1);
6791 -- end record;
6793 -- The discriminants actually present in R, T1 and T2 as well as their CD,
6794 -- ORC and ICH fields are:
6796 -- Discrim CD ORC ICH
6797 -- ^^^^^^^ ^^ ^^^ ^^^
6798 -- D1 in R empty itself no
6799 -- D2 in R empty itself no
6800 -- D3 in R empty itself no
6802 -- D1 in T1 D1 in R D1 in R no
6803 -- D2 in T1 D2 in R D2 in R no
6804 -- D3 in T1 D3 in R D3 in R no
6806 -- X1 in T2 D3 in T1 D3 in R no
6807 -- X2 in T2 D1 in T1 D1 in R no
6809 -- 5. FIRST TRANSFORMATION FOR DERIVED RECORDS
6811 -- Regardless of whether we dealing with a tagged or untagged type
6812 -- we will transform all derived type declarations of the form
6814 -- type T is new R (...) [with ...];
6815 -- or
6816 -- subtype S is R (...);
6817 -- type T is new S [with ...];
6818 -- into
6819 -- type BT is new R [with ...];
6820 -- subtype T is BT (...);
6822 -- That is, the base derived type is constrained only if it has no
6823 -- discriminants. The reason for doing this is that GNAT's semantic model
6824 -- assumes that a base type with discriminants is unconstrained.
6826 -- Note that, strictly speaking, the above transformation is not always
6827 -- correct. Consider for instance the following excerpt from ACVC b34011a:
6829 -- procedure B34011A is
6830 -- type REC (D : integer := 0) is record
6831 -- I : Integer;
6832 -- end record;
6834 -- package P is
6835 -- type T6 is new Rec;
6836 -- function F return T6;
6837 -- end P;
6839 -- use P;
6840 -- package Q6 is
6841 -- type U is new T6 (Q6.F.I); -- ERROR: Q6.F.
6842 -- end Q6;
6844 -- The definition of Q6.U is illegal. However transforming Q6.U into
6846 -- type BaseU is new T6;
6847 -- subtype U is BaseU (Q6.F.I)
6849 -- turns U into a legal subtype, which is incorrect. To avoid this problem
6850 -- we always analyze the constraint (in this case (Q6.F.I)) before applying
6851 -- the transformation described above.
6853 -- There is another instance where the above transformation is incorrect.
6854 -- Consider:
6856 -- package Pack is
6857 -- type Base (D : Integer) is tagged null record;
6858 -- procedure P (X : Base);
6860 -- type Der is new Base (2) with null record;
6861 -- procedure P (X : Der);
6862 -- end Pack;
6864 -- Then the above transformation turns this into
6866 -- type Der_Base is new Base with null record;
6867 -- -- procedure P (X : Base) is implicitly inherited here
6868 -- -- as procedure P (X : Der_Base).
6870 -- subtype Der is Der_Base (2);
6871 -- procedure P (X : Der);
6872 -- -- The overriding of P (X : Der_Base) is illegal since we
6873 -- -- have a parameter conformance problem.
6875 -- To get around this problem, after having semantically processed Der_Base
6876 -- and the rewritten subtype declaration for Der, we copy Der_Base field
6877 -- Discriminant_Constraint from Der so that when parameter conformance is
6878 -- checked when P is overridden, no semantic errors are flagged.
6880 -- 6. SECOND TRANSFORMATION FOR DERIVED RECORDS
6882 -- Regardless of whether we are dealing with a tagged or untagged type
6883 -- we will transform all derived type declarations of the form
6885 -- type R (D1, .., Dn : ...) is [tagged] record ...;
6886 -- type T is new R [with ...];
6887 -- into
6888 -- type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
6890 -- The reason for such transformation is that it allows us to implement a
6891 -- very clean form of component inheritance as explained below.
6893 -- Note that this transformation is not achieved by direct tree rewriting
6894 -- and manipulation, but rather by redoing the semantic actions that the
6895 -- above transformation will entail. This is done directly in routine
6896 -- Inherit_Components.
6898 -- 7. TYPE DERIVATION AND COMPONENT INHERITANCE
6900 -- In both tagged and untagged derived types, regular non discriminant
6901 -- components are inherited in the derived type from the parent type. In
6902 -- the absence of discriminants component, inheritance is straightforward
6903 -- as components can simply be copied from the parent.
6905 -- If the parent has discriminants, inheriting components constrained with
6906 -- these discriminants requires caution. Consider the following example:
6908 -- type R (D1, D2 : Positive) is [tagged] record
6909 -- S : String (D1 .. D2);
6910 -- end record;
6912 -- type T1 is new R [with null record];
6913 -- type T2 (X : positive) is new R (1, X) [with null record];
6915 -- As explained in 6. above, T1 is rewritten as
6916 -- type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
6917 -- which makes the treatment for T1 and T2 identical.
6919 -- What we want when inheriting S, is that references to D1 and D2 in R are
6920 -- replaced with references to their correct constraints, i.e. D1 and D2 in
6921 -- T1 and 1 and X in T2. So all R's discriminant references are replaced
6922 -- with either discriminant references in the derived type or expressions.
6923 -- This replacement is achieved as follows: before inheriting R's
6924 -- components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
6925 -- created in the scope of T1 (resp. scope of T2) so that discriminants D1
6926 -- and D2 of T1 are visible (resp. discriminant X of T2 is visible).
6927 -- For T2, for instance, this has the effect of replacing String (D1 .. D2)
6928 -- by String (1 .. X).
6930 -- 8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
6932 -- We explain here the rules governing private type extensions relevant to
6933 -- type derivation. These rules are explained on the following example:
6935 -- type D [(...)] is new A [(...)] with private; <-- partial view
6936 -- type D [(...)] is new P [(...)] with null record; <-- full view
6938 -- Type A is called the ancestor subtype of the private extension.
6939 -- Type P is the parent type of the full view of the private extension. It
6940 -- must be A or a type derived from A.
6942 -- The rules concerning the discriminants of private type extensions are
6943 -- [7.3(10-13)]:
6945 -- o If a private extension inherits known discriminants from the ancestor
6946 -- subtype, then the full view shall also inherit its discriminants from
6947 -- the ancestor subtype and the parent subtype of the full view shall be
6948 -- constrained if and only if the ancestor subtype is constrained.
6950 -- o If a partial view has unknown discriminants, then the full view may
6951 -- define a definite or an indefinite subtype, with or without
6952 -- discriminants.
6954 -- o If a partial view has neither known nor unknown discriminants, then
6955 -- the full view shall define a definite subtype.
6957 -- o If the ancestor subtype of a private extension has constrained
6958 -- discriminants, then the parent subtype of the full view shall impose a
6959 -- statically matching constraint on those discriminants.
6961 -- This means that only the following forms of private extensions are
6962 -- allowed:
6964 -- type D is new A with private; <-- partial view
6965 -- type D is new P with null record; <-- full view
6967 -- If A has no discriminants than P has no discriminants, otherwise P must
6968 -- inherit A's discriminants.
6970 -- type D is new A (...) with private; <-- partial view
6971 -- type D is new P (:::) with null record; <-- full view
6973 -- P must inherit A's discriminants and (...) and (:::) must statically
6974 -- match.
6976 -- subtype A is R (...);
6977 -- type D is new A with private; <-- partial view
6978 -- type D is new P with null record; <-- full view
6980 -- P must have inherited R's discriminants and must be derived from A or
6981 -- any of its subtypes.
6983 -- type D (..) is new A with private; <-- partial view
6984 -- type D (..) is new P [(:::)] with null record; <-- full view
6986 -- No specific constraints on P's discriminants or constraint (:::).
6987 -- Note that A can be unconstrained, but the parent subtype P must either
6988 -- be constrained or (:::) must be present.
6990 -- type D (..) is new A [(...)] with private; <-- partial view
6991 -- type D (..) is new P [(:::)] with null record; <-- full view
6993 -- P's constraints on A's discriminants must statically match those
6994 -- imposed by (...).
6996 -- 9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
6998 -- The full view of a private extension is handled exactly as described
6999 -- above. The model chose for the private view of a private extension is
7000 -- the same for what concerns discriminants (i.e. they receive the same
7001 -- treatment as in the tagged case). However, the private view of the
7002 -- private extension always inherits the components of the parent base,
7003 -- without replacing any discriminant reference. Strictly speaking this is
7004 -- incorrect. However, Gigi never uses this view to generate code so this
7005 -- is a purely semantic issue. In theory, a set of transformations similar
7006 -- to those given in 5. and 6. above could be applied to private views of
7007 -- private extensions to have the same model of component inheritance as
7008 -- for non private extensions. However, this is not done because it would
7009 -- further complicate private type processing. Semantically speaking, this
7010 -- leaves us in an uncomfortable situation. As an example consider:
7012 -- package Pack is
7013 -- type R (D : integer) is tagged record
7014 -- S : String (1 .. D);
7015 -- end record;
7016 -- procedure P (X : R);
7017 -- type T is new R (1) with private;
7018 -- private
7019 -- type T is new R (1) with null record;
7020 -- end;
7022 -- This is transformed into:
7024 -- package Pack is
7025 -- type R (D : integer) is tagged record
7026 -- S : String (1 .. D);
7027 -- end record;
7028 -- procedure P (X : R);
7029 -- type T is new R (1) with private;
7030 -- private
7031 -- type BaseT is new R with null record;
7032 -- subtype T is BaseT (1);
7033 -- end;
7035 -- (strictly speaking the above is incorrect Ada)
7037 -- From the semantic standpoint the private view of private extension T
7038 -- should be flagged as constrained since one can clearly have
7040 -- Obj : T;
7042 -- in a unit withing Pack. However, when deriving subprograms for the
7043 -- private view of private extension T, T must be seen as unconstrained
7044 -- since T has discriminants (this is a constraint of the current
7045 -- subprogram derivation model). Thus, when processing the private view of
7046 -- a private extension such as T, we first mark T as unconstrained, we
7047 -- process it, we perform program derivation and just before returning from
7048 -- Build_Derived_Record_Type we mark T as constrained.
7050 -- ??? Are there are other uncomfortable cases that we will have to
7051 -- deal with.
7053 -- 10. RECORD_TYPE_WITH_PRIVATE complications
7055 -- Types that are derived from a visible record type and have a private
7056 -- extension present other peculiarities. They behave mostly like private
7057 -- types, but if they have primitive operations defined, these will not
7058 -- have the proper signatures for further inheritance, because other
7059 -- primitive operations will use the implicit base that we define for
7060 -- private derivations below. This affect subprogram inheritance (see
7061 -- Derive_Subprograms for details). We also derive the implicit base from
7062 -- the base type of the full view, so that the implicit base is a record
7063 -- type and not another private type, This avoids infinite loops.
7065 procedure Build_Derived_Record_Type
7066 (N : Node_Id;
7067 Parent_Type : Entity_Id;
7068 Derived_Type : Entity_Id;
7069 Derive_Subps : Boolean := True)
7071 Discriminant_Specs : constant Boolean :=
7072 Present (Discriminant_Specifications (N));
7073 Is_Tagged : constant Boolean := Is_Tagged_Type (Parent_Type);
7074 Loc : constant Source_Ptr := Sloc (N);
7075 Private_Extension : constant Boolean :=
7076 Nkind (N) = N_Private_Extension_Declaration;
7077 Assoc_List : Elist_Id;
7078 Constraint_Present : Boolean;
7079 Constrs : Elist_Id;
7080 Discrim : Entity_Id;
7081 Indic : Node_Id;
7082 Inherit_Discrims : Boolean := False;
7083 Last_Discrim : Entity_Id;
7084 New_Base : Entity_Id;
7085 New_Decl : Node_Id;
7086 New_Discrs : Elist_Id;
7087 New_Indic : Node_Id;
7088 Parent_Base : Entity_Id;
7089 Save_Etype : Entity_Id;
7090 Save_Discr_Constr : Elist_Id;
7091 Save_Next_Entity : Entity_Id;
7092 Type_Def : Node_Id;
7094 Discs : Elist_Id := New_Elmt_List;
7095 -- An empty Discs list means that there were no constraints in the
7096 -- subtype indication or that there was an error processing it.
7098 begin
7099 if Ekind (Parent_Type) = E_Record_Type_With_Private
7100 and then Present (Full_View (Parent_Type))
7101 and then Has_Discriminants (Parent_Type)
7102 then
7103 Parent_Base := Base_Type (Full_View (Parent_Type));
7104 else
7105 Parent_Base := Base_Type (Parent_Type);
7106 end if;
7108 -- AI05-0115 : if this is a derivation from a private type in some
7109 -- other scope that may lead to invisible components for the derived
7110 -- type, mark it accordingly.
7112 if Is_Private_Type (Parent_Type) then
7113 if Scope (Parent_Type) = Scope (Derived_Type) then
7114 null;
7116 elsif In_Open_Scopes (Scope (Parent_Type))
7117 and then In_Private_Part (Scope (Parent_Type))
7118 then
7119 null;
7121 else
7122 Set_Has_Private_Ancestor (Derived_Type);
7123 end if;
7125 else
7126 Set_Has_Private_Ancestor
7127 (Derived_Type, Has_Private_Ancestor (Parent_Type));
7128 end if;
7130 -- Before we start the previously documented transformations, here is
7131 -- little fix for size and alignment of tagged types. Normally when we
7132 -- derive type D from type P, we copy the size and alignment of P as the
7133 -- default for D, and in the absence of explicit representation clauses
7134 -- for D, the size and alignment are indeed the same as the parent.
7136 -- But this is wrong for tagged types, since fields may be added, and
7137 -- the default size may need to be larger, and the default alignment may
7138 -- need to be larger.
7140 -- We therefore reset the size and alignment fields in the tagged case.
7141 -- Note that the size and alignment will in any case be at least as
7142 -- large as the parent type (since the derived type has a copy of the
7143 -- parent type in the _parent field)
7145 -- The type is also marked as being tagged here, which is needed when
7146 -- processing components with a self-referential anonymous access type
7147 -- in the call to Check_Anonymous_Access_Components below. Note that
7148 -- this flag is also set later on for completeness.
7150 if Is_Tagged then
7151 Set_Is_Tagged_Type (Derived_Type);
7152 Init_Size_Align (Derived_Type);
7153 end if;
7155 -- STEP 0a: figure out what kind of derived type declaration we have
7157 if Private_Extension then
7158 Type_Def := N;
7159 Set_Ekind (Derived_Type, E_Record_Type_With_Private);
7161 else
7162 Type_Def := Type_Definition (N);
7164 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
7165 -- Parent_Base can be a private type or private extension. However,
7166 -- for tagged types with an extension the newly added fields are
7167 -- visible and hence the Derived_Type is always an E_Record_Type.
7168 -- (except that the parent may have its own private fields).
7169 -- For untagged types we preserve the Ekind of the Parent_Base.
7171 if Present (Record_Extension_Part (Type_Def)) then
7172 Set_Ekind (Derived_Type, E_Record_Type);
7174 -- Create internal access types for components with anonymous
7175 -- access types.
7177 if Ada_Version >= Ada_2005 then
7178 Check_Anonymous_Access_Components
7179 (N, Derived_Type, Derived_Type,
7180 Component_List (Record_Extension_Part (Type_Def)));
7181 end if;
7183 else
7184 Set_Ekind (Derived_Type, Ekind (Parent_Base));
7185 end if;
7186 end if;
7188 -- Indic can either be an N_Identifier if the subtype indication
7189 -- contains no constraint or an N_Subtype_Indication if the subtype
7190 -- indication has a constraint.
7192 Indic := Subtype_Indication (Type_Def);
7193 Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
7195 -- Check that the type has visible discriminants. The type may be
7196 -- a private type with unknown discriminants whose full view has
7197 -- discriminants which are invisible.
7199 if Constraint_Present then
7200 if not Has_Discriminants (Parent_Base)
7201 or else
7202 (Has_Unknown_Discriminants (Parent_Base)
7203 and then Is_Private_Type (Parent_Base))
7204 then
7205 Error_Msg_N
7206 ("invalid constraint: type has no discriminant",
7207 Constraint (Indic));
7209 Constraint_Present := False;
7210 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
7212 elsif Is_Constrained (Parent_Type) then
7213 Error_Msg_N
7214 ("invalid constraint: parent type is already constrained",
7215 Constraint (Indic));
7217 Constraint_Present := False;
7218 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
7219 end if;
7220 end if;
7222 -- STEP 0b: If needed, apply transformation given in point 5. above
7224 if not Private_Extension
7225 and then Has_Discriminants (Parent_Type)
7226 and then not Discriminant_Specs
7227 and then (Is_Constrained (Parent_Type) or else Constraint_Present)
7228 then
7229 -- First, we must analyze the constraint (see comment in point 5.)
7231 if Constraint_Present then
7232 New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
7234 if Has_Discriminants (Derived_Type)
7235 and then Has_Private_Declaration (Derived_Type)
7236 and then Present (Discriminant_Constraint (Derived_Type))
7237 then
7238 -- Verify that constraints of the full view statically match
7239 -- those given in the partial view.
7241 declare
7242 C1, C2 : Elmt_Id;
7244 begin
7245 C1 := First_Elmt (New_Discrs);
7246 C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
7247 while Present (C1) and then Present (C2) loop
7248 if Fully_Conformant_Expressions (Node (C1), Node (C2))
7249 or else
7250 (Is_OK_Static_Expression (Node (C1))
7251 and then
7252 Is_OK_Static_Expression (Node (C2))
7253 and then
7254 Expr_Value (Node (C1)) = Expr_Value (Node (C2)))
7255 then
7256 null;
7258 else
7259 Error_Msg_N (
7260 "constraint not conformant to previous declaration",
7261 Node (C1));
7262 end if;
7264 Next_Elmt (C1);
7265 Next_Elmt (C2);
7266 end loop;
7267 end;
7268 end if;
7269 end if;
7271 -- Insert and analyze the declaration for the unconstrained base type
7273 New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
7275 New_Decl :=
7276 Make_Full_Type_Declaration (Loc,
7277 Defining_Identifier => New_Base,
7278 Type_Definition =>
7279 Make_Derived_Type_Definition (Loc,
7280 Abstract_Present => Abstract_Present (Type_Def),
7281 Limited_Present => Limited_Present (Type_Def),
7282 Subtype_Indication =>
7283 New_Occurrence_Of (Parent_Base, Loc),
7284 Record_Extension_Part =>
7285 Relocate_Node (Record_Extension_Part (Type_Def)),
7286 Interface_List => Interface_List (Type_Def)));
7288 Set_Parent (New_Decl, Parent (N));
7289 Mark_Rewrite_Insertion (New_Decl);
7290 Insert_Before (N, New_Decl);
7292 -- In the extension case, make sure ancestor is frozen appropriately
7293 -- (see also non-discriminated case below).
7295 if Present (Record_Extension_Part (Type_Def))
7296 or else Is_Interface (Parent_Base)
7297 then
7298 Freeze_Before (New_Decl, Parent_Type);
7299 end if;
7301 -- Note that this call passes False for the Derive_Subps parameter
7302 -- because subprogram derivation is deferred until after creating
7303 -- the subtype (see below).
7305 Build_Derived_Type
7306 (New_Decl, Parent_Base, New_Base,
7307 Is_Completion => True, Derive_Subps => False);
7309 -- ??? This needs re-examination to determine whether the
7310 -- above call can simply be replaced by a call to Analyze.
7312 Set_Analyzed (New_Decl);
7314 -- Insert and analyze the declaration for the constrained subtype
7316 if Constraint_Present then
7317 New_Indic :=
7318 Make_Subtype_Indication (Loc,
7319 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
7320 Constraint => Relocate_Node (Constraint (Indic)));
7322 else
7323 declare
7324 Constr_List : constant List_Id := New_List;
7325 C : Elmt_Id;
7326 Expr : Node_Id;
7328 begin
7329 C := First_Elmt (Discriminant_Constraint (Parent_Type));
7330 while Present (C) loop
7331 Expr := Node (C);
7333 -- It is safe here to call New_Copy_Tree since
7334 -- Force_Evaluation was called on each constraint in
7335 -- Build_Discriminant_Constraints.
7337 Append (New_Copy_Tree (Expr), To => Constr_List);
7339 Next_Elmt (C);
7340 end loop;
7342 New_Indic :=
7343 Make_Subtype_Indication (Loc,
7344 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
7345 Constraint =>
7346 Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
7347 end;
7348 end if;
7350 Rewrite (N,
7351 Make_Subtype_Declaration (Loc,
7352 Defining_Identifier => Derived_Type,
7353 Subtype_Indication => New_Indic));
7355 Analyze (N);
7357 -- Derivation of subprograms must be delayed until the full subtype
7358 -- has been established, to ensure proper overriding of subprograms
7359 -- inherited by full types. If the derivations occurred as part of
7360 -- the call to Build_Derived_Type above, then the check for type
7361 -- conformance would fail because earlier primitive subprograms
7362 -- could still refer to the full type prior the change to the new
7363 -- subtype and hence would not match the new base type created here.
7364 -- Subprograms are not derived, however, when Derive_Subps is False
7365 -- (since otherwise there could be redundant derivations).
7367 if Derive_Subps then
7368 Derive_Subprograms (Parent_Type, Derived_Type);
7369 end if;
7371 -- For tagged types the Discriminant_Constraint of the new base itype
7372 -- is inherited from the first subtype so that no subtype conformance
7373 -- problem arise when the first subtype overrides primitive
7374 -- operations inherited by the implicit base type.
7376 if Is_Tagged then
7377 Set_Discriminant_Constraint
7378 (New_Base, Discriminant_Constraint (Derived_Type));
7379 end if;
7381 return;
7382 end if;
7384 -- If we get here Derived_Type will have no discriminants or it will be
7385 -- a discriminated unconstrained base type.
7387 -- STEP 1a: perform preliminary actions/checks for derived tagged types
7389 if Is_Tagged then
7391 -- The parent type is frozen for non-private extensions (RM 13.14(7))
7392 -- The declaration of a specific descendant of an interface type
7393 -- freezes the interface type (RM 13.14).
7395 if not Private_Extension or else Is_Interface (Parent_Base) then
7396 Freeze_Before (N, Parent_Type);
7397 end if;
7399 -- In Ada 2005 (AI-344), the restriction that a derived tagged type
7400 -- cannot be declared at a deeper level than its parent type is
7401 -- removed. The check on derivation within a generic body is also
7402 -- relaxed, but there's a restriction that a derived tagged type
7403 -- cannot be declared in a generic body if it's derived directly
7404 -- or indirectly from a formal type of that generic.
7406 if Ada_Version >= Ada_2005 then
7407 if Present (Enclosing_Generic_Body (Derived_Type)) then
7408 declare
7409 Ancestor_Type : Entity_Id;
7411 begin
7412 -- Check to see if any ancestor of the derived type is a
7413 -- formal type.
7415 Ancestor_Type := Parent_Type;
7416 while not Is_Generic_Type (Ancestor_Type)
7417 and then Etype (Ancestor_Type) /= Ancestor_Type
7418 loop
7419 Ancestor_Type := Etype (Ancestor_Type);
7420 end loop;
7422 -- If the derived type does have a formal type as an
7423 -- ancestor, then it's an error if the derived type is
7424 -- declared within the body of the generic unit that
7425 -- declares the formal type in its generic formal part. It's
7426 -- sufficient to check whether the ancestor type is declared
7427 -- inside the same generic body as the derived type (such as
7428 -- within a nested generic spec), in which case the
7429 -- derivation is legal. If the formal type is declared
7430 -- outside of that generic body, then it's guaranteed that
7431 -- the derived type is declared within the generic body of
7432 -- the generic unit declaring the formal type.
7434 if Is_Generic_Type (Ancestor_Type)
7435 and then Enclosing_Generic_Body (Ancestor_Type) /=
7436 Enclosing_Generic_Body (Derived_Type)
7437 then
7438 Error_Msg_NE
7439 ("parent type of& must not be descendant of formal type"
7440 & " of an enclosing generic body",
7441 Indic, Derived_Type);
7442 end if;
7443 end;
7444 end if;
7446 elsif Type_Access_Level (Derived_Type) /=
7447 Type_Access_Level (Parent_Type)
7448 and then not Is_Generic_Type (Derived_Type)
7449 then
7450 if Is_Controlled (Parent_Type) then
7451 Error_Msg_N
7452 ("controlled type must be declared at the library level",
7453 Indic);
7454 else
7455 Error_Msg_N
7456 ("type extension at deeper accessibility level than parent",
7457 Indic);
7458 end if;
7460 else
7461 declare
7462 GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
7464 begin
7465 if Present (GB)
7466 and then GB /= Enclosing_Generic_Body (Parent_Base)
7467 then
7468 Error_Msg_NE
7469 ("parent type of& must not be outside generic body"
7470 & " (RM 3.9.1(4))",
7471 Indic, Derived_Type);
7472 end if;
7473 end;
7474 end if;
7475 end if;
7477 -- Ada 2005 (AI-251)
7479 if Ada_Version >= Ada_2005 and then Is_Tagged then
7481 -- "The declaration of a specific descendant of an interface type
7482 -- freezes the interface type" (RM 13.14).
7484 declare
7485 Iface : Node_Id;
7486 begin
7487 if Is_Non_Empty_List (Interface_List (Type_Def)) then
7488 Iface := First (Interface_List (Type_Def));
7489 while Present (Iface) loop
7490 Freeze_Before (N, Etype (Iface));
7491 Next (Iface);
7492 end loop;
7493 end if;
7494 end;
7495 end if;
7497 -- STEP 1b : preliminary cleanup of the full view of private types
7499 -- If the type is already marked as having discriminants, then it's the
7500 -- completion of a private type or private extension and we need to
7501 -- retain the discriminants from the partial view if the current
7502 -- declaration has Discriminant_Specifications so that we can verify
7503 -- conformance. However, we must remove any existing components that
7504 -- were inherited from the parent (and attached in Copy_And_Swap)
7505 -- because the full type inherits all appropriate components anyway, and
7506 -- we do not want the partial view's components interfering.
7508 if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
7509 Discrim := First_Discriminant (Derived_Type);
7510 loop
7511 Last_Discrim := Discrim;
7512 Next_Discriminant (Discrim);
7513 exit when No (Discrim);
7514 end loop;
7516 Set_Last_Entity (Derived_Type, Last_Discrim);
7518 -- In all other cases wipe out the list of inherited components (even
7519 -- inherited discriminants), it will be properly rebuilt here.
7521 else
7522 Set_First_Entity (Derived_Type, Empty);
7523 Set_Last_Entity (Derived_Type, Empty);
7524 end if;
7526 -- STEP 1c: Initialize some flags for the Derived_Type
7528 -- The following flags must be initialized here so that
7529 -- Process_Discriminants can check that discriminants of tagged types do
7530 -- not have a default initial value and that access discriminants are
7531 -- only specified for limited records. For completeness, these flags are
7532 -- also initialized along with all the other flags below.
7534 -- AI-419: Limitedness is not inherited from an interface parent, so to
7535 -- be limited in that case the type must be explicitly declared as
7536 -- limited. However, task and protected interfaces are always limited.
7538 if Limited_Present (Type_Def) then
7539 Set_Is_Limited_Record (Derived_Type);
7541 elsif Is_Limited_Record (Parent_Type)
7542 or else (Present (Full_View (Parent_Type))
7543 and then Is_Limited_Record (Full_View (Parent_Type)))
7544 then
7545 if not Is_Interface (Parent_Type)
7546 or else Is_Synchronized_Interface (Parent_Type)
7547 or else Is_Protected_Interface (Parent_Type)
7548 or else Is_Task_Interface (Parent_Type)
7549 then
7550 Set_Is_Limited_Record (Derived_Type);
7551 end if;
7552 end if;
7554 -- STEP 2a: process discriminants of derived type if any
7556 Push_Scope (Derived_Type);
7558 if Discriminant_Specs then
7559 Set_Has_Unknown_Discriminants (Derived_Type, False);
7561 -- The following call initializes fields Has_Discriminants and
7562 -- Discriminant_Constraint, unless we are processing the completion
7563 -- of a private type declaration.
7565 Check_Or_Process_Discriminants (N, Derived_Type);
7567 -- For untagged types, the constraint on the Parent_Type must be
7568 -- present and is used to rename the discriminants.
7570 if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
7571 Error_Msg_N ("untagged parent must have discriminants", Indic);
7573 elsif not Is_Tagged and then not Constraint_Present then
7574 Error_Msg_N
7575 ("discriminant constraint needed for derived untagged records",
7576 Indic);
7578 -- Otherwise the parent subtype must be constrained unless we have a
7579 -- private extension.
7581 elsif not Constraint_Present
7582 and then not Private_Extension
7583 and then not Is_Constrained (Parent_Type)
7584 then
7585 Error_Msg_N
7586 ("unconstrained type not allowed in this context", Indic);
7588 elsif Constraint_Present then
7589 -- The following call sets the field Corresponding_Discriminant
7590 -- for the discriminants in the Derived_Type.
7592 Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
7594 -- For untagged types all new discriminants must rename
7595 -- discriminants in the parent. For private extensions new
7596 -- discriminants cannot rename old ones (implied by [7.3(13)]).
7598 Discrim := First_Discriminant (Derived_Type);
7599 while Present (Discrim) loop
7600 if not Is_Tagged
7601 and then No (Corresponding_Discriminant (Discrim))
7602 then
7603 Error_Msg_N
7604 ("new discriminants must constrain old ones", Discrim);
7606 elsif Private_Extension
7607 and then Present (Corresponding_Discriminant (Discrim))
7608 then
7609 Error_Msg_N
7610 ("only static constraints allowed for parent"
7611 & " discriminants in the partial view", Indic);
7612 exit;
7613 end if;
7615 -- If a new discriminant is used in the constraint, then its
7616 -- subtype must be statically compatible with the parent
7617 -- discriminant's subtype (3.7(15)).
7619 -- However, if the record contains an array constrained by
7620 -- the discriminant but with some different bound, the compiler
7621 -- attemps to create a smaller range for the discriminant type.
7622 -- (See exp_ch3.Adjust_Discriminants). In this case, where
7623 -- the discriminant type is a scalar type, the check must use
7624 -- the original discriminant type in the parent declaration.
7626 declare
7627 Corr_Disc : constant Entity_Id :=
7628 Corresponding_Discriminant (Discrim);
7629 Disc_Type : constant Entity_Id := Etype (Discrim);
7630 Corr_Type : Entity_Id;
7632 begin
7633 if Present (Corr_Disc) then
7634 if Is_Scalar_Type (Disc_Type) then
7635 Corr_Type :=
7636 Entity (Discriminant_Type (Parent (Corr_Disc)));
7637 else
7638 Corr_Type := Etype (Corr_Disc);
7639 end if;
7641 if not
7642 Subtypes_Statically_Compatible (Disc_Type, Corr_Type)
7643 then
7644 Error_Msg_N
7645 ("subtype must be compatible "
7646 & "with parent discriminant",
7647 Discrim);
7648 end if;
7649 end if;
7650 end;
7652 Next_Discriminant (Discrim);
7653 end loop;
7655 -- Check whether the constraints of the full view statically
7656 -- match those imposed by the parent subtype [7.3(13)].
7658 if Present (Stored_Constraint (Derived_Type)) then
7659 declare
7660 C1, C2 : Elmt_Id;
7662 begin
7663 C1 := First_Elmt (Discs);
7664 C2 := First_Elmt (Stored_Constraint (Derived_Type));
7665 while Present (C1) and then Present (C2) loop
7666 if not
7667 Fully_Conformant_Expressions (Node (C1), Node (C2))
7668 then
7669 Error_Msg_N
7670 ("not conformant with previous declaration",
7671 Node (C1));
7672 end if;
7674 Next_Elmt (C1);
7675 Next_Elmt (C2);
7676 end loop;
7677 end;
7678 end if;
7679 end if;
7681 -- STEP 2b: No new discriminants, inherit discriminants if any
7683 else
7684 if Private_Extension then
7685 Set_Has_Unknown_Discriminants
7686 (Derived_Type,
7687 Has_Unknown_Discriminants (Parent_Type)
7688 or else Unknown_Discriminants_Present (N));
7690 -- The partial view of the parent may have unknown discriminants,
7691 -- but if the full view has discriminants and the parent type is
7692 -- in scope they must be inherited.
7694 elsif Has_Unknown_Discriminants (Parent_Type)
7695 and then
7696 (not Has_Discriminants (Parent_Type)
7697 or else not In_Open_Scopes (Scope (Parent_Type)))
7698 then
7699 Set_Has_Unknown_Discriminants (Derived_Type);
7700 end if;
7702 if not Has_Unknown_Discriminants (Derived_Type)
7703 and then not Has_Unknown_Discriminants (Parent_Base)
7704 and then Has_Discriminants (Parent_Type)
7705 then
7706 Inherit_Discrims := True;
7707 Set_Has_Discriminants
7708 (Derived_Type, True);
7709 Set_Discriminant_Constraint
7710 (Derived_Type, Discriminant_Constraint (Parent_Base));
7711 end if;
7713 -- The following test is true for private types (remember
7714 -- transformation 5. is not applied to those) and in an error
7715 -- situation.
7717 if Constraint_Present then
7718 Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
7719 end if;
7721 -- For now mark a new derived type as constrained only if it has no
7722 -- discriminants. At the end of Build_Derived_Record_Type we properly
7723 -- set this flag in the case of private extensions. See comments in
7724 -- point 9. just before body of Build_Derived_Record_Type.
7726 Set_Is_Constrained
7727 (Derived_Type,
7728 not (Inherit_Discrims
7729 or else Has_Unknown_Discriminants (Derived_Type)));
7730 end if;
7732 -- STEP 3: initialize fields of derived type
7734 Set_Is_Tagged_Type (Derived_Type, Is_Tagged);
7735 Set_Stored_Constraint (Derived_Type, No_Elist);
7737 -- Ada 2005 (AI-251): Private type-declarations can implement interfaces
7738 -- but cannot be interfaces
7740 if not Private_Extension
7741 and then Ekind (Derived_Type) /= E_Private_Type
7742 and then Ekind (Derived_Type) /= E_Limited_Private_Type
7743 then
7744 if Interface_Present (Type_Def) then
7745 Analyze_Interface_Declaration (Derived_Type, Type_Def);
7746 end if;
7748 Set_Interfaces (Derived_Type, No_Elist);
7749 end if;
7751 -- Fields inherited from the Parent_Type
7753 Set_Has_Specified_Layout
7754 (Derived_Type, Has_Specified_Layout (Parent_Type));
7755 Set_Is_Limited_Composite
7756 (Derived_Type, Is_Limited_Composite (Parent_Type));
7757 Set_Is_Private_Composite
7758 (Derived_Type, Is_Private_Composite (Parent_Type));
7760 -- Fields inherited from the Parent_Base
7762 Set_Has_Controlled_Component
7763 (Derived_Type, Has_Controlled_Component (Parent_Base));
7764 Set_Has_Non_Standard_Rep
7765 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
7766 Set_Has_Primitive_Operations
7767 (Derived_Type, Has_Primitive_Operations (Parent_Base));
7769 -- Fields inherited from the Parent_Base in the non-private case
7771 if Ekind (Derived_Type) = E_Record_Type then
7772 Set_Has_Complex_Representation
7773 (Derived_Type, Has_Complex_Representation (Parent_Base));
7774 end if;
7776 -- Fields inherited from the Parent_Base for record types
7778 if Is_Record_Type (Derived_Type) then
7780 declare
7781 Parent_Full : Entity_Id;
7783 begin
7784 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
7785 -- Parent_Base can be a private type or private extension. Go
7786 -- to the full view here to get the E_Record_Type specific flags.
7788 if Present (Full_View (Parent_Base)) then
7789 Parent_Full := Full_View (Parent_Base);
7790 else
7791 Parent_Full := Parent_Base;
7792 end if;
7794 Set_OK_To_Reorder_Components
7795 (Derived_Type, OK_To_Reorder_Components (Parent_Full));
7796 end;
7797 end if;
7799 -- Set fields for private derived types
7801 if Is_Private_Type (Derived_Type) then
7802 Set_Depends_On_Private (Derived_Type, True);
7803 Set_Private_Dependents (Derived_Type, New_Elmt_List);
7805 -- Inherit fields from non private record types. If this is the
7806 -- completion of a derivation from a private type, the parent itself
7807 -- is private, and the attributes come from its full view, which must
7808 -- be present.
7810 else
7811 if Is_Private_Type (Parent_Base)
7812 and then not Is_Record_Type (Parent_Base)
7813 then
7814 Set_Component_Alignment
7815 (Derived_Type, Component_Alignment (Full_View (Parent_Base)));
7816 Set_C_Pass_By_Copy
7817 (Derived_Type, C_Pass_By_Copy (Full_View (Parent_Base)));
7818 else
7819 Set_Component_Alignment
7820 (Derived_Type, Component_Alignment (Parent_Base));
7821 Set_C_Pass_By_Copy
7822 (Derived_Type, C_Pass_By_Copy (Parent_Base));
7823 end if;
7824 end if;
7826 -- Set fields for tagged types
7828 if Is_Tagged then
7829 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
7831 -- All tagged types defined in Ada.Finalization are controlled
7833 if Chars (Scope (Derived_Type)) = Name_Finalization
7834 and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
7835 and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
7836 then
7837 Set_Is_Controlled (Derived_Type);
7838 else
7839 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Base));
7840 end if;
7842 -- Minor optimization: there is no need to generate the class-wide
7843 -- entity associated with an underlying record view.
7845 if not Is_Underlying_Record_View (Derived_Type) then
7846 Make_Class_Wide_Type (Derived_Type);
7847 end if;
7849 Set_Is_Abstract_Type (Derived_Type, Abstract_Present (Type_Def));
7851 if Has_Discriminants (Derived_Type)
7852 and then Constraint_Present
7853 then
7854 Set_Stored_Constraint
7855 (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
7856 end if;
7858 if Ada_Version >= Ada_2005 then
7859 declare
7860 Ifaces_List : Elist_Id;
7862 begin
7863 -- Checks rules 3.9.4 (13/2 and 14/2)
7865 if Comes_From_Source (Derived_Type)
7866 and then not Is_Private_Type (Derived_Type)
7867 and then Is_Interface (Parent_Type)
7868 and then not Is_Interface (Derived_Type)
7869 then
7870 if Is_Task_Interface (Parent_Type) then
7871 Error_Msg_N
7872 ("(Ada 2005) task type required (RM 3.9.4 (13.2))",
7873 Derived_Type);
7875 elsif Is_Protected_Interface (Parent_Type) then
7876 Error_Msg_N
7877 ("(Ada 2005) protected type required (RM 3.9.4 (14.2))",
7878 Derived_Type);
7879 end if;
7880 end if;
7882 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
7884 Check_Interfaces (N, Type_Def);
7886 -- Ada 2005 (AI-251): Collect the list of progenitors that are
7887 -- not already in the parents.
7889 Collect_Interfaces
7890 (T => Derived_Type,
7891 Ifaces_List => Ifaces_List,
7892 Exclude_Parents => True);
7894 Set_Interfaces (Derived_Type, Ifaces_List);
7896 -- If the derived type is the anonymous type created for
7897 -- a declaration whose parent has a constraint, propagate
7898 -- the interface list to the source type. This must be done
7899 -- prior to the completion of the analysis of the source type
7900 -- because the components in the extension may contain current
7901 -- instances whose legality depends on some ancestor.
7903 if Is_Itype (Derived_Type) then
7904 declare
7905 Def : constant Node_Id :=
7906 Associated_Node_For_Itype (Derived_Type);
7907 begin
7908 if Present (Def)
7909 and then Nkind (Def) = N_Full_Type_Declaration
7910 then
7911 Set_Interfaces
7912 (Defining_Identifier (Def), Ifaces_List);
7913 end if;
7914 end;
7915 end if;
7916 end;
7917 end if;
7919 else
7920 Set_Is_Packed (Derived_Type, Is_Packed (Parent_Base));
7921 Set_Has_Non_Standard_Rep
7922 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
7923 end if;
7925 -- STEP 4: Inherit components from the parent base and constrain them.
7926 -- Apply the second transformation described in point 6. above.
7928 if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
7929 or else not Has_Discriminants (Parent_Type)
7930 or else not Is_Constrained (Parent_Type)
7931 then
7932 Constrs := Discs;
7933 else
7934 Constrs := Discriminant_Constraint (Parent_Type);
7935 end if;
7937 Assoc_List :=
7938 Inherit_Components
7939 (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
7941 -- STEP 5a: Copy the parent record declaration for untagged types
7943 if not Is_Tagged then
7945 -- Discriminant_Constraint (Derived_Type) has been properly
7946 -- constructed. Save it and temporarily set it to Empty because we
7947 -- do not want the call to New_Copy_Tree below to mess this list.
7949 if Has_Discriminants (Derived_Type) then
7950 Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
7951 Set_Discriminant_Constraint (Derived_Type, No_Elist);
7952 else
7953 Save_Discr_Constr := No_Elist;
7954 end if;
7956 -- Save the Etype field of Derived_Type. It is correctly set now,
7957 -- but the call to New_Copy tree may remap it to point to itself,
7958 -- which is not what we want. Ditto for the Next_Entity field.
7960 Save_Etype := Etype (Derived_Type);
7961 Save_Next_Entity := Next_Entity (Derived_Type);
7963 -- Assoc_List maps all stored discriminants in the Parent_Base to
7964 -- stored discriminants in the Derived_Type. It is fundamental that
7965 -- no types or itypes with discriminants other than the stored
7966 -- discriminants appear in the entities declared inside
7967 -- Derived_Type, since the back end cannot deal with it.
7969 New_Decl :=
7970 New_Copy_Tree
7971 (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
7973 -- Restore the fields saved prior to the New_Copy_Tree call
7974 -- and compute the stored constraint.
7976 Set_Etype (Derived_Type, Save_Etype);
7977 Set_Next_Entity (Derived_Type, Save_Next_Entity);
7979 if Has_Discriminants (Derived_Type) then
7980 Set_Discriminant_Constraint
7981 (Derived_Type, Save_Discr_Constr);
7982 Set_Stored_Constraint
7983 (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
7984 Replace_Components (Derived_Type, New_Decl);
7985 Set_Has_Implicit_Dereference
7986 (Derived_Type, Has_Implicit_Dereference (Parent_Type));
7987 end if;
7989 -- Insert the new derived type declaration
7991 Rewrite (N, New_Decl);
7993 -- STEP 5b: Complete the processing for record extensions in generics
7995 -- There is no completion for record extensions declared in the
7996 -- parameter part of a generic, so we need to complete processing for
7997 -- these generic record extensions here. The Record_Type_Definition call
7998 -- will change the Ekind of the components from E_Void to E_Component.
8000 elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
8001 Record_Type_Definition (Empty, Derived_Type);
8003 -- STEP 5c: Process the record extension for non private tagged types
8005 elsif not Private_Extension then
8007 -- Add the _parent field in the derived type
8009 Expand_Record_Extension (Derived_Type, Type_Def);
8011 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
8012 -- implemented interfaces if we are in expansion mode
8014 if Expander_Active
8015 and then Has_Interfaces (Derived_Type)
8016 then
8017 Add_Interface_Tag_Components (N, Derived_Type);
8018 end if;
8020 -- Analyze the record extension
8022 Record_Type_Definition
8023 (Record_Extension_Part (Type_Def), Derived_Type);
8024 end if;
8026 End_Scope;
8028 -- Nothing else to do if there is an error in the derivation.
8029 -- An unusual case: the full view may be derived from a type in an
8030 -- instance, when the partial view was used illegally as an actual
8031 -- in that instance, leading to a circular definition.
8033 if Etype (Derived_Type) = Any_Type
8034 or else Etype (Parent_Type) = Derived_Type
8035 then
8036 return;
8037 end if;
8039 -- Set delayed freeze and then derive subprograms, we need to do
8040 -- this in this order so that derived subprograms inherit the
8041 -- derived freeze if necessary.
8043 Set_Has_Delayed_Freeze (Derived_Type);
8045 if Derive_Subps then
8046 Derive_Subprograms (Parent_Type, Derived_Type);
8047 end if;
8049 -- If we have a private extension which defines a constrained derived
8050 -- type mark as constrained here after we have derived subprograms. See
8051 -- comment on point 9. just above the body of Build_Derived_Record_Type.
8053 if Private_Extension and then Inherit_Discrims then
8054 if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
8055 Set_Is_Constrained (Derived_Type, True);
8056 Set_Discriminant_Constraint (Derived_Type, Discs);
8058 elsif Is_Constrained (Parent_Type) then
8059 Set_Is_Constrained
8060 (Derived_Type, True);
8061 Set_Discriminant_Constraint
8062 (Derived_Type, Discriminant_Constraint (Parent_Type));
8063 end if;
8064 end if;
8066 -- Update the class-wide type, which shares the now-completed entity
8067 -- list with its specific type. In case of underlying record views,
8068 -- we do not generate the corresponding class wide entity.
8070 if Is_Tagged
8071 and then not Is_Underlying_Record_View (Derived_Type)
8072 then
8073 Set_First_Entity
8074 (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
8075 Set_Last_Entity
8076 (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
8077 end if;
8079 Check_Function_Writable_Actuals (N);
8080 end Build_Derived_Record_Type;
8082 ------------------------
8083 -- Build_Derived_Type --
8084 ------------------------
8086 procedure Build_Derived_Type
8087 (N : Node_Id;
8088 Parent_Type : Entity_Id;
8089 Derived_Type : Entity_Id;
8090 Is_Completion : Boolean;
8091 Derive_Subps : Boolean := True)
8093 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
8095 begin
8096 -- Set common attributes
8098 Set_Scope (Derived_Type, Current_Scope);
8100 Set_Ekind (Derived_Type, Ekind (Parent_Base));
8101 Set_Etype (Derived_Type, Parent_Base);
8102 Set_Has_Task (Derived_Type, Has_Task (Parent_Base));
8104 Set_Size_Info (Derived_Type, Parent_Type);
8105 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
8106 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
8107 Set_Is_Tagged_Type (Derived_Type, Is_Tagged_Type (Parent_Type));
8109 -- If the parent type is a private subtype, the convention on the base
8110 -- type may be set in the private part, and not propagated to the
8111 -- subtype until later, so we obtain the convention from the base type.
8113 Set_Convention (Derived_Type, Convention (Parent_Base));
8115 -- Propagate invariant information. The new type has invariants if
8116 -- they are inherited from the parent type, and these invariants can
8117 -- be further inherited, so both flags are set.
8119 -- We similarly inherit predicates
8121 if Has_Predicates (Parent_Type) then
8122 Set_Has_Predicates (Derived_Type);
8123 end if;
8125 -- The derived type inherits the representation clauses of the parent.
8126 -- However, for a private type that is completed by a derivation, there
8127 -- may be operation attributes that have been specified already (stream
8128 -- attributes and External_Tag) and those must be provided. Finally,
8129 -- if the partial view is a private extension, the representation items
8130 -- of the parent have been inherited already, and should not be chained
8131 -- twice to the derived type.
8133 if Is_Tagged_Type (Parent_Type)
8134 and then Present (First_Rep_Item (Derived_Type))
8135 then
8136 -- The existing items are either operational items or items inherited
8137 -- from a private extension declaration.
8139 declare
8140 Rep : Node_Id;
8141 -- Used to iterate over representation items of the derived type
8143 Last_Rep : Node_Id;
8144 -- Last representation item of the (non-empty) representation
8145 -- item list of the derived type.
8147 Found : Boolean := False;
8149 begin
8150 Rep := First_Rep_Item (Derived_Type);
8151 Last_Rep := Rep;
8152 while Present (Rep) loop
8153 if Rep = First_Rep_Item (Parent_Type) then
8154 Found := True;
8155 exit;
8157 else
8158 Rep := Next_Rep_Item (Rep);
8160 if Present (Rep) then
8161 Last_Rep := Rep;
8162 end if;
8163 end if;
8164 end loop;
8166 -- Here if we either encountered the parent type's first rep
8167 -- item on the derived type's rep item list (in which case
8168 -- Found is True, and we have nothing else to do), or if we
8169 -- reached the last rep item of the derived type, which is
8170 -- Last_Rep, in which case we further chain the parent type's
8171 -- rep items to those of the derived type.
8173 if not Found then
8174 Set_Next_Rep_Item (Last_Rep, First_Rep_Item (Parent_Type));
8175 end if;
8176 end;
8178 else
8179 Set_First_Rep_Item (Derived_Type, First_Rep_Item (Parent_Type));
8180 end if;
8182 case Ekind (Parent_Type) is
8183 when Numeric_Kind =>
8184 Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
8186 when Array_Kind =>
8187 Build_Derived_Array_Type (N, Parent_Type, Derived_Type);
8189 when E_Record_Type
8190 | E_Record_Subtype
8191 | Class_Wide_Kind =>
8192 Build_Derived_Record_Type
8193 (N, Parent_Type, Derived_Type, Derive_Subps);
8194 return;
8196 when Enumeration_Kind =>
8197 Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
8199 when Access_Kind =>
8200 Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
8202 when Incomplete_Or_Private_Kind =>
8203 Build_Derived_Private_Type
8204 (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
8206 -- For discriminated types, the derivation includes deriving
8207 -- primitive operations. For others it is done below.
8209 if Is_Tagged_Type (Parent_Type)
8210 or else Has_Discriminants (Parent_Type)
8211 or else (Present (Full_View (Parent_Type))
8212 and then Has_Discriminants (Full_View (Parent_Type)))
8213 then
8214 return;
8215 end if;
8217 when Concurrent_Kind =>
8218 Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
8220 when others =>
8221 raise Program_Error;
8222 end case;
8224 if Etype (Derived_Type) = Any_Type then
8225 return;
8226 end if;
8228 -- Set delayed freeze and then derive subprograms, we need to do this
8229 -- in this order so that derived subprograms inherit the derived freeze
8230 -- if necessary.
8232 Set_Has_Delayed_Freeze (Derived_Type);
8233 if Derive_Subps then
8234 Derive_Subprograms (Parent_Type, Derived_Type);
8235 end if;
8237 Set_Has_Primitive_Operations
8238 (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
8239 end Build_Derived_Type;
8241 -----------------------
8242 -- Build_Discriminal --
8243 -----------------------
8245 procedure Build_Discriminal (Discrim : Entity_Id) is
8246 D_Minal : Entity_Id;
8247 CR_Disc : Entity_Id;
8249 begin
8250 -- A discriminal has the same name as the discriminant
8252 D_Minal := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
8254 Set_Ekind (D_Minal, E_In_Parameter);
8255 Set_Mechanism (D_Minal, Default_Mechanism);
8256 Set_Etype (D_Minal, Etype (Discrim));
8257 Set_Scope (D_Minal, Current_Scope);
8259 Set_Discriminal (Discrim, D_Minal);
8260 Set_Discriminal_Link (D_Minal, Discrim);
8262 -- For task types, build at once the discriminants of the corresponding
8263 -- record, which are needed if discriminants are used in entry defaults
8264 -- and in family bounds.
8266 if Is_Concurrent_Type (Current_Scope)
8267 or else Is_Limited_Type (Current_Scope)
8268 then
8269 CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
8271 Set_Ekind (CR_Disc, E_In_Parameter);
8272 Set_Mechanism (CR_Disc, Default_Mechanism);
8273 Set_Etype (CR_Disc, Etype (Discrim));
8274 Set_Scope (CR_Disc, Current_Scope);
8275 Set_Discriminal_Link (CR_Disc, Discrim);
8276 Set_CR_Discriminant (Discrim, CR_Disc);
8277 end if;
8278 end Build_Discriminal;
8280 ------------------------------------
8281 -- Build_Discriminant_Constraints --
8282 ------------------------------------
8284 function Build_Discriminant_Constraints
8285 (T : Entity_Id;
8286 Def : Node_Id;
8287 Derived_Def : Boolean := False) return Elist_Id
8289 C : constant Node_Id := Constraint (Def);
8290 Nb_Discr : constant Nat := Number_Discriminants (T);
8292 Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
8293 -- Saves the expression corresponding to a given discriminant in T
8295 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
8296 -- Return the Position number within array Discr_Expr of a discriminant
8297 -- D within the discriminant list of the discriminated type T.
8299 procedure Process_Discriminant_Expression
8300 (Expr : Node_Id;
8301 D : Entity_Id);
8302 -- If this is a discriminant constraint on a partial view, do not
8303 -- generate an overflow check on the discriminant expression. The check
8304 -- will be generated when constraining the full view. Otherwise the
8305 -- backend creates duplicate symbols for the temporaries corresponding
8306 -- to the expressions to be checked, causing spurious assembler errors.
8308 ------------------
8309 -- Pos_Of_Discr --
8310 ------------------
8312 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
8313 Disc : Entity_Id;
8315 begin
8316 Disc := First_Discriminant (T);
8317 for J in Discr_Expr'Range loop
8318 if Disc = D then
8319 return J;
8320 end if;
8322 Next_Discriminant (Disc);
8323 end loop;
8325 -- Note: Since this function is called on discriminants that are
8326 -- known to belong to the discriminated type, falling through the
8327 -- loop with no match signals an internal compiler error.
8329 raise Program_Error;
8330 end Pos_Of_Discr;
8332 -------------------------------------
8333 -- Process_Discriminant_Expression --
8334 -------------------------------------
8336 procedure Process_Discriminant_Expression
8337 (Expr : Node_Id;
8338 D : Entity_Id)
8340 BDT : constant Entity_Id := Base_Type (Etype (D));
8342 begin
8343 -- If this is a discriminant constraint on a partial view, do
8344 -- not generate an overflow on the discriminant expression. The
8345 -- check will be generated when constraining the full view.
8347 if Is_Private_Type (T)
8348 and then Present (Full_View (T))
8349 then
8350 Analyze_And_Resolve (Expr, BDT, Suppress => Overflow_Check);
8351 else
8352 Analyze_And_Resolve (Expr, BDT);
8353 end if;
8354 end Process_Discriminant_Expression;
8356 -- Declarations local to Build_Discriminant_Constraints
8358 Discr : Entity_Id;
8359 E : Entity_Id;
8360 Elist : constant Elist_Id := New_Elmt_List;
8362 Constr : Node_Id;
8363 Expr : Node_Id;
8364 Id : Node_Id;
8365 Position : Nat;
8366 Found : Boolean;
8368 Discrim_Present : Boolean := False;
8370 -- Start of processing for Build_Discriminant_Constraints
8372 begin
8373 -- The following loop will process positional associations only.
8374 -- For a positional association, the (single) discriminant is
8375 -- implicitly specified by position, in textual order (RM 3.7.2).
8377 Discr := First_Discriminant (T);
8378 Constr := First (Constraints (C));
8379 for D in Discr_Expr'Range loop
8380 exit when Nkind (Constr) = N_Discriminant_Association;
8382 if No (Constr) then
8383 Error_Msg_N ("too few discriminants given in constraint", C);
8384 return New_Elmt_List;
8386 elsif Nkind (Constr) = N_Range
8387 or else (Nkind (Constr) = N_Attribute_Reference
8388 and then
8389 Attribute_Name (Constr) = Name_Range)
8390 then
8391 Error_Msg_N
8392 ("a range is not a valid discriminant constraint", Constr);
8393 Discr_Expr (D) := Error;
8395 else
8396 Process_Discriminant_Expression (Constr, Discr);
8397 Discr_Expr (D) := Constr;
8398 end if;
8400 Next_Discriminant (Discr);
8401 Next (Constr);
8402 end loop;
8404 if No (Discr) and then Present (Constr) then
8405 Error_Msg_N ("too many discriminants given in constraint", Constr);
8406 return New_Elmt_List;
8407 end if;
8409 -- Named associations can be given in any order, but if both positional
8410 -- and named associations are used in the same discriminant constraint,
8411 -- then positional associations must occur first, at their normal
8412 -- position. Hence once a named association is used, the rest of the
8413 -- discriminant constraint must use only named associations.
8415 while Present (Constr) loop
8417 -- Positional association forbidden after a named association
8419 if Nkind (Constr) /= N_Discriminant_Association then
8420 Error_Msg_N ("positional association follows named one", Constr);
8421 return New_Elmt_List;
8423 -- Otherwise it is a named association
8425 else
8426 -- E records the type of the discriminants in the named
8427 -- association. All the discriminants specified in the same name
8428 -- association must have the same type.
8430 E := Empty;
8432 -- Search the list of discriminants in T to see if the simple name
8433 -- given in the constraint matches any of them.
8435 Id := First (Selector_Names (Constr));
8436 while Present (Id) loop
8437 Found := False;
8439 -- If Original_Discriminant is present, we are processing a
8440 -- generic instantiation and this is an instance node. We need
8441 -- to find the name of the corresponding discriminant in the
8442 -- actual record type T and not the name of the discriminant in
8443 -- the generic formal. Example:
8445 -- generic
8446 -- type G (D : int) is private;
8447 -- package P is
8448 -- subtype W is G (D => 1);
8449 -- end package;
8450 -- type Rec (X : int) is record ... end record;
8451 -- package Q is new P (G => Rec);
8453 -- At the point of the instantiation, formal type G is Rec
8454 -- and therefore when reanalyzing "subtype W is G (D => 1);"
8455 -- which really looks like "subtype W is Rec (D => 1);" at
8456 -- the point of instantiation, we want to find the discriminant
8457 -- that corresponds to D in Rec, i.e. X.
8459 if Present (Original_Discriminant (Id))
8460 and then In_Instance
8461 then
8462 Discr := Find_Corresponding_Discriminant (Id, T);
8463 Found := True;
8465 else
8466 Discr := First_Discriminant (T);
8467 while Present (Discr) loop
8468 if Chars (Discr) = Chars (Id) then
8469 Found := True;
8470 exit;
8471 end if;
8473 Next_Discriminant (Discr);
8474 end loop;
8476 if not Found then
8477 Error_Msg_N ("& does not match any discriminant", Id);
8478 return New_Elmt_List;
8480 -- If the parent type is a generic formal, preserve the
8481 -- name of the discriminant for subsequent instances.
8482 -- see comment at the beginning of this if statement.
8484 elsif Is_Generic_Type (Root_Type (T)) then
8485 Set_Original_Discriminant (Id, Discr);
8486 end if;
8487 end if;
8489 Position := Pos_Of_Discr (T, Discr);
8491 if Present (Discr_Expr (Position)) then
8492 Error_Msg_N ("duplicate constraint for discriminant&", Id);
8494 else
8495 -- Each discriminant specified in the same named association
8496 -- must be associated with a separate copy of the
8497 -- corresponding expression.
8499 if Present (Next (Id)) then
8500 Expr := New_Copy_Tree (Expression (Constr));
8501 Set_Parent (Expr, Parent (Expression (Constr)));
8502 else
8503 Expr := Expression (Constr);
8504 end if;
8506 Discr_Expr (Position) := Expr;
8507 Process_Discriminant_Expression (Expr, Discr);
8508 end if;
8510 -- A discriminant association with more than one discriminant
8511 -- name is only allowed if the named discriminants are all of
8512 -- the same type (RM 3.7.1(8)).
8514 if E = Empty then
8515 E := Base_Type (Etype (Discr));
8517 elsif Base_Type (Etype (Discr)) /= E then
8518 Error_Msg_N
8519 ("all discriminants in an association " &
8520 "must have the same type", Id);
8521 end if;
8523 Next (Id);
8524 end loop;
8525 end if;
8527 Next (Constr);
8528 end loop;
8530 -- A discriminant constraint must provide exactly one value for each
8531 -- discriminant of the type (RM 3.7.1(8)).
8533 for J in Discr_Expr'Range loop
8534 if No (Discr_Expr (J)) then
8535 Error_Msg_N ("too few discriminants given in constraint", C);
8536 return New_Elmt_List;
8537 end if;
8538 end loop;
8540 -- Determine if there are discriminant expressions in the constraint
8542 for J in Discr_Expr'Range loop
8543 if Denotes_Discriminant
8544 (Discr_Expr (J), Check_Concurrent => True)
8545 then
8546 Discrim_Present := True;
8547 end if;
8548 end loop;
8550 -- Build an element list consisting of the expressions given in the
8551 -- discriminant constraint and apply the appropriate checks. The list
8552 -- is constructed after resolving any named discriminant associations
8553 -- and therefore the expressions appear in the textual order of the
8554 -- discriminants.
8556 Discr := First_Discriminant (T);
8557 for J in Discr_Expr'Range loop
8558 if Discr_Expr (J) /= Error then
8559 Append_Elmt (Discr_Expr (J), Elist);
8561 -- If any of the discriminant constraints is given by a
8562 -- discriminant and we are in a derived type declaration we
8563 -- have a discriminant renaming. Establish link between new
8564 -- and old discriminant.
8566 if Denotes_Discriminant (Discr_Expr (J)) then
8567 if Derived_Def then
8568 Set_Corresponding_Discriminant
8569 (Entity (Discr_Expr (J)), Discr);
8570 end if;
8572 -- Force the evaluation of non-discriminant expressions.
8573 -- If we have found a discriminant in the constraint 3.4(26)
8574 -- and 3.8(18) demand that no range checks are performed are
8575 -- after evaluation. If the constraint is for a component
8576 -- definition that has a per-object constraint, expressions are
8577 -- evaluated but not checked either. In all other cases perform
8578 -- a range check.
8580 else
8581 if Discrim_Present then
8582 null;
8584 elsif Nkind (Parent (Parent (Def))) = N_Component_Declaration
8585 and then
8586 Has_Per_Object_Constraint
8587 (Defining_Identifier (Parent (Parent (Def))))
8588 then
8589 null;
8591 elsif Is_Access_Type (Etype (Discr)) then
8592 Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
8594 else
8595 Apply_Range_Check (Discr_Expr (J), Etype (Discr));
8596 end if;
8598 Force_Evaluation (Discr_Expr (J));
8599 end if;
8601 -- Check that the designated type of an access discriminant's
8602 -- expression is not a class-wide type unless the discriminant's
8603 -- designated type is also class-wide.
8605 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
8606 and then not Is_Class_Wide_Type
8607 (Designated_Type (Etype (Discr)))
8608 and then Etype (Discr_Expr (J)) /= Any_Type
8609 and then Is_Class_Wide_Type
8610 (Designated_Type (Etype (Discr_Expr (J))))
8611 then
8612 Wrong_Type (Discr_Expr (J), Etype (Discr));
8614 elsif Is_Access_Type (Etype (Discr))
8615 and then not Is_Access_Constant (Etype (Discr))
8616 and then Is_Access_Type (Etype (Discr_Expr (J)))
8617 and then Is_Access_Constant (Etype (Discr_Expr (J)))
8618 then
8619 Error_Msg_NE
8620 ("constraint for discriminant& must be access to variable",
8621 Def, Discr);
8622 end if;
8623 end if;
8625 Next_Discriminant (Discr);
8626 end loop;
8628 return Elist;
8629 end Build_Discriminant_Constraints;
8631 ---------------------------------
8632 -- Build_Discriminated_Subtype --
8633 ---------------------------------
8635 procedure Build_Discriminated_Subtype
8636 (T : Entity_Id;
8637 Def_Id : Entity_Id;
8638 Elist : Elist_Id;
8639 Related_Nod : Node_Id;
8640 For_Access : Boolean := False)
8642 Has_Discrs : constant Boolean := Has_Discriminants (T);
8643 Constrained : constant Boolean :=
8644 (Has_Discrs
8645 and then not Is_Empty_Elmt_List (Elist)
8646 and then not Is_Class_Wide_Type (T))
8647 or else Is_Constrained (T);
8649 begin
8650 if Ekind (T) = E_Record_Type then
8651 if For_Access then
8652 Set_Ekind (Def_Id, E_Private_Subtype);
8653 Set_Is_For_Access_Subtype (Def_Id, True);
8654 else
8655 Set_Ekind (Def_Id, E_Record_Subtype);
8656 end if;
8658 -- Inherit preelaboration flag from base, for types for which it
8659 -- may have been set: records, private types, protected types.
8661 Set_Known_To_Have_Preelab_Init
8662 (Def_Id, Known_To_Have_Preelab_Init (T));
8664 elsif Ekind (T) = E_Task_Type then
8665 Set_Ekind (Def_Id, E_Task_Subtype);
8667 elsif Ekind (T) = E_Protected_Type then
8668 Set_Ekind (Def_Id, E_Protected_Subtype);
8669 Set_Known_To_Have_Preelab_Init
8670 (Def_Id, Known_To_Have_Preelab_Init (T));
8672 elsif Is_Private_Type (T) then
8673 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
8674 Set_Known_To_Have_Preelab_Init
8675 (Def_Id, Known_To_Have_Preelab_Init (T));
8677 -- Private subtypes may have private dependents
8679 Set_Private_Dependents (Def_Id, New_Elmt_List);
8681 elsif Is_Class_Wide_Type (T) then
8682 Set_Ekind (Def_Id, E_Class_Wide_Subtype);
8684 else
8685 -- Incomplete type. Attach subtype to list of dependents, to be
8686 -- completed with full view of parent type, unless is it the
8687 -- designated subtype of a record component within an init_proc.
8688 -- This last case arises for a component of an access type whose
8689 -- designated type is incomplete (e.g. a Taft Amendment type).
8690 -- The designated subtype is within an inner scope, and needs no
8691 -- elaboration, because only the access type is needed in the
8692 -- initialization procedure.
8694 Set_Ekind (Def_Id, Ekind (T));
8696 if For_Access and then Within_Init_Proc then
8697 null;
8698 else
8699 Append_Elmt (Def_Id, Private_Dependents (T));
8700 end if;
8701 end if;
8703 Set_Etype (Def_Id, T);
8704 Init_Size_Align (Def_Id);
8705 Set_Has_Discriminants (Def_Id, Has_Discrs);
8706 Set_Is_Constrained (Def_Id, Constrained);
8708 Set_First_Entity (Def_Id, First_Entity (T));
8709 Set_Last_Entity (Def_Id, Last_Entity (T));
8710 Set_Has_Implicit_Dereference
8711 (Def_Id, Has_Implicit_Dereference (T));
8713 -- If the subtype is the completion of a private declaration, there may
8714 -- have been representation clauses for the partial view, and they must
8715 -- be preserved. Build_Derived_Type chains the inherited clauses with
8716 -- the ones appearing on the extension. If this comes from a subtype
8717 -- declaration, all clauses are inherited.
8719 if No (First_Rep_Item (Def_Id)) then
8720 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
8721 end if;
8723 if Is_Tagged_Type (T) then
8724 Set_Is_Tagged_Type (Def_Id);
8725 Make_Class_Wide_Type (Def_Id);
8726 end if;
8728 Set_Stored_Constraint (Def_Id, No_Elist);
8730 if Has_Discrs then
8731 Set_Discriminant_Constraint (Def_Id, Elist);
8732 Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
8733 end if;
8735 if Is_Tagged_Type (T) then
8737 -- Ada 2005 (AI-251): In case of concurrent types we inherit the
8738 -- concurrent record type (which has the list of primitive
8739 -- operations).
8741 if Ada_Version >= Ada_2005
8742 and then Is_Concurrent_Type (T)
8743 then
8744 Set_Corresponding_Record_Type (Def_Id,
8745 Corresponding_Record_Type (T));
8746 else
8747 Set_Direct_Primitive_Operations (Def_Id,
8748 Direct_Primitive_Operations (T));
8749 end if;
8751 Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
8752 end if;
8754 -- Subtypes introduced by component declarations do not need to be
8755 -- marked as delayed, and do not get freeze nodes, because the semantics
8756 -- verifies that the parents of the subtypes are frozen before the
8757 -- enclosing record is frozen.
8759 if not Is_Type (Scope (Def_Id)) then
8760 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
8762 if Is_Private_Type (T)
8763 and then Present (Full_View (T))
8764 then
8765 Conditional_Delay (Def_Id, Full_View (T));
8766 else
8767 Conditional_Delay (Def_Id, T);
8768 end if;
8769 end if;
8771 if Is_Record_Type (T) then
8772 Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
8774 if Has_Discrs
8775 and then not Is_Empty_Elmt_List (Elist)
8776 and then not For_Access
8777 then
8778 Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
8779 elsif not For_Access then
8780 Set_Cloned_Subtype (Def_Id, T);
8781 end if;
8782 end if;
8783 end Build_Discriminated_Subtype;
8785 ---------------------------
8786 -- Build_Itype_Reference --
8787 ---------------------------
8789 procedure Build_Itype_Reference
8790 (Ityp : Entity_Id;
8791 Nod : Node_Id)
8793 IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
8794 begin
8796 -- Itype references are only created for use by the back-end
8798 if Inside_A_Generic then
8799 return;
8800 else
8801 Set_Itype (IR, Ityp);
8802 Insert_After (Nod, IR);
8803 end if;
8804 end Build_Itype_Reference;
8806 ------------------------
8807 -- Build_Scalar_Bound --
8808 ------------------------
8810 function Build_Scalar_Bound
8811 (Bound : Node_Id;
8812 Par_T : Entity_Id;
8813 Der_T : Entity_Id) return Node_Id
8815 New_Bound : Entity_Id;
8817 begin
8818 -- Note: not clear why this is needed, how can the original bound
8819 -- be unanalyzed at this point? and if it is, what business do we
8820 -- have messing around with it? and why is the base type of the
8821 -- parent type the right type for the resolution. It probably is
8822 -- not! It is OK for the new bound we are creating, but not for
8823 -- the old one??? Still if it never happens, no problem!
8825 Analyze_And_Resolve (Bound, Base_Type (Par_T));
8827 if Nkind_In (Bound, N_Integer_Literal, N_Real_Literal) then
8828 New_Bound := New_Copy (Bound);
8829 Set_Etype (New_Bound, Der_T);
8830 Set_Analyzed (New_Bound);
8832 elsif Is_Entity_Name (Bound) then
8833 New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
8835 -- The following is almost certainly wrong. What business do we have
8836 -- relocating a node (Bound) that is presumably still attached to
8837 -- the tree elsewhere???
8839 else
8840 New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
8841 end if;
8843 Set_Etype (New_Bound, Der_T);
8844 return New_Bound;
8845 end Build_Scalar_Bound;
8847 --------------------------------
8848 -- Build_Underlying_Full_View --
8849 --------------------------------
8851 procedure Build_Underlying_Full_View
8852 (N : Node_Id;
8853 Typ : Entity_Id;
8854 Par : Entity_Id)
8856 Loc : constant Source_Ptr := Sloc (N);
8857 Subt : constant Entity_Id :=
8858 Make_Defining_Identifier
8859 (Loc, New_External_Name (Chars (Typ), 'S'));
8861 Constr : Node_Id;
8862 Indic : Node_Id;
8863 C : Node_Id;
8864 Id : Node_Id;
8866 procedure Set_Discriminant_Name (Id : Node_Id);
8867 -- If the derived type has discriminants, they may rename discriminants
8868 -- of the parent. When building the full view of the parent, we need to
8869 -- recover the names of the original discriminants if the constraint is
8870 -- given by named associations.
8872 ---------------------------
8873 -- Set_Discriminant_Name --
8874 ---------------------------
8876 procedure Set_Discriminant_Name (Id : Node_Id) is
8877 Disc : Entity_Id;
8879 begin
8880 Set_Original_Discriminant (Id, Empty);
8882 if Has_Discriminants (Typ) then
8883 Disc := First_Discriminant (Typ);
8884 while Present (Disc) loop
8885 if Chars (Disc) = Chars (Id)
8886 and then Present (Corresponding_Discriminant (Disc))
8887 then
8888 Set_Chars (Id, Chars (Corresponding_Discriminant (Disc)));
8889 end if;
8890 Next_Discriminant (Disc);
8891 end loop;
8892 end if;
8893 end Set_Discriminant_Name;
8895 -- Start of processing for Build_Underlying_Full_View
8897 begin
8898 if Nkind (N) = N_Full_Type_Declaration then
8899 Constr := Constraint (Subtype_Indication (Type_Definition (N)));
8901 elsif Nkind (N) = N_Subtype_Declaration then
8902 Constr := New_Copy_Tree (Constraint (Subtype_Indication (N)));
8904 elsif Nkind (N) = N_Component_Declaration then
8905 Constr :=
8906 New_Copy_Tree
8907 (Constraint (Subtype_Indication (Component_Definition (N))));
8909 else
8910 raise Program_Error;
8911 end if;
8913 C := First (Constraints (Constr));
8914 while Present (C) loop
8915 if Nkind (C) = N_Discriminant_Association then
8916 Id := First (Selector_Names (C));
8917 while Present (Id) loop
8918 Set_Discriminant_Name (Id);
8919 Next (Id);
8920 end loop;
8921 end if;
8923 Next (C);
8924 end loop;
8926 Indic :=
8927 Make_Subtype_Declaration (Loc,
8928 Defining_Identifier => Subt,
8929 Subtype_Indication =>
8930 Make_Subtype_Indication (Loc,
8931 Subtype_Mark => New_Reference_To (Par, Loc),
8932 Constraint => New_Copy_Tree (Constr)));
8934 -- If this is a component subtype for an outer itype, it is not
8935 -- a list member, so simply set the parent link for analysis: if
8936 -- the enclosing type does not need to be in a declarative list,
8937 -- neither do the components.
8939 if Is_List_Member (N)
8940 and then Nkind (N) /= N_Component_Declaration
8941 then
8942 Insert_Before (N, Indic);
8943 else
8944 Set_Parent (Indic, Parent (N));
8945 end if;
8947 Analyze (Indic);
8948 Set_Underlying_Full_View (Typ, Full_View (Subt));
8949 end Build_Underlying_Full_View;
8951 -------------------------------
8952 -- Check_Abstract_Overriding --
8953 -------------------------------
8955 procedure Check_Abstract_Overriding (T : Entity_Id) is
8956 Alias_Subp : Entity_Id;
8957 Elmt : Elmt_Id;
8958 Op_List : Elist_Id;
8959 Subp : Entity_Id;
8960 Type_Def : Node_Id;
8962 procedure Check_Pragma_Implemented (Subp : Entity_Id);
8963 -- Ada 2012 (AI05-0030): Subprogram Subp overrides an interface routine
8964 -- which has pragma Implemented already set. Check whether Subp's entity
8965 -- kind conforms to the implementation kind of the overridden routine.
8967 procedure Check_Pragma_Implemented
8968 (Subp : Entity_Id;
8969 Iface_Subp : Entity_Id);
8970 -- Ada 2012 (AI05-0030): Subprogram Subp overrides interface routine
8971 -- Iface_Subp and both entities have pragma Implemented already set on
8972 -- them. Check whether the two implementation kinds are conforming.
8974 procedure Inherit_Pragma_Implemented
8975 (Subp : Entity_Id;
8976 Iface_Subp : Entity_Id);
8977 -- Ada 2012 (AI05-0030): Interface primitive Subp overrides interface
8978 -- subprogram Iface_Subp which has been marked by pragma Implemented.
8979 -- Propagate the implementation kind of Iface_Subp to Subp.
8981 ------------------------------
8982 -- Check_Pragma_Implemented --
8983 ------------------------------
8985 procedure Check_Pragma_Implemented (Subp : Entity_Id) is
8986 Iface_Alias : constant Entity_Id := Interface_Alias (Subp);
8987 Impl_Kind : constant Name_Id := Implementation_Kind (Iface_Alias);
8988 Subp_Alias : constant Entity_Id := Alias (Subp);
8989 Contr_Typ : Entity_Id;
8990 Impl_Subp : Entity_Id;
8992 begin
8993 -- Subp must have an alias since it is a hidden entity used to link
8994 -- an interface subprogram to its overriding counterpart.
8996 pragma Assert (Present (Subp_Alias));
8998 -- Handle aliases to synchronized wrappers
9000 Impl_Subp := Subp_Alias;
9002 if Is_Primitive_Wrapper (Impl_Subp) then
9003 Impl_Subp := Wrapped_Entity (Impl_Subp);
9004 end if;
9006 -- Extract the type of the controlling formal
9008 Contr_Typ := Etype (First_Formal (Subp_Alias));
9010 if Is_Concurrent_Record_Type (Contr_Typ) then
9011 Contr_Typ := Corresponding_Concurrent_Type (Contr_Typ);
9012 end if;
9014 -- An interface subprogram whose implementation kind is By_Entry must
9015 -- be implemented by an entry.
9017 if Impl_Kind = Name_By_Entry
9018 and then Ekind (Impl_Subp) /= E_Entry
9019 then
9020 Error_Msg_Node_2 := Iface_Alias;
9021 Error_Msg_NE
9022 ("type & must implement abstract subprogram & with an entry",
9023 Subp_Alias, Contr_Typ);
9025 elsif Impl_Kind = Name_By_Protected_Procedure then
9027 -- An interface subprogram whose implementation kind is By_
9028 -- Protected_Procedure cannot be implemented by a primitive
9029 -- procedure of a task type.
9031 if Ekind (Contr_Typ) /= E_Protected_Type then
9032 Error_Msg_Node_2 := Contr_Typ;
9033 Error_Msg_NE
9034 ("interface subprogram & cannot be implemented by a " &
9035 "primitive procedure of task type &", Subp_Alias,
9036 Iface_Alias);
9038 -- An interface subprogram whose implementation kind is By_
9039 -- Protected_Procedure must be implemented by a procedure.
9041 elsif Ekind (Impl_Subp) /= E_Procedure then
9042 Error_Msg_Node_2 := Iface_Alias;
9043 Error_Msg_NE
9044 ("type & must implement abstract subprogram & with a " &
9045 "procedure", Subp_Alias, Contr_Typ);
9046 end if;
9047 end if;
9048 end Check_Pragma_Implemented;
9050 ------------------------------
9051 -- Check_Pragma_Implemented --
9052 ------------------------------
9054 procedure Check_Pragma_Implemented
9055 (Subp : Entity_Id;
9056 Iface_Subp : Entity_Id)
9058 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
9059 Subp_Kind : constant Name_Id := Implementation_Kind (Subp);
9061 begin
9062 -- Ada 2012 (AI05-0030): The implementation kinds of an overridden
9063 -- and overriding subprogram are different. In general this is an
9064 -- error except when the implementation kind of the overridden
9065 -- subprograms is By_Any or Optional.
9067 if Iface_Kind /= Subp_Kind
9068 and then Iface_Kind /= Name_By_Any
9069 and then Iface_Kind /= Name_Optional
9070 then
9071 if Iface_Kind = Name_By_Entry then
9072 Error_Msg_N
9073 ("incompatible implementation kind, overridden subprogram " &
9074 "is marked By_Entry", Subp);
9075 else
9076 Error_Msg_N
9077 ("incompatible implementation kind, overridden subprogram " &
9078 "is marked By_Protected_Procedure", Subp);
9079 end if;
9080 end if;
9081 end Check_Pragma_Implemented;
9083 --------------------------------
9084 -- Inherit_Pragma_Implemented --
9085 --------------------------------
9087 procedure Inherit_Pragma_Implemented
9088 (Subp : Entity_Id;
9089 Iface_Subp : Entity_Id)
9091 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
9092 Loc : constant Source_Ptr := Sloc (Subp);
9093 Impl_Prag : Node_Id;
9095 begin
9096 -- Since the implementation kind is stored as a representation item
9097 -- rather than a flag, create a pragma node.
9099 Impl_Prag :=
9100 Make_Pragma (Loc,
9101 Chars => Name_Implemented,
9102 Pragma_Argument_Associations => New_List (
9103 Make_Pragma_Argument_Association (Loc,
9104 Expression => New_Reference_To (Subp, Loc)),
9106 Make_Pragma_Argument_Association (Loc,
9107 Expression => Make_Identifier (Loc, Iface_Kind))));
9109 -- The pragma doesn't need to be analyzed because it is internally
9110 -- built. It is safe to directly register it as a rep item since we
9111 -- are only interested in the characters of the implementation kind.
9113 Record_Rep_Item (Subp, Impl_Prag);
9114 end Inherit_Pragma_Implemented;
9116 -- Start of processing for Check_Abstract_Overriding
9118 begin
9119 Op_List := Primitive_Operations (T);
9121 -- Loop to check primitive operations
9123 Elmt := First_Elmt (Op_List);
9124 while Present (Elmt) loop
9125 Subp := Node (Elmt);
9126 Alias_Subp := Alias (Subp);
9128 -- Inherited subprograms are identified by the fact that they do not
9129 -- come from source, and the associated source location is the
9130 -- location of the first subtype of the derived type.
9132 -- Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
9133 -- subprograms that "require overriding".
9135 -- Special exception, do not complain about failure to override the
9136 -- stream routines _Input and _Output, as well as the primitive
9137 -- operations used in dispatching selects since we always provide
9138 -- automatic overridings for these subprograms.
9140 -- Also ignore this rule for convention CIL since .NET libraries
9141 -- do bizarre things with interfaces???
9143 -- The partial view of T may have been a private extension, for
9144 -- which inherited functions dispatching on result are abstract.
9145 -- If the full view is a null extension, there is no need for
9146 -- overriding in Ada 2005, but wrappers need to be built for them
9147 -- (see exp_ch3, Build_Controlling_Function_Wrappers).
9149 if Is_Null_Extension (T)
9150 and then Has_Controlling_Result (Subp)
9151 and then Ada_Version >= Ada_2005
9152 and then Present (Alias_Subp)
9153 and then not Comes_From_Source (Subp)
9154 and then not Is_Abstract_Subprogram (Alias_Subp)
9155 and then not Is_Access_Type (Etype (Subp))
9156 then
9157 null;
9159 -- Ada 2005 (AI-251): Internal entities of interfaces need no
9160 -- processing because this check is done with the aliased
9161 -- entity
9163 elsif Present (Interface_Alias (Subp)) then
9164 null;
9166 elsif (Is_Abstract_Subprogram (Subp)
9167 or else Requires_Overriding (Subp)
9168 or else
9169 (Has_Controlling_Result (Subp)
9170 and then Present (Alias_Subp)
9171 and then not Comes_From_Source (Subp)
9172 and then Sloc (Subp) = Sloc (First_Subtype (T))))
9173 and then not Is_TSS (Subp, TSS_Stream_Input)
9174 and then not Is_TSS (Subp, TSS_Stream_Output)
9175 and then not Is_Abstract_Type (T)
9176 and then Convention (T) /= Convention_CIL
9177 and then not Is_Predefined_Interface_Primitive (Subp)
9179 -- Ada 2005 (AI-251): Do not consider hidden entities associated
9180 -- with abstract interface types because the check will be done
9181 -- with the aliased entity (otherwise we generate a duplicated
9182 -- error message).
9184 and then not Present (Interface_Alias (Subp))
9185 then
9186 if Present (Alias_Subp) then
9188 -- Only perform the check for a derived subprogram when the
9189 -- type has an explicit record extension. This avoids incorrect
9190 -- flagging of abstract subprograms for the case of a type
9191 -- without an extension that is derived from a formal type
9192 -- with a tagged actual (can occur within a private part).
9194 -- Ada 2005 (AI-391): In the case of an inherited function with
9195 -- a controlling result of the type, the rule does not apply if
9196 -- the type is a null extension (unless the parent function
9197 -- itself is abstract, in which case the function must still be
9198 -- be overridden). The expander will generate an overriding
9199 -- wrapper function calling the parent subprogram (see
9200 -- Exp_Ch3.Make_Controlling_Wrapper_Functions).
9202 Type_Def := Type_Definition (Parent (T));
9204 if Nkind (Type_Def) = N_Derived_Type_Definition
9205 and then Present (Record_Extension_Part (Type_Def))
9206 and then
9207 (Ada_Version < Ada_2005
9208 or else not Is_Null_Extension (T)
9209 or else Ekind (Subp) = E_Procedure
9210 or else not Has_Controlling_Result (Subp)
9211 or else Is_Abstract_Subprogram (Alias_Subp)
9212 or else Requires_Overriding (Subp)
9213 or else Is_Access_Type (Etype (Subp)))
9214 then
9215 -- Avoid reporting error in case of abstract predefined
9216 -- primitive inherited from interface type because the
9217 -- body of internally generated predefined primitives
9218 -- of tagged types are generated later by Freeze_Type
9220 if Is_Interface (Root_Type (T))
9221 and then Is_Abstract_Subprogram (Subp)
9222 and then Is_Predefined_Dispatching_Operation (Subp)
9223 and then not Comes_From_Source (Ultimate_Alias (Subp))
9224 then
9225 null;
9227 else
9228 Error_Msg_NE
9229 ("type must be declared abstract or & overridden",
9230 T, Subp);
9232 -- Traverse the whole chain of aliased subprograms to
9233 -- complete the error notification. This is especially
9234 -- useful for traceability of the chain of entities when
9235 -- the subprogram corresponds with an interface
9236 -- subprogram (which may be defined in another package).
9238 if Present (Alias_Subp) then
9239 declare
9240 E : Entity_Id;
9242 begin
9243 E := Subp;
9244 while Present (Alias (E)) loop
9246 -- Avoid reporting redundant errors on entities
9247 -- inherited from interfaces
9249 if Sloc (E) /= Sloc (T) then
9250 Error_Msg_Sloc := Sloc (E);
9251 Error_Msg_NE
9252 ("\& has been inherited #", T, Subp);
9253 end if;
9255 E := Alias (E);
9256 end loop;
9258 Error_Msg_Sloc := Sloc (E);
9260 -- AI05-0068: report if there is an overriding
9261 -- non-abstract subprogram that is invisible.
9263 if Is_Hidden (E)
9264 and then not Is_Abstract_Subprogram (E)
9265 then
9266 Error_Msg_NE
9267 ("\& subprogram# is not visible",
9268 T, Subp);
9270 else
9271 Error_Msg_NE
9272 ("\& has been inherited from subprogram #",
9273 T, Subp);
9274 end if;
9275 end;
9276 end if;
9277 end if;
9279 -- Ada 2005 (AI-345): Protected or task type implementing
9280 -- abstract interfaces.
9282 elsif Is_Concurrent_Record_Type (T)
9283 and then Present (Interfaces (T))
9284 then
9285 -- The controlling formal of Subp must be of mode "out",
9286 -- "in out" or an access-to-variable to be overridden.
9288 if Ekind (First_Formal (Subp)) = E_In_Parameter
9289 and then Ekind (Subp) /= E_Function
9290 then
9291 if not Is_Predefined_Dispatching_Operation (Subp)
9292 and then Is_Protected_Type
9293 (Corresponding_Concurrent_Type (T))
9294 then
9295 Error_Msg_PT (T, Subp);
9296 end if;
9298 -- Some other kind of overriding failure
9300 else
9301 Error_Msg_NE
9302 ("interface subprogram & must be overridden",
9303 T, Subp);
9305 -- Examine primitive operations of synchronized type,
9306 -- to find homonyms that have the wrong profile.
9308 declare
9309 Prim : Entity_Id;
9311 begin
9312 Prim :=
9313 First_Entity (Corresponding_Concurrent_Type (T));
9314 while Present (Prim) loop
9315 if Chars (Prim) = Chars (Subp) then
9316 Error_Msg_NE
9317 ("profile is not type conformant with "
9318 & "prefixed view profile of "
9319 & "inherited operation&", Prim, Subp);
9320 end if;
9322 Next_Entity (Prim);
9323 end loop;
9324 end;
9325 end if;
9326 end if;
9328 else
9329 Error_Msg_Node_2 := T;
9330 Error_Msg_N
9331 ("abstract subprogram& not allowed for type&", Subp);
9333 -- Also post unconditional warning on the type (unconditional
9334 -- so that if there are more than one of these cases, we get
9335 -- them all, and not just the first one).
9337 Error_Msg_Node_2 := Subp;
9338 Error_Msg_N ("nonabstract type& has abstract subprogram&!", T);
9339 end if;
9340 end if;
9342 -- Ada 2012 (AI05-0030): Perform some checks related to pragma
9343 -- Implemented
9345 -- Subp is an expander-generated procedure which maps an interface
9346 -- alias to a protected wrapper. The interface alias is flagged by
9347 -- pragma Implemented. Ensure that Subp is a procedure when the
9348 -- implementation kind is By_Protected_Procedure or an entry when
9349 -- By_Entry.
9351 if Ada_Version >= Ada_2012
9352 and then Is_Hidden (Subp)
9353 and then Present (Interface_Alias (Subp))
9354 and then Has_Rep_Pragma (Interface_Alias (Subp), Name_Implemented)
9355 then
9356 Check_Pragma_Implemented (Subp);
9357 end if;
9359 -- Subp is an interface primitive which overrides another interface
9360 -- primitive marked with pragma Implemented.
9362 if Ada_Version >= Ada_2012
9363 and then Present (Overridden_Operation (Subp))
9364 and then Has_Rep_Pragma
9365 (Overridden_Operation (Subp), Name_Implemented)
9366 then
9367 -- If the overriding routine is also marked by Implemented, check
9368 -- that the two implementation kinds are conforming.
9370 if Has_Rep_Pragma (Subp, Name_Implemented) then
9371 Check_Pragma_Implemented
9372 (Subp => Subp,
9373 Iface_Subp => Overridden_Operation (Subp));
9375 -- Otherwise the overriding routine inherits the implementation
9376 -- kind from the overridden subprogram.
9378 else
9379 Inherit_Pragma_Implemented
9380 (Subp => Subp,
9381 Iface_Subp => Overridden_Operation (Subp));
9382 end if;
9383 end if;
9385 Next_Elmt (Elmt);
9386 end loop;
9387 end Check_Abstract_Overriding;
9389 ------------------------------------------------
9390 -- Check_Access_Discriminant_Requires_Limited --
9391 ------------------------------------------------
9393 procedure Check_Access_Discriminant_Requires_Limited
9394 (D : Node_Id;
9395 Loc : Node_Id)
9397 begin
9398 -- A discriminant_specification for an access discriminant shall appear
9399 -- only in the declaration for a task or protected type, or for a type
9400 -- with the reserved word 'limited' in its definition or in one of its
9401 -- ancestors (RM 3.7(10)).
9403 -- AI-0063: The proper condition is that type must be immutably limited,
9404 -- or else be a partial view.
9406 if Nkind (Discriminant_Type (D)) = N_Access_Definition then
9407 if Is_Immutably_Limited_Type (Current_Scope)
9408 or else
9409 (Nkind (Parent (Current_Scope)) = N_Private_Type_Declaration
9410 and then Limited_Present (Parent (Current_Scope)))
9411 then
9412 null;
9414 else
9415 Error_Msg_N
9416 ("access discriminants allowed only for limited types", Loc);
9417 end if;
9418 end if;
9419 end Check_Access_Discriminant_Requires_Limited;
9421 -----------------------------------
9422 -- Check_Aliased_Component_Types --
9423 -----------------------------------
9425 procedure Check_Aliased_Component_Types (T : Entity_Id) is
9426 C : Entity_Id;
9428 begin
9429 -- ??? Also need to check components of record extensions, but not
9430 -- components of protected types (which are always limited).
9432 -- Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
9433 -- types to be unconstrained. This is safe because it is illegal to
9434 -- create access subtypes to such types with explicit discriminant
9435 -- constraints.
9437 if not Is_Limited_Type (T) then
9438 if Ekind (T) = E_Record_Type then
9439 C := First_Component (T);
9440 while Present (C) loop
9441 if Is_Aliased (C)
9442 and then Has_Discriminants (Etype (C))
9443 and then not Is_Constrained (Etype (C))
9444 and then not In_Instance_Body
9445 and then Ada_Version < Ada_2005
9446 then
9447 Error_Msg_N
9448 ("aliased component must be constrained (RM 3.6(11))",
9450 end if;
9452 Next_Component (C);
9453 end loop;
9455 elsif Ekind (T) = E_Array_Type then
9456 if Has_Aliased_Components (T)
9457 and then Has_Discriminants (Component_Type (T))
9458 and then not Is_Constrained (Component_Type (T))
9459 and then not In_Instance_Body
9460 and then Ada_Version < Ada_2005
9461 then
9462 Error_Msg_N
9463 ("aliased component type must be constrained (RM 3.6(11))",
9465 end if;
9466 end if;
9467 end if;
9468 end Check_Aliased_Component_Types;
9470 ----------------------
9471 -- Check_Completion --
9472 ----------------------
9474 procedure Check_Completion (Body_Id : Node_Id := Empty) is
9475 E : Entity_Id;
9477 procedure Post_Error;
9478 -- Post error message for lack of completion for entity E
9480 ----------------
9481 -- Post_Error --
9482 ----------------
9484 procedure Post_Error is
9486 procedure Missing_Body;
9487 -- Output missing body message
9489 ------------------
9490 -- Missing_Body --
9491 ------------------
9493 procedure Missing_Body is
9494 begin
9495 -- Spec is in same unit, so we can post on spec
9497 if In_Same_Source_Unit (Body_Id, E) then
9498 Error_Msg_N ("missing body for &", E);
9500 -- Spec is in a separate unit, so we have to post on the body
9502 else
9503 Error_Msg_NE ("missing body for & declared#!", Body_Id, E);
9504 end if;
9505 end Missing_Body;
9507 -- Start of processing for Post_Error
9509 begin
9510 if not Comes_From_Source (E) then
9512 if Ekind_In (E, E_Task_Type, E_Protected_Type) then
9513 -- It may be an anonymous protected type created for a
9514 -- single variable. Post error on variable, if present.
9516 declare
9517 Var : Entity_Id;
9519 begin
9520 Var := First_Entity (Current_Scope);
9521 while Present (Var) loop
9522 exit when Etype (Var) = E
9523 and then Comes_From_Source (Var);
9525 Next_Entity (Var);
9526 end loop;
9528 if Present (Var) then
9529 E := Var;
9530 end if;
9531 end;
9532 end if;
9533 end if;
9535 -- If a generated entity has no completion, then either previous
9536 -- semantic errors have disabled the expansion phase, or else we had
9537 -- missing subunits, or else we are compiling without expansion,
9538 -- or else something is very wrong.
9540 if not Comes_From_Source (E) then
9541 pragma Assert
9542 (Serious_Errors_Detected > 0
9543 or else Configurable_Run_Time_Violations > 0
9544 or else Subunits_Missing
9545 or else not Expander_Active);
9546 return;
9548 -- Here for source entity
9550 else
9551 -- Here if no body to post the error message, so we post the error
9552 -- on the declaration that has no completion. This is not really
9553 -- the right place to post it, think about this later ???
9555 if No (Body_Id) then
9556 if Is_Type (E) then
9557 Error_Msg_NE
9558 ("missing full declaration for }", Parent (E), E);
9559 else
9560 Error_Msg_NE ("missing body for &", Parent (E), E);
9561 end if;
9563 -- Package body has no completion for a declaration that appears
9564 -- in the corresponding spec. Post error on the body, with a
9565 -- reference to the non-completed declaration.
9567 else
9568 Error_Msg_Sloc := Sloc (E);
9570 if Is_Type (E) then
9571 Error_Msg_NE ("missing full declaration for }!", Body_Id, E);
9573 elsif Is_Overloadable (E)
9574 and then Current_Entity_In_Scope (E) /= E
9575 then
9576 -- It may be that the completion is mistyped and appears as
9577 -- a distinct overloading of the entity.
9579 declare
9580 Candidate : constant Entity_Id :=
9581 Current_Entity_In_Scope (E);
9582 Decl : constant Node_Id :=
9583 Unit_Declaration_Node (Candidate);
9585 begin
9586 if Is_Overloadable (Candidate)
9587 and then Ekind (Candidate) = Ekind (E)
9588 and then Nkind (Decl) = N_Subprogram_Body
9589 and then Acts_As_Spec (Decl)
9590 then
9591 Check_Type_Conformant (Candidate, E);
9593 else
9594 Missing_Body;
9595 end if;
9596 end;
9598 else
9599 Missing_Body;
9600 end if;
9601 end if;
9602 end if;
9603 end Post_Error;
9605 -- Start of processing for Check_Completion
9607 begin
9608 E := First_Entity (Current_Scope);
9609 while Present (E) loop
9610 if Is_Intrinsic_Subprogram (E) then
9611 null;
9613 -- The following situation requires special handling: a child unit
9614 -- that appears in the context clause of the body of its parent:
9616 -- procedure Parent.Child (...);
9618 -- with Parent.Child;
9619 -- package body Parent is
9621 -- Here Parent.Child appears as a local entity, but should not be
9622 -- flagged as requiring completion, because it is a compilation
9623 -- unit.
9625 -- Ignore missing completion for a subprogram that does not come from
9626 -- source (including the _Call primitive operation of RAS types,
9627 -- which has to have the flag Comes_From_Source for other purposes):
9628 -- we assume that the expander will provide the missing completion.
9629 -- In case of previous errors, other expansion actions that provide
9630 -- bodies for null procedures with not be invoked, so inhibit message
9631 -- in those cases.
9633 -- Note that E_Operator is not in the list that follows, because
9634 -- this kind is reserved for predefined operators, that are
9635 -- intrinsic and do not need completion.
9637 elsif Ekind (E) = E_Function
9638 or else Ekind (E) = E_Procedure
9639 or else Ekind (E) = E_Generic_Function
9640 or else Ekind (E) = E_Generic_Procedure
9641 then
9642 if Has_Completion (E) then
9643 null;
9645 elsif Is_Subprogram (E) and then Is_Abstract_Subprogram (E) then
9646 null;
9648 elsif Is_Subprogram (E)
9649 and then (not Comes_From_Source (E)
9650 or else Chars (E) = Name_uCall)
9651 then
9652 null;
9654 elsif
9655 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
9656 then
9657 null;
9659 elsif Nkind (Parent (E)) = N_Procedure_Specification
9660 and then Null_Present (Parent (E))
9661 and then Serious_Errors_Detected > 0
9662 then
9663 null;
9665 else
9666 Post_Error;
9667 end if;
9669 elsif Is_Entry (E) then
9670 if not Has_Completion (E) and then
9671 (Ekind (Scope (E)) = E_Protected_Object
9672 or else Ekind (Scope (E)) = E_Protected_Type)
9673 then
9674 Post_Error;
9675 end if;
9677 elsif Is_Package_Or_Generic_Package (E) then
9678 if Unit_Requires_Body (E) then
9679 if not Has_Completion (E)
9680 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
9681 N_Compilation_Unit
9682 then
9683 Post_Error;
9684 end if;
9686 elsif not Is_Child_Unit (E) then
9687 May_Need_Implicit_Body (E);
9688 end if;
9690 -- A formal incomplete type (Ada 2012) does not require a completion;
9691 -- other incomplete type declarations do.
9693 elsif Ekind (E) = E_Incomplete_Type
9694 and then No (Underlying_Type (E))
9695 and then not Is_Generic_Type (E)
9696 then
9697 Post_Error;
9699 elsif (Ekind (E) = E_Task_Type or else
9700 Ekind (E) = E_Protected_Type)
9701 and then not Has_Completion (E)
9702 then
9703 Post_Error;
9705 -- A single task declared in the current scope is a constant, verify
9706 -- that the body of its anonymous type is in the same scope. If the
9707 -- task is defined elsewhere, this may be a renaming declaration for
9708 -- which no completion is needed.
9710 elsif Ekind (E) = E_Constant
9711 and then Ekind (Etype (E)) = E_Task_Type
9712 and then not Has_Completion (Etype (E))
9713 and then Scope (Etype (E)) = Current_Scope
9714 then
9715 Post_Error;
9717 elsif Ekind (E) = E_Protected_Object
9718 and then not Has_Completion (Etype (E))
9719 then
9720 Post_Error;
9722 elsif Ekind (E) = E_Record_Type then
9723 if Is_Tagged_Type (E) then
9724 Check_Abstract_Overriding (E);
9725 Check_Conventions (E);
9726 end if;
9728 Check_Aliased_Component_Types (E);
9730 elsif Ekind (E) = E_Array_Type then
9731 Check_Aliased_Component_Types (E);
9733 end if;
9735 Next_Entity (E);
9736 end loop;
9737 end Check_Completion;
9739 ------------------------------------
9740 -- Check_CPP_Type_Has_No_Defaults --
9741 ------------------------------------
9743 procedure Check_CPP_Type_Has_No_Defaults (T : Entity_Id) is
9744 Tdef : constant Node_Id := Type_Definition (Declaration_Node (T));
9745 Clist : Node_Id;
9746 Comp : Node_Id;
9748 begin
9749 -- Obtain the component list
9751 if Nkind (Tdef) = N_Record_Definition then
9752 Clist := Component_List (Tdef);
9753 else pragma Assert (Nkind (Tdef) = N_Derived_Type_Definition);
9754 Clist := Component_List (Record_Extension_Part (Tdef));
9755 end if;
9757 -- Check all components to ensure no default expressions
9759 if Present (Clist) then
9760 Comp := First (Component_Items (Clist));
9761 while Present (Comp) loop
9762 if Present (Expression (Comp)) then
9763 Error_Msg_N
9764 ("component of imported 'C'P'P type cannot have "
9765 & "default expression", Expression (Comp));
9766 end if;
9768 Next (Comp);
9769 end loop;
9770 end if;
9771 end Check_CPP_Type_Has_No_Defaults;
9773 ----------------------------
9774 -- Check_Delta_Expression --
9775 ----------------------------
9777 procedure Check_Delta_Expression (E : Node_Id) is
9778 begin
9779 if not (Is_Real_Type (Etype (E))) then
9780 Wrong_Type (E, Any_Real);
9782 elsif not Is_OK_Static_Expression (E) then
9783 Flag_Non_Static_Expr
9784 ("non-static expression used for delta value!", E);
9786 elsif not UR_Is_Positive (Expr_Value_R (E)) then
9787 Error_Msg_N ("delta expression must be positive", E);
9789 else
9790 return;
9791 end if;
9793 -- If any of above errors occurred, then replace the incorrect
9794 -- expression by the real 0.1, which should prevent further errors.
9796 Rewrite (E,
9797 Make_Real_Literal (Sloc (E), Ureal_Tenth));
9798 Analyze_And_Resolve (E, Standard_Float);
9799 end Check_Delta_Expression;
9801 -----------------------------
9802 -- Check_Digits_Expression --
9803 -----------------------------
9805 procedure Check_Digits_Expression (E : Node_Id) is
9806 begin
9807 if not (Is_Integer_Type (Etype (E))) then
9808 Wrong_Type (E, Any_Integer);
9810 elsif not Is_OK_Static_Expression (E) then
9811 Flag_Non_Static_Expr
9812 ("non-static expression used for digits value!", E);
9814 elsif Expr_Value (E) <= 0 then
9815 Error_Msg_N ("digits value must be greater than zero", E);
9817 else
9818 return;
9819 end if;
9821 -- If any of above errors occurred, then replace the incorrect
9822 -- expression by the integer 1, which should prevent further errors.
9824 Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
9825 Analyze_And_Resolve (E, Standard_Integer);
9827 end Check_Digits_Expression;
9829 --------------------------
9830 -- Check_Initialization --
9831 --------------------------
9833 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
9834 begin
9835 if Is_Limited_Type (T)
9836 and then not In_Instance
9837 and then not In_Inlined_Body
9838 then
9839 if not OK_For_Limited_Init (T, Exp) then
9841 -- In GNAT mode, this is just a warning, to allow it to be evilly
9842 -- turned off. Otherwise it is a real error.
9844 if GNAT_Mode then
9845 Error_Msg_N
9846 ("?cannot initialize entities of limited type!", Exp);
9848 elsif Ada_Version < Ada_2005 then
9850 -- The side effect removal machinery may generate illegal Ada
9851 -- code to avoid the usage of access types and 'reference in
9852 -- SPARK mode. Since this is legal code with respect to theorem
9853 -- proving, do not emit the error.
9855 if SPARK_Mode
9856 and then Nkind (Exp) = N_Function_Call
9857 and then Nkind (Parent (Exp)) = N_Object_Declaration
9858 and then not Comes_From_Source
9859 (Defining_Identifier (Parent (Exp)))
9860 then
9861 null;
9863 else
9864 Error_Msg_N
9865 ("cannot initialize entities of limited type", Exp);
9866 Explain_Limited_Type (T, Exp);
9867 end if;
9869 else
9870 -- Specialize error message according to kind of illegal
9871 -- initial expression.
9873 if Nkind (Exp) = N_Type_Conversion
9874 and then Nkind (Expression (Exp)) = N_Function_Call
9875 then
9876 Error_Msg_N
9877 ("illegal context for call"
9878 & " to function with limited result", Exp);
9880 else
9881 Error_Msg_N
9882 ("initialization of limited object requires aggregate "
9883 & "or function call", Exp);
9884 end if;
9885 end if;
9886 end if;
9887 end if;
9888 end Check_Initialization;
9890 ----------------------
9891 -- Check_Interfaces --
9892 ----------------------
9894 procedure Check_Interfaces (N : Node_Id; Def : Node_Id) is
9895 Parent_Type : constant Entity_Id := Etype (Defining_Identifier (N));
9897 Iface : Node_Id;
9898 Iface_Def : Node_Id;
9899 Iface_Typ : Entity_Id;
9900 Parent_Node : Node_Id;
9902 Is_Task : Boolean := False;
9903 -- Set True if parent type or any progenitor is a task interface
9905 Is_Protected : Boolean := False;
9906 -- Set True if parent type or any progenitor is a protected interface
9908 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
9909 -- Check that a progenitor is compatible with declaration.
9910 -- Error is posted on Error_Node.
9912 ------------------
9913 -- Check_Ifaces --
9914 ------------------
9916 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
9917 Iface_Id : constant Entity_Id :=
9918 Defining_Identifier (Parent (Iface_Def));
9919 Type_Def : Node_Id;
9921 begin
9922 if Nkind (N) = N_Private_Extension_Declaration then
9923 Type_Def := N;
9924 else
9925 Type_Def := Type_Definition (N);
9926 end if;
9928 if Is_Task_Interface (Iface_Id) then
9929 Is_Task := True;
9931 elsif Is_Protected_Interface (Iface_Id) then
9932 Is_Protected := True;
9933 end if;
9935 if Is_Synchronized_Interface (Iface_Id) then
9937 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
9938 -- extension derived from a synchronized interface must explicitly
9939 -- be declared synchronized, because the full view will be a
9940 -- synchronized type.
9942 if Nkind (N) = N_Private_Extension_Declaration then
9943 if not Synchronized_Present (N) then
9944 Error_Msg_NE
9945 ("private extension of& must be explicitly synchronized",
9946 N, Iface_Id);
9947 end if;
9949 -- However, by 3.9.4(16/2), a full type that is a record extension
9950 -- is never allowed to derive from a synchronized interface (note
9951 -- that interfaces must be excluded from this check, because those
9952 -- are represented by derived type definitions in some cases).
9954 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition
9955 and then not Interface_Present (Type_Definition (N))
9956 then
9957 Error_Msg_N ("record extension cannot derive from synchronized"
9958 & " interface", Error_Node);
9959 end if;
9960 end if;
9962 -- Check that the characteristics of the progenitor are compatible
9963 -- with the explicit qualifier in the declaration.
9964 -- The check only applies to qualifiers that come from source.
9965 -- Limited_Present also appears in the declaration of corresponding
9966 -- records, and the check does not apply to them.
9968 if Limited_Present (Type_Def)
9969 and then not
9970 Is_Concurrent_Record_Type (Defining_Identifier (N))
9971 then
9972 if Is_Limited_Interface (Parent_Type)
9973 and then not Is_Limited_Interface (Iface_Id)
9974 then
9975 Error_Msg_NE
9976 ("progenitor& must be limited interface",
9977 Error_Node, Iface_Id);
9979 elsif
9980 (Task_Present (Iface_Def)
9981 or else Protected_Present (Iface_Def)
9982 or else Synchronized_Present (Iface_Def))
9983 and then Nkind (N) /= N_Private_Extension_Declaration
9984 and then not Error_Posted (N)
9985 then
9986 Error_Msg_NE
9987 ("progenitor& must be limited interface",
9988 Error_Node, Iface_Id);
9989 end if;
9991 -- Protected interfaces can only inherit from limited, synchronized
9992 -- or protected interfaces.
9994 elsif Nkind (N) = N_Full_Type_Declaration
9995 and then Protected_Present (Type_Def)
9996 then
9997 if Limited_Present (Iface_Def)
9998 or else Synchronized_Present (Iface_Def)
9999 or else Protected_Present (Iface_Def)
10000 then
10001 null;
10003 elsif Task_Present (Iface_Def) then
10004 Error_Msg_N ("(Ada 2005) protected interface cannot inherit"
10005 & " from task interface", Error_Node);
10007 else
10008 Error_Msg_N ("(Ada 2005) protected interface cannot inherit"
10009 & " from non-limited interface", Error_Node);
10010 end if;
10012 -- Ada 2005 (AI-345): Synchronized interfaces can only inherit from
10013 -- limited and synchronized.
10015 elsif Synchronized_Present (Type_Def) then
10016 if Limited_Present (Iface_Def)
10017 or else Synchronized_Present (Iface_Def)
10018 then
10019 null;
10021 elsif Protected_Present (Iface_Def)
10022 and then Nkind (N) /= N_Private_Extension_Declaration
10023 then
10024 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
10025 & " from protected interface", Error_Node);
10027 elsif Task_Present (Iface_Def)
10028 and then Nkind (N) /= N_Private_Extension_Declaration
10029 then
10030 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
10031 & " from task interface", Error_Node);
10033 elsif not Is_Limited_Interface (Iface_Id) then
10034 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
10035 & " from non-limited interface", Error_Node);
10036 end if;
10038 -- Ada 2005 (AI-345): Task interfaces can only inherit from limited,
10039 -- synchronized or task interfaces.
10041 elsif Nkind (N) = N_Full_Type_Declaration
10042 and then Task_Present (Type_Def)
10043 then
10044 if Limited_Present (Iface_Def)
10045 or else Synchronized_Present (Iface_Def)
10046 or else Task_Present (Iface_Def)
10047 then
10048 null;
10050 elsif Protected_Present (Iface_Def) then
10051 Error_Msg_N ("(Ada 2005) task interface cannot inherit from"
10052 & " protected interface", Error_Node);
10054 else
10055 Error_Msg_N ("(Ada 2005) task interface cannot inherit from"
10056 & " non-limited interface", Error_Node);
10057 end if;
10058 end if;
10059 end Check_Ifaces;
10061 -- Start of processing for Check_Interfaces
10063 begin
10064 if Is_Interface (Parent_Type) then
10065 if Is_Task_Interface (Parent_Type) then
10066 Is_Task := True;
10068 elsif Is_Protected_Interface (Parent_Type) then
10069 Is_Protected := True;
10070 end if;
10071 end if;
10073 if Nkind (N) = N_Private_Extension_Declaration then
10075 -- Check that progenitors are compatible with declaration
10077 Iface := First (Interface_List (Def));
10078 while Present (Iface) loop
10079 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
10081 Parent_Node := Parent (Base_Type (Iface_Typ));
10082 Iface_Def := Type_Definition (Parent_Node);
10084 if not Is_Interface (Iface_Typ) then
10085 Diagnose_Interface (Iface, Iface_Typ);
10087 else
10088 Check_Ifaces (Iface_Def, Iface);
10089 end if;
10091 Next (Iface);
10092 end loop;
10094 if Is_Task and Is_Protected then
10095 Error_Msg_N
10096 ("type cannot derive from task and protected interface", N);
10097 end if;
10099 return;
10100 end if;
10102 -- Full type declaration of derived type.
10103 -- Check compatibility with parent if it is interface type
10105 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
10106 and then Is_Interface (Parent_Type)
10107 then
10108 Parent_Node := Parent (Parent_Type);
10110 -- More detailed checks for interface varieties
10112 Check_Ifaces
10113 (Iface_Def => Type_Definition (Parent_Node),
10114 Error_Node => Subtype_Indication (Type_Definition (N)));
10115 end if;
10117 Iface := First (Interface_List (Def));
10118 while Present (Iface) loop
10119 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
10121 Parent_Node := Parent (Base_Type (Iface_Typ));
10122 Iface_Def := Type_Definition (Parent_Node);
10124 if not Is_Interface (Iface_Typ) then
10125 Diagnose_Interface (Iface, Iface_Typ);
10127 else
10128 -- "The declaration of a specific descendant of an interface
10129 -- type freezes the interface type" RM 13.14
10131 Freeze_Before (N, Iface_Typ);
10132 Check_Ifaces (Iface_Def, Error_Node => Iface);
10133 end if;
10135 Next (Iface);
10136 end loop;
10138 if Is_Task and Is_Protected then
10139 Error_Msg_N
10140 ("type cannot derive from task and protected interface", N);
10141 end if;
10142 end Check_Interfaces;
10144 ------------------------------------
10145 -- Check_Or_Process_Discriminants --
10146 ------------------------------------
10148 -- If an incomplete or private type declaration was already given for the
10149 -- type, the discriminants may have already been processed if they were
10150 -- present on the incomplete declaration. In this case a full conformance
10151 -- check has been performed in Find_Type_Name, and we then recheck here
10152 -- some properties that can't be checked on the partial view alone.
10153 -- Otherwise we call Process_Discriminants.
10155 procedure Check_Or_Process_Discriminants
10156 (N : Node_Id;
10157 T : Entity_Id;
10158 Prev : Entity_Id := Empty)
10160 begin
10161 if Has_Discriminants (T) then
10163 -- Discriminants are already set on T if they were already present
10164 -- on the partial view. Make them visible to component declarations.
10166 declare
10167 D : Entity_Id;
10168 -- Discriminant on T (full view) referencing expr on partial view
10170 Prev_D : Entity_Id;
10171 -- Entity of corresponding discriminant on partial view
10173 New_D : Node_Id;
10174 -- Discriminant specification for full view, expression is the
10175 -- syntactic copy on full view (which has been checked for
10176 -- conformance with partial view), only used here to post error
10177 -- message.
10179 begin
10180 D := First_Discriminant (T);
10181 New_D := First (Discriminant_Specifications (N));
10182 while Present (D) loop
10183 Prev_D := Current_Entity (D);
10184 Set_Current_Entity (D);
10185 Set_Is_Immediately_Visible (D);
10186 Set_Homonym (D, Prev_D);
10188 -- Handle the case where there is an untagged partial view and
10189 -- the full view is tagged: must disallow discriminants with
10190 -- defaults, unless compiling for Ada 2012, which allows a
10191 -- limited tagged type to have defaulted discriminants (see
10192 -- AI05-0214). However, suppress the error here if it was
10193 -- already reported on the default expression of the partial
10194 -- view.
10196 if Is_Tagged_Type (T)
10197 and then Present (Expression (Parent (D)))
10198 and then (not Is_Limited_Type (Current_Scope)
10199 or else Ada_Version < Ada_2012)
10200 and then not Error_Posted (Expression (Parent (D)))
10201 then
10202 if Ada_Version >= Ada_2012 then
10203 Error_Msg_N
10204 ("discriminants of nonlimited tagged type cannot have"
10205 & " defaults",
10206 Expression (New_D));
10207 else
10208 Error_Msg_N
10209 ("discriminants of tagged type cannot have defaults",
10210 Expression (New_D));
10211 end if;
10212 end if;
10214 -- Ada 2005 (AI-230): Access discriminant allowed in
10215 -- non-limited record types.
10217 if Ada_Version < Ada_2005 then
10219 -- This restriction gets applied to the full type here. It
10220 -- has already been applied earlier to the partial view.
10222 Check_Access_Discriminant_Requires_Limited (Parent (D), N);
10223 end if;
10225 Next_Discriminant (D);
10226 Next (New_D);
10227 end loop;
10228 end;
10230 elsif Present (Discriminant_Specifications (N)) then
10231 Process_Discriminants (N, Prev);
10232 end if;
10233 end Check_Or_Process_Discriminants;
10235 ----------------------
10236 -- Check_Real_Bound --
10237 ----------------------
10239 procedure Check_Real_Bound (Bound : Node_Id) is
10240 begin
10241 if not Is_Real_Type (Etype (Bound)) then
10242 Error_Msg_N
10243 ("bound in real type definition must be of real type", Bound);
10245 elsif not Is_OK_Static_Expression (Bound) then
10246 Flag_Non_Static_Expr
10247 ("non-static expression used for real type bound!", Bound);
10249 else
10250 return;
10251 end if;
10253 Rewrite
10254 (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
10255 Analyze (Bound);
10256 Resolve (Bound, Standard_Float);
10257 end Check_Real_Bound;
10259 ------------------------------
10260 -- Complete_Private_Subtype --
10261 ------------------------------
10263 procedure Complete_Private_Subtype
10264 (Priv : Entity_Id;
10265 Full : Entity_Id;
10266 Full_Base : Entity_Id;
10267 Related_Nod : Node_Id)
10269 Save_Next_Entity : Entity_Id;
10270 Save_Homonym : Entity_Id;
10272 begin
10273 -- Set semantic attributes for (implicit) private subtype completion.
10274 -- If the full type has no discriminants, then it is a copy of the full
10275 -- view of the base. Otherwise, it is a subtype of the base with a
10276 -- possible discriminant constraint. Save and restore the original
10277 -- Next_Entity field of full to ensure that the calls to Copy_Node
10278 -- do not corrupt the entity chain.
10280 -- Note that the type of the full view is the same entity as the type of
10281 -- the partial view. In this fashion, the subtype has access to the
10282 -- correct view of the parent.
10284 Save_Next_Entity := Next_Entity (Full);
10285 Save_Homonym := Homonym (Priv);
10287 case Ekind (Full_Base) is
10288 when E_Record_Type |
10289 E_Record_Subtype |
10290 Class_Wide_Kind |
10291 Private_Kind |
10292 Task_Kind |
10293 Protected_Kind =>
10294 Copy_Node (Priv, Full);
10296 Set_Has_Discriminants
10297 (Full, Has_Discriminants (Full_Base));
10298 Set_Has_Unknown_Discriminants
10299 (Full, Has_Unknown_Discriminants (Full_Base));
10300 Set_First_Entity (Full, First_Entity (Full_Base));
10301 Set_Last_Entity (Full, Last_Entity (Full_Base));
10303 when others =>
10304 Copy_Node (Full_Base, Full);
10306 Set_Chars (Full, Chars (Priv));
10307 Conditional_Delay (Full, Priv);
10308 Set_Sloc (Full, Sloc (Priv));
10309 end case;
10311 Set_Next_Entity (Full, Save_Next_Entity);
10312 Set_Homonym (Full, Save_Homonym);
10313 Set_Associated_Node_For_Itype (Full, Related_Nod);
10315 -- Set common attributes for all subtypes: kind, convention, etc.
10317 Set_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
10318 Set_Convention (Full, Convention (Full_Base));
10320 -- The Etype of the full view is inconsistent. Gigi needs to see the
10321 -- structural full view, which is what the current scheme gives:
10322 -- the Etype of the full view is the etype of the full base. However,
10323 -- if the full base is a derived type, the full view then looks like
10324 -- a subtype of the parent, not a subtype of the full base. If instead
10325 -- we write:
10327 -- Set_Etype (Full, Full_Base);
10329 -- then we get inconsistencies in the front-end (confusion between
10330 -- views). Several outstanding bugs are related to this ???
10332 Set_Is_First_Subtype (Full, False);
10333 Set_Scope (Full, Scope (Priv));
10334 Set_Size_Info (Full, Full_Base);
10335 Set_RM_Size (Full, RM_Size (Full_Base));
10336 Set_Is_Itype (Full);
10338 -- A subtype of a private-type-without-discriminants, whose full-view
10339 -- has discriminants with default expressions, is not constrained!
10341 if not Has_Discriminants (Priv) then
10342 Set_Is_Constrained (Full, Is_Constrained (Full_Base));
10344 if Has_Discriminants (Full_Base) then
10345 Set_Discriminant_Constraint
10346 (Full, Discriminant_Constraint (Full_Base));
10348 -- The partial view may have been indefinite, the full view
10349 -- might not be.
10351 Set_Has_Unknown_Discriminants
10352 (Full, Has_Unknown_Discriminants (Full_Base));
10353 end if;
10354 end if;
10356 Set_First_Rep_Item (Full, First_Rep_Item (Full_Base));
10357 Set_Depends_On_Private (Full, Has_Private_Component (Full));
10359 -- Freeze the private subtype entity if its parent is delayed, and not
10360 -- already frozen. We skip this processing if the type is an anonymous
10361 -- subtype of a record component, or is the corresponding record of a
10362 -- protected type, since ???
10364 if not Is_Type (Scope (Full)) then
10365 Set_Has_Delayed_Freeze (Full,
10366 Has_Delayed_Freeze (Full_Base)
10367 and then (not Is_Frozen (Full_Base)));
10368 end if;
10370 Set_Freeze_Node (Full, Empty);
10371 Set_Is_Frozen (Full, False);
10372 Set_Full_View (Priv, Full);
10374 if Has_Discriminants (Full) then
10375 Set_Stored_Constraint_From_Discriminant_Constraint (Full);
10376 Set_Stored_Constraint (Priv, Stored_Constraint (Full));
10378 if Has_Unknown_Discriminants (Full) then
10379 Set_Discriminant_Constraint (Full, No_Elist);
10380 end if;
10381 end if;
10383 if Ekind (Full_Base) = E_Record_Type
10384 and then Has_Discriminants (Full_Base)
10385 and then Has_Discriminants (Priv) -- might not, if errors
10386 and then not Has_Unknown_Discriminants (Priv)
10387 and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
10388 then
10389 Create_Constrained_Components
10390 (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
10392 -- If the full base is itself derived from private, build a congruent
10393 -- subtype of its underlying type, for use by the back end. For a
10394 -- constrained record component, the declaration cannot be placed on
10395 -- the component list, but it must nevertheless be built an analyzed, to
10396 -- supply enough information for Gigi to compute the size of component.
10398 elsif Ekind (Full_Base) in Private_Kind
10399 and then Is_Derived_Type (Full_Base)
10400 and then Has_Discriminants (Full_Base)
10401 and then (Ekind (Current_Scope) /= E_Record_Subtype)
10402 then
10403 if not Is_Itype (Priv)
10404 and then
10405 Nkind (Subtype_Indication (Parent (Priv))) = N_Subtype_Indication
10406 then
10407 Build_Underlying_Full_View
10408 (Parent (Priv), Full, Etype (Full_Base));
10410 elsif Nkind (Related_Nod) = N_Component_Declaration then
10411 Build_Underlying_Full_View (Related_Nod, Full, Etype (Full_Base));
10412 end if;
10414 elsif Is_Record_Type (Full_Base) then
10416 -- Show Full is simply a renaming of Full_Base
10418 Set_Cloned_Subtype (Full, Full_Base);
10419 end if;
10421 -- It is unsafe to share the bounds of a scalar type, because the Itype
10422 -- is elaborated on demand, and if a bound is non-static then different
10423 -- orders of elaboration in different units will lead to different
10424 -- external symbols.
10426 if Is_Scalar_Type (Full_Base) then
10427 Set_Scalar_Range (Full,
10428 Make_Range (Sloc (Related_Nod),
10429 Low_Bound =>
10430 Duplicate_Subexpr_No_Checks (Type_Low_Bound (Full_Base)),
10431 High_Bound =>
10432 Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
10434 -- This completion inherits the bounds of the full parent, but if
10435 -- the parent is an unconstrained floating point type, so is the
10436 -- completion.
10438 if Is_Floating_Point_Type (Full_Base) then
10439 Set_Includes_Infinities
10440 (Scalar_Range (Full), Has_Infinities (Full_Base));
10441 end if;
10442 end if;
10444 -- ??? It seems that a lot of fields are missing that should be copied
10445 -- from Full_Base to Full. Here are some that are introduced in a
10446 -- non-disruptive way but a cleanup is necessary.
10448 if Is_Tagged_Type (Full_Base) then
10449 Set_Is_Tagged_Type (Full);
10450 Set_Direct_Primitive_Operations (Full,
10451 Direct_Primitive_Operations (Full_Base));
10453 -- Inherit class_wide type of full_base in case the partial view was
10454 -- not tagged. Otherwise it has already been created when the private
10455 -- subtype was analyzed.
10457 if No (Class_Wide_Type (Full)) then
10458 Set_Class_Wide_Type (Full, Class_Wide_Type (Full_Base));
10459 end if;
10461 -- If this is a subtype of a protected or task type, constrain its
10462 -- corresponding record, unless this is a subtype without constraints,
10463 -- i.e. a simple renaming as with an actual subtype in an instance.
10465 elsif Is_Concurrent_Type (Full_Base) then
10466 if Has_Discriminants (Full)
10467 and then Present (Corresponding_Record_Type (Full_Base))
10468 and then
10469 not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
10470 then
10471 Set_Corresponding_Record_Type (Full,
10472 Constrain_Corresponding_Record
10473 (Full, Corresponding_Record_Type (Full_Base),
10474 Related_Nod, Full_Base));
10476 else
10477 Set_Corresponding_Record_Type (Full,
10478 Corresponding_Record_Type (Full_Base));
10479 end if;
10480 end if;
10482 -- Link rep item chain, and also setting of Has_Predicates from private
10483 -- subtype to full subtype, since we will need these on the full subtype
10484 -- to create the predicate function. Note that the full subtype may
10485 -- already have rep items, inherited from the full view of the base
10486 -- type, so we must be sure not to overwrite these entries.
10488 declare
10489 Append : Boolean;
10490 Item : Node_Id;
10491 Next_Item : Node_Id;
10493 begin
10494 Item := First_Rep_Item (Full);
10496 -- If no existing rep items on full type, we can just link directly
10497 -- to the list of items on the private type.
10499 if No (Item) then
10500 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
10502 -- Otherwise, search to the end of items currently linked to the full
10503 -- subtype and append the private items to the end. However, if Priv
10504 -- and Full already have the same list of rep items, then the append
10505 -- is not done, as that would create a circularity.
10507 elsif Item /= First_Rep_Item (Priv) then
10508 Append := True;
10510 loop
10511 Next_Item := Next_Rep_Item (Item);
10512 exit when No (Next_Item);
10513 Item := Next_Item;
10515 -- If the private view has aspect specifications, the full view
10516 -- inherits them. Since these aspects may already have been
10517 -- attached to the full view during derivation, do not append
10518 -- them if already present.
10520 if Item = First_Rep_Item (Priv) then
10521 Append := False;
10522 exit;
10523 end if;
10524 end loop;
10526 -- And link the private type items at the end of the chain
10528 if Append then
10529 Set_Next_Rep_Item (Item, First_Rep_Item (Priv));
10530 end if;
10531 end if;
10532 end;
10534 -- Make sure Has_Predicates is set on full type if it is set on the
10535 -- private type. Note that it may already be set on the full type and
10536 -- if so, we don't want to unset it.
10538 if Has_Predicates (Priv) then
10539 Set_Has_Predicates (Full);
10540 end if;
10541 end Complete_Private_Subtype;
10543 ----------------------------
10544 -- Constant_Redeclaration --
10545 ----------------------------
10547 procedure Constant_Redeclaration
10548 (Id : Entity_Id;
10549 N : Node_Id;
10550 T : out Entity_Id)
10552 Prev : constant Entity_Id := Current_Entity_In_Scope (Id);
10553 Obj_Def : constant Node_Id := Object_Definition (N);
10554 New_T : Entity_Id;
10556 procedure Check_Possible_Deferred_Completion
10557 (Prev_Id : Entity_Id;
10558 Prev_Obj_Def : Node_Id;
10559 Curr_Obj_Def : Node_Id);
10560 -- Determine whether the two object definitions describe the partial
10561 -- and the full view of a constrained deferred constant. Generate
10562 -- a subtype for the full view and verify that it statically matches
10563 -- the subtype of the partial view.
10565 procedure Check_Recursive_Declaration (Typ : Entity_Id);
10566 -- If deferred constant is an access type initialized with an allocator,
10567 -- check whether there is an illegal recursion in the definition,
10568 -- through a default value of some record subcomponent. This is normally
10569 -- detected when generating init procs, but requires this additional
10570 -- mechanism when expansion is disabled.
10572 ----------------------------------------
10573 -- Check_Possible_Deferred_Completion --
10574 ----------------------------------------
10576 procedure Check_Possible_Deferred_Completion
10577 (Prev_Id : Entity_Id;
10578 Prev_Obj_Def : Node_Id;
10579 Curr_Obj_Def : Node_Id)
10581 begin
10582 if Nkind (Prev_Obj_Def) = N_Subtype_Indication
10583 and then Present (Constraint (Prev_Obj_Def))
10584 and then Nkind (Curr_Obj_Def) = N_Subtype_Indication
10585 and then Present (Constraint (Curr_Obj_Def))
10586 then
10587 declare
10588 Loc : constant Source_Ptr := Sloc (N);
10589 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
10590 Decl : constant Node_Id :=
10591 Make_Subtype_Declaration (Loc,
10592 Defining_Identifier => Def_Id,
10593 Subtype_Indication =>
10594 Relocate_Node (Curr_Obj_Def));
10596 begin
10597 Insert_Before_And_Analyze (N, Decl);
10598 Set_Etype (Id, Def_Id);
10600 if not Subtypes_Statically_Match (Etype (Prev_Id), Def_Id) then
10601 Error_Msg_Sloc := Sloc (Prev_Id);
10602 Error_Msg_N ("subtype does not statically match deferred " &
10603 "declaration#", N);
10604 end if;
10605 end;
10606 end if;
10607 end Check_Possible_Deferred_Completion;
10609 ---------------------------------
10610 -- Check_Recursive_Declaration --
10611 ---------------------------------
10613 procedure Check_Recursive_Declaration (Typ : Entity_Id) is
10614 Comp : Entity_Id;
10616 begin
10617 if Is_Record_Type (Typ) then
10618 Comp := First_Component (Typ);
10619 while Present (Comp) loop
10620 if Comes_From_Source (Comp) then
10621 if Present (Expression (Parent (Comp)))
10622 and then Is_Entity_Name (Expression (Parent (Comp)))
10623 and then Entity (Expression (Parent (Comp))) = Prev
10624 then
10625 Error_Msg_Sloc := Sloc (Parent (Comp));
10626 Error_Msg_NE
10627 ("illegal circularity with declaration for&#",
10628 N, Comp);
10629 return;
10631 elsif Is_Record_Type (Etype (Comp)) then
10632 Check_Recursive_Declaration (Etype (Comp));
10633 end if;
10634 end if;
10636 Next_Component (Comp);
10637 end loop;
10638 end if;
10639 end Check_Recursive_Declaration;
10641 -- Start of processing for Constant_Redeclaration
10643 begin
10644 if Nkind (Parent (Prev)) = N_Object_Declaration then
10645 if Nkind (Object_Definition
10646 (Parent (Prev))) = N_Subtype_Indication
10647 then
10648 -- Find type of new declaration. The constraints of the two
10649 -- views must match statically, but there is no point in
10650 -- creating an itype for the full view.
10652 if Nkind (Obj_Def) = N_Subtype_Indication then
10653 Find_Type (Subtype_Mark (Obj_Def));
10654 New_T := Entity (Subtype_Mark (Obj_Def));
10656 else
10657 Find_Type (Obj_Def);
10658 New_T := Entity (Obj_Def);
10659 end if;
10661 T := Etype (Prev);
10663 else
10664 -- The full view may impose a constraint, even if the partial
10665 -- view does not, so construct the subtype.
10667 New_T := Find_Type_Of_Object (Obj_Def, N);
10668 T := New_T;
10669 end if;
10671 else
10672 -- Current declaration is illegal, diagnosed below in Enter_Name
10674 T := Empty;
10675 New_T := Any_Type;
10676 end if;
10678 -- If previous full declaration or a renaming declaration exists, or if
10679 -- a homograph is present, let Enter_Name handle it, either with an
10680 -- error or with the removal of an overridden implicit subprogram.
10682 if Ekind (Prev) /= E_Constant
10683 or else Nkind (Parent (Prev)) = N_Object_Renaming_Declaration
10684 or else Present (Expression (Parent (Prev)))
10685 or else Present (Full_View (Prev))
10686 then
10687 Enter_Name (Id);
10689 -- Verify that types of both declarations match, or else that both types
10690 -- are anonymous access types whose designated subtypes statically match
10691 -- (as allowed in Ada 2005 by AI-385).
10693 elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
10694 and then
10695 (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
10696 or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
10697 or else Is_Access_Constant (Etype (New_T)) /=
10698 Is_Access_Constant (Etype (Prev))
10699 or else Can_Never_Be_Null (Etype (New_T)) /=
10700 Can_Never_Be_Null (Etype (Prev))
10701 or else Null_Exclusion_Present (Parent (Prev)) /=
10702 Null_Exclusion_Present (Parent (Id))
10703 or else not Subtypes_Statically_Match
10704 (Designated_Type (Etype (Prev)),
10705 Designated_Type (Etype (New_T))))
10706 then
10707 Error_Msg_Sloc := Sloc (Prev);
10708 Error_Msg_N ("type does not match declaration#", N);
10709 Set_Full_View (Prev, Id);
10710 Set_Etype (Id, Any_Type);
10712 elsif
10713 Null_Exclusion_Present (Parent (Prev))
10714 and then not Null_Exclusion_Present (N)
10715 then
10716 Error_Msg_Sloc := Sloc (Prev);
10717 Error_Msg_N ("null-exclusion does not match declaration#", N);
10718 Set_Full_View (Prev, Id);
10719 Set_Etype (Id, Any_Type);
10721 -- If so, process the full constant declaration
10723 else
10724 -- RM 7.4 (6): If the subtype defined by the subtype_indication in
10725 -- the deferred declaration is constrained, then the subtype defined
10726 -- by the subtype_indication in the full declaration shall match it
10727 -- statically.
10729 Check_Possible_Deferred_Completion
10730 (Prev_Id => Prev,
10731 Prev_Obj_Def => Object_Definition (Parent (Prev)),
10732 Curr_Obj_Def => Obj_Def);
10734 Set_Full_View (Prev, Id);
10735 Set_Is_Public (Id, Is_Public (Prev));
10736 Set_Is_Internal (Id);
10737 Append_Entity (Id, Current_Scope);
10739 -- Check ALIASED present if present before (RM 7.4(7))
10741 if Is_Aliased (Prev)
10742 and then not Aliased_Present (N)
10743 then
10744 Error_Msg_Sloc := Sloc (Prev);
10745 Error_Msg_N ("ALIASED required (see declaration#)", N);
10746 end if;
10748 -- Check that placement is in private part and that the incomplete
10749 -- declaration appeared in the visible part.
10751 if Ekind (Current_Scope) = E_Package
10752 and then not In_Private_Part (Current_Scope)
10753 then
10754 Error_Msg_Sloc := Sloc (Prev);
10755 Error_Msg_N
10756 ("full constant for declaration#"
10757 & " must be in private part", N);
10759 elsif Ekind (Current_Scope) = E_Package
10760 and then
10761 List_Containing (Parent (Prev)) /=
10762 Visible_Declarations
10763 (Specification (Unit_Declaration_Node (Current_Scope)))
10764 then
10765 Error_Msg_N
10766 ("deferred constant must be declared in visible part",
10767 Parent (Prev));
10768 end if;
10770 if Is_Access_Type (T)
10771 and then Nkind (Expression (N)) = N_Allocator
10772 then
10773 Check_Recursive_Declaration (Designated_Type (T));
10774 end if;
10776 -- A deferred constant is a visible entity. If type has invariants,
10777 -- verify that the initial value satisfies them.
10779 if Has_Invariants (T) and then Present (Invariant_Procedure (T)) then
10780 Insert_After (N,
10781 Make_Invariant_Call (New_Occurrence_Of (Prev, Sloc (N))));
10782 end if;
10783 end if;
10784 end Constant_Redeclaration;
10786 ----------------------
10787 -- Constrain_Access --
10788 ----------------------
10790 procedure Constrain_Access
10791 (Def_Id : in out Entity_Id;
10792 S : Node_Id;
10793 Related_Nod : Node_Id)
10795 T : constant Entity_Id := Entity (Subtype_Mark (S));
10796 Desig_Type : constant Entity_Id := Designated_Type (T);
10797 Desig_Subtype : Entity_Id := Create_Itype (E_Void, Related_Nod);
10798 Constraint_OK : Boolean := True;
10800 function Has_Defaulted_Discriminants (Typ : Entity_Id) return Boolean;
10801 -- Simple predicate to test for defaulted discriminants
10802 -- Shouldn't this be in sem_util???
10804 ---------------------------------
10805 -- Has_Defaulted_Discriminants --
10806 ---------------------------------
10808 function Has_Defaulted_Discriminants (Typ : Entity_Id) return Boolean is
10809 begin
10810 return Has_Discriminants (Typ)
10811 and then Present (First_Discriminant (Typ))
10812 and then Present
10813 (Discriminant_Default_Value (First_Discriminant (Typ)));
10814 end Has_Defaulted_Discriminants;
10816 -- Start of processing for Constrain_Access
10818 begin
10819 if Is_Array_Type (Desig_Type) then
10820 Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
10822 elsif (Is_Record_Type (Desig_Type)
10823 or else Is_Incomplete_Or_Private_Type (Desig_Type))
10824 and then not Is_Constrained (Desig_Type)
10825 then
10826 -- ??? The following code is a temporary kludge to ignore a
10827 -- discriminant constraint on access type if it is constraining
10828 -- the current record. Avoid creating the implicit subtype of the
10829 -- record we are currently compiling since right now, we cannot
10830 -- handle these. For now, just return the access type itself.
10832 if Desig_Type = Current_Scope
10833 and then No (Def_Id)
10834 then
10835 Set_Ekind (Desig_Subtype, E_Record_Subtype);
10836 Def_Id := Entity (Subtype_Mark (S));
10838 -- This call added to ensure that the constraint is analyzed
10839 -- (needed for a B test). Note that we still return early from
10840 -- this procedure to avoid recursive processing. ???
10842 Constrain_Discriminated_Type
10843 (Desig_Subtype, S, Related_Nod, For_Access => True);
10844 return;
10845 end if;
10847 -- Enforce rule that the constraint is illegal if there is an
10848 -- unconstrained view of the designated type. This means that the
10849 -- partial view (either a private type declaration or a derivation
10850 -- from a private type) has no discriminants. (Defect Report
10851 -- 8652/0008, Technical Corrigendum 1, checked by ACATS B371001).
10853 -- Rule updated for Ada 2005: the private type is said to have
10854 -- a constrained partial view, given that objects of the type
10855 -- can be declared. Furthermore, the rule applies to all access
10856 -- types, unlike the rule concerning default discriminants (see
10857 -- RM 3.7.1(7/3))
10859 if (Ekind (T) = E_General_Access_Type
10860 or else Ada_Version >= Ada_2005)
10861 and then Has_Private_Declaration (Desig_Type)
10862 and then In_Open_Scopes (Scope (Desig_Type))
10863 and then Has_Discriminants (Desig_Type)
10864 then
10865 declare
10866 Pack : constant Node_Id :=
10867 Unit_Declaration_Node (Scope (Desig_Type));
10868 Decls : List_Id;
10869 Decl : Node_Id;
10871 begin
10872 if Nkind (Pack) = N_Package_Declaration then
10873 Decls := Visible_Declarations (Specification (Pack));
10874 Decl := First (Decls);
10875 while Present (Decl) loop
10876 if (Nkind (Decl) = N_Private_Type_Declaration
10877 and then
10878 Chars (Defining_Identifier (Decl)) =
10879 Chars (Desig_Type))
10881 or else
10882 (Nkind (Decl) = N_Full_Type_Declaration
10883 and then
10884 Chars (Defining_Identifier (Decl)) =
10885 Chars (Desig_Type)
10886 and then Is_Derived_Type (Desig_Type)
10887 and then
10888 Has_Private_Declaration (Etype (Desig_Type)))
10889 then
10890 if No (Discriminant_Specifications (Decl)) then
10891 Error_Msg_N
10892 ("cannot constrain access type if designated " &
10893 "type has constrained partial view", S);
10894 end if;
10896 exit;
10897 end if;
10899 Next (Decl);
10900 end loop;
10901 end if;
10902 end;
10903 end if;
10905 Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
10906 For_Access => True);
10908 elsif (Is_Task_Type (Desig_Type)
10909 or else Is_Protected_Type (Desig_Type))
10910 and then not Is_Constrained (Desig_Type)
10911 then
10912 Constrain_Concurrent
10913 (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
10915 else
10916 Error_Msg_N ("invalid constraint on access type", S);
10917 Desig_Subtype := Desig_Type; -- Ignore invalid constraint.
10918 Constraint_OK := False;
10919 end if;
10921 if No (Def_Id) then
10922 Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
10923 else
10924 Set_Ekind (Def_Id, E_Access_Subtype);
10925 end if;
10927 if Constraint_OK then
10928 Set_Etype (Def_Id, Base_Type (T));
10930 if Is_Private_Type (Desig_Type) then
10931 Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
10932 end if;
10933 else
10934 Set_Etype (Def_Id, Any_Type);
10935 end if;
10937 Set_Size_Info (Def_Id, T);
10938 Set_Is_Constrained (Def_Id, Constraint_OK);
10939 Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
10940 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
10941 Set_Is_Access_Constant (Def_Id, Is_Access_Constant (T));
10943 Conditional_Delay (Def_Id, T);
10945 -- AI-363 : Subtypes of general access types whose designated types have
10946 -- default discriminants are disallowed. In instances, the rule has to
10947 -- be checked against the actual, of which T is the subtype. In a
10948 -- generic body, the rule is checked assuming that the actual type has
10949 -- defaulted discriminants.
10951 if Ada_Version >= Ada_2005 or else Warn_On_Ada_2005_Compatibility then
10952 if Ekind (Base_Type (T)) = E_General_Access_Type
10953 and then Has_Defaulted_Discriminants (Desig_Type)
10954 then
10955 if Ada_Version < Ada_2005 then
10956 Error_Msg_N
10957 ("access subtype of general access type would not " &
10958 "be allowed in Ada 2005?y?", S);
10959 else
10960 Error_Msg_N
10961 ("access subtype of general access type not allowed", S);
10962 end if;
10964 Error_Msg_N ("\discriminants have defaults", S);
10966 elsif Is_Access_Type (T)
10967 and then Is_Generic_Type (Desig_Type)
10968 and then Has_Discriminants (Desig_Type)
10969 and then In_Package_Body (Current_Scope)
10970 then
10971 if Ada_Version < Ada_2005 then
10972 Error_Msg_N
10973 ("access subtype would not be allowed in generic body " &
10974 "in Ada 2005?y?", S);
10975 else
10976 Error_Msg_N
10977 ("access subtype not allowed in generic body", S);
10978 end if;
10980 Error_Msg_N
10981 ("\designated type is a discriminated formal", S);
10982 end if;
10983 end if;
10984 end Constrain_Access;
10986 ---------------------
10987 -- Constrain_Array --
10988 ---------------------
10990 procedure Constrain_Array
10991 (Def_Id : in out Entity_Id;
10992 SI : Node_Id;
10993 Related_Nod : Node_Id;
10994 Related_Id : Entity_Id;
10995 Suffix : Character)
10997 C : constant Node_Id := Constraint (SI);
10998 Number_Of_Constraints : Nat := 0;
10999 Index : Node_Id;
11000 S, T : Entity_Id;
11001 Constraint_OK : Boolean := True;
11003 begin
11004 T := Entity (Subtype_Mark (SI));
11006 if Ekind (T) in Access_Kind then
11007 T := Designated_Type (T);
11008 end if;
11010 -- If an index constraint follows a subtype mark in a subtype indication
11011 -- then the type or subtype denoted by the subtype mark must not already
11012 -- impose an index constraint. The subtype mark must denote either an
11013 -- unconstrained array type or an access type whose designated type
11014 -- is such an array type... (RM 3.6.1)
11016 if Is_Constrained (T) then
11017 Error_Msg_N ("array type is already constrained", Subtype_Mark (SI));
11018 Constraint_OK := False;
11020 else
11021 S := First (Constraints (C));
11022 while Present (S) loop
11023 Number_Of_Constraints := Number_Of_Constraints + 1;
11024 Next (S);
11025 end loop;
11027 -- In either case, the index constraint must provide a discrete
11028 -- range for each index of the array type and the type of each
11029 -- discrete range must be the same as that of the corresponding
11030 -- index. (RM 3.6.1)
11032 if Number_Of_Constraints /= Number_Dimensions (T) then
11033 Error_Msg_NE ("incorrect number of index constraints for }", C, T);
11034 Constraint_OK := False;
11036 else
11037 S := First (Constraints (C));
11038 Index := First_Index (T);
11039 Analyze (Index);
11041 -- Apply constraints to each index type
11043 for J in 1 .. Number_Of_Constraints loop
11044 Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
11045 Next (Index);
11046 Next (S);
11047 end loop;
11049 end if;
11050 end if;
11052 if No (Def_Id) then
11053 Def_Id :=
11054 Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
11055 Set_Parent (Def_Id, Related_Nod);
11057 else
11058 Set_Ekind (Def_Id, E_Array_Subtype);
11059 end if;
11061 Set_Size_Info (Def_Id, (T));
11062 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
11063 Set_Etype (Def_Id, Base_Type (T));
11065 if Constraint_OK then
11066 Set_First_Index (Def_Id, First (Constraints (C)));
11067 else
11068 Set_First_Index (Def_Id, First_Index (T));
11069 end if;
11071 Set_Is_Constrained (Def_Id, True);
11072 Set_Is_Aliased (Def_Id, Is_Aliased (T));
11073 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
11075 Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
11076 Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
11078 -- A subtype does not inherit the packed_array_type of is parent. We
11079 -- need to initialize the attribute because if Def_Id is previously
11080 -- analyzed through a limited_with clause, it will have the attributes
11081 -- of an incomplete type, one of which is an Elist that overlaps the
11082 -- Packed_Array_Type field.
11084 Set_Packed_Array_Type (Def_Id, Empty);
11086 -- Build a freeze node if parent still needs one. Also make sure that
11087 -- the Depends_On_Private status is set because the subtype will need
11088 -- reprocessing at the time the base type does, and also we must set a
11089 -- conditional delay.
11091 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
11092 Conditional_Delay (Def_Id, T);
11093 end Constrain_Array;
11095 ------------------------------
11096 -- Constrain_Component_Type --
11097 ------------------------------
11099 function Constrain_Component_Type
11100 (Comp : Entity_Id;
11101 Constrained_Typ : Entity_Id;
11102 Related_Node : Node_Id;
11103 Typ : Entity_Id;
11104 Constraints : Elist_Id) return Entity_Id
11106 Loc : constant Source_Ptr := Sloc (Constrained_Typ);
11107 Compon_Type : constant Entity_Id := Etype (Comp);
11108 Array_Comp : Node_Id;
11110 function Build_Constrained_Array_Type
11111 (Old_Type : Entity_Id) return Entity_Id;
11112 -- If Old_Type is an array type, one of whose indexes is constrained
11113 -- by a discriminant, build an Itype whose constraint replaces the
11114 -- discriminant with its value in the constraint.
11116 function Build_Constrained_Discriminated_Type
11117 (Old_Type : Entity_Id) return Entity_Id;
11118 -- Ditto for record components
11120 function Build_Constrained_Access_Type
11121 (Old_Type : Entity_Id) return Entity_Id;
11122 -- Ditto for access types. Makes use of previous two functions, to
11123 -- constrain designated type.
11125 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id;
11126 -- T is an array or discriminated type, C is a list of constraints
11127 -- that apply to T. This routine builds the constrained subtype.
11129 function Is_Discriminant (Expr : Node_Id) return Boolean;
11130 -- Returns True if Expr is a discriminant
11132 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id;
11133 -- Find the value of discriminant Discrim in Constraint
11135 -----------------------------------
11136 -- Build_Constrained_Access_Type --
11137 -----------------------------------
11139 function Build_Constrained_Access_Type
11140 (Old_Type : Entity_Id) return Entity_Id
11142 Desig_Type : constant Entity_Id := Designated_Type (Old_Type);
11143 Itype : Entity_Id;
11144 Desig_Subtype : Entity_Id;
11145 Scop : Entity_Id;
11147 begin
11148 -- if the original access type was not embedded in the enclosing
11149 -- type definition, there is no need to produce a new access
11150 -- subtype. In fact every access type with an explicit constraint
11151 -- generates an itype whose scope is the enclosing record.
11153 if not Is_Type (Scope (Old_Type)) then
11154 return Old_Type;
11156 elsif Is_Array_Type (Desig_Type) then
11157 Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
11159 elsif Has_Discriminants (Desig_Type) then
11161 -- This may be an access type to an enclosing record type for
11162 -- which we are constructing the constrained components. Return
11163 -- the enclosing record subtype. This is not always correct,
11164 -- but avoids infinite recursion. ???
11166 Desig_Subtype := Any_Type;
11168 for J in reverse 0 .. Scope_Stack.Last loop
11169 Scop := Scope_Stack.Table (J).Entity;
11171 if Is_Type (Scop)
11172 and then Base_Type (Scop) = Base_Type (Desig_Type)
11173 then
11174 Desig_Subtype := Scop;
11175 end if;
11177 exit when not Is_Type (Scop);
11178 end loop;
11180 if Desig_Subtype = Any_Type then
11181 Desig_Subtype :=
11182 Build_Constrained_Discriminated_Type (Desig_Type);
11183 end if;
11185 else
11186 return Old_Type;
11187 end if;
11189 if Desig_Subtype /= Desig_Type then
11191 -- The Related_Node better be here or else we won't be able
11192 -- to attach new itypes to a node in the tree.
11194 pragma Assert (Present (Related_Node));
11196 Itype := Create_Itype (E_Access_Subtype, Related_Node);
11198 Set_Etype (Itype, Base_Type (Old_Type));
11199 Set_Size_Info (Itype, (Old_Type));
11200 Set_Directly_Designated_Type (Itype, Desig_Subtype);
11201 Set_Depends_On_Private (Itype, Has_Private_Component
11202 (Old_Type));
11203 Set_Is_Access_Constant (Itype, Is_Access_Constant
11204 (Old_Type));
11206 -- The new itype needs freezing when it depends on a not frozen
11207 -- type and the enclosing subtype needs freezing.
11209 if Has_Delayed_Freeze (Constrained_Typ)
11210 and then not Is_Frozen (Constrained_Typ)
11211 then
11212 Conditional_Delay (Itype, Base_Type (Old_Type));
11213 end if;
11215 return Itype;
11217 else
11218 return Old_Type;
11219 end if;
11220 end Build_Constrained_Access_Type;
11222 ----------------------------------
11223 -- Build_Constrained_Array_Type --
11224 ----------------------------------
11226 function Build_Constrained_Array_Type
11227 (Old_Type : Entity_Id) return Entity_Id
11229 Lo_Expr : Node_Id;
11230 Hi_Expr : Node_Id;
11231 Old_Index : Node_Id;
11232 Range_Node : Node_Id;
11233 Constr_List : List_Id;
11235 Need_To_Create_Itype : Boolean := False;
11237 begin
11238 Old_Index := First_Index (Old_Type);
11239 while Present (Old_Index) loop
11240 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
11242 if Is_Discriminant (Lo_Expr)
11243 or else Is_Discriminant (Hi_Expr)
11244 then
11245 Need_To_Create_Itype := True;
11246 end if;
11248 Next_Index (Old_Index);
11249 end loop;
11251 if Need_To_Create_Itype then
11252 Constr_List := New_List;
11254 Old_Index := First_Index (Old_Type);
11255 while Present (Old_Index) loop
11256 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
11258 if Is_Discriminant (Lo_Expr) then
11259 Lo_Expr := Get_Discr_Value (Lo_Expr);
11260 end if;
11262 if Is_Discriminant (Hi_Expr) then
11263 Hi_Expr := Get_Discr_Value (Hi_Expr);
11264 end if;
11266 Range_Node :=
11267 Make_Range
11268 (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
11270 Append (Range_Node, To => Constr_List);
11272 Next_Index (Old_Index);
11273 end loop;
11275 return Build_Subtype (Old_Type, Constr_List);
11277 else
11278 return Old_Type;
11279 end if;
11280 end Build_Constrained_Array_Type;
11282 ------------------------------------------
11283 -- Build_Constrained_Discriminated_Type --
11284 ------------------------------------------
11286 function Build_Constrained_Discriminated_Type
11287 (Old_Type : Entity_Id) return Entity_Id
11289 Expr : Node_Id;
11290 Constr_List : List_Id;
11291 Old_Constraint : Elmt_Id;
11293 Need_To_Create_Itype : Boolean := False;
11295 begin
11296 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
11297 while Present (Old_Constraint) loop
11298 Expr := Node (Old_Constraint);
11300 if Is_Discriminant (Expr) then
11301 Need_To_Create_Itype := True;
11302 end if;
11304 Next_Elmt (Old_Constraint);
11305 end loop;
11307 if Need_To_Create_Itype then
11308 Constr_List := New_List;
11310 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
11311 while Present (Old_Constraint) loop
11312 Expr := Node (Old_Constraint);
11314 if Is_Discriminant (Expr) then
11315 Expr := Get_Discr_Value (Expr);
11316 end if;
11318 Append (New_Copy_Tree (Expr), To => Constr_List);
11320 Next_Elmt (Old_Constraint);
11321 end loop;
11323 return Build_Subtype (Old_Type, Constr_List);
11325 else
11326 return Old_Type;
11327 end if;
11328 end Build_Constrained_Discriminated_Type;
11330 -------------------
11331 -- Build_Subtype --
11332 -------------------
11334 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id is
11335 Indic : Node_Id;
11336 Subtyp_Decl : Node_Id;
11337 Def_Id : Entity_Id;
11338 Btyp : Entity_Id := Base_Type (T);
11340 begin
11341 -- The Related_Node better be here or else we won't be able to
11342 -- attach new itypes to a node in the tree.
11344 pragma Assert (Present (Related_Node));
11346 -- If the view of the component's type is incomplete or private
11347 -- with unknown discriminants, then the constraint must be applied
11348 -- to the full type.
11350 if Has_Unknown_Discriminants (Btyp)
11351 and then Present (Underlying_Type (Btyp))
11352 then
11353 Btyp := Underlying_Type (Btyp);
11354 end if;
11356 Indic :=
11357 Make_Subtype_Indication (Loc,
11358 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
11359 Constraint => Make_Index_Or_Discriminant_Constraint (Loc, C));
11361 Def_Id := Create_Itype (Ekind (T), Related_Node);
11363 Subtyp_Decl :=
11364 Make_Subtype_Declaration (Loc,
11365 Defining_Identifier => Def_Id,
11366 Subtype_Indication => Indic);
11368 Set_Parent (Subtyp_Decl, Parent (Related_Node));
11370 -- Itypes must be analyzed with checks off (see package Itypes)
11372 Analyze (Subtyp_Decl, Suppress => All_Checks);
11374 return Def_Id;
11375 end Build_Subtype;
11377 ---------------------
11378 -- Get_Discr_Value --
11379 ---------------------
11381 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id is
11382 D : Entity_Id;
11383 E : Elmt_Id;
11385 begin
11386 -- The discriminant may be declared for the type, in which case we
11387 -- find it by iterating over the list of discriminants. If the
11388 -- discriminant is inherited from a parent type, it appears as the
11389 -- corresponding discriminant of the current type. This will be the
11390 -- case when constraining an inherited component whose constraint is
11391 -- given by a discriminant of the parent.
11393 D := First_Discriminant (Typ);
11394 E := First_Elmt (Constraints);
11396 while Present (D) loop
11397 if D = Entity (Discrim)
11398 or else D = CR_Discriminant (Entity (Discrim))
11399 or else Corresponding_Discriminant (D) = Entity (Discrim)
11400 then
11401 return Node (E);
11402 end if;
11404 Next_Discriminant (D);
11405 Next_Elmt (E);
11406 end loop;
11408 -- The Corresponding_Discriminant mechanism is incomplete, because
11409 -- the correspondence between new and old discriminants is not one
11410 -- to one: one new discriminant can constrain several old ones. In
11411 -- that case, scan sequentially the stored_constraint, the list of
11412 -- discriminants of the parents, and the constraints.
11414 -- Previous code checked for the present of the Stored_Constraint
11415 -- list for the derived type, but did not use it at all. Should it
11416 -- be present when the component is a discriminated task type?
11418 if Is_Derived_Type (Typ)
11419 and then Scope (Entity (Discrim)) = Etype (Typ)
11420 then
11421 D := First_Discriminant (Etype (Typ));
11422 E := First_Elmt (Constraints);
11423 while Present (D) loop
11424 if D = Entity (Discrim) then
11425 return Node (E);
11426 end if;
11428 Next_Discriminant (D);
11429 Next_Elmt (E);
11430 end loop;
11431 end if;
11433 -- Something is wrong if we did not find the value
11435 raise Program_Error;
11436 end Get_Discr_Value;
11438 ---------------------
11439 -- Is_Discriminant --
11440 ---------------------
11442 function Is_Discriminant (Expr : Node_Id) return Boolean is
11443 Discrim_Scope : Entity_Id;
11445 begin
11446 if Denotes_Discriminant (Expr) then
11447 Discrim_Scope := Scope (Entity (Expr));
11449 -- Either we have a reference to one of Typ's discriminants,
11451 pragma Assert (Discrim_Scope = Typ
11453 -- or to the discriminants of the parent type, in the case
11454 -- of a derivation of a tagged type with variants.
11456 or else Discrim_Scope = Etype (Typ)
11457 or else Full_View (Discrim_Scope) = Etype (Typ)
11459 -- or same as above for the case where the discriminants
11460 -- were declared in Typ's private view.
11462 or else (Is_Private_Type (Discrim_Scope)
11463 and then Chars (Discrim_Scope) = Chars (Typ))
11465 -- or else we are deriving from the full view and the
11466 -- discriminant is declared in the private entity.
11468 or else (Is_Private_Type (Typ)
11469 and then Chars (Discrim_Scope) = Chars (Typ))
11471 -- Or we are constrained the corresponding record of a
11472 -- synchronized type that completes a private declaration.
11474 or else (Is_Concurrent_Record_Type (Typ)
11475 and then
11476 Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
11478 -- or we have a class-wide type, in which case make sure the
11479 -- discriminant found belongs to the root type.
11481 or else (Is_Class_Wide_Type (Typ)
11482 and then Etype (Typ) = Discrim_Scope));
11484 return True;
11485 end if;
11487 -- In all other cases we have something wrong
11489 return False;
11490 end Is_Discriminant;
11492 -- Start of processing for Constrain_Component_Type
11494 begin
11495 if Nkind (Parent (Comp)) = N_Component_Declaration
11496 and then Comes_From_Source (Parent (Comp))
11497 and then Comes_From_Source
11498 (Subtype_Indication (Component_Definition (Parent (Comp))))
11499 and then
11500 Is_Entity_Name
11501 (Subtype_Indication (Component_Definition (Parent (Comp))))
11502 then
11503 return Compon_Type;
11505 elsif Is_Array_Type (Compon_Type) then
11506 Array_Comp := Build_Constrained_Array_Type (Compon_Type);
11508 -- If the component of the parent is packed, and the record type is
11509 -- already frozen, as is the case for an itype, the component type
11510 -- itself will not be frozen, and the packed array type for it must
11511 -- be constructed explicitly. Since the creation of packed types is
11512 -- an expansion activity, we only do this if expansion is active.
11514 if Expander_Active
11515 and then Is_Packed (Compon_Type)
11516 and then Is_Frozen (Current_Scope)
11517 then
11518 Create_Packed_Array_Type (Array_Comp);
11519 end if;
11521 return Array_Comp;
11523 elsif Has_Discriminants (Compon_Type) then
11524 return Build_Constrained_Discriminated_Type (Compon_Type);
11526 elsif Is_Access_Type (Compon_Type) then
11527 return Build_Constrained_Access_Type (Compon_Type);
11529 else
11530 return Compon_Type;
11531 end if;
11532 end Constrain_Component_Type;
11534 --------------------------
11535 -- Constrain_Concurrent --
11536 --------------------------
11538 -- For concurrent types, the associated record value type carries the same
11539 -- discriminants, so when we constrain a concurrent type, we must constrain
11540 -- the corresponding record type as well.
11542 procedure Constrain_Concurrent
11543 (Def_Id : in out Entity_Id;
11544 SI : Node_Id;
11545 Related_Nod : Node_Id;
11546 Related_Id : Entity_Id;
11547 Suffix : Character)
11549 -- Retrieve Base_Type to ensure getting to the concurrent type in the
11550 -- case of a private subtype (needed when only doing semantic analysis).
11552 T_Ent : Entity_Id := Base_Type (Entity (Subtype_Mark (SI)));
11553 T_Val : Entity_Id;
11555 begin
11556 if Ekind (T_Ent) in Access_Kind then
11557 T_Ent := Designated_Type (T_Ent);
11558 end if;
11560 T_Val := Corresponding_Record_Type (T_Ent);
11562 if Present (T_Val) then
11564 if No (Def_Id) then
11565 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
11566 end if;
11568 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
11570 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
11571 Set_Corresponding_Record_Type (Def_Id,
11572 Constrain_Corresponding_Record
11573 (Def_Id, T_Val, Related_Nod, Related_Id));
11575 else
11576 -- If there is no associated record, expansion is disabled and this
11577 -- is a generic context. Create a subtype in any case, so that
11578 -- semantic analysis can proceed.
11580 if No (Def_Id) then
11581 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
11582 end if;
11584 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
11585 end if;
11586 end Constrain_Concurrent;
11588 ------------------------------------
11589 -- Constrain_Corresponding_Record --
11590 ------------------------------------
11592 function Constrain_Corresponding_Record
11593 (Prot_Subt : Entity_Id;
11594 Corr_Rec : Entity_Id;
11595 Related_Nod : Node_Id;
11596 Related_Id : Entity_Id) return Entity_Id
11598 T_Sub : constant Entity_Id :=
11599 Create_Itype (E_Record_Subtype, Related_Nod, Related_Id, 'V');
11601 begin
11602 Set_Etype (T_Sub, Corr_Rec);
11603 Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
11604 Set_Is_Constrained (T_Sub, True);
11605 Set_First_Entity (T_Sub, First_Entity (Corr_Rec));
11606 Set_Last_Entity (T_Sub, Last_Entity (Corr_Rec));
11608 -- As elsewhere, we do not want to create a freeze node for this itype
11609 -- if it is created for a constrained component of an enclosing record
11610 -- because references to outer discriminants will appear out of scope.
11612 if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
11613 Conditional_Delay (T_Sub, Corr_Rec);
11614 else
11615 Set_Is_Frozen (T_Sub);
11616 end if;
11618 if Has_Discriminants (Prot_Subt) then -- False only if errors.
11619 Set_Discriminant_Constraint
11620 (T_Sub, Discriminant_Constraint (Prot_Subt));
11621 Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
11622 Create_Constrained_Components
11623 (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
11624 end if;
11626 Set_Depends_On_Private (T_Sub, Has_Private_Component (T_Sub));
11628 return T_Sub;
11629 end Constrain_Corresponding_Record;
11631 -----------------------
11632 -- Constrain_Decimal --
11633 -----------------------
11635 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id) is
11636 T : constant Entity_Id := Entity (Subtype_Mark (S));
11637 C : constant Node_Id := Constraint (S);
11638 Loc : constant Source_Ptr := Sloc (C);
11639 Range_Expr : Node_Id;
11640 Digits_Expr : Node_Id;
11641 Digits_Val : Uint;
11642 Bound_Val : Ureal;
11644 begin
11645 Set_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
11647 if Nkind (C) = N_Range_Constraint then
11648 Range_Expr := Range_Expression (C);
11649 Digits_Val := Digits_Value (T);
11651 else
11652 pragma Assert (Nkind (C) = N_Digits_Constraint);
11654 Check_SPARK_Restriction ("digits constraint is not allowed", S);
11656 Digits_Expr := Digits_Expression (C);
11657 Analyze_And_Resolve (Digits_Expr, Any_Integer);
11659 Check_Digits_Expression (Digits_Expr);
11660 Digits_Val := Expr_Value (Digits_Expr);
11662 if Digits_Val > Digits_Value (T) then
11663 Error_Msg_N
11664 ("digits expression is incompatible with subtype", C);
11665 Digits_Val := Digits_Value (T);
11666 end if;
11668 if Present (Range_Constraint (C)) then
11669 Range_Expr := Range_Expression (Range_Constraint (C));
11670 else
11671 Range_Expr := Empty;
11672 end if;
11673 end if;
11675 Set_Etype (Def_Id, Base_Type (T));
11676 Set_Size_Info (Def_Id, (T));
11677 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
11678 Set_Delta_Value (Def_Id, Delta_Value (T));
11679 Set_Scale_Value (Def_Id, Scale_Value (T));
11680 Set_Small_Value (Def_Id, Small_Value (T));
11681 Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
11682 Set_Digits_Value (Def_Id, Digits_Val);
11684 -- Manufacture range from given digits value if no range present
11686 if No (Range_Expr) then
11687 Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
11688 Range_Expr :=
11689 Make_Range (Loc,
11690 Low_Bound =>
11691 Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
11692 High_Bound =>
11693 Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
11694 end if;
11696 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
11697 Set_Discrete_RM_Size (Def_Id);
11699 -- Unconditionally delay the freeze, since we cannot set size
11700 -- information in all cases correctly until the freeze point.
11702 Set_Has_Delayed_Freeze (Def_Id);
11703 end Constrain_Decimal;
11705 ----------------------------------
11706 -- Constrain_Discriminated_Type --
11707 ----------------------------------
11709 procedure Constrain_Discriminated_Type
11710 (Def_Id : Entity_Id;
11711 S : Node_Id;
11712 Related_Nod : Node_Id;
11713 For_Access : Boolean := False)
11715 E : constant Entity_Id := Entity (Subtype_Mark (S));
11716 T : Entity_Id;
11717 C : Node_Id;
11718 Elist : Elist_Id := New_Elmt_List;
11720 procedure Fixup_Bad_Constraint;
11721 -- This is called after finding a bad constraint, and after having
11722 -- posted an appropriate error message. The mission is to leave the
11723 -- entity T in as reasonable state as possible!
11725 --------------------------
11726 -- Fixup_Bad_Constraint --
11727 --------------------------
11729 procedure Fixup_Bad_Constraint is
11730 begin
11731 -- Set a reasonable Ekind for the entity. For an incomplete type,
11732 -- we can't do much, but for other types, we can set the proper
11733 -- corresponding subtype kind.
11735 if Ekind (T) = E_Incomplete_Type then
11736 Set_Ekind (Def_Id, Ekind (T));
11737 else
11738 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
11739 end if;
11741 -- Set Etype to the known type, to reduce chances of cascaded errors
11743 Set_Etype (Def_Id, E);
11744 Set_Error_Posted (Def_Id);
11745 end Fixup_Bad_Constraint;
11747 -- Start of processing for Constrain_Discriminated_Type
11749 begin
11750 C := Constraint (S);
11752 -- A discriminant constraint is only allowed in a subtype indication,
11753 -- after a subtype mark. This subtype mark must denote either a type
11754 -- with discriminants, or an access type whose designated type is a
11755 -- type with discriminants. A discriminant constraint specifies the
11756 -- values of these discriminants (RM 3.7.2(5)).
11758 T := Base_Type (Entity (Subtype_Mark (S)));
11760 if Ekind (T) in Access_Kind then
11761 T := Designated_Type (T);
11762 end if;
11764 -- Ada 2005 (AI-412): Constrained incomplete subtypes are illegal.
11765 -- Avoid generating an error for access-to-incomplete subtypes.
11767 if Ada_Version >= Ada_2005
11768 and then Ekind (T) = E_Incomplete_Type
11769 and then Nkind (Parent (S)) = N_Subtype_Declaration
11770 and then not Is_Itype (Def_Id)
11771 then
11772 -- A little sanity check, emit an error message if the type
11773 -- has discriminants to begin with. Type T may be a regular
11774 -- incomplete type or imported via a limited with clause.
11776 if Has_Discriminants (T)
11777 or else
11778 (From_With_Type (T)
11779 and then Present (Non_Limited_View (T))
11780 and then Nkind (Parent (Non_Limited_View (T))) =
11781 N_Full_Type_Declaration
11782 and then Present (Discriminant_Specifications
11783 (Parent (Non_Limited_View (T)))))
11784 then
11785 Error_Msg_N
11786 ("(Ada 2005) incomplete subtype may not be constrained", C);
11787 else
11788 Error_Msg_N ("invalid constraint: type has no discriminant", C);
11789 end if;
11791 Fixup_Bad_Constraint;
11792 return;
11794 -- Check that the type has visible discriminants. The type may be
11795 -- a private type with unknown discriminants whose full view has
11796 -- discriminants which are invisible.
11798 elsif not Has_Discriminants (T)
11799 or else
11800 (Has_Unknown_Discriminants (T)
11801 and then Is_Private_Type (T))
11802 then
11803 Error_Msg_N ("invalid constraint: type has no discriminant", C);
11804 Fixup_Bad_Constraint;
11805 return;
11807 elsif Is_Constrained (E)
11808 or else (Ekind (E) = E_Class_Wide_Subtype
11809 and then Present (Discriminant_Constraint (E)))
11810 then
11811 Error_Msg_N ("type is already constrained", Subtype_Mark (S));
11812 Fixup_Bad_Constraint;
11813 return;
11814 end if;
11816 -- T may be an unconstrained subtype (e.g. a generic actual).
11817 -- Constraint applies to the base type.
11819 T := Base_Type (T);
11821 Elist := Build_Discriminant_Constraints (T, S);
11823 -- If the list returned was empty we had an error in building the
11824 -- discriminant constraint. We have also already signalled an error
11825 -- in the incomplete type case
11827 if Is_Empty_Elmt_List (Elist) then
11828 Fixup_Bad_Constraint;
11829 return;
11830 end if;
11832 Build_Discriminated_Subtype (T, Def_Id, Elist, Related_Nod, For_Access);
11833 end Constrain_Discriminated_Type;
11835 ---------------------------
11836 -- Constrain_Enumeration --
11837 ---------------------------
11839 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id) is
11840 T : constant Entity_Id := Entity (Subtype_Mark (S));
11841 C : constant Node_Id := Constraint (S);
11843 begin
11844 Set_Ekind (Def_Id, E_Enumeration_Subtype);
11846 Set_First_Literal (Def_Id, First_Literal (Base_Type (T)));
11848 Set_Etype (Def_Id, Base_Type (T));
11849 Set_Size_Info (Def_Id, (T));
11850 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
11851 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
11853 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
11855 Set_Discrete_RM_Size (Def_Id);
11856 end Constrain_Enumeration;
11858 ----------------------
11859 -- Constrain_Float --
11860 ----------------------
11862 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id) is
11863 T : constant Entity_Id := Entity (Subtype_Mark (S));
11864 C : Node_Id;
11865 D : Node_Id;
11866 Rais : Node_Id;
11868 begin
11869 Set_Ekind (Def_Id, E_Floating_Point_Subtype);
11871 Set_Etype (Def_Id, Base_Type (T));
11872 Set_Size_Info (Def_Id, (T));
11873 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
11875 -- Process the constraint
11877 C := Constraint (S);
11879 -- Digits constraint present
11881 if Nkind (C) = N_Digits_Constraint then
11883 Check_SPARK_Restriction ("digits constraint is not allowed", S);
11884 Check_Restriction (No_Obsolescent_Features, C);
11886 if Warn_On_Obsolescent_Feature then
11887 Error_Msg_N
11888 ("subtype digits constraint is an " &
11889 "obsolescent feature (RM J.3(8))?j?", C);
11890 end if;
11892 D := Digits_Expression (C);
11893 Analyze_And_Resolve (D, Any_Integer);
11894 Check_Digits_Expression (D);
11895 Set_Digits_Value (Def_Id, Expr_Value (D));
11897 -- Check that digits value is in range. Obviously we can do this
11898 -- at compile time, but it is strictly a runtime check, and of
11899 -- course there is an ACVC test that checks this!
11901 if Digits_Value (Def_Id) > Digits_Value (T) then
11902 Error_Msg_Uint_1 := Digits_Value (T);
11903 Error_Msg_N ("??digits value is too large, maximum is ^", D);
11904 Rais :=
11905 Make_Raise_Constraint_Error (Sloc (D),
11906 Reason => CE_Range_Check_Failed);
11907 Insert_Action (Declaration_Node (Def_Id), Rais);
11908 end if;
11910 C := Range_Constraint (C);
11912 -- No digits constraint present
11914 else
11915 Set_Digits_Value (Def_Id, Digits_Value (T));
11916 end if;
11918 -- Range constraint present
11920 if Nkind (C) = N_Range_Constraint then
11921 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
11923 -- No range constraint present
11925 else
11926 pragma Assert (No (C));
11927 Set_Scalar_Range (Def_Id, Scalar_Range (T));
11928 end if;
11930 Set_Is_Constrained (Def_Id);
11931 end Constrain_Float;
11933 ---------------------
11934 -- Constrain_Index --
11935 ---------------------
11937 procedure Constrain_Index
11938 (Index : Node_Id;
11939 S : Node_Id;
11940 Related_Nod : Node_Id;
11941 Related_Id : Entity_Id;
11942 Suffix : Character;
11943 Suffix_Index : Nat)
11945 Def_Id : Entity_Id;
11946 R : Node_Id := Empty;
11947 T : constant Entity_Id := Etype (Index);
11949 begin
11950 if Nkind (S) = N_Range
11951 or else
11952 (Nkind (S) = N_Attribute_Reference
11953 and then Attribute_Name (S) = Name_Range)
11954 then
11955 -- A Range attribute will be transformed into N_Range by Resolve
11957 Analyze (S);
11958 Set_Etype (S, T);
11959 R := S;
11961 Process_Range_Expr_In_Decl (R, T, Empty_List);
11963 if not Error_Posted (S)
11964 and then
11965 (Nkind (S) /= N_Range
11966 or else not Covers (T, (Etype (Low_Bound (S))))
11967 or else not Covers (T, (Etype (High_Bound (S)))))
11968 then
11969 if Base_Type (T) /= Any_Type
11970 and then Etype (Low_Bound (S)) /= Any_Type
11971 and then Etype (High_Bound (S)) /= Any_Type
11972 then
11973 Error_Msg_N ("range expected", S);
11974 end if;
11975 end if;
11977 elsif Nkind (S) = N_Subtype_Indication then
11979 -- The parser has verified that this is a discrete indication
11981 Resolve_Discrete_Subtype_Indication (S, T);
11982 R := Range_Expression (Constraint (S));
11984 -- Capture values of bounds and generate temporaries for them if
11985 -- needed, since checks may cause duplication of the expressions
11986 -- which must not be reevaluated.
11988 -- The forced evaluation removes side effects from expressions,
11989 -- which should occur also in SPARK mode. Otherwise, we end up with
11990 -- unexpected insertions of actions at places where this is not
11991 -- supposed to occur, e.g. on default parameters of a call.
11993 if Expander_Active then
11994 Force_Evaluation (Low_Bound (R));
11995 Force_Evaluation (High_Bound (R));
11996 end if;
11998 elsif Nkind (S) = N_Discriminant_Association then
12000 -- Syntactically valid in subtype indication
12002 Error_Msg_N ("invalid index constraint", S);
12003 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
12004 return;
12006 -- Subtype_Mark case, no anonymous subtypes to construct
12008 else
12009 Analyze (S);
12011 if Is_Entity_Name (S) then
12012 if not Is_Type (Entity (S)) then
12013 Error_Msg_N ("expect subtype mark for index constraint", S);
12015 elsif Base_Type (Entity (S)) /= Base_Type (T) then
12016 Wrong_Type (S, Base_Type (T));
12018 -- Check error of subtype with predicate in index constraint
12020 else
12021 Bad_Predicated_Subtype_Use
12022 ("subtype& has predicate, not allowed in index constraint",
12023 S, Entity (S));
12024 end if;
12026 return;
12028 else
12029 Error_Msg_N ("invalid index constraint", S);
12030 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
12031 return;
12032 end if;
12033 end if;
12035 Def_Id :=
12036 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
12038 Set_Etype (Def_Id, Base_Type (T));
12040 if Is_Modular_Integer_Type (T) then
12041 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
12043 elsif Is_Integer_Type (T) then
12044 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
12046 else
12047 Set_Ekind (Def_Id, E_Enumeration_Subtype);
12048 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
12049 Set_First_Literal (Def_Id, First_Literal (T));
12050 end if;
12052 Set_Size_Info (Def_Id, (T));
12053 Set_RM_Size (Def_Id, RM_Size (T));
12054 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
12056 Set_Scalar_Range (Def_Id, R);
12058 Set_Etype (S, Def_Id);
12059 Set_Discrete_RM_Size (Def_Id);
12060 end Constrain_Index;
12062 -----------------------
12063 -- Constrain_Integer --
12064 -----------------------
12066 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id) is
12067 T : constant Entity_Id := Entity (Subtype_Mark (S));
12068 C : constant Node_Id := Constraint (S);
12070 begin
12071 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
12073 if Is_Modular_Integer_Type (T) then
12074 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
12075 else
12076 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
12077 end if;
12079 Set_Etype (Def_Id, Base_Type (T));
12080 Set_Size_Info (Def_Id, (T));
12081 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
12082 Set_Discrete_RM_Size (Def_Id);
12083 end Constrain_Integer;
12085 ------------------------------
12086 -- Constrain_Ordinary_Fixed --
12087 ------------------------------
12089 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id) is
12090 T : constant Entity_Id := Entity (Subtype_Mark (S));
12091 C : Node_Id;
12092 D : Node_Id;
12093 Rais : Node_Id;
12095 begin
12096 Set_Ekind (Def_Id, E_Ordinary_Fixed_Point_Subtype);
12097 Set_Etype (Def_Id, Base_Type (T));
12098 Set_Size_Info (Def_Id, (T));
12099 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
12100 Set_Small_Value (Def_Id, Small_Value (T));
12102 -- Process the constraint
12104 C := Constraint (S);
12106 -- Delta constraint present
12108 if Nkind (C) = N_Delta_Constraint then
12110 Check_SPARK_Restriction ("delta constraint is not allowed", S);
12111 Check_Restriction (No_Obsolescent_Features, C);
12113 if Warn_On_Obsolescent_Feature then
12114 Error_Msg_S
12115 ("subtype delta constraint is an " &
12116 "obsolescent feature (RM J.3(7))?j?");
12117 end if;
12119 D := Delta_Expression (C);
12120 Analyze_And_Resolve (D, Any_Real);
12121 Check_Delta_Expression (D);
12122 Set_Delta_Value (Def_Id, Expr_Value_R (D));
12124 -- Check that delta value is in range. Obviously we can do this
12125 -- at compile time, but it is strictly a runtime check, and of
12126 -- course there is an ACVC test that checks this!
12128 if Delta_Value (Def_Id) < Delta_Value (T) then
12129 Error_Msg_N ("??delta value is too small", D);
12130 Rais :=
12131 Make_Raise_Constraint_Error (Sloc (D),
12132 Reason => CE_Range_Check_Failed);
12133 Insert_Action (Declaration_Node (Def_Id), Rais);
12134 end if;
12136 C := Range_Constraint (C);
12138 -- No delta constraint present
12140 else
12141 Set_Delta_Value (Def_Id, Delta_Value (T));
12142 end if;
12144 -- Range constraint present
12146 if Nkind (C) = N_Range_Constraint then
12147 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
12149 -- No range constraint present
12151 else
12152 pragma Assert (No (C));
12153 Set_Scalar_Range (Def_Id, Scalar_Range (T));
12155 end if;
12157 Set_Discrete_RM_Size (Def_Id);
12159 -- Unconditionally delay the freeze, since we cannot set size
12160 -- information in all cases correctly until the freeze point.
12162 Set_Has_Delayed_Freeze (Def_Id);
12163 end Constrain_Ordinary_Fixed;
12165 -----------------------
12166 -- Contain_Interface --
12167 -----------------------
12169 function Contain_Interface
12170 (Iface : Entity_Id;
12171 Ifaces : Elist_Id) return Boolean
12173 Iface_Elmt : Elmt_Id;
12175 begin
12176 if Present (Ifaces) then
12177 Iface_Elmt := First_Elmt (Ifaces);
12178 while Present (Iface_Elmt) loop
12179 if Node (Iface_Elmt) = Iface then
12180 return True;
12181 end if;
12183 Next_Elmt (Iface_Elmt);
12184 end loop;
12185 end if;
12187 return False;
12188 end Contain_Interface;
12190 ---------------------------
12191 -- Convert_Scalar_Bounds --
12192 ---------------------------
12194 procedure Convert_Scalar_Bounds
12195 (N : Node_Id;
12196 Parent_Type : Entity_Id;
12197 Derived_Type : Entity_Id;
12198 Loc : Source_Ptr)
12200 Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
12202 Lo : Node_Id;
12203 Hi : Node_Id;
12204 Rng : Node_Id;
12206 begin
12207 -- Defend against previous errors
12209 if No (Scalar_Range (Derived_Type)) then
12210 Check_Error_Detected;
12211 return;
12212 end if;
12214 Lo := Build_Scalar_Bound
12215 (Type_Low_Bound (Derived_Type),
12216 Parent_Type, Implicit_Base);
12218 Hi := Build_Scalar_Bound
12219 (Type_High_Bound (Derived_Type),
12220 Parent_Type, Implicit_Base);
12222 Rng :=
12223 Make_Range (Loc,
12224 Low_Bound => Lo,
12225 High_Bound => Hi);
12227 Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
12229 Set_Parent (Rng, N);
12230 Set_Scalar_Range (Derived_Type, Rng);
12232 -- Analyze the bounds
12234 Analyze_And_Resolve (Lo, Implicit_Base);
12235 Analyze_And_Resolve (Hi, Implicit_Base);
12237 -- Analyze the range itself, except that we do not analyze it if
12238 -- the bounds are real literals, and we have a fixed-point type.
12239 -- The reason for this is that we delay setting the bounds in this
12240 -- case till we know the final Small and Size values (see circuit
12241 -- in Freeze.Freeze_Fixed_Point_Type for further details).
12243 if Is_Fixed_Point_Type (Parent_Type)
12244 and then Nkind (Lo) = N_Real_Literal
12245 and then Nkind (Hi) = N_Real_Literal
12246 then
12247 return;
12249 -- Here we do the analysis of the range
12251 -- Note: we do this manually, since if we do a normal Analyze and
12252 -- Resolve call, there are problems with the conversions used for
12253 -- the derived type range.
12255 else
12256 Set_Etype (Rng, Implicit_Base);
12257 Set_Analyzed (Rng, True);
12258 end if;
12259 end Convert_Scalar_Bounds;
12261 -------------------
12262 -- Copy_And_Swap --
12263 -------------------
12265 procedure Copy_And_Swap (Priv, Full : Entity_Id) is
12266 begin
12267 -- Initialize new full declaration entity by copying the pertinent
12268 -- fields of the corresponding private declaration entity.
12270 -- We temporarily set Ekind to a value appropriate for a type to
12271 -- avoid assert failures in Einfo from checking for setting type
12272 -- attributes on something that is not a type. Ekind (Priv) is an
12273 -- appropriate choice, since it allowed the attributes to be set
12274 -- in the first place. This Ekind value will be modified later.
12276 Set_Ekind (Full, Ekind (Priv));
12278 -- Also set Etype temporarily to Any_Type, again, in the absence
12279 -- of errors, it will be properly reset, and if there are errors,
12280 -- then we want a value of Any_Type to remain.
12282 Set_Etype (Full, Any_Type);
12284 -- Now start copying attributes
12286 Set_Has_Discriminants (Full, Has_Discriminants (Priv));
12288 if Has_Discriminants (Full) then
12289 Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
12290 Set_Stored_Constraint (Full, Stored_Constraint (Priv));
12291 end if;
12293 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
12294 Set_Homonym (Full, Homonym (Priv));
12295 Set_Is_Immediately_Visible (Full, Is_Immediately_Visible (Priv));
12296 Set_Is_Public (Full, Is_Public (Priv));
12297 Set_Is_Pure (Full, Is_Pure (Priv));
12298 Set_Is_Tagged_Type (Full, Is_Tagged_Type (Priv));
12299 Set_Has_Pragma_Unmodified (Full, Has_Pragma_Unmodified (Priv));
12300 Set_Has_Pragma_Unreferenced (Full, Has_Pragma_Unreferenced (Priv));
12301 Set_Has_Pragma_Unreferenced_Objects
12302 (Full, Has_Pragma_Unreferenced_Objects
12303 (Priv));
12305 Conditional_Delay (Full, Priv);
12307 if Is_Tagged_Type (Full) then
12308 Set_Direct_Primitive_Operations (Full,
12309 Direct_Primitive_Operations (Priv));
12311 if Is_Base_Type (Priv) then
12312 Set_Class_Wide_Type (Full, Class_Wide_Type (Priv));
12313 end if;
12314 end if;
12316 Set_Is_Volatile (Full, Is_Volatile (Priv));
12317 Set_Treat_As_Volatile (Full, Treat_As_Volatile (Priv));
12318 Set_Scope (Full, Scope (Priv));
12319 Set_Next_Entity (Full, Next_Entity (Priv));
12320 Set_First_Entity (Full, First_Entity (Priv));
12321 Set_Last_Entity (Full, Last_Entity (Priv));
12323 -- If access types have been recorded for later handling, keep them in
12324 -- the full view so that they get handled when the full view freeze
12325 -- node is expanded.
12327 if Present (Freeze_Node (Priv))
12328 and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
12329 then
12330 Ensure_Freeze_Node (Full);
12331 Set_Access_Types_To_Process
12332 (Freeze_Node (Full),
12333 Access_Types_To_Process (Freeze_Node (Priv)));
12334 end if;
12336 -- Swap the two entities. Now Private is the full type entity and Full
12337 -- is the private one. They will be swapped back at the end of the
12338 -- private part. This swapping ensures that the entity that is visible
12339 -- in the private part is the full declaration.
12341 Exchange_Entities (Priv, Full);
12342 Append_Entity (Full, Scope (Full));
12343 end Copy_And_Swap;
12345 -------------------------------------
12346 -- Copy_Array_Base_Type_Attributes --
12347 -------------------------------------
12349 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
12350 begin
12351 Set_Component_Alignment (T1, Component_Alignment (T2));
12352 Set_Component_Type (T1, Component_Type (T2));
12353 Set_Component_Size (T1, Component_Size (T2));
12354 Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
12355 Set_Has_Non_Standard_Rep (T1, Has_Non_Standard_Rep (T2));
12356 Set_Has_Task (T1, Has_Task (T2));
12357 Set_Is_Packed (T1, Is_Packed (T2));
12358 Set_Has_Aliased_Components (T1, Has_Aliased_Components (T2));
12359 Set_Has_Atomic_Components (T1, Has_Atomic_Components (T2));
12360 Set_Has_Volatile_Components (T1, Has_Volatile_Components (T2));
12361 end Copy_Array_Base_Type_Attributes;
12363 -----------------------------------
12364 -- Copy_Array_Subtype_Attributes --
12365 -----------------------------------
12367 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
12368 begin
12369 Set_Size_Info (T1, T2);
12371 Set_First_Index (T1, First_Index (T2));
12372 Set_Is_Aliased (T1, Is_Aliased (T2));
12373 Set_Is_Volatile (T1, Is_Volatile (T2));
12374 Set_Treat_As_Volatile (T1, Treat_As_Volatile (T2));
12375 Set_Is_Constrained (T1, Is_Constrained (T2));
12376 Set_Depends_On_Private (T1, Has_Private_Component (T2));
12377 Set_First_Rep_Item (T1, First_Rep_Item (T2));
12378 Set_Convention (T1, Convention (T2));
12379 Set_Is_Limited_Composite (T1, Is_Limited_Composite (T2));
12380 Set_Is_Private_Composite (T1, Is_Private_Composite (T2));
12381 Set_Packed_Array_Type (T1, Packed_Array_Type (T2));
12382 end Copy_Array_Subtype_Attributes;
12384 -----------------------------------
12385 -- Create_Constrained_Components --
12386 -----------------------------------
12388 procedure Create_Constrained_Components
12389 (Subt : Entity_Id;
12390 Decl_Node : Node_Id;
12391 Typ : Entity_Id;
12392 Constraints : Elist_Id)
12394 Loc : constant Source_Ptr := Sloc (Subt);
12395 Comp_List : constant Elist_Id := New_Elmt_List;
12396 Parent_Type : constant Entity_Id := Etype (Typ);
12397 Assoc_List : constant List_Id := New_List;
12398 Discr_Val : Elmt_Id;
12399 Errors : Boolean;
12400 New_C : Entity_Id;
12401 Old_C : Entity_Id;
12402 Is_Static : Boolean := True;
12404 procedure Collect_Fixed_Components (Typ : Entity_Id);
12405 -- Collect parent type components that do not appear in a variant part
12407 procedure Create_All_Components;
12408 -- Iterate over Comp_List to create the components of the subtype
12410 function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
12411 -- Creates a new component from Old_Compon, copying all the fields from
12412 -- it, including its Etype, inserts the new component in the Subt entity
12413 -- chain and returns the new component.
12415 function Is_Variant_Record (T : Entity_Id) return Boolean;
12416 -- If true, and discriminants are static, collect only components from
12417 -- variants selected by discriminant values.
12419 ------------------------------
12420 -- Collect_Fixed_Components --
12421 ------------------------------
12423 procedure Collect_Fixed_Components (Typ : Entity_Id) is
12424 begin
12425 -- Build association list for discriminants, and find components of the
12426 -- variant part selected by the values of the discriminants.
12428 Old_C := First_Discriminant (Typ);
12429 Discr_Val := First_Elmt (Constraints);
12430 while Present (Old_C) loop
12431 Append_To (Assoc_List,
12432 Make_Component_Association (Loc,
12433 Choices => New_List (New_Occurrence_Of (Old_C, Loc)),
12434 Expression => New_Copy (Node (Discr_Val))));
12436 Next_Elmt (Discr_Val);
12437 Next_Discriminant (Old_C);
12438 end loop;
12440 -- The tag and the possible parent component are unconditionally in
12441 -- the subtype.
12443 if Is_Tagged_Type (Typ)
12444 or else Has_Controlled_Component (Typ)
12445 then
12446 Old_C := First_Component (Typ);
12447 while Present (Old_C) loop
12448 if Nam_In (Chars (Old_C), Name_uTag, Name_uParent) then
12449 Append_Elmt (Old_C, Comp_List);
12450 end if;
12452 Next_Component (Old_C);
12453 end loop;
12454 end if;
12455 end Collect_Fixed_Components;
12457 ---------------------------
12458 -- Create_All_Components --
12459 ---------------------------
12461 procedure Create_All_Components is
12462 Comp : Elmt_Id;
12464 begin
12465 Comp := First_Elmt (Comp_List);
12466 while Present (Comp) loop
12467 Old_C := Node (Comp);
12468 New_C := Create_Component (Old_C);
12470 Set_Etype
12471 (New_C,
12472 Constrain_Component_Type
12473 (Old_C, Subt, Decl_Node, Typ, Constraints));
12474 Set_Is_Public (New_C, Is_Public (Subt));
12476 Next_Elmt (Comp);
12477 end loop;
12478 end Create_All_Components;
12480 ----------------------
12481 -- Create_Component --
12482 ----------------------
12484 function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
12485 New_Compon : constant Entity_Id := New_Copy (Old_Compon);
12487 begin
12488 if Ekind (Old_Compon) = E_Discriminant
12489 and then Is_Completely_Hidden (Old_Compon)
12490 then
12491 -- This is a shadow discriminant created for a discriminant of
12492 -- the parent type, which needs to be present in the subtype.
12493 -- Give the shadow discriminant an internal name that cannot
12494 -- conflict with that of visible components.
12496 Set_Chars (New_Compon, New_Internal_Name ('C'));
12497 end if;
12499 -- Set the parent so we have a proper link for freezing etc. This is
12500 -- not a real parent pointer, since of course our parent does not own
12501 -- up to us and reference us, we are an illegitimate child of the
12502 -- original parent!
12504 Set_Parent (New_Compon, Parent (Old_Compon));
12506 -- If the old component's Esize was already determined and is a
12507 -- static value, then the new component simply inherits it. Otherwise
12508 -- the old component's size may require run-time determination, but
12509 -- the new component's size still might be statically determinable
12510 -- (if, for example it has a static constraint). In that case we want
12511 -- Layout_Type to recompute the component's size, so we reset its
12512 -- size and positional fields.
12514 if Frontend_Layout_On_Target
12515 and then not Known_Static_Esize (Old_Compon)
12516 then
12517 Set_Esize (New_Compon, Uint_0);
12518 Init_Normalized_First_Bit (New_Compon);
12519 Init_Normalized_Position (New_Compon);
12520 Init_Normalized_Position_Max (New_Compon);
12521 end if;
12523 -- We do not want this node marked as Comes_From_Source, since
12524 -- otherwise it would get first class status and a separate cross-
12525 -- reference line would be generated. Illegitimate children do not
12526 -- rate such recognition.
12528 Set_Comes_From_Source (New_Compon, False);
12530 -- But it is a real entity, and a birth certificate must be properly
12531 -- registered by entering it into the entity list.
12533 Enter_Name (New_Compon);
12535 return New_Compon;
12536 end Create_Component;
12538 -----------------------
12539 -- Is_Variant_Record --
12540 -----------------------
12542 function Is_Variant_Record (T : Entity_Id) return Boolean is
12543 begin
12544 return Nkind (Parent (T)) = N_Full_Type_Declaration
12545 and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
12546 and then Present (Component_List (Type_Definition (Parent (T))))
12547 and then
12548 Present
12549 (Variant_Part (Component_List (Type_Definition (Parent (T)))));
12550 end Is_Variant_Record;
12552 -- Start of processing for Create_Constrained_Components
12554 begin
12555 pragma Assert (Subt /= Base_Type (Subt));
12556 pragma Assert (Typ = Base_Type (Typ));
12558 Set_First_Entity (Subt, Empty);
12559 Set_Last_Entity (Subt, Empty);
12561 -- Check whether constraint is fully static, in which case we can
12562 -- optimize the list of components.
12564 Discr_Val := First_Elmt (Constraints);
12565 while Present (Discr_Val) loop
12566 if not Is_OK_Static_Expression (Node (Discr_Val)) then
12567 Is_Static := False;
12568 exit;
12569 end if;
12571 Next_Elmt (Discr_Val);
12572 end loop;
12574 Set_Has_Static_Discriminants (Subt, Is_Static);
12576 Push_Scope (Subt);
12578 -- Inherit the discriminants of the parent type
12580 Add_Discriminants : declare
12581 Num_Disc : Int;
12582 Num_Gird : Int;
12584 begin
12585 Num_Disc := 0;
12586 Old_C := First_Discriminant (Typ);
12588 while Present (Old_C) loop
12589 Num_Disc := Num_Disc + 1;
12590 New_C := Create_Component (Old_C);
12591 Set_Is_Public (New_C, Is_Public (Subt));
12592 Next_Discriminant (Old_C);
12593 end loop;
12595 -- For an untagged derived subtype, the number of discriminants may
12596 -- be smaller than the number of inherited discriminants, because
12597 -- several of them may be renamed by a single new discriminant or
12598 -- constrained. In this case, add the hidden discriminants back into
12599 -- the subtype, because they need to be present if the optimizer of
12600 -- the GCC 4.x back-end decides to break apart assignments between
12601 -- objects using the parent view into member-wise assignments.
12603 Num_Gird := 0;
12605 if Is_Derived_Type (Typ)
12606 and then not Is_Tagged_Type (Typ)
12607 then
12608 Old_C := First_Stored_Discriminant (Typ);
12610 while Present (Old_C) loop
12611 Num_Gird := Num_Gird + 1;
12612 Next_Stored_Discriminant (Old_C);
12613 end loop;
12614 end if;
12616 if Num_Gird > Num_Disc then
12618 -- Find out multiple uses of new discriminants, and add hidden
12619 -- components for the extra renamed discriminants. We recognize
12620 -- multiple uses through the Corresponding_Discriminant of a
12621 -- new discriminant: if it constrains several old discriminants,
12622 -- this field points to the last one in the parent type. The
12623 -- stored discriminants of the derived type have the same name
12624 -- as those of the parent.
12626 declare
12627 Constr : Elmt_Id;
12628 New_Discr : Entity_Id;
12629 Old_Discr : Entity_Id;
12631 begin
12632 Constr := First_Elmt (Stored_Constraint (Typ));
12633 Old_Discr := First_Stored_Discriminant (Typ);
12634 while Present (Constr) loop
12635 if Is_Entity_Name (Node (Constr))
12636 and then Ekind (Entity (Node (Constr))) = E_Discriminant
12637 then
12638 New_Discr := Entity (Node (Constr));
12640 if Chars (Corresponding_Discriminant (New_Discr)) /=
12641 Chars (Old_Discr)
12642 then
12643 -- The new discriminant has been used to rename a
12644 -- subsequent old discriminant. Introduce a shadow
12645 -- component for the current old discriminant.
12647 New_C := Create_Component (Old_Discr);
12648 Set_Original_Record_Component (New_C, Old_Discr);
12649 end if;
12651 else
12652 -- The constraint has eliminated the old discriminant.
12653 -- Introduce a shadow component.
12655 New_C := Create_Component (Old_Discr);
12656 Set_Original_Record_Component (New_C, Old_Discr);
12657 end if;
12659 Next_Elmt (Constr);
12660 Next_Stored_Discriminant (Old_Discr);
12661 end loop;
12662 end;
12663 end if;
12664 end Add_Discriminants;
12666 if Is_Static
12667 and then Is_Variant_Record (Typ)
12668 then
12669 Collect_Fixed_Components (Typ);
12671 Gather_Components (
12672 Typ,
12673 Component_List (Type_Definition (Parent (Typ))),
12674 Governed_By => Assoc_List,
12675 Into => Comp_List,
12676 Report_Errors => Errors);
12677 pragma Assert (not Errors);
12679 Create_All_Components;
12681 -- If the subtype declaration is created for a tagged type derivation
12682 -- with constraints, we retrieve the record definition of the parent
12683 -- type to select the components of the proper variant.
12685 elsif Is_Static
12686 and then Is_Tagged_Type (Typ)
12687 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
12688 and then
12689 Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
12690 and then Is_Variant_Record (Parent_Type)
12691 then
12692 Collect_Fixed_Components (Typ);
12694 Gather_Components (
12695 Typ,
12696 Component_List (Type_Definition (Parent (Parent_Type))),
12697 Governed_By => Assoc_List,
12698 Into => Comp_List,
12699 Report_Errors => Errors);
12700 pragma Assert (not Errors);
12702 -- If the tagged derivation has a type extension, collect all the
12703 -- new components therein.
12705 if Present
12706 (Record_Extension_Part (Type_Definition (Parent (Typ))))
12707 then
12708 Old_C := First_Component (Typ);
12709 while Present (Old_C) loop
12710 if Original_Record_Component (Old_C) = Old_C
12711 and then Chars (Old_C) /= Name_uTag
12712 and then Chars (Old_C) /= Name_uParent
12713 then
12714 Append_Elmt (Old_C, Comp_List);
12715 end if;
12717 Next_Component (Old_C);
12718 end loop;
12719 end if;
12721 Create_All_Components;
12723 else
12724 -- If discriminants are not static, or if this is a multi-level type
12725 -- extension, we have to include all components of the parent type.
12727 Old_C := First_Component (Typ);
12728 while Present (Old_C) loop
12729 New_C := Create_Component (Old_C);
12731 Set_Etype
12732 (New_C,
12733 Constrain_Component_Type
12734 (Old_C, Subt, Decl_Node, Typ, Constraints));
12735 Set_Is_Public (New_C, Is_Public (Subt));
12737 Next_Component (Old_C);
12738 end loop;
12739 end if;
12741 End_Scope;
12742 end Create_Constrained_Components;
12744 ------------------------------------------
12745 -- Decimal_Fixed_Point_Type_Declaration --
12746 ------------------------------------------
12748 procedure Decimal_Fixed_Point_Type_Declaration
12749 (T : Entity_Id;
12750 Def : Node_Id)
12752 Loc : constant Source_Ptr := Sloc (Def);
12753 Digs_Expr : constant Node_Id := Digits_Expression (Def);
12754 Delta_Expr : constant Node_Id := Delta_Expression (Def);
12755 Implicit_Base : Entity_Id;
12756 Digs_Val : Uint;
12757 Delta_Val : Ureal;
12758 Scale_Val : Uint;
12759 Bound_Val : Ureal;
12761 begin
12762 Check_SPARK_Restriction
12763 ("decimal fixed point type is not allowed", Def);
12764 Check_Restriction (No_Fixed_Point, Def);
12766 -- Create implicit base type
12768 Implicit_Base :=
12769 Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
12770 Set_Etype (Implicit_Base, Implicit_Base);
12772 -- Analyze and process delta expression
12774 Analyze_And_Resolve (Delta_Expr, Universal_Real);
12776 Check_Delta_Expression (Delta_Expr);
12777 Delta_Val := Expr_Value_R (Delta_Expr);
12779 -- Check delta is power of 10, and determine scale value from it
12781 declare
12782 Val : Ureal;
12784 begin
12785 Scale_Val := Uint_0;
12786 Val := Delta_Val;
12788 if Val < Ureal_1 then
12789 while Val < Ureal_1 loop
12790 Val := Val * Ureal_10;
12791 Scale_Val := Scale_Val + 1;
12792 end loop;
12794 if Scale_Val > 18 then
12795 Error_Msg_N ("scale exceeds maximum value of 18", Def);
12796 Scale_Val := UI_From_Int (+18);
12797 end if;
12799 else
12800 while Val > Ureal_1 loop
12801 Val := Val / Ureal_10;
12802 Scale_Val := Scale_Val - 1;
12803 end loop;
12805 if Scale_Val < -18 then
12806 Error_Msg_N ("scale is less than minimum value of -18", Def);
12807 Scale_Val := UI_From_Int (-18);
12808 end if;
12809 end if;
12811 if Val /= Ureal_1 then
12812 Error_Msg_N ("delta expression must be a power of 10", Def);
12813 Delta_Val := Ureal_10 ** (-Scale_Val);
12814 end if;
12815 end;
12817 -- Set delta, scale and small (small = delta for decimal type)
12819 Set_Delta_Value (Implicit_Base, Delta_Val);
12820 Set_Scale_Value (Implicit_Base, Scale_Val);
12821 Set_Small_Value (Implicit_Base, Delta_Val);
12823 -- Analyze and process digits expression
12825 Analyze_And_Resolve (Digs_Expr, Any_Integer);
12826 Check_Digits_Expression (Digs_Expr);
12827 Digs_Val := Expr_Value (Digs_Expr);
12829 if Digs_Val > 18 then
12830 Digs_Val := UI_From_Int (+18);
12831 Error_Msg_N ("digits value out of range, maximum is 18", Digs_Expr);
12832 end if;
12834 Set_Digits_Value (Implicit_Base, Digs_Val);
12835 Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
12837 -- Set range of base type from digits value for now. This will be
12838 -- expanded to represent the true underlying base range by Freeze.
12840 Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
12842 -- Note: We leave size as zero for now, size will be set at freeze
12843 -- time. We have to do this for ordinary fixed-point, because the size
12844 -- depends on the specified small, and we might as well do the same for
12845 -- decimal fixed-point.
12847 pragma Assert (Esize (Implicit_Base) = Uint_0);
12849 -- If there are bounds given in the declaration use them as the
12850 -- bounds of the first named subtype.
12852 if Present (Real_Range_Specification (Def)) then
12853 declare
12854 RRS : constant Node_Id := Real_Range_Specification (Def);
12855 Low : constant Node_Id := Low_Bound (RRS);
12856 High : constant Node_Id := High_Bound (RRS);
12857 Low_Val : Ureal;
12858 High_Val : Ureal;
12860 begin
12861 Analyze_And_Resolve (Low, Any_Real);
12862 Analyze_And_Resolve (High, Any_Real);
12863 Check_Real_Bound (Low);
12864 Check_Real_Bound (High);
12865 Low_Val := Expr_Value_R (Low);
12866 High_Val := Expr_Value_R (High);
12868 if Low_Val < (-Bound_Val) then
12869 Error_Msg_N
12870 ("range low bound too small for digits value", Low);
12871 Low_Val := -Bound_Val;
12872 end if;
12874 if High_Val > Bound_Val then
12875 Error_Msg_N
12876 ("range high bound too large for digits value", High);
12877 High_Val := Bound_Val;
12878 end if;
12880 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
12881 end;
12883 -- If no explicit range, use range that corresponds to given
12884 -- digits value. This will end up as the final range for the
12885 -- first subtype.
12887 else
12888 Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
12889 end if;
12891 -- Complete entity for first subtype
12893 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
12894 Set_Etype (T, Implicit_Base);
12895 Set_Size_Info (T, Implicit_Base);
12896 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
12897 Set_Digits_Value (T, Digs_Val);
12898 Set_Delta_Value (T, Delta_Val);
12899 Set_Small_Value (T, Delta_Val);
12900 Set_Scale_Value (T, Scale_Val);
12901 Set_Is_Constrained (T);
12902 end Decimal_Fixed_Point_Type_Declaration;
12904 -----------------------------------
12905 -- Derive_Progenitor_Subprograms --
12906 -----------------------------------
12908 procedure Derive_Progenitor_Subprograms
12909 (Parent_Type : Entity_Id;
12910 Tagged_Type : Entity_Id)
12912 E : Entity_Id;
12913 Elmt : Elmt_Id;
12914 Iface : Entity_Id;
12915 Iface_Elmt : Elmt_Id;
12916 Iface_Subp : Entity_Id;
12917 New_Subp : Entity_Id := Empty;
12918 Prim_Elmt : Elmt_Id;
12919 Subp : Entity_Id;
12920 Typ : Entity_Id;
12922 begin
12923 pragma Assert (Ada_Version >= Ada_2005
12924 and then Is_Record_Type (Tagged_Type)
12925 and then Is_Tagged_Type (Tagged_Type)
12926 and then Has_Interfaces (Tagged_Type));
12928 -- Step 1: Transfer to the full-view primitives associated with the
12929 -- partial-view that cover interface primitives. Conceptually this
12930 -- work should be done later by Process_Full_View; done here to
12931 -- simplify its implementation at later stages. It can be safely
12932 -- done here because interfaces must be visible in the partial and
12933 -- private view (RM 7.3(7.3/2)).
12935 -- Small optimization: This work is only required if the parent may
12936 -- have entities whose Alias attribute reference an interface primitive.
12937 -- Such a situation may occur if the parent is an abstract type and the
12938 -- primitive has not been yet overridden or if the parent is a generic
12939 -- formal type covering interfaces.
12941 -- If the tagged type is not abstract, it cannot have abstract
12942 -- primitives (the only entities in the list of primitives of
12943 -- non-abstract tagged types that can reference abstract primitives
12944 -- through its Alias attribute are the internal entities that have
12945 -- attribute Interface_Alias, and these entities are generated later
12946 -- by Add_Internal_Interface_Entities).
12948 if In_Private_Part (Current_Scope)
12949 and then (Is_Abstract_Type (Parent_Type)
12950 or else
12951 Is_Generic_Type (Parent_Type))
12952 then
12953 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
12954 while Present (Elmt) loop
12955 Subp := Node (Elmt);
12957 -- At this stage it is not possible to have entities in the list
12958 -- of primitives that have attribute Interface_Alias.
12960 pragma Assert (No (Interface_Alias (Subp)));
12962 Typ := Find_Dispatching_Type (Ultimate_Alias (Subp));
12964 if Is_Interface (Typ) then
12965 E := Find_Primitive_Covering_Interface
12966 (Tagged_Type => Tagged_Type,
12967 Iface_Prim => Subp);
12969 if Present (E)
12970 and then Find_Dispatching_Type (Ultimate_Alias (E)) /= Typ
12971 then
12972 Replace_Elmt (Elmt, E);
12973 Remove_Homonym (Subp);
12974 end if;
12975 end if;
12977 Next_Elmt (Elmt);
12978 end loop;
12979 end if;
12981 -- Step 2: Add primitives of progenitors that are not implemented by
12982 -- parents of Tagged_Type.
12984 if Present (Interfaces (Base_Type (Tagged_Type))) then
12985 Iface_Elmt := First_Elmt (Interfaces (Base_Type (Tagged_Type)));
12986 while Present (Iface_Elmt) loop
12987 Iface := Node (Iface_Elmt);
12989 Prim_Elmt := First_Elmt (Primitive_Operations (Iface));
12990 while Present (Prim_Elmt) loop
12991 Iface_Subp := Node (Prim_Elmt);
12993 -- Exclude derivation of predefined primitives except those
12994 -- that come from source, or are inherited from one that comes
12995 -- from source. Required to catch declarations of equality
12996 -- operators of interfaces. For example:
12998 -- type Iface is interface;
12999 -- function "=" (Left, Right : Iface) return Boolean;
13001 if not Is_Predefined_Dispatching_Operation (Iface_Subp)
13002 or else Comes_From_Source (Ultimate_Alias (Iface_Subp))
13003 then
13004 E := Find_Primitive_Covering_Interface
13005 (Tagged_Type => Tagged_Type,
13006 Iface_Prim => Iface_Subp);
13008 -- If not found we derive a new primitive leaving its alias
13009 -- attribute referencing the interface primitive.
13011 if No (E) then
13012 Derive_Subprogram
13013 (New_Subp, Iface_Subp, Tagged_Type, Iface);
13015 -- Ada 2012 (AI05-0197): If the covering primitive's name
13016 -- differs from the name of the interface primitive then it
13017 -- is a private primitive inherited from a parent type. In
13018 -- such case, given that Tagged_Type covers the interface,
13019 -- the inherited private primitive becomes visible. For such
13020 -- purpose we add a new entity that renames the inherited
13021 -- private primitive.
13023 elsif Chars (E) /= Chars (Iface_Subp) then
13024 pragma Assert (Has_Suffix (E, 'P'));
13025 Derive_Subprogram
13026 (New_Subp, Iface_Subp, Tagged_Type, Iface);
13027 Set_Alias (New_Subp, E);
13028 Set_Is_Abstract_Subprogram (New_Subp,
13029 Is_Abstract_Subprogram (E));
13031 -- Propagate to the full view interface entities associated
13032 -- with the partial view.
13034 elsif In_Private_Part (Current_Scope)
13035 and then Present (Alias (E))
13036 and then Alias (E) = Iface_Subp
13037 and then
13038 List_Containing (Parent (E)) /=
13039 Private_Declarations
13040 (Specification
13041 (Unit_Declaration_Node (Current_Scope)))
13042 then
13043 Append_Elmt (E, Primitive_Operations (Tagged_Type));
13044 end if;
13045 end if;
13047 Next_Elmt (Prim_Elmt);
13048 end loop;
13050 Next_Elmt (Iface_Elmt);
13051 end loop;
13052 end if;
13053 end Derive_Progenitor_Subprograms;
13055 -----------------------
13056 -- Derive_Subprogram --
13057 -----------------------
13059 procedure Derive_Subprogram
13060 (New_Subp : in out Entity_Id;
13061 Parent_Subp : Entity_Id;
13062 Derived_Type : Entity_Id;
13063 Parent_Type : Entity_Id;
13064 Actual_Subp : Entity_Id := Empty)
13066 Formal : Entity_Id;
13067 -- Formal parameter of parent primitive operation
13069 Formal_Of_Actual : Entity_Id;
13070 -- Formal parameter of actual operation, when the derivation is to
13071 -- create a renaming for a primitive operation of an actual in an
13072 -- instantiation.
13074 New_Formal : Entity_Id;
13075 -- Formal of inherited operation
13077 Visible_Subp : Entity_Id := Parent_Subp;
13079 function Is_Private_Overriding return Boolean;
13080 -- If Subp is a private overriding of a visible operation, the inherited
13081 -- operation derives from the overridden op (even though its body is the
13082 -- overriding one) and the inherited operation is visible now. See
13083 -- sem_disp to see the full details of the handling of the overridden
13084 -- subprogram, which is removed from the list of primitive operations of
13085 -- the type. The overridden subprogram is saved locally in Visible_Subp,
13086 -- and used to diagnose abstract operations that need overriding in the
13087 -- derived type.
13089 procedure Replace_Type (Id, New_Id : Entity_Id);
13090 -- When the type is an anonymous access type, create a new access type
13091 -- designating the derived type.
13093 procedure Set_Derived_Name;
13094 -- This procedure sets the appropriate Chars name for New_Subp. This
13095 -- is normally just a copy of the parent name. An exception arises for
13096 -- type support subprograms, where the name is changed to reflect the
13097 -- name of the derived type, e.g. if type foo is derived from type bar,
13098 -- then a procedure barDA is derived with a name fooDA.
13100 ---------------------------
13101 -- Is_Private_Overriding --
13102 ---------------------------
13104 function Is_Private_Overriding return Boolean is
13105 Prev : Entity_Id;
13107 begin
13108 -- If the parent is not a dispatching operation there is no
13109 -- need to investigate overridings
13111 if not Is_Dispatching_Operation (Parent_Subp) then
13112 return False;
13113 end if;
13115 -- The visible operation that is overridden is a homonym of the
13116 -- parent subprogram. We scan the homonym chain to find the one
13117 -- whose alias is the subprogram we are deriving.
13119 Prev := Current_Entity (Parent_Subp);
13120 while Present (Prev) loop
13121 if Ekind (Prev) = Ekind (Parent_Subp)
13122 and then Alias (Prev) = Parent_Subp
13123 and then Scope (Parent_Subp) = Scope (Prev)
13124 and then not Is_Hidden (Prev)
13125 then
13126 Visible_Subp := Prev;
13127 return True;
13128 end if;
13130 Prev := Homonym (Prev);
13131 end loop;
13133 return False;
13134 end Is_Private_Overriding;
13136 ------------------
13137 -- Replace_Type --
13138 ------------------
13140 procedure Replace_Type (Id, New_Id : Entity_Id) is
13141 Acc_Type : Entity_Id;
13142 Par : constant Node_Id := Parent (Derived_Type);
13144 begin
13145 -- When the type is an anonymous access type, create a new access
13146 -- type designating the derived type. This itype must be elaborated
13147 -- at the point of the derivation, not on subsequent calls that may
13148 -- be out of the proper scope for Gigi, so we insert a reference to
13149 -- it after the derivation.
13151 if Ekind (Etype (Id)) = E_Anonymous_Access_Type then
13152 declare
13153 Desig_Typ : Entity_Id := Designated_Type (Etype (Id));
13155 begin
13156 if Ekind (Desig_Typ) = E_Record_Type_With_Private
13157 and then Present (Full_View (Desig_Typ))
13158 and then not Is_Private_Type (Parent_Type)
13159 then
13160 Desig_Typ := Full_View (Desig_Typ);
13161 end if;
13163 if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
13165 -- Ada 2005 (AI-251): Handle also derivations of abstract
13166 -- interface primitives.
13168 or else (Is_Interface (Desig_Typ)
13169 and then not Is_Class_Wide_Type (Desig_Typ))
13170 then
13171 Acc_Type := New_Copy (Etype (Id));
13172 Set_Etype (Acc_Type, Acc_Type);
13173 Set_Scope (Acc_Type, New_Subp);
13175 -- Compute size of anonymous access type
13177 if Is_Array_Type (Desig_Typ)
13178 and then not Is_Constrained (Desig_Typ)
13179 then
13180 Init_Size (Acc_Type, 2 * System_Address_Size);
13181 else
13182 Init_Size (Acc_Type, System_Address_Size);
13183 end if;
13185 Init_Alignment (Acc_Type);
13186 Set_Directly_Designated_Type (Acc_Type, Derived_Type);
13188 Set_Etype (New_Id, Acc_Type);
13189 Set_Scope (New_Id, New_Subp);
13191 -- Create a reference to it
13192 Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
13194 else
13195 Set_Etype (New_Id, Etype (Id));
13196 end if;
13197 end;
13199 elsif Base_Type (Etype (Id)) = Base_Type (Parent_Type)
13200 or else
13201 (Ekind (Etype (Id)) = E_Record_Type_With_Private
13202 and then Present (Full_View (Etype (Id)))
13203 and then
13204 Base_Type (Full_View (Etype (Id))) = Base_Type (Parent_Type))
13205 then
13206 -- Constraint checks on formals are generated during expansion,
13207 -- based on the signature of the original subprogram. The bounds
13208 -- of the derived type are not relevant, and thus we can use
13209 -- the base type for the formals. However, the return type may be
13210 -- used in a context that requires that the proper static bounds
13211 -- be used (a case statement, for example) and for those cases
13212 -- we must use the derived type (first subtype), not its base.
13214 -- If the derived_type_definition has no constraints, we know that
13215 -- the derived type has the same constraints as the first subtype
13216 -- of the parent, and we can also use it rather than its base,
13217 -- which can lead to more efficient code.
13219 if Etype (Id) = Parent_Type then
13220 if Is_Scalar_Type (Parent_Type)
13221 and then
13222 Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
13223 then
13224 Set_Etype (New_Id, Derived_Type);
13226 elsif Nkind (Par) = N_Full_Type_Declaration
13227 and then
13228 Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
13229 and then
13230 Is_Entity_Name
13231 (Subtype_Indication (Type_Definition (Par)))
13232 then
13233 Set_Etype (New_Id, Derived_Type);
13235 else
13236 Set_Etype (New_Id, Base_Type (Derived_Type));
13237 end if;
13239 else
13240 Set_Etype (New_Id, Base_Type (Derived_Type));
13241 end if;
13243 else
13244 Set_Etype (New_Id, Etype (Id));
13245 end if;
13246 end Replace_Type;
13248 ----------------------
13249 -- Set_Derived_Name --
13250 ----------------------
13252 procedure Set_Derived_Name is
13253 Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
13254 begin
13255 if Nm = TSS_Null then
13256 Set_Chars (New_Subp, Chars (Parent_Subp));
13257 else
13258 Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
13259 end if;
13260 end Set_Derived_Name;
13262 -- Start of processing for Derive_Subprogram
13264 begin
13265 New_Subp :=
13266 New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
13267 Set_Ekind (New_Subp, Ekind (Parent_Subp));
13268 Set_Contract (New_Subp, Make_Contract (Sloc (New_Subp)));
13270 -- Check whether the inherited subprogram is a private operation that
13271 -- should be inherited but not yet made visible. Such subprograms can
13272 -- become visible at a later point (e.g., the private part of a public
13273 -- child unit) via Declare_Inherited_Private_Subprograms. If the
13274 -- following predicate is true, then this is not such a private
13275 -- operation and the subprogram simply inherits the name of the parent
13276 -- subprogram. Note the special check for the names of controlled
13277 -- operations, which are currently exempted from being inherited with
13278 -- a hidden name because they must be findable for generation of
13279 -- implicit run-time calls.
13281 if not Is_Hidden (Parent_Subp)
13282 or else Is_Internal (Parent_Subp)
13283 or else Is_Private_Overriding
13284 or else Is_Internal_Name (Chars (Parent_Subp))
13285 or else Nam_In (Chars (Parent_Subp), Name_Initialize,
13286 Name_Adjust,
13287 Name_Finalize)
13288 then
13289 Set_Derived_Name;
13291 -- An inherited dispatching equality will be overridden by an internally
13292 -- generated one, or by an explicit one, so preserve its name and thus
13293 -- its entry in the dispatch table. Otherwise, if Parent_Subp is a
13294 -- private operation it may become invisible if the full view has
13295 -- progenitors, and the dispatch table will be malformed.
13296 -- We check that the type is limited to handle the anomalous declaration
13297 -- of Limited_Controlled, which is derived from a non-limited type, and
13298 -- which is handled specially elsewhere as well.
13300 elsif Chars (Parent_Subp) = Name_Op_Eq
13301 and then Is_Dispatching_Operation (Parent_Subp)
13302 and then Etype (Parent_Subp) = Standard_Boolean
13303 and then not Is_Limited_Type (Etype (First_Formal (Parent_Subp)))
13304 and then
13305 Etype (First_Formal (Parent_Subp)) =
13306 Etype (Next_Formal (First_Formal (Parent_Subp)))
13307 then
13308 Set_Derived_Name;
13310 -- If parent is hidden, this can be a regular derivation if the
13311 -- parent is immediately visible in a non-instantiating context,
13312 -- or if we are in the private part of an instance. This test
13313 -- should still be refined ???
13315 -- The test for In_Instance_Not_Visible avoids inheriting the derived
13316 -- operation as a non-visible operation in cases where the parent
13317 -- subprogram might not be visible now, but was visible within the
13318 -- original generic, so it would be wrong to make the inherited
13319 -- subprogram non-visible now. (Not clear if this test is fully
13320 -- correct; are there any cases where we should declare the inherited
13321 -- operation as not visible to avoid it being overridden, e.g., when
13322 -- the parent type is a generic actual with private primitives ???)
13324 -- (they should be treated the same as other private inherited
13325 -- subprograms, but it's not clear how to do this cleanly). ???
13327 elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
13328 and then Is_Immediately_Visible (Parent_Subp)
13329 and then not In_Instance)
13330 or else In_Instance_Not_Visible
13331 then
13332 Set_Derived_Name;
13334 -- Ada 2005 (AI-251): Regular derivation if the parent subprogram
13335 -- overrides an interface primitive because interface primitives
13336 -- must be visible in the partial view of the parent (RM 7.3 (7.3/2))
13338 elsif Ada_Version >= Ada_2005
13339 and then Is_Dispatching_Operation (Parent_Subp)
13340 and then Covers_Some_Interface (Parent_Subp)
13341 then
13342 Set_Derived_Name;
13344 -- Otherwise, the type is inheriting a private operation, so enter
13345 -- it with a special name so it can't be overridden.
13347 else
13348 Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
13349 end if;
13351 Set_Parent (New_Subp, Parent (Derived_Type));
13353 if Present (Actual_Subp) then
13354 Replace_Type (Actual_Subp, New_Subp);
13355 else
13356 Replace_Type (Parent_Subp, New_Subp);
13357 end if;
13359 Conditional_Delay (New_Subp, Parent_Subp);
13361 -- If we are creating a renaming for a primitive operation of an
13362 -- actual of a generic derived type, we must examine the signature
13363 -- of the actual primitive, not that of the generic formal, which for
13364 -- example may be an interface. However the name and initial value
13365 -- of the inherited operation are those of the formal primitive.
13367 Formal := First_Formal (Parent_Subp);
13369 if Present (Actual_Subp) then
13370 Formal_Of_Actual := First_Formal (Actual_Subp);
13371 else
13372 Formal_Of_Actual := Empty;
13373 end if;
13375 while Present (Formal) loop
13376 New_Formal := New_Copy (Formal);
13378 -- Normally we do not go copying parents, but in the case of
13379 -- formals, we need to link up to the declaration (which is the
13380 -- parameter specification), and it is fine to link up to the
13381 -- original formal's parameter specification in this case.
13383 Set_Parent (New_Formal, Parent (Formal));
13384 Append_Entity (New_Formal, New_Subp);
13386 if Present (Formal_Of_Actual) then
13387 Replace_Type (Formal_Of_Actual, New_Formal);
13388 Next_Formal (Formal_Of_Actual);
13389 else
13390 Replace_Type (Formal, New_Formal);
13391 end if;
13393 Next_Formal (Formal);
13394 end loop;
13396 -- If this derivation corresponds to a tagged generic actual, then
13397 -- primitive operations rename those of the actual. Otherwise the
13398 -- primitive operations rename those of the parent type, If the parent
13399 -- renames an intrinsic operator, so does the new subprogram. We except
13400 -- concatenation, which is always properly typed, and does not get
13401 -- expanded as other intrinsic operations.
13403 if No (Actual_Subp) then
13404 if Is_Intrinsic_Subprogram (Parent_Subp) then
13405 Set_Is_Intrinsic_Subprogram (New_Subp);
13407 if Present (Alias (Parent_Subp))
13408 and then Chars (Parent_Subp) /= Name_Op_Concat
13409 then
13410 Set_Alias (New_Subp, Alias (Parent_Subp));
13411 else
13412 Set_Alias (New_Subp, Parent_Subp);
13413 end if;
13415 else
13416 Set_Alias (New_Subp, Parent_Subp);
13417 end if;
13419 else
13420 Set_Alias (New_Subp, Actual_Subp);
13421 end if;
13423 -- Derived subprograms of a tagged type must inherit the convention
13424 -- of the parent subprogram (a requirement of AI-117). Derived
13425 -- subprograms of untagged types simply get convention Ada by default.
13427 -- If the derived type is a tagged generic formal type with unknown
13428 -- discriminants, its convention is intrinsic (RM 6.3.1 (8)).
13430 -- However, if the type is derived from a generic formal, the further
13431 -- inherited subprogram has the convention of the non-generic ancestor.
13432 -- Otherwise there would be no way to override the operation.
13433 -- (This is subject to forthcoming ARG discussions).
13435 if Is_Tagged_Type (Derived_Type) then
13436 if Is_Generic_Type (Derived_Type)
13437 and then Has_Unknown_Discriminants (Derived_Type)
13438 then
13439 Set_Convention (New_Subp, Convention_Intrinsic);
13441 else
13442 if Is_Generic_Type (Parent_Type)
13443 and then Has_Unknown_Discriminants (Parent_Type)
13444 then
13445 Set_Convention (New_Subp, Convention (Alias (Parent_Subp)));
13446 else
13447 Set_Convention (New_Subp, Convention (Parent_Subp));
13448 end if;
13449 end if;
13450 end if;
13452 -- Predefined controlled operations retain their name even if the parent
13453 -- is hidden (see above), but they are not primitive operations if the
13454 -- ancestor is not visible, for example if the parent is a private
13455 -- extension completed with a controlled extension. Note that a full
13456 -- type that is controlled can break privacy: the flag Is_Controlled is
13457 -- set on both views of the type.
13459 if Is_Controlled (Parent_Type)
13460 and then Nam_In (Chars (Parent_Subp), Name_Initialize,
13461 Name_Adjust,
13462 Name_Finalize)
13463 and then Is_Hidden (Parent_Subp)
13464 and then not Is_Visibly_Controlled (Parent_Type)
13465 then
13466 Set_Is_Hidden (New_Subp);
13467 end if;
13469 Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
13470 Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
13472 if Ekind (Parent_Subp) = E_Procedure then
13473 Set_Is_Valued_Procedure
13474 (New_Subp, Is_Valued_Procedure (Parent_Subp));
13475 else
13476 Set_Has_Controlling_Result
13477 (New_Subp, Has_Controlling_Result (Parent_Subp));
13478 end if;
13480 -- No_Return must be inherited properly. If this is overridden in the
13481 -- case of a dispatching operation, then a check is made in Sem_Disp
13482 -- that the overriding operation is also No_Return (no such check is
13483 -- required for the case of non-dispatching operation.
13485 Set_No_Return (New_Subp, No_Return (Parent_Subp));
13487 -- A derived function with a controlling result is abstract. If the
13488 -- Derived_Type is a nonabstract formal generic derived type, then
13489 -- inherited operations are not abstract: the required check is done at
13490 -- instantiation time. If the derivation is for a generic actual, the
13491 -- function is not abstract unless the actual is.
13493 if Is_Generic_Type (Derived_Type)
13494 and then not Is_Abstract_Type (Derived_Type)
13495 then
13496 null;
13498 -- Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
13499 -- properties of the subprogram, as defined in RM-3.9.3(4/2-6/2).
13501 elsif Ada_Version >= Ada_2005
13502 and then (Is_Abstract_Subprogram (Alias (New_Subp))
13503 or else (Is_Tagged_Type (Derived_Type)
13504 and then Etype (New_Subp) = Derived_Type
13505 and then not Is_Null_Extension (Derived_Type))
13506 or else (Is_Tagged_Type (Derived_Type)
13507 and then Ekind (Etype (New_Subp)) =
13508 E_Anonymous_Access_Type
13509 and then Designated_Type (Etype (New_Subp)) =
13510 Derived_Type
13511 and then not Is_Null_Extension (Derived_Type)))
13512 and then No (Actual_Subp)
13513 then
13514 if not Is_Tagged_Type (Derived_Type)
13515 or else Is_Abstract_Type (Derived_Type)
13516 or else Is_Abstract_Subprogram (Alias (New_Subp))
13517 then
13518 Set_Is_Abstract_Subprogram (New_Subp);
13519 else
13520 Set_Requires_Overriding (New_Subp);
13521 end if;
13523 elsif Ada_Version < Ada_2005
13524 and then (Is_Abstract_Subprogram (Alias (New_Subp))
13525 or else (Is_Tagged_Type (Derived_Type)
13526 and then Etype (New_Subp) = Derived_Type
13527 and then No (Actual_Subp)))
13528 then
13529 Set_Is_Abstract_Subprogram (New_Subp);
13531 -- AI05-0097 : an inherited operation that dispatches on result is
13532 -- abstract if the derived type is abstract, even if the parent type
13533 -- is concrete and the derived type is a null extension.
13535 elsif Has_Controlling_Result (Alias (New_Subp))
13536 and then Is_Abstract_Type (Etype (New_Subp))
13537 then
13538 Set_Is_Abstract_Subprogram (New_Subp);
13540 -- Finally, if the parent type is abstract we must verify that all
13541 -- inherited operations are either non-abstract or overridden, or that
13542 -- the derived type itself is abstract (this check is performed at the
13543 -- end of a package declaration, in Check_Abstract_Overriding). A
13544 -- private overriding in the parent type will not be visible in the
13545 -- derivation if we are not in an inner package or in a child unit of
13546 -- the parent type, in which case the abstractness of the inherited
13547 -- operation is carried to the new subprogram.
13549 elsif Is_Abstract_Type (Parent_Type)
13550 and then not In_Open_Scopes (Scope (Parent_Type))
13551 and then Is_Private_Overriding
13552 and then Is_Abstract_Subprogram (Visible_Subp)
13553 then
13554 if No (Actual_Subp) then
13555 Set_Alias (New_Subp, Visible_Subp);
13556 Set_Is_Abstract_Subprogram (New_Subp, True);
13558 else
13559 -- If this is a derivation for an instance of a formal derived
13560 -- type, abstractness comes from the primitive operation of the
13561 -- actual, not from the operation inherited from the ancestor.
13563 Set_Is_Abstract_Subprogram
13564 (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
13565 end if;
13566 end if;
13568 New_Overloaded_Entity (New_Subp, Derived_Type);
13570 -- Check for case of a derived subprogram for the instantiation of a
13571 -- formal derived tagged type, if so mark the subprogram as dispatching
13572 -- and inherit the dispatching attributes of the actual subprogram. The
13573 -- derived subprogram is effectively renaming of the actual subprogram,
13574 -- so it needs to have the same attributes as the actual.
13576 if Present (Actual_Subp)
13577 and then Is_Dispatching_Operation (Actual_Subp)
13578 then
13579 Set_Is_Dispatching_Operation (New_Subp);
13581 if Present (DTC_Entity (Actual_Subp)) then
13582 Set_DTC_Entity (New_Subp, DTC_Entity (Actual_Subp));
13583 Set_DT_Position (New_Subp, DT_Position (Actual_Subp));
13584 end if;
13585 end if;
13587 -- Indicate that a derived subprogram does not require a body and that
13588 -- it does not require processing of default expressions.
13590 Set_Has_Completion (New_Subp);
13591 Set_Default_Expressions_Processed (New_Subp);
13593 if Ekind (New_Subp) = E_Function then
13594 Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
13595 end if;
13596 end Derive_Subprogram;
13598 ------------------------
13599 -- Derive_Subprograms --
13600 ------------------------
13602 procedure Derive_Subprograms
13603 (Parent_Type : Entity_Id;
13604 Derived_Type : Entity_Id;
13605 Generic_Actual : Entity_Id := Empty)
13607 Op_List : constant Elist_Id :=
13608 Collect_Primitive_Operations (Parent_Type);
13610 function Check_Derived_Type return Boolean;
13611 -- Check that all the entities derived from Parent_Type are found in
13612 -- the list of primitives of Derived_Type exactly in the same order.
13614 procedure Derive_Interface_Subprogram
13615 (New_Subp : in out Entity_Id;
13616 Subp : Entity_Id;
13617 Actual_Subp : Entity_Id);
13618 -- Derive New_Subp from the ultimate alias of the parent subprogram Subp
13619 -- (which is an interface primitive). If Generic_Actual is present then
13620 -- Actual_Subp is the actual subprogram corresponding with the generic
13621 -- subprogram Subp.
13623 function Check_Derived_Type return Boolean is
13624 E : Entity_Id;
13625 Elmt : Elmt_Id;
13626 List : Elist_Id;
13627 New_Subp : Entity_Id;
13628 Op_Elmt : Elmt_Id;
13629 Subp : Entity_Id;
13631 begin
13632 -- Traverse list of entities in the current scope searching for
13633 -- an incomplete type whose full-view is derived type
13635 E := First_Entity (Scope (Derived_Type));
13636 while Present (E) and then E /= Derived_Type loop
13637 if Ekind (E) = E_Incomplete_Type
13638 and then Present (Full_View (E))
13639 and then Full_View (E) = Derived_Type
13640 then
13641 -- Disable this test if Derived_Type completes an incomplete
13642 -- type because in such case more primitives can be added
13643 -- later to the list of primitives of Derived_Type by routine
13644 -- Process_Incomplete_Dependents
13646 return True;
13647 end if;
13649 E := Next_Entity (E);
13650 end loop;
13652 List := Collect_Primitive_Operations (Derived_Type);
13653 Elmt := First_Elmt (List);
13655 Op_Elmt := First_Elmt (Op_List);
13656 while Present (Op_Elmt) loop
13657 Subp := Node (Op_Elmt);
13658 New_Subp := Node (Elmt);
13660 -- At this early stage Derived_Type has no entities with attribute
13661 -- Interface_Alias. In addition, such primitives are always
13662 -- located at the end of the list of primitives of Parent_Type.
13663 -- Therefore, if found we can safely stop processing pending
13664 -- entities.
13666 exit when Present (Interface_Alias (Subp));
13668 -- Handle hidden entities
13670 if not Is_Predefined_Dispatching_Operation (Subp)
13671 and then Is_Hidden (Subp)
13672 then
13673 if Present (New_Subp)
13674 and then Primitive_Names_Match (Subp, New_Subp)
13675 then
13676 Next_Elmt (Elmt);
13677 end if;
13679 else
13680 if not Present (New_Subp)
13681 or else Ekind (Subp) /= Ekind (New_Subp)
13682 or else not Primitive_Names_Match (Subp, New_Subp)
13683 then
13684 return False;
13685 end if;
13687 Next_Elmt (Elmt);
13688 end if;
13690 Next_Elmt (Op_Elmt);
13691 end loop;
13693 return True;
13694 end Check_Derived_Type;
13696 ---------------------------------
13697 -- Derive_Interface_Subprogram --
13698 ---------------------------------
13700 procedure Derive_Interface_Subprogram
13701 (New_Subp : in out Entity_Id;
13702 Subp : Entity_Id;
13703 Actual_Subp : Entity_Id)
13705 Iface_Subp : constant Entity_Id := Ultimate_Alias (Subp);
13706 Iface_Type : constant Entity_Id := Find_Dispatching_Type (Iface_Subp);
13708 begin
13709 pragma Assert (Is_Interface (Iface_Type));
13711 Derive_Subprogram
13712 (New_Subp => New_Subp,
13713 Parent_Subp => Iface_Subp,
13714 Derived_Type => Derived_Type,
13715 Parent_Type => Iface_Type,
13716 Actual_Subp => Actual_Subp);
13718 -- Given that this new interface entity corresponds with a primitive
13719 -- of the parent that was not overridden we must leave it associated
13720 -- with its parent primitive to ensure that it will share the same
13721 -- dispatch table slot when overridden.
13723 if No (Actual_Subp) then
13724 Set_Alias (New_Subp, Subp);
13726 -- For instantiations this is not needed since the previous call to
13727 -- Derive_Subprogram leaves the entity well decorated.
13729 else
13730 pragma Assert (Alias (New_Subp) = Actual_Subp);
13731 null;
13732 end if;
13733 end Derive_Interface_Subprogram;
13735 -- Local variables
13737 Alias_Subp : Entity_Id;
13738 Act_List : Elist_Id;
13739 Act_Elmt : Elmt_Id;
13740 Act_Subp : Entity_Id := Empty;
13741 Elmt : Elmt_Id;
13742 Need_Search : Boolean := False;
13743 New_Subp : Entity_Id := Empty;
13744 Parent_Base : Entity_Id;
13745 Subp : Entity_Id;
13747 -- Start of processing for Derive_Subprograms
13749 begin
13750 if Ekind (Parent_Type) = E_Record_Type_With_Private
13751 and then Has_Discriminants (Parent_Type)
13752 and then Present (Full_View (Parent_Type))
13753 then
13754 Parent_Base := Full_View (Parent_Type);
13755 else
13756 Parent_Base := Parent_Type;
13757 end if;
13759 if Present (Generic_Actual) then
13760 Act_List := Collect_Primitive_Operations (Generic_Actual);
13761 Act_Elmt := First_Elmt (Act_List);
13762 else
13763 Act_List := No_Elist;
13764 Act_Elmt := No_Elmt;
13765 end if;
13767 -- Derive primitives inherited from the parent. Note that if the generic
13768 -- actual is present, this is not really a type derivation, it is a
13769 -- completion within an instance.
13771 -- Case 1: Derived_Type does not implement interfaces
13773 if not Is_Tagged_Type (Derived_Type)
13774 or else (not Has_Interfaces (Derived_Type)
13775 and then not (Present (Generic_Actual)
13776 and then Has_Interfaces (Generic_Actual)))
13777 then
13778 Elmt := First_Elmt (Op_List);
13779 while Present (Elmt) loop
13780 Subp := Node (Elmt);
13782 -- Literals are derived earlier in the process of building the
13783 -- derived type, and are skipped here.
13785 if Ekind (Subp) = E_Enumeration_Literal then
13786 null;
13788 -- The actual is a direct descendant and the common primitive
13789 -- operations appear in the same order.
13791 -- If the generic parent type is present, the derived type is an
13792 -- instance of a formal derived type, and within the instance its
13793 -- operations are those of the actual. We derive from the formal
13794 -- type but make the inherited operations aliases of the
13795 -- corresponding operations of the actual.
13797 else
13798 pragma Assert (No (Node (Act_Elmt))
13799 or else (Primitive_Names_Match (Subp, Node (Act_Elmt))
13800 and then
13801 Type_Conformant
13802 (Subp, Node (Act_Elmt),
13803 Skip_Controlling_Formals => True)));
13805 Derive_Subprogram
13806 (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
13808 if Present (Act_Elmt) then
13809 Next_Elmt (Act_Elmt);
13810 end if;
13811 end if;
13813 Next_Elmt (Elmt);
13814 end loop;
13816 -- Case 2: Derived_Type implements interfaces
13818 else
13819 -- If the parent type has no predefined primitives we remove
13820 -- predefined primitives from the list of primitives of generic
13821 -- actual to simplify the complexity of this algorithm.
13823 if Present (Generic_Actual) then
13824 declare
13825 Has_Predefined_Primitives : Boolean := False;
13827 begin
13828 -- Check if the parent type has predefined primitives
13830 Elmt := First_Elmt (Op_List);
13831 while Present (Elmt) loop
13832 Subp := Node (Elmt);
13834 if Is_Predefined_Dispatching_Operation (Subp)
13835 and then not Comes_From_Source (Ultimate_Alias (Subp))
13836 then
13837 Has_Predefined_Primitives := True;
13838 exit;
13839 end if;
13841 Next_Elmt (Elmt);
13842 end loop;
13844 -- Remove predefined primitives of Generic_Actual. We must use
13845 -- an auxiliary list because in case of tagged types the value
13846 -- returned by Collect_Primitive_Operations is the value stored
13847 -- in its Primitive_Operations attribute (and we don't want to
13848 -- modify its current contents).
13850 if not Has_Predefined_Primitives then
13851 declare
13852 Aux_List : constant Elist_Id := New_Elmt_List;
13854 begin
13855 Elmt := First_Elmt (Act_List);
13856 while Present (Elmt) loop
13857 Subp := Node (Elmt);
13859 if not Is_Predefined_Dispatching_Operation (Subp)
13860 or else Comes_From_Source (Subp)
13861 then
13862 Append_Elmt (Subp, Aux_List);
13863 end if;
13865 Next_Elmt (Elmt);
13866 end loop;
13868 Act_List := Aux_List;
13869 end;
13870 end if;
13872 Act_Elmt := First_Elmt (Act_List);
13873 Act_Subp := Node (Act_Elmt);
13874 end;
13875 end if;
13877 -- Stage 1: If the generic actual is not present we derive the
13878 -- primitives inherited from the parent type. If the generic parent
13879 -- type is present, the derived type is an instance of a formal
13880 -- derived type, and within the instance its operations are those of
13881 -- the actual. We derive from the formal type but make the inherited
13882 -- operations aliases of the corresponding operations of the actual.
13884 Elmt := First_Elmt (Op_List);
13885 while Present (Elmt) loop
13886 Subp := Node (Elmt);
13887 Alias_Subp := Ultimate_Alias (Subp);
13889 -- Do not derive internal entities of the parent that link
13890 -- interface primitives with their covering primitive. These
13891 -- entities will be added to this type when frozen.
13893 if Present (Interface_Alias (Subp)) then
13894 goto Continue;
13895 end if;
13897 -- If the generic actual is present find the corresponding
13898 -- operation in the generic actual. If the parent type is a
13899 -- direct ancestor of the derived type then, even if it is an
13900 -- interface, the operations are inherited from the primary
13901 -- dispatch table and are in the proper order. If we detect here
13902 -- that primitives are not in the same order we traverse the list
13903 -- of primitive operations of the actual to find the one that
13904 -- implements the interface primitive.
13906 if Need_Search
13907 or else
13908 (Present (Generic_Actual)
13909 and then Present (Act_Subp)
13910 and then not
13911 (Primitive_Names_Match (Subp, Act_Subp)
13912 and then
13913 Type_Conformant (Subp, Act_Subp,
13914 Skip_Controlling_Formals => True)))
13915 then
13916 pragma Assert (not Is_Ancestor (Parent_Base, Generic_Actual,
13917 Use_Full_View => True));
13919 -- Remember that we need searching for all pending primitives
13921 Need_Search := True;
13923 -- Handle entities associated with interface primitives
13925 if Present (Alias_Subp)
13926 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
13927 and then not Is_Predefined_Dispatching_Operation (Subp)
13928 then
13929 -- Search for the primitive in the homonym chain
13931 Act_Subp :=
13932 Find_Primitive_Covering_Interface
13933 (Tagged_Type => Generic_Actual,
13934 Iface_Prim => Alias_Subp);
13936 -- Previous search may not locate primitives covering
13937 -- interfaces defined in generics units or instantiations.
13938 -- (it fails if the covering primitive has formals whose
13939 -- type is also defined in generics or instantiations).
13940 -- In such case we search in the list of primitives of the
13941 -- generic actual for the internal entity that links the
13942 -- interface primitive and the covering primitive.
13944 if No (Act_Subp)
13945 and then Is_Generic_Type (Parent_Type)
13946 then
13947 -- This code has been designed to handle only generic
13948 -- formals that implement interfaces that are defined
13949 -- in a generic unit or instantiation. If this code is
13950 -- needed for other cases we must review it because
13951 -- (given that it relies on Original_Location to locate
13952 -- the primitive of Generic_Actual that covers the
13953 -- interface) it could leave linked through attribute
13954 -- Alias entities of unrelated instantiations).
13956 pragma Assert
13957 (Is_Generic_Unit
13958 (Scope (Find_Dispatching_Type (Alias_Subp)))
13959 or else
13960 Instantiation_Depth
13961 (Sloc (Find_Dispatching_Type (Alias_Subp))) > 0);
13963 declare
13964 Iface_Prim_Loc : constant Source_Ptr :=
13965 Original_Location (Sloc (Alias_Subp));
13967 Elmt : Elmt_Id;
13968 Prim : Entity_Id;
13970 begin
13971 Elmt :=
13972 First_Elmt (Primitive_Operations (Generic_Actual));
13974 Search : while Present (Elmt) loop
13975 Prim := Node (Elmt);
13977 if Present (Interface_Alias (Prim))
13978 and then Original_Location
13979 (Sloc (Interface_Alias (Prim))) =
13980 Iface_Prim_Loc
13981 then
13982 Act_Subp := Alias (Prim);
13983 exit Search;
13984 end if;
13986 Next_Elmt (Elmt);
13987 end loop Search;
13988 end;
13989 end if;
13991 pragma Assert (Present (Act_Subp)
13992 or else Is_Abstract_Type (Generic_Actual)
13993 or else Serious_Errors_Detected > 0);
13995 -- Handle predefined primitives plus the rest of user-defined
13996 -- primitives
13998 else
13999 Act_Elmt := First_Elmt (Act_List);
14000 while Present (Act_Elmt) loop
14001 Act_Subp := Node (Act_Elmt);
14003 exit when Primitive_Names_Match (Subp, Act_Subp)
14004 and then Type_Conformant
14005 (Subp, Act_Subp,
14006 Skip_Controlling_Formals => True)
14007 and then No (Interface_Alias (Act_Subp));
14009 Next_Elmt (Act_Elmt);
14010 end loop;
14012 if No (Act_Elmt) then
14013 Act_Subp := Empty;
14014 end if;
14015 end if;
14016 end if;
14018 -- Case 1: If the parent is a limited interface then it has the
14019 -- predefined primitives of synchronized interfaces. However, the
14020 -- actual type may be a non-limited type and hence it does not
14021 -- have such primitives.
14023 if Present (Generic_Actual)
14024 and then not Present (Act_Subp)
14025 and then Is_Limited_Interface (Parent_Base)
14026 and then Is_Predefined_Interface_Primitive (Subp)
14027 then
14028 null;
14030 -- Case 2: Inherit entities associated with interfaces that were
14031 -- not covered by the parent type. We exclude here null interface
14032 -- primitives because they do not need special management.
14034 -- We also exclude interface operations that are renamings. If the
14035 -- subprogram is an explicit renaming of an interface primitive,
14036 -- it is a regular primitive operation, and the presence of its
14037 -- alias is not relevant: it has to be derived like any other
14038 -- primitive.
14040 elsif Present (Alias (Subp))
14041 and then Nkind (Unit_Declaration_Node (Subp)) /=
14042 N_Subprogram_Renaming_Declaration
14043 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
14044 and then not
14045 (Nkind (Parent (Alias_Subp)) = N_Procedure_Specification
14046 and then Null_Present (Parent (Alias_Subp)))
14047 then
14048 -- If this is an abstract private type then we transfer the
14049 -- derivation of the interface primitive from the partial view
14050 -- to the full view. This is safe because all the interfaces
14051 -- must be visible in the partial view. Done to avoid adding
14052 -- a new interface derivation to the private part of the
14053 -- enclosing package; otherwise this new derivation would be
14054 -- decorated as hidden when the analysis of the enclosing
14055 -- package completes.
14057 if Is_Abstract_Type (Derived_Type)
14058 and then In_Private_Part (Current_Scope)
14059 and then Has_Private_Declaration (Derived_Type)
14060 then
14061 declare
14062 Partial_View : Entity_Id;
14063 Elmt : Elmt_Id;
14064 Ent : Entity_Id;
14066 begin
14067 Partial_View := First_Entity (Current_Scope);
14068 loop
14069 exit when No (Partial_View)
14070 or else (Has_Private_Declaration (Partial_View)
14071 and then
14072 Full_View (Partial_View) = Derived_Type);
14074 Next_Entity (Partial_View);
14075 end loop;
14077 -- If the partial view was not found then the source code
14078 -- has errors and the derivation is not needed.
14080 if Present (Partial_View) then
14081 Elmt :=
14082 First_Elmt (Primitive_Operations (Partial_View));
14083 while Present (Elmt) loop
14084 Ent := Node (Elmt);
14086 if Present (Alias (Ent))
14087 and then Ultimate_Alias (Ent) = Alias (Subp)
14088 then
14089 Append_Elmt
14090 (Ent, Primitive_Operations (Derived_Type));
14091 exit;
14092 end if;
14094 Next_Elmt (Elmt);
14095 end loop;
14097 -- If the interface primitive was not found in the
14098 -- partial view then this interface primitive was
14099 -- overridden. We add a derivation to activate in
14100 -- Derive_Progenitor_Subprograms the machinery to
14101 -- search for it.
14103 if No (Elmt) then
14104 Derive_Interface_Subprogram
14105 (New_Subp => New_Subp,
14106 Subp => Subp,
14107 Actual_Subp => Act_Subp);
14108 end if;
14109 end if;
14110 end;
14111 else
14112 Derive_Interface_Subprogram
14113 (New_Subp => New_Subp,
14114 Subp => Subp,
14115 Actual_Subp => Act_Subp);
14116 end if;
14118 -- Case 3: Common derivation
14120 else
14121 Derive_Subprogram
14122 (New_Subp => New_Subp,
14123 Parent_Subp => Subp,
14124 Derived_Type => Derived_Type,
14125 Parent_Type => Parent_Base,
14126 Actual_Subp => Act_Subp);
14127 end if;
14129 -- No need to update Act_Elm if we must search for the
14130 -- corresponding operation in the generic actual
14132 if not Need_Search
14133 and then Present (Act_Elmt)
14134 then
14135 Next_Elmt (Act_Elmt);
14136 Act_Subp := Node (Act_Elmt);
14137 end if;
14139 <<Continue>>
14140 Next_Elmt (Elmt);
14141 end loop;
14143 -- Inherit additional operations from progenitors. If the derived
14144 -- type is a generic actual, there are not new primitive operations
14145 -- for the type because it has those of the actual, and therefore
14146 -- nothing needs to be done. The renamings generated above are not
14147 -- primitive operations, and their purpose is simply to make the
14148 -- proper operations visible within an instantiation.
14150 if No (Generic_Actual) then
14151 Derive_Progenitor_Subprograms (Parent_Base, Derived_Type);
14152 end if;
14153 end if;
14155 -- Final check: Direct descendants must have their primitives in the
14156 -- same order. We exclude from this test untagged types and instances
14157 -- of formal derived types. We skip this test if we have already
14158 -- reported serious errors in the sources.
14160 pragma Assert (not Is_Tagged_Type (Derived_Type)
14161 or else Present (Generic_Actual)
14162 or else Serious_Errors_Detected > 0
14163 or else Check_Derived_Type);
14164 end Derive_Subprograms;
14166 --------------------------------
14167 -- Derived_Standard_Character --
14168 --------------------------------
14170 procedure Derived_Standard_Character
14171 (N : Node_Id;
14172 Parent_Type : Entity_Id;
14173 Derived_Type : Entity_Id)
14175 Loc : constant Source_Ptr := Sloc (N);
14176 Def : constant Node_Id := Type_Definition (N);
14177 Indic : constant Node_Id := Subtype_Indication (Def);
14178 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
14179 Implicit_Base : constant Entity_Id :=
14180 Create_Itype
14181 (E_Enumeration_Type, N, Derived_Type, 'B');
14183 Lo : Node_Id;
14184 Hi : Node_Id;
14186 begin
14187 Discard_Node (Process_Subtype (Indic, N));
14189 Set_Etype (Implicit_Base, Parent_Base);
14190 Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
14191 Set_RM_Size (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
14193 Set_Is_Character_Type (Implicit_Base, True);
14194 Set_Has_Delayed_Freeze (Implicit_Base);
14196 -- The bounds of the implicit base are the bounds of the parent base.
14197 -- Note that their type is the parent base.
14199 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
14200 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
14202 Set_Scalar_Range (Implicit_Base,
14203 Make_Range (Loc,
14204 Low_Bound => Lo,
14205 High_Bound => Hi));
14207 Conditional_Delay (Derived_Type, Parent_Type);
14209 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
14210 Set_Etype (Derived_Type, Implicit_Base);
14211 Set_Size_Info (Derived_Type, Parent_Type);
14213 if Unknown_RM_Size (Derived_Type) then
14214 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
14215 end if;
14217 Set_Is_Character_Type (Derived_Type, True);
14219 if Nkind (Indic) /= N_Subtype_Indication then
14221 -- If no explicit constraint, the bounds are those
14222 -- of the parent type.
14224 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Type));
14225 Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
14226 Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
14227 end if;
14229 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
14231 -- Because the implicit base is used in the conversion of the bounds, we
14232 -- have to freeze it now. This is similar to what is done for numeric
14233 -- types, and it equally suspicious, but otherwise a non-static bound
14234 -- will have a reference to an unfrozen type, which is rejected by Gigi
14235 -- (???). This requires specific care for definition of stream
14236 -- attributes. For details, see comments at the end of
14237 -- Build_Derived_Numeric_Type.
14239 Freeze_Before (N, Implicit_Base);
14240 end Derived_Standard_Character;
14242 ------------------------------
14243 -- Derived_Type_Declaration --
14244 ------------------------------
14246 procedure Derived_Type_Declaration
14247 (T : Entity_Id;
14248 N : Node_Id;
14249 Is_Completion : Boolean)
14251 Parent_Type : Entity_Id;
14253 function Comes_From_Generic (Typ : Entity_Id) return Boolean;
14254 -- Check whether the parent type is a generic formal, or derives
14255 -- directly or indirectly from one.
14257 ------------------------
14258 -- Comes_From_Generic --
14259 ------------------------
14261 function Comes_From_Generic (Typ : Entity_Id) return Boolean is
14262 begin
14263 if Is_Generic_Type (Typ) then
14264 return True;
14266 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
14267 return True;
14269 elsif Is_Private_Type (Typ)
14270 and then Present (Full_View (Typ))
14271 and then Is_Generic_Type (Root_Type (Full_View (Typ)))
14272 then
14273 return True;
14275 elsif Is_Generic_Actual_Type (Typ) then
14276 return True;
14278 else
14279 return False;
14280 end if;
14281 end Comes_From_Generic;
14283 -- Local variables
14285 Def : constant Node_Id := Type_Definition (N);
14286 Iface_Def : Node_Id;
14287 Indic : constant Node_Id := Subtype_Indication (Def);
14288 Extension : constant Node_Id := Record_Extension_Part (Def);
14289 Parent_Node : Node_Id;
14290 Taggd : Boolean;
14292 -- Start of processing for Derived_Type_Declaration
14294 begin
14295 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
14297 -- Ada 2005 (AI-251): In case of interface derivation check that the
14298 -- parent is also an interface.
14300 if Interface_Present (Def) then
14301 Check_SPARK_Restriction ("interface is not allowed", Def);
14303 if not Is_Interface (Parent_Type) then
14304 Diagnose_Interface (Indic, Parent_Type);
14306 else
14307 Parent_Node := Parent (Base_Type (Parent_Type));
14308 Iface_Def := Type_Definition (Parent_Node);
14310 -- Ada 2005 (AI-251): Limited interfaces can only inherit from
14311 -- other limited interfaces.
14313 if Limited_Present (Def) then
14314 if Limited_Present (Iface_Def) then
14315 null;
14317 elsif Protected_Present (Iface_Def) then
14318 Error_Msg_NE
14319 ("descendant of& must be declared"
14320 & " as a protected interface",
14321 N, Parent_Type);
14323 elsif Synchronized_Present (Iface_Def) then
14324 Error_Msg_NE
14325 ("descendant of& must be declared"
14326 & " as a synchronized interface",
14327 N, Parent_Type);
14329 elsif Task_Present (Iface_Def) then
14330 Error_Msg_NE
14331 ("descendant of& must be declared as a task interface",
14332 N, Parent_Type);
14334 else
14335 Error_Msg_N
14336 ("(Ada 2005) limited interface cannot "
14337 & "inherit from non-limited interface", Indic);
14338 end if;
14340 -- Ada 2005 (AI-345): Non-limited interfaces can only inherit
14341 -- from non-limited or limited interfaces.
14343 elsif not Protected_Present (Def)
14344 and then not Synchronized_Present (Def)
14345 and then not Task_Present (Def)
14346 then
14347 if Limited_Present (Iface_Def) then
14348 null;
14350 elsif Protected_Present (Iface_Def) then
14351 Error_Msg_NE
14352 ("descendant of& must be declared"
14353 & " as a protected interface",
14354 N, Parent_Type);
14356 elsif Synchronized_Present (Iface_Def) then
14357 Error_Msg_NE
14358 ("descendant of& must be declared"
14359 & " as a synchronized interface",
14360 N, Parent_Type);
14362 elsif Task_Present (Iface_Def) then
14363 Error_Msg_NE
14364 ("descendant of& must be declared as a task interface",
14365 N, Parent_Type);
14366 else
14367 null;
14368 end if;
14369 end if;
14370 end if;
14371 end if;
14373 if Is_Tagged_Type (Parent_Type)
14374 and then Is_Concurrent_Type (Parent_Type)
14375 and then not Is_Interface (Parent_Type)
14376 then
14377 Error_Msg_N
14378 ("parent type of a record extension cannot be "
14379 & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
14380 Set_Etype (T, Any_Type);
14381 return;
14382 end if;
14384 -- Ada 2005 (AI-251): Decorate all the names in the list of ancestor
14385 -- interfaces
14387 if Is_Tagged_Type (Parent_Type)
14388 and then Is_Non_Empty_List (Interface_List (Def))
14389 then
14390 declare
14391 Intf : Node_Id;
14392 T : Entity_Id;
14394 begin
14395 Intf := First (Interface_List (Def));
14396 while Present (Intf) loop
14397 T := Find_Type_Of_Subtype_Indic (Intf);
14399 if not Is_Interface (T) then
14400 Diagnose_Interface (Intf, T);
14402 -- Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
14403 -- a limited type from having a nonlimited progenitor.
14405 elsif (Limited_Present (Def)
14406 or else (not Is_Interface (Parent_Type)
14407 and then Is_Limited_Type (Parent_Type)))
14408 and then not Is_Limited_Interface (T)
14409 then
14410 Error_Msg_NE
14411 ("progenitor interface& of limited type must be limited",
14412 N, T);
14413 end if;
14415 Next (Intf);
14416 end loop;
14417 end;
14418 end if;
14420 if Parent_Type = Any_Type
14421 or else Etype (Parent_Type) = Any_Type
14422 or else (Is_Class_Wide_Type (Parent_Type)
14423 and then Etype (Parent_Type) = T)
14424 then
14425 -- If Parent_Type is undefined or illegal, make new type into a
14426 -- subtype of Any_Type, and set a few attributes to prevent cascaded
14427 -- errors. If this is a self-definition, emit error now.
14429 if T = Parent_Type
14430 or else T = Etype (Parent_Type)
14431 then
14432 Error_Msg_N ("type cannot be used in its own definition", Indic);
14433 end if;
14435 Set_Ekind (T, Ekind (Parent_Type));
14436 Set_Etype (T, Any_Type);
14437 Set_Scalar_Range (T, Scalar_Range (Any_Type));
14439 if Is_Tagged_Type (T)
14440 and then Is_Record_Type (T)
14441 then
14442 Set_Direct_Primitive_Operations (T, New_Elmt_List);
14443 end if;
14445 return;
14446 end if;
14448 -- Ada 2005 (AI-251): The case in which the parent of the full-view is
14449 -- an interface is special because the list of interfaces in the full
14450 -- view can be given in any order. For example:
14452 -- type A is interface;
14453 -- type B is interface and A;
14454 -- type D is new B with private;
14455 -- private
14456 -- type D is new A and B with null record; -- 1 --
14458 -- In this case we perform the following transformation of -1-:
14460 -- type D is new B and A with null record;
14462 -- If the parent of the full-view covers the parent of the partial-view
14463 -- we have two possible cases:
14465 -- 1) They have the same parent
14466 -- 2) The parent of the full-view implements some further interfaces
14468 -- In both cases we do not need to perform the transformation. In the
14469 -- first case the source program is correct and the transformation is
14470 -- not needed; in the second case the source program does not fulfill
14471 -- the no-hidden interfaces rule (AI-396) and the error will be reported
14472 -- later.
14474 -- This transformation not only simplifies the rest of the analysis of
14475 -- this type declaration but also simplifies the correct generation of
14476 -- the object layout to the expander.
14478 if In_Private_Part (Current_Scope)
14479 and then Is_Interface (Parent_Type)
14480 then
14481 declare
14482 Iface : Node_Id;
14483 Partial_View : Entity_Id;
14484 Partial_View_Parent : Entity_Id;
14485 New_Iface : Node_Id;
14487 begin
14488 -- Look for the associated private type declaration
14490 Partial_View := First_Entity (Current_Scope);
14491 loop
14492 exit when No (Partial_View)
14493 or else (Has_Private_Declaration (Partial_View)
14494 and then Full_View (Partial_View) = T);
14496 Next_Entity (Partial_View);
14497 end loop;
14499 -- If the partial view was not found then the source code has
14500 -- errors and the transformation is not needed.
14502 if Present (Partial_View) then
14503 Partial_View_Parent := Etype (Partial_View);
14505 -- If the parent of the full-view covers the parent of the
14506 -- partial-view we have nothing else to do.
14508 if Interface_Present_In_Ancestor
14509 (Parent_Type, Partial_View_Parent)
14510 then
14511 null;
14513 -- Traverse the list of interfaces of the full-view to look
14514 -- for the parent of the partial-view and perform the tree
14515 -- transformation.
14517 else
14518 Iface := First (Interface_List (Def));
14519 while Present (Iface) loop
14520 if Etype (Iface) = Etype (Partial_View) then
14521 Rewrite (Subtype_Indication (Def),
14522 New_Copy (Subtype_Indication
14523 (Parent (Partial_View))));
14525 New_Iface :=
14526 Make_Identifier (Sloc (N), Chars (Parent_Type));
14527 Append (New_Iface, Interface_List (Def));
14529 -- Analyze the transformed code
14531 Derived_Type_Declaration (T, N, Is_Completion);
14532 return;
14533 end if;
14535 Next (Iface);
14536 end loop;
14537 end if;
14538 end if;
14539 end;
14540 end if;
14542 -- Only composite types other than array types are allowed to have
14543 -- discriminants. In SPARK, no types are allowed to have discriminants.
14545 if Present (Discriminant_Specifications (N)) then
14546 if (Is_Elementary_Type (Parent_Type)
14547 or else Is_Array_Type (Parent_Type))
14548 and then not Error_Posted (N)
14549 then
14550 Error_Msg_N
14551 ("elementary or array type cannot have discriminants",
14552 Defining_Identifier (First (Discriminant_Specifications (N))));
14553 Set_Has_Discriminants (T, False);
14554 else
14555 Check_SPARK_Restriction ("discriminant type is not allowed", N);
14556 end if;
14557 end if;
14559 -- In Ada 83, a derived type defined in a package specification cannot
14560 -- be used for further derivation until the end of its visible part.
14561 -- Note that derivation in the private part of the package is allowed.
14563 if Ada_Version = Ada_83
14564 and then Is_Derived_Type (Parent_Type)
14565 and then In_Visible_Part (Scope (Parent_Type))
14566 then
14567 if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
14568 Error_Msg_N
14569 ("(Ada 83): premature use of type for derivation", Indic);
14570 end if;
14571 end if;
14573 -- Check for early use of incomplete or private type
14575 if Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
14576 Error_Msg_N ("premature derivation of incomplete type", Indic);
14577 return;
14579 elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
14580 and then not Comes_From_Generic (Parent_Type))
14581 or else Has_Private_Component (Parent_Type)
14582 then
14583 -- The ancestor type of a formal type can be incomplete, in which
14584 -- case only the operations of the partial view are available in the
14585 -- generic. Subsequent checks may be required when the full view is
14586 -- analyzed to verify that a derivation from a tagged type has an
14587 -- extension.
14589 if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
14590 null;
14592 elsif No (Underlying_Type (Parent_Type))
14593 or else Has_Private_Component (Parent_Type)
14594 then
14595 Error_Msg_N
14596 ("premature derivation of derived or private type", Indic);
14598 -- Flag the type itself as being in error, this prevents some
14599 -- nasty problems with subsequent uses of the malformed type.
14601 Set_Error_Posted (T);
14603 -- Check that within the immediate scope of an untagged partial
14604 -- view it's illegal to derive from the partial view if the
14605 -- full view is tagged. (7.3(7))
14607 -- We verify that the Parent_Type is a partial view by checking
14608 -- that it is not a Full_Type_Declaration (i.e. a private type or
14609 -- private extension declaration), to distinguish a partial view
14610 -- from a derivation from a private type which also appears as
14611 -- E_Private_Type. If the parent base type is not declared in an
14612 -- enclosing scope there is no need to check.
14614 elsif Present (Full_View (Parent_Type))
14615 and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
14616 and then not Is_Tagged_Type (Parent_Type)
14617 and then Is_Tagged_Type (Full_View (Parent_Type))
14618 and then In_Open_Scopes (Scope (Base_Type (Parent_Type)))
14619 then
14620 Error_Msg_N
14621 ("premature derivation from type with tagged full view",
14622 Indic);
14623 end if;
14624 end if;
14626 -- Check that form of derivation is appropriate
14628 Taggd := Is_Tagged_Type (Parent_Type);
14630 -- Perhaps the parent type should be changed to the class-wide type's
14631 -- specific type in this case to prevent cascading errors ???
14633 if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
14634 Error_Msg_N ("parent type must not be a class-wide type", Indic);
14635 return;
14636 end if;
14638 if Present (Extension) and then not Taggd then
14639 Error_Msg_N
14640 ("type derived from untagged type cannot have extension", Indic);
14642 elsif No (Extension) and then Taggd then
14644 -- If this declaration is within a private part (or body) of a
14645 -- generic instantiation then the derivation is allowed (the parent
14646 -- type can only appear tagged in this case if it's a generic actual
14647 -- type, since it would otherwise have been rejected in the analysis
14648 -- of the generic template).
14650 if not Is_Generic_Actual_Type (Parent_Type)
14651 or else In_Visible_Part (Scope (Parent_Type))
14652 then
14653 if Is_Class_Wide_Type (Parent_Type) then
14654 Error_Msg_N
14655 ("parent type must not be a class-wide type", Indic);
14657 -- Use specific type to prevent cascaded errors.
14659 Parent_Type := Etype (Parent_Type);
14661 else
14662 Error_Msg_N
14663 ("type derived from tagged type must have extension", Indic);
14664 end if;
14665 end if;
14666 end if;
14668 -- AI-443: Synchronized formal derived types require a private
14669 -- extension. There is no point in checking the ancestor type or
14670 -- the progenitors since the construct is wrong to begin with.
14672 if Ada_Version >= Ada_2005
14673 and then Is_Generic_Type (T)
14674 and then Present (Original_Node (N))
14675 then
14676 declare
14677 Decl : constant Node_Id := Original_Node (N);
14679 begin
14680 if Nkind (Decl) = N_Formal_Type_Declaration
14681 and then Nkind (Formal_Type_Definition (Decl)) =
14682 N_Formal_Derived_Type_Definition
14683 and then Synchronized_Present (Formal_Type_Definition (Decl))
14684 and then No (Extension)
14686 -- Avoid emitting a duplicate error message
14688 and then not Error_Posted (Indic)
14689 then
14690 Error_Msg_N
14691 ("synchronized derived type must have extension", N);
14692 end if;
14693 end;
14694 end if;
14696 if Null_Exclusion_Present (Def)
14697 and then not Is_Access_Type (Parent_Type)
14698 then
14699 Error_Msg_N ("null exclusion can only apply to an access type", N);
14700 end if;
14702 -- Avoid deriving parent primitives of underlying record views
14704 Build_Derived_Type (N, Parent_Type, T, Is_Completion,
14705 Derive_Subps => not Is_Underlying_Record_View (T));
14707 -- AI-419: The parent type of an explicitly limited derived type must
14708 -- be a limited type or a limited interface.
14710 if Limited_Present (Def) then
14711 Set_Is_Limited_Record (T);
14713 if Is_Interface (T) then
14714 Set_Is_Limited_Interface (T);
14715 end if;
14717 if not Is_Limited_Type (Parent_Type)
14718 and then
14719 (not Is_Interface (Parent_Type)
14720 or else not Is_Limited_Interface (Parent_Type))
14721 then
14722 -- AI05-0096: a derivation in the private part of an instance is
14723 -- legal if the generic formal is untagged limited, and the actual
14724 -- is non-limited.
14726 if Is_Generic_Actual_Type (Parent_Type)
14727 and then In_Private_Part (Current_Scope)
14728 and then
14729 not Is_Tagged_Type
14730 (Generic_Parent_Type (Parent (Parent_Type)))
14731 then
14732 null;
14734 else
14735 Error_Msg_NE
14736 ("parent type& of limited type must be limited",
14737 N, Parent_Type);
14738 end if;
14739 end if;
14740 end if;
14742 -- In SPARK, there are no derived type definitions other than type
14743 -- extensions of tagged record types.
14745 if No (Extension) then
14746 Check_SPARK_Restriction ("derived type is not allowed", N);
14747 end if;
14748 end Derived_Type_Declaration;
14750 ------------------------
14751 -- Diagnose_Interface --
14752 ------------------------
14754 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id) is
14755 begin
14756 if not Is_Interface (E)
14757 and then E /= Any_Type
14758 then
14759 Error_Msg_NE ("(Ada 2005) & must be an interface", N, E);
14760 end if;
14761 end Diagnose_Interface;
14763 ----------------------------------
14764 -- Enumeration_Type_Declaration --
14765 ----------------------------------
14767 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
14768 Ev : Uint;
14769 L : Node_Id;
14770 R_Node : Node_Id;
14771 B_Node : Node_Id;
14773 begin
14774 -- Create identifier node representing lower bound
14776 B_Node := New_Node (N_Identifier, Sloc (Def));
14777 L := First (Literals (Def));
14778 Set_Chars (B_Node, Chars (L));
14779 Set_Entity (B_Node, L);
14780 Set_Etype (B_Node, T);
14781 Set_Is_Static_Expression (B_Node, True);
14783 R_Node := New_Node (N_Range, Sloc (Def));
14784 Set_Low_Bound (R_Node, B_Node);
14786 Set_Ekind (T, E_Enumeration_Type);
14787 Set_First_Literal (T, L);
14788 Set_Etype (T, T);
14789 Set_Is_Constrained (T);
14791 Ev := Uint_0;
14793 -- Loop through literals of enumeration type setting pos and rep values
14794 -- except that if the Ekind is already set, then it means the literal
14795 -- was already constructed (case of a derived type declaration and we
14796 -- should not disturb the Pos and Rep values.
14798 while Present (L) loop
14799 if Ekind (L) /= E_Enumeration_Literal then
14800 Set_Ekind (L, E_Enumeration_Literal);
14801 Set_Enumeration_Pos (L, Ev);
14802 Set_Enumeration_Rep (L, Ev);
14803 Set_Is_Known_Valid (L, True);
14804 end if;
14806 Set_Etype (L, T);
14807 New_Overloaded_Entity (L);
14808 Generate_Definition (L);
14809 Set_Convention (L, Convention_Intrinsic);
14811 -- Case of character literal
14813 if Nkind (L) = N_Defining_Character_Literal then
14814 Set_Is_Character_Type (T, True);
14816 -- Check violation of No_Wide_Characters
14818 if Restriction_Check_Required (No_Wide_Characters) then
14819 Get_Name_String (Chars (L));
14821 if Name_Len >= 3 and then Name_Buffer (1 .. 2) = "QW" then
14822 Check_Restriction (No_Wide_Characters, L);
14823 end if;
14824 end if;
14825 end if;
14827 Ev := Ev + 1;
14828 Next (L);
14829 end loop;
14831 -- Now create a node representing upper bound
14833 B_Node := New_Node (N_Identifier, Sloc (Def));
14834 Set_Chars (B_Node, Chars (Last (Literals (Def))));
14835 Set_Entity (B_Node, Last (Literals (Def)));
14836 Set_Etype (B_Node, T);
14837 Set_Is_Static_Expression (B_Node, True);
14839 Set_High_Bound (R_Node, B_Node);
14841 -- Initialize various fields of the type. Some of this information
14842 -- may be overwritten later through rep.clauses.
14844 Set_Scalar_Range (T, R_Node);
14845 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
14846 Set_Enum_Esize (T);
14847 Set_Enum_Pos_To_Rep (T, Empty);
14849 -- Set Discard_Names if configuration pragma set, or if there is
14850 -- a parameterless pragma in the current declarative region
14852 if Global_Discard_Names or else Discard_Names (Scope (T)) then
14853 Set_Discard_Names (T);
14854 end if;
14856 -- Process end label if there is one
14858 if Present (Def) then
14859 Process_End_Label (Def, 'e', T);
14860 end if;
14861 end Enumeration_Type_Declaration;
14863 ---------------------------------
14864 -- Expand_To_Stored_Constraint --
14865 ---------------------------------
14867 function Expand_To_Stored_Constraint
14868 (Typ : Entity_Id;
14869 Constraint : Elist_Id) return Elist_Id
14871 Explicitly_Discriminated_Type : Entity_Id;
14872 Expansion : Elist_Id;
14873 Discriminant : Entity_Id;
14875 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
14876 -- Find the nearest type that actually specifies discriminants
14878 ---------------------------------
14879 -- Type_With_Explicit_Discrims --
14880 ---------------------------------
14882 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
14883 Typ : constant E := Base_Type (Id);
14885 begin
14886 if Ekind (Typ) in Incomplete_Or_Private_Kind then
14887 if Present (Full_View (Typ)) then
14888 return Type_With_Explicit_Discrims (Full_View (Typ));
14889 end if;
14891 else
14892 if Has_Discriminants (Typ) then
14893 return Typ;
14894 end if;
14895 end if;
14897 if Etype (Typ) = Typ then
14898 return Empty;
14899 elsif Has_Discriminants (Typ) then
14900 return Typ;
14901 else
14902 return Type_With_Explicit_Discrims (Etype (Typ));
14903 end if;
14905 end Type_With_Explicit_Discrims;
14907 -- Start of processing for Expand_To_Stored_Constraint
14909 begin
14910 if No (Constraint)
14911 or else Is_Empty_Elmt_List (Constraint)
14912 then
14913 return No_Elist;
14914 end if;
14916 Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
14918 if No (Explicitly_Discriminated_Type) then
14919 return No_Elist;
14920 end if;
14922 Expansion := New_Elmt_List;
14924 Discriminant :=
14925 First_Stored_Discriminant (Explicitly_Discriminated_Type);
14926 while Present (Discriminant) loop
14927 Append_Elmt (
14928 Get_Discriminant_Value (
14929 Discriminant, Explicitly_Discriminated_Type, Constraint),
14930 Expansion);
14931 Next_Stored_Discriminant (Discriminant);
14932 end loop;
14934 return Expansion;
14935 end Expand_To_Stored_Constraint;
14937 ---------------------------
14938 -- Find_Hidden_Interface --
14939 ---------------------------
14941 function Find_Hidden_Interface
14942 (Src : Elist_Id;
14943 Dest : Elist_Id) return Entity_Id
14945 Iface : Entity_Id;
14946 Iface_Elmt : Elmt_Id;
14948 begin
14949 if Present (Src) and then Present (Dest) then
14950 Iface_Elmt := First_Elmt (Src);
14951 while Present (Iface_Elmt) loop
14952 Iface := Node (Iface_Elmt);
14954 if Is_Interface (Iface)
14955 and then not Contain_Interface (Iface, Dest)
14956 then
14957 return Iface;
14958 end if;
14960 Next_Elmt (Iface_Elmt);
14961 end loop;
14962 end if;
14964 return Empty;
14965 end Find_Hidden_Interface;
14967 --------------------
14968 -- Find_Type_Name --
14969 --------------------
14971 function Find_Type_Name (N : Node_Id) return Entity_Id is
14972 Id : constant Entity_Id := Defining_Identifier (N);
14973 Prev : Entity_Id;
14974 New_Id : Entity_Id;
14975 Prev_Par : Node_Id;
14977 procedure Check_Duplicate_Aspects;
14978 -- Check that aspects specified in a completion have not been specified
14979 -- already in the partial view. Type_Invariant and others can be
14980 -- specified on either view but never on both.
14982 procedure Tag_Mismatch;
14983 -- Diagnose a tagged partial view whose full view is untagged.
14984 -- We post the message on the full view, with a reference to
14985 -- the previous partial view. The partial view can be private
14986 -- or incomplete, and these are handled in a different manner,
14987 -- so we determine the position of the error message from the
14988 -- respective slocs of both.
14990 -----------------------------
14991 -- Check_Duplicate_Aspects --
14992 -----------------------------
14993 procedure Check_Duplicate_Aspects is
14994 Prev_Aspects : constant List_Id := Aspect_Specifications (Prev_Par);
14995 Full_Aspects : constant List_Id := Aspect_Specifications (N);
14996 F_Spec, P_Spec : Node_Id;
14998 begin
14999 if Present (Prev_Aspects) and then Present (Full_Aspects) then
15000 F_Spec := First (Full_Aspects);
15001 while Present (F_Spec) loop
15002 P_Spec := First (Prev_Aspects);
15003 while Present (P_Spec) loop
15005 Chars (Identifier (P_Spec)) = Chars (Identifier (F_Spec))
15006 then
15007 Error_Msg_N
15008 ("aspect already specified in private declaration",
15009 F_Spec);
15010 Remove (F_Spec);
15011 return;
15012 end if;
15014 Next (P_Spec);
15015 end loop;
15017 Next (F_Spec);
15018 end loop;
15019 end if;
15020 end Check_Duplicate_Aspects;
15022 ------------------
15023 -- Tag_Mismatch --
15024 ------------------
15026 procedure Tag_Mismatch is
15027 begin
15028 if Sloc (Prev) < Sloc (Id) then
15029 if Ada_Version >= Ada_2012
15030 and then Nkind (N) = N_Private_Type_Declaration
15031 then
15032 Error_Msg_NE
15033 ("declaration of private } must be a tagged type ", Id, Prev);
15034 else
15035 Error_Msg_NE
15036 ("full declaration of } must be a tagged type ", Id, Prev);
15037 end if;
15038 else
15039 if Ada_Version >= Ada_2012
15040 and then Nkind (N) = N_Private_Type_Declaration
15041 then
15042 Error_Msg_NE
15043 ("declaration of private } must be a tagged type ", Prev, Id);
15044 else
15045 Error_Msg_NE
15046 ("full declaration of } must be a tagged type ", Prev, Id);
15047 end if;
15048 end if;
15049 end Tag_Mismatch;
15051 -- Start of processing for Find_Type_Name
15053 begin
15054 -- Find incomplete declaration, if one was given
15056 Prev := Current_Entity_In_Scope (Id);
15058 -- New type declaration
15060 if No (Prev) then
15061 Enter_Name (Id);
15062 return Id;
15064 -- Previous declaration exists
15066 else
15067 Prev_Par := Parent (Prev);
15069 -- Error if not incomplete/private case except if previous
15070 -- declaration is implicit, etc. Enter_Name will emit error if
15071 -- appropriate.
15073 if not Is_Incomplete_Or_Private_Type (Prev) then
15074 Enter_Name (Id);
15075 New_Id := Id;
15077 -- Check invalid completion of private or incomplete type
15079 elsif not Nkind_In (N, N_Full_Type_Declaration,
15080 N_Task_Type_Declaration,
15081 N_Protected_Type_Declaration)
15082 and then
15083 (Ada_Version < Ada_2012
15084 or else not Is_Incomplete_Type (Prev)
15085 or else not Nkind_In (N, N_Private_Type_Declaration,
15086 N_Private_Extension_Declaration))
15087 then
15088 -- Completion must be a full type declarations (RM 7.3(4))
15090 Error_Msg_Sloc := Sloc (Prev);
15091 Error_Msg_NE ("invalid completion of }", Id, Prev);
15093 -- Set scope of Id to avoid cascaded errors. Entity is never
15094 -- examined again, except when saving globals in generics.
15096 Set_Scope (Id, Current_Scope);
15097 New_Id := Id;
15099 -- If this is a repeated incomplete declaration, no further
15100 -- checks are possible.
15102 if Nkind (N) = N_Incomplete_Type_Declaration then
15103 return Prev;
15104 end if;
15106 -- Case of full declaration of incomplete type
15108 elsif Ekind (Prev) = E_Incomplete_Type
15109 and then (Ada_Version < Ada_2012
15110 or else No (Full_View (Prev))
15111 or else not Is_Private_Type (Full_View (Prev)))
15112 then
15114 -- Indicate that the incomplete declaration has a matching full
15115 -- declaration. The defining occurrence of the incomplete
15116 -- declaration remains the visible one, and the procedure
15117 -- Get_Full_View dereferences it whenever the type is used.
15119 if Present (Full_View (Prev)) then
15120 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
15121 end if;
15123 Set_Full_View (Prev, Id);
15124 Append_Entity (Id, Current_Scope);
15125 Set_Is_Public (Id, Is_Public (Prev));
15126 Set_Is_Internal (Id);
15127 New_Id := Prev;
15129 -- If the incomplete view is tagged, a class_wide type has been
15130 -- created already. Use it for the private type as well, in order
15131 -- to prevent multiple incompatible class-wide types that may be
15132 -- created for self-referential anonymous access components.
15134 if Is_Tagged_Type (Prev)
15135 and then Present (Class_Wide_Type (Prev))
15136 then
15137 Set_Ekind (Id, Ekind (Prev)); -- will be reset later
15138 Set_Class_Wide_Type (Id, Class_Wide_Type (Prev));
15140 -- If the incomplete type is completed by a private declaration
15141 -- the class-wide type remains associated with the incomplete
15142 -- type, to prevent order-of-elaboration issues in gigi, else
15143 -- we associate the class-wide type with the known full view.
15145 if Nkind (N) /= N_Private_Type_Declaration then
15146 Set_Etype (Class_Wide_Type (Id), Id);
15147 end if;
15148 end if;
15150 -- Case of full declaration of private type
15152 else
15153 -- If the private type was a completion of an incomplete type then
15154 -- update Prev to reference the private type
15156 if Ada_Version >= Ada_2012
15157 and then Ekind (Prev) = E_Incomplete_Type
15158 and then Present (Full_View (Prev))
15159 and then Is_Private_Type (Full_View (Prev))
15160 then
15161 Prev := Full_View (Prev);
15162 Prev_Par := Parent (Prev);
15163 end if;
15165 if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
15166 if Etype (Prev) /= Prev then
15168 -- Prev is a private subtype or a derived type, and needs
15169 -- no completion.
15171 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
15172 New_Id := Id;
15174 elsif Ekind (Prev) = E_Private_Type
15175 and then Nkind_In (N, N_Task_Type_Declaration,
15176 N_Protected_Type_Declaration)
15177 then
15178 Error_Msg_N
15179 ("completion of nonlimited type cannot be limited", N);
15181 elsif Ekind (Prev) = E_Record_Type_With_Private
15182 and then Nkind_In (N, N_Task_Type_Declaration,
15183 N_Protected_Type_Declaration)
15184 then
15185 if not Is_Limited_Record (Prev) then
15186 Error_Msg_N
15187 ("completion of nonlimited type cannot be limited", N);
15189 elsif No (Interface_List (N)) then
15190 Error_Msg_N
15191 ("completion of tagged private type must be tagged",
15193 end if;
15195 elsif Nkind (N) = N_Full_Type_Declaration
15196 and then
15197 Nkind (Type_Definition (N)) = N_Record_Definition
15198 and then Interface_Present (Type_Definition (N))
15199 then
15200 Error_Msg_N
15201 ("completion of private type cannot be an interface", N);
15202 end if;
15204 -- Ada 2005 (AI-251): Private extension declaration of a task
15205 -- type or a protected type. This case arises when covering
15206 -- interface types.
15208 elsif Nkind_In (N, N_Task_Type_Declaration,
15209 N_Protected_Type_Declaration)
15210 then
15211 null;
15213 elsif Nkind (N) /= N_Full_Type_Declaration
15214 or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
15215 then
15216 Error_Msg_N
15217 ("full view of private extension must be an extension", N);
15219 elsif not (Abstract_Present (Parent (Prev)))
15220 and then Abstract_Present (Type_Definition (N))
15221 then
15222 Error_Msg_N
15223 ("full view of non-abstract extension cannot be abstract", N);
15224 end if;
15226 if not In_Private_Part (Current_Scope) then
15227 Error_Msg_N
15228 ("declaration of full view must appear in private part", N);
15229 end if;
15231 if Ada_Version >= Ada_2012 then
15232 Check_Duplicate_Aspects;
15233 end if;
15235 Copy_And_Swap (Prev, Id);
15236 Set_Has_Private_Declaration (Prev);
15237 Set_Has_Private_Declaration (Id);
15239 -- Preserve aspect and iterator flags that may have been set on
15240 -- the partial view.
15242 Set_Has_Delayed_Aspects (Prev, Has_Delayed_Aspects (Id));
15243 Set_Has_Implicit_Dereference (Prev, Has_Implicit_Dereference (Id));
15245 -- If no error, propagate freeze_node from private to full view.
15246 -- It may have been generated for an early operational item.
15248 if Present (Freeze_Node (Id))
15249 and then Serious_Errors_Detected = 0
15250 and then No (Full_View (Id))
15251 then
15252 Set_Freeze_Node (Prev, Freeze_Node (Id));
15253 Set_Freeze_Node (Id, Empty);
15254 Set_First_Rep_Item (Prev, First_Rep_Item (Id));
15255 end if;
15257 Set_Full_View (Id, Prev);
15258 New_Id := Prev;
15259 end if;
15261 -- Verify that full declaration conforms to partial one
15263 if Is_Incomplete_Or_Private_Type (Prev)
15264 and then Present (Discriminant_Specifications (Prev_Par))
15265 then
15266 if Present (Discriminant_Specifications (N)) then
15267 if Ekind (Prev) = E_Incomplete_Type then
15268 Check_Discriminant_Conformance (N, Prev, Prev);
15269 else
15270 Check_Discriminant_Conformance (N, Prev, Id);
15271 end if;
15273 else
15274 Error_Msg_N
15275 ("missing discriminants in full type declaration", N);
15277 -- To avoid cascaded errors on subsequent use, share the
15278 -- discriminants of the partial view.
15280 Set_Discriminant_Specifications (N,
15281 Discriminant_Specifications (Prev_Par));
15282 end if;
15283 end if;
15285 -- A prior untagged partial view can have an associated class-wide
15286 -- type due to use of the class attribute, and in this case the full
15287 -- type must also be tagged. This Ada 95 usage is deprecated in favor
15288 -- of incomplete tagged declarations, but we check for it.
15290 if Is_Type (Prev)
15291 and then (Is_Tagged_Type (Prev)
15292 or else Present (Class_Wide_Type (Prev)))
15293 then
15294 -- Ada 2012 (AI05-0162): A private type may be the completion of
15295 -- an incomplete type
15297 if Ada_Version >= Ada_2012
15298 and then Is_Incomplete_Type (Prev)
15299 and then Nkind_In (N, N_Private_Type_Declaration,
15300 N_Private_Extension_Declaration)
15301 then
15302 -- No need to check private extensions since they are tagged
15304 if Nkind (N) = N_Private_Type_Declaration
15305 and then not Tagged_Present (N)
15306 then
15307 Tag_Mismatch;
15308 end if;
15310 -- The full declaration is either a tagged type (including
15311 -- a synchronized type that implements interfaces) or a
15312 -- type extension, otherwise this is an error.
15314 elsif Nkind_In (N, N_Task_Type_Declaration,
15315 N_Protected_Type_Declaration)
15316 then
15317 if No (Interface_List (N))
15318 and then not Error_Posted (N)
15319 then
15320 Tag_Mismatch;
15321 end if;
15323 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
15325 -- Indicate that the previous declaration (tagged incomplete
15326 -- or private declaration) requires the same on the full one.
15328 if not Tagged_Present (Type_Definition (N)) then
15329 Tag_Mismatch;
15330 Set_Is_Tagged_Type (Id);
15331 end if;
15333 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
15334 if No (Record_Extension_Part (Type_Definition (N))) then
15335 Error_Msg_NE
15336 ("full declaration of } must be a record extension",
15337 Prev, Id);
15339 -- Set some attributes to produce a usable full view
15341 Set_Is_Tagged_Type (Id);
15342 end if;
15344 else
15345 Tag_Mismatch;
15346 end if;
15347 end if;
15349 if Present (Prev)
15350 and then Nkind (Parent (Prev)) = N_Incomplete_Type_Declaration
15351 and then Present (Premature_Use (Parent (Prev)))
15352 then
15353 Error_Msg_Sloc := Sloc (N);
15354 Error_Msg_N
15355 ("\full declaration #", Premature_Use (Parent (Prev)));
15356 end if;
15358 return New_Id;
15359 end if;
15360 end Find_Type_Name;
15362 -------------------------
15363 -- Find_Type_Of_Object --
15364 -------------------------
15366 function Find_Type_Of_Object
15367 (Obj_Def : Node_Id;
15368 Related_Nod : Node_Id) return Entity_Id
15370 Def_Kind : constant Node_Kind := Nkind (Obj_Def);
15371 P : Node_Id := Parent (Obj_Def);
15372 T : Entity_Id;
15373 Nam : Name_Id;
15375 begin
15376 -- If the parent is a component_definition node we climb to the
15377 -- component_declaration node
15379 if Nkind (P) = N_Component_Definition then
15380 P := Parent (P);
15381 end if;
15383 -- Case of an anonymous array subtype
15385 if Nkind_In (Def_Kind, N_Constrained_Array_Definition,
15386 N_Unconstrained_Array_Definition)
15387 then
15388 T := Empty;
15389 Array_Type_Declaration (T, Obj_Def);
15391 -- Create an explicit subtype whenever possible
15393 elsif Nkind (P) /= N_Component_Declaration
15394 and then Def_Kind = N_Subtype_Indication
15395 then
15396 -- Base name of subtype on object name, which will be unique in
15397 -- the current scope.
15399 -- If this is a duplicate declaration, return base type, to avoid
15400 -- generating duplicate anonymous types.
15402 if Error_Posted (P) then
15403 Analyze (Subtype_Mark (Obj_Def));
15404 return Entity (Subtype_Mark (Obj_Def));
15405 end if;
15407 Nam :=
15408 New_External_Name
15409 (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
15411 T := Make_Defining_Identifier (Sloc (P), Nam);
15413 Insert_Action (Obj_Def,
15414 Make_Subtype_Declaration (Sloc (P),
15415 Defining_Identifier => T,
15416 Subtype_Indication => Relocate_Node (Obj_Def)));
15418 -- This subtype may need freezing, and this will not be done
15419 -- automatically if the object declaration is not in declarative
15420 -- part. Since this is an object declaration, the type cannot always
15421 -- be frozen here. Deferred constants do not freeze their type
15422 -- (which often enough will be private).
15424 if Nkind (P) = N_Object_Declaration
15425 and then Constant_Present (P)
15426 and then No (Expression (P))
15427 then
15428 null;
15429 else
15430 Insert_Actions (Obj_Def, Freeze_Entity (T, P));
15431 end if;
15433 -- Ada 2005 AI-406: the object definition in an object declaration
15434 -- can be an access definition.
15436 elsif Def_Kind = N_Access_Definition then
15437 T := Access_Definition (Related_Nod, Obj_Def);
15439 Set_Is_Local_Anonymous_Access
15441 V => (Ada_Version < Ada_2012)
15442 or else (Nkind (P) /= N_Object_Declaration)
15443 or else Is_Library_Level_Entity (Defining_Identifier (P)));
15445 -- Otherwise, the object definition is just a subtype_mark
15447 else
15448 T := Process_Subtype (Obj_Def, Related_Nod);
15450 -- If expansion is disabled an object definition that is an aggregate
15451 -- will not get expanded and may lead to scoping problems in the back
15452 -- end, if the object is referenced in an inner scope. In that case
15453 -- create an itype reference for the object definition now. This
15454 -- may be redundant in some cases, but harmless.
15456 if Is_Itype (T)
15457 and then Nkind (Related_Nod) = N_Object_Declaration
15458 and then ASIS_Mode
15459 then
15460 Build_Itype_Reference (T, Related_Nod);
15461 end if;
15462 end if;
15464 return T;
15465 end Find_Type_Of_Object;
15467 --------------------------------
15468 -- Find_Type_Of_Subtype_Indic --
15469 --------------------------------
15471 function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
15472 Typ : Entity_Id;
15474 begin
15475 -- Case of subtype mark with a constraint
15477 if Nkind (S) = N_Subtype_Indication then
15478 Find_Type (Subtype_Mark (S));
15479 Typ := Entity (Subtype_Mark (S));
15481 if not
15482 Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
15483 then
15484 Error_Msg_N
15485 ("incorrect constraint for this kind of type", Constraint (S));
15486 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
15487 end if;
15489 -- Otherwise we have a subtype mark without a constraint
15491 elsif Error_Posted (S) then
15492 Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
15493 return Any_Type;
15495 else
15496 Find_Type (S);
15497 Typ := Entity (S);
15498 end if;
15500 -- Check No_Wide_Characters restriction
15502 Check_Wide_Character_Restriction (Typ, S);
15504 return Typ;
15505 end Find_Type_Of_Subtype_Indic;
15507 -------------------------------------
15508 -- Floating_Point_Type_Declaration --
15509 -------------------------------------
15511 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
15512 Digs : constant Node_Id := Digits_Expression (Def);
15513 Max_Digs_Val : constant Uint := Digits_Value (Standard_Long_Long_Float);
15514 Digs_Val : Uint;
15515 Base_Typ : Entity_Id;
15516 Implicit_Base : Entity_Id;
15517 Bound : Node_Id;
15519 function Can_Derive_From (E : Entity_Id) return Boolean;
15520 -- Find if given digits value, and possibly a specified range, allows
15521 -- derivation from specified type
15523 function Find_Base_Type return Entity_Id;
15524 -- Find a predefined base type that Def can derive from, or generate
15525 -- an error and substitute Long_Long_Float if none exists.
15527 ---------------------
15528 -- Can_Derive_From --
15529 ---------------------
15531 function Can_Derive_From (E : Entity_Id) return Boolean is
15532 Spec : constant Entity_Id := Real_Range_Specification (Def);
15534 begin
15535 -- Check specified "digits" constraint
15537 if Digs_Val > Digits_Value (E) then
15538 return False;
15539 end if;
15541 -- Avoid types not matching pragma Float_Representation, if present
15543 if (Opt.Float_Format = 'I' and then Float_Rep (E) /= IEEE_Binary)
15544 or else
15545 (Opt.Float_Format = 'V' and then Float_Rep (E) /= VAX_Native)
15546 then
15547 return False;
15548 end if;
15550 -- Check for matching range, if specified
15552 if Present (Spec) then
15553 if Expr_Value_R (Type_Low_Bound (E)) >
15554 Expr_Value_R (Low_Bound (Spec))
15555 then
15556 return False;
15557 end if;
15559 if Expr_Value_R (Type_High_Bound (E)) <
15560 Expr_Value_R (High_Bound (Spec))
15561 then
15562 return False;
15563 end if;
15564 end if;
15566 return True;
15567 end Can_Derive_From;
15569 --------------------
15570 -- Find_Base_Type --
15571 --------------------
15573 function Find_Base_Type return Entity_Id is
15574 Choice : Elmt_Id := First_Elmt (Predefined_Float_Types);
15576 begin
15577 -- Iterate over the predefined types in order, returning the first
15578 -- one that Def can derive from.
15580 while Present (Choice) loop
15581 if Can_Derive_From (Node (Choice)) then
15582 return Node (Choice);
15583 end if;
15585 Next_Elmt (Choice);
15586 end loop;
15588 -- If we can't derive from any existing type, use Long_Long_Float
15589 -- and give appropriate message explaining the problem.
15591 if Digs_Val > Max_Digs_Val then
15592 -- It might be the case that there is a type with the requested
15593 -- range, just not the combination of digits and range.
15595 Error_Msg_N
15596 ("no predefined type has requested range and precision",
15597 Real_Range_Specification (Def));
15599 else
15600 Error_Msg_N
15601 ("range too large for any predefined type",
15602 Real_Range_Specification (Def));
15603 end if;
15605 return Standard_Long_Long_Float;
15606 end Find_Base_Type;
15608 -- Start of processing for Floating_Point_Type_Declaration
15610 begin
15611 Check_Restriction (No_Floating_Point, Def);
15613 -- Create an implicit base type
15615 Implicit_Base :=
15616 Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
15618 -- Analyze and verify digits value
15620 Analyze_And_Resolve (Digs, Any_Integer);
15621 Check_Digits_Expression (Digs);
15622 Digs_Val := Expr_Value (Digs);
15624 -- Process possible range spec and find correct type to derive from
15626 Process_Real_Range_Specification (Def);
15628 -- Check that requested number of digits is not too high.
15630 if Digs_Val > Max_Digs_Val then
15631 -- The check for Max_Base_Digits may be somewhat expensive, as it
15632 -- requires reading System, so only do it when necessary.
15634 declare
15635 Max_Base_Digits : constant Uint :=
15636 Expr_Value
15637 (Expression
15638 (Parent (RTE (RE_Max_Base_Digits))));
15640 begin
15641 if Digs_Val > Max_Base_Digits then
15642 Error_Msg_Uint_1 := Max_Base_Digits;
15643 Error_Msg_N ("digits value out of range, maximum is ^", Digs);
15645 elsif No (Real_Range_Specification (Def)) then
15646 Error_Msg_Uint_1 := Max_Digs_Val;
15647 Error_Msg_N ("types with more than ^ digits need range spec "
15648 & "(RM 3.5.7(6))", Digs);
15649 end if;
15650 end;
15651 end if;
15653 -- Find a suitable type to derive from or complain and use a substitute
15655 Base_Typ := Find_Base_Type;
15657 -- If there are bounds given in the declaration use them as the bounds
15658 -- of the type, otherwise use the bounds of the predefined base type
15659 -- that was chosen based on the Digits value.
15661 if Present (Real_Range_Specification (Def)) then
15662 Set_Scalar_Range (T, Real_Range_Specification (Def));
15663 Set_Is_Constrained (T);
15665 -- The bounds of this range must be converted to machine numbers
15666 -- in accordance with RM 4.9(38).
15668 Bound := Type_Low_Bound (T);
15670 if Nkind (Bound) = N_Real_Literal then
15671 Set_Realval
15672 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
15673 Set_Is_Machine_Number (Bound);
15674 end if;
15676 Bound := Type_High_Bound (T);
15678 if Nkind (Bound) = N_Real_Literal then
15679 Set_Realval
15680 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
15681 Set_Is_Machine_Number (Bound);
15682 end if;
15684 else
15685 Set_Scalar_Range (T, Scalar_Range (Base_Typ));
15686 end if;
15688 -- Complete definition of implicit base and declared first subtype
15690 Set_Etype (Implicit_Base, Base_Typ);
15692 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
15693 Set_Size_Info (Implicit_Base, (Base_Typ));
15694 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
15695 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
15696 Set_Digits_Value (Implicit_Base, Digits_Value (Base_Typ));
15697 Set_Float_Rep (Implicit_Base, Float_Rep (Base_Typ));
15699 Set_Ekind (T, E_Floating_Point_Subtype);
15700 Set_Etype (T, Implicit_Base);
15702 Set_Size_Info (T, (Implicit_Base));
15703 Set_RM_Size (T, RM_Size (Implicit_Base));
15704 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
15705 Set_Digits_Value (T, Digs_Val);
15706 end Floating_Point_Type_Declaration;
15708 ----------------------------
15709 -- Get_Discriminant_Value --
15710 ----------------------------
15712 -- This is the situation:
15714 -- There is a non-derived type
15716 -- type T0 (Dx, Dy, Dz...)
15718 -- There are zero or more levels of derivation, with each derivation
15719 -- either purely inheriting the discriminants, or defining its own.
15721 -- type Ti is new Ti-1
15722 -- or
15723 -- type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
15724 -- or
15725 -- subtype Ti is ...
15727 -- The subtype issue is avoided by the use of Original_Record_Component,
15728 -- and the fact that derived subtypes also derive the constraints.
15730 -- This chain leads back from
15732 -- Typ_For_Constraint
15734 -- Typ_For_Constraint has discriminants, and the value for each
15735 -- discriminant is given by its corresponding Elmt of Constraints.
15737 -- Discriminant is some discriminant in this hierarchy
15739 -- We need to return its value
15741 -- We do this by recursively searching each level, and looking for
15742 -- Discriminant. Once we get to the bottom, we start backing up
15743 -- returning the value for it which may in turn be a discriminant
15744 -- further up, so on the backup we continue the substitution.
15746 function Get_Discriminant_Value
15747 (Discriminant : Entity_Id;
15748 Typ_For_Constraint : Entity_Id;
15749 Constraint : Elist_Id) return Node_Id
15751 function Root_Corresponding_Discriminant
15752 (Discr : Entity_Id) return Entity_Id;
15753 -- Given a discriminant, traverse the chain of inherited discriminants
15754 -- and return the topmost discriminant.
15756 function Search_Derivation_Levels
15757 (Ti : Entity_Id;
15758 Discrim_Values : Elist_Id;
15759 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
15760 -- This is the routine that performs the recursive search of levels
15761 -- as described above.
15763 -------------------------------------
15764 -- Root_Corresponding_Discriminant --
15765 -------------------------------------
15767 function Root_Corresponding_Discriminant
15768 (Discr : Entity_Id) return Entity_Id
15770 D : Entity_Id;
15772 begin
15773 D := Discr;
15774 while Present (Corresponding_Discriminant (D)) loop
15775 D := Corresponding_Discriminant (D);
15776 end loop;
15778 return D;
15779 end Root_Corresponding_Discriminant;
15781 ------------------------------
15782 -- Search_Derivation_Levels --
15783 ------------------------------
15785 function Search_Derivation_Levels
15786 (Ti : Entity_Id;
15787 Discrim_Values : Elist_Id;
15788 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
15790 Assoc : Elmt_Id;
15791 Disc : Entity_Id;
15792 Result : Node_Or_Entity_Id;
15793 Result_Entity : Node_Id;
15795 begin
15796 -- If inappropriate type, return Error, this happens only in
15797 -- cascaded error situations, and we want to avoid a blow up.
15799 if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
15800 return Error;
15801 end if;
15803 -- Look deeper if possible. Use Stored_Constraints only for
15804 -- untagged types. For tagged types use the given constraint.
15805 -- This asymmetry needs explanation???
15807 if not Stored_Discrim_Values
15808 and then Present (Stored_Constraint (Ti))
15809 and then not Is_Tagged_Type (Ti)
15810 then
15811 Result :=
15812 Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
15813 else
15814 declare
15815 Td : constant Entity_Id := Etype (Ti);
15817 begin
15818 if Td = Ti then
15819 Result := Discriminant;
15821 else
15822 if Present (Stored_Constraint (Ti)) then
15823 Result :=
15824 Search_Derivation_Levels
15825 (Td, Stored_Constraint (Ti), True);
15826 else
15827 Result :=
15828 Search_Derivation_Levels
15829 (Td, Discrim_Values, Stored_Discrim_Values);
15830 end if;
15831 end if;
15832 end;
15833 end if;
15835 -- Extra underlying places to search, if not found above. For
15836 -- concurrent types, the relevant discriminant appears in the
15837 -- corresponding record. For a type derived from a private type
15838 -- without discriminant, the full view inherits the discriminants
15839 -- of the full view of the parent.
15841 if Result = Discriminant then
15842 if Is_Concurrent_Type (Ti)
15843 and then Present (Corresponding_Record_Type (Ti))
15844 then
15845 Result :=
15846 Search_Derivation_Levels (
15847 Corresponding_Record_Type (Ti),
15848 Discrim_Values,
15849 Stored_Discrim_Values);
15851 elsif Is_Private_Type (Ti)
15852 and then not Has_Discriminants (Ti)
15853 and then Present (Full_View (Ti))
15854 and then Etype (Full_View (Ti)) /= Ti
15855 then
15856 Result :=
15857 Search_Derivation_Levels (
15858 Full_View (Ti),
15859 Discrim_Values,
15860 Stored_Discrim_Values);
15861 end if;
15862 end if;
15864 -- If Result is not a (reference to a) discriminant, return it,
15865 -- otherwise set Result_Entity to the discriminant.
15867 if Nkind (Result) = N_Defining_Identifier then
15868 pragma Assert (Result = Discriminant);
15869 Result_Entity := Result;
15871 else
15872 if not Denotes_Discriminant (Result) then
15873 return Result;
15874 end if;
15876 Result_Entity := Entity (Result);
15877 end if;
15879 -- See if this level of derivation actually has discriminants
15880 -- because tagged derivations can add them, hence the lower
15881 -- levels need not have any.
15883 if not Has_Discriminants (Ti) then
15884 return Result;
15885 end if;
15887 -- Scan Ti's discriminants for Result_Entity,
15888 -- and return its corresponding value, if any.
15890 Result_Entity := Original_Record_Component (Result_Entity);
15892 Assoc := First_Elmt (Discrim_Values);
15894 if Stored_Discrim_Values then
15895 Disc := First_Stored_Discriminant (Ti);
15896 else
15897 Disc := First_Discriminant (Ti);
15898 end if;
15900 while Present (Disc) loop
15901 pragma Assert (Present (Assoc));
15903 if Original_Record_Component (Disc) = Result_Entity then
15904 return Node (Assoc);
15905 end if;
15907 Next_Elmt (Assoc);
15909 if Stored_Discrim_Values then
15910 Next_Stored_Discriminant (Disc);
15911 else
15912 Next_Discriminant (Disc);
15913 end if;
15914 end loop;
15916 -- Could not find it
15918 return Result;
15919 end Search_Derivation_Levels;
15921 -- Local Variables
15923 Result : Node_Or_Entity_Id;
15925 -- Start of processing for Get_Discriminant_Value
15927 begin
15928 -- ??? This routine is a gigantic mess and will be deleted. For the
15929 -- time being just test for the trivial case before calling recurse.
15931 if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
15932 declare
15933 D : Entity_Id;
15934 E : Elmt_Id;
15936 begin
15937 D := First_Discriminant (Typ_For_Constraint);
15938 E := First_Elmt (Constraint);
15939 while Present (D) loop
15940 if Chars (D) = Chars (Discriminant) then
15941 return Node (E);
15942 end if;
15944 Next_Discriminant (D);
15945 Next_Elmt (E);
15946 end loop;
15947 end;
15948 end if;
15950 Result := Search_Derivation_Levels
15951 (Typ_For_Constraint, Constraint, False);
15953 -- ??? hack to disappear when this routine is gone
15955 if Nkind (Result) = N_Defining_Identifier then
15956 declare
15957 D : Entity_Id;
15958 E : Elmt_Id;
15960 begin
15961 D := First_Discriminant (Typ_For_Constraint);
15962 E := First_Elmt (Constraint);
15963 while Present (D) loop
15964 if Root_Corresponding_Discriminant (D) = Discriminant then
15965 return Node (E);
15966 end if;
15968 Next_Discriminant (D);
15969 Next_Elmt (E);
15970 end loop;
15971 end;
15972 end if;
15974 pragma Assert (Nkind (Result) /= N_Defining_Identifier);
15975 return Result;
15976 end Get_Discriminant_Value;
15978 --------------------------
15979 -- Has_Range_Constraint --
15980 --------------------------
15982 function Has_Range_Constraint (N : Node_Id) return Boolean is
15983 C : constant Node_Id := Constraint (N);
15985 begin
15986 if Nkind (C) = N_Range_Constraint then
15987 return True;
15989 elsif Nkind (C) = N_Digits_Constraint then
15990 return
15991 Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
15992 or else
15993 Present (Range_Constraint (C));
15995 elsif Nkind (C) = N_Delta_Constraint then
15996 return Present (Range_Constraint (C));
15998 else
15999 return False;
16000 end if;
16001 end Has_Range_Constraint;
16003 ------------------------
16004 -- Inherit_Components --
16005 ------------------------
16007 function Inherit_Components
16008 (N : Node_Id;
16009 Parent_Base : Entity_Id;
16010 Derived_Base : Entity_Id;
16011 Is_Tagged : Boolean;
16012 Inherit_Discr : Boolean;
16013 Discs : Elist_Id) return Elist_Id
16015 Assoc_List : constant Elist_Id := New_Elmt_List;
16017 procedure Inherit_Component
16018 (Old_C : Entity_Id;
16019 Plain_Discrim : Boolean := False;
16020 Stored_Discrim : Boolean := False);
16021 -- Inherits component Old_C from Parent_Base to the Derived_Base. If
16022 -- Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
16023 -- True, Old_C is a stored discriminant. If they are both false then
16024 -- Old_C is a regular component.
16026 -----------------------
16027 -- Inherit_Component --
16028 -----------------------
16030 procedure Inherit_Component
16031 (Old_C : Entity_Id;
16032 Plain_Discrim : Boolean := False;
16033 Stored_Discrim : Boolean := False)
16035 procedure Set_Anonymous_Type (Id : Entity_Id);
16036 -- Id denotes the entity of an access discriminant or anonymous
16037 -- access component. Set the type of Id to either the same type of
16038 -- Old_C or create a new one depending on whether the parent and
16039 -- the child types are in the same scope.
16041 ------------------------
16042 -- Set_Anonymous_Type --
16043 ------------------------
16045 procedure Set_Anonymous_Type (Id : Entity_Id) is
16046 Old_Typ : constant Entity_Id := Etype (Old_C);
16048 begin
16049 if Scope (Parent_Base) = Scope (Derived_Base) then
16050 Set_Etype (Id, Old_Typ);
16052 -- The parent and the derived type are in two different scopes.
16053 -- Reuse the type of the original discriminant / component by
16054 -- copying it in order to preserve all attributes.
16056 else
16057 declare
16058 Typ : constant Entity_Id := New_Copy (Old_Typ);
16060 begin
16061 Set_Etype (Id, Typ);
16063 -- Since we do not generate component declarations for
16064 -- inherited components, associate the itype with the
16065 -- derived type.
16067 Set_Associated_Node_For_Itype (Typ, Parent (Derived_Base));
16068 Set_Scope (Typ, Derived_Base);
16069 end;
16070 end if;
16071 end Set_Anonymous_Type;
16073 -- Local variables and constants
16075 New_C : constant Entity_Id := New_Copy (Old_C);
16077 Corr_Discrim : Entity_Id;
16078 Discrim : Entity_Id;
16080 -- Start of processing for Inherit_Component
16082 begin
16083 pragma Assert (not Is_Tagged or else not Stored_Discrim);
16085 Set_Parent (New_C, Parent (Old_C));
16087 -- Regular discriminants and components must be inserted in the scope
16088 -- of the Derived_Base. Do it here.
16090 if not Stored_Discrim then
16091 Enter_Name (New_C);
16092 end if;
16094 -- For tagged types the Original_Record_Component must point to
16095 -- whatever this field was pointing to in the parent type. This has
16096 -- already been achieved by the call to New_Copy above.
16098 if not Is_Tagged then
16099 Set_Original_Record_Component (New_C, New_C);
16100 end if;
16102 -- Set the proper type of an access discriminant
16104 if Ekind (New_C) = E_Discriminant
16105 and then Ekind (Etype (New_C)) = E_Anonymous_Access_Type
16106 then
16107 Set_Anonymous_Type (New_C);
16108 end if;
16110 -- If we have inherited a component then see if its Etype contains
16111 -- references to Parent_Base discriminants. In this case, replace
16112 -- these references with the constraints given in Discs. We do not
16113 -- do this for the partial view of private types because this is
16114 -- not needed (only the components of the full view will be used
16115 -- for code generation) and cause problem. We also avoid this
16116 -- transformation in some error situations.
16118 if Ekind (New_C) = E_Component then
16120 -- Set the proper type of an anonymous access component
16122 if Ekind (Etype (New_C)) = E_Anonymous_Access_Type then
16123 Set_Anonymous_Type (New_C);
16125 elsif (Is_Private_Type (Derived_Base)
16126 and then not Is_Generic_Type (Derived_Base))
16127 or else (Is_Empty_Elmt_List (Discs)
16128 and then not Expander_Active)
16129 then
16130 Set_Etype (New_C, Etype (Old_C));
16132 else
16133 -- The current component introduces a circularity of the
16134 -- following kind:
16136 -- limited with Pack_2;
16137 -- package Pack_1 is
16138 -- type T_1 is tagged record
16139 -- Comp : access Pack_2.T_2;
16140 -- ...
16141 -- end record;
16142 -- end Pack_1;
16144 -- with Pack_1;
16145 -- package Pack_2 is
16146 -- type T_2 is new Pack_1.T_1 with ...;
16147 -- end Pack_2;
16149 Set_Etype
16150 (New_C,
16151 Constrain_Component_Type
16152 (Old_C, Derived_Base, N, Parent_Base, Discs));
16153 end if;
16154 end if;
16156 -- In derived tagged types it is illegal to reference a non
16157 -- discriminant component in the parent type. To catch this, mark
16158 -- these components with an Ekind of E_Void. This will be reset in
16159 -- Record_Type_Definition after processing the record extension of
16160 -- the derived type.
16162 -- If the declaration is a private extension, there is no further
16163 -- record extension to process, and the components retain their
16164 -- current kind, because they are visible at this point.
16166 if Is_Tagged and then Ekind (New_C) = E_Component
16167 and then Nkind (N) /= N_Private_Extension_Declaration
16168 then
16169 Set_Ekind (New_C, E_Void);
16170 end if;
16172 if Plain_Discrim then
16173 Set_Corresponding_Discriminant (New_C, Old_C);
16174 Build_Discriminal (New_C);
16176 -- If we are explicitly inheriting a stored discriminant it will be
16177 -- completely hidden.
16179 elsif Stored_Discrim then
16180 Set_Corresponding_Discriminant (New_C, Empty);
16181 Set_Discriminal (New_C, Empty);
16182 Set_Is_Completely_Hidden (New_C);
16184 -- Set the Original_Record_Component of each discriminant in the
16185 -- derived base to point to the corresponding stored that we just
16186 -- created.
16188 Discrim := First_Discriminant (Derived_Base);
16189 while Present (Discrim) loop
16190 Corr_Discrim := Corresponding_Discriminant (Discrim);
16192 -- Corr_Discrim could be missing in an error situation
16194 if Present (Corr_Discrim)
16195 and then Original_Record_Component (Corr_Discrim) = Old_C
16196 then
16197 Set_Original_Record_Component (Discrim, New_C);
16198 end if;
16200 Next_Discriminant (Discrim);
16201 end loop;
16203 Append_Entity (New_C, Derived_Base);
16204 end if;
16206 if not Is_Tagged then
16207 Append_Elmt (Old_C, Assoc_List);
16208 Append_Elmt (New_C, Assoc_List);
16209 end if;
16210 end Inherit_Component;
16212 -- Variables local to Inherit_Component
16214 Loc : constant Source_Ptr := Sloc (N);
16216 Parent_Discrim : Entity_Id;
16217 Stored_Discrim : Entity_Id;
16218 D : Entity_Id;
16219 Component : Entity_Id;
16221 -- Start of processing for Inherit_Components
16223 begin
16224 if not Is_Tagged then
16225 Append_Elmt (Parent_Base, Assoc_List);
16226 Append_Elmt (Derived_Base, Assoc_List);
16227 end if;
16229 -- Inherit parent discriminants if needed
16231 if Inherit_Discr then
16232 Parent_Discrim := First_Discriminant (Parent_Base);
16233 while Present (Parent_Discrim) loop
16234 Inherit_Component (Parent_Discrim, Plain_Discrim => True);
16235 Next_Discriminant (Parent_Discrim);
16236 end loop;
16237 end if;
16239 -- Create explicit stored discrims for untagged types when necessary
16241 if not Has_Unknown_Discriminants (Derived_Base)
16242 and then Has_Discriminants (Parent_Base)
16243 and then not Is_Tagged
16244 and then
16245 (not Inherit_Discr
16246 or else First_Discriminant (Parent_Base) /=
16247 First_Stored_Discriminant (Parent_Base))
16248 then
16249 Stored_Discrim := First_Stored_Discriminant (Parent_Base);
16250 while Present (Stored_Discrim) loop
16251 Inherit_Component (Stored_Discrim, Stored_Discrim => True);
16252 Next_Stored_Discriminant (Stored_Discrim);
16253 end loop;
16254 end if;
16256 -- See if we can apply the second transformation for derived types, as
16257 -- explained in point 6. in the comments above Build_Derived_Record_Type
16258 -- This is achieved by appending Derived_Base discriminants into Discs,
16259 -- which has the side effect of returning a non empty Discs list to the
16260 -- caller of Inherit_Components, which is what we want. This must be
16261 -- done for private derived types if there are explicit stored
16262 -- discriminants, to ensure that we can retrieve the values of the
16263 -- constraints provided in the ancestors.
16265 if Inherit_Discr
16266 and then Is_Empty_Elmt_List (Discs)
16267 and then Present (First_Discriminant (Derived_Base))
16268 and then
16269 (not Is_Private_Type (Derived_Base)
16270 or else Is_Completely_Hidden
16271 (First_Stored_Discriminant (Derived_Base))
16272 or else Is_Generic_Type (Derived_Base))
16273 then
16274 D := First_Discriminant (Derived_Base);
16275 while Present (D) loop
16276 Append_Elmt (New_Reference_To (D, Loc), Discs);
16277 Next_Discriminant (D);
16278 end loop;
16279 end if;
16281 -- Finally, inherit non-discriminant components unless they are not
16282 -- visible because defined or inherited from the full view of the
16283 -- parent. Don't inherit the _parent field of the parent type.
16285 Component := First_Entity (Parent_Base);
16286 while Present (Component) loop
16288 -- Ada 2005 (AI-251): Do not inherit components associated with
16289 -- secondary tags of the parent.
16291 if Ekind (Component) = E_Component
16292 and then Present (Related_Type (Component))
16293 then
16294 null;
16296 elsif Ekind (Component) /= E_Component
16297 or else Chars (Component) = Name_uParent
16298 then
16299 null;
16301 -- If the derived type is within the parent type's declarative
16302 -- region, then the components can still be inherited even though
16303 -- they aren't visible at this point. This can occur for cases
16304 -- such as within public child units where the components must
16305 -- become visible upon entering the child unit's private part.
16307 elsif not Is_Visible_Component (Component)
16308 and then not In_Open_Scopes (Scope (Parent_Base))
16309 then
16310 null;
16312 elsif Ekind_In (Derived_Base, E_Private_Type,
16313 E_Limited_Private_Type)
16314 then
16315 null;
16317 else
16318 Inherit_Component (Component);
16319 end if;
16321 Next_Entity (Component);
16322 end loop;
16324 -- For tagged derived types, inherited discriminants cannot be used in
16325 -- component declarations of the record extension part. To achieve this
16326 -- we mark the inherited discriminants as not visible.
16328 if Is_Tagged and then Inherit_Discr then
16329 D := First_Discriminant (Derived_Base);
16330 while Present (D) loop
16331 Set_Is_Immediately_Visible (D, False);
16332 Next_Discriminant (D);
16333 end loop;
16334 end if;
16336 return Assoc_List;
16337 end Inherit_Components;
16339 -----------------------
16340 -- Is_Null_Extension --
16341 -----------------------
16343 function Is_Null_Extension (T : Entity_Id) return Boolean is
16344 Type_Decl : constant Node_Id := Parent (Base_Type (T));
16345 Comp_List : Node_Id;
16346 Comp : Node_Id;
16348 begin
16349 if Nkind (Type_Decl) /= N_Full_Type_Declaration
16350 or else not Is_Tagged_Type (T)
16351 or else Nkind (Type_Definition (Type_Decl)) /=
16352 N_Derived_Type_Definition
16353 or else No (Record_Extension_Part (Type_Definition (Type_Decl)))
16354 then
16355 return False;
16356 end if;
16358 Comp_List :=
16359 Component_List (Record_Extension_Part (Type_Definition (Type_Decl)));
16361 if Present (Discriminant_Specifications (Type_Decl)) then
16362 return False;
16364 elsif Present (Comp_List)
16365 and then Is_Non_Empty_List (Component_Items (Comp_List))
16366 then
16367 Comp := First (Component_Items (Comp_List));
16369 -- Only user-defined components are relevant. The component list
16370 -- may also contain a parent component and internal components
16371 -- corresponding to secondary tags, but these do not determine
16372 -- whether this is a null extension.
16374 while Present (Comp) loop
16375 if Comes_From_Source (Comp) then
16376 return False;
16377 end if;
16379 Next (Comp);
16380 end loop;
16382 return True;
16383 else
16384 return True;
16385 end if;
16386 end Is_Null_Extension;
16388 ------------------------------
16389 -- Is_Valid_Constraint_Kind --
16390 ------------------------------
16392 function Is_Valid_Constraint_Kind
16393 (T_Kind : Type_Kind;
16394 Constraint_Kind : Node_Kind) return Boolean
16396 begin
16397 case T_Kind is
16398 when Enumeration_Kind |
16399 Integer_Kind =>
16400 return Constraint_Kind = N_Range_Constraint;
16402 when Decimal_Fixed_Point_Kind =>
16403 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
16404 N_Range_Constraint);
16406 when Ordinary_Fixed_Point_Kind =>
16407 return Nkind_In (Constraint_Kind, N_Delta_Constraint,
16408 N_Range_Constraint);
16410 when Float_Kind =>
16411 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
16412 N_Range_Constraint);
16414 when Access_Kind |
16415 Array_Kind |
16416 E_Record_Type |
16417 E_Record_Subtype |
16418 Class_Wide_Kind |
16419 E_Incomplete_Type |
16420 Private_Kind |
16421 Concurrent_Kind =>
16422 return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
16424 when others =>
16425 return True; -- Error will be detected later
16426 end case;
16427 end Is_Valid_Constraint_Kind;
16429 --------------------------
16430 -- Is_Visible_Component --
16431 --------------------------
16433 function Is_Visible_Component
16434 (C : Entity_Id;
16435 N : Node_Id := Empty) return Boolean
16437 Original_Comp : Entity_Id := Empty;
16438 Original_Scope : Entity_Id;
16439 Type_Scope : Entity_Id;
16441 function Is_Local_Type (Typ : Entity_Id) return Boolean;
16442 -- Check whether parent type of inherited component is declared locally,
16443 -- possibly within a nested package or instance. The current scope is
16444 -- the derived record itself.
16446 -------------------
16447 -- Is_Local_Type --
16448 -------------------
16450 function Is_Local_Type (Typ : Entity_Id) return Boolean is
16451 Scop : Entity_Id;
16453 begin
16454 Scop := Scope (Typ);
16455 while Present (Scop)
16456 and then Scop /= Standard_Standard
16457 loop
16458 if Scop = Scope (Current_Scope) then
16459 return True;
16460 end if;
16462 Scop := Scope (Scop);
16463 end loop;
16465 return False;
16466 end Is_Local_Type;
16468 -- Start of processing for Is_Visible_Component
16470 begin
16471 if Ekind_In (C, E_Component, E_Discriminant) then
16472 Original_Comp := Original_Record_Component (C);
16473 end if;
16475 if No (Original_Comp) then
16477 -- Premature usage, or previous error
16479 return False;
16481 else
16482 Original_Scope := Scope (Original_Comp);
16483 Type_Scope := Scope (Base_Type (Scope (C)));
16484 end if;
16486 -- For an untagged type derived from a private type, the only visible
16487 -- components are new discriminants. In an instance all components are
16488 -- visible (see Analyze_Selected_Component).
16490 if not Is_Tagged_Type (Original_Scope) then
16491 return not Has_Private_Ancestor (Original_Scope)
16492 or else In_Open_Scopes (Scope (Original_Scope))
16493 or else In_Instance
16494 or else (Ekind (Original_Comp) = E_Discriminant
16495 and then Original_Scope = Type_Scope);
16497 -- If it is _Parent or _Tag, there is no visibility issue
16499 elsif not Comes_From_Source (Original_Comp) then
16500 return True;
16502 -- Discriminants are visible unless the (private) type has unknown
16503 -- discriminants. If the discriminant reference is inserted for a
16504 -- discriminant check on a full view it is also visible.
16506 elsif Ekind (Original_Comp) = E_Discriminant
16507 and then
16508 (not Has_Unknown_Discriminants (Original_Scope)
16509 or else (Present (N)
16510 and then Nkind (N) = N_Selected_Component
16511 and then Nkind (Prefix (N)) = N_Type_Conversion
16512 and then not Comes_From_Source (Prefix (N))))
16513 then
16514 return True;
16516 -- In the body of an instantiation, no need to check for the visibility
16517 -- of a component.
16519 elsif In_Instance_Body then
16520 return True;
16522 -- If the component has been declared in an ancestor which is currently
16523 -- a private type, then it is not visible. The same applies if the
16524 -- component's containing type is not in an open scope and the original
16525 -- component's enclosing type is a visible full view of a private type
16526 -- (which can occur in cases where an attempt is being made to reference
16527 -- a component in a sibling package that is inherited from a visible
16528 -- component of a type in an ancestor package; the component in the
16529 -- sibling package should not be visible even though the component it
16530 -- inherited from is visible). This does not apply however in the case
16531 -- where the scope of the type is a private child unit, or when the
16532 -- parent comes from a local package in which the ancestor is currently
16533 -- visible. The latter suppression of visibility is needed for cases
16534 -- that are tested in B730006.
16536 elsif Is_Private_Type (Original_Scope)
16537 or else
16538 (not Is_Private_Descendant (Type_Scope)
16539 and then not In_Open_Scopes (Type_Scope)
16540 and then Has_Private_Declaration (Original_Scope))
16541 then
16542 -- If the type derives from an entity in a formal package, there
16543 -- are no additional visible components.
16545 if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
16546 N_Formal_Package_Declaration
16547 then
16548 return False;
16550 -- if we are not in the private part of the current package, there
16551 -- are no additional visible components.
16553 elsif Ekind (Scope (Current_Scope)) = E_Package
16554 and then not In_Private_Part (Scope (Current_Scope))
16555 then
16556 return False;
16557 else
16558 return
16559 Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
16560 and then In_Open_Scopes (Scope (Original_Scope))
16561 and then Is_Local_Type (Type_Scope);
16562 end if;
16564 -- There is another weird way in which a component may be invisible when
16565 -- the private and the full view are not derived from the same ancestor.
16566 -- Here is an example :
16568 -- type A1 is tagged record F1 : integer; end record;
16569 -- type A2 is new A1 with record F2 : integer; end record;
16570 -- type T is new A1 with private;
16571 -- private
16572 -- type T is new A2 with null record;
16574 -- In this case, the full view of T inherits F1 and F2 but the private
16575 -- view inherits only F1
16577 else
16578 declare
16579 Ancestor : Entity_Id := Scope (C);
16581 begin
16582 loop
16583 if Ancestor = Original_Scope then
16584 return True;
16585 elsif Ancestor = Etype (Ancestor) then
16586 return False;
16587 end if;
16589 Ancestor := Etype (Ancestor);
16590 end loop;
16591 end;
16592 end if;
16593 end Is_Visible_Component;
16595 --------------------------
16596 -- Make_Class_Wide_Type --
16597 --------------------------
16599 procedure Make_Class_Wide_Type (T : Entity_Id) is
16600 CW_Type : Entity_Id;
16601 CW_Name : Name_Id;
16602 Next_E : Entity_Id;
16604 begin
16605 if Present (Class_Wide_Type (T)) then
16607 -- The class-wide type is a partially decorated entity created for a
16608 -- unanalyzed tagged type referenced through a limited with clause.
16609 -- When the tagged type is analyzed, its class-wide type needs to be
16610 -- redecorated. Note that we reuse the entity created by Decorate_
16611 -- Tagged_Type in order to preserve all links.
16613 if Materialize_Entity (Class_Wide_Type (T)) then
16614 CW_Type := Class_Wide_Type (T);
16615 Set_Materialize_Entity (CW_Type, False);
16617 -- The class wide type can have been defined by the partial view, in
16618 -- which case everything is already done.
16620 else
16621 return;
16622 end if;
16624 -- Default case, we need to create a new class-wide type
16626 else
16627 CW_Type :=
16628 New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
16629 end if;
16631 -- Inherit root type characteristics
16633 CW_Name := Chars (CW_Type);
16634 Next_E := Next_Entity (CW_Type);
16635 Copy_Node (T, CW_Type);
16636 Set_Comes_From_Source (CW_Type, False);
16637 Set_Chars (CW_Type, CW_Name);
16638 Set_Parent (CW_Type, Parent (T));
16639 Set_Next_Entity (CW_Type, Next_E);
16641 -- Ensure we have a new freeze node for the class-wide type. The partial
16642 -- view may have freeze action of its own, requiring a proper freeze
16643 -- node, and the same freeze node cannot be shared between the two
16644 -- types.
16646 Set_Has_Delayed_Freeze (CW_Type);
16647 Set_Freeze_Node (CW_Type, Empty);
16649 -- Customize the class-wide type: It has no prim. op., it cannot be
16650 -- abstract and its Etype points back to the specific root type.
16652 Set_Ekind (CW_Type, E_Class_Wide_Type);
16653 Set_Is_Tagged_Type (CW_Type, True);
16654 Set_Direct_Primitive_Operations (CW_Type, New_Elmt_List);
16655 Set_Is_Abstract_Type (CW_Type, False);
16656 Set_Is_Constrained (CW_Type, False);
16657 Set_Is_First_Subtype (CW_Type, Is_First_Subtype (T));
16659 if Ekind (T) = E_Class_Wide_Subtype then
16660 Set_Etype (CW_Type, Etype (Base_Type (T)));
16661 else
16662 Set_Etype (CW_Type, T);
16663 end if;
16665 -- If this is the class_wide type of a constrained subtype, it does
16666 -- not have discriminants.
16668 Set_Has_Discriminants (CW_Type,
16669 Has_Discriminants (T) and then not Is_Constrained (T));
16671 Set_Has_Unknown_Discriminants (CW_Type, True);
16672 Set_Class_Wide_Type (T, CW_Type);
16673 Set_Equivalent_Type (CW_Type, Empty);
16675 -- The class-wide type of a class-wide type is itself (RM 3.9(14))
16677 Set_Class_Wide_Type (CW_Type, CW_Type);
16678 end Make_Class_Wide_Type;
16680 ----------------
16681 -- Make_Index --
16682 ----------------
16684 procedure Make_Index
16685 (I : Node_Id;
16686 Related_Nod : Node_Id;
16687 Related_Id : Entity_Id := Empty;
16688 Suffix_Index : Nat := 1;
16689 In_Iter_Schm : Boolean := False)
16691 R : Node_Id;
16692 T : Entity_Id;
16693 Def_Id : Entity_Id := Empty;
16694 Found : Boolean := False;
16696 begin
16697 -- For a discrete range used in a constrained array definition and
16698 -- defined by a range, an implicit conversion to the predefined type
16699 -- INTEGER is assumed if each bound is either a numeric literal, a named
16700 -- number, or an attribute, and the type of both bounds (prior to the
16701 -- implicit conversion) is the type universal_integer. Otherwise, both
16702 -- bounds must be of the same discrete type, other than universal
16703 -- integer; this type must be determinable independently of the
16704 -- context, but using the fact that the type must be discrete and that
16705 -- both bounds must have the same type.
16707 -- Character literals also have a universal type in the absence of
16708 -- of additional context, and are resolved to Standard_Character.
16710 if Nkind (I) = N_Range then
16712 -- The index is given by a range constraint. The bounds are known
16713 -- to be of a consistent type.
16715 if not Is_Overloaded (I) then
16716 T := Etype (I);
16718 -- For universal bounds, choose the specific predefined type
16720 if T = Universal_Integer then
16721 T := Standard_Integer;
16723 elsif T = Any_Character then
16724 Ambiguous_Character (Low_Bound (I));
16726 T := Standard_Character;
16727 end if;
16729 -- The node may be overloaded because some user-defined operators
16730 -- are available, but if a universal interpretation exists it is
16731 -- also the selected one.
16733 elsif Universal_Interpretation (I) = Universal_Integer then
16734 T := Standard_Integer;
16736 else
16737 T := Any_Type;
16739 declare
16740 Ind : Interp_Index;
16741 It : Interp;
16743 begin
16744 Get_First_Interp (I, Ind, It);
16745 while Present (It.Typ) loop
16746 if Is_Discrete_Type (It.Typ) then
16748 if Found
16749 and then not Covers (It.Typ, T)
16750 and then not Covers (T, It.Typ)
16751 then
16752 Error_Msg_N ("ambiguous bounds in discrete range", I);
16753 exit;
16754 else
16755 T := It.Typ;
16756 Found := True;
16757 end if;
16758 end if;
16760 Get_Next_Interp (Ind, It);
16761 end loop;
16763 if T = Any_Type then
16764 Error_Msg_N ("discrete type required for range", I);
16765 Set_Etype (I, Any_Type);
16766 return;
16768 elsif T = Universal_Integer then
16769 T := Standard_Integer;
16770 end if;
16771 end;
16772 end if;
16774 if not Is_Discrete_Type (T) then
16775 Error_Msg_N ("discrete type required for range", I);
16776 Set_Etype (I, Any_Type);
16777 return;
16778 end if;
16780 if Nkind (Low_Bound (I)) = N_Attribute_Reference
16781 and then Attribute_Name (Low_Bound (I)) = Name_First
16782 and then Is_Entity_Name (Prefix (Low_Bound (I)))
16783 and then Is_Type (Entity (Prefix (Low_Bound (I))))
16784 and then Is_Discrete_Type (Entity (Prefix (Low_Bound (I))))
16785 then
16786 -- The type of the index will be the type of the prefix, as long
16787 -- as the upper bound is 'Last of the same type.
16789 Def_Id := Entity (Prefix (Low_Bound (I)));
16791 if Nkind (High_Bound (I)) /= N_Attribute_Reference
16792 or else Attribute_Name (High_Bound (I)) /= Name_Last
16793 or else not Is_Entity_Name (Prefix (High_Bound (I)))
16794 or else Entity (Prefix (High_Bound (I))) /= Def_Id
16795 then
16796 Def_Id := Empty;
16797 end if;
16798 end if;
16800 R := I;
16801 Process_Range_Expr_In_Decl (R, T, In_Iter_Schm => In_Iter_Schm);
16803 elsif Nkind (I) = N_Subtype_Indication then
16805 -- The index is given by a subtype with a range constraint
16807 T := Base_Type (Entity (Subtype_Mark (I)));
16809 if not Is_Discrete_Type (T) then
16810 Error_Msg_N ("discrete type required for range", I);
16811 Set_Etype (I, Any_Type);
16812 return;
16813 end if;
16815 R := Range_Expression (Constraint (I));
16817 Resolve (R, T);
16818 Process_Range_Expr_In_Decl
16819 (R, Entity (Subtype_Mark (I)), In_Iter_Schm => In_Iter_Schm);
16821 elsif Nkind (I) = N_Attribute_Reference then
16823 -- The parser guarantees that the attribute is a RANGE attribute
16825 -- If the node denotes the range of a type mark, that is also the
16826 -- resulting type, and we do no need to create an Itype for it.
16828 if Is_Entity_Name (Prefix (I))
16829 and then Comes_From_Source (I)
16830 and then Is_Type (Entity (Prefix (I)))
16831 and then Is_Discrete_Type (Entity (Prefix (I)))
16832 then
16833 Def_Id := Entity (Prefix (I));
16834 end if;
16836 Analyze_And_Resolve (I);
16837 T := Etype (I);
16838 R := I;
16840 -- If none of the above, must be a subtype. We convert this to a
16841 -- range attribute reference because in the case of declared first
16842 -- named subtypes, the types in the range reference can be different
16843 -- from the type of the entity. A range attribute normalizes the
16844 -- reference and obtains the correct types for the bounds.
16846 -- This transformation is in the nature of an expansion, is only
16847 -- done if expansion is active. In particular, it is not done on
16848 -- formal generic types, because we need to retain the name of the
16849 -- original index for instantiation purposes.
16851 else
16852 if not Is_Entity_Name (I) or else not Is_Type (Entity (I)) then
16853 Error_Msg_N ("invalid subtype mark in discrete range ", I);
16854 Set_Etype (I, Any_Integer);
16855 return;
16857 else
16858 -- The type mark may be that of an incomplete type. It is only
16859 -- now that we can get the full view, previous analysis does
16860 -- not look specifically for a type mark.
16862 Set_Entity (I, Get_Full_View (Entity (I)));
16863 Set_Etype (I, Entity (I));
16864 Def_Id := Entity (I);
16866 if not Is_Discrete_Type (Def_Id) then
16867 Error_Msg_N ("discrete type required for index", I);
16868 Set_Etype (I, Any_Type);
16869 return;
16870 end if;
16871 end if;
16873 if Expander_Active then
16874 Rewrite (I,
16875 Make_Attribute_Reference (Sloc (I),
16876 Attribute_Name => Name_Range,
16877 Prefix => Relocate_Node (I)));
16879 -- The original was a subtype mark that does not freeze. This
16880 -- means that the rewritten version must not freeze either.
16882 Set_Must_Not_Freeze (I);
16883 Set_Must_Not_Freeze (Prefix (I));
16884 Analyze_And_Resolve (I);
16885 T := Etype (I);
16886 R := I;
16888 -- If expander is inactive, type is legal, nothing else to construct
16890 else
16891 return;
16892 end if;
16893 end if;
16895 if not Is_Discrete_Type (T) then
16896 Error_Msg_N ("discrete type required for range", I);
16897 Set_Etype (I, Any_Type);
16898 return;
16900 elsif T = Any_Type then
16901 Set_Etype (I, Any_Type);
16902 return;
16903 end if;
16905 -- We will now create the appropriate Itype to describe the range, but
16906 -- first a check. If we originally had a subtype, then we just label
16907 -- the range with this subtype. Not only is there no need to construct
16908 -- a new subtype, but it is wrong to do so for two reasons:
16910 -- 1. A legality concern, if we have a subtype, it must not freeze,
16911 -- and the Itype would cause freezing incorrectly
16913 -- 2. An efficiency concern, if we created an Itype, it would not be
16914 -- recognized as the same type for the purposes of eliminating
16915 -- checks in some circumstances.
16917 -- We signal this case by setting the subtype entity in Def_Id
16919 if No (Def_Id) then
16920 Def_Id :=
16921 Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
16922 Set_Etype (Def_Id, Base_Type (T));
16924 if Is_Signed_Integer_Type (T) then
16925 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
16927 elsif Is_Modular_Integer_Type (T) then
16928 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
16930 else
16931 Set_Ekind (Def_Id, E_Enumeration_Subtype);
16932 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
16933 Set_First_Literal (Def_Id, First_Literal (T));
16934 end if;
16936 Set_Size_Info (Def_Id, (T));
16937 Set_RM_Size (Def_Id, RM_Size (T));
16938 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
16940 Set_Scalar_Range (Def_Id, R);
16941 Conditional_Delay (Def_Id, T);
16943 -- In the subtype indication case, if the immediate parent of the
16944 -- new subtype is non-static, then the subtype we create is non-
16945 -- static, even if its bounds are static.
16947 if Nkind (I) = N_Subtype_Indication
16948 and then not Is_Static_Subtype (Entity (Subtype_Mark (I)))
16949 then
16950 Set_Is_Non_Static_Subtype (Def_Id);
16951 end if;
16952 end if;
16954 -- Final step is to label the index with this constructed type
16956 Set_Etype (I, Def_Id);
16957 end Make_Index;
16959 ------------------------------
16960 -- Modular_Type_Declaration --
16961 ------------------------------
16963 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
16964 Mod_Expr : constant Node_Id := Expression (Def);
16965 M_Val : Uint;
16967 procedure Set_Modular_Size (Bits : Int);
16968 -- Sets RM_Size to Bits, and Esize to normal word size above this
16970 ----------------------
16971 -- Set_Modular_Size --
16972 ----------------------
16974 procedure Set_Modular_Size (Bits : Int) is
16975 begin
16976 Set_RM_Size (T, UI_From_Int (Bits));
16978 if Bits <= 8 then
16979 Init_Esize (T, 8);
16981 elsif Bits <= 16 then
16982 Init_Esize (T, 16);
16984 elsif Bits <= 32 then
16985 Init_Esize (T, 32);
16987 else
16988 Init_Esize (T, System_Max_Binary_Modulus_Power);
16989 end if;
16991 if not Non_Binary_Modulus (T)
16992 and then Esize (T) = RM_Size (T)
16993 then
16994 Set_Is_Known_Valid (T);
16995 end if;
16996 end Set_Modular_Size;
16998 -- Start of processing for Modular_Type_Declaration
17000 begin
17001 -- If the mod expression is (exactly) 2 * literal, where literal is
17002 -- 64 or less,then almost certainly the * was meant to be **. Warn!
17004 if Warn_On_Suspicious_Modulus_Value
17005 and then Nkind (Mod_Expr) = N_Op_Multiply
17006 and then Nkind (Left_Opnd (Mod_Expr)) = N_Integer_Literal
17007 and then Intval (Left_Opnd (Mod_Expr)) = Uint_2
17008 and then Nkind (Right_Opnd (Mod_Expr)) = N_Integer_Literal
17009 and then Intval (Right_Opnd (Mod_Expr)) <= Uint_64
17010 then
17011 Error_Msg_N
17012 ("suspicious MOD value, was '*'* intended'??M?", Mod_Expr);
17013 end if;
17015 -- Proceed with analysis of mod expression
17017 Analyze_And_Resolve (Mod_Expr, Any_Integer);
17018 Set_Etype (T, T);
17019 Set_Ekind (T, E_Modular_Integer_Type);
17020 Init_Alignment (T);
17021 Set_Is_Constrained (T);
17023 if not Is_OK_Static_Expression (Mod_Expr) then
17024 Flag_Non_Static_Expr
17025 ("non-static expression used for modular type bound!", Mod_Expr);
17026 M_Val := 2 ** System_Max_Binary_Modulus_Power;
17027 else
17028 M_Val := Expr_Value (Mod_Expr);
17029 end if;
17031 if M_Val < 1 then
17032 Error_Msg_N ("modulus value must be positive", Mod_Expr);
17033 M_Val := 2 ** System_Max_Binary_Modulus_Power;
17034 end if;
17036 Set_Modulus (T, M_Val);
17038 -- Create bounds for the modular type based on the modulus given in
17039 -- the type declaration and then analyze and resolve those bounds.
17041 Set_Scalar_Range (T,
17042 Make_Range (Sloc (Mod_Expr),
17043 Low_Bound => Make_Integer_Literal (Sloc (Mod_Expr), 0),
17044 High_Bound => Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
17046 -- Properly analyze the literals for the range. We do this manually
17047 -- because we can't go calling Resolve, since we are resolving these
17048 -- bounds with the type, and this type is certainly not complete yet!
17050 Set_Etype (Low_Bound (Scalar_Range (T)), T);
17051 Set_Etype (High_Bound (Scalar_Range (T)), T);
17052 Set_Is_Static_Expression (Low_Bound (Scalar_Range (T)));
17053 Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
17055 -- Loop through powers of two to find number of bits required
17057 for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
17059 -- Binary case
17061 if M_Val = 2 ** Bits then
17062 Set_Modular_Size (Bits);
17063 return;
17065 -- Non-binary case
17067 elsif M_Val < 2 ** Bits then
17068 Check_SPARK_Restriction ("modulus should be a power of 2", T);
17069 Set_Non_Binary_Modulus (T);
17071 if Bits > System_Max_Nonbinary_Modulus_Power then
17072 Error_Msg_Uint_1 :=
17073 UI_From_Int (System_Max_Nonbinary_Modulus_Power);
17074 Error_Msg_F
17075 ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
17076 Set_Modular_Size (System_Max_Binary_Modulus_Power);
17077 return;
17079 else
17080 -- In the non-binary case, set size as per RM 13.3(55)
17082 Set_Modular_Size (Bits);
17083 return;
17084 end if;
17085 end if;
17087 end loop;
17089 -- If we fall through, then the size exceed System.Max_Binary_Modulus
17090 -- so we just signal an error and set the maximum size.
17092 Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
17093 Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
17095 Set_Modular_Size (System_Max_Binary_Modulus_Power);
17096 Init_Alignment (T);
17098 end Modular_Type_Declaration;
17100 --------------------------
17101 -- New_Concatenation_Op --
17102 --------------------------
17104 procedure New_Concatenation_Op (Typ : Entity_Id) is
17105 Loc : constant Source_Ptr := Sloc (Typ);
17106 Op : Entity_Id;
17108 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
17109 -- Create abbreviated declaration for the formal of a predefined
17110 -- Operator 'Op' of type 'Typ'
17112 --------------------
17113 -- Make_Op_Formal --
17114 --------------------
17116 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
17117 Formal : Entity_Id;
17118 begin
17119 Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
17120 Set_Etype (Formal, Typ);
17121 Set_Mechanism (Formal, Default_Mechanism);
17122 return Formal;
17123 end Make_Op_Formal;
17125 -- Start of processing for New_Concatenation_Op
17127 begin
17128 Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
17130 Set_Ekind (Op, E_Operator);
17131 Set_Scope (Op, Current_Scope);
17132 Set_Etype (Op, Typ);
17133 Set_Homonym (Op, Get_Name_Entity_Id (Name_Op_Concat));
17134 Set_Is_Immediately_Visible (Op);
17135 Set_Is_Intrinsic_Subprogram (Op);
17136 Set_Has_Completion (Op);
17137 Append_Entity (Op, Current_Scope);
17139 Set_Name_Entity_Id (Name_Op_Concat, Op);
17141 Append_Entity (Make_Op_Formal (Typ, Op), Op);
17142 Append_Entity (Make_Op_Formal (Typ, Op), Op);
17143 end New_Concatenation_Op;
17145 -------------------------
17146 -- OK_For_Limited_Init --
17147 -------------------------
17149 -- ???Check all calls of this, and compare the conditions under which it's
17150 -- called.
17152 function OK_For_Limited_Init
17153 (Typ : Entity_Id;
17154 Exp : Node_Id) return Boolean
17156 begin
17157 return Is_CPP_Constructor_Call (Exp)
17158 or else (Ada_Version >= Ada_2005
17159 and then not Debug_Flag_Dot_L
17160 and then OK_For_Limited_Init_In_05 (Typ, Exp));
17161 end OK_For_Limited_Init;
17163 -------------------------------
17164 -- OK_For_Limited_Init_In_05 --
17165 -------------------------------
17167 function OK_For_Limited_Init_In_05
17168 (Typ : Entity_Id;
17169 Exp : Node_Id) return Boolean
17171 begin
17172 -- An object of a limited interface type can be initialized with any
17173 -- expression of a nonlimited descendant type.
17175 if Is_Class_Wide_Type (Typ)
17176 and then Is_Limited_Interface (Typ)
17177 and then not Is_Limited_Type (Etype (Exp))
17178 then
17179 return True;
17180 end if;
17182 -- Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
17183 -- case of limited aggregates (including extension aggregates), and
17184 -- function calls. The function call may have been given in prefixed
17185 -- notation, in which case the original node is an indexed component.
17186 -- If the function is parameterless, the original node was an explicit
17187 -- dereference. The function may also be parameterless, in which case
17188 -- the source node is just an identifier.
17190 case Nkind (Original_Node (Exp)) is
17191 when N_Aggregate | N_Extension_Aggregate | N_Function_Call | N_Op =>
17192 return True;
17194 when N_Identifier =>
17195 return Present (Entity (Original_Node (Exp)))
17196 and then Ekind (Entity (Original_Node (Exp))) = E_Function;
17198 when N_Qualified_Expression =>
17199 return
17200 OK_For_Limited_Init_In_05
17201 (Typ, Expression (Original_Node (Exp)));
17203 -- Ada 2005 (AI-251): If a class-wide interface object is initialized
17204 -- with a function call, the expander has rewritten the call into an
17205 -- N_Type_Conversion node to force displacement of the pointer to
17206 -- reference the component containing the secondary dispatch table.
17207 -- Otherwise a type conversion is not a legal context.
17208 -- A return statement for a build-in-place function returning a
17209 -- synchronized type also introduces an unchecked conversion.
17211 when N_Type_Conversion |
17212 N_Unchecked_Type_Conversion =>
17213 return not Comes_From_Source (Exp)
17214 and then
17215 OK_For_Limited_Init_In_05
17216 (Typ, Expression (Original_Node (Exp)));
17218 when N_Indexed_Component |
17219 N_Selected_Component |
17220 N_Explicit_Dereference =>
17221 return Nkind (Exp) = N_Function_Call;
17223 -- A use of 'Input is a function call, hence allowed. Normally the
17224 -- attribute will be changed to a call, but the attribute by itself
17225 -- can occur with -gnatc.
17227 when N_Attribute_Reference =>
17228 return Attribute_Name (Original_Node (Exp)) = Name_Input;
17230 -- For a case expression, all dependent expressions must be legal
17232 when N_Case_Expression =>
17233 declare
17234 Alt : Node_Id;
17236 begin
17237 Alt := First (Alternatives (Original_Node (Exp)));
17238 while Present (Alt) loop
17239 if not OK_For_Limited_Init_In_05 (Typ, Expression (Alt)) then
17240 return False;
17241 end if;
17243 Next (Alt);
17244 end loop;
17246 return True;
17247 end;
17249 -- For an if expression, all dependent expressions must be legal
17251 when N_If_Expression =>
17252 declare
17253 Then_Expr : constant Node_Id :=
17254 Next (First (Expressions (Original_Node (Exp))));
17255 Else_Expr : constant Node_Id := Next (Then_Expr);
17256 begin
17257 return OK_For_Limited_Init_In_05 (Typ, Then_Expr)
17258 and then
17259 OK_For_Limited_Init_In_05 (Typ, Else_Expr);
17260 end;
17262 when others =>
17263 return False;
17264 end case;
17265 end OK_For_Limited_Init_In_05;
17267 -------------------------------------------
17268 -- Ordinary_Fixed_Point_Type_Declaration --
17269 -------------------------------------------
17271 procedure Ordinary_Fixed_Point_Type_Declaration
17272 (T : Entity_Id;
17273 Def : Node_Id)
17275 Loc : constant Source_Ptr := Sloc (Def);
17276 Delta_Expr : constant Node_Id := Delta_Expression (Def);
17277 RRS : constant Node_Id := Real_Range_Specification (Def);
17278 Implicit_Base : Entity_Id;
17279 Delta_Val : Ureal;
17280 Small_Val : Ureal;
17281 Low_Val : Ureal;
17282 High_Val : Ureal;
17284 begin
17285 Check_Restriction (No_Fixed_Point, Def);
17287 -- Create implicit base type
17289 Implicit_Base :=
17290 Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
17291 Set_Etype (Implicit_Base, Implicit_Base);
17293 -- Analyze and process delta expression
17295 Analyze_And_Resolve (Delta_Expr, Any_Real);
17297 Check_Delta_Expression (Delta_Expr);
17298 Delta_Val := Expr_Value_R (Delta_Expr);
17300 Set_Delta_Value (Implicit_Base, Delta_Val);
17302 -- Compute default small from given delta, which is the largest power
17303 -- of two that does not exceed the given delta value.
17305 declare
17306 Tmp : Ureal;
17307 Scale : Int;
17309 begin
17310 Tmp := Ureal_1;
17311 Scale := 0;
17313 if Delta_Val < Ureal_1 then
17314 while Delta_Val < Tmp loop
17315 Tmp := Tmp / Ureal_2;
17316 Scale := Scale + 1;
17317 end loop;
17319 else
17320 loop
17321 Tmp := Tmp * Ureal_2;
17322 exit when Tmp > Delta_Val;
17323 Scale := Scale - 1;
17324 end loop;
17325 end if;
17327 Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
17328 end;
17330 Set_Small_Value (Implicit_Base, Small_Val);
17332 -- If no range was given, set a dummy range
17334 if RRS <= Empty_Or_Error then
17335 Low_Val := -Small_Val;
17336 High_Val := Small_Val;
17338 -- Otherwise analyze and process given range
17340 else
17341 declare
17342 Low : constant Node_Id := Low_Bound (RRS);
17343 High : constant Node_Id := High_Bound (RRS);
17345 begin
17346 Analyze_And_Resolve (Low, Any_Real);
17347 Analyze_And_Resolve (High, Any_Real);
17348 Check_Real_Bound (Low);
17349 Check_Real_Bound (High);
17351 -- Obtain and set the range
17353 Low_Val := Expr_Value_R (Low);
17354 High_Val := Expr_Value_R (High);
17356 if Low_Val > High_Val then
17357 Error_Msg_NE ("??fixed point type& has null range", Def, T);
17358 end if;
17359 end;
17360 end if;
17362 -- The range for both the implicit base and the declared first subtype
17363 -- cannot be set yet, so we use the special routine Set_Fixed_Range to
17364 -- set a temporary range in place. Note that the bounds of the base
17365 -- type will be widened to be symmetrical and to fill the available
17366 -- bits when the type is frozen.
17368 -- We could do this with all discrete types, and probably should, but
17369 -- we absolutely have to do it for fixed-point, since the end-points
17370 -- of the range and the size are determined by the small value, which
17371 -- could be reset before the freeze point.
17373 Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
17374 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
17376 -- Complete definition of first subtype
17378 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
17379 Set_Etype (T, Implicit_Base);
17380 Init_Size_Align (T);
17381 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
17382 Set_Small_Value (T, Small_Val);
17383 Set_Delta_Value (T, Delta_Val);
17384 Set_Is_Constrained (T);
17386 end Ordinary_Fixed_Point_Type_Declaration;
17388 ----------------------------------------
17389 -- Prepare_Private_Subtype_Completion --
17390 ----------------------------------------
17392 procedure Prepare_Private_Subtype_Completion
17393 (Id : Entity_Id;
17394 Related_Nod : Node_Id)
17396 Id_B : constant Entity_Id := Base_Type (Id);
17397 Full_B : constant Entity_Id := Full_View (Id_B);
17398 Full : Entity_Id;
17400 begin
17401 if Present (Full_B) then
17403 -- The Base_Type is already completed, we can complete the subtype
17404 -- now. We have to create a new entity with the same name, Thus we
17405 -- can't use Create_Itype.
17407 Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
17408 Set_Is_Itype (Full);
17409 Set_Associated_Node_For_Itype (Full, Related_Nod);
17410 Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
17411 end if;
17413 -- The parent subtype may be private, but the base might not, in some
17414 -- nested instances. In that case, the subtype does not need to be
17415 -- exchanged. It would still be nice to make private subtypes and their
17416 -- bases consistent at all times ???
17418 if Is_Private_Type (Id_B) then
17419 Append_Elmt (Id, Private_Dependents (Id_B));
17420 end if;
17421 end Prepare_Private_Subtype_Completion;
17423 ---------------------------
17424 -- Process_Discriminants --
17425 ---------------------------
17427 procedure Process_Discriminants
17428 (N : Node_Id;
17429 Prev : Entity_Id := Empty)
17431 Elist : constant Elist_Id := New_Elmt_List;
17432 Id : Node_Id;
17433 Discr : Node_Id;
17434 Discr_Number : Uint;
17435 Discr_Type : Entity_Id;
17436 Default_Present : Boolean := False;
17437 Default_Not_Present : Boolean := False;
17439 begin
17440 -- A composite type other than an array type can have discriminants.
17441 -- On entry, the current scope is the composite type.
17443 -- The discriminants are initially entered into the scope of the type
17444 -- via Enter_Name with the default Ekind of E_Void to prevent premature
17445 -- use, as explained at the end of this procedure.
17447 Discr := First (Discriminant_Specifications (N));
17448 while Present (Discr) loop
17449 Enter_Name (Defining_Identifier (Discr));
17451 -- For navigation purposes we add a reference to the discriminant
17452 -- in the entity for the type. If the current declaration is a
17453 -- completion, place references on the partial view. Otherwise the
17454 -- type is the current scope.
17456 if Present (Prev) then
17458 -- The references go on the partial view, if present. If the
17459 -- partial view has discriminants, the references have been
17460 -- generated already.
17462 if not Has_Discriminants (Prev) then
17463 Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
17464 end if;
17465 else
17466 Generate_Reference
17467 (Current_Scope, Defining_Identifier (Discr), 'd');
17468 end if;
17470 if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
17471 Discr_Type := Access_Definition (Discr, Discriminant_Type (Discr));
17473 -- Ada 2005 (AI-254)
17475 if Present (Access_To_Subprogram_Definition
17476 (Discriminant_Type (Discr)))
17477 and then Protected_Present (Access_To_Subprogram_Definition
17478 (Discriminant_Type (Discr)))
17479 then
17480 Discr_Type :=
17481 Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
17482 end if;
17484 else
17485 Find_Type (Discriminant_Type (Discr));
17486 Discr_Type := Etype (Discriminant_Type (Discr));
17488 if Error_Posted (Discriminant_Type (Discr)) then
17489 Discr_Type := Any_Type;
17490 end if;
17491 end if;
17493 if Is_Access_Type (Discr_Type) then
17495 -- Ada 2005 (AI-230): Access discriminant allowed in non-limited
17496 -- record types
17498 if Ada_Version < Ada_2005 then
17499 Check_Access_Discriminant_Requires_Limited
17500 (Discr, Discriminant_Type (Discr));
17501 end if;
17503 if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
17504 Error_Msg_N
17505 ("(Ada 83) access discriminant not allowed", Discr);
17506 end if;
17508 elsif not Is_Discrete_Type (Discr_Type) then
17509 Error_Msg_N ("discriminants must have a discrete or access type",
17510 Discriminant_Type (Discr));
17511 end if;
17513 Set_Etype (Defining_Identifier (Discr), Discr_Type);
17515 -- If a discriminant specification includes the assignment compound
17516 -- delimiter followed by an expression, the expression is the default
17517 -- expression of the discriminant; the default expression must be of
17518 -- the type of the discriminant. (RM 3.7.1) Since this expression is
17519 -- a default expression, we do the special preanalysis, since this
17520 -- expression does not freeze (see "Handling of Default and Per-
17521 -- Object Expressions" in spec of package Sem).
17523 if Present (Expression (Discr)) then
17524 Preanalyze_Spec_Expression (Expression (Discr), Discr_Type);
17526 if Nkind (N) = N_Formal_Type_Declaration then
17527 Error_Msg_N
17528 ("discriminant defaults not allowed for formal type",
17529 Expression (Discr));
17531 -- Flag an error for a tagged type with defaulted discriminants,
17532 -- excluding limited tagged types when compiling for Ada 2012
17533 -- (see AI05-0214).
17535 elsif Is_Tagged_Type (Current_Scope)
17536 and then (not Is_Limited_Type (Current_Scope)
17537 or else Ada_Version < Ada_2012)
17538 and then Comes_From_Source (N)
17539 then
17540 -- Note: see similar test in Check_Or_Process_Discriminants, to
17541 -- handle the (illegal) case of the completion of an untagged
17542 -- view with discriminants with defaults by a tagged full view.
17543 -- We skip the check if Discr does not come from source, to
17544 -- account for the case of an untagged derived type providing
17545 -- defaults for a renamed discriminant from a private untagged
17546 -- ancestor with a tagged full view (ACATS B460006).
17548 if Ada_Version >= Ada_2012 then
17549 Error_Msg_N
17550 ("discriminants of nonlimited tagged type cannot have"
17551 & " defaults",
17552 Expression (Discr));
17553 else
17554 Error_Msg_N
17555 ("discriminants of tagged type cannot have defaults",
17556 Expression (Discr));
17557 end if;
17559 else
17560 Default_Present := True;
17561 Append_Elmt (Expression (Discr), Elist);
17563 -- Tag the defining identifiers for the discriminants with
17564 -- their corresponding default expressions from the tree.
17566 Set_Discriminant_Default_Value
17567 (Defining_Identifier (Discr), Expression (Discr));
17568 end if;
17570 else
17571 Default_Not_Present := True;
17572 end if;
17574 -- Ada 2005 (AI-231): Create an Itype that is a duplicate of
17575 -- Discr_Type but with the null-exclusion attribute
17577 if Ada_Version >= Ada_2005 then
17579 -- Ada 2005 (AI-231): Static checks
17581 if Can_Never_Be_Null (Discr_Type) then
17582 Null_Exclusion_Static_Checks (Discr);
17584 elsif Is_Access_Type (Discr_Type)
17585 and then Null_Exclusion_Present (Discr)
17587 -- No need to check itypes because in their case this check
17588 -- was done at their point of creation
17590 and then not Is_Itype (Discr_Type)
17591 then
17592 if Can_Never_Be_Null (Discr_Type) then
17593 Error_Msg_NE
17594 ("`NOT NULL` not allowed (& already excludes null)",
17595 Discr,
17596 Discr_Type);
17597 end if;
17599 Set_Etype (Defining_Identifier (Discr),
17600 Create_Null_Excluding_Itype
17601 (T => Discr_Type,
17602 Related_Nod => Discr));
17604 -- Check for improper null exclusion if the type is otherwise
17605 -- legal for a discriminant.
17607 elsif Null_Exclusion_Present (Discr)
17608 and then Is_Discrete_Type (Discr_Type)
17609 then
17610 Error_Msg_N
17611 ("null exclusion can only apply to an access type", Discr);
17612 end if;
17614 -- Ada 2005 (AI-402): access discriminants of nonlimited types
17615 -- can't have defaults. Synchronized types, or types that are
17616 -- explicitly limited are fine, but special tests apply to derived
17617 -- types in generics: in a generic body we have to assume the
17618 -- worst, and therefore defaults are not allowed if the parent is
17619 -- a generic formal private type (see ACATS B370001).
17621 if Is_Access_Type (Discr_Type) and then Default_Present then
17622 if Ekind (Discr_Type) /= E_Anonymous_Access_Type
17623 or else Is_Limited_Record (Current_Scope)
17624 or else Is_Concurrent_Type (Current_Scope)
17625 or else Is_Concurrent_Record_Type (Current_Scope)
17626 or else Ekind (Current_Scope) = E_Limited_Private_Type
17627 then
17628 if not Is_Derived_Type (Current_Scope)
17629 or else not Is_Generic_Type (Etype (Current_Scope))
17630 or else not In_Package_Body (Scope (Etype (Current_Scope)))
17631 or else Limited_Present
17632 (Type_Definition (Parent (Current_Scope)))
17633 then
17634 null;
17636 else
17637 Error_Msg_N ("access discriminants of nonlimited types",
17638 Expression (Discr));
17639 Error_Msg_N ("\cannot have defaults", Expression (Discr));
17640 end if;
17642 elsif Present (Expression (Discr)) then
17643 Error_Msg_N
17644 ("(Ada 2005) access discriminants of nonlimited types",
17645 Expression (Discr));
17646 Error_Msg_N ("\cannot have defaults", Expression (Discr));
17647 end if;
17648 end if;
17649 end if;
17651 Next (Discr);
17652 end loop;
17654 -- An element list consisting of the default expressions of the
17655 -- discriminants is constructed in the above loop and used to set
17656 -- the Discriminant_Constraint attribute for the type. If an object
17657 -- is declared of this (record or task) type without any explicit
17658 -- discriminant constraint given, this element list will form the
17659 -- actual parameters for the corresponding initialization procedure
17660 -- for the type.
17662 Set_Discriminant_Constraint (Current_Scope, Elist);
17663 Set_Stored_Constraint (Current_Scope, No_Elist);
17665 -- Default expressions must be provided either for all or for none
17666 -- of the discriminants of a discriminant part. (RM 3.7.1)
17668 if Default_Present and then Default_Not_Present then
17669 Error_Msg_N
17670 ("incomplete specification of defaults for discriminants", N);
17671 end if;
17673 -- The use of the name of a discriminant is not allowed in default
17674 -- expressions of a discriminant part if the specification of the
17675 -- discriminant is itself given in the discriminant part. (RM 3.7.1)
17677 -- To detect this, the discriminant names are entered initially with an
17678 -- Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
17679 -- attempt to use a void entity (for example in an expression that is
17680 -- type-checked) produces the error message: premature usage. Now after
17681 -- completing the semantic analysis of the discriminant part, we can set
17682 -- the Ekind of all the discriminants appropriately.
17684 Discr := First (Discriminant_Specifications (N));
17685 Discr_Number := Uint_1;
17686 while Present (Discr) loop
17687 Id := Defining_Identifier (Discr);
17688 Set_Ekind (Id, E_Discriminant);
17689 Init_Component_Location (Id);
17690 Init_Esize (Id);
17691 Set_Discriminant_Number (Id, Discr_Number);
17693 -- Make sure this is always set, even in illegal programs
17695 Set_Corresponding_Discriminant (Id, Empty);
17697 -- Initialize the Original_Record_Component to the entity itself.
17698 -- Inherit_Components will propagate the right value to
17699 -- discriminants in derived record types.
17701 Set_Original_Record_Component (Id, Id);
17703 -- Create the discriminal for the discriminant
17705 Build_Discriminal (Id);
17707 Next (Discr);
17708 Discr_Number := Discr_Number + 1;
17709 end loop;
17711 Set_Has_Discriminants (Current_Scope);
17712 end Process_Discriminants;
17714 -----------------------
17715 -- Process_Full_View --
17716 -----------------------
17718 procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
17719 Priv_Parent : Entity_Id;
17720 Full_Parent : Entity_Id;
17721 Full_Indic : Node_Id;
17723 procedure Collect_Implemented_Interfaces
17724 (Typ : Entity_Id;
17725 Ifaces : Elist_Id);
17726 -- Ada 2005: Gather all the interfaces that Typ directly or
17727 -- inherently implements. Duplicate entries are not added to
17728 -- the list Ifaces.
17730 ------------------------------------
17731 -- Collect_Implemented_Interfaces --
17732 ------------------------------------
17734 procedure Collect_Implemented_Interfaces
17735 (Typ : Entity_Id;
17736 Ifaces : Elist_Id)
17738 Iface : Entity_Id;
17739 Iface_Elmt : Elmt_Id;
17741 begin
17742 -- Abstract interfaces are only associated with tagged record types
17744 if not Is_Tagged_Type (Typ)
17745 or else not Is_Record_Type (Typ)
17746 then
17747 return;
17748 end if;
17750 -- Recursively climb to the ancestors
17752 if Etype (Typ) /= Typ
17754 -- Protect the frontend against wrong cyclic declarations like:
17756 -- type B is new A with private;
17757 -- type C is new A with private;
17758 -- private
17759 -- type B is new C with null record;
17760 -- type C is new B with null record;
17762 and then Etype (Typ) /= Priv_T
17763 and then Etype (Typ) /= Full_T
17764 then
17765 -- Keep separate the management of private type declarations
17767 if Ekind (Typ) = E_Record_Type_With_Private then
17769 -- Handle the following erroneous case:
17770 -- type Private_Type is tagged private;
17771 -- private
17772 -- type Private_Type is new Type_Implementing_Iface;
17774 if Present (Full_View (Typ))
17775 and then Etype (Typ) /= Full_View (Typ)
17776 then
17777 if Is_Interface (Etype (Typ)) then
17778 Append_Unique_Elmt (Etype (Typ), Ifaces);
17779 end if;
17781 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
17782 end if;
17784 -- Non-private types
17786 else
17787 if Is_Interface (Etype (Typ)) then
17788 Append_Unique_Elmt (Etype (Typ), Ifaces);
17789 end if;
17791 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
17792 end if;
17793 end if;
17795 -- Handle entities in the list of abstract interfaces
17797 if Present (Interfaces (Typ)) then
17798 Iface_Elmt := First_Elmt (Interfaces (Typ));
17799 while Present (Iface_Elmt) loop
17800 Iface := Node (Iface_Elmt);
17802 pragma Assert (Is_Interface (Iface));
17804 if not Contain_Interface (Iface, Ifaces) then
17805 Append_Elmt (Iface, Ifaces);
17806 Collect_Implemented_Interfaces (Iface, Ifaces);
17807 end if;
17809 Next_Elmt (Iface_Elmt);
17810 end loop;
17811 end if;
17812 end Collect_Implemented_Interfaces;
17814 -- Start of processing for Process_Full_View
17816 begin
17817 -- First some sanity checks that must be done after semantic
17818 -- decoration of the full view and thus cannot be placed with other
17819 -- similar checks in Find_Type_Name
17821 if not Is_Limited_Type (Priv_T)
17822 and then (Is_Limited_Type (Full_T)
17823 or else Is_Limited_Composite (Full_T))
17824 then
17825 if In_Instance then
17826 null;
17827 else
17828 Error_Msg_N
17829 ("completion of nonlimited type cannot be limited", Full_T);
17830 Explain_Limited_Type (Full_T, Full_T);
17831 end if;
17833 elsif Is_Abstract_Type (Full_T)
17834 and then not Is_Abstract_Type (Priv_T)
17835 then
17836 Error_Msg_N
17837 ("completion of nonabstract type cannot be abstract", Full_T);
17839 elsif Is_Tagged_Type (Priv_T)
17840 and then Is_Limited_Type (Priv_T)
17841 and then not Is_Limited_Type (Full_T)
17842 then
17843 -- If pragma CPP_Class was applied to the private declaration
17844 -- propagate the limitedness to the full-view
17846 if Is_CPP_Class (Priv_T) then
17847 Set_Is_Limited_Record (Full_T);
17849 -- GNAT allow its own definition of Limited_Controlled to disobey
17850 -- this rule in order in ease the implementation. This test is safe
17851 -- because Root_Controlled is defined in a child of System that
17852 -- normal programs are not supposed to use.
17854 elsif Is_RTE (Etype (Full_T), RE_Root_Controlled) then
17855 Set_Is_Limited_Composite (Full_T);
17856 else
17857 Error_Msg_N
17858 ("completion of limited tagged type must be limited", Full_T);
17859 end if;
17861 elsif Is_Generic_Type (Priv_T) then
17862 Error_Msg_N ("generic type cannot have a completion", Full_T);
17863 end if;
17865 -- Check that ancestor interfaces of private and full views are
17866 -- consistent. We omit this check for synchronized types because
17867 -- they are performed on the corresponding record type when frozen.
17869 if Ada_Version >= Ada_2005
17870 and then Is_Tagged_Type (Priv_T)
17871 and then Is_Tagged_Type (Full_T)
17872 and then not Is_Concurrent_Type (Full_T)
17873 then
17874 declare
17875 Iface : Entity_Id;
17876 Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
17877 Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
17879 begin
17880 Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
17881 Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
17883 -- Ada 2005 (AI-251): The partial view shall be a descendant of
17884 -- an interface type if and only if the full type is descendant
17885 -- of the interface type (AARM 7.3 (7.3/2)).
17887 Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
17889 if Present (Iface) then
17890 Error_Msg_NE
17891 ("interface & not implemented by full type " &
17892 "(RM-2005 7.3 (7.3/2))", Priv_T, Iface);
17893 end if;
17895 Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
17897 if Present (Iface) then
17898 Error_Msg_NE
17899 ("interface & not implemented by partial view " &
17900 "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
17901 end if;
17902 end;
17903 end if;
17905 if Is_Tagged_Type (Priv_T)
17906 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
17907 and then Is_Derived_Type (Full_T)
17908 then
17909 Priv_Parent := Etype (Priv_T);
17911 -- The full view of a private extension may have been transformed
17912 -- into an unconstrained derived type declaration and a subtype
17913 -- declaration (see build_derived_record_type for details).
17915 if Nkind (N) = N_Subtype_Declaration then
17916 Full_Indic := Subtype_Indication (N);
17917 Full_Parent := Etype (Base_Type (Full_T));
17918 else
17919 Full_Indic := Subtype_Indication (Type_Definition (N));
17920 Full_Parent := Etype (Full_T);
17921 end if;
17923 -- Check that the parent type of the full type is a descendant of
17924 -- the ancestor subtype given in the private extension. If either
17925 -- entity has an Etype equal to Any_Type then we had some previous
17926 -- error situation [7.3(8)].
17928 if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
17929 return;
17931 -- Ada 2005 (AI-251): Interfaces in the full-typ can be given in
17932 -- any order. Therefore we don't have to check that its parent must
17933 -- be a descendant of the parent of the private type declaration.
17935 elsif Is_Interface (Priv_Parent)
17936 and then Is_Interface (Full_Parent)
17937 then
17938 null;
17940 -- Ada 2005 (AI-251): If the parent of the private type declaration
17941 -- is an interface there is no need to check that it is an ancestor
17942 -- of the associated full type declaration. The required tests for
17943 -- this case are performed by Build_Derived_Record_Type.
17945 elsif not Is_Interface (Base_Type (Priv_Parent))
17946 and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
17947 then
17948 Error_Msg_N
17949 ("parent of full type must descend from parent"
17950 & " of private extension", Full_Indic);
17952 -- First check a formal restriction, and then proceed with checking
17953 -- Ada rules. Since the formal restriction is not a serious error, we
17954 -- don't prevent further error detection for this check, hence the
17955 -- ELSE.
17957 else
17959 -- In formal mode, when completing a private extension the type
17960 -- named in the private part must be exactly the same as that
17961 -- named in the visible part.
17963 if Priv_Parent /= Full_Parent then
17964 Error_Msg_Name_1 := Chars (Priv_Parent);
17965 Check_SPARK_Restriction ("% expected", Full_Indic);
17966 end if;
17968 -- Check the rules of 7.3(10): if the private extension inherits
17969 -- known discriminants, then the full type must also inherit those
17970 -- discriminants from the same (ancestor) type, and the parent
17971 -- subtype of the full type must be constrained if and only if
17972 -- the ancestor subtype of the private extension is constrained.
17974 if No (Discriminant_Specifications (Parent (Priv_T)))
17975 and then not Has_Unknown_Discriminants (Priv_T)
17976 and then Has_Discriminants (Base_Type (Priv_Parent))
17977 then
17978 declare
17979 Priv_Indic : constant Node_Id :=
17980 Subtype_Indication (Parent (Priv_T));
17982 Priv_Constr : constant Boolean :=
17983 Is_Constrained (Priv_Parent)
17984 or else
17985 Nkind (Priv_Indic) = N_Subtype_Indication
17986 or else
17987 Is_Constrained (Entity (Priv_Indic));
17989 Full_Constr : constant Boolean :=
17990 Is_Constrained (Full_Parent)
17991 or else
17992 Nkind (Full_Indic) = N_Subtype_Indication
17993 or else
17994 Is_Constrained (Entity (Full_Indic));
17996 Priv_Discr : Entity_Id;
17997 Full_Discr : Entity_Id;
17999 begin
18000 Priv_Discr := First_Discriminant (Priv_Parent);
18001 Full_Discr := First_Discriminant (Full_Parent);
18002 while Present (Priv_Discr) and then Present (Full_Discr) loop
18003 if Original_Record_Component (Priv_Discr) =
18004 Original_Record_Component (Full_Discr)
18005 or else
18006 Corresponding_Discriminant (Priv_Discr) =
18007 Corresponding_Discriminant (Full_Discr)
18008 then
18009 null;
18010 else
18011 exit;
18012 end if;
18014 Next_Discriminant (Priv_Discr);
18015 Next_Discriminant (Full_Discr);
18016 end loop;
18018 if Present (Priv_Discr) or else Present (Full_Discr) then
18019 Error_Msg_N
18020 ("full view must inherit discriminants of the parent"
18021 & " type used in the private extension", Full_Indic);
18023 elsif Priv_Constr and then not Full_Constr then
18024 Error_Msg_N
18025 ("parent subtype of full type must be constrained",
18026 Full_Indic);
18028 elsif Full_Constr and then not Priv_Constr then
18029 Error_Msg_N
18030 ("parent subtype of full type must be unconstrained",
18031 Full_Indic);
18032 end if;
18033 end;
18035 -- Check the rules of 7.3(12): if a partial view has neither
18036 -- known or unknown discriminants, then the full type
18037 -- declaration shall define a definite subtype.
18039 elsif not Has_Unknown_Discriminants (Priv_T)
18040 and then not Has_Discriminants (Priv_T)
18041 and then not Is_Constrained (Full_T)
18042 then
18043 Error_Msg_N
18044 ("full view must define a constrained type if partial view"
18045 & " has no discriminants", Full_T);
18046 end if;
18048 -- ??????? Do we implement the following properly ?????
18049 -- If the ancestor subtype of a private extension has constrained
18050 -- discriminants, then the parent subtype of the full view shall
18051 -- impose a statically matching constraint on those discriminants
18052 -- [7.3(13)].
18053 end if;
18055 else
18056 -- For untagged types, verify that a type without discriminants
18057 -- is not completed with an unconstrained type.
18059 if not Is_Indefinite_Subtype (Priv_T)
18060 and then Is_Indefinite_Subtype (Full_T)
18061 then
18062 Error_Msg_N ("full view of type must be definite subtype", Full_T);
18063 end if;
18064 end if;
18066 -- AI-419: verify that the use of "limited" is consistent
18068 declare
18069 Orig_Decl : constant Node_Id := Original_Node (N);
18071 begin
18072 if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
18073 and then not Limited_Present (Parent (Priv_T))
18074 and then not Synchronized_Present (Parent (Priv_T))
18075 and then Nkind (Orig_Decl) = N_Full_Type_Declaration
18076 and then Nkind
18077 (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
18078 and then Limited_Present (Type_Definition (Orig_Decl))
18079 then
18080 Error_Msg_N
18081 ("full view of non-limited extension cannot be limited", N);
18082 end if;
18083 end;
18085 -- Ada 2005 (AI-443): A synchronized private extension must be
18086 -- completed by a task or protected type.
18088 if Ada_Version >= Ada_2005
18089 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
18090 and then Synchronized_Present (Parent (Priv_T))
18091 and then not Is_Concurrent_Type (Full_T)
18092 then
18093 Error_Msg_N ("full view of synchronized extension must " &
18094 "be synchronized type", N);
18095 end if;
18097 -- Ada 2005 AI-363: if the full view has discriminants with
18098 -- defaults, it is illegal to declare constrained access subtypes
18099 -- whose designated type is the current type. This allows objects
18100 -- of the type that are declared in the heap to be unconstrained.
18102 if not Has_Unknown_Discriminants (Priv_T)
18103 and then not Has_Discriminants (Priv_T)
18104 and then Has_Discriminants (Full_T)
18105 and then
18106 Present (Discriminant_Default_Value (First_Discriminant (Full_T)))
18107 then
18108 Set_Has_Constrained_Partial_View (Full_T);
18109 Set_Has_Constrained_Partial_View (Priv_T);
18110 end if;
18112 -- Create a full declaration for all its subtypes recorded in
18113 -- Private_Dependents and swap them similarly to the base type. These
18114 -- are subtypes that have been define before the full declaration of
18115 -- the private type. We also swap the entry in Private_Dependents list
18116 -- so we can properly restore the private view on exit from the scope.
18118 declare
18119 Priv_Elmt : Elmt_Id;
18120 Priv : Entity_Id;
18121 Full : Entity_Id;
18123 begin
18124 Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
18125 while Present (Priv_Elmt) loop
18126 Priv := Node (Priv_Elmt);
18128 if Ekind_In (Priv, E_Private_Subtype,
18129 E_Limited_Private_Subtype,
18130 E_Record_Subtype_With_Private)
18131 then
18132 Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
18133 Set_Is_Itype (Full);
18134 Set_Parent (Full, Parent (Priv));
18135 Set_Associated_Node_For_Itype (Full, N);
18137 -- Now we need to complete the private subtype, but since the
18138 -- base type has already been swapped, we must also swap the
18139 -- subtypes (and thus, reverse the arguments in the call to
18140 -- Complete_Private_Subtype).
18142 Copy_And_Swap (Priv, Full);
18143 Complete_Private_Subtype (Full, Priv, Full_T, N);
18144 Replace_Elmt (Priv_Elmt, Full);
18145 end if;
18147 Next_Elmt (Priv_Elmt);
18148 end loop;
18149 end;
18151 -- If the private view was tagged, copy the new primitive operations
18152 -- from the private view to the full view.
18154 if Is_Tagged_Type (Full_T) then
18155 declare
18156 Disp_Typ : Entity_Id;
18157 Full_List : Elist_Id;
18158 Prim : Entity_Id;
18159 Prim_Elmt : Elmt_Id;
18160 Priv_List : Elist_Id;
18162 function Contains
18163 (E : Entity_Id;
18164 L : Elist_Id) return Boolean;
18165 -- Determine whether list L contains element E
18167 --------------
18168 -- Contains --
18169 --------------
18171 function Contains
18172 (E : Entity_Id;
18173 L : Elist_Id) return Boolean
18175 List_Elmt : Elmt_Id;
18177 begin
18178 List_Elmt := First_Elmt (L);
18179 while Present (List_Elmt) loop
18180 if Node (List_Elmt) = E then
18181 return True;
18182 end if;
18184 Next_Elmt (List_Elmt);
18185 end loop;
18187 return False;
18188 end Contains;
18190 -- Start of processing
18192 begin
18193 if Is_Tagged_Type (Priv_T) then
18194 Priv_List := Primitive_Operations (Priv_T);
18195 Prim_Elmt := First_Elmt (Priv_List);
18197 -- In the case of a concurrent type completing a private tagged
18198 -- type, primitives may have been declared in between the two
18199 -- views. These subprograms need to be wrapped the same way
18200 -- entries and protected procedures are handled because they
18201 -- cannot be directly shared by the two views.
18203 if Is_Concurrent_Type (Full_T) then
18204 declare
18205 Conc_Typ : constant Entity_Id :=
18206 Corresponding_Record_Type (Full_T);
18207 Curr_Nod : Node_Id := Parent (Conc_Typ);
18208 Wrap_Spec : Node_Id;
18210 begin
18211 while Present (Prim_Elmt) loop
18212 Prim := Node (Prim_Elmt);
18214 if Comes_From_Source (Prim)
18215 and then not Is_Abstract_Subprogram (Prim)
18216 then
18217 Wrap_Spec :=
18218 Make_Subprogram_Declaration (Sloc (Prim),
18219 Specification =>
18220 Build_Wrapper_Spec
18221 (Subp_Id => Prim,
18222 Obj_Typ => Conc_Typ,
18223 Formals =>
18224 Parameter_Specifications (
18225 Parent (Prim))));
18227 Insert_After (Curr_Nod, Wrap_Spec);
18228 Curr_Nod := Wrap_Spec;
18230 Analyze (Wrap_Spec);
18231 end if;
18233 Next_Elmt (Prim_Elmt);
18234 end loop;
18236 return;
18237 end;
18239 -- For non-concurrent types, transfer explicit primitives, but
18240 -- omit those inherited from the parent of the private view
18241 -- since they will be re-inherited later on.
18243 else
18244 Full_List := Primitive_Operations (Full_T);
18246 while Present (Prim_Elmt) loop
18247 Prim := Node (Prim_Elmt);
18249 if Comes_From_Source (Prim)
18250 and then not Contains (Prim, Full_List)
18251 then
18252 Append_Elmt (Prim, Full_List);
18253 end if;
18255 Next_Elmt (Prim_Elmt);
18256 end loop;
18257 end if;
18259 -- Untagged private view
18261 else
18262 Full_List := Primitive_Operations (Full_T);
18264 -- In this case the partial view is untagged, so here we locate
18265 -- all of the earlier primitives that need to be treated as
18266 -- dispatching (those that appear between the two views). Note
18267 -- that these additional operations must all be new operations
18268 -- (any earlier operations that override inherited operations
18269 -- of the full view will already have been inserted in the
18270 -- primitives list, marked by Check_Operation_From_Private_View
18271 -- as dispatching. Note that implicit "/=" operators are
18272 -- excluded from being added to the primitives list since they
18273 -- shouldn't be treated as dispatching (tagged "/=" is handled
18274 -- specially).
18276 Prim := Next_Entity (Full_T);
18277 while Present (Prim) and then Prim /= Priv_T loop
18278 if Ekind_In (Prim, E_Procedure, E_Function) then
18279 Disp_Typ := Find_Dispatching_Type (Prim);
18281 if Disp_Typ = Full_T
18282 and then (Chars (Prim) /= Name_Op_Ne
18283 or else Comes_From_Source (Prim))
18284 then
18285 Check_Controlling_Formals (Full_T, Prim);
18287 if not Is_Dispatching_Operation (Prim) then
18288 Append_Elmt (Prim, Full_List);
18289 Set_Is_Dispatching_Operation (Prim, True);
18290 Set_DT_Position (Prim, No_Uint);
18291 end if;
18293 elsif Is_Dispatching_Operation (Prim)
18294 and then Disp_Typ /= Full_T
18295 then
18297 -- Verify that it is not otherwise controlled by a
18298 -- formal or a return value of type T.
18300 Check_Controlling_Formals (Disp_Typ, Prim);
18301 end if;
18302 end if;
18304 Next_Entity (Prim);
18305 end loop;
18306 end if;
18308 -- For the tagged case, the two views can share the same primitive
18309 -- operations list and the same class-wide type. Update attributes
18310 -- of the class-wide type which depend on the full declaration.
18312 if Is_Tagged_Type (Priv_T) then
18313 Set_Direct_Primitive_Operations (Priv_T, Full_List);
18314 Set_Class_Wide_Type
18315 (Base_Type (Full_T), Class_Wide_Type (Priv_T));
18317 Set_Has_Task (Class_Wide_Type (Priv_T), Has_Task (Full_T));
18318 end if;
18319 end;
18320 end if;
18322 -- Ada 2005 AI 161: Check preelaboratable initialization consistency
18324 if Known_To_Have_Preelab_Init (Priv_T) then
18326 -- Case where there is a pragma Preelaborable_Initialization. We
18327 -- always allow this in predefined units, which is a bit of a kludge,
18328 -- but it means we don't have to struggle to meet the requirements in
18329 -- the RM for having Preelaborable Initialization. Otherwise we
18330 -- require that the type meets the RM rules. But we can't check that
18331 -- yet, because of the rule about overriding Initialize, so we simply
18332 -- set a flag that will be checked at freeze time.
18334 if not In_Predefined_Unit (Full_T) then
18335 Set_Must_Have_Preelab_Init (Full_T);
18336 end if;
18337 end if;
18339 -- If pragma CPP_Class was applied to the private type declaration,
18340 -- propagate it now to the full type declaration.
18342 if Is_CPP_Class (Priv_T) then
18343 Set_Is_CPP_Class (Full_T);
18344 Set_Convention (Full_T, Convention_CPP);
18346 -- Check that components of imported CPP types do not have default
18347 -- expressions.
18349 Check_CPP_Type_Has_No_Defaults (Full_T);
18350 end if;
18352 -- If the private view has user specified stream attributes, then so has
18353 -- the full view.
18355 -- Why the test, how could these flags be already set in Full_T ???
18357 if Has_Specified_Stream_Read (Priv_T) then
18358 Set_Has_Specified_Stream_Read (Full_T);
18359 end if;
18361 if Has_Specified_Stream_Write (Priv_T) then
18362 Set_Has_Specified_Stream_Write (Full_T);
18363 end if;
18365 if Has_Specified_Stream_Input (Priv_T) then
18366 Set_Has_Specified_Stream_Input (Full_T);
18367 end if;
18369 if Has_Specified_Stream_Output (Priv_T) then
18370 Set_Has_Specified_Stream_Output (Full_T);
18371 end if;
18373 -- Propagate invariants to full type
18375 if Has_Invariants (Priv_T) then
18376 Set_Has_Invariants (Full_T);
18377 Set_Invariant_Procedure (Full_T, Invariant_Procedure (Priv_T));
18378 end if;
18380 if Has_Inheritable_Invariants (Priv_T) then
18381 Set_Has_Inheritable_Invariants (Full_T);
18382 end if;
18384 -- Propagate predicates to full type
18386 if Has_Predicates (Priv_T) then
18387 Set_Predicate_Function (Priv_T, Predicate_Function (Full_T));
18388 Set_Has_Predicates (Full_T);
18389 end if;
18390 end Process_Full_View;
18392 -----------------------------------
18393 -- Process_Incomplete_Dependents --
18394 -----------------------------------
18396 procedure Process_Incomplete_Dependents
18397 (N : Node_Id;
18398 Full_T : Entity_Id;
18399 Inc_T : Entity_Id)
18401 Inc_Elmt : Elmt_Id;
18402 Priv_Dep : Entity_Id;
18403 New_Subt : Entity_Id;
18405 Disc_Constraint : Elist_Id;
18407 begin
18408 if No (Private_Dependents (Inc_T)) then
18409 return;
18410 end if;
18412 -- Itypes that may be generated by the completion of an incomplete
18413 -- subtype are not used by the back-end and not attached to the tree.
18414 -- They are created only for constraint-checking purposes.
18416 Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
18417 while Present (Inc_Elmt) loop
18418 Priv_Dep := Node (Inc_Elmt);
18420 if Ekind (Priv_Dep) = E_Subprogram_Type then
18422 -- An Access_To_Subprogram type may have a return type or a
18423 -- parameter type that is incomplete. Replace with the full view.
18425 if Etype (Priv_Dep) = Inc_T then
18426 Set_Etype (Priv_Dep, Full_T);
18427 end if;
18429 declare
18430 Formal : Entity_Id;
18432 begin
18433 Formal := First_Formal (Priv_Dep);
18434 while Present (Formal) loop
18435 if Etype (Formal) = Inc_T then
18436 Set_Etype (Formal, Full_T);
18437 end if;
18439 Next_Formal (Formal);
18440 end loop;
18441 end;
18443 elsif Is_Overloadable (Priv_Dep) then
18445 -- If a subprogram in the incomplete dependents list is primitive
18446 -- for a tagged full type then mark it as a dispatching operation,
18447 -- check whether it overrides an inherited subprogram, and check
18448 -- restrictions on its controlling formals. Note that a protected
18449 -- operation is never dispatching: only its wrapper operation
18450 -- (which has convention Ada) is.
18452 if Is_Tagged_Type (Full_T)
18453 and then Is_Primitive (Priv_Dep)
18454 and then Convention (Priv_Dep) /= Convention_Protected
18455 then
18456 Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
18457 Set_Is_Dispatching_Operation (Priv_Dep);
18458 Check_Controlling_Formals (Full_T, Priv_Dep);
18459 end if;
18461 elsif Ekind (Priv_Dep) = E_Subprogram_Body then
18463 -- Can happen during processing of a body before the completion
18464 -- of a TA type. Ignore, because spec is also on dependent list.
18466 return;
18468 -- Ada 2005 (AI-412): Transform a regular incomplete subtype into a
18469 -- corresponding subtype of the full view.
18471 elsif Ekind (Priv_Dep) = E_Incomplete_Subtype then
18472 Set_Subtype_Indication
18473 (Parent (Priv_Dep), New_Reference_To (Full_T, Sloc (Priv_Dep)));
18474 Set_Etype (Priv_Dep, Full_T);
18475 Set_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
18476 Set_Analyzed (Parent (Priv_Dep), False);
18478 -- Reanalyze the declaration, suppressing the call to
18479 -- Enter_Name to avoid duplicate names.
18481 Analyze_Subtype_Declaration
18482 (N => Parent (Priv_Dep),
18483 Skip => True);
18485 -- Dependent is a subtype
18487 else
18488 -- We build a new subtype indication using the full view of the
18489 -- incomplete parent. The discriminant constraints have been
18490 -- elaborated already at the point of the subtype declaration.
18492 New_Subt := Create_Itype (E_Void, N);
18494 if Has_Discriminants (Full_T) then
18495 Disc_Constraint := Discriminant_Constraint (Priv_Dep);
18496 else
18497 Disc_Constraint := No_Elist;
18498 end if;
18500 Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
18501 Set_Full_View (Priv_Dep, New_Subt);
18502 end if;
18504 Next_Elmt (Inc_Elmt);
18505 end loop;
18506 end Process_Incomplete_Dependents;
18508 --------------------------------
18509 -- Process_Range_Expr_In_Decl --
18510 --------------------------------
18512 procedure Process_Range_Expr_In_Decl
18513 (R : Node_Id;
18514 T : Entity_Id;
18515 Check_List : List_Id := Empty_List;
18516 R_Check_Off : Boolean := False;
18517 In_Iter_Schm : Boolean := False)
18519 Lo, Hi : Node_Id;
18520 R_Checks : Check_Result;
18521 Insert_Node : Node_Id;
18522 Def_Id : Entity_Id;
18524 begin
18525 Analyze_And_Resolve (R, Base_Type (T));
18527 if Nkind (R) = N_Range then
18529 -- In SPARK, all ranges should be static, with the exception of the
18530 -- discrete type definition of a loop parameter specification.
18532 if not In_Iter_Schm
18533 and then not Is_Static_Range (R)
18534 then
18535 Check_SPARK_Restriction ("range should be static", R);
18536 end if;
18538 Lo := Low_Bound (R);
18539 Hi := High_Bound (R);
18541 -- We need to ensure validity of the bounds here, because if we
18542 -- go ahead and do the expansion, then the expanded code will get
18543 -- analyzed with range checks suppressed and we miss the check.
18545 Validity_Check_Range (R);
18547 -- If there were errors in the declaration, try and patch up some
18548 -- common mistakes in the bounds. The cases handled are literals
18549 -- which are Integer where the expected type is Real and vice versa.
18550 -- These corrections allow the compilation process to proceed further
18551 -- along since some basic assumptions of the format of the bounds
18552 -- are guaranteed.
18554 if Etype (R) = Any_Type then
18556 if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
18557 Rewrite (Lo,
18558 Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
18560 elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
18561 Rewrite (Hi,
18562 Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
18564 elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
18565 Rewrite (Lo,
18566 Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
18568 elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
18569 Rewrite (Hi,
18570 Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
18571 end if;
18573 Set_Etype (Lo, T);
18574 Set_Etype (Hi, T);
18575 end if;
18577 -- If the bounds of the range have been mistakenly given as string
18578 -- literals (perhaps in place of character literals), then an error
18579 -- has already been reported, but we rewrite the string literal as a
18580 -- bound of the range's type to avoid blowups in later processing
18581 -- that looks at static values.
18583 if Nkind (Lo) = N_String_Literal then
18584 Rewrite (Lo,
18585 Make_Attribute_Reference (Sloc (Lo),
18586 Attribute_Name => Name_First,
18587 Prefix => New_Reference_To (T, Sloc (Lo))));
18588 Analyze_And_Resolve (Lo);
18589 end if;
18591 if Nkind (Hi) = N_String_Literal then
18592 Rewrite (Hi,
18593 Make_Attribute_Reference (Sloc (Hi),
18594 Attribute_Name => Name_First,
18595 Prefix => New_Reference_To (T, Sloc (Hi))));
18596 Analyze_And_Resolve (Hi);
18597 end if;
18599 -- If bounds aren't scalar at this point then exit, avoiding
18600 -- problems with further processing of the range in this procedure.
18602 if not Is_Scalar_Type (Etype (Lo)) then
18603 return;
18604 end if;
18606 -- Resolve (actually Sem_Eval) has checked that the bounds are in
18607 -- then range of the base type. Here we check whether the bounds
18608 -- are in the range of the subtype itself. Note that if the bounds
18609 -- represent the null range the Constraint_Error exception should
18610 -- not be raised.
18612 -- ??? The following code should be cleaned up as follows
18614 -- 1. The Is_Null_Range (Lo, Hi) test should disappear since it
18615 -- is done in the call to Range_Check (R, T); below
18617 -- 2. The use of R_Check_Off should be investigated and possibly
18618 -- removed, this would clean up things a bit.
18620 if Is_Null_Range (Lo, Hi) then
18621 null;
18623 else
18624 -- Capture values of bounds and generate temporaries for them
18625 -- if needed, before applying checks, since checks may cause
18626 -- duplication of the expression without forcing evaluation.
18628 -- The forced evaluation removes side effects from expressions,
18629 -- which should occur also in SPARK mode. Otherwise, we end up
18630 -- with unexpected insertions of actions at places where this is
18631 -- not supposed to occur, e.g. on default parameters of a call.
18633 if Expander_Active then
18634 Force_Evaluation (Lo);
18635 Force_Evaluation (Hi);
18636 end if;
18638 -- We use a flag here instead of suppressing checks on the
18639 -- type because the type we check against isn't necessarily
18640 -- the place where we put the check.
18642 if not R_Check_Off then
18643 R_Checks := Get_Range_Checks (R, T);
18645 -- Look up tree to find an appropriate insertion point. We
18646 -- can't just use insert_actions because later processing
18647 -- depends on the insertion node. Prior to Ada 2012 the
18648 -- insertion point could only be a declaration or a loop, but
18649 -- quantified expressions can appear within any context in an
18650 -- expression, and the insertion point can be any statement,
18651 -- pragma, or declaration.
18653 Insert_Node := Parent (R);
18654 while Present (Insert_Node) loop
18655 exit when
18656 Nkind (Insert_Node) in N_Declaration
18657 and then
18658 not Nkind_In
18659 (Insert_Node, N_Component_Declaration,
18660 N_Loop_Parameter_Specification,
18661 N_Function_Specification,
18662 N_Procedure_Specification);
18664 exit when Nkind (Insert_Node) in N_Later_Decl_Item
18665 or else Nkind (Insert_Node) in
18666 N_Statement_Other_Than_Procedure_Call
18667 or else Nkind_In (Insert_Node, N_Procedure_Call_Statement,
18668 N_Pragma);
18670 Insert_Node := Parent (Insert_Node);
18671 end loop;
18673 -- Why would Type_Decl not be present??? Without this test,
18674 -- short regression tests fail.
18676 if Present (Insert_Node) then
18678 -- Case of loop statement. Verify that the range is part
18679 -- of the subtype indication of the iteration scheme.
18681 if Nkind (Insert_Node) = N_Loop_Statement then
18682 declare
18683 Indic : Node_Id;
18685 begin
18686 Indic := Parent (R);
18687 while Present (Indic)
18688 and then Nkind (Indic) /= N_Subtype_Indication
18689 loop
18690 Indic := Parent (Indic);
18691 end loop;
18693 if Present (Indic) then
18694 Def_Id := Etype (Subtype_Mark (Indic));
18696 Insert_Range_Checks
18697 (R_Checks,
18698 Insert_Node,
18699 Def_Id,
18700 Sloc (Insert_Node),
18702 Do_Before => True);
18703 end if;
18704 end;
18706 -- Insertion before a declaration. If the declaration
18707 -- includes discriminants, the list of applicable checks
18708 -- is given by the caller.
18710 elsif Nkind (Insert_Node) in N_Declaration then
18711 Def_Id := Defining_Identifier (Insert_Node);
18713 if (Ekind (Def_Id) = E_Record_Type
18714 and then Depends_On_Discriminant (R))
18715 or else
18716 (Ekind (Def_Id) = E_Protected_Type
18717 and then Has_Discriminants (Def_Id))
18718 then
18719 Append_Range_Checks
18720 (R_Checks,
18721 Check_List, Def_Id, Sloc (Insert_Node), R);
18723 else
18724 Insert_Range_Checks
18725 (R_Checks,
18726 Insert_Node, Def_Id, Sloc (Insert_Node), R);
18728 end if;
18730 -- Insertion before a statement. Range appears in the
18731 -- context of a quantified expression. Insertion will
18732 -- take place when expression is expanded.
18734 else
18735 null;
18736 end if;
18737 end if;
18738 end if;
18739 end if;
18741 -- Case of other than an explicit N_Range node
18743 -- The forced evaluation removes side effects from expressions, which
18744 -- should occur also in SPARK mode. Otherwise, we end up with unexpected
18745 -- insertions of actions at places where this is not supposed to occur,
18746 -- e.g. on default parameters of a call.
18748 elsif Expander_Active then
18749 Get_Index_Bounds (R, Lo, Hi);
18750 Force_Evaluation (Lo);
18751 Force_Evaluation (Hi);
18752 end if;
18753 end Process_Range_Expr_In_Decl;
18755 --------------------------------------
18756 -- Process_Real_Range_Specification --
18757 --------------------------------------
18759 procedure Process_Real_Range_Specification (Def : Node_Id) is
18760 Spec : constant Node_Id := Real_Range_Specification (Def);
18761 Lo : Node_Id;
18762 Hi : Node_Id;
18763 Err : Boolean := False;
18765 procedure Analyze_Bound (N : Node_Id);
18766 -- Analyze and check one bound
18768 -------------------
18769 -- Analyze_Bound --
18770 -------------------
18772 procedure Analyze_Bound (N : Node_Id) is
18773 begin
18774 Analyze_And_Resolve (N, Any_Real);
18776 if not Is_OK_Static_Expression (N) then
18777 Flag_Non_Static_Expr
18778 ("bound in real type definition is not static!", N);
18779 Err := True;
18780 end if;
18781 end Analyze_Bound;
18783 -- Start of processing for Process_Real_Range_Specification
18785 begin
18786 if Present (Spec) then
18787 Lo := Low_Bound (Spec);
18788 Hi := High_Bound (Spec);
18789 Analyze_Bound (Lo);
18790 Analyze_Bound (Hi);
18792 -- If error, clear away junk range specification
18794 if Err then
18795 Set_Real_Range_Specification (Def, Empty);
18796 end if;
18797 end if;
18798 end Process_Real_Range_Specification;
18800 ---------------------
18801 -- Process_Subtype --
18802 ---------------------
18804 function Process_Subtype
18805 (S : Node_Id;
18806 Related_Nod : Node_Id;
18807 Related_Id : Entity_Id := Empty;
18808 Suffix : Character := ' ') return Entity_Id
18810 P : Node_Id;
18811 Def_Id : Entity_Id;
18812 Error_Node : Node_Id;
18813 Full_View_Id : Entity_Id;
18814 Subtype_Mark_Id : Entity_Id;
18816 May_Have_Null_Exclusion : Boolean;
18818 procedure Check_Incomplete (T : Entity_Id);
18819 -- Called to verify that an incomplete type is not used prematurely
18821 ----------------------
18822 -- Check_Incomplete --
18823 ----------------------
18825 procedure Check_Incomplete (T : Entity_Id) is
18826 begin
18827 -- Ada 2005 (AI-412): Incomplete subtypes are legal
18829 if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
18830 and then
18831 not (Ada_Version >= Ada_2005
18832 and then
18833 (Nkind (Parent (T)) = N_Subtype_Declaration
18834 or else
18835 (Nkind (Parent (T)) = N_Subtype_Indication
18836 and then Nkind (Parent (Parent (T))) =
18837 N_Subtype_Declaration)))
18838 then
18839 Error_Msg_N ("invalid use of type before its full declaration", T);
18840 end if;
18841 end Check_Incomplete;
18843 -- Start of processing for Process_Subtype
18845 begin
18846 -- Case of no constraints present
18848 if Nkind (S) /= N_Subtype_Indication then
18849 Find_Type (S);
18850 Check_Incomplete (S);
18851 P := Parent (S);
18853 -- Ada 2005 (AI-231): Static check
18855 if Ada_Version >= Ada_2005
18856 and then Present (P)
18857 and then Null_Exclusion_Present (P)
18858 and then Nkind (P) /= N_Access_To_Object_Definition
18859 and then not Is_Access_Type (Entity (S))
18860 then
18861 Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
18862 end if;
18864 -- The following is ugly, can't we have a range or even a flag???
18866 May_Have_Null_Exclusion :=
18867 Nkind_In (P, N_Access_Definition,
18868 N_Access_Function_Definition,
18869 N_Access_Procedure_Definition,
18870 N_Access_To_Object_Definition,
18871 N_Allocator,
18872 N_Component_Definition)
18873 or else
18874 Nkind_In (P, N_Derived_Type_Definition,
18875 N_Discriminant_Specification,
18876 N_Formal_Object_Declaration,
18877 N_Object_Declaration,
18878 N_Object_Renaming_Declaration,
18879 N_Parameter_Specification,
18880 N_Subtype_Declaration);
18882 -- Create an Itype that is a duplicate of Entity (S) but with the
18883 -- null-exclusion attribute.
18885 if May_Have_Null_Exclusion
18886 and then Is_Access_Type (Entity (S))
18887 and then Null_Exclusion_Present (P)
18889 -- No need to check the case of an access to object definition.
18890 -- It is correct to define double not-null pointers.
18892 -- Example:
18893 -- type Not_Null_Int_Ptr is not null access Integer;
18894 -- type Acc is not null access Not_Null_Int_Ptr;
18896 and then Nkind (P) /= N_Access_To_Object_Definition
18897 then
18898 if Can_Never_Be_Null (Entity (S)) then
18899 case Nkind (Related_Nod) is
18900 when N_Full_Type_Declaration =>
18901 if Nkind (Type_Definition (Related_Nod))
18902 in N_Array_Type_Definition
18903 then
18904 Error_Node :=
18905 Subtype_Indication
18906 (Component_Definition
18907 (Type_Definition (Related_Nod)));
18908 else
18909 Error_Node :=
18910 Subtype_Indication (Type_Definition (Related_Nod));
18911 end if;
18913 when N_Subtype_Declaration =>
18914 Error_Node := Subtype_Indication (Related_Nod);
18916 when N_Object_Declaration =>
18917 Error_Node := Object_Definition (Related_Nod);
18919 when N_Component_Declaration =>
18920 Error_Node :=
18921 Subtype_Indication (Component_Definition (Related_Nod));
18923 when N_Allocator =>
18924 Error_Node := Expression (Related_Nod);
18926 when others =>
18927 pragma Assert (False);
18928 Error_Node := Related_Nod;
18929 end case;
18931 Error_Msg_NE
18932 ("`NOT NULL` not allowed (& already excludes null)",
18933 Error_Node,
18934 Entity (S));
18935 end if;
18937 Set_Etype (S,
18938 Create_Null_Excluding_Itype
18939 (T => Entity (S),
18940 Related_Nod => P));
18941 Set_Entity (S, Etype (S));
18942 end if;
18944 return Entity (S);
18946 -- Case of constraint present, so that we have an N_Subtype_Indication
18947 -- node (this node is created only if constraints are present).
18949 else
18950 Find_Type (Subtype_Mark (S));
18952 if Nkind (Parent (S)) /= N_Access_To_Object_Definition
18953 and then not
18954 (Nkind (Parent (S)) = N_Subtype_Declaration
18955 and then Is_Itype (Defining_Identifier (Parent (S))))
18956 then
18957 Check_Incomplete (Subtype_Mark (S));
18958 end if;
18960 P := Parent (S);
18961 Subtype_Mark_Id := Entity (Subtype_Mark (S));
18963 -- Explicit subtype declaration case
18965 if Nkind (P) = N_Subtype_Declaration then
18966 Def_Id := Defining_Identifier (P);
18968 -- Explicit derived type definition case
18970 elsif Nkind (P) = N_Derived_Type_Definition then
18971 Def_Id := Defining_Identifier (Parent (P));
18973 -- Implicit case, the Def_Id must be created as an implicit type.
18974 -- The one exception arises in the case of concurrent types, array
18975 -- and access types, where other subsidiary implicit types may be
18976 -- created and must appear before the main implicit type. In these
18977 -- cases we leave Def_Id set to Empty as a signal that Create_Itype
18978 -- has not yet been called to create Def_Id.
18980 else
18981 if Is_Array_Type (Subtype_Mark_Id)
18982 or else Is_Concurrent_Type (Subtype_Mark_Id)
18983 or else Is_Access_Type (Subtype_Mark_Id)
18984 then
18985 Def_Id := Empty;
18987 -- For the other cases, we create a new unattached Itype,
18988 -- and set the indication to ensure it gets attached later.
18990 else
18991 Def_Id :=
18992 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
18993 end if;
18994 end if;
18996 -- If the kind of constraint is invalid for this kind of type,
18997 -- then give an error, and then pretend no constraint was given.
18999 if not Is_Valid_Constraint_Kind
19000 (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
19001 then
19002 Error_Msg_N
19003 ("incorrect constraint for this kind of type", Constraint (S));
19005 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
19007 -- Set Ekind of orphan itype, to prevent cascaded errors
19009 if Present (Def_Id) then
19010 Set_Ekind (Def_Id, Ekind (Any_Type));
19011 end if;
19013 -- Make recursive call, having got rid of the bogus constraint
19015 return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
19016 end if;
19018 -- Remaining processing depends on type. Select on Base_Type kind to
19019 -- ensure getting to the concrete type kind in the case of a private
19020 -- subtype (needed when only doing semantic analysis).
19022 case Ekind (Base_Type (Subtype_Mark_Id)) is
19023 when Access_Kind =>
19024 Constrain_Access (Def_Id, S, Related_Nod);
19026 if Expander_Active
19027 and then Is_Itype (Designated_Type (Def_Id))
19028 and then Nkind (Related_Nod) = N_Subtype_Declaration
19029 and then not Is_Incomplete_Type (Designated_Type (Def_Id))
19030 then
19031 Build_Itype_Reference
19032 (Designated_Type (Def_Id), Related_Nod);
19033 end if;
19035 when Array_Kind =>
19036 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
19038 when Decimal_Fixed_Point_Kind =>
19039 Constrain_Decimal (Def_Id, S);
19041 when Enumeration_Kind =>
19042 Constrain_Enumeration (Def_Id, S);
19044 when Ordinary_Fixed_Point_Kind =>
19045 Constrain_Ordinary_Fixed (Def_Id, S);
19047 when Float_Kind =>
19048 Constrain_Float (Def_Id, S);
19050 when Integer_Kind =>
19051 Constrain_Integer (Def_Id, S);
19053 when E_Record_Type |
19054 E_Record_Subtype |
19055 Class_Wide_Kind |
19056 E_Incomplete_Type =>
19057 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
19059 if Ekind (Def_Id) = E_Incomplete_Type then
19060 Set_Private_Dependents (Def_Id, New_Elmt_List);
19061 end if;
19063 when Private_Kind =>
19064 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
19065 Set_Private_Dependents (Def_Id, New_Elmt_List);
19067 -- In case of an invalid constraint prevent further processing
19068 -- since the type constructed is missing expected fields.
19070 if Etype (Def_Id) = Any_Type then
19071 return Def_Id;
19072 end if;
19074 -- If the full view is that of a task with discriminants,
19075 -- we must constrain both the concurrent type and its
19076 -- corresponding record type. Otherwise we will just propagate
19077 -- the constraint to the full view, if available.
19079 if Present (Full_View (Subtype_Mark_Id))
19080 and then Has_Discriminants (Subtype_Mark_Id)
19081 and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
19082 then
19083 Full_View_Id :=
19084 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
19086 Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
19087 Constrain_Concurrent (Full_View_Id, S,
19088 Related_Nod, Related_Id, Suffix);
19089 Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
19090 Set_Full_View (Def_Id, Full_View_Id);
19092 -- Introduce an explicit reference to the private subtype,
19093 -- to prevent scope anomalies in gigi if first use appears
19094 -- in a nested context, e.g. a later function body.
19095 -- Should this be generated in other contexts than a full
19096 -- type declaration?
19098 if Is_Itype (Def_Id)
19099 and then
19100 Nkind (Parent (P)) = N_Full_Type_Declaration
19101 then
19102 Build_Itype_Reference (Def_Id, Parent (P));
19103 end if;
19105 else
19106 Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
19107 end if;
19109 when Concurrent_Kind =>
19110 Constrain_Concurrent (Def_Id, S,
19111 Related_Nod, Related_Id, Suffix);
19113 when others =>
19114 Error_Msg_N ("invalid subtype mark in subtype indication", S);
19115 end case;
19117 -- Size and Convention are always inherited from the base type
19119 Set_Size_Info (Def_Id, (Subtype_Mark_Id));
19120 Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
19122 return Def_Id;
19123 end if;
19124 end Process_Subtype;
19126 ---------------------------------------
19127 -- Check_Anonymous_Access_Components --
19128 ---------------------------------------
19130 procedure Check_Anonymous_Access_Components
19131 (Typ_Decl : Node_Id;
19132 Typ : Entity_Id;
19133 Prev : Entity_Id;
19134 Comp_List : Node_Id)
19136 Loc : constant Source_Ptr := Sloc (Typ_Decl);
19137 Anon_Access : Entity_Id;
19138 Acc_Def : Node_Id;
19139 Comp : Node_Id;
19140 Comp_Def : Node_Id;
19141 Decl : Node_Id;
19142 Type_Def : Node_Id;
19144 procedure Build_Incomplete_Type_Declaration;
19145 -- If the record type contains components that include an access to the
19146 -- current record, then create an incomplete type declaration for the
19147 -- record, to be used as the designated type of the anonymous access.
19148 -- This is done only once, and only if there is no previous partial
19149 -- view of the type.
19151 function Designates_T (Subt : Node_Id) return Boolean;
19152 -- Check whether a node designates the enclosing record type, or 'Class
19153 -- of that type
19155 function Mentions_T (Acc_Def : Node_Id) return Boolean;
19156 -- Check whether an access definition includes a reference to
19157 -- the enclosing record type. The reference can be a subtype mark
19158 -- in the access definition itself, a 'Class attribute reference, or
19159 -- recursively a reference appearing in a parameter specification
19160 -- or result definition of an access_to_subprogram definition.
19162 --------------------------------------
19163 -- Build_Incomplete_Type_Declaration --
19164 --------------------------------------
19166 procedure Build_Incomplete_Type_Declaration is
19167 Decl : Node_Id;
19168 Inc_T : Entity_Id;
19169 H : Entity_Id;
19171 -- Is_Tagged indicates whether the type is tagged. It is tagged if
19172 -- it's "is new ... with record" or else "is tagged record ...".
19174 Is_Tagged : constant Boolean :=
19175 (Nkind (Type_Definition (Typ_Decl)) = N_Derived_Type_Definition
19176 and then
19177 Present
19178 (Record_Extension_Part (Type_Definition (Typ_Decl))))
19179 or else
19180 (Nkind (Type_Definition (Typ_Decl)) = N_Record_Definition
19181 and then Tagged_Present (Type_Definition (Typ_Decl)));
19183 begin
19184 -- If there is a previous partial view, no need to create a new one
19185 -- If the partial view, given by Prev, is incomplete, If Prev is
19186 -- a private declaration, full declaration is flagged accordingly.
19188 if Prev /= Typ then
19189 if Is_Tagged then
19190 Make_Class_Wide_Type (Prev);
19191 Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
19192 Set_Etype (Class_Wide_Type (Typ), Typ);
19193 end if;
19195 return;
19197 elsif Has_Private_Declaration (Typ) then
19199 -- If we refer to T'Class inside T, and T is the completion of a
19200 -- private type, then we need to make sure the class-wide type
19201 -- exists.
19203 if Is_Tagged then
19204 Make_Class_Wide_Type (Typ);
19205 end if;
19207 return;
19209 -- If there was a previous anonymous access type, the incomplete
19210 -- type declaration will have been created already.
19212 elsif Present (Current_Entity (Typ))
19213 and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
19214 and then Full_View (Current_Entity (Typ)) = Typ
19215 then
19216 if Is_Tagged
19217 and then Comes_From_Source (Current_Entity (Typ))
19218 and then not Is_Tagged_Type (Current_Entity (Typ))
19219 then
19220 Make_Class_Wide_Type (Typ);
19221 Error_Msg_N
19222 ("incomplete view of tagged type should be declared tagged??",
19223 Parent (Current_Entity (Typ)));
19224 end if;
19225 return;
19227 else
19228 Inc_T := Make_Defining_Identifier (Loc, Chars (Typ));
19229 Decl := Make_Incomplete_Type_Declaration (Loc, Inc_T);
19231 -- Type has already been inserted into the current scope. Remove
19232 -- it, and add incomplete declaration for type, so that subsequent
19233 -- anonymous access types can use it. The entity is unchained from
19234 -- the homonym list and from immediate visibility. After analysis,
19235 -- the entity in the incomplete declaration becomes immediately
19236 -- visible in the record declaration that follows.
19238 H := Current_Entity (Typ);
19240 if H = Typ then
19241 Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
19242 else
19243 while Present (H)
19244 and then Homonym (H) /= Typ
19245 loop
19246 H := Homonym (Typ);
19247 end loop;
19249 Set_Homonym (H, Homonym (Typ));
19250 end if;
19252 Insert_Before (Typ_Decl, Decl);
19253 Analyze (Decl);
19254 Set_Full_View (Inc_T, Typ);
19256 if Is_Tagged then
19258 -- Create a common class-wide type for both views, and set the
19259 -- Etype of the class-wide type to the full view.
19261 Make_Class_Wide_Type (Inc_T);
19262 Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
19263 Set_Etype (Class_Wide_Type (Typ), Typ);
19264 end if;
19265 end if;
19266 end Build_Incomplete_Type_Declaration;
19268 ------------------
19269 -- Designates_T --
19270 ------------------
19272 function Designates_T (Subt : Node_Id) return Boolean is
19273 Type_Id : constant Name_Id := Chars (Typ);
19275 function Names_T (Nam : Node_Id) return Boolean;
19276 -- The record type has not been introduced in the current scope
19277 -- yet, so we must examine the name of the type itself, either
19278 -- an identifier T, or an expanded name of the form P.T, where
19279 -- P denotes the current scope.
19281 -------------
19282 -- Names_T --
19283 -------------
19285 function Names_T (Nam : Node_Id) return Boolean is
19286 begin
19287 if Nkind (Nam) = N_Identifier then
19288 return Chars (Nam) = Type_Id;
19290 elsif Nkind (Nam) = N_Selected_Component then
19291 if Chars (Selector_Name (Nam)) = Type_Id then
19292 if Nkind (Prefix (Nam)) = N_Identifier then
19293 return Chars (Prefix (Nam)) = Chars (Current_Scope);
19295 elsif Nkind (Prefix (Nam)) = N_Selected_Component then
19296 return Chars (Selector_Name (Prefix (Nam))) =
19297 Chars (Current_Scope);
19298 else
19299 return False;
19300 end if;
19302 else
19303 return False;
19304 end if;
19306 else
19307 return False;
19308 end if;
19309 end Names_T;
19311 -- Start of processing for Designates_T
19313 begin
19314 if Nkind (Subt) = N_Identifier then
19315 return Chars (Subt) = Type_Id;
19317 -- Reference can be through an expanded name which has not been
19318 -- analyzed yet, and which designates enclosing scopes.
19320 elsif Nkind (Subt) = N_Selected_Component then
19321 if Names_T (Subt) then
19322 return True;
19324 -- Otherwise it must denote an entity that is already visible.
19325 -- The access definition may name a subtype of the enclosing
19326 -- type, if there is a previous incomplete declaration for it.
19328 else
19329 Find_Selected_Component (Subt);
19330 return
19331 Is_Entity_Name (Subt)
19332 and then Scope (Entity (Subt)) = Current_Scope
19333 and then
19334 (Chars (Base_Type (Entity (Subt))) = Type_Id
19335 or else
19336 (Is_Class_Wide_Type (Entity (Subt))
19337 and then
19338 Chars (Etype (Base_Type (Entity (Subt)))) =
19339 Type_Id));
19340 end if;
19342 -- A reference to the current type may appear as the prefix of
19343 -- a 'Class attribute.
19345 elsif Nkind (Subt) = N_Attribute_Reference
19346 and then Attribute_Name (Subt) = Name_Class
19347 then
19348 return Names_T (Prefix (Subt));
19350 else
19351 return False;
19352 end if;
19353 end Designates_T;
19355 ----------------
19356 -- Mentions_T --
19357 ----------------
19359 function Mentions_T (Acc_Def : Node_Id) return Boolean is
19360 Param_Spec : Node_Id;
19362 Acc_Subprg : constant Node_Id :=
19363 Access_To_Subprogram_Definition (Acc_Def);
19365 begin
19366 if No (Acc_Subprg) then
19367 return Designates_T (Subtype_Mark (Acc_Def));
19368 end if;
19370 -- Component is an access_to_subprogram: examine its formals,
19371 -- and result definition in the case of an access_to_function.
19373 Param_Spec := First (Parameter_Specifications (Acc_Subprg));
19374 while Present (Param_Spec) loop
19375 if Nkind (Parameter_Type (Param_Spec)) = N_Access_Definition
19376 and then Mentions_T (Parameter_Type (Param_Spec))
19377 then
19378 return True;
19380 elsif Designates_T (Parameter_Type (Param_Spec)) then
19381 return True;
19382 end if;
19384 Next (Param_Spec);
19385 end loop;
19387 if Nkind (Acc_Subprg) = N_Access_Function_Definition then
19388 if Nkind (Result_Definition (Acc_Subprg)) =
19389 N_Access_Definition
19390 then
19391 return Mentions_T (Result_Definition (Acc_Subprg));
19392 else
19393 return Designates_T (Result_Definition (Acc_Subprg));
19394 end if;
19395 end if;
19397 return False;
19398 end Mentions_T;
19400 -- Start of processing for Check_Anonymous_Access_Components
19402 begin
19403 if No (Comp_List) then
19404 return;
19405 end if;
19407 Comp := First (Component_Items (Comp_List));
19408 while Present (Comp) loop
19409 if Nkind (Comp) = N_Component_Declaration
19410 and then Present
19411 (Access_Definition (Component_Definition (Comp)))
19412 and then
19413 Mentions_T (Access_Definition (Component_Definition (Comp)))
19414 then
19415 Comp_Def := Component_Definition (Comp);
19416 Acc_Def :=
19417 Access_To_Subprogram_Definition
19418 (Access_Definition (Comp_Def));
19420 Build_Incomplete_Type_Declaration;
19421 Anon_Access := Make_Temporary (Loc, 'S');
19423 -- Create a declaration for the anonymous access type: either
19424 -- an access_to_object or an access_to_subprogram.
19426 if Present (Acc_Def) then
19427 if Nkind (Acc_Def) = N_Access_Function_Definition then
19428 Type_Def :=
19429 Make_Access_Function_Definition (Loc,
19430 Parameter_Specifications =>
19431 Parameter_Specifications (Acc_Def),
19432 Result_Definition => Result_Definition (Acc_Def));
19433 else
19434 Type_Def :=
19435 Make_Access_Procedure_Definition (Loc,
19436 Parameter_Specifications =>
19437 Parameter_Specifications (Acc_Def));
19438 end if;
19440 else
19441 Type_Def :=
19442 Make_Access_To_Object_Definition (Loc,
19443 Subtype_Indication =>
19444 Relocate_Node
19445 (Subtype_Mark
19446 (Access_Definition (Comp_Def))));
19448 Set_Constant_Present
19449 (Type_Def, Constant_Present (Access_Definition (Comp_Def)));
19450 Set_All_Present
19451 (Type_Def, All_Present (Access_Definition (Comp_Def)));
19452 end if;
19454 Set_Null_Exclusion_Present
19455 (Type_Def,
19456 Null_Exclusion_Present (Access_Definition (Comp_Def)));
19458 Decl :=
19459 Make_Full_Type_Declaration (Loc,
19460 Defining_Identifier => Anon_Access,
19461 Type_Definition => Type_Def);
19463 Insert_Before (Typ_Decl, Decl);
19464 Analyze (Decl);
19466 -- If an access to subprogram, create the extra formals
19468 if Present (Acc_Def) then
19469 Create_Extra_Formals (Designated_Type (Anon_Access));
19471 -- If an access to object, preserve entity of designated type,
19472 -- for ASIS use, before rewriting the component definition.
19474 else
19475 declare
19476 Desig : Entity_Id;
19478 begin
19479 Desig := Entity (Subtype_Indication (Type_Def));
19481 -- If the access definition is to the current record,
19482 -- the visible entity at this point is an incomplete
19483 -- type. Retrieve the full view to simplify ASIS queries
19485 if Ekind (Desig) = E_Incomplete_Type then
19486 Desig := Full_View (Desig);
19487 end if;
19489 Set_Entity
19490 (Subtype_Mark (Access_Definition (Comp_Def)), Desig);
19491 end;
19492 end if;
19494 Rewrite (Comp_Def,
19495 Make_Component_Definition (Loc,
19496 Subtype_Indication =>
19497 New_Occurrence_Of (Anon_Access, Loc)));
19499 if Ekind (Designated_Type (Anon_Access)) = E_Subprogram_Type then
19500 Set_Ekind (Anon_Access, E_Anonymous_Access_Subprogram_Type);
19501 else
19502 Set_Ekind (Anon_Access, E_Anonymous_Access_Type);
19503 end if;
19505 Set_Is_Local_Anonymous_Access (Anon_Access);
19506 end if;
19508 Next (Comp);
19509 end loop;
19511 if Present (Variant_Part (Comp_List)) then
19512 declare
19513 V : Node_Id;
19514 begin
19515 V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
19516 while Present (V) loop
19517 Check_Anonymous_Access_Components
19518 (Typ_Decl, Typ, Prev, Component_List (V));
19519 Next_Non_Pragma (V);
19520 end loop;
19521 end;
19522 end if;
19523 end Check_Anonymous_Access_Components;
19525 ----------------------------------
19526 -- Preanalyze_Assert_Expression --
19527 ----------------------------------
19529 procedure Preanalyze_Assert_Expression (N : Node_Id; T : Entity_Id) is
19530 begin
19531 In_Assertion_Expr := In_Assertion_Expr + 1;
19532 Preanalyze_Spec_Expression (N, T);
19533 In_Assertion_Expr := In_Assertion_Expr - 1;
19534 end Preanalyze_Assert_Expression;
19536 --------------------------------
19537 -- Preanalyze_Spec_Expression --
19538 --------------------------------
19540 procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
19541 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
19542 begin
19543 In_Spec_Expression := True;
19544 Preanalyze_And_Resolve (N, T);
19545 In_Spec_Expression := Save_In_Spec_Expression;
19546 end Preanalyze_Spec_Expression;
19548 -----------------------------
19549 -- Record_Type_Declaration --
19550 -----------------------------
19552 procedure Record_Type_Declaration
19553 (T : Entity_Id;
19554 N : Node_Id;
19555 Prev : Entity_Id)
19557 Def : constant Node_Id := Type_Definition (N);
19558 Is_Tagged : Boolean;
19559 Tag_Comp : Entity_Id;
19561 begin
19562 -- These flags must be initialized before calling Process_Discriminants
19563 -- because this routine makes use of them.
19565 Set_Ekind (T, E_Record_Type);
19566 Set_Etype (T, T);
19567 Init_Size_Align (T);
19568 Set_Interfaces (T, No_Elist);
19569 Set_Stored_Constraint (T, No_Elist);
19571 -- Normal case
19573 if Ada_Version < Ada_2005
19574 or else not Interface_Present (Def)
19575 then
19576 if Limited_Present (Def) then
19577 Check_SPARK_Restriction ("limited is not allowed", N);
19578 end if;
19580 if Abstract_Present (Def) then
19581 Check_SPARK_Restriction ("abstract is not allowed", N);
19582 end if;
19584 -- The flag Is_Tagged_Type might have already been set by
19585 -- Find_Type_Name if it detected an error for declaration T. This
19586 -- arises in the case of private tagged types where the full view
19587 -- omits the word tagged.
19589 Is_Tagged :=
19590 Tagged_Present (Def)
19591 or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
19593 Set_Is_Tagged_Type (T, Is_Tagged);
19594 Set_Is_Limited_Record (T, Limited_Present (Def));
19596 -- Type is abstract if full declaration carries keyword, or if
19597 -- previous partial view did.
19599 Set_Is_Abstract_Type (T, Is_Abstract_Type (T)
19600 or else Abstract_Present (Def));
19602 else
19603 Check_SPARK_Restriction ("interface is not allowed", N);
19605 Is_Tagged := True;
19606 Analyze_Interface_Declaration (T, Def);
19608 if Present (Discriminant_Specifications (N)) then
19609 Error_Msg_N
19610 ("interface types cannot have discriminants",
19611 Defining_Identifier
19612 (First (Discriminant_Specifications (N))));
19613 end if;
19614 end if;
19616 -- First pass: if there are self-referential access components,
19617 -- create the required anonymous access type declarations, and if
19618 -- need be an incomplete type declaration for T itself.
19620 Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
19622 if Ada_Version >= Ada_2005
19623 and then Present (Interface_List (Def))
19624 then
19625 Check_Interfaces (N, Def);
19627 declare
19628 Ifaces_List : Elist_Id;
19630 begin
19631 -- Ada 2005 (AI-251): Collect the list of progenitors that are not
19632 -- already in the parents.
19634 Collect_Interfaces
19635 (T => T,
19636 Ifaces_List => Ifaces_List,
19637 Exclude_Parents => True);
19639 Set_Interfaces (T, Ifaces_List);
19640 end;
19641 end if;
19643 -- Records constitute a scope for the component declarations within.
19644 -- The scope is created prior to the processing of these declarations.
19645 -- Discriminants are processed first, so that they are visible when
19646 -- processing the other components. The Ekind of the record type itself
19647 -- is set to E_Record_Type (subtypes appear as E_Record_Subtype).
19649 -- Enter record scope
19651 Push_Scope (T);
19653 -- If an incomplete or private type declaration was already given for
19654 -- the type, then this scope already exists, and the discriminants have
19655 -- been declared within. We must verify that the full declaration
19656 -- matches the incomplete one.
19658 Check_Or_Process_Discriminants (N, T, Prev);
19660 Set_Is_Constrained (T, not Has_Discriminants (T));
19661 Set_Has_Delayed_Freeze (T, True);
19663 -- For tagged types add a manually analyzed component corresponding
19664 -- to the component _tag, the corresponding piece of tree will be
19665 -- expanded as part of the freezing actions if it is not a CPP_Class.
19667 if Is_Tagged then
19669 -- Do not add the tag unless we are in expansion mode
19671 if Expander_Active then
19672 Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
19673 Enter_Name (Tag_Comp);
19675 Set_Ekind (Tag_Comp, E_Component);
19676 Set_Is_Tag (Tag_Comp);
19677 Set_Is_Aliased (Tag_Comp);
19678 Set_Etype (Tag_Comp, RTE (RE_Tag));
19679 Set_DT_Entry_Count (Tag_Comp, No_Uint);
19680 Set_Original_Record_Component (Tag_Comp, Tag_Comp);
19681 Init_Component_Location (Tag_Comp);
19683 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
19684 -- implemented interfaces.
19686 if Has_Interfaces (T) then
19687 Add_Interface_Tag_Components (N, T);
19688 end if;
19689 end if;
19691 Make_Class_Wide_Type (T);
19692 Set_Direct_Primitive_Operations (T, New_Elmt_List);
19693 end if;
19695 -- We must suppress range checks when processing record components in
19696 -- the presence of discriminants, since we don't want spurious checks to
19697 -- be generated during their analysis, but Suppress_Range_Checks flags
19698 -- must be reset the after processing the record definition.
19700 -- Note: this is the only use of Kill_Range_Checks, and is a bit odd,
19701 -- couldn't we just use the normal range check suppression method here.
19702 -- That would seem cleaner ???
19704 if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
19705 Set_Kill_Range_Checks (T, True);
19706 Record_Type_Definition (Def, Prev);
19707 Set_Kill_Range_Checks (T, False);
19708 else
19709 Record_Type_Definition (Def, Prev);
19710 end if;
19712 -- Exit from record scope
19714 End_Scope;
19716 -- Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
19717 -- the implemented interfaces and associate them an aliased entity.
19719 if Is_Tagged
19720 and then not Is_Empty_List (Interface_List (Def))
19721 then
19722 Derive_Progenitor_Subprograms (T, T);
19723 end if;
19725 Check_Function_Writable_Actuals (N);
19726 end Record_Type_Declaration;
19728 ----------------------------
19729 -- Record_Type_Definition --
19730 ----------------------------
19732 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
19733 Component : Entity_Id;
19734 Ctrl_Components : Boolean := False;
19735 Final_Storage_Only : Boolean;
19736 T : Entity_Id;
19738 begin
19739 if Ekind (Prev_T) = E_Incomplete_Type then
19740 T := Full_View (Prev_T);
19741 else
19742 T := Prev_T;
19743 end if;
19745 -- In SPARK, tagged types and type extensions may only be declared in
19746 -- the specification of library unit packages.
19748 if Present (Def) and then Is_Tagged_Type (T) then
19749 declare
19750 Typ : Node_Id;
19751 Ctxt : Node_Id;
19753 begin
19754 if Nkind (Parent (Def)) = N_Full_Type_Declaration then
19755 Typ := Parent (Def);
19756 else
19757 pragma Assert
19758 (Nkind (Parent (Def)) = N_Derived_Type_Definition);
19759 Typ := Parent (Parent (Def));
19760 end if;
19762 Ctxt := Parent (Typ);
19764 if Nkind (Ctxt) = N_Package_Body
19765 and then Nkind (Parent (Ctxt)) = N_Compilation_Unit
19766 then
19767 Check_SPARK_Restriction
19768 ("type should be defined in package specification", Typ);
19770 elsif Nkind (Ctxt) /= N_Package_Specification
19771 or else Nkind (Parent (Parent (Ctxt))) /= N_Compilation_Unit
19772 then
19773 Check_SPARK_Restriction
19774 ("type should be defined in library unit package", Typ);
19775 end if;
19776 end;
19777 end if;
19779 Final_Storage_Only := not Is_Controlled (T);
19781 -- Ada 2005: check whether an explicit Limited is present in a derived
19782 -- type declaration.
19784 if Nkind (Parent (Def)) = N_Derived_Type_Definition
19785 and then Limited_Present (Parent (Def))
19786 then
19787 Set_Is_Limited_Record (T);
19788 end if;
19790 -- If the component list of a record type is defined by the reserved
19791 -- word null and there is no discriminant part, then the record type has
19792 -- no components and all records of the type are null records (RM 3.7)
19793 -- This procedure is also called to process the extension part of a
19794 -- record extension, in which case the current scope may have inherited
19795 -- components.
19797 if No (Def)
19798 or else No (Component_List (Def))
19799 or else Null_Present (Component_List (Def))
19800 then
19801 if not Is_Tagged_Type (T) then
19802 Check_SPARK_Restriction ("non-tagged record cannot be null", Def);
19803 end if;
19805 else
19806 Analyze_Declarations (Component_Items (Component_List (Def)));
19808 if Present (Variant_Part (Component_List (Def))) then
19809 Check_SPARK_Restriction ("variant part is not allowed", Def);
19810 Analyze (Variant_Part (Component_List (Def)));
19811 end if;
19812 end if;
19814 -- After completing the semantic analysis of the record definition,
19815 -- record components, both new and inherited, are accessible. Set their
19816 -- kind accordingly. Exclude malformed itypes from illegal declarations,
19817 -- whose Ekind may be void.
19819 Component := First_Entity (Current_Scope);
19820 while Present (Component) loop
19821 if Ekind (Component) = E_Void
19822 and then not Is_Itype (Component)
19823 then
19824 Set_Ekind (Component, E_Component);
19825 Init_Component_Location (Component);
19826 end if;
19828 if Has_Task (Etype (Component)) then
19829 Set_Has_Task (T);
19830 end if;
19832 if Ekind (Component) /= E_Component then
19833 null;
19835 -- Do not set Has_Controlled_Component on a class-wide equivalent
19836 -- type. See Make_CW_Equivalent_Type.
19838 elsif not Is_Class_Wide_Equivalent_Type (T)
19839 and then (Has_Controlled_Component (Etype (Component))
19840 or else (Chars (Component) /= Name_uParent
19841 and then Is_Controlled (Etype (Component))))
19842 then
19843 Set_Has_Controlled_Component (T, True);
19844 Final_Storage_Only :=
19845 Final_Storage_Only
19846 and then Finalize_Storage_Only (Etype (Component));
19847 Ctrl_Components := True;
19848 end if;
19850 Next_Entity (Component);
19851 end loop;
19853 -- A Type is Finalize_Storage_Only only if all its controlled components
19854 -- are also.
19856 if Ctrl_Components then
19857 Set_Finalize_Storage_Only (T, Final_Storage_Only);
19858 end if;
19860 -- Place reference to end record on the proper entity, which may
19861 -- be a partial view.
19863 if Present (Def) then
19864 Process_End_Label (Def, 'e', Prev_T);
19865 end if;
19866 end Record_Type_Definition;
19868 ------------------------
19869 -- Replace_Components --
19870 ------------------------
19872 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id) is
19873 function Process (N : Node_Id) return Traverse_Result;
19875 -------------
19876 -- Process --
19877 -------------
19879 function Process (N : Node_Id) return Traverse_Result is
19880 Comp : Entity_Id;
19882 begin
19883 if Nkind (N) = N_Discriminant_Specification then
19884 Comp := First_Discriminant (Typ);
19885 while Present (Comp) loop
19886 if Chars (Comp) = Chars (Defining_Identifier (N)) then
19887 Set_Defining_Identifier (N, Comp);
19888 exit;
19889 end if;
19891 Next_Discriminant (Comp);
19892 end loop;
19894 elsif Nkind (N) = N_Component_Declaration then
19895 Comp := First_Component (Typ);
19896 while Present (Comp) loop
19897 if Chars (Comp) = Chars (Defining_Identifier (N)) then
19898 Set_Defining_Identifier (N, Comp);
19899 exit;
19900 end if;
19902 Next_Component (Comp);
19903 end loop;
19904 end if;
19906 return OK;
19907 end Process;
19909 procedure Replace is new Traverse_Proc (Process);
19911 -- Start of processing for Replace_Components
19913 begin
19914 Replace (Decl);
19915 end Replace_Components;
19917 -------------------------------
19918 -- Set_Completion_Referenced --
19919 -------------------------------
19921 procedure Set_Completion_Referenced (E : Entity_Id) is
19922 begin
19923 -- If in main unit, mark entity that is a completion as referenced,
19924 -- warnings go on the partial view when needed.
19926 if In_Extended_Main_Source_Unit (E) then
19927 Set_Referenced (E);
19928 end if;
19929 end Set_Completion_Referenced;
19931 ---------------------
19932 -- Set_Fixed_Range --
19933 ---------------------
19935 -- The range for fixed-point types is complicated by the fact that we
19936 -- do not know the exact end points at the time of the declaration. This
19937 -- is true for three reasons:
19939 -- A size clause may affect the fudging of the end-points.
19940 -- A small clause may affect the values of the end-points.
19941 -- We try to include the end-points if it does not affect the size.
19943 -- This means that the actual end-points must be established at the
19944 -- point when the type is frozen. Meanwhile, we first narrow the range
19945 -- as permitted (so that it will fit if necessary in a small specified
19946 -- size), and then build a range subtree with these narrowed bounds.
19947 -- Set_Fixed_Range constructs the range from real literal values, and
19948 -- sets the range as the Scalar_Range of the given fixed-point type entity.
19950 -- The parent of this range is set to point to the entity so that it is
19951 -- properly hooked into the tree (unlike normal Scalar_Range entries for
19952 -- other scalar types, which are just pointers to the range in the
19953 -- original tree, this would otherwise be an orphan).
19955 -- The tree is left unanalyzed. When the type is frozen, the processing
19956 -- in Freeze.Freeze_Fixed_Point_Type notices that the range is not
19957 -- analyzed, and uses this as an indication that it should complete
19958 -- work on the range (it will know the final small and size values).
19960 procedure Set_Fixed_Range
19961 (E : Entity_Id;
19962 Loc : Source_Ptr;
19963 Lo : Ureal;
19964 Hi : Ureal)
19966 S : constant Node_Id :=
19967 Make_Range (Loc,
19968 Low_Bound => Make_Real_Literal (Loc, Lo),
19969 High_Bound => Make_Real_Literal (Loc, Hi));
19970 begin
19971 Set_Scalar_Range (E, S);
19972 Set_Parent (S, E);
19974 -- Before the freeze point, the bounds of a fixed point are universal
19975 -- and carry the corresponding type.
19977 Set_Etype (Low_Bound (S), Universal_Real);
19978 Set_Etype (High_Bound (S), Universal_Real);
19979 end Set_Fixed_Range;
19981 ----------------------------------
19982 -- Set_Scalar_Range_For_Subtype --
19983 ----------------------------------
19985 procedure Set_Scalar_Range_For_Subtype
19986 (Def_Id : Entity_Id;
19987 R : Node_Id;
19988 Subt : Entity_Id)
19990 Kind : constant Entity_Kind := Ekind (Def_Id);
19992 begin
19993 -- Defend against previous error
19995 if Nkind (R) = N_Error then
19996 return;
19997 end if;
19999 Set_Scalar_Range (Def_Id, R);
20001 -- We need to link the range into the tree before resolving it so
20002 -- that types that are referenced, including importantly the subtype
20003 -- itself, are properly frozen (Freeze_Expression requires that the
20004 -- expression be properly linked into the tree). Of course if it is
20005 -- already linked in, then we do not disturb the current link.
20007 if No (Parent (R)) then
20008 Set_Parent (R, Def_Id);
20009 end if;
20011 -- Reset the kind of the subtype during analysis of the range, to
20012 -- catch possible premature use in the bounds themselves.
20014 Set_Ekind (Def_Id, E_Void);
20015 Process_Range_Expr_In_Decl (R, Subt);
20016 Set_Ekind (Def_Id, Kind);
20017 end Set_Scalar_Range_For_Subtype;
20019 --------------------------------------------------------
20020 -- Set_Stored_Constraint_From_Discriminant_Constraint --
20021 --------------------------------------------------------
20023 procedure Set_Stored_Constraint_From_Discriminant_Constraint
20024 (E : Entity_Id)
20026 begin
20027 -- Make sure set if encountered during Expand_To_Stored_Constraint
20029 Set_Stored_Constraint (E, No_Elist);
20031 -- Give it the right value
20033 if Is_Constrained (E) and then Has_Discriminants (E) then
20034 Set_Stored_Constraint (E,
20035 Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
20036 end if;
20037 end Set_Stored_Constraint_From_Discriminant_Constraint;
20039 -------------------------------------
20040 -- Signed_Integer_Type_Declaration --
20041 -------------------------------------
20043 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
20044 Implicit_Base : Entity_Id;
20045 Base_Typ : Entity_Id;
20046 Lo_Val : Uint;
20047 Hi_Val : Uint;
20048 Errs : Boolean := False;
20049 Lo : Node_Id;
20050 Hi : Node_Id;
20052 function Can_Derive_From (E : Entity_Id) return Boolean;
20053 -- Determine whether given bounds allow derivation from specified type
20055 procedure Check_Bound (Expr : Node_Id);
20056 -- Check bound to make sure it is integral and static. If not, post
20057 -- appropriate error message and set Errs flag
20059 ---------------------
20060 -- Can_Derive_From --
20061 ---------------------
20063 -- Note we check both bounds against both end values, to deal with
20064 -- strange types like ones with a range of 0 .. -12341234.
20066 function Can_Derive_From (E : Entity_Id) return Boolean is
20067 Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
20068 Hi : constant Uint := Expr_Value (Type_High_Bound (E));
20069 begin
20070 return Lo <= Lo_Val and then Lo_Val <= Hi
20071 and then
20072 Lo <= Hi_Val and then Hi_Val <= Hi;
20073 end Can_Derive_From;
20075 -----------------
20076 -- Check_Bound --
20077 -----------------
20079 procedure Check_Bound (Expr : Node_Id) is
20080 begin
20081 -- If a range constraint is used as an integer type definition, each
20082 -- bound of the range must be defined by a static expression of some
20083 -- integer type, but the two bounds need not have the same integer
20084 -- type (Negative bounds are allowed.) (RM 3.5.4)
20086 if not Is_Integer_Type (Etype (Expr)) then
20087 Error_Msg_N
20088 ("integer type definition bounds must be of integer type", Expr);
20089 Errs := True;
20091 elsif not Is_OK_Static_Expression (Expr) then
20092 Flag_Non_Static_Expr
20093 ("non-static expression used for integer type bound!", Expr);
20094 Errs := True;
20096 -- The bounds are folded into literals, and we set their type to be
20097 -- universal, to avoid typing difficulties: we cannot set the type
20098 -- of the literal to the new type, because this would be a forward
20099 -- reference for the back end, and if the original type is user-
20100 -- defined this can lead to spurious semantic errors (e.g. 2928-003).
20102 else
20103 if Is_Entity_Name (Expr) then
20104 Fold_Uint (Expr, Expr_Value (Expr), True);
20105 end if;
20107 Set_Etype (Expr, Universal_Integer);
20108 end if;
20109 end Check_Bound;
20111 -- Start of processing for Signed_Integer_Type_Declaration
20113 begin
20114 -- Create an anonymous base type
20116 Implicit_Base :=
20117 Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
20119 -- Analyze and check the bounds, they can be of any integer type
20121 Lo := Low_Bound (Def);
20122 Hi := High_Bound (Def);
20124 -- Arbitrarily use Integer as the type if either bound had an error
20126 if Hi = Error or else Lo = Error then
20127 Base_Typ := Any_Integer;
20128 Set_Error_Posted (T, True);
20130 -- Here both bounds are OK expressions
20132 else
20133 Analyze_And_Resolve (Lo, Any_Integer);
20134 Analyze_And_Resolve (Hi, Any_Integer);
20136 Check_Bound (Lo);
20137 Check_Bound (Hi);
20139 if Errs then
20140 Hi := Type_High_Bound (Standard_Long_Long_Integer);
20141 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
20142 end if;
20144 -- Find type to derive from
20146 Lo_Val := Expr_Value (Lo);
20147 Hi_Val := Expr_Value (Hi);
20149 if Can_Derive_From (Standard_Short_Short_Integer) then
20150 Base_Typ := Base_Type (Standard_Short_Short_Integer);
20152 elsif Can_Derive_From (Standard_Short_Integer) then
20153 Base_Typ := Base_Type (Standard_Short_Integer);
20155 elsif Can_Derive_From (Standard_Integer) then
20156 Base_Typ := Base_Type (Standard_Integer);
20158 elsif Can_Derive_From (Standard_Long_Integer) then
20159 Base_Typ := Base_Type (Standard_Long_Integer);
20161 elsif Can_Derive_From (Standard_Long_Long_Integer) then
20162 Base_Typ := Base_Type (Standard_Long_Long_Integer);
20164 else
20165 Base_Typ := Base_Type (Standard_Long_Long_Integer);
20166 Error_Msg_N ("integer type definition bounds out of range", Def);
20167 Hi := Type_High_Bound (Standard_Long_Long_Integer);
20168 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
20169 end if;
20170 end if;
20172 -- Complete both implicit base and declared first subtype entities
20174 Set_Etype (Implicit_Base, Base_Typ);
20175 Set_Size_Info (Implicit_Base, (Base_Typ));
20176 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
20177 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
20179 Set_Ekind (T, E_Signed_Integer_Subtype);
20180 Set_Etype (T, Implicit_Base);
20182 -- In formal verification mode, restrict the base type's range to the
20183 -- minimum allowed by RM 3.5.4, namely the smallest symmetric range
20184 -- around zero with a possible extra negative value that contains the
20185 -- subtype range. Keep Size, RM_Size and First_Rep_Item info, which
20186 -- should not be relied upon in formal verification.
20188 if SPARK_Strict_Mode then
20189 declare
20190 Sym_Hi_Val : Uint;
20191 Sym_Lo_Val : Uint;
20192 Dloc : constant Source_Ptr := Sloc (Def);
20193 Lbound : Node_Id;
20194 Ubound : Node_Id;
20195 Bounds : Node_Id;
20197 begin
20198 -- If the subtype range is empty, the smallest base type range
20199 -- is the symmetric range around zero containing Lo_Val and
20200 -- Hi_Val.
20202 if UI_Gt (Lo_Val, Hi_Val) then
20203 Sym_Hi_Val := UI_Max (UI_Abs (Lo_Val), UI_Abs (Hi_Val));
20204 Sym_Lo_Val := UI_Negate (Sym_Hi_Val);
20206 -- Otherwise, if the subtype range is not empty and Hi_Val has
20207 -- the largest absolute value, Hi_Val is non negative and the
20208 -- smallest base type range is the symmetric range around zero
20209 -- containing Hi_Val.
20211 elsif UI_Le (UI_Abs (Lo_Val), UI_Abs (Hi_Val)) then
20212 Sym_Hi_Val := Hi_Val;
20213 Sym_Lo_Val := UI_Negate (Hi_Val);
20215 -- Otherwise, the subtype range is not empty, Lo_Val has the
20216 -- strictly largest absolute value, Lo_Val is negative and the
20217 -- smallest base type range is the symmetric range around zero
20218 -- with an extra negative value Lo_Val.
20220 else
20221 Sym_Lo_Val := Lo_Val;
20222 Sym_Hi_Val := UI_Sub (UI_Negate (Lo_Val), Uint_1);
20223 end if;
20225 Lbound := Make_Integer_Literal (Dloc, Sym_Lo_Val);
20226 Ubound := Make_Integer_Literal (Dloc, Sym_Hi_Val);
20227 Set_Is_Static_Expression (Lbound);
20228 Set_Is_Static_Expression (Ubound);
20229 Analyze_And_Resolve (Lbound, Any_Integer);
20230 Analyze_And_Resolve (Ubound, Any_Integer);
20232 Bounds := Make_Range (Dloc, Lbound, Ubound);
20233 Set_Etype (Bounds, Base_Typ);
20235 Set_Scalar_Range (Implicit_Base, Bounds);
20236 end;
20238 else
20239 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
20240 end if;
20242 Set_Size_Info (T, (Implicit_Base));
20243 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
20244 Set_Scalar_Range (T, Def);
20245 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
20246 Set_Is_Constrained (T);
20247 end Signed_Integer_Type_Declaration;
20249 end Sem_Ch3;