Merge from mainline (167278:168000).
[official-gcc/graphite-test-results.git] / gcc / ada / sem_ch3.adb
blob6587916206d7350a2ab3ac535112b9fba763f57d
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-2010, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Debug; use Debug;
30 with Elists; use Elists;
31 with Einfo; use Einfo;
32 with Errout; use Errout;
33 with Eval_Fat; use Eval_Fat;
34 with Exp_Ch3; use Exp_Ch3;
35 with Exp_Ch9; use Exp_Ch9;
36 with Exp_Disp; use Exp_Disp;
37 with Exp_Dist; use Exp_Dist;
38 with Exp_Tss; use Exp_Tss;
39 with Exp_Util; use Exp_Util;
40 with Fname; use Fname;
41 with Freeze; use Freeze;
42 with Itypes; use Itypes;
43 with Layout; use Layout;
44 with Lib; use Lib;
45 with Lib.Xref; use Lib.Xref;
46 with Namet; use Namet;
47 with Nmake; use Nmake;
48 with Opt; use Opt;
49 with Restrict; use Restrict;
50 with Rident; use Rident;
51 with Rtsfind; use Rtsfind;
52 with Sem; use Sem;
53 with Sem_Aux; use Sem_Aux;
54 with Sem_Case; use Sem_Case;
55 with Sem_Cat; use Sem_Cat;
56 with Sem_Ch6; use Sem_Ch6;
57 with Sem_Ch7; use Sem_Ch7;
58 with Sem_Ch8; use Sem_Ch8;
59 with Sem_Ch13; use Sem_Ch13;
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 and is
589 -- a power of two (implementation restriction).
591 procedure New_Concatenation_Op (Typ : Entity_Id);
592 -- Create an abbreviated declaration for an operator in order to
593 -- materialize concatenation on array types.
595 procedure Ordinary_Fixed_Point_Type_Declaration
596 (T : Entity_Id;
597 Def : Node_Id);
598 -- Create a new ordinary fixed point type, and apply the constraint to
599 -- obtain subtype of it.
601 procedure Prepare_Private_Subtype_Completion
602 (Id : Entity_Id;
603 Related_Nod : Node_Id);
604 -- Id is a subtype of some private type. Creates the full declaration
605 -- associated with Id whenever possible, i.e. when the full declaration
606 -- of the base type is already known. Records each subtype into
607 -- Private_Dependents of the base type.
609 procedure Process_Incomplete_Dependents
610 (N : Node_Id;
611 Full_T : Entity_Id;
612 Inc_T : Entity_Id);
613 -- Process all entities that depend on an incomplete type. There include
614 -- subtypes, subprogram types that mention the incomplete type in their
615 -- profiles, and subprogram with access parameters that designate the
616 -- incomplete type.
618 -- Inc_T is the defining identifier of an incomplete type declaration, its
619 -- Ekind is E_Incomplete_Type.
621 -- N is the corresponding N_Full_Type_Declaration for Inc_T.
623 -- Full_T is N's defining identifier.
625 -- Subtypes of incomplete types with discriminants are completed when the
626 -- parent type is. This is simpler than private subtypes, because they can
627 -- only appear in the same scope, and there is no need to exchange views.
628 -- Similarly, access_to_subprogram types may have a parameter or a return
629 -- type that is an incomplete type, and that must be replaced with the
630 -- full type.
632 -- If the full type is tagged, subprogram with access parameters that
633 -- designated the incomplete may be primitive operations of the full type,
634 -- and have to be processed accordingly.
636 procedure Process_Real_Range_Specification (Def : Node_Id);
637 -- Given the type definition for a real type, this procedure processes and
638 -- checks the real range specification of this type definition if one is
639 -- present. If errors are found, error messages are posted, and the
640 -- Real_Range_Specification of Def is reset to Empty.
642 procedure Record_Type_Declaration
643 (T : Entity_Id;
644 N : Node_Id;
645 Prev : Entity_Id);
646 -- Process a record type declaration (for both untagged and tagged
647 -- records). Parameters T and N are exactly like in procedure
648 -- Derived_Type_Declaration, except that no flag Is_Completion is needed
649 -- for this routine. If this is the completion of an incomplete type
650 -- declaration, Prev is the entity of the incomplete declaration, used for
651 -- cross-referencing. Otherwise Prev = T.
653 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id);
654 -- This routine is used to process the actual record type definition (both
655 -- for untagged and tagged records). Def is a record type definition node.
656 -- This procedure analyzes the components in this record type definition.
657 -- Prev_T is the entity for the enclosing record type. It is provided so
658 -- that its Has_Task flag can be set if any of the component have Has_Task
659 -- set. If the declaration is the completion of an incomplete type
660 -- declaration, Prev_T is the original incomplete type, whose full view is
661 -- the record type.
663 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id);
664 -- Subsidiary to Build_Derived_Record_Type. For untagged records, we
665 -- build a copy of the declaration tree of the parent, and we create
666 -- independently the list of components for the derived type. Semantic
667 -- information uses the component entities, but record representation
668 -- clauses are validated on the declaration tree. This procedure replaces
669 -- discriminants and components in the declaration with those that have
670 -- been created by Inherit_Components.
672 procedure Set_Fixed_Range
673 (E : Entity_Id;
674 Loc : Source_Ptr;
675 Lo : Ureal;
676 Hi : Ureal);
677 -- Build a range node with the given bounds and set it as the Scalar_Range
678 -- of the given fixed-point type entity. Loc is the source location used
679 -- for the constructed range. See body for further details.
681 procedure Set_Scalar_Range_For_Subtype
682 (Def_Id : Entity_Id;
683 R : Node_Id;
684 Subt : Entity_Id);
685 -- This routine is used to set the scalar range field for a subtype given
686 -- Def_Id, the entity for the subtype, and R, the range expression for the
687 -- scalar range. Subt provides the parent subtype to be used to analyze,
688 -- resolve, and check the given range.
690 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id);
691 -- Create a new signed integer entity, and apply the constraint to obtain
692 -- the required first named subtype of this type.
694 procedure Set_Stored_Constraint_From_Discriminant_Constraint
695 (E : Entity_Id);
696 -- E is some record type. This routine computes E's Stored_Constraint
697 -- from its Discriminant_Constraint.
699 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id);
700 -- Check that an entity in a list of progenitors is an interface,
701 -- emit error otherwise.
703 -----------------------
704 -- Access_Definition --
705 -----------------------
707 function Access_Definition
708 (Related_Nod : Node_Id;
709 N : Node_Id) return Entity_Id
711 Loc : constant Source_Ptr := Sloc (Related_Nod);
712 Anon_Type : Entity_Id;
713 Anon_Scope : Entity_Id;
714 Desig_Type : Entity_Id;
715 Decl : Entity_Id;
716 Enclosing_Prot_Type : Entity_Id := Empty;
718 begin
719 if Is_Entry (Current_Scope)
720 and then Is_Task_Type (Etype (Scope (Current_Scope)))
721 then
722 Error_Msg_N ("task entries cannot have access parameters", N);
723 return Empty;
724 end if;
726 -- Ada 2005: for an object declaration the corresponding anonymous
727 -- type is declared in the current scope.
729 -- If the access definition is the return type of another access to
730 -- function, scope is the current one, because it is the one of the
731 -- current type declaration.
733 if Nkind_In (Related_Nod, N_Object_Declaration,
734 N_Access_Function_Definition)
735 then
736 Anon_Scope := Current_Scope;
738 -- For the anonymous function result case, retrieve the scope of the
739 -- function specification's associated entity rather than using the
740 -- current scope. The current scope will be the function itself if the
741 -- formal part is currently being analyzed, but will be the parent scope
742 -- in the case of a parameterless function, and we always want to use
743 -- the function's parent scope. Finally, if the function is a child
744 -- unit, we must traverse the tree to retrieve the proper entity.
746 elsif Nkind (Related_Nod) = N_Function_Specification
747 and then Nkind (Parent (N)) /= N_Parameter_Specification
748 then
749 -- If the current scope is a protected type, the anonymous access
750 -- is associated with one of the protected operations, and must
751 -- be available in the scope that encloses the protected declaration.
752 -- Otherwise the type is in the scope enclosing the subprogram.
754 -- If the function has formals, The return type of a subprogram
755 -- declaration is analyzed in the scope of the subprogram (see
756 -- Process_Formals) and thus the protected type, if present, is
757 -- the scope of the current function scope.
759 if Ekind (Current_Scope) = E_Protected_Type then
760 Enclosing_Prot_Type := Current_Scope;
762 elsif Ekind (Current_Scope) = E_Function
763 and then Ekind (Scope (Current_Scope)) = E_Protected_Type
764 then
765 Enclosing_Prot_Type := Scope (Current_Scope);
766 end if;
768 if Present (Enclosing_Prot_Type) then
769 Anon_Scope := Scope (Enclosing_Prot_Type);
771 else
772 Anon_Scope := Scope (Defining_Entity (Related_Nod));
773 end if;
775 else
776 -- For access formals, access components, and access discriminants,
777 -- the scope is that of the enclosing declaration,
779 Anon_Scope := Scope (Current_Scope);
780 end if;
782 Anon_Type :=
783 Create_Itype
784 (E_Anonymous_Access_Type, Related_Nod, Scope_Id => Anon_Scope);
786 if All_Present (N)
787 and then Ada_Version >= Ada_2005
788 then
789 Error_Msg_N ("ALL is not permitted for anonymous access types", N);
790 end if;
792 -- Ada 2005 (AI-254): In case of anonymous access to subprograms call
793 -- the corresponding semantic routine
795 if Present (Access_To_Subprogram_Definition (N)) then
796 Access_Subprogram_Declaration
797 (T_Name => Anon_Type,
798 T_Def => Access_To_Subprogram_Definition (N));
800 if Ekind (Anon_Type) = E_Access_Protected_Subprogram_Type then
801 Set_Ekind
802 (Anon_Type, E_Anonymous_Access_Protected_Subprogram_Type);
803 else
804 Set_Ekind
805 (Anon_Type, E_Anonymous_Access_Subprogram_Type);
806 end if;
808 Set_Can_Use_Internal_Rep
809 (Anon_Type, not Always_Compatible_Rep_On_Target);
811 -- If the anonymous access is associated with a protected operation
812 -- create a reference to it after the enclosing protected definition
813 -- because the itype will be used in the subsequent bodies.
815 if Ekind (Current_Scope) = E_Protected_Type then
816 Build_Itype_Reference (Anon_Type, Parent (Current_Scope));
817 end if;
819 return Anon_Type;
820 end if;
822 Find_Type (Subtype_Mark (N));
823 Desig_Type := Entity (Subtype_Mark (N));
825 Set_Directly_Designated_Type (Anon_Type, Desig_Type);
826 Set_Etype (Anon_Type, Anon_Type);
828 -- Make sure the anonymous access type has size and alignment fields
829 -- set, as required by gigi. This is necessary in the case of the
830 -- Task_Body_Procedure.
832 if not Has_Private_Component (Desig_Type) then
833 Layout_Type (Anon_Type);
834 end if;
836 -- Ada 2005 (AI-231): Ada 2005 semantics for anonymous access differs
837 -- from Ada 95 semantics. In Ada 2005, anonymous access must specify if
838 -- the null value is allowed. In Ada 95 the null value is never allowed.
840 if Ada_Version >= Ada_2005 then
841 Set_Can_Never_Be_Null (Anon_Type, Null_Exclusion_Present (N));
842 else
843 Set_Can_Never_Be_Null (Anon_Type, True);
844 end if;
846 -- The anonymous access type is as public as the discriminated type or
847 -- subprogram that defines it. It is imported (for back-end purposes)
848 -- if the designated type is.
850 Set_Is_Public (Anon_Type, Is_Public (Scope (Anon_Type)));
852 -- Ada 2005 (AI-231): Propagate the access-constant attribute
854 Set_Is_Access_Constant (Anon_Type, Constant_Present (N));
856 -- The context is either a subprogram declaration, object declaration,
857 -- or an access discriminant, in a private or a full type declaration.
858 -- In the case of a subprogram, if the designated type is incomplete,
859 -- the operation will be a primitive operation of the full type, to be
860 -- updated subsequently. If the type is imported through a limited_with
861 -- clause, the subprogram is not a primitive operation of the type
862 -- (which is declared elsewhere in some other scope).
864 if Ekind (Desig_Type) = E_Incomplete_Type
865 and then not From_With_Type (Desig_Type)
866 and then Is_Overloadable (Current_Scope)
867 then
868 Append_Elmt (Current_Scope, Private_Dependents (Desig_Type));
869 Set_Has_Delayed_Freeze (Current_Scope);
870 end if;
872 -- Ada 2005: if the designated type is an interface that may contain
873 -- tasks, create a Master entity for the declaration. This must be done
874 -- before expansion of the full declaration, because the declaration may
875 -- include an expression that is an allocator, whose expansion needs the
876 -- proper Master for the created tasks.
878 if Nkind (Related_Nod) = N_Object_Declaration
879 and then Expander_Active
880 then
881 if Is_Interface (Desig_Type)
882 and then Is_Limited_Record (Desig_Type)
883 then
884 Build_Class_Wide_Master (Anon_Type);
886 -- Similarly, if the type is an anonymous access that designates
887 -- tasks, create a master entity for it in the current context.
889 elsif Has_Task (Desig_Type)
890 and then Comes_From_Source (Related_Nod)
891 and then not Restriction_Active (No_Task_Hierarchy)
892 then
893 if not Has_Master_Entity (Current_Scope) then
894 Decl :=
895 Make_Object_Declaration (Loc,
896 Defining_Identifier =>
897 Make_Defining_Identifier (Loc, Name_uMaster),
898 Constant_Present => True,
899 Object_Definition =>
900 New_Reference_To (RTE (RE_Master_Id), Loc),
901 Expression =>
902 Make_Explicit_Dereference (Loc,
903 New_Reference_To (RTE (RE_Current_Master), Loc)));
905 Insert_Before (Related_Nod, Decl);
906 Analyze (Decl);
908 Set_Master_Id (Anon_Type, Defining_Identifier (Decl));
909 Set_Has_Master_Entity (Current_Scope);
910 else
911 Build_Master_Renaming (Related_Nod, Anon_Type);
912 end if;
913 end if;
914 end if;
916 -- For a private component of a protected type, it is imperative that
917 -- the back-end elaborate the type immediately after the protected
918 -- declaration, because this type will be used in the declarations
919 -- created for the component within each protected body, so we must
920 -- create an itype reference for it now.
922 if Nkind (Parent (Related_Nod)) = N_Protected_Definition then
923 Build_Itype_Reference (Anon_Type, Parent (Parent (Related_Nod)));
925 -- Similarly, if the access definition is the return result of a
926 -- function, create an itype reference for it because it will be used
927 -- within the function body. For a regular function that is not a
928 -- compilation unit, insert reference after the declaration. For a
929 -- protected operation, insert it after the enclosing protected type
930 -- declaration. In either case, do not create a reference for a type
931 -- obtained through a limited_with clause, because this would introduce
932 -- semantic dependencies.
934 -- Similarly, do not create a reference if the designated type is a
935 -- generic formal, because no use of it will reach the backend.
937 elsif Nkind (Related_Nod) = N_Function_Specification
938 and then not From_With_Type (Desig_Type)
939 and then not Is_Generic_Type (Desig_Type)
940 then
941 if Present (Enclosing_Prot_Type) then
942 Build_Itype_Reference (Anon_Type, Parent (Enclosing_Prot_Type));
944 elsif Is_List_Member (Parent (Related_Nod))
945 and then Nkind (Parent (N)) /= N_Parameter_Specification
946 then
947 Build_Itype_Reference (Anon_Type, Parent (Related_Nod));
948 end if;
950 -- Finally, create an itype reference for an object declaration of an
951 -- anonymous access type. This is strictly necessary only for deferred
952 -- constants, but in any case will avoid out-of-scope problems in the
953 -- back-end.
955 elsif Nkind (Related_Nod) = N_Object_Declaration then
956 Build_Itype_Reference (Anon_Type, Related_Nod);
957 end if;
959 return Anon_Type;
960 end Access_Definition;
962 -----------------------------------
963 -- Access_Subprogram_Declaration --
964 -----------------------------------
966 procedure Access_Subprogram_Declaration
967 (T_Name : Entity_Id;
968 T_Def : Node_Id)
971 procedure Check_For_Premature_Usage (Def : Node_Id);
972 -- Check that type T_Name is not used, directly or recursively, as a
973 -- parameter or a return type in Def. Def is either a subtype, an
974 -- access_definition, or an access_to_subprogram_definition.
976 -------------------------------
977 -- Check_For_Premature_Usage --
978 -------------------------------
980 procedure Check_For_Premature_Usage (Def : Node_Id) is
981 Param : Node_Id;
983 begin
984 -- Check for a subtype mark
986 if Nkind (Def) in N_Has_Etype then
987 if Etype (Def) = T_Name then
988 Error_Msg_N
989 ("type& cannot be used before end of its declaration", Def);
990 end if;
992 -- If this is not a subtype, then this is an access_definition
994 elsif Nkind (Def) = N_Access_Definition then
995 if Present (Access_To_Subprogram_Definition (Def)) then
996 Check_For_Premature_Usage
997 (Access_To_Subprogram_Definition (Def));
998 else
999 Check_For_Premature_Usage (Subtype_Mark (Def));
1000 end if;
1002 -- The only cases left are N_Access_Function_Definition and
1003 -- N_Access_Procedure_Definition.
1005 else
1006 if Present (Parameter_Specifications (Def)) then
1007 Param := First (Parameter_Specifications (Def));
1008 while Present (Param) loop
1009 Check_For_Premature_Usage (Parameter_Type (Param));
1010 Param := Next (Param);
1011 end loop;
1012 end if;
1014 if Nkind (Def) = N_Access_Function_Definition then
1015 Check_For_Premature_Usage (Result_Definition (Def));
1016 end if;
1017 end if;
1018 end Check_For_Premature_Usage;
1020 -- Local variables
1022 Formals : constant List_Id := Parameter_Specifications (T_Def);
1023 Formal : Entity_Id;
1024 D_Ityp : Node_Id;
1025 Desig_Type : constant Entity_Id :=
1026 Create_Itype (E_Subprogram_Type, Parent (T_Def));
1028 -- Start of processing for Access_Subprogram_Declaration
1030 begin
1031 -- Associate the Itype node with the inner full-type declaration or
1032 -- subprogram spec or entry body. This is required to handle nested
1033 -- anonymous declarations. For example:
1035 -- procedure P
1036 -- (X : access procedure
1037 -- (Y : access procedure
1038 -- (Z : access T)))
1040 D_Ityp := Associated_Node_For_Itype (Desig_Type);
1041 while not (Nkind_In (D_Ityp, N_Full_Type_Declaration,
1042 N_Private_Type_Declaration,
1043 N_Private_Extension_Declaration,
1044 N_Procedure_Specification,
1045 N_Function_Specification,
1046 N_Entry_Body)
1048 or else
1049 Nkind_In (D_Ityp, N_Object_Declaration,
1050 N_Object_Renaming_Declaration,
1051 N_Formal_Object_Declaration,
1052 N_Formal_Type_Declaration,
1053 N_Task_Type_Declaration,
1054 N_Protected_Type_Declaration))
1055 loop
1056 D_Ityp := Parent (D_Ityp);
1057 pragma Assert (D_Ityp /= Empty);
1058 end loop;
1060 Set_Associated_Node_For_Itype (Desig_Type, D_Ityp);
1062 if Nkind_In (D_Ityp, N_Procedure_Specification,
1063 N_Function_Specification)
1064 then
1065 Set_Scope (Desig_Type, Scope (Defining_Entity (D_Ityp)));
1067 elsif Nkind_In (D_Ityp, N_Full_Type_Declaration,
1068 N_Object_Declaration,
1069 N_Object_Renaming_Declaration,
1070 N_Formal_Type_Declaration)
1071 then
1072 Set_Scope (Desig_Type, Scope (Defining_Identifier (D_Ityp)));
1073 end if;
1075 if Nkind (T_Def) = N_Access_Function_Definition then
1076 if Nkind (Result_Definition (T_Def)) = N_Access_Definition then
1077 declare
1078 Acc : constant Node_Id := Result_Definition (T_Def);
1080 begin
1081 if Present (Access_To_Subprogram_Definition (Acc))
1082 and then
1083 Protected_Present (Access_To_Subprogram_Definition (Acc))
1084 then
1085 Set_Etype
1086 (Desig_Type,
1087 Replace_Anonymous_Access_To_Protected_Subprogram
1088 (T_Def));
1090 else
1091 Set_Etype
1092 (Desig_Type,
1093 Access_Definition (T_Def, Result_Definition (T_Def)));
1094 end if;
1095 end;
1097 else
1098 Analyze (Result_Definition (T_Def));
1100 declare
1101 Typ : constant Entity_Id := Entity (Result_Definition (T_Def));
1103 begin
1104 -- If a null exclusion is imposed on the result type, then
1105 -- create a null-excluding itype (an access subtype) and use
1106 -- it as the function's Etype.
1108 if Is_Access_Type (Typ)
1109 and then Null_Exclusion_In_Return_Present (T_Def)
1110 then
1111 Set_Etype (Desig_Type,
1112 Create_Null_Excluding_Itype
1113 (T => Typ,
1114 Related_Nod => T_Def,
1115 Scope_Id => Current_Scope));
1117 else
1118 if From_With_Type (Typ) then
1120 -- AI05-151: Incomplete types are allowed in all basic
1121 -- declarations, including access to subprograms.
1123 if Ada_Version >= Ada_2012 then
1124 null;
1126 else
1127 Error_Msg_NE
1128 ("illegal use of incomplete type&",
1129 Result_Definition (T_Def), Typ);
1130 end if;
1132 elsif Ekind (Current_Scope) = E_Package
1133 and then In_Private_Part (Current_Scope)
1134 then
1135 if Ekind (Typ) = E_Incomplete_Type then
1136 Append_Elmt (Desig_Type, Private_Dependents (Typ));
1138 elsif Is_Class_Wide_Type (Typ)
1139 and then Ekind (Etype (Typ)) = E_Incomplete_Type
1140 then
1141 Append_Elmt
1142 (Desig_Type, Private_Dependents (Etype (Typ)));
1143 end if;
1144 end if;
1146 Set_Etype (Desig_Type, Typ);
1147 end if;
1148 end;
1149 end if;
1151 if not (Is_Type (Etype (Desig_Type))) then
1152 Error_Msg_N
1153 ("expect type in function specification",
1154 Result_Definition (T_Def));
1155 end if;
1157 else
1158 Set_Etype (Desig_Type, Standard_Void_Type);
1159 end if;
1161 if Present (Formals) then
1162 Push_Scope (Desig_Type);
1164 -- A bit of a kludge here. These kludges will be removed when Itypes
1165 -- have proper parent pointers to their declarations???
1167 -- Kludge 1) Link defining_identifier of formals. Required by
1168 -- First_Formal to provide its functionality.
1170 declare
1171 F : Node_Id;
1173 begin
1174 F := First (Formals);
1175 while Present (F) loop
1176 if No (Parent (Defining_Identifier (F))) then
1177 Set_Parent (Defining_Identifier (F), F);
1178 end if;
1180 Next (F);
1181 end loop;
1182 end;
1184 Process_Formals (Formals, Parent (T_Def));
1186 -- Kludge 2) End_Scope requires that the parent pointer be set to
1187 -- something reasonable, but Itypes don't have parent pointers. So
1188 -- we set it and then unset it ???
1190 Set_Parent (Desig_Type, T_Name);
1191 End_Scope;
1192 Set_Parent (Desig_Type, Empty);
1193 end if;
1195 -- Check for premature usage of the type being defined
1197 Check_For_Premature_Usage (T_Def);
1199 -- The return type and/or any parameter type may be incomplete. Mark
1200 -- the subprogram_type as depending on the incomplete type, so that
1201 -- it can be updated when the full type declaration is seen. This
1202 -- only applies to incomplete types declared in some enclosing scope,
1203 -- not to limited views from other packages.
1205 if Present (Formals) then
1206 Formal := First_Formal (Desig_Type);
1207 while Present (Formal) loop
1208 if Ekind (Formal) /= E_In_Parameter
1209 and then Nkind (T_Def) = N_Access_Function_Definition
1210 then
1211 Error_Msg_N ("functions can only have IN parameters", Formal);
1212 end if;
1214 if Ekind (Etype (Formal)) = E_Incomplete_Type
1215 and then In_Open_Scopes (Scope (Etype (Formal)))
1216 then
1217 Append_Elmt (Desig_Type, Private_Dependents (Etype (Formal)));
1218 Set_Has_Delayed_Freeze (Desig_Type);
1219 end if;
1221 Next_Formal (Formal);
1222 end loop;
1223 end if;
1225 -- If the return type is incomplete, this is legal as long as the
1226 -- type is declared in the current scope and will be completed in
1227 -- it (rather than being part of limited view).
1229 if Ekind (Etype (Desig_Type)) = E_Incomplete_Type
1230 and then not Has_Delayed_Freeze (Desig_Type)
1231 and then In_Open_Scopes (Scope (Etype (Desig_Type)))
1232 then
1233 Append_Elmt (Desig_Type, Private_Dependents (Etype (Desig_Type)));
1234 Set_Has_Delayed_Freeze (Desig_Type);
1235 end if;
1237 Check_Delayed_Subprogram (Desig_Type);
1239 if Protected_Present (T_Def) then
1240 Set_Ekind (T_Name, E_Access_Protected_Subprogram_Type);
1241 Set_Convention (Desig_Type, Convention_Protected);
1242 else
1243 Set_Ekind (T_Name, E_Access_Subprogram_Type);
1244 end if;
1246 Set_Can_Use_Internal_Rep (T_Name, not Always_Compatible_Rep_On_Target);
1248 Set_Etype (T_Name, T_Name);
1249 Init_Size_Align (T_Name);
1250 Set_Directly_Designated_Type (T_Name, Desig_Type);
1252 -- Ada 2005 (AI-231): Propagate the null-excluding attribute
1254 Set_Can_Never_Be_Null (T_Name, Null_Exclusion_Present (T_Def));
1256 Check_Restriction (No_Access_Subprograms, T_Def);
1257 end Access_Subprogram_Declaration;
1259 ----------------------------
1260 -- Access_Type_Declaration --
1261 ----------------------------
1263 procedure Access_Type_Declaration (T : Entity_Id; Def : Node_Id) is
1264 S : constant Node_Id := Subtype_Indication (Def);
1265 P : constant Node_Id := Parent (Def);
1266 begin
1267 -- Check for permissible use of incomplete type
1269 if Nkind (S) /= N_Subtype_Indication then
1270 Analyze (S);
1272 if Ekind (Root_Type (Entity (S))) = E_Incomplete_Type then
1273 Set_Directly_Designated_Type (T, Entity (S));
1274 else
1275 Set_Directly_Designated_Type (T,
1276 Process_Subtype (S, P, T, 'P'));
1277 end if;
1279 else
1280 Set_Directly_Designated_Type (T,
1281 Process_Subtype (S, P, T, 'P'));
1282 end if;
1284 if All_Present (Def) or Constant_Present (Def) then
1285 Set_Ekind (T, E_General_Access_Type);
1286 else
1287 Set_Ekind (T, E_Access_Type);
1288 end if;
1290 if Base_Type (Designated_Type (T)) = T then
1291 Error_Msg_N ("access type cannot designate itself", S);
1293 -- In Ada 2005, the type may have a limited view through some unit
1294 -- in its own context, allowing the following circularity that cannot
1295 -- be detected earlier
1297 elsif Is_Class_Wide_Type (Designated_Type (T))
1298 and then Etype (Designated_Type (T)) = T
1299 then
1300 Error_Msg_N
1301 ("access type cannot designate its own classwide type", S);
1303 -- Clean up indication of tagged status to prevent cascaded errors
1305 Set_Is_Tagged_Type (T, False);
1306 end if;
1308 Set_Etype (T, T);
1310 -- If the type has appeared already in a with_type clause, it is
1311 -- frozen and the pointer size is already set. Else, initialize.
1313 if not From_With_Type (T) then
1314 Init_Size_Align (T);
1315 end if;
1317 -- Note that Has_Task is always false, since the access type itself
1318 -- is not a task type. See Einfo for more description on this point.
1319 -- Exactly the same consideration applies to Has_Controlled_Component.
1321 Set_Has_Task (T, False);
1322 Set_Has_Controlled_Component (T, False);
1324 -- Initialize Associated_Final_Chain explicitly to Empty, to avoid
1325 -- problems where an incomplete view of this entity has been previously
1326 -- established by a limited with and an overlaid version of this field
1327 -- (Stored_Constraint) was initialized for the incomplete view.
1329 Set_Associated_Final_Chain (T, Empty);
1331 -- Ada 2005 (AI-231): Propagate the null-excluding and access-constant
1332 -- attributes
1334 Set_Can_Never_Be_Null (T, Null_Exclusion_Present (Def));
1335 Set_Is_Access_Constant (T, Constant_Present (Def));
1336 end Access_Type_Declaration;
1338 ----------------------------------
1339 -- Add_Interface_Tag_Components --
1340 ----------------------------------
1342 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id) is
1343 Loc : constant Source_Ptr := Sloc (N);
1344 L : List_Id;
1345 Last_Tag : Node_Id;
1347 procedure Add_Tag (Iface : Entity_Id);
1348 -- Add tag for one of the progenitor interfaces
1350 -------------
1351 -- Add_Tag --
1352 -------------
1354 procedure Add_Tag (Iface : Entity_Id) is
1355 Decl : Node_Id;
1356 Def : Node_Id;
1357 Tag : Entity_Id;
1358 Offset : Entity_Id;
1360 begin
1361 pragma Assert (Is_Tagged_Type (Iface)
1362 and then Is_Interface (Iface));
1364 -- This is a reasonable place to propagate predicates
1366 if Has_Predicates (Iface) then
1367 Set_Has_Predicates (Typ);
1368 end if;
1370 Def :=
1371 Make_Component_Definition (Loc,
1372 Aliased_Present => True,
1373 Subtype_Indication =>
1374 New_Occurrence_Of (RTE (RE_Interface_Tag), Loc));
1376 Tag := Make_Temporary (Loc, 'V');
1378 Decl :=
1379 Make_Component_Declaration (Loc,
1380 Defining_Identifier => Tag,
1381 Component_Definition => Def);
1383 Analyze_Component_Declaration (Decl);
1385 Set_Analyzed (Decl);
1386 Set_Ekind (Tag, E_Component);
1387 Set_Is_Tag (Tag);
1388 Set_Is_Aliased (Tag);
1389 Set_Related_Type (Tag, Iface);
1390 Init_Component_Location (Tag);
1392 pragma Assert (Is_Frozen (Iface));
1394 Set_DT_Entry_Count (Tag,
1395 DT_Entry_Count (First_Entity (Iface)));
1397 if No (Last_Tag) then
1398 Prepend (Decl, L);
1399 else
1400 Insert_After (Last_Tag, Decl);
1401 end if;
1403 Last_Tag := Decl;
1405 -- If the ancestor has discriminants we need to give special support
1406 -- to store the offset_to_top value of the secondary dispatch tables.
1407 -- For this purpose we add a supplementary component just after the
1408 -- field that contains the tag associated with each secondary DT.
1410 if Typ /= Etype (Typ)
1411 and then Has_Discriminants (Etype (Typ))
1412 then
1413 Def :=
1414 Make_Component_Definition (Loc,
1415 Subtype_Indication =>
1416 New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
1418 Offset := Make_Temporary (Loc, 'V');
1420 Decl :=
1421 Make_Component_Declaration (Loc,
1422 Defining_Identifier => Offset,
1423 Component_Definition => Def);
1425 Analyze_Component_Declaration (Decl);
1427 Set_Analyzed (Decl);
1428 Set_Ekind (Offset, E_Component);
1429 Set_Is_Aliased (Offset);
1430 Set_Related_Type (Offset, Iface);
1431 Init_Component_Location (Offset);
1432 Insert_After (Last_Tag, Decl);
1433 Last_Tag := Decl;
1434 end if;
1435 end Add_Tag;
1437 -- Local variables
1439 Elmt : Elmt_Id;
1440 Ext : Node_Id;
1441 Comp : Node_Id;
1443 -- Start of processing for Add_Interface_Tag_Components
1445 begin
1446 if not RTE_Available (RE_Interface_Tag) then
1447 Error_Msg
1448 ("(Ada 2005) interface types not supported by this run-time!",
1449 Sloc (N));
1450 return;
1451 end if;
1453 if Ekind (Typ) /= E_Record_Type
1454 or else (Is_Concurrent_Record_Type (Typ)
1455 and then Is_Empty_List (Abstract_Interface_List (Typ)))
1456 or else (not Is_Concurrent_Record_Type (Typ)
1457 and then No (Interfaces (Typ))
1458 and then Is_Empty_Elmt_List (Interfaces (Typ)))
1459 then
1460 return;
1461 end if;
1463 -- Find the current last tag
1465 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1466 Ext := Record_Extension_Part (Type_Definition (N));
1467 else
1468 pragma Assert (Nkind (Type_Definition (N)) = N_Record_Definition);
1469 Ext := Type_Definition (N);
1470 end if;
1472 Last_Tag := Empty;
1474 if not (Present (Component_List (Ext))) then
1475 Set_Null_Present (Ext, False);
1476 L := New_List;
1477 Set_Component_List (Ext,
1478 Make_Component_List (Loc,
1479 Component_Items => L,
1480 Null_Present => False));
1481 else
1482 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1483 L := Component_Items
1484 (Component_List
1485 (Record_Extension_Part
1486 (Type_Definition (N))));
1487 else
1488 L := Component_Items
1489 (Component_List
1490 (Type_Definition (N)));
1491 end if;
1493 -- Find the last tag component
1495 Comp := First (L);
1496 while Present (Comp) loop
1497 if Nkind (Comp) = N_Component_Declaration
1498 and then Is_Tag (Defining_Identifier (Comp))
1499 then
1500 Last_Tag := Comp;
1501 end if;
1503 Next (Comp);
1504 end loop;
1505 end if;
1507 -- At this point L references the list of components and Last_Tag
1508 -- references the current last tag (if any). Now we add the tag
1509 -- corresponding with all the interfaces that are not implemented
1510 -- by the parent.
1512 if Present (Interfaces (Typ)) then
1513 Elmt := First_Elmt (Interfaces (Typ));
1514 while Present (Elmt) loop
1515 Add_Tag (Node (Elmt));
1516 Next_Elmt (Elmt);
1517 end loop;
1518 end if;
1519 end Add_Interface_Tag_Components;
1521 -------------------------------------
1522 -- Add_Internal_Interface_Entities --
1523 -------------------------------------
1525 procedure Add_Internal_Interface_Entities (Tagged_Type : Entity_Id) is
1526 Elmt : Elmt_Id;
1527 Iface : Entity_Id;
1528 Iface_Elmt : Elmt_Id;
1529 Iface_Prim : Entity_Id;
1530 Ifaces_List : Elist_Id;
1531 New_Subp : Entity_Id := Empty;
1532 Prim : Entity_Id;
1533 Restore_Scope : Boolean := False;
1535 begin
1536 pragma Assert (Ada_Version >= Ada_2005
1537 and then Is_Record_Type (Tagged_Type)
1538 and then Is_Tagged_Type (Tagged_Type)
1539 and then Has_Interfaces (Tagged_Type)
1540 and then not Is_Interface (Tagged_Type));
1542 -- Ensure that the internal entities are added to the scope of the type
1544 if Scope (Tagged_Type) /= Current_Scope then
1545 Push_Scope (Scope (Tagged_Type));
1546 Restore_Scope := True;
1547 end if;
1549 Collect_Interfaces (Tagged_Type, Ifaces_List);
1551 Iface_Elmt := First_Elmt (Ifaces_List);
1552 while Present (Iface_Elmt) loop
1553 Iface := Node (Iface_Elmt);
1555 -- Originally we excluded here from this processing interfaces that
1556 -- are parents of Tagged_Type because their primitives are located
1557 -- in the primary dispatch table (and hence no auxiliary internal
1558 -- entities are required to handle secondary dispatch tables in such
1559 -- case). However, these auxiliary entities are also required to
1560 -- handle derivations of interfaces in formals of generics (see
1561 -- Derive_Subprograms).
1563 Elmt := First_Elmt (Primitive_Operations (Iface));
1564 while Present (Elmt) loop
1565 Iface_Prim := Node (Elmt);
1567 if not Is_Predefined_Dispatching_Operation (Iface_Prim) then
1568 Prim :=
1569 Find_Primitive_Covering_Interface
1570 (Tagged_Type => Tagged_Type,
1571 Iface_Prim => Iface_Prim);
1573 pragma Assert (Present (Prim));
1575 -- Ada 2012 (AI05-0197): If the name of the covering primitive
1576 -- differs from the name of the interface primitive then it is
1577 -- a private primitive inherited from a parent type. In such
1578 -- case, given that Tagged_Type covers the interface, the
1579 -- inherited private primitive becomes visible. For such
1580 -- purpose we add a new entity that renames the inherited
1581 -- private primitive.
1583 if Chars (Prim) /= Chars (Iface_Prim) then
1584 pragma Assert (Has_Suffix (Prim, 'P'));
1585 Derive_Subprogram
1586 (New_Subp => New_Subp,
1587 Parent_Subp => Iface_Prim,
1588 Derived_Type => Tagged_Type,
1589 Parent_Type => Iface);
1590 Set_Alias (New_Subp, Prim);
1591 Set_Is_Abstract_Subprogram
1592 (New_Subp, Is_Abstract_Subprogram (Prim));
1593 end if;
1595 Derive_Subprogram
1596 (New_Subp => New_Subp,
1597 Parent_Subp => Iface_Prim,
1598 Derived_Type => Tagged_Type,
1599 Parent_Type => Iface);
1601 -- Ada 2005 (AI-251): Decorate internal entity Iface_Subp
1602 -- associated with interface types. These entities are
1603 -- only registered in the list of primitives of its
1604 -- corresponding tagged type because they are only used
1605 -- to fill the contents of the secondary dispatch tables.
1606 -- Therefore they are removed from the homonym chains.
1608 Set_Is_Hidden (New_Subp);
1609 Set_Is_Internal (New_Subp);
1610 Set_Alias (New_Subp, Prim);
1611 Set_Is_Abstract_Subprogram
1612 (New_Subp, Is_Abstract_Subprogram (Prim));
1613 Set_Interface_Alias (New_Subp, Iface_Prim);
1615 -- Internal entities associated with interface types are
1616 -- only registered in the list of primitives of the tagged
1617 -- type. They are only used to fill the contents of the
1618 -- secondary dispatch tables. Therefore they are not needed
1619 -- in the homonym chains.
1621 Remove_Homonym (New_Subp);
1623 -- Hidden entities associated with interfaces must have set
1624 -- the Has_Delay_Freeze attribute to ensure that, in case of
1625 -- locally defined tagged types (or compiling with static
1626 -- dispatch tables generation disabled) the corresponding
1627 -- entry of the secondary dispatch table is filled when
1628 -- such an entity is frozen.
1630 Set_Has_Delayed_Freeze (New_Subp);
1631 end if;
1633 Next_Elmt (Elmt);
1634 end loop;
1636 Next_Elmt (Iface_Elmt);
1637 end loop;
1639 if Restore_Scope then
1640 Pop_Scope;
1641 end if;
1642 end Add_Internal_Interface_Entities;
1644 -----------------------------------
1645 -- Analyze_Component_Declaration --
1646 -----------------------------------
1648 procedure Analyze_Component_Declaration (N : Node_Id) is
1649 Id : constant Entity_Id := Defining_Identifier (N);
1650 E : constant Node_Id := Expression (N);
1651 T : Entity_Id;
1652 P : Entity_Id;
1654 function Contains_POC (Constr : Node_Id) return Boolean;
1655 -- Determines whether a constraint uses the discriminant of a record
1656 -- type thus becoming a per-object constraint (POC).
1658 function Is_Known_Limited (Typ : Entity_Id) return Boolean;
1659 -- Typ is the type of the current component, check whether this type is
1660 -- a limited type. Used to validate declaration against that of
1661 -- enclosing record.
1663 ------------------
1664 -- Contains_POC --
1665 ------------------
1667 function Contains_POC (Constr : Node_Id) return Boolean is
1668 begin
1669 -- Prevent cascaded errors
1671 if Error_Posted (Constr) then
1672 return False;
1673 end if;
1675 case Nkind (Constr) is
1676 when N_Attribute_Reference =>
1677 return
1678 Attribute_Name (Constr) = Name_Access
1679 and then Prefix (Constr) = Scope (Entity (Prefix (Constr)));
1681 when N_Discriminant_Association =>
1682 return Denotes_Discriminant (Expression (Constr));
1684 when N_Identifier =>
1685 return Denotes_Discriminant (Constr);
1687 when N_Index_Or_Discriminant_Constraint =>
1688 declare
1689 IDC : Node_Id;
1691 begin
1692 IDC := First (Constraints (Constr));
1693 while Present (IDC) loop
1695 -- One per-object constraint is sufficient
1697 if Contains_POC (IDC) then
1698 return True;
1699 end if;
1701 Next (IDC);
1702 end loop;
1704 return False;
1705 end;
1707 when N_Range =>
1708 return Denotes_Discriminant (Low_Bound (Constr))
1709 or else
1710 Denotes_Discriminant (High_Bound (Constr));
1712 when N_Range_Constraint =>
1713 return Denotes_Discriminant (Range_Expression (Constr));
1715 when others =>
1716 return False;
1718 end case;
1719 end Contains_POC;
1721 ----------------------
1722 -- Is_Known_Limited --
1723 ----------------------
1725 function Is_Known_Limited (Typ : Entity_Id) return Boolean is
1726 P : constant Entity_Id := Etype (Typ);
1727 R : constant Entity_Id := Root_Type (Typ);
1729 begin
1730 if Is_Limited_Record (Typ) then
1731 return True;
1733 -- If the root type is limited (and not a limited interface)
1734 -- so is the current type
1736 elsif Is_Limited_Record (R)
1737 and then
1738 (not Is_Interface (R)
1739 or else not Is_Limited_Interface (R))
1740 then
1741 return True;
1743 -- Else the type may have a limited interface progenitor, but a
1744 -- limited record parent.
1746 elsif R /= P
1747 and then Is_Limited_Record (P)
1748 then
1749 return True;
1751 else
1752 return False;
1753 end if;
1754 end Is_Known_Limited;
1756 -- Start of processing for Analyze_Component_Declaration
1758 begin
1759 Generate_Definition (Id);
1760 Enter_Name (Id);
1762 if Present (Subtype_Indication (Component_Definition (N))) then
1763 T := Find_Type_Of_Object
1764 (Subtype_Indication (Component_Definition (N)), N);
1766 -- Ada 2005 (AI-230): Access Definition case
1768 else
1769 pragma Assert (Present
1770 (Access_Definition (Component_Definition (N))));
1772 T := Access_Definition
1773 (Related_Nod => N,
1774 N => Access_Definition (Component_Definition (N)));
1775 Set_Is_Local_Anonymous_Access (T);
1777 -- Ada 2005 (AI-254)
1779 if Present (Access_To_Subprogram_Definition
1780 (Access_Definition (Component_Definition (N))))
1781 and then Protected_Present (Access_To_Subprogram_Definition
1782 (Access_Definition
1783 (Component_Definition (N))))
1784 then
1785 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1786 end if;
1787 end if;
1789 -- If the subtype is a constrained subtype of the enclosing record,
1790 -- (which must have a partial view) the back-end does not properly
1791 -- handle the recursion. Rewrite the component declaration with an
1792 -- explicit subtype indication, which is acceptable to Gigi. We can copy
1793 -- the tree directly because side effects have already been removed from
1794 -- discriminant constraints.
1796 if Ekind (T) = E_Access_Subtype
1797 and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
1798 and then Comes_From_Source (T)
1799 and then Nkind (Parent (T)) = N_Subtype_Declaration
1800 and then Etype (Directly_Designated_Type (T)) = Current_Scope
1801 then
1802 Rewrite
1803 (Subtype_Indication (Component_Definition (N)),
1804 New_Copy_Tree (Subtype_Indication (Parent (T))));
1805 T := Find_Type_Of_Object
1806 (Subtype_Indication (Component_Definition (N)), N);
1807 end if;
1809 -- If the component declaration includes a default expression, then we
1810 -- check that the component is not of a limited type (RM 3.7(5)),
1811 -- and do the special preanalysis of the expression (see section on
1812 -- "Handling of Default and Per-Object Expressions" in the spec of
1813 -- package Sem).
1815 if Present (E) then
1816 Preanalyze_Spec_Expression (E, T);
1817 Check_Initialization (T, E);
1819 if Ada_Version >= Ada_2005
1820 and then Ekind (T) = E_Anonymous_Access_Type
1821 and then Etype (E) /= Any_Type
1822 then
1823 -- Check RM 3.9.2(9): "if the expected type for an expression is
1824 -- an anonymous access-to-specific tagged type, then the object
1825 -- designated by the expression shall not be dynamically tagged
1826 -- unless it is a controlling operand in a call on a dispatching
1827 -- operation"
1829 if Is_Tagged_Type (Directly_Designated_Type (T))
1830 and then
1831 Ekind (Directly_Designated_Type (T)) /= E_Class_Wide_Type
1832 and then
1833 Ekind (Directly_Designated_Type (Etype (E))) =
1834 E_Class_Wide_Type
1835 then
1836 Error_Msg_N
1837 ("access to specific tagged type required (RM 3.9.2(9))", E);
1838 end if;
1840 -- (Ada 2005: AI-230): Accessibility check for anonymous
1841 -- components
1843 if Type_Access_Level (Etype (E)) > Type_Access_Level (T) then
1844 Error_Msg_N
1845 ("expression has deeper access level than component " &
1846 "(RM 3.10.2 (12.2))", E);
1847 end if;
1849 -- The initialization expression is a reference to an access
1850 -- discriminant. The type of the discriminant is always deeper
1851 -- than any access type.
1853 if Ekind (Etype (E)) = E_Anonymous_Access_Type
1854 and then Is_Entity_Name (E)
1855 and then Ekind (Entity (E)) = E_In_Parameter
1856 and then Present (Discriminal_Link (Entity (E)))
1857 then
1858 Error_Msg_N
1859 ("discriminant has deeper accessibility level than target",
1861 end if;
1862 end if;
1863 end if;
1865 -- The parent type may be a private view with unknown discriminants,
1866 -- and thus unconstrained. Regular components must be constrained.
1868 if Is_Indefinite_Subtype (T) and then Chars (Id) /= Name_uParent then
1869 if Is_Class_Wide_Type (T) then
1870 Error_Msg_N
1871 ("class-wide subtype with unknown discriminants" &
1872 " in component declaration",
1873 Subtype_Indication (Component_Definition (N)));
1874 else
1875 Error_Msg_N
1876 ("unconstrained subtype in component declaration",
1877 Subtype_Indication (Component_Definition (N)));
1878 end if;
1880 -- Components cannot be abstract, except for the special case of
1881 -- the _Parent field (case of extending an abstract tagged type)
1883 elsif Is_Abstract_Type (T) and then Chars (Id) /= Name_uParent then
1884 Error_Msg_N ("type of a component cannot be abstract", N);
1885 end if;
1887 Set_Etype (Id, T);
1888 Set_Is_Aliased (Id, Aliased_Present (Component_Definition (N)));
1890 -- The component declaration may have a per-object constraint, set
1891 -- the appropriate flag in the defining identifier of the subtype.
1893 if Present (Subtype_Indication (Component_Definition (N))) then
1894 declare
1895 Sindic : constant Node_Id :=
1896 Subtype_Indication (Component_Definition (N));
1897 begin
1898 if Nkind (Sindic) = N_Subtype_Indication
1899 and then Present (Constraint (Sindic))
1900 and then Contains_POC (Constraint (Sindic))
1901 then
1902 Set_Has_Per_Object_Constraint (Id);
1903 end if;
1904 end;
1905 end if;
1907 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
1908 -- out some static checks.
1910 if Ada_Version >= Ada_2005
1911 and then Can_Never_Be_Null (T)
1912 then
1913 Null_Exclusion_Static_Checks (N);
1914 end if;
1916 -- If this component is private (or depends on a private type), flag the
1917 -- record type to indicate that some operations are not available.
1919 P := Private_Component (T);
1921 if Present (P) then
1923 -- Check for circular definitions
1925 if P = Any_Type then
1926 Set_Etype (Id, Any_Type);
1928 -- There is a gap in the visibility of operations only if the
1929 -- component type is not defined in the scope of the record type.
1931 elsif Scope (P) = Scope (Current_Scope) then
1932 null;
1934 elsif Is_Limited_Type (P) then
1935 Set_Is_Limited_Composite (Current_Scope);
1937 else
1938 Set_Is_Private_Composite (Current_Scope);
1939 end if;
1940 end if;
1942 if P /= Any_Type
1943 and then Is_Limited_Type (T)
1944 and then Chars (Id) /= Name_uParent
1945 and then Is_Tagged_Type (Current_Scope)
1946 then
1947 if Is_Derived_Type (Current_Scope)
1948 and then not Is_Known_Limited (Current_Scope)
1949 then
1950 Error_Msg_N
1951 ("extension of nonlimited type cannot have limited components",
1954 if Is_Interface (Root_Type (Current_Scope)) then
1955 Error_Msg_N
1956 ("\limitedness is not inherited from limited interface", N);
1957 Error_Msg_N ("\add LIMITED to type indication", N);
1958 end if;
1960 Explain_Limited_Type (T, N);
1961 Set_Etype (Id, Any_Type);
1962 Set_Is_Limited_Composite (Current_Scope, False);
1964 elsif not Is_Derived_Type (Current_Scope)
1965 and then not Is_Limited_Record (Current_Scope)
1966 and then not Is_Concurrent_Type (Current_Scope)
1967 then
1968 Error_Msg_N
1969 ("nonlimited tagged type cannot have limited components", N);
1970 Explain_Limited_Type (T, N);
1971 Set_Etype (Id, Any_Type);
1972 Set_Is_Limited_Composite (Current_Scope, False);
1973 end if;
1974 end if;
1976 Set_Original_Record_Component (Id, Id);
1977 Analyze_Aspect_Specifications (N, Id, Aspect_Specifications (N));
1978 end Analyze_Component_Declaration;
1980 --------------------------
1981 -- Analyze_Declarations --
1982 --------------------------
1984 procedure Analyze_Declarations (L : List_Id) is
1985 D : Node_Id;
1986 Freeze_From : Entity_Id := Empty;
1987 Next_Node : Node_Id;
1989 procedure Adjust_D;
1990 -- Adjust D not to include implicit label declarations, since these
1991 -- have strange Sloc values that result in elaboration check problems.
1992 -- (They have the sloc of the label as found in the source, and that
1993 -- is ahead of the current declarative part).
1995 --------------
1996 -- Adjust_D --
1997 --------------
1999 procedure Adjust_D is
2000 begin
2001 while Present (Prev (D))
2002 and then Nkind (D) = N_Implicit_Label_Declaration
2003 loop
2004 Prev (D);
2005 end loop;
2006 end Adjust_D;
2008 -- Start of processing for Analyze_Declarations
2010 begin
2011 D := First (L);
2012 while Present (D) loop
2014 -- Complete analysis of declaration
2016 Analyze (D);
2017 Next_Node := Next (D);
2019 if No (Freeze_From) then
2020 Freeze_From := First_Entity (Current_Scope);
2021 end if;
2023 -- At the end of a declarative part, freeze remaining entities
2024 -- declared in it. The end of the visible declarations of package
2025 -- specification is not the end of a declarative part if private
2026 -- declarations are present. The end of a package declaration is a
2027 -- freezing point only if it a library package. A task definition or
2028 -- protected type definition is not a freeze point either. Finally,
2029 -- we do not freeze entities in generic scopes, because there is no
2030 -- code generated for them and freeze nodes will be generated for
2031 -- the instance.
2033 -- The end of a package instantiation is not a freeze point, but
2034 -- for now we make it one, because the generic body is inserted
2035 -- (currently) immediately after. Generic instantiations will not
2036 -- be a freeze point once delayed freezing of bodies is implemented.
2037 -- (This is needed in any case for early instantiations ???).
2039 if No (Next_Node) then
2040 if Nkind_In (Parent (L), N_Component_List,
2041 N_Task_Definition,
2042 N_Protected_Definition)
2043 then
2044 null;
2046 elsif Nkind (Parent (L)) /= N_Package_Specification then
2047 if Nkind (Parent (L)) = N_Package_Body then
2048 Freeze_From := First_Entity (Current_Scope);
2049 end if;
2051 Adjust_D;
2052 Freeze_All (Freeze_From, D);
2053 Freeze_From := Last_Entity (Current_Scope);
2055 elsif Scope (Current_Scope) /= Standard_Standard
2056 and then not Is_Child_Unit (Current_Scope)
2057 and then No (Generic_Parent (Parent (L)))
2058 then
2059 null;
2061 elsif L /= Visible_Declarations (Parent (L))
2062 or else No (Private_Declarations (Parent (L)))
2063 or else Is_Empty_List (Private_Declarations (Parent (L)))
2064 then
2065 Adjust_D;
2066 Freeze_All (Freeze_From, D);
2067 Freeze_From := Last_Entity (Current_Scope);
2068 end if;
2070 -- If next node is a body then freeze all types before the body.
2071 -- An exception occurs for some expander-generated bodies. If these
2072 -- are generated at places where in general language rules would not
2073 -- allow a freeze point, then we assume that the expander has
2074 -- explicitly checked that all required types are properly frozen,
2075 -- and we do not cause general freezing here. This special circuit
2076 -- is used when the encountered body is marked as having already
2077 -- been analyzed.
2079 -- In all other cases (bodies that come from source, and expander
2080 -- generated bodies that have not been analyzed yet), freeze all
2081 -- types now. Note that in the latter case, the expander must take
2082 -- care to attach the bodies at a proper place in the tree so as to
2083 -- not cause unwanted freezing at that point.
2085 elsif not Analyzed (Next_Node)
2086 and then (Nkind_In (Next_Node, N_Subprogram_Body,
2087 N_Entry_Body,
2088 N_Package_Body,
2089 N_Protected_Body,
2090 N_Task_Body)
2091 or else
2092 Nkind (Next_Node) in N_Body_Stub)
2093 then
2094 Adjust_D;
2095 Freeze_All (Freeze_From, D);
2096 Freeze_From := Last_Entity (Current_Scope);
2097 end if;
2099 D := Next_Node;
2100 end loop;
2102 -- One more thing to do, we need to scan the declarations to check
2103 -- for any precondition/postcondition pragmas (Pre/Post aspects have
2104 -- by this stage been converted into corresponding pragmas). It is
2105 -- at this point that we analyze the expressions in such pragmas,
2106 -- to implement the delayed visibility requirement.
2108 declare
2109 Decl : Node_Id;
2110 Spec : Node_Id;
2111 Sent : Entity_Id;
2112 Prag : Node_Id;
2114 begin
2115 Decl := First (L);
2116 while Present (Decl) loop
2117 if Nkind (Original_Node (Decl)) = N_Subprogram_Declaration then
2118 Spec := Specification (Original_Node (Decl));
2119 Sent := Defining_Unit_Name (Spec);
2120 Prag := Spec_PPC_List (Sent);
2121 while Present (Prag) loop
2122 Analyze_PPC_In_Decl_Part (Prag, Sent);
2123 Prag := Next_Pragma (Prag);
2124 end loop;
2125 end if;
2127 Next (Decl);
2128 end loop;
2129 end;
2130 end Analyze_Declarations;
2132 -----------------------------------
2133 -- Analyze_Full_Type_Declaration --
2134 -----------------------------------
2136 procedure Analyze_Full_Type_Declaration (N : Node_Id) is
2137 Def : constant Node_Id := Type_Definition (N);
2138 Def_Id : constant Entity_Id := Defining_Identifier (N);
2139 T : Entity_Id;
2140 Prev : Entity_Id;
2142 Is_Remote : constant Boolean :=
2143 (Is_Remote_Types (Current_Scope)
2144 or else Is_Remote_Call_Interface (Current_Scope))
2145 and then not (In_Private_Part (Current_Scope)
2146 or else In_Package_Body (Current_Scope));
2148 procedure Check_Ops_From_Incomplete_Type;
2149 -- If there is a tagged incomplete partial view of the type, transfer
2150 -- its operations to the full view, and indicate that the type of the
2151 -- controlling parameter (s) is this full view.
2153 ------------------------------------
2154 -- Check_Ops_From_Incomplete_Type --
2155 ------------------------------------
2157 procedure Check_Ops_From_Incomplete_Type is
2158 Elmt : Elmt_Id;
2159 Formal : Entity_Id;
2160 Op : Entity_Id;
2162 begin
2163 if Prev /= T
2164 and then Ekind (Prev) = E_Incomplete_Type
2165 and then Is_Tagged_Type (Prev)
2166 and then Is_Tagged_Type (T)
2167 then
2168 Elmt := First_Elmt (Primitive_Operations (Prev));
2169 while Present (Elmt) loop
2170 Op := Node (Elmt);
2171 Prepend_Elmt (Op, Primitive_Operations (T));
2173 Formal := First_Formal (Op);
2174 while Present (Formal) loop
2175 if Etype (Formal) = Prev then
2176 Set_Etype (Formal, T);
2177 end if;
2179 Next_Formal (Formal);
2180 end loop;
2182 if Etype (Op) = Prev then
2183 Set_Etype (Op, T);
2184 end if;
2186 Next_Elmt (Elmt);
2187 end loop;
2188 end if;
2189 end Check_Ops_From_Incomplete_Type;
2191 -- Start of processing for Analyze_Full_Type_Declaration
2193 begin
2194 Prev := Find_Type_Name (N);
2196 -- The full view, if present, now points to the current type
2198 -- Ada 2005 (AI-50217): If the type was previously decorated when
2199 -- imported through a LIMITED WITH clause, it appears as incomplete
2200 -- but has no full view.
2202 if Ekind (Prev) = E_Incomplete_Type
2203 and then Present (Full_View (Prev))
2204 then
2205 T := Full_View (Prev);
2206 else
2207 T := Prev;
2208 end if;
2210 Set_Is_Pure (T, Is_Pure (Current_Scope));
2212 -- We set the flag Is_First_Subtype here. It is needed to set the
2213 -- corresponding flag for the Implicit class-wide-type created
2214 -- during tagged types processing.
2216 Set_Is_First_Subtype (T, True);
2218 -- Only composite types other than array types are allowed to have
2219 -- discriminants.
2221 case Nkind (Def) is
2223 -- For derived types, the rule will be checked once we've figured
2224 -- out the parent type.
2226 when N_Derived_Type_Definition =>
2227 null;
2229 -- For record types, discriminants are allowed
2231 when N_Record_Definition =>
2232 null;
2234 when others =>
2235 if Present (Discriminant_Specifications (N)) then
2236 Error_Msg_N
2237 ("elementary or array type cannot have discriminants",
2238 Defining_Identifier
2239 (First (Discriminant_Specifications (N))));
2240 end if;
2241 end case;
2243 -- Elaborate the type definition according to kind, and generate
2244 -- subsidiary (implicit) subtypes where needed. We skip this if it was
2245 -- already done (this happens during the reanalysis that follows a call
2246 -- to the high level optimizer).
2248 if not Analyzed (T) then
2249 Set_Analyzed (T);
2251 case Nkind (Def) is
2253 when N_Access_To_Subprogram_Definition =>
2254 Access_Subprogram_Declaration (T, Def);
2256 -- If this is a remote access to subprogram, we must create the
2257 -- equivalent fat pointer type, and related subprograms.
2259 if Is_Remote then
2260 Process_Remote_AST_Declaration (N);
2261 end if;
2263 -- Validate categorization rule against access type declaration
2264 -- usually a violation in Pure unit, Shared_Passive unit.
2266 Validate_Access_Type_Declaration (T, N);
2268 when N_Access_To_Object_Definition =>
2269 Access_Type_Declaration (T, Def);
2271 -- Validate categorization rule against access type declaration
2272 -- usually a violation in Pure unit, Shared_Passive unit.
2274 Validate_Access_Type_Declaration (T, N);
2276 -- If we are in a Remote_Call_Interface package and define a
2277 -- RACW, then calling stubs and specific stream attributes
2278 -- must be added.
2280 if Is_Remote
2281 and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
2282 then
2283 Add_RACW_Features (Def_Id);
2284 end if;
2286 -- Set no strict aliasing flag if config pragma seen
2288 if Opt.No_Strict_Aliasing then
2289 Set_No_Strict_Aliasing (Base_Type (Def_Id));
2290 end if;
2292 when N_Array_Type_Definition =>
2293 Array_Type_Declaration (T, Def);
2295 when N_Derived_Type_Definition =>
2296 Derived_Type_Declaration (T, N, T /= Def_Id);
2298 when N_Enumeration_Type_Definition =>
2299 Enumeration_Type_Declaration (T, Def);
2301 when N_Floating_Point_Definition =>
2302 Floating_Point_Type_Declaration (T, Def);
2304 when N_Decimal_Fixed_Point_Definition =>
2305 Decimal_Fixed_Point_Type_Declaration (T, Def);
2307 when N_Ordinary_Fixed_Point_Definition =>
2308 Ordinary_Fixed_Point_Type_Declaration (T, Def);
2310 when N_Signed_Integer_Type_Definition =>
2311 Signed_Integer_Type_Declaration (T, Def);
2313 when N_Modular_Type_Definition =>
2314 Modular_Type_Declaration (T, Def);
2316 when N_Record_Definition =>
2317 Record_Type_Declaration (T, N, Prev);
2319 -- If declaration has a parse error, nothing to elaborate.
2321 when N_Error =>
2322 null;
2324 when others =>
2325 raise Program_Error;
2327 end case;
2328 end if;
2330 if Etype (T) = Any_Type then
2331 return;
2332 end if;
2334 -- Some common processing for all types
2336 Set_Depends_On_Private (T, Has_Private_Component (T));
2337 Check_Ops_From_Incomplete_Type;
2339 -- Both the declared entity, and its anonymous base type if one
2340 -- was created, need freeze nodes allocated.
2342 declare
2343 B : constant Entity_Id := Base_Type (T);
2345 begin
2346 -- In the case where the base type differs from the first subtype, we
2347 -- pre-allocate a freeze node, and set the proper link to the first
2348 -- subtype. Freeze_Entity will use this preallocated freeze node when
2349 -- it freezes the entity.
2351 -- This does not apply if the base type is a generic type, whose
2352 -- declaration is independent of the current derived definition.
2354 if B /= T and then not Is_Generic_Type (B) then
2355 Ensure_Freeze_Node (B);
2356 Set_First_Subtype_Link (Freeze_Node (B), T);
2357 end if;
2359 -- A type that is imported through a limited_with clause cannot
2360 -- generate any code, and thus need not be frozen. However, an access
2361 -- type with an imported designated type needs a finalization list,
2362 -- which may be referenced in some other package that has non-limited
2363 -- visibility on the designated type. Thus we must create the
2364 -- finalization list at the point the access type is frozen, to
2365 -- prevent unsatisfied references at link time.
2367 if not From_With_Type (T) or else Is_Access_Type (T) then
2368 Set_Has_Delayed_Freeze (T);
2369 end if;
2370 end;
2372 -- Case where T is the full declaration of some private type which has
2373 -- been swapped in Defining_Identifier (N).
2375 if T /= Def_Id and then Is_Private_Type (Def_Id) then
2376 Process_Full_View (N, T, Def_Id);
2378 -- Record the reference. The form of this is a little strange, since
2379 -- the full declaration has been swapped in. So the first parameter
2380 -- here represents the entity to which a reference is made which is
2381 -- the "real" entity, i.e. the one swapped in, and the second
2382 -- parameter provides the reference location.
2384 -- Also, we want to kill Has_Pragma_Unreferenced temporarily here
2385 -- since we don't want a complaint about the full type being an
2386 -- unwanted reference to the private type
2388 declare
2389 B : constant Boolean := Has_Pragma_Unreferenced (T);
2390 begin
2391 Set_Has_Pragma_Unreferenced (T, False);
2392 Generate_Reference (T, T, 'c');
2393 Set_Has_Pragma_Unreferenced (T, B);
2394 end;
2396 Set_Completion_Referenced (Def_Id);
2398 -- For completion of incomplete type, process incomplete dependents
2399 -- and always mark the full type as referenced (it is the incomplete
2400 -- type that we get for any real reference).
2402 elsif Ekind (Prev) = E_Incomplete_Type then
2403 Process_Incomplete_Dependents (N, T, Prev);
2404 Generate_Reference (Prev, Def_Id, 'c');
2405 Set_Completion_Referenced (Def_Id);
2407 -- If not private type or incomplete type completion, this is a real
2408 -- definition of a new entity, so record it.
2410 else
2411 Generate_Definition (Def_Id);
2412 end if;
2414 if Chars (Scope (Def_Id)) = Name_System
2415 and then Chars (Def_Id) = Name_Address
2416 and then Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (N)))
2417 then
2418 Set_Is_Descendent_Of_Address (Def_Id);
2419 Set_Is_Descendent_Of_Address (Base_Type (Def_Id));
2420 Set_Is_Descendent_Of_Address (Prev);
2421 end if;
2423 Set_Optimize_Alignment_Flags (Def_Id);
2424 Check_Eliminated (Def_Id);
2426 Analyze_Aspect_Specifications (N, Def_Id, Aspect_Specifications (N));
2427 end Analyze_Full_Type_Declaration;
2429 ----------------------------------
2430 -- Analyze_Incomplete_Type_Decl --
2431 ----------------------------------
2433 procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
2434 F : constant Boolean := Is_Pure (Current_Scope);
2435 T : Entity_Id;
2437 begin
2438 Generate_Definition (Defining_Identifier (N));
2440 -- Process an incomplete declaration. The identifier must not have been
2441 -- declared already in the scope. However, an incomplete declaration may
2442 -- appear in the private part of a package, for a private type that has
2443 -- already been declared.
2445 -- In this case, the discriminants (if any) must match
2447 T := Find_Type_Name (N);
2449 Set_Ekind (T, E_Incomplete_Type);
2450 Init_Size_Align (T);
2451 Set_Is_First_Subtype (T, True);
2452 Set_Etype (T, T);
2454 -- Ada 2005 (AI-326): Minimum decoration to give support to tagged
2455 -- incomplete types.
2457 if Tagged_Present (N) then
2458 Set_Is_Tagged_Type (T);
2459 Make_Class_Wide_Type (T);
2460 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2461 end if;
2463 Push_Scope (T);
2465 Set_Stored_Constraint (T, No_Elist);
2467 if Present (Discriminant_Specifications (N)) then
2468 Process_Discriminants (N);
2469 end if;
2471 End_Scope;
2473 -- If the type has discriminants, non-trivial subtypes may be
2474 -- declared before the full view of the type. The full views of those
2475 -- subtypes will be built after the full view of the type.
2477 Set_Private_Dependents (T, New_Elmt_List);
2478 Set_Is_Pure (T, F);
2479 end Analyze_Incomplete_Type_Decl;
2481 -----------------------------------
2482 -- Analyze_Interface_Declaration --
2483 -----------------------------------
2485 procedure Analyze_Interface_Declaration (T : Entity_Id; Def : Node_Id) is
2486 CW : constant Entity_Id := Class_Wide_Type (T);
2488 begin
2489 Set_Is_Tagged_Type (T);
2491 Set_Is_Limited_Record (T, Limited_Present (Def)
2492 or else Task_Present (Def)
2493 or else Protected_Present (Def)
2494 or else Synchronized_Present (Def));
2496 -- Type is abstract if full declaration carries keyword, or if previous
2497 -- partial view did.
2499 Set_Is_Abstract_Type (T);
2500 Set_Is_Interface (T);
2502 -- Type is a limited interface if it includes the keyword limited, task,
2503 -- protected, or synchronized.
2505 Set_Is_Limited_Interface
2506 (T, Limited_Present (Def)
2507 or else Protected_Present (Def)
2508 or else Synchronized_Present (Def)
2509 or else Task_Present (Def));
2511 Set_Interfaces (T, New_Elmt_List);
2512 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2514 -- Complete the decoration of the class-wide entity if it was already
2515 -- built (i.e. during the creation of the limited view)
2517 if Present (CW) then
2518 Set_Is_Interface (CW);
2519 Set_Is_Limited_Interface (CW, Is_Limited_Interface (T));
2520 end if;
2522 -- Check runtime support for synchronized interfaces
2524 if VM_Target = No_VM
2525 and then (Is_Task_Interface (T)
2526 or else Is_Protected_Interface (T)
2527 or else Is_Synchronized_Interface (T))
2528 and then not RTE_Available (RE_Select_Specific_Data)
2529 then
2530 Error_Msg_CRT ("synchronized interfaces", T);
2531 end if;
2532 end Analyze_Interface_Declaration;
2534 -----------------------------
2535 -- Analyze_Itype_Reference --
2536 -----------------------------
2538 -- Nothing to do. This node is placed in the tree only for the benefit of
2539 -- back end processing, and has no effect on the semantic processing.
2541 procedure Analyze_Itype_Reference (N : Node_Id) is
2542 begin
2543 pragma Assert (Is_Itype (Itype (N)));
2544 null;
2545 end Analyze_Itype_Reference;
2547 --------------------------------
2548 -- Analyze_Number_Declaration --
2549 --------------------------------
2551 procedure Analyze_Number_Declaration (N : Node_Id) is
2552 Id : constant Entity_Id := Defining_Identifier (N);
2553 E : constant Node_Id := Expression (N);
2554 T : Entity_Id;
2555 Index : Interp_Index;
2556 It : Interp;
2558 begin
2559 Generate_Definition (Id);
2560 Enter_Name (Id);
2562 -- This is an optimization of a common case of an integer literal
2564 if Nkind (E) = N_Integer_Literal then
2565 Set_Is_Static_Expression (E, True);
2566 Set_Etype (E, Universal_Integer);
2568 Set_Etype (Id, Universal_Integer);
2569 Set_Ekind (Id, E_Named_Integer);
2570 Set_Is_Frozen (Id, True);
2571 return;
2572 end if;
2574 Set_Is_Pure (Id, Is_Pure (Current_Scope));
2576 -- Process expression, replacing error by integer zero, to avoid
2577 -- cascaded errors or aborts further along in the processing
2579 -- Replace Error by integer zero, which seems least likely to
2580 -- cause cascaded errors.
2582 if E = Error then
2583 Rewrite (E, Make_Integer_Literal (Sloc (E), Uint_0));
2584 Set_Error_Posted (E);
2585 end if;
2587 Analyze (E);
2589 -- Verify that the expression is static and numeric. If
2590 -- the expression is overloaded, we apply the preference
2591 -- rule that favors root numeric types.
2593 if not Is_Overloaded (E) then
2594 T := Etype (E);
2596 else
2597 T := Any_Type;
2599 Get_First_Interp (E, Index, It);
2600 while Present (It.Typ) loop
2601 if (Is_Integer_Type (It.Typ)
2602 or else Is_Real_Type (It.Typ))
2603 and then (Scope (Base_Type (It.Typ))) = Standard_Standard
2604 then
2605 if T = Any_Type then
2606 T := It.Typ;
2608 elsif It.Typ = Universal_Real
2609 or else It.Typ = Universal_Integer
2610 then
2611 -- Choose universal interpretation over any other
2613 T := It.Typ;
2614 exit;
2615 end if;
2616 end if;
2618 Get_Next_Interp (Index, It);
2619 end loop;
2620 end if;
2622 if Is_Integer_Type (T) then
2623 Resolve (E, T);
2624 Set_Etype (Id, Universal_Integer);
2625 Set_Ekind (Id, E_Named_Integer);
2627 elsif Is_Real_Type (T) then
2629 -- Because the real value is converted to universal_real, this is a
2630 -- legal context for a universal fixed expression.
2632 if T = Universal_Fixed then
2633 declare
2634 Loc : constant Source_Ptr := Sloc (N);
2635 Conv : constant Node_Id := Make_Type_Conversion (Loc,
2636 Subtype_Mark =>
2637 New_Occurrence_Of (Universal_Real, Loc),
2638 Expression => Relocate_Node (E));
2640 begin
2641 Rewrite (E, Conv);
2642 Analyze (E);
2643 end;
2645 elsif T = Any_Fixed then
2646 Error_Msg_N ("illegal context for mixed mode operation", E);
2648 -- Expression is of the form : universal_fixed * integer. Try to
2649 -- resolve as universal_real.
2651 T := Universal_Real;
2652 Set_Etype (E, T);
2653 end if;
2655 Resolve (E, T);
2656 Set_Etype (Id, Universal_Real);
2657 Set_Ekind (Id, E_Named_Real);
2659 else
2660 Wrong_Type (E, Any_Numeric);
2661 Resolve (E, T);
2663 Set_Etype (Id, T);
2664 Set_Ekind (Id, E_Constant);
2665 Set_Never_Set_In_Source (Id, True);
2666 Set_Is_True_Constant (Id, True);
2667 return;
2668 end if;
2670 if Nkind_In (E, N_Integer_Literal, N_Real_Literal) then
2671 Set_Etype (E, Etype (Id));
2672 end if;
2674 if not Is_OK_Static_Expression (E) then
2675 Flag_Non_Static_Expr
2676 ("non-static expression used in number declaration!", E);
2677 Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
2678 Set_Etype (E, Any_Type);
2679 end if;
2680 end Analyze_Number_Declaration;
2682 --------------------------------
2683 -- Analyze_Object_Declaration --
2684 --------------------------------
2686 procedure Analyze_Object_Declaration (N : Node_Id) is
2687 Loc : constant Source_Ptr := Sloc (N);
2688 Id : constant Entity_Id := Defining_Identifier (N);
2689 T : Entity_Id;
2690 Act_T : Entity_Id;
2692 E : Node_Id := Expression (N);
2693 -- E is set to Expression (N) throughout this routine. When
2694 -- Expression (N) is modified, E is changed accordingly.
2696 Prev_Entity : Entity_Id := Empty;
2698 function Count_Tasks (T : Entity_Id) return Uint;
2699 -- This function is called when a non-generic library level object of a
2700 -- task type is declared. Its function is to count the static number of
2701 -- tasks declared within the type (it is only called if Has_Tasks is set
2702 -- for T). As a side effect, if an array of tasks with non-static bounds
2703 -- or a variant record type is encountered, Check_Restrictions is called
2704 -- indicating the count is unknown.
2706 -----------------
2707 -- Count_Tasks --
2708 -----------------
2710 function Count_Tasks (T : Entity_Id) return Uint is
2711 C : Entity_Id;
2712 X : Node_Id;
2713 V : Uint;
2715 begin
2716 if Is_Task_Type (T) then
2717 return Uint_1;
2719 elsif Is_Record_Type (T) then
2720 if Has_Discriminants (T) then
2721 Check_Restriction (Max_Tasks, N);
2722 return Uint_0;
2724 else
2725 V := Uint_0;
2726 C := First_Component (T);
2727 while Present (C) loop
2728 V := V + Count_Tasks (Etype (C));
2729 Next_Component (C);
2730 end loop;
2732 return V;
2733 end if;
2735 elsif Is_Array_Type (T) then
2736 X := First_Index (T);
2737 V := Count_Tasks (Component_Type (T));
2738 while Present (X) loop
2739 C := Etype (X);
2741 if not Is_Static_Subtype (C) then
2742 Check_Restriction (Max_Tasks, N);
2743 return Uint_0;
2744 else
2745 V := V * (UI_Max (Uint_0,
2746 Expr_Value (Type_High_Bound (C)) -
2747 Expr_Value (Type_Low_Bound (C)) + Uint_1));
2748 end if;
2750 Next_Index (X);
2751 end loop;
2753 return V;
2755 else
2756 return Uint_0;
2757 end if;
2758 end Count_Tasks;
2760 -- Start of processing for Analyze_Object_Declaration
2762 begin
2763 -- There are three kinds of implicit types generated by an
2764 -- object declaration:
2766 -- 1. Those for generated by the original Object Definition
2768 -- 2. Those generated by the Expression
2770 -- 3. Those used to constrained the Object Definition with the
2771 -- expression constraints when it is unconstrained
2773 -- They must be generated in this order to avoid order of elaboration
2774 -- issues. Thus the first step (after entering the name) is to analyze
2775 -- the object definition.
2777 if Constant_Present (N) then
2778 Prev_Entity := Current_Entity_In_Scope (Id);
2780 if Present (Prev_Entity)
2781 and then
2782 -- If the homograph is an implicit subprogram, it is overridden
2783 -- by the current declaration.
2785 ((Is_Overloadable (Prev_Entity)
2786 and then Is_Inherited_Operation (Prev_Entity))
2788 -- The current object is a discriminal generated for an entry
2789 -- family index. Even though the index is a constant, in this
2790 -- particular context there is no true constant redeclaration.
2791 -- Enter_Name will handle the visibility.
2793 or else
2794 (Is_Discriminal (Id)
2795 and then Ekind (Discriminal_Link (Id)) =
2796 E_Entry_Index_Parameter)
2798 -- The current object is the renaming for a generic declared
2799 -- within the instance.
2801 or else
2802 (Ekind (Prev_Entity) = E_Package
2803 and then Nkind (Parent (Prev_Entity)) =
2804 N_Package_Renaming_Declaration
2805 and then not Comes_From_Source (Prev_Entity)
2806 and then Is_Generic_Instance (Renamed_Entity (Prev_Entity))))
2807 then
2808 Prev_Entity := Empty;
2809 end if;
2810 end if;
2812 if Present (Prev_Entity) then
2813 Constant_Redeclaration (Id, N, T);
2815 Generate_Reference (Prev_Entity, Id, 'c');
2816 Set_Completion_Referenced (Id);
2818 if Error_Posted (N) then
2820 -- Type mismatch or illegal redeclaration, Do not analyze
2821 -- expression to avoid cascaded errors.
2823 T := Find_Type_Of_Object (Object_Definition (N), N);
2824 Set_Etype (Id, T);
2825 Set_Ekind (Id, E_Variable);
2826 goto Leave;
2827 end if;
2829 -- In the normal case, enter identifier at the start to catch premature
2830 -- usage in the initialization expression.
2832 else
2833 Generate_Definition (Id);
2834 Enter_Name (Id);
2836 Mark_Coextensions (N, Object_Definition (N));
2838 T := Find_Type_Of_Object (Object_Definition (N), N);
2840 if Nkind (Object_Definition (N)) = N_Access_Definition
2841 and then Present
2842 (Access_To_Subprogram_Definition (Object_Definition (N)))
2843 and then Protected_Present
2844 (Access_To_Subprogram_Definition (Object_Definition (N)))
2845 then
2846 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
2847 end if;
2849 if Error_Posted (Id) then
2850 Set_Etype (Id, T);
2851 Set_Ekind (Id, E_Variable);
2852 goto Leave;
2853 end if;
2854 end if;
2856 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
2857 -- out some static checks
2859 if Ada_Version >= Ada_2005
2860 and then Can_Never_Be_Null (T)
2861 then
2862 -- In case of aggregates we must also take care of the correct
2863 -- initialization of nested aggregates bug this is done at the
2864 -- point of the analysis of the aggregate (see sem_aggr.adb)
2866 if Present (Expression (N))
2867 and then Nkind (Expression (N)) = N_Aggregate
2868 then
2869 null;
2871 else
2872 declare
2873 Save_Typ : constant Entity_Id := Etype (Id);
2874 begin
2875 Set_Etype (Id, T); -- Temp. decoration for static checks
2876 Null_Exclusion_Static_Checks (N);
2877 Set_Etype (Id, Save_Typ);
2878 end;
2879 end if;
2880 end if;
2882 Set_Is_Pure (Id, Is_Pure (Current_Scope));
2884 -- If deferred constant, make sure context is appropriate. We detect
2885 -- a deferred constant as a constant declaration with no expression.
2886 -- A deferred constant can appear in a package body if its completion
2887 -- is by means of an interface pragma.
2889 if Constant_Present (N)
2890 and then No (E)
2891 then
2892 -- A deferred constant may appear in the declarative part of the
2893 -- following constructs:
2895 -- blocks
2896 -- entry bodies
2897 -- extended return statements
2898 -- package specs
2899 -- package bodies
2900 -- subprogram bodies
2901 -- task bodies
2903 -- When declared inside a package spec, a deferred constant must be
2904 -- completed by a full constant declaration or pragma Import. In all
2905 -- other cases, the only proper completion is pragma Import. Extended
2906 -- return statements are flagged as invalid contexts because they do
2907 -- not have a declarative part and so cannot accommodate the pragma.
2909 if Ekind (Current_Scope) = E_Return_Statement then
2910 Error_Msg_N
2911 ("invalid context for deferred constant declaration (RM 7.4)",
2913 Error_Msg_N
2914 ("\declaration requires an initialization expression",
2916 Set_Constant_Present (N, False);
2918 -- In Ada 83, deferred constant must be of private type
2920 elsif not Is_Private_Type (T) then
2921 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
2922 Error_Msg_N
2923 ("(Ada 83) deferred constant must be private type", N);
2924 end if;
2925 end if;
2927 -- If not a deferred constant, then object declaration freezes its type
2929 else
2930 Check_Fully_Declared (T, N);
2931 Freeze_Before (N, T);
2932 end if;
2934 -- If the object was created by a constrained array definition, then
2935 -- set the link in both the anonymous base type and anonymous subtype
2936 -- that are built to represent the array type to point to the object.
2938 if Nkind (Object_Definition (Declaration_Node (Id))) =
2939 N_Constrained_Array_Definition
2940 then
2941 Set_Related_Array_Object (T, Id);
2942 Set_Related_Array_Object (Base_Type (T), Id);
2943 end if;
2945 -- Special checks for protected objects not at library level
2947 if Is_Protected_Type (T)
2948 and then not Is_Library_Level_Entity (Id)
2949 then
2950 Check_Restriction (No_Local_Protected_Objects, Id);
2952 -- Protected objects with interrupt handlers must be at library level
2954 -- Ada 2005: this test is not needed (and the corresponding clause
2955 -- in the RM is removed) because accessibility checks are sufficient
2956 -- to make handlers not at the library level illegal.
2958 if Has_Interrupt_Handler (T)
2959 and then Ada_Version < Ada_2005
2960 then
2961 Error_Msg_N
2962 ("interrupt object can only be declared at library level", Id);
2963 end if;
2964 end if;
2966 -- The actual subtype of the object is the nominal subtype, unless
2967 -- the nominal one is unconstrained and obtained from the expression.
2969 Act_T := T;
2971 -- Process initialization expression if present and not in error
2973 if Present (E) and then E /= Error then
2975 -- Generate an error in case of CPP class-wide object initialization.
2976 -- Required because otherwise the expansion of the class-wide
2977 -- assignment would try to use 'size to initialize the object
2978 -- (primitive that is not available in CPP tagged types).
2980 if Is_Class_Wide_Type (Act_T)
2981 and then
2982 (Is_CPP_Class (Root_Type (Etype (Act_T)))
2983 or else
2984 (Present (Full_View (Root_Type (Etype (Act_T))))
2985 and then
2986 Is_CPP_Class (Full_View (Root_Type (Etype (Act_T))))))
2987 then
2988 Error_Msg_N
2989 ("predefined assignment not available for 'C'P'P tagged types",
2991 end if;
2993 Mark_Coextensions (N, E);
2994 Analyze (E);
2996 -- In case of errors detected in the analysis of the expression,
2997 -- decorate it with the expected type to avoid cascaded errors
2999 if No (Etype (E)) then
3000 Set_Etype (E, T);
3001 end if;
3003 -- If an initialization expression is present, then we set the
3004 -- Is_True_Constant flag. It will be reset if this is a variable
3005 -- and it is indeed modified.
3007 Set_Is_True_Constant (Id, True);
3009 -- If we are analyzing a constant declaration, set its completion
3010 -- flag after analyzing and resolving the expression.
3012 if Constant_Present (N) then
3013 Set_Has_Completion (Id);
3014 end if;
3016 -- Set type and resolve (type may be overridden later on)
3018 Set_Etype (Id, T);
3019 Resolve (E, T);
3021 -- If E is null and has been replaced by an N_Raise_Constraint_Error
3022 -- node (which was marked already-analyzed), we need to set the type
3023 -- to something other than Any_Access in order to keep gigi happy.
3025 if Etype (E) = Any_Access then
3026 Set_Etype (E, T);
3027 end if;
3029 -- If the object is an access to variable, the initialization
3030 -- expression cannot be an access to constant.
3032 if Is_Access_Type (T)
3033 and then not Is_Access_Constant (T)
3034 and then Is_Access_Type (Etype (E))
3035 and then Is_Access_Constant (Etype (E))
3036 then
3037 Error_Msg_N
3038 ("access to variable cannot be initialized "
3039 & "with an access-to-constant expression", E);
3040 end if;
3042 if not Assignment_OK (N) then
3043 Check_Initialization (T, E);
3044 end if;
3046 Check_Unset_Reference (E);
3048 -- If this is a variable, then set current value. If this is a
3049 -- declared constant of a scalar type with a static expression,
3050 -- indicate that it is always valid.
3052 if not Constant_Present (N) then
3053 if Compile_Time_Known_Value (E) then
3054 Set_Current_Value (Id, E);
3055 end if;
3057 elsif Is_Scalar_Type (T)
3058 and then Is_OK_Static_Expression (E)
3059 then
3060 Set_Is_Known_Valid (Id);
3061 end if;
3063 -- Deal with setting of null flags
3065 if Is_Access_Type (T) then
3066 if Known_Non_Null (E) then
3067 Set_Is_Known_Non_Null (Id, True);
3068 elsif Known_Null (E)
3069 and then not Can_Never_Be_Null (Id)
3070 then
3071 Set_Is_Known_Null (Id, True);
3072 end if;
3073 end if;
3075 -- Check incorrect use of dynamically tagged expressions.
3077 if Is_Tagged_Type (T) then
3078 Check_Dynamically_Tagged_Expression
3079 (Expr => E,
3080 Typ => T,
3081 Related_Nod => N);
3082 end if;
3084 Apply_Scalar_Range_Check (E, T);
3085 Apply_Static_Length_Check (E, T);
3086 end if;
3088 -- If the No_Streams restriction is set, check that the type of the
3089 -- object is not, and does not contain, any subtype derived from
3090 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
3091 -- Has_Stream just for efficiency reasons. There is no point in
3092 -- spending time on a Has_Stream check if the restriction is not set.
3094 if Restriction_Check_Required (No_Streams) then
3095 if Has_Stream (T) then
3096 Check_Restriction (No_Streams, N);
3097 end if;
3098 end if;
3100 -- Deal with predicate check before we start to do major rewriting.
3101 -- it is OK to initialize and then check the initialized value, since
3102 -- the object goes out of scope if we get a predicate failure. Note
3103 -- that we do this in the analyzer and not the expander because the
3104 -- analyzer does some substantial rewriting in some cases.
3106 -- We need a predicate check if the type has predicates, and if either
3107 -- there is an initializing expression, or for default initialization
3108 -- when we have at least one case of an explicit default initial value.
3110 if not Suppress_Assignment_Checks (N)
3111 and then Present (Predicate_Function (T))
3112 and then
3113 (Present (E)
3114 or else
3115 Is_Partially_Initialized_Type (T, Include_Implicit => False))
3116 then
3117 Insert_After (N,
3118 Make_Predicate_Check (T, New_Occurrence_Of (Id, Loc)));
3119 end if;
3121 -- Case of unconstrained type
3123 if Is_Indefinite_Subtype (T) then
3125 -- Nothing to do in deferred constant case
3127 if Constant_Present (N) and then No (E) then
3128 null;
3130 -- Case of no initialization present
3132 elsif No (E) then
3133 if No_Initialization (N) then
3134 null;
3136 elsif Is_Class_Wide_Type (T) then
3137 Error_Msg_N
3138 ("initialization required in class-wide declaration ", N);
3140 else
3141 Error_Msg_N
3142 ("unconstrained subtype not allowed (need initialization)",
3143 Object_Definition (N));
3145 if Is_Record_Type (T) and then Has_Discriminants (T) then
3146 Error_Msg_N
3147 ("\provide initial value or explicit discriminant values",
3148 Object_Definition (N));
3150 Error_Msg_NE
3151 ("\or give default discriminant values for type&",
3152 Object_Definition (N), T);
3154 elsif Is_Array_Type (T) then
3155 Error_Msg_N
3156 ("\provide initial value or explicit array bounds",
3157 Object_Definition (N));
3158 end if;
3159 end if;
3161 -- Case of initialization present but in error. Set initial
3162 -- expression as absent (but do not make above complaints)
3164 elsif E = Error then
3165 Set_Expression (N, Empty);
3166 E := Empty;
3168 -- Case of initialization present
3170 else
3171 -- Not allowed in Ada 83
3173 if not Constant_Present (N) then
3174 if Ada_Version = Ada_83
3175 and then Comes_From_Source (Object_Definition (N))
3176 then
3177 Error_Msg_N
3178 ("(Ada 83) unconstrained variable not allowed",
3179 Object_Definition (N));
3180 end if;
3181 end if;
3183 -- Now we constrain the variable from the initializing expression
3185 -- If the expression is an aggregate, it has been expanded into
3186 -- individual assignments. Retrieve the actual type from the
3187 -- expanded construct.
3189 if Is_Array_Type (T)
3190 and then No_Initialization (N)
3191 and then Nkind (Original_Node (E)) = N_Aggregate
3192 then
3193 Act_T := Etype (E);
3195 -- In case of class-wide interface object declarations we delay
3196 -- the generation of the equivalent record type declarations until
3197 -- its expansion because there are cases in they are not required.
3199 elsif Is_Interface (T) then
3200 null;
3202 else
3203 Expand_Subtype_From_Expr (N, T, Object_Definition (N), E);
3204 Act_T := Find_Type_Of_Object (Object_Definition (N), N);
3205 end if;
3207 Set_Is_Constr_Subt_For_U_Nominal (Act_T);
3209 if Aliased_Present (N) then
3210 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
3211 end if;
3213 Freeze_Before (N, Act_T);
3214 Freeze_Before (N, T);
3215 end if;
3217 elsif Is_Array_Type (T)
3218 and then No_Initialization (N)
3219 and then Nkind (Original_Node (E)) = N_Aggregate
3220 then
3221 if not Is_Entity_Name (Object_Definition (N)) then
3222 Act_T := Etype (E);
3223 Check_Compile_Time_Size (Act_T);
3225 if Aliased_Present (N) then
3226 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
3227 end if;
3228 end if;
3230 -- When the given object definition and the aggregate are specified
3231 -- independently, and their lengths might differ do a length check.
3232 -- This cannot happen if the aggregate is of the form (others =>...)
3234 if not Is_Constrained (T) then
3235 null;
3237 elsif Nkind (E) = N_Raise_Constraint_Error then
3239 -- Aggregate is statically illegal. Place back in declaration
3241 Set_Expression (N, E);
3242 Set_No_Initialization (N, False);
3244 elsif T = Etype (E) then
3245 null;
3247 elsif Nkind (E) = N_Aggregate
3248 and then Present (Component_Associations (E))
3249 and then Present (Choices (First (Component_Associations (E))))
3250 and then Nkind (First
3251 (Choices (First (Component_Associations (E))))) = N_Others_Choice
3252 then
3253 null;
3255 else
3256 Apply_Length_Check (E, T);
3257 end if;
3259 -- If the type is limited unconstrained with defaulted discriminants and
3260 -- there is no expression, then the object is constrained by the
3261 -- defaults, so it is worthwhile building the corresponding subtype.
3263 elsif (Is_Limited_Record (T) or else Is_Concurrent_Type (T))
3264 and then not Is_Constrained (T)
3265 and then Has_Discriminants (T)
3266 then
3267 if No (E) then
3268 Act_T := Build_Default_Subtype (T, N);
3269 else
3270 -- Ada 2005: a limited object may be initialized by means of an
3271 -- aggregate. If the type has default discriminants it has an
3272 -- unconstrained nominal type, Its actual subtype will be obtained
3273 -- from the aggregate, and not from the default discriminants.
3275 Act_T := Etype (E);
3276 end if;
3278 Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
3280 elsif Present (Underlying_Type (T))
3281 and then not Is_Constrained (Underlying_Type (T))
3282 and then Has_Discriminants (Underlying_Type (T))
3283 and then Nkind (E) = N_Function_Call
3284 and then Constant_Present (N)
3285 then
3286 -- The back-end has problems with constants of a discriminated type
3287 -- with defaults, if the initial value is a function call. We
3288 -- generate an intermediate temporary for the result of the call.
3289 -- It is unclear why this should make it acceptable to gcc. ???
3291 Remove_Side_Effects (E);
3292 end if;
3294 -- Check No_Wide_Characters restriction
3296 Check_Wide_Character_Restriction (T, Object_Definition (N));
3298 -- Indicate this is not set in source. Certainly true for constants,
3299 -- and true for variables so far (will be reset for a variable if and
3300 -- when we encounter a modification in the source).
3302 Set_Never_Set_In_Source (Id, True);
3304 -- Now establish the proper kind and type of the object
3306 if Constant_Present (N) then
3307 Set_Ekind (Id, E_Constant);
3308 Set_Is_True_Constant (Id, True);
3310 else
3311 Set_Ekind (Id, E_Variable);
3313 -- A variable is set as shared passive if it appears in a shared
3314 -- passive package, and is at the outer level. This is not done
3315 -- for entities generated during expansion, because those are
3316 -- always manipulated locally.
3318 if Is_Shared_Passive (Current_Scope)
3319 and then Is_Library_Level_Entity (Id)
3320 and then Comes_From_Source (Id)
3321 then
3322 Set_Is_Shared_Passive (Id);
3323 Check_Shared_Var (Id, T, N);
3324 end if;
3326 -- Set Has_Initial_Value if initializing expression present. Note
3327 -- that if there is no initializing expression, we leave the state
3328 -- of this flag unchanged (usually it will be False, but notably in
3329 -- the case of exception choice variables, it will already be true).
3331 if Present (E) then
3332 Set_Has_Initial_Value (Id, True);
3333 end if;
3334 end if;
3336 -- Initialize alignment and size and capture alignment setting
3338 Init_Alignment (Id);
3339 Init_Esize (Id);
3340 Set_Optimize_Alignment_Flags (Id);
3342 -- Deal with aliased case
3344 if Aliased_Present (N) then
3345 Set_Is_Aliased (Id);
3347 -- If the object is aliased and the type is unconstrained with
3348 -- defaulted discriminants and there is no expression, then the
3349 -- object is constrained by the defaults, so it is worthwhile
3350 -- building the corresponding subtype.
3352 -- Ada 2005 (AI-363): If the aliased object is discriminated and
3353 -- unconstrained, then only establish an actual subtype if the
3354 -- nominal subtype is indefinite. In definite cases the object is
3355 -- unconstrained in Ada 2005.
3357 if No (E)
3358 and then Is_Record_Type (T)
3359 and then not Is_Constrained (T)
3360 and then Has_Discriminants (T)
3361 and then (Ada_Version < Ada_2005 or else Is_Indefinite_Subtype (T))
3362 then
3363 Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
3364 end if;
3365 end if;
3367 -- Now we can set the type of the object
3369 Set_Etype (Id, Act_T);
3371 -- Deal with controlled types
3373 if Has_Controlled_Component (Etype (Id))
3374 or else Is_Controlled (Etype (Id))
3375 then
3376 if not Is_Library_Level_Entity (Id) then
3377 Check_Restriction (No_Nested_Finalization, N);
3378 else
3379 Validate_Controlled_Object (Id);
3380 end if;
3382 -- Generate a warning when an initialization causes an obvious ABE
3383 -- violation. If the init expression is a simple aggregate there
3384 -- shouldn't be any initialize/adjust call generated. This will be
3385 -- true as soon as aggregates are built in place when possible.
3387 -- ??? at the moment we do not generate warnings for temporaries
3388 -- created for those aggregates although Program_Error might be
3389 -- generated if compiled with -gnato.
3391 if Is_Controlled (Etype (Id))
3392 and then Comes_From_Source (Id)
3393 then
3394 declare
3395 BT : constant Entity_Id := Base_Type (Etype (Id));
3397 Implicit_Call : Entity_Id;
3398 pragma Warnings (Off, Implicit_Call);
3399 -- ??? what is this for (never referenced!)
3401 function Is_Aggr (N : Node_Id) return Boolean;
3402 -- Check that N is an aggregate
3404 -------------
3405 -- Is_Aggr --
3406 -------------
3408 function Is_Aggr (N : Node_Id) return Boolean is
3409 begin
3410 case Nkind (Original_Node (N)) is
3411 when N_Aggregate | N_Extension_Aggregate =>
3412 return True;
3414 when N_Qualified_Expression |
3415 N_Type_Conversion |
3416 N_Unchecked_Type_Conversion =>
3417 return Is_Aggr (Expression (Original_Node (N)));
3419 when others =>
3420 return False;
3421 end case;
3422 end Is_Aggr;
3424 begin
3425 -- If no underlying type, we already are in an error situation.
3426 -- Do not try to add a warning since we do not have access to
3427 -- prim-op list.
3429 if No (Underlying_Type (BT)) then
3430 Implicit_Call := Empty;
3432 -- A generic type does not have usable primitive operators.
3433 -- Initialization calls are built for instances.
3435 elsif Is_Generic_Type (BT) then
3436 Implicit_Call := Empty;
3438 -- If the init expression is not an aggregate, an adjust call
3439 -- will be generated
3441 elsif Present (E) and then not Is_Aggr (E) then
3442 Implicit_Call := Find_Prim_Op (BT, Name_Adjust);
3444 -- If no init expression and we are not in the deferred
3445 -- constant case, an Initialize call will be generated
3447 elsif No (E) and then not Constant_Present (N) then
3448 Implicit_Call := Find_Prim_Op (BT, Name_Initialize);
3450 else
3451 Implicit_Call := Empty;
3452 end if;
3453 end;
3454 end if;
3455 end if;
3457 if Has_Task (Etype (Id)) then
3458 Check_Restriction (No_Tasking, N);
3460 -- Deal with counting max tasks
3462 -- Nothing to do if inside a generic
3464 if Inside_A_Generic then
3465 null;
3467 -- If library level entity, then count tasks
3469 elsif Is_Library_Level_Entity (Id) then
3470 Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
3472 -- If not library level entity, then indicate we don't know max
3473 -- tasks and also check task hierarchy restriction and blocking
3474 -- operation (since starting a task is definitely blocking!)
3476 else
3477 Check_Restriction (Max_Tasks, N);
3478 Check_Restriction (No_Task_Hierarchy, N);
3479 Check_Potentially_Blocking_Operation (N);
3480 end if;
3482 -- A rather specialized test. If we see two tasks being declared
3483 -- of the same type in the same object declaration, and the task
3484 -- has an entry with an address clause, we know that program error
3485 -- will be raised at run time since we can't have two tasks with
3486 -- entries at the same address.
3488 if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
3489 declare
3490 E : Entity_Id;
3492 begin
3493 E := First_Entity (Etype (Id));
3494 while Present (E) loop
3495 if Ekind (E) = E_Entry
3496 and then Present (Get_Attribute_Definition_Clause
3497 (E, Attribute_Address))
3498 then
3499 Error_Msg_N
3500 ("?more than one task with same entry address", N);
3501 Error_Msg_N
3502 ("\?Program_Error will be raised at run time", N);
3503 Insert_Action (N,
3504 Make_Raise_Program_Error (Loc,
3505 Reason => PE_Duplicated_Entry_Address));
3506 exit;
3507 end if;
3509 Next_Entity (E);
3510 end loop;
3511 end;
3512 end if;
3513 end if;
3515 -- Some simple constant-propagation: if the expression is a constant
3516 -- string initialized with a literal, share the literal. This avoids
3517 -- a run-time copy.
3519 if Present (E)
3520 and then Is_Entity_Name (E)
3521 and then Ekind (Entity (E)) = E_Constant
3522 and then Base_Type (Etype (E)) = Standard_String
3523 then
3524 declare
3525 Val : constant Node_Id := Constant_Value (Entity (E));
3526 begin
3527 if Present (Val)
3528 and then Nkind (Val) = N_String_Literal
3529 then
3530 Rewrite (E, New_Copy (Val));
3531 end if;
3532 end;
3533 end if;
3535 -- Another optimization: if the nominal subtype is unconstrained and
3536 -- the expression is a function call that returns an unconstrained
3537 -- type, rewrite the declaration as a renaming of the result of the
3538 -- call. The exceptions below are cases where the copy is expected,
3539 -- either by the back end (Aliased case) or by the semantics, as for
3540 -- initializing controlled types or copying tags for classwide types.
3542 if Present (E)
3543 and then Nkind (E) = N_Explicit_Dereference
3544 and then Nkind (Original_Node (E)) = N_Function_Call
3545 and then not Is_Library_Level_Entity (Id)
3546 and then not Is_Constrained (Underlying_Type (T))
3547 and then not Is_Aliased (Id)
3548 and then not Is_Class_Wide_Type (T)
3549 and then not Is_Controlled (T)
3550 and then not Has_Controlled_Component (Base_Type (T))
3551 and then Expander_Active
3552 then
3553 Rewrite (N,
3554 Make_Object_Renaming_Declaration (Loc,
3555 Defining_Identifier => Id,
3556 Access_Definition => Empty,
3557 Subtype_Mark => New_Occurrence_Of
3558 (Base_Type (Etype (Id)), Loc),
3559 Name => E));
3561 Set_Renamed_Object (Id, E);
3563 -- Force generation of debugging information for the constant and for
3564 -- the renamed function call.
3566 Set_Debug_Info_Needed (Id);
3567 Set_Debug_Info_Needed (Entity (Prefix (E)));
3568 end if;
3570 if Present (Prev_Entity)
3571 and then Is_Frozen (Prev_Entity)
3572 and then not Error_Posted (Id)
3573 then
3574 Error_Msg_N ("full constant declaration appears too late", N);
3575 end if;
3577 Check_Eliminated (Id);
3579 -- Deal with setting In_Private_Part flag if in private part
3581 if Ekind (Scope (Id)) = E_Package
3582 and then In_Private_Part (Scope (Id))
3583 then
3584 Set_In_Private_Part (Id);
3585 end if;
3587 -- Check for violation of No_Local_Timing_Events
3589 if Is_RTE (Etype (Id), RE_Timing_Event)
3590 and then not Is_Library_Level_Entity (Id)
3591 then
3592 Check_Restriction (No_Local_Timing_Events, N);
3593 end if;
3595 <<Leave>>
3596 Analyze_Aspect_Specifications (N, Id, Aspect_Specifications (N));
3597 end Analyze_Object_Declaration;
3599 ---------------------------
3600 -- Analyze_Others_Choice --
3601 ---------------------------
3603 -- Nothing to do for the others choice node itself, the semantic analysis
3604 -- of the others choice will occur as part of the processing of the parent
3606 procedure Analyze_Others_Choice (N : Node_Id) is
3607 pragma Warnings (Off, N);
3608 begin
3609 null;
3610 end Analyze_Others_Choice;
3612 -------------------------------------------
3613 -- Analyze_Private_Extension_Declaration --
3614 -------------------------------------------
3616 procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
3617 T : constant Entity_Id := Defining_Identifier (N);
3618 Indic : constant Node_Id := Subtype_Indication (N);
3619 Parent_Type : Entity_Id;
3620 Parent_Base : Entity_Id;
3622 begin
3623 -- Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
3625 if Is_Non_Empty_List (Interface_List (N)) then
3626 declare
3627 Intf : Node_Id;
3628 T : Entity_Id;
3630 begin
3631 Intf := First (Interface_List (N));
3632 while Present (Intf) loop
3633 T := Find_Type_Of_Subtype_Indic (Intf);
3635 Diagnose_Interface (Intf, T);
3636 Next (Intf);
3637 end loop;
3638 end;
3639 end if;
3641 Generate_Definition (T);
3643 -- For other than Ada 2012, just enter the name in the current scope
3645 if Ada_Version < Ada_2012 then
3646 Enter_Name (T);
3648 -- Ada 2012 (AI05-0162): Enter the name in the current scope handling
3649 -- case of private type that completes an incomplete type.
3651 else
3652 declare
3653 Prev : Entity_Id;
3655 begin
3656 Prev := Find_Type_Name (N);
3658 pragma Assert (Prev = T
3659 or else (Ekind (Prev) = E_Incomplete_Type
3660 and then Present (Full_View (Prev))
3661 and then Full_View (Prev) = T));
3662 end;
3663 end if;
3665 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
3666 Parent_Base := Base_Type (Parent_Type);
3668 if Parent_Type = Any_Type
3669 or else Etype (Parent_Type) = Any_Type
3670 then
3671 Set_Ekind (T, Ekind (Parent_Type));
3672 Set_Etype (T, Any_Type);
3673 goto Leave;
3675 elsif not Is_Tagged_Type (Parent_Type) then
3676 Error_Msg_N
3677 ("parent of type extension must be a tagged type ", Indic);
3678 goto Leave;
3680 elsif Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
3681 Error_Msg_N ("premature derivation of incomplete type", Indic);
3682 goto Leave;
3684 elsif Is_Concurrent_Type (Parent_Type) then
3685 Error_Msg_N
3686 ("parent type of a private extension cannot be "
3687 & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
3689 Set_Etype (T, Any_Type);
3690 Set_Ekind (T, E_Limited_Private_Type);
3691 Set_Private_Dependents (T, New_Elmt_List);
3692 Set_Error_Posted (T);
3693 goto Leave;
3694 end if;
3696 -- Perhaps the parent type should be changed to the class-wide type's
3697 -- specific type in this case to prevent cascading errors ???
3699 if Is_Class_Wide_Type (Parent_Type) then
3700 Error_Msg_N
3701 ("parent of type extension must not be a class-wide type", Indic);
3702 goto Leave;
3703 end if;
3705 if (not Is_Package_Or_Generic_Package (Current_Scope)
3706 and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
3707 or else In_Private_Part (Current_Scope)
3709 then
3710 Error_Msg_N ("invalid context for private extension", N);
3711 end if;
3713 -- Set common attributes
3715 Set_Is_Pure (T, Is_Pure (Current_Scope));
3716 Set_Scope (T, Current_Scope);
3717 Set_Ekind (T, E_Record_Type_With_Private);
3718 Init_Size_Align (T);
3720 Set_Etype (T, Parent_Base);
3721 Set_Has_Task (T, Has_Task (Parent_Base));
3723 Set_Convention (T, Convention (Parent_Type));
3724 Set_First_Rep_Item (T, First_Rep_Item (Parent_Type));
3725 Set_Is_First_Subtype (T);
3726 Make_Class_Wide_Type (T);
3728 if Unknown_Discriminants_Present (N) then
3729 Set_Discriminant_Constraint (T, No_Elist);
3730 end if;
3732 Build_Derived_Record_Type (N, Parent_Type, T);
3734 -- Propagate inherited invariant information. The new type has
3735 -- invariants, if the parent type has inheritable invariants,
3736 -- and these invariants can in turn be inherited.
3738 if Has_Inheritable_Invariants (Parent_Type) then
3739 Set_Has_Inheritable_Invariants (T);
3740 Set_Has_Invariants (T);
3741 end if;
3743 -- Ada 2005 (AI-443): Synchronized private extension or a rewritten
3744 -- synchronized formal derived type.
3746 if Ada_Version >= Ada_2005
3747 and then Synchronized_Present (N)
3748 then
3749 Set_Is_Limited_Record (T);
3751 -- Formal derived type case
3753 if Is_Generic_Type (T) then
3755 -- The parent must be a tagged limited type or a synchronized
3756 -- interface.
3758 if (not Is_Tagged_Type (Parent_Type)
3759 or else not Is_Limited_Type (Parent_Type))
3760 and then
3761 (not Is_Interface (Parent_Type)
3762 or else not Is_Synchronized_Interface (Parent_Type))
3763 then
3764 Error_Msg_NE ("parent type of & must be tagged limited " &
3765 "or synchronized", N, T);
3766 end if;
3768 -- The progenitors (if any) must be limited or synchronized
3769 -- interfaces.
3771 if Present (Interfaces (T)) then
3772 declare
3773 Iface : Entity_Id;
3774 Iface_Elmt : Elmt_Id;
3776 begin
3777 Iface_Elmt := First_Elmt (Interfaces (T));
3778 while Present (Iface_Elmt) loop
3779 Iface := Node (Iface_Elmt);
3781 if not Is_Limited_Interface (Iface)
3782 and then not Is_Synchronized_Interface (Iface)
3783 then
3784 Error_Msg_NE ("progenitor & must be limited " &
3785 "or synchronized", N, Iface);
3786 end if;
3788 Next_Elmt (Iface_Elmt);
3789 end loop;
3790 end;
3791 end if;
3793 -- Regular derived extension, the parent must be a limited or
3794 -- synchronized interface.
3796 else
3797 if not Is_Interface (Parent_Type)
3798 or else (not Is_Limited_Interface (Parent_Type)
3799 and then
3800 not Is_Synchronized_Interface (Parent_Type))
3801 then
3802 Error_Msg_NE
3803 ("parent type of & must be limited interface", N, T);
3804 end if;
3805 end if;
3807 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
3808 -- extension with a synchronized parent must be explicitly declared
3809 -- synchronized, because the full view will be a synchronized type.
3810 -- This must be checked before the check for limited types below,
3811 -- to ensure that types declared limited are not allowed to extend
3812 -- synchronized interfaces.
3814 elsif Is_Interface (Parent_Type)
3815 and then Is_Synchronized_Interface (Parent_Type)
3816 and then not Synchronized_Present (N)
3817 then
3818 Error_Msg_NE
3819 ("private extension of& must be explicitly synchronized",
3820 N, Parent_Type);
3822 elsif Limited_Present (N) then
3823 Set_Is_Limited_Record (T);
3825 if not Is_Limited_Type (Parent_Type)
3826 and then
3827 (not Is_Interface (Parent_Type)
3828 or else not Is_Limited_Interface (Parent_Type))
3829 then
3830 Error_Msg_NE ("parent type& of limited extension must be limited",
3831 N, Parent_Type);
3832 end if;
3833 end if;
3835 <<Leave>>
3836 Analyze_Aspect_Specifications (N, T, Aspect_Specifications (N));
3837 end Analyze_Private_Extension_Declaration;
3839 ---------------------------------
3840 -- Analyze_Subtype_Declaration --
3841 ---------------------------------
3843 procedure Analyze_Subtype_Declaration
3844 (N : Node_Id;
3845 Skip : Boolean := False)
3847 Id : constant Entity_Id := Defining_Identifier (N);
3848 T : Entity_Id;
3849 R_Checks : Check_Result;
3851 begin
3852 Generate_Definition (Id);
3853 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3854 Init_Size_Align (Id);
3856 -- The following guard condition on Enter_Name is to handle cases where
3857 -- the defining identifier has already been entered into the scope but
3858 -- the declaration as a whole needs to be analyzed.
3860 -- This case in particular happens for derived enumeration types. The
3861 -- derived enumeration type is processed as an inserted enumeration type
3862 -- declaration followed by a rewritten subtype declaration. The defining
3863 -- identifier, however, is entered into the name scope very early in the
3864 -- processing of the original type declaration and therefore needs to be
3865 -- avoided here, when the created subtype declaration is analyzed. (See
3866 -- Build_Derived_Types)
3868 -- This also happens when the full view of a private type is derived
3869 -- type with constraints. In this case the entity has been introduced
3870 -- in the private declaration.
3872 if Skip
3873 or else (Present (Etype (Id))
3874 and then (Is_Private_Type (Etype (Id))
3875 or else Is_Task_Type (Etype (Id))
3876 or else Is_Rewrite_Substitution (N)))
3877 then
3878 null;
3880 else
3881 Enter_Name (Id);
3882 end if;
3884 T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
3886 -- Inherit common attributes
3888 Set_Is_Generic_Type (Id, Is_Generic_Type (Base_Type (T)));
3889 Set_Is_Volatile (Id, Is_Volatile (T));
3890 Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
3891 Set_Is_Atomic (Id, Is_Atomic (T));
3892 Set_Is_Ada_2005_Only (Id, Is_Ada_2005_Only (T));
3893 Set_Is_Ada_2012_Only (Id, Is_Ada_2012_Only (T));
3894 Set_Convention (Id, Convention (T));
3896 -- If ancestor has predicates then so does the subtype, and in addition
3897 -- we must delay the freeze to properly arrange predicate inheritance.
3899 -- The Ancestor_Type test is a big kludge, there seem to be cases in
3900 -- which T = ID, so the above tests and assignments do nothing???
3902 if Has_Predicates (T)
3903 or else (Present (Ancestor_Subtype (T))
3904 and then Has_Predicates (Ancestor_Subtype (T)))
3905 then
3906 Set_Has_Predicates (Id);
3907 Set_Has_Delayed_Freeze (Id);
3908 end if;
3910 -- In the case where there is no constraint given in the subtype
3911 -- indication, Process_Subtype just returns the Subtype_Mark, so its
3912 -- semantic attributes must be established here.
3914 if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
3915 Set_Etype (Id, Base_Type (T));
3917 case Ekind (T) is
3918 when Array_Kind =>
3919 Set_Ekind (Id, E_Array_Subtype);
3920 Copy_Array_Subtype_Attributes (Id, T);
3922 when Decimal_Fixed_Point_Kind =>
3923 Set_Ekind (Id, E_Decimal_Fixed_Point_Subtype);
3924 Set_Digits_Value (Id, Digits_Value (T));
3925 Set_Delta_Value (Id, Delta_Value (T));
3926 Set_Scale_Value (Id, Scale_Value (T));
3927 Set_Small_Value (Id, Small_Value (T));
3928 Set_Scalar_Range (Id, Scalar_Range (T));
3929 Set_Machine_Radix_10 (Id, Machine_Radix_10 (T));
3930 Set_Is_Constrained (Id, Is_Constrained (T));
3931 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
3932 Set_RM_Size (Id, RM_Size (T));
3934 when Enumeration_Kind =>
3935 Set_Ekind (Id, E_Enumeration_Subtype);
3936 Set_First_Literal (Id, First_Literal (Base_Type (T)));
3937 Set_Scalar_Range (Id, Scalar_Range (T));
3938 Set_Is_Character_Type (Id, Is_Character_Type (T));
3939 Set_Is_Constrained (Id, Is_Constrained (T));
3940 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
3941 Set_RM_Size (Id, RM_Size (T));
3943 when Ordinary_Fixed_Point_Kind =>
3944 Set_Ekind (Id, E_Ordinary_Fixed_Point_Subtype);
3945 Set_Scalar_Range (Id, Scalar_Range (T));
3946 Set_Small_Value (Id, Small_Value (T));
3947 Set_Delta_Value (Id, Delta_Value (T));
3948 Set_Is_Constrained (Id, Is_Constrained (T));
3949 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
3950 Set_RM_Size (Id, RM_Size (T));
3952 when Float_Kind =>
3953 Set_Ekind (Id, E_Floating_Point_Subtype);
3954 Set_Scalar_Range (Id, Scalar_Range (T));
3955 Set_Digits_Value (Id, Digits_Value (T));
3956 Set_Is_Constrained (Id, Is_Constrained (T));
3958 when Signed_Integer_Kind =>
3959 Set_Ekind (Id, E_Signed_Integer_Subtype);
3960 Set_Scalar_Range (Id, Scalar_Range (T));
3961 Set_Is_Constrained (Id, Is_Constrained (T));
3962 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
3963 Set_RM_Size (Id, RM_Size (T));
3965 when Modular_Integer_Kind =>
3966 Set_Ekind (Id, E_Modular_Integer_Subtype);
3967 Set_Scalar_Range (Id, Scalar_Range (T));
3968 Set_Is_Constrained (Id, Is_Constrained (T));
3969 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
3970 Set_RM_Size (Id, RM_Size (T));
3972 when Class_Wide_Kind =>
3973 Set_Ekind (Id, E_Class_Wide_Subtype);
3974 Set_First_Entity (Id, First_Entity (T));
3975 Set_Last_Entity (Id, Last_Entity (T));
3976 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
3977 Set_Cloned_Subtype (Id, T);
3978 Set_Is_Tagged_Type (Id, True);
3979 Set_Has_Unknown_Discriminants
3980 (Id, True);
3982 if Ekind (T) = E_Class_Wide_Subtype then
3983 Set_Equivalent_Type (Id, Equivalent_Type (T));
3984 end if;
3986 when E_Record_Type | E_Record_Subtype =>
3987 Set_Ekind (Id, E_Record_Subtype);
3989 if Ekind (T) = E_Record_Subtype
3990 and then Present (Cloned_Subtype (T))
3991 then
3992 Set_Cloned_Subtype (Id, Cloned_Subtype (T));
3993 else
3994 Set_Cloned_Subtype (Id, T);
3995 end if;
3997 Set_First_Entity (Id, First_Entity (T));
3998 Set_Last_Entity (Id, Last_Entity (T));
3999 Set_Has_Discriminants (Id, Has_Discriminants (T));
4000 Set_Is_Constrained (Id, Is_Constrained (T));
4001 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
4002 Set_Has_Unknown_Discriminants
4003 (Id, Has_Unknown_Discriminants (T));
4005 if Has_Discriminants (T) then
4006 Set_Discriminant_Constraint
4007 (Id, Discriminant_Constraint (T));
4008 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
4010 elsif Has_Unknown_Discriminants (Id) then
4011 Set_Discriminant_Constraint (Id, No_Elist);
4012 end if;
4014 if Is_Tagged_Type (T) then
4015 Set_Is_Tagged_Type (Id);
4016 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
4017 Set_Direct_Primitive_Operations
4018 (Id, Direct_Primitive_Operations (T));
4019 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
4021 if Is_Interface (T) then
4022 Set_Is_Interface (Id);
4023 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
4024 end if;
4025 end if;
4027 when Private_Kind =>
4028 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
4029 Set_Has_Discriminants (Id, Has_Discriminants (T));
4030 Set_Is_Constrained (Id, Is_Constrained (T));
4031 Set_First_Entity (Id, First_Entity (T));
4032 Set_Last_Entity (Id, Last_Entity (T));
4033 Set_Private_Dependents (Id, New_Elmt_List);
4034 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
4035 Set_Has_Unknown_Discriminants
4036 (Id, Has_Unknown_Discriminants (T));
4037 Set_Known_To_Have_Preelab_Init
4038 (Id, Known_To_Have_Preelab_Init (T));
4040 if Is_Tagged_Type (T) then
4041 Set_Is_Tagged_Type (Id);
4042 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
4043 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
4044 Set_Direct_Primitive_Operations (Id,
4045 Direct_Primitive_Operations (T));
4046 end if;
4048 -- In general the attributes of the subtype of a private type
4049 -- are the attributes of the partial view of parent. However,
4050 -- the full view may be a discriminated type, and the subtype
4051 -- must share the discriminant constraint to generate correct
4052 -- calls to initialization procedures.
4054 if Has_Discriminants (T) then
4055 Set_Discriminant_Constraint
4056 (Id, Discriminant_Constraint (T));
4057 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
4059 elsif Present (Full_View (T))
4060 and then Has_Discriminants (Full_View (T))
4061 then
4062 Set_Discriminant_Constraint
4063 (Id, Discriminant_Constraint (Full_View (T)));
4064 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
4066 -- This would seem semantically correct, but apparently
4067 -- confuses the back-end. To be explained and checked with
4068 -- current version ???
4070 -- Set_Has_Discriminants (Id);
4071 end if;
4073 Prepare_Private_Subtype_Completion (Id, N);
4075 when Access_Kind =>
4076 Set_Ekind (Id, E_Access_Subtype);
4077 Set_Is_Constrained (Id, Is_Constrained (T));
4078 Set_Is_Access_Constant
4079 (Id, Is_Access_Constant (T));
4080 Set_Directly_Designated_Type
4081 (Id, Designated_Type (T));
4082 Set_Can_Never_Be_Null (Id, Can_Never_Be_Null (T));
4084 -- A Pure library_item must not contain the declaration of a
4085 -- named access type, except within a subprogram, generic
4086 -- subprogram, task unit, or protected unit, or if it has
4087 -- a specified Storage_Size of zero (RM05-10.2.1(15.4-15.5)).
4089 if Comes_From_Source (Id)
4090 and then In_Pure_Unit
4091 and then not In_Subprogram_Task_Protected_Unit
4092 and then not No_Pool_Assigned (Id)
4093 then
4094 Error_Msg_N
4095 ("named access types not allowed in pure unit", N);
4096 end if;
4098 when Concurrent_Kind =>
4099 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
4100 Set_Corresponding_Record_Type (Id,
4101 Corresponding_Record_Type (T));
4102 Set_First_Entity (Id, First_Entity (T));
4103 Set_First_Private_Entity (Id, First_Private_Entity (T));
4104 Set_Has_Discriminants (Id, Has_Discriminants (T));
4105 Set_Is_Constrained (Id, Is_Constrained (T));
4106 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
4107 Set_Last_Entity (Id, Last_Entity (T));
4109 if Has_Discriminants (T) then
4110 Set_Discriminant_Constraint (Id,
4111 Discriminant_Constraint (T));
4112 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
4113 end if;
4115 when E_Incomplete_Type =>
4116 if Ada_Version >= Ada_2005 then
4117 Set_Ekind (Id, E_Incomplete_Subtype);
4119 -- Ada 2005 (AI-412): Decorate an incomplete subtype
4120 -- of an incomplete type visible through a limited
4121 -- with clause.
4123 if From_With_Type (T)
4124 and then Present (Non_Limited_View (T))
4125 then
4126 Set_From_With_Type (Id);
4127 Set_Non_Limited_View (Id, Non_Limited_View (T));
4129 -- Ada 2005 (AI-412): Add the regular incomplete subtype
4130 -- to the private dependents of the original incomplete
4131 -- type for future transformation.
4133 else
4134 Append_Elmt (Id, Private_Dependents (T));
4135 end if;
4137 -- If the subtype name denotes an incomplete type an error
4138 -- was already reported by Process_Subtype.
4140 else
4141 Set_Etype (Id, Any_Type);
4142 end if;
4144 when others =>
4145 raise Program_Error;
4146 end case;
4147 end if;
4149 if Etype (Id) = Any_Type then
4150 goto Leave;
4151 end if;
4153 -- Some common processing on all types
4155 Set_Size_Info (Id, T);
4156 Set_First_Rep_Item (Id, First_Rep_Item (T));
4158 T := Etype (Id);
4160 Set_Is_Immediately_Visible (Id, True);
4161 Set_Depends_On_Private (Id, Has_Private_Component (T));
4162 Set_Is_Descendent_Of_Address (Id, Is_Descendent_Of_Address (T));
4164 if Is_Interface (T) then
4165 Set_Is_Interface (Id);
4166 end if;
4168 if Present (Generic_Parent_Type (N))
4169 and then
4170 (Nkind
4171 (Parent (Generic_Parent_Type (N))) /= N_Formal_Type_Declaration
4172 or else Nkind
4173 (Formal_Type_Definition (Parent (Generic_Parent_Type (N))))
4174 /= N_Formal_Private_Type_Definition)
4175 then
4176 if Is_Tagged_Type (Id) then
4178 -- If this is a generic actual subtype for a synchronized type,
4179 -- the primitive operations are those of the corresponding record
4180 -- for which there is a separate subtype declaration.
4182 if Is_Concurrent_Type (Id) then
4183 null;
4184 elsif Is_Class_Wide_Type (Id) then
4185 Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
4186 else
4187 Derive_Subprograms (Generic_Parent_Type (N), Id, T);
4188 end if;
4190 elsif Scope (Etype (Id)) /= Standard_Standard then
4191 Derive_Subprograms (Generic_Parent_Type (N), Id);
4192 end if;
4193 end if;
4195 if Is_Private_Type (T)
4196 and then Present (Full_View (T))
4197 then
4198 Conditional_Delay (Id, Full_View (T));
4200 -- The subtypes of components or subcomponents of protected types
4201 -- do not need freeze nodes, which would otherwise appear in the
4202 -- wrong scope (before the freeze node for the protected type). The
4203 -- proper subtypes are those of the subcomponents of the corresponding
4204 -- record.
4206 elsif Ekind (Scope (Id)) /= E_Protected_Type
4207 and then Present (Scope (Scope (Id))) -- error defense!
4208 and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
4209 then
4210 Conditional_Delay (Id, T);
4211 end if;
4213 -- Check that constraint_error is raised for a scalar subtype
4214 -- indication when the lower or upper bound of a non-null range
4215 -- lies outside the range of the type mark.
4217 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
4218 if Is_Scalar_Type (Etype (Id))
4219 and then Scalar_Range (Id) /=
4220 Scalar_Range (Etype (Subtype_Mark
4221 (Subtype_Indication (N))))
4222 then
4223 Apply_Range_Check
4224 (Scalar_Range (Id),
4225 Etype (Subtype_Mark (Subtype_Indication (N))));
4227 elsif Is_Array_Type (Etype (Id))
4228 and then Present (First_Index (Id))
4229 then
4230 -- This really should be a subprogram that finds the indications
4231 -- to check???
4233 if ((Nkind (First_Index (Id)) = N_Identifier
4234 and then Ekind (Entity (First_Index (Id))) in Scalar_Kind)
4235 or else Nkind (First_Index (Id)) = N_Subtype_Indication)
4236 and then
4237 Nkind (Scalar_Range (Etype (First_Index (Id)))) = N_Range
4238 then
4239 declare
4240 Target_Typ : constant Entity_Id :=
4241 Etype
4242 (First_Index (Etype
4243 (Subtype_Mark (Subtype_Indication (N)))));
4244 begin
4245 R_Checks :=
4246 Get_Range_Checks
4247 (Scalar_Range (Etype (First_Index (Id))),
4248 Target_Typ,
4249 Etype (First_Index (Id)),
4250 Defining_Identifier (N));
4252 Insert_Range_Checks
4253 (R_Checks,
4255 Target_Typ,
4256 Sloc (Defining_Identifier (N)));
4257 end;
4258 end if;
4259 end if;
4260 end if;
4262 -- Make sure that generic actual types are properly frozen. The subtype
4263 -- is marked as a generic actual type when the enclosing instance is
4264 -- analyzed, so here we identify the subtype from the tree structure.
4266 if Expander_Active
4267 and then Is_Generic_Actual_Type (Id)
4268 and then In_Instance
4269 and then not Comes_From_Source (N)
4270 and then Nkind (Subtype_Indication (N)) /= N_Subtype_Indication
4271 and then Is_Frozen (T)
4272 then
4273 Freeze_Before (N, Id);
4274 end if;
4276 Set_Optimize_Alignment_Flags (Id);
4277 Check_Eliminated (Id);
4279 <<Leave>>
4280 Analyze_Aspect_Specifications (N, Id, Aspect_Specifications (N));
4281 end Analyze_Subtype_Declaration;
4283 --------------------------------
4284 -- Analyze_Subtype_Indication --
4285 --------------------------------
4287 procedure Analyze_Subtype_Indication (N : Node_Id) is
4288 T : constant Entity_Id := Subtype_Mark (N);
4289 R : constant Node_Id := Range_Expression (Constraint (N));
4291 begin
4292 Analyze (T);
4294 if R /= Error then
4295 Analyze (R);
4296 Set_Etype (N, Etype (R));
4297 Resolve (R, Entity (T));
4298 else
4299 Set_Error_Posted (R);
4300 Set_Error_Posted (T);
4301 end if;
4302 end Analyze_Subtype_Indication;
4304 --------------------------
4305 -- Analyze_Variant_Part --
4306 --------------------------
4308 procedure Analyze_Variant_Part (N : Node_Id) is
4310 procedure Non_Static_Choice_Error (Choice : Node_Id);
4311 -- Error routine invoked by the generic instantiation below when the
4312 -- variant part has a non static choice.
4314 procedure Process_Declarations (Variant : Node_Id);
4315 -- Analyzes all the declarations associated with a Variant. Needed by
4316 -- the generic instantiation below.
4318 package Variant_Choices_Processing is new
4319 Generic_Choices_Processing
4320 (Get_Alternatives => Variants,
4321 Get_Choices => Discrete_Choices,
4322 Process_Empty_Choice => No_OP,
4323 Process_Non_Static_Choice => Non_Static_Choice_Error,
4324 Process_Associated_Node => Process_Declarations);
4325 use Variant_Choices_Processing;
4326 -- Instantiation of the generic choice processing package
4328 -----------------------------
4329 -- Non_Static_Choice_Error --
4330 -----------------------------
4332 procedure Non_Static_Choice_Error (Choice : Node_Id) is
4333 begin
4334 Flag_Non_Static_Expr
4335 ("choice given in variant part is not static!", Choice);
4336 end Non_Static_Choice_Error;
4338 --------------------------
4339 -- Process_Declarations --
4340 --------------------------
4342 procedure Process_Declarations (Variant : Node_Id) is
4343 begin
4344 if not Null_Present (Component_List (Variant)) then
4345 Analyze_Declarations (Component_Items (Component_List (Variant)));
4347 if Present (Variant_Part (Component_List (Variant))) then
4348 Analyze (Variant_Part (Component_List (Variant)));
4349 end if;
4350 end if;
4351 end Process_Declarations;
4353 -- Local Variables
4355 Discr_Name : Node_Id;
4356 Discr_Type : Entity_Id;
4358 Dont_Care : Boolean;
4359 Others_Present : Boolean := False;
4361 pragma Warnings (Off, Dont_Care);
4362 pragma Warnings (Off, Others_Present);
4363 -- We don't care about the assigned values of any of these
4365 -- Start of processing for Analyze_Variant_Part
4367 begin
4368 Discr_Name := Name (N);
4369 Analyze (Discr_Name);
4371 -- If Discr_Name bad, get out (prevent cascaded errors)
4373 if Etype (Discr_Name) = Any_Type then
4374 return;
4375 end if;
4377 -- Check invalid discriminant in variant part
4379 if Ekind (Entity (Discr_Name)) /= E_Discriminant then
4380 Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
4381 end if;
4383 Discr_Type := Etype (Entity (Discr_Name));
4385 if not Is_Discrete_Type (Discr_Type) then
4386 Error_Msg_N
4387 ("discriminant in a variant part must be of a discrete type",
4388 Name (N));
4389 return;
4390 end if;
4392 -- Call the instantiated Analyze_Choices which does the rest of the work
4394 Analyze_Choices (N, Discr_Type, Dont_Care, Others_Present);
4395 end Analyze_Variant_Part;
4397 ----------------------------
4398 -- Array_Type_Declaration --
4399 ----------------------------
4401 procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
4402 Component_Def : constant Node_Id := Component_Definition (Def);
4403 Element_Type : Entity_Id;
4404 Implicit_Base : Entity_Id;
4405 Index : Node_Id;
4406 Related_Id : Entity_Id := Empty;
4407 Nb_Index : Nat;
4408 P : constant Node_Id := Parent (Def);
4409 Priv : Entity_Id;
4411 begin
4412 if Nkind (Def) = N_Constrained_Array_Definition then
4413 Index := First (Discrete_Subtype_Definitions (Def));
4414 else
4415 Index := First (Subtype_Marks (Def));
4416 end if;
4418 -- Find proper names for the implicit types which may be public. In case
4419 -- of anonymous arrays we use the name of the first object of that type
4420 -- as prefix.
4422 if No (T) then
4423 Related_Id := Defining_Identifier (P);
4424 else
4425 Related_Id := T;
4426 end if;
4428 Nb_Index := 1;
4429 while Present (Index) loop
4430 Analyze (Index);
4432 -- Add a subtype declaration for each index of private array type
4433 -- declaration whose etype is also private. For example:
4435 -- package Pkg is
4436 -- type Index is private;
4437 -- private
4438 -- type Table is array (Index) of ...
4439 -- end;
4441 -- This is currently required by the expander for the internally
4442 -- generated equality subprogram of records with variant parts in
4443 -- which the etype of some component is such private type.
4445 if Ekind (Current_Scope) = E_Package
4446 and then In_Private_Part (Current_Scope)
4447 and then Has_Private_Declaration (Etype (Index))
4448 then
4449 declare
4450 Loc : constant Source_Ptr := Sloc (Def);
4451 New_E : Entity_Id;
4452 Decl : Entity_Id;
4454 begin
4455 New_E := Make_Temporary (Loc, 'T');
4456 Set_Is_Internal (New_E);
4458 Decl :=
4459 Make_Subtype_Declaration (Loc,
4460 Defining_Identifier => New_E,
4461 Subtype_Indication =>
4462 New_Occurrence_Of (Etype (Index), Loc));
4464 Insert_Before (Parent (Def), Decl);
4465 Analyze (Decl);
4466 Set_Etype (Index, New_E);
4468 -- If the index is a range the Entity attribute is not
4469 -- available. Example:
4471 -- package Pkg is
4472 -- type T is private;
4473 -- private
4474 -- type T is new Natural;
4475 -- Table : array (T(1) .. T(10)) of Boolean;
4476 -- end Pkg;
4478 if Nkind (Index) /= N_Range then
4479 Set_Entity (Index, New_E);
4480 end if;
4481 end;
4482 end if;
4484 Make_Index (Index, P, Related_Id, Nb_Index);
4486 -- Check error of subtype with predicate for index type
4488 Bad_Predicated_Subtype_Use
4489 ("subtype& has predicate, not allowed as index subtype",
4490 Index, Etype (Index));
4492 -- Move to next index
4494 Next_Index (Index);
4495 Nb_Index := Nb_Index + 1;
4496 end loop;
4498 -- Process subtype indication if one is present
4500 if Present (Subtype_Indication (Component_Def)) then
4501 Element_Type :=
4502 Process_Subtype
4503 (Subtype_Indication (Component_Def), P, Related_Id, 'C');
4505 -- Ada 2005 (AI-230): Access Definition case
4507 else pragma Assert (Present (Access_Definition (Component_Def)));
4509 -- Indicate that the anonymous access type is created by the
4510 -- array type declaration.
4512 Element_Type := Access_Definition
4513 (Related_Nod => P,
4514 N => Access_Definition (Component_Def));
4515 Set_Is_Local_Anonymous_Access (Element_Type);
4517 -- Propagate the parent. This field is needed if we have to generate
4518 -- the master_id associated with an anonymous access to task type
4519 -- component (see Expand_N_Full_Type_Declaration.Build_Master)
4521 Set_Parent (Element_Type, Parent (T));
4523 -- Ada 2005 (AI-230): In case of components that are anonymous access
4524 -- types the level of accessibility depends on the enclosing type
4525 -- declaration
4527 Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
4529 -- Ada 2005 (AI-254)
4531 declare
4532 CD : constant Node_Id :=
4533 Access_To_Subprogram_Definition
4534 (Access_Definition (Component_Def));
4535 begin
4536 if Present (CD) and then Protected_Present (CD) then
4537 Element_Type :=
4538 Replace_Anonymous_Access_To_Protected_Subprogram (Def);
4539 end if;
4540 end;
4541 end if;
4543 -- Constrained array case
4545 if No (T) then
4546 T := Create_Itype (E_Void, P, Related_Id, 'T');
4547 end if;
4549 if Nkind (Def) = N_Constrained_Array_Definition then
4551 -- Establish Implicit_Base as unconstrained base type
4553 Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
4555 Set_Etype (Implicit_Base, Implicit_Base);
4556 Set_Scope (Implicit_Base, Current_Scope);
4557 Set_Has_Delayed_Freeze (Implicit_Base);
4559 -- The constrained array type is a subtype of the unconstrained one
4561 Set_Ekind (T, E_Array_Subtype);
4562 Init_Size_Align (T);
4563 Set_Etype (T, Implicit_Base);
4564 Set_Scope (T, Current_Scope);
4565 Set_Is_Constrained (T, True);
4566 Set_First_Index (T, First (Discrete_Subtype_Definitions (Def)));
4567 Set_Has_Delayed_Freeze (T);
4569 -- Complete setup of implicit base type
4571 Set_First_Index (Implicit_Base, First_Index (T));
4572 Set_Component_Type (Implicit_Base, Element_Type);
4573 Set_Has_Task (Implicit_Base, Has_Task (Element_Type));
4574 Set_Component_Size (Implicit_Base, Uint_0);
4575 Set_Packed_Array_Type (Implicit_Base, Empty);
4576 Set_Has_Controlled_Component
4577 (Implicit_Base, Has_Controlled_Component
4578 (Element_Type)
4579 or else Is_Controlled
4580 (Element_Type));
4581 Set_Finalize_Storage_Only
4582 (Implicit_Base, Finalize_Storage_Only
4583 (Element_Type));
4585 -- Unconstrained array case
4587 else
4588 Set_Ekind (T, E_Array_Type);
4589 Init_Size_Align (T);
4590 Set_Etype (T, T);
4591 Set_Scope (T, Current_Scope);
4592 Set_Component_Size (T, Uint_0);
4593 Set_Is_Constrained (T, False);
4594 Set_First_Index (T, First (Subtype_Marks (Def)));
4595 Set_Has_Delayed_Freeze (T, True);
4596 Set_Has_Task (T, Has_Task (Element_Type));
4597 Set_Has_Controlled_Component (T, Has_Controlled_Component
4598 (Element_Type)
4599 or else
4600 Is_Controlled (Element_Type));
4601 Set_Finalize_Storage_Only (T, Finalize_Storage_Only
4602 (Element_Type));
4603 end if;
4605 -- Common attributes for both cases
4607 Set_Component_Type (Base_Type (T), Element_Type);
4608 Set_Packed_Array_Type (T, Empty);
4610 if Aliased_Present (Component_Definition (Def)) then
4611 Set_Has_Aliased_Components (Etype (T));
4612 end if;
4614 -- Ada 2005 (AI-231): Propagate the null-excluding attribute to the
4615 -- array type to ensure that objects of this type are initialized.
4617 if Ada_Version >= Ada_2005
4618 and then Can_Never_Be_Null (Element_Type)
4619 then
4620 Set_Can_Never_Be_Null (T);
4622 if Null_Exclusion_Present (Component_Definition (Def))
4624 -- No need to check itypes because in their case this check was
4625 -- done at their point of creation
4627 and then not Is_Itype (Element_Type)
4628 then
4629 Error_Msg_N
4630 ("`NOT NULL` not allowed (null already excluded)",
4631 Subtype_Indication (Component_Definition (Def)));
4632 end if;
4633 end if;
4635 Priv := Private_Component (Element_Type);
4637 if Present (Priv) then
4639 -- Check for circular definitions
4641 if Priv = Any_Type then
4642 Set_Component_Type (Etype (T), Any_Type);
4644 -- There is a gap in the visibility of operations on the composite
4645 -- type only if the component type is defined in a different scope.
4647 elsif Scope (Priv) = Current_Scope then
4648 null;
4650 elsif Is_Limited_Type (Priv) then
4651 Set_Is_Limited_Composite (Etype (T));
4652 Set_Is_Limited_Composite (T);
4653 else
4654 Set_Is_Private_Composite (Etype (T));
4655 Set_Is_Private_Composite (T);
4656 end if;
4657 end if;
4659 -- A syntax error in the declaration itself may lead to an empty index
4660 -- list, in which case do a minimal patch.
4662 if No (First_Index (T)) then
4663 Error_Msg_N ("missing index definition in array type declaration", T);
4665 declare
4666 Indexes : constant List_Id :=
4667 New_List (New_Occurrence_Of (Any_Id, Sloc (T)));
4668 begin
4669 Set_Discrete_Subtype_Definitions (Def, Indexes);
4670 Set_First_Index (T, First (Indexes));
4671 return;
4672 end;
4673 end if;
4675 -- Create a concatenation operator for the new type. Internal array
4676 -- types created for packed entities do not need such, they are
4677 -- compatible with the user-defined type.
4679 if Number_Dimensions (T) = 1
4680 and then not Is_Packed_Array_Type (T)
4681 then
4682 New_Concatenation_Op (T);
4683 end if;
4685 -- In the case of an unconstrained array the parser has already verified
4686 -- that all the indexes are unconstrained but we still need to make sure
4687 -- that the element type is constrained.
4689 if Is_Indefinite_Subtype (Element_Type) then
4690 Error_Msg_N
4691 ("unconstrained element type in array declaration",
4692 Subtype_Indication (Component_Def));
4694 elsif Is_Abstract_Type (Element_Type) then
4695 Error_Msg_N
4696 ("the type of a component cannot be abstract",
4697 Subtype_Indication (Component_Def));
4698 end if;
4699 end Array_Type_Declaration;
4701 ------------------------------------------------------
4702 -- Replace_Anonymous_Access_To_Protected_Subprogram --
4703 ------------------------------------------------------
4705 function Replace_Anonymous_Access_To_Protected_Subprogram
4706 (N : Node_Id) return Entity_Id
4708 Loc : constant Source_Ptr := Sloc (N);
4710 Curr_Scope : constant Scope_Stack_Entry :=
4711 Scope_Stack.Table (Scope_Stack.Last);
4713 Anon : constant Entity_Id := Make_Temporary (Loc, 'S');
4714 Acc : Node_Id;
4715 Comp : Node_Id;
4716 Decl : Node_Id;
4717 P : Node_Id;
4719 begin
4720 Set_Is_Internal (Anon);
4722 case Nkind (N) is
4723 when N_Component_Declaration |
4724 N_Unconstrained_Array_Definition |
4725 N_Constrained_Array_Definition =>
4726 Comp := Component_Definition (N);
4727 Acc := Access_Definition (Comp);
4729 when N_Discriminant_Specification =>
4730 Comp := Discriminant_Type (N);
4731 Acc := Comp;
4733 when N_Parameter_Specification =>
4734 Comp := Parameter_Type (N);
4735 Acc := Comp;
4737 when N_Access_Function_Definition =>
4738 Comp := Result_Definition (N);
4739 Acc := Comp;
4741 when N_Object_Declaration =>
4742 Comp := Object_Definition (N);
4743 Acc := Comp;
4745 when N_Function_Specification =>
4746 Comp := Result_Definition (N);
4747 Acc := Comp;
4749 when others =>
4750 raise Program_Error;
4751 end case;
4753 Decl := Make_Full_Type_Declaration (Loc,
4754 Defining_Identifier => Anon,
4755 Type_Definition =>
4756 Copy_Separate_Tree (Access_To_Subprogram_Definition (Acc)));
4758 Mark_Rewrite_Insertion (Decl);
4760 -- Insert the new declaration in the nearest enclosing scope. If the
4761 -- node is a body and N is its return type, the declaration belongs in
4762 -- the enclosing scope.
4764 P := Parent (N);
4766 if Nkind (P) = N_Subprogram_Body
4767 and then Nkind (N) = N_Function_Specification
4768 then
4769 P := Parent (P);
4770 end if;
4772 while Present (P) and then not Has_Declarations (P) loop
4773 P := Parent (P);
4774 end loop;
4776 pragma Assert (Present (P));
4778 if Nkind (P) = N_Package_Specification then
4779 Prepend (Decl, Visible_Declarations (P));
4780 else
4781 Prepend (Decl, Declarations (P));
4782 end if;
4784 -- Replace the anonymous type with an occurrence of the new declaration.
4785 -- In all cases the rewritten node does not have the null-exclusion
4786 -- attribute because (if present) it was already inherited by the
4787 -- anonymous entity (Anon). Thus, in case of components we do not
4788 -- inherit this attribute.
4790 if Nkind (N) = N_Parameter_Specification then
4791 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
4792 Set_Etype (Defining_Identifier (N), Anon);
4793 Set_Null_Exclusion_Present (N, False);
4795 elsif Nkind (N) = N_Object_Declaration then
4796 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
4797 Set_Etype (Defining_Identifier (N), Anon);
4799 elsif Nkind (N) = N_Access_Function_Definition then
4800 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
4802 elsif Nkind (N) = N_Function_Specification then
4803 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
4804 Set_Etype (Defining_Unit_Name (N), Anon);
4806 else
4807 Rewrite (Comp,
4808 Make_Component_Definition (Loc,
4809 Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
4810 end if;
4812 Mark_Rewrite_Insertion (Comp);
4814 if Nkind_In (N, N_Object_Declaration, N_Access_Function_Definition) then
4815 Analyze (Decl);
4817 else
4818 -- Temporarily remove the current scope (record or subprogram) from
4819 -- the stack to add the new declarations to the enclosing scope.
4821 Scope_Stack.Decrement_Last;
4822 Analyze (Decl);
4823 Set_Is_Itype (Anon);
4824 Scope_Stack.Append (Curr_Scope);
4825 end if;
4827 Set_Ekind (Anon, E_Anonymous_Access_Protected_Subprogram_Type);
4828 Set_Can_Use_Internal_Rep (Anon, not Always_Compatible_Rep_On_Target);
4829 return Anon;
4830 end Replace_Anonymous_Access_To_Protected_Subprogram;
4832 -------------------------------
4833 -- Build_Derived_Access_Type --
4834 -------------------------------
4836 procedure Build_Derived_Access_Type
4837 (N : Node_Id;
4838 Parent_Type : Entity_Id;
4839 Derived_Type : Entity_Id)
4841 S : constant Node_Id := Subtype_Indication (Type_Definition (N));
4843 Desig_Type : Entity_Id;
4844 Discr : Entity_Id;
4845 Discr_Con_Elist : Elist_Id;
4846 Discr_Con_El : Elmt_Id;
4847 Subt : Entity_Id;
4849 begin
4850 -- Set the designated type so it is available in case this is an access
4851 -- to a self-referential type, e.g. a standard list type with a next
4852 -- pointer. Will be reset after subtype is built.
4854 Set_Directly_Designated_Type
4855 (Derived_Type, Designated_Type (Parent_Type));
4857 Subt := Process_Subtype (S, N);
4859 if Nkind (S) /= N_Subtype_Indication
4860 and then Subt /= Base_Type (Subt)
4861 then
4862 Set_Ekind (Derived_Type, E_Access_Subtype);
4863 end if;
4865 if Ekind (Derived_Type) = E_Access_Subtype then
4866 declare
4867 Pbase : constant Entity_Id := Base_Type (Parent_Type);
4868 Ibase : constant Entity_Id :=
4869 Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
4870 Svg_Chars : constant Name_Id := Chars (Ibase);
4871 Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
4873 begin
4874 Copy_Node (Pbase, Ibase);
4876 Set_Chars (Ibase, Svg_Chars);
4877 Set_Next_Entity (Ibase, Svg_Next_E);
4878 Set_Sloc (Ibase, Sloc (Derived_Type));
4879 Set_Scope (Ibase, Scope (Derived_Type));
4880 Set_Freeze_Node (Ibase, Empty);
4881 Set_Is_Frozen (Ibase, False);
4882 Set_Comes_From_Source (Ibase, False);
4883 Set_Is_First_Subtype (Ibase, False);
4885 Set_Etype (Ibase, Pbase);
4886 Set_Etype (Derived_Type, Ibase);
4887 end;
4888 end if;
4890 Set_Directly_Designated_Type
4891 (Derived_Type, Designated_Type (Subt));
4893 Set_Is_Constrained (Derived_Type, Is_Constrained (Subt));
4894 Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
4895 Set_Size_Info (Derived_Type, Parent_Type);
4896 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
4897 Set_Depends_On_Private (Derived_Type,
4898 Has_Private_Component (Derived_Type));
4899 Conditional_Delay (Derived_Type, Subt);
4901 -- Ada 2005 (AI-231): Set the null-exclusion attribute, and verify
4902 -- that it is not redundant.
4904 if Null_Exclusion_Present (Type_Definition (N)) then
4905 Set_Can_Never_Be_Null (Derived_Type);
4907 if Can_Never_Be_Null (Parent_Type)
4908 and then False
4909 then
4910 Error_Msg_NE
4911 ("`NOT NULL` not allowed (& already excludes null)",
4912 N, Parent_Type);
4913 end if;
4915 elsif Can_Never_Be_Null (Parent_Type) then
4916 Set_Can_Never_Be_Null (Derived_Type);
4917 end if;
4919 -- Note: we do not copy the Storage_Size_Variable, since we always go to
4920 -- the root type for this information.
4922 -- Apply range checks to discriminants for derived record case
4923 -- ??? THIS CODE SHOULD NOT BE HERE REALLY.
4925 Desig_Type := Designated_Type (Derived_Type);
4926 if Is_Composite_Type (Desig_Type)
4927 and then (not Is_Array_Type (Desig_Type))
4928 and then Has_Discriminants (Desig_Type)
4929 and then Base_Type (Desig_Type) /= Desig_Type
4930 then
4931 Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
4932 Discr_Con_El := First_Elmt (Discr_Con_Elist);
4934 Discr := First_Discriminant (Base_Type (Desig_Type));
4935 while Present (Discr_Con_El) loop
4936 Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
4937 Next_Elmt (Discr_Con_El);
4938 Next_Discriminant (Discr);
4939 end loop;
4940 end if;
4941 end Build_Derived_Access_Type;
4943 ------------------------------
4944 -- Build_Derived_Array_Type --
4945 ------------------------------
4947 procedure Build_Derived_Array_Type
4948 (N : Node_Id;
4949 Parent_Type : Entity_Id;
4950 Derived_Type : Entity_Id)
4952 Loc : constant Source_Ptr := Sloc (N);
4953 Tdef : constant Node_Id := Type_Definition (N);
4954 Indic : constant Node_Id := Subtype_Indication (Tdef);
4955 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
4956 Implicit_Base : Entity_Id;
4957 New_Indic : Node_Id;
4959 procedure Make_Implicit_Base;
4960 -- If the parent subtype is constrained, the derived type is a subtype
4961 -- of an implicit base type derived from the parent base.
4963 ------------------------
4964 -- Make_Implicit_Base --
4965 ------------------------
4967 procedure Make_Implicit_Base is
4968 begin
4969 Implicit_Base :=
4970 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
4972 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
4973 Set_Etype (Implicit_Base, Parent_Base);
4975 Copy_Array_Subtype_Attributes (Implicit_Base, Parent_Base);
4976 Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
4978 Set_Has_Delayed_Freeze (Implicit_Base, True);
4979 end Make_Implicit_Base;
4981 -- Start of processing for Build_Derived_Array_Type
4983 begin
4984 if not Is_Constrained (Parent_Type) then
4985 if Nkind (Indic) /= N_Subtype_Indication then
4986 Set_Ekind (Derived_Type, E_Array_Type);
4988 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
4989 Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
4991 Set_Has_Delayed_Freeze (Derived_Type, True);
4993 else
4994 Make_Implicit_Base;
4995 Set_Etype (Derived_Type, Implicit_Base);
4997 New_Indic :=
4998 Make_Subtype_Declaration (Loc,
4999 Defining_Identifier => Derived_Type,
5000 Subtype_Indication =>
5001 Make_Subtype_Indication (Loc,
5002 Subtype_Mark => New_Reference_To (Implicit_Base, Loc),
5003 Constraint => Constraint (Indic)));
5005 Rewrite (N, New_Indic);
5006 Analyze (N);
5007 end if;
5009 else
5010 if Nkind (Indic) /= N_Subtype_Indication then
5011 Make_Implicit_Base;
5013 Set_Ekind (Derived_Type, Ekind (Parent_Type));
5014 Set_Etype (Derived_Type, Implicit_Base);
5015 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
5017 else
5018 Error_Msg_N ("illegal constraint on constrained type", Indic);
5019 end if;
5020 end if;
5022 -- If parent type is not a derived type itself, and is declared in
5023 -- closed scope (e.g. a subprogram), then we must explicitly introduce
5024 -- the new type's concatenation operator since Derive_Subprograms
5025 -- will not inherit the parent's operator. If the parent type is
5026 -- unconstrained, the operator is of the unconstrained base type.
5028 if Number_Dimensions (Parent_Type) = 1
5029 and then not Is_Limited_Type (Parent_Type)
5030 and then not Is_Derived_Type (Parent_Type)
5031 and then not Is_Package_Or_Generic_Package
5032 (Scope (Base_Type (Parent_Type)))
5033 then
5034 if not Is_Constrained (Parent_Type)
5035 and then Is_Constrained (Derived_Type)
5036 then
5037 New_Concatenation_Op (Implicit_Base);
5038 else
5039 New_Concatenation_Op (Derived_Type);
5040 end if;
5041 end if;
5042 end Build_Derived_Array_Type;
5044 -----------------------------------
5045 -- Build_Derived_Concurrent_Type --
5046 -----------------------------------
5048 procedure Build_Derived_Concurrent_Type
5049 (N : Node_Id;
5050 Parent_Type : Entity_Id;
5051 Derived_Type : Entity_Id)
5053 Loc : constant Source_Ptr := Sloc (N);
5055 Corr_Record : constant Entity_Id := Make_Temporary (Loc, 'C');
5056 Corr_Decl : Node_Id;
5057 Corr_Decl_Needed : Boolean;
5058 -- If the derived type has fewer discriminants than its parent, the
5059 -- corresponding record is also a derived type, in order to account for
5060 -- the bound discriminants. We create a full type declaration for it in
5061 -- this case.
5063 Constraint_Present : constant Boolean :=
5064 Nkind (Subtype_Indication (Type_Definition (N))) =
5065 N_Subtype_Indication;
5067 D_Constraint : Node_Id;
5068 New_Constraint : Elist_Id;
5069 Old_Disc : Entity_Id;
5070 New_Disc : Entity_Id;
5071 New_N : Node_Id;
5073 begin
5074 Set_Stored_Constraint (Derived_Type, No_Elist);
5075 Corr_Decl_Needed := False;
5076 Old_Disc := Empty;
5078 if Present (Discriminant_Specifications (N))
5079 and then Constraint_Present
5080 then
5081 Old_Disc := First_Discriminant (Parent_Type);
5082 New_Disc := First (Discriminant_Specifications (N));
5083 while Present (New_Disc) and then Present (Old_Disc) loop
5084 Next_Discriminant (Old_Disc);
5085 Next (New_Disc);
5086 end loop;
5087 end if;
5089 if Present (Old_Disc) and then Expander_Active then
5091 -- The new type has fewer discriminants, so we need to create a new
5092 -- corresponding record, which is derived from the corresponding
5093 -- record of the parent, and has a stored constraint that captures
5094 -- the values of the discriminant constraints. The corresponding
5095 -- record is needed only if expander is active and code generation is
5096 -- enabled.
5098 -- The type declaration for the derived corresponding record has the
5099 -- same discriminant part and constraints as the current declaration.
5100 -- Copy the unanalyzed tree to build declaration.
5102 Corr_Decl_Needed := True;
5103 New_N := Copy_Separate_Tree (N);
5105 Corr_Decl :=
5106 Make_Full_Type_Declaration (Loc,
5107 Defining_Identifier => Corr_Record,
5108 Discriminant_Specifications =>
5109 Discriminant_Specifications (New_N),
5110 Type_Definition =>
5111 Make_Derived_Type_Definition (Loc,
5112 Subtype_Indication =>
5113 Make_Subtype_Indication (Loc,
5114 Subtype_Mark =>
5115 New_Occurrence_Of
5116 (Corresponding_Record_Type (Parent_Type), Loc),
5117 Constraint =>
5118 Constraint
5119 (Subtype_Indication (Type_Definition (New_N))))));
5120 end if;
5122 -- Copy Storage_Size and Relative_Deadline variables if task case
5124 if Is_Task_Type (Parent_Type) then
5125 Set_Storage_Size_Variable (Derived_Type,
5126 Storage_Size_Variable (Parent_Type));
5127 Set_Relative_Deadline_Variable (Derived_Type,
5128 Relative_Deadline_Variable (Parent_Type));
5129 end if;
5131 if Present (Discriminant_Specifications (N)) then
5132 Push_Scope (Derived_Type);
5133 Check_Or_Process_Discriminants (N, Derived_Type);
5135 if Constraint_Present then
5136 New_Constraint :=
5137 Expand_To_Stored_Constraint
5138 (Parent_Type,
5139 Build_Discriminant_Constraints
5140 (Parent_Type,
5141 Subtype_Indication (Type_Definition (N)), True));
5142 end if;
5144 End_Scope;
5146 elsif Constraint_Present then
5148 -- Build constrained subtype and derive from it
5150 declare
5151 Loc : constant Source_Ptr := Sloc (N);
5152 Anon : constant Entity_Id :=
5153 Make_Defining_Identifier (Loc,
5154 Chars => New_External_Name (Chars (Derived_Type), 'T'));
5155 Decl : Node_Id;
5157 begin
5158 Decl :=
5159 Make_Subtype_Declaration (Loc,
5160 Defining_Identifier => Anon,
5161 Subtype_Indication =>
5162 Subtype_Indication (Type_Definition (N)));
5163 Insert_Before (N, Decl);
5164 Analyze (Decl);
5166 Rewrite (Subtype_Indication (Type_Definition (N)),
5167 New_Occurrence_Of (Anon, Loc));
5168 Set_Analyzed (Derived_Type, False);
5169 Analyze (N);
5170 return;
5171 end;
5172 end if;
5174 -- By default, operations and private data are inherited from parent.
5175 -- However, in the presence of bound discriminants, a new corresponding
5176 -- record will be created, see below.
5178 Set_Has_Discriminants
5179 (Derived_Type, Has_Discriminants (Parent_Type));
5180 Set_Corresponding_Record_Type
5181 (Derived_Type, Corresponding_Record_Type (Parent_Type));
5183 -- Is_Constrained is set according the parent subtype, but is set to
5184 -- False if the derived type is declared with new discriminants.
5186 Set_Is_Constrained
5187 (Derived_Type,
5188 (Is_Constrained (Parent_Type) or else Constraint_Present)
5189 and then not Present (Discriminant_Specifications (N)));
5191 if Constraint_Present then
5192 if not Has_Discriminants (Parent_Type) then
5193 Error_Msg_N ("untagged parent must have discriminants", N);
5195 elsif Present (Discriminant_Specifications (N)) then
5197 -- Verify that new discriminants are used to constrain old ones
5199 D_Constraint :=
5200 First
5201 (Constraints
5202 (Constraint (Subtype_Indication (Type_Definition (N)))));
5204 Old_Disc := First_Discriminant (Parent_Type);
5206 while Present (D_Constraint) loop
5207 if Nkind (D_Constraint) /= N_Discriminant_Association then
5209 -- Positional constraint. If it is a reference to a new
5210 -- discriminant, it constrains the corresponding old one.
5212 if Nkind (D_Constraint) = N_Identifier then
5213 New_Disc := First_Discriminant (Derived_Type);
5214 while Present (New_Disc) loop
5215 exit when Chars (New_Disc) = Chars (D_Constraint);
5216 Next_Discriminant (New_Disc);
5217 end loop;
5219 if Present (New_Disc) then
5220 Set_Corresponding_Discriminant (New_Disc, Old_Disc);
5221 end if;
5222 end if;
5224 Next_Discriminant (Old_Disc);
5226 -- if this is a named constraint, search by name for the old
5227 -- discriminants constrained by the new one.
5229 elsif Nkind (Expression (D_Constraint)) = N_Identifier then
5231 -- Find new discriminant with that name
5233 New_Disc := First_Discriminant (Derived_Type);
5234 while Present (New_Disc) loop
5235 exit when
5236 Chars (New_Disc) = Chars (Expression (D_Constraint));
5237 Next_Discriminant (New_Disc);
5238 end loop;
5240 if Present (New_Disc) then
5242 -- Verify that new discriminant renames some discriminant
5243 -- of the parent type, and associate the new discriminant
5244 -- with one or more old ones that it renames.
5246 declare
5247 Selector : Node_Id;
5249 begin
5250 Selector := First (Selector_Names (D_Constraint));
5251 while Present (Selector) loop
5252 Old_Disc := First_Discriminant (Parent_Type);
5253 while Present (Old_Disc) loop
5254 exit when Chars (Old_Disc) = Chars (Selector);
5255 Next_Discriminant (Old_Disc);
5256 end loop;
5258 if Present (Old_Disc) then
5259 Set_Corresponding_Discriminant
5260 (New_Disc, Old_Disc);
5261 end if;
5263 Next (Selector);
5264 end loop;
5265 end;
5266 end if;
5267 end if;
5269 Next (D_Constraint);
5270 end loop;
5272 New_Disc := First_Discriminant (Derived_Type);
5273 while Present (New_Disc) loop
5274 if No (Corresponding_Discriminant (New_Disc)) then
5275 Error_Msg_NE
5276 ("new discriminant& must constrain old one", N, New_Disc);
5278 elsif not
5279 Subtypes_Statically_Compatible
5280 (Etype (New_Disc),
5281 Etype (Corresponding_Discriminant (New_Disc)))
5282 then
5283 Error_Msg_NE
5284 ("& not statically compatible with parent discriminant",
5285 N, New_Disc);
5286 end if;
5288 Next_Discriminant (New_Disc);
5289 end loop;
5290 end if;
5292 elsif Present (Discriminant_Specifications (N)) then
5293 Error_Msg_N
5294 ("missing discriminant constraint in untagged derivation", N);
5295 end if;
5297 -- The entity chain of the derived type includes the new discriminants
5298 -- but shares operations with the parent.
5300 if Present (Discriminant_Specifications (N)) then
5301 Old_Disc := First_Discriminant (Parent_Type);
5302 while Present (Old_Disc) loop
5303 if No (Next_Entity (Old_Disc))
5304 or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
5305 then
5306 Set_Next_Entity
5307 (Last_Entity (Derived_Type), Next_Entity (Old_Disc));
5308 exit;
5309 end if;
5311 Next_Discriminant (Old_Disc);
5312 end loop;
5314 else
5315 Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
5316 if Has_Discriminants (Parent_Type) then
5317 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
5318 Set_Discriminant_Constraint (
5319 Derived_Type, Discriminant_Constraint (Parent_Type));
5320 end if;
5321 end if;
5323 Set_Last_Entity (Derived_Type, Last_Entity (Parent_Type));
5325 Set_Has_Completion (Derived_Type);
5327 if Corr_Decl_Needed then
5328 Set_Stored_Constraint (Derived_Type, New_Constraint);
5329 Insert_After (N, Corr_Decl);
5330 Analyze (Corr_Decl);
5331 Set_Corresponding_Record_Type (Derived_Type, Corr_Record);
5332 end if;
5333 end Build_Derived_Concurrent_Type;
5335 ------------------------------------
5336 -- Build_Derived_Enumeration_Type --
5337 ------------------------------------
5339 procedure Build_Derived_Enumeration_Type
5340 (N : Node_Id;
5341 Parent_Type : Entity_Id;
5342 Derived_Type : Entity_Id)
5344 Loc : constant Source_Ptr := Sloc (N);
5345 Def : constant Node_Id := Type_Definition (N);
5346 Indic : constant Node_Id := Subtype_Indication (Def);
5347 Implicit_Base : Entity_Id;
5348 Literal : Entity_Id;
5349 New_Lit : Entity_Id;
5350 Literals_List : List_Id;
5351 Type_Decl : Node_Id;
5352 Hi, Lo : Node_Id;
5353 Rang_Expr : Node_Id;
5355 begin
5356 -- Since types Standard.Character and Standard.[Wide_]Wide_Character do
5357 -- not have explicit literals lists we need to process types derived
5358 -- from them specially. This is handled by Derived_Standard_Character.
5359 -- If the parent type is a generic type, there are no literals either,
5360 -- and we construct the same skeletal representation as for the generic
5361 -- parent type.
5363 if Is_Standard_Character_Type (Parent_Type) then
5364 Derived_Standard_Character (N, Parent_Type, Derived_Type);
5366 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
5367 declare
5368 Lo : Node_Id;
5369 Hi : Node_Id;
5371 begin
5372 if Nkind (Indic) /= N_Subtype_Indication then
5373 Lo :=
5374 Make_Attribute_Reference (Loc,
5375 Attribute_Name => Name_First,
5376 Prefix => New_Reference_To (Derived_Type, Loc));
5377 Set_Etype (Lo, Derived_Type);
5379 Hi :=
5380 Make_Attribute_Reference (Loc,
5381 Attribute_Name => Name_Last,
5382 Prefix => New_Reference_To (Derived_Type, Loc));
5383 Set_Etype (Hi, Derived_Type);
5385 Set_Scalar_Range (Derived_Type,
5386 Make_Range (Loc,
5387 Low_Bound => Lo,
5388 High_Bound => Hi));
5389 else
5391 -- Analyze subtype indication and verify compatibility
5392 -- with parent type.
5394 if Base_Type (Process_Subtype (Indic, N)) /=
5395 Base_Type (Parent_Type)
5396 then
5397 Error_Msg_N
5398 ("illegal constraint for formal discrete type", N);
5399 end if;
5400 end if;
5401 end;
5403 else
5404 -- If a constraint is present, analyze the bounds to catch
5405 -- premature usage of the derived literals.
5407 if Nkind (Indic) = N_Subtype_Indication
5408 and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
5409 then
5410 Analyze (Low_Bound (Range_Expression (Constraint (Indic))));
5411 Analyze (High_Bound (Range_Expression (Constraint (Indic))));
5412 end if;
5414 -- Introduce an implicit base type for the derived type even if there
5415 -- is no constraint attached to it, since this seems closer to the
5416 -- Ada semantics. Build a full type declaration tree for the derived
5417 -- type using the implicit base type as the defining identifier. The
5418 -- build a subtype declaration tree which applies the constraint (if
5419 -- any) have it replace the derived type declaration.
5421 Literal := First_Literal (Parent_Type);
5422 Literals_List := New_List;
5423 while Present (Literal)
5424 and then Ekind (Literal) = E_Enumeration_Literal
5425 loop
5426 -- Literals of the derived type have the same representation as
5427 -- those of the parent type, but this representation can be
5428 -- overridden by an explicit representation clause. Indicate
5429 -- that there is no explicit representation given yet. These
5430 -- derived literals are implicit operations of the new type,
5431 -- and can be overridden by explicit ones.
5433 if Nkind (Literal) = N_Defining_Character_Literal then
5434 New_Lit :=
5435 Make_Defining_Character_Literal (Loc, Chars (Literal));
5436 else
5437 New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
5438 end if;
5440 Set_Ekind (New_Lit, E_Enumeration_Literal);
5441 Set_Enumeration_Pos (New_Lit, Enumeration_Pos (Literal));
5442 Set_Enumeration_Rep (New_Lit, Enumeration_Rep (Literal));
5443 Set_Enumeration_Rep_Expr (New_Lit, Empty);
5444 Set_Alias (New_Lit, Literal);
5445 Set_Is_Known_Valid (New_Lit, True);
5447 Append (New_Lit, Literals_List);
5448 Next_Literal (Literal);
5449 end loop;
5451 Implicit_Base :=
5452 Make_Defining_Identifier (Sloc (Derived_Type),
5453 Chars => New_External_Name (Chars (Derived_Type), 'B'));
5455 -- Indicate the proper nature of the derived type. This must be done
5456 -- before analysis of the literals, to recognize cases when a literal
5457 -- may be hidden by a previous explicit function definition (cf.
5458 -- c83031a).
5460 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
5461 Set_Etype (Derived_Type, Implicit_Base);
5463 Type_Decl :=
5464 Make_Full_Type_Declaration (Loc,
5465 Defining_Identifier => Implicit_Base,
5466 Discriminant_Specifications => No_List,
5467 Type_Definition =>
5468 Make_Enumeration_Type_Definition (Loc, Literals_List));
5470 Mark_Rewrite_Insertion (Type_Decl);
5471 Insert_Before (N, Type_Decl);
5472 Analyze (Type_Decl);
5474 -- After the implicit base is analyzed its Etype needs to be changed
5475 -- to reflect the fact that it is derived from the parent type which
5476 -- was ignored during analysis. We also set the size at this point.
5478 Set_Etype (Implicit_Base, Parent_Type);
5480 Set_Size_Info (Implicit_Base, Parent_Type);
5481 Set_RM_Size (Implicit_Base, RM_Size (Parent_Type));
5482 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
5484 -- Copy other flags from parent type
5486 Set_Has_Non_Standard_Rep
5487 (Implicit_Base, Has_Non_Standard_Rep
5488 (Parent_Type));
5489 Set_Has_Pragma_Ordered
5490 (Implicit_Base, Has_Pragma_Ordered
5491 (Parent_Type));
5492 Set_Has_Delayed_Freeze (Implicit_Base);
5494 -- Process the subtype indication including a validation check on the
5495 -- constraint, if any. If a constraint is given, its bounds must be
5496 -- implicitly converted to the new type.
5498 if Nkind (Indic) = N_Subtype_Indication then
5499 declare
5500 R : constant Node_Id :=
5501 Range_Expression (Constraint (Indic));
5503 begin
5504 if Nkind (R) = N_Range then
5505 Hi := Build_Scalar_Bound
5506 (High_Bound (R), Parent_Type, Implicit_Base);
5507 Lo := Build_Scalar_Bound
5508 (Low_Bound (R), Parent_Type, Implicit_Base);
5510 else
5511 -- Constraint is a Range attribute. Replace with explicit
5512 -- mention of the bounds of the prefix, which must be a
5513 -- subtype.
5515 Analyze (Prefix (R));
5516 Hi :=
5517 Convert_To (Implicit_Base,
5518 Make_Attribute_Reference (Loc,
5519 Attribute_Name => Name_Last,
5520 Prefix =>
5521 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
5523 Lo :=
5524 Convert_To (Implicit_Base,
5525 Make_Attribute_Reference (Loc,
5526 Attribute_Name => Name_First,
5527 Prefix =>
5528 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
5529 end if;
5530 end;
5532 else
5533 Hi :=
5534 Build_Scalar_Bound
5535 (Type_High_Bound (Parent_Type),
5536 Parent_Type, Implicit_Base);
5537 Lo :=
5538 Build_Scalar_Bound
5539 (Type_Low_Bound (Parent_Type),
5540 Parent_Type, Implicit_Base);
5541 end if;
5543 Rang_Expr :=
5544 Make_Range (Loc,
5545 Low_Bound => Lo,
5546 High_Bound => Hi);
5548 -- If we constructed a default range for the case where no range
5549 -- was given, then the expressions in the range must not freeze
5550 -- since they do not correspond to expressions in the source.
5552 if Nkind (Indic) /= N_Subtype_Indication then
5553 Set_Must_Not_Freeze (Lo);
5554 Set_Must_Not_Freeze (Hi);
5555 Set_Must_Not_Freeze (Rang_Expr);
5556 end if;
5558 Rewrite (N,
5559 Make_Subtype_Declaration (Loc,
5560 Defining_Identifier => Derived_Type,
5561 Subtype_Indication =>
5562 Make_Subtype_Indication (Loc,
5563 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
5564 Constraint =>
5565 Make_Range_Constraint (Loc,
5566 Range_Expression => Rang_Expr))));
5568 Analyze (N);
5570 -- If pragma Discard_Names applies on the first subtype of the parent
5571 -- type, then it must be applied on this subtype as well.
5573 if Einfo.Discard_Names (First_Subtype (Parent_Type)) then
5574 Set_Discard_Names (Derived_Type);
5575 end if;
5577 -- Apply a range check. Since this range expression doesn't have an
5578 -- Etype, we have to specifically pass the Source_Typ parameter. Is
5579 -- this right???
5581 if Nkind (Indic) = N_Subtype_Indication then
5582 Apply_Range_Check (Range_Expression (Constraint (Indic)),
5583 Parent_Type,
5584 Source_Typ => Entity (Subtype_Mark (Indic)));
5585 end if;
5586 end if;
5587 end Build_Derived_Enumeration_Type;
5589 --------------------------------
5590 -- Build_Derived_Numeric_Type --
5591 --------------------------------
5593 procedure Build_Derived_Numeric_Type
5594 (N : Node_Id;
5595 Parent_Type : Entity_Id;
5596 Derived_Type : Entity_Id)
5598 Loc : constant Source_Ptr := Sloc (N);
5599 Tdef : constant Node_Id := Type_Definition (N);
5600 Indic : constant Node_Id := Subtype_Indication (Tdef);
5601 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
5602 No_Constraint : constant Boolean := Nkind (Indic) /=
5603 N_Subtype_Indication;
5604 Implicit_Base : Entity_Id;
5606 Lo : Node_Id;
5607 Hi : Node_Id;
5609 begin
5610 -- Process the subtype indication including a validation check on
5611 -- the constraint if any.
5613 Discard_Node (Process_Subtype (Indic, N));
5615 -- Introduce an implicit base type for the derived type even if there
5616 -- is no constraint attached to it, since this seems closer to the Ada
5617 -- semantics.
5619 Implicit_Base :=
5620 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
5622 Set_Etype (Implicit_Base, Parent_Base);
5623 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
5624 Set_Size_Info (Implicit_Base, Parent_Base);
5625 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
5626 Set_Parent (Implicit_Base, Parent (Derived_Type));
5627 Set_Is_Known_Valid (Implicit_Base, Is_Known_Valid (Parent_Base));
5629 -- Set RM Size for discrete type or decimal fixed-point type
5630 -- Ordinary fixed-point is excluded, why???
5632 if Is_Discrete_Type (Parent_Base)
5633 or else Is_Decimal_Fixed_Point_Type (Parent_Base)
5634 then
5635 Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
5636 end if;
5638 Set_Has_Delayed_Freeze (Implicit_Base);
5640 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
5641 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
5643 Set_Scalar_Range (Implicit_Base,
5644 Make_Range (Loc,
5645 Low_Bound => Lo,
5646 High_Bound => Hi));
5648 if Has_Infinities (Parent_Base) then
5649 Set_Includes_Infinities (Scalar_Range (Implicit_Base));
5650 end if;
5652 -- The Derived_Type, which is the entity of the declaration, is a
5653 -- subtype of the implicit base. Its Ekind is a subtype, even in the
5654 -- absence of an explicit constraint.
5656 Set_Etype (Derived_Type, Implicit_Base);
5658 -- If we did not have a constraint, then the Ekind is set from the
5659 -- parent type (otherwise Process_Subtype has set the bounds)
5661 if No_Constraint then
5662 Set_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
5663 end if;
5665 -- If we did not have a range constraint, then set the range from the
5666 -- parent type. Otherwise, the Process_Subtype call has set the bounds.
5668 if No_Constraint
5669 or else not Has_Range_Constraint (Indic)
5670 then
5671 Set_Scalar_Range (Derived_Type,
5672 Make_Range (Loc,
5673 Low_Bound => New_Copy_Tree (Type_Low_Bound (Parent_Type)),
5674 High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
5675 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
5677 if Has_Infinities (Parent_Type) then
5678 Set_Includes_Infinities (Scalar_Range (Derived_Type));
5679 end if;
5681 Set_Is_Known_Valid (Derived_Type, Is_Known_Valid (Parent_Type));
5682 end if;
5684 Set_Is_Descendent_Of_Address (Derived_Type,
5685 Is_Descendent_Of_Address (Parent_Type));
5686 Set_Is_Descendent_Of_Address (Implicit_Base,
5687 Is_Descendent_Of_Address (Parent_Type));
5689 -- Set remaining type-specific fields, depending on numeric type
5691 if Is_Modular_Integer_Type (Parent_Type) then
5692 Set_Modulus (Implicit_Base, Modulus (Parent_Base));
5694 Set_Non_Binary_Modulus
5695 (Implicit_Base, Non_Binary_Modulus (Parent_Base));
5697 Set_Is_Known_Valid
5698 (Implicit_Base, Is_Known_Valid (Parent_Base));
5700 elsif Is_Floating_Point_Type (Parent_Type) then
5702 -- Digits of base type is always copied from the digits value of
5703 -- the parent base type, but the digits of the derived type will
5704 -- already have been set if there was a constraint present.
5706 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
5707 Set_Float_Rep (Implicit_Base, Float_Rep (Parent_Base));
5709 if No_Constraint then
5710 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
5711 end if;
5713 elsif Is_Fixed_Point_Type (Parent_Type) then
5715 -- Small of base type and derived type are always copied from the
5716 -- parent base type, since smalls never change. The delta of the
5717 -- base type is also copied from the parent base type. However the
5718 -- delta of the derived type will have been set already if a
5719 -- constraint was present.
5721 Set_Small_Value (Derived_Type, Small_Value (Parent_Base));
5722 Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
5723 Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
5725 if No_Constraint then
5726 Set_Delta_Value (Derived_Type, Delta_Value (Parent_Type));
5727 end if;
5729 -- The scale and machine radix in the decimal case are always
5730 -- copied from the parent base type.
5732 if Is_Decimal_Fixed_Point_Type (Parent_Type) then
5733 Set_Scale_Value (Derived_Type, Scale_Value (Parent_Base));
5734 Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
5736 Set_Machine_Radix_10
5737 (Derived_Type, Machine_Radix_10 (Parent_Base));
5738 Set_Machine_Radix_10
5739 (Implicit_Base, Machine_Radix_10 (Parent_Base));
5741 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
5743 if No_Constraint then
5744 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
5746 else
5747 -- the analysis of the subtype_indication sets the
5748 -- digits value of the derived type.
5750 null;
5751 end if;
5752 end if;
5753 end if;
5755 -- The type of the bounds is that of the parent type, and they
5756 -- must be converted to the derived type.
5758 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
5760 -- The implicit_base should be frozen when the derived type is frozen,
5761 -- but note that it is used in the conversions of the bounds. For fixed
5762 -- types we delay the determination of the bounds until the proper
5763 -- freezing point. For other numeric types this is rejected by GCC, for
5764 -- reasons that are currently unclear (???), so we choose to freeze the
5765 -- implicit base now. In the case of integers and floating point types
5766 -- this is harmless because subsequent representation clauses cannot
5767 -- affect anything, but it is still baffling that we cannot use the
5768 -- same mechanism for all derived numeric types.
5770 -- There is a further complication: actually *some* representation
5771 -- clauses can affect the implicit base type. Namely, attribute
5772 -- definition clauses for stream-oriented attributes need to set the
5773 -- corresponding TSS entries on the base type, and this normally cannot
5774 -- be done after the base type is frozen, so the circuitry in
5775 -- Sem_Ch13.New_Stream_Subprogram must account for this possibility and
5776 -- not use Set_TSS in this case.
5778 if Is_Fixed_Point_Type (Parent_Type) then
5779 Conditional_Delay (Implicit_Base, Parent_Type);
5780 else
5781 Freeze_Before (N, Implicit_Base);
5782 end if;
5783 end Build_Derived_Numeric_Type;
5785 --------------------------------
5786 -- Build_Derived_Private_Type --
5787 --------------------------------
5789 procedure Build_Derived_Private_Type
5790 (N : Node_Id;
5791 Parent_Type : Entity_Id;
5792 Derived_Type : Entity_Id;
5793 Is_Completion : Boolean;
5794 Derive_Subps : Boolean := True)
5796 Loc : constant Source_Ptr := Sloc (N);
5797 Der_Base : Entity_Id;
5798 Discr : Entity_Id;
5799 Full_Decl : Node_Id := Empty;
5800 Full_Der : Entity_Id;
5801 Full_P : Entity_Id;
5802 Last_Discr : Entity_Id;
5803 Par_Scope : constant Entity_Id := Scope (Base_Type (Parent_Type));
5804 Swapped : Boolean := False;
5806 procedure Copy_And_Build;
5807 -- Copy derived type declaration, replace parent with its full view,
5808 -- and analyze new declaration.
5810 --------------------
5811 -- Copy_And_Build --
5812 --------------------
5814 procedure Copy_And_Build is
5815 Full_N : Node_Id;
5817 begin
5818 if Ekind (Parent_Type) in Record_Kind
5819 or else
5820 (Ekind (Parent_Type) in Enumeration_Kind
5821 and then not Is_Standard_Character_Type (Parent_Type)
5822 and then not Is_Generic_Type (Root_Type (Parent_Type)))
5823 then
5824 Full_N := New_Copy_Tree (N);
5825 Insert_After (N, Full_N);
5826 Build_Derived_Type (
5827 Full_N, Parent_Type, Full_Der, True, Derive_Subps => False);
5829 else
5830 Build_Derived_Type (
5831 N, Parent_Type, Full_Der, True, Derive_Subps => False);
5832 end if;
5833 end Copy_And_Build;
5835 -- Start of processing for Build_Derived_Private_Type
5837 begin
5838 if Is_Tagged_Type (Parent_Type) then
5839 Full_P := Full_View (Parent_Type);
5841 -- A type extension of a type with unknown discriminants is an
5842 -- indefinite type that the back-end cannot handle directly.
5843 -- We treat it as a private type, and build a completion that is
5844 -- derived from the full view of the parent, and hopefully has
5845 -- known discriminants.
5847 -- If the full view of the parent type has an underlying record view,
5848 -- use it to generate the underlying record view of this derived type
5849 -- (required for chains of derivations with unknown discriminants).
5851 -- Minor optimization: we avoid the generation of useless underlying
5852 -- record view entities if the private type declaration has unknown
5853 -- discriminants but its corresponding full view has no
5854 -- discriminants.
5856 if Has_Unknown_Discriminants (Parent_Type)
5857 and then Present (Full_P)
5858 and then (Has_Discriminants (Full_P)
5859 or else Present (Underlying_Record_View (Full_P)))
5860 and then not In_Open_Scopes (Par_Scope)
5861 and then Expander_Active
5862 then
5863 declare
5864 Full_Der : constant Entity_Id := Make_Temporary (Loc, 'T');
5865 New_Ext : constant Node_Id :=
5866 Copy_Separate_Tree
5867 (Record_Extension_Part (Type_Definition (N)));
5868 Decl : Node_Id;
5870 begin
5871 Build_Derived_Record_Type
5872 (N, Parent_Type, Derived_Type, Derive_Subps);
5874 -- Build anonymous completion, as a derivation from the full
5875 -- view of the parent. This is not a completion in the usual
5876 -- sense, because the current type is not private.
5878 Decl :=
5879 Make_Full_Type_Declaration (Loc,
5880 Defining_Identifier => Full_Der,
5881 Type_Definition =>
5882 Make_Derived_Type_Definition (Loc,
5883 Subtype_Indication =>
5884 New_Copy_Tree
5885 (Subtype_Indication (Type_Definition (N))),
5886 Record_Extension_Part => New_Ext));
5888 -- If the parent type has an underlying record view, use it
5889 -- here to build the new underlying record view.
5891 if Present (Underlying_Record_View (Full_P)) then
5892 pragma Assert
5893 (Nkind (Subtype_Indication (Type_Definition (Decl)))
5894 = N_Identifier);
5895 Set_Entity (Subtype_Indication (Type_Definition (Decl)),
5896 Underlying_Record_View (Full_P));
5897 end if;
5899 Install_Private_Declarations (Par_Scope);
5900 Install_Visible_Declarations (Par_Scope);
5901 Insert_Before (N, Decl);
5903 -- Mark entity as an underlying record view before analysis,
5904 -- to avoid generating the list of its primitive operations
5905 -- (which is not really required for this entity) and thus
5906 -- prevent spurious errors associated with missing overriding
5907 -- of abstract primitives (overridden only for Derived_Type).
5909 Set_Ekind (Full_Der, E_Record_Type);
5910 Set_Is_Underlying_Record_View (Full_Der);
5912 Analyze (Decl);
5914 pragma Assert (Has_Discriminants (Full_Der)
5915 and then not Has_Unknown_Discriminants (Full_Der));
5917 Uninstall_Declarations (Par_Scope);
5919 -- Freeze the underlying record view, to prevent generation of
5920 -- useless dispatching information, which is simply shared with
5921 -- the real derived type.
5923 Set_Is_Frozen (Full_Der);
5925 -- Set up links between real entity and underlying record view
5927 Set_Underlying_Record_View (Derived_Type, Base_Type (Full_Der));
5928 Set_Underlying_Record_View (Base_Type (Full_Der), Derived_Type);
5929 end;
5931 -- If discriminants are known, build derived record
5933 else
5934 Build_Derived_Record_Type
5935 (N, Parent_Type, Derived_Type, Derive_Subps);
5936 end if;
5938 return;
5940 elsif Has_Discriminants (Parent_Type) then
5941 if Present (Full_View (Parent_Type)) then
5942 if not Is_Completion then
5944 -- Copy declaration for subsequent analysis, to provide a
5945 -- completion for what is a private declaration. Indicate that
5946 -- the full type is internally generated.
5948 Full_Decl := New_Copy_Tree (N);
5949 Full_Der := New_Copy (Derived_Type);
5950 Set_Comes_From_Source (Full_Decl, False);
5951 Set_Comes_From_Source (Full_Der, False);
5952 Set_Parent (Full_Der, Full_Decl);
5954 Insert_After (N, Full_Decl);
5956 else
5957 -- If this is a completion, the full view being built is itself
5958 -- private. We build a subtype of the parent with the same
5959 -- constraints as this full view, to convey to the back end the
5960 -- constrained components and the size of this subtype. If the
5961 -- parent is constrained, its full view can serve as the
5962 -- underlying full view of the derived type.
5964 if No (Discriminant_Specifications (N)) then
5965 if Nkind (Subtype_Indication (Type_Definition (N))) =
5966 N_Subtype_Indication
5967 then
5968 Build_Underlying_Full_View (N, Derived_Type, Parent_Type);
5970 elsif Is_Constrained (Full_View (Parent_Type)) then
5971 Set_Underlying_Full_View
5972 (Derived_Type, Full_View (Parent_Type));
5973 end if;
5975 else
5976 -- If there are new discriminants, the parent subtype is
5977 -- constrained by them, but it is not clear how to build
5978 -- the Underlying_Full_View in this case???
5980 null;
5981 end if;
5982 end if;
5983 end if;
5985 -- Build partial view of derived type from partial view of parent
5987 Build_Derived_Record_Type
5988 (N, Parent_Type, Derived_Type, Derive_Subps);
5990 if Present (Full_View (Parent_Type)) and then not Is_Completion then
5991 if not In_Open_Scopes (Par_Scope)
5992 or else not In_Same_Source_Unit (N, Parent_Type)
5993 then
5994 -- Swap partial and full views temporarily
5996 Install_Private_Declarations (Par_Scope);
5997 Install_Visible_Declarations (Par_Scope);
5998 Swapped := True;
5999 end if;
6001 -- Build full view of derived type from full view of parent which
6002 -- is now installed. Subprograms have been derived on the partial
6003 -- view, the completion does not derive them anew.
6005 if not Is_Tagged_Type (Parent_Type) then
6007 -- If the parent is itself derived from another private type,
6008 -- installing the private declarations has not affected its
6009 -- privacy status, so use its own full view explicitly.
6011 if Is_Private_Type (Parent_Type) then
6012 Build_Derived_Record_Type
6013 (Full_Decl, Full_View (Parent_Type), Full_Der, False);
6014 else
6015 Build_Derived_Record_Type
6016 (Full_Decl, Parent_Type, Full_Der, False);
6017 end if;
6019 else
6020 -- If full view of parent is tagged, the completion inherits
6021 -- the proper primitive operations.
6023 Set_Defining_Identifier (Full_Decl, Full_Der);
6024 Build_Derived_Record_Type
6025 (Full_Decl, Parent_Type, Full_Der, Derive_Subps);
6026 end if;
6028 -- The full declaration has been introduced into the tree and
6029 -- processed in the step above. It should not be analyzed again
6030 -- (when encountered later in the current list of declarations)
6031 -- to prevent spurious name conflicts. The full entity remains
6032 -- invisible.
6034 Set_Analyzed (Full_Decl);
6036 if Swapped then
6037 Uninstall_Declarations (Par_Scope);
6039 if In_Open_Scopes (Par_Scope) then
6040 Install_Visible_Declarations (Par_Scope);
6041 end if;
6042 end if;
6044 Der_Base := Base_Type (Derived_Type);
6045 Set_Full_View (Derived_Type, Full_Der);
6046 Set_Full_View (Der_Base, Base_Type (Full_Der));
6048 -- Copy the discriminant list from full view to the partial views
6049 -- (base type and its subtype). Gigi requires that the partial and
6050 -- full views have the same discriminants.
6052 -- Note that since the partial view is pointing to discriminants
6053 -- in the full view, their scope will be that of the full view.
6054 -- This might cause some front end problems and need adjustment???
6056 Discr := First_Discriminant (Base_Type (Full_Der));
6057 Set_First_Entity (Der_Base, Discr);
6059 loop
6060 Last_Discr := Discr;
6061 Next_Discriminant (Discr);
6062 exit when No (Discr);
6063 end loop;
6065 Set_Last_Entity (Der_Base, Last_Discr);
6067 Set_First_Entity (Derived_Type, First_Entity (Der_Base));
6068 Set_Last_Entity (Derived_Type, Last_Entity (Der_Base));
6069 Set_Stored_Constraint (Full_Der, Stored_Constraint (Derived_Type));
6071 else
6072 -- If this is a completion, the derived type stays private and
6073 -- there is no need to create a further full view, except in the
6074 -- unusual case when the derivation is nested within a child unit,
6075 -- see below.
6077 null;
6078 end if;
6080 elsif Present (Full_View (Parent_Type))
6081 and then Has_Discriminants (Full_View (Parent_Type))
6082 then
6083 if Has_Unknown_Discriminants (Parent_Type)
6084 and then Nkind (Subtype_Indication (Type_Definition (N))) =
6085 N_Subtype_Indication
6086 then
6087 Error_Msg_N
6088 ("cannot constrain type with unknown discriminants",
6089 Subtype_Indication (Type_Definition (N)));
6090 return;
6091 end if;
6093 -- If full view of parent is a record type, build full view as a
6094 -- derivation from the parent's full view. Partial view remains
6095 -- private. For code generation and linking, the full view must have
6096 -- the same public status as the partial one. This full view is only
6097 -- needed if the parent type is in an enclosing scope, so that the
6098 -- full view may actually become visible, e.g. in a child unit. This
6099 -- is both more efficient, and avoids order of freezing problems with
6100 -- the added entities.
6102 if not Is_Private_Type (Full_View (Parent_Type))
6103 and then (In_Open_Scopes (Scope (Parent_Type)))
6104 then
6105 Full_Der :=
6106 Make_Defining_Identifier
6107 (Sloc (Derived_Type), Chars (Derived_Type));
6108 Set_Is_Itype (Full_Der);
6109 Set_Has_Private_Declaration (Full_Der);
6110 Set_Has_Private_Declaration (Derived_Type);
6111 Set_Associated_Node_For_Itype (Full_Der, N);
6112 Set_Parent (Full_Der, Parent (Derived_Type));
6113 Set_Full_View (Derived_Type, Full_Der);
6114 Set_Is_Public (Full_Der, Is_Public (Derived_Type));
6115 Full_P := Full_View (Parent_Type);
6116 Exchange_Declarations (Parent_Type);
6117 Copy_And_Build;
6118 Exchange_Declarations (Full_P);
6120 else
6121 Build_Derived_Record_Type
6122 (N, Full_View (Parent_Type), Derived_Type,
6123 Derive_Subps => False);
6124 end if;
6126 -- In any case, the primitive operations are inherited from the
6127 -- parent type, not from the internal full view.
6129 Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
6131 if Derive_Subps then
6132 Derive_Subprograms (Parent_Type, Derived_Type);
6133 end if;
6135 else
6136 -- Untagged type, No discriminants on either view
6138 if Nkind (Subtype_Indication (Type_Definition (N))) =
6139 N_Subtype_Indication
6140 then
6141 Error_Msg_N
6142 ("illegal constraint on type without discriminants", N);
6143 end if;
6145 if Present (Discriminant_Specifications (N))
6146 and then Present (Full_View (Parent_Type))
6147 and then not Is_Tagged_Type (Full_View (Parent_Type))
6148 then
6149 Error_Msg_N ("cannot add discriminants to untagged type", N);
6150 end if;
6152 Set_Stored_Constraint (Derived_Type, No_Elist);
6153 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
6154 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
6155 Set_Has_Controlled_Component
6156 (Derived_Type, Has_Controlled_Component
6157 (Parent_Type));
6159 -- Direct controlled types do not inherit Finalize_Storage_Only flag
6161 if not Is_Controlled (Parent_Type) then
6162 Set_Finalize_Storage_Only
6163 (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
6164 end if;
6166 -- Construct the implicit full view by deriving from full view of the
6167 -- parent type. In order to get proper visibility, we install the
6168 -- parent scope and its declarations.
6170 -- ??? If the parent is untagged private and its completion is
6171 -- tagged, this mechanism will not work because we cannot derive from
6172 -- the tagged full view unless we have an extension.
6174 if Present (Full_View (Parent_Type))
6175 and then not Is_Tagged_Type (Full_View (Parent_Type))
6176 and then not Is_Completion
6177 then
6178 Full_Der :=
6179 Make_Defining_Identifier
6180 (Sloc (Derived_Type), Chars (Derived_Type));
6181 Set_Is_Itype (Full_Der);
6182 Set_Has_Private_Declaration (Full_Der);
6183 Set_Has_Private_Declaration (Derived_Type);
6184 Set_Associated_Node_For_Itype (Full_Der, N);
6185 Set_Parent (Full_Der, Parent (Derived_Type));
6186 Set_Full_View (Derived_Type, Full_Der);
6188 if not In_Open_Scopes (Par_Scope) then
6189 Install_Private_Declarations (Par_Scope);
6190 Install_Visible_Declarations (Par_Scope);
6191 Copy_And_Build;
6192 Uninstall_Declarations (Par_Scope);
6194 -- If parent scope is open and in another unit, and parent has a
6195 -- completion, then the derivation is taking place in the visible
6196 -- part of a child unit. In that case retrieve the full view of
6197 -- the parent momentarily.
6199 elsif not In_Same_Source_Unit (N, Parent_Type) then
6200 Full_P := Full_View (Parent_Type);
6201 Exchange_Declarations (Parent_Type);
6202 Copy_And_Build;
6203 Exchange_Declarations (Full_P);
6205 -- Otherwise it is a local derivation
6207 else
6208 Copy_And_Build;
6209 end if;
6211 Set_Scope (Full_Der, Current_Scope);
6212 Set_Is_First_Subtype (Full_Der,
6213 Is_First_Subtype (Derived_Type));
6214 Set_Has_Size_Clause (Full_Der, False);
6215 Set_Has_Alignment_Clause (Full_Der, False);
6216 Set_Next_Entity (Full_Der, Empty);
6217 Set_Has_Delayed_Freeze (Full_Der);
6218 Set_Is_Frozen (Full_Der, False);
6219 Set_Freeze_Node (Full_Der, Empty);
6220 Set_Depends_On_Private (Full_Der,
6221 Has_Private_Component (Full_Der));
6222 Set_Public_Status (Full_Der);
6223 end if;
6224 end if;
6226 Set_Has_Unknown_Discriminants (Derived_Type,
6227 Has_Unknown_Discriminants (Parent_Type));
6229 if Is_Private_Type (Derived_Type) then
6230 Set_Private_Dependents (Derived_Type, New_Elmt_List);
6231 end if;
6233 if Is_Private_Type (Parent_Type)
6234 and then Base_Type (Parent_Type) = Parent_Type
6235 and then In_Open_Scopes (Scope (Parent_Type))
6236 then
6237 Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
6239 if Is_Child_Unit (Scope (Current_Scope))
6240 and then Is_Completion
6241 and then In_Private_Part (Current_Scope)
6242 and then Scope (Parent_Type) /= Current_Scope
6243 then
6244 -- This is the unusual case where a type completed by a private
6245 -- derivation occurs within a package nested in a child unit, and
6246 -- the parent is declared in an ancestor. In this case, the full
6247 -- view of the parent type will become visible in the body of
6248 -- the enclosing child, and only then will the current type be
6249 -- possibly non-private. We build a underlying full view that
6250 -- will be installed when the enclosing child body is compiled.
6252 Full_Der :=
6253 Make_Defining_Identifier
6254 (Sloc (Derived_Type), Chars (Derived_Type));
6255 Set_Is_Itype (Full_Der);
6256 Build_Itype_Reference (Full_Der, N);
6258 -- The full view will be used to swap entities on entry/exit to
6259 -- the body, and must appear in the entity list for the package.
6261 Append_Entity (Full_Der, Scope (Derived_Type));
6262 Set_Has_Private_Declaration (Full_Der);
6263 Set_Has_Private_Declaration (Derived_Type);
6264 Set_Associated_Node_For_Itype (Full_Der, N);
6265 Set_Parent (Full_Der, Parent (Derived_Type));
6266 Full_P := Full_View (Parent_Type);
6267 Exchange_Declarations (Parent_Type);
6268 Copy_And_Build;
6269 Exchange_Declarations (Full_P);
6270 Set_Underlying_Full_View (Derived_Type, Full_Der);
6271 end if;
6272 end if;
6273 end Build_Derived_Private_Type;
6275 -------------------------------
6276 -- Build_Derived_Record_Type --
6277 -------------------------------
6279 -- 1. INTRODUCTION
6281 -- Ideally we would like to use the same model of type derivation for
6282 -- tagged and untagged record types. Unfortunately this is not quite
6283 -- possible because the semantics of representation clauses is different
6284 -- for tagged and untagged records under inheritance. Consider the
6285 -- following:
6287 -- type R (...) is [tagged] record ... end record;
6288 -- type T (...) is new R (...) [with ...];
6290 -- The representation clauses for T can specify a completely different
6291 -- record layout from R's. Hence the same component can be placed in two
6292 -- very different positions in objects of type T and R. If R and T are
6293 -- tagged types, representation clauses for T can only specify the layout
6294 -- of non inherited components, thus components that are common in R and T
6295 -- have the same position in objects of type R and T.
6297 -- This has two implications. The first is that the entire tree for R's
6298 -- declaration needs to be copied for T in the untagged case, so that T
6299 -- can be viewed as a record type of its own with its own representation
6300 -- clauses. The second implication is the way we handle discriminants.
6301 -- Specifically, in the untagged case we need a way to communicate to Gigi
6302 -- what are the real discriminants in the record, while for the semantics
6303 -- we need to consider those introduced by the user to rename the
6304 -- discriminants in the parent type. This is handled by introducing the
6305 -- notion of stored discriminants. See below for more.
6307 -- Fortunately the way regular components are inherited can be handled in
6308 -- the same way in tagged and untagged types.
6310 -- To complicate things a bit more the private view of a private extension
6311 -- cannot be handled in the same way as the full view (for one thing the
6312 -- semantic rules are somewhat different). We will explain what differs
6313 -- below.
6315 -- 2. DISCRIMINANTS UNDER INHERITANCE
6317 -- The semantic rules governing the discriminants of derived types are
6318 -- quite subtle.
6320 -- type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
6321 -- [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
6323 -- If parent type has discriminants, then the discriminants that are
6324 -- declared in the derived type are [3.4 (11)]:
6326 -- o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
6327 -- there is one;
6329 -- o Otherwise, each discriminant of the parent type (implicitly declared
6330 -- in the same order with the same specifications). In this case, the
6331 -- discriminants are said to be "inherited", or if unknown in the parent
6332 -- are also unknown in the derived type.
6334 -- Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
6336 -- o The parent subtype shall be constrained;
6338 -- o If the parent type is not a tagged type, then each discriminant of
6339 -- the derived type shall be used in the constraint defining a parent
6340 -- subtype. [Implementation note: This ensures that the new discriminant
6341 -- can share storage with an existing discriminant.]
6343 -- For the derived type each discriminant of the parent type is either
6344 -- inherited, constrained to equal some new discriminant of the derived
6345 -- type, or constrained to the value of an expression.
6347 -- When inherited or constrained to equal some new discriminant, the
6348 -- parent discriminant and the discriminant of the derived type are said
6349 -- to "correspond".
6351 -- If a discriminant of the parent type is constrained to a specific value
6352 -- in the derived type definition, then the discriminant is said to be
6353 -- "specified" by that derived type definition.
6355 -- 3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
6357 -- We have spoken about stored discriminants in point 1 (introduction)
6358 -- above. There are two sort of stored discriminants: implicit and
6359 -- explicit. As long as the derived type inherits the same discriminants as
6360 -- the root record type, stored discriminants are the same as regular
6361 -- discriminants, and are said to be implicit. However, if any discriminant
6362 -- in the root type was renamed in the derived type, then the derived
6363 -- type will contain explicit stored discriminants. Explicit stored
6364 -- discriminants are discriminants in addition to the semantically visible
6365 -- discriminants defined for the derived type. Stored discriminants are
6366 -- used by Gigi to figure out what are the physical discriminants in
6367 -- objects of the derived type (see precise definition in einfo.ads).
6368 -- As an example, consider the following:
6370 -- type R (D1, D2, D3 : Int) is record ... end record;
6371 -- type T1 is new R;
6372 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
6373 -- type T3 is new T2;
6374 -- type T4 (Y : Int) is new T3 (Y, 99);
6376 -- The following table summarizes the discriminants and stored
6377 -- discriminants in R and T1 through T4.
6379 -- Type Discrim Stored Discrim Comment
6380 -- R (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in R
6381 -- T1 (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in T1
6382 -- T2 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T2
6383 -- T3 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T3
6384 -- T4 (Y) (D1, D2, D3) Girder discrims EXPLICIT in T4
6386 -- Field Corresponding_Discriminant (abbreviated CD below) allows us to
6387 -- find the corresponding discriminant in the parent type, while
6388 -- Original_Record_Component (abbreviated ORC below), the actual physical
6389 -- component that is renamed. Finally the field Is_Completely_Hidden
6390 -- (abbreviated ICH below) is set for all explicit stored discriminants
6391 -- (see einfo.ads for more info). For the above example this gives:
6393 -- Discrim CD ORC ICH
6394 -- ^^^^^^^ ^^ ^^^ ^^^
6395 -- D1 in R empty itself no
6396 -- D2 in R empty itself no
6397 -- D3 in R empty itself no
6399 -- D1 in T1 D1 in R itself no
6400 -- D2 in T1 D2 in R itself no
6401 -- D3 in T1 D3 in R itself no
6403 -- X1 in T2 D3 in T1 D3 in T2 no
6404 -- X2 in T2 D1 in T1 D1 in T2 no
6405 -- D1 in T2 empty itself yes
6406 -- D2 in T2 empty itself yes
6407 -- D3 in T2 empty itself yes
6409 -- X1 in T3 X1 in T2 D3 in T3 no
6410 -- X2 in T3 X2 in T2 D1 in T3 no
6411 -- D1 in T3 empty itself yes
6412 -- D2 in T3 empty itself yes
6413 -- D3 in T3 empty itself yes
6415 -- Y in T4 X1 in T3 D3 in T3 no
6416 -- D1 in T3 empty itself yes
6417 -- D2 in T3 empty itself yes
6418 -- D3 in T3 empty itself yes
6420 -- 4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
6422 -- Type derivation for tagged types is fairly straightforward. If no
6423 -- discriminants are specified by the derived type, these are inherited
6424 -- from the parent. No explicit stored discriminants are ever necessary.
6425 -- The only manipulation that is done to the tree is that of adding a
6426 -- _parent field with parent type and constrained to the same constraint
6427 -- specified for the parent in the derived type definition. For instance:
6429 -- type R (D1, D2, D3 : Int) is tagged record ... end record;
6430 -- type T1 is new R with null record;
6431 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
6433 -- are changed into:
6435 -- type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
6436 -- _parent : R (D1, D2, D3);
6437 -- end record;
6439 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
6440 -- _parent : T1 (X2, 88, X1);
6441 -- end record;
6443 -- The discriminants actually present in R, T1 and T2 as well as their CD,
6444 -- ORC and ICH fields are:
6446 -- Discrim CD ORC ICH
6447 -- ^^^^^^^ ^^ ^^^ ^^^
6448 -- D1 in R empty itself no
6449 -- D2 in R empty itself no
6450 -- D3 in R empty itself no
6452 -- D1 in T1 D1 in R D1 in R no
6453 -- D2 in T1 D2 in R D2 in R no
6454 -- D3 in T1 D3 in R D3 in R no
6456 -- X1 in T2 D3 in T1 D3 in R no
6457 -- X2 in T2 D1 in T1 D1 in R no
6459 -- 5. FIRST TRANSFORMATION FOR DERIVED RECORDS
6461 -- Regardless of whether we dealing with a tagged or untagged type
6462 -- we will transform all derived type declarations of the form
6464 -- type T is new R (...) [with ...];
6465 -- or
6466 -- subtype S is R (...);
6467 -- type T is new S [with ...];
6468 -- into
6469 -- type BT is new R [with ...];
6470 -- subtype T is BT (...);
6472 -- That is, the base derived type is constrained only if it has no
6473 -- discriminants. The reason for doing this is that GNAT's semantic model
6474 -- assumes that a base type with discriminants is unconstrained.
6476 -- Note that, strictly speaking, the above transformation is not always
6477 -- correct. Consider for instance the following excerpt from ACVC b34011a:
6479 -- procedure B34011A is
6480 -- type REC (D : integer := 0) is record
6481 -- I : Integer;
6482 -- end record;
6484 -- package P is
6485 -- type T6 is new Rec;
6486 -- function F return T6;
6487 -- end P;
6489 -- use P;
6490 -- package Q6 is
6491 -- type U is new T6 (Q6.F.I); -- ERROR: Q6.F.
6492 -- end Q6;
6494 -- The definition of Q6.U is illegal. However transforming Q6.U into
6496 -- type BaseU is new T6;
6497 -- subtype U is BaseU (Q6.F.I)
6499 -- turns U into a legal subtype, which is incorrect. To avoid this problem
6500 -- we always analyze the constraint (in this case (Q6.F.I)) before applying
6501 -- the transformation described above.
6503 -- There is another instance where the above transformation is incorrect.
6504 -- Consider:
6506 -- package Pack is
6507 -- type Base (D : Integer) is tagged null record;
6508 -- procedure P (X : Base);
6510 -- type Der is new Base (2) with null record;
6511 -- procedure P (X : Der);
6512 -- end Pack;
6514 -- Then the above transformation turns this into
6516 -- type Der_Base is new Base with null record;
6517 -- -- procedure P (X : Base) is implicitly inherited here
6518 -- -- as procedure P (X : Der_Base).
6520 -- subtype Der is Der_Base (2);
6521 -- procedure P (X : Der);
6522 -- -- The overriding of P (X : Der_Base) is illegal since we
6523 -- -- have a parameter conformance problem.
6525 -- To get around this problem, after having semantically processed Der_Base
6526 -- and the rewritten subtype declaration for Der, we copy Der_Base field
6527 -- Discriminant_Constraint from Der so that when parameter conformance is
6528 -- checked when P is overridden, no semantic errors are flagged.
6530 -- 6. SECOND TRANSFORMATION FOR DERIVED RECORDS
6532 -- Regardless of whether we are dealing with a tagged or untagged type
6533 -- we will transform all derived type declarations of the form
6535 -- type R (D1, .., Dn : ...) is [tagged] record ...;
6536 -- type T is new R [with ...];
6537 -- into
6538 -- type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
6540 -- The reason for such transformation is that it allows us to implement a
6541 -- very clean form of component inheritance as explained below.
6543 -- Note that this transformation is not achieved by direct tree rewriting
6544 -- and manipulation, but rather by redoing the semantic actions that the
6545 -- above transformation will entail. This is done directly in routine
6546 -- Inherit_Components.
6548 -- 7. TYPE DERIVATION AND COMPONENT INHERITANCE
6550 -- In both tagged and untagged derived types, regular non discriminant
6551 -- components are inherited in the derived type from the parent type. In
6552 -- the absence of discriminants component, inheritance is straightforward
6553 -- as components can simply be copied from the parent.
6555 -- If the parent has discriminants, inheriting components constrained with
6556 -- these discriminants requires caution. Consider the following example:
6558 -- type R (D1, D2 : Positive) is [tagged] record
6559 -- S : String (D1 .. D2);
6560 -- end record;
6562 -- type T1 is new R [with null record];
6563 -- type T2 (X : positive) is new R (1, X) [with null record];
6565 -- As explained in 6. above, T1 is rewritten as
6566 -- type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
6567 -- which makes the treatment for T1 and T2 identical.
6569 -- What we want when inheriting S, is that references to D1 and D2 in R are
6570 -- replaced with references to their correct constraints, i.e. D1 and D2 in
6571 -- T1 and 1 and X in T2. So all R's discriminant references are replaced
6572 -- with either discriminant references in the derived type or expressions.
6573 -- This replacement is achieved as follows: before inheriting R's
6574 -- components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
6575 -- created in the scope of T1 (resp. scope of T2) so that discriminants D1
6576 -- and D2 of T1 are visible (resp. discriminant X of T2 is visible).
6577 -- For T2, for instance, this has the effect of replacing String (D1 .. D2)
6578 -- by String (1 .. X).
6580 -- 8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
6582 -- We explain here the rules governing private type extensions relevant to
6583 -- type derivation. These rules are explained on the following example:
6585 -- type D [(...)] is new A [(...)] with private; <-- partial view
6586 -- type D [(...)] is new P [(...)] with null record; <-- full view
6588 -- Type A is called the ancestor subtype of the private extension.
6589 -- Type P is the parent type of the full view of the private extension. It
6590 -- must be A or a type derived from A.
6592 -- The rules concerning the discriminants of private type extensions are
6593 -- [7.3(10-13)]:
6595 -- o If a private extension inherits known discriminants from the ancestor
6596 -- subtype, then the full view shall also inherit its discriminants from
6597 -- the ancestor subtype and the parent subtype of the full view shall be
6598 -- constrained if and only if the ancestor subtype is constrained.
6600 -- o If a partial view has unknown discriminants, then the full view may
6601 -- define a definite or an indefinite subtype, with or without
6602 -- discriminants.
6604 -- o If a partial view has neither known nor unknown discriminants, then
6605 -- the full view shall define a definite subtype.
6607 -- o If the ancestor subtype of a private extension has constrained
6608 -- discriminants, then the parent subtype of the full view shall impose a
6609 -- statically matching constraint on those discriminants.
6611 -- This means that only the following forms of private extensions are
6612 -- allowed:
6614 -- type D is new A with private; <-- partial view
6615 -- type D is new P with null record; <-- full view
6617 -- If A has no discriminants than P has no discriminants, otherwise P must
6618 -- inherit A's discriminants.
6620 -- type D is new A (...) with private; <-- partial view
6621 -- type D is new P (:::) with null record; <-- full view
6623 -- P must inherit A's discriminants and (...) and (:::) must statically
6624 -- match.
6626 -- subtype A is R (...);
6627 -- type D is new A with private; <-- partial view
6628 -- type D is new P with null record; <-- full view
6630 -- P must have inherited R's discriminants and must be derived from A or
6631 -- any of its subtypes.
6633 -- type D (..) is new A with private; <-- partial view
6634 -- type D (..) is new P [(:::)] with null record; <-- full view
6636 -- No specific constraints on P's discriminants or constraint (:::).
6637 -- Note that A can be unconstrained, but the parent subtype P must either
6638 -- be constrained or (:::) must be present.
6640 -- type D (..) is new A [(...)] with private; <-- partial view
6641 -- type D (..) is new P [(:::)] with null record; <-- full view
6643 -- P's constraints on A's discriminants must statically match those
6644 -- imposed by (...).
6646 -- 9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
6648 -- The full view of a private extension is handled exactly as described
6649 -- above. The model chose for the private view of a private extension is
6650 -- the same for what concerns discriminants (i.e. they receive the same
6651 -- treatment as in the tagged case). However, the private view of the
6652 -- private extension always inherits the components of the parent base,
6653 -- without replacing any discriminant reference. Strictly speaking this is
6654 -- incorrect. However, Gigi never uses this view to generate code so this
6655 -- is a purely semantic issue. In theory, a set of transformations similar
6656 -- to those given in 5. and 6. above could be applied to private views of
6657 -- private extensions to have the same model of component inheritance as
6658 -- for non private extensions. However, this is not done because it would
6659 -- further complicate private type processing. Semantically speaking, this
6660 -- leaves us in an uncomfortable situation. As an example consider:
6662 -- package Pack is
6663 -- type R (D : integer) is tagged record
6664 -- S : String (1 .. D);
6665 -- end record;
6666 -- procedure P (X : R);
6667 -- type T is new R (1) with private;
6668 -- private
6669 -- type T is new R (1) with null record;
6670 -- end;
6672 -- This is transformed into:
6674 -- package Pack is
6675 -- type R (D : integer) is tagged record
6676 -- S : String (1 .. D);
6677 -- end record;
6678 -- procedure P (X : R);
6679 -- type T is new R (1) with private;
6680 -- private
6681 -- type BaseT is new R with null record;
6682 -- subtype T is BaseT (1);
6683 -- end;
6685 -- (strictly speaking the above is incorrect Ada)
6687 -- From the semantic standpoint the private view of private extension T
6688 -- should be flagged as constrained since one can clearly have
6690 -- Obj : T;
6692 -- in a unit withing Pack. However, when deriving subprograms for the
6693 -- private view of private extension T, T must be seen as unconstrained
6694 -- since T has discriminants (this is a constraint of the current
6695 -- subprogram derivation model). Thus, when processing the private view of
6696 -- a private extension such as T, we first mark T as unconstrained, we
6697 -- process it, we perform program derivation and just before returning from
6698 -- Build_Derived_Record_Type we mark T as constrained.
6700 -- ??? Are there are other uncomfortable cases that we will have to
6701 -- deal with.
6703 -- 10. RECORD_TYPE_WITH_PRIVATE complications
6705 -- Types that are derived from a visible record type and have a private
6706 -- extension present other peculiarities. They behave mostly like private
6707 -- types, but if they have primitive operations defined, these will not
6708 -- have the proper signatures for further inheritance, because other
6709 -- primitive operations will use the implicit base that we define for
6710 -- private derivations below. This affect subprogram inheritance (see
6711 -- Derive_Subprograms for details). We also derive the implicit base from
6712 -- the base type of the full view, so that the implicit base is a record
6713 -- type and not another private type, This avoids infinite loops.
6715 procedure Build_Derived_Record_Type
6716 (N : Node_Id;
6717 Parent_Type : Entity_Id;
6718 Derived_Type : Entity_Id;
6719 Derive_Subps : Boolean := True)
6721 Loc : constant Source_Ptr := Sloc (N);
6722 Parent_Base : Entity_Id;
6723 Type_Def : Node_Id;
6724 Indic : Node_Id;
6725 Discrim : Entity_Id;
6726 Last_Discrim : Entity_Id;
6727 Constrs : Elist_Id;
6729 Discs : Elist_Id := New_Elmt_List;
6730 -- An empty Discs list means that there were no constraints in the
6731 -- subtype indication or that there was an error processing it.
6733 Assoc_List : Elist_Id;
6734 New_Discrs : Elist_Id;
6735 New_Base : Entity_Id;
6736 New_Decl : Node_Id;
6737 New_Indic : Node_Id;
6739 Is_Tagged : constant Boolean := Is_Tagged_Type (Parent_Type);
6740 Discriminant_Specs : constant Boolean :=
6741 Present (Discriminant_Specifications (N));
6742 Private_Extension : constant Boolean :=
6743 Nkind (N) = N_Private_Extension_Declaration;
6745 Constraint_Present : Boolean;
6746 Inherit_Discrims : Boolean := False;
6747 Save_Etype : Entity_Id;
6748 Save_Discr_Constr : Elist_Id;
6749 Save_Next_Entity : Entity_Id;
6751 begin
6752 if Ekind (Parent_Type) = E_Record_Type_With_Private
6753 and then Present (Full_View (Parent_Type))
6754 and then Has_Discriminants (Parent_Type)
6755 then
6756 Parent_Base := Base_Type (Full_View (Parent_Type));
6757 else
6758 Parent_Base := Base_Type (Parent_Type);
6759 end if;
6761 -- Before we start the previously documented transformations, here is
6762 -- little fix for size and alignment of tagged types. Normally when we
6763 -- derive type D from type P, we copy the size and alignment of P as the
6764 -- default for D, and in the absence of explicit representation clauses
6765 -- for D, the size and alignment are indeed the same as the parent.
6767 -- But this is wrong for tagged types, since fields may be added, and
6768 -- the default size may need to be larger, and the default alignment may
6769 -- need to be larger.
6771 -- We therefore reset the size and alignment fields in the tagged case.
6772 -- Note that the size and alignment will in any case be at least as
6773 -- large as the parent type (since the derived type has a copy of the
6774 -- parent type in the _parent field)
6776 -- The type is also marked as being tagged here, which is needed when
6777 -- processing components with a self-referential anonymous access type
6778 -- in the call to Check_Anonymous_Access_Components below. Note that
6779 -- this flag is also set later on for completeness.
6781 if Is_Tagged then
6782 Set_Is_Tagged_Type (Derived_Type);
6783 Init_Size_Align (Derived_Type);
6784 end if;
6786 -- STEP 0a: figure out what kind of derived type declaration we have
6788 if Private_Extension then
6789 Type_Def := N;
6790 Set_Ekind (Derived_Type, E_Record_Type_With_Private);
6792 else
6793 Type_Def := Type_Definition (N);
6795 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
6796 -- Parent_Base can be a private type or private extension. However,
6797 -- for tagged types with an extension the newly added fields are
6798 -- visible and hence the Derived_Type is always an E_Record_Type.
6799 -- (except that the parent may have its own private fields).
6800 -- For untagged types we preserve the Ekind of the Parent_Base.
6802 if Present (Record_Extension_Part (Type_Def)) then
6803 Set_Ekind (Derived_Type, E_Record_Type);
6805 -- Create internal access types for components with anonymous
6806 -- access types.
6808 if Ada_Version >= Ada_2005 then
6809 Check_Anonymous_Access_Components
6810 (N, Derived_Type, Derived_Type,
6811 Component_List (Record_Extension_Part (Type_Def)));
6812 end if;
6814 else
6815 Set_Ekind (Derived_Type, Ekind (Parent_Base));
6816 end if;
6817 end if;
6819 -- Indic can either be an N_Identifier if the subtype indication
6820 -- contains no constraint or an N_Subtype_Indication if the subtype
6821 -- indication has a constraint.
6823 Indic := Subtype_Indication (Type_Def);
6824 Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
6826 -- Check that the type has visible discriminants. The type may be
6827 -- a private type with unknown discriminants whose full view has
6828 -- discriminants which are invisible.
6830 if Constraint_Present then
6831 if not Has_Discriminants (Parent_Base)
6832 or else
6833 (Has_Unknown_Discriminants (Parent_Base)
6834 and then Is_Private_Type (Parent_Base))
6835 then
6836 Error_Msg_N
6837 ("invalid constraint: type has no discriminant",
6838 Constraint (Indic));
6840 Constraint_Present := False;
6841 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
6843 elsif Is_Constrained (Parent_Type) then
6844 Error_Msg_N
6845 ("invalid constraint: parent type is already constrained",
6846 Constraint (Indic));
6848 Constraint_Present := False;
6849 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
6850 end if;
6851 end if;
6853 -- STEP 0b: If needed, apply transformation given in point 5. above
6855 if not Private_Extension
6856 and then Has_Discriminants (Parent_Type)
6857 and then not Discriminant_Specs
6858 and then (Is_Constrained (Parent_Type) or else Constraint_Present)
6859 then
6860 -- First, we must analyze the constraint (see comment in point 5.)
6862 if Constraint_Present then
6863 New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
6865 if Has_Discriminants (Derived_Type)
6866 and then Has_Private_Declaration (Derived_Type)
6867 and then Present (Discriminant_Constraint (Derived_Type))
6868 then
6869 -- Verify that constraints of the full view statically match
6870 -- those given in the partial view.
6872 declare
6873 C1, C2 : Elmt_Id;
6875 begin
6876 C1 := First_Elmt (New_Discrs);
6877 C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
6878 while Present (C1) and then Present (C2) loop
6879 if Fully_Conformant_Expressions (Node (C1), Node (C2))
6880 or else
6881 (Is_OK_Static_Expression (Node (C1))
6882 and then
6883 Is_OK_Static_Expression (Node (C2))
6884 and then
6885 Expr_Value (Node (C1)) = Expr_Value (Node (C2)))
6886 then
6887 null;
6889 else
6890 Error_Msg_N (
6891 "constraint not conformant to previous declaration",
6892 Node (C1));
6893 end if;
6895 Next_Elmt (C1);
6896 Next_Elmt (C2);
6897 end loop;
6898 end;
6899 end if;
6900 end if;
6902 -- Insert and analyze the declaration for the unconstrained base type
6904 New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
6906 New_Decl :=
6907 Make_Full_Type_Declaration (Loc,
6908 Defining_Identifier => New_Base,
6909 Type_Definition =>
6910 Make_Derived_Type_Definition (Loc,
6911 Abstract_Present => Abstract_Present (Type_Def),
6912 Limited_Present => Limited_Present (Type_Def),
6913 Subtype_Indication =>
6914 New_Occurrence_Of (Parent_Base, Loc),
6915 Record_Extension_Part =>
6916 Relocate_Node (Record_Extension_Part (Type_Def)),
6917 Interface_List => Interface_List (Type_Def)));
6919 Set_Parent (New_Decl, Parent (N));
6920 Mark_Rewrite_Insertion (New_Decl);
6921 Insert_Before (N, New_Decl);
6923 -- In the extension case, make sure ancestor is frozen appropriately
6924 -- (see also non-discriminated case below).
6926 if Present (Record_Extension_Part (Type_Def))
6927 or else Is_Interface (Parent_Base)
6928 then
6929 Freeze_Before (New_Decl, Parent_Type);
6930 end if;
6932 -- Note that this call passes False for the Derive_Subps parameter
6933 -- because subprogram derivation is deferred until after creating
6934 -- the subtype (see below).
6936 Build_Derived_Type
6937 (New_Decl, Parent_Base, New_Base,
6938 Is_Completion => True, Derive_Subps => False);
6940 -- ??? This needs re-examination to determine whether the
6941 -- above call can simply be replaced by a call to Analyze.
6943 Set_Analyzed (New_Decl);
6945 -- Insert and analyze the declaration for the constrained subtype
6947 if Constraint_Present then
6948 New_Indic :=
6949 Make_Subtype_Indication (Loc,
6950 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
6951 Constraint => Relocate_Node (Constraint (Indic)));
6953 else
6954 declare
6955 Constr_List : constant List_Id := New_List;
6956 C : Elmt_Id;
6957 Expr : Node_Id;
6959 begin
6960 C := First_Elmt (Discriminant_Constraint (Parent_Type));
6961 while Present (C) loop
6962 Expr := Node (C);
6964 -- It is safe here to call New_Copy_Tree since
6965 -- Force_Evaluation was called on each constraint in
6966 -- Build_Discriminant_Constraints.
6968 Append (New_Copy_Tree (Expr), To => Constr_List);
6970 Next_Elmt (C);
6971 end loop;
6973 New_Indic :=
6974 Make_Subtype_Indication (Loc,
6975 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
6976 Constraint =>
6977 Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
6978 end;
6979 end if;
6981 Rewrite (N,
6982 Make_Subtype_Declaration (Loc,
6983 Defining_Identifier => Derived_Type,
6984 Subtype_Indication => New_Indic));
6986 Analyze (N);
6988 -- Derivation of subprograms must be delayed until the full subtype
6989 -- has been established to ensure proper overriding of subprograms
6990 -- inherited by full types. If the derivations occurred as part of
6991 -- the call to Build_Derived_Type above, then the check for type
6992 -- conformance would fail because earlier primitive subprograms
6993 -- could still refer to the full type prior the change to the new
6994 -- subtype and hence would not match the new base type created here.
6996 Derive_Subprograms (Parent_Type, Derived_Type);
6998 -- For tagged types the Discriminant_Constraint of the new base itype
6999 -- is inherited from the first subtype so that no subtype conformance
7000 -- problem arise when the first subtype overrides primitive
7001 -- operations inherited by the implicit base type.
7003 if Is_Tagged then
7004 Set_Discriminant_Constraint
7005 (New_Base, Discriminant_Constraint (Derived_Type));
7006 end if;
7008 return;
7009 end if;
7011 -- If we get here Derived_Type will have no discriminants or it will be
7012 -- a discriminated unconstrained base type.
7014 -- STEP 1a: perform preliminary actions/checks for derived tagged types
7016 if Is_Tagged then
7018 -- The parent type is frozen for non-private extensions (RM 13.14(7))
7019 -- The declaration of a specific descendant of an interface type
7020 -- freezes the interface type (RM 13.14).
7022 if not Private_Extension or else Is_Interface (Parent_Base) then
7023 Freeze_Before (N, Parent_Type);
7024 end if;
7026 -- In Ada 2005 (AI-344), the restriction that a derived tagged type
7027 -- cannot be declared at a deeper level than its parent type is
7028 -- removed. The check on derivation within a generic body is also
7029 -- relaxed, but there's a restriction that a derived tagged type
7030 -- cannot be declared in a generic body if it's derived directly
7031 -- or indirectly from a formal type of that generic.
7033 if Ada_Version >= Ada_2005 then
7034 if Present (Enclosing_Generic_Body (Derived_Type)) then
7035 declare
7036 Ancestor_Type : Entity_Id;
7038 begin
7039 -- Check to see if any ancestor of the derived type is a
7040 -- formal type.
7042 Ancestor_Type := Parent_Type;
7043 while not Is_Generic_Type (Ancestor_Type)
7044 and then Etype (Ancestor_Type) /= Ancestor_Type
7045 loop
7046 Ancestor_Type := Etype (Ancestor_Type);
7047 end loop;
7049 -- If the derived type does have a formal type as an
7050 -- ancestor, then it's an error if the derived type is
7051 -- declared within the body of the generic unit that
7052 -- declares the formal type in its generic formal part. It's
7053 -- sufficient to check whether the ancestor type is declared
7054 -- inside the same generic body as the derived type (such as
7055 -- within a nested generic spec), in which case the
7056 -- derivation is legal. If the formal type is declared
7057 -- outside of that generic body, then it's guaranteed that
7058 -- the derived type is declared within the generic body of
7059 -- the generic unit declaring the formal type.
7061 if Is_Generic_Type (Ancestor_Type)
7062 and then Enclosing_Generic_Body (Ancestor_Type) /=
7063 Enclosing_Generic_Body (Derived_Type)
7064 then
7065 Error_Msg_NE
7066 ("parent type of& must not be descendant of formal type"
7067 & " of an enclosing generic body",
7068 Indic, Derived_Type);
7069 end if;
7070 end;
7071 end if;
7073 elsif Type_Access_Level (Derived_Type) /=
7074 Type_Access_Level (Parent_Type)
7075 and then not Is_Generic_Type (Derived_Type)
7076 then
7077 if Is_Controlled (Parent_Type) then
7078 Error_Msg_N
7079 ("controlled type must be declared at the library level",
7080 Indic);
7081 else
7082 Error_Msg_N
7083 ("type extension at deeper accessibility level than parent",
7084 Indic);
7085 end if;
7087 else
7088 declare
7089 GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
7091 begin
7092 if Present (GB)
7093 and then GB /= Enclosing_Generic_Body (Parent_Base)
7094 then
7095 Error_Msg_NE
7096 ("parent type of& must not be outside generic body"
7097 & " (RM 3.9.1(4))",
7098 Indic, Derived_Type);
7099 end if;
7100 end;
7101 end if;
7102 end if;
7104 -- Ada 2005 (AI-251)
7106 if Ada_Version >= Ada_2005 and then Is_Tagged then
7108 -- "The declaration of a specific descendant of an interface type
7109 -- freezes the interface type" (RM 13.14).
7111 declare
7112 Iface : Node_Id;
7113 begin
7114 if Is_Non_Empty_List (Interface_List (Type_Def)) then
7115 Iface := First (Interface_List (Type_Def));
7116 while Present (Iface) loop
7117 Freeze_Before (N, Etype (Iface));
7118 Next (Iface);
7119 end loop;
7120 end if;
7121 end;
7122 end if;
7124 -- STEP 1b : preliminary cleanup of the full view of private types
7126 -- If the type is already marked as having discriminants, then it's the
7127 -- completion of a private type or private extension and we need to
7128 -- retain the discriminants from the partial view if the current
7129 -- declaration has Discriminant_Specifications so that we can verify
7130 -- conformance. However, we must remove any existing components that
7131 -- were inherited from the parent (and attached in Copy_And_Swap)
7132 -- because the full type inherits all appropriate components anyway, and
7133 -- we do not want the partial view's components interfering.
7135 if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
7136 Discrim := First_Discriminant (Derived_Type);
7137 loop
7138 Last_Discrim := Discrim;
7139 Next_Discriminant (Discrim);
7140 exit when No (Discrim);
7141 end loop;
7143 Set_Last_Entity (Derived_Type, Last_Discrim);
7145 -- In all other cases wipe out the list of inherited components (even
7146 -- inherited discriminants), it will be properly rebuilt here.
7148 else
7149 Set_First_Entity (Derived_Type, Empty);
7150 Set_Last_Entity (Derived_Type, Empty);
7151 end if;
7153 -- STEP 1c: Initialize some flags for the Derived_Type
7155 -- The following flags must be initialized here so that
7156 -- Process_Discriminants can check that discriminants of tagged types do
7157 -- not have a default initial value and that access discriminants are
7158 -- only specified for limited records. For completeness, these flags are
7159 -- also initialized along with all the other flags below.
7161 -- AI-419: Limitedness is not inherited from an interface parent, so to
7162 -- be limited in that case the type must be explicitly declared as
7163 -- limited. However, task and protected interfaces are always limited.
7165 if Limited_Present (Type_Def) then
7166 Set_Is_Limited_Record (Derived_Type);
7168 elsif Is_Limited_Record (Parent_Type)
7169 or else (Present (Full_View (Parent_Type))
7170 and then Is_Limited_Record (Full_View (Parent_Type)))
7171 then
7172 if not Is_Interface (Parent_Type)
7173 or else Is_Synchronized_Interface (Parent_Type)
7174 or else Is_Protected_Interface (Parent_Type)
7175 or else Is_Task_Interface (Parent_Type)
7176 then
7177 Set_Is_Limited_Record (Derived_Type);
7178 end if;
7179 end if;
7181 -- STEP 2a: process discriminants of derived type if any
7183 Push_Scope (Derived_Type);
7185 if Discriminant_Specs then
7186 Set_Has_Unknown_Discriminants (Derived_Type, False);
7188 -- The following call initializes fields Has_Discriminants and
7189 -- Discriminant_Constraint, unless we are processing the completion
7190 -- of a private type declaration.
7192 Check_Or_Process_Discriminants (N, Derived_Type);
7194 -- For untagged types, the constraint on the Parent_Type must be
7195 -- present and is used to rename the discriminants.
7197 if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
7198 Error_Msg_N ("untagged parent must have discriminants", Indic);
7200 elsif not Is_Tagged and then not Constraint_Present then
7201 Error_Msg_N
7202 ("discriminant constraint needed for derived untagged records",
7203 Indic);
7205 -- Otherwise the parent subtype must be constrained unless we have a
7206 -- private extension.
7208 elsif not Constraint_Present
7209 and then not Private_Extension
7210 and then not Is_Constrained (Parent_Type)
7211 then
7212 Error_Msg_N
7213 ("unconstrained type not allowed in this context", Indic);
7215 elsif Constraint_Present then
7216 -- The following call sets the field Corresponding_Discriminant
7217 -- for the discriminants in the Derived_Type.
7219 Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
7221 -- For untagged types all new discriminants must rename
7222 -- discriminants in the parent. For private extensions new
7223 -- discriminants cannot rename old ones (implied by [7.3(13)]).
7225 Discrim := First_Discriminant (Derived_Type);
7226 while Present (Discrim) loop
7227 if not Is_Tagged
7228 and then No (Corresponding_Discriminant (Discrim))
7229 then
7230 Error_Msg_N
7231 ("new discriminants must constrain old ones", Discrim);
7233 elsif Private_Extension
7234 and then Present (Corresponding_Discriminant (Discrim))
7235 then
7236 Error_Msg_N
7237 ("only static constraints allowed for parent"
7238 & " discriminants in the partial view", Indic);
7239 exit;
7240 end if;
7242 -- If a new discriminant is used in the constraint, then its
7243 -- subtype must be statically compatible with the parent
7244 -- discriminant's subtype (3.7(15)).
7246 if Present (Corresponding_Discriminant (Discrim))
7247 and then
7248 not Subtypes_Statically_Compatible
7249 (Etype (Discrim),
7250 Etype (Corresponding_Discriminant (Discrim)))
7251 then
7252 Error_Msg_N
7253 ("subtype must be compatible with parent discriminant",
7254 Discrim);
7255 end if;
7257 Next_Discriminant (Discrim);
7258 end loop;
7260 -- Check whether the constraints of the full view statically
7261 -- match those imposed by the parent subtype [7.3(13)].
7263 if Present (Stored_Constraint (Derived_Type)) then
7264 declare
7265 C1, C2 : Elmt_Id;
7267 begin
7268 C1 := First_Elmt (Discs);
7269 C2 := First_Elmt (Stored_Constraint (Derived_Type));
7270 while Present (C1) and then Present (C2) loop
7271 if not
7272 Fully_Conformant_Expressions (Node (C1), Node (C2))
7273 then
7274 Error_Msg_N
7275 ("not conformant with previous declaration",
7276 Node (C1));
7277 end if;
7279 Next_Elmt (C1);
7280 Next_Elmt (C2);
7281 end loop;
7282 end;
7283 end if;
7284 end if;
7286 -- STEP 2b: No new discriminants, inherit discriminants if any
7288 else
7289 if Private_Extension then
7290 Set_Has_Unknown_Discriminants
7291 (Derived_Type,
7292 Has_Unknown_Discriminants (Parent_Type)
7293 or else Unknown_Discriminants_Present (N));
7295 -- The partial view of the parent may have unknown discriminants,
7296 -- but if the full view has discriminants and the parent type is
7297 -- in scope they must be inherited.
7299 elsif Has_Unknown_Discriminants (Parent_Type)
7300 and then
7301 (not Has_Discriminants (Parent_Type)
7302 or else not In_Open_Scopes (Scope (Parent_Type)))
7303 then
7304 Set_Has_Unknown_Discriminants (Derived_Type);
7305 end if;
7307 if not Has_Unknown_Discriminants (Derived_Type)
7308 and then not Has_Unknown_Discriminants (Parent_Base)
7309 and then Has_Discriminants (Parent_Type)
7310 then
7311 Inherit_Discrims := True;
7312 Set_Has_Discriminants
7313 (Derived_Type, True);
7314 Set_Discriminant_Constraint
7315 (Derived_Type, Discriminant_Constraint (Parent_Base));
7316 end if;
7318 -- The following test is true for private types (remember
7319 -- transformation 5. is not applied to those) and in an error
7320 -- situation.
7322 if Constraint_Present then
7323 Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
7324 end if;
7326 -- For now mark a new derived type as constrained only if it has no
7327 -- discriminants. At the end of Build_Derived_Record_Type we properly
7328 -- set this flag in the case of private extensions. See comments in
7329 -- point 9. just before body of Build_Derived_Record_Type.
7331 Set_Is_Constrained
7332 (Derived_Type,
7333 not (Inherit_Discrims
7334 or else Has_Unknown_Discriminants (Derived_Type)));
7335 end if;
7337 -- STEP 3: initialize fields of derived type
7339 Set_Is_Tagged_Type (Derived_Type, Is_Tagged);
7340 Set_Stored_Constraint (Derived_Type, No_Elist);
7342 -- Ada 2005 (AI-251): Private type-declarations can implement interfaces
7343 -- but cannot be interfaces
7345 if not Private_Extension
7346 and then Ekind (Derived_Type) /= E_Private_Type
7347 and then Ekind (Derived_Type) /= E_Limited_Private_Type
7348 then
7349 if Interface_Present (Type_Def) then
7350 Analyze_Interface_Declaration (Derived_Type, Type_Def);
7351 end if;
7353 Set_Interfaces (Derived_Type, No_Elist);
7354 end if;
7356 -- Fields inherited from the Parent_Type
7358 Set_Discard_Names
7359 (Derived_Type, Einfo.Discard_Names (Parent_Type));
7360 Set_Has_Specified_Layout
7361 (Derived_Type, Has_Specified_Layout (Parent_Type));
7362 Set_Is_Limited_Composite
7363 (Derived_Type, Is_Limited_Composite (Parent_Type));
7364 Set_Is_Private_Composite
7365 (Derived_Type, Is_Private_Composite (Parent_Type));
7367 -- Fields inherited from the Parent_Base
7369 Set_Has_Controlled_Component
7370 (Derived_Type, Has_Controlled_Component (Parent_Base));
7371 Set_Has_Non_Standard_Rep
7372 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
7373 Set_Has_Primitive_Operations
7374 (Derived_Type, Has_Primitive_Operations (Parent_Base));
7376 -- Fields inherited from the Parent_Base in the non-private case
7378 if Ekind (Derived_Type) = E_Record_Type then
7379 Set_Has_Complex_Representation
7380 (Derived_Type, Has_Complex_Representation (Parent_Base));
7381 end if;
7383 -- Fields inherited from the Parent_Base for record types
7385 if Is_Record_Type (Derived_Type) then
7387 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
7388 -- Parent_Base can be a private type or private extension.
7390 if Present (Full_View (Parent_Base)) then
7391 Set_OK_To_Reorder_Components
7392 (Derived_Type,
7393 OK_To_Reorder_Components (Full_View (Parent_Base)));
7394 Set_Reverse_Bit_Order
7395 (Derived_Type, Reverse_Bit_Order (Full_View (Parent_Base)));
7396 else
7397 Set_OK_To_Reorder_Components
7398 (Derived_Type, OK_To_Reorder_Components (Parent_Base));
7399 Set_Reverse_Bit_Order
7400 (Derived_Type, Reverse_Bit_Order (Parent_Base));
7401 end if;
7402 end if;
7404 -- Direct controlled types do not inherit Finalize_Storage_Only flag
7406 if not Is_Controlled (Parent_Type) then
7407 Set_Finalize_Storage_Only
7408 (Derived_Type, Finalize_Storage_Only (Parent_Type));
7409 end if;
7411 -- Set fields for private derived types
7413 if Is_Private_Type (Derived_Type) then
7414 Set_Depends_On_Private (Derived_Type, True);
7415 Set_Private_Dependents (Derived_Type, New_Elmt_List);
7417 -- Inherit fields from non private record types. If this is the
7418 -- completion of a derivation from a private type, the parent itself
7419 -- is private, and the attributes come from its full view, which must
7420 -- be present.
7422 else
7423 if Is_Private_Type (Parent_Base)
7424 and then not Is_Record_Type (Parent_Base)
7425 then
7426 Set_Component_Alignment
7427 (Derived_Type, Component_Alignment (Full_View (Parent_Base)));
7428 Set_C_Pass_By_Copy
7429 (Derived_Type, C_Pass_By_Copy (Full_View (Parent_Base)));
7430 else
7431 Set_Component_Alignment
7432 (Derived_Type, Component_Alignment (Parent_Base));
7433 Set_C_Pass_By_Copy
7434 (Derived_Type, C_Pass_By_Copy (Parent_Base));
7435 end if;
7436 end if;
7438 -- Set fields for tagged types
7440 if Is_Tagged then
7441 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
7443 -- All tagged types defined in Ada.Finalization are controlled
7445 if Chars (Scope (Derived_Type)) = Name_Finalization
7446 and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
7447 and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
7448 then
7449 Set_Is_Controlled (Derived_Type);
7450 else
7451 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Base));
7452 end if;
7454 -- Minor optimization: there is no need to generate the class-wide
7455 -- entity associated with an underlying record view.
7457 if not Is_Underlying_Record_View (Derived_Type) then
7458 Make_Class_Wide_Type (Derived_Type);
7459 end if;
7461 Set_Is_Abstract_Type (Derived_Type, Abstract_Present (Type_Def));
7463 if Has_Discriminants (Derived_Type)
7464 and then Constraint_Present
7465 then
7466 Set_Stored_Constraint
7467 (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
7468 end if;
7470 if Ada_Version >= Ada_2005 then
7471 declare
7472 Ifaces_List : Elist_Id;
7474 begin
7475 -- Checks rules 3.9.4 (13/2 and 14/2)
7477 if Comes_From_Source (Derived_Type)
7478 and then not Is_Private_Type (Derived_Type)
7479 and then Is_Interface (Parent_Type)
7480 and then not Is_Interface (Derived_Type)
7481 then
7482 if Is_Task_Interface (Parent_Type) then
7483 Error_Msg_N
7484 ("(Ada 2005) task type required (RM 3.9.4 (13.2))",
7485 Derived_Type);
7487 elsif Is_Protected_Interface (Parent_Type) then
7488 Error_Msg_N
7489 ("(Ada 2005) protected type required (RM 3.9.4 (14.2))",
7490 Derived_Type);
7491 end if;
7492 end if;
7494 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
7496 Check_Interfaces (N, Type_Def);
7498 -- Ada 2005 (AI-251): Collect the list of progenitors that are
7499 -- not already in the parents.
7501 Collect_Interfaces
7502 (T => Derived_Type,
7503 Ifaces_List => Ifaces_List,
7504 Exclude_Parents => True);
7506 Set_Interfaces (Derived_Type, Ifaces_List);
7508 -- If the derived type is the anonymous type created for
7509 -- a declaration whose parent has a constraint, propagate
7510 -- the interface list to the source type. This must be done
7511 -- prior to the completion of the analysis of the source type
7512 -- because the components in the extension may contain current
7513 -- instances whose legality depends on some ancestor.
7515 if Is_Itype (Derived_Type) then
7516 declare
7517 Def : constant Node_Id :=
7518 Associated_Node_For_Itype (Derived_Type);
7519 begin
7520 if Present (Def)
7521 and then Nkind (Def) = N_Full_Type_Declaration
7522 then
7523 Set_Interfaces
7524 (Defining_Identifier (Def), Ifaces_List);
7525 end if;
7526 end;
7527 end if;
7528 end;
7529 end if;
7531 else
7532 Set_Is_Packed (Derived_Type, Is_Packed (Parent_Base));
7533 Set_Has_Non_Standard_Rep
7534 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
7535 end if;
7537 -- STEP 4: Inherit components from the parent base and constrain them.
7538 -- Apply the second transformation described in point 6. above.
7540 if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
7541 or else not Has_Discriminants (Parent_Type)
7542 or else not Is_Constrained (Parent_Type)
7543 then
7544 Constrs := Discs;
7545 else
7546 Constrs := Discriminant_Constraint (Parent_Type);
7547 end if;
7549 Assoc_List :=
7550 Inherit_Components
7551 (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
7553 -- STEP 5a: Copy the parent record declaration for untagged types
7555 if not Is_Tagged then
7557 -- Discriminant_Constraint (Derived_Type) has been properly
7558 -- constructed. Save it and temporarily set it to Empty because we
7559 -- do not want the call to New_Copy_Tree below to mess this list.
7561 if Has_Discriminants (Derived_Type) then
7562 Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
7563 Set_Discriminant_Constraint (Derived_Type, No_Elist);
7564 else
7565 Save_Discr_Constr := No_Elist;
7566 end if;
7568 -- Save the Etype field of Derived_Type. It is correctly set now,
7569 -- but the call to New_Copy tree may remap it to point to itself,
7570 -- which is not what we want. Ditto for the Next_Entity field.
7572 Save_Etype := Etype (Derived_Type);
7573 Save_Next_Entity := Next_Entity (Derived_Type);
7575 -- Assoc_List maps all stored discriminants in the Parent_Base to
7576 -- stored discriminants in the Derived_Type. It is fundamental that
7577 -- no types or itypes with discriminants other than the stored
7578 -- discriminants appear in the entities declared inside
7579 -- Derived_Type, since the back end cannot deal with it.
7581 New_Decl :=
7582 New_Copy_Tree
7583 (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
7585 -- Restore the fields saved prior to the New_Copy_Tree call
7586 -- and compute the stored constraint.
7588 Set_Etype (Derived_Type, Save_Etype);
7589 Set_Next_Entity (Derived_Type, Save_Next_Entity);
7591 if Has_Discriminants (Derived_Type) then
7592 Set_Discriminant_Constraint
7593 (Derived_Type, Save_Discr_Constr);
7594 Set_Stored_Constraint
7595 (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
7596 Replace_Components (Derived_Type, New_Decl);
7597 end if;
7599 -- Insert the new derived type declaration
7601 Rewrite (N, New_Decl);
7603 -- STEP 5b: Complete the processing for record extensions in generics
7605 -- There is no completion for record extensions declared in the
7606 -- parameter part of a generic, so we need to complete processing for
7607 -- these generic record extensions here. The Record_Type_Definition call
7608 -- will change the Ekind of the components from E_Void to E_Component.
7610 elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
7611 Record_Type_Definition (Empty, Derived_Type);
7613 -- STEP 5c: Process the record extension for non private tagged types
7615 elsif not Private_Extension then
7617 -- Add the _parent field in the derived type
7619 Expand_Record_Extension (Derived_Type, Type_Def);
7621 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
7622 -- implemented interfaces if we are in expansion mode
7624 if Expander_Active
7625 and then Has_Interfaces (Derived_Type)
7626 then
7627 Add_Interface_Tag_Components (N, Derived_Type);
7628 end if;
7630 -- Analyze the record extension
7632 Record_Type_Definition
7633 (Record_Extension_Part (Type_Def), Derived_Type);
7634 end if;
7636 End_Scope;
7638 -- Nothing else to do if there is an error in the derivation.
7639 -- An unusual case: the full view may be derived from a type in an
7640 -- instance, when the partial view was used illegally as an actual
7641 -- in that instance, leading to a circular definition.
7643 if Etype (Derived_Type) = Any_Type
7644 or else Etype (Parent_Type) = Derived_Type
7645 then
7646 return;
7647 end if;
7649 -- Set delayed freeze and then derive subprograms, we need to do
7650 -- this in this order so that derived subprograms inherit the
7651 -- derived freeze if necessary.
7653 Set_Has_Delayed_Freeze (Derived_Type);
7655 if Derive_Subps then
7656 Derive_Subprograms (Parent_Type, Derived_Type);
7657 end if;
7659 -- If we have a private extension which defines a constrained derived
7660 -- type mark as constrained here after we have derived subprograms. See
7661 -- comment on point 9. just above the body of Build_Derived_Record_Type.
7663 if Private_Extension and then Inherit_Discrims then
7664 if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
7665 Set_Is_Constrained (Derived_Type, True);
7666 Set_Discriminant_Constraint (Derived_Type, Discs);
7668 elsif Is_Constrained (Parent_Type) then
7669 Set_Is_Constrained
7670 (Derived_Type, True);
7671 Set_Discriminant_Constraint
7672 (Derived_Type, Discriminant_Constraint (Parent_Type));
7673 end if;
7674 end if;
7676 -- Update the class-wide type, which shares the now-completed entity
7677 -- list with its specific type. In case of underlying record views,
7678 -- we do not generate the corresponding class wide entity.
7680 if Is_Tagged
7681 and then not Is_Underlying_Record_View (Derived_Type)
7682 then
7683 Set_First_Entity
7684 (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
7685 Set_Last_Entity
7686 (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
7687 end if;
7689 -- Update the scope of anonymous access types of discriminants and other
7690 -- components, to prevent scope anomalies in gigi, when the derivation
7691 -- appears in a scope nested within that of the parent.
7693 declare
7694 D : Entity_Id;
7696 begin
7697 D := First_Entity (Derived_Type);
7698 while Present (D) loop
7699 if Ekind_In (D, E_Discriminant, E_Component) then
7700 if Is_Itype (Etype (D))
7701 and then Ekind (Etype (D)) = E_Anonymous_Access_Type
7702 then
7703 Set_Scope (Etype (D), Current_Scope);
7704 end if;
7705 end if;
7707 Next_Entity (D);
7708 end loop;
7709 end;
7710 end Build_Derived_Record_Type;
7712 ------------------------
7713 -- Build_Derived_Type --
7714 ------------------------
7716 procedure Build_Derived_Type
7717 (N : Node_Id;
7718 Parent_Type : Entity_Id;
7719 Derived_Type : Entity_Id;
7720 Is_Completion : Boolean;
7721 Derive_Subps : Boolean := True)
7723 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
7725 begin
7726 -- Set common attributes
7728 Set_Scope (Derived_Type, Current_Scope);
7730 Set_Ekind (Derived_Type, Ekind (Parent_Base));
7731 Set_Etype (Derived_Type, Parent_Base);
7732 Set_Has_Task (Derived_Type, Has_Task (Parent_Base));
7734 Set_Size_Info (Derived_Type, Parent_Type);
7735 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
7736 Set_Convention (Derived_Type, Convention (Parent_Type));
7737 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
7738 Set_Is_Tagged_Type (Derived_Type, Is_Tagged_Type (Parent_Type));
7740 -- Propagate invariant information. The new type has invariants if
7741 -- they are inherited from the parent type, and these invariants can
7742 -- be further inherited, so both flags are set.
7744 if Has_Inheritable_Invariants (Parent_Type) then
7745 Set_Has_Inheritable_Invariants (Derived_Type);
7746 Set_Has_Invariants (Derived_Type);
7747 end if;
7749 -- We similarly inherit predicates
7751 if Has_Predicates (Parent_Type) then
7752 Set_Has_Predicates (Derived_Type);
7753 end if;
7755 -- The derived type inherits the representation clauses of the parent.
7756 -- However, for a private type that is completed by a derivation, there
7757 -- may be operation attributes that have been specified already (stream
7758 -- attributes and External_Tag) and those must be provided. Finally,
7759 -- if the partial view is a private extension, the representation items
7760 -- of the parent have been inherited already, and should not be chained
7761 -- twice to the derived type.
7763 if Is_Tagged_Type (Parent_Type)
7764 and then Present (First_Rep_Item (Derived_Type))
7765 then
7766 -- The existing items are either operational items or items inherited
7767 -- from a private extension declaration.
7769 declare
7770 Rep : Node_Id;
7771 -- Used to iterate over representation items of the derived type
7773 Last_Rep : Node_Id;
7774 -- Last representation item of the (non-empty) representation
7775 -- item list of the derived type.
7777 Found : Boolean := False;
7779 begin
7780 Rep := First_Rep_Item (Derived_Type);
7781 Last_Rep := Rep;
7782 while Present (Rep) loop
7783 if Rep = First_Rep_Item (Parent_Type) then
7784 Found := True;
7785 exit;
7787 else
7788 Rep := Next_Rep_Item (Rep);
7790 if Present (Rep) then
7791 Last_Rep := Rep;
7792 end if;
7793 end if;
7794 end loop;
7796 -- Here if we either encountered the parent type's first rep
7797 -- item on the derived type's rep item list (in which case
7798 -- Found is True, and we have nothing else to do), or if we
7799 -- reached the last rep item of the derived type, which is
7800 -- Last_Rep, in which case we further chain the parent type's
7801 -- rep items to those of the derived type.
7803 if not Found then
7804 Set_Next_Rep_Item (Last_Rep, First_Rep_Item (Parent_Type));
7805 end if;
7806 end;
7808 else
7809 Set_First_Rep_Item (Derived_Type, First_Rep_Item (Parent_Type));
7810 end if;
7812 case Ekind (Parent_Type) is
7813 when Numeric_Kind =>
7814 Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
7816 when Array_Kind =>
7817 Build_Derived_Array_Type (N, Parent_Type, Derived_Type);
7819 when E_Record_Type
7820 | E_Record_Subtype
7821 | Class_Wide_Kind =>
7822 Build_Derived_Record_Type
7823 (N, Parent_Type, Derived_Type, Derive_Subps);
7824 return;
7826 when Enumeration_Kind =>
7827 Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
7829 when Access_Kind =>
7830 Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
7832 when Incomplete_Or_Private_Kind =>
7833 Build_Derived_Private_Type
7834 (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
7836 -- For discriminated types, the derivation includes deriving
7837 -- primitive operations. For others it is done below.
7839 if Is_Tagged_Type (Parent_Type)
7840 or else Has_Discriminants (Parent_Type)
7841 or else (Present (Full_View (Parent_Type))
7842 and then Has_Discriminants (Full_View (Parent_Type)))
7843 then
7844 return;
7845 end if;
7847 when Concurrent_Kind =>
7848 Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
7850 when others =>
7851 raise Program_Error;
7852 end case;
7854 if Etype (Derived_Type) = Any_Type then
7855 return;
7856 end if;
7858 -- Set delayed freeze and then derive subprograms, we need to do this
7859 -- in this order so that derived subprograms inherit the derived freeze
7860 -- if necessary.
7862 Set_Has_Delayed_Freeze (Derived_Type);
7863 if Derive_Subps then
7864 Derive_Subprograms (Parent_Type, Derived_Type);
7865 end if;
7867 Set_Has_Primitive_Operations
7868 (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
7869 end Build_Derived_Type;
7871 -----------------------
7872 -- Build_Discriminal --
7873 -----------------------
7875 procedure Build_Discriminal (Discrim : Entity_Id) is
7876 D_Minal : Entity_Id;
7877 CR_Disc : Entity_Id;
7879 begin
7880 -- A discriminal has the same name as the discriminant
7882 D_Minal := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
7884 Set_Ekind (D_Minal, E_In_Parameter);
7885 Set_Mechanism (D_Minal, Default_Mechanism);
7886 Set_Etype (D_Minal, Etype (Discrim));
7887 Set_Scope (D_Minal, Current_Scope);
7889 Set_Discriminal (Discrim, D_Minal);
7890 Set_Discriminal_Link (D_Minal, Discrim);
7892 -- For task types, build at once the discriminants of the corresponding
7893 -- record, which are needed if discriminants are used in entry defaults
7894 -- and in family bounds.
7896 if Is_Concurrent_Type (Current_Scope)
7897 or else Is_Limited_Type (Current_Scope)
7898 then
7899 CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
7901 Set_Ekind (CR_Disc, E_In_Parameter);
7902 Set_Mechanism (CR_Disc, Default_Mechanism);
7903 Set_Etype (CR_Disc, Etype (Discrim));
7904 Set_Scope (CR_Disc, Current_Scope);
7905 Set_Discriminal_Link (CR_Disc, Discrim);
7906 Set_CR_Discriminant (Discrim, CR_Disc);
7907 end if;
7908 end Build_Discriminal;
7910 ------------------------------------
7911 -- Build_Discriminant_Constraints --
7912 ------------------------------------
7914 function Build_Discriminant_Constraints
7915 (T : Entity_Id;
7916 Def : Node_Id;
7917 Derived_Def : Boolean := False) return Elist_Id
7919 C : constant Node_Id := Constraint (Def);
7920 Nb_Discr : constant Nat := Number_Discriminants (T);
7922 Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
7923 -- Saves the expression corresponding to a given discriminant in T
7925 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
7926 -- Return the Position number within array Discr_Expr of a discriminant
7927 -- D within the discriminant list of the discriminated type T.
7929 ------------------
7930 -- Pos_Of_Discr --
7931 ------------------
7933 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
7934 Disc : Entity_Id;
7936 begin
7937 Disc := First_Discriminant (T);
7938 for J in Discr_Expr'Range loop
7939 if Disc = D then
7940 return J;
7941 end if;
7943 Next_Discriminant (Disc);
7944 end loop;
7946 -- Note: Since this function is called on discriminants that are
7947 -- known to belong to the discriminated type, falling through the
7948 -- loop with no match signals an internal compiler error.
7950 raise Program_Error;
7951 end Pos_Of_Discr;
7953 -- Declarations local to Build_Discriminant_Constraints
7955 Discr : Entity_Id;
7956 E : Entity_Id;
7957 Elist : constant Elist_Id := New_Elmt_List;
7959 Constr : Node_Id;
7960 Expr : Node_Id;
7961 Id : Node_Id;
7962 Position : Nat;
7963 Found : Boolean;
7965 Discrim_Present : Boolean := False;
7967 -- Start of processing for Build_Discriminant_Constraints
7969 begin
7970 -- The following loop will process positional associations only.
7971 -- For a positional association, the (single) discriminant is
7972 -- implicitly specified by position, in textual order (RM 3.7.2).
7974 Discr := First_Discriminant (T);
7975 Constr := First (Constraints (C));
7976 for D in Discr_Expr'Range loop
7977 exit when Nkind (Constr) = N_Discriminant_Association;
7979 if No (Constr) then
7980 Error_Msg_N ("too few discriminants given in constraint", C);
7981 return New_Elmt_List;
7983 elsif Nkind (Constr) = N_Range
7984 or else (Nkind (Constr) = N_Attribute_Reference
7985 and then
7986 Attribute_Name (Constr) = Name_Range)
7987 then
7988 Error_Msg_N
7989 ("a range is not a valid discriminant constraint", Constr);
7990 Discr_Expr (D) := Error;
7992 else
7993 Analyze_And_Resolve (Constr, Base_Type (Etype (Discr)));
7994 Discr_Expr (D) := Constr;
7995 end if;
7997 Next_Discriminant (Discr);
7998 Next (Constr);
7999 end loop;
8001 if No (Discr) and then Present (Constr) then
8002 Error_Msg_N ("too many discriminants given in constraint", Constr);
8003 return New_Elmt_List;
8004 end if;
8006 -- Named associations can be given in any order, but if both positional
8007 -- and named associations are used in the same discriminant constraint,
8008 -- then positional associations must occur first, at their normal
8009 -- position. Hence once a named association is used, the rest of the
8010 -- discriminant constraint must use only named associations.
8012 while Present (Constr) loop
8014 -- Positional association forbidden after a named association
8016 if Nkind (Constr) /= N_Discriminant_Association then
8017 Error_Msg_N ("positional association follows named one", Constr);
8018 return New_Elmt_List;
8020 -- Otherwise it is a named association
8022 else
8023 -- E records the type of the discriminants in the named
8024 -- association. All the discriminants specified in the same name
8025 -- association must have the same type.
8027 E := Empty;
8029 -- Search the list of discriminants in T to see if the simple name
8030 -- given in the constraint matches any of them.
8032 Id := First (Selector_Names (Constr));
8033 while Present (Id) loop
8034 Found := False;
8036 -- If Original_Discriminant is present, we are processing a
8037 -- generic instantiation and this is an instance node. We need
8038 -- to find the name of the corresponding discriminant in the
8039 -- actual record type T and not the name of the discriminant in
8040 -- the generic formal. Example:
8042 -- generic
8043 -- type G (D : int) is private;
8044 -- package P is
8045 -- subtype W is G (D => 1);
8046 -- end package;
8047 -- type Rec (X : int) is record ... end record;
8048 -- package Q is new P (G => Rec);
8050 -- At the point of the instantiation, formal type G is Rec
8051 -- and therefore when reanalyzing "subtype W is G (D => 1);"
8052 -- which really looks like "subtype W is Rec (D => 1);" at
8053 -- the point of instantiation, we want to find the discriminant
8054 -- that corresponds to D in Rec, i.e. X.
8056 if Present (Original_Discriminant (Id)) then
8057 Discr := Find_Corresponding_Discriminant (Id, T);
8058 Found := True;
8060 else
8061 Discr := First_Discriminant (T);
8062 while Present (Discr) loop
8063 if Chars (Discr) = Chars (Id) then
8064 Found := True;
8065 exit;
8066 end if;
8068 Next_Discriminant (Discr);
8069 end loop;
8071 if not Found then
8072 Error_Msg_N ("& does not match any discriminant", Id);
8073 return New_Elmt_List;
8075 -- The following is only useful for the benefit of generic
8076 -- instances but it does not interfere with other
8077 -- processing for the non-generic case so we do it in all
8078 -- cases (for generics this statement is executed when
8079 -- processing the generic definition, see comment at the
8080 -- beginning of this if statement).
8082 else
8083 Set_Original_Discriminant (Id, Discr);
8084 end if;
8085 end if;
8087 Position := Pos_Of_Discr (T, Discr);
8089 if Present (Discr_Expr (Position)) then
8090 Error_Msg_N ("duplicate constraint for discriminant&", Id);
8092 else
8093 -- Each discriminant specified in the same named association
8094 -- must be associated with a separate copy of the
8095 -- corresponding expression.
8097 if Present (Next (Id)) then
8098 Expr := New_Copy_Tree (Expression (Constr));
8099 Set_Parent (Expr, Parent (Expression (Constr)));
8100 else
8101 Expr := Expression (Constr);
8102 end if;
8104 Discr_Expr (Position) := Expr;
8105 Analyze_And_Resolve (Expr, Base_Type (Etype (Discr)));
8106 end if;
8108 -- A discriminant association with more than one discriminant
8109 -- name is only allowed if the named discriminants are all of
8110 -- the same type (RM 3.7.1(8)).
8112 if E = Empty then
8113 E := Base_Type (Etype (Discr));
8115 elsif Base_Type (Etype (Discr)) /= E then
8116 Error_Msg_N
8117 ("all discriminants in an association " &
8118 "must have the same type", Id);
8119 end if;
8121 Next (Id);
8122 end loop;
8123 end if;
8125 Next (Constr);
8126 end loop;
8128 -- A discriminant constraint must provide exactly one value for each
8129 -- discriminant of the type (RM 3.7.1(8)).
8131 for J in Discr_Expr'Range loop
8132 if No (Discr_Expr (J)) then
8133 Error_Msg_N ("too few discriminants given in constraint", C);
8134 return New_Elmt_List;
8135 end if;
8136 end loop;
8138 -- Determine if there are discriminant expressions in the constraint
8140 for J in Discr_Expr'Range loop
8141 if Denotes_Discriminant
8142 (Discr_Expr (J), Check_Concurrent => True)
8143 then
8144 Discrim_Present := True;
8145 end if;
8146 end loop;
8148 -- Build an element list consisting of the expressions given in the
8149 -- discriminant constraint and apply the appropriate checks. The list
8150 -- is constructed after resolving any named discriminant associations
8151 -- and therefore the expressions appear in the textual order of the
8152 -- discriminants.
8154 Discr := First_Discriminant (T);
8155 for J in Discr_Expr'Range loop
8156 if Discr_Expr (J) /= Error then
8157 Append_Elmt (Discr_Expr (J), Elist);
8159 -- If any of the discriminant constraints is given by a
8160 -- discriminant and we are in a derived type declaration we
8161 -- have a discriminant renaming. Establish link between new
8162 -- and old discriminant.
8164 if Denotes_Discriminant (Discr_Expr (J)) then
8165 if Derived_Def then
8166 Set_Corresponding_Discriminant
8167 (Entity (Discr_Expr (J)), Discr);
8168 end if;
8170 -- Force the evaluation of non-discriminant expressions.
8171 -- If we have found a discriminant in the constraint 3.4(26)
8172 -- and 3.8(18) demand that no range checks are performed are
8173 -- after evaluation. If the constraint is for a component
8174 -- definition that has a per-object constraint, expressions are
8175 -- evaluated but not checked either. In all other cases perform
8176 -- a range check.
8178 else
8179 if Discrim_Present then
8180 null;
8182 elsif Nkind (Parent (Parent (Def))) = N_Component_Declaration
8183 and then
8184 Has_Per_Object_Constraint
8185 (Defining_Identifier (Parent (Parent (Def))))
8186 then
8187 null;
8189 elsif Is_Access_Type (Etype (Discr)) then
8190 Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
8192 else
8193 Apply_Range_Check (Discr_Expr (J), Etype (Discr));
8194 end if;
8196 Force_Evaluation (Discr_Expr (J));
8197 end if;
8199 -- Check that the designated type of an access discriminant's
8200 -- expression is not a class-wide type unless the discriminant's
8201 -- designated type is also class-wide.
8203 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
8204 and then not Is_Class_Wide_Type
8205 (Designated_Type (Etype (Discr)))
8206 and then Etype (Discr_Expr (J)) /= Any_Type
8207 and then Is_Class_Wide_Type
8208 (Designated_Type (Etype (Discr_Expr (J))))
8209 then
8210 Wrong_Type (Discr_Expr (J), Etype (Discr));
8212 elsif Is_Access_Type (Etype (Discr))
8213 and then not Is_Access_Constant (Etype (Discr))
8214 and then Is_Access_Type (Etype (Discr_Expr (J)))
8215 and then Is_Access_Constant (Etype (Discr_Expr (J)))
8216 then
8217 Error_Msg_NE
8218 ("constraint for discriminant& must be access to variable",
8219 Def, Discr);
8220 end if;
8221 end if;
8223 Next_Discriminant (Discr);
8224 end loop;
8226 return Elist;
8227 end Build_Discriminant_Constraints;
8229 ---------------------------------
8230 -- Build_Discriminated_Subtype --
8231 ---------------------------------
8233 procedure Build_Discriminated_Subtype
8234 (T : Entity_Id;
8235 Def_Id : Entity_Id;
8236 Elist : Elist_Id;
8237 Related_Nod : Node_Id;
8238 For_Access : Boolean := False)
8240 Has_Discrs : constant Boolean := Has_Discriminants (T);
8241 Constrained : constant Boolean :=
8242 (Has_Discrs
8243 and then not Is_Empty_Elmt_List (Elist)
8244 and then not Is_Class_Wide_Type (T))
8245 or else Is_Constrained (T);
8247 begin
8248 if Ekind (T) = E_Record_Type then
8249 if For_Access then
8250 Set_Ekind (Def_Id, E_Private_Subtype);
8251 Set_Is_For_Access_Subtype (Def_Id, True);
8252 else
8253 Set_Ekind (Def_Id, E_Record_Subtype);
8254 end if;
8256 -- Inherit preelaboration flag from base, for types for which it
8257 -- may have been set: records, private types, protected types.
8259 Set_Known_To_Have_Preelab_Init
8260 (Def_Id, Known_To_Have_Preelab_Init (T));
8262 elsif Ekind (T) = E_Task_Type then
8263 Set_Ekind (Def_Id, E_Task_Subtype);
8265 elsif Ekind (T) = E_Protected_Type then
8266 Set_Ekind (Def_Id, E_Protected_Subtype);
8267 Set_Known_To_Have_Preelab_Init
8268 (Def_Id, Known_To_Have_Preelab_Init (T));
8270 elsif Is_Private_Type (T) then
8271 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
8272 Set_Known_To_Have_Preelab_Init
8273 (Def_Id, Known_To_Have_Preelab_Init (T));
8275 elsif Is_Class_Wide_Type (T) then
8276 Set_Ekind (Def_Id, E_Class_Wide_Subtype);
8278 else
8279 -- Incomplete type. Attach subtype to list of dependents, to be
8280 -- completed with full view of parent type, unless is it the
8281 -- designated subtype of a record component within an init_proc.
8282 -- This last case arises for a component of an access type whose
8283 -- designated type is incomplete (e.g. a Taft Amendment type).
8284 -- The designated subtype is within an inner scope, and needs no
8285 -- elaboration, because only the access type is needed in the
8286 -- initialization procedure.
8288 Set_Ekind (Def_Id, Ekind (T));
8290 if For_Access and then Within_Init_Proc then
8291 null;
8292 else
8293 Append_Elmt (Def_Id, Private_Dependents (T));
8294 end if;
8295 end if;
8297 Set_Etype (Def_Id, T);
8298 Init_Size_Align (Def_Id);
8299 Set_Has_Discriminants (Def_Id, Has_Discrs);
8300 Set_Is_Constrained (Def_Id, Constrained);
8302 Set_First_Entity (Def_Id, First_Entity (T));
8303 Set_Last_Entity (Def_Id, Last_Entity (T));
8305 -- If the subtype is the completion of a private declaration, there may
8306 -- have been representation clauses for the partial view, and they must
8307 -- be preserved. Build_Derived_Type chains the inherited clauses with
8308 -- the ones appearing on the extension. If this comes from a subtype
8309 -- declaration, all clauses are inherited.
8311 if No (First_Rep_Item (Def_Id)) then
8312 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
8313 end if;
8315 if Is_Tagged_Type (T) then
8316 Set_Is_Tagged_Type (Def_Id);
8317 Make_Class_Wide_Type (Def_Id);
8318 end if;
8320 Set_Stored_Constraint (Def_Id, No_Elist);
8322 if Has_Discrs then
8323 Set_Discriminant_Constraint (Def_Id, Elist);
8324 Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
8325 end if;
8327 if Is_Tagged_Type (T) then
8329 -- Ada 2005 (AI-251): In case of concurrent types we inherit the
8330 -- concurrent record type (which has the list of primitive
8331 -- operations).
8333 if Ada_Version >= Ada_2005
8334 and then Is_Concurrent_Type (T)
8335 then
8336 Set_Corresponding_Record_Type (Def_Id,
8337 Corresponding_Record_Type (T));
8338 else
8339 Set_Direct_Primitive_Operations (Def_Id,
8340 Direct_Primitive_Operations (T));
8341 end if;
8343 Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
8344 end if;
8346 -- Subtypes introduced by component declarations do not need to be
8347 -- marked as delayed, and do not get freeze nodes, because the semantics
8348 -- verifies that the parents of the subtypes are frozen before the
8349 -- enclosing record is frozen.
8351 if not Is_Type (Scope (Def_Id)) then
8352 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
8354 if Is_Private_Type (T)
8355 and then Present (Full_View (T))
8356 then
8357 Conditional_Delay (Def_Id, Full_View (T));
8358 else
8359 Conditional_Delay (Def_Id, T);
8360 end if;
8361 end if;
8363 if Is_Record_Type (T) then
8364 Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
8366 if Has_Discrs
8367 and then not Is_Empty_Elmt_List (Elist)
8368 and then not For_Access
8369 then
8370 Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
8371 elsif not For_Access then
8372 Set_Cloned_Subtype (Def_Id, T);
8373 end if;
8374 end if;
8375 end Build_Discriminated_Subtype;
8377 ---------------------------
8378 -- Build_Itype_Reference --
8379 ---------------------------
8381 procedure Build_Itype_Reference
8382 (Ityp : Entity_Id;
8383 Nod : Node_Id)
8385 IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
8386 begin
8387 Set_Itype (IR, Ityp);
8388 Insert_After (Nod, IR);
8389 end Build_Itype_Reference;
8391 ------------------------
8392 -- Build_Scalar_Bound --
8393 ------------------------
8395 function Build_Scalar_Bound
8396 (Bound : Node_Id;
8397 Par_T : Entity_Id;
8398 Der_T : Entity_Id) return Node_Id
8400 New_Bound : Entity_Id;
8402 begin
8403 -- Note: not clear why this is needed, how can the original bound
8404 -- be unanalyzed at this point? and if it is, what business do we
8405 -- have messing around with it? and why is the base type of the
8406 -- parent type the right type for the resolution. It probably is
8407 -- not! It is OK for the new bound we are creating, but not for
8408 -- the old one??? Still if it never happens, no problem!
8410 Analyze_And_Resolve (Bound, Base_Type (Par_T));
8412 if Nkind_In (Bound, N_Integer_Literal, N_Real_Literal) then
8413 New_Bound := New_Copy (Bound);
8414 Set_Etype (New_Bound, Der_T);
8415 Set_Analyzed (New_Bound);
8417 elsif Is_Entity_Name (Bound) then
8418 New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
8420 -- The following is almost certainly wrong. What business do we have
8421 -- relocating a node (Bound) that is presumably still attached to
8422 -- the tree elsewhere???
8424 else
8425 New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
8426 end if;
8428 Set_Etype (New_Bound, Der_T);
8429 return New_Bound;
8430 end Build_Scalar_Bound;
8432 --------------------------------
8433 -- Build_Underlying_Full_View --
8434 --------------------------------
8436 procedure Build_Underlying_Full_View
8437 (N : Node_Id;
8438 Typ : Entity_Id;
8439 Par : Entity_Id)
8441 Loc : constant Source_Ptr := Sloc (N);
8442 Subt : constant Entity_Id :=
8443 Make_Defining_Identifier
8444 (Loc, New_External_Name (Chars (Typ), 'S'));
8446 Constr : Node_Id;
8447 Indic : Node_Id;
8448 C : Node_Id;
8449 Id : Node_Id;
8451 procedure Set_Discriminant_Name (Id : Node_Id);
8452 -- If the derived type has discriminants, they may rename discriminants
8453 -- of the parent. When building the full view of the parent, we need to
8454 -- recover the names of the original discriminants if the constraint is
8455 -- given by named associations.
8457 ---------------------------
8458 -- Set_Discriminant_Name --
8459 ---------------------------
8461 procedure Set_Discriminant_Name (Id : Node_Id) is
8462 Disc : Entity_Id;
8464 begin
8465 Set_Original_Discriminant (Id, Empty);
8467 if Has_Discriminants (Typ) then
8468 Disc := First_Discriminant (Typ);
8469 while Present (Disc) loop
8470 if Chars (Disc) = Chars (Id)
8471 and then Present (Corresponding_Discriminant (Disc))
8472 then
8473 Set_Chars (Id, Chars (Corresponding_Discriminant (Disc)));
8474 end if;
8475 Next_Discriminant (Disc);
8476 end loop;
8477 end if;
8478 end Set_Discriminant_Name;
8480 -- Start of processing for Build_Underlying_Full_View
8482 begin
8483 if Nkind (N) = N_Full_Type_Declaration then
8484 Constr := Constraint (Subtype_Indication (Type_Definition (N)));
8486 elsif Nkind (N) = N_Subtype_Declaration then
8487 Constr := New_Copy_Tree (Constraint (Subtype_Indication (N)));
8489 elsif Nkind (N) = N_Component_Declaration then
8490 Constr :=
8491 New_Copy_Tree
8492 (Constraint (Subtype_Indication (Component_Definition (N))));
8494 else
8495 raise Program_Error;
8496 end if;
8498 C := First (Constraints (Constr));
8499 while Present (C) loop
8500 if Nkind (C) = N_Discriminant_Association then
8501 Id := First (Selector_Names (C));
8502 while Present (Id) loop
8503 Set_Discriminant_Name (Id);
8504 Next (Id);
8505 end loop;
8506 end if;
8508 Next (C);
8509 end loop;
8511 Indic :=
8512 Make_Subtype_Declaration (Loc,
8513 Defining_Identifier => Subt,
8514 Subtype_Indication =>
8515 Make_Subtype_Indication (Loc,
8516 Subtype_Mark => New_Reference_To (Par, Loc),
8517 Constraint => New_Copy_Tree (Constr)));
8519 -- If this is a component subtype for an outer itype, it is not
8520 -- a list member, so simply set the parent link for analysis: if
8521 -- the enclosing type does not need to be in a declarative list,
8522 -- neither do the components.
8524 if Is_List_Member (N)
8525 and then Nkind (N) /= N_Component_Declaration
8526 then
8527 Insert_Before (N, Indic);
8528 else
8529 Set_Parent (Indic, Parent (N));
8530 end if;
8532 Analyze (Indic);
8533 Set_Underlying_Full_View (Typ, Full_View (Subt));
8534 end Build_Underlying_Full_View;
8536 -------------------------------
8537 -- Check_Abstract_Overriding --
8538 -------------------------------
8540 procedure Check_Abstract_Overriding (T : Entity_Id) is
8541 Alias_Subp : Entity_Id;
8542 Elmt : Elmt_Id;
8543 Op_List : Elist_Id;
8544 Subp : Entity_Id;
8545 Type_Def : Node_Id;
8547 procedure Check_Pragma_Implemented (Subp : Entity_Id);
8548 -- Ada 2012 (AI05-0030): Subprogram Subp overrides an interface routine
8549 -- which has pragma Implemented already set. Check whether Subp's entity
8550 -- kind conforms to the implementation kind of the overridden routine.
8552 procedure Check_Pragma_Implemented
8553 (Subp : Entity_Id;
8554 Iface_Subp : Entity_Id);
8555 -- Ada 2012 (AI05-0030): Subprogram Subp overrides interface routine
8556 -- Iface_Subp and both entities have pragma Implemented already set on
8557 -- them. Check whether the two implementation kinds are conforming.
8559 procedure Inherit_Pragma_Implemented
8560 (Subp : Entity_Id;
8561 Iface_Subp : Entity_Id);
8562 -- Ada 2012 (AI05-0030): Interface primitive Subp overrides interface
8563 -- subprogram Iface_Subp which has been marked by pragma Implemented.
8564 -- Propagate the implementation kind of Iface_Subp to Subp.
8566 ------------------------------
8567 -- Check_Pragma_Implemented --
8568 ------------------------------
8570 procedure Check_Pragma_Implemented (Subp : Entity_Id) is
8571 Iface_Alias : constant Entity_Id := Interface_Alias (Subp);
8572 Impl_Kind : constant Name_Id := Implementation_Kind (Iface_Alias);
8573 Contr_Typ : Entity_Id;
8575 begin
8576 -- Subp must have an alias since it is a hidden entity used to link
8577 -- an interface subprogram to its overriding counterpart.
8579 pragma Assert (Present (Alias (Subp)));
8581 -- Extract the type of the controlling formal
8583 Contr_Typ := Etype (First_Formal (Alias (Subp)));
8585 if Is_Concurrent_Record_Type (Contr_Typ) then
8586 Contr_Typ := Corresponding_Concurrent_Type (Contr_Typ);
8587 end if;
8589 -- An interface subprogram whose implementation kind is By_Entry must
8590 -- be implemented by an entry.
8592 if Impl_Kind = Name_By_Entry
8593 and then Ekind (Wrapped_Entity (Alias (Subp))) /= E_Entry
8594 then
8595 Error_Msg_Node_2 := Iface_Alias;
8596 Error_Msg_NE
8597 ("type & must implement abstract subprogram & with an entry",
8598 Alias (Subp), Contr_Typ);
8600 elsif Impl_Kind = Name_By_Protected_Procedure then
8602 -- An interface subprogram whose implementation kind is By_
8603 -- Protected_Procedure cannot be implemented by a primitive
8604 -- procedure of a task type.
8606 if Ekind (Contr_Typ) /= E_Protected_Type then
8607 Error_Msg_Node_2 := Contr_Typ;
8608 Error_Msg_NE
8609 ("interface subprogram & cannot be implemented by a " &
8610 "primitive procedure of task type &", Alias (Subp),
8611 Iface_Alias);
8613 -- An interface subprogram whose implementation kind is By_
8614 -- Protected_Procedure must be implemented by a procedure.
8616 elsif Is_Primitive_Wrapper (Alias (Subp))
8617 and then Ekind (Wrapped_Entity (Alias (Subp))) /= E_Procedure
8618 then
8619 Error_Msg_Node_2 := Iface_Alias;
8620 Error_Msg_NE
8621 ("type & must implement abstract subprogram & with a " &
8622 "procedure", Alias (Subp), Contr_Typ);
8623 end if;
8624 end if;
8625 end Check_Pragma_Implemented;
8627 ------------------------------
8628 -- Check_Pragma_Implemented --
8629 ------------------------------
8631 procedure Check_Pragma_Implemented
8632 (Subp : Entity_Id;
8633 Iface_Subp : Entity_Id)
8635 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
8636 Subp_Kind : constant Name_Id := Implementation_Kind (Subp);
8638 begin
8639 -- Ada 2012 (AI05-0030): The implementation kinds of an overridden
8640 -- and overriding subprogram are different. In general this is an
8641 -- error except when the implementation kind of the overridden
8642 -- subprograms is By_Any.
8644 if Iface_Kind /= Subp_Kind
8645 and then Iface_Kind /= Name_By_Any
8646 then
8647 if Iface_Kind = Name_By_Entry then
8648 Error_Msg_N
8649 ("incompatible implementation kind, overridden subprogram " &
8650 "is marked By_Entry", Subp);
8651 else
8652 Error_Msg_N
8653 ("incompatible implementation kind, overridden subprogram " &
8654 "is marked By_Protected_Procedure", Subp);
8655 end if;
8656 end if;
8657 end Check_Pragma_Implemented;
8659 --------------------------------
8660 -- Inherit_Pragma_Implemented --
8661 --------------------------------
8663 procedure Inherit_Pragma_Implemented
8664 (Subp : Entity_Id;
8665 Iface_Subp : Entity_Id)
8667 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
8668 Loc : constant Source_Ptr := Sloc (Subp);
8669 Impl_Prag : Node_Id;
8671 begin
8672 -- Since the implementation kind is stored as a representation item
8673 -- rather than a flag, create a pragma node.
8675 Impl_Prag :=
8676 Make_Pragma (Loc,
8677 Chars => Name_Implemented,
8678 Pragma_Argument_Associations => New_List (
8679 Make_Pragma_Argument_Association (Loc,
8680 Expression =>
8681 New_Reference_To (Subp, Loc)),
8683 Make_Pragma_Argument_Association (Loc,
8684 Expression => Make_Identifier (Loc, Iface_Kind))));
8686 -- The pragma doesn't need to be analyzed because it is internaly
8687 -- build. It is safe to directly register it as a rep item since we
8688 -- are only interested in the characters of the implementation kind.
8690 Record_Rep_Item (Subp, Impl_Prag);
8691 end Inherit_Pragma_Implemented;
8693 -- Start of processing for Check_Abstract_Overriding
8695 begin
8696 Op_List := Primitive_Operations (T);
8698 -- Loop to check primitive operations
8700 Elmt := First_Elmt (Op_List);
8701 while Present (Elmt) loop
8702 Subp := Node (Elmt);
8703 Alias_Subp := Alias (Subp);
8705 -- Inherited subprograms are identified by the fact that they do not
8706 -- come from source, and the associated source location is the
8707 -- location of the first subtype of the derived type.
8709 -- Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
8710 -- subprograms that "require overriding".
8712 -- Special exception, do not complain about failure to override the
8713 -- stream routines _Input and _Output, as well as the primitive
8714 -- operations used in dispatching selects since we always provide
8715 -- automatic overridings for these subprograms.
8717 -- Also ignore this rule for convention CIL since .NET libraries
8718 -- do bizarre things with interfaces???
8720 -- The partial view of T may have been a private extension, for
8721 -- which inherited functions dispatching on result are abstract.
8722 -- If the full view is a null extension, there is no need for
8723 -- overriding in Ada2005, but wrappers need to be built for them
8724 -- (see exp_ch3, Build_Controlling_Function_Wrappers).
8726 if Is_Null_Extension (T)
8727 and then Has_Controlling_Result (Subp)
8728 and then Ada_Version >= Ada_2005
8729 and then Present (Alias_Subp)
8730 and then not Comes_From_Source (Subp)
8731 and then not Is_Abstract_Subprogram (Alias_Subp)
8732 and then not Is_Access_Type (Etype (Subp))
8733 then
8734 null;
8736 -- Ada 2005 (AI-251): Internal entities of interfaces need no
8737 -- processing because this check is done with the aliased
8738 -- entity
8740 elsif Present (Interface_Alias (Subp)) then
8741 null;
8743 elsif (Is_Abstract_Subprogram (Subp)
8744 or else Requires_Overriding (Subp)
8745 or else
8746 (Has_Controlling_Result (Subp)
8747 and then Present (Alias_Subp)
8748 and then not Comes_From_Source (Subp)
8749 and then Sloc (Subp) = Sloc (First_Subtype (T))))
8750 and then not Is_TSS (Subp, TSS_Stream_Input)
8751 and then not Is_TSS (Subp, TSS_Stream_Output)
8752 and then not Is_Abstract_Type (T)
8753 and then Convention (T) /= Convention_CIL
8754 and then not Is_Predefined_Interface_Primitive (Subp)
8756 -- Ada 2005 (AI-251): Do not consider hidden entities associated
8757 -- with abstract interface types because the check will be done
8758 -- with the aliased entity (otherwise we generate a duplicated
8759 -- error message).
8761 and then not Present (Interface_Alias (Subp))
8762 then
8763 if Present (Alias_Subp) then
8765 -- Only perform the check for a derived subprogram when the
8766 -- type has an explicit record extension. This avoids incorrect
8767 -- flagging of abstract subprograms for the case of a type
8768 -- without an extension that is derived from a formal type
8769 -- with a tagged actual (can occur within a private part).
8771 -- Ada 2005 (AI-391): In the case of an inherited function with
8772 -- a controlling result of the type, the rule does not apply if
8773 -- the type is a null extension (unless the parent function
8774 -- itself is abstract, in which case the function must still be
8775 -- be overridden). The expander will generate an overriding
8776 -- wrapper function calling the parent subprogram (see
8777 -- Exp_Ch3.Make_Controlling_Wrapper_Functions).
8779 Type_Def := Type_Definition (Parent (T));
8781 if Nkind (Type_Def) = N_Derived_Type_Definition
8782 and then Present (Record_Extension_Part (Type_Def))
8783 and then
8784 (Ada_Version < Ada_2005
8785 or else not Is_Null_Extension (T)
8786 or else Ekind (Subp) = E_Procedure
8787 or else not Has_Controlling_Result (Subp)
8788 or else Is_Abstract_Subprogram (Alias_Subp)
8789 or else Requires_Overriding (Subp)
8790 or else Is_Access_Type (Etype (Subp)))
8791 then
8792 -- Avoid reporting error in case of abstract predefined
8793 -- primitive inherited from interface type because the
8794 -- body of internally generated predefined primitives
8795 -- of tagged types are generated later by Freeze_Type
8797 if Is_Interface (Root_Type (T))
8798 and then Is_Abstract_Subprogram (Subp)
8799 and then Is_Predefined_Dispatching_Operation (Subp)
8800 and then not Comes_From_Source (Ultimate_Alias (Subp))
8801 then
8802 null;
8804 else
8805 Error_Msg_NE
8806 ("type must be declared abstract or & overridden",
8807 T, Subp);
8809 -- Traverse the whole chain of aliased subprograms to
8810 -- complete the error notification. This is especially
8811 -- useful for traceability of the chain of entities when
8812 -- the subprogram corresponds with an interface
8813 -- subprogram (which may be defined in another package).
8815 if Present (Alias_Subp) then
8816 declare
8817 E : Entity_Id;
8819 begin
8820 E := Subp;
8821 while Present (Alias (E)) loop
8822 Error_Msg_Sloc := Sloc (E);
8823 Error_Msg_NE
8824 ("\& has been inherited #", T, Subp);
8825 E := Alias (E);
8826 end loop;
8828 Error_Msg_Sloc := Sloc (E);
8829 Error_Msg_NE
8830 ("\& has been inherited from subprogram #",
8831 T, Subp);
8832 end;
8833 end if;
8834 end if;
8836 -- Ada 2005 (AI-345): Protected or task type implementing
8837 -- abstract interfaces.
8839 elsif Is_Concurrent_Record_Type (T)
8840 and then Present (Interfaces (T))
8841 then
8842 -- The controlling formal of Subp must be of mode "out",
8843 -- "in out" or an access-to-variable to be overridden.
8845 -- Error message below needs rewording (remember comma
8846 -- in -gnatj mode) ???
8848 if Ekind (First_Formal (Subp)) = E_In_Parameter
8849 and then Ekind (Subp) /= E_Function
8850 then
8851 if not Is_Predefined_Dispatching_Operation (Subp) then
8852 Error_Msg_NE
8853 ("first formal of & must be of mode `OUT`, " &
8854 "`IN OUT` or access-to-variable", T, Subp);
8855 Error_Msg_N
8856 ("\to be overridden by protected procedure or " &
8857 "entry (RM 9.4(11.9/2))", T);
8858 end if;
8860 -- Some other kind of overriding failure
8862 else
8863 Error_Msg_NE
8864 ("interface subprogram & must be overridden",
8865 T, Subp);
8867 -- Examine primitive operations of synchronized type,
8868 -- to find homonyms that have the wrong profile.
8870 declare
8871 Prim : Entity_Id;
8873 begin
8874 Prim :=
8875 First_Entity (Corresponding_Concurrent_Type (T));
8876 while Present (Prim) loop
8877 if Chars (Prim) = Chars (Subp) then
8878 Error_Msg_NE
8879 ("profile is not type conformant with "
8880 & "prefixed view profile of "
8881 & "inherited operation&", Prim, Subp);
8882 end if;
8884 Next_Entity (Prim);
8885 end loop;
8886 end;
8887 end if;
8888 end if;
8890 else
8891 Error_Msg_Node_2 := T;
8892 Error_Msg_N
8893 ("abstract subprogram& not allowed for type&", Subp);
8895 -- Also post unconditional warning on the type (unconditional
8896 -- so that if there are more than one of these cases, we get
8897 -- them all, and not just the first one).
8899 Error_Msg_Node_2 := Subp;
8900 Error_Msg_N ("nonabstract type& has abstract subprogram&!", T);
8901 end if;
8902 end if;
8904 -- Ada 2012 (AI05-0030): Perform some checks related to pragma
8905 -- Implemented
8907 -- Subp is an expander-generated procedure which maps an interface
8908 -- alias to a protected wrapper. The interface alias is flagged by
8909 -- pragma Implemented. Ensure that Subp is a procedure when the
8910 -- implementation kind is By_Protected_Procedure or an entry when
8911 -- By_Entry.
8913 if Ada_Version >= Ada_2012
8914 and then Is_Hidden (Subp)
8915 and then Present (Interface_Alias (Subp))
8916 and then Has_Rep_Pragma (Interface_Alias (Subp), Name_Implemented)
8917 then
8918 Check_Pragma_Implemented (Subp);
8919 end if;
8921 -- Subp is an interface primitive which overrides another interface
8922 -- primitive marked with pragma Implemented.
8924 if Ada_Version >= Ada_2012
8925 and then Present (Overridden_Operation (Subp))
8926 and then Has_Rep_Pragma
8927 (Overridden_Operation (Subp), Name_Implemented)
8928 then
8929 -- If the overriding routine is also marked by Implemented, check
8930 -- that the two implementation kinds are conforming.
8932 if Has_Rep_Pragma (Subp, Name_Implemented) then
8933 Check_Pragma_Implemented
8934 (Subp => Subp,
8935 Iface_Subp => Overridden_Operation (Subp));
8937 -- Otherwise the overriding routine inherits the implementation
8938 -- kind from the overridden subprogram.
8940 else
8941 Inherit_Pragma_Implemented
8942 (Subp => Subp,
8943 Iface_Subp => Overridden_Operation (Subp));
8944 end if;
8945 end if;
8947 Next_Elmt (Elmt);
8948 end loop;
8949 end Check_Abstract_Overriding;
8951 ------------------------------------------------
8952 -- Check_Access_Discriminant_Requires_Limited --
8953 ------------------------------------------------
8955 procedure Check_Access_Discriminant_Requires_Limited
8956 (D : Node_Id;
8957 Loc : Node_Id)
8959 begin
8960 -- A discriminant_specification for an access discriminant shall appear
8961 -- only in the declaration for a task or protected type, or for a type
8962 -- with the reserved word 'limited' in its definition or in one of its
8963 -- ancestors (RM 3.7(10)).
8965 -- AI-0063: The proper condition is that type must be immutably limited,
8966 -- or else be a partial view.
8968 if Nkind (Discriminant_Type (D)) = N_Access_Definition then
8969 if Is_Immutably_Limited_Type (Current_Scope)
8970 or else
8971 (Nkind (Parent (Current_Scope)) = N_Private_Type_Declaration
8972 and then Limited_Present (Parent (Current_Scope)))
8973 then
8974 null;
8976 else
8977 Error_Msg_N
8978 ("access discriminants allowed only for limited types", Loc);
8979 end if;
8980 end if;
8981 end Check_Access_Discriminant_Requires_Limited;
8983 -----------------------------------
8984 -- Check_Aliased_Component_Types --
8985 -----------------------------------
8987 procedure Check_Aliased_Component_Types (T : Entity_Id) is
8988 C : Entity_Id;
8990 begin
8991 -- ??? Also need to check components of record extensions, but not
8992 -- components of protected types (which are always limited).
8994 -- Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
8995 -- types to be unconstrained. This is safe because it is illegal to
8996 -- create access subtypes to such types with explicit discriminant
8997 -- constraints.
8999 if not Is_Limited_Type (T) then
9000 if Ekind (T) = E_Record_Type then
9001 C := First_Component (T);
9002 while Present (C) loop
9003 if Is_Aliased (C)
9004 and then Has_Discriminants (Etype (C))
9005 and then not Is_Constrained (Etype (C))
9006 and then not In_Instance_Body
9007 and then Ada_Version < Ada_2005
9008 then
9009 Error_Msg_N
9010 ("aliased component must be constrained (RM 3.6(11))",
9012 end if;
9014 Next_Component (C);
9015 end loop;
9017 elsif Ekind (T) = E_Array_Type then
9018 if Has_Aliased_Components (T)
9019 and then Has_Discriminants (Component_Type (T))
9020 and then not Is_Constrained (Component_Type (T))
9021 and then not In_Instance_Body
9022 and then Ada_Version < Ada_2005
9023 then
9024 Error_Msg_N
9025 ("aliased component type must be constrained (RM 3.6(11))",
9027 end if;
9028 end if;
9029 end if;
9030 end Check_Aliased_Component_Types;
9032 ----------------------
9033 -- Check_Completion --
9034 ----------------------
9036 procedure Check_Completion (Body_Id : Node_Id := Empty) is
9037 E : Entity_Id;
9039 procedure Post_Error;
9040 -- Post error message for lack of completion for entity E
9042 ----------------
9043 -- Post_Error --
9044 ----------------
9046 procedure Post_Error is
9048 procedure Missing_Body;
9049 -- Output missing body message
9051 ------------------
9052 -- Missing_Body --
9053 ------------------
9055 procedure Missing_Body is
9056 begin
9057 -- Spec is in same unit, so we can post on spec
9059 if In_Same_Source_Unit (Body_Id, E) then
9060 Error_Msg_N ("missing body for &", E);
9062 -- Spec is in a separate unit, so we have to post on the body
9064 else
9065 Error_Msg_NE ("missing body for & declared#!", Body_Id, E);
9066 end if;
9067 end Missing_Body;
9069 -- Start of processing for Post_Error
9071 begin
9072 if not Comes_From_Source (E) then
9074 if Ekind_In (E, E_Task_Type, E_Protected_Type) then
9075 -- It may be an anonymous protected type created for a
9076 -- single variable. Post error on variable, if present.
9078 declare
9079 Var : Entity_Id;
9081 begin
9082 Var := First_Entity (Current_Scope);
9083 while Present (Var) loop
9084 exit when Etype (Var) = E
9085 and then Comes_From_Source (Var);
9087 Next_Entity (Var);
9088 end loop;
9090 if Present (Var) then
9091 E := Var;
9092 end if;
9093 end;
9094 end if;
9095 end if;
9097 -- If a generated entity has no completion, then either previous
9098 -- semantic errors have disabled the expansion phase, or else we had
9099 -- missing subunits, or else we are compiling without expansion,
9100 -- or else something is very wrong.
9102 if not Comes_From_Source (E) then
9103 pragma Assert
9104 (Serious_Errors_Detected > 0
9105 or else Configurable_Run_Time_Violations > 0
9106 or else Subunits_Missing
9107 or else not Expander_Active);
9108 return;
9110 -- Here for source entity
9112 else
9113 -- Here if no body to post the error message, so we post the error
9114 -- on the declaration that has no completion. This is not really
9115 -- the right place to post it, think about this later ???
9117 if No (Body_Id) then
9118 if Is_Type (E) then
9119 Error_Msg_NE
9120 ("missing full declaration for }", Parent (E), E);
9121 else
9122 Error_Msg_NE ("missing body for &", Parent (E), E);
9123 end if;
9125 -- Package body has no completion for a declaration that appears
9126 -- in the corresponding spec. Post error on the body, with a
9127 -- reference to the non-completed declaration.
9129 else
9130 Error_Msg_Sloc := Sloc (E);
9132 if Is_Type (E) then
9133 Error_Msg_NE ("missing full declaration for }!", Body_Id, E);
9135 elsif Is_Overloadable (E)
9136 and then Current_Entity_In_Scope (E) /= E
9137 then
9138 -- It may be that the completion is mistyped and appears as
9139 -- a distinct overloading of the entity.
9141 declare
9142 Candidate : constant Entity_Id :=
9143 Current_Entity_In_Scope (E);
9144 Decl : constant Node_Id :=
9145 Unit_Declaration_Node (Candidate);
9147 begin
9148 if Is_Overloadable (Candidate)
9149 and then Ekind (Candidate) = Ekind (E)
9150 and then Nkind (Decl) = N_Subprogram_Body
9151 and then Acts_As_Spec (Decl)
9152 then
9153 Check_Type_Conformant (Candidate, E);
9155 else
9156 Missing_Body;
9157 end if;
9158 end;
9160 else
9161 Missing_Body;
9162 end if;
9163 end if;
9164 end if;
9165 end Post_Error;
9167 -- Start of processing for Check_Completion
9169 begin
9170 E := First_Entity (Current_Scope);
9171 while Present (E) loop
9172 if Is_Intrinsic_Subprogram (E) then
9173 null;
9175 -- The following situation requires special handling: a child unit
9176 -- that appears in the context clause of the body of its parent:
9178 -- procedure Parent.Child (...);
9180 -- with Parent.Child;
9181 -- package body Parent is
9183 -- Here Parent.Child appears as a local entity, but should not be
9184 -- flagged as requiring completion, because it is a compilation
9185 -- unit.
9187 -- Ignore missing completion for a subprogram that does not come from
9188 -- source (including the _Call primitive operation of RAS types,
9189 -- which has to have the flag Comes_From_Source for other purposes):
9190 -- we assume that the expander will provide the missing completion.
9191 -- In case of previous errors, other expansion actions that provide
9192 -- bodies for null procedures with not be invoked, so inhibit message
9193 -- in those cases.
9194 -- Note that E_Operator is not in the list that follows, because
9195 -- this kind is reserved for predefined operators, that are
9196 -- intrinsic and do not need completion.
9198 elsif Ekind (E) = E_Function
9199 or else Ekind (E) = E_Procedure
9200 or else Ekind (E) = E_Generic_Function
9201 or else Ekind (E) = E_Generic_Procedure
9202 then
9203 if Has_Completion (E) then
9204 null;
9206 elsif Is_Subprogram (E) and then Is_Abstract_Subprogram (E) then
9207 null;
9209 elsif Is_Subprogram (E)
9210 and then (not Comes_From_Source (E)
9211 or else Chars (E) = Name_uCall)
9212 then
9213 null;
9215 elsif
9216 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
9217 then
9218 null;
9220 elsif Nkind (Parent (E)) = N_Procedure_Specification
9221 and then Null_Present (Parent (E))
9222 and then Serious_Errors_Detected > 0
9223 then
9224 null;
9226 else
9227 Post_Error;
9228 end if;
9230 elsif Is_Entry (E) then
9231 if not Has_Completion (E) and then
9232 (Ekind (Scope (E)) = E_Protected_Object
9233 or else Ekind (Scope (E)) = E_Protected_Type)
9234 then
9235 Post_Error;
9236 end if;
9238 elsif Is_Package_Or_Generic_Package (E) then
9239 if Unit_Requires_Body (E) then
9240 if not Has_Completion (E)
9241 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
9242 N_Compilation_Unit
9243 then
9244 Post_Error;
9245 end if;
9247 elsif not Is_Child_Unit (E) then
9248 May_Need_Implicit_Body (E);
9249 end if;
9251 elsif Ekind (E) = E_Incomplete_Type
9252 and then No (Underlying_Type (E))
9253 then
9254 Post_Error;
9256 elsif (Ekind (E) = E_Task_Type or else
9257 Ekind (E) = E_Protected_Type)
9258 and then not Has_Completion (E)
9259 then
9260 Post_Error;
9262 -- A single task declared in the current scope is a constant, verify
9263 -- that the body of its anonymous type is in the same scope. If the
9264 -- task is defined elsewhere, this may be a renaming declaration for
9265 -- which no completion is needed.
9267 elsif Ekind (E) = E_Constant
9268 and then Ekind (Etype (E)) = E_Task_Type
9269 and then not Has_Completion (Etype (E))
9270 and then Scope (Etype (E)) = Current_Scope
9271 then
9272 Post_Error;
9274 elsif Ekind (E) = E_Protected_Object
9275 and then not Has_Completion (Etype (E))
9276 then
9277 Post_Error;
9279 elsif Ekind (E) = E_Record_Type then
9280 if Is_Tagged_Type (E) then
9281 Check_Abstract_Overriding (E);
9282 Check_Conventions (E);
9283 end if;
9285 Check_Aliased_Component_Types (E);
9287 elsif Ekind (E) = E_Array_Type then
9288 Check_Aliased_Component_Types (E);
9290 end if;
9292 Next_Entity (E);
9293 end loop;
9294 end Check_Completion;
9296 ----------------------------
9297 -- Check_Delta_Expression --
9298 ----------------------------
9300 procedure Check_Delta_Expression (E : Node_Id) is
9301 begin
9302 if not (Is_Real_Type (Etype (E))) then
9303 Wrong_Type (E, Any_Real);
9305 elsif not Is_OK_Static_Expression (E) then
9306 Flag_Non_Static_Expr
9307 ("non-static expression used for delta value!", E);
9309 elsif not UR_Is_Positive (Expr_Value_R (E)) then
9310 Error_Msg_N ("delta expression must be positive", E);
9312 else
9313 return;
9314 end if;
9316 -- If any of above errors occurred, then replace the incorrect
9317 -- expression by the real 0.1, which should prevent further errors.
9319 Rewrite (E,
9320 Make_Real_Literal (Sloc (E), Ureal_Tenth));
9321 Analyze_And_Resolve (E, Standard_Float);
9322 end Check_Delta_Expression;
9324 -----------------------------
9325 -- Check_Digits_Expression --
9326 -----------------------------
9328 procedure Check_Digits_Expression (E : Node_Id) is
9329 begin
9330 if not (Is_Integer_Type (Etype (E))) then
9331 Wrong_Type (E, Any_Integer);
9333 elsif not Is_OK_Static_Expression (E) then
9334 Flag_Non_Static_Expr
9335 ("non-static expression used for digits value!", E);
9337 elsif Expr_Value (E) <= 0 then
9338 Error_Msg_N ("digits value must be greater than zero", E);
9340 else
9341 return;
9342 end if;
9344 -- If any of above errors occurred, then replace the incorrect
9345 -- expression by the integer 1, which should prevent further errors.
9347 Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
9348 Analyze_And_Resolve (E, Standard_Integer);
9350 end Check_Digits_Expression;
9352 --------------------------
9353 -- Check_Initialization --
9354 --------------------------
9356 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
9357 begin
9358 if Is_Limited_Type (T)
9359 and then not In_Instance
9360 and then not In_Inlined_Body
9361 then
9362 if not OK_For_Limited_Init (T, Exp) then
9364 -- In GNAT mode, this is just a warning, to allow it to be evilly
9365 -- turned off. Otherwise it is a real error.
9367 if GNAT_Mode then
9368 Error_Msg_N
9369 ("?cannot initialize entities of limited type!", Exp);
9371 elsif Ada_Version < Ada_2005 then
9372 Error_Msg_N
9373 ("cannot initialize entities of limited type", Exp);
9374 Explain_Limited_Type (T, Exp);
9376 else
9377 -- Specialize error message according to kind of illegal
9378 -- initial expression.
9380 if Nkind (Exp) = N_Type_Conversion
9381 and then Nkind (Expression (Exp)) = N_Function_Call
9382 then
9383 Error_Msg_N
9384 ("illegal context for call"
9385 & " to function with limited result", Exp);
9387 else
9388 Error_Msg_N
9389 ("initialization of limited object requires aggregate "
9390 & "or function call", Exp);
9391 end if;
9392 end if;
9393 end if;
9394 end if;
9395 end Check_Initialization;
9397 ----------------------
9398 -- Check_Interfaces --
9399 ----------------------
9401 procedure Check_Interfaces (N : Node_Id; Def : Node_Id) is
9402 Parent_Type : constant Entity_Id := Etype (Defining_Identifier (N));
9404 Iface : Node_Id;
9405 Iface_Def : Node_Id;
9406 Iface_Typ : Entity_Id;
9407 Parent_Node : Node_Id;
9409 Is_Task : Boolean := False;
9410 -- Set True if parent type or any progenitor is a task interface
9412 Is_Protected : Boolean := False;
9413 -- Set True if parent type or any progenitor is a protected interface
9415 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
9416 -- Check that a progenitor is compatible with declaration.
9417 -- Error is posted on Error_Node.
9419 ------------------
9420 -- Check_Ifaces --
9421 ------------------
9423 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
9424 Iface_Id : constant Entity_Id :=
9425 Defining_Identifier (Parent (Iface_Def));
9426 Type_Def : Node_Id;
9428 begin
9429 if Nkind (N) = N_Private_Extension_Declaration then
9430 Type_Def := N;
9431 else
9432 Type_Def := Type_Definition (N);
9433 end if;
9435 if Is_Task_Interface (Iface_Id) then
9436 Is_Task := True;
9438 elsif Is_Protected_Interface (Iface_Id) then
9439 Is_Protected := True;
9440 end if;
9442 if Is_Synchronized_Interface (Iface_Id) then
9444 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
9445 -- extension derived from a synchronized interface must explicitly
9446 -- be declared synchronized, because the full view will be a
9447 -- synchronized type.
9449 if Nkind (N) = N_Private_Extension_Declaration then
9450 if not Synchronized_Present (N) then
9451 Error_Msg_NE
9452 ("private extension of& must be explicitly synchronized",
9453 N, Iface_Id);
9454 end if;
9456 -- However, by 3.9.4(16/2), a full type that is a record extension
9457 -- is never allowed to derive from a synchronized interface (note
9458 -- that interfaces must be excluded from this check, because those
9459 -- are represented by derived type definitions in some cases).
9461 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition
9462 and then not Interface_Present (Type_Definition (N))
9463 then
9464 Error_Msg_N ("record extension cannot derive from synchronized"
9465 & " interface", Error_Node);
9466 end if;
9467 end if;
9469 -- Check that the characteristics of the progenitor are compatible
9470 -- with the explicit qualifier in the declaration.
9471 -- The check only applies to qualifiers that come from source.
9472 -- Limited_Present also appears in the declaration of corresponding
9473 -- records, and the check does not apply to them.
9475 if Limited_Present (Type_Def)
9476 and then not
9477 Is_Concurrent_Record_Type (Defining_Identifier (N))
9478 then
9479 if Is_Limited_Interface (Parent_Type)
9480 and then not Is_Limited_Interface (Iface_Id)
9481 then
9482 Error_Msg_NE
9483 ("progenitor& must be limited interface",
9484 Error_Node, Iface_Id);
9486 elsif
9487 (Task_Present (Iface_Def)
9488 or else Protected_Present (Iface_Def)
9489 or else Synchronized_Present (Iface_Def))
9490 and then Nkind (N) /= N_Private_Extension_Declaration
9491 and then not Error_Posted (N)
9492 then
9493 Error_Msg_NE
9494 ("progenitor& must be limited interface",
9495 Error_Node, Iface_Id);
9496 end if;
9498 -- Protected interfaces can only inherit from limited, synchronized
9499 -- or protected interfaces.
9501 elsif Nkind (N) = N_Full_Type_Declaration
9502 and then Protected_Present (Type_Def)
9503 then
9504 if Limited_Present (Iface_Def)
9505 or else Synchronized_Present (Iface_Def)
9506 or else Protected_Present (Iface_Def)
9507 then
9508 null;
9510 elsif Task_Present (Iface_Def) then
9511 Error_Msg_N ("(Ada 2005) protected interface cannot inherit"
9512 & " from task interface", Error_Node);
9514 else
9515 Error_Msg_N ("(Ada 2005) protected interface cannot inherit"
9516 & " from non-limited interface", Error_Node);
9517 end if;
9519 -- Ada 2005 (AI-345): Synchronized interfaces can only inherit from
9520 -- limited and synchronized.
9522 elsif Synchronized_Present (Type_Def) then
9523 if Limited_Present (Iface_Def)
9524 or else Synchronized_Present (Iface_Def)
9525 then
9526 null;
9528 elsif Protected_Present (Iface_Def)
9529 and then Nkind (N) /= N_Private_Extension_Declaration
9530 then
9531 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
9532 & " from protected interface", Error_Node);
9534 elsif Task_Present (Iface_Def)
9535 and then Nkind (N) /= N_Private_Extension_Declaration
9536 then
9537 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
9538 & " from task interface", Error_Node);
9540 elsif not Is_Limited_Interface (Iface_Id) then
9541 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
9542 & " from non-limited interface", Error_Node);
9543 end if;
9545 -- Ada 2005 (AI-345): Task interfaces can only inherit from limited,
9546 -- synchronized or task interfaces.
9548 elsif Nkind (N) = N_Full_Type_Declaration
9549 and then Task_Present (Type_Def)
9550 then
9551 if Limited_Present (Iface_Def)
9552 or else Synchronized_Present (Iface_Def)
9553 or else Task_Present (Iface_Def)
9554 then
9555 null;
9557 elsif Protected_Present (Iface_Def) then
9558 Error_Msg_N ("(Ada 2005) task interface cannot inherit from"
9559 & " protected interface", Error_Node);
9561 else
9562 Error_Msg_N ("(Ada 2005) task interface cannot inherit from"
9563 & " non-limited interface", Error_Node);
9564 end if;
9565 end if;
9566 end Check_Ifaces;
9568 -- Start of processing for Check_Interfaces
9570 begin
9571 if Is_Interface (Parent_Type) then
9572 if Is_Task_Interface (Parent_Type) then
9573 Is_Task := True;
9575 elsif Is_Protected_Interface (Parent_Type) then
9576 Is_Protected := True;
9577 end if;
9578 end if;
9580 if Nkind (N) = N_Private_Extension_Declaration then
9582 -- Check that progenitors are compatible with declaration
9584 Iface := First (Interface_List (Def));
9585 while Present (Iface) loop
9586 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
9588 Parent_Node := Parent (Base_Type (Iface_Typ));
9589 Iface_Def := Type_Definition (Parent_Node);
9591 if not Is_Interface (Iface_Typ) then
9592 Diagnose_Interface (Iface, Iface_Typ);
9594 else
9595 Check_Ifaces (Iface_Def, Iface);
9596 end if;
9598 Next (Iface);
9599 end loop;
9601 if Is_Task and Is_Protected then
9602 Error_Msg_N
9603 ("type cannot derive from task and protected interface", N);
9604 end if;
9606 return;
9607 end if;
9609 -- Full type declaration of derived type.
9610 -- Check compatibility with parent if it is interface type
9612 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
9613 and then Is_Interface (Parent_Type)
9614 then
9615 Parent_Node := Parent (Parent_Type);
9617 -- More detailed checks for interface varieties
9619 Check_Ifaces
9620 (Iface_Def => Type_Definition (Parent_Node),
9621 Error_Node => Subtype_Indication (Type_Definition (N)));
9622 end if;
9624 Iface := First (Interface_List (Def));
9625 while Present (Iface) loop
9626 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
9628 Parent_Node := Parent (Base_Type (Iface_Typ));
9629 Iface_Def := Type_Definition (Parent_Node);
9631 if not Is_Interface (Iface_Typ) then
9632 Diagnose_Interface (Iface, Iface_Typ);
9634 else
9635 -- "The declaration of a specific descendant of an interface
9636 -- type freezes the interface type" RM 13.14
9638 Freeze_Before (N, Iface_Typ);
9639 Check_Ifaces (Iface_Def, Error_Node => Iface);
9640 end if;
9642 Next (Iface);
9643 end loop;
9645 if Is_Task and Is_Protected then
9646 Error_Msg_N
9647 ("type cannot derive from task and protected interface", N);
9648 end if;
9649 end Check_Interfaces;
9651 ------------------------------------
9652 -- Check_Or_Process_Discriminants --
9653 ------------------------------------
9655 -- If an incomplete or private type declaration was already given for the
9656 -- type, the discriminants may have already been processed if they were
9657 -- present on the incomplete declaration. In this case a full conformance
9658 -- check has been performed in Find_Type_Name, and we then recheck here
9659 -- some properties that can't be checked on the partial view alone.
9660 -- Otherwise we call Process_Discriminants.
9662 procedure Check_Or_Process_Discriminants
9663 (N : Node_Id;
9664 T : Entity_Id;
9665 Prev : Entity_Id := Empty)
9667 begin
9668 if Has_Discriminants (T) then
9670 -- Discriminants are already set on T if they were already present
9671 -- on the partial view. Make them visible to component declarations.
9673 declare
9674 D : Entity_Id;
9675 -- Discriminant on T (full view) referencing expr on partial view
9677 Prev_D : Entity_Id;
9678 -- Entity of corresponding discriminant on partial view
9680 New_D : Node_Id;
9681 -- Discriminant specification for full view, expression is the
9682 -- syntactic copy on full view (which has been checked for
9683 -- conformance with partial view), only used here to post error
9684 -- message.
9686 begin
9687 D := First_Discriminant (T);
9688 New_D := First (Discriminant_Specifications (N));
9689 while Present (D) loop
9690 Prev_D := Current_Entity (D);
9691 Set_Current_Entity (D);
9692 Set_Is_Immediately_Visible (D);
9693 Set_Homonym (D, Prev_D);
9695 -- Handle the case where there is an untagged partial view and
9696 -- the full view is tagged: must disallow discriminants with
9697 -- defaults, unless compiling for Ada 2012, which allows a
9698 -- limited tagged type to have defaulted discriminants (see
9699 -- AI05-0214). However, suppress the error here if it was
9700 -- already reported on the default expression of the partial
9701 -- view.
9703 if Is_Tagged_Type (T)
9704 and then Present (Expression (Parent (D)))
9705 and then (not Is_Limited_Type (Current_Scope)
9706 or else Ada_Version < Ada_2012)
9707 and then not Error_Posted (Expression (Parent (D)))
9708 then
9709 if Ada_Version >= Ada_2012 then
9710 Error_Msg_N
9711 ("discriminants of nonlimited tagged type cannot have"
9712 & " defaults",
9713 Expression (New_D));
9714 else
9715 Error_Msg_N
9716 ("discriminants of tagged type cannot have defaults",
9717 Expression (New_D));
9718 end if;
9719 end if;
9721 -- Ada 2005 (AI-230): Access discriminant allowed in
9722 -- non-limited record types.
9724 if Ada_Version < Ada_2005 then
9726 -- This restriction gets applied to the full type here. It
9727 -- has already been applied earlier to the partial view.
9729 Check_Access_Discriminant_Requires_Limited (Parent (D), N);
9730 end if;
9732 Next_Discriminant (D);
9733 Next (New_D);
9734 end loop;
9735 end;
9737 elsif Present (Discriminant_Specifications (N)) then
9738 Process_Discriminants (N, Prev);
9739 end if;
9740 end Check_Or_Process_Discriminants;
9742 ----------------------
9743 -- Check_Real_Bound --
9744 ----------------------
9746 procedure Check_Real_Bound (Bound : Node_Id) is
9747 begin
9748 if not Is_Real_Type (Etype (Bound)) then
9749 Error_Msg_N
9750 ("bound in real type definition must be of real type", Bound);
9752 elsif not Is_OK_Static_Expression (Bound) then
9753 Flag_Non_Static_Expr
9754 ("non-static expression used for real type bound!", Bound);
9756 else
9757 return;
9758 end if;
9760 Rewrite
9761 (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
9762 Analyze (Bound);
9763 Resolve (Bound, Standard_Float);
9764 end Check_Real_Bound;
9766 ------------------------------
9767 -- Complete_Private_Subtype --
9768 ------------------------------
9770 procedure Complete_Private_Subtype
9771 (Priv : Entity_Id;
9772 Full : Entity_Id;
9773 Full_Base : Entity_Id;
9774 Related_Nod : Node_Id)
9776 Save_Next_Entity : Entity_Id;
9777 Save_Homonym : Entity_Id;
9779 begin
9780 -- Set semantic attributes for (implicit) private subtype completion.
9781 -- If the full type has no discriminants, then it is a copy of the full
9782 -- view of the base. Otherwise, it is a subtype of the base with a
9783 -- possible discriminant constraint. Save and restore the original
9784 -- Next_Entity field of full to ensure that the calls to Copy_Node
9785 -- do not corrupt the entity chain.
9787 -- Note that the type of the full view is the same entity as the type of
9788 -- the partial view. In this fashion, the subtype has access to the
9789 -- correct view of the parent.
9791 Save_Next_Entity := Next_Entity (Full);
9792 Save_Homonym := Homonym (Priv);
9794 case Ekind (Full_Base) is
9795 when E_Record_Type |
9796 E_Record_Subtype |
9797 Class_Wide_Kind |
9798 Private_Kind |
9799 Task_Kind |
9800 Protected_Kind =>
9801 Copy_Node (Priv, Full);
9803 Set_Has_Discriminants (Full, Has_Discriminants (Full_Base));
9804 Set_First_Entity (Full, First_Entity (Full_Base));
9805 Set_Last_Entity (Full, Last_Entity (Full_Base));
9807 when others =>
9808 Copy_Node (Full_Base, Full);
9809 Set_Chars (Full, Chars (Priv));
9810 Conditional_Delay (Full, Priv);
9811 Set_Sloc (Full, Sloc (Priv));
9812 end case;
9814 Set_Next_Entity (Full, Save_Next_Entity);
9815 Set_Homonym (Full, Save_Homonym);
9816 Set_Associated_Node_For_Itype (Full, Related_Nod);
9818 -- Set common attributes for all subtypes
9820 Set_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
9822 -- The Etype of the full view is inconsistent. Gigi needs to see the
9823 -- structural full view, which is what the current scheme gives:
9824 -- the Etype of the full view is the etype of the full base. However,
9825 -- if the full base is a derived type, the full view then looks like
9826 -- a subtype of the parent, not a subtype of the full base. If instead
9827 -- we write:
9829 -- Set_Etype (Full, Full_Base);
9831 -- then we get inconsistencies in the front-end (confusion between
9832 -- views). Several outstanding bugs are related to this ???
9834 Set_Is_First_Subtype (Full, False);
9835 Set_Scope (Full, Scope (Priv));
9836 Set_Size_Info (Full, Full_Base);
9837 Set_RM_Size (Full, RM_Size (Full_Base));
9838 Set_Is_Itype (Full);
9840 -- A subtype of a private-type-without-discriminants, whose full-view
9841 -- has discriminants with default expressions, is not constrained!
9843 if not Has_Discriminants (Priv) then
9844 Set_Is_Constrained (Full, Is_Constrained (Full_Base));
9846 if Has_Discriminants (Full_Base) then
9847 Set_Discriminant_Constraint
9848 (Full, Discriminant_Constraint (Full_Base));
9850 -- The partial view may have been indefinite, the full view
9851 -- might not be.
9853 Set_Has_Unknown_Discriminants
9854 (Full, Has_Unknown_Discriminants (Full_Base));
9855 end if;
9856 end if;
9858 Set_First_Rep_Item (Full, First_Rep_Item (Full_Base));
9859 Set_Depends_On_Private (Full, Has_Private_Component (Full));
9861 -- Freeze the private subtype entity if its parent is delayed, and not
9862 -- already frozen. We skip this processing if the type is an anonymous
9863 -- subtype of a record component, or is the corresponding record of a
9864 -- protected type, since ???
9866 if not Is_Type (Scope (Full)) then
9867 Set_Has_Delayed_Freeze (Full,
9868 Has_Delayed_Freeze (Full_Base)
9869 and then (not Is_Frozen (Full_Base)));
9870 end if;
9872 Set_Freeze_Node (Full, Empty);
9873 Set_Is_Frozen (Full, False);
9874 Set_Full_View (Priv, Full);
9876 if Has_Discriminants (Full) then
9877 Set_Stored_Constraint_From_Discriminant_Constraint (Full);
9878 Set_Stored_Constraint (Priv, Stored_Constraint (Full));
9880 if Has_Unknown_Discriminants (Full) then
9881 Set_Discriminant_Constraint (Full, No_Elist);
9882 end if;
9883 end if;
9885 if Ekind (Full_Base) = E_Record_Type
9886 and then Has_Discriminants (Full_Base)
9887 and then Has_Discriminants (Priv) -- might not, if errors
9888 and then not Has_Unknown_Discriminants (Priv)
9889 and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
9890 then
9891 Create_Constrained_Components
9892 (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
9894 -- If the full base is itself derived from private, build a congruent
9895 -- subtype of its underlying type, for use by the back end. For a
9896 -- constrained record component, the declaration cannot be placed on
9897 -- the component list, but it must nevertheless be built an analyzed, to
9898 -- supply enough information for Gigi to compute the size of component.
9900 elsif Ekind (Full_Base) in Private_Kind
9901 and then Is_Derived_Type (Full_Base)
9902 and then Has_Discriminants (Full_Base)
9903 and then (Ekind (Current_Scope) /= E_Record_Subtype)
9904 then
9905 if not Is_Itype (Priv)
9906 and then
9907 Nkind (Subtype_Indication (Parent (Priv))) = N_Subtype_Indication
9908 then
9909 Build_Underlying_Full_View
9910 (Parent (Priv), Full, Etype (Full_Base));
9912 elsif Nkind (Related_Nod) = N_Component_Declaration then
9913 Build_Underlying_Full_View (Related_Nod, Full, Etype (Full_Base));
9914 end if;
9916 elsif Is_Record_Type (Full_Base) then
9918 -- Show Full is simply a renaming of Full_Base
9920 Set_Cloned_Subtype (Full, Full_Base);
9921 end if;
9923 -- It is unsafe to share to bounds of a scalar type, because the Itype
9924 -- is elaborated on demand, and if a bound is non-static then different
9925 -- orders of elaboration in different units will lead to different
9926 -- external symbols.
9928 if Is_Scalar_Type (Full_Base) then
9929 Set_Scalar_Range (Full,
9930 Make_Range (Sloc (Related_Nod),
9931 Low_Bound =>
9932 Duplicate_Subexpr_No_Checks (Type_Low_Bound (Full_Base)),
9933 High_Bound =>
9934 Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
9936 -- This completion inherits the bounds of the full parent, but if
9937 -- the parent is an unconstrained floating point type, so is the
9938 -- completion.
9940 if Is_Floating_Point_Type (Full_Base) then
9941 Set_Includes_Infinities
9942 (Scalar_Range (Full), Has_Infinities (Full_Base));
9943 end if;
9944 end if;
9946 -- ??? It seems that a lot of fields are missing that should be copied
9947 -- from Full_Base to Full. Here are some that are introduced in a
9948 -- non-disruptive way but a cleanup is necessary.
9950 if Is_Tagged_Type (Full_Base) then
9951 Set_Is_Tagged_Type (Full);
9952 Set_Direct_Primitive_Operations (Full,
9953 Direct_Primitive_Operations (Full_Base));
9955 -- Inherit class_wide type of full_base in case the partial view was
9956 -- not tagged. Otherwise it has already been created when the private
9957 -- subtype was analyzed.
9959 if No (Class_Wide_Type (Full)) then
9960 Set_Class_Wide_Type (Full, Class_Wide_Type (Full_Base));
9961 end if;
9963 -- If this is a subtype of a protected or task type, constrain its
9964 -- corresponding record, unless this is a subtype without constraints,
9965 -- i.e. a simple renaming as with an actual subtype in an instance.
9967 elsif Is_Concurrent_Type (Full_Base) then
9968 if Has_Discriminants (Full)
9969 and then Present (Corresponding_Record_Type (Full_Base))
9970 and then
9971 not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
9972 then
9973 Set_Corresponding_Record_Type (Full,
9974 Constrain_Corresponding_Record
9975 (Full, Corresponding_Record_Type (Full_Base),
9976 Related_Nod, Full_Base));
9978 else
9979 Set_Corresponding_Record_Type (Full,
9980 Corresponding_Record_Type (Full_Base));
9981 end if;
9982 end if;
9984 -- Link rep item chain, and also setting of Has_Predicates from private
9985 -- subtype to full subtype, since we will need these on the full subtype
9986 -- to create the predicate function. Note that the full subtype may
9987 -- already have rep items, inherited from the full view of the base
9988 -- type, so we must be sure not to overwrite these entries.
9990 declare
9991 Item : Node_Id;
9992 Next_Item : Node_Id;
9994 begin
9995 Item := First_Rep_Item (Full);
9997 -- If no existing rep items on full type, we can just link directly
9998 -- to the list of items on the private type.
10000 if No (Item) then
10001 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
10003 -- Else search to end of items currently linked to the full subtype
10005 else
10006 loop
10007 Next_Item := Next_Rep_Item (Item);
10008 exit when No (Next_Item);
10009 Item := Next_Item;
10010 end loop;
10012 -- And link the private type items at the end of the chain
10014 Set_Next_Rep_Item (Item, First_Rep_Item (Priv));
10015 end if;
10016 end;
10018 -- Make sure Has_Predicates is set on full type if it is set on the
10019 -- private type. Note that it may already be set on the full type and
10020 -- if so, we don't want to unset it.
10022 if Has_Predicates (Priv) then
10023 Set_Has_Predicates (Full);
10024 end if;
10025 end Complete_Private_Subtype;
10027 ----------------------------
10028 -- Constant_Redeclaration --
10029 ----------------------------
10031 procedure Constant_Redeclaration
10032 (Id : Entity_Id;
10033 N : Node_Id;
10034 T : out Entity_Id)
10036 Prev : constant Entity_Id := Current_Entity_In_Scope (Id);
10037 Obj_Def : constant Node_Id := Object_Definition (N);
10038 New_T : Entity_Id;
10040 procedure Check_Possible_Deferred_Completion
10041 (Prev_Id : Entity_Id;
10042 Prev_Obj_Def : Node_Id;
10043 Curr_Obj_Def : Node_Id);
10044 -- Determine whether the two object definitions describe the partial
10045 -- and the full view of a constrained deferred constant. Generate
10046 -- a subtype for the full view and verify that it statically matches
10047 -- the subtype of the partial view.
10049 procedure Check_Recursive_Declaration (Typ : Entity_Id);
10050 -- If deferred constant is an access type initialized with an allocator,
10051 -- check whether there is an illegal recursion in the definition,
10052 -- through a default value of some record subcomponent. This is normally
10053 -- detected when generating init procs, but requires this additional
10054 -- mechanism when expansion is disabled.
10056 ----------------------------------------
10057 -- Check_Possible_Deferred_Completion --
10058 ----------------------------------------
10060 procedure Check_Possible_Deferred_Completion
10061 (Prev_Id : Entity_Id;
10062 Prev_Obj_Def : Node_Id;
10063 Curr_Obj_Def : Node_Id)
10065 begin
10066 if Nkind (Prev_Obj_Def) = N_Subtype_Indication
10067 and then Present (Constraint (Prev_Obj_Def))
10068 and then Nkind (Curr_Obj_Def) = N_Subtype_Indication
10069 and then Present (Constraint (Curr_Obj_Def))
10070 then
10071 declare
10072 Loc : constant Source_Ptr := Sloc (N);
10073 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
10074 Decl : constant Node_Id :=
10075 Make_Subtype_Declaration (Loc,
10076 Defining_Identifier => Def_Id,
10077 Subtype_Indication =>
10078 Relocate_Node (Curr_Obj_Def));
10080 begin
10081 Insert_Before_And_Analyze (N, Decl);
10082 Set_Etype (Id, Def_Id);
10084 if not Subtypes_Statically_Match (Etype (Prev_Id), Def_Id) then
10085 Error_Msg_Sloc := Sloc (Prev_Id);
10086 Error_Msg_N ("subtype does not statically match deferred " &
10087 "declaration#", N);
10088 end if;
10089 end;
10090 end if;
10091 end Check_Possible_Deferred_Completion;
10093 ---------------------------------
10094 -- Check_Recursive_Declaration --
10095 ---------------------------------
10097 procedure Check_Recursive_Declaration (Typ : Entity_Id) is
10098 Comp : Entity_Id;
10100 begin
10101 if Is_Record_Type (Typ) then
10102 Comp := First_Component (Typ);
10103 while Present (Comp) loop
10104 if Comes_From_Source (Comp) then
10105 if Present (Expression (Parent (Comp)))
10106 and then Is_Entity_Name (Expression (Parent (Comp)))
10107 and then Entity (Expression (Parent (Comp))) = Prev
10108 then
10109 Error_Msg_Sloc := Sloc (Parent (Comp));
10110 Error_Msg_NE
10111 ("illegal circularity with declaration for&#",
10112 N, Comp);
10113 return;
10115 elsif Is_Record_Type (Etype (Comp)) then
10116 Check_Recursive_Declaration (Etype (Comp));
10117 end if;
10118 end if;
10120 Next_Component (Comp);
10121 end loop;
10122 end if;
10123 end Check_Recursive_Declaration;
10125 -- Start of processing for Constant_Redeclaration
10127 begin
10128 if Nkind (Parent (Prev)) = N_Object_Declaration then
10129 if Nkind (Object_Definition
10130 (Parent (Prev))) = N_Subtype_Indication
10131 then
10132 -- Find type of new declaration. The constraints of the two
10133 -- views must match statically, but there is no point in
10134 -- creating an itype for the full view.
10136 if Nkind (Obj_Def) = N_Subtype_Indication then
10137 Find_Type (Subtype_Mark (Obj_Def));
10138 New_T := Entity (Subtype_Mark (Obj_Def));
10140 else
10141 Find_Type (Obj_Def);
10142 New_T := Entity (Obj_Def);
10143 end if;
10145 T := Etype (Prev);
10147 else
10148 -- The full view may impose a constraint, even if the partial
10149 -- view does not, so construct the subtype.
10151 New_T := Find_Type_Of_Object (Obj_Def, N);
10152 T := New_T;
10153 end if;
10155 else
10156 -- Current declaration is illegal, diagnosed below in Enter_Name
10158 T := Empty;
10159 New_T := Any_Type;
10160 end if;
10162 -- If previous full declaration or a renaming declaration exists, or if
10163 -- a homograph is present, let Enter_Name handle it, either with an
10164 -- error or with the removal of an overridden implicit subprogram.
10166 if Ekind (Prev) /= E_Constant
10167 or else Nkind (Parent (Prev)) = N_Object_Renaming_Declaration
10168 or else Present (Expression (Parent (Prev)))
10169 or else Present (Full_View (Prev))
10170 then
10171 Enter_Name (Id);
10173 -- Verify that types of both declarations match, or else that both types
10174 -- are anonymous access types whose designated subtypes statically match
10175 -- (as allowed in Ada 2005 by AI-385).
10177 elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
10178 and then
10179 (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
10180 or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
10181 or else Is_Access_Constant (Etype (New_T)) /=
10182 Is_Access_Constant (Etype (Prev))
10183 or else Can_Never_Be_Null (Etype (New_T)) /=
10184 Can_Never_Be_Null (Etype (Prev))
10185 or else Null_Exclusion_Present (Parent (Prev)) /=
10186 Null_Exclusion_Present (Parent (Id))
10187 or else not Subtypes_Statically_Match
10188 (Designated_Type (Etype (Prev)),
10189 Designated_Type (Etype (New_T))))
10190 then
10191 Error_Msg_Sloc := Sloc (Prev);
10192 Error_Msg_N ("type does not match declaration#", N);
10193 Set_Full_View (Prev, Id);
10194 Set_Etype (Id, Any_Type);
10196 elsif
10197 Null_Exclusion_Present (Parent (Prev))
10198 and then not Null_Exclusion_Present (N)
10199 then
10200 Error_Msg_Sloc := Sloc (Prev);
10201 Error_Msg_N ("null-exclusion does not match declaration#", N);
10202 Set_Full_View (Prev, Id);
10203 Set_Etype (Id, Any_Type);
10205 -- If so, process the full constant declaration
10207 else
10208 -- RM 7.4 (6): If the subtype defined by the subtype_indication in
10209 -- the deferred declaration is constrained, then the subtype defined
10210 -- by the subtype_indication in the full declaration shall match it
10211 -- statically.
10213 Check_Possible_Deferred_Completion
10214 (Prev_Id => Prev,
10215 Prev_Obj_Def => Object_Definition (Parent (Prev)),
10216 Curr_Obj_Def => Obj_Def);
10218 Set_Full_View (Prev, Id);
10219 Set_Is_Public (Id, Is_Public (Prev));
10220 Set_Is_Internal (Id);
10221 Append_Entity (Id, Current_Scope);
10223 -- Check ALIASED present if present before (RM 7.4(7))
10225 if Is_Aliased (Prev)
10226 and then not Aliased_Present (N)
10227 then
10228 Error_Msg_Sloc := Sloc (Prev);
10229 Error_Msg_N ("ALIASED required (see declaration#)", N);
10230 end if;
10232 -- Check that placement is in private part and that the incomplete
10233 -- declaration appeared in the visible part.
10235 if Ekind (Current_Scope) = E_Package
10236 and then not In_Private_Part (Current_Scope)
10237 then
10238 Error_Msg_Sloc := Sloc (Prev);
10239 Error_Msg_N
10240 ("full constant for declaration#"
10241 & " must be in private part", N);
10243 elsif Ekind (Current_Scope) = E_Package
10244 and then
10245 List_Containing (Parent (Prev)) /=
10246 Visible_Declarations
10247 (Specification (Unit_Declaration_Node (Current_Scope)))
10248 then
10249 Error_Msg_N
10250 ("deferred constant must be declared in visible part",
10251 Parent (Prev));
10252 end if;
10254 if Is_Access_Type (T)
10255 and then Nkind (Expression (N)) = N_Allocator
10256 then
10257 Check_Recursive_Declaration (Designated_Type (T));
10258 end if;
10259 end if;
10260 end Constant_Redeclaration;
10262 ----------------------
10263 -- Constrain_Access --
10264 ----------------------
10266 procedure Constrain_Access
10267 (Def_Id : in out Entity_Id;
10268 S : Node_Id;
10269 Related_Nod : Node_Id)
10271 T : constant Entity_Id := Entity (Subtype_Mark (S));
10272 Desig_Type : constant Entity_Id := Designated_Type (T);
10273 Desig_Subtype : Entity_Id := Create_Itype (E_Void, Related_Nod);
10274 Constraint_OK : Boolean := True;
10276 function Has_Defaulted_Discriminants (Typ : Entity_Id) return Boolean;
10277 -- Simple predicate to test for defaulted discriminants
10278 -- Shouldn't this be in sem_util???
10280 ---------------------------------
10281 -- Has_Defaulted_Discriminants --
10282 ---------------------------------
10284 function Has_Defaulted_Discriminants (Typ : Entity_Id) return Boolean is
10285 begin
10286 return Has_Discriminants (Typ)
10287 and then Present (First_Discriminant (Typ))
10288 and then Present
10289 (Discriminant_Default_Value (First_Discriminant (Typ)));
10290 end Has_Defaulted_Discriminants;
10292 -- Start of processing for Constrain_Access
10294 begin
10295 if Is_Array_Type (Desig_Type) then
10296 Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
10298 elsif (Is_Record_Type (Desig_Type)
10299 or else Is_Incomplete_Or_Private_Type (Desig_Type))
10300 and then not Is_Constrained (Desig_Type)
10301 then
10302 -- ??? The following code is a temporary kludge to ignore a
10303 -- discriminant constraint on access type if it is constraining
10304 -- the current record. Avoid creating the implicit subtype of the
10305 -- record we are currently compiling since right now, we cannot
10306 -- handle these. For now, just return the access type itself.
10308 if Desig_Type = Current_Scope
10309 and then No (Def_Id)
10310 then
10311 Set_Ekind (Desig_Subtype, E_Record_Subtype);
10312 Def_Id := Entity (Subtype_Mark (S));
10314 -- This call added to ensure that the constraint is analyzed
10315 -- (needed for a B test). Note that we still return early from
10316 -- this procedure to avoid recursive processing. ???
10318 Constrain_Discriminated_Type
10319 (Desig_Subtype, S, Related_Nod, For_Access => True);
10320 return;
10321 end if;
10323 if (Ekind (T) = E_General_Access_Type
10324 or else Ada_Version >= Ada_2005)
10325 and then Has_Private_Declaration (Desig_Type)
10326 and then In_Open_Scopes (Scope (Desig_Type))
10327 and then Has_Discriminants (Desig_Type)
10328 then
10329 -- Enforce rule that the constraint is illegal if there is
10330 -- an unconstrained view of the designated type. This means
10331 -- that the partial view (either a private type declaration or
10332 -- a derivation from a private type) has no discriminants.
10333 -- (Defect Report 8652/0008, Technical Corrigendum 1, checked
10334 -- by ACATS B371001).
10336 -- Rule updated for Ada 2005: the private type is said to have
10337 -- a constrained partial view, given that objects of the type
10338 -- can be declared. Furthermore, the rule applies to all access
10339 -- types, unlike the rule concerning default discriminants.
10341 declare
10342 Pack : constant Node_Id :=
10343 Unit_Declaration_Node (Scope (Desig_Type));
10344 Decls : List_Id;
10345 Decl : Node_Id;
10347 begin
10348 if Nkind (Pack) = N_Package_Declaration then
10349 Decls := Visible_Declarations (Specification (Pack));
10350 Decl := First (Decls);
10351 while Present (Decl) loop
10352 if (Nkind (Decl) = N_Private_Type_Declaration
10353 and then
10354 Chars (Defining_Identifier (Decl)) =
10355 Chars (Desig_Type))
10357 or else
10358 (Nkind (Decl) = N_Full_Type_Declaration
10359 and then
10360 Chars (Defining_Identifier (Decl)) =
10361 Chars (Desig_Type)
10362 and then Is_Derived_Type (Desig_Type)
10363 and then
10364 Has_Private_Declaration (Etype (Desig_Type)))
10365 then
10366 if No (Discriminant_Specifications (Decl)) then
10367 Error_Msg_N
10368 ("cannot constrain general access type if " &
10369 "designated type has constrained partial view",
10371 end if;
10373 exit;
10374 end if;
10376 Next (Decl);
10377 end loop;
10378 end if;
10379 end;
10380 end if;
10382 Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
10383 For_Access => True);
10385 elsif (Is_Task_Type (Desig_Type)
10386 or else Is_Protected_Type (Desig_Type))
10387 and then not Is_Constrained (Desig_Type)
10388 then
10389 Constrain_Concurrent
10390 (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
10392 else
10393 Error_Msg_N ("invalid constraint on access type", S);
10394 Desig_Subtype := Desig_Type; -- Ignore invalid constraint.
10395 Constraint_OK := False;
10396 end if;
10398 if No (Def_Id) then
10399 Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
10400 else
10401 Set_Ekind (Def_Id, E_Access_Subtype);
10402 end if;
10404 if Constraint_OK then
10405 Set_Etype (Def_Id, Base_Type (T));
10407 if Is_Private_Type (Desig_Type) then
10408 Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
10409 end if;
10410 else
10411 Set_Etype (Def_Id, Any_Type);
10412 end if;
10414 Set_Size_Info (Def_Id, T);
10415 Set_Is_Constrained (Def_Id, Constraint_OK);
10416 Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
10417 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
10418 Set_Is_Access_Constant (Def_Id, Is_Access_Constant (T));
10420 Conditional_Delay (Def_Id, T);
10422 -- AI-363 : Subtypes of general access types whose designated types have
10423 -- default discriminants are disallowed. In instances, the rule has to
10424 -- be checked against the actual, of which T is the subtype. In a
10425 -- generic body, the rule is checked assuming that the actual type has
10426 -- defaulted discriminants.
10428 if Ada_Version >= Ada_2005 or else Warn_On_Ada_2005_Compatibility then
10429 if Ekind (Base_Type (T)) = E_General_Access_Type
10430 and then Has_Defaulted_Discriminants (Desig_Type)
10431 then
10432 if Ada_Version < Ada_2005 then
10433 Error_Msg_N
10434 ("access subtype of general access type would not " &
10435 "be allowed in Ada 2005?", S);
10436 else
10437 Error_Msg_N
10438 ("access subype of general access type not allowed", S);
10439 end if;
10441 Error_Msg_N ("\discriminants have defaults", S);
10443 elsif Is_Access_Type (T)
10444 and then Is_Generic_Type (Desig_Type)
10445 and then Has_Discriminants (Desig_Type)
10446 and then In_Package_Body (Current_Scope)
10447 then
10448 if Ada_Version < Ada_2005 then
10449 Error_Msg_N
10450 ("access subtype would not be allowed in generic body " &
10451 "in Ada 2005?", S);
10452 else
10453 Error_Msg_N
10454 ("access subtype not allowed in generic body", S);
10455 end if;
10457 Error_Msg_N
10458 ("\designated type is a discriminated formal", S);
10459 end if;
10460 end if;
10461 end Constrain_Access;
10463 ---------------------
10464 -- Constrain_Array --
10465 ---------------------
10467 procedure Constrain_Array
10468 (Def_Id : in out Entity_Id;
10469 SI : Node_Id;
10470 Related_Nod : Node_Id;
10471 Related_Id : Entity_Id;
10472 Suffix : Character)
10474 C : constant Node_Id := Constraint (SI);
10475 Number_Of_Constraints : Nat := 0;
10476 Index : Node_Id;
10477 S, T : Entity_Id;
10478 Constraint_OK : Boolean := True;
10480 begin
10481 T := Entity (Subtype_Mark (SI));
10483 if Ekind (T) in Access_Kind then
10484 T := Designated_Type (T);
10485 end if;
10487 -- If an index constraint follows a subtype mark in a subtype indication
10488 -- then the type or subtype denoted by the subtype mark must not already
10489 -- impose an index constraint. The subtype mark must denote either an
10490 -- unconstrained array type or an access type whose designated type
10491 -- is such an array type... (RM 3.6.1)
10493 if Is_Constrained (T) then
10494 Error_Msg_N ("array type is already constrained", Subtype_Mark (SI));
10495 Constraint_OK := False;
10497 else
10498 S := First (Constraints (C));
10499 while Present (S) loop
10500 Number_Of_Constraints := Number_Of_Constraints + 1;
10501 Next (S);
10502 end loop;
10504 -- In either case, the index constraint must provide a discrete
10505 -- range for each index of the array type and the type of each
10506 -- discrete range must be the same as that of the corresponding
10507 -- index. (RM 3.6.1)
10509 if Number_Of_Constraints /= Number_Dimensions (T) then
10510 Error_Msg_NE ("incorrect number of index constraints for }", C, T);
10511 Constraint_OK := False;
10513 else
10514 S := First (Constraints (C));
10515 Index := First_Index (T);
10516 Analyze (Index);
10518 -- Apply constraints to each index type
10520 for J in 1 .. Number_Of_Constraints loop
10521 Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
10522 Next (Index);
10523 Next (S);
10524 end loop;
10526 end if;
10527 end if;
10529 if No (Def_Id) then
10530 Def_Id :=
10531 Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
10532 Set_Parent (Def_Id, Related_Nod);
10534 else
10535 Set_Ekind (Def_Id, E_Array_Subtype);
10536 end if;
10538 Set_Size_Info (Def_Id, (T));
10539 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10540 Set_Etype (Def_Id, Base_Type (T));
10542 if Constraint_OK then
10543 Set_First_Index (Def_Id, First (Constraints (C)));
10544 else
10545 Set_First_Index (Def_Id, First_Index (T));
10546 end if;
10548 Set_Is_Constrained (Def_Id, True);
10549 Set_Is_Aliased (Def_Id, Is_Aliased (T));
10550 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
10552 Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
10553 Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
10555 -- A subtype does not inherit the packed_array_type of is parent. We
10556 -- need to initialize the attribute because if Def_Id is previously
10557 -- analyzed through a limited_with clause, it will have the attributes
10558 -- of an incomplete type, one of which is an Elist that overlaps the
10559 -- Packed_Array_Type field.
10561 Set_Packed_Array_Type (Def_Id, Empty);
10563 -- Build a freeze node if parent still needs one. Also make sure that
10564 -- the Depends_On_Private status is set because the subtype will need
10565 -- reprocessing at the time the base type does, and also we must set a
10566 -- conditional delay.
10568 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
10569 Conditional_Delay (Def_Id, T);
10570 end Constrain_Array;
10572 ------------------------------
10573 -- Constrain_Component_Type --
10574 ------------------------------
10576 function Constrain_Component_Type
10577 (Comp : Entity_Id;
10578 Constrained_Typ : Entity_Id;
10579 Related_Node : Node_Id;
10580 Typ : Entity_Id;
10581 Constraints : Elist_Id) return Entity_Id
10583 Loc : constant Source_Ptr := Sloc (Constrained_Typ);
10584 Compon_Type : constant Entity_Id := Etype (Comp);
10586 function Build_Constrained_Array_Type
10587 (Old_Type : Entity_Id) return Entity_Id;
10588 -- If Old_Type is an array type, one of whose indexes is constrained
10589 -- by a discriminant, build an Itype whose constraint replaces the
10590 -- discriminant with its value in the constraint.
10592 function Build_Constrained_Discriminated_Type
10593 (Old_Type : Entity_Id) return Entity_Id;
10594 -- Ditto for record components
10596 function Build_Constrained_Access_Type
10597 (Old_Type : Entity_Id) return Entity_Id;
10598 -- Ditto for access types. Makes use of previous two functions, to
10599 -- constrain designated type.
10601 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id;
10602 -- T is an array or discriminated type, C is a list of constraints
10603 -- that apply to T. This routine builds the constrained subtype.
10605 function Is_Discriminant (Expr : Node_Id) return Boolean;
10606 -- Returns True if Expr is a discriminant
10608 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id;
10609 -- Find the value of discriminant Discrim in Constraint
10611 -----------------------------------
10612 -- Build_Constrained_Access_Type --
10613 -----------------------------------
10615 function Build_Constrained_Access_Type
10616 (Old_Type : Entity_Id) return Entity_Id
10618 Desig_Type : constant Entity_Id := Designated_Type (Old_Type);
10619 Itype : Entity_Id;
10620 Desig_Subtype : Entity_Id;
10621 Scop : Entity_Id;
10623 begin
10624 -- if the original access type was not embedded in the enclosing
10625 -- type definition, there is no need to produce a new access
10626 -- subtype. In fact every access type with an explicit constraint
10627 -- generates an itype whose scope is the enclosing record.
10629 if not Is_Type (Scope (Old_Type)) then
10630 return Old_Type;
10632 elsif Is_Array_Type (Desig_Type) then
10633 Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
10635 elsif Has_Discriminants (Desig_Type) then
10637 -- This may be an access type to an enclosing record type for
10638 -- which we are constructing the constrained components. Return
10639 -- the enclosing record subtype. This is not always correct,
10640 -- but avoids infinite recursion. ???
10642 Desig_Subtype := Any_Type;
10644 for J in reverse 0 .. Scope_Stack.Last loop
10645 Scop := Scope_Stack.Table (J).Entity;
10647 if Is_Type (Scop)
10648 and then Base_Type (Scop) = Base_Type (Desig_Type)
10649 then
10650 Desig_Subtype := Scop;
10651 end if;
10653 exit when not Is_Type (Scop);
10654 end loop;
10656 if Desig_Subtype = Any_Type then
10657 Desig_Subtype :=
10658 Build_Constrained_Discriminated_Type (Desig_Type);
10659 end if;
10661 else
10662 return Old_Type;
10663 end if;
10665 if Desig_Subtype /= Desig_Type then
10667 -- The Related_Node better be here or else we won't be able
10668 -- to attach new itypes to a node in the tree.
10670 pragma Assert (Present (Related_Node));
10672 Itype := Create_Itype (E_Access_Subtype, Related_Node);
10674 Set_Etype (Itype, Base_Type (Old_Type));
10675 Set_Size_Info (Itype, (Old_Type));
10676 Set_Directly_Designated_Type (Itype, Desig_Subtype);
10677 Set_Depends_On_Private (Itype, Has_Private_Component
10678 (Old_Type));
10679 Set_Is_Access_Constant (Itype, Is_Access_Constant
10680 (Old_Type));
10682 -- The new itype needs freezing when it depends on a not frozen
10683 -- type and the enclosing subtype needs freezing.
10685 if Has_Delayed_Freeze (Constrained_Typ)
10686 and then not Is_Frozen (Constrained_Typ)
10687 then
10688 Conditional_Delay (Itype, Base_Type (Old_Type));
10689 end if;
10691 return Itype;
10693 else
10694 return Old_Type;
10695 end if;
10696 end Build_Constrained_Access_Type;
10698 ----------------------------------
10699 -- Build_Constrained_Array_Type --
10700 ----------------------------------
10702 function Build_Constrained_Array_Type
10703 (Old_Type : Entity_Id) return Entity_Id
10705 Lo_Expr : Node_Id;
10706 Hi_Expr : Node_Id;
10707 Old_Index : Node_Id;
10708 Range_Node : Node_Id;
10709 Constr_List : List_Id;
10711 Need_To_Create_Itype : Boolean := False;
10713 begin
10714 Old_Index := First_Index (Old_Type);
10715 while Present (Old_Index) loop
10716 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
10718 if Is_Discriminant (Lo_Expr)
10719 or else Is_Discriminant (Hi_Expr)
10720 then
10721 Need_To_Create_Itype := True;
10722 end if;
10724 Next_Index (Old_Index);
10725 end loop;
10727 if Need_To_Create_Itype then
10728 Constr_List := New_List;
10730 Old_Index := First_Index (Old_Type);
10731 while Present (Old_Index) loop
10732 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
10734 if Is_Discriminant (Lo_Expr) then
10735 Lo_Expr := Get_Discr_Value (Lo_Expr);
10736 end if;
10738 if Is_Discriminant (Hi_Expr) then
10739 Hi_Expr := Get_Discr_Value (Hi_Expr);
10740 end if;
10742 Range_Node :=
10743 Make_Range
10744 (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
10746 Append (Range_Node, To => Constr_List);
10748 Next_Index (Old_Index);
10749 end loop;
10751 return Build_Subtype (Old_Type, Constr_List);
10753 else
10754 return Old_Type;
10755 end if;
10756 end Build_Constrained_Array_Type;
10758 ------------------------------------------
10759 -- Build_Constrained_Discriminated_Type --
10760 ------------------------------------------
10762 function Build_Constrained_Discriminated_Type
10763 (Old_Type : Entity_Id) return Entity_Id
10765 Expr : Node_Id;
10766 Constr_List : List_Id;
10767 Old_Constraint : Elmt_Id;
10769 Need_To_Create_Itype : Boolean := False;
10771 begin
10772 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
10773 while Present (Old_Constraint) loop
10774 Expr := Node (Old_Constraint);
10776 if Is_Discriminant (Expr) then
10777 Need_To_Create_Itype := True;
10778 end if;
10780 Next_Elmt (Old_Constraint);
10781 end loop;
10783 if Need_To_Create_Itype then
10784 Constr_List := New_List;
10786 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
10787 while Present (Old_Constraint) loop
10788 Expr := Node (Old_Constraint);
10790 if Is_Discriminant (Expr) then
10791 Expr := Get_Discr_Value (Expr);
10792 end if;
10794 Append (New_Copy_Tree (Expr), To => Constr_List);
10796 Next_Elmt (Old_Constraint);
10797 end loop;
10799 return Build_Subtype (Old_Type, Constr_List);
10801 else
10802 return Old_Type;
10803 end if;
10804 end Build_Constrained_Discriminated_Type;
10806 -------------------
10807 -- Build_Subtype --
10808 -------------------
10810 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id is
10811 Indic : Node_Id;
10812 Subtyp_Decl : Node_Id;
10813 Def_Id : Entity_Id;
10814 Btyp : Entity_Id := Base_Type (T);
10816 begin
10817 -- The Related_Node better be here or else we won't be able to
10818 -- attach new itypes to a node in the tree.
10820 pragma Assert (Present (Related_Node));
10822 -- If the view of the component's type is incomplete or private
10823 -- with unknown discriminants, then the constraint must be applied
10824 -- to the full type.
10826 if Has_Unknown_Discriminants (Btyp)
10827 and then Present (Underlying_Type (Btyp))
10828 then
10829 Btyp := Underlying_Type (Btyp);
10830 end if;
10832 Indic :=
10833 Make_Subtype_Indication (Loc,
10834 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
10835 Constraint => Make_Index_Or_Discriminant_Constraint (Loc, C));
10837 Def_Id := Create_Itype (Ekind (T), Related_Node);
10839 Subtyp_Decl :=
10840 Make_Subtype_Declaration (Loc,
10841 Defining_Identifier => Def_Id,
10842 Subtype_Indication => Indic);
10844 Set_Parent (Subtyp_Decl, Parent (Related_Node));
10846 -- Itypes must be analyzed with checks off (see package Itypes)
10848 Analyze (Subtyp_Decl, Suppress => All_Checks);
10850 return Def_Id;
10851 end Build_Subtype;
10853 ---------------------
10854 -- Get_Discr_Value --
10855 ---------------------
10857 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id is
10858 D : Entity_Id;
10859 E : Elmt_Id;
10861 begin
10862 -- The discriminant may be declared for the type, in which case we
10863 -- find it by iterating over the list of discriminants. If the
10864 -- discriminant is inherited from a parent type, it appears as the
10865 -- corresponding discriminant of the current type. This will be the
10866 -- case when constraining an inherited component whose constraint is
10867 -- given by a discriminant of the parent.
10869 D := First_Discriminant (Typ);
10870 E := First_Elmt (Constraints);
10872 while Present (D) loop
10873 if D = Entity (Discrim)
10874 or else D = CR_Discriminant (Entity (Discrim))
10875 or else Corresponding_Discriminant (D) = Entity (Discrim)
10876 then
10877 return Node (E);
10878 end if;
10880 Next_Discriminant (D);
10881 Next_Elmt (E);
10882 end loop;
10884 -- The Corresponding_Discriminant mechanism is incomplete, because
10885 -- the correspondence between new and old discriminants is not one
10886 -- to one: one new discriminant can constrain several old ones. In
10887 -- that case, scan sequentially the stored_constraint, the list of
10888 -- discriminants of the parents, and the constraints.
10889 -- Previous code checked for the present of the Stored_Constraint
10890 -- list for the derived type, but did not use it at all. Should it
10891 -- be present when the component is a discriminated task type?
10893 if Is_Derived_Type (Typ)
10894 and then Scope (Entity (Discrim)) = Etype (Typ)
10895 then
10896 D := First_Discriminant (Etype (Typ));
10897 E := First_Elmt (Constraints);
10898 while Present (D) loop
10899 if D = Entity (Discrim) then
10900 return Node (E);
10901 end if;
10903 Next_Discriminant (D);
10904 Next_Elmt (E);
10905 end loop;
10906 end if;
10908 -- Something is wrong if we did not find the value
10910 raise Program_Error;
10911 end Get_Discr_Value;
10913 ---------------------
10914 -- Is_Discriminant --
10915 ---------------------
10917 function Is_Discriminant (Expr : Node_Id) return Boolean is
10918 Discrim_Scope : Entity_Id;
10920 begin
10921 if Denotes_Discriminant (Expr) then
10922 Discrim_Scope := Scope (Entity (Expr));
10924 -- Either we have a reference to one of Typ's discriminants,
10926 pragma Assert (Discrim_Scope = Typ
10928 -- or to the discriminants of the parent type, in the case
10929 -- of a derivation of a tagged type with variants.
10931 or else Discrim_Scope = Etype (Typ)
10932 or else Full_View (Discrim_Scope) = Etype (Typ)
10934 -- or same as above for the case where the discriminants
10935 -- were declared in Typ's private view.
10937 or else (Is_Private_Type (Discrim_Scope)
10938 and then Chars (Discrim_Scope) = Chars (Typ))
10940 -- or else we are deriving from the full view and the
10941 -- discriminant is declared in the private entity.
10943 or else (Is_Private_Type (Typ)
10944 and then Chars (Discrim_Scope) = Chars (Typ))
10946 -- Or we are constrained the corresponding record of a
10947 -- synchronized type that completes a private declaration.
10949 or else (Is_Concurrent_Record_Type (Typ)
10950 and then
10951 Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
10953 -- or we have a class-wide type, in which case make sure the
10954 -- discriminant found belongs to the root type.
10956 or else (Is_Class_Wide_Type (Typ)
10957 and then Etype (Typ) = Discrim_Scope));
10959 return True;
10960 end if;
10962 -- In all other cases we have something wrong
10964 return False;
10965 end Is_Discriminant;
10967 -- Start of processing for Constrain_Component_Type
10969 begin
10970 if Nkind (Parent (Comp)) = N_Component_Declaration
10971 and then Comes_From_Source (Parent (Comp))
10972 and then Comes_From_Source
10973 (Subtype_Indication (Component_Definition (Parent (Comp))))
10974 and then
10975 Is_Entity_Name
10976 (Subtype_Indication (Component_Definition (Parent (Comp))))
10977 then
10978 return Compon_Type;
10980 elsif Is_Array_Type (Compon_Type) then
10981 return Build_Constrained_Array_Type (Compon_Type);
10983 elsif Has_Discriminants (Compon_Type) then
10984 return Build_Constrained_Discriminated_Type (Compon_Type);
10986 elsif Is_Access_Type (Compon_Type) then
10987 return Build_Constrained_Access_Type (Compon_Type);
10989 else
10990 return Compon_Type;
10991 end if;
10992 end Constrain_Component_Type;
10994 --------------------------
10995 -- Constrain_Concurrent --
10996 --------------------------
10998 -- For concurrent types, the associated record value type carries the same
10999 -- discriminants, so when we constrain a concurrent type, we must constrain
11000 -- the corresponding record type as well.
11002 procedure Constrain_Concurrent
11003 (Def_Id : in out Entity_Id;
11004 SI : Node_Id;
11005 Related_Nod : Node_Id;
11006 Related_Id : Entity_Id;
11007 Suffix : Character)
11009 T_Ent : Entity_Id := Entity (Subtype_Mark (SI));
11010 T_Val : Entity_Id;
11012 begin
11013 if Ekind (T_Ent) in Access_Kind then
11014 T_Ent := Designated_Type (T_Ent);
11015 end if;
11017 T_Val := Corresponding_Record_Type (T_Ent);
11019 if Present (T_Val) then
11021 if No (Def_Id) then
11022 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
11023 end if;
11025 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
11027 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
11028 Set_Corresponding_Record_Type (Def_Id,
11029 Constrain_Corresponding_Record
11030 (Def_Id, T_Val, Related_Nod, Related_Id));
11032 else
11033 -- If there is no associated record, expansion is disabled and this
11034 -- is a generic context. Create a subtype in any case, so that
11035 -- semantic analysis can proceed.
11037 if No (Def_Id) then
11038 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
11039 end if;
11041 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
11042 end if;
11043 end Constrain_Concurrent;
11045 ------------------------------------
11046 -- Constrain_Corresponding_Record --
11047 ------------------------------------
11049 function Constrain_Corresponding_Record
11050 (Prot_Subt : Entity_Id;
11051 Corr_Rec : Entity_Id;
11052 Related_Nod : Node_Id;
11053 Related_Id : Entity_Id) return Entity_Id
11055 T_Sub : constant Entity_Id :=
11056 Create_Itype (E_Record_Subtype, Related_Nod, Related_Id, 'V');
11058 begin
11059 Set_Etype (T_Sub, Corr_Rec);
11060 Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
11061 Set_Is_Constrained (T_Sub, True);
11062 Set_First_Entity (T_Sub, First_Entity (Corr_Rec));
11063 Set_Last_Entity (T_Sub, Last_Entity (Corr_Rec));
11065 -- As elsewhere, we do not want to create a freeze node for this itype
11066 -- if it is created for a constrained component of an enclosing record
11067 -- because references to outer discriminants will appear out of scope.
11069 if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
11070 Conditional_Delay (T_Sub, Corr_Rec);
11071 else
11072 Set_Is_Frozen (T_Sub);
11073 end if;
11075 if Has_Discriminants (Prot_Subt) then -- False only if errors.
11076 Set_Discriminant_Constraint
11077 (T_Sub, Discriminant_Constraint (Prot_Subt));
11078 Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
11079 Create_Constrained_Components
11080 (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
11081 end if;
11083 Set_Depends_On_Private (T_Sub, Has_Private_Component (T_Sub));
11085 return T_Sub;
11086 end Constrain_Corresponding_Record;
11088 -----------------------
11089 -- Constrain_Decimal --
11090 -----------------------
11092 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id) is
11093 T : constant Entity_Id := Entity (Subtype_Mark (S));
11094 C : constant Node_Id := Constraint (S);
11095 Loc : constant Source_Ptr := Sloc (C);
11096 Range_Expr : Node_Id;
11097 Digits_Expr : Node_Id;
11098 Digits_Val : Uint;
11099 Bound_Val : Ureal;
11101 begin
11102 Set_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
11104 if Nkind (C) = N_Range_Constraint then
11105 Range_Expr := Range_Expression (C);
11106 Digits_Val := Digits_Value (T);
11108 else
11109 pragma Assert (Nkind (C) = N_Digits_Constraint);
11110 Digits_Expr := Digits_Expression (C);
11111 Analyze_And_Resolve (Digits_Expr, Any_Integer);
11113 Check_Digits_Expression (Digits_Expr);
11114 Digits_Val := Expr_Value (Digits_Expr);
11116 if Digits_Val > Digits_Value (T) then
11117 Error_Msg_N
11118 ("digits expression is incompatible with subtype", C);
11119 Digits_Val := Digits_Value (T);
11120 end if;
11122 if Present (Range_Constraint (C)) then
11123 Range_Expr := Range_Expression (Range_Constraint (C));
11124 else
11125 Range_Expr := Empty;
11126 end if;
11127 end if;
11129 Set_Etype (Def_Id, Base_Type (T));
11130 Set_Size_Info (Def_Id, (T));
11131 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
11132 Set_Delta_Value (Def_Id, Delta_Value (T));
11133 Set_Scale_Value (Def_Id, Scale_Value (T));
11134 Set_Small_Value (Def_Id, Small_Value (T));
11135 Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
11136 Set_Digits_Value (Def_Id, Digits_Val);
11138 -- Manufacture range from given digits value if no range present
11140 if No (Range_Expr) then
11141 Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
11142 Range_Expr :=
11143 Make_Range (Loc,
11144 Low_Bound =>
11145 Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
11146 High_Bound =>
11147 Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
11148 end if;
11150 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
11151 Set_Discrete_RM_Size (Def_Id);
11153 -- Unconditionally delay the freeze, since we cannot set size
11154 -- information in all cases correctly until the freeze point.
11156 Set_Has_Delayed_Freeze (Def_Id);
11157 end Constrain_Decimal;
11159 ----------------------------------
11160 -- Constrain_Discriminated_Type --
11161 ----------------------------------
11163 procedure Constrain_Discriminated_Type
11164 (Def_Id : Entity_Id;
11165 S : Node_Id;
11166 Related_Nod : Node_Id;
11167 For_Access : Boolean := False)
11169 E : constant Entity_Id := Entity (Subtype_Mark (S));
11170 T : Entity_Id;
11171 C : Node_Id;
11172 Elist : Elist_Id := New_Elmt_List;
11174 procedure Fixup_Bad_Constraint;
11175 -- This is called after finding a bad constraint, and after having
11176 -- posted an appropriate error message. The mission is to leave the
11177 -- entity T in as reasonable state as possible!
11179 --------------------------
11180 -- Fixup_Bad_Constraint --
11181 --------------------------
11183 procedure Fixup_Bad_Constraint is
11184 begin
11185 -- Set a reasonable Ekind for the entity. For an incomplete type,
11186 -- we can't do much, but for other types, we can set the proper
11187 -- corresponding subtype kind.
11189 if Ekind (T) = E_Incomplete_Type then
11190 Set_Ekind (Def_Id, Ekind (T));
11191 else
11192 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
11193 end if;
11195 -- Set Etype to the known type, to reduce chances of cascaded errors
11197 Set_Etype (Def_Id, E);
11198 Set_Error_Posted (Def_Id);
11199 end Fixup_Bad_Constraint;
11201 -- Start of processing for Constrain_Discriminated_Type
11203 begin
11204 C := Constraint (S);
11206 -- A discriminant constraint is only allowed in a subtype indication,
11207 -- after a subtype mark. This subtype mark must denote either a type
11208 -- with discriminants, or an access type whose designated type is a
11209 -- type with discriminants. A discriminant constraint specifies the
11210 -- values of these discriminants (RM 3.7.2(5)).
11212 T := Base_Type (Entity (Subtype_Mark (S)));
11214 if Ekind (T) in Access_Kind then
11215 T := Designated_Type (T);
11216 end if;
11218 -- Ada 2005 (AI-412): Constrained incomplete subtypes are illegal.
11219 -- Avoid generating an error for access-to-incomplete subtypes.
11221 if Ada_Version >= Ada_2005
11222 and then Ekind (T) = E_Incomplete_Type
11223 and then Nkind (Parent (S)) = N_Subtype_Declaration
11224 and then not Is_Itype (Def_Id)
11225 then
11226 -- A little sanity check, emit an error message if the type
11227 -- has discriminants to begin with. Type T may be a regular
11228 -- incomplete type or imported via a limited with clause.
11230 if Has_Discriminants (T)
11231 or else
11232 (From_With_Type (T)
11233 and then Present (Non_Limited_View (T))
11234 and then Nkind (Parent (Non_Limited_View (T))) =
11235 N_Full_Type_Declaration
11236 and then Present (Discriminant_Specifications
11237 (Parent (Non_Limited_View (T)))))
11238 then
11239 Error_Msg_N
11240 ("(Ada 2005) incomplete subtype may not be constrained", C);
11241 else
11242 Error_Msg_N ("invalid constraint: type has no discriminant", C);
11243 end if;
11245 Fixup_Bad_Constraint;
11246 return;
11248 -- Check that the type has visible discriminants. The type may be
11249 -- a private type with unknown discriminants whose full view has
11250 -- discriminants which are invisible.
11252 elsif not Has_Discriminants (T)
11253 or else
11254 (Has_Unknown_Discriminants (T)
11255 and then Is_Private_Type (T))
11256 then
11257 Error_Msg_N ("invalid constraint: type has no discriminant", C);
11258 Fixup_Bad_Constraint;
11259 return;
11261 elsif Is_Constrained (E)
11262 or else (Ekind (E) = E_Class_Wide_Subtype
11263 and then Present (Discriminant_Constraint (E)))
11264 then
11265 Error_Msg_N ("type is already constrained", Subtype_Mark (S));
11266 Fixup_Bad_Constraint;
11267 return;
11268 end if;
11270 -- T may be an unconstrained subtype (e.g. a generic actual).
11271 -- Constraint applies to the base type.
11273 T := Base_Type (T);
11275 Elist := Build_Discriminant_Constraints (T, S);
11277 -- If the list returned was empty we had an error in building the
11278 -- discriminant constraint. We have also already signalled an error
11279 -- in the incomplete type case
11281 if Is_Empty_Elmt_List (Elist) then
11282 Fixup_Bad_Constraint;
11283 return;
11284 end if;
11286 Build_Discriminated_Subtype (T, Def_Id, Elist, Related_Nod, For_Access);
11287 end Constrain_Discriminated_Type;
11289 ---------------------------
11290 -- Constrain_Enumeration --
11291 ---------------------------
11293 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id) is
11294 T : constant Entity_Id := Entity (Subtype_Mark (S));
11295 C : constant Node_Id := Constraint (S);
11297 begin
11298 Set_Ekind (Def_Id, E_Enumeration_Subtype);
11300 Set_First_Literal (Def_Id, First_Literal (Base_Type (T)));
11302 Set_Etype (Def_Id, Base_Type (T));
11303 Set_Size_Info (Def_Id, (T));
11304 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
11305 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
11307 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
11309 Set_Discrete_RM_Size (Def_Id);
11310 end Constrain_Enumeration;
11312 ----------------------
11313 -- Constrain_Float --
11314 ----------------------
11316 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id) is
11317 T : constant Entity_Id := Entity (Subtype_Mark (S));
11318 C : Node_Id;
11319 D : Node_Id;
11320 Rais : Node_Id;
11322 begin
11323 Set_Ekind (Def_Id, E_Floating_Point_Subtype);
11325 Set_Etype (Def_Id, Base_Type (T));
11326 Set_Size_Info (Def_Id, (T));
11327 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
11329 -- Process the constraint
11331 C := Constraint (S);
11333 -- Digits constraint present
11335 if Nkind (C) = N_Digits_Constraint then
11336 Check_Restriction (No_Obsolescent_Features, C);
11338 if Warn_On_Obsolescent_Feature then
11339 Error_Msg_N
11340 ("subtype digits constraint is an " &
11341 "obsolescent feature (RM J.3(8))?", C);
11342 end if;
11344 D := Digits_Expression (C);
11345 Analyze_And_Resolve (D, Any_Integer);
11346 Check_Digits_Expression (D);
11347 Set_Digits_Value (Def_Id, Expr_Value (D));
11349 -- Check that digits value is in range. Obviously we can do this
11350 -- at compile time, but it is strictly a runtime check, and of
11351 -- course there is an ACVC test that checks this!
11353 if Digits_Value (Def_Id) > Digits_Value (T) then
11354 Error_Msg_Uint_1 := Digits_Value (T);
11355 Error_Msg_N ("?digits value is too large, maximum is ^", D);
11356 Rais :=
11357 Make_Raise_Constraint_Error (Sloc (D),
11358 Reason => CE_Range_Check_Failed);
11359 Insert_Action (Declaration_Node (Def_Id), Rais);
11360 end if;
11362 C := Range_Constraint (C);
11364 -- No digits constraint present
11366 else
11367 Set_Digits_Value (Def_Id, Digits_Value (T));
11368 end if;
11370 -- Range constraint present
11372 if Nkind (C) = N_Range_Constraint then
11373 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
11375 -- No range constraint present
11377 else
11378 pragma Assert (No (C));
11379 Set_Scalar_Range (Def_Id, Scalar_Range (T));
11380 end if;
11382 Set_Is_Constrained (Def_Id);
11383 end Constrain_Float;
11385 ---------------------
11386 -- Constrain_Index --
11387 ---------------------
11389 procedure Constrain_Index
11390 (Index : Node_Id;
11391 S : Node_Id;
11392 Related_Nod : Node_Id;
11393 Related_Id : Entity_Id;
11394 Suffix : Character;
11395 Suffix_Index : Nat)
11397 Def_Id : Entity_Id;
11398 R : Node_Id := Empty;
11399 T : constant Entity_Id := Etype (Index);
11401 begin
11402 if Nkind (S) = N_Range
11403 or else
11404 (Nkind (S) = N_Attribute_Reference
11405 and then Attribute_Name (S) = Name_Range)
11406 then
11407 -- A Range attribute will transformed into N_Range by Resolve
11409 Analyze (S);
11410 Set_Etype (S, T);
11411 R := S;
11413 Process_Range_Expr_In_Decl (R, T, Empty_List);
11415 if not Error_Posted (S)
11416 and then
11417 (Nkind (S) /= N_Range
11418 or else not Covers (T, (Etype (Low_Bound (S))))
11419 or else not Covers (T, (Etype (High_Bound (S)))))
11420 then
11421 if Base_Type (T) /= Any_Type
11422 and then Etype (Low_Bound (S)) /= Any_Type
11423 and then Etype (High_Bound (S)) /= Any_Type
11424 then
11425 Error_Msg_N ("range expected", S);
11426 end if;
11427 end if;
11429 elsif Nkind (S) = N_Subtype_Indication then
11431 -- The parser has verified that this is a discrete indication
11433 Resolve_Discrete_Subtype_Indication (S, T);
11434 R := Range_Expression (Constraint (S));
11436 elsif Nkind (S) = N_Discriminant_Association then
11438 -- Syntactically valid in subtype indication
11440 Error_Msg_N ("invalid index constraint", S);
11441 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
11442 return;
11444 -- Subtype_Mark case, no anonymous subtypes to construct
11446 else
11447 Analyze (S);
11449 if Is_Entity_Name (S) then
11450 if not Is_Type (Entity (S)) then
11451 Error_Msg_N ("expect subtype mark for index constraint", S);
11453 elsif Base_Type (Entity (S)) /= Base_Type (T) then
11454 Wrong_Type (S, Base_Type (T));
11456 -- Check error of subtype with predicate in index constraint
11458 else
11459 Bad_Predicated_Subtype_Use
11460 ("subtype& has predicate, not allowed in index constraint",
11461 S, Entity (S));
11462 end if;
11464 return;
11466 else
11467 Error_Msg_N ("invalid index constraint", S);
11468 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
11469 return;
11470 end if;
11471 end if;
11473 Def_Id :=
11474 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
11476 Set_Etype (Def_Id, Base_Type (T));
11478 if Is_Modular_Integer_Type (T) then
11479 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
11481 elsif Is_Integer_Type (T) then
11482 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
11484 else
11485 Set_Ekind (Def_Id, E_Enumeration_Subtype);
11486 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
11487 Set_First_Literal (Def_Id, First_Literal (T));
11488 end if;
11490 Set_Size_Info (Def_Id, (T));
11491 Set_RM_Size (Def_Id, RM_Size (T));
11492 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
11494 Set_Scalar_Range (Def_Id, R);
11496 Set_Etype (S, Def_Id);
11497 Set_Discrete_RM_Size (Def_Id);
11498 end Constrain_Index;
11500 -----------------------
11501 -- Constrain_Integer --
11502 -----------------------
11504 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id) is
11505 T : constant Entity_Id := Entity (Subtype_Mark (S));
11506 C : constant Node_Id := Constraint (S);
11508 begin
11509 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
11511 if Is_Modular_Integer_Type (T) then
11512 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
11513 else
11514 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
11515 end if;
11517 Set_Etype (Def_Id, Base_Type (T));
11518 Set_Size_Info (Def_Id, (T));
11519 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
11520 Set_Discrete_RM_Size (Def_Id);
11521 end Constrain_Integer;
11523 ------------------------------
11524 -- Constrain_Ordinary_Fixed --
11525 ------------------------------
11527 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id) is
11528 T : constant Entity_Id := Entity (Subtype_Mark (S));
11529 C : Node_Id;
11530 D : Node_Id;
11531 Rais : Node_Id;
11533 begin
11534 Set_Ekind (Def_Id, E_Ordinary_Fixed_Point_Subtype);
11535 Set_Etype (Def_Id, Base_Type (T));
11536 Set_Size_Info (Def_Id, (T));
11537 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
11538 Set_Small_Value (Def_Id, Small_Value (T));
11540 -- Process the constraint
11542 C := Constraint (S);
11544 -- Delta constraint present
11546 if Nkind (C) = N_Delta_Constraint then
11547 Check_Restriction (No_Obsolescent_Features, C);
11549 if Warn_On_Obsolescent_Feature then
11550 Error_Msg_S
11551 ("subtype delta constraint is an " &
11552 "obsolescent feature (RM J.3(7))?");
11553 end if;
11555 D := Delta_Expression (C);
11556 Analyze_And_Resolve (D, Any_Real);
11557 Check_Delta_Expression (D);
11558 Set_Delta_Value (Def_Id, Expr_Value_R (D));
11560 -- Check that delta value is in range. Obviously we can do this
11561 -- at compile time, but it is strictly a runtime check, and of
11562 -- course there is an ACVC test that checks this!
11564 if Delta_Value (Def_Id) < Delta_Value (T) then
11565 Error_Msg_N ("?delta value is too small", D);
11566 Rais :=
11567 Make_Raise_Constraint_Error (Sloc (D),
11568 Reason => CE_Range_Check_Failed);
11569 Insert_Action (Declaration_Node (Def_Id), Rais);
11570 end if;
11572 C := Range_Constraint (C);
11574 -- No delta constraint present
11576 else
11577 Set_Delta_Value (Def_Id, Delta_Value (T));
11578 end if;
11580 -- Range constraint present
11582 if Nkind (C) = N_Range_Constraint then
11583 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
11585 -- No range constraint present
11587 else
11588 pragma Assert (No (C));
11589 Set_Scalar_Range (Def_Id, Scalar_Range (T));
11591 end if;
11593 Set_Discrete_RM_Size (Def_Id);
11595 -- Unconditionally delay the freeze, since we cannot set size
11596 -- information in all cases correctly until the freeze point.
11598 Set_Has_Delayed_Freeze (Def_Id);
11599 end Constrain_Ordinary_Fixed;
11601 -----------------------
11602 -- Contain_Interface --
11603 -----------------------
11605 function Contain_Interface
11606 (Iface : Entity_Id;
11607 Ifaces : Elist_Id) return Boolean
11609 Iface_Elmt : Elmt_Id;
11611 begin
11612 if Present (Ifaces) then
11613 Iface_Elmt := First_Elmt (Ifaces);
11614 while Present (Iface_Elmt) loop
11615 if Node (Iface_Elmt) = Iface then
11616 return True;
11617 end if;
11619 Next_Elmt (Iface_Elmt);
11620 end loop;
11621 end if;
11623 return False;
11624 end Contain_Interface;
11626 ---------------------------
11627 -- Convert_Scalar_Bounds --
11628 ---------------------------
11630 procedure Convert_Scalar_Bounds
11631 (N : Node_Id;
11632 Parent_Type : Entity_Id;
11633 Derived_Type : Entity_Id;
11634 Loc : Source_Ptr)
11636 Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
11638 Lo : Node_Id;
11639 Hi : Node_Id;
11640 Rng : Node_Id;
11642 begin
11643 -- Defend against previous errors
11645 if No (Scalar_Range (Derived_Type)) then
11646 return;
11647 end if;
11649 Lo := Build_Scalar_Bound
11650 (Type_Low_Bound (Derived_Type),
11651 Parent_Type, Implicit_Base);
11653 Hi := Build_Scalar_Bound
11654 (Type_High_Bound (Derived_Type),
11655 Parent_Type, Implicit_Base);
11657 Rng :=
11658 Make_Range (Loc,
11659 Low_Bound => Lo,
11660 High_Bound => Hi);
11662 Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
11664 Set_Parent (Rng, N);
11665 Set_Scalar_Range (Derived_Type, Rng);
11667 -- Analyze the bounds
11669 Analyze_And_Resolve (Lo, Implicit_Base);
11670 Analyze_And_Resolve (Hi, Implicit_Base);
11672 -- Analyze the range itself, except that we do not analyze it if
11673 -- the bounds are real literals, and we have a fixed-point type.
11674 -- The reason for this is that we delay setting the bounds in this
11675 -- case till we know the final Small and Size values (see circuit
11676 -- in Freeze.Freeze_Fixed_Point_Type for further details).
11678 if Is_Fixed_Point_Type (Parent_Type)
11679 and then Nkind (Lo) = N_Real_Literal
11680 and then Nkind (Hi) = N_Real_Literal
11681 then
11682 return;
11684 -- Here we do the analysis of the range
11686 -- Note: we do this manually, since if we do a normal Analyze and
11687 -- Resolve call, there are problems with the conversions used for
11688 -- the derived type range.
11690 else
11691 Set_Etype (Rng, Implicit_Base);
11692 Set_Analyzed (Rng, True);
11693 end if;
11694 end Convert_Scalar_Bounds;
11696 -------------------
11697 -- Copy_And_Swap --
11698 -------------------
11700 procedure Copy_And_Swap (Priv, Full : Entity_Id) is
11701 begin
11702 -- Initialize new full declaration entity by copying the pertinent
11703 -- fields of the corresponding private declaration entity.
11705 -- We temporarily set Ekind to a value appropriate for a type to
11706 -- avoid assert failures in Einfo from checking for setting type
11707 -- attributes on something that is not a type. Ekind (Priv) is an
11708 -- appropriate choice, since it allowed the attributes to be set
11709 -- in the first place. This Ekind value will be modified later.
11711 Set_Ekind (Full, Ekind (Priv));
11713 -- Also set Etype temporarily to Any_Type, again, in the absence
11714 -- of errors, it will be properly reset, and if there are errors,
11715 -- then we want a value of Any_Type to remain.
11717 Set_Etype (Full, Any_Type);
11719 -- Now start copying attributes
11721 Set_Has_Discriminants (Full, Has_Discriminants (Priv));
11723 if Has_Discriminants (Full) then
11724 Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
11725 Set_Stored_Constraint (Full, Stored_Constraint (Priv));
11726 end if;
11728 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
11729 Set_Homonym (Full, Homonym (Priv));
11730 Set_Is_Immediately_Visible (Full, Is_Immediately_Visible (Priv));
11731 Set_Is_Public (Full, Is_Public (Priv));
11732 Set_Is_Pure (Full, Is_Pure (Priv));
11733 Set_Is_Tagged_Type (Full, Is_Tagged_Type (Priv));
11734 Set_Has_Pragma_Unmodified (Full, Has_Pragma_Unmodified (Priv));
11735 Set_Has_Pragma_Unreferenced (Full, Has_Pragma_Unreferenced (Priv));
11736 Set_Has_Pragma_Unreferenced_Objects
11737 (Full, Has_Pragma_Unreferenced_Objects
11738 (Priv));
11740 Conditional_Delay (Full, Priv);
11742 if Is_Tagged_Type (Full) then
11743 Set_Direct_Primitive_Operations (Full,
11744 Direct_Primitive_Operations (Priv));
11746 if Is_Base_Type (Priv) then
11747 Set_Class_Wide_Type (Full, Class_Wide_Type (Priv));
11748 end if;
11749 end if;
11751 Set_Is_Volatile (Full, Is_Volatile (Priv));
11752 Set_Treat_As_Volatile (Full, Treat_As_Volatile (Priv));
11753 Set_Scope (Full, Scope (Priv));
11754 Set_Next_Entity (Full, Next_Entity (Priv));
11755 Set_First_Entity (Full, First_Entity (Priv));
11756 Set_Last_Entity (Full, Last_Entity (Priv));
11758 -- If access types have been recorded for later handling, keep them in
11759 -- the full view so that they get handled when the full view freeze
11760 -- node is expanded.
11762 if Present (Freeze_Node (Priv))
11763 and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
11764 then
11765 Ensure_Freeze_Node (Full);
11766 Set_Access_Types_To_Process
11767 (Freeze_Node (Full),
11768 Access_Types_To_Process (Freeze_Node (Priv)));
11769 end if;
11771 -- Swap the two entities. Now Privat is the full type entity and Full is
11772 -- the private one. They will be swapped back at the end of the private
11773 -- part. This swapping ensures that the entity that is visible in the
11774 -- private part is the full declaration.
11776 Exchange_Entities (Priv, Full);
11777 Append_Entity (Full, Scope (Full));
11778 end Copy_And_Swap;
11780 -------------------------------------
11781 -- Copy_Array_Base_Type_Attributes --
11782 -------------------------------------
11784 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
11785 begin
11786 Set_Component_Alignment (T1, Component_Alignment (T2));
11787 Set_Component_Type (T1, Component_Type (T2));
11788 Set_Component_Size (T1, Component_Size (T2));
11789 Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
11790 Set_Finalize_Storage_Only (T1, Finalize_Storage_Only (T2));
11791 Set_Has_Non_Standard_Rep (T1, Has_Non_Standard_Rep (T2));
11792 Set_Has_Task (T1, Has_Task (T2));
11793 Set_Is_Packed (T1, Is_Packed (T2));
11794 Set_Has_Aliased_Components (T1, Has_Aliased_Components (T2));
11795 Set_Has_Atomic_Components (T1, Has_Atomic_Components (T2));
11796 Set_Has_Volatile_Components (T1, Has_Volatile_Components (T2));
11797 end Copy_Array_Base_Type_Attributes;
11799 -----------------------------------
11800 -- Copy_Array_Subtype_Attributes --
11801 -----------------------------------
11803 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
11804 begin
11805 Set_Size_Info (T1, T2);
11807 Set_First_Index (T1, First_Index (T2));
11808 Set_Is_Aliased (T1, Is_Aliased (T2));
11809 Set_Is_Atomic (T1, Is_Atomic (T2));
11810 Set_Is_Volatile (T1, Is_Volatile (T2));
11811 Set_Treat_As_Volatile (T1, Treat_As_Volatile (T2));
11812 Set_Is_Constrained (T1, Is_Constrained (T2));
11813 Set_Depends_On_Private (T1, Has_Private_Component (T2));
11814 Set_First_Rep_Item (T1, First_Rep_Item (T2));
11815 Set_Convention (T1, Convention (T2));
11816 Set_Is_Limited_Composite (T1, Is_Limited_Composite (T2));
11817 Set_Is_Private_Composite (T1, Is_Private_Composite (T2));
11818 Set_Packed_Array_Type (T1, Packed_Array_Type (T2));
11819 end Copy_Array_Subtype_Attributes;
11821 -----------------------------------
11822 -- Create_Constrained_Components --
11823 -----------------------------------
11825 procedure Create_Constrained_Components
11826 (Subt : Entity_Id;
11827 Decl_Node : Node_Id;
11828 Typ : Entity_Id;
11829 Constraints : Elist_Id)
11831 Loc : constant Source_Ptr := Sloc (Subt);
11832 Comp_List : constant Elist_Id := New_Elmt_List;
11833 Parent_Type : constant Entity_Id := Etype (Typ);
11834 Assoc_List : constant List_Id := New_List;
11835 Discr_Val : Elmt_Id;
11836 Errors : Boolean;
11837 New_C : Entity_Id;
11838 Old_C : Entity_Id;
11839 Is_Static : Boolean := True;
11841 procedure Collect_Fixed_Components (Typ : Entity_Id);
11842 -- Collect parent type components that do not appear in a variant part
11844 procedure Create_All_Components;
11845 -- Iterate over Comp_List to create the components of the subtype
11847 function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
11848 -- Creates a new component from Old_Compon, copying all the fields from
11849 -- it, including its Etype, inserts the new component in the Subt entity
11850 -- chain and returns the new component.
11852 function Is_Variant_Record (T : Entity_Id) return Boolean;
11853 -- If true, and discriminants are static, collect only components from
11854 -- variants selected by discriminant values.
11856 ------------------------------
11857 -- Collect_Fixed_Components --
11858 ------------------------------
11860 procedure Collect_Fixed_Components (Typ : Entity_Id) is
11861 begin
11862 -- Build association list for discriminants, and find components of the
11863 -- variant part selected by the values of the discriminants.
11865 Old_C := First_Discriminant (Typ);
11866 Discr_Val := First_Elmt (Constraints);
11867 while Present (Old_C) loop
11868 Append_To (Assoc_List,
11869 Make_Component_Association (Loc,
11870 Choices => New_List (New_Occurrence_Of (Old_C, Loc)),
11871 Expression => New_Copy (Node (Discr_Val))));
11873 Next_Elmt (Discr_Val);
11874 Next_Discriminant (Old_C);
11875 end loop;
11877 -- The tag, and the possible parent and controller components
11878 -- are unconditionally in the subtype.
11880 if Is_Tagged_Type (Typ)
11881 or else Has_Controlled_Component (Typ)
11882 then
11883 Old_C := First_Component (Typ);
11884 while Present (Old_C) loop
11885 if Chars ((Old_C)) = Name_uTag
11886 or else Chars ((Old_C)) = Name_uParent
11887 or else Chars ((Old_C)) = Name_uController
11888 then
11889 Append_Elmt (Old_C, Comp_List);
11890 end if;
11892 Next_Component (Old_C);
11893 end loop;
11894 end if;
11895 end Collect_Fixed_Components;
11897 ---------------------------
11898 -- Create_All_Components --
11899 ---------------------------
11901 procedure Create_All_Components is
11902 Comp : Elmt_Id;
11904 begin
11905 Comp := First_Elmt (Comp_List);
11906 while Present (Comp) loop
11907 Old_C := Node (Comp);
11908 New_C := Create_Component (Old_C);
11910 Set_Etype
11911 (New_C,
11912 Constrain_Component_Type
11913 (Old_C, Subt, Decl_Node, Typ, Constraints));
11914 Set_Is_Public (New_C, Is_Public (Subt));
11916 Next_Elmt (Comp);
11917 end loop;
11918 end Create_All_Components;
11920 ----------------------
11921 -- Create_Component --
11922 ----------------------
11924 function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
11925 New_Compon : constant Entity_Id := New_Copy (Old_Compon);
11927 begin
11928 if Ekind (Old_Compon) = E_Discriminant
11929 and then Is_Completely_Hidden (Old_Compon)
11930 then
11931 -- This is a shadow discriminant created for a discriminant of
11932 -- the parent type, which needs to be present in the subtype.
11933 -- Give the shadow discriminant an internal name that cannot
11934 -- conflict with that of visible components.
11936 Set_Chars (New_Compon, New_Internal_Name ('C'));
11937 end if;
11939 -- Set the parent so we have a proper link for freezing etc. This is
11940 -- not a real parent pointer, since of course our parent does not own
11941 -- up to us and reference us, we are an illegitimate child of the
11942 -- original parent!
11944 Set_Parent (New_Compon, Parent (Old_Compon));
11946 -- If the old component's Esize was already determined and is a
11947 -- static value, then the new component simply inherits it. Otherwise
11948 -- the old component's size may require run-time determination, but
11949 -- the new component's size still might be statically determinable
11950 -- (if, for example it has a static constraint). In that case we want
11951 -- Layout_Type to recompute the component's size, so we reset its
11952 -- size and positional fields.
11954 if Frontend_Layout_On_Target
11955 and then not Known_Static_Esize (Old_Compon)
11956 then
11957 Set_Esize (New_Compon, Uint_0);
11958 Init_Normalized_First_Bit (New_Compon);
11959 Init_Normalized_Position (New_Compon);
11960 Init_Normalized_Position_Max (New_Compon);
11961 end if;
11963 -- We do not want this node marked as Comes_From_Source, since
11964 -- otherwise it would get first class status and a separate cross-
11965 -- reference line would be generated. Illegitimate children do not
11966 -- rate such recognition.
11968 Set_Comes_From_Source (New_Compon, False);
11970 -- But it is a real entity, and a birth certificate must be properly
11971 -- registered by entering it into the entity list.
11973 Enter_Name (New_Compon);
11975 return New_Compon;
11976 end Create_Component;
11978 -----------------------
11979 -- Is_Variant_Record --
11980 -----------------------
11982 function Is_Variant_Record (T : Entity_Id) return Boolean is
11983 begin
11984 return Nkind (Parent (T)) = N_Full_Type_Declaration
11985 and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
11986 and then Present (Component_List (Type_Definition (Parent (T))))
11987 and then
11988 Present
11989 (Variant_Part (Component_List (Type_Definition (Parent (T)))));
11990 end Is_Variant_Record;
11992 -- Start of processing for Create_Constrained_Components
11994 begin
11995 pragma Assert (Subt /= Base_Type (Subt));
11996 pragma Assert (Typ = Base_Type (Typ));
11998 Set_First_Entity (Subt, Empty);
11999 Set_Last_Entity (Subt, Empty);
12001 -- Check whether constraint is fully static, in which case we can
12002 -- optimize the list of components.
12004 Discr_Val := First_Elmt (Constraints);
12005 while Present (Discr_Val) loop
12006 if not Is_OK_Static_Expression (Node (Discr_Val)) then
12007 Is_Static := False;
12008 exit;
12009 end if;
12011 Next_Elmt (Discr_Val);
12012 end loop;
12014 Set_Has_Static_Discriminants (Subt, Is_Static);
12016 Push_Scope (Subt);
12018 -- Inherit the discriminants of the parent type
12020 Add_Discriminants : declare
12021 Num_Disc : Int;
12022 Num_Gird : Int;
12024 begin
12025 Num_Disc := 0;
12026 Old_C := First_Discriminant (Typ);
12028 while Present (Old_C) loop
12029 Num_Disc := Num_Disc + 1;
12030 New_C := Create_Component (Old_C);
12031 Set_Is_Public (New_C, Is_Public (Subt));
12032 Next_Discriminant (Old_C);
12033 end loop;
12035 -- For an untagged derived subtype, the number of discriminants may
12036 -- be smaller than the number of inherited discriminants, because
12037 -- several of them may be renamed by a single new discriminant or
12038 -- constrained. In this case, add the hidden discriminants back into
12039 -- the subtype, because they need to be present if the optimizer of
12040 -- the GCC 4.x back-end decides to break apart assignments between
12041 -- objects using the parent view into member-wise assignments.
12043 Num_Gird := 0;
12045 if Is_Derived_Type (Typ)
12046 and then not Is_Tagged_Type (Typ)
12047 then
12048 Old_C := First_Stored_Discriminant (Typ);
12050 while Present (Old_C) loop
12051 Num_Gird := Num_Gird + 1;
12052 Next_Stored_Discriminant (Old_C);
12053 end loop;
12054 end if;
12056 if Num_Gird > Num_Disc then
12058 -- Find out multiple uses of new discriminants, and add hidden
12059 -- components for the extra renamed discriminants. We recognize
12060 -- multiple uses through the Corresponding_Discriminant of a
12061 -- new discriminant: if it constrains several old discriminants,
12062 -- this field points to the last one in the parent type. The
12063 -- stored discriminants of the derived type have the same name
12064 -- as those of the parent.
12066 declare
12067 Constr : Elmt_Id;
12068 New_Discr : Entity_Id;
12069 Old_Discr : Entity_Id;
12071 begin
12072 Constr := First_Elmt (Stored_Constraint (Typ));
12073 Old_Discr := First_Stored_Discriminant (Typ);
12074 while Present (Constr) loop
12075 if Is_Entity_Name (Node (Constr))
12076 and then Ekind (Entity (Node (Constr))) = E_Discriminant
12077 then
12078 New_Discr := Entity (Node (Constr));
12080 if Chars (Corresponding_Discriminant (New_Discr)) /=
12081 Chars (Old_Discr)
12082 then
12083 -- The new discriminant has been used to rename a
12084 -- subsequent old discriminant. Introduce a shadow
12085 -- component for the current old discriminant.
12087 New_C := Create_Component (Old_Discr);
12088 Set_Original_Record_Component (New_C, Old_Discr);
12089 end if;
12091 else
12092 -- The constraint has eliminated the old discriminant.
12093 -- Introduce a shadow component.
12095 New_C := Create_Component (Old_Discr);
12096 Set_Original_Record_Component (New_C, Old_Discr);
12097 end if;
12099 Next_Elmt (Constr);
12100 Next_Stored_Discriminant (Old_Discr);
12101 end loop;
12102 end;
12103 end if;
12104 end Add_Discriminants;
12106 if Is_Static
12107 and then Is_Variant_Record (Typ)
12108 then
12109 Collect_Fixed_Components (Typ);
12111 Gather_Components (
12112 Typ,
12113 Component_List (Type_Definition (Parent (Typ))),
12114 Governed_By => Assoc_List,
12115 Into => Comp_List,
12116 Report_Errors => Errors);
12117 pragma Assert (not Errors);
12119 Create_All_Components;
12121 -- If the subtype declaration is created for a tagged type derivation
12122 -- with constraints, we retrieve the record definition of the parent
12123 -- type to select the components of the proper variant.
12125 elsif Is_Static
12126 and then Is_Tagged_Type (Typ)
12127 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
12128 and then
12129 Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
12130 and then Is_Variant_Record (Parent_Type)
12131 then
12132 Collect_Fixed_Components (Typ);
12134 Gather_Components (
12135 Typ,
12136 Component_List (Type_Definition (Parent (Parent_Type))),
12137 Governed_By => Assoc_List,
12138 Into => Comp_List,
12139 Report_Errors => Errors);
12140 pragma Assert (not Errors);
12142 -- If the tagged derivation has a type extension, collect all the
12143 -- new components therein.
12145 if Present
12146 (Record_Extension_Part (Type_Definition (Parent (Typ))))
12147 then
12148 Old_C := First_Component (Typ);
12149 while Present (Old_C) loop
12150 if Original_Record_Component (Old_C) = Old_C
12151 and then Chars (Old_C) /= Name_uTag
12152 and then Chars (Old_C) /= Name_uParent
12153 and then Chars (Old_C) /= Name_uController
12154 then
12155 Append_Elmt (Old_C, Comp_List);
12156 end if;
12158 Next_Component (Old_C);
12159 end loop;
12160 end if;
12162 Create_All_Components;
12164 else
12165 -- If discriminants are not static, or if this is a multi-level type
12166 -- extension, we have to include all components of the parent type.
12168 Old_C := First_Component (Typ);
12169 while Present (Old_C) loop
12170 New_C := Create_Component (Old_C);
12172 Set_Etype
12173 (New_C,
12174 Constrain_Component_Type
12175 (Old_C, Subt, Decl_Node, Typ, Constraints));
12176 Set_Is_Public (New_C, Is_Public (Subt));
12178 Next_Component (Old_C);
12179 end loop;
12180 end if;
12182 End_Scope;
12183 end Create_Constrained_Components;
12185 ------------------------------------------
12186 -- Decimal_Fixed_Point_Type_Declaration --
12187 ------------------------------------------
12189 procedure Decimal_Fixed_Point_Type_Declaration
12190 (T : Entity_Id;
12191 Def : Node_Id)
12193 Loc : constant Source_Ptr := Sloc (Def);
12194 Digs_Expr : constant Node_Id := Digits_Expression (Def);
12195 Delta_Expr : constant Node_Id := Delta_Expression (Def);
12196 Implicit_Base : Entity_Id;
12197 Digs_Val : Uint;
12198 Delta_Val : Ureal;
12199 Scale_Val : Uint;
12200 Bound_Val : Ureal;
12202 begin
12203 Check_Restriction (No_Fixed_Point, Def);
12205 -- Create implicit base type
12207 Implicit_Base :=
12208 Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
12209 Set_Etype (Implicit_Base, Implicit_Base);
12211 -- Analyze and process delta expression
12213 Analyze_And_Resolve (Delta_Expr, Universal_Real);
12215 Check_Delta_Expression (Delta_Expr);
12216 Delta_Val := Expr_Value_R (Delta_Expr);
12218 -- Check delta is power of 10, and determine scale value from it
12220 declare
12221 Val : Ureal;
12223 begin
12224 Scale_Val := Uint_0;
12225 Val := Delta_Val;
12227 if Val < Ureal_1 then
12228 while Val < Ureal_1 loop
12229 Val := Val * Ureal_10;
12230 Scale_Val := Scale_Val + 1;
12231 end loop;
12233 if Scale_Val > 18 then
12234 Error_Msg_N ("scale exceeds maximum value of 18", Def);
12235 Scale_Val := UI_From_Int (+18);
12236 end if;
12238 else
12239 while Val > Ureal_1 loop
12240 Val := Val / Ureal_10;
12241 Scale_Val := Scale_Val - 1;
12242 end loop;
12244 if Scale_Val < -18 then
12245 Error_Msg_N ("scale is less than minimum value of -18", Def);
12246 Scale_Val := UI_From_Int (-18);
12247 end if;
12248 end if;
12250 if Val /= Ureal_1 then
12251 Error_Msg_N ("delta expression must be a power of 10", Def);
12252 Delta_Val := Ureal_10 ** (-Scale_Val);
12253 end if;
12254 end;
12256 -- Set delta, scale and small (small = delta for decimal type)
12258 Set_Delta_Value (Implicit_Base, Delta_Val);
12259 Set_Scale_Value (Implicit_Base, Scale_Val);
12260 Set_Small_Value (Implicit_Base, Delta_Val);
12262 -- Analyze and process digits expression
12264 Analyze_And_Resolve (Digs_Expr, Any_Integer);
12265 Check_Digits_Expression (Digs_Expr);
12266 Digs_Val := Expr_Value (Digs_Expr);
12268 if Digs_Val > 18 then
12269 Digs_Val := UI_From_Int (+18);
12270 Error_Msg_N ("digits value out of range, maximum is 18", Digs_Expr);
12271 end if;
12273 Set_Digits_Value (Implicit_Base, Digs_Val);
12274 Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
12276 -- Set range of base type from digits value for now. This will be
12277 -- expanded to represent the true underlying base range by Freeze.
12279 Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
12281 -- Note: We leave size as zero for now, size will be set at freeze
12282 -- time. We have to do this for ordinary fixed-point, because the size
12283 -- depends on the specified small, and we might as well do the same for
12284 -- decimal fixed-point.
12286 pragma Assert (Esize (Implicit_Base) = Uint_0);
12288 -- If there are bounds given in the declaration use them as the
12289 -- bounds of the first named subtype.
12291 if Present (Real_Range_Specification (Def)) then
12292 declare
12293 RRS : constant Node_Id := Real_Range_Specification (Def);
12294 Low : constant Node_Id := Low_Bound (RRS);
12295 High : constant Node_Id := High_Bound (RRS);
12296 Low_Val : Ureal;
12297 High_Val : Ureal;
12299 begin
12300 Analyze_And_Resolve (Low, Any_Real);
12301 Analyze_And_Resolve (High, Any_Real);
12302 Check_Real_Bound (Low);
12303 Check_Real_Bound (High);
12304 Low_Val := Expr_Value_R (Low);
12305 High_Val := Expr_Value_R (High);
12307 if Low_Val < (-Bound_Val) then
12308 Error_Msg_N
12309 ("range low bound too small for digits value", Low);
12310 Low_Val := -Bound_Val;
12311 end if;
12313 if High_Val > Bound_Val then
12314 Error_Msg_N
12315 ("range high bound too large for digits value", High);
12316 High_Val := Bound_Val;
12317 end if;
12319 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
12320 end;
12322 -- If no explicit range, use range that corresponds to given
12323 -- digits value. This will end up as the final range for the
12324 -- first subtype.
12326 else
12327 Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
12328 end if;
12330 -- Complete entity for first subtype
12332 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
12333 Set_Etype (T, Implicit_Base);
12334 Set_Size_Info (T, Implicit_Base);
12335 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
12336 Set_Digits_Value (T, Digs_Val);
12337 Set_Delta_Value (T, Delta_Val);
12338 Set_Small_Value (T, Delta_Val);
12339 Set_Scale_Value (T, Scale_Val);
12340 Set_Is_Constrained (T);
12341 end Decimal_Fixed_Point_Type_Declaration;
12343 -----------------------------------
12344 -- Derive_Progenitor_Subprograms --
12345 -----------------------------------
12347 procedure Derive_Progenitor_Subprograms
12348 (Parent_Type : Entity_Id;
12349 Tagged_Type : Entity_Id)
12351 E : Entity_Id;
12352 Elmt : Elmt_Id;
12353 Iface : Entity_Id;
12354 Iface_Elmt : Elmt_Id;
12355 Iface_Subp : Entity_Id;
12356 New_Subp : Entity_Id := Empty;
12357 Prim_Elmt : Elmt_Id;
12358 Subp : Entity_Id;
12359 Typ : Entity_Id;
12361 begin
12362 pragma Assert (Ada_Version >= Ada_2005
12363 and then Is_Record_Type (Tagged_Type)
12364 and then Is_Tagged_Type (Tagged_Type)
12365 and then Has_Interfaces (Tagged_Type));
12367 -- Step 1: Transfer to the full-view primitives associated with the
12368 -- partial-view that cover interface primitives. Conceptually this
12369 -- work should be done later by Process_Full_View; done here to
12370 -- simplify its implementation at later stages. It can be safely
12371 -- done here because interfaces must be visible in the partial and
12372 -- private view (RM 7.3(7.3/2)).
12374 -- Small optimization: This work is only required if the parent is
12375 -- abstract. If the tagged type is not abstract, it cannot have
12376 -- abstract primitives (the only entities in the list of primitives of
12377 -- non-abstract tagged types that can reference abstract primitives
12378 -- through its Alias attribute are the internal entities that have
12379 -- attribute Interface_Alias, and these entities are generated later
12380 -- by Add_Internal_Interface_Entities).
12382 if In_Private_Part (Current_Scope)
12383 and then Is_Abstract_Type (Parent_Type)
12384 then
12385 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
12386 while Present (Elmt) loop
12387 Subp := Node (Elmt);
12389 -- At this stage it is not possible to have entities in the list
12390 -- of primitives that have attribute Interface_Alias
12392 pragma Assert (No (Interface_Alias (Subp)));
12394 Typ := Find_Dispatching_Type (Ultimate_Alias (Subp));
12396 if Is_Interface (Typ) then
12397 E := Find_Primitive_Covering_Interface
12398 (Tagged_Type => Tagged_Type,
12399 Iface_Prim => Subp);
12401 if Present (E)
12402 and then Find_Dispatching_Type (Ultimate_Alias (E)) /= Typ
12403 then
12404 Replace_Elmt (Elmt, E);
12405 Remove_Homonym (Subp);
12406 end if;
12407 end if;
12409 Next_Elmt (Elmt);
12410 end loop;
12411 end if;
12413 -- Step 2: Add primitives of progenitors that are not implemented by
12414 -- parents of Tagged_Type
12416 if Present (Interfaces (Base_Type (Tagged_Type))) then
12417 Iface_Elmt := First_Elmt (Interfaces (Base_Type (Tagged_Type)));
12418 while Present (Iface_Elmt) loop
12419 Iface := Node (Iface_Elmt);
12421 Prim_Elmt := First_Elmt (Primitive_Operations (Iface));
12422 while Present (Prim_Elmt) loop
12423 Iface_Subp := Node (Prim_Elmt);
12425 -- Exclude derivation of predefined primitives except those
12426 -- that come from source. Required to catch declarations of
12427 -- equality operators of interfaces. For example:
12429 -- type Iface is interface;
12430 -- function "=" (Left, Right : Iface) return Boolean;
12432 if not Is_Predefined_Dispatching_Operation (Iface_Subp)
12433 or else Comes_From_Source (Iface_Subp)
12434 then
12435 E := Find_Primitive_Covering_Interface
12436 (Tagged_Type => Tagged_Type,
12437 Iface_Prim => Iface_Subp);
12439 -- If not found we derive a new primitive leaving its alias
12440 -- attribute referencing the interface primitive
12442 if No (E) then
12443 Derive_Subprogram
12444 (New_Subp, Iface_Subp, Tagged_Type, Iface);
12446 -- Ada 2012 (AI05-0197): If the covering primitive's name
12447 -- differs from the name of the interface primitive then it
12448 -- is a private primitive inherited from a parent type. In
12449 -- such case, given that Tagged_Type covers the interface,
12450 -- the inherited private primitive becomes visible. For such
12451 -- purpose we add a new entity that renames the inherited
12452 -- private primitive.
12454 elsif Chars (E) /= Chars (Iface_Subp) then
12455 pragma Assert (Has_Suffix (E, 'P'));
12456 Derive_Subprogram
12457 (New_Subp, Iface_Subp, Tagged_Type, Iface);
12458 Set_Alias (New_Subp, E);
12459 Set_Is_Abstract_Subprogram (New_Subp,
12460 Is_Abstract_Subprogram (E));
12462 -- Propagate to the full view interface entities associated
12463 -- with the partial view
12465 elsif In_Private_Part (Current_Scope)
12466 and then Present (Alias (E))
12467 and then Alias (E) = Iface_Subp
12468 and then
12469 List_Containing (Parent (E)) /=
12470 Private_Declarations
12471 (Specification
12472 (Unit_Declaration_Node (Current_Scope)))
12473 then
12474 Append_Elmt (E, Primitive_Operations (Tagged_Type));
12475 end if;
12476 end if;
12478 Next_Elmt (Prim_Elmt);
12479 end loop;
12481 Next_Elmt (Iface_Elmt);
12482 end loop;
12483 end if;
12484 end Derive_Progenitor_Subprograms;
12486 -----------------------
12487 -- Derive_Subprogram --
12488 -----------------------
12490 procedure Derive_Subprogram
12491 (New_Subp : in out Entity_Id;
12492 Parent_Subp : Entity_Id;
12493 Derived_Type : Entity_Id;
12494 Parent_Type : Entity_Id;
12495 Actual_Subp : Entity_Id := Empty)
12497 Formal : Entity_Id;
12498 -- Formal parameter of parent primitive operation
12500 Formal_Of_Actual : Entity_Id;
12501 -- Formal parameter of actual operation, when the derivation is to
12502 -- create a renaming for a primitive operation of an actual in an
12503 -- instantiation.
12505 New_Formal : Entity_Id;
12506 -- Formal of inherited operation
12508 Visible_Subp : Entity_Id := Parent_Subp;
12510 function Is_Private_Overriding return Boolean;
12511 -- If Subp is a private overriding of a visible operation, the inherited
12512 -- operation derives from the overridden op (even though its body is the
12513 -- overriding one) and the inherited operation is visible now. See
12514 -- sem_disp to see the full details of the handling of the overridden
12515 -- subprogram, which is removed from the list of primitive operations of
12516 -- the type. The overridden subprogram is saved locally in Visible_Subp,
12517 -- and used to diagnose abstract operations that need overriding in the
12518 -- derived type.
12520 procedure Replace_Type (Id, New_Id : Entity_Id);
12521 -- When the type is an anonymous access type, create a new access type
12522 -- designating the derived type.
12524 procedure Set_Derived_Name;
12525 -- This procedure sets the appropriate Chars name for New_Subp. This
12526 -- is normally just a copy of the parent name. An exception arises for
12527 -- type support subprograms, where the name is changed to reflect the
12528 -- name of the derived type, e.g. if type foo is derived from type bar,
12529 -- then a procedure barDA is derived with a name fooDA.
12531 ---------------------------
12532 -- Is_Private_Overriding --
12533 ---------------------------
12535 function Is_Private_Overriding return Boolean is
12536 Prev : Entity_Id;
12538 begin
12539 -- If the parent is not a dispatching operation there is no
12540 -- need to investigate overridings
12542 if not Is_Dispatching_Operation (Parent_Subp) then
12543 return False;
12544 end if;
12546 -- The visible operation that is overridden is a homonym of the
12547 -- parent subprogram. We scan the homonym chain to find the one
12548 -- whose alias is the subprogram we are deriving.
12550 Prev := Current_Entity (Parent_Subp);
12551 while Present (Prev) loop
12552 if Ekind (Prev) = Ekind (Parent_Subp)
12553 and then Alias (Prev) = Parent_Subp
12554 and then Scope (Parent_Subp) = Scope (Prev)
12555 and then not Is_Hidden (Prev)
12556 then
12557 Visible_Subp := Prev;
12558 return True;
12559 end if;
12561 Prev := Homonym (Prev);
12562 end loop;
12564 return False;
12565 end Is_Private_Overriding;
12567 ------------------
12568 -- Replace_Type --
12569 ------------------
12571 procedure Replace_Type (Id, New_Id : Entity_Id) is
12572 Acc_Type : Entity_Id;
12573 Par : constant Node_Id := Parent (Derived_Type);
12575 begin
12576 -- When the type is an anonymous access type, create a new access
12577 -- type designating the derived type. This itype must be elaborated
12578 -- at the point of the derivation, not on subsequent calls that may
12579 -- be out of the proper scope for Gigi, so we insert a reference to
12580 -- it after the derivation.
12582 if Ekind (Etype (Id)) = E_Anonymous_Access_Type then
12583 declare
12584 Desig_Typ : Entity_Id := Designated_Type (Etype (Id));
12586 begin
12587 if Ekind (Desig_Typ) = E_Record_Type_With_Private
12588 and then Present (Full_View (Desig_Typ))
12589 and then not Is_Private_Type (Parent_Type)
12590 then
12591 Desig_Typ := Full_View (Desig_Typ);
12592 end if;
12594 if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
12596 -- Ada 2005 (AI-251): Handle also derivations of abstract
12597 -- interface primitives.
12599 or else (Is_Interface (Desig_Typ)
12600 and then not Is_Class_Wide_Type (Desig_Typ))
12601 then
12602 Acc_Type := New_Copy (Etype (Id));
12603 Set_Etype (Acc_Type, Acc_Type);
12604 Set_Scope (Acc_Type, New_Subp);
12606 -- Compute size of anonymous access type
12608 if Is_Array_Type (Desig_Typ)
12609 and then not Is_Constrained (Desig_Typ)
12610 then
12611 Init_Size (Acc_Type, 2 * System_Address_Size);
12612 else
12613 Init_Size (Acc_Type, System_Address_Size);
12614 end if;
12616 Init_Alignment (Acc_Type);
12617 Set_Directly_Designated_Type (Acc_Type, Derived_Type);
12619 Set_Etype (New_Id, Acc_Type);
12620 Set_Scope (New_Id, New_Subp);
12622 -- Create a reference to it
12623 Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
12625 else
12626 Set_Etype (New_Id, Etype (Id));
12627 end if;
12628 end;
12630 elsif Base_Type (Etype (Id)) = Base_Type (Parent_Type)
12631 or else
12632 (Ekind (Etype (Id)) = E_Record_Type_With_Private
12633 and then Present (Full_View (Etype (Id)))
12634 and then
12635 Base_Type (Full_View (Etype (Id))) = Base_Type (Parent_Type))
12636 then
12637 -- Constraint checks on formals are generated during expansion,
12638 -- based on the signature of the original subprogram. The bounds
12639 -- of the derived type are not relevant, and thus we can use
12640 -- the base type for the formals. However, the return type may be
12641 -- used in a context that requires that the proper static bounds
12642 -- be used (a case statement, for example) and for those cases
12643 -- we must use the derived type (first subtype), not its base.
12645 -- If the derived_type_definition has no constraints, we know that
12646 -- the derived type has the same constraints as the first subtype
12647 -- of the parent, and we can also use it rather than its base,
12648 -- which can lead to more efficient code.
12650 if Etype (Id) = Parent_Type then
12651 if Is_Scalar_Type (Parent_Type)
12652 and then
12653 Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
12654 then
12655 Set_Etype (New_Id, Derived_Type);
12657 elsif Nkind (Par) = N_Full_Type_Declaration
12658 and then
12659 Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
12660 and then
12661 Is_Entity_Name
12662 (Subtype_Indication (Type_Definition (Par)))
12663 then
12664 Set_Etype (New_Id, Derived_Type);
12666 else
12667 Set_Etype (New_Id, Base_Type (Derived_Type));
12668 end if;
12670 else
12671 Set_Etype (New_Id, Base_Type (Derived_Type));
12672 end if;
12674 else
12675 Set_Etype (New_Id, Etype (Id));
12676 end if;
12677 end Replace_Type;
12679 ----------------------
12680 -- Set_Derived_Name --
12681 ----------------------
12683 procedure Set_Derived_Name is
12684 Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
12685 begin
12686 if Nm = TSS_Null then
12687 Set_Chars (New_Subp, Chars (Parent_Subp));
12688 else
12689 Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
12690 end if;
12691 end Set_Derived_Name;
12693 -- Start of processing for Derive_Subprogram
12695 begin
12696 New_Subp :=
12697 New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
12698 Set_Ekind (New_Subp, Ekind (Parent_Subp));
12700 -- Check whether the inherited subprogram is a private operation that
12701 -- should be inherited but not yet made visible. Such subprograms can
12702 -- become visible at a later point (e.g., the private part of a public
12703 -- child unit) via Declare_Inherited_Private_Subprograms. If the
12704 -- following predicate is true, then this is not such a private
12705 -- operation and the subprogram simply inherits the name of the parent
12706 -- subprogram. Note the special check for the names of controlled
12707 -- operations, which are currently exempted from being inherited with
12708 -- a hidden name because they must be findable for generation of
12709 -- implicit run-time calls.
12711 if not Is_Hidden (Parent_Subp)
12712 or else Is_Internal (Parent_Subp)
12713 or else Is_Private_Overriding
12714 or else Is_Internal_Name (Chars (Parent_Subp))
12715 or else Chars (Parent_Subp) = Name_Initialize
12716 or else Chars (Parent_Subp) = Name_Adjust
12717 or else Chars (Parent_Subp) = Name_Finalize
12718 then
12719 Set_Derived_Name;
12721 -- An inherited dispatching equality will be overridden by an internally
12722 -- generated one, or by an explicit one, so preserve its name and thus
12723 -- its entry in the dispatch table. Otherwise, if Parent_Subp is a
12724 -- private operation it may become invisible if the full view has
12725 -- progenitors, and the dispatch table will be malformed.
12726 -- We check that the type is limited to handle the anomalous declaration
12727 -- of Limited_Controlled, which is derived from a non-limited type, and
12728 -- which is handled specially elsewhere as well.
12730 elsif Chars (Parent_Subp) = Name_Op_Eq
12731 and then Is_Dispatching_Operation (Parent_Subp)
12732 and then Etype (Parent_Subp) = Standard_Boolean
12733 and then not Is_Limited_Type (Etype (First_Formal (Parent_Subp)))
12734 and then
12735 Etype (First_Formal (Parent_Subp)) =
12736 Etype (Next_Formal (First_Formal (Parent_Subp)))
12737 then
12738 Set_Derived_Name;
12740 -- If parent is hidden, this can be a regular derivation if the
12741 -- parent is immediately visible in a non-instantiating context,
12742 -- or if we are in the private part of an instance. This test
12743 -- should still be refined ???
12745 -- The test for In_Instance_Not_Visible avoids inheriting the derived
12746 -- operation as a non-visible operation in cases where the parent
12747 -- subprogram might not be visible now, but was visible within the
12748 -- original generic, so it would be wrong to make the inherited
12749 -- subprogram non-visible now. (Not clear if this test is fully
12750 -- correct; are there any cases where we should declare the inherited
12751 -- operation as not visible to avoid it being overridden, e.g., when
12752 -- the parent type is a generic actual with private primitives ???)
12754 -- (they should be treated the same as other private inherited
12755 -- subprograms, but it's not clear how to do this cleanly). ???
12757 elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
12758 and then Is_Immediately_Visible (Parent_Subp)
12759 and then not In_Instance)
12760 or else In_Instance_Not_Visible
12761 then
12762 Set_Derived_Name;
12764 -- Ada 2005 (AI-251): Regular derivation if the parent subprogram
12765 -- overrides an interface primitive because interface primitives
12766 -- must be visible in the partial view of the parent (RM 7.3 (7.3/2))
12768 elsif Ada_Version >= Ada_2005
12769 and then Is_Dispatching_Operation (Parent_Subp)
12770 and then Covers_Some_Interface (Parent_Subp)
12771 then
12772 Set_Derived_Name;
12774 -- Otherwise, the type is inheriting a private operation, so enter
12775 -- it with a special name so it can't be overridden.
12777 else
12778 Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
12779 end if;
12781 Set_Parent (New_Subp, Parent (Derived_Type));
12783 if Present (Actual_Subp) then
12784 Replace_Type (Actual_Subp, New_Subp);
12785 else
12786 Replace_Type (Parent_Subp, New_Subp);
12787 end if;
12789 Conditional_Delay (New_Subp, Parent_Subp);
12791 -- If we are creating a renaming for a primitive operation of an
12792 -- actual of a generic derived type, we must examine the signature
12793 -- of the actual primitive, not that of the generic formal, which for
12794 -- example may be an interface. However the name and initial value
12795 -- of the inherited operation are those of the formal primitive.
12797 Formal := First_Formal (Parent_Subp);
12799 if Present (Actual_Subp) then
12800 Formal_Of_Actual := First_Formal (Actual_Subp);
12801 else
12802 Formal_Of_Actual := Empty;
12803 end if;
12805 while Present (Formal) loop
12806 New_Formal := New_Copy (Formal);
12808 -- Normally we do not go copying parents, but in the case of
12809 -- formals, we need to link up to the declaration (which is the
12810 -- parameter specification), and it is fine to link up to the
12811 -- original formal's parameter specification in this case.
12813 Set_Parent (New_Formal, Parent (Formal));
12814 Append_Entity (New_Formal, New_Subp);
12816 if Present (Formal_Of_Actual) then
12817 Replace_Type (Formal_Of_Actual, New_Formal);
12818 Next_Formal (Formal_Of_Actual);
12819 else
12820 Replace_Type (Formal, New_Formal);
12821 end if;
12823 Next_Formal (Formal);
12824 end loop;
12826 -- If this derivation corresponds to a tagged generic actual, then
12827 -- primitive operations rename those of the actual. Otherwise the
12828 -- primitive operations rename those of the parent type, If the parent
12829 -- renames an intrinsic operator, so does the new subprogram. We except
12830 -- concatenation, which is always properly typed, and does not get
12831 -- expanded as other intrinsic operations.
12833 if No (Actual_Subp) then
12834 if Is_Intrinsic_Subprogram (Parent_Subp) then
12835 Set_Is_Intrinsic_Subprogram (New_Subp);
12837 if Present (Alias (Parent_Subp))
12838 and then Chars (Parent_Subp) /= Name_Op_Concat
12839 then
12840 Set_Alias (New_Subp, Alias (Parent_Subp));
12841 else
12842 Set_Alias (New_Subp, Parent_Subp);
12843 end if;
12845 else
12846 Set_Alias (New_Subp, Parent_Subp);
12847 end if;
12849 else
12850 Set_Alias (New_Subp, Actual_Subp);
12851 end if;
12853 -- Derived subprograms of a tagged type must inherit the convention
12854 -- of the parent subprogram (a requirement of AI-117). Derived
12855 -- subprograms of untagged types simply get convention Ada by default.
12857 if Is_Tagged_Type (Derived_Type) then
12858 Set_Convention (New_Subp, Convention (Parent_Subp));
12859 end if;
12861 -- Predefined controlled operations retain their name even if the parent
12862 -- is hidden (see above), but they are not primitive operations if the
12863 -- ancestor is not visible, for example if the parent is a private
12864 -- extension completed with a controlled extension. Note that a full
12865 -- type that is controlled can break privacy: the flag Is_Controlled is
12866 -- set on both views of the type.
12868 if Is_Controlled (Parent_Type)
12869 and then
12870 (Chars (Parent_Subp) = Name_Initialize
12871 or else Chars (Parent_Subp) = Name_Adjust
12872 or else Chars (Parent_Subp) = Name_Finalize)
12873 and then Is_Hidden (Parent_Subp)
12874 and then not Is_Visibly_Controlled (Parent_Type)
12875 then
12876 Set_Is_Hidden (New_Subp);
12877 end if;
12879 Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
12880 Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
12882 if Ekind (Parent_Subp) = E_Procedure then
12883 Set_Is_Valued_Procedure
12884 (New_Subp, Is_Valued_Procedure (Parent_Subp));
12885 else
12886 Set_Has_Controlling_Result
12887 (New_Subp, Has_Controlling_Result (Parent_Subp));
12888 end if;
12890 -- No_Return must be inherited properly. If this is overridden in the
12891 -- case of a dispatching operation, then a check is made in Sem_Disp
12892 -- that the overriding operation is also No_Return (no such check is
12893 -- required for the case of non-dispatching operation.
12895 Set_No_Return (New_Subp, No_Return (Parent_Subp));
12897 -- A derived function with a controlling result is abstract. If the
12898 -- Derived_Type is a nonabstract formal generic derived type, then
12899 -- inherited operations are not abstract: the required check is done at
12900 -- instantiation time. If the derivation is for a generic actual, the
12901 -- function is not abstract unless the actual is.
12903 if Is_Generic_Type (Derived_Type)
12904 and then not Is_Abstract_Type (Derived_Type)
12905 then
12906 null;
12908 -- Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
12909 -- properties of the subprogram, as defined in RM-3.9.3(4/2-6/2).
12911 elsif Ada_Version >= Ada_2005
12912 and then (Is_Abstract_Subprogram (Alias (New_Subp))
12913 or else (Is_Tagged_Type (Derived_Type)
12914 and then Etype (New_Subp) = Derived_Type
12915 and then not Is_Null_Extension (Derived_Type))
12916 or else (Is_Tagged_Type (Derived_Type)
12917 and then Ekind (Etype (New_Subp)) =
12918 E_Anonymous_Access_Type
12919 and then Designated_Type (Etype (New_Subp)) =
12920 Derived_Type
12921 and then not Is_Null_Extension (Derived_Type)))
12922 and then No (Actual_Subp)
12923 then
12924 if not Is_Tagged_Type (Derived_Type)
12925 or else Is_Abstract_Type (Derived_Type)
12926 or else Is_Abstract_Subprogram (Alias (New_Subp))
12927 then
12928 Set_Is_Abstract_Subprogram (New_Subp);
12929 else
12930 Set_Requires_Overriding (New_Subp);
12931 end if;
12933 elsif Ada_Version < Ada_2005
12934 and then (Is_Abstract_Subprogram (Alias (New_Subp))
12935 or else (Is_Tagged_Type (Derived_Type)
12936 and then Etype (New_Subp) = Derived_Type
12937 and then No (Actual_Subp)))
12938 then
12939 Set_Is_Abstract_Subprogram (New_Subp);
12941 -- AI05-0097 : an inherited operation that dispatches on result is
12942 -- abstract if the derived type is abstract, even if the parent type
12943 -- is concrete and the derived type is a null extension.
12945 elsif Has_Controlling_Result (Alias (New_Subp))
12946 and then Is_Abstract_Type (Etype (New_Subp))
12947 then
12948 Set_Is_Abstract_Subprogram (New_Subp);
12950 -- Finally, if the parent type is abstract we must verify that all
12951 -- inherited operations are either non-abstract or overridden, or that
12952 -- the derived type itself is abstract (this check is performed at the
12953 -- end of a package declaration, in Check_Abstract_Overriding). A
12954 -- private overriding in the parent type will not be visible in the
12955 -- derivation if we are not in an inner package or in a child unit of
12956 -- the parent type, in which case the abstractness of the inherited
12957 -- operation is carried to the new subprogram.
12959 elsif Is_Abstract_Type (Parent_Type)
12960 and then not In_Open_Scopes (Scope (Parent_Type))
12961 and then Is_Private_Overriding
12962 and then Is_Abstract_Subprogram (Visible_Subp)
12963 then
12964 if No (Actual_Subp) then
12965 Set_Alias (New_Subp, Visible_Subp);
12966 Set_Is_Abstract_Subprogram (New_Subp, True);
12968 else
12969 -- If this is a derivation for an instance of a formal derived
12970 -- type, abstractness comes from the primitive operation of the
12971 -- actual, not from the operation inherited from the ancestor.
12973 Set_Is_Abstract_Subprogram
12974 (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
12975 end if;
12976 end if;
12978 New_Overloaded_Entity (New_Subp, Derived_Type);
12980 -- Check for case of a derived subprogram for the instantiation of a
12981 -- formal derived tagged type, if so mark the subprogram as dispatching
12982 -- and inherit the dispatching attributes of the parent subprogram. The
12983 -- derived subprogram is effectively renaming of the actual subprogram,
12984 -- so it needs to have the same attributes as the actual.
12986 if Present (Actual_Subp)
12987 and then Is_Dispatching_Operation (Parent_Subp)
12988 then
12989 Set_Is_Dispatching_Operation (New_Subp);
12991 if Present (DTC_Entity (Parent_Subp)) then
12992 Set_DTC_Entity (New_Subp, DTC_Entity (Parent_Subp));
12993 Set_DT_Position (New_Subp, DT_Position (Parent_Subp));
12994 end if;
12995 end if;
12997 -- Indicate that a derived subprogram does not require a body and that
12998 -- it does not require processing of default expressions.
13000 Set_Has_Completion (New_Subp);
13001 Set_Default_Expressions_Processed (New_Subp);
13003 if Ekind (New_Subp) = E_Function then
13004 Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
13005 end if;
13006 end Derive_Subprogram;
13008 ------------------------
13009 -- Derive_Subprograms --
13010 ------------------------
13012 procedure Derive_Subprograms
13013 (Parent_Type : Entity_Id;
13014 Derived_Type : Entity_Id;
13015 Generic_Actual : Entity_Id := Empty)
13017 Op_List : constant Elist_Id :=
13018 Collect_Primitive_Operations (Parent_Type);
13020 function Check_Derived_Type return Boolean;
13021 -- Check that all the entities derived from Parent_Type are found in
13022 -- the list of primitives of Derived_Type exactly in the same order.
13024 procedure Derive_Interface_Subprogram
13025 (New_Subp : in out Entity_Id;
13026 Subp : Entity_Id;
13027 Actual_Subp : Entity_Id);
13028 -- Derive New_Subp from the ultimate alias of the parent subprogram Subp
13029 -- (which is an interface primitive). If Generic_Actual is present then
13030 -- Actual_Subp is the actual subprogram corresponding with the generic
13031 -- subprogram Subp.
13033 function Check_Derived_Type return Boolean is
13034 E : Entity_Id;
13035 Elmt : Elmt_Id;
13036 List : Elist_Id;
13037 New_Subp : Entity_Id;
13038 Op_Elmt : Elmt_Id;
13039 Subp : Entity_Id;
13041 begin
13042 -- Traverse list of entities in the current scope searching for
13043 -- an incomplete type whose full-view is derived type
13045 E := First_Entity (Scope (Derived_Type));
13046 while Present (E)
13047 and then E /= Derived_Type
13048 loop
13049 if Ekind (E) = E_Incomplete_Type
13050 and then Present (Full_View (E))
13051 and then Full_View (E) = Derived_Type
13052 then
13053 -- Disable this test if Derived_Type completes an incomplete
13054 -- type because in such case more primitives can be added
13055 -- later to the list of primitives of Derived_Type by routine
13056 -- Process_Incomplete_Dependents
13058 return True;
13059 end if;
13061 E := Next_Entity (E);
13062 end loop;
13064 List := Collect_Primitive_Operations (Derived_Type);
13065 Elmt := First_Elmt (List);
13067 Op_Elmt := First_Elmt (Op_List);
13068 while Present (Op_Elmt) loop
13069 Subp := Node (Op_Elmt);
13070 New_Subp := Node (Elmt);
13072 -- At this early stage Derived_Type has no entities with attribute
13073 -- Interface_Alias. In addition, such primitives are always
13074 -- located at the end of the list of primitives of Parent_Type.
13075 -- Therefore, if found we can safely stop processing pending
13076 -- entities.
13078 exit when Present (Interface_Alias (Subp));
13080 -- Handle hidden entities
13082 if not Is_Predefined_Dispatching_Operation (Subp)
13083 and then Is_Hidden (Subp)
13084 then
13085 if Present (New_Subp)
13086 and then Primitive_Names_Match (Subp, New_Subp)
13087 then
13088 Next_Elmt (Elmt);
13089 end if;
13091 else
13092 if not Present (New_Subp)
13093 or else Ekind (Subp) /= Ekind (New_Subp)
13094 or else not Primitive_Names_Match (Subp, New_Subp)
13095 then
13096 return False;
13097 end if;
13099 Next_Elmt (Elmt);
13100 end if;
13102 Next_Elmt (Op_Elmt);
13103 end loop;
13105 return True;
13106 end Check_Derived_Type;
13108 ---------------------------------
13109 -- Derive_Interface_Subprogram --
13110 ---------------------------------
13112 procedure Derive_Interface_Subprogram
13113 (New_Subp : in out Entity_Id;
13114 Subp : Entity_Id;
13115 Actual_Subp : Entity_Id)
13117 Iface_Subp : constant Entity_Id := Ultimate_Alias (Subp);
13118 Iface_Type : constant Entity_Id := Find_Dispatching_Type (Iface_Subp);
13120 begin
13121 pragma Assert (Is_Interface (Iface_Type));
13123 Derive_Subprogram
13124 (New_Subp => New_Subp,
13125 Parent_Subp => Iface_Subp,
13126 Derived_Type => Derived_Type,
13127 Parent_Type => Iface_Type,
13128 Actual_Subp => Actual_Subp);
13130 -- Given that this new interface entity corresponds with a primitive
13131 -- of the parent that was not overridden we must leave it associated
13132 -- with its parent primitive to ensure that it will share the same
13133 -- dispatch table slot when overridden.
13135 if No (Actual_Subp) then
13136 Set_Alias (New_Subp, Subp);
13138 -- For instantiations this is not needed since the previous call to
13139 -- Derive_Subprogram leaves the entity well decorated.
13141 else
13142 pragma Assert (Alias (New_Subp) = Actual_Subp);
13143 null;
13144 end if;
13145 end Derive_Interface_Subprogram;
13147 -- Local variables
13149 Alias_Subp : Entity_Id;
13150 Act_List : Elist_Id;
13151 Act_Elmt : Elmt_Id := No_Elmt;
13152 Act_Subp : Entity_Id := Empty;
13153 Elmt : Elmt_Id;
13154 Need_Search : Boolean := False;
13155 New_Subp : Entity_Id := Empty;
13156 Parent_Base : Entity_Id;
13157 Subp : Entity_Id;
13159 -- Start of processing for Derive_Subprograms
13161 begin
13162 if Ekind (Parent_Type) = E_Record_Type_With_Private
13163 and then Has_Discriminants (Parent_Type)
13164 and then Present (Full_View (Parent_Type))
13165 then
13166 Parent_Base := Full_View (Parent_Type);
13167 else
13168 Parent_Base := Parent_Type;
13169 end if;
13171 if Present (Generic_Actual) then
13172 Act_List := Collect_Primitive_Operations (Generic_Actual);
13173 Act_Elmt := First_Elmt (Act_List);
13174 end if;
13176 -- Derive primitives inherited from the parent. Note that if the generic
13177 -- actual is present, this is not really a type derivation, it is a
13178 -- completion within an instance.
13180 -- Case 1: Derived_Type does not implement interfaces
13182 if not Is_Tagged_Type (Derived_Type)
13183 or else (not Has_Interfaces (Derived_Type)
13184 and then not (Present (Generic_Actual)
13185 and then
13186 Has_Interfaces (Generic_Actual)))
13187 then
13188 Elmt := First_Elmt (Op_List);
13189 while Present (Elmt) loop
13190 Subp := Node (Elmt);
13192 -- Literals are derived earlier in the process of building the
13193 -- derived type, and are skipped here.
13195 if Ekind (Subp) = E_Enumeration_Literal then
13196 null;
13198 -- The actual is a direct descendant and the common primitive
13199 -- operations appear in the same order.
13201 -- If the generic parent type is present, the derived type is an
13202 -- instance of a formal derived type, and within the instance its
13203 -- operations are those of the actual. We derive from the formal
13204 -- type but make the inherited operations aliases of the
13205 -- corresponding operations of the actual.
13207 else
13208 pragma Assert (No (Node (Act_Elmt))
13209 or else (Primitive_Names_Match (Subp, Node (Act_Elmt))
13210 and then
13211 Type_Conformant (Subp, Node (Act_Elmt),
13212 Skip_Controlling_Formals => True)));
13214 Derive_Subprogram
13215 (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
13217 if Present (Act_Elmt) then
13218 Next_Elmt (Act_Elmt);
13219 end if;
13220 end if;
13222 Next_Elmt (Elmt);
13223 end loop;
13225 -- Case 2: Derived_Type implements interfaces
13227 else
13228 -- If the parent type has no predefined primitives we remove
13229 -- predefined primitives from the list of primitives of generic
13230 -- actual to simplify the complexity of this algorithm.
13232 if Present (Generic_Actual) then
13233 declare
13234 Has_Predefined_Primitives : Boolean := False;
13236 begin
13237 -- Check if the parent type has predefined primitives
13239 Elmt := First_Elmt (Op_List);
13240 while Present (Elmt) loop
13241 Subp := Node (Elmt);
13243 if Is_Predefined_Dispatching_Operation (Subp)
13244 and then not Comes_From_Source (Ultimate_Alias (Subp))
13245 then
13246 Has_Predefined_Primitives := True;
13247 exit;
13248 end if;
13250 Next_Elmt (Elmt);
13251 end loop;
13253 -- Remove predefined primitives of Generic_Actual. We must use
13254 -- an auxiliary list because in case of tagged types the value
13255 -- returned by Collect_Primitive_Operations is the value stored
13256 -- in its Primitive_Operations attribute (and we don't want to
13257 -- modify its current contents).
13259 if not Has_Predefined_Primitives then
13260 declare
13261 Aux_List : constant Elist_Id := New_Elmt_List;
13263 begin
13264 Elmt := First_Elmt (Act_List);
13265 while Present (Elmt) loop
13266 Subp := Node (Elmt);
13268 if not Is_Predefined_Dispatching_Operation (Subp)
13269 or else Comes_From_Source (Subp)
13270 then
13271 Append_Elmt (Subp, Aux_List);
13272 end if;
13274 Next_Elmt (Elmt);
13275 end loop;
13277 Act_List := Aux_List;
13278 end;
13279 end if;
13281 Act_Elmt := First_Elmt (Act_List);
13282 Act_Subp := Node (Act_Elmt);
13283 end;
13284 end if;
13286 -- Stage 1: If the generic actual is not present we derive the
13287 -- primitives inherited from the parent type. If the generic parent
13288 -- type is present, the derived type is an instance of a formal
13289 -- derived type, and within the instance its operations are those of
13290 -- the actual. We derive from the formal type but make the inherited
13291 -- operations aliases of the corresponding operations of the actual.
13293 Elmt := First_Elmt (Op_List);
13294 while Present (Elmt) loop
13295 Subp := Node (Elmt);
13296 Alias_Subp := Ultimate_Alias (Subp);
13298 -- Do not derive internal entities of the parent that link
13299 -- interface primitives with their covering primitive. These
13300 -- entities will be added to this type when frozen.
13302 if Present (Interface_Alias (Subp)) then
13303 goto Continue;
13304 end if;
13306 -- If the generic actual is present find the corresponding
13307 -- operation in the generic actual. If the parent type is a
13308 -- direct ancestor of the derived type then, even if it is an
13309 -- interface, the operations are inherited from the primary
13310 -- dispatch table and are in the proper order. If we detect here
13311 -- that primitives are not in the same order we traverse the list
13312 -- of primitive operations of the actual to find the one that
13313 -- implements the interface primitive.
13315 if Need_Search
13316 or else
13317 (Present (Generic_Actual)
13318 and then Present (Act_Subp)
13319 and then not
13320 (Primitive_Names_Match (Subp, Act_Subp)
13321 and then
13322 Type_Conformant (Subp, Act_Subp,
13323 Skip_Controlling_Formals => True)))
13324 then
13325 pragma Assert (not Is_Ancestor (Parent_Base, Generic_Actual));
13327 -- Remember that we need searching for all pending primitives
13329 Need_Search := True;
13331 -- Handle entities associated with interface primitives
13333 if Present (Alias_Subp)
13334 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
13335 and then not Is_Predefined_Dispatching_Operation (Subp)
13336 then
13337 -- Search for the primitive in the homonym chain
13339 Act_Subp :=
13340 Find_Primitive_Covering_Interface
13341 (Tagged_Type => Generic_Actual,
13342 Iface_Prim => Alias_Subp);
13344 -- Previous search may not locate primitives covering
13345 -- interfaces defined in generics units or instantiations.
13346 -- (it fails if the covering primitive has formals whose
13347 -- type is also defined in generics or instantiations).
13348 -- In such case we search in the list of primitives of the
13349 -- generic actual for the internal entity that links the
13350 -- interface primitive and the covering primitive.
13352 if No (Act_Subp)
13353 and then Is_Generic_Type (Parent_Type)
13354 then
13355 -- This code has been designed to handle only generic
13356 -- formals that implement interfaces that are defined
13357 -- in a generic unit or instantiation. If this code is
13358 -- needed for other cases we must review it because
13359 -- (given that it relies on Original_Location to locate
13360 -- the primitive of Generic_Actual that covers the
13361 -- interface) it could leave linked through attribute
13362 -- Alias entities of unrelated instantiations).
13364 pragma Assert
13365 (Is_Generic_Unit
13366 (Scope (Find_Dispatching_Type (Alias_Subp)))
13367 or else
13368 Instantiation_Depth
13369 (Sloc (Find_Dispatching_Type (Alias_Subp))) > 0);
13371 declare
13372 Iface_Prim_Loc : constant Source_Ptr :=
13373 Original_Location (Sloc (Alias_Subp));
13374 Elmt : Elmt_Id;
13375 Prim : Entity_Id;
13376 begin
13377 Elmt :=
13378 First_Elmt (Primitive_Operations (Generic_Actual));
13380 Search : while Present (Elmt) loop
13381 Prim := Node (Elmt);
13383 if Present (Interface_Alias (Prim))
13384 and then Original_Location
13385 (Sloc (Interface_Alias (Prim)))
13386 = Iface_Prim_Loc
13387 then
13388 Act_Subp := Alias (Prim);
13389 exit Search;
13390 end if;
13392 Next_Elmt (Elmt);
13393 end loop Search;
13394 end;
13395 end if;
13397 pragma Assert (Present (Act_Subp)
13398 or else Is_Abstract_Type (Generic_Actual)
13399 or else Serious_Errors_Detected > 0);
13401 -- Handle predefined primitives plus the rest of user-defined
13402 -- primitives
13404 else
13405 Act_Elmt := First_Elmt (Act_List);
13406 while Present (Act_Elmt) loop
13407 Act_Subp := Node (Act_Elmt);
13409 exit when Primitive_Names_Match (Subp, Act_Subp)
13410 and then Type_Conformant
13411 (Subp, Act_Subp,
13412 Skip_Controlling_Formals => True)
13413 and then No (Interface_Alias (Act_Subp));
13415 Next_Elmt (Act_Elmt);
13416 end loop;
13418 if No (Act_Elmt) then
13419 Act_Subp := Empty;
13420 end if;
13421 end if;
13422 end if;
13424 -- Case 1: If the parent is a limited interface then it has the
13425 -- predefined primitives of synchronized interfaces. However, the
13426 -- actual type may be a non-limited type and hence it does not
13427 -- have such primitives.
13429 if Present (Generic_Actual)
13430 and then not Present (Act_Subp)
13431 and then Is_Limited_Interface (Parent_Base)
13432 and then Is_Predefined_Interface_Primitive (Subp)
13433 then
13434 null;
13436 -- Case 2: Inherit entities associated with interfaces that were
13437 -- not covered by the parent type. We exclude here null interface
13438 -- primitives because they do not need special management.
13440 -- We also exclude interface operations that are renamings. If the
13441 -- subprogram is an explicit renaming of an interface primitive,
13442 -- it is a regular primitive operation, and the presence of its
13443 -- alias is not relevant: it has to be derived like any other
13444 -- primitive.
13446 elsif Present (Alias (Subp))
13447 and then Nkind (Unit_Declaration_Node (Subp)) /=
13448 N_Subprogram_Renaming_Declaration
13449 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
13450 and then not
13451 (Nkind (Parent (Alias_Subp)) = N_Procedure_Specification
13452 and then Null_Present (Parent (Alias_Subp)))
13453 then
13454 -- If this is an abstract private type then we transfer the
13455 -- derivation of the interface primitive from the partial view
13456 -- to the full view. This is safe because all the interfaces
13457 -- must be visible in the partial view. Done to avoid adding
13458 -- a new interface derivation to the private part of the
13459 -- enclosing package; otherwise this new derivation would be
13460 -- decorated as hidden when the analysis of the enclosing
13461 -- package completes.
13463 if Is_Abstract_Type (Derived_Type)
13464 and then In_Private_Part (Current_Scope)
13465 and then Has_Private_Declaration (Derived_Type)
13466 then
13467 declare
13468 Partial_View : Entity_Id;
13469 Elmt : Elmt_Id;
13470 Ent : Entity_Id;
13472 begin
13473 Partial_View := First_Entity (Current_Scope);
13474 loop
13475 exit when No (Partial_View)
13476 or else (Has_Private_Declaration (Partial_View)
13477 and then
13478 Full_View (Partial_View) = Derived_Type);
13480 Next_Entity (Partial_View);
13481 end loop;
13483 -- If the partial view was not found then the source code
13484 -- has errors and the derivation is not needed.
13486 if Present (Partial_View) then
13487 Elmt :=
13488 First_Elmt (Primitive_Operations (Partial_View));
13489 while Present (Elmt) loop
13490 Ent := Node (Elmt);
13492 if Present (Alias (Ent))
13493 and then Ultimate_Alias (Ent) = Alias (Subp)
13494 then
13495 Append_Elmt
13496 (Ent, Primitive_Operations (Derived_Type));
13497 exit;
13498 end if;
13500 Next_Elmt (Elmt);
13501 end loop;
13503 -- If the interface primitive was not found in the
13504 -- partial view then this interface primitive was
13505 -- overridden. We add a derivation to activate in
13506 -- Derive_Progenitor_Subprograms the machinery to
13507 -- search for it.
13509 if No (Elmt) then
13510 Derive_Interface_Subprogram
13511 (New_Subp => New_Subp,
13512 Subp => Subp,
13513 Actual_Subp => Act_Subp);
13514 end if;
13515 end if;
13516 end;
13517 else
13518 Derive_Interface_Subprogram
13519 (New_Subp => New_Subp,
13520 Subp => Subp,
13521 Actual_Subp => Act_Subp);
13522 end if;
13524 -- Case 3: Common derivation
13526 else
13527 Derive_Subprogram
13528 (New_Subp => New_Subp,
13529 Parent_Subp => Subp,
13530 Derived_Type => Derived_Type,
13531 Parent_Type => Parent_Base,
13532 Actual_Subp => Act_Subp);
13533 end if;
13535 -- No need to update Act_Elm if we must search for the
13536 -- corresponding operation in the generic actual
13538 if not Need_Search
13539 and then Present (Act_Elmt)
13540 then
13541 Next_Elmt (Act_Elmt);
13542 Act_Subp := Node (Act_Elmt);
13543 end if;
13545 <<Continue>>
13546 Next_Elmt (Elmt);
13547 end loop;
13549 -- Inherit additional operations from progenitors. If the derived
13550 -- type is a generic actual, there are not new primitive operations
13551 -- for the type because it has those of the actual, and therefore
13552 -- nothing needs to be done. The renamings generated above are not
13553 -- primitive operations, and their purpose is simply to make the
13554 -- proper operations visible within an instantiation.
13556 if No (Generic_Actual) then
13557 Derive_Progenitor_Subprograms (Parent_Base, Derived_Type);
13558 end if;
13559 end if;
13561 -- Final check: Direct descendants must have their primitives in the
13562 -- same order. We exclude from this test untagged types and instances
13563 -- of formal derived types. We skip this test if we have already
13564 -- reported serious errors in the sources.
13566 pragma Assert (not Is_Tagged_Type (Derived_Type)
13567 or else Present (Generic_Actual)
13568 or else Serious_Errors_Detected > 0
13569 or else Check_Derived_Type);
13570 end Derive_Subprograms;
13572 --------------------------------
13573 -- Derived_Standard_Character --
13574 --------------------------------
13576 procedure Derived_Standard_Character
13577 (N : Node_Id;
13578 Parent_Type : Entity_Id;
13579 Derived_Type : Entity_Id)
13581 Loc : constant Source_Ptr := Sloc (N);
13582 Def : constant Node_Id := Type_Definition (N);
13583 Indic : constant Node_Id := Subtype_Indication (Def);
13584 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
13585 Implicit_Base : constant Entity_Id :=
13586 Create_Itype
13587 (E_Enumeration_Type, N, Derived_Type, 'B');
13589 Lo : Node_Id;
13590 Hi : Node_Id;
13592 begin
13593 Discard_Node (Process_Subtype (Indic, N));
13595 Set_Etype (Implicit_Base, Parent_Base);
13596 Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
13597 Set_RM_Size (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
13599 Set_Is_Character_Type (Implicit_Base, True);
13600 Set_Has_Delayed_Freeze (Implicit_Base);
13602 -- The bounds of the implicit base are the bounds of the parent base.
13603 -- Note that their type is the parent base.
13605 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
13606 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
13608 Set_Scalar_Range (Implicit_Base,
13609 Make_Range (Loc,
13610 Low_Bound => Lo,
13611 High_Bound => Hi));
13613 Conditional_Delay (Derived_Type, Parent_Type);
13615 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
13616 Set_Etype (Derived_Type, Implicit_Base);
13617 Set_Size_Info (Derived_Type, Parent_Type);
13619 if Unknown_RM_Size (Derived_Type) then
13620 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
13621 end if;
13623 Set_Is_Character_Type (Derived_Type, True);
13625 if Nkind (Indic) /= N_Subtype_Indication then
13627 -- If no explicit constraint, the bounds are those
13628 -- of the parent type.
13630 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Type));
13631 Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
13632 Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
13633 end if;
13635 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
13637 -- Because the implicit base is used in the conversion of the bounds, we
13638 -- have to freeze it now. This is similar to what is done for numeric
13639 -- types, and it equally suspicious, but otherwise a non-static bound
13640 -- will have a reference to an unfrozen type, which is rejected by Gigi
13641 -- (???). This requires specific care for definition of stream
13642 -- attributes. For details, see comments at the end of
13643 -- Build_Derived_Numeric_Type.
13645 Freeze_Before (N, Implicit_Base);
13646 end Derived_Standard_Character;
13648 ------------------------------
13649 -- Derived_Type_Declaration --
13650 ------------------------------
13652 procedure Derived_Type_Declaration
13653 (T : Entity_Id;
13654 N : Node_Id;
13655 Is_Completion : Boolean)
13657 Parent_Type : Entity_Id;
13659 function Comes_From_Generic (Typ : Entity_Id) return Boolean;
13660 -- Check whether the parent type is a generic formal, or derives
13661 -- directly or indirectly from one.
13663 ------------------------
13664 -- Comes_From_Generic --
13665 ------------------------
13667 function Comes_From_Generic (Typ : Entity_Id) return Boolean is
13668 begin
13669 if Is_Generic_Type (Typ) then
13670 return True;
13672 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
13673 return True;
13675 elsif Is_Private_Type (Typ)
13676 and then Present (Full_View (Typ))
13677 and then Is_Generic_Type (Root_Type (Full_View (Typ)))
13678 then
13679 return True;
13681 elsif Is_Generic_Actual_Type (Typ) then
13682 return True;
13684 else
13685 return False;
13686 end if;
13687 end Comes_From_Generic;
13689 -- Local variables
13691 Def : constant Node_Id := Type_Definition (N);
13692 Iface_Def : Node_Id;
13693 Indic : constant Node_Id := Subtype_Indication (Def);
13694 Extension : constant Node_Id := Record_Extension_Part (Def);
13695 Parent_Node : Node_Id;
13696 Parent_Scope : Entity_Id;
13697 Taggd : Boolean;
13699 -- Start of processing for Derived_Type_Declaration
13701 begin
13702 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
13704 -- Ada 2005 (AI-251): In case of interface derivation check that the
13705 -- parent is also an interface.
13707 if Interface_Present (Def) then
13708 if not Is_Interface (Parent_Type) then
13709 Diagnose_Interface (Indic, Parent_Type);
13711 else
13712 Parent_Node := Parent (Base_Type (Parent_Type));
13713 Iface_Def := Type_Definition (Parent_Node);
13715 -- Ada 2005 (AI-251): Limited interfaces can only inherit from
13716 -- other limited interfaces.
13718 if Limited_Present (Def) then
13719 if Limited_Present (Iface_Def) then
13720 null;
13722 elsif Protected_Present (Iface_Def) then
13723 Error_Msg_NE
13724 ("descendant of& must be declared"
13725 & " as a protected interface",
13726 N, Parent_Type);
13728 elsif Synchronized_Present (Iface_Def) then
13729 Error_Msg_NE
13730 ("descendant of& must be declared"
13731 & " as a synchronized interface",
13732 N, Parent_Type);
13734 elsif Task_Present (Iface_Def) then
13735 Error_Msg_NE
13736 ("descendant of& must be declared as a task interface",
13737 N, Parent_Type);
13739 else
13740 Error_Msg_N
13741 ("(Ada 2005) limited interface cannot "
13742 & "inherit from non-limited interface", Indic);
13743 end if;
13745 -- Ada 2005 (AI-345): Non-limited interfaces can only inherit
13746 -- from non-limited or limited interfaces.
13748 elsif not Protected_Present (Def)
13749 and then not Synchronized_Present (Def)
13750 and then not Task_Present (Def)
13751 then
13752 if Limited_Present (Iface_Def) then
13753 null;
13755 elsif Protected_Present (Iface_Def) then
13756 Error_Msg_NE
13757 ("descendant of& must be declared"
13758 & " as a protected interface",
13759 N, Parent_Type);
13761 elsif Synchronized_Present (Iface_Def) then
13762 Error_Msg_NE
13763 ("descendant of& must be declared"
13764 & " as a synchronized interface",
13765 N, Parent_Type);
13767 elsif Task_Present (Iface_Def) then
13768 Error_Msg_NE
13769 ("descendant of& must be declared as a task interface",
13770 N, Parent_Type);
13771 else
13772 null;
13773 end if;
13774 end if;
13775 end if;
13776 end if;
13778 if Is_Tagged_Type (Parent_Type)
13779 and then Is_Concurrent_Type (Parent_Type)
13780 and then not Is_Interface (Parent_Type)
13781 then
13782 Error_Msg_N
13783 ("parent type of a record extension cannot be "
13784 & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
13785 Set_Etype (T, Any_Type);
13786 return;
13787 end if;
13789 -- Ada 2005 (AI-251): Decorate all the names in the list of ancestor
13790 -- interfaces
13792 if Is_Tagged_Type (Parent_Type)
13793 and then Is_Non_Empty_List (Interface_List (Def))
13794 then
13795 declare
13796 Intf : Node_Id;
13797 T : Entity_Id;
13799 begin
13800 Intf := First (Interface_List (Def));
13801 while Present (Intf) loop
13802 T := Find_Type_Of_Subtype_Indic (Intf);
13804 if not Is_Interface (T) then
13805 Diagnose_Interface (Intf, T);
13807 -- Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
13808 -- a limited type from having a nonlimited progenitor.
13810 elsif (Limited_Present (Def)
13811 or else (not Is_Interface (Parent_Type)
13812 and then Is_Limited_Type (Parent_Type)))
13813 and then not Is_Limited_Interface (T)
13814 then
13815 Error_Msg_NE
13816 ("progenitor interface& of limited type must be limited",
13817 N, T);
13818 end if;
13820 Next (Intf);
13821 end loop;
13822 end;
13823 end if;
13825 if Parent_Type = Any_Type
13826 or else Etype (Parent_Type) = Any_Type
13827 or else (Is_Class_Wide_Type (Parent_Type)
13828 and then Etype (Parent_Type) = T)
13829 then
13830 -- If Parent_Type is undefined or illegal, make new type into a
13831 -- subtype of Any_Type, and set a few attributes to prevent cascaded
13832 -- errors. If this is a self-definition, emit error now.
13834 if T = Parent_Type
13835 or else T = Etype (Parent_Type)
13836 then
13837 Error_Msg_N ("type cannot be used in its own definition", Indic);
13838 end if;
13840 Set_Ekind (T, Ekind (Parent_Type));
13841 Set_Etype (T, Any_Type);
13842 Set_Scalar_Range (T, Scalar_Range (Any_Type));
13844 if Is_Tagged_Type (T)
13845 and then Is_Record_Type (T)
13846 then
13847 Set_Direct_Primitive_Operations (T, New_Elmt_List);
13848 end if;
13850 return;
13851 end if;
13853 -- Ada 2005 (AI-251): The case in which the parent of the full-view is
13854 -- an interface is special because the list of interfaces in the full
13855 -- view can be given in any order. For example:
13857 -- type A is interface;
13858 -- type B is interface and A;
13859 -- type D is new B with private;
13860 -- private
13861 -- type D is new A and B with null record; -- 1 --
13863 -- In this case we perform the following transformation of -1-:
13865 -- type D is new B and A with null record;
13867 -- If the parent of the full-view covers the parent of the partial-view
13868 -- we have two possible cases:
13870 -- 1) They have the same parent
13871 -- 2) The parent of the full-view implements some further interfaces
13873 -- In both cases we do not need to perform the transformation. In the
13874 -- first case the source program is correct and the transformation is
13875 -- not needed; in the second case the source program does not fulfill
13876 -- the no-hidden interfaces rule (AI-396) and the error will be reported
13877 -- later.
13879 -- This transformation not only simplifies the rest of the analysis of
13880 -- this type declaration but also simplifies the correct generation of
13881 -- the object layout to the expander.
13883 if In_Private_Part (Current_Scope)
13884 and then Is_Interface (Parent_Type)
13885 then
13886 declare
13887 Iface : Node_Id;
13888 Partial_View : Entity_Id;
13889 Partial_View_Parent : Entity_Id;
13890 New_Iface : Node_Id;
13892 begin
13893 -- Look for the associated private type declaration
13895 Partial_View := First_Entity (Current_Scope);
13896 loop
13897 exit when No (Partial_View)
13898 or else (Has_Private_Declaration (Partial_View)
13899 and then Full_View (Partial_View) = T);
13901 Next_Entity (Partial_View);
13902 end loop;
13904 -- If the partial view was not found then the source code has
13905 -- errors and the transformation is not needed.
13907 if Present (Partial_View) then
13908 Partial_View_Parent := Etype (Partial_View);
13910 -- If the parent of the full-view covers the parent of the
13911 -- partial-view we have nothing else to do.
13913 if Interface_Present_In_Ancestor
13914 (Parent_Type, Partial_View_Parent)
13915 then
13916 null;
13918 -- Traverse the list of interfaces of the full-view to look
13919 -- for the parent of the partial-view and perform the tree
13920 -- transformation.
13922 else
13923 Iface := First (Interface_List (Def));
13924 while Present (Iface) loop
13925 if Etype (Iface) = Etype (Partial_View) then
13926 Rewrite (Subtype_Indication (Def),
13927 New_Copy (Subtype_Indication
13928 (Parent (Partial_View))));
13930 New_Iface :=
13931 Make_Identifier (Sloc (N), Chars (Parent_Type));
13932 Append (New_Iface, Interface_List (Def));
13934 -- Analyze the transformed code
13936 Derived_Type_Declaration (T, N, Is_Completion);
13937 return;
13938 end if;
13940 Next (Iface);
13941 end loop;
13942 end if;
13943 end if;
13944 end;
13945 end if;
13947 -- Only composite types other than array types are allowed to have
13948 -- discriminants.
13950 if Present (Discriminant_Specifications (N))
13951 and then (Is_Elementary_Type (Parent_Type)
13952 or else Is_Array_Type (Parent_Type))
13953 and then not Error_Posted (N)
13954 then
13955 Error_Msg_N
13956 ("elementary or array type cannot have discriminants",
13957 Defining_Identifier (First (Discriminant_Specifications (N))));
13958 Set_Has_Discriminants (T, False);
13959 end if;
13961 -- In Ada 83, a derived type defined in a package specification cannot
13962 -- be used for further derivation until the end of its visible part.
13963 -- Note that derivation in the private part of the package is allowed.
13965 if Ada_Version = Ada_83
13966 and then Is_Derived_Type (Parent_Type)
13967 and then In_Visible_Part (Scope (Parent_Type))
13968 then
13969 if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
13970 Error_Msg_N
13971 ("(Ada 83): premature use of type for derivation", Indic);
13972 end if;
13973 end if;
13975 -- Check for early use of incomplete or private type
13977 if Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
13978 Error_Msg_N ("premature derivation of incomplete type", Indic);
13979 return;
13981 elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
13982 and then not Comes_From_Generic (Parent_Type))
13983 or else Has_Private_Component (Parent_Type)
13984 then
13985 -- The ancestor type of a formal type can be incomplete, in which
13986 -- case only the operations of the partial view are available in
13987 -- the generic. Subsequent checks may be required when the full
13988 -- view is analyzed, to verify that derivation from a tagged type
13989 -- has an extension.
13991 if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
13992 null;
13994 elsif No (Underlying_Type (Parent_Type))
13995 or else Has_Private_Component (Parent_Type)
13996 then
13997 Error_Msg_N
13998 ("premature derivation of derived or private type", Indic);
14000 -- Flag the type itself as being in error, this prevents some
14001 -- nasty problems with subsequent uses of the malformed type.
14003 Set_Error_Posted (T);
14005 -- Check that within the immediate scope of an untagged partial
14006 -- view it's illegal to derive from the partial view if the
14007 -- full view is tagged. (7.3(7))
14009 -- We verify that the Parent_Type is a partial view by checking
14010 -- that it is not a Full_Type_Declaration (i.e. a private type or
14011 -- private extension declaration), to distinguish a partial view
14012 -- from a derivation from a private type which also appears as
14013 -- E_Private_Type.
14015 elsif Present (Full_View (Parent_Type))
14016 and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
14017 and then not Is_Tagged_Type (Parent_Type)
14018 and then Is_Tagged_Type (Full_View (Parent_Type))
14019 then
14020 Parent_Scope := Scope (T);
14021 while Present (Parent_Scope)
14022 and then Parent_Scope /= Standard_Standard
14023 loop
14024 if Parent_Scope = Scope (Parent_Type) then
14025 Error_Msg_N
14026 ("premature derivation from type with tagged full view",
14027 Indic);
14028 end if;
14030 Parent_Scope := Scope (Parent_Scope);
14031 end loop;
14032 end if;
14033 end if;
14035 -- Check that form of derivation is appropriate
14037 Taggd := Is_Tagged_Type (Parent_Type);
14039 -- Perhaps the parent type should be changed to the class-wide type's
14040 -- specific type in this case to prevent cascading errors ???
14042 if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
14043 Error_Msg_N ("parent type must not be a class-wide type", Indic);
14044 return;
14045 end if;
14047 if Present (Extension) and then not Taggd then
14048 Error_Msg_N
14049 ("type derived from untagged type cannot have extension", Indic);
14051 elsif No (Extension) and then Taggd then
14053 -- If this declaration is within a private part (or body) of a
14054 -- generic instantiation then the derivation is allowed (the parent
14055 -- type can only appear tagged in this case if it's a generic actual
14056 -- type, since it would otherwise have been rejected in the analysis
14057 -- of the generic template).
14059 if not Is_Generic_Actual_Type (Parent_Type)
14060 or else In_Visible_Part (Scope (Parent_Type))
14061 then
14062 if Is_Class_Wide_Type (Parent_Type) then
14063 Error_Msg_N
14064 ("parent type must not be a class-wide type", Indic);
14066 -- Use specific type to prevent cascaded errors.
14068 Parent_Type := Etype (Parent_Type);
14070 else
14071 Error_Msg_N
14072 ("type derived from tagged type must have extension", Indic);
14073 end if;
14074 end if;
14075 end if;
14077 -- AI-443: Synchronized formal derived types require a private
14078 -- extension. There is no point in checking the ancestor type or
14079 -- the progenitors since the construct is wrong to begin with.
14081 if Ada_Version >= Ada_2005
14082 and then Is_Generic_Type (T)
14083 and then Present (Original_Node (N))
14084 then
14085 declare
14086 Decl : constant Node_Id := Original_Node (N);
14088 begin
14089 if Nkind (Decl) = N_Formal_Type_Declaration
14090 and then Nkind (Formal_Type_Definition (Decl)) =
14091 N_Formal_Derived_Type_Definition
14092 and then Synchronized_Present (Formal_Type_Definition (Decl))
14093 and then No (Extension)
14095 -- Avoid emitting a duplicate error message
14097 and then not Error_Posted (Indic)
14098 then
14099 Error_Msg_N
14100 ("synchronized derived type must have extension", N);
14101 end if;
14102 end;
14103 end if;
14105 if Null_Exclusion_Present (Def)
14106 and then not Is_Access_Type (Parent_Type)
14107 then
14108 Error_Msg_N ("null exclusion can only apply to an access type", N);
14109 end if;
14111 -- Avoid deriving parent primitives of underlying record views
14113 Build_Derived_Type (N, Parent_Type, T, Is_Completion,
14114 Derive_Subps => not Is_Underlying_Record_View (T));
14116 -- AI-419: The parent type of an explicitly limited derived type must
14117 -- be a limited type or a limited interface.
14119 if Limited_Present (Def) then
14120 Set_Is_Limited_Record (T);
14122 if Is_Interface (T) then
14123 Set_Is_Limited_Interface (T);
14124 end if;
14126 if not Is_Limited_Type (Parent_Type)
14127 and then
14128 (not Is_Interface (Parent_Type)
14129 or else not Is_Limited_Interface (Parent_Type))
14130 then
14131 -- AI05-0096: a derivation in the private part of an instance is
14132 -- legal if the generic formal is untagged limited, and the actual
14133 -- is non-limited.
14135 if Is_Generic_Actual_Type (Parent_Type)
14136 and then In_Private_Part (Current_Scope)
14137 and then
14138 not Is_Tagged_Type
14139 (Generic_Parent_Type (Parent (Parent_Type)))
14140 then
14141 null;
14143 else
14144 Error_Msg_NE
14145 ("parent type& of limited type must be limited",
14146 N, Parent_Type);
14147 end if;
14148 end if;
14149 end if;
14150 end Derived_Type_Declaration;
14152 ------------------------
14153 -- Diagnose_Interface --
14154 ------------------------
14156 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id) is
14157 begin
14158 if not Is_Interface (E)
14159 and then E /= Any_Type
14160 then
14161 Error_Msg_NE ("(Ada 2005) & must be an interface", N, E);
14162 end if;
14163 end Diagnose_Interface;
14165 ----------------------------------
14166 -- Enumeration_Type_Declaration --
14167 ----------------------------------
14169 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
14170 Ev : Uint;
14171 L : Node_Id;
14172 R_Node : Node_Id;
14173 B_Node : Node_Id;
14175 begin
14176 -- Create identifier node representing lower bound
14178 B_Node := New_Node (N_Identifier, Sloc (Def));
14179 L := First (Literals (Def));
14180 Set_Chars (B_Node, Chars (L));
14181 Set_Entity (B_Node, L);
14182 Set_Etype (B_Node, T);
14183 Set_Is_Static_Expression (B_Node, True);
14185 R_Node := New_Node (N_Range, Sloc (Def));
14186 Set_Low_Bound (R_Node, B_Node);
14188 Set_Ekind (T, E_Enumeration_Type);
14189 Set_First_Literal (T, L);
14190 Set_Etype (T, T);
14191 Set_Is_Constrained (T);
14193 Ev := Uint_0;
14195 -- Loop through literals of enumeration type setting pos and rep values
14196 -- except that if the Ekind is already set, then it means the literal
14197 -- was already constructed (case of a derived type declaration and we
14198 -- should not disturb the Pos and Rep values.
14200 while Present (L) loop
14201 if Ekind (L) /= E_Enumeration_Literal then
14202 Set_Ekind (L, E_Enumeration_Literal);
14203 Set_Enumeration_Pos (L, Ev);
14204 Set_Enumeration_Rep (L, Ev);
14205 Set_Is_Known_Valid (L, True);
14206 end if;
14208 Set_Etype (L, T);
14209 New_Overloaded_Entity (L);
14210 Generate_Definition (L);
14211 Set_Convention (L, Convention_Intrinsic);
14213 -- Case of character literal
14215 if Nkind (L) = N_Defining_Character_Literal then
14216 Set_Is_Character_Type (T, True);
14218 -- Check violation of No_Wide_Characters
14220 if Restriction_Check_Required (No_Wide_Characters) then
14221 Get_Name_String (Chars (L));
14223 if Name_Len >= 3 and then Name_Buffer (1 .. 2) = "QW" then
14224 Check_Restriction (No_Wide_Characters, L);
14225 end if;
14226 end if;
14227 end if;
14229 Ev := Ev + 1;
14230 Next (L);
14231 end loop;
14233 -- Now create a node representing upper bound
14235 B_Node := New_Node (N_Identifier, Sloc (Def));
14236 Set_Chars (B_Node, Chars (Last (Literals (Def))));
14237 Set_Entity (B_Node, Last (Literals (Def)));
14238 Set_Etype (B_Node, T);
14239 Set_Is_Static_Expression (B_Node, True);
14241 Set_High_Bound (R_Node, B_Node);
14243 -- Initialize various fields of the type. Some of this information
14244 -- may be overwritten later through rep.clauses.
14246 Set_Scalar_Range (T, R_Node);
14247 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
14248 Set_Enum_Esize (T);
14249 Set_Enum_Pos_To_Rep (T, Empty);
14251 -- Set Discard_Names if configuration pragma set, or if there is
14252 -- a parameterless pragma in the current declarative region
14254 if Global_Discard_Names
14255 or else Discard_Names (Scope (T))
14256 then
14257 Set_Discard_Names (T);
14258 end if;
14260 -- Process end label if there is one
14262 if Present (Def) then
14263 Process_End_Label (Def, 'e', T);
14264 end if;
14265 end Enumeration_Type_Declaration;
14267 ---------------------------------
14268 -- Expand_To_Stored_Constraint --
14269 ---------------------------------
14271 function Expand_To_Stored_Constraint
14272 (Typ : Entity_Id;
14273 Constraint : Elist_Id) return Elist_Id
14275 Explicitly_Discriminated_Type : Entity_Id;
14276 Expansion : Elist_Id;
14277 Discriminant : Entity_Id;
14279 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
14280 -- Find the nearest type that actually specifies discriminants
14282 ---------------------------------
14283 -- Type_With_Explicit_Discrims --
14284 ---------------------------------
14286 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
14287 Typ : constant E := Base_Type (Id);
14289 begin
14290 if Ekind (Typ) in Incomplete_Or_Private_Kind then
14291 if Present (Full_View (Typ)) then
14292 return Type_With_Explicit_Discrims (Full_View (Typ));
14293 end if;
14295 else
14296 if Has_Discriminants (Typ) then
14297 return Typ;
14298 end if;
14299 end if;
14301 if Etype (Typ) = Typ then
14302 return Empty;
14303 elsif Has_Discriminants (Typ) then
14304 return Typ;
14305 else
14306 return Type_With_Explicit_Discrims (Etype (Typ));
14307 end if;
14309 end Type_With_Explicit_Discrims;
14311 -- Start of processing for Expand_To_Stored_Constraint
14313 begin
14314 if No (Constraint)
14315 or else Is_Empty_Elmt_List (Constraint)
14316 then
14317 return No_Elist;
14318 end if;
14320 Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
14322 if No (Explicitly_Discriminated_Type) then
14323 return No_Elist;
14324 end if;
14326 Expansion := New_Elmt_List;
14328 Discriminant :=
14329 First_Stored_Discriminant (Explicitly_Discriminated_Type);
14330 while Present (Discriminant) loop
14331 Append_Elmt (
14332 Get_Discriminant_Value (
14333 Discriminant, Explicitly_Discriminated_Type, Constraint),
14334 Expansion);
14335 Next_Stored_Discriminant (Discriminant);
14336 end loop;
14338 return Expansion;
14339 end Expand_To_Stored_Constraint;
14341 ---------------------------
14342 -- Find_Hidden_Interface --
14343 ---------------------------
14345 function Find_Hidden_Interface
14346 (Src : Elist_Id;
14347 Dest : Elist_Id) return Entity_Id
14349 Iface : Entity_Id;
14350 Iface_Elmt : Elmt_Id;
14352 begin
14353 if Present (Src) and then Present (Dest) then
14354 Iface_Elmt := First_Elmt (Src);
14355 while Present (Iface_Elmt) loop
14356 Iface := Node (Iface_Elmt);
14358 if Is_Interface (Iface)
14359 and then not Contain_Interface (Iface, Dest)
14360 then
14361 return Iface;
14362 end if;
14364 Next_Elmt (Iface_Elmt);
14365 end loop;
14366 end if;
14368 return Empty;
14369 end Find_Hidden_Interface;
14371 --------------------
14372 -- Find_Type_Name --
14373 --------------------
14375 function Find_Type_Name (N : Node_Id) return Entity_Id is
14376 Id : constant Entity_Id := Defining_Identifier (N);
14377 Prev : Entity_Id;
14378 New_Id : Entity_Id;
14379 Prev_Par : Node_Id;
14381 procedure Tag_Mismatch;
14382 -- Diagnose a tagged partial view whose full view is untagged.
14383 -- We post the message on the full view, with a reference to
14384 -- the previous partial view. The partial view can be private
14385 -- or incomplete, and these are handled in a different manner,
14386 -- so we determine the position of the error message from the
14387 -- respective slocs of both.
14389 ------------------
14390 -- Tag_Mismatch --
14391 ------------------
14393 procedure Tag_Mismatch is
14394 begin
14395 if Sloc (Prev) < Sloc (Id) then
14396 if Ada_Version >= Ada_2012
14397 and then Nkind (N) = N_Private_Type_Declaration
14398 then
14399 Error_Msg_NE
14400 ("declaration of private } must be a tagged type ", Id, Prev);
14401 else
14402 Error_Msg_NE
14403 ("full declaration of } must be a tagged type ", Id, Prev);
14404 end if;
14405 else
14406 if Ada_Version >= Ada_2012
14407 and then Nkind (N) = N_Private_Type_Declaration
14408 then
14409 Error_Msg_NE
14410 ("declaration of private } must be a tagged type ", Prev, Id);
14411 else
14412 Error_Msg_NE
14413 ("full declaration of } must be a tagged type ", Prev, Id);
14414 end if;
14415 end if;
14416 end Tag_Mismatch;
14418 -- Start of processing for Find_Type_Name
14420 begin
14421 -- Find incomplete declaration, if one was given
14423 Prev := Current_Entity_In_Scope (Id);
14425 -- New type declaration
14427 if No (Prev) then
14428 Enter_Name (Id);
14429 return Id;
14431 -- Previous declaration exists
14433 else
14434 Prev_Par := Parent (Prev);
14436 -- Error if not incomplete/private case except if previous
14437 -- declaration is implicit, etc. Enter_Name will emit error if
14438 -- appropriate.
14440 if not Is_Incomplete_Or_Private_Type (Prev) then
14441 Enter_Name (Id);
14442 New_Id := Id;
14444 -- Check invalid completion of private or incomplete type
14446 elsif not Nkind_In (N, N_Full_Type_Declaration,
14447 N_Task_Type_Declaration,
14448 N_Protected_Type_Declaration)
14449 and then
14450 (Ada_Version < Ada_2012
14451 or else not Is_Incomplete_Type (Prev)
14452 or else not Nkind_In (N, N_Private_Type_Declaration,
14453 N_Private_Extension_Declaration))
14454 then
14455 -- Completion must be a full type declarations (RM 7.3(4))
14457 Error_Msg_Sloc := Sloc (Prev);
14458 Error_Msg_NE ("invalid completion of }", Id, Prev);
14460 -- Set scope of Id to avoid cascaded errors. Entity is never
14461 -- examined again, except when saving globals in generics.
14463 Set_Scope (Id, Current_Scope);
14464 New_Id := Id;
14466 -- If this is a repeated incomplete declaration, no further
14467 -- checks are possible.
14469 if Nkind (N) = N_Incomplete_Type_Declaration then
14470 return Prev;
14471 end if;
14473 -- Case of full declaration of incomplete type
14475 elsif Ekind (Prev) = E_Incomplete_Type
14476 and then (Ada_Version < Ada_2012
14477 or else No (Full_View (Prev))
14478 or else not Is_Private_Type (Full_View (Prev)))
14479 then
14481 -- Indicate that the incomplete declaration has a matching full
14482 -- declaration. The defining occurrence of the incomplete
14483 -- declaration remains the visible one, and the procedure
14484 -- Get_Full_View dereferences it whenever the type is used.
14486 if Present (Full_View (Prev)) then
14487 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
14488 end if;
14490 Set_Full_View (Prev, Id);
14491 Append_Entity (Id, Current_Scope);
14492 Set_Is_Public (Id, Is_Public (Prev));
14493 Set_Is_Internal (Id);
14494 New_Id := Prev;
14496 -- If the incomplete view is tagged, a class_wide type has been
14497 -- created already. Use it for the private type as well, in order
14498 -- to prevent multiple incompatible class-wide types that may be
14499 -- created for self-referential anonymous access components.
14501 if Is_Tagged_Type (Prev)
14502 and then Present (Class_Wide_Type (Prev))
14503 then
14504 Set_Ekind (Id, Ekind (Prev)); -- will be reset later
14505 Set_Class_Wide_Type (Id, Class_Wide_Type (Prev));
14506 Set_Etype (Class_Wide_Type (Id), Id);
14507 end if;
14509 -- Case of full declaration of private type
14511 else
14512 -- If the private type was a completion of an incomplete type then
14513 -- update Prev to reference the private type
14515 if Ada_Version >= Ada_2012
14516 and then Ekind (Prev) = E_Incomplete_Type
14517 and then Present (Full_View (Prev))
14518 and then Is_Private_Type (Full_View (Prev))
14519 then
14520 Prev := Full_View (Prev);
14521 Prev_Par := Parent (Prev);
14522 end if;
14524 if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
14525 if Etype (Prev) /= Prev then
14527 -- Prev is a private subtype or a derived type, and needs
14528 -- no completion.
14530 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
14531 New_Id := Id;
14533 elsif Ekind (Prev) = E_Private_Type
14534 and then Nkind_In (N, N_Task_Type_Declaration,
14535 N_Protected_Type_Declaration)
14536 then
14537 Error_Msg_N
14538 ("completion of nonlimited type cannot be limited", N);
14540 elsif Ekind (Prev) = E_Record_Type_With_Private
14541 and then Nkind_In (N, N_Task_Type_Declaration,
14542 N_Protected_Type_Declaration)
14543 then
14544 if not Is_Limited_Record (Prev) then
14545 Error_Msg_N
14546 ("completion of nonlimited type cannot be limited", N);
14548 elsif No (Interface_List (N)) then
14549 Error_Msg_N
14550 ("completion of tagged private type must be tagged",
14552 end if;
14554 elsif Nkind (N) = N_Full_Type_Declaration
14555 and then
14556 Nkind (Type_Definition (N)) = N_Record_Definition
14557 and then Interface_Present (Type_Definition (N))
14558 then
14559 Error_Msg_N
14560 ("completion of private type cannot be an interface", N);
14561 end if;
14563 -- Ada 2005 (AI-251): Private extension declaration of a task
14564 -- type or a protected type. This case arises when covering
14565 -- interface types.
14567 elsif Nkind_In (N, N_Task_Type_Declaration,
14568 N_Protected_Type_Declaration)
14569 then
14570 null;
14572 elsif Nkind (N) /= N_Full_Type_Declaration
14573 or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
14574 then
14575 Error_Msg_N
14576 ("full view of private extension must be an extension", N);
14578 elsif not (Abstract_Present (Parent (Prev)))
14579 and then Abstract_Present (Type_Definition (N))
14580 then
14581 Error_Msg_N
14582 ("full view of non-abstract extension cannot be abstract", N);
14583 end if;
14585 if not In_Private_Part (Current_Scope) then
14586 Error_Msg_N
14587 ("declaration of full view must appear in private part", N);
14588 end if;
14590 Copy_And_Swap (Prev, Id);
14591 Set_Has_Private_Declaration (Prev);
14592 Set_Has_Private_Declaration (Id);
14594 -- If no error, propagate freeze_node from private to full view.
14595 -- It may have been generated for an early operational item.
14597 if Present (Freeze_Node (Id))
14598 and then Serious_Errors_Detected = 0
14599 and then No (Full_View (Id))
14600 then
14601 Set_Freeze_Node (Prev, Freeze_Node (Id));
14602 Set_Freeze_Node (Id, Empty);
14603 Set_First_Rep_Item (Prev, First_Rep_Item (Id));
14604 end if;
14606 Set_Full_View (Id, Prev);
14607 New_Id := Prev;
14608 end if;
14610 -- Verify that full declaration conforms to partial one
14612 if Is_Incomplete_Or_Private_Type (Prev)
14613 and then Present (Discriminant_Specifications (Prev_Par))
14614 then
14615 if Present (Discriminant_Specifications (N)) then
14616 if Ekind (Prev) = E_Incomplete_Type then
14617 Check_Discriminant_Conformance (N, Prev, Prev);
14618 else
14619 Check_Discriminant_Conformance (N, Prev, Id);
14620 end if;
14622 else
14623 Error_Msg_N
14624 ("missing discriminants in full type declaration", N);
14626 -- To avoid cascaded errors on subsequent use, share the
14627 -- discriminants of the partial view.
14629 Set_Discriminant_Specifications (N,
14630 Discriminant_Specifications (Prev_Par));
14631 end if;
14632 end if;
14634 -- A prior untagged partial view can have an associated class-wide
14635 -- type due to use of the class attribute, and in this case the full
14636 -- type must also be tagged. This Ada 95 usage is deprecated in favor
14637 -- of incomplete tagged declarations, but we check for it.
14639 if Is_Type (Prev)
14640 and then (Is_Tagged_Type (Prev)
14641 or else Present (Class_Wide_Type (Prev)))
14642 then
14643 -- Ada 2012 (AI05-0162): A private type may be the completion of
14644 -- an incomplete type
14646 if Ada_Version >= Ada_2012
14647 and then Is_Incomplete_Type (Prev)
14648 and then Nkind_In (N, N_Private_Type_Declaration,
14649 N_Private_Extension_Declaration)
14650 then
14651 -- No need to check private extensions since they are tagged
14653 if Nkind (N) = N_Private_Type_Declaration
14654 and then not Tagged_Present (N)
14655 then
14656 Tag_Mismatch;
14657 end if;
14659 -- The full declaration is either a tagged type (including
14660 -- a synchronized type that implements interfaces) or a
14661 -- type extension, otherwise this is an error.
14663 elsif Nkind_In (N, N_Task_Type_Declaration,
14664 N_Protected_Type_Declaration)
14665 then
14666 if No (Interface_List (N))
14667 and then not Error_Posted (N)
14668 then
14669 Tag_Mismatch;
14670 end if;
14672 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
14674 -- Indicate that the previous declaration (tagged incomplete
14675 -- or private declaration) requires the same on the full one.
14677 if not Tagged_Present (Type_Definition (N)) then
14678 Tag_Mismatch;
14679 Set_Is_Tagged_Type (Id);
14680 end if;
14682 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
14683 if No (Record_Extension_Part (Type_Definition (N))) then
14684 Error_Msg_NE
14685 ("full declaration of } must be a record extension",
14686 Prev, Id);
14688 -- Set some attributes to produce a usable full view
14690 Set_Is_Tagged_Type (Id);
14691 end if;
14693 else
14694 Tag_Mismatch;
14695 end if;
14696 end if;
14698 return New_Id;
14699 end if;
14700 end Find_Type_Name;
14702 -------------------------
14703 -- Find_Type_Of_Object --
14704 -------------------------
14706 function Find_Type_Of_Object
14707 (Obj_Def : Node_Id;
14708 Related_Nod : Node_Id) return Entity_Id
14710 Def_Kind : constant Node_Kind := Nkind (Obj_Def);
14711 P : Node_Id := Parent (Obj_Def);
14712 T : Entity_Id;
14713 Nam : Name_Id;
14715 begin
14716 -- If the parent is a component_definition node we climb to the
14717 -- component_declaration node
14719 if Nkind (P) = N_Component_Definition then
14720 P := Parent (P);
14721 end if;
14723 -- Case of an anonymous array subtype
14725 if Nkind_In (Def_Kind, N_Constrained_Array_Definition,
14726 N_Unconstrained_Array_Definition)
14727 then
14728 T := Empty;
14729 Array_Type_Declaration (T, Obj_Def);
14731 -- Create an explicit subtype whenever possible
14733 elsif Nkind (P) /= N_Component_Declaration
14734 and then Def_Kind = N_Subtype_Indication
14735 then
14736 -- Base name of subtype on object name, which will be unique in
14737 -- the current scope.
14739 -- If this is a duplicate declaration, return base type, to avoid
14740 -- generating duplicate anonymous types.
14742 if Error_Posted (P) then
14743 Analyze (Subtype_Mark (Obj_Def));
14744 return Entity (Subtype_Mark (Obj_Def));
14745 end if;
14747 Nam :=
14748 New_External_Name
14749 (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
14751 T := Make_Defining_Identifier (Sloc (P), Nam);
14753 Insert_Action (Obj_Def,
14754 Make_Subtype_Declaration (Sloc (P),
14755 Defining_Identifier => T,
14756 Subtype_Indication => Relocate_Node (Obj_Def)));
14758 -- This subtype may need freezing, and this will not be done
14759 -- automatically if the object declaration is not in declarative
14760 -- part. Since this is an object declaration, the type cannot always
14761 -- be frozen here. Deferred constants do not freeze their type
14762 -- (which often enough will be private).
14764 if Nkind (P) = N_Object_Declaration
14765 and then Constant_Present (P)
14766 and then No (Expression (P))
14767 then
14768 null;
14769 else
14770 Insert_Actions (Obj_Def, Freeze_Entity (T, P));
14771 end if;
14773 -- Ada 2005 AI-406: the object definition in an object declaration
14774 -- can be an access definition.
14776 elsif Def_Kind = N_Access_Definition then
14777 T := Access_Definition (Related_Nod, Obj_Def);
14778 Set_Is_Local_Anonymous_Access (T);
14780 -- Otherwise, the object definition is just a subtype_mark
14782 else
14783 T := Process_Subtype (Obj_Def, Related_Nod);
14784 end if;
14786 return T;
14787 end Find_Type_Of_Object;
14789 --------------------------------
14790 -- Find_Type_Of_Subtype_Indic --
14791 --------------------------------
14793 function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
14794 Typ : Entity_Id;
14796 begin
14797 -- Case of subtype mark with a constraint
14799 if Nkind (S) = N_Subtype_Indication then
14800 Find_Type (Subtype_Mark (S));
14801 Typ := Entity (Subtype_Mark (S));
14803 if not
14804 Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
14805 then
14806 Error_Msg_N
14807 ("incorrect constraint for this kind of type", Constraint (S));
14808 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
14809 end if;
14811 -- Otherwise we have a subtype mark without a constraint
14813 elsif Error_Posted (S) then
14814 Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
14815 return Any_Type;
14817 else
14818 Find_Type (S);
14819 Typ := Entity (S);
14820 end if;
14822 -- Check No_Wide_Characters restriction
14824 Check_Wide_Character_Restriction (Typ, S);
14826 return Typ;
14827 end Find_Type_Of_Subtype_Indic;
14829 -------------------------------------
14830 -- Floating_Point_Type_Declaration --
14831 -------------------------------------
14833 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
14834 Digs : constant Node_Id := Digits_Expression (Def);
14835 Digs_Val : Uint;
14836 Base_Typ : Entity_Id;
14837 Implicit_Base : Entity_Id;
14838 Bound : Node_Id;
14840 function Can_Derive_From (E : Entity_Id) return Boolean;
14841 -- Find if given digits value allows derivation from specified type
14843 ---------------------
14844 -- Can_Derive_From --
14845 ---------------------
14847 function Can_Derive_From (E : Entity_Id) return Boolean is
14848 Spec : constant Entity_Id := Real_Range_Specification (Def);
14850 begin
14851 if Digs_Val > Digits_Value (E) then
14852 return False;
14853 end if;
14855 if Present (Spec) then
14856 if Expr_Value_R (Type_Low_Bound (E)) >
14857 Expr_Value_R (Low_Bound (Spec))
14858 then
14859 return False;
14860 end if;
14862 if Expr_Value_R (Type_High_Bound (E)) <
14863 Expr_Value_R (High_Bound (Spec))
14864 then
14865 return False;
14866 end if;
14867 end if;
14869 return True;
14870 end Can_Derive_From;
14872 -- Start of processing for Floating_Point_Type_Declaration
14874 begin
14875 Check_Restriction (No_Floating_Point, Def);
14877 -- Create an implicit base type
14879 Implicit_Base :=
14880 Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
14882 -- Analyze and verify digits value
14884 Analyze_And_Resolve (Digs, Any_Integer);
14885 Check_Digits_Expression (Digs);
14886 Digs_Val := Expr_Value (Digs);
14888 -- Process possible range spec and find correct type to derive from
14890 Process_Real_Range_Specification (Def);
14892 if Can_Derive_From (Standard_Short_Float) then
14893 Base_Typ := Standard_Short_Float;
14894 elsif Can_Derive_From (Standard_Float) then
14895 Base_Typ := Standard_Float;
14896 elsif Can_Derive_From (Standard_Long_Float) then
14897 Base_Typ := Standard_Long_Float;
14898 elsif Can_Derive_From (Standard_Long_Long_Float) then
14899 Base_Typ := Standard_Long_Long_Float;
14901 -- If we can't derive from any existing type, use long_long_float
14902 -- and give appropriate message explaining the problem.
14904 else
14905 Base_Typ := Standard_Long_Long_Float;
14907 if Digs_Val >= Digits_Value (Standard_Long_Long_Float) then
14908 Error_Msg_Uint_1 := Digits_Value (Standard_Long_Long_Float);
14909 Error_Msg_N ("digits value out of range, maximum is ^", Digs);
14911 else
14912 Error_Msg_N
14913 ("range too large for any predefined type",
14914 Real_Range_Specification (Def));
14915 end if;
14916 end if;
14918 -- If there are bounds given in the declaration use them as the bounds
14919 -- of the type, otherwise use the bounds of the predefined base type
14920 -- that was chosen based on the Digits value.
14922 if Present (Real_Range_Specification (Def)) then
14923 Set_Scalar_Range (T, Real_Range_Specification (Def));
14924 Set_Is_Constrained (T);
14926 -- The bounds of this range must be converted to machine numbers
14927 -- in accordance with RM 4.9(38).
14929 Bound := Type_Low_Bound (T);
14931 if Nkind (Bound) = N_Real_Literal then
14932 Set_Realval
14933 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
14934 Set_Is_Machine_Number (Bound);
14935 end if;
14937 Bound := Type_High_Bound (T);
14939 if Nkind (Bound) = N_Real_Literal then
14940 Set_Realval
14941 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
14942 Set_Is_Machine_Number (Bound);
14943 end if;
14945 else
14946 Set_Scalar_Range (T, Scalar_Range (Base_Typ));
14947 end if;
14949 -- Complete definition of implicit base and declared first subtype
14951 Set_Etype (Implicit_Base, Base_Typ);
14953 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
14954 Set_Size_Info (Implicit_Base, (Base_Typ));
14955 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
14956 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
14957 Set_Digits_Value (Implicit_Base, Digits_Value (Base_Typ));
14958 Set_Float_Rep (Implicit_Base, Float_Rep (Base_Typ));
14960 Set_Ekind (T, E_Floating_Point_Subtype);
14961 Set_Etype (T, Implicit_Base);
14963 Set_Size_Info (T, (Implicit_Base));
14964 Set_RM_Size (T, RM_Size (Implicit_Base));
14965 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
14966 Set_Digits_Value (T, Digs_Val);
14967 end Floating_Point_Type_Declaration;
14969 ----------------------------
14970 -- Get_Discriminant_Value --
14971 ----------------------------
14973 -- This is the situation:
14975 -- There is a non-derived type
14977 -- type T0 (Dx, Dy, Dz...)
14979 -- There are zero or more levels of derivation, with each derivation
14980 -- either purely inheriting the discriminants, or defining its own.
14982 -- type Ti is new Ti-1
14983 -- or
14984 -- type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
14985 -- or
14986 -- subtype Ti is ...
14988 -- The subtype issue is avoided by the use of Original_Record_Component,
14989 -- and the fact that derived subtypes also derive the constraints.
14991 -- This chain leads back from
14993 -- Typ_For_Constraint
14995 -- Typ_For_Constraint has discriminants, and the value for each
14996 -- discriminant is given by its corresponding Elmt of Constraints.
14998 -- Discriminant is some discriminant in this hierarchy
15000 -- We need to return its value
15002 -- We do this by recursively searching each level, and looking for
15003 -- Discriminant. Once we get to the bottom, we start backing up
15004 -- returning the value for it which may in turn be a discriminant
15005 -- further up, so on the backup we continue the substitution.
15007 function Get_Discriminant_Value
15008 (Discriminant : Entity_Id;
15009 Typ_For_Constraint : Entity_Id;
15010 Constraint : Elist_Id) return Node_Id
15012 function Search_Derivation_Levels
15013 (Ti : Entity_Id;
15014 Discrim_Values : Elist_Id;
15015 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
15016 -- This is the routine that performs the recursive search of levels
15017 -- as described above.
15019 ------------------------------
15020 -- Search_Derivation_Levels --
15021 ------------------------------
15023 function Search_Derivation_Levels
15024 (Ti : Entity_Id;
15025 Discrim_Values : Elist_Id;
15026 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
15028 Assoc : Elmt_Id;
15029 Disc : Entity_Id;
15030 Result : Node_Or_Entity_Id;
15031 Result_Entity : Node_Id;
15033 begin
15034 -- If inappropriate type, return Error, this happens only in
15035 -- cascaded error situations, and we want to avoid a blow up.
15037 if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
15038 return Error;
15039 end if;
15041 -- Look deeper if possible. Use Stored_Constraints only for
15042 -- untagged types. For tagged types use the given constraint.
15043 -- This asymmetry needs explanation???
15045 if not Stored_Discrim_Values
15046 and then Present (Stored_Constraint (Ti))
15047 and then not Is_Tagged_Type (Ti)
15048 then
15049 Result :=
15050 Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
15051 else
15052 declare
15053 Td : constant Entity_Id := Etype (Ti);
15055 begin
15056 if Td = Ti then
15057 Result := Discriminant;
15059 else
15060 if Present (Stored_Constraint (Ti)) then
15061 Result :=
15062 Search_Derivation_Levels
15063 (Td, Stored_Constraint (Ti), True);
15064 else
15065 Result :=
15066 Search_Derivation_Levels
15067 (Td, Discrim_Values, Stored_Discrim_Values);
15068 end if;
15069 end if;
15070 end;
15071 end if;
15073 -- Extra underlying places to search, if not found above. For
15074 -- concurrent types, the relevant discriminant appears in the
15075 -- corresponding record. For a type derived from a private type
15076 -- without discriminant, the full view inherits the discriminants
15077 -- of the full view of the parent.
15079 if Result = Discriminant then
15080 if Is_Concurrent_Type (Ti)
15081 and then Present (Corresponding_Record_Type (Ti))
15082 then
15083 Result :=
15084 Search_Derivation_Levels (
15085 Corresponding_Record_Type (Ti),
15086 Discrim_Values,
15087 Stored_Discrim_Values);
15089 elsif Is_Private_Type (Ti)
15090 and then not Has_Discriminants (Ti)
15091 and then Present (Full_View (Ti))
15092 and then Etype (Full_View (Ti)) /= Ti
15093 then
15094 Result :=
15095 Search_Derivation_Levels (
15096 Full_View (Ti),
15097 Discrim_Values,
15098 Stored_Discrim_Values);
15099 end if;
15100 end if;
15102 -- If Result is not a (reference to a) discriminant, return it,
15103 -- otherwise set Result_Entity to the discriminant.
15105 if Nkind (Result) = N_Defining_Identifier then
15106 pragma Assert (Result = Discriminant);
15107 Result_Entity := Result;
15109 else
15110 if not Denotes_Discriminant (Result) then
15111 return Result;
15112 end if;
15114 Result_Entity := Entity (Result);
15115 end if;
15117 -- See if this level of derivation actually has discriminants
15118 -- because tagged derivations can add them, hence the lower
15119 -- levels need not have any.
15121 if not Has_Discriminants (Ti) then
15122 return Result;
15123 end if;
15125 -- Scan Ti's discriminants for Result_Entity,
15126 -- and return its corresponding value, if any.
15128 Result_Entity := Original_Record_Component (Result_Entity);
15130 Assoc := First_Elmt (Discrim_Values);
15132 if Stored_Discrim_Values then
15133 Disc := First_Stored_Discriminant (Ti);
15134 else
15135 Disc := First_Discriminant (Ti);
15136 end if;
15138 while Present (Disc) loop
15139 pragma Assert (Present (Assoc));
15141 if Original_Record_Component (Disc) = Result_Entity then
15142 return Node (Assoc);
15143 end if;
15145 Next_Elmt (Assoc);
15147 if Stored_Discrim_Values then
15148 Next_Stored_Discriminant (Disc);
15149 else
15150 Next_Discriminant (Disc);
15151 end if;
15152 end loop;
15154 -- Could not find it
15156 return Result;
15157 end Search_Derivation_Levels;
15159 -- Local Variables
15161 Result : Node_Or_Entity_Id;
15163 -- Start of processing for Get_Discriminant_Value
15165 begin
15166 -- ??? This routine is a gigantic mess and will be deleted. For the
15167 -- time being just test for the trivial case before calling recurse.
15169 if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
15170 declare
15171 D : Entity_Id;
15172 E : Elmt_Id;
15174 begin
15175 D := First_Discriminant (Typ_For_Constraint);
15176 E := First_Elmt (Constraint);
15177 while Present (D) loop
15178 if Chars (D) = Chars (Discriminant) then
15179 return Node (E);
15180 end if;
15182 Next_Discriminant (D);
15183 Next_Elmt (E);
15184 end loop;
15185 end;
15186 end if;
15188 Result := Search_Derivation_Levels
15189 (Typ_For_Constraint, Constraint, False);
15191 -- ??? hack to disappear when this routine is gone
15193 if Nkind (Result) = N_Defining_Identifier then
15194 declare
15195 D : Entity_Id;
15196 E : Elmt_Id;
15198 begin
15199 D := First_Discriminant (Typ_For_Constraint);
15200 E := First_Elmt (Constraint);
15201 while Present (D) loop
15202 if Corresponding_Discriminant (D) = Discriminant then
15203 return Node (E);
15204 end if;
15206 Next_Discriminant (D);
15207 Next_Elmt (E);
15208 end loop;
15209 end;
15210 end if;
15212 pragma Assert (Nkind (Result) /= N_Defining_Identifier);
15213 return Result;
15214 end Get_Discriminant_Value;
15216 --------------------------
15217 -- Has_Range_Constraint --
15218 --------------------------
15220 function Has_Range_Constraint (N : Node_Id) return Boolean is
15221 C : constant Node_Id := Constraint (N);
15223 begin
15224 if Nkind (C) = N_Range_Constraint then
15225 return True;
15227 elsif Nkind (C) = N_Digits_Constraint then
15228 return
15229 Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
15230 or else
15231 Present (Range_Constraint (C));
15233 elsif Nkind (C) = N_Delta_Constraint then
15234 return Present (Range_Constraint (C));
15236 else
15237 return False;
15238 end if;
15239 end Has_Range_Constraint;
15241 ------------------------
15242 -- Inherit_Components --
15243 ------------------------
15245 function Inherit_Components
15246 (N : Node_Id;
15247 Parent_Base : Entity_Id;
15248 Derived_Base : Entity_Id;
15249 Is_Tagged : Boolean;
15250 Inherit_Discr : Boolean;
15251 Discs : Elist_Id) return Elist_Id
15253 Assoc_List : constant Elist_Id := New_Elmt_List;
15255 procedure Inherit_Component
15256 (Old_C : Entity_Id;
15257 Plain_Discrim : Boolean := False;
15258 Stored_Discrim : Boolean := False);
15259 -- Inherits component Old_C from Parent_Base to the Derived_Base. If
15260 -- Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
15261 -- True, Old_C is a stored discriminant. If they are both false then
15262 -- Old_C is a regular component.
15264 -----------------------
15265 -- Inherit_Component --
15266 -----------------------
15268 procedure Inherit_Component
15269 (Old_C : Entity_Id;
15270 Plain_Discrim : Boolean := False;
15271 Stored_Discrim : Boolean := False)
15273 New_C : constant Entity_Id := New_Copy (Old_C);
15275 Discrim : Entity_Id;
15276 Corr_Discrim : Entity_Id;
15278 begin
15279 pragma Assert (not Is_Tagged or else not Stored_Discrim);
15281 Set_Parent (New_C, Parent (Old_C));
15283 -- Regular discriminants and components must be inserted in the scope
15284 -- of the Derived_Base. Do it here.
15286 if not Stored_Discrim then
15287 Enter_Name (New_C);
15288 end if;
15290 -- For tagged types the Original_Record_Component must point to
15291 -- whatever this field was pointing to in the parent type. This has
15292 -- already been achieved by the call to New_Copy above.
15294 if not Is_Tagged then
15295 Set_Original_Record_Component (New_C, New_C);
15296 end if;
15298 -- If we have inherited a component then see if its Etype contains
15299 -- references to Parent_Base discriminants. In this case, replace
15300 -- these references with the constraints given in Discs. We do not
15301 -- do this for the partial view of private types because this is
15302 -- not needed (only the components of the full view will be used
15303 -- for code generation) and cause problem. We also avoid this
15304 -- transformation in some error situations.
15306 if Ekind (New_C) = E_Component then
15307 if (Is_Private_Type (Derived_Base)
15308 and then not Is_Generic_Type (Derived_Base))
15309 or else (Is_Empty_Elmt_List (Discs)
15310 and then not Expander_Active)
15311 then
15312 Set_Etype (New_C, Etype (Old_C));
15314 else
15315 -- The current component introduces a circularity of the
15316 -- following kind:
15318 -- limited with Pack_2;
15319 -- package Pack_1 is
15320 -- type T_1 is tagged record
15321 -- Comp : access Pack_2.T_2;
15322 -- ...
15323 -- end record;
15324 -- end Pack_1;
15326 -- with Pack_1;
15327 -- package Pack_2 is
15328 -- type T_2 is new Pack_1.T_1 with ...;
15329 -- end Pack_2;
15331 Set_Etype
15332 (New_C,
15333 Constrain_Component_Type
15334 (Old_C, Derived_Base, N, Parent_Base, Discs));
15335 end if;
15336 end if;
15338 -- In derived tagged types it is illegal to reference a non
15339 -- discriminant component in the parent type. To catch this, mark
15340 -- these components with an Ekind of E_Void. This will be reset in
15341 -- Record_Type_Definition after processing the record extension of
15342 -- the derived type.
15344 -- If the declaration is a private extension, there is no further
15345 -- record extension to process, and the components retain their
15346 -- current kind, because they are visible at this point.
15348 if Is_Tagged and then Ekind (New_C) = E_Component
15349 and then Nkind (N) /= N_Private_Extension_Declaration
15350 then
15351 Set_Ekind (New_C, E_Void);
15352 end if;
15354 if Plain_Discrim then
15355 Set_Corresponding_Discriminant (New_C, Old_C);
15356 Build_Discriminal (New_C);
15358 -- If we are explicitly inheriting a stored discriminant it will be
15359 -- completely hidden.
15361 elsif Stored_Discrim then
15362 Set_Corresponding_Discriminant (New_C, Empty);
15363 Set_Discriminal (New_C, Empty);
15364 Set_Is_Completely_Hidden (New_C);
15366 -- Set the Original_Record_Component of each discriminant in the
15367 -- derived base to point to the corresponding stored that we just
15368 -- created.
15370 Discrim := First_Discriminant (Derived_Base);
15371 while Present (Discrim) loop
15372 Corr_Discrim := Corresponding_Discriminant (Discrim);
15374 -- Corr_Discrim could be missing in an error situation
15376 if Present (Corr_Discrim)
15377 and then Original_Record_Component (Corr_Discrim) = Old_C
15378 then
15379 Set_Original_Record_Component (Discrim, New_C);
15380 end if;
15382 Next_Discriminant (Discrim);
15383 end loop;
15385 Append_Entity (New_C, Derived_Base);
15386 end if;
15388 if not Is_Tagged then
15389 Append_Elmt (Old_C, Assoc_List);
15390 Append_Elmt (New_C, Assoc_List);
15391 end if;
15392 end Inherit_Component;
15394 -- Variables local to Inherit_Component
15396 Loc : constant Source_Ptr := Sloc (N);
15398 Parent_Discrim : Entity_Id;
15399 Stored_Discrim : Entity_Id;
15400 D : Entity_Id;
15401 Component : Entity_Id;
15403 -- Start of processing for Inherit_Components
15405 begin
15406 if not Is_Tagged then
15407 Append_Elmt (Parent_Base, Assoc_List);
15408 Append_Elmt (Derived_Base, Assoc_List);
15409 end if;
15411 -- Inherit parent discriminants if needed
15413 if Inherit_Discr then
15414 Parent_Discrim := First_Discriminant (Parent_Base);
15415 while Present (Parent_Discrim) loop
15416 Inherit_Component (Parent_Discrim, Plain_Discrim => True);
15417 Next_Discriminant (Parent_Discrim);
15418 end loop;
15419 end if;
15421 -- Create explicit stored discrims for untagged types when necessary
15423 if not Has_Unknown_Discriminants (Derived_Base)
15424 and then Has_Discriminants (Parent_Base)
15425 and then not Is_Tagged
15426 and then
15427 (not Inherit_Discr
15428 or else First_Discriminant (Parent_Base) /=
15429 First_Stored_Discriminant (Parent_Base))
15430 then
15431 Stored_Discrim := First_Stored_Discriminant (Parent_Base);
15432 while Present (Stored_Discrim) loop
15433 Inherit_Component (Stored_Discrim, Stored_Discrim => True);
15434 Next_Stored_Discriminant (Stored_Discrim);
15435 end loop;
15436 end if;
15438 -- See if we can apply the second transformation for derived types, as
15439 -- explained in point 6. in the comments above Build_Derived_Record_Type
15440 -- This is achieved by appending Derived_Base discriminants into Discs,
15441 -- which has the side effect of returning a non empty Discs list to the
15442 -- caller of Inherit_Components, which is what we want. This must be
15443 -- done for private derived types if there are explicit stored
15444 -- discriminants, to ensure that we can retrieve the values of the
15445 -- constraints provided in the ancestors.
15447 if Inherit_Discr
15448 and then Is_Empty_Elmt_List (Discs)
15449 and then Present (First_Discriminant (Derived_Base))
15450 and then
15451 (not Is_Private_Type (Derived_Base)
15452 or else Is_Completely_Hidden
15453 (First_Stored_Discriminant (Derived_Base))
15454 or else Is_Generic_Type (Derived_Base))
15455 then
15456 D := First_Discriminant (Derived_Base);
15457 while Present (D) loop
15458 Append_Elmt (New_Reference_To (D, Loc), Discs);
15459 Next_Discriminant (D);
15460 end loop;
15461 end if;
15463 -- Finally, inherit non-discriminant components unless they are not
15464 -- visible because defined or inherited from the full view of the
15465 -- parent. Don't inherit the _parent field of the parent type.
15467 Component := First_Entity (Parent_Base);
15468 while Present (Component) loop
15470 -- Ada 2005 (AI-251): Do not inherit components associated with
15471 -- secondary tags of the parent.
15473 if Ekind (Component) = E_Component
15474 and then Present (Related_Type (Component))
15475 then
15476 null;
15478 elsif Ekind (Component) /= E_Component
15479 or else Chars (Component) = Name_uParent
15480 then
15481 null;
15483 -- If the derived type is within the parent type's declarative
15484 -- region, then the components can still be inherited even though
15485 -- they aren't visible at this point. This can occur for cases
15486 -- such as within public child units where the components must
15487 -- become visible upon entering the child unit's private part.
15489 elsif not Is_Visible_Component (Component)
15490 and then not In_Open_Scopes (Scope (Parent_Base))
15491 then
15492 null;
15494 elsif Ekind_In (Derived_Base, E_Private_Type,
15495 E_Limited_Private_Type)
15496 then
15497 null;
15499 else
15500 Inherit_Component (Component);
15501 end if;
15503 Next_Entity (Component);
15504 end loop;
15506 -- For tagged derived types, inherited discriminants cannot be used in
15507 -- component declarations of the record extension part. To achieve this
15508 -- we mark the inherited discriminants as not visible.
15510 if Is_Tagged and then Inherit_Discr then
15511 D := First_Discriminant (Derived_Base);
15512 while Present (D) loop
15513 Set_Is_Immediately_Visible (D, False);
15514 Next_Discriminant (D);
15515 end loop;
15516 end if;
15518 return Assoc_List;
15519 end Inherit_Components;
15521 -----------------------
15522 -- Is_Null_Extension --
15523 -----------------------
15525 function Is_Null_Extension (T : Entity_Id) return Boolean is
15526 Type_Decl : constant Node_Id := Parent (Base_Type (T));
15527 Comp_List : Node_Id;
15528 Comp : Node_Id;
15530 begin
15531 if Nkind (Type_Decl) /= N_Full_Type_Declaration
15532 or else not Is_Tagged_Type (T)
15533 or else Nkind (Type_Definition (Type_Decl)) /=
15534 N_Derived_Type_Definition
15535 or else No (Record_Extension_Part (Type_Definition (Type_Decl)))
15536 then
15537 return False;
15538 end if;
15540 Comp_List :=
15541 Component_List (Record_Extension_Part (Type_Definition (Type_Decl)));
15543 if Present (Discriminant_Specifications (Type_Decl)) then
15544 return False;
15546 elsif Present (Comp_List)
15547 and then Is_Non_Empty_List (Component_Items (Comp_List))
15548 then
15549 Comp := First (Component_Items (Comp_List));
15551 -- Only user-defined components are relevant. The component list
15552 -- may also contain a parent component and internal components
15553 -- corresponding to secondary tags, but these do not determine
15554 -- whether this is a null extension.
15556 while Present (Comp) loop
15557 if Comes_From_Source (Comp) then
15558 return False;
15559 end if;
15561 Next (Comp);
15562 end loop;
15564 return True;
15565 else
15566 return True;
15567 end if;
15568 end Is_Null_Extension;
15570 ------------------------------
15571 -- Is_Valid_Constraint_Kind --
15572 ------------------------------
15574 function Is_Valid_Constraint_Kind
15575 (T_Kind : Type_Kind;
15576 Constraint_Kind : Node_Kind) return Boolean
15578 begin
15579 case T_Kind is
15580 when Enumeration_Kind |
15581 Integer_Kind =>
15582 return Constraint_Kind = N_Range_Constraint;
15584 when Decimal_Fixed_Point_Kind =>
15585 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
15586 N_Range_Constraint);
15588 when Ordinary_Fixed_Point_Kind =>
15589 return Nkind_In (Constraint_Kind, N_Delta_Constraint,
15590 N_Range_Constraint);
15592 when Float_Kind =>
15593 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
15594 N_Range_Constraint);
15596 when Access_Kind |
15597 Array_Kind |
15598 E_Record_Type |
15599 E_Record_Subtype |
15600 Class_Wide_Kind |
15601 E_Incomplete_Type |
15602 Private_Kind |
15603 Concurrent_Kind =>
15604 return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
15606 when others =>
15607 return True; -- Error will be detected later
15608 end case;
15609 end Is_Valid_Constraint_Kind;
15611 --------------------------
15612 -- Is_Visible_Component --
15613 --------------------------
15615 function Is_Visible_Component (C : Entity_Id) return Boolean is
15616 Original_Comp : Entity_Id := Empty;
15617 Original_Scope : Entity_Id;
15618 Type_Scope : Entity_Id;
15620 function Is_Local_Type (Typ : Entity_Id) return Boolean;
15621 -- Check whether parent type of inherited component is declared locally,
15622 -- possibly within a nested package or instance. The current scope is
15623 -- the derived record itself.
15625 -------------------
15626 -- Is_Local_Type --
15627 -------------------
15629 function Is_Local_Type (Typ : Entity_Id) return Boolean is
15630 Scop : Entity_Id;
15632 begin
15633 Scop := Scope (Typ);
15634 while Present (Scop)
15635 and then Scop /= Standard_Standard
15636 loop
15637 if Scop = Scope (Current_Scope) then
15638 return True;
15639 end if;
15641 Scop := Scope (Scop);
15642 end loop;
15644 return False;
15645 end Is_Local_Type;
15647 -- Start of processing for Is_Visible_Component
15649 begin
15650 if Ekind_In (C, E_Component, E_Discriminant) then
15651 Original_Comp := Original_Record_Component (C);
15652 end if;
15654 if No (Original_Comp) then
15656 -- Premature usage, or previous error
15658 return False;
15660 else
15661 Original_Scope := Scope (Original_Comp);
15662 Type_Scope := Scope (Base_Type (Scope (C)));
15663 end if;
15665 -- This test only concerns tagged types
15667 if not Is_Tagged_Type (Original_Scope) then
15668 return True;
15670 -- If it is _Parent or _Tag, there is no visibility issue
15672 elsif not Comes_From_Source (Original_Comp) then
15673 return True;
15675 -- If we are in the body of an instantiation, the component is visible
15676 -- even when the parent type (possibly defined in an enclosing unit or
15677 -- in a parent unit) might not.
15679 elsif In_Instance_Body then
15680 return True;
15682 -- Discriminants are always visible
15684 elsif Ekind (Original_Comp) = E_Discriminant
15685 and then not Has_Unknown_Discriminants (Original_Scope)
15686 then
15687 return True;
15689 -- If the component has been declared in an ancestor which is currently
15690 -- a private type, then it is not visible. The same applies if the
15691 -- component's containing type is not in an open scope and the original
15692 -- component's enclosing type is a visible full view of a private type
15693 -- (which can occur in cases where an attempt is being made to reference
15694 -- a component in a sibling package that is inherited from a visible
15695 -- component of a type in an ancestor package; the component in the
15696 -- sibling package should not be visible even though the component it
15697 -- inherited from is visible). This does not apply however in the case
15698 -- where the scope of the type is a private child unit, or when the
15699 -- parent comes from a local package in which the ancestor is currently
15700 -- visible. The latter suppression of visibility is needed for cases
15701 -- that are tested in B730006.
15703 elsif Is_Private_Type (Original_Scope)
15704 or else
15705 (not Is_Private_Descendant (Type_Scope)
15706 and then not In_Open_Scopes (Type_Scope)
15707 and then Has_Private_Declaration (Original_Scope))
15708 then
15709 -- If the type derives from an entity in a formal package, there
15710 -- are no additional visible components.
15712 if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
15713 N_Formal_Package_Declaration
15714 then
15715 return False;
15717 -- if we are not in the private part of the current package, there
15718 -- are no additional visible components.
15720 elsif Ekind (Scope (Current_Scope)) = E_Package
15721 and then not In_Private_Part (Scope (Current_Scope))
15722 then
15723 return False;
15724 else
15725 return
15726 Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
15727 and then In_Open_Scopes (Scope (Original_Scope))
15728 and then Is_Local_Type (Type_Scope);
15729 end if;
15731 -- There is another weird way in which a component may be invisible
15732 -- when the private and the full view are not derived from the same
15733 -- ancestor. Here is an example :
15735 -- type A1 is tagged record F1 : integer; end record;
15736 -- type A2 is new A1 with record F2 : integer; end record;
15737 -- type T is new A1 with private;
15738 -- private
15739 -- type T is new A2 with null record;
15741 -- In this case, the full view of T inherits F1 and F2 but the private
15742 -- view inherits only F1
15744 else
15745 declare
15746 Ancestor : Entity_Id := Scope (C);
15748 begin
15749 loop
15750 if Ancestor = Original_Scope then
15751 return True;
15752 elsif Ancestor = Etype (Ancestor) then
15753 return False;
15754 end if;
15756 Ancestor := Etype (Ancestor);
15757 end loop;
15758 end;
15759 end if;
15760 end Is_Visible_Component;
15762 --------------------------
15763 -- Make_Class_Wide_Type --
15764 --------------------------
15766 procedure Make_Class_Wide_Type (T : Entity_Id) is
15767 CW_Type : Entity_Id;
15768 CW_Name : Name_Id;
15769 Next_E : Entity_Id;
15771 begin
15772 -- The class wide type can have been defined by the partial view, in
15773 -- which case everything is already done.
15775 if Present (Class_Wide_Type (T)) then
15776 return;
15777 end if;
15779 CW_Type :=
15780 New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
15782 -- Inherit root type characteristics
15784 CW_Name := Chars (CW_Type);
15785 Next_E := Next_Entity (CW_Type);
15786 Copy_Node (T, CW_Type);
15787 Set_Comes_From_Source (CW_Type, False);
15788 Set_Chars (CW_Type, CW_Name);
15789 Set_Parent (CW_Type, Parent (T));
15790 Set_Next_Entity (CW_Type, Next_E);
15792 -- Ensure we have a new freeze node for the class-wide type. The partial
15793 -- view may have freeze action of its own, requiring a proper freeze
15794 -- node, and the same freeze node cannot be shared between the two
15795 -- types.
15797 Set_Has_Delayed_Freeze (CW_Type);
15798 Set_Freeze_Node (CW_Type, Empty);
15800 -- Customize the class-wide type: It has no prim. op., it cannot be
15801 -- abstract and its Etype points back to the specific root type.
15803 Set_Ekind (CW_Type, E_Class_Wide_Type);
15804 Set_Is_Tagged_Type (CW_Type, True);
15805 Set_Direct_Primitive_Operations (CW_Type, New_Elmt_List);
15806 Set_Is_Abstract_Type (CW_Type, False);
15807 Set_Is_Constrained (CW_Type, False);
15808 Set_Is_First_Subtype (CW_Type, Is_First_Subtype (T));
15810 if Ekind (T) = E_Class_Wide_Subtype then
15811 Set_Etype (CW_Type, Etype (Base_Type (T)));
15812 else
15813 Set_Etype (CW_Type, T);
15814 end if;
15816 -- If this is the class_wide type of a constrained subtype, it does
15817 -- not have discriminants.
15819 Set_Has_Discriminants (CW_Type,
15820 Has_Discriminants (T) and then not Is_Constrained (T));
15822 Set_Has_Unknown_Discriminants (CW_Type, True);
15823 Set_Class_Wide_Type (T, CW_Type);
15824 Set_Equivalent_Type (CW_Type, Empty);
15826 -- The class-wide type of a class-wide type is itself (RM 3.9(14))
15828 Set_Class_Wide_Type (CW_Type, CW_Type);
15829 end Make_Class_Wide_Type;
15831 ----------------
15832 -- Make_Index --
15833 ----------------
15835 procedure Make_Index
15836 (I : Node_Id;
15837 Related_Nod : Node_Id;
15838 Related_Id : Entity_Id := Empty;
15839 Suffix_Index : Nat := 1)
15841 R : Node_Id;
15842 T : Entity_Id;
15843 Def_Id : Entity_Id := Empty;
15844 Found : Boolean := False;
15846 begin
15847 -- For a discrete range used in a constrained array definition and
15848 -- defined by a range, an implicit conversion to the predefined type
15849 -- INTEGER is assumed if each bound is either a numeric literal, a named
15850 -- number, or an attribute, and the type of both bounds (prior to the
15851 -- implicit conversion) is the type universal_integer. Otherwise, both
15852 -- bounds must be of the same discrete type, other than universal
15853 -- integer; this type must be determinable independently of the
15854 -- context, but using the fact that the type must be discrete and that
15855 -- both bounds must have the same type.
15857 -- Character literals also have a universal type in the absence of
15858 -- of additional context, and are resolved to Standard_Character.
15860 if Nkind (I) = N_Range then
15862 -- The index is given by a range constraint. The bounds are known
15863 -- to be of a consistent type.
15865 if not Is_Overloaded (I) then
15866 T := Etype (I);
15868 -- For universal bounds, choose the specific predefined type
15870 if T = Universal_Integer then
15871 T := Standard_Integer;
15873 elsif T = Any_Character then
15874 Ambiguous_Character (Low_Bound (I));
15876 T := Standard_Character;
15877 end if;
15879 -- The node may be overloaded because some user-defined operators
15880 -- are available, but if a universal interpretation exists it is
15881 -- also the selected one.
15883 elsif Universal_Interpretation (I) = Universal_Integer then
15884 T := Standard_Integer;
15886 else
15887 T := Any_Type;
15889 declare
15890 Ind : Interp_Index;
15891 It : Interp;
15893 begin
15894 Get_First_Interp (I, Ind, It);
15895 while Present (It.Typ) loop
15896 if Is_Discrete_Type (It.Typ) then
15898 if Found
15899 and then not Covers (It.Typ, T)
15900 and then not Covers (T, It.Typ)
15901 then
15902 Error_Msg_N ("ambiguous bounds in discrete range", I);
15903 exit;
15904 else
15905 T := It.Typ;
15906 Found := True;
15907 end if;
15908 end if;
15910 Get_Next_Interp (Ind, It);
15911 end loop;
15913 if T = Any_Type then
15914 Error_Msg_N ("discrete type required for range", I);
15915 Set_Etype (I, Any_Type);
15916 return;
15918 elsif T = Universal_Integer then
15919 T := Standard_Integer;
15920 end if;
15921 end;
15922 end if;
15924 if not Is_Discrete_Type (T) then
15925 Error_Msg_N ("discrete type required for range", I);
15926 Set_Etype (I, Any_Type);
15927 return;
15928 end if;
15930 if Nkind (Low_Bound (I)) = N_Attribute_Reference
15931 and then Attribute_Name (Low_Bound (I)) = Name_First
15932 and then Is_Entity_Name (Prefix (Low_Bound (I)))
15933 and then Is_Type (Entity (Prefix (Low_Bound (I))))
15934 and then Is_Discrete_Type (Entity (Prefix (Low_Bound (I))))
15935 then
15936 -- The type of the index will be the type of the prefix, as long
15937 -- as the upper bound is 'Last of the same type.
15939 Def_Id := Entity (Prefix (Low_Bound (I)));
15941 if Nkind (High_Bound (I)) /= N_Attribute_Reference
15942 or else Attribute_Name (High_Bound (I)) /= Name_Last
15943 or else not Is_Entity_Name (Prefix (High_Bound (I)))
15944 or else Entity (Prefix (High_Bound (I))) /= Def_Id
15945 then
15946 Def_Id := Empty;
15947 end if;
15948 end if;
15950 R := I;
15951 Process_Range_Expr_In_Decl (R, T);
15953 elsif Nkind (I) = N_Subtype_Indication then
15955 -- The index is given by a subtype with a range constraint
15957 T := Base_Type (Entity (Subtype_Mark (I)));
15959 if not Is_Discrete_Type (T) then
15960 Error_Msg_N ("discrete type required for range", I);
15961 Set_Etype (I, Any_Type);
15962 return;
15963 end if;
15965 R := Range_Expression (Constraint (I));
15967 Resolve (R, T);
15968 Process_Range_Expr_In_Decl (R, Entity (Subtype_Mark (I)));
15970 elsif Nkind (I) = N_Attribute_Reference then
15972 -- The parser guarantees that the attribute is a RANGE attribute
15974 -- If the node denotes the range of a type mark, that is also the
15975 -- resulting type, and we do no need to create an Itype for it.
15977 if Is_Entity_Name (Prefix (I))
15978 and then Comes_From_Source (I)
15979 and then Is_Type (Entity (Prefix (I)))
15980 and then Is_Discrete_Type (Entity (Prefix (I)))
15981 then
15982 Def_Id := Entity (Prefix (I));
15983 end if;
15985 Analyze_And_Resolve (I);
15986 T := Etype (I);
15987 R := I;
15989 -- If none of the above, must be a subtype. We convert this to a
15990 -- range attribute reference because in the case of declared first
15991 -- named subtypes, the types in the range reference can be different
15992 -- from the type of the entity. A range attribute normalizes the
15993 -- reference and obtains the correct types for the bounds.
15995 -- This transformation is in the nature of an expansion, is only
15996 -- done if expansion is active. In particular, it is not done on
15997 -- formal generic types, because we need to retain the name of the
15998 -- original index for instantiation purposes.
16000 else
16001 if not Is_Entity_Name (I) or else not Is_Type (Entity (I)) then
16002 Error_Msg_N ("invalid subtype mark in discrete range ", I);
16003 Set_Etype (I, Any_Integer);
16004 return;
16006 else
16007 -- The type mark may be that of an incomplete type. It is only
16008 -- now that we can get the full view, previous analysis does
16009 -- not look specifically for a type mark.
16011 Set_Entity (I, Get_Full_View (Entity (I)));
16012 Set_Etype (I, Entity (I));
16013 Def_Id := Entity (I);
16015 if not Is_Discrete_Type (Def_Id) then
16016 Error_Msg_N ("discrete type required for index", I);
16017 Set_Etype (I, Any_Type);
16018 return;
16019 end if;
16020 end if;
16022 if Expander_Active then
16023 Rewrite (I,
16024 Make_Attribute_Reference (Sloc (I),
16025 Attribute_Name => Name_Range,
16026 Prefix => Relocate_Node (I)));
16028 -- The original was a subtype mark that does not freeze. This
16029 -- means that the rewritten version must not freeze either.
16031 Set_Must_Not_Freeze (I);
16032 Set_Must_Not_Freeze (Prefix (I));
16034 -- Is order critical??? if so, document why, if not
16035 -- use Analyze_And_Resolve
16037 Analyze_And_Resolve (I);
16038 T := Etype (I);
16039 R := I;
16041 -- If expander is inactive, type is legal, nothing else to construct
16043 else
16044 return;
16045 end if;
16046 end if;
16048 if not Is_Discrete_Type (T) then
16049 Error_Msg_N ("discrete type required for range", I);
16050 Set_Etype (I, Any_Type);
16051 return;
16053 elsif T = Any_Type then
16054 Set_Etype (I, Any_Type);
16055 return;
16056 end if;
16058 -- We will now create the appropriate Itype to describe the range, but
16059 -- first a check. If we originally had a subtype, then we just label
16060 -- the range with this subtype. Not only is there no need to construct
16061 -- a new subtype, but it is wrong to do so for two reasons:
16063 -- 1. A legality concern, if we have a subtype, it must not freeze,
16064 -- and the Itype would cause freezing incorrectly
16066 -- 2. An efficiency concern, if we created an Itype, it would not be
16067 -- recognized as the same type for the purposes of eliminating
16068 -- checks in some circumstances.
16070 -- We signal this case by setting the subtype entity in Def_Id
16072 if No (Def_Id) then
16073 Def_Id :=
16074 Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
16075 Set_Etype (Def_Id, Base_Type (T));
16077 if Is_Signed_Integer_Type (T) then
16078 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
16080 elsif Is_Modular_Integer_Type (T) then
16081 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
16083 else
16084 Set_Ekind (Def_Id, E_Enumeration_Subtype);
16085 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
16086 Set_First_Literal (Def_Id, First_Literal (T));
16087 end if;
16089 Set_Size_Info (Def_Id, (T));
16090 Set_RM_Size (Def_Id, RM_Size (T));
16091 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
16093 Set_Scalar_Range (Def_Id, R);
16094 Conditional_Delay (Def_Id, T);
16096 -- In the subtype indication case, if the immediate parent of the
16097 -- new subtype is non-static, then the subtype we create is non-
16098 -- static, even if its bounds are static.
16100 if Nkind (I) = N_Subtype_Indication
16101 and then not Is_Static_Subtype (Entity (Subtype_Mark (I)))
16102 then
16103 Set_Is_Non_Static_Subtype (Def_Id);
16104 end if;
16105 end if;
16107 -- Final step is to label the index with this constructed type
16109 Set_Etype (I, Def_Id);
16110 end Make_Index;
16112 ------------------------------
16113 -- Modular_Type_Declaration --
16114 ------------------------------
16116 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
16117 Mod_Expr : constant Node_Id := Expression (Def);
16118 M_Val : Uint;
16120 procedure Set_Modular_Size (Bits : Int);
16121 -- Sets RM_Size to Bits, and Esize to normal word size above this
16123 ----------------------
16124 -- Set_Modular_Size --
16125 ----------------------
16127 procedure Set_Modular_Size (Bits : Int) is
16128 begin
16129 Set_RM_Size (T, UI_From_Int (Bits));
16131 if Bits <= 8 then
16132 Init_Esize (T, 8);
16134 elsif Bits <= 16 then
16135 Init_Esize (T, 16);
16137 elsif Bits <= 32 then
16138 Init_Esize (T, 32);
16140 else
16141 Init_Esize (T, System_Max_Binary_Modulus_Power);
16142 end if;
16144 if not Non_Binary_Modulus (T)
16145 and then Esize (T) = RM_Size (T)
16146 then
16147 Set_Is_Known_Valid (T);
16148 end if;
16149 end Set_Modular_Size;
16151 -- Start of processing for Modular_Type_Declaration
16153 begin
16154 Analyze_And_Resolve (Mod_Expr, Any_Integer);
16155 Set_Etype (T, T);
16156 Set_Ekind (T, E_Modular_Integer_Type);
16157 Init_Alignment (T);
16158 Set_Is_Constrained (T);
16160 if not Is_OK_Static_Expression (Mod_Expr) then
16161 Flag_Non_Static_Expr
16162 ("non-static expression used for modular type bound!", Mod_Expr);
16163 M_Val := 2 ** System_Max_Binary_Modulus_Power;
16164 else
16165 M_Val := Expr_Value (Mod_Expr);
16166 end if;
16168 if M_Val < 1 then
16169 Error_Msg_N ("modulus value must be positive", Mod_Expr);
16170 M_Val := 2 ** System_Max_Binary_Modulus_Power;
16171 end if;
16173 Set_Modulus (T, M_Val);
16175 -- Create bounds for the modular type based on the modulus given in
16176 -- the type declaration and then analyze and resolve those bounds.
16178 Set_Scalar_Range (T,
16179 Make_Range (Sloc (Mod_Expr),
16180 Low_Bound => Make_Integer_Literal (Sloc (Mod_Expr), 0),
16181 High_Bound => Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
16183 -- Properly analyze the literals for the range. We do this manually
16184 -- because we can't go calling Resolve, since we are resolving these
16185 -- bounds with the type, and this type is certainly not complete yet!
16187 Set_Etype (Low_Bound (Scalar_Range (T)), T);
16188 Set_Etype (High_Bound (Scalar_Range (T)), T);
16189 Set_Is_Static_Expression (Low_Bound (Scalar_Range (T)));
16190 Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
16192 -- Loop through powers of two to find number of bits required
16194 for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
16196 -- Binary case
16198 if M_Val = 2 ** Bits then
16199 Set_Modular_Size (Bits);
16200 return;
16202 -- Non-binary case
16204 elsif M_Val < 2 ** Bits then
16205 Set_Non_Binary_Modulus (T);
16207 if Bits > System_Max_Nonbinary_Modulus_Power then
16208 Error_Msg_Uint_1 :=
16209 UI_From_Int (System_Max_Nonbinary_Modulus_Power);
16210 Error_Msg_F
16211 ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
16212 Set_Modular_Size (System_Max_Binary_Modulus_Power);
16213 return;
16215 else
16216 -- In the non-binary case, set size as per RM 13.3(55)
16218 Set_Modular_Size (Bits);
16219 return;
16220 end if;
16221 end if;
16223 end loop;
16225 -- If we fall through, then the size exceed System.Max_Binary_Modulus
16226 -- so we just signal an error and set the maximum size.
16228 Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
16229 Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
16231 Set_Modular_Size (System_Max_Binary_Modulus_Power);
16232 Init_Alignment (T);
16234 end Modular_Type_Declaration;
16236 --------------------------
16237 -- New_Concatenation_Op --
16238 --------------------------
16240 procedure New_Concatenation_Op (Typ : Entity_Id) is
16241 Loc : constant Source_Ptr := Sloc (Typ);
16242 Op : Entity_Id;
16244 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
16245 -- Create abbreviated declaration for the formal of a predefined
16246 -- Operator 'Op' of type 'Typ'
16248 --------------------
16249 -- Make_Op_Formal --
16250 --------------------
16252 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
16253 Formal : Entity_Id;
16254 begin
16255 Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
16256 Set_Etype (Formal, Typ);
16257 Set_Mechanism (Formal, Default_Mechanism);
16258 return Formal;
16259 end Make_Op_Formal;
16261 -- Start of processing for New_Concatenation_Op
16263 begin
16264 Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
16266 Set_Ekind (Op, E_Operator);
16267 Set_Scope (Op, Current_Scope);
16268 Set_Etype (Op, Typ);
16269 Set_Homonym (Op, Get_Name_Entity_Id (Name_Op_Concat));
16270 Set_Is_Immediately_Visible (Op);
16271 Set_Is_Intrinsic_Subprogram (Op);
16272 Set_Has_Completion (Op);
16273 Append_Entity (Op, Current_Scope);
16275 Set_Name_Entity_Id (Name_Op_Concat, Op);
16277 Append_Entity (Make_Op_Formal (Typ, Op), Op);
16278 Append_Entity (Make_Op_Formal (Typ, Op), Op);
16279 end New_Concatenation_Op;
16281 -------------------------
16282 -- OK_For_Limited_Init --
16283 -------------------------
16285 -- ???Check all calls of this, and compare the conditions under which it's
16286 -- called.
16288 function OK_For_Limited_Init
16289 (Typ : Entity_Id;
16290 Exp : Node_Id) return Boolean
16292 begin
16293 return Is_CPP_Constructor_Call (Exp)
16294 or else (Ada_Version >= Ada_2005
16295 and then not Debug_Flag_Dot_L
16296 and then OK_For_Limited_Init_In_05 (Typ, Exp));
16297 end OK_For_Limited_Init;
16299 -------------------------------
16300 -- OK_For_Limited_Init_In_05 --
16301 -------------------------------
16303 function OK_For_Limited_Init_In_05
16304 (Typ : Entity_Id;
16305 Exp : Node_Id) return Boolean
16307 begin
16308 -- An object of a limited interface type can be initialized with any
16309 -- expression of a nonlimited descendant type.
16311 if Is_Class_Wide_Type (Typ)
16312 and then Is_Limited_Interface (Typ)
16313 and then not Is_Limited_Type (Etype (Exp))
16314 then
16315 return True;
16316 end if;
16318 -- Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
16319 -- case of limited aggregates (including extension aggregates), and
16320 -- function calls. The function call may have been given in prefixed
16321 -- notation, in which case the original node is an indexed component.
16322 -- If the function is parameterless, the original node was an explicit
16323 -- dereference.
16325 case Nkind (Original_Node (Exp)) is
16326 when N_Aggregate | N_Extension_Aggregate | N_Function_Call | N_Op =>
16327 return True;
16329 when N_Qualified_Expression =>
16330 return
16331 OK_For_Limited_Init_In_05
16332 (Typ, Expression (Original_Node (Exp)));
16334 -- Ada 2005 (AI-251): If a class-wide interface object is initialized
16335 -- with a function call, the expander has rewritten the call into an
16336 -- N_Type_Conversion node to force displacement of the pointer to
16337 -- reference the component containing the secondary dispatch table.
16338 -- Otherwise a type conversion is not a legal context.
16339 -- A return statement for a build-in-place function returning a
16340 -- synchronized type also introduces an unchecked conversion.
16342 when N_Type_Conversion |
16343 N_Unchecked_Type_Conversion =>
16344 return not Comes_From_Source (Exp)
16345 and then
16346 OK_For_Limited_Init_In_05
16347 (Typ, Expression (Original_Node (Exp)));
16349 when N_Indexed_Component |
16350 N_Selected_Component |
16351 N_Explicit_Dereference =>
16352 return Nkind (Exp) = N_Function_Call;
16354 -- A use of 'Input is a function call, hence allowed. Normally the
16355 -- attribute will be changed to a call, but the attribute by itself
16356 -- can occur with -gnatc.
16358 when N_Attribute_Reference =>
16359 return Attribute_Name (Original_Node (Exp)) = Name_Input;
16361 when others =>
16362 return False;
16363 end case;
16364 end OK_For_Limited_Init_In_05;
16366 -------------------------------------------
16367 -- Ordinary_Fixed_Point_Type_Declaration --
16368 -------------------------------------------
16370 procedure Ordinary_Fixed_Point_Type_Declaration
16371 (T : Entity_Id;
16372 Def : Node_Id)
16374 Loc : constant Source_Ptr := Sloc (Def);
16375 Delta_Expr : constant Node_Id := Delta_Expression (Def);
16376 RRS : constant Node_Id := Real_Range_Specification (Def);
16377 Implicit_Base : Entity_Id;
16378 Delta_Val : Ureal;
16379 Small_Val : Ureal;
16380 Low_Val : Ureal;
16381 High_Val : Ureal;
16383 begin
16384 Check_Restriction (No_Fixed_Point, Def);
16386 -- Create implicit base type
16388 Implicit_Base :=
16389 Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
16390 Set_Etype (Implicit_Base, Implicit_Base);
16392 -- Analyze and process delta expression
16394 Analyze_And_Resolve (Delta_Expr, Any_Real);
16396 Check_Delta_Expression (Delta_Expr);
16397 Delta_Val := Expr_Value_R (Delta_Expr);
16399 Set_Delta_Value (Implicit_Base, Delta_Val);
16401 -- Compute default small from given delta, which is the largest power
16402 -- of two that does not exceed the given delta value.
16404 declare
16405 Tmp : Ureal;
16406 Scale : Int;
16408 begin
16409 Tmp := Ureal_1;
16410 Scale := 0;
16412 if Delta_Val < Ureal_1 then
16413 while Delta_Val < Tmp loop
16414 Tmp := Tmp / Ureal_2;
16415 Scale := Scale + 1;
16416 end loop;
16418 else
16419 loop
16420 Tmp := Tmp * Ureal_2;
16421 exit when Tmp > Delta_Val;
16422 Scale := Scale - 1;
16423 end loop;
16424 end if;
16426 Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
16427 end;
16429 Set_Small_Value (Implicit_Base, Small_Val);
16431 -- If no range was given, set a dummy range
16433 if RRS <= Empty_Or_Error then
16434 Low_Val := -Small_Val;
16435 High_Val := Small_Val;
16437 -- Otherwise analyze and process given range
16439 else
16440 declare
16441 Low : constant Node_Id := Low_Bound (RRS);
16442 High : constant Node_Id := High_Bound (RRS);
16444 begin
16445 Analyze_And_Resolve (Low, Any_Real);
16446 Analyze_And_Resolve (High, Any_Real);
16447 Check_Real_Bound (Low);
16448 Check_Real_Bound (High);
16450 -- Obtain and set the range
16452 Low_Val := Expr_Value_R (Low);
16453 High_Val := Expr_Value_R (High);
16455 if Low_Val > High_Val then
16456 Error_Msg_NE ("?fixed point type& has null range", Def, T);
16457 end if;
16458 end;
16459 end if;
16461 -- The range for both the implicit base and the declared first subtype
16462 -- cannot be set yet, so we use the special routine Set_Fixed_Range to
16463 -- set a temporary range in place. Note that the bounds of the base
16464 -- type will be widened to be symmetrical and to fill the available
16465 -- bits when the type is frozen.
16467 -- We could do this with all discrete types, and probably should, but
16468 -- we absolutely have to do it for fixed-point, since the end-points
16469 -- of the range and the size are determined by the small value, which
16470 -- could be reset before the freeze point.
16472 Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
16473 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
16475 -- Complete definition of first subtype
16477 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
16478 Set_Etype (T, Implicit_Base);
16479 Init_Size_Align (T);
16480 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
16481 Set_Small_Value (T, Small_Val);
16482 Set_Delta_Value (T, Delta_Val);
16483 Set_Is_Constrained (T);
16485 end Ordinary_Fixed_Point_Type_Declaration;
16487 ----------------------------------------
16488 -- Prepare_Private_Subtype_Completion --
16489 ----------------------------------------
16491 procedure Prepare_Private_Subtype_Completion
16492 (Id : Entity_Id;
16493 Related_Nod : Node_Id)
16495 Id_B : constant Entity_Id := Base_Type (Id);
16496 Full_B : constant Entity_Id := Full_View (Id_B);
16497 Full : Entity_Id;
16499 begin
16500 if Present (Full_B) then
16502 -- The Base_Type is already completed, we can complete the subtype
16503 -- now. We have to create a new entity with the same name, Thus we
16504 -- can't use Create_Itype.
16506 -- This is messy, should be fixed ???
16508 Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
16509 Set_Is_Itype (Full);
16510 Set_Associated_Node_For_Itype (Full, Related_Nod);
16511 Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
16512 end if;
16514 -- The parent subtype may be private, but the base might not, in some
16515 -- nested instances. In that case, the subtype does not need to be
16516 -- exchanged. It would still be nice to make private subtypes and their
16517 -- bases consistent at all times ???
16519 if Is_Private_Type (Id_B) then
16520 Append_Elmt (Id, Private_Dependents (Id_B));
16521 end if;
16523 end Prepare_Private_Subtype_Completion;
16525 ---------------------------
16526 -- Process_Discriminants --
16527 ---------------------------
16529 procedure Process_Discriminants
16530 (N : Node_Id;
16531 Prev : Entity_Id := Empty)
16533 Elist : constant Elist_Id := New_Elmt_List;
16534 Id : Node_Id;
16535 Discr : Node_Id;
16536 Discr_Number : Uint;
16537 Discr_Type : Entity_Id;
16538 Default_Present : Boolean := False;
16539 Default_Not_Present : Boolean := False;
16541 begin
16542 -- A composite type other than an array type can have discriminants.
16543 -- On entry, the current scope is the composite type.
16545 -- The discriminants are initially entered into the scope of the type
16546 -- via Enter_Name with the default Ekind of E_Void to prevent premature
16547 -- use, as explained at the end of this procedure.
16549 Discr := First (Discriminant_Specifications (N));
16550 while Present (Discr) loop
16551 Enter_Name (Defining_Identifier (Discr));
16553 -- For navigation purposes we add a reference to the discriminant
16554 -- in the entity for the type. If the current declaration is a
16555 -- completion, place references on the partial view. Otherwise the
16556 -- type is the current scope.
16558 if Present (Prev) then
16560 -- The references go on the partial view, if present. If the
16561 -- partial view has discriminants, the references have been
16562 -- generated already.
16564 if not Has_Discriminants (Prev) then
16565 Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
16566 end if;
16567 else
16568 Generate_Reference
16569 (Current_Scope, Defining_Identifier (Discr), 'd');
16570 end if;
16572 if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
16573 Discr_Type := Access_Definition (Discr, Discriminant_Type (Discr));
16575 -- Ada 2005 (AI-254)
16577 if Present (Access_To_Subprogram_Definition
16578 (Discriminant_Type (Discr)))
16579 and then Protected_Present (Access_To_Subprogram_Definition
16580 (Discriminant_Type (Discr)))
16581 then
16582 Discr_Type :=
16583 Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
16584 end if;
16586 else
16587 Find_Type (Discriminant_Type (Discr));
16588 Discr_Type := Etype (Discriminant_Type (Discr));
16590 if Error_Posted (Discriminant_Type (Discr)) then
16591 Discr_Type := Any_Type;
16592 end if;
16593 end if;
16595 if Is_Access_Type (Discr_Type) then
16597 -- Ada 2005 (AI-230): Access discriminant allowed in non-limited
16598 -- record types
16600 if Ada_Version < Ada_2005 then
16601 Check_Access_Discriminant_Requires_Limited
16602 (Discr, Discriminant_Type (Discr));
16603 end if;
16605 if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
16606 Error_Msg_N
16607 ("(Ada 83) access discriminant not allowed", Discr);
16608 end if;
16610 elsif not Is_Discrete_Type (Discr_Type) then
16611 Error_Msg_N ("discriminants must have a discrete or access type",
16612 Discriminant_Type (Discr));
16613 end if;
16615 Set_Etype (Defining_Identifier (Discr), Discr_Type);
16617 -- If a discriminant specification includes the assignment compound
16618 -- delimiter followed by an expression, the expression is the default
16619 -- expression of the discriminant; the default expression must be of
16620 -- the type of the discriminant. (RM 3.7.1) Since this expression is
16621 -- a default expression, we do the special preanalysis, since this
16622 -- expression does not freeze (see "Handling of Default and Per-
16623 -- Object Expressions" in spec of package Sem).
16625 if Present (Expression (Discr)) then
16626 Preanalyze_Spec_Expression (Expression (Discr), Discr_Type);
16628 if Nkind (N) = N_Formal_Type_Declaration then
16629 Error_Msg_N
16630 ("discriminant defaults not allowed for formal type",
16631 Expression (Discr));
16633 -- Flag an error for a tagged type with defaulted discriminants,
16634 -- excluding limited tagged types when compiling for Ada 2012
16635 -- (see AI05-0214).
16637 elsif Is_Tagged_Type (Current_Scope)
16638 and then (not Is_Limited_Type (Current_Scope)
16639 or else Ada_Version < Ada_2012)
16640 and then Comes_From_Source (N)
16641 then
16642 -- Note: see similar test in Check_Or_Process_Discriminants, to
16643 -- handle the (illegal) case of the completion of an untagged
16644 -- view with discriminants with defaults by a tagged full view.
16645 -- We skip the check if Discr does not come from source, to
16646 -- account for the case of an untagged derived type providing
16647 -- defaults for a renamed discriminant from a private untagged
16648 -- ancestor with a tagged full view (ACATS B460006).
16650 if Ada_Version >= Ada_2012 then
16651 Error_Msg_N
16652 ("discriminants of nonlimited tagged type cannot have"
16653 & " defaults",
16654 Expression (Discr));
16655 else
16656 Error_Msg_N
16657 ("discriminants of tagged type cannot have defaults",
16658 Expression (Discr));
16659 end if;
16661 else
16662 Default_Present := True;
16663 Append_Elmt (Expression (Discr), Elist);
16665 -- Tag the defining identifiers for the discriminants with
16666 -- their corresponding default expressions from the tree.
16668 Set_Discriminant_Default_Value
16669 (Defining_Identifier (Discr), Expression (Discr));
16670 end if;
16672 else
16673 Default_Not_Present := True;
16674 end if;
16676 -- Ada 2005 (AI-231): Create an Itype that is a duplicate of
16677 -- Discr_Type but with the null-exclusion attribute
16679 if Ada_Version >= Ada_2005 then
16681 -- Ada 2005 (AI-231): Static checks
16683 if Can_Never_Be_Null (Discr_Type) then
16684 Null_Exclusion_Static_Checks (Discr);
16686 elsif Is_Access_Type (Discr_Type)
16687 and then Null_Exclusion_Present (Discr)
16689 -- No need to check itypes because in their case this check
16690 -- was done at their point of creation
16692 and then not Is_Itype (Discr_Type)
16693 then
16694 if Can_Never_Be_Null (Discr_Type) then
16695 Error_Msg_NE
16696 ("`NOT NULL` not allowed (& already excludes null)",
16697 Discr,
16698 Discr_Type);
16699 end if;
16701 Set_Etype (Defining_Identifier (Discr),
16702 Create_Null_Excluding_Itype
16703 (T => Discr_Type,
16704 Related_Nod => Discr));
16706 -- Check for improper null exclusion if the type is otherwise
16707 -- legal for a discriminant.
16709 elsif Null_Exclusion_Present (Discr)
16710 and then Is_Discrete_Type (Discr_Type)
16711 then
16712 Error_Msg_N
16713 ("null exclusion can only apply to an access type", Discr);
16714 end if;
16716 -- Ada 2005 (AI-402): access discriminants of nonlimited types
16717 -- can't have defaults. Synchronized types, or types that are
16718 -- explicitly limited are fine, but special tests apply to derived
16719 -- types in generics: in a generic body we have to assume the
16720 -- worst, and therefore defaults are not allowed if the parent is
16721 -- a generic formal private type (see ACATS B370001).
16723 if Is_Access_Type (Discr_Type) then
16724 if Ekind (Discr_Type) /= E_Anonymous_Access_Type
16725 or else not Default_Present
16726 or else Is_Limited_Record (Current_Scope)
16727 or else Is_Concurrent_Type (Current_Scope)
16728 or else Is_Concurrent_Record_Type (Current_Scope)
16729 or else Ekind (Current_Scope) = E_Limited_Private_Type
16730 then
16731 if not Is_Derived_Type (Current_Scope)
16732 or else not Is_Generic_Type (Etype (Current_Scope))
16733 or else not In_Package_Body (Scope (Etype (Current_Scope)))
16734 or else Limited_Present
16735 (Type_Definition (Parent (Current_Scope)))
16736 then
16737 null;
16739 else
16740 Error_Msg_N ("access discriminants of nonlimited types",
16741 Expression (Discr));
16742 Error_Msg_N ("\cannot have defaults", Expression (Discr));
16743 end if;
16745 elsif Present (Expression (Discr)) then
16746 Error_Msg_N
16747 ("(Ada 2005) access discriminants of nonlimited types",
16748 Expression (Discr));
16749 Error_Msg_N ("\cannot have defaults", Expression (Discr));
16750 end if;
16751 end if;
16752 end if;
16754 Next (Discr);
16755 end loop;
16757 -- An element list consisting of the default expressions of the
16758 -- discriminants is constructed in the above loop and used to set
16759 -- the Discriminant_Constraint attribute for the type. If an object
16760 -- is declared of this (record or task) type without any explicit
16761 -- discriminant constraint given, this element list will form the
16762 -- actual parameters for the corresponding initialization procedure
16763 -- for the type.
16765 Set_Discriminant_Constraint (Current_Scope, Elist);
16766 Set_Stored_Constraint (Current_Scope, No_Elist);
16768 -- Default expressions must be provided either for all or for none
16769 -- of the discriminants of a discriminant part. (RM 3.7.1)
16771 if Default_Present and then Default_Not_Present then
16772 Error_Msg_N
16773 ("incomplete specification of defaults for discriminants", N);
16774 end if;
16776 -- The use of the name of a discriminant is not allowed in default
16777 -- expressions of a discriminant part if the specification of the
16778 -- discriminant is itself given in the discriminant part. (RM 3.7.1)
16780 -- To detect this, the discriminant names are entered initially with an
16781 -- Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
16782 -- attempt to use a void entity (for example in an expression that is
16783 -- type-checked) produces the error message: premature usage. Now after
16784 -- completing the semantic analysis of the discriminant part, we can set
16785 -- the Ekind of all the discriminants appropriately.
16787 Discr := First (Discriminant_Specifications (N));
16788 Discr_Number := Uint_1;
16789 while Present (Discr) loop
16790 Id := Defining_Identifier (Discr);
16791 Set_Ekind (Id, E_Discriminant);
16792 Init_Component_Location (Id);
16793 Init_Esize (Id);
16794 Set_Discriminant_Number (Id, Discr_Number);
16796 -- Make sure this is always set, even in illegal programs
16798 Set_Corresponding_Discriminant (Id, Empty);
16800 -- Initialize the Original_Record_Component to the entity itself.
16801 -- Inherit_Components will propagate the right value to
16802 -- discriminants in derived record types.
16804 Set_Original_Record_Component (Id, Id);
16806 -- Create the discriminal for the discriminant
16808 Build_Discriminal (Id);
16810 Next (Discr);
16811 Discr_Number := Discr_Number + 1;
16812 end loop;
16814 Set_Has_Discriminants (Current_Scope);
16815 end Process_Discriminants;
16817 -----------------------
16818 -- Process_Full_View --
16819 -----------------------
16821 procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
16822 Priv_Parent : Entity_Id;
16823 Full_Parent : Entity_Id;
16824 Full_Indic : Node_Id;
16826 procedure Collect_Implemented_Interfaces
16827 (Typ : Entity_Id;
16828 Ifaces : Elist_Id);
16829 -- Ada 2005: Gather all the interfaces that Typ directly or
16830 -- inherently implements. Duplicate entries are not added to
16831 -- the list Ifaces.
16833 ------------------------------------
16834 -- Collect_Implemented_Interfaces --
16835 ------------------------------------
16837 procedure Collect_Implemented_Interfaces
16838 (Typ : Entity_Id;
16839 Ifaces : Elist_Id)
16841 Iface : Entity_Id;
16842 Iface_Elmt : Elmt_Id;
16844 begin
16845 -- Abstract interfaces are only associated with tagged record types
16847 if not Is_Tagged_Type (Typ)
16848 or else not Is_Record_Type (Typ)
16849 then
16850 return;
16851 end if;
16853 -- Recursively climb to the ancestors
16855 if Etype (Typ) /= Typ
16857 -- Protect the frontend against wrong cyclic declarations like:
16859 -- type B is new A with private;
16860 -- type C is new A with private;
16861 -- private
16862 -- type B is new C with null record;
16863 -- type C is new B with null record;
16865 and then Etype (Typ) /= Priv_T
16866 and then Etype (Typ) /= Full_T
16867 then
16868 -- Keep separate the management of private type declarations
16870 if Ekind (Typ) = E_Record_Type_With_Private then
16872 -- Handle the following erronous case:
16873 -- type Private_Type is tagged private;
16874 -- private
16875 -- type Private_Type is new Type_Implementing_Iface;
16877 if Present (Full_View (Typ))
16878 and then Etype (Typ) /= Full_View (Typ)
16879 then
16880 if Is_Interface (Etype (Typ)) then
16881 Append_Unique_Elmt (Etype (Typ), Ifaces);
16882 end if;
16884 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
16885 end if;
16887 -- Non-private types
16889 else
16890 if Is_Interface (Etype (Typ)) then
16891 Append_Unique_Elmt (Etype (Typ), Ifaces);
16892 end if;
16894 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
16895 end if;
16896 end if;
16898 -- Handle entities in the list of abstract interfaces
16900 if Present (Interfaces (Typ)) then
16901 Iface_Elmt := First_Elmt (Interfaces (Typ));
16902 while Present (Iface_Elmt) loop
16903 Iface := Node (Iface_Elmt);
16905 pragma Assert (Is_Interface (Iface));
16907 if not Contain_Interface (Iface, Ifaces) then
16908 Append_Elmt (Iface, Ifaces);
16909 Collect_Implemented_Interfaces (Iface, Ifaces);
16910 end if;
16912 Next_Elmt (Iface_Elmt);
16913 end loop;
16914 end if;
16915 end Collect_Implemented_Interfaces;
16917 -- Start of processing for Process_Full_View
16919 begin
16920 -- First some sanity checks that must be done after semantic
16921 -- decoration of the full view and thus cannot be placed with other
16922 -- similar checks in Find_Type_Name
16924 if not Is_Limited_Type (Priv_T)
16925 and then (Is_Limited_Type (Full_T)
16926 or else Is_Limited_Composite (Full_T))
16927 then
16928 Error_Msg_N
16929 ("completion of nonlimited type cannot be limited", Full_T);
16930 Explain_Limited_Type (Full_T, Full_T);
16932 elsif Is_Abstract_Type (Full_T)
16933 and then not Is_Abstract_Type (Priv_T)
16934 then
16935 Error_Msg_N
16936 ("completion of nonabstract type cannot be abstract", Full_T);
16938 elsif Is_Tagged_Type (Priv_T)
16939 and then Is_Limited_Type (Priv_T)
16940 and then not Is_Limited_Type (Full_T)
16941 then
16942 -- If pragma CPP_Class was applied to the private declaration
16943 -- propagate the limitedness to the full-view
16945 if Is_CPP_Class (Priv_T) then
16946 Set_Is_Limited_Record (Full_T);
16948 -- GNAT allow its own definition of Limited_Controlled to disobey
16949 -- this rule in order in ease the implementation. The next test is
16950 -- safe because Root_Controlled is defined in a private system child
16952 elsif Etype (Full_T) = Full_View (RTE (RE_Root_Controlled)) then
16953 Set_Is_Limited_Composite (Full_T);
16954 else
16955 Error_Msg_N
16956 ("completion of limited tagged type must be limited", Full_T);
16957 end if;
16959 elsif Is_Generic_Type (Priv_T) then
16960 Error_Msg_N ("generic type cannot have a completion", Full_T);
16961 end if;
16963 -- Check that ancestor interfaces of private and full views are
16964 -- consistent. We omit this check for synchronized types because
16965 -- they are performed on the corresponding record type when frozen.
16967 if Ada_Version >= Ada_2005
16968 and then Is_Tagged_Type (Priv_T)
16969 and then Is_Tagged_Type (Full_T)
16970 and then not Is_Concurrent_Type (Full_T)
16971 then
16972 declare
16973 Iface : Entity_Id;
16974 Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
16975 Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
16977 begin
16978 Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
16979 Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
16981 -- Ada 2005 (AI-251): The partial view shall be a descendant of
16982 -- an interface type if and only if the full type is descendant
16983 -- of the interface type (AARM 7.3 (7.3/2).
16985 Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
16987 if Present (Iface) then
16988 Error_Msg_NE
16989 ("interface & not implemented by full type " &
16990 "(RM-2005 7.3 (7.3/2))", Priv_T, Iface);
16991 end if;
16993 Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
16995 if Present (Iface) then
16996 Error_Msg_NE
16997 ("interface & not implemented by partial view " &
16998 "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
16999 end if;
17000 end;
17001 end if;
17003 if Is_Tagged_Type (Priv_T)
17004 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
17005 and then Is_Derived_Type (Full_T)
17006 then
17007 Priv_Parent := Etype (Priv_T);
17009 -- The full view of a private extension may have been transformed
17010 -- into an unconstrained derived type declaration and a subtype
17011 -- declaration (see build_derived_record_type for details).
17013 if Nkind (N) = N_Subtype_Declaration then
17014 Full_Indic := Subtype_Indication (N);
17015 Full_Parent := Etype (Base_Type (Full_T));
17016 else
17017 Full_Indic := Subtype_Indication (Type_Definition (N));
17018 Full_Parent := Etype (Full_T);
17019 end if;
17021 -- Check that the parent type of the full type is a descendant of
17022 -- the ancestor subtype given in the private extension. If either
17023 -- entity has an Etype equal to Any_Type then we had some previous
17024 -- error situation [7.3(8)].
17026 if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
17027 return;
17029 -- Ada 2005 (AI-251): Interfaces in the full-typ can be given in
17030 -- any order. Therefore we don't have to check that its parent must
17031 -- be a descendant of the parent of the private type declaration.
17033 elsif Is_Interface (Priv_Parent)
17034 and then Is_Interface (Full_Parent)
17035 then
17036 null;
17038 -- Ada 2005 (AI-251): If the parent of the private type declaration
17039 -- is an interface there is no need to check that it is an ancestor
17040 -- of the associated full type declaration. The required tests for
17041 -- this case are performed by Build_Derived_Record_Type.
17043 elsif not Is_Interface (Base_Type (Priv_Parent))
17044 and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
17045 then
17046 Error_Msg_N
17047 ("parent of full type must descend from parent"
17048 & " of private extension", Full_Indic);
17050 -- Check the rules of 7.3(10): if the private extension inherits
17051 -- known discriminants, then the full type must also inherit those
17052 -- discriminants from the same (ancestor) type, and the parent
17053 -- subtype of the full type must be constrained if and only if
17054 -- the ancestor subtype of the private extension is constrained.
17056 elsif No (Discriminant_Specifications (Parent (Priv_T)))
17057 and then not Has_Unknown_Discriminants (Priv_T)
17058 and then Has_Discriminants (Base_Type (Priv_Parent))
17059 then
17060 declare
17061 Priv_Indic : constant Node_Id :=
17062 Subtype_Indication (Parent (Priv_T));
17064 Priv_Constr : constant Boolean :=
17065 Is_Constrained (Priv_Parent)
17066 or else
17067 Nkind (Priv_Indic) = N_Subtype_Indication
17068 or else Is_Constrained (Entity (Priv_Indic));
17070 Full_Constr : constant Boolean :=
17071 Is_Constrained (Full_Parent)
17072 or else
17073 Nkind (Full_Indic) = N_Subtype_Indication
17074 or else Is_Constrained (Entity (Full_Indic));
17076 Priv_Discr : Entity_Id;
17077 Full_Discr : Entity_Id;
17079 begin
17080 Priv_Discr := First_Discriminant (Priv_Parent);
17081 Full_Discr := First_Discriminant (Full_Parent);
17082 while Present (Priv_Discr) and then Present (Full_Discr) loop
17083 if Original_Record_Component (Priv_Discr) =
17084 Original_Record_Component (Full_Discr)
17085 or else
17086 Corresponding_Discriminant (Priv_Discr) =
17087 Corresponding_Discriminant (Full_Discr)
17088 then
17089 null;
17090 else
17091 exit;
17092 end if;
17094 Next_Discriminant (Priv_Discr);
17095 Next_Discriminant (Full_Discr);
17096 end loop;
17098 if Present (Priv_Discr) or else Present (Full_Discr) then
17099 Error_Msg_N
17100 ("full view must inherit discriminants of the parent type"
17101 & " used in the private extension", Full_Indic);
17103 elsif Priv_Constr and then not Full_Constr then
17104 Error_Msg_N
17105 ("parent subtype of full type must be constrained",
17106 Full_Indic);
17108 elsif Full_Constr and then not Priv_Constr then
17109 Error_Msg_N
17110 ("parent subtype of full type must be unconstrained",
17111 Full_Indic);
17112 end if;
17113 end;
17115 -- Check the rules of 7.3(12): if a partial view has neither known
17116 -- or unknown discriminants, then the full type declaration shall
17117 -- define a definite subtype.
17119 elsif not Has_Unknown_Discriminants (Priv_T)
17120 and then not Has_Discriminants (Priv_T)
17121 and then not Is_Constrained (Full_T)
17122 then
17123 Error_Msg_N
17124 ("full view must define a constrained type if partial view"
17125 & " has no discriminants", Full_T);
17126 end if;
17128 -- ??????? Do we implement the following properly ?????
17129 -- If the ancestor subtype of a private extension has constrained
17130 -- discriminants, then the parent subtype of the full view shall
17131 -- impose a statically matching constraint on those discriminants
17132 -- [7.3(13)].
17134 else
17135 -- For untagged types, verify that a type without discriminants
17136 -- is not completed with an unconstrained type.
17138 if not Is_Indefinite_Subtype (Priv_T)
17139 and then Is_Indefinite_Subtype (Full_T)
17140 then
17141 Error_Msg_N ("full view of type must be definite subtype", Full_T);
17142 end if;
17143 end if;
17145 -- AI-419: verify that the use of "limited" is consistent
17147 declare
17148 Orig_Decl : constant Node_Id := Original_Node (N);
17150 begin
17151 if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
17152 and then not Limited_Present (Parent (Priv_T))
17153 and then not Synchronized_Present (Parent (Priv_T))
17154 and then Nkind (Orig_Decl) = N_Full_Type_Declaration
17155 and then Nkind
17156 (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
17157 and then Limited_Present (Type_Definition (Orig_Decl))
17158 then
17159 Error_Msg_N
17160 ("full view of non-limited extension cannot be limited", N);
17161 end if;
17162 end;
17164 -- Ada 2005 (AI-443): A synchronized private extension must be
17165 -- completed by a task or protected type.
17167 if Ada_Version >= Ada_2005
17168 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
17169 and then Synchronized_Present (Parent (Priv_T))
17170 and then not Is_Concurrent_Type (Full_T)
17171 then
17172 Error_Msg_N ("full view of synchronized extension must " &
17173 "be synchronized type", N);
17174 end if;
17176 -- Ada 2005 AI-363: if the full view has discriminants with
17177 -- defaults, it is illegal to declare constrained access subtypes
17178 -- whose designated type is the current type. This allows objects
17179 -- of the type that are declared in the heap to be unconstrained.
17181 if not Has_Unknown_Discriminants (Priv_T)
17182 and then not Has_Discriminants (Priv_T)
17183 and then Has_Discriminants (Full_T)
17184 and then
17185 Present (Discriminant_Default_Value (First_Discriminant (Full_T)))
17186 then
17187 Set_Has_Constrained_Partial_View (Full_T);
17188 Set_Has_Constrained_Partial_View (Priv_T);
17189 end if;
17191 -- Create a full declaration for all its subtypes recorded in
17192 -- Private_Dependents and swap them similarly to the base type. These
17193 -- are subtypes that have been define before the full declaration of
17194 -- the private type. We also swap the entry in Private_Dependents list
17195 -- so we can properly restore the private view on exit from the scope.
17197 declare
17198 Priv_Elmt : Elmt_Id;
17199 Priv : Entity_Id;
17200 Full : Entity_Id;
17202 begin
17203 Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
17204 while Present (Priv_Elmt) loop
17205 Priv := Node (Priv_Elmt);
17207 if Ekind_In (Priv, E_Private_Subtype,
17208 E_Limited_Private_Subtype,
17209 E_Record_Subtype_With_Private)
17210 then
17211 Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
17212 Set_Is_Itype (Full);
17213 Set_Parent (Full, Parent (Priv));
17214 Set_Associated_Node_For_Itype (Full, N);
17216 -- Now we need to complete the private subtype, but since the
17217 -- base type has already been swapped, we must also swap the
17218 -- subtypes (and thus, reverse the arguments in the call to
17219 -- Complete_Private_Subtype).
17221 Copy_And_Swap (Priv, Full);
17222 Complete_Private_Subtype (Full, Priv, Full_T, N);
17223 Replace_Elmt (Priv_Elmt, Full);
17224 end if;
17226 Next_Elmt (Priv_Elmt);
17227 end loop;
17228 end;
17230 -- If the private view was tagged, copy the new primitive operations
17231 -- from the private view to the full view.
17233 if Is_Tagged_Type (Full_T) then
17234 declare
17235 Disp_Typ : Entity_Id;
17236 Full_List : Elist_Id;
17237 Prim : Entity_Id;
17238 Prim_Elmt : Elmt_Id;
17239 Priv_List : Elist_Id;
17241 function Contains
17242 (E : Entity_Id;
17243 L : Elist_Id) return Boolean;
17244 -- Determine whether list L contains element E
17246 --------------
17247 -- Contains --
17248 --------------
17250 function Contains
17251 (E : Entity_Id;
17252 L : Elist_Id) return Boolean
17254 List_Elmt : Elmt_Id;
17256 begin
17257 List_Elmt := First_Elmt (L);
17258 while Present (List_Elmt) loop
17259 if Node (List_Elmt) = E then
17260 return True;
17261 end if;
17263 Next_Elmt (List_Elmt);
17264 end loop;
17266 return False;
17267 end Contains;
17269 -- Start of processing
17271 begin
17272 if Is_Tagged_Type (Priv_T) then
17273 Priv_List := Primitive_Operations (Priv_T);
17274 Prim_Elmt := First_Elmt (Priv_List);
17276 -- In the case of a concurrent type completing a private tagged
17277 -- type, primitives may have been declared in between the two
17278 -- views. These subprograms need to be wrapped the same way
17279 -- entries and protected procedures are handled because they
17280 -- cannot be directly shared by the two views.
17282 if Is_Concurrent_Type (Full_T) then
17283 declare
17284 Conc_Typ : constant Entity_Id :=
17285 Corresponding_Record_Type (Full_T);
17286 Curr_Nod : Node_Id := Parent (Conc_Typ);
17287 Wrap_Spec : Node_Id;
17289 begin
17290 while Present (Prim_Elmt) loop
17291 Prim := Node (Prim_Elmt);
17293 if Comes_From_Source (Prim)
17294 and then not Is_Abstract_Subprogram (Prim)
17295 then
17296 Wrap_Spec :=
17297 Make_Subprogram_Declaration (Sloc (Prim),
17298 Specification =>
17299 Build_Wrapper_Spec
17300 (Subp_Id => Prim,
17301 Obj_Typ => Conc_Typ,
17302 Formals =>
17303 Parameter_Specifications (
17304 Parent (Prim))));
17306 Insert_After (Curr_Nod, Wrap_Spec);
17307 Curr_Nod := Wrap_Spec;
17309 Analyze (Wrap_Spec);
17310 end if;
17312 Next_Elmt (Prim_Elmt);
17313 end loop;
17315 return;
17316 end;
17318 -- For non-concurrent types, transfer explicit primitives, but
17319 -- omit those inherited from the parent of the private view
17320 -- since they will be re-inherited later on.
17322 else
17323 Full_List := Primitive_Operations (Full_T);
17325 while Present (Prim_Elmt) loop
17326 Prim := Node (Prim_Elmt);
17328 if Comes_From_Source (Prim)
17329 and then not Contains (Prim, Full_List)
17330 then
17331 Append_Elmt (Prim, Full_List);
17332 end if;
17334 Next_Elmt (Prim_Elmt);
17335 end loop;
17336 end if;
17338 -- Untagged private view
17340 else
17341 Full_List := Primitive_Operations (Full_T);
17343 -- In this case the partial view is untagged, so here we locate
17344 -- all of the earlier primitives that need to be treated as
17345 -- dispatching (those that appear between the two views). Note
17346 -- that these additional operations must all be new operations
17347 -- (any earlier operations that override inherited operations
17348 -- of the full view will already have been inserted in the
17349 -- primitives list, marked by Check_Operation_From_Private_View
17350 -- as dispatching. Note that implicit "/=" operators are
17351 -- excluded from being added to the primitives list since they
17352 -- shouldn't be treated as dispatching (tagged "/=" is handled
17353 -- specially).
17355 Prim := Next_Entity (Full_T);
17356 while Present (Prim) and then Prim /= Priv_T loop
17357 if Ekind_In (Prim, E_Procedure, E_Function) then
17358 Disp_Typ := Find_Dispatching_Type (Prim);
17360 if Disp_Typ = Full_T
17361 and then (Chars (Prim) /= Name_Op_Ne
17362 or else Comes_From_Source (Prim))
17363 then
17364 Check_Controlling_Formals (Full_T, Prim);
17366 if not Is_Dispatching_Operation (Prim) then
17367 Append_Elmt (Prim, Full_List);
17368 Set_Is_Dispatching_Operation (Prim, True);
17369 Set_DT_Position (Prim, No_Uint);
17370 end if;
17372 elsif Is_Dispatching_Operation (Prim)
17373 and then Disp_Typ /= Full_T
17374 then
17376 -- Verify that it is not otherwise controlled by a
17377 -- formal or a return value of type T.
17379 Check_Controlling_Formals (Disp_Typ, Prim);
17380 end if;
17381 end if;
17383 Next_Entity (Prim);
17384 end loop;
17385 end if;
17387 -- For the tagged case, the two views can share the same primitive
17388 -- operations list and the same class-wide type. Update attributes
17389 -- of the class-wide type which depend on the full declaration.
17391 if Is_Tagged_Type (Priv_T) then
17392 Set_Direct_Primitive_Operations (Priv_T, Full_List);
17393 Set_Class_Wide_Type
17394 (Base_Type (Full_T), Class_Wide_Type (Priv_T));
17396 Set_Has_Task (Class_Wide_Type (Priv_T), Has_Task (Full_T));
17397 end if;
17398 end;
17399 end if;
17401 -- Ada 2005 AI 161: Check preelaboratable initialization consistency
17403 if Known_To_Have_Preelab_Init (Priv_T) then
17405 -- Case where there is a pragma Preelaborable_Initialization. We
17406 -- always allow this in predefined units, which is a bit of a kludge,
17407 -- but it means we don't have to struggle to meet the requirements in
17408 -- the RM for having Preelaborable Initialization. Otherwise we
17409 -- require that the type meets the RM rules. But we can't check that
17410 -- yet, because of the rule about overriding Ininitialize, so we
17411 -- simply set a flag that will be checked at freeze time.
17413 if not In_Predefined_Unit (Full_T) then
17414 Set_Must_Have_Preelab_Init (Full_T);
17415 end if;
17416 end if;
17418 -- If pragma CPP_Class was applied to the private type declaration,
17419 -- propagate it now to the full type declaration.
17421 if Is_CPP_Class (Priv_T) then
17422 Set_Is_CPP_Class (Full_T);
17423 Set_Convention (Full_T, Convention_CPP);
17424 end if;
17426 -- If the private view has user specified stream attributes, then so has
17427 -- the full view.
17429 -- Why the test, how could these flags be already set in Full_T ???
17431 if Has_Specified_Stream_Read (Priv_T) then
17432 Set_Has_Specified_Stream_Read (Full_T);
17433 end if;
17435 if Has_Specified_Stream_Write (Priv_T) then
17436 Set_Has_Specified_Stream_Write (Full_T);
17437 end if;
17439 if Has_Specified_Stream_Input (Priv_T) then
17440 Set_Has_Specified_Stream_Input (Full_T);
17441 end if;
17443 if Has_Specified_Stream_Output (Priv_T) then
17444 Set_Has_Specified_Stream_Output (Full_T);
17445 end if;
17447 -- Propagate invariants to full type
17449 if Has_Invariants (Priv_T) then
17450 Set_Has_Invariants (Full_T);
17451 Set_Invariant_Procedure (Full_T, Invariant_Procedure (Priv_T));
17452 end if;
17454 if Has_Inheritable_Invariants (Priv_T) then
17455 Set_Has_Inheritable_Invariants (Full_T);
17456 end if;
17458 -- Propagate predicates to full type
17460 if Has_Predicates (Priv_T) then
17461 Set_Predicate_Function (Priv_T, Predicate_Function (Full_T));
17462 Set_Has_Predicates (Priv_T);
17463 end if;
17464 end Process_Full_View;
17466 -----------------------------------
17467 -- Process_Incomplete_Dependents --
17468 -----------------------------------
17470 procedure Process_Incomplete_Dependents
17471 (N : Node_Id;
17472 Full_T : Entity_Id;
17473 Inc_T : Entity_Id)
17475 Inc_Elmt : Elmt_Id;
17476 Priv_Dep : Entity_Id;
17477 New_Subt : Entity_Id;
17479 Disc_Constraint : Elist_Id;
17481 begin
17482 if No (Private_Dependents (Inc_T)) then
17483 return;
17484 end if;
17486 -- Itypes that may be generated by the completion of an incomplete
17487 -- subtype are not used by the back-end and not attached to the tree.
17488 -- They are created only for constraint-checking purposes.
17490 Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
17491 while Present (Inc_Elmt) loop
17492 Priv_Dep := Node (Inc_Elmt);
17494 if Ekind (Priv_Dep) = E_Subprogram_Type then
17496 -- An Access_To_Subprogram type may have a return type or a
17497 -- parameter type that is incomplete. Replace with the full view.
17499 if Etype (Priv_Dep) = Inc_T then
17500 Set_Etype (Priv_Dep, Full_T);
17501 end if;
17503 declare
17504 Formal : Entity_Id;
17506 begin
17507 Formal := First_Formal (Priv_Dep);
17508 while Present (Formal) loop
17509 if Etype (Formal) = Inc_T then
17510 Set_Etype (Formal, Full_T);
17511 end if;
17513 Next_Formal (Formal);
17514 end loop;
17515 end;
17517 elsif Is_Overloadable (Priv_Dep) then
17519 -- A protected operation is never dispatching: only its
17520 -- wrapper operation (which has convention Ada) is.
17522 if Is_Tagged_Type (Full_T)
17523 and then Convention (Priv_Dep) /= Convention_Protected
17524 then
17526 -- Subprogram has an access parameter whose designated type
17527 -- was incomplete. Reexamine declaration now, because it may
17528 -- be a primitive operation of the full type.
17530 Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
17531 Set_Is_Dispatching_Operation (Priv_Dep);
17532 Check_Controlling_Formals (Full_T, Priv_Dep);
17533 end if;
17535 elsif Ekind (Priv_Dep) = E_Subprogram_Body then
17537 -- Can happen during processing of a body before the completion
17538 -- of a TA type. Ignore, because spec is also on dependent list.
17540 return;
17542 -- Ada 2005 (AI-412): Transform a regular incomplete subtype into a
17543 -- corresponding subtype of the full view.
17545 elsif Ekind (Priv_Dep) = E_Incomplete_Subtype then
17546 Set_Subtype_Indication
17547 (Parent (Priv_Dep), New_Reference_To (Full_T, Sloc (Priv_Dep)));
17548 Set_Etype (Priv_Dep, Full_T);
17549 Set_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
17550 Set_Analyzed (Parent (Priv_Dep), False);
17552 -- Reanalyze the declaration, suppressing the call to
17553 -- Enter_Name to avoid duplicate names.
17555 Analyze_Subtype_Declaration
17556 (N => Parent (Priv_Dep),
17557 Skip => True);
17559 -- Dependent is a subtype
17561 else
17562 -- We build a new subtype indication using the full view of the
17563 -- incomplete parent. The discriminant constraints have been
17564 -- elaborated already at the point of the subtype declaration.
17566 New_Subt := Create_Itype (E_Void, N);
17568 if Has_Discriminants (Full_T) then
17569 Disc_Constraint := Discriminant_Constraint (Priv_Dep);
17570 else
17571 Disc_Constraint := No_Elist;
17572 end if;
17574 Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
17575 Set_Full_View (Priv_Dep, New_Subt);
17576 end if;
17578 Next_Elmt (Inc_Elmt);
17579 end loop;
17580 end Process_Incomplete_Dependents;
17582 --------------------------------
17583 -- Process_Range_Expr_In_Decl --
17584 --------------------------------
17586 procedure Process_Range_Expr_In_Decl
17587 (R : Node_Id;
17588 T : Entity_Id;
17589 Check_List : List_Id := Empty_List;
17590 R_Check_Off : Boolean := False)
17592 Lo, Hi : Node_Id;
17593 R_Checks : Check_Result;
17594 Insert_Node : Node_Id;
17595 Def_Id : Entity_Id;
17597 begin
17598 Analyze_And_Resolve (R, Base_Type (T));
17600 if Nkind (R) = N_Range then
17601 Lo := Low_Bound (R);
17602 Hi := High_Bound (R);
17604 -- We need to ensure validity of the bounds here, because if we
17605 -- go ahead and do the expansion, then the expanded code will get
17606 -- analyzed with range checks suppressed and we miss the check.
17608 Validity_Check_Range (R);
17610 -- If there were errors in the declaration, try and patch up some
17611 -- common mistakes in the bounds. The cases handled are literals
17612 -- which are Integer where the expected type is Real and vice versa.
17613 -- These corrections allow the compilation process to proceed further
17614 -- along since some basic assumptions of the format of the bounds
17615 -- are guaranteed.
17617 if Etype (R) = Any_Type then
17619 if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
17620 Rewrite (Lo,
17621 Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
17623 elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
17624 Rewrite (Hi,
17625 Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
17627 elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
17628 Rewrite (Lo,
17629 Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
17631 elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
17632 Rewrite (Hi,
17633 Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
17634 end if;
17636 Set_Etype (Lo, T);
17637 Set_Etype (Hi, T);
17638 end if;
17640 -- If the bounds of the range have been mistakenly given as string
17641 -- literals (perhaps in place of character literals), then an error
17642 -- has already been reported, but we rewrite the string literal as a
17643 -- bound of the range's type to avoid blowups in later processing
17644 -- that looks at static values.
17646 if Nkind (Lo) = N_String_Literal then
17647 Rewrite (Lo,
17648 Make_Attribute_Reference (Sloc (Lo),
17649 Attribute_Name => Name_First,
17650 Prefix => New_Reference_To (T, Sloc (Lo))));
17651 Analyze_And_Resolve (Lo);
17652 end if;
17654 if Nkind (Hi) = N_String_Literal then
17655 Rewrite (Hi,
17656 Make_Attribute_Reference (Sloc (Hi),
17657 Attribute_Name => Name_First,
17658 Prefix => New_Reference_To (T, Sloc (Hi))));
17659 Analyze_And_Resolve (Hi);
17660 end if;
17662 -- If bounds aren't scalar at this point then exit, avoiding
17663 -- problems with further processing of the range in this procedure.
17665 if not Is_Scalar_Type (Etype (Lo)) then
17666 return;
17667 end if;
17669 -- Resolve (actually Sem_Eval) has checked that the bounds are in
17670 -- then range of the base type. Here we check whether the bounds
17671 -- are in the range of the subtype itself. Note that if the bounds
17672 -- represent the null range the Constraint_Error exception should
17673 -- not be raised.
17675 -- ??? The following code should be cleaned up as follows
17677 -- 1. The Is_Null_Range (Lo, Hi) test should disappear since it
17678 -- is done in the call to Range_Check (R, T); below
17680 -- 2. The use of R_Check_Off should be investigated and possibly
17681 -- removed, this would clean up things a bit.
17683 if Is_Null_Range (Lo, Hi) then
17684 null;
17686 else
17687 -- Capture values of bounds and generate temporaries for them
17688 -- if needed, before applying checks, since checks may cause
17689 -- duplication of the expression without forcing evaluation.
17691 if Expander_Active then
17692 Force_Evaluation (Lo);
17693 Force_Evaluation (Hi);
17694 end if;
17696 -- We use a flag here instead of suppressing checks on the
17697 -- type because the type we check against isn't necessarily
17698 -- the place where we put the check.
17700 if not R_Check_Off then
17701 R_Checks := Get_Range_Checks (R, T);
17703 -- Look up tree to find an appropriate insertion point. We
17704 -- can't just use insert_actions because later processing
17705 -- depends on the insertion node. Prior to Ada2012 the
17706 -- insertion point could only be a declaration or a loop, but
17707 -- quantified expressions can appear within any context in an
17708 -- expression, and the insertion point can be any statement,
17709 -- pragma, or declaration.
17711 Insert_Node := Parent (R);
17712 while Present (Insert_Node) loop
17713 exit when
17714 Nkind (Insert_Node) in N_Declaration
17715 and then
17716 not Nkind_In
17717 (Insert_Node, N_Component_Declaration,
17718 N_Loop_Parameter_Specification,
17719 N_Function_Specification,
17720 N_Procedure_Specification);
17722 exit when Nkind (Insert_Node) in N_Later_Decl_Item
17723 or else Nkind (Insert_Node) in
17724 N_Statement_Other_Than_Procedure_Call
17725 or else Nkind_In (Insert_Node, N_Procedure_Call_Statement,
17726 N_Pragma);
17728 Insert_Node := Parent (Insert_Node);
17729 end loop;
17731 -- Why would Type_Decl not be present??? Without this test,
17732 -- short regression tests fail.
17734 if Present (Insert_Node) then
17736 -- Case of loop statement. Verify that the range is part
17737 -- of the subtype indication of the iteration scheme.
17739 if Nkind (Insert_Node) = N_Loop_Statement then
17740 declare
17741 Indic : Node_Id;
17743 begin
17744 Indic := Parent (R);
17745 while Present (Indic)
17746 and then Nkind (Indic) /= N_Subtype_Indication
17747 loop
17748 Indic := Parent (Indic);
17749 end loop;
17751 if Present (Indic) then
17752 Def_Id := Etype (Subtype_Mark (Indic));
17754 Insert_Range_Checks
17755 (R_Checks,
17756 Insert_Node,
17757 Def_Id,
17758 Sloc (Insert_Node),
17760 Do_Before => True);
17761 end if;
17762 end;
17764 -- Insertion before a declaration. If the declaration
17765 -- includes discriminants, the list of applicable checks
17766 -- is given by the caller.
17768 elsif Nkind (Insert_Node) in N_Declaration then
17769 Def_Id := Defining_Identifier (Insert_Node);
17771 if (Ekind (Def_Id) = E_Record_Type
17772 and then Depends_On_Discriminant (R))
17773 or else
17774 (Ekind (Def_Id) = E_Protected_Type
17775 and then Has_Discriminants (Def_Id))
17776 then
17777 Append_Range_Checks
17778 (R_Checks,
17779 Check_List, Def_Id, Sloc (Insert_Node), R);
17781 else
17782 Insert_Range_Checks
17783 (R_Checks,
17784 Insert_Node, Def_Id, Sloc (Insert_Node), R);
17786 end if;
17788 -- Insertion before a statement. Range appears in the
17789 -- context of a quantified expression. Insertion will
17790 -- take place when expression is expanded.
17792 else
17793 null;
17794 end if;
17795 end if;
17796 end if;
17797 end if;
17799 -- Case of other than an explicit N_Range node
17801 elsif Expander_Active then
17802 Get_Index_Bounds (R, Lo, Hi);
17803 Force_Evaluation (Lo);
17804 Force_Evaluation (Hi);
17805 end if;
17806 end Process_Range_Expr_In_Decl;
17808 --------------------------------------
17809 -- Process_Real_Range_Specification --
17810 --------------------------------------
17812 procedure Process_Real_Range_Specification (Def : Node_Id) is
17813 Spec : constant Node_Id := Real_Range_Specification (Def);
17814 Lo : Node_Id;
17815 Hi : Node_Id;
17816 Err : Boolean := False;
17818 procedure Analyze_Bound (N : Node_Id);
17819 -- Analyze and check one bound
17821 -------------------
17822 -- Analyze_Bound --
17823 -------------------
17825 procedure Analyze_Bound (N : Node_Id) is
17826 begin
17827 Analyze_And_Resolve (N, Any_Real);
17829 if not Is_OK_Static_Expression (N) then
17830 Flag_Non_Static_Expr
17831 ("bound in real type definition is not static!", N);
17832 Err := True;
17833 end if;
17834 end Analyze_Bound;
17836 -- Start of processing for Process_Real_Range_Specification
17838 begin
17839 if Present (Spec) then
17840 Lo := Low_Bound (Spec);
17841 Hi := High_Bound (Spec);
17842 Analyze_Bound (Lo);
17843 Analyze_Bound (Hi);
17845 -- If error, clear away junk range specification
17847 if Err then
17848 Set_Real_Range_Specification (Def, Empty);
17849 end if;
17850 end if;
17851 end Process_Real_Range_Specification;
17853 ---------------------
17854 -- Process_Subtype --
17855 ---------------------
17857 function Process_Subtype
17858 (S : Node_Id;
17859 Related_Nod : Node_Id;
17860 Related_Id : Entity_Id := Empty;
17861 Suffix : Character := ' ') return Entity_Id
17863 P : Node_Id;
17864 Def_Id : Entity_Id;
17865 Error_Node : Node_Id;
17866 Full_View_Id : Entity_Id;
17867 Subtype_Mark_Id : Entity_Id;
17869 May_Have_Null_Exclusion : Boolean;
17871 procedure Check_Incomplete (T : Entity_Id);
17872 -- Called to verify that an incomplete type is not used prematurely
17874 ----------------------
17875 -- Check_Incomplete --
17876 ----------------------
17878 procedure Check_Incomplete (T : Entity_Id) is
17879 begin
17880 -- Ada 2005 (AI-412): Incomplete subtypes are legal
17882 if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
17883 and then
17884 not (Ada_Version >= Ada_2005
17885 and then
17886 (Nkind (Parent (T)) = N_Subtype_Declaration
17887 or else
17888 (Nkind (Parent (T)) = N_Subtype_Indication
17889 and then Nkind (Parent (Parent (T))) =
17890 N_Subtype_Declaration)))
17891 then
17892 Error_Msg_N ("invalid use of type before its full declaration", T);
17893 end if;
17894 end Check_Incomplete;
17896 -- Start of processing for Process_Subtype
17898 begin
17899 -- Case of no constraints present
17901 if Nkind (S) /= N_Subtype_Indication then
17902 Find_Type (S);
17903 Check_Incomplete (S);
17904 P := Parent (S);
17906 -- Ada 2005 (AI-231): Static check
17908 if Ada_Version >= Ada_2005
17909 and then Present (P)
17910 and then Null_Exclusion_Present (P)
17911 and then Nkind (P) /= N_Access_To_Object_Definition
17912 and then not Is_Access_Type (Entity (S))
17913 then
17914 Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
17915 end if;
17917 -- The following is ugly, can't we have a range or even a flag???
17919 May_Have_Null_Exclusion :=
17920 Nkind_In (P, N_Access_Definition,
17921 N_Access_Function_Definition,
17922 N_Access_Procedure_Definition,
17923 N_Access_To_Object_Definition,
17924 N_Allocator,
17925 N_Component_Definition)
17926 or else
17927 Nkind_In (P, N_Derived_Type_Definition,
17928 N_Discriminant_Specification,
17929 N_Formal_Object_Declaration,
17930 N_Object_Declaration,
17931 N_Object_Renaming_Declaration,
17932 N_Parameter_Specification,
17933 N_Subtype_Declaration);
17935 -- Create an Itype that is a duplicate of Entity (S) but with the
17936 -- null-exclusion attribute.
17938 if May_Have_Null_Exclusion
17939 and then Is_Access_Type (Entity (S))
17940 and then Null_Exclusion_Present (P)
17942 -- No need to check the case of an access to object definition.
17943 -- It is correct to define double not-null pointers.
17945 -- Example:
17946 -- type Not_Null_Int_Ptr is not null access Integer;
17947 -- type Acc is not null access Not_Null_Int_Ptr;
17949 and then Nkind (P) /= N_Access_To_Object_Definition
17950 then
17951 if Can_Never_Be_Null (Entity (S)) then
17952 case Nkind (Related_Nod) is
17953 when N_Full_Type_Declaration =>
17954 if Nkind (Type_Definition (Related_Nod))
17955 in N_Array_Type_Definition
17956 then
17957 Error_Node :=
17958 Subtype_Indication
17959 (Component_Definition
17960 (Type_Definition (Related_Nod)));
17961 else
17962 Error_Node :=
17963 Subtype_Indication (Type_Definition (Related_Nod));
17964 end if;
17966 when N_Subtype_Declaration =>
17967 Error_Node := Subtype_Indication (Related_Nod);
17969 when N_Object_Declaration =>
17970 Error_Node := Object_Definition (Related_Nod);
17972 when N_Component_Declaration =>
17973 Error_Node :=
17974 Subtype_Indication (Component_Definition (Related_Nod));
17976 when N_Allocator =>
17977 Error_Node := Expression (Related_Nod);
17979 when others =>
17980 pragma Assert (False);
17981 Error_Node := Related_Nod;
17982 end case;
17984 Error_Msg_NE
17985 ("`NOT NULL` not allowed (& already excludes null)",
17986 Error_Node,
17987 Entity (S));
17988 end if;
17990 Set_Etype (S,
17991 Create_Null_Excluding_Itype
17992 (T => Entity (S),
17993 Related_Nod => P));
17994 Set_Entity (S, Etype (S));
17995 end if;
17997 return Entity (S);
17999 -- Case of constraint present, so that we have an N_Subtype_Indication
18000 -- node (this node is created only if constraints are present).
18002 else
18003 Find_Type (Subtype_Mark (S));
18005 if Nkind (Parent (S)) /= N_Access_To_Object_Definition
18006 and then not
18007 (Nkind (Parent (S)) = N_Subtype_Declaration
18008 and then Is_Itype (Defining_Identifier (Parent (S))))
18009 then
18010 Check_Incomplete (Subtype_Mark (S));
18011 end if;
18013 P := Parent (S);
18014 Subtype_Mark_Id := Entity (Subtype_Mark (S));
18016 -- Explicit subtype declaration case
18018 if Nkind (P) = N_Subtype_Declaration then
18019 Def_Id := Defining_Identifier (P);
18021 -- Explicit derived type definition case
18023 elsif Nkind (P) = N_Derived_Type_Definition then
18024 Def_Id := Defining_Identifier (Parent (P));
18026 -- Implicit case, the Def_Id must be created as an implicit type.
18027 -- The one exception arises in the case of concurrent types, array
18028 -- and access types, where other subsidiary implicit types may be
18029 -- created and must appear before the main implicit type. In these
18030 -- cases we leave Def_Id set to Empty as a signal that Create_Itype
18031 -- has not yet been called to create Def_Id.
18033 else
18034 if Is_Array_Type (Subtype_Mark_Id)
18035 or else Is_Concurrent_Type (Subtype_Mark_Id)
18036 or else Is_Access_Type (Subtype_Mark_Id)
18037 then
18038 Def_Id := Empty;
18040 -- For the other cases, we create a new unattached Itype,
18041 -- and set the indication to ensure it gets attached later.
18043 else
18044 Def_Id :=
18045 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
18046 end if;
18047 end if;
18049 -- If the kind of constraint is invalid for this kind of type,
18050 -- then give an error, and then pretend no constraint was given.
18052 if not Is_Valid_Constraint_Kind
18053 (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
18054 then
18055 Error_Msg_N
18056 ("incorrect constraint for this kind of type", Constraint (S));
18058 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
18060 -- Set Ekind of orphan itype, to prevent cascaded errors
18062 if Present (Def_Id) then
18063 Set_Ekind (Def_Id, Ekind (Any_Type));
18064 end if;
18066 -- Make recursive call, having got rid of the bogus constraint
18068 return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
18069 end if;
18071 -- Remaining processing depends on type
18073 case Ekind (Subtype_Mark_Id) is
18074 when Access_Kind =>
18075 Constrain_Access (Def_Id, S, Related_Nod);
18077 if Expander_Active
18078 and then Is_Itype (Designated_Type (Def_Id))
18079 and then Nkind (Related_Nod) = N_Subtype_Declaration
18080 and then not Is_Incomplete_Type (Designated_Type (Def_Id))
18081 then
18082 Build_Itype_Reference
18083 (Designated_Type (Def_Id), Related_Nod);
18084 end if;
18086 when Array_Kind =>
18087 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
18089 when Decimal_Fixed_Point_Kind =>
18090 Constrain_Decimal (Def_Id, S);
18092 when Enumeration_Kind =>
18093 Constrain_Enumeration (Def_Id, S);
18095 when Ordinary_Fixed_Point_Kind =>
18096 Constrain_Ordinary_Fixed (Def_Id, S);
18098 when Float_Kind =>
18099 Constrain_Float (Def_Id, S);
18101 when Integer_Kind =>
18102 Constrain_Integer (Def_Id, S);
18104 when E_Record_Type |
18105 E_Record_Subtype |
18106 Class_Wide_Kind |
18107 E_Incomplete_Type =>
18108 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
18110 if Ekind (Def_Id) = E_Incomplete_Type then
18111 Set_Private_Dependents (Def_Id, New_Elmt_List);
18112 end if;
18114 when Private_Kind =>
18115 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
18116 Set_Private_Dependents (Def_Id, New_Elmt_List);
18118 -- In case of an invalid constraint prevent further processing
18119 -- since the type constructed is missing expected fields.
18121 if Etype (Def_Id) = Any_Type then
18122 return Def_Id;
18123 end if;
18125 -- If the full view is that of a task with discriminants,
18126 -- we must constrain both the concurrent type and its
18127 -- corresponding record type. Otherwise we will just propagate
18128 -- the constraint to the full view, if available.
18130 if Present (Full_View (Subtype_Mark_Id))
18131 and then Has_Discriminants (Subtype_Mark_Id)
18132 and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
18133 then
18134 Full_View_Id :=
18135 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
18137 Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
18138 Constrain_Concurrent (Full_View_Id, S,
18139 Related_Nod, Related_Id, Suffix);
18140 Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
18141 Set_Full_View (Def_Id, Full_View_Id);
18143 -- Introduce an explicit reference to the private subtype,
18144 -- to prevent scope anomalies in gigi if first use appears
18145 -- in a nested context, e.g. a later function body.
18146 -- Should this be generated in other contexts than a full
18147 -- type declaration?
18149 if Is_Itype (Def_Id)
18150 and then
18151 Nkind (Parent (P)) = N_Full_Type_Declaration
18152 then
18153 Build_Itype_Reference (Def_Id, Parent (P));
18154 end if;
18156 else
18157 Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
18158 end if;
18160 when Concurrent_Kind =>
18161 Constrain_Concurrent (Def_Id, S,
18162 Related_Nod, Related_Id, Suffix);
18164 when others =>
18165 Error_Msg_N ("invalid subtype mark in subtype indication", S);
18166 end case;
18168 -- Size and Convention are always inherited from the base type
18170 Set_Size_Info (Def_Id, (Subtype_Mark_Id));
18171 Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
18173 return Def_Id;
18174 end if;
18175 end Process_Subtype;
18177 ---------------------------------------
18178 -- Check_Anonymous_Access_Components --
18179 ---------------------------------------
18181 procedure Check_Anonymous_Access_Components
18182 (Typ_Decl : Node_Id;
18183 Typ : Entity_Id;
18184 Prev : Entity_Id;
18185 Comp_List : Node_Id)
18187 Loc : constant Source_Ptr := Sloc (Typ_Decl);
18188 Anon_Access : Entity_Id;
18189 Acc_Def : Node_Id;
18190 Comp : Node_Id;
18191 Comp_Def : Node_Id;
18192 Decl : Node_Id;
18193 Type_Def : Node_Id;
18195 procedure Build_Incomplete_Type_Declaration;
18196 -- If the record type contains components that include an access to the
18197 -- current record, then create an incomplete type declaration for the
18198 -- record, to be used as the designated type of the anonymous access.
18199 -- This is done only once, and only if there is no previous partial
18200 -- view of the type.
18202 function Designates_T (Subt : Node_Id) return Boolean;
18203 -- Check whether a node designates the enclosing record type, or 'Class
18204 -- of that type
18206 function Mentions_T (Acc_Def : Node_Id) return Boolean;
18207 -- Check whether an access definition includes a reference to
18208 -- the enclosing record type. The reference can be a subtype mark
18209 -- in the access definition itself, a 'Class attribute reference, or
18210 -- recursively a reference appearing in a parameter specification
18211 -- or result definition of an access_to_subprogram definition.
18213 --------------------------------------
18214 -- Build_Incomplete_Type_Declaration --
18215 --------------------------------------
18217 procedure Build_Incomplete_Type_Declaration is
18218 Decl : Node_Id;
18219 Inc_T : Entity_Id;
18220 H : Entity_Id;
18222 -- Is_Tagged indicates whether the type is tagged. It is tagged if
18223 -- it's "is new ... with record" or else "is tagged record ...".
18225 Is_Tagged : constant Boolean :=
18226 (Nkind (Type_Definition (Typ_Decl)) = N_Derived_Type_Definition
18227 and then
18228 Present
18229 (Record_Extension_Part (Type_Definition (Typ_Decl))))
18230 or else
18231 (Nkind (Type_Definition (Typ_Decl)) = N_Record_Definition
18232 and then Tagged_Present (Type_Definition (Typ_Decl)));
18234 begin
18235 -- If there is a previous partial view, no need to create a new one
18236 -- If the partial view, given by Prev, is incomplete, If Prev is
18237 -- a private declaration, full declaration is flagged accordingly.
18239 if Prev /= Typ then
18240 if Is_Tagged then
18241 Make_Class_Wide_Type (Prev);
18242 Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
18243 Set_Etype (Class_Wide_Type (Typ), Typ);
18244 end if;
18246 return;
18248 elsif Has_Private_Declaration (Typ) then
18250 -- If we refer to T'Class inside T, and T is the completion of a
18251 -- private type, then we need to make sure the class-wide type
18252 -- exists.
18254 if Is_Tagged then
18255 Make_Class_Wide_Type (Typ);
18256 end if;
18258 return;
18260 -- If there was a previous anonymous access type, the incomplete
18261 -- type declaration will have been created already.
18263 elsif Present (Current_Entity (Typ))
18264 and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
18265 and then Full_View (Current_Entity (Typ)) = Typ
18266 then
18267 if Is_Tagged
18268 and then Comes_From_Source (Current_Entity (Typ))
18269 and then not Is_Tagged_Type (Current_Entity (Typ))
18270 then
18271 Make_Class_Wide_Type (Typ);
18272 Error_Msg_N
18273 ("incomplete view of tagged type should be declared tagged?",
18274 Parent (Current_Entity (Typ)));
18275 end if;
18276 return;
18278 else
18279 Inc_T := Make_Defining_Identifier (Loc, Chars (Typ));
18280 Decl := Make_Incomplete_Type_Declaration (Loc, Inc_T);
18282 -- Type has already been inserted into the current scope. Remove
18283 -- it, and add incomplete declaration for type, so that subsequent
18284 -- anonymous access types can use it. The entity is unchained from
18285 -- the homonym list and from immediate visibility. After analysis,
18286 -- the entity in the incomplete declaration becomes immediately
18287 -- visible in the record declaration that follows.
18289 H := Current_Entity (Typ);
18291 if H = Typ then
18292 Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
18293 else
18294 while Present (H)
18295 and then Homonym (H) /= Typ
18296 loop
18297 H := Homonym (Typ);
18298 end loop;
18300 Set_Homonym (H, Homonym (Typ));
18301 end if;
18303 Insert_Before (Typ_Decl, Decl);
18304 Analyze (Decl);
18305 Set_Full_View (Inc_T, Typ);
18307 if Is_Tagged then
18309 -- Create a common class-wide type for both views, and set the
18310 -- Etype of the class-wide type to the full view.
18312 Make_Class_Wide_Type (Inc_T);
18313 Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
18314 Set_Etype (Class_Wide_Type (Typ), Typ);
18315 end if;
18316 end if;
18317 end Build_Incomplete_Type_Declaration;
18319 ------------------
18320 -- Designates_T --
18321 ------------------
18323 function Designates_T (Subt : Node_Id) return Boolean is
18324 Type_Id : constant Name_Id := Chars (Typ);
18326 function Names_T (Nam : Node_Id) return Boolean;
18327 -- The record type has not been introduced in the current scope
18328 -- yet, so we must examine the name of the type itself, either
18329 -- an identifier T, or an expanded name of the form P.T, where
18330 -- P denotes the current scope.
18332 -------------
18333 -- Names_T --
18334 -------------
18336 function Names_T (Nam : Node_Id) return Boolean is
18337 begin
18338 if Nkind (Nam) = N_Identifier then
18339 return Chars (Nam) = Type_Id;
18341 elsif Nkind (Nam) = N_Selected_Component then
18342 if Chars (Selector_Name (Nam)) = Type_Id then
18343 if Nkind (Prefix (Nam)) = N_Identifier then
18344 return Chars (Prefix (Nam)) = Chars (Current_Scope);
18346 elsif Nkind (Prefix (Nam)) = N_Selected_Component then
18347 return Chars (Selector_Name (Prefix (Nam))) =
18348 Chars (Current_Scope);
18349 else
18350 return False;
18351 end if;
18353 else
18354 return False;
18355 end if;
18357 else
18358 return False;
18359 end if;
18360 end Names_T;
18362 -- Start of processing for Designates_T
18364 begin
18365 if Nkind (Subt) = N_Identifier then
18366 return Chars (Subt) = Type_Id;
18368 -- Reference can be through an expanded name which has not been
18369 -- analyzed yet, and which designates enclosing scopes.
18371 elsif Nkind (Subt) = N_Selected_Component then
18372 if Names_T (Subt) then
18373 return True;
18375 -- Otherwise it must denote an entity that is already visible.
18376 -- The access definition may name a subtype of the enclosing
18377 -- type, if there is a previous incomplete declaration for it.
18379 else
18380 Find_Selected_Component (Subt);
18381 return
18382 Is_Entity_Name (Subt)
18383 and then Scope (Entity (Subt)) = Current_Scope
18384 and then
18385 (Chars (Base_Type (Entity (Subt))) = Type_Id
18386 or else
18387 (Is_Class_Wide_Type (Entity (Subt))
18388 and then
18389 Chars (Etype (Base_Type (Entity (Subt)))) =
18390 Type_Id));
18391 end if;
18393 -- A reference to the current type may appear as the prefix of
18394 -- a 'Class attribute.
18396 elsif Nkind (Subt) = N_Attribute_Reference
18397 and then Attribute_Name (Subt) = Name_Class
18398 then
18399 return Names_T (Prefix (Subt));
18401 else
18402 return False;
18403 end if;
18404 end Designates_T;
18406 ----------------
18407 -- Mentions_T --
18408 ----------------
18410 function Mentions_T (Acc_Def : Node_Id) return Boolean is
18411 Param_Spec : Node_Id;
18413 Acc_Subprg : constant Node_Id :=
18414 Access_To_Subprogram_Definition (Acc_Def);
18416 begin
18417 if No (Acc_Subprg) then
18418 return Designates_T (Subtype_Mark (Acc_Def));
18419 end if;
18421 -- Component is an access_to_subprogram: examine its formals,
18422 -- and result definition in the case of an access_to_function.
18424 Param_Spec := First (Parameter_Specifications (Acc_Subprg));
18425 while Present (Param_Spec) loop
18426 if Nkind (Parameter_Type (Param_Spec)) = N_Access_Definition
18427 and then Mentions_T (Parameter_Type (Param_Spec))
18428 then
18429 return True;
18431 elsif Designates_T (Parameter_Type (Param_Spec)) then
18432 return True;
18433 end if;
18435 Next (Param_Spec);
18436 end loop;
18438 if Nkind (Acc_Subprg) = N_Access_Function_Definition then
18439 if Nkind (Result_Definition (Acc_Subprg)) =
18440 N_Access_Definition
18441 then
18442 return Mentions_T (Result_Definition (Acc_Subprg));
18443 else
18444 return Designates_T (Result_Definition (Acc_Subprg));
18445 end if;
18446 end if;
18448 return False;
18449 end Mentions_T;
18451 -- Start of processing for Check_Anonymous_Access_Components
18453 begin
18454 if No (Comp_List) then
18455 return;
18456 end if;
18458 Comp := First (Component_Items (Comp_List));
18459 while Present (Comp) loop
18460 if Nkind (Comp) = N_Component_Declaration
18461 and then Present
18462 (Access_Definition (Component_Definition (Comp)))
18463 and then
18464 Mentions_T (Access_Definition (Component_Definition (Comp)))
18465 then
18466 Comp_Def := Component_Definition (Comp);
18467 Acc_Def :=
18468 Access_To_Subprogram_Definition
18469 (Access_Definition (Comp_Def));
18471 Build_Incomplete_Type_Declaration;
18472 Anon_Access := Make_Temporary (Loc, 'S');
18474 -- Create a declaration for the anonymous access type: either
18475 -- an access_to_object or an access_to_subprogram.
18477 if Present (Acc_Def) then
18478 if Nkind (Acc_Def) = N_Access_Function_Definition then
18479 Type_Def :=
18480 Make_Access_Function_Definition (Loc,
18481 Parameter_Specifications =>
18482 Parameter_Specifications (Acc_Def),
18483 Result_Definition => Result_Definition (Acc_Def));
18484 else
18485 Type_Def :=
18486 Make_Access_Procedure_Definition (Loc,
18487 Parameter_Specifications =>
18488 Parameter_Specifications (Acc_Def));
18489 end if;
18491 else
18492 Type_Def :=
18493 Make_Access_To_Object_Definition (Loc,
18494 Subtype_Indication =>
18495 Relocate_Node
18496 (Subtype_Mark
18497 (Access_Definition (Comp_Def))));
18499 Set_Constant_Present
18500 (Type_Def, Constant_Present (Access_Definition (Comp_Def)));
18501 Set_All_Present
18502 (Type_Def, All_Present (Access_Definition (Comp_Def)));
18503 end if;
18505 Set_Null_Exclusion_Present
18506 (Type_Def,
18507 Null_Exclusion_Present (Access_Definition (Comp_Def)));
18509 Decl :=
18510 Make_Full_Type_Declaration (Loc,
18511 Defining_Identifier => Anon_Access,
18512 Type_Definition => Type_Def);
18514 Insert_Before (Typ_Decl, Decl);
18515 Analyze (Decl);
18517 -- If an access to object, Preserve entity of designated type,
18518 -- for ASIS use, before rewriting the component definition.
18520 if No (Acc_Def) then
18521 declare
18522 Desig : Entity_Id;
18524 begin
18525 Desig := Entity (Subtype_Indication (Type_Def));
18527 -- If the access definition is to the current record,
18528 -- the visible entity at this point is an incomplete
18529 -- type. Retrieve the full view to simplify ASIS queries
18531 if Ekind (Desig) = E_Incomplete_Type then
18532 Desig := Full_View (Desig);
18533 end if;
18535 Set_Entity
18536 (Subtype_Mark (Access_Definition (Comp_Def)), Desig);
18537 end;
18538 end if;
18540 Rewrite (Comp_Def,
18541 Make_Component_Definition (Loc,
18542 Subtype_Indication =>
18543 New_Occurrence_Of (Anon_Access, Loc)));
18545 if Ekind (Designated_Type (Anon_Access)) = E_Subprogram_Type then
18546 Set_Ekind (Anon_Access, E_Anonymous_Access_Subprogram_Type);
18547 else
18548 Set_Ekind (Anon_Access, E_Anonymous_Access_Type);
18549 end if;
18551 Set_Is_Local_Anonymous_Access (Anon_Access);
18552 end if;
18554 Next (Comp);
18555 end loop;
18557 if Present (Variant_Part (Comp_List)) then
18558 declare
18559 V : Node_Id;
18560 begin
18561 V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
18562 while Present (V) loop
18563 Check_Anonymous_Access_Components
18564 (Typ_Decl, Typ, Prev, Component_List (V));
18565 Next_Non_Pragma (V);
18566 end loop;
18567 end;
18568 end if;
18569 end Check_Anonymous_Access_Components;
18571 --------------------------------
18572 -- Preanalyze_Spec_Expression --
18573 --------------------------------
18575 procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
18576 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
18577 begin
18578 In_Spec_Expression := True;
18579 Preanalyze_And_Resolve (N, T);
18580 In_Spec_Expression := Save_In_Spec_Expression;
18581 end Preanalyze_Spec_Expression;
18583 -----------------------------
18584 -- Record_Type_Declaration --
18585 -----------------------------
18587 procedure Record_Type_Declaration
18588 (T : Entity_Id;
18589 N : Node_Id;
18590 Prev : Entity_Id)
18592 Def : constant Node_Id := Type_Definition (N);
18593 Is_Tagged : Boolean;
18594 Tag_Comp : Entity_Id;
18596 begin
18597 -- These flags must be initialized before calling Process_Discriminants
18598 -- because this routine makes use of them.
18600 Set_Ekind (T, E_Record_Type);
18601 Set_Etype (T, T);
18602 Init_Size_Align (T);
18603 Set_Interfaces (T, No_Elist);
18604 Set_Stored_Constraint (T, No_Elist);
18606 -- Normal case
18608 if Ada_Version < Ada_2005
18609 or else not Interface_Present (Def)
18610 then
18611 -- The flag Is_Tagged_Type might have already been set by
18612 -- Find_Type_Name if it detected an error for declaration T. This
18613 -- arises in the case of private tagged types where the full view
18614 -- omits the word tagged.
18616 Is_Tagged :=
18617 Tagged_Present (Def)
18618 or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
18620 Set_Is_Tagged_Type (T, Is_Tagged);
18621 Set_Is_Limited_Record (T, Limited_Present (Def));
18623 -- Type is abstract if full declaration carries keyword, or if
18624 -- previous partial view did.
18626 Set_Is_Abstract_Type (T, Is_Abstract_Type (T)
18627 or else Abstract_Present (Def));
18629 else
18630 Is_Tagged := True;
18631 Analyze_Interface_Declaration (T, Def);
18633 if Present (Discriminant_Specifications (N)) then
18634 Error_Msg_N
18635 ("interface types cannot have discriminants",
18636 Defining_Identifier
18637 (First (Discriminant_Specifications (N))));
18638 end if;
18639 end if;
18641 -- First pass: if there are self-referential access components,
18642 -- create the required anonymous access type declarations, and if
18643 -- need be an incomplete type declaration for T itself.
18645 Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
18647 if Ada_Version >= Ada_2005
18648 and then Present (Interface_List (Def))
18649 then
18650 Check_Interfaces (N, Def);
18652 declare
18653 Ifaces_List : Elist_Id;
18655 begin
18656 -- Ada 2005 (AI-251): Collect the list of progenitors that are not
18657 -- already in the parents.
18659 Collect_Interfaces
18660 (T => T,
18661 Ifaces_List => Ifaces_List,
18662 Exclude_Parents => True);
18664 Set_Interfaces (T, Ifaces_List);
18665 end;
18666 end if;
18668 -- Records constitute a scope for the component declarations within.
18669 -- The scope is created prior to the processing of these declarations.
18670 -- Discriminants are processed first, so that they are visible when
18671 -- processing the other components. The Ekind of the record type itself
18672 -- is set to E_Record_Type (subtypes appear as E_Record_Subtype).
18674 -- Enter record scope
18676 Push_Scope (T);
18678 -- If an incomplete or private type declaration was already given for
18679 -- the type, then this scope already exists, and the discriminants have
18680 -- been declared within. We must verify that the full declaration
18681 -- matches the incomplete one.
18683 Check_Or_Process_Discriminants (N, T, Prev);
18685 Set_Is_Constrained (T, not Has_Discriminants (T));
18686 Set_Has_Delayed_Freeze (T, True);
18688 -- For tagged types add a manually analyzed component corresponding
18689 -- to the component _tag, the corresponding piece of tree will be
18690 -- expanded as part of the freezing actions if it is not a CPP_Class.
18692 if Is_Tagged then
18694 -- Do not add the tag unless we are in expansion mode
18696 if Expander_Active then
18697 Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
18698 Enter_Name (Tag_Comp);
18700 Set_Ekind (Tag_Comp, E_Component);
18701 Set_Is_Tag (Tag_Comp);
18702 Set_Is_Aliased (Tag_Comp);
18703 Set_Etype (Tag_Comp, RTE (RE_Tag));
18704 Set_DT_Entry_Count (Tag_Comp, No_Uint);
18705 Set_Original_Record_Component (Tag_Comp, Tag_Comp);
18706 Init_Component_Location (Tag_Comp);
18708 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
18709 -- implemented interfaces.
18711 if Has_Interfaces (T) then
18712 Add_Interface_Tag_Components (N, T);
18713 end if;
18714 end if;
18716 Make_Class_Wide_Type (T);
18717 Set_Direct_Primitive_Operations (T, New_Elmt_List);
18718 end if;
18720 -- We must suppress range checks when processing record components in
18721 -- the presence of discriminants, since we don't want spurious checks to
18722 -- be generated during their analysis, but Suppress_Range_Checks flags
18723 -- must be reset the after processing the record definition.
18725 -- Note: this is the only use of Kill_Range_Checks, and is a bit odd,
18726 -- couldn't we just use the normal range check suppression method here.
18727 -- That would seem cleaner ???
18729 if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
18730 Set_Kill_Range_Checks (T, True);
18731 Record_Type_Definition (Def, Prev);
18732 Set_Kill_Range_Checks (T, False);
18733 else
18734 Record_Type_Definition (Def, Prev);
18735 end if;
18737 -- Exit from record scope
18739 End_Scope;
18741 -- Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
18742 -- the implemented interfaces and associate them an aliased entity.
18744 if Is_Tagged
18745 and then not Is_Empty_List (Interface_List (Def))
18746 then
18747 Derive_Progenitor_Subprograms (T, T);
18748 end if;
18749 end Record_Type_Declaration;
18751 ----------------------------
18752 -- Record_Type_Definition --
18753 ----------------------------
18755 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
18756 Component : Entity_Id;
18757 Ctrl_Components : Boolean := False;
18758 Final_Storage_Only : Boolean;
18759 T : Entity_Id;
18761 begin
18762 if Ekind (Prev_T) = E_Incomplete_Type then
18763 T := Full_View (Prev_T);
18764 else
18765 T := Prev_T;
18766 end if;
18768 Final_Storage_Only := not Is_Controlled (T);
18770 -- Ada 2005: check whether an explicit Limited is present in a derived
18771 -- type declaration.
18773 if Nkind (Parent (Def)) = N_Derived_Type_Definition
18774 and then Limited_Present (Parent (Def))
18775 then
18776 Set_Is_Limited_Record (T);
18777 end if;
18779 -- If the component list of a record type is defined by the reserved
18780 -- word null and there is no discriminant part, then the record type has
18781 -- no components and all records of the type are null records (RM 3.7)
18782 -- This procedure is also called to process the extension part of a
18783 -- record extension, in which case the current scope may have inherited
18784 -- components.
18786 if No (Def)
18787 or else No (Component_List (Def))
18788 or else Null_Present (Component_List (Def))
18789 then
18790 null;
18792 else
18793 Analyze_Declarations (Component_Items (Component_List (Def)));
18795 if Present (Variant_Part (Component_List (Def))) then
18796 Analyze (Variant_Part (Component_List (Def)));
18797 end if;
18798 end if;
18800 -- After completing the semantic analysis of the record definition,
18801 -- record components, both new and inherited, are accessible. Set their
18802 -- kind accordingly. Exclude malformed itypes from illegal declarations,
18803 -- whose Ekind may be void.
18805 Component := First_Entity (Current_Scope);
18806 while Present (Component) loop
18807 if Ekind (Component) = E_Void
18808 and then not Is_Itype (Component)
18809 then
18810 Set_Ekind (Component, E_Component);
18811 Init_Component_Location (Component);
18812 end if;
18814 if Has_Task (Etype (Component)) then
18815 Set_Has_Task (T);
18816 end if;
18818 if Ekind (Component) /= E_Component then
18819 null;
18821 -- Do not set Has_Controlled_Component on a class-wide equivalent
18822 -- type. See Make_CW_Equivalent_Type.
18824 elsif not Is_Class_Wide_Equivalent_Type (T)
18825 and then (Has_Controlled_Component (Etype (Component))
18826 or else (Chars (Component) /= Name_uParent
18827 and then Is_Controlled (Etype (Component))))
18828 then
18829 Set_Has_Controlled_Component (T, True);
18830 Final_Storage_Only :=
18831 Final_Storage_Only
18832 and then Finalize_Storage_Only (Etype (Component));
18833 Ctrl_Components := True;
18834 end if;
18836 Next_Entity (Component);
18837 end loop;
18839 -- A Type is Finalize_Storage_Only only if all its controlled components
18840 -- are also.
18842 if Ctrl_Components then
18843 Set_Finalize_Storage_Only (T, Final_Storage_Only);
18844 end if;
18846 -- Place reference to end record on the proper entity, which may
18847 -- be a partial view.
18849 if Present (Def) then
18850 Process_End_Label (Def, 'e', Prev_T);
18851 end if;
18852 end Record_Type_Definition;
18854 ------------------------
18855 -- Replace_Components --
18856 ------------------------
18858 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id) is
18859 function Process (N : Node_Id) return Traverse_Result;
18861 -------------
18862 -- Process --
18863 -------------
18865 function Process (N : Node_Id) return Traverse_Result is
18866 Comp : Entity_Id;
18868 begin
18869 if Nkind (N) = N_Discriminant_Specification then
18870 Comp := First_Discriminant (Typ);
18871 while Present (Comp) loop
18872 if Chars (Comp) = Chars (Defining_Identifier (N)) then
18873 Set_Defining_Identifier (N, Comp);
18874 exit;
18875 end if;
18877 Next_Discriminant (Comp);
18878 end loop;
18880 elsif Nkind (N) = N_Component_Declaration then
18881 Comp := First_Component (Typ);
18882 while Present (Comp) loop
18883 if Chars (Comp) = Chars (Defining_Identifier (N)) then
18884 Set_Defining_Identifier (N, Comp);
18885 exit;
18886 end if;
18888 Next_Component (Comp);
18889 end loop;
18890 end if;
18892 return OK;
18893 end Process;
18895 procedure Replace is new Traverse_Proc (Process);
18897 -- Start of processing for Replace_Components
18899 begin
18900 Replace (Decl);
18901 end Replace_Components;
18903 -------------------------------
18904 -- Set_Completion_Referenced --
18905 -------------------------------
18907 procedure Set_Completion_Referenced (E : Entity_Id) is
18908 begin
18909 -- If in main unit, mark entity that is a completion as referenced,
18910 -- warnings go on the partial view when needed.
18912 if In_Extended_Main_Source_Unit (E) then
18913 Set_Referenced (E);
18914 end if;
18915 end Set_Completion_Referenced;
18917 ---------------------
18918 -- Set_Fixed_Range --
18919 ---------------------
18921 -- The range for fixed-point types is complicated by the fact that we
18922 -- do not know the exact end points at the time of the declaration. This
18923 -- is true for three reasons:
18925 -- A size clause may affect the fudging of the end-points
18926 -- A small clause may affect the values of the end-points
18927 -- We try to include the end-points if it does not affect the size
18929 -- This means that the actual end-points must be established at the point
18930 -- when the type is frozen. Meanwhile, we first narrow the range as
18931 -- permitted (so that it will fit if necessary in a small specified size),
18932 -- and then build a range subtree with these narrowed bounds.
18934 -- Set_Fixed_Range constructs the range from real literal values, and sets
18935 -- the range as the Scalar_Range of the given fixed-point type entity.
18937 -- The parent of this range is set to point to the entity so that it is
18938 -- properly hooked into the tree (unlike normal Scalar_Range entries for
18939 -- other scalar types, which are just pointers to the range in the
18940 -- original tree, this would otherwise be an orphan).
18942 -- The tree is left unanalyzed. When the type is frozen, the processing
18943 -- in Freeze.Freeze_Fixed_Point_Type notices that the range is not
18944 -- analyzed, and uses this as an indication that it should complete
18945 -- work on the range (it will know the final small and size values).
18947 procedure Set_Fixed_Range
18948 (E : Entity_Id;
18949 Loc : Source_Ptr;
18950 Lo : Ureal;
18951 Hi : Ureal)
18953 S : constant Node_Id :=
18954 Make_Range (Loc,
18955 Low_Bound => Make_Real_Literal (Loc, Lo),
18956 High_Bound => Make_Real_Literal (Loc, Hi));
18957 begin
18958 Set_Scalar_Range (E, S);
18959 Set_Parent (S, E);
18960 end Set_Fixed_Range;
18962 ----------------------------------
18963 -- Set_Scalar_Range_For_Subtype --
18964 ----------------------------------
18966 procedure Set_Scalar_Range_For_Subtype
18967 (Def_Id : Entity_Id;
18968 R : Node_Id;
18969 Subt : Entity_Id)
18971 Kind : constant Entity_Kind := Ekind (Def_Id);
18973 begin
18974 -- Defend against previous error
18976 if Nkind (R) = N_Error then
18977 return;
18978 end if;
18980 Set_Scalar_Range (Def_Id, R);
18982 -- We need to link the range into the tree before resolving it so
18983 -- that types that are referenced, including importantly the subtype
18984 -- itself, are properly frozen (Freeze_Expression requires that the
18985 -- expression be properly linked into the tree). Of course if it is
18986 -- already linked in, then we do not disturb the current link.
18988 if No (Parent (R)) then
18989 Set_Parent (R, Def_Id);
18990 end if;
18992 -- Reset the kind of the subtype during analysis of the range, to
18993 -- catch possible premature use in the bounds themselves.
18995 Set_Ekind (Def_Id, E_Void);
18996 Process_Range_Expr_In_Decl (R, Subt);
18997 Set_Ekind (Def_Id, Kind);
18998 end Set_Scalar_Range_For_Subtype;
19000 --------------------------------------------------------
19001 -- Set_Stored_Constraint_From_Discriminant_Constraint --
19002 --------------------------------------------------------
19004 procedure Set_Stored_Constraint_From_Discriminant_Constraint
19005 (E : Entity_Id)
19007 begin
19008 -- Make sure set if encountered during Expand_To_Stored_Constraint
19010 Set_Stored_Constraint (E, No_Elist);
19012 -- Give it the right value
19014 if Is_Constrained (E) and then Has_Discriminants (E) then
19015 Set_Stored_Constraint (E,
19016 Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
19017 end if;
19018 end Set_Stored_Constraint_From_Discriminant_Constraint;
19020 -------------------------------------
19021 -- Signed_Integer_Type_Declaration --
19022 -------------------------------------
19024 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
19025 Implicit_Base : Entity_Id;
19026 Base_Typ : Entity_Id;
19027 Lo_Val : Uint;
19028 Hi_Val : Uint;
19029 Errs : Boolean := False;
19030 Lo : Node_Id;
19031 Hi : Node_Id;
19033 function Can_Derive_From (E : Entity_Id) return Boolean;
19034 -- Determine whether given bounds allow derivation from specified type
19036 procedure Check_Bound (Expr : Node_Id);
19037 -- Check bound to make sure it is integral and static. If not, post
19038 -- appropriate error message and set Errs flag
19040 ---------------------
19041 -- Can_Derive_From --
19042 ---------------------
19044 -- Note we check both bounds against both end values, to deal with
19045 -- strange types like ones with a range of 0 .. -12341234.
19047 function Can_Derive_From (E : Entity_Id) return Boolean is
19048 Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
19049 Hi : constant Uint := Expr_Value (Type_High_Bound (E));
19050 begin
19051 return Lo <= Lo_Val and then Lo_Val <= Hi
19052 and then
19053 Lo <= Hi_Val and then Hi_Val <= Hi;
19054 end Can_Derive_From;
19056 -----------------
19057 -- Check_Bound --
19058 -----------------
19060 procedure Check_Bound (Expr : Node_Id) is
19061 begin
19062 -- If a range constraint is used as an integer type definition, each
19063 -- bound of the range must be defined by a static expression of some
19064 -- integer type, but the two bounds need not have the same integer
19065 -- type (Negative bounds are allowed.) (RM 3.5.4)
19067 if not Is_Integer_Type (Etype (Expr)) then
19068 Error_Msg_N
19069 ("integer type definition bounds must be of integer type", Expr);
19070 Errs := True;
19072 elsif not Is_OK_Static_Expression (Expr) then
19073 Flag_Non_Static_Expr
19074 ("non-static expression used for integer type bound!", Expr);
19075 Errs := True;
19077 -- The bounds are folded into literals, and we set their type to be
19078 -- universal, to avoid typing difficulties: we cannot set the type
19079 -- of the literal to the new type, because this would be a forward
19080 -- reference for the back end, and if the original type is user-
19081 -- defined this can lead to spurious semantic errors (e.g. 2928-003).
19083 else
19084 if Is_Entity_Name (Expr) then
19085 Fold_Uint (Expr, Expr_Value (Expr), True);
19086 end if;
19088 Set_Etype (Expr, Universal_Integer);
19089 end if;
19090 end Check_Bound;
19092 -- Start of processing for Signed_Integer_Type_Declaration
19094 begin
19095 -- Create an anonymous base type
19097 Implicit_Base :=
19098 Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
19100 -- Analyze and check the bounds, they can be of any integer type
19102 Lo := Low_Bound (Def);
19103 Hi := High_Bound (Def);
19105 -- Arbitrarily use Integer as the type if either bound had an error
19107 if Hi = Error or else Lo = Error then
19108 Base_Typ := Any_Integer;
19109 Set_Error_Posted (T, True);
19111 -- Here both bounds are OK expressions
19113 else
19114 Analyze_And_Resolve (Lo, Any_Integer);
19115 Analyze_And_Resolve (Hi, Any_Integer);
19117 Check_Bound (Lo);
19118 Check_Bound (Hi);
19120 if Errs then
19121 Hi := Type_High_Bound (Standard_Long_Long_Integer);
19122 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
19123 end if;
19125 -- Find type to derive from
19127 Lo_Val := Expr_Value (Lo);
19128 Hi_Val := Expr_Value (Hi);
19130 if Can_Derive_From (Standard_Short_Short_Integer) then
19131 Base_Typ := Base_Type (Standard_Short_Short_Integer);
19133 elsif Can_Derive_From (Standard_Short_Integer) then
19134 Base_Typ := Base_Type (Standard_Short_Integer);
19136 elsif Can_Derive_From (Standard_Integer) then
19137 Base_Typ := Base_Type (Standard_Integer);
19139 elsif Can_Derive_From (Standard_Long_Integer) then
19140 Base_Typ := Base_Type (Standard_Long_Integer);
19142 elsif Can_Derive_From (Standard_Long_Long_Integer) then
19143 Base_Typ := Base_Type (Standard_Long_Long_Integer);
19145 else
19146 Base_Typ := Base_Type (Standard_Long_Long_Integer);
19147 Error_Msg_N ("integer type definition bounds out of range", Def);
19148 Hi := Type_High_Bound (Standard_Long_Long_Integer);
19149 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
19150 end if;
19151 end if;
19153 -- Complete both implicit base and declared first subtype entities
19155 Set_Etype (Implicit_Base, Base_Typ);
19156 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
19157 Set_Size_Info (Implicit_Base, (Base_Typ));
19158 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
19159 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
19161 Set_Ekind (T, E_Signed_Integer_Subtype);
19162 Set_Etype (T, Implicit_Base);
19164 Set_Size_Info (T, (Implicit_Base));
19165 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
19166 Set_Scalar_Range (T, Def);
19167 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
19168 Set_Is_Constrained (T);
19169 end Signed_Integer_Type_Declaration;
19171 end Sem_Ch3;