PR c++/54038
[official-gcc.git] / gcc / ada / sem_ch3.adb
blob71c075571db090c74970d5645c4210862c842da3
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-2012, 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 Atree; use Atree;
27 with Checks; use Checks;
28 with Debug; use Debug;
29 with Elists; use Elists;
30 with Einfo; use Einfo;
31 with Errout; use Errout;
32 with Eval_Fat; use Eval_Fat;
33 with Exp_Ch3; use Exp_Ch3;
34 with Exp_Ch9; use Exp_Ch9;
35 with Exp_Disp; use Exp_Disp;
36 with Exp_Dist; use Exp_Dist;
37 with Exp_Tss; use Exp_Tss;
38 with Exp_Util; use Exp_Util;
39 with Fname; use Fname;
40 with Freeze; use Freeze;
41 with Itypes; use Itypes;
42 with Layout; use Layout;
43 with Lib; use Lib;
44 with Lib.Xref; use Lib.Xref;
45 with Namet; use Namet;
46 with Nmake; use Nmake;
47 with Opt; use Opt;
48 with Restrict; use Restrict;
49 with Rident; use Rident;
50 with Rtsfind; use Rtsfind;
51 with Sem; use Sem;
52 with Sem_Aux; use Sem_Aux;
53 with Sem_Case; use Sem_Case;
54 with Sem_Cat; use Sem_Cat;
55 with Sem_Ch6; use Sem_Ch6;
56 with Sem_Ch7; use Sem_Ch7;
57 with Sem_Ch8; use Sem_Ch8;
58 with Sem_Ch13; use Sem_Ch13;
59 with Sem_Dim; use Sem_Dim;
60 with Sem_Disp; use Sem_Disp;
61 with Sem_Dist; use Sem_Dist;
62 with Sem_Elim; use Sem_Elim;
63 with Sem_Eval; use Sem_Eval;
64 with Sem_Mech; use Sem_Mech;
65 with Sem_Prag; use Sem_Prag;
66 with Sem_Res; use Sem_Res;
67 with Sem_Smem; use Sem_Smem;
68 with Sem_Type; use Sem_Type;
69 with Sem_Util; use Sem_Util;
70 with Sem_Warn; use Sem_Warn;
71 with Stand; use Stand;
72 with Sinfo; use Sinfo;
73 with Sinput; use Sinput;
74 with Snames; use Snames;
75 with Targparm; use Targparm;
76 with Tbuild; use Tbuild;
77 with Ttypes; use Ttypes;
78 with Uintp; use Uintp;
79 with Urealp; use Urealp;
81 package body Sem_Ch3 is
83 -----------------------
84 -- Local Subprograms --
85 -----------------------
87 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id);
88 -- Ada 2005 (AI-251): Add the tag components corresponding to all the
89 -- abstract interface types implemented by a record type or a derived
90 -- record type.
92 procedure Build_Derived_Type
93 (N : Node_Id;
94 Parent_Type : Entity_Id;
95 Derived_Type : Entity_Id;
96 Is_Completion : Boolean;
97 Derive_Subps : Boolean := True);
98 -- Create and decorate a Derived_Type given the Parent_Type entity. N is
99 -- the N_Full_Type_Declaration node containing the derived type definition.
100 -- Parent_Type is the entity for the parent type in the derived type
101 -- definition and Derived_Type the actual derived type. Is_Completion must
102 -- be set to False if Derived_Type is the N_Defining_Identifier node in N
103 -- (i.e. Derived_Type = Defining_Identifier (N)). In this case N is not the
104 -- completion of a private type declaration. If Is_Completion is set to
105 -- True, N is the completion of a private type declaration and Derived_Type
106 -- is different from the defining identifier inside N (i.e. Derived_Type /=
107 -- Defining_Identifier (N)). Derive_Subps indicates whether the parent
108 -- subprograms should be derived. The only case where this parameter is
109 -- False is when Build_Derived_Type is recursively called to process an
110 -- implicit derived full type for a type derived from a private type (in
111 -- that case the subprograms must only be derived for the private view of
112 -- the type).
114 -- ??? These flags need a bit of re-examination and re-documentation:
115 -- ??? are they both necessary (both seem related to the recursion)?
117 procedure Build_Derived_Access_Type
118 (N : Node_Id;
119 Parent_Type : Entity_Id;
120 Derived_Type : Entity_Id);
121 -- Subsidiary procedure to Build_Derived_Type. For a derived access type,
122 -- create an implicit base if the parent type is constrained or if the
123 -- subtype indication has a constraint.
125 procedure Build_Derived_Array_Type
126 (N : Node_Id;
127 Parent_Type : Entity_Id;
128 Derived_Type : Entity_Id);
129 -- Subsidiary procedure to Build_Derived_Type. For a derived array type,
130 -- create an implicit base if the parent type is constrained or if the
131 -- subtype indication has a constraint.
133 procedure Build_Derived_Concurrent_Type
134 (N : Node_Id;
135 Parent_Type : Entity_Id;
136 Derived_Type : Entity_Id);
137 -- Subsidiary procedure to Build_Derived_Type. For a derived task or
138 -- protected type, inherit entries and protected subprograms, check
139 -- legality of discriminant constraints if any.
141 procedure Build_Derived_Enumeration_Type
142 (N : Node_Id;
143 Parent_Type : Entity_Id;
144 Derived_Type : Entity_Id);
145 -- Subsidiary procedure to Build_Derived_Type. For a derived enumeration
146 -- type, we must create a new list of literals. Types derived from
147 -- Character and [Wide_]Wide_Character are special-cased.
149 procedure Build_Derived_Numeric_Type
150 (N : Node_Id;
151 Parent_Type : Entity_Id;
152 Derived_Type : Entity_Id);
153 -- Subsidiary procedure to Build_Derived_Type. For numeric types, create
154 -- an anonymous base type, and propagate constraint to subtype if needed.
156 procedure Build_Derived_Private_Type
157 (N : Node_Id;
158 Parent_Type : Entity_Id;
159 Derived_Type : Entity_Id;
160 Is_Completion : Boolean;
161 Derive_Subps : Boolean := True);
162 -- Subsidiary procedure to Build_Derived_Type. This procedure is complex
163 -- because the parent may or may not have a completion, and the derivation
164 -- may itself be a completion.
166 procedure Build_Derived_Record_Type
167 (N : Node_Id;
168 Parent_Type : Entity_Id;
169 Derived_Type : Entity_Id;
170 Derive_Subps : Boolean := True);
171 -- Subsidiary procedure for Build_Derived_Type and
172 -- Analyze_Private_Extension_Declaration used for tagged and untagged
173 -- record types. All parameters are as in Build_Derived_Type except that
174 -- N, in addition to being an N_Full_Type_Declaration node, can also be an
175 -- N_Private_Extension_Declaration node. See the definition of this routine
176 -- for much more info. Derive_Subps indicates whether subprograms should
177 -- be derived from the parent type. The only case where Derive_Subps is
178 -- False is for an implicit derived full type for a type derived from a
179 -- private type (see Build_Derived_Type).
181 procedure Build_Discriminal (Discrim : Entity_Id);
182 -- Create the discriminal corresponding to discriminant Discrim, that is
183 -- the parameter corresponding to Discrim to be used in initialization
184 -- procedures for the type where Discrim is a discriminant. Discriminals
185 -- are not used during semantic analysis, and are not fully defined
186 -- entities until expansion. Thus they are not given a scope until
187 -- initialization procedures are built.
189 function Build_Discriminant_Constraints
190 (T : Entity_Id;
191 Def : Node_Id;
192 Derived_Def : Boolean := False) return Elist_Id;
193 -- Validate discriminant constraints and return the list of the constraints
194 -- in order of discriminant declarations, where T is the discriminated
195 -- unconstrained type. Def is the N_Subtype_Indication node where the
196 -- discriminants constraints for T are specified. Derived_Def is True
197 -- when building the discriminant constraints in a derived type definition
198 -- of the form "type D (...) is new T (xxx)". In this case T is the parent
199 -- type and Def is the constraint "(xxx)" on T and this routine sets the
200 -- Corresponding_Discriminant field of the discriminants in the derived
201 -- type D to point to the corresponding discriminants in the parent type T.
203 procedure Build_Discriminated_Subtype
204 (T : Entity_Id;
205 Def_Id : Entity_Id;
206 Elist : Elist_Id;
207 Related_Nod : Node_Id;
208 For_Access : Boolean := False);
209 -- Subsidiary procedure to Constrain_Discriminated_Type and to
210 -- Process_Incomplete_Dependents. Given
212 -- T (a possibly discriminated base type)
213 -- Def_Id (a very partially built subtype for T),
215 -- the call completes Def_Id to be the appropriate E_*_Subtype.
217 -- The Elist is the list of discriminant constraints if any (it is set
218 -- to No_Elist if T is not a discriminated type, and to an empty list if
219 -- T has discriminants but there are no discriminant constraints). The
220 -- Related_Nod is the same as Decl_Node in Create_Constrained_Components.
221 -- The For_Access says whether or not this subtype is really constraining
222 -- an access type. That is its sole purpose is the designated type of an
223 -- access type -- in which case a Private_Subtype Is_For_Access_Subtype
224 -- is built to avoid freezing T when the access subtype is frozen.
226 function Build_Scalar_Bound
227 (Bound : Node_Id;
228 Par_T : Entity_Id;
229 Der_T : Entity_Id) return Node_Id;
230 -- The bounds of a derived scalar type are conversions of the bounds of
231 -- the parent type. Optimize the representation if the bounds are literals.
232 -- Needs a more complete spec--what are the parameters exactly, and what
233 -- exactly is the returned value, and how is Bound affected???
235 procedure Build_Underlying_Full_View
236 (N : Node_Id;
237 Typ : Entity_Id;
238 Par : Entity_Id);
239 -- If the completion of a private type is itself derived from a private
240 -- type, or if the full view of a private subtype is itself private, the
241 -- back-end has no way to compute the actual size of this type. We build
242 -- an internal subtype declaration of the proper parent type to convey
243 -- this information. This extra mechanism is needed because a full
244 -- view cannot itself have a full view (it would get clobbered during
245 -- view exchanges).
247 procedure Check_Access_Discriminant_Requires_Limited
248 (D : Node_Id;
249 Loc : Node_Id);
250 -- Check the restriction that the type to which an access discriminant
251 -- belongs must be a concurrent type or a descendant of a type with
252 -- the reserved word 'limited' in its declaration.
254 procedure Check_Anonymous_Access_Components
255 (Typ_Decl : Node_Id;
256 Typ : Entity_Id;
257 Prev : Entity_Id;
258 Comp_List : Node_Id);
259 -- Ada 2005 AI-382: an access component in a record definition can refer to
260 -- the enclosing record, in which case it denotes the type itself, and not
261 -- the current instance of the type. We create an anonymous access type for
262 -- the component, and flag it as an access to a component, so accessibility
263 -- checks are properly performed on it. The declaration of the access type
264 -- is placed ahead of that of the record to prevent order-of-elaboration
265 -- circularity issues in Gigi. We create an incomplete type for the record
266 -- declaration, which is the designated type of the anonymous access.
268 procedure Check_Delta_Expression (E : Node_Id);
269 -- Check that the expression represented by E is suitable for use as a
270 -- delta expression, i.e. it is of real type and is static.
272 procedure Check_Digits_Expression (E : Node_Id);
273 -- Check that the expression represented by E is suitable for use as a
274 -- digits expression, i.e. it is of integer type, positive and static.
276 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id);
277 -- Validate the initialization of an object declaration. T is the required
278 -- type, and Exp is the initialization expression.
280 procedure Check_Interfaces (N : Node_Id; Def : Node_Id);
281 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
283 procedure Check_Or_Process_Discriminants
284 (N : Node_Id;
285 T : Entity_Id;
286 Prev : Entity_Id := Empty);
287 -- If N is the full declaration of the completion T of an incomplete or
288 -- private type, check its discriminants (which are already known to be
289 -- conformant with those of the partial view, see Find_Type_Name),
290 -- otherwise process them. Prev is the entity of the partial declaration,
291 -- if any.
293 procedure Check_Real_Bound (Bound : Node_Id);
294 -- Check given bound for being of real type and static. If not, post an
295 -- appropriate message, and rewrite the bound with the real literal zero.
297 procedure Constant_Redeclaration
298 (Id : Entity_Id;
299 N : Node_Id;
300 T : out Entity_Id);
301 -- Various checks on legality of full declaration of deferred constant.
302 -- Id is the entity for the redeclaration, N is the N_Object_Declaration,
303 -- node. The caller has not yet set any attributes of this entity.
305 function Contain_Interface
306 (Iface : Entity_Id;
307 Ifaces : Elist_Id) return Boolean;
308 -- Ada 2005: Determine whether Iface is present in the list Ifaces
310 procedure Convert_Scalar_Bounds
311 (N : Node_Id;
312 Parent_Type : Entity_Id;
313 Derived_Type : Entity_Id;
314 Loc : Source_Ptr);
315 -- For derived scalar types, convert the bounds in the type definition to
316 -- the derived type, and complete their analysis. Given a constraint of the
317 -- form ".. new T range Lo .. Hi", Lo and Hi are analyzed and resolved with
318 -- T'Base, the parent_type. The bounds of the derived type (the anonymous
319 -- base) are copies of Lo and Hi. Finally, the bounds of the derived
320 -- subtype are conversions of those bounds to the derived_type, so that
321 -- their typing is consistent.
323 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id);
324 -- Copies attributes from array base type T2 to array base type T1. Copies
325 -- only attributes that apply to base types, but not subtypes.
327 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id);
328 -- Copies attributes from array subtype T2 to array subtype T1. Copies
329 -- attributes that apply to both subtypes and base types.
331 procedure Create_Constrained_Components
332 (Subt : Entity_Id;
333 Decl_Node : Node_Id;
334 Typ : Entity_Id;
335 Constraints : Elist_Id);
336 -- Build the list of entities for a constrained discriminated record
337 -- subtype. If a component depends on a discriminant, replace its subtype
338 -- using the discriminant values in the discriminant constraint. Subt
339 -- is the defining identifier for the subtype whose list of constrained
340 -- entities we will create. Decl_Node is the type declaration node where
341 -- we will attach all the itypes created. Typ is the base discriminated
342 -- type for the subtype Subt. Constraints is the list of discriminant
343 -- constraints for Typ.
345 function Constrain_Component_Type
346 (Comp : Entity_Id;
347 Constrained_Typ : Entity_Id;
348 Related_Node : Node_Id;
349 Typ : Entity_Id;
350 Constraints : Elist_Id) return Entity_Id;
351 -- Given a discriminated base type Typ, a list of discriminant constraint
352 -- Constraints for Typ and a component of Typ, with type Compon_Type,
353 -- create and return the type corresponding to Compon_type where all
354 -- discriminant references are replaced with the corresponding constraint.
355 -- If no discriminant references occur in Compon_Typ then return it as is.
356 -- Constrained_Typ is the final constrained subtype to which the
357 -- constrained Compon_Type belongs. Related_Node is the node where we will
358 -- attach all the itypes created.
360 -- Above description is confused, what is Compon_Type???
362 procedure Constrain_Access
363 (Def_Id : in out Entity_Id;
364 S : Node_Id;
365 Related_Nod : Node_Id);
366 -- Apply a list of constraints to an access type. If Def_Id is empty, it is
367 -- an anonymous type created for a subtype indication. In that case it is
368 -- created in the procedure and attached to Related_Nod.
370 procedure Constrain_Array
371 (Def_Id : in out Entity_Id;
372 SI : Node_Id;
373 Related_Nod : Node_Id;
374 Related_Id : Entity_Id;
375 Suffix : Character);
376 -- Apply a list of index constraints to an unconstrained array type. The
377 -- first parameter is the entity for the resulting subtype. A value of
378 -- Empty for Def_Id indicates that an implicit type must be created, but
379 -- creation is delayed (and must be done by this procedure) because other
380 -- subsidiary implicit types must be created first (which is why Def_Id
381 -- is an in/out parameter). The second parameter is a subtype indication
382 -- node for the constrained array to be created (e.g. something of the
383 -- form string (1 .. 10)). Related_Nod gives the place where this type
384 -- has to be inserted in the tree. The Related_Id and Suffix parameters
385 -- are used to build the associated Implicit type name.
387 procedure Constrain_Concurrent
388 (Def_Id : in out Entity_Id;
389 SI : Node_Id;
390 Related_Nod : Node_Id;
391 Related_Id : Entity_Id;
392 Suffix : Character);
393 -- Apply list of discriminant constraints to an unconstrained concurrent
394 -- type.
396 -- SI is the N_Subtype_Indication node containing the constraint and
397 -- the unconstrained type to constrain.
399 -- Def_Id is the entity for the resulting constrained subtype. A value
400 -- of Empty for Def_Id indicates that an implicit type must be created,
401 -- but creation is delayed (and must be done by this procedure) because
402 -- other subsidiary implicit types must be created first (which is why
403 -- Def_Id is an in/out parameter).
405 -- Related_Nod gives the place where this type has to be inserted
406 -- in the tree
408 -- The last two arguments are used to create its external name if needed.
410 function Constrain_Corresponding_Record
411 (Prot_Subt : Entity_Id;
412 Corr_Rec : Entity_Id;
413 Related_Nod : Node_Id;
414 Related_Id : Entity_Id) return Entity_Id;
415 -- When constraining a protected type or task type with discriminants,
416 -- constrain the corresponding record with the same discriminant values.
418 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id);
419 -- Constrain a decimal fixed point type with a digits constraint and/or a
420 -- range constraint, and build E_Decimal_Fixed_Point_Subtype entity.
422 procedure Constrain_Discriminated_Type
423 (Def_Id : Entity_Id;
424 S : Node_Id;
425 Related_Nod : Node_Id;
426 For_Access : Boolean := False);
427 -- Process discriminant constraints of composite type. Verify that values
428 -- have been provided for all discriminants, that the original type is
429 -- unconstrained, and that the types of the supplied expressions match
430 -- the discriminant types. The first three parameters are like in routine
431 -- Constrain_Concurrent. See Build_Discriminated_Subtype for an explanation
432 -- of For_Access.
434 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id);
435 -- Constrain an enumeration type with a range constraint. This is identical
436 -- to Constrain_Integer, but for the Ekind of the resulting subtype.
438 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id);
439 -- Constrain a floating point type with either a digits constraint
440 -- and/or a range constraint, building a E_Floating_Point_Subtype.
442 procedure Constrain_Index
443 (Index : Node_Id;
444 S : Node_Id;
445 Related_Nod : Node_Id;
446 Related_Id : Entity_Id;
447 Suffix : Character;
448 Suffix_Index : Nat);
449 -- Process an index constraint S in a constrained array declaration. The
450 -- constraint can be a subtype name, or a range with or without an explicit
451 -- subtype mark. The index is the corresponding index of the unconstrained
452 -- array. The Related_Id and Suffix parameters are used to build the
453 -- associated Implicit type name.
455 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id);
456 -- Build subtype of a signed or modular integer type
458 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id);
459 -- Constrain an ordinary fixed point type with a range constraint, and
460 -- build an E_Ordinary_Fixed_Point_Subtype entity.
462 procedure Copy_And_Swap (Priv, Full : Entity_Id);
463 -- Copy the Priv entity into the entity of its full declaration then swap
464 -- the two entities in such a manner that the former private type is now
465 -- seen as a full type.
467 procedure Decimal_Fixed_Point_Type_Declaration
468 (T : Entity_Id;
469 Def : Node_Id);
470 -- Create a new decimal fixed point type, and apply the constraint to
471 -- obtain a subtype of this new type.
473 procedure Complete_Private_Subtype
474 (Priv : Entity_Id;
475 Full : Entity_Id;
476 Full_Base : Entity_Id;
477 Related_Nod : Node_Id);
478 -- Complete the implicit full view of a private subtype by setting the
479 -- appropriate semantic fields. If the full view of the parent is a record
480 -- type, build constrained components of subtype.
482 procedure Derive_Progenitor_Subprograms
483 (Parent_Type : Entity_Id;
484 Tagged_Type : Entity_Id);
485 -- Ada 2005 (AI-251): To complete type derivation, collect the primitive
486 -- operations of progenitors of Tagged_Type, and replace the subsidiary
487 -- subtypes with Tagged_Type, to build the specs of the inherited interface
488 -- primitives. The derived primitives are aliased to those of the
489 -- interface. This routine takes care also of transferring to the full view
490 -- subprograms associated with the partial view of Tagged_Type that cover
491 -- interface primitives.
493 procedure Derived_Standard_Character
494 (N : Node_Id;
495 Parent_Type : Entity_Id;
496 Derived_Type : Entity_Id);
497 -- Subsidiary procedure to Build_Derived_Enumeration_Type which handles
498 -- derivations from types Standard.Character and Standard.Wide_Character.
500 procedure Derived_Type_Declaration
501 (T : Entity_Id;
502 N : Node_Id;
503 Is_Completion : Boolean);
504 -- Process a derived type declaration. Build_Derived_Type is invoked
505 -- to process the actual derived type definition. Parameters N and
506 -- Is_Completion have the same meaning as in Build_Derived_Type.
507 -- T is the N_Defining_Identifier for the entity defined in the
508 -- N_Full_Type_Declaration node N, that is T is the derived type.
510 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id);
511 -- Insert each literal in symbol table, as an overloadable identifier. Each
512 -- enumeration type is mapped into a sequence of integers, and each literal
513 -- is defined as a constant with integer value. If any of the literals are
514 -- character literals, the type is a character type, which means that
515 -- strings are legal aggregates for arrays of components of the type.
517 function Expand_To_Stored_Constraint
518 (Typ : Entity_Id;
519 Constraint : Elist_Id) return Elist_Id;
520 -- Given a constraint (i.e. a list of expressions) on the discriminants of
521 -- Typ, expand it into a constraint on the stored discriminants and return
522 -- the new list of expressions constraining the stored discriminants.
524 function Find_Type_Of_Object
525 (Obj_Def : Node_Id;
526 Related_Nod : Node_Id) return Entity_Id;
527 -- Get type entity for object referenced by Obj_Def, attaching the
528 -- implicit types generated to Related_Nod
530 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id);
531 -- Create a new float and apply the constraint to obtain subtype of it
533 function Has_Range_Constraint (N : Node_Id) return Boolean;
534 -- Given an N_Subtype_Indication node N, return True if a range constraint
535 -- is present, either directly, or as part of a digits or delta constraint.
536 -- In addition, a digits constraint in the decimal case returns True, since
537 -- it establishes a default range if no explicit range is present.
539 function Inherit_Components
540 (N : Node_Id;
541 Parent_Base : Entity_Id;
542 Derived_Base : Entity_Id;
543 Is_Tagged : Boolean;
544 Inherit_Discr : Boolean;
545 Discs : Elist_Id) return Elist_Id;
546 -- Called from Build_Derived_Record_Type to inherit the components of
547 -- Parent_Base (a base type) into the Derived_Base (the derived base type).
548 -- For more information on derived types and component inheritance please
549 -- consult the comment above the body of Build_Derived_Record_Type.
551 -- N is the original derived type declaration
553 -- Is_Tagged is set if we are dealing with tagged types
555 -- If Inherit_Discr is set, Derived_Base inherits its discriminants from
556 -- Parent_Base, otherwise no discriminants are inherited.
558 -- Discs gives the list of constraints that apply to Parent_Base in the
559 -- derived type declaration. If Discs is set to No_Elist, then we have
560 -- the following situation:
562 -- type Parent (D1..Dn : ..) is [tagged] record ...;
563 -- type Derived is new Parent [with ...];
565 -- which gets treated as
567 -- type Derived (D1..Dn : ..) is new Parent (D1,..,Dn) [with ...];
569 -- For untagged types the returned value is an association list. The list
570 -- starts from the association (Parent_Base => Derived_Base), and then it
571 -- contains a sequence of the associations of the form
573 -- (Old_Component => New_Component),
575 -- where Old_Component is the Entity_Id of a component in Parent_Base and
576 -- New_Component is the Entity_Id of the corresponding component in
577 -- Derived_Base. For untagged records, this association list is needed when
578 -- copying the record declaration for the derived base. In the tagged case
579 -- the value returned is irrelevant.
581 function Is_Valid_Constraint_Kind
582 (T_Kind : Type_Kind;
583 Constraint_Kind : Node_Kind) return Boolean;
584 -- Returns True if it is legal to apply the given kind of constraint to the
585 -- given kind of type (index constraint to an array type, for example).
587 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id);
588 -- Create new modular type. Verify that modulus is in bounds
590 procedure New_Concatenation_Op (Typ : Entity_Id);
591 -- Create an abbreviated declaration for an operator in order to
592 -- materialize concatenation on array types.
594 procedure Ordinary_Fixed_Point_Type_Declaration
595 (T : Entity_Id;
596 Def : Node_Id);
597 -- Create a new ordinary fixed point type, and apply the constraint to
598 -- obtain subtype of it.
600 procedure Prepare_Private_Subtype_Completion
601 (Id : Entity_Id;
602 Related_Nod : Node_Id);
603 -- Id is a subtype of some private type. Creates the full declaration
604 -- associated with Id whenever possible, i.e. when the full declaration
605 -- of the base type is already known. Records each subtype into
606 -- Private_Dependents of the base type.
608 procedure Process_Incomplete_Dependents
609 (N : Node_Id;
610 Full_T : Entity_Id;
611 Inc_T : Entity_Id);
612 -- Process all entities that depend on an incomplete type. There include
613 -- subtypes, subprogram types that mention the incomplete type in their
614 -- profiles, and subprogram with access parameters that designate the
615 -- incomplete type.
617 -- Inc_T is the defining identifier of an incomplete type declaration, its
618 -- Ekind is E_Incomplete_Type.
620 -- N is the corresponding N_Full_Type_Declaration for Inc_T.
622 -- Full_T is N's defining identifier.
624 -- Subtypes of incomplete types with discriminants are completed when the
625 -- parent type is. This is simpler than private subtypes, because they can
626 -- only appear in the same scope, and there is no need to exchange views.
627 -- Similarly, access_to_subprogram types may have a parameter or a return
628 -- type that is an incomplete type, and that must be replaced with the
629 -- full type.
631 -- If the full type is tagged, subprogram with access parameters that
632 -- designated the incomplete may be primitive operations of the full type,
633 -- and have to be processed accordingly.
635 procedure Process_Real_Range_Specification (Def : Node_Id);
636 -- Given the type definition for a real type, this procedure processes and
637 -- checks the real range specification of this type definition if one is
638 -- present. If errors are found, error messages are posted, and the
639 -- Real_Range_Specification of Def is reset to Empty.
641 procedure Record_Type_Declaration
642 (T : Entity_Id;
643 N : Node_Id;
644 Prev : Entity_Id);
645 -- Process a record type declaration (for both untagged and tagged
646 -- records). Parameters T and N are exactly like in procedure
647 -- Derived_Type_Declaration, except that no flag Is_Completion is needed
648 -- for this routine. If this is the completion of an incomplete type
649 -- declaration, Prev is the entity of the incomplete declaration, used for
650 -- cross-referencing. Otherwise Prev = T.
652 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id);
653 -- This routine is used to process the actual record type definition (both
654 -- for untagged and tagged records). Def is a record type definition node.
655 -- This procedure analyzes the components in this record type definition.
656 -- Prev_T is the entity for the enclosing record type. It is provided so
657 -- that its Has_Task flag can be set if any of the component have Has_Task
658 -- set. If the declaration is the completion of an incomplete type
659 -- declaration, Prev_T is the original incomplete type, whose full view is
660 -- the record type.
662 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id);
663 -- Subsidiary to Build_Derived_Record_Type. For untagged records, we
664 -- build a copy of the declaration tree of the parent, and we create
665 -- independently the list of components for the derived type. Semantic
666 -- information uses the component entities, but record representation
667 -- clauses are validated on the declaration tree. This procedure replaces
668 -- discriminants and components in the declaration with those that have
669 -- been created by Inherit_Components.
671 procedure Set_Fixed_Range
672 (E : Entity_Id;
673 Loc : Source_Ptr;
674 Lo : Ureal;
675 Hi : Ureal);
676 -- Build a range node with the given bounds and set it as the Scalar_Range
677 -- of the given fixed-point type entity. Loc is the source location used
678 -- for the constructed range. See body for further details.
680 procedure Set_Scalar_Range_For_Subtype
681 (Def_Id : Entity_Id;
682 R : Node_Id;
683 Subt : Entity_Id);
684 -- This routine is used to set the scalar range field for a subtype given
685 -- Def_Id, the entity for the subtype, and R, the range expression for the
686 -- scalar range. Subt provides the parent subtype to be used to analyze,
687 -- resolve, and check the given range.
689 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id);
690 -- Create a new signed integer entity, and apply the constraint to obtain
691 -- the required first named subtype of this type.
693 procedure Set_Stored_Constraint_From_Discriminant_Constraint
694 (E : Entity_Id);
695 -- E is some record type. This routine computes E's Stored_Constraint
696 -- from its Discriminant_Constraint.
698 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id);
699 -- Check that an entity in a list of progenitors is an interface,
700 -- emit error otherwise.
702 -----------------------
703 -- Access_Definition --
704 -----------------------
706 function Access_Definition
707 (Related_Nod : Node_Id;
708 N : Node_Id) return Entity_Id
710 Anon_Type : Entity_Id;
711 Anon_Scope : Entity_Id;
712 Desig_Type : Entity_Id;
713 Enclosing_Prot_Type : Entity_Id := Empty;
715 begin
716 Check_SPARK_Restriction ("access type is not allowed", N);
718 if Is_Entry (Current_Scope)
719 and then Is_Task_Type (Etype (Scope (Current_Scope)))
720 then
721 Error_Msg_N ("task entries cannot have access parameters", N);
722 return Empty;
723 end if;
725 -- Ada 2005: for an object declaration the corresponding anonymous
726 -- type is declared in the current scope.
728 -- If the access definition is the return type of another access to
729 -- function, scope is the current one, because it is the one of the
730 -- current type declaration, except for the pathological case below.
732 if Nkind_In (Related_Nod, N_Object_Declaration,
733 N_Access_Function_Definition)
734 then
735 Anon_Scope := Current_Scope;
737 -- A pathological case: function returning access functions that
738 -- return access functions, etc. Each anonymous access type created
739 -- is in the enclosing scope of the outermost function.
741 declare
742 Par : Node_Id;
744 begin
745 Par := Related_Nod;
746 while Nkind_In (Par, N_Access_Function_Definition,
747 N_Access_Definition)
748 loop
749 Par := Parent (Par);
750 end loop;
752 if Nkind (Par) = N_Function_Specification then
753 Anon_Scope := Scope (Defining_Entity (Par));
754 end if;
755 end;
757 -- For the anonymous function result case, retrieve the scope of the
758 -- function specification's associated entity rather than using the
759 -- current scope. The current scope will be the function itself if the
760 -- formal part is currently being analyzed, but will be the parent scope
761 -- in the case of a parameterless function, and we always want to use
762 -- the function's parent scope. Finally, if the function is a child
763 -- unit, we must traverse the tree to retrieve the proper entity.
765 elsif Nkind (Related_Nod) = N_Function_Specification
766 and then Nkind (Parent (N)) /= N_Parameter_Specification
767 then
768 -- If the current scope is a protected type, the anonymous access
769 -- is associated with one of the protected operations, and must
770 -- be available in the scope that encloses the protected declaration.
771 -- Otherwise the type is in the scope enclosing the subprogram.
773 -- If the function has formals, The return type of a subprogram
774 -- declaration is analyzed in the scope of the subprogram (see
775 -- Process_Formals) and thus the protected type, if present, is
776 -- the scope of the current function scope.
778 if Ekind (Current_Scope) = E_Protected_Type then
779 Enclosing_Prot_Type := Current_Scope;
781 elsif Ekind (Current_Scope) = E_Function
782 and then Ekind (Scope (Current_Scope)) = E_Protected_Type
783 then
784 Enclosing_Prot_Type := Scope (Current_Scope);
785 end if;
787 if Present (Enclosing_Prot_Type) then
788 Anon_Scope := Scope (Enclosing_Prot_Type);
790 else
791 Anon_Scope := Scope (Defining_Entity (Related_Nod));
792 end if;
794 -- For an access type definition, if the current scope is a child
795 -- unit it is the scope of the type.
797 elsif Is_Compilation_Unit (Current_Scope) then
798 Anon_Scope := Current_Scope;
800 -- For access formals, access components, and access discriminants, the
801 -- scope is that of the enclosing declaration,
803 else
804 Anon_Scope := Scope (Current_Scope);
805 end if;
807 Anon_Type :=
808 Create_Itype
809 (E_Anonymous_Access_Type, Related_Nod, Scope_Id => Anon_Scope);
811 if All_Present (N)
812 and then Ada_Version >= Ada_2005
813 then
814 Error_Msg_N ("ALL is not permitted for anonymous access types", N);
815 end if;
817 -- Ada 2005 (AI-254): In case of anonymous access to subprograms call
818 -- the corresponding semantic routine
820 if Present (Access_To_Subprogram_Definition (N)) then
822 -- Compiler runtime units are compiled in Ada 2005 mode when building
823 -- the runtime library but must also be compilable in Ada 95 mode
824 -- (when bootstrapping the compiler).
826 Check_Compiler_Unit (N);
828 Access_Subprogram_Declaration
829 (T_Name => Anon_Type,
830 T_Def => Access_To_Subprogram_Definition (N));
832 if Ekind (Anon_Type) = E_Access_Protected_Subprogram_Type then
833 Set_Ekind
834 (Anon_Type, E_Anonymous_Access_Protected_Subprogram_Type);
835 else
836 Set_Ekind
837 (Anon_Type, E_Anonymous_Access_Subprogram_Type);
838 end if;
840 Set_Can_Use_Internal_Rep
841 (Anon_Type, not Always_Compatible_Rep_On_Target);
843 -- If the anonymous access is associated with a protected operation,
844 -- create a reference to it after the enclosing protected definition
845 -- because the itype will be used in the subsequent bodies.
847 if Ekind (Current_Scope) = E_Protected_Type then
848 Build_Itype_Reference (Anon_Type, Parent (Current_Scope));
849 end if;
851 return Anon_Type;
852 end if;
854 Find_Type (Subtype_Mark (N));
855 Desig_Type := Entity (Subtype_Mark (N));
857 Set_Directly_Designated_Type (Anon_Type, Desig_Type);
858 Set_Etype (Anon_Type, Anon_Type);
860 -- Make sure the anonymous access type has size and alignment fields
861 -- set, as required by gigi. This is necessary in the case of the
862 -- Task_Body_Procedure.
864 if not Has_Private_Component (Desig_Type) then
865 Layout_Type (Anon_Type);
866 end if;
868 -- Ada 2005 (AI-231): Ada 2005 semantics for anonymous access differs
869 -- from Ada 95 semantics. In Ada 2005, anonymous access must specify if
870 -- the null value is allowed. In Ada 95 the null value is never allowed.
872 if Ada_Version >= Ada_2005 then
873 Set_Can_Never_Be_Null (Anon_Type, Null_Exclusion_Present (N));
874 else
875 Set_Can_Never_Be_Null (Anon_Type, True);
876 end if;
878 -- The anonymous access type is as public as the discriminated type or
879 -- subprogram that defines it. It is imported (for back-end purposes)
880 -- if the designated type is.
882 Set_Is_Public (Anon_Type, Is_Public (Scope (Anon_Type)));
884 -- Ada 2005 (AI-231): Propagate the access-constant attribute
886 Set_Is_Access_Constant (Anon_Type, Constant_Present (N));
888 -- The context is either a subprogram declaration, object declaration,
889 -- or an access discriminant, in a private or a full type declaration.
890 -- In the case of a subprogram, if the designated type is incomplete,
891 -- the operation will be a primitive operation of the full type, to be
892 -- updated subsequently. If the type is imported through a limited_with
893 -- clause, the subprogram is not a primitive operation of the type
894 -- (which is declared elsewhere in some other scope).
896 if Ekind (Desig_Type) = E_Incomplete_Type
897 and then not From_With_Type (Desig_Type)
898 and then Is_Overloadable (Current_Scope)
899 then
900 Append_Elmt (Current_Scope, Private_Dependents (Desig_Type));
901 Set_Has_Delayed_Freeze (Current_Scope);
902 end if;
904 -- Ada 2005: if the designated type is an interface that may contain
905 -- tasks, create a Master entity for the declaration. This must be done
906 -- before expansion of the full declaration, because the declaration may
907 -- include an expression that is an allocator, whose expansion needs the
908 -- proper Master for the created tasks.
910 if Nkind (Related_Nod) = N_Object_Declaration
911 and then Expander_Active
912 then
913 if Is_Interface (Desig_Type)
914 and then Is_Limited_Record (Desig_Type)
915 then
916 Build_Class_Wide_Master (Anon_Type);
918 -- Similarly, if the type is an anonymous access that designates
919 -- tasks, create a master entity for it in the current context.
921 elsif Has_Task (Desig_Type)
922 and then Comes_From_Source (Related_Nod)
923 then
924 Build_Master_Entity (Defining_Identifier (Related_Nod));
925 Build_Master_Renaming (Anon_Type);
926 end if;
927 end if;
929 -- For a private component of a protected type, it is imperative that
930 -- the back-end elaborate the type immediately after the protected
931 -- declaration, because this type will be used in the declarations
932 -- created for the component within each protected body, so we must
933 -- create an itype reference for it now.
935 if Nkind (Parent (Related_Nod)) = N_Protected_Definition then
936 Build_Itype_Reference (Anon_Type, Parent (Parent (Related_Nod)));
938 -- Similarly, if the access definition is the return result of a
939 -- function, create an itype reference for it because it will be used
940 -- within the function body. For a regular function that is not a
941 -- compilation unit, insert reference after the declaration. For a
942 -- protected operation, insert it after the enclosing protected type
943 -- declaration. In either case, do not create a reference for a type
944 -- obtained through a limited_with clause, because this would introduce
945 -- semantic dependencies.
947 -- Similarly, do not create a reference if the designated type is a
948 -- generic formal, because no use of it will reach the backend.
950 elsif Nkind (Related_Nod) = N_Function_Specification
951 and then not From_With_Type (Desig_Type)
952 and then not Is_Generic_Type (Desig_Type)
953 then
954 if Present (Enclosing_Prot_Type) then
955 Build_Itype_Reference (Anon_Type, Parent (Enclosing_Prot_Type));
957 elsif Is_List_Member (Parent (Related_Nod))
958 and then Nkind (Parent (N)) /= N_Parameter_Specification
959 then
960 Build_Itype_Reference (Anon_Type, Parent (Related_Nod));
961 end if;
963 -- Finally, create an itype reference for an object declaration of an
964 -- anonymous access type. This is strictly necessary only for deferred
965 -- constants, but in any case will avoid out-of-scope problems in the
966 -- back-end.
968 elsif Nkind (Related_Nod) = N_Object_Declaration then
969 Build_Itype_Reference (Anon_Type, Related_Nod);
970 end if;
972 return Anon_Type;
973 end Access_Definition;
975 -----------------------------------
976 -- Access_Subprogram_Declaration --
977 -----------------------------------
979 procedure Access_Subprogram_Declaration
980 (T_Name : Entity_Id;
981 T_Def : Node_Id)
984 procedure Check_For_Premature_Usage (Def : Node_Id);
985 -- Check that type T_Name is not used, directly or recursively, as a
986 -- parameter or a return type in Def. Def is either a subtype, an
987 -- access_definition, or an access_to_subprogram_definition.
989 -------------------------------
990 -- Check_For_Premature_Usage --
991 -------------------------------
993 procedure Check_For_Premature_Usage (Def : Node_Id) is
994 Param : Node_Id;
996 begin
997 -- Check for a subtype mark
999 if Nkind (Def) in N_Has_Etype then
1000 if Etype (Def) = T_Name then
1001 Error_Msg_N
1002 ("type& cannot be used before end of its declaration", Def);
1003 end if;
1005 -- If this is not a subtype, then this is an access_definition
1007 elsif Nkind (Def) = N_Access_Definition then
1008 if Present (Access_To_Subprogram_Definition (Def)) then
1009 Check_For_Premature_Usage
1010 (Access_To_Subprogram_Definition (Def));
1011 else
1012 Check_For_Premature_Usage (Subtype_Mark (Def));
1013 end if;
1015 -- The only cases left are N_Access_Function_Definition and
1016 -- N_Access_Procedure_Definition.
1018 else
1019 if Present (Parameter_Specifications (Def)) then
1020 Param := First (Parameter_Specifications (Def));
1021 while Present (Param) loop
1022 Check_For_Premature_Usage (Parameter_Type (Param));
1023 Param := Next (Param);
1024 end loop;
1025 end if;
1027 if Nkind (Def) = N_Access_Function_Definition then
1028 Check_For_Premature_Usage (Result_Definition (Def));
1029 end if;
1030 end if;
1031 end Check_For_Premature_Usage;
1033 -- Local variables
1035 Formals : constant List_Id := Parameter_Specifications (T_Def);
1036 Formal : Entity_Id;
1037 D_Ityp : Node_Id;
1038 Desig_Type : constant Entity_Id :=
1039 Create_Itype (E_Subprogram_Type, Parent (T_Def));
1041 -- Start of processing for Access_Subprogram_Declaration
1043 begin
1044 Check_SPARK_Restriction ("access type is not allowed", T_Def);
1046 -- Associate the Itype node with the inner full-type declaration or
1047 -- subprogram spec or entry body. This is required to handle nested
1048 -- anonymous declarations. For example:
1050 -- procedure P
1051 -- (X : access procedure
1052 -- (Y : access procedure
1053 -- (Z : access T)))
1055 D_Ityp := Associated_Node_For_Itype (Desig_Type);
1056 while not (Nkind_In (D_Ityp, N_Full_Type_Declaration,
1057 N_Private_Type_Declaration,
1058 N_Private_Extension_Declaration,
1059 N_Procedure_Specification,
1060 N_Function_Specification,
1061 N_Entry_Body)
1063 or else
1064 Nkind_In (D_Ityp, N_Object_Declaration,
1065 N_Object_Renaming_Declaration,
1066 N_Formal_Object_Declaration,
1067 N_Formal_Type_Declaration,
1068 N_Task_Type_Declaration,
1069 N_Protected_Type_Declaration))
1070 loop
1071 D_Ityp := Parent (D_Ityp);
1072 pragma Assert (D_Ityp /= Empty);
1073 end loop;
1075 Set_Associated_Node_For_Itype (Desig_Type, D_Ityp);
1077 if Nkind_In (D_Ityp, N_Procedure_Specification,
1078 N_Function_Specification)
1079 then
1080 Set_Scope (Desig_Type, Scope (Defining_Entity (D_Ityp)));
1082 elsif Nkind_In (D_Ityp, N_Full_Type_Declaration,
1083 N_Object_Declaration,
1084 N_Object_Renaming_Declaration,
1085 N_Formal_Type_Declaration)
1086 then
1087 Set_Scope (Desig_Type, Scope (Defining_Identifier (D_Ityp)));
1088 end if;
1090 if Nkind (T_Def) = N_Access_Function_Definition then
1091 if Nkind (Result_Definition (T_Def)) = N_Access_Definition then
1092 declare
1093 Acc : constant Node_Id := Result_Definition (T_Def);
1095 begin
1096 if Present (Access_To_Subprogram_Definition (Acc))
1097 and then
1098 Protected_Present (Access_To_Subprogram_Definition (Acc))
1099 then
1100 Set_Etype
1101 (Desig_Type,
1102 Replace_Anonymous_Access_To_Protected_Subprogram
1103 (T_Def));
1105 else
1106 Set_Etype
1107 (Desig_Type,
1108 Access_Definition (T_Def, Result_Definition (T_Def)));
1109 end if;
1110 end;
1112 else
1113 Analyze (Result_Definition (T_Def));
1115 declare
1116 Typ : constant Entity_Id := Entity (Result_Definition (T_Def));
1118 begin
1119 -- If a null exclusion is imposed on the result type, then
1120 -- create a null-excluding itype (an access subtype) and use
1121 -- it as the function's Etype.
1123 if Is_Access_Type (Typ)
1124 and then Null_Exclusion_In_Return_Present (T_Def)
1125 then
1126 Set_Etype (Desig_Type,
1127 Create_Null_Excluding_Itype
1128 (T => Typ,
1129 Related_Nod => T_Def,
1130 Scope_Id => Current_Scope));
1132 else
1133 if From_With_Type (Typ) then
1135 -- AI05-151: Incomplete types are allowed in all basic
1136 -- declarations, including access to subprograms.
1138 if Ada_Version >= Ada_2012 then
1139 null;
1141 else
1142 Error_Msg_NE
1143 ("illegal use of incomplete type&",
1144 Result_Definition (T_Def), Typ);
1145 end if;
1147 elsif Ekind (Current_Scope) = E_Package
1148 and then In_Private_Part (Current_Scope)
1149 then
1150 if Ekind (Typ) = E_Incomplete_Type then
1151 Append_Elmt (Desig_Type, Private_Dependents (Typ));
1153 elsif Is_Class_Wide_Type (Typ)
1154 and then Ekind (Etype (Typ)) = E_Incomplete_Type
1155 then
1156 Append_Elmt
1157 (Desig_Type, Private_Dependents (Etype (Typ)));
1158 end if;
1159 end if;
1161 Set_Etype (Desig_Type, Typ);
1162 end if;
1163 end;
1164 end if;
1166 if not (Is_Type (Etype (Desig_Type))) then
1167 Error_Msg_N
1168 ("expect type in function specification",
1169 Result_Definition (T_Def));
1170 end if;
1172 else
1173 Set_Etype (Desig_Type, Standard_Void_Type);
1174 end if;
1176 if Present (Formals) then
1177 Push_Scope (Desig_Type);
1179 -- A bit of a kludge here. These kludges will be removed when Itypes
1180 -- have proper parent pointers to their declarations???
1182 -- Kludge 1) Link defining_identifier of formals. Required by
1183 -- First_Formal to provide its functionality.
1185 declare
1186 F : Node_Id;
1188 begin
1189 F := First (Formals);
1191 -- In ASIS mode, the access_to_subprogram may be analyzed twice,
1192 -- when it is part of an unconstrained type and subtype expansion
1193 -- is disabled. To avoid back-end problems with shared profiles,
1194 -- use previous subprogram type as the designated type.
1196 if ASIS_Mode
1197 and then Present (Scope (Defining_Identifier (F)))
1198 then
1199 Set_Etype (T_Name, T_Name);
1200 Init_Size_Align (T_Name);
1201 Set_Directly_Designated_Type (T_Name,
1202 Scope (Defining_Identifier (F)));
1203 return;
1204 end if;
1206 while Present (F) loop
1207 if No (Parent (Defining_Identifier (F))) then
1208 Set_Parent (Defining_Identifier (F), F);
1209 end if;
1211 Next (F);
1212 end loop;
1213 end;
1215 Process_Formals (Formals, Parent (T_Def));
1217 -- Kludge 2) End_Scope requires that the parent pointer be set to
1218 -- something reasonable, but Itypes don't have parent pointers. So
1219 -- we set it and then unset it ???
1221 Set_Parent (Desig_Type, T_Name);
1222 End_Scope;
1223 Set_Parent (Desig_Type, Empty);
1224 end if;
1226 -- Check for premature usage of the type being defined
1228 Check_For_Premature_Usage (T_Def);
1230 -- The return type and/or any parameter type may be incomplete. Mark
1231 -- the subprogram_type as depending on the incomplete type, so that
1232 -- it can be updated when the full type declaration is seen. This
1233 -- only applies to incomplete types declared in some enclosing scope,
1234 -- not to limited views from other packages.
1236 if Present (Formals) then
1237 Formal := First_Formal (Desig_Type);
1238 while Present (Formal) loop
1239 if Ekind (Formal) /= E_In_Parameter
1240 and then Nkind (T_Def) = N_Access_Function_Definition
1241 then
1242 Error_Msg_N ("functions can only have IN parameters", Formal);
1243 end if;
1245 if Ekind (Etype (Formal)) = E_Incomplete_Type
1246 and then In_Open_Scopes (Scope (Etype (Formal)))
1247 then
1248 Append_Elmt (Desig_Type, Private_Dependents (Etype (Formal)));
1249 Set_Has_Delayed_Freeze (Desig_Type);
1250 end if;
1252 Next_Formal (Formal);
1253 end loop;
1254 end if;
1256 -- If the return type is incomplete, this is legal as long as the
1257 -- type is declared in the current scope and will be completed in
1258 -- it (rather than being part of limited view).
1260 if Ekind (Etype (Desig_Type)) = E_Incomplete_Type
1261 and then not Has_Delayed_Freeze (Desig_Type)
1262 and then In_Open_Scopes (Scope (Etype (Desig_Type)))
1263 then
1264 Append_Elmt (Desig_Type, Private_Dependents (Etype (Desig_Type)));
1265 Set_Has_Delayed_Freeze (Desig_Type);
1266 end if;
1268 Check_Delayed_Subprogram (Desig_Type);
1270 if Protected_Present (T_Def) then
1271 Set_Ekind (T_Name, E_Access_Protected_Subprogram_Type);
1272 Set_Convention (Desig_Type, Convention_Protected);
1273 else
1274 Set_Ekind (T_Name, E_Access_Subprogram_Type);
1275 end if;
1277 Set_Can_Use_Internal_Rep (T_Name, not Always_Compatible_Rep_On_Target);
1279 Set_Etype (T_Name, T_Name);
1280 Init_Size_Align (T_Name);
1281 Set_Directly_Designated_Type (T_Name, Desig_Type);
1283 -- Ada 2005 (AI-231): Propagate the null-excluding attribute
1285 Set_Can_Never_Be_Null (T_Name, Null_Exclusion_Present (T_Def));
1287 Check_Restriction (No_Access_Subprograms, T_Def);
1288 end Access_Subprogram_Declaration;
1290 ----------------------------
1291 -- Access_Type_Declaration --
1292 ----------------------------
1294 procedure Access_Type_Declaration (T : Entity_Id; Def : Node_Id) is
1295 P : constant Node_Id := Parent (Def);
1296 S : constant Node_Id := Subtype_Indication (Def);
1298 Full_Desig : Entity_Id;
1300 begin
1301 Check_SPARK_Restriction ("access type is not allowed", Def);
1303 -- Check for permissible use of incomplete type
1305 if Nkind (S) /= N_Subtype_Indication then
1306 Analyze (S);
1308 if Ekind (Root_Type (Entity (S))) = E_Incomplete_Type then
1309 Set_Directly_Designated_Type (T, Entity (S));
1310 else
1311 Set_Directly_Designated_Type (T,
1312 Process_Subtype (S, P, T, 'P'));
1313 end if;
1315 else
1316 Set_Directly_Designated_Type (T,
1317 Process_Subtype (S, P, T, 'P'));
1318 end if;
1320 if All_Present (Def) or Constant_Present (Def) then
1321 Set_Ekind (T, E_General_Access_Type);
1322 else
1323 Set_Ekind (T, E_Access_Type);
1324 end if;
1326 Full_Desig := Designated_Type (T);
1328 if Base_Type (Full_Desig) = T then
1329 Error_Msg_N ("access type cannot designate itself", S);
1331 -- In Ada 2005, the type may have a limited view through some unit
1332 -- in its own context, allowing the following circularity that cannot
1333 -- be detected earlier
1335 elsif Is_Class_Wide_Type (Full_Desig)
1336 and then Etype (Full_Desig) = T
1337 then
1338 Error_Msg_N
1339 ("access type cannot designate its own classwide type", S);
1341 -- Clean up indication of tagged status to prevent cascaded errors
1343 Set_Is_Tagged_Type (T, False);
1344 end if;
1346 Set_Etype (T, T);
1348 -- If the type has appeared already in a with_type clause, it is
1349 -- frozen and the pointer size is already set. Else, initialize.
1351 if not From_With_Type (T) then
1352 Init_Size_Align (T);
1353 end if;
1355 -- Note that Has_Task is always false, since the access type itself
1356 -- is not a task type. See Einfo for more description on this point.
1357 -- Exactly the same consideration applies to Has_Controlled_Component.
1359 Set_Has_Task (T, False);
1360 Set_Has_Controlled_Component (T, False);
1362 -- Initialize field Finalization_Master explicitly to Empty, to avoid
1363 -- problems where an incomplete view of this entity has been previously
1364 -- established by a limited with and an overlaid version of this field
1365 -- (Stored_Constraint) was initialized for the incomplete view.
1367 -- This reset is performed in most cases except where the access type
1368 -- has been created for the purposes of allocating or deallocating a
1369 -- build-in-place object. Such access types have explicitly set pools
1370 -- and finalization masters.
1372 if No (Associated_Storage_Pool (T)) then
1373 Set_Finalization_Master (T, Empty);
1374 end if;
1376 -- Ada 2005 (AI-231): Propagate the null-excluding and access-constant
1377 -- attributes
1379 Set_Can_Never_Be_Null (T, Null_Exclusion_Present (Def));
1380 Set_Is_Access_Constant (T, Constant_Present (Def));
1381 end Access_Type_Declaration;
1383 ----------------------------------
1384 -- Add_Interface_Tag_Components --
1385 ----------------------------------
1387 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id) is
1388 Loc : constant Source_Ptr := Sloc (N);
1389 L : List_Id;
1390 Last_Tag : Node_Id;
1392 procedure Add_Tag (Iface : Entity_Id);
1393 -- Add tag for one of the progenitor interfaces
1395 -------------
1396 -- Add_Tag --
1397 -------------
1399 procedure Add_Tag (Iface : Entity_Id) is
1400 Decl : Node_Id;
1401 Def : Node_Id;
1402 Tag : Entity_Id;
1403 Offset : Entity_Id;
1405 begin
1406 pragma Assert (Is_Tagged_Type (Iface)
1407 and then Is_Interface (Iface));
1409 -- This is a reasonable place to propagate predicates
1411 if Has_Predicates (Iface) then
1412 Set_Has_Predicates (Typ);
1413 end if;
1415 Def :=
1416 Make_Component_Definition (Loc,
1417 Aliased_Present => True,
1418 Subtype_Indication =>
1419 New_Occurrence_Of (RTE (RE_Interface_Tag), Loc));
1421 Tag := Make_Temporary (Loc, 'V');
1423 Decl :=
1424 Make_Component_Declaration (Loc,
1425 Defining_Identifier => Tag,
1426 Component_Definition => Def);
1428 Analyze_Component_Declaration (Decl);
1430 Set_Analyzed (Decl);
1431 Set_Ekind (Tag, E_Component);
1432 Set_Is_Tag (Tag);
1433 Set_Is_Aliased (Tag);
1434 Set_Related_Type (Tag, Iface);
1435 Init_Component_Location (Tag);
1437 pragma Assert (Is_Frozen (Iface));
1439 Set_DT_Entry_Count (Tag,
1440 DT_Entry_Count (First_Entity (Iface)));
1442 if No (Last_Tag) then
1443 Prepend (Decl, L);
1444 else
1445 Insert_After (Last_Tag, Decl);
1446 end if;
1448 Last_Tag := Decl;
1450 -- If the ancestor has discriminants we need to give special support
1451 -- to store the offset_to_top value of the secondary dispatch tables.
1452 -- For this purpose we add a supplementary component just after the
1453 -- field that contains the tag associated with each secondary DT.
1455 if Typ /= Etype (Typ)
1456 and then Has_Discriminants (Etype (Typ))
1457 then
1458 Def :=
1459 Make_Component_Definition (Loc,
1460 Subtype_Indication =>
1461 New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
1463 Offset := Make_Temporary (Loc, 'V');
1465 Decl :=
1466 Make_Component_Declaration (Loc,
1467 Defining_Identifier => Offset,
1468 Component_Definition => Def);
1470 Analyze_Component_Declaration (Decl);
1472 Set_Analyzed (Decl);
1473 Set_Ekind (Offset, E_Component);
1474 Set_Is_Aliased (Offset);
1475 Set_Related_Type (Offset, Iface);
1476 Init_Component_Location (Offset);
1477 Insert_After (Last_Tag, Decl);
1478 Last_Tag := Decl;
1479 end if;
1480 end Add_Tag;
1482 -- Local variables
1484 Elmt : Elmt_Id;
1485 Ext : Node_Id;
1486 Comp : Node_Id;
1488 -- Start of processing for Add_Interface_Tag_Components
1490 begin
1491 if not RTE_Available (RE_Interface_Tag) then
1492 Error_Msg
1493 ("(Ada 2005) interface types not supported by this run-time!",
1494 Sloc (N));
1495 return;
1496 end if;
1498 if Ekind (Typ) /= E_Record_Type
1499 or else (Is_Concurrent_Record_Type (Typ)
1500 and then Is_Empty_List (Abstract_Interface_List (Typ)))
1501 or else (not Is_Concurrent_Record_Type (Typ)
1502 and then No (Interfaces (Typ))
1503 and then Is_Empty_Elmt_List (Interfaces (Typ)))
1504 then
1505 return;
1506 end if;
1508 -- Find the current last tag
1510 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1511 Ext := Record_Extension_Part (Type_Definition (N));
1512 else
1513 pragma Assert (Nkind (Type_Definition (N)) = N_Record_Definition);
1514 Ext := Type_Definition (N);
1515 end if;
1517 Last_Tag := Empty;
1519 if not (Present (Component_List (Ext))) then
1520 Set_Null_Present (Ext, False);
1521 L := New_List;
1522 Set_Component_List (Ext,
1523 Make_Component_List (Loc,
1524 Component_Items => L,
1525 Null_Present => False));
1526 else
1527 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1528 L := Component_Items
1529 (Component_List
1530 (Record_Extension_Part
1531 (Type_Definition (N))));
1532 else
1533 L := Component_Items
1534 (Component_List
1535 (Type_Definition (N)));
1536 end if;
1538 -- Find the last tag component
1540 Comp := First (L);
1541 while Present (Comp) loop
1542 if Nkind (Comp) = N_Component_Declaration
1543 and then Is_Tag (Defining_Identifier (Comp))
1544 then
1545 Last_Tag := Comp;
1546 end if;
1548 Next (Comp);
1549 end loop;
1550 end if;
1552 -- At this point L references the list of components and Last_Tag
1553 -- references the current last tag (if any). Now we add the tag
1554 -- corresponding with all the interfaces that are not implemented
1555 -- by the parent.
1557 if Present (Interfaces (Typ)) then
1558 Elmt := First_Elmt (Interfaces (Typ));
1559 while Present (Elmt) loop
1560 Add_Tag (Node (Elmt));
1561 Next_Elmt (Elmt);
1562 end loop;
1563 end if;
1564 end Add_Interface_Tag_Components;
1566 -------------------------------------
1567 -- Add_Internal_Interface_Entities --
1568 -------------------------------------
1570 procedure Add_Internal_Interface_Entities (Tagged_Type : Entity_Id) is
1571 Elmt : Elmt_Id;
1572 Iface : Entity_Id;
1573 Iface_Elmt : Elmt_Id;
1574 Iface_Prim : Entity_Id;
1575 Ifaces_List : Elist_Id;
1576 New_Subp : Entity_Id := Empty;
1577 Prim : Entity_Id;
1578 Restore_Scope : Boolean := False;
1580 begin
1581 pragma Assert (Ada_Version >= Ada_2005
1582 and then Is_Record_Type (Tagged_Type)
1583 and then Is_Tagged_Type (Tagged_Type)
1584 and then Has_Interfaces (Tagged_Type)
1585 and then not Is_Interface (Tagged_Type));
1587 -- Ensure that the internal entities are added to the scope of the type
1589 if Scope (Tagged_Type) /= Current_Scope then
1590 Push_Scope (Scope (Tagged_Type));
1591 Restore_Scope := True;
1592 end if;
1594 Collect_Interfaces (Tagged_Type, Ifaces_List);
1596 Iface_Elmt := First_Elmt (Ifaces_List);
1597 while Present (Iface_Elmt) loop
1598 Iface := Node (Iface_Elmt);
1600 -- Originally we excluded here from this processing interfaces that
1601 -- are parents of Tagged_Type because their primitives are located
1602 -- in the primary dispatch table (and hence no auxiliary internal
1603 -- entities are required to handle secondary dispatch tables in such
1604 -- case). However, these auxiliary entities are also required to
1605 -- handle derivations of interfaces in formals of generics (see
1606 -- Derive_Subprograms).
1608 Elmt := First_Elmt (Primitive_Operations (Iface));
1609 while Present (Elmt) loop
1610 Iface_Prim := Node (Elmt);
1612 if not Is_Predefined_Dispatching_Operation (Iface_Prim) then
1613 Prim :=
1614 Find_Primitive_Covering_Interface
1615 (Tagged_Type => Tagged_Type,
1616 Iface_Prim => Iface_Prim);
1618 if No (Prim) and then Serious_Errors_Detected > 0 then
1619 goto Continue;
1620 end if;
1622 pragma Assert (Present (Prim));
1624 -- Ada 2012 (AI05-0197): If the name of the covering primitive
1625 -- differs from the name of the interface primitive then it is
1626 -- a private primitive inherited from a parent type. In such
1627 -- case, given that Tagged_Type covers the interface, the
1628 -- inherited private primitive becomes visible. For such
1629 -- purpose we add a new entity that renames the inherited
1630 -- private primitive.
1632 if Chars (Prim) /= Chars (Iface_Prim) then
1633 pragma Assert (Has_Suffix (Prim, 'P'));
1634 Derive_Subprogram
1635 (New_Subp => New_Subp,
1636 Parent_Subp => Iface_Prim,
1637 Derived_Type => Tagged_Type,
1638 Parent_Type => Iface);
1639 Set_Alias (New_Subp, Prim);
1640 Set_Is_Abstract_Subprogram
1641 (New_Subp, Is_Abstract_Subprogram (Prim));
1642 end if;
1644 Derive_Subprogram
1645 (New_Subp => New_Subp,
1646 Parent_Subp => Iface_Prim,
1647 Derived_Type => Tagged_Type,
1648 Parent_Type => Iface);
1650 -- Ada 2005 (AI-251): Decorate internal entity Iface_Subp
1651 -- associated with interface types. These entities are
1652 -- only registered in the list of primitives of its
1653 -- corresponding tagged type because they are only used
1654 -- to fill the contents of the secondary dispatch tables.
1655 -- Therefore they are removed from the homonym chains.
1657 Set_Is_Hidden (New_Subp);
1658 Set_Is_Internal (New_Subp);
1659 Set_Alias (New_Subp, Prim);
1660 Set_Is_Abstract_Subprogram
1661 (New_Subp, Is_Abstract_Subprogram (Prim));
1662 Set_Interface_Alias (New_Subp, Iface_Prim);
1664 -- Internal entities associated with interface types are
1665 -- only registered in the list of primitives of the tagged
1666 -- type. They are only used to fill the contents of the
1667 -- secondary dispatch tables. Therefore they are not needed
1668 -- in the homonym chains.
1670 Remove_Homonym (New_Subp);
1672 -- Hidden entities associated with interfaces must have set
1673 -- the Has_Delay_Freeze attribute to ensure that, in case of
1674 -- locally defined tagged types (or compiling with static
1675 -- dispatch tables generation disabled) the corresponding
1676 -- entry of the secondary dispatch table is filled when
1677 -- such an entity is frozen.
1679 Set_Has_Delayed_Freeze (New_Subp);
1680 end if;
1682 <<Continue>>
1683 Next_Elmt (Elmt);
1684 end loop;
1686 Next_Elmt (Iface_Elmt);
1687 end loop;
1689 if Restore_Scope then
1690 Pop_Scope;
1691 end if;
1692 end Add_Internal_Interface_Entities;
1694 -----------------------------------
1695 -- Analyze_Component_Declaration --
1696 -----------------------------------
1698 procedure Analyze_Component_Declaration (N : Node_Id) is
1699 Id : constant Entity_Id := Defining_Identifier (N);
1700 E : constant Node_Id := Expression (N);
1701 Typ : constant Node_Id :=
1702 Subtype_Indication (Component_Definition (N));
1703 T : Entity_Id;
1704 P : Entity_Id;
1706 function Contains_POC (Constr : Node_Id) return Boolean;
1707 -- Determines whether a constraint uses the discriminant of a record
1708 -- type thus becoming a per-object constraint (POC).
1710 function Is_Known_Limited (Typ : Entity_Id) return Boolean;
1711 -- Typ is the type of the current component, check whether this type is
1712 -- a limited type. Used to validate declaration against that of
1713 -- enclosing record.
1715 ------------------
1716 -- Contains_POC --
1717 ------------------
1719 function Contains_POC (Constr : Node_Id) return Boolean is
1720 begin
1721 -- Prevent cascaded errors
1723 if Error_Posted (Constr) then
1724 return False;
1725 end if;
1727 case Nkind (Constr) is
1728 when N_Attribute_Reference =>
1729 return
1730 Attribute_Name (Constr) = Name_Access
1731 and then Prefix (Constr) = Scope (Entity (Prefix (Constr)));
1733 when N_Discriminant_Association =>
1734 return Denotes_Discriminant (Expression (Constr));
1736 when N_Identifier =>
1737 return Denotes_Discriminant (Constr);
1739 when N_Index_Or_Discriminant_Constraint =>
1740 declare
1741 IDC : Node_Id;
1743 begin
1744 IDC := First (Constraints (Constr));
1745 while Present (IDC) loop
1747 -- One per-object constraint is sufficient
1749 if Contains_POC (IDC) then
1750 return True;
1751 end if;
1753 Next (IDC);
1754 end loop;
1756 return False;
1757 end;
1759 when N_Range =>
1760 return Denotes_Discriminant (Low_Bound (Constr))
1761 or else
1762 Denotes_Discriminant (High_Bound (Constr));
1764 when N_Range_Constraint =>
1765 return Denotes_Discriminant (Range_Expression (Constr));
1767 when others =>
1768 return False;
1770 end case;
1771 end Contains_POC;
1773 ----------------------
1774 -- Is_Known_Limited --
1775 ----------------------
1777 function Is_Known_Limited (Typ : Entity_Id) return Boolean is
1778 P : constant Entity_Id := Etype (Typ);
1779 R : constant Entity_Id := Root_Type (Typ);
1781 begin
1782 if Is_Limited_Record (Typ) then
1783 return True;
1785 -- If the root type is limited (and not a limited interface)
1786 -- so is the current type
1788 elsif Is_Limited_Record (R)
1789 and then
1790 (not Is_Interface (R)
1791 or else not Is_Limited_Interface (R))
1792 then
1793 return True;
1795 -- Else the type may have a limited interface progenitor, but a
1796 -- limited record parent.
1798 elsif R /= P
1799 and then Is_Limited_Record (P)
1800 then
1801 return True;
1803 else
1804 return False;
1805 end if;
1806 end Is_Known_Limited;
1808 -- Start of processing for Analyze_Component_Declaration
1810 begin
1811 Generate_Definition (Id);
1812 Enter_Name (Id);
1814 if Present (Typ) then
1815 T := Find_Type_Of_Object
1816 (Subtype_Indication (Component_Definition (N)), N);
1818 if not Nkind_In (Typ, N_Identifier, N_Expanded_Name) then
1819 Check_SPARK_Restriction ("subtype mark required", Typ);
1820 end if;
1822 -- Ada 2005 (AI-230): Access Definition case
1824 else
1825 pragma Assert (Present
1826 (Access_Definition (Component_Definition (N))));
1828 T := Access_Definition
1829 (Related_Nod => N,
1830 N => Access_Definition (Component_Definition (N)));
1831 Set_Is_Local_Anonymous_Access (T);
1833 -- Ada 2005 (AI-254)
1835 if Present (Access_To_Subprogram_Definition
1836 (Access_Definition (Component_Definition (N))))
1837 and then Protected_Present (Access_To_Subprogram_Definition
1838 (Access_Definition
1839 (Component_Definition (N))))
1840 then
1841 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1842 end if;
1843 end if;
1845 -- If the subtype is a constrained subtype of the enclosing record,
1846 -- (which must have a partial view) the back-end does not properly
1847 -- handle the recursion. Rewrite the component declaration with an
1848 -- explicit subtype indication, which is acceptable to Gigi. We can copy
1849 -- the tree directly because side effects have already been removed from
1850 -- discriminant constraints.
1852 if Ekind (T) = E_Access_Subtype
1853 and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
1854 and then Comes_From_Source (T)
1855 and then Nkind (Parent (T)) = N_Subtype_Declaration
1856 and then Etype (Directly_Designated_Type (T)) = Current_Scope
1857 then
1858 Rewrite
1859 (Subtype_Indication (Component_Definition (N)),
1860 New_Copy_Tree (Subtype_Indication (Parent (T))));
1861 T := Find_Type_Of_Object
1862 (Subtype_Indication (Component_Definition (N)), N);
1863 end if;
1865 -- If the component declaration includes a default expression, then we
1866 -- check that the component is not of a limited type (RM 3.7(5)),
1867 -- and do the special preanalysis of the expression (see section on
1868 -- "Handling of Default and Per-Object Expressions" in the spec of
1869 -- package Sem).
1871 if Present (E) then
1872 Check_SPARK_Restriction ("default expression is not allowed", E);
1873 Preanalyze_Spec_Expression (E, T);
1874 Check_Initialization (T, E);
1876 if Ada_Version >= Ada_2005
1877 and then Ekind (T) = E_Anonymous_Access_Type
1878 and then Etype (E) /= Any_Type
1879 then
1880 -- Check RM 3.9.2(9): "if the expected type for an expression is
1881 -- an anonymous access-to-specific tagged type, then the object
1882 -- designated by the expression shall not be dynamically tagged
1883 -- unless it is a controlling operand in a call on a dispatching
1884 -- operation"
1886 if Is_Tagged_Type (Directly_Designated_Type (T))
1887 and then
1888 Ekind (Directly_Designated_Type (T)) /= E_Class_Wide_Type
1889 and then
1890 Ekind (Directly_Designated_Type (Etype (E))) =
1891 E_Class_Wide_Type
1892 then
1893 Error_Msg_N
1894 ("access to specific tagged type required (RM 3.9.2(9))", E);
1895 end if;
1897 -- (Ada 2005: AI-230): Accessibility check for anonymous
1898 -- components
1900 if Type_Access_Level (Etype (E)) >
1901 Deepest_Type_Access_Level (T)
1902 then
1903 Error_Msg_N
1904 ("expression has deeper access level than component " &
1905 "(RM 3.10.2 (12.2))", E);
1906 end if;
1908 -- The initialization expression is a reference to an access
1909 -- discriminant. The type of the discriminant is always deeper
1910 -- than any access type.
1912 if Ekind (Etype (E)) = E_Anonymous_Access_Type
1913 and then Is_Entity_Name (E)
1914 and then Ekind (Entity (E)) = E_In_Parameter
1915 and then Present (Discriminal_Link (Entity (E)))
1916 then
1917 Error_Msg_N
1918 ("discriminant has deeper accessibility level than target",
1920 end if;
1921 end if;
1922 end if;
1924 -- The parent type may be a private view with unknown discriminants,
1925 -- and thus unconstrained. Regular components must be constrained.
1927 if Is_Indefinite_Subtype (T) and then Chars (Id) /= Name_uParent then
1928 if Is_Class_Wide_Type (T) then
1929 Error_Msg_N
1930 ("class-wide subtype with unknown discriminants" &
1931 " in component declaration",
1932 Subtype_Indication (Component_Definition (N)));
1933 else
1934 Error_Msg_N
1935 ("unconstrained subtype in component declaration",
1936 Subtype_Indication (Component_Definition (N)));
1937 end if;
1939 -- Components cannot be abstract, except for the special case of
1940 -- the _Parent field (case of extending an abstract tagged type)
1942 elsif Is_Abstract_Type (T) and then Chars (Id) /= Name_uParent then
1943 Error_Msg_N ("type of a component cannot be abstract", N);
1944 end if;
1946 Set_Etype (Id, T);
1947 Set_Is_Aliased (Id, Aliased_Present (Component_Definition (N)));
1949 -- The component declaration may have a per-object constraint, set
1950 -- the appropriate flag in the defining identifier of the subtype.
1952 if Present (Subtype_Indication (Component_Definition (N))) then
1953 declare
1954 Sindic : constant Node_Id :=
1955 Subtype_Indication (Component_Definition (N));
1956 begin
1957 if Nkind (Sindic) = N_Subtype_Indication
1958 and then Present (Constraint (Sindic))
1959 and then Contains_POC (Constraint (Sindic))
1960 then
1961 Set_Has_Per_Object_Constraint (Id);
1962 end if;
1963 end;
1964 end if;
1966 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
1967 -- out some static checks.
1969 if Ada_Version >= Ada_2005
1970 and then Can_Never_Be_Null (T)
1971 then
1972 Null_Exclusion_Static_Checks (N);
1973 end if;
1975 -- If this component is private (or depends on a private type), flag the
1976 -- record type to indicate that some operations are not available.
1978 P := Private_Component (T);
1980 if Present (P) then
1982 -- Check for circular definitions
1984 if P = Any_Type then
1985 Set_Etype (Id, Any_Type);
1987 -- There is a gap in the visibility of operations only if the
1988 -- component type is not defined in the scope of the record type.
1990 elsif Scope (P) = Scope (Current_Scope) then
1991 null;
1993 elsif Is_Limited_Type (P) then
1994 Set_Is_Limited_Composite (Current_Scope);
1996 else
1997 Set_Is_Private_Composite (Current_Scope);
1998 end if;
1999 end if;
2001 if P /= Any_Type
2002 and then Is_Limited_Type (T)
2003 and then Chars (Id) /= Name_uParent
2004 and then Is_Tagged_Type (Current_Scope)
2005 then
2006 if Is_Derived_Type (Current_Scope)
2007 and then not Is_Known_Limited (Current_Scope)
2008 then
2009 Error_Msg_N
2010 ("extension of nonlimited type cannot have limited components",
2013 if Is_Interface (Root_Type (Current_Scope)) then
2014 Error_Msg_N
2015 ("\limitedness is not inherited from limited interface", N);
2016 Error_Msg_N ("\add LIMITED to type indication", N);
2017 end if;
2019 Explain_Limited_Type (T, N);
2020 Set_Etype (Id, Any_Type);
2021 Set_Is_Limited_Composite (Current_Scope, False);
2023 elsif not Is_Derived_Type (Current_Scope)
2024 and then not Is_Limited_Record (Current_Scope)
2025 and then not Is_Concurrent_Type (Current_Scope)
2026 then
2027 Error_Msg_N
2028 ("nonlimited tagged type cannot have limited components", N);
2029 Explain_Limited_Type (T, N);
2030 Set_Etype (Id, Any_Type);
2031 Set_Is_Limited_Composite (Current_Scope, False);
2032 end if;
2033 end if;
2035 Set_Original_Record_Component (Id, Id);
2037 if Has_Aspects (N) then
2038 Analyze_Aspect_Specifications (N, Id);
2039 end if;
2041 Analyze_Dimension (N);
2042 end Analyze_Component_Declaration;
2044 --------------------------
2045 -- Analyze_Declarations --
2046 --------------------------
2048 procedure Analyze_Declarations (L : List_Id) is
2049 D : Node_Id;
2050 Freeze_From : Entity_Id := Empty;
2051 Next_Node : Node_Id;
2053 procedure Adjust_D;
2054 -- Adjust D not to include implicit label declarations, since these
2055 -- have strange Sloc values that result in elaboration check problems.
2056 -- (They have the sloc of the label as found in the source, and that
2057 -- is ahead of the current declarative part).
2059 --------------
2060 -- Adjust_D --
2061 --------------
2063 procedure Adjust_D is
2064 begin
2065 while Present (Prev (D))
2066 and then Nkind (D) = N_Implicit_Label_Declaration
2067 loop
2068 Prev (D);
2069 end loop;
2070 end Adjust_D;
2072 -- Start of processing for Analyze_Declarations
2074 begin
2075 if Restriction_Check_Required (SPARK) then
2076 Check_Later_Vs_Basic_Declarations (L, During_Parsing => False);
2077 end if;
2079 D := First (L);
2080 while Present (D) loop
2082 -- Package spec cannot contain a package declaration in SPARK
2084 if Nkind (D) = N_Package_Declaration
2085 and then Nkind (Parent (L)) = N_Package_Specification
2086 then
2087 Check_SPARK_Restriction
2088 ("package specification cannot contain a package declaration",
2090 end if;
2092 -- Complete analysis of declaration
2094 Analyze (D);
2095 Next_Node := Next (D);
2097 if No (Freeze_From) then
2098 Freeze_From := First_Entity (Current_Scope);
2099 end if;
2101 -- At the end of a declarative part, freeze remaining entities
2102 -- declared in it. The end of the visible declarations of package
2103 -- specification is not the end of a declarative part if private
2104 -- declarations are present. The end of a package declaration is a
2105 -- freezing point only if it a library package. A task definition or
2106 -- protected type definition is not a freeze point either. Finally,
2107 -- we do not freeze entities in generic scopes, because there is no
2108 -- code generated for them and freeze nodes will be generated for
2109 -- the instance.
2111 -- The end of a package instantiation is not a freeze point, but
2112 -- for now we make it one, because the generic body is inserted
2113 -- (currently) immediately after. Generic instantiations will not
2114 -- be a freeze point once delayed freezing of bodies is implemented.
2115 -- (This is needed in any case for early instantiations ???).
2117 if No (Next_Node) then
2118 if Nkind_In (Parent (L), N_Component_List,
2119 N_Task_Definition,
2120 N_Protected_Definition)
2121 then
2122 null;
2124 elsif Nkind (Parent (L)) /= N_Package_Specification then
2125 if Nkind (Parent (L)) = N_Package_Body then
2126 Freeze_From := First_Entity (Current_Scope);
2127 end if;
2129 Adjust_D;
2130 Freeze_All (Freeze_From, D);
2131 Freeze_From := Last_Entity (Current_Scope);
2133 elsif Scope (Current_Scope) /= Standard_Standard
2134 and then not Is_Child_Unit (Current_Scope)
2135 and then No (Generic_Parent (Parent (L)))
2136 then
2137 null;
2139 elsif L /= Visible_Declarations (Parent (L))
2140 or else No (Private_Declarations (Parent (L)))
2141 or else Is_Empty_List (Private_Declarations (Parent (L)))
2142 then
2143 Adjust_D;
2144 Freeze_All (Freeze_From, D);
2145 Freeze_From := Last_Entity (Current_Scope);
2146 end if;
2148 -- If next node is a body then freeze all types before the body.
2149 -- An exception occurs for some expander-generated bodies. If these
2150 -- are generated at places where in general language rules would not
2151 -- allow a freeze point, then we assume that the expander has
2152 -- explicitly checked that all required types are properly frozen,
2153 -- and we do not cause general freezing here. This special circuit
2154 -- is used when the encountered body is marked as having already
2155 -- been analyzed.
2157 -- In all other cases (bodies that come from source, and expander
2158 -- generated bodies that have not been analyzed yet), freeze all
2159 -- types now. Note that in the latter case, the expander must take
2160 -- care to attach the bodies at a proper place in the tree so as to
2161 -- not cause unwanted freezing at that point.
2163 elsif not Analyzed (Next_Node)
2164 and then (Nkind_In (Next_Node, N_Subprogram_Body,
2165 N_Entry_Body,
2166 N_Package_Body,
2167 N_Protected_Body,
2168 N_Task_Body)
2169 or else
2170 Nkind (Next_Node) in N_Body_Stub)
2171 then
2172 Adjust_D;
2173 Freeze_All (Freeze_From, D);
2174 Freeze_From := Last_Entity (Current_Scope);
2175 end if;
2177 D := Next_Node;
2178 end loop;
2180 -- One more thing to do, we need to scan the declarations to check
2181 -- for any precondition/postcondition pragmas (Pre/Post aspects have
2182 -- by this stage been converted into corresponding pragmas). It is
2183 -- at this point that we analyze the expressions in such pragmas,
2184 -- to implement the delayed visibility requirement.
2186 declare
2187 Decl : Node_Id;
2188 Spec : Node_Id;
2189 Sent : Entity_Id;
2190 Prag : Node_Id;
2192 begin
2193 Decl := First (L);
2194 while Present (Decl) loop
2195 if Nkind (Original_Node (Decl)) = N_Subprogram_Declaration then
2196 Spec := Specification (Original_Node (Decl));
2197 Sent := Defining_Unit_Name (Spec);
2199 -- Analyze preconditions and postconditions
2201 Prag := Spec_PPC_List (Contract (Sent));
2202 while Present (Prag) loop
2203 Analyze_PPC_In_Decl_Part (Prag, Sent);
2204 Prag := Next_Pragma (Prag);
2205 end loop;
2207 -- Analyze contract-cases and test-cases
2209 Prag := Spec_CTC_List (Contract (Sent));
2210 while Present (Prag) loop
2211 Analyze_CTC_In_Decl_Part (Prag, Sent);
2212 Prag := Next_Pragma (Prag);
2213 end loop;
2215 -- At this point, entities have been attached to identifiers.
2216 -- This is required to be able to detect suspicious contracts.
2218 Check_Subprogram_Contract (Sent);
2219 end if;
2221 Next (Decl);
2222 end loop;
2223 end;
2224 end Analyze_Declarations;
2226 -----------------------------------
2227 -- Analyze_Full_Type_Declaration --
2228 -----------------------------------
2230 procedure Analyze_Full_Type_Declaration (N : Node_Id) is
2231 Def : constant Node_Id := Type_Definition (N);
2232 Def_Id : constant Entity_Id := Defining_Identifier (N);
2233 T : Entity_Id;
2234 Prev : Entity_Id;
2236 Is_Remote : constant Boolean :=
2237 (Is_Remote_Types (Current_Scope)
2238 or else Is_Remote_Call_Interface (Current_Scope))
2239 and then not (In_Private_Part (Current_Scope)
2240 or else In_Package_Body (Current_Scope));
2242 procedure Check_Ops_From_Incomplete_Type;
2243 -- If there is a tagged incomplete partial view of the type, traverse
2244 -- the primitives of the incomplete view and change the type of any
2245 -- controlling formals and result to indicate the full view. The
2246 -- primitives will be added to the full type's primitive operations
2247 -- list later in Sem_Disp.Check_Operation_From_Incomplete_Type (which
2248 -- is called from Process_Incomplete_Dependents).
2250 ------------------------------------
2251 -- Check_Ops_From_Incomplete_Type --
2252 ------------------------------------
2254 procedure Check_Ops_From_Incomplete_Type is
2255 Elmt : Elmt_Id;
2256 Formal : Entity_Id;
2257 Op : Entity_Id;
2259 begin
2260 if Prev /= T
2261 and then Ekind (Prev) = E_Incomplete_Type
2262 and then Is_Tagged_Type (Prev)
2263 and then Is_Tagged_Type (T)
2264 then
2265 Elmt := First_Elmt (Primitive_Operations (Prev));
2266 while Present (Elmt) loop
2267 Op := Node (Elmt);
2269 Formal := First_Formal (Op);
2270 while Present (Formal) loop
2271 if Etype (Formal) = Prev then
2272 Set_Etype (Formal, T);
2273 end if;
2275 Next_Formal (Formal);
2276 end loop;
2278 if Etype (Op) = Prev then
2279 Set_Etype (Op, T);
2280 end if;
2282 Next_Elmt (Elmt);
2283 end loop;
2284 end if;
2285 end Check_Ops_From_Incomplete_Type;
2287 -- Start of processing for Analyze_Full_Type_Declaration
2289 begin
2290 Prev := Find_Type_Name (N);
2292 -- The full view, if present, now points to the current type
2294 -- Ada 2005 (AI-50217): If the type was previously decorated when
2295 -- imported through a LIMITED WITH clause, it appears as incomplete
2296 -- but has no full view.
2298 if Ekind (Prev) = E_Incomplete_Type
2299 and then Present (Full_View (Prev))
2300 then
2301 T := Full_View (Prev);
2302 else
2303 T := Prev;
2304 end if;
2306 Set_Is_Pure (T, Is_Pure (Current_Scope));
2308 -- We set the flag Is_First_Subtype here. It is needed to set the
2309 -- corresponding flag for the Implicit class-wide-type created
2310 -- during tagged types processing.
2312 Set_Is_First_Subtype (T, True);
2314 -- Only composite types other than array types are allowed to have
2315 -- discriminants.
2317 case Nkind (Def) is
2319 -- For derived types, the rule will be checked once we've figured
2320 -- out the parent type.
2322 when N_Derived_Type_Definition =>
2323 null;
2325 -- For record types, discriminants are allowed, unless we are in
2326 -- SPARK.
2328 when N_Record_Definition =>
2329 if Present (Discriminant_Specifications (N)) then
2330 Check_SPARK_Restriction
2331 ("discriminant type is not allowed",
2332 Defining_Identifier
2333 (First (Discriminant_Specifications (N))));
2334 end if;
2336 when others =>
2337 if Present (Discriminant_Specifications (N)) then
2338 Error_Msg_N
2339 ("elementary or array type cannot have discriminants",
2340 Defining_Identifier
2341 (First (Discriminant_Specifications (N))));
2342 end if;
2343 end case;
2345 -- Elaborate the type definition according to kind, and generate
2346 -- subsidiary (implicit) subtypes where needed. We skip this if it was
2347 -- already done (this happens during the reanalysis that follows a call
2348 -- to the high level optimizer).
2350 if not Analyzed (T) then
2351 Set_Analyzed (T);
2353 case Nkind (Def) is
2355 when N_Access_To_Subprogram_Definition =>
2356 Access_Subprogram_Declaration (T, Def);
2358 -- If this is a remote access to subprogram, we must create the
2359 -- equivalent fat pointer type, and related subprograms.
2361 if Is_Remote then
2362 Process_Remote_AST_Declaration (N);
2363 end if;
2365 -- Validate categorization rule against access type declaration
2366 -- usually a violation in Pure unit, Shared_Passive unit.
2368 Validate_Access_Type_Declaration (T, N);
2370 when N_Access_To_Object_Definition =>
2371 Access_Type_Declaration (T, Def);
2373 -- Validate categorization rule against access type declaration
2374 -- usually a violation in Pure unit, Shared_Passive unit.
2376 Validate_Access_Type_Declaration (T, N);
2378 -- If we are in a Remote_Call_Interface package and define a
2379 -- RACW, then calling stubs and specific stream attributes
2380 -- must be added.
2382 if Is_Remote
2383 and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
2384 then
2385 Add_RACW_Features (Def_Id);
2386 end if;
2388 -- Set no strict aliasing flag if config pragma seen
2390 if Opt.No_Strict_Aliasing then
2391 Set_No_Strict_Aliasing (Base_Type (Def_Id));
2392 end if;
2394 when N_Array_Type_Definition =>
2395 Array_Type_Declaration (T, Def);
2397 when N_Derived_Type_Definition =>
2398 Derived_Type_Declaration (T, N, T /= Def_Id);
2400 when N_Enumeration_Type_Definition =>
2401 Enumeration_Type_Declaration (T, Def);
2403 when N_Floating_Point_Definition =>
2404 Floating_Point_Type_Declaration (T, Def);
2406 when N_Decimal_Fixed_Point_Definition =>
2407 Decimal_Fixed_Point_Type_Declaration (T, Def);
2409 when N_Ordinary_Fixed_Point_Definition =>
2410 Ordinary_Fixed_Point_Type_Declaration (T, Def);
2412 when N_Signed_Integer_Type_Definition =>
2413 Signed_Integer_Type_Declaration (T, Def);
2415 when N_Modular_Type_Definition =>
2416 Modular_Type_Declaration (T, Def);
2418 when N_Record_Definition =>
2419 Record_Type_Declaration (T, N, Prev);
2421 -- If declaration has a parse error, nothing to elaborate.
2423 when N_Error =>
2424 null;
2426 when others =>
2427 raise Program_Error;
2429 end case;
2430 end if;
2432 if Etype (T) = Any_Type then
2433 return;
2434 end if;
2436 -- Controlled type is not allowed in SPARK
2438 if Is_Visibly_Controlled (T) then
2439 Check_SPARK_Restriction ("controlled type is not allowed", N);
2440 end if;
2442 -- Some common processing for all types
2444 Set_Depends_On_Private (T, Has_Private_Component (T));
2445 Check_Ops_From_Incomplete_Type;
2447 -- Both the declared entity, and its anonymous base type if one
2448 -- was created, need freeze nodes allocated.
2450 declare
2451 B : constant Entity_Id := Base_Type (T);
2453 begin
2454 -- In the case where the base type differs from the first subtype, we
2455 -- pre-allocate a freeze node, and set the proper link to the first
2456 -- subtype. Freeze_Entity will use this preallocated freeze node when
2457 -- it freezes the entity.
2459 -- This does not apply if the base type is a generic type, whose
2460 -- declaration is independent of the current derived definition.
2462 if B /= T and then not Is_Generic_Type (B) then
2463 Ensure_Freeze_Node (B);
2464 Set_First_Subtype_Link (Freeze_Node (B), T);
2465 end if;
2467 -- A type that is imported through a limited_with clause cannot
2468 -- generate any code, and thus need not be frozen. However, an access
2469 -- type with an imported designated type needs a finalization list,
2470 -- which may be referenced in some other package that has non-limited
2471 -- visibility on the designated type. Thus we must create the
2472 -- finalization list at the point the access type is frozen, to
2473 -- prevent unsatisfied references at link time.
2475 if not From_With_Type (T) or else Is_Access_Type (T) then
2476 Set_Has_Delayed_Freeze (T);
2477 end if;
2478 end;
2480 -- Case where T is the full declaration of some private type which has
2481 -- been swapped in Defining_Identifier (N).
2483 if T /= Def_Id and then Is_Private_Type (Def_Id) then
2484 Process_Full_View (N, T, Def_Id);
2486 -- Record the reference. The form of this is a little strange, since
2487 -- the full declaration has been swapped in. So the first parameter
2488 -- here represents the entity to which a reference is made which is
2489 -- the "real" entity, i.e. the one swapped in, and the second
2490 -- parameter provides the reference location.
2492 -- Also, we want to kill Has_Pragma_Unreferenced temporarily here
2493 -- since we don't want a complaint about the full type being an
2494 -- unwanted reference to the private type
2496 declare
2497 B : constant Boolean := Has_Pragma_Unreferenced (T);
2498 begin
2499 Set_Has_Pragma_Unreferenced (T, False);
2500 Generate_Reference (T, T, 'c');
2501 Set_Has_Pragma_Unreferenced (T, B);
2502 end;
2504 Set_Completion_Referenced (Def_Id);
2506 -- For completion of incomplete type, process incomplete dependents
2507 -- and always mark the full type as referenced (it is the incomplete
2508 -- type that we get for any real reference).
2510 elsif Ekind (Prev) = E_Incomplete_Type then
2511 Process_Incomplete_Dependents (N, T, Prev);
2512 Generate_Reference (Prev, Def_Id, 'c');
2513 Set_Completion_Referenced (Def_Id);
2515 -- If not private type or incomplete type completion, this is a real
2516 -- definition of a new entity, so record it.
2518 else
2519 Generate_Definition (Def_Id);
2520 end if;
2522 if Chars (Scope (Def_Id)) = Name_System
2523 and then Chars (Def_Id) = Name_Address
2524 and then Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (N)))
2525 then
2526 Set_Is_Descendent_Of_Address (Def_Id);
2527 Set_Is_Descendent_Of_Address (Base_Type (Def_Id));
2528 Set_Is_Descendent_Of_Address (Prev);
2529 end if;
2531 Set_Optimize_Alignment_Flags (Def_Id);
2532 Check_Eliminated (Def_Id);
2534 -- If the declaration is a completion and aspects are present, apply
2535 -- them to the entity for the type which is currently the partial
2536 -- view, but which is the one that will be frozen.
2538 if Has_Aspects (N) then
2539 if Prev /= Def_Id then
2540 Analyze_Aspect_Specifications (N, Prev);
2541 else
2542 Analyze_Aspect_Specifications (N, Def_Id);
2543 end if;
2544 end if;
2545 end Analyze_Full_Type_Declaration;
2547 ----------------------------------
2548 -- Analyze_Incomplete_Type_Decl --
2549 ----------------------------------
2551 procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
2552 F : constant Boolean := Is_Pure (Current_Scope);
2553 T : Entity_Id;
2555 begin
2556 Check_SPARK_Restriction ("incomplete type is not allowed", N);
2558 Generate_Definition (Defining_Identifier (N));
2560 -- Process an incomplete declaration. The identifier must not have been
2561 -- declared already in the scope. However, an incomplete declaration may
2562 -- appear in the private part of a package, for a private type that has
2563 -- already been declared.
2565 -- In this case, the discriminants (if any) must match
2567 T := Find_Type_Name (N);
2569 Set_Ekind (T, E_Incomplete_Type);
2570 Init_Size_Align (T);
2571 Set_Is_First_Subtype (T, True);
2572 Set_Etype (T, T);
2574 -- Ada 2005 (AI-326): Minimum decoration to give support to tagged
2575 -- incomplete types.
2577 if Tagged_Present (N) then
2578 Set_Is_Tagged_Type (T);
2579 Make_Class_Wide_Type (T);
2580 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2581 end if;
2583 Push_Scope (T);
2585 Set_Stored_Constraint (T, No_Elist);
2587 if Present (Discriminant_Specifications (N)) then
2588 Process_Discriminants (N);
2589 end if;
2591 End_Scope;
2593 -- If the type has discriminants, non-trivial subtypes may be
2594 -- declared before the full view of the type. The full views of those
2595 -- subtypes will be built after the full view of the type.
2597 Set_Private_Dependents (T, New_Elmt_List);
2598 Set_Is_Pure (T, F);
2599 end Analyze_Incomplete_Type_Decl;
2601 -----------------------------------
2602 -- Analyze_Interface_Declaration --
2603 -----------------------------------
2605 procedure Analyze_Interface_Declaration (T : Entity_Id; Def : Node_Id) is
2606 CW : constant Entity_Id := Class_Wide_Type (T);
2608 begin
2609 Set_Is_Tagged_Type (T);
2611 Set_Is_Limited_Record (T, Limited_Present (Def)
2612 or else Task_Present (Def)
2613 or else Protected_Present (Def)
2614 or else Synchronized_Present (Def));
2616 -- Type is abstract if full declaration carries keyword, or if previous
2617 -- partial view did.
2619 Set_Is_Abstract_Type (T);
2620 Set_Is_Interface (T);
2622 -- Type is a limited interface if it includes the keyword limited, task,
2623 -- protected, or synchronized.
2625 Set_Is_Limited_Interface
2626 (T, Limited_Present (Def)
2627 or else Protected_Present (Def)
2628 or else Synchronized_Present (Def)
2629 or else Task_Present (Def));
2631 Set_Interfaces (T, New_Elmt_List);
2632 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2634 -- Complete the decoration of the class-wide entity if it was already
2635 -- built (i.e. during the creation of the limited view)
2637 if Present (CW) then
2638 Set_Is_Interface (CW);
2639 Set_Is_Limited_Interface (CW, Is_Limited_Interface (T));
2640 end if;
2642 -- Check runtime support for synchronized interfaces
2644 if VM_Target = No_VM
2645 and then (Is_Task_Interface (T)
2646 or else Is_Protected_Interface (T)
2647 or else Is_Synchronized_Interface (T))
2648 and then not RTE_Available (RE_Select_Specific_Data)
2649 then
2650 Error_Msg_CRT ("synchronized interfaces", T);
2651 end if;
2652 end Analyze_Interface_Declaration;
2654 -----------------------------
2655 -- Analyze_Itype_Reference --
2656 -----------------------------
2658 -- Nothing to do. This node is placed in the tree only for the benefit of
2659 -- back end processing, and has no effect on the semantic processing.
2661 procedure Analyze_Itype_Reference (N : Node_Id) is
2662 begin
2663 pragma Assert (Is_Itype (Itype (N)));
2664 null;
2665 end Analyze_Itype_Reference;
2667 --------------------------------
2668 -- Analyze_Number_Declaration --
2669 --------------------------------
2671 procedure Analyze_Number_Declaration (N : Node_Id) is
2672 Id : constant Entity_Id := Defining_Identifier (N);
2673 E : constant Node_Id := Expression (N);
2674 T : Entity_Id;
2675 Index : Interp_Index;
2676 It : Interp;
2678 begin
2679 Generate_Definition (Id);
2680 Enter_Name (Id);
2682 -- This is an optimization of a common case of an integer literal
2684 if Nkind (E) = N_Integer_Literal then
2685 Set_Is_Static_Expression (E, True);
2686 Set_Etype (E, Universal_Integer);
2688 Set_Etype (Id, Universal_Integer);
2689 Set_Ekind (Id, E_Named_Integer);
2690 Set_Is_Frozen (Id, True);
2691 return;
2692 end if;
2694 Set_Is_Pure (Id, Is_Pure (Current_Scope));
2696 -- Process expression, replacing error by integer zero, to avoid
2697 -- cascaded errors or aborts further along in the processing
2699 -- Replace Error by integer zero, which seems least likely to cause
2700 -- cascaded errors.
2702 if E = Error then
2703 Rewrite (E, Make_Integer_Literal (Sloc (E), Uint_0));
2704 Set_Error_Posted (E);
2705 end if;
2707 Analyze (E);
2709 -- Verify that the expression is static and numeric. If
2710 -- the expression is overloaded, we apply the preference
2711 -- rule that favors root numeric types.
2713 if not Is_Overloaded (E) then
2714 T := Etype (E);
2716 else
2717 T := Any_Type;
2719 Get_First_Interp (E, Index, It);
2720 while Present (It.Typ) loop
2721 if (Is_Integer_Type (It.Typ)
2722 or else Is_Real_Type (It.Typ))
2723 and then (Scope (Base_Type (It.Typ))) = Standard_Standard
2724 then
2725 if T = Any_Type then
2726 T := It.Typ;
2728 elsif It.Typ = Universal_Real
2729 or else It.Typ = Universal_Integer
2730 then
2731 -- Choose universal interpretation over any other
2733 T := It.Typ;
2734 exit;
2735 end if;
2736 end if;
2738 Get_Next_Interp (Index, It);
2739 end loop;
2740 end if;
2742 if Is_Integer_Type (T) then
2743 Resolve (E, T);
2744 Set_Etype (Id, Universal_Integer);
2745 Set_Ekind (Id, E_Named_Integer);
2747 elsif Is_Real_Type (T) then
2749 -- Because the real value is converted to universal_real, this is a
2750 -- legal context for a universal fixed expression.
2752 if T = Universal_Fixed then
2753 declare
2754 Loc : constant Source_Ptr := Sloc (N);
2755 Conv : constant Node_Id := Make_Type_Conversion (Loc,
2756 Subtype_Mark =>
2757 New_Occurrence_Of (Universal_Real, Loc),
2758 Expression => Relocate_Node (E));
2760 begin
2761 Rewrite (E, Conv);
2762 Analyze (E);
2763 end;
2765 elsif T = Any_Fixed then
2766 Error_Msg_N ("illegal context for mixed mode operation", E);
2768 -- Expression is of the form : universal_fixed * integer. Try to
2769 -- resolve as universal_real.
2771 T := Universal_Real;
2772 Set_Etype (E, T);
2773 end if;
2775 Resolve (E, T);
2776 Set_Etype (Id, Universal_Real);
2777 Set_Ekind (Id, E_Named_Real);
2779 else
2780 Wrong_Type (E, Any_Numeric);
2781 Resolve (E, T);
2783 Set_Etype (Id, T);
2784 Set_Ekind (Id, E_Constant);
2785 Set_Never_Set_In_Source (Id, True);
2786 Set_Is_True_Constant (Id, True);
2787 return;
2788 end if;
2790 if Nkind_In (E, N_Integer_Literal, N_Real_Literal) then
2791 Set_Etype (E, Etype (Id));
2792 end if;
2794 if not Is_OK_Static_Expression (E) then
2795 Flag_Non_Static_Expr
2796 ("non-static expression used in number declaration!", E);
2797 Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
2798 Set_Etype (E, Any_Type);
2799 end if;
2800 end Analyze_Number_Declaration;
2802 --------------------------------
2803 -- Analyze_Object_Declaration --
2804 --------------------------------
2806 procedure Analyze_Object_Declaration (N : Node_Id) is
2807 Loc : constant Source_Ptr := Sloc (N);
2808 Id : constant Entity_Id := Defining_Identifier (N);
2809 T : Entity_Id;
2810 Act_T : Entity_Id;
2812 E : Node_Id := Expression (N);
2813 -- E is set to Expression (N) throughout this routine. When
2814 -- Expression (N) is modified, E is changed accordingly.
2816 Prev_Entity : Entity_Id := Empty;
2818 function Count_Tasks (T : Entity_Id) return Uint;
2819 -- This function is called when a non-generic library level object of a
2820 -- task type is declared. Its function is to count the static number of
2821 -- tasks declared within the type (it is only called if Has_Tasks is set
2822 -- for T). As a side effect, if an array of tasks with non-static bounds
2823 -- or a variant record type is encountered, Check_Restrictions is called
2824 -- indicating the count is unknown.
2826 -----------------
2827 -- Count_Tasks --
2828 -----------------
2830 function Count_Tasks (T : Entity_Id) return Uint is
2831 C : Entity_Id;
2832 X : Node_Id;
2833 V : Uint;
2835 begin
2836 if Is_Task_Type (T) then
2837 return Uint_1;
2839 elsif Is_Record_Type (T) then
2840 if Has_Discriminants (T) then
2841 Check_Restriction (Max_Tasks, N);
2842 return Uint_0;
2844 else
2845 V := Uint_0;
2846 C := First_Component (T);
2847 while Present (C) loop
2848 V := V + Count_Tasks (Etype (C));
2849 Next_Component (C);
2850 end loop;
2852 return V;
2853 end if;
2855 elsif Is_Array_Type (T) then
2856 X := First_Index (T);
2857 V := Count_Tasks (Component_Type (T));
2858 while Present (X) loop
2859 C := Etype (X);
2861 if not Is_Static_Subtype (C) then
2862 Check_Restriction (Max_Tasks, N);
2863 return Uint_0;
2864 else
2865 V := V * (UI_Max (Uint_0,
2866 Expr_Value (Type_High_Bound (C)) -
2867 Expr_Value (Type_Low_Bound (C)) + Uint_1));
2868 end if;
2870 Next_Index (X);
2871 end loop;
2873 return V;
2875 else
2876 return Uint_0;
2877 end if;
2878 end Count_Tasks;
2880 -- Start of processing for Analyze_Object_Declaration
2882 begin
2883 -- There are three kinds of implicit types generated by an
2884 -- object declaration:
2886 -- 1. Those generated by the original Object Definition
2888 -- 2. Those generated by the Expression
2890 -- 3. Those used to constrain the Object Definition with the
2891 -- expression constraints when the definition is unconstrained.
2893 -- They must be generated in this order to avoid order of elaboration
2894 -- issues. Thus the first step (after entering the name) is to analyze
2895 -- the object definition.
2897 if Constant_Present (N) then
2898 Prev_Entity := Current_Entity_In_Scope (Id);
2900 if Present (Prev_Entity)
2901 and then
2903 -- If the homograph is an implicit subprogram, it is overridden
2904 -- by the current declaration.
2906 ((Is_Overloadable (Prev_Entity)
2907 and then Is_Inherited_Operation (Prev_Entity))
2909 -- The current object is a discriminal generated for an entry
2910 -- family index. Even though the index is a constant, in this
2911 -- particular context there is no true constant redeclaration.
2912 -- Enter_Name will handle the visibility.
2914 or else
2915 (Is_Discriminal (Id)
2916 and then Ekind (Discriminal_Link (Id)) =
2917 E_Entry_Index_Parameter)
2919 -- The current object is the renaming for a generic declared
2920 -- within the instance.
2922 or else
2923 (Ekind (Prev_Entity) = E_Package
2924 and then Nkind (Parent (Prev_Entity)) =
2925 N_Package_Renaming_Declaration
2926 and then not Comes_From_Source (Prev_Entity)
2927 and then Is_Generic_Instance (Renamed_Entity (Prev_Entity))))
2928 then
2929 Prev_Entity := Empty;
2930 end if;
2931 end if;
2933 if Present (Prev_Entity) then
2934 Constant_Redeclaration (Id, N, T);
2936 Generate_Reference (Prev_Entity, Id, 'c');
2937 Set_Completion_Referenced (Id);
2939 if Error_Posted (N) then
2941 -- Type mismatch or illegal redeclaration, Do not analyze
2942 -- expression to avoid cascaded errors.
2944 T := Find_Type_Of_Object (Object_Definition (N), N);
2945 Set_Etype (Id, T);
2946 Set_Ekind (Id, E_Variable);
2947 goto Leave;
2948 end if;
2950 -- In the normal case, enter identifier at the start to catch premature
2951 -- usage in the initialization expression.
2953 else
2954 Generate_Definition (Id);
2955 Enter_Name (Id);
2957 Mark_Coextensions (N, Object_Definition (N));
2959 T := Find_Type_Of_Object (Object_Definition (N), N);
2961 if Nkind (Object_Definition (N)) = N_Access_Definition
2962 and then Present
2963 (Access_To_Subprogram_Definition (Object_Definition (N)))
2964 and then Protected_Present
2965 (Access_To_Subprogram_Definition (Object_Definition (N)))
2966 then
2967 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
2968 end if;
2970 if Error_Posted (Id) then
2971 Set_Etype (Id, T);
2972 Set_Ekind (Id, E_Variable);
2973 goto Leave;
2974 end if;
2975 end if;
2977 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
2978 -- out some static checks
2980 if Ada_Version >= Ada_2005
2981 and then Can_Never_Be_Null (T)
2982 then
2983 -- In case of aggregates we must also take care of the correct
2984 -- initialization of nested aggregates bug this is done at the
2985 -- point of the analysis of the aggregate (see sem_aggr.adb)
2987 if Present (Expression (N))
2988 and then Nkind (Expression (N)) = N_Aggregate
2989 then
2990 null;
2992 else
2993 declare
2994 Save_Typ : constant Entity_Id := Etype (Id);
2995 begin
2996 Set_Etype (Id, T); -- Temp. decoration for static checks
2997 Null_Exclusion_Static_Checks (N);
2998 Set_Etype (Id, Save_Typ);
2999 end;
3000 end if;
3001 end if;
3003 -- Object is marked pure if it is in a pure scope
3005 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3007 -- If deferred constant, make sure context is appropriate. We detect
3008 -- a deferred constant as a constant declaration with no expression.
3009 -- A deferred constant can appear in a package body if its completion
3010 -- is by means of an interface pragma.
3012 if Constant_Present (N)
3013 and then No (E)
3014 then
3015 -- A deferred constant may appear in the declarative part of the
3016 -- following constructs:
3018 -- blocks
3019 -- entry bodies
3020 -- extended return statements
3021 -- package specs
3022 -- package bodies
3023 -- subprogram bodies
3024 -- task bodies
3026 -- When declared inside a package spec, a deferred constant must be
3027 -- completed by a full constant declaration or pragma Import. In all
3028 -- other cases, the only proper completion is pragma Import. Extended
3029 -- return statements are flagged as invalid contexts because they do
3030 -- not have a declarative part and so cannot accommodate the pragma.
3032 if Ekind (Current_Scope) = E_Return_Statement then
3033 Error_Msg_N
3034 ("invalid context for deferred constant declaration (RM 7.4)",
3036 Error_Msg_N
3037 ("\declaration requires an initialization expression",
3039 Set_Constant_Present (N, False);
3041 -- In Ada 83, deferred constant must be of private type
3043 elsif not Is_Private_Type (T) then
3044 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
3045 Error_Msg_N
3046 ("(Ada 83) deferred constant must be private type", N);
3047 end if;
3048 end if;
3050 -- If not a deferred constant, then object declaration freezes its type
3052 else
3053 Check_Fully_Declared (T, N);
3054 Freeze_Before (N, T);
3055 end if;
3057 -- If the object was created by a constrained array definition, then
3058 -- set the link in both the anonymous base type and anonymous subtype
3059 -- that are built to represent the array type to point to the object.
3061 if Nkind (Object_Definition (Declaration_Node (Id))) =
3062 N_Constrained_Array_Definition
3063 then
3064 Set_Related_Array_Object (T, Id);
3065 Set_Related_Array_Object (Base_Type (T), Id);
3066 end if;
3068 -- Special checks for protected objects not at library level
3070 if Is_Protected_Type (T)
3071 and then not Is_Library_Level_Entity (Id)
3072 then
3073 Check_Restriction (No_Local_Protected_Objects, Id);
3075 -- Protected objects with interrupt handlers must be at library level
3077 -- Ada 2005: this test is not needed (and the corresponding clause
3078 -- in the RM is removed) because accessibility checks are sufficient
3079 -- to make handlers not at the library level illegal.
3081 if Has_Interrupt_Handler (T)
3082 and then Ada_Version < Ada_2005
3083 then
3084 Error_Msg_N
3085 ("interrupt object can only be declared at library level", Id);
3086 end if;
3087 end if;
3089 -- The actual subtype of the object is the nominal subtype, unless
3090 -- the nominal one is unconstrained and obtained from the expression.
3092 Act_T := T;
3094 -- These checks should be performed before the initialization expression
3095 -- is considered, so that the Object_Definition node is still the same
3096 -- as in source code.
3098 -- In SPARK, the nominal subtype shall be given by a subtype mark and
3099 -- shall not be unconstrained. (The only exception to this is the
3100 -- admission of declarations of constants of type String.)
3102 if not
3103 Nkind_In (Object_Definition (N), N_Identifier, N_Expanded_Name)
3104 then
3105 Check_SPARK_Restriction
3106 ("subtype mark required", Object_Definition (N));
3108 elsif Is_Array_Type (T)
3109 and then not Is_Constrained (T)
3110 and then T /= Standard_String
3111 then
3112 Check_SPARK_Restriction
3113 ("subtype mark of constrained type expected",
3114 Object_Definition (N));
3115 end if;
3117 -- There are no aliased objects in SPARK
3119 if Aliased_Present (N) then
3120 Check_SPARK_Restriction ("aliased object is not allowed", N);
3121 end if;
3123 -- Process initialization expression if present and not in error
3125 if Present (E) and then E /= Error then
3127 -- Generate an error in case of CPP class-wide object initialization.
3128 -- Required because otherwise the expansion of the class-wide
3129 -- assignment would try to use 'size to initialize the object
3130 -- (primitive that is not available in CPP tagged types).
3132 if Is_Class_Wide_Type (Act_T)
3133 and then
3134 (Is_CPP_Class (Root_Type (Etype (Act_T)))
3135 or else
3136 (Present (Full_View (Root_Type (Etype (Act_T))))
3137 and then
3138 Is_CPP_Class (Full_View (Root_Type (Etype (Act_T))))))
3139 then
3140 Error_Msg_N
3141 ("predefined assignment not available for 'C'P'P tagged types",
3143 end if;
3145 Mark_Coextensions (N, E);
3146 Analyze (E);
3148 -- In case of errors detected in the analysis of the expression,
3149 -- decorate it with the expected type to avoid cascaded errors
3151 if No (Etype (E)) then
3152 Set_Etype (E, T);
3153 end if;
3155 -- If an initialization expression is present, then we set the
3156 -- Is_True_Constant flag. It will be reset if this is a variable
3157 -- and it is indeed modified.
3159 Set_Is_True_Constant (Id, True);
3161 -- If we are analyzing a constant declaration, set its completion
3162 -- flag after analyzing and resolving the expression.
3164 if Constant_Present (N) then
3165 Set_Has_Completion (Id);
3166 end if;
3168 -- Set type and resolve (type may be overridden later on)
3170 Set_Etype (Id, T);
3171 Resolve (E, T);
3173 -- No further action needed if E is a call to an inlined function
3174 -- which returns an unconstrained type and it has been expanded into
3175 -- a procedure call. In that case N has been replaced by an object
3176 -- declaration without initializing expression and it has been
3177 -- analyzed (see Expand_Inlined_Call).
3179 if Debug_Flag_Dot_K
3180 and then Expander_Active
3181 and then Nkind (E) = N_Function_Call
3182 and then Nkind (Name (E)) in N_Has_Entity
3183 and then Is_Inlined (Entity (Name (E)))
3184 and then not Is_Constrained (Etype (E))
3185 and then Analyzed (N)
3186 and then No (Expression (N))
3187 then
3188 return;
3189 end if;
3191 -- If E is null and has been replaced by an N_Raise_Constraint_Error
3192 -- node (which was marked already-analyzed), we need to set the type
3193 -- to something other than Any_Access in order to keep gigi happy.
3195 if Etype (E) = Any_Access then
3196 Set_Etype (E, T);
3197 end if;
3199 -- If the object is an access to variable, the initialization
3200 -- expression cannot be an access to constant.
3202 if Is_Access_Type (T)
3203 and then not Is_Access_Constant (T)
3204 and then Is_Access_Type (Etype (E))
3205 and then Is_Access_Constant (Etype (E))
3206 then
3207 Error_Msg_N
3208 ("access to variable cannot be initialized "
3209 & "with an access-to-constant expression", E);
3210 end if;
3212 if not Assignment_OK (N) then
3213 Check_Initialization (T, E);
3214 end if;
3216 Check_Unset_Reference (E);
3218 -- If this is a variable, then set current value. If this is a
3219 -- declared constant of a scalar type with a static expression,
3220 -- indicate that it is always valid.
3222 if not Constant_Present (N) then
3223 if Compile_Time_Known_Value (E) then
3224 Set_Current_Value (Id, E);
3225 end if;
3227 elsif Is_Scalar_Type (T)
3228 and then Is_OK_Static_Expression (E)
3229 then
3230 Set_Is_Known_Valid (Id);
3231 end if;
3233 -- Deal with setting of null flags
3235 if Is_Access_Type (T) then
3236 if Known_Non_Null (E) then
3237 Set_Is_Known_Non_Null (Id, True);
3238 elsif Known_Null (E)
3239 and then not Can_Never_Be_Null (Id)
3240 then
3241 Set_Is_Known_Null (Id, True);
3242 end if;
3243 end if;
3245 -- Check incorrect use of dynamically tagged expressions.
3247 if Is_Tagged_Type (T) then
3248 Check_Dynamically_Tagged_Expression
3249 (Expr => E,
3250 Typ => T,
3251 Related_Nod => N);
3252 end if;
3254 Apply_Scalar_Range_Check (E, T);
3255 Apply_Static_Length_Check (E, T);
3257 if Nkind (Original_Node (N)) = N_Object_Declaration
3258 and then Comes_From_Source (Original_Node (N))
3260 -- Only call test if needed
3262 and then Restriction_Check_Required (SPARK)
3263 and then not Is_SPARK_Initialization_Expr (E)
3264 then
3265 Check_SPARK_Restriction
3266 ("initialization expression is not appropriate", E);
3267 end if;
3268 end if;
3270 -- If the No_Streams restriction is set, check that the type of the
3271 -- object is not, and does not contain, any subtype derived from
3272 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
3273 -- Has_Stream just for efficiency reasons. There is no point in
3274 -- spending time on a Has_Stream check if the restriction is not set.
3276 if Restriction_Check_Required (No_Streams) then
3277 if Has_Stream (T) then
3278 Check_Restriction (No_Streams, N);
3279 end if;
3280 end if;
3282 -- Deal with predicate check before we start to do major rewriting.
3283 -- it is OK to initialize and then check the initialized value, since
3284 -- the object goes out of scope if we get a predicate failure. Note
3285 -- that we do this in the analyzer and not the expander because the
3286 -- analyzer does some substantial rewriting in some cases.
3288 -- We need a predicate check if the type has predicates, and if either
3289 -- there is an initializing expression, or for default initialization
3290 -- when we have at least one case of an explicit default initial value.
3292 if not Suppress_Assignment_Checks (N)
3293 and then Present (Predicate_Function (T))
3294 and then
3295 (Present (E)
3296 or else
3297 Is_Partially_Initialized_Type (T, Include_Implicit => False))
3298 then
3299 Insert_After (N,
3300 Make_Predicate_Check (T, New_Occurrence_Of (Id, Loc)));
3301 end if;
3303 -- Case of unconstrained type
3305 if Is_Indefinite_Subtype (T) then
3307 -- In SPARK, a declaration of unconstrained type is allowed
3308 -- only for constants of type string.
3310 if Is_String_Type (T) and then not Constant_Present (N) then
3311 Check_SPARK_Restriction
3312 ("declaration of object of unconstrained type not allowed",
3314 end if;
3316 -- Nothing to do in deferred constant case
3318 if Constant_Present (N) and then No (E) then
3319 null;
3321 -- Case of no initialization present
3323 elsif No (E) then
3324 if No_Initialization (N) then
3325 null;
3327 elsif Is_Class_Wide_Type (T) then
3328 Error_Msg_N
3329 ("initialization required in class-wide declaration ", N);
3331 else
3332 Error_Msg_N
3333 ("unconstrained subtype not allowed (need initialization)",
3334 Object_Definition (N));
3336 if Is_Record_Type (T) and then Has_Discriminants (T) then
3337 Error_Msg_N
3338 ("\provide initial value or explicit discriminant values",
3339 Object_Definition (N));
3341 Error_Msg_NE
3342 ("\or give default discriminant values for type&",
3343 Object_Definition (N), T);
3345 elsif Is_Array_Type (T) then
3346 Error_Msg_N
3347 ("\provide initial value or explicit array bounds",
3348 Object_Definition (N));
3349 end if;
3350 end if;
3352 -- Case of initialization present but in error. Set initial
3353 -- expression as absent (but do not make above complaints)
3355 elsif E = Error then
3356 Set_Expression (N, Empty);
3357 E := Empty;
3359 -- Case of initialization present
3361 else
3362 -- Check restrictions in Ada 83
3364 if not Constant_Present (N) then
3366 -- Unconstrained variables not allowed in Ada 83 mode
3368 if Ada_Version = Ada_83
3369 and then Comes_From_Source (Object_Definition (N))
3370 then
3371 Error_Msg_N
3372 ("(Ada 83) unconstrained variable not allowed",
3373 Object_Definition (N));
3374 end if;
3375 end if;
3377 -- Now we constrain the variable from the initializing expression
3379 -- If the expression is an aggregate, it has been expanded into
3380 -- individual assignments. Retrieve the actual type from the
3381 -- expanded construct.
3383 if Is_Array_Type (T)
3384 and then No_Initialization (N)
3385 and then Nkind (Original_Node (E)) = N_Aggregate
3386 then
3387 Act_T := Etype (E);
3389 -- In case of class-wide interface object declarations we delay
3390 -- the generation of the equivalent record type declarations until
3391 -- its expansion because there are cases in they are not required.
3393 elsif Is_Interface (T) then
3394 null;
3396 else
3397 Expand_Subtype_From_Expr (N, T, Object_Definition (N), E);
3398 Act_T := Find_Type_Of_Object (Object_Definition (N), N);
3399 end if;
3401 Set_Is_Constr_Subt_For_U_Nominal (Act_T);
3403 if Aliased_Present (N) then
3404 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
3405 end if;
3407 Freeze_Before (N, Act_T);
3408 Freeze_Before (N, T);
3409 end if;
3411 elsif Is_Array_Type (T)
3412 and then No_Initialization (N)
3413 and then Nkind (Original_Node (E)) = N_Aggregate
3414 then
3415 if not Is_Entity_Name (Object_Definition (N)) then
3416 Act_T := Etype (E);
3417 Check_Compile_Time_Size (Act_T);
3419 if Aliased_Present (N) then
3420 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
3421 end if;
3422 end if;
3424 -- When the given object definition and the aggregate are specified
3425 -- independently, and their lengths might differ do a length check.
3426 -- This cannot happen if the aggregate is of the form (others =>...)
3428 if not Is_Constrained (T) then
3429 null;
3431 elsif Nkind (E) = N_Raise_Constraint_Error then
3433 -- Aggregate is statically illegal. Place back in declaration
3435 Set_Expression (N, E);
3436 Set_No_Initialization (N, False);
3438 elsif T = Etype (E) then
3439 null;
3441 elsif Nkind (E) = N_Aggregate
3442 and then Present (Component_Associations (E))
3443 and then Present (Choices (First (Component_Associations (E))))
3444 and then Nkind (First
3445 (Choices (First (Component_Associations (E))))) = N_Others_Choice
3446 then
3447 null;
3449 else
3450 Apply_Length_Check (E, T);
3451 end if;
3453 -- If the type is limited unconstrained with defaulted discriminants and
3454 -- there is no expression, then the object is constrained by the
3455 -- defaults, so it is worthwhile building the corresponding subtype.
3457 elsif (Is_Limited_Record (T) or else Is_Concurrent_Type (T))
3458 and then not Is_Constrained (T)
3459 and then Has_Discriminants (T)
3460 then
3461 if No (E) then
3462 Act_T := Build_Default_Subtype (T, N);
3463 else
3464 -- Ada 2005: a limited object may be initialized by means of an
3465 -- aggregate. If the type has default discriminants it has an
3466 -- unconstrained nominal type, Its actual subtype will be obtained
3467 -- from the aggregate, and not from the default discriminants.
3469 Act_T := Etype (E);
3470 end if;
3472 Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
3474 elsif Present (Underlying_Type (T))
3475 and then not Is_Constrained (Underlying_Type (T))
3476 and then Has_Discriminants (Underlying_Type (T))
3477 and then Nkind (E) = N_Function_Call
3478 and then Constant_Present (N)
3479 then
3480 -- The back-end has problems with constants of a discriminated type
3481 -- with defaults, if the initial value is a function call. We
3482 -- generate an intermediate temporary for the result of the call.
3483 -- It is unclear why this should make it acceptable to gcc. ???
3485 Remove_Side_Effects (E);
3487 -- If this is a constant declaration of an unconstrained type and
3488 -- the initialization is an aggregate, we can use the subtype of the
3489 -- aggregate for the declared entity because it is immutable.
3491 elsif not Is_Constrained (T)
3492 and then Has_Discriminants (T)
3493 and then Constant_Present (N)
3494 and then not Has_Unchecked_Union (T)
3495 and then Nkind (E) = N_Aggregate
3496 then
3497 Act_T := Etype (E);
3498 end if;
3500 -- Check No_Wide_Characters restriction
3502 Check_Wide_Character_Restriction (T, Object_Definition (N));
3504 -- Indicate this is not set in source. Certainly true for constants, and
3505 -- true for variables so far (will be reset for a variable if and when
3506 -- we encounter a modification in the source).
3508 Set_Never_Set_In_Source (Id, True);
3510 -- Now establish the proper kind and type of the object
3512 if Constant_Present (N) then
3513 Set_Ekind (Id, E_Constant);
3514 Set_Is_True_Constant (Id, True);
3516 else
3517 Set_Ekind (Id, E_Variable);
3519 -- A variable is set as shared passive if it appears in a shared
3520 -- passive package, and is at the outer level. This is not done for
3521 -- entities generated during expansion, because those are always
3522 -- manipulated locally.
3524 if Is_Shared_Passive (Current_Scope)
3525 and then Is_Library_Level_Entity (Id)
3526 and then Comes_From_Source (Id)
3527 then
3528 Set_Is_Shared_Passive (Id);
3529 Check_Shared_Var (Id, T, N);
3530 end if;
3532 -- Set Has_Initial_Value if initializing expression present. Note
3533 -- that if there is no initializing expression, we leave the state
3534 -- of this flag unchanged (usually it will be False, but notably in
3535 -- the case of exception choice variables, it will already be true).
3537 if Present (E) then
3538 Set_Has_Initial_Value (Id, True);
3539 end if;
3540 end if;
3542 -- Initialize alignment and size and capture alignment setting
3544 Init_Alignment (Id);
3545 Init_Esize (Id);
3546 Set_Optimize_Alignment_Flags (Id);
3548 -- Deal with aliased case
3550 if Aliased_Present (N) then
3551 Set_Is_Aliased (Id);
3553 -- If the object is aliased and the type is unconstrained with
3554 -- defaulted discriminants and there is no expression, then the
3555 -- object is constrained by the defaults, so it is worthwhile
3556 -- building the corresponding subtype.
3558 -- Ada 2005 (AI-363): If the aliased object is discriminated and
3559 -- unconstrained, then only establish an actual subtype if the
3560 -- nominal subtype is indefinite. In definite cases the object is
3561 -- unconstrained in Ada 2005.
3563 if No (E)
3564 and then Is_Record_Type (T)
3565 and then not Is_Constrained (T)
3566 and then Has_Discriminants (T)
3567 and then (Ada_Version < Ada_2005 or else Is_Indefinite_Subtype (T))
3568 then
3569 Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
3570 end if;
3571 end if;
3573 -- Now we can set the type of the object
3575 Set_Etype (Id, Act_T);
3577 -- Object is marked to be treated as volatile if type is volatile and
3578 -- we clear the Current_Value setting that may have been set above.
3580 if Treat_As_Volatile (Etype (Id)) then
3581 Set_Treat_As_Volatile (Id);
3582 Set_Current_Value (Id, Empty);
3583 end if;
3585 -- Deal with controlled types
3587 if Has_Controlled_Component (Etype (Id))
3588 or else Is_Controlled (Etype (Id))
3589 then
3590 if not Is_Library_Level_Entity (Id) then
3591 Check_Restriction (No_Nested_Finalization, N);
3592 else
3593 Validate_Controlled_Object (Id);
3594 end if;
3595 end if;
3597 if Has_Task (Etype (Id)) then
3598 Check_Restriction (No_Tasking, N);
3600 -- Deal with counting max tasks
3602 -- Nothing to do if inside a generic
3604 if Inside_A_Generic then
3605 null;
3607 -- If library level entity, then count tasks
3609 elsif Is_Library_Level_Entity (Id) then
3610 Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
3612 -- If not library level entity, then indicate we don't know max
3613 -- tasks and also check task hierarchy restriction and blocking
3614 -- operation (since starting a task is definitely blocking!)
3616 else
3617 Check_Restriction (Max_Tasks, N);
3618 Check_Restriction (No_Task_Hierarchy, N);
3619 Check_Potentially_Blocking_Operation (N);
3620 end if;
3622 -- A rather specialized test. If we see two tasks being declared
3623 -- of the same type in the same object declaration, and the task
3624 -- has an entry with an address clause, we know that program error
3625 -- will be raised at run time since we can't have two tasks with
3626 -- entries at the same address.
3628 if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
3629 declare
3630 E : Entity_Id;
3632 begin
3633 E := First_Entity (Etype (Id));
3634 while Present (E) loop
3635 if Ekind (E) = E_Entry
3636 and then Present (Get_Attribute_Definition_Clause
3637 (E, Attribute_Address))
3638 then
3639 Error_Msg_N
3640 ("?more than one task with same entry address", N);
3641 Error_Msg_N
3642 ("\?Program_Error will be raised at run time", N);
3643 Insert_Action (N,
3644 Make_Raise_Program_Error (Loc,
3645 Reason => PE_Duplicated_Entry_Address));
3646 exit;
3647 end if;
3649 Next_Entity (E);
3650 end loop;
3651 end;
3652 end if;
3653 end if;
3655 -- Some simple constant-propagation: if the expression is a constant
3656 -- string initialized with a literal, share the literal. This avoids
3657 -- a run-time copy.
3659 if Present (E)
3660 and then Is_Entity_Name (E)
3661 and then Ekind (Entity (E)) = E_Constant
3662 and then Base_Type (Etype (E)) = Standard_String
3663 then
3664 declare
3665 Val : constant Node_Id := Constant_Value (Entity (E));
3666 begin
3667 if Present (Val)
3668 and then Nkind (Val) = N_String_Literal
3669 then
3670 Rewrite (E, New_Copy (Val));
3671 end if;
3672 end;
3673 end if;
3675 -- Another optimization: if the nominal subtype is unconstrained and
3676 -- the expression is a function call that returns an unconstrained
3677 -- type, rewrite the declaration as a renaming of the result of the
3678 -- call. The exceptions below are cases where the copy is expected,
3679 -- either by the back end (Aliased case) or by the semantics, as for
3680 -- initializing controlled types or copying tags for classwide types.
3682 if Present (E)
3683 and then Nkind (E) = N_Explicit_Dereference
3684 and then Nkind (Original_Node (E)) = N_Function_Call
3685 and then not Is_Library_Level_Entity (Id)
3686 and then not Is_Constrained (Underlying_Type (T))
3687 and then not Is_Aliased (Id)
3688 and then not Is_Class_Wide_Type (T)
3689 and then not Is_Controlled (T)
3690 and then not Has_Controlled_Component (Base_Type (T))
3691 and then Expander_Active
3692 then
3693 Rewrite (N,
3694 Make_Object_Renaming_Declaration (Loc,
3695 Defining_Identifier => Id,
3696 Access_Definition => Empty,
3697 Subtype_Mark => New_Occurrence_Of
3698 (Base_Type (Etype (Id)), Loc),
3699 Name => E));
3701 Set_Renamed_Object (Id, E);
3703 -- Force generation of debugging information for the constant and for
3704 -- the renamed function call.
3706 Set_Debug_Info_Needed (Id);
3707 Set_Debug_Info_Needed (Entity (Prefix (E)));
3708 end if;
3710 if Present (Prev_Entity)
3711 and then Is_Frozen (Prev_Entity)
3712 and then not Error_Posted (Id)
3713 then
3714 Error_Msg_N ("full constant declaration appears too late", N);
3715 end if;
3717 Check_Eliminated (Id);
3719 -- Deal with setting In_Private_Part flag if in private part
3721 if Ekind (Scope (Id)) = E_Package
3722 and then In_Private_Part (Scope (Id))
3723 then
3724 Set_In_Private_Part (Id);
3725 end if;
3727 -- Check for violation of No_Local_Timing_Events
3729 if Restriction_Check_Required (No_Local_Timing_Events)
3730 and then not Is_Library_Level_Entity (Id)
3731 and then Is_RTE (Etype (Id), RE_Timing_Event)
3732 then
3733 Check_Restriction (No_Local_Timing_Events, N);
3734 end if;
3736 <<Leave>>
3737 if Has_Aspects (N) then
3738 Analyze_Aspect_Specifications (N, Id);
3739 end if;
3741 Analyze_Dimension (N);
3742 end Analyze_Object_Declaration;
3744 ---------------------------
3745 -- Analyze_Others_Choice --
3746 ---------------------------
3748 -- Nothing to do for the others choice node itself, the semantic analysis
3749 -- of the others choice will occur as part of the processing of the parent
3751 procedure Analyze_Others_Choice (N : Node_Id) is
3752 pragma Warnings (Off, N);
3753 begin
3754 null;
3755 end Analyze_Others_Choice;
3757 -------------------------------------------
3758 -- Analyze_Private_Extension_Declaration --
3759 -------------------------------------------
3761 procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
3762 T : constant Entity_Id := Defining_Identifier (N);
3763 Indic : constant Node_Id := Subtype_Indication (N);
3764 Parent_Type : Entity_Id;
3765 Parent_Base : Entity_Id;
3767 begin
3768 -- Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
3770 if Is_Non_Empty_List (Interface_List (N)) then
3771 declare
3772 Intf : Node_Id;
3773 T : Entity_Id;
3775 begin
3776 Intf := First (Interface_List (N));
3777 while Present (Intf) loop
3778 T := Find_Type_Of_Subtype_Indic (Intf);
3780 Diagnose_Interface (Intf, T);
3781 Next (Intf);
3782 end loop;
3783 end;
3784 end if;
3786 Generate_Definition (T);
3788 -- For other than Ada 2012, just enter the name in the current scope
3790 if Ada_Version < Ada_2012 then
3791 Enter_Name (T);
3793 -- Ada 2012 (AI05-0162): Enter the name in the current scope handling
3794 -- case of private type that completes an incomplete type.
3796 else
3797 declare
3798 Prev : Entity_Id;
3800 begin
3801 Prev := Find_Type_Name (N);
3803 pragma Assert (Prev = T
3804 or else (Ekind (Prev) = E_Incomplete_Type
3805 and then Present (Full_View (Prev))
3806 and then Full_View (Prev) = T));
3807 end;
3808 end if;
3810 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
3811 Parent_Base := Base_Type (Parent_Type);
3813 if Parent_Type = Any_Type
3814 or else Etype (Parent_Type) = Any_Type
3815 then
3816 Set_Ekind (T, Ekind (Parent_Type));
3817 Set_Etype (T, Any_Type);
3818 goto Leave;
3820 elsif not Is_Tagged_Type (Parent_Type) then
3821 Error_Msg_N
3822 ("parent of type extension must be a tagged type ", Indic);
3823 goto Leave;
3825 elsif Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
3826 Error_Msg_N ("premature derivation of incomplete type", Indic);
3827 goto Leave;
3829 elsif Is_Concurrent_Type (Parent_Type) then
3830 Error_Msg_N
3831 ("parent type of a private extension cannot be "
3832 & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
3834 Set_Etype (T, Any_Type);
3835 Set_Ekind (T, E_Limited_Private_Type);
3836 Set_Private_Dependents (T, New_Elmt_List);
3837 Set_Error_Posted (T);
3838 goto Leave;
3839 end if;
3841 -- Perhaps the parent type should be changed to the class-wide type's
3842 -- specific type in this case to prevent cascading errors ???
3844 if Is_Class_Wide_Type (Parent_Type) then
3845 Error_Msg_N
3846 ("parent of type extension must not be a class-wide type", Indic);
3847 goto Leave;
3848 end if;
3850 if (not Is_Package_Or_Generic_Package (Current_Scope)
3851 and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
3852 or else In_Private_Part (Current_Scope)
3854 then
3855 Error_Msg_N ("invalid context for private extension", N);
3856 end if;
3858 -- Set common attributes
3860 Set_Is_Pure (T, Is_Pure (Current_Scope));
3861 Set_Scope (T, Current_Scope);
3862 Set_Ekind (T, E_Record_Type_With_Private);
3863 Init_Size_Align (T);
3865 Set_Etype (T, Parent_Base);
3866 Set_Has_Task (T, Has_Task (Parent_Base));
3868 Set_Convention (T, Convention (Parent_Type));
3869 Set_First_Rep_Item (T, First_Rep_Item (Parent_Type));
3870 Set_Is_First_Subtype (T);
3871 Make_Class_Wide_Type (T);
3873 if Unknown_Discriminants_Present (N) then
3874 Set_Discriminant_Constraint (T, No_Elist);
3875 end if;
3877 Build_Derived_Record_Type (N, Parent_Type, T);
3879 -- Propagate inherited invariant information. The new type has
3880 -- invariants, if the parent type has inheritable invariants,
3881 -- and these invariants can in turn be inherited.
3883 if Has_Inheritable_Invariants (Parent_Type) then
3884 Set_Has_Inheritable_Invariants (T);
3885 Set_Has_Invariants (T);
3886 end if;
3888 -- Ada 2005 (AI-443): Synchronized private extension or a rewritten
3889 -- synchronized formal derived type.
3891 if Ada_Version >= Ada_2005
3892 and then Synchronized_Present (N)
3893 then
3894 Set_Is_Limited_Record (T);
3896 -- Formal derived type case
3898 if Is_Generic_Type (T) then
3900 -- The parent must be a tagged limited type or a synchronized
3901 -- interface.
3903 if (not Is_Tagged_Type (Parent_Type)
3904 or else not Is_Limited_Type (Parent_Type))
3905 and then
3906 (not Is_Interface (Parent_Type)
3907 or else not Is_Synchronized_Interface (Parent_Type))
3908 then
3909 Error_Msg_NE ("parent type of & must be tagged limited " &
3910 "or synchronized", N, T);
3911 end if;
3913 -- The progenitors (if any) must be limited or synchronized
3914 -- interfaces.
3916 if Present (Interfaces (T)) then
3917 declare
3918 Iface : Entity_Id;
3919 Iface_Elmt : Elmt_Id;
3921 begin
3922 Iface_Elmt := First_Elmt (Interfaces (T));
3923 while Present (Iface_Elmt) loop
3924 Iface := Node (Iface_Elmt);
3926 if not Is_Limited_Interface (Iface)
3927 and then not Is_Synchronized_Interface (Iface)
3928 then
3929 Error_Msg_NE ("progenitor & must be limited " &
3930 "or synchronized", N, Iface);
3931 end if;
3933 Next_Elmt (Iface_Elmt);
3934 end loop;
3935 end;
3936 end if;
3938 -- Regular derived extension, the parent must be a limited or
3939 -- synchronized interface.
3941 else
3942 if not Is_Interface (Parent_Type)
3943 or else (not Is_Limited_Interface (Parent_Type)
3944 and then
3945 not Is_Synchronized_Interface (Parent_Type))
3946 then
3947 Error_Msg_NE
3948 ("parent type of & must be limited interface", N, T);
3949 end if;
3950 end if;
3952 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
3953 -- extension with a synchronized parent must be explicitly declared
3954 -- synchronized, because the full view will be a synchronized type.
3955 -- This must be checked before the check for limited types below,
3956 -- to ensure that types declared limited are not allowed to extend
3957 -- synchronized interfaces.
3959 elsif Is_Interface (Parent_Type)
3960 and then Is_Synchronized_Interface (Parent_Type)
3961 and then not Synchronized_Present (N)
3962 then
3963 Error_Msg_NE
3964 ("private extension of& must be explicitly synchronized",
3965 N, Parent_Type);
3967 elsif Limited_Present (N) then
3968 Set_Is_Limited_Record (T);
3970 if not Is_Limited_Type (Parent_Type)
3971 and then
3972 (not Is_Interface (Parent_Type)
3973 or else not Is_Limited_Interface (Parent_Type))
3974 then
3975 Error_Msg_NE ("parent type& of limited extension must be limited",
3976 N, Parent_Type);
3977 end if;
3978 end if;
3980 <<Leave>>
3981 if Has_Aspects (N) then
3982 Analyze_Aspect_Specifications (N, T);
3983 end if;
3984 end Analyze_Private_Extension_Declaration;
3986 ---------------------------------
3987 -- Analyze_Subtype_Declaration --
3988 ---------------------------------
3990 procedure Analyze_Subtype_Declaration
3991 (N : Node_Id;
3992 Skip : Boolean := False)
3994 Id : constant Entity_Id := Defining_Identifier (N);
3995 T : Entity_Id;
3996 R_Checks : Check_Result;
3998 begin
3999 Generate_Definition (Id);
4000 Set_Is_Pure (Id, Is_Pure (Current_Scope));
4001 Init_Size_Align (Id);
4003 -- The following guard condition on Enter_Name is to handle cases where
4004 -- the defining identifier has already been entered into the scope but
4005 -- the declaration as a whole needs to be analyzed.
4007 -- This case in particular happens for derived enumeration types. The
4008 -- derived enumeration type is processed as an inserted enumeration type
4009 -- declaration followed by a rewritten subtype declaration. The defining
4010 -- identifier, however, is entered into the name scope very early in the
4011 -- processing of the original type declaration and therefore needs to be
4012 -- avoided here, when the created subtype declaration is analyzed. (See
4013 -- Build_Derived_Types)
4015 -- This also happens when the full view of a private type is derived
4016 -- type with constraints. In this case the entity has been introduced
4017 -- in the private declaration.
4019 if Skip
4020 or else (Present (Etype (Id))
4021 and then (Is_Private_Type (Etype (Id))
4022 or else Is_Task_Type (Etype (Id))
4023 or else Is_Rewrite_Substitution (N)))
4024 then
4025 null;
4027 else
4028 Enter_Name (Id);
4029 end if;
4031 T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
4033 -- Class-wide equivalent types of records with unknown discriminants
4034 -- involve the generation of an itype which serves as the private view
4035 -- of a constrained record subtype. In such cases the base type of the
4036 -- current subtype we are processing is the private itype. Use the full
4037 -- of the private itype when decorating various attributes.
4039 if Is_Itype (T)
4040 and then Is_Private_Type (T)
4041 and then Present (Full_View (T))
4042 then
4043 T := Full_View (T);
4044 end if;
4046 -- Inherit common attributes
4048 Set_Is_Generic_Type (Id, Is_Generic_Type (Base_Type (T)));
4049 Set_Is_Volatile (Id, Is_Volatile (T));
4050 Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
4051 Set_Is_Atomic (Id, Is_Atomic (T));
4052 Set_Is_Ada_2005_Only (Id, Is_Ada_2005_Only (T));
4053 Set_Is_Ada_2012_Only (Id, Is_Ada_2012_Only (T));
4054 Set_Convention (Id, Convention (T));
4056 -- If ancestor has predicates then so does the subtype, and in addition
4057 -- we must delay the freeze to properly arrange predicate inheritance.
4059 -- The Ancestor_Type test is a big kludge, there seem to be cases in
4060 -- which T = ID, so the above tests and assignments do nothing???
4062 if Has_Predicates (T)
4063 or else (Present (Ancestor_Subtype (T))
4064 and then Has_Predicates (Ancestor_Subtype (T)))
4065 then
4066 Set_Has_Predicates (Id);
4067 Set_Has_Delayed_Freeze (Id);
4068 end if;
4070 -- Subtype of Boolean cannot have a constraint in SPARK
4072 if Is_Boolean_Type (T)
4073 and then Nkind (Subtype_Indication (N)) = N_Subtype_Indication
4074 then
4075 Check_SPARK_Restriction
4076 ("subtype of Boolean cannot have constraint", N);
4077 end if;
4079 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
4080 declare
4081 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
4082 One_Cstr : Node_Id;
4083 Low : Node_Id;
4084 High : Node_Id;
4086 begin
4087 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint then
4088 One_Cstr := First (Constraints (Cstr));
4089 while Present (One_Cstr) loop
4091 -- Index or discriminant constraint in SPARK must be a
4092 -- subtype mark.
4094 if not
4095 Nkind_In (One_Cstr, N_Identifier, N_Expanded_Name)
4096 then
4097 Check_SPARK_Restriction
4098 ("subtype mark required", One_Cstr);
4100 -- String subtype must have a lower bound of 1 in SPARK.
4101 -- Note that we do not need to test for the non-static case
4102 -- here, since that was already taken care of in
4103 -- Process_Range_Expr_In_Decl.
4105 elsif Base_Type (T) = Standard_String then
4106 Get_Index_Bounds (One_Cstr, Low, High);
4108 if Is_OK_Static_Expression (Low)
4109 and then Expr_Value (Low) /= 1
4110 then
4111 Check_SPARK_Restriction
4112 ("String subtype must have lower bound of 1", N);
4113 end if;
4114 end if;
4116 Next (One_Cstr);
4117 end loop;
4118 end if;
4119 end;
4120 end if;
4122 -- In the case where there is no constraint given in the subtype
4123 -- indication, Process_Subtype just returns the Subtype_Mark, so its
4124 -- semantic attributes must be established here.
4126 if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
4127 Set_Etype (Id, Base_Type (T));
4129 -- Subtype of unconstrained array without constraint is not allowed
4130 -- in SPARK.
4132 if Is_Array_Type (T)
4133 and then not Is_Constrained (T)
4134 then
4135 Check_SPARK_Restriction
4136 ("subtype of unconstrained array must have constraint", N);
4137 end if;
4139 case Ekind (T) is
4140 when Array_Kind =>
4141 Set_Ekind (Id, E_Array_Subtype);
4142 Copy_Array_Subtype_Attributes (Id, T);
4144 when Decimal_Fixed_Point_Kind =>
4145 Set_Ekind (Id, E_Decimal_Fixed_Point_Subtype);
4146 Set_Digits_Value (Id, Digits_Value (T));
4147 Set_Delta_Value (Id, Delta_Value (T));
4148 Set_Scale_Value (Id, Scale_Value (T));
4149 Set_Small_Value (Id, Small_Value (T));
4150 Set_Scalar_Range (Id, Scalar_Range (T));
4151 Set_Machine_Radix_10 (Id, Machine_Radix_10 (T));
4152 Set_Is_Constrained (Id, Is_Constrained (T));
4153 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4154 Set_RM_Size (Id, RM_Size (T));
4156 when Enumeration_Kind =>
4157 Set_Ekind (Id, E_Enumeration_Subtype);
4158 Set_First_Literal (Id, First_Literal (Base_Type (T)));
4159 Set_Scalar_Range (Id, Scalar_Range (T));
4160 Set_Is_Character_Type (Id, Is_Character_Type (T));
4161 Set_Is_Constrained (Id, Is_Constrained (T));
4162 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4163 Set_RM_Size (Id, RM_Size (T));
4165 when Ordinary_Fixed_Point_Kind =>
4166 Set_Ekind (Id, E_Ordinary_Fixed_Point_Subtype);
4167 Set_Scalar_Range (Id, Scalar_Range (T));
4168 Set_Small_Value (Id, Small_Value (T));
4169 Set_Delta_Value (Id, Delta_Value (T));
4170 Set_Is_Constrained (Id, Is_Constrained (T));
4171 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4172 Set_RM_Size (Id, RM_Size (T));
4174 when Float_Kind =>
4175 Set_Ekind (Id, E_Floating_Point_Subtype);
4176 Set_Scalar_Range (Id, Scalar_Range (T));
4177 Set_Digits_Value (Id, Digits_Value (T));
4178 Set_Is_Constrained (Id, Is_Constrained (T));
4180 when Signed_Integer_Kind =>
4181 Set_Ekind (Id, E_Signed_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 Modular_Integer_Kind =>
4188 Set_Ekind (Id, E_Modular_Integer_Subtype);
4189 Set_Scalar_Range (Id, Scalar_Range (T));
4190 Set_Is_Constrained (Id, Is_Constrained (T));
4191 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4192 Set_RM_Size (Id, RM_Size (T));
4194 when Class_Wide_Kind =>
4195 Set_Ekind (Id, E_Class_Wide_Subtype);
4196 Set_First_Entity (Id, First_Entity (T));
4197 Set_Last_Entity (Id, Last_Entity (T));
4198 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
4199 Set_Cloned_Subtype (Id, T);
4200 Set_Is_Tagged_Type (Id, True);
4201 Set_Has_Unknown_Discriminants
4202 (Id, True);
4204 if Ekind (T) = E_Class_Wide_Subtype then
4205 Set_Equivalent_Type (Id, Equivalent_Type (T));
4206 end if;
4208 when E_Record_Type | E_Record_Subtype =>
4209 Set_Ekind (Id, E_Record_Subtype);
4211 if Ekind (T) = E_Record_Subtype
4212 and then Present (Cloned_Subtype (T))
4213 then
4214 Set_Cloned_Subtype (Id, Cloned_Subtype (T));
4215 else
4216 Set_Cloned_Subtype (Id, T);
4217 end if;
4219 Set_First_Entity (Id, First_Entity (T));
4220 Set_Last_Entity (Id, Last_Entity (T));
4221 Set_Has_Discriminants (Id, Has_Discriminants (T));
4222 Set_Is_Constrained (Id, Is_Constrained (T));
4223 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
4224 Set_Has_Implicit_Dereference
4225 (Id, Has_Implicit_Dereference (T));
4226 Set_Has_Unknown_Discriminants
4227 (Id, Has_Unknown_Discriminants (T));
4229 if Has_Discriminants (T) then
4230 Set_Discriminant_Constraint
4231 (Id, Discriminant_Constraint (T));
4232 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
4234 elsif Has_Unknown_Discriminants (Id) then
4235 Set_Discriminant_Constraint (Id, No_Elist);
4236 end if;
4238 if Is_Tagged_Type (T) then
4239 Set_Is_Tagged_Type (Id);
4240 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
4241 Set_Direct_Primitive_Operations
4242 (Id, Direct_Primitive_Operations (T));
4243 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
4245 if Is_Interface (T) then
4246 Set_Is_Interface (Id);
4247 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
4248 end if;
4249 end if;
4251 when Private_Kind =>
4252 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
4253 Set_Has_Discriminants (Id, Has_Discriminants (T));
4254 Set_Is_Constrained (Id, Is_Constrained (T));
4255 Set_First_Entity (Id, First_Entity (T));
4256 Set_Last_Entity (Id, Last_Entity (T));
4257 Set_Private_Dependents (Id, New_Elmt_List);
4258 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
4259 Set_Has_Implicit_Dereference
4260 (Id, Has_Implicit_Dereference (T));
4261 Set_Has_Unknown_Discriminants
4262 (Id, Has_Unknown_Discriminants (T));
4263 Set_Known_To_Have_Preelab_Init
4264 (Id, Known_To_Have_Preelab_Init (T));
4266 if Is_Tagged_Type (T) then
4267 Set_Is_Tagged_Type (Id);
4268 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
4269 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
4270 Set_Direct_Primitive_Operations (Id,
4271 Direct_Primitive_Operations (T));
4272 end if;
4274 -- In general the attributes of the subtype of a private type
4275 -- are the attributes of the partial view of parent. However,
4276 -- the full view may be a discriminated type, and the subtype
4277 -- must share the discriminant constraint to generate correct
4278 -- calls to initialization procedures.
4280 if Has_Discriminants (T) then
4281 Set_Discriminant_Constraint
4282 (Id, Discriminant_Constraint (T));
4283 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
4285 elsif Present (Full_View (T))
4286 and then Has_Discriminants (Full_View (T))
4287 then
4288 Set_Discriminant_Constraint
4289 (Id, Discriminant_Constraint (Full_View (T)));
4290 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
4292 -- This would seem semantically correct, but apparently
4293 -- confuses the back-end. To be explained and checked with
4294 -- current version ???
4296 -- Set_Has_Discriminants (Id);
4297 end if;
4299 Prepare_Private_Subtype_Completion (Id, N);
4301 when Access_Kind =>
4302 Set_Ekind (Id, E_Access_Subtype);
4303 Set_Is_Constrained (Id, Is_Constrained (T));
4304 Set_Is_Access_Constant
4305 (Id, Is_Access_Constant (T));
4306 Set_Directly_Designated_Type
4307 (Id, Designated_Type (T));
4308 Set_Can_Never_Be_Null (Id, Can_Never_Be_Null (T));
4310 -- A Pure library_item must not contain the declaration of a
4311 -- named access type, except within a subprogram, generic
4312 -- subprogram, task unit, or protected unit, or if it has
4313 -- a specified Storage_Size of zero (RM05-10.2.1(15.4-15.5)).
4315 if Comes_From_Source (Id)
4316 and then In_Pure_Unit
4317 and then not In_Subprogram_Task_Protected_Unit
4318 and then not No_Pool_Assigned (Id)
4319 then
4320 Error_Msg_N
4321 ("named access types not allowed in pure unit", N);
4322 end if;
4324 when Concurrent_Kind =>
4325 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
4326 Set_Corresponding_Record_Type (Id,
4327 Corresponding_Record_Type (T));
4328 Set_First_Entity (Id, First_Entity (T));
4329 Set_First_Private_Entity (Id, First_Private_Entity (T));
4330 Set_Has_Discriminants (Id, Has_Discriminants (T));
4331 Set_Is_Constrained (Id, Is_Constrained (T));
4332 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
4333 Set_Last_Entity (Id, Last_Entity (T));
4335 if Has_Discriminants (T) then
4336 Set_Discriminant_Constraint (Id,
4337 Discriminant_Constraint (T));
4338 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
4339 end if;
4341 when E_Incomplete_Type =>
4342 if Ada_Version >= Ada_2005 then
4344 -- In Ada 2005 an incomplete type can be explicitly tagged:
4345 -- propagate indication.
4347 Set_Ekind (Id, E_Incomplete_Subtype);
4348 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
4349 Set_Private_Dependents (Id, New_Elmt_List);
4351 -- Ada 2005 (AI-412): Decorate an incomplete subtype of an
4352 -- incomplete type visible through a limited with clause.
4354 if From_With_Type (T)
4355 and then Present (Non_Limited_View (T))
4356 then
4357 Set_From_With_Type (Id);
4358 Set_Non_Limited_View (Id, Non_Limited_View (T));
4360 -- Ada 2005 (AI-412): Add the regular incomplete subtype
4361 -- to the private dependents of the original incomplete
4362 -- type for future transformation.
4364 else
4365 Append_Elmt (Id, Private_Dependents (T));
4366 end if;
4368 -- If the subtype name denotes an incomplete type an error
4369 -- was already reported by Process_Subtype.
4371 else
4372 Set_Etype (Id, Any_Type);
4373 end if;
4375 when others =>
4376 raise Program_Error;
4377 end case;
4378 end if;
4380 if Etype (Id) = Any_Type then
4381 goto Leave;
4382 end if;
4384 -- Some common processing on all types
4386 Set_Size_Info (Id, T);
4387 Set_First_Rep_Item (Id, First_Rep_Item (T));
4389 T := Etype (Id);
4391 Set_Is_Immediately_Visible (Id, True);
4392 Set_Depends_On_Private (Id, Has_Private_Component (T));
4393 Set_Is_Descendent_Of_Address (Id, Is_Descendent_Of_Address (T));
4395 if Is_Interface (T) then
4396 Set_Is_Interface (Id);
4397 end if;
4399 if Present (Generic_Parent_Type (N))
4400 and then
4401 (Nkind
4402 (Parent (Generic_Parent_Type (N))) /= N_Formal_Type_Declaration
4403 or else Nkind
4404 (Formal_Type_Definition (Parent (Generic_Parent_Type (N))))
4405 /= N_Formal_Private_Type_Definition)
4406 then
4407 if Is_Tagged_Type (Id) then
4409 -- If this is a generic actual subtype for a synchronized type,
4410 -- the primitive operations are those of the corresponding record
4411 -- for which there is a separate subtype declaration.
4413 if Is_Concurrent_Type (Id) then
4414 null;
4415 elsif Is_Class_Wide_Type (Id) then
4416 Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
4417 else
4418 Derive_Subprograms (Generic_Parent_Type (N), Id, T);
4419 end if;
4421 elsif Scope (Etype (Id)) /= Standard_Standard then
4422 Derive_Subprograms (Generic_Parent_Type (N), Id);
4423 end if;
4424 end if;
4426 if Is_Private_Type (T)
4427 and then Present (Full_View (T))
4428 then
4429 Conditional_Delay (Id, Full_View (T));
4431 -- The subtypes of components or subcomponents of protected types
4432 -- do not need freeze nodes, which would otherwise appear in the
4433 -- wrong scope (before the freeze node for the protected type). The
4434 -- proper subtypes are those of the subcomponents of the corresponding
4435 -- record.
4437 elsif Ekind (Scope (Id)) /= E_Protected_Type
4438 and then Present (Scope (Scope (Id))) -- error defense!
4439 and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
4440 then
4441 Conditional_Delay (Id, T);
4442 end if;
4444 -- Check that Constraint_Error is raised for a scalar subtype indication
4445 -- when the lower or upper bound of a non-null range lies outside the
4446 -- range of the type mark.
4448 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
4449 if Is_Scalar_Type (Etype (Id))
4450 and then Scalar_Range (Id) /=
4451 Scalar_Range (Etype (Subtype_Mark
4452 (Subtype_Indication (N))))
4453 then
4454 Apply_Range_Check
4455 (Scalar_Range (Id),
4456 Etype (Subtype_Mark (Subtype_Indication (N))));
4458 -- In the array case, check compatibility for each index
4460 elsif Is_Array_Type (Etype (Id))
4461 and then Present (First_Index (Id))
4462 then
4463 -- This really should be a subprogram that finds the indications
4464 -- to check???
4466 declare
4467 Subt_Index : Node_Id := First_Index (Id);
4468 Target_Index : Node_Id :=
4469 First_Index (Etype
4470 (Subtype_Mark (Subtype_Indication (N))));
4471 Has_Dyn_Chk : Boolean := Has_Dynamic_Range_Check (N);
4473 begin
4474 while Present (Subt_Index) loop
4475 if ((Nkind (Subt_Index) = N_Identifier
4476 and then Ekind (Entity (Subt_Index)) in Scalar_Kind)
4477 or else Nkind (Subt_Index) = N_Subtype_Indication)
4478 and then
4479 Nkind (Scalar_Range (Etype (Subt_Index))) = N_Range
4480 then
4481 declare
4482 Target_Typ : constant Entity_Id :=
4483 Etype (Target_Index);
4484 begin
4485 R_Checks :=
4486 Get_Range_Checks
4487 (Scalar_Range (Etype (Subt_Index)),
4488 Target_Typ,
4489 Etype (Subt_Index),
4490 Defining_Identifier (N));
4492 -- Reset Has_Dynamic_Range_Check on the subtype to
4493 -- prevent elision of the index check due to a dynamic
4494 -- check generated for a preceding index (needed since
4495 -- Insert_Range_Checks tries to avoid generating
4496 -- redundant checks on a given declaration).
4498 Set_Has_Dynamic_Range_Check (N, False);
4500 Insert_Range_Checks
4501 (R_Checks,
4503 Target_Typ,
4504 Sloc (Defining_Identifier (N)));
4506 -- Record whether this index involved a dynamic check
4508 Has_Dyn_Chk :=
4509 Has_Dyn_Chk or else Has_Dynamic_Range_Check (N);
4510 end;
4511 end if;
4513 Next_Index (Subt_Index);
4514 Next_Index (Target_Index);
4515 end loop;
4517 -- Finally, mark whether the subtype involves dynamic checks
4519 Set_Has_Dynamic_Range_Check (N, Has_Dyn_Chk);
4520 end;
4521 end if;
4522 end if;
4524 -- Make sure that generic actual types are properly frozen. The subtype
4525 -- is marked as a generic actual type when the enclosing instance is
4526 -- analyzed, so here we identify the subtype from the tree structure.
4528 if Expander_Active
4529 and then Is_Generic_Actual_Type (Id)
4530 and then In_Instance
4531 and then not Comes_From_Source (N)
4532 and then Nkind (Subtype_Indication (N)) /= N_Subtype_Indication
4533 and then Is_Frozen (T)
4534 then
4535 Freeze_Before (N, Id);
4536 end if;
4538 Set_Optimize_Alignment_Flags (Id);
4539 Check_Eliminated (Id);
4541 <<Leave>>
4542 if Has_Aspects (N) then
4543 Analyze_Aspect_Specifications (N, Id);
4544 end if;
4546 Analyze_Dimension (N);
4547 end Analyze_Subtype_Declaration;
4549 --------------------------------
4550 -- Analyze_Subtype_Indication --
4551 --------------------------------
4553 procedure Analyze_Subtype_Indication (N : Node_Id) is
4554 T : constant Entity_Id := Subtype_Mark (N);
4555 R : constant Node_Id := Range_Expression (Constraint (N));
4557 begin
4558 Analyze (T);
4560 if R /= Error then
4561 Analyze (R);
4562 Set_Etype (N, Etype (R));
4563 Resolve (R, Entity (T));
4564 else
4565 Set_Error_Posted (R);
4566 Set_Error_Posted (T);
4567 end if;
4568 end Analyze_Subtype_Indication;
4570 --------------------------
4571 -- Analyze_Variant_Part --
4572 --------------------------
4574 procedure Analyze_Variant_Part (N : Node_Id) is
4576 procedure Non_Static_Choice_Error (Choice : Node_Id);
4577 -- Error routine invoked by the generic instantiation below when the
4578 -- variant part has a non static choice.
4580 procedure Process_Declarations (Variant : Node_Id);
4581 -- Analyzes all the declarations associated with a Variant. Needed by
4582 -- the generic instantiation below.
4584 package Variant_Choices_Processing is new
4585 Generic_Choices_Processing
4586 (Get_Alternatives => Variants,
4587 Get_Choices => Discrete_Choices,
4588 Process_Empty_Choice => No_OP,
4589 Process_Non_Static_Choice => Non_Static_Choice_Error,
4590 Process_Associated_Node => Process_Declarations);
4591 use Variant_Choices_Processing;
4592 -- Instantiation of the generic choice processing package
4594 -----------------------------
4595 -- Non_Static_Choice_Error --
4596 -----------------------------
4598 procedure Non_Static_Choice_Error (Choice : Node_Id) is
4599 begin
4600 Flag_Non_Static_Expr
4601 ("choice given in variant part is not static!", Choice);
4602 end Non_Static_Choice_Error;
4604 --------------------------
4605 -- Process_Declarations --
4606 --------------------------
4608 procedure Process_Declarations (Variant : Node_Id) is
4609 begin
4610 if not Null_Present (Component_List (Variant)) then
4611 Analyze_Declarations (Component_Items (Component_List (Variant)));
4613 if Present (Variant_Part (Component_List (Variant))) then
4614 Analyze (Variant_Part (Component_List (Variant)));
4615 end if;
4616 end if;
4617 end Process_Declarations;
4619 -- Local Variables
4621 Discr_Name : Node_Id;
4622 Discr_Type : Entity_Id;
4624 Dont_Care : Boolean;
4625 Others_Present : Boolean := False;
4627 pragma Warnings (Off, Dont_Care);
4628 pragma Warnings (Off, Others_Present);
4629 -- We don't care about the assigned values of any of these
4631 -- Start of processing for Analyze_Variant_Part
4633 begin
4634 Discr_Name := Name (N);
4635 Analyze (Discr_Name);
4637 -- If Discr_Name bad, get out (prevent cascaded errors)
4639 if Etype (Discr_Name) = Any_Type then
4640 return;
4641 end if;
4643 -- Check invalid discriminant in variant part
4645 if Ekind (Entity (Discr_Name)) /= E_Discriminant then
4646 Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
4647 end if;
4649 Discr_Type := Etype (Entity (Discr_Name));
4651 if not Is_Discrete_Type (Discr_Type) then
4652 Error_Msg_N
4653 ("discriminant in a variant part must be of a discrete type",
4654 Name (N));
4655 return;
4656 end if;
4658 -- Call the instantiated Analyze_Choices which does the rest of the work
4660 Analyze_Choices (N, Discr_Type, Dont_Care, Others_Present);
4661 end Analyze_Variant_Part;
4663 ----------------------------
4664 -- Array_Type_Declaration --
4665 ----------------------------
4667 procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
4668 Component_Def : constant Node_Id := Component_Definition (Def);
4669 Component_Typ : constant Node_Id := Subtype_Indication (Component_Def);
4670 Element_Type : Entity_Id;
4671 Implicit_Base : Entity_Id;
4672 Index : Node_Id;
4673 Related_Id : Entity_Id := Empty;
4674 Nb_Index : Nat;
4675 P : constant Node_Id := Parent (Def);
4676 Priv : Entity_Id;
4678 begin
4679 if Nkind (Def) = N_Constrained_Array_Definition then
4680 Index := First (Discrete_Subtype_Definitions (Def));
4681 else
4682 Index := First (Subtype_Marks (Def));
4683 end if;
4685 -- Find proper names for the implicit types which may be public. In case
4686 -- of anonymous arrays we use the name of the first object of that type
4687 -- as prefix.
4689 if No (T) then
4690 Related_Id := Defining_Identifier (P);
4691 else
4692 Related_Id := T;
4693 end if;
4695 Nb_Index := 1;
4696 while Present (Index) loop
4697 Analyze (Index);
4699 if not Nkind_In (Index, N_Identifier, N_Expanded_Name) then
4700 Check_SPARK_Restriction ("subtype mark required", Index);
4701 end if;
4703 -- Add a subtype declaration for each index of private array type
4704 -- declaration whose etype is also private. For example:
4706 -- package Pkg is
4707 -- type Index is private;
4708 -- private
4709 -- type Table is array (Index) of ...
4710 -- end;
4712 -- This is currently required by the expander for the internally
4713 -- generated equality subprogram of records with variant parts in
4714 -- which the etype of some component is such private type.
4716 if Ekind (Current_Scope) = E_Package
4717 and then In_Private_Part (Current_Scope)
4718 and then Has_Private_Declaration (Etype (Index))
4719 then
4720 declare
4721 Loc : constant Source_Ptr := Sloc (Def);
4722 New_E : Entity_Id;
4723 Decl : Entity_Id;
4725 begin
4726 New_E := Make_Temporary (Loc, 'T');
4727 Set_Is_Internal (New_E);
4729 Decl :=
4730 Make_Subtype_Declaration (Loc,
4731 Defining_Identifier => New_E,
4732 Subtype_Indication =>
4733 New_Occurrence_Of (Etype (Index), Loc));
4735 Insert_Before (Parent (Def), Decl);
4736 Analyze (Decl);
4737 Set_Etype (Index, New_E);
4739 -- If the index is a range the Entity attribute is not
4740 -- available. Example:
4742 -- package Pkg is
4743 -- type T is private;
4744 -- private
4745 -- type T is new Natural;
4746 -- Table : array (T(1) .. T(10)) of Boolean;
4747 -- end Pkg;
4749 if Nkind (Index) /= N_Range then
4750 Set_Entity (Index, New_E);
4751 end if;
4752 end;
4753 end if;
4755 Make_Index (Index, P, Related_Id, Nb_Index);
4757 -- Check error of subtype with predicate for index type
4759 Bad_Predicated_Subtype_Use
4760 ("subtype& has predicate, not allowed as index subtype",
4761 Index, Etype (Index));
4763 -- Move to next index
4765 Next_Index (Index);
4766 Nb_Index := Nb_Index + 1;
4767 end loop;
4769 -- Process subtype indication if one is present
4771 if Present (Component_Typ) then
4772 Element_Type := Process_Subtype (Component_Typ, P, Related_Id, 'C');
4774 Set_Etype (Component_Typ, Element_Type);
4776 if not Nkind_In (Component_Typ, N_Identifier, N_Expanded_Name) then
4777 Check_SPARK_Restriction ("subtype mark required", Component_Typ);
4778 end if;
4780 -- Ada 2005 (AI-230): Access Definition case
4782 else pragma Assert (Present (Access_Definition (Component_Def)));
4784 -- Indicate that the anonymous access type is created by the
4785 -- array type declaration.
4787 Element_Type := Access_Definition
4788 (Related_Nod => P,
4789 N => Access_Definition (Component_Def));
4790 Set_Is_Local_Anonymous_Access (Element_Type);
4792 -- Propagate the parent. This field is needed if we have to generate
4793 -- the master_id associated with an anonymous access to task type
4794 -- component (see Expand_N_Full_Type_Declaration.Build_Master)
4796 Set_Parent (Element_Type, Parent (T));
4798 -- Ada 2005 (AI-230): In case of components that are anonymous access
4799 -- types the level of accessibility depends on the enclosing type
4800 -- declaration
4802 Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
4804 -- Ada 2005 (AI-254)
4806 declare
4807 CD : constant Node_Id :=
4808 Access_To_Subprogram_Definition
4809 (Access_Definition (Component_Def));
4810 begin
4811 if Present (CD) and then Protected_Present (CD) then
4812 Element_Type :=
4813 Replace_Anonymous_Access_To_Protected_Subprogram (Def);
4814 end if;
4815 end;
4816 end if;
4818 -- Constrained array case
4820 if No (T) then
4821 T := Create_Itype (E_Void, P, Related_Id, 'T');
4822 end if;
4824 if Nkind (Def) = N_Constrained_Array_Definition then
4826 -- Establish Implicit_Base as unconstrained base type
4828 Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
4830 Set_Etype (Implicit_Base, Implicit_Base);
4831 Set_Scope (Implicit_Base, Current_Scope);
4832 Set_Has_Delayed_Freeze (Implicit_Base);
4834 -- The constrained array type is a subtype of the unconstrained one
4836 Set_Ekind (T, E_Array_Subtype);
4837 Init_Size_Align (T);
4838 Set_Etype (T, Implicit_Base);
4839 Set_Scope (T, Current_Scope);
4840 Set_Is_Constrained (T, True);
4841 Set_First_Index (T, First (Discrete_Subtype_Definitions (Def)));
4842 Set_Has_Delayed_Freeze (T);
4844 -- Complete setup of implicit base type
4846 Set_First_Index (Implicit_Base, First_Index (T));
4847 Set_Component_Type (Implicit_Base, Element_Type);
4848 Set_Has_Task (Implicit_Base, Has_Task (Element_Type));
4849 Set_Component_Size (Implicit_Base, Uint_0);
4850 Set_Packed_Array_Type (Implicit_Base, Empty);
4851 Set_Has_Controlled_Component
4852 (Implicit_Base, Has_Controlled_Component
4853 (Element_Type)
4854 or else Is_Controlled
4855 (Element_Type));
4856 Set_Finalize_Storage_Only
4857 (Implicit_Base, Finalize_Storage_Only
4858 (Element_Type));
4860 -- Unconstrained array case
4862 else
4863 Set_Ekind (T, E_Array_Type);
4864 Init_Size_Align (T);
4865 Set_Etype (T, T);
4866 Set_Scope (T, Current_Scope);
4867 Set_Component_Size (T, Uint_0);
4868 Set_Is_Constrained (T, False);
4869 Set_First_Index (T, First (Subtype_Marks (Def)));
4870 Set_Has_Delayed_Freeze (T, True);
4871 Set_Has_Task (T, Has_Task (Element_Type));
4872 Set_Has_Controlled_Component (T, Has_Controlled_Component
4873 (Element_Type)
4874 or else
4875 Is_Controlled (Element_Type));
4876 Set_Finalize_Storage_Only (T, Finalize_Storage_Only
4877 (Element_Type));
4878 end if;
4880 -- Common attributes for both cases
4882 Set_Component_Type (Base_Type (T), Element_Type);
4883 Set_Packed_Array_Type (T, Empty);
4885 if Aliased_Present (Component_Definition (Def)) then
4886 Check_SPARK_Restriction
4887 ("aliased is not allowed", Component_Definition (Def));
4888 Set_Has_Aliased_Components (Etype (T));
4889 end if;
4891 -- Ada 2005 (AI-231): Propagate the null-excluding attribute to the
4892 -- array type to ensure that objects of this type are initialized.
4894 if Ada_Version >= Ada_2005
4895 and then Can_Never_Be_Null (Element_Type)
4896 then
4897 Set_Can_Never_Be_Null (T);
4899 if Null_Exclusion_Present (Component_Definition (Def))
4901 -- No need to check itypes because in their case this check was
4902 -- done at their point of creation
4904 and then not Is_Itype (Element_Type)
4905 then
4906 Error_Msg_N
4907 ("`NOT NULL` not allowed (null already excluded)",
4908 Subtype_Indication (Component_Definition (Def)));
4909 end if;
4910 end if;
4912 Priv := Private_Component (Element_Type);
4914 if Present (Priv) then
4916 -- Check for circular definitions
4918 if Priv = Any_Type then
4919 Set_Component_Type (Etype (T), Any_Type);
4921 -- There is a gap in the visibility of operations on the composite
4922 -- type only if the component type is defined in a different scope.
4924 elsif Scope (Priv) = Current_Scope then
4925 null;
4927 elsif Is_Limited_Type (Priv) then
4928 Set_Is_Limited_Composite (Etype (T));
4929 Set_Is_Limited_Composite (T);
4930 else
4931 Set_Is_Private_Composite (Etype (T));
4932 Set_Is_Private_Composite (T);
4933 end if;
4934 end if;
4936 -- A syntax error in the declaration itself may lead to an empty index
4937 -- list, in which case do a minimal patch.
4939 if No (First_Index (T)) then
4940 Error_Msg_N ("missing index definition in array type declaration", T);
4942 declare
4943 Indexes : constant List_Id :=
4944 New_List (New_Occurrence_Of (Any_Id, Sloc (T)));
4945 begin
4946 Set_Discrete_Subtype_Definitions (Def, Indexes);
4947 Set_First_Index (T, First (Indexes));
4948 return;
4949 end;
4950 end if;
4952 -- Create a concatenation operator for the new type. Internal array
4953 -- types created for packed entities do not need such, they are
4954 -- compatible with the user-defined type.
4956 if Number_Dimensions (T) = 1
4957 and then not Is_Packed_Array_Type (T)
4958 then
4959 New_Concatenation_Op (T);
4960 end if;
4962 -- In the case of an unconstrained array the parser has already verified
4963 -- that all the indexes are unconstrained but we still need to make sure
4964 -- that the element type is constrained.
4966 if Is_Indefinite_Subtype (Element_Type) then
4967 Error_Msg_N
4968 ("unconstrained element type in array declaration",
4969 Subtype_Indication (Component_Def));
4971 elsif Is_Abstract_Type (Element_Type) then
4972 Error_Msg_N
4973 ("the type of a component cannot be abstract",
4974 Subtype_Indication (Component_Def));
4975 end if;
4977 -- Ada 2012: if the element type has invariants we must create an
4978 -- invariant procedure for the array type as well.
4980 if Has_Invariants (Element_Type) then
4981 Set_Has_Invariants (T);
4982 end if;
4983 end Array_Type_Declaration;
4985 ------------------------------------------------------
4986 -- Replace_Anonymous_Access_To_Protected_Subprogram --
4987 ------------------------------------------------------
4989 function Replace_Anonymous_Access_To_Protected_Subprogram
4990 (N : Node_Id) return Entity_Id
4992 Loc : constant Source_Ptr := Sloc (N);
4994 Curr_Scope : constant Scope_Stack_Entry :=
4995 Scope_Stack.Table (Scope_Stack.Last);
4997 Anon : constant Entity_Id := Make_Temporary (Loc, 'S');
4998 Acc : Node_Id;
4999 Comp : Node_Id;
5000 Decl : Node_Id;
5001 P : Node_Id;
5003 begin
5004 Set_Is_Internal (Anon);
5006 case Nkind (N) is
5007 when N_Component_Declaration |
5008 N_Unconstrained_Array_Definition |
5009 N_Constrained_Array_Definition =>
5010 Comp := Component_Definition (N);
5011 Acc := Access_Definition (Comp);
5013 when N_Discriminant_Specification =>
5014 Comp := Discriminant_Type (N);
5015 Acc := Comp;
5017 when N_Parameter_Specification =>
5018 Comp := Parameter_Type (N);
5019 Acc := Comp;
5021 when N_Access_Function_Definition =>
5022 Comp := Result_Definition (N);
5023 Acc := Comp;
5025 when N_Object_Declaration =>
5026 Comp := Object_Definition (N);
5027 Acc := Comp;
5029 when N_Function_Specification =>
5030 Comp := Result_Definition (N);
5031 Acc := Comp;
5033 when others =>
5034 raise Program_Error;
5035 end case;
5037 Decl := Make_Full_Type_Declaration (Loc,
5038 Defining_Identifier => Anon,
5039 Type_Definition =>
5040 Copy_Separate_Tree (Access_To_Subprogram_Definition (Acc)));
5042 Mark_Rewrite_Insertion (Decl);
5044 -- Insert the new declaration in the nearest enclosing scope. If the
5045 -- node is a body and N is its return type, the declaration belongs in
5046 -- the enclosing scope.
5048 P := Parent (N);
5050 if Nkind (P) = N_Subprogram_Body
5051 and then Nkind (N) = N_Function_Specification
5052 then
5053 P := Parent (P);
5054 end if;
5056 while Present (P) and then not Has_Declarations (P) loop
5057 P := Parent (P);
5058 end loop;
5060 pragma Assert (Present (P));
5062 if Nkind (P) = N_Package_Specification then
5063 Prepend (Decl, Visible_Declarations (P));
5064 else
5065 Prepend (Decl, Declarations (P));
5066 end if;
5068 -- Replace the anonymous type with an occurrence of the new declaration.
5069 -- In all cases the rewritten node does not have the null-exclusion
5070 -- attribute because (if present) it was already inherited by the
5071 -- anonymous entity (Anon). Thus, in case of components we do not
5072 -- inherit this attribute.
5074 if Nkind (N) = N_Parameter_Specification then
5075 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5076 Set_Etype (Defining_Identifier (N), Anon);
5077 Set_Null_Exclusion_Present (N, False);
5079 elsif Nkind (N) = N_Object_Declaration then
5080 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5081 Set_Etype (Defining_Identifier (N), Anon);
5083 elsif Nkind (N) = N_Access_Function_Definition then
5084 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5086 elsif Nkind (N) = N_Function_Specification then
5087 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5088 Set_Etype (Defining_Unit_Name (N), Anon);
5090 else
5091 Rewrite (Comp,
5092 Make_Component_Definition (Loc,
5093 Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
5094 end if;
5096 Mark_Rewrite_Insertion (Comp);
5098 if Nkind_In (N, N_Object_Declaration, N_Access_Function_Definition) then
5099 Analyze (Decl);
5101 else
5102 -- Temporarily remove the current scope (record or subprogram) from
5103 -- the stack to add the new declarations to the enclosing scope.
5105 Scope_Stack.Decrement_Last;
5106 Analyze (Decl);
5107 Set_Is_Itype (Anon);
5108 Scope_Stack.Append (Curr_Scope);
5109 end if;
5111 Set_Ekind (Anon, E_Anonymous_Access_Protected_Subprogram_Type);
5112 Set_Can_Use_Internal_Rep (Anon, not Always_Compatible_Rep_On_Target);
5113 return Anon;
5114 end Replace_Anonymous_Access_To_Protected_Subprogram;
5116 -------------------------------
5117 -- Build_Derived_Access_Type --
5118 -------------------------------
5120 procedure Build_Derived_Access_Type
5121 (N : Node_Id;
5122 Parent_Type : Entity_Id;
5123 Derived_Type : Entity_Id)
5125 S : constant Node_Id := Subtype_Indication (Type_Definition (N));
5127 Desig_Type : Entity_Id;
5128 Discr : Entity_Id;
5129 Discr_Con_Elist : Elist_Id;
5130 Discr_Con_El : Elmt_Id;
5131 Subt : Entity_Id;
5133 begin
5134 -- Set the designated type so it is available in case this is an access
5135 -- to a self-referential type, e.g. a standard list type with a next
5136 -- pointer. Will be reset after subtype is built.
5138 Set_Directly_Designated_Type
5139 (Derived_Type, Designated_Type (Parent_Type));
5141 Subt := Process_Subtype (S, N);
5143 if Nkind (S) /= N_Subtype_Indication
5144 and then Subt /= Base_Type (Subt)
5145 then
5146 Set_Ekind (Derived_Type, E_Access_Subtype);
5147 end if;
5149 if Ekind (Derived_Type) = E_Access_Subtype then
5150 declare
5151 Pbase : constant Entity_Id := Base_Type (Parent_Type);
5152 Ibase : constant Entity_Id :=
5153 Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
5154 Svg_Chars : constant Name_Id := Chars (Ibase);
5155 Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
5157 begin
5158 Copy_Node (Pbase, Ibase);
5160 Set_Chars (Ibase, Svg_Chars);
5161 Set_Next_Entity (Ibase, Svg_Next_E);
5162 Set_Sloc (Ibase, Sloc (Derived_Type));
5163 Set_Scope (Ibase, Scope (Derived_Type));
5164 Set_Freeze_Node (Ibase, Empty);
5165 Set_Is_Frozen (Ibase, False);
5166 Set_Comes_From_Source (Ibase, False);
5167 Set_Is_First_Subtype (Ibase, False);
5169 Set_Etype (Ibase, Pbase);
5170 Set_Etype (Derived_Type, Ibase);
5171 end;
5172 end if;
5174 Set_Directly_Designated_Type
5175 (Derived_Type, Designated_Type (Subt));
5177 Set_Is_Constrained (Derived_Type, Is_Constrained (Subt));
5178 Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
5179 Set_Size_Info (Derived_Type, Parent_Type);
5180 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
5181 Set_Depends_On_Private (Derived_Type,
5182 Has_Private_Component (Derived_Type));
5183 Conditional_Delay (Derived_Type, Subt);
5185 -- Ada 2005 (AI-231): Set the null-exclusion attribute, and verify
5186 -- that it is not redundant.
5188 if Null_Exclusion_Present (Type_Definition (N)) then
5189 Set_Can_Never_Be_Null (Derived_Type);
5191 if Can_Never_Be_Null (Parent_Type)
5192 and then False
5193 then
5194 Error_Msg_NE
5195 ("`NOT NULL` not allowed (& already excludes null)",
5196 N, Parent_Type);
5197 end if;
5199 elsif Can_Never_Be_Null (Parent_Type) then
5200 Set_Can_Never_Be_Null (Derived_Type);
5201 end if;
5203 -- Note: we do not copy the Storage_Size_Variable, since we always go to
5204 -- the root type for this information.
5206 -- Apply range checks to discriminants for derived record case
5207 -- ??? THIS CODE SHOULD NOT BE HERE REALLY.
5209 Desig_Type := Designated_Type (Derived_Type);
5210 if Is_Composite_Type (Desig_Type)
5211 and then (not Is_Array_Type (Desig_Type))
5212 and then Has_Discriminants (Desig_Type)
5213 and then Base_Type (Desig_Type) /= Desig_Type
5214 then
5215 Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
5216 Discr_Con_El := First_Elmt (Discr_Con_Elist);
5218 Discr := First_Discriminant (Base_Type (Desig_Type));
5219 while Present (Discr_Con_El) loop
5220 Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
5221 Next_Elmt (Discr_Con_El);
5222 Next_Discriminant (Discr);
5223 end loop;
5224 end if;
5225 end Build_Derived_Access_Type;
5227 ------------------------------
5228 -- Build_Derived_Array_Type --
5229 ------------------------------
5231 procedure Build_Derived_Array_Type
5232 (N : Node_Id;
5233 Parent_Type : Entity_Id;
5234 Derived_Type : Entity_Id)
5236 Loc : constant Source_Ptr := Sloc (N);
5237 Tdef : constant Node_Id := Type_Definition (N);
5238 Indic : constant Node_Id := Subtype_Indication (Tdef);
5239 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
5240 Implicit_Base : Entity_Id;
5241 New_Indic : Node_Id;
5243 procedure Make_Implicit_Base;
5244 -- If the parent subtype is constrained, the derived type is a subtype
5245 -- of an implicit base type derived from the parent base.
5247 ------------------------
5248 -- Make_Implicit_Base --
5249 ------------------------
5251 procedure Make_Implicit_Base is
5252 begin
5253 Implicit_Base :=
5254 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
5256 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
5257 Set_Etype (Implicit_Base, Parent_Base);
5259 Copy_Array_Subtype_Attributes (Implicit_Base, Parent_Base);
5260 Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
5262 Set_Has_Delayed_Freeze (Implicit_Base, True);
5263 end Make_Implicit_Base;
5265 -- Start of processing for Build_Derived_Array_Type
5267 begin
5268 if not Is_Constrained (Parent_Type) then
5269 if Nkind (Indic) /= N_Subtype_Indication then
5270 Set_Ekind (Derived_Type, E_Array_Type);
5272 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
5273 Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
5275 Set_Has_Delayed_Freeze (Derived_Type, True);
5277 else
5278 Make_Implicit_Base;
5279 Set_Etype (Derived_Type, Implicit_Base);
5281 New_Indic :=
5282 Make_Subtype_Declaration (Loc,
5283 Defining_Identifier => Derived_Type,
5284 Subtype_Indication =>
5285 Make_Subtype_Indication (Loc,
5286 Subtype_Mark => New_Reference_To (Implicit_Base, Loc),
5287 Constraint => Constraint (Indic)));
5289 Rewrite (N, New_Indic);
5290 Analyze (N);
5291 end if;
5293 else
5294 if Nkind (Indic) /= N_Subtype_Indication then
5295 Make_Implicit_Base;
5297 Set_Ekind (Derived_Type, Ekind (Parent_Type));
5298 Set_Etype (Derived_Type, Implicit_Base);
5299 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
5301 else
5302 Error_Msg_N ("illegal constraint on constrained type", Indic);
5303 end if;
5304 end if;
5306 -- If parent type is not a derived type itself, and is declared in
5307 -- closed scope (e.g. a subprogram), then we must explicitly introduce
5308 -- the new type's concatenation operator since Derive_Subprograms
5309 -- will not inherit the parent's operator. If the parent type is
5310 -- unconstrained, the operator is of the unconstrained base type.
5312 if Number_Dimensions (Parent_Type) = 1
5313 and then not Is_Limited_Type (Parent_Type)
5314 and then not Is_Derived_Type (Parent_Type)
5315 and then not Is_Package_Or_Generic_Package
5316 (Scope (Base_Type (Parent_Type)))
5317 then
5318 if not Is_Constrained (Parent_Type)
5319 and then Is_Constrained (Derived_Type)
5320 then
5321 New_Concatenation_Op (Implicit_Base);
5322 else
5323 New_Concatenation_Op (Derived_Type);
5324 end if;
5325 end if;
5326 end Build_Derived_Array_Type;
5328 -----------------------------------
5329 -- Build_Derived_Concurrent_Type --
5330 -----------------------------------
5332 procedure Build_Derived_Concurrent_Type
5333 (N : Node_Id;
5334 Parent_Type : Entity_Id;
5335 Derived_Type : Entity_Id)
5337 Loc : constant Source_Ptr := Sloc (N);
5339 Corr_Record : constant Entity_Id := Make_Temporary (Loc, 'C');
5340 Corr_Decl : Node_Id;
5341 Corr_Decl_Needed : Boolean;
5342 -- If the derived type has fewer discriminants than its parent, the
5343 -- corresponding record is also a derived type, in order to account for
5344 -- the bound discriminants. We create a full type declaration for it in
5345 -- this case.
5347 Constraint_Present : constant Boolean :=
5348 Nkind (Subtype_Indication (Type_Definition (N))) =
5349 N_Subtype_Indication;
5351 D_Constraint : Node_Id;
5352 New_Constraint : Elist_Id;
5353 Old_Disc : Entity_Id;
5354 New_Disc : Entity_Id;
5355 New_N : Node_Id;
5357 begin
5358 Set_Stored_Constraint (Derived_Type, No_Elist);
5359 Corr_Decl_Needed := False;
5360 Old_Disc := Empty;
5362 if Present (Discriminant_Specifications (N))
5363 and then Constraint_Present
5364 then
5365 Old_Disc := First_Discriminant (Parent_Type);
5366 New_Disc := First (Discriminant_Specifications (N));
5367 while Present (New_Disc) and then Present (Old_Disc) loop
5368 Next_Discriminant (Old_Disc);
5369 Next (New_Disc);
5370 end loop;
5371 end if;
5373 if Present (Old_Disc) and then Expander_Active then
5375 -- The new type has fewer discriminants, so we need to create a new
5376 -- corresponding record, which is derived from the corresponding
5377 -- record of the parent, and has a stored constraint that captures
5378 -- the values of the discriminant constraints. The corresponding
5379 -- record is needed only if expander is active and code generation is
5380 -- enabled.
5382 -- The type declaration for the derived corresponding record has the
5383 -- same discriminant part and constraints as the current declaration.
5384 -- Copy the unanalyzed tree to build declaration.
5386 Corr_Decl_Needed := True;
5387 New_N := Copy_Separate_Tree (N);
5389 Corr_Decl :=
5390 Make_Full_Type_Declaration (Loc,
5391 Defining_Identifier => Corr_Record,
5392 Discriminant_Specifications =>
5393 Discriminant_Specifications (New_N),
5394 Type_Definition =>
5395 Make_Derived_Type_Definition (Loc,
5396 Subtype_Indication =>
5397 Make_Subtype_Indication (Loc,
5398 Subtype_Mark =>
5399 New_Occurrence_Of
5400 (Corresponding_Record_Type (Parent_Type), Loc),
5401 Constraint =>
5402 Constraint
5403 (Subtype_Indication (Type_Definition (New_N))))));
5404 end if;
5406 -- Copy Storage_Size and Relative_Deadline variables if task case
5408 if Is_Task_Type (Parent_Type) then
5409 Set_Storage_Size_Variable (Derived_Type,
5410 Storage_Size_Variable (Parent_Type));
5411 Set_Relative_Deadline_Variable (Derived_Type,
5412 Relative_Deadline_Variable (Parent_Type));
5413 end if;
5415 if Present (Discriminant_Specifications (N)) then
5416 Push_Scope (Derived_Type);
5417 Check_Or_Process_Discriminants (N, Derived_Type);
5419 if Constraint_Present then
5420 New_Constraint :=
5421 Expand_To_Stored_Constraint
5422 (Parent_Type,
5423 Build_Discriminant_Constraints
5424 (Parent_Type,
5425 Subtype_Indication (Type_Definition (N)), True));
5426 end if;
5428 End_Scope;
5430 elsif Constraint_Present then
5432 -- Build constrained subtype and derive from it
5434 declare
5435 Loc : constant Source_Ptr := Sloc (N);
5436 Anon : constant Entity_Id :=
5437 Make_Defining_Identifier (Loc,
5438 Chars => New_External_Name (Chars (Derived_Type), 'T'));
5439 Decl : Node_Id;
5441 begin
5442 Decl :=
5443 Make_Subtype_Declaration (Loc,
5444 Defining_Identifier => Anon,
5445 Subtype_Indication =>
5446 Subtype_Indication (Type_Definition (N)));
5447 Insert_Before (N, Decl);
5448 Analyze (Decl);
5450 Rewrite (Subtype_Indication (Type_Definition (N)),
5451 New_Occurrence_Of (Anon, Loc));
5452 Set_Analyzed (Derived_Type, False);
5453 Analyze (N);
5454 return;
5455 end;
5456 end if;
5458 -- By default, operations and private data are inherited from parent.
5459 -- However, in the presence of bound discriminants, a new corresponding
5460 -- record will be created, see below.
5462 Set_Has_Discriminants
5463 (Derived_Type, Has_Discriminants (Parent_Type));
5464 Set_Corresponding_Record_Type
5465 (Derived_Type, Corresponding_Record_Type (Parent_Type));
5467 -- Is_Constrained is set according the parent subtype, but is set to
5468 -- False if the derived type is declared with new discriminants.
5470 Set_Is_Constrained
5471 (Derived_Type,
5472 (Is_Constrained (Parent_Type) or else Constraint_Present)
5473 and then not Present (Discriminant_Specifications (N)));
5475 if Constraint_Present then
5476 if not Has_Discriminants (Parent_Type) then
5477 Error_Msg_N ("untagged parent must have discriminants", N);
5479 elsif Present (Discriminant_Specifications (N)) then
5481 -- Verify that new discriminants are used to constrain old ones
5483 D_Constraint :=
5484 First
5485 (Constraints
5486 (Constraint (Subtype_Indication (Type_Definition (N)))));
5488 Old_Disc := First_Discriminant (Parent_Type);
5490 while Present (D_Constraint) loop
5491 if Nkind (D_Constraint) /= N_Discriminant_Association then
5493 -- Positional constraint. If it is a reference to a new
5494 -- discriminant, it constrains the corresponding old one.
5496 if Nkind (D_Constraint) = N_Identifier then
5497 New_Disc := First_Discriminant (Derived_Type);
5498 while Present (New_Disc) loop
5499 exit when Chars (New_Disc) = Chars (D_Constraint);
5500 Next_Discriminant (New_Disc);
5501 end loop;
5503 if Present (New_Disc) then
5504 Set_Corresponding_Discriminant (New_Disc, Old_Disc);
5505 end if;
5506 end if;
5508 Next_Discriminant (Old_Disc);
5510 -- if this is a named constraint, search by name for the old
5511 -- discriminants constrained by the new one.
5513 elsif Nkind (Expression (D_Constraint)) = N_Identifier then
5515 -- Find new discriminant with that name
5517 New_Disc := First_Discriminant (Derived_Type);
5518 while Present (New_Disc) loop
5519 exit when
5520 Chars (New_Disc) = Chars (Expression (D_Constraint));
5521 Next_Discriminant (New_Disc);
5522 end loop;
5524 if Present (New_Disc) then
5526 -- Verify that new discriminant renames some discriminant
5527 -- of the parent type, and associate the new discriminant
5528 -- with one or more old ones that it renames.
5530 declare
5531 Selector : Node_Id;
5533 begin
5534 Selector := First (Selector_Names (D_Constraint));
5535 while Present (Selector) loop
5536 Old_Disc := First_Discriminant (Parent_Type);
5537 while Present (Old_Disc) loop
5538 exit when Chars (Old_Disc) = Chars (Selector);
5539 Next_Discriminant (Old_Disc);
5540 end loop;
5542 if Present (Old_Disc) then
5543 Set_Corresponding_Discriminant
5544 (New_Disc, Old_Disc);
5545 end if;
5547 Next (Selector);
5548 end loop;
5549 end;
5550 end if;
5551 end if;
5553 Next (D_Constraint);
5554 end loop;
5556 New_Disc := First_Discriminant (Derived_Type);
5557 while Present (New_Disc) loop
5558 if No (Corresponding_Discriminant (New_Disc)) then
5559 Error_Msg_NE
5560 ("new discriminant& must constrain old one", N, New_Disc);
5562 elsif not
5563 Subtypes_Statically_Compatible
5564 (Etype (New_Disc),
5565 Etype (Corresponding_Discriminant (New_Disc)))
5566 then
5567 Error_Msg_NE
5568 ("& not statically compatible with parent discriminant",
5569 N, New_Disc);
5570 end if;
5572 Next_Discriminant (New_Disc);
5573 end loop;
5574 end if;
5576 elsif Present (Discriminant_Specifications (N)) then
5577 Error_Msg_N
5578 ("missing discriminant constraint in untagged derivation", N);
5579 end if;
5581 -- The entity chain of the derived type includes the new discriminants
5582 -- but shares operations with the parent.
5584 if Present (Discriminant_Specifications (N)) then
5585 Old_Disc := First_Discriminant (Parent_Type);
5586 while Present (Old_Disc) loop
5587 if No (Next_Entity (Old_Disc))
5588 or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
5589 then
5590 Set_Next_Entity
5591 (Last_Entity (Derived_Type), Next_Entity (Old_Disc));
5592 exit;
5593 end if;
5595 Next_Discriminant (Old_Disc);
5596 end loop;
5598 else
5599 Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
5600 if Has_Discriminants (Parent_Type) then
5601 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
5602 Set_Discriminant_Constraint (
5603 Derived_Type, Discriminant_Constraint (Parent_Type));
5604 end if;
5605 end if;
5607 Set_Last_Entity (Derived_Type, Last_Entity (Parent_Type));
5609 Set_Has_Completion (Derived_Type);
5611 if Corr_Decl_Needed then
5612 Set_Stored_Constraint (Derived_Type, New_Constraint);
5613 Insert_After (N, Corr_Decl);
5614 Analyze (Corr_Decl);
5615 Set_Corresponding_Record_Type (Derived_Type, Corr_Record);
5616 end if;
5617 end Build_Derived_Concurrent_Type;
5619 ------------------------------------
5620 -- Build_Derived_Enumeration_Type --
5621 ------------------------------------
5623 procedure Build_Derived_Enumeration_Type
5624 (N : Node_Id;
5625 Parent_Type : Entity_Id;
5626 Derived_Type : Entity_Id)
5628 Loc : constant Source_Ptr := Sloc (N);
5629 Def : constant Node_Id := Type_Definition (N);
5630 Indic : constant Node_Id := Subtype_Indication (Def);
5631 Implicit_Base : Entity_Id;
5632 Literal : Entity_Id;
5633 New_Lit : Entity_Id;
5634 Literals_List : List_Id;
5635 Type_Decl : Node_Id;
5636 Hi, Lo : Node_Id;
5637 Rang_Expr : Node_Id;
5639 begin
5640 -- Since types Standard.Character and Standard.[Wide_]Wide_Character do
5641 -- not have explicit literals lists we need to process types derived
5642 -- from them specially. This is handled by Derived_Standard_Character.
5643 -- If the parent type is a generic type, there are no literals either,
5644 -- and we construct the same skeletal representation as for the generic
5645 -- parent type.
5647 if Is_Standard_Character_Type (Parent_Type) then
5648 Derived_Standard_Character (N, Parent_Type, Derived_Type);
5650 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
5651 declare
5652 Lo : Node_Id;
5653 Hi : Node_Id;
5655 begin
5656 if Nkind (Indic) /= N_Subtype_Indication then
5657 Lo :=
5658 Make_Attribute_Reference (Loc,
5659 Attribute_Name => Name_First,
5660 Prefix => New_Reference_To (Derived_Type, Loc));
5661 Set_Etype (Lo, Derived_Type);
5663 Hi :=
5664 Make_Attribute_Reference (Loc,
5665 Attribute_Name => Name_Last,
5666 Prefix => New_Reference_To (Derived_Type, Loc));
5667 Set_Etype (Hi, Derived_Type);
5669 Set_Scalar_Range (Derived_Type,
5670 Make_Range (Loc,
5671 Low_Bound => Lo,
5672 High_Bound => Hi));
5673 else
5675 -- Analyze subtype indication and verify compatibility
5676 -- with parent type.
5678 if Base_Type (Process_Subtype (Indic, N)) /=
5679 Base_Type (Parent_Type)
5680 then
5681 Error_Msg_N
5682 ("illegal constraint for formal discrete type", N);
5683 end if;
5684 end if;
5685 end;
5687 else
5688 -- If a constraint is present, analyze the bounds to catch
5689 -- premature usage of the derived literals.
5691 if Nkind (Indic) = N_Subtype_Indication
5692 and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
5693 then
5694 Analyze (Low_Bound (Range_Expression (Constraint (Indic))));
5695 Analyze (High_Bound (Range_Expression (Constraint (Indic))));
5696 end if;
5698 -- Introduce an implicit base type for the derived type even if there
5699 -- is no constraint attached to it, since this seems closer to the
5700 -- Ada semantics. Build a full type declaration tree for the derived
5701 -- type using the implicit base type as the defining identifier. The
5702 -- build a subtype declaration tree which applies the constraint (if
5703 -- any) have it replace the derived type declaration.
5705 Literal := First_Literal (Parent_Type);
5706 Literals_List := New_List;
5707 while Present (Literal)
5708 and then Ekind (Literal) = E_Enumeration_Literal
5709 loop
5710 -- Literals of the derived type have the same representation as
5711 -- those of the parent type, but this representation can be
5712 -- overridden by an explicit representation clause. Indicate
5713 -- that there is no explicit representation given yet. These
5714 -- derived literals are implicit operations of the new type,
5715 -- and can be overridden by explicit ones.
5717 if Nkind (Literal) = N_Defining_Character_Literal then
5718 New_Lit :=
5719 Make_Defining_Character_Literal (Loc, Chars (Literal));
5720 else
5721 New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
5722 end if;
5724 Set_Ekind (New_Lit, E_Enumeration_Literal);
5725 Set_Enumeration_Pos (New_Lit, Enumeration_Pos (Literal));
5726 Set_Enumeration_Rep (New_Lit, Enumeration_Rep (Literal));
5727 Set_Enumeration_Rep_Expr (New_Lit, Empty);
5728 Set_Alias (New_Lit, Literal);
5729 Set_Is_Known_Valid (New_Lit, True);
5731 Append (New_Lit, Literals_List);
5732 Next_Literal (Literal);
5733 end loop;
5735 Implicit_Base :=
5736 Make_Defining_Identifier (Sloc (Derived_Type),
5737 Chars => New_External_Name (Chars (Derived_Type), 'B'));
5739 -- Indicate the proper nature of the derived type. This must be done
5740 -- before analysis of the literals, to recognize cases when a literal
5741 -- may be hidden by a previous explicit function definition (cf.
5742 -- c83031a).
5744 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
5745 Set_Etype (Derived_Type, Implicit_Base);
5747 Type_Decl :=
5748 Make_Full_Type_Declaration (Loc,
5749 Defining_Identifier => Implicit_Base,
5750 Discriminant_Specifications => No_List,
5751 Type_Definition =>
5752 Make_Enumeration_Type_Definition (Loc, Literals_List));
5754 Mark_Rewrite_Insertion (Type_Decl);
5755 Insert_Before (N, Type_Decl);
5756 Analyze (Type_Decl);
5758 -- After the implicit base is analyzed its Etype needs to be changed
5759 -- to reflect the fact that it is derived from the parent type which
5760 -- was ignored during analysis. We also set the size at this point.
5762 Set_Etype (Implicit_Base, Parent_Type);
5764 Set_Size_Info (Implicit_Base, Parent_Type);
5765 Set_RM_Size (Implicit_Base, RM_Size (Parent_Type));
5766 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
5768 -- Copy other flags from parent type
5770 Set_Has_Non_Standard_Rep
5771 (Implicit_Base, Has_Non_Standard_Rep
5772 (Parent_Type));
5773 Set_Has_Pragma_Ordered
5774 (Implicit_Base, Has_Pragma_Ordered
5775 (Parent_Type));
5776 Set_Has_Delayed_Freeze (Implicit_Base);
5778 -- Process the subtype indication including a validation check on the
5779 -- constraint, if any. If a constraint is given, its bounds must be
5780 -- implicitly converted to the new type.
5782 if Nkind (Indic) = N_Subtype_Indication then
5783 declare
5784 R : constant Node_Id :=
5785 Range_Expression (Constraint (Indic));
5787 begin
5788 if Nkind (R) = N_Range then
5789 Hi := Build_Scalar_Bound
5790 (High_Bound (R), Parent_Type, Implicit_Base);
5791 Lo := Build_Scalar_Bound
5792 (Low_Bound (R), Parent_Type, Implicit_Base);
5794 else
5795 -- Constraint is a Range attribute. Replace with explicit
5796 -- mention of the bounds of the prefix, which must be a
5797 -- subtype.
5799 Analyze (Prefix (R));
5800 Hi :=
5801 Convert_To (Implicit_Base,
5802 Make_Attribute_Reference (Loc,
5803 Attribute_Name => Name_Last,
5804 Prefix =>
5805 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
5807 Lo :=
5808 Convert_To (Implicit_Base,
5809 Make_Attribute_Reference (Loc,
5810 Attribute_Name => Name_First,
5811 Prefix =>
5812 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
5813 end if;
5814 end;
5816 else
5817 Hi :=
5818 Build_Scalar_Bound
5819 (Type_High_Bound (Parent_Type),
5820 Parent_Type, Implicit_Base);
5821 Lo :=
5822 Build_Scalar_Bound
5823 (Type_Low_Bound (Parent_Type),
5824 Parent_Type, Implicit_Base);
5825 end if;
5827 Rang_Expr :=
5828 Make_Range (Loc,
5829 Low_Bound => Lo,
5830 High_Bound => Hi);
5832 -- If we constructed a default range for the case where no range
5833 -- was given, then the expressions in the range must not freeze
5834 -- since they do not correspond to expressions in the source.
5836 if Nkind (Indic) /= N_Subtype_Indication then
5837 Set_Must_Not_Freeze (Lo);
5838 Set_Must_Not_Freeze (Hi);
5839 Set_Must_Not_Freeze (Rang_Expr);
5840 end if;
5842 Rewrite (N,
5843 Make_Subtype_Declaration (Loc,
5844 Defining_Identifier => Derived_Type,
5845 Subtype_Indication =>
5846 Make_Subtype_Indication (Loc,
5847 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
5848 Constraint =>
5849 Make_Range_Constraint (Loc,
5850 Range_Expression => Rang_Expr))));
5852 Analyze (N);
5854 -- If pragma Discard_Names applies on the first subtype of the parent
5855 -- type, then it must be applied on this subtype as well.
5857 if Einfo.Discard_Names (First_Subtype (Parent_Type)) then
5858 Set_Discard_Names (Derived_Type);
5859 end if;
5861 -- Apply a range check. Since this range expression doesn't have an
5862 -- Etype, we have to specifically pass the Source_Typ parameter. Is
5863 -- this right???
5865 if Nkind (Indic) = N_Subtype_Indication then
5866 Apply_Range_Check (Range_Expression (Constraint (Indic)),
5867 Parent_Type,
5868 Source_Typ => Entity (Subtype_Mark (Indic)));
5869 end if;
5870 end if;
5871 end Build_Derived_Enumeration_Type;
5873 --------------------------------
5874 -- Build_Derived_Numeric_Type --
5875 --------------------------------
5877 procedure Build_Derived_Numeric_Type
5878 (N : Node_Id;
5879 Parent_Type : Entity_Id;
5880 Derived_Type : Entity_Id)
5882 Loc : constant Source_Ptr := Sloc (N);
5883 Tdef : constant Node_Id := Type_Definition (N);
5884 Indic : constant Node_Id := Subtype_Indication (Tdef);
5885 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
5886 No_Constraint : constant Boolean := Nkind (Indic) /=
5887 N_Subtype_Indication;
5888 Implicit_Base : Entity_Id;
5890 Lo : Node_Id;
5891 Hi : Node_Id;
5893 begin
5894 -- Process the subtype indication including a validation check on
5895 -- the constraint if any.
5897 Discard_Node (Process_Subtype (Indic, N));
5899 -- Introduce an implicit base type for the derived type even if there
5900 -- is no constraint attached to it, since this seems closer to the Ada
5901 -- semantics.
5903 Implicit_Base :=
5904 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
5906 Set_Etype (Implicit_Base, Parent_Base);
5907 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
5908 Set_Size_Info (Implicit_Base, Parent_Base);
5909 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
5910 Set_Parent (Implicit_Base, Parent (Derived_Type));
5911 Set_Is_Known_Valid (Implicit_Base, Is_Known_Valid (Parent_Base));
5913 -- Set RM Size for discrete type or decimal fixed-point type
5914 -- Ordinary fixed-point is excluded, why???
5916 if Is_Discrete_Type (Parent_Base)
5917 or else Is_Decimal_Fixed_Point_Type (Parent_Base)
5918 then
5919 Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
5920 end if;
5922 Set_Has_Delayed_Freeze (Implicit_Base);
5924 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
5925 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
5927 Set_Scalar_Range (Implicit_Base,
5928 Make_Range (Loc,
5929 Low_Bound => Lo,
5930 High_Bound => Hi));
5932 if Has_Infinities (Parent_Base) then
5933 Set_Includes_Infinities (Scalar_Range (Implicit_Base));
5934 end if;
5936 -- The Derived_Type, which is the entity of the declaration, is a
5937 -- subtype of the implicit base. Its Ekind is a subtype, even in the
5938 -- absence of an explicit constraint.
5940 Set_Etype (Derived_Type, Implicit_Base);
5942 -- If we did not have a constraint, then the Ekind is set from the
5943 -- parent type (otherwise Process_Subtype has set the bounds)
5945 if No_Constraint then
5946 Set_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
5947 end if;
5949 -- If we did not have a range constraint, then set the range from the
5950 -- parent type. Otherwise, the Process_Subtype call has set the bounds.
5952 if No_Constraint
5953 or else not Has_Range_Constraint (Indic)
5954 then
5955 Set_Scalar_Range (Derived_Type,
5956 Make_Range (Loc,
5957 Low_Bound => New_Copy_Tree (Type_Low_Bound (Parent_Type)),
5958 High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
5959 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
5961 if Has_Infinities (Parent_Type) then
5962 Set_Includes_Infinities (Scalar_Range (Derived_Type));
5963 end if;
5965 Set_Is_Known_Valid (Derived_Type, Is_Known_Valid (Parent_Type));
5966 end if;
5968 Set_Is_Descendent_Of_Address (Derived_Type,
5969 Is_Descendent_Of_Address (Parent_Type));
5970 Set_Is_Descendent_Of_Address (Implicit_Base,
5971 Is_Descendent_Of_Address (Parent_Type));
5973 -- Set remaining type-specific fields, depending on numeric type
5975 if Is_Modular_Integer_Type (Parent_Type) then
5976 Set_Modulus (Implicit_Base, Modulus (Parent_Base));
5978 Set_Non_Binary_Modulus
5979 (Implicit_Base, Non_Binary_Modulus (Parent_Base));
5981 Set_Is_Known_Valid
5982 (Implicit_Base, Is_Known_Valid (Parent_Base));
5984 elsif Is_Floating_Point_Type (Parent_Type) then
5986 -- Digits of base type is always copied from the digits value of
5987 -- the parent base type, but the digits of the derived type will
5988 -- already have been set if there was a constraint present.
5990 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
5991 Set_Float_Rep (Implicit_Base, Float_Rep (Parent_Base));
5993 if No_Constraint then
5994 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
5995 end if;
5997 elsif Is_Fixed_Point_Type (Parent_Type) then
5999 -- Small of base type and derived type are always copied from the
6000 -- parent base type, since smalls never change. The delta of the
6001 -- base type is also copied from the parent base type. However the
6002 -- delta of the derived type will have been set already if a
6003 -- constraint was present.
6005 Set_Small_Value (Derived_Type, Small_Value (Parent_Base));
6006 Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
6007 Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
6009 if No_Constraint then
6010 Set_Delta_Value (Derived_Type, Delta_Value (Parent_Type));
6011 end if;
6013 -- The scale and machine radix in the decimal case are always
6014 -- copied from the parent base type.
6016 if Is_Decimal_Fixed_Point_Type (Parent_Type) then
6017 Set_Scale_Value (Derived_Type, Scale_Value (Parent_Base));
6018 Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
6020 Set_Machine_Radix_10
6021 (Derived_Type, Machine_Radix_10 (Parent_Base));
6022 Set_Machine_Radix_10
6023 (Implicit_Base, Machine_Radix_10 (Parent_Base));
6025 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
6027 if No_Constraint then
6028 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
6030 else
6031 -- the analysis of the subtype_indication sets the
6032 -- digits value of the derived type.
6034 null;
6035 end if;
6036 end if;
6037 end if;
6039 -- The type of the bounds is that of the parent type, and they
6040 -- must be converted to the derived type.
6042 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
6044 -- The implicit_base should be frozen when the derived type is frozen,
6045 -- but note that it is used in the conversions of the bounds. For fixed
6046 -- types we delay the determination of the bounds until the proper
6047 -- freezing point. For other numeric types this is rejected by GCC, for
6048 -- reasons that are currently unclear (???), so we choose to freeze the
6049 -- implicit base now. In the case of integers and floating point types
6050 -- this is harmless because subsequent representation clauses cannot
6051 -- affect anything, but it is still baffling that we cannot use the
6052 -- same mechanism for all derived numeric types.
6054 -- There is a further complication: actually *some* representation
6055 -- clauses can affect the implicit base type. Namely, attribute
6056 -- definition clauses for stream-oriented attributes need to set the
6057 -- corresponding TSS entries on the base type, and this normally cannot
6058 -- be done after the base type is frozen, so the circuitry in
6059 -- Sem_Ch13.New_Stream_Subprogram must account for this possibility and
6060 -- not use Set_TSS in this case.
6062 if Is_Fixed_Point_Type (Parent_Type) then
6063 Conditional_Delay (Implicit_Base, Parent_Type);
6064 else
6065 Freeze_Before (N, Implicit_Base);
6066 end if;
6067 end Build_Derived_Numeric_Type;
6069 --------------------------------
6070 -- Build_Derived_Private_Type --
6071 --------------------------------
6073 procedure Build_Derived_Private_Type
6074 (N : Node_Id;
6075 Parent_Type : Entity_Id;
6076 Derived_Type : Entity_Id;
6077 Is_Completion : Boolean;
6078 Derive_Subps : Boolean := True)
6080 Loc : constant Source_Ptr := Sloc (N);
6081 Der_Base : Entity_Id;
6082 Discr : Entity_Id;
6083 Full_Decl : Node_Id := Empty;
6084 Full_Der : Entity_Id;
6085 Full_P : Entity_Id;
6086 Last_Discr : Entity_Id;
6087 Par_Scope : constant Entity_Id := Scope (Base_Type (Parent_Type));
6088 Swapped : Boolean := False;
6090 procedure Copy_And_Build;
6091 -- Copy derived type declaration, replace parent with its full view,
6092 -- and analyze new declaration.
6094 --------------------
6095 -- Copy_And_Build --
6096 --------------------
6098 procedure Copy_And_Build is
6099 Full_N : Node_Id;
6101 begin
6102 if Ekind (Parent_Type) in Record_Kind
6103 or else
6104 (Ekind (Parent_Type) in Enumeration_Kind
6105 and then not Is_Standard_Character_Type (Parent_Type)
6106 and then not Is_Generic_Type (Root_Type (Parent_Type)))
6107 then
6108 Full_N := New_Copy_Tree (N);
6109 Insert_After (N, Full_N);
6110 Build_Derived_Type (
6111 Full_N, Parent_Type, Full_Der, True, Derive_Subps => False);
6113 else
6114 Build_Derived_Type (
6115 N, Parent_Type, Full_Der, True, Derive_Subps => False);
6116 end if;
6117 end Copy_And_Build;
6119 -- Start of processing for Build_Derived_Private_Type
6121 begin
6122 if Is_Tagged_Type (Parent_Type) then
6123 Full_P := Full_View (Parent_Type);
6125 -- A type extension of a type with unknown discriminants is an
6126 -- indefinite type that the back-end cannot handle directly.
6127 -- We treat it as a private type, and build a completion that is
6128 -- derived from the full view of the parent, and hopefully has
6129 -- known discriminants.
6131 -- If the full view of the parent type has an underlying record view,
6132 -- use it to generate the underlying record view of this derived type
6133 -- (required for chains of derivations with unknown discriminants).
6135 -- Minor optimization: we avoid the generation of useless underlying
6136 -- record view entities if the private type declaration has unknown
6137 -- discriminants but its corresponding full view has no
6138 -- discriminants.
6140 if Has_Unknown_Discriminants (Parent_Type)
6141 and then Present (Full_P)
6142 and then (Has_Discriminants (Full_P)
6143 or else Present (Underlying_Record_View (Full_P)))
6144 and then not In_Open_Scopes (Par_Scope)
6145 and then Expander_Active
6146 then
6147 declare
6148 Full_Der : constant Entity_Id := Make_Temporary (Loc, 'T');
6149 New_Ext : constant Node_Id :=
6150 Copy_Separate_Tree
6151 (Record_Extension_Part (Type_Definition (N)));
6152 Decl : Node_Id;
6154 begin
6155 Build_Derived_Record_Type
6156 (N, Parent_Type, Derived_Type, Derive_Subps);
6158 -- Build anonymous completion, as a derivation from the full
6159 -- view of the parent. This is not a completion in the usual
6160 -- sense, because the current type is not private.
6162 Decl :=
6163 Make_Full_Type_Declaration (Loc,
6164 Defining_Identifier => Full_Der,
6165 Type_Definition =>
6166 Make_Derived_Type_Definition (Loc,
6167 Subtype_Indication =>
6168 New_Copy_Tree
6169 (Subtype_Indication (Type_Definition (N))),
6170 Record_Extension_Part => New_Ext));
6172 -- If the parent type has an underlying record view, use it
6173 -- here to build the new underlying record view.
6175 if Present (Underlying_Record_View (Full_P)) then
6176 pragma Assert
6177 (Nkind (Subtype_Indication (Type_Definition (Decl)))
6178 = N_Identifier);
6179 Set_Entity (Subtype_Indication (Type_Definition (Decl)),
6180 Underlying_Record_View (Full_P));
6181 end if;
6183 Install_Private_Declarations (Par_Scope);
6184 Install_Visible_Declarations (Par_Scope);
6185 Insert_Before (N, Decl);
6187 -- Mark entity as an underlying record view before analysis,
6188 -- to avoid generating the list of its primitive operations
6189 -- (which is not really required for this entity) and thus
6190 -- prevent spurious errors associated with missing overriding
6191 -- of abstract primitives (overridden only for Derived_Type).
6193 Set_Ekind (Full_Der, E_Record_Type);
6194 Set_Is_Underlying_Record_View (Full_Der);
6196 Analyze (Decl);
6198 pragma Assert (Has_Discriminants (Full_Der)
6199 and then not Has_Unknown_Discriminants (Full_Der));
6201 Uninstall_Declarations (Par_Scope);
6203 -- Freeze the underlying record view, to prevent generation of
6204 -- useless dispatching information, which is simply shared with
6205 -- the real derived type.
6207 Set_Is_Frozen (Full_Der);
6209 -- Set up links between real entity and underlying record view
6211 Set_Underlying_Record_View (Derived_Type, Base_Type (Full_Der));
6212 Set_Underlying_Record_View (Base_Type (Full_Der), Derived_Type);
6213 end;
6215 -- If discriminants are known, build derived record
6217 else
6218 Build_Derived_Record_Type
6219 (N, Parent_Type, Derived_Type, Derive_Subps);
6220 end if;
6222 return;
6224 elsif Has_Discriminants (Parent_Type) then
6225 if Present (Full_View (Parent_Type)) then
6226 if not Is_Completion then
6228 -- Copy declaration for subsequent analysis, to provide a
6229 -- completion for what is a private declaration. Indicate that
6230 -- the full type is internally generated.
6232 Full_Decl := New_Copy_Tree (N);
6233 Full_Der := New_Copy (Derived_Type);
6234 Set_Comes_From_Source (Full_Decl, False);
6235 Set_Comes_From_Source (Full_Der, False);
6236 Set_Parent (Full_Der, Full_Decl);
6238 Insert_After (N, Full_Decl);
6240 else
6241 -- If this is a completion, the full view being built is itself
6242 -- private. We build a subtype of the parent with the same
6243 -- constraints as this full view, to convey to the back end the
6244 -- constrained components and the size of this subtype. If the
6245 -- parent is constrained, its full view can serve as the
6246 -- underlying full view of the derived type.
6248 if No (Discriminant_Specifications (N)) then
6249 if Nkind (Subtype_Indication (Type_Definition (N))) =
6250 N_Subtype_Indication
6251 then
6252 Build_Underlying_Full_View (N, Derived_Type, Parent_Type);
6254 elsif Is_Constrained (Full_View (Parent_Type)) then
6255 Set_Underlying_Full_View
6256 (Derived_Type, Full_View (Parent_Type));
6257 end if;
6259 else
6260 -- If there are new discriminants, the parent subtype is
6261 -- constrained by them, but it is not clear how to build
6262 -- the Underlying_Full_View in this case???
6264 null;
6265 end if;
6266 end if;
6267 end if;
6269 -- Build partial view of derived type from partial view of parent
6271 Build_Derived_Record_Type
6272 (N, Parent_Type, Derived_Type, Derive_Subps);
6274 if Present (Full_View (Parent_Type)) and then not Is_Completion then
6275 if not In_Open_Scopes (Par_Scope)
6276 or else not In_Same_Source_Unit (N, Parent_Type)
6277 then
6278 -- Swap partial and full views temporarily
6280 Install_Private_Declarations (Par_Scope);
6281 Install_Visible_Declarations (Par_Scope);
6282 Swapped := True;
6283 end if;
6285 -- Build full view of derived type from full view of parent which
6286 -- is now installed. Subprograms have been derived on the partial
6287 -- view, the completion does not derive them anew.
6289 if not Is_Tagged_Type (Parent_Type) then
6291 -- If the parent is itself derived from another private type,
6292 -- installing the private declarations has not affected its
6293 -- privacy status, so use its own full view explicitly.
6295 if Is_Private_Type (Parent_Type) then
6296 Build_Derived_Record_Type
6297 (Full_Decl, Full_View (Parent_Type), Full_Der, False);
6298 else
6299 Build_Derived_Record_Type
6300 (Full_Decl, Parent_Type, Full_Der, False);
6301 end if;
6303 else
6304 -- If full view of parent is tagged, the completion inherits
6305 -- the proper primitive operations.
6307 Set_Defining_Identifier (Full_Decl, Full_Der);
6308 Build_Derived_Record_Type
6309 (Full_Decl, Parent_Type, Full_Der, Derive_Subps);
6310 end if;
6312 -- The full declaration has been introduced into the tree and
6313 -- processed in the step above. It should not be analyzed again
6314 -- (when encountered later in the current list of declarations)
6315 -- to prevent spurious name conflicts. The full entity remains
6316 -- invisible.
6318 Set_Analyzed (Full_Decl);
6320 if Swapped then
6321 Uninstall_Declarations (Par_Scope);
6323 if In_Open_Scopes (Par_Scope) then
6324 Install_Visible_Declarations (Par_Scope);
6325 end if;
6326 end if;
6328 Der_Base := Base_Type (Derived_Type);
6329 Set_Full_View (Derived_Type, Full_Der);
6330 Set_Full_View (Der_Base, Base_Type (Full_Der));
6332 -- Copy the discriminant list from full view to the partial views
6333 -- (base type and its subtype). Gigi requires that the partial and
6334 -- full views have the same discriminants.
6336 -- Note that since the partial view is pointing to discriminants
6337 -- in the full view, their scope will be that of the full view.
6338 -- This might cause some front end problems and need adjustment???
6340 Discr := First_Discriminant (Base_Type (Full_Der));
6341 Set_First_Entity (Der_Base, Discr);
6343 loop
6344 Last_Discr := Discr;
6345 Next_Discriminant (Discr);
6346 exit when No (Discr);
6347 end loop;
6349 Set_Last_Entity (Der_Base, Last_Discr);
6351 Set_First_Entity (Derived_Type, First_Entity (Der_Base));
6352 Set_Last_Entity (Derived_Type, Last_Entity (Der_Base));
6353 Set_Stored_Constraint (Full_Der, Stored_Constraint (Derived_Type));
6355 else
6356 -- If this is a completion, the derived type stays private and
6357 -- there is no need to create a further full view, except in the
6358 -- unusual case when the derivation is nested within a child unit,
6359 -- see below.
6361 null;
6362 end if;
6364 elsif Present (Full_View (Parent_Type))
6365 and then Has_Discriminants (Full_View (Parent_Type))
6366 then
6367 if Has_Unknown_Discriminants (Parent_Type)
6368 and then Nkind (Subtype_Indication (Type_Definition (N))) =
6369 N_Subtype_Indication
6370 then
6371 Error_Msg_N
6372 ("cannot constrain type with unknown discriminants",
6373 Subtype_Indication (Type_Definition (N)));
6374 return;
6375 end if;
6377 -- If full view of parent is a record type, build full view as a
6378 -- derivation from the parent's full view. Partial view remains
6379 -- private. For code generation and linking, the full view must have
6380 -- the same public status as the partial one. This full view is only
6381 -- needed if the parent type is in an enclosing scope, so that the
6382 -- full view may actually become visible, e.g. in a child unit. This
6383 -- is both more efficient, and avoids order of freezing problems with
6384 -- the added entities.
6386 if not Is_Private_Type (Full_View (Parent_Type))
6387 and then (In_Open_Scopes (Scope (Parent_Type)))
6388 then
6389 Full_Der :=
6390 Make_Defining_Identifier
6391 (Sloc (Derived_Type), Chars (Derived_Type));
6392 Set_Is_Itype (Full_Der);
6393 Set_Has_Private_Declaration (Full_Der);
6394 Set_Has_Private_Declaration (Derived_Type);
6395 Set_Associated_Node_For_Itype (Full_Der, N);
6396 Set_Parent (Full_Der, Parent (Derived_Type));
6397 Set_Full_View (Derived_Type, Full_Der);
6398 Set_Is_Public (Full_Der, Is_Public (Derived_Type));
6399 Full_P := Full_View (Parent_Type);
6400 Exchange_Declarations (Parent_Type);
6401 Copy_And_Build;
6402 Exchange_Declarations (Full_P);
6404 else
6405 Build_Derived_Record_Type
6406 (N, Full_View (Parent_Type), Derived_Type,
6407 Derive_Subps => False);
6408 end if;
6410 -- In any case, the primitive operations are inherited from the
6411 -- parent type, not from the internal full view.
6413 Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
6415 if Derive_Subps then
6416 Derive_Subprograms (Parent_Type, Derived_Type);
6417 end if;
6419 else
6420 -- Untagged type, No discriminants on either view
6422 if Nkind (Subtype_Indication (Type_Definition (N))) =
6423 N_Subtype_Indication
6424 then
6425 Error_Msg_N
6426 ("illegal constraint on type without discriminants", N);
6427 end if;
6429 if Present (Discriminant_Specifications (N))
6430 and then Present (Full_View (Parent_Type))
6431 and then not Is_Tagged_Type (Full_View (Parent_Type))
6432 then
6433 Error_Msg_N ("cannot add discriminants to untagged type", N);
6434 end if;
6436 Set_Stored_Constraint (Derived_Type, No_Elist);
6437 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
6438 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
6439 Set_Has_Controlled_Component
6440 (Derived_Type, Has_Controlled_Component
6441 (Parent_Type));
6443 -- Direct controlled types do not inherit Finalize_Storage_Only flag
6445 if not Is_Controlled (Parent_Type) then
6446 Set_Finalize_Storage_Only
6447 (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
6448 end if;
6450 -- Construct the implicit full view by deriving from full view of the
6451 -- parent type. In order to get proper visibility, we install the
6452 -- parent scope and its declarations.
6454 -- ??? If the parent is untagged private and its completion is
6455 -- tagged, this mechanism will not work because we cannot derive from
6456 -- the tagged full view unless we have an extension.
6458 if Present (Full_View (Parent_Type))
6459 and then not Is_Tagged_Type (Full_View (Parent_Type))
6460 and then not Is_Completion
6461 then
6462 Full_Der :=
6463 Make_Defining_Identifier
6464 (Sloc (Derived_Type), Chars (Derived_Type));
6465 Set_Is_Itype (Full_Der);
6466 Set_Has_Private_Declaration (Full_Der);
6467 Set_Has_Private_Declaration (Derived_Type);
6468 Set_Associated_Node_For_Itype (Full_Der, N);
6469 Set_Parent (Full_Der, Parent (Derived_Type));
6470 Set_Full_View (Derived_Type, Full_Der);
6472 if not In_Open_Scopes (Par_Scope) then
6473 Install_Private_Declarations (Par_Scope);
6474 Install_Visible_Declarations (Par_Scope);
6475 Copy_And_Build;
6476 Uninstall_Declarations (Par_Scope);
6478 -- If parent scope is open and in another unit, and parent has a
6479 -- completion, then the derivation is taking place in the visible
6480 -- part of a child unit. In that case retrieve the full view of
6481 -- the parent momentarily.
6483 elsif not In_Same_Source_Unit (N, Parent_Type) then
6484 Full_P := Full_View (Parent_Type);
6485 Exchange_Declarations (Parent_Type);
6486 Copy_And_Build;
6487 Exchange_Declarations (Full_P);
6489 -- Otherwise it is a local derivation
6491 else
6492 Copy_And_Build;
6493 end if;
6495 Set_Scope (Full_Der, Current_Scope);
6496 Set_Is_First_Subtype (Full_Der,
6497 Is_First_Subtype (Derived_Type));
6498 Set_Has_Size_Clause (Full_Der, False);
6499 Set_Has_Alignment_Clause (Full_Der, False);
6500 Set_Next_Entity (Full_Der, Empty);
6501 Set_Has_Delayed_Freeze (Full_Der);
6502 Set_Is_Frozen (Full_Der, False);
6503 Set_Freeze_Node (Full_Der, Empty);
6504 Set_Depends_On_Private (Full_Der,
6505 Has_Private_Component (Full_Der));
6506 Set_Public_Status (Full_Der);
6507 end if;
6508 end if;
6510 Set_Has_Unknown_Discriminants (Derived_Type,
6511 Has_Unknown_Discriminants (Parent_Type));
6513 if Is_Private_Type (Derived_Type) then
6514 Set_Private_Dependents (Derived_Type, New_Elmt_List);
6515 end if;
6517 if Is_Private_Type (Parent_Type)
6518 and then Base_Type (Parent_Type) = Parent_Type
6519 and then In_Open_Scopes (Scope (Parent_Type))
6520 then
6521 Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
6523 if Is_Child_Unit (Scope (Current_Scope))
6524 and then Is_Completion
6525 and then In_Private_Part (Current_Scope)
6526 and then Scope (Parent_Type) /= Current_Scope
6527 then
6528 -- This is the unusual case where a type completed by a private
6529 -- derivation occurs within a package nested in a child unit, and
6530 -- the parent is declared in an ancestor. In this case, the full
6531 -- view of the parent type will become visible in the body of
6532 -- the enclosing child, and only then will the current type be
6533 -- possibly non-private. We build a underlying full view that
6534 -- will be installed when the enclosing child body is compiled.
6536 Full_Der :=
6537 Make_Defining_Identifier
6538 (Sloc (Derived_Type), Chars (Derived_Type));
6539 Set_Is_Itype (Full_Der);
6540 Build_Itype_Reference (Full_Der, N);
6542 -- The full view will be used to swap entities on entry/exit to
6543 -- the body, and must appear in the entity list for the package.
6545 Append_Entity (Full_Der, Scope (Derived_Type));
6546 Set_Has_Private_Declaration (Full_Der);
6547 Set_Has_Private_Declaration (Derived_Type);
6548 Set_Associated_Node_For_Itype (Full_Der, N);
6549 Set_Parent (Full_Der, Parent (Derived_Type));
6550 Full_P := Full_View (Parent_Type);
6551 Exchange_Declarations (Parent_Type);
6552 Copy_And_Build;
6553 Exchange_Declarations (Full_P);
6554 Set_Underlying_Full_View (Derived_Type, Full_Der);
6555 end if;
6556 end if;
6557 end Build_Derived_Private_Type;
6559 -------------------------------
6560 -- Build_Derived_Record_Type --
6561 -------------------------------
6563 -- 1. INTRODUCTION
6565 -- Ideally we would like to use the same model of type derivation for
6566 -- tagged and untagged record types. Unfortunately this is not quite
6567 -- possible because the semantics of representation clauses is different
6568 -- for tagged and untagged records under inheritance. Consider the
6569 -- following:
6571 -- type R (...) is [tagged] record ... end record;
6572 -- type T (...) is new R (...) [with ...];
6574 -- The representation clauses for T can specify a completely different
6575 -- record layout from R's. Hence the same component can be placed in two
6576 -- very different positions in objects of type T and R. If R and T are
6577 -- tagged types, representation clauses for T can only specify the layout
6578 -- of non inherited components, thus components that are common in R and T
6579 -- have the same position in objects of type R and T.
6581 -- This has two implications. The first is that the entire tree for R's
6582 -- declaration needs to be copied for T in the untagged case, so that T
6583 -- can be viewed as a record type of its own with its own representation
6584 -- clauses. The second implication is the way we handle discriminants.
6585 -- Specifically, in the untagged case we need a way to communicate to Gigi
6586 -- what are the real discriminants in the record, while for the semantics
6587 -- we need to consider those introduced by the user to rename the
6588 -- discriminants in the parent type. This is handled by introducing the
6589 -- notion of stored discriminants. See below for more.
6591 -- Fortunately the way regular components are inherited can be handled in
6592 -- the same way in tagged and untagged types.
6594 -- To complicate things a bit more the private view of a private extension
6595 -- cannot be handled in the same way as the full view (for one thing the
6596 -- semantic rules are somewhat different). We will explain what differs
6597 -- below.
6599 -- 2. DISCRIMINANTS UNDER INHERITANCE
6601 -- The semantic rules governing the discriminants of derived types are
6602 -- quite subtle.
6604 -- type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
6605 -- [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
6607 -- If parent type has discriminants, then the discriminants that are
6608 -- declared in the derived type are [3.4 (11)]:
6610 -- o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
6611 -- there is one;
6613 -- o Otherwise, each discriminant of the parent type (implicitly declared
6614 -- in the same order with the same specifications). In this case, the
6615 -- discriminants are said to be "inherited", or if unknown in the parent
6616 -- are also unknown in the derived type.
6618 -- Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
6620 -- o The parent subtype shall be constrained;
6622 -- o If the parent type is not a tagged type, then each discriminant of
6623 -- the derived type shall be used in the constraint defining a parent
6624 -- subtype. [Implementation note: This ensures that the new discriminant
6625 -- can share storage with an existing discriminant.]
6627 -- For the derived type each discriminant of the parent type is either
6628 -- inherited, constrained to equal some new discriminant of the derived
6629 -- type, or constrained to the value of an expression.
6631 -- When inherited or constrained to equal some new discriminant, the
6632 -- parent discriminant and the discriminant of the derived type are said
6633 -- to "correspond".
6635 -- If a discriminant of the parent type is constrained to a specific value
6636 -- in the derived type definition, then the discriminant is said to be
6637 -- "specified" by that derived type definition.
6639 -- 3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
6641 -- We have spoken about stored discriminants in point 1 (introduction)
6642 -- above. There are two sort of stored discriminants: implicit and
6643 -- explicit. As long as the derived type inherits the same discriminants as
6644 -- the root record type, stored discriminants are the same as regular
6645 -- discriminants, and are said to be implicit. However, if any discriminant
6646 -- in the root type was renamed in the derived type, then the derived
6647 -- type will contain explicit stored discriminants. Explicit stored
6648 -- discriminants are discriminants in addition to the semantically visible
6649 -- discriminants defined for the derived type. Stored discriminants are
6650 -- used by Gigi to figure out what are the physical discriminants in
6651 -- objects of the derived type (see precise definition in einfo.ads).
6652 -- As an example, consider the following:
6654 -- type R (D1, D2, D3 : Int) is record ... end record;
6655 -- type T1 is new R;
6656 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
6657 -- type T3 is new T2;
6658 -- type T4 (Y : Int) is new T3 (Y, 99);
6660 -- The following table summarizes the discriminants and stored
6661 -- discriminants in R and T1 through T4.
6663 -- Type Discrim Stored Discrim Comment
6664 -- R (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in R
6665 -- T1 (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in T1
6666 -- T2 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T2
6667 -- T3 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T3
6668 -- T4 (Y) (D1, D2, D3) Girder discrims EXPLICIT in T4
6670 -- Field Corresponding_Discriminant (abbreviated CD below) allows us to
6671 -- find the corresponding discriminant in the parent type, while
6672 -- Original_Record_Component (abbreviated ORC below), the actual physical
6673 -- component that is renamed. Finally the field Is_Completely_Hidden
6674 -- (abbreviated ICH below) is set for all explicit stored discriminants
6675 -- (see einfo.ads for more info). For the above example this gives:
6677 -- Discrim CD ORC ICH
6678 -- ^^^^^^^ ^^ ^^^ ^^^
6679 -- D1 in R empty itself no
6680 -- D2 in R empty itself no
6681 -- D3 in R empty itself no
6683 -- D1 in T1 D1 in R itself no
6684 -- D2 in T1 D2 in R itself no
6685 -- D3 in T1 D3 in R itself no
6687 -- X1 in T2 D3 in T1 D3 in T2 no
6688 -- X2 in T2 D1 in T1 D1 in T2 no
6689 -- D1 in T2 empty itself yes
6690 -- D2 in T2 empty itself yes
6691 -- D3 in T2 empty itself yes
6693 -- X1 in T3 X1 in T2 D3 in T3 no
6694 -- X2 in T3 X2 in T2 D1 in T3 no
6695 -- D1 in T3 empty itself yes
6696 -- D2 in T3 empty itself yes
6697 -- D3 in T3 empty itself yes
6699 -- Y in T4 X1 in T3 D3 in T3 no
6700 -- D1 in T3 empty itself yes
6701 -- D2 in T3 empty itself yes
6702 -- D3 in T3 empty itself yes
6704 -- 4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
6706 -- Type derivation for tagged types is fairly straightforward. If no
6707 -- discriminants are specified by the derived type, these are inherited
6708 -- from the parent. No explicit stored discriminants are ever necessary.
6709 -- The only manipulation that is done to the tree is that of adding a
6710 -- _parent field with parent type and constrained to the same constraint
6711 -- specified for the parent in the derived type definition. For instance:
6713 -- type R (D1, D2, D3 : Int) is tagged record ... end record;
6714 -- type T1 is new R with null record;
6715 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
6717 -- are changed into:
6719 -- type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
6720 -- _parent : R (D1, D2, D3);
6721 -- end record;
6723 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
6724 -- _parent : T1 (X2, 88, X1);
6725 -- end record;
6727 -- The discriminants actually present in R, T1 and T2 as well as their CD,
6728 -- ORC and ICH fields are:
6730 -- Discrim CD ORC ICH
6731 -- ^^^^^^^ ^^ ^^^ ^^^
6732 -- D1 in R empty itself no
6733 -- D2 in R empty itself no
6734 -- D3 in R empty itself no
6736 -- D1 in T1 D1 in R D1 in R no
6737 -- D2 in T1 D2 in R D2 in R no
6738 -- D3 in T1 D3 in R D3 in R no
6740 -- X1 in T2 D3 in T1 D3 in R no
6741 -- X2 in T2 D1 in T1 D1 in R no
6743 -- 5. FIRST TRANSFORMATION FOR DERIVED RECORDS
6745 -- Regardless of whether we dealing with a tagged or untagged type
6746 -- we will transform all derived type declarations of the form
6748 -- type T is new R (...) [with ...];
6749 -- or
6750 -- subtype S is R (...);
6751 -- type T is new S [with ...];
6752 -- into
6753 -- type BT is new R [with ...];
6754 -- subtype T is BT (...);
6756 -- That is, the base derived type is constrained only if it has no
6757 -- discriminants. The reason for doing this is that GNAT's semantic model
6758 -- assumes that a base type with discriminants is unconstrained.
6760 -- Note that, strictly speaking, the above transformation is not always
6761 -- correct. Consider for instance the following excerpt from ACVC b34011a:
6763 -- procedure B34011A is
6764 -- type REC (D : integer := 0) is record
6765 -- I : Integer;
6766 -- end record;
6768 -- package P is
6769 -- type T6 is new Rec;
6770 -- function F return T6;
6771 -- end P;
6773 -- use P;
6774 -- package Q6 is
6775 -- type U is new T6 (Q6.F.I); -- ERROR: Q6.F.
6776 -- end Q6;
6778 -- The definition of Q6.U is illegal. However transforming Q6.U into
6780 -- type BaseU is new T6;
6781 -- subtype U is BaseU (Q6.F.I)
6783 -- turns U into a legal subtype, which is incorrect. To avoid this problem
6784 -- we always analyze the constraint (in this case (Q6.F.I)) before applying
6785 -- the transformation described above.
6787 -- There is another instance where the above transformation is incorrect.
6788 -- Consider:
6790 -- package Pack is
6791 -- type Base (D : Integer) is tagged null record;
6792 -- procedure P (X : Base);
6794 -- type Der is new Base (2) with null record;
6795 -- procedure P (X : Der);
6796 -- end Pack;
6798 -- Then the above transformation turns this into
6800 -- type Der_Base is new Base with null record;
6801 -- -- procedure P (X : Base) is implicitly inherited here
6802 -- -- as procedure P (X : Der_Base).
6804 -- subtype Der is Der_Base (2);
6805 -- procedure P (X : Der);
6806 -- -- The overriding of P (X : Der_Base) is illegal since we
6807 -- -- have a parameter conformance problem.
6809 -- To get around this problem, after having semantically processed Der_Base
6810 -- and the rewritten subtype declaration for Der, we copy Der_Base field
6811 -- Discriminant_Constraint from Der so that when parameter conformance is
6812 -- checked when P is overridden, no semantic errors are flagged.
6814 -- 6. SECOND TRANSFORMATION FOR DERIVED RECORDS
6816 -- Regardless of whether we are dealing with a tagged or untagged type
6817 -- we will transform all derived type declarations of the form
6819 -- type R (D1, .., Dn : ...) is [tagged] record ...;
6820 -- type T is new R [with ...];
6821 -- into
6822 -- type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
6824 -- The reason for such transformation is that it allows us to implement a
6825 -- very clean form of component inheritance as explained below.
6827 -- Note that this transformation is not achieved by direct tree rewriting
6828 -- and manipulation, but rather by redoing the semantic actions that the
6829 -- above transformation will entail. This is done directly in routine
6830 -- Inherit_Components.
6832 -- 7. TYPE DERIVATION AND COMPONENT INHERITANCE
6834 -- In both tagged and untagged derived types, regular non discriminant
6835 -- components are inherited in the derived type from the parent type. In
6836 -- the absence of discriminants component, inheritance is straightforward
6837 -- as components can simply be copied from the parent.
6839 -- If the parent has discriminants, inheriting components constrained with
6840 -- these discriminants requires caution. Consider the following example:
6842 -- type R (D1, D2 : Positive) is [tagged] record
6843 -- S : String (D1 .. D2);
6844 -- end record;
6846 -- type T1 is new R [with null record];
6847 -- type T2 (X : positive) is new R (1, X) [with null record];
6849 -- As explained in 6. above, T1 is rewritten as
6850 -- type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
6851 -- which makes the treatment for T1 and T2 identical.
6853 -- What we want when inheriting S, is that references to D1 and D2 in R are
6854 -- replaced with references to their correct constraints, i.e. D1 and D2 in
6855 -- T1 and 1 and X in T2. So all R's discriminant references are replaced
6856 -- with either discriminant references in the derived type or expressions.
6857 -- This replacement is achieved as follows: before inheriting R's
6858 -- components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
6859 -- created in the scope of T1 (resp. scope of T2) so that discriminants D1
6860 -- and D2 of T1 are visible (resp. discriminant X of T2 is visible).
6861 -- For T2, for instance, this has the effect of replacing String (D1 .. D2)
6862 -- by String (1 .. X).
6864 -- 8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
6866 -- We explain here the rules governing private type extensions relevant to
6867 -- type derivation. These rules are explained on the following example:
6869 -- type D [(...)] is new A [(...)] with private; <-- partial view
6870 -- type D [(...)] is new P [(...)] with null record; <-- full view
6872 -- Type A is called the ancestor subtype of the private extension.
6873 -- Type P is the parent type of the full view of the private extension. It
6874 -- must be A or a type derived from A.
6876 -- The rules concerning the discriminants of private type extensions are
6877 -- [7.3(10-13)]:
6879 -- o If a private extension inherits known discriminants from the ancestor
6880 -- subtype, then the full view shall also inherit its discriminants from
6881 -- the ancestor subtype and the parent subtype of the full view shall be
6882 -- constrained if and only if the ancestor subtype is constrained.
6884 -- o If a partial view has unknown discriminants, then the full view may
6885 -- define a definite or an indefinite subtype, with or without
6886 -- discriminants.
6888 -- o If a partial view has neither known nor unknown discriminants, then
6889 -- the full view shall define a definite subtype.
6891 -- o If the ancestor subtype of a private extension has constrained
6892 -- discriminants, then the parent subtype of the full view shall impose a
6893 -- statically matching constraint on those discriminants.
6895 -- This means that only the following forms of private extensions are
6896 -- allowed:
6898 -- type D is new A with private; <-- partial view
6899 -- type D is new P with null record; <-- full view
6901 -- If A has no discriminants than P has no discriminants, otherwise P must
6902 -- inherit A's discriminants.
6904 -- type D is new A (...) with private; <-- partial view
6905 -- type D is new P (:::) with null record; <-- full view
6907 -- P must inherit A's discriminants and (...) and (:::) must statically
6908 -- match.
6910 -- subtype A is R (...);
6911 -- type D is new A with private; <-- partial view
6912 -- type D is new P with null record; <-- full view
6914 -- P must have inherited R's discriminants and must be derived from A or
6915 -- any of its subtypes.
6917 -- type D (..) is new A with private; <-- partial view
6918 -- type D (..) is new P [(:::)] with null record; <-- full view
6920 -- No specific constraints on P's discriminants or constraint (:::).
6921 -- Note that A can be unconstrained, but the parent subtype P must either
6922 -- be constrained or (:::) must be present.
6924 -- type D (..) is new A [(...)] with private; <-- partial view
6925 -- type D (..) is new P [(:::)] with null record; <-- full view
6927 -- P's constraints on A's discriminants must statically match those
6928 -- imposed by (...).
6930 -- 9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
6932 -- The full view of a private extension is handled exactly as described
6933 -- above. The model chose for the private view of a private extension is
6934 -- the same for what concerns discriminants (i.e. they receive the same
6935 -- treatment as in the tagged case). However, the private view of the
6936 -- private extension always inherits the components of the parent base,
6937 -- without replacing any discriminant reference. Strictly speaking this is
6938 -- incorrect. However, Gigi never uses this view to generate code so this
6939 -- is a purely semantic issue. In theory, a set of transformations similar
6940 -- to those given in 5. and 6. above could be applied to private views of
6941 -- private extensions to have the same model of component inheritance as
6942 -- for non private extensions. However, this is not done because it would
6943 -- further complicate private type processing. Semantically speaking, this
6944 -- leaves us in an uncomfortable situation. As an example consider:
6946 -- package Pack is
6947 -- type R (D : integer) is tagged record
6948 -- S : String (1 .. D);
6949 -- end record;
6950 -- procedure P (X : R);
6951 -- type T is new R (1) with private;
6952 -- private
6953 -- type T is new R (1) with null record;
6954 -- end;
6956 -- This is transformed into:
6958 -- package Pack is
6959 -- type R (D : integer) is tagged record
6960 -- S : String (1 .. D);
6961 -- end record;
6962 -- procedure P (X : R);
6963 -- type T is new R (1) with private;
6964 -- private
6965 -- type BaseT is new R with null record;
6966 -- subtype T is BaseT (1);
6967 -- end;
6969 -- (strictly speaking the above is incorrect Ada)
6971 -- From the semantic standpoint the private view of private extension T
6972 -- should be flagged as constrained since one can clearly have
6974 -- Obj : T;
6976 -- in a unit withing Pack. However, when deriving subprograms for the
6977 -- private view of private extension T, T must be seen as unconstrained
6978 -- since T has discriminants (this is a constraint of the current
6979 -- subprogram derivation model). Thus, when processing the private view of
6980 -- a private extension such as T, we first mark T as unconstrained, we
6981 -- process it, we perform program derivation and just before returning from
6982 -- Build_Derived_Record_Type we mark T as constrained.
6984 -- ??? Are there are other uncomfortable cases that we will have to
6985 -- deal with.
6987 -- 10. RECORD_TYPE_WITH_PRIVATE complications
6989 -- Types that are derived from a visible record type and have a private
6990 -- extension present other peculiarities. They behave mostly like private
6991 -- types, but if they have primitive operations defined, these will not
6992 -- have the proper signatures for further inheritance, because other
6993 -- primitive operations will use the implicit base that we define for
6994 -- private derivations below. This affect subprogram inheritance (see
6995 -- Derive_Subprograms for details). We also derive the implicit base from
6996 -- the base type of the full view, so that the implicit base is a record
6997 -- type and not another private type, This avoids infinite loops.
6999 procedure Build_Derived_Record_Type
7000 (N : Node_Id;
7001 Parent_Type : Entity_Id;
7002 Derived_Type : Entity_Id;
7003 Derive_Subps : Boolean := True)
7005 Discriminant_Specs : constant Boolean :=
7006 Present (Discriminant_Specifications (N));
7007 Is_Tagged : constant Boolean := Is_Tagged_Type (Parent_Type);
7008 Loc : constant Source_Ptr := Sloc (N);
7009 Private_Extension : constant Boolean :=
7010 Nkind (N) = N_Private_Extension_Declaration;
7011 Assoc_List : Elist_Id;
7012 Constraint_Present : Boolean;
7013 Constrs : Elist_Id;
7014 Discrim : Entity_Id;
7015 Indic : Node_Id;
7016 Inherit_Discrims : Boolean := False;
7017 Last_Discrim : Entity_Id;
7018 New_Base : Entity_Id;
7019 New_Decl : Node_Id;
7020 New_Discrs : Elist_Id;
7021 New_Indic : Node_Id;
7022 Parent_Base : Entity_Id;
7023 Save_Etype : Entity_Id;
7024 Save_Discr_Constr : Elist_Id;
7025 Save_Next_Entity : Entity_Id;
7026 Type_Def : Node_Id;
7028 Discs : Elist_Id := New_Elmt_List;
7029 -- An empty Discs list means that there were no constraints in the
7030 -- subtype indication or that there was an error processing it.
7032 begin
7033 if Ekind (Parent_Type) = E_Record_Type_With_Private
7034 and then Present (Full_View (Parent_Type))
7035 and then Has_Discriminants (Parent_Type)
7036 then
7037 Parent_Base := Base_Type (Full_View (Parent_Type));
7038 else
7039 Parent_Base := Base_Type (Parent_Type);
7040 end if;
7042 -- AI05-0115 : if this is a derivation from a private type in some
7043 -- other scope that may lead to invisible components for the derived
7044 -- type, mark it accordingly.
7046 if Is_Private_Type (Parent_Type) then
7047 if Scope (Parent_Type) = Scope (Derived_Type) then
7048 null;
7050 elsif In_Open_Scopes (Scope (Parent_Type))
7051 and then In_Private_Part (Scope (Parent_Type))
7052 then
7053 null;
7055 else
7056 Set_Has_Private_Ancestor (Derived_Type);
7057 end if;
7059 else
7060 Set_Has_Private_Ancestor
7061 (Derived_Type, Has_Private_Ancestor (Parent_Type));
7062 end if;
7064 -- Before we start the previously documented transformations, here is
7065 -- little fix for size and alignment of tagged types. Normally when we
7066 -- derive type D from type P, we copy the size and alignment of P as the
7067 -- default for D, and in the absence of explicit representation clauses
7068 -- for D, the size and alignment are indeed the same as the parent.
7070 -- But this is wrong for tagged types, since fields may be added, and
7071 -- the default size may need to be larger, and the default alignment may
7072 -- need to be larger.
7074 -- We therefore reset the size and alignment fields in the tagged case.
7075 -- Note that the size and alignment will in any case be at least as
7076 -- large as the parent type (since the derived type has a copy of the
7077 -- parent type in the _parent field)
7079 -- The type is also marked as being tagged here, which is needed when
7080 -- processing components with a self-referential anonymous access type
7081 -- in the call to Check_Anonymous_Access_Components below. Note that
7082 -- this flag is also set later on for completeness.
7084 if Is_Tagged then
7085 Set_Is_Tagged_Type (Derived_Type);
7086 Init_Size_Align (Derived_Type);
7087 end if;
7089 -- STEP 0a: figure out what kind of derived type declaration we have
7091 if Private_Extension then
7092 Type_Def := N;
7093 Set_Ekind (Derived_Type, E_Record_Type_With_Private);
7095 else
7096 Type_Def := Type_Definition (N);
7098 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
7099 -- Parent_Base can be a private type or private extension. However,
7100 -- for tagged types with an extension the newly added fields are
7101 -- visible and hence the Derived_Type is always an E_Record_Type.
7102 -- (except that the parent may have its own private fields).
7103 -- For untagged types we preserve the Ekind of the Parent_Base.
7105 if Present (Record_Extension_Part (Type_Def)) then
7106 Set_Ekind (Derived_Type, E_Record_Type);
7108 -- Create internal access types for components with anonymous
7109 -- access types.
7111 if Ada_Version >= Ada_2005 then
7112 Check_Anonymous_Access_Components
7113 (N, Derived_Type, Derived_Type,
7114 Component_List (Record_Extension_Part (Type_Def)));
7115 end if;
7117 else
7118 Set_Ekind (Derived_Type, Ekind (Parent_Base));
7119 end if;
7120 end if;
7122 -- Indic can either be an N_Identifier if the subtype indication
7123 -- contains no constraint or an N_Subtype_Indication if the subtype
7124 -- indication has a constraint.
7126 Indic := Subtype_Indication (Type_Def);
7127 Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
7129 -- Check that the type has visible discriminants. The type may be
7130 -- a private type with unknown discriminants whose full view has
7131 -- discriminants which are invisible.
7133 if Constraint_Present then
7134 if not Has_Discriminants (Parent_Base)
7135 or else
7136 (Has_Unknown_Discriminants (Parent_Base)
7137 and then Is_Private_Type (Parent_Base))
7138 then
7139 Error_Msg_N
7140 ("invalid constraint: type has no discriminant",
7141 Constraint (Indic));
7143 Constraint_Present := False;
7144 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
7146 elsif Is_Constrained (Parent_Type) then
7147 Error_Msg_N
7148 ("invalid constraint: parent type is already constrained",
7149 Constraint (Indic));
7151 Constraint_Present := False;
7152 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
7153 end if;
7154 end if;
7156 -- STEP 0b: If needed, apply transformation given in point 5. above
7158 if not Private_Extension
7159 and then Has_Discriminants (Parent_Type)
7160 and then not Discriminant_Specs
7161 and then (Is_Constrained (Parent_Type) or else Constraint_Present)
7162 then
7163 -- First, we must analyze the constraint (see comment in point 5.)
7165 if Constraint_Present then
7166 New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
7168 if Has_Discriminants (Derived_Type)
7169 and then Has_Private_Declaration (Derived_Type)
7170 and then Present (Discriminant_Constraint (Derived_Type))
7171 then
7172 -- Verify that constraints of the full view statically match
7173 -- those given in the partial view.
7175 declare
7176 C1, C2 : Elmt_Id;
7178 begin
7179 C1 := First_Elmt (New_Discrs);
7180 C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
7181 while Present (C1) and then Present (C2) loop
7182 if Fully_Conformant_Expressions (Node (C1), Node (C2))
7183 or else
7184 (Is_OK_Static_Expression (Node (C1))
7185 and then
7186 Is_OK_Static_Expression (Node (C2))
7187 and then
7188 Expr_Value (Node (C1)) = Expr_Value (Node (C2)))
7189 then
7190 null;
7192 else
7193 Error_Msg_N (
7194 "constraint not conformant to previous declaration",
7195 Node (C1));
7196 end if;
7198 Next_Elmt (C1);
7199 Next_Elmt (C2);
7200 end loop;
7201 end;
7202 end if;
7203 end if;
7205 -- Insert and analyze the declaration for the unconstrained base type
7207 New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
7209 New_Decl :=
7210 Make_Full_Type_Declaration (Loc,
7211 Defining_Identifier => New_Base,
7212 Type_Definition =>
7213 Make_Derived_Type_Definition (Loc,
7214 Abstract_Present => Abstract_Present (Type_Def),
7215 Limited_Present => Limited_Present (Type_Def),
7216 Subtype_Indication =>
7217 New_Occurrence_Of (Parent_Base, Loc),
7218 Record_Extension_Part =>
7219 Relocate_Node (Record_Extension_Part (Type_Def)),
7220 Interface_List => Interface_List (Type_Def)));
7222 Set_Parent (New_Decl, Parent (N));
7223 Mark_Rewrite_Insertion (New_Decl);
7224 Insert_Before (N, New_Decl);
7226 -- In the extension case, make sure ancestor is frozen appropriately
7227 -- (see also non-discriminated case below).
7229 if Present (Record_Extension_Part (Type_Def))
7230 or else Is_Interface (Parent_Base)
7231 then
7232 Freeze_Before (New_Decl, Parent_Type);
7233 end if;
7235 -- Note that this call passes False for the Derive_Subps parameter
7236 -- because subprogram derivation is deferred until after creating
7237 -- the subtype (see below).
7239 Build_Derived_Type
7240 (New_Decl, Parent_Base, New_Base,
7241 Is_Completion => True, Derive_Subps => False);
7243 -- ??? This needs re-examination to determine whether the
7244 -- above call can simply be replaced by a call to Analyze.
7246 Set_Analyzed (New_Decl);
7248 -- Insert and analyze the declaration for the constrained subtype
7250 if Constraint_Present then
7251 New_Indic :=
7252 Make_Subtype_Indication (Loc,
7253 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
7254 Constraint => Relocate_Node (Constraint (Indic)));
7256 else
7257 declare
7258 Constr_List : constant List_Id := New_List;
7259 C : Elmt_Id;
7260 Expr : Node_Id;
7262 begin
7263 C := First_Elmt (Discriminant_Constraint (Parent_Type));
7264 while Present (C) loop
7265 Expr := Node (C);
7267 -- It is safe here to call New_Copy_Tree since
7268 -- Force_Evaluation was called on each constraint in
7269 -- Build_Discriminant_Constraints.
7271 Append (New_Copy_Tree (Expr), To => Constr_List);
7273 Next_Elmt (C);
7274 end loop;
7276 New_Indic :=
7277 Make_Subtype_Indication (Loc,
7278 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
7279 Constraint =>
7280 Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
7281 end;
7282 end if;
7284 Rewrite (N,
7285 Make_Subtype_Declaration (Loc,
7286 Defining_Identifier => Derived_Type,
7287 Subtype_Indication => New_Indic));
7289 Analyze (N);
7291 -- Derivation of subprograms must be delayed until the full subtype
7292 -- has been established, to ensure proper overriding of subprograms
7293 -- inherited by full types. If the derivations occurred as part of
7294 -- the call to Build_Derived_Type above, then the check for type
7295 -- conformance would fail because earlier primitive subprograms
7296 -- could still refer to the full type prior the change to the new
7297 -- subtype and hence would not match the new base type created here.
7298 -- Subprograms are not derived, however, when Derive_Subps is False
7299 -- (since otherwise there could be redundant derivations).
7301 if Derive_Subps then
7302 Derive_Subprograms (Parent_Type, Derived_Type);
7303 end if;
7305 -- For tagged types the Discriminant_Constraint of the new base itype
7306 -- is inherited from the first subtype so that no subtype conformance
7307 -- problem arise when the first subtype overrides primitive
7308 -- operations inherited by the implicit base type.
7310 if Is_Tagged then
7311 Set_Discriminant_Constraint
7312 (New_Base, Discriminant_Constraint (Derived_Type));
7313 end if;
7315 return;
7316 end if;
7318 -- If we get here Derived_Type will have no discriminants or it will be
7319 -- a discriminated unconstrained base type.
7321 -- STEP 1a: perform preliminary actions/checks for derived tagged types
7323 if Is_Tagged then
7325 -- The parent type is frozen for non-private extensions (RM 13.14(7))
7326 -- The declaration of a specific descendant of an interface type
7327 -- freezes the interface type (RM 13.14).
7329 if not Private_Extension or else Is_Interface (Parent_Base) then
7330 Freeze_Before (N, Parent_Type);
7331 end if;
7333 -- In Ada 2005 (AI-344), the restriction that a derived tagged type
7334 -- cannot be declared at a deeper level than its parent type is
7335 -- removed. The check on derivation within a generic body is also
7336 -- relaxed, but there's a restriction that a derived tagged type
7337 -- cannot be declared in a generic body if it's derived directly
7338 -- or indirectly from a formal type of that generic.
7340 if Ada_Version >= Ada_2005 then
7341 if Present (Enclosing_Generic_Body (Derived_Type)) then
7342 declare
7343 Ancestor_Type : Entity_Id;
7345 begin
7346 -- Check to see if any ancestor of the derived type is a
7347 -- formal type.
7349 Ancestor_Type := Parent_Type;
7350 while not Is_Generic_Type (Ancestor_Type)
7351 and then Etype (Ancestor_Type) /= Ancestor_Type
7352 loop
7353 Ancestor_Type := Etype (Ancestor_Type);
7354 end loop;
7356 -- If the derived type does have a formal type as an
7357 -- ancestor, then it's an error if the derived type is
7358 -- declared within the body of the generic unit that
7359 -- declares the formal type in its generic formal part. It's
7360 -- sufficient to check whether the ancestor type is declared
7361 -- inside the same generic body as the derived type (such as
7362 -- within a nested generic spec), in which case the
7363 -- derivation is legal. If the formal type is declared
7364 -- outside of that generic body, then it's guaranteed that
7365 -- the derived type is declared within the generic body of
7366 -- the generic unit declaring the formal type.
7368 if Is_Generic_Type (Ancestor_Type)
7369 and then Enclosing_Generic_Body (Ancestor_Type) /=
7370 Enclosing_Generic_Body (Derived_Type)
7371 then
7372 Error_Msg_NE
7373 ("parent type of& must not be descendant of formal type"
7374 & " of an enclosing generic body",
7375 Indic, Derived_Type);
7376 end if;
7377 end;
7378 end if;
7380 elsif Type_Access_Level (Derived_Type) /=
7381 Type_Access_Level (Parent_Type)
7382 and then not Is_Generic_Type (Derived_Type)
7383 then
7384 if Is_Controlled (Parent_Type) then
7385 Error_Msg_N
7386 ("controlled type must be declared at the library level",
7387 Indic);
7388 else
7389 Error_Msg_N
7390 ("type extension at deeper accessibility level than parent",
7391 Indic);
7392 end if;
7394 else
7395 declare
7396 GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
7398 begin
7399 if Present (GB)
7400 and then GB /= Enclosing_Generic_Body (Parent_Base)
7401 then
7402 Error_Msg_NE
7403 ("parent type of& must not be outside generic body"
7404 & " (RM 3.9.1(4))",
7405 Indic, Derived_Type);
7406 end if;
7407 end;
7408 end if;
7409 end if;
7411 -- Ada 2005 (AI-251)
7413 if Ada_Version >= Ada_2005 and then Is_Tagged then
7415 -- "The declaration of a specific descendant of an interface type
7416 -- freezes the interface type" (RM 13.14).
7418 declare
7419 Iface : Node_Id;
7420 begin
7421 if Is_Non_Empty_List (Interface_List (Type_Def)) then
7422 Iface := First (Interface_List (Type_Def));
7423 while Present (Iface) loop
7424 Freeze_Before (N, Etype (Iface));
7425 Next (Iface);
7426 end loop;
7427 end if;
7428 end;
7429 end if;
7431 -- STEP 1b : preliminary cleanup of the full view of private types
7433 -- If the type is already marked as having discriminants, then it's the
7434 -- completion of a private type or private extension and we need to
7435 -- retain the discriminants from the partial view if the current
7436 -- declaration has Discriminant_Specifications so that we can verify
7437 -- conformance. However, we must remove any existing components that
7438 -- were inherited from the parent (and attached in Copy_And_Swap)
7439 -- because the full type inherits all appropriate components anyway, and
7440 -- we do not want the partial view's components interfering.
7442 if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
7443 Discrim := First_Discriminant (Derived_Type);
7444 loop
7445 Last_Discrim := Discrim;
7446 Next_Discriminant (Discrim);
7447 exit when No (Discrim);
7448 end loop;
7450 Set_Last_Entity (Derived_Type, Last_Discrim);
7452 -- In all other cases wipe out the list of inherited components (even
7453 -- inherited discriminants), it will be properly rebuilt here.
7455 else
7456 Set_First_Entity (Derived_Type, Empty);
7457 Set_Last_Entity (Derived_Type, Empty);
7458 end if;
7460 -- STEP 1c: Initialize some flags for the Derived_Type
7462 -- The following flags must be initialized here so that
7463 -- Process_Discriminants can check that discriminants of tagged types do
7464 -- not have a default initial value and that access discriminants are
7465 -- only specified for limited records. For completeness, these flags are
7466 -- also initialized along with all the other flags below.
7468 -- AI-419: Limitedness is not inherited from an interface parent, so to
7469 -- be limited in that case the type must be explicitly declared as
7470 -- limited. However, task and protected interfaces are always limited.
7472 if Limited_Present (Type_Def) then
7473 Set_Is_Limited_Record (Derived_Type);
7475 elsif Is_Limited_Record (Parent_Type)
7476 or else (Present (Full_View (Parent_Type))
7477 and then Is_Limited_Record (Full_View (Parent_Type)))
7478 then
7479 if not Is_Interface (Parent_Type)
7480 or else Is_Synchronized_Interface (Parent_Type)
7481 or else Is_Protected_Interface (Parent_Type)
7482 or else Is_Task_Interface (Parent_Type)
7483 then
7484 Set_Is_Limited_Record (Derived_Type);
7485 end if;
7486 end if;
7488 -- STEP 2a: process discriminants of derived type if any
7490 Push_Scope (Derived_Type);
7492 if Discriminant_Specs then
7493 Set_Has_Unknown_Discriminants (Derived_Type, False);
7495 -- The following call initializes fields Has_Discriminants and
7496 -- Discriminant_Constraint, unless we are processing the completion
7497 -- of a private type declaration.
7499 Check_Or_Process_Discriminants (N, Derived_Type);
7501 -- For untagged types, the constraint on the Parent_Type must be
7502 -- present and is used to rename the discriminants.
7504 if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
7505 Error_Msg_N ("untagged parent must have discriminants", Indic);
7507 elsif not Is_Tagged and then not Constraint_Present then
7508 Error_Msg_N
7509 ("discriminant constraint needed for derived untagged records",
7510 Indic);
7512 -- Otherwise the parent subtype must be constrained unless we have a
7513 -- private extension.
7515 elsif not Constraint_Present
7516 and then not Private_Extension
7517 and then not Is_Constrained (Parent_Type)
7518 then
7519 Error_Msg_N
7520 ("unconstrained type not allowed in this context", Indic);
7522 elsif Constraint_Present then
7523 -- The following call sets the field Corresponding_Discriminant
7524 -- for the discriminants in the Derived_Type.
7526 Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
7528 -- For untagged types all new discriminants must rename
7529 -- discriminants in the parent. For private extensions new
7530 -- discriminants cannot rename old ones (implied by [7.3(13)]).
7532 Discrim := First_Discriminant (Derived_Type);
7533 while Present (Discrim) loop
7534 if not Is_Tagged
7535 and then No (Corresponding_Discriminant (Discrim))
7536 then
7537 Error_Msg_N
7538 ("new discriminants must constrain old ones", Discrim);
7540 elsif Private_Extension
7541 and then Present (Corresponding_Discriminant (Discrim))
7542 then
7543 Error_Msg_N
7544 ("only static constraints allowed for parent"
7545 & " discriminants in the partial view", Indic);
7546 exit;
7547 end if;
7549 -- If a new discriminant is used in the constraint, then its
7550 -- subtype must be statically compatible with the parent
7551 -- discriminant's subtype (3.7(15)).
7553 if Present (Corresponding_Discriminant (Discrim))
7554 and then
7555 not Subtypes_Statically_Compatible
7556 (Etype (Discrim),
7557 Etype (Corresponding_Discriminant (Discrim)))
7558 then
7559 Error_Msg_N
7560 ("subtype must be compatible with parent discriminant",
7561 Discrim);
7562 end if;
7564 Next_Discriminant (Discrim);
7565 end loop;
7567 -- Check whether the constraints of the full view statically
7568 -- match those imposed by the parent subtype [7.3(13)].
7570 if Present (Stored_Constraint (Derived_Type)) then
7571 declare
7572 C1, C2 : Elmt_Id;
7574 begin
7575 C1 := First_Elmt (Discs);
7576 C2 := First_Elmt (Stored_Constraint (Derived_Type));
7577 while Present (C1) and then Present (C2) loop
7578 if not
7579 Fully_Conformant_Expressions (Node (C1), Node (C2))
7580 then
7581 Error_Msg_N
7582 ("not conformant with previous declaration",
7583 Node (C1));
7584 end if;
7586 Next_Elmt (C1);
7587 Next_Elmt (C2);
7588 end loop;
7589 end;
7590 end if;
7591 end if;
7593 -- STEP 2b: No new discriminants, inherit discriminants if any
7595 else
7596 if Private_Extension then
7597 Set_Has_Unknown_Discriminants
7598 (Derived_Type,
7599 Has_Unknown_Discriminants (Parent_Type)
7600 or else Unknown_Discriminants_Present (N));
7602 -- The partial view of the parent may have unknown discriminants,
7603 -- but if the full view has discriminants and the parent type is
7604 -- in scope they must be inherited.
7606 elsif Has_Unknown_Discriminants (Parent_Type)
7607 and then
7608 (not Has_Discriminants (Parent_Type)
7609 or else not In_Open_Scopes (Scope (Parent_Type)))
7610 then
7611 Set_Has_Unknown_Discriminants (Derived_Type);
7612 end if;
7614 if not Has_Unknown_Discriminants (Derived_Type)
7615 and then not Has_Unknown_Discriminants (Parent_Base)
7616 and then Has_Discriminants (Parent_Type)
7617 then
7618 Inherit_Discrims := True;
7619 Set_Has_Discriminants
7620 (Derived_Type, True);
7621 Set_Discriminant_Constraint
7622 (Derived_Type, Discriminant_Constraint (Parent_Base));
7623 end if;
7625 -- The following test is true for private types (remember
7626 -- transformation 5. is not applied to those) and in an error
7627 -- situation.
7629 if Constraint_Present then
7630 Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
7631 end if;
7633 -- For now mark a new derived type as constrained only if it has no
7634 -- discriminants. At the end of Build_Derived_Record_Type we properly
7635 -- set this flag in the case of private extensions. See comments in
7636 -- point 9. just before body of Build_Derived_Record_Type.
7638 Set_Is_Constrained
7639 (Derived_Type,
7640 not (Inherit_Discrims
7641 or else Has_Unknown_Discriminants (Derived_Type)));
7642 end if;
7644 -- STEP 3: initialize fields of derived type
7646 Set_Is_Tagged_Type (Derived_Type, Is_Tagged);
7647 Set_Stored_Constraint (Derived_Type, No_Elist);
7649 -- Ada 2005 (AI-251): Private type-declarations can implement interfaces
7650 -- but cannot be interfaces
7652 if not Private_Extension
7653 and then Ekind (Derived_Type) /= E_Private_Type
7654 and then Ekind (Derived_Type) /= E_Limited_Private_Type
7655 then
7656 if Interface_Present (Type_Def) then
7657 Analyze_Interface_Declaration (Derived_Type, Type_Def);
7658 end if;
7660 Set_Interfaces (Derived_Type, No_Elist);
7661 end if;
7663 -- Fields inherited from the Parent_Type
7665 Set_Discard_Names
7666 (Derived_Type, Einfo.Discard_Names (Parent_Type));
7667 Set_Has_Specified_Layout
7668 (Derived_Type, Has_Specified_Layout (Parent_Type));
7669 Set_Is_Limited_Composite
7670 (Derived_Type, Is_Limited_Composite (Parent_Type));
7671 Set_Is_Private_Composite
7672 (Derived_Type, Is_Private_Composite (Parent_Type));
7674 -- Fields inherited from the Parent_Base
7676 Set_Has_Controlled_Component
7677 (Derived_Type, Has_Controlled_Component (Parent_Base));
7678 Set_Has_Non_Standard_Rep
7679 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
7680 Set_Has_Primitive_Operations
7681 (Derived_Type, Has_Primitive_Operations (Parent_Base));
7683 -- Fields inherited from the Parent_Base in the non-private case
7685 if Ekind (Derived_Type) = E_Record_Type then
7686 Set_Has_Complex_Representation
7687 (Derived_Type, Has_Complex_Representation (Parent_Base));
7688 end if;
7690 -- Fields inherited from the Parent_Base for record types
7692 if Is_Record_Type (Derived_Type) then
7694 declare
7695 Parent_Full : Entity_Id;
7697 begin
7698 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
7699 -- Parent_Base can be a private type or private extension. Go
7700 -- to the full view here to get the E_Record_Type specific flags.
7702 if Present (Full_View (Parent_Base)) then
7703 Parent_Full := Full_View (Parent_Base);
7704 else
7705 Parent_Full := Parent_Base;
7706 end if;
7708 Set_OK_To_Reorder_Components
7709 (Derived_Type, OK_To_Reorder_Components (Parent_Full));
7710 Set_Reverse_Bit_Order
7711 (Derived_Type, Reverse_Bit_Order (Parent_Full));
7712 Set_Reverse_Storage_Order
7713 (Derived_Type, Reverse_Storage_Order (Parent_Full));
7714 end;
7715 end if;
7717 -- Direct controlled types do not inherit Finalize_Storage_Only flag
7719 if not Is_Controlled (Parent_Type) then
7720 Set_Finalize_Storage_Only
7721 (Derived_Type, Finalize_Storage_Only (Parent_Type));
7722 end if;
7724 -- Set fields for private derived types
7726 if Is_Private_Type (Derived_Type) then
7727 Set_Depends_On_Private (Derived_Type, True);
7728 Set_Private_Dependents (Derived_Type, New_Elmt_List);
7730 -- Inherit fields from non private record types. If this is the
7731 -- completion of a derivation from a private type, the parent itself
7732 -- is private, and the attributes come from its full view, which must
7733 -- be present.
7735 else
7736 if Is_Private_Type (Parent_Base)
7737 and then not Is_Record_Type (Parent_Base)
7738 then
7739 Set_Component_Alignment
7740 (Derived_Type, Component_Alignment (Full_View (Parent_Base)));
7741 Set_C_Pass_By_Copy
7742 (Derived_Type, C_Pass_By_Copy (Full_View (Parent_Base)));
7743 else
7744 Set_Component_Alignment
7745 (Derived_Type, Component_Alignment (Parent_Base));
7746 Set_C_Pass_By_Copy
7747 (Derived_Type, C_Pass_By_Copy (Parent_Base));
7748 end if;
7749 end if;
7751 -- Set fields for tagged types
7753 if Is_Tagged then
7754 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
7756 -- All tagged types defined in Ada.Finalization are controlled
7758 if Chars (Scope (Derived_Type)) = Name_Finalization
7759 and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
7760 and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
7761 then
7762 Set_Is_Controlled (Derived_Type);
7763 else
7764 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Base));
7765 end if;
7767 -- Minor optimization: there is no need to generate the class-wide
7768 -- entity associated with an underlying record view.
7770 if not Is_Underlying_Record_View (Derived_Type) then
7771 Make_Class_Wide_Type (Derived_Type);
7772 end if;
7774 Set_Is_Abstract_Type (Derived_Type, Abstract_Present (Type_Def));
7776 if Has_Discriminants (Derived_Type)
7777 and then Constraint_Present
7778 then
7779 Set_Stored_Constraint
7780 (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
7781 end if;
7783 if Ada_Version >= Ada_2005 then
7784 declare
7785 Ifaces_List : Elist_Id;
7787 begin
7788 -- Checks rules 3.9.4 (13/2 and 14/2)
7790 if Comes_From_Source (Derived_Type)
7791 and then not Is_Private_Type (Derived_Type)
7792 and then Is_Interface (Parent_Type)
7793 and then not Is_Interface (Derived_Type)
7794 then
7795 if Is_Task_Interface (Parent_Type) then
7796 Error_Msg_N
7797 ("(Ada 2005) task type required (RM 3.9.4 (13.2))",
7798 Derived_Type);
7800 elsif Is_Protected_Interface (Parent_Type) then
7801 Error_Msg_N
7802 ("(Ada 2005) protected type required (RM 3.9.4 (14.2))",
7803 Derived_Type);
7804 end if;
7805 end if;
7807 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
7809 Check_Interfaces (N, Type_Def);
7811 -- Ada 2005 (AI-251): Collect the list of progenitors that are
7812 -- not already in the parents.
7814 Collect_Interfaces
7815 (T => Derived_Type,
7816 Ifaces_List => Ifaces_List,
7817 Exclude_Parents => True);
7819 Set_Interfaces (Derived_Type, Ifaces_List);
7821 -- If the derived type is the anonymous type created for
7822 -- a declaration whose parent has a constraint, propagate
7823 -- the interface list to the source type. This must be done
7824 -- prior to the completion of the analysis of the source type
7825 -- because the components in the extension may contain current
7826 -- instances whose legality depends on some ancestor.
7828 if Is_Itype (Derived_Type) then
7829 declare
7830 Def : constant Node_Id :=
7831 Associated_Node_For_Itype (Derived_Type);
7832 begin
7833 if Present (Def)
7834 and then Nkind (Def) = N_Full_Type_Declaration
7835 then
7836 Set_Interfaces
7837 (Defining_Identifier (Def), Ifaces_List);
7838 end if;
7839 end;
7840 end if;
7841 end;
7842 end if;
7844 else
7845 Set_Is_Packed (Derived_Type, Is_Packed (Parent_Base));
7846 Set_Has_Non_Standard_Rep
7847 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
7848 end if;
7850 -- STEP 4: Inherit components from the parent base and constrain them.
7851 -- Apply the second transformation described in point 6. above.
7853 if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
7854 or else not Has_Discriminants (Parent_Type)
7855 or else not Is_Constrained (Parent_Type)
7856 then
7857 Constrs := Discs;
7858 else
7859 Constrs := Discriminant_Constraint (Parent_Type);
7860 end if;
7862 Assoc_List :=
7863 Inherit_Components
7864 (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
7866 -- STEP 5a: Copy the parent record declaration for untagged types
7868 if not Is_Tagged then
7870 -- Discriminant_Constraint (Derived_Type) has been properly
7871 -- constructed. Save it and temporarily set it to Empty because we
7872 -- do not want the call to New_Copy_Tree below to mess this list.
7874 if Has_Discriminants (Derived_Type) then
7875 Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
7876 Set_Discriminant_Constraint (Derived_Type, No_Elist);
7877 else
7878 Save_Discr_Constr := No_Elist;
7879 end if;
7881 -- Save the Etype field of Derived_Type. It is correctly set now,
7882 -- but the call to New_Copy tree may remap it to point to itself,
7883 -- which is not what we want. Ditto for the Next_Entity field.
7885 Save_Etype := Etype (Derived_Type);
7886 Save_Next_Entity := Next_Entity (Derived_Type);
7888 -- Assoc_List maps all stored discriminants in the Parent_Base to
7889 -- stored discriminants in the Derived_Type. It is fundamental that
7890 -- no types or itypes with discriminants other than the stored
7891 -- discriminants appear in the entities declared inside
7892 -- Derived_Type, since the back end cannot deal with it.
7894 New_Decl :=
7895 New_Copy_Tree
7896 (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
7898 -- Restore the fields saved prior to the New_Copy_Tree call
7899 -- and compute the stored constraint.
7901 Set_Etype (Derived_Type, Save_Etype);
7902 Set_Next_Entity (Derived_Type, Save_Next_Entity);
7904 if Has_Discriminants (Derived_Type) then
7905 Set_Discriminant_Constraint
7906 (Derived_Type, Save_Discr_Constr);
7907 Set_Stored_Constraint
7908 (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
7909 Replace_Components (Derived_Type, New_Decl);
7910 Set_Has_Implicit_Dereference
7911 (Derived_Type, Has_Implicit_Dereference (Parent_Type));
7912 end if;
7914 -- Insert the new derived type declaration
7916 Rewrite (N, New_Decl);
7918 -- STEP 5b: Complete the processing for record extensions in generics
7920 -- There is no completion for record extensions declared in the
7921 -- parameter part of a generic, so we need to complete processing for
7922 -- these generic record extensions here. The Record_Type_Definition call
7923 -- will change the Ekind of the components from E_Void to E_Component.
7925 elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
7926 Record_Type_Definition (Empty, Derived_Type);
7928 -- STEP 5c: Process the record extension for non private tagged types
7930 elsif not Private_Extension then
7932 -- Add the _parent field in the derived type
7934 Expand_Record_Extension (Derived_Type, Type_Def);
7936 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
7937 -- implemented interfaces if we are in expansion mode
7939 if Expander_Active
7940 and then Has_Interfaces (Derived_Type)
7941 then
7942 Add_Interface_Tag_Components (N, Derived_Type);
7943 end if;
7945 -- Analyze the record extension
7947 Record_Type_Definition
7948 (Record_Extension_Part (Type_Def), Derived_Type);
7949 end if;
7951 End_Scope;
7953 -- Nothing else to do if there is an error in the derivation.
7954 -- An unusual case: the full view may be derived from a type in an
7955 -- instance, when the partial view was used illegally as an actual
7956 -- in that instance, leading to a circular definition.
7958 if Etype (Derived_Type) = Any_Type
7959 or else Etype (Parent_Type) = Derived_Type
7960 then
7961 return;
7962 end if;
7964 -- Set delayed freeze and then derive subprograms, we need to do
7965 -- this in this order so that derived subprograms inherit the
7966 -- derived freeze if necessary.
7968 Set_Has_Delayed_Freeze (Derived_Type);
7970 if Derive_Subps then
7971 Derive_Subprograms (Parent_Type, Derived_Type);
7972 end if;
7974 -- If we have a private extension which defines a constrained derived
7975 -- type mark as constrained here after we have derived subprograms. See
7976 -- comment on point 9. just above the body of Build_Derived_Record_Type.
7978 if Private_Extension and then Inherit_Discrims then
7979 if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
7980 Set_Is_Constrained (Derived_Type, True);
7981 Set_Discriminant_Constraint (Derived_Type, Discs);
7983 elsif Is_Constrained (Parent_Type) then
7984 Set_Is_Constrained
7985 (Derived_Type, True);
7986 Set_Discriminant_Constraint
7987 (Derived_Type, Discriminant_Constraint (Parent_Type));
7988 end if;
7989 end if;
7991 -- Update the class-wide type, which shares the now-completed entity
7992 -- list with its specific type. In case of underlying record views,
7993 -- we do not generate the corresponding class wide entity.
7995 if Is_Tagged
7996 and then not Is_Underlying_Record_View (Derived_Type)
7997 then
7998 Set_First_Entity
7999 (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
8000 Set_Last_Entity
8001 (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
8002 end if;
8003 end Build_Derived_Record_Type;
8005 ------------------------
8006 -- Build_Derived_Type --
8007 ------------------------
8009 procedure Build_Derived_Type
8010 (N : Node_Id;
8011 Parent_Type : Entity_Id;
8012 Derived_Type : Entity_Id;
8013 Is_Completion : Boolean;
8014 Derive_Subps : Boolean := True)
8016 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
8018 begin
8019 -- Set common attributes
8021 Set_Scope (Derived_Type, Current_Scope);
8023 Set_Ekind (Derived_Type, Ekind (Parent_Base));
8024 Set_Etype (Derived_Type, Parent_Base);
8025 Set_Has_Task (Derived_Type, Has_Task (Parent_Base));
8027 Set_Size_Info (Derived_Type, Parent_Type);
8028 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
8029 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
8030 Set_Is_Tagged_Type (Derived_Type, Is_Tagged_Type (Parent_Type));
8032 -- If the parent type is a private subtype, the convention on the base
8033 -- type may be set in the private part, and not propagated to the
8034 -- subtype until later, so we obtain the convention from the base type.
8036 Set_Convention (Derived_Type, Convention (Parent_Base));
8038 -- Propagate invariant information. The new type has invariants if
8039 -- they are inherited from the parent type, and these invariants can
8040 -- be further inherited, so both flags are set.
8042 if Has_Inheritable_Invariants (Parent_Type) then
8043 Set_Has_Inheritable_Invariants (Derived_Type);
8044 Set_Has_Invariants (Derived_Type);
8045 end if;
8047 -- We similarly inherit predicates
8049 if Has_Predicates (Parent_Type) then
8050 Set_Has_Predicates (Derived_Type);
8051 end if;
8053 -- The derived type inherits the representation clauses of the parent.
8054 -- However, for a private type that is completed by a derivation, there
8055 -- may be operation attributes that have been specified already (stream
8056 -- attributes and External_Tag) and those must be provided. Finally,
8057 -- if the partial view is a private extension, the representation items
8058 -- of the parent have been inherited already, and should not be chained
8059 -- twice to the derived type.
8061 if Is_Tagged_Type (Parent_Type)
8062 and then Present (First_Rep_Item (Derived_Type))
8063 then
8064 -- The existing items are either operational items or items inherited
8065 -- from a private extension declaration.
8067 declare
8068 Rep : Node_Id;
8069 -- Used to iterate over representation items of the derived type
8071 Last_Rep : Node_Id;
8072 -- Last representation item of the (non-empty) representation
8073 -- item list of the derived type.
8075 Found : Boolean := False;
8077 begin
8078 Rep := First_Rep_Item (Derived_Type);
8079 Last_Rep := Rep;
8080 while Present (Rep) loop
8081 if Rep = First_Rep_Item (Parent_Type) then
8082 Found := True;
8083 exit;
8085 else
8086 Rep := Next_Rep_Item (Rep);
8088 if Present (Rep) then
8089 Last_Rep := Rep;
8090 end if;
8091 end if;
8092 end loop;
8094 -- Here if we either encountered the parent type's first rep
8095 -- item on the derived type's rep item list (in which case
8096 -- Found is True, and we have nothing else to do), or if we
8097 -- reached the last rep item of the derived type, which is
8098 -- Last_Rep, in which case we further chain the parent type's
8099 -- rep items to those of the derived type.
8101 if not Found then
8102 Set_Next_Rep_Item (Last_Rep, First_Rep_Item (Parent_Type));
8103 end if;
8104 end;
8106 else
8107 Set_First_Rep_Item (Derived_Type, First_Rep_Item (Parent_Type));
8108 end if;
8110 case Ekind (Parent_Type) is
8111 when Numeric_Kind =>
8112 Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
8114 when Array_Kind =>
8115 Build_Derived_Array_Type (N, Parent_Type, Derived_Type);
8117 when E_Record_Type
8118 | E_Record_Subtype
8119 | Class_Wide_Kind =>
8120 Build_Derived_Record_Type
8121 (N, Parent_Type, Derived_Type, Derive_Subps);
8122 return;
8124 when Enumeration_Kind =>
8125 Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
8127 when Access_Kind =>
8128 Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
8130 when Incomplete_Or_Private_Kind =>
8131 Build_Derived_Private_Type
8132 (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
8134 -- For discriminated types, the derivation includes deriving
8135 -- primitive operations. For others it is done below.
8137 if Is_Tagged_Type (Parent_Type)
8138 or else Has_Discriminants (Parent_Type)
8139 or else (Present (Full_View (Parent_Type))
8140 and then Has_Discriminants (Full_View (Parent_Type)))
8141 then
8142 return;
8143 end if;
8145 when Concurrent_Kind =>
8146 Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
8148 when others =>
8149 raise Program_Error;
8150 end case;
8152 if Etype (Derived_Type) = Any_Type then
8153 return;
8154 end if;
8156 -- Set delayed freeze and then derive subprograms, we need to do this
8157 -- in this order so that derived subprograms inherit the derived freeze
8158 -- if necessary.
8160 Set_Has_Delayed_Freeze (Derived_Type);
8161 if Derive_Subps then
8162 Derive_Subprograms (Parent_Type, Derived_Type);
8163 end if;
8165 Set_Has_Primitive_Operations
8166 (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
8167 end Build_Derived_Type;
8169 -----------------------
8170 -- Build_Discriminal --
8171 -----------------------
8173 procedure Build_Discriminal (Discrim : Entity_Id) is
8174 D_Minal : Entity_Id;
8175 CR_Disc : Entity_Id;
8177 begin
8178 -- A discriminal has the same name as the discriminant
8180 D_Minal := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
8182 Set_Ekind (D_Minal, E_In_Parameter);
8183 Set_Mechanism (D_Minal, Default_Mechanism);
8184 Set_Etype (D_Minal, Etype (Discrim));
8185 Set_Scope (D_Minal, Current_Scope);
8187 Set_Discriminal (Discrim, D_Minal);
8188 Set_Discriminal_Link (D_Minal, Discrim);
8190 -- For task types, build at once the discriminants of the corresponding
8191 -- record, which are needed if discriminants are used in entry defaults
8192 -- and in family bounds.
8194 if Is_Concurrent_Type (Current_Scope)
8195 or else Is_Limited_Type (Current_Scope)
8196 then
8197 CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
8199 Set_Ekind (CR_Disc, E_In_Parameter);
8200 Set_Mechanism (CR_Disc, Default_Mechanism);
8201 Set_Etype (CR_Disc, Etype (Discrim));
8202 Set_Scope (CR_Disc, Current_Scope);
8203 Set_Discriminal_Link (CR_Disc, Discrim);
8204 Set_CR_Discriminant (Discrim, CR_Disc);
8205 end if;
8206 end Build_Discriminal;
8208 ------------------------------------
8209 -- Build_Discriminant_Constraints --
8210 ------------------------------------
8212 function Build_Discriminant_Constraints
8213 (T : Entity_Id;
8214 Def : Node_Id;
8215 Derived_Def : Boolean := False) return Elist_Id
8217 C : constant Node_Id := Constraint (Def);
8218 Nb_Discr : constant Nat := Number_Discriminants (T);
8220 Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
8221 -- Saves the expression corresponding to a given discriminant in T
8223 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
8224 -- Return the Position number within array Discr_Expr of a discriminant
8225 -- D within the discriminant list of the discriminated type T.
8227 ------------------
8228 -- Pos_Of_Discr --
8229 ------------------
8231 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
8232 Disc : Entity_Id;
8234 begin
8235 Disc := First_Discriminant (T);
8236 for J in Discr_Expr'Range loop
8237 if Disc = D then
8238 return J;
8239 end if;
8241 Next_Discriminant (Disc);
8242 end loop;
8244 -- Note: Since this function is called on discriminants that are
8245 -- known to belong to the discriminated type, falling through the
8246 -- loop with no match signals an internal compiler error.
8248 raise Program_Error;
8249 end Pos_Of_Discr;
8251 -- Declarations local to Build_Discriminant_Constraints
8253 Discr : Entity_Id;
8254 E : Entity_Id;
8255 Elist : constant Elist_Id := New_Elmt_List;
8257 Constr : Node_Id;
8258 Expr : Node_Id;
8259 Id : Node_Id;
8260 Position : Nat;
8261 Found : Boolean;
8263 Discrim_Present : Boolean := False;
8265 -- Start of processing for Build_Discriminant_Constraints
8267 begin
8268 -- The following loop will process positional associations only.
8269 -- For a positional association, the (single) discriminant is
8270 -- implicitly specified by position, in textual order (RM 3.7.2).
8272 Discr := First_Discriminant (T);
8273 Constr := First (Constraints (C));
8274 for D in Discr_Expr'Range loop
8275 exit when Nkind (Constr) = N_Discriminant_Association;
8277 if No (Constr) then
8278 Error_Msg_N ("too few discriminants given in constraint", C);
8279 return New_Elmt_List;
8281 elsif Nkind (Constr) = N_Range
8282 or else (Nkind (Constr) = N_Attribute_Reference
8283 and then
8284 Attribute_Name (Constr) = Name_Range)
8285 then
8286 Error_Msg_N
8287 ("a range is not a valid discriminant constraint", Constr);
8288 Discr_Expr (D) := Error;
8290 else
8291 Analyze_And_Resolve (Constr, Base_Type (Etype (Discr)));
8292 Discr_Expr (D) := Constr;
8293 end if;
8295 Next_Discriminant (Discr);
8296 Next (Constr);
8297 end loop;
8299 if No (Discr) and then Present (Constr) then
8300 Error_Msg_N ("too many discriminants given in constraint", Constr);
8301 return New_Elmt_List;
8302 end if;
8304 -- Named associations can be given in any order, but if both positional
8305 -- and named associations are used in the same discriminant constraint,
8306 -- then positional associations must occur first, at their normal
8307 -- position. Hence once a named association is used, the rest of the
8308 -- discriminant constraint must use only named associations.
8310 while Present (Constr) loop
8312 -- Positional association forbidden after a named association
8314 if Nkind (Constr) /= N_Discriminant_Association then
8315 Error_Msg_N ("positional association follows named one", Constr);
8316 return New_Elmt_List;
8318 -- Otherwise it is a named association
8320 else
8321 -- E records the type of the discriminants in the named
8322 -- association. All the discriminants specified in the same name
8323 -- association must have the same type.
8325 E := Empty;
8327 -- Search the list of discriminants in T to see if the simple name
8328 -- given in the constraint matches any of them.
8330 Id := First (Selector_Names (Constr));
8331 while Present (Id) loop
8332 Found := False;
8334 -- If Original_Discriminant is present, we are processing a
8335 -- generic instantiation and this is an instance node. We need
8336 -- to find the name of the corresponding discriminant in the
8337 -- actual record type T and not the name of the discriminant in
8338 -- the generic formal. Example:
8340 -- generic
8341 -- type G (D : int) is private;
8342 -- package P is
8343 -- subtype W is G (D => 1);
8344 -- end package;
8345 -- type Rec (X : int) is record ... end record;
8346 -- package Q is new P (G => Rec);
8348 -- At the point of the instantiation, formal type G is Rec
8349 -- and therefore when reanalyzing "subtype W is G (D => 1);"
8350 -- which really looks like "subtype W is Rec (D => 1);" at
8351 -- the point of instantiation, we want to find the discriminant
8352 -- that corresponds to D in Rec, i.e. X.
8354 if Present (Original_Discriminant (Id))
8355 and then In_Instance
8356 then
8357 Discr := Find_Corresponding_Discriminant (Id, T);
8358 Found := True;
8360 else
8361 Discr := First_Discriminant (T);
8362 while Present (Discr) loop
8363 if Chars (Discr) = Chars (Id) then
8364 Found := True;
8365 exit;
8366 end if;
8368 Next_Discriminant (Discr);
8369 end loop;
8371 if not Found then
8372 Error_Msg_N ("& does not match any discriminant", Id);
8373 return New_Elmt_List;
8375 -- If the parent type is a generic formal, preserve the
8376 -- name of the discriminant for subsequent instances.
8377 -- see comment at the beginning of this if statement.
8379 elsif Is_Generic_Type (Root_Type (T)) then
8380 Set_Original_Discriminant (Id, Discr);
8381 end if;
8382 end if;
8384 Position := Pos_Of_Discr (T, Discr);
8386 if Present (Discr_Expr (Position)) then
8387 Error_Msg_N ("duplicate constraint for discriminant&", Id);
8389 else
8390 -- Each discriminant specified in the same named association
8391 -- must be associated with a separate copy of the
8392 -- corresponding expression.
8394 if Present (Next (Id)) then
8395 Expr := New_Copy_Tree (Expression (Constr));
8396 Set_Parent (Expr, Parent (Expression (Constr)));
8397 else
8398 Expr := Expression (Constr);
8399 end if;
8401 Discr_Expr (Position) := Expr;
8402 Analyze_And_Resolve (Expr, Base_Type (Etype (Discr)));
8403 end if;
8405 -- A discriminant association with more than one discriminant
8406 -- name is only allowed if the named discriminants are all of
8407 -- the same type (RM 3.7.1(8)).
8409 if E = Empty then
8410 E := Base_Type (Etype (Discr));
8412 elsif Base_Type (Etype (Discr)) /= E then
8413 Error_Msg_N
8414 ("all discriminants in an association " &
8415 "must have the same type", Id);
8416 end if;
8418 Next (Id);
8419 end loop;
8420 end if;
8422 Next (Constr);
8423 end loop;
8425 -- A discriminant constraint must provide exactly one value for each
8426 -- discriminant of the type (RM 3.7.1(8)).
8428 for J in Discr_Expr'Range loop
8429 if No (Discr_Expr (J)) then
8430 Error_Msg_N ("too few discriminants given in constraint", C);
8431 return New_Elmt_List;
8432 end if;
8433 end loop;
8435 -- Determine if there are discriminant expressions in the constraint
8437 for J in Discr_Expr'Range loop
8438 if Denotes_Discriminant
8439 (Discr_Expr (J), Check_Concurrent => True)
8440 then
8441 Discrim_Present := True;
8442 end if;
8443 end loop;
8445 -- Build an element list consisting of the expressions given in the
8446 -- discriminant constraint and apply the appropriate checks. The list
8447 -- is constructed after resolving any named discriminant associations
8448 -- and therefore the expressions appear in the textual order of the
8449 -- discriminants.
8451 Discr := First_Discriminant (T);
8452 for J in Discr_Expr'Range loop
8453 if Discr_Expr (J) /= Error then
8454 Append_Elmt (Discr_Expr (J), Elist);
8456 -- If any of the discriminant constraints is given by a
8457 -- discriminant and we are in a derived type declaration we
8458 -- have a discriminant renaming. Establish link between new
8459 -- and old discriminant.
8461 if Denotes_Discriminant (Discr_Expr (J)) then
8462 if Derived_Def then
8463 Set_Corresponding_Discriminant
8464 (Entity (Discr_Expr (J)), Discr);
8465 end if;
8467 -- Force the evaluation of non-discriminant expressions.
8468 -- If we have found a discriminant in the constraint 3.4(26)
8469 -- and 3.8(18) demand that no range checks are performed are
8470 -- after evaluation. If the constraint is for a component
8471 -- definition that has a per-object constraint, expressions are
8472 -- evaluated but not checked either. In all other cases perform
8473 -- a range check.
8475 else
8476 if Discrim_Present then
8477 null;
8479 elsif Nkind (Parent (Parent (Def))) = N_Component_Declaration
8480 and then
8481 Has_Per_Object_Constraint
8482 (Defining_Identifier (Parent (Parent (Def))))
8483 then
8484 null;
8486 elsif Is_Access_Type (Etype (Discr)) then
8487 Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
8489 else
8490 Apply_Range_Check (Discr_Expr (J), Etype (Discr));
8491 end if;
8493 Force_Evaluation (Discr_Expr (J));
8494 end if;
8496 -- Check that the designated type of an access discriminant's
8497 -- expression is not a class-wide type unless the discriminant's
8498 -- designated type is also class-wide.
8500 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
8501 and then not Is_Class_Wide_Type
8502 (Designated_Type (Etype (Discr)))
8503 and then Etype (Discr_Expr (J)) /= Any_Type
8504 and then Is_Class_Wide_Type
8505 (Designated_Type (Etype (Discr_Expr (J))))
8506 then
8507 Wrong_Type (Discr_Expr (J), Etype (Discr));
8509 elsif Is_Access_Type (Etype (Discr))
8510 and then not Is_Access_Constant (Etype (Discr))
8511 and then Is_Access_Type (Etype (Discr_Expr (J)))
8512 and then Is_Access_Constant (Etype (Discr_Expr (J)))
8513 then
8514 Error_Msg_NE
8515 ("constraint for discriminant& must be access to variable",
8516 Def, Discr);
8517 end if;
8518 end if;
8520 Next_Discriminant (Discr);
8521 end loop;
8523 return Elist;
8524 end Build_Discriminant_Constraints;
8526 ---------------------------------
8527 -- Build_Discriminated_Subtype --
8528 ---------------------------------
8530 procedure Build_Discriminated_Subtype
8531 (T : Entity_Id;
8532 Def_Id : Entity_Id;
8533 Elist : Elist_Id;
8534 Related_Nod : Node_Id;
8535 For_Access : Boolean := False)
8537 Has_Discrs : constant Boolean := Has_Discriminants (T);
8538 Constrained : constant Boolean :=
8539 (Has_Discrs
8540 and then not Is_Empty_Elmt_List (Elist)
8541 and then not Is_Class_Wide_Type (T))
8542 or else Is_Constrained (T);
8544 begin
8545 if Ekind (T) = E_Record_Type then
8546 if For_Access then
8547 Set_Ekind (Def_Id, E_Private_Subtype);
8548 Set_Is_For_Access_Subtype (Def_Id, True);
8549 else
8550 Set_Ekind (Def_Id, E_Record_Subtype);
8551 end if;
8553 -- Inherit preelaboration flag from base, for types for which it
8554 -- may have been set: records, private types, protected types.
8556 Set_Known_To_Have_Preelab_Init
8557 (Def_Id, Known_To_Have_Preelab_Init (T));
8559 elsif Ekind (T) = E_Task_Type then
8560 Set_Ekind (Def_Id, E_Task_Subtype);
8562 elsif Ekind (T) = E_Protected_Type then
8563 Set_Ekind (Def_Id, E_Protected_Subtype);
8564 Set_Known_To_Have_Preelab_Init
8565 (Def_Id, Known_To_Have_Preelab_Init (T));
8567 elsif Is_Private_Type (T) then
8568 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
8569 Set_Known_To_Have_Preelab_Init
8570 (Def_Id, Known_To_Have_Preelab_Init (T));
8572 elsif Is_Class_Wide_Type (T) then
8573 Set_Ekind (Def_Id, E_Class_Wide_Subtype);
8575 else
8576 -- Incomplete type. Attach subtype to list of dependents, to be
8577 -- completed with full view of parent type, unless is it the
8578 -- designated subtype of a record component within an init_proc.
8579 -- This last case arises for a component of an access type whose
8580 -- designated type is incomplete (e.g. a Taft Amendment type).
8581 -- The designated subtype is within an inner scope, and needs no
8582 -- elaboration, because only the access type is needed in the
8583 -- initialization procedure.
8585 Set_Ekind (Def_Id, Ekind (T));
8587 if For_Access and then Within_Init_Proc then
8588 null;
8589 else
8590 Append_Elmt (Def_Id, Private_Dependents (T));
8591 end if;
8592 end if;
8594 Set_Etype (Def_Id, T);
8595 Init_Size_Align (Def_Id);
8596 Set_Has_Discriminants (Def_Id, Has_Discrs);
8597 Set_Is_Constrained (Def_Id, Constrained);
8599 Set_First_Entity (Def_Id, First_Entity (T));
8600 Set_Last_Entity (Def_Id, Last_Entity (T));
8601 Set_Has_Implicit_Dereference
8602 (Def_Id, Has_Implicit_Dereference (T));
8604 -- If the subtype is the completion of a private declaration, there may
8605 -- have been representation clauses for the partial view, and they must
8606 -- be preserved. Build_Derived_Type chains the inherited clauses with
8607 -- the ones appearing on the extension. If this comes from a subtype
8608 -- declaration, all clauses are inherited.
8610 if No (First_Rep_Item (Def_Id)) then
8611 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
8612 end if;
8614 if Is_Tagged_Type (T) then
8615 Set_Is_Tagged_Type (Def_Id);
8616 Make_Class_Wide_Type (Def_Id);
8617 end if;
8619 Set_Stored_Constraint (Def_Id, No_Elist);
8621 if Has_Discrs then
8622 Set_Discriminant_Constraint (Def_Id, Elist);
8623 Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
8624 end if;
8626 if Is_Tagged_Type (T) then
8628 -- Ada 2005 (AI-251): In case of concurrent types we inherit the
8629 -- concurrent record type (which has the list of primitive
8630 -- operations).
8632 if Ada_Version >= Ada_2005
8633 and then Is_Concurrent_Type (T)
8634 then
8635 Set_Corresponding_Record_Type (Def_Id,
8636 Corresponding_Record_Type (T));
8637 else
8638 Set_Direct_Primitive_Operations (Def_Id,
8639 Direct_Primitive_Operations (T));
8640 end if;
8642 Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
8643 end if;
8645 -- Subtypes introduced by component declarations do not need to be
8646 -- marked as delayed, and do not get freeze nodes, because the semantics
8647 -- verifies that the parents of the subtypes are frozen before the
8648 -- enclosing record is frozen.
8650 if not Is_Type (Scope (Def_Id)) then
8651 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
8653 if Is_Private_Type (T)
8654 and then Present (Full_View (T))
8655 then
8656 Conditional_Delay (Def_Id, Full_View (T));
8657 else
8658 Conditional_Delay (Def_Id, T);
8659 end if;
8660 end if;
8662 if Is_Record_Type (T) then
8663 Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
8665 if Has_Discrs
8666 and then not Is_Empty_Elmt_List (Elist)
8667 and then not For_Access
8668 then
8669 Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
8670 elsif not For_Access then
8671 Set_Cloned_Subtype (Def_Id, T);
8672 end if;
8673 end if;
8674 end Build_Discriminated_Subtype;
8676 ---------------------------
8677 -- Build_Itype_Reference --
8678 ---------------------------
8680 procedure Build_Itype_Reference
8681 (Ityp : Entity_Id;
8682 Nod : Node_Id)
8684 IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
8685 begin
8687 -- Itype references are only created for use by the back-end
8689 if Inside_A_Generic then
8690 return;
8691 else
8692 Set_Itype (IR, Ityp);
8693 Insert_After (Nod, IR);
8694 end if;
8695 end Build_Itype_Reference;
8697 ------------------------
8698 -- Build_Scalar_Bound --
8699 ------------------------
8701 function Build_Scalar_Bound
8702 (Bound : Node_Id;
8703 Par_T : Entity_Id;
8704 Der_T : Entity_Id) return Node_Id
8706 New_Bound : Entity_Id;
8708 begin
8709 -- Note: not clear why this is needed, how can the original bound
8710 -- be unanalyzed at this point? and if it is, what business do we
8711 -- have messing around with it? and why is the base type of the
8712 -- parent type the right type for the resolution. It probably is
8713 -- not! It is OK for the new bound we are creating, but not for
8714 -- the old one??? Still if it never happens, no problem!
8716 Analyze_And_Resolve (Bound, Base_Type (Par_T));
8718 if Nkind_In (Bound, N_Integer_Literal, N_Real_Literal) then
8719 New_Bound := New_Copy (Bound);
8720 Set_Etype (New_Bound, Der_T);
8721 Set_Analyzed (New_Bound);
8723 elsif Is_Entity_Name (Bound) then
8724 New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
8726 -- The following is almost certainly wrong. What business do we have
8727 -- relocating a node (Bound) that is presumably still attached to
8728 -- the tree elsewhere???
8730 else
8731 New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
8732 end if;
8734 Set_Etype (New_Bound, Der_T);
8735 return New_Bound;
8736 end Build_Scalar_Bound;
8738 --------------------------------
8739 -- Build_Underlying_Full_View --
8740 --------------------------------
8742 procedure Build_Underlying_Full_View
8743 (N : Node_Id;
8744 Typ : Entity_Id;
8745 Par : Entity_Id)
8747 Loc : constant Source_Ptr := Sloc (N);
8748 Subt : constant Entity_Id :=
8749 Make_Defining_Identifier
8750 (Loc, New_External_Name (Chars (Typ), 'S'));
8752 Constr : Node_Id;
8753 Indic : Node_Id;
8754 C : Node_Id;
8755 Id : Node_Id;
8757 procedure Set_Discriminant_Name (Id : Node_Id);
8758 -- If the derived type has discriminants, they may rename discriminants
8759 -- of the parent. When building the full view of the parent, we need to
8760 -- recover the names of the original discriminants if the constraint is
8761 -- given by named associations.
8763 ---------------------------
8764 -- Set_Discriminant_Name --
8765 ---------------------------
8767 procedure Set_Discriminant_Name (Id : Node_Id) is
8768 Disc : Entity_Id;
8770 begin
8771 Set_Original_Discriminant (Id, Empty);
8773 if Has_Discriminants (Typ) then
8774 Disc := First_Discriminant (Typ);
8775 while Present (Disc) loop
8776 if Chars (Disc) = Chars (Id)
8777 and then Present (Corresponding_Discriminant (Disc))
8778 then
8779 Set_Chars (Id, Chars (Corresponding_Discriminant (Disc)));
8780 end if;
8781 Next_Discriminant (Disc);
8782 end loop;
8783 end if;
8784 end Set_Discriminant_Name;
8786 -- Start of processing for Build_Underlying_Full_View
8788 begin
8789 if Nkind (N) = N_Full_Type_Declaration then
8790 Constr := Constraint (Subtype_Indication (Type_Definition (N)));
8792 elsif Nkind (N) = N_Subtype_Declaration then
8793 Constr := New_Copy_Tree (Constraint (Subtype_Indication (N)));
8795 elsif Nkind (N) = N_Component_Declaration then
8796 Constr :=
8797 New_Copy_Tree
8798 (Constraint (Subtype_Indication (Component_Definition (N))));
8800 else
8801 raise Program_Error;
8802 end if;
8804 C := First (Constraints (Constr));
8805 while Present (C) loop
8806 if Nkind (C) = N_Discriminant_Association then
8807 Id := First (Selector_Names (C));
8808 while Present (Id) loop
8809 Set_Discriminant_Name (Id);
8810 Next (Id);
8811 end loop;
8812 end if;
8814 Next (C);
8815 end loop;
8817 Indic :=
8818 Make_Subtype_Declaration (Loc,
8819 Defining_Identifier => Subt,
8820 Subtype_Indication =>
8821 Make_Subtype_Indication (Loc,
8822 Subtype_Mark => New_Reference_To (Par, Loc),
8823 Constraint => New_Copy_Tree (Constr)));
8825 -- If this is a component subtype for an outer itype, it is not
8826 -- a list member, so simply set the parent link for analysis: if
8827 -- the enclosing type does not need to be in a declarative list,
8828 -- neither do the components.
8830 if Is_List_Member (N)
8831 and then Nkind (N) /= N_Component_Declaration
8832 then
8833 Insert_Before (N, Indic);
8834 else
8835 Set_Parent (Indic, Parent (N));
8836 end if;
8838 Analyze (Indic);
8839 Set_Underlying_Full_View (Typ, Full_View (Subt));
8840 end Build_Underlying_Full_View;
8842 -------------------------------
8843 -- Check_Abstract_Overriding --
8844 -------------------------------
8846 procedure Check_Abstract_Overriding (T : Entity_Id) is
8847 Alias_Subp : Entity_Id;
8848 Elmt : Elmt_Id;
8849 Op_List : Elist_Id;
8850 Subp : Entity_Id;
8851 Type_Def : Node_Id;
8853 procedure Check_Pragma_Implemented (Subp : Entity_Id);
8854 -- Ada 2012 (AI05-0030): Subprogram Subp overrides an interface routine
8855 -- which has pragma Implemented already set. Check whether Subp's entity
8856 -- kind conforms to the implementation kind of the overridden routine.
8858 procedure Check_Pragma_Implemented
8859 (Subp : Entity_Id;
8860 Iface_Subp : Entity_Id);
8861 -- Ada 2012 (AI05-0030): Subprogram Subp overrides interface routine
8862 -- Iface_Subp and both entities have pragma Implemented already set on
8863 -- them. Check whether the two implementation kinds are conforming.
8865 procedure Inherit_Pragma_Implemented
8866 (Subp : Entity_Id;
8867 Iface_Subp : Entity_Id);
8868 -- Ada 2012 (AI05-0030): Interface primitive Subp overrides interface
8869 -- subprogram Iface_Subp which has been marked by pragma Implemented.
8870 -- Propagate the implementation kind of Iface_Subp to Subp.
8872 ------------------------------
8873 -- Check_Pragma_Implemented --
8874 ------------------------------
8876 procedure Check_Pragma_Implemented (Subp : Entity_Id) is
8877 Iface_Alias : constant Entity_Id := Interface_Alias (Subp);
8878 Impl_Kind : constant Name_Id := Implementation_Kind (Iface_Alias);
8879 Subp_Alias : constant Entity_Id := Alias (Subp);
8880 Contr_Typ : Entity_Id;
8881 Impl_Subp : Entity_Id;
8883 begin
8884 -- Subp must have an alias since it is a hidden entity used to link
8885 -- an interface subprogram to its overriding counterpart.
8887 pragma Assert (Present (Subp_Alias));
8889 -- Handle aliases to synchronized wrappers
8891 Impl_Subp := Subp_Alias;
8893 if Is_Primitive_Wrapper (Impl_Subp) then
8894 Impl_Subp := Wrapped_Entity (Impl_Subp);
8895 end if;
8897 -- Extract the type of the controlling formal
8899 Contr_Typ := Etype (First_Formal (Subp_Alias));
8901 if Is_Concurrent_Record_Type (Contr_Typ) then
8902 Contr_Typ := Corresponding_Concurrent_Type (Contr_Typ);
8903 end if;
8905 -- An interface subprogram whose implementation kind is By_Entry must
8906 -- be implemented by an entry.
8908 if Impl_Kind = Name_By_Entry
8909 and then Ekind (Impl_Subp) /= E_Entry
8910 then
8911 Error_Msg_Node_2 := Iface_Alias;
8912 Error_Msg_NE
8913 ("type & must implement abstract subprogram & with an entry",
8914 Subp_Alias, Contr_Typ);
8916 elsif Impl_Kind = Name_By_Protected_Procedure then
8918 -- An interface subprogram whose implementation kind is By_
8919 -- Protected_Procedure cannot be implemented by a primitive
8920 -- procedure of a task type.
8922 if Ekind (Contr_Typ) /= E_Protected_Type then
8923 Error_Msg_Node_2 := Contr_Typ;
8924 Error_Msg_NE
8925 ("interface subprogram & cannot be implemented by a " &
8926 "primitive procedure of task type &", Subp_Alias,
8927 Iface_Alias);
8929 -- An interface subprogram whose implementation kind is By_
8930 -- Protected_Procedure must be implemented by a procedure.
8932 elsif Ekind (Impl_Subp) /= E_Procedure then
8933 Error_Msg_Node_2 := Iface_Alias;
8934 Error_Msg_NE
8935 ("type & must implement abstract subprogram & with a " &
8936 "procedure", Subp_Alias, Contr_Typ);
8937 end if;
8938 end if;
8939 end Check_Pragma_Implemented;
8941 ------------------------------
8942 -- Check_Pragma_Implemented --
8943 ------------------------------
8945 procedure Check_Pragma_Implemented
8946 (Subp : Entity_Id;
8947 Iface_Subp : Entity_Id)
8949 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
8950 Subp_Kind : constant Name_Id := Implementation_Kind (Subp);
8952 begin
8953 -- Ada 2012 (AI05-0030): The implementation kinds of an overridden
8954 -- and overriding subprogram are different. In general this is an
8955 -- error except when the implementation kind of the overridden
8956 -- subprograms is By_Any or Optional.
8958 if Iface_Kind /= Subp_Kind
8959 and then Iface_Kind /= Name_By_Any
8960 and then Iface_Kind /= Name_Optional
8961 then
8962 if Iface_Kind = Name_By_Entry then
8963 Error_Msg_N
8964 ("incompatible implementation kind, overridden subprogram " &
8965 "is marked By_Entry", Subp);
8966 else
8967 Error_Msg_N
8968 ("incompatible implementation kind, overridden subprogram " &
8969 "is marked By_Protected_Procedure", Subp);
8970 end if;
8971 end if;
8972 end Check_Pragma_Implemented;
8974 --------------------------------
8975 -- Inherit_Pragma_Implemented --
8976 --------------------------------
8978 procedure Inherit_Pragma_Implemented
8979 (Subp : Entity_Id;
8980 Iface_Subp : Entity_Id)
8982 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
8983 Loc : constant Source_Ptr := Sloc (Subp);
8984 Impl_Prag : Node_Id;
8986 begin
8987 -- Since the implementation kind is stored as a representation item
8988 -- rather than a flag, create a pragma node.
8990 Impl_Prag :=
8991 Make_Pragma (Loc,
8992 Chars => Name_Implemented,
8993 Pragma_Argument_Associations => New_List (
8994 Make_Pragma_Argument_Association (Loc,
8995 Expression =>
8996 New_Reference_To (Subp, Loc)),
8998 Make_Pragma_Argument_Association (Loc,
8999 Expression => Make_Identifier (Loc, Iface_Kind))));
9001 -- The pragma doesn't need to be analyzed because it is internally
9002 -- build. It is safe to directly register it as a rep item since we
9003 -- are only interested in the characters of the implementation kind.
9005 Record_Rep_Item (Subp, Impl_Prag);
9006 end Inherit_Pragma_Implemented;
9008 -- Start of processing for Check_Abstract_Overriding
9010 begin
9011 Op_List := Primitive_Operations (T);
9013 -- Loop to check primitive operations
9015 Elmt := First_Elmt (Op_List);
9016 while Present (Elmt) loop
9017 Subp := Node (Elmt);
9018 Alias_Subp := Alias (Subp);
9020 -- Inherited subprograms are identified by the fact that they do not
9021 -- come from source, and the associated source location is the
9022 -- location of the first subtype of the derived type.
9024 -- Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
9025 -- subprograms that "require overriding".
9027 -- Special exception, do not complain about failure to override the
9028 -- stream routines _Input and _Output, as well as the primitive
9029 -- operations used in dispatching selects since we always provide
9030 -- automatic overridings for these subprograms.
9032 -- Also ignore this rule for convention CIL since .NET libraries
9033 -- do bizarre things with interfaces???
9035 -- The partial view of T may have been a private extension, for
9036 -- which inherited functions dispatching on result are abstract.
9037 -- If the full view is a null extension, there is no need for
9038 -- overriding in Ada 2005, but wrappers need to be built for them
9039 -- (see exp_ch3, Build_Controlling_Function_Wrappers).
9041 if Is_Null_Extension (T)
9042 and then Has_Controlling_Result (Subp)
9043 and then Ada_Version >= Ada_2005
9044 and then Present (Alias_Subp)
9045 and then not Comes_From_Source (Subp)
9046 and then not Is_Abstract_Subprogram (Alias_Subp)
9047 and then not Is_Access_Type (Etype (Subp))
9048 then
9049 null;
9051 -- Ada 2005 (AI-251): Internal entities of interfaces need no
9052 -- processing because this check is done with the aliased
9053 -- entity
9055 elsif Present (Interface_Alias (Subp)) then
9056 null;
9058 elsif (Is_Abstract_Subprogram (Subp)
9059 or else Requires_Overriding (Subp)
9060 or else
9061 (Has_Controlling_Result (Subp)
9062 and then Present (Alias_Subp)
9063 and then not Comes_From_Source (Subp)
9064 and then Sloc (Subp) = Sloc (First_Subtype (T))))
9065 and then not Is_TSS (Subp, TSS_Stream_Input)
9066 and then not Is_TSS (Subp, TSS_Stream_Output)
9067 and then not Is_Abstract_Type (T)
9068 and then Convention (T) /= Convention_CIL
9069 and then not Is_Predefined_Interface_Primitive (Subp)
9071 -- Ada 2005 (AI-251): Do not consider hidden entities associated
9072 -- with abstract interface types because the check will be done
9073 -- with the aliased entity (otherwise we generate a duplicated
9074 -- error message).
9076 and then not Present (Interface_Alias (Subp))
9077 then
9078 if Present (Alias_Subp) then
9080 -- Only perform the check for a derived subprogram when the
9081 -- type has an explicit record extension. This avoids incorrect
9082 -- flagging of abstract subprograms for the case of a type
9083 -- without an extension that is derived from a formal type
9084 -- with a tagged actual (can occur within a private part).
9086 -- Ada 2005 (AI-391): In the case of an inherited function with
9087 -- a controlling result of the type, the rule does not apply if
9088 -- the type is a null extension (unless the parent function
9089 -- itself is abstract, in which case the function must still be
9090 -- be overridden). The expander will generate an overriding
9091 -- wrapper function calling the parent subprogram (see
9092 -- Exp_Ch3.Make_Controlling_Wrapper_Functions).
9094 Type_Def := Type_Definition (Parent (T));
9096 if Nkind (Type_Def) = N_Derived_Type_Definition
9097 and then Present (Record_Extension_Part (Type_Def))
9098 and then
9099 (Ada_Version < Ada_2005
9100 or else not Is_Null_Extension (T)
9101 or else Ekind (Subp) = E_Procedure
9102 or else not Has_Controlling_Result (Subp)
9103 or else Is_Abstract_Subprogram (Alias_Subp)
9104 or else Requires_Overriding (Subp)
9105 or else Is_Access_Type (Etype (Subp)))
9106 then
9107 -- Avoid reporting error in case of abstract predefined
9108 -- primitive inherited from interface type because the
9109 -- body of internally generated predefined primitives
9110 -- of tagged types are generated later by Freeze_Type
9112 if Is_Interface (Root_Type (T))
9113 and then Is_Abstract_Subprogram (Subp)
9114 and then Is_Predefined_Dispatching_Operation (Subp)
9115 and then not Comes_From_Source (Ultimate_Alias (Subp))
9116 then
9117 null;
9119 else
9120 Error_Msg_NE
9121 ("type must be declared abstract or & overridden",
9122 T, Subp);
9124 -- Traverse the whole chain of aliased subprograms to
9125 -- complete the error notification. This is especially
9126 -- useful for traceability of the chain of entities when
9127 -- the subprogram corresponds with an interface
9128 -- subprogram (which may be defined in another package).
9130 if Present (Alias_Subp) then
9131 declare
9132 E : Entity_Id;
9134 begin
9135 E := Subp;
9136 while Present (Alias (E)) loop
9138 -- Avoid reporting redundant errors on entities
9139 -- inherited from interfaces
9141 if Sloc (E) /= Sloc (T) then
9142 Error_Msg_Sloc := Sloc (E);
9143 Error_Msg_NE
9144 ("\& has been inherited #", T, Subp);
9145 end if;
9147 E := Alias (E);
9148 end loop;
9150 Error_Msg_Sloc := Sloc (E);
9152 -- AI05-0068: report if there is an overriding
9153 -- non-abstract subprogram that is invisible.
9155 if Is_Hidden (E)
9156 and then not Is_Abstract_Subprogram (E)
9157 then
9158 Error_Msg_NE
9159 ("\& subprogram# is not visible",
9160 T, Subp);
9162 else
9163 Error_Msg_NE
9164 ("\& has been inherited from subprogram #",
9165 T, Subp);
9166 end if;
9167 end;
9168 end if;
9169 end if;
9171 -- Ada 2005 (AI-345): Protected or task type implementing
9172 -- abstract interfaces.
9174 elsif Is_Concurrent_Record_Type (T)
9175 and then Present (Interfaces (T))
9176 then
9177 -- The controlling formal of Subp must be of mode "out",
9178 -- "in out" or an access-to-variable to be overridden.
9180 if Ekind (First_Formal (Subp)) = E_In_Parameter
9181 and then Ekind (Subp) /= E_Function
9182 then
9183 if not Is_Predefined_Dispatching_Operation (Subp)
9184 and then Is_Protected_Type
9185 (Corresponding_Concurrent_Type (T))
9186 then
9187 Error_Msg_PT (T, Subp);
9188 end if;
9190 -- Some other kind of overriding failure
9192 else
9193 Error_Msg_NE
9194 ("interface subprogram & must be overridden",
9195 T, Subp);
9197 -- Examine primitive operations of synchronized type,
9198 -- to find homonyms that have the wrong profile.
9200 declare
9201 Prim : Entity_Id;
9203 begin
9204 Prim :=
9205 First_Entity (Corresponding_Concurrent_Type (T));
9206 while Present (Prim) loop
9207 if Chars (Prim) = Chars (Subp) then
9208 Error_Msg_NE
9209 ("profile is not type conformant with "
9210 & "prefixed view profile of "
9211 & "inherited operation&", Prim, Subp);
9212 end if;
9214 Next_Entity (Prim);
9215 end loop;
9216 end;
9217 end if;
9218 end if;
9220 else
9221 Error_Msg_Node_2 := T;
9222 Error_Msg_N
9223 ("abstract subprogram& not allowed for type&", Subp);
9225 -- Also post unconditional warning on the type (unconditional
9226 -- so that if there are more than one of these cases, we get
9227 -- them all, and not just the first one).
9229 Error_Msg_Node_2 := Subp;
9230 Error_Msg_N ("nonabstract type& has abstract subprogram&!", T);
9231 end if;
9232 end if;
9234 -- Ada 2012 (AI05-0030): Perform some checks related to pragma
9235 -- Implemented
9237 -- Subp is an expander-generated procedure which maps an interface
9238 -- alias to a protected wrapper. The interface alias is flagged by
9239 -- pragma Implemented. Ensure that Subp is a procedure when the
9240 -- implementation kind is By_Protected_Procedure or an entry when
9241 -- By_Entry.
9243 if Ada_Version >= Ada_2012
9244 and then Is_Hidden (Subp)
9245 and then Present (Interface_Alias (Subp))
9246 and then Has_Rep_Pragma (Interface_Alias (Subp), Name_Implemented)
9247 then
9248 Check_Pragma_Implemented (Subp);
9249 end if;
9251 -- Subp is an interface primitive which overrides another interface
9252 -- primitive marked with pragma Implemented.
9254 if Ada_Version >= Ada_2012
9255 and then Present (Overridden_Operation (Subp))
9256 and then Has_Rep_Pragma
9257 (Overridden_Operation (Subp), Name_Implemented)
9258 then
9259 -- If the overriding routine is also marked by Implemented, check
9260 -- that the two implementation kinds are conforming.
9262 if Has_Rep_Pragma (Subp, Name_Implemented) then
9263 Check_Pragma_Implemented
9264 (Subp => Subp,
9265 Iface_Subp => Overridden_Operation (Subp));
9267 -- Otherwise the overriding routine inherits the implementation
9268 -- kind from the overridden subprogram.
9270 else
9271 Inherit_Pragma_Implemented
9272 (Subp => Subp,
9273 Iface_Subp => Overridden_Operation (Subp));
9274 end if;
9275 end if;
9277 Next_Elmt (Elmt);
9278 end loop;
9279 end Check_Abstract_Overriding;
9281 ------------------------------------------------
9282 -- Check_Access_Discriminant_Requires_Limited --
9283 ------------------------------------------------
9285 procedure Check_Access_Discriminant_Requires_Limited
9286 (D : Node_Id;
9287 Loc : Node_Id)
9289 begin
9290 -- A discriminant_specification for an access discriminant shall appear
9291 -- only in the declaration for a task or protected type, or for a type
9292 -- with the reserved word 'limited' in its definition or in one of its
9293 -- ancestors (RM 3.7(10)).
9295 -- AI-0063: The proper condition is that type must be immutably limited,
9296 -- or else be a partial view.
9298 if Nkind (Discriminant_Type (D)) = N_Access_Definition then
9299 if Is_Immutably_Limited_Type (Current_Scope)
9300 or else
9301 (Nkind (Parent (Current_Scope)) = N_Private_Type_Declaration
9302 and then Limited_Present (Parent (Current_Scope)))
9303 then
9304 null;
9306 else
9307 Error_Msg_N
9308 ("access discriminants allowed only for limited types", Loc);
9309 end if;
9310 end if;
9311 end Check_Access_Discriminant_Requires_Limited;
9313 -----------------------------------
9314 -- Check_Aliased_Component_Types --
9315 -----------------------------------
9317 procedure Check_Aliased_Component_Types (T : Entity_Id) is
9318 C : Entity_Id;
9320 begin
9321 -- ??? Also need to check components of record extensions, but not
9322 -- components of protected types (which are always limited).
9324 -- Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
9325 -- types to be unconstrained. This is safe because it is illegal to
9326 -- create access subtypes to such types with explicit discriminant
9327 -- constraints.
9329 if not Is_Limited_Type (T) then
9330 if Ekind (T) = E_Record_Type then
9331 C := First_Component (T);
9332 while Present (C) loop
9333 if Is_Aliased (C)
9334 and then Has_Discriminants (Etype (C))
9335 and then not Is_Constrained (Etype (C))
9336 and then not In_Instance_Body
9337 and then Ada_Version < Ada_2005
9338 then
9339 Error_Msg_N
9340 ("aliased component must be constrained (RM 3.6(11))",
9342 end if;
9344 Next_Component (C);
9345 end loop;
9347 elsif Ekind (T) = E_Array_Type then
9348 if Has_Aliased_Components (T)
9349 and then Has_Discriminants (Component_Type (T))
9350 and then not Is_Constrained (Component_Type (T))
9351 and then not In_Instance_Body
9352 and then Ada_Version < Ada_2005
9353 then
9354 Error_Msg_N
9355 ("aliased component type must be constrained (RM 3.6(11))",
9357 end if;
9358 end if;
9359 end if;
9360 end Check_Aliased_Component_Types;
9362 ----------------------
9363 -- Check_Completion --
9364 ----------------------
9366 procedure Check_Completion (Body_Id : Node_Id := Empty) is
9367 E : Entity_Id;
9369 procedure Post_Error;
9370 -- Post error message for lack of completion for entity E
9372 ----------------
9373 -- Post_Error --
9374 ----------------
9376 procedure Post_Error is
9378 procedure Missing_Body;
9379 -- Output missing body message
9381 ------------------
9382 -- Missing_Body --
9383 ------------------
9385 procedure Missing_Body is
9386 begin
9387 -- Spec is in same unit, so we can post on spec
9389 if In_Same_Source_Unit (Body_Id, E) then
9390 Error_Msg_N ("missing body for &", E);
9392 -- Spec is in a separate unit, so we have to post on the body
9394 else
9395 Error_Msg_NE ("missing body for & declared#!", Body_Id, E);
9396 end if;
9397 end Missing_Body;
9399 -- Start of processing for Post_Error
9401 begin
9402 if not Comes_From_Source (E) then
9404 if Ekind_In (E, E_Task_Type, E_Protected_Type) then
9405 -- It may be an anonymous protected type created for a
9406 -- single variable. Post error on variable, if present.
9408 declare
9409 Var : Entity_Id;
9411 begin
9412 Var := First_Entity (Current_Scope);
9413 while Present (Var) loop
9414 exit when Etype (Var) = E
9415 and then Comes_From_Source (Var);
9417 Next_Entity (Var);
9418 end loop;
9420 if Present (Var) then
9421 E := Var;
9422 end if;
9423 end;
9424 end if;
9425 end if;
9427 -- If a generated entity has no completion, then either previous
9428 -- semantic errors have disabled the expansion phase, or else we had
9429 -- missing subunits, or else we are compiling without expansion,
9430 -- or else something is very wrong.
9432 if not Comes_From_Source (E) then
9433 pragma Assert
9434 (Serious_Errors_Detected > 0
9435 or else Configurable_Run_Time_Violations > 0
9436 or else Subunits_Missing
9437 or else not Expander_Active);
9438 return;
9440 -- Here for source entity
9442 else
9443 -- Here if no body to post the error message, so we post the error
9444 -- on the declaration that has no completion. This is not really
9445 -- the right place to post it, think about this later ???
9447 if No (Body_Id) then
9448 if Is_Type (E) then
9449 Error_Msg_NE
9450 ("missing full declaration for }", Parent (E), E);
9451 else
9452 Error_Msg_NE ("missing body for &", Parent (E), E);
9453 end if;
9455 -- Package body has no completion for a declaration that appears
9456 -- in the corresponding spec. Post error on the body, with a
9457 -- reference to the non-completed declaration.
9459 else
9460 Error_Msg_Sloc := Sloc (E);
9462 if Is_Type (E) then
9463 Error_Msg_NE ("missing full declaration for }!", Body_Id, E);
9465 elsif Is_Overloadable (E)
9466 and then Current_Entity_In_Scope (E) /= E
9467 then
9468 -- It may be that the completion is mistyped and appears as
9469 -- a distinct overloading of the entity.
9471 declare
9472 Candidate : constant Entity_Id :=
9473 Current_Entity_In_Scope (E);
9474 Decl : constant Node_Id :=
9475 Unit_Declaration_Node (Candidate);
9477 begin
9478 if Is_Overloadable (Candidate)
9479 and then Ekind (Candidate) = Ekind (E)
9480 and then Nkind (Decl) = N_Subprogram_Body
9481 and then Acts_As_Spec (Decl)
9482 then
9483 Check_Type_Conformant (Candidate, E);
9485 else
9486 Missing_Body;
9487 end if;
9488 end;
9490 else
9491 Missing_Body;
9492 end if;
9493 end if;
9494 end if;
9495 end Post_Error;
9497 -- Start of processing for Check_Completion
9499 begin
9500 E := First_Entity (Current_Scope);
9501 while Present (E) loop
9502 if Is_Intrinsic_Subprogram (E) then
9503 null;
9505 -- The following situation requires special handling: a child unit
9506 -- that appears in the context clause of the body of its parent:
9508 -- procedure Parent.Child (...);
9510 -- with Parent.Child;
9511 -- package body Parent is
9513 -- Here Parent.Child appears as a local entity, but should not be
9514 -- flagged as requiring completion, because it is a compilation
9515 -- unit.
9517 -- Ignore missing completion for a subprogram that does not come from
9518 -- source (including the _Call primitive operation of RAS types,
9519 -- which has to have the flag Comes_From_Source for other purposes):
9520 -- we assume that the expander will provide the missing completion.
9521 -- In case of previous errors, other expansion actions that provide
9522 -- bodies for null procedures with not be invoked, so inhibit message
9523 -- in those cases.
9525 -- Note that E_Operator is not in the list that follows, because
9526 -- this kind is reserved for predefined operators, that are
9527 -- intrinsic and do not need completion.
9529 elsif Ekind (E) = E_Function
9530 or else Ekind (E) = E_Procedure
9531 or else Ekind (E) = E_Generic_Function
9532 or else Ekind (E) = E_Generic_Procedure
9533 then
9534 if Has_Completion (E) then
9535 null;
9537 elsif Is_Subprogram (E) and then Is_Abstract_Subprogram (E) then
9538 null;
9540 elsif Is_Subprogram (E)
9541 and then (not Comes_From_Source (E)
9542 or else Chars (E) = Name_uCall)
9543 then
9544 null;
9546 elsif
9547 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
9548 then
9549 null;
9551 elsif Nkind (Parent (E)) = N_Procedure_Specification
9552 and then Null_Present (Parent (E))
9553 and then Serious_Errors_Detected > 0
9554 then
9555 null;
9557 else
9558 Post_Error;
9559 end if;
9561 elsif Is_Entry (E) then
9562 if not Has_Completion (E) and then
9563 (Ekind (Scope (E)) = E_Protected_Object
9564 or else Ekind (Scope (E)) = E_Protected_Type)
9565 then
9566 Post_Error;
9567 end if;
9569 elsif Is_Package_Or_Generic_Package (E) then
9570 if Unit_Requires_Body (E) then
9571 if not Has_Completion (E)
9572 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
9573 N_Compilation_Unit
9574 then
9575 Post_Error;
9576 end if;
9578 elsif not Is_Child_Unit (E) then
9579 May_Need_Implicit_Body (E);
9580 end if;
9582 -- A formal incomplete type (Ada 2012) does not require a completion;
9583 -- other incomplete type declarations do.
9585 elsif Ekind (E) = E_Incomplete_Type
9586 and then No (Underlying_Type (E))
9587 and then not Is_Generic_Type (E)
9588 then
9589 Post_Error;
9591 elsif (Ekind (E) = E_Task_Type or else
9592 Ekind (E) = E_Protected_Type)
9593 and then not Has_Completion (E)
9594 then
9595 Post_Error;
9597 -- A single task declared in the current scope is a constant, verify
9598 -- that the body of its anonymous type is in the same scope. If the
9599 -- task is defined elsewhere, this may be a renaming declaration for
9600 -- which no completion is needed.
9602 elsif Ekind (E) = E_Constant
9603 and then Ekind (Etype (E)) = E_Task_Type
9604 and then not Has_Completion (Etype (E))
9605 and then Scope (Etype (E)) = Current_Scope
9606 then
9607 Post_Error;
9609 elsif Ekind (E) = E_Protected_Object
9610 and then not Has_Completion (Etype (E))
9611 then
9612 Post_Error;
9614 elsif Ekind (E) = E_Record_Type then
9615 if Is_Tagged_Type (E) then
9616 Check_Abstract_Overriding (E);
9617 Check_Conventions (E);
9618 end if;
9620 Check_Aliased_Component_Types (E);
9622 elsif Ekind (E) = E_Array_Type then
9623 Check_Aliased_Component_Types (E);
9625 end if;
9627 Next_Entity (E);
9628 end loop;
9629 end Check_Completion;
9631 ------------------------------------
9632 -- Check_CPP_Type_Has_No_Defaults --
9633 ------------------------------------
9635 procedure Check_CPP_Type_Has_No_Defaults (T : Entity_Id) is
9636 Tdef : constant Node_Id := Type_Definition (Declaration_Node (T));
9637 Clist : Node_Id;
9638 Comp : Node_Id;
9640 begin
9641 -- Obtain the component list
9643 if Nkind (Tdef) = N_Record_Definition then
9644 Clist := Component_List (Tdef);
9645 else pragma Assert (Nkind (Tdef) = N_Derived_Type_Definition);
9646 Clist := Component_List (Record_Extension_Part (Tdef));
9647 end if;
9649 -- Check all components to ensure no default expressions
9651 if Present (Clist) then
9652 Comp := First (Component_Items (Clist));
9653 while Present (Comp) loop
9654 if Present (Expression (Comp)) then
9655 Error_Msg_N
9656 ("component of imported 'C'P'P type cannot have "
9657 & "default expression", Expression (Comp));
9658 end if;
9660 Next (Comp);
9661 end loop;
9662 end if;
9663 end Check_CPP_Type_Has_No_Defaults;
9665 ----------------------------
9666 -- Check_Delta_Expression --
9667 ----------------------------
9669 procedure Check_Delta_Expression (E : Node_Id) is
9670 begin
9671 if not (Is_Real_Type (Etype (E))) then
9672 Wrong_Type (E, Any_Real);
9674 elsif not Is_OK_Static_Expression (E) then
9675 Flag_Non_Static_Expr
9676 ("non-static expression used for delta value!", E);
9678 elsif not UR_Is_Positive (Expr_Value_R (E)) then
9679 Error_Msg_N ("delta expression must be positive", E);
9681 else
9682 return;
9683 end if;
9685 -- If any of above errors occurred, then replace the incorrect
9686 -- expression by the real 0.1, which should prevent further errors.
9688 Rewrite (E,
9689 Make_Real_Literal (Sloc (E), Ureal_Tenth));
9690 Analyze_And_Resolve (E, Standard_Float);
9691 end Check_Delta_Expression;
9693 -----------------------------
9694 -- Check_Digits_Expression --
9695 -----------------------------
9697 procedure Check_Digits_Expression (E : Node_Id) is
9698 begin
9699 if not (Is_Integer_Type (Etype (E))) then
9700 Wrong_Type (E, Any_Integer);
9702 elsif not Is_OK_Static_Expression (E) then
9703 Flag_Non_Static_Expr
9704 ("non-static expression used for digits value!", E);
9706 elsif Expr_Value (E) <= 0 then
9707 Error_Msg_N ("digits value must be greater than zero", E);
9709 else
9710 return;
9711 end if;
9713 -- If any of above errors occurred, then replace the incorrect
9714 -- expression by the integer 1, which should prevent further errors.
9716 Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
9717 Analyze_And_Resolve (E, Standard_Integer);
9719 end Check_Digits_Expression;
9721 --------------------------
9722 -- Check_Initialization --
9723 --------------------------
9725 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
9726 begin
9727 if Is_Limited_Type (T)
9728 and then not In_Instance
9729 and then not In_Inlined_Body
9730 then
9731 if not OK_For_Limited_Init (T, Exp) then
9733 -- In GNAT mode, this is just a warning, to allow it to be evilly
9734 -- turned off. Otherwise it is a real error.
9736 if GNAT_Mode then
9737 Error_Msg_N
9738 ("?cannot initialize entities of limited type!", Exp);
9740 elsif Ada_Version < Ada_2005 then
9742 -- The side effect removal machinery may generate illegal Ada
9743 -- code to avoid the usage of access types and 'reference in
9744 -- Alfa mode. Since this is legal code with respect to theorem
9745 -- proving, do not emit the error.
9747 if Alfa_Mode
9748 and then Nkind (Exp) = N_Function_Call
9749 and then Nkind (Parent (Exp)) = N_Object_Declaration
9750 and then not Comes_From_Source
9751 (Defining_Identifier (Parent (Exp)))
9752 then
9753 null;
9755 else
9756 Error_Msg_N
9757 ("cannot initialize entities of limited type", Exp);
9758 Explain_Limited_Type (T, Exp);
9759 end if;
9761 else
9762 -- Specialize error message according to kind of illegal
9763 -- initial expression.
9765 if Nkind (Exp) = N_Type_Conversion
9766 and then Nkind (Expression (Exp)) = N_Function_Call
9767 then
9768 Error_Msg_N
9769 ("illegal context for call"
9770 & " to function with limited result", Exp);
9772 else
9773 Error_Msg_N
9774 ("initialization of limited object requires aggregate "
9775 & "or function call", Exp);
9776 end if;
9777 end if;
9778 end if;
9779 end if;
9780 end Check_Initialization;
9782 ----------------------
9783 -- Check_Interfaces --
9784 ----------------------
9786 procedure Check_Interfaces (N : Node_Id; Def : Node_Id) is
9787 Parent_Type : constant Entity_Id := Etype (Defining_Identifier (N));
9789 Iface : Node_Id;
9790 Iface_Def : Node_Id;
9791 Iface_Typ : Entity_Id;
9792 Parent_Node : Node_Id;
9794 Is_Task : Boolean := False;
9795 -- Set True if parent type or any progenitor is a task interface
9797 Is_Protected : Boolean := False;
9798 -- Set True if parent type or any progenitor is a protected interface
9800 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
9801 -- Check that a progenitor is compatible with declaration.
9802 -- Error is posted on Error_Node.
9804 ------------------
9805 -- Check_Ifaces --
9806 ------------------
9808 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
9809 Iface_Id : constant Entity_Id :=
9810 Defining_Identifier (Parent (Iface_Def));
9811 Type_Def : Node_Id;
9813 begin
9814 if Nkind (N) = N_Private_Extension_Declaration then
9815 Type_Def := N;
9816 else
9817 Type_Def := Type_Definition (N);
9818 end if;
9820 if Is_Task_Interface (Iface_Id) then
9821 Is_Task := True;
9823 elsif Is_Protected_Interface (Iface_Id) then
9824 Is_Protected := True;
9825 end if;
9827 if Is_Synchronized_Interface (Iface_Id) then
9829 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
9830 -- extension derived from a synchronized interface must explicitly
9831 -- be declared synchronized, because the full view will be a
9832 -- synchronized type.
9834 if Nkind (N) = N_Private_Extension_Declaration then
9835 if not Synchronized_Present (N) then
9836 Error_Msg_NE
9837 ("private extension of& must be explicitly synchronized",
9838 N, Iface_Id);
9839 end if;
9841 -- However, by 3.9.4(16/2), a full type that is a record extension
9842 -- is never allowed to derive from a synchronized interface (note
9843 -- that interfaces must be excluded from this check, because those
9844 -- are represented by derived type definitions in some cases).
9846 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition
9847 and then not Interface_Present (Type_Definition (N))
9848 then
9849 Error_Msg_N ("record extension cannot derive from synchronized"
9850 & " interface", Error_Node);
9851 end if;
9852 end if;
9854 -- Check that the characteristics of the progenitor are compatible
9855 -- with the explicit qualifier in the declaration.
9856 -- The check only applies to qualifiers that come from source.
9857 -- Limited_Present also appears in the declaration of corresponding
9858 -- records, and the check does not apply to them.
9860 if Limited_Present (Type_Def)
9861 and then not
9862 Is_Concurrent_Record_Type (Defining_Identifier (N))
9863 then
9864 if Is_Limited_Interface (Parent_Type)
9865 and then not Is_Limited_Interface (Iface_Id)
9866 then
9867 Error_Msg_NE
9868 ("progenitor& must be limited interface",
9869 Error_Node, Iface_Id);
9871 elsif
9872 (Task_Present (Iface_Def)
9873 or else Protected_Present (Iface_Def)
9874 or else Synchronized_Present (Iface_Def))
9875 and then Nkind (N) /= N_Private_Extension_Declaration
9876 and then not Error_Posted (N)
9877 then
9878 Error_Msg_NE
9879 ("progenitor& must be limited interface",
9880 Error_Node, Iface_Id);
9881 end if;
9883 -- Protected interfaces can only inherit from limited, synchronized
9884 -- or protected interfaces.
9886 elsif Nkind (N) = N_Full_Type_Declaration
9887 and then Protected_Present (Type_Def)
9888 then
9889 if Limited_Present (Iface_Def)
9890 or else Synchronized_Present (Iface_Def)
9891 or else Protected_Present (Iface_Def)
9892 then
9893 null;
9895 elsif Task_Present (Iface_Def) then
9896 Error_Msg_N ("(Ada 2005) protected interface cannot inherit"
9897 & " from task interface", Error_Node);
9899 else
9900 Error_Msg_N ("(Ada 2005) protected interface cannot inherit"
9901 & " from non-limited interface", Error_Node);
9902 end if;
9904 -- Ada 2005 (AI-345): Synchronized interfaces can only inherit from
9905 -- limited and synchronized.
9907 elsif Synchronized_Present (Type_Def) then
9908 if Limited_Present (Iface_Def)
9909 or else Synchronized_Present (Iface_Def)
9910 then
9911 null;
9913 elsif Protected_Present (Iface_Def)
9914 and then Nkind (N) /= N_Private_Extension_Declaration
9915 then
9916 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
9917 & " from protected interface", Error_Node);
9919 elsif Task_Present (Iface_Def)
9920 and then Nkind (N) /= N_Private_Extension_Declaration
9921 then
9922 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
9923 & " from task interface", Error_Node);
9925 elsif not Is_Limited_Interface (Iface_Id) then
9926 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
9927 & " from non-limited interface", Error_Node);
9928 end if;
9930 -- Ada 2005 (AI-345): Task interfaces can only inherit from limited,
9931 -- synchronized or task interfaces.
9933 elsif Nkind (N) = N_Full_Type_Declaration
9934 and then Task_Present (Type_Def)
9935 then
9936 if Limited_Present (Iface_Def)
9937 or else Synchronized_Present (Iface_Def)
9938 or else Task_Present (Iface_Def)
9939 then
9940 null;
9942 elsif Protected_Present (Iface_Def) then
9943 Error_Msg_N ("(Ada 2005) task interface cannot inherit from"
9944 & " protected interface", Error_Node);
9946 else
9947 Error_Msg_N ("(Ada 2005) task interface cannot inherit from"
9948 & " non-limited interface", Error_Node);
9949 end if;
9950 end if;
9951 end Check_Ifaces;
9953 -- Start of processing for Check_Interfaces
9955 begin
9956 if Is_Interface (Parent_Type) then
9957 if Is_Task_Interface (Parent_Type) then
9958 Is_Task := True;
9960 elsif Is_Protected_Interface (Parent_Type) then
9961 Is_Protected := True;
9962 end if;
9963 end if;
9965 if Nkind (N) = N_Private_Extension_Declaration then
9967 -- Check that progenitors are compatible with declaration
9969 Iface := First (Interface_List (Def));
9970 while Present (Iface) loop
9971 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
9973 Parent_Node := Parent (Base_Type (Iface_Typ));
9974 Iface_Def := Type_Definition (Parent_Node);
9976 if not Is_Interface (Iface_Typ) then
9977 Diagnose_Interface (Iface, Iface_Typ);
9979 else
9980 Check_Ifaces (Iface_Def, Iface);
9981 end if;
9983 Next (Iface);
9984 end loop;
9986 if Is_Task and Is_Protected then
9987 Error_Msg_N
9988 ("type cannot derive from task and protected interface", N);
9989 end if;
9991 return;
9992 end if;
9994 -- Full type declaration of derived type.
9995 -- Check compatibility with parent if it is interface type
9997 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
9998 and then Is_Interface (Parent_Type)
9999 then
10000 Parent_Node := Parent (Parent_Type);
10002 -- More detailed checks for interface varieties
10004 Check_Ifaces
10005 (Iface_Def => Type_Definition (Parent_Node),
10006 Error_Node => Subtype_Indication (Type_Definition (N)));
10007 end if;
10009 Iface := First (Interface_List (Def));
10010 while Present (Iface) loop
10011 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
10013 Parent_Node := Parent (Base_Type (Iface_Typ));
10014 Iface_Def := Type_Definition (Parent_Node);
10016 if not Is_Interface (Iface_Typ) then
10017 Diagnose_Interface (Iface, Iface_Typ);
10019 else
10020 -- "The declaration of a specific descendant of an interface
10021 -- type freezes the interface type" RM 13.14
10023 Freeze_Before (N, Iface_Typ);
10024 Check_Ifaces (Iface_Def, Error_Node => Iface);
10025 end if;
10027 Next (Iface);
10028 end loop;
10030 if Is_Task and Is_Protected then
10031 Error_Msg_N
10032 ("type cannot derive from task and protected interface", N);
10033 end if;
10034 end Check_Interfaces;
10036 ------------------------------------
10037 -- Check_Or_Process_Discriminants --
10038 ------------------------------------
10040 -- If an incomplete or private type declaration was already given for the
10041 -- type, the discriminants may have already been processed if they were
10042 -- present on the incomplete declaration. In this case a full conformance
10043 -- check has been performed in Find_Type_Name, and we then recheck here
10044 -- some properties that can't be checked on the partial view alone.
10045 -- Otherwise we call Process_Discriminants.
10047 procedure Check_Or_Process_Discriminants
10048 (N : Node_Id;
10049 T : Entity_Id;
10050 Prev : Entity_Id := Empty)
10052 begin
10053 if Has_Discriminants (T) then
10055 -- Discriminants are already set on T if they were already present
10056 -- on the partial view. Make them visible to component declarations.
10058 declare
10059 D : Entity_Id;
10060 -- Discriminant on T (full view) referencing expr on partial view
10062 Prev_D : Entity_Id;
10063 -- Entity of corresponding discriminant on partial view
10065 New_D : Node_Id;
10066 -- Discriminant specification for full view, expression is the
10067 -- syntactic copy on full view (which has been checked for
10068 -- conformance with partial view), only used here to post error
10069 -- message.
10071 begin
10072 D := First_Discriminant (T);
10073 New_D := First (Discriminant_Specifications (N));
10074 while Present (D) loop
10075 Prev_D := Current_Entity (D);
10076 Set_Current_Entity (D);
10077 Set_Is_Immediately_Visible (D);
10078 Set_Homonym (D, Prev_D);
10080 -- Handle the case where there is an untagged partial view and
10081 -- the full view is tagged: must disallow discriminants with
10082 -- defaults, unless compiling for Ada 2012, which allows a
10083 -- limited tagged type to have defaulted discriminants (see
10084 -- AI05-0214). However, suppress the error here if it was
10085 -- already reported on the default expression of the partial
10086 -- view.
10088 if Is_Tagged_Type (T)
10089 and then Present (Expression (Parent (D)))
10090 and then (not Is_Limited_Type (Current_Scope)
10091 or else Ada_Version < Ada_2012)
10092 and then not Error_Posted (Expression (Parent (D)))
10093 then
10094 if Ada_Version >= Ada_2012 then
10095 Error_Msg_N
10096 ("discriminants of nonlimited tagged type cannot have"
10097 & " defaults",
10098 Expression (New_D));
10099 else
10100 Error_Msg_N
10101 ("discriminants of tagged type cannot have defaults",
10102 Expression (New_D));
10103 end if;
10104 end if;
10106 -- Ada 2005 (AI-230): Access discriminant allowed in
10107 -- non-limited record types.
10109 if Ada_Version < Ada_2005 then
10111 -- This restriction gets applied to the full type here. It
10112 -- has already been applied earlier to the partial view.
10114 Check_Access_Discriminant_Requires_Limited (Parent (D), N);
10115 end if;
10117 Next_Discriminant (D);
10118 Next (New_D);
10119 end loop;
10120 end;
10122 elsif Present (Discriminant_Specifications (N)) then
10123 Process_Discriminants (N, Prev);
10124 end if;
10125 end Check_Or_Process_Discriminants;
10127 ----------------------
10128 -- Check_Real_Bound --
10129 ----------------------
10131 procedure Check_Real_Bound (Bound : Node_Id) is
10132 begin
10133 if not Is_Real_Type (Etype (Bound)) then
10134 Error_Msg_N
10135 ("bound in real type definition must be of real type", Bound);
10137 elsif not Is_OK_Static_Expression (Bound) then
10138 Flag_Non_Static_Expr
10139 ("non-static expression used for real type bound!", Bound);
10141 else
10142 return;
10143 end if;
10145 Rewrite
10146 (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
10147 Analyze (Bound);
10148 Resolve (Bound, Standard_Float);
10149 end Check_Real_Bound;
10151 ------------------------------
10152 -- Complete_Private_Subtype --
10153 ------------------------------
10155 procedure Complete_Private_Subtype
10156 (Priv : Entity_Id;
10157 Full : Entity_Id;
10158 Full_Base : Entity_Id;
10159 Related_Nod : Node_Id)
10161 Save_Next_Entity : Entity_Id;
10162 Save_Homonym : Entity_Id;
10164 begin
10165 -- Set semantic attributes for (implicit) private subtype completion.
10166 -- If the full type has no discriminants, then it is a copy of the full
10167 -- view of the base. Otherwise, it is a subtype of the base with a
10168 -- possible discriminant constraint. Save and restore the original
10169 -- Next_Entity field of full to ensure that the calls to Copy_Node
10170 -- do not corrupt the entity chain.
10172 -- Note that the type of the full view is the same entity as the type of
10173 -- the partial view. In this fashion, the subtype has access to the
10174 -- correct view of the parent.
10176 Save_Next_Entity := Next_Entity (Full);
10177 Save_Homonym := Homonym (Priv);
10179 case Ekind (Full_Base) is
10180 when E_Record_Type |
10181 E_Record_Subtype |
10182 Class_Wide_Kind |
10183 Private_Kind |
10184 Task_Kind |
10185 Protected_Kind =>
10186 Copy_Node (Priv, Full);
10188 Set_Has_Discriminants (Full, Has_Discriminants (Full_Base));
10189 Set_First_Entity (Full, First_Entity (Full_Base));
10190 Set_Last_Entity (Full, Last_Entity (Full_Base));
10192 when others =>
10193 Copy_Node (Full_Base, Full);
10194 Set_Chars (Full, Chars (Priv));
10195 Conditional_Delay (Full, Priv);
10196 Set_Sloc (Full, Sloc (Priv));
10197 end case;
10199 Set_Next_Entity (Full, Save_Next_Entity);
10200 Set_Homonym (Full, Save_Homonym);
10201 Set_Associated_Node_For_Itype (Full, Related_Nod);
10203 -- Set common attributes for all subtypes: kind, convention, etc.
10205 Set_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
10206 Set_Convention (Full, Convention (Full_Base));
10208 -- The Etype of the full view is inconsistent. Gigi needs to see the
10209 -- structural full view, which is what the current scheme gives:
10210 -- the Etype of the full view is the etype of the full base. However,
10211 -- if the full base is a derived type, the full view then looks like
10212 -- a subtype of the parent, not a subtype of the full base. If instead
10213 -- we write:
10215 -- Set_Etype (Full, Full_Base);
10217 -- then we get inconsistencies in the front-end (confusion between
10218 -- views). Several outstanding bugs are related to this ???
10220 Set_Is_First_Subtype (Full, False);
10221 Set_Scope (Full, Scope (Priv));
10222 Set_Size_Info (Full, Full_Base);
10223 Set_RM_Size (Full, RM_Size (Full_Base));
10224 Set_Is_Itype (Full);
10226 -- A subtype of a private-type-without-discriminants, whose full-view
10227 -- has discriminants with default expressions, is not constrained!
10229 if not Has_Discriminants (Priv) then
10230 Set_Is_Constrained (Full, Is_Constrained (Full_Base));
10232 if Has_Discriminants (Full_Base) then
10233 Set_Discriminant_Constraint
10234 (Full, Discriminant_Constraint (Full_Base));
10236 -- The partial view may have been indefinite, the full view
10237 -- might not be.
10239 Set_Has_Unknown_Discriminants
10240 (Full, Has_Unknown_Discriminants (Full_Base));
10241 end if;
10242 end if;
10244 Set_First_Rep_Item (Full, First_Rep_Item (Full_Base));
10245 Set_Depends_On_Private (Full, Has_Private_Component (Full));
10247 -- Freeze the private subtype entity if its parent is delayed, and not
10248 -- already frozen. We skip this processing if the type is an anonymous
10249 -- subtype of a record component, or is the corresponding record of a
10250 -- protected type, since ???
10252 if not Is_Type (Scope (Full)) then
10253 Set_Has_Delayed_Freeze (Full,
10254 Has_Delayed_Freeze (Full_Base)
10255 and then (not Is_Frozen (Full_Base)));
10256 end if;
10258 Set_Freeze_Node (Full, Empty);
10259 Set_Is_Frozen (Full, False);
10260 Set_Full_View (Priv, Full);
10262 if Has_Discriminants (Full) then
10263 Set_Stored_Constraint_From_Discriminant_Constraint (Full);
10264 Set_Stored_Constraint (Priv, Stored_Constraint (Full));
10266 if Has_Unknown_Discriminants (Full) then
10267 Set_Discriminant_Constraint (Full, No_Elist);
10268 end if;
10269 end if;
10271 if Ekind (Full_Base) = E_Record_Type
10272 and then Has_Discriminants (Full_Base)
10273 and then Has_Discriminants (Priv) -- might not, if errors
10274 and then not Has_Unknown_Discriminants (Priv)
10275 and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
10276 then
10277 Create_Constrained_Components
10278 (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
10280 -- If the full base is itself derived from private, build a congruent
10281 -- subtype of its underlying type, for use by the back end. For a
10282 -- constrained record component, the declaration cannot be placed on
10283 -- the component list, but it must nevertheless be built an analyzed, to
10284 -- supply enough information for Gigi to compute the size of component.
10286 elsif Ekind (Full_Base) in Private_Kind
10287 and then Is_Derived_Type (Full_Base)
10288 and then Has_Discriminants (Full_Base)
10289 and then (Ekind (Current_Scope) /= E_Record_Subtype)
10290 then
10291 if not Is_Itype (Priv)
10292 and then
10293 Nkind (Subtype_Indication (Parent (Priv))) = N_Subtype_Indication
10294 then
10295 Build_Underlying_Full_View
10296 (Parent (Priv), Full, Etype (Full_Base));
10298 elsif Nkind (Related_Nod) = N_Component_Declaration then
10299 Build_Underlying_Full_View (Related_Nod, Full, Etype (Full_Base));
10300 end if;
10302 elsif Is_Record_Type (Full_Base) then
10304 -- Show Full is simply a renaming of Full_Base
10306 Set_Cloned_Subtype (Full, Full_Base);
10307 end if;
10309 -- It is unsafe to share to bounds of a scalar type, because the Itype
10310 -- is elaborated on demand, and if a bound is non-static then different
10311 -- orders of elaboration in different units will lead to different
10312 -- external symbols.
10314 if Is_Scalar_Type (Full_Base) then
10315 Set_Scalar_Range (Full,
10316 Make_Range (Sloc (Related_Nod),
10317 Low_Bound =>
10318 Duplicate_Subexpr_No_Checks (Type_Low_Bound (Full_Base)),
10319 High_Bound =>
10320 Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
10322 -- This completion inherits the bounds of the full parent, but if
10323 -- the parent is an unconstrained floating point type, so is the
10324 -- completion.
10326 if Is_Floating_Point_Type (Full_Base) then
10327 Set_Includes_Infinities
10328 (Scalar_Range (Full), Has_Infinities (Full_Base));
10329 end if;
10330 end if;
10332 -- ??? It seems that a lot of fields are missing that should be copied
10333 -- from Full_Base to Full. Here are some that are introduced in a
10334 -- non-disruptive way but a cleanup is necessary.
10336 if Is_Tagged_Type (Full_Base) then
10337 Set_Is_Tagged_Type (Full);
10338 Set_Direct_Primitive_Operations (Full,
10339 Direct_Primitive_Operations (Full_Base));
10341 -- Inherit class_wide type of full_base in case the partial view was
10342 -- not tagged. Otherwise it has already been created when the private
10343 -- subtype was analyzed.
10345 if No (Class_Wide_Type (Full)) then
10346 Set_Class_Wide_Type (Full, Class_Wide_Type (Full_Base));
10347 end if;
10349 -- If this is a subtype of a protected or task type, constrain its
10350 -- corresponding record, unless this is a subtype without constraints,
10351 -- i.e. a simple renaming as with an actual subtype in an instance.
10353 elsif Is_Concurrent_Type (Full_Base) then
10354 if Has_Discriminants (Full)
10355 and then Present (Corresponding_Record_Type (Full_Base))
10356 and then
10357 not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
10358 then
10359 Set_Corresponding_Record_Type (Full,
10360 Constrain_Corresponding_Record
10361 (Full, Corresponding_Record_Type (Full_Base),
10362 Related_Nod, Full_Base));
10364 else
10365 Set_Corresponding_Record_Type (Full,
10366 Corresponding_Record_Type (Full_Base));
10367 end if;
10368 end if;
10370 -- Link rep item chain, and also setting of Has_Predicates from private
10371 -- subtype to full subtype, since we will need these on the full subtype
10372 -- to create the predicate function. Note that the full subtype may
10373 -- already have rep items, inherited from the full view of the base
10374 -- type, so we must be sure not to overwrite these entries.
10376 declare
10377 Append : Boolean;
10378 Item : Node_Id;
10379 Next_Item : Node_Id;
10381 begin
10382 Item := First_Rep_Item (Full);
10384 -- If no existing rep items on full type, we can just link directly
10385 -- to the list of items on the private type.
10387 if No (Item) then
10388 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
10390 -- Otherwise, search to the end of items currently linked to the full
10391 -- subtype and append the private items to the end. However, if Priv
10392 -- and Full already have the same list of rep items, then the append
10393 -- is not done, as that would create a circularity.
10395 elsif Item /= First_Rep_Item (Priv) then
10396 Append := True;
10398 loop
10399 Next_Item := Next_Rep_Item (Item);
10400 exit when No (Next_Item);
10401 Item := Next_Item;
10403 -- If the private view has aspect specifications, the full view
10404 -- inherits them. Since these aspects may already have been
10405 -- attached to the full view during derivation, do not append
10406 -- them if already present.
10408 if Item = First_Rep_Item (Priv) then
10409 Append := False;
10410 exit;
10411 end if;
10412 end loop;
10414 -- And link the private type items at the end of the chain
10416 if Append then
10417 Set_Next_Rep_Item (Item, First_Rep_Item (Priv));
10418 end if;
10419 end if;
10420 end;
10422 -- Make sure Has_Predicates is set on full type if it is set on the
10423 -- private type. Note that it may already be set on the full type and
10424 -- if so, we don't want to unset it.
10426 if Has_Predicates (Priv) then
10427 Set_Has_Predicates (Full);
10428 end if;
10429 end Complete_Private_Subtype;
10431 ----------------------------
10432 -- Constant_Redeclaration --
10433 ----------------------------
10435 procedure Constant_Redeclaration
10436 (Id : Entity_Id;
10437 N : Node_Id;
10438 T : out Entity_Id)
10440 Prev : constant Entity_Id := Current_Entity_In_Scope (Id);
10441 Obj_Def : constant Node_Id := Object_Definition (N);
10442 New_T : Entity_Id;
10444 procedure Check_Possible_Deferred_Completion
10445 (Prev_Id : Entity_Id;
10446 Prev_Obj_Def : Node_Id;
10447 Curr_Obj_Def : Node_Id);
10448 -- Determine whether the two object definitions describe the partial
10449 -- and the full view of a constrained deferred constant. Generate
10450 -- a subtype for the full view and verify that it statically matches
10451 -- the subtype of the partial view.
10453 procedure Check_Recursive_Declaration (Typ : Entity_Id);
10454 -- If deferred constant is an access type initialized with an allocator,
10455 -- check whether there is an illegal recursion in the definition,
10456 -- through a default value of some record subcomponent. This is normally
10457 -- detected when generating init procs, but requires this additional
10458 -- mechanism when expansion is disabled.
10460 ----------------------------------------
10461 -- Check_Possible_Deferred_Completion --
10462 ----------------------------------------
10464 procedure Check_Possible_Deferred_Completion
10465 (Prev_Id : Entity_Id;
10466 Prev_Obj_Def : Node_Id;
10467 Curr_Obj_Def : Node_Id)
10469 begin
10470 if Nkind (Prev_Obj_Def) = N_Subtype_Indication
10471 and then Present (Constraint (Prev_Obj_Def))
10472 and then Nkind (Curr_Obj_Def) = N_Subtype_Indication
10473 and then Present (Constraint (Curr_Obj_Def))
10474 then
10475 declare
10476 Loc : constant Source_Ptr := Sloc (N);
10477 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
10478 Decl : constant Node_Id :=
10479 Make_Subtype_Declaration (Loc,
10480 Defining_Identifier => Def_Id,
10481 Subtype_Indication =>
10482 Relocate_Node (Curr_Obj_Def));
10484 begin
10485 Insert_Before_And_Analyze (N, Decl);
10486 Set_Etype (Id, Def_Id);
10488 if not Subtypes_Statically_Match (Etype (Prev_Id), Def_Id) then
10489 Error_Msg_Sloc := Sloc (Prev_Id);
10490 Error_Msg_N ("subtype does not statically match deferred " &
10491 "declaration#", N);
10492 end if;
10493 end;
10494 end if;
10495 end Check_Possible_Deferred_Completion;
10497 ---------------------------------
10498 -- Check_Recursive_Declaration --
10499 ---------------------------------
10501 procedure Check_Recursive_Declaration (Typ : Entity_Id) is
10502 Comp : Entity_Id;
10504 begin
10505 if Is_Record_Type (Typ) then
10506 Comp := First_Component (Typ);
10507 while Present (Comp) loop
10508 if Comes_From_Source (Comp) then
10509 if Present (Expression (Parent (Comp)))
10510 and then Is_Entity_Name (Expression (Parent (Comp)))
10511 and then Entity (Expression (Parent (Comp))) = Prev
10512 then
10513 Error_Msg_Sloc := Sloc (Parent (Comp));
10514 Error_Msg_NE
10515 ("illegal circularity with declaration for&#",
10516 N, Comp);
10517 return;
10519 elsif Is_Record_Type (Etype (Comp)) then
10520 Check_Recursive_Declaration (Etype (Comp));
10521 end if;
10522 end if;
10524 Next_Component (Comp);
10525 end loop;
10526 end if;
10527 end Check_Recursive_Declaration;
10529 -- Start of processing for Constant_Redeclaration
10531 begin
10532 if Nkind (Parent (Prev)) = N_Object_Declaration then
10533 if Nkind (Object_Definition
10534 (Parent (Prev))) = N_Subtype_Indication
10535 then
10536 -- Find type of new declaration. The constraints of the two
10537 -- views must match statically, but there is no point in
10538 -- creating an itype for the full view.
10540 if Nkind (Obj_Def) = N_Subtype_Indication then
10541 Find_Type (Subtype_Mark (Obj_Def));
10542 New_T := Entity (Subtype_Mark (Obj_Def));
10544 else
10545 Find_Type (Obj_Def);
10546 New_T := Entity (Obj_Def);
10547 end if;
10549 T := Etype (Prev);
10551 else
10552 -- The full view may impose a constraint, even if the partial
10553 -- view does not, so construct the subtype.
10555 New_T := Find_Type_Of_Object (Obj_Def, N);
10556 T := New_T;
10557 end if;
10559 else
10560 -- Current declaration is illegal, diagnosed below in Enter_Name
10562 T := Empty;
10563 New_T := Any_Type;
10564 end if;
10566 -- If previous full declaration or a renaming declaration exists, or if
10567 -- a homograph is present, let Enter_Name handle it, either with an
10568 -- error or with the removal of an overridden implicit subprogram.
10570 if Ekind (Prev) /= E_Constant
10571 or else Nkind (Parent (Prev)) = N_Object_Renaming_Declaration
10572 or else Present (Expression (Parent (Prev)))
10573 or else Present (Full_View (Prev))
10574 then
10575 Enter_Name (Id);
10577 -- Verify that types of both declarations match, or else that both types
10578 -- are anonymous access types whose designated subtypes statically match
10579 -- (as allowed in Ada 2005 by AI-385).
10581 elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
10582 and then
10583 (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
10584 or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
10585 or else Is_Access_Constant (Etype (New_T)) /=
10586 Is_Access_Constant (Etype (Prev))
10587 or else Can_Never_Be_Null (Etype (New_T)) /=
10588 Can_Never_Be_Null (Etype (Prev))
10589 or else Null_Exclusion_Present (Parent (Prev)) /=
10590 Null_Exclusion_Present (Parent (Id))
10591 or else not Subtypes_Statically_Match
10592 (Designated_Type (Etype (Prev)),
10593 Designated_Type (Etype (New_T))))
10594 then
10595 Error_Msg_Sloc := Sloc (Prev);
10596 Error_Msg_N ("type does not match declaration#", N);
10597 Set_Full_View (Prev, Id);
10598 Set_Etype (Id, Any_Type);
10600 elsif
10601 Null_Exclusion_Present (Parent (Prev))
10602 and then not Null_Exclusion_Present (N)
10603 then
10604 Error_Msg_Sloc := Sloc (Prev);
10605 Error_Msg_N ("null-exclusion does not match declaration#", N);
10606 Set_Full_View (Prev, Id);
10607 Set_Etype (Id, Any_Type);
10609 -- If so, process the full constant declaration
10611 else
10612 -- RM 7.4 (6): If the subtype defined by the subtype_indication in
10613 -- the deferred declaration is constrained, then the subtype defined
10614 -- by the subtype_indication in the full declaration shall match it
10615 -- statically.
10617 Check_Possible_Deferred_Completion
10618 (Prev_Id => Prev,
10619 Prev_Obj_Def => Object_Definition (Parent (Prev)),
10620 Curr_Obj_Def => Obj_Def);
10622 Set_Full_View (Prev, Id);
10623 Set_Is_Public (Id, Is_Public (Prev));
10624 Set_Is_Internal (Id);
10625 Append_Entity (Id, Current_Scope);
10627 -- Check ALIASED present if present before (RM 7.4(7))
10629 if Is_Aliased (Prev)
10630 and then not Aliased_Present (N)
10631 then
10632 Error_Msg_Sloc := Sloc (Prev);
10633 Error_Msg_N ("ALIASED required (see declaration#)", N);
10634 end if;
10636 -- Check that placement is in private part and that the incomplete
10637 -- declaration appeared in the visible part.
10639 if Ekind (Current_Scope) = E_Package
10640 and then not In_Private_Part (Current_Scope)
10641 then
10642 Error_Msg_Sloc := Sloc (Prev);
10643 Error_Msg_N
10644 ("full constant for declaration#"
10645 & " must be in private part", N);
10647 elsif Ekind (Current_Scope) = E_Package
10648 and then
10649 List_Containing (Parent (Prev)) /=
10650 Visible_Declarations
10651 (Specification (Unit_Declaration_Node (Current_Scope)))
10652 then
10653 Error_Msg_N
10654 ("deferred constant must be declared in visible part",
10655 Parent (Prev));
10656 end if;
10658 if Is_Access_Type (T)
10659 and then Nkind (Expression (N)) = N_Allocator
10660 then
10661 Check_Recursive_Declaration (Designated_Type (T));
10662 end if;
10663 end if;
10664 end Constant_Redeclaration;
10666 ----------------------
10667 -- Constrain_Access --
10668 ----------------------
10670 procedure Constrain_Access
10671 (Def_Id : in out Entity_Id;
10672 S : Node_Id;
10673 Related_Nod : Node_Id)
10675 T : constant Entity_Id := Entity (Subtype_Mark (S));
10676 Desig_Type : constant Entity_Id := Designated_Type (T);
10677 Desig_Subtype : Entity_Id := Create_Itype (E_Void, Related_Nod);
10678 Constraint_OK : Boolean := True;
10680 function Has_Defaulted_Discriminants (Typ : Entity_Id) return Boolean;
10681 -- Simple predicate to test for defaulted discriminants
10682 -- Shouldn't this be in sem_util???
10684 ---------------------------------
10685 -- Has_Defaulted_Discriminants --
10686 ---------------------------------
10688 function Has_Defaulted_Discriminants (Typ : Entity_Id) return Boolean is
10689 begin
10690 return Has_Discriminants (Typ)
10691 and then Present (First_Discriminant (Typ))
10692 and then Present
10693 (Discriminant_Default_Value (First_Discriminant (Typ)));
10694 end Has_Defaulted_Discriminants;
10696 -- Start of processing for Constrain_Access
10698 begin
10699 if Is_Array_Type (Desig_Type) then
10700 Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
10702 elsif (Is_Record_Type (Desig_Type)
10703 or else Is_Incomplete_Or_Private_Type (Desig_Type))
10704 and then not Is_Constrained (Desig_Type)
10705 then
10706 -- ??? The following code is a temporary kludge to ignore a
10707 -- discriminant constraint on access type if it is constraining
10708 -- the current record. Avoid creating the implicit subtype of the
10709 -- record we are currently compiling since right now, we cannot
10710 -- handle these. For now, just return the access type itself.
10712 if Desig_Type = Current_Scope
10713 and then No (Def_Id)
10714 then
10715 Set_Ekind (Desig_Subtype, E_Record_Subtype);
10716 Def_Id := Entity (Subtype_Mark (S));
10718 -- This call added to ensure that the constraint is analyzed
10719 -- (needed for a B test). Note that we still return early from
10720 -- this procedure to avoid recursive processing. ???
10722 Constrain_Discriminated_Type
10723 (Desig_Subtype, S, Related_Nod, For_Access => True);
10724 return;
10725 end if;
10727 -- Enforce rule that the constraint is illegal if there is an
10728 -- unconstrained view of the designated type. This means that the
10729 -- partial view (either a private type declaration or a derivation
10730 -- from a private type) has no discriminants. (Defect Report
10731 -- 8652/0008, Technical Corrigendum 1, checked by ACATS B371001).
10733 -- Rule updated for Ada 2005: the private type is said to have
10734 -- a constrained partial view, given that objects of the type
10735 -- can be declared. Furthermore, the rule applies to all access
10736 -- types, unlike the rule concerning default discriminants (see
10737 -- RM 3.7.1(7/3))
10739 if (Ekind (T) = E_General_Access_Type
10740 or else Ada_Version >= Ada_2005)
10741 and then Has_Private_Declaration (Desig_Type)
10742 and then In_Open_Scopes (Scope (Desig_Type))
10743 and then Has_Discriminants (Desig_Type)
10744 then
10745 declare
10746 Pack : constant Node_Id :=
10747 Unit_Declaration_Node (Scope (Desig_Type));
10748 Decls : List_Id;
10749 Decl : Node_Id;
10751 begin
10752 if Nkind (Pack) = N_Package_Declaration then
10753 Decls := Visible_Declarations (Specification (Pack));
10754 Decl := First (Decls);
10755 while Present (Decl) loop
10756 if (Nkind (Decl) = N_Private_Type_Declaration
10757 and then
10758 Chars (Defining_Identifier (Decl)) =
10759 Chars (Desig_Type))
10761 or else
10762 (Nkind (Decl) = N_Full_Type_Declaration
10763 and then
10764 Chars (Defining_Identifier (Decl)) =
10765 Chars (Desig_Type)
10766 and then Is_Derived_Type (Desig_Type)
10767 and then
10768 Has_Private_Declaration (Etype (Desig_Type)))
10769 then
10770 if No (Discriminant_Specifications (Decl)) then
10771 Error_Msg_N
10772 ("cannot constrain access type if designated " &
10773 "type has constrained partial view", S);
10774 end if;
10776 exit;
10777 end if;
10779 Next (Decl);
10780 end loop;
10781 end if;
10782 end;
10783 end if;
10785 Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
10786 For_Access => True);
10788 elsif (Is_Task_Type (Desig_Type)
10789 or else Is_Protected_Type (Desig_Type))
10790 and then not Is_Constrained (Desig_Type)
10791 then
10792 Constrain_Concurrent
10793 (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
10795 else
10796 Error_Msg_N ("invalid constraint on access type", S);
10797 Desig_Subtype := Desig_Type; -- Ignore invalid constraint.
10798 Constraint_OK := False;
10799 end if;
10801 if No (Def_Id) then
10802 Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
10803 else
10804 Set_Ekind (Def_Id, E_Access_Subtype);
10805 end if;
10807 if Constraint_OK then
10808 Set_Etype (Def_Id, Base_Type (T));
10810 if Is_Private_Type (Desig_Type) then
10811 Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
10812 end if;
10813 else
10814 Set_Etype (Def_Id, Any_Type);
10815 end if;
10817 Set_Size_Info (Def_Id, T);
10818 Set_Is_Constrained (Def_Id, Constraint_OK);
10819 Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
10820 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
10821 Set_Is_Access_Constant (Def_Id, Is_Access_Constant (T));
10823 Conditional_Delay (Def_Id, T);
10825 -- AI-363 : Subtypes of general access types whose designated types have
10826 -- default discriminants are disallowed. In instances, the rule has to
10827 -- be checked against the actual, of which T is the subtype. In a
10828 -- generic body, the rule is checked assuming that the actual type has
10829 -- defaulted discriminants.
10831 if Ada_Version >= Ada_2005 or else Warn_On_Ada_2005_Compatibility then
10832 if Ekind (Base_Type (T)) = E_General_Access_Type
10833 and then Has_Defaulted_Discriminants (Desig_Type)
10834 then
10835 if Ada_Version < Ada_2005 then
10836 Error_Msg_N
10837 ("access subtype of general access type would not " &
10838 "be allowed in Ada 2005?", S);
10839 else
10840 Error_Msg_N
10841 ("access subtype of general access type not allowed", S);
10842 end if;
10844 Error_Msg_N ("\discriminants have defaults", S);
10846 elsif Is_Access_Type (T)
10847 and then Is_Generic_Type (Desig_Type)
10848 and then Has_Discriminants (Desig_Type)
10849 and then In_Package_Body (Current_Scope)
10850 then
10851 if Ada_Version < Ada_2005 then
10852 Error_Msg_N
10853 ("access subtype would not be allowed in generic body " &
10854 "in Ada 2005?", S);
10855 else
10856 Error_Msg_N
10857 ("access subtype not allowed in generic body", S);
10858 end if;
10860 Error_Msg_N
10861 ("\designated type is a discriminated formal", S);
10862 end if;
10863 end if;
10864 end Constrain_Access;
10866 ---------------------
10867 -- Constrain_Array --
10868 ---------------------
10870 procedure Constrain_Array
10871 (Def_Id : in out Entity_Id;
10872 SI : Node_Id;
10873 Related_Nod : Node_Id;
10874 Related_Id : Entity_Id;
10875 Suffix : Character)
10877 C : constant Node_Id := Constraint (SI);
10878 Number_Of_Constraints : Nat := 0;
10879 Index : Node_Id;
10880 S, T : Entity_Id;
10881 Constraint_OK : Boolean := True;
10883 begin
10884 T := Entity (Subtype_Mark (SI));
10886 if Ekind (T) in Access_Kind then
10887 T := Designated_Type (T);
10888 end if;
10890 -- If an index constraint follows a subtype mark in a subtype indication
10891 -- then the type or subtype denoted by the subtype mark must not already
10892 -- impose an index constraint. The subtype mark must denote either an
10893 -- unconstrained array type or an access type whose designated type
10894 -- is such an array type... (RM 3.6.1)
10896 if Is_Constrained (T) then
10897 Error_Msg_N ("array type is already constrained", Subtype_Mark (SI));
10898 Constraint_OK := False;
10900 else
10901 S := First (Constraints (C));
10902 while Present (S) loop
10903 Number_Of_Constraints := Number_Of_Constraints + 1;
10904 Next (S);
10905 end loop;
10907 -- In either case, the index constraint must provide a discrete
10908 -- range for each index of the array type and the type of each
10909 -- discrete range must be the same as that of the corresponding
10910 -- index. (RM 3.6.1)
10912 if Number_Of_Constraints /= Number_Dimensions (T) then
10913 Error_Msg_NE ("incorrect number of index constraints for }", C, T);
10914 Constraint_OK := False;
10916 else
10917 S := First (Constraints (C));
10918 Index := First_Index (T);
10919 Analyze (Index);
10921 -- Apply constraints to each index type
10923 for J in 1 .. Number_Of_Constraints loop
10924 Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
10925 Next (Index);
10926 Next (S);
10927 end loop;
10929 end if;
10930 end if;
10932 if No (Def_Id) then
10933 Def_Id :=
10934 Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
10935 Set_Parent (Def_Id, Related_Nod);
10937 else
10938 Set_Ekind (Def_Id, E_Array_Subtype);
10939 end if;
10941 Set_Size_Info (Def_Id, (T));
10942 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10943 Set_Etype (Def_Id, Base_Type (T));
10945 if Constraint_OK then
10946 Set_First_Index (Def_Id, First (Constraints (C)));
10947 else
10948 Set_First_Index (Def_Id, First_Index (T));
10949 end if;
10951 Set_Is_Constrained (Def_Id, True);
10952 Set_Is_Aliased (Def_Id, Is_Aliased (T));
10953 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
10955 Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
10956 Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
10958 -- A subtype does not inherit the packed_array_type of is parent. We
10959 -- need to initialize the attribute because if Def_Id is previously
10960 -- analyzed through a limited_with clause, it will have the attributes
10961 -- of an incomplete type, one of which is an Elist that overlaps the
10962 -- Packed_Array_Type field.
10964 Set_Packed_Array_Type (Def_Id, Empty);
10966 -- Build a freeze node if parent still needs one. Also make sure that
10967 -- the Depends_On_Private status is set because the subtype will need
10968 -- reprocessing at the time the base type does, and also we must set a
10969 -- conditional delay.
10971 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
10972 Conditional_Delay (Def_Id, T);
10973 end Constrain_Array;
10975 ------------------------------
10976 -- Constrain_Component_Type --
10977 ------------------------------
10979 function Constrain_Component_Type
10980 (Comp : Entity_Id;
10981 Constrained_Typ : Entity_Id;
10982 Related_Node : Node_Id;
10983 Typ : Entity_Id;
10984 Constraints : Elist_Id) return Entity_Id
10986 Loc : constant Source_Ptr := Sloc (Constrained_Typ);
10987 Compon_Type : constant Entity_Id := Etype (Comp);
10989 function Build_Constrained_Array_Type
10990 (Old_Type : Entity_Id) return Entity_Id;
10991 -- If Old_Type is an array type, one of whose indexes is constrained
10992 -- by a discriminant, build an Itype whose constraint replaces the
10993 -- discriminant with its value in the constraint.
10995 function Build_Constrained_Discriminated_Type
10996 (Old_Type : Entity_Id) return Entity_Id;
10997 -- Ditto for record components
10999 function Build_Constrained_Access_Type
11000 (Old_Type : Entity_Id) return Entity_Id;
11001 -- Ditto for access types. Makes use of previous two functions, to
11002 -- constrain designated type.
11004 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id;
11005 -- T is an array or discriminated type, C is a list of constraints
11006 -- that apply to T. This routine builds the constrained subtype.
11008 function Is_Discriminant (Expr : Node_Id) return Boolean;
11009 -- Returns True if Expr is a discriminant
11011 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id;
11012 -- Find the value of discriminant Discrim in Constraint
11014 -----------------------------------
11015 -- Build_Constrained_Access_Type --
11016 -----------------------------------
11018 function Build_Constrained_Access_Type
11019 (Old_Type : Entity_Id) return Entity_Id
11021 Desig_Type : constant Entity_Id := Designated_Type (Old_Type);
11022 Itype : Entity_Id;
11023 Desig_Subtype : Entity_Id;
11024 Scop : Entity_Id;
11026 begin
11027 -- if the original access type was not embedded in the enclosing
11028 -- type definition, there is no need to produce a new access
11029 -- subtype. In fact every access type with an explicit constraint
11030 -- generates an itype whose scope is the enclosing record.
11032 if not Is_Type (Scope (Old_Type)) then
11033 return Old_Type;
11035 elsif Is_Array_Type (Desig_Type) then
11036 Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
11038 elsif Has_Discriminants (Desig_Type) then
11040 -- This may be an access type to an enclosing record type for
11041 -- which we are constructing the constrained components. Return
11042 -- the enclosing record subtype. This is not always correct,
11043 -- but avoids infinite recursion. ???
11045 Desig_Subtype := Any_Type;
11047 for J in reverse 0 .. Scope_Stack.Last loop
11048 Scop := Scope_Stack.Table (J).Entity;
11050 if Is_Type (Scop)
11051 and then Base_Type (Scop) = Base_Type (Desig_Type)
11052 then
11053 Desig_Subtype := Scop;
11054 end if;
11056 exit when not Is_Type (Scop);
11057 end loop;
11059 if Desig_Subtype = Any_Type then
11060 Desig_Subtype :=
11061 Build_Constrained_Discriminated_Type (Desig_Type);
11062 end if;
11064 else
11065 return Old_Type;
11066 end if;
11068 if Desig_Subtype /= Desig_Type then
11070 -- The Related_Node better be here or else we won't be able
11071 -- to attach new itypes to a node in the tree.
11073 pragma Assert (Present (Related_Node));
11075 Itype := Create_Itype (E_Access_Subtype, Related_Node);
11077 Set_Etype (Itype, Base_Type (Old_Type));
11078 Set_Size_Info (Itype, (Old_Type));
11079 Set_Directly_Designated_Type (Itype, Desig_Subtype);
11080 Set_Depends_On_Private (Itype, Has_Private_Component
11081 (Old_Type));
11082 Set_Is_Access_Constant (Itype, Is_Access_Constant
11083 (Old_Type));
11085 -- The new itype needs freezing when it depends on a not frozen
11086 -- type and the enclosing subtype needs freezing.
11088 if Has_Delayed_Freeze (Constrained_Typ)
11089 and then not Is_Frozen (Constrained_Typ)
11090 then
11091 Conditional_Delay (Itype, Base_Type (Old_Type));
11092 end if;
11094 return Itype;
11096 else
11097 return Old_Type;
11098 end if;
11099 end Build_Constrained_Access_Type;
11101 ----------------------------------
11102 -- Build_Constrained_Array_Type --
11103 ----------------------------------
11105 function Build_Constrained_Array_Type
11106 (Old_Type : Entity_Id) return Entity_Id
11108 Lo_Expr : Node_Id;
11109 Hi_Expr : Node_Id;
11110 Old_Index : Node_Id;
11111 Range_Node : Node_Id;
11112 Constr_List : List_Id;
11114 Need_To_Create_Itype : Boolean := False;
11116 begin
11117 Old_Index := First_Index (Old_Type);
11118 while Present (Old_Index) loop
11119 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
11121 if Is_Discriminant (Lo_Expr)
11122 or else Is_Discriminant (Hi_Expr)
11123 then
11124 Need_To_Create_Itype := True;
11125 end if;
11127 Next_Index (Old_Index);
11128 end loop;
11130 if Need_To_Create_Itype then
11131 Constr_List := New_List;
11133 Old_Index := First_Index (Old_Type);
11134 while Present (Old_Index) loop
11135 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
11137 if Is_Discriminant (Lo_Expr) then
11138 Lo_Expr := Get_Discr_Value (Lo_Expr);
11139 end if;
11141 if Is_Discriminant (Hi_Expr) then
11142 Hi_Expr := Get_Discr_Value (Hi_Expr);
11143 end if;
11145 Range_Node :=
11146 Make_Range
11147 (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
11149 Append (Range_Node, To => Constr_List);
11151 Next_Index (Old_Index);
11152 end loop;
11154 return Build_Subtype (Old_Type, Constr_List);
11156 else
11157 return Old_Type;
11158 end if;
11159 end Build_Constrained_Array_Type;
11161 ------------------------------------------
11162 -- Build_Constrained_Discriminated_Type --
11163 ------------------------------------------
11165 function Build_Constrained_Discriminated_Type
11166 (Old_Type : Entity_Id) return Entity_Id
11168 Expr : Node_Id;
11169 Constr_List : List_Id;
11170 Old_Constraint : Elmt_Id;
11172 Need_To_Create_Itype : Boolean := False;
11174 begin
11175 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
11176 while Present (Old_Constraint) loop
11177 Expr := Node (Old_Constraint);
11179 if Is_Discriminant (Expr) then
11180 Need_To_Create_Itype := True;
11181 end if;
11183 Next_Elmt (Old_Constraint);
11184 end loop;
11186 if Need_To_Create_Itype then
11187 Constr_List := New_List;
11189 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
11190 while Present (Old_Constraint) loop
11191 Expr := Node (Old_Constraint);
11193 if Is_Discriminant (Expr) then
11194 Expr := Get_Discr_Value (Expr);
11195 end if;
11197 Append (New_Copy_Tree (Expr), To => Constr_List);
11199 Next_Elmt (Old_Constraint);
11200 end loop;
11202 return Build_Subtype (Old_Type, Constr_List);
11204 else
11205 return Old_Type;
11206 end if;
11207 end Build_Constrained_Discriminated_Type;
11209 -------------------
11210 -- Build_Subtype --
11211 -------------------
11213 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id is
11214 Indic : Node_Id;
11215 Subtyp_Decl : Node_Id;
11216 Def_Id : Entity_Id;
11217 Btyp : Entity_Id := Base_Type (T);
11219 begin
11220 -- The Related_Node better be here or else we won't be able to
11221 -- attach new itypes to a node in the tree.
11223 pragma Assert (Present (Related_Node));
11225 -- If the view of the component's type is incomplete or private
11226 -- with unknown discriminants, then the constraint must be applied
11227 -- to the full type.
11229 if Has_Unknown_Discriminants (Btyp)
11230 and then Present (Underlying_Type (Btyp))
11231 then
11232 Btyp := Underlying_Type (Btyp);
11233 end if;
11235 Indic :=
11236 Make_Subtype_Indication (Loc,
11237 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
11238 Constraint => Make_Index_Or_Discriminant_Constraint (Loc, C));
11240 Def_Id := Create_Itype (Ekind (T), Related_Node);
11242 Subtyp_Decl :=
11243 Make_Subtype_Declaration (Loc,
11244 Defining_Identifier => Def_Id,
11245 Subtype_Indication => Indic);
11247 Set_Parent (Subtyp_Decl, Parent (Related_Node));
11249 -- Itypes must be analyzed with checks off (see package Itypes)
11251 Analyze (Subtyp_Decl, Suppress => All_Checks);
11253 return Def_Id;
11254 end Build_Subtype;
11256 ---------------------
11257 -- Get_Discr_Value --
11258 ---------------------
11260 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id is
11261 D : Entity_Id;
11262 E : Elmt_Id;
11264 begin
11265 -- The discriminant may be declared for the type, in which case we
11266 -- find it by iterating over the list of discriminants. If the
11267 -- discriminant is inherited from a parent type, it appears as the
11268 -- corresponding discriminant of the current type. This will be the
11269 -- case when constraining an inherited component whose constraint is
11270 -- given by a discriminant of the parent.
11272 D := First_Discriminant (Typ);
11273 E := First_Elmt (Constraints);
11275 while Present (D) loop
11276 if D = Entity (Discrim)
11277 or else D = CR_Discriminant (Entity (Discrim))
11278 or else Corresponding_Discriminant (D) = Entity (Discrim)
11279 then
11280 return Node (E);
11281 end if;
11283 Next_Discriminant (D);
11284 Next_Elmt (E);
11285 end loop;
11287 -- The Corresponding_Discriminant mechanism is incomplete, because
11288 -- the correspondence between new and old discriminants is not one
11289 -- to one: one new discriminant can constrain several old ones. In
11290 -- that case, scan sequentially the stored_constraint, the list of
11291 -- discriminants of the parents, and the constraints.
11292 -- Previous code checked for the present of the Stored_Constraint
11293 -- list for the derived type, but did not use it at all. Should it
11294 -- be present when the component is a discriminated task type?
11296 if Is_Derived_Type (Typ)
11297 and then Scope (Entity (Discrim)) = Etype (Typ)
11298 then
11299 D := First_Discriminant (Etype (Typ));
11300 E := First_Elmt (Constraints);
11301 while Present (D) loop
11302 if D = Entity (Discrim) then
11303 return Node (E);
11304 end if;
11306 Next_Discriminant (D);
11307 Next_Elmt (E);
11308 end loop;
11309 end if;
11311 -- Something is wrong if we did not find the value
11313 raise Program_Error;
11314 end Get_Discr_Value;
11316 ---------------------
11317 -- Is_Discriminant --
11318 ---------------------
11320 function Is_Discriminant (Expr : Node_Id) return Boolean is
11321 Discrim_Scope : Entity_Id;
11323 begin
11324 if Denotes_Discriminant (Expr) then
11325 Discrim_Scope := Scope (Entity (Expr));
11327 -- Either we have a reference to one of Typ's discriminants,
11329 pragma Assert (Discrim_Scope = Typ
11331 -- or to the discriminants of the parent type, in the case
11332 -- of a derivation of a tagged type with variants.
11334 or else Discrim_Scope = Etype (Typ)
11335 or else Full_View (Discrim_Scope) = Etype (Typ)
11337 -- or same as above for the case where the discriminants
11338 -- were declared in Typ's private view.
11340 or else (Is_Private_Type (Discrim_Scope)
11341 and then Chars (Discrim_Scope) = Chars (Typ))
11343 -- or else we are deriving from the full view and the
11344 -- discriminant is declared in the private entity.
11346 or else (Is_Private_Type (Typ)
11347 and then Chars (Discrim_Scope) = Chars (Typ))
11349 -- Or we are constrained the corresponding record of a
11350 -- synchronized type that completes a private declaration.
11352 or else (Is_Concurrent_Record_Type (Typ)
11353 and then
11354 Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
11356 -- or we have a class-wide type, in which case make sure the
11357 -- discriminant found belongs to the root type.
11359 or else (Is_Class_Wide_Type (Typ)
11360 and then Etype (Typ) = Discrim_Scope));
11362 return True;
11363 end if;
11365 -- In all other cases we have something wrong
11367 return False;
11368 end Is_Discriminant;
11370 -- Start of processing for Constrain_Component_Type
11372 begin
11373 if Nkind (Parent (Comp)) = N_Component_Declaration
11374 and then Comes_From_Source (Parent (Comp))
11375 and then Comes_From_Source
11376 (Subtype_Indication (Component_Definition (Parent (Comp))))
11377 and then
11378 Is_Entity_Name
11379 (Subtype_Indication (Component_Definition (Parent (Comp))))
11380 then
11381 return Compon_Type;
11383 elsif Is_Array_Type (Compon_Type) then
11384 return Build_Constrained_Array_Type (Compon_Type);
11386 elsif Has_Discriminants (Compon_Type) then
11387 return Build_Constrained_Discriminated_Type (Compon_Type);
11389 elsif Is_Access_Type (Compon_Type) then
11390 return Build_Constrained_Access_Type (Compon_Type);
11392 else
11393 return Compon_Type;
11394 end if;
11395 end Constrain_Component_Type;
11397 --------------------------
11398 -- Constrain_Concurrent --
11399 --------------------------
11401 -- For concurrent types, the associated record value type carries the same
11402 -- discriminants, so when we constrain a concurrent type, we must constrain
11403 -- the corresponding record type as well.
11405 procedure Constrain_Concurrent
11406 (Def_Id : in out Entity_Id;
11407 SI : Node_Id;
11408 Related_Nod : Node_Id;
11409 Related_Id : Entity_Id;
11410 Suffix : Character)
11412 -- Retrieve Base_Type to ensure getting to the concurrent type in the
11413 -- case of a private subtype (needed when only doing semantic analysis).
11415 T_Ent : Entity_Id := Base_Type (Entity (Subtype_Mark (SI)));
11416 T_Val : Entity_Id;
11418 begin
11419 if Ekind (T_Ent) in Access_Kind then
11420 T_Ent := Designated_Type (T_Ent);
11421 end if;
11423 T_Val := Corresponding_Record_Type (T_Ent);
11425 if Present (T_Val) then
11427 if No (Def_Id) then
11428 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
11429 end if;
11431 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
11433 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
11434 Set_Corresponding_Record_Type (Def_Id,
11435 Constrain_Corresponding_Record
11436 (Def_Id, T_Val, Related_Nod, Related_Id));
11438 else
11439 -- If there is no associated record, expansion is disabled and this
11440 -- is a generic context. Create a subtype in any case, so that
11441 -- semantic analysis can proceed.
11443 if No (Def_Id) then
11444 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
11445 end if;
11447 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
11448 end if;
11449 end Constrain_Concurrent;
11451 ------------------------------------
11452 -- Constrain_Corresponding_Record --
11453 ------------------------------------
11455 function Constrain_Corresponding_Record
11456 (Prot_Subt : Entity_Id;
11457 Corr_Rec : Entity_Id;
11458 Related_Nod : Node_Id;
11459 Related_Id : Entity_Id) return Entity_Id
11461 T_Sub : constant Entity_Id :=
11462 Create_Itype (E_Record_Subtype, Related_Nod, Related_Id, 'V');
11464 begin
11465 Set_Etype (T_Sub, Corr_Rec);
11466 Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
11467 Set_Is_Constrained (T_Sub, True);
11468 Set_First_Entity (T_Sub, First_Entity (Corr_Rec));
11469 Set_Last_Entity (T_Sub, Last_Entity (Corr_Rec));
11471 -- As elsewhere, we do not want to create a freeze node for this itype
11472 -- if it is created for a constrained component of an enclosing record
11473 -- because references to outer discriminants will appear out of scope.
11475 if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
11476 Conditional_Delay (T_Sub, Corr_Rec);
11477 else
11478 Set_Is_Frozen (T_Sub);
11479 end if;
11481 if Has_Discriminants (Prot_Subt) then -- False only if errors.
11482 Set_Discriminant_Constraint
11483 (T_Sub, Discriminant_Constraint (Prot_Subt));
11484 Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
11485 Create_Constrained_Components
11486 (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
11487 end if;
11489 Set_Depends_On_Private (T_Sub, Has_Private_Component (T_Sub));
11491 return T_Sub;
11492 end Constrain_Corresponding_Record;
11494 -----------------------
11495 -- Constrain_Decimal --
11496 -----------------------
11498 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id) is
11499 T : constant Entity_Id := Entity (Subtype_Mark (S));
11500 C : constant Node_Id := Constraint (S);
11501 Loc : constant Source_Ptr := Sloc (C);
11502 Range_Expr : Node_Id;
11503 Digits_Expr : Node_Id;
11504 Digits_Val : Uint;
11505 Bound_Val : Ureal;
11507 begin
11508 Set_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
11510 if Nkind (C) = N_Range_Constraint then
11511 Range_Expr := Range_Expression (C);
11512 Digits_Val := Digits_Value (T);
11514 else
11515 pragma Assert (Nkind (C) = N_Digits_Constraint);
11517 Check_SPARK_Restriction ("digits constraint is not allowed", S);
11519 Digits_Expr := Digits_Expression (C);
11520 Analyze_And_Resolve (Digits_Expr, Any_Integer);
11522 Check_Digits_Expression (Digits_Expr);
11523 Digits_Val := Expr_Value (Digits_Expr);
11525 if Digits_Val > Digits_Value (T) then
11526 Error_Msg_N
11527 ("digits expression is incompatible with subtype", C);
11528 Digits_Val := Digits_Value (T);
11529 end if;
11531 if Present (Range_Constraint (C)) then
11532 Range_Expr := Range_Expression (Range_Constraint (C));
11533 else
11534 Range_Expr := Empty;
11535 end if;
11536 end if;
11538 Set_Etype (Def_Id, Base_Type (T));
11539 Set_Size_Info (Def_Id, (T));
11540 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
11541 Set_Delta_Value (Def_Id, Delta_Value (T));
11542 Set_Scale_Value (Def_Id, Scale_Value (T));
11543 Set_Small_Value (Def_Id, Small_Value (T));
11544 Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
11545 Set_Digits_Value (Def_Id, Digits_Val);
11547 -- Manufacture range from given digits value if no range present
11549 if No (Range_Expr) then
11550 Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
11551 Range_Expr :=
11552 Make_Range (Loc,
11553 Low_Bound =>
11554 Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
11555 High_Bound =>
11556 Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
11557 end if;
11559 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
11560 Set_Discrete_RM_Size (Def_Id);
11562 -- Unconditionally delay the freeze, since we cannot set size
11563 -- information in all cases correctly until the freeze point.
11565 Set_Has_Delayed_Freeze (Def_Id);
11566 end Constrain_Decimal;
11568 ----------------------------------
11569 -- Constrain_Discriminated_Type --
11570 ----------------------------------
11572 procedure Constrain_Discriminated_Type
11573 (Def_Id : Entity_Id;
11574 S : Node_Id;
11575 Related_Nod : Node_Id;
11576 For_Access : Boolean := False)
11578 E : constant Entity_Id := Entity (Subtype_Mark (S));
11579 T : Entity_Id;
11580 C : Node_Id;
11581 Elist : Elist_Id := New_Elmt_List;
11583 procedure Fixup_Bad_Constraint;
11584 -- This is called after finding a bad constraint, and after having
11585 -- posted an appropriate error message. The mission is to leave the
11586 -- entity T in as reasonable state as possible!
11588 --------------------------
11589 -- Fixup_Bad_Constraint --
11590 --------------------------
11592 procedure Fixup_Bad_Constraint is
11593 begin
11594 -- Set a reasonable Ekind for the entity. For an incomplete type,
11595 -- we can't do much, but for other types, we can set the proper
11596 -- corresponding subtype kind.
11598 if Ekind (T) = E_Incomplete_Type then
11599 Set_Ekind (Def_Id, Ekind (T));
11600 else
11601 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
11602 end if;
11604 -- Set Etype to the known type, to reduce chances of cascaded errors
11606 Set_Etype (Def_Id, E);
11607 Set_Error_Posted (Def_Id);
11608 end Fixup_Bad_Constraint;
11610 -- Start of processing for Constrain_Discriminated_Type
11612 begin
11613 C := Constraint (S);
11615 -- A discriminant constraint is only allowed in a subtype indication,
11616 -- after a subtype mark. This subtype mark must denote either a type
11617 -- with discriminants, or an access type whose designated type is a
11618 -- type with discriminants. A discriminant constraint specifies the
11619 -- values of these discriminants (RM 3.7.2(5)).
11621 T := Base_Type (Entity (Subtype_Mark (S)));
11623 if Ekind (T) in Access_Kind then
11624 T := Designated_Type (T);
11625 end if;
11627 -- Ada 2005 (AI-412): Constrained incomplete subtypes are illegal.
11628 -- Avoid generating an error for access-to-incomplete subtypes.
11630 if Ada_Version >= Ada_2005
11631 and then Ekind (T) = E_Incomplete_Type
11632 and then Nkind (Parent (S)) = N_Subtype_Declaration
11633 and then not Is_Itype (Def_Id)
11634 then
11635 -- A little sanity check, emit an error message if the type
11636 -- has discriminants to begin with. Type T may be a regular
11637 -- incomplete type or imported via a limited with clause.
11639 if Has_Discriminants (T)
11640 or else
11641 (From_With_Type (T)
11642 and then Present (Non_Limited_View (T))
11643 and then Nkind (Parent (Non_Limited_View (T))) =
11644 N_Full_Type_Declaration
11645 and then Present (Discriminant_Specifications
11646 (Parent (Non_Limited_View (T)))))
11647 then
11648 Error_Msg_N
11649 ("(Ada 2005) incomplete subtype may not be constrained", C);
11650 else
11651 Error_Msg_N ("invalid constraint: type has no discriminant", C);
11652 end if;
11654 Fixup_Bad_Constraint;
11655 return;
11657 -- Check that the type has visible discriminants. The type may be
11658 -- a private type with unknown discriminants whose full view has
11659 -- discriminants which are invisible.
11661 elsif not Has_Discriminants (T)
11662 or else
11663 (Has_Unknown_Discriminants (T)
11664 and then Is_Private_Type (T))
11665 then
11666 Error_Msg_N ("invalid constraint: type has no discriminant", C);
11667 Fixup_Bad_Constraint;
11668 return;
11670 elsif Is_Constrained (E)
11671 or else (Ekind (E) = E_Class_Wide_Subtype
11672 and then Present (Discriminant_Constraint (E)))
11673 then
11674 Error_Msg_N ("type is already constrained", Subtype_Mark (S));
11675 Fixup_Bad_Constraint;
11676 return;
11677 end if;
11679 -- T may be an unconstrained subtype (e.g. a generic actual).
11680 -- Constraint applies to the base type.
11682 T := Base_Type (T);
11684 Elist := Build_Discriminant_Constraints (T, S);
11686 -- If the list returned was empty we had an error in building the
11687 -- discriminant constraint. We have also already signalled an error
11688 -- in the incomplete type case
11690 if Is_Empty_Elmt_List (Elist) then
11691 Fixup_Bad_Constraint;
11692 return;
11693 end if;
11695 Build_Discriminated_Subtype (T, Def_Id, Elist, Related_Nod, For_Access);
11696 end Constrain_Discriminated_Type;
11698 ---------------------------
11699 -- Constrain_Enumeration --
11700 ---------------------------
11702 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id) is
11703 T : constant Entity_Id := Entity (Subtype_Mark (S));
11704 C : constant Node_Id := Constraint (S);
11706 begin
11707 Set_Ekind (Def_Id, E_Enumeration_Subtype);
11709 Set_First_Literal (Def_Id, First_Literal (Base_Type (T)));
11711 Set_Etype (Def_Id, Base_Type (T));
11712 Set_Size_Info (Def_Id, (T));
11713 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
11714 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
11716 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
11718 Set_Discrete_RM_Size (Def_Id);
11719 end Constrain_Enumeration;
11721 ----------------------
11722 -- Constrain_Float --
11723 ----------------------
11725 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id) is
11726 T : constant Entity_Id := Entity (Subtype_Mark (S));
11727 C : Node_Id;
11728 D : Node_Id;
11729 Rais : Node_Id;
11731 begin
11732 Set_Ekind (Def_Id, E_Floating_Point_Subtype);
11734 Set_Etype (Def_Id, Base_Type (T));
11735 Set_Size_Info (Def_Id, (T));
11736 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
11738 -- Process the constraint
11740 C := Constraint (S);
11742 -- Digits constraint present
11744 if Nkind (C) = N_Digits_Constraint then
11746 Check_SPARK_Restriction ("digits constraint is not allowed", S);
11747 Check_Restriction (No_Obsolescent_Features, C);
11749 if Warn_On_Obsolescent_Feature then
11750 Error_Msg_N
11751 ("subtype digits constraint is an " &
11752 "obsolescent feature (RM J.3(8))?", C);
11753 end if;
11755 D := Digits_Expression (C);
11756 Analyze_And_Resolve (D, Any_Integer);
11757 Check_Digits_Expression (D);
11758 Set_Digits_Value (Def_Id, Expr_Value (D));
11760 -- Check that digits value is in range. Obviously we can do this
11761 -- at compile time, but it is strictly a runtime check, and of
11762 -- course there is an ACVC test that checks this!
11764 if Digits_Value (Def_Id) > Digits_Value (T) then
11765 Error_Msg_Uint_1 := Digits_Value (T);
11766 Error_Msg_N ("?digits value is too large, maximum is ^", D);
11767 Rais :=
11768 Make_Raise_Constraint_Error (Sloc (D),
11769 Reason => CE_Range_Check_Failed);
11770 Insert_Action (Declaration_Node (Def_Id), Rais);
11771 end if;
11773 C := Range_Constraint (C);
11775 -- No digits constraint present
11777 else
11778 Set_Digits_Value (Def_Id, Digits_Value (T));
11779 end if;
11781 -- Range constraint present
11783 if Nkind (C) = N_Range_Constraint then
11784 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
11786 -- No range constraint present
11788 else
11789 pragma Assert (No (C));
11790 Set_Scalar_Range (Def_Id, Scalar_Range (T));
11791 end if;
11793 Set_Is_Constrained (Def_Id);
11794 end Constrain_Float;
11796 ---------------------
11797 -- Constrain_Index --
11798 ---------------------
11800 procedure Constrain_Index
11801 (Index : Node_Id;
11802 S : Node_Id;
11803 Related_Nod : Node_Id;
11804 Related_Id : Entity_Id;
11805 Suffix : Character;
11806 Suffix_Index : Nat)
11808 Def_Id : Entity_Id;
11809 R : Node_Id := Empty;
11810 T : constant Entity_Id := Etype (Index);
11812 begin
11813 if Nkind (S) = N_Range
11814 or else
11815 (Nkind (S) = N_Attribute_Reference
11816 and then Attribute_Name (S) = Name_Range)
11817 then
11818 -- A Range attribute will be transformed into N_Range by Resolve
11820 Analyze (S);
11821 Set_Etype (S, T);
11822 R := S;
11824 Process_Range_Expr_In_Decl (R, T, Empty_List);
11826 if not Error_Posted (S)
11827 and then
11828 (Nkind (S) /= N_Range
11829 or else not Covers (T, (Etype (Low_Bound (S))))
11830 or else not Covers (T, (Etype (High_Bound (S)))))
11831 then
11832 if Base_Type (T) /= Any_Type
11833 and then Etype (Low_Bound (S)) /= Any_Type
11834 and then Etype (High_Bound (S)) /= Any_Type
11835 then
11836 Error_Msg_N ("range expected", S);
11837 end if;
11838 end if;
11840 elsif Nkind (S) = N_Subtype_Indication then
11842 -- The parser has verified that this is a discrete indication
11844 Resolve_Discrete_Subtype_Indication (S, T);
11845 R := Range_Expression (Constraint (S));
11847 -- Capture values of bounds and generate temporaries for them if
11848 -- needed, since checks may cause duplication of the expressions
11849 -- which must not be reevaluated.
11851 -- The forced evaluation removes side effects from expressions,
11852 -- which should occur also in Alfa mode. Otherwise, we end up with
11853 -- unexpected insertions of actions at places where this is not
11854 -- supposed to occur, e.g. on default parameters of a call.
11856 if Expander_Active then
11857 Force_Evaluation (Low_Bound (R));
11858 Force_Evaluation (High_Bound (R));
11859 end if;
11861 elsif Nkind (S) = N_Discriminant_Association then
11863 -- Syntactically valid in subtype indication
11865 Error_Msg_N ("invalid index constraint", S);
11866 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
11867 return;
11869 -- Subtype_Mark case, no anonymous subtypes to construct
11871 else
11872 Analyze (S);
11874 if Is_Entity_Name (S) then
11875 if not Is_Type (Entity (S)) then
11876 Error_Msg_N ("expect subtype mark for index constraint", S);
11878 elsif Base_Type (Entity (S)) /= Base_Type (T) then
11879 Wrong_Type (S, Base_Type (T));
11881 -- Check error of subtype with predicate in index constraint
11883 else
11884 Bad_Predicated_Subtype_Use
11885 ("subtype& has predicate, not allowed in index constraint",
11886 S, Entity (S));
11887 end if;
11889 return;
11891 else
11892 Error_Msg_N ("invalid index constraint", S);
11893 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
11894 return;
11895 end if;
11896 end if;
11898 Def_Id :=
11899 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
11901 Set_Etype (Def_Id, Base_Type (T));
11903 if Is_Modular_Integer_Type (T) then
11904 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
11906 elsif Is_Integer_Type (T) then
11907 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
11909 else
11910 Set_Ekind (Def_Id, E_Enumeration_Subtype);
11911 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
11912 Set_First_Literal (Def_Id, First_Literal (T));
11913 end if;
11915 Set_Size_Info (Def_Id, (T));
11916 Set_RM_Size (Def_Id, RM_Size (T));
11917 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
11919 Set_Scalar_Range (Def_Id, R);
11921 Set_Etype (S, Def_Id);
11922 Set_Discrete_RM_Size (Def_Id);
11923 end Constrain_Index;
11925 -----------------------
11926 -- Constrain_Integer --
11927 -----------------------
11929 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id) is
11930 T : constant Entity_Id := Entity (Subtype_Mark (S));
11931 C : constant Node_Id := Constraint (S);
11933 begin
11934 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
11936 if Is_Modular_Integer_Type (T) then
11937 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
11938 else
11939 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
11940 end if;
11942 Set_Etype (Def_Id, Base_Type (T));
11943 Set_Size_Info (Def_Id, (T));
11944 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
11945 Set_Discrete_RM_Size (Def_Id);
11946 end Constrain_Integer;
11948 ------------------------------
11949 -- Constrain_Ordinary_Fixed --
11950 ------------------------------
11952 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id) is
11953 T : constant Entity_Id := Entity (Subtype_Mark (S));
11954 C : Node_Id;
11955 D : Node_Id;
11956 Rais : Node_Id;
11958 begin
11959 Set_Ekind (Def_Id, E_Ordinary_Fixed_Point_Subtype);
11960 Set_Etype (Def_Id, Base_Type (T));
11961 Set_Size_Info (Def_Id, (T));
11962 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
11963 Set_Small_Value (Def_Id, Small_Value (T));
11965 -- Process the constraint
11967 C := Constraint (S);
11969 -- Delta constraint present
11971 if Nkind (C) = N_Delta_Constraint then
11973 Check_SPARK_Restriction ("delta constraint is not allowed", S);
11974 Check_Restriction (No_Obsolescent_Features, C);
11976 if Warn_On_Obsolescent_Feature then
11977 Error_Msg_S
11978 ("subtype delta constraint is an " &
11979 "obsolescent feature (RM J.3(7))?");
11980 end if;
11982 D := Delta_Expression (C);
11983 Analyze_And_Resolve (D, Any_Real);
11984 Check_Delta_Expression (D);
11985 Set_Delta_Value (Def_Id, Expr_Value_R (D));
11987 -- Check that delta value is in range. Obviously we can do this
11988 -- at compile time, but it is strictly a runtime check, and of
11989 -- course there is an ACVC test that checks this!
11991 if Delta_Value (Def_Id) < Delta_Value (T) then
11992 Error_Msg_N ("?delta value is too small", D);
11993 Rais :=
11994 Make_Raise_Constraint_Error (Sloc (D),
11995 Reason => CE_Range_Check_Failed);
11996 Insert_Action (Declaration_Node (Def_Id), Rais);
11997 end if;
11999 C := Range_Constraint (C);
12001 -- No delta constraint present
12003 else
12004 Set_Delta_Value (Def_Id, Delta_Value (T));
12005 end if;
12007 -- Range constraint present
12009 if Nkind (C) = N_Range_Constraint then
12010 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
12012 -- No range constraint present
12014 else
12015 pragma Assert (No (C));
12016 Set_Scalar_Range (Def_Id, Scalar_Range (T));
12018 end if;
12020 Set_Discrete_RM_Size (Def_Id);
12022 -- Unconditionally delay the freeze, since we cannot set size
12023 -- information in all cases correctly until the freeze point.
12025 Set_Has_Delayed_Freeze (Def_Id);
12026 end Constrain_Ordinary_Fixed;
12028 -----------------------
12029 -- Contain_Interface --
12030 -----------------------
12032 function Contain_Interface
12033 (Iface : Entity_Id;
12034 Ifaces : Elist_Id) return Boolean
12036 Iface_Elmt : Elmt_Id;
12038 begin
12039 if Present (Ifaces) then
12040 Iface_Elmt := First_Elmt (Ifaces);
12041 while Present (Iface_Elmt) loop
12042 if Node (Iface_Elmt) = Iface then
12043 return True;
12044 end if;
12046 Next_Elmt (Iface_Elmt);
12047 end loop;
12048 end if;
12050 return False;
12051 end Contain_Interface;
12053 ---------------------------
12054 -- Convert_Scalar_Bounds --
12055 ---------------------------
12057 procedure Convert_Scalar_Bounds
12058 (N : Node_Id;
12059 Parent_Type : Entity_Id;
12060 Derived_Type : Entity_Id;
12061 Loc : Source_Ptr)
12063 Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
12065 Lo : Node_Id;
12066 Hi : Node_Id;
12067 Rng : Node_Id;
12069 begin
12070 -- Defend against previous errors
12072 if No (Scalar_Range (Derived_Type)) then
12073 return;
12074 end if;
12076 Lo := Build_Scalar_Bound
12077 (Type_Low_Bound (Derived_Type),
12078 Parent_Type, Implicit_Base);
12080 Hi := Build_Scalar_Bound
12081 (Type_High_Bound (Derived_Type),
12082 Parent_Type, Implicit_Base);
12084 Rng :=
12085 Make_Range (Loc,
12086 Low_Bound => Lo,
12087 High_Bound => Hi);
12089 Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
12091 Set_Parent (Rng, N);
12092 Set_Scalar_Range (Derived_Type, Rng);
12094 -- Analyze the bounds
12096 Analyze_And_Resolve (Lo, Implicit_Base);
12097 Analyze_And_Resolve (Hi, Implicit_Base);
12099 -- Analyze the range itself, except that we do not analyze it if
12100 -- the bounds are real literals, and we have a fixed-point type.
12101 -- The reason for this is that we delay setting the bounds in this
12102 -- case till we know the final Small and Size values (see circuit
12103 -- in Freeze.Freeze_Fixed_Point_Type for further details).
12105 if Is_Fixed_Point_Type (Parent_Type)
12106 and then Nkind (Lo) = N_Real_Literal
12107 and then Nkind (Hi) = N_Real_Literal
12108 then
12109 return;
12111 -- Here we do the analysis of the range
12113 -- Note: we do this manually, since if we do a normal Analyze and
12114 -- Resolve call, there are problems with the conversions used for
12115 -- the derived type range.
12117 else
12118 Set_Etype (Rng, Implicit_Base);
12119 Set_Analyzed (Rng, True);
12120 end if;
12121 end Convert_Scalar_Bounds;
12123 -------------------
12124 -- Copy_And_Swap --
12125 -------------------
12127 procedure Copy_And_Swap (Priv, Full : Entity_Id) is
12128 begin
12129 -- Initialize new full declaration entity by copying the pertinent
12130 -- fields of the corresponding private declaration entity.
12132 -- We temporarily set Ekind to a value appropriate for a type to
12133 -- avoid assert failures in Einfo from checking for setting type
12134 -- attributes on something that is not a type. Ekind (Priv) is an
12135 -- appropriate choice, since it allowed the attributes to be set
12136 -- in the first place. This Ekind value will be modified later.
12138 Set_Ekind (Full, Ekind (Priv));
12140 -- Also set Etype temporarily to Any_Type, again, in the absence
12141 -- of errors, it will be properly reset, and if there are errors,
12142 -- then we want a value of Any_Type to remain.
12144 Set_Etype (Full, Any_Type);
12146 -- Now start copying attributes
12148 Set_Has_Discriminants (Full, Has_Discriminants (Priv));
12150 if Has_Discriminants (Full) then
12151 Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
12152 Set_Stored_Constraint (Full, Stored_Constraint (Priv));
12153 end if;
12155 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
12156 Set_Homonym (Full, Homonym (Priv));
12157 Set_Is_Immediately_Visible (Full, Is_Immediately_Visible (Priv));
12158 Set_Is_Public (Full, Is_Public (Priv));
12159 Set_Is_Pure (Full, Is_Pure (Priv));
12160 Set_Is_Tagged_Type (Full, Is_Tagged_Type (Priv));
12161 Set_Has_Pragma_Unmodified (Full, Has_Pragma_Unmodified (Priv));
12162 Set_Has_Pragma_Unreferenced (Full, Has_Pragma_Unreferenced (Priv));
12163 Set_Has_Pragma_Unreferenced_Objects
12164 (Full, Has_Pragma_Unreferenced_Objects
12165 (Priv));
12167 Conditional_Delay (Full, Priv);
12169 if Is_Tagged_Type (Full) then
12170 Set_Direct_Primitive_Operations (Full,
12171 Direct_Primitive_Operations (Priv));
12173 if Is_Base_Type (Priv) then
12174 Set_Class_Wide_Type (Full, Class_Wide_Type (Priv));
12175 end if;
12176 end if;
12178 Set_Is_Volatile (Full, Is_Volatile (Priv));
12179 Set_Treat_As_Volatile (Full, Treat_As_Volatile (Priv));
12180 Set_Scope (Full, Scope (Priv));
12181 Set_Next_Entity (Full, Next_Entity (Priv));
12182 Set_First_Entity (Full, First_Entity (Priv));
12183 Set_Last_Entity (Full, Last_Entity (Priv));
12185 -- If access types have been recorded for later handling, keep them in
12186 -- the full view so that they get handled when the full view freeze
12187 -- node is expanded.
12189 if Present (Freeze_Node (Priv))
12190 and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
12191 then
12192 Ensure_Freeze_Node (Full);
12193 Set_Access_Types_To_Process
12194 (Freeze_Node (Full),
12195 Access_Types_To_Process (Freeze_Node (Priv)));
12196 end if;
12198 -- Swap the two entities. Now Private is the full type entity and Full
12199 -- is the private one. They will be swapped back at the end of the
12200 -- private part. This swapping ensures that the entity that is visible
12201 -- in the private part is the full declaration.
12203 Exchange_Entities (Priv, Full);
12204 Append_Entity (Full, Scope (Full));
12205 end Copy_And_Swap;
12207 -------------------------------------
12208 -- Copy_Array_Base_Type_Attributes --
12209 -------------------------------------
12211 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
12212 begin
12213 Set_Component_Alignment (T1, Component_Alignment (T2));
12214 Set_Component_Type (T1, Component_Type (T2));
12215 Set_Component_Size (T1, Component_Size (T2));
12216 Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
12217 Set_Finalize_Storage_Only (T1, Finalize_Storage_Only (T2));
12218 Set_Has_Non_Standard_Rep (T1, Has_Non_Standard_Rep (T2));
12219 Set_Has_Task (T1, Has_Task (T2));
12220 Set_Is_Packed (T1, Is_Packed (T2));
12221 Set_Has_Aliased_Components (T1, Has_Aliased_Components (T2));
12222 Set_Has_Atomic_Components (T1, Has_Atomic_Components (T2));
12223 Set_Has_Volatile_Components (T1, Has_Volatile_Components (T2));
12224 end Copy_Array_Base_Type_Attributes;
12226 -----------------------------------
12227 -- Copy_Array_Subtype_Attributes --
12228 -----------------------------------
12230 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
12231 begin
12232 Set_Size_Info (T1, T2);
12234 Set_First_Index (T1, First_Index (T2));
12235 Set_Is_Aliased (T1, Is_Aliased (T2));
12236 Set_Is_Atomic (T1, Is_Atomic (T2));
12237 Set_Is_Volatile (T1, Is_Volatile (T2));
12238 Set_Treat_As_Volatile (T1, Treat_As_Volatile (T2));
12239 Set_Is_Constrained (T1, Is_Constrained (T2));
12240 Set_Depends_On_Private (T1, Has_Private_Component (T2));
12241 Set_First_Rep_Item (T1, First_Rep_Item (T2));
12242 Set_Convention (T1, Convention (T2));
12243 Set_Is_Limited_Composite (T1, Is_Limited_Composite (T2));
12244 Set_Is_Private_Composite (T1, Is_Private_Composite (T2));
12245 Set_Packed_Array_Type (T1, Packed_Array_Type (T2));
12246 end Copy_Array_Subtype_Attributes;
12248 -----------------------------------
12249 -- Create_Constrained_Components --
12250 -----------------------------------
12252 procedure Create_Constrained_Components
12253 (Subt : Entity_Id;
12254 Decl_Node : Node_Id;
12255 Typ : Entity_Id;
12256 Constraints : Elist_Id)
12258 Loc : constant Source_Ptr := Sloc (Subt);
12259 Comp_List : constant Elist_Id := New_Elmt_List;
12260 Parent_Type : constant Entity_Id := Etype (Typ);
12261 Assoc_List : constant List_Id := New_List;
12262 Discr_Val : Elmt_Id;
12263 Errors : Boolean;
12264 New_C : Entity_Id;
12265 Old_C : Entity_Id;
12266 Is_Static : Boolean := True;
12268 procedure Collect_Fixed_Components (Typ : Entity_Id);
12269 -- Collect parent type components that do not appear in a variant part
12271 procedure Create_All_Components;
12272 -- Iterate over Comp_List to create the components of the subtype
12274 function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
12275 -- Creates a new component from Old_Compon, copying all the fields from
12276 -- it, including its Etype, inserts the new component in the Subt entity
12277 -- chain and returns the new component.
12279 function Is_Variant_Record (T : Entity_Id) return Boolean;
12280 -- If true, and discriminants are static, collect only components from
12281 -- variants selected by discriminant values.
12283 ------------------------------
12284 -- Collect_Fixed_Components --
12285 ------------------------------
12287 procedure Collect_Fixed_Components (Typ : Entity_Id) is
12288 begin
12289 -- Build association list for discriminants, and find components of the
12290 -- variant part selected by the values of the discriminants.
12292 Old_C := First_Discriminant (Typ);
12293 Discr_Val := First_Elmt (Constraints);
12294 while Present (Old_C) loop
12295 Append_To (Assoc_List,
12296 Make_Component_Association (Loc,
12297 Choices => New_List (New_Occurrence_Of (Old_C, Loc)),
12298 Expression => New_Copy (Node (Discr_Val))));
12300 Next_Elmt (Discr_Val);
12301 Next_Discriminant (Old_C);
12302 end loop;
12304 -- The tag and the possible parent component are unconditionally in
12305 -- the subtype.
12307 if Is_Tagged_Type (Typ)
12308 or else Has_Controlled_Component (Typ)
12309 then
12310 Old_C := First_Component (Typ);
12311 while Present (Old_C) loop
12312 if Chars ((Old_C)) = Name_uTag
12313 or else Chars ((Old_C)) = Name_uParent
12314 then
12315 Append_Elmt (Old_C, Comp_List);
12316 end if;
12318 Next_Component (Old_C);
12319 end loop;
12320 end if;
12321 end Collect_Fixed_Components;
12323 ---------------------------
12324 -- Create_All_Components --
12325 ---------------------------
12327 procedure Create_All_Components is
12328 Comp : Elmt_Id;
12330 begin
12331 Comp := First_Elmt (Comp_List);
12332 while Present (Comp) loop
12333 Old_C := Node (Comp);
12334 New_C := Create_Component (Old_C);
12336 Set_Etype
12337 (New_C,
12338 Constrain_Component_Type
12339 (Old_C, Subt, Decl_Node, Typ, Constraints));
12340 Set_Is_Public (New_C, Is_Public (Subt));
12342 Next_Elmt (Comp);
12343 end loop;
12344 end Create_All_Components;
12346 ----------------------
12347 -- Create_Component --
12348 ----------------------
12350 function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
12351 New_Compon : constant Entity_Id := New_Copy (Old_Compon);
12353 begin
12354 if Ekind (Old_Compon) = E_Discriminant
12355 and then Is_Completely_Hidden (Old_Compon)
12356 then
12357 -- This is a shadow discriminant created for a discriminant of
12358 -- the parent type, which needs to be present in the subtype.
12359 -- Give the shadow discriminant an internal name that cannot
12360 -- conflict with that of visible components.
12362 Set_Chars (New_Compon, New_Internal_Name ('C'));
12363 end if;
12365 -- Set the parent so we have a proper link for freezing etc. This is
12366 -- not a real parent pointer, since of course our parent does not own
12367 -- up to us and reference us, we are an illegitimate child of the
12368 -- original parent!
12370 Set_Parent (New_Compon, Parent (Old_Compon));
12372 -- If the old component's Esize was already determined and is a
12373 -- static value, then the new component simply inherits it. Otherwise
12374 -- the old component's size may require run-time determination, but
12375 -- the new component's size still might be statically determinable
12376 -- (if, for example it has a static constraint). In that case we want
12377 -- Layout_Type to recompute the component's size, so we reset its
12378 -- size and positional fields.
12380 if Frontend_Layout_On_Target
12381 and then not Known_Static_Esize (Old_Compon)
12382 then
12383 Set_Esize (New_Compon, Uint_0);
12384 Init_Normalized_First_Bit (New_Compon);
12385 Init_Normalized_Position (New_Compon);
12386 Init_Normalized_Position_Max (New_Compon);
12387 end if;
12389 -- We do not want this node marked as Comes_From_Source, since
12390 -- otherwise it would get first class status and a separate cross-
12391 -- reference line would be generated. Illegitimate children do not
12392 -- rate such recognition.
12394 Set_Comes_From_Source (New_Compon, False);
12396 -- But it is a real entity, and a birth certificate must be properly
12397 -- registered by entering it into the entity list.
12399 Enter_Name (New_Compon);
12401 return New_Compon;
12402 end Create_Component;
12404 -----------------------
12405 -- Is_Variant_Record --
12406 -----------------------
12408 function Is_Variant_Record (T : Entity_Id) return Boolean is
12409 begin
12410 return Nkind (Parent (T)) = N_Full_Type_Declaration
12411 and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
12412 and then Present (Component_List (Type_Definition (Parent (T))))
12413 and then
12414 Present
12415 (Variant_Part (Component_List (Type_Definition (Parent (T)))));
12416 end Is_Variant_Record;
12418 -- Start of processing for Create_Constrained_Components
12420 begin
12421 pragma Assert (Subt /= Base_Type (Subt));
12422 pragma Assert (Typ = Base_Type (Typ));
12424 Set_First_Entity (Subt, Empty);
12425 Set_Last_Entity (Subt, Empty);
12427 -- Check whether constraint is fully static, in which case we can
12428 -- optimize the list of components.
12430 Discr_Val := First_Elmt (Constraints);
12431 while Present (Discr_Val) loop
12432 if not Is_OK_Static_Expression (Node (Discr_Val)) then
12433 Is_Static := False;
12434 exit;
12435 end if;
12437 Next_Elmt (Discr_Val);
12438 end loop;
12440 Set_Has_Static_Discriminants (Subt, Is_Static);
12442 Push_Scope (Subt);
12444 -- Inherit the discriminants of the parent type
12446 Add_Discriminants : declare
12447 Num_Disc : Int;
12448 Num_Gird : Int;
12450 begin
12451 Num_Disc := 0;
12452 Old_C := First_Discriminant (Typ);
12454 while Present (Old_C) loop
12455 Num_Disc := Num_Disc + 1;
12456 New_C := Create_Component (Old_C);
12457 Set_Is_Public (New_C, Is_Public (Subt));
12458 Next_Discriminant (Old_C);
12459 end loop;
12461 -- For an untagged derived subtype, the number of discriminants may
12462 -- be smaller than the number of inherited discriminants, because
12463 -- several of them may be renamed by a single new discriminant or
12464 -- constrained. In this case, add the hidden discriminants back into
12465 -- the subtype, because they need to be present if the optimizer of
12466 -- the GCC 4.x back-end decides to break apart assignments between
12467 -- objects using the parent view into member-wise assignments.
12469 Num_Gird := 0;
12471 if Is_Derived_Type (Typ)
12472 and then not Is_Tagged_Type (Typ)
12473 then
12474 Old_C := First_Stored_Discriminant (Typ);
12476 while Present (Old_C) loop
12477 Num_Gird := Num_Gird + 1;
12478 Next_Stored_Discriminant (Old_C);
12479 end loop;
12480 end if;
12482 if Num_Gird > Num_Disc then
12484 -- Find out multiple uses of new discriminants, and add hidden
12485 -- components for the extra renamed discriminants. We recognize
12486 -- multiple uses through the Corresponding_Discriminant of a
12487 -- new discriminant: if it constrains several old discriminants,
12488 -- this field points to the last one in the parent type. The
12489 -- stored discriminants of the derived type have the same name
12490 -- as those of the parent.
12492 declare
12493 Constr : Elmt_Id;
12494 New_Discr : Entity_Id;
12495 Old_Discr : Entity_Id;
12497 begin
12498 Constr := First_Elmt (Stored_Constraint (Typ));
12499 Old_Discr := First_Stored_Discriminant (Typ);
12500 while Present (Constr) loop
12501 if Is_Entity_Name (Node (Constr))
12502 and then Ekind (Entity (Node (Constr))) = E_Discriminant
12503 then
12504 New_Discr := Entity (Node (Constr));
12506 if Chars (Corresponding_Discriminant (New_Discr)) /=
12507 Chars (Old_Discr)
12508 then
12509 -- The new discriminant has been used to rename a
12510 -- subsequent old discriminant. Introduce a shadow
12511 -- component for the current old discriminant.
12513 New_C := Create_Component (Old_Discr);
12514 Set_Original_Record_Component (New_C, Old_Discr);
12515 end if;
12517 else
12518 -- The constraint has eliminated the old discriminant.
12519 -- Introduce a shadow component.
12521 New_C := Create_Component (Old_Discr);
12522 Set_Original_Record_Component (New_C, Old_Discr);
12523 end if;
12525 Next_Elmt (Constr);
12526 Next_Stored_Discriminant (Old_Discr);
12527 end loop;
12528 end;
12529 end if;
12530 end Add_Discriminants;
12532 if Is_Static
12533 and then Is_Variant_Record (Typ)
12534 then
12535 Collect_Fixed_Components (Typ);
12537 Gather_Components (
12538 Typ,
12539 Component_List (Type_Definition (Parent (Typ))),
12540 Governed_By => Assoc_List,
12541 Into => Comp_List,
12542 Report_Errors => Errors);
12543 pragma Assert (not Errors);
12545 Create_All_Components;
12547 -- If the subtype declaration is created for a tagged type derivation
12548 -- with constraints, we retrieve the record definition of the parent
12549 -- type to select the components of the proper variant.
12551 elsif Is_Static
12552 and then Is_Tagged_Type (Typ)
12553 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
12554 and then
12555 Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
12556 and then Is_Variant_Record (Parent_Type)
12557 then
12558 Collect_Fixed_Components (Typ);
12560 Gather_Components (
12561 Typ,
12562 Component_List (Type_Definition (Parent (Parent_Type))),
12563 Governed_By => Assoc_List,
12564 Into => Comp_List,
12565 Report_Errors => Errors);
12566 pragma Assert (not Errors);
12568 -- If the tagged derivation has a type extension, collect all the
12569 -- new components therein.
12571 if Present
12572 (Record_Extension_Part (Type_Definition (Parent (Typ))))
12573 then
12574 Old_C := First_Component (Typ);
12575 while Present (Old_C) loop
12576 if Original_Record_Component (Old_C) = Old_C
12577 and then Chars (Old_C) /= Name_uTag
12578 and then Chars (Old_C) /= Name_uParent
12579 then
12580 Append_Elmt (Old_C, Comp_List);
12581 end if;
12583 Next_Component (Old_C);
12584 end loop;
12585 end if;
12587 Create_All_Components;
12589 else
12590 -- If discriminants are not static, or if this is a multi-level type
12591 -- extension, we have to include all components of the parent type.
12593 Old_C := First_Component (Typ);
12594 while Present (Old_C) loop
12595 New_C := Create_Component (Old_C);
12597 Set_Etype
12598 (New_C,
12599 Constrain_Component_Type
12600 (Old_C, Subt, Decl_Node, Typ, Constraints));
12601 Set_Is_Public (New_C, Is_Public (Subt));
12603 Next_Component (Old_C);
12604 end loop;
12605 end if;
12607 End_Scope;
12608 end Create_Constrained_Components;
12610 ------------------------------------------
12611 -- Decimal_Fixed_Point_Type_Declaration --
12612 ------------------------------------------
12614 procedure Decimal_Fixed_Point_Type_Declaration
12615 (T : Entity_Id;
12616 Def : Node_Id)
12618 Loc : constant Source_Ptr := Sloc (Def);
12619 Digs_Expr : constant Node_Id := Digits_Expression (Def);
12620 Delta_Expr : constant Node_Id := Delta_Expression (Def);
12621 Implicit_Base : Entity_Id;
12622 Digs_Val : Uint;
12623 Delta_Val : Ureal;
12624 Scale_Val : Uint;
12625 Bound_Val : Ureal;
12627 begin
12628 Check_SPARK_Restriction
12629 ("decimal fixed point type is not allowed", Def);
12630 Check_Restriction (No_Fixed_Point, Def);
12632 -- Create implicit base type
12634 Implicit_Base :=
12635 Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
12636 Set_Etype (Implicit_Base, Implicit_Base);
12638 -- Analyze and process delta expression
12640 Analyze_And_Resolve (Delta_Expr, Universal_Real);
12642 Check_Delta_Expression (Delta_Expr);
12643 Delta_Val := Expr_Value_R (Delta_Expr);
12645 -- Check delta is power of 10, and determine scale value from it
12647 declare
12648 Val : Ureal;
12650 begin
12651 Scale_Val := Uint_0;
12652 Val := Delta_Val;
12654 if Val < Ureal_1 then
12655 while Val < Ureal_1 loop
12656 Val := Val * Ureal_10;
12657 Scale_Val := Scale_Val + 1;
12658 end loop;
12660 if Scale_Val > 18 then
12661 Error_Msg_N ("scale exceeds maximum value of 18", Def);
12662 Scale_Val := UI_From_Int (+18);
12663 end if;
12665 else
12666 while Val > Ureal_1 loop
12667 Val := Val / Ureal_10;
12668 Scale_Val := Scale_Val - 1;
12669 end loop;
12671 if Scale_Val < -18 then
12672 Error_Msg_N ("scale is less than minimum value of -18", Def);
12673 Scale_Val := UI_From_Int (-18);
12674 end if;
12675 end if;
12677 if Val /= Ureal_1 then
12678 Error_Msg_N ("delta expression must be a power of 10", Def);
12679 Delta_Val := Ureal_10 ** (-Scale_Val);
12680 end if;
12681 end;
12683 -- Set delta, scale and small (small = delta for decimal type)
12685 Set_Delta_Value (Implicit_Base, Delta_Val);
12686 Set_Scale_Value (Implicit_Base, Scale_Val);
12687 Set_Small_Value (Implicit_Base, Delta_Val);
12689 -- Analyze and process digits expression
12691 Analyze_And_Resolve (Digs_Expr, Any_Integer);
12692 Check_Digits_Expression (Digs_Expr);
12693 Digs_Val := Expr_Value (Digs_Expr);
12695 if Digs_Val > 18 then
12696 Digs_Val := UI_From_Int (+18);
12697 Error_Msg_N ("digits value out of range, maximum is 18", Digs_Expr);
12698 end if;
12700 Set_Digits_Value (Implicit_Base, Digs_Val);
12701 Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
12703 -- Set range of base type from digits value for now. This will be
12704 -- expanded to represent the true underlying base range by Freeze.
12706 Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
12708 -- Note: We leave size as zero for now, size will be set at freeze
12709 -- time. We have to do this for ordinary fixed-point, because the size
12710 -- depends on the specified small, and we might as well do the same for
12711 -- decimal fixed-point.
12713 pragma Assert (Esize (Implicit_Base) = Uint_0);
12715 -- If there are bounds given in the declaration use them as the
12716 -- bounds of the first named subtype.
12718 if Present (Real_Range_Specification (Def)) then
12719 declare
12720 RRS : constant Node_Id := Real_Range_Specification (Def);
12721 Low : constant Node_Id := Low_Bound (RRS);
12722 High : constant Node_Id := High_Bound (RRS);
12723 Low_Val : Ureal;
12724 High_Val : Ureal;
12726 begin
12727 Analyze_And_Resolve (Low, Any_Real);
12728 Analyze_And_Resolve (High, Any_Real);
12729 Check_Real_Bound (Low);
12730 Check_Real_Bound (High);
12731 Low_Val := Expr_Value_R (Low);
12732 High_Val := Expr_Value_R (High);
12734 if Low_Val < (-Bound_Val) then
12735 Error_Msg_N
12736 ("range low bound too small for digits value", Low);
12737 Low_Val := -Bound_Val;
12738 end if;
12740 if High_Val > Bound_Val then
12741 Error_Msg_N
12742 ("range high bound too large for digits value", High);
12743 High_Val := Bound_Val;
12744 end if;
12746 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
12747 end;
12749 -- If no explicit range, use range that corresponds to given
12750 -- digits value. This will end up as the final range for the
12751 -- first subtype.
12753 else
12754 Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
12755 end if;
12757 -- Complete entity for first subtype
12759 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
12760 Set_Etype (T, Implicit_Base);
12761 Set_Size_Info (T, Implicit_Base);
12762 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
12763 Set_Digits_Value (T, Digs_Val);
12764 Set_Delta_Value (T, Delta_Val);
12765 Set_Small_Value (T, Delta_Val);
12766 Set_Scale_Value (T, Scale_Val);
12767 Set_Is_Constrained (T);
12768 end Decimal_Fixed_Point_Type_Declaration;
12770 -----------------------------------
12771 -- Derive_Progenitor_Subprograms --
12772 -----------------------------------
12774 procedure Derive_Progenitor_Subprograms
12775 (Parent_Type : Entity_Id;
12776 Tagged_Type : Entity_Id)
12778 E : Entity_Id;
12779 Elmt : Elmt_Id;
12780 Iface : Entity_Id;
12781 Iface_Elmt : Elmt_Id;
12782 Iface_Subp : Entity_Id;
12783 New_Subp : Entity_Id := Empty;
12784 Prim_Elmt : Elmt_Id;
12785 Subp : Entity_Id;
12786 Typ : Entity_Id;
12788 begin
12789 pragma Assert (Ada_Version >= Ada_2005
12790 and then Is_Record_Type (Tagged_Type)
12791 and then Is_Tagged_Type (Tagged_Type)
12792 and then Has_Interfaces (Tagged_Type));
12794 -- Step 1: Transfer to the full-view primitives associated with the
12795 -- partial-view that cover interface primitives. Conceptually this
12796 -- work should be done later by Process_Full_View; done here to
12797 -- simplify its implementation at later stages. It can be safely
12798 -- done here because interfaces must be visible in the partial and
12799 -- private view (RM 7.3(7.3/2)).
12801 -- Small optimization: This work is only required if the parent is
12802 -- abstract. If the tagged type is not abstract, it cannot have
12803 -- abstract primitives (the only entities in the list of primitives of
12804 -- non-abstract tagged types that can reference abstract primitives
12805 -- through its Alias attribute are the internal entities that have
12806 -- attribute Interface_Alias, and these entities are generated later
12807 -- by Add_Internal_Interface_Entities).
12809 if In_Private_Part (Current_Scope)
12810 and then Is_Abstract_Type (Parent_Type)
12811 then
12812 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
12813 while Present (Elmt) loop
12814 Subp := Node (Elmt);
12816 -- At this stage it is not possible to have entities in the list
12817 -- of primitives that have attribute Interface_Alias
12819 pragma Assert (No (Interface_Alias (Subp)));
12821 Typ := Find_Dispatching_Type (Ultimate_Alias (Subp));
12823 if Is_Interface (Typ) then
12824 E := Find_Primitive_Covering_Interface
12825 (Tagged_Type => Tagged_Type,
12826 Iface_Prim => Subp);
12828 if Present (E)
12829 and then Find_Dispatching_Type (Ultimate_Alias (E)) /= Typ
12830 then
12831 Replace_Elmt (Elmt, E);
12832 Remove_Homonym (Subp);
12833 end if;
12834 end if;
12836 Next_Elmt (Elmt);
12837 end loop;
12838 end if;
12840 -- Step 2: Add primitives of progenitors that are not implemented by
12841 -- parents of Tagged_Type
12843 if Present (Interfaces (Base_Type (Tagged_Type))) then
12844 Iface_Elmt := First_Elmt (Interfaces (Base_Type (Tagged_Type)));
12845 while Present (Iface_Elmt) loop
12846 Iface := Node (Iface_Elmt);
12848 Prim_Elmt := First_Elmt (Primitive_Operations (Iface));
12849 while Present (Prim_Elmt) loop
12850 Iface_Subp := Node (Prim_Elmt);
12852 -- Exclude derivation of predefined primitives except those
12853 -- that come from source, or are inherited from one that comes
12854 -- from source. Required to catch declarations of equality
12855 -- operators of interfaces. For example:
12857 -- type Iface is interface;
12858 -- function "=" (Left, Right : Iface) return Boolean;
12860 if not Is_Predefined_Dispatching_Operation (Iface_Subp)
12861 or else Comes_From_Source (Ultimate_Alias (Iface_Subp))
12862 then
12863 E := Find_Primitive_Covering_Interface
12864 (Tagged_Type => Tagged_Type,
12865 Iface_Prim => Iface_Subp);
12867 -- If not found we derive a new primitive leaving its alias
12868 -- attribute referencing the interface primitive
12870 if No (E) then
12871 Derive_Subprogram
12872 (New_Subp, Iface_Subp, Tagged_Type, Iface);
12874 -- Ada 2012 (AI05-0197): If the covering primitive's name
12875 -- differs from the name of the interface primitive then it
12876 -- is a private primitive inherited from a parent type. In
12877 -- such case, given that Tagged_Type covers the interface,
12878 -- the inherited private primitive becomes visible. For such
12879 -- purpose we add a new entity that renames the inherited
12880 -- private primitive.
12882 elsif Chars (E) /= Chars (Iface_Subp) then
12883 pragma Assert (Has_Suffix (E, 'P'));
12884 Derive_Subprogram
12885 (New_Subp, Iface_Subp, Tagged_Type, Iface);
12886 Set_Alias (New_Subp, E);
12887 Set_Is_Abstract_Subprogram (New_Subp,
12888 Is_Abstract_Subprogram (E));
12890 -- Propagate to the full view interface entities associated
12891 -- with the partial view
12893 elsif In_Private_Part (Current_Scope)
12894 and then Present (Alias (E))
12895 and then Alias (E) = Iface_Subp
12896 and then
12897 List_Containing (Parent (E)) /=
12898 Private_Declarations
12899 (Specification
12900 (Unit_Declaration_Node (Current_Scope)))
12901 then
12902 Append_Elmt (E, Primitive_Operations (Tagged_Type));
12903 end if;
12904 end if;
12906 Next_Elmt (Prim_Elmt);
12907 end loop;
12909 Next_Elmt (Iface_Elmt);
12910 end loop;
12911 end if;
12912 end Derive_Progenitor_Subprograms;
12914 -----------------------
12915 -- Derive_Subprogram --
12916 -----------------------
12918 procedure Derive_Subprogram
12919 (New_Subp : in out Entity_Id;
12920 Parent_Subp : Entity_Id;
12921 Derived_Type : Entity_Id;
12922 Parent_Type : Entity_Id;
12923 Actual_Subp : Entity_Id := Empty)
12925 Formal : Entity_Id;
12926 -- Formal parameter of parent primitive operation
12928 Formal_Of_Actual : Entity_Id;
12929 -- Formal parameter of actual operation, when the derivation is to
12930 -- create a renaming for a primitive operation of an actual in an
12931 -- instantiation.
12933 New_Formal : Entity_Id;
12934 -- Formal of inherited operation
12936 Visible_Subp : Entity_Id := Parent_Subp;
12938 function Is_Private_Overriding return Boolean;
12939 -- If Subp is a private overriding of a visible operation, the inherited
12940 -- operation derives from the overridden op (even though its body is the
12941 -- overriding one) and the inherited operation is visible now. See
12942 -- sem_disp to see the full details of the handling of the overridden
12943 -- subprogram, which is removed from the list of primitive operations of
12944 -- the type. The overridden subprogram is saved locally in Visible_Subp,
12945 -- and used to diagnose abstract operations that need overriding in the
12946 -- derived type.
12948 procedure Replace_Type (Id, New_Id : Entity_Id);
12949 -- When the type is an anonymous access type, create a new access type
12950 -- designating the derived type.
12952 procedure Set_Derived_Name;
12953 -- This procedure sets the appropriate Chars name for New_Subp. This
12954 -- is normally just a copy of the parent name. An exception arises for
12955 -- type support subprograms, where the name is changed to reflect the
12956 -- name of the derived type, e.g. if type foo is derived from type bar,
12957 -- then a procedure barDA is derived with a name fooDA.
12959 ---------------------------
12960 -- Is_Private_Overriding --
12961 ---------------------------
12963 function Is_Private_Overriding return Boolean is
12964 Prev : Entity_Id;
12966 begin
12967 -- If the parent is not a dispatching operation there is no
12968 -- need to investigate overridings
12970 if not Is_Dispatching_Operation (Parent_Subp) then
12971 return False;
12972 end if;
12974 -- The visible operation that is overridden is a homonym of the
12975 -- parent subprogram. We scan the homonym chain to find the one
12976 -- whose alias is the subprogram we are deriving.
12978 Prev := Current_Entity (Parent_Subp);
12979 while Present (Prev) loop
12980 if Ekind (Prev) = Ekind (Parent_Subp)
12981 and then Alias (Prev) = Parent_Subp
12982 and then Scope (Parent_Subp) = Scope (Prev)
12983 and then not Is_Hidden (Prev)
12984 then
12985 Visible_Subp := Prev;
12986 return True;
12987 end if;
12989 Prev := Homonym (Prev);
12990 end loop;
12992 return False;
12993 end Is_Private_Overriding;
12995 ------------------
12996 -- Replace_Type --
12997 ------------------
12999 procedure Replace_Type (Id, New_Id : Entity_Id) is
13000 Acc_Type : Entity_Id;
13001 Par : constant Node_Id := Parent (Derived_Type);
13003 begin
13004 -- When the type is an anonymous access type, create a new access
13005 -- type designating the derived type. This itype must be elaborated
13006 -- at the point of the derivation, not on subsequent calls that may
13007 -- be out of the proper scope for Gigi, so we insert a reference to
13008 -- it after the derivation.
13010 if Ekind (Etype (Id)) = E_Anonymous_Access_Type then
13011 declare
13012 Desig_Typ : Entity_Id := Designated_Type (Etype (Id));
13014 begin
13015 if Ekind (Desig_Typ) = E_Record_Type_With_Private
13016 and then Present (Full_View (Desig_Typ))
13017 and then not Is_Private_Type (Parent_Type)
13018 then
13019 Desig_Typ := Full_View (Desig_Typ);
13020 end if;
13022 if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
13024 -- Ada 2005 (AI-251): Handle also derivations of abstract
13025 -- interface primitives.
13027 or else (Is_Interface (Desig_Typ)
13028 and then not Is_Class_Wide_Type (Desig_Typ))
13029 then
13030 Acc_Type := New_Copy (Etype (Id));
13031 Set_Etype (Acc_Type, Acc_Type);
13032 Set_Scope (Acc_Type, New_Subp);
13034 -- Compute size of anonymous access type
13036 if Is_Array_Type (Desig_Typ)
13037 and then not Is_Constrained (Desig_Typ)
13038 then
13039 Init_Size (Acc_Type, 2 * System_Address_Size);
13040 else
13041 Init_Size (Acc_Type, System_Address_Size);
13042 end if;
13044 Init_Alignment (Acc_Type);
13045 Set_Directly_Designated_Type (Acc_Type, Derived_Type);
13047 Set_Etype (New_Id, Acc_Type);
13048 Set_Scope (New_Id, New_Subp);
13050 -- Create a reference to it
13051 Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
13053 else
13054 Set_Etype (New_Id, Etype (Id));
13055 end if;
13056 end;
13058 elsif Base_Type (Etype (Id)) = Base_Type (Parent_Type)
13059 or else
13060 (Ekind (Etype (Id)) = E_Record_Type_With_Private
13061 and then Present (Full_View (Etype (Id)))
13062 and then
13063 Base_Type (Full_View (Etype (Id))) = Base_Type (Parent_Type))
13064 then
13065 -- Constraint checks on formals are generated during expansion,
13066 -- based on the signature of the original subprogram. The bounds
13067 -- of the derived type are not relevant, and thus we can use
13068 -- the base type for the formals. However, the return type may be
13069 -- used in a context that requires that the proper static bounds
13070 -- be used (a case statement, for example) and for those cases
13071 -- we must use the derived type (first subtype), not its base.
13073 -- If the derived_type_definition has no constraints, we know that
13074 -- the derived type has the same constraints as the first subtype
13075 -- of the parent, and we can also use it rather than its base,
13076 -- which can lead to more efficient code.
13078 if Etype (Id) = Parent_Type then
13079 if Is_Scalar_Type (Parent_Type)
13080 and then
13081 Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
13082 then
13083 Set_Etype (New_Id, Derived_Type);
13085 elsif Nkind (Par) = N_Full_Type_Declaration
13086 and then
13087 Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
13088 and then
13089 Is_Entity_Name
13090 (Subtype_Indication (Type_Definition (Par)))
13091 then
13092 Set_Etype (New_Id, Derived_Type);
13094 else
13095 Set_Etype (New_Id, Base_Type (Derived_Type));
13096 end if;
13098 else
13099 Set_Etype (New_Id, Base_Type (Derived_Type));
13100 end if;
13102 else
13103 Set_Etype (New_Id, Etype (Id));
13104 end if;
13105 end Replace_Type;
13107 ----------------------
13108 -- Set_Derived_Name --
13109 ----------------------
13111 procedure Set_Derived_Name is
13112 Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
13113 begin
13114 if Nm = TSS_Null then
13115 Set_Chars (New_Subp, Chars (Parent_Subp));
13116 else
13117 Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
13118 end if;
13119 end Set_Derived_Name;
13121 -- Start of processing for Derive_Subprogram
13123 begin
13124 New_Subp :=
13125 New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
13126 Set_Ekind (New_Subp, Ekind (Parent_Subp));
13127 Set_Contract (New_Subp, Make_Contract (Sloc (New_Subp)));
13129 -- Check whether the inherited subprogram is a private operation that
13130 -- should be inherited but not yet made visible. Such subprograms can
13131 -- become visible at a later point (e.g., the private part of a public
13132 -- child unit) via Declare_Inherited_Private_Subprograms. If the
13133 -- following predicate is true, then this is not such a private
13134 -- operation and the subprogram simply inherits the name of the parent
13135 -- subprogram. Note the special check for the names of controlled
13136 -- operations, which are currently exempted from being inherited with
13137 -- a hidden name because they must be findable for generation of
13138 -- implicit run-time calls.
13140 if not Is_Hidden (Parent_Subp)
13141 or else Is_Internal (Parent_Subp)
13142 or else Is_Private_Overriding
13143 or else Is_Internal_Name (Chars (Parent_Subp))
13144 or else Chars (Parent_Subp) = Name_Initialize
13145 or else Chars (Parent_Subp) = Name_Adjust
13146 or else Chars (Parent_Subp) = Name_Finalize
13147 then
13148 Set_Derived_Name;
13150 -- An inherited dispatching equality will be overridden by an internally
13151 -- generated one, or by an explicit one, so preserve its name and thus
13152 -- its entry in the dispatch table. Otherwise, if Parent_Subp is a
13153 -- private operation it may become invisible if the full view has
13154 -- progenitors, and the dispatch table will be malformed.
13155 -- We check that the type is limited to handle the anomalous declaration
13156 -- of Limited_Controlled, which is derived from a non-limited type, and
13157 -- which is handled specially elsewhere as well.
13159 elsif Chars (Parent_Subp) = Name_Op_Eq
13160 and then Is_Dispatching_Operation (Parent_Subp)
13161 and then Etype (Parent_Subp) = Standard_Boolean
13162 and then not Is_Limited_Type (Etype (First_Formal (Parent_Subp)))
13163 and then
13164 Etype (First_Formal (Parent_Subp)) =
13165 Etype (Next_Formal (First_Formal (Parent_Subp)))
13166 then
13167 Set_Derived_Name;
13169 -- If parent is hidden, this can be a regular derivation if the
13170 -- parent is immediately visible in a non-instantiating context,
13171 -- or if we are in the private part of an instance. This test
13172 -- should still be refined ???
13174 -- The test for In_Instance_Not_Visible avoids inheriting the derived
13175 -- operation as a non-visible operation in cases where the parent
13176 -- subprogram might not be visible now, but was visible within the
13177 -- original generic, so it would be wrong to make the inherited
13178 -- subprogram non-visible now. (Not clear if this test is fully
13179 -- correct; are there any cases where we should declare the inherited
13180 -- operation as not visible to avoid it being overridden, e.g., when
13181 -- the parent type is a generic actual with private primitives ???)
13183 -- (they should be treated the same as other private inherited
13184 -- subprograms, but it's not clear how to do this cleanly). ???
13186 elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
13187 and then Is_Immediately_Visible (Parent_Subp)
13188 and then not In_Instance)
13189 or else In_Instance_Not_Visible
13190 then
13191 Set_Derived_Name;
13193 -- Ada 2005 (AI-251): Regular derivation if the parent subprogram
13194 -- overrides an interface primitive because interface primitives
13195 -- must be visible in the partial view of the parent (RM 7.3 (7.3/2))
13197 elsif Ada_Version >= Ada_2005
13198 and then Is_Dispatching_Operation (Parent_Subp)
13199 and then Covers_Some_Interface (Parent_Subp)
13200 then
13201 Set_Derived_Name;
13203 -- Otherwise, the type is inheriting a private operation, so enter
13204 -- it with a special name so it can't be overridden.
13206 else
13207 Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
13208 end if;
13210 Set_Parent (New_Subp, Parent (Derived_Type));
13212 if Present (Actual_Subp) then
13213 Replace_Type (Actual_Subp, New_Subp);
13214 else
13215 Replace_Type (Parent_Subp, New_Subp);
13216 end if;
13218 Conditional_Delay (New_Subp, Parent_Subp);
13220 -- If we are creating a renaming for a primitive operation of an
13221 -- actual of a generic derived type, we must examine the signature
13222 -- of the actual primitive, not that of the generic formal, which for
13223 -- example may be an interface. However the name and initial value
13224 -- of the inherited operation are those of the formal primitive.
13226 Formal := First_Formal (Parent_Subp);
13228 if Present (Actual_Subp) then
13229 Formal_Of_Actual := First_Formal (Actual_Subp);
13230 else
13231 Formal_Of_Actual := Empty;
13232 end if;
13234 while Present (Formal) loop
13235 New_Formal := New_Copy (Formal);
13237 -- Normally we do not go copying parents, but in the case of
13238 -- formals, we need to link up to the declaration (which is the
13239 -- parameter specification), and it is fine to link up to the
13240 -- original formal's parameter specification in this case.
13242 Set_Parent (New_Formal, Parent (Formal));
13243 Append_Entity (New_Formal, New_Subp);
13245 if Present (Formal_Of_Actual) then
13246 Replace_Type (Formal_Of_Actual, New_Formal);
13247 Next_Formal (Formal_Of_Actual);
13248 else
13249 Replace_Type (Formal, New_Formal);
13250 end if;
13252 Next_Formal (Formal);
13253 end loop;
13255 -- If this derivation corresponds to a tagged generic actual, then
13256 -- primitive operations rename those of the actual. Otherwise the
13257 -- primitive operations rename those of the parent type, If the parent
13258 -- renames an intrinsic operator, so does the new subprogram. We except
13259 -- concatenation, which is always properly typed, and does not get
13260 -- expanded as other intrinsic operations.
13262 if No (Actual_Subp) then
13263 if Is_Intrinsic_Subprogram (Parent_Subp) then
13264 Set_Is_Intrinsic_Subprogram (New_Subp);
13266 if Present (Alias (Parent_Subp))
13267 and then Chars (Parent_Subp) /= Name_Op_Concat
13268 then
13269 Set_Alias (New_Subp, Alias (Parent_Subp));
13270 else
13271 Set_Alias (New_Subp, Parent_Subp);
13272 end if;
13274 else
13275 Set_Alias (New_Subp, Parent_Subp);
13276 end if;
13278 else
13279 Set_Alias (New_Subp, Actual_Subp);
13280 end if;
13282 -- Derived subprograms of a tagged type must inherit the convention
13283 -- of the parent subprogram (a requirement of AI-117). Derived
13284 -- subprograms of untagged types simply get convention Ada by default.
13286 if Is_Tagged_Type (Derived_Type) then
13287 Set_Convention (New_Subp, Convention (Parent_Subp));
13288 end if;
13290 -- Predefined controlled operations retain their name even if the parent
13291 -- is hidden (see above), but they are not primitive operations if the
13292 -- ancestor is not visible, for example if the parent is a private
13293 -- extension completed with a controlled extension. Note that a full
13294 -- type that is controlled can break privacy: the flag Is_Controlled is
13295 -- set on both views of the type.
13297 if Is_Controlled (Parent_Type)
13298 and then
13299 (Chars (Parent_Subp) = Name_Initialize
13300 or else Chars (Parent_Subp) = Name_Adjust
13301 or else Chars (Parent_Subp) = Name_Finalize)
13302 and then Is_Hidden (Parent_Subp)
13303 and then not Is_Visibly_Controlled (Parent_Type)
13304 then
13305 Set_Is_Hidden (New_Subp);
13306 end if;
13308 Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
13309 Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
13311 if Ekind (Parent_Subp) = E_Procedure then
13312 Set_Is_Valued_Procedure
13313 (New_Subp, Is_Valued_Procedure (Parent_Subp));
13314 else
13315 Set_Has_Controlling_Result
13316 (New_Subp, Has_Controlling_Result (Parent_Subp));
13317 end if;
13319 -- No_Return must be inherited properly. If this is overridden in the
13320 -- case of a dispatching operation, then a check is made in Sem_Disp
13321 -- that the overriding operation is also No_Return (no such check is
13322 -- required for the case of non-dispatching operation.
13324 Set_No_Return (New_Subp, No_Return (Parent_Subp));
13326 -- A derived function with a controlling result is abstract. If the
13327 -- Derived_Type is a nonabstract formal generic derived type, then
13328 -- inherited operations are not abstract: the required check is done at
13329 -- instantiation time. If the derivation is for a generic actual, the
13330 -- function is not abstract unless the actual is.
13332 if Is_Generic_Type (Derived_Type)
13333 and then not Is_Abstract_Type (Derived_Type)
13334 then
13335 null;
13337 -- Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
13338 -- properties of the subprogram, as defined in RM-3.9.3(4/2-6/2).
13340 elsif Ada_Version >= Ada_2005
13341 and then (Is_Abstract_Subprogram (Alias (New_Subp))
13342 or else (Is_Tagged_Type (Derived_Type)
13343 and then Etype (New_Subp) = Derived_Type
13344 and then not Is_Null_Extension (Derived_Type))
13345 or else (Is_Tagged_Type (Derived_Type)
13346 and then Ekind (Etype (New_Subp)) =
13347 E_Anonymous_Access_Type
13348 and then Designated_Type (Etype (New_Subp)) =
13349 Derived_Type
13350 and then not Is_Null_Extension (Derived_Type)))
13351 and then No (Actual_Subp)
13352 then
13353 if not Is_Tagged_Type (Derived_Type)
13354 or else Is_Abstract_Type (Derived_Type)
13355 or else Is_Abstract_Subprogram (Alias (New_Subp))
13356 then
13357 Set_Is_Abstract_Subprogram (New_Subp);
13358 else
13359 Set_Requires_Overriding (New_Subp);
13360 end if;
13362 elsif Ada_Version < Ada_2005
13363 and then (Is_Abstract_Subprogram (Alias (New_Subp))
13364 or else (Is_Tagged_Type (Derived_Type)
13365 and then Etype (New_Subp) = Derived_Type
13366 and then No (Actual_Subp)))
13367 then
13368 Set_Is_Abstract_Subprogram (New_Subp);
13370 -- AI05-0097 : an inherited operation that dispatches on result is
13371 -- abstract if the derived type is abstract, even if the parent type
13372 -- is concrete and the derived type is a null extension.
13374 elsif Has_Controlling_Result (Alias (New_Subp))
13375 and then Is_Abstract_Type (Etype (New_Subp))
13376 then
13377 Set_Is_Abstract_Subprogram (New_Subp);
13379 -- Finally, if the parent type is abstract we must verify that all
13380 -- inherited operations are either non-abstract or overridden, or that
13381 -- the derived type itself is abstract (this check is performed at the
13382 -- end of a package declaration, in Check_Abstract_Overriding). A
13383 -- private overriding in the parent type will not be visible in the
13384 -- derivation if we are not in an inner package or in a child unit of
13385 -- the parent type, in which case the abstractness of the inherited
13386 -- operation is carried to the new subprogram.
13388 elsif Is_Abstract_Type (Parent_Type)
13389 and then not In_Open_Scopes (Scope (Parent_Type))
13390 and then Is_Private_Overriding
13391 and then Is_Abstract_Subprogram (Visible_Subp)
13392 then
13393 if No (Actual_Subp) then
13394 Set_Alias (New_Subp, Visible_Subp);
13395 Set_Is_Abstract_Subprogram (New_Subp, True);
13397 else
13398 -- If this is a derivation for an instance of a formal derived
13399 -- type, abstractness comes from the primitive operation of the
13400 -- actual, not from the operation inherited from the ancestor.
13402 Set_Is_Abstract_Subprogram
13403 (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
13404 end if;
13405 end if;
13407 New_Overloaded_Entity (New_Subp, Derived_Type);
13409 -- Check for case of a derived subprogram for the instantiation of a
13410 -- formal derived tagged type, if so mark the subprogram as dispatching
13411 -- and inherit the dispatching attributes of the actual subprogram. The
13412 -- derived subprogram is effectively renaming of the actual subprogram,
13413 -- so it needs to have the same attributes as the actual.
13415 if Present (Actual_Subp)
13416 and then Is_Dispatching_Operation (Actual_Subp)
13417 then
13418 Set_Is_Dispatching_Operation (New_Subp);
13420 if Present (DTC_Entity (Actual_Subp)) then
13421 Set_DTC_Entity (New_Subp, DTC_Entity (Actual_Subp));
13422 Set_DT_Position (New_Subp, DT_Position (Actual_Subp));
13423 end if;
13424 end if;
13426 -- Indicate that a derived subprogram does not require a body and that
13427 -- it does not require processing of default expressions.
13429 Set_Has_Completion (New_Subp);
13430 Set_Default_Expressions_Processed (New_Subp);
13432 if Ekind (New_Subp) = E_Function then
13433 Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
13434 end if;
13435 end Derive_Subprogram;
13437 ------------------------
13438 -- Derive_Subprograms --
13439 ------------------------
13441 procedure Derive_Subprograms
13442 (Parent_Type : Entity_Id;
13443 Derived_Type : Entity_Id;
13444 Generic_Actual : Entity_Id := Empty)
13446 Op_List : constant Elist_Id :=
13447 Collect_Primitive_Operations (Parent_Type);
13449 function Check_Derived_Type return Boolean;
13450 -- Check that all the entities derived from Parent_Type are found in
13451 -- the list of primitives of Derived_Type exactly in the same order.
13453 procedure Derive_Interface_Subprogram
13454 (New_Subp : in out Entity_Id;
13455 Subp : Entity_Id;
13456 Actual_Subp : Entity_Id);
13457 -- Derive New_Subp from the ultimate alias of the parent subprogram Subp
13458 -- (which is an interface primitive). If Generic_Actual is present then
13459 -- Actual_Subp is the actual subprogram corresponding with the generic
13460 -- subprogram Subp.
13462 function Check_Derived_Type return Boolean is
13463 E : Entity_Id;
13464 Elmt : Elmt_Id;
13465 List : Elist_Id;
13466 New_Subp : Entity_Id;
13467 Op_Elmt : Elmt_Id;
13468 Subp : Entity_Id;
13470 begin
13471 -- Traverse list of entities in the current scope searching for
13472 -- an incomplete type whose full-view is derived type
13474 E := First_Entity (Scope (Derived_Type));
13475 while Present (E)
13476 and then E /= Derived_Type
13477 loop
13478 if Ekind (E) = E_Incomplete_Type
13479 and then Present (Full_View (E))
13480 and then Full_View (E) = Derived_Type
13481 then
13482 -- Disable this test if Derived_Type completes an incomplete
13483 -- type because in such case more primitives can be added
13484 -- later to the list of primitives of Derived_Type by routine
13485 -- Process_Incomplete_Dependents
13487 return True;
13488 end if;
13490 E := Next_Entity (E);
13491 end loop;
13493 List := Collect_Primitive_Operations (Derived_Type);
13494 Elmt := First_Elmt (List);
13496 Op_Elmt := First_Elmt (Op_List);
13497 while Present (Op_Elmt) loop
13498 Subp := Node (Op_Elmt);
13499 New_Subp := Node (Elmt);
13501 -- At this early stage Derived_Type has no entities with attribute
13502 -- Interface_Alias. In addition, such primitives are always
13503 -- located at the end of the list of primitives of Parent_Type.
13504 -- Therefore, if found we can safely stop processing pending
13505 -- entities.
13507 exit when Present (Interface_Alias (Subp));
13509 -- Handle hidden entities
13511 if not Is_Predefined_Dispatching_Operation (Subp)
13512 and then Is_Hidden (Subp)
13513 then
13514 if Present (New_Subp)
13515 and then Primitive_Names_Match (Subp, New_Subp)
13516 then
13517 Next_Elmt (Elmt);
13518 end if;
13520 else
13521 if not Present (New_Subp)
13522 or else Ekind (Subp) /= Ekind (New_Subp)
13523 or else not Primitive_Names_Match (Subp, New_Subp)
13524 then
13525 return False;
13526 end if;
13528 Next_Elmt (Elmt);
13529 end if;
13531 Next_Elmt (Op_Elmt);
13532 end loop;
13534 return True;
13535 end Check_Derived_Type;
13537 ---------------------------------
13538 -- Derive_Interface_Subprogram --
13539 ---------------------------------
13541 procedure Derive_Interface_Subprogram
13542 (New_Subp : in out Entity_Id;
13543 Subp : Entity_Id;
13544 Actual_Subp : Entity_Id)
13546 Iface_Subp : constant Entity_Id := Ultimate_Alias (Subp);
13547 Iface_Type : constant Entity_Id := Find_Dispatching_Type (Iface_Subp);
13549 begin
13550 pragma Assert (Is_Interface (Iface_Type));
13552 Derive_Subprogram
13553 (New_Subp => New_Subp,
13554 Parent_Subp => Iface_Subp,
13555 Derived_Type => Derived_Type,
13556 Parent_Type => Iface_Type,
13557 Actual_Subp => Actual_Subp);
13559 -- Given that this new interface entity corresponds with a primitive
13560 -- of the parent that was not overridden we must leave it associated
13561 -- with its parent primitive to ensure that it will share the same
13562 -- dispatch table slot when overridden.
13564 if No (Actual_Subp) then
13565 Set_Alias (New_Subp, Subp);
13567 -- For instantiations this is not needed since the previous call to
13568 -- Derive_Subprogram leaves the entity well decorated.
13570 else
13571 pragma Assert (Alias (New_Subp) = Actual_Subp);
13572 null;
13573 end if;
13574 end Derive_Interface_Subprogram;
13576 -- Local variables
13578 Alias_Subp : Entity_Id;
13579 Act_List : Elist_Id;
13580 Act_Elmt : Elmt_Id := No_Elmt;
13581 Act_Subp : Entity_Id := Empty;
13582 Elmt : Elmt_Id;
13583 Need_Search : Boolean := False;
13584 New_Subp : Entity_Id := Empty;
13585 Parent_Base : Entity_Id;
13586 Subp : Entity_Id;
13588 -- Start of processing for Derive_Subprograms
13590 begin
13591 if Ekind (Parent_Type) = E_Record_Type_With_Private
13592 and then Has_Discriminants (Parent_Type)
13593 and then Present (Full_View (Parent_Type))
13594 then
13595 Parent_Base := Full_View (Parent_Type);
13596 else
13597 Parent_Base := Parent_Type;
13598 end if;
13600 if Present (Generic_Actual) then
13601 Act_List := Collect_Primitive_Operations (Generic_Actual);
13602 Act_Elmt := First_Elmt (Act_List);
13603 end if;
13605 -- Derive primitives inherited from the parent. Note that if the generic
13606 -- actual is present, this is not really a type derivation, it is a
13607 -- completion within an instance.
13609 -- Case 1: Derived_Type does not implement interfaces
13611 if not Is_Tagged_Type (Derived_Type)
13612 or else (not Has_Interfaces (Derived_Type)
13613 and then not (Present (Generic_Actual)
13614 and then
13615 Has_Interfaces (Generic_Actual)))
13616 then
13617 Elmt := First_Elmt (Op_List);
13618 while Present (Elmt) loop
13619 Subp := Node (Elmt);
13621 -- Literals are derived earlier in the process of building the
13622 -- derived type, and are skipped here.
13624 if Ekind (Subp) = E_Enumeration_Literal then
13625 null;
13627 -- The actual is a direct descendant and the common primitive
13628 -- operations appear in the same order.
13630 -- If the generic parent type is present, the derived type is an
13631 -- instance of a formal derived type, and within the instance its
13632 -- operations are those of the actual. We derive from the formal
13633 -- type but make the inherited operations aliases of the
13634 -- corresponding operations of the actual.
13636 else
13637 pragma Assert (No (Node (Act_Elmt))
13638 or else (Primitive_Names_Match (Subp, Node (Act_Elmt))
13639 and then
13640 Type_Conformant (Subp, Node (Act_Elmt),
13641 Skip_Controlling_Formals => True)));
13643 Derive_Subprogram
13644 (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
13646 if Present (Act_Elmt) then
13647 Next_Elmt (Act_Elmt);
13648 end if;
13649 end if;
13651 Next_Elmt (Elmt);
13652 end loop;
13654 -- Case 2: Derived_Type implements interfaces
13656 else
13657 -- If the parent type has no predefined primitives we remove
13658 -- predefined primitives from the list of primitives of generic
13659 -- actual to simplify the complexity of this algorithm.
13661 if Present (Generic_Actual) then
13662 declare
13663 Has_Predefined_Primitives : Boolean := False;
13665 begin
13666 -- Check if the parent type has predefined primitives
13668 Elmt := First_Elmt (Op_List);
13669 while Present (Elmt) loop
13670 Subp := Node (Elmt);
13672 if Is_Predefined_Dispatching_Operation (Subp)
13673 and then not Comes_From_Source (Ultimate_Alias (Subp))
13674 then
13675 Has_Predefined_Primitives := True;
13676 exit;
13677 end if;
13679 Next_Elmt (Elmt);
13680 end loop;
13682 -- Remove predefined primitives of Generic_Actual. We must use
13683 -- an auxiliary list because in case of tagged types the value
13684 -- returned by Collect_Primitive_Operations is the value stored
13685 -- in its Primitive_Operations attribute (and we don't want to
13686 -- modify its current contents).
13688 if not Has_Predefined_Primitives then
13689 declare
13690 Aux_List : constant Elist_Id := New_Elmt_List;
13692 begin
13693 Elmt := First_Elmt (Act_List);
13694 while Present (Elmt) loop
13695 Subp := Node (Elmt);
13697 if not Is_Predefined_Dispatching_Operation (Subp)
13698 or else Comes_From_Source (Subp)
13699 then
13700 Append_Elmt (Subp, Aux_List);
13701 end if;
13703 Next_Elmt (Elmt);
13704 end loop;
13706 Act_List := Aux_List;
13707 end;
13708 end if;
13710 Act_Elmt := First_Elmt (Act_List);
13711 Act_Subp := Node (Act_Elmt);
13712 end;
13713 end if;
13715 -- Stage 1: If the generic actual is not present we derive the
13716 -- primitives inherited from the parent type. If the generic parent
13717 -- type is present, the derived type is an instance of a formal
13718 -- derived type, and within the instance its operations are those of
13719 -- the actual. We derive from the formal type but make the inherited
13720 -- operations aliases of the corresponding operations of the actual.
13722 Elmt := First_Elmt (Op_List);
13723 while Present (Elmt) loop
13724 Subp := Node (Elmt);
13725 Alias_Subp := Ultimate_Alias (Subp);
13727 -- Do not derive internal entities of the parent that link
13728 -- interface primitives with their covering primitive. These
13729 -- entities will be added to this type when frozen.
13731 if Present (Interface_Alias (Subp)) then
13732 goto Continue;
13733 end if;
13735 -- If the generic actual is present find the corresponding
13736 -- operation in the generic actual. If the parent type is a
13737 -- direct ancestor of the derived type then, even if it is an
13738 -- interface, the operations are inherited from the primary
13739 -- dispatch table and are in the proper order. If we detect here
13740 -- that primitives are not in the same order we traverse the list
13741 -- of primitive operations of the actual to find the one that
13742 -- implements the interface primitive.
13744 if Need_Search
13745 or else
13746 (Present (Generic_Actual)
13747 and then Present (Act_Subp)
13748 and then not
13749 (Primitive_Names_Match (Subp, Act_Subp)
13750 and then
13751 Type_Conformant (Subp, Act_Subp,
13752 Skip_Controlling_Formals => True)))
13753 then
13754 pragma Assert (not Is_Ancestor (Parent_Base, Generic_Actual,
13755 Use_Full_View => True));
13757 -- Remember that we need searching for all pending primitives
13759 Need_Search := True;
13761 -- Handle entities associated with interface primitives
13763 if Present (Alias_Subp)
13764 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
13765 and then not Is_Predefined_Dispatching_Operation (Subp)
13766 then
13767 -- Search for the primitive in the homonym chain
13769 Act_Subp :=
13770 Find_Primitive_Covering_Interface
13771 (Tagged_Type => Generic_Actual,
13772 Iface_Prim => Alias_Subp);
13774 -- Previous search may not locate primitives covering
13775 -- interfaces defined in generics units or instantiations.
13776 -- (it fails if the covering primitive has formals whose
13777 -- type is also defined in generics or instantiations).
13778 -- In such case we search in the list of primitives of the
13779 -- generic actual for the internal entity that links the
13780 -- interface primitive and the covering primitive.
13782 if No (Act_Subp)
13783 and then Is_Generic_Type (Parent_Type)
13784 then
13785 -- This code has been designed to handle only generic
13786 -- formals that implement interfaces that are defined
13787 -- in a generic unit or instantiation. If this code is
13788 -- needed for other cases we must review it because
13789 -- (given that it relies on Original_Location to locate
13790 -- the primitive of Generic_Actual that covers the
13791 -- interface) it could leave linked through attribute
13792 -- Alias entities of unrelated instantiations).
13794 pragma Assert
13795 (Is_Generic_Unit
13796 (Scope (Find_Dispatching_Type (Alias_Subp)))
13797 or else
13798 Instantiation_Depth
13799 (Sloc (Find_Dispatching_Type (Alias_Subp))) > 0);
13801 declare
13802 Iface_Prim_Loc : constant Source_Ptr :=
13803 Original_Location (Sloc (Alias_Subp));
13804 Elmt : Elmt_Id;
13805 Prim : Entity_Id;
13806 begin
13807 Elmt :=
13808 First_Elmt (Primitive_Operations (Generic_Actual));
13810 Search : while Present (Elmt) loop
13811 Prim := Node (Elmt);
13813 if Present (Interface_Alias (Prim))
13814 and then Original_Location
13815 (Sloc (Interface_Alias (Prim)))
13816 = Iface_Prim_Loc
13817 then
13818 Act_Subp := Alias (Prim);
13819 exit Search;
13820 end if;
13822 Next_Elmt (Elmt);
13823 end loop Search;
13824 end;
13825 end if;
13827 pragma Assert (Present (Act_Subp)
13828 or else Is_Abstract_Type (Generic_Actual)
13829 or else Serious_Errors_Detected > 0);
13831 -- Handle predefined primitives plus the rest of user-defined
13832 -- primitives
13834 else
13835 Act_Elmt := First_Elmt (Act_List);
13836 while Present (Act_Elmt) loop
13837 Act_Subp := Node (Act_Elmt);
13839 exit when Primitive_Names_Match (Subp, Act_Subp)
13840 and then Type_Conformant
13841 (Subp, Act_Subp,
13842 Skip_Controlling_Formals => True)
13843 and then No (Interface_Alias (Act_Subp));
13845 Next_Elmt (Act_Elmt);
13846 end loop;
13848 if No (Act_Elmt) then
13849 Act_Subp := Empty;
13850 end if;
13851 end if;
13852 end if;
13854 -- Case 1: If the parent is a limited interface then it has the
13855 -- predefined primitives of synchronized interfaces. However, the
13856 -- actual type may be a non-limited type and hence it does not
13857 -- have such primitives.
13859 if Present (Generic_Actual)
13860 and then not Present (Act_Subp)
13861 and then Is_Limited_Interface (Parent_Base)
13862 and then Is_Predefined_Interface_Primitive (Subp)
13863 then
13864 null;
13866 -- Case 2: Inherit entities associated with interfaces that were
13867 -- not covered by the parent type. We exclude here null interface
13868 -- primitives because they do not need special management.
13870 -- We also exclude interface operations that are renamings. If the
13871 -- subprogram is an explicit renaming of an interface primitive,
13872 -- it is a regular primitive operation, and the presence of its
13873 -- alias is not relevant: it has to be derived like any other
13874 -- primitive.
13876 elsif Present (Alias (Subp))
13877 and then Nkind (Unit_Declaration_Node (Subp)) /=
13878 N_Subprogram_Renaming_Declaration
13879 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
13880 and then not
13881 (Nkind (Parent (Alias_Subp)) = N_Procedure_Specification
13882 and then Null_Present (Parent (Alias_Subp)))
13883 then
13884 -- If this is an abstract private type then we transfer the
13885 -- derivation of the interface primitive from the partial view
13886 -- to the full view. This is safe because all the interfaces
13887 -- must be visible in the partial view. Done to avoid adding
13888 -- a new interface derivation to the private part of the
13889 -- enclosing package; otherwise this new derivation would be
13890 -- decorated as hidden when the analysis of the enclosing
13891 -- package completes.
13893 if Is_Abstract_Type (Derived_Type)
13894 and then In_Private_Part (Current_Scope)
13895 and then Has_Private_Declaration (Derived_Type)
13896 then
13897 declare
13898 Partial_View : Entity_Id;
13899 Elmt : Elmt_Id;
13900 Ent : Entity_Id;
13902 begin
13903 Partial_View := First_Entity (Current_Scope);
13904 loop
13905 exit when No (Partial_View)
13906 or else (Has_Private_Declaration (Partial_View)
13907 and then
13908 Full_View (Partial_View) = Derived_Type);
13910 Next_Entity (Partial_View);
13911 end loop;
13913 -- If the partial view was not found then the source code
13914 -- has errors and the derivation is not needed.
13916 if Present (Partial_View) then
13917 Elmt :=
13918 First_Elmt (Primitive_Operations (Partial_View));
13919 while Present (Elmt) loop
13920 Ent := Node (Elmt);
13922 if Present (Alias (Ent))
13923 and then Ultimate_Alias (Ent) = Alias (Subp)
13924 then
13925 Append_Elmt
13926 (Ent, Primitive_Operations (Derived_Type));
13927 exit;
13928 end if;
13930 Next_Elmt (Elmt);
13931 end loop;
13933 -- If the interface primitive was not found in the
13934 -- partial view then this interface primitive was
13935 -- overridden. We add a derivation to activate in
13936 -- Derive_Progenitor_Subprograms the machinery to
13937 -- search for it.
13939 if No (Elmt) then
13940 Derive_Interface_Subprogram
13941 (New_Subp => New_Subp,
13942 Subp => Subp,
13943 Actual_Subp => Act_Subp);
13944 end if;
13945 end if;
13946 end;
13947 else
13948 Derive_Interface_Subprogram
13949 (New_Subp => New_Subp,
13950 Subp => Subp,
13951 Actual_Subp => Act_Subp);
13952 end if;
13954 -- Case 3: Common derivation
13956 else
13957 Derive_Subprogram
13958 (New_Subp => New_Subp,
13959 Parent_Subp => Subp,
13960 Derived_Type => Derived_Type,
13961 Parent_Type => Parent_Base,
13962 Actual_Subp => Act_Subp);
13963 end if;
13965 -- No need to update Act_Elm if we must search for the
13966 -- corresponding operation in the generic actual
13968 if not Need_Search
13969 and then Present (Act_Elmt)
13970 then
13971 Next_Elmt (Act_Elmt);
13972 Act_Subp := Node (Act_Elmt);
13973 end if;
13975 <<Continue>>
13976 Next_Elmt (Elmt);
13977 end loop;
13979 -- Inherit additional operations from progenitors. If the derived
13980 -- type is a generic actual, there are not new primitive operations
13981 -- for the type because it has those of the actual, and therefore
13982 -- nothing needs to be done. The renamings generated above are not
13983 -- primitive operations, and their purpose is simply to make the
13984 -- proper operations visible within an instantiation.
13986 if No (Generic_Actual) then
13987 Derive_Progenitor_Subprograms (Parent_Base, Derived_Type);
13988 end if;
13989 end if;
13991 -- Final check: Direct descendants must have their primitives in the
13992 -- same order. We exclude from this test untagged types and instances
13993 -- of formal derived types. We skip this test if we have already
13994 -- reported serious errors in the sources.
13996 pragma Assert (not Is_Tagged_Type (Derived_Type)
13997 or else Present (Generic_Actual)
13998 or else Serious_Errors_Detected > 0
13999 or else Check_Derived_Type);
14000 end Derive_Subprograms;
14002 --------------------------------
14003 -- Derived_Standard_Character --
14004 --------------------------------
14006 procedure Derived_Standard_Character
14007 (N : Node_Id;
14008 Parent_Type : Entity_Id;
14009 Derived_Type : Entity_Id)
14011 Loc : constant Source_Ptr := Sloc (N);
14012 Def : constant Node_Id := Type_Definition (N);
14013 Indic : constant Node_Id := Subtype_Indication (Def);
14014 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
14015 Implicit_Base : constant Entity_Id :=
14016 Create_Itype
14017 (E_Enumeration_Type, N, Derived_Type, 'B');
14019 Lo : Node_Id;
14020 Hi : Node_Id;
14022 begin
14023 Discard_Node (Process_Subtype (Indic, N));
14025 Set_Etype (Implicit_Base, Parent_Base);
14026 Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
14027 Set_RM_Size (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
14029 Set_Is_Character_Type (Implicit_Base, True);
14030 Set_Has_Delayed_Freeze (Implicit_Base);
14032 -- The bounds of the implicit base are the bounds of the parent base.
14033 -- Note that their type is the parent base.
14035 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
14036 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
14038 Set_Scalar_Range (Implicit_Base,
14039 Make_Range (Loc,
14040 Low_Bound => Lo,
14041 High_Bound => Hi));
14043 Conditional_Delay (Derived_Type, Parent_Type);
14045 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
14046 Set_Etype (Derived_Type, Implicit_Base);
14047 Set_Size_Info (Derived_Type, Parent_Type);
14049 if Unknown_RM_Size (Derived_Type) then
14050 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
14051 end if;
14053 Set_Is_Character_Type (Derived_Type, True);
14055 if Nkind (Indic) /= N_Subtype_Indication then
14057 -- If no explicit constraint, the bounds are those
14058 -- of the parent type.
14060 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Type));
14061 Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
14062 Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
14063 end if;
14065 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
14067 -- Because the implicit base is used in the conversion of the bounds, we
14068 -- have to freeze it now. This is similar to what is done for numeric
14069 -- types, and it equally suspicious, but otherwise a non-static bound
14070 -- will have a reference to an unfrozen type, which is rejected by Gigi
14071 -- (???). This requires specific care for definition of stream
14072 -- attributes. For details, see comments at the end of
14073 -- Build_Derived_Numeric_Type.
14075 Freeze_Before (N, Implicit_Base);
14076 end Derived_Standard_Character;
14078 ------------------------------
14079 -- Derived_Type_Declaration --
14080 ------------------------------
14082 procedure Derived_Type_Declaration
14083 (T : Entity_Id;
14084 N : Node_Id;
14085 Is_Completion : Boolean)
14087 Parent_Type : Entity_Id;
14089 function Comes_From_Generic (Typ : Entity_Id) return Boolean;
14090 -- Check whether the parent type is a generic formal, or derives
14091 -- directly or indirectly from one.
14093 ------------------------
14094 -- Comes_From_Generic --
14095 ------------------------
14097 function Comes_From_Generic (Typ : Entity_Id) return Boolean is
14098 begin
14099 if Is_Generic_Type (Typ) then
14100 return True;
14102 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
14103 return True;
14105 elsif Is_Private_Type (Typ)
14106 and then Present (Full_View (Typ))
14107 and then Is_Generic_Type (Root_Type (Full_View (Typ)))
14108 then
14109 return True;
14111 elsif Is_Generic_Actual_Type (Typ) then
14112 return True;
14114 else
14115 return False;
14116 end if;
14117 end Comes_From_Generic;
14119 -- Local variables
14121 Def : constant Node_Id := Type_Definition (N);
14122 Iface_Def : Node_Id;
14123 Indic : constant Node_Id := Subtype_Indication (Def);
14124 Extension : constant Node_Id := Record_Extension_Part (Def);
14125 Parent_Node : Node_Id;
14126 Taggd : Boolean;
14128 -- Start of processing for Derived_Type_Declaration
14130 begin
14131 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
14133 -- Ada 2005 (AI-251): In case of interface derivation check that the
14134 -- parent is also an interface.
14136 if Interface_Present (Def) then
14137 Check_SPARK_Restriction ("interface is not allowed", Def);
14139 if not Is_Interface (Parent_Type) then
14140 Diagnose_Interface (Indic, Parent_Type);
14142 else
14143 Parent_Node := Parent (Base_Type (Parent_Type));
14144 Iface_Def := Type_Definition (Parent_Node);
14146 -- Ada 2005 (AI-251): Limited interfaces can only inherit from
14147 -- other limited interfaces.
14149 if Limited_Present (Def) then
14150 if Limited_Present (Iface_Def) then
14151 null;
14153 elsif Protected_Present (Iface_Def) then
14154 Error_Msg_NE
14155 ("descendant of& must be declared"
14156 & " as a protected interface",
14157 N, Parent_Type);
14159 elsif Synchronized_Present (Iface_Def) then
14160 Error_Msg_NE
14161 ("descendant of& must be declared"
14162 & " as a synchronized interface",
14163 N, Parent_Type);
14165 elsif Task_Present (Iface_Def) then
14166 Error_Msg_NE
14167 ("descendant of& must be declared as a task interface",
14168 N, Parent_Type);
14170 else
14171 Error_Msg_N
14172 ("(Ada 2005) limited interface cannot "
14173 & "inherit from non-limited interface", Indic);
14174 end if;
14176 -- Ada 2005 (AI-345): Non-limited interfaces can only inherit
14177 -- from non-limited or limited interfaces.
14179 elsif not Protected_Present (Def)
14180 and then not Synchronized_Present (Def)
14181 and then not Task_Present (Def)
14182 then
14183 if Limited_Present (Iface_Def) then
14184 null;
14186 elsif Protected_Present (Iface_Def) then
14187 Error_Msg_NE
14188 ("descendant of& must be declared"
14189 & " as a protected interface",
14190 N, Parent_Type);
14192 elsif Synchronized_Present (Iface_Def) then
14193 Error_Msg_NE
14194 ("descendant of& must be declared"
14195 & " as a synchronized interface",
14196 N, Parent_Type);
14198 elsif Task_Present (Iface_Def) then
14199 Error_Msg_NE
14200 ("descendant of& must be declared as a task interface",
14201 N, Parent_Type);
14202 else
14203 null;
14204 end if;
14205 end if;
14206 end if;
14207 end if;
14209 if Is_Tagged_Type (Parent_Type)
14210 and then Is_Concurrent_Type (Parent_Type)
14211 and then not Is_Interface (Parent_Type)
14212 then
14213 Error_Msg_N
14214 ("parent type of a record extension cannot be "
14215 & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
14216 Set_Etype (T, Any_Type);
14217 return;
14218 end if;
14220 -- Ada 2005 (AI-251): Decorate all the names in the list of ancestor
14221 -- interfaces
14223 if Is_Tagged_Type (Parent_Type)
14224 and then Is_Non_Empty_List (Interface_List (Def))
14225 then
14226 declare
14227 Intf : Node_Id;
14228 T : Entity_Id;
14230 begin
14231 Intf := First (Interface_List (Def));
14232 while Present (Intf) loop
14233 T := Find_Type_Of_Subtype_Indic (Intf);
14235 if not Is_Interface (T) then
14236 Diagnose_Interface (Intf, T);
14238 -- Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
14239 -- a limited type from having a nonlimited progenitor.
14241 elsif (Limited_Present (Def)
14242 or else (not Is_Interface (Parent_Type)
14243 and then Is_Limited_Type (Parent_Type)))
14244 and then not Is_Limited_Interface (T)
14245 then
14246 Error_Msg_NE
14247 ("progenitor interface& of limited type must be limited",
14248 N, T);
14249 end if;
14251 Next (Intf);
14252 end loop;
14253 end;
14254 end if;
14256 if Parent_Type = Any_Type
14257 or else Etype (Parent_Type) = Any_Type
14258 or else (Is_Class_Wide_Type (Parent_Type)
14259 and then Etype (Parent_Type) = T)
14260 then
14261 -- If Parent_Type is undefined or illegal, make new type into a
14262 -- subtype of Any_Type, and set a few attributes to prevent cascaded
14263 -- errors. If this is a self-definition, emit error now.
14265 if T = Parent_Type
14266 or else T = Etype (Parent_Type)
14267 then
14268 Error_Msg_N ("type cannot be used in its own definition", Indic);
14269 end if;
14271 Set_Ekind (T, Ekind (Parent_Type));
14272 Set_Etype (T, Any_Type);
14273 Set_Scalar_Range (T, Scalar_Range (Any_Type));
14275 if Is_Tagged_Type (T)
14276 and then Is_Record_Type (T)
14277 then
14278 Set_Direct_Primitive_Operations (T, New_Elmt_List);
14279 end if;
14281 return;
14282 end if;
14284 -- Ada 2005 (AI-251): The case in which the parent of the full-view is
14285 -- an interface is special because the list of interfaces in the full
14286 -- view can be given in any order. For example:
14288 -- type A is interface;
14289 -- type B is interface and A;
14290 -- type D is new B with private;
14291 -- private
14292 -- type D is new A and B with null record; -- 1 --
14294 -- In this case we perform the following transformation of -1-:
14296 -- type D is new B and A with null record;
14298 -- If the parent of the full-view covers the parent of the partial-view
14299 -- we have two possible cases:
14301 -- 1) They have the same parent
14302 -- 2) The parent of the full-view implements some further interfaces
14304 -- In both cases we do not need to perform the transformation. In the
14305 -- first case the source program is correct and the transformation is
14306 -- not needed; in the second case the source program does not fulfill
14307 -- the no-hidden interfaces rule (AI-396) and the error will be reported
14308 -- later.
14310 -- This transformation not only simplifies the rest of the analysis of
14311 -- this type declaration but also simplifies the correct generation of
14312 -- the object layout to the expander.
14314 if In_Private_Part (Current_Scope)
14315 and then Is_Interface (Parent_Type)
14316 then
14317 declare
14318 Iface : Node_Id;
14319 Partial_View : Entity_Id;
14320 Partial_View_Parent : Entity_Id;
14321 New_Iface : Node_Id;
14323 begin
14324 -- Look for the associated private type declaration
14326 Partial_View := First_Entity (Current_Scope);
14327 loop
14328 exit when No (Partial_View)
14329 or else (Has_Private_Declaration (Partial_View)
14330 and then Full_View (Partial_View) = T);
14332 Next_Entity (Partial_View);
14333 end loop;
14335 -- If the partial view was not found then the source code has
14336 -- errors and the transformation is not needed.
14338 if Present (Partial_View) then
14339 Partial_View_Parent := Etype (Partial_View);
14341 -- If the parent of the full-view covers the parent of the
14342 -- partial-view we have nothing else to do.
14344 if Interface_Present_In_Ancestor
14345 (Parent_Type, Partial_View_Parent)
14346 then
14347 null;
14349 -- Traverse the list of interfaces of the full-view to look
14350 -- for the parent of the partial-view and perform the tree
14351 -- transformation.
14353 else
14354 Iface := First (Interface_List (Def));
14355 while Present (Iface) loop
14356 if Etype (Iface) = Etype (Partial_View) then
14357 Rewrite (Subtype_Indication (Def),
14358 New_Copy (Subtype_Indication
14359 (Parent (Partial_View))));
14361 New_Iface :=
14362 Make_Identifier (Sloc (N), Chars (Parent_Type));
14363 Append (New_Iface, Interface_List (Def));
14365 -- Analyze the transformed code
14367 Derived_Type_Declaration (T, N, Is_Completion);
14368 return;
14369 end if;
14371 Next (Iface);
14372 end loop;
14373 end if;
14374 end if;
14375 end;
14376 end if;
14378 -- Only composite types other than array types are allowed to have
14379 -- discriminants. In SPARK, no types are allowed to have discriminants.
14381 if Present (Discriminant_Specifications (N)) then
14382 if (Is_Elementary_Type (Parent_Type)
14383 or else Is_Array_Type (Parent_Type))
14384 and then not Error_Posted (N)
14385 then
14386 Error_Msg_N
14387 ("elementary or array type cannot have discriminants",
14388 Defining_Identifier (First (Discriminant_Specifications (N))));
14389 Set_Has_Discriminants (T, False);
14390 else
14391 Check_SPARK_Restriction ("discriminant type is not allowed", N);
14392 end if;
14393 end if;
14395 -- In Ada 83, a derived type defined in a package specification cannot
14396 -- be used for further derivation until the end of its visible part.
14397 -- Note that derivation in the private part of the package is allowed.
14399 if Ada_Version = Ada_83
14400 and then Is_Derived_Type (Parent_Type)
14401 and then In_Visible_Part (Scope (Parent_Type))
14402 then
14403 if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
14404 Error_Msg_N
14405 ("(Ada 83): premature use of type for derivation", Indic);
14406 end if;
14407 end if;
14409 -- Check for early use of incomplete or private type
14411 if Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
14412 Error_Msg_N ("premature derivation of incomplete type", Indic);
14413 return;
14415 elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
14416 and then not Comes_From_Generic (Parent_Type))
14417 or else Has_Private_Component (Parent_Type)
14418 then
14419 -- The ancestor type of a formal type can be incomplete, in which
14420 -- case only the operations of the partial view are available in the
14421 -- generic. Subsequent checks may be required when the full view is
14422 -- analyzed to verify that a derivation from a tagged type has an
14423 -- extension.
14425 if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
14426 null;
14428 elsif No (Underlying_Type (Parent_Type))
14429 or else Has_Private_Component (Parent_Type)
14430 then
14431 Error_Msg_N
14432 ("premature derivation of derived or private type", Indic);
14434 -- Flag the type itself as being in error, this prevents some
14435 -- nasty problems with subsequent uses of the malformed type.
14437 Set_Error_Posted (T);
14439 -- Check that within the immediate scope of an untagged partial
14440 -- view it's illegal to derive from the partial view if the
14441 -- full view is tagged. (7.3(7))
14443 -- We verify that the Parent_Type is a partial view by checking
14444 -- that it is not a Full_Type_Declaration (i.e. a private type or
14445 -- private extension declaration), to distinguish a partial view
14446 -- from a derivation from a private type which also appears as
14447 -- E_Private_Type. If the parent base type is not declared in an
14448 -- enclosing scope there is no need to check.
14450 elsif Present (Full_View (Parent_Type))
14451 and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
14452 and then not Is_Tagged_Type (Parent_Type)
14453 and then Is_Tagged_Type (Full_View (Parent_Type))
14454 and then In_Open_Scopes (Scope (Base_Type (Parent_Type)))
14455 then
14456 Error_Msg_N
14457 ("premature derivation from type with tagged full view",
14458 Indic);
14459 end if;
14460 end if;
14462 -- Check that form of derivation is appropriate
14464 Taggd := Is_Tagged_Type (Parent_Type);
14466 -- Perhaps the parent type should be changed to the class-wide type's
14467 -- specific type in this case to prevent cascading errors ???
14469 if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
14470 Error_Msg_N ("parent type must not be a class-wide type", Indic);
14471 return;
14472 end if;
14474 if Present (Extension) and then not Taggd then
14475 Error_Msg_N
14476 ("type derived from untagged type cannot have extension", Indic);
14478 elsif No (Extension) and then Taggd then
14480 -- If this declaration is within a private part (or body) of a
14481 -- generic instantiation then the derivation is allowed (the parent
14482 -- type can only appear tagged in this case if it's a generic actual
14483 -- type, since it would otherwise have been rejected in the analysis
14484 -- of the generic template).
14486 if not Is_Generic_Actual_Type (Parent_Type)
14487 or else In_Visible_Part (Scope (Parent_Type))
14488 then
14489 if Is_Class_Wide_Type (Parent_Type) then
14490 Error_Msg_N
14491 ("parent type must not be a class-wide type", Indic);
14493 -- Use specific type to prevent cascaded errors.
14495 Parent_Type := Etype (Parent_Type);
14497 else
14498 Error_Msg_N
14499 ("type derived from tagged type must have extension", Indic);
14500 end if;
14501 end if;
14502 end if;
14504 -- AI-443: Synchronized formal derived types require a private
14505 -- extension. There is no point in checking the ancestor type or
14506 -- the progenitors since the construct is wrong to begin with.
14508 if Ada_Version >= Ada_2005
14509 and then Is_Generic_Type (T)
14510 and then Present (Original_Node (N))
14511 then
14512 declare
14513 Decl : constant Node_Id := Original_Node (N);
14515 begin
14516 if Nkind (Decl) = N_Formal_Type_Declaration
14517 and then Nkind (Formal_Type_Definition (Decl)) =
14518 N_Formal_Derived_Type_Definition
14519 and then Synchronized_Present (Formal_Type_Definition (Decl))
14520 and then No (Extension)
14522 -- Avoid emitting a duplicate error message
14524 and then not Error_Posted (Indic)
14525 then
14526 Error_Msg_N
14527 ("synchronized derived type must have extension", N);
14528 end if;
14529 end;
14530 end if;
14532 if Null_Exclusion_Present (Def)
14533 and then not Is_Access_Type (Parent_Type)
14534 then
14535 Error_Msg_N ("null exclusion can only apply to an access type", N);
14536 end if;
14538 -- Avoid deriving parent primitives of underlying record views
14540 Build_Derived_Type (N, Parent_Type, T, Is_Completion,
14541 Derive_Subps => not Is_Underlying_Record_View (T));
14543 -- AI-419: The parent type of an explicitly limited derived type must
14544 -- be a limited type or a limited interface.
14546 if Limited_Present (Def) then
14547 Set_Is_Limited_Record (T);
14549 if Is_Interface (T) then
14550 Set_Is_Limited_Interface (T);
14551 end if;
14553 if not Is_Limited_Type (Parent_Type)
14554 and then
14555 (not Is_Interface (Parent_Type)
14556 or else not Is_Limited_Interface (Parent_Type))
14557 then
14558 -- AI05-0096: a derivation in the private part of an instance is
14559 -- legal if the generic formal is untagged limited, and the actual
14560 -- is non-limited.
14562 if Is_Generic_Actual_Type (Parent_Type)
14563 and then In_Private_Part (Current_Scope)
14564 and then
14565 not Is_Tagged_Type
14566 (Generic_Parent_Type (Parent (Parent_Type)))
14567 then
14568 null;
14570 else
14571 Error_Msg_NE
14572 ("parent type& of limited type must be limited",
14573 N, Parent_Type);
14574 end if;
14575 end if;
14576 end if;
14578 -- In SPARK, there are no derived type definitions other than type
14579 -- extensions of tagged record types.
14581 if No (Extension) then
14582 Check_SPARK_Restriction ("derived type is not allowed", N);
14583 end if;
14584 end Derived_Type_Declaration;
14586 ------------------------
14587 -- Diagnose_Interface --
14588 ------------------------
14590 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id) is
14591 begin
14592 if not Is_Interface (E)
14593 and then E /= Any_Type
14594 then
14595 Error_Msg_NE ("(Ada 2005) & must be an interface", N, E);
14596 end if;
14597 end Diagnose_Interface;
14599 ----------------------------------
14600 -- Enumeration_Type_Declaration --
14601 ----------------------------------
14603 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
14604 Ev : Uint;
14605 L : Node_Id;
14606 R_Node : Node_Id;
14607 B_Node : Node_Id;
14609 begin
14610 -- Create identifier node representing lower bound
14612 B_Node := New_Node (N_Identifier, Sloc (Def));
14613 L := First (Literals (Def));
14614 Set_Chars (B_Node, Chars (L));
14615 Set_Entity (B_Node, L);
14616 Set_Etype (B_Node, T);
14617 Set_Is_Static_Expression (B_Node, True);
14619 R_Node := New_Node (N_Range, Sloc (Def));
14620 Set_Low_Bound (R_Node, B_Node);
14622 Set_Ekind (T, E_Enumeration_Type);
14623 Set_First_Literal (T, L);
14624 Set_Etype (T, T);
14625 Set_Is_Constrained (T);
14627 Ev := Uint_0;
14629 -- Loop through literals of enumeration type setting pos and rep values
14630 -- except that if the Ekind is already set, then it means the literal
14631 -- was already constructed (case of a derived type declaration and we
14632 -- should not disturb the Pos and Rep values.
14634 while Present (L) loop
14635 if Ekind (L) /= E_Enumeration_Literal then
14636 Set_Ekind (L, E_Enumeration_Literal);
14637 Set_Enumeration_Pos (L, Ev);
14638 Set_Enumeration_Rep (L, Ev);
14639 Set_Is_Known_Valid (L, True);
14640 end if;
14642 Set_Etype (L, T);
14643 New_Overloaded_Entity (L);
14644 Generate_Definition (L);
14645 Set_Convention (L, Convention_Intrinsic);
14647 -- Case of character literal
14649 if Nkind (L) = N_Defining_Character_Literal then
14650 Set_Is_Character_Type (T, True);
14652 -- Check violation of No_Wide_Characters
14654 if Restriction_Check_Required (No_Wide_Characters) then
14655 Get_Name_String (Chars (L));
14657 if Name_Len >= 3 and then Name_Buffer (1 .. 2) = "QW" then
14658 Check_Restriction (No_Wide_Characters, L);
14659 end if;
14660 end if;
14661 end if;
14663 Ev := Ev + 1;
14664 Next (L);
14665 end loop;
14667 -- Now create a node representing upper bound
14669 B_Node := New_Node (N_Identifier, Sloc (Def));
14670 Set_Chars (B_Node, Chars (Last (Literals (Def))));
14671 Set_Entity (B_Node, Last (Literals (Def)));
14672 Set_Etype (B_Node, T);
14673 Set_Is_Static_Expression (B_Node, True);
14675 Set_High_Bound (R_Node, B_Node);
14677 -- Initialize various fields of the type. Some of this information
14678 -- may be overwritten later through rep.clauses.
14680 Set_Scalar_Range (T, R_Node);
14681 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
14682 Set_Enum_Esize (T);
14683 Set_Enum_Pos_To_Rep (T, Empty);
14685 -- Set Discard_Names if configuration pragma set, or if there is
14686 -- a parameterless pragma in the current declarative region
14688 if Global_Discard_Names
14689 or else Discard_Names (Scope (T))
14690 then
14691 Set_Discard_Names (T);
14692 end if;
14694 -- Process end label if there is one
14696 if Present (Def) then
14697 Process_End_Label (Def, 'e', T);
14698 end if;
14699 end Enumeration_Type_Declaration;
14701 ---------------------------------
14702 -- Expand_To_Stored_Constraint --
14703 ---------------------------------
14705 function Expand_To_Stored_Constraint
14706 (Typ : Entity_Id;
14707 Constraint : Elist_Id) return Elist_Id
14709 Explicitly_Discriminated_Type : Entity_Id;
14710 Expansion : Elist_Id;
14711 Discriminant : Entity_Id;
14713 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
14714 -- Find the nearest type that actually specifies discriminants
14716 ---------------------------------
14717 -- Type_With_Explicit_Discrims --
14718 ---------------------------------
14720 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
14721 Typ : constant E := Base_Type (Id);
14723 begin
14724 if Ekind (Typ) in Incomplete_Or_Private_Kind then
14725 if Present (Full_View (Typ)) then
14726 return Type_With_Explicit_Discrims (Full_View (Typ));
14727 end if;
14729 else
14730 if Has_Discriminants (Typ) then
14731 return Typ;
14732 end if;
14733 end if;
14735 if Etype (Typ) = Typ then
14736 return Empty;
14737 elsif Has_Discriminants (Typ) then
14738 return Typ;
14739 else
14740 return Type_With_Explicit_Discrims (Etype (Typ));
14741 end if;
14743 end Type_With_Explicit_Discrims;
14745 -- Start of processing for Expand_To_Stored_Constraint
14747 begin
14748 if No (Constraint)
14749 or else Is_Empty_Elmt_List (Constraint)
14750 then
14751 return No_Elist;
14752 end if;
14754 Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
14756 if No (Explicitly_Discriminated_Type) then
14757 return No_Elist;
14758 end if;
14760 Expansion := New_Elmt_List;
14762 Discriminant :=
14763 First_Stored_Discriminant (Explicitly_Discriminated_Type);
14764 while Present (Discriminant) loop
14765 Append_Elmt (
14766 Get_Discriminant_Value (
14767 Discriminant, Explicitly_Discriminated_Type, Constraint),
14768 Expansion);
14769 Next_Stored_Discriminant (Discriminant);
14770 end loop;
14772 return Expansion;
14773 end Expand_To_Stored_Constraint;
14775 ---------------------------
14776 -- Find_Hidden_Interface --
14777 ---------------------------
14779 function Find_Hidden_Interface
14780 (Src : Elist_Id;
14781 Dest : Elist_Id) return Entity_Id
14783 Iface : Entity_Id;
14784 Iface_Elmt : Elmt_Id;
14786 begin
14787 if Present (Src) and then Present (Dest) then
14788 Iface_Elmt := First_Elmt (Src);
14789 while Present (Iface_Elmt) loop
14790 Iface := Node (Iface_Elmt);
14792 if Is_Interface (Iface)
14793 and then not Contain_Interface (Iface, Dest)
14794 then
14795 return Iface;
14796 end if;
14798 Next_Elmt (Iface_Elmt);
14799 end loop;
14800 end if;
14802 return Empty;
14803 end Find_Hidden_Interface;
14805 --------------------
14806 -- Find_Type_Name --
14807 --------------------
14809 function Find_Type_Name (N : Node_Id) return Entity_Id is
14810 Id : constant Entity_Id := Defining_Identifier (N);
14811 Prev : Entity_Id;
14812 New_Id : Entity_Id;
14813 Prev_Par : Node_Id;
14815 procedure Tag_Mismatch;
14816 -- Diagnose a tagged partial view whose full view is untagged.
14817 -- We post the message on the full view, with a reference to
14818 -- the previous partial view. The partial view can be private
14819 -- or incomplete, and these are handled in a different manner,
14820 -- so we determine the position of the error message from the
14821 -- respective slocs of both.
14823 ------------------
14824 -- Tag_Mismatch --
14825 ------------------
14827 procedure Tag_Mismatch is
14828 begin
14829 if Sloc (Prev) < Sloc (Id) then
14830 if Ada_Version >= Ada_2012
14831 and then Nkind (N) = N_Private_Type_Declaration
14832 then
14833 Error_Msg_NE
14834 ("declaration of private } must be a tagged type ", Id, Prev);
14835 else
14836 Error_Msg_NE
14837 ("full declaration of } must be a tagged type ", Id, Prev);
14838 end if;
14839 else
14840 if Ada_Version >= Ada_2012
14841 and then Nkind (N) = N_Private_Type_Declaration
14842 then
14843 Error_Msg_NE
14844 ("declaration of private } must be a tagged type ", Prev, Id);
14845 else
14846 Error_Msg_NE
14847 ("full declaration of } must be a tagged type ", Prev, Id);
14848 end if;
14849 end if;
14850 end Tag_Mismatch;
14852 -- Start of processing for Find_Type_Name
14854 begin
14855 -- Find incomplete declaration, if one was given
14857 Prev := Current_Entity_In_Scope (Id);
14859 -- New type declaration
14861 if No (Prev) then
14862 Enter_Name (Id);
14863 return Id;
14865 -- Previous declaration exists
14867 else
14868 Prev_Par := Parent (Prev);
14870 -- Error if not incomplete/private case except if previous
14871 -- declaration is implicit, etc. Enter_Name will emit error if
14872 -- appropriate.
14874 if not Is_Incomplete_Or_Private_Type (Prev) then
14875 Enter_Name (Id);
14876 New_Id := Id;
14878 -- Check invalid completion of private or incomplete type
14880 elsif not Nkind_In (N, N_Full_Type_Declaration,
14881 N_Task_Type_Declaration,
14882 N_Protected_Type_Declaration)
14883 and then
14884 (Ada_Version < Ada_2012
14885 or else not Is_Incomplete_Type (Prev)
14886 or else not Nkind_In (N, N_Private_Type_Declaration,
14887 N_Private_Extension_Declaration))
14888 then
14889 -- Completion must be a full type declarations (RM 7.3(4))
14891 Error_Msg_Sloc := Sloc (Prev);
14892 Error_Msg_NE ("invalid completion of }", Id, Prev);
14894 -- Set scope of Id to avoid cascaded errors. Entity is never
14895 -- examined again, except when saving globals in generics.
14897 Set_Scope (Id, Current_Scope);
14898 New_Id := Id;
14900 -- If this is a repeated incomplete declaration, no further
14901 -- checks are possible.
14903 if Nkind (N) = N_Incomplete_Type_Declaration then
14904 return Prev;
14905 end if;
14907 -- Case of full declaration of incomplete type
14909 elsif Ekind (Prev) = E_Incomplete_Type
14910 and then (Ada_Version < Ada_2012
14911 or else No (Full_View (Prev))
14912 or else not Is_Private_Type (Full_View (Prev)))
14913 then
14915 -- Indicate that the incomplete declaration has a matching full
14916 -- declaration. The defining occurrence of the incomplete
14917 -- declaration remains the visible one, and the procedure
14918 -- Get_Full_View dereferences it whenever the type is used.
14920 if Present (Full_View (Prev)) then
14921 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
14922 end if;
14924 Set_Full_View (Prev, Id);
14925 Append_Entity (Id, Current_Scope);
14926 Set_Is_Public (Id, Is_Public (Prev));
14927 Set_Is_Internal (Id);
14928 New_Id := Prev;
14930 -- If the incomplete view is tagged, a class_wide type has been
14931 -- created already. Use it for the private type as well, in order
14932 -- to prevent multiple incompatible class-wide types that may be
14933 -- created for self-referential anonymous access components.
14935 if Is_Tagged_Type (Prev)
14936 and then Present (Class_Wide_Type (Prev))
14937 then
14938 Set_Ekind (Id, Ekind (Prev)); -- will be reset later
14939 Set_Class_Wide_Type (Id, Class_Wide_Type (Prev));
14941 -- If the incomplete type is completed by a private declaration
14942 -- the class-wide type remains associated with the incomplete
14943 -- type, to prevent order-of-elaboration issues in gigi, else
14944 -- we associate the class-wide type with the known full view.
14946 if Nkind (N) /= N_Private_Type_Declaration then
14947 Set_Etype (Class_Wide_Type (Id), Id);
14948 end if;
14949 end if;
14951 -- Case of full declaration of private type
14953 else
14954 -- If the private type was a completion of an incomplete type then
14955 -- update Prev to reference the private type
14957 if Ada_Version >= Ada_2012
14958 and then Ekind (Prev) = E_Incomplete_Type
14959 and then Present (Full_View (Prev))
14960 and then Is_Private_Type (Full_View (Prev))
14961 then
14962 Prev := Full_View (Prev);
14963 Prev_Par := Parent (Prev);
14964 end if;
14966 if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
14967 if Etype (Prev) /= Prev then
14969 -- Prev is a private subtype or a derived type, and needs
14970 -- no completion.
14972 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
14973 New_Id := Id;
14975 elsif Ekind (Prev) = E_Private_Type
14976 and then Nkind_In (N, N_Task_Type_Declaration,
14977 N_Protected_Type_Declaration)
14978 then
14979 Error_Msg_N
14980 ("completion of nonlimited type cannot be limited", N);
14982 elsif Ekind (Prev) = E_Record_Type_With_Private
14983 and then Nkind_In (N, N_Task_Type_Declaration,
14984 N_Protected_Type_Declaration)
14985 then
14986 if not Is_Limited_Record (Prev) then
14987 Error_Msg_N
14988 ("completion of nonlimited type cannot be limited", N);
14990 elsif No (Interface_List (N)) then
14991 Error_Msg_N
14992 ("completion of tagged private type must be tagged",
14994 end if;
14996 elsif Nkind (N) = N_Full_Type_Declaration
14997 and then
14998 Nkind (Type_Definition (N)) = N_Record_Definition
14999 and then Interface_Present (Type_Definition (N))
15000 then
15001 Error_Msg_N
15002 ("completion of private type cannot be an interface", N);
15003 end if;
15005 -- Ada 2005 (AI-251): Private extension declaration of a task
15006 -- type or a protected type. This case arises when covering
15007 -- interface types.
15009 elsif Nkind_In (N, N_Task_Type_Declaration,
15010 N_Protected_Type_Declaration)
15011 then
15012 null;
15014 elsif Nkind (N) /= N_Full_Type_Declaration
15015 or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
15016 then
15017 Error_Msg_N
15018 ("full view of private extension must be an extension", N);
15020 elsif not (Abstract_Present (Parent (Prev)))
15021 and then Abstract_Present (Type_Definition (N))
15022 then
15023 Error_Msg_N
15024 ("full view of non-abstract extension cannot be abstract", N);
15025 end if;
15027 if not In_Private_Part (Current_Scope) then
15028 Error_Msg_N
15029 ("declaration of full view must appear in private part", N);
15030 end if;
15032 Copy_And_Swap (Prev, Id);
15033 Set_Has_Private_Declaration (Prev);
15034 Set_Has_Private_Declaration (Id);
15036 -- Preserve aspect and iterator flags that may have been set on
15037 -- the partial view.
15039 Set_Has_Delayed_Aspects (Prev, Has_Delayed_Aspects (Id));
15040 Set_Has_Implicit_Dereference (Prev, Has_Implicit_Dereference (Id));
15042 -- If no error, propagate freeze_node from private to full view.
15043 -- It may have been generated for an early operational item.
15045 if Present (Freeze_Node (Id))
15046 and then Serious_Errors_Detected = 0
15047 and then No (Full_View (Id))
15048 then
15049 Set_Freeze_Node (Prev, Freeze_Node (Id));
15050 Set_Freeze_Node (Id, Empty);
15051 Set_First_Rep_Item (Prev, First_Rep_Item (Id));
15052 end if;
15054 Set_Full_View (Id, Prev);
15055 New_Id := Prev;
15056 end if;
15058 -- Verify that full declaration conforms to partial one
15060 if Is_Incomplete_Or_Private_Type (Prev)
15061 and then Present (Discriminant_Specifications (Prev_Par))
15062 then
15063 if Present (Discriminant_Specifications (N)) then
15064 if Ekind (Prev) = E_Incomplete_Type then
15065 Check_Discriminant_Conformance (N, Prev, Prev);
15066 else
15067 Check_Discriminant_Conformance (N, Prev, Id);
15068 end if;
15070 else
15071 Error_Msg_N
15072 ("missing discriminants in full type declaration", N);
15074 -- To avoid cascaded errors on subsequent use, share the
15075 -- discriminants of the partial view.
15077 Set_Discriminant_Specifications (N,
15078 Discriminant_Specifications (Prev_Par));
15079 end if;
15080 end if;
15082 -- A prior untagged partial view can have an associated class-wide
15083 -- type due to use of the class attribute, and in this case the full
15084 -- type must also be tagged. This Ada 95 usage is deprecated in favor
15085 -- of incomplete tagged declarations, but we check for it.
15087 if Is_Type (Prev)
15088 and then (Is_Tagged_Type (Prev)
15089 or else Present (Class_Wide_Type (Prev)))
15090 then
15091 -- Ada 2012 (AI05-0162): A private type may be the completion of
15092 -- an incomplete type
15094 if Ada_Version >= Ada_2012
15095 and then Is_Incomplete_Type (Prev)
15096 and then Nkind_In (N, N_Private_Type_Declaration,
15097 N_Private_Extension_Declaration)
15098 then
15099 -- No need to check private extensions since they are tagged
15101 if Nkind (N) = N_Private_Type_Declaration
15102 and then not Tagged_Present (N)
15103 then
15104 Tag_Mismatch;
15105 end if;
15107 -- The full declaration is either a tagged type (including
15108 -- a synchronized type that implements interfaces) or a
15109 -- type extension, otherwise this is an error.
15111 elsif Nkind_In (N, N_Task_Type_Declaration,
15112 N_Protected_Type_Declaration)
15113 then
15114 if No (Interface_List (N))
15115 and then not Error_Posted (N)
15116 then
15117 Tag_Mismatch;
15118 end if;
15120 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
15122 -- Indicate that the previous declaration (tagged incomplete
15123 -- or private declaration) requires the same on the full one.
15125 if not Tagged_Present (Type_Definition (N)) then
15126 Tag_Mismatch;
15127 Set_Is_Tagged_Type (Id);
15128 end if;
15130 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
15131 if No (Record_Extension_Part (Type_Definition (N))) then
15132 Error_Msg_NE
15133 ("full declaration of } must be a record extension",
15134 Prev, Id);
15136 -- Set some attributes to produce a usable full view
15138 Set_Is_Tagged_Type (Id);
15139 end if;
15141 else
15142 Tag_Mismatch;
15143 end if;
15144 end if;
15146 if Present (Prev)
15147 and then Nkind (Parent (Prev)) = N_Incomplete_Type_Declaration
15148 and then Present (Premature_Use (Parent (Prev)))
15149 then
15150 Error_Msg_Sloc := Sloc (N);
15151 Error_Msg_N
15152 ("\full declaration #", Premature_Use (Parent (Prev)));
15153 end if;
15155 return New_Id;
15156 end if;
15157 end Find_Type_Name;
15159 -------------------------
15160 -- Find_Type_Of_Object --
15161 -------------------------
15163 function Find_Type_Of_Object
15164 (Obj_Def : Node_Id;
15165 Related_Nod : Node_Id) return Entity_Id
15167 Def_Kind : constant Node_Kind := Nkind (Obj_Def);
15168 P : Node_Id := Parent (Obj_Def);
15169 T : Entity_Id;
15170 Nam : Name_Id;
15172 begin
15173 -- If the parent is a component_definition node we climb to the
15174 -- component_declaration node
15176 if Nkind (P) = N_Component_Definition then
15177 P := Parent (P);
15178 end if;
15180 -- Case of an anonymous array subtype
15182 if Nkind_In (Def_Kind, N_Constrained_Array_Definition,
15183 N_Unconstrained_Array_Definition)
15184 then
15185 T := Empty;
15186 Array_Type_Declaration (T, Obj_Def);
15188 -- Create an explicit subtype whenever possible
15190 elsif Nkind (P) /= N_Component_Declaration
15191 and then Def_Kind = N_Subtype_Indication
15192 then
15193 -- Base name of subtype on object name, which will be unique in
15194 -- the current scope.
15196 -- If this is a duplicate declaration, return base type, to avoid
15197 -- generating duplicate anonymous types.
15199 if Error_Posted (P) then
15200 Analyze (Subtype_Mark (Obj_Def));
15201 return Entity (Subtype_Mark (Obj_Def));
15202 end if;
15204 Nam :=
15205 New_External_Name
15206 (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
15208 T := Make_Defining_Identifier (Sloc (P), Nam);
15210 Insert_Action (Obj_Def,
15211 Make_Subtype_Declaration (Sloc (P),
15212 Defining_Identifier => T,
15213 Subtype_Indication => Relocate_Node (Obj_Def)));
15215 -- This subtype may need freezing, and this will not be done
15216 -- automatically if the object declaration is not in declarative
15217 -- part. Since this is an object declaration, the type cannot always
15218 -- be frozen here. Deferred constants do not freeze their type
15219 -- (which often enough will be private).
15221 if Nkind (P) = N_Object_Declaration
15222 and then Constant_Present (P)
15223 and then No (Expression (P))
15224 then
15225 null;
15226 else
15227 Insert_Actions (Obj_Def, Freeze_Entity (T, P));
15228 end if;
15230 -- Ada 2005 AI-406: the object definition in an object declaration
15231 -- can be an access definition.
15233 elsif Def_Kind = N_Access_Definition then
15234 T := Access_Definition (Related_Nod, Obj_Def);
15236 Set_Is_Local_Anonymous_Access
15238 V => (Ada_Version < Ada_2012)
15239 or else (Nkind (P) /= N_Object_Declaration)
15240 or else Is_Library_Level_Entity (Defining_Identifier (P)));
15242 -- Otherwise, the object definition is just a subtype_mark
15244 else
15245 T := Process_Subtype (Obj_Def, Related_Nod);
15247 -- If expansion is disabled an object definition that is an aggregate
15248 -- will not get expanded and may lead to scoping problems in the back
15249 -- end, if the object is referenced in an inner scope. In that case
15250 -- create an itype reference for the object definition now. This
15251 -- may be redundant in some cases, but harmless.
15253 if Is_Itype (T)
15254 and then Nkind (Related_Nod) = N_Object_Declaration
15255 and then ASIS_Mode
15256 then
15257 Build_Itype_Reference (T, Related_Nod);
15258 end if;
15259 end if;
15261 return T;
15262 end Find_Type_Of_Object;
15264 --------------------------------
15265 -- Find_Type_Of_Subtype_Indic --
15266 --------------------------------
15268 function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
15269 Typ : Entity_Id;
15271 begin
15272 -- Case of subtype mark with a constraint
15274 if Nkind (S) = N_Subtype_Indication then
15275 Find_Type (Subtype_Mark (S));
15276 Typ := Entity (Subtype_Mark (S));
15278 if not
15279 Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
15280 then
15281 Error_Msg_N
15282 ("incorrect constraint for this kind of type", Constraint (S));
15283 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
15284 end if;
15286 -- Otherwise we have a subtype mark without a constraint
15288 elsif Error_Posted (S) then
15289 Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
15290 return Any_Type;
15292 else
15293 Find_Type (S);
15294 Typ := Entity (S);
15295 end if;
15297 -- Check No_Wide_Characters restriction
15299 Check_Wide_Character_Restriction (Typ, S);
15301 return Typ;
15302 end Find_Type_Of_Subtype_Indic;
15304 -------------------------------------
15305 -- Floating_Point_Type_Declaration --
15306 -------------------------------------
15308 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
15309 Digs : constant Node_Id := Digits_Expression (Def);
15310 Max_Digs_Val : constant Uint := Digits_Value (Standard_Long_Long_Float);
15311 Digs_Val : Uint;
15312 Base_Typ : Entity_Id;
15313 Implicit_Base : Entity_Id;
15314 Bound : Node_Id;
15316 function Can_Derive_From (E : Entity_Id) return Boolean;
15317 -- Find if given digits value, and possibly a specified range, allows
15318 -- derivation from specified type
15320 function Find_Base_Type return Entity_Id;
15321 -- Find a predefined base type that Def can derive from, or generate
15322 -- an error and substitute Long_Long_Float if none exists.
15324 ---------------------
15325 -- Can_Derive_From --
15326 ---------------------
15328 function Can_Derive_From (E : Entity_Id) return Boolean is
15329 Spec : constant Entity_Id := Real_Range_Specification (Def);
15331 begin
15332 -- Check specified "digits" constraint
15334 if Digs_Val > Digits_Value (E) then
15335 return False;
15336 end if;
15338 -- Avoid types not matching pragma Float_Representation, if present
15340 if (Opt.Float_Format = 'I' and then Float_Rep (E) /= IEEE_Binary)
15341 or else
15342 (Opt.Float_Format = 'V' and then Float_Rep (E) /= VAX_Native)
15343 then
15344 return False;
15345 end if;
15347 -- Check for matching range, if specified
15349 if Present (Spec) then
15350 if Expr_Value_R (Type_Low_Bound (E)) >
15351 Expr_Value_R (Low_Bound (Spec))
15352 then
15353 return False;
15354 end if;
15356 if Expr_Value_R (Type_High_Bound (E)) <
15357 Expr_Value_R (High_Bound (Spec))
15358 then
15359 return False;
15360 end if;
15361 end if;
15363 return True;
15364 end Can_Derive_From;
15366 --------------------
15367 -- Find_Base_Type --
15368 --------------------
15370 function Find_Base_Type return Entity_Id is
15371 Choice : Elmt_Id := First_Elmt (Predefined_Float_Types);
15373 begin
15374 -- Iterate over the predefined types in order, returning the first
15375 -- one that Def can derive from.
15377 while Present (Choice) loop
15378 if Can_Derive_From (Node (Choice)) then
15379 return Node (Choice);
15380 end if;
15382 Next_Elmt (Choice);
15383 end loop;
15385 -- If we can't derive from any existing type, use Long_Long_Float
15386 -- and give appropriate message explaining the problem.
15388 if Digs_Val > Max_Digs_Val then
15389 -- It might be the case that there is a type with the requested
15390 -- range, just not the combination of digits and range.
15392 Error_Msg_N
15393 ("no predefined type has requested range and precision",
15394 Real_Range_Specification (Def));
15396 else
15397 Error_Msg_N
15398 ("range too large for any predefined type",
15399 Real_Range_Specification (Def));
15400 end if;
15402 return Standard_Long_Long_Float;
15403 end Find_Base_Type;
15405 -- Start of processing for Floating_Point_Type_Declaration
15407 begin
15408 Check_Restriction (No_Floating_Point, Def);
15410 -- Create an implicit base type
15412 Implicit_Base :=
15413 Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
15415 -- Analyze and verify digits value
15417 Analyze_And_Resolve (Digs, Any_Integer);
15418 Check_Digits_Expression (Digs);
15419 Digs_Val := Expr_Value (Digs);
15421 -- Process possible range spec and find correct type to derive from
15423 Process_Real_Range_Specification (Def);
15425 -- Check that requested number of digits is not too high.
15427 if Digs_Val > Max_Digs_Val then
15428 -- The check for Max_Base_Digits may be somewhat expensive, as it
15429 -- requires reading System, so only do it when necessary.
15431 declare
15432 Max_Base_Digits : constant Uint :=
15433 Expr_Value
15434 (Expression
15435 (Parent (RTE (RE_Max_Base_Digits))));
15437 begin
15438 if Digs_Val > Max_Base_Digits then
15439 Error_Msg_Uint_1 := Max_Base_Digits;
15440 Error_Msg_N ("digits value out of range, maximum is ^", Digs);
15442 elsif No (Real_Range_Specification (Def)) then
15443 Error_Msg_Uint_1 := Max_Digs_Val;
15444 Error_Msg_N ("types with more than ^ digits need range spec "
15445 & "(RM 3.5.7(6))", Digs);
15446 end if;
15447 end;
15448 end if;
15450 -- Find a suitable type to derive from or complain and use a substitute
15452 Base_Typ := Find_Base_Type;
15454 -- If there are bounds given in the declaration use them as the bounds
15455 -- of the type, otherwise use the bounds of the predefined base type
15456 -- that was chosen based on the Digits value.
15458 if Present (Real_Range_Specification (Def)) then
15459 Set_Scalar_Range (T, Real_Range_Specification (Def));
15460 Set_Is_Constrained (T);
15462 -- The bounds of this range must be converted to machine numbers
15463 -- in accordance with RM 4.9(38).
15465 Bound := Type_Low_Bound (T);
15467 if Nkind (Bound) = N_Real_Literal then
15468 Set_Realval
15469 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
15470 Set_Is_Machine_Number (Bound);
15471 end if;
15473 Bound := Type_High_Bound (T);
15475 if Nkind (Bound) = N_Real_Literal then
15476 Set_Realval
15477 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
15478 Set_Is_Machine_Number (Bound);
15479 end if;
15481 else
15482 Set_Scalar_Range (T, Scalar_Range (Base_Typ));
15483 end if;
15485 -- Complete definition of implicit base and declared first subtype
15487 Set_Etype (Implicit_Base, Base_Typ);
15489 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
15490 Set_Size_Info (Implicit_Base, (Base_Typ));
15491 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
15492 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
15493 Set_Digits_Value (Implicit_Base, Digits_Value (Base_Typ));
15494 Set_Float_Rep (Implicit_Base, Float_Rep (Base_Typ));
15496 Set_Ekind (T, E_Floating_Point_Subtype);
15497 Set_Etype (T, Implicit_Base);
15499 Set_Size_Info (T, (Implicit_Base));
15500 Set_RM_Size (T, RM_Size (Implicit_Base));
15501 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
15502 Set_Digits_Value (T, Digs_Val);
15503 end Floating_Point_Type_Declaration;
15505 ----------------------------
15506 -- Get_Discriminant_Value --
15507 ----------------------------
15509 -- This is the situation:
15511 -- There is a non-derived type
15513 -- type T0 (Dx, Dy, Dz...)
15515 -- There are zero or more levels of derivation, with each derivation
15516 -- either purely inheriting the discriminants, or defining its own.
15518 -- type Ti is new Ti-1
15519 -- or
15520 -- type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
15521 -- or
15522 -- subtype Ti is ...
15524 -- The subtype issue is avoided by the use of Original_Record_Component,
15525 -- and the fact that derived subtypes also derive the constraints.
15527 -- This chain leads back from
15529 -- Typ_For_Constraint
15531 -- Typ_For_Constraint has discriminants, and the value for each
15532 -- discriminant is given by its corresponding Elmt of Constraints.
15534 -- Discriminant is some discriminant in this hierarchy
15536 -- We need to return its value
15538 -- We do this by recursively searching each level, and looking for
15539 -- Discriminant. Once we get to the bottom, we start backing up
15540 -- returning the value for it which may in turn be a discriminant
15541 -- further up, so on the backup we continue the substitution.
15543 function Get_Discriminant_Value
15544 (Discriminant : Entity_Id;
15545 Typ_For_Constraint : Entity_Id;
15546 Constraint : Elist_Id) return Node_Id
15548 function Root_Corresponding_Discriminant
15549 (Discr : Entity_Id) return Entity_Id;
15550 -- Given a discriminant, traverse the chain of inherited discriminants
15551 -- and return the topmost discriminant.
15553 function Search_Derivation_Levels
15554 (Ti : Entity_Id;
15555 Discrim_Values : Elist_Id;
15556 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
15557 -- This is the routine that performs the recursive search of levels
15558 -- as described above.
15560 -------------------------------------
15561 -- Root_Corresponding_Discriminant --
15562 -------------------------------------
15564 function Root_Corresponding_Discriminant
15565 (Discr : Entity_Id) return Entity_Id
15567 D : Entity_Id;
15569 begin
15570 D := Discr;
15571 while Present (Corresponding_Discriminant (D)) loop
15572 D := Corresponding_Discriminant (D);
15573 end loop;
15575 return D;
15576 end Root_Corresponding_Discriminant;
15578 ------------------------------
15579 -- Search_Derivation_Levels --
15580 ------------------------------
15582 function Search_Derivation_Levels
15583 (Ti : Entity_Id;
15584 Discrim_Values : Elist_Id;
15585 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
15587 Assoc : Elmt_Id;
15588 Disc : Entity_Id;
15589 Result : Node_Or_Entity_Id;
15590 Result_Entity : Node_Id;
15592 begin
15593 -- If inappropriate type, return Error, this happens only in
15594 -- cascaded error situations, and we want to avoid a blow up.
15596 if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
15597 return Error;
15598 end if;
15600 -- Look deeper if possible. Use Stored_Constraints only for
15601 -- untagged types. For tagged types use the given constraint.
15602 -- This asymmetry needs explanation???
15604 if not Stored_Discrim_Values
15605 and then Present (Stored_Constraint (Ti))
15606 and then not Is_Tagged_Type (Ti)
15607 then
15608 Result :=
15609 Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
15610 else
15611 declare
15612 Td : constant Entity_Id := Etype (Ti);
15614 begin
15615 if Td = Ti then
15616 Result := Discriminant;
15618 else
15619 if Present (Stored_Constraint (Ti)) then
15620 Result :=
15621 Search_Derivation_Levels
15622 (Td, Stored_Constraint (Ti), True);
15623 else
15624 Result :=
15625 Search_Derivation_Levels
15626 (Td, Discrim_Values, Stored_Discrim_Values);
15627 end if;
15628 end if;
15629 end;
15630 end if;
15632 -- Extra underlying places to search, if not found above. For
15633 -- concurrent types, the relevant discriminant appears in the
15634 -- corresponding record. For a type derived from a private type
15635 -- without discriminant, the full view inherits the discriminants
15636 -- of the full view of the parent.
15638 if Result = Discriminant then
15639 if Is_Concurrent_Type (Ti)
15640 and then Present (Corresponding_Record_Type (Ti))
15641 then
15642 Result :=
15643 Search_Derivation_Levels (
15644 Corresponding_Record_Type (Ti),
15645 Discrim_Values,
15646 Stored_Discrim_Values);
15648 elsif Is_Private_Type (Ti)
15649 and then not Has_Discriminants (Ti)
15650 and then Present (Full_View (Ti))
15651 and then Etype (Full_View (Ti)) /= Ti
15652 then
15653 Result :=
15654 Search_Derivation_Levels (
15655 Full_View (Ti),
15656 Discrim_Values,
15657 Stored_Discrim_Values);
15658 end if;
15659 end if;
15661 -- If Result is not a (reference to a) discriminant, return it,
15662 -- otherwise set Result_Entity to the discriminant.
15664 if Nkind (Result) = N_Defining_Identifier then
15665 pragma Assert (Result = Discriminant);
15666 Result_Entity := Result;
15668 else
15669 if not Denotes_Discriminant (Result) then
15670 return Result;
15671 end if;
15673 Result_Entity := Entity (Result);
15674 end if;
15676 -- See if this level of derivation actually has discriminants
15677 -- because tagged derivations can add them, hence the lower
15678 -- levels need not have any.
15680 if not Has_Discriminants (Ti) then
15681 return Result;
15682 end if;
15684 -- Scan Ti's discriminants for Result_Entity,
15685 -- and return its corresponding value, if any.
15687 Result_Entity := Original_Record_Component (Result_Entity);
15689 Assoc := First_Elmt (Discrim_Values);
15691 if Stored_Discrim_Values then
15692 Disc := First_Stored_Discriminant (Ti);
15693 else
15694 Disc := First_Discriminant (Ti);
15695 end if;
15697 while Present (Disc) loop
15698 pragma Assert (Present (Assoc));
15700 if Original_Record_Component (Disc) = Result_Entity then
15701 return Node (Assoc);
15702 end if;
15704 Next_Elmt (Assoc);
15706 if Stored_Discrim_Values then
15707 Next_Stored_Discriminant (Disc);
15708 else
15709 Next_Discriminant (Disc);
15710 end if;
15711 end loop;
15713 -- Could not find it
15715 return Result;
15716 end Search_Derivation_Levels;
15718 -- Local Variables
15720 Result : Node_Or_Entity_Id;
15722 -- Start of processing for Get_Discriminant_Value
15724 begin
15725 -- ??? This routine is a gigantic mess and will be deleted. For the
15726 -- time being just test for the trivial case before calling recurse.
15728 if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
15729 declare
15730 D : Entity_Id;
15731 E : Elmt_Id;
15733 begin
15734 D := First_Discriminant (Typ_For_Constraint);
15735 E := First_Elmt (Constraint);
15736 while Present (D) loop
15737 if Chars (D) = Chars (Discriminant) then
15738 return Node (E);
15739 end if;
15741 Next_Discriminant (D);
15742 Next_Elmt (E);
15743 end loop;
15744 end;
15745 end if;
15747 Result := Search_Derivation_Levels
15748 (Typ_For_Constraint, Constraint, False);
15750 -- ??? hack to disappear when this routine is gone
15752 if Nkind (Result) = N_Defining_Identifier then
15753 declare
15754 D : Entity_Id;
15755 E : Elmt_Id;
15757 begin
15758 D := First_Discriminant (Typ_For_Constraint);
15759 E := First_Elmt (Constraint);
15760 while Present (D) loop
15761 if Root_Corresponding_Discriminant (D) = Discriminant then
15762 return Node (E);
15763 end if;
15765 Next_Discriminant (D);
15766 Next_Elmt (E);
15767 end loop;
15768 end;
15769 end if;
15771 pragma Assert (Nkind (Result) /= N_Defining_Identifier);
15772 return Result;
15773 end Get_Discriminant_Value;
15775 --------------------------
15776 -- Has_Range_Constraint --
15777 --------------------------
15779 function Has_Range_Constraint (N : Node_Id) return Boolean is
15780 C : constant Node_Id := Constraint (N);
15782 begin
15783 if Nkind (C) = N_Range_Constraint then
15784 return True;
15786 elsif Nkind (C) = N_Digits_Constraint then
15787 return
15788 Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
15789 or else
15790 Present (Range_Constraint (C));
15792 elsif Nkind (C) = N_Delta_Constraint then
15793 return Present (Range_Constraint (C));
15795 else
15796 return False;
15797 end if;
15798 end Has_Range_Constraint;
15800 ------------------------
15801 -- Inherit_Components --
15802 ------------------------
15804 function Inherit_Components
15805 (N : Node_Id;
15806 Parent_Base : Entity_Id;
15807 Derived_Base : Entity_Id;
15808 Is_Tagged : Boolean;
15809 Inherit_Discr : Boolean;
15810 Discs : Elist_Id) return Elist_Id
15812 Assoc_List : constant Elist_Id := New_Elmt_List;
15814 procedure Inherit_Component
15815 (Old_C : Entity_Id;
15816 Plain_Discrim : Boolean := False;
15817 Stored_Discrim : Boolean := False);
15818 -- Inherits component Old_C from Parent_Base to the Derived_Base. If
15819 -- Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
15820 -- True, Old_C is a stored discriminant. If they are both false then
15821 -- Old_C is a regular component.
15823 -----------------------
15824 -- Inherit_Component --
15825 -----------------------
15827 procedure Inherit_Component
15828 (Old_C : Entity_Id;
15829 Plain_Discrim : Boolean := False;
15830 Stored_Discrim : Boolean := False)
15832 procedure Set_Anonymous_Type (Id : Entity_Id);
15833 -- Id denotes the entity of an access discriminant or anonymous
15834 -- access component. Set the type of Id to either the same type of
15835 -- Old_C or create a new one depending on whether the parent and
15836 -- the child types are in the same scope.
15838 ------------------------
15839 -- Set_Anonymous_Type --
15840 ------------------------
15842 procedure Set_Anonymous_Type (Id : Entity_Id) is
15843 Old_Typ : constant Entity_Id := Etype (Old_C);
15845 begin
15846 if Scope (Parent_Base) = Scope (Derived_Base) then
15847 Set_Etype (Id, Old_Typ);
15849 -- The parent and the derived type are in two different scopes.
15850 -- Reuse the type of the original discriminant / component by
15851 -- copying it in order to preserve all attributes.
15853 else
15854 declare
15855 Typ : constant Entity_Id := New_Copy (Old_Typ);
15857 begin
15858 Set_Etype (Id, Typ);
15860 -- Since we do not generate component declarations for
15861 -- inherited components, associate the itype with the
15862 -- derived type.
15864 Set_Associated_Node_For_Itype (Typ, Parent (Derived_Base));
15865 Set_Scope (Typ, Derived_Base);
15866 end;
15867 end if;
15868 end Set_Anonymous_Type;
15870 -- Local variables and constants
15872 New_C : constant Entity_Id := New_Copy (Old_C);
15874 Corr_Discrim : Entity_Id;
15875 Discrim : Entity_Id;
15877 -- Start of processing for Inherit_Component
15879 begin
15880 pragma Assert (not Is_Tagged or else not Stored_Discrim);
15882 Set_Parent (New_C, Parent (Old_C));
15884 -- Regular discriminants and components must be inserted in the scope
15885 -- of the Derived_Base. Do it here.
15887 if not Stored_Discrim then
15888 Enter_Name (New_C);
15889 end if;
15891 -- For tagged types the Original_Record_Component must point to
15892 -- whatever this field was pointing to in the parent type. This has
15893 -- already been achieved by the call to New_Copy above.
15895 if not Is_Tagged then
15896 Set_Original_Record_Component (New_C, New_C);
15897 end if;
15899 -- Set the proper type of an access discriminant
15901 if Ekind (New_C) = E_Discriminant
15902 and then Ekind (Etype (New_C)) = E_Anonymous_Access_Type
15903 then
15904 Set_Anonymous_Type (New_C);
15905 end if;
15907 -- If we have inherited a component then see if its Etype contains
15908 -- references to Parent_Base discriminants. In this case, replace
15909 -- these references with the constraints given in Discs. We do not
15910 -- do this for the partial view of private types because this is
15911 -- not needed (only the components of the full view will be used
15912 -- for code generation) and cause problem. We also avoid this
15913 -- transformation in some error situations.
15915 if Ekind (New_C) = E_Component then
15917 -- Set the proper type of an anonymous access component
15919 if Ekind (Etype (New_C)) = E_Anonymous_Access_Type then
15920 Set_Anonymous_Type (New_C);
15922 elsif (Is_Private_Type (Derived_Base)
15923 and then not Is_Generic_Type (Derived_Base))
15924 or else (Is_Empty_Elmt_List (Discs)
15925 and then not Expander_Active)
15926 then
15927 Set_Etype (New_C, Etype (Old_C));
15929 else
15930 -- The current component introduces a circularity of the
15931 -- following kind:
15933 -- limited with Pack_2;
15934 -- package Pack_1 is
15935 -- type T_1 is tagged record
15936 -- Comp : access Pack_2.T_2;
15937 -- ...
15938 -- end record;
15939 -- end Pack_1;
15941 -- with Pack_1;
15942 -- package Pack_2 is
15943 -- type T_2 is new Pack_1.T_1 with ...;
15944 -- end Pack_2;
15946 Set_Etype
15947 (New_C,
15948 Constrain_Component_Type
15949 (Old_C, Derived_Base, N, Parent_Base, Discs));
15950 end if;
15951 end if;
15953 -- In derived tagged types it is illegal to reference a non
15954 -- discriminant component in the parent type. To catch this, mark
15955 -- these components with an Ekind of E_Void. This will be reset in
15956 -- Record_Type_Definition after processing the record extension of
15957 -- the derived type.
15959 -- If the declaration is a private extension, there is no further
15960 -- record extension to process, and the components retain their
15961 -- current kind, because they are visible at this point.
15963 if Is_Tagged and then Ekind (New_C) = E_Component
15964 and then Nkind (N) /= N_Private_Extension_Declaration
15965 then
15966 Set_Ekind (New_C, E_Void);
15967 end if;
15969 if Plain_Discrim then
15970 Set_Corresponding_Discriminant (New_C, Old_C);
15971 Build_Discriminal (New_C);
15973 -- If we are explicitly inheriting a stored discriminant it will be
15974 -- completely hidden.
15976 elsif Stored_Discrim then
15977 Set_Corresponding_Discriminant (New_C, Empty);
15978 Set_Discriminal (New_C, Empty);
15979 Set_Is_Completely_Hidden (New_C);
15981 -- Set the Original_Record_Component of each discriminant in the
15982 -- derived base to point to the corresponding stored that we just
15983 -- created.
15985 Discrim := First_Discriminant (Derived_Base);
15986 while Present (Discrim) loop
15987 Corr_Discrim := Corresponding_Discriminant (Discrim);
15989 -- Corr_Discrim could be missing in an error situation
15991 if Present (Corr_Discrim)
15992 and then Original_Record_Component (Corr_Discrim) = Old_C
15993 then
15994 Set_Original_Record_Component (Discrim, New_C);
15995 end if;
15997 Next_Discriminant (Discrim);
15998 end loop;
16000 Append_Entity (New_C, Derived_Base);
16001 end if;
16003 if not Is_Tagged then
16004 Append_Elmt (Old_C, Assoc_List);
16005 Append_Elmt (New_C, Assoc_List);
16006 end if;
16007 end Inherit_Component;
16009 -- Variables local to Inherit_Component
16011 Loc : constant Source_Ptr := Sloc (N);
16013 Parent_Discrim : Entity_Id;
16014 Stored_Discrim : Entity_Id;
16015 D : Entity_Id;
16016 Component : Entity_Id;
16018 -- Start of processing for Inherit_Components
16020 begin
16021 if not Is_Tagged then
16022 Append_Elmt (Parent_Base, Assoc_List);
16023 Append_Elmt (Derived_Base, Assoc_List);
16024 end if;
16026 -- Inherit parent discriminants if needed
16028 if Inherit_Discr then
16029 Parent_Discrim := First_Discriminant (Parent_Base);
16030 while Present (Parent_Discrim) loop
16031 Inherit_Component (Parent_Discrim, Plain_Discrim => True);
16032 Next_Discriminant (Parent_Discrim);
16033 end loop;
16034 end if;
16036 -- Create explicit stored discrims for untagged types when necessary
16038 if not Has_Unknown_Discriminants (Derived_Base)
16039 and then Has_Discriminants (Parent_Base)
16040 and then not Is_Tagged
16041 and then
16042 (not Inherit_Discr
16043 or else First_Discriminant (Parent_Base) /=
16044 First_Stored_Discriminant (Parent_Base))
16045 then
16046 Stored_Discrim := First_Stored_Discriminant (Parent_Base);
16047 while Present (Stored_Discrim) loop
16048 Inherit_Component (Stored_Discrim, Stored_Discrim => True);
16049 Next_Stored_Discriminant (Stored_Discrim);
16050 end loop;
16051 end if;
16053 -- See if we can apply the second transformation for derived types, as
16054 -- explained in point 6. in the comments above Build_Derived_Record_Type
16055 -- This is achieved by appending Derived_Base discriminants into Discs,
16056 -- which has the side effect of returning a non empty Discs list to the
16057 -- caller of Inherit_Components, which is what we want. This must be
16058 -- done for private derived types if there are explicit stored
16059 -- discriminants, to ensure that we can retrieve the values of the
16060 -- constraints provided in the ancestors.
16062 if Inherit_Discr
16063 and then Is_Empty_Elmt_List (Discs)
16064 and then Present (First_Discriminant (Derived_Base))
16065 and then
16066 (not Is_Private_Type (Derived_Base)
16067 or else Is_Completely_Hidden
16068 (First_Stored_Discriminant (Derived_Base))
16069 or else Is_Generic_Type (Derived_Base))
16070 then
16071 D := First_Discriminant (Derived_Base);
16072 while Present (D) loop
16073 Append_Elmt (New_Reference_To (D, Loc), Discs);
16074 Next_Discriminant (D);
16075 end loop;
16076 end if;
16078 -- Finally, inherit non-discriminant components unless they are not
16079 -- visible because defined or inherited from the full view of the
16080 -- parent. Don't inherit the _parent field of the parent type.
16082 Component := First_Entity (Parent_Base);
16083 while Present (Component) loop
16085 -- Ada 2005 (AI-251): Do not inherit components associated with
16086 -- secondary tags of the parent.
16088 if Ekind (Component) = E_Component
16089 and then Present (Related_Type (Component))
16090 then
16091 null;
16093 elsif Ekind (Component) /= E_Component
16094 or else Chars (Component) = Name_uParent
16095 then
16096 null;
16098 -- If the derived type is within the parent type's declarative
16099 -- region, then the components can still be inherited even though
16100 -- they aren't visible at this point. This can occur for cases
16101 -- such as within public child units where the components must
16102 -- become visible upon entering the child unit's private part.
16104 elsif not Is_Visible_Component (Component)
16105 and then not In_Open_Scopes (Scope (Parent_Base))
16106 then
16107 null;
16109 elsif Ekind_In (Derived_Base, E_Private_Type,
16110 E_Limited_Private_Type)
16111 then
16112 null;
16114 else
16115 Inherit_Component (Component);
16116 end if;
16118 Next_Entity (Component);
16119 end loop;
16121 -- For tagged derived types, inherited discriminants cannot be used in
16122 -- component declarations of the record extension part. To achieve this
16123 -- we mark the inherited discriminants as not visible.
16125 if Is_Tagged and then Inherit_Discr then
16126 D := First_Discriminant (Derived_Base);
16127 while Present (D) loop
16128 Set_Is_Immediately_Visible (D, False);
16129 Next_Discriminant (D);
16130 end loop;
16131 end if;
16133 return Assoc_List;
16134 end Inherit_Components;
16136 -----------------------
16137 -- Is_Constant_Bound --
16138 -----------------------
16140 function Is_Constant_Bound (Exp : Node_Id) return Boolean is
16141 begin
16142 if Compile_Time_Known_Value (Exp) then
16143 return True;
16145 elsif Is_Entity_Name (Exp)
16146 and then Present (Entity (Exp))
16147 then
16148 return Is_Constant_Object (Entity (Exp))
16149 or else Ekind (Entity (Exp)) = E_Enumeration_Literal;
16151 elsif Nkind (Exp) in N_Binary_Op then
16152 return Is_Constant_Bound (Left_Opnd (Exp))
16153 and then Is_Constant_Bound (Right_Opnd (Exp))
16154 and then Scope (Entity (Exp)) = Standard_Standard;
16156 else
16157 return False;
16158 end if;
16159 end Is_Constant_Bound;
16161 -----------------------
16162 -- Is_Null_Extension --
16163 -----------------------
16165 function Is_Null_Extension (T : Entity_Id) return Boolean is
16166 Type_Decl : constant Node_Id := Parent (Base_Type (T));
16167 Comp_List : Node_Id;
16168 Comp : Node_Id;
16170 begin
16171 if Nkind (Type_Decl) /= N_Full_Type_Declaration
16172 or else not Is_Tagged_Type (T)
16173 or else Nkind (Type_Definition (Type_Decl)) /=
16174 N_Derived_Type_Definition
16175 or else No (Record_Extension_Part (Type_Definition (Type_Decl)))
16176 then
16177 return False;
16178 end if;
16180 Comp_List :=
16181 Component_List (Record_Extension_Part (Type_Definition (Type_Decl)));
16183 if Present (Discriminant_Specifications (Type_Decl)) then
16184 return False;
16186 elsif Present (Comp_List)
16187 and then Is_Non_Empty_List (Component_Items (Comp_List))
16188 then
16189 Comp := First (Component_Items (Comp_List));
16191 -- Only user-defined components are relevant. The component list
16192 -- may also contain a parent component and internal components
16193 -- corresponding to secondary tags, but these do not determine
16194 -- whether this is a null extension.
16196 while Present (Comp) loop
16197 if Comes_From_Source (Comp) then
16198 return False;
16199 end if;
16201 Next (Comp);
16202 end loop;
16204 return True;
16205 else
16206 return True;
16207 end if;
16208 end Is_Null_Extension;
16210 ------------------------------
16211 -- Is_Valid_Constraint_Kind --
16212 ------------------------------
16214 function Is_Valid_Constraint_Kind
16215 (T_Kind : Type_Kind;
16216 Constraint_Kind : Node_Kind) return Boolean
16218 begin
16219 case T_Kind is
16220 when Enumeration_Kind |
16221 Integer_Kind =>
16222 return Constraint_Kind = N_Range_Constraint;
16224 when Decimal_Fixed_Point_Kind =>
16225 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
16226 N_Range_Constraint);
16228 when Ordinary_Fixed_Point_Kind =>
16229 return Nkind_In (Constraint_Kind, N_Delta_Constraint,
16230 N_Range_Constraint);
16232 when Float_Kind =>
16233 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
16234 N_Range_Constraint);
16236 when Access_Kind |
16237 Array_Kind |
16238 E_Record_Type |
16239 E_Record_Subtype |
16240 Class_Wide_Kind |
16241 E_Incomplete_Type |
16242 Private_Kind |
16243 Concurrent_Kind =>
16244 return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
16246 when others =>
16247 return True; -- Error will be detected later
16248 end case;
16249 end Is_Valid_Constraint_Kind;
16251 --------------------------
16252 -- Is_Visible_Component --
16253 --------------------------
16255 function Is_Visible_Component (C : Entity_Id) return Boolean is
16256 Original_Comp : Entity_Id := Empty;
16257 Original_Scope : Entity_Id;
16258 Type_Scope : Entity_Id;
16260 function Is_Local_Type (Typ : Entity_Id) return Boolean;
16261 -- Check whether parent type of inherited component is declared locally,
16262 -- possibly within a nested package or instance. The current scope is
16263 -- the derived record itself.
16265 -------------------
16266 -- Is_Local_Type --
16267 -------------------
16269 function Is_Local_Type (Typ : Entity_Id) return Boolean is
16270 Scop : Entity_Id;
16272 begin
16273 Scop := Scope (Typ);
16274 while Present (Scop)
16275 and then Scop /= Standard_Standard
16276 loop
16277 if Scop = Scope (Current_Scope) then
16278 return True;
16279 end if;
16281 Scop := Scope (Scop);
16282 end loop;
16284 return False;
16285 end Is_Local_Type;
16287 -- Start of processing for Is_Visible_Component
16289 begin
16290 if Ekind_In (C, E_Component, E_Discriminant) then
16291 Original_Comp := Original_Record_Component (C);
16292 end if;
16294 if No (Original_Comp) then
16296 -- Premature usage, or previous error
16298 return False;
16300 else
16301 Original_Scope := Scope (Original_Comp);
16302 Type_Scope := Scope (Base_Type (Scope (C)));
16303 end if;
16305 -- This test only concerns tagged types
16307 if not Is_Tagged_Type (Original_Scope) then
16308 return True;
16310 -- If it is _Parent or _Tag, there is no visibility issue
16312 elsif not Comes_From_Source (Original_Comp) then
16313 return True;
16315 -- Discriminants are always visible
16317 elsif Ekind (Original_Comp) = E_Discriminant
16318 and then not Has_Unknown_Discriminants (Original_Scope)
16319 then
16320 return True;
16322 -- In the body of an instantiation, no need to check for the visibility
16323 -- of a component.
16325 elsif In_Instance_Body then
16326 return True;
16328 -- If the component has been declared in an ancestor which is currently
16329 -- a private type, then it is not visible. The same applies if the
16330 -- component's containing type is not in an open scope and the original
16331 -- component's enclosing type is a visible full view of a private type
16332 -- (which can occur in cases where an attempt is being made to reference
16333 -- a component in a sibling package that is inherited from a visible
16334 -- component of a type in an ancestor package; the component in the
16335 -- sibling package should not be visible even though the component it
16336 -- inherited from is visible). This does not apply however in the case
16337 -- where the scope of the type is a private child unit, or when the
16338 -- parent comes from a local package in which the ancestor is currently
16339 -- visible. The latter suppression of visibility is needed for cases
16340 -- that are tested in B730006.
16342 elsif Is_Private_Type (Original_Scope)
16343 or else
16344 (not Is_Private_Descendant (Type_Scope)
16345 and then not In_Open_Scopes (Type_Scope)
16346 and then Has_Private_Declaration (Original_Scope))
16347 then
16348 -- If the type derives from an entity in a formal package, there
16349 -- are no additional visible components.
16351 if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
16352 N_Formal_Package_Declaration
16353 then
16354 return False;
16356 -- if we are not in the private part of the current package, there
16357 -- are no additional visible components.
16359 elsif Ekind (Scope (Current_Scope)) = E_Package
16360 and then not In_Private_Part (Scope (Current_Scope))
16361 then
16362 return False;
16363 else
16364 return
16365 Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
16366 and then In_Open_Scopes (Scope (Original_Scope))
16367 and then Is_Local_Type (Type_Scope);
16368 end if;
16370 -- There is another weird way in which a component may be invisible
16371 -- when the private and the full view are not derived from the same
16372 -- ancestor. Here is an example :
16374 -- type A1 is tagged record F1 : integer; end record;
16375 -- type A2 is new A1 with record F2 : integer; end record;
16376 -- type T is new A1 with private;
16377 -- private
16378 -- type T is new A2 with null record;
16380 -- In this case, the full view of T inherits F1 and F2 but the private
16381 -- view inherits only F1
16383 else
16384 declare
16385 Ancestor : Entity_Id := Scope (C);
16387 begin
16388 loop
16389 if Ancestor = Original_Scope then
16390 return True;
16391 elsif Ancestor = Etype (Ancestor) then
16392 return False;
16393 end if;
16395 Ancestor := Etype (Ancestor);
16396 end loop;
16397 end;
16398 end if;
16399 end Is_Visible_Component;
16401 --------------------------
16402 -- Make_Class_Wide_Type --
16403 --------------------------
16405 procedure Make_Class_Wide_Type (T : Entity_Id) is
16406 CW_Type : Entity_Id;
16407 CW_Name : Name_Id;
16408 Next_E : Entity_Id;
16410 begin
16411 if Present (Class_Wide_Type (T)) then
16413 -- The class-wide type is a partially decorated entity created for a
16414 -- unanalyzed tagged type referenced through a limited with clause.
16415 -- When the tagged type is analyzed, its class-wide type needs to be
16416 -- redecorated. Note that we reuse the entity created by Decorate_
16417 -- Tagged_Type in order to preserve all links.
16419 if Materialize_Entity (Class_Wide_Type (T)) then
16420 CW_Type := Class_Wide_Type (T);
16421 Set_Materialize_Entity (CW_Type, False);
16423 -- The class wide type can have been defined by the partial view, in
16424 -- which case everything is already done.
16426 else
16427 return;
16428 end if;
16430 -- Default case, we need to create a new class-wide type
16432 else
16433 CW_Type :=
16434 New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
16435 end if;
16437 -- Inherit root type characteristics
16439 CW_Name := Chars (CW_Type);
16440 Next_E := Next_Entity (CW_Type);
16441 Copy_Node (T, CW_Type);
16442 Set_Comes_From_Source (CW_Type, False);
16443 Set_Chars (CW_Type, CW_Name);
16444 Set_Parent (CW_Type, Parent (T));
16445 Set_Next_Entity (CW_Type, Next_E);
16447 -- Ensure we have a new freeze node for the class-wide type. The partial
16448 -- view may have freeze action of its own, requiring a proper freeze
16449 -- node, and the same freeze node cannot be shared between the two
16450 -- types.
16452 Set_Has_Delayed_Freeze (CW_Type);
16453 Set_Freeze_Node (CW_Type, Empty);
16455 -- Customize the class-wide type: It has no prim. op., it cannot be
16456 -- abstract and its Etype points back to the specific root type.
16458 Set_Ekind (CW_Type, E_Class_Wide_Type);
16459 Set_Is_Tagged_Type (CW_Type, True);
16460 Set_Direct_Primitive_Operations (CW_Type, New_Elmt_List);
16461 Set_Is_Abstract_Type (CW_Type, False);
16462 Set_Is_Constrained (CW_Type, False);
16463 Set_Is_First_Subtype (CW_Type, Is_First_Subtype (T));
16465 if Ekind (T) = E_Class_Wide_Subtype then
16466 Set_Etype (CW_Type, Etype (Base_Type (T)));
16467 else
16468 Set_Etype (CW_Type, T);
16469 end if;
16471 -- If this is the class_wide type of a constrained subtype, it does
16472 -- not have discriminants.
16474 Set_Has_Discriminants (CW_Type,
16475 Has_Discriminants (T) and then not Is_Constrained (T));
16477 Set_Has_Unknown_Discriminants (CW_Type, True);
16478 Set_Class_Wide_Type (T, CW_Type);
16479 Set_Equivalent_Type (CW_Type, Empty);
16481 -- The class-wide type of a class-wide type is itself (RM 3.9(14))
16483 Set_Class_Wide_Type (CW_Type, CW_Type);
16484 end Make_Class_Wide_Type;
16486 ----------------
16487 -- Make_Index --
16488 ----------------
16490 procedure Make_Index
16491 (I : Node_Id;
16492 Related_Nod : Node_Id;
16493 Related_Id : Entity_Id := Empty;
16494 Suffix_Index : Nat := 1;
16495 In_Iter_Schm : Boolean := False)
16497 R : Node_Id;
16498 T : Entity_Id;
16499 Def_Id : Entity_Id := Empty;
16500 Found : Boolean := False;
16502 begin
16503 -- For a discrete range used in a constrained array definition and
16504 -- defined by a range, an implicit conversion to the predefined type
16505 -- INTEGER is assumed if each bound is either a numeric literal, a named
16506 -- number, or an attribute, and the type of both bounds (prior to the
16507 -- implicit conversion) is the type universal_integer. Otherwise, both
16508 -- bounds must be of the same discrete type, other than universal
16509 -- integer; this type must be determinable independently of the
16510 -- context, but using the fact that the type must be discrete and that
16511 -- both bounds must have the same type.
16513 -- Character literals also have a universal type in the absence of
16514 -- of additional context, and are resolved to Standard_Character.
16516 if Nkind (I) = N_Range then
16518 -- The index is given by a range constraint. The bounds are known
16519 -- to be of a consistent type.
16521 if not Is_Overloaded (I) then
16522 T := Etype (I);
16524 -- For universal bounds, choose the specific predefined type
16526 if T = Universal_Integer then
16527 T := Standard_Integer;
16529 elsif T = Any_Character then
16530 Ambiguous_Character (Low_Bound (I));
16532 T := Standard_Character;
16533 end if;
16535 -- The node may be overloaded because some user-defined operators
16536 -- are available, but if a universal interpretation exists it is
16537 -- also the selected one.
16539 elsif Universal_Interpretation (I) = Universal_Integer then
16540 T := Standard_Integer;
16542 else
16543 T := Any_Type;
16545 declare
16546 Ind : Interp_Index;
16547 It : Interp;
16549 begin
16550 Get_First_Interp (I, Ind, It);
16551 while Present (It.Typ) loop
16552 if Is_Discrete_Type (It.Typ) then
16554 if Found
16555 and then not Covers (It.Typ, T)
16556 and then not Covers (T, It.Typ)
16557 then
16558 Error_Msg_N ("ambiguous bounds in discrete range", I);
16559 exit;
16560 else
16561 T := It.Typ;
16562 Found := True;
16563 end if;
16564 end if;
16566 Get_Next_Interp (Ind, It);
16567 end loop;
16569 if T = Any_Type then
16570 Error_Msg_N ("discrete type required for range", I);
16571 Set_Etype (I, Any_Type);
16572 return;
16574 elsif T = Universal_Integer then
16575 T := Standard_Integer;
16576 end if;
16577 end;
16578 end if;
16580 if not Is_Discrete_Type (T) then
16581 Error_Msg_N ("discrete type required for range", I);
16582 Set_Etype (I, Any_Type);
16583 return;
16584 end if;
16586 if Nkind (Low_Bound (I)) = N_Attribute_Reference
16587 and then Attribute_Name (Low_Bound (I)) = Name_First
16588 and then Is_Entity_Name (Prefix (Low_Bound (I)))
16589 and then Is_Type (Entity (Prefix (Low_Bound (I))))
16590 and then Is_Discrete_Type (Entity (Prefix (Low_Bound (I))))
16591 then
16592 -- The type of the index will be the type of the prefix, as long
16593 -- as the upper bound is 'Last of the same type.
16595 Def_Id := Entity (Prefix (Low_Bound (I)));
16597 if Nkind (High_Bound (I)) /= N_Attribute_Reference
16598 or else Attribute_Name (High_Bound (I)) /= Name_Last
16599 or else not Is_Entity_Name (Prefix (High_Bound (I)))
16600 or else Entity (Prefix (High_Bound (I))) /= Def_Id
16601 then
16602 Def_Id := Empty;
16603 end if;
16604 end if;
16606 R := I;
16607 Process_Range_Expr_In_Decl (R, T, In_Iter_Schm => In_Iter_Schm);
16609 elsif Nkind (I) = N_Subtype_Indication then
16611 -- The index is given by a subtype with a range constraint
16613 T := Base_Type (Entity (Subtype_Mark (I)));
16615 if not Is_Discrete_Type (T) then
16616 Error_Msg_N ("discrete type required for range", I);
16617 Set_Etype (I, Any_Type);
16618 return;
16619 end if;
16621 R := Range_Expression (Constraint (I));
16623 Resolve (R, T);
16624 Process_Range_Expr_In_Decl
16625 (R, Entity (Subtype_Mark (I)), In_Iter_Schm => In_Iter_Schm);
16627 elsif Nkind (I) = N_Attribute_Reference then
16629 -- The parser guarantees that the attribute is a RANGE attribute
16631 -- If the node denotes the range of a type mark, that is also the
16632 -- resulting type, and we do no need to create an Itype for it.
16634 if Is_Entity_Name (Prefix (I))
16635 and then Comes_From_Source (I)
16636 and then Is_Type (Entity (Prefix (I)))
16637 and then Is_Discrete_Type (Entity (Prefix (I)))
16638 then
16639 Def_Id := Entity (Prefix (I));
16640 end if;
16642 Analyze_And_Resolve (I);
16643 T := Etype (I);
16644 R := I;
16646 -- If none of the above, must be a subtype. We convert this to a
16647 -- range attribute reference because in the case of declared first
16648 -- named subtypes, the types in the range reference can be different
16649 -- from the type of the entity. A range attribute normalizes the
16650 -- reference and obtains the correct types for the bounds.
16652 -- This transformation is in the nature of an expansion, is only
16653 -- done if expansion is active. In particular, it is not done on
16654 -- formal generic types, because we need to retain the name of the
16655 -- original index for instantiation purposes.
16657 else
16658 if not Is_Entity_Name (I) or else not Is_Type (Entity (I)) then
16659 Error_Msg_N ("invalid subtype mark in discrete range ", I);
16660 Set_Etype (I, Any_Integer);
16661 return;
16663 else
16664 -- The type mark may be that of an incomplete type. It is only
16665 -- now that we can get the full view, previous analysis does
16666 -- not look specifically for a type mark.
16668 Set_Entity (I, Get_Full_View (Entity (I)));
16669 Set_Etype (I, Entity (I));
16670 Def_Id := Entity (I);
16672 if not Is_Discrete_Type (Def_Id) then
16673 Error_Msg_N ("discrete type required for index", I);
16674 Set_Etype (I, Any_Type);
16675 return;
16676 end if;
16677 end if;
16679 if Expander_Active then
16680 Rewrite (I,
16681 Make_Attribute_Reference (Sloc (I),
16682 Attribute_Name => Name_Range,
16683 Prefix => Relocate_Node (I)));
16685 -- The original was a subtype mark that does not freeze. This
16686 -- means that the rewritten version must not freeze either.
16688 Set_Must_Not_Freeze (I);
16689 Set_Must_Not_Freeze (Prefix (I));
16691 -- Is order critical??? if so, document why, if not
16692 -- use Analyze_And_Resolve
16694 Analyze_And_Resolve (I);
16695 T := Etype (I);
16696 R := I;
16698 -- If expander is inactive, type is legal, nothing else to construct
16700 else
16701 return;
16702 end if;
16703 end if;
16705 if not Is_Discrete_Type (T) then
16706 Error_Msg_N ("discrete type required for range", I);
16707 Set_Etype (I, Any_Type);
16708 return;
16710 elsif T = Any_Type then
16711 Set_Etype (I, Any_Type);
16712 return;
16713 end if;
16715 -- We will now create the appropriate Itype to describe the range, but
16716 -- first a check. If we originally had a subtype, then we just label
16717 -- the range with this subtype. Not only is there no need to construct
16718 -- a new subtype, but it is wrong to do so for two reasons:
16720 -- 1. A legality concern, if we have a subtype, it must not freeze,
16721 -- and the Itype would cause freezing incorrectly
16723 -- 2. An efficiency concern, if we created an Itype, it would not be
16724 -- recognized as the same type for the purposes of eliminating
16725 -- checks in some circumstances.
16727 -- We signal this case by setting the subtype entity in Def_Id
16729 if No (Def_Id) then
16730 Def_Id :=
16731 Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
16732 Set_Etype (Def_Id, Base_Type (T));
16734 if Is_Signed_Integer_Type (T) then
16735 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
16737 elsif Is_Modular_Integer_Type (T) then
16738 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
16740 else
16741 Set_Ekind (Def_Id, E_Enumeration_Subtype);
16742 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
16743 Set_First_Literal (Def_Id, First_Literal (T));
16744 end if;
16746 Set_Size_Info (Def_Id, (T));
16747 Set_RM_Size (Def_Id, RM_Size (T));
16748 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
16750 Set_Scalar_Range (Def_Id, R);
16751 Conditional_Delay (Def_Id, T);
16753 -- In the subtype indication case, if the immediate parent of the
16754 -- new subtype is non-static, then the subtype we create is non-
16755 -- static, even if its bounds are static.
16757 if Nkind (I) = N_Subtype_Indication
16758 and then not Is_Static_Subtype (Entity (Subtype_Mark (I)))
16759 then
16760 Set_Is_Non_Static_Subtype (Def_Id);
16761 end if;
16762 end if;
16764 -- Final step is to label the index with this constructed type
16766 Set_Etype (I, Def_Id);
16767 end Make_Index;
16769 ------------------------------
16770 -- Modular_Type_Declaration --
16771 ------------------------------
16773 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
16774 Mod_Expr : constant Node_Id := Expression (Def);
16775 M_Val : Uint;
16777 procedure Set_Modular_Size (Bits : Int);
16778 -- Sets RM_Size to Bits, and Esize to normal word size above this
16780 ----------------------
16781 -- Set_Modular_Size --
16782 ----------------------
16784 procedure Set_Modular_Size (Bits : Int) is
16785 begin
16786 Set_RM_Size (T, UI_From_Int (Bits));
16788 if Bits <= 8 then
16789 Init_Esize (T, 8);
16791 elsif Bits <= 16 then
16792 Init_Esize (T, 16);
16794 elsif Bits <= 32 then
16795 Init_Esize (T, 32);
16797 else
16798 Init_Esize (T, System_Max_Binary_Modulus_Power);
16799 end if;
16801 if not Non_Binary_Modulus (T)
16802 and then Esize (T) = RM_Size (T)
16803 then
16804 Set_Is_Known_Valid (T);
16805 end if;
16806 end Set_Modular_Size;
16808 -- Start of processing for Modular_Type_Declaration
16810 begin
16811 -- If the mod expression is (exactly) 2 * literal, where literal is
16812 -- 64 or less,then almost certainly the * was meant to be **. Warn!
16814 if Warn_On_Suspicious_Modulus_Value
16815 and then Nkind (Mod_Expr) = N_Op_Multiply
16816 and then Nkind (Left_Opnd (Mod_Expr)) = N_Integer_Literal
16817 and then Intval (Left_Opnd (Mod_Expr)) = Uint_2
16818 and then Nkind (Right_Opnd (Mod_Expr)) = N_Integer_Literal
16819 and then Intval (Right_Opnd (Mod_Expr)) <= Uint_64
16820 then
16821 Error_Msg_N ("suspicious MOD value, was '*'* intended'??", Mod_Expr);
16822 end if;
16824 -- Proceed with analysis of mod expression
16826 Analyze_And_Resolve (Mod_Expr, Any_Integer);
16827 Set_Etype (T, T);
16828 Set_Ekind (T, E_Modular_Integer_Type);
16829 Init_Alignment (T);
16830 Set_Is_Constrained (T);
16832 if not Is_OK_Static_Expression (Mod_Expr) then
16833 Flag_Non_Static_Expr
16834 ("non-static expression used for modular type bound!", Mod_Expr);
16835 M_Val := 2 ** System_Max_Binary_Modulus_Power;
16836 else
16837 M_Val := Expr_Value (Mod_Expr);
16838 end if;
16840 if M_Val < 1 then
16841 Error_Msg_N ("modulus value must be positive", Mod_Expr);
16842 M_Val := 2 ** System_Max_Binary_Modulus_Power;
16843 end if;
16845 Set_Modulus (T, M_Val);
16847 -- Create bounds for the modular type based on the modulus given in
16848 -- the type declaration and then analyze and resolve those bounds.
16850 Set_Scalar_Range (T,
16851 Make_Range (Sloc (Mod_Expr),
16852 Low_Bound => Make_Integer_Literal (Sloc (Mod_Expr), 0),
16853 High_Bound => Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
16855 -- Properly analyze the literals for the range. We do this manually
16856 -- because we can't go calling Resolve, since we are resolving these
16857 -- bounds with the type, and this type is certainly not complete yet!
16859 Set_Etype (Low_Bound (Scalar_Range (T)), T);
16860 Set_Etype (High_Bound (Scalar_Range (T)), T);
16861 Set_Is_Static_Expression (Low_Bound (Scalar_Range (T)));
16862 Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
16864 -- Loop through powers of two to find number of bits required
16866 for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
16868 -- Binary case
16870 if M_Val = 2 ** Bits then
16871 Set_Modular_Size (Bits);
16872 return;
16874 -- Non-binary case
16876 elsif M_Val < 2 ** Bits then
16877 Check_SPARK_Restriction ("modulus should be a power of 2", T);
16878 Set_Non_Binary_Modulus (T);
16880 if Bits > System_Max_Nonbinary_Modulus_Power then
16881 Error_Msg_Uint_1 :=
16882 UI_From_Int (System_Max_Nonbinary_Modulus_Power);
16883 Error_Msg_F
16884 ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
16885 Set_Modular_Size (System_Max_Binary_Modulus_Power);
16886 return;
16888 else
16889 -- In the non-binary case, set size as per RM 13.3(55)
16891 Set_Modular_Size (Bits);
16892 return;
16893 end if;
16894 end if;
16896 end loop;
16898 -- If we fall through, then the size exceed System.Max_Binary_Modulus
16899 -- so we just signal an error and set the maximum size.
16901 Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
16902 Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
16904 Set_Modular_Size (System_Max_Binary_Modulus_Power);
16905 Init_Alignment (T);
16907 end Modular_Type_Declaration;
16909 --------------------------
16910 -- New_Concatenation_Op --
16911 --------------------------
16913 procedure New_Concatenation_Op (Typ : Entity_Id) is
16914 Loc : constant Source_Ptr := Sloc (Typ);
16915 Op : Entity_Id;
16917 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
16918 -- Create abbreviated declaration for the formal of a predefined
16919 -- Operator 'Op' of type 'Typ'
16921 --------------------
16922 -- Make_Op_Formal --
16923 --------------------
16925 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
16926 Formal : Entity_Id;
16927 begin
16928 Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
16929 Set_Etype (Formal, Typ);
16930 Set_Mechanism (Formal, Default_Mechanism);
16931 return Formal;
16932 end Make_Op_Formal;
16934 -- Start of processing for New_Concatenation_Op
16936 begin
16937 Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
16939 Set_Ekind (Op, E_Operator);
16940 Set_Scope (Op, Current_Scope);
16941 Set_Etype (Op, Typ);
16942 Set_Homonym (Op, Get_Name_Entity_Id (Name_Op_Concat));
16943 Set_Is_Immediately_Visible (Op);
16944 Set_Is_Intrinsic_Subprogram (Op);
16945 Set_Has_Completion (Op);
16946 Append_Entity (Op, Current_Scope);
16948 Set_Name_Entity_Id (Name_Op_Concat, Op);
16950 Append_Entity (Make_Op_Formal (Typ, Op), Op);
16951 Append_Entity (Make_Op_Formal (Typ, Op), Op);
16952 end New_Concatenation_Op;
16954 -------------------------
16955 -- OK_For_Limited_Init --
16956 -------------------------
16958 -- ???Check all calls of this, and compare the conditions under which it's
16959 -- called.
16961 function OK_For_Limited_Init
16962 (Typ : Entity_Id;
16963 Exp : Node_Id) return Boolean
16965 begin
16966 return Is_CPP_Constructor_Call (Exp)
16967 or else (Ada_Version >= Ada_2005
16968 and then not Debug_Flag_Dot_L
16969 and then OK_For_Limited_Init_In_05 (Typ, Exp));
16970 end OK_For_Limited_Init;
16972 -------------------------------
16973 -- OK_For_Limited_Init_In_05 --
16974 -------------------------------
16976 function OK_For_Limited_Init_In_05
16977 (Typ : Entity_Id;
16978 Exp : Node_Id) return Boolean
16980 begin
16981 -- An object of a limited interface type can be initialized with any
16982 -- expression of a nonlimited descendant type.
16984 if Is_Class_Wide_Type (Typ)
16985 and then Is_Limited_Interface (Typ)
16986 and then not Is_Limited_Type (Etype (Exp))
16987 then
16988 return True;
16989 end if;
16991 -- Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
16992 -- case of limited aggregates (including extension aggregates), and
16993 -- function calls. The function call may have been given in prefixed
16994 -- notation, in which case the original node is an indexed component.
16995 -- If the function is parameterless, the original node was an explicit
16996 -- dereference. The function may also be parameterless, in which case
16997 -- the source node is just an identifier.
16999 case Nkind (Original_Node (Exp)) is
17000 when N_Aggregate | N_Extension_Aggregate | N_Function_Call | N_Op =>
17001 return True;
17003 when N_Identifier =>
17004 return Present (Entity (Original_Node (Exp)))
17005 and then Ekind (Entity (Original_Node (Exp))) = E_Function;
17007 when N_Qualified_Expression =>
17008 return
17009 OK_For_Limited_Init_In_05
17010 (Typ, Expression (Original_Node (Exp)));
17012 -- Ada 2005 (AI-251): If a class-wide interface object is initialized
17013 -- with a function call, the expander has rewritten the call into an
17014 -- N_Type_Conversion node to force displacement of the pointer to
17015 -- reference the component containing the secondary dispatch table.
17016 -- Otherwise a type conversion is not a legal context.
17017 -- A return statement for a build-in-place function returning a
17018 -- synchronized type also introduces an unchecked conversion.
17020 when N_Type_Conversion |
17021 N_Unchecked_Type_Conversion =>
17022 return not Comes_From_Source (Exp)
17023 and then
17024 OK_For_Limited_Init_In_05
17025 (Typ, Expression (Original_Node (Exp)));
17027 when N_Indexed_Component |
17028 N_Selected_Component |
17029 N_Explicit_Dereference =>
17030 return Nkind (Exp) = N_Function_Call;
17032 -- A use of 'Input is a function call, hence allowed. Normally the
17033 -- attribute will be changed to a call, but the attribute by itself
17034 -- can occur with -gnatc.
17036 when N_Attribute_Reference =>
17037 return Attribute_Name (Original_Node (Exp)) = Name_Input;
17039 -- For a conditional expression, all dependent expressions must be
17040 -- legal constructs.
17042 when N_Conditional_Expression =>
17043 declare
17044 Then_Expr : constant Node_Id :=
17045 Next (First (Expressions (Original_Node (Exp))));
17046 Else_Expr : constant Node_Id := Next (Then_Expr);
17047 begin
17048 return OK_For_Limited_Init_In_05 (Typ, Then_Expr)
17049 and then OK_For_Limited_Init_In_05 (Typ, Else_Expr);
17050 end;
17052 when N_Case_Expression =>
17053 declare
17054 Alt : Node_Id;
17056 begin
17057 Alt := First (Alternatives (Original_Node (Exp)));
17058 while Present (Alt) loop
17059 if not OK_For_Limited_Init_In_05 (Typ, Expression (Alt)) then
17060 return False;
17061 end if;
17063 Next (Alt);
17064 end loop;
17066 return True;
17067 end;
17069 when others =>
17070 return False;
17071 end case;
17072 end OK_For_Limited_Init_In_05;
17074 -------------------------------------------
17075 -- Ordinary_Fixed_Point_Type_Declaration --
17076 -------------------------------------------
17078 procedure Ordinary_Fixed_Point_Type_Declaration
17079 (T : Entity_Id;
17080 Def : Node_Id)
17082 Loc : constant Source_Ptr := Sloc (Def);
17083 Delta_Expr : constant Node_Id := Delta_Expression (Def);
17084 RRS : constant Node_Id := Real_Range_Specification (Def);
17085 Implicit_Base : Entity_Id;
17086 Delta_Val : Ureal;
17087 Small_Val : Ureal;
17088 Low_Val : Ureal;
17089 High_Val : Ureal;
17091 begin
17092 Check_Restriction (No_Fixed_Point, Def);
17094 -- Create implicit base type
17096 Implicit_Base :=
17097 Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
17098 Set_Etype (Implicit_Base, Implicit_Base);
17100 -- Analyze and process delta expression
17102 Analyze_And_Resolve (Delta_Expr, Any_Real);
17104 Check_Delta_Expression (Delta_Expr);
17105 Delta_Val := Expr_Value_R (Delta_Expr);
17107 Set_Delta_Value (Implicit_Base, Delta_Val);
17109 -- Compute default small from given delta, which is the largest power
17110 -- of two that does not exceed the given delta value.
17112 declare
17113 Tmp : Ureal;
17114 Scale : Int;
17116 begin
17117 Tmp := Ureal_1;
17118 Scale := 0;
17120 if Delta_Val < Ureal_1 then
17121 while Delta_Val < Tmp loop
17122 Tmp := Tmp / Ureal_2;
17123 Scale := Scale + 1;
17124 end loop;
17126 else
17127 loop
17128 Tmp := Tmp * Ureal_2;
17129 exit when Tmp > Delta_Val;
17130 Scale := Scale - 1;
17131 end loop;
17132 end if;
17134 Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
17135 end;
17137 Set_Small_Value (Implicit_Base, Small_Val);
17139 -- If no range was given, set a dummy range
17141 if RRS <= Empty_Or_Error then
17142 Low_Val := -Small_Val;
17143 High_Val := Small_Val;
17145 -- Otherwise analyze and process given range
17147 else
17148 declare
17149 Low : constant Node_Id := Low_Bound (RRS);
17150 High : constant Node_Id := High_Bound (RRS);
17152 begin
17153 Analyze_And_Resolve (Low, Any_Real);
17154 Analyze_And_Resolve (High, Any_Real);
17155 Check_Real_Bound (Low);
17156 Check_Real_Bound (High);
17158 -- Obtain and set the range
17160 Low_Val := Expr_Value_R (Low);
17161 High_Val := Expr_Value_R (High);
17163 if Low_Val > High_Val then
17164 Error_Msg_NE ("?fixed point type& has null range", Def, T);
17165 end if;
17166 end;
17167 end if;
17169 -- The range for both the implicit base and the declared first subtype
17170 -- cannot be set yet, so we use the special routine Set_Fixed_Range to
17171 -- set a temporary range in place. Note that the bounds of the base
17172 -- type will be widened to be symmetrical and to fill the available
17173 -- bits when the type is frozen.
17175 -- We could do this with all discrete types, and probably should, but
17176 -- we absolutely have to do it for fixed-point, since the end-points
17177 -- of the range and the size are determined by the small value, which
17178 -- could be reset before the freeze point.
17180 Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
17181 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
17183 -- Complete definition of first subtype
17185 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
17186 Set_Etype (T, Implicit_Base);
17187 Init_Size_Align (T);
17188 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
17189 Set_Small_Value (T, Small_Val);
17190 Set_Delta_Value (T, Delta_Val);
17191 Set_Is_Constrained (T);
17193 end Ordinary_Fixed_Point_Type_Declaration;
17195 ----------------------------------------
17196 -- Prepare_Private_Subtype_Completion --
17197 ----------------------------------------
17199 procedure Prepare_Private_Subtype_Completion
17200 (Id : Entity_Id;
17201 Related_Nod : Node_Id)
17203 Id_B : constant Entity_Id := Base_Type (Id);
17204 Full_B : constant Entity_Id := Full_View (Id_B);
17205 Full : Entity_Id;
17207 begin
17208 if Present (Full_B) then
17210 -- The Base_Type is already completed, we can complete the subtype
17211 -- now. We have to create a new entity with the same name, Thus we
17212 -- can't use Create_Itype.
17214 -- This is messy, should be fixed ???
17216 Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
17217 Set_Is_Itype (Full);
17218 Set_Associated_Node_For_Itype (Full, Related_Nod);
17219 Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
17220 end if;
17222 -- The parent subtype may be private, but the base might not, in some
17223 -- nested instances. In that case, the subtype does not need to be
17224 -- exchanged. It would still be nice to make private subtypes and their
17225 -- bases consistent at all times ???
17227 if Is_Private_Type (Id_B) then
17228 Append_Elmt (Id, Private_Dependents (Id_B));
17229 end if;
17231 end Prepare_Private_Subtype_Completion;
17233 ---------------------------
17234 -- Process_Discriminants --
17235 ---------------------------
17237 procedure Process_Discriminants
17238 (N : Node_Id;
17239 Prev : Entity_Id := Empty)
17241 Elist : constant Elist_Id := New_Elmt_List;
17242 Id : Node_Id;
17243 Discr : Node_Id;
17244 Discr_Number : Uint;
17245 Discr_Type : Entity_Id;
17246 Default_Present : Boolean := False;
17247 Default_Not_Present : Boolean := False;
17249 begin
17250 -- A composite type other than an array type can have discriminants.
17251 -- On entry, the current scope is the composite type.
17253 -- The discriminants are initially entered into the scope of the type
17254 -- via Enter_Name with the default Ekind of E_Void to prevent premature
17255 -- use, as explained at the end of this procedure.
17257 Discr := First (Discriminant_Specifications (N));
17258 while Present (Discr) loop
17259 Enter_Name (Defining_Identifier (Discr));
17261 -- For navigation purposes we add a reference to the discriminant
17262 -- in the entity for the type. If the current declaration is a
17263 -- completion, place references on the partial view. Otherwise the
17264 -- type is the current scope.
17266 if Present (Prev) then
17268 -- The references go on the partial view, if present. If the
17269 -- partial view has discriminants, the references have been
17270 -- generated already.
17272 if not Has_Discriminants (Prev) then
17273 Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
17274 end if;
17275 else
17276 Generate_Reference
17277 (Current_Scope, Defining_Identifier (Discr), 'd');
17278 end if;
17280 if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
17281 Discr_Type := Access_Definition (Discr, Discriminant_Type (Discr));
17283 -- Ada 2005 (AI-254)
17285 if Present (Access_To_Subprogram_Definition
17286 (Discriminant_Type (Discr)))
17287 and then Protected_Present (Access_To_Subprogram_Definition
17288 (Discriminant_Type (Discr)))
17289 then
17290 Discr_Type :=
17291 Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
17292 end if;
17294 else
17295 Find_Type (Discriminant_Type (Discr));
17296 Discr_Type := Etype (Discriminant_Type (Discr));
17298 if Error_Posted (Discriminant_Type (Discr)) then
17299 Discr_Type := Any_Type;
17300 end if;
17301 end if;
17303 if Is_Access_Type (Discr_Type) then
17305 -- Ada 2005 (AI-230): Access discriminant allowed in non-limited
17306 -- record types
17308 if Ada_Version < Ada_2005 then
17309 Check_Access_Discriminant_Requires_Limited
17310 (Discr, Discriminant_Type (Discr));
17311 end if;
17313 if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
17314 Error_Msg_N
17315 ("(Ada 83) access discriminant not allowed", Discr);
17316 end if;
17318 elsif not Is_Discrete_Type (Discr_Type) then
17319 Error_Msg_N ("discriminants must have a discrete or access type",
17320 Discriminant_Type (Discr));
17321 end if;
17323 Set_Etype (Defining_Identifier (Discr), Discr_Type);
17325 -- If a discriminant specification includes the assignment compound
17326 -- delimiter followed by an expression, the expression is the default
17327 -- expression of the discriminant; the default expression must be of
17328 -- the type of the discriminant. (RM 3.7.1) Since this expression is
17329 -- a default expression, we do the special preanalysis, since this
17330 -- expression does not freeze (see "Handling of Default and Per-
17331 -- Object Expressions" in spec of package Sem).
17333 if Present (Expression (Discr)) then
17334 Preanalyze_Spec_Expression (Expression (Discr), Discr_Type);
17336 if Nkind (N) = N_Formal_Type_Declaration then
17337 Error_Msg_N
17338 ("discriminant defaults not allowed for formal type",
17339 Expression (Discr));
17341 -- Flag an error for a tagged type with defaulted discriminants,
17342 -- excluding limited tagged types when compiling for Ada 2012
17343 -- (see AI05-0214).
17345 elsif Is_Tagged_Type (Current_Scope)
17346 and then (not Is_Limited_Type (Current_Scope)
17347 or else Ada_Version < Ada_2012)
17348 and then Comes_From_Source (N)
17349 then
17350 -- Note: see similar test in Check_Or_Process_Discriminants, to
17351 -- handle the (illegal) case of the completion of an untagged
17352 -- view with discriminants with defaults by a tagged full view.
17353 -- We skip the check if Discr does not come from source, to
17354 -- account for the case of an untagged derived type providing
17355 -- defaults for a renamed discriminant from a private untagged
17356 -- ancestor with a tagged full view (ACATS B460006).
17358 if Ada_Version >= Ada_2012 then
17359 Error_Msg_N
17360 ("discriminants of nonlimited tagged type cannot have"
17361 & " defaults",
17362 Expression (Discr));
17363 else
17364 Error_Msg_N
17365 ("discriminants of tagged type cannot have defaults",
17366 Expression (Discr));
17367 end if;
17369 else
17370 Default_Present := True;
17371 Append_Elmt (Expression (Discr), Elist);
17373 -- Tag the defining identifiers for the discriminants with
17374 -- their corresponding default expressions from the tree.
17376 Set_Discriminant_Default_Value
17377 (Defining_Identifier (Discr), Expression (Discr));
17378 end if;
17380 else
17381 Default_Not_Present := True;
17382 end if;
17384 -- Ada 2005 (AI-231): Create an Itype that is a duplicate of
17385 -- Discr_Type but with the null-exclusion attribute
17387 if Ada_Version >= Ada_2005 then
17389 -- Ada 2005 (AI-231): Static checks
17391 if Can_Never_Be_Null (Discr_Type) then
17392 Null_Exclusion_Static_Checks (Discr);
17394 elsif Is_Access_Type (Discr_Type)
17395 and then Null_Exclusion_Present (Discr)
17397 -- No need to check itypes because in their case this check
17398 -- was done at their point of creation
17400 and then not Is_Itype (Discr_Type)
17401 then
17402 if Can_Never_Be_Null (Discr_Type) then
17403 Error_Msg_NE
17404 ("`NOT NULL` not allowed (& already excludes null)",
17405 Discr,
17406 Discr_Type);
17407 end if;
17409 Set_Etype (Defining_Identifier (Discr),
17410 Create_Null_Excluding_Itype
17411 (T => Discr_Type,
17412 Related_Nod => Discr));
17414 -- Check for improper null exclusion if the type is otherwise
17415 -- legal for a discriminant.
17417 elsif Null_Exclusion_Present (Discr)
17418 and then Is_Discrete_Type (Discr_Type)
17419 then
17420 Error_Msg_N
17421 ("null exclusion can only apply to an access type", Discr);
17422 end if;
17424 -- Ada 2005 (AI-402): access discriminants of nonlimited types
17425 -- can't have defaults. Synchronized types, or types that are
17426 -- explicitly limited are fine, but special tests apply to derived
17427 -- types in generics: in a generic body we have to assume the
17428 -- worst, and therefore defaults are not allowed if the parent is
17429 -- a generic formal private type (see ACATS B370001).
17431 if Is_Access_Type (Discr_Type) and then Default_Present then
17432 if Ekind (Discr_Type) /= E_Anonymous_Access_Type
17433 or else Is_Limited_Record (Current_Scope)
17434 or else Is_Concurrent_Type (Current_Scope)
17435 or else Is_Concurrent_Record_Type (Current_Scope)
17436 or else Ekind (Current_Scope) = E_Limited_Private_Type
17437 then
17438 if not Is_Derived_Type (Current_Scope)
17439 or else not Is_Generic_Type (Etype (Current_Scope))
17440 or else not In_Package_Body (Scope (Etype (Current_Scope)))
17441 or else Limited_Present
17442 (Type_Definition (Parent (Current_Scope)))
17443 then
17444 null;
17446 else
17447 Error_Msg_N ("access discriminants of nonlimited types",
17448 Expression (Discr));
17449 Error_Msg_N ("\cannot have defaults", Expression (Discr));
17450 end if;
17452 elsif Present (Expression (Discr)) then
17453 Error_Msg_N
17454 ("(Ada 2005) access discriminants of nonlimited types",
17455 Expression (Discr));
17456 Error_Msg_N ("\cannot have defaults", Expression (Discr));
17457 end if;
17458 end if;
17459 end if;
17461 Next (Discr);
17462 end loop;
17464 -- An element list consisting of the default expressions of the
17465 -- discriminants is constructed in the above loop and used to set
17466 -- the Discriminant_Constraint attribute for the type. If an object
17467 -- is declared of this (record or task) type without any explicit
17468 -- discriminant constraint given, this element list will form the
17469 -- actual parameters for the corresponding initialization procedure
17470 -- for the type.
17472 Set_Discriminant_Constraint (Current_Scope, Elist);
17473 Set_Stored_Constraint (Current_Scope, No_Elist);
17475 -- Default expressions must be provided either for all or for none
17476 -- of the discriminants of a discriminant part. (RM 3.7.1)
17478 if Default_Present and then Default_Not_Present then
17479 Error_Msg_N
17480 ("incomplete specification of defaults for discriminants", N);
17481 end if;
17483 -- The use of the name of a discriminant is not allowed in default
17484 -- expressions of a discriminant part if the specification of the
17485 -- discriminant is itself given in the discriminant part. (RM 3.7.1)
17487 -- To detect this, the discriminant names are entered initially with an
17488 -- Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
17489 -- attempt to use a void entity (for example in an expression that is
17490 -- type-checked) produces the error message: premature usage. Now after
17491 -- completing the semantic analysis of the discriminant part, we can set
17492 -- the Ekind of all the discriminants appropriately.
17494 Discr := First (Discriminant_Specifications (N));
17495 Discr_Number := Uint_1;
17496 while Present (Discr) loop
17497 Id := Defining_Identifier (Discr);
17498 Set_Ekind (Id, E_Discriminant);
17499 Init_Component_Location (Id);
17500 Init_Esize (Id);
17501 Set_Discriminant_Number (Id, Discr_Number);
17503 -- Make sure this is always set, even in illegal programs
17505 Set_Corresponding_Discriminant (Id, Empty);
17507 -- Initialize the Original_Record_Component to the entity itself.
17508 -- Inherit_Components will propagate the right value to
17509 -- discriminants in derived record types.
17511 Set_Original_Record_Component (Id, Id);
17513 -- Create the discriminal for the discriminant
17515 Build_Discriminal (Id);
17517 Next (Discr);
17518 Discr_Number := Discr_Number + 1;
17519 end loop;
17521 Set_Has_Discriminants (Current_Scope);
17522 end Process_Discriminants;
17524 -----------------------
17525 -- Process_Full_View --
17526 -----------------------
17528 procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
17529 Priv_Parent : Entity_Id;
17530 Full_Parent : Entity_Id;
17531 Full_Indic : Node_Id;
17533 procedure Collect_Implemented_Interfaces
17534 (Typ : Entity_Id;
17535 Ifaces : Elist_Id);
17536 -- Ada 2005: Gather all the interfaces that Typ directly or
17537 -- inherently implements. Duplicate entries are not added to
17538 -- the list Ifaces.
17540 ------------------------------------
17541 -- Collect_Implemented_Interfaces --
17542 ------------------------------------
17544 procedure Collect_Implemented_Interfaces
17545 (Typ : Entity_Id;
17546 Ifaces : Elist_Id)
17548 Iface : Entity_Id;
17549 Iface_Elmt : Elmt_Id;
17551 begin
17552 -- Abstract interfaces are only associated with tagged record types
17554 if not Is_Tagged_Type (Typ)
17555 or else not Is_Record_Type (Typ)
17556 then
17557 return;
17558 end if;
17560 -- Recursively climb to the ancestors
17562 if Etype (Typ) /= Typ
17564 -- Protect the frontend against wrong cyclic declarations like:
17566 -- type B is new A with private;
17567 -- type C is new A with private;
17568 -- private
17569 -- type B is new C with null record;
17570 -- type C is new B with null record;
17572 and then Etype (Typ) /= Priv_T
17573 and then Etype (Typ) /= Full_T
17574 then
17575 -- Keep separate the management of private type declarations
17577 if Ekind (Typ) = E_Record_Type_With_Private then
17579 -- Handle the following erroneous case:
17580 -- type Private_Type is tagged private;
17581 -- private
17582 -- type Private_Type is new Type_Implementing_Iface;
17584 if Present (Full_View (Typ))
17585 and then Etype (Typ) /= Full_View (Typ)
17586 then
17587 if Is_Interface (Etype (Typ)) then
17588 Append_Unique_Elmt (Etype (Typ), Ifaces);
17589 end if;
17591 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
17592 end if;
17594 -- Non-private types
17596 else
17597 if Is_Interface (Etype (Typ)) then
17598 Append_Unique_Elmt (Etype (Typ), Ifaces);
17599 end if;
17601 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
17602 end if;
17603 end if;
17605 -- Handle entities in the list of abstract interfaces
17607 if Present (Interfaces (Typ)) then
17608 Iface_Elmt := First_Elmt (Interfaces (Typ));
17609 while Present (Iface_Elmt) loop
17610 Iface := Node (Iface_Elmt);
17612 pragma Assert (Is_Interface (Iface));
17614 if not Contain_Interface (Iface, Ifaces) then
17615 Append_Elmt (Iface, Ifaces);
17616 Collect_Implemented_Interfaces (Iface, Ifaces);
17617 end if;
17619 Next_Elmt (Iface_Elmt);
17620 end loop;
17621 end if;
17622 end Collect_Implemented_Interfaces;
17624 -- Start of processing for Process_Full_View
17626 begin
17627 -- First some sanity checks that must be done after semantic
17628 -- decoration of the full view and thus cannot be placed with other
17629 -- similar checks in Find_Type_Name
17631 if not Is_Limited_Type (Priv_T)
17632 and then (Is_Limited_Type (Full_T)
17633 or else Is_Limited_Composite (Full_T))
17634 then
17635 if In_Instance then
17636 null;
17637 else
17638 Error_Msg_N
17639 ("completion of nonlimited type cannot be limited", Full_T);
17640 Explain_Limited_Type (Full_T, Full_T);
17641 end if;
17643 elsif Is_Abstract_Type (Full_T)
17644 and then not Is_Abstract_Type (Priv_T)
17645 then
17646 Error_Msg_N
17647 ("completion of nonabstract type cannot be abstract", Full_T);
17649 elsif Is_Tagged_Type (Priv_T)
17650 and then Is_Limited_Type (Priv_T)
17651 and then not Is_Limited_Type (Full_T)
17652 then
17653 -- If pragma CPP_Class was applied to the private declaration
17654 -- propagate the limitedness to the full-view
17656 if Is_CPP_Class (Priv_T) then
17657 Set_Is_Limited_Record (Full_T);
17659 -- GNAT allow its own definition of Limited_Controlled to disobey
17660 -- this rule in order in ease the implementation. This test is safe
17661 -- because Root_Controlled is defined in a child of System that
17662 -- normal programs are not supposed to use.
17664 elsif Is_RTE (Etype (Full_T), RE_Root_Controlled) then
17665 Set_Is_Limited_Composite (Full_T);
17666 else
17667 Error_Msg_N
17668 ("completion of limited tagged type must be limited", Full_T);
17669 end if;
17671 elsif Is_Generic_Type (Priv_T) then
17672 Error_Msg_N ("generic type cannot have a completion", Full_T);
17673 end if;
17675 -- Check that ancestor interfaces of private and full views are
17676 -- consistent. We omit this check for synchronized types because
17677 -- they are performed on the corresponding record type when frozen.
17679 if Ada_Version >= Ada_2005
17680 and then Is_Tagged_Type (Priv_T)
17681 and then Is_Tagged_Type (Full_T)
17682 and then not Is_Concurrent_Type (Full_T)
17683 then
17684 declare
17685 Iface : Entity_Id;
17686 Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
17687 Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
17689 begin
17690 Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
17691 Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
17693 -- Ada 2005 (AI-251): The partial view shall be a descendant of
17694 -- an interface type if and only if the full type is descendant
17695 -- of the interface type (AARM 7.3 (7.3/2)).
17697 Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
17699 if Present (Iface) then
17700 Error_Msg_NE
17701 ("interface & not implemented by full type " &
17702 "(RM-2005 7.3 (7.3/2))", Priv_T, Iface);
17703 end if;
17705 Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
17707 if Present (Iface) then
17708 Error_Msg_NE
17709 ("interface & not implemented by partial view " &
17710 "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
17711 end if;
17712 end;
17713 end if;
17715 if Is_Tagged_Type (Priv_T)
17716 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
17717 and then Is_Derived_Type (Full_T)
17718 then
17719 Priv_Parent := Etype (Priv_T);
17721 -- The full view of a private extension may have been transformed
17722 -- into an unconstrained derived type declaration and a subtype
17723 -- declaration (see build_derived_record_type for details).
17725 if Nkind (N) = N_Subtype_Declaration then
17726 Full_Indic := Subtype_Indication (N);
17727 Full_Parent := Etype (Base_Type (Full_T));
17728 else
17729 Full_Indic := Subtype_Indication (Type_Definition (N));
17730 Full_Parent := Etype (Full_T);
17731 end if;
17733 -- Check that the parent type of the full type is a descendant of
17734 -- the ancestor subtype given in the private extension. If either
17735 -- entity has an Etype equal to Any_Type then we had some previous
17736 -- error situation [7.3(8)].
17738 if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
17739 return;
17741 -- Ada 2005 (AI-251): Interfaces in the full-typ can be given in
17742 -- any order. Therefore we don't have to check that its parent must
17743 -- be a descendant of the parent of the private type declaration.
17745 elsif Is_Interface (Priv_Parent)
17746 and then Is_Interface (Full_Parent)
17747 then
17748 null;
17750 -- Ada 2005 (AI-251): If the parent of the private type declaration
17751 -- is an interface there is no need to check that it is an ancestor
17752 -- of the associated full type declaration. The required tests for
17753 -- this case are performed by Build_Derived_Record_Type.
17755 elsif not Is_Interface (Base_Type (Priv_Parent))
17756 and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
17757 then
17758 Error_Msg_N
17759 ("parent of full type must descend from parent"
17760 & " of private extension", Full_Indic);
17762 -- First check a formal restriction, and then proceed with checking
17763 -- Ada rules. Since the formal restriction is not a serious error, we
17764 -- don't prevent further error detection for this check, hence the
17765 -- ELSE.
17767 else
17769 -- In formal mode, when completing a private extension the type
17770 -- named in the private part must be exactly the same as that
17771 -- named in the visible part.
17773 if Priv_Parent /= Full_Parent then
17774 Error_Msg_Name_1 := Chars (Priv_Parent);
17775 Check_SPARK_Restriction ("% expected", Full_Indic);
17776 end if;
17778 -- Check the rules of 7.3(10): if the private extension inherits
17779 -- known discriminants, then the full type must also inherit those
17780 -- discriminants from the same (ancestor) type, and the parent
17781 -- subtype of the full type must be constrained if and only if
17782 -- the ancestor subtype of the private extension is constrained.
17784 if No (Discriminant_Specifications (Parent (Priv_T)))
17785 and then not Has_Unknown_Discriminants (Priv_T)
17786 and then Has_Discriminants (Base_Type (Priv_Parent))
17787 then
17788 declare
17789 Priv_Indic : constant Node_Id :=
17790 Subtype_Indication (Parent (Priv_T));
17792 Priv_Constr : constant Boolean :=
17793 Is_Constrained (Priv_Parent)
17794 or else
17795 Nkind (Priv_Indic) = N_Subtype_Indication
17796 or else
17797 Is_Constrained (Entity (Priv_Indic));
17799 Full_Constr : constant Boolean :=
17800 Is_Constrained (Full_Parent)
17801 or else
17802 Nkind (Full_Indic) = N_Subtype_Indication
17803 or else
17804 Is_Constrained (Entity (Full_Indic));
17806 Priv_Discr : Entity_Id;
17807 Full_Discr : Entity_Id;
17809 begin
17810 Priv_Discr := First_Discriminant (Priv_Parent);
17811 Full_Discr := First_Discriminant (Full_Parent);
17812 while Present (Priv_Discr) and then Present (Full_Discr) loop
17813 if Original_Record_Component (Priv_Discr) =
17814 Original_Record_Component (Full_Discr)
17815 or else
17816 Corresponding_Discriminant (Priv_Discr) =
17817 Corresponding_Discriminant (Full_Discr)
17818 then
17819 null;
17820 else
17821 exit;
17822 end if;
17824 Next_Discriminant (Priv_Discr);
17825 Next_Discriminant (Full_Discr);
17826 end loop;
17828 if Present (Priv_Discr) or else Present (Full_Discr) then
17829 Error_Msg_N
17830 ("full view must inherit discriminants of the parent"
17831 & " type used in the private extension", Full_Indic);
17833 elsif Priv_Constr and then not Full_Constr then
17834 Error_Msg_N
17835 ("parent subtype of full type must be constrained",
17836 Full_Indic);
17838 elsif Full_Constr and then not Priv_Constr then
17839 Error_Msg_N
17840 ("parent subtype of full type must be unconstrained",
17841 Full_Indic);
17842 end if;
17843 end;
17845 -- Check the rules of 7.3(12): if a partial view has neither
17846 -- known or unknown discriminants, then the full type
17847 -- declaration shall define a definite subtype.
17849 elsif not Has_Unknown_Discriminants (Priv_T)
17850 and then not Has_Discriminants (Priv_T)
17851 and then not Is_Constrained (Full_T)
17852 then
17853 Error_Msg_N
17854 ("full view must define a constrained type if partial view"
17855 & " has no discriminants", Full_T);
17856 end if;
17858 -- ??????? Do we implement the following properly ?????
17859 -- If the ancestor subtype of a private extension has constrained
17860 -- discriminants, then the parent subtype of the full view shall
17861 -- impose a statically matching constraint on those discriminants
17862 -- [7.3(13)].
17863 end if;
17865 else
17866 -- For untagged types, verify that a type without discriminants
17867 -- is not completed with an unconstrained type.
17869 if not Is_Indefinite_Subtype (Priv_T)
17870 and then Is_Indefinite_Subtype (Full_T)
17871 then
17872 Error_Msg_N ("full view of type must be definite subtype", Full_T);
17873 end if;
17874 end if;
17876 -- AI-419: verify that the use of "limited" is consistent
17878 declare
17879 Orig_Decl : constant Node_Id := Original_Node (N);
17881 begin
17882 if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
17883 and then not Limited_Present (Parent (Priv_T))
17884 and then not Synchronized_Present (Parent (Priv_T))
17885 and then Nkind (Orig_Decl) = N_Full_Type_Declaration
17886 and then Nkind
17887 (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
17888 and then Limited_Present (Type_Definition (Orig_Decl))
17889 then
17890 Error_Msg_N
17891 ("full view of non-limited extension cannot be limited", N);
17892 end if;
17893 end;
17895 -- Ada 2005 (AI-443): A synchronized private extension must be
17896 -- completed by a task or protected type.
17898 if Ada_Version >= Ada_2005
17899 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
17900 and then Synchronized_Present (Parent (Priv_T))
17901 and then not Is_Concurrent_Type (Full_T)
17902 then
17903 Error_Msg_N ("full view of synchronized extension must " &
17904 "be synchronized type", N);
17905 end if;
17907 -- Ada 2005 AI-363: if the full view has discriminants with
17908 -- defaults, it is illegal to declare constrained access subtypes
17909 -- whose designated type is the current type. This allows objects
17910 -- of the type that are declared in the heap to be unconstrained.
17912 if not Has_Unknown_Discriminants (Priv_T)
17913 and then not Has_Discriminants (Priv_T)
17914 and then Has_Discriminants (Full_T)
17915 and then
17916 Present (Discriminant_Default_Value (First_Discriminant (Full_T)))
17917 then
17918 Set_Has_Constrained_Partial_View (Full_T);
17919 Set_Has_Constrained_Partial_View (Priv_T);
17920 end if;
17922 -- Create a full declaration for all its subtypes recorded in
17923 -- Private_Dependents and swap them similarly to the base type. These
17924 -- are subtypes that have been define before the full declaration of
17925 -- the private type. We also swap the entry in Private_Dependents list
17926 -- so we can properly restore the private view on exit from the scope.
17928 declare
17929 Priv_Elmt : Elmt_Id;
17930 Priv : Entity_Id;
17931 Full : Entity_Id;
17933 begin
17934 Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
17935 while Present (Priv_Elmt) loop
17936 Priv := Node (Priv_Elmt);
17938 if Ekind_In (Priv, E_Private_Subtype,
17939 E_Limited_Private_Subtype,
17940 E_Record_Subtype_With_Private)
17941 then
17942 Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
17943 Set_Is_Itype (Full);
17944 Set_Parent (Full, Parent (Priv));
17945 Set_Associated_Node_For_Itype (Full, N);
17947 -- Now we need to complete the private subtype, but since the
17948 -- base type has already been swapped, we must also swap the
17949 -- subtypes (and thus, reverse the arguments in the call to
17950 -- Complete_Private_Subtype).
17952 Copy_And_Swap (Priv, Full);
17953 Complete_Private_Subtype (Full, Priv, Full_T, N);
17954 Replace_Elmt (Priv_Elmt, Full);
17955 end if;
17957 Next_Elmt (Priv_Elmt);
17958 end loop;
17959 end;
17961 -- If the private view was tagged, copy the new primitive operations
17962 -- from the private view to the full view.
17964 if Is_Tagged_Type (Full_T) then
17965 declare
17966 Disp_Typ : Entity_Id;
17967 Full_List : Elist_Id;
17968 Prim : Entity_Id;
17969 Prim_Elmt : Elmt_Id;
17970 Priv_List : Elist_Id;
17972 function Contains
17973 (E : Entity_Id;
17974 L : Elist_Id) return Boolean;
17975 -- Determine whether list L contains element E
17977 --------------
17978 -- Contains --
17979 --------------
17981 function Contains
17982 (E : Entity_Id;
17983 L : Elist_Id) return Boolean
17985 List_Elmt : Elmt_Id;
17987 begin
17988 List_Elmt := First_Elmt (L);
17989 while Present (List_Elmt) loop
17990 if Node (List_Elmt) = E then
17991 return True;
17992 end if;
17994 Next_Elmt (List_Elmt);
17995 end loop;
17997 return False;
17998 end Contains;
18000 -- Start of processing
18002 begin
18003 if Is_Tagged_Type (Priv_T) then
18004 Priv_List := Primitive_Operations (Priv_T);
18005 Prim_Elmt := First_Elmt (Priv_List);
18007 -- In the case of a concurrent type completing a private tagged
18008 -- type, primitives may have been declared in between the two
18009 -- views. These subprograms need to be wrapped the same way
18010 -- entries and protected procedures are handled because they
18011 -- cannot be directly shared by the two views.
18013 if Is_Concurrent_Type (Full_T) then
18014 declare
18015 Conc_Typ : constant Entity_Id :=
18016 Corresponding_Record_Type (Full_T);
18017 Curr_Nod : Node_Id := Parent (Conc_Typ);
18018 Wrap_Spec : Node_Id;
18020 begin
18021 while Present (Prim_Elmt) loop
18022 Prim := Node (Prim_Elmt);
18024 if Comes_From_Source (Prim)
18025 and then not Is_Abstract_Subprogram (Prim)
18026 then
18027 Wrap_Spec :=
18028 Make_Subprogram_Declaration (Sloc (Prim),
18029 Specification =>
18030 Build_Wrapper_Spec
18031 (Subp_Id => Prim,
18032 Obj_Typ => Conc_Typ,
18033 Formals =>
18034 Parameter_Specifications (
18035 Parent (Prim))));
18037 Insert_After (Curr_Nod, Wrap_Spec);
18038 Curr_Nod := Wrap_Spec;
18040 Analyze (Wrap_Spec);
18041 end if;
18043 Next_Elmt (Prim_Elmt);
18044 end loop;
18046 return;
18047 end;
18049 -- For non-concurrent types, transfer explicit primitives, but
18050 -- omit those inherited from the parent of the private view
18051 -- since they will be re-inherited later on.
18053 else
18054 Full_List := Primitive_Operations (Full_T);
18056 while Present (Prim_Elmt) loop
18057 Prim := Node (Prim_Elmt);
18059 if Comes_From_Source (Prim)
18060 and then not Contains (Prim, Full_List)
18061 then
18062 Append_Elmt (Prim, Full_List);
18063 end if;
18065 Next_Elmt (Prim_Elmt);
18066 end loop;
18067 end if;
18069 -- Untagged private view
18071 else
18072 Full_List := Primitive_Operations (Full_T);
18074 -- In this case the partial view is untagged, so here we locate
18075 -- all of the earlier primitives that need to be treated as
18076 -- dispatching (those that appear between the two views). Note
18077 -- that these additional operations must all be new operations
18078 -- (any earlier operations that override inherited operations
18079 -- of the full view will already have been inserted in the
18080 -- primitives list, marked by Check_Operation_From_Private_View
18081 -- as dispatching. Note that implicit "/=" operators are
18082 -- excluded from being added to the primitives list since they
18083 -- shouldn't be treated as dispatching (tagged "/=" is handled
18084 -- specially).
18086 Prim := Next_Entity (Full_T);
18087 while Present (Prim) and then Prim /= Priv_T loop
18088 if Ekind_In (Prim, E_Procedure, E_Function) then
18089 Disp_Typ := Find_Dispatching_Type (Prim);
18091 if Disp_Typ = Full_T
18092 and then (Chars (Prim) /= Name_Op_Ne
18093 or else Comes_From_Source (Prim))
18094 then
18095 Check_Controlling_Formals (Full_T, Prim);
18097 if not Is_Dispatching_Operation (Prim) then
18098 Append_Elmt (Prim, Full_List);
18099 Set_Is_Dispatching_Operation (Prim, True);
18100 Set_DT_Position (Prim, No_Uint);
18101 end if;
18103 elsif Is_Dispatching_Operation (Prim)
18104 and then Disp_Typ /= Full_T
18105 then
18107 -- Verify that it is not otherwise controlled by a
18108 -- formal or a return value of type T.
18110 Check_Controlling_Formals (Disp_Typ, Prim);
18111 end if;
18112 end if;
18114 Next_Entity (Prim);
18115 end loop;
18116 end if;
18118 -- For the tagged case, the two views can share the same primitive
18119 -- operations list and the same class-wide type. Update attributes
18120 -- of the class-wide type which depend on the full declaration.
18122 if Is_Tagged_Type (Priv_T) then
18123 Set_Direct_Primitive_Operations (Priv_T, Full_List);
18124 Set_Class_Wide_Type
18125 (Base_Type (Full_T), Class_Wide_Type (Priv_T));
18127 Set_Has_Task (Class_Wide_Type (Priv_T), Has_Task (Full_T));
18128 end if;
18129 end;
18130 end if;
18132 -- Ada 2005 AI 161: Check preelaboratable initialization consistency
18134 if Known_To_Have_Preelab_Init (Priv_T) then
18136 -- Case where there is a pragma Preelaborable_Initialization. We
18137 -- always allow this in predefined units, which is a bit of a kludge,
18138 -- but it means we don't have to struggle to meet the requirements in
18139 -- the RM for having Preelaborable Initialization. Otherwise we
18140 -- require that the type meets the RM rules. But we can't check that
18141 -- yet, because of the rule about overriding Initialize, so we simply
18142 -- set a flag that will be checked at freeze time.
18144 if not In_Predefined_Unit (Full_T) then
18145 Set_Must_Have_Preelab_Init (Full_T);
18146 end if;
18147 end if;
18149 -- If pragma CPP_Class was applied to the private type declaration,
18150 -- propagate it now to the full type declaration.
18152 if Is_CPP_Class (Priv_T) then
18153 Set_Is_CPP_Class (Full_T);
18154 Set_Convention (Full_T, Convention_CPP);
18156 -- Check that components of imported CPP types do not have default
18157 -- expressions.
18159 Check_CPP_Type_Has_No_Defaults (Full_T);
18160 end if;
18162 -- If the private view has user specified stream attributes, then so has
18163 -- the full view.
18165 -- Why the test, how could these flags be already set in Full_T ???
18167 if Has_Specified_Stream_Read (Priv_T) then
18168 Set_Has_Specified_Stream_Read (Full_T);
18169 end if;
18171 if Has_Specified_Stream_Write (Priv_T) then
18172 Set_Has_Specified_Stream_Write (Full_T);
18173 end if;
18175 if Has_Specified_Stream_Input (Priv_T) then
18176 Set_Has_Specified_Stream_Input (Full_T);
18177 end if;
18179 if Has_Specified_Stream_Output (Priv_T) then
18180 Set_Has_Specified_Stream_Output (Full_T);
18181 end if;
18183 -- Propagate invariants to full type
18185 if Has_Invariants (Priv_T) then
18186 Set_Has_Invariants (Full_T);
18187 Set_Invariant_Procedure (Full_T, Invariant_Procedure (Priv_T));
18188 end if;
18190 if Has_Inheritable_Invariants (Priv_T) then
18191 Set_Has_Inheritable_Invariants (Full_T);
18192 end if;
18194 -- Propagate predicates to full type
18196 if Has_Predicates (Priv_T) then
18197 Set_Predicate_Function (Priv_T, Predicate_Function (Full_T));
18198 Set_Has_Predicates (Full_T);
18199 end if;
18200 end Process_Full_View;
18202 -----------------------------------
18203 -- Process_Incomplete_Dependents --
18204 -----------------------------------
18206 procedure Process_Incomplete_Dependents
18207 (N : Node_Id;
18208 Full_T : Entity_Id;
18209 Inc_T : Entity_Id)
18211 Inc_Elmt : Elmt_Id;
18212 Priv_Dep : Entity_Id;
18213 New_Subt : Entity_Id;
18215 Disc_Constraint : Elist_Id;
18217 begin
18218 if No (Private_Dependents (Inc_T)) then
18219 return;
18220 end if;
18222 -- Itypes that may be generated by the completion of an incomplete
18223 -- subtype are not used by the back-end and not attached to the tree.
18224 -- They are created only for constraint-checking purposes.
18226 Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
18227 while Present (Inc_Elmt) loop
18228 Priv_Dep := Node (Inc_Elmt);
18230 if Ekind (Priv_Dep) = E_Subprogram_Type then
18232 -- An Access_To_Subprogram type may have a return type or a
18233 -- parameter type that is incomplete. Replace with the full view.
18235 if Etype (Priv_Dep) = Inc_T then
18236 Set_Etype (Priv_Dep, Full_T);
18237 end if;
18239 declare
18240 Formal : Entity_Id;
18242 begin
18243 Formal := First_Formal (Priv_Dep);
18244 while Present (Formal) loop
18245 if Etype (Formal) = Inc_T then
18246 Set_Etype (Formal, Full_T);
18247 end if;
18249 Next_Formal (Formal);
18250 end loop;
18251 end;
18253 elsif Is_Overloadable (Priv_Dep) then
18255 -- If a subprogram in the incomplete dependents list is primitive
18256 -- for a tagged full type then mark it as a dispatching operation,
18257 -- check whether it overrides an inherited subprogram, and check
18258 -- restrictions on its controlling formals. Note that a protected
18259 -- operation is never dispatching: only its wrapper operation
18260 -- (which has convention Ada) is.
18262 if Is_Tagged_Type (Full_T)
18263 and then Is_Primitive (Priv_Dep)
18264 and then Convention (Priv_Dep) /= Convention_Protected
18265 then
18266 Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
18267 Set_Is_Dispatching_Operation (Priv_Dep);
18268 Check_Controlling_Formals (Full_T, Priv_Dep);
18269 end if;
18271 elsif Ekind (Priv_Dep) = E_Subprogram_Body then
18273 -- Can happen during processing of a body before the completion
18274 -- of a TA type. Ignore, because spec is also on dependent list.
18276 return;
18278 -- Ada 2005 (AI-412): Transform a regular incomplete subtype into a
18279 -- corresponding subtype of the full view.
18281 elsif Ekind (Priv_Dep) = E_Incomplete_Subtype then
18282 Set_Subtype_Indication
18283 (Parent (Priv_Dep), New_Reference_To (Full_T, Sloc (Priv_Dep)));
18284 Set_Etype (Priv_Dep, Full_T);
18285 Set_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
18286 Set_Analyzed (Parent (Priv_Dep), False);
18288 -- Reanalyze the declaration, suppressing the call to
18289 -- Enter_Name to avoid duplicate names.
18291 Analyze_Subtype_Declaration
18292 (N => Parent (Priv_Dep),
18293 Skip => True);
18295 -- Dependent is a subtype
18297 else
18298 -- We build a new subtype indication using the full view of the
18299 -- incomplete parent. The discriminant constraints have been
18300 -- elaborated already at the point of the subtype declaration.
18302 New_Subt := Create_Itype (E_Void, N);
18304 if Has_Discriminants (Full_T) then
18305 Disc_Constraint := Discriminant_Constraint (Priv_Dep);
18306 else
18307 Disc_Constraint := No_Elist;
18308 end if;
18310 Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
18311 Set_Full_View (Priv_Dep, New_Subt);
18312 end if;
18314 Next_Elmt (Inc_Elmt);
18315 end loop;
18316 end Process_Incomplete_Dependents;
18318 --------------------------------
18319 -- Process_Range_Expr_In_Decl --
18320 --------------------------------
18322 procedure Process_Range_Expr_In_Decl
18323 (R : Node_Id;
18324 T : Entity_Id;
18325 Check_List : List_Id := Empty_List;
18326 R_Check_Off : Boolean := False;
18327 In_Iter_Schm : Boolean := False)
18329 Lo, Hi : Node_Id;
18330 R_Checks : Check_Result;
18331 Insert_Node : Node_Id;
18332 Def_Id : Entity_Id;
18334 begin
18335 Analyze_And_Resolve (R, Base_Type (T));
18337 if Nkind (R) = N_Range then
18339 -- In SPARK, all ranges should be static, with the exception of the
18340 -- discrete type definition of a loop parameter specification.
18342 if not In_Iter_Schm
18343 and then not Is_Static_Range (R)
18344 then
18345 Check_SPARK_Restriction ("range should be static", R);
18346 end if;
18348 Lo := Low_Bound (R);
18349 Hi := High_Bound (R);
18351 -- We need to ensure validity of the bounds here, because if we
18352 -- go ahead and do the expansion, then the expanded code will get
18353 -- analyzed with range checks suppressed and we miss the check.
18355 Validity_Check_Range (R);
18357 -- If there were errors in the declaration, try and patch up some
18358 -- common mistakes in the bounds. The cases handled are literals
18359 -- which are Integer where the expected type is Real and vice versa.
18360 -- These corrections allow the compilation process to proceed further
18361 -- along since some basic assumptions of the format of the bounds
18362 -- are guaranteed.
18364 if Etype (R) = Any_Type then
18366 if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
18367 Rewrite (Lo,
18368 Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
18370 elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
18371 Rewrite (Hi,
18372 Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
18374 elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
18375 Rewrite (Lo,
18376 Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
18378 elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
18379 Rewrite (Hi,
18380 Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
18381 end if;
18383 Set_Etype (Lo, T);
18384 Set_Etype (Hi, T);
18385 end if;
18387 -- If the bounds of the range have been mistakenly given as string
18388 -- literals (perhaps in place of character literals), then an error
18389 -- has already been reported, but we rewrite the string literal as a
18390 -- bound of the range's type to avoid blowups in later processing
18391 -- that looks at static values.
18393 if Nkind (Lo) = N_String_Literal then
18394 Rewrite (Lo,
18395 Make_Attribute_Reference (Sloc (Lo),
18396 Attribute_Name => Name_First,
18397 Prefix => New_Reference_To (T, Sloc (Lo))));
18398 Analyze_And_Resolve (Lo);
18399 end if;
18401 if Nkind (Hi) = N_String_Literal then
18402 Rewrite (Hi,
18403 Make_Attribute_Reference (Sloc (Hi),
18404 Attribute_Name => Name_First,
18405 Prefix => New_Reference_To (T, Sloc (Hi))));
18406 Analyze_And_Resolve (Hi);
18407 end if;
18409 -- If bounds aren't scalar at this point then exit, avoiding
18410 -- problems with further processing of the range in this procedure.
18412 if not Is_Scalar_Type (Etype (Lo)) then
18413 return;
18414 end if;
18416 -- Resolve (actually Sem_Eval) has checked that the bounds are in
18417 -- then range of the base type. Here we check whether the bounds
18418 -- are in the range of the subtype itself. Note that if the bounds
18419 -- represent the null range the Constraint_Error exception should
18420 -- not be raised.
18422 -- ??? The following code should be cleaned up as follows
18424 -- 1. The Is_Null_Range (Lo, Hi) test should disappear since it
18425 -- is done in the call to Range_Check (R, T); below
18427 -- 2. The use of R_Check_Off should be investigated and possibly
18428 -- removed, this would clean up things a bit.
18430 if Is_Null_Range (Lo, Hi) then
18431 null;
18433 else
18434 -- Capture values of bounds and generate temporaries for them
18435 -- if needed, before applying checks, since checks may cause
18436 -- duplication of the expression without forcing evaluation.
18438 -- The forced evaluation removes side effects from expressions,
18439 -- which should occur also in Alfa mode. Otherwise, we end up with
18440 -- unexpected insertions of actions at places where this is not
18441 -- supposed to occur, e.g. on default parameters of a call.
18443 if Expander_Active then
18444 Force_Evaluation (Lo);
18445 Force_Evaluation (Hi);
18446 end if;
18448 -- We use a flag here instead of suppressing checks on the
18449 -- type because the type we check against isn't necessarily
18450 -- the place where we put the check.
18452 if not R_Check_Off then
18453 R_Checks := Get_Range_Checks (R, T);
18455 -- Look up tree to find an appropriate insertion point. We
18456 -- can't just use insert_actions because later processing
18457 -- depends on the insertion node. Prior to Ada 2012 the
18458 -- insertion point could only be a declaration or a loop, but
18459 -- quantified expressions can appear within any context in an
18460 -- expression, and the insertion point can be any statement,
18461 -- pragma, or declaration.
18463 Insert_Node := Parent (R);
18464 while Present (Insert_Node) loop
18465 exit when
18466 Nkind (Insert_Node) in N_Declaration
18467 and then
18468 not Nkind_In
18469 (Insert_Node, N_Component_Declaration,
18470 N_Loop_Parameter_Specification,
18471 N_Function_Specification,
18472 N_Procedure_Specification);
18474 exit when Nkind (Insert_Node) in N_Later_Decl_Item
18475 or else Nkind (Insert_Node) in
18476 N_Statement_Other_Than_Procedure_Call
18477 or else Nkind_In (Insert_Node, N_Procedure_Call_Statement,
18478 N_Pragma);
18480 Insert_Node := Parent (Insert_Node);
18481 end loop;
18483 -- Why would Type_Decl not be present??? Without this test,
18484 -- short regression tests fail.
18486 if Present (Insert_Node) then
18488 -- Case of loop statement. Verify that the range is part
18489 -- of the subtype indication of the iteration scheme.
18491 if Nkind (Insert_Node) = N_Loop_Statement then
18492 declare
18493 Indic : Node_Id;
18495 begin
18496 Indic := Parent (R);
18497 while Present (Indic)
18498 and then Nkind (Indic) /= N_Subtype_Indication
18499 loop
18500 Indic := Parent (Indic);
18501 end loop;
18503 if Present (Indic) then
18504 Def_Id := Etype (Subtype_Mark (Indic));
18506 Insert_Range_Checks
18507 (R_Checks,
18508 Insert_Node,
18509 Def_Id,
18510 Sloc (Insert_Node),
18512 Do_Before => True);
18513 end if;
18514 end;
18516 -- Insertion before a declaration. If the declaration
18517 -- includes discriminants, the list of applicable checks
18518 -- is given by the caller.
18520 elsif Nkind (Insert_Node) in N_Declaration then
18521 Def_Id := Defining_Identifier (Insert_Node);
18523 if (Ekind (Def_Id) = E_Record_Type
18524 and then Depends_On_Discriminant (R))
18525 or else
18526 (Ekind (Def_Id) = E_Protected_Type
18527 and then Has_Discriminants (Def_Id))
18528 then
18529 Append_Range_Checks
18530 (R_Checks,
18531 Check_List, Def_Id, Sloc (Insert_Node), R);
18533 else
18534 Insert_Range_Checks
18535 (R_Checks,
18536 Insert_Node, Def_Id, Sloc (Insert_Node), R);
18538 end if;
18540 -- Insertion before a statement. Range appears in the
18541 -- context of a quantified expression. Insertion will
18542 -- take place when expression is expanded.
18544 else
18545 null;
18546 end if;
18547 end if;
18548 end if;
18549 end if;
18551 -- Case of other than an explicit N_Range node
18553 -- The forced evaluation removes side effects from expressions, which
18554 -- should occur also in Alfa mode. Otherwise, we end up with unexpected
18555 -- insertions of actions at places where this is not supposed to occur,
18556 -- e.g. on default parameters of a call.
18558 elsif Expander_Active then
18559 Get_Index_Bounds (R, Lo, Hi);
18560 Force_Evaluation (Lo);
18561 Force_Evaluation (Hi);
18562 end if;
18563 end Process_Range_Expr_In_Decl;
18565 --------------------------------------
18566 -- Process_Real_Range_Specification --
18567 --------------------------------------
18569 procedure Process_Real_Range_Specification (Def : Node_Id) is
18570 Spec : constant Node_Id := Real_Range_Specification (Def);
18571 Lo : Node_Id;
18572 Hi : Node_Id;
18573 Err : Boolean := False;
18575 procedure Analyze_Bound (N : Node_Id);
18576 -- Analyze and check one bound
18578 -------------------
18579 -- Analyze_Bound --
18580 -------------------
18582 procedure Analyze_Bound (N : Node_Id) is
18583 begin
18584 Analyze_And_Resolve (N, Any_Real);
18586 if not Is_OK_Static_Expression (N) then
18587 Flag_Non_Static_Expr
18588 ("bound in real type definition is not static!", N);
18589 Err := True;
18590 end if;
18591 end Analyze_Bound;
18593 -- Start of processing for Process_Real_Range_Specification
18595 begin
18596 if Present (Spec) then
18597 Lo := Low_Bound (Spec);
18598 Hi := High_Bound (Spec);
18599 Analyze_Bound (Lo);
18600 Analyze_Bound (Hi);
18602 -- If error, clear away junk range specification
18604 if Err then
18605 Set_Real_Range_Specification (Def, Empty);
18606 end if;
18607 end if;
18608 end Process_Real_Range_Specification;
18610 ---------------------
18611 -- Process_Subtype --
18612 ---------------------
18614 function Process_Subtype
18615 (S : Node_Id;
18616 Related_Nod : Node_Id;
18617 Related_Id : Entity_Id := Empty;
18618 Suffix : Character := ' ') return Entity_Id
18620 P : Node_Id;
18621 Def_Id : Entity_Id;
18622 Error_Node : Node_Id;
18623 Full_View_Id : Entity_Id;
18624 Subtype_Mark_Id : Entity_Id;
18626 May_Have_Null_Exclusion : Boolean;
18628 procedure Check_Incomplete (T : Entity_Id);
18629 -- Called to verify that an incomplete type is not used prematurely
18631 ----------------------
18632 -- Check_Incomplete --
18633 ----------------------
18635 procedure Check_Incomplete (T : Entity_Id) is
18636 begin
18637 -- Ada 2005 (AI-412): Incomplete subtypes are legal
18639 if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
18640 and then
18641 not (Ada_Version >= Ada_2005
18642 and then
18643 (Nkind (Parent (T)) = N_Subtype_Declaration
18644 or else
18645 (Nkind (Parent (T)) = N_Subtype_Indication
18646 and then Nkind (Parent (Parent (T))) =
18647 N_Subtype_Declaration)))
18648 then
18649 Error_Msg_N ("invalid use of type before its full declaration", T);
18650 end if;
18651 end Check_Incomplete;
18653 -- Start of processing for Process_Subtype
18655 begin
18656 -- Case of no constraints present
18658 if Nkind (S) /= N_Subtype_Indication then
18659 Find_Type (S);
18660 Check_Incomplete (S);
18661 P := Parent (S);
18663 -- Ada 2005 (AI-231): Static check
18665 if Ada_Version >= Ada_2005
18666 and then Present (P)
18667 and then Null_Exclusion_Present (P)
18668 and then Nkind (P) /= N_Access_To_Object_Definition
18669 and then not Is_Access_Type (Entity (S))
18670 then
18671 Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
18672 end if;
18674 -- The following is ugly, can't we have a range or even a flag???
18676 May_Have_Null_Exclusion :=
18677 Nkind_In (P, N_Access_Definition,
18678 N_Access_Function_Definition,
18679 N_Access_Procedure_Definition,
18680 N_Access_To_Object_Definition,
18681 N_Allocator,
18682 N_Component_Definition)
18683 or else
18684 Nkind_In (P, N_Derived_Type_Definition,
18685 N_Discriminant_Specification,
18686 N_Formal_Object_Declaration,
18687 N_Object_Declaration,
18688 N_Object_Renaming_Declaration,
18689 N_Parameter_Specification,
18690 N_Subtype_Declaration);
18692 -- Create an Itype that is a duplicate of Entity (S) but with the
18693 -- null-exclusion attribute.
18695 if May_Have_Null_Exclusion
18696 and then Is_Access_Type (Entity (S))
18697 and then Null_Exclusion_Present (P)
18699 -- No need to check the case of an access to object definition.
18700 -- It is correct to define double not-null pointers.
18702 -- Example:
18703 -- type Not_Null_Int_Ptr is not null access Integer;
18704 -- type Acc is not null access Not_Null_Int_Ptr;
18706 and then Nkind (P) /= N_Access_To_Object_Definition
18707 then
18708 if Can_Never_Be_Null (Entity (S)) then
18709 case Nkind (Related_Nod) is
18710 when N_Full_Type_Declaration =>
18711 if Nkind (Type_Definition (Related_Nod))
18712 in N_Array_Type_Definition
18713 then
18714 Error_Node :=
18715 Subtype_Indication
18716 (Component_Definition
18717 (Type_Definition (Related_Nod)));
18718 else
18719 Error_Node :=
18720 Subtype_Indication (Type_Definition (Related_Nod));
18721 end if;
18723 when N_Subtype_Declaration =>
18724 Error_Node := Subtype_Indication (Related_Nod);
18726 when N_Object_Declaration =>
18727 Error_Node := Object_Definition (Related_Nod);
18729 when N_Component_Declaration =>
18730 Error_Node :=
18731 Subtype_Indication (Component_Definition (Related_Nod));
18733 when N_Allocator =>
18734 Error_Node := Expression (Related_Nod);
18736 when others =>
18737 pragma Assert (False);
18738 Error_Node := Related_Nod;
18739 end case;
18741 Error_Msg_NE
18742 ("`NOT NULL` not allowed (& already excludes null)",
18743 Error_Node,
18744 Entity (S));
18745 end if;
18747 Set_Etype (S,
18748 Create_Null_Excluding_Itype
18749 (T => Entity (S),
18750 Related_Nod => P));
18751 Set_Entity (S, Etype (S));
18752 end if;
18754 return Entity (S);
18756 -- Case of constraint present, so that we have an N_Subtype_Indication
18757 -- node (this node is created only if constraints are present).
18759 else
18760 Find_Type (Subtype_Mark (S));
18762 if Nkind (Parent (S)) /= N_Access_To_Object_Definition
18763 and then not
18764 (Nkind (Parent (S)) = N_Subtype_Declaration
18765 and then Is_Itype (Defining_Identifier (Parent (S))))
18766 then
18767 Check_Incomplete (Subtype_Mark (S));
18768 end if;
18770 P := Parent (S);
18771 Subtype_Mark_Id := Entity (Subtype_Mark (S));
18773 -- Explicit subtype declaration case
18775 if Nkind (P) = N_Subtype_Declaration then
18776 Def_Id := Defining_Identifier (P);
18778 -- Explicit derived type definition case
18780 elsif Nkind (P) = N_Derived_Type_Definition then
18781 Def_Id := Defining_Identifier (Parent (P));
18783 -- Implicit case, the Def_Id must be created as an implicit type.
18784 -- The one exception arises in the case of concurrent types, array
18785 -- and access types, where other subsidiary implicit types may be
18786 -- created and must appear before the main implicit type. In these
18787 -- cases we leave Def_Id set to Empty as a signal that Create_Itype
18788 -- has not yet been called to create Def_Id.
18790 else
18791 if Is_Array_Type (Subtype_Mark_Id)
18792 or else Is_Concurrent_Type (Subtype_Mark_Id)
18793 or else Is_Access_Type (Subtype_Mark_Id)
18794 then
18795 Def_Id := Empty;
18797 -- For the other cases, we create a new unattached Itype,
18798 -- and set the indication to ensure it gets attached later.
18800 else
18801 Def_Id :=
18802 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
18803 end if;
18804 end if;
18806 -- If the kind of constraint is invalid for this kind of type,
18807 -- then give an error, and then pretend no constraint was given.
18809 if not Is_Valid_Constraint_Kind
18810 (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
18811 then
18812 Error_Msg_N
18813 ("incorrect constraint for this kind of type", Constraint (S));
18815 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
18817 -- Set Ekind of orphan itype, to prevent cascaded errors
18819 if Present (Def_Id) then
18820 Set_Ekind (Def_Id, Ekind (Any_Type));
18821 end if;
18823 -- Make recursive call, having got rid of the bogus constraint
18825 return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
18826 end if;
18828 -- Remaining processing depends on type. Select on Base_Type kind to
18829 -- ensure getting to the concrete type kind in the case of a private
18830 -- subtype (needed when only doing semantic analysis).
18832 case Ekind (Base_Type (Subtype_Mark_Id)) is
18833 when Access_Kind =>
18834 Constrain_Access (Def_Id, S, Related_Nod);
18836 if Expander_Active
18837 and then Is_Itype (Designated_Type (Def_Id))
18838 and then Nkind (Related_Nod) = N_Subtype_Declaration
18839 and then not Is_Incomplete_Type (Designated_Type (Def_Id))
18840 then
18841 Build_Itype_Reference
18842 (Designated_Type (Def_Id), Related_Nod);
18843 end if;
18845 when Array_Kind =>
18846 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
18848 when Decimal_Fixed_Point_Kind =>
18849 Constrain_Decimal (Def_Id, S);
18851 when Enumeration_Kind =>
18852 Constrain_Enumeration (Def_Id, S);
18854 when Ordinary_Fixed_Point_Kind =>
18855 Constrain_Ordinary_Fixed (Def_Id, S);
18857 when Float_Kind =>
18858 Constrain_Float (Def_Id, S);
18860 when Integer_Kind =>
18861 Constrain_Integer (Def_Id, S);
18863 when E_Record_Type |
18864 E_Record_Subtype |
18865 Class_Wide_Kind |
18866 E_Incomplete_Type =>
18867 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
18869 if Ekind (Def_Id) = E_Incomplete_Type then
18870 Set_Private_Dependents (Def_Id, New_Elmt_List);
18871 end if;
18873 when Private_Kind =>
18874 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
18875 Set_Private_Dependents (Def_Id, New_Elmt_List);
18877 -- In case of an invalid constraint prevent further processing
18878 -- since the type constructed is missing expected fields.
18880 if Etype (Def_Id) = Any_Type then
18881 return Def_Id;
18882 end if;
18884 -- If the full view is that of a task with discriminants,
18885 -- we must constrain both the concurrent type and its
18886 -- corresponding record type. Otherwise we will just propagate
18887 -- the constraint to the full view, if available.
18889 if Present (Full_View (Subtype_Mark_Id))
18890 and then Has_Discriminants (Subtype_Mark_Id)
18891 and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
18892 then
18893 Full_View_Id :=
18894 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
18896 Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
18897 Constrain_Concurrent (Full_View_Id, S,
18898 Related_Nod, Related_Id, Suffix);
18899 Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
18900 Set_Full_View (Def_Id, Full_View_Id);
18902 -- Introduce an explicit reference to the private subtype,
18903 -- to prevent scope anomalies in gigi if first use appears
18904 -- in a nested context, e.g. a later function body.
18905 -- Should this be generated in other contexts than a full
18906 -- type declaration?
18908 if Is_Itype (Def_Id)
18909 and then
18910 Nkind (Parent (P)) = N_Full_Type_Declaration
18911 then
18912 Build_Itype_Reference (Def_Id, Parent (P));
18913 end if;
18915 else
18916 Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
18917 end if;
18919 when Concurrent_Kind =>
18920 Constrain_Concurrent (Def_Id, S,
18921 Related_Nod, Related_Id, Suffix);
18923 when others =>
18924 Error_Msg_N ("invalid subtype mark in subtype indication", S);
18925 end case;
18927 -- Size and Convention are always inherited from the base type
18929 Set_Size_Info (Def_Id, (Subtype_Mark_Id));
18930 Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
18932 return Def_Id;
18933 end if;
18934 end Process_Subtype;
18936 ---------------------------------------
18937 -- Check_Anonymous_Access_Components --
18938 ---------------------------------------
18940 procedure Check_Anonymous_Access_Components
18941 (Typ_Decl : Node_Id;
18942 Typ : Entity_Id;
18943 Prev : Entity_Id;
18944 Comp_List : Node_Id)
18946 Loc : constant Source_Ptr := Sloc (Typ_Decl);
18947 Anon_Access : Entity_Id;
18948 Acc_Def : Node_Id;
18949 Comp : Node_Id;
18950 Comp_Def : Node_Id;
18951 Decl : Node_Id;
18952 Type_Def : Node_Id;
18954 procedure Build_Incomplete_Type_Declaration;
18955 -- If the record type contains components that include an access to the
18956 -- current record, then create an incomplete type declaration for the
18957 -- record, to be used as the designated type of the anonymous access.
18958 -- This is done only once, and only if there is no previous partial
18959 -- view of the type.
18961 function Designates_T (Subt : Node_Id) return Boolean;
18962 -- Check whether a node designates the enclosing record type, or 'Class
18963 -- of that type
18965 function Mentions_T (Acc_Def : Node_Id) return Boolean;
18966 -- Check whether an access definition includes a reference to
18967 -- the enclosing record type. The reference can be a subtype mark
18968 -- in the access definition itself, a 'Class attribute reference, or
18969 -- recursively a reference appearing in a parameter specification
18970 -- or result definition of an access_to_subprogram definition.
18972 --------------------------------------
18973 -- Build_Incomplete_Type_Declaration --
18974 --------------------------------------
18976 procedure Build_Incomplete_Type_Declaration is
18977 Decl : Node_Id;
18978 Inc_T : Entity_Id;
18979 H : Entity_Id;
18981 -- Is_Tagged indicates whether the type is tagged. It is tagged if
18982 -- it's "is new ... with record" or else "is tagged record ...".
18984 Is_Tagged : constant Boolean :=
18985 (Nkind (Type_Definition (Typ_Decl)) = N_Derived_Type_Definition
18986 and then
18987 Present
18988 (Record_Extension_Part (Type_Definition (Typ_Decl))))
18989 or else
18990 (Nkind (Type_Definition (Typ_Decl)) = N_Record_Definition
18991 and then Tagged_Present (Type_Definition (Typ_Decl)));
18993 begin
18994 -- If there is a previous partial view, no need to create a new one
18995 -- If the partial view, given by Prev, is incomplete, If Prev is
18996 -- a private declaration, full declaration is flagged accordingly.
18998 if Prev /= Typ then
18999 if Is_Tagged then
19000 Make_Class_Wide_Type (Prev);
19001 Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
19002 Set_Etype (Class_Wide_Type (Typ), Typ);
19003 end if;
19005 return;
19007 elsif Has_Private_Declaration (Typ) then
19009 -- If we refer to T'Class inside T, and T is the completion of a
19010 -- private type, then we need to make sure the class-wide type
19011 -- exists.
19013 if Is_Tagged then
19014 Make_Class_Wide_Type (Typ);
19015 end if;
19017 return;
19019 -- If there was a previous anonymous access type, the incomplete
19020 -- type declaration will have been created already.
19022 elsif Present (Current_Entity (Typ))
19023 and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
19024 and then Full_View (Current_Entity (Typ)) = Typ
19025 then
19026 if Is_Tagged
19027 and then Comes_From_Source (Current_Entity (Typ))
19028 and then not Is_Tagged_Type (Current_Entity (Typ))
19029 then
19030 Make_Class_Wide_Type (Typ);
19031 Error_Msg_N
19032 ("incomplete view of tagged type should be declared tagged?",
19033 Parent (Current_Entity (Typ)));
19034 end if;
19035 return;
19037 else
19038 Inc_T := Make_Defining_Identifier (Loc, Chars (Typ));
19039 Decl := Make_Incomplete_Type_Declaration (Loc, Inc_T);
19041 -- Type has already been inserted into the current scope. Remove
19042 -- it, and add incomplete declaration for type, so that subsequent
19043 -- anonymous access types can use it. The entity is unchained from
19044 -- the homonym list and from immediate visibility. After analysis,
19045 -- the entity in the incomplete declaration becomes immediately
19046 -- visible in the record declaration that follows.
19048 H := Current_Entity (Typ);
19050 if H = Typ then
19051 Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
19052 else
19053 while Present (H)
19054 and then Homonym (H) /= Typ
19055 loop
19056 H := Homonym (Typ);
19057 end loop;
19059 Set_Homonym (H, Homonym (Typ));
19060 end if;
19062 Insert_Before (Typ_Decl, Decl);
19063 Analyze (Decl);
19064 Set_Full_View (Inc_T, Typ);
19066 if Is_Tagged then
19068 -- Create a common class-wide type for both views, and set the
19069 -- Etype of the class-wide type to the full view.
19071 Make_Class_Wide_Type (Inc_T);
19072 Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
19073 Set_Etype (Class_Wide_Type (Typ), Typ);
19074 end if;
19075 end if;
19076 end Build_Incomplete_Type_Declaration;
19078 ------------------
19079 -- Designates_T --
19080 ------------------
19082 function Designates_T (Subt : Node_Id) return Boolean is
19083 Type_Id : constant Name_Id := Chars (Typ);
19085 function Names_T (Nam : Node_Id) return Boolean;
19086 -- The record type has not been introduced in the current scope
19087 -- yet, so we must examine the name of the type itself, either
19088 -- an identifier T, or an expanded name of the form P.T, where
19089 -- P denotes the current scope.
19091 -------------
19092 -- Names_T --
19093 -------------
19095 function Names_T (Nam : Node_Id) return Boolean is
19096 begin
19097 if Nkind (Nam) = N_Identifier then
19098 return Chars (Nam) = Type_Id;
19100 elsif Nkind (Nam) = N_Selected_Component then
19101 if Chars (Selector_Name (Nam)) = Type_Id then
19102 if Nkind (Prefix (Nam)) = N_Identifier then
19103 return Chars (Prefix (Nam)) = Chars (Current_Scope);
19105 elsif Nkind (Prefix (Nam)) = N_Selected_Component then
19106 return Chars (Selector_Name (Prefix (Nam))) =
19107 Chars (Current_Scope);
19108 else
19109 return False;
19110 end if;
19112 else
19113 return False;
19114 end if;
19116 else
19117 return False;
19118 end if;
19119 end Names_T;
19121 -- Start of processing for Designates_T
19123 begin
19124 if Nkind (Subt) = N_Identifier then
19125 return Chars (Subt) = Type_Id;
19127 -- Reference can be through an expanded name which has not been
19128 -- analyzed yet, and which designates enclosing scopes.
19130 elsif Nkind (Subt) = N_Selected_Component then
19131 if Names_T (Subt) then
19132 return True;
19134 -- Otherwise it must denote an entity that is already visible.
19135 -- The access definition may name a subtype of the enclosing
19136 -- type, if there is a previous incomplete declaration for it.
19138 else
19139 Find_Selected_Component (Subt);
19140 return
19141 Is_Entity_Name (Subt)
19142 and then Scope (Entity (Subt)) = Current_Scope
19143 and then
19144 (Chars (Base_Type (Entity (Subt))) = Type_Id
19145 or else
19146 (Is_Class_Wide_Type (Entity (Subt))
19147 and then
19148 Chars (Etype (Base_Type (Entity (Subt)))) =
19149 Type_Id));
19150 end if;
19152 -- A reference to the current type may appear as the prefix of
19153 -- a 'Class attribute.
19155 elsif Nkind (Subt) = N_Attribute_Reference
19156 and then Attribute_Name (Subt) = Name_Class
19157 then
19158 return Names_T (Prefix (Subt));
19160 else
19161 return False;
19162 end if;
19163 end Designates_T;
19165 ----------------
19166 -- Mentions_T --
19167 ----------------
19169 function Mentions_T (Acc_Def : Node_Id) return Boolean is
19170 Param_Spec : Node_Id;
19172 Acc_Subprg : constant Node_Id :=
19173 Access_To_Subprogram_Definition (Acc_Def);
19175 begin
19176 if No (Acc_Subprg) then
19177 return Designates_T (Subtype_Mark (Acc_Def));
19178 end if;
19180 -- Component is an access_to_subprogram: examine its formals,
19181 -- and result definition in the case of an access_to_function.
19183 Param_Spec := First (Parameter_Specifications (Acc_Subprg));
19184 while Present (Param_Spec) loop
19185 if Nkind (Parameter_Type (Param_Spec)) = N_Access_Definition
19186 and then Mentions_T (Parameter_Type (Param_Spec))
19187 then
19188 return True;
19190 elsif Designates_T (Parameter_Type (Param_Spec)) then
19191 return True;
19192 end if;
19194 Next (Param_Spec);
19195 end loop;
19197 if Nkind (Acc_Subprg) = N_Access_Function_Definition then
19198 if Nkind (Result_Definition (Acc_Subprg)) =
19199 N_Access_Definition
19200 then
19201 return Mentions_T (Result_Definition (Acc_Subprg));
19202 else
19203 return Designates_T (Result_Definition (Acc_Subprg));
19204 end if;
19205 end if;
19207 return False;
19208 end Mentions_T;
19210 -- Start of processing for Check_Anonymous_Access_Components
19212 begin
19213 if No (Comp_List) then
19214 return;
19215 end if;
19217 Comp := First (Component_Items (Comp_List));
19218 while Present (Comp) loop
19219 if Nkind (Comp) = N_Component_Declaration
19220 and then Present
19221 (Access_Definition (Component_Definition (Comp)))
19222 and then
19223 Mentions_T (Access_Definition (Component_Definition (Comp)))
19224 then
19225 Comp_Def := Component_Definition (Comp);
19226 Acc_Def :=
19227 Access_To_Subprogram_Definition
19228 (Access_Definition (Comp_Def));
19230 Build_Incomplete_Type_Declaration;
19231 Anon_Access := Make_Temporary (Loc, 'S');
19233 -- Create a declaration for the anonymous access type: either
19234 -- an access_to_object or an access_to_subprogram.
19236 if Present (Acc_Def) then
19237 if Nkind (Acc_Def) = N_Access_Function_Definition then
19238 Type_Def :=
19239 Make_Access_Function_Definition (Loc,
19240 Parameter_Specifications =>
19241 Parameter_Specifications (Acc_Def),
19242 Result_Definition => Result_Definition (Acc_Def));
19243 else
19244 Type_Def :=
19245 Make_Access_Procedure_Definition (Loc,
19246 Parameter_Specifications =>
19247 Parameter_Specifications (Acc_Def));
19248 end if;
19250 else
19251 Type_Def :=
19252 Make_Access_To_Object_Definition (Loc,
19253 Subtype_Indication =>
19254 Relocate_Node
19255 (Subtype_Mark
19256 (Access_Definition (Comp_Def))));
19258 Set_Constant_Present
19259 (Type_Def, Constant_Present (Access_Definition (Comp_Def)));
19260 Set_All_Present
19261 (Type_Def, All_Present (Access_Definition (Comp_Def)));
19262 end if;
19264 Set_Null_Exclusion_Present
19265 (Type_Def,
19266 Null_Exclusion_Present (Access_Definition (Comp_Def)));
19268 Decl :=
19269 Make_Full_Type_Declaration (Loc,
19270 Defining_Identifier => Anon_Access,
19271 Type_Definition => Type_Def);
19273 Insert_Before (Typ_Decl, Decl);
19274 Analyze (Decl);
19276 -- If an access to subprogram, create the extra formals
19278 if Present (Acc_Def) then
19279 Create_Extra_Formals (Designated_Type (Anon_Access));
19281 -- If an access to object, preserve entity of designated type,
19282 -- for ASIS use, before rewriting the component definition.
19284 else
19285 declare
19286 Desig : Entity_Id;
19288 begin
19289 Desig := Entity (Subtype_Indication (Type_Def));
19291 -- If the access definition is to the current record,
19292 -- the visible entity at this point is an incomplete
19293 -- type. Retrieve the full view to simplify ASIS queries
19295 if Ekind (Desig) = E_Incomplete_Type then
19296 Desig := Full_View (Desig);
19297 end if;
19299 Set_Entity
19300 (Subtype_Mark (Access_Definition (Comp_Def)), Desig);
19301 end;
19302 end if;
19304 Rewrite (Comp_Def,
19305 Make_Component_Definition (Loc,
19306 Subtype_Indication =>
19307 New_Occurrence_Of (Anon_Access, Loc)));
19309 if Ekind (Designated_Type (Anon_Access)) = E_Subprogram_Type then
19310 Set_Ekind (Anon_Access, E_Anonymous_Access_Subprogram_Type);
19311 else
19312 Set_Ekind (Anon_Access, E_Anonymous_Access_Type);
19313 end if;
19315 Set_Is_Local_Anonymous_Access (Anon_Access);
19316 end if;
19318 Next (Comp);
19319 end loop;
19321 if Present (Variant_Part (Comp_List)) then
19322 declare
19323 V : Node_Id;
19324 begin
19325 V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
19326 while Present (V) loop
19327 Check_Anonymous_Access_Components
19328 (Typ_Decl, Typ, Prev, Component_List (V));
19329 Next_Non_Pragma (V);
19330 end loop;
19331 end;
19332 end if;
19333 end Check_Anonymous_Access_Components;
19335 --------------------------------
19336 -- Preanalyze_Spec_Expression --
19337 --------------------------------
19339 procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
19340 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
19341 begin
19342 In_Spec_Expression := True;
19343 Preanalyze_And_Resolve (N, T);
19344 In_Spec_Expression := Save_In_Spec_Expression;
19345 end Preanalyze_Spec_Expression;
19347 -----------------------------
19348 -- Record_Type_Declaration --
19349 -----------------------------
19351 procedure Record_Type_Declaration
19352 (T : Entity_Id;
19353 N : Node_Id;
19354 Prev : Entity_Id)
19356 Def : constant Node_Id := Type_Definition (N);
19357 Is_Tagged : Boolean;
19358 Tag_Comp : Entity_Id;
19360 begin
19361 -- These flags must be initialized before calling Process_Discriminants
19362 -- because this routine makes use of them.
19364 Set_Ekind (T, E_Record_Type);
19365 Set_Etype (T, T);
19366 Init_Size_Align (T);
19367 Set_Interfaces (T, No_Elist);
19368 Set_Stored_Constraint (T, No_Elist);
19370 -- Normal case
19372 if Ada_Version < Ada_2005
19373 or else not Interface_Present (Def)
19374 then
19375 if Limited_Present (Def) then
19376 Check_SPARK_Restriction ("limited is not allowed", N);
19377 end if;
19379 if Abstract_Present (Def) then
19380 Check_SPARK_Restriction ("abstract is not allowed", N);
19381 end if;
19383 -- The flag Is_Tagged_Type might have already been set by
19384 -- Find_Type_Name if it detected an error for declaration T. This
19385 -- arises in the case of private tagged types where the full view
19386 -- omits the word tagged.
19388 Is_Tagged :=
19389 Tagged_Present (Def)
19390 or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
19392 Set_Is_Tagged_Type (T, Is_Tagged);
19393 Set_Is_Limited_Record (T, Limited_Present (Def));
19395 -- Type is abstract if full declaration carries keyword, or if
19396 -- previous partial view did.
19398 Set_Is_Abstract_Type (T, Is_Abstract_Type (T)
19399 or else Abstract_Present (Def));
19401 else
19402 Check_SPARK_Restriction ("interface is not allowed", N);
19404 Is_Tagged := True;
19405 Analyze_Interface_Declaration (T, Def);
19407 if Present (Discriminant_Specifications (N)) then
19408 Error_Msg_N
19409 ("interface types cannot have discriminants",
19410 Defining_Identifier
19411 (First (Discriminant_Specifications (N))));
19412 end if;
19413 end if;
19415 -- First pass: if there are self-referential access components,
19416 -- create the required anonymous access type declarations, and if
19417 -- need be an incomplete type declaration for T itself.
19419 Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
19421 if Ada_Version >= Ada_2005
19422 and then Present (Interface_List (Def))
19423 then
19424 Check_Interfaces (N, Def);
19426 declare
19427 Ifaces_List : Elist_Id;
19429 begin
19430 -- Ada 2005 (AI-251): Collect the list of progenitors that are not
19431 -- already in the parents.
19433 Collect_Interfaces
19434 (T => T,
19435 Ifaces_List => Ifaces_List,
19436 Exclude_Parents => True);
19438 Set_Interfaces (T, Ifaces_List);
19439 end;
19440 end if;
19442 -- Records constitute a scope for the component declarations within.
19443 -- The scope is created prior to the processing of these declarations.
19444 -- Discriminants are processed first, so that they are visible when
19445 -- processing the other components. The Ekind of the record type itself
19446 -- is set to E_Record_Type (subtypes appear as E_Record_Subtype).
19448 -- Enter record scope
19450 Push_Scope (T);
19452 -- If an incomplete or private type declaration was already given for
19453 -- the type, then this scope already exists, and the discriminants have
19454 -- been declared within. We must verify that the full declaration
19455 -- matches the incomplete one.
19457 Check_Or_Process_Discriminants (N, T, Prev);
19459 Set_Is_Constrained (T, not Has_Discriminants (T));
19460 Set_Has_Delayed_Freeze (T, True);
19462 -- For tagged types add a manually analyzed component corresponding
19463 -- to the component _tag, the corresponding piece of tree will be
19464 -- expanded as part of the freezing actions if it is not a CPP_Class.
19466 if Is_Tagged then
19468 -- Do not add the tag unless we are in expansion mode
19470 if Expander_Active then
19471 Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
19472 Enter_Name (Tag_Comp);
19474 Set_Ekind (Tag_Comp, E_Component);
19475 Set_Is_Tag (Tag_Comp);
19476 Set_Is_Aliased (Tag_Comp);
19477 Set_Etype (Tag_Comp, RTE (RE_Tag));
19478 Set_DT_Entry_Count (Tag_Comp, No_Uint);
19479 Set_Original_Record_Component (Tag_Comp, Tag_Comp);
19480 Init_Component_Location (Tag_Comp);
19482 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
19483 -- implemented interfaces.
19485 if Has_Interfaces (T) then
19486 Add_Interface_Tag_Components (N, T);
19487 end if;
19488 end if;
19490 Make_Class_Wide_Type (T);
19491 Set_Direct_Primitive_Operations (T, New_Elmt_List);
19492 end if;
19494 -- We must suppress range checks when processing record components in
19495 -- the presence of discriminants, since we don't want spurious checks to
19496 -- be generated during their analysis, but Suppress_Range_Checks flags
19497 -- must be reset the after processing the record definition.
19499 -- Note: this is the only use of Kill_Range_Checks, and is a bit odd,
19500 -- couldn't we just use the normal range check suppression method here.
19501 -- That would seem cleaner ???
19503 if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
19504 Set_Kill_Range_Checks (T, True);
19505 Record_Type_Definition (Def, Prev);
19506 Set_Kill_Range_Checks (T, False);
19507 else
19508 Record_Type_Definition (Def, Prev);
19509 end if;
19511 -- Exit from record scope
19513 End_Scope;
19515 -- Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
19516 -- the implemented interfaces and associate them an aliased entity.
19518 if Is_Tagged
19519 and then not Is_Empty_List (Interface_List (Def))
19520 then
19521 Derive_Progenitor_Subprograms (T, T);
19522 end if;
19523 end Record_Type_Declaration;
19525 ----------------------------
19526 -- Record_Type_Definition --
19527 ----------------------------
19529 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
19530 Component : Entity_Id;
19531 Ctrl_Components : Boolean := False;
19532 Final_Storage_Only : Boolean;
19533 T : Entity_Id;
19535 begin
19536 if Ekind (Prev_T) = E_Incomplete_Type then
19537 T := Full_View (Prev_T);
19538 else
19539 T := Prev_T;
19540 end if;
19542 -- In SPARK, tagged types and type extensions may only be declared in
19543 -- the specification of library unit packages.
19545 if Present (Def) and then Is_Tagged_Type (T) then
19546 declare
19547 Typ : Node_Id;
19548 Ctxt : Node_Id;
19550 begin
19551 if Nkind (Parent (Def)) = N_Full_Type_Declaration then
19552 Typ := Parent (Def);
19553 else
19554 pragma Assert
19555 (Nkind (Parent (Def)) = N_Derived_Type_Definition);
19556 Typ := Parent (Parent (Def));
19557 end if;
19559 Ctxt := Parent (Typ);
19561 if Nkind (Ctxt) = N_Package_Body
19562 and then Nkind (Parent (Ctxt)) = N_Compilation_Unit
19563 then
19564 Check_SPARK_Restriction
19565 ("type should be defined in package specification", Typ);
19567 elsif Nkind (Ctxt) /= N_Package_Specification
19568 or else Nkind (Parent (Parent (Ctxt))) /= N_Compilation_Unit
19569 then
19570 Check_SPARK_Restriction
19571 ("type should be defined in library unit package", Typ);
19572 end if;
19573 end;
19574 end if;
19576 Final_Storage_Only := not Is_Controlled (T);
19578 -- Ada 2005: check whether an explicit Limited is present in a derived
19579 -- type declaration.
19581 if Nkind (Parent (Def)) = N_Derived_Type_Definition
19582 and then Limited_Present (Parent (Def))
19583 then
19584 Set_Is_Limited_Record (T);
19585 end if;
19587 -- If the component list of a record type is defined by the reserved
19588 -- word null and there is no discriminant part, then the record type has
19589 -- no components and all records of the type are null records (RM 3.7)
19590 -- This procedure is also called to process the extension part of a
19591 -- record extension, in which case the current scope may have inherited
19592 -- components.
19594 if No (Def)
19595 or else No (Component_List (Def))
19596 or else Null_Present (Component_List (Def))
19597 then
19598 if not Is_Tagged_Type (T) then
19599 Check_SPARK_Restriction ("non-tagged record cannot be null", Def);
19600 end if;
19602 else
19603 Analyze_Declarations (Component_Items (Component_List (Def)));
19605 if Present (Variant_Part (Component_List (Def))) then
19606 Check_SPARK_Restriction ("variant part is not allowed", Def);
19607 Analyze (Variant_Part (Component_List (Def)));
19608 end if;
19609 end if;
19611 -- After completing the semantic analysis of the record definition,
19612 -- record components, both new and inherited, are accessible. Set their
19613 -- kind accordingly. Exclude malformed itypes from illegal declarations,
19614 -- whose Ekind may be void.
19616 Component := First_Entity (Current_Scope);
19617 while Present (Component) loop
19618 if Ekind (Component) = E_Void
19619 and then not Is_Itype (Component)
19620 then
19621 Set_Ekind (Component, E_Component);
19622 Init_Component_Location (Component);
19623 end if;
19625 if Has_Task (Etype (Component)) then
19626 Set_Has_Task (T);
19627 end if;
19629 if Ekind (Component) /= E_Component then
19630 null;
19632 -- Do not set Has_Controlled_Component on a class-wide equivalent
19633 -- type. See Make_CW_Equivalent_Type.
19635 elsif not Is_Class_Wide_Equivalent_Type (T)
19636 and then (Has_Controlled_Component (Etype (Component))
19637 or else (Chars (Component) /= Name_uParent
19638 and then Is_Controlled (Etype (Component))))
19639 then
19640 Set_Has_Controlled_Component (T, True);
19641 Final_Storage_Only :=
19642 Final_Storage_Only
19643 and then Finalize_Storage_Only (Etype (Component));
19644 Ctrl_Components := True;
19645 end if;
19647 Next_Entity (Component);
19648 end loop;
19650 -- A Type is Finalize_Storage_Only only if all its controlled components
19651 -- are also.
19653 if Ctrl_Components then
19654 Set_Finalize_Storage_Only (T, Final_Storage_Only);
19655 end if;
19657 -- Place reference to end record on the proper entity, which may
19658 -- be a partial view.
19660 if Present (Def) then
19661 Process_End_Label (Def, 'e', Prev_T);
19662 end if;
19663 end Record_Type_Definition;
19665 ------------------------
19666 -- Replace_Components --
19667 ------------------------
19669 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id) is
19670 function Process (N : Node_Id) return Traverse_Result;
19672 -------------
19673 -- Process --
19674 -------------
19676 function Process (N : Node_Id) return Traverse_Result is
19677 Comp : Entity_Id;
19679 begin
19680 if Nkind (N) = N_Discriminant_Specification then
19681 Comp := First_Discriminant (Typ);
19682 while Present (Comp) loop
19683 if Chars (Comp) = Chars (Defining_Identifier (N)) then
19684 Set_Defining_Identifier (N, Comp);
19685 exit;
19686 end if;
19688 Next_Discriminant (Comp);
19689 end loop;
19691 elsif Nkind (N) = N_Component_Declaration then
19692 Comp := First_Component (Typ);
19693 while Present (Comp) loop
19694 if Chars (Comp) = Chars (Defining_Identifier (N)) then
19695 Set_Defining_Identifier (N, Comp);
19696 exit;
19697 end if;
19699 Next_Component (Comp);
19700 end loop;
19701 end if;
19703 return OK;
19704 end Process;
19706 procedure Replace is new Traverse_Proc (Process);
19708 -- Start of processing for Replace_Components
19710 begin
19711 Replace (Decl);
19712 end Replace_Components;
19714 -------------------------------
19715 -- Set_Completion_Referenced --
19716 -------------------------------
19718 procedure Set_Completion_Referenced (E : Entity_Id) is
19719 begin
19720 -- If in main unit, mark entity that is a completion as referenced,
19721 -- warnings go on the partial view when needed.
19723 if In_Extended_Main_Source_Unit (E) then
19724 Set_Referenced (E);
19725 end if;
19726 end Set_Completion_Referenced;
19728 ---------------------
19729 -- Set_Fixed_Range --
19730 ---------------------
19732 -- The range for fixed-point types is complicated by the fact that we
19733 -- do not know the exact end points at the time of the declaration. This
19734 -- is true for three reasons:
19736 -- A size clause may affect the fudging of the end-points.
19737 -- A small clause may affect the values of the end-points.
19738 -- We try to include the end-points if it does not affect the size.
19740 -- This means that the actual end-points must be established at the
19741 -- point when the type is frozen. Meanwhile, we first narrow the range
19742 -- as permitted (so that it will fit if necessary in a small specified
19743 -- size), and then build a range subtree with these narrowed bounds.
19744 -- Set_Fixed_Range constructs the range from real literal values, and
19745 -- sets the range as the Scalar_Range of the given fixed-point type entity.
19747 -- The parent of this range is set to point to the entity so that it is
19748 -- properly hooked into the tree (unlike normal Scalar_Range entries for
19749 -- other scalar types, which are just pointers to the range in the
19750 -- original tree, this would otherwise be an orphan).
19752 -- The tree is left unanalyzed. When the type is frozen, the processing
19753 -- in Freeze.Freeze_Fixed_Point_Type notices that the range is not
19754 -- analyzed, and uses this as an indication that it should complete
19755 -- work on the range (it will know the final small and size values).
19757 procedure Set_Fixed_Range
19758 (E : Entity_Id;
19759 Loc : Source_Ptr;
19760 Lo : Ureal;
19761 Hi : Ureal)
19763 S : constant Node_Id :=
19764 Make_Range (Loc,
19765 Low_Bound => Make_Real_Literal (Loc, Lo),
19766 High_Bound => Make_Real_Literal (Loc, Hi));
19767 begin
19768 Set_Scalar_Range (E, S);
19769 Set_Parent (S, E);
19771 -- Before the freeze point, the bounds of a fixed point are universal
19772 -- and carry the corresponding type.
19774 Set_Etype (Low_Bound (S), Universal_Real);
19775 Set_Etype (High_Bound (S), Universal_Real);
19776 end Set_Fixed_Range;
19778 ----------------------------------
19779 -- Set_Scalar_Range_For_Subtype --
19780 ----------------------------------
19782 procedure Set_Scalar_Range_For_Subtype
19783 (Def_Id : Entity_Id;
19784 R : Node_Id;
19785 Subt : Entity_Id)
19787 Kind : constant Entity_Kind := Ekind (Def_Id);
19789 begin
19790 -- Defend against previous error
19792 if Nkind (R) = N_Error then
19793 return;
19794 end if;
19796 Set_Scalar_Range (Def_Id, R);
19798 -- We need to link the range into the tree before resolving it so
19799 -- that types that are referenced, including importantly the subtype
19800 -- itself, are properly frozen (Freeze_Expression requires that the
19801 -- expression be properly linked into the tree). Of course if it is
19802 -- already linked in, then we do not disturb the current link.
19804 if No (Parent (R)) then
19805 Set_Parent (R, Def_Id);
19806 end if;
19808 -- Reset the kind of the subtype during analysis of the range, to
19809 -- catch possible premature use in the bounds themselves.
19811 Set_Ekind (Def_Id, E_Void);
19812 Process_Range_Expr_In_Decl (R, Subt);
19813 Set_Ekind (Def_Id, Kind);
19814 end Set_Scalar_Range_For_Subtype;
19816 --------------------------------------------------------
19817 -- Set_Stored_Constraint_From_Discriminant_Constraint --
19818 --------------------------------------------------------
19820 procedure Set_Stored_Constraint_From_Discriminant_Constraint
19821 (E : Entity_Id)
19823 begin
19824 -- Make sure set if encountered during Expand_To_Stored_Constraint
19826 Set_Stored_Constraint (E, No_Elist);
19828 -- Give it the right value
19830 if Is_Constrained (E) and then Has_Discriminants (E) then
19831 Set_Stored_Constraint (E,
19832 Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
19833 end if;
19834 end Set_Stored_Constraint_From_Discriminant_Constraint;
19836 -------------------------------------
19837 -- Signed_Integer_Type_Declaration --
19838 -------------------------------------
19840 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
19841 Implicit_Base : Entity_Id;
19842 Base_Typ : Entity_Id;
19843 Lo_Val : Uint;
19844 Hi_Val : Uint;
19845 Errs : Boolean := False;
19846 Lo : Node_Id;
19847 Hi : Node_Id;
19849 function Can_Derive_From (E : Entity_Id) return Boolean;
19850 -- Determine whether given bounds allow derivation from specified type
19852 procedure Check_Bound (Expr : Node_Id);
19853 -- Check bound to make sure it is integral and static. If not, post
19854 -- appropriate error message and set Errs flag
19856 ---------------------
19857 -- Can_Derive_From --
19858 ---------------------
19860 -- Note we check both bounds against both end values, to deal with
19861 -- strange types like ones with a range of 0 .. -12341234.
19863 function Can_Derive_From (E : Entity_Id) return Boolean is
19864 Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
19865 Hi : constant Uint := Expr_Value (Type_High_Bound (E));
19866 begin
19867 return Lo <= Lo_Val and then Lo_Val <= Hi
19868 and then
19869 Lo <= Hi_Val and then Hi_Val <= Hi;
19870 end Can_Derive_From;
19872 -----------------
19873 -- Check_Bound --
19874 -----------------
19876 procedure Check_Bound (Expr : Node_Id) is
19877 begin
19878 -- If a range constraint is used as an integer type definition, each
19879 -- bound of the range must be defined by a static expression of some
19880 -- integer type, but the two bounds need not have the same integer
19881 -- type (Negative bounds are allowed.) (RM 3.5.4)
19883 if not Is_Integer_Type (Etype (Expr)) then
19884 Error_Msg_N
19885 ("integer type definition bounds must be of integer type", Expr);
19886 Errs := True;
19888 elsif not Is_OK_Static_Expression (Expr) then
19889 Flag_Non_Static_Expr
19890 ("non-static expression used for integer type bound!", Expr);
19891 Errs := True;
19893 -- The bounds are folded into literals, and we set their type to be
19894 -- universal, to avoid typing difficulties: we cannot set the type
19895 -- of the literal to the new type, because this would be a forward
19896 -- reference for the back end, and if the original type is user-
19897 -- defined this can lead to spurious semantic errors (e.g. 2928-003).
19899 else
19900 if Is_Entity_Name (Expr) then
19901 Fold_Uint (Expr, Expr_Value (Expr), True);
19902 end if;
19904 Set_Etype (Expr, Universal_Integer);
19905 end if;
19906 end Check_Bound;
19908 -- Start of processing for Signed_Integer_Type_Declaration
19910 begin
19911 -- Create an anonymous base type
19913 Implicit_Base :=
19914 Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
19916 -- Analyze and check the bounds, they can be of any integer type
19918 Lo := Low_Bound (Def);
19919 Hi := High_Bound (Def);
19921 -- Arbitrarily use Integer as the type if either bound had an error
19923 if Hi = Error or else Lo = Error then
19924 Base_Typ := Any_Integer;
19925 Set_Error_Posted (T, True);
19927 -- Here both bounds are OK expressions
19929 else
19930 Analyze_And_Resolve (Lo, Any_Integer);
19931 Analyze_And_Resolve (Hi, Any_Integer);
19933 Check_Bound (Lo);
19934 Check_Bound (Hi);
19936 if Errs then
19937 Hi := Type_High_Bound (Standard_Long_Long_Integer);
19938 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
19939 end if;
19941 -- Find type to derive from
19943 Lo_Val := Expr_Value (Lo);
19944 Hi_Val := Expr_Value (Hi);
19946 if Can_Derive_From (Standard_Short_Short_Integer) then
19947 Base_Typ := Base_Type (Standard_Short_Short_Integer);
19949 elsif Can_Derive_From (Standard_Short_Integer) then
19950 Base_Typ := Base_Type (Standard_Short_Integer);
19952 elsif Can_Derive_From (Standard_Integer) then
19953 Base_Typ := Base_Type (Standard_Integer);
19955 elsif Can_Derive_From (Standard_Long_Integer) then
19956 Base_Typ := Base_Type (Standard_Long_Integer);
19958 elsif Can_Derive_From (Standard_Long_Long_Integer) then
19959 Base_Typ := Base_Type (Standard_Long_Long_Integer);
19961 else
19962 Base_Typ := Base_Type (Standard_Long_Long_Integer);
19963 Error_Msg_N ("integer type definition bounds out of range", Def);
19964 Hi := Type_High_Bound (Standard_Long_Long_Integer);
19965 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
19966 end if;
19967 end if;
19969 -- Complete both implicit base and declared first subtype entities
19971 Set_Etype (Implicit_Base, Base_Typ);
19972 Set_Size_Info (Implicit_Base, (Base_Typ));
19973 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
19974 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
19976 Set_Ekind (T, E_Signed_Integer_Subtype);
19977 Set_Etype (T, Implicit_Base);
19979 -- In formal verification mode, restrict the base type's range to the
19980 -- minimum allowed by RM 3.5.4, namely the smallest symmetric range
19981 -- around zero with a possible extra negative value that contains the
19982 -- subtype range. Keep Size, RM_Size and First_Rep_Item info, which
19983 -- should not be relied upon in formal verification.
19985 if Strict_Alfa_Mode then
19986 declare
19987 Sym_Hi_Val : Uint;
19988 Sym_Lo_Val : Uint;
19989 Dloc : constant Source_Ptr := Sloc (Def);
19990 Lbound : Node_Id;
19991 Ubound : Node_Id;
19992 Bounds : Node_Id;
19994 begin
19995 -- If the subtype range is empty, the smallest base type range
19996 -- is the symmetric range around zero containing Lo_Val and
19997 -- Hi_Val.
19999 if UI_Gt (Lo_Val, Hi_Val) then
20000 Sym_Hi_Val := UI_Max (UI_Abs (Lo_Val), UI_Abs (Hi_Val));
20001 Sym_Lo_Val := UI_Negate (Sym_Hi_Val);
20003 -- Otherwise, if the subtype range is not empty and Hi_Val has
20004 -- the largest absolute value, Hi_Val is non negative and the
20005 -- smallest base type range is the symmetric range around zero
20006 -- containing Hi_Val.
20008 elsif UI_Le (UI_Abs (Lo_Val), UI_Abs (Hi_Val)) then
20009 Sym_Hi_Val := Hi_Val;
20010 Sym_Lo_Val := UI_Negate (Hi_Val);
20012 -- Otherwise, the subtype range is not empty, Lo_Val has the
20013 -- strictly largest absolute value, Lo_Val is negative and the
20014 -- smallest base type range is the symmetric range around zero
20015 -- with an extra negative value Lo_Val.
20017 else
20018 Sym_Lo_Val := Lo_Val;
20019 Sym_Hi_Val := UI_Sub (UI_Negate (Lo_Val), Uint_1);
20020 end if;
20022 Lbound := Make_Integer_Literal (Dloc, Sym_Lo_Val);
20023 Ubound := Make_Integer_Literal (Dloc, Sym_Hi_Val);
20024 Set_Is_Static_Expression (Lbound);
20025 Set_Is_Static_Expression (Ubound);
20026 Analyze_And_Resolve (Lbound, Any_Integer);
20027 Analyze_And_Resolve (Ubound, Any_Integer);
20029 Bounds := Make_Range (Dloc, Lbound, Ubound);
20030 Set_Etype (Bounds, Base_Typ);
20032 Set_Scalar_Range (Implicit_Base, Bounds);
20033 end;
20035 else
20036 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
20037 end if;
20039 Set_Size_Info (T, (Implicit_Base));
20040 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
20041 Set_Scalar_Range (T, Def);
20042 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
20043 Set_Is_Constrained (T);
20044 end Signed_Integer_Type_Declaration;
20046 end Sem_Ch3;