PR testsuite/44195
[official-gcc.git] / gcc / ada / sem_ch3.adb
blobd5b39f99f9d00a77367e2f57d0c03460aa77e183
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 Atree; use Atree;
27 with Checks; use Checks;
28 with Debug; use Debug;
29 with Elists; use Elists;
30 with Einfo; use Einfo;
31 with Errout; use Errout;
32 with Eval_Fat; use Eval_Fat;
33 with Exp_Ch3; use Exp_Ch3;
34 with Exp_Ch9; use Exp_Ch9;
35 with Exp_Disp; use Exp_Disp;
36 with Exp_Dist; use Exp_Dist;
37 with Exp_Tss; use Exp_Tss;
38 with Exp_Util; use Exp_Util;
39 with Fname; use Fname;
40 with Freeze; use Freeze;
41 with Itypes; use Itypes;
42 with Layout; use Layout;
43 with Lib; use Lib;
44 with Lib.Xref; use Lib.Xref;
45 with Namet; use Namet;
46 with Nmake; use Nmake;
47 with Opt; use Opt;
48 with Restrict; use Restrict;
49 with Rident; use Rident;
50 with Rtsfind; use Rtsfind;
51 with Sem; use Sem;
52 with Sem_Aux; use Sem_Aux;
53 with Sem_Case; use Sem_Case;
54 with Sem_Cat; use Sem_Cat;
55 with Sem_Ch6; use Sem_Ch6;
56 with Sem_Ch7; use Sem_Ch7;
57 with Sem_Ch8; use Sem_Ch8;
58 with Sem_Ch13; use Sem_Ch13;
59 with Sem_Disp; use Sem_Disp;
60 with Sem_Dist; use Sem_Dist;
61 with Sem_Elim; use Sem_Elim;
62 with Sem_Eval; use Sem_Eval;
63 with Sem_Mech; use Sem_Mech;
64 with Sem_Res; use Sem_Res;
65 with Sem_Smem; use Sem_Smem;
66 with Sem_Type; use Sem_Type;
67 with Sem_Util; use Sem_Util;
68 with Sem_Warn; use Sem_Warn;
69 with Stand; use Stand;
70 with Sinfo; use Sinfo;
71 with Sinput; use Sinput;
72 with Snames; use Snames;
73 with Targparm; use Targparm;
74 with Tbuild; use Tbuild;
75 with Ttypes; use Ttypes;
76 with Uintp; use Uintp;
77 with Urealp; use Urealp;
79 package body Sem_Ch3 is
81 -----------------------
82 -- Local Subprograms --
83 -----------------------
85 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id);
86 -- Ada 2005 (AI-251): Add the tag components corresponding to all the
87 -- abstract interface types implemented by a record type or a derived
88 -- record type.
90 procedure Build_Derived_Type
91 (N : Node_Id;
92 Parent_Type : Entity_Id;
93 Derived_Type : Entity_Id;
94 Is_Completion : Boolean;
95 Derive_Subps : Boolean := True);
96 -- Create and decorate a Derived_Type given the Parent_Type entity. N is
97 -- the N_Full_Type_Declaration node containing the derived type definition.
98 -- Parent_Type is the entity for the parent type in the derived type
99 -- definition and Derived_Type the actual derived type. Is_Completion must
100 -- be set to False if Derived_Type is the N_Defining_Identifier node in N
101 -- (i.e. Derived_Type = Defining_Identifier (N)). In this case N is not the
102 -- completion of a private type declaration. If Is_Completion is set to
103 -- True, N is the completion of a private type declaration and Derived_Type
104 -- is different from the defining identifier inside N (i.e. Derived_Type /=
105 -- Defining_Identifier (N)). Derive_Subps indicates whether the parent
106 -- subprograms should be derived. The only case where this parameter is
107 -- False is when Build_Derived_Type is recursively called to process an
108 -- implicit derived full type for a type derived from a private type (in
109 -- that case the subprograms must only be derived for the private view of
110 -- the type).
112 -- ??? These flags need a bit of re-examination and re-documentation:
113 -- ??? are they both necessary (both seem related to the recursion)?
115 procedure Build_Derived_Access_Type
116 (N : Node_Id;
117 Parent_Type : Entity_Id;
118 Derived_Type : Entity_Id);
119 -- Subsidiary procedure to Build_Derived_Type. For a derived access type,
120 -- create an implicit base if the parent type is constrained or if the
121 -- subtype indication has a constraint.
123 procedure Build_Derived_Array_Type
124 (N : Node_Id;
125 Parent_Type : Entity_Id;
126 Derived_Type : Entity_Id);
127 -- Subsidiary procedure to Build_Derived_Type. For a derived array type,
128 -- create an implicit base if the parent type is constrained or if the
129 -- subtype indication has a constraint.
131 procedure Build_Derived_Concurrent_Type
132 (N : Node_Id;
133 Parent_Type : Entity_Id;
134 Derived_Type : Entity_Id);
135 -- Subsidiary procedure to Build_Derived_Type. For a derived task or
136 -- protected type, inherit entries and protected subprograms, check
137 -- legality of discriminant constraints if any.
139 procedure Build_Derived_Enumeration_Type
140 (N : Node_Id;
141 Parent_Type : Entity_Id;
142 Derived_Type : Entity_Id);
143 -- Subsidiary procedure to Build_Derived_Type. For a derived enumeration
144 -- type, we must create a new list of literals. Types derived from
145 -- Character and [Wide_]Wide_Character are special-cased.
147 procedure Build_Derived_Numeric_Type
148 (N : Node_Id;
149 Parent_Type : Entity_Id;
150 Derived_Type : Entity_Id);
151 -- Subsidiary procedure to Build_Derived_Type. For numeric types, create
152 -- an anonymous base type, and propagate constraint to subtype if needed.
154 procedure Build_Derived_Private_Type
155 (N : Node_Id;
156 Parent_Type : Entity_Id;
157 Derived_Type : Entity_Id;
158 Is_Completion : Boolean;
159 Derive_Subps : Boolean := True);
160 -- Subsidiary procedure to Build_Derived_Type. This procedure is complex
161 -- because the parent may or may not have a completion, and the derivation
162 -- may itself be a completion.
164 procedure Build_Derived_Record_Type
165 (N : Node_Id;
166 Parent_Type : Entity_Id;
167 Derived_Type : Entity_Id;
168 Derive_Subps : Boolean := True);
169 -- Subsidiary procedure for Build_Derived_Type and
170 -- Analyze_Private_Extension_Declaration used for tagged and untagged
171 -- record types. All parameters are as in Build_Derived_Type except that
172 -- N, in addition to being an N_Full_Type_Declaration node, can also be an
173 -- N_Private_Extension_Declaration node. See the definition of this routine
174 -- for much more info. Derive_Subps indicates whether subprograms should
175 -- be derived from the parent type. The only case where Derive_Subps is
176 -- False is for an implicit derived full type for a type derived from a
177 -- private type (see Build_Derived_Type).
179 procedure Build_Discriminal (Discrim : Entity_Id);
180 -- Create the discriminal corresponding to discriminant Discrim, that is
181 -- the parameter corresponding to Discrim to be used in initialization
182 -- procedures for the type where Discrim is a discriminant. Discriminals
183 -- are not used during semantic analysis, and are not fully defined
184 -- entities until expansion. Thus they are not given a scope until
185 -- initialization procedures are built.
187 function Build_Discriminant_Constraints
188 (T : Entity_Id;
189 Def : Node_Id;
190 Derived_Def : Boolean := False) return Elist_Id;
191 -- Validate discriminant constraints and return the list of the constraints
192 -- in order of discriminant declarations, where T is the discriminated
193 -- unconstrained type. Def is the N_Subtype_Indication node where the
194 -- discriminants constraints for T are specified. Derived_Def is True
195 -- when building the discriminant constraints in a derived type definition
196 -- of the form "type D (...) is new T (xxx)". In this case T is the parent
197 -- type and Def is the constraint "(xxx)" on T and this routine sets the
198 -- Corresponding_Discriminant field of the discriminants in the derived
199 -- type D to point to the corresponding discriminants in the parent type T.
201 procedure Build_Discriminated_Subtype
202 (T : Entity_Id;
203 Def_Id : Entity_Id;
204 Elist : Elist_Id;
205 Related_Nod : Node_Id;
206 For_Access : Boolean := False);
207 -- Subsidiary procedure to Constrain_Discriminated_Type and to
208 -- Process_Incomplete_Dependents. Given
210 -- T (a possibly discriminated base type)
211 -- Def_Id (a very partially built subtype for T),
213 -- the call completes Def_Id to be the appropriate E_*_Subtype.
215 -- The Elist is the list of discriminant constraints if any (it is set
216 -- to No_Elist if T is not a discriminated type, and to an empty list if
217 -- T has discriminants but there are no discriminant constraints). The
218 -- Related_Nod is the same as Decl_Node in Create_Constrained_Components.
219 -- The For_Access says whether or not this subtype is really constraining
220 -- an access type. That is its sole purpose is the designated type of an
221 -- access type -- in which case a Private_Subtype Is_For_Access_Subtype
222 -- is built to avoid freezing T when the access subtype is frozen.
224 function Build_Scalar_Bound
225 (Bound : Node_Id;
226 Par_T : Entity_Id;
227 Der_T : Entity_Id) return Node_Id;
228 -- The bounds of a derived scalar type are conversions of the bounds of
229 -- the parent type. Optimize the representation if the bounds are literals.
230 -- Needs a more complete spec--what are the parameters exactly, and what
231 -- exactly is the returned value, and how is Bound affected???
233 procedure Build_Underlying_Full_View
234 (N : Node_Id;
235 Typ : Entity_Id;
236 Par : Entity_Id);
237 -- If the completion of a private type is itself derived from a private
238 -- type, or if the full view of a private subtype is itself private, the
239 -- back-end has no way to compute the actual size of this type. We build
240 -- an internal subtype declaration of the proper parent type to convey
241 -- this information. This extra mechanism is needed because a full
242 -- view cannot itself have a full view (it would get clobbered during
243 -- view exchanges).
245 procedure Check_Access_Discriminant_Requires_Limited
246 (D : Node_Id;
247 Loc : Node_Id);
248 -- Check the restriction that the type to which an access discriminant
249 -- belongs must be a concurrent type or a descendant of a type with
250 -- the reserved word 'limited' in its declaration.
252 procedure Check_Anonymous_Access_Components
253 (Typ_Decl : Node_Id;
254 Typ : Entity_Id;
255 Prev : Entity_Id;
256 Comp_List : Node_Id);
257 -- Ada 2005 AI-382: an access component in a record definition can refer to
258 -- the enclosing record, in which case it denotes the type itself, and not
259 -- the current instance of the type. We create an anonymous access type for
260 -- the component, and flag it as an access to a component, so accessibility
261 -- checks are properly performed on it. The declaration of the access type
262 -- is placed ahead of that of the record to prevent order-of-elaboration
263 -- circularity issues in Gigi. We create an incomplete type for the record
264 -- declaration, which is the designated type of the anonymous access.
266 procedure Check_Delta_Expression (E : Node_Id);
267 -- Check that the expression represented by E is suitable for use as a
268 -- delta expression, i.e. it is of real type and is static.
270 procedure Check_Digits_Expression (E : Node_Id);
271 -- Check that the expression represented by E is suitable for use as a
272 -- digits expression, i.e. it is of integer type, positive and static.
274 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id);
275 -- Validate the initialization of an object declaration. T is the required
276 -- type, and Exp is the initialization expression.
278 procedure Check_Interfaces (N : Node_Id; Def : Node_Id);
279 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
281 procedure Check_Or_Process_Discriminants
282 (N : Node_Id;
283 T : Entity_Id;
284 Prev : Entity_Id := Empty);
285 -- If T is the full declaration of an incomplete or private type, check the
286 -- conformance of the discriminants, otherwise process them. Prev is the
287 -- entity of the partial declaration, if any.
289 procedure Check_Real_Bound (Bound : Node_Id);
290 -- Check given bound for being of real type and static. If not, post an
291 -- appropriate message, and rewrite the bound with the real literal zero.
293 procedure Constant_Redeclaration
294 (Id : Entity_Id;
295 N : Node_Id;
296 T : out Entity_Id);
297 -- Various checks on legality of full declaration of deferred constant.
298 -- Id is the entity for the redeclaration, N is the N_Object_Declaration,
299 -- node. The caller has not yet set any attributes of this entity.
301 function Contain_Interface
302 (Iface : Entity_Id;
303 Ifaces : Elist_Id) return Boolean;
304 -- Ada 2005: Determine whether Iface is present in the list Ifaces
306 procedure Convert_Scalar_Bounds
307 (N : Node_Id;
308 Parent_Type : Entity_Id;
309 Derived_Type : Entity_Id;
310 Loc : Source_Ptr);
311 -- For derived scalar types, convert the bounds in the type definition to
312 -- the derived type, and complete their analysis. Given a constraint of the
313 -- form ".. new T range Lo .. Hi", Lo and Hi are analyzed and resolved with
314 -- T'Base, the parent_type. The bounds of the derived type (the anonymous
315 -- base) are copies of Lo and Hi. Finally, the bounds of the derived
316 -- subtype are conversions of those bounds to the derived_type, so that
317 -- their typing is consistent.
319 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id);
320 -- Copies attributes from array base type T2 to array base type T1. Copies
321 -- only attributes that apply to base types, but not subtypes.
323 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id);
324 -- Copies attributes from array subtype T2 to array subtype T1. Copies
325 -- attributes that apply to both subtypes and base types.
327 procedure Create_Constrained_Components
328 (Subt : Entity_Id;
329 Decl_Node : Node_Id;
330 Typ : Entity_Id;
331 Constraints : Elist_Id);
332 -- Build the list of entities for a constrained discriminated record
333 -- subtype. If a component depends on a discriminant, replace its subtype
334 -- using the discriminant values in the discriminant constraint. Subt
335 -- is the defining identifier for the subtype whose list of constrained
336 -- entities we will create. Decl_Node is the type declaration node where
337 -- we will attach all the itypes created. Typ is the base discriminated
338 -- type for the subtype Subt. Constraints is the list of discriminant
339 -- constraints for Typ.
341 function Constrain_Component_Type
342 (Comp : Entity_Id;
343 Constrained_Typ : Entity_Id;
344 Related_Node : Node_Id;
345 Typ : Entity_Id;
346 Constraints : Elist_Id) return Entity_Id;
347 -- Given a discriminated base type Typ, a list of discriminant constraint
348 -- Constraints for Typ and a component of Typ, with type Compon_Type,
349 -- create and return the type corresponding to Compon_type where all
350 -- discriminant references are replaced with the corresponding constraint.
351 -- If no discriminant references occur in Compon_Typ then return it as is.
352 -- Constrained_Typ is the final constrained subtype to which the
353 -- constrained Compon_Type belongs. Related_Node is the node where we will
354 -- attach all the itypes created.
356 -- Above description is confused, what is Compon_Type???
358 procedure Constrain_Access
359 (Def_Id : in out Entity_Id;
360 S : Node_Id;
361 Related_Nod : Node_Id);
362 -- Apply a list of constraints to an access type. If Def_Id is empty, it is
363 -- an anonymous type created for a subtype indication. In that case it is
364 -- created in the procedure and attached to Related_Nod.
366 procedure Constrain_Array
367 (Def_Id : in out Entity_Id;
368 SI : Node_Id;
369 Related_Nod : Node_Id;
370 Related_Id : Entity_Id;
371 Suffix : Character);
372 -- Apply a list of index constraints to an unconstrained array type. The
373 -- first parameter is the entity for the resulting subtype. A value of
374 -- Empty for Def_Id indicates that an implicit type must be created, but
375 -- creation is delayed (and must be done by this procedure) because other
376 -- subsidiary implicit types must be created first (which is why Def_Id
377 -- is an in/out parameter). The second parameter is a subtype indication
378 -- node for the constrained array to be created (e.g. something of the
379 -- form string (1 .. 10)). Related_Nod gives the place where this type
380 -- has to be inserted in the tree. The Related_Id and Suffix parameters
381 -- are used to build the associated Implicit type name.
383 procedure Constrain_Concurrent
384 (Def_Id : in out Entity_Id;
385 SI : Node_Id;
386 Related_Nod : Node_Id;
387 Related_Id : Entity_Id;
388 Suffix : Character);
389 -- Apply list of discriminant constraints to an unconstrained concurrent
390 -- type.
392 -- SI is the N_Subtype_Indication node containing the constraint and
393 -- the unconstrained type to constrain.
395 -- Def_Id is the entity for the resulting constrained subtype. A value
396 -- of Empty for Def_Id indicates that an implicit type must be created,
397 -- but creation is delayed (and must be done by this procedure) because
398 -- other subsidiary implicit types must be created first (which is why
399 -- Def_Id is an in/out parameter).
401 -- Related_Nod gives the place where this type has to be inserted
402 -- in the tree
404 -- The last two arguments are used to create its external name if needed.
406 function Constrain_Corresponding_Record
407 (Prot_Subt : Entity_Id;
408 Corr_Rec : Entity_Id;
409 Related_Nod : Node_Id;
410 Related_Id : Entity_Id) return Entity_Id;
411 -- When constraining a protected type or task type with discriminants,
412 -- constrain the corresponding record with the same discriminant values.
414 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id);
415 -- Constrain a decimal fixed point type with a digits constraint and/or a
416 -- range constraint, and build E_Decimal_Fixed_Point_Subtype entity.
418 procedure Constrain_Discriminated_Type
419 (Def_Id : Entity_Id;
420 S : Node_Id;
421 Related_Nod : Node_Id;
422 For_Access : Boolean := False);
423 -- Process discriminant constraints of composite type. Verify that values
424 -- have been provided for all discriminants, that the original type is
425 -- unconstrained, and that the types of the supplied expressions match
426 -- the discriminant types. The first three parameters are like in routine
427 -- Constrain_Concurrent. See Build_Discriminated_Subtype for an explanation
428 -- of For_Access.
430 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id);
431 -- Constrain an enumeration type with a range constraint. This is identical
432 -- to Constrain_Integer, but for the Ekind of the resulting subtype.
434 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id);
435 -- Constrain a floating point type with either a digits constraint
436 -- and/or a range constraint, building a E_Floating_Point_Subtype.
438 procedure Constrain_Index
439 (Index : Node_Id;
440 S : Node_Id;
441 Related_Nod : Node_Id;
442 Related_Id : Entity_Id;
443 Suffix : Character;
444 Suffix_Index : Nat);
445 -- Process an index constraint in a constrained array declaration. The
446 -- constraint can be a subtype name, or a range with or without an explicit
447 -- subtype mark. The index is the corresponding index of the unconstrained
448 -- array. The Related_Id and Suffix parameters are used to build the
449 -- associated Implicit type name.
451 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id);
452 -- Build subtype of a signed or modular integer type
454 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id);
455 -- Constrain an ordinary fixed point type with a range constraint, and
456 -- build an E_Ordinary_Fixed_Point_Subtype entity.
458 procedure Copy_And_Swap (Priv, Full : Entity_Id);
459 -- Copy the Priv entity into the entity of its full declaration then swap
460 -- the two entities in such a manner that the former private type is now
461 -- seen as a full type.
463 procedure Decimal_Fixed_Point_Type_Declaration
464 (T : Entity_Id;
465 Def : Node_Id);
466 -- Create a new decimal fixed point type, and apply the constraint to
467 -- obtain a subtype of this new type.
469 procedure Complete_Private_Subtype
470 (Priv : Entity_Id;
471 Full : Entity_Id;
472 Full_Base : Entity_Id;
473 Related_Nod : Node_Id);
474 -- Complete the implicit full view of a private subtype by setting the
475 -- appropriate semantic fields. If the full view of the parent is a record
476 -- type, build constrained components of subtype.
478 procedure Derive_Progenitor_Subprograms
479 (Parent_Type : Entity_Id;
480 Tagged_Type : Entity_Id);
481 -- Ada 2005 (AI-251): To complete type derivation, collect the primitive
482 -- operations of progenitors of Tagged_Type, and replace the subsidiary
483 -- subtypes with Tagged_Type, to build the specs of the inherited interface
484 -- primitives. The derived primitives are aliased to those of the
485 -- interface. This routine takes care also of transferring to the full-view
486 -- subprograms associated with the partial-view of Tagged_Type that cover
487 -- interface primitives.
489 procedure Derived_Standard_Character
490 (N : Node_Id;
491 Parent_Type : Entity_Id;
492 Derived_Type : Entity_Id);
493 -- Subsidiary procedure to Build_Derived_Enumeration_Type which handles
494 -- derivations from types Standard.Character and Standard.Wide_Character.
496 procedure Derived_Type_Declaration
497 (T : Entity_Id;
498 N : Node_Id;
499 Is_Completion : Boolean);
500 -- Process a derived type declaration. Build_Derived_Type is invoked
501 -- to process the actual derived type definition. Parameters N and
502 -- Is_Completion have the same meaning as in Build_Derived_Type.
503 -- T is the N_Defining_Identifier for the entity defined in the
504 -- N_Full_Type_Declaration node N, that is T is the derived type.
506 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id);
507 -- Insert each literal in symbol table, as an overloadable identifier. Each
508 -- enumeration type is mapped into a sequence of integers, and each literal
509 -- is defined as a constant with integer value. If any of the literals are
510 -- character literals, the type is a character type, which means that
511 -- strings are legal aggregates for arrays of components of the type.
513 function Expand_To_Stored_Constraint
514 (Typ : Entity_Id;
515 Constraint : Elist_Id) return Elist_Id;
516 -- Given a constraint (i.e. a list of expressions) on the discriminants of
517 -- Typ, expand it into a constraint on the stored discriminants and return
518 -- the new list of expressions constraining the stored discriminants.
520 function Find_Type_Of_Object
521 (Obj_Def : Node_Id;
522 Related_Nod : Node_Id) return Entity_Id;
523 -- Get type entity for object referenced by Obj_Def, attaching the
524 -- implicit types generated to Related_Nod
526 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id);
527 -- Create a new float and apply the constraint to obtain subtype of it
529 function Has_Range_Constraint (N : Node_Id) return Boolean;
530 -- Given an N_Subtype_Indication node N, return True if a range constraint
531 -- is present, either directly, or as part of a digits or delta constraint.
532 -- In addition, a digits constraint in the decimal case returns True, since
533 -- it establishes a default range if no explicit range is present.
535 function Inherit_Components
536 (N : Node_Id;
537 Parent_Base : Entity_Id;
538 Derived_Base : Entity_Id;
539 Is_Tagged : Boolean;
540 Inherit_Discr : Boolean;
541 Discs : Elist_Id) return Elist_Id;
542 -- Called from Build_Derived_Record_Type to inherit the components of
543 -- Parent_Base (a base type) into the Derived_Base (the derived base type).
544 -- For more information on derived types and component inheritance please
545 -- consult the comment above the body of Build_Derived_Record_Type.
547 -- N is the original derived type declaration
549 -- Is_Tagged is set if we are dealing with tagged types
551 -- If Inherit_Discr is set, Derived_Base inherits its discriminants from
552 -- Parent_Base, otherwise no discriminants are inherited.
554 -- Discs gives the list of constraints that apply to Parent_Base in the
555 -- derived type declaration. If Discs is set to No_Elist, then we have
556 -- the following situation:
558 -- type Parent (D1..Dn : ..) is [tagged] record ...;
559 -- type Derived is new Parent [with ...];
561 -- which gets treated as
563 -- type Derived (D1..Dn : ..) is new Parent (D1,..,Dn) [with ...];
565 -- For untagged types the returned value is an association list. The list
566 -- starts from the association (Parent_Base => Derived_Base), and then it
567 -- contains a sequence of the associations of the form
569 -- (Old_Component => New_Component),
571 -- where Old_Component is the Entity_Id of a component in Parent_Base and
572 -- New_Component is the Entity_Id of the corresponding component in
573 -- Derived_Base. For untagged records, this association list is needed when
574 -- copying the record declaration for the derived base. In the tagged case
575 -- the value returned is irrelevant.
577 function Is_Progenitor
578 (Iface : Entity_Id;
579 Typ : Entity_Id) return Boolean;
580 -- Determine whether the interface Iface is implemented by Typ. It requires
581 -- traversing the list of abstract interfaces of the type, as well as that
582 -- of the ancestor types. The predicate is used to determine when a formal
583 -- in the signature of an inherited operation must carry the derived type.
585 function Is_Valid_Constraint_Kind
586 (T_Kind : Type_Kind;
587 Constraint_Kind : Node_Kind) return Boolean;
588 -- Returns True if it is legal to apply the given kind of constraint to the
589 -- given kind of type (index constraint to an array type, for example).
591 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id);
592 -- Create new modular type. Verify that modulus is in bounds and is
593 -- a power of two (implementation restriction).
595 procedure New_Concatenation_Op (Typ : Entity_Id);
596 -- Create an abbreviated declaration for an operator in order to
597 -- materialize concatenation on array types.
599 procedure Ordinary_Fixed_Point_Type_Declaration
600 (T : Entity_Id;
601 Def : Node_Id);
602 -- Create a new ordinary fixed point type, and apply the constraint to
603 -- obtain subtype of it.
605 procedure Prepare_Private_Subtype_Completion
606 (Id : Entity_Id;
607 Related_Nod : Node_Id);
608 -- Id is a subtype of some private type. Creates the full declaration
609 -- associated with Id whenever possible, i.e. when the full declaration
610 -- of the base type is already known. Records each subtype into
611 -- Private_Dependents of the base type.
613 procedure Process_Incomplete_Dependents
614 (N : Node_Id;
615 Full_T : Entity_Id;
616 Inc_T : Entity_Id);
617 -- Process all entities that depend on an incomplete type. There include
618 -- subtypes, subprogram types that mention the incomplete type in their
619 -- profiles, and subprogram with access parameters that designate the
620 -- incomplete type.
622 -- Inc_T is the defining identifier of an incomplete type declaration, its
623 -- Ekind is E_Incomplete_Type.
625 -- N is the corresponding N_Full_Type_Declaration for Inc_T.
627 -- Full_T is N's defining identifier.
629 -- Subtypes of incomplete types with discriminants are completed when the
630 -- parent type is. This is simpler than private subtypes, because they can
631 -- only appear in the same scope, and there is no need to exchange views.
632 -- Similarly, access_to_subprogram types may have a parameter or a return
633 -- type that is an incomplete type, and that must be replaced with the
634 -- full type.
636 -- If the full type is tagged, subprogram with access parameters that
637 -- designated the incomplete may be primitive operations of the full type,
638 -- and have to be processed accordingly.
640 procedure Process_Real_Range_Specification (Def : Node_Id);
641 -- Given the type definition for a real type, this procedure processes and
642 -- checks the real range specification of this type definition if one is
643 -- present. If errors are found, error messages are posted, and the
644 -- Real_Range_Specification of Def is reset to Empty.
646 procedure Record_Type_Declaration
647 (T : Entity_Id;
648 N : Node_Id;
649 Prev : Entity_Id);
650 -- Process a record type declaration (for both untagged and tagged
651 -- records). Parameters T and N are exactly like in procedure
652 -- Derived_Type_Declaration, except that no flag Is_Completion is needed
653 -- for this routine. If this is the completion of an incomplete type
654 -- declaration, Prev is the entity of the incomplete declaration, used for
655 -- cross-referencing. Otherwise Prev = T.
657 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id);
658 -- This routine is used to process the actual record type definition (both
659 -- for untagged and tagged records). Def is a record type definition node.
660 -- This procedure analyzes the components in this record type definition.
661 -- Prev_T is the entity for the enclosing record type. It is provided so
662 -- that its Has_Task flag can be set if any of the component have Has_Task
663 -- set. If the declaration is the completion of an incomplete type
664 -- declaration, Prev_T is the original incomplete type, whose full view is
665 -- the record type.
667 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id);
668 -- Subsidiary to Build_Derived_Record_Type. For untagged records, we
669 -- build a copy of the declaration tree of the parent, and we create
670 -- independently the list of components for the derived type. Semantic
671 -- information uses the component entities, but record representation
672 -- clauses are validated on the declaration tree. This procedure replaces
673 -- discriminants and components in the declaration with those that have
674 -- been created by Inherit_Components.
676 procedure Set_Fixed_Range
677 (E : Entity_Id;
678 Loc : Source_Ptr;
679 Lo : Ureal;
680 Hi : Ureal);
681 -- Build a range node with the given bounds and set it as the Scalar_Range
682 -- of the given fixed-point type entity. Loc is the source location used
683 -- for the constructed range. See body for further details.
685 procedure Set_Scalar_Range_For_Subtype
686 (Def_Id : Entity_Id;
687 R : Node_Id;
688 Subt : Entity_Id);
689 -- This routine is used to set the scalar range field for a subtype given
690 -- Def_Id, the entity for the subtype, and R, the range expression for the
691 -- scalar range. Subt provides the parent subtype to be used to analyze,
692 -- resolve, and check the given range.
694 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id);
695 -- Create a new signed integer entity, and apply the constraint to obtain
696 -- the required first named subtype of this type.
698 procedure Set_Stored_Constraint_From_Discriminant_Constraint
699 (E : Entity_Id);
700 -- E is some record type. This routine computes E's Stored_Constraint
701 -- from its Discriminant_Constraint.
703 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id);
704 -- Check that an entity in a list of progenitors is an interface,
705 -- emit error otherwise.
707 -----------------------
708 -- Access_Definition --
709 -----------------------
711 function Access_Definition
712 (Related_Nod : Node_Id;
713 N : Node_Id) return Entity_Id
715 Loc : constant Source_Ptr := Sloc (Related_Nod);
716 Anon_Type : Entity_Id;
717 Anon_Scope : Entity_Id;
718 Desig_Type : Entity_Id;
719 Decl : Entity_Id;
720 Enclosing_Prot_Type : Entity_Id := Empty;
722 begin
723 if Is_Entry (Current_Scope)
724 and then Is_Task_Type (Etype (Scope (Current_Scope)))
725 then
726 Error_Msg_N ("task entries cannot have access parameters", N);
727 return Empty;
728 end if;
730 -- Ada 2005: for an object declaration the corresponding anonymous
731 -- type is declared in the current scope.
733 -- If the access definition is the return type of another access to
734 -- function, scope is the current one, because it is the one of the
735 -- current type declaration.
737 if Nkind_In (Related_Nod, N_Object_Declaration,
738 N_Access_Function_Definition)
739 then
740 Anon_Scope := Current_Scope;
742 -- For the anonymous function result case, retrieve the scope of the
743 -- function specification's associated entity rather than using the
744 -- current scope. The current scope will be the function itself if the
745 -- formal part is currently being analyzed, but will be the parent scope
746 -- in the case of a parameterless function, and we always want to use
747 -- the function's parent scope. Finally, if the function is a child
748 -- unit, we must traverse the tree to retrieve the proper entity.
750 elsif Nkind (Related_Nod) = N_Function_Specification
751 and then Nkind (Parent (N)) /= N_Parameter_Specification
752 then
753 -- If the current scope is a protected type, the anonymous access
754 -- is associated with one of the protected operations, and must
755 -- be available in the scope that encloses the protected declaration.
756 -- Otherwise the type is in the scope enclosing the subprogram.
758 -- If the function has formals, The return type of a subprogram
759 -- declaration is analyzed in the scope of the subprogram (see
760 -- Process_Formals) and thus the protected type, if present, is
761 -- the scope of the current function scope.
763 if Ekind (Current_Scope) = E_Protected_Type then
764 Enclosing_Prot_Type := Current_Scope;
766 elsif Ekind (Current_Scope) = E_Function
767 and then Ekind (Scope (Current_Scope)) = E_Protected_Type
768 then
769 Enclosing_Prot_Type := Scope (Current_Scope);
770 end if;
772 if Present (Enclosing_Prot_Type) then
773 Anon_Scope := Scope (Enclosing_Prot_Type);
775 else
776 Anon_Scope := Scope (Defining_Entity (Related_Nod));
777 end if;
779 else
780 -- For access formals, access components, and access discriminants,
781 -- the scope is that of the enclosing declaration,
783 Anon_Scope := Scope (Current_Scope);
784 end if;
786 Anon_Type :=
787 Create_Itype
788 (E_Anonymous_Access_Type, Related_Nod, Scope_Id => Anon_Scope);
790 if All_Present (N)
791 and then Ada_Version >= Ada_05
792 then
793 Error_Msg_N ("ALL is not permitted for anonymous access types", N);
794 end if;
796 -- Ada 2005 (AI-254): In case of anonymous access to subprograms call
797 -- the corresponding semantic routine
799 if Present (Access_To_Subprogram_Definition (N)) then
800 Access_Subprogram_Declaration
801 (T_Name => Anon_Type,
802 T_Def => Access_To_Subprogram_Definition (N));
804 if Ekind (Anon_Type) = E_Access_Protected_Subprogram_Type then
805 Set_Ekind
806 (Anon_Type, E_Anonymous_Access_Protected_Subprogram_Type);
807 else
808 Set_Ekind
809 (Anon_Type, E_Anonymous_Access_Subprogram_Type);
810 end if;
812 Set_Can_Use_Internal_Rep
813 (Anon_Type, not Always_Compatible_Rep_On_Target);
815 -- If the anonymous access is associated with a protected operation
816 -- create a reference to it after the enclosing protected definition
817 -- because the itype will be used in the subsequent bodies.
819 if Ekind (Current_Scope) = E_Protected_Type then
820 Build_Itype_Reference (Anon_Type, Parent (Current_Scope));
821 end if;
823 return Anon_Type;
824 end if;
826 Find_Type (Subtype_Mark (N));
827 Desig_Type := Entity (Subtype_Mark (N));
829 Set_Directly_Designated_Type (Anon_Type, Desig_Type);
830 Set_Etype (Anon_Type, Anon_Type);
832 -- Make sure the anonymous access type has size and alignment fields
833 -- set, as required by gigi. This is necessary in the case of the
834 -- Task_Body_Procedure.
836 if not Has_Private_Component (Desig_Type) then
837 Layout_Type (Anon_Type);
838 end if;
840 -- ???The following makes no sense, because Anon_Type is an access type
841 -- and therefore cannot have components, private or otherwise. Hence
842 -- the assertion. Not sure what was meant, here.
843 Set_Depends_On_Private (Anon_Type, Has_Private_Component (Anon_Type));
844 pragma Assert (not Depends_On_Private (Anon_Type));
846 -- Ada 2005 (AI-231): Ada 2005 semantics for anonymous access differs
847 -- from Ada 95 semantics. In Ada 2005, anonymous access must specify if
848 -- the null value is allowed. In Ada 95 the null value is never allowed.
850 if Ada_Version >= Ada_05 then
851 Set_Can_Never_Be_Null (Anon_Type, Null_Exclusion_Present (N));
852 else
853 Set_Can_Never_Be_Null (Anon_Type, True);
854 end if;
856 -- The anonymous access type is as public as the discriminated type or
857 -- subprogram that defines it. It is imported (for back-end purposes)
858 -- if the designated type is.
860 Set_Is_Public (Anon_Type, Is_Public (Scope (Anon_Type)));
862 -- Ada 2005 (AI-231): Propagate the access-constant attribute
864 Set_Is_Access_Constant (Anon_Type, Constant_Present (N));
866 -- The context is either a subprogram declaration, object declaration,
867 -- or an access discriminant, in a private or a full type declaration.
868 -- In the case of a subprogram, if the designated type is incomplete,
869 -- the operation will be a primitive operation of the full type, to be
870 -- updated subsequently. If the type is imported through a limited_with
871 -- clause, the subprogram is not a primitive operation of the type
872 -- (which is declared elsewhere in some other scope).
874 if Ekind (Desig_Type) = E_Incomplete_Type
875 and then not From_With_Type (Desig_Type)
876 and then Is_Overloadable (Current_Scope)
877 then
878 Append_Elmt (Current_Scope, Private_Dependents (Desig_Type));
879 Set_Has_Delayed_Freeze (Current_Scope);
880 end if;
882 -- Ada 2005: if the designated type is an interface that may contain
883 -- tasks, create a Master entity for the declaration. This must be done
884 -- before expansion of the full declaration, because the declaration may
885 -- include an expression that is an allocator, whose expansion needs the
886 -- proper Master for the created tasks.
888 if Nkind (Related_Nod) = N_Object_Declaration
889 and then Expander_Active
890 then
891 if Is_Interface (Desig_Type)
892 and then Is_Limited_Record (Desig_Type)
893 then
894 Build_Class_Wide_Master (Anon_Type);
896 -- Similarly, if the type is an anonymous access that designates
897 -- tasks, create a master entity for it in the current context.
899 elsif Has_Task (Desig_Type)
900 and then Comes_From_Source (Related_Nod)
901 then
902 if not Has_Master_Entity (Current_Scope) then
903 Decl :=
904 Make_Object_Declaration (Loc,
905 Defining_Identifier =>
906 Make_Defining_Identifier (Loc, Name_uMaster),
907 Constant_Present => True,
908 Object_Definition =>
909 New_Reference_To (RTE (RE_Master_Id), Loc),
910 Expression =>
911 Make_Explicit_Dereference (Loc,
912 New_Reference_To (RTE (RE_Current_Master), Loc)));
914 Insert_Before (Related_Nod, Decl);
915 Analyze (Decl);
917 Set_Master_Id (Anon_Type, Defining_Identifier (Decl));
918 Set_Has_Master_Entity (Current_Scope);
919 else
920 Build_Master_Renaming (Related_Nod, Anon_Type);
921 end if;
922 end if;
923 end if;
925 -- For a private component of a protected type, it is imperative that
926 -- the back-end elaborate the type immediately after the protected
927 -- declaration, because this type will be used in the declarations
928 -- created for the component within each protected body, so we must
929 -- create an itype reference for it now.
931 if Nkind (Parent (Related_Nod)) = N_Protected_Definition then
932 Build_Itype_Reference (Anon_Type, Parent (Parent (Related_Nod)));
934 -- Similarly, if the access definition is the return result of a
935 -- function, create an itype reference for it because it will be used
936 -- within the function body. For a regular function that is not a
937 -- compilation unit, insert reference after the declaration. For a
938 -- protected operation, insert it after the enclosing protected type
939 -- declaration. In either case, do not create a reference for a type
940 -- obtained through a limited_with clause, because this would introduce
941 -- semantic dependencies.
943 -- Similarly, do not create a reference if the designated type is a
944 -- generic formal, because no use of it will reach the backend.
946 elsif Nkind (Related_Nod) = N_Function_Specification
947 and then not From_With_Type (Desig_Type)
948 and then not Is_Generic_Type (Desig_Type)
949 then
950 if Present (Enclosing_Prot_Type) then
951 Build_Itype_Reference (Anon_Type, Parent (Enclosing_Prot_Type));
953 elsif Is_List_Member (Parent (Related_Nod))
954 and then Nkind (Parent (N)) /= N_Parameter_Specification
955 then
956 Build_Itype_Reference (Anon_Type, Parent (Related_Nod));
957 end if;
959 -- Finally, create an itype reference for an object declaration of an
960 -- anonymous access type. This is strictly necessary only for deferred
961 -- constants, but in any case will avoid out-of-scope problems in the
962 -- back-end.
964 elsif Nkind (Related_Nod) = N_Object_Declaration then
965 Build_Itype_Reference (Anon_Type, Related_Nod);
966 end if;
968 return Anon_Type;
969 end Access_Definition;
971 -----------------------------------
972 -- Access_Subprogram_Declaration --
973 -----------------------------------
975 procedure Access_Subprogram_Declaration
976 (T_Name : Entity_Id;
977 T_Def : Node_Id)
980 procedure Check_For_Premature_Usage (Def : Node_Id);
981 -- Check that type T_Name is not used, directly or recursively, as a
982 -- parameter or a return type in Def. Def is either a subtype, an
983 -- access_definition, or an access_to_subprogram_definition.
985 -------------------------------
986 -- Check_For_Premature_Usage --
987 -------------------------------
989 procedure Check_For_Premature_Usage (Def : Node_Id) is
990 Param : Node_Id;
992 begin
993 -- Check for a subtype mark
995 if Nkind (Def) in N_Has_Etype then
996 if Etype (Def) = T_Name then
997 Error_Msg_N
998 ("type& cannot be used before end of its declaration", Def);
999 end if;
1001 -- If this is not a subtype, then this is an access_definition
1003 elsif Nkind (Def) = N_Access_Definition then
1004 if Present (Access_To_Subprogram_Definition (Def)) then
1005 Check_For_Premature_Usage
1006 (Access_To_Subprogram_Definition (Def));
1007 else
1008 Check_For_Premature_Usage (Subtype_Mark (Def));
1009 end if;
1011 -- The only cases left are N_Access_Function_Definition and
1012 -- N_Access_Procedure_Definition.
1014 else
1015 if Present (Parameter_Specifications (Def)) then
1016 Param := First (Parameter_Specifications (Def));
1017 while Present (Param) loop
1018 Check_For_Premature_Usage (Parameter_Type (Param));
1019 Param := Next (Param);
1020 end loop;
1021 end if;
1023 if Nkind (Def) = N_Access_Function_Definition then
1024 Check_For_Premature_Usage (Result_Definition (Def));
1025 end if;
1026 end if;
1027 end Check_For_Premature_Usage;
1029 -- Local variables
1031 Formals : constant List_Id := Parameter_Specifications (T_Def);
1032 Formal : Entity_Id;
1033 D_Ityp : Node_Id;
1034 Desig_Type : constant Entity_Id :=
1035 Create_Itype (E_Subprogram_Type, Parent (T_Def));
1037 -- Start of processing for Access_Subprogram_Declaration
1039 begin
1040 -- Associate the Itype node with the inner full-type declaration or
1041 -- subprogram spec or entry body. This is required to handle nested
1042 -- anonymous declarations. For example:
1044 -- procedure P
1045 -- (X : access procedure
1046 -- (Y : access procedure
1047 -- (Z : access T)))
1049 D_Ityp := Associated_Node_For_Itype (Desig_Type);
1050 while not (Nkind_In (D_Ityp, N_Full_Type_Declaration,
1051 N_Private_Type_Declaration,
1052 N_Private_Extension_Declaration,
1053 N_Procedure_Specification,
1054 N_Function_Specification,
1055 N_Entry_Body)
1057 or else
1058 Nkind_In (D_Ityp, N_Object_Declaration,
1059 N_Object_Renaming_Declaration,
1060 N_Formal_Object_Declaration,
1061 N_Formal_Type_Declaration,
1062 N_Task_Type_Declaration,
1063 N_Protected_Type_Declaration))
1064 loop
1065 D_Ityp := Parent (D_Ityp);
1066 pragma Assert (D_Ityp /= Empty);
1067 end loop;
1069 Set_Associated_Node_For_Itype (Desig_Type, D_Ityp);
1071 if Nkind_In (D_Ityp, N_Procedure_Specification,
1072 N_Function_Specification)
1073 then
1074 Set_Scope (Desig_Type, Scope (Defining_Entity (D_Ityp)));
1076 elsif Nkind_In (D_Ityp, N_Full_Type_Declaration,
1077 N_Object_Declaration,
1078 N_Object_Renaming_Declaration,
1079 N_Formal_Type_Declaration)
1080 then
1081 Set_Scope (Desig_Type, Scope (Defining_Identifier (D_Ityp)));
1082 end if;
1084 if Nkind (T_Def) = N_Access_Function_Definition then
1085 if Nkind (Result_Definition (T_Def)) = N_Access_Definition then
1086 declare
1087 Acc : constant Node_Id := Result_Definition (T_Def);
1089 begin
1090 if Present (Access_To_Subprogram_Definition (Acc))
1091 and then
1092 Protected_Present (Access_To_Subprogram_Definition (Acc))
1093 then
1094 Set_Etype
1095 (Desig_Type,
1096 Replace_Anonymous_Access_To_Protected_Subprogram
1097 (T_Def));
1099 else
1100 Set_Etype
1101 (Desig_Type,
1102 Access_Definition (T_Def, Result_Definition (T_Def)));
1103 end if;
1104 end;
1106 else
1107 Analyze (Result_Definition (T_Def));
1109 declare
1110 Typ : constant Entity_Id := Entity (Result_Definition (T_Def));
1112 begin
1113 -- If a null exclusion is imposed on the result type, then
1114 -- create a null-excluding itype (an access subtype) and use
1115 -- it as the function's Etype.
1117 if Is_Access_Type (Typ)
1118 and then Null_Exclusion_In_Return_Present (T_Def)
1119 then
1120 Set_Etype (Desig_Type,
1121 Create_Null_Excluding_Itype
1122 (T => Typ,
1123 Related_Nod => T_Def,
1124 Scope_Id => Current_Scope));
1126 else
1127 if From_With_Type (Typ) then
1128 Error_Msg_NE
1129 ("illegal use of incomplete type&",
1130 Result_Definition (T_Def), Typ);
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 Def :=
1365 Make_Component_Definition (Loc,
1366 Aliased_Present => True,
1367 Subtype_Indication =>
1368 New_Occurrence_Of (RTE (RE_Interface_Tag), Loc));
1370 Tag := Make_Temporary (Loc, 'V');
1372 Decl :=
1373 Make_Component_Declaration (Loc,
1374 Defining_Identifier => Tag,
1375 Component_Definition => Def);
1377 Analyze_Component_Declaration (Decl);
1379 Set_Analyzed (Decl);
1380 Set_Ekind (Tag, E_Component);
1381 Set_Is_Tag (Tag);
1382 Set_Is_Aliased (Tag);
1383 Set_Related_Type (Tag, Iface);
1384 Init_Component_Location (Tag);
1386 pragma Assert (Is_Frozen (Iface));
1388 Set_DT_Entry_Count (Tag,
1389 DT_Entry_Count (First_Entity (Iface)));
1391 if No (Last_Tag) then
1392 Prepend (Decl, L);
1393 else
1394 Insert_After (Last_Tag, Decl);
1395 end if;
1397 Last_Tag := Decl;
1399 -- If the ancestor has discriminants we need to give special support
1400 -- to store the offset_to_top value of the secondary dispatch tables.
1401 -- For this purpose we add a supplementary component just after the
1402 -- field that contains the tag associated with each secondary DT.
1404 if Typ /= Etype (Typ)
1405 and then Has_Discriminants (Etype (Typ))
1406 then
1407 Def :=
1408 Make_Component_Definition (Loc,
1409 Subtype_Indication =>
1410 New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
1412 Offset := Make_Temporary (Loc, 'V');
1414 Decl :=
1415 Make_Component_Declaration (Loc,
1416 Defining_Identifier => Offset,
1417 Component_Definition => Def);
1419 Analyze_Component_Declaration (Decl);
1421 Set_Analyzed (Decl);
1422 Set_Ekind (Offset, E_Component);
1423 Set_Is_Aliased (Offset);
1424 Set_Related_Type (Offset, Iface);
1425 Init_Component_Location (Offset);
1426 Insert_After (Last_Tag, Decl);
1427 Last_Tag := Decl;
1428 end if;
1429 end Add_Tag;
1431 -- Local variables
1433 Elmt : Elmt_Id;
1434 Ext : Node_Id;
1435 Comp : Node_Id;
1437 -- Start of processing for Add_Interface_Tag_Components
1439 begin
1440 if not RTE_Available (RE_Interface_Tag) then
1441 Error_Msg
1442 ("(Ada 2005) interface types not supported by this run-time!",
1443 Sloc (N));
1444 return;
1445 end if;
1447 if Ekind (Typ) /= E_Record_Type
1448 or else (Is_Concurrent_Record_Type (Typ)
1449 and then Is_Empty_List (Abstract_Interface_List (Typ)))
1450 or else (not Is_Concurrent_Record_Type (Typ)
1451 and then No (Interfaces (Typ))
1452 and then Is_Empty_Elmt_List (Interfaces (Typ)))
1453 then
1454 return;
1455 end if;
1457 -- Find the current last tag
1459 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1460 Ext := Record_Extension_Part (Type_Definition (N));
1461 else
1462 pragma Assert (Nkind (Type_Definition (N)) = N_Record_Definition);
1463 Ext := Type_Definition (N);
1464 end if;
1466 Last_Tag := Empty;
1468 if not (Present (Component_List (Ext))) then
1469 Set_Null_Present (Ext, False);
1470 L := New_List;
1471 Set_Component_List (Ext,
1472 Make_Component_List (Loc,
1473 Component_Items => L,
1474 Null_Present => False));
1475 else
1476 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1477 L := Component_Items
1478 (Component_List
1479 (Record_Extension_Part
1480 (Type_Definition (N))));
1481 else
1482 L := Component_Items
1483 (Component_List
1484 (Type_Definition (N)));
1485 end if;
1487 -- Find the last tag component
1489 Comp := First (L);
1490 while Present (Comp) loop
1491 if Nkind (Comp) = N_Component_Declaration
1492 and then Is_Tag (Defining_Identifier (Comp))
1493 then
1494 Last_Tag := Comp;
1495 end if;
1497 Next (Comp);
1498 end loop;
1499 end if;
1501 -- At this point L references the list of components and Last_Tag
1502 -- references the current last tag (if any). Now we add the tag
1503 -- corresponding with all the interfaces that are not implemented
1504 -- by the parent.
1506 if Present (Interfaces (Typ)) then
1507 Elmt := First_Elmt (Interfaces (Typ));
1508 while Present (Elmt) loop
1509 Add_Tag (Node (Elmt));
1510 Next_Elmt (Elmt);
1511 end loop;
1512 end if;
1513 end Add_Interface_Tag_Components;
1515 -------------------------------------
1516 -- Add_Internal_Interface_Entities --
1517 -------------------------------------
1519 procedure Add_Internal_Interface_Entities (Tagged_Type : Entity_Id) is
1520 Elmt : Elmt_Id;
1521 Iface : Entity_Id;
1522 Iface_Elmt : Elmt_Id;
1523 Iface_Prim : Entity_Id;
1524 Ifaces_List : Elist_Id;
1525 New_Subp : Entity_Id := Empty;
1526 Prim : Entity_Id;
1527 Restore_Scope : Boolean := False;
1529 begin
1530 pragma Assert (Ada_Version >= Ada_05
1531 and then Is_Record_Type (Tagged_Type)
1532 and then Is_Tagged_Type (Tagged_Type)
1533 and then Has_Interfaces (Tagged_Type)
1534 and then not Is_Interface (Tagged_Type));
1536 -- Ensure that the internal entities are added to the scope of the type
1538 if Scope (Tagged_Type) /= Current_Scope then
1539 Push_Scope (Scope (Tagged_Type));
1540 Restore_Scope := True;
1541 end if;
1543 Collect_Interfaces (Tagged_Type, Ifaces_List);
1545 Iface_Elmt := First_Elmt (Ifaces_List);
1546 while Present (Iface_Elmt) loop
1547 Iface := Node (Iface_Elmt);
1549 -- Originally we excluded here from this processing interfaces that
1550 -- are parents of Tagged_Type because their primitives are located
1551 -- in the primary dispatch table (and hence no auxiliary internal
1552 -- entities are required to handle secondary dispatch tables in such
1553 -- case). However, these auxiliary entities are also required to
1554 -- handle derivations of interfaces in formals of generics (see
1555 -- Derive_Subprograms).
1557 Elmt := First_Elmt (Primitive_Operations (Iface));
1558 while Present (Elmt) loop
1559 Iface_Prim := Node (Elmt);
1561 if not Is_Predefined_Dispatching_Operation (Iface_Prim) then
1562 Prim :=
1563 Find_Primitive_Covering_Interface
1564 (Tagged_Type => Tagged_Type,
1565 Iface_Prim => Iface_Prim);
1567 -- Handle cases where the type has no primitive covering this
1568 -- interface primitive.
1570 if No (Prim) then
1572 -- if the tagged type is defined at library level then we
1573 -- invoke Check_Abstract_Overriding to report the error
1574 -- and thus avoid generating the dispatch tables.
1576 if Is_Library_Level_Tagged_Type (Tagged_Type) then
1577 Check_Abstract_Overriding (Tagged_Type);
1578 pragma Assert (Serious_Errors_Detected > 0);
1579 return;
1581 -- For tagged types defined in nested scopes it is still
1582 -- possible to cover this interface primitive by means of
1583 -- late overriding (see Override_Dispatching_Operation).
1585 -- Search in the list of primitives of the type for the
1586 -- entity that will be overridden in such case to reference
1587 -- it in the internal entity that we build here. If the
1588 -- primitive is not overridden then the error will be
1589 -- reported later as part of the analysis of entities
1590 -- defined in the enclosing scope.
1592 else
1593 declare
1594 El : Elmt_Id;
1596 begin
1597 El := First_Elmt (Primitive_Operations (Tagged_Type));
1598 while Present (El)
1599 and then Alias (Node (El)) /= Iface_Prim
1600 loop
1601 Next_Elmt (El);
1602 end loop;
1604 pragma Assert (Present (El));
1605 Prim := Node (El);
1606 end;
1607 end if;
1608 end if;
1610 Derive_Subprogram
1611 (New_Subp => New_Subp,
1612 Parent_Subp => Iface_Prim,
1613 Derived_Type => Tagged_Type,
1614 Parent_Type => Iface);
1616 -- Ada 2005 (AI-251): Decorate internal entity Iface_Subp
1617 -- associated with interface types. These entities are
1618 -- only registered in the list of primitives of its
1619 -- corresponding tagged type because they are only used
1620 -- to fill the contents of the secondary dispatch tables.
1621 -- Therefore they are removed from the homonym chains.
1623 Set_Is_Hidden (New_Subp);
1624 Set_Is_Internal (New_Subp);
1625 Set_Alias (New_Subp, Prim);
1626 Set_Is_Abstract_Subprogram
1627 (New_Subp, Is_Abstract_Subprogram (Prim));
1628 Set_Interface_Alias (New_Subp, Iface_Prim);
1630 -- Internal entities associated with interface types are
1631 -- only registered in the list of primitives of the tagged
1632 -- type. They are only used to fill the contents of the
1633 -- secondary dispatch tables. Therefore they are not needed
1634 -- in the homonym chains.
1636 Remove_Homonym (New_Subp);
1638 -- Hidden entities associated with interfaces must have set
1639 -- the Has_Delay_Freeze attribute to ensure that, in case of
1640 -- locally defined tagged types (or compiling with static
1641 -- dispatch tables generation disabled) the corresponding
1642 -- entry of the secondary dispatch table is filled when
1643 -- such an entity is frozen.
1645 Set_Has_Delayed_Freeze (New_Subp);
1646 end if;
1648 Next_Elmt (Elmt);
1649 end loop;
1651 Next_Elmt (Iface_Elmt);
1652 end loop;
1654 if Restore_Scope then
1655 Pop_Scope;
1656 end if;
1657 end Add_Internal_Interface_Entities;
1659 -----------------------------------
1660 -- Analyze_Component_Declaration --
1661 -----------------------------------
1663 procedure Analyze_Component_Declaration (N : Node_Id) is
1664 Id : constant Entity_Id := Defining_Identifier (N);
1665 E : constant Node_Id := Expression (N);
1666 T : Entity_Id;
1667 P : Entity_Id;
1669 function Contains_POC (Constr : Node_Id) return Boolean;
1670 -- Determines whether a constraint uses the discriminant of a record
1671 -- type thus becoming a per-object constraint (POC).
1673 function Is_Known_Limited (Typ : Entity_Id) return Boolean;
1674 -- Typ is the type of the current component, check whether this type is
1675 -- a limited type. Used to validate declaration against that of
1676 -- enclosing record.
1678 ------------------
1679 -- Contains_POC --
1680 ------------------
1682 function Contains_POC (Constr : Node_Id) return Boolean is
1683 begin
1684 -- Prevent cascaded errors
1686 if Error_Posted (Constr) then
1687 return False;
1688 end if;
1690 case Nkind (Constr) is
1691 when N_Attribute_Reference =>
1692 return
1693 Attribute_Name (Constr) = Name_Access
1694 and then Prefix (Constr) = Scope (Entity (Prefix (Constr)));
1696 when N_Discriminant_Association =>
1697 return Denotes_Discriminant (Expression (Constr));
1699 when N_Identifier =>
1700 return Denotes_Discriminant (Constr);
1702 when N_Index_Or_Discriminant_Constraint =>
1703 declare
1704 IDC : Node_Id;
1706 begin
1707 IDC := First (Constraints (Constr));
1708 while Present (IDC) loop
1710 -- One per-object constraint is sufficient
1712 if Contains_POC (IDC) then
1713 return True;
1714 end if;
1716 Next (IDC);
1717 end loop;
1719 return False;
1720 end;
1722 when N_Range =>
1723 return Denotes_Discriminant (Low_Bound (Constr))
1724 or else
1725 Denotes_Discriminant (High_Bound (Constr));
1727 when N_Range_Constraint =>
1728 return Denotes_Discriminant (Range_Expression (Constr));
1730 when others =>
1731 return False;
1733 end case;
1734 end Contains_POC;
1736 ----------------------
1737 -- Is_Known_Limited --
1738 ----------------------
1740 function Is_Known_Limited (Typ : Entity_Id) return Boolean is
1741 P : constant Entity_Id := Etype (Typ);
1742 R : constant Entity_Id := Root_Type (Typ);
1744 begin
1745 if Is_Limited_Record (Typ) then
1746 return True;
1748 -- If the root type is limited (and not a limited interface)
1749 -- so is the current type
1751 elsif Is_Limited_Record (R)
1752 and then
1753 (not Is_Interface (R)
1754 or else not Is_Limited_Interface (R))
1755 then
1756 return True;
1758 -- Else the type may have a limited interface progenitor, but a
1759 -- limited record parent.
1761 elsif R /= P
1762 and then Is_Limited_Record (P)
1763 then
1764 return True;
1766 else
1767 return False;
1768 end if;
1769 end Is_Known_Limited;
1771 -- Start of processing for Analyze_Component_Declaration
1773 begin
1774 Generate_Definition (Id);
1775 Enter_Name (Id);
1777 if Present (Subtype_Indication (Component_Definition (N))) then
1778 T := Find_Type_Of_Object
1779 (Subtype_Indication (Component_Definition (N)), N);
1781 -- Ada 2005 (AI-230): Access Definition case
1783 else
1784 pragma Assert (Present
1785 (Access_Definition (Component_Definition (N))));
1787 T := Access_Definition
1788 (Related_Nod => N,
1789 N => Access_Definition (Component_Definition (N)));
1790 Set_Is_Local_Anonymous_Access (T);
1792 -- Ada 2005 (AI-254)
1794 if Present (Access_To_Subprogram_Definition
1795 (Access_Definition (Component_Definition (N))))
1796 and then Protected_Present (Access_To_Subprogram_Definition
1797 (Access_Definition
1798 (Component_Definition (N))))
1799 then
1800 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1801 end if;
1802 end if;
1804 -- If the subtype is a constrained subtype of the enclosing record,
1805 -- (which must have a partial view) the back-end does not properly
1806 -- handle the recursion. Rewrite the component declaration with an
1807 -- explicit subtype indication, which is acceptable to Gigi. We can copy
1808 -- the tree directly because side effects have already been removed from
1809 -- discriminant constraints.
1811 if Ekind (T) = E_Access_Subtype
1812 and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
1813 and then Comes_From_Source (T)
1814 and then Nkind (Parent (T)) = N_Subtype_Declaration
1815 and then Etype (Directly_Designated_Type (T)) = Current_Scope
1816 then
1817 Rewrite
1818 (Subtype_Indication (Component_Definition (N)),
1819 New_Copy_Tree (Subtype_Indication (Parent (T))));
1820 T := Find_Type_Of_Object
1821 (Subtype_Indication (Component_Definition (N)), N);
1822 end if;
1824 -- If the component declaration includes a default expression, then we
1825 -- check that the component is not of a limited type (RM 3.7(5)),
1826 -- and do the special preanalysis of the expression (see section on
1827 -- "Handling of Default and Per-Object Expressions" in the spec of
1828 -- package Sem).
1830 if Present (E) then
1831 Preanalyze_Spec_Expression (E, T);
1832 Check_Initialization (T, E);
1834 if Ada_Version >= Ada_05
1835 and then Ekind (T) = E_Anonymous_Access_Type
1836 and then Etype (E) /= Any_Type
1837 then
1838 -- Check RM 3.9.2(9): "if the expected type for an expression is
1839 -- an anonymous access-to-specific tagged type, then the object
1840 -- designated by the expression shall not be dynamically tagged
1841 -- unless it is a controlling operand in a call on a dispatching
1842 -- operation"
1844 if Is_Tagged_Type (Directly_Designated_Type (T))
1845 and then
1846 Ekind (Directly_Designated_Type (T)) /= E_Class_Wide_Type
1847 and then
1848 Ekind (Directly_Designated_Type (Etype (E))) =
1849 E_Class_Wide_Type
1850 then
1851 Error_Msg_N
1852 ("access to specific tagged type required (RM 3.9.2(9))", E);
1853 end if;
1855 -- (Ada 2005: AI-230): Accessibility check for anonymous
1856 -- components
1858 if Type_Access_Level (Etype (E)) > Type_Access_Level (T) then
1859 Error_Msg_N
1860 ("expression has deeper access level than component " &
1861 "(RM 3.10.2 (12.2))", E);
1862 end if;
1864 -- The initialization expression is a reference to an access
1865 -- discriminant. The type of the discriminant is always deeper
1866 -- than any access type.
1868 if Ekind (Etype (E)) = E_Anonymous_Access_Type
1869 and then Is_Entity_Name (E)
1870 and then Ekind (Entity (E)) = E_In_Parameter
1871 and then Present (Discriminal_Link (Entity (E)))
1872 then
1873 Error_Msg_N
1874 ("discriminant has deeper accessibility level than target",
1876 end if;
1877 end if;
1878 end if;
1880 -- The parent type may be a private view with unknown discriminants,
1881 -- and thus unconstrained. Regular components must be constrained.
1883 if Is_Indefinite_Subtype (T) and then Chars (Id) /= Name_uParent then
1884 if Is_Class_Wide_Type (T) then
1885 Error_Msg_N
1886 ("class-wide subtype with unknown discriminants" &
1887 " in component declaration",
1888 Subtype_Indication (Component_Definition (N)));
1889 else
1890 Error_Msg_N
1891 ("unconstrained subtype in component declaration",
1892 Subtype_Indication (Component_Definition (N)));
1893 end if;
1895 -- Components cannot be abstract, except for the special case of
1896 -- the _Parent field (case of extending an abstract tagged type)
1898 elsif Is_Abstract_Type (T) and then Chars (Id) /= Name_uParent then
1899 Error_Msg_N ("type of a component cannot be abstract", N);
1900 end if;
1902 Set_Etype (Id, T);
1903 Set_Is_Aliased (Id, Aliased_Present (Component_Definition (N)));
1905 -- The component declaration may have a per-object constraint, set
1906 -- the appropriate flag in the defining identifier of the subtype.
1908 if Present (Subtype_Indication (Component_Definition (N))) then
1909 declare
1910 Sindic : constant Node_Id :=
1911 Subtype_Indication (Component_Definition (N));
1912 begin
1913 if Nkind (Sindic) = N_Subtype_Indication
1914 and then Present (Constraint (Sindic))
1915 and then Contains_POC (Constraint (Sindic))
1916 then
1917 Set_Has_Per_Object_Constraint (Id);
1918 end if;
1919 end;
1920 end if;
1922 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
1923 -- out some static checks.
1925 if Ada_Version >= Ada_05
1926 and then Can_Never_Be_Null (T)
1927 then
1928 Null_Exclusion_Static_Checks (N);
1929 end if;
1931 -- If this component is private (or depends on a private type), flag the
1932 -- record type to indicate that some operations are not available.
1934 P := Private_Component (T);
1936 if Present (P) then
1938 -- Check for circular definitions
1940 if P = Any_Type then
1941 Set_Etype (Id, Any_Type);
1943 -- There is a gap in the visibility of operations only if the
1944 -- component type is not defined in the scope of the record type.
1946 elsif Scope (P) = Scope (Current_Scope) then
1947 null;
1949 elsif Is_Limited_Type (P) then
1950 Set_Is_Limited_Composite (Current_Scope);
1952 else
1953 Set_Is_Private_Composite (Current_Scope);
1954 end if;
1955 end if;
1957 if P /= Any_Type
1958 and then Is_Limited_Type (T)
1959 and then Chars (Id) /= Name_uParent
1960 and then Is_Tagged_Type (Current_Scope)
1961 then
1962 if Is_Derived_Type (Current_Scope)
1963 and then not Is_Known_Limited (Current_Scope)
1964 then
1965 Error_Msg_N
1966 ("extension of nonlimited type cannot have limited components",
1969 if Is_Interface (Root_Type (Current_Scope)) then
1970 Error_Msg_N
1971 ("\limitedness is not inherited from limited interface", N);
1972 Error_Msg_N ("\add LIMITED to type indication", N);
1973 end if;
1975 Explain_Limited_Type (T, N);
1976 Set_Etype (Id, Any_Type);
1977 Set_Is_Limited_Composite (Current_Scope, False);
1979 elsif not Is_Derived_Type (Current_Scope)
1980 and then not Is_Limited_Record (Current_Scope)
1981 and then not Is_Concurrent_Type (Current_Scope)
1982 then
1983 Error_Msg_N
1984 ("nonlimited tagged type cannot have limited components", N);
1985 Explain_Limited_Type (T, N);
1986 Set_Etype (Id, Any_Type);
1987 Set_Is_Limited_Composite (Current_Scope, False);
1988 end if;
1989 end if;
1991 Set_Original_Record_Component (Id, Id);
1992 end Analyze_Component_Declaration;
1994 --------------------------
1995 -- Analyze_Declarations --
1996 --------------------------
1998 procedure Analyze_Declarations (L : List_Id) is
1999 D : Node_Id;
2000 Freeze_From : Entity_Id := Empty;
2001 Next_Node : Node_Id;
2003 procedure Adjust_D;
2004 -- Adjust D not to include implicit label declarations, since these
2005 -- have strange Sloc values that result in elaboration check problems.
2006 -- (They have the sloc of the label as found in the source, and that
2007 -- is ahead of the current declarative part).
2009 --------------
2010 -- Adjust_D --
2011 --------------
2013 procedure Adjust_D is
2014 begin
2015 while Present (Prev (D))
2016 and then Nkind (D) = N_Implicit_Label_Declaration
2017 loop
2018 Prev (D);
2019 end loop;
2020 end Adjust_D;
2022 -- Start of processing for Analyze_Declarations
2024 begin
2025 D := First (L);
2026 while Present (D) loop
2028 -- Complete analysis of declaration
2030 Analyze (D);
2031 Next_Node := Next (D);
2033 if No (Freeze_From) then
2034 Freeze_From := First_Entity (Current_Scope);
2035 end if;
2037 -- At the end of a declarative part, freeze remaining entities
2038 -- declared in it. The end of the visible declarations of package
2039 -- specification is not the end of a declarative part if private
2040 -- declarations are present. The end of a package declaration is a
2041 -- freezing point only if it a library package. A task definition or
2042 -- protected type definition is not a freeze point either. Finally,
2043 -- we do not freeze entities in generic scopes, because there is no
2044 -- code generated for them and freeze nodes will be generated for
2045 -- the instance.
2047 -- The end of a package instantiation is not a freeze point, but
2048 -- for now we make it one, because the generic body is inserted
2049 -- (currently) immediately after. Generic instantiations will not
2050 -- be a freeze point once delayed freezing of bodies is implemented.
2051 -- (This is needed in any case for early instantiations ???).
2053 if No (Next_Node) then
2054 if Nkind_In (Parent (L), N_Component_List,
2055 N_Task_Definition,
2056 N_Protected_Definition)
2057 then
2058 null;
2060 elsif Nkind (Parent (L)) /= N_Package_Specification then
2061 if Nkind (Parent (L)) = N_Package_Body then
2062 Freeze_From := First_Entity (Current_Scope);
2063 end if;
2065 Adjust_D;
2066 Freeze_All (Freeze_From, D);
2067 Freeze_From := Last_Entity (Current_Scope);
2069 elsif Scope (Current_Scope) /= Standard_Standard
2070 and then not Is_Child_Unit (Current_Scope)
2071 and then No (Generic_Parent (Parent (L)))
2072 then
2073 null;
2075 elsif L /= Visible_Declarations (Parent (L))
2076 or else No (Private_Declarations (Parent (L)))
2077 or else Is_Empty_List (Private_Declarations (Parent (L)))
2078 then
2079 Adjust_D;
2080 Freeze_All (Freeze_From, D);
2081 Freeze_From := Last_Entity (Current_Scope);
2082 end if;
2084 -- If next node is a body then freeze all types before the body.
2085 -- An exception occurs for some expander-generated bodies. If these
2086 -- are generated at places where in general language rules would not
2087 -- allow a freeze point, then we assume that the expander has
2088 -- explicitly checked that all required types are properly frozen,
2089 -- and we do not cause general freezing here. This special circuit
2090 -- is used when the encountered body is marked as having already
2091 -- been analyzed.
2093 -- In all other cases (bodies that come from source, and expander
2094 -- generated bodies that have not been analyzed yet), freeze all
2095 -- types now. Note that in the latter case, the expander must take
2096 -- care to attach the bodies at a proper place in the tree so as to
2097 -- not cause unwanted freezing at that point.
2099 elsif not Analyzed (Next_Node)
2100 and then (Nkind_In (Next_Node, N_Subprogram_Body,
2101 N_Entry_Body,
2102 N_Package_Body,
2103 N_Protected_Body,
2104 N_Task_Body)
2105 or else
2106 Nkind (Next_Node) in N_Body_Stub)
2107 then
2108 Adjust_D;
2109 Freeze_All (Freeze_From, D);
2110 Freeze_From := Last_Entity (Current_Scope);
2111 end if;
2113 D := Next_Node;
2114 end loop;
2115 end Analyze_Declarations;
2117 ----------------------------------
2118 -- Analyze_Incomplete_Type_Decl --
2119 ----------------------------------
2121 procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
2122 F : constant Boolean := Is_Pure (Current_Scope);
2123 T : Entity_Id;
2125 begin
2126 Generate_Definition (Defining_Identifier (N));
2128 -- Process an incomplete declaration. The identifier must not have been
2129 -- declared already in the scope. However, an incomplete declaration may
2130 -- appear in the private part of a package, for a private type that has
2131 -- already been declared.
2133 -- In this case, the discriminants (if any) must match
2135 T := Find_Type_Name (N);
2137 Set_Ekind (T, E_Incomplete_Type);
2138 Init_Size_Align (T);
2139 Set_Is_First_Subtype (T, True);
2140 Set_Etype (T, T);
2142 -- Ada 2005 (AI-326): Minimum decoration to give support to tagged
2143 -- incomplete types.
2145 if Tagged_Present (N) then
2146 Set_Is_Tagged_Type (T);
2147 Make_Class_Wide_Type (T);
2148 Set_Primitive_Operations (T, New_Elmt_List);
2149 end if;
2151 Push_Scope (T);
2153 Set_Stored_Constraint (T, No_Elist);
2155 if Present (Discriminant_Specifications (N)) then
2156 Process_Discriminants (N);
2157 end if;
2159 End_Scope;
2161 -- If the type has discriminants, non-trivial subtypes may be
2162 -- declared before the full view of the type. The full views of those
2163 -- subtypes will be built after the full view of the type.
2165 Set_Private_Dependents (T, New_Elmt_List);
2166 Set_Is_Pure (T, F);
2167 end Analyze_Incomplete_Type_Decl;
2169 -----------------------------------
2170 -- Analyze_Interface_Declaration --
2171 -----------------------------------
2173 procedure Analyze_Interface_Declaration (T : Entity_Id; Def : Node_Id) is
2174 CW : constant Entity_Id := Class_Wide_Type (T);
2176 begin
2177 Set_Is_Tagged_Type (T);
2179 Set_Is_Limited_Record (T, Limited_Present (Def)
2180 or else Task_Present (Def)
2181 or else Protected_Present (Def)
2182 or else Synchronized_Present (Def));
2184 -- Type is abstract if full declaration carries keyword, or if previous
2185 -- partial view did.
2187 Set_Is_Abstract_Type (T);
2188 Set_Is_Interface (T);
2190 -- Type is a limited interface if it includes the keyword limited, task,
2191 -- protected, or synchronized.
2193 Set_Is_Limited_Interface
2194 (T, Limited_Present (Def)
2195 or else Protected_Present (Def)
2196 or else Synchronized_Present (Def)
2197 or else Task_Present (Def));
2199 Set_Interfaces (T, New_Elmt_List);
2200 Set_Primitive_Operations (T, New_Elmt_List);
2202 -- Complete the decoration of the class-wide entity if it was already
2203 -- built (i.e. during the creation of the limited view)
2205 if Present (CW) then
2206 Set_Is_Interface (CW);
2207 Set_Is_Limited_Interface (CW, Is_Limited_Interface (T));
2208 end if;
2210 -- Check runtime support for synchronized interfaces
2212 if VM_Target = No_VM
2213 and then (Is_Task_Interface (T)
2214 or else Is_Protected_Interface (T)
2215 or else Is_Synchronized_Interface (T))
2216 and then not RTE_Available (RE_Select_Specific_Data)
2217 then
2218 Error_Msg_CRT ("synchronized interfaces", T);
2219 end if;
2220 end Analyze_Interface_Declaration;
2222 -----------------------------
2223 -- Analyze_Itype_Reference --
2224 -----------------------------
2226 -- Nothing to do. This node is placed in the tree only for the benefit of
2227 -- back end processing, and has no effect on the semantic processing.
2229 procedure Analyze_Itype_Reference (N : Node_Id) is
2230 begin
2231 pragma Assert (Is_Itype (Itype (N)));
2232 null;
2233 end Analyze_Itype_Reference;
2235 --------------------------------
2236 -- Analyze_Number_Declaration --
2237 --------------------------------
2239 procedure Analyze_Number_Declaration (N : Node_Id) is
2240 Id : constant Entity_Id := Defining_Identifier (N);
2241 E : constant Node_Id := Expression (N);
2242 T : Entity_Id;
2243 Index : Interp_Index;
2244 It : Interp;
2246 begin
2247 Generate_Definition (Id);
2248 Enter_Name (Id);
2250 -- This is an optimization of a common case of an integer literal
2252 if Nkind (E) = N_Integer_Literal then
2253 Set_Is_Static_Expression (E, True);
2254 Set_Etype (E, Universal_Integer);
2256 Set_Etype (Id, Universal_Integer);
2257 Set_Ekind (Id, E_Named_Integer);
2258 Set_Is_Frozen (Id, True);
2259 return;
2260 end if;
2262 Set_Is_Pure (Id, Is_Pure (Current_Scope));
2264 -- Process expression, replacing error by integer zero, to avoid
2265 -- cascaded errors or aborts further along in the processing
2267 -- Replace Error by integer zero, which seems least likely to
2268 -- cause cascaded errors.
2270 if E = Error then
2271 Rewrite (E, Make_Integer_Literal (Sloc (E), Uint_0));
2272 Set_Error_Posted (E);
2273 end if;
2275 Analyze (E);
2277 -- Verify that the expression is static and numeric. If
2278 -- the expression is overloaded, we apply the preference
2279 -- rule that favors root numeric types.
2281 if not Is_Overloaded (E) then
2282 T := Etype (E);
2284 else
2285 T := Any_Type;
2287 Get_First_Interp (E, Index, It);
2288 while Present (It.Typ) loop
2289 if (Is_Integer_Type (It.Typ)
2290 or else Is_Real_Type (It.Typ))
2291 and then (Scope (Base_Type (It.Typ))) = Standard_Standard
2292 then
2293 if T = Any_Type then
2294 T := It.Typ;
2296 elsif It.Typ = Universal_Real
2297 or else It.Typ = Universal_Integer
2298 then
2299 -- Choose universal interpretation over any other
2301 T := It.Typ;
2302 exit;
2303 end if;
2304 end if;
2306 Get_Next_Interp (Index, It);
2307 end loop;
2308 end if;
2310 if Is_Integer_Type (T) then
2311 Resolve (E, T);
2312 Set_Etype (Id, Universal_Integer);
2313 Set_Ekind (Id, E_Named_Integer);
2315 elsif Is_Real_Type (T) then
2317 -- Because the real value is converted to universal_real, this is a
2318 -- legal context for a universal fixed expression.
2320 if T = Universal_Fixed then
2321 declare
2322 Loc : constant Source_Ptr := Sloc (N);
2323 Conv : constant Node_Id := Make_Type_Conversion (Loc,
2324 Subtype_Mark =>
2325 New_Occurrence_Of (Universal_Real, Loc),
2326 Expression => Relocate_Node (E));
2328 begin
2329 Rewrite (E, Conv);
2330 Analyze (E);
2331 end;
2333 elsif T = Any_Fixed then
2334 Error_Msg_N ("illegal context for mixed mode operation", E);
2336 -- Expression is of the form : universal_fixed * integer. Try to
2337 -- resolve as universal_real.
2339 T := Universal_Real;
2340 Set_Etype (E, T);
2341 end if;
2343 Resolve (E, T);
2344 Set_Etype (Id, Universal_Real);
2345 Set_Ekind (Id, E_Named_Real);
2347 else
2348 Wrong_Type (E, Any_Numeric);
2349 Resolve (E, T);
2351 Set_Etype (Id, T);
2352 Set_Ekind (Id, E_Constant);
2353 Set_Never_Set_In_Source (Id, True);
2354 Set_Is_True_Constant (Id, True);
2355 return;
2356 end if;
2358 if Nkind_In (E, N_Integer_Literal, N_Real_Literal) then
2359 Set_Etype (E, Etype (Id));
2360 end if;
2362 if not Is_OK_Static_Expression (E) then
2363 Flag_Non_Static_Expr
2364 ("non-static expression used in number declaration!", E);
2365 Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
2366 Set_Etype (E, Any_Type);
2367 end if;
2368 end Analyze_Number_Declaration;
2370 --------------------------------
2371 -- Analyze_Object_Declaration --
2372 --------------------------------
2374 procedure Analyze_Object_Declaration (N : Node_Id) is
2375 Loc : constant Source_Ptr := Sloc (N);
2376 Id : constant Entity_Id := Defining_Identifier (N);
2377 T : Entity_Id;
2378 Act_T : Entity_Id;
2380 E : Node_Id := Expression (N);
2381 -- E is set to Expression (N) throughout this routine. When
2382 -- Expression (N) is modified, E is changed accordingly.
2384 Prev_Entity : Entity_Id := Empty;
2386 function Count_Tasks (T : Entity_Id) return Uint;
2387 -- This function is called when a non-generic library level object of a
2388 -- task type is declared. Its function is to count the static number of
2389 -- tasks declared within the type (it is only called if Has_Tasks is set
2390 -- for T). As a side effect, if an array of tasks with non-static bounds
2391 -- or a variant record type is encountered, Check_Restrictions is called
2392 -- indicating the count is unknown.
2394 -----------------
2395 -- Count_Tasks --
2396 -----------------
2398 function Count_Tasks (T : Entity_Id) return Uint is
2399 C : Entity_Id;
2400 X : Node_Id;
2401 V : Uint;
2403 begin
2404 if Is_Task_Type (T) then
2405 return Uint_1;
2407 elsif Is_Record_Type (T) then
2408 if Has_Discriminants (T) then
2409 Check_Restriction (Max_Tasks, N);
2410 return Uint_0;
2412 else
2413 V := Uint_0;
2414 C := First_Component (T);
2415 while Present (C) loop
2416 V := V + Count_Tasks (Etype (C));
2417 Next_Component (C);
2418 end loop;
2420 return V;
2421 end if;
2423 elsif Is_Array_Type (T) then
2424 X := First_Index (T);
2425 V := Count_Tasks (Component_Type (T));
2426 while Present (X) loop
2427 C := Etype (X);
2429 if not Is_Static_Subtype (C) then
2430 Check_Restriction (Max_Tasks, N);
2431 return Uint_0;
2432 else
2433 V := V * (UI_Max (Uint_0,
2434 Expr_Value (Type_High_Bound (C)) -
2435 Expr_Value (Type_Low_Bound (C)) + Uint_1));
2436 end if;
2438 Next_Index (X);
2439 end loop;
2441 return V;
2443 else
2444 return Uint_0;
2445 end if;
2446 end Count_Tasks;
2448 -- Start of processing for Analyze_Object_Declaration
2450 begin
2451 -- There are three kinds of implicit types generated by an
2452 -- object declaration:
2454 -- 1. Those for generated by the original Object Definition
2456 -- 2. Those generated by the Expression
2458 -- 3. Those used to constrained the Object Definition with the
2459 -- expression constraints when it is unconstrained
2461 -- They must be generated in this order to avoid order of elaboration
2462 -- issues. Thus the first step (after entering the name) is to analyze
2463 -- the object definition.
2465 if Constant_Present (N) then
2466 Prev_Entity := Current_Entity_In_Scope (Id);
2468 if Present (Prev_Entity)
2469 and then
2470 -- If the homograph is an implicit subprogram, it is overridden
2471 -- by the current declaration.
2473 ((Is_Overloadable (Prev_Entity)
2474 and then Is_Inherited_Operation (Prev_Entity))
2476 -- The current object is a discriminal generated for an entry
2477 -- family index. Even though the index is a constant, in this
2478 -- particular context there is no true constant redeclaration.
2479 -- Enter_Name will handle the visibility.
2481 or else
2482 (Is_Discriminal (Id)
2483 and then Ekind (Discriminal_Link (Id)) =
2484 E_Entry_Index_Parameter)
2486 -- The current object is the renaming for a generic declared
2487 -- within the instance.
2489 or else
2490 (Ekind (Prev_Entity) = E_Package
2491 and then Nkind (Parent (Prev_Entity)) =
2492 N_Package_Renaming_Declaration
2493 and then not Comes_From_Source (Prev_Entity)
2494 and then Is_Generic_Instance (Renamed_Entity (Prev_Entity))))
2495 then
2496 Prev_Entity := Empty;
2497 end if;
2498 end if;
2500 if Present (Prev_Entity) then
2501 Constant_Redeclaration (Id, N, T);
2503 Generate_Reference (Prev_Entity, Id, 'c');
2504 Set_Completion_Referenced (Id);
2506 if Error_Posted (N) then
2508 -- Type mismatch or illegal redeclaration, Do not analyze
2509 -- expression to avoid cascaded errors.
2511 T := Find_Type_Of_Object (Object_Definition (N), N);
2512 Set_Etype (Id, T);
2513 Set_Ekind (Id, E_Variable);
2514 return;
2515 end if;
2517 -- In the normal case, enter identifier at the start to catch premature
2518 -- usage in the initialization expression.
2520 else
2521 Generate_Definition (Id);
2522 Enter_Name (Id);
2524 Mark_Coextensions (N, Object_Definition (N));
2526 T := Find_Type_Of_Object (Object_Definition (N), N);
2528 if Nkind (Object_Definition (N)) = N_Access_Definition
2529 and then Present
2530 (Access_To_Subprogram_Definition (Object_Definition (N)))
2531 and then Protected_Present
2532 (Access_To_Subprogram_Definition (Object_Definition (N)))
2533 then
2534 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
2535 end if;
2537 if Error_Posted (Id) then
2538 Set_Etype (Id, T);
2539 Set_Ekind (Id, E_Variable);
2540 return;
2541 end if;
2542 end if;
2544 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
2545 -- out some static checks
2547 if Ada_Version >= Ada_05
2548 and then Can_Never_Be_Null (T)
2549 then
2550 -- In case of aggregates we must also take care of the correct
2551 -- initialization of nested aggregates bug this is done at the
2552 -- point of the analysis of the aggregate (see sem_aggr.adb)
2554 if Present (Expression (N))
2555 and then Nkind (Expression (N)) = N_Aggregate
2556 then
2557 null;
2559 else
2560 declare
2561 Save_Typ : constant Entity_Id := Etype (Id);
2562 begin
2563 Set_Etype (Id, T); -- Temp. decoration for static checks
2564 Null_Exclusion_Static_Checks (N);
2565 Set_Etype (Id, Save_Typ);
2566 end;
2567 end if;
2568 end if;
2570 Set_Is_Pure (Id, Is_Pure (Current_Scope));
2572 -- If deferred constant, make sure context is appropriate. We detect
2573 -- a deferred constant as a constant declaration with no expression.
2574 -- A deferred constant can appear in a package body if its completion
2575 -- is by means of an interface pragma.
2577 if Constant_Present (N)
2578 and then No (E)
2579 then
2580 -- A deferred constant may appear in the declarative part of the
2581 -- following constructs:
2583 -- blocks
2584 -- entry bodies
2585 -- extended return statements
2586 -- package specs
2587 -- package bodies
2588 -- subprogram bodies
2589 -- task bodies
2591 -- When declared inside a package spec, a deferred constant must be
2592 -- completed by a full constant declaration or pragma Import. In all
2593 -- other cases, the only proper completion is pragma Import. Extended
2594 -- return statements are flagged as invalid contexts because they do
2595 -- not have a declarative part and so cannot accommodate the pragma.
2597 if Ekind (Current_Scope) = E_Return_Statement then
2598 Error_Msg_N
2599 ("invalid context for deferred constant declaration (RM 7.4)",
2601 Error_Msg_N
2602 ("\declaration requires an initialization expression",
2604 Set_Constant_Present (N, False);
2606 -- In Ada 83, deferred constant must be of private type
2608 elsif not Is_Private_Type (T) then
2609 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
2610 Error_Msg_N
2611 ("(Ada 83) deferred constant must be private type", N);
2612 end if;
2613 end if;
2615 -- If not a deferred constant, then object declaration freezes its type
2617 else
2618 Check_Fully_Declared (T, N);
2619 Freeze_Before (N, T);
2620 end if;
2622 -- If the object was created by a constrained array definition, then
2623 -- set the link in both the anonymous base type and anonymous subtype
2624 -- that are built to represent the array type to point to the object.
2626 if Nkind (Object_Definition (Declaration_Node (Id))) =
2627 N_Constrained_Array_Definition
2628 then
2629 Set_Related_Array_Object (T, Id);
2630 Set_Related_Array_Object (Base_Type (T), Id);
2631 end if;
2633 -- Special checks for protected objects not at library level
2635 if Is_Protected_Type (T)
2636 and then not Is_Library_Level_Entity (Id)
2637 then
2638 Check_Restriction (No_Local_Protected_Objects, Id);
2640 -- Protected objects with interrupt handlers must be at library level
2642 -- Ada 2005: this test is not needed (and the corresponding clause
2643 -- in the RM is removed) because accessibility checks are sufficient
2644 -- to make handlers not at the library level illegal.
2646 if Has_Interrupt_Handler (T)
2647 and then Ada_Version < Ada_05
2648 then
2649 Error_Msg_N
2650 ("interrupt object can only be declared at library level", Id);
2651 end if;
2652 end if;
2654 -- The actual subtype of the object is the nominal subtype, unless
2655 -- the nominal one is unconstrained and obtained from the expression.
2657 Act_T := T;
2659 -- Process initialization expression if present and not in error
2661 if Present (E) and then E /= Error then
2663 -- Generate an error in case of CPP class-wide object initialization.
2664 -- Required because otherwise the expansion of the class-wide
2665 -- assignment would try to use 'size to initialize the object
2666 -- (primitive that is not available in CPP tagged types).
2668 if Is_Class_Wide_Type (Act_T)
2669 and then
2670 (Is_CPP_Class (Root_Type (Etype (Act_T)))
2671 or else
2672 (Present (Full_View (Root_Type (Etype (Act_T))))
2673 and then
2674 Is_CPP_Class (Full_View (Root_Type (Etype (Act_T))))))
2675 then
2676 Error_Msg_N
2677 ("predefined assignment not available for 'C'P'P tagged types",
2679 end if;
2681 Mark_Coextensions (N, E);
2682 Analyze (E);
2684 -- In case of errors detected in the analysis of the expression,
2685 -- decorate it with the expected type to avoid cascaded errors
2687 if No (Etype (E)) then
2688 Set_Etype (E, T);
2689 end if;
2691 -- If an initialization expression is present, then we set the
2692 -- Is_True_Constant flag. It will be reset if this is a variable
2693 -- and it is indeed modified.
2695 Set_Is_True_Constant (Id, True);
2697 -- If we are analyzing a constant declaration, set its completion
2698 -- flag after analyzing and resolving the expression.
2700 if Constant_Present (N) then
2701 Set_Has_Completion (Id);
2702 end if;
2704 -- Set type and resolve (type may be overridden later on)
2706 Set_Etype (Id, T);
2707 Resolve (E, T);
2709 -- If E is null and has been replaced by an N_Raise_Constraint_Error
2710 -- node (which was marked already-analyzed), we need to set the type
2711 -- to something other than Any_Access in order to keep gigi happy.
2713 if Etype (E) = Any_Access then
2714 Set_Etype (E, T);
2715 end if;
2717 -- If the object is an access to variable, the initialization
2718 -- expression cannot be an access to constant.
2720 if Is_Access_Type (T)
2721 and then not Is_Access_Constant (T)
2722 and then Is_Access_Type (Etype (E))
2723 and then Is_Access_Constant (Etype (E))
2724 then
2725 Error_Msg_N
2726 ("access to variable cannot be initialized "
2727 & "with an access-to-constant expression", E);
2728 end if;
2730 if not Assignment_OK (N) then
2731 Check_Initialization (T, E);
2732 end if;
2734 Check_Unset_Reference (E);
2736 -- If this is a variable, then set current value. If this is a
2737 -- declared constant of a scalar type with a static expression,
2738 -- indicate that it is always valid.
2740 if not Constant_Present (N) then
2741 if Compile_Time_Known_Value (E) then
2742 Set_Current_Value (Id, E);
2743 end if;
2745 elsif Is_Scalar_Type (T)
2746 and then Is_OK_Static_Expression (E)
2747 then
2748 Set_Is_Known_Valid (Id);
2749 end if;
2751 -- Deal with setting of null flags
2753 if Is_Access_Type (T) then
2754 if Known_Non_Null (E) then
2755 Set_Is_Known_Non_Null (Id, True);
2756 elsif Known_Null (E)
2757 and then not Can_Never_Be_Null (Id)
2758 then
2759 Set_Is_Known_Null (Id, True);
2760 end if;
2761 end if;
2763 -- Check incorrect use of dynamically tagged expressions.
2765 if Is_Tagged_Type (T) then
2766 Check_Dynamically_Tagged_Expression
2767 (Expr => E,
2768 Typ => T,
2769 Related_Nod => N);
2770 end if;
2772 Apply_Scalar_Range_Check (E, T);
2773 Apply_Static_Length_Check (E, T);
2774 end if;
2776 -- If the No_Streams restriction is set, check that the type of the
2777 -- object is not, and does not contain, any subtype derived from
2778 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
2779 -- Has_Stream just for efficiency reasons. There is no point in
2780 -- spending time on a Has_Stream check if the restriction is not set.
2782 if Restrictions.Set (No_Streams) then
2783 if Has_Stream (T) then
2784 Check_Restriction (No_Streams, N);
2785 end if;
2786 end if;
2788 -- Case of unconstrained type
2790 if Is_Indefinite_Subtype (T) then
2792 -- Nothing to do in deferred constant case
2794 if Constant_Present (N) and then No (E) then
2795 null;
2797 -- Case of no initialization present
2799 elsif No (E) then
2800 if No_Initialization (N) then
2801 null;
2803 elsif Is_Class_Wide_Type (T) then
2804 Error_Msg_N
2805 ("initialization required in class-wide declaration ", N);
2807 else
2808 Error_Msg_N
2809 ("unconstrained subtype not allowed (need initialization)",
2810 Object_Definition (N));
2812 if Is_Record_Type (T) and then Has_Discriminants (T) then
2813 Error_Msg_N
2814 ("\provide initial value or explicit discriminant values",
2815 Object_Definition (N));
2817 Error_Msg_NE
2818 ("\or give default discriminant values for type&",
2819 Object_Definition (N), T);
2821 elsif Is_Array_Type (T) then
2822 Error_Msg_N
2823 ("\provide initial value or explicit array bounds",
2824 Object_Definition (N));
2825 end if;
2826 end if;
2828 -- Case of initialization present but in error. Set initial
2829 -- expression as absent (but do not make above complaints)
2831 elsif E = Error then
2832 Set_Expression (N, Empty);
2833 E := Empty;
2835 -- Case of initialization present
2837 else
2838 -- Not allowed in Ada 83
2840 if not Constant_Present (N) then
2841 if Ada_Version = Ada_83
2842 and then Comes_From_Source (Object_Definition (N))
2843 then
2844 Error_Msg_N
2845 ("(Ada 83) unconstrained variable not allowed",
2846 Object_Definition (N));
2847 end if;
2848 end if;
2850 -- Now we constrain the variable from the initializing expression
2852 -- If the expression is an aggregate, it has been expanded into
2853 -- individual assignments. Retrieve the actual type from the
2854 -- expanded construct.
2856 if Is_Array_Type (T)
2857 and then No_Initialization (N)
2858 and then Nkind (Original_Node (E)) = N_Aggregate
2859 then
2860 Act_T := Etype (E);
2862 -- In case of class-wide interface object declarations we delay
2863 -- the generation of the equivalent record type declarations until
2864 -- its expansion because there are cases in they are not required.
2866 elsif Is_Interface (T) then
2867 null;
2869 else
2870 Expand_Subtype_From_Expr (N, T, Object_Definition (N), E);
2871 Act_T := Find_Type_Of_Object (Object_Definition (N), N);
2872 end if;
2874 Set_Is_Constr_Subt_For_U_Nominal (Act_T);
2876 if Aliased_Present (N) then
2877 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
2878 end if;
2880 Freeze_Before (N, Act_T);
2881 Freeze_Before (N, T);
2882 end if;
2884 elsif Is_Array_Type (T)
2885 and then No_Initialization (N)
2886 and then Nkind (Original_Node (E)) = N_Aggregate
2887 then
2888 if not Is_Entity_Name (Object_Definition (N)) then
2889 Act_T := Etype (E);
2890 Check_Compile_Time_Size (Act_T);
2892 if Aliased_Present (N) then
2893 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
2894 end if;
2895 end if;
2897 -- When the given object definition and the aggregate are specified
2898 -- independently, and their lengths might differ do a length check.
2899 -- This cannot happen if the aggregate is of the form (others =>...)
2901 if not Is_Constrained (T) then
2902 null;
2904 elsif Nkind (E) = N_Raise_Constraint_Error then
2906 -- Aggregate is statically illegal. Place back in declaration
2908 Set_Expression (N, E);
2909 Set_No_Initialization (N, False);
2911 elsif T = Etype (E) then
2912 null;
2914 elsif Nkind (E) = N_Aggregate
2915 and then Present (Component_Associations (E))
2916 and then Present (Choices (First (Component_Associations (E))))
2917 and then Nkind (First
2918 (Choices (First (Component_Associations (E))))) = N_Others_Choice
2919 then
2920 null;
2922 else
2923 Apply_Length_Check (E, T);
2924 end if;
2926 -- If the type is limited unconstrained with defaulted discriminants and
2927 -- there is no expression, then the object is constrained by the
2928 -- defaults, so it is worthwhile building the corresponding subtype.
2930 elsif (Is_Limited_Record (T) or else Is_Concurrent_Type (T))
2931 and then not Is_Constrained (T)
2932 and then Has_Discriminants (T)
2933 then
2934 if No (E) then
2935 Act_T := Build_Default_Subtype (T, N);
2936 else
2937 -- Ada 2005: a limited object may be initialized by means of an
2938 -- aggregate. If the type has default discriminants it has an
2939 -- unconstrained nominal type, Its actual subtype will be obtained
2940 -- from the aggregate, and not from the default discriminants.
2942 Act_T := Etype (E);
2943 end if;
2945 Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
2947 elsif Present (Underlying_Type (T))
2948 and then not Is_Constrained (Underlying_Type (T))
2949 and then Has_Discriminants (Underlying_Type (T))
2950 and then Nkind (E) = N_Function_Call
2951 and then Constant_Present (N)
2952 then
2953 -- The back-end has problems with constants of a discriminated type
2954 -- with defaults, if the initial value is a function call. We
2955 -- generate an intermediate temporary for the result of the call.
2956 -- It is unclear why this should make it acceptable to gcc. ???
2958 Remove_Side_Effects (E);
2959 end if;
2961 -- Check No_Wide_Characters restriction
2963 if T = Standard_Wide_Character
2964 or else T = Standard_Wide_Wide_Character
2965 or else Root_Type (T) = Standard_Wide_String
2966 or else Root_Type (T) = Standard_Wide_Wide_String
2967 then
2968 Check_Restriction (No_Wide_Characters, Object_Definition (N));
2969 end if;
2971 -- Indicate this is not set in source. Certainly true for constants,
2972 -- and true for variables so far (will be reset for a variable if and
2973 -- when we encounter a modification in the source).
2975 Set_Never_Set_In_Source (Id, True);
2977 -- Now establish the proper kind and type of the object
2979 if Constant_Present (N) then
2980 Set_Ekind (Id, E_Constant);
2981 Set_Is_True_Constant (Id, True);
2983 else
2984 Set_Ekind (Id, E_Variable);
2986 -- A variable is set as shared passive if it appears in a shared
2987 -- passive package, and is at the outer level. This is not done
2988 -- for entities generated during expansion, because those are
2989 -- always manipulated locally.
2991 if Is_Shared_Passive (Current_Scope)
2992 and then Is_Library_Level_Entity (Id)
2993 and then Comes_From_Source (Id)
2994 then
2995 Set_Is_Shared_Passive (Id);
2996 Check_Shared_Var (Id, T, N);
2997 end if;
2999 -- Set Has_Initial_Value if initializing expression present. Note
3000 -- that if there is no initializing expression, we leave the state
3001 -- of this flag unchanged (usually it will be False, but notably in
3002 -- the case of exception choice variables, it will already be true).
3004 if Present (E) then
3005 Set_Has_Initial_Value (Id, True);
3006 end if;
3007 end if;
3009 -- Initialize alignment and size and capture alignment setting
3011 Init_Alignment (Id);
3012 Init_Esize (Id);
3013 Set_Optimize_Alignment_Flags (Id);
3015 -- Deal with aliased case
3017 if Aliased_Present (N) then
3018 Set_Is_Aliased (Id);
3020 -- If the object is aliased and the type is unconstrained with
3021 -- defaulted discriminants and there is no expression, then the
3022 -- object is constrained by the defaults, so it is worthwhile
3023 -- building the corresponding subtype.
3025 -- Ada 2005 (AI-363): If the aliased object is discriminated and
3026 -- unconstrained, then only establish an actual subtype if the
3027 -- nominal subtype is indefinite. In definite cases the object is
3028 -- unconstrained in Ada 2005.
3030 if No (E)
3031 and then Is_Record_Type (T)
3032 and then not Is_Constrained (T)
3033 and then Has_Discriminants (T)
3034 and then (Ada_Version < Ada_05 or else Is_Indefinite_Subtype (T))
3035 then
3036 Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
3037 end if;
3038 end if;
3040 -- Now we can set the type of the object
3042 Set_Etype (Id, Act_T);
3044 -- Deal with controlled types
3046 if Has_Controlled_Component (Etype (Id))
3047 or else Is_Controlled (Etype (Id))
3048 then
3049 if not Is_Library_Level_Entity (Id) then
3050 Check_Restriction (No_Nested_Finalization, N);
3051 else
3052 Validate_Controlled_Object (Id);
3053 end if;
3055 -- Generate a warning when an initialization causes an obvious ABE
3056 -- violation. If the init expression is a simple aggregate there
3057 -- shouldn't be any initialize/adjust call generated. This will be
3058 -- true as soon as aggregates are built in place when possible.
3060 -- ??? at the moment we do not generate warnings for temporaries
3061 -- created for those aggregates although Program_Error might be
3062 -- generated if compiled with -gnato.
3064 if Is_Controlled (Etype (Id))
3065 and then Comes_From_Source (Id)
3066 then
3067 declare
3068 BT : constant Entity_Id := Base_Type (Etype (Id));
3070 Implicit_Call : Entity_Id;
3071 pragma Warnings (Off, Implicit_Call);
3072 -- ??? what is this for (never referenced!)
3074 function Is_Aggr (N : Node_Id) return Boolean;
3075 -- Check that N is an aggregate
3077 -------------
3078 -- Is_Aggr --
3079 -------------
3081 function Is_Aggr (N : Node_Id) return Boolean is
3082 begin
3083 case Nkind (Original_Node (N)) is
3084 when N_Aggregate | N_Extension_Aggregate =>
3085 return True;
3087 when N_Qualified_Expression |
3088 N_Type_Conversion |
3089 N_Unchecked_Type_Conversion =>
3090 return Is_Aggr (Expression (Original_Node (N)));
3092 when others =>
3093 return False;
3094 end case;
3095 end Is_Aggr;
3097 begin
3098 -- If no underlying type, we already are in an error situation.
3099 -- Do not try to add a warning since we do not have access to
3100 -- prim-op list.
3102 if No (Underlying_Type (BT)) then
3103 Implicit_Call := Empty;
3105 -- A generic type does not have usable primitive operators.
3106 -- Initialization calls are built for instances.
3108 elsif Is_Generic_Type (BT) then
3109 Implicit_Call := Empty;
3111 -- If the init expression is not an aggregate, an adjust call
3112 -- will be generated
3114 elsif Present (E) and then not Is_Aggr (E) then
3115 Implicit_Call := Find_Prim_Op (BT, Name_Adjust);
3117 -- If no init expression and we are not in the deferred
3118 -- constant case, an Initialize call will be generated
3120 elsif No (E) and then not Constant_Present (N) then
3121 Implicit_Call := Find_Prim_Op (BT, Name_Initialize);
3123 else
3124 Implicit_Call := Empty;
3125 end if;
3126 end;
3127 end if;
3128 end if;
3130 if Has_Task (Etype (Id)) then
3131 Check_Restriction (No_Tasking, N);
3133 -- Deal with counting max tasks
3135 -- Nothing to do if inside a generic
3137 if Inside_A_Generic then
3138 null;
3140 -- If library level entity, then count tasks
3142 elsif Is_Library_Level_Entity (Id) then
3143 Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
3145 -- If not library level entity, then indicate we don't know max
3146 -- tasks and also check task hierarchy restriction and blocking
3147 -- operation (since starting a task is definitely blocking!)
3149 else
3150 Check_Restriction (Max_Tasks, N);
3151 Check_Restriction (No_Task_Hierarchy, N);
3152 Check_Potentially_Blocking_Operation (N);
3153 end if;
3155 -- A rather specialized test. If we see two tasks being declared
3156 -- of the same type in the same object declaration, and the task
3157 -- has an entry with an address clause, we know that program error
3158 -- will be raised at run-time since we can't have two tasks with
3159 -- entries at the same address.
3161 if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
3162 declare
3163 E : Entity_Id;
3165 begin
3166 E := First_Entity (Etype (Id));
3167 while Present (E) loop
3168 if Ekind (E) = E_Entry
3169 and then Present (Get_Attribute_Definition_Clause
3170 (E, Attribute_Address))
3171 then
3172 Error_Msg_N
3173 ("?more than one task with same entry address", N);
3174 Error_Msg_N
3175 ("\?Program_Error will be raised at run time", N);
3176 Insert_Action (N,
3177 Make_Raise_Program_Error (Loc,
3178 Reason => PE_Duplicated_Entry_Address));
3179 exit;
3180 end if;
3182 Next_Entity (E);
3183 end loop;
3184 end;
3185 end if;
3186 end if;
3188 -- Some simple constant-propagation: if the expression is a constant
3189 -- string initialized with a literal, share the literal. This avoids
3190 -- a run-time copy.
3192 if Present (E)
3193 and then Is_Entity_Name (E)
3194 and then Ekind (Entity (E)) = E_Constant
3195 and then Base_Type (Etype (E)) = Standard_String
3196 then
3197 declare
3198 Val : constant Node_Id := Constant_Value (Entity (E));
3199 begin
3200 if Present (Val)
3201 and then Nkind (Val) = N_String_Literal
3202 then
3203 Rewrite (E, New_Copy (Val));
3204 end if;
3205 end;
3206 end if;
3208 -- Another optimization: if the nominal subtype is unconstrained and
3209 -- the expression is a function call that returns an unconstrained
3210 -- type, rewrite the declaration as a renaming of the result of the
3211 -- call. The exceptions below are cases where the copy is expected,
3212 -- either by the back end (Aliased case) or by the semantics, as for
3213 -- initializing controlled types or copying tags for classwide types.
3215 if Present (E)
3216 and then Nkind (E) = N_Explicit_Dereference
3217 and then Nkind (Original_Node (E)) = N_Function_Call
3218 and then not Is_Library_Level_Entity (Id)
3219 and then not Is_Constrained (Underlying_Type (T))
3220 and then not Is_Aliased (Id)
3221 and then not Is_Class_Wide_Type (T)
3222 and then not Is_Controlled (T)
3223 and then not Has_Controlled_Component (Base_Type (T))
3224 and then Expander_Active
3225 then
3226 Rewrite (N,
3227 Make_Object_Renaming_Declaration (Loc,
3228 Defining_Identifier => Id,
3229 Access_Definition => Empty,
3230 Subtype_Mark => New_Occurrence_Of
3231 (Base_Type (Etype (Id)), Loc),
3232 Name => E));
3234 Set_Renamed_Object (Id, E);
3236 -- Force generation of debugging information for the constant and for
3237 -- the renamed function call.
3239 Set_Debug_Info_Needed (Id);
3240 Set_Debug_Info_Needed (Entity (Prefix (E)));
3241 end if;
3243 if Present (Prev_Entity)
3244 and then Is_Frozen (Prev_Entity)
3245 and then not Error_Posted (Id)
3246 then
3247 Error_Msg_N ("full constant declaration appears too late", N);
3248 end if;
3250 Check_Eliminated (Id);
3252 -- Deal with setting In_Private_Part flag if in private part
3254 if Ekind (Scope (Id)) = E_Package
3255 and then In_Private_Part (Scope (Id))
3256 then
3257 Set_In_Private_Part (Id);
3258 end if;
3260 -- Check for violation of No_Local_Timing_Events
3262 if Is_RTE (Etype (Id), RE_Timing_Event)
3263 and then not Is_Library_Level_Entity (Id)
3264 then
3265 Check_Restriction (No_Local_Timing_Events, N);
3266 end if;
3267 end Analyze_Object_Declaration;
3269 ---------------------------
3270 -- Analyze_Others_Choice --
3271 ---------------------------
3273 -- Nothing to do for the others choice node itself, the semantic analysis
3274 -- of the others choice will occur as part of the processing of the parent
3276 procedure Analyze_Others_Choice (N : Node_Id) is
3277 pragma Warnings (Off, N);
3278 begin
3279 null;
3280 end Analyze_Others_Choice;
3282 -------------------------------------------
3283 -- Analyze_Private_Extension_Declaration --
3284 -------------------------------------------
3286 procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
3287 T : constant Entity_Id := Defining_Identifier (N);
3288 Indic : constant Node_Id := Subtype_Indication (N);
3289 Parent_Type : Entity_Id;
3290 Parent_Base : Entity_Id;
3292 begin
3293 -- Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
3295 if Is_Non_Empty_List (Interface_List (N)) then
3296 declare
3297 Intf : Node_Id;
3298 T : Entity_Id;
3300 begin
3301 Intf := First (Interface_List (N));
3302 while Present (Intf) loop
3303 T := Find_Type_Of_Subtype_Indic (Intf);
3305 Diagnose_Interface (Intf, T);
3306 Next (Intf);
3307 end loop;
3308 end;
3309 end if;
3311 Generate_Definition (T);
3312 Enter_Name (T);
3314 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
3315 Parent_Base := Base_Type (Parent_Type);
3317 if Parent_Type = Any_Type
3318 or else Etype (Parent_Type) = Any_Type
3319 then
3320 Set_Ekind (T, Ekind (Parent_Type));
3321 Set_Etype (T, Any_Type);
3322 return;
3324 elsif not Is_Tagged_Type (Parent_Type) then
3325 Error_Msg_N
3326 ("parent of type extension must be a tagged type ", Indic);
3327 return;
3329 elsif Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
3330 Error_Msg_N ("premature derivation of incomplete type", Indic);
3331 return;
3333 elsif Is_Concurrent_Type (Parent_Type) then
3334 Error_Msg_N
3335 ("parent type of a private extension cannot be "
3336 & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
3338 Set_Etype (T, Any_Type);
3339 Set_Ekind (T, E_Limited_Private_Type);
3340 Set_Private_Dependents (T, New_Elmt_List);
3341 Set_Error_Posted (T);
3342 return;
3343 end if;
3345 -- Perhaps the parent type should be changed to the class-wide type's
3346 -- specific type in this case to prevent cascading errors ???
3348 if Is_Class_Wide_Type (Parent_Type) then
3349 Error_Msg_N
3350 ("parent of type extension must not be a class-wide type", Indic);
3351 return;
3352 end if;
3354 if (not Is_Package_Or_Generic_Package (Current_Scope)
3355 and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
3356 or else In_Private_Part (Current_Scope)
3358 then
3359 Error_Msg_N ("invalid context for private extension", N);
3360 end if;
3362 -- Set common attributes
3364 Set_Is_Pure (T, Is_Pure (Current_Scope));
3365 Set_Scope (T, Current_Scope);
3366 Set_Ekind (T, E_Record_Type_With_Private);
3367 Init_Size_Align (T);
3369 Set_Etype (T, Parent_Base);
3370 Set_Has_Task (T, Has_Task (Parent_Base));
3372 Set_Convention (T, Convention (Parent_Type));
3373 Set_First_Rep_Item (T, First_Rep_Item (Parent_Type));
3374 Set_Is_First_Subtype (T);
3375 Make_Class_Wide_Type (T);
3377 if Unknown_Discriminants_Present (N) then
3378 Set_Discriminant_Constraint (T, No_Elist);
3379 end if;
3381 Build_Derived_Record_Type (N, Parent_Type, T);
3383 -- Ada 2005 (AI-443): Synchronized private extension or a rewritten
3384 -- synchronized formal derived type.
3386 if Ada_Version >= Ada_05
3387 and then Synchronized_Present (N)
3388 then
3389 Set_Is_Limited_Record (T);
3391 -- Formal derived type case
3393 if Is_Generic_Type (T) then
3395 -- The parent must be a tagged limited type or a synchronized
3396 -- interface.
3398 if (not Is_Tagged_Type (Parent_Type)
3399 or else not Is_Limited_Type (Parent_Type))
3400 and then
3401 (not Is_Interface (Parent_Type)
3402 or else not Is_Synchronized_Interface (Parent_Type))
3403 then
3404 Error_Msg_NE ("parent type of & must be tagged limited " &
3405 "or synchronized", N, T);
3406 end if;
3408 -- The progenitors (if any) must be limited or synchronized
3409 -- interfaces.
3411 if Present (Interfaces (T)) then
3412 declare
3413 Iface : Entity_Id;
3414 Iface_Elmt : Elmt_Id;
3416 begin
3417 Iface_Elmt := First_Elmt (Interfaces (T));
3418 while Present (Iface_Elmt) loop
3419 Iface := Node (Iface_Elmt);
3421 if not Is_Limited_Interface (Iface)
3422 and then not Is_Synchronized_Interface (Iface)
3423 then
3424 Error_Msg_NE ("progenitor & must be limited " &
3425 "or synchronized", N, Iface);
3426 end if;
3428 Next_Elmt (Iface_Elmt);
3429 end loop;
3430 end;
3431 end if;
3433 -- Regular derived extension, the parent must be a limited or
3434 -- synchronized interface.
3436 else
3437 if not Is_Interface (Parent_Type)
3438 or else (not Is_Limited_Interface (Parent_Type)
3439 and then
3440 not Is_Synchronized_Interface (Parent_Type))
3441 then
3442 Error_Msg_NE
3443 ("parent type of & must be limited interface", N, T);
3444 end if;
3445 end if;
3447 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
3448 -- extension with a synchronized parent must be explicitly declared
3449 -- synchronized, because the full view will be a synchronized type.
3450 -- This must be checked before the check for limited types below,
3451 -- to ensure that types declared limited are not allowed to extend
3452 -- synchronized interfaces.
3454 elsif Is_Interface (Parent_Type)
3455 and then Is_Synchronized_Interface (Parent_Type)
3456 and then not Synchronized_Present (N)
3457 then
3458 Error_Msg_NE
3459 ("private extension of& must be explicitly synchronized",
3460 N, Parent_Type);
3462 elsif Limited_Present (N) then
3463 Set_Is_Limited_Record (T);
3465 if not Is_Limited_Type (Parent_Type)
3466 and then
3467 (not Is_Interface (Parent_Type)
3468 or else not Is_Limited_Interface (Parent_Type))
3469 then
3470 Error_Msg_NE ("parent type& of limited extension must be limited",
3471 N, Parent_Type);
3472 end if;
3473 end if;
3474 end Analyze_Private_Extension_Declaration;
3476 ---------------------------------
3477 -- Analyze_Subtype_Declaration --
3478 ---------------------------------
3480 procedure Analyze_Subtype_Declaration
3481 (N : Node_Id;
3482 Skip : Boolean := False)
3484 Id : constant Entity_Id := Defining_Identifier (N);
3485 T : Entity_Id;
3486 R_Checks : Check_Result;
3488 begin
3489 Generate_Definition (Id);
3490 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3491 Init_Size_Align (Id);
3493 -- The following guard condition on Enter_Name is to handle cases where
3494 -- the defining identifier has already been entered into the scope but
3495 -- the declaration as a whole needs to be analyzed.
3497 -- This case in particular happens for derived enumeration types. The
3498 -- derived enumeration type is processed as an inserted enumeration type
3499 -- declaration followed by a rewritten subtype declaration. The defining
3500 -- identifier, however, is entered into the name scope very early in the
3501 -- processing of the original type declaration and therefore needs to be
3502 -- avoided here, when the created subtype declaration is analyzed. (See
3503 -- Build_Derived_Types)
3505 -- This also happens when the full view of a private type is derived
3506 -- type with constraints. In this case the entity has been introduced
3507 -- in the private declaration.
3509 if Skip
3510 or else (Present (Etype (Id))
3511 and then (Is_Private_Type (Etype (Id))
3512 or else Is_Task_Type (Etype (Id))
3513 or else Is_Rewrite_Substitution (N)))
3514 then
3515 null;
3517 else
3518 Enter_Name (Id);
3519 end if;
3521 T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
3523 -- Inherit common attributes
3525 Set_Is_Generic_Type (Id, Is_Generic_Type (Base_Type (T)));
3526 Set_Is_Volatile (Id, Is_Volatile (T));
3527 Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
3528 Set_Is_Atomic (Id, Is_Atomic (T));
3529 Set_Is_Ada_2005_Only (Id, Is_Ada_2005_Only (T));
3530 Set_Convention (Id, Convention (T));
3532 -- In the case where there is no constraint given in the subtype
3533 -- indication, Process_Subtype just returns the Subtype_Mark, so its
3534 -- semantic attributes must be established here.
3536 if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
3537 Set_Etype (Id, Base_Type (T));
3539 case Ekind (T) is
3540 when Array_Kind =>
3541 Set_Ekind (Id, E_Array_Subtype);
3542 Copy_Array_Subtype_Attributes (Id, T);
3544 when Decimal_Fixed_Point_Kind =>
3545 Set_Ekind (Id, E_Decimal_Fixed_Point_Subtype);
3546 Set_Digits_Value (Id, Digits_Value (T));
3547 Set_Delta_Value (Id, Delta_Value (T));
3548 Set_Scale_Value (Id, Scale_Value (T));
3549 Set_Small_Value (Id, Small_Value (T));
3550 Set_Scalar_Range (Id, Scalar_Range (T));
3551 Set_Machine_Radix_10 (Id, Machine_Radix_10 (T));
3552 Set_Is_Constrained (Id, Is_Constrained (T));
3553 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
3554 Set_RM_Size (Id, RM_Size (T));
3556 when Enumeration_Kind =>
3557 Set_Ekind (Id, E_Enumeration_Subtype);
3558 Set_First_Literal (Id, First_Literal (Base_Type (T)));
3559 Set_Scalar_Range (Id, Scalar_Range (T));
3560 Set_Is_Character_Type (Id, Is_Character_Type (T));
3561 Set_Is_Constrained (Id, Is_Constrained (T));
3562 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
3563 Set_RM_Size (Id, RM_Size (T));
3565 when Ordinary_Fixed_Point_Kind =>
3566 Set_Ekind (Id, E_Ordinary_Fixed_Point_Subtype);
3567 Set_Scalar_Range (Id, Scalar_Range (T));
3568 Set_Small_Value (Id, Small_Value (T));
3569 Set_Delta_Value (Id, Delta_Value (T));
3570 Set_Is_Constrained (Id, Is_Constrained (T));
3571 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
3572 Set_RM_Size (Id, RM_Size (T));
3574 when Float_Kind =>
3575 Set_Ekind (Id, E_Floating_Point_Subtype);
3576 Set_Scalar_Range (Id, Scalar_Range (T));
3577 Set_Digits_Value (Id, Digits_Value (T));
3578 Set_Is_Constrained (Id, Is_Constrained (T));
3580 when Signed_Integer_Kind =>
3581 Set_Ekind (Id, E_Signed_Integer_Subtype);
3582 Set_Scalar_Range (Id, Scalar_Range (T));
3583 Set_Is_Constrained (Id, Is_Constrained (T));
3584 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
3585 Set_RM_Size (Id, RM_Size (T));
3587 when Modular_Integer_Kind =>
3588 Set_Ekind (Id, E_Modular_Integer_Subtype);
3589 Set_Scalar_Range (Id, Scalar_Range (T));
3590 Set_Is_Constrained (Id, Is_Constrained (T));
3591 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
3592 Set_RM_Size (Id, RM_Size (T));
3594 when Class_Wide_Kind =>
3595 Set_Ekind (Id, E_Class_Wide_Subtype);
3596 Set_First_Entity (Id, First_Entity (T));
3597 Set_Last_Entity (Id, Last_Entity (T));
3598 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
3599 Set_Cloned_Subtype (Id, T);
3600 Set_Is_Tagged_Type (Id, True);
3601 Set_Has_Unknown_Discriminants
3602 (Id, True);
3604 if Ekind (T) = E_Class_Wide_Subtype then
3605 Set_Equivalent_Type (Id, Equivalent_Type (T));
3606 end if;
3608 when E_Record_Type | E_Record_Subtype =>
3609 Set_Ekind (Id, E_Record_Subtype);
3611 if Ekind (T) = E_Record_Subtype
3612 and then Present (Cloned_Subtype (T))
3613 then
3614 Set_Cloned_Subtype (Id, Cloned_Subtype (T));
3615 else
3616 Set_Cloned_Subtype (Id, T);
3617 end if;
3619 Set_First_Entity (Id, First_Entity (T));
3620 Set_Last_Entity (Id, Last_Entity (T));
3621 Set_Has_Discriminants (Id, Has_Discriminants (T));
3622 Set_Is_Constrained (Id, Is_Constrained (T));
3623 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
3624 Set_Has_Unknown_Discriminants
3625 (Id, Has_Unknown_Discriminants (T));
3627 if Has_Discriminants (T) then
3628 Set_Discriminant_Constraint
3629 (Id, Discriminant_Constraint (T));
3630 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
3632 elsif Has_Unknown_Discriminants (Id) then
3633 Set_Discriminant_Constraint (Id, No_Elist);
3634 end if;
3636 if Is_Tagged_Type (T) then
3637 Set_Is_Tagged_Type (Id);
3638 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
3639 Set_Primitive_Operations
3640 (Id, Primitive_Operations (T));
3641 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
3643 if Is_Interface (T) then
3644 Set_Is_Interface (Id);
3645 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
3646 end if;
3647 end if;
3649 when Private_Kind =>
3650 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
3651 Set_Has_Discriminants (Id, Has_Discriminants (T));
3652 Set_Is_Constrained (Id, Is_Constrained (T));
3653 Set_First_Entity (Id, First_Entity (T));
3654 Set_Last_Entity (Id, Last_Entity (T));
3655 Set_Private_Dependents (Id, New_Elmt_List);
3656 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
3657 Set_Has_Unknown_Discriminants
3658 (Id, Has_Unknown_Discriminants (T));
3659 Set_Known_To_Have_Preelab_Init
3660 (Id, Known_To_Have_Preelab_Init (T));
3662 if Is_Tagged_Type (T) then
3663 Set_Is_Tagged_Type (Id);
3664 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
3665 Set_Primitive_Operations (Id, Primitive_Operations (T));
3666 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
3667 end if;
3669 -- In general the attributes of the subtype of a private type
3670 -- are the attributes of the partial view of parent. However,
3671 -- the full view may be a discriminated type, and the subtype
3672 -- must share the discriminant constraint to generate correct
3673 -- calls to initialization procedures.
3675 if Has_Discriminants (T) then
3676 Set_Discriminant_Constraint
3677 (Id, Discriminant_Constraint (T));
3678 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
3680 elsif Present (Full_View (T))
3681 and then Has_Discriminants (Full_View (T))
3682 then
3683 Set_Discriminant_Constraint
3684 (Id, Discriminant_Constraint (Full_View (T)));
3685 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
3687 -- This would seem semantically correct, but apparently
3688 -- confuses the back-end. To be explained and checked with
3689 -- current version ???
3691 -- Set_Has_Discriminants (Id);
3692 end if;
3694 Prepare_Private_Subtype_Completion (Id, N);
3696 when Access_Kind =>
3697 Set_Ekind (Id, E_Access_Subtype);
3698 Set_Is_Constrained (Id, Is_Constrained (T));
3699 Set_Is_Access_Constant
3700 (Id, Is_Access_Constant (T));
3701 Set_Directly_Designated_Type
3702 (Id, Designated_Type (T));
3703 Set_Can_Never_Be_Null (Id, Can_Never_Be_Null (T));
3705 -- A Pure library_item must not contain the declaration of a
3706 -- named access type, except within a subprogram, generic
3707 -- subprogram, task unit, or protected unit, or if it has
3708 -- a specified Storage_Size of zero (RM05-10.2.1(15.4-15.5)).
3710 if Comes_From_Source (Id)
3711 and then In_Pure_Unit
3712 and then not In_Subprogram_Task_Protected_Unit
3713 and then not No_Pool_Assigned (Id)
3714 then
3715 Error_Msg_N
3716 ("named access types not allowed in pure unit", N);
3717 end if;
3719 when Concurrent_Kind =>
3720 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
3721 Set_Corresponding_Record_Type (Id,
3722 Corresponding_Record_Type (T));
3723 Set_First_Entity (Id, First_Entity (T));
3724 Set_First_Private_Entity (Id, First_Private_Entity (T));
3725 Set_Has_Discriminants (Id, Has_Discriminants (T));
3726 Set_Is_Constrained (Id, Is_Constrained (T));
3727 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
3728 Set_Last_Entity (Id, Last_Entity (T));
3730 if Has_Discriminants (T) then
3731 Set_Discriminant_Constraint (Id,
3732 Discriminant_Constraint (T));
3733 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
3734 end if;
3736 when E_Incomplete_Type =>
3737 if Ada_Version >= Ada_05 then
3738 Set_Ekind (Id, E_Incomplete_Subtype);
3740 -- Ada 2005 (AI-412): Decorate an incomplete subtype
3741 -- of an incomplete type visible through a limited
3742 -- with clause.
3744 if From_With_Type (T)
3745 and then Present (Non_Limited_View (T))
3746 then
3747 Set_From_With_Type (Id);
3748 Set_Non_Limited_View (Id, Non_Limited_View (T));
3750 -- Ada 2005 (AI-412): Add the regular incomplete subtype
3751 -- to the private dependents of the original incomplete
3752 -- type for future transformation.
3754 else
3755 Append_Elmt (Id, Private_Dependents (T));
3756 end if;
3758 -- If the subtype name denotes an incomplete type an error
3759 -- was already reported by Process_Subtype.
3761 else
3762 Set_Etype (Id, Any_Type);
3763 end if;
3765 when others =>
3766 raise Program_Error;
3767 end case;
3768 end if;
3770 if Etype (Id) = Any_Type then
3771 return;
3772 end if;
3774 -- Some common processing on all types
3776 Set_Size_Info (Id, T);
3777 Set_First_Rep_Item (Id, First_Rep_Item (T));
3779 T := Etype (Id);
3781 Set_Is_Immediately_Visible (Id, True);
3782 Set_Depends_On_Private (Id, Has_Private_Component (T));
3783 Set_Is_Descendent_Of_Address (Id, Is_Descendent_Of_Address (T));
3785 if Is_Interface (T) then
3786 Set_Is_Interface (Id);
3787 end if;
3789 if Present (Generic_Parent_Type (N))
3790 and then
3791 (Nkind
3792 (Parent (Generic_Parent_Type (N))) /= N_Formal_Type_Declaration
3793 or else Nkind
3794 (Formal_Type_Definition (Parent (Generic_Parent_Type (N))))
3795 /= N_Formal_Private_Type_Definition)
3796 then
3797 if Is_Tagged_Type (Id) then
3799 -- If this is a generic actual subtype for a synchronized type,
3800 -- the primitive operations are those of the corresponding record
3801 -- for which there is a separate subtype declaration.
3803 if Is_Concurrent_Type (Id) then
3804 null;
3805 elsif Is_Class_Wide_Type (Id) then
3806 Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
3807 else
3808 Derive_Subprograms (Generic_Parent_Type (N), Id, T);
3809 end if;
3811 elsif Scope (Etype (Id)) /= Standard_Standard then
3812 Derive_Subprograms (Generic_Parent_Type (N), Id);
3813 end if;
3814 end if;
3816 if Is_Private_Type (T)
3817 and then Present (Full_View (T))
3818 then
3819 Conditional_Delay (Id, Full_View (T));
3821 -- The subtypes of components or subcomponents of protected types
3822 -- do not need freeze nodes, which would otherwise appear in the
3823 -- wrong scope (before the freeze node for the protected type). The
3824 -- proper subtypes are those of the subcomponents of the corresponding
3825 -- record.
3827 elsif Ekind (Scope (Id)) /= E_Protected_Type
3828 and then Present (Scope (Scope (Id))) -- error defense!
3829 and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
3830 then
3831 Conditional_Delay (Id, T);
3832 end if;
3834 -- Check that constraint_error is raised for a scalar subtype
3835 -- indication when the lower or upper bound of a non-null range
3836 -- lies outside the range of the type mark.
3838 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
3839 if Is_Scalar_Type (Etype (Id))
3840 and then Scalar_Range (Id) /=
3841 Scalar_Range (Etype (Subtype_Mark
3842 (Subtype_Indication (N))))
3843 then
3844 Apply_Range_Check
3845 (Scalar_Range (Id),
3846 Etype (Subtype_Mark (Subtype_Indication (N))));
3848 elsif Is_Array_Type (Etype (Id))
3849 and then Present (First_Index (Id))
3850 then
3851 -- This really should be a subprogram that finds the indications
3852 -- to check???
3854 if ((Nkind (First_Index (Id)) = N_Identifier
3855 and then Ekind (Entity (First_Index (Id))) in Scalar_Kind)
3856 or else Nkind (First_Index (Id)) = N_Subtype_Indication)
3857 and then
3858 Nkind (Scalar_Range (Etype (First_Index (Id)))) = N_Range
3859 then
3860 declare
3861 Target_Typ : constant Entity_Id :=
3862 Etype
3863 (First_Index (Etype
3864 (Subtype_Mark (Subtype_Indication (N)))));
3865 begin
3866 R_Checks :=
3867 Get_Range_Checks
3868 (Scalar_Range (Etype (First_Index (Id))),
3869 Target_Typ,
3870 Etype (First_Index (Id)),
3871 Defining_Identifier (N));
3873 Insert_Range_Checks
3874 (R_Checks,
3876 Target_Typ,
3877 Sloc (Defining_Identifier (N)));
3878 end;
3879 end if;
3880 end if;
3881 end if;
3883 Set_Optimize_Alignment_Flags (Id);
3884 Check_Eliminated (Id);
3885 end Analyze_Subtype_Declaration;
3887 --------------------------------
3888 -- Analyze_Subtype_Indication --
3889 --------------------------------
3891 procedure Analyze_Subtype_Indication (N : Node_Id) is
3892 T : constant Entity_Id := Subtype_Mark (N);
3893 R : constant Node_Id := Range_Expression (Constraint (N));
3895 begin
3896 Analyze (T);
3898 if R /= Error then
3899 Analyze (R);
3900 Set_Etype (N, Etype (R));
3901 Resolve (R, Entity (T));
3902 else
3903 Set_Error_Posted (R);
3904 Set_Error_Posted (T);
3905 end if;
3906 end Analyze_Subtype_Indication;
3908 ------------------------------
3909 -- Analyze_Type_Declaration --
3910 ------------------------------
3912 procedure Analyze_Type_Declaration (N : Node_Id) is
3913 Def : constant Node_Id := Type_Definition (N);
3914 Def_Id : constant Entity_Id := Defining_Identifier (N);
3915 T : Entity_Id;
3916 Prev : Entity_Id;
3918 Is_Remote : constant Boolean :=
3919 (Is_Remote_Types (Current_Scope)
3920 or else Is_Remote_Call_Interface (Current_Scope))
3921 and then not (In_Private_Part (Current_Scope)
3922 or else In_Package_Body (Current_Scope));
3924 procedure Check_Ops_From_Incomplete_Type;
3925 -- If there is a tagged incomplete partial view of the type, transfer
3926 -- its operations to the full view, and indicate that the type of the
3927 -- controlling parameter (s) is this full view.
3929 ------------------------------------
3930 -- Check_Ops_From_Incomplete_Type --
3931 ------------------------------------
3933 procedure Check_Ops_From_Incomplete_Type is
3934 Elmt : Elmt_Id;
3935 Formal : Entity_Id;
3936 Op : Entity_Id;
3938 begin
3939 if Prev /= T
3940 and then Ekind (Prev) = E_Incomplete_Type
3941 and then Is_Tagged_Type (Prev)
3942 and then Is_Tagged_Type (T)
3943 then
3944 Elmt := First_Elmt (Primitive_Operations (Prev));
3945 while Present (Elmt) loop
3946 Op := Node (Elmt);
3947 Prepend_Elmt (Op, Primitive_Operations (T));
3949 Formal := First_Formal (Op);
3950 while Present (Formal) loop
3951 if Etype (Formal) = Prev then
3952 Set_Etype (Formal, T);
3953 end if;
3955 Next_Formal (Formal);
3956 end loop;
3958 if Etype (Op) = Prev then
3959 Set_Etype (Op, T);
3960 end if;
3962 Next_Elmt (Elmt);
3963 end loop;
3964 end if;
3965 end Check_Ops_From_Incomplete_Type;
3967 -- Start of processing for Analyze_Type_Declaration
3969 begin
3970 Prev := Find_Type_Name (N);
3972 -- The full view, if present, now points to the current type
3974 -- Ada 2005 (AI-50217): If the type was previously decorated when
3975 -- imported through a LIMITED WITH clause, it appears as incomplete
3976 -- but has no full view.
3977 -- If the incomplete view is tagged, a class_wide type has been
3978 -- created already. Use it for the full view as well, to prevent
3979 -- multiple incompatible class-wide types that may be created for
3980 -- self-referential anonymous access components.
3982 if Ekind (Prev) = E_Incomplete_Type
3983 and then Present (Full_View (Prev))
3984 then
3985 T := Full_View (Prev);
3987 if Is_Tagged_Type (Prev)
3988 and then Present (Class_Wide_Type (Prev))
3989 then
3990 Set_Ekind (T, Ekind (Prev)); -- will be reset later
3991 Set_Class_Wide_Type (T, Class_Wide_Type (Prev));
3992 Set_Etype (Class_Wide_Type (T), T);
3993 end if;
3995 else
3996 T := Prev;
3997 end if;
3999 Set_Is_Pure (T, Is_Pure (Current_Scope));
4001 -- We set the flag Is_First_Subtype here. It is needed to set the
4002 -- corresponding flag for the Implicit class-wide-type created
4003 -- during tagged types processing.
4005 Set_Is_First_Subtype (T, True);
4007 -- Only composite types other than array types are allowed to have
4008 -- discriminants.
4010 case Nkind (Def) is
4012 -- For derived types, the rule will be checked once we've figured
4013 -- out the parent type.
4015 when N_Derived_Type_Definition =>
4016 null;
4018 -- For record types, discriminants are allowed
4020 when N_Record_Definition =>
4021 null;
4023 when others =>
4024 if Present (Discriminant_Specifications (N)) then
4025 Error_Msg_N
4026 ("elementary or array type cannot have discriminants",
4027 Defining_Identifier
4028 (First (Discriminant_Specifications (N))));
4029 end if;
4030 end case;
4032 -- Elaborate the type definition according to kind, and generate
4033 -- subsidiary (implicit) subtypes where needed. We skip this if it was
4034 -- already done (this happens during the reanalysis that follows a call
4035 -- to the high level optimizer).
4037 if not Analyzed (T) then
4038 Set_Analyzed (T);
4040 case Nkind (Def) is
4042 when N_Access_To_Subprogram_Definition =>
4043 Access_Subprogram_Declaration (T, Def);
4045 -- If this is a remote access to subprogram, we must create the
4046 -- equivalent fat pointer type, and related subprograms.
4048 if Is_Remote then
4049 Process_Remote_AST_Declaration (N);
4050 end if;
4052 -- Validate categorization rule against access type declaration
4053 -- usually a violation in Pure unit, Shared_Passive unit.
4055 Validate_Access_Type_Declaration (T, N);
4057 when N_Access_To_Object_Definition =>
4058 Access_Type_Declaration (T, Def);
4060 -- Validate categorization rule against access type declaration
4061 -- usually a violation in Pure unit, Shared_Passive unit.
4063 Validate_Access_Type_Declaration (T, N);
4065 -- If we are in a Remote_Call_Interface package and define a
4066 -- RACW, then calling stubs and specific stream attributes
4067 -- must be added.
4069 if Is_Remote
4070 and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
4071 then
4072 Add_RACW_Features (Def_Id);
4073 end if;
4075 -- Set no strict aliasing flag if config pragma seen
4077 if Opt.No_Strict_Aliasing then
4078 Set_No_Strict_Aliasing (Base_Type (Def_Id));
4079 end if;
4081 when N_Array_Type_Definition =>
4082 Array_Type_Declaration (T, Def);
4084 when N_Derived_Type_Definition =>
4085 Derived_Type_Declaration (T, N, T /= Def_Id);
4087 when N_Enumeration_Type_Definition =>
4088 Enumeration_Type_Declaration (T, Def);
4090 when N_Floating_Point_Definition =>
4091 Floating_Point_Type_Declaration (T, Def);
4093 when N_Decimal_Fixed_Point_Definition =>
4094 Decimal_Fixed_Point_Type_Declaration (T, Def);
4096 when N_Ordinary_Fixed_Point_Definition =>
4097 Ordinary_Fixed_Point_Type_Declaration (T, Def);
4099 when N_Signed_Integer_Type_Definition =>
4100 Signed_Integer_Type_Declaration (T, Def);
4102 when N_Modular_Type_Definition =>
4103 Modular_Type_Declaration (T, Def);
4105 when N_Record_Definition =>
4106 Record_Type_Declaration (T, N, Prev);
4108 when others =>
4109 raise Program_Error;
4111 end case;
4112 end if;
4114 if Etype (T) = Any_Type then
4115 return;
4116 end if;
4118 -- Some common processing for all types
4120 Set_Depends_On_Private (T, Has_Private_Component (T));
4121 Check_Ops_From_Incomplete_Type;
4123 -- Both the declared entity, and its anonymous base type if one
4124 -- was created, need freeze nodes allocated.
4126 declare
4127 B : constant Entity_Id := Base_Type (T);
4129 begin
4130 -- In the case where the base type differs from the first subtype, we
4131 -- pre-allocate a freeze node, and set the proper link to the first
4132 -- subtype. Freeze_Entity will use this preallocated freeze node when
4133 -- it freezes the entity.
4135 -- This does not apply if the base type is a generic type, whose
4136 -- declaration is independent of the current derived definition.
4138 if B /= T and then not Is_Generic_Type (B) then
4139 Ensure_Freeze_Node (B);
4140 Set_First_Subtype_Link (Freeze_Node (B), T);
4141 end if;
4143 -- A type that is imported through a limited_with clause cannot
4144 -- generate any code, and thus need not be frozen. However, an access
4145 -- type with an imported designated type needs a finalization list,
4146 -- which may be referenced in some other package that has non-limited
4147 -- visibility on the designated type. Thus we must create the
4148 -- finalization list at the point the access type is frozen, to
4149 -- prevent unsatisfied references at link time.
4151 if not From_With_Type (T) or else Is_Access_Type (T) then
4152 Set_Has_Delayed_Freeze (T);
4153 end if;
4154 end;
4156 -- Case where T is the full declaration of some private type which has
4157 -- been swapped in Defining_Identifier (N).
4159 if T /= Def_Id and then Is_Private_Type (Def_Id) then
4160 Process_Full_View (N, T, Def_Id);
4162 -- Record the reference. The form of this is a little strange, since
4163 -- the full declaration has been swapped in. So the first parameter
4164 -- here represents the entity to which a reference is made which is
4165 -- the "real" entity, i.e. the one swapped in, and the second
4166 -- parameter provides the reference location.
4168 -- Also, we want to kill Has_Pragma_Unreferenced temporarily here
4169 -- since we don't want a complaint about the full type being an
4170 -- unwanted reference to the private type
4172 declare
4173 B : constant Boolean := Has_Pragma_Unreferenced (T);
4174 begin
4175 Set_Has_Pragma_Unreferenced (T, False);
4176 Generate_Reference (T, T, 'c');
4177 Set_Has_Pragma_Unreferenced (T, B);
4178 end;
4180 Set_Completion_Referenced (Def_Id);
4182 -- For completion of incomplete type, process incomplete dependents
4183 -- and always mark the full type as referenced (it is the incomplete
4184 -- type that we get for any real reference).
4186 elsif Ekind (Prev) = E_Incomplete_Type then
4187 Process_Incomplete_Dependents (N, T, Prev);
4188 Generate_Reference (Prev, Def_Id, 'c');
4189 Set_Completion_Referenced (Def_Id);
4191 -- If not private type or incomplete type completion, this is a real
4192 -- definition of a new entity, so record it.
4194 else
4195 Generate_Definition (Def_Id);
4196 end if;
4198 if Chars (Scope (Def_Id)) = Name_System
4199 and then Chars (Def_Id) = Name_Address
4200 and then Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (N)))
4201 then
4202 Set_Is_Descendent_Of_Address (Def_Id);
4203 Set_Is_Descendent_Of_Address (Base_Type (Def_Id));
4204 Set_Is_Descendent_Of_Address (Prev);
4205 end if;
4207 Set_Optimize_Alignment_Flags (Def_Id);
4208 Check_Eliminated (Def_Id);
4209 end Analyze_Type_Declaration;
4211 --------------------------
4212 -- Analyze_Variant_Part --
4213 --------------------------
4215 procedure Analyze_Variant_Part (N : Node_Id) is
4217 procedure Non_Static_Choice_Error (Choice : Node_Id);
4218 -- Error routine invoked by the generic instantiation below when the
4219 -- variant part has a non static choice.
4221 procedure Process_Declarations (Variant : Node_Id);
4222 -- Analyzes all the declarations associated with a Variant. Needed by
4223 -- the generic instantiation below.
4225 package Variant_Choices_Processing is new
4226 Generic_Choices_Processing
4227 (Get_Alternatives => Variants,
4228 Get_Choices => Discrete_Choices,
4229 Process_Empty_Choice => No_OP,
4230 Process_Non_Static_Choice => Non_Static_Choice_Error,
4231 Process_Associated_Node => Process_Declarations);
4232 use Variant_Choices_Processing;
4233 -- Instantiation of the generic choice processing package
4235 -----------------------------
4236 -- Non_Static_Choice_Error --
4237 -----------------------------
4239 procedure Non_Static_Choice_Error (Choice : Node_Id) is
4240 begin
4241 Flag_Non_Static_Expr
4242 ("choice given in variant part is not static!", Choice);
4243 end Non_Static_Choice_Error;
4245 --------------------------
4246 -- Process_Declarations --
4247 --------------------------
4249 procedure Process_Declarations (Variant : Node_Id) is
4250 begin
4251 if not Null_Present (Component_List (Variant)) then
4252 Analyze_Declarations (Component_Items (Component_List (Variant)));
4254 if Present (Variant_Part (Component_List (Variant))) then
4255 Analyze (Variant_Part (Component_List (Variant)));
4256 end if;
4257 end if;
4258 end Process_Declarations;
4260 -- Local Variables
4262 Discr_Name : Node_Id;
4263 Discr_Type : Entity_Id;
4265 Case_Table : Choice_Table_Type (1 .. Number_Of_Choices (N));
4266 Last_Choice : Nat;
4267 Dont_Care : Boolean;
4268 Others_Present : Boolean := False;
4270 pragma Warnings (Off, Case_Table);
4271 pragma Warnings (Off, Last_Choice);
4272 pragma Warnings (Off, Dont_Care);
4273 pragma Warnings (Off, Others_Present);
4274 -- We don't care about the assigned values of any of these
4276 -- Start of processing for Analyze_Variant_Part
4278 begin
4279 Discr_Name := Name (N);
4280 Analyze (Discr_Name);
4282 -- If Discr_Name bad, get out (prevent cascaded errors)
4284 if Etype (Discr_Name) = Any_Type then
4285 return;
4286 end if;
4288 -- Check invalid discriminant in variant part
4290 if Ekind (Entity (Discr_Name)) /= E_Discriminant then
4291 Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
4292 end if;
4294 Discr_Type := Etype (Entity (Discr_Name));
4296 if not Is_Discrete_Type (Discr_Type) then
4297 Error_Msg_N
4298 ("discriminant in a variant part must be of a discrete type",
4299 Name (N));
4300 return;
4301 end if;
4303 -- Call the instantiated Analyze_Choices which does the rest of the work
4305 Analyze_Choices
4306 (N, Discr_Type, Case_Table, Last_Choice, Dont_Care, Others_Present);
4307 end Analyze_Variant_Part;
4309 ----------------------------
4310 -- Array_Type_Declaration --
4311 ----------------------------
4313 procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
4314 Component_Def : constant Node_Id := Component_Definition (Def);
4315 Element_Type : Entity_Id;
4316 Implicit_Base : Entity_Id;
4317 Index : Node_Id;
4318 Related_Id : Entity_Id := Empty;
4319 Nb_Index : Nat;
4320 P : constant Node_Id := Parent (Def);
4321 Priv : Entity_Id;
4323 begin
4324 if Nkind (Def) = N_Constrained_Array_Definition then
4325 Index := First (Discrete_Subtype_Definitions (Def));
4326 else
4327 Index := First (Subtype_Marks (Def));
4328 end if;
4330 -- Find proper names for the implicit types which may be public. In case
4331 -- of anonymous arrays we use the name of the first object of that type
4332 -- as prefix.
4334 if No (T) then
4335 Related_Id := Defining_Identifier (P);
4336 else
4337 Related_Id := T;
4338 end if;
4340 Nb_Index := 1;
4341 while Present (Index) loop
4342 Analyze (Index);
4344 -- Add a subtype declaration for each index of private array type
4345 -- declaration whose etype is also private. For example:
4347 -- package Pkg is
4348 -- type Index is private;
4349 -- private
4350 -- type Table is array (Index) of ...
4351 -- end;
4353 -- This is currently required by the expander for the internally
4354 -- generated equality subprogram of records with variant parts in
4355 -- which the etype of some component is such private type.
4357 if Ekind (Current_Scope) = E_Package
4358 and then In_Private_Part (Current_Scope)
4359 and then Has_Private_Declaration (Etype (Index))
4360 then
4361 declare
4362 Loc : constant Source_Ptr := Sloc (Def);
4363 New_E : Entity_Id;
4364 Decl : Entity_Id;
4366 begin
4367 New_E := Make_Temporary (Loc, 'T');
4368 Set_Is_Internal (New_E);
4370 Decl :=
4371 Make_Subtype_Declaration (Loc,
4372 Defining_Identifier => New_E,
4373 Subtype_Indication =>
4374 New_Occurrence_Of (Etype (Index), Loc));
4376 Insert_Before (Parent (Def), Decl);
4377 Analyze (Decl);
4378 Set_Etype (Index, New_E);
4380 -- If the index is a range the Entity attribute is not
4381 -- available. Example:
4383 -- package Pkg is
4384 -- type T is private;
4385 -- private
4386 -- type T is new Natural;
4387 -- Table : array (T(1) .. T(10)) of Boolean;
4388 -- end Pkg;
4390 if Nkind (Index) /= N_Range then
4391 Set_Entity (Index, New_E);
4392 end if;
4393 end;
4394 end if;
4396 Make_Index (Index, P, Related_Id, Nb_Index);
4397 Next_Index (Index);
4398 Nb_Index := Nb_Index + 1;
4399 end loop;
4401 -- Process subtype indication if one is present
4403 if Present (Subtype_Indication (Component_Def)) then
4404 Element_Type :=
4405 Process_Subtype
4406 (Subtype_Indication (Component_Def), P, Related_Id, 'C');
4408 -- Ada 2005 (AI-230): Access Definition case
4410 else pragma Assert (Present (Access_Definition (Component_Def)));
4412 -- Indicate that the anonymous access type is created by the
4413 -- array type declaration.
4415 Element_Type := Access_Definition
4416 (Related_Nod => P,
4417 N => Access_Definition (Component_Def));
4418 Set_Is_Local_Anonymous_Access (Element_Type);
4420 -- Propagate the parent. This field is needed if we have to generate
4421 -- the master_id associated with an anonymous access to task type
4422 -- component (see Expand_N_Full_Type_Declaration.Build_Master)
4424 Set_Parent (Element_Type, Parent (T));
4426 -- Ada 2005 (AI-230): In case of components that are anonymous access
4427 -- types the level of accessibility depends on the enclosing type
4428 -- declaration
4430 Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
4432 -- Ada 2005 (AI-254)
4434 declare
4435 CD : constant Node_Id :=
4436 Access_To_Subprogram_Definition
4437 (Access_Definition (Component_Def));
4438 begin
4439 if Present (CD) and then Protected_Present (CD) then
4440 Element_Type :=
4441 Replace_Anonymous_Access_To_Protected_Subprogram (Def);
4442 end if;
4443 end;
4444 end if;
4446 -- Constrained array case
4448 if No (T) then
4449 T := Create_Itype (E_Void, P, Related_Id, 'T');
4450 end if;
4452 if Nkind (Def) = N_Constrained_Array_Definition then
4454 -- Establish Implicit_Base as unconstrained base type
4456 Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
4458 Set_Etype (Implicit_Base, Implicit_Base);
4459 Set_Scope (Implicit_Base, Current_Scope);
4460 Set_Has_Delayed_Freeze (Implicit_Base);
4462 -- The constrained array type is a subtype of the unconstrained one
4464 Set_Ekind (T, E_Array_Subtype);
4465 Init_Size_Align (T);
4466 Set_Etype (T, Implicit_Base);
4467 Set_Scope (T, Current_Scope);
4468 Set_Is_Constrained (T, True);
4469 Set_First_Index (T, First (Discrete_Subtype_Definitions (Def)));
4470 Set_Has_Delayed_Freeze (T);
4472 -- Complete setup of implicit base type
4474 Set_First_Index (Implicit_Base, First_Index (T));
4475 Set_Component_Type (Implicit_Base, Element_Type);
4476 Set_Has_Task (Implicit_Base, Has_Task (Element_Type));
4477 Set_Component_Size (Implicit_Base, Uint_0);
4478 Set_Packed_Array_Type (Implicit_Base, Empty);
4479 Set_Has_Controlled_Component
4480 (Implicit_Base, Has_Controlled_Component
4481 (Element_Type)
4482 or else Is_Controlled
4483 (Element_Type));
4484 Set_Finalize_Storage_Only
4485 (Implicit_Base, Finalize_Storage_Only
4486 (Element_Type));
4488 -- Unconstrained array case
4490 else
4491 Set_Ekind (T, E_Array_Type);
4492 Init_Size_Align (T);
4493 Set_Etype (T, T);
4494 Set_Scope (T, Current_Scope);
4495 Set_Component_Size (T, Uint_0);
4496 Set_Is_Constrained (T, False);
4497 Set_First_Index (T, First (Subtype_Marks (Def)));
4498 Set_Has_Delayed_Freeze (T, True);
4499 Set_Has_Task (T, Has_Task (Element_Type));
4500 Set_Has_Controlled_Component (T, Has_Controlled_Component
4501 (Element_Type)
4502 or else
4503 Is_Controlled (Element_Type));
4504 Set_Finalize_Storage_Only (T, Finalize_Storage_Only
4505 (Element_Type));
4506 end if;
4508 -- Common attributes for both cases
4510 Set_Component_Type (Base_Type (T), Element_Type);
4511 Set_Packed_Array_Type (T, Empty);
4513 if Aliased_Present (Component_Definition (Def)) then
4514 Set_Has_Aliased_Components (Etype (T));
4515 end if;
4517 -- Ada 2005 (AI-231): Propagate the null-excluding attribute to the
4518 -- array type to ensure that objects of this type are initialized.
4520 if Ada_Version >= Ada_05
4521 and then Can_Never_Be_Null (Element_Type)
4522 then
4523 Set_Can_Never_Be_Null (T);
4525 if Null_Exclusion_Present (Component_Definition (Def))
4527 -- No need to check itypes because in their case this check was
4528 -- done at their point of creation
4530 and then not Is_Itype (Element_Type)
4531 then
4532 Error_Msg_N
4533 ("`NOT NULL` not allowed (null already excluded)",
4534 Subtype_Indication (Component_Definition (Def)));
4535 end if;
4536 end if;
4538 Priv := Private_Component (Element_Type);
4540 if Present (Priv) then
4542 -- Check for circular definitions
4544 if Priv = Any_Type then
4545 Set_Component_Type (Etype (T), Any_Type);
4547 -- There is a gap in the visibility of operations on the composite
4548 -- type only if the component type is defined in a different scope.
4550 elsif Scope (Priv) = Current_Scope then
4551 null;
4553 elsif Is_Limited_Type (Priv) then
4554 Set_Is_Limited_Composite (Etype (T));
4555 Set_Is_Limited_Composite (T);
4556 else
4557 Set_Is_Private_Composite (Etype (T));
4558 Set_Is_Private_Composite (T);
4559 end if;
4560 end if;
4562 -- A syntax error in the declaration itself may lead to an empty index
4563 -- list, in which case do a minimal patch.
4565 if No (First_Index (T)) then
4566 Error_Msg_N ("missing index definition in array type declaration", T);
4568 declare
4569 Indices : constant List_Id :=
4570 New_List (New_Occurrence_Of (Any_Id, Sloc (T)));
4571 begin
4572 Set_Discrete_Subtype_Definitions (Def, Indices);
4573 Set_First_Index (T, First (Indices));
4574 return;
4575 end;
4576 end if;
4578 -- Create a concatenation operator for the new type. Internal array
4579 -- types created for packed entities do not need such, they are
4580 -- compatible with the user-defined type.
4582 if Number_Dimensions (T) = 1
4583 and then not Is_Packed_Array_Type (T)
4584 then
4585 New_Concatenation_Op (T);
4586 end if;
4588 -- In the case of an unconstrained array the parser has already verified
4589 -- that all the indices are unconstrained but we still need to make sure
4590 -- that the element type is constrained.
4592 if Is_Indefinite_Subtype (Element_Type) then
4593 Error_Msg_N
4594 ("unconstrained element type in array declaration",
4595 Subtype_Indication (Component_Def));
4597 elsif Is_Abstract_Type (Element_Type) then
4598 Error_Msg_N
4599 ("the type of a component cannot be abstract",
4600 Subtype_Indication (Component_Def));
4601 end if;
4602 end Array_Type_Declaration;
4604 ------------------------------------------------------
4605 -- Replace_Anonymous_Access_To_Protected_Subprogram --
4606 ------------------------------------------------------
4608 function Replace_Anonymous_Access_To_Protected_Subprogram
4609 (N : Node_Id) return Entity_Id
4611 Loc : constant Source_Ptr := Sloc (N);
4613 Curr_Scope : constant Scope_Stack_Entry :=
4614 Scope_Stack.Table (Scope_Stack.Last);
4616 Anon : constant Entity_Id := Make_Temporary (Loc, 'S');
4617 Acc : Node_Id;
4618 Comp : Node_Id;
4619 Decl : Node_Id;
4620 P : Node_Id;
4622 begin
4623 Set_Is_Internal (Anon);
4625 case Nkind (N) is
4626 when N_Component_Declaration |
4627 N_Unconstrained_Array_Definition |
4628 N_Constrained_Array_Definition =>
4629 Comp := Component_Definition (N);
4630 Acc := Access_Definition (Comp);
4632 when N_Discriminant_Specification =>
4633 Comp := Discriminant_Type (N);
4634 Acc := Comp;
4636 when N_Parameter_Specification =>
4637 Comp := Parameter_Type (N);
4638 Acc := Comp;
4640 when N_Access_Function_Definition =>
4641 Comp := Result_Definition (N);
4642 Acc := Comp;
4644 when N_Object_Declaration =>
4645 Comp := Object_Definition (N);
4646 Acc := Comp;
4648 when N_Function_Specification =>
4649 Comp := Result_Definition (N);
4650 Acc := Comp;
4652 when others =>
4653 raise Program_Error;
4654 end case;
4656 Decl := Make_Full_Type_Declaration (Loc,
4657 Defining_Identifier => Anon,
4658 Type_Definition =>
4659 Copy_Separate_Tree (Access_To_Subprogram_Definition (Acc)));
4661 Mark_Rewrite_Insertion (Decl);
4663 -- Insert the new declaration in the nearest enclosing scope. If the
4664 -- node is a body and N is its return type, the declaration belongs in
4665 -- the enclosing scope.
4667 P := Parent (N);
4669 if Nkind (P) = N_Subprogram_Body
4670 and then Nkind (N) = N_Function_Specification
4671 then
4672 P := Parent (P);
4673 end if;
4675 while Present (P) and then not Has_Declarations (P) loop
4676 P := Parent (P);
4677 end loop;
4679 pragma Assert (Present (P));
4681 if Nkind (P) = N_Package_Specification then
4682 Prepend (Decl, Visible_Declarations (P));
4683 else
4684 Prepend (Decl, Declarations (P));
4685 end if;
4687 -- Replace the anonymous type with an occurrence of the new declaration.
4688 -- In all cases the rewritten node does not have the null-exclusion
4689 -- attribute because (if present) it was already inherited by the
4690 -- anonymous entity (Anon). Thus, in case of components we do not
4691 -- inherit this attribute.
4693 if Nkind (N) = N_Parameter_Specification then
4694 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
4695 Set_Etype (Defining_Identifier (N), Anon);
4696 Set_Null_Exclusion_Present (N, False);
4698 elsif Nkind (N) = N_Object_Declaration then
4699 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
4700 Set_Etype (Defining_Identifier (N), Anon);
4702 elsif Nkind (N) = N_Access_Function_Definition then
4703 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
4705 elsif Nkind (N) = N_Function_Specification then
4706 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
4707 Set_Etype (Defining_Unit_Name (N), Anon);
4709 else
4710 Rewrite (Comp,
4711 Make_Component_Definition (Loc,
4712 Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
4713 end if;
4715 Mark_Rewrite_Insertion (Comp);
4717 if Nkind_In (N, N_Object_Declaration, N_Access_Function_Definition) then
4718 Analyze (Decl);
4720 else
4721 -- Temporarily remove the current scope (record or subprogram) from
4722 -- the stack to add the new declarations to the enclosing scope.
4724 Scope_Stack.Decrement_Last;
4725 Analyze (Decl);
4726 Set_Is_Itype (Anon);
4727 Scope_Stack.Append (Curr_Scope);
4728 end if;
4730 Set_Ekind (Anon, E_Anonymous_Access_Protected_Subprogram_Type);
4731 Set_Can_Use_Internal_Rep (Anon, not Always_Compatible_Rep_On_Target);
4732 return Anon;
4733 end Replace_Anonymous_Access_To_Protected_Subprogram;
4735 -------------------------------
4736 -- Build_Derived_Access_Type --
4737 -------------------------------
4739 procedure Build_Derived_Access_Type
4740 (N : Node_Id;
4741 Parent_Type : Entity_Id;
4742 Derived_Type : Entity_Id)
4744 S : constant Node_Id := Subtype_Indication (Type_Definition (N));
4746 Desig_Type : Entity_Id;
4747 Discr : Entity_Id;
4748 Discr_Con_Elist : Elist_Id;
4749 Discr_Con_El : Elmt_Id;
4750 Subt : Entity_Id;
4752 begin
4753 -- Set the designated type so it is available in case this is an access
4754 -- to a self-referential type, e.g. a standard list type with a next
4755 -- pointer. Will be reset after subtype is built.
4757 Set_Directly_Designated_Type
4758 (Derived_Type, Designated_Type (Parent_Type));
4760 Subt := Process_Subtype (S, N);
4762 if Nkind (S) /= N_Subtype_Indication
4763 and then Subt /= Base_Type (Subt)
4764 then
4765 Set_Ekind (Derived_Type, E_Access_Subtype);
4766 end if;
4768 if Ekind (Derived_Type) = E_Access_Subtype then
4769 declare
4770 Pbase : constant Entity_Id := Base_Type (Parent_Type);
4771 Ibase : constant Entity_Id :=
4772 Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
4773 Svg_Chars : constant Name_Id := Chars (Ibase);
4774 Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
4776 begin
4777 Copy_Node (Pbase, Ibase);
4779 Set_Chars (Ibase, Svg_Chars);
4780 Set_Next_Entity (Ibase, Svg_Next_E);
4781 Set_Sloc (Ibase, Sloc (Derived_Type));
4782 Set_Scope (Ibase, Scope (Derived_Type));
4783 Set_Freeze_Node (Ibase, Empty);
4784 Set_Is_Frozen (Ibase, False);
4785 Set_Comes_From_Source (Ibase, False);
4786 Set_Is_First_Subtype (Ibase, False);
4788 Set_Etype (Ibase, Pbase);
4789 Set_Etype (Derived_Type, Ibase);
4790 end;
4791 end if;
4793 Set_Directly_Designated_Type
4794 (Derived_Type, Designated_Type (Subt));
4796 Set_Is_Constrained (Derived_Type, Is_Constrained (Subt));
4797 Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
4798 Set_Size_Info (Derived_Type, Parent_Type);
4799 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
4800 Set_Depends_On_Private (Derived_Type,
4801 Has_Private_Component (Derived_Type));
4802 Conditional_Delay (Derived_Type, Subt);
4804 -- Ada 2005 (AI-231): Set the null-exclusion attribute, and verify
4805 -- that it is not redundant.
4807 if Null_Exclusion_Present (Type_Definition (N)) then
4808 Set_Can_Never_Be_Null (Derived_Type);
4810 if Can_Never_Be_Null (Parent_Type)
4811 and then False
4812 then
4813 Error_Msg_NE
4814 ("`NOT NULL` not allowed (& already excludes null)",
4815 N, Parent_Type);
4816 end if;
4818 elsif Can_Never_Be_Null (Parent_Type) then
4819 Set_Can_Never_Be_Null (Derived_Type);
4820 end if;
4822 -- Note: we do not copy the Storage_Size_Variable, since we always go to
4823 -- the root type for this information.
4825 -- Apply range checks to discriminants for derived record case
4826 -- ??? THIS CODE SHOULD NOT BE HERE REALLY.
4828 Desig_Type := Designated_Type (Derived_Type);
4829 if Is_Composite_Type (Desig_Type)
4830 and then (not Is_Array_Type (Desig_Type))
4831 and then Has_Discriminants (Desig_Type)
4832 and then Base_Type (Desig_Type) /= Desig_Type
4833 then
4834 Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
4835 Discr_Con_El := First_Elmt (Discr_Con_Elist);
4837 Discr := First_Discriminant (Base_Type (Desig_Type));
4838 while Present (Discr_Con_El) loop
4839 Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
4840 Next_Elmt (Discr_Con_El);
4841 Next_Discriminant (Discr);
4842 end loop;
4843 end if;
4844 end Build_Derived_Access_Type;
4846 ------------------------------
4847 -- Build_Derived_Array_Type --
4848 ------------------------------
4850 procedure Build_Derived_Array_Type
4851 (N : Node_Id;
4852 Parent_Type : Entity_Id;
4853 Derived_Type : Entity_Id)
4855 Loc : constant Source_Ptr := Sloc (N);
4856 Tdef : constant Node_Id := Type_Definition (N);
4857 Indic : constant Node_Id := Subtype_Indication (Tdef);
4858 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
4859 Implicit_Base : Entity_Id;
4860 New_Indic : Node_Id;
4862 procedure Make_Implicit_Base;
4863 -- If the parent subtype is constrained, the derived type is a subtype
4864 -- of an implicit base type derived from the parent base.
4866 ------------------------
4867 -- Make_Implicit_Base --
4868 ------------------------
4870 procedure Make_Implicit_Base is
4871 begin
4872 Implicit_Base :=
4873 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
4875 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
4876 Set_Etype (Implicit_Base, Parent_Base);
4878 Copy_Array_Subtype_Attributes (Implicit_Base, Parent_Base);
4879 Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
4881 Set_Has_Delayed_Freeze (Implicit_Base, True);
4882 end Make_Implicit_Base;
4884 -- Start of processing for Build_Derived_Array_Type
4886 begin
4887 if not Is_Constrained (Parent_Type) then
4888 if Nkind (Indic) /= N_Subtype_Indication then
4889 Set_Ekind (Derived_Type, E_Array_Type);
4891 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
4892 Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
4894 Set_Has_Delayed_Freeze (Derived_Type, True);
4896 else
4897 Make_Implicit_Base;
4898 Set_Etype (Derived_Type, Implicit_Base);
4900 New_Indic :=
4901 Make_Subtype_Declaration (Loc,
4902 Defining_Identifier => Derived_Type,
4903 Subtype_Indication =>
4904 Make_Subtype_Indication (Loc,
4905 Subtype_Mark => New_Reference_To (Implicit_Base, Loc),
4906 Constraint => Constraint (Indic)));
4908 Rewrite (N, New_Indic);
4909 Analyze (N);
4910 end if;
4912 else
4913 if Nkind (Indic) /= N_Subtype_Indication then
4914 Make_Implicit_Base;
4916 Set_Ekind (Derived_Type, Ekind (Parent_Type));
4917 Set_Etype (Derived_Type, Implicit_Base);
4918 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
4920 else
4921 Error_Msg_N ("illegal constraint on constrained type", Indic);
4922 end if;
4923 end if;
4925 -- If parent type is not a derived type itself, and is declared in
4926 -- closed scope (e.g. a subprogram), then we must explicitly introduce
4927 -- the new type's concatenation operator since Derive_Subprograms
4928 -- will not inherit the parent's operator. If the parent type is
4929 -- unconstrained, the operator is of the unconstrained base type.
4931 if Number_Dimensions (Parent_Type) = 1
4932 and then not Is_Limited_Type (Parent_Type)
4933 and then not Is_Derived_Type (Parent_Type)
4934 and then not Is_Package_Or_Generic_Package
4935 (Scope (Base_Type (Parent_Type)))
4936 then
4937 if not Is_Constrained (Parent_Type)
4938 and then Is_Constrained (Derived_Type)
4939 then
4940 New_Concatenation_Op (Implicit_Base);
4941 else
4942 New_Concatenation_Op (Derived_Type);
4943 end if;
4944 end if;
4945 end Build_Derived_Array_Type;
4947 -----------------------------------
4948 -- Build_Derived_Concurrent_Type --
4949 -----------------------------------
4951 procedure Build_Derived_Concurrent_Type
4952 (N : Node_Id;
4953 Parent_Type : Entity_Id;
4954 Derived_Type : Entity_Id)
4956 Loc : constant Source_Ptr := Sloc (N);
4958 Corr_Record : constant Entity_Id := Make_Temporary (Loc, 'C');
4959 Corr_Decl : Node_Id;
4960 Corr_Decl_Needed : Boolean;
4961 -- If the derived type has fewer discriminants than its parent, the
4962 -- corresponding record is also a derived type, in order to account for
4963 -- the bound discriminants. We create a full type declaration for it in
4964 -- this case.
4966 Constraint_Present : constant Boolean :=
4967 Nkind (Subtype_Indication (Type_Definition (N))) =
4968 N_Subtype_Indication;
4970 D_Constraint : Node_Id;
4971 New_Constraint : Elist_Id;
4972 Old_Disc : Entity_Id;
4973 New_Disc : Entity_Id;
4974 New_N : Node_Id;
4976 begin
4977 Set_Stored_Constraint (Derived_Type, No_Elist);
4978 Corr_Decl_Needed := False;
4979 Old_Disc := Empty;
4981 if Present (Discriminant_Specifications (N))
4982 and then Constraint_Present
4983 then
4984 Old_Disc := First_Discriminant (Parent_Type);
4985 New_Disc := First (Discriminant_Specifications (N));
4986 while Present (New_Disc) and then Present (Old_Disc) loop
4987 Next_Discriminant (Old_Disc);
4988 Next (New_Disc);
4989 end loop;
4990 end if;
4992 if Present (Old_Disc) then
4994 -- The new type has fewer discriminants, so we need to create a new
4995 -- corresponding record, which is derived from the corresponding
4996 -- record of the parent, and has a stored constraint that captures
4997 -- the values of the discriminant constraints.
4999 -- The type declaration for the derived corresponding record has
5000 -- the same discriminant part and constraints as the current
5001 -- declaration. Copy the unanalyzed tree to build declaration.
5003 Corr_Decl_Needed := True;
5004 New_N := Copy_Separate_Tree (N);
5006 Corr_Decl :=
5007 Make_Full_Type_Declaration (Loc,
5008 Defining_Identifier => Corr_Record,
5009 Discriminant_Specifications =>
5010 Discriminant_Specifications (New_N),
5011 Type_Definition =>
5012 Make_Derived_Type_Definition (Loc,
5013 Subtype_Indication =>
5014 Make_Subtype_Indication (Loc,
5015 Subtype_Mark =>
5016 New_Occurrence_Of
5017 (Corresponding_Record_Type (Parent_Type), Loc),
5018 Constraint =>
5019 Constraint
5020 (Subtype_Indication (Type_Definition (New_N))))));
5021 end if;
5023 -- Copy Storage_Size and Relative_Deadline variables if task case
5025 if Is_Task_Type (Parent_Type) then
5026 Set_Storage_Size_Variable (Derived_Type,
5027 Storage_Size_Variable (Parent_Type));
5028 Set_Relative_Deadline_Variable (Derived_Type,
5029 Relative_Deadline_Variable (Parent_Type));
5030 end if;
5032 if Present (Discriminant_Specifications (N)) then
5033 Push_Scope (Derived_Type);
5034 Check_Or_Process_Discriminants (N, Derived_Type);
5036 if Constraint_Present then
5037 New_Constraint :=
5038 Expand_To_Stored_Constraint
5039 (Parent_Type,
5040 Build_Discriminant_Constraints
5041 (Parent_Type,
5042 Subtype_Indication (Type_Definition (N)), True));
5043 end if;
5045 End_Scope;
5047 elsif Constraint_Present then
5049 -- Build constrained subtype and derive from it
5051 declare
5052 Loc : constant Source_Ptr := Sloc (N);
5053 Anon : constant Entity_Id :=
5054 Make_Defining_Identifier (Loc,
5055 New_External_Name (Chars (Derived_Type), 'T'));
5056 Decl : Node_Id;
5058 begin
5059 Decl :=
5060 Make_Subtype_Declaration (Loc,
5061 Defining_Identifier => Anon,
5062 Subtype_Indication =>
5063 Subtype_Indication (Type_Definition (N)));
5064 Insert_Before (N, Decl);
5065 Analyze (Decl);
5067 Rewrite (Subtype_Indication (Type_Definition (N)),
5068 New_Occurrence_Of (Anon, Loc));
5069 Set_Analyzed (Derived_Type, False);
5070 Analyze (N);
5071 return;
5072 end;
5073 end if;
5075 -- By default, operations and private data are inherited from parent.
5076 -- However, in the presence of bound discriminants, a new corresponding
5077 -- record will be created, see below.
5079 Set_Has_Discriminants
5080 (Derived_Type, Has_Discriminants (Parent_Type));
5081 Set_Corresponding_Record_Type
5082 (Derived_Type, Corresponding_Record_Type (Parent_Type));
5084 -- Is_Constrained is set according the parent subtype, but is set to
5085 -- False if the derived type is declared with new discriminants.
5087 Set_Is_Constrained
5088 (Derived_Type,
5089 (Is_Constrained (Parent_Type) or else Constraint_Present)
5090 and then not Present (Discriminant_Specifications (N)));
5092 if Constraint_Present then
5093 if not Has_Discriminants (Parent_Type) then
5094 Error_Msg_N ("untagged parent must have discriminants", N);
5096 elsif Present (Discriminant_Specifications (N)) then
5098 -- Verify that new discriminants are used to constrain old ones
5100 D_Constraint :=
5101 First
5102 (Constraints
5103 (Constraint (Subtype_Indication (Type_Definition (N)))));
5105 Old_Disc := First_Discriminant (Parent_Type);
5107 while Present (D_Constraint) loop
5108 if Nkind (D_Constraint) /= N_Discriminant_Association then
5110 -- Positional constraint. If it is a reference to a new
5111 -- discriminant, it constrains the corresponding old one.
5113 if Nkind (D_Constraint) = N_Identifier then
5114 New_Disc := First_Discriminant (Derived_Type);
5115 while Present (New_Disc) loop
5116 exit when Chars (New_Disc) = Chars (D_Constraint);
5117 Next_Discriminant (New_Disc);
5118 end loop;
5120 if Present (New_Disc) then
5121 Set_Corresponding_Discriminant (New_Disc, Old_Disc);
5122 end if;
5123 end if;
5125 Next_Discriminant (Old_Disc);
5127 -- if this is a named constraint, search by name for the old
5128 -- discriminants constrained by the new one.
5130 elsif Nkind (Expression (D_Constraint)) = N_Identifier then
5132 -- Find new discriminant with that name
5134 New_Disc := First_Discriminant (Derived_Type);
5135 while Present (New_Disc) loop
5136 exit when
5137 Chars (New_Disc) = Chars (Expression (D_Constraint));
5138 Next_Discriminant (New_Disc);
5139 end loop;
5141 if Present (New_Disc) then
5143 -- Verify that new discriminant renames some discriminant
5144 -- of the parent type, and associate the new discriminant
5145 -- with one or more old ones that it renames.
5147 declare
5148 Selector : Node_Id;
5150 begin
5151 Selector := First (Selector_Names (D_Constraint));
5152 while Present (Selector) loop
5153 Old_Disc := First_Discriminant (Parent_Type);
5154 while Present (Old_Disc) loop
5155 exit when Chars (Old_Disc) = Chars (Selector);
5156 Next_Discriminant (Old_Disc);
5157 end loop;
5159 if Present (Old_Disc) then
5160 Set_Corresponding_Discriminant
5161 (New_Disc, Old_Disc);
5162 end if;
5164 Next (Selector);
5165 end loop;
5166 end;
5167 end if;
5168 end if;
5170 Next (D_Constraint);
5171 end loop;
5173 New_Disc := First_Discriminant (Derived_Type);
5174 while Present (New_Disc) loop
5175 if No (Corresponding_Discriminant (New_Disc)) then
5176 Error_Msg_NE
5177 ("new discriminant& must constrain old one", N, New_Disc);
5179 elsif not
5180 Subtypes_Statically_Compatible
5181 (Etype (New_Disc),
5182 Etype (Corresponding_Discriminant (New_Disc)))
5183 then
5184 Error_Msg_NE
5185 ("& not statically compatible with parent discriminant",
5186 N, New_Disc);
5187 end if;
5189 Next_Discriminant (New_Disc);
5190 end loop;
5191 end if;
5193 elsif Present (Discriminant_Specifications (N)) then
5194 Error_Msg_N
5195 ("missing discriminant constraint in untagged derivation", N);
5196 end if;
5198 -- The entity chain of the derived type includes the new discriminants
5199 -- but shares operations with the parent.
5201 if Present (Discriminant_Specifications (N)) then
5202 Old_Disc := First_Discriminant (Parent_Type);
5203 while Present (Old_Disc) loop
5204 if No (Next_Entity (Old_Disc))
5205 or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
5206 then
5207 Set_Next_Entity
5208 (Last_Entity (Derived_Type), Next_Entity (Old_Disc));
5209 exit;
5210 end if;
5212 Next_Discriminant (Old_Disc);
5213 end loop;
5215 else
5216 Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
5217 if Has_Discriminants (Parent_Type) then
5218 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
5219 Set_Discriminant_Constraint (
5220 Derived_Type, Discriminant_Constraint (Parent_Type));
5221 end if;
5222 end if;
5224 Set_Last_Entity (Derived_Type, Last_Entity (Parent_Type));
5226 Set_Has_Completion (Derived_Type);
5228 if Corr_Decl_Needed then
5229 Set_Stored_Constraint (Derived_Type, New_Constraint);
5230 Insert_After (N, Corr_Decl);
5231 Analyze (Corr_Decl);
5232 Set_Corresponding_Record_Type (Derived_Type, Corr_Record);
5233 end if;
5234 end Build_Derived_Concurrent_Type;
5236 ------------------------------------
5237 -- Build_Derived_Enumeration_Type --
5238 ------------------------------------
5240 procedure Build_Derived_Enumeration_Type
5241 (N : Node_Id;
5242 Parent_Type : Entity_Id;
5243 Derived_Type : Entity_Id)
5245 Loc : constant Source_Ptr := Sloc (N);
5246 Def : constant Node_Id := Type_Definition (N);
5247 Indic : constant Node_Id := Subtype_Indication (Def);
5248 Implicit_Base : Entity_Id;
5249 Literal : Entity_Id;
5250 New_Lit : Entity_Id;
5251 Literals_List : List_Id;
5252 Type_Decl : Node_Id;
5253 Hi, Lo : Node_Id;
5254 Rang_Expr : Node_Id;
5256 begin
5257 -- Since types Standard.Character and Standard.[Wide_]Wide_Character do
5258 -- not have explicit literals lists we need to process types derived
5259 -- from them specially. This is handled by Derived_Standard_Character.
5260 -- If the parent type is a generic type, there are no literals either,
5261 -- and we construct the same skeletal representation as for the generic
5262 -- parent type.
5264 if Is_Standard_Character_Type (Parent_Type) then
5265 Derived_Standard_Character (N, Parent_Type, Derived_Type);
5267 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
5268 declare
5269 Lo : Node_Id;
5270 Hi : Node_Id;
5272 begin
5273 if Nkind (Indic) /= N_Subtype_Indication then
5274 Lo :=
5275 Make_Attribute_Reference (Loc,
5276 Attribute_Name => Name_First,
5277 Prefix => New_Reference_To (Derived_Type, Loc));
5278 Set_Etype (Lo, Derived_Type);
5280 Hi :=
5281 Make_Attribute_Reference (Loc,
5282 Attribute_Name => Name_Last,
5283 Prefix => New_Reference_To (Derived_Type, Loc));
5284 Set_Etype (Hi, Derived_Type);
5286 Set_Scalar_Range (Derived_Type,
5287 Make_Range (Loc,
5288 Low_Bound => Lo,
5289 High_Bound => Hi));
5290 else
5292 -- Analyze subtype indication and verify compatibility
5293 -- with parent type.
5295 if Base_Type (Process_Subtype (Indic, N)) /=
5296 Base_Type (Parent_Type)
5297 then
5298 Error_Msg_N
5299 ("illegal constraint for formal discrete type", N);
5300 end if;
5301 end if;
5302 end;
5304 else
5305 -- If a constraint is present, analyze the bounds to catch
5306 -- premature usage of the derived literals.
5308 if Nkind (Indic) = N_Subtype_Indication
5309 and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
5310 then
5311 Analyze (Low_Bound (Range_Expression (Constraint (Indic))));
5312 Analyze (High_Bound (Range_Expression (Constraint (Indic))));
5313 end if;
5315 -- Introduce an implicit base type for the derived type even if there
5316 -- is no constraint attached to it, since this seems closer to the
5317 -- Ada semantics. Build a full type declaration tree for the derived
5318 -- type using the implicit base type as the defining identifier. The
5319 -- build a subtype declaration tree which applies the constraint (if
5320 -- any) have it replace the derived type declaration.
5322 Literal := First_Literal (Parent_Type);
5323 Literals_List := New_List;
5324 while Present (Literal)
5325 and then Ekind (Literal) = E_Enumeration_Literal
5326 loop
5327 -- Literals of the derived type have the same representation as
5328 -- those of the parent type, but this representation can be
5329 -- overridden by an explicit representation clause. Indicate
5330 -- that there is no explicit representation given yet. These
5331 -- derived literals are implicit operations of the new type,
5332 -- and can be overridden by explicit ones.
5334 if Nkind (Literal) = N_Defining_Character_Literal then
5335 New_Lit :=
5336 Make_Defining_Character_Literal (Loc, Chars (Literal));
5337 else
5338 New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
5339 end if;
5341 Set_Ekind (New_Lit, E_Enumeration_Literal);
5342 Set_Enumeration_Pos (New_Lit, Enumeration_Pos (Literal));
5343 Set_Enumeration_Rep (New_Lit, Enumeration_Rep (Literal));
5344 Set_Enumeration_Rep_Expr (New_Lit, Empty);
5345 Set_Alias (New_Lit, Literal);
5346 Set_Is_Known_Valid (New_Lit, True);
5348 Append (New_Lit, Literals_List);
5349 Next_Literal (Literal);
5350 end loop;
5352 Implicit_Base :=
5353 Make_Defining_Identifier (Sloc (Derived_Type),
5354 New_External_Name (Chars (Derived_Type), 'B'));
5356 -- Indicate the proper nature of the derived type. This must be done
5357 -- before analysis of the literals, to recognize cases when a literal
5358 -- may be hidden by a previous explicit function definition (cf.
5359 -- c83031a).
5361 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
5362 Set_Etype (Derived_Type, Implicit_Base);
5364 Type_Decl :=
5365 Make_Full_Type_Declaration (Loc,
5366 Defining_Identifier => Implicit_Base,
5367 Discriminant_Specifications => No_List,
5368 Type_Definition =>
5369 Make_Enumeration_Type_Definition (Loc, Literals_List));
5371 Mark_Rewrite_Insertion (Type_Decl);
5372 Insert_Before (N, Type_Decl);
5373 Analyze (Type_Decl);
5375 -- After the implicit base is analyzed its Etype needs to be changed
5376 -- to reflect the fact that it is derived from the parent type which
5377 -- was ignored during analysis. We also set the size at this point.
5379 Set_Etype (Implicit_Base, Parent_Type);
5381 Set_Size_Info (Implicit_Base, Parent_Type);
5382 Set_RM_Size (Implicit_Base, RM_Size (Parent_Type));
5383 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
5385 Set_Has_Non_Standard_Rep
5386 (Implicit_Base, Has_Non_Standard_Rep
5387 (Parent_Type));
5388 Set_Has_Delayed_Freeze (Implicit_Base);
5390 -- Process the subtype indication including a validation check on the
5391 -- constraint, if any. If a constraint is given, its bounds must be
5392 -- implicitly converted to the new type.
5394 if Nkind (Indic) = N_Subtype_Indication then
5395 declare
5396 R : constant Node_Id :=
5397 Range_Expression (Constraint (Indic));
5399 begin
5400 if Nkind (R) = N_Range then
5401 Hi := Build_Scalar_Bound
5402 (High_Bound (R), Parent_Type, Implicit_Base);
5403 Lo := Build_Scalar_Bound
5404 (Low_Bound (R), Parent_Type, Implicit_Base);
5406 else
5407 -- Constraint is a Range attribute. Replace with explicit
5408 -- mention of the bounds of the prefix, which must be a
5409 -- subtype.
5411 Analyze (Prefix (R));
5412 Hi :=
5413 Convert_To (Implicit_Base,
5414 Make_Attribute_Reference (Loc,
5415 Attribute_Name => Name_Last,
5416 Prefix =>
5417 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
5419 Lo :=
5420 Convert_To (Implicit_Base,
5421 Make_Attribute_Reference (Loc,
5422 Attribute_Name => Name_First,
5423 Prefix =>
5424 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
5425 end if;
5426 end;
5428 else
5429 Hi :=
5430 Build_Scalar_Bound
5431 (Type_High_Bound (Parent_Type),
5432 Parent_Type, Implicit_Base);
5433 Lo :=
5434 Build_Scalar_Bound
5435 (Type_Low_Bound (Parent_Type),
5436 Parent_Type, Implicit_Base);
5437 end if;
5439 Rang_Expr :=
5440 Make_Range (Loc,
5441 Low_Bound => Lo,
5442 High_Bound => Hi);
5444 -- If we constructed a default range for the case where no range
5445 -- was given, then the expressions in the range must not freeze
5446 -- since they do not correspond to expressions in the source.
5448 if Nkind (Indic) /= N_Subtype_Indication then
5449 Set_Must_Not_Freeze (Lo);
5450 Set_Must_Not_Freeze (Hi);
5451 Set_Must_Not_Freeze (Rang_Expr);
5452 end if;
5454 Rewrite (N,
5455 Make_Subtype_Declaration (Loc,
5456 Defining_Identifier => Derived_Type,
5457 Subtype_Indication =>
5458 Make_Subtype_Indication (Loc,
5459 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
5460 Constraint =>
5461 Make_Range_Constraint (Loc,
5462 Range_Expression => Rang_Expr))));
5464 Analyze (N);
5466 -- If pragma Discard_Names applies on the first subtype of the parent
5467 -- type, then it must be applied on this subtype as well.
5469 if Einfo.Discard_Names (First_Subtype (Parent_Type)) then
5470 Set_Discard_Names (Derived_Type);
5471 end if;
5473 -- Apply a range check. Since this range expression doesn't have an
5474 -- Etype, we have to specifically pass the Source_Typ parameter. Is
5475 -- this right???
5477 if Nkind (Indic) = N_Subtype_Indication then
5478 Apply_Range_Check (Range_Expression (Constraint (Indic)),
5479 Parent_Type,
5480 Source_Typ => Entity (Subtype_Mark (Indic)));
5481 end if;
5482 end if;
5483 end Build_Derived_Enumeration_Type;
5485 --------------------------------
5486 -- Build_Derived_Numeric_Type --
5487 --------------------------------
5489 procedure Build_Derived_Numeric_Type
5490 (N : Node_Id;
5491 Parent_Type : Entity_Id;
5492 Derived_Type : Entity_Id)
5494 Loc : constant Source_Ptr := Sloc (N);
5495 Tdef : constant Node_Id := Type_Definition (N);
5496 Indic : constant Node_Id := Subtype_Indication (Tdef);
5497 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
5498 No_Constraint : constant Boolean := Nkind (Indic) /=
5499 N_Subtype_Indication;
5500 Implicit_Base : Entity_Id;
5502 Lo : Node_Id;
5503 Hi : Node_Id;
5505 begin
5506 -- Process the subtype indication including a validation check on
5507 -- the constraint if any.
5509 Discard_Node (Process_Subtype (Indic, N));
5511 -- Introduce an implicit base type for the derived type even if there
5512 -- is no constraint attached to it, since this seems closer to the Ada
5513 -- semantics.
5515 Implicit_Base :=
5516 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
5518 Set_Etype (Implicit_Base, Parent_Base);
5519 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
5520 Set_Size_Info (Implicit_Base, Parent_Base);
5521 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
5522 Set_Parent (Implicit_Base, Parent (Derived_Type));
5523 Set_Is_Known_Valid (Implicit_Base, Is_Known_Valid (Parent_Base));
5525 -- Set RM Size for discrete type or decimal fixed-point type
5526 -- Ordinary fixed-point is excluded, why???
5528 if Is_Discrete_Type (Parent_Base)
5529 or else Is_Decimal_Fixed_Point_Type (Parent_Base)
5530 then
5531 Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
5532 end if;
5534 Set_Has_Delayed_Freeze (Implicit_Base);
5536 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
5537 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
5539 Set_Scalar_Range (Implicit_Base,
5540 Make_Range (Loc,
5541 Low_Bound => Lo,
5542 High_Bound => Hi));
5544 if Has_Infinities (Parent_Base) then
5545 Set_Includes_Infinities (Scalar_Range (Implicit_Base));
5546 end if;
5548 -- The Derived_Type, which is the entity of the declaration, is a
5549 -- subtype of the implicit base. Its Ekind is a subtype, even in the
5550 -- absence of an explicit constraint.
5552 Set_Etype (Derived_Type, Implicit_Base);
5554 -- If we did not have a constraint, then the Ekind is set from the
5555 -- parent type (otherwise Process_Subtype has set the bounds)
5557 if No_Constraint then
5558 Set_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
5559 end if;
5561 -- If we did not have a range constraint, then set the range from the
5562 -- parent type. Otherwise, the call to Process_Subtype has set the
5563 -- bounds.
5565 if No_Constraint
5566 or else not Has_Range_Constraint (Indic)
5567 then
5568 Set_Scalar_Range (Derived_Type,
5569 Make_Range (Loc,
5570 Low_Bound => New_Copy_Tree (Type_Low_Bound (Parent_Type)),
5571 High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
5572 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
5574 if Has_Infinities (Parent_Type) then
5575 Set_Includes_Infinities (Scalar_Range (Derived_Type));
5576 end if;
5578 Set_Is_Known_Valid (Derived_Type, Is_Known_Valid (Parent_Type));
5579 end if;
5581 Set_Is_Descendent_Of_Address (Derived_Type,
5582 Is_Descendent_Of_Address (Parent_Type));
5583 Set_Is_Descendent_Of_Address (Implicit_Base,
5584 Is_Descendent_Of_Address (Parent_Type));
5586 -- Set remaining type-specific fields, depending on numeric type
5588 if Is_Modular_Integer_Type (Parent_Type) then
5589 Set_Modulus (Implicit_Base, Modulus (Parent_Base));
5591 Set_Non_Binary_Modulus
5592 (Implicit_Base, Non_Binary_Modulus (Parent_Base));
5594 Set_Is_Known_Valid
5595 (Implicit_Base, Is_Known_Valid (Parent_Base));
5597 elsif Is_Floating_Point_Type (Parent_Type) then
5599 -- Digits of base type is always copied from the digits value of
5600 -- the parent base type, but the digits of the derived type will
5601 -- already have been set if there was a constraint present.
5603 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
5604 Set_Vax_Float (Implicit_Base, Vax_Float (Parent_Base));
5606 if No_Constraint then
5607 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
5608 end if;
5610 elsif Is_Fixed_Point_Type (Parent_Type) then
5612 -- Small of base type and derived type are always copied from the
5613 -- parent base type, since smalls never change. The delta of the
5614 -- base type is also copied from the parent base type. However the
5615 -- delta of the derived type will have been set already if a
5616 -- constraint was present.
5618 Set_Small_Value (Derived_Type, Small_Value (Parent_Base));
5619 Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
5620 Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
5622 if No_Constraint then
5623 Set_Delta_Value (Derived_Type, Delta_Value (Parent_Type));
5624 end if;
5626 -- The scale and machine radix in the decimal case are always
5627 -- copied from the parent base type.
5629 if Is_Decimal_Fixed_Point_Type (Parent_Type) then
5630 Set_Scale_Value (Derived_Type, Scale_Value (Parent_Base));
5631 Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
5633 Set_Machine_Radix_10
5634 (Derived_Type, Machine_Radix_10 (Parent_Base));
5635 Set_Machine_Radix_10
5636 (Implicit_Base, Machine_Radix_10 (Parent_Base));
5638 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
5640 if No_Constraint then
5641 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
5643 else
5644 -- the analysis of the subtype_indication sets the
5645 -- digits value of the derived type.
5647 null;
5648 end if;
5649 end if;
5650 end if;
5652 -- The type of the bounds is that of the parent type, and they
5653 -- must be converted to the derived type.
5655 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
5657 -- The implicit_base should be frozen when the derived type is frozen,
5658 -- but note that it is used in the conversions of the bounds. For fixed
5659 -- types we delay the determination of the bounds until the proper
5660 -- freezing point. For other numeric types this is rejected by GCC, for
5661 -- reasons that are currently unclear (???), so we choose to freeze the
5662 -- implicit base now. In the case of integers and floating point types
5663 -- this is harmless because subsequent representation clauses cannot
5664 -- affect anything, but it is still baffling that we cannot use the
5665 -- same mechanism for all derived numeric types.
5667 -- There is a further complication: actually *some* representation
5668 -- clauses can affect the implicit base type. Namely, attribute
5669 -- definition clauses for stream-oriented attributes need to set the
5670 -- corresponding TSS entries on the base type, and this normally cannot
5671 -- be done after the base type is frozen, so the circuitry in
5672 -- Sem_Ch13.New_Stream_Subprogram must account for this possibility and
5673 -- not use Set_TSS in this case.
5675 if Is_Fixed_Point_Type (Parent_Type) then
5676 Conditional_Delay (Implicit_Base, Parent_Type);
5677 else
5678 Freeze_Before (N, Implicit_Base);
5679 end if;
5680 end Build_Derived_Numeric_Type;
5682 --------------------------------
5683 -- Build_Derived_Private_Type --
5684 --------------------------------
5686 procedure Build_Derived_Private_Type
5687 (N : Node_Id;
5688 Parent_Type : Entity_Id;
5689 Derived_Type : Entity_Id;
5690 Is_Completion : Boolean;
5691 Derive_Subps : Boolean := True)
5693 Loc : constant Source_Ptr := Sloc (N);
5694 Der_Base : Entity_Id;
5695 Discr : Entity_Id;
5696 Full_Decl : Node_Id := Empty;
5697 Full_Der : Entity_Id;
5698 Full_P : Entity_Id;
5699 Last_Discr : Entity_Id;
5700 Par_Scope : constant Entity_Id := Scope (Base_Type (Parent_Type));
5701 Swapped : Boolean := False;
5703 procedure Copy_And_Build;
5704 -- Copy derived type declaration, replace parent with its full view,
5705 -- and analyze new declaration.
5707 --------------------
5708 -- Copy_And_Build --
5709 --------------------
5711 procedure Copy_And_Build is
5712 Full_N : Node_Id;
5714 begin
5715 if Ekind (Parent_Type) in Record_Kind
5716 or else
5717 (Ekind (Parent_Type) in Enumeration_Kind
5718 and then not Is_Standard_Character_Type (Parent_Type)
5719 and then not Is_Generic_Type (Root_Type (Parent_Type)))
5720 then
5721 Full_N := New_Copy_Tree (N);
5722 Insert_After (N, Full_N);
5723 Build_Derived_Type (
5724 Full_N, Parent_Type, Full_Der, True, Derive_Subps => False);
5726 else
5727 Build_Derived_Type (
5728 N, Parent_Type, Full_Der, True, Derive_Subps => False);
5729 end if;
5730 end Copy_And_Build;
5732 -- Start of processing for Build_Derived_Private_Type
5734 begin
5735 if Is_Tagged_Type (Parent_Type) then
5736 Full_P := Full_View (Parent_Type);
5738 -- A type extension of a type with unknown discriminants is an
5739 -- indefinite type that the back-end cannot handle directly.
5740 -- We treat it as a private type, and build a completion that is
5741 -- derived from the full view of the parent, and hopefully has
5742 -- known discriminants.
5744 -- If the full view of the parent type has an underlying record view,
5745 -- use it to generate the underlying record view of this derived type
5746 -- (required for chains of derivations with unknown discriminants).
5748 -- Minor optimization: we avoid the generation of useless underlying
5749 -- record view entities if the private type declaration has unknown
5750 -- discriminants but its corresponding full view has no
5751 -- discriminants.
5753 if Has_Unknown_Discriminants (Parent_Type)
5754 and then Present (Full_P)
5755 and then (Has_Discriminants (Full_P)
5756 or else Present (Underlying_Record_View (Full_P)))
5757 and then not In_Open_Scopes (Par_Scope)
5758 and then Expander_Active
5759 then
5760 declare
5761 Full_Der : constant Entity_Id := Make_Temporary (Loc, 'T');
5762 New_Ext : constant Node_Id :=
5763 Copy_Separate_Tree
5764 (Record_Extension_Part (Type_Definition (N)));
5765 Decl : Node_Id;
5767 begin
5768 Build_Derived_Record_Type
5769 (N, Parent_Type, Derived_Type, Derive_Subps);
5771 -- Build anonymous completion, as a derivation from the full
5772 -- view of the parent. This is not a completion in the usual
5773 -- sense, because the current type is not private.
5775 Decl :=
5776 Make_Full_Type_Declaration (Loc,
5777 Defining_Identifier => Full_Der,
5778 Type_Definition =>
5779 Make_Derived_Type_Definition (Loc,
5780 Subtype_Indication =>
5781 New_Copy_Tree
5782 (Subtype_Indication (Type_Definition (N))),
5783 Record_Extension_Part => New_Ext));
5785 -- If the parent type has an underlying record view, use it
5786 -- here to build the new underlying record view.
5788 if Present (Underlying_Record_View (Full_P)) then
5789 pragma Assert
5790 (Nkind (Subtype_Indication (Type_Definition (Decl)))
5791 = N_Identifier);
5792 Set_Entity (Subtype_Indication (Type_Definition (Decl)),
5793 Underlying_Record_View (Full_P));
5794 end if;
5796 Install_Private_Declarations (Par_Scope);
5797 Install_Visible_Declarations (Par_Scope);
5798 Insert_Before (N, Decl);
5800 -- Mark entity as an underlying record view before analysis,
5801 -- to avoid generating the list of its primitive operations
5802 -- (which is not really required for this entity) and thus
5803 -- prevent spurious errors associated with missing overriding
5804 -- of abstract primitives (overridden only for Derived_Type).
5806 Set_Ekind (Full_Der, E_Record_Type);
5807 Set_Is_Underlying_Record_View (Full_Der);
5809 Analyze (Decl);
5811 pragma Assert (Has_Discriminants (Full_Der)
5812 and then not Has_Unknown_Discriminants (Full_Der));
5814 Uninstall_Declarations (Par_Scope);
5816 -- Freeze the underlying record view, to prevent generation of
5817 -- useless dispatching information, which is simply shared with
5818 -- the real derived type.
5820 Set_Is_Frozen (Full_Der);
5822 -- Set up links between real entity and underlying record view
5824 Set_Underlying_Record_View (Derived_Type, Base_Type (Full_Der));
5825 Set_Underlying_Record_View (Base_Type (Full_Der), Derived_Type);
5826 end;
5828 -- If discriminants are known, build derived record
5830 else
5831 Build_Derived_Record_Type
5832 (N, Parent_Type, Derived_Type, Derive_Subps);
5833 end if;
5835 return;
5837 elsif Has_Discriminants (Parent_Type) then
5838 if Present (Full_View (Parent_Type)) then
5839 if not Is_Completion then
5841 -- Copy declaration for subsequent analysis, to provide a
5842 -- completion for what is a private declaration. Indicate that
5843 -- the full type is internally generated.
5845 Full_Decl := New_Copy_Tree (N);
5846 Full_Der := New_Copy (Derived_Type);
5847 Set_Comes_From_Source (Full_Decl, False);
5848 Set_Comes_From_Source (Full_Der, False);
5850 Insert_After (N, Full_Decl);
5852 else
5853 -- If this is a completion, the full view being built is itself
5854 -- private. We build a subtype of the parent with the same
5855 -- constraints as this full view, to convey to the back end the
5856 -- constrained components and the size of this subtype. If the
5857 -- parent is constrained, its full view can serve as the
5858 -- underlying full view of the derived type.
5860 if No (Discriminant_Specifications (N)) then
5861 if Nkind (Subtype_Indication (Type_Definition (N))) =
5862 N_Subtype_Indication
5863 then
5864 Build_Underlying_Full_View (N, Derived_Type, Parent_Type);
5866 elsif Is_Constrained (Full_View (Parent_Type)) then
5867 Set_Underlying_Full_View
5868 (Derived_Type, Full_View (Parent_Type));
5869 end if;
5871 else
5872 -- If there are new discriminants, the parent subtype is
5873 -- constrained by them, but it is not clear how to build
5874 -- the Underlying_Full_View in this case???
5876 null;
5877 end if;
5878 end if;
5879 end if;
5881 -- Build partial view of derived type from partial view of parent
5883 Build_Derived_Record_Type
5884 (N, Parent_Type, Derived_Type, Derive_Subps);
5886 if Present (Full_View (Parent_Type)) and then not Is_Completion then
5887 if not In_Open_Scopes (Par_Scope)
5888 or else not In_Same_Source_Unit (N, Parent_Type)
5889 then
5890 -- Swap partial and full views temporarily
5892 Install_Private_Declarations (Par_Scope);
5893 Install_Visible_Declarations (Par_Scope);
5894 Swapped := True;
5895 end if;
5897 -- Build full view of derived type from full view of parent which
5898 -- is now installed. Subprograms have been derived on the partial
5899 -- view, the completion does not derive them anew.
5901 if not Is_Tagged_Type (Parent_Type) then
5903 -- If the parent is itself derived from another private type,
5904 -- installing the private declarations has not affected its
5905 -- privacy status, so use its own full view explicitly.
5907 if Is_Private_Type (Parent_Type) then
5908 Build_Derived_Record_Type
5909 (Full_Decl, Full_View (Parent_Type), Full_Der, False);
5910 else
5911 Build_Derived_Record_Type
5912 (Full_Decl, Parent_Type, Full_Der, False);
5913 end if;
5915 else
5916 -- If full view of parent is tagged, the completion inherits
5917 -- the proper primitive operations.
5919 Set_Defining_Identifier (Full_Decl, Full_Der);
5920 Build_Derived_Record_Type
5921 (Full_Decl, Parent_Type, Full_Der, Derive_Subps);
5922 Set_Analyzed (Full_Decl);
5923 end if;
5925 if Swapped then
5926 Uninstall_Declarations (Par_Scope);
5928 if In_Open_Scopes (Par_Scope) then
5929 Install_Visible_Declarations (Par_Scope);
5930 end if;
5931 end if;
5933 Der_Base := Base_Type (Derived_Type);
5934 Set_Full_View (Derived_Type, Full_Der);
5935 Set_Full_View (Der_Base, Base_Type (Full_Der));
5937 -- Copy the discriminant list from full view to the partial views
5938 -- (base type and its subtype). Gigi requires that the partial and
5939 -- full views have the same discriminants.
5941 -- Note that since the partial view is pointing to discriminants
5942 -- in the full view, their scope will be that of the full view.
5943 -- This might cause some front end problems and need adjustment???
5945 Discr := First_Discriminant (Base_Type (Full_Der));
5946 Set_First_Entity (Der_Base, Discr);
5948 loop
5949 Last_Discr := Discr;
5950 Next_Discriminant (Discr);
5951 exit when No (Discr);
5952 end loop;
5954 Set_Last_Entity (Der_Base, Last_Discr);
5956 Set_First_Entity (Derived_Type, First_Entity (Der_Base));
5957 Set_Last_Entity (Derived_Type, Last_Entity (Der_Base));
5958 Set_Stored_Constraint (Full_Der, Stored_Constraint (Derived_Type));
5960 else
5961 -- If this is a completion, the derived type stays private and
5962 -- there is no need to create a further full view, except in the
5963 -- unusual case when the derivation is nested within a child unit,
5964 -- see below.
5966 null;
5967 end if;
5969 elsif Present (Full_View (Parent_Type))
5970 and then Has_Discriminants (Full_View (Parent_Type))
5971 then
5972 if Has_Unknown_Discriminants (Parent_Type)
5973 and then Nkind (Subtype_Indication (Type_Definition (N))) =
5974 N_Subtype_Indication
5975 then
5976 Error_Msg_N
5977 ("cannot constrain type with unknown discriminants",
5978 Subtype_Indication (Type_Definition (N)));
5979 return;
5980 end if;
5982 -- If full view of parent is a record type, build full view as a
5983 -- derivation from the parent's full view. Partial view remains
5984 -- private. For code generation and linking, the full view must have
5985 -- the same public status as the partial one. This full view is only
5986 -- needed if the parent type is in an enclosing scope, so that the
5987 -- full view may actually become visible, e.g. in a child unit. This
5988 -- is both more efficient, and avoids order of freezing problems with
5989 -- the added entities.
5991 if not Is_Private_Type (Full_View (Parent_Type))
5992 and then (In_Open_Scopes (Scope (Parent_Type)))
5993 then
5994 Full_Der := Make_Defining_Identifier (Sloc (Derived_Type),
5995 Chars (Derived_Type));
5996 Set_Is_Itype (Full_Der);
5997 Set_Has_Private_Declaration (Full_Der);
5998 Set_Has_Private_Declaration (Derived_Type);
5999 Set_Associated_Node_For_Itype (Full_Der, N);
6000 Set_Parent (Full_Der, Parent (Derived_Type));
6001 Set_Full_View (Derived_Type, Full_Der);
6002 Set_Is_Public (Full_Der, Is_Public (Derived_Type));
6003 Full_P := Full_View (Parent_Type);
6004 Exchange_Declarations (Parent_Type);
6005 Copy_And_Build;
6006 Exchange_Declarations (Full_P);
6008 else
6009 Build_Derived_Record_Type
6010 (N, Full_View (Parent_Type), Derived_Type,
6011 Derive_Subps => False);
6012 end if;
6014 -- In any case, the primitive operations are inherited from the
6015 -- parent type, not from the internal full view.
6017 Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
6019 if Derive_Subps then
6020 Derive_Subprograms (Parent_Type, Derived_Type);
6021 end if;
6023 else
6024 -- Untagged type, No discriminants on either view
6026 if Nkind (Subtype_Indication (Type_Definition (N))) =
6027 N_Subtype_Indication
6028 then
6029 Error_Msg_N
6030 ("illegal constraint on type without discriminants", N);
6031 end if;
6033 if Present (Discriminant_Specifications (N))
6034 and then Present (Full_View (Parent_Type))
6035 and then not Is_Tagged_Type (Full_View (Parent_Type))
6036 then
6037 Error_Msg_N ("cannot add discriminants to untagged type", N);
6038 end if;
6040 Set_Stored_Constraint (Derived_Type, No_Elist);
6041 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
6042 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
6043 Set_Has_Controlled_Component
6044 (Derived_Type, Has_Controlled_Component
6045 (Parent_Type));
6047 -- Direct controlled types do not inherit Finalize_Storage_Only flag
6049 if not Is_Controlled (Parent_Type) then
6050 Set_Finalize_Storage_Only
6051 (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
6052 end if;
6054 -- Construct the implicit full view by deriving from full view of the
6055 -- parent type. In order to get proper visibility, we install the
6056 -- parent scope and its declarations.
6058 -- ??? If the parent is untagged private and its completion is
6059 -- tagged, this mechanism will not work because we cannot derive from
6060 -- the tagged full view unless we have an extension.
6062 if Present (Full_View (Parent_Type))
6063 and then not Is_Tagged_Type (Full_View (Parent_Type))
6064 and then not Is_Completion
6065 then
6066 Full_Der :=
6067 Make_Defining_Identifier (Sloc (Derived_Type),
6068 Chars => Chars (Derived_Type));
6069 Set_Is_Itype (Full_Der);
6070 Set_Has_Private_Declaration (Full_Der);
6071 Set_Has_Private_Declaration (Derived_Type);
6072 Set_Associated_Node_For_Itype (Full_Der, N);
6073 Set_Parent (Full_Der, Parent (Derived_Type));
6074 Set_Full_View (Derived_Type, Full_Der);
6076 if not In_Open_Scopes (Par_Scope) then
6077 Install_Private_Declarations (Par_Scope);
6078 Install_Visible_Declarations (Par_Scope);
6079 Copy_And_Build;
6080 Uninstall_Declarations (Par_Scope);
6082 -- If parent scope is open and in another unit, and parent has a
6083 -- completion, then the derivation is taking place in the visible
6084 -- part of a child unit. In that case retrieve the full view of
6085 -- the parent momentarily.
6087 elsif not In_Same_Source_Unit (N, Parent_Type) then
6088 Full_P := Full_View (Parent_Type);
6089 Exchange_Declarations (Parent_Type);
6090 Copy_And_Build;
6091 Exchange_Declarations (Full_P);
6093 -- Otherwise it is a local derivation
6095 else
6096 Copy_And_Build;
6097 end if;
6099 Set_Scope (Full_Der, Current_Scope);
6100 Set_Is_First_Subtype (Full_Der,
6101 Is_First_Subtype (Derived_Type));
6102 Set_Has_Size_Clause (Full_Der, False);
6103 Set_Has_Alignment_Clause (Full_Der, False);
6104 Set_Next_Entity (Full_Der, Empty);
6105 Set_Has_Delayed_Freeze (Full_Der);
6106 Set_Is_Frozen (Full_Der, False);
6107 Set_Freeze_Node (Full_Der, Empty);
6108 Set_Depends_On_Private (Full_Der,
6109 Has_Private_Component (Full_Der));
6110 Set_Public_Status (Full_Der);
6111 end if;
6112 end if;
6114 Set_Has_Unknown_Discriminants (Derived_Type,
6115 Has_Unknown_Discriminants (Parent_Type));
6117 if Is_Private_Type (Derived_Type) then
6118 Set_Private_Dependents (Derived_Type, New_Elmt_List);
6119 end if;
6121 if Is_Private_Type (Parent_Type)
6122 and then Base_Type (Parent_Type) = Parent_Type
6123 and then In_Open_Scopes (Scope (Parent_Type))
6124 then
6125 Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
6127 if Is_Child_Unit (Scope (Current_Scope))
6128 and then Is_Completion
6129 and then In_Private_Part (Current_Scope)
6130 and then Scope (Parent_Type) /= Current_Scope
6131 then
6132 -- This is the unusual case where a type completed by a private
6133 -- derivation occurs within a package nested in a child unit, and
6134 -- the parent is declared in an ancestor. In this case, the full
6135 -- view of the parent type will become visible in the body of
6136 -- the enclosing child, and only then will the current type be
6137 -- possibly non-private. We build a underlying full view that
6138 -- will be installed when the enclosing child body is compiled.
6140 Full_Der :=
6141 Make_Defining_Identifier (Sloc (Derived_Type),
6142 Chars => Chars (Derived_Type));
6143 Set_Is_Itype (Full_Der);
6144 Build_Itype_Reference (Full_Der, N);
6146 -- The full view will be used to swap entities on entry/exit to
6147 -- the body, and must appear in the entity list for the package.
6149 Append_Entity (Full_Der, Scope (Derived_Type));
6150 Set_Has_Private_Declaration (Full_Der);
6151 Set_Has_Private_Declaration (Derived_Type);
6152 Set_Associated_Node_For_Itype (Full_Der, N);
6153 Set_Parent (Full_Der, Parent (Derived_Type));
6154 Full_P := Full_View (Parent_Type);
6155 Exchange_Declarations (Parent_Type);
6156 Copy_And_Build;
6157 Exchange_Declarations (Full_P);
6158 Set_Underlying_Full_View (Derived_Type, Full_Der);
6159 end if;
6160 end if;
6161 end Build_Derived_Private_Type;
6163 -------------------------------
6164 -- Build_Derived_Record_Type --
6165 -------------------------------
6167 -- 1. INTRODUCTION
6169 -- Ideally we would like to use the same model of type derivation for
6170 -- tagged and untagged record types. Unfortunately this is not quite
6171 -- possible because the semantics of representation clauses is different
6172 -- for tagged and untagged records under inheritance. Consider the
6173 -- following:
6175 -- type R (...) is [tagged] record ... end record;
6176 -- type T (...) is new R (...) [with ...];
6178 -- The representation clauses for T can specify a completely different
6179 -- record layout from R's. Hence the same component can be placed in two
6180 -- very different positions in objects of type T and R. If R and T are
6181 -- tagged types, representation clauses for T can only specify the layout
6182 -- of non inherited components, thus components that are common in R and T
6183 -- have the same position in objects of type R and T.
6185 -- This has two implications. The first is that the entire tree for R's
6186 -- declaration needs to be copied for T in the untagged case, so that T
6187 -- can be viewed as a record type of its own with its own representation
6188 -- clauses. The second implication is the way we handle discriminants.
6189 -- Specifically, in the untagged case we need a way to communicate to Gigi
6190 -- what are the real discriminants in the record, while for the semantics
6191 -- we need to consider those introduced by the user to rename the
6192 -- discriminants in the parent type. This is handled by introducing the
6193 -- notion of stored discriminants. See below for more.
6195 -- Fortunately the way regular components are inherited can be handled in
6196 -- the same way in tagged and untagged types.
6198 -- To complicate things a bit more the private view of a private extension
6199 -- cannot be handled in the same way as the full view (for one thing the
6200 -- semantic rules are somewhat different). We will explain what differs
6201 -- below.
6203 -- 2. DISCRIMINANTS UNDER INHERITANCE
6205 -- The semantic rules governing the discriminants of derived types are
6206 -- quite subtle.
6208 -- type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
6209 -- [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
6211 -- If parent type has discriminants, then the discriminants that are
6212 -- declared in the derived type are [3.4 (11)]:
6214 -- o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
6215 -- there is one;
6217 -- o Otherwise, each discriminant of the parent type (implicitly declared
6218 -- in the same order with the same specifications). In this case, the
6219 -- discriminants are said to be "inherited", or if unknown in the parent
6220 -- are also unknown in the derived type.
6222 -- Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
6224 -- o The parent subtype shall be constrained;
6226 -- o If the parent type is not a tagged type, then each discriminant of
6227 -- the derived type shall be used in the constraint defining a parent
6228 -- subtype. [Implementation note: This ensures that the new discriminant
6229 -- can share storage with an existing discriminant.]
6231 -- For the derived type each discriminant of the parent type is either
6232 -- inherited, constrained to equal some new discriminant of the derived
6233 -- type, or constrained to the value of an expression.
6235 -- When inherited or constrained to equal some new discriminant, the
6236 -- parent discriminant and the discriminant of the derived type are said
6237 -- to "correspond".
6239 -- If a discriminant of the parent type is constrained to a specific value
6240 -- in the derived type definition, then the discriminant is said to be
6241 -- "specified" by that derived type definition.
6243 -- 3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
6245 -- We have spoken about stored discriminants in point 1 (introduction)
6246 -- above. There are two sort of stored discriminants: implicit and
6247 -- explicit. As long as the derived type inherits the same discriminants as
6248 -- the root record type, stored discriminants are the same as regular
6249 -- discriminants, and are said to be implicit. However, if any discriminant
6250 -- in the root type was renamed in the derived type, then the derived
6251 -- type will contain explicit stored discriminants. Explicit stored
6252 -- discriminants are discriminants in addition to the semantically visible
6253 -- discriminants defined for the derived type. Stored discriminants are
6254 -- used by Gigi to figure out what are the physical discriminants in
6255 -- objects of the derived type (see precise definition in einfo.ads).
6256 -- As an example, consider the following:
6258 -- type R (D1, D2, D3 : Int) is record ... end record;
6259 -- type T1 is new R;
6260 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
6261 -- type T3 is new T2;
6262 -- type T4 (Y : Int) is new T3 (Y, 99);
6264 -- The following table summarizes the discriminants and stored
6265 -- discriminants in R and T1 through T4.
6267 -- Type Discrim Stored Discrim Comment
6268 -- R (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in R
6269 -- T1 (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in T1
6270 -- T2 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T2
6271 -- T3 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T3
6272 -- T4 (Y) (D1, D2, D3) Girder discrims EXPLICIT in T4
6274 -- Field Corresponding_Discriminant (abbreviated CD below) allows us to
6275 -- find the corresponding discriminant in the parent type, while
6276 -- Original_Record_Component (abbreviated ORC below), the actual physical
6277 -- component that is renamed. Finally the field Is_Completely_Hidden
6278 -- (abbreviated ICH below) is set for all explicit stored discriminants
6279 -- (see einfo.ads for more info). For the above example this gives:
6281 -- Discrim CD ORC ICH
6282 -- ^^^^^^^ ^^ ^^^ ^^^
6283 -- D1 in R empty itself no
6284 -- D2 in R empty itself no
6285 -- D3 in R empty itself no
6287 -- D1 in T1 D1 in R itself no
6288 -- D2 in T1 D2 in R itself no
6289 -- D3 in T1 D3 in R itself no
6291 -- X1 in T2 D3 in T1 D3 in T2 no
6292 -- X2 in T2 D1 in T1 D1 in T2 no
6293 -- D1 in T2 empty itself yes
6294 -- D2 in T2 empty itself yes
6295 -- D3 in T2 empty itself yes
6297 -- X1 in T3 X1 in T2 D3 in T3 no
6298 -- X2 in T3 X2 in T2 D1 in T3 no
6299 -- D1 in T3 empty itself yes
6300 -- D2 in T3 empty itself yes
6301 -- D3 in T3 empty itself yes
6303 -- Y in T4 X1 in T3 D3 in T3 no
6304 -- D1 in T3 empty itself yes
6305 -- D2 in T3 empty itself yes
6306 -- D3 in T3 empty itself yes
6308 -- 4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
6310 -- Type derivation for tagged types is fairly straightforward. If no
6311 -- discriminants are specified by the derived type, these are inherited
6312 -- from the parent. No explicit stored discriminants are ever necessary.
6313 -- The only manipulation that is done to the tree is that of adding a
6314 -- _parent field with parent type and constrained to the same constraint
6315 -- specified for the parent in the derived type definition. For instance:
6317 -- type R (D1, D2, D3 : Int) is tagged record ... end record;
6318 -- type T1 is new R with null record;
6319 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
6321 -- are changed into:
6323 -- type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
6324 -- _parent : R (D1, D2, D3);
6325 -- end record;
6327 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
6328 -- _parent : T1 (X2, 88, X1);
6329 -- end record;
6331 -- The discriminants actually present in R, T1 and T2 as well as their CD,
6332 -- ORC and ICH fields are:
6334 -- Discrim CD ORC ICH
6335 -- ^^^^^^^ ^^ ^^^ ^^^
6336 -- D1 in R empty itself no
6337 -- D2 in R empty itself no
6338 -- D3 in R empty itself no
6340 -- D1 in T1 D1 in R D1 in R no
6341 -- D2 in T1 D2 in R D2 in R no
6342 -- D3 in T1 D3 in R D3 in R no
6344 -- X1 in T2 D3 in T1 D3 in R no
6345 -- X2 in T2 D1 in T1 D1 in R no
6347 -- 5. FIRST TRANSFORMATION FOR DERIVED RECORDS
6349 -- Regardless of whether we dealing with a tagged or untagged type
6350 -- we will transform all derived type declarations of the form
6352 -- type T is new R (...) [with ...];
6353 -- or
6354 -- subtype S is R (...);
6355 -- type T is new S [with ...];
6356 -- into
6357 -- type BT is new R [with ...];
6358 -- subtype T is BT (...);
6360 -- That is, the base derived type is constrained only if it has no
6361 -- discriminants. The reason for doing this is that GNAT's semantic model
6362 -- assumes that a base type with discriminants is unconstrained.
6364 -- Note that, strictly speaking, the above transformation is not always
6365 -- correct. Consider for instance the following excerpt from ACVC b34011a:
6367 -- procedure B34011A is
6368 -- type REC (D : integer := 0) is record
6369 -- I : Integer;
6370 -- end record;
6372 -- package P is
6373 -- type T6 is new Rec;
6374 -- function F return T6;
6375 -- end P;
6377 -- use P;
6378 -- package Q6 is
6379 -- type U is new T6 (Q6.F.I); -- ERROR: Q6.F.
6380 -- end Q6;
6382 -- The definition of Q6.U is illegal. However transforming Q6.U into
6384 -- type BaseU is new T6;
6385 -- subtype U is BaseU (Q6.F.I)
6387 -- turns U into a legal subtype, which is incorrect. To avoid this problem
6388 -- we always analyze the constraint (in this case (Q6.F.I)) before applying
6389 -- the transformation described above.
6391 -- There is another instance where the above transformation is incorrect.
6392 -- Consider:
6394 -- package Pack is
6395 -- type Base (D : Integer) is tagged null record;
6396 -- procedure P (X : Base);
6398 -- type Der is new Base (2) with null record;
6399 -- procedure P (X : Der);
6400 -- end Pack;
6402 -- Then the above transformation turns this into
6404 -- type Der_Base is new Base with null record;
6405 -- -- procedure P (X : Base) is implicitly inherited here
6406 -- -- as procedure P (X : Der_Base).
6408 -- subtype Der is Der_Base (2);
6409 -- procedure P (X : Der);
6410 -- -- The overriding of P (X : Der_Base) is illegal since we
6411 -- -- have a parameter conformance problem.
6413 -- To get around this problem, after having semantically processed Der_Base
6414 -- and the rewritten subtype declaration for Der, we copy Der_Base field
6415 -- Discriminant_Constraint from Der so that when parameter conformance is
6416 -- checked when P is overridden, no semantic errors are flagged.
6418 -- 6. SECOND TRANSFORMATION FOR DERIVED RECORDS
6420 -- Regardless of whether we are dealing with a tagged or untagged type
6421 -- we will transform all derived type declarations of the form
6423 -- type R (D1, .., Dn : ...) is [tagged] record ...;
6424 -- type T is new R [with ...];
6425 -- into
6426 -- type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
6428 -- The reason for such transformation is that it allows us to implement a
6429 -- very clean form of component inheritance as explained below.
6431 -- Note that this transformation is not achieved by direct tree rewriting
6432 -- and manipulation, but rather by redoing the semantic actions that the
6433 -- above transformation will entail. This is done directly in routine
6434 -- Inherit_Components.
6436 -- 7. TYPE DERIVATION AND COMPONENT INHERITANCE
6438 -- In both tagged and untagged derived types, regular non discriminant
6439 -- components are inherited in the derived type from the parent type. In
6440 -- the absence of discriminants component, inheritance is straightforward
6441 -- as components can simply be copied from the parent.
6443 -- If the parent has discriminants, inheriting components constrained with
6444 -- these discriminants requires caution. Consider the following example:
6446 -- type R (D1, D2 : Positive) is [tagged] record
6447 -- S : String (D1 .. D2);
6448 -- end record;
6450 -- type T1 is new R [with null record];
6451 -- type T2 (X : positive) is new R (1, X) [with null record];
6453 -- As explained in 6. above, T1 is rewritten as
6454 -- type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
6455 -- which makes the treatment for T1 and T2 identical.
6457 -- What we want when inheriting S, is that references to D1 and D2 in R are
6458 -- replaced with references to their correct constraints, i.e. D1 and D2 in
6459 -- T1 and 1 and X in T2. So all R's discriminant references are replaced
6460 -- with either discriminant references in the derived type or expressions.
6461 -- This replacement is achieved as follows: before inheriting R's
6462 -- components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
6463 -- created in the scope of T1 (resp. scope of T2) so that discriminants D1
6464 -- and D2 of T1 are visible (resp. discriminant X of T2 is visible).
6465 -- For T2, for instance, this has the effect of replacing String (D1 .. D2)
6466 -- by String (1 .. X).
6468 -- 8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
6470 -- We explain here the rules governing private type extensions relevant to
6471 -- type derivation. These rules are explained on the following example:
6473 -- type D [(...)] is new A [(...)] with private; <-- partial view
6474 -- type D [(...)] is new P [(...)] with null record; <-- full view
6476 -- Type A is called the ancestor subtype of the private extension.
6477 -- Type P is the parent type of the full view of the private extension. It
6478 -- must be A or a type derived from A.
6480 -- The rules concerning the discriminants of private type extensions are
6481 -- [7.3(10-13)]:
6483 -- o If a private extension inherits known discriminants from the ancestor
6484 -- subtype, then the full view shall also inherit its discriminants from
6485 -- the ancestor subtype and the parent subtype of the full view shall be
6486 -- constrained if and only if the ancestor subtype is constrained.
6488 -- o If a partial view has unknown discriminants, then the full view may
6489 -- define a definite or an indefinite subtype, with or without
6490 -- discriminants.
6492 -- o If a partial view has neither known nor unknown discriminants, then
6493 -- the full view shall define a definite subtype.
6495 -- o If the ancestor subtype of a private extension has constrained
6496 -- discriminants, then the parent subtype of the full view shall impose a
6497 -- statically matching constraint on those discriminants.
6499 -- This means that only the following forms of private extensions are
6500 -- allowed:
6502 -- type D is new A with private; <-- partial view
6503 -- type D is new P with null record; <-- full view
6505 -- If A has no discriminants than P has no discriminants, otherwise P must
6506 -- inherit A's discriminants.
6508 -- type D is new A (...) with private; <-- partial view
6509 -- type D is new P (:::) with null record; <-- full view
6511 -- P must inherit A's discriminants and (...) and (:::) must statically
6512 -- match.
6514 -- subtype A is R (...);
6515 -- type D is new A with private; <-- partial view
6516 -- type D is new P with null record; <-- full view
6518 -- P must have inherited R's discriminants and must be derived from A or
6519 -- any of its subtypes.
6521 -- type D (..) is new A with private; <-- partial view
6522 -- type D (..) is new P [(:::)] with null record; <-- full view
6524 -- No specific constraints on P's discriminants or constraint (:::).
6525 -- Note that A can be unconstrained, but the parent subtype P must either
6526 -- be constrained or (:::) must be present.
6528 -- type D (..) is new A [(...)] with private; <-- partial view
6529 -- type D (..) is new P [(:::)] with null record; <-- full view
6531 -- P's constraints on A's discriminants must statically match those
6532 -- imposed by (...).
6534 -- 9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
6536 -- The full view of a private extension is handled exactly as described
6537 -- above. The model chose for the private view of a private extension is
6538 -- the same for what concerns discriminants (i.e. they receive the same
6539 -- treatment as in the tagged case). However, the private view of the
6540 -- private extension always inherits the components of the parent base,
6541 -- without replacing any discriminant reference. Strictly speaking this is
6542 -- incorrect. However, Gigi never uses this view to generate code so this
6543 -- is a purely semantic issue. In theory, a set of transformations similar
6544 -- to those given in 5. and 6. above could be applied to private views of
6545 -- private extensions to have the same model of component inheritance as
6546 -- for non private extensions. However, this is not done because it would
6547 -- further complicate private type processing. Semantically speaking, this
6548 -- leaves us in an uncomfortable situation. As an example consider:
6550 -- package Pack is
6551 -- type R (D : integer) is tagged record
6552 -- S : String (1 .. D);
6553 -- end record;
6554 -- procedure P (X : R);
6555 -- type T is new R (1) with private;
6556 -- private
6557 -- type T is new R (1) with null record;
6558 -- end;
6560 -- This is transformed into:
6562 -- package Pack is
6563 -- type R (D : integer) is tagged record
6564 -- S : String (1 .. D);
6565 -- end record;
6566 -- procedure P (X : R);
6567 -- type T is new R (1) with private;
6568 -- private
6569 -- type BaseT is new R with null record;
6570 -- subtype T is BaseT (1);
6571 -- end;
6573 -- (strictly speaking the above is incorrect Ada)
6575 -- From the semantic standpoint the private view of private extension T
6576 -- should be flagged as constrained since one can clearly have
6578 -- Obj : T;
6580 -- in a unit withing Pack. However, when deriving subprograms for the
6581 -- private view of private extension T, T must be seen as unconstrained
6582 -- since T has discriminants (this is a constraint of the current
6583 -- subprogram derivation model). Thus, when processing the private view of
6584 -- a private extension such as T, we first mark T as unconstrained, we
6585 -- process it, we perform program derivation and just before returning from
6586 -- Build_Derived_Record_Type we mark T as constrained.
6588 -- ??? Are there are other uncomfortable cases that we will have to
6589 -- deal with.
6591 -- 10. RECORD_TYPE_WITH_PRIVATE complications
6593 -- Types that are derived from a visible record type and have a private
6594 -- extension present other peculiarities. They behave mostly like private
6595 -- types, but if they have primitive operations defined, these will not
6596 -- have the proper signatures for further inheritance, because other
6597 -- primitive operations will use the implicit base that we define for
6598 -- private derivations below. This affect subprogram inheritance (see
6599 -- Derive_Subprograms for details). We also derive the implicit base from
6600 -- the base type of the full view, so that the implicit base is a record
6601 -- type and not another private type, This avoids infinite loops.
6603 procedure Build_Derived_Record_Type
6604 (N : Node_Id;
6605 Parent_Type : Entity_Id;
6606 Derived_Type : Entity_Id;
6607 Derive_Subps : Boolean := True)
6609 Loc : constant Source_Ptr := Sloc (N);
6610 Parent_Base : Entity_Id;
6611 Type_Def : Node_Id;
6612 Indic : Node_Id;
6613 Discrim : Entity_Id;
6614 Last_Discrim : Entity_Id;
6615 Constrs : Elist_Id;
6617 Discs : Elist_Id := New_Elmt_List;
6618 -- An empty Discs list means that there were no constraints in the
6619 -- subtype indication or that there was an error processing it.
6621 Assoc_List : Elist_Id;
6622 New_Discrs : Elist_Id;
6623 New_Base : Entity_Id;
6624 New_Decl : Node_Id;
6625 New_Indic : Node_Id;
6627 Is_Tagged : constant Boolean := Is_Tagged_Type (Parent_Type);
6628 Discriminant_Specs : constant Boolean :=
6629 Present (Discriminant_Specifications (N));
6630 Private_Extension : constant Boolean :=
6631 Nkind (N) = N_Private_Extension_Declaration;
6633 Constraint_Present : Boolean;
6634 Inherit_Discrims : Boolean := False;
6635 Save_Etype : Entity_Id;
6636 Save_Discr_Constr : Elist_Id;
6637 Save_Next_Entity : Entity_Id;
6639 begin
6640 if Ekind (Parent_Type) = E_Record_Type_With_Private
6641 and then Present (Full_View (Parent_Type))
6642 and then Has_Discriminants (Parent_Type)
6643 then
6644 Parent_Base := Base_Type (Full_View (Parent_Type));
6645 else
6646 Parent_Base := Base_Type (Parent_Type);
6647 end if;
6649 -- Before we start the previously documented transformations, here is
6650 -- little fix for size and alignment of tagged types. Normally when we
6651 -- derive type D from type P, we copy the size and alignment of P as the
6652 -- default for D, and in the absence of explicit representation clauses
6653 -- for D, the size and alignment are indeed the same as the parent.
6655 -- But this is wrong for tagged types, since fields may be added, and
6656 -- the default size may need to be larger, and the default alignment may
6657 -- need to be larger.
6659 -- We therefore reset the size and alignment fields in the tagged case.
6660 -- Note that the size and alignment will in any case be at least as
6661 -- large as the parent type (since the derived type has a copy of the
6662 -- parent type in the _parent field)
6664 -- The type is also marked as being tagged here, which is needed when
6665 -- processing components with a self-referential anonymous access type
6666 -- in the call to Check_Anonymous_Access_Components below. Note that
6667 -- this flag is also set later on for completeness.
6669 if Is_Tagged then
6670 Set_Is_Tagged_Type (Derived_Type);
6671 Init_Size_Align (Derived_Type);
6672 end if;
6674 -- STEP 0a: figure out what kind of derived type declaration we have
6676 if Private_Extension then
6677 Type_Def := N;
6678 Set_Ekind (Derived_Type, E_Record_Type_With_Private);
6680 else
6681 Type_Def := Type_Definition (N);
6683 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
6684 -- Parent_Base can be a private type or private extension. However,
6685 -- for tagged types with an extension the newly added fields are
6686 -- visible and hence the Derived_Type is always an E_Record_Type.
6687 -- (except that the parent may have its own private fields).
6688 -- For untagged types we preserve the Ekind of the Parent_Base.
6690 if Present (Record_Extension_Part (Type_Def)) then
6691 Set_Ekind (Derived_Type, E_Record_Type);
6693 -- Create internal access types for components with anonymous
6694 -- access types.
6696 if Ada_Version >= Ada_05 then
6697 Check_Anonymous_Access_Components
6698 (N, Derived_Type, Derived_Type,
6699 Component_List (Record_Extension_Part (Type_Def)));
6700 end if;
6702 else
6703 Set_Ekind (Derived_Type, Ekind (Parent_Base));
6704 end if;
6705 end if;
6707 -- Indic can either be an N_Identifier if the subtype indication
6708 -- contains no constraint or an N_Subtype_Indication if the subtype
6709 -- indication has a constraint.
6711 Indic := Subtype_Indication (Type_Def);
6712 Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
6714 -- Check that the type has visible discriminants. The type may be
6715 -- a private type with unknown discriminants whose full view has
6716 -- discriminants which are invisible.
6718 if Constraint_Present then
6719 if not Has_Discriminants (Parent_Base)
6720 or else
6721 (Has_Unknown_Discriminants (Parent_Base)
6722 and then Is_Private_Type (Parent_Base))
6723 then
6724 Error_Msg_N
6725 ("invalid constraint: type has no discriminant",
6726 Constraint (Indic));
6728 Constraint_Present := False;
6729 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
6731 elsif Is_Constrained (Parent_Type) then
6732 Error_Msg_N
6733 ("invalid constraint: parent type is already constrained",
6734 Constraint (Indic));
6736 Constraint_Present := False;
6737 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
6738 end if;
6739 end if;
6741 -- STEP 0b: If needed, apply transformation given in point 5. above
6743 if not Private_Extension
6744 and then Has_Discriminants (Parent_Type)
6745 and then not Discriminant_Specs
6746 and then (Is_Constrained (Parent_Type) or else Constraint_Present)
6747 then
6748 -- First, we must analyze the constraint (see comment in point 5.)
6750 if Constraint_Present then
6751 New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
6753 if Has_Discriminants (Derived_Type)
6754 and then Has_Private_Declaration (Derived_Type)
6755 and then Present (Discriminant_Constraint (Derived_Type))
6756 then
6757 -- Verify that constraints of the full view statically match
6758 -- those given in the partial view.
6760 declare
6761 C1, C2 : Elmt_Id;
6763 begin
6764 C1 := First_Elmt (New_Discrs);
6765 C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
6766 while Present (C1) and then Present (C2) loop
6767 if Fully_Conformant_Expressions (Node (C1), Node (C2))
6768 or else
6769 (Is_OK_Static_Expression (Node (C1))
6770 and then
6771 Is_OK_Static_Expression (Node (C2))
6772 and then
6773 Expr_Value (Node (C1)) = Expr_Value (Node (C2)))
6774 then
6775 null;
6777 else
6778 Error_Msg_N (
6779 "constraint not conformant to previous declaration",
6780 Node (C1));
6781 end if;
6783 Next_Elmt (C1);
6784 Next_Elmt (C2);
6785 end loop;
6786 end;
6787 end if;
6788 end if;
6790 -- Insert and analyze the declaration for the unconstrained base type
6792 New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
6794 New_Decl :=
6795 Make_Full_Type_Declaration (Loc,
6796 Defining_Identifier => New_Base,
6797 Type_Definition =>
6798 Make_Derived_Type_Definition (Loc,
6799 Abstract_Present => Abstract_Present (Type_Def),
6800 Limited_Present => Limited_Present (Type_Def),
6801 Subtype_Indication =>
6802 New_Occurrence_Of (Parent_Base, Loc),
6803 Record_Extension_Part =>
6804 Relocate_Node (Record_Extension_Part (Type_Def)),
6805 Interface_List => Interface_List (Type_Def)));
6807 Set_Parent (New_Decl, Parent (N));
6808 Mark_Rewrite_Insertion (New_Decl);
6809 Insert_Before (N, New_Decl);
6811 -- In the extension case, make sure ancestor is frozen appropriately
6812 -- (see also non-discriminated case below).
6814 if Present (Record_Extension_Part (Type_Def))
6815 or else Is_Interface (Parent_Base)
6816 then
6817 Freeze_Before (New_Decl, Parent_Type);
6818 end if;
6820 -- Note that this call passes False for the Derive_Subps parameter
6821 -- because subprogram derivation is deferred until after creating
6822 -- the subtype (see below).
6824 Build_Derived_Type
6825 (New_Decl, Parent_Base, New_Base,
6826 Is_Completion => True, Derive_Subps => False);
6828 -- ??? This needs re-examination to determine whether the
6829 -- above call can simply be replaced by a call to Analyze.
6831 Set_Analyzed (New_Decl);
6833 -- Insert and analyze the declaration for the constrained subtype
6835 if Constraint_Present then
6836 New_Indic :=
6837 Make_Subtype_Indication (Loc,
6838 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
6839 Constraint => Relocate_Node (Constraint (Indic)));
6841 else
6842 declare
6843 Constr_List : constant List_Id := New_List;
6844 C : Elmt_Id;
6845 Expr : Node_Id;
6847 begin
6848 C := First_Elmt (Discriminant_Constraint (Parent_Type));
6849 while Present (C) loop
6850 Expr := Node (C);
6852 -- It is safe here to call New_Copy_Tree since
6853 -- Force_Evaluation was called on each constraint in
6854 -- Build_Discriminant_Constraints.
6856 Append (New_Copy_Tree (Expr), To => Constr_List);
6858 Next_Elmt (C);
6859 end loop;
6861 New_Indic :=
6862 Make_Subtype_Indication (Loc,
6863 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
6864 Constraint =>
6865 Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
6866 end;
6867 end if;
6869 Rewrite (N,
6870 Make_Subtype_Declaration (Loc,
6871 Defining_Identifier => Derived_Type,
6872 Subtype_Indication => New_Indic));
6874 Analyze (N);
6876 -- Derivation of subprograms must be delayed until the full subtype
6877 -- has been established to ensure proper overriding of subprograms
6878 -- inherited by full types. If the derivations occurred as part of
6879 -- the call to Build_Derived_Type above, then the check for type
6880 -- conformance would fail because earlier primitive subprograms
6881 -- could still refer to the full type prior the change to the new
6882 -- subtype and hence would not match the new base type created here.
6884 Derive_Subprograms (Parent_Type, Derived_Type);
6886 -- For tagged types the Discriminant_Constraint of the new base itype
6887 -- is inherited from the first subtype so that no subtype conformance
6888 -- problem arise when the first subtype overrides primitive
6889 -- operations inherited by the implicit base type.
6891 if Is_Tagged then
6892 Set_Discriminant_Constraint
6893 (New_Base, Discriminant_Constraint (Derived_Type));
6894 end if;
6896 return;
6897 end if;
6899 -- If we get here Derived_Type will have no discriminants or it will be
6900 -- a discriminated unconstrained base type.
6902 -- STEP 1a: perform preliminary actions/checks for derived tagged types
6904 if Is_Tagged then
6906 -- The parent type is frozen for non-private extensions (RM 13.14(7))
6907 -- The declaration of a specific descendant of an interface type
6908 -- freezes the interface type (RM 13.14).
6910 if not Private_Extension or else Is_Interface (Parent_Base) then
6911 Freeze_Before (N, Parent_Type);
6912 end if;
6914 -- In Ada 2005 (AI-344), the restriction that a derived tagged type
6915 -- cannot be declared at a deeper level than its parent type is
6916 -- removed. The check on derivation within a generic body is also
6917 -- relaxed, but there's a restriction that a derived tagged type
6918 -- cannot be declared in a generic body if it's derived directly
6919 -- or indirectly from a formal type of that generic.
6921 if Ada_Version >= Ada_05 then
6922 if Present (Enclosing_Generic_Body (Derived_Type)) then
6923 declare
6924 Ancestor_Type : Entity_Id;
6926 begin
6927 -- Check to see if any ancestor of the derived type is a
6928 -- formal type.
6930 Ancestor_Type := Parent_Type;
6931 while not Is_Generic_Type (Ancestor_Type)
6932 and then Etype (Ancestor_Type) /= Ancestor_Type
6933 loop
6934 Ancestor_Type := Etype (Ancestor_Type);
6935 end loop;
6937 -- If the derived type does have a formal type as an
6938 -- ancestor, then it's an error if the derived type is
6939 -- declared within the body of the generic unit that
6940 -- declares the formal type in its generic formal part. It's
6941 -- sufficient to check whether the ancestor type is declared
6942 -- inside the same generic body as the derived type (such as
6943 -- within a nested generic spec), in which case the
6944 -- derivation is legal. If the formal type is declared
6945 -- outside of that generic body, then it's guaranteed that
6946 -- the derived type is declared within the generic body of
6947 -- the generic unit declaring the formal type.
6949 if Is_Generic_Type (Ancestor_Type)
6950 and then Enclosing_Generic_Body (Ancestor_Type) /=
6951 Enclosing_Generic_Body (Derived_Type)
6952 then
6953 Error_Msg_NE
6954 ("parent type of& must not be descendant of formal type"
6955 & " of an enclosing generic body",
6956 Indic, Derived_Type);
6957 end if;
6958 end;
6959 end if;
6961 elsif Type_Access_Level (Derived_Type) /=
6962 Type_Access_Level (Parent_Type)
6963 and then not Is_Generic_Type (Derived_Type)
6964 then
6965 if Is_Controlled (Parent_Type) then
6966 Error_Msg_N
6967 ("controlled type must be declared at the library level",
6968 Indic);
6969 else
6970 Error_Msg_N
6971 ("type extension at deeper accessibility level than parent",
6972 Indic);
6973 end if;
6975 else
6976 declare
6977 GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
6979 begin
6980 if Present (GB)
6981 and then GB /= Enclosing_Generic_Body (Parent_Base)
6982 then
6983 Error_Msg_NE
6984 ("parent type of& must not be outside generic body"
6985 & " (RM 3.9.1(4))",
6986 Indic, Derived_Type);
6987 end if;
6988 end;
6989 end if;
6990 end if;
6992 -- Ada 2005 (AI-251)
6994 if Ada_Version >= Ada_05 and then Is_Tagged then
6996 -- "The declaration of a specific descendant of an interface type
6997 -- freezes the interface type" (RM 13.14).
6999 declare
7000 Iface : Node_Id;
7001 begin
7002 if Is_Non_Empty_List (Interface_List (Type_Def)) then
7003 Iface := First (Interface_List (Type_Def));
7004 while Present (Iface) loop
7005 Freeze_Before (N, Etype (Iface));
7006 Next (Iface);
7007 end loop;
7008 end if;
7009 end;
7010 end if;
7012 -- STEP 1b : preliminary cleanup of the full view of private types
7014 -- If the type is already marked as having discriminants, then it's the
7015 -- completion of a private type or private extension and we need to
7016 -- retain the discriminants from the partial view if the current
7017 -- declaration has Discriminant_Specifications so that we can verify
7018 -- conformance. However, we must remove any existing components that
7019 -- were inherited from the parent (and attached in Copy_And_Swap)
7020 -- because the full type inherits all appropriate components anyway, and
7021 -- we do not want the partial view's components interfering.
7023 if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
7024 Discrim := First_Discriminant (Derived_Type);
7025 loop
7026 Last_Discrim := Discrim;
7027 Next_Discriminant (Discrim);
7028 exit when No (Discrim);
7029 end loop;
7031 Set_Last_Entity (Derived_Type, Last_Discrim);
7033 -- In all other cases wipe out the list of inherited components (even
7034 -- inherited discriminants), it will be properly rebuilt here.
7036 else
7037 Set_First_Entity (Derived_Type, Empty);
7038 Set_Last_Entity (Derived_Type, Empty);
7039 end if;
7041 -- STEP 1c: Initialize some flags for the Derived_Type
7043 -- The following flags must be initialized here so that
7044 -- Process_Discriminants can check that discriminants of tagged types do
7045 -- not have a default initial value and that access discriminants are
7046 -- only specified for limited records. For completeness, these flags are
7047 -- also initialized along with all the other flags below.
7049 -- AI-419: Limitedness is not inherited from an interface parent, so to
7050 -- be limited in that case the type must be explicitly declared as
7051 -- limited. However, task and protected interfaces are always limited.
7053 if Limited_Present (Type_Def) then
7054 Set_Is_Limited_Record (Derived_Type);
7056 elsif Is_Limited_Record (Parent_Type)
7057 or else (Present (Full_View (Parent_Type))
7058 and then Is_Limited_Record (Full_View (Parent_Type)))
7059 then
7060 if not Is_Interface (Parent_Type)
7061 or else Is_Synchronized_Interface (Parent_Type)
7062 or else Is_Protected_Interface (Parent_Type)
7063 or else Is_Task_Interface (Parent_Type)
7064 then
7065 Set_Is_Limited_Record (Derived_Type);
7066 end if;
7067 end if;
7069 -- STEP 2a: process discriminants of derived type if any
7071 Push_Scope (Derived_Type);
7073 if Discriminant_Specs then
7074 Set_Has_Unknown_Discriminants (Derived_Type, False);
7076 -- The following call initializes fields Has_Discriminants and
7077 -- Discriminant_Constraint, unless we are processing the completion
7078 -- of a private type declaration.
7080 Check_Or_Process_Discriminants (N, Derived_Type);
7082 -- For non-tagged types the constraint on the Parent_Type must be
7083 -- present and is used to rename the discriminants.
7085 if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
7086 Error_Msg_N ("untagged parent must have discriminants", Indic);
7088 elsif not Is_Tagged and then not Constraint_Present then
7089 Error_Msg_N
7090 ("discriminant constraint needed for derived untagged records",
7091 Indic);
7093 -- Otherwise the parent subtype must be constrained unless we have a
7094 -- private extension.
7096 elsif not Constraint_Present
7097 and then not Private_Extension
7098 and then not Is_Constrained (Parent_Type)
7099 then
7100 Error_Msg_N
7101 ("unconstrained type not allowed in this context", Indic);
7103 elsif Constraint_Present then
7104 -- The following call sets the field Corresponding_Discriminant
7105 -- for the discriminants in the Derived_Type.
7107 Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
7109 -- For untagged types all new discriminants must rename
7110 -- discriminants in the parent. For private extensions new
7111 -- discriminants cannot rename old ones (implied by [7.3(13)]).
7113 Discrim := First_Discriminant (Derived_Type);
7114 while Present (Discrim) loop
7115 if not Is_Tagged
7116 and then No (Corresponding_Discriminant (Discrim))
7117 then
7118 Error_Msg_N
7119 ("new discriminants must constrain old ones", Discrim);
7121 elsif Private_Extension
7122 and then Present (Corresponding_Discriminant (Discrim))
7123 then
7124 Error_Msg_N
7125 ("only static constraints allowed for parent"
7126 & " discriminants in the partial view", Indic);
7127 exit;
7128 end if;
7130 -- If a new discriminant is used in the constraint, then its
7131 -- subtype must be statically compatible with the parent
7132 -- discriminant's subtype (3.7(15)).
7134 if Present (Corresponding_Discriminant (Discrim))
7135 and then
7136 not Subtypes_Statically_Compatible
7137 (Etype (Discrim),
7138 Etype (Corresponding_Discriminant (Discrim)))
7139 then
7140 Error_Msg_N
7141 ("subtype must be compatible with parent discriminant",
7142 Discrim);
7143 end if;
7145 Next_Discriminant (Discrim);
7146 end loop;
7148 -- Check whether the constraints of the full view statically
7149 -- match those imposed by the parent subtype [7.3(13)].
7151 if Present (Stored_Constraint (Derived_Type)) then
7152 declare
7153 C1, C2 : Elmt_Id;
7155 begin
7156 C1 := First_Elmt (Discs);
7157 C2 := First_Elmt (Stored_Constraint (Derived_Type));
7158 while Present (C1) and then Present (C2) loop
7159 if not
7160 Fully_Conformant_Expressions (Node (C1), Node (C2))
7161 then
7162 Error_Msg_N
7163 ("not conformant with previous declaration",
7164 Node (C1));
7165 end if;
7167 Next_Elmt (C1);
7168 Next_Elmt (C2);
7169 end loop;
7170 end;
7171 end if;
7172 end if;
7174 -- STEP 2b: No new discriminants, inherit discriminants if any
7176 else
7177 if Private_Extension then
7178 Set_Has_Unknown_Discriminants
7179 (Derived_Type,
7180 Has_Unknown_Discriminants (Parent_Type)
7181 or else Unknown_Discriminants_Present (N));
7183 -- The partial view of the parent may have unknown discriminants,
7184 -- but if the full view has discriminants and the parent type is
7185 -- in scope they must be inherited.
7187 elsif Has_Unknown_Discriminants (Parent_Type)
7188 and then
7189 (not Has_Discriminants (Parent_Type)
7190 or else not In_Open_Scopes (Scope (Parent_Type)))
7191 then
7192 Set_Has_Unknown_Discriminants (Derived_Type);
7193 end if;
7195 if not Has_Unknown_Discriminants (Derived_Type)
7196 and then not Has_Unknown_Discriminants (Parent_Base)
7197 and then Has_Discriminants (Parent_Type)
7198 then
7199 Inherit_Discrims := True;
7200 Set_Has_Discriminants
7201 (Derived_Type, True);
7202 Set_Discriminant_Constraint
7203 (Derived_Type, Discriminant_Constraint (Parent_Base));
7204 end if;
7206 -- The following test is true for private types (remember
7207 -- transformation 5. is not applied to those) and in an error
7208 -- situation.
7210 if Constraint_Present then
7211 Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
7212 end if;
7214 -- For now mark a new derived type as constrained only if it has no
7215 -- discriminants. At the end of Build_Derived_Record_Type we properly
7216 -- set this flag in the case of private extensions. See comments in
7217 -- point 9. just before body of Build_Derived_Record_Type.
7219 Set_Is_Constrained
7220 (Derived_Type,
7221 not (Inherit_Discrims
7222 or else Has_Unknown_Discriminants (Derived_Type)));
7223 end if;
7225 -- STEP 3: initialize fields of derived type
7227 Set_Is_Tagged_Type (Derived_Type, Is_Tagged);
7228 Set_Stored_Constraint (Derived_Type, No_Elist);
7230 -- Ada 2005 (AI-251): Private type-declarations can implement interfaces
7231 -- but cannot be interfaces
7233 if not Private_Extension
7234 and then Ekind (Derived_Type) /= E_Private_Type
7235 and then Ekind (Derived_Type) /= E_Limited_Private_Type
7236 then
7237 if Interface_Present (Type_Def) then
7238 Analyze_Interface_Declaration (Derived_Type, Type_Def);
7239 end if;
7241 Set_Interfaces (Derived_Type, No_Elist);
7242 end if;
7244 -- Fields inherited from the Parent_Type
7246 Set_Discard_Names
7247 (Derived_Type, Einfo.Discard_Names (Parent_Type));
7248 Set_Has_Specified_Layout
7249 (Derived_Type, Has_Specified_Layout (Parent_Type));
7250 Set_Is_Limited_Composite
7251 (Derived_Type, Is_Limited_Composite (Parent_Type));
7252 Set_Is_Private_Composite
7253 (Derived_Type, Is_Private_Composite (Parent_Type));
7255 -- Fields inherited from the Parent_Base
7257 Set_Has_Controlled_Component
7258 (Derived_Type, Has_Controlled_Component (Parent_Base));
7259 Set_Has_Non_Standard_Rep
7260 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
7261 Set_Has_Primitive_Operations
7262 (Derived_Type, Has_Primitive_Operations (Parent_Base));
7264 -- Fields inherited from the Parent_Base in the non-private case
7266 if Ekind (Derived_Type) = E_Record_Type then
7267 Set_Has_Complex_Representation
7268 (Derived_Type, Has_Complex_Representation (Parent_Base));
7269 end if;
7271 -- Fields inherited from the Parent_Base for record types
7273 if Is_Record_Type (Derived_Type) then
7275 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
7276 -- Parent_Base can be a private type or private extension.
7278 if Present (Full_View (Parent_Base)) then
7279 Set_OK_To_Reorder_Components
7280 (Derived_Type,
7281 OK_To_Reorder_Components (Full_View (Parent_Base)));
7282 Set_Reverse_Bit_Order
7283 (Derived_Type, Reverse_Bit_Order (Full_View (Parent_Base)));
7284 else
7285 Set_OK_To_Reorder_Components
7286 (Derived_Type, OK_To_Reorder_Components (Parent_Base));
7287 Set_Reverse_Bit_Order
7288 (Derived_Type, Reverse_Bit_Order (Parent_Base));
7289 end if;
7290 end if;
7292 -- Direct controlled types do not inherit Finalize_Storage_Only flag
7294 if not Is_Controlled (Parent_Type) then
7295 Set_Finalize_Storage_Only
7296 (Derived_Type, Finalize_Storage_Only (Parent_Type));
7297 end if;
7299 -- Set fields for private derived types
7301 if Is_Private_Type (Derived_Type) then
7302 Set_Depends_On_Private (Derived_Type, True);
7303 Set_Private_Dependents (Derived_Type, New_Elmt_List);
7305 -- Inherit fields from non private record types. If this is the
7306 -- completion of a derivation from a private type, the parent itself
7307 -- is private, and the attributes come from its full view, which must
7308 -- be present.
7310 else
7311 if Is_Private_Type (Parent_Base)
7312 and then not Is_Record_Type (Parent_Base)
7313 then
7314 Set_Component_Alignment
7315 (Derived_Type, Component_Alignment (Full_View (Parent_Base)));
7316 Set_C_Pass_By_Copy
7317 (Derived_Type, C_Pass_By_Copy (Full_View (Parent_Base)));
7318 else
7319 Set_Component_Alignment
7320 (Derived_Type, Component_Alignment (Parent_Base));
7321 Set_C_Pass_By_Copy
7322 (Derived_Type, C_Pass_By_Copy (Parent_Base));
7323 end if;
7324 end if;
7326 -- Set fields for tagged types
7328 if Is_Tagged then
7329 Set_Primitive_Operations (Derived_Type, New_Elmt_List);
7331 -- All tagged types defined in Ada.Finalization are controlled
7333 if Chars (Scope (Derived_Type)) = Name_Finalization
7334 and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
7335 and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
7336 then
7337 Set_Is_Controlled (Derived_Type);
7338 else
7339 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Base));
7340 end if;
7342 -- Minor optimization: there is no need to generate the class-wide
7343 -- entity associated with an underlying record view.
7345 if not Is_Underlying_Record_View (Derived_Type) then
7346 Make_Class_Wide_Type (Derived_Type);
7347 end if;
7349 Set_Is_Abstract_Type (Derived_Type, Abstract_Present (Type_Def));
7351 if Has_Discriminants (Derived_Type)
7352 and then Constraint_Present
7353 then
7354 Set_Stored_Constraint
7355 (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
7356 end if;
7358 if Ada_Version >= Ada_05 then
7359 declare
7360 Ifaces_List : Elist_Id;
7362 begin
7363 -- Checks rules 3.9.4 (13/2 and 14/2)
7365 if Comes_From_Source (Derived_Type)
7366 and then not Is_Private_Type (Derived_Type)
7367 and then Is_Interface (Parent_Type)
7368 and then not Is_Interface (Derived_Type)
7369 then
7370 if Is_Task_Interface (Parent_Type) then
7371 Error_Msg_N
7372 ("(Ada 2005) task type required (RM 3.9.4 (13.2))",
7373 Derived_Type);
7375 elsif Is_Protected_Interface (Parent_Type) then
7376 Error_Msg_N
7377 ("(Ada 2005) protected type required (RM 3.9.4 (14.2))",
7378 Derived_Type);
7379 end if;
7380 end if;
7382 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
7384 Check_Interfaces (N, Type_Def);
7386 -- Ada 2005 (AI-251): Collect the list of progenitors that are
7387 -- not already in the parents.
7389 Collect_Interfaces
7390 (T => Derived_Type,
7391 Ifaces_List => Ifaces_List,
7392 Exclude_Parents => True);
7394 Set_Interfaces (Derived_Type, Ifaces_List);
7396 -- If the derived type is the anonymous type created for
7397 -- a declaration whose parent has a constraint, propagate
7398 -- the interface list to the source type. This must be done
7399 -- prior to the completion of the analysis of the source type
7400 -- because the components in the extension may contain current
7401 -- instances whose legality depends on some ancestor.
7403 if Is_Itype (Derived_Type) then
7404 declare
7405 Def : constant Node_Id :=
7406 Associated_Node_For_Itype (Derived_Type);
7407 begin
7408 if Present (Def)
7409 and then Nkind (Def) = N_Full_Type_Declaration
7410 then
7411 Set_Interfaces
7412 (Defining_Identifier (Def), Ifaces_List);
7413 end if;
7414 end;
7415 end if;
7416 end;
7417 end if;
7419 else
7420 Set_Is_Packed (Derived_Type, Is_Packed (Parent_Base));
7421 Set_Has_Non_Standard_Rep
7422 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
7423 end if;
7425 -- STEP 4: Inherit components from the parent base and constrain them.
7426 -- Apply the second transformation described in point 6. above.
7428 if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
7429 or else not Has_Discriminants (Parent_Type)
7430 or else not Is_Constrained (Parent_Type)
7431 then
7432 Constrs := Discs;
7433 else
7434 Constrs := Discriminant_Constraint (Parent_Type);
7435 end if;
7437 Assoc_List :=
7438 Inherit_Components
7439 (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
7441 -- STEP 5a: Copy the parent record declaration for untagged types
7443 if not Is_Tagged then
7445 -- Discriminant_Constraint (Derived_Type) has been properly
7446 -- constructed. Save it and temporarily set it to Empty because we
7447 -- do not want the call to New_Copy_Tree below to mess this list.
7449 if Has_Discriminants (Derived_Type) then
7450 Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
7451 Set_Discriminant_Constraint (Derived_Type, No_Elist);
7452 else
7453 Save_Discr_Constr := No_Elist;
7454 end if;
7456 -- Save the Etype field of Derived_Type. It is correctly set now,
7457 -- but the call to New_Copy tree may remap it to point to itself,
7458 -- which is not what we want. Ditto for the Next_Entity field.
7460 Save_Etype := Etype (Derived_Type);
7461 Save_Next_Entity := Next_Entity (Derived_Type);
7463 -- Assoc_List maps all stored discriminants in the Parent_Base to
7464 -- stored discriminants in the Derived_Type. It is fundamental that
7465 -- no types or itypes with discriminants other than the stored
7466 -- discriminants appear in the entities declared inside
7467 -- Derived_Type, since the back end cannot deal with it.
7469 New_Decl :=
7470 New_Copy_Tree
7471 (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
7473 -- Restore the fields saved prior to the New_Copy_Tree call
7474 -- and compute the stored constraint.
7476 Set_Etype (Derived_Type, Save_Etype);
7477 Set_Next_Entity (Derived_Type, Save_Next_Entity);
7479 if Has_Discriminants (Derived_Type) then
7480 Set_Discriminant_Constraint
7481 (Derived_Type, Save_Discr_Constr);
7482 Set_Stored_Constraint
7483 (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
7484 Replace_Components (Derived_Type, New_Decl);
7485 end if;
7487 -- Insert the new derived type declaration
7489 Rewrite (N, New_Decl);
7491 -- STEP 5b: Complete the processing for record extensions in generics
7493 -- There is no completion for record extensions declared in the
7494 -- parameter part of a generic, so we need to complete processing for
7495 -- these generic record extensions here. The Record_Type_Definition call
7496 -- will change the Ekind of the components from E_Void to E_Component.
7498 elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
7499 Record_Type_Definition (Empty, Derived_Type);
7501 -- STEP 5c: Process the record extension for non private tagged types
7503 elsif not Private_Extension then
7505 -- Add the _parent field in the derived type
7507 Expand_Record_Extension (Derived_Type, Type_Def);
7509 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
7510 -- implemented interfaces if we are in expansion mode
7512 if Expander_Active
7513 and then Has_Interfaces (Derived_Type)
7514 then
7515 Add_Interface_Tag_Components (N, Derived_Type);
7516 end if;
7518 -- Analyze the record extension
7520 Record_Type_Definition
7521 (Record_Extension_Part (Type_Def), Derived_Type);
7522 end if;
7524 End_Scope;
7526 -- Nothing else to do if there is an error in the derivation.
7527 -- An unusual case: the full view may be derived from a type in an
7528 -- instance, when the partial view was used illegally as an actual
7529 -- in that instance, leading to a circular definition.
7531 if Etype (Derived_Type) = Any_Type
7532 or else Etype (Parent_Type) = Derived_Type
7533 then
7534 return;
7535 end if;
7537 -- Set delayed freeze and then derive subprograms, we need to do
7538 -- this in this order so that derived subprograms inherit the
7539 -- derived freeze if necessary.
7541 Set_Has_Delayed_Freeze (Derived_Type);
7543 if Derive_Subps then
7544 Derive_Subprograms (Parent_Type, Derived_Type);
7545 end if;
7547 -- If we have a private extension which defines a constrained derived
7548 -- type mark as constrained here after we have derived subprograms. See
7549 -- comment on point 9. just above the body of Build_Derived_Record_Type.
7551 if Private_Extension and then Inherit_Discrims then
7552 if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
7553 Set_Is_Constrained (Derived_Type, True);
7554 Set_Discriminant_Constraint (Derived_Type, Discs);
7556 elsif Is_Constrained (Parent_Type) then
7557 Set_Is_Constrained
7558 (Derived_Type, True);
7559 Set_Discriminant_Constraint
7560 (Derived_Type, Discriminant_Constraint (Parent_Type));
7561 end if;
7562 end if;
7564 -- Update the class-wide type, which shares the now-completed entity
7565 -- list with its specific type. In case of underlying record views,
7566 -- we do not generate the corresponding class wide entity.
7568 if Is_Tagged
7569 and then not Is_Underlying_Record_View (Derived_Type)
7570 then
7571 Set_First_Entity
7572 (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
7573 Set_Last_Entity
7574 (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
7575 end if;
7577 -- Update the scope of anonymous access types of discriminants and other
7578 -- components, to prevent scope anomalies in gigi, when the derivation
7579 -- appears in a scope nested within that of the parent.
7581 declare
7582 D : Entity_Id;
7584 begin
7585 D := First_Entity (Derived_Type);
7586 while Present (D) loop
7587 if Ekind_In (D, E_Discriminant, E_Component) then
7588 if Is_Itype (Etype (D))
7589 and then Ekind (Etype (D)) = E_Anonymous_Access_Type
7590 then
7591 Set_Scope (Etype (D), Current_Scope);
7592 end if;
7593 end if;
7595 Next_Entity (D);
7596 end loop;
7597 end;
7598 end Build_Derived_Record_Type;
7600 ------------------------
7601 -- Build_Derived_Type --
7602 ------------------------
7604 procedure Build_Derived_Type
7605 (N : Node_Id;
7606 Parent_Type : Entity_Id;
7607 Derived_Type : Entity_Id;
7608 Is_Completion : Boolean;
7609 Derive_Subps : Boolean := True)
7611 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
7613 begin
7614 -- Set common attributes
7616 Set_Scope (Derived_Type, Current_Scope);
7618 Set_Ekind (Derived_Type, Ekind (Parent_Base));
7619 Set_Etype (Derived_Type, Parent_Base);
7620 Set_Has_Task (Derived_Type, Has_Task (Parent_Base));
7622 Set_Size_Info (Derived_Type, Parent_Type);
7623 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
7624 Set_Convention (Derived_Type, Convention (Parent_Type));
7625 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
7626 Set_Is_Tagged_Type (Derived_Type, Is_Tagged_Type (Parent_Type));
7628 -- The derived type inherits the representation clauses of the parent.
7629 -- However, for a private type that is completed by a derivation, there
7630 -- may be operation attributes that have been specified already (stream
7631 -- attributes and External_Tag) and those must be provided. Finally,
7632 -- if the partial view is a private extension, the representation items
7633 -- of the parent have been inherited already, and should not be chained
7634 -- twice to the derived type.
7636 if Is_Tagged_Type (Parent_Type)
7637 and then Present (First_Rep_Item (Derived_Type))
7638 then
7639 -- The existing items are either operational items or items inherited
7640 -- from a private extension declaration.
7642 declare
7643 Rep : Node_Id;
7644 -- Used to iterate over representation items of the derived type
7646 Last_Rep : Node_Id;
7647 -- Last representation item of the (non-empty) representation
7648 -- item list of the derived type.
7650 Found : Boolean := False;
7652 begin
7653 Rep := First_Rep_Item (Derived_Type);
7654 Last_Rep := Rep;
7655 while Present (Rep) loop
7656 if Rep = First_Rep_Item (Parent_Type) then
7657 Found := True;
7658 exit;
7660 else
7661 Rep := Next_Rep_Item (Rep);
7663 if Present (Rep) then
7664 Last_Rep := Rep;
7665 end if;
7666 end if;
7667 end loop;
7669 -- Here if we either encountered the parent type's first rep
7670 -- item on the derived type's rep item list (in which case
7671 -- Found is True, and we have nothing else to do), or if we
7672 -- reached the last rep item of the derived type, which is
7673 -- Last_Rep, in which case we further chain the parent type's
7674 -- rep items to those of the derived type.
7676 if not Found then
7677 Set_Next_Rep_Item (Last_Rep, First_Rep_Item (Parent_Type));
7678 end if;
7679 end;
7681 else
7682 Set_First_Rep_Item (Derived_Type, First_Rep_Item (Parent_Type));
7683 end if;
7685 case Ekind (Parent_Type) is
7686 when Numeric_Kind =>
7687 Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
7689 when Array_Kind =>
7690 Build_Derived_Array_Type (N, Parent_Type, Derived_Type);
7692 when E_Record_Type
7693 | E_Record_Subtype
7694 | Class_Wide_Kind =>
7695 Build_Derived_Record_Type
7696 (N, Parent_Type, Derived_Type, Derive_Subps);
7697 return;
7699 when Enumeration_Kind =>
7700 Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
7702 when Access_Kind =>
7703 Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
7705 when Incomplete_Or_Private_Kind =>
7706 Build_Derived_Private_Type
7707 (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
7709 -- For discriminated types, the derivation includes deriving
7710 -- primitive operations. For others it is done below.
7712 if Is_Tagged_Type (Parent_Type)
7713 or else Has_Discriminants (Parent_Type)
7714 or else (Present (Full_View (Parent_Type))
7715 and then Has_Discriminants (Full_View (Parent_Type)))
7716 then
7717 return;
7718 end if;
7720 when Concurrent_Kind =>
7721 Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
7723 when others =>
7724 raise Program_Error;
7725 end case;
7727 if Etype (Derived_Type) = Any_Type then
7728 return;
7729 end if;
7731 -- Set delayed freeze and then derive subprograms, we need to do this
7732 -- in this order so that derived subprograms inherit the derived freeze
7733 -- if necessary.
7735 Set_Has_Delayed_Freeze (Derived_Type);
7736 if Derive_Subps then
7737 Derive_Subprograms (Parent_Type, Derived_Type);
7738 end if;
7740 Set_Has_Primitive_Operations
7741 (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
7742 end Build_Derived_Type;
7744 -----------------------
7745 -- Build_Discriminal --
7746 -----------------------
7748 procedure Build_Discriminal (Discrim : Entity_Id) is
7749 D_Minal : Entity_Id;
7750 CR_Disc : Entity_Id;
7752 begin
7753 -- A discriminal has the same name as the discriminant
7755 D_Minal :=
7756 Make_Defining_Identifier (Sloc (Discrim),
7757 Chars => Chars (Discrim));
7759 Set_Ekind (D_Minal, E_In_Parameter);
7760 Set_Mechanism (D_Minal, Default_Mechanism);
7761 Set_Etype (D_Minal, Etype (Discrim));
7762 Set_Scope (D_Minal, Current_Scope);
7764 Set_Discriminal (Discrim, D_Minal);
7765 Set_Discriminal_Link (D_Minal, Discrim);
7767 -- For task types, build at once the discriminants of the corresponding
7768 -- record, which are needed if discriminants are used in entry defaults
7769 -- and in family bounds.
7771 if Is_Concurrent_Type (Current_Scope)
7772 or else Is_Limited_Type (Current_Scope)
7773 then
7774 CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
7776 Set_Ekind (CR_Disc, E_In_Parameter);
7777 Set_Mechanism (CR_Disc, Default_Mechanism);
7778 Set_Etype (CR_Disc, Etype (Discrim));
7779 Set_Scope (CR_Disc, Current_Scope);
7780 Set_Discriminal_Link (CR_Disc, Discrim);
7781 Set_CR_Discriminant (Discrim, CR_Disc);
7782 end if;
7783 end Build_Discriminal;
7785 ------------------------------------
7786 -- Build_Discriminant_Constraints --
7787 ------------------------------------
7789 function Build_Discriminant_Constraints
7790 (T : Entity_Id;
7791 Def : Node_Id;
7792 Derived_Def : Boolean := False) return Elist_Id
7794 C : constant Node_Id := Constraint (Def);
7795 Nb_Discr : constant Nat := Number_Discriminants (T);
7797 Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
7798 -- Saves the expression corresponding to a given discriminant in T
7800 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
7801 -- Return the Position number within array Discr_Expr of a discriminant
7802 -- D within the discriminant list of the discriminated type T.
7804 ------------------
7805 -- Pos_Of_Discr --
7806 ------------------
7808 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
7809 Disc : Entity_Id;
7811 begin
7812 Disc := First_Discriminant (T);
7813 for J in Discr_Expr'Range loop
7814 if Disc = D then
7815 return J;
7816 end if;
7818 Next_Discriminant (Disc);
7819 end loop;
7821 -- Note: Since this function is called on discriminants that are
7822 -- known to belong to the discriminated type, falling through the
7823 -- loop with no match signals an internal compiler error.
7825 raise Program_Error;
7826 end Pos_Of_Discr;
7828 -- Declarations local to Build_Discriminant_Constraints
7830 Discr : Entity_Id;
7831 E : Entity_Id;
7832 Elist : constant Elist_Id := New_Elmt_List;
7834 Constr : Node_Id;
7835 Expr : Node_Id;
7836 Id : Node_Id;
7837 Position : Nat;
7838 Found : Boolean;
7840 Discrim_Present : Boolean := False;
7842 -- Start of processing for Build_Discriminant_Constraints
7844 begin
7845 -- The following loop will process positional associations only.
7846 -- For a positional association, the (single) discriminant is
7847 -- implicitly specified by position, in textual order (RM 3.7.2).
7849 Discr := First_Discriminant (T);
7850 Constr := First (Constraints (C));
7851 for D in Discr_Expr'Range loop
7852 exit when Nkind (Constr) = N_Discriminant_Association;
7854 if No (Constr) then
7855 Error_Msg_N ("too few discriminants given in constraint", C);
7856 return New_Elmt_List;
7858 elsif Nkind (Constr) = N_Range
7859 or else (Nkind (Constr) = N_Attribute_Reference
7860 and then
7861 Attribute_Name (Constr) = Name_Range)
7862 then
7863 Error_Msg_N
7864 ("a range is not a valid discriminant constraint", Constr);
7865 Discr_Expr (D) := Error;
7867 else
7868 Analyze_And_Resolve (Constr, Base_Type (Etype (Discr)));
7869 Discr_Expr (D) := Constr;
7870 end if;
7872 Next_Discriminant (Discr);
7873 Next (Constr);
7874 end loop;
7876 if No (Discr) and then Present (Constr) then
7877 Error_Msg_N ("too many discriminants given in constraint", Constr);
7878 return New_Elmt_List;
7879 end if;
7881 -- Named associations can be given in any order, but if both positional
7882 -- and named associations are used in the same discriminant constraint,
7883 -- then positional associations must occur first, at their normal
7884 -- position. Hence once a named association is used, the rest of the
7885 -- discriminant constraint must use only named associations.
7887 while Present (Constr) loop
7889 -- Positional association forbidden after a named association
7891 if Nkind (Constr) /= N_Discriminant_Association then
7892 Error_Msg_N ("positional association follows named one", Constr);
7893 return New_Elmt_List;
7895 -- Otherwise it is a named association
7897 else
7898 -- E records the type of the discriminants in the named
7899 -- association. All the discriminants specified in the same name
7900 -- association must have the same type.
7902 E := Empty;
7904 -- Search the list of discriminants in T to see if the simple name
7905 -- given in the constraint matches any of them.
7907 Id := First (Selector_Names (Constr));
7908 while Present (Id) loop
7909 Found := False;
7911 -- If Original_Discriminant is present, we are processing a
7912 -- generic instantiation and this is an instance node. We need
7913 -- to find the name of the corresponding discriminant in the
7914 -- actual record type T and not the name of the discriminant in
7915 -- the generic formal. Example:
7917 -- generic
7918 -- type G (D : int) is private;
7919 -- package P is
7920 -- subtype W is G (D => 1);
7921 -- end package;
7922 -- type Rec (X : int) is record ... end record;
7923 -- package Q is new P (G => Rec);
7925 -- At the point of the instantiation, formal type G is Rec
7926 -- and therefore when reanalyzing "subtype W is G (D => 1);"
7927 -- which really looks like "subtype W is Rec (D => 1);" at
7928 -- the point of instantiation, we want to find the discriminant
7929 -- that corresponds to D in Rec, i.e. X.
7931 if Present (Original_Discriminant (Id)) then
7932 Discr := Find_Corresponding_Discriminant (Id, T);
7933 Found := True;
7935 else
7936 Discr := First_Discriminant (T);
7937 while Present (Discr) loop
7938 if Chars (Discr) = Chars (Id) then
7939 Found := True;
7940 exit;
7941 end if;
7943 Next_Discriminant (Discr);
7944 end loop;
7946 if not Found then
7947 Error_Msg_N ("& does not match any discriminant", Id);
7948 return New_Elmt_List;
7950 -- The following is only useful for the benefit of generic
7951 -- instances but it does not interfere with other
7952 -- processing for the non-generic case so we do it in all
7953 -- cases (for generics this statement is executed when
7954 -- processing the generic definition, see comment at the
7955 -- beginning of this if statement).
7957 else
7958 Set_Original_Discriminant (Id, Discr);
7959 end if;
7960 end if;
7962 Position := Pos_Of_Discr (T, Discr);
7964 if Present (Discr_Expr (Position)) then
7965 Error_Msg_N ("duplicate constraint for discriminant&", Id);
7967 else
7968 -- Each discriminant specified in the same named association
7969 -- must be associated with a separate copy of the
7970 -- corresponding expression.
7972 if Present (Next (Id)) then
7973 Expr := New_Copy_Tree (Expression (Constr));
7974 Set_Parent (Expr, Parent (Expression (Constr)));
7975 else
7976 Expr := Expression (Constr);
7977 end if;
7979 Discr_Expr (Position) := Expr;
7980 Analyze_And_Resolve (Expr, Base_Type (Etype (Discr)));
7981 end if;
7983 -- A discriminant association with more than one discriminant
7984 -- name is only allowed if the named discriminants are all of
7985 -- the same type (RM 3.7.1(8)).
7987 if E = Empty then
7988 E := Base_Type (Etype (Discr));
7990 elsif Base_Type (Etype (Discr)) /= E then
7991 Error_Msg_N
7992 ("all discriminants in an association " &
7993 "must have the same type", Id);
7994 end if;
7996 Next (Id);
7997 end loop;
7998 end if;
8000 Next (Constr);
8001 end loop;
8003 -- A discriminant constraint must provide exactly one value for each
8004 -- discriminant of the type (RM 3.7.1(8)).
8006 for J in Discr_Expr'Range loop
8007 if No (Discr_Expr (J)) then
8008 Error_Msg_N ("too few discriminants given in constraint", C);
8009 return New_Elmt_List;
8010 end if;
8011 end loop;
8013 -- Determine if there are discriminant expressions in the constraint
8015 for J in Discr_Expr'Range loop
8016 if Denotes_Discriminant
8017 (Discr_Expr (J), Check_Concurrent => True)
8018 then
8019 Discrim_Present := True;
8020 end if;
8021 end loop;
8023 -- Build an element list consisting of the expressions given in the
8024 -- discriminant constraint and apply the appropriate checks. The list
8025 -- is constructed after resolving any named discriminant associations
8026 -- and therefore the expressions appear in the textual order of the
8027 -- discriminants.
8029 Discr := First_Discriminant (T);
8030 for J in Discr_Expr'Range loop
8031 if Discr_Expr (J) /= Error then
8032 Append_Elmt (Discr_Expr (J), Elist);
8034 -- If any of the discriminant constraints is given by a
8035 -- discriminant and we are in a derived type declaration we
8036 -- have a discriminant renaming. Establish link between new
8037 -- and old discriminant.
8039 if Denotes_Discriminant (Discr_Expr (J)) then
8040 if Derived_Def then
8041 Set_Corresponding_Discriminant
8042 (Entity (Discr_Expr (J)), Discr);
8043 end if;
8045 -- Force the evaluation of non-discriminant expressions.
8046 -- If we have found a discriminant in the constraint 3.4(26)
8047 -- and 3.8(18) demand that no range checks are performed are
8048 -- after evaluation. If the constraint is for a component
8049 -- definition that has a per-object constraint, expressions are
8050 -- evaluated but not checked either. In all other cases perform
8051 -- a range check.
8053 else
8054 if Discrim_Present then
8055 null;
8057 elsif Nkind (Parent (Parent (Def))) = N_Component_Declaration
8058 and then
8059 Has_Per_Object_Constraint
8060 (Defining_Identifier (Parent (Parent (Def))))
8061 then
8062 null;
8064 elsif Is_Access_Type (Etype (Discr)) then
8065 Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
8067 else
8068 Apply_Range_Check (Discr_Expr (J), Etype (Discr));
8069 end if;
8071 Force_Evaluation (Discr_Expr (J));
8072 end if;
8074 -- Check that the designated type of an access discriminant's
8075 -- expression is not a class-wide type unless the discriminant's
8076 -- designated type is also class-wide.
8078 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
8079 and then not Is_Class_Wide_Type
8080 (Designated_Type (Etype (Discr)))
8081 and then Etype (Discr_Expr (J)) /= Any_Type
8082 and then Is_Class_Wide_Type
8083 (Designated_Type (Etype (Discr_Expr (J))))
8084 then
8085 Wrong_Type (Discr_Expr (J), Etype (Discr));
8087 elsif Is_Access_Type (Etype (Discr))
8088 and then not Is_Access_Constant (Etype (Discr))
8089 and then Is_Access_Type (Etype (Discr_Expr (J)))
8090 and then Is_Access_Constant (Etype (Discr_Expr (J)))
8091 then
8092 Error_Msg_NE
8093 ("constraint for discriminant& must be access to variable",
8094 Def, Discr);
8095 end if;
8096 end if;
8098 Next_Discriminant (Discr);
8099 end loop;
8101 return Elist;
8102 end Build_Discriminant_Constraints;
8104 ---------------------------------
8105 -- Build_Discriminated_Subtype --
8106 ---------------------------------
8108 procedure Build_Discriminated_Subtype
8109 (T : Entity_Id;
8110 Def_Id : Entity_Id;
8111 Elist : Elist_Id;
8112 Related_Nod : Node_Id;
8113 For_Access : Boolean := False)
8115 Has_Discrs : constant Boolean := Has_Discriminants (T);
8116 Constrained : constant Boolean :=
8117 (Has_Discrs
8118 and then not Is_Empty_Elmt_List (Elist)
8119 and then not Is_Class_Wide_Type (T))
8120 or else Is_Constrained (T);
8122 begin
8123 if Ekind (T) = E_Record_Type then
8124 if For_Access then
8125 Set_Ekind (Def_Id, E_Private_Subtype);
8126 Set_Is_For_Access_Subtype (Def_Id, True);
8127 else
8128 Set_Ekind (Def_Id, E_Record_Subtype);
8129 end if;
8131 -- Inherit preelaboration flag from base, for types for which it
8132 -- may have been set: records, private types, protected types.
8134 Set_Known_To_Have_Preelab_Init
8135 (Def_Id, Known_To_Have_Preelab_Init (T));
8137 elsif Ekind (T) = E_Task_Type then
8138 Set_Ekind (Def_Id, E_Task_Subtype);
8140 elsif Ekind (T) = E_Protected_Type then
8141 Set_Ekind (Def_Id, E_Protected_Subtype);
8142 Set_Known_To_Have_Preelab_Init
8143 (Def_Id, Known_To_Have_Preelab_Init (T));
8145 elsif Is_Private_Type (T) then
8146 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
8147 Set_Known_To_Have_Preelab_Init
8148 (Def_Id, Known_To_Have_Preelab_Init (T));
8150 elsif Is_Class_Wide_Type (T) then
8151 Set_Ekind (Def_Id, E_Class_Wide_Subtype);
8153 else
8154 -- Incomplete type. Attach subtype to list of dependents, to be
8155 -- completed with full view of parent type, unless is it the
8156 -- designated subtype of a record component within an init_proc.
8157 -- This last case arises for a component of an access type whose
8158 -- designated type is incomplete (e.g. a Taft Amendment type).
8159 -- The designated subtype is within an inner scope, and needs no
8160 -- elaboration, because only the access type is needed in the
8161 -- initialization procedure.
8163 Set_Ekind (Def_Id, Ekind (T));
8165 if For_Access and then Within_Init_Proc then
8166 null;
8167 else
8168 Append_Elmt (Def_Id, Private_Dependents (T));
8169 end if;
8170 end if;
8172 Set_Etype (Def_Id, T);
8173 Init_Size_Align (Def_Id);
8174 Set_Has_Discriminants (Def_Id, Has_Discrs);
8175 Set_Is_Constrained (Def_Id, Constrained);
8177 Set_First_Entity (Def_Id, First_Entity (T));
8178 Set_Last_Entity (Def_Id, Last_Entity (T));
8180 -- If the subtype is the completion of a private declaration, there may
8181 -- have been representation clauses for the partial view, and they must
8182 -- be preserved. Build_Derived_Type chains the inherited clauses with
8183 -- the ones appearing on the extension. If this comes from a subtype
8184 -- declaration, all clauses are inherited.
8186 if No (First_Rep_Item (Def_Id)) then
8187 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
8188 end if;
8190 if Is_Tagged_Type (T) then
8191 Set_Is_Tagged_Type (Def_Id);
8192 Make_Class_Wide_Type (Def_Id);
8193 end if;
8195 Set_Stored_Constraint (Def_Id, No_Elist);
8197 if Has_Discrs then
8198 Set_Discriminant_Constraint (Def_Id, Elist);
8199 Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
8200 end if;
8202 if Is_Tagged_Type (T) then
8204 -- Ada 2005 (AI-251): In case of concurrent types we inherit the
8205 -- concurrent record type (which has the list of primitive
8206 -- operations).
8208 if Ada_Version >= Ada_05
8209 and then Is_Concurrent_Type (T)
8210 then
8211 Set_Corresponding_Record_Type (Def_Id,
8212 Corresponding_Record_Type (T));
8213 else
8214 Set_Primitive_Operations (Def_Id, Primitive_Operations (T));
8215 end if;
8217 Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
8218 end if;
8220 -- Subtypes introduced by component declarations do not need to be
8221 -- marked as delayed, and do not get freeze nodes, because the semantics
8222 -- verifies that the parents of the subtypes are frozen before the
8223 -- enclosing record is frozen.
8225 if not Is_Type (Scope (Def_Id)) then
8226 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
8228 if Is_Private_Type (T)
8229 and then Present (Full_View (T))
8230 then
8231 Conditional_Delay (Def_Id, Full_View (T));
8232 else
8233 Conditional_Delay (Def_Id, T);
8234 end if;
8235 end if;
8237 if Is_Record_Type (T) then
8238 Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
8240 if Has_Discrs
8241 and then not Is_Empty_Elmt_List (Elist)
8242 and then not For_Access
8243 then
8244 Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
8245 elsif not For_Access then
8246 Set_Cloned_Subtype (Def_Id, T);
8247 end if;
8248 end if;
8249 end Build_Discriminated_Subtype;
8251 ---------------------------
8252 -- Build_Itype_Reference --
8253 ---------------------------
8255 procedure Build_Itype_Reference
8256 (Ityp : Entity_Id;
8257 Nod : Node_Id)
8259 IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
8260 begin
8261 Set_Itype (IR, Ityp);
8262 Insert_After (Nod, IR);
8263 end Build_Itype_Reference;
8265 ------------------------
8266 -- Build_Scalar_Bound --
8267 ------------------------
8269 function Build_Scalar_Bound
8270 (Bound : Node_Id;
8271 Par_T : Entity_Id;
8272 Der_T : Entity_Id) return Node_Id
8274 New_Bound : Entity_Id;
8276 begin
8277 -- Note: not clear why this is needed, how can the original bound
8278 -- be unanalyzed at this point? and if it is, what business do we
8279 -- have messing around with it? and why is the base type of the
8280 -- parent type the right type for the resolution. It probably is
8281 -- not! It is OK for the new bound we are creating, but not for
8282 -- the old one??? Still if it never happens, no problem!
8284 Analyze_And_Resolve (Bound, Base_Type (Par_T));
8286 if Nkind_In (Bound, N_Integer_Literal, N_Real_Literal) then
8287 New_Bound := New_Copy (Bound);
8288 Set_Etype (New_Bound, Der_T);
8289 Set_Analyzed (New_Bound);
8291 elsif Is_Entity_Name (Bound) then
8292 New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
8294 -- The following is almost certainly wrong. What business do we have
8295 -- relocating a node (Bound) that is presumably still attached to
8296 -- the tree elsewhere???
8298 else
8299 New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
8300 end if;
8302 Set_Etype (New_Bound, Der_T);
8303 return New_Bound;
8304 end Build_Scalar_Bound;
8306 --------------------------------
8307 -- Build_Underlying_Full_View --
8308 --------------------------------
8310 procedure Build_Underlying_Full_View
8311 (N : Node_Id;
8312 Typ : Entity_Id;
8313 Par : Entity_Id)
8315 Loc : constant Source_Ptr := Sloc (N);
8316 Subt : constant Entity_Id :=
8317 Make_Defining_Identifier
8318 (Loc, New_External_Name (Chars (Typ), 'S'));
8320 Constr : Node_Id;
8321 Indic : Node_Id;
8322 C : Node_Id;
8323 Id : Node_Id;
8325 procedure Set_Discriminant_Name (Id : Node_Id);
8326 -- If the derived type has discriminants, they may rename discriminants
8327 -- of the parent. When building the full view of the parent, we need to
8328 -- recover the names of the original discriminants if the constraint is
8329 -- given by named associations.
8331 ---------------------------
8332 -- Set_Discriminant_Name --
8333 ---------------------------
8335 procedure Set_Discriminant_Name (Id : Node_Id) is
8336 Disc : Entity_Id;
8338 begin
8339 Set_Original_Discriminant (Id, Empty);
8341 if Has_Discriminants (Typ) then
8342 Disc := First_Discriminant (Typ);
8343 while Present (Disc) loop
8344 if Chars (Disc) = Chars (Id)
8345 and then Present (Corresponding_Discriminant (Disc))
8346 then
8347 Set_Chars (Id, Chars (Corresponding_Discriminant (Disc)));
8348 end if;
8349 Next_Discriminant (Disc);
8350 end loop;
8351 end if;
8352 end Set_Discriminant_Name;
8354 -- Start of processing for Build_Underlying_Full_View
8356 begin
8357 if Nkind (N) = N_Full_Type_Declaration then
8358 Constr := Constraint (Subtype_Indication (Type_Definition (N)));
8360 elsif Nkind (N) = N_Subtype_Declaration then
8361 Constr := New_Copy_Tree (Constraint (Subtype_Indication (N)));
8363 elsif Nkind (N) = N_Component_Declaration then
8364 Constr :=
8365 New_Copy_Tree
8366 (Constraint (Subtype_Indication (Component_Definition (N))));
8368 else
8369 raise Program_Error;
8370 end if;
8372 C := First (Constraints (Constr));
8373 while Present (C) loop
8374 if Nkind (C) = N_Discriminant_Association then
8375 Id := First (Selector_Names (C));
8376 while Present (Id) loop
8377 Set_Discriminant_Name (Id);
8378 Next (Id);
8379 end loop;
8380 end if;
8382 Next (C);
8383 end loop;
8385 Indic :=
8386 Make_Subtype_Declaration (Loc,
8387 Defining_Identifier => Subt,
8388 Subtype_Indication =>
8389 Make_Subtype_Indication (Loc,
8390 Subtype_Mark => New_Reference_To (Par, Loc),
8391 Constraint => New_Copy_Tree (Constr)));
8393 -- If this is a component subtype for an outer itype, it is not
8394 -- a list member, so simply set the parent link for analysis: if
8395 -- the enclosing type does not need to be in a declarative list,
8396 -- neither do the components.
8398 if Is_List_Member (N)
8399 and then Nkind (N) /= N_Component_Declaration
8400 then
8401 Insert_Before (N, Indic);
8402 else
8403 Set_Parent (Indic, Parent (N));
8404 end if;
8406 Analyze (Indic);
8407 Set_Underlying_Full_View (Typ, Full_View (Subt));
8408 end Build_Underlying_Full_View;
8410 -------------------------------
8411 -- Check_Abstract_Overriding --
8412 -------------------------------
8414 procedure Check_Abstract_Overriding (T : Entity_Id) is
8415 Alias_Subp : Entity_Id;
8416 Elmt : Elmt_Id;
8417 Op_List : Elist_Id;
8418 Subp : Entity_Id;
8419 Type_Def : Node_Id;
8421 begin
8422 Op_List := Primitive_Operations (T);
8424 -- Loop to check primitive operations
8426 Elmt := First_Elmt (Op_List);
8427 while Present (Elmt) loop
8428 Subp := Node (Elmt);
8429 Alias_Subp := Alias (Subp);
8431 -- Inherited subprograms are identified by the fact that they do not
8432 -- come from source, and the associated source location is the
8433 -- location of the first subtype of the derived type.
8435 -- Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
8436 -- subprograms that "require overriding".
8438 -- Special exception, do not complain about failure to override the
8439 -- stream routines _Input and _Output, as well as the primitive
8440 -- operations used in dispatching selects since we always provide
8441 -- automatic overridings for these subprograms.
8443 -- Also ignore this rule for convention CIL since .NET libraries
8444 -- do bizarre things with interfaces???
8446 -- The partial view of T may have been a private extension, for
8447 -- which inherited functions dispatching on result are abstract.
8448 -- If the full view is a null extension, there is no need for
8449 -- overriding in Ada2005, but wrappers need to be built for them
8450 -- (see exp_ch3, Build_Controlling_Function_Wrappers).
8452 if Is_Null_Extension (T)
8453 and then Has_Controlling_Result (Subp)
8454 and then Ada_Version >= Ada_05
8455 and then Present (Alias_Subp)
8456 and then not Comes_From_Source (Subp)
8457 and then not Is_Abstract_Subprogram (Alias_Subp)
8458 and then not Is_Access_Type (Etype (Subp))
8459 then
8460 null;
8462 -- Ada 2005 (AI-251): Internal entities of interfaces need no
8463 -- processing because this check is done with the aliased
8464 -- entity
8466 elsif Present (Interface_Alias (Subp)) then
8467 null;
8469 elsif (Is_Abstract_Subprogram (Subp)
8470 or else Requires_Overriding (Subp)
8471 or else
8472 (Has_Controlling_Result (Subp)
8473 and then Present (Alias_Subp)
8474 and then not Comes_From_Source (Subp)
8475 and then Sloc (Subp) = Sloc (First_Subtype (T))))
8476 and then not Is_TSS (Subp, TSS_Stream_Input)
8477 and then not Is_TSS (Subp, TSS_Stream_Output)
8478 and then not Is_Abstract_Type (T)
8479 and then Convention (T) /= Convention_CIL
8480 and then not Is_Predefined_Interface_Primitive (Subp)
8482 -- Ada 2005 (AI-251): Do not consider hidden entities associated
8483 -- with abstract interface types because the check will be done
8484 -- with the aliased entity (otherwise we generate a duplicated
8485 -- error message).
8487 and then not Present (Interface_Alias (Subp))
8488 then
8489 if Present (Alias_Subp) then
8491 -- Only perform the check for a derived subprogram when the
8492 -- type has an explicit record extension. This avoids incorrect
8493 -- flagging of abstract subprograms for the case of a type
8494 -- without an extension that is derived from a formal type
8495 -- with a tagged actual (can occur within a private part).
8497 -- Ada 2005 (AI-391): In the case of an inherited function with
8498 -- a controlling result of the type, the rule does not apply if
8499 -- the type is a null extension (unless the parent function
8500 -- itself is abstract, in which case the function must still be
8501 -- be overridden). The expander will generate an overriding
8502 -- wrapper function calling the parent subprogram (see
8503 -- Exp_Ch3.Make_Controlling_Wrapper_Functions).
8505 Type_Def := Type_Definition (Parent (T));
8507 if Nkind (Type_Def) = N_Derived_Type_Definition
8508 and then Present (Record_Extension_Part (Type_Def))
8509 and then
8510 (Ada_Version < Ada_05
8511 or else not Is_Null_Extension (T)
8512 or else Ekind (Subp) = E_Procedure
8513 or else not Has_Controlling_Result (Subp)
8514 or else Is_Abstract_Subprogram (Alias_Subp)
8515 or else Requires_Overriding (Subp)
8516 or else Is_Access_Type (Etype (Subp)))
8517 then
8518 -- Avoid reporting error in case of abstract predefined
8519 -- primitive inherited from interface type because the
8520 -- body of internally generated predefined primitives
8521 -- of tagged types are generated later by Freeze_Type
8523 if Is_Interface (Root_Type (T))
8524 and then Is_Abstract_Subprogram (Subp)
8525 and then Is_Predefined_Dispatching_Operation (Subp)
8526 and then not Comes_From_Source (Ultimate_Alias (Subp))
8527 then
8528 null;
8530 else
8531 Error_Msg_NE
8532 ("type must be declared abstract or & overridden",
8533 T, Subp);
8535 -- Traverse the whole chain of aliased subprograms to
8536 -- complete the error notification. This is especially
8537 -- useful for traceability of the chain of entities when
8538 -- the subprogram corresponds with an interface
8539 -- subprogram (which may be defined in another package).
8541 if Present (Alias_Subp) then
8542 declare
8543 E : Entity_Id;
8545 begin
8546 E := Subp;
8547 while Present (Alias (E)) loop
8548 Error_Msg_Sloc := Sloc (E);
8549 Error_Msg_NE
8550 ("\& has been inherited #", T, Subp);
8551 E := Alias (E);
8552 end loop;
8554 Error_Msg_Sloc := Sloc (E);
8555 Error_Msg_NE
8556 ("\& has been inherited from subprogram #",
8557 T, Subp);
8558 end;
8559 end if;
8560 end if;
8562 -- Ada 2005 (AI-345): Protected or task type implementing
8563 -- abstract interfaces.
8565 elsif Is_Concurrent_Record_Type (T)
8566 and then Present (Interfaces (T))
8567 then
8568 -- The controlling formal of Subp must be of mode "out",
8569 -- "in out" or an access-to-variable to be overridden.
8571 -- Error message below needs rewording (remember comma
8572 -- in -gnatj mode) ???
8574 if Ekind (First_Formal (Subp)) = E_In_Parameter
8575 and then Ekind (Subp) /= E_Function
8576 then
8577 if not Is_Predefined_Dispatching_Operation (Subp) then
8578 Error_Msg_NE
8579 ("first formal of & must be of mode `OUT`, " &
8580 "`IN OUT` or access-to-variable", T, Subp);
8581 Error_Msg_N
8582 ("\to be overridden by protected procedure or " &
8583 "entry (RM 9.4(11.9/2))", T);
8584 end if;
8586 -- Some other kind of overriding failure
8588 else
8589 Error_Msg_NE
8590 ("interface subprogram & must be overridden",
8591 T, Subp);
8593 -- Examine primitive operations of synchronized type,
8594 -- to find homonyms that have the wrong profile.
8596 declare
8597 Prim : Entity_Id;
8599 begin
8600 Prim :=
8601 First_Entity (Corresponding_Concurrent_Type (T));
8602 while Present (Prim) loop
8603 if Chars (Prim) = Chars (Subp) then
8604 Error_Msg_NE
8605 ("profile is not type conformant with "
8606 & "prefixed view profile of "
8607 & "inherited operation&", Prim, Subp);
8608 end if;
8610 Next_Entity (Prim);
8611 end loop;
8612 end;
8613 end if;
8614 end if;
8616 else
8617 Error_Msg_Node_2 := T;
8618 Error_Msg_N
8619 ("abstract subprogram& not allowed for type&", Subp);
8621 -- Also post unconditional warning on the type (unconditional
8622 -- so that if there are more than one of these cases, we get
8623 -- them all, and not just the first one).
8625 Error_Msg_Node_2 := Subp;
8626 Error_Msg_N ("nonabstract type& has abstract subprogram&!", T);
8627 end if;
8628 end if;
8630 -- Ada 2005 (AI05-0030): Inspect hidden subprograms which provide
8631 -- the mapping between interface and implementing type primitives.
8632 -- If the interface alias is marked as Implemented_By_Entry, the
8633 -- alias must be an entry wrapper.
8635 if Ada_Version >= Ada_05
8636 and then Is_Hidden (Subp)
8637 and then Present (Interface_Alias (Subp))
8638 and then Implemented_By_Entry (Interface_Alias (Subp))
8639 and then Present (Alias_Subp)
8640 and then
8641 (not Is_Primitive_Wrapper (Alias_Subp)
8642 or else Ekind (Wrapped_Entity (Alias_Subp)) /= E_Entry)
8643 then
8644 declare
8645 Error_Ent : Entity_Id := T;
8647 begin
8648 if Is_Concurrent_Record_Type (Error_Ent) then
8649 Error_Ent := Corresponding_Concurrent_Type (Error_Ent);
8650 end if;
8652 Error_Msg_Node_2 := Interface_Alias (Subp);
8653 Error_Msg_NE
8654 ("type & must implement abstract subprogram & with an entry",
8655 Error_Ent, Error_Ent);
8656 end;
8657 end if;
8659 Next_Elmt (Elmt);
8660 end loop;
8661 end Check_Abstract_Overriding;
8663 ------------------------------------------------
8664 -- Check_Access_Discriminant_Requires_Limited --
8665 ------------------------------------------------
8667 procedure Check_Access_Discriminant_Requires_Limited
8668 (D : Node_Id;
8669 Loc : Node_Id)
8671 begin
8672 -- A discriminant_specification for an access discriminant shall appear
8673 -- only in the declaration for a task or protected type, or for a type
8674 -- with the reserved word 'limited' in its definition or in one of its
8675 -- ancestors. (RM 3.7(10))
8677 if Nkind (Discriminant_Type (D)) = N_Access_Definition
8678 and then not Is_Concurrent_Type (Current_Scope)
8679 and then not Is_Concurrent_Record_Type (Current_Scope)
8680 and then not Is_Limited_Record (Current_Scope)
8681 and then Ekind (Current_Scope) /= E_Limited_Private_Type
8682 then
8683 Error_Msg_N
8684 ("access discriminants allowed only for limited types", Loc);
8685 end if;
8686 end Check_Access_Discriminant_Requires_Limited;
8688 -----------------------------------
8689 -- Check_Aliased_Component_Types --
8690 -----------------------------------
8692 procedure Check_Aliased_Component_Types (T : Entity_Id) is
8693 C : Entity_Id;
8695 begin
8696 -- ??? Also need to check components of record extensions, but not
8697 -- components of protected types (which are always limited).
8699 -- Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
8700 -- types to be unconstrained. This is safe because it is illegal to
8701 -- create access subtypes to such types with explicit discriminant
8702 -- constraints.
8704 if not Is_Limited_Type (T) then
8705 if Ekind (T) = E_Record_Type then
8706 C := First_Component (T);
8707 while Present (C) loop
8708 if Is_Aliased (C)
8709 and then Has_Discriminants (Etype (C))
8710 and then not Is_Constrained (Etype (C))
8711 and then not In_Instance_Body
8712 and then Ada_Version < Ada_05
8713 then
8714 Error_Msg_N
8715 ("aliased component must be constrained (RM 3.6(11))",
8717 end if;
8719 Next_Component (C);
8720 end loop;
8722 elsif Ekind (T) = E_Array_Type then
8723 if Has_Aliased_Components (T)
8724 and then Has_Discriminants (Component_Type (T))
8725 and then not Is_Constrained (Component_Type (T))
8726 and then not In_Instance_Body
8727 and then Ada_Version < Ada_05
8728 then
8729 Error_Msg_N
8730 ("aliased component type must be constrained (RM 3.6(11))",
8732 end if;
8733 end if;
8734 end if;
8735 end Check_Aliased_Component_Types;
8737 ----------------------
8738 -- Check_Completion --
8739 ----------------------
8741 procedure Check_Completion (Body_Id : Node_Id := Empty) is
8742 E : Entity_Id;
8744 procedure Post_Error;
8745 -- Post error message for lack of completion for entity E
8747 ----------------
8748 -- Post_Error --
8749 ----------------
8751 procedure Post_Error is
8753 procedure Missing_Body;
8754 -- Output missing body message
8756 ------------------
8757 -- Missing_Body --
8758 ------------------
8760 procedure Missing_Body is
8761 begin
8762 -- Spec is in same unit, so we can post on spec
8764 if In_Same_Source_Unit (Body_Id, E) then
8765 Error_Msg_N ("missing body for &", E);
8767 -- Spec is in a separate unit, so we have to post on the body
8769 else
8770 Error_Msg_NE ("missing body for & declared#!", Body_Id, E);
8771 end if;
8772 end Missing_Body;
8774 -- Start of processing for Post_Error
8776 begin
8777 if not Comes_From_Source (E) then
8779 if Ekind_In (E, E_Task_Type, E_Protected_Type) then
8780 -- It may be an anonymous protected type created for a
8781 -- single variable. Post error on variable, if present.
8783 declare
8784 Var : Entity_Id;
8786 begin
8787 Var := First_Entity (Current_Scope);
8788 while Present (Var) loop
8789 exit when Etype (Var) = E
8790 and then Comes_From_Source (Var);
8792 Next_Entity (Var);
8793 end loop;
8795 if Present (Var) then
8796 E := Var;
8797 end if;
8798 end;
8799 end if;
8800 end if;
8802 -- If a generated entity has no completion, then either previous
8803 -- semantic errors have disabled the expansion phase, or else we had
8804 -- missing subunits, or else we are compiling without expansion,
8805 -- or else something is very wrong.
8807 if not Comes_From_Source (E) then
8808 pragma Assert
8809 (Serious_Errors_Detected > 0
8810 or else Configurable_Run_Time_Violations > 0
8811 or else Subunits_Missing
8812 or else not Expander_Active);
8813 return;
8815 -- Here for source entity
8817 else
8818 -- Here if no body to post the error message, so we post the error
8819 -- on the declaration that has no completion. This is not really
8820 -- the right place to post it, think about this later ???
8822 if No (Body_Id) then
8823 if Is_Type (E) then
8824 Error_Msg_NE
8825 ("missing full declaration for }", Parent (E), E);
8826 else
8827 Error_Msg_NE ("missing body for &", Parent (E), E);
8828 end if;
8830 -- Package body has no completion for a declaration that appears
8831 -- in the corresponding spec. Post error on the body, with a
8832 -- reference to the non-completed declaration.
8834 else
8835 Error_Msg_Sloc := Sloc (E);
8837 if Is_Type (E) then
8838 Error_Msg_NE ("missing full declaration for }!", Body_Id, E);
8840 elsif Is_Overloadable (E)
8841 and then Current_Entity_In_Scope (E) /= E
8842 then
8843 -- It may be that the completion is mistyped and appears as
8844 -- a distinct overloading of the entity.
8846 declare
8847 Candidate : constant Entity_Id :=
8848 Current_Entity_In_Scope (E);
8849 Decl : constant Node_Id :=
8850 Unit_Declaration_Node (Candidate);
8852 begin
8853 if Is_Overloadable (Candidate)
8854 and then Ekind (Candidate) = Ekind (E)
8855 and then Nkind (Decl) = N_Subprogram_Body
8856 and then Acts_As_Spec (Decl)
8857 then
8858 Check_Type_Conformant (Candidate, E);
8860 else
8861 Missing_Body;
8862 end if;
8863 end;
8865 else
8866 Missing_Body;
8867 end if;
8868 end if;
8869 end if;
8870 end Post_Error;
8872 -- Start of processing for Check_Completion
8874 begin
8875 E := First_Entity (Current_Scope);
8876 while Present (E) loop
8877 if Is_Intrinsic_Subprogram (E) then
8878 null;
8880 -- The following situation requires special handling: a child unit
8881 -- that appears in the context clause of the body of its parent:
8883 -- procedure Parent.Child (...);
8885 -- with Parent.Child;
8886 -- package body Parent is
8888 -- Here Parent.Child appears as a local entity, but should not be
8889 -- flagged as requiring completion, because it is a compilation
8890 -- unit.
8892 -- Ignore missing completion for a subprogram that does not come from
8893 -- source (including the _Call primitive operation of RAS types,
8894 -- which has to have the flag Comes_From_Source for other purposes):
8895 -- we assume that the expander will provide the missing completion.
8896 -- In case of previous errors, other expansion actions that provide
8897 -- bodies for null procedures with not be invoked, so inhibit message
8898 -- in those cases.
8899 -- Note that E_Operator is not in the list that follows, because
8900 -- this kind is reserved for predefined operators, that are
8901 -- intrinsic and do not need completion.
8903 elsif Ekind (E) = E_Function
8904 or else Ekind (E) = E_Procedure
8905 or else Ekind (E) = E_Generic_Function
8906 or else Ekind (E) = E_Generic_Procedure
8907 then
8908 if Has_Completion (E) then
8909 null;
8911 elsif Is_Subprogram (E) and then Is_Abstract_Subprogram (E) then
8912 null;
8914 elsif Is_Subprogram (E)
8915 and then (not Comes_From_Source (E)
8916 or else Chars (E) = Name_uCall)
8917 then
8918 null;
8920 elsif
8921 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
8922 then
8923 null;
8925 elsif Nkind (Parent (E)) = N_Procedure_Specification
8926 and then Null_Present (Parent (E))
8927 and then Serious_Errors_Detected > 0
8928 then
8929 null;
8931 else
8932 Post_Error;
8933 end if;
8935 elsif Is_Entry (E) then
8936 if not Has_Completion (E) and then
8937 (Ekind (Scope (E)) = E_Protected_Object
8938 or else Ekind (Scope (E)) = E_Protected_Type)
8939 then
8940 Post_Error;
8941 end if;
8943 elsif Is_Package_Or_Generic_Package (E) then
8944 if Unit_Requires_Body (E) then
8945 if not Has_Completion (E)
8946 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
8947 N_Compilation_Unit
8948 then
8949 Post_Error;
8950 end if;
8952 elsif not Is_Child_Unit (E) then
8953 May_Need_Implicit_Body (E);
8954 end if;
8956 elsif Ekind (E) = E_Incomplete_Type
8957 and then No (Underlying_Type (E))
8958 then
8959 Post_Error;
8961 elsif (Ekind (E) = E_Task_Type or else
8962 Ekind (E) = E_Protected_Type)
8963 and then not Has_Completion (E)
8964 then
8965 Post_Error;
8967 -- A single task declared in the current scope is a constant, verify
8968 -- that the body of its anonymous type is in the same scope. If the
8969 -- task is defined elsewhere, this may be a renaming declaration for
8970 -- which no completion is needed.
8972 elsif Ekind (E) = E_Constant
8973 and then Ekind (Etype (E)) = E_Task_Type
8974 and then not Has_Completion (Etype (E))
8975 and then Scope (Etype (E)) = Current_Scope
8976 then
8977 Post_Error;
8979 elsif Ekind (E) = E_Protected_Object
8980 and then not Has_Completion (Etype (E))
8981 then
8982 Post_Error;
8984 elsif Ekind (E) = E_Record_Type then
8985 if Is_Tagged_Type (E) then
8986 Check_Abstract_Overriding (E);
8987 Check_Conventions (E);
8988 end if;
8990 Check_Aliased_Component_Types (E);
8992 elsif Ekind (E) = E_Array_Type then
8993 Check_Aliased_Component_Types (E);
8995 end if;
8997 Next_Entity (E);
8998 end loop;
8999 end Check_Completion;
9001 ----------------------------
9002 -- Check_Delta_Expression --
9003 ----------------------------
9005 procedure Check_Delta_Expression (E : Node_Id) is
9006 begin
9007 if not (Is_Real_Type (Etype (E))) then
9008 Wrong_Type (E, Any_Real);
9010 elsif not Is_OK_Static_Expression (E) then
9011 Flag_Non_Static_Expr
9012 ("non-static expression used for delta value!", E);
9014 elsif not UR_Is_Positive (Expr_Value_R (E)) then
9015 Error_Msg_N ("delta expression must be positive", E);
9017 else
9018 return;
9019 end if;
9021 -- If any of above errors occurred, then replace the incorrect
9022 -- expression by the real 0.1, which should prevent further errors.
9024 Rewrite (E,
9025 Make_Real_Literal (Sloc (E), Ureal_Tenth));
9026 Analyze_And_Resolve (E, Standard_Float);
9027 end Check_Delta_Expression;
9029 -----------------------------
9030 -- Check_Digits_Expression --
9031 -----------------------------
9033 procedure Check_Digits_Expression (E : Node_Id) is
9034 begin
9035 if not (Is_Integer_Type (Etype (E))) then
9036 Wrong_Type (E, Any_Integer);
9038 elsif not Is_OK_Static_Expression (E) then
9039 Flag_Non_Static_Expr
9040 ("non-static expression used for digits value!", E);
9042 elsif Expr_Value (E) <= 0 then
9043 Error_Msg_N ("digits value must be greater than zero", E);
9045 else
9046 return;
9047 end if;
9049 -- If any of above errors occurred, then replace the incorrect
9050 -- expression by the integer 1, which should prevent further errors.
9052 Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
9053 Analyze_And_Resolve (E, Standard_Integer);
9055 end Check_Digits_Expression;
9057 --------------------------
9058 -- Check_Initialization --
9059 --------------------------
9061 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
9062 begin
9063 if Is_Limited_Type (T)
9064 and then not In_Instance
9065 and then not In_Inlined_Body
9066 then
9067 if not OK_For_Limited_Init (T, Exp) then
9069 -- In GNAT mode, this is just a warning, to allow it to be evilly
9070 -- turned off. Otherwise it is a real error.
9072 if GNAT_Mode then
9073 Error_Msg_N
9074 ("?cannot initialize entities of limited type!", Exp);
9076 elsif Ada_Version < Ada_05 then
9077 Error_Msg_N
9078 ("cannot initialize entities of limited type", Exp);
9079 Explain_Limited_Type (T, Exp);
9081 else
9082 -- Specialize error message according to kind of illegal
9083 -- initial expression.
9085 if Nkind (Exp) = N_Type_Conversion
9086 and then Nkind (Expression (Exp)) = N_Function_Call
9087 then
9088 Error_Msg_N
9089 ("illegal context for call"
9090 & " to function with limited result", Exp);
9092 else
9093 Error_Msg_N
9094 ("initialization of limited object requires aggregate "
9095 & "or function call", Exp);
9096 end if;
9097 end if;
9098 end if;
9099 end if;
9100 end Check_Initialization;
9102 ----------------------
9103 -- Check_Interfaces --
9104 ----------------------
9106 procedure Check_Interfaces (N : Node_Id; Def : Node_Id) is
9107 Parent_Type : constant Entity_Id := Etype (Defining_Identifier (N));
9109 Iface : Node_Id;
9110 Iface_Def : Node_Id;
9111 Iface_Typ : Entity_Id;
9112 Parent_Node : Node_Id;
9114 Is_Task : Boolean := False;
9115 -- Set True if parent type or any progenitor is a task interface
9117 Is_Protected : Boolean := False;
9118 -- Set True if parent type or any progenitor is a protected interface
9120 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
9121 -- Check that a progenitor is compatible with declaration.
9122 -- Error is posted on Error_Node.
9124 ------------------
9125 -- Check_Ifaces --
9126 ------------------
9128 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
9129 Iface_Id : constant Entity_Id :=
9130 Defining_Identifier (Parent (Iface_Def));
9131 Type_Def : Node_Id;
9133 begin
9134 if Nkind (N) = N_Private_Extension_Declaration then
9135 Type_Def := N;
9136 else
9137 Type_Def := Type_Definition (N);
9138 end if;
9140 if Is_Task_Interface (Iface_Id) then
9141 Is_Task := True;
9143 elsif Is_Protected_Interface (Iface_Id) then
9144 Is_Protected := True;
9145 end if;
9147 if Is_Synchronized_Interface (Iface_Id) then
9149 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
9150 -- extension derived from a synchronized interface must explicitly
9151 -- be declared synchronized, because the full view will be a
9152 -- synchronized type.
9154 if Nkind (N) = N_Private_Extension_Declaration then
9155 if not Synchronized_Present (N) then
9156 Error_Msg_NE
9157 ("private extension of& must be explicitly synchronized",
9158 N, Iface_Id);
9159 end if;
9161 -- However, by 3.9.4(16/2), a full type that is a record extension
9162 -- is never allowed to derive from a synchronized interface (note
9163 -- that interfaces must be excluded from this check, because those
9164 -- are represented by derived type definitions in some cases).
9166 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition
9167 and then not Interface_Present (Type_Definition (N))
9168 then
9169 Error_Msg_N ("record extension cannot derive from synchronized"
9170 & " interface", Error_Node);
9171 end if;
9172 end if;
9174 -- Check that the characteristics of the progenitor are compatible
9175 -- with the explicit qualifier in the declaration.
9176 -- The check only applies to qualifiers that come from source.
9177 -- Limited_Present also appears in the declaration of corresponding
9178 -- records, and the check does not apply to them.
9180 if Limited_Present (Type_Def)
9181 and then not
9182 Is_Concurrent_Record_Type (Defining_Identifier (N))
9183 then
9184 if Is_Limited_Interface (Parent_Type)
9185 and then not Is_Limited_Interface (Iface_Id)
9186 then
9187 Error_Msg_NE
9188 ("progenitor& must be limited interface",
9189 Error_Node, Iface_Id);
9191 elsif
9192 (Task_Present (Iface_Def)
9193 or else Protected_Present (Iface_Def)
9194 or else Synchronized_Present (Iface_Def))
9195 and then Nkind (N) /= N_Private_Extension_Declaration
9196 and then not Error_Posted (N)
9197 then
9198 Error_Msg_NE
9199 ("progenitor& must be limited interface",
9200 Error_Node, Iface_Id);
9201 end if;
9203 -- Protected interfaces can only inherit from limited, synchronized
9204 -- or protected interfaces.
9206 elsif Nkind (N) = N_Full_Type_Declaration
9207 and then Protected_Present (Type_Def)
9208 then
9209 if Limited_Present (Iface_Def)
9210 or else Synchronized_Present (Iface_Def)
9211 or else Protected_Present (Iface_Def)
9212 then
9213 null;
9215 elsif Task_Present (Iface_Def) then
9216 Error_Msg_N ("(Ada 2005) protected interface cannot inherit"
9217 & " from task interface", Error_Node);
9219 else
9220 Error_Msg_N ("(Ada 2005) protected interface cannot inherit"
9221 & " from non-limited interface", Error_Node);
9222 end if;
9224 -- Ada 2005 (AI-345): Synchronized interfaces can only inherit from
9225 -- limited and synchronized.
9227 elsif Synchronized_Present (Type_Def) then
9228 if Limited_Present (Iface_Def)
9229 or else Synchronized_Present (Iface_Def)
9230 then
9231 null;
9233 elsif Protected_Present (Iface_Def)
9234 and then Nkind (N) /= N_Private_Extension_Declaration
9235 then
9236 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
9237 & " from protected interface", Error_Node);
9239 elsif Task_Present (Iface_Def)
9240 and then Nkind (N) /= N_Private_Extension_Declaration
9241 then
9242 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
9243 & " from task interface", Error_Node);
9245 elsif not Is_Limited_Interface (Iface_Id) then
9246 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
9247 & " from non-limited interface", Error_Node);
9248 end if;
9250 -- Ada 2005 (AI-345): Task interfaces can only inherit from limited,
9251 -- synchronized or task interfaces.
9253 elsif Nkind (N) = N_Full_Type_Declaration
9254 and then Task_Present (Type_Def)
9255 then
9256 if Limited_Present (Iface_Def)
9257 or else Synchronized_Present (Iface_Def)
9258 or else Task_Present (Iface_Def)
9259 then
9260 null;
9262 elsif Protected_Present (Iface_Def) then
9263 Error_Msg_N ("(Ada 2005) task interface cannot inherit from"
9264 & " protected interface", Error_Node);
9266 else
9267 Error_Msg_N ("(Ada 2005) task interface cannot inherit from"
9268 & " non-limited interface", Error_Node);
9269 end if;
9270 end if;
9271 end Check_Ifaces;
9273 -- Start of processing for Check_Interfaces
9275 begin
9276 if Is_Interface (Parent_Type) then
9277 if Is_Task_Interface (Parent_Type) then
9278 Is_Task := True;
9280 elsif Is_Protected_Interface (Parent_Type) then
9281 Is_Protected := True;
9282 end if;
9283 end if;
9285 if Nkind (N) = N_Private_Extension_Declaration then
9287 -- Check that progenitors are compatible with declaration
9289 Iface := First (Interface_List (Def));
9290 while Present (Iface) loop
9291 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
9293 Parent_Node := Parent (Base_Type (Iface_Typ));
9294 Iface_Def := Type_Definition (Parent_Node);
9296 if not Is_Interface (Iface_Typ) then
9297 Diagnose_Interface (Iface, Iface_Typ);
9299 else
9300 Check_Ifaces (Iface_Def, Iface);
9301 end if;
9303 Next (Iface);
9304 end loop;
9306 if Is_Task and Is_Protected then
9307 Error_Msg_N
9308 ("type cannot derive from task and protected interface", N);
9309 end if;
9311 return;
9312 end if;
9314 -- Full type declaration of derived type.
9315 -- Check compatibility with parent if it is interface type
9317 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
9318 and then Is_Interface (Parent_Type)
9319 then
9320 Parent_Node := Parent (Parent_Type);
9322 -- More detailed checks for interface varieties
9324 Check_Ifaces
9325 (Iface_Def => Type_Definition (Parent_Node),
9326 Error_Node => Subtype_Indication (Type_Definition (N)));
9327 end if;
9329 Iface := First (Interface_List (Def));
9330 while Present (Iface) loop
9331 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
9333 Parent_Node := Parent (Base_Type (Iface_Typ));
9334 Iface_Def := Type_Definition (Parent_Node);
9336 if not Is_Interface (Iface_Typ) then
9337 Diagnose_Interface (Iface, Iface_Typ);
9339 else
9340 -- "The declaration of a specific descendant of an interface
9341 -- type freezes the interface type" RM 13.14
9343 Freeze_Before (N, Iface_Typ);
9344 Check_Ifaces (Iface_Def, Error_Node => Iface);
9345 end if;
9347 Next (Iface);
9348 end loop;
9350 if Is_Task and Is_Protected then
9351 Error_Msg_N
9352 ("type cannot derive from task and protected interface", N);
9353 end if;
9354 end Check_Interfaces;
9356 ------------------------------------
9357 -- Check_Or_Process_Discriminants --
9358 ------------------------------------
9360 -- If an incomplete or private type declaration was already given for the
9361 -- type, the discriminants may have already been processed if they were
9362 -- present on the incomplete declaration. In this case a full conformance
9363 -- check is performed otherwise just process them.
9365 procedure Check_Or_Process_Discriminants
9366 (N : Node_Id;
9367 T : Entity_Id;
9368 Prev : Entity_Id := Empty)
9370 begin
9371 if Has_Discriminants (T) then
9373 -- Make the discriminants visible to component declarations
9375 declare
9376 D : Entity_Id;
9377 Prev : Entity_Id;
9379 begin
9380 D := First_Discriminant (T);
9381 while Present (D) loop
9382 Prev := Current_Entity (D);
9383 Set_Current_Entity (D);
9384 Set_Is_Immediately_Visible (D);
9385 Set_Homonym (D, Prev);
9387 -- Ada 2005 (AI-230): Access discriminant allowed in
9388 -- non-limited record types.
9390 if Ada_Version < Ada_05 then
9392 -- This restriction gets applied to the full type here. It
9393 -- has already been applied earlier to the partial view.
9395 Check_Access_Discriminant_Requires_Limited (Parent (D), N);
9396 end if;
9398 Next_Discriminant (D);
9399 end loop;
9400 end;
9402 elsif Present (Discriminant_Specifications (N)) then
9403 Process_Discriminants (N, Prev);
9404 end if;
9405 end Check_Or_Process_Discriminants;
9407 ----------------------
9408 -- Check_Real_Bound --
9409 ----------------------
9411 procedure Check_Real_Bound (Bound : Node_Id) is
9412 begin
9413 if not Is_Real_Type (Etype (Bound)) then
9414 Error_Msg_N
9415 ("bound in real type definition must be of real type", Bound);
9417 elsif not Is_OK_Static_Expression (Bound) then
9418 Flag_Non_Static_Expr
9419 ("non-static expression used for real type bound!", Bound);
9421 else
9422 return;
9423 end if;
9425 Rewrite
9426 (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
9427 Analyze (Bound);
9428 Resolve (Bound, Standard_Float);
9429 end Check_Real_Bound;
9431 ------------------------------
9432 -- Complete_Private_Subtype --
9433 ------------------------------
9435 procedure Complete_Private_Subtype
9436 (Priv : Entity_Id;
9437 Full : Entity_Id;
9438 Full_Base : Entity_Id;
9439 Related_Nod : Node_Id)
9441 Save_Next_Entity : Entity_Id;
9442 Save_Homonym : Entity_Id;
9444 begin
9445 -- Set semantic attributes for (implicit) private subtype completion.
9446 -- If the full type has no discriminants, then it is a copy of the full
9447 -- view of the base. Otherwise, it is a subtype of the base with a
9448 -- possible discriminant constraint. Save and restore the original
9449 -- Next_Entity field of full to ensure that the calls to Copy_Node
9450 -- do not corrupt the entity chain.
9452 -- Note that the type of the full view is the same entity as the type of
9453 -- the partial view. In this fashion, the subtype has access to the
9454 -- correct view of the parent.
9456 Save_Next_Entity := Next_Entity (Full);
9457 Save_Homonym := Homonym (Priv);
9459 case Ekind (Full_Base) is
9460 when E_Record_Type |
9461 E_Record_Subtype |
9462 Class_Wide_Kind |
9463 Private_Kind |
9464 Task_Kind |
9465 Protected_Kind =>
9466 Copy_Node (Priv, Full);
9468 Set_Has_Discriminants (Full, Has_Discriminants (Full_Base));
9469 Set_First_Entity (Full, First_Entity (Full_Base));
9470 Set_Last_Entity (Full, Last_Entity (Full_Base));
9472 when others =>
9473 Copy_Node (Full_Base, Full);
9474 Set_Chars (Full, Chars (Priv));
9475 Conditional_Delay (Full, Priv);
9476 Set_Sloc (Full, Sloc (Priv));
9477 end case;
9479 Set_Next_Entity (Full, Save_Next_Entity);
9480 Set_Homonym (Full, Save_Homonym);
9481 Set_Associated_Node_For_Itype (Full, Related_Nod);
9483 -- Set common attributes for all subtypes
9485 Set_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
9487 -- The Etype of the full view is inconsistent. Gigi needs to see the
9488 -- structural full view, which is what the current scheme gives:
9489 -- the Etype of the full view is the etype of the full base. However,
9490 -- if the full base is a derived type, the full view then looks like
9491 -- a subtype of the parent, not a subtype of the full base. If instead
9492 -- we write:
9494 -- Set_Etype (Full, Full_Base);
9496 -- then we get inconsistencies in the front-end (confusion between
9497 -- views). Several outstanding bugs are related to this ???
9499 Set_Is_First_Subtype (Full, False);
9500 Set_Scope (Full, Scope (Priv));
9501 Set_Size_Info (Full, Full_Base);
9502 Set_RM_Size (Full, RM_Size (Full_Base));
9503 Set_Is_Itype (Full);
9505 -- A subtype of a private-type-without-discriminants, whose full-view
9506 -- has discriminants with default expressions, is not constrained!
9508 if not Has_Discriminants (Priv) then
9509 Set_Is_Constrained (Full, Is_Constrained (Full_Base));
9511 if Has_Discriminants (Full_Base) then
9512 Set_Discriminant_Constraint
9513 (Full, Discriminant_Constraint (Full_Base));
9515 -- The partial view may have been indefinite, the full view
9516 -- might not be.
9518 Set_Has_Unknown_Discriminants
9519 (Full, Has_Unknown_Discriminants (Full_Base));
9520 end if;
9521 end if;
9523 Set_First_Rep_Item (Full, First_Rep_Item (Full_Base));
9524 Set_Depends_On_Private (Full, Has_Private_Component (Full));
9526 -- Freeze the private subtype entity if its parent is delayed, and not
9527 -- already frozen. We skip this processing if the type is an anonymous
9528 -- subtype of a record component, or is the corresponding record of a
9529 -- protected type, since ???
9531 if not Is_Type (Scope (Full)) then
9532 Set_Has_Delayed_Freeze (Full,
9533 Has_Delayed_Freeze (Full_Base)
9534 and then (not Is_Frozen (Full_Base)));
9535 end if;
9537 Set_Freeze_Node (Full, Empty);
9538 Set_Is_Frozen (Full, False);
9539 Set_Full_View (Priv, Full);
9541 if Has_Discriminants (Full) then
9542 Set_Stored_Constraint_From_Discriminant_Constraint (Full);
9543 Set_Stored_Constraint (Priv, Stored_Constraint (Full));
9545 if Has_Unknown_Discriminants (Full) then
9546 Set_Discriminant_Constraint (Full, No_Elist);
9547 end if;
9548 end if;
9550 if Ekind (Full_Base) = E_Record_Type
9551 and then Has_Discriminants (Full_Base)
9552 and then Has_Discriminants (Priv) -- might not, if errors
9553 and then not Has_Unknown_Discriminants (Priv)
9554 and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
9555 then
9556 Create_Constrained_Components
9557 (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
9559 -- If the full base is itself derived from private, build a congruent
9560 -- subtype of its underlying type, for use by the back end. For a
9561 -- constrained record component, the declaration cannot be placed on
9562 -- the component list, but it must nevertheless be built an analyzed, to
9563 -- supply enough information for Gigi to compute the size of component.
9565 elsif Ekind (Full_Base) in Private_Kind
9566 and then Is_Derived_Type (Full_Base)
9567 and then Has_Discriminants (Full_Base)
9568 and then (Ekind (Current_Scope) /= E_Record_Subtype)
9569 then
9570 if not Is_Itype (Priv)
9571 and then
9572 Nkind (Subtype_Indication (Parent (Priv))) = N_Subtype_Indication
9573 then
9574 Build_Underlying_Full_View
9575 (Parent (Priv), Full, Etype (Full_Base));
9577 elsif Nkind (Related_Nod) = N_Component_Declaration then
9578 Build_Underlying_Full_View (Related_Nod, Full, Etype (Full_Base));
9579 end if;
9581 elsif Is_Record_Type (Full_Base) then
9583 -- Show Full is simply a renaming of Full_Base
9585 Set_Cloned_Subtype (Full, Full_Base);
9586 end if;
9588 -- It is unsafe to share to bounds of a scalar type, because the Itype
9589 -- is elaborated on demand, and if a bound is non-static then different
9590 -- orders of elaboration in different units will lead to different
9591 -- external symbols.
9593 if Is_Scalar_Type (Full_Base) then
9594 Set_Scalar_Range (Full,
9595 Make_Range (Sloc (Related_Nod),
9596 Low_Bound =>
9597 Duplicate_Subexpr_No_Checks (Type_Low_Bound (Full_Base)),
9598 High_Bound =>
9599 Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
9601 -- This completion inherits the bounds of the full parent, but if
9602 -- the parent is an unconstrained floating point type, so is the
9603 -- completion.
9605 if Is_Floating_Point_Type (Full_Base) then
9606 Set_Includes_Infinities
9607 (Scalar_Range (Full), Has_Infinities (Full_Base));
9608 end if;
9609 end if;
9611 -- ??? It seems that a lot of fields are missing that should be copied
9612 -- from Full_Base to Full. Here are some that are introduced in a
9613 -- non-disruptive way but a cleanup is necessary.
9615 if Is_Tagged_Type (Full_Base) then
9616 Set_Is_Tagged_Type (Full);
9617 Set_Primitive_Operations (Full, Primitive_Operations (Full_Base));
9619 -- Inherit class_wide type of full_base in case the partial view was
9620 -- not tagged. Otherwise it has already been created when the private
9621 -- subtype was analyzed.
9623 if No (Class_Wide_Type (Full)) then
9624 Set_Class_Wide_Type (Full, Class_Wide_Type (Full_Base));
9625 end if;
9627 -- If this is a subtype of a protected or task type, constrain its
9628 -- corresponding record, unless this is a subtype without constraints,
9629 -- i.e. a simple renaming as with an actual subtype in an instance.
9631 elsif Is_Concurrent_Type (Full_Base) then
9632 if Has_Discriminants (Full)
9633 and then Present (Corresponding_Record_Type (Full_Base))
9634 and then
9635 not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
9636 then
9637 Set_Corresponding_Record_Type (Full,
9638 Constrain_Corresponding_Record
9639 (Full, Corresponding_Record_Type (Full_Base),
9640 Related_Nod, Full_Base));
9642 else
9643 Set_Corresponding_Record_Type (Full,
9644 Corresponding_Record_Type (Full_Base));
9645 end if;
9646 end if;
9647 end Complete_Private_Subtype;
9649 ----------------------------
9650 -- Constant_Redeclaration --
9651 ----------------------------
9653 procedure Constant_Redeclaration
9654 (Id : Entity_Id;
9655 N : Node_Id;
9656 T : out Entity_Id)
9658 Prev : constant Entity_Id := Current_Entity_In_Scope (Id);
9659 Obj_Def : constant Node_Id := Object_Definition (N);
9660 New_T : Entity_Id;
9662 procedure Check_Possible_Deferred_Completion
9663 (Prev_Id : Entity_Id;
9664 Prev_Obj_Def : Node_Id;
9665 Curr_Obj_Def : Node_Id);
9666 -- Determine whether the two object definitions describe the partial
9667 -- and the full view of a constrained deferred constant. Generate
9668 -- a subtype for the full view and verify that it statically matches
9669 -- the subtype of the partial view.
9671 procedure Check_Recursive_Declaration (Typ : Entity_Id);
9672 -- If deferred constant is an access type initialized with an allocator,
9673 -- check whether there is an illegal recursion in the definition,
9674 -- through a default value of some record subcomponent. This is normally
9675 -- detected when generating init procs, but requires this additional
9676 -- mechanism when expansion is disabled.
9678 ----------------------------------------
9679 -- Check_Possible_Deferred_Completion --
9680 ----------------------------------------
9682 procedure Check_Possible_Deferred_Completion
9683 (Prev_Id : Entity_Id;
9684 Prev_Obj_Def : Node_Id;
9685 Curr_Obj_Def : Node_Id)
9687 begin
9688 if Nkind (Prev_Obj_Def) = N_Subtype_Indication
9689 and then Present (Constraint (Prev_Obj_Def))
9690 and then Nkind (Curr_Obj_Def) = N_Subtype_Indication
9691 and then Present (Constraint (Curr_Obj_Def))
9692 then
9693 declare
9694 Loc : constant Source_Ptr := Sloc (N);
9695 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
9696 Decl : constant Node_Id :=
9697 Make_Subtype_Declaration (Loc,
9698 Defining_Identifier => Def_Id,
9699 Subtype_Indication =>
9700 Relocate_Node (Curr_Obj_Def));
9702 begin
9703 Insert_Before_And_Analyze (N, Decl);
9704 Set_Etype (Id, Def_Id);
9706 if not Subtypes_Statically_Match (Etype (Prev_Id), Def_Id) then
9707 Error_Msg_Sloc := Sloc (Prev_Id);
9708 Error_Msg_N ("subtype does not statically match deferred " &
9709 "declaration#", N);
9710 end if;
9711 end;
9712 end if;
9713 end Check_Possible_Deferred_Completion;
9715 ---------------------------------
9716 -- Check_Recursive_Declaration --
9717 ---------------------------------
9719 procedure Check_Recursive_Declaration (Typ : Entity_Id) is
9720 Comp : Entity_Id;
9722 begin
9723 if Is_Record_Type (Typ) then
9724 Comp := First_Component (Typ);
9725 while Present (Comp) loop
9726 if Comes_From_Source (Comp) then
9727 if Present (Expression (Parent (Comp)))
9728 and then Is_Entity_Name (Expression (Parent (Comp)))
9729 and then Entity (Expression (Parent (Comp))) = Prev
9730 then
9731 Error_Msg_Sloc := Sloc (Parent (Comp));
9732 Error_Msg_NE
9733 ("illegal circularity with declaration for&#",
9734 N, Comp);
9735 return;
9737 elsif Is_Record_Type (Etype (Comp)) then
9738 Check_Recursive_Declaration (Etype (Comp));
9739 end if;
9740 end if;
9742 Next_Component (Comp);
9743 end loop;
9744 end if;
9745 end Check_Recursive_Declaration;
9747 -- Start of processing for Constant_Redeclaration
9749 begin
9750 if Nkind (Parent (Prev)) = N_Object_Declaration then
9751 if Nkind (Object_Definition
9752 (Parent (Prev))) = N_Subtype_Indication
9753 then
9754 -- Find type of new declaration. The constraints of the two
9755 -- views must match statically, but there is no point in
9756 -- creating an itype for the full view.
9758 if Nkind (Obj_Def) = N_Subtype_Indication then
9759 Find_Type (Subtype_Mark (Obj_Def));
9760 New_T := Entity (Subtype_Mark (Obj_Def));
9762 else
9763 Find_Type (Obj_Def);
9764 New_T := Entity (Obj_Def);
9765 end if;
9767 T := Etype (Prev);
9769 else
9770 -- The full view may impose a constraint, even if the partial
9771 -- view does not, so construct the subtype.
9773 New_T := Find_Type_Of_Object (Obj_Def, N);
9774 T := New_T;
9775 end if;
9777 else
9778 -- Current declaration is illegal, diagnosed below in Enter_Name
9780 T := Empty;
9781 New_T := Any_Type;
9782 end if;
9784 -- If previous full declaration or a renaming declaration exists, or if
9785 -- a homograph is present, let Enter_Name handle it, either with an
9786 -- error or with the removal of an overridden implicit subprogram.
9788 if Ekind (Prev) /= E_Constant
9789 or else Nkind (Parent (Prev)) = N_Object_Renaming_Declaration
9790 or else Present (Expression (Parent (Prev)))
9791 or else Present (Full_View (Prev))
9792 then
9793 Enter_Name (Id);
9795 -- Verify that types of both declarations match, or else that both types
9796 -- are anonymous access types whose designated subtypes statically match
9797 -- (as allowed in Ada 2005 by AI-385).
9799 elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
9800 and then
9801 (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
9802 or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
9803 or else Is_Access_Constant (Etype (New_T)) /=
9804 Is_Access_Constant (Etype (Prev))
9805 or else Can_Never_Be_Null (Etype (New_T)) /=
9806 Can_Never_Be_Null (Etype (Prev))
9807 or else Null_Exclusion_Present (Parent (Prev)) /=
9808 Null_Exclusion_Present (Parent (Id))
9809 or else not Subtypes_Statically_Match
9810 (Designated_Type (Etype (Prev)),
9811 Designated_Type (Etype (New_T))))
9812 then
9813 Error_Msg_Sloc := Sloc (Prev);
9814 Error_Msg_N ("type does not match declaration#", N);
9815 Set_Full_View (Prev, Id);
9816 Set_Etype (Id, Any_Type);
9818 elsif
9819 Null_Exclusion_Present (Parent (Prev))
9820 and then not Null_Exclusion_Present (N)
9821 then
9822 Error_Msg_Sloc := Sloc (Prev);
9823 Error_Msg_N ("null-exclusion does not match declaration#", N);
9824 Set_Full_View (Prev, Id);
9825 Set_Etype (Id, Any_Type);
9827 -- If so, process the full constant declaration
9829 else
9830 -- RM 7.4 (6): If the subtype defined by the subtype_indication in
9831 -- the deferred declaration is constrained, then the subtype defined
9832 -- by the subtype_indication in the full declaration shall match it
9833 -- statically.
9835 Check_Possible_Deferred_Completion
9836 (Prev_Id => Prev,
9837 Prev_Obj_Def => Object_Definition (Parent (Prev)),
9838 Curr_Obj_Def => Obj_Def);
9840 Set_Full_View (Prev, Id);
9841 Set_Is_Public (Id, Is_Public (Prev));
9842 Set_Is_Internal (Id);
9843 Append_Entity (Id, Current_Scope);
9845 -- Check ALIASED present if present before (RM 7.4(7))
9847 if Is_Aliased (Prev)
9848 and then not Aliased_Present (N)
9849 then
9850 Error_Msg_Sloc := Sloc (Prev);
9851 Error_Msg_N ("ALIASED required (see declaration#)", N);
9852 end if;
9854 -- Check that placement is in private part and that the incomplete
9855 -- declaration appeared in the visible part.
9857 if Ekind (Current_Scope) = E_Package
9858 and then not In_Private_Part (Current_Scope)
9859 then
9860 Error_Msg_Sloc := Sloc (Prev);
9861 Error_Msg_N
9862 ("full constant for declaration#"
9863 & " must be in private part", N);
9865 elsif Ekind (Current_Scope) = E_Package
9866 and then
9867 List_Containing (Parent (Prev)) /=
9868 Visible_Declarations
9869 (Specification (Unit_Declaration_Node (Current_Scope)))
9870 then
9871 Error_Msg_N
9872 ("deferred constant must be declared in visible part",
9873 Parent (Prev));
9874 end if;
9876 if Is_Access_Type (T)
9877 and then Nkind (Expression (N)) = N_Allocator
9878 then
9879 Check_Recursive_Declaration (Designated_Type (T));
9880 end if;
9881 end if;
9882 end Constant_Redeclaration;
9884 ----------------------
9885 -- Constrain_Access --
9886 ----------------------
9888 procedure Constrain_Access
9889 (Def_Id : in out Entity_Id;
9890 S : Node_Id;
9891 Related_Nod : Node_Id)
9893 T : constant Entity_Id := Entity (Subtype_Mark (S));
9894 Desig_Type : constant Entity_Id := Designated_Type (T);
9895 Desig_Subtype : Entity_Id := Create_Itype (E_Void, Related_Nod);
9896 Constraint_OK : Boolean := True;
9898 function Has_Defaulted_Discriminants (Typ : Entity_Id) return Boolean;
9899 -- Simple predicate to test for defaulted discriminants
9900 -- Shouldn't this be in sem_util???
9902 ---------------------------------
9903 -- Has_Defaulted_Discriminants --
9904 ---------------------------------
9906 function Has_Defaulted_Discriminants (Typ : Entity_Id) return Boolean is
9907 begin
9908 return Has_Discriminants (Typ)
9909 and then Present (First_Discriminant (Typ))
9910 and then Present
9911 (Discriminant_Default_Value (First_Discriminant (Typ)));
9912 end Has_Defaulted_Discriminants;
9914 -- Start of processing for Constrain_Access
9916 begin
9917 if Is_Array_Type (Desig_Type) then
9918 Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
9920 elsif (Is_Record_Type (Desig_Type)
9921 or else Is_Incomplete_Or_Private_Type (Desig_Type))
9922 and then not Is_Constrained (Desig_Type)
9923 then
9924 -- ??? The following code is a temporary kludge to ignore a
9925 -- discriminant constraint on access type if it is constraining
9926 -- the current record. Avoid creating the implicit subtype of the
9927 -- record we are currently compiling since right now, we cannot
9928 -- handle these. For now, just return the access type itself.
9930 if Desig_Type = Current_Scope
9931 and then No (Def_Id)
9932 then
9933 Set_Ekind (Desig_Subtype, E_Record_Subtype);
9934 Def_Id := Entity (Subtype_Mark (S));
9936 -- This call added to ensure that the constraint is analyzed
9937 -- (needed for a B test). Note that we still return early from
9938 -- this procedure to avoid recursive processing. ???
9940 Constrain_Discriminated_Type
9941 (Desig_Subtype, S, Related_Nod, For_Access => True);
9942 return;
9943 end if;
9945 if (Ekind (T) = E_General_Access_Type
9946 or else Ada_Version >= Ada_05)
9947 and then Has_Private_Declaration (Desig_Type)
9948 and then In_Open_Scopes (Scope (Desig_Type))
9949 and then Has_Discriminants (Desig_Type)
9950 then
9951 -- Enforce rule that the constraint is illegal if there is
9952 -- an unconstrained view of the designated type. This means
9953 -- that the partial view (either a private type declaration or
9954 -- a derivation from a private type) has no discriminants.
9955 -- (Defect Report 8652/0008, Technical Corrigendum 1, checked
9956 -- by ACATS B371001).
9958 -- Rule updated for Ada 2005: the private type is said to have
9959 -- a constrained partial view, given that objects of the type
9960 -- can be declared. Furthermore, the rule applies to all access
9961 -- types, unlike the rule concerning default discriminants.
9963 declare
9964 Pack : constant Node_Id :=
9965 Unit_Declaration_Node (Scope (Desig_Type));
9966 Decls : List_Id;
9967 Decl : Node_Id;
9969 begin
9970 if Nkind (Pack) = N_Package_Declaration then
9971 Decls := Visible_Declarations (Specification (Pack));
9972 Decl := First (Decls);
9973 while Present (Decl) loop
9974 if (Nkind (Decl) = N_Private_Type_Declaration
9975 and then
9976 Chars (Defining_Identifier (Decl)) =
9977 Chars (Desig_Type))
9979 or else
9980 (Nkind (Decl) = N_Full_Type_Declaration
9981 and then
9982 Chars (Defining_Identifier (Decl)) =
9983 Chars (Desig_Type)
9984 and then Is_Derived_Type (Desig_Type)
9985 and then
9986 Has_Private_Declaration (Etype (Desig_Type)))
9987 then
9988 if No (Discriminant_Specifications (Decl)) then
9989 Error_Msg_N
9990 ("cannot constrain general access type if " &
9991 "designated type has constrained partial view",
9993 end if;
9995 exit;
9996 end if;
9998 Next (Decl);
9999 end loop;
10000 end if;
10001 end;
10002 end if;
10004 Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
10005 For_Access => True);
10007 elsif (Is_Task_Type (Desig_Type)
10008 or else Is_Protected_Type (Desig_Type))
10009 and then not Is_Constrained (Desig_Type)
10010 then
10011 Constrain_Concurrent
10012 (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
10014 else
10015 Error_Msg_N ("invalid constraint on access type", S);
10016 Desig_Subtype := Desig_Type; -- Ignore invalid constraint.
10017 Constraint_OK := False;
10018 end if;
10020 if No (Def_Id) then
10021 Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
10022 else
10023 Set_Ekind (Def_Id, E_Access_Subtype);
10024 end if;
10026 if Constraint_OK then
10027 Set_Etype (Def_Id, Base_Type (T));
10029 if Is_Private_Type (Desig_Type) then
10030 Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
10031 end if;
10032 else
10033 Set_Etype (Def_Id, Any_Type);
10034 end if;
10036 Set_Size_Info (Def_Id, T);
10037 Set_Is_Constrained (Def_Id, Constraint_OK);
10038 Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
10039 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
10040 Set_Is_Access_Constant (Def_Id, Is_Access_Constant (T));
10042 Conditional_Delay (Def_Id, T);
10044 -- AI-363 : Subtypes of general access types whose designated types have
10045 -- default discriminants are disallowed. In instances, the rule has to
10046 -- be checked against the actual, of which T is the subtype. In a
10047 -- generic body, the rule is checked assuming that the actual type has
10048 -- defaulted discriminants.
10050 if Ada_Version >= Ada_05 or else Warn_On_Ada_2005_Compatibility then
10051 if Ekind (Base_Type (T)) = E_General_Access_Type
10052 and then Has_Defaulted_Discriminants (Desig_Type)
10053 then
10054 if Ada_Version < Ada_05 then
10055 Error_Msg_N
10056 ("access subtype of general access type would not " &
10057 "be allowed in Ada 2005?", S);
10058 else
10059 Error_Msg_N
10060 ("access subype of general access type not allowed", S);
10061 end if;
10063 Error_Msg_N ("\discriminants have defaults", S);
10065 elsif Is_Access_Type (T)
10066 and then Is_Generic_Type (Desig_Type)
10067 and then Has_Discriminants (Desig_Type)
10068 and then In_Package_Body (Current_Scope)
10069 then
10070 if Ada_Version < Ada_05 then
10071 Error_Msg_N
10072 ("access subtype would not be allowed in generic body " &
10073 "in Ada 2005?", S);
10074 else
10075 Error_Msg_N
10076 ("access subtype not allowed in generic body", S);
10077 end if;
10079 Error_Msg_N
10080 ("\designated type is a discriminated formal", S);
10081 end if;
10082 end if;
10083 end Constrain_Access;
10085 ---------------------
10086 -- Constrain_Array --
10087 ---------------------
10089 procedure Constrain_Array
10090 (Def_Id : in out Entity_Id;
10091 SI : Node_Id;
10092 Related_Nod : Node_Id;
10093 Related_Id : Entity_Id;
10094 Suffix : Character)
10096 C : constant Node_Id := Constraint (SI);
10097 Number_Of_Constraints : Nat := 0;
10098 Index : Node_Id;
10099 S, T : Entity_Id;
10100 Constraint_OK : Boolean := True;
10102 begin
10103 T := Entity (Subtype_Mark (SI));
10105 if Ekind (T) in Access_Kind then
10106 T := Designated_Type (T);
10107 end if;
10109 -- If an index constraint follows a subtype mark in a subtype indication
10110 -- then the type or subtype denoted by the subtype mark must not already
10111 -- impose an index constraint. The subtype mark must denote either an
10112 -- unconstrained array type or an access type whose designated type
10113 -- is such an array type... (RM 3.6.1)
10115 if Is_Constrained (T) then
10116 Error_Msg_N ("array type is already constrained", Subtype_Mark (SI));
10117 Constraint_OK := False;
10119 else
10120 S := First (Constraints (C));
10121 while Present (S) loop
10122 Number_Of_Constraints := Number_Of_Constraints + 1;
10123 Next (S);
10124 end loop;
10126 -- In either case, the index constraint must provide a discrete
10127 -- range for each index of the array type and the type of each
10128 -- discrete range must be the same as that of the corresponding
10129 -- index. (RM 3.6.1)
10131 if Number_Of_Constraints /= Number_Dimensions (T) then
10132 Error_Msg_NE ("incorrect number of index constraints for }", C, T);
10133 Constraint_OK := False;
10135 else
10136 S := First (Constraints (C));
10137 Index := First_Index (T);
10138 Analyze (Index);
10140 -- Apply constraints to each index type
10142 for J in 1 .. Number_Of_Constraints loop
10143 Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
10144 Next (Index);
10145 Next (S);
10146 end loop;
10148 end if;
10149 end if;
10151 if No (Def_Id) then
10152 Def_Id :=
10153 Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
10154 Set_Parent (Def_Id, Related_Nod);
10156 else
10157 Set_Ekind (Def_Id, E_Array_Subtype);
10158 end if;
10160 Set_Size_Info (Def_Id, (T));
10161 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10162 Set_Etype (Def_Id, Base_Type (T));
10164 if Constraint_OK then
10165 Set_First_Index (Def_Id, First (Constraints (C)));
10166 else
10167 Set_First_Index (Def_Id, First_Index (T));
10168 end if;
10170 Set_Is_Constrained (Def_Id, True);
10171 Set_Is_Aliased (Def_Id, Is_Aliased (T));
10172 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
10174 Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
10175 Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
10177 -- A subtype does not inherit the packed_array_type of is parent. We
10178 -- need to initialize the attribute because if Def_Id is previously
10179 -- analyzed through a limited_with clause, it will have the attributes
10180 -- of an incomplete type, one of which is an Elist that overlaps the
10181 -- Packed_Array_Type field.
10183 Set_Packed_Array_Type (Def_Id, Empty);
10185 -- Build a freeze node if parent still needs one. Also make sure that
10186 -- the Depends_On_Private status is set because the subtype will need
10187 -- reprocessing at the time the base type does, and also we must set a
10188 -- conditional delay.
10190 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
10191 Conditional_Delay (Def_Id, T);
10192 end Constrain_Array;
10194 ------------------------------
10195 -- Constrain_Component_Type --
10196 ------------------------------
10198 function Constrain_Component_Type
10199 (Comp : Entity_Id;
10200 Constrained_Typ : Entity_Id;
10201 Related_Node : Node_Id;
10202 Typ : Entity_Id;
10203 Constraints : Elist_Id) return Entity_Id
10205 Loc : constant Source_Ptr := Sloc (Constrained_Typ);
10206 Compon_Type : constant Entity_Id := Etype (Comp);
10208 function Build_Constrained_Array_Type
10209 (Old_Type : Entity_Id) return Entity_Id;
10210 -- If Old_Type is an array type, one of whose indices is constrained
10211 -- by a discriminant, build an Itype whose constraint replaces the
10212 -- discriminant with its value in the constraint.
10214 function Build_Constrained_Discriminated_Type
10215 (Old_Type : Entity_Id) return Entity_Id;
10216 -- Ditto for record components
10218 function Build_Constrained_Access_Type
10219 (Old_Type : Entity_Id) return Entity_Id;
10220 -- Ditto for access types. Makes use of previous two functions, to
10221 -- constrain designated type.
10223 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id;
10224 -- T is an array or discriminated type, C is a list of constraints
10225 -- that apply to T. This routine builds the constrained subtype.
10227 function Is_Discriminant (Expr : Node_Id) return Boolean;
10228 -- Returns True if Expr is a discriminant
10230 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id;
10231 -- Find the value of discriminant Discrim in Constraint
10233 -----------------------------------
10234 -- Build_Constrained_Access_Type --
10235 -----------------------------------
10237 function Build_Constrained_Access_Type
10238 (Old_Type : Entity_Id) return Entity_Id
10240 Desig_Type : constant Entity_Id := Designated_Type (Old_Type);
10241 Itype : Entity_Id;
10242 Desig_Subtype : Entity_Id;
10243 Scop : Entity_Id;
10245 begin
10246 -- if the original access type was not embedded in the enclosing
10247 -- type definition, there is no need to produce a new access
10248 -- subtype. In fact every access type with an explicit constraint
10249 -- generates an itype whose scope is the enclosing record.
10251 if not Is_Type (Scope (Old_Type)) then
10252 return Old_Type;
10254 elsif Is_Array_Type (Desig_Type) then
10255 Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
10257 elsif Has_Discriminants (Desig_Type) then
10259 -- This may be an access type to an enclosing record type for
10260 -- which we are constructing the constrained components. Return
10261 -- the enclosing record subtype. This is not always correct,
10262 -- but avoids infinite recursion. ???
10264 Desig_Subtype := Any_Type;
10266 for J in reverse 0 .. Scope_Stack.Last loop
10267 Scop := Scope_Stack.Table (J).Entity;
10269 if Is_Type (Scop)
10270 and then Base_Type (Scop) = Base_Type (Desig_Type)
10271 then
10272 Desig_Subtype := Scop;
10273 end if;
10275 exit when not Is_Type (Scop);
10276 end loop;
10278 if Desig_Subtype = Any_Type then
10279 Desig_Subtype :=
10280 Build_Constrained_Discriminated_Type (Desig_Type);
10281 end if;
10283 else
10284 return Old_Type;
10285 end if;
10287 if Desig_Subtype /= Desig_Type then
10289 -- The Related_Node better be here or else we won't be able
10290 -- to attach new itypes to a node in the tree.
10292 pragma Assert (Present (Related_Node));
10294 Itype := Create_Itype (E_Access_Subtype, Related_Node);
10296 Set_Etype (Itype, Base_Type (Old_Type));
10297 Set_Size_Info (Itype, (Old_Type));
10298 Set_Directly_Designated_Type (Itype, Desig_Subtype);
10299 Set_Depends_On_Private (Itype, Has_Private_Component
10300 (Old_Type));
10301 Set_Is_Access_Constant (Itype, Is_Access_Constant
10302 (Old_Type));
10304 -- The new itype needs freezing when it depends on a not frozen
10305 -- type and the enclosing subtype needs freezing.
10307 if Has_Delayed_Freeze (Constrained_Typ)
10308 and then not Is_Frozen (Constrained_Typ)
10309 then
10310 Conditional_Delay (Itype, Base_Type (Old_Type));
10311 end if;
10313 return Itype;
10315 else
10316 return Old_Type;
10317 end if;
10318 end Build_Constrained_Access_Type;
10320 ----------------------------------
10321 -- Build_Constrained_Array_Type --
10322 ----------------------------------
10324 function Build_Constrained_Array_Type
10325 (Old_Type : Entity_Id) return Entity_Id
10327 Lo_Expr : Node_Id;
10328 Hi_Expr : Node_Id;
10329 Old_Index : Node_Id;
10330 Range_Node : Node_Id;
10331 Constr_List : List_Id;
10333 Need_To_Create_Itype : Boolean := False;
10335 begin
10336 Old_Index := First_Index (Old_Type);
10337 while Present (Old_Index) loop
10338 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
10340 if Is_Discriminant (Lo_Expr)
10341 or else Is_Discriminant (Hi_Expr)
10342 then
10343 Need_To_Create_Itype := True;
10344 end if;
10346 Next_Index (Old_Index);
10347 end loop;
10349 if Need_To_Create_Itype then
10350 Constr_List := New_List;
10352 Old_Index := First_Index (Old_Type);
10353 while Present (Old_Index) loop
10354 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
10356 if Is_Discriminant (Lo_Expr) then
10357 Lo_Expr := Get_Discr_Value (Lo_Expr);
10358 end if;
10360 if Is_Discriminant (Hi_Expr) then
10361 Hi_Expr := Get_Discr_Value (Hi_Expr);
10362 end if;
10364 Range_Node :=
10365 Make_Range
10366 (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
10368 Append (Range_Node, To => Constr_List);
10370 Next_Index (Old_Index);
10371 end loop;
10373 return Build_Subtype (Old_Type, Constr_List);
10375 else
10376 return Old_Type;
10377 end if;
10378 end Build_Constrained_Array_Type;
10380 ------------------------------------------
10381 -- Build_Constrained_Discriminated_Type --
10382 ------------------------------------------
10384 function Build_Constrained_Discriminated_Type
10385 (Old_Type : Entity_Id) return Entity_Id
10387 Expr : Node_Id;
10388 Constr_List : List_Id;
10389 Old_Constraint : Elmt_Id;
10391 Need_To_Create_Itype : Boolean := False;
10393 begin
10394 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
10395 while Present (Old_Constraint) loop
10396 Expr := Node (Old_Constraint);
10398 if Is_Discriminant (Expr) then
10399 Need_To_Create_Itype := True;
10400 end if;
10402 Next_Elmt (Old_Constraint);
10403 end loop;
10405 if Need_To_Create_Itype then
10406 Constr_List := New_List;
10408 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
10409 while Present (Old_Constraint) loop
10410 Expr := Node (Old_Constraint);
10412 if Is_Discriminant (Expr) then
10413 Expr := Get_Discr_Value (Expr);
10414 end if;
10416 Append (New_Copy_Tree (Expr), To => Constr_List);
10418 Next_Elmt (Old_Constraint);
10419 end loop;
10421 return Build_Subtype (Old_Type, Constr_List);
10423 else
10424 return Old_Type;
10425 end if;
10426 end Build_Constrained_Discriminated_Type;
10428 -------------------
10429 -- Build_Subtype --
10430 -------------------
10432 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id is
10433 Indic : Node_Id;
10434 Subtyp_Decl : Node_Id;
10435 Def_Id : Entity_Id;
10436 Btyp : Entity_Id := Base_Type (T);
10438 begin
10439 -- The Related_Node better be here or else we won't be able to
10440 -- attach new itypes to a node in the tree.
10442 pragma Assert (Present (Related_Node));
10444 -- If the view of the component's type is incomplete or private
10445 -- with unknown discriminants, then the constraint must be applied
10446 -- to the full type.
10448 if Has_Unknown_Discriminants (Btyp)
10449 and then Present (Underlying_Type (Btyp))
10450 then
10451 Btyp := Underlying_Type (Btyp);
10452 end if;
10454 Indic :=
10455 Make_Subtype_Indication (Loc,
10456 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
10457 Constraint => Make_Index_Or_Discriminant_Constraint (Loc, C));
10459 Def_Id := Create_Itype (Ekind (T), Related_Node);
10461 Subtyp_Decl :=
10462 Make_Subtype_Declaration (Loc,
10463 Defining_Identifier => Def_Id,
10464 Subtype_Indication => Indic);
10466 Set_Parent (Subtyp_Decl, Parent (Related_Node));
10468 -- Itypes must be analyzed with checks off (see package Itypes)
10470 Analyze (Subtyp_Decl, Suppress => All_Checks);
10472 return Def_Id;
10473 end Build_Subtype;
10475 ---------------------
10476 -- Get_Discr_Value --
10477 ---------------------
10479 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id is
10480 D : Entity_Id;
10481 E : Elmt_Id;
10483 begin
10484 -- The discriminant may be declared for the type, in which case we
10485 -- find it by iterating over the list of discriminants. If the
10486 -- discriminant is inherited from a parent type, it appears as the
10487 -- corresponding discriminant of the current type. This will be the
10488 -- case when constraining an inherited component whose constraint is
10489 -- given by a discriminant of the parent.
10491 D := First_Discriminant (Typ);
10492 E := First_Elmt (Constraints);
10494 while Present (D) loop
10495 if D = Entity (Discrim)
10496 or else D = CR_Discriminant (Entity (Discrim))
10497 or else Corresponding_Discriminant (D) = Entity (Discrim)
10498 then
10499 return Node (E);
10500 end if;
10502 Next_Discriminant (D);
10503 Next_Elmt (E);
10504 end loop;
10506 -- The corresponding_Discriminant mechanism is incomplete, because
10507 -- the correspondence between new and old discriminants is not one
10508 -- to one: one new discriminant can constrain several old ones. In
10509 -- that case, scan sequentially the stored_constraint, the list of
10510 -- discriminants of the parents, and the constraints.
10511 -- Previous code checked for the present of the Stored_Constraint
10512 -- list for the derived type, but did not use it at all. Should it
10513 -- be present when the component is a discriminated task type?
10515 if Is_Derived_Type (Typ)
10516 and then Scope (Entity (Discrim)) = Etype (Typ)
10517 then
10518 D := First_Discriminant (Etype (Typ));
10519 E := First_Elmt (Constraints);
10520 while Present (D) loop
10521 if D = Entity (Discrim) then
10522 return Node (E);
10523 end if;
10525 Next_Discriminant (D);
10526 Next_Elmt (E);
10527 end loop;
10528 end if;
10530 -- Something is wrong if we did not find the value
10532 raise Program_Error;
10533 end Get_Discr_Value;
10535 ---------------------
10536 -- Is_Discriminant --
10537 ---------------------
10539 function Is_Discriminant (Expr : Node_Id) return Boolean is
10540 Discrim_Scope : Entity_Id;
10542 begin
10543 if Denotes_Discriminant (Expr) then
10544 Discrim_Scope := Scope (Entity (Expr));
10546 -- Either we have a reference to one of Typ's discriminants,
10548 pragma Assert (Discrim_Scope = Typ
10550 -- or to the discriminants of the parent type, in the case
10551 -- of a derivation of a tagged type with variants.
10553 or else Discrim_Scope = Etype (Typ)
10554 or else Full_View (Discrim_Scope) = Etype (Typ)
10556 -- or same as above for the case where the discriminants
10557 -- were declared in Typ's private view.
10559 or else (Is_Private_Type (Discrim_Scope)
10560 and then Chars (Discrim_Scope) = Chars (Typ))
10562 -- or else we are deriving from the full view and the
10563 -- discriminant is declared in the private entity.
10565 or else (Is_Private_Type (Typ)
10566 and then Chars (Discrim_Scope) = Chars (Typ))
10568 -- Or we are constrained the corresponding record of a
10569 -- synchronized type that completes a private declaration.
10571 or else (Is_Concurrent_Record_Type (Typ)
10572 and then
10573 Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
10575 -- or we have a class-wide type, in which case make sure the
10576 -- discriminant found belongs to the root type.
10578 or else (Is_Class_Wide_Type (Typ)
10579 and then Etype (Typ) = Discrim_Scope));
10581 return True;
10582 end if;
10584 -- In all other cases we have something wrong
10586 return False;
10587 end Is_Discriminant;
10589 -- Start of processing for Constrain_Component_Type
10591 begin
10592 if Nkind (Parent (Comp)) = N_Component_Declaration
10593 and then Comes_From_Source (Parent (Comp))
10594 and then Comes_From_Source
10595 (Subtype_Indication (Component_Definition (Parent (Comp))))
10596 and then
10597 Is_Entity_Name
10598 (Subtype_Indication (Component_Definition (Parent (Comp))))
10599 then
10600 return Compon_Type;
10602 elsif Is_Array_Type (Compon_Type) then
10603 return Build_Constrained_Array_Type (Compon_Type);
10605 elsif Has_Discriminants (Compon_Type) then
10606 return Build_Constrained_Discriminated_Type (Compon_Type);
10608 elsif Is_Access_Type (Compon_Type) then
10609 return Build_Constrained_Access_Type (Compon_Type);
10611 else
10612 return Compon_Type;
10613 end if;
10614 end Constrain_Component_Type;
10616 --------------------------
10617 -- Constrain_Concurrent --
10618 --------------------------
10620 -- For concurrent types, the associated record value type carries the same
10621 -- discriminants, so when we constrain a concurrent type, we must constrain
10622 -- the corresponding record type as well.
10624 procedure Constrain_Concurrent
10625 (Def_Id : in out Entity_Id;
10626 SI : Node_Id;
10627 Related_Nod : Node_Id;
10628 Related_Id : Entity_Id;
10629 Suffix : Character)
10631 T_Ent : Entity_Id := Entity (Subtype_Mark (SI));
10632 T_Val : Entity_Id;
10634 begin
10635 if Ekind (T_Ent) in Access_Kind then
10636 T_Ent := Designated_Type (T_Ent);
10637 end if;
10639 T_Val := Corresponding_Record_Type (T_Ent);
10641 if Present (T_Val) then
10643 if No (Def_Id) then
10644 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
10645 end if;
10647 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
10649 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
10650 Set_Corresponding_Record_Type (Def_Id,
10651 Constrain_Corresponding_Record
10652 (Def_Id, T_Val, Related_Nod, Related_Id));
10654 else
10655 -- If there is no associated record, expansion is disabled and this
10656 -- is a generic context. Create a subtype in any case, so that
10657 -- semantic analysis can proceed.
10659 if No (Def_Id) then
10660 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
10661 end if;
10663 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
10664 end if;
10665 end Constrain_Concurrent;
10667 ------------------------------------
10668 -- Constrain_Corresponding_Record --
10669 ------------------------------------
10671 function Constrain_Corresponding_Record
10672 (Prot_Subt : Entity_Id;
10673 Corr_Rec : Entity_Id;
10674 Related_Nod : Node_Id;
10675 Related_Id : Entity_Id) return Entity_Id
10677 T_Sub : constant Entity_Id :=
10678 Create_Itype (E_Record_Subtype, Related_Nod, Related_Id, 'V');
10680 begin
10681 Set_Etype (T_Sub, Corr_Rec);
10682 Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
10683 Set_Is_Constrained (T_Sub, True);
10684 Set_First_Entity (T_Sub, First_Entity (Corr_Rec));
10685 Set_Last_Entity (T_Sub, Last_Entity (Corr_Rec));
10687 -- As elsewhere, we do not want to create a freeze node for this itype
10688 -- if it is created for a constrained component of an enclosing record
10689 -- because references to outer discriminants will appear out of scope.
10691 if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
10692 Conditional_Delay (T_Sub, Corr_Rec);
10693 else
10694 Set_Is_Frozen (T_Sub);
10695 end if;
10697 if Has_Discriminants (Prot_Subt) then -- False only if errors.
10698 Set_Discriminant_Constraint
10699 (T_Sub, Discriminant_Constraint (Prot_Subt));
10700 Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
10701 Create_Constrained_Components
10702 (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
10703 end if;
10705 Set_Depends_On_Private (T_Sub, Has_Private_Component (T_Sub));
10707 return T_Sub;
10708 end Constrain_Corresponding_Record;
10710 -----------------------
10711 -- Constrain_Decimal --
10712 -----------------------
10714 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id) is
10715 T : constant Entity_Id := Entity (Subtype_Mark (S));
10716 C : constant Node_Id := Constraint (S);
10717 Loc : constant Source_Ptr := Sloc (C);
10718 Range_Expr : Node_Id;
10719 Digits_Expr : Node_Id;
10720 Digits_Val : Uint;
10721 Bound_Val : Ureal;
10723 begin
10724 Set_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
10726 if Nkind (C) = N_Range_Constraint then
10727 Range_Expr := Range_Expression (C);
10728 Digits_Val := Digits_Value (T);
10730 else
10731 pragma Assert (Nkind (C) = N_Digits_Constraint);
10732 Digits_Expr := Digits_Expression (C);
10733 Analyze_And_Resolve (Digits_Expr, Any_Integer);
10735 Check_Digits_Expression (Digits_Expr);
10736 Digits_Val := Expr_Value (Digits_Expr);
10738 if Digits_Val > Digits_Value (T) then
10739 Error_Msg_N
10740 ("digits expression is incompatible with subtype", C);
10741 Digits_Val := Digits_Value (T);
10742 end if;
10744 if Present (Range_Constraint (C)) then
10745 Range_Expr := Range_Expression (Range_Constraint (C));
10746 else
10747 Range_Expr := Empty;
10748 end if;
10749 end if;
10751 Set_Etype (Def_Id, Base_Type (T));
10752 Set_Size_Info (Def_Id, (T));
10753 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10754 Set_Delta_Value (Def_Id, Delta_Value (T));
10755 Set_Scale_Value (Def_Id, Scale_Value (T));
10756 Set_Small_Value (Def_Id, Small_Value (T));
10757 Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
10758 Set_Digits_Value (Def_Id, Digits_Val);
10760 -- Manufacture range from given digits value if no range present
10762 if No (Range_Expr) then
10763 Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
10764 Range_Expr :=
10765 Make_Range (Loc,
10766 Low_Bound =>
10767 Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
10768 High_Bound =>
10769 Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
10770 end if;
10772 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
10773 Set_Discrete_RM_Size (Def_Id);
10775 -- Unconditionally delay the freeze, since we cannot set size
10776 -- information in all cases correctly until the freeze point.
10778 Set_Has_Delayed_Freeze (Def_Id);
10779 end Constrain_Decimal;
10781 ----------------------------------
10782 -- Constrain_Discriminated_Type --
10783 ----------------------------------
10785 procedure Constrain_Discriminated_Type
10786 (Def_Id : Entity_Id;
10787 S : Node_Id;
10788 Related_Nod : Node_Id;
10789 For_Access : Boolean := False)
10791 E : constant Entity_Id := Entity (Subtype_Mark (S));
10792 T : Entity_Id;
10793 C : Node_Id;
10794 Elist : Elist_Id := New_Elmt_List;
10796 procedure Fixup_Bad_Constraint;
10797 -- This is called after finding a bad constraint, and after having
10798 -- posted an appropriate error message. The mission is to leave the
10799 -- entity T in as reasonable state as possible!
10801 --------------------------
10802 -- Fixup_Bad_Constraint --
10803 --------------------------
10805 procedure Fixup_Bad_Constraint is
10806 begin
10807 -- Set a reasonable Ekind for the entity. For an incomplete type,
10808 -- we can't do much, but for other types, we can set the proper
10809 -- corresponding subtype kind.
10811 if Ekind (T) = E_Incomplete_Type then
10812 Set_Ekind (Def_Id, Ekind (T));
10813 else
10814 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
10815 end if;
10817 -- Set Etype to the known type, to reduce chances of cascaded errors
10819 Set_Etype (Def_Id, E);
10820 Set_Error_Posted (Def_Id);
10821 end Fixup_Bad_Constraint;
10823 -- Start of processing for Constrain_Discriminated_Type
10825 begin
10826 C := Constraint (S);
10828 -- A discriminant constraint is only allowed in a subtype indication,
10829 -- after a subtype mark. This subtype mark must denote either a type
10830 -- with discriminants, or an access type whose designated type is a
10831 -- type with discriminants. A discriminant constraint specifies the
10832 -- values of these discriminants (RM 3.7.2(5)).
10834 T := Base_Type (Entity (Subtype_Mark (S)));
10836 if Ekind (T) in Access_Kind then
10837 T := Designated_Type (T);
10838 end if;
10840 -- Ada 2005 (AI-412): Constrained incomplete subtypes are illegal.
10841 -- Avoid generating an error for access-to-incomplete subtypes.
10843 if Ada_Version >= Ada_05
10844 and then Ekind (T) = E_Incomplete_Type
10845 and then Nkind (Parent (S)) = N_Subtype_Declaration
10846 and then not Is_Itype (Def_Id)
10847 then
10848 -- A little sanity check, emit an error message if the type
10849 -- has discriminants to begin with. Type T may be a regular
10850 -- incomplete type or imported via a limited with clause.
10852 if Has_Discriminants (T)
10853 or else
10854 (From_With_Type (T)
10855 and then Present (Non_Limited_View (T))
10856 and then Nkind (Parent (Non_Limited_View (T))) =
10857 N_Full_Type_Declaration
10858 and then Present (Discriminant_Specifications
10859 (Parent (Non_Limited_View (T)))))
10860 then
10861 Error_Msg_N
10862 ("(Ada 2005) incomplete subtype may not be constrained", C);
10863 else
10864 Error_Msg_N ("invalid constraint: type has no discriminant", C);
10865 end if;
10867 Fixup_Bad_Constraint;
10868 return;
10870 -- Check that the type has visible discriminants. The type may be
10871 -- a private type with unknown discriminants whose full view has
10872 -- discriminants which are invisible.
10874 elsif not Has_Discriminants (T)
10875 or else
10876 (Has_Unknown_Discriminants (T)
10877 and then Is_Private_Type (T))
10878 then
10879 Error_Msg_N ("invalid constraint: type has no discriminant", C);
10880 Fixup_Bad_Constraint;
10881 return;
10883 elsif Is_Constrained (E)
10884 or else (Ekind (E) = E_Class_Wide_Subtype
10885 and then Present (Discriminant_Constraint (E)))
10886 then
10887 Error_Msg_N ("type is already constrained", Subtype_Mark (S));
10888 Fixup_Bad_Constraint;
10889 return;
10890 end if;
10892 -- T may be an unconstrained subtype (e.g. a generic actual).
10893 -- Constraint applies to the base type.
10895 T := Base_Type (T);
10897 Elist := Build_Discriminant_Constraints (T, S);
10899 -- If the list returned was empty we had an error in building the
10900 -- discriminant constraint. We have also already signalled an error
10901 -- in the incomplete type case
10903 if Is_Empty_Elmt_List (Elist) then
10904 Fixup_Bad_Constraint;
10905 return;
10906 end if;
10908 Build_Discriminated_Subtype (T, Def_Id, Elist, Related_Nod, For_Access);
10909 end Constrain_Discriminated_Type;
10911 ---------------------------
10912 -- Constrain_Enumeration --
10913 ---------------------------
10915 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id) is
10916 T : constant Entity_Id := Entity (Subtype_Mark (S));
10917 C : constant Node_Id := Constraint (S);
10919 begin
10920 Set_Ekind (Def_Id, E_Enumeration_Subtype);
10922 Set_First_Literal (Def_Id, First_Literal (Base_Type (T)));
10924 Set_Etype (Def_Id, Base_Type (T));
10925 Set_Size_Info (Def_Id, (T));
10926 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10927 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
10929 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
10931 Set_Discrete_RM_Size (Def_Id);
10932 end Constrain_Enumeration;
10934 ----------------------
10935 -- Constrain_Float --
10936 ----------------------
10938 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id) is
10939 T : constant Entity_Id := Entity (Subtype_Mark (S));
10940 C : Node_Id;
10941 D : Node_Id;
10942 Rais : Node_Id;
10944 begin
10945 Set_Ekind (Def_Id, E_Floating_Point_Subtype);
10947 Set_Etype (Def_Id, Base_Type (T));
10948 Set_Size_Info (Def_Id, (T));
10949 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10951 -- Process the constraint
10953 C := Constraint (S);
10955 -- Digits constraint present
10957 if Nkind (C) = N_Digits_Constraint then
10958 Check_Restriction (No_Obsolescent_Features, C);
10960 if Warn_On_Obsolescent_Feature then
10961 Error_Msg_N
10962 ("subtype digits constraint is an " &
10963 "obsolescent feature (RM J.3(8))?", C);
10964 end if;
10966 D := Digits_Expression (C);
10967 Analyze_And_Resolve (D, Any_Integer);
10968 Check_Digits_Expression (D);
10969 Set_Digits_Value (Def_Id, Expr_Value (D));
10971 -- Check that digits value is in range. Obviously we can do this
10972 -- at compile time, but it is strictly a runtime check, and of
10973 -- course there is an ACVC test that checks this!
10975 if Digits_Value (Def_Id) > Digits_Value (T) then
10976 Error_Msg_Uint_1 := Digits_Value (T);
10977 Error_Msg_N ("?digits value is too large, maximum is ^", D);
10978 Rais :=
10979 Make_Raise_Constraint_Error (Sloc (D),
10980 Reason => CE_Range_Check_Failed);
10981 Insert_Action (Declaration_Node (Def_Id), Rais);
10982 end if;
10984 C := Range_Constraint (C);
10986 -- No digits constraint present
10988 else
10989 Set_Digits_Value (Def_Id, Digits_Value (T));
10990 end if;
10992 -- Range constraint present
10994 if Nkind (C) = N_Range_Constraint then
10995 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
10997 -- No range constraint present
10999 else
11000 pragma Assert (No (C));
11001 Set_Scalar_Range (Def_Id, Scalar_Range (T));
11002 end if;
11004 Set_Is_Constrained (Def_Id);
11005 end Constrain_Float;
11007 ---------------------
11008 -- Constrain_Index --
11009 ---------------------
11011 procedure Constrain_Index
11012 (Index : Node_Id;
11013 S : Node_Id;
11014 Related_Nod : Node_Id;
11015 Related_Id : Entity_Id;
11016 Suffix : Character;
11017 Suffix_Index : Nat)
11019 Def_Id : Entity_Id;
11020 R : Node_Id := Empty;
11021 T : constant Entity_Id := Etype (Index);
11023 begin
11024 if Nkind (S) = N_Range
11025 or else
11026 (Nkind (S) = N_Attribute_Reference
11027 and then Attribute_Name (S) = Name_Range)
11028 then
11029 -- A Range attribute will transformed into N_Range by Resolve
11031 Analyze (S);
11032 Set_Etype (S, T);
11033 R := S;
11035 Process_Range_Expr_In_Decl (R, T, Empty_List);
11037 if not Error_Posted (S)
11038 and then
11039 (Nkind (S) /= N_Range
11040 or else not Covers (T, (Etype (Low_Bound (S))))
11041 or else not Covers (T, (Etype (High_Bound (S)))))
11042 then
11043 if Base_Type (T) /= Any_Type
11044 and then Etype (Low_Bound (S)) /= Any_Type
11045 and then Etype (High_Bound (S)) /= Any_Type
11046 then
11047 Error_Msg_N ("range expected", S);
11048 end if;
11049 end if;
11051 elsif Nkind (S) = N_Subtype_Indication then
11053 -- The parser has verified that this is a discrete indication
11055 Resolve_Discrete_Subtype_Indication (S, T);
11056 R := Range_Expression (Constraint (S));
11058 elsif Nkind (S) = N_Discriminant_Association then
11060 -- Syntactically valid in subtype indication
11062 Error_Msg_N ("invalid index constraint", S);
11063 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
11064 return;
11066 -- Subtype_Mark case, no anonymous subtypes to construct
11068 else
11069 Analyze (S);
11071 if Is_Entity_Name (S) then
11072 if not Is_Type (Entity (S)) then
11073 Error_Msg_N ("expect subtype mark for index constraint", S);
11075 elsif Base_Type (Entity (S)) /= Base_Type (T) then
11076 Wrong_Type (S, Base_Type (T));
11077 end if;
11079 return;
11081 else
11082 Error_Msg_N ("invalid index constraint", S);
11083 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
11084 return;
11085 end if;
11086 end if;
11088 Def_Id :=
11089 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
11091 Set_Etype (Def_Id, Base_Type (T));
11093 if Is_Modular_Integer_Type (T) then
11094 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
11096 elsif Is_Integer_Type (T) then
11097 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
11099 else
11100 Set_Ekind (Def_Id, E_Enumeration_Subtype);
11101 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
11102 Set_First_Literal (Def_Id, First_Literal (T));
11103 end if;
11105 Set_Size_Info (Def_Id, (T));
11106 Set_RM_Size (Def_Id, RM_Size (T));
11107 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
11109 Set_Scalar_Range (Def_Id, R);
11111 Set_Etype (S, Def_Id);
11112 Set_Discrete_RM_Size (Def_Id);
11113 end Constrain_Index;
11115 -----------------------
11116 -- Constrain_Integer --
11117 -----------------------
11119 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id) is
11120 T : constant Entity_Id := Entity (Subtype_Mark (S));
11121 C : constant Node_Id := Constraint (S);
11123 begin
11124 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
11126 if Is_Modular_Integer_Type (T) then
11127 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
11128 else
11129 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
11130 end if;
11132 Set_Etype (Def_Id, Base_Type (T));
11133 Set_Size_Info (Def_Id, (T));
11134 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
11135 Set_Discrete_RM_Size (Def_Id);
11136 end Constrain_Integer;
11138 ------------------------------
11139 -- Constrain_Ordinary_Fixed --
11140 ------------------------------
11142 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id) is
11143 T : constant Entity_Id := Entity (Subtype_Mark (S));
11144 C : Node_Id;
11145 D : Node_Id;
11146 Rais : Node_Id;
11148 begin
11149 Set_Ekind (Def_Id, E_Ordinary_Fixed_Point_Subtype);
11150 Set_Etype (Def_Id, Base_Type (T));
11151 Set_Size_Info (Def_Id, (T));
11152 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
11153 Set_Small_Value (Def_Id, Small_Value (T));
11155 -- Process the constraint
11157 C := Constraint (S);
11159 -- Delta constraint present
11161 if Nkind (C) = N_Delta_Constraint then
11162 Check_Restriction (No_Obsolescent_Features, C);
11164 if Warn_On_Obsolescent_Feature then
11165 Error_Msg_S
11166 ("subtype delta constraint is an " &
11167 "obsolescent feature (RM J.3(7))?");
11168 end if;
11170 D := Delta_Expression (C);
11171 Analyze_And_Resolve (D, Any_Real);
11172 Check_Delta_Expression (D);
11173 Set_Delta_Value (Def_Id, Expr_Value_R (D));
11175 -- Check that delta value is in range. Obviously we can do this
11176 -- at compile time, but it is strictly a runtime check, and of
11177 -- course there is an ACVC test that checks this!
11179 if Delta_Value (Def_Id) < Delta_Value (T) then
11180 Error_Msg_N ("?delta value is too small", D);
11181 Rais :=
11182 Make_Raise_Constraint_Error (Sloc (D),
11183 Reason => CE_Range_Check_Failed);
11184 Insert_Action (Declaration_Node (Def_Id), Rais);
11185 end if;
11187 C := Range_Constraint (C);
11189 -- No delta constraint present
11191 else
11192 Set_Delta_Value (Def_Id, Delta_Value (T));
11193 end if;
11195 -- Range constraint present
11197 if Nkind (C) = N_Range_Constraint then
11198 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
11200 -- No range constraint present
11202 else
11203 pragma Assert (No (C));
11204 Set_Scalar_Range (Def_Id, Scalar_Range (T));
11206 end if;
11208 Set_Discrete_RM_Size (Def_Id);
11210 -- Unconditionally delay the freeze, since we cannot set size
11211 -- information in all cases correctly until the freeze point.
11213 Set_Has_Delayed_Freeze (Def_Id);
11214 end Constrain_Ordinary_Fixed;
11216 -----------------------
11217 -- Contain_Interface --
11218 -----------------------
11220 function Contain_Interface
11221 (Iface : Entity_Id;
11222 Ifaces : Elist_Id) return Boolean
11224 Iface_Elmt : Elmt_Id;
11226 begin
11227 if Present (Ifaces) then
11228 Iface_Elmt := First_Elmt (Ifaces);
11229 while Present (Iface_Elmt) loop
11230 if Node (Iface_Elmt) = Iface then
11231 return True;
11232 end if;
11234 Next_Elmt (Iface_Elmt);
11235 end loop;
11236 end if;
11238 return False;
11239 end Contain_Interface;
11241 ---------------------------
11242 -- Convert_Scalar_Bounds --
11243 ---------------------------
11245 procedure Convert_Scalar_Bounds
11246 (N : Node_Id;
11247 Parent_Type : Entity_Id;
11248 Derived_Type : Entity_Id;
11249 Loc : Source_Ptr)
11251 Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
11253 Lo : Node_Id;
11254 Hi : Node_Id;
11255 Rng : Node_Id;
11257 begin
11258 Lo := Build_Scalar_Bound
11259 (Type_Low_Bound (Derived_Type),
11260 Parent_Type, Implicit_Base);
11262 Hi := Build_Scalar_Bound
11263 (Type_High_Bound (Derived_Type),
11264 Parent_Type, Implicit_Base);
11266 Rng :=
11267 Make_Range (Loc,
11268 Low_Bound => Lo,
11269 High_Bound => Hi);
11271 Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
11273 Set_Parent (Rng, N);
11274 Set_Scalar_Range (Derived_Type, Rng);
11276 -- Analyze the bounds
11278 Analyze_And_Resolve (Lo, Implicit_Base);
11279 Analyze_And_Resolve (Hi, Implicit_Base);
11281 -- Analyze the range itself, except that we do not analyze it if
11282 -- the bounds are real literals, and we have a fixed-point type.
11283 -- The reason for this is that we delay setting the bounds in this
11284 -- case till we know the final Small and Size values (see circuit
11285 -- in Freeze.Freeze_Fixed_Point_Type for further details).
11287 if Is_Fixed_Point_Type (Parent_Type)
11288 and then Nkind (Lo) = N_Real_Literal
11289 and then Nkind (Hi) = N_Real_Literal
11290 then
11291 return;
11293 -- Here we do the analysis of the range
11295 -- Note: we do this manually, since if we do a normal Analyze and
11296 -- Resolve call, there are problems with the conversions used for
11297 -- the derived type range.
11299 else
11300 Set_Etype (Rng, Implicit_Base);
11301 Set_Analyzed (Rng, True);
11302 end if;
11303 end Convert_Scalar_Bounds;
11305 -------------------
11306 -- Copy_And_Swap --
11307 -------------------
11309 procedure Copy_And_Swap (Priv, Full : Entity_Id) is
11310 begin
11311 -- Initialize new full declaration entity by copying the pertinent
11312 -- fields of the corresponding private declaration entity.
11314 -- We temporarily set Ekind to a value appropriate for a type to
11315 -- avoid assert failures in Einfo from checking for setting type
11316 -- attributes on something that is not a type. Ekind (Priv) is an
11317 -- appropriate choice, since it allowed the attributes to be set
11318 -- in the first place. This Ekind value will be modified later.
11320 Set_Ekind (Full, Ekind (Priv));
11322 -- Also set Etype temporarily to Any_Type, again, in the absence
11323 -- of errors, it will be properly reset, and if there are errors,
11324 -- then we want a value of Any_Type to remain.
11326 Set_Etype (Full, Any_Type);
11328 -- Now start copying attributes
11330 Set_Has_Discriminants (Full, Has_Discriminants (Priv));
11332 if Has_Discriminants (Full) then
11333 Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
11334 Set_Stored_Constraint (Full, Stored_Constraint (Priv));
11335 end if;
11337 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
11338 Set_Homonym (Full, Homonym (Priv));
11339 Set_Is_Immediately_Visible (Full, Is_Immediately_Visible (Priv));
11340 Set_Is_Public (Full, Is_Public (Priv));
11341 Set_Is_Pure (Full, Is_Pure (Priv));
11342 Set_Is_Tagged_Type (Full, Is_Tagged_Type (Priv));
11343 Set_Has_Pragma_Unmodified (Full, Has_Pragma_Unmodified (Priv));
11344 Set_Has_Pragma_Unreferenced (Full, Has_Pragma_Unreferenced (Priv));
11345 Set_Has_Pragma_Unreferenced_Objects
11346 (Full, Has_Pragma_Unreferenced_Objects
11347 (Priv));
11349 Conditional_Delay (Full, Priv);
11351 if Is_Tagged_Type (Full) then
11352 Set_Primitive_Operations (Full, Primitive_Operations (Priv));
11354 if Priv = Base_Type (Priv) then
11355 Set_Class_Wide_Type (Full, Class_Wide_Type (Priv));
11356 end if;
11357 end if;
11359 Set_Is_Volatile (Full, Is_Volatile (Priv));
11360 Set_Treat_As_Volatile (Full, Treat_As_Volatile (Priv));
11361 Set_Scope (Full, Scope (Priv));
11362 Set_Next_Entity (Full, Next_Entity (Priv));
11363 Set_First_Entity (Full, First_Entity (Priv));
11364 Set_Last_Entity (Full, Last_Entity (Priv));
11366 -- If access types have been recorded for later handling, keep them in
11367 -- the full view so that they get handled when the full view freeze
11368 -- node is expanded.
11370 if Present (Freeze_Node (Priv))
11371 and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
11372 then
11373 Ensure_Freeze_Node (Full);
11374 Set_Access_Types_To_Process
11375 (Freeze_Node (Full),
11376 Access_Types_To_Process (Freeze_Node (Priv)));
11377 end if;
11379 -- Swap the two entities. Now Privat is the full type entity and Full is
11380 -- the private one. They will be swapped back at the end of the private
11381 -- part. This swapping ensures that the entity that is visible in the
11382 -- private part is the full declaration.
11384 Exchange_Entities (Priv, Full);
11385 Append_Entity (Full, Scope (Full));
11386 end Copy_And_Swap;
11388 -------------------------------------
11389 -- Copy_Array_Base_Type_Attributes --
11390 -------------------------------------
11392 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
11393 begin
11394 Set_Component_Alignment (T1, Component_Alignment (T2));
11395 Set_Component_Type (T1, Component_Type (T2));
11396 Set_Component_Size (T1, Component_Size (T2));
11397 Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
11398 Set_Finalize_Storage_Only (T1, Finalize_Storage_Only (T2));
11399 Set_Has_Non_Standard_Rep (T1, Has_Non_Standard_Rep (T2));
11400 Set_Has_Task (T1, Has_Task (T2));
11401 Set_Is_Packed (T1, Is_Packed (T2));
11402 Set_Has_Aliased_Components (T1, Has_Aliased_Components (T2));
11403 Set_Has_Atomic_Components (T1, Has_Atomic_Components (T2));
11404 Set_Has_Volatile_Components (T1, Has_Volatile_Components (T2));
11405 end Copy_Array_Base_Type_Attributes;
11407 -----------------------------------
11408 -- Copy_Array_Subtype_Attributes --
11409 -----------------------------------
11411 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
11412 begin
11413 Set_Size_Info (T1, T2);
11415 Set_First_Index (T1, First_Index (T2));
11416 Set_Is_Aliased (T1, Is_Aliased (T2));
11417 Set_Is_Atomic (T1, Is_Atomic (T2));
11418 Set_Is_Volatile (T1, Is_Volatile (T2));
11419 Set_Treat_As_Volatile (T1, Treat_As_Volatile (T2));
11420 Set_Is_Constrained (T1, Is_Constrained (T2));
11421 Set_Depends_On_Private (T1, Has_Private_Component (T2));
11422 Set_First_Rep_Item (T1, First_Rep_Item (T2));
11423 Set_Convention (T1, Convention (T2));
11424 Set_Is_Limited_Composite (T1, Is_Limited_Composite (T2));
11425 Set_Is_Private_Composite (T1, Is_Private_Composite (T2));
11426 Set_Packed_Array_Type (T1, Packed_Array_Type (T2));
11427 end Copy_Array_Subtype_Attributes;
11429 -----------------------------------
11430 -- Create_Constrained_Components --
11431 -----------------------------------
11433 procedure Create_Constrained_Components
11434 (Subt : Entity_Id;
11435 Decl_Node : Node_Id;
11436 Typ : Entity_Id;
11437 Constraints : Elist_Id)
11439 Loc : constant Source_Ptr := Sloc (Subt);
11440 Comp_List : constant Elist_Id := New_Elmt_List;
11441 Parent_Type : constant Entity_Id := Etype (Typ);
11442 Assoc_List : constant List_Id := New_List;
11443 Discr_Val : Elmt_Id;
11444 Errors : Boolean;
11445 New_C : Entity_Id;
11446 Old_C : Entity_Id;
11447 Is_Static : Boolean := True;
11449 procedure Collect_Fixed_Components (Typ : Entity_Id);
11450 -- Collect parent type components that do not appear in a variant part
11452 procedure Create_All_Components;
11453 -- Iterate over Comp_List to create the components of the subtype
11455 function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
11456 -- Creates a new component from Old_Compon, copying all the fields from
11457 -- it, including its Etype, inserts the new component in the Subt entity
11458 -- chain and returns the new component.
11460 function Is_Variant_Record (T : Entity_Id) return Boolean;
11461 -- If true, and discriminants are static, collect only components from
11462 -- variants selected by discriminant values.
11464 ------------------------------
11465 -- Collect_Fixed_Components --
11466 ------------------------------
11468 procedure Collect_Fixed_Components (Typ : Entity_Id) is
11469 begin
11470 -- Build association list for discriminants, and find components of the
11471 -- variant part selected by the values of the discriminants.
11473 Old_C := First_Discriminant (Typ);
11474 Discr_Val := First_Elmt (Constraints);
11475 while Present (Old_C) loop
11476 Append_To (Assoc_List,
11477 Make_Component_Association (Loc,
11478 Choices => New_List (New_Occurrence_Of (Old_C, Loc)),
11479 Expression => New_Copy (Node (Discr_Val))));
11481 Next_Elmt (Discr_Val);
11482 Next_Discriminant (Old_C);
11483 end loop;
11485 -- The tag, and the possible parent and controller components
11486 -- are unconditionally in the subtype.
11488 if Is_Tagged_Type (Typ)
11489 or else Has_Controlled_Component (Typ)
11490 then
11491 Old_C := First_Component (Typ);
11492 while Present (Old_C) loop
11493 if Chars ((Old_C)) = Name_uTag
11494 or else Chars ((Old_C)) = Name_uParent
11495 or else Chars ((Old_C)) = Name_uController
11496 then
11497 Append_Elmt (Old_C, Comp_List);
11498 end if;
11500 Next_Component (Old_C);
11501 end loop;
11502 end if;
11503 end Collect_Fixed_Components;
11505 ---------------------------
11506 -- Create_All_Components --
11507 ---------------------------
11509 procedure Create_All_Components is
11510 Comp : Elmt_Id;
11512 begin
11513 Comp := First_Elmt (Comp_List);
11514 while Present (Comp) loop
11515 Old_C := Node (Comp);
11516 New_C := Create_Component (Old_C);
11518 Set_Etype
11519 (New_C,
11520 Constrain_Component_Type
11521 (Old_C, Subt, Decl_Node, Typ, Constraints));
11522 Set_Is_Public (New_C, Is_Public (Subt));
11524 Next_Elmt (Comp);
11525 end loop;
11526 end Create_All_Components;
11528 ----------------------
11529 -- Create_Component --
11530 ----------------------
11532 function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
11533 New_Compon : constant Entity_Id := New_Copy (Old_Compon);
11535 begin
11536 if Ekind (Old_Compon) = E_Discriminant
11537 and then Is_Completely_Hidden (Old_Compon)
11538 then
11539 -- This is a shadow discriminant created for a discriminant of
11540 -- the parent type, which needs to be present in the subtype.
11541 -- Give the shadow discriminant an internal name that cannot
11542 -- conflict with that of visible components.
11544 Set_Chars (New_Compon, New_Internal_Name ('C'));
11545 end if;
11547 -- Set the parent so we have a proper link for freezing etc. This is
11548 -- not a real parent pointer, since of course our parent does not own
11549 -- up to us and reference us, we are an illegitimate child of the
11550 -- original parent!
11552 Set_Parent (New_Compon, Parent (Old_Compon));
11554 -- If the old component's Esize was already determined and is a
11555 -- static value, then the new component simply inherits it. Otherwise
11556 -- the old component's size may require run-time determination, but
11557 -- the new component's size still might be statically determinable
11558 -- (if, for example it has a static constraint). In that case we want
11559 -- Layout_Type to recompute the component's size, so we reset its
11560 -- size and positional fields.
11562 if Frontend_Layout_On_Target
11563 and then not Known_Static_Esize (Old_Compon)
11564 then
11565 Set_Esize (New_Compon, Uint_0);
11566 Init_Normalized_First_Bit (New_Compon);
11567 Init_Normalized_Position (New_Compon);
11568 Init_Normalized_Position_Max (New_Compon);
11569 end if;
11571 -- We do not want this node marked as Comes_From_Source, since
11572 -- otherwise it would get first class status and a separate cross-
11573 -- reference line would be generated. Illegitimate children do not
11574 -- rate such recognition.
11576 Set_Comes_From_Source (New_Compon, False);
11578 -- But it is a real entity, and a birth certificate must be properly
11579 -- registered by entering it into the entity list.
11581 Enter_Name (New_Compon);
11583 return New_Compon;
11584 end Create_Component;
11586 -----------------------
11587 -- Is_Variant_Record --
11588 -----------------------
11590 function Is_Variant_Record (T : Entity_Id) return Boolean is
11591 begin
11592 return Nkind (Parent (T)) = N_Full_Type_Declaration
11593 and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
11594 and then Present (Component_List (Type_Definition (Parent (T))))
11595 and then
11596 Present
11597 (Variant_Part (Component_List (Type_Definition (Parent (T)))));
11598 end Is_Variant_Record;
11600 -- Start of processing for Create_Constrained_Components
11602 begin
11603 pragma Assert (Subt /= Base_Type (Subt));
11604 pragma Assert (Typ = Base_Type (Typ));
11606 Set_First_Entity (Subt, Empty);
11607 Set_Last_Entity (Subt, Empty);
11609 -- Check whether constraint is fully static, in which case we can
11610 -- optimize the list of components.
11612 Discr_Val := First_Elmt (Constraints);
11613 while Present (Discr_Val) loop
11614 if not Is_OK_Static_Expression (Node (Discr_Val)) then
11615 Is_Static := False;
11616 exit;
11617 end if;
11619 Next_Elmt (Discr_Val);
11620 end loop;
11622 Set_Has_Static_Discriminants (Subt, Is_Static);
11624 Push_Scope (Subt);
11626 -- Inherit the discriminants of the parent type
11628 Add_Discriminants : declare
11629 Num_Disc : Int;
11630 Num_Gird : Int;
11632 begin
11633 Num_Disc := 0;
11634 Old_C := First_Discriminant (Typ);
11636 while Present (Old_C) loop
11637 Num_Disc := Num_Disc + 1;
11638 New_C := Create_Component (Old_C);
11639 Set_Is_Public (New_C, Is_Public (Subt));
11640 Next_Discriminant (Old_C);
11641 end loop;
11643 -- For an untagged derived subtype, the number of discriminants may
11644 -- be smaller than the number of inherited discriminants, because
11645 -- several of them may be renamed by a single new discriminant or
11646 -- constrained. In this case, add the hidden discriminants back into
11647 -- the subtype, because they need to be present if the optimizer of
11648 -- the GCC 4.x back-end decides to break apart assignments between
11649 -- objects using the parent view into member-wise assignments.
11651 Num_Gird := 0;
11653 if Is_Derived_Type (Typ)
11654 and then not Is_Tagged_Type (Typ)
11655 then
11656 Old_C := First_Stored_Discriminant (Typ);
11658 while Present (Old_C) loop
11659 Num_Gird := Num_Gird + 1;
11660 Next_Stored_Discriminant (Old_C);
11661 end loop;
11662 end if;
11664 if Num_Gird > Num_Disc then
11666 -- Find out multiple uses of new discriminants, and add hidden
11667 -- components for the extra renamed discriminants. We recognize
11668 -- multiple uses through the Corresponding_Discriminant of a
11669 -- new discriminant: if it constrains several old discriminants,
11670 -- this field points to the last one in the parent type. The
11671 -- stored discriminants of the derived type have the same name
11672 -- as those of the parent.
11674 declare
11675 Constr : Elmt_Id;
11676 New_Discr : Entity_Id;
11677 Old_Discr : Entity_Id;
11679 begin
11680 Constr := First_Elmt (Stored_Constraint (Typ));
11681 Old_Discr := First_Stored_Discriminant (Typ);
11682 while Present (Constr) loop
11683 if Is_Entity_Name (Node (Constr))
11684 and then Ekind (Entity (Node (Constr))) = E_Discriminant
11685 then
11686 New_Discr := Entity (Node (Constr));
11688 if Chars (Corresponding_Discriminant (New_Discr)) /=
11689 Chars (Old_Discr)
11690 then
11691 -- The new discriminant has been used to rename a
11692 -- subsequent old discriminant. Introduce a shadow
11693 -- component for the current old discriminant.
11695 New_C := Create_Component (Old_Discr);
11696 Set_Original_Record_Component (New_C, Old_Discr);
11697 end if;
11699 else
11700 -- The constraint has eliminated the old discriminant.
11701 -- Introduce a shadow component.
11703 New_C := Create_Component (Old_Discr);
11704 Set_Original_Record_Component (New_C, Old_Discr);
11705 end if;
11707 Next_Elmt (Constr);
11708 Next_Stored_Discriminant (Old_Discr);
11709 end loop;
11710 end;
11711 end if;
11712 end Add_Discriminants;
11714 if Is_Static
11715 and then Is_Variant_Record (Typ)
11716 then
11717 Collect_Fixed_Components (Typ);
11719 Gather_Components (
11720 Typ,
11721 Component_List (Type_Definition (Parent (Typ))),
11722 Governed_By => Assoc_List,
11723 Into => Comp_List,
11724 Report_Errors => Errors);
11725 pragma Assert (not Errors);
11727 Create_All_Components;
11729 -- If the subtype declaration is created for a tagged type derivation
11730 -- with constraints, we retrieve the record definition of the parent
11731 -- type to select the components of the proper variant.
11733 elsif Is_Static
11734 and then Is_Tagged_Type (Typ)
11735 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
11736 and then
11737 Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
11738 and then Is_Variant_Record (Parent_Type)
11739 then
11740 Collect_Fixed_Components (Typ);
11742 Gather_Components (
11743 Typ,
11744 Component_List (Type_Definition (Parent (Parent_Type))),
11745 Governed_By => Assoc_List,
11746 Into => Comp_List,
11747 Report_Errors => Errors);
11748 pragma Assert (not Errors);
11750 -- If the tagged derivation has a type extension, collect all the
11751 -- new components therein.
11753 if Present
11754 (Record_Extension_Part (Type_Definition (Parent (Typ))))
11755 then
11756 Old_C := First_Component (Typ);
11757 while Present (Old_C) loop
11758 if Original_Record_Component (Old_C) = Old_C
11759 and then Chars (Old_C) /= Name_uTag
11760 and then Chars (Old_C) /= Name_uParent
11761 and then Chars (Old_C) /= Name_uController
11762 then
11763 Append_Elmt (Old_C, Comp_List);
11764 end if;
11766 Next_Component (Old_C);
11767 end loop;
11768 end if;
11770 Create_All_Components;
11772 else
11773 -- If discriminants are not static, or if this is a multi-level type
11774 -- extension, we have to include all components of the parent type.
11776 Old_C := First_Component (Typ);
11777 while Present (Old_C) loop
11778 New_C := Create_Component (Old_C);
11780 Set_Etype
11781 (New_C,
11782 Constrain_Component_Type
11783 (Old_C, Subt, Decl_Node, Typ, Constraints));
11784 Set_Is_Public (New_C, Is_Public (Subt));
11786 Next_Component (Old_C);
11787 end loop;
11788 end if;
11790 End_Scope;
11791 end Create_Constrained_Components;
11793 ------------------------------------------
11794 -- Decimal_Fixed_Point_Type_Declaration --
11795 ------------------------------------------
11797 procedure Decimal_Fixed_Point_Type_Declaration
11798 (T : Entity_Id;
11799 Def : Node_Id)
11801 Loc : constant Source_Ptr := Sloc (Def);
11802 Digs_Expr : constant Node_Id := Digits_Expression (Def);
11803 Delta_Expr : constant Node_Id := Delta_Expression (Def);
11804 Implicit_Base : Entity_Id;
11805 Digs_Val : Uint;
11806 Delta_Val : Ureal;
11807 Scale_Val : Uint;
11808 Bound_Val : Ureal;
11810 begin
11811 Check_Restriction (No_Fixed_Point, Def);
11813 -- Create implicit base type
11815 Implicit_Base :=
11816 Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
11817 Set_Etype (Implicit_Base, Implicit_Base);
11819 -- Analyze and process delta expression
11821 Analyze_And_Resolve (Delta_Expr, Universal_Real);
11823 Check_Delta_Expression (Delta_Expr);
11824 Delta_Val := Expr_Value_R (Delta_Expr);
11826 -- Check delta is power of 10, and determine scale value from it
11828 declare
11829 Val : Ureal;
11831 begin
11832 Scale_Val := Uint_0;
11833 Val := Delta_Val;
11835 if Val < Ureal_1 then
11836 while Val < Ureal_1 loop
11837 Val := Val * Ureal_10;
11838 Scale_Val := Scale_Val + 1;
11839 end loop;
11841 if Scale_Val > 18 then
11842 Error_Msg_N ("scale exceeds maximum value of 18", Def);
11843 Scale_Val := UI_From_Int (+18);
11844 end if;
11846 else
11847 while Val > Ureal_1 loop
11848 Val := Val / Ureal_10;
11849 Scale_Val := Scale_Val - 1;
11850 end loop;
11852 if Scale_Val < -18 then
11853 Error_Msg_N ("scale is less than minimum value of -18", Def);
11854 Scale_Val := UI_From_Int (-18);
11855 end if;
11856 end if;
11858 if Val /= Ureal_1 then
11859 Error_Msg_N ("delta expression must be a power of 10", Def);
11860 Delta_Val := Ureal_10 ** (-Scale_Val);
11861 end if;
11862 end;
11864 -- Set delta, scale and small (small = delta for decimal type)
11866 Set_Delta_Value (Implicit_Base, Delta_Val);
11867 Set_Scale_Value (Implicit_Base, Scale_Val);
11868 Set_Small_Value (Implicit_Base, Delta_Val);
11870 -- Analyze and process digits expression
11872 Analyze_And_Resolve (Digs_Expr, Any_Integer);
11873 Check_Digits_Expression (Digs_Expr);
11874 Digs_Val := Expr_Value (Digs_Expr);
11876 if Digs_Val > 18 then
11877 Digs_Val := UI_From_Int (+18);
11878 Error_Msg_N ("digits value out of range, maximum is 18", Digs_Expr);
11879 end if;
11881 Set_Digits_Value (Implicit_Base, Digs_Val);
11882 Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
11884 -- Set range of base type from digits value for now. This will be
11885 -- expanded to represent the true underlying base range by Freeze.
11887 Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
11889 -- Note: We leave size as zero for now, size will be set at freeze
11890 -- time. We have to do this for ordinary fixed-point, because the size
11891 -- depends on the specified small, and we might as well do the same for
11892 -- decimal fixed-point.
11894 pragma Assert (Esize (Implicit_Base) = Uint_0);
11896 -- If there are bounds given in the declaration use them as the
11897 -- bounds of the first named subtype.
11899 if Present (Real_Range_Specification (Def)) then
11900 declare
11901 RRS : constant Node_Id := Real_Range_Specification (Def);
11902 Low : constant Node_Id := Low_Bound (RRS);
11903 High : constant Node_Id := High_Bound (RRS);
11904 Low_Val : Ureal;
11905 High_Val : Ureal;
11907 begin
11908 Analyze_And_Resolve (Low, Any_Real);
11909 Analyze_And_Resolve (High, Any_Real);
11910 Check_Real_Bound (Low);
11911 Check_Real_Bound (High);
11912 Low_Val := Expr_Value_R (Low);
11913 High_Val := Expr_Value_R (High);
11915 if Low_Val < (-Bound_Val) then
11916 Error_Msg_N
11917 ("range low bound too small for digits value", Low);
11918 Low_Val := -Bound_Val;
11919 end if;
11921 if High_Val > Bound_Val then
11922 Error_Msg_N
11923 ("range high bound too large for digits value", High);
11924 High_Val := Bound_Val;
11925 end if;
11927 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
11928 end;
11930 -- If no explicit range, use range that corresponds to given
11931 -- digits value. This will end up as the final range for the
11932 -- first subtype.
11934 else
11935 Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
11936 end if;
11938 -- Complete entity for first subtype
11940 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
11941 Set_Etype (T, Implicit_Base);
11942 Set_Size_Info (T, Implicit_Base);
11943 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
11944 Set_Digits_Value (T, Digs_Val);
11945 Set_Delta_Value (T, Delta_Val);
11946 Set_Small_Value (T, Delta_Val);
11947 Set_Scale_Value (T, Scale_Val);
11948 Set_Is_Constrained (T);
11949 end Decimal_Fixed_Point_Type_Declaration;
11951 -----------------------------------
11952 -- Derive_Progenitor_Subprograms --
11953 -----------------------------------
11955 procedure Derive_Progenitor_Subprograms
11956 (Parent_Type : Entity_Id;
11957 Tagged_Type : Entity_Id)
11959 E : Entity_Id;
11960 Elmt : Elmt_Id;
11961 Iface : Entity_Id;
11962 Iface_Elmt : Elmt_Id;
11963 Iface_Subp : Entity_Id;
11964 New_Subp : Entity_Id := Empty;
11965 Prim_Elmt : Elmt_Id;
11966 Subp : Entity_Id;
11967 Typ : Entity_Id;
11969 begin
11970 pragma Assert (Ada_Version >= Ada_05
11971 and then Is_Record_Type (Tagged_Type)
11972 and then Is_Tagged_Type (Tagged_Type)
11973 and then Has_Interfaces (Tagged_Type));
11975 -- Step 1: Transfer to the full-view primitives associated with the
11976 -- partial-view that cover interface primitives. Conceptually this
11977 -- work should be done later by Process_Full_View; done here to
11978 -- simplify its implementation at later stages. It can be safely
11979 -- done here because interfaces must be visible in the partial and
11980 -- private view (RM 7.3(7.3/2)).
11982 -- Small optimization: This work is only required if the parent is
11983 -- abstract. If the tagged type is not abstract, it cannot have
11984 -- abstract primitives (the only entities in the list of primitives of
11985 -- non-abstract tagged types that can reference abstract primitives
11986 -- through its Alias attribute are the internal entities that have
11987 -- attribute Interface_Alias, and these entities are generated later
11988 -- by Add_Internal_Interface_Entities).
11990 if In_Private_Part (Current_Scope)
11991 and then Is_Abstract_Type (Parent_Type)
11992 then
11993 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
11994 while Present (Elmt) loop
11995 Subp := Node (Elmt);
11997 -- At this stage it is not possible to have entities in the list
11998 -- of primitives that have attribute Interface_Alias
12000 pragma Assert (No (Interface_Alias (Subp)));
12002 Typ := Find_Dispatching_Type (Ultimate_Alias (Subp));
12004 if Is_Interface (Typ) then
12005 E := Find_Primitive_Covering_Interface
12006 (Tagged_Type => Tagged_Type,
12007 Iface_Prim => Subp);
12009 if Present (E)
12010 and then Find_Dispatching_Type (Ultimate_Alias (E)) /= Typ
12011 then
12012 Replace_Elmt (Elmt, E);
12013 Remove_Homonym (Subp);
12014 end if;
12015 end if;
12017 Next_Elmt (Elmt);
12018 end loop;
12019 end if;
12021 -- Step 2: Add primitives of progenitors that are not implemented by
12022 -- parents of Tagged_Type
12024 if Present (Interfaces (Base_Type (Tagged_Type))) then
12025 Iface_Elmt := First_Elmt (Interfaces (Base_Type (Tagged_Type)));
12026 while Present (Iface_Elmt) loop
12027 Iface := Node (Iface_Elmt);
12029 Prim_Elmt := First_Elmt (Primitive_Operations (Iface));
12030 while Present (Prim_Elmt) loop
12031 Iface_Subp := Node (Prim_Elmt);
12033 -- Exclude derivation of predefined primitives except those
12034 -- that come from source. Required to catch declarations of
12035 -- equality operators of interfaces. For example:
12037 -- type Iface is interface;
12038 -- function "=" (Left, Right : Iface) return Boolean;
12040 if not Is_Predefined_Dispatching_Operation (Iface_Subp)
12041 or else Comes_From_Source (Iface_Subp)
12042 then
12043 E := Find_Primitive_Covering_Interface
12044 (Tagged_Type => Tagged_Type,
12045 Iface_Prim => Iface_Subp);
12047 -- If not found we derive a new primitive leaving its alias
12048 -- attribute referencing the interface primitive
12050 if No (E) then
12051 Derive_Subprogram
12052 (New_Subp, Iface_Subp, Tagged_Type, Iface);
12054 -- Propagate to the full view interface entities associated
12055 -- with the partial view
12057 elsif In_Private_Part (Current_Scope)
12058 and then Present (Alias (E))
12059 and then Alias (E) = Iface_Subp
12060 and then
12061 List_Containing (Parent (E)) /=
12062 Private_Declarations
12063 (Specification
12064 (Unit_Declaration_Node (Current_Scope)))
12065 then
12066 Append_Elmt (E, Primitive_Operations (Tagged_Type));
12067 end if;
12068 end if;
12070 Next_Elmt (Prim_Elmt);
12071 end loop;
12073 Next_Elmt (Iface_Elmt);
12074 end loop;
12075 end if;
12076 end Derive_Progenitor_Subprograms;
12078 -----------------------
12079 -- Derive_Subprogram --
12080 -----------------------
12082 procedure Derive_Subprogram
12083 (New_Subp : in out Entity_Id;
12084 Parent_Subp : Entity_Id;
12085 Derived_Type : Entity_Id;
12086 Parent_Type : Entity_Id;
12087 Actual_Subp : Entity_Id := Empty)
12089 Formal : Entity_Id;
12090 -- Formal parameter of parent primitive operation
12092 Formal_Of_Actual : Entity_Id;
12093 -- Formal parameter of actual operation, when the derivation is to
12094 -- create a renaming for a primitive operation of an actual in an
12095 -- instantiation.
12097 New_Formal : Entity_Id;
12098 -- Formal of inherited operation
12100 Visible_Subp : Entity_Id := Parent_Subp;
12102 function Is_Private_Overriding return Boolean;
12103 -- If Subp is a private overriding of a visible operation, the inherited
12104 -- operation derives from the overridden op (even though its body is the
12105 -- overriding one) and the inherited operation is visible now. See
12106 -- sem_disp to see the full details of the handling of the overridden
12107 -- subprogram, which is removed from the list of primitive operations of
12108 -- the type. The overridden subprogram is saved locally in Visible_Subp,
12109 -- and used to diagnose abstract operations that need overriding in the
12110 -- derived type.
12112 procedure Replace_Type (Id, New_Id : Entity_Id);
12113 -- When the type is an anonymous access type, create a new access type
12114 -- designating the derived type.
12116 procedure Set_Derived_Name;
12117 -- This procedure sets the appropriate Chars name for New_Subp. This
12118 -- is normally just a copy of the parent name. An exception arises for
12119 -- type support subprograms, where the name is changed to reflect the
12120 -- name of the derived type, e.g. if type foo is derived from type bar,
12121 -- then a procedure barDA is derived with a name fooDA.
12123 ---------------------------
12124 -- Is_Private_Overriding --
12125 ---------------------------
12127 function Is_Private_Overriding return Boolean is
12128 Prev : Entity_Id;
12130 begin
12131 -- If the parent is not a dispatching operation there is no
12132 -- need to investigate overridings
12134 if not Is_Dispatching_Operation (Parent_Subp) then
12135 return False;
12136 end if;
12138 -- The visible operation that is overridden is a homonym of the
12139 -- parent subprogram. We scan the homonym chain to find the one
12140 -- whose alias is the subprogram we are deriving.
12142 Prev := Current_Entity (Parent_Subp);
12143 while Present (Prev) loop
12144 if Ekind (Prev) = Ekind (Parent_Subp)
12145 and then Alias (Prev) = Parent_Subp
12146 and then Scope (Parent_Subp) = Scope (Prev)
12147 and then not Is_Hidden (Prev)
12148 then
12149 Visible_Subp := Prev;
12150 return True;
12151 end if;
12153 Prev := Homonym (Prev);
12154 end loop;
12156 return False;
12157 end Is_Private_Overriding;
12159 ------------------
12160 -- Replace_Type --
12161 ------------------
12163 procedure Replace_Type (Id, New_Id : Entity_Id) is
12164 Acc_Type : Entity_Id;
12165 Par : constant Node_Id := Parent (Derived_Type);
12167 begin
12168 -- When the type is an anonymous access type, create a new access
12169 -- type designating the derived type. This itype must be elaborated
12170 -- at the point of the derivation, not on subsequent calls that may
12171 -- be out of the proper scope for Gigi, so we insert a reference to
12172 -- it after the derivation.
12174 if Ekind (Etype (Id)) = E_Anonymous_Access_Type then
12175 declare
12176 Desig_Typ : Entity_Id := Designated_Type (Etype (Id));
12178 begin
12179 if Ekind (Desig_Typ) = E_Record_Type_With_Private
12180 and then Present (Full_View (Desig_Typ))
12181 and then not Is_Private_Type (Parent_Type)
12182 then
12183 Desig_Typ := Full_View (Desig_Typ);
12184 end if;
12186 if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
12188 -- Ada 2005 (AI-251): Handle also derivations of abstract
12189 -- interface primitives.
12191 or else (Is_Interface (Desig_Typ)
12192 and then not Is_Class_Wide_Type (Desig_Typ))
12193 then
12194 Acc_Type := New_Copy (Etype (Id));
12195 Set_Etype (Acc_Type, Acc_Type);
12196 Set_Scope (Acc_Type, New_Subp);
12198 -- Compute size of anonymous access type
12200 if Is_Array_Type (Desig_Typ)
12201 and then not Is_Constrained (Desig_Typ)
12202 then
12203 Init_Size (Acc_Type, 2 * System_Address_Size);
12204 else
12205 Init_Size (Acc_Type, System_Address_Size);
12206 end if;
12208 Init_Alignment (Acc_Type);
12209 Set_Directly_Designated_Type (Acc_Type, Derived_Type);
12211 Set_Etype (New_Id, Acc_Type);
12212 Set_Scope (New_Id, New_Subp);
12214 -- Create a reference to it
12215 Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
12217 else
12218 Set_Etype (New_Id, Etype (Id));
12219 end if;
12220 end;
12222 elsif Base_Type (Etype (Id)) = Base_Type (Parent_Type)
12223 or else
12224 (Ekind (Etype (Id)) = E_Record_Type_With_Private
12225 and then Present (Full_View (Etype (Id)))
12226 and then
12227 Base_Type (Full_View (Etype (Id))) = Base_Type (Parent_Type))
12228 then
12229 -- Constraint checks on formals are generated during expansion,
12230 -- based on the signature of the original subprogram. The bounds
12231 -- of the derived type are not relevant, and thus we can use
12232 -- the base type for the formals. However, the return type may be
12233 -- used in a context that requires that the proper static bounds
12234 -- be used (a case statement, for example) and for those cases
12235 -- we must use the derived type (first subtype), not its base.
12237 -- If the derived_type_definition has no constraints, we know that
12238 -- the derived type has the same constraints as the first subtype
12239 -- of the parent, and we can also use it rather than its base,
12240 -- which can lead to more efficient code.
12242 if Etype (Id) = Parent_Type then
12243 if Is_Scalar_Type (Parent_Type)
12244 and then
12245 Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
12246 then
12247 Set_Etype (New_Id, Derived_Type);
12249 elsif Nkind (Par) = N_Full_Type_Declaration
12250 and then
12251 Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
12252 and then
12253 Is_Entity_Name
12254 (Subtype_Indication (Type_Definition (Par)))
12255 then
12256 Set_Etype (New_Id, Derived_Type);
12258 else
12259 Set_Etype (New_Id, Base_Type (Derived_Type));
12260 end if;
12262 else
12263 Set_Etype (New_Id, Base_Type (Derived_Type));
12264 end if;
12266 -- Ada 2005 (AI-251): Handle derivations of abstract interface
12267 -- primitives.
12269 elsif Is_Interface (Etype (Id))
12270 and then not Is_Class_Wide_Type (Etype (Id))
12271 and then Is_Progenitor (Etype (Id), Derived_Type)
12272 then
12273 Set_Etype (New_Id, Derived_Type);
12275 else
12276 Set_Etype (New_Id, Etype (Id));
12277 end if;
12278 end Replace_Type;
12280 ----------------------
12281 -- Set_Derived_Name --
12282 ----------------------
12284 procedure Set_Derived_Name is
12285 Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
12286 begin
12287 if Nm = TSS_Null then
12288 Set_Chars (New_Subp, Chars (Parent_Subp));
12289 else
12290 Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
12291 end if;
12292 end Set_Derived_Name;
12294 -- Local variables
12296 Parent_Overrides_Interface_Primitive : Boolean := False;
12298 -- Start of processing for Derive_Subprogram
12300 begin
12301 New_Subp :=
12302 New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
12303 Set_Ekind (New_Subp, Ekind (Parent_Subp));
12305 -- Check whether the parent overrides an interface primitive
12307 if Is_Overriding_Operation (Parent_Subp) then
12308 declare
12309 E : Entity_Id := Parent_Subp;
12310 begin
12311 while Present (Overridden_Operation (E)) loop
12312 E := Ultimate_Alias (Overridden_Operation (E));
12313 end loop;
12315 Parent_Overrides_Interface_Primitive :=
12316 Is_Dispatching_Operation (E)
12317 and then Present (Find_Dispatching_Type (E))
12318 and then Is_Interface (Find_Dispatching_Type (E));
12319 end;
12320 end if;
12322 -- Check whether the inherited subprogram is a private operation that
12323 -- should be inherited but not yet made visible. Such subprograms can
12324 -- become visible at a later point (e.g., the private part of a public
12325 -- child unit) via Declare_Inherited_Private_Subprograms. If the
12326 -- following predicate is true, then this is not such a private
12327 -- operation and the subprogram simply inherits the name of the parent
12328 -- subprogram. Note the special check for the names of controlled
12329 -- operations, which are currently exempted from being inherited with
12330 -- a hidden name because they must be findable for generation of
12331 -- implicit run-time calls.
12333 if not Is_Hidden (Parent_Subp)
12334 or else Is_Internal (Parent_Subp)
12335 or else Is_Private_Overriding
12336 or else Is_Internal_Name (Chars (Parent_Subp))
12337 or else Chars (Parent_Subp) = Name_Initialize
12338 or else Chars (Parent_Subp) = Name_Adjust
12339 or else Chars (Parent_Subp) = Name_Finalize
12340 then
12341 Set_Derived_Name;
12343 -- An inherited dispatching equality will be overridden by an internally
12344 -- generated one, or by an explicit one, so preserve its name and thus
12345 -- its entry in the dispatch table. Otherwise, if Parent_Subp is a
12346 -- private operation it may become invisible if the full view has
12347 -- progenitors, and the dispatch table will be malformed.
12348 -- We check that the type is limited to handle the anomalous declaration
12349 -- of Limited_Controlled, which is derived from a non-limited type, and
12350 -- which is handled specially elsewhere as well.
12352 elsif Chars (Parent_Subp) = Name_Op_Eq
12353 and then Is_Dispatching_Operation (Parent_Subp)
12354 and then Etype (Parent_Subp) = Standard_Boolean
12355 and then not Is_Limited_Type (Etype (First_Formal (Parent_Subp)))
12356 and then
12357 Etype (First_Formal (Parent_Subp)) =
12358 Etype (Next_Formal (First_Formal (Parent_Subp)))
12359 then
12360 Set_Derived_Name;
12362 -- If parent is hidden, this can be a regular derivation if the
12363 -- parent is immediately visible in a non-instantiating context,
12364 -- or if we are in the private part of an instance. This test
12365 -- should still be refined ???
12367 -- The test for In_Instance_Not_Visible avoids inheriting the derived
12368 -- operation as a non-visible operation in cases where the parent
12369 -- subprogram might not be visible now, but was visible within the
12370 -- original generic, so it would be wrong to make the inherited
12371 -- subprogram non-visible now. (Not clear if this test is fully
12372 -- correct; are there any cases where we should declare the inherited
12373 -- operation as not visible to avoid it being overridden, e.g., when
12374 -- the parent type is a generic actual with private primitives ???)
12376 -- (they should be treated the same as other private inherited
12377 -- subprograms, but it's not clear how to do this cleanly). ???
12379 elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
12380 and then Is_Immediately_Visible (Parent_Subp)
12381 and then not In_Instance)
12382 or else In_Instance_Not_Visible
12383 then
12384 Set_Derived_Name;
12386 -- Ada 2005 (AI-251): Regular derivation if the parent subprogram
12387 -- overrides an interface primitive because interface primitives
12388 -- must be visible in the partial view of the parent (RM 7.3 (7.3/2))
12390 elsif Parent_Overrides_Interface_Primitive then
12391 Set_Derived_Name;
12393 -- Otherwise, the type is inheriting a private operation, so enter
12394 -- it with a special name so it can't be overridden.
12396 else
12397 Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
12398 end if;
12400 Set_Parent (New_Subp, Parent (Derived_Type));
12402 if Present (Actual_Subp) then
12403 Replace_Type (Actual_Subp, New_Subp);
12404 else
12405 Replace_Type (Parent_Subp, New_Subp);
12406 end if;
12408 Conditional_Delay (New_Subp, Parent_Subp);
12410 -- If we are creating a renaming for a primitive operation of an
12411 -- actual of a generic derived type, we must examine the signature
12412 -- of the actual primitive, not that of the generic formal, which for
12413 -- example may be an interface. However the name and initial value
12414 -- of the inherited operation are those of the formal primitive.
12416 Formal := First_Formal (Parent_Subp);
12418 if Present (Actual_Subp) then
12419 Formal_Of_Actual := First_Formal (Actual_Subp);
12420 else
12421 Formal_Of_Actual := Empty;
12422 end if;
12424 while Present (Formal) loop
12425 New_Formal := New_Copy (Formal);
12427 -- Normally we do not go copying parents, but in the case of
12428 -- formals, we need to link up to the declaration (which is the
12429 -- parameter specification), and it is fine to link up to the
12430 -- original formal's parameter specification in this case.
12432 Set_Parent (New_Formal, Parent (Formal));
12433 Append_Entity (New_Formal, New_Subp);
12435 if Present (Formal_Of_Actual) then
12436 Replace_Type (Formal_Of_Actual, New_Formal);
12437 Next_Formal (Formal_Of_Actual);
12438 else
12439 Replace_Type (Formal, New_Formal);
12440 end if;
12442 Next_Formal (Formal);
12443 end loop;
12445 -- If this derivation corresponds to a tagged generic actual, then
12446 -- primitive operations rename those of the actual. Otherwise the
12447 -- primitive operations rename those of the parent type, If the parent
12448 -- renames an intrinsic operator, so does the new subprogram. We except
12449 -- concatenation, which is always properly typed, and does not get
12450 -- expanded as other intrinsic operations.
12452 if No (Actual_Subp) then
12453 if Is_Intrinsic_Subprogram (Parent_Subp) then
12454 Set_Is_Intrinsic_Subprogram (New_Subp);
12456 if Present (Alias (Parent_Subp))
12457 and then Chars (Parent_Subp) /= Name_Op_Concat
12458 then
12459 Set_Alias (New_Subp, Alias (Parent_Subp));
12460 else
12461 Set_Alias (New_Subp, Parent_Subp);
12462 end if;
12464 else
12465 Set_Alias (New_Subp, Parent_Subp);
12466 end if;
12468 else
12469 Set_Alias (New_Subp, Actual_Subp);
12470 end if;
12472 -- Derived subprograms of a tagged type must inherit the convention
12473 -- of the parent subprogram (a requirement of AI-117). Derived
12474 -- subprograms of untagged types simply get convention Ada by default.
12476 if Is_Tagged_Type (Derived_Type) then
12477 Set_Convention (New_Subp, Convention (Parent_Subp));
12478 end if;
12480 -- Predefined controlled operations retain their name even if the parent
12481 -- is hidden (see above), but they are not primitive operations if the
12482 -- ancestor is not visible, for example if the parent is a private
12483 -- extension completed with a controlled extension. Note that a full
12484 -- type that is controlled can break privacy: the flag Is_Controlled is
12485 -- set on both views of the type.
12487 if Is_Controlled (Parent_Type)
12488 and then
12489 (Chars (Parent_Subp) = Name_Initialize
12490 or else Chars (Parent_Subp) = Name_Adjust
12491 or else Chars (Parent_Subp) = Name_Finalize)
12492 and then Is_Hidden (Parent_Subp)
12493 and then not Is_Visibly_Controlled (Parent_Type)
12494 then
12495 Set_Is_Hidden (New_Subp);
12496 end if;
12498 Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
12499 Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
12501 if Ekind (Parent_Subp) = E_Procedure then
12502 Set_Is_Valued_Procedure
12503 (New_Subp, Is_Valued_Procedure (Parent_Subp));
12504 end if;
12506 -- No_Return must be inherited properly. If this is overridden in the
12507 -- case of a dispatching operation, then a check is made in Sem_Disp
12508 -- that the overriding operation is also No_Return (no such check is
12509 -- required for the case of non-dispatching operation.
12511 Set_No_Return (New_Subp, No_Return (Parent_Subp));
12513 -- A derived function with a controlling result is abstract. If the
12514 -- Derived_Type is a nonabstract formal generic derived type, then
12515 -- inherited operations are not abstract: the required check is done at
12516 -- instantiation time. If the derivation is for a generic actual, the
12517 -- function is not abstract unless the actual is.
12519 if Is_Generic_Type (Derived_Type)
12520 and then not Is_Abstract_Type (Derived_Type)
12521 then
12522 null;
12524 -- Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
12525 -- properties of the subprogram, as defined in RM-3.9.3(4/2-6/2).
12527 elsif Ada_Version >= Ada_05
12528 and then (Is_Abstract_Subprogram (Alias (New_Subp))
12529 or else (Is_Tagged_Type (Derived_Type)
12530 and then Etype (New_Subp) = Derived_Type
12531 and then not Is_Null_Extension (Derived_Type))
12532 or else (Is_Tagged_Type (Derived_Type)
12533 and then Ekind (Etype (New_Subp)) =
12534 E_Anonymous_Access_Type
12535 and then Designated_Type (Etype (New_Subp)) =
12536 Derived_Type
12537 and then not Is_Null_Extension (Derived_Type)))
12538 and then No (Actual_Subp)
12539 then
12540 if not Is_Tagged_Type (Derived_Type)
12541 or else Is_Abstract_Type (Derived_Type)
12542 or else Is_Abstract_Subprogram (Alias (New_Subp))
12543 then
12544 Set_Is_Abstract_Subprogram (New_Subp);
12545 else
12546 Set_Requires_Overriding (New_Subp);
12547 end if;
12549 elsif Ada_Version < Ada_05
12550 and then (Is_Abstract_Subprogram (Alias (New_Subp))
12551 or else (Is_Tagged_Type (Derived_Type)
12552 and then Etype (New_Subp) = Derived_Type
12553 and then No (Actual_Subp)))
12554 then
12555 Set_Is_Abstract_Subprogram (New_Subp);
12557 -- Finally, if the parent type is abstract we must verify that all
12558 -- inherited operations are either non-abstract or overridden, or that
12559 -- the derived type itself is abstract (this check is performed at the
12560 -- end of a package declaration, in Check_Abstract_Overriding). A
12561 -- private overriding in the parent type will not be visible in the
12562 -- derivation if we are not in an inner package or in a child unit of
12563 -- the parent type, in which case the abstractness of the inherited
12564 -- operation is carried to the new subprogram.
12566 elsif Is_Abstract_Type (Parent_Type)
12567 and then not In_Open_Scopes (Scope (Parent_Type))
12568 and then Is_Private_Overriding
12569 and then Is_Abstract_Subprogram (Visible_Subp)
12570 then
12571 if No (Actual_Subp) then
12572 Set_Alias (New_Subp, Visible_Subp);
12573 Set_Is_Abstract_Subprogram (New_Subp, True);
12575 else
12576 -- If this is a derivation for an instance of a formal derived
12577 -- type, abstractness comes from the primitive operation of the
12578 -- actual, not from the operation inherited from the ancestor.
12580 Set_Is_Abstract_Subprogram
12581 (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
12582 end if;
12583 end if;
12585 New_Overloaded_Entity (New_Subp, Derived_Type);
12587 -- Check for case of a derived subprogram for the instantiation of a
12588 -- formal derived tagged type, if so mark the subprogram as dispatching
12589 -- and inherit the dispatching attributes of the parent subprogram. The
12590 -- derived subprogram is effectively renaming of the actual subprogram,
12591 -- so it needs to have the same attributes as the actual.
12593 if Present (Actual_Subp)
12594 and then Is_Dispatching_Operation (Parent_Subp)
12595 then
12596 Set_Is_Dispatching_Operation (New_Subp);
12598 if Present (DTC_Entity (Parent_Subp)) then
12599 Set_DTC_Entity (New_Subp, DTC_Entity (Parent_Subp));
12600 Set_DT_Position (New_Subp, DT_Position (Parent_Subp));
12601 end if;
12602 end if;
12604 -- Indicate that a derived subprogram does not require a body and that
12605 -- it does not require processing of default expressions.
12607 Set_Has_Completion (New_Subp);
12608 Set_Default_Expressions_Processed (New_Subp);
12610 if Ekind (New_Subp) = E_Function then
12611 Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
12612 end if;
12613 end Derive_Subprogram;
12615 ------------------------
12616 -- Derive_Subprograms --
12617 ------------------------
12619 procedure Derive_Subprograms
12620 (Parent_Type : Entity_Id;
12621 Derived_Type : Entity_Id;
12622 Generic_Actual : Entity_Id := Empty)
12624 Op_List : constant Elist_Id :=
12625 Collect_Primitive_Operations (Parent_Type);
12627 function Check_Derived_Type return Boolean;
12628 -- Check that all primitive inherited from Parent_Type are found in
12629 -- the list of primitives of Derived_Type exactly in the same order.
12631 function Check_Derived_Type return Boolean is
12632 E : Entity_Id;
12633 Elmt : Elmt_Id;
12634 List : Elist_Id;
12635 New_Subp : Entity_Id;
12636 Op_Elmt : Elmt_Id;
12637 Subp : Entity_Id;
12639 begin
12640 -- Traverse list of entities in the current scope searching for
12641 -- an incomplete type whose full-view is derived type
12643 E := First_Entity (Scope (Derived_Type));
12644 while Present (E)
12645 and then E /= Derived_Type
12646 loop
12647 if Ekind (E) = E_Incomplete_Type
12648 and then Present (Full_View (E))
12649 and then Full_View (E) = Derived_Type
12650 then
12651 -- Disable this test if Derived_Type completes an incomplete
12652 -- type because in such case more primitives can be added
12653 -- later to the list of primitives of Derived_Type by routine
12654 -- Process_Incomplete_Dependents
12656 return True;
12657 end if;
12659 E := Next_Entity (E);
12660 end loop;
12662 List := Collect_Primitive_Operations (Derived_Type);
12663 Elmt := First_Elmt (List);
12665 Op_Elmt := First_Elmt (Op_List);
12666 while Present (Op_Elmt) loop
12667 Subp := Node (Op_Elmt);
12668 New_Subp := Node (Elmt);
12670 -- At this early stage Derived_Type has no entities with attribute
12671 -- Interface_Alias. In addition, such primitives are always
12672 -- located at the end of the list of primitives of Parent_Type.
12673 -- Therefore, if found we can safely stop processing pending
12674 -- entities.
12676 exit when Present (Interface_Alias (Subp));
12678 -- Handle hidden entities
12680 if not Is_Predefined_Dispatching_Operation (Subp)
12681 and then Is_Hidden (Subp)
12682 then
12683 if Present (New_Subp)
12684 and then Primitive_Names_Match (Subp, New_Subp)
12685 then
12686 Next_Elmt (Elmt);
12687 end if;
12689 else
12690 if not Present (New_Subp)
12691 or else Ekind (Subp) /= Ekind (New_Subp)
12692 or else not Primitive_Names_Match (Subp, New_Subp)
12693 then
12694 return False;
12695 end if;
12697 Next_Elmt (Elmt);
12698 end if;
12700 Next_Elmt (Op_Elmt);
12701 end loop;
12703 return True;
12704 end Check_Derived_Type;
12706 -- Local variables
12708 Alias_Subp : Entity_Id;
12709 Act_List : Elist_Id;
12710 Act_Elmt : Elmt_Id := No_Elmt;
12711 Act_Subp : Entity_Id := Empty;
12712 Elmt : Elmt_Id;
12713 Need_Search : Boolean := False;
12714 New_Subp : Entity_Id := Empty;
12715 Parent_Base : Entity_Id;
12716 Subp : Entity_Id;
12718 -- Start of processing for Derive_Subprograms
12720 begin
12721 if Ekind (Parent_Type) = E_Record_Type_With_Private
12722 and then Has_Discriminants (Parent_Type)
12723 and then Present (Full_View (Parent_Type))
12724 then
12725 Parent_Base := Full_View (Parent_Type);
12726 else
12727 Parent_Base := Parent_Type;
12728 end if;
12730 if Present (Generic_Actual) then
12731 Act_List := Collect_Primitive_Operations (Generic_Actual);
12732 Act_Elmt := First_Elmt (Act_List);
12733 end if;
12735 -- Derive primitives inherited from the parent. Note that if the generic
12736 -- actual is present, this is not really a type derivation, it is a
12737 -- completion within an instance.
12739 -- Case 1: Derived_Type does not implement interfaces
12741 if not Is_Tagged_Type (Derived_Type)
12742 or else (not Has_Interfaces (Derived_Type)
12743 and then not (Present (Generic_Actual)
12744 and then
12745 Has_Interfaces (Generic_Actual)))
12746 then
12747 Elmt := First_Elmt (Op_List);
12748 while Present (Elmt) loop
12749 Subp := Node (Elmt);
12751 -- Literals are derived earlier in the process of building the
12752 -- derived type, and are skipped here.
12754 if Ekind (Subp) = E_Enumeration_Literal then
12755 null;
12757 -- The actual is a direct descendant and the common primitive
12758 -- operations appear in the same order.
12760 -- If the generic parent type is present, the derived type is an
12761 -- instance of a formal derived type, and within the instance its
12762 -- operations are those of the actual. We derive from the formal
12763 -- type but make the inherited operations aliases of the
12764 -- corresponding operations of the actual.
12766 else
12767 pragma Assert (No (Node (Act_Elmt))
12768 or else (Primitive_Names_Match (Subp, Node (Act_Elmt))
12769 and then
12770 Type_Conformant (Subp, Node (Act_Elmt),
12771 Skip_Controlling_Formals => True)));
12773 Derive_Subprogram
12774 (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
12776 if Present (Act_Elmt) then
12777 Next_Elmt (Act_Elmt);
12778 end if;
12779 end if;
12781 Next_Elmt (Elmt);
12782 end loop;
12784 -- Case 2: Derived_Type implements interfaces
12786 else
12787 -- If the parent type has no predefined primitives we remove
12788 -- predefined primitives from the list of primitives of generic
12789 -- actual to simplify the complexity of this algorithm.
12791 if Present (Generic_Actual) then
12792 declare
12793 Has_Predefined_Primitives : Boolean := False;
12795 begin
12796 -- Check if the parent type has predefined primitives
12798 Elmt := First_Elmt (Op_List);
12799 while Present (Elmt) loop
12800 Subp := Node (Elmt);
12802 if Is_Predefined_Dispatching_Operation (Subp)
12803 and then not Comes_From_Source (Ultimate_Alias (Subp))
12804 then
12805 Has_Predefined_Primitives := True;
12806 exit;
12807 end if;
12809 Next_Elmt (Elmt);
12810 end loop;
12812 -- Remove predefined primitives of Generic_Actual. We must use
12813 -- an auxiliary list because in case of tagged types the value
12814 -- returned by Collect_Primitive_Operations is the value stored
12815 -- in its Primitive_Operations attribute (and we don't want to
12816 -- modify its current contents).
12818 if not Has_Predefined_Primitives then
12819 declare
12820 Aux_List : constant Elist_Id := New_Elmt_List;
12822 begin
12823 Elmt := First_Elmt (Act_List);
12824 while Present (Elmt) loop
12825 Subp := Node (Elmt);
12827 if not Is_Predefined_Dispatching_Operation (Subp)
12828 or else Comes_From_Source (Subp)
12829 then
12830 Append_Elmt (Subp, Aux_List);
12831 end if;
12833 Next_Elmt (Elmt);
12834 end loop;
12836 Act_List := Aux_List;
12837 end;
12838 end if;
12840 Act_Elmt := First_Elmt (Act_List);
12841 Act_Subp := Node (Act_Elmt);
12842 end;
12843 end if;
12845 -- Stage 1: If the generic actual is not present we derive the
12846 -- primitives inherited from the parent type. If the generic parent
12847 -- type is present, the derived type is an instance of a formal
12848 -- derived type, and within the instance its operations are those of
12849 -- the actual. We derive from the formal type but make the inherited
12850 -- operations aliases of the corresponding operations of the actual.
12852 Elmt := First_Elmt (Op_List);
12853 while Present (Elmt) loop
12854 Subp := Node (Elmt);
12855 Alias_Subp := Ultimate_Alias (Subp);
12857 -- Do not derive internal entities of the parent that link
12858 -- interface primitives and its covering primitive. These
12859 -- entities will be added to this type when frozen.
12861 if Present (Interface_Alias (Subp)) then
12862 goto Continue;
12863 end if;
12865 -- If the generic actual is present find the corresponding
12866 -- operation in the generic actual. If the parent type is a
12867 -- direct ancestor of the derived type then, even if it is an
12868 -- interface, the operations are inherited from the primary
12869 -- dispatch table and are in the proper order. If we detect here
12870 -- that primitives are not in the same order we traverse the list
12871 -- of primitive operations of the actual to find the one that
12872 -- implements the interface primitive.
12874 if Need_Search
12875 or else
12876 (Present (Generic_Actual)
12877 and then Present (Act_Subp)
12878 and then not
12879 (Primitive_Names_Match (Subp, Act_Subp)
12880 and then
12881 Type_Conformant (Subp, Act_Subp,
12882 Skip_Controlling_Formals => True)))
12883 then
12884 pragma Assert (not Is_Ancestor (Parent_Base, Generic_Actual));
12886 -- Remember that we need searching for all pending primitives
12888 Need_Search := True;
12890 -- Handle entities associated with interface primitives
12892 if Present (Alias_Subp)
12893 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
12894 and then not Is_Predefined_Dispatching_Operation (Subp)
12895 then
12896 -- Search for the primitive in the homonym chain
12898 Act_Subp :=
12899 Find_Primitive_Covering_Interface
12900 (Tagged_Type => Generic_Actual,
12901 Iface_Prim => Alias_Subp);
12903 -- Previous search may not locate primitives covering
12904 -- interfaces defined in generics units or instantiations.
12905 -- (it fails if the covering primitive has formals whose
12906 -- type is also defined in generics or instantiations).
12907 -- In such case we search in the list of primitives of the
12908 -- generic actual for the internal entity that links the
12909 -- interface primitive and the covering primitive.
12911 if No (Act_Subp)
12912 and then Is_Generic_Type (Parent_Type)
12913 then
12914 -- This code has been designed to handle only generic
12915 -- formals that implement interfaces that are defined
12916 -- in a generic unit or instantiation. If this code is
12917 -- needed for other cases we must review it because
12918 -- (given that it relies on Original_Location to locate
12919 -- the primitive of Generic_Actual that covers the
12920 -- interface) it could leave linked through attribute
12921 -- Alias entities of unrelated instantiations).
12923 pragma Assert
12924 (Is_Generic_Unit
12925 (Scope (Find_Dispatching_Type (Alias_Subp)))
12926 or else
12927 Instantiation_Depth
12928 (Sloc (Find_Dispatching_Type (Alias_Subp))) > 0);
12930 declare
12931 Iface_Prim_Loc : constant Source_Ptr :=
12932 Original_Location (Sloc (Alias_Subp));
12933 Elmt : Elmt_Id;
12934 Prim : Entity_Id;
12935 begin
12936 Elmt :=
12937 First_Elmt (Primitive_Operations (Generic_Actual));
12939 Search : while Present (Elmt) loop
12940 Prim := Node (Elmt);
12942 if Present (Interface_Alias (Prim))
12943 and then Original_Location
12944 (Sloc (Interface_Alias (Prim)))
12945 = Iface_Prim_Loc
12946 then
12947 Act_Subp := Alias (Prim);
12948 exit Search;
12949 end if;
12951 Next_Elmt (Elmt);
12952 end loop Search;
12953 end;
12954 end if;
12956 pragma Assert (Present (Act_Subp)
12957 or else Is_Abstract_Type (Generic_Actual)
12958 or else Serious_Errors_Detected > 0);
12960 -- Handle predefined primitives plus the rest of user-defined
12961 -- primitives
12963 else
12964 Act_Elmt := First_Elmt (Act_List);
12965 while Present (Act_Elmt) loop
12966 Act_Subp := Node (Act_Elmt);
12968 exit when Primitive_Names_Match (Subp, Act_Subp)
12969 and then Type_Conformant
12970 (Subp, Act_Subp,
12971 Skip_Controlling_Formals => True)
12972 and then No (Interface_Alias (Act_Subp));
12974 Next_Elmt (Act_Elmt);
12975 end loop;
12977 if No (Act_Elmt) then
12978 Act_Subp := Empty;
12979 end if;
12980 end if;
12981 end if;
12983 -- Case 1: If the parent is a limited interface then it has the
12984 -- predefined primitives of synchronized interfaces. However, the
12985 -- actual type may be a non-limited type and hence it does not
12986 -- have such primitives.
12988 if Present (Generic_Actual)
12989 and then not Present (Act_Subp)
12990 and then Is_Limited_Interface (Parent_Base)
12991 and then Is_Predefined_Interface_Primitive (Subp)
12992 then
12993 null;
12995 -- Case 2: Inherit entities associated with interfaces that
12996 -- were not covered by the parent type. We exclude here null
12997 -- interface primitives because they do not need special
12998 -- management.
13000 elsif Present (Alias (Subp))
13001 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
13002 and then not
13003 (Nkind (Parent (Alias_Subp)) = N_Procedure_Specification
13004 and then Null_Present (Parent (Alias_Subp)))
13005 then
13006 Derive_Subprogram
13007 (New_Subp => New_Subp,
13008 Parent_Subp => Alias_Subp,
13009 Derived_Type => Derived_Type,
13010 Parent_Type => Find_Dispatching_Type (Alias_Subp),
13011 Actual_Subp => Act_Subp);
13013 if No (Generic_Actual) then
13014 Set_Alias (New_Subp, Subp);
13015 end if;
13017 -- Case 3: Common derivation
13019 else
13020 Derive_Subprogram
13021 (New_Subp => New_Subp,
13022 Parent_Subp => Subp,
13023 Derived_Type => Derived_Type,
13024 Parent_Type => Parent_Base,
13025 Actual_Subp => Act_Subp);
13026 end if;
13028 -- No need to update Act_Elm if we must search for the
13029 -- corresponding operation in the generic actual
13031 if not Need_Search
13032 and then Present (Act_Elmt)
13033 then
13034 Next_Elmt (Act_Elmt);
13035 Act_Subp := Node (Act_Elmt);
13036 end if;
13038 <<Continue>>
13039 Next_Elmt (Elmt);
13040 end loop;
13042 -- Inherit additional operations from progenitors. If the derived
13043 -- type is a generic actual, there are not new primitive operations
13044 -- for the type because it has those of the actual, and therefore
13045 -- nothing needs to be done. The renamings generated above are not
13046 -- primitive operations, and their purpose is simply to make the
13047 -- proper operations visible within an instantiation.
13049 if No (Generic_Actual) then
13050 Derive_Progenitor_Subprograms (Parent_Base, Derived_Type);
13051 end if;
13052 end if;
13054 -- Final check: Direct descendants must have their primitives in the
13055 -- same order. We exclude from this test non-tagged types and instances
13056 -- of formal derived types. We skip this test if we have already
13057 -- reported serious errors in the sources.
13059 pragma Assert (not Is_Tagged_Type (Derived_Type)
13060 or else Present (Generic_Actual)
13061 or else Serious_Errors_Detected > 0
13062 or else Check_Derived_Type);
13063 end Derive_Subprograms;
13065 --------------------------------
13066 -- Derived_Standard_Character --
13067 --------------------------------
13069 procedure Derived_Standard_Character
13070 (N : Node_Id;
13071 Parent_Type : Entity_Id;
13072 Derived_Type : Entity_Id)
13074 Loc : constant Source_Ptr := Sloc (N);
13075 Def : constant Node_Id := Type_Definition (N);
13076 Indic : constant Node_Id := Subtype_Indication (Def);
13077 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
13078 Implicit_Base : constant Entity_Id :=
13079 Create_Itype
13080 (E_Enumeration_Type, N, Derived_Type, 'B');
13082 Lo : Node_Id;
13083 Hi : Node_Id;
13085 begin
13086 Discard_Node (Process_Subtype (Indic, N));
13088 Set_Etype (Implicit_Base, Parent_Base);
13089 Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
13090 Set_RM_Size (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
13092 Set_Is_Character_Type (Implicit_Base, True);
13093 Set_Has_Delayed_Freeze (Implicit_Base);
13095 -- The bounds of the implicit base are the bounds of the parent base.
13096 -- Note that their type is the parent base.
13098 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
13099 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
13101 Set_Scalar_Range (Implicit_Base,
13102 Make_Range (Loc,
13103 Low_Bound => Lo,
13104 High_Bound => Hi));
13106 Conditional_Delay (Derived_Type, Parent_Type);
13108 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
13109 Set_Etype (Derived_Type, Implicit_Base);
13110 Set_Size_Info (Derived_Type, Parent_Type);
13112 if Unknown_RM_Size (Derived_Type) then
13113 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
13114 end if;
13116 Set_Is_Character_Type (Derived_Type, True);
13118 if Nkind (Indic) /= N_Subtype_Indication then
13120 -- If no explicit constraint, the bounds are those
13121 -- of the parent type.
13123 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Type));
13124 Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
13125 Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
13126 end if;
13128 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
13130 -- Because the implicit base is used in the conversion of the bounds, we
13131 -- have to freeze it now. This is similar to what is done for numeric
13132 -- types, and it equally suspicious, but otherwise a non-static bound
13133 -- will have a reference to an unfrozen type, which is rejected by Gigi
13134 -- (???). This requires specific care for definition of stream
13135 -- attributes. For details, see comments at the end of
13136 -- Build_Derived_Numeric_Type.
13138 Freeze_Before (N, Implicit_Base);
13139 end Derived_Standard_Character;
13141 ------------------------------
13142 -- Derived_Type_Declaration --
13143 ------------------------------
13145 procedure Derived_Type_Declaration
13146 (T : Entity_Id;
13147 N : Node_Id;
13148 Is_Completion : Boolean)
13150 Parent_Type : Entity_Id;
13152 function Comes_From_Generic (Typ : Entity_Id) return Boolean;
13153 -- Check whether the parent type is a generic formal, or derives
13154 -- directly or indirectly from one.
13156 ------------------------
13157 -- Comes_From_Generic --
13158 ------------------------
13160 function Comes_From_Generic (Typ : Entity_Id) return Boolean is
13161 begin
13162 if Is_Generic_Type (Typ) then
13163 return True;
13165 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
13166 return True;
13168 elsif Is_Private_Type (Typ)
13169 and then Present (Full_View (Typ))
13170 and then Is_Generic_Type (Root_Type (Full_View (Typ)))
13171 then
13172 return True;
13174 elsif Is_Generic_Actual_Type (Typ) then
13175 return True;
13177 else
13178 return False;
13179 end if;
13180 end Comes_From_Generic;
13182 -- Local variables
13184 Def : constant Node_Id := Type_Definition (N);
13185 Iface_Def : Node_Id;
13186 Indic : constant Node_Id := Subtype_Indication (Def);
13187 Extension : constant Node_Id := Record_Extension_Part (Def);
13188 Parent_Node : Node_Id;
13189 Parent_Scope : Entity_Id;
13190 Taggd : Boolean;
13192 -- Start of processing for Derived_Type_Declaration
13194 begin
13195 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
13197 -- Ada 2005 (AI-251): In case of interface derivation check that the
13198 -- parent is also an interface.
13200 if Interface_Present (Def) then
13201 if not Is_Interface (Parent_Type) then
13202 Diagnose_Interface (Indic, Parent_Type);
13204 else
13205 Parent_Node := Parent (Base_Type (Parent_Type));
13206 Iface_Def := Type_Definition (Parent_Node);
13208 -- Ada 2005 (AI-251): Limited interfaces can only inherit from
13209 -- other limited interfaces.
13211 if Limited_Present (Def) then
13212 if Limited_Present (Iface_Def) then
13213 null;
13215 elsif Protected_Present (Iface_Def) then
13216 Error_Msg_NE
13217 ("descendant of& must be declared"
13218 & " as a protected interface",
13219 N, Parent_Type);
13221 elsif Synchronized_Present (Iface_Def) then
13222 Error_Msg_NE
13223 ("descendant of& must be declared"
13224 & " as a synchronized interface",
13225 N, Parent_Type);
13227 elsif Task_Present (Iface_Def) then
13228 Error_Msg_NE
13229 ("descendant of& must be declared as a task interface",
13230 N, Parent_Type);
13232 else
13233 Error_Msg_N
13234 ("(Ada 2005) limited interface cannot "
13235 & "inherit from non-limited interface", Indic);
13236 end if;
13238 -- Ada 2005 (AI-345): Non-limited interfaces can only inherit
13239 -- from non-limited or limited interfaces.
13241 elsif not Protected_Present (Def)
13242 and then not Synchronized_Present (Def)
13243 and then not Task_Present (Def)
13244 then
13245 if Limited_Present (Iface_Def) then
13246 null;
13248 elsif Protected_Present (Iface_Def) then
13249 Error_Msg_NE
13250 ("descendant of& must be declared"
13251 & " as a protected interface",
13252 N, Parent_Type);
13254 elsif Synchronized_Present (Iface_Def) then
13255 Error_Msg_NE
13256 ("descendant of& must be declared"
13257 & " as a synchronized interface",
13258 N, Parent_Type);
13260 elsif Task_Present (Iface_Def) then
13261 Error_Msg_NE
13262 ("descendant of& must be declared as a task interface",
13263 N, Parent_Type);
13264 else
13265 null;
13266 end if;
13267 end if;
13268 end if;
13269 end if;
13271 if Is_Tagged_Type (Parent_Type)
13272 and then Is_Concurrent_Type (Parent_Type)
13273 and then not Is_Interface (Parent_Type)
13274 then
13275 Error_Msg_N
13276 ("parent type of a record extension cannot be "
13277 & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
13278 Set_Etype (T, Any_Type);
13279 return;
13280 end if;
13282 -- Ada 2005 (AI-251): Decorate all the names in the list of ancestor
13283 -- interfaces
13285 if Is_Tagged_Type (Parent_Type)
13286 and then Is_Non_Empty_List (Interface_List (Def))
13287 then
13288 declare
13289 Intf : Node_Id;
13290 T : Entity_Id;
13292 begin
13293 Intf := First (Interface_List (Def));
13294 while Present (Intf) loop
13295 T := Find_Type_Of_Subtype_Indic (Intf);
13297 if not Is_Interface (T) then
13298 Diagnose_Interface (Intf, T);
13300 -- Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
13301 -- a limited type from having a nonlimited progenitor.
13303 elsif (Limited_Present (Def)
13304 or else (not Is_Interface (Parent_Type)
13305 and then Is_Limited_Type (Parent_Type)))
13306 and then not Is_Limited_Interface (T)
13307 then
13308 Error_Msg_NE
13309 ("progenitor interface& of limited type must be limited",
13310 N, T);
13311 end if;
13313 Next (Intf);
13314 end loop;
13315 end;
13316 end if;
13318 if Parent_Type = Any_Type
13319 or else Etype (Parent_Type) = Any_Type
13320 or else (Is_Class_Wide_Type (Parent_Type)
13321 and then Etype (Parent_Type) = T)
13322 then
13323 -- If Parent_Type is undefined or illegal, make new type into a
13324 -- subtype of Any_Type, and set a few attributes to prevent cascaded
13325 -- errors. If this is a self-definition, emit error now.
13327 if T = Parent_Type
13328 or else T = Etype (Parent_Type)
13329 then
13330 Error_Msg_N ("type cannot be used in its own definition", Indic);
13331 end if;
13333 Set_Ekind (T, Ekind (Parent_Type));
13334 Set_Etype (T, Any_Type);
13335 Set_Scalar_Range (T, Scalar_Range (Any_Type));
13337 if Is_Tagged_Type (T) then
13338 Set_Primitive_Operations (T, New_Elmt_List);
13339 end if;
13341 return;
13342 end if;
13344 -- Ada 2005 (AI-251): The case in which the parent of the full-view is
13345 -- an interface is special because the list of interfaces in the full
13346 -- view can be given in any order. For example:
13348 -- type A is interface;
13349 -- type B is interface and A;
13350 -- type D is new B with private;
13351 -- private
13352 -- type D is new A and B with null record; -- 1 --
13354 -- In this case we perform the following transformation of -1-:
13356 -- type D is new B and A with null record;
13358 -- If the parent of the full-view covers the parent of the partial-view
13359 -- we have two possible cases:
13361 -- 1) They have the same parent
13362 -- 2) The parent of the full-view implements some further interfaces
13364 -- In both cases we do not need to perform the transformation. In the
13365 -- first case the source program is correct and the transformation is
13366 -- not needed; in the second case the source program does not fulfill
13367 -- the no-hidden interfaces rule (AI-396) and the error will be reported
13368 -- later.
13370 -- This transformation not only simplifies the rest of the analysis of
13371 -- this type declaration but also simplifies the correct generation of
13372 -- the object layout to the expander.
13374 if In_Private_Part (Current_Scope)
13375 and then Is_Interface (Parent_Type)
13376 then
13377 declare
13378 Iface : Node_Id;
13379 Partial_View : Entity_Id;
13380 Partial_View_Parent : Entity_Id;
13381 New_Iface : Node_Id;
13383 begin
13384 -- Look for the associated private type declaration
13386 Partial_View := First_Entity (Current_Scope);
13387 loop
13388 exit when No (Partial_View)
13389 or else (Has_Private_Declaration (Partial_View)
13390 and then Full_View (Partial_View) = T);
13392 Next_Entity (Partial_View);
13393 end loop;
13395 -- If the partial view was not found then the source code has
13396 -- errors and the transformation is not needed.
13398 if Present (Partial_View) then
13399 Partial_View_Parent := Etype (Partial_View);
13401 -- If the parent of the full-view covers the parent of the
13402 -- partial-view we have nothing else to do.
13404 if Interface_Present_In_Ancestor
13405 (Parent_Type, Partial_View_Parent)
13406 then
13407 null;
13409 -- Traverse the list of interfaces of the full-view to look
13410 -- for the parent of the partial-view and perform the tree
13411 -- transformation.
13413 else
13414 Iface := First (Interface_List (Def));
13415 while Present (Iface) loop
13416 if Etype (Iface) = Etype (Partial_View) then
13417 Rewrite (Subtype_Indication (Def),
13418 New_Copy (Subtype_Indication
13419 (Parent (Partial_View))));
13421 New_Iface := Make_Identifier (Sloc (N),
13422 Chars (Parent_Type));
13423 Append (New_Iface, Interface_List (Def));
13425 -- Analyze the transformed code
13427 Derived_Type_Declaration (T, N, Is_Completion);
13428 return;
13429 end if;
13431 Next (Iface);
13432 end loop;
13433 end if;
13434 end if;
13435 end;
13436 end if;
13438 -- Only composite types other than array types are allowed to have
13439 -- discriminants.
13441 if Present (Discriminant_Specifications (N))
13442 and then (Is_Elementary_Type (Parent_Type)
13443 or else Is_Array_Type (Parent_Type))
13444 and then not Error_Posted (N)
13445 then
13446 Error_Msg_N
13447 ("elementary or array type cannot have discriminants",
13448 Defining_Identifier (First (Discriminant_Specifications (N))));
13449 Set_Has_Discriminants (T, False);
13450 end if;
13452 -- In Ada 83, a derived type defined in a package specification cannot
13453 -- be used for further derivation until the end of its visible part.
13454 -- Note that derivation in the private part of the package is allowed.
13456 if Ada_Version = Ada_83
13457 and then Is_Derived_Type (Parent_Type)
13458 and then In_Visible_Part (Scope (Parent_Type))
13459 then
13460 if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
13461 Error_Msg_N
13462 ("(Ada 83): premature use of type for derivation", Indic);
13463 end if;
13464 end if;
13466 -- Check for early use of incomplete or private type
13468 if Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
13469 Error_Msg_N ("premature derivation of incomplete type", Indic);
13470 return;
13472 elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
13473 and then not Comes_From_Generic (Parent_Type))
13474 or else Has_Private_Component (Parent_Type)
13475 then
13476 -- The ancestor type of a formal type can be incomplete, in which
13477 -- case only the operations of the partial view are available in
13478 -- the generic. Subsequent checks may be required when the full
13479 -- view is analyzed, to verify that derivation from a tagged type
13480 -- has an extension.
13482 if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
13483 null;
13485 elsif No (Underlying_Type (Parent_Type))
13486 or else Has_Private_Component (Parent_Type)
13487 then
13488 Error_Msg_N
13489 ("premature derivation of derived or private type", Indic);
13491 -- Flag the type itself as being in error, this prevents some
13492 -- nasty problems with subsequent uses of the malformed type.
13494 Set_Error_Posted (T);
13496 -- Check that within the immediate scope of an untagged partial
13497 -- view it's illegal to derive from the partial view if the
13498 -- full view is tagged. (7.3(7))
13500 -- We verify that the Parent_Type is a partial view by checking
13501 -- that it is not a Full_Type_Declaration (i.e. a private type or
13502 -- private extension declaration), to distinguish a partial view
13503 -- from a derivation from a private type which also appears as
13504 -- E_Private_Type.
13506 elsif Present (Full_View (Parent_Type))
13507 and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
13508 and then not Is_Tagged_Type (Parent_Type)
13509 and then Is_Tagged_Type (Full_View (Parent_Type))
13510 then
13511 Parent_Scope := Scope (T);
13512 while Present (Parent_Scope)
13513 and then Parent_Scope /= Standard_Standard
13514 loop
13515 if Parent_Scope = Scope (Parent_Type) then
13516 Error_Msg_N
13517 ("premature derivation from type with tagged full view",
13518 Indic);
13519 end if;
13521 Parent_Scope := Scope (Parent_Scope);
13522 end loop;
13523 end if;
13524 end if;
13526 -- Check that form of derivation is appropriate
13528 Taggd := Is_Tagged_Type (Parent_Type);
13530 -- Perhaps the parent type should be changed to the class-wide type's
13531 -- specific type in this case to prevent cascading errors ???
13533 if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
13534 Error_Msg_N ("parent type must not be a class-wide type", Indic);
13535 return;
13536 end if;
13538 if Present (Extension) and then not Taggd then
13539 Error_Msg_N
13540 ("type derived from untagged type cannot have extension", Indic);
13542 elsif No (Extension) and then Taggd then
13544 -- If this declaration is within a private part (or body) of a
13545 -- generic instantiation then the derivation is allowed (the parent
13546 -- type can only appear tagged in this case if it's a generic actual
13547 -- type, since it would otherwise have been rejected in the analysis
13548 -- of the generic template).
13550 if not Is_Generic_Actual_Type (Parent_Type)
13551 or else In_Visible_Part (Scope (Parent_Type))
13552 then
13553 Error_Msg_N
13554 ("type derived from tagged type must have extension", Indic);
13555 end if;
13556 end if;
13558 -- AI-443: Synchronized formal derived types require a private
13559 -- extension. There is no point in checking the ancestor type or
13560 -- the progenitors since the construct is wrong to begin with.
13562 if Ada_Version >= Ada_05
13563 and then Is_Generic_Type (T)
13564 and then Present (Original_Node (N))
13565 then
13566 declare
13567 Decl : constant Node_Id := Original_Node (N);
13569 begin
13570 if Nkind (Decl) = N_Formal_Type_Declaration
13571 and then Nkind (Formal_Type_Definition (Decl)) =
13572 N_Formal_Derived_Type_Definition
13573 and then Synchronized_Present (Formal_Type_Definition (Decl))
13574 and then No (Extension)
13576 -- Avoid emitting a duplicate error message
13578 and then not Error_Posted (Indic)
13579 then
13580 Error_Msg_N
13581 ("synchronized derived type must have extension", N);
13582 end if;
13583 end;
13584 end if;
13586 if Null_Exclusion_Present (Def)
13587 and then not Is_Access_Type (Parent_Type)
13588 then
13589 Error_Msg_N ("null exclusion can only apply to an access type", N);
13590 end if;
13592 -- Avoid deriving parent primitives of underlying record views
13594 Build_Derived_Type (N, Parent_Type, T, Is_Completion,
13595 Derive_Subps => not Is_Underlying_Record_View (T));
13597 -- AI-419: The parent type of an explicitly limited derived type must
13598 -- be a limited type or a limited interface.
13600 if Limited_Present (Def) then
13601 Set_Is_Limited_Record (T);
13603 if Is_Interface (T) then
13604 Set_Is_Limited_Interface (T);
13605 end if;
13607 if not Is_Limited_Type (Parent_Type)
13608 and then
13609 (not Is_Interface (Parent_Type)
13610 or else not Is_Limited_Interface (Parent_Type))
13611 then
13612 Error_Msg_NE
13613 ("parent type& of limited type must be limited",
13614 N, Parent_Type);
13615 end if;
13616 end if;
13617 end Derived_Type_Declaration;
13619 ------------------------
13620 -- Diagnose_Interface --
13621 ------------------------
13623 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id) is
13624 begin
13625 if not Is_Interface (E)
13626 and then E /= Any_Type
13627 then
13628 Error_Msg_NE ("(Ada 2005) & must be an interface", N, E);
13629 end if;
13630 end Diagnose_Interface;
13632 ----------------------------------
13633 -- Enumeration_Type_Declaration --
13634 ----------------------------------
13636 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
13637 Ev : Uint;
13638 L : Node_Id;
13639 R_Node : Node_Id;
13640 B_Node : Node_Id;
13642 begin
13643 -- Create identifier node representing lower bound
13645 B_Node := New_Node (N_Identifier, Sloc (Def));
13646 L := First (Literals (Def));
13647 Set_Chars (B_Node, Chars (L));
13648 Set_Entity (B_Node, L);
13649 Set_Etype (B_Node, T);
13650 Set_Is_Static_Expression (B_Node, True);
13652 R_Node := New_Node (N_Range, Sloc (Def));
13653 Set_Low_Bound (R_Node, B_Node);
13655 Set_Ekind (T, E_Enumeration_Type);
13656 Set_First_Literal (T, L);
13657 Set_Etype (T, T);
13658 Set_Is_Constrained (T);
13660 Ev := Uint_0;
13662 -- Loop through literals of enumeration type setting pos and rep values
13663 -- except that if the Ekind is already set, then it means the literal
13664 -- was already constructed (case of a derived type declaration and we
13665 -- should not disturb the Pos and Rep values.
13667 while Present (L) loop
13668 if Ekind (L) /= E_Enumeration_Literal then
13669 Set_Ekind (L, E_Enumeration_Literal);
13670 Set_Enumeration_Pos (L, Ev);
13671 Set_Enumeration_Rep (L, Ev);
13672 Set_Is_Known_Valid (L, True);
13673 end if;
13675 Set_Etype (L, T);
13676 New_Overloaded_Entity (L);
13677 Generate_Definition (L);
13678 Set_Convention (L, Convention_Intrinsic);
13680 if Nkind (L) = N_Defining_Character_Literal then
13681 Set_Is_Character_Type (T, True);
13682 end if;
13684 Ev := Ev + 1;
13685 Next (L);
13686 end loop;
13688 -- Now create a node representing upper bound
13690 B_Node := New_Node (N_Identifier, Sloc (Def));
13691 Set_Chars (B_Node, Chars (Last (Literals (Def))));
13692 Set_Entity (B_Node, Last (Literals (Def)));
13693 Set_Etype (B_Node, T);
13694 Set_Is_Static_Expression (B_Node, True);
13696 Set_High_Bound (R_Node, B_Node);
13698 -- Initialize various fields of the type. Some of this information
13699 -- may be overwritten later through rep.clauses.
13701 Set_Scalar_Range (T, R_Node);
13702 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
13703 Set_Enum_Esize (T);
13704 Set_Enum_Pos_To_Rep (T, Empty);
13706 -- Set Discard_Names if configuration pragma set, or if there is
13707 -- a parameterless pragma in the current declarative region
13709 if Global_Discard_Names
13710 or else Discard_Names (Scope (T))
13711 then
13712 Set_Discard_Names (T);
13713 end if;
13715 -- Process end label if there is one
13717 if Present (Def) then
13718 Process_End_Label (Def, 'e', T);
13719 end if;
13720 end Enumeration_Type_Declaration;
13722 ---------------------------------
13723 -- Expand_To_Stored_Constraint --
13724 ---------------------------------
13726 function Expand_To_Stored_Constraint
13727 (Typ : Entity_Id;
13728 Constraint : Elist_Id) return Elist_Id
13730 Explicitly_Discriminated_Type : Entity_Id;
13731 Expansion : Elist_Id;
13732 Discriminant : Entity_Id;
13734 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
13735 -- Find the nearest type that actually specifies discriminants
13737 ---------------------------------
13738 -- Type_With_Explicit_Discrims --
13739 ---------------------------------
13741 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
13742 Typ : constant E := Base_Type (Id);
13744 begin
13745 if Ekind (Typ) in Incomplete_Or_Private_Kind then
13746 if Present (Full_View (Typ)) then
13747 return Type_With_Explicit_Discrims (Full_View (Typ));
13748 end if;
13750 else
13751 if Has_Discriminants (Typ) then
13752 return Typ;
13753 end if;
13754 end if;
13756 if Etype (Typ) = Typ then
13757 return Empty;
13758 elsif Has_Discriminants (Typ) then
13759 return Typ;
13760 else
13761 return Type_With_Explicit_Discrims (Etype (Typ));
13762 end if;
13764 end Type_With_Explicit_Discrims;
13766 -- Start of processing for Expand_To_Stored_Constraint
13768 begin
13769 if No (Constraint)
13770 or else Is_Empty_Elmt_List (Constraint)
13771 then
13772 return No_Elist;
13773 end if;
13775 Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
13777 if No (Explicitly_Discriminated_Type) then
13778 return No_Elist;
13779 end if;
13781 Expansion := New_Elmt_List;
13783 Discriminant :=
13784 First_Stored_Discriminant (Explicitly_Discriminated_Type);
13785 while Present (Discriminant) loop
13786 Append_Elmt (
13787 Get_Discriminant_Value (
13788 Discriminant, Explicitly_Discriminated_Type, Constraint),
13789 Expansion);
13790 Next_Stored_Discriminant (Discriminant);
13791 end loop;
13793 return Expansion;
13794 end Expand_To_Stored_Constraint;
13796 ---------------------------
13797 -- Find_Hidden_Interface --
13798 ---------------------------
13800 function Find_Hidden_Interface
13801 (Src : Elist_Id;
13802 Dest : Elist_Id) return Entity_Id
13804 Iface : Entity_Id;
13805 Iface_Elmt : Elmt_Id;
13807 begin
13808 if Present (Src) and then Present (Dest) then
13809 Iface_Elmt := First_Elmt (Src);
13810 while Present (Iface_Elmt) loop
13811 Iface := Node (Iface_Elmt);
13813 if Is_Interface (Iface)
13814 and then not Contain_Interface (Iface, Dest)
13815 then
13816 return Iface;
13817 end if;
13819 Next_Elmt (Iface_Elmt);
13820 end loop;
13821 end if;
13823 return Empty;
13824 end Find_Hidden_Interface;
13826 --------------------
13827 -- Find_Type_Name --
13828 --------------------
13830 function Find_Type_Name (N : Node_Id) return Entity_Id is
13831 Id : constant Entity_Id := Defining_Identifier (N);
13832 Prev : Entity_Id;
13833 New_Id : Entity_Id;
13834 Prev_Par : Node_Id;
13836 procedure Tag_Mismatch;
13837 -- Diagnose a tagged partial view whose full view is untagged.
13838 -- We post the message on the full view, with a reference to
13839 -- the previous partial view. The partial view can be private
13840 -- or incomplete, and these are handled in a different manner,
13841 -- so we determine the position of the error message from the
13842 -- respective slocs of both.
13844 ------------------
13845 -- Tag_Mismatch --
13846 ------------------
13848 procedure Tag_Mismatch is
13849 begin
13850 if Sloc (Prev) < Sloc (Id) then
13851 Error_Msg_NE
13852 ("full declaration of } must be a tagged type ", Id, Prev);
13853 else
13854 Error_Msg_NE
13855 ("full declaration of } must be a tagged type ", Prev, Id);
13856 end if;
13857 end Tag_Mismatch;
13859 -- Start of processing for Find_Type_Name
13861 begin
13862 -- Find incomplete declaration, if one was given
13864 Prev := Current_Entity_In_Scope (Id);
13866 if Present (Prev) then
13868 -- Previous declaration exists. Error if not incomplete/private case
13869 -- except if previous declaration is implicit, etc. Enter_Name will
13870 -- emit error if appropriate.
13872 Prev_Par := Parent (Prev);
13874 if not Is_Incomplete_Or_Private_Type (Prev) then
13875 Enter_Name (Id);
13876 New_Id := Id;
13878 elsif not Nkind_In (N, N_Full_Type_Declaration,
13879 N_Task_Type_Declaration,
13880 N_Protected_Type_Declaration)
13881 then
13882 -- Completion must be a full type declarations (RM 7.3(4))
13884 Error_Msg_Sloc := Sloc (Prev);
13885 Error_Msg_NE ("invalid completion of }", Id, Prev);
13887 -- Set scope of Id to avoid cascaded errors. Entity is never
13888 -- examined again, except when saving globals in generics.
13890 Set_Scope (Id, Current_Scope);
13891 New_Id := Id;
13893 -- If this is a repeated incomplete declaration, no further
13894 -- checks are possible.
13896 if Nkind (N) = N_Incomplete_Type_Declaration then
13897 return Prev;
13898 end if;
13900 -- Case of full declaration of incomplete type
13902 elsif Ekind (Prev) = E_Incomplete_Type then
13904 -- Indicate that the incomplete declaration has a matching full
13905 -- declaration. The defining occurrence of the incomplete
13906 -- declaration remains the visible one, and the procedure
13907 -- Get_Full_View dereferences it whenever the type is used.
13909 if Present (Full_View (Prev)) then
13910 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
13911 end if;
13913 Set_Full_View (Prev, Id);
13914 Append_Entity (Id, Current_Scope);
13915 Set_Is_Public (Id, Is_Public (Prev));
13916 Set_Is_Internal (Id);
13917 New_Id := Prev;
13919 -- Case of full declaration of private type
13921 else
13922 if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
13923 if Etype (Prev) /= Prev then
13925 -- Prev is a private subtype or a derived type, and needs
13926 -- no completion.
13928 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
13929 New_Id := Id;
13931 elsif Ekind (Prev) = E_Private_Type
13932 and then Nkind_In (N, N_Task_Type_Declaration,
13933 N_Protected_Type_Declaration)
13934 then
13935 Error_Msg_N
13936 ("completion of nonlimited type cannot be limited", N);
13938 elsif Ekind (Prev) = E_Record_Type_With_Private
13939 and then Nkind_In (N, N_Task_Type_Declaration,
13940 N_Protected_Type_Declaration)
13941 then
13942 if not Is_Limited_Record (Prev) then
13943 Error_Msg_N
13944 ("completion of nonlimited type cannot be limited", N);
13946 elsif No (Interface_List (N)) then
13947 Error_Msg_N
13948 ("completion of tagged private type must be tagged",
13950 end if;
13952 elsif Nkind (N) = N_Full_Type_Declaration
13953 and then
13954 Nkind (Type_Definition (N)) = N_Record_Definition
13955 and then Interface_Present (Type_Definition (N))
13956 then
13957 Error_Msg_N
13958 ("completion of private type cannot be an interface", N);
13959 end if;
13961 -- Ada 2005 (AI-251): Private extension declaration of a task
13962 -- type or a protected type. This case arises when covering
13963 -- interface types.
13965 elsif Nkind_In (N, N_Task_Type_Declaration,
13966 N_Protected_Type_Declaration)
13967 then
13968 null;
13970 elsif Nkind (N) /= N_Full_Type_Declaration
13971 or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
13972 then
13973 Error_Msg_N
13974 ("full view of private extension must be an extension", N);
13976 elsif not (Abstract_Present (Parent (Prev)))
13977 and then Abstract_Present (Type_Definition (N))
13978 then
13979 Error_Msg_N
13980 ("full view of non-abstract extension cannot be abstract", N);
13981 end if;
13983 if not In_Private_Part (Current_Scope) then
13984 Error_Msg_N
13985 ("declaration of full view must appear in private part", N);
13986 end if;
13988 Copy_And_Swap (Prev, Id);
13989 Set_Has_Private_Declaration (Prev);
13990 Set_Has_Private_Declaration (Id);
13992 -- If no error, propagate freeze_node from private to full view.
13993 -- It may have been generated for an early operational item.
13995 if Present (Freeze_Node (Id))
13996 and then Serious_Errors_Detected = 0
13997 and then No (Full_View (Id))
13998 then
13999 Set_Freeze_Node (Prev, Freeze_Node (Id));
14000 Set_Freeze_Node (Id, Empty);
14001 Set_First_Rep_Item (Prev, First_Rep_Item (Id));
14002 end if;
14004 Set_Full_View (Id, Prev);
14005 New_Id := Prev;
14006 end if;
14008 -- Verify that full declaration conforms to partial one
14010 if Is_Incomplete_Or_Private_Type (Prev)
14011 and then Present (Discriminant_Specifications (Prev_Par))
14012 then
14013 if Present (Discriminant_Specifications (N)) then
14014 if Ekind (Prev) = E_Incomplete_Type then
14015 Check_Discriminant_Conformance (N, Prev, Prev);
14016 else
14017 Check_Discriminant_Conformance (N, Prev, Id);
14018 end if;
14020 else
14021 Error_Msg_N
14022 ("missing discriminants in full type declaration", N);
14024 -- To avoid cascaded errors on subsequent use, share the
14025 -- discriminants of the partial view.
14027 Set_Discriminant_Specifications (N,
14028 Discriminant_Specifications (Prev_Par));
14029 end if;
14030 end if;
14032 -- A prior untagged partial view can have an associated class-wide
14033 -- type due to use of the class attribute, and in this case the full
14034 -- type must also be tagged. This Ada 95 usage is deprecated in favor
14035 -- of incomplete tagged declarations, but we check for it.
14037 if Is_Type (Prev)
14038 and then (Is_Tagged_Type (Prev)
14039 or else Present (Class_Wide_Type (Prev)))
14040 then
14041 -- The full declaration is either a tagged type (including
14042 -- a synchronized type that implements interfaces) or a
14043 -- type extension, otherwise this is an error.
14045 if Nkind_In (N, N_Task_Type_Declaration,
14046 N_Protected_Type_Declaration)
14047 then
14048 if No (Interface_List (N))
14049 and then not Error_Posted (N)
14050 then
14051 Tag_Mismatch;
14052 end if;
14054 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
14056 -- Indicate that the previous declaration (tagged incomplete
14057 -- or private declaration) requires the same on the full one.
14059 if not Tagged_Present (Type_Definition (N)) then
14060 Tag_Mismatch;
14061 Set_Is_Tagged_Type (Id);
14062 Set_Primitive_Operations (Id, New_Elmt_List);
14063 end if;
14065 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
14066 if No (Record_Extension_Part (Type_Definition (N))) then
14067 Error_Msg_NE
14068 ("full declaration of } must be a record extension",
14069 Prev, Id);
14071 -- Set some attributes to produce a usable full view
14073 Set_Is_Tagged_Type (Id);
14074 Set_Primitive_Operations (Id, New_Elmt_List);
14075 end if;
14077 else
14078 Tag_Mismatch;
14079 end if;
14080 end if;
14082 return New_Id;
14084 else
14085 -- New type declaration
14087 Enter_Name (Id);
14088 return Id;
14089 end if;
14090 end Find_Type_Name;
14092 -------------------------
14093 -- Find_Type_Of_Object --
14094 -------------------------
14096 function Find_Type_Of_Object
14097 (Obj_Def : Node_Id;
14098 Related_Nod : Node_Id) return Entity_Id
14100 Def_Kind : constant Node_Kind := Nkind (Obj_Def);
14101 P : Node_Id := Parent (Obj_Def);
14102 T : Entity_Id;
14103 Nam : Name_Id;
14105 begin
14106 -- If the parent is a component_definition node we climb to the
14107 -- component_declaration node
14109 if Nkind (P) = N_Component_Definition then
14110 P := Parent (P);
14111 end if;
14113 -- Case of an anonymous array subtype
14115 if Nkind_In (Def_Kind, N_Constrained_Array_Definition,
14116 N_Unconstrained_Array_Definition)
14117 then
14118 T := Empty;
14119 Array_Type_Declaration (T, Obj_Def);
14121 -- Create an explicit subtype whenever possible
14123 elsif Nkind (P) /= N_Component_Declaration
14124 and then Def_Kind = N_Subtype_Indication
14125 then
14126 -- Base name of subtype on object name, which will be unique in
14127 -- the current scope.
14129 -- If this is a duplicate declaration, return base type, to avoid
14130 -- generating duplicate anonymous types.
14132 if Error_Posted (P) then
14133 Analyze (Subtype_Mark (Obj_Def));
14134 return Entity (Subtype_Mark (Obj_Def));
14135 end if;
14137 Nam :=
14138 New_External_Name
14139 (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
14141 T := Make_Defining_Identifier (Sloc (P), Nam);
14143 Insert_Action (Obj_Def,
14144 Make_Subtype_Declaration (Sloc (P),
14145 Defining_Identifier => T,
14146 Subtype_Indication => Relocate_Node (Obj_Def)));
14148 -- This subtype may need freezing, and this will not be done
14149 -- automatically if the object declaration is not in declarative
14150 -- part. Since this is an object declaration, the type cannot always
14151 -- be frozen here. Deferred constants do not freeze their type
14152 -- (which often enough will be private).
14154 if Nkind (P) = N_Object_Declaration
14155 and then Constant_Present (P)
14156 and then No (Expression (P))
14157 then
14158 null;
14159 else
14160 Insert_Actions (Obj_Def, Freeze_Entity (T, Sloc (P)));
14161 end if;
14163 -- Ada 2005 AI-406: the object definition in an object declaration
14164 -- can be an access definition.
14166 elsif Def_Kind = N_Access_Definition then
14167 T := Access_Definition (Related_Nod, Obj_Def);
14168 Set_Is_Local_Anonymous_Access (T);
14170 -- Otherwise, the object definition is just a subtype_mark
14172 else
14173 T := Process_Subtype (Obj_Def, Related_Nod);
14174 end if;
14176 return T;
14177 end Find_Type_Of_Object;
14179 --------------------------------
14180 -- Find_Type_Of_Subtype_Indic --
14181 --------------------------------
14183 function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
14184 Typ : Entity_Id;
14186 begin
14187 -- Case of subtype mark with a constraint
14189 if Nkind (S) = N_Subtype_Indication then
14190 Find_Type (Subtype_Mark (S));
14191 Typ := Entity (Subtype_Mark (S));
14193 if not
14194 Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
14195 then
14196 Error_Msg_N
14197 ("incorrect constraint for this kind of type", Constraint (S));
14198 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
14199 end if;
14201 -- Otherwise we have a subtype mark without a constraint
14203 elsif Error_Posted (S) then
14204 Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
14205 return Any_Type;
14207 else
14208 Find_Type (S);
14209 Typ := Entity (S);
14210 end if;
14212 -- Check No_Wide_Characters restriction
14214 if Typ = Standard_Wide_Character
14215 or else Typ = Standard_Wide_Wide_Character
14216 or else Typ = Standard_Wide_String
14217 or else Typ = Standard_Wide_Wide_String
14218 then
14219 Check_Restriction (No_Wide_Characters, S);
14220 end if;
14222 return Typ;
14223 end Find_Type_Of_Subtype_Indic;
14225 -------------------------------------
14226 -- Floating_Point_Type_Declaration --
14227 -------------------------------------
14229 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
14230 Digs : constant Node_Id := Digits_Expression (Def);
14231 Digs_Val : Uint;
14232 Base_Typ : Entity_Id;
14233 Implicit_Base : Entity_Id;
14234 Bound : Node_Id;
14236 function Can_Derive_From (E : Entity_Id) return Boolean;
14237 -- Find if given digits value allows derivation from specified type
14239 ---------------------
14240 -- Can_Derive_From --
14241 ---------------------
14243 function Can_Derive_From (E : Entity_Id) return Boolean is
14244 Spec : constant Entity_Id := Real_Range_Specification (Def);
14246 begin
14247 if Digs_Val > Digits_Value (E) then
14248 return False;
14249 end if;
14251 if Present (Spec) then
14252 if Expr_Value_R (Type_Low_Bound (E)) >
14253 Expr_Value_R (Low_Bound (Spec))
14254 then
14255 return False;
14256 end if;
14258 if Expr_Value_R (Type_High_Bound (E)) <
14259 Expr_Value_R (High_Bound (Spec))
14260 then
14261 return False;
14262 end if;
14263 end if;
14265 return True;
14266 end Can_Derive_From;
14268 -- Start of processing for Floating_Point_Type_Declaration
14270 begin
14271 Check_Restriction (No_Floating_Point, Def);
14273 -- Create an implicit base type
14275 Implicit_Base :=
14276 Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
14278 -- Analyze and verify digits value
14280 Analyze_And_Resolve (Digs, Any_Integer);
14281 Check_Digits_Expression (Digs);
14282 Digs_Val := Expr_Value (Digs);
14284 -- Process possible range spec and find correct type to derive from
14286 Process_Real_Range_Specification (Def);
14288 if Can_Derive_From (Standard_Short_Float) then
14289 Base_Typ := Standard_Short_Float;
14290 elsif Can_Derive_From (Standard_Float) then
14291 Base_Typ := Standard_Float;
14292 elsif Can_Derive_From (Standard_Long_Float) then
14293 Base_Typ := Standard_Long_Float;
14294 elsif Can_Derive_From (Standard_Long_Long_Float) then
14295 Base_Typ := Standard_Long_Long_Float;
14297 -- If we can't derive from any existing type, use long_long_float
14298 -- and give appropriate message explaining the problem.
14300 else
14301 Base_Typ := Standard_Long_Long_Float;
14303 if Digs_Val >= Digits_Value (Standard_Long_Long_Float) then
14304 Error_Msg_Uint_1 := Digits_Value (Standard_Long_Long_Float);
14305 Error_Msg_N ("digits value out of range, maximum is ^", Digs);
14307 else
14308 Error_Msg_N
14309 ("range too large for any predefined type",
14310 Real_Range_Specification (Def));
14311 end if;
14312 end if;
14314 -- If there are bounds given in the declaration use them as the bounds
14315 -- of the type, otherwise use the bounds of the predefined base type
14316 -- that was chosen based on the Digits value.
14318 if Present (Real_Range_Specification (Def)) then
14319 Set_Scalar_Range (T, Real_Range_Specification (Def));
14320 Set_Is_Constrained (T);
14322 -- The bounds of this range must be converted to machine numbers
14323 -- in accordance with RM 4.9(38).
14325 Bound := Type_Low_Bound (T);
14327 if Nkind (Bound) = N_Real_Literal then
14328 Set_Realval
14329 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
14330 Set_Is_Machine_Number (Bound);
14331 end if;
14333 Bound := Type_High_Bound (T);
14335 if Nkind (Bound) = N_Real_Literal then
14336 Set_Realval
14337 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
14338 Set_Is_Machine_Number (Bound);
14339 end if;
14341 else
14342 Set_Scalar_Range (T, Scalar_Range (Base_Typ));
14343 end if;
14345 -- Complete definition of implicit base and declared first subtype
14347 Set_Etype (Implicit_Base, Base_Typ);
14349 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
14350 Set_Size_Info (Implicit_Base, (Base_Typ));
14351 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
14352 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
14353 Set_Digits_Value (Implicit_Base, Digits_Value (Base_Typ));
14354 Set_Vax_Float (Implicit_Base, Vax_Float (Base_Typ));
14356 Set_Ekind (T, E_Floating_Point_Subtype);
14357 Set_Etype (T, Implicit_Base);
14359 Set_Size_Info (T, (Implicit_Base));
14360 Set_RM_Size (T, RM_Size (Implicit_Base));
14361 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
14362 Set_Digits_Value (T, Digs_Val);
14363 end Floating_Point_Type_Declaration;
14365 ----------------------------
14366 -- Get_Discriminant_Value --
14367 ----------------------------
14369 -- This is the situation:
14371 -- There is a non-derived type
14373 -- type T0 (Dx, Dy, Dz...)
14375 -- There are zero or more levels of derivation, with each derivation
14376 -- either purely inheriting the discriminants, or defining its own.
14378 -- type Ti is new Ti-1
14379 -- or
14380 -- type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
14381 -- or
14382 -- subtype Ti is ...
14384 -- The subtype issue is avoided by the use of Original_Record_Component,
14385 -- and the fact that derived subtypes also derive the constraints.
14387 -- This chain leads back from
14389 -- Typ_For_Constraint
14391 -- Typ_For_Constraint has discriminants, and the value for each
14392 -- discriminant is given by its corresponding Elmt of Constraints.
14394 -- Discriminant is some discriminant in this hierarchy
14396 -- We need to return its value
14398 -- We do this by recursively searching each level, and looking for
14399 -- Discriminant. Once we get to the bottom, we start backing up
14400 -- returning the value for it which may in turn be a discriminant
14401 -- further up, so on the backup we continue the substitution.
14403 function Get_Discriminant_Value
14404 (Discriminant : Entity_Id;
14405 Typ_For_Constraint : Entity_Id;
14406 Constraint : Elist_Id) return Node_Id
14408 function Search_Derivation_Levels
14409 (Ti : Entity_Id;
14410 Discrim_Values : Elist_Id;
14411 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
14412 -- This is the routine that performs the recursive search of levels
14413 -- as described above.
14415 ------------------------------
14416 -- Search_Derivation_Levels --
14417 ------------------------------
14419 function Search_Derivation_Levels
14420 (Ti : Entity_Id;
14421 Discrim_Values : Elist_Id;
14422 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
14424 Assoc : Elmt_Id;
14425 Disc : Entity_Id;
14426 Result : Node_Or_Entity_Id;
14427 Result_Entity : Node_Id;
14429 begin
14430 -- If inappropriate type, return Error, this happens only in
14431 -- cascaded error situations, and we want to avoid a blow up.
14433 if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
14434 return Error;
14435 end if;
14437 -- Look deeper if possible. Use Stored_Constraints only for
14438 -- untagged types. For tagged types use the given constraint.
14439 -- This asymmetry needs explanation???
14441 if not Stored_Discrim_Values
14442 and then Present (Stored_Constraint (Ti))
14443 and then not Is_Tagged_Type (Ti)
14444 then
14445 Result :=
14446 Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
14447 else
14448 declare
14449 Td : constant Entity_Id := Etype (Ti);
14451 begin
14452 if Td = Ti then
14453 Result := Discriminant;
14455 else
14456 if Present (Stored_Constraint (Ti)) then
14457 Result :=
14458 Search_Derivation_Levels
14459 (Td, Stored_Constraint (Ti), True);
14460 else
14461 Result :=
14462 Search_Derivation_Levels
14463 (Td, Discrim_Values, Stored_Discrim_Values);
14464 end if;
14465 end if;
14466 end;
14467 end if;
14469 -- Extra underlying places to search, if not found above. For
14470 -- concurrent types, the relevant discriminant appears in the
14471 -- corresponding record. For a type derived from a private type
14472 -- without discriminant, the full view inherits the discriminants
14473 -- of the full view of the parent.
14475 if Result = Discriminant then
14476 if Is_Concurrent_Type (Ti)
14477 and then Present (Corresponding_Record_Type (Ti))
14478 then
14479 Result :=
14480 Search_Derivation_Levels (
14481 Corresponding_Record_Type (Ti),
14482 Discrim_Values,
14483 Stored_Discrim_Values);
14485 elsif Is_Private_Type (Ti)
14486 and then not Has_Discriminants (Ti)
14487 and then Present (Full_View (Ti))
14488 and then Etype (Full_View (Ti)) /= Ti
14489 then
14490 Result :=
14491 Search_Derivation_Levels (
14492 Full_View (Ti),
14493 Discrim_Values,
14494 Stored_Discrim_Values);
14495 end if;
14496 end if;
14498 -- If Result is not a (reference to a) discriminant, return it,
14499 -- otherwise set Result_Entity to the discriminant.
14501 if Nkind (Result) = N_Defining_Identifier then
14502 pragma Assert (Result = Discriminant);
14503 Result_Entity := Result;
14505 else
14506 if not Denotes_Discriminant (Result) then
14507 return Result;
14508 end if;
14510 Result_Entity := Entity (Result);
14511 end if;
14513 -- See if this level of derivation actually has discriminants
14514 -- because tagged derivations can add them, hence the lower
14515 -- levels need not have any.
14517 if not Has_Discriminants (Ti) then
14518 return Result;
14519 end if;
14521 -- Scan Ti's discriminants for Result_Entity,
14522 -- and return its corresponding value, if any.
14524 Result_Entity := Original_Record_Component (Result_Entity);
14526 Assoc := First_Elmt (Discrim_Values);
14528 if Stored_Discrim_Values then
14529 Disc := First_Stored_Discriminant (Ti);
14530 else
14531 Disc := First_Discriminant (Ti);
14532 end if;
14534 while Present (Disc) loop
14535 pragma Assert (Present (Assoc));
14537 if Original_Record_Component (Disc) = Result_Entity then
14538 return Node (Assoc);
14539 end if;
14541 Next_Elmt (Assoc);
14543 if Stored_Discrim_Values then
14544 Next_Stored_Discriminant (Disc);
14545 else
14546 Next_Discriminant (Disc);
14547 end if;
14548 end loop;
14550 -- Could not find it
14552 return Result;
14553 end Search_Derivation_Levels;
14555 -- Local Variables
14557 Result : Node_Or_Entity_Id;
14559 -- Start of processing for Get_Discriminant_Value
14561 begin
14562 -- ??? This routine is a gigantic mess and will be deleted. For the
14563 -- time being just test for the trivial case before calling recurse.
14565 if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
14566 declare
14567 D : Entity_Id;
14568 E : Elmt_Id;
14570 begin
14571 D := First_Discriminant (Typ_For_Constraint);
14572 E := First_Elmt (Constraint);
14573 while Present (D) loop
14574 if Chars (D) = Chars (Discriminant) then
14575 return Node (E);
14576 end if;
14578 Next_Discriminant (D);
14579 Next_Elmt (E);
14580 end loop;
14581 end;
14582 end if;
14584 Result := Search_Derivation_Levels
14585 (Typ_For_Constraint, Constraint, False);
14587 -- ??? hack to disappear when this routine is gone
14589 if Nkind (Result) = N_Defining_Identifier then
14590 declare
14591 D : Entity_Id;
14592 E : Elmt_Id;
14594 begin
14595 D := First_Discriminant (Typ_For_Constraint);
14596 E := First_Elmt (Constraint);
14597 while Present (D) loop
14598 if Corresponding_Discriminant (D) = Discriminant then
14599 return Node (E);
14600 end if;
14602 Next_Discriminant (D);
14603 Next_Elmt (E);
14604 end loop;
14605 end;
14606 end if;
14608 pragma Assert (Nkind (Result) /= N_Defining_Identifier);
14609 return Result;
14610 end Get_Discriminant_Value;
14612 --------------------------
14613 -- Has_Range_Constraint --
14614 --------------------------
14616 function Has_Range_Constraint (N : Node_Id) return Boolean is
14617 C : constant Node_Id := Constraint (N);
14619 begin
14620 if Nkind (C) = N_Range_Constraint then
14621 return True;
14623 elsif Nkind (C) = N_Digits_Constraint then
14624 return
14625 Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
14626 or else
14627 Present (Range_Constraint (C));
14629 elsif Nkind (C) = N_Delta_Constraint then
14630 return Present (Range_Constraint (C));
14632 else
14633 return False;
14634 end if;
14635 end Has_Range_Constraint;
14637 ------------------------
14638 -- Inherit_Components --
14639 ------------------------
14641 function Inherit_Components
14642 (N : Node_Id;
14643 Parent_Base : Entity_Id;
14644 Derived_Base : Entity_Id;
14645 Is_Tagged : Boolean;
14646 Inherit_Discr : Boolean;
14647 Discs : Elist_Id) return Elist_Id
14649 Assoc_List : constant Elist_Id := New_Elmt_List;
14651 procedure Inherit_Component
14652 (Old_C : Entity_Id;
14653 Plain_Discrim : Boolean := False;
14654 Stored_Discrim : Boolean := False);
14655 -- Inherits component Old_C from Parent_Base to the Derived_Base. If
14656 -- Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
14657 -- True, Old_C is a stored discriminant. If they are both false then
14658 -- Old_C is a regular component.
14660 -----------------------
14661 -- Inherit_Component --
14662 -----------------------
14664 procedure Inherit_Component
14665 (Old_C : Entity_Id;
14666 Plain_Discrim : Boolean := False;
14667 Stored_Discrim : Boolean := False)
14669 New_C : constant Entity_Id := New_Copy (Old_C);
14671 Discrim : Entity_Id;
14672 Corr_Discrim : Entity_Id;
14674 begin
14675 pragma Assert (not Is_Tagged or else not Stored_Discrim);
14677 Set_Parent (New_C, Parent (Old_C));
14679 -- Regular discriminants and components must be inserted in the scope
14680 -- of the Derived_Base. Do it here.
14682 if not Stored_Discrim then
14683 Enter_Name (New_C);
14684 end if;
14686 -- For tagged types the Original_Record_Component must point to
14687 -- whatever this field was pointing to in the parent type. This has
14688 -- already been achieved by the call to New_Copy above.
14690 if not Is_Tagged then
14691 Set_Original_Record_Component (New_C, New_C);
14692 end if;
14694 -- If we have inherited a component then see if its Etype contains
14695 -- references to Parent_Base discriminants. In this case, replace
14696 -- these references with the constraints given in Discs. We do not
14697 -- do this for the partial view of private types because this is
14698 -- not needed (only the components of the full view will be used
14699 -- for code generation) and cause problem. We also avoid this
14700 -- transformation in some error situations.
14702 if Ekind (New_C) = E_Component then
14703 if (Is_Private_Type (Derived_Base)
14704 and then not Is_Generic_Type (Derived_Base))
14705 or else (Is_Empty_Elmt_List (Discs)
14706 and then not Expander_Active)
14707 then
14708 Set_Etype (New_C, Etype (Old_C));
14710 else
14711 -- The current component introduces a circularity of the
14712 -- following kind:
14714 -- limited with Pack_2;
14715 -- package Pack_1 is
14716 -- type T_1 is tagged record
14717 -- Comp : access Pack_2.T_2;
14718 -- ...
14719 -- end record;
14720 -- end Pack_1;
14722 -- with Pack_1;
14723 -- package Pack_2 is
14724 -- type T_2 is new Pack_1.T_1 with ...;
14725 -- end Pack_2;
14727 Set_Etype
14728 (New_C,
14729 Constrain_Component_Type
14730 (Old_C, Derived_Base, N, Parent_Base, Discs));
14731 end if;
14732 end if;
14734 -- In derived tagged types it is illegal to reference a non
14735 -- discriminant component in the parent type. To catch this, mark
14736 -- these components with an Ekind of E_Void. This will be reset in
14737 -- Record_Type_Definition after processing the record extension of
14738 -- the derived type.
14740 -- If the declaration is a private extension, there is no further
14741 -- record extension to process, and the components retain their
14742 -- current kind, because they are visible at this point.
14744 if Is_Tagged and then Ekind (New_C) = E_Component
14745 and then Nkind (N) /= N_Private_Extension_Declaration
14746 then
14747 Set_Ekind (New_C, E_Void);
14748 end if;
14750 if Plain_Discrim then
14751 Set_Corresponding_Discriminant (New_C, Old_C);
14752 Build_Discriminal (New_C);
14754 -- If we are explicitly inheriting a stored discriminant it will be
14755 -- completely hidden.
14757 elsif Stored_Discrim then
14758 Set_Corresponding_Discriminant (New_C, Empty);
14759 Set_Discriminal (New_C, Empty);
14760 Set_Is_Completely_Hidden (New_C);
14762 -- Set the Original_Record_Component of each discriminant in the
14763 -- derived base to point to the corresponding stored that we just
14764 -- created.
14766 Discrim := First_Discriminant (Derived_Base);
14767 while Present (Discrim) loop
14768 Corr_Discrim := Corresponding_Discriminant (Discrim);
14770 -- Corr_Discrim could be missing in an error situation
14772 if Present (Corr_Discrim)
14773 and then Original_Record_Component (Corr_Discrim) = Old_C
14774 then
14775 Set_Original_Record_Component (Discrim, New_C);
14776 end if;
14778 Next_Discriminant (Discrim);
14779 end loop;
14781 Append_Entity (New_C, Derived_Base);
14782 end if;
14784 if not Is_Tagged then
14785 Append_Elmt (Old_C, Assoc_List);
14786 Append_Elmt (New_C, Assoc_List);
14787 end if;
14788 end Inherit_Component;
14790 -- Variables local to Inherit_Component
14792 Loc : constant Source_Ptr := Sloc (N);
14794 Parent_Discrim : Entity_Id;
14795 Stored_Discrim : Entity_Id;
14796 D : Entity_Id;
14797 Component : Entity_Id;
14799 -- Start of processing for Inherit_Components
14801 begin
14802 if not Is_Tagged then
14803 Append_Elmt (Parent_Base, Assoc_List);
14804 Append_Elmt (Derived_Base, Assoc_List);
14805 end if;
14807 -- Inherit parent discriminants if needed
14809 if Inherit_Discr then
14810 Parent_Discrim := First_Discriminant (Parent_Base);
14811 while Present (Parent_Discrim) loop
14812 Inherit_Component (Parent_Discrim, Plain_Discrim => True);
14813 Next_Discriminant (Parent_Discrim);
14814 end loop;
14815 end if;
14817 -- Create explicit stored discrims for untagged types when necessary
14819 if not Has_Unknown_Discriminants (Derived_Base)
14820 and then Has_Discriminants (Parent_Base)
14821 and then not Is_Tagged
14822 and then
14823 (not Inherit_Discr
14824 or else First_Discriminant (Parent_Base) /=
14825 First_Stored_Discriminant (Parent_Base))
14826 then
14827 Stored_Discrim := First_Stored_Discriminant (Parent_Base);
14828 while Present (Stored_Discrim) loop
14829 Inherit_Component (Stored_Discrim, Stored_Discrim => True);
14830 Next_Stored_Discriminant (Stored_Discrim);
14831 end loop;
14832 end if;
14834 -- See if we can apply the second transformation for derived types, as
14835 -- explained in point 6. in the comments above Build_Derived_Record_Type
14836 -- This is achieved by appending Derived_Base discriminants into Discs,
14837 -- which has the side effect of returning a non empty Discs list to the
14838 -- caller of Inherit_Components, which is what we want. This must be
14839 -- done for private derived types if there are explicit stored
14840 -- discriminants, to ensure that we can retrieve the values of the
14841 -- constraints provided in the ancestors.
14843 if Inherit_Discr
14844 and then Is_Empty_Elmt_List (Discs)
14845 and then Present (First_Discriminant (Derived_Base))
14846 and then
14847 (not Is_Private_Type (Derived_Base)
14848 or else Is_Completely_Hidden
14849 (First_Stored_Discriminant (Derived_Base))
14850 or else Is_Generic_Type (Derived_Base))
14851 then
14852 D := First_Discriminant (Derived_Base);
14853 while Present (D) loop
14854 Append_Elmt (New_Reference_To (D, Loc), Discs);
14855 Next_Discriminant (D);
14856 end loop;
14857 end if;
14859 -- Finally, inherit non-discriminant components unless they are not
14860 -- visible because defined or inherited from the full view of the
14861 -- parent. Don't inherit the _parent field of the parent type.
14863 Component := First_Entity (Parent_Base);
14864 while Present (Component) loop
14866 -- Ada 2005 (AI-251): Do not inherit components associated with
14867 -- secondary tags of the parent.
14869 if Ekind (Component) = E_Component
14870 and then Present (Related_Type (Component))
14871 then
14872 null;
14874 elsif Ekind (Component) /= E_Component
14875 or else Chars (Component) = Name_uParent
14876 then
14877 null;
14879 -- If the derived type is within the parent type's declarative
14880 -- region, then the components can still be inherited even though
14881 -- they aren't visible at this point. This can occur for cases
14882 -- such as within public child units where the components must
14883 -- become visible upon entering the child unit's private part.
14885 elsif not Is_Visible_Component (Component)
14886 and then not In_Open_Scopes (Scope (Parent_Base))
14887 then
14888 null;
14890 elsif Ekind_In (Derived_Base, E_Private_Type,
14891 E_Limited_Private_Type)
14892 then
14893 null;
14895 else
14896 Inherit_Component (Component);
14897 end if;
14899 Next_Entity (Component);
14900 end loop;
14902 -- For tagged derived types, inherited discriminants cannot be used in
14903 -- component declarations of the record extension part. To achieve this
14904 -- we mark the inherited discriminants as not visible.
14906 if Is_Tagged and then Inherit_Discr then
14907 D := First_Discriminant (Derived_Base);
14908 while Present (D) loop
14909 Set_Is_Immediately_Visible (D, False);
14910 Next_Discriminant (D);
14911 end loop;
14912 end if;
14914 return Assoc_List;
14915 end Inherit_Components;
14917 -----------------------
14918 -- Is_Null_Extension --
14919 -----------------------
14921 function Is_Null_Extension (T : Entity_Id) return Boolean is
14922 Type_Decl : constant Node_Id := Parent (Base_Type (T));
14923 Comp_List : Node_Id;
14924 Comp : Node_Id;
14926 begin
14927 if Nkind (Type_Decl) /= N_Full_Type_Declaration
14928 or else not Is_Tagged_Type (T)
14929 or else Nkind (Type_Definition (Type_Decl)) /=
14930 N_Derived_Type_Definition
14931 or else No (Record_Extension_Part (Type_Definition (Type_Decl)))
14932 then
14933 return False;
14934 end if;
14936 Comp_List :=
14937 Component_List (Record_Extension_Part (Type_Definition (Type_Decl)));
14939 if Present (Discriminant_Specifications (Type_Decl)) then
14940 return False;
14942 elsif Present (Comp_List)
14943 and then Is_Non_Empty_List (Component_Items (Comp_List))
14944 then
14945 Comp := First (Component_Items (Comp_List));
14947 -- Only user-defined components are relevant. The component list
14948 -- may also contain a parent component and internal components
14949 -- corresponding to secondary tags, but these do not determine
14950 -- whether this is a null extension.
14952 while Present (Comp) loop
14953 if Comes_From_Source (Comp) then
14954 return False;
14955 end if;
14957 Next (Comp);
14958 end loop;
14960 return True;
14961 else
14962 return True;
14963 end if;
14964 end Is_Null_Extension;
14966 --------------------
14967 -- Is_Progenitor --
14968 --------------------
14970 function Is_Progenitor
14971 (Iface : Entity_Id;
14972 Typ : Entity_Id) return Boolean
14974 begin
14975 return Implements_Interface (Typ, Iface,
14976 Exclude_Parents => True);
14977 end Is_Progenitor;
14979 ------------------------------
14980 -- Is_Valid_Constraint_Kind --
14981 ------------------------------
14983 function Is_Valid_Constraint_Kind
14984 (T_Kind : Type_Kind;
14985 Constraint_Kind : Node_Kind) return Boolean
14987 begin
14988 case T_Kind is
14989 when Enumeration_Kind |
14990 Integer_Kind =>
14991 return Constraint_Kind = N_Range_Constraint;
14993 when Decimal_Fixed_Point_Kind =>
14994 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
14995 N_Range_Constraint);
14997 when Ordinary_Fixed_Point_Kind =>
14998 return Nkind_In (Constraint_Kind, N_Delta_Constraint,
14999 N_Range_Constraint);
15001 when Float_Kind =>
15002 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
15003 N_Range_Constraint);
15005 when Access_Kind |
15006 Array_Kind |
15007 E_Record_Type |
15008 E_Record_Subtype |
15009 Class_Wide_Kind |
15010 E_Incomplete_Type |
15011 Private_Kind |
15012 Concurrent_Kind =>
15013 return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
15015 when others =>
15016 return True; -- Error will be detected later
15017 end case;
15018 end Is_Valid_Constraint_Kind;
15020 --------------------------
15021 -- Is_Visible_Component --
15022 --------------------------
15024 function Is_Visible_Component (C : Entity_Id) return Boolean is
15025 Original_Comp : Entity_Id := Empty;
15026 Original_Scope : Entity_Id;
15027 Type_Scope : Entity_Id;
15029 function Is_Local_Type (Typ : Entity_Id) return Boolean;
15030 -- Check whether parent type of inherited component is declared locally,
15031 -- possibly within a nested package or instance. The current scope is
15032 -- the derived record itself.
15034 -------------------
15035 -- Is_Local_Type --
15036 -------------------
15038 function Is_Local_Type (Typ : Entity_Id) return Boolean is
15039 Scop : Entity_Id;
15041 begin
15042 Scop := Scope (Typ);
15043 while Present (Scop)
15044 and then Scop /= Standard_Standard
15045 loop
15046 if Scop = Scope (Current_Scope) then
15047 return True;
15048 end if;
15050 Scop := Scope (Scop);
15051 end loop;
15053 return False;
15054 end Is_Local_Type;
15056 -- Start of processing for Is_Visible_Component
15058 begin
15059 if Ekind_In (C, E_Component, E_Discriminant) then
15060 Original_Comp := Original_Record_Component (C);
15061 end if;
15063 if No (Original_Comp) then
15065 -- Premature usage, or previous error
15067 return False;
15069 else
15070 Original_Scope := Scope (Original_Comp);
15071 Type_Scope := Scope (Base_Type (Scope (C)));
15072 end if;
15074 -- This test only concerns tagged types
15076 if not Is_Tagged_Type (Original_Scope) then
15077 return True;
15079 -- If it is _Parent or _Tag, there is no visibility issue
15081 elsif not Comes_From_Source (Original_Comp) then
15082 return True;
15084 -- If we are in the body of an instantiation, the component is visible
15085 -- even when the parent type (possibly defined in an enclosing unit or
15086 -- in a parent unit) might not.
15088 elsif In_Instance_Body then
15089 return True;
15091 -- Discriminants are always visible
15093 elsif Ekind (Original_Comp) = E_Discriminant
15094 and then not Has_Unknown_Discriminants (Original_Scope)
15095 then
15096 return True;
15098 -- If the component has been declared in an ancestor which is currently
15099 -- a private type, then it is not visible. The same applies if the
15100 -- component's containing type is not in an open scope and the original
15101 -- component's enclosing type is a visible full view of a private type
15102 -- (which can occur in cases where an attempt is being made to reference
15103 -- a component in a sibling package that is inherited from a visible
15104 -- component of a type in an ancestor package; the component in the
15105 -- sibling package should not be visible even though the component it
15106 -- inherited from is visible). This does not apply however in the case
15107 -- where the scope of the type is a private child unit, or when the
15108 -- parent comes from a local package in which the ancestor is currently
15109 -- visible. The latter suppression of visibility is needed for cases
15110 -- that are tested in B730006.
15112 elsif Is_Private_Type (Original_Scope)
15113 or else
15114 (not Is_Private_Descendant (Type_Scope)
15115 and then not In_Open_Scopes (Type_Scope)
15116 and then Has_Private_Declaration (Original_Scope))
15117 then
15118 -- If the type derives from an entity in a formal package, there
15119 -- are no additional visible components.
15121 if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
15122 N_Formal_Package_Declaration
15123 then
15124 return False;
15126 -- if we are not in the private part of the current package, there
15127 -- are no additional visible components.
15129 elsif Ekind (Scope (Current_Scope)) = E_Package
15130 and then not In_Private_Part (Scope (Current_Scope))
15131 then
15132 return False;
15133 else
15134 return
15135 Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
15136 and then In_Open_Scopes (Scope (Original_Scope))
15137 and then Is_Local_Type (Type_Scope);
15138 end if;
15140 -- There is another weird way in which a component may be invisible
15141 -- when the private and the full view are not derived from the same
15142 -- ancestor. Here is an example :
15144 -- type A1 is tagged record F1 : integer; end record;
15145 -- type A2 is new A1 with record F2 : integer; end record;
15146 -- type T is new A1 with private;
15147 -- private
15148 -- type T is new A2 with null record;
15150 -- In this case, the full view of T inherits F1 and F2 but the private
15151 -- view inherits only F1
15153 else
15154 declare
15155 Ancestor : Entity_Id := Scope (C);
15157 begin
15158 loop
15159 if Ancestor = Original_Scope then
15160 return True;
15161 elsif Ancestor = Etype (Ancestor) then
15162 return False;
15163 end if;
15165 Ancestor := Etype (Ancestor);
15166 end loop;
15167 end;
15168 end if;
15169 end Is_Visible_Component;
15171 --------------------------
15172 -- Make_Class_Wide_Type --
15173 --------------------------
15175 procedure Make_Class_Wide_Type (T : Entity_Id) is
15176 CW_Type : Entity_Id;
15177 CW_Name : Name_Id;
15178 Next_E : Entity_Id;
15180 begin
15181 -- The class wide type can have been defined by the partial view, in
15182 -- which case everything is already done.
15184 if Present (Class_Wide_Type (T)) then
15185 return;
15186 end if;
15188 CW_Type :=
15189 New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
15191 -- Inherit root type characteristics
15193 CW_Name := Chars (CW_Type);
15194 Next_E := Next_Entity (CW_Type);
15195 Copy_Node (T, CW_Type);
15196 Set_Comes_From_Source (CW_Type, False);
15197 Set_Chars (CW_Type, CW_Name);
15198 Set_Parent (CW_Type, Parent (T));
15199 Set_Next_Entity (CW_Type, Next_E);
15201 -- Ensure we have a new freeze node for the class-wide type. The partial
15202 -- view may have freeze action of its own, requiring a proper freeze
15203 -- node, and the same freeze node cannot be shared between the two
15204 -- types.
15206 Set_Has_Delayed_Freeze (CW_Type);
15207 Set_Freeze_Node (CW_Type, Empty);
15209 -- Customize the class-wide type: It has no prim. op., it cannot be
15210 -- abstract and its Etype points back to the specific root type.
15212 Set_Ekind (CW_Type, E_Class_Wide_Type);
15213 Set_Is_Tagged_Type (CW_Type, True);
15214 Set_Primitive_Operations (CW_Type, New_Elmt_List);
15215 Set_Is_Abstract_Type (CW_Type, False);
15216 Set_Is_Constrained (CW_Type, False);
15217 Set_Is_First_Subtype (CW_Type, Is_First_Subtype (T));
15219 if Ekind (T) = E_Class_Wide_Subtype then
15220 Set_Etype (CW_Type, Etype (Base_Type (T)));
15221 else
15222 Set_Etype (CW_Type, T);
15223 end if;
15225 -- If this is the class_wide type of a constrained subtype, it does
15226 -- not have discriminants.
15228 Set_Has_Discriminants (CW_Type,
15229 Has_Discriminants (T) and then not Is_Constrained (T));
15231 Set_Has_Unknown_Discriminants (CW_Type, True);
15232 Set_Class_Wide_Type (T, CW_Type);
15233 Set_Equivalent_Type (CW_Type, Empty);
15235 -- The class-wide type of a class-wide type is itself (RM 3.9(14))
15237 Set_Class_Wide_Type (CW_Type, CW_Type);
15238 end Make_Class_Wide_Type;
15240 ----------------
15241 -- Make_Index --
15242 ----------------
15244 procedure Make_Index
15245 (I : Node_Id;
15246 Related_Nod : Node_Id;
15247 Related_Id : Entity_Id := Empty;
15248 Suffix_Index : Nat := 1)
15250 R : Node_Id;
15251 T : Entity_Id;
15252 Def_Id : Entity_Id := Empty;
15253 Found : Boolean := False;
15255 begin
15256 -- For a discrete range used in a constrained array definition and
15257 -- defined by a range, an implicit conversion to the predefined type
15258 -- INTEGER is assumed if each bound is either a numeric literal, a named
15259 -- number, or an attribute, and the type of both bounds (prior to the
15260 -- implicit conversion) is the type universal_integer. Otherwise, both
15261 -- bounds must be of the same discrete type, other than universal
15262 -- integer; this type must be determinable independently of the
15263 -- context, but using the fact that the type must be discrete and that
15264 -- both bounds must have the same type.
15266 -- Character literals also have a universal type in the absence of
15267 -- of additional context, and are resolved to Standard_Character.
15269 if Nkind (I) = N_Range then
15271 -- The index is given by a range constraint. The bounds are known
15272 -- to be of a consistent type.
15274 if not Is_Overloaded (I) then
15275 T := Etype (I);
15277 -- For universal bounds, choose the specific predefined type
15279 if T = Universal_Integer then
15280 T := Standard_Integer;
15282 elsif T = Any_Character then
15283 Ambiguous_Character (Low_Bound (I));
15285 T := Standard_Character;
15286 end if;
15288 -- The node may be overloaded because some user-defined operators
15289 -- are available, but if a universal interpretation exists it is
15290 -- also the selected one.
15292 elsif Universal_Interpretation (I) = Universal_Integer then
15293 T := Standard_Integer;
15295 else
15296 T := Any_Type;
15298 declare
15299 Ind : Interp_Index;
15300 It : Interp;
15302 begin
15303 Get_First_Interp (I, Ind, It);
15304 while Present (It.Typ) loop
15305 if Is_Discrete_Type (It.Typ) then
15307 if Found
15308 and then not Covers (It.Typ, T)
15309 and then not Covers (T, It.Typ)
15310 then
15311 Error_Msg_N ("ambiguous bounds in discrete range", I);
15312 exit;
15313 else
15314 T := It.Typ;
15315 Found := True;
15316 end if;
15317 end if;
15319 Get_Next_Interp (Ind, It);
15320 end loop;
15322 if T = Any_Type then
15323 Error_Msg_N ("discrete type required for range", I);
15324 Set_Etype (I, Any_Type);
15325 return;
15327 elsif T = Universal_Integer then
15328 T := Standard_Integer;
15329 end if;
15330 end;
15331 end if;
15333 if not Is_Discrete_Type (T) then
15334 Error_Msg_N ("discrete type required for range", I);
15335 Set_Etype (I, Any_Type);
15336 return;
15337 end if;
15339 if Nkind (Low_Bound (I)) = N_Attribute_Reference
15340 and then Attribute_Name (Low_Bound (I)) = Name_First
15341 and then Is_Entity_Name (Prefix (Low_Bound (I)))
15342 and then Is_Type (Entity (Prefix (Low_Bound (I))))
15343 and then Is_Discrete_Type (Entity (Prefix (Low_Bound (I))))
15344 then
15345 -- The type of the index will be the type of the prefix, as long
15346 -- as the upper bound is 'Last of the same type.
15348 Def_Id := Entity (Prefix (Low_Bound (I)));
15350 if Nkind (High_Bound (I)) /= N_Attribute_Reference
15351 or else Attribute_Name (High_Bound (I)) /= Name_Last
15352 or else not Is_Entity_Name (Prefix (High_Bound (I)))
15353 or else Entity (Prefix (High_Bound (I))) /= Def_Id
15354 then
15355 Def_Id := Empty;
15356 end if;
15357 end if;
15359 R := I;
15360 Process_Range_Expr_In_Decl (R, T);
15362 elsif Nkind (I) = N_Subtype_Indication then
15364 -- The index is given by a subtype with a range constraint
15366 T := Base_Type (Entity (Subtype_Mark (I)));
15368 if not Is_Discrete_Type (T) then
15369 Error_Msg_N ("discrete type required for range", I);
15370 Set_Etype (I, Any_Type);
15371 return;
15372 end if;
15374 R := Range_Expression (Constraint (I));
15376 Resolve (R, T);
15377 Process_Range_Expr_In_Decl (R, Entity (Subtype_Mark (I)));
15379 elsif Nkind (I) = N_Attribute_Reference then
15381 -- The parser guarantees that the attribute is a RANGE attribute
15383 -- If the node denotes the range of a type mark, that is also the
15384 -- resulting type, and we do no need to create an Itype for it.
15386 if Is_Entity_Name (Prefix (I))
15387 and then Comes_From_Source (I)
15388 and then Is_Type (Entity (Prefix (I)))
15389 and then Is_Discrete_Type (Entity (Prefix (I)))
15390 then
15391 Def_Id := Entity (Prefix (I));
15392 end if;
15394 Analyze_And_Resolve (I);
15395 T := Etype (I);
15396 R := I;
15398 -- If none of the above, must be a subtype. We convert this to a
15399 -- range attribute reference because in the case of declared first
15400 -- named subtypes, the types in the range reference can be different
15401 -- from the type of the entity. A range attribute normalizes the
15402 -- reference and obtains the correct types for the bounds.
15404 -- This transformation is in the nature of an expansion, is only
15405 -- done if expansion is active. In particular, it is not done on
15406 -- formal generic types, because we need to retain the name of the
15407 -- original index for instantiation purposes.
15409 else
15410 if not Is_Entity_Name (I) or else not Is_Type (Entity (I)) then
15411 Error_Msg_N ("invalid subtype mark in discrete range ", I);
15412 Set_Etype (I, Any_Integer);
15413 return;
15415 else
15416 -- The type mark may be that of an incomplete type. It is only
15417 -- now that we can get the full view, previous analysis does
15418 -- not look specifically for a type mark.
15420 Set_Entity (I, Get_Full_View (Entity (I)));
15421 Set_Etype (I, Entity (I));
15422 Def_Id := Entity (I);
15424 if not Is_Discrete_Type (Def_Id) then
15425 Error_Msg_N ("discrete type required for index", I);
15426 Set_Etype (I, Any_Type);
15427 return;
15428 end if;
15429 end if;
15431 if Expander_Active then
15432 Rewrite (I,
15433 Make_Attribute_Reference (Sloc (I),
15434 Attribute_Name => Name_Range,
15435 Prefix => Relocate_Node (I)));
15437 -- The original was a subtype mark that does not freeze. This
15438 -- means that the rewritten version must not freeze either.
15440 Set_Must_Not_Freeze (I);
15441 Set_Must_Not_Freeze (Prefix (I));
15443 -- Is order critical??? if so, document why, if not
15444 -- use Analyze_And_Resolve
15446 Analyze_And_Resolve (I);
15447 T := Etype (I);
15448 R := I;
15450 -- If expander is inactive, type is legal, nothing else to construct
15452 else
15453 return;
15454 end if;
15455 end if;
15457 if not Is_Discrete_Type (T) then
15458 Error_Msg_N ("discrete type required for range", I);
15459 Set_Etype (I, Any_Type);
15460 return;
15462 elsif T = Any_Type then
15463 Set_Etype (I, Any_Type);
15464 return;
15465 end if;
15467 -- We will now create the appropriate Itype to describe the range, but
15468 -- first a check. If we originally had a subtype, then we just label
15469 -- the range with this subtype. Not only is there no need to construct
15470 -- a new subtype, but it is wrong to do so for two reasons:
15472 -- 1. A legality concern, if we have a subtype, it must not freeze,
15473 -- and the Itype would cause freezing incorrectly
15475 -- 2. An efficiency concern, if we created an Itype, it would not be
15476 -- recognized as the same type for the purposes of eliminating
15477 -- checks in some circumstances.
15479 -- We signal this case by setting the subtype entity in Def_Id
15481 if No (Def_Id) then
15482 Def_Id :=
15483 Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
15484 Set_Etype (Def_Id, Base_Type (T));
15486 if Is_Signed_Integer_Type (T) then
15487 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
15489 elsif Is_Modular_Integer_Type (T) then
15490 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
15492 else
15493 Set_Ekind (Def_Id, E_Enumeration_Subtype);
15494 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
15495 Set_First_Literal (Def_Id, First_Literal (T));
15496 end if;
15498 Set_Size_Info (Def_Id, (T));
15499 Set_RM_Size (Def_Id, RM_Size (T));
15500 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
15502 Set_Scalar_Range (Def_Id, R);
15503 Conditional_Delay (Def_Id, T);
15505 -- In the subtype indication case, if the immediate parent of the
15506 -- new subtype is non-static, then the subtype we create is non-
15507 -- static, even if its bounds are static.
15509 if Nkind (I) = N_Subtype_Indication
15510 and then not Is_Static_Subtype (Entity (Subtype_Mark (I)))
15511 then
15512 Set_Is_Non_Static_Subtype (Def_Id);
15513 end if;
15514 end if;
15516 -- Final step is to label the index with this constructed type
15518 Set_Etype (I, Def_Id);
15519 end Make_Index;
15521 ------------------------------
15522 -- Modular_Type_Declaration --
15523 ------------------------------
15525 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
15526 Mod_Expr : constant Node_Id := Expression (Def);
15527 M_Val : Uint;
15529 procedure Set_Modular_Size (Bits : Int);
15530 -- Sets RM_Size to Bits, and Esize to normal word size above this
15532 ----------------------
15533 -- Set_Modular_Size --
15534 ----------------------
15536 procedure Set_Modular_Size (Bits : Int) is
15537 begin
15538 Set_RM_Size (T, UI_From_Int (Bits));
15540 if Bits <= 8 then
15541 Init_Esize (T, 8);
15543 elsif Bits <= 16 then
15544 Init_Esize (T, 16);
15546 elsif Bits <= 32 then
15547 Init_Esize (T, 32);
15549 else
15550 Init_Esize (T, System_Max_Binary_Modulus_Power);
15551 end if;
15553 if not Non_Binary_Modulus (T)
15554 and then Esize (T) = RM_Size (T)
15555 then
15556 Set_Is_Known_Valid (T);
15557 end if;
15558 end Set_Modular_Size;
15560 -- Start of processing for Modular_Type_Declaration
15562 begin
15563 Analyze_And_Resolve (Mod_Expr, Any_Integer);
15564 Set_Etype (T, T);
15565 Set_Ekind (T, E_Modular_Integer_Type);
15566 Init_Alignment (T);
15567 Set_Is_Constrained (T);
15569 if not Is_OK_Static_Expression (Mod_Expr) then
15570 Flag_Non_Static_Expr
15571 ("non-static expression used for modular type bound!", Mod_Expr);
15572 M_Val := 2 ** System_Max_Binary_Modulus_Power;
15573 else
15574 M_Val := Expr_Value (Mod_Expr);
15575 end if;
15577 if M_Val < 1 then
15578 Error_Msg_N ("modulus value must be positive", Mod_Expr);
15579 M_Val := 2 ** System_Max_Binary_Modulus_Power;
15580 end if;
15582 Set_Modulus (T, M_Val);
15584 -- Create bounds for the modular type based on the modulus given in
15585 -- the type declaration and then analyze and resolve those bounds.
15587 Set_Scalar_Range (T,
15588 Make_Range (Sloc (Mod_Expr),
15589 Low_Bound =>
15590 Make_Integer_Literal (Sloc (Mod_Expr), 0),
15591 High_Bound =>
15592 Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
15594 -- Properly analyze the literals for the range. We do this manually
15595 -- because we can't go calling Resolve, since we are resolving these
15596 -- bounds with the type, and this type is certainly not complete yet!
15598 Set_Etype (Low_Bound (Scalar_Range (T)), T);
15599 Set_Etype (High_Bound (Scalar_Range (T)), T);
15600 Set_Is_Static_Expression (Low_Bound (Scalar_Range (T)));
15601 Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
15603 -- Loop through powers of two to find number of bits required
15605 for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
15607 -- Binary case
15609 if M_Val = 2 ** Bits then
15610 Set_Modular_Size (Bits);
15611 return;
15613 -- Non-binary case
15615 elsif M_Val < 2 ** Bits then
15616 Set_Non_Binary_Modulus (T);
15618 if Bits > System_Max_Nonbinary_Modulus_Power then
15619 Error_Msg_Uint_1 :=
15620 UI_From_Int (System_Max_Nonbinary_Modulus_Power);
15621 Error_Msg_F
15622 ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
15623 Set_Modular_Size (System_Max_Binary_Modulus_Power);
15624 return;
15626 else
15627 -- In the non-binary case, set size as per RM 13.3(55)
15629 Set_Modular_Size (Bits);
15630 return;
15631 end if;
15632 end if;
15634 end loop;
15636 -- If we fall through, then the size exceed System.Max_Binary_Modulus
15637 -- so we just signal an error and set the maximum size.
15639 Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
15640 Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
15642 Set_Modular_Size (System_Max_Binary_Modulus_Power);
15643 Init_Alignment (T);
15645 end Modular_Type_Declaration;
15647 --------------------------
15648 -- New_Concatenation_Op --
15649 --------------------------
15651 procedure New_Concatenation_Op (Typ : Entity_Id) is
15652 Loc : constant Source_Ptr := Sloc (Typ);
15653 Op : Entity_Id;
15655 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
15656 -- Create abbreviated declaration for the formal of a predefined
15657 -- Operator 'Op' of type 'Typ'
15659 --------------------
15660 -- Make_Op_Formal --
15661 --------------------
15663 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
15664 Formal : Entity_Id;
15665 begin
15666 Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
15667 Set_Etype (Formal, Typ);
15668 Set_Mechanism (Formal, Default_Mechanism);
15669 return Formal;
15670 end Make_Op_Formal;
15672 -- Start of processing for New_Concatenation_Op
15674 begin
15675 Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
15677 Set_Ekind (Op, E_Operator);
15678 Set_Scope (Op, Current_Scope);
15679 Set_Etype (Op, Typ);
15680 Set_Homonym (Op, Get_Name_Entity_Id (Name_Op_Concat));
15681 Set_Is_Immediately_Visible (Op);
15682 Set_Is_Intrinsic_Subprogram (Op);
15683 Set_Has_Completion (Op);
15684 Append_Entity (Op, Current_Scope);
15686 Set_Name_Entity_Id (Name_Op_Concat, Op);
15688 Append_Entity (Make_Op_Formal (Typ, Op), Op);
15689 Append_Entity (Make_Op_Formal (Typ, Op), Op);
15690 end New_Concatenation_Op;
15692 -------------------------
15693 -- OK_For_Limited_Init --
15694 -------------------------
15696 -- ???Check all calls of this, and compare the conditions under which it's
15697 -- called.
15699 function OK_For_Limited_Init
15700 (Typ : Entity_Id;
15701 Exp : Node_Id) return Boolean
15703 begin
15704 return Is_CPP_Constructor_Call (Exp)
15705 or else (Ada_Version >= Ada_05
15706 and then not Debug_Flag_Dot_L
15707 and then OK_For_Limited_Init_In_05 (Typ, Exp));
15708 end OK_For_Limited_Init;
15710 -------------------------------
15711 -- OK_For_Limited_Init_In_05 --
15712 -------------------------------
15714 function OK_For_Limited_Init_In_05
15715 (Typ : Entity_Id;
15716 Exp : Node_Id) return Boolean
15718 begin
15719 -- An object of a limited interface type can be initialized with any
15720 -- expression of a nonlimited descendant type.
15722 if Is_Class_Wide_Type (Typ)
15723 and then Is_Limited_Interface (Typ)
15724 and then not Is_Limited_Type (Etype (Exp))
15725 then
15726 return True;
15727 end if;
15729 -- Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
15730 -- case of limited aggregates (including extension aggregates), and
15731 -- function calls. The function call may have been give in prefixed
15732 -- notation, in which case the original node is an indexed component.
15734 case Nkind (Original_Node (Exp)) is
15735 when N_Aggregate | N_Extension_Aggregate | N_Function_Call | N_Op =>
15736 return True;
15738 when N_Qualified_Expression =>
15739 return
15740 OK_For_Limited_Init_In_05
15741 (Typ, Expression (Original_Node (Exp)));
15743 -- Ada 2005 (AI-251): If a class-wide interface object is initialized
15744 -- with a function call, the expander has rewritten the call into an
15745 -- N_Type_Conversion node to force displacement of the pointer to
15746 -- reference the component containing the secondary dispatch table.
15747 -- Otherwise a type conversion is not a legal context.
15748 -- A return statement for a build-in-place function returning a
15749 -- synchronized type also introduces an unchecked conversion.
15751 when N_Type_Conversion | N_Unchecked_Type_Conversion =>
15752 return not Comes_From_Source (Exp)
15753 and then
15754 OK_For_Limited_Init_In_05
15755 (Typ, Expression (Original_Node (Exp)));
15757 when N_Indexed_Component | N_Selected_Component =>
15758 return Nkind (Exp) = N_Function_Call;
15760 -- A use of 'Input is a function call, hence allowed. Normally the
15761 -- attribute will be changed to a call, but the attribute by itself
15762 -- can occur with -gnatc.
15764 when N_Attribute_Reference =>
15765 return Attribute_Name (Original_Node (Exp)) = Name_Input;
15767 when others =>
15768 return False;
15769 end case;
15770 end OK_For_Limited_Init_In_05;
15772 -------------------------------------------
15773 -- Ordinary_Fixed_Point_Type_Declaration --
15774 -------------------------------------------
15776 procedure Ordinary_Fixed_Point_Type_Declaration
15777 (T : Entity_Id;
15778 Def : Node_Id)
15780 Loc : constant Source_Ptr := Sloc (Def);
15781 Delta_Expr : constant Node_Id := Delta_Expression (Def);
15782 RRS : constant Node_Id := Real_Range_Specification (Def);
15783 Implicit_Base : Entity_Id;
15784 Delta_Val : Ureal;
15785 Small_Val : Ureal;
15786 Low_Val : Ureal;
15787 High_Val : Ureal;
15789 begin
15790 Check_Restriction (No_Fixed_Point, Def);
15792 -- Create implicit base type
15794 Implicit_Base :=
15795 Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
15796 Set_Etype (Implicit_Base, Implicit_Base);
15798 -- Analyze and process delta expression
15800 Analyze_And_Resolve (Delta_Expr, Any_Real);
15802 Check_Delta_Expression (Delta_Expr);
15803 Delta_Val := Expr_Value_R (Delta_Expr);
15805 Set_Delta_Value (Implicit_Base, Delta_Val);
15807 -- Compute default small from given delta, which is the largest power
15808 -- of two that does not exceed the given delta value.
15810 declare
15811 Tmp : Ureal;
15812 Scale : Int;
15814 begin
15815 Tmp := Ureal_1;
15816 Scale := 0;
15818 if Delta_Val < Ureal_1 then
15819 while Delta_Val < Tmp loop
15820 Tmp := Tmp / Ureal_2;
15821 Scale := Scale + 1;
15822 end loop;
15824 else
15825 loop
15826 Tmp := Tmp * Ureal_2;
15827 exit when Tmp > Delta_Val;
15828 Scale := Scale - 1;
15829 end loop;
15830 end if;
15832 Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
15833 end;
15835 Set_Small_Value (Implicit_Base, Small_Val);
15837 -- If no range was given, set a dummy range
15839 if RRS <= Empty_Or_Error then
15840 Low_Val := -Small_Val;
15841 High_Val := Small_Val;
15843 -- Otherwise analyze and process given range
15845 else
15846 declare
15847 Low : constant Node_Id := Low_Bound (RRS);
15848 High : constant Node_Id := High_Bound (RRS);
15850 begin
15851 Analyze_And_Resolve (Low, Any_Real);
15852 Analyze_And_Resolve (High, Any_Real);
15853 Check_Real_Bound (Low);
15854 Check_Real_Bound (High);
15856 -- Obtain and set the range
15858 Low_Val := Expr_Value_R (Low);
15859 High_Val := Expr_Value_R (High);
15861 if Low_Val > High_Val then
15862 Error_Msg_NE ("?fixed point type& has null range", Def, T);
15863 end if;
15864 end;
15865 end if;
15867 -- The range for both the implicit base and the declared first subtype
15868 -- cannot be set yet, so we use the special routine Set_Fixed_Range to
15869 -- set a temporary range in place. Note that the bounds of the base
15870 -- type will be widened to be symmetrical and to fill the available
15871 -- bits when the type is frozen.
15873 -- We could do this with all discrete types, and probably should, but
15874 -- we absolutely have to do it for fixed-point, since the end-points
15875 -- of the range and the size are determined by the small value, which
15876 -- could be reset before the freeze point.
15878 Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
15879 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
15881 -- Complete definition of first subtype
15883 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
15884 Set_Etype (T, Implicit_Base);
15885 Init_Size_Align (T);
15886 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
15887 Set_Small_Value (T, Small_Val);
15888 Set_Delta_Value (T, Delta_Val);
15889 Set_Is_Constrained (T);
15891 end Ordinary_Fixed_Point_Type_Declaration;
15893 ----------------------------------------
15894 -- Prepare_Private_Subtype_Completion --
15895 ----------------------------------------
15897 procedure Prepare_Private_Subtype_Completion
15898 (Id : Entity_Id;
15899 Related_Nod : Node_Id)
15901 Id_B : constant Entity_Id := Base_Type (Id);
15902 Full_B : constant Entity_Id := Full_View (Id_B);
15903 Full : Entity_Id;
15905 begin
15906 if Present (Full_B) then
15908 -- The Base_Type is already completed, we can complete the subtype
15909 -- now. We have to create a new entity with the same name, Thus we
15910 -- can't use Create_Itype.
15912 -- This is messy, should be fixed ???
15914 Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
15915 Set_Is_Itype (Full);
15916 Set_Associated_Node_For_Itype (Full, Related_Nod);
15917 Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
15918 end if;
15920 -- The parent subtype may be private, but the base might not, in some
15921 -- nested instances. In that case, the subtype does not need to be
15922 -- exchanged. It would still be nice to make private subtypes and their
15923 -- bases consistent at all times ???
15925 if Is_Private_Type (Id_B) then
15926 Append_Elmt (Id, Private_Dependents (Id_B));
15927 end if;
15929 end Prepare_Private_Subtype_Completion;
15931 ---------------------------
15932 -- Process_Discriminants --
15933 ---------------------------
15935 procedure Process_Discriminants
15936 (N : Node_Id;
15937 Prev : Entity_Id := Empty)
15939 Elist : constant Elist_Id := New_Elmt_List;
15940 Id : Node_Id;
15941 Discr : Node_Id;
15942 Discr_Number : Uint;
15943 Discr_Type : Entity_Id;
15944 Default_Present : Boolean := False;
15945 Default_Not_Present : Boolean := False;
15947 begin
15948 -- A composite type other than an array type can have discriminants.
15949 -- On entry, the current scope is the composite type.
15951 -- The discriminants are initially entered into the scope of the type
15952 -- via Enter_Name with the default Ekind of E_Void to prevent premature
15953 -- use, as explained at the end of this procedure.
15955 Discr := First (Discriminant_Specifications (N));
15956 while Present (Discr) loop
15957 Enter_Name (Defining_Identifier (Discr));
15959 -- For navigation purposes we add a reference to the discriminant
15960 -- in the entity for the type. If the current declaration is a
15961 -- completion, place references on the partial view. Otherwise the
15962 -- type is the current scope.
15964 if Present (Prev) then
15966 -- The references go on the partial view, if present. If the
15967 -- partial view has discriminants, the references have been
15968 -- generated already.
15970 if not Has_Discriminants (Prev) then
15971 Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
15972 end if;
15973 else
15974 Generate_Reference
15975 (Current_Scope, Defining_Identifier (Discr), 'd');
15976 end if;
15978 if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
15979 Discr_Type := Access_Definition (Discr, Discriminant_Type (Discr));
15981 -- Ada 2005 (AI-254)
15983 if Present (Access_To_Subprogram_Definition
15984 (Discriminant_Type (Discr)))
15985 and then Protected_Present (Access_To_Subprogram_Definition
15986 (Discriminant_Type (Discr)))
15987 then
15988 Discr_Type :=
15989 Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
15990 end if;
15992 else
15993 Find_Type (Discriminant_Type (Discr));
15994 Discr_Type := Etype (Discriminant_Type (Discr));
15996 if Error_Posted (Discriminant_Type (Discr)) then
15997 Discr_Type := Any_Type;
15998 end if;
15999 end if;
16001 if Is_Access_Type (Discr_Type) then
16003 -- Ada 2005 (AI-230): Access discriminant allowed in non-limited
16004 -- record types
16006 if Ada_Version < Ada_05 then
16007 Check_Access_Discriminant_Requires_Limited
16008 (Discr, Discriminant_Type (Discr));
16009 end if;
16011 if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
16012 Error_Msg_N
16013 ("(Ada 83) access discriminant not allowed", Discr);
16014 end if;
16016 elsif not Is_Discrete_Type (Discr_Type) then
16017 Error_Msg_N ("discriminants must have a discrete or access type",
16018 Discriminant_Type (Discr));
16019 end if;
16021 Set_Etype (Defining_Identifier (Discr), Discr_Type);
16023 -- If a discriminant specification includes the assignment compound
16024 -- delimiter followed by an expression, the expression is the default
16025 -- expression of the discriminant; the default expression must be of
16026 -- the type of the discriminant. (RM 3.7.1) Since this expression is
16027 -- a default expression, we do the special preanalysis, since this
16028 -- expression does not freeze (see "Handling of Default and Per-
16029 -- Object Expressions" in spec of package Sem).
16031 if Present (Expression (Discr)) then
16032 Preanalyze_Spec_Expression (Expression (Discr), Discr_Type);
16034 if Nkind (N) = N_Formal_Type_Declaration then
16035 Error_Msg_N
16036 ("discriminant defaults not allowed for formal type",
16037 Expression (Discr));
16039 -- Tagged types cannot have defaulted discriminants, but a
16040 -- non-tagged private type with defaulted discriminants
16041 -- can have a tagged completion.
16043 elsif Is_Tagged_Type (Current_Scope)
16044 and then Comes_From_Source (N)
16045 then
16046 Error_Msg_N
16047 ("discriminants of tagged type cannot have defaults",
16048 Expression (Discr));
16050 else
16051 Default_Present := True;
16052 Append_Elmt (Expression (Discr), Elist);
16054 -- Tag the defining identifiers for the discriminants with
16055 -- their corresponding default expressions from the tree.
16057 Set_Discriminant_Default_Value
16058 (Defining_Identifier (Discr), Expression (Discr));
16059 end if;
16061 else
16062 Default_Not_Present := True;
16063 end if;
16065 -- Ada 2005 (AI-231): Create an Itype that is a duplicate of
16066 -- Discr_Type but with the null-exclusion attribute
16068 if Ada_Version >= Ada_05 then
16070 -- Ada 2005 (AI-231): Static checks
16072 if Can_Never_Be_Null (Discr_Type) then
16073 Null_Exclusion_Static_Checks (Discr);
16075 elsif Is_Access_Type (Discr_Type)
16076 and then Null_Exclusion_Present (Discr)
16078 -- No need to check itypes because in their case this check
16079 -- was done at their point of creation
16081 and then not Is_Itype (Discr_Type)
16082 then
16083 if Can_Never_Be_Null (Discr_Type) then
16084 Error_Msg_NE
16085 ("`NOT NULL` not allowed (& already excludes null)",
16086 Discr,
16087 Discr_Type);
16088 end if;
16090 Set_Etype (Defining_Identifier (Discr),
16091 Create_Null_Excluding_Itype
16092 (T => Discr_Type,
16093 Related_Nod => Discr));
16095 -- Check for improper null exclusion if the type is otherwise
16096 -- legal for a discriminant.
16098 elsif Null_Exclusion_Present (Discr)
16099 and then Is_Discrete_Type (Discr_Type)
16100 then
16101 Error_Msg_N
16102 ("null exclusion can only apply to an access type", Discr);
16103 end if;
16105 -- Ada 2005 (AI-402): access discriminants of nonlimited types
16106 -- can't have defaults. Synchronized types, or types that are
16107 -- explicitly limited are fine, but special tests apply to derived
16108 -- types in generics: in a generic body we have to assume the
16109 -- worst, and therefore defaults are not allowed if the parent is
16110 -- a generic formal private type (see ACATS B370001).
16112 if Is_Access_Type (Discr_Type) then
16113 if Ekind (Discr_Type) /= E_Anonymous_Access_Type
16114 or else not Default_Present
16115 or else Is_Limited_Record (Current_Scope)
16116 or else Is_Concurrent_Type (Current_Scope)
16117 or else Is_Concurrent_Record_Type (Current_Scope)
16118 or else Ekind (Current_Scope) = E_Limited_Private_Type
16119 then
16120 if not Is_Derived_Type (Current_Scope)
16121 or else not Is_Generic_Type (Etype (Current_Scope))
16122 or else not In_Package_Body (Scope (Etype (Current_Scope)))
16123 or else Limited_Present
16124 (Type_Definition (Parent (Current_Scope)))
16125 then
16126 null;
16128 else
16129 Error_Msg_N ("access discriminants of nonlimited types",
16130 Expression (Discr));
16131 Error_Msg_N ("\cannot have defaults", Expression (Discr));
16132 end if;
16134 elsif Present (Expression (Discr)) then
16135 Error_Msg_N
16136 ("(Ada 2005) access discriminants of nonlimited types",
16137 Expression (Discr));
16138 Error_Msg_N ("\cannot have defaults", Expression (Discr));
16139 end if;
16140 end if;
16141 end if;
16143 Next (Discr);
16144 end loop;
16146 -- An element list consisting of the default expressions of the
16147 -- discriminants is constructed in the above loop and used to set
16148 -- the Discriminant_Constraint attribute for the type. If an object
16149 -- is declared of this (record or task) type without any explicit
16150 -- discriminant constraint given, this element list will form the
16151 -- actual parameters for the corresponding initialization procedure
16152 -- for the type.
16154 Set_Discriminant_Constraint (Current_Scope, Elist);
16155 Set_Stored_Constraint (Current_Scope, No_Elist);
16157 -- Default expressions must be provided either for all or for none
16158 -- of the discriminants of a discriminant part. (RM 3.7.1)
16160 if Default_Present and then Default_Not_Present then
16161 Error_Msg_N
16162 ("incomplete specification of defaults for discriminants", N);
16163 end if;
16165 -- The use of the name of a discriminant is not allowed in default
16166 -- expressions of a discriminant part if the specification of the
16167 -- discriminant is itself given in the discriminant part. (RM 3.7.1)
16169 -- To detect this, the discriminant names are entered initially with an
16170 -- Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
16171 -- attempt to use a void entity (for example in an expression that is
16172 -- type-checked) produces the error message: premature usage. Now after
16173 -- completing the semantic analysis of the discriminant part, we can set
16174 -- the Ekind of all the discriminants appropriately.
16176 Discr := First (Discriminant_Specifications (N));
16177 Discr_Number := Uint_1;
16178 while Present (Discr) loop
16179 Id := Defining_Identifier (Discr);
16180 Set_Ekind (Id, E_Discriminant);
16181 Init_Component_Location (Id);
16182 Init_Esize (Id);
16183 Set_Discriminant_Number (Id, Discr_Number);
16185 -- Make sure this is always set, even in illegal programs
16187 Set_Corresponding_Discriminant (Id, Empty);
16189 -- Initialize the Original_Record_Component to the entity itself.
16190 -- Inherit_Components will propagate the right value to
16191 -- discriminants in derived record types.
16193 Set_Original_Record_Component (Id, Id);
16195 -- Create the discriminal for the discriminant
16197 Build_Discriminal (Id);
16199 Next (Discr);
16200 Discr_Number := Discr_Number + 1;
16201 end loop;
16203 Set_Has_Discriminants (Current_Scope);
16204 end Process_Discriminants;
16206 -----------------------
16207 -- Process_Full_View --
16208 -----------------------
16210 procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
16211 Priv_Parent : Entity_Id;
16212 Full_Parent : Entity_Id;
16213 Full_Indic : Node_Id;
16215 procedure Collect_Implemented_Interfaces
16216 (Typ : Entity_Id;
16217 Ifaces : Elist_Id);
16218 -- Ada 2005: Gather all the interfaces that Typ directly or
16219 -- inherently implements. Duplicate entries are not added to
16220 -- the list Ifaces.
16222 ------------------------------------
16223 -- Collect_Implemented_Interfaces --
16224 ------------------------------------
16226 procedure Collect_Implemented_Interfaces
16227 (Typ : Entity_Id;
16228 Ifaces : Elist_Id)
16230 Iface : Entity_Id;
16231 Iface_Elmt : Elmt_Id;
16233 begin
16234 -- Abstract interfaces are only associated with tagged record types
16236 if not Is_Tagged_Type (Typ)
16237 or else not Is_Record_Type (Typ)
16238 then
16239 return;
16240 end if;
16242 -- Recursively climb to the ancestors
16244 if Etype (Typ) /= Typ
16246 -- Protect the frontend against wrong cyclic declarations like:
16248 -- type B is new A with private;
16249 -- type C is new A with private;
16250 -- private
16251 -- type B is new C with null record;
16252 -- type C is new B with null record;
16254 and then Etype (Typ) /= Priv_T
16255 and then Etype (Typ) /= Full_T
16256 then
16257 -- Keep separate the management of private type declarations
16259 if Ekind (Typ) = E_Record_Type_With_Private then
16261 -- Handle the following erronous case:
16262 -- type Private_Type is tagged private;
16263 -- private
16264 -- type Private_Type is new Type_Implementing_Iface;
16266 if Present (Full_View (Typ))
16267 and then Etype (Typ) /= Full_View (Typ)
16268 then
16269 if Is_Interface (Etype (Typ)) then
16270 Append_Unique_Elmt (Etype (Typ), Ifaces);
16271 end if;
16273 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
16274 end if;
16276 -- Non-private types
16278 else
16279 if Is_Interface (Etype (Typ)) then
16280 Append_Unique_Elmt (Etype (Typ), Ifaces);
16281 end if;
16283 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
16284 end if;
16285 end if;
16287 -- Handle entities in the list of abstract interfaces
16289 if Present (Interfaces (Typ)) then
16290 Iface_Elmt := First_Elmt (Interfaces (Typ));
16291 while Present (Iface_Elmt) loop
16292 Iface := Node (Iface_Elmt);
16294 pragma Assert (Is_Interface (Iface));
16296 if not Contain_Interface (Iface, Ifaces) then
16297 Append_Elmt (Iface, Ifaces);
16298 Collect_Implemented_Interfaces (Iface, Ifaces);
16299 end if;
16301 Next_Elmt (Iface_Elmt);
16302 end loop;
16303 end if;
16304 end Collect_Implemented_Interfaces;
16306 -- Start of processing for Process_Full_View
16308 begin
16309 -- First some sanity checks that must be done after semantic
16310 -- decoration of the full view and thus cannot be placed with other
16311 -- similar checks in Find_Type_Name
16313 if not Is_Limited_Type (Priv_T)
16314 and then (Is_Limited_Type (Full_T)
16315 or else Is_Limited_Composite (Full_T))
16316 then
16317 Error_Msg_N
16318 ("completion of nonlimited type cannot be limited", Full_T);
16319 Explain_Limited_Type (Full_T, Full_T);
16321 elsif Is_Abstract_Type (Full_T)
16322 and then not Is_Abstract_Type (Priv_T)
16323 then
16324 Error_Msg_N
16325 ("completion of nonabstract type cannot be abstract", Full_T);
16327 elsif Is_Tagged_Type (Priv_T)
16328 and then Is_Limited_Type (Priv_T)
16329 and then not Is_Limited_Type (Full_T)
16330 then
16331 -- If pragma CPP_Class was applied to the private declaration
16332 -- propagate the limitedness to the full-view
16334 if Is_CPP_Class (Priv_T) then
16335 Set_Is_Limited_Record (Full_T);
16337 -- GNAT allow its own definition of Limited_Controlled to disobey
16338 -- this rule in order in ease the implementation. The next test is
16339 -- safe because Root_Controlled is defined in a private system child
16341 elsif Etype (Full_T) = Full_View (RTE (RE_Root_Controlled)) then
16342 Set_Is_Limited_Composite (Full_T);
16343 else
16344 Error_Msg_N
16345 ("completion of limited tagged type must be limited", Full_T);
16346 end if;
16348 elsif Is_Generic_Type (Priv_T) then
16349 Error_Msg_N ("generic type cannot have a completion", Full_T);
16350 end if;
16352 -- Check that ancestor interfaces of private and full views are
16353 -- consistent. We omit this check for synchronized types because
16354 -- they are performed on the corresponding record type when frozen.
16356 if Ada_Version >= Ada_05
16357 and then Is_Tagged_Type (Priv_T)
16358 and then Is_Tagged_Type (Full_T)
16359 and then not Is_Concurrent_Type (Full_T)
16360 then
16361 declare
16362 Iface : Entity_Id;
16363 Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
16364 Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
16366 begin
16367 Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
16368 Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
16370 -- Ada 2005 (AI-251): The partial view shall be a descendant of
16371 -- an interface type if and only if the full type is descendant
16372 -- of the interface type (AARM 7.3 (7.3/2).
16374 Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
16376 if Present (Iface) then
16377 Error_Msg_NE
16378 ("interface & not implemented by full type " &
16379 "(RM-2005 7.3 (7.3/2))", Priv_T, Iface);
16380 end if;
16382 Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
16384 if Present (Iface) then
16385 Error_Msg_NE
16386 ("interface & not implemented by partial view " &
16387 "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
16388 end if;
16389 end;
16390 end if;
16392 if Is_Tagged_Type (Priv_T)
16393 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
16394 and then Is_Derived_Type (Full_T)
16395 then
16396 Priv_Parent := Etype (Priv_T);
16398 -- The full view of a private extension may have been transformed
16399 -- into an unconstrained derived type declaration and a subtype
16400 -- declaration (see build_derived_record_type for details).
16402 if Nkind (N) = N_Subtype_Declaration then
16403 Full_Indic := Subtype_Indication (N);
16404 Full_Parent := Etype (Base_Type (Full_T));
16405 else
16406 Full_Indic := Subtype_Indication (Type_Definition (N));
16407 Full_Parent := Etype (Full_T);
16408 end if;
16410 -- Check that the parent type of the full type is a descendant of
16411 -- the ancestor subtype given in the private extension. If either
16412 -- entity has an Etype equal to Any_Type then we had some previous
16413 -- error situation [7.3(8)].
16415 if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
16416 return;
16418 -- Ada 2005 (AI-251): Interfaces in the full-typ can be given in
16419 -- any order. Therefore we don't have to check that its parent must
16420 -- be a descendant of the parent of the private type declaration.
16422 elsif Is_Interface (Priv_Parent)
16423 and then Is_Interface (Full_Parent)
16424 then
16425 null;
16427 -- Ada 2005 (AI-251): If the parent of the private type declaration
16428 -- is an interface there is no need to check that it is an ancestor
16429 -- of the associated full type declaration. The required tests for
16430 -- this case are performed by Build_Derived_Record_Type.
16432 elsif not Is_Interface (Base_Type (Priv_Parent))
16433 and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
16434 then
16435 Error_Msg_N
16436 ("parent of full type must descend from parent"
16437 & " of private extension", Full_Indic);
16439 -- Check the rules of 7.3(10): if the private extension inherits
16440 -- known discriminants, then the full type must also inherit those
16441 -- discriminants from the same (ancestor) type, and the parent
16442 -- subtype of the full type must be constrained if and only if
16443 -- the ancestor subtype of the private extension is constrained.
16445 elsif No (Discriminant_Specifications (Parent (Priv_T)))
16446 and then not Has_Unknown_Discriminants (Priv_T)
16447 and then Has_Discriminants (Base_Type (Priv_Parent))
16448 then
16449 declare
16450 Priv_Indic : constant Node_Id :=
16451 Subtype_Indication (Parent (Priv_T));
16453 Priv_Constr : constant Boolean :=
16454 Is_Constrained (Priv_Parent)
16455 or else
16456 Nkind (Priv_Indic) = N_Subtype_Indication
16457 or else Is_Constrained (Entity (Priv_Indic));
16459 Full_Constr : constant Boolean :=
16460 Is_Constrained (Full_Parent)
16461 or else
16462 Nkind (Full_Indic) = N_Subtype_Indication
16463 or else Is_Constrained (Entity (Full_Indic));
16465 Priv_Discr : Entity_Id;
16466 Full_Discr : Entity_Id;
16468 begin
16469 Priv_Discr := First_Discriminant (Priv_Parent);
16470 Full_Discr := First_Discriminant (Full_Parent);
16471 while Present (Priv_Discr) and then Present (Full_Discr) loop
16472 if Original_Record_Component (Priv_Discr) =
16473 Original_Record_Component (Full_Discr)
16474 or else
16475 Corresponding_Discriminant (Priv_Discr) =
16476 Corresponding_Discriminant (Full_Discr)
16477 then
16478 null;
16479 else
16480 exit;
16481 end if;
16483 Next_Discriminant (Priv_Discr);
16484 Next_Discriminant (Full_Discr);
16485 end loop;
16487 if Present (Priv_Discr) or else Present (Full_Discr) then
16488 Error_Msg_N
16489 ("full view must inherit discriminants of the parent type"
16490 & " used in the private extension", Full_Indic);
16492 elsif Priv_Constr and then not Full_Constr then
16493 Error_Msg_N
16494 ("parent subtype of full type must be constrained",
16495 Full_Indic);
16497 elsif Full_Constr and then not Priv_Constr then
16498 Error_Msg_N
16499 ("parent subtype of full type must be unconstrained",
16500 Full_Indic);
16501 end if;
16502 end;
16504 -- Check the rules of 7.3(12): if a partial view has neither known
16505 -- or unknown discriminants, then the full type declaration shall
16506 -- define a definite subtype.
16508 elsif not Has_Unknown_Discriminants (Priv_T)
16509 and then not Has_Discriminants (Priv_T)
16510 and then not Is_Constrained (Full_T)
16511 then
16512 Error_Msg_N
16513 ("full view must define a constrained type if partial view"
16514 & " has no discriminants", Full_T);
16515 end if;
16517 -- ??????? Do we implement the following properly ?????
16518 -- If the ancestor subtype of a private extension has constrained
16519 -- discriminants, then the parent subtype of the full view shall
16520 -- impose a statically matching constraint on those discriminants
16521 -- [7.3(13)].
16523 else
16524 -- For untagged types, verify that a type without discriminants
16525 -- is not completed with an unconstrained type.
16527 if not Is_Indefinite_Subtype (Priv_T)
16528 and then Is_Indefinite_Subtype (Full_T)
16529 then
16530 Error_Msg_N ("full view of type must be definite subtype", Full_T);
16531 end if;
16532 end if;
16534 -- AI-419: verify that the use of "limited" is consistent
16536 declare
16537 Orig_Decl : constant Node_Id := Original_Node (N);
16539 begin
16540 if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
16541 and then not Limited_Present (Parent (Priv_T))
16542 and then not Synchronized_Present (Parent (Priv_T))
16543 and then Nkind (Orig_Decl) = N_Full_Type_Declaration
16544 and then Nkind
16545 (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
16546 and then Limited_Present (Type_Definition (Orig_Decl))
16547 then
16548 Error_Msg_N
16549 ("full view of non-limited extension cannot be limited", N);
16550 end if;
16551 end;
16553 -- Ada 2005 (AI-443): A synchronized private extension must be
16554 -- completed by a task or protected type.
16556 if Ada_Version >= Ada_05
16557 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
16558 and then Synchronized_Present (Parent (Priv_T))
16559 and then not Is_Concurrent_Type (Full_T)
16560 then
16561 Error_Msg_N ("full view of synchronized extension must " &
16562 "be synchronized type", N);
16563 end if;
16565 -- Ada 2005 AI-363: if the full view has discriminants with
16566 -- defaults, it is illegal to declare constrained access subtypes
16567 -- whose designated type is the current type. This allows objects
16568 -- of the type that are declared in the heap to be unconstrained.
16570 if not Has_Unknown_Discriminants (Priv_T)
16571 and then not Has_Discriminants (Priv_T)
16572 and then Has_Discriminants (Full_T)
16573 and then
16574 Present (Discriminant_Default_Value (First_Discriminant (Full_T)))
16575 then
16576 Set_Has_Constrained_Partial_View (Full_T);
16577 Set_Has_Constrained_Partial_View (Priv_T);
16578 end if;
16580 -- Create a full declaration for all its subtypes recorded in
16581 -- Private_Dependents and swap them similarly to the base type. These
16582 -- are subtypes that have been define before the full declaration of
16583 -- the private type. We also swap the entry in Private_Dependents list
16584 -- so we can properly restore the private view on exit from the scope.
16586 declare
16587 Priv_Elmt : Elmt_Id;
16588 Priv : Entity_Id;
16589 Full : Entity_Id;
16591 begin
16592 Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
16593 while Present (Priv_Elmt) loop
16594 Priv := Node (Priv_Elmt);
16596 if Ekind_In (Priv, E_Private_Subtype,
16597 E_Limited_Private_Subtype,
16598 E_Record_Subtype_With_Private)
16599 then
16600 Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
16601 Set_Is_Itype (Full);
16602 Set_Parent (Full, Parent (Priv));
16603 Set_Associated_Node_For_Itype (Full, N);
16605 -- Now we need to complete the private subtype, but since the
16606 -- base type has already been swapped, we must also swap the
16607 -- subtypes (and thus, reverse the arguments in the call to
16608 -- Complete_Private_Subtype).
16610 Copy_And_Swap (Priv, Full);
16611 Complete_Private_Subtype (Full, Priv, Full_T, N);
16612 Replace_Elmt (Priv_Elmt, Full);
16613 end if;
16615 Next_Elmt (Priv_Elmt);
16616 end loop;
16617 end;
16619 -- If the private view was tagged, copy the new primitive operations
16620 -- from the private view to the full view.
16622 if Is_Tagged_Type (Full_T) then
16623 declare
16624 Disp_Typ : Entity_Id;
16625 Full_List : Elist_Id;
16626 Prim : Entity_Id;
16627 Prim_Elmt : Elmt_Id;
16628 Priv_List : Elist_Id;
16630 function Contains
16631 (E : Entity_Id;
16632 L : Elist_Id) return Boolean;
16633 -- Determine whether list L contains element E
16635 --------------
16636 -- Contains --
16637 --------------
16639 function Contains
16640 (E : Entity_Id;
16641 L : Elist_Id) return Boolean
16643 List_Elmt : Elmt_Id;
16645 begin
16646 List_Elmt := First_Elmt (L);
16647 while Present (List_Elmt) loop
16648 if Node (List_Elmt) = E then
16649 return True;
16650 end if;
16652 Next_Elmt (List_Elmt);
16653 end loop;
16655 return False;
16656 end Contains;
16658 -- Start of processing
16660 begin
16661 if Is_Tagged_Type (Priv_T) then
16662 Priv_List := Primitive_Operations (Priv_T);
16663 Prim_Elmt := First_Elmt (Priv_List);
16665 -- In the case of a concurrent type completing a private tagged
16666 -- type, primitives may have been declared in between the two
16667 -- views. These subprograms need to be wrapped the same way
16668 -- entries and protected procedures are handled because they
16669 -- cannot be directly shared by the two views.
16671 if Is_Concurrent_Type (Full_T) then
16672 declare
16673 Conc_Typ : constant Entity_Id :=
16674 Corresponding_Record_Type (Full_T);
16675 Curr_Nod : Node_Id := Parent (Conc_Typ);
16676 Wrap_Spec : Node_Id;
16678 begin
16679 while Present (Prim_Elmt) loop
16680 Prim := Node (Prim_Elmt);
16682 if Comes_From_Source (Prim)
16683 and then not Is_Abstract_Subprogram (Prim)
16684 then
16685 Wrap_Spec :=
16686 Make_Subprogram_Declaration (Sloc (Prim),
16687 Specification =>
16688 Build_Wrapper_Spec
16689 (Subp_Id => Prim,
16690 Obj_Typ => Conc_Typ,
16691 Formals =>
16692 Parameter_Specifications (
16693 Parent (Prim))));
16695 Insert_After (Curr_Nod, Wrap_Spec);
16696 Curr_Nod := Wrap_Spec;
16698 Analyze (Wrap_Spec);
16699 end if;
16701 Next_Elmt (Prim_Elmt);
16702 end loop;
16704 return;
16705 end;
16707 -- For non-concurrent types, transfer explicit primitives, but
16708 -- omit those inherited from the parent of the private view
16709 -- since they will be re-inherited later on.
16711 else
16712 Full_List := Primitive_Operations (Full_T);
16714 while Present (Prim_Elmt) loop
16715 Prim := Node (Prim_Elmt);
16717 if Comes_From_Source (Prim)
16718 and then not Contains (Prim, Full_List)
16719 then
16720 Append_Elmt (Prim, Full_List);
16721 end if;
16723 Next_Elmt (Prim_Elmt);
16724 end loop;
16725 end if;
16727 -- Untagged private view
16729 else
16730 Full_List := Primitive_Operations (Full_T);
16732 -- In this case the partial view is untagged, so here we locate
16733 -- all of the earlier primitives that need to be treated as
16734 -- dispatching (those that appear between the two views). Note
16735 -- that these additional operations must all be new operations
16736 -- (any earlier operations that override inherited operations
16737 -- of the full view will already have been inserted in the
16738 -- primitives list, marked by Check_Operation_From_Private_View
16739 -- as dispatching. Note that implicit "/=" operators are
16740 -- excluded from being added to the primitives list since they
16741 -- shouldn't be treated as dispatching (tagged "/=" is handled
16742 -- specially).
16744 Prim := Next_Entity (Full_T);
16745 while Present (Prim) and then Prim /= Priv_T loop
16746 if Ekind_In (Prim, E_Procedure, E_Function) then
16747 Disp_Typ := Find_Dispatching_Type (Prim);
16749 if Disp_Typ = Full_T
16750 and then (Chars (Prim) /= Name_Op_Ne
16751 or else Comes_From_Source (Prim))
16752 then
16753 Check_Controlling_Formals (Full_T, Prim);
16755 if not Is_Dispatching_Operation (Prim) then
16756 Append_Elmt (Prim, Full_List);
16757 Set_Is_Dispatching_Operation (Prim, True);
16758 Set_DT_Position (Prim, No_Uint);
16759 end if;
16761 elsif Is_Dispatching_Operation (Prim)
16762 and then Disp_Typ /= Full_T
16763 then
16765 -- Verify that it is not otherwise controlled by a
16766 -- formal or a return value of type T.
16768 Check_Controlling_Formals (Disp_Typ, Prim);
16769 end if;
16770 end if;
16772 Next_Entity (Prim);
16773 end loop;
16774 end if;
16776 -- For the tagged case, the two views can share the same primitive
16777 -- operations list and the same class-wide type. Update attributes
16778 -- of the class-wide type which depend on the full declaration.
16780 if Is_Tagged_Type (Priv_T) then
16781 Set_Primitive_Operations (Priv_T, Full_List);
16782 Set_Class_Wide_Type
16783 (Base_Type (Full_T), Class_Wide_Type (Priv_T));
16785 Set_Has_Task (Class_Wide_Type (Priv_T), Has_Task (Full_T));
16786 end if;
16787 end;
16788 end if;
16790 -- Ada 2005 AI 161: Check preelaboratable initialization consistency
16792 if Known_To_Have_Preelab_Init (Priv_T) then
16794 -- Case where there is a pragma Preelaborable_Initialization. We
16795 -- always allow this in predefined units, which is a bit of a kludge,
16796 -- but it means we don't have to struggle to meet the requirements in
16797 -- the RM for having Preelaborable Initialization. Otherwise we
16798 -- require that the type meets the RM rules. But we can't check that
16799 -- yet, because of the rule about overriding Ininitialize, so we
16800 -- simply set a flag that will be checked at freeze time.
16802 if not In_Predefined_Unit (Full_T) then
16803 Set_Must_Have_Preelab_Init (Full_T);
16804 end if;
16805 end if;
16807 -- If pragma CPP_Class was applied to the private type declaration,
16808 -- propagate it now to the full type declaration.
16810 if Is_CPP_Class (Priv_T) then
16811 Set_Is_CPP_Class (Full_T);
16812 Set_Convention (Full_T, Convention_CPP);
16813 end if;
16815 -- If the private view has user specified stream attributes, then so has
16816 -- the full view.
16818 if Has_Specified_Stream_Read (Priv_T) then
16819 Set_Has_Specified_Stream_Read (Full_T);
16820 end if;
16821 if Has_Specified_Stream_Write (Priv_T) then
16822 Set_Has_Specified_Stream_Write (Full_T);
16823 end if;
16824 if Has_Specified_Stream_Input (Priv_T) then
16825 Set_Has_Specified_Stream_Input (Full_T);
16826 end if;
16827 if Has_Specified_Stream_Output (Priv_T) then
16828 Set_Has_Specified_Stream_Output (Full_T);
16829 end if;
16830 end Process_Full_View;
16832 -----------------------------------
16833 -- Process_Incomplete_Dependents --
16834 -----------------------------------
16836 procedure Process_Incomplete_Dependents
16837 (N : Node_Id;
16838 Full_T : Entity_Id;
16839 Inc_T : Entity_Id)
16841 Inc_Elmt : Elmt_Id;
16842 Priv_Dep : Entity_Id;
16843 New_Subt : Entity_Id;
16845 Disc_Constraint : Elist_Id;
16847 begin
16848 if No (Private_Dependents (Inc_T)) then
16849 return;
16850 end if;
16852 -- Itypes that may be generated by the completion of an incomplete
16853 -- subtype are not used by the back-end and not attached to the tree.
16854 -- They are created only for constraint-checking purposes.
16856 Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
16857 while Present (Inc_Elmt) loop
16858 Priv_Dep := Node (Inc_Elmt);
16860 if Ekind (Priv_Dep) = E_Subprogram_Type then
16862 -- An Access_To_Subprogram type may have a return type or a
16863 -- parameter type that is incomplete. Replace with the full view.
16865 if Etype (Priv_Dep) = Inc_T then
16866 Set_Etype (Priv_Dep, Full_T);
16867 end if;
16869 declare
16870 Formal : Entity_Id;
16872 begin
16873 Formal := First_Formal (Priv_Dep);
16874 while Present (Formal) loop
16875 if Etype (Formal) = Inc_T then
16876 Set_Etype (Formal, Full_T);
16877 end if;
16879 Next_Formal (Formal);
16880 end loop;
16881 end;
16883 elsif Is_Overloadable (Priv_Dep) then
16885 -- A protected operation is never dispatching: only its
16886 -- wrapper operation (which has convention Ada) is.
16888 if Is_Tagged_Type (Full_T)
16889 and then Convention (Priv_Dep) /= Convention_Protected
16890 then
16892 -- Subprogram has an access parameter whose designated type
16893 -- was incomplete. Reexamine declaration now, because it may
16894 -- be a primitive operation of the full type.
16896 Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
16897 Set_Is_Dispatching_Operation (Priv_Dep);
16898 Check_Controlling_Formals (Full_T, Priv_Dep);
16899 end if;
16901 elsif Ekind (Priv_Dep) = E_Subprogram_Body then
16903 -- Can happen during processing of a body before the completion
16904 -- of a TA type. Ignore, because spec is also on dependent list.
16906 return;
16908 -- Ada 2005 (AI-412): Transform a regular incomplete subtype into a
16909 -- corresponding subtype of the full view.
16911 elsif Ekind (Priv_Dep) = E_Incomplete_Subtype then
16912 Set_Subtype_Indication
16913 (Parent (Priv_Dep), New_Reference_To (Full_T, Sloc (Priv_Dep)));
16914 Set_Etype (Priv_Dep, Full_T);
16915 Set_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
16916 Set_Analyzed (Parent (Priv_Dep), False);
16918 -- Reanalyze the declaration, suppressing the call to
16919 -- Enter_Name to avoid duplicate names.
16921 Analyze_Subtype_Declaration
16922 (N => Parent (Priv_Dep),
16923 Skip => True);
16925 -- Dependent is a subtype
16927 else
16928 -- We build a new subtype indication using the full view of the
16929 -- incomplete parent. The discriminant constraints have been
16930 -- elaborated already at the point of the subtype declaration.
16932 New_Subt := Create_Itype (E_Void, N);
16934 if Has_Discriminants (Full_T) then
16935 Disc_Constraint := Discriminant_Constraint (Priv_Dep);
16936 else
16937 Disc_Constraint := No_Elist;
16938 end if;
16940 Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
16941 Set_Full_View (Priv_Dep, New_Subt);
16942 end if;
16944 Next_Elmt (Inc_Elmt);
16945 end loop;
16946 end Process_Incomplete_Dependents;
16948 --------------------------------
16949 -- Process_Range_Expr_In_Decl --
16950 --------------------------------
16952 procedure Process_Range_Expr_In_Decl
16953 (R : Node_Id;
16954 T : Entity_Id;
16955 Check_List : List_Id := Empty_List;
16956 R_Check_Off : Boolean := False)
16958 Lo, Hi : Node_Id;
16959 R_Checks : Check_Result;
16960 Type_Decl : Node_Id;
16961 Def_Id : Entity_Id;
16963 begin
16964 Analyze_And_Resolve (R, Base_Type (T));
16966 if Nkind (R) = N_Range then
16967 Lo := Low_Bound (R);
16968 Hi := High_Bound (R);
16970 -- We need to ensure validity of the bounds here, because if we
16971 -- go ahead and do the expansion, then the expanded code will get
16972 -- analyzed with range checks suppressed and we miss the check.
16974 Validity_Check_Range (R);
16976 -- If there were errors in the declaration, try and patch up some
16977 -- common mistakes in the bounds. The cases handled are literals
16978 -- which are Integer where the expected type is Real and vice versa.
16979 -- These corrections allow the compilation process to proceed further
16980 -- along since some basic assumptions of the format of the bounds
16981 -- are guaranteed.
16983 if Etype (R) = Any_Type then
16985 if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
16986 Rewrite (Lo,
16987 Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
16989 elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
16990 Rewrite (Hi,
16991 Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
16993 elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
16994 Rewrite (Lo,
16995 Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
16997 elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
16998 Rewrite (Hi,
16999 Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
17000 end if;
17002 Set_Etype (Lo, T);
17003 Set_Etype (Hi, T);
17004 end if;
17006 -- If the bounds of the range have been mistakenly given as string
17007 -- literals (perhaps in place of character literals), then an error
17008 -- has already been reported, but we rewrite the string literal as a
17009 -- bound of the range's type to avoid blowups in later processing
17010 -- that looks at static values.
17012 if Nkind (Lo) = N_String_Literal then
17013 Rewrite (Lo,
17014 Make_Attribute_Reference (Sloc (Lo),
17015 Attribute_Name => Name_First,
17016 Prefix => New_Reference_To (T, Sloc (Lo))));
17017 Analyze_And_Resolve (Lo);
17018 end if;
17020 if Nkind (Hi) = N_String_Literal then
17021 Rewrite (Hi,
17022 Make_Attribute_Reference (Sloc (Hi),
17023 Attribute_Name => Name_First,
17024 Prefix => New_Reference_To (T, Sloc (Hi))));
17025 Analyze_And_Resolve (Hi);
17026 end if;
17028 -- If bounds aren't scalar at this point then exit, avoiding
17029 -- problems with further processing of the range in this procedure.
17031 if not Is_Scalar_Type (Etype (Lo)) then
17032 return;
17033 end if;
17035 -- Resolve (actually Sem_Eval) has checked that the bounds are in
17036 -- then range of the base type. Here we check whether the bounds
17037 -- are in the range of the subtype itself. Note that if the bounds
17038 -- represent the null range the Constraint_Error exception should
17039 -- not be raised.
17041 -- ??? The following code should be cleaned up as follows
17043 -- 1. The Is_Null_Range (Lo, Hi) test should disappear since it
17044 -- is done in the call to Range_Check (R, T); below
17046 -- 2. The use of R_Check_Off should be investigated and possibly
17047 -- removed, this would clean up things a bit.
17049 if Is_Null_Range (Lo, Hi) then
17050 null;
17052 else
17053 -- Capture values of bounds and generate temporaries for them
17054 -- if needed, before applying checks, since checks may cause
17055 -- duplication of the expression without forcing evaluation.
17057 if Expander_Active then
17058 Force_Evaluation (Lo);
17059 Force_Evaluation (Hi);
17060 end if;
17062 -- We use a flag here instead of suppressing checks on the
17063 -- type because the type we check against isn't necessarily
17064 -- the place where we put the check.
17066 if not R_Check_Off then
17067 R_Checks := Get_Range_Checks (R, T);
17069 -- Look up tree to find an appropriate insertion point.
17070 -- This seems really junk code, and very brittle, couldn't
17071 -- we just use an insert actions call of some kind ???
17073 Type_Decl := Parent (R);
17074 while Present (Type_Decl) and then not
17075 (Nkind_In (Type_Decl, N_Full_Type_Declaration,
17076 N_Subtype_Declaration,
17077 N_Loop_Statement,
17078 N_Task_Type_Declaration)
17079 or else
17080 Nkind_In (Type_Decl, N_Single_Task_Declaration,
17081 N_Protected_Type_Declaration,
17082 N_Single_Protected_Declaration))
17083 loop
17084 Type_Decl := Parent (Type_Decl);
17085 end loop;
17087 -- Why would Type_Decl not be present??? Without this test,
17088 -- short regression tests fail.
17090 if Present (Type_Decl) then
17092 -- Case of loop statement (more comments ???)
17094 if Nkind (Type_Decl) = N_Loop_Statement then
17095 declare
17096 Indic : Node_Id;
17098 begin
17099 Indic := Parent (R);
17100 while Present (Indic)
17101 and then Nkind (Indic) /= N_Subtype_Indication
17102 loop
17103 Indic := Parent (Indic);
17104 end loop;
17106 if Present (Indic) then
17107 Def_Id := Etype (Subtype_Mark (Indic));
17109 Insert_Range_Checks
17110 (R_Checks,
17111 Type_Decl,
17112 Def_Id,
17113 Sloc (Type_Decl),
17115 Do_Before => True);
17116 end if;
17117 end;
17119 -- All other cases (more comments ???)
17121 else
17122 Def_Id := Defining_Identifier (Type_Decl);
17124 if (Ekind (Def_Id) = E_Record_Type
17125 and then Depends_On_Discriminant (R))
17126 or else
17127 (Ekind (Def_Id) = E_Protected_Type
17128 and then Has_Discriminants (Def_Id))
17129 then
17130 Append_Range_Checks
17131 (R_Checks, Check_List, Def_Id, Sloc (Type_Decl), R);
17133 else
17134 Insert_Range_Checks
17135 (R_Checks, Type_Decl, Def_Id, Sloc (Type_Decl), R);
17137 end if;
17138 end if;
17139 end if;
17140 end if;
17141 end if;
17143 elsif Expander_Active then
17144 Get_Index_Bounds (R, Lo, Hi);
17145 Force_Evaluation (Lo);
17146 Force_Evaluation (Hi);
17147 end if;
17148 end Process_Range_Expr_In_Decl;
17150 --------------------------------------
17151 -- Process_Real_Range_Specification --
17152 --------------------------------------
17154 procedure Process_Real_Range_Specification (Def : Node_Id) is
17155 Spec : constant Node_Id := Real_Range_Specification (Def);
17156 Lo : Node_Id;
17157 Hi : Node_Id;
17158 Err : Boolean := False;
17160 procedure Analyze_Bound (N : Node_Id);
17161 -- Analyze and check one bound
17163 -------------------
17164 -- Analyze_Bound --
17165 -------------------
17167 procedure Analyze_Bound (N : Node_Id) is
17168 begin
17169 Analyze_And_Resolve (N, Any_Real);
17171 if not Is_OK_Static_Expression (N) then
17172 Flag_Non_Static_Expr
17173 ("bound in real type definition is not static!", N);
17174 Err := True;
17175 end if;
17176 end Analyze_Bound;
17178 -- Start of processing for Process_Real_Range_Specification
17180 begin
17181 if Present (Spec) then
17182 Lo := Low_Bound (Spec);
17183 Hi := High_Bound (Spec);
17184 Analyze_Bound (Lo);
17185 Analyze_Bound (Hi);
17187 -- If error, clear away junk range specification
17189 if Err then
17190 Set_Real_Range_Specification (Def, Empty);
17191 end if;
17192 end if;
17193 end Process_Real_Range_Specification;
17195 ---------------------
17196 -- Process_Subtype --
17197 ---------------------
17199 function Process_Subtype
17200 (S : Node_Id;
17201 Related_Nod : Node_Id;
17202 Related_Id : Entity_Id := Empty;
17203 Suffix : Character := ' ') return Entity_Id
17205 P : Node_Id;
17206 Def_Id : Entity_Id;
17207 Error_Node : Node_Id;
17208 Full_View_Id : Entity_Id;
17209 Subtype_Mark_Id : Entity_Id;
17211 May_Have_Null_Exclusion : Boolean;
17213 procedure Check_Incomplete (T : Entity_Id);
17214 -- Called to verify that an incomplete type is not used prematurely
17216 ----------------------
17217 -- Check_Incomplete --
17218 ----------------------
17220 procedure Check_Incomplete (T : Entity_Id) is
17221 begin
17222 -- Ada 2005 (AI-412): Incomplete subtypes are legal
17224 if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
17225 and then
17226 not (Ada_Version >= Ada_05
17227 and then
17228 (Nkind (Parent (T)) = N_Subtype_Declaration
17229 or else
17230 (Nkind (Parent (T)) = N_Subtype_Indication
17231 and then Nkind (Parent (Parent (T))) =
17232 N_Subtype_Declaration)))
17233 then
17234 Error_Msg_N ("invalid use of type before its full declaration", T);
17235 end if;
17236 end Check_Incomplete;
17238 -- Start of processing for Process_Subtype
17240 begin
17241 -- Case of no constraints present
17243 if Nkind (S) /= N_Subtype_Indication then
17244 Find_Type (S);
17245 Check_Incomplete (S);
17246 P := Parent (S);
17248 -- Ada 2005 (AI-231): Static check
17250 if Ada_Version >= Ada_05
17251 and then Present (P)
17252 and then Null_Exclusion_Present (P)
17253 and then Nkind (P) /= N_Access_To_Object_Definition
17254 and then not Is_Access_Type (Entity (S))
17255 then
17256 Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
17257 end if;
17259 -- The following is ugly, can't we have a range or even a flag???
17261 May_Have_Null_Exclusion :=
17262 Nkind_In (P, N_Access_Definition,
17263 N_Access_Function_Definition,
17264 N_Access_Procedure_Definition,
17265 N_Access_To_Object_Definition,
17266 N_Allocator,
17267 N_Component_Definition)
17268 or else
17269 Nkind_In (P, N_Derived_Type_Definition,
17270 N_Discriminant_Specification,
17271 N_Formal_Object_Declaration,
17272 N_Object_Declaration,
17273 N_Object_Renaming_Declaration,
17274 N_Parameter_Specification,
17275 N_Subtype_Declaration);
17277 -- Create an Itype that is a duplicate of Entity (S) but with the
17278 -- null-exclusion attribute
17280 if May_Have_Null_Exclusion
17281 and then Is_Access_Type (Entity (S))
17282 and then Null_Exclusion_Present (P)
17284 -- No need to check the case of an access to object definition.
17285 -- It is correct to define double not-null pointers.
17287 -- Example:
17288 -- type Not_Null_Int_Ptr is not null access Integer;
17289 -- type Acc is not null access Not_Null_Int_Ptr;
17291 and then Nkind (P) /= N_Access_To_Object_Definition
17292 then
17293 if Can_Never_Be_Null (Entity (S)) then
17294 case Nkind (Related_Nod) is
17295 when N_Full_Type_Declaration =>
17296 if Nkind (Type_Definition (Related_Nod))
17297 in N_Array_Type_Definition
17298 then
17299 Error_Node :=
17300 Subtype_Indication
17301 (Component_Definition
17302 (Type_Definition (Related_Nod)));
17303 else
17304 Error_Node :=
17305 Subtype_Indication (Type_Definition (Related_Nod));
17306 end if;
17308 when N_Subtype_Declaration =>
17309 Error_Node := Subtype_Indication (Related_Nod);
17311 when N_Object_Declaration =>
17312 Error_Node := Object_Definition (Related_Nod);
17314 when N_Component_Declaration =>
17315 Error_Node :=
17316 Subtype_Indication (Component_Definition (Related_Nod));
17318 when N_Allocator =>
17319 Error_Node := Expression (Related_Nod);
17321 when others =>
17322 pragma Assert (False);
17323 Error_Node := Related_Nod;
17324 end case;
17326 Error_Msg_NE
17327 ("`NOT NULL` not allowed (& already excludes null)",
17328 Error_Node,
17329 Entity (S));
17330 end if;
17332 Set_Etype (S,
17333 Create_Null_Excluding_Itype
17334 (T => Entity (S),
17335 Related_Nod => P));
17336 Set_Entity (S, Etype (S));
17337 end if;
17339 return Entity (S);
17341 -- Case of constraint present, so that we have an N_Subtype_Indication
17342 -- node (this node is created only if constraints are present).
17344 else
17345 Find_Type (Subtype_Mark (S));
17347 if Nkind (Parent (S)) /= N_Access_To_Object_Definition
17348 and then not
17349 (Nkind (Parent (S)) = N_Subtype_Declaration
17350 and then Is_Itype (Defining_Identifier (Parent (S))))
17351 then
17352 Check_Incomplete (Subtype_Mark (S));
17353 end if;
17355 P := Parent (S);
17356 Subtype_Mark_Id := Entity (Subtype_Mark (S));
17358 -- Explicit subtype declaration case
17360 if Nkind (P) = N_Subtype_Declaration then
17361 Def_Id := Defining_Identifier (P);
17363 -- Explicit derived type definition case
17365 elsif Nkind (P) = N_Derived_Type_Definition then
17366 Def_Id := Defining_Identifier (Parent (P));
17368 -- Implicit case, the Def_Id must be created as an implicit type.
17369 -- The one exception arises in the case of concurrent types, array
17370 -- and access types, where other subsidiary implicit types may be
17371 -- created and must appear before the main implicit type. In these
17372 -- cases we leave Def_Id set to Empty as a signal that Create_Itype
17373 -- has not yet been called to create Def_Id.
17375 else
17376 if Is_Array_Type (Subtype_Mark_Id)
17377 or else Is_Concurrent_Type (Subtype_Mark_Id)
17378 or else Is_Access_Type (Subtype_Mark_Id)
17379 then
17380 Def_Id := Empty;
17382 -- For the other cases, we create a new unattached Itype,
17383 -- and set the indication to ensure it gets attached later.
17385 else
17386 Def_Id :=
17387 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
17388 end if;
17389 end if;
17391 -- If the kind of constraint is invalid for this kind of type,
17392 -- then give an error, and then pretend no constraint was given.
17394 if not Is_Valid_Constraint_Kind
17395 (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
17396 then
17397 Error_Msg_N
17398 ("incorrect constraint for this kind of type", Constraint (S));
17400 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
17402 -- Set Ekind of orphan itype, to prevent cascaded errors
17404 if Present (Def_Id) then
17405 Set_Ekind (Def_Id, Ekind (Any_Type));
17406 end if;
17408 -- Make recursive call, having got rid of the bogus constraint
17410 return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
17411 end if;
17413 -- Remaining processing depends on type
17415 case Ekind (Subtype_Mark_Id) is
17416 when Access_Kind =>
17417 Constrain_Access (Def_Id, S, Related_Nod);
17419 if Expander_Active
17420 and then Is_Itype (Designated_Type (Def_Id))
17421 and then Nkind (Related_Nod) = N_Subtype_Declaration
17422 and then not Is_Incomplete_Type (Designated_Type (Def_Id))
17423 then
17424 Build_Itype_Reference
17425 (Designated_Type (Def_Id), Related_Nod);
17426 end if;
17428 when Array_Kind =>
17429 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
17431 when Decimal_Fixed_Point_Kind =>
17432 Constrain_Decimal (Def_Id, S);
17434 when Enumeration_Kind =>
17435 Constrain_Enumeration (Def_Id, S);
17437 when Ordinary_Fixed_Point_Kind =>
17438 Constrain_Ordinary_Fixed (Def_Id, S);
17440 when Float_Kind =>
17441 Constrain_Float (Def_Id, S);
17443 when Integer_Kind =>
17444 Constrain_Integer (Def_Id, S);
17446 when E_Record_Type |
17447 E_Record_Subtype |
17448 Class_Wide_Kind |
17449 E_Incomplete_Type =>
17450 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
17452 if Ekind (Def_Id) = E_Incomplete_Type then
17453 Set_Private_Dependents (Def_Id, New_Elmt_List);
17454 end if;
17456 when Private_Kind =>
17457 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
17458 Set_Private_Dependents (Def_Id, New_Elmt_List);
17460 -- In case of an invalid constraint prevent further processing
17461 -- since the type constructed is missing expected fields.
17463 if Etype (Def_Id) = Any_Type then
17464 return Def_Id;
17465 end if;
17467 -- If the full view is that of a task with discriminants,
17468 -- we must constrain both the concurrent type and its
17469 -- corresponding record type. Otherwise we will just propagate
17470 -- the constraint to the full view, if available.
17472 if Present (Full_View (Subtype_Mark_Id))
17473 and then Has_Discriminants (Subtype_Mark_Id)
17474 and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
17475 then
17476 Full_View_Id :=
17477 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
17479 Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
17480 Constrain_Concurrent (Full_View_Id, S,
17481 Related_Nod, Related_Id, Suffix);
17482 Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
17483 Set_Full_View (Def_Id, Full_View_Id);
17485 -- Introduce an explicit reference to the private subtype,
17486 -- to prevent scope anomalies in gigi if first use appears
17487 -- in a nested context, e.g. a later function body.
17488 -- Should this be generated in other contexts than a full
17489 -- type declaration?
17491 if Is_Itype (Def_Id)
17492 and then
17493 Nkind (Parent (P)) = N_Full_Type_Declaration
17494 then
17495 Build_Itype_Reference (Def_Id, Parent (P));
17496 end if;
17498 else
17499 Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
17500 end if;
17502 when Concurrent_Kind =>
17503 Constrain_Concurrent (Def_Id, S,
17504 Related_Nod, Related_Id, Suffix);
17506 when others =>
17507 Error_Msg_N ("invalid subtype mark in subtype indication", S);
17508 end case;
17510 -- Size and Convention are always inherited from the base type
17512 Set_Size_Info (Def_Id, (Subtype_Mark_Id));
17513 Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
17515 return Def_Id;
17516 end if;
17517 end Process_Subtype;
17519 ---------------------------------------
17520 -- Check_Anonymous_Access_Components --
17521 ---------------------------------------
17523 procedure Check_Anonymous_Access_Components
17524 (Typ_Decl : Node_Id;
17525 Typ : Entity_Id;
17526 Prev : Entity_Id;
17527 Comp_List : Node_Id)
17529 Loc : constant Source_Ptr := Sloc (Typ_Decl);
17530 Anon_Access : Entity_Id;
17531 Acc_Def : Node_Id;
17532 Comp : Node_Id;
17533 Comp_Def : Node_Id;
17534 Decl : Node_Id;
17535 Type_Def : Node_Id;
17537 procedure Build_Incomplete_Type_Declaration;
17538 -- If the record type contains components that include an access to the
17539 -- current record, then create an incomplete type declaration for the
17540 -- record, to be used as the designated type of the anonymous access.
17541 -- This is done only once, and only if there is no previous partial
17542 -- view of the type.
17544 function Designates_T (Subt : Node_Id) return Boolean;
17545 -- Check whether a node designates the enclosing record type, or 'Class
17546 -- of that type
17548 function Mentions_T (Acc_Def : Node_Id) return Boolean;
17549 -- Check whether an access definition includes a reference to
17550 -- the enclosing record type. The reference can be a subtype mark
17551 -- in the access definition itself, a 'Class attribute reference, or
17552 -- recursively a reference appearing in a parameter specification
17553 -- or result definition of an access_to_subprogram definition.
17555 --------------------------------------
17556 -- Build_Incomplete_Type_Declaration --
17557 --------------------------------------
17559 procedure Build_Incomplete_Type_Declaration is
17560 Decl : Node_Id;
17561 Inc_T : Entity_Id;
17562 H : Entity_Id;
17564 -- Is_Tagged indicates whether the type is tagged. It is tagged if
17565 -- it's "is new ... with record" or else "is tagged record ...".
17567 Is_Tagged : constant Boolean :=
17568 (Nkind (Type_Definition (Typ_Decl)) = N_Derived_Type_Definition
17569 and then
17570 Present
17571 (Record_Extension_Part (Type_Definition (Typ_Decl))))
17572 or else
17573 (Nkind (Type_Definition (Typ_Decl)) = N_Record_Definition
17574 and then Tagged_Present (Type_Definition (Typ_Decl)));
17576 begin
17577 -- If there is a previous partial view, no need to create a new one
17578 -- If the partial view, given by Prev, is incomplete, If Prev is
17579 -- a private declaration, full declaration is flagged accordingly.
17581 if Prev /= Typ then
17582 if Is_Tagged then
17583 Make_Class_Wide_Type (Prev);
17584 Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
17585 Set_Etype (Class_Wide_Type (Typ), Typ);
17586 end if;
17588 return;
17590 elsif Has_Private_Declaration (Typ) then
17592 -- If we refer to T'Class inside T, and T is the completion of a
17593 -- private type, then we need to make sure the class-wide type
17594 -- exists.
17596 if Is_Tagged then
17597 Make_Class_Wide_Type (Typ);
17598 end if;
17600 return;
17602 -- If there was a previous anonymous access type, the incomplete
17603 -- type declaration will have been created already.
17605 elsif Present (Current_Entity (Typ))
17606 and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
17607 and then Full_View (Current_Entity (Typ)) = Typ
17608 then
17609 if Is_Tagged
17610 and then Comes_From_Source (Current_Entity (Typ))
17611 and then not Is_Tagged_Type (Current_Entity (Typ))
17612 then
17613 Make_Class_Wide_Type (Typ);
17614 Error_Msg_N
17615 ("incomplete view of tagged type should be declared tagged?",
17616 Parent (Current_Entity (Typ)));
17617 end if;
17618 return;
17620 else
17621 Inc_T := Make_Defining_Identifier (Loc, Chars (Typ));
17622 Decl := Make_Incomplete_Type_Declaration (Loc, Inc_T);
17624 -- Type has already been inserted into the current scope. Remove
17625 -- it, and add incomplete declaration for type, so that subsequent
17626 -- anonymous access types can use it. The entity is unchained from
17627 -- the homonym list and from immediate visibility. After analysis,
17628 -- the entity in the incomplete declaration becomes immediately
17629 -- visible in the record declaration that follows.
17631 H := Current_Entity (Typ);
17633 if H = Typ then
17634 Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
17635 else
17636 while Present (H)
17637 and then Homonym (H) /= Typ
17638 loop
17639 H := Homonym (Typ);
17640 end loop;
17642 Set_Homonym (H, Homonym (Typ));
17643 end if;
17645 Insert_Before (Typ_Decl, Decl);
17646 Analyze (Decl);
17647 Set_Full_View (Inc_T, Typ);
17649 if Is_Tagged then
17651 -- Create a common class-wide type for both views, and set the
17652 -- Etype of the class-wide type to the full view.
17654 Make_Class_Wide_Type (Inc_T);
17655 Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
17656 Set_Etype (Class_Wide_Type (Typ), Typ);
17657 end if;
17658 end if;
17659 end Build_Incomplete_Type_Declaration;
17661 ------------------
17662 -- Designates_T --
17663 ------------------
17665 function Designates_T (Subt : Node_Id) return Boolean is
17666 Type_Id : constant Name_Id := Chars (Typ);
17668 function Names_T (Nam : Node_Id) return Boolean;
17669 -- The record type has not been introduced in the current scope
17670 -- yet, so we must examine the name of the type itself, either
17671 -- an identifier T, or an expanded name of the form P.T, where
17672 -- P denotes the current scope.
17674 -------------
17675 -- Names_T --
17676 -------------
17678 function Names_T (Nam : Node_Id) return Boolean is
17679 begin
17680 if Nkind (Nam) = N_Identifier then
17681 return Chars (Nam) = Type_Id;
17683 elsif Nkind (Nam) = N_Selected_Component then
17684 if Chars (Selector_Name (Nam)) = Type_Id then
17685 if Nkind (Prefix (Nam)) = N_Identifier then
17686 return Chars (Prefix (Nam)) = Chars (Current_Scope);
17688 elsif Nkind (Prefix (Nam)) = N_Selected_Component then
17689 return Chars (Selector_Name (Prefix (Nam))) =
17690 Chars (Current_Scope);
17691 else
17692 return False;
17693 end if;
17695 else
17696 return False;
17697 end if;
17699 else
17700 return False;
17701 end if;
17702 end Names_T;
17704 -- Start of processing for Designates_T
17706 begin
17707 if Nkind (Subt) = N_Identifier then
17708 return Chars (Subt) = Type_Id;
17710 -- Reference can be through an expanded name which has not been
17711 -- analyzed yet, and which designates enclosing scopes.
17713 elsif Nkind (Subt) = N_Selected_Component then
17714 if Names_T (Subt) then
17715 return True;
17717 -- Otherwise it must denote an entity that is already visible.
17718 -- The access definition may name a subtype of the enclosing
17719 -- type, if there is a previous incomplete declaration for it.
17721 else
17722 Find_Selected_Component (Subt);
17723 return
17724 Is_Entity_Name (Subt)
17725 and then Scope (Entity (Subt)) = Current_Scope
17726 and then
17727 (Chars (Base_Type (Entity (Subt))) = Type_Id
17728 or else
17729 (Is_Class_Wide_Type (Entity (Subt))
17730 and then
17731 Chars (Etype (Base_Type (Entity (Subt)))) =
17732 Type_Id));
17733 end if;
17735 -- A reference to the current type may appear as the prefix of
17736 -- a 'Class attribute.
17738 elsif Nkind (Subt) = N_Attribute_Reference
17739 and then Attribute_Name (Subt) = Name_Class
17740 then
17741 return Names_T (Prefix (Subt));
17743 else
17744 return False;
17745 end if;
17746 end Designates_T;
17748 ----------------
17749 -- Mentions_T --
17750 ----------------
17752 function Mentions_T (Acc_Def : Node_Id) return Boolean is
17753 Param_Spec : Node_Id;
17755 Acc_Subprg : constant Node_Id :=
17756 Access_To_Subprogram_Definition (Acc_Def);
17758 begin
17759 if No (Acc_Subprg) then
17760 return Designates_T (Subtype_Mark (Acc_Def));
17761 end if;
17763 -- Component is an access_to_subprogram: examine its formals,
17764 -- and result definition in the case of an access_to_function.
17766 Param_Spec := First (Parameter_Specifications (Acc_Subprg));
17767 while Present (Param_Spec) loop
17768 if Nkind (Parameter_Type (Param_Spec)) = N_Access_Definition
17769 and then Mentions_T (Parameter_Type (Param_Spec))
17770 then
17771 return True;
17773 elsif Designates_T (Parameter_Type (Param_Spec)) then
17774 return True;
17775 end if;
17777 Next (Param_Spec);
17778 end loop;
17780 if Nkind (Acc_Subprg) = N_Access_Function_Definition then
17781 if Nkind (Result_Definition (Acc_Subprg)) =
17782 N_Access_Definition
17783 then
17784 return Mentions_T (Result_Definition (Acc_Subprg));
17785 else
17786 return Designates_T (Result_Definition (Acc_Subprg));
17787 end if;
17788 end if;
17790 return False;
17791 end Mentions_T;
17793 -- Start of processing for Check_Anonymous_Access_Components
17795 begin
17796 if No (Comp_List) then
17797 return;
17798 end if;
17800 Comp := First (Component_Items (Comp_List));
17801 while Present (Comp) loop
17802 if Nkind (Comp) = N_Component_Declaration
17803 and then Present
17804 (Access_Definition (Component_Definition (Comp)))
17805 and then
17806 Mentions_T (Access_Definition (Component_Definition (Comp)))
17807 then
17808 Comp_Def := Component_Definition (Comp);
17809 Acc_Def :=
17810 Access_To_Subprogram_Definition
17811 (Access_Definition (Comp_Def));
17813 Build_Incomplete_Type_Declaration;
17814 Anon_Access := Make_Temporary (Loc, 'S');
17816 -- Create a declaration for the anonymous access type: either
17817 -- an access_to_object or an access_to_subprogram.
17819 if Present (Acc_Def) then
17820 if Nkind (Acc_Def) = N_Access_Function_Definition then
17821 Type_Def :=
17822 Make_Access_Function_Definition (Loc,
17823 Parameter_Specifications =>
17824 Parameter_Specifications (Acc_Def),
17825 Result_Definition => Result_Definition (Acc_Def));
17826 else
17827 Type_Def :=
17828 Make_Access_Procedure_Definition (Loc,
17829 Parameter_Specifications =>
17830 Parameter_Specifications (Acc_Def));
17831 end if;
17833 else
17834 Type_Def :=
17835 Make_Access_To_Object_Definition (Loc,
17836 Subtype_Indication =>
17837 Relocate_Node
17838 (Subtype_Mark
17839 (Access_Definition (Comp_Def))));
17841 Set_Constant_Present
17842 (Type_Def, Constant_Present (Access_Definition (Comp_Def)));
17843 Set_All_Present
17844 (Type_Def, All_Present (Access_Definition (Comp_Def)));
17845 end if;
17847 Set_Null_Exclusion_Present
17848 (Type_Def,
17849 Null_Exclusion_Present (Access_Definition (Comp_Def)));
17851 Decl :=
17852 Make_Full_Type_Declaration (Loc,
17853 Defining_Identifier => Anon_Access,
17854 Type_Definition => Type_Def);
17856 Insert_Before (Typ_Decl, Decl);
17857 Analyze (Decl);
17859 -- If an access to object, Preserve entity of designated type,
17860 -- for ASIS use, before rewriting the component definition.
17862 if No (Acc_Def) then
17863 declare
17864 Desig : Entity_Id;
17866 begin
17867 Desig := Entity (Subtype_Indication (Type_Def));
17869 -- If the access definition is to the current record,
17870 -- the visible entity at this point is an incomplete
17871 -- type. Retrieve the full view to simplify ASIS queries
17873 if Ekind (Desig) = E_Incomplete_Type then
17874 Desig := Full_View (Desig);
17875 end if;
17877 Set_Entity
17878 (Subtype_Mark (Access_Definition (Comp_Def)), Desig);
17879 end;
17880 end if;
17882 Rewrite (Comp_Def,
17883 Make_Component_Definition (Loc,
17884 Subtype_Indication =>
17885 New_Occurrence_Of (Anon_Access, Loc)));
17887 if Ekind (Designated_Type (Anon_Access)) = E_Subprogram_Type then
17888 Set_Ekind (Anon_Access, E_Anonymous_Access_Subprogram_Type);
17889 else
17890 Set_Ekind (Anon_Access, E_Anonymous_Access_Type);
17891 end if;
17893 Set_Is_Local_Anonymous_Access (Anon_Access);
17894 end if;
17896 Next (Comp);
17897 end loop;
17899 if Present (Variant_Part (Comp_List)) then
17900 declare
17901 V : Node_Id;
17902 begin
17903 V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
17904 while Present (V) loop
17905 Check_Anonymous_Access_Components
17906 (Typ_Decl, Typ, Prev, Component_List (V));
17907 Next_Non_Pragma (V);
17908 end loop;
17909 end;
17910 end if;
17911 end Check_Anonymous_Access_Components;
17913 --------------------------------
17914 -- Preanalyze_Spec_Expression --
17915 --------------------------------
17917 procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
17918 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
17919 begin
17920 In_Spec_Expression := True;
17921 Preanalyze_And_Resolve (N, T);
17922 In_Spec_Expression := Save_In_Spec_Expression;
17923 end Preanalyze_Spec_Expression;
17925 -----------------------------
17926 -- Record_Type_Declaration --
17927 -----------------------------
17929 procedure Record_Type_Declaration
17930 (T : Entity_Id;
17931 N : Node_Id;
17932 Prev : Entity_Id)
17934 Def : constant Node_Id := Type_Definition (N);
17935 Is_Tagged : Boolean;
17936 Tag_Comp : Entity_Id;
17938 begin
17939 -- These flags must be initialized before calling Process_Discriminants
17940 -- because this routine makes use of them.
17942 Set_Ekind (T, E_Record_Type);
17943 Set_Etype (T, T);
17944 Init_Size_Align (T);
17945 Set_Interfaces (T, No_Elist);
17946 Set_Stored_Constraint (T, No_Elist);
17948 -- Normal case
17950 if Ada_Version < Ada_05
17951 or else not Interface_Present (Def)
17952 then
17953 -- The flag Is_Tagged_Type might have already been set by
17954 -- Find_Type_Name if it detected an error for declaration T. This
17955 -- arises in the case of private tagged types where the full view
17956 -- omits the word tagged.
17958 Is_Tagged :=
17959 Tagged_Present (Def)
17960 or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
17962 Set_Is_Tagged_Type (T, Is_Tagged);
17963 Set_Is_Limited_Record (T, Limited_Present (Def));
17965 -- Type is abstract if full declaration carries keyword, or if
17966 -- previous partial view did.
17968 Set_Is_Abstract_Type (T, Is_Abstract_Type (T)
17969 or else Abstract_Present (Def));
17971 else
17972 Is_Tagged := True;
17973 Analyze_Interface_Declaration (T, Def);
17975 if Present (Discriminant_Specifications (N)) then
17976 Error_Msg_N
17977 ("interface types cannot have discriminants",
17978 Defining_Identifier
17979 (First (Discriminant_Specifications (N))));
17980 end if;
17981 end if;
17983 -- First pass: if there are self-referential access components,
17984 -- create the required anonymous access type declarations, and if
17985 -- need be an incomplete type declaration for T itself.
17987 Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
17989 if Ada_Version >= Ada_05
17990 and then Present (Interface_List (Def))
17991 then
17992 Check_Interfaces (N, Def);
17994 declare
17995 Ifaces_List : Elist_Id;
17997 begin
17998 -- Ada 2005 (AI-251): Collect the list of progenitors that are not
17999 -- already in the parents.
18001 Collect_Interfaces
18002 (T => T,
18003 Ifaces_List => Ifaces_List,
18004 Exclude_Parents => True);
18006 Set_Interfaces (T, Ifaces_List);
18007 end;
18008 end if;
18010 -- Records constitute a scope for the component declarations within.
18011 -- The scope is created prior to the processing of these declarations.
18012 -- Discriminants are processed first, so that they are visible when
18013 -- processing the other components. The Ekind of the record type itself
18014 -- is set to E_Record_Type (subtypes appear as E_Record_Subtype).
18016 -- Enter record scope
18018 Push_Scope (T);
18020 -- If an incomplete or private type declaration was already given for
18021 -- the type, then this scope already exists, and the discriminants have
18022 -- been declared within. We must verify that the full declaration
18023 -- matches the incomplete one.
18025 Check_Or_Process_Discriminants (N, T, Prev);
18027 Set_Is_Constrained (T, not Has_Discriminants (T));
18028 Set_Has_Delayed_Freeze (T, True);
18030 -- For tagged types add a manually analyzed component corresponding
18031 -- to the component _tag, the corresponding piece of tree will be
18032 -- expanded as part of the freezing actions if it is not a CPP_Class.
18034 if Is_Tagged then
18036 -- Do not add the tag unless we are in expansion mode
18038 if Expander_Active then
18039 Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
18040 Enter_Name (Tag_Comp);
18042 Set_Ekind (Tag_Comp, E_Component);
18043 Set_Is_Tag (Tag_Comp);
18044 Set_Is_Aliased (Tag_Comp);
18045 Set_Etype (Tag_Comp, RTE (RE_Tag));
18046 Set_DT_Entry_Count (Tag_Comp, No_Uint);
18047 Set_Original_Record_Component (Tag_Comp, Tag_Comp);
18048 Init_Component_Location (Tag_Comp);
18050 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
18051 -- implemented interfaces.
18053 if Has_Interfaces (T) then
18054 Add_Interface_Tag_Components (N, T);
18055 end if;
18056 end if;
18058 Make_Class_Wide_Type (T);
18059 Set_Primitive_Operations (T, New_Elmt_List);
18060 end if;
18062 -- We must suppress range checks when processing the components
18063 -- of a record in the presence of discriminants, since we don't
18064 -- want spurious checks to be generated during their analysis, but
18065 -- must reset the Suppress_Range_Checks flags after having processed
18066 -- the record definition.
18068 -- Note: this is the only use of Kill_Range_Checks, and is a bit odd,
18069 -- couldn't we just use the normal range check suppression method here.
18070 -- That would seem cleaner ???
18072 if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
18073 Set_Kill_Range_Checks (T, True);
18074 Record_Type_Definition (Def, Prev);
18075 Set_Kill_Range_Checks (T, False);
18076 else
18077 Record_Type_Definition (Def, Prev);
18078 end if;
18080 -- Exit from record scope
18082 End_Scope;
18084 -- Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
18085 -- the implemented interfaces and associate them an aliased entity.
18087 if Is_Tagged
18088 and then not Is_Empty_List (Interface_List (Def))
18089 then
18090 Derive_Progenitor_Subprograms (T, T);
18091 end if;
18092 end Record_Type_Declaration;
18094 ----------------------------
18095 -- Record_Type_Definition --
18096 ----------------------------
18098 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
18099 Component : Entity_Id;
18100 Ctrl_Components : Boolean := False;
18101 Final_Storage_Only : Boolean;
18102 T : Entity_Id;
18104 begin
18105 if Ekind (Prev_T) = E_Incomplete_Type then
18106 T := Full_View (Prev_T);
18107 else
18108 T := Prev_T;
18109 end if;
18111 Final_Storage_Only := not Is_Controlled (T);
18113 -- Ada 2005: check whether an explicit Limited is present in a derived
18114 -- type declaration.
18116 if Nkind (Parent (Def)) = N_Derived_Type_Definition
18117 and then Limited_Present (Parent (Def))
18118 then
18119 Set_Is_Limited_Record (T);
18120 end if;
18122 -- If the component list of a record type is defined by the reserved
18123 -- word null and there is no discriminant part, then the record type has
18124 -- no components and all records of the type are null records (RM 3.7)
18125 -- This procedure is also called to process the extension part of a
18126 -- record extension, in which case the current scope may have inherited
18127 -- components.
18129 if No (Def)
18130 or else No (Component_List (Def))
18131 or else Null_Present (Component_List (Def))
18132 then
18133 null;
18135 else
18136 Analyze_Declarations (Component_Items (Component_List (Def)));
18138 if Present (Variant_Part (Component_List (Def))) then
18139 Analyze (Variant_Part (Component_List (Def)));
18140 end if;
18141 end if;
18143 -- After completing the semantic analysis of the record definition,
18144 -- record components, both new and inherited, are accessible. Set their
18145 -- kind accordingly. Exclude malformed itypes from illegal declarations,
18146 -- whose Ekind may be void.
18148 Component := First_Entity (Current_Scope);
18149 while Present (Component) loop
18150 if Ekind (Component) = E_Void
18151 and then not Is_Itype (Component)
18152 then
18153 Set_Ekind (Component, E_Component);
18154 Init_Component_Location (Component);
18155 end if;
18157 if Has_Task (Etype (Component)) then
18158 Set_Has_Task (T);
18159 end if;
18161 if Ekind (Component) /= E_Component then
18162 null;
18164 -- Do not set Has_Controlled_Component on a class-wide equivalent
18165 -- type. See Make_CW_Equivalent_Type.
18167 elsif not Is_Class_Wide_Equivalent_Type (T)
18168 and then (Has_Controlled_Component (Etype (Component))
18169 or else (Chars (Component) /= Name_uParent
18170 and then Is_Controlled (Etype (Component))))
18171 then
18172 Set_Has_Controlled_Component (T, True);
18173 Final_Storage_Only :=
18174 Final_Storage_Only
18175 and then Finalize_Storage_Only (Etype (Component));
18176 Ctrl_Components := True;
18177 end if;
18179 Next_Entity (Component);
18180 end loop;
18182 -- A Type is Finalize_Storage_Only only if all its controlled components
18183 -- are also.
18185 if Ctrl_Components then
18186 Set_Finalize_Storage_Only (T, Final_Storage_Only);
18187 end if;
18189 -- Place reference to end record on the proper entity, which may
18190 -- be a partial view.
18192 if Present (Def) then
18193 Process_End_Label (Def, 'e', Prev_T);
18194 end if;
18195 end Record_Type_Definition;
18197 ------------------------
18198 -- Replace_Components --
18199 ------------------------
18201 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id) is
18202 function Process (N : Node_Id) return Traverse_Result;
18204 -------------
18205 -- Process --
18206 -------------
18208 function Process (N : Node_Id) return Traverse_Result is
18209 Comp : Entity_Id;
18211 begin
18212 if Nkind (N) = N_Discriminant_Specification then
18213 Comp := First_Discriminant (Typ);
18214 while Present (Comp) loop
18215 if Chars (Comp) = Chars (Defining_Identifier (N)) then
18216 Set_Defining_Identifier (N, Comp);
18217 exit;
18218 end if;
18220 Next_Discriminant (Comp);
18221 end loop;
18223 elsif Nkind (N) = N_Component_Declaration then
18224 Comp := First_Component (Typ);
18225 while Present (Comp) loop
18226 if Chars (Comp) = Chars (Defining_Identifier (N)) then
18227 Set_Defining_Identifier (N, Comp);
18228 exit;
18229 end if;
18231 Next_Component (Comp);
18232 end loop;
18233 end if;
18235 return OK;
18236 end Process;
18238 procedure Replace is new Traverse_Proc (Process);
18240 -- Start of processing for Replace_Components
18242 begin
18243 Replace (Decl);
18244 end Replace_Components;
18246 -------------------------------
18247 -- Set_Completion_Referenced --
18248 -------------------------------
18250 procedure Set_Completion_Referenced (E : Entity_Id) is
18251 begin
18252 -- If in main unit, mark entity that is a completion as referenced,
18253 -- warnings go on the partial view when needed.
18255 if In_Extended_Main_Source_Unit (E) then
18256 Set_Referenced (E);
18257 end if;
18258 end Set_Completion_Referenced;
18260 ---------------------
18261 -- Set_Fixed_Range --
18262 ---------------------
18264 -- The range for fixed-point types is complicated by the fact that we
18265 -- do not know the exact end points at the time of the declaration. This
18266 -- is true for three reasons:
18268 -- A size clause may affect the fudging of the end-points
18269 -- A small clause may affect the values of the end-points
18270 -- We try to include the end-points if it does not affect the size
18272 -- This means that the actual end-points must be established at the point
18273 -- when the type is frozen. Meanwhile, we first narrow the range as
18274 -- permitted (so that it will fit if necessary in a small specified size),
18275 -- and then build a range subtree with these narrowed bounds.
18277 -- Set_Fixed_Range constructs the range from real literal values, and sets
18278 -- the range as the Scalar_Range of the given fixed-point type entity.
18280 -- The parent of this range is set to point to the entity so that it is
18281 -- properly hooked into the tree (unlike normal Scalar_Range entries for
18282 -- other scalar types, which are just pointers to the range in the
18283 -- original tree, this would otherwise be an orphan).
18285 -- The tree is left unanalyzed. When the type is frozen, the processing
18286 -- in Freeze.Freeze_Fixed_Point_Type notices that the range is not
18287 -- analyzed, and uses this as an indication that it should complete
18288 -- work on the range (it will know the final small and size values).
18290 procedure Set_Fixed_Range
18291 (E : Entity_Id;
18292 Loc : Source_Ptr;
18293 Lo : Ureal;
18294 Hi : Ureal)
18296 S : constant Node_Id :=
18297 Make_Range (Loc,
18298 Low_Bound => Make_Real_Literal (Loc, Lo),
18299 High_Bound => Make_Real_Literal (Loc, Hi));
18300 begin
18301 Set_Scalar_Range (E, S);
18302 Set_Parent (S, E);
18303 end Set_Fixed_Range;
18305 ----------------------------------
18306 -- Set_Scalar_Range_For_Subtype --
18307 ----------------------------------
18309 procedure Set_Scalar_Range_For_Subtype
18310 (Def_Id : Entity_Id;
18311 R : Node_Id;
18312 Subt : Entity_Id)
18314 Kind : constant Entity_Kind := Ekind (Def_Id);
18316 begin
18317 Set_Scalar_Range (Def_Id, R);
18319 -- We need to link the range into the tree before resolving it so
18320 -- that types that are referenced, including importantly the subtype
18321 -- itself, are properly frozen (Freeze_Expression requires that the
18322 -- expression be properly linked into the tree). Of course if it is
18323 -- already linked in, then we do not disturb the current link.
18325 if No (Parent (R)) then
18326 Set_Parent (R, Def_Id);
18327 end if;
18329 -- Reset the kind of the subtype during analysis of the range, to
18330 -- catch possible premature use in the bounds themselves.
18332 Set_Ekind (Def_Id, E_Void);
18333 Process_Range_Expr_In_Decl (R, Subt);
18334 Set_Ekind (Def_Id, Kind);
18335 end Set_Scalar_Range_For_Subtype;
18337 --------------------------------------------------------
18338 -- Set_Stored_Constraint_From_Discriminant_Constraint --
18339 --------------------------------------------------------
18341 procedure Set_Stored_Constraint_From_Discriminant_Constraint
18342 (E : Entity_Id)
18344 begin
18345 -- Make sure set if encountered during Expand_To_Stored_Constraint
18347 Set_Stored_Constraint (E, No_Elist);
18349 -- Give it the right value
18351 if Is_Constrained (E) and then Has_Discriminants (E) then
18352 Set_Stored_Constraint (E,
18353 Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
18354 end if;
18355 end Set_Stored_Constraint_From_Discriminant_Constraint;
18357 -------------------------------------
18358 -- Signed_Integer_Type_Declaration --
18359 -------------------------------------
18361 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
18362 Implicit_Base : Entity_Id;
18363 Base_Typ : Entity_Id;
18364 Lo_Val : Uint;
18365 Hi_Val : Uint;
18366 Errs : Boolean := False;
18367 Lo : Node_Id;
18368 Hi : Node_Id;
18370 function Can_Derive_From (E : Entity_Id) return Boolean;
18371 -- Determine whether given bounds allow derivation from specified type
18373 procedure Check_Bound (Expr : Node_Id);
18374 -- Check bound to make sure it is integral and static. If not, post
18375 -- appropriate error message and set Errs flag
18377 ---------------------
18378 -- Can_Derive_From --
18379 ---------------------
18381 -- Note we check both bounds against both end values, to deal with
18382 -- strange types like ones with a range of 0 .. -12341234.
18384 function Can_Derive_From (E : Entity_Id) return Boolean is
18385 Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
18386 Hi : constant Uint := Expr_Value (Type_High_Bound (E));
18387 begin
18388 return Lo <= Lo_Val and then Lo_Val <= Hi
18389 and then
18390 Lo <= Hi_Val and then Hi_Val <= Hi;
18391 end Can_Derive_From;
18393 -----------------
18394 -- Check_Bound --
18395 -----------------
18397 procedure Check_Bound (Expr : Node_Id) is
18398 begin
18399 -- If a range constraint is used as an integer type definition, each
18400 -- bound of the range must be defined by a static expression of some
18401 -- integer type, but the two bounds need not have the same integer
18402 -- type (Negative bounds are allowed.) (RM 3.5.4)
18404 if not Is_Integer_Type (Etype (Expr)) then
18405 Error_Msg_N
18406 ("integer type definition bounds must be of integer type", Expr);
18407 Errs := True;
18409 elsif not Is_OK_Static_Expression (Expr) then
18410 Flag_Non_Static_Expr
18411 ("non-static expression used for integer type bound!", Expr);
18412 Errs := True;
18414 -- The bounds are folded into literals, and we set their type to be
18415 -- universal, to avoid typing difficulties: we cannot set the type
18416 -- of the literal to the new type, because this would be a forward
18417 -- reference for the back end, and if the original type is user-
18418 -- defined this can lead to spurious semantic errors (e.g. 2928-003).
18420 else
18421 if Is_Entity_Name (Expr) then
18422 Fold_Uint (Expr, Expr_Value (Expr), True);
18423 end if;
18425 Set_Etype (Expr, Universal_Integer);
18426 end if;
18427 end Check_Bound;
18429 -- Start of processing for Signed_Integer_Type_Declaration
18431 begin
18432 -- Create an anonymous base type
18434 Implicit_Base :=
18435 Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
18437 -- Analyze and check the bounds, they can be of any integer type
18439 Lo := Low_Bound (Def);
18440 Hi := High_Bound (Def);
18442 -- Arbitrarily use Integer as the type if either bound had an error
18444 if Hi = Error or else Lo = Error then
18445 Base_Typ := Any_Integer;
18446 Set_Error_Posted (T, True);
18448 -- Here both bounds are OK expressions
18450 else
18451 Analyze_And_Resolve (Lo, Any_Integer);
18452 Analyze_And_Resolve (Hi, Any_Integer);
18454 Check_Bound (Lo);
18455 Check_Bound (Hi);
18457 if Errs then
18458 Hi := Type_High_Bound (Standard_Long_Long_Integer);
18459 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
18460 end if;
18462 -- Find type to derive from
18464 Lo_Val := Expr_Value (Lo);
18465 Hi_Val := Expr_Value (Hi);
18467 if Can_Derive_From (Standard_Short_Short_Integer) then
18468 Base_Typ := Base_Type (Standard_Short_Short_Integer);
18470 elsif Can_Derive_From (Standard_Short_Integer) then
18471 Base_Typ := Base_Type (Standard_Short_Integer);
18473 elsif Can_Derive_From (Standard_Integer) then
18474 Base_Typ := Base_Type (Standard_Integer);
18476 elsif Can_Derive_From (Standard_Long_Integer) then
18477 Base_Typ := Base_Type (Standard_Long_Integer);
18479 elsif Can_Derive_From (Standard_Long_Long_Integer) then
18480 Base_Typ := Base_Type (Standard_Long_Long_Integer);
18482 else
18483 Base_Typ := Base_Type (Standard_Long_Long_Integer);
18484 Error_Msg_N ("integer type definition bounds out of range", Def);
18485 Hi := Type_High_Bound (Standard_Long_Long_Integer);
18486 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
18487 end if;
18488 end if;
18490 -- Complete both implicit base and declared first subtype entities
18492 Set_Etype (Implicit_Base, Base_Typ);
18493 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
18494 Set_Size_Info (Implicit_Base, (Base_Typ));
18495 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
18496 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
18498 Set_Ekind (T, E_Signed_Integer_Subtype);
18499 Set_Etype (T, Implicit_Base);
18501 Set_Size_Info (T, (Implicit_Base));
18502 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
18503 Set_Scalar_Range (T, Def);
18504 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
18505 Set_Is_Constrained (T);
18506 end Signed_Integer_Type_Declaration;
18508 end Sem_Ch3;