2008-05-30 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / ada / sem_ch3.adb
blobf0065b739013462e76e47d8b297697b5bb3a5930
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-2008, 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_Disp; use Exp_Disp;
35 with Exp_Dist; use Exp_Dist;
36 with Exp_Tss; use Exp_Tss;
37 with Exp_Util; use Exp_Util;
38 with Fname; use Fname;
39 with Freeze; use Freeze;
40 with Itypes; use Itypes;
41 with Layout; use Layout;
42 with Lib; use Lib;
43 with Lib.Xref; use Lib.Xref;
44 with Namet; use Namet;
45 with Nmake; use Nmake;
46 with Opt; use Opt;
47 with Restrict; use Restrict;
48 with Rident; use Rident;
49 with Rtsfind; use Rtsfind;
50 with Sem; use Sem;
51 with Sem_Case; use Sem_Case;
52 with Sem_Cat; use Sem_Cat;
53 with Sem_Ch6; use Sem_Ch6;
54 with Sem_Ch7; use Sem_Ch7;
55 with Sem_Ch8; use Sem_Ch8;
56 with Sem_Ch13; use Sem_Ch13;
57 with Sem_Disp; use Sem_Disp;
58 with Sem_Dist; use Sem_Dist;
59 with Sem_Elim; use Sem_Elim;
60 with Sem_Eval; use Sem_Eval;
61 with Sem_Mech; use Sem_Mech;
62 with Sem_Res; use Sem_Res;
63 with Sem_Smem; use Sem_Smem;
64 with Sem_Type; use Sem_Type;
65 with Sem_Util; use Sem_Util;
66 with Sem_Warn; use Sem_Warn;
67 with Stand; use Stand;
68 with Sinfo; use Sinfo;
69 with Snames; use Snames;
70 with Targparm; use Targparm;
71 with Tbuild; use Tbuild;
72 with Ttypes; use Ttypes;
73 with Uintp; use Uintp;
74 with Urealp; use Urealp;
76 package body Sem_Ch3 is
78 -----------------------
79 -- Local Subprograms --
80 -----------------------
82 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id);
83 -- Ada 2005 (AI-251): Add the tag components corresponding to all the
84 -- abstract interface types implemented by a record type or a derived
85 -- record type.
87 procedure Build_Derived_Type
88 (N : Node_Id;
89 Parent_Type : Entity_Id;
90 Derived_Type : Entity_Id;
91 Is_Completion : Boolean;
92 Derive_Subps : Boolean := True);
93 -- Create and decorate a Derived_Type given the Parent_Type entity. N is
94 -- the N_Full_Type_Declaration node containing the derived type definition.
95 -- Parent_Type is the entity for the parent type in the derived type
96 -- definition and Derived_Type the actual derived type. Is_Completion must
97 -- be set to False if Derived_Type is the N_Defining_Identifier node in N
98 -- (i.e. Derived_Type = Defining_Identifier (N)). In this case N is not the
99 -- completion of a private type declaration. If Is_Completion is set to
100 -- True, N is the completion of a private type declaration and Derived_Type
101 -- is different from the defining identifier inside N (i.e. Derived_Type /=
102 -- Defining_Identifier (N)). Derive_Subps indicates whether the parent
103 -- subprograms should be derived. The only case where this parameter is
104 -- False is when Build_Derived_Type is recursively called to process an
105 -- implicit derived full type for a type derived from a private type (in
106 -- that case the subprograms must only be derived for the private view of
107 -- the type).
109 -- ??? These flags need a bit of re-examination and re-documentation:
110 -- ??? are they both necessary (both seem related to the recursion)?
112 procedure Build_Derived_Access_Type
113 (N : Node_Id;
114 Parent_Type : Entity_Id;
115 Derived_Type : Entity_Id);
116 -- Subsidiary procedure to Build_Derived_Type. For a derived access type,
117 -- create an implicit base if the parent type is constrained or if the
118 -- subtype indication has a constraint.
120 procedure Build_Derived_Array_Type
121 (N : Node_Id;
122 Parent_Type : Entity_Id;
123 Derived_Type : Entity_Id);
124 -- Subsidiary procedure to Build_Derived_Type. For a derived array type,
125 -- create an implicit base if the parent type is constrained or if the
126 -- subtype indication has a constraint.
128 procedure Build_Derived_Concurrent_Type
129 (N : Node_Id;
130 Parent_Type : Entity_Id;
131 Derived_Type : Entity_Id);
132 -- Subsidiary procedure to Build_Derived_Type. For a derived task or
133 -- protected type, inherit entries and protected subprograms, check
134 -- legality of discriminant constraints if any.
136 procedure Build_Derived_Enumeration_Type
137 (N : Node_Id;
138 Parent_Type : Entity_Id;
139 Derived_Type : Entity_Id);
140 -- Subsidiary procedure to Build_Derived_Type. For a derived enumeration
141 -- type, we must create a new list of literals. Types derived from
142 -- Character and Wide_Character are special-cased.
144 procedure Build_Derived_Numeric_Type
145 (N : Node_Id;
146 Parent_Type : Entity_Id;
147 Derived_Type : Entity_Id);
148 -- Subsidiary procedure to Build_Derived_Type. For numeric types, create
149 -- an anonymous base type, and propagate constraint to subtype if needed.
151 procedure Build_Derived_Private_Type
152 (N : Node_Id;
153 Parent_Type : Entity_Id;
154 Derived_Type : Entity_Id;
155 Is_Completion : Boolean;
156 Derive_Subps : Boolean := True);
157 -- Subsidiary procedure to Build_Derived_Type. This procedure is complex
158 -- because the parent may or may not have a completion, and the derivation
159 -- may itself be a completion.
161 procedure Build_Derived_Record_Type
162 (N : Node_Id;
163 Parent_Type : Entity_Id;
164 Derived_Type : Entity_Id;
165 Derive_Subps : Boolean := True);
166 -- Subsidiary procedure for Build_Derived_Type and
167 -- Analyze_Private_Extension_Declaration used for tagged and untagged
168 -- record types. All parameters are as in Build_Derived_Type except that
169 -- N, in addition to being an N_Full_Type_Declaration node, can also be an
170 -- N_Private_Extension_Declaration node. See the definition of this routine
171 -- for much more info. Derive_Subps indicates whether subprograms should
172 -- be derived from the parent type. The only case where Derive_Subps is
173 -- False is for an implicit derived full type for a type derived from a
174 -- private type (see Build_Derived_Type).
176 procedure Build_Discriminal (Discrim : Entity_Id);
177 -- Create the discriminal corresponding to discriminant Discrim, that is
178 -- the parameter corresponding to Discrim to be used in initialization
179 -- procedures for the type where Discrim is a discriminant. Discriminals
180 -- are not used during semantic analysis, and are not fully defined
181 -- entities until expansion. Thus they are not given a scope until
182 -- initialization procedures are built.
184 function Build_Discriminant_Constraints
185 (T : Entity_Id;
186 Def : Node_Id;
187 Derived_Def : Boolean := False) return Elist_Id;
188 -- Validate discriminant constraints and return the list of the constraints
189 -- in order of discriminant declarations, where T is the discriminated
190 -- unconstrained type. Def is the N_Subtype_Indication node where the
191 -- discriminants constraints for T are specified. Derived_Def is True
192 -- when building the discriminant constraints in a derived type definition
193 -- of the form "type D (...) is new T (xxx)". In this case T is the parent
194 -- type and Def is the constraint "(xxx)" on T and this routine sets the
195 -- Corresponding_Discriminant field of the discriminants in the derived
196 -- type D to point to the corresponding discriminants in the parent type T.
198 procedure Build_Discriminated_Subtype
199 (T : Entity_Id;
200 Def_Id : Entity_Id;
201 Elist : Elist_Id;
202 Related_Nod : Node_Id;
203 For_Access : Boolean := False);
204 -- Subsidiary procedure to Constrain_Discriminated_Type and to
205 -- Process_Incomplete_Dependents. Given
207 -- T (a possibly discriminated base type)
208 -- Def_Id (a very partially built subtype for T),
210 -- the call completes Def_Id to be the appropriate E_*_Subtype.
212 -- The Elist is the list of discriminant constraints if any (it is set
213 -- to No_Elist if T is not a discriminated type, and to an empty list if
214 -- T has discriminants but there are no discriminant constraints). The
215 -- Related_Nod is the same as Decl_Node in Create_Constrained_Components.
216 -- The For_Access says whether or not this subtype is really constraining
217 -- an access type. That is its sole purpose is the designated type of an
218 -- access type -- in which case a Private_Subtype Is_For_Access_Subtype
219 -- is built to avoid freezing T when the access subtype is frozen.
221 function Build_Scalar_Bound
222 (Bound : Node_Id;
223 Par_T : Entity_Id;
224 Der_T : Entity_Id) return Node_Id;
225 -- The bounds of a derived scalar type are conversions of the bounds of
226 -- the parent type. Optimize the representation if the bounds are literals.
227 -- Needs a more complete spec--what are the parameters exactly, and what
228 -- exactly is the returned value, and how is Bound affected???
230 procedure Build_Itype_Reference
231 (Ityp : Entity_Id;
232 Nod : Node_Id);
233 -- Create a reference to an internal type, for use by Gigi. The back-end
234 -- elaborates itypes on demand, i.e. when their first use is seen. This
235 -- can lead to scope anomalies if the first use is within a scope that is
236 -- nested within the scope that contains the point of definition of the
237 -- itype. The Itype_Reference node forces the elaboration of the itype
238 -- in the proper scope. The node is inserted after Nod, which is the
239 -- enclosing declaration that generated Ityp.
241 -- A related mechanism is used during expansion, for itypes created in
242 -- branches of conditionals. See Ensure_Defined in exp_util.
243 -- Could both mechanisms be merged ???
245 procedure Build_Underlying_Full_View
246 (N : Node_Id;
247 Typ : Entity_Id;
248 Par : Entity_Id);
249 -- If the completion of a private type is itself derived from a private
250 -- type, or if the full view of a private subtype is itself private, the
251 -- back-end has no way to compute the actual size of this type. We build
252 -- an internal subtype declaration of the proper parent type to convey
253 -- this information. This extra mechanism is needed because a full
254 -- view cannot itself have a full view (it would get clobbered during
255 -- view exchanges).
257 procedure Check_Access_Discriminant_Requires_Limited
258 (D : Node_Id;
259 Loc : Node_Id);
260 -- Check the restriction that the type to which an access discriminant
261 -- belongs must be a concurrent type or a descendant of a type with
262 -- the reserved word 'limited' in its declaration.
264 procedure Check_Anonymous_Access_Components
265 (Typ_Decl : Node_Id;
266 Typ : Entity_Id;
267 Prev : Entity_Id;
268 Comp_List : Node_Id);
269 -- Ada 2005 AI-382: an access component in a record definition can refer to
270 -- the enclosing record, in which case it denotes the type itself, and not
271 -- the current instance of the type. We create an anonymous access type for
272 -- the component, and flag it as an access to a component, so accessibility
273 -- checks are properly performed on it. The declaration of the access type
274 -- is placed ahead of that of the record to prevent order-of-elaboration
275 -- circularity issues in Gigi. We create an incomplete type for the record
276 -- declaration, which is the designated type of the anonymous access.
278 procedure Check_Delta_Expression (E : Node_Id);
279 -- Check that the expression represented by E is suitable for use as a
280 -- delta expression, i.e. it is of real type and is static.
282 procedure Check_Digits_Expression (E : Node_Id);
283 -- Check that the expression represented by E is suitable for use as a
284 -- digits expression, i.e. it is of integer type, positive and static.
286 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id);
287 -- Validate the initialization of an object declaration. T is the required
288 -- type, and Exp is the initialization expression.
290 procedure Check_Interfaces (N : Node_Id; Def : Node_Id);
291 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
293 procedure Check_Or_Process_Discriminants
294 (N : Node_Id;
295 T : Entity_Id;
296 Prev : Entity_Id := Empty);
297 -- If T is the full declaration of an incomplete or private type, check the
298 -- conformance of the discriminants, otherwise process them. Prev is the
299 -- entity of the partial declaration, if any.
301 procedure Check_Real_Bound (Bound : Node_Id);
302 -- Check given bound for being of real type and static. If not, post an
303 -- appropriate message, and rewrite the bound with the real literal zero.
305 procedure Constant_Redeclaration
306 (Id : Entity_Id;
307 N : Node_Id;
308 T : out Entity_Id);
309 -- Various checks on legality of full declaration of deferred constant.
310 -- Id is the entity for the redeclaration, N is the N_Object_Declaration,
311 -- node. The caller has not yet set any attributes of this entity.
313 function Contain_Interface
314 (Iface : Entity_Id;
315 Ifaces : Elist_Id) return Boolean;
316 -- Ada 2005: Determine whether Iface is present in the list Ifaces
318 procedure Convert_Scalar_Bounds
319 (N : Node_Id;
320 Parent_Type : Entity_Id;
321 Derived_Type : Entity_Id;
322 Loc : Source_Ptr);
323 -- For derived scalar types, convert the bounds in the type definition to
324 -- the derived type, and complete their analysis. Given a constraint of the
325 -- form ".. new T range Lo .. Hi", Lo and Hi are analyzed and resolved with
326 -- T'Base, the parent_type. The bounds of the derived type (the anonymous
327 -- base) are copies of Lo and Hi. Finally, the bounds of the derived
328 -- subtype are conversions of those bounds to the derived_type, so that
329 -- their typing is consistent.
331 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id);
332 -- Copies attributes from array base type T2 to array base type T1. Copies
333 -- only attributes that apply to base types, but not subtypes.
335 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id);
336 -- Copies attributes from array subtype T2 to array subtype T1. Copies
337 -- attributes that apply to both subtypes and base types.
339 procedure Create_Constrained_Components
340 (Subt : Entity_Id;
341 Decl_Node : Node_Id;
342 Typ : Entity_Id;
343 Constraints : Elist_Id);
344 -- Build the list of entities for a constrained discriminated record
345 -- subtype. If a component depends on a discriminant, replace its subtype
346 -- using the discriminant values in the discriminant constraint. Subt
347 -- is the defining identifier for the subtype whose list of constrained
348 -- entities we will create. Decl_Node is the type declaration node where
349 -- we will attach all the itypes created. Typ is the base discriminated
350 -- type for the subtype Subt. Constraints is the list of discriminant
351 -- constraints for Typ.
353 function Constrain_Component_Type
354 (Comp : Entity_Id;
355 Constrained_Typ : Entity_Id;
356 Related_Node : Node_Id;
357 Typ : Entity_Id;
358 Constraints : Elist_Id) return Entity_Id;
359 -- Given a discriminated base type Typ, a list of discriminant constraint
360 -- Constraints for Typ and a component of Typ, with type Compon_Type,
361 -- create and return the type corresponding to Compon_type where all
362 -- discriminant references are replaced with the corresponding constraint.
363 -- If no discriminant references occur in Compon_Typ then return it as is.
364 -- Constrained_Typ is the final constrained subtype to which the
365 -- constrained Compon_Type belongs. Related_Node is the node where we will
366 -- attach all the itypes created.
368 -- Above description is confused, what is Compon_Type???
370 procedure Constrain_Access
371 (Def_Id : in out Entity_Id;
372 S : Node_Id;
373 Related_Nod : Node_Id);
374 -- Apply a list of constraints to an access type. If Def_Id is empty, it is
375 -- an anonymous type created for a subtype indication. In that case it is
376 -- created in the procedure and attached to Related_Nod.
378 procedure Constrain_Array
379 (Def_Id : in out Entity_Id;
380 SI : Node_Id;
381 Related_Nod : Node_Id;
382 Related_Id : Entity_Id;
383 Suffix : Character);
384 -- Apply a list of index constraints to an unconstrained array type. The
385 -- first parameter is the entity for the resulting subtype. A value of
386 -- Empty for Def_Id indicates that an implicit type must be created, but
387 -- creation is delayed (and must be done by this procedure) because other
388 -- subsidiary implicit types must be created first (which is why Def_Id
389 -- is an in/out parameter). The second parameter is a subtype indication
390 -- node for the constrained array to be created (e.g. something of the
391 -- form string (1 .. 10)). Related_Nod gives the place where this type
392 -- has to be inserted in the tree. The Related_Id and Suffix parameters
393 -- are used to build the associated Implicit type name.
395 procedure Constrain_Concurrent
396 (Def_Id : in out Entity_Id;
397 SI : Node_Id;
398 Related_Nod : Node_Id;
399 Related_Id : Entity_Id;
400 Suffix : Character);
401 -- Apply list of discriminant constraints to an unconstrained concurrent
402 -- type.
404 -- SI is the N_Subtype_Indication node containing the constraint and
405 -- the unconstrained type to constrain.
407 -- Def_Id is the entity for the resulting constrained subtype. A value
408 -- of Empty for Def_Id indicates that an implicit type must be created,
409 -- but creation is delayed (and must be done by this procedure) because
410 -- other subsidiary implicit types must be created first (which is why
411 -- Def_Id is an in/out parameter).
413 -- Related_Nod gives the place where this type has to be inserted
414 -- in the tree
416 -- The last two arguments are used to create its external name if needed.
418 function Constrain_Corresponding_Record
419 (Prot_Subt : Entity_Id;
420 Corr_Rec : Entity_Id;
421 Related_Nod : Node_Id;
422 Related_Id : Entity_Id) return Entity_Id;
423 -- When constraining a protected type or task type with discriminants,
424 -- constrain the corresponding record with the same discriminant values.
426 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id);
427 -- Constrain a decimal fixed point type with a digits constraint and/or a
428 -- range constraint, and build E_Decimal_Fixed_Point_Subtype entity.
430 procedure Constrain_Discriminated_Type
431 (Def_Id : Entity_Id;
432 S : Node_Id;
433 Related_Nod : Node_Id;
434 For_Access : Boolean := False);
435 -- Process discriminant constraints of composite type. Verify that values
436 -- have been provided for all discriminants, that the original type is
437 -- unconstrained, and that the types of the supplied expressions match
438 -- the discriminant types. The first three parameters are like in routine
439 -- Constrain_Concurrent. See Build_Discriminated_Subtype for an explanation
440 -- of For_Access.
442 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id);
443 -- Constrain an enumeration type with a range constraint. This is identical
444 -- to Constrain_Integer, but for the Ekind of the resulting subtype.
446 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id);
447 -- Constrain a floating point type with either a digits constraint
448 -- and/or a range constraint, building a E_Floating_Point_Subtype.
450 procedure Constrain_Index
451 (Index : Node_Id;
452 S : Node_Id;
453 Related_Nod : Node_Id;
454 Related_Id : Entity_Id;
455 Suffix : Character;
456 Suffix_Index : Nat);
457 -- Process an index constraint in a constrained array declaration. The
458 -- constraint can be a subtype name, or a range with or without an explicit
459 -- subtype mark. The index is the corresponding index of the unconstrained
460 -- array. The Related_Id and Suffix parameters are used to build the
461 -- associated Implicit type name.
463 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id);
464 -- Build subtype of a signed or modular integer type
466 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id);
467 -- Constrain an ordinary fixed point type with a range constraint, and
468 -- build an E_Ordinary_Fixed_Point_Subtype entity.
470 procedure Copy_And_Swap (Priv, Full : Entity_Id);
471 -- Copy the Priv entity into the entity of its full declaration then swap
472 -- the two entities in such a manner that the former private type is now
473 -- seen as a full type.
475 procedure Decimal_Fixed_Point_Type_Declaration
476 (T : Entity_Id;
477 Def : Node_Id);
478 -- Create a new decimal fixed point type, and apply the constraint to
479 -- obtain a subtype of this new type.
481 procedure Complete_Private_Subtype
482 (Priv : Entity_Id;
483 Full : Entity_Id;
484 Full_Base : Entity_Id;
485 Related_Nod : Node_Id);
486 -- Complete the implicit full view of a private subtype by setting the
487 -- appropriate semantic fields. If the full view of the parent is a record
488 -- type, build constrained components of subtype.
490 procedure Derive_Progenitor_Subprograms
491 (Parent_Type : Entity_Id;
492 Tagged_Type : Entity_Id);
493 -- Ada 2005 (AI-251): To complete type derivation, collect the primitive
494 -- operations of progenitors of Tagged_Type, and replace the subsidiary
495 -- subtypes with Tagged_Type, to build the specs of the inherited interface
496 -- primitives. The derived primitives are aliased to those of the
497 -- interface. This routine takes care also of transferring to the full-view
498 -- subprograms associated with the partial-view of Tagged_Type that cover
499 -- interface primitives.
501 procedure Derived_Standard_Character
502 (N : Node_Id;
503 Parent_Type : Entity_Id;
504 Derived_Type : Entity_Id);
505 -- Subsidiary procedure to Build_Derived_Enumeration_Type which handles
506 -- derivations from types Standard.Character and Standard.Wide_Character.
508 procedure Derived_Type_Declaration
509 (T : Entity_Id;
510 N : Node_Id;
511 Is_Completion : Boolean);
512 -- Process a derived type declaration. Build_Derived_Type is invoked
513 -- to process the actual derived type definition. Parameters N and
514 -- Is_Completion have the same meaning as in Build_Derived_Type.
515 -- T is the N_Defining_Identifier for the entity defined in the
516 -- N_Full_Type_Declaration node N, that is T is the derived type.
518 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id);
519 -- Insert each literal in symbol table, as an overloadable identifier. Each
520 -- enumeration type is mapped into a sequence of integers, and each literal
521 -- is defined as a constant with integer value. If any of the literals are
522 -- character literals, the type is a character type, which means that
523 -- strings are legal aggregates for arrays of components of the type.
525 function Expand_To_Stored_Constraint
526 (Typ : Entity_Id;
527 Constraint : Elist_Id) return Elist_Id;
528 -- Given a constraint (i.e. a list of expressions) on the discriminants of
529 -- Typ, expand it into a constraint on the stored discriminants and return
530 -- the new list of expressions constraining the stored discriminants.
532 function Find_Type_Of_Object
533 (Obj_Def : Node_Id;
534 Related_Nod : Node_Id) return Entity_Id;
535 -- Get type entity for object referenced by Obj_Def, attaching the
536 -- implicit types generated to Related_Nod
538 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id);
539 -- Create a new float and apply the constraint to obtain subtype of it
541 function Has_Range_Constraint (N : Node_Id) return Boolean;
542 -- Given an N_Subtype_Indication node N, return True if a range constraint
543 -- is present, either directly, or as part of a digits or delta constraint.
544 -- In addition, a digits constraint in the decimal case returns True, since
545 -- it establishes a default range if no explicit range is present.
547 function Inherit_Components
548 (N : Node_Id;
549 Parent_Base : Entity_Id;
550 Derived_Base : Entity_Id;
551 Is_Tagged : Boolean;
552 Inherit_Discr : Boolean;
553 Discs : Elist_Id) return Elist_Id;
554 -- Called from Build_Derived_Record_Type to inherit the components of
555 -- Parent_Base (a base type) into the Derived_Base (the derived base type).
556 -- For more information on derived types and component inheritance please
557 -- consult the comment above the body of Build_Derived_Record_Type.
559 -- N is the original derived type declaration
561 -- Is_Tagged is set if we are dealing with tagged types
563 -- If Inherit_Discr is set, Derived_Base inherits its discriminants from
564 -- Parent_Base, otherwise no discriminants are inherited.
566 -- Discs gives the list of constraints that apply to Parent_Base in the
567 -- derived type declaration. If Discs is set to No_Elist, then we have
568 -- the following situation:
570 -- type Parent (D1..Dn : ..) is [tagged] record ...;
571 -- type Derived is new Parent [with ...];
573 -- which gets treated as
575 -- type Derived (D1..Dn : ..) is new Parent (D1,..,Dn) [with ...];
577 -- For untagged types the returned value is an association list. The list
578 -- starts from the association (Parent_Base => Derived_Base), and then it
579 -- contains a sequence of the associations of the form
581 -- (Old_Component => New_Component),
583 -- where Old_Component is the Entity_Id of a component in Parent_Base and
584 -- New_Component is the Entity_Id of the corresponding component in
585 -- Derived_Base. For untagged records, this association list is needed when
586 -- copying the record declaration for the derived base. In the tagged case
587 -- the value returned is irrelevant.
589 function Is_Progenitor
590 (Iface : Entity_Id;
591 Typ : Entity_Id) return Boolean;
592 -- Determine whether type Typ implements interface Iface. This requires
593 -- traversing the list of abstract interfaces of the type, as well as that
594 -- of the ancestor types. The predicate is used to determine when a formal
595 -- in the signature of an inherited operation must carry the derived type.
597 function Is_Valid_Constraint_Kind
598 (T_Kind : Type_Kind;
599 Constraint_Kind : Node_Kind) return Boolean;
600 -- Returns True if it is legal to apply the given kind of constraint to the
601 -- given kind of type (index constraint to an array type, for example).
603 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id);
604 -- Create new modular type. Verify that modulus is in bounds and is
605 -- a power of two (implementation restriction).
607 procedure New_Concatenation_Op (Typ : Entity_Id);
608 -- Create an abbreviated declaration for an operator in order to
609 -- materialize concatenation on array types.
611 procedure Ordinary_Fixed_Point_Type_Declaration
612 (T : Entity_Id;
613 Def : Node_Id);
614 -- Create a new ordinary fixed point type, and apply the constraint to
615 -- obtain subtype of it.
617 procedure Prepare_Private_Subtype_Completion
618 (Id : Entity_Id;
619 Related_Nod : Node_Id);
620 -- Id is a subtype of some private type. Creates the full declaration
621 -- associated with Id whenever possible, i.e. when the full declaration
622 -- of the base type is already known. Records each subtype into
623 -- Private_Dependents of the base type.
625 procedure Process_Incomplete_Dependents
626 (N : Node_Id;
627 Full_T : Entity_Id;
628 Inc_T : Entity_Id);
629 -- Process all entities that depend on an incomplete type. There include
630 -- subtypes, subprogram types that mention the incomplete type in their
631 -- profiles, and subprogram with access parameters that designate the
632 -- incomplete type.
634 -- Inc_T is the defining identifier of an incomplete type declaration, its
635 -- Ekind is E_Incomplete_Type.
637 -- N is the corresponding N_Full_Type_Declaration for Inc_T.
639 -- Full_T is N's defining identifier.
641 -- Subtypes of incomplete types with discriminants are completed when the
642 -- parent type is. This is simpler than private subtypes, because they can
643 -- only appear in the same scope, and there is no need to exchange views.
644 -- Similarly, access_to_subprogram types may have a parameter or a return
645 -- type that is an incomplete type, and that must be replaced with the
646 -- full type.
648 -- If the full type is tagged, subprogram with access parameters that
649 -- designated the incomplete may be primitive operations of the full type,
650 -- and have to be processed accordingly.
652 procedure Process_Real_Range_Specification (Def : Node_Id);
653 -- Given the type definition for a real type, this procedure processes and
654 -- checks the real range specification of this type definition if one is
655 -- present. If errors are found, error messages are posted, and the
656 -- Real_Range_Specification of Def is reset to Empty.
658 procedure Record_Type_Declaration
659 (T : Entity_Id;
660 N : Node_Id;
661 Prev : Entity_Id);
662 -- Process a record type declaration (for both untagged and tagged
663 -- records). Parameters T and N are exactly like in procedure
664 -- Derived_Type_Declaration, except that no flag Is_Completion is needed
665 -- for this routine. If this is the completion of an incomplete type
666 -- declaration, Prev is the entity of the incomplete declaration, used for
667 -- cross-referencing. Otherwise Prev = T.
669 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id);
670 -- This routine is used to process the actual record type definition (both
671 -- for untagged and tagged records). Def is a record type definition node.
672 -- This procedure analyzes the components in this record type definition.
673 -- Prev_T is the entity for the enclosing record type. It is provided so
674 -- that its Has_Task flag can be set if any of the component have Has_Task
675 -- set. If the declaration is the completion of an incomplete type
676 -- declaration, Prev_T is the original incomplete type, whose full view is
677 -- the record type.
679 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id);
680 -- Subsidiary to Build_Derived_Record_Type. For untagged records, we
681 -- build a copy of the declaration tree of the parent, and we create
682 -- independently the list of components for the derived type. Semantic
683 -- information uses the component entities, but record representation
684 -- clauses are validated on the declaration tree. This procedure replaces
685 -- discriminants and components in the declaration with those that have
686 -- been created by Inherit_Components.
688 procedure Set_Fixed_Range
689 (E : Entity_Id;
690 Loc : Source_Ptr;
691 Lo : Ureal;
692 Hi : Ureal);
693 -- Build a range node with the given bounds and set it as the Scalar_Range
694 -- of the given fixed-point type entity. Loc is the source location used
695 -- for the constructed range. See body for further details.
697 procedure Set_Scalar_Range_For_Subtype
698 (Def_Id : Entity_Id;
699 R : Node_Id;
700 Subt : Entity_Id);
701 -- This routine is used to set the scalar range field for a subtype given
702 -- Def_Id, the entity for the subtype, and R, the range expression for the
703 -- scalar range. Subt provides the parent subtype to be used to analyze,
704 -- resolve, and check the given range.
706 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id);
707 -- Create a new signed integer entity, and apply the constraint to obtain
708 -- the required first named subtype of this type.
710 procedure Set_Stored_Constraint_From_Discriminant_Constraint
711 (E : Entity_Id);
712 -- E is some record type. This routine computes E's Stored_Constraint
713 -- from its Discriminant_Constraint.
715 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id);
716 -- Check that an entity in a list of progenitors is an interface,
717 -- emit error otherwise.
719 -----------------------
720 -- Access_Definition --
721 -----------------------
723 function Access_Definition
724 (Related_Nod : Node_Id;
725 N : Node_Id) return Entity_Id
727 Loc : constant Source_Ptr := Sloc (Related_Nod);
728 Anon_Type : Entity_Id;
729 Anon_Scope : Entity_Id;
730 Desig_Type : Entity_Id;
731 Decl : Entity_Id;
733 begin
734 if Is_Entry (Current_Scope)
735 and then Is_Task_Type (Etype (Scope (Current_Scope)))
736 then
737 Error_Msg_N ("task entries cannot have access parameters", N);
738 return Empty;
739 end if;
741 -- Ada 2005: for an object declaration the corresponding anonymous
742 -- type is declared in the current scope.
744 -- If the access definition is the return type of another access to
745 -- function, scope is the current one, because it is the one of the
746 -- current type declaration.
748 if Nkind_In (Related_Nod, N_Object_Declaration,
749 N_Access_Function_Definition)
750 then
751 Anon_Scope := Current_Scope;
753 -- For the anonymous function result case, retrieve the scope of the
754 -- function specification's associated entity rather than using the
755 -- current scope. The current scope will be the function itself if the
756 -- formal part is currently being analyzed, but will be the parent scope
757 -- in the case of a parameterless function, and we always want to use
758 -- the function's parent scope. Finally, if the function is a child
759 -- unit, we must traverse the tree to retrieve the proper entity.
761 elsif Nkind (Related_Nod) = N_Function_Specification
762 and then Nkind (Parent (N)) /= N_Parameter_Specification
763 then
764 -- If the current scope is a protected type, the anonymous access
765 -- is associated with one of the protected operations, and must
766 -- be available in the scope that encloses the protected declaration.
767 -- Otherwise the type is is in the scope enclosing the subprogram.
769 if Ekind (Current_Scope) = E_Protected_Type then
770 Anon_Scope := Scope (Scope (Defining_Entity (Related_Nod)));
771 else
772 Anon_Scope := Scope (Defining_Entity (Related_Nod));
773 end if;
775 else
776 -- For access formals, access components, and access discriminants,
777 -- the scope is that of the enclosing declaration,
779 Anon_Scope := Scope (Current_Scope);
780 end if;
782 Anon_Type :=
783 Create_Itype
784 (E_Anonymous_Access_Type, Related_Nod, Scope_Id => Anon_Scope);
786 if All_Present (N)
787 and then Ada_Version >= Ada_05
788 then
789 Error_Msg_N ("ALL is not permitted for anonymous access types", N);
790 end if;
792 -- Ada 2005 (AI-254): In case of anonymous access to subprograms call
793 -- the corresponding semantic routine
795 if Present (Access_To_Subprogram_Definition (N)) then
796 Access_Subprogram_Declaration
797 (T_Name => Anon_Type,
798 T_Def => Access_To_Subprogram_Definition (N));
800 if Ekind (Anon_Type) = E_Access_Protected_Subprogram_Type then
801 Set_Ekind
802 (Anon_Type, E_Anonymous_Access_Protected_Subprogram_Type);
803 else
804 Set_Ekind
805 (Anon_Type, E_Anonymous_Access_Subprogram_Type);
806 end if;
808 Set_Can_Use_Internal_Rep
809 (Anon_Type, not Always_Compatible_Rep_On_Target);
811 -- If the anonymous access is associated with a protected operation
812 -- create a reference to it after the enclosing protected definition
813 -- because the itype will be used in the subsequent bodies.
815 if Ekind (Current_Scope) = E_Protected_Type then
816 Build_Itype_Reference (Anon_Type, Parent (Current_Scope));
817 end if;
819 return Anon_Type;
820 end if;
822 Find_Type (Subtype_Mark (N));
823 Desig_Type := Entity (Subtype_Mark (N));
825 Set_Directly_Designated_Type
826 (Anon_Type, Desig_Type);
827 Set_Etype (Anon_Type, Anon_Type);
829 -- Make sure the anonymous access type has size and alignment fields
830 -- set, as required by gigi. This is necessary in the case of the
831 -- Task_Body_Procedure.
833 if not Has_Private_Component (Desig_Type) then
834 Layout_Type (Anon_Type);
835 end if;
837 -- ???The following makes no sense, because Anon_Type is an access type
838 -- and therefore cannot have components, private or otherwise. Hence
839 -- the assertion. Not sure what was meant, here.
840 Set_Depends_On_Private (Anon_Type, Has_Private_Component (Anon_Type));
841 pragma Assert (not Depends_On_Private (Anon_Type));
843 -- Ada 2005 (AI-231): Ada 2005 semantics for anonymous access differs
844 -- from Ada 95 semantics. In Ada 2005, anonymous access must specify if
845 -- the null value is allowed. In Ada 95 the null value is never allowed.
847 if Ada_Version >= Ada_05 then
848 Set_Can_Never_Be_Null (Anon_Type, Null_Exclusion_Present (N));
849 else
850 Set_Can_Never_Be_Null (Anon_Type, True);
851 end if;
853 -- The anonymous access type is as public as the discriminated type or
854 -- subprogram that defines it. It is imported (for back-end purposes)
855 -- if the designated type is.
857 Set_Is_Public (Anon_Type, Is_Public (Scope (Anon_Type)));
859 -- Ada 2005 (AI-50217): Propagate the attribute that indicates that the
860 -- designated type comes from the limited view.
862 Set_From_With_Type (Anon_Type, From_With_Type (Desig_Type));
864 -- Ada 2005 (AI-231): Propagate the access-constant attribute
866 Set_Is_Access_Constant (Anon_Type, Constant_Present (N));
868 -- The context is either a subprogram declaration, object declaration,
869 -- or an access discriminant, in a private or a full type declaration.
870 -- In the case of a subprogram, if the designated type is incomplete,
871 -- the operation will be a primitive operation of the full type, to be
872 -- updated subsequently. If the type is imported through a limited_with
873 -- clause, the subprogram is not a primitive operation of the type
874 -- (which is declared elsewhere in some other scope).
876 if Ekind (Desig_Type) = E_Incomplete_Type
877 and then not From_With_Type (Desig_Type)
878 and then Is_Overloadable (Current_Scope)
879 then
880 Append_Elmt (Current_Scope, Private_Dependents (Desig_Type));
881 Set_Has_Delayed_Freeze (Current_Scope);
882 end if;
884 -- Ada 2005: if the designated type is an interface that may contain
885 -- tasks, create a Master entity for the declaration. This must be done
886 -- before expansion of the full declaration, because the declaration may
887 -- include an expression that is an allocator, whose expansion needs the
888 -- proper Master for the created tasks.
890 if Nkind (Related_Nod) = N_Object_Declaration
891 and then Expander_Active
892 then
893 if Is_Interface (Desig_Type)
894 and then Is_Limited_Record (Desig_Type)
895 then
896 Build_Class_Wide_Master (Anon_Type);
898 -- Similarly, if the type is an anonymous access that designates
899 -- tasks, create a master entity for it in the current context.
901 elsif Has_Task (Desig_Type)
902 and then Comes_From_Source (Related_Nod)
903 then
904 if not Has_Master_Entity (Current_Scope) then
905 Decl :=
906 Make_Object_Declaration (Loc,
907 Defining_Identifier =>
908 Make_Defining_Identifier (Loc, Name_uMaster),
909 Constant_Present => True,
910 Object_Definition =>
911 New_Reference_To (RTE (RE_Master_Id), Loc),
912 Expression =>
913 Make_Explicit_Dereference (Loc,
914 New_Reference_To (RTE (RE_Current_Master), Loc)));
916 Insert_Before (Related_Nod, Decl);
917 Analyze (Decl);
919 Set_Master_Id (Anon_Type, Defining_Identifier (Decl));
920 Set_Has_Master_Entity (Current_Scope);
921 else
922 Build_Master_Renaming (Related_Nod, Anon_Type);
923 end if;
924 end if;
925 end if;
927 -- For a private component of a protected type, it is imperative that
928 -- the back-end elaborate the type immediately after the protected
929 -- declaration, because this type will be used in the declarations
930 -- created for the component within each protected body, so we must
931 -- create an itype reference for it now.
933 if Nkind (Parent (Related_Nod)) = N_Protected_Definition then
934 Build_Itype_Reference (Anon_Type, Parent (Parent (Related_Nod)));
936 -- Similarly, if the access definition is the return result of a
937 -- protected function, create an itype reference for it because it
938 -- will be used within the function body.
940 elsif Nkind (Related_Nod) = N_Function_Specification
941 and then Ekind (Current_Scope) = E_Protected_Type
942 then
943 Build_Itype_Reference (Anon_Type, Parent (Current_Scope));
945 -- Finally, create an itype reference for an object declaration of
946 -- an anonymous access type. This is strictly necessary only for
947 -- deferred constants, but in any case will avoid out-of-scope
948 -- problems in the back-end.
950 elsif Nkind (Related_Nod) = N_Object_Declaration then
951 Build_Itype_Reference (Anon_Type, Related_Nod);
952 end if;
954 return Anon_Type;
955 end Access_Definition;
957 -----------------------------------
958 -- Access_Subprogram_Declaration --
959 -----------------------------------
961 procedure Access_Subprogram_Declaration
962 (T_Name : Entity_Id;
963 T_Def : Node_Id)
966 procedure Check_For_Premature_Usage (Def : Node_Id);
967 -- Check that type T_Name is not used, directly or recursively,
968 -- as a parameter or a return type in Def. Def is either a subtype,
969 -- an access_definition, or an access_to_subprogram_definition.
971 -------------------------------
972 -- Check_For_Premature_Usage --
973 -------------------------------
975 procedure Check_For_Premature_Usage (Def : Node_Id) is
976 Param : Node_Id;
978 begin
979 -- Check for a subtype mark
981 if Nkind (Def) in N_Has_Etype then
982 if Etype (Def) = T_Name then
983 Error_Msg_N
984 ("type& cannot be used before end of its declaration", Def);
985 end if;
987 -- If this is not a subtype, then this is an access_definition
989 elsif Nkind (Def) = N_Access_Definition then
990 if Present (Access_To_Subprogram_Definition (Def)) then
991 Check_For_Premature_Usage
992 (Access_To_Subprogram_Definition (Def));
993 else
994 Check_For_Premature_Usage (Subtype_Mark (Def));
995 end if;
997 -- The only cases left are N_Access_Function_Definition and
998 -- N_Access_Procedure_Definition.
1000 else
1001 if Present (Parameter_Specifications (Def)) then
1002 Param := First (Parameter_Specifications (Def));
1003 while Present (Param) loop
1004 Check_For_Premature_Usage (Parameter_Type (Param));
1005 Param := Next (Param);
1006 end loop;
1007 end if;
1009 if Nkind (Def) = N_Access_Function_Definition then
1010 Check_For_Premature_Usage (Result_Definition (Def));
1011 end if;
1012 end if;
1013 end Check_For_Premature_Usage;
1015 -- Local variables
1017 Formals : constant List_Id := Parameter_Specifications (T_Def);
1018 Formal : Entity_Id;
1019 D_Ityp : Node_Id;
1020 Desig_Type : constant Entity_Id :=
1021 Create_Itype (E_Subprogram_Type, Parent (T_Def));
1023 -- Start of processing for Access_Subprogram_Declaration
1025 begin
1026 -- Associate the Itype node with the inner full-type declaration or
1027 -- subprogram spec. This is required to handle nested anonymous
1028 -- declarations. For example:
1030 -- procedure P
1031 -- (X : access procedure
1032 -- (Y : access procedure
1033 -- (Z : access T)))
1035 D_Ityp := Associated_Node_For_Itype (Desig_Type);
1036 while not (Nkind_In (D_Ityp, N_Full_Type_Declaration,
1037 N_Private_Type_Declaration,
1038 N_Private_Extension_Declaration,
1039 N_Procedure_Specification,
1040 N_Function_Specification)
1041 or else
1042 Nkind_In (D_Ityp, N_Object_Declaration,
1043 N_Object_Renaming_Declaration,
1044 N_Formal_Type_Declaration,
1045 N_Task_Type_Declaration,
1046 N_Protected_Type_Declaration))
1047 loop
1048 D_Ityp := Parent (D_Ityp);
1049 pragma Assert (D_Ityp /= Empty);
1050 end loop;
1052 Set_Associated_Node_For_Itype (Desig_Type, D_Ityp);
1054 if Nkind_In (D_Ityp, N_Procedure_Specification,
1055 N_Function_Specification)
1056 then
1057 Set_Scope (Desig_Type, Scope (Defining_Entity (D_Ityp)));
1059 elsif Nkind_In (D_Ityp, N_Full_Type_Declaration,
1060 N_Object_Declaration,
1061 N_Object_Renaming_Declaration,
1062 N_Formal_Type_Declaration)
1063 then
1064 Set_Scope (Desig_Type, Scope (Defining_Identifier (D_Ityp)));
1065 end if;
1067 if Nkind (T_Def) = N_Access_Function_Definition then
1068 if Nkind (Result_Definition (T_Def)) = N_Access_Definition then
1069 declare
1070 Acc : constant Node_Id := Result_Definition (T_Def);
1072 begin
1073 if Present (Access_To_Subprogram_Definition (Acc))
1074 and then
1075 Protected_Present (Access_To_Subprogram_Definition (Acc))
1076 then
1077 Set_Etype
1078 (Desig_Type,
1079 Replace_Anonymous_Access_To_Protected_Subprogram
1080 (T_Def));
1082 else
1083 Set_Etype
1084 (Desig_Type,
1085 Access_Definition (T_Def, Result_Definition (T_Def)));
1086 end if;
1087 end;
1089 else
1090 Analyze (Result_Definition (T_Def));
1091 Set_Etype (Desig_Type, Entity (Result_Definition (T_Def)));
1092 end if;
1094 if not (Is_Type (Etype (Desig_Type))) then
1095 Error_Msg_N
1096 ("expect type in function specification",
1097 Result_Definition (T_Def));
1098 end if;
1100 else
1101 Set_Etype (Desig_Type, Standard_Void_Type);
1102 end if;
1104 if Present (Formals) then
1105 Push_Scope (Desig_Type);
1106 Process_Formals (Formals, Parent (T_Def));
1108 -- A bit of a kludge here, End_Scope requires that the parent
1109 -- pointer be set to something reasonable, but Itypes don't have
1110 -- parent pointers. So we set it and then unset it ??? If and when
1111 -- Itypes have proper parent pointers to their declarations, this
1112 -- kludge can be removed.
1114 Set_Parent (Desig_Type, T_Name);
1115 End_Scope;
1116 Set_Parent (Desig_Type, Empty);
1117 end if;
1119 -- Check for premature usage of the type being defined
1121 Check_For_Premature_Usage (T_Def);
1123 -- The return type and/or any parameter type may be incomplete. Mark
1124 -- the subprogram_type as depending on the incomplete type, so that
1125 -- it can be updated when the full type declaration is seen. This
1126 -- only applies to incomplete types declared in some enclosing scope,
1127 -- not to limited views from other packages.
1129 if Present (Formals) then
1130 Formal := First_Formal (Desig_Type);
1131 while Present (Formal) loop
1132 if Ekind (Formal) /= E_In_Parameter
1133 and then Nkind (T_Def) = N_Access_Function_Definition
1134 then
1135 Error_Msg_N ("functions can only have IN parameters", Formal);
1136 end if;
1138 if Ekind (Etype (Formal)) = E_Incomplete_Type
1139 and then In_Open_Scopes (Scope (Etype (Formal)))
1140 then
1141 Append_Elmt (Desig_Type, Private_Dependents (Etype (Formal)));
1142 Set_Has_Delayed_Freeze (Desig_Type);
1143 end if;
1145 Next_Formal (Formal);
1146 end loop;
1147 end if;
1149 if Ekind (Etype (Desig_Type)) = E_Incomplete_Type
1150 and then not Has_Delayed_Freeze (Desig_Type)
1151 then
1152 Append_Elmt (Desig_Type, Private_Dependents (Etype (Desig_Type)));
1153 Set_Has_Delayed_Freeze (Desig_Type);
1154 end if;
1156 Check_Delayed_Subprogram (Desig_Type);
1158 if Protected_Present (T_Def) then
1159 Set_Ekind (T_Name, E_Access_Protected_Subprogram_Type);
1160 Set_Convention (Desig_Type, Convention_Protected);
1161 else
1162 Set_Ekind (T_Name, E_Access_Subprogram_Type);
1163 end if;
1165 Set_Can_Use_Internal_Rep (T_Name, not Always_Compatible_Rep_On_Target);
1167 Set_Etype (T_Name, T_Name);
1168 Init_Size_Align (T_Name);
1169 Set_Directly_Designated_Type (T_Name, Desig_Type);
1171 -- Ada 2005 (AI-231): Propagate the null-excluding attribute
1173 Set_Can_Never_Be_Null (T_Name, Null_Exclusion_Present (T_Def));
1175 Check_Restriction (No_Access_Subprograms, T_Def);
1176 end Access_Subprogram_Declaration;
1178 ----------------------------
1179 -- Access_Type_Declaration --
1180 ----------------------------
1182 procedure Access_Type_Declaration (T : Entity_Id; Def : Node_Id) is
1183 S : constant Node_Id := Subtype_Indication (Def);
1184 P : constant Node_Id := Parent (Def);
1186 Desig : Entity_Id;
1187 -- Designated type
1189 begin
1190 -- Check for permissible use of incomplete type
1192 if Nkind (S) /= N_Subtype_Indication then
1193 Analyze (S);
1195 if Ekind (Root_Type (Entity (S))) = E_Incomplete_Type then
1196 Set_Directly_Designated_Type (T, Entity (S));
1197 else
1198 Set_Directly_Designated_Type (T,
1199 Process_Subtype (S, P, T, 'P'));
1200 end if;
1202 else
1203 Set_Directly_Designated_Type (T,
1204 Process_Subtype (S, P, T, 'P'));
1205 end if;
1207 if All_Present (Def) or Constant_Present (Def) then
1208 Set_Ekind (T, E_General_Access_Type);
1209 else
1210 Set_Ekind (T, E_Access_Type);
1211 end if;
1213 if Base_Type (Designated_Type (T)) = T then
1214 Error_Msg_N ("access type cannot designate itself", S);
1216 -- In Ada 2005, the type may have a limited view through some unit
1217 -- in its own context, allowing the following circularity that cannot
1218 -- be detected earlier
1220 elsif Is_Class_Wide_Type (Designated_Type (T))
1221 and then Etype (Designated_Type (T)) = T
1222 then
1223 Error_Msg_N
1224 ("access type cannot designate its own classwide type", S);
1226 -- Clean up indication of tagged status to prevent cascaded errors
1228 Set_Is_Tagged_Type (T, False);
1229 end if;
1231 Set_Etype (T, T);
1233 -- If the type has appeared already in a with_type clause, it is
1234 -- frozen and the pointer size is already set. Else, initialize.
1236 if not From_With_Type (T) then
1237 Init_Size_Align (T);
1238 end if;
1240 Desig := Designated_Type (T);
1242 -- If designated type is an imported tagged type, indicate that the
1243 -- access type is also imported, and therefore restricted in its use.
1244 -- The access type may already be imported, so keep setting otherwise.
1246 -- Ada 2005 (AI-50217): If the non-limited view of the designated type
1247 -- is available, use it as the designated type of the access type, so
1248 -- that the back-end gets a usable entity.
1250 if From_With_Type (Desig)
1251 and then Ekind (Desig) /= E_Access_Type
1252 then
1253 Set_From_With_Type (T);
1254 end if;
1256 -- Note that Has_Task is always false, since the access type itself
1257 -- is not a task type. See Einfo for more description on this point.
1258 -- Exactly the same consideration applies to Has_Controlled_Component.
1260 Set_Has_Task (T, False);
1261 Set_Has_Controlled_Component (T, False);
1263 -- Initialize Associated_Final_Chain explicitly to Empty, to avoid
1264 -- problems where an incomplete view of this entity has been previously
1265 -- established by a limited with and an overlaid version of this field
1266 -- (Stored_Constraint) was initialized for the incomplete view.
1268 Set_Associated_Final_Chain (T, Empty);
1270 -- Ada 2005 (AI-231): Propagate the null-excluding and access-constant
1271 -- attributes
1273 Set_Can_Never_Be_Null (T, Null_Exclusion_Present (Def));
1274 Set_Is_Access_Constant (T, Constant_Present (Def));
1275 end Access_Type_Declaration;
1277 ----------------------------------
1278 -- Add_Interface_Tag_Components --
1279 ----------------------------------
1281 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id) is
1282 Loc : constant Source_Ptr := Sloc (N);
1283 L : List_Id;
1284 Last_Tag : Node_Id;
1286 procedure Add_Tag (Iface : Entity_Id);
1287 -- Add tag for one of the progenitor interfaces
1289 -------------
1290 -- Add_Tag --
1291 -------------
1293 procedure Add_Tag (Iface : Entity_Id) is
1294 Decl : Node_Id;
1295 Def : Node_Id;
1296 Tag : Entity_Id;
1297 Offset : Entity_Id;
1299 begin
1300 pragma Assert (Is_Tagged_Type (Iface)
1301 and then Is_Interface (Iface));
1303 Def :=
1304 Make_Component_Definition (Loc,
1305 Aliased_Present => True,
1306 Subtype_Indication =>
1307 New_Occurrence_Of (RTE (RE_Interface_Tag), Loc));
1309 Tag := Make_Defining_Identifier (Loc, New_Internal_Name ('V'));
1311 Decl :=
1312 Make_Component_Declaration (Loc,
1313 Defining_Identifier => Tag,
1314 Component_Definition => Def);
1316 Analyze_Component_Declaration (Decl);
1318 Set_Analyzed (Decl);
1319 Set_Ekind (Tag, E_Component);
1320 Set_Is_Tag (Tag);
1321 Set_Is_Aliased (Tag);
1322 Set_Related_Type (Tag, Iface);
1323 Init_Component_Location (Tag);
1325 pragma Assert (Is_Frozen (Iface));
1327 Set_DT_Entry_Count (Tag,
1328 DT_Entry_Count (First_Entity (Iface)));
1330 if No (Last_Tag) then
1331 Prepend (Decl, L);
1332 else
1333 Insert_After (Last_Tag, Decl);
1334 end if;
1336 Last_Tag := Decl;
1338 -- If the ancestor has discriminants we need to give special support
1339 -- to store the offset_to_top value of the secondary dispatch tables.
1340 -- For this purpose we add a supplementary component just after the
1341 -- field that contains the tag associated with each secondary DT.
1343 if Typ /= Etype (Typ)
1344 and then Has_Discriminants (Etype (Typ))
1345 then
1346 Def :=
1347 Make_Component_Definition (Loc,
1348 Subtype_Indication =>
1349 New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
1351 Offset :=
1352 Make_Defining_Identifier (Loc, New_Internal_Name ('V'));
1354 Decl :=
1355 Make_Component_Declaration (Loc,
1356 Defining_Identifier => Offset,
1357 Component_Definition => Def);
1359 Analyze_Component_Declaration (Decl);
1361 Set_Analyzed (Decl);
1362 Set_Ekind (Offset, E_Component);
1363 Set_Is_Aliased (Offset);
1364 Set_Related_Type (Offset, Iface);
1365 Init_Component_Location (Offset);
1366 Insert_After (Last_Tag, Decl);
1367 Last_Tag := Decl;
1368 end if;
1369 end Add_Tag;
1371 -- Local variables
1373 Elmt : Elmt_Id;
1374 Ext : Node_Id;
1375 Comp : Node_Id;
1377 -- Start of processing for Add_Interface_Tag_Components
1379 begin
1380 if not RTE_Available (RE_Interface_Tag) then
1381 Error_Msg
1382 ("(Ada 2005) interface types not supported by this run-time!",
1383 Sloc (N));
1384 return;
1385 end if;
1387 if Ekind (Typ) /= E_Record_Type
1388 or else (Is_Concurrent_Record_Type (Typ)
1389 and then Is_Empty_List (Abstract_Interface_List (Typ)))
1390 or else (not Is_Concurrent_Record_Type (Typ)
1391 and then No (Interfaces (Typ))
1392 and then Is_Empty_Elmt_List (Interfaces (Typ)))
1393 then
1394 return;
1395 end if;
1397 -- Find the current last tag
1399 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1400 Ext := Record_Extension_Part (Type_Definition (N));
1401 else
1402 pragma Assert (Nkind (Type_Definition (N)) = N_Record_Definition);
1403 Ext := Type_Definition (N);
1404 end if;
1406 Last_Tag := Empty;
1408 if not (Present (Component_List (Ext))) then
1409 Set_Null_Present (Ext, False);
1410 L := New_List;
1411 Set_Component_List (Ext,
1412 Make_Component_List (Loc,
1413 Component_Items => L,
1414 Null_Present => False));
1415 else
1416 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1417 L := Component_Items
1418 (Component_List
1419 (Record_Extension_Part
1420 (Type_Definition (N))));
1421 else
1422 L := Component_Items
1423 (Component_List
1424 (Type_Definition (N)));
1425 end if;
1427 -- Find the last tag component
1429 Comp := First (L);
1430 while Present (Comp) loop
1431 if Nkind (Comp) = N_Component_Declaration
1432 and then Is_Tag (Defining_Identifier (Comp))
1433 then
1434 Last_Tag := Comp;
1435 end if;
1437 Next (Comp);
1438 end loop;
1439 end if;
1441 -- At this point L references the list of components and Last_Tag
1442 -- references the current last tag (if any). Now we add the tag
1443 -- corresponding with all the interfaces that are not implemented
1444 -- by the parent.
1446 if Present (Interfaces (Typ)) then
1447 Elmt := First_Elmt (Interfaces (Typ));
1448 while Present (Elmt) loop
1449 Add_Tag (Node (Elmt));
1450 Next_Elmt (Elmt);
1451 end loop;
1452 end if;
1453 end Add_Interface_Tag_Components;
1455 -----------------------------------
1456 -- Analyze_Component_Declaration --
1457 -----------------------------------
1459 procedure Analyze_Component_Declaration (N : Node_Id) is
1460 Id : constant Entity_Id := Defining_Identifier (N);
1461 E : constant Node_Id := Expression (N);
1462 T : Entity_Id;
1463 P : Entity_Id;
1465 function Contains_POC (Constr : Node_Id) return Boolean;
1466 -- Determines whether a constraint uses the discriminant of a record
1467 -- type thus becoming a per-object constraint (POC).
1469 function Is_Known_Limited (Typ : Entity_Id) return Boolean;
1470 -- Typ is the type of the current component, check whether this type is
1471 -- a limited type. Used to validate declaration against that of
1472 -- enclosing record.
1474 ------------------
1475 -- Contains_POC --
1476 ------------------
1478 function Contains_POC (Constr : Node_Id) return Boolean is
1479 begin
1480 -- Prevent cascaded errors
1482 if Error_Posted (Constr) then
1483 return False;
1484 end if;
1486 case Nkind (Constr) is
1487 when N_Attribute_Reference =>
1488 return
1489 Attribute_Name (Constr) = Name_Access
1490 and then Prefix (Constr) = Scope (Entity (Prefix (Constr)));
1492 when N_Discriminant_Association =>
1493 return Denotes_Discriminant (Expression (Constr));
1495 when N_Identifier =>
1496 return Denotes_Discriminant (Constr);
1498 when N_Index_Or_Discriminant_Constraint =>
1499 declare
1500 IDC : Node_Id;
1502 begin
1503 IDC := First (Constraints (Constr));
1504 while Present (IDC) loop
1506 -- One per-object constraint is sufficient
1508 if Contains_POC (IDC) then
1509 return True;
1510 end if;
1512 Next (IDC);
1513 end loop;
1515 return False;
1516 end;
1518 when N_Range =>
1519 return Denotes_Discriminant (Low_Bound (Constr))
1520 or else
1521 Denotes_Discriminant (High_Bound (Constr));
1523 when N_Range_Constraint =>
1524 return Denotes_Discriminant (Range_Expression (Constr));
1526 when others =>
1527 return False;
1529 end case;
1530 end Contains_POC;
1532 ----------------------
1533 -- Is_Known_Limited --
1534 ----------------------
1536 function Is_Known_Limited (Typ : Entity_Id) return Boolean is
1537 P : constant Entity_Id := Etype (Typ);
1538 R : constant Entity_Id := Root_Type (Typ);
1540 begin
1541 if Is_Limited_Record (Typ) then
1542 return True;
1544 -- If the root type is limited (and not a limited interface)
1545 -- so is the current type
1547 elsif Is_Limited_Record (R)
1548 and then
1549 (not Is_Interface (R)
1550 or else not Is_Limited_Interface (R))
1551 then
1552 return True;
1554 -- Else the type may have a limited interface progenitor, but a
1555 -- limited record parent.
1557 elsif R /= P
1558 and then Is_Limited_Record (P)
1559 then
1560 return True;
1562 else
1563 return False;
1564 end if;
1565 end Is_Known_Limited;
1567 -- Start of processing for Analyze_Component_Declaration
1569 begin
1570 Generate_Definition (Id);
1571 Enter_Name (Id);
1573 if Present (Subtype_Indication (Component_Definition (N))) then
1574 T := Find_Type_Of_Object
1575 (Subtype_Indication (Component_Definition (N)), N);
1577 -- Ada 2005 (AI-230): Access Definition case
1579 else
1580 pragma Assert (Present
1581 (Access_Definition (Component_Definition (N))));
1583 T := Access_Definition
1584 (Related_Nod => N,
1585 N => Access_Definition (Component_Definition (N)));
1586 Set_Is_Local_Anonymous_Access (T);
1588 -- Ada 2005 (AI-254)
1590 if Present (Access_To_Subprogram_Definition
1591 (Access_Definition (Component_Definition (N))))
1592 and then Protected_Present (Access_To_Subprogram_Definition
1593 (Access_Definition
1594 (Component_Definition (N))))
1595 then
1596 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1597 end if;
1598 end if;
1600 -- If the subtype is a constrained subtype of the enclosing record,
1601 -- (which must have a partial view) the back-end does not properly
1602 -- handle the recursion. Rewrite the component declaration with an
1603 -- explicit subtype indication, which is acceptable to Gigi. We can copy
1604 -- the tree directly because side effects have already been removed from
1605 -- discriminant constraints.
1607 if Ekind (T) = E_Access_Subtype
1608 and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
1609 and then Comes_From_Source (T)
1610 and then Nkind (Parent (T)) = N_Subtype_Declaration
1611 and then Etype (Directly_Designated_Type (T)) = Current_Scope
1612 then
1613 Rewrite
1614 (Subtype_Indication (Component_Definition (N)),
1615 New_Copy_Tree (Subtype_Indication (Parent (T))));
1616 T := Find_Type_Of_Object
1617 (Subtype_Indication (Component_Definition (N)), N);
1618 end if;
1620 -- If the component declaration includes a default expression, then we
1621 -- check that the component is not of a limited type (RM 3.7(5)),
1622 -- and do the special preanalysis of the expression (see section on
1623 -- "Handling of Default and Per-Object Expressions" in the spec of
1624 -- package Sem).
1626 if Present (E) then
1627 Preanalyze_Spec_Expression (E, T);
1628 Check_Initialization (T, E);
1630 if Ada_Version >= Ada_05
1631 and then Ekind (T) = E_Anonymous_Access_Type
1632 then
1633 -- Check RM 3.9.2(9): "if the expected type for an expression is
1634 -- an anonymous access-to-specific tagged type, then the object
1635 -- designated by the expression shall not be dynamically tagged
1636 -- unless it is a controlling operand in a call on a dispatching
1637 -- operation"
1639 if Is_Tagged_Type (Directly_Designated_Type (T))
1640 and then
1641 Ekind (Directly_Designated_Type (T)) /= E_Class_Wide_Type
1642 and then
1643 Ekind (Directly_Designated_Type (Etype (E))) =
1644 E_Class_Wide_Type
1645 then
1646 Error_Msg_N
1647 ("access to specific tagged type required (RM 3.9.2(9))", E);
1648 end if;
1650 -- (Ada 2005: AI-230): Accessibility check for anonymous
1651 -- components
1653 if Type_Access_Level (Etype (E)) > Type_Access_Level (T) then
1654 Error_Msg_N
1655 ("expression has deeper access level than component " &
1656 "(RM 3.10.2 (12.2))", E);
1657 end if;
1659 -- The initialization expression is a reference to an access
1660 -- discriminant. The type of the discriminant is always deeper
1661 -- than any access type.
1663 if Ekind (Etype (E)) = E_Anonymous_Access_Type
1664 and then Is_Entity_Name (E)
1665 and then Ekind (Entity (E)) = E_In_Parameter
1666 and then Present (Discriminal_Link (Entity (E)))
1667 then
1668 Error_Msg_N
1669 ("discriminant has deeper accessibility level than target",
1671 end if;
1672 end if;
1673 end if;
1675 -- The parent type may be a private view with unknown discriminants,
1676 -- and thus unconstrained. Regular components must be constrained.
1678 if Is_Indefinite_Subtype (T) and then Chars (Id) /= Name_uParent then
1679 if Is_Class_Wide_Type (T) then
1680 Error_Msg_N
1681 ("class-wide subtype with unknown discriminants" &
1682 " in component declaration",
1683 Subtype_Indication (Component_Definition (N)));
1684 else
1685 Error_Msg_N
1686 ("unconstrained subtype in component declaration",
1687 Subtype_Indication (Component_Definition (N)));
1688 end if;
1690 -- Components cannot be abstract, except for the special case of
1691 -- the _Parent field (case of extending an abstract tagged type)
1693 elsif Is_Abstract_Type (T) and then Chars (Id) /= Name_uParent then
1694 Error_Msg_N ("type of a component cannot be abstract", N);
1695 end if;
1697 Set_Etype (Id, T);
1698 Set_Is_Aliased (Id, Aliased_Present (Component_Definition (N)));
1700 -- The component declaration may have a per-object constraint, set
1701 -- the appropriate flag in the defining identifier of the subtype.
1703 if Present (Subtype_Indication (Component_Definition (N))) then
1704 declare
1705 Sindic : constant Node_Id :=
1706 Subtype_Indication (Component_Definition (N));
1707 begin
1708 if Nkind (Sindic) = N_Subtype_Indication
1709 and then Present (Constraint (Sindic))
1710 and then Contains_POC (Constraint (Sindic))
1711 then
1712 Set_Has_Per_Object_Constraint (Id);
1713 end if;
1714 end;
1715 end if;
1717 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
1718 -- out some static checks.
1720 if Ada_Version >= Ada_05
1721 and then Can_Never_Be_Null (T)
1722 then
1723 Null_Exclusion_Static_Checks (N);
1724 end if;
1726 -- If this component is private (or depends on a private type), flag the
1727 -- record type to indicate that some operations are not available.
1729 P := Private_Component (T);
1731 if Present (P) then
1733 -- Check for circular definitions
1735 if P = Any_Type then
1736 Set_Etype (Id, Any_Type);
1738 -- There is a gap in the visibility of operations only if the
1739 -- component type is not defined in the scope of the record type.
1741 elsif Scope (P) = Scope (Current_Scope) then
1742 null;
1744 elsif Is_Limited_Type (P) then
1745 Set_Is_Limited_Composite (Current_Scope);
1747 else
1748 Set_Is_Private_Composite (Current_Scope);
1749 end if;
1750 end if;
1752 if P /= Any_Type
1753 and then Is_Limited_Type (T)
1754 and then Chars (Id) /= Name_uParent
1755 and then Is_Tagged_Type (Current_Scope)
1756 then
1757 if Is_Derived_Type (Current_Scope)
1758 and then not Is_Known_Limited (Current_Scope)
1759 then
1760 Error_Msg_N
1761 ("extension of nonlimited type cannot have limited components",
1764 if Is_Interface (Root_Type (Current_Scope)) then
1765 Error_Msg_N
1766 ("\limitedness is not inherited from limited interface", N);
1767 Error_Msg_N
1768 ("\add LIMITED to type indication", N);
1769 end if;
1771 Explain_Limited_Type (T, N);
1772 Set_Etype (Id, Any_Type);
1773 Set_Is_Limited_Composite (Current_Scope, False);
1775 elsif not Is_Derived_Type (Current_Scope)
1776 and then not Is_Limited_Record (Current_Scope)
1777 and then not Is_Concurrent_Type (Current_Scope)
1778 then
1779 Error_Msg_N
1780 ("nonlimited tagged type cannot have limited components", N);
1781 Explain_Limited_Type (T, N);
1782 Set_Etype (Id, Any_Type);
1783 Set_Is_Limited_Composite (Current_Scope, False);
1784 end if;
1785 end if;
1787 Set_Original_Record_Component (Id, Id);
1788 end Analyze_Component_Declaration;
1790 --------------------------
1791 -- Analyze_Declarations --
1792 --------------------------
1794 procedure Analyze_Declarations (L : List_Id) is
1795 D : Node_Id;
1796 Freeze_From : Entity_Id := Empty;
1797 Next_Node : Node_Id;
1799 procedure Adjust_D;
1800 -- Adjust D not to include implicit label declarations, since these
1801 -- have strange Sloc values that result in elaboration check problems.
1802 -- (They have the sloc of the label as found in the source, and that
1803 -- is ahead of the current declarative part).
1805 --------------
1806 -- Adjust_D --
1807 --------------
1809 procedure Adjust_D is
1810 begin
1811 while Present (Prev (D))
1812 and then Nkind (D) = N_Implicit_Label_Declaration
1813 loop
1814 Prev (D);
1815 end loop;
1816 end Adjust_D;
1818 -- Start of processing for Analyze_Declarations
1820 begin
1821 D := First (L);
1822 while Present (D) loop
1824 -- Complete analysis of declaration
1826 Analyze (D);
1827 Next_Node := Next (D);
1829 if No (Freeze_From) then
1830 Freeze_From := First_Entity (Current_Scope);
1831 end if;
1833 -- At the end of a declarative part, freeze remaining entities
1834 -- declared in it. The end of the visible declarations of package
1835 -- specification is not the end of a declarative part if private
1836 -- declarations are present. The end of a package declaration is a
1837 -- freezing point only if it a library package. A task definition or
1838 -- protected type definition is not a freeze point either. Finally,
1839 -- we do not freeze entities in generic scopes, because there is no
1840 -- code generated for them and freeze nodes will be generated for
1841 -- the instance.
1843 -- The end of a package instantiation is not a freeze point, but
1844 -- for now we make it one, because the generic body is inserted
1845 -- (currently) immediately after. Generic instantiations will not
1846 -- be a freeze point once delayed freezing of bodies is implemented.
1847 -- (This is needed in any case for early instantiations ???).
1849 if No (Next_Node) then
1850 if Nkind_In (Parent (L), N_Component_List,
1851 N_Task_Definition,
1852 N_Protected_Definition)
1853 then
1854 null;
1856 elsif Nkind (Parent (L)) /= N_Package_Specification then
1857 if Nkind (Parent (L)) = N_Package_Body then
1858 Freeze_From := First_Entity (Current_Scope);
1859 end if;
1861 Adjust_D;
1862 Freeze_All (Freeze_From, D);
1863 Freeze_From := Last_Entity (Current_Scope);
1865 elsif Scope (Current_Scope) /= Standard_Standard
1866 and then not Is_Child_Unit (Current_Scope)
1867 and then No (Generic_Parent (Parent (L)))
1868 then
1869 null;
1871 elsif L /= Visible_Declarations (Parent (L))
1872 or else No (Private_Declarations (Parent (L)))
1873 or else Is_Empty_List (Private_Declarations (Parent (L)))
1874 then
1875 Adjust_D;
1876 Freeze_All (Freeze_From, D);
1877 Freeze_From := Last_Entity (Current_Scope);
1878 end if;
1880 -- If next node is a body then freeze all types before the body.
1881 -- An exception occurs for some expander-generated bodies. If these
1882 -- are generated at places where in general language rules would not
1883 -- allow a freeze point, then we assume that the expander has
1884 -- explicitly checked that all required types are properly frozen,
1885 -- and we do not cause general freezing here. This special circuit
1886 -- is used when the encountered body is marked as having already
1887 -- been analyzed.
1889 -- In all other cases (bodies that come from source, and expander
1890 -- generated bodies that have not been analyzed yet), freeze all
1891 -- types now. Note that in the latter case, the expander must take
1892 -- care to attach the bodies at a proper place in the tree so as to
1893 -- not cause unwanted freezing at that point.
1895 elsif not Analyzed (Next_Node)
1896 and then (Nkind_In (Next_Node, N_Subprogram_Body,
1897 N_Entry_Body,
1898 N_Package_Body,
1899 N_Protected_Body,
1900 N_Task_Body)
1901 or else
1902 Nkind (Next_Node) in N_Body_Stub)
1903 then
1904 Adjust_D;
1905 Freeze_All (Freeze_From, D);
1906 Freeze_From := Last_Entity (Current_Scope);
1907 end if;
1909 D := Next_Node;
1910 end loop;
1911 end Analyze_Declarations;
1913 ----------------------------------
1914 -- Analyze_Incomplete_Type_Decl --
1915 ----------------------------------
1917 procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
1918 F : constant Boolean := Is_Pure (Current_Scope);
1919 T : Entity_Id;
1921 begin
1922 Generate_Definition (Defining_Identifier (N));
1924 -- Process an incomplete declaration. The identifier must not have been
1925 -- declared already in the scope. However, an incomplete declaration may
1926 -- appear in the private part of a package, for a private type that has
1927 -- already been declared.
1929 -- In this case, the discriminants (if any) must match
1931 T := Find_Type_Name (N);
1933 Set_Ekind (T, E_Incomplete_Type);
1934 Init_Size_Align (T);
1935 Set_Is_First_Subtype (T, True);
1936 Set_Etype (T, T);
1938 -- Ada 2005 (AI-326): Minimum decoration to give support to tagged
1939 -- incomplete types.
1941 if Tagged_Present (N) then
1942 Set_Is_Tagged_Type (T);
1943 Make_Class_Wide_Type (T);
1944 Set_Primitive_Operations (T, New_Elmt_List);
1945 end if;
1947 Push_Scope (T);
1949 Set_Stored_Constraint (T, No_Elist);
1951 if Present (Discriminant_Specifications (N)) then
1952 Process_Discriminants (N);
1953 end if;
1955 End_Scope;
1957 -- If the type has discriminants, non-trivial subtypes may be be
1958 -- declared before the full view of the type. The full views of those
1959 -- subtypes will be built after the full view of the type.
1961 Set_Private_Dependents (T, New_Elmt_List);
1962 Set_Is_Pure (T, F);
1963 end Analyze_Incomplete_Type_Decl;
1965 -----------------------------------
1966 -- Analyze_Interface_Declaration --
1967 -----------------------------------
1969 procedure Analyze_Interface_Declaration (T : Entity_Id; Def : Node_Id) is
1970 CW : constant Entity_Id := Class_Wide_Type (T);
1972 begin
1973 Set_Is_Tagged_Type (T);
1975 Set_Is_Limited_Record (T, Limited_Present (Def)
1976 or else Task_Present (Def)
1977 or else Protected_Present (Def)
1978 or else Synchronized_Present (Def));
1980 -- Type is abstract if full declaration carries keyword, or if previous
1981 -- partial view did.
1983 Set_Is_Abstract_Type (T);
1984 Set_Is_Interface (T);
1986 -- Type is a limited interface if it includes the keyword limited, task,
1987 -- protected, or synchronized.
1989 Set_Is_Limited_Interface
1990 (T, Limited_Present (Def)
1991 or else Protected_Present (Def)
1992 or else Synchronized_Present (Def)
1993 or else Task_Present (Def));
1995 Set_Is_Protected_Interface (T, Protected_Present (Def));
1996 Set_Is_Task_Interface (T, Task_Present (Def));
1998 -- Type is a synchronized interface if it includes the keyword task,
1999 -- protected, or synchronized.
2001 Set_Is_Synchronized_Interface
2002 (T, Synchronized_Present (Def)
2003 or else Protected_Present (Def)
2004 or else Task_Present (Def));
2006 Set_Interfaces (T, New_Elmt_List);
2007 Set_Primitive_Operations (T, New_Elmt_List);
2009 -- Complete the decoration of the class-wide entity if it was already
2010 -- built (i.e. during the creation of the limited view)
2012 if Present (CW) then
2013 Set_Is_Interface (CW);
2014 Set_Is_Limited_Interface (CW, Is_Limited_Interface (T));
2015 Set_Is_Protected_Interface (CW, Is_Protected_Interface (T));
2016 Set_Is_Synchronized_Interface (CW, Is_Synchronized_Interface (T));
2017 Set_Is_Task_Interface (CW, Is_Task_Interface (T));
2018 end if;
2020 -- Check runtime support for synchronized interfaces
2022 if VM_Target = No_VM
2023 and then (Is_Task_Interface (T)
2024 or else Is_Protected_Interface (T)
2025 or else Is_Synchronized_Interface (T))
2026 and then not RTE_Available (RE_Select_Specific_Data)
2027 then
2028 Error_Msg_CRT ("synchronized interfaces", T);
2029 end if;
2030 end Analyze_Interface_Declaration;
2032 -----------------------------
2033 -- Analyze_Itype_Reference --
2034 -----------------------------
2036 -- Nothing to do. This node is placed in the tree only for the benefit of
2037 -- back end processing, and has no effect on the semantic processing.
2039 procedure Analyze_Itype_Reference (N : Node_Id) is
2040 begin
2041 pragma Assert (Is_Itype (Itype (N)));
2042 null;
2043 end Analyze_Itype_Reference;
2045 --------------------------------
2046 -- Analyze_Number_Declaration --
2047 --------------------------------
2049 procedure Analyze_Number_Declaration (N : Node_Id) is
2050 Id : constant Entity_Id := Defining_Identifier (N);
2051 E : constant Node_Id := Expression (N);
2052 T : Entity_Id;
2053 Index : Interp_Index;
2054 It : Interp;
2056 begin
2057 Generate_Definition (Id);
2058 Enter_Name (Id);
2060 -- This is an optimization of a common case of an integer literal
2062 if Nkind (E) = N_Integer_Literal then
2063 Set_Is_Static_Expression (E, True);
2064 Set_Etype (E, Universal_Integer);
2066 Set_Etype (Id, Universal_Integer);
2067 Set_Ekind (Id, E_Named_Integer);
2068 Set_Is_Frozen (Id, True);
2069 return;
2070 end if;
2072 Set_Is_Pure (Id, Is_Pure (Current_Scope));
2074 -- Process expression, replacing error by integer zero, to avoid
2075 -- cascaded errors or aborts further along in the processing
2077 -- Replace Error by integer zero, which seems least likely to
2078 -- cause cascaded errors.
2080 if E = Error then
2081 Rewrite (E, Make_Integer_Literal (Sloc (E), Uint_0));
2082 Set_Error_Posted (E);
2083 end if;
2085 Analyze (E);
2087 -- Verify that the expression is static and numeric. If
2088 -- the expression is overloaded, we apply the preference
2089 -- rule that favors root numeric types.
2091 if not Is_Overloaded (E) then
2092 T := Etype (E);
2094 else
2095 T := Any_Type;
2097 Get_First_Interp (E, Index, It);
2098 while Present (It.Typ) loop
2099 if (Is_Integer_Type (It.Typ)
2100 or else Is_Real_Type (It.Typ))
2101 and then (Scope (Base_Type (It.Typ))) = Standard_Standard
2102 then
2103 if T = Any_Type then
2104 T := It.Typ;
2106 elsif It.Typ = Universal_Real
2107 or else It.Typ = Universal_Integer
2108 then
2109 -- Choose universal interpretation over any other
2111 T := It.Typ;
2112 exit;
2113 end if;
2114 end if;
2116 Get_Next_Interp (Index, It);
2117 end loop;
2118 end if;
2120 if Is_Integer_Type (T) then
2121 Resolve (E, T);
2122 Set_Etype (Id, Universal_Integer);
2123 Set_Ekind (Id, E_Named_Integer);
2125 elsif Is_Real_Type (T) then
2127 -- Because the real value is converted to universal_real, this is a
2128 -- legal context for a universal fixed expression.
2130 if T = Universal_Fixed then
2131 declare
2132 Loc : constant Source_Ptr := Sloc (N);
2133 Conv : constant Node_Id := Make_Type_Conversion (Loc,
2134 Subtype_Mark =>
2135 New_Occurrence_Of (Universal_Real, Loc),
2136 Expression => Relocate_Node (E));
2138 begin
2139 Rewrite (E, Conv);
2140 Analyze (E);
2141 end;
2143 elsif T = Any_Fixed then
2144 Error_Msg_N ("illegal context for mixed mode operation", E);
2146 -- Expression is of the form : universal_fixed * integer. Try to
2147 -- resolve as universal_real.
2149 T := Universal_Real;
2150 Set_Etype (E, T);
2151 end if;
2153 Resolve (E, T);
2154 Set_Etype (Id, Universal_Real);
2155 Set_Ekind (Id, E_Named_Real);
2157 else
2158 Wrong_Type (E, Any_Numeric);
2159 Resolve (E, T);
2161 Set_Etype (Id, T);
2162 Set_Ekind (Id, E_Constant);
2163 Set_Never_Set_In_Source (Id, True);
2164 Set_Is_True_Constant (Id, True);
2165 return;
2166 end if;
2168 if Nkind_In (E, N_Integer_Literal, N_Real_Literal) then
2169 Set_Etype (E, Etype (Id));
2170 end if;
2172 if not Is_OK_Static_Expression (E) then
2173 Flag_Non_Static_Expr
2174 ("non-static expression used in number declaration!", E);
2175 Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
2176 Set_Etype (E, Any_Type);
2177 end if;
2178 end Analyze_Number_Declaration;
2180 --------------------------------
2181 -- Analyze_Object_Declaration --
2182 --------------------------------
2184 procedure Analyze_Object_Declaration (N : Node_Id) is
2185 Loc : constant Source_Ptr := Sloc (N);
2186 Id : constant Entity_Id := Defining_Identifier (N);
2187 T : Entity_Id;
2188 Act_T : Entity_Id;
2190 E : Node_Id := Expression (N);
2191 -- E is set to Expression (N) throughout this routine. When
2192 -- Expression (N) is modified, E is changed accordingly.
2194 Prev_Entity : Entity_Id := Empty;
2196 function Count_Tasks (T : Entity_Id) return Uint;
2197 -- This function is called when a non-generic library level object of a
2198 -- task type is declared. Its function is to count the static number of
2199 -- tasks declared within the type (it is only called if Has_Tasks is set
2200 -- for T). As a side effect, if an array of tasks with non-static bounds
2201 -- or a variant record type is encountered, Check_Restrictions is called
2202 -- indicating the count is unknown.
2204 -----------------
2205 -- Count_Tasks --
2206 -----------------
2208 function Count_Tasks (T : Entity_Id) return Uint is
2209 C : Entity_Id;
2210 X : Node_Id;
2211 V : Uint;
2213 begin
2214 if Is_Task_Type (T) then
2215 return Uint_1;
2217 elsif Is_Record_Type (T) then
2218 if Has_Discriminants (T) then
2219 Check_Restriction (Max_Tasks, N);
2220 return Uint_0;
2222 else
2223 V := Uint_0;
2224 C := First_Component (T);
2225 while Present (C) loop
2226 V := V + Count_Tasks (Etype (C));
2227 Next_Component (C);
2228 end loop;
2230 return V;
2231 end if;
2233 elsif Is_Array_Type (T) then
2234 X := First_Index (T);
2235 V := Count_Tasks (Component_Type (T));
2236 while Present (X) loop
2237 C := Etype (X);
2239 if not Is_Static_Subtype (C) then
2240 Check_Restriction (Max_Tasks, N);
2241 return Uint_0;
2242 else
2243 V := V * (UI_Max (Uint_0,
2244 Expr_Value (Type_High_Bound (C)) -
2245 Expr_Value (Type_Low_Bound (C)) + Uint_1));
2246 end if;
2248 Next_Index (X);
2249 end loop;
2251 return V;
2253 else
2254 return Uint_0;
2255 end if;
2256 end Count_Tasks;
2258 -- Start of processing for Analyze_Object_Declaration
2260 begin
2261 -- There are three kinds of implicit types generated by an
2262 -- object declaration:
2264 -- 1. Those for generated by the original Object Definition
2266 -- 2. Those generated by the Expression
2268 -- 3. Those used to constrained the Object Definition with the
2269 -- expression constraints when it is unconstrained
2271 -- They must be generated in this order to avoid order of elaboration
2272 -- issues. Thus the first step (after entering the name) is to analyze
2273 -- the object definition.
2275 if Constant_Present (N) then
2276 Prev_Entity := Current_Entity_In_Scope (Id);
2278 -- If the homograph is an implicit subprogram, it is overridden by
2279 -- the current declaration.
2281 if Present (Prev_Entity)
2282 and then
2283 ((Is_Overloadable (Prev_Entity)
2284 and then Is_Inherited_Operation (Prev_Entity))
2286 -- The current object is a discriminal generated for an entry
2287 -- family index. Even though the index is a constant, in this
2288 -- particular context there is no true constant redeclaration.
2289 -- Enter_Name will handle the visibility.
2291 or else
2292 (Is_Discriminal (Id)
2293 and then Ekind (Discriminal_Link (Id)) =
2294 E_Entry_Index_Parameter))
2295 then
2296 Prev_Entity := Empty;
2297 end if;
2298 end if;
2300 if Present (Prev_Entity) then
2301 Constant_Redeclaration (Id, N, T);
2303 Generate_Reference (Prev_Entity, Id, 'c');
2304 Set_Completion_Referenced (Id);
2306 if Error_Posted (N) then
2308 -- Type mismatch or illegal redeclaration, Do not analyze
2309 -- expression to avoid cascaded errors.
2311 T := Find_Type_Of_Object (Object_Definition (N), N);
2312 Set_Etype (Id, T);
2313 Set_Ekind (Id, E_Variable);
2314 return;
2315 end if;
2317 -- In the normal case, enter identifier at the start to catch premature
2318 -- usage in the initialization expression.
2320 else
2321 Generate_Definition (Id);
2322 Enter_Name (Id);
2324 Mark_Coextensions (N, Object_Definition (N));
2326 T := Find_Type_Of_Object (Object_Definition (N), N);
2328 if Nkind (Object_Definition (N)) = N_Access_Definition
2329 and then Present
2330 (Access_To_Subprogram_Definition (Object_Definition (N)))
2331 and then Protected_Present
2332 (Access_To_Subprogram_Definition (Object_Definition (N)))
2333 then
2334 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
2335 end if;
2337 if Error_Posted (Id) then
2338 Set_Etype (Id, T);
2339 Set_Ekind (Id, E_Variable);
2340 return;
2341 end if;
2342 end if;
2344 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
2345 -- out some static checks
2347 if Ada_Version >= Ada_05
2348 and then Can_Never_Be_Null (T)
2349 then
2350 -- In case of aggregates we must also take care of the correct
2351 -- initialization of nested aggregates bug this is done at the
2352 -- point of the analysis of the aggregate (see sem_aggr.adb)
2354 if Present (Expression (N))
2355 and then Nkind (Expression (N)) = N_Aggregate
2356 then
2357 null;
2359 else
2360 declare
2361 Save_Typ : constant Entity_Id := Etype (Id);
2362 begin
2363 Set_Etype (Id, T); -- Temp. decoration for static checks
2364 Null_Exclusion_Static_Checks (N);
2365 Set_Etype (Id, Save_Typ);
2366 end;
2367 end if;
2368 end if;
2370 Set_Is_Pure (Id, Is_Pure (Current_Scope));
2372 -- If deferred constant, make sure context is appropriate. We detect
2373 -- a deferred constant as a constant declaration with no expression.
2374 -- A deferred constant can appear in a package body if its completion
2375 -- is by means of an interface pragma.
2377 if Constant_Present (N)
2378 and then No (E)
2379 then
2380 -- We exclude forward references to tags
2382 if Is_Imported (Defining_Identifier (N))
2383 and then
2384 (T = RTE (RE_Tag)
2385 or else
2386 (Present (Full_View (T))
2387 and then Full_View (T) = RTE (RE_Tag)))
2388 then
2389 null;
2391 -- A deferred constant may appear in the declarative part of the
2392 -- following constructs:
2394 -- blocks
2395 -- entry bodies
2396 -- extended return statements
2397 -- package specs
2398 -- package bodies
2399 -- subprogram bodies
2400 -- task bodies
2402 -- When declared inside a package spec, a deferred constant must be
2403 -- completed by a full constant declaration or pragma Import. In all
2404 -- other cases, the only proper completion is pragma Import. Extended
2405 -- return statements are flagged as invalid contexts because they do
2406 -- not have a declarative part and so cannot accommodate the pragma.
2408 elsif Ekind (Current_Scope) = E_Return_Statement then
2409 Error_Msg_N
2410 ("invalid context for deferred constant declaration (RM 7.4)",
2412 Error_Msg_N
2413 ("\declaration requires an initialization expression",
2415 Set_Constant_Present (N, False);
2417 -- In Ada 83, deferred constant must be of private type
2419 elsif not Is_Private_Type (T) then
2420 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
2421 Error_Msg_N
2422 ("(Ada 83) deferred constant must be private type", N);
2423 end if;
2424 end if;
2426 -- If not a deferred constant, then object declaration freezes its type
2428 else
2429 Check_Fully_Declared (T, N);
2430 Freeze_Before (N, T);
2431 end if;
2433 -- If the object was created by a constrained array definition, then
2434 -- set the link in both the anonymous base type and anonymous subtype
2435 -- that are built to represent the array type to point to the object.
2437 if Nkind (Object_Definition (Declaration_Node (Id))) =
2438 N_Constrained_Array_Definition
2439 then
2440 Set_Related_Array_Object (T, Id);
2441 Set_Related_Array_Object (Base_Type (T), Id);
2442 end if;
2444 -- Special checks for protected objects not at library level
2446 if Is_Protected_Type (T)
2447 and then not Is_Library_Level_Entity (Id)
2448 then
2449 Check_Restriction (No_Local_Protected_Objects, Id);
2451 -- Protected objects with interrupt handlers must be at library level
2453 -- Ada 2005: this test is not needed (and the corresponding clause
2454 -- in the RM is removed) because accessibility checks are sufficient
2455 -- to make handlers not at the library level illegal.
2457 if Has_Interrupt_Handler (T)
2458 and then Ada_Version < Ada_05
2459 then
2460 Error_Msg_N
2461 ("interrupt object can only be declared at library level", Id);
2462 end if;
2463 end if;
2465 -- The actual subtype of the object is the nominal subtype, unless
2466 -- the nominal one is unconstrained and obtained from the expression.
2468 Act_T := T;
2470 -- Process initialization expression if present and not in error
2472 if Present (E) and then E /= Error then
2474 -- Generate an error in case of CPP class-wide object initialization.
2475 -- Required because otherwise the expansion of the class-wide
2476 -- assignment would try to use 'size to initialize the object
2477 -- (primitive that is not available in CPP tagged types).
2479 if Is_Class_Wide_Type (Act_T)
2480 and then
2481 (Is_CPP_Class (Root_Type (Etype (Act_T)))
2482 or else
2483 (Present (Full_View (Root_Type (Etype (Act_T))))
2484 and then
2485 Is_CPP_Class (Full_View (Root_Type (Etype (Act_T))))))
2486 then
2487 Error_Msg_N
2488 ("predefined assignment not available for 'C'P'P tagged types",
2490 end if;
2492 Mark_Coextensions (N, E);
2493 Analyze (E);
2495 -- In case of errors detected in the analysis of the expression,
2496 -- decorate it with the expected type to avoid cascaded errors
2498 if No (Etype (E)) then
2499 Set_Etype (E, T);
2500 end if;
2502 -- If an initialization expression is present, then we set the
2503 -- Is_True_Constant flag. It will be reset if this is a variable
2504 -- and it is indeed modified.
2506 Set_Is_True_Constant (Id, True);
2508 -- If we are analyzing a constant declaration, set its completion
2509 -- flag after analyzing and resolving the expression.
2511 if Constant_Present (N) then
2512 Set_Has_Completion (Id);
2513 end if;
2515 -- Set type and resolve (type may be overridden later on)
2517 Set_Etype (Id, T);
2518 Resolve (E, T);
2520 -- If the object is an access to variable, the initialization
2521 -- expression cannot be an access to constant.
2523 if Is_Access_Type (T)
2524 and then not Is_Access_Constant (T)
2525 and then Is_Access_Type (Etype (E))
2526 and then Is_Access_Constant (Etype (E))
2527 then
2528 Error_Msg_N
2529 ("object that is an access to variable cannot be initialized " &
2530 "with an access-to-constant expression", E);
2531 end if;
2533 if not Assignment_OK (N) then
2534 Check_Initialization (T, E);
2535 end if;
2537 Check_Unset_Reference (E);
2539 -- If this is a variable, then set current value
2541 if not Constant_Present (N) then
2542 if Compile_Time_Known_Value (E) then
2543 Set_Current_Value (Id, E);
2544 end if;
2545 end if;
2547 -- Deal with setting of null flags
2549 if Is_Access_Type (T) then
2550 if Known_Non_Null (E) then
2551 Set_Is_Known_Non_Null (Id, True);
2552 elsif Known_Null (E)
2553 and then not Can_Never_Be_Null (Id)
2554 then
2555 Set_Is_Known_Null (Id, True);
2556 end if;
2557 end if;
2559 -- Check incorrect use of dynamically tagged expressions. Note
2560 -- the use of Is_Tagged_Type (T) which seems redundant but is in
2561 -- fact important to avoid spurious errors due to expanded code
2562 -- for dispatching functions over an anonymous access type
2564 if (Is_Class_Wide_Type (Etype (E)) or else Is_Dynamically_Tagged (E))
2565 and then Is_Tagged_Type (T)
2566 and then not Is_Class_Wide_Type (T)
2567 then
2568 Error_Msg_N ("dynamically tagged expression not allowed!", E);
2569 end if;
2571 Apply_Scalar_Range_Check (E, T);
2572 Apply_Static_Length_Check (E, T);
2573 end if;
2575 -- If the No_Streams restriction is set, check that the type of the
2576 -- object is not, and does not contain, any subtype derived from
2577 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
2578 -- Has_Stream just for efficiency reasons. There is no point in
2579 -- spending time on a Has_Stream check if the restriction is not set.
2581 if Restrictions.Set (No_Streams) then
2582 if Has_Stream (T) then
2583 Check_Restriction (No_Streams, N);
2584 end if;
2585 end if;
2587 -- Abstract type is never permitted for a variable or constant.
2588 -- Note: we inhibit this check for objects that do not come from
2589 -- source because there is at least one case (the expansion of
2590 -- x'class'input where x is abstract) where we legitimately
2591 -- generate an abstract object.
2593 if Is_Abstract_Type (T) and then Comes_From_Source (N) then
2594 Error_Msg_N ("type of object cannot be abstract",
2595 Object_Definition (N));
2597 if Is_CPP_Class (T) then
2598 Error_Msg_NE ("\} may need a cpp_constructor",
2599 Object_Definition (N), T);
2600 end if;
2602 -- Case of unconstrained type
2604 elsif Is_Indefinite_Subtype (T) then
2606 -- Nothing to do in deferred constant case
2608 if Constant_Present (N) and then No (E) then
2609 null;
2611 -- Case of no initialization present
2613 elsif No (E) then
2614 if No_Initialization (N) then
2615 null;
2617 elsif Is_Class_Wide_Type (T) then
2618 Error_Msg_N
2619 ("initialization required in class-wide declaration ", N);
2621 else
2622 Error_Msg_N
2623 ("unconstrained subtype not allowed (need initialization)",
2624 Object_Definition (N));
2626 if Is_Record_Type (T) and then Has_Discriminants (T) then
2627 Error_Msg_N
2628 ("\provide initial value or explicit discriminant values",
2629 Object_Definition (N));
2631 Error_Msg_NE
2632 ("\or give default discriminant values for type&",
2633 Object_Definition (N), T);
2635 elsif Is_Array_Type (T) then
2636 Error_Msg_N
2637 ("\provide initial value or explicit array bounds",
2638 Object_Definition (N));
2639 end if;
2640 end if;
2642 -- Case of initialization present but in error. Set initial
2643 -- expression as absent (but do not make above complaints)
2645 elsif E = Error then
2646 Set_Expression (N, Empty);
2647 E := Empty;
2649 -- Case of initialization present
2651 else
2652 -- Not allowed in Ada 83
2654 if not Constant_Present (N) then
2655 if Ada_Version = Ada_83
2656 and then Comes_From_Source (Object_Definition (N))
2657 then
2658 Error_Msg_N
2659 ("(Ada 83) unconstrained variable not allowed",
2660 Object_Definition (N));
2661 end if;
2662 end if;
2664 -- Now we constrain the variable from the initializing expression
2666 -- If the expression is an aggregate, it has been expanded into
2667 -- individual assignments. Retrieve the actual type from the
2668 -- expanded construct.
2670 if Is_Array_Type (T)
2671 and then No_Initialization (N)
2672 and then Nkind (Original_Node (E)) = N_Aggregate
2673 then
2674 Act_T := Etype (E);
2676 else
2677 Expand_Subtype_From_Expr (N, T, Object_Definition (N), E);
2678 Act_T := Find_Type_Of_Object (Object_Definition (N), N);
2679 end if;
2681 Set_Is_Constr_Subt_For_U_Nominal (Act_T);
2683 if Aliased_Present (N) then
2684 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
2685 end if;
2687 Freeze_Before (N, Act_T);
2688 Freeze_Before (N, T);
2689 end if;
2691 elsif Is_Array_Type (T)
2692 and then No_Initialization (N)
2693 and then Nkind (Original_Node (E)) = N_Aggregate
2694 then
2695 if not Is_Entity_Name (Object_Definition (N)) then
2696 Act_T := Etype (E);
2697 Check_Compile_Time_Size (Act_T);
2699 if Aliased_Present (N) then
2700 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
2701 end if;
2702 end if;
2704 -- When the given object definition and the aggregate are specified
2705 -- independently, and their lengths might differ do a length check.
2706 -- This cannot happen if the aggregate is of the form (others =>...)
2708 if not Is_Constrained (T) then
2709 null;
2711 elsif Nkind (E) = N_Raise_Constraint_Error then
2713 -- Aggregate is statically illegal. Place back in declaration
2715 Set_Expression (N, E);
2716 Set_No_Initialization (N, False);
2718 elsif T = Etype (E) then
2719 null;
2721 elsif Nkind (E) = N_Aggregate
2722 and then Present (Component_Associations (E))
2723 and then Present (Choices (First (Component_Associations (E))))
2724 and then Nkind (First
2725 (Choices (First (Component_Associations (E))))) = N_Others_Choice
2726 then
2727 null;
2729 else
2730 Apply_Length_Check (E, T);
2731 end if;
2733 -- If the type is limited unconstrained with defaulted discriminants
2734 -- and there is no expression, then the object is constrained by the
2735 -- defaults, so it is worthwhile building the corresponding subtype.
2737 elsif (Is_Limited_Record (T)
2738 or else Is_Concurrent_Type (T))
2739 and then not Is_Constrained (T)
2740 and then Has_Discriminants (T)
2741 then
2742 if No (E) then
2743 Act_T := Build_Default_Subtype (T, N);
2744 else
2745 -- Ada 2005: a limited object may be initialized by means of an
2746 -- aggregate. If the type has default discriminants it has an
2747 -- unconstrained nominal type, Its actual subtype will be obtained
2748 -- from the aggregate, and not from the default discriminants.
2750 Act_T := Etype (E);
2751 end if;
2753 Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
2755 elsif Present (Underlying_Type (T))
2756 and then not Is_Constrained (Underlying_Type (T))
2757 and then Has_Discriminants (Underlying_Type (T))
2758 and then Nkind (E) = N_Function_Call
2759 and then Constant_Present (N)
2760 then
2761 -- The back-end has problems with constants of a discriminated type
2762 -- with defaults, if the initial value is a function call. We
2763 -- generate an intermediate temporary for the result of the call.
2764 -- It is unclear why this should make it acceptable to gcc. ???
2766 Remove_Side_Effects (E);
2767 end if;
2769 -- Check No_Wide_Characters restriction
2771 if T = Standard_Wide_Character
2772 or else T = Standard_Wide_Wide_Character
2773 or else Root_Type (T) = Standard_Wide_String
2774 or else Root_Type (T) = Standard_Wide_Wide_String
2775 then
2776 Check_Restriction (No_Wide_Characters, Object_Definition (N));
2777 end if;
2779 -- Indicate this is not set in source. Certainly true for constants,
2780 -- and true for variables so far (will be reset for a variable if and
2781 -- when we encounter a modification in the source).
2783 Set_Never_Set_In_Source (Id, True);
2785 -- Now establish the proper kind and type of the object
2787 if Constant_Present (N) then
2788 Set_Ekind (Id, E_Constant);
2789 Set_Is_True_Constant (Id, True);
2791 else
2792 Set_Ekind (Id, E_Variable);
2794 -- A variable is set as shared passive if it appears in a shared
2795 -- passive package, and is at the outer level. This is not done
2796 -- for entities generated during expansion, because those are
2797 -- always manipulated locally.
2799 if Is_Shared_Passive (Current_Scope)
2800 and then Is_Library_Level_Entity (Id)
2801 and then Comes_From_Source (Id)
2802 then
2803 Set_Is_Shared_Passive (Id);
2804 Check_Shared_Var (Id, T, N);
2805 end if;
2807 -- Set Has_Initial_Value if initializing expression present. Note
2808 -- that if there is no initializing expression, we leave the state
2809 -- of this flag unchanged (usually it will be False, but notably in
2810 -- the case of exception choice variables, it will already be true).
2812 if Present (E) then
2813 Set_Has_Initial_Value (Id, True);
2814 end if;
2815 end if;
2817 -- Initialize alignment and size and capture alignment setting
2819 Init_Alignment (Id);
2820 Init_Esize (Id);
2821 Set_Optimize_Alignment_Flags (Id);
2823 -- Deal with aliased case
2825 if Aliased_Present (N) then
2826 Set_Is_Aliased (Id);
2828 -- If the object is aliased and the type is unconstrained with
2829 -- defaulted discriminants and there is no expression, then the
2830 -- object is constrained by the defaults, so it is worthwhile
2831 -- building the corresponding subtype.
2833 -- Ada 2005 (AI-363): If the aliased object is discriminated and
2834 -- unconstrained, then only establish an actual subtype if the
2835 -- nominal subtype is indefinite. In definite cases the object is
2836 -- unconstrained in Ada 2005.
2838 if No (E)
2839 and then Is_Record_Type (T)
2840 and then not Is_Constrained (T)
2841 and then Has_Discriminants (T)
2842 and then (Ada_Version < Ada_05 or else Is_Indefinite_Subtype (T))
2843 then
2844 Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
2845 end if;
2846 end if;
2848 -- Now we can set the type of the object
2850 Set_Etype (Id, Act_T);
2852 -- Deal with controlled types
2854 if Has_Controlled_Component (Etype (Id))
2855 or else Is_Controlled (Etype (Id))
2856 then
2857 if not Is_Library_Level_Entity (Id) then
2858 Check_Restriction (No_Nested_Finalization, N);
2859 else
2860 Validate_Controlled_Object (Id);
2861 end if;
2863 -- Generate a warning when an initialization causes an obvious ABE
2864 -- violation. If the init expression is a simple aggregate there
2865 -- shouldn't be any initialize/adjust call generated. This will be
2866 -- true as soon as aggregates are built in place when possible.
2868 -- ??? at the moment we do not generate warnings for temporaries
2869 -- created for those aggregates although Program_Error might be
2870 -- generated if compiled with -gnato.
2872 if Is_Controlled (Etype (Id))
2873 and then Comes_From_Source (Id)
2874 then
2875 declare
2876 BT : constant Entity_Id := Base_Type (Etype (Id));
2878 Implicit_Call : Entity_Id;
2879 pragma Warnings (Off, Implicit_Call);
2880 -- ??? what is this for (never referenced!)
2882 function Is_Aggr (N : Node_Id) return Boolean;
2883 -- Check that N is an aggregate
2885 -------------
2886 -- Is_Aggr --
2887 -------------
2889 function Is_Aggr (N : Node_Id) return Boolean is
2890 begin
2891 case Nkind (Original_Node (N)) is
2892 when N_Aggregate | N_Extension_Aggregate =>
2893 return True;
2895 when N_Qualified_Expression |
2896 N_Type_Conversion |
2897 N_Unchecked_Type_Conversion =>
2898 return Is_Aggr (Expression (Original_Node (N)));
2900 when others =>
2901 return False;
2902 end case;
2903 end Is_Aggr;
2905 begin
2906 -- If no underlying type, we already are in an error situation.
2907 -- Do not try to add a warning since we do not have access to
2908 -- prim-op list.
2910 if No (Underlying_Type (BT)) then
2911 Implicit_Call := Empty;
2913 -- A generic type does not have usable primitive operators.
2914 -- Initialization calls are built for instances.
2916 elsif Is_Generic_Type (BT) then
2917 Implicit_Call := Empty;
2919 -- If the init expression is not an aggregate, an adjust call
2920 -- will be generated
2922 elsif Present (E) and then not Is_Aggr (E) then
2923 Implicit_Call := Find_Prim_Op (BT, Name_Adjust);
2925 -- If no init expression and we are not in the deferred
2926 -- constant case, an Initialize call will be generated
2928 elsif No (E) and then not Constant_Present (N) then
2929 Implicit_Call := Find_Prim_Op (BT, Name_Initialize);
2931 else
2932 Implicit_Call := Empty;
2933 end if;
2934 end;
2935 end if;
2936 end if;
2938 if Has_Task (Etype (Id)) then
2939 Check_Restriction (No_Tasking, N);
2941 -- Deal with counting max tasks
2943 -- Nothing to do if inside a generic
2945 if Inside_A_Generic then
2946 null;
2948 -- If library level entity, then count tasks
2950 elsif Is_Library_Level_Entity (Id) then
2951 Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
2953 -- If not library level entity, then indicate we don't know max
2954 -- tasks and also check task hierarchy restriction and blocking
2955 -- operation (since starting a task is definitely blocking!)
2957 else
2958 Check_Restriction (Max_Tasks, N);
2959 Check_Restriction (No_Task_Hierarchy, N);
2960 Check_Potentially_Blocking_Operation (N);
2961 end if;
2963 -- A rather specialized test. If we see two tasks being declared
2964 -- of the same type in the same object declaration, and the task
2965 -- has an entry with an address clause, we know that program error
2966 -- will be raised at run-time since we can't have two tasks with
2967 -- entries at the same address.
2969 if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
2970 declare
2971 E : Entity_Id;
2973 begin
2974 E := First_Entity (Etype (Id));
2975 while Present (E) loop
2976 if Ekind (E) = E_Entry
2977 and then Present (Get_Attribute_Definition_Clause
2978 (E, Attribute_Address))
2979 then
2980 Error_Msg_N
2981 ("?more than one task with same entry address", N);
2982 Error_Msg_N
2983 ("\?Program_Error will be raised at run time", N);
2984 Insert_Action (N,
2985 Make_Raise_Program_Error (Loc,
2986 Reason => PE_Duplicated_Entry_Address));
2987 exit;
2988 end if;
2990 Next_Entity (E);
2991 end loop;
2992 end;
2993 end if;
2994 end if;
2996 -- Some simple constant-propagation: if the expression is a constant
2997 -- string initialized with a literal, share the literal. This avoids
2998 -- a run-time copy.
3000 if Present (E)
3001 and then Is_Entity_Name (E)
3002 and then Ekind (Entity (E)) = E_Constant
3003 and then Base_Type (Etype (E)) = Standard_String
3004 then
3005 declare
3006 Val : constant Node_Id := Constant_Value (Entity (E));
3007 begin
3008 if Present (Val)
3009 and then Nkind (Val) = N_String_Literal
3010 then
3011 Rewrite (E, New_Copy (Val));
3012 end if;
3013 end;
3014 end if;
3016 -- Another optimization: if the nominal subtype is unconstrained and
3017 -- the expression is a function call that returns an unconstrained
3018 -- type, rewrite the declaration as a renaming of the result of the
3019 -- call. The exceptions below are cases where the copy is expected,
3020 -- either by the back end (Aliased case) or by the semantics, as for
3021 -- initializing controlled types or copying tags for classwide types.
3023 if Present (E)
3024 and then Nkind (E) = N_Explicit_Dereference
3025 and then Nkind (Original_Node (E)) = N_Function_Call
3026 and then not Is_Library_Level_Entity (Id)
3027 and then not Is_Constrained (Underlying_Type (T))
3028 and then not Is_Aliased (Id)
3029 and then not Is_Class_Wide_Type (T)
3030 and then not Is_Controlled (T)
3031 and then not Has_Controlled_Component (Base_Type (T))
3032 and then Expander_Active
3033 then
3034 Rewrite (N,
3035 Make_Object_Renaming_Declaration (Loc,
3036 Defining_Identifier => Id,
3037 Access_Definition => Empty,
3038 Subtype_Mark => New_Occurrence_Of
3039 (Base_Type (Etype (Id)), Loc),
3040 Name => E));
3042 Set_Renamed_Object (Id, E);
3044 -- Force generation of debugging information for the constant and for
3045 -- the renamed function call.
3047 Set_Debug_Info_Needed (Id);
3048 Set_Debug_Info_Needed (Entity (Prefix (E)));
3049 end if;
3051 if Present (Prev_Entity)
3052 and then Is_Frozen (Prev_Entity)
3053 and then not Error_Posted (Id)
3054 then
3055 Error_Msg_N ("full constant declaration appears too late", N);
3056 end if;
3058 Check_Eliminated (Id);
3060 -- Deal with setting In_Private_Part flag if in private part
3062 if Ekind (Scope (Id)) = E_Package
3063 and then In_Private_Part (Scope (Id))
3064 then
3065 Set_In_Private_Part (Id);
3066 end if;
3068 -- Check for violation of No_Local_Timing_Events
3070 if Is_RTE (Etype (Id), RE_Timing_Event)
3071 and then not Is_Library_Level_Entity (Id)
3072 then
3073 Check_Restriction (No_Local_Timing_Events, N);
3074 end if;
3075 end Analyze_Object_Declaration;
3077 ---------------------------
3078 -- Analyze_Others_Choice --
3079 ---------------------------
3081 -- Nothing to do for the others choice node itself, the semantic analysis
3082 -- of the others choice will occur as part of the processing of the parent
3084 procedure Analyze_Others_Choice (N : Node_Id) is
3085 pragma Warnings (Off, N);
3086 begin
3087 null;
3088 end Analyze_Others_Choice;
3090 -------------------------------------------
3091 -- Analyze_Private_Extension_Declaration --
3092 -------------------------------------------
3094 procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
3095 T : constant Entity_Id := Defining_Identifier (N);
3096 Indic : constant Node_Id := Subtype_Indication (N);
3097 Parent_Type : Entity_Id;
3098 Parent_Base : Entity_Id;
3100 begin
3101 -- Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
3103 if Is_Non_Empty_List (Interface_List (N)) then
3104 declare
3105 Intf : Node_Id;
3106 T : Entity_Id;
3108 begin
3109 Intf := First (Interface_List (N));
3110 while Present (Intf) loop
3111 T := Find_Type_Of_Subtype_Indic (Intf);
3113 Diagnose_Interface (Intf, T);
3114 Next (Intf);
3115 end loop;
3116 end;
3117 end if;
3119 Generate_Definition (T);
3120 Enter_Name (T);
3122 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
3123 Parent_Base := Base_Type (Parent_Type);
3125 if Parent_Type = Any_Type
3126 or else Etype (Parent_Type) = Any_Type
3127 then
3128 Set_Ekind (T, Ekind (Parent_Type));
3129 Set_Etype (T, Any_Type);
3130 return;
3132 elsif not Is_Tagged_Type (Parent_Type) then
3133 Error_Msg_N
3134 ("parent of type extension must be a tagged type ", Indic);
3135 return;
3137 elsif Ekind (Parent_Type) = E_Void
3138 or else Ekind (Parent_Type) = E_Incomplete_Type
3139 then
3140 Error_Msg_N ("premature derivation of incomplete type", Indic);
3141 return;
3143 elsif Is_Concurrent_Type (Parent_Type) then
3144 Error_Msg_N
3145 ("parent type of a private extension cannot be "
3146 & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
3148 Set_Etype (T, Any_Type);
3149 Set_Ekind (T, E_Limited_Private_Type);
3150 Set_Private_Dependents (T, New_Elmt_List);
3151 Set_Error_Posted (T);
3152 return;
3153 end if;
3155 -- Perhaps the parent type should be changed to the class-wide type's
3156 -- specific type in this case to prevent cascading errors ???
3158 if Is_Class_Wide_Type (Parent_Type) then
3159 Error_Msg_N
3160 ("parent of type extension must not be a class-wide type", Indic);
3161 return;
3162 end if;
3164 if (not Is_Package_Or_Generic_Package (Current_Scope)
3165 and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
3166 or else In_Private_Part (Current_Scope)
3168 then
3169 Error_Msg_N ("invalid context for private extension", N);
3170 end if;
3172 -- Set common attributes
3174 Set_Is_Pure (T, Is_Pure (Current_Scope));
3175 Set_Scope (T, Current_Scope);
3176 Set_Ekind (T, E_Record_Type_With_Private);
3177 Init_Size_Align (T);
3179 Set_Etype (T, Parent_Base);
3180 Set_Has_Task (T, Has_Task (Parent_Base));
3182 Set_Convention (T, Convention (Parent_Type));
3183 Set_First_Rep_Item (T, First_Rep_Item (Parent_Type));
3184 Set_Is_First_Subtype (T);
3185 Make_Class_Wide_Type (T);
3187 if Unknown_Discriminants_Present (N) then
3188 Set_Discriminant_Constraint (T, No_Elist);
3189 end if;
3191 Build_Derived_Record_Type (N, Parent_Type, T);
3193 -- Ada 2005 (AI-443): Synchronized private extension or a rewritten
3194 -- synchronized formal derived type.
3196 if Ada_Version >= Ada_05
3197 and then Synchronized_Present (N)
3198 then
3199 Set_Is_Limited_Record (T);
3201 -- Formal derived type case
3203 if Is_Generic_Type (T) then
3205 -- The parent must be a tagged limited type or a synchronized
3206 -- interface.
3208 if (not Is_Tagged_Type (Parent_Type)
3209 or else not Is_Limited_Type (Parent_Type))
3210 and then
3211 (not Is_Interface (Parent_Type)
3212 or else not Is_Synchronized_Interface (Parent_Type))
3213 then
3214 Error_Msg_NE ("parent type of & must be tagged limited " &
3215 "or synchronized", N, T);
3216 end if;
3218 -- The progenitors (if any) must be limited or synchronized
3219 -- interfaces.
3221 if Present (Interfaces (T)) then
3222 declare
3223 Iface : Entity_Id;
3224 Iface_Elmt : Elmt_Id;
3226 begin
3227 Iface_Elmt := First_Elmt (Interfaces (T));
3228 while Present (Iface_Elmt) loop
3229 Iface := Node (Iface_Elmt);
3231 if not Is_Limited_Interface (Iface)
3232 and then not Is_Synchronized_Interface (Iface)
3233 then
3234 Error_Msg_NE ("progenitor & must be limited " &
3235 "or synchronized", N, Iface);
3236 end if;
3238 Next_Elmt (Iface_Elmt);
3239 end loop;
3240 end;
3241 end if;
3243 -- Regular derived extension, the parent must be a limited or
3244 -- synchronized interface.
3246 else
3247 if not Is_Interface (Parent_Type)
3248 or else (not Is_Limited_Interface (Parent_Type)
3249 and then
3250 not Is_Synchronized_Interface (Parent_Type))
3251 then
3252 Error_Msg_NE
3253 ("parent type of & must be limited interface", N, T);
3254 end if;
3255 end if;
3257 elsif Limited_Present (N) then
3258 Set_Is_Limited_Record (T);
3260 if not Is_Limited_Type (Parent_Type)
3261 and then
3262 (not Is_Interface (Parent_Type)
3263 or else not Is_Limited_Interface (Parent_Type))
3264 then
3265 Error_Msg_NE ("parent type& of limited extension must be limited",
3266 N, Parent_Type);
3267 end if;
3268 end if;
3269 end Analyze_Private_Extension_Declaration;
3271 ---------------------------------
3272 -- Analyze_Subtype_Declaration --
3273 ---------------------------------
3275 procedure Analyze_Subtype_Declaration
3276 (N : Node_Id;
3277 Skip : Boolean := False)
3279 Id : constant Entity_Id := Defining_Identifier (N);
3280 T : Entity_Id;
3281 R_Checks : Check_Result;
3283 begin
3284 Generate_Definition (Id);
3285 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3286 Init_Size_Align (Id);
3288 -- The following guard condition on Enter_Name is to handle cases where
3289 -- the defining identifier has already been entered into the scope but
3290 -- the declaration as a whole needs to be analyzed.
3292 -- This case in particular happens for derived enumeration types. The
3293 -- derived enumeration type is processed as an inserted enumeration type
3294 -- declaration followed by a rewritten subtype declaration. The defining
3295 -- identifier, however, is entered into the name scope very early in the
3296 -- processing of the original type declaration and therefore needs to be
3297 -- avoided here, when the created subtype declaration is analyzed. (See
3298 -- Build_Derived_Types)
3300 -- This also happens when the full view of a private type is derived
3301 -- type with constraints. In this case the entity has been introduced
3302 -- in the private declaration.
3304 if Skip
3305 or else (Present (Etype (Id))
3306 and then (Is_Private_Type (Etype (Id))
3307 or else Is_Task_Type (Etype (Id))
3308 or else Is_Rewrite_Substitution (N)))
3309 then
3310 null;
3312 else
3313 Enter_Name (Id);
3314 end if;
3316 T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
3318 -- Inherit common attributes
3320 Set_Is_Generic_Type (Id, Is_Generic_Type (Base_Type (T)));
3321 Set_Is_Volatile (Id, Is_Volatile (T));
3322 Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
3323 Set_Is_Atomic (Id, Is_Atomic (T));
3324 Set_Is_Ada_2005_Only (Id, Is_Ada_2005_Only (T));
3325 Set_Convention (Id, Convention (T));
3327 -- In the case where there is no constraint given in the subtype
3328 -- indication, Process_Subtype just returns the Subtype_Mark, so its
3329 -- semantic attributes must be established here.
3331 if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
3332 Set_Etype (Id, Base_Type (T));
3334 case Ekind (T) is
3335 when Array_Kind =>
3336 Set_Ekind (Id, E_Array_Subtype);
3337 Copy_Array_Subtype_Attributes (Id, T);
3339 when Decimal_Fixed_Point_Kind =>
3340 Set_Ekind (Id, E_Decimal_Fixed_Point_Subtype);
3341 Set_Digits_Value (Id, Digits_Value (T));
3342 Set_Delta_Value (Id, Delta_Value (T));
3343 Set_Scale_Value (Id, Scale_Value (T));
3344 Set_Small_Value (Id, Small_Value (T));
3345 Set_Scalar_Range (Id, Scalar_Range (T));
3346 Set_Machine_Radix_10 (Id, Machine_Radix_10 (T));
3347 Set_Is_Constrained (Id, Is_Constrained (T));
3348 Set_RM_Size (Id, RM_Size (T));
3350 when Enumeration_Kind =>
3351 Set_Ekind (Id, E_Enumeration_Subtype);
3352 Set_First_Literal (Id, First_Literal (Base_Type (T)));
3353 Set_Scalar_Range (Id, Scalar_Range (T));
3354 Set_Is_Character_Type (Id, Is_Character_Type (T));
3355 Set_Is_Constrained (Id, Is_Constrained (T));
3356 Set_RM_Size (Id, RM_Size (T));
3358 when Ordinary_Fixed_Point_Kind =>
3359 Set_Ekind (Id, E_Ordinary_Fixed_Point_Subtype);
3360 Set_Scalar_Range (Id, Scalar_Range (T));
3361 Set_Small_Value (Id, Small_Value (T));
3362 Set_Delta_Value (Id, Delta_Value (T));
3363 Set_Is_Constrained (Id, Is_Constrained (T));
3364 Set_RM_Size (Id, RM_Size (T));
3366 when Float_Kind =>
3367 Set_Ekind (Id, E_Floating_Point_Subtype);
3368 Set_Scalar_Range (Id, Scalar_Range (T));
3369 Set_Digits_Value (Id, Digits_Value (T));
3370 Set_Is_Constrained (Id, Is_Constrained (T));
3372 when Signed_Integer_Kind =>
3373 Set_Ekind (Id, E_Signed_Integer_Subtype);
3374 Set_Scalar_Range (Id, Scalar_Range (T));
3375 Set_Is_Constrained (Id, Is_Constrained (T));
3376 Set_RM_Size (Id, RM_Size (T));
3378 when Modular_Integer_Kind =>
3379 Set_Ekind (Id, E_Modular_Integer_Subtype);
3380 Set_Scalar_Range (Id, Scalar_Range (T));
3381 Set_Is_Constrained (Id, Is_Constrained (T));
3382 Set_RM_Size (Id, RM_Size (T));
3384 when Class_Wide_Kind =>
3385 Set_Ekind (Id, E_Class_Wide_Subtype);
3386 Set_First_Entity (Id, First_Entity (T));
3387 Set_Last_Entity (Id, Last_Entity (T));
3388 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
3389 Set_Cloned_Subtype (Id, T);
3390 Set_Is_Tagged_Type (Id, True);
3391 Set_Has_Unknown_Discriminants
3392 (Id, True);
3394 if Ekind (T) = E_Class_Wide_Subtype then
3395 Set_Equivalent_Type (Id, Equivalent_Type (T));
3396 end if;
3398 when E_Record_Type | E_Record_Subtype =>
3399 Set_Ekind (Id, E_Record_Subtype);
3401 if Ekind (T) = E_Record_Subtype
3402 and then Present (Cloned_Subtype (T))
3403 then
3404 Set_Cloned_Subtype (Id, Cloned_Subtype (T));
3405 else
3406 Set_Cloned_Subtype (Id, T);
3407 end if;
3409 Set_First_Entity (Id, First_Entity (T));
3410 Set_Last_Entity (Id, Last_Entity (T));
3411 Set_Has_Discriminants (Id, Has_Discriminants (T));
3412 Set_Is_Constrained (Id, Is_Constrained (T));
3413 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
3414 Set_Has_Unknown_Discriminants
3415 (Id, Has_Unknown_Discriminants (T));
3417 if Has_Discriminants (T) then
3418 Set_Discriminant_Constraint
3419 (Id, Discriminant_Constraint (T));
3420 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
3422 elsif Has_Unknown_Discriminants (Id) then
3423 Set_Discriminant_Constraint (Id, No_Elist);
3424 end if;
3426 if Is_Tagged_Type (T) then
3427 Set_Is_Tagged_Type (Id);
3428 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
3429 Set_Primitive_Operations
3430 (Id, Primitive_Operations (T));
3431 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
3433 if Is_Interface (T) then
3434 Set_Is_Interface (Id);
3435 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
3436 end if;
3437 end if;
3439 when Private_Kind =>
3440 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
3441 Set_Has_Discriminants (Id, Has_Discriminants (T));
3442 Set_Is_Constrained (Id, Is_Constrained (T));
3443 Set_First_Entity (Id, First_Entity (T));
3444 Set_Last_Entity (Id, Last_Entity (T));
3445 Set_Private_Dependents (Id, New_Elmt_List);
3446 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
3447 Set_Has_Unknown_Discriminants
3448 (Id, Has_Unknown_Discriminants (T));
3449 Set_Known_To_Have_Preelab_Init
3450 (Id, Known_To_Have_Preelab_Init (T));
3452 if Is_Tagged_Type (T) then
3453 Set_Is_Tagged_Type (Id);
3454 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
3455 Set_Primitive_Operations (Id, Primitive_Operations (T));
3456 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
3457 end if;
3459 -- In general the attributes of the subtype of a private type
3460 -- are the attributes of the partial view of parent. However,
3461 -- the full view may be a discriminated type, and the subtype
3462 -- must share the discriminant constraint to generate correct
3463 -- calls to initialization procedures.
3465 if Has_Discriminants (T) then
3466 Set_Discriminant_Constraint
3467 (Id, Discriminant_Constraint (T));
3468 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
3470 elsif Present (Full_View (T))
3471 and then Has_Discriminants (Full_View (T))
3472 then
3473 Set_Discriminant_Constraint
3474 (Id, Discriminant_Constraint (Full_View (T)));
3475 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
3477 -- This would seem semantically correct, but apparently
3478 -- confuses the back-end. To be explained and checked with
3479 -- current version ???
3481 -- Set_Has_Discriminants (Id);
3482 end if;
3484 Prepare_Private_Subtype_Completion (Id, N);
3486 when Access_Kind =>
3487 Set_Ekind (Id, E_Access_Subtype);
3488 Set_Is_Constrained (Id, Is_Constrained (T));
3489 Set_Is_Access_Constant
3490 (Id, Is_Access_Constant (T));
3491 Set_Directly_Designated_Type
3492 (Id, Designated_Type (T));
3493 Set_Can_Never_Be_Null (Id, Can_Never_Be_Null (T));
3495 -- A Pure library_item must not contain the declaration of a
3496 -- named access type, except within a subprogram, generic
3497 -- subprogram, task unit, or protected unit (RM 10.2.1(16)).
3499 if Comes_From_Source (Id)
3500 and then In_Pure_Unit
3501 and then not In_Subprogram_Task_Protected_Unit
3502 then
3503 Error_Msg_N
3504 ("named access types not allowed in pure unit", N);
3505 end if;
3507 when Concurrent_Kind =>
3508 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
3509 Set_Corresponding_Record_Type (Id,
3510 Corresponding_Record_Type (T));
3511 Set_First_Entity (Id, First_Entity (T));
3512 Set_First_Private_Entity (Id, First_Private_Entity (T));
3513 Set_Has_Discriminants (Id, Has_Discriminants (T));
3514 Set_Is_Constrained (Id, Is_Constrained (T));
3515 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
3516 Set_Last_Entity (Id, Last_Entity (T));
3518 if Has_Discriminants (T) then
3519 Set_Discriminant_Constraint (Id,
3520 Discriminant_Constraint (T));
3521 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
3522 end if;
3524 when E_Incomplete_Type =>
3525 if Ada_Version >= Ada_05 then
3526 Set_Ekind (Id, E_Incomplete_Subtype);
3528 -- Ada 2005 (AI-412): Decorate an incomplete subtype
3529 -- of an incomplete type visible through a limited
3530 -- with clause.
3532 if From_With_Type (T)
3533 and then Present (Non_Limited_View (T))
3534 then
3535 Set_From_With_Type (Id);
3536 Set_Non_Limited_View (Id, Non_Limited_View (T));
3538 -- Ada 2005 (AI-412): Add the regular incomplete subtype
3539 -- to the private dependents of the original incomplete
3540 -- type for future transformation.
3542 else
3543 Append_Elmt (Id, Private_Dependents (T));
3544 end if;
3546 -- If the subtype name denotes an incomplete type an error
3547 -- was already reported by Process_Subtype.
3549 else
3550 Set_Etype (Id, Any_Type);
3551 end if;
3553 when others =>
3554 raise Program_Error;
3555 end case;
3556 end if;
3558 if Etype (Id) = Any_Type then
3559 return;
3560 end if;
3562 -- Some common processing on all types
3564 Set_Size_Info (Id, T);
3565 Set_First_Rep_Item (Id, First_Rep_Item (T));
3567 T := Etype (Id);
3569 Set_Is_Immediately_Visible (Id, True);
3570 Set_Depends_On_Private (Id, Has_Private_Component (T));
3571 Set_Is_Descendent_Of_Address (Id, Is_Descendent_Of_Address (T));
3573 if Is_Interface (T) then
3574 Set_Is_Interface (Id);
3575 end if;
3577 if Present (Generic_Parent_Type (N))
3578 and then
3579 (Nkind
3580 (Parent (Generic_Parent_Type (N))) /= N_Formal_Type_Declaration
3581 or else Nkind
3582 (Formal_Type_Definition (Parent (Generic_Parent_Type (N))))
3583 /= N_Formal_Private_Type_Definition)
3584 then
3585 if Is_Tagged_Type (Id) then
3587 -- If this is a generic actual subtype for a synchronized type,
3588 -- the primitive operations are those of the corresponding record
3589 -- for which there is a separate subtype declaration.
3591 if Is_Concurrent_Type (Id) then
3592 null;
3593 elsif Is_Class_Wide_Type (Id) then
3594 Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
3595 else
3596 Derive_Subprograms (Generic_Parent_Type (N), Id, T);
3597 end if;
3599 elsif Scope (Etype (Id)) /= Standard_Standard then
3600 Derive_Subprograms (Generic_Parent_Type (N), Id);
3601 end if;
3602 end if;
3604 if Is_Private_Type (T)
3605 and then Present (Full_View (T))
3606 then
3607 Conditional_Delay (Id, Full_View (T));
3609 -- The subtypes of components or subcomponents of protected types
3610 -- do not need freeze nodes, which would otherwise appear in the
3611 -- wrong scope (before the freeze node for the protected type). The
3612 -- proper subtypes are those of the subcomponents of the corresponding
3613 -- record.
3615 elsif Ekind (Scope (Id)) /= E_Protected_Type
3616 and then Present (Scope (Scope (Id))) -- error defense!
3617 and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
3618 then
3619 Conditional_Delay (Id, T);
3620 end if;
3622 -- Check that constraint_error is raised for a scalar subtype
3623 -- indication when the lower or upper bound of a non-null range
3624 -- lies outside the range of the type mark.
3626 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
3627 if Is_Scalar_Type (Etype (Id))
3628 and then Scalar_Range (Id) /=
3629 Scalar_Range (Etype (Subtype_Mark
3630 (Subtype_Indication (N))))
3631 then
3632 Apply_Range_Check
3633 (Scalar_Range (Id),
3634 Etype (Subtype_Mark (Subtype_Indication (N))));
3636 elsif Is_Array_Type (Etype (Id))
3637 and then Present (First_Index (Id))
3638 then
3639 -- This really should be a subprogram that finds the indications
3640 -- to check???
3642 if ((Nkind (First_Index (Id)) = N_Identifier
3643 and then Ekind (Entity (First_Index (Id))) in Scalar_Kind)
3644 or else Nkind (First_Index (Id)) = N_Subtype_Indication)
3645 and then
3646 Nkind (Scalar_Range (Etype (First_Index (Id)))) = N_Range
3647 then
3648 declare
3649 Target_Typ : constant Entity_Id :=
3650 Etype
3651 (First_Index (Etype
3652 (Subtype_Mark (Subtype_Indication (N)))));
3653 begin
3654 R_Checks :=
3655 Get_Range_Checks
3656 (Scalar_Range (Etype (First_Index (Id))),
3657 Target_Typ,
3658 Etype (First_Index (Id)),
3659 Defining_Identifier (N));
3661 Insert_Range_Checks
3662 (R_Checks,
3664 Target_Typ,
3665 Sloc (Defining_Identifier (N)));
3666 end;
3667 end if;
3668 end if;
3669 end if;
3671 Set_Optimize_Alignment_Flags (Id);
3672 Check_Eliminated (Id);
3673 end Analyze_Subtype_Declaration;
3675 --------------------------------
3676 -- Analyze_Subtype_Indication --
3677 --------------------------------
3679 procedure Analyze_Subtype_Indication (N : Node_Id) is
3680 T : constant Entity_Id := Subtype_Mark (N);
3681 R : constant Node_Id := Range_Expression (Constraint (N));
3683 begin
3684 Analyze (T);
3686 if R /= Error then
3687 Analyze (R);
3688 Set_Etype (N, Etype (R));
3689 Resolve (R, Entity (T));
3690 else
3691 Set_Error_Posted (R);
3692 Set_Error_Posted (T);
3693 end if;
3694 end Analyze_Subtype_Indication;
3696 ------------------------------
3697 -- Analyze_Type_Declaration --
3698 ------------------------------
3700 procedure Analyze_Type_Declaration (N : Node_Id) is
3701 Def : constant Node_Id := Type_Definition (N);
3702 Def_Id : constant Entity_Id := Defining_Identifier (N);
3703 T : Entity_Id;
3704 Prev : Entity_Id;
3706 Is_Remote : constant Boolean :=
3707 (Is_Remote_Types (Current_Scope)
3708 or else Is_Remote_Call_Interface (Current_Scope))
3709 and then not (In_Private_Part (Current_Scope)
3710 or else In_Package_Body (Current_Scope));
3712 procedure Check_Ops_From_Incomplete_Type;
3713 -- If there is a tagged incomplete partial view of the type, transfer
3714 -- its operations to the full view, and indicate that the type of the
3715 -- controlling parameter (s) is this full view.
3717 ------------------------------------
3718 -- Check_Ops_From_Incomplete_Type --
3719 ------------------------------------
3721 procedure Check_Ops_From_Incomplete_Type is
3722 Elmt : Elmt_Id;
3723 Formal : Entity_Id;
3724 Op : Entity_Id;
3726 begin
3727 if Prev /= T
3728 and then Ekind (Prev) = E_Incomplete_Type
3729 and then Is_Tagged_Type (Prev)
3730 and then Is_Tagged_Type (T)
3731 then
3732 Elmt := First_Elmt (Primitive_Operations (Prev));
3733 while Present (Elmt) loop
3734 Op := Node (Elmt);
3735 Prepend_Elmt (Op, Primitive_Operations (T));
3737 Formal := First_Formal (Op);
3738 while Present (Formal) loop
3739 if Etype (Formal) = Prev then
3740 Set_Etype (Formal, T);
3741 end if;
3743 Next_Formal (Formal);
3744 end loop;
3746 if Etype (Op) = Prev then
3747 Set_Etype (Op, T);
3748 end if;
3750 Next_Elmt (Elmt);
3751 end loop;
3752 end if;
3753 end Check_Ops_From_Incomplete_Type;
3755 -- Start of processing for Analyze_Type_Declaration
3757 begin
3758 Prev := Find_Type_Name (N);
3760 -- The full view, if present, now points to the current type
3762 -- Ada 2005 (AI-50217): If the type was previously decorated when
3763 -- imported through a LIMITED WITH clause, it appears as incomplete
3764 -- but has no full view.
3765 -- If the incomplete view is tagged, a class_wide type has been
3766 -- created already. Use it for the full view as well, to prevent
3767 -- multiple incompatible class-wide types that may be created for
3768 -- self-referential anonymous access components.
3770 if Ekind (Prev) = E_Incomplete_Type
3771 and then Present (Full_View (Prev))
3772 then
3773 T := Full_View (Prev);
3775 if Is_Tagged_Type (Prev)
3776 and then Present (Class_Wide_Type (Prev))
3777 then
3778 Set_Ekind (T, Ekind (Prev)); -- will be reset later
3779 Set_Class_Wide_Type (T, Class_Wide_Type (Prev));
3780 Set_Etype (Class_Wide_Type (T), T);
3781 end if;
3783 else
3784 T := Prev;
3785 end if;
3787 Set_Is_Pure (T, Is_Pure (Current_Scope));
3789 -- We set the flag Is_First_Subtype here. It is needed to set the
3790 -- corresponding flag for the Implicit class-wide-type created
3791 -- during tagged types processing.
3793 Set_Is_First_Subtype (T, True);
3795 -- Only composite types other than array types are allowed to have
3796 -- discriminants.
3798 case Nkind (Def) is
3800 -- For derived types, the rule will be checked once we've figured
3801 -- out the parent type.
3803 when N_Derived_Type_Definition =>
3804 null;
3806 -- For record types, discriminants are allowed
3808 when N_Record_Definition =>
3809 null;
3811 when others =>
3812 if Present (Discriminant_Specifications (N)) then
3813 Error_Msg_N
3814 ("elementary or array type cannot have discriminants",
3815 Defining_Identifier
3816 (First (Discriminant_Specifications (N))));
3817 end if;
3818 end case;
3820 -- Elaborate the type definition according to kind, and generate
3821 -- subsidiary (implicit) subtypes where needed. We skip this if it was
3822 -- already done (this happens during the reanalysis that follows a call
3823 -- to the high level optimizer).
3825 if not Analyzed (T) then
3826 Set_Analyzed (T);
3828 case Nkind (Def) is
3830 when N_Access_To_Subprogram_Definition =>
3831 Access_Subprogram_Declaration (T, Def);
3833 -- If this is a remote access to subprogram, we must create the
3834 -- equivalent fat pointer type, and related subprograms.
3836 if Is_Remote then
3837 Process_Remote_AST_Declaration (N);
3838 end if;
3840 -- Validate categorization rule against access type declaration
3841 -- usually a violation in Pure unit, Shared_Passive unit.
3843 Validate_Access_Type_Declaration (T, N);
3845 when N_Access_To_Object_Definition =>
3846 Access_Type_Declaration (T, Def);
3848 -- Validate categorization rule against access type declaration
3849 -- usually a violation in Pure unit, Shared_Passive unit.
3851 Validate_Access_Type_Declaration (T, N);
3853 -- If we are in a Remote_Call_Interface package and define a
3854 -- RACW, then calling stubs and specific stream attributes
3855 -- must be added.
3857 if Is_Remote
3858 and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
3859 then
3860 Add_RACW_Features (Def_Id);
3861 end if;
3863 -- Set no strict aliasing flag if config pragma seen
3865 if Opt.No_Strict_Aliasing then
3866 Set_No_Strict_Aliasing (Base_Type (Def_Id));
3867 end if;
3869 when N_Array_Type_Definition =>
3870 Array_Type_Declaration (T, Def);
3872 when N_Derived_Type_Definition =>
3873 Derived_Type_Declaration (T, N, T /= Def_Id);
3875 when N_Enumeration_Type_Definition =>
3876 Enumeration_Type_Declaration (T, Def);
3878 when N_Floating_Point_Definition =>
3879 Floating_Point_Type_Declaration (T, Def);
3881 when N_Decimal_Fixed_Point_Definition =>
3882 Decimal_Fixed_Point_Type_Declaration (T, Def);
3884 when N_Ordinary_Fixed_Point_Definition =>
3885 Ordinary_Fixed_Point_Type_Declaration (T, Def);
3887 when N_Signed_Integer_Type_Definition =>
3888 Signed_Integer_Type_Declaration (T, Def);
3890 when N_Modular_Type_Definition =>
3891 Modular_Type_Declaration (T, Def);
3893 when N_Record_Definition =>
3894 Record_Type_Declaration (T, N, Prev);
3896 when others =>
3897 raise Program_Error;
3899 end case;
3900 end if;
3902 if Etype (T) = Any_Type then
3903 return;
3904 end if;
3906 -- Some common processing for all types
3908 Set_Depends_On_Private (T, Has_Private_Component (T));
3909 Check_Ops_From_Incomplete_Type;
3911 -- Both the declared entity, and its anonymous base type if one
3912 -- was created, need freeze nodes allocated.
3914 declare
3915 B : constant Entity_Id := Base_Type (T);
3917 begin
3918 -- In the case where the base type differs from the first subtype, we
3919 -- pre-allocate a freeze node, and set the proper link to the first
3920 -- subtype. Freeze_Entity will use this preallocated freeze node when
3921 -- it freezes the entity.
3923 if B /= T then
3924 Ensure_Freeze_Node (B);
3925 Set_First_Subtype_Link (Freeze_Node (B), T);
3926 end if;
3928 if not From_With_Type (T) then
3929 Set_Has_Delayed_Freeze (T);
3930 end if;
3931 end;
3933 -- Case of T is the full declaration of some private type which has
3934 -- been swapped in Defining_Identifier (N).
3936 if T /= Def_Id and then Is_Private_Type (Def_Id) then
3937 Process_Full_View (N, T, Def_Id);
3939 -- Record the reference. The form of this is a little strange, since
3940 -- the full declaration has been swapped in. So the first parameter
3941 -- here represents the entity to which a reference is made which is
3942 -- the "real" entity, i.e. the one swapped in, and the second
3943 -- parameter provides the reference location.
3945 -- Also, we want to kill Has_Pragma_Unreferenced temporarily here
3946 -- since we don't want a complaint about the full type being an
3947 -- unwanted reference to the private type
3949 declare
3950 B : constant Boolean := Has_Pragma_Unreferenced (T);
3951 begin
3952 Set_Has_Pragma_Unreferenced (T, False);
3953 Generate_Reference (T, T, 'c');
3954 Set_Has_Pragma_Unreferenced (T, B);
3955 end;
3957 Set_Completion_Referenced (Def_Id);
3959 -- For completion of incomplete type, process incomplete dependents
3960 -- and always mark the full type as referenced (it is the incomplete
3961 -- type that we get for any real reference).
3963 elsif Ekind (Prev) = E_Incomplete_Type then
3964 Process_Incomplete_Dependents (N, T, Prev);
3965 Generate_Reference (Prev, Def_Id, 'c');
3966 Set_Completion_Referenced (Def_Id);
3968 -- If not private type or incomplete type completion, this is a real
3969 -- definition of a new entity, so record it.
3971 else
3972 Generate_Definition (Def_Id);
3973 end if;
3975 if Chars (Scope (Def_Id)) = Name_System
3976 and then Chars (Def_Id) = Name_Address
3977 and then Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (N)))
3978 then
3979 Set_Is_Descendent_Of_Address (Def_Id);
3980 Set_Is_Descendent_Of_Address (Base_Type (Def_Id));
3981 Set_Is_Descendent_Of_Address (Prev);
3982 end if;
3984 Set_Optimize_Alignment_Flags (Def_Id);
3985 Check_Eliminated (Def_Id);
3986 end Analyze_Type_Declaration;
3988 --------------------------
3989 -- Analyze_Variant_Part --
3990 --------------------------
3992 procedure Analyze_Variant_Part (N : Node_Id) is
3994 procedure Non_Static_Choice_Error (Choice : Node_Id);
3995 -- Error routine invoked by the generic instantiation below when the
3996 -- variant part has a non static choice.
3998 procedure Process_Declarations (Variant : Node_Id);
3999 -- Analyzes all the declarations associated with a Variant. Needed by
4000 -- the generic instantiation below.
4002 package Variant_Choices_Processing is new
4003 Generic_Choices_Processing
4004 (Get_Alternatives => Variants,
4005 Get_Choices => Discrete_Choices,
4006 Process_Empty_Choice => No_OP,
4007 Process_Non_Static_Choice => Non_Static_Choice_Error,
4008 Process_Associated_Node => Process_Declarations);
4009 use Variant_Choices_Processing;
4010 -- Instantiation of the generic choice processing package
4012 -----------------------------
4013 -- Non_Static_Choice_Error --
4014 -----------------------------
4016 procedure Non_Static_Choice_Error (Choice : Node_Id) is
4017 begin
4018 Flag_Non_Static_Expr
4019 ("choice given in variant part is not static!", Choice);
4020 end Non_Static_Choice_Error;
4022 --------------------------
4023 -- Process_Declarations --
4024 --------------------------
4026 procedure Process_Declarations (Variant : Node_Id) is
4027 begin
4028 if not Null_Present (Component_List (Variant)) then
4029 Analyze_Declarations (Component_Items (Component_List (Variant)));
4031 if Present (Variant_Part (Component_List (Variant))) then
4032 Analyze (Variant_Part (Component_List (Variant)));
4033 end if;
4034 end if;
4035 end Process_Declarations;
4037 -- Local Variables
4039 Discr_Name : Node_Id;
4040 Discr_Type : Entity_Id;
4042 Case_Table : Choice_Table_Type (1 .. Number_Of_Choices (N));
4043 Last_Choice : Nat;
4044 Dont_Care : Boolean;
4045 Others_Present : Boolean := False;
4047 pragma Warnings (Off, Case_Table);
4048 pragma Warnings (Off, Last_Choice);
4049 pragma Warnings (Off, Dont_Care);
4050 pragma Warnings (Off, Others_Present);
4051 -- We don't care about the assigned values of any of these
4053 -- Start of processing for Analyze_Variant_Part
4055 begin
4056 Discr_Name := Name (N);
4057 Analyze (Discr_Name);
4059 -- If Discr_Name bad, get out (prevent cascaded errors)
4061 if Etype (Discr_Name) = Any_Type then
4062 return;
4063 end if;
4065 -- Check invalid discriminant in variant part
4067 if Ekind (Entity (Discr_Name)) /= E_Discriminant then
4068 Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
4069 end if;
4071 Discr_Type := Etype (Entity (Discr_Name));
4073 if not Is_Discrete_Type (Discr_Type) then
4074 Error_Msg_N
4075 ("discriminant in a variant part must be of a discrete type",
4076 Name (N));
4077 return;
4078 end if;
4080 -- Call the instantiated Analyze_Choices which does the rest of the work
4082 Analyze_Choices
4083 (N, Discr_Type, Case_Table, Last_Choice, Dont_Care, Others_Present);
4084 end Analyze_Variant_Part;
4086 ----------------------------
4087 -- Array_Type_Declaration --
4088 ----------------------------
4090 procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
4091 Component_Def : constant Node_Id := Component_Definition (Def);
4092 Element_Type : Entity_Id;
4093 Implicit_Base : Entity_Id;
4094 Index : Node_Id;
4095 Related_Id : Entity_Id := Empty;
4096 Nb_Index : Nat;
4097 P : constant Node_Id := Parent (Def);
4098 Priv : Entity_Id;
4100 begin
4101 if Nkind (Def) = N_Constrained_Array_Definition then
4102 Index := First (Discrete_Subtype_Definitions (Def));
4103 else
4104 Index := First (Subtype_Marks (Def));
4105 end if;
4107 -- Find proper names for the implicit types which may be public. In case
4108 -- of anonymous arrays we use the name of the first object of that type
4109 -- as prefix.
4111 if No (T) then
4112 Related_Id := Defining_Identifier (P);
4113 else
4114 Related_Id := T;
4115 end if;
4117 Nb_Index := 1;
4118 while Present (Index) loop
4119 Analyze (Index);
4121 -- Add a subtype declaration for each index of private array type
4122 -- declaration whose etype is also private. For example:
4124 -- package Pkg is
4125 -- type Index is private;
4126 -- private
4127 -- type Table is array (Index) of ...
4128 -- end;
4130 -- This is currently required by the expander for the internally
4131 -- generated equality subprogram of records with variant parts in
4132 -- which the etype of some component is such private type.
4134 if Ekind (Current_Scope) = E_Package
4135 and then In_Private_Part (Current_Scope)
4136 and then Has_Private_Declaration (Etype (Index))
4137 then
4138 declare
4139 Loc : constant Source_Ptr := Sloc (Def);
4140 New_E : Entity_Id;
4141 Decl : Entity_Id;
4143 begin
4144 New_E :=
4145 Make_Defining_Identifier (Loc,
4146 Chars => New_Internal_Name ('T'));
4147 Set_Is_Internal (New_E);
4149 Decl :=
4150 Make_Subtype_Declaration (Loc,
4151 Defining_Identifier => New_E,
4152 Subtype_Indication =>
4153 New_Occurrence_Of (Etype (Index), Loc));
4155 Insert_Before (Parent (Def), Decl);
4156 Analyze (Decl);
4157 Set_Etype (Index, New_E);
4159 -- If the index is a range the Entity attribute is not
4160 -- available. Example:
4162 -- package Pkg is
4163 -- type T is private;
4164 -- private
4165 -- type T is new Natural;
4166 -- Table : array (T(1) .. T(10)) of Boolean;
4167 -- end Pkg;
4169 if Nkind (Index) /= N_Range then
4170 Set_Entity (Index, New_E);
4171 end if;
4172 end;
4173 end if;
4175 Make_Index (Index, P, Related_Id, Nb_Index);
4176 Next_Index (Index);
4177 Nb_Index := Nb_Index + 1;
4178 end loop;
4180 -- Process subtype indication if one is present
4182 if Present (Subtype_Indication (Component_Def)) then
4183 Element_Type :=
4184 Process_Subtype
4185 (Subtype_Indication (Component_Def), P, Related_Id, 'C');
4187 -- Ada 2005 (AI-230): Access Definition case
4189 else pragma Assert (Present (Access_Definition (Component_Def)));
4191 -- Indicate that the anonymous access type is created by the
4192 -- array type declaration.
4194 Element_Type := Access_Definition
4195 (Related_Nod => P,
4196 N => Access_Definition (Component_Def));
4197 Set_Is_Local_Anonymous_Access (Element_Type);
4199 -- Propagate the parent. This field is needed if we have to generate
4200 -- the master_id associated with an anonymous access to task type
4201 -- component (see Expand_N_Full_Type_Declaration.Build_Master)
4203 Set_Parent (Element_Type, Parent (T));
4205 -- Ada 2005 (AI-230): In case of components that are anonymous access
4206 -- types the level of accessibility depends on the enclosing type
4207 -- declaration
4209 Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
4211 -- Ada 2005 (AI-254)
4213 declare
4214 CD : constant Node_Id :=
4215 Access_To_Subprogram_Definition
4216 (Access_Definition (Component_Def));
4217 begin
4218 if Present (CD) and then Protected_Present (CD) then
4219 Element_Type :=
4220 Replace_Anonymous_Access_To_Protected_Subprogram (Def);
4221 end if;
4222 end;
4223 end if;
4225 -- Constrained array case
4227 if No (T) then
4228 T := Create_Itype (E_Void, P, Related_Id, 'T');
4229 end if;
4231 if Nkind (Def) = N_Constrained_Array_Definition then
4233 -- Establish Implicit_Base as unconstrained base type
4235 Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
4237 Set_Etype (Implicit_Base, Implicit_Base);
4238 Set_Scope (Implicit_Base, Current_Scope);
4239 Set_Has_Delayed_Freeze (Implicit_Base);
4241 -- The constrained array type is a subtype of the unconstrained one
4243 Set_Ekind (T, E_Array_Subtype);
4244 Init_Size_Align (T);
4245 Set_Etype (T, Implicit_Base);
4246 Set_Scope (T, Current_Scope);
4247 Set_Is_Constrained (T, True);
4248 Set_First_Index (T, First (Discrete_Subtype_Definitions (Def)));
4249 Set_Has_Delayed_Freeze (T);
4251 -- Complete setup of implicit base type
4253 Set_First_Index (Implicit_Base, First_Index (T));
4254 Set_Component_Type (Implicit_Base, Element_Type);
4255 Set_Has_Task (Implicit_Base, Has_Task (Element_Type));
4256 Set_Component_Size (Implicit_Base, Uint_0);
4257 Set_Packed_Array_Type (Implicit_Base, Empty);
4258 Set_Has_Controlled_Component
4259 (Implicit_Base, Has_Controlled_Component
4260 (Element_Type)
4261 or else Is_Controlled
4262 (Element_Type));
4263 Set_Finalize_Storage_Only
4264 (Implicit_Base, Finalize_Storage_Only
4265 (Element_Type));
4267 -- Unconstrained array case
4269 else
4270 Set_Ekind (T, E_Array_Type);
4271 Init_Size_Align (T);
4272 Set_Etype (T, T);
4273 Set_Scope (T, Current_Scope);
4274 Set_Component_Size (T, Uint_0);
4275 Set_Is_Constrained (T, False);
4276 Set_First_Index (T, First (Subtype_Marks (Def)));
4277 Set_Has_Delayed_Freeze (T, True);
4278 Set_Has_Task (T, Has_Task (Element_Type));
4279 Set_Has_Controlled_Component (T, Has_Controlled_Component
4280 (Element_Type)
4281 or else
4282 Is_Controlled (Element_Type));
4283 Set_Finalize_Storage_Only (T, Finalize_Storage_Only
4284 (Element_Type));
4285 end if;
4287 -- Common attributes for both cases
4289 Set_Component_Type (Base_Type (T), Element_Type);
4290 Set_Packed_Array_Type (T, Empty);
4292 if Aliased_Present (Component_Definition (Def)) then
4293 Set_Has_Aliased_Components (Etype (T));
4294 end if;
4296 -- Ada 2005 (AI-231): Propagate the null-excluding attribute to the
4297 -- array type to ensure that objects of this type are initialized.
4299 if Ada_Version >= Ada_05
4300 and then Can_Never_Be_Null (Element_Type)
4301 then
4302 Set_Can_Never_Be_Null (T);
4304 if Null_Exclusion_Present (Component_Definition (Def))
4306 -- No need to check itypes because in their case this check was
4307 -- done at their point of creation
4309 and then not Is_Itype (Element_Type)
4310 then
4311 Error_Msg_N
4312 ("`NOT NULL` not allowed (null already excluded)",
4313 Subtype_Indication (Component_Definition (Def)));
4314 end if;
4315 end if;
4317 Priv := Private_Component (Element_Type);
4319 if Present (Priv) then
4321 -- Check for circular definitions
4323 if Priv = Any_Type then
4324 Set_Component_Type (Etype (T), Any_Type);
4326 -- There is a gap in the visibility of operations on the composite
4327 -- type only if the component type is defined in a different scope.
4329 elsif Scope (Priv) = Current_Scope then
4330 null;
4332 elsif Is_Limited_Type (Priv) then
4333 Set_Is_Limited_Composite (Etype (T));
4334 Set_Is_Limited_Composite (T);
4335 else
4336 Set_Is_Private_Composite (Etype (T));
4337 Set_Is_Private_Composite (T);
4338 end if;
4339 end if;
4341 -- A syntax error in the declaration itself may lead to an empty index
4342 -- list, in which case do a minimal patch.
4344 if No (First_Index (T)) then
4345 Error_Msg_N ("missing index definition in array type declaration", T);
4347 declare
4348 Indices : constant List_Id :=
4349 New_List (New_Occurrence_Of (Any_Id, Sloc (T)));
4350 begin
4351 Set_Discrete_Subtype_Definitions (Def, Indices);
4352 Set_First_Index (T, First (Indices));
4353 return;
4354 end;
4355 end if;
4357 -- Create a concatenation operator for the new type. Internal array
4358 -- types created for packed entities do not need such, they are
4359 -- compatible with the user-defined type.
4361 if Number_Dimensions (T) = 1
4362 and then not Is_Packed_Array_Type (T)
4363 then
4364 New_Concatenation_Op (T);
4365 end if;
4367 -- In the case of an unconstrained array the parser has already verified
4368 -- that all the indices are unconstrained but we still need to make sure
4369 -- that the element type is constrained.
4371 if Is_Indefinite_Subtype (Element_Type) then
4372 Error_Msg_N
4373 ("unconstrained element type in array declaration",
4374 Subtype_Indication (Component_Def));
4376 elsif Is_Abstract_Type (Element_Type) then
4377 Error_Msg_N
4378 ("the type of a component cannot be abstract",
4379 Subtype_Indication (Component_Def));
4380 end if;
4381 end Array_Type_Declaration;
4383 ------------------------------------------------------
4384 -- Replace_Anonymous_Access_To_Protected_Subprogram --
4385 ------------------------------------------------------
4387 function Replace_Anonymous_Access_To_Protected_Subprogram
4388 (N : Node_Id) return Entity_Id
4390 Loc : constant Source_Ptr := Sloc (N);
4392 Curr_Scope : constant Scope_Stack_Entry :=
4393 Scope_Stack.Table (Scope_Stack.Last);
4395 Anon : constant Entity_Id :=
4396 Make_Defining_Identifier (Loc,
4397 Chars => New_Internal_Name ('S'));
4399 Acc : Node_Id;
4400 Comp : Node_Id;
4401 Decl : Node_Id;
4402 P : Node_Id;
4404 begin
4405 Set_Is_Internal (Anon);
4407 case Nkind (N) is
4408 when N_Component_Declaration |
4409 N_Unconstrained_Array_Definition |
4410 N_Constrained_Array_Definition =>
4411 Comp := Component_Definition (N);
4412 Acc := Access_Definition (Comp);
4414 when N_Discriminant_Specification =>
4415 Comp := Discriminant_Type (N);
4416 Acc := Comp;
4418 when N_Parameter_Specification =>
4419 Comp := Parameter_Type (N);
4420 Acc := Comp;
4422 when N_Access_Function_Definition =>
4423 Comp := Result_Definition (N);
4424 Acc := Comp;
4426 when N_Object_Declaration =>
4427 Comp := Object_Definition (N);
4428 Acc := Comp;
4430 when others =>
4431 raise Program_Error;
4432 end case;
4434 Decl := Make_Full_Type_Declaration (Loc,
4435 Defining_Identifier => Anon,
4436 Type_Definition =>
4437 Copy_Separate_Tree (Access_To_Subprogram_Definition (Acc)));
4439 Mark_Rewrite_Insertion (Decl);
4441 -- Insert the new declaration in the nearest enclosing scope
4443 P := Parent (N);
4444 while Present (P) and then not Has_Declarations (P) loop
4445 P := Parent (P);
4446 end loop;
4448 pragma Assert (Present (P));
4450 if Nkind (P) = N_Package_Specification then
4451 Prepend (Decl, Visible_Declarations (P));
4452 else
4453 Prepend (Decl, Declarations (P));
4454 end if;
4456 -- Replace the anonymous type with an occurrence of the new declaration.
4457 -- In all cases the rewritten node does not have the null-exclusion
4458 -- attribute because (if present) it was already inherited by the
4459 -- anonymous entity (Anon). Thus, in case of components we do not
4460 -- inherit this attribute.
4462 if Nkind (N) = N_Parameter_Specification then
4463 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
4464 Set_Etype (Defining_Identifier (N), Anon);
4465 Set_Null_Exclusion_Present (N, False);
4467 elsif Nkind (N) = N_Object_Declaration then
4468 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
4469 Set_Etype (Defining_Identifier (N), Anon);
4471 elsif Nkind (N) = N_Access_Function_Definition then
4472 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
4474 else
4475 Rewrite (Comp,
4476 Make_Component_Definition (Loc,
4477 Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
4478 end if;
4480 Mark_Rewrite_Insertion (Comp);
4482 -- Temporarily remove the current scope from the stack to add the new
4483 -- declarations to the enclosing scope
4485 if Nkind_In (N, N_Object_Declaration, N_Access_Function_Definition) then
4486 Analyze (Decl);
4488 else
4489 Scope_Stack.Decrement_Last;
4490 Analyze (Decl);
4491 Set_Is_Itype (Anon);
4492 Scope_Stack.Append (Curr_Scope);
4493 end if;
4495 Set_Ekind (Anon, E_Anonymous_Access_Protected_Subprogram_Type);
4496 Set_Can_Use_Internal_Rep (Anon, not Always_Compatible_Rep_On_Target);
4497 return Anon;
4498 end Replace_Anonymous_Access_To_Protected_Subprogram;
4500 -------------------------------
4501 -- Build_Derived_Access_Type --
4502 -------------------------------
4504 procedure Build_Derived_Access_Type
4505 (N : Node_Id;
4506 Parent_Type : Entity_Id;
4507 Derived_Type : Entity_Id)
4509 S : constant Node_Id := Subtype_Indication (Type_Definition (N));
4511 Desig_Type : Entity_Id;
4512 Discr : Entity_Id;
4513 Discr_Con_Elist : Elist_Id;
4514 Discr_Con_El : Elmt_Id;
4515 Subt : Entity_Id;
4517 begin
4518 -- Set the designated type so it is available in case this is an access
4519 -- to a self-referential type, e.g. a standard list type with a next
4520 -- pointer. Will be reset after subtype is built.
4522 Set_Directly_Designated_Type
4523 (Derived_Type, Designated_Type (Parent_Type));
4525 Subt := Process_Subtype (S, N);
4527 if Nkind (S) /= N_Subtype_Indication
4528 and then Subt /= Base_Type (Subt)
4529 then
4530 Set_Ekind (Derived_Type, E_Access_Subtype);
4531 end if;
4533 if Ekind (Derived_Type) = E_Access_Subtype then
4534 declare
4535 Pbase : constant Entity_Id := Base_Type (Parent_Type);
4536 Ibase : constant Entity_Id :=
4537 Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
4538 Svg_Chars : constant Name_Id := Chars (Ibase);
4539 Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
4541 begin
4542 Copy_Node (Pbase, Ibase);
4544 Set_Chars (Ibase, Svg_Chars);
4545 Set_Next_Entity (Ibase, Svg_Next_E);
4546 Set_Sloc (Ibase, Sloc (Derived_Type));
4547 Set_Scope (Ibase, Scope (Derived_Type));
4548 Set_Freeze_Node (Ibase, Empty);
4549 Set_Is_Frozen (Ibase, False);
4550 Set_Comes_From_Source (Ibase, False);
4551 Set_Is_First_Subtype (Ibase, False);
4553 Set_Etype (Ibase, Pbase);
4554 Set_Etype (Derived_Type, Ibase);
4555 end;
4556 end if;
4558 Set_Directly_Designated_Type
4559 (Derived_Type, Designated_Type (Subt));
4561 Set_Is_Constrained (Derived_Type, Is_Constrained (Subt));
4562 Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
4563 Set_Size_Info (Derived_Type, Parent_Type);
4564 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
4565 Set_Depends_On_Private (Derived_Type,
4566 Has_Private_Component (Derived_Type));
4567 Conditional_Delay (Derived_Type, Subt);
4569 -- Ada 2005 (AI-231). Set the null-exclusion attribute
4571 if Null_Exclusion_Present (Type_Definition (N))
4572 or else Can_Never_Be_Null (Parent_Type)
4573 then
4574 Set_Can_Never_Be_Null (Derived_Type);
4575 end if;
4577 -- Note: we do not copy the Storage_Size_Variable, since we always go to
4578 -- the root type for this information.
4580 -- Apply range checks to discriminants for derived record case
4581 -- ??? THIS CODE SHOULD NOT BE HERE REALLY.
4583 Desig_Type := Designated_Type (Derived_Type);
4584 if Is_Composite_Type (Desig_Type)
4585 and then (not Is_Array_Type (Desig_Type))
4586 and then Has_Discriminants (Desig_Type)
4587 and then Base_Type (Desig_Type) /= Desig_Type
4588 then
4589 Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
4590 Discr_Con_El := First_Elmt (Discr_Con_Elist);
4592 Discr := First_Discriminant (Base_Type (Desig_Type));
4593 while Present (Discr_Con_El) loop
4594 Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
4595 Next_Elmt (Discr_Con_El);
4596 Next_Discriminant (Discr);
4597 end loop;
4598 end if;
4599 end Build_Derived_Access_Type;
4601 ------------------------------
4602 -- Build_Derived_Array_Type --
4603 ------------------------------
4605 procedure Build_Derived_Array_Type
4606 (N : Node_Id;
4607 Parent_Type : Entity_Id;
4608 Derived_Type : Entity_Id)
4610 Loc : constant Source_Ptr := Sloc (N);
4611 Tdef : constant Node_Id := Type_Definition (N);
4612 Indic : constant Node_Id := Subtype_Indication (Tdef);
4613 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
4614 Implicit_Base : Entity_Id;
4615 New_Indic : Node_Id;
4617 procedure Make_Implicit_Base;
4618 -- If the parent subtype is constrained, the derived type is a subtype
4619 -- of an implicit base type derived from the parent base.
4621 ------------------------
4622 -- Make_Implicit_Base --
4623 ------------------------
4625 procedure Make_Implicit_Base is
4626 begin
4627 Implicit_Base :=
4628 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
4630 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
4631 Set_Etype (Implicit_Base, Parent_Base);
4633 Copy_Array_Subtype_Attributes (Implicit_Base, Parent_Base);
4634 Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
4636 Set_Has_Delayed_Freeze (Implicit_Base, True);
4637 end Make_Implicit_Base;
4639 -- Start of processing for Build_Derived_Array_Type
4641 begin
4642 if not Is_Constrained (Parent_Type) then
4643 if Nkind (Indic) /= N_Subtype_Indication then
4644 Set_Ekind (Derived_Type, E_Array_Type);
4646 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
4647 Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
4649 Set_Has_Delayed_Freeze (Derived_Type, True);
4651 else
4652 Make_Implicit_Base;
4653 Set_Etype (Derived_Type, Implicit_Base);
4655 New_Indic :=
4656 Make_Subtype_Declaration (Loc,
4657 Defining_Identifier => Derived_Type,
4658 Subtype_Indication =>
4659 Make_Subtype_Indication (Loc,
4660 Subtype_Mark => New_Reference_To (Implicit_Base, Loc),
4661 Constraint => Constraint (Indic)));
4663 Rewrite (N, New_Indic);
4664 Analyze (N);
4665 end if;
4667 else
4668 if Nkind (Indic) /= N_Subtype_Indication then
4669 Make_Implicit_Base;
4671 Set_Ekind (Derived_Type, Ekind (Parent_Type));
4672 Set_Etype (Derived_Type, Implicit_Base);
4673 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
4675 else
4676 Error_Msg_N ("illegal constraint on constrained type", Indic);
4677 end if;
4678 end if;
4680 -- If parent type is not a derived type itself, and is declared in
4681 -- closed scope (e.g. a subprogram), then we must explicitly introduce
4682 -- the new type's concatenation operator since Derive_Subprograms
4683 -- will not inherit the parent's operator. If the parent type is
4684 -- unconstrained, the operator is of the unconstrained base type.
4686 if Number_Dimensions (Parent_Type) = 1
4687 and then not Is_Limited_Type (Parent_Type)
4688 and then not Is_Derived_Type (Parent_Type)
4689 and then not Is_Package_Or_Generic_Package
4690 (Scope (Base_Type (Parent_Type)))
4691 then
4692 if not Is_Constrained (Parent_Type)
4693 and then Is_Constrained (Derived_Type)
4694 then
4695 New_Concatenation_Op (Implicit_Base);
4696 else
4697 New_Concatenation_Op (Derived_Type);
4698 end if;
4699 end if;
4700 end Build_Derived_Array_Type;
4702 -----------------------------------
4703 -- Build_Derived_Concurrent_Type --
4704 -----------------------------------
4706 procedure Build_Derived_Concurrent_Type
4707 (N : Node_Id;
4708 Parent_Type : Entity_Id;
4709 Derived_Type : Entity_Id)
4711 D_Constraint : Node_Id;
4712 Disc_Spec : Node_Id;
4713 Old_Disc : Entity_Id;
4714 New_Disc : Entity_Id;
4716 Constraint_Present : constant Boolean :=
4717 Nkind (Subtype_Indication (Type_Definition (N)))
4718 = N_Subtype_Indication;
4720 begin
4721 Set_Stored_Constraint (Derived_Type, No_Elist);
4723 -- Copy Storage_Size and Relative_Deadline variables if task case
4725 if Is_Task_Type (Parent_Type) then
4726 Set_Storage_Size_Variable (Derived_Type,
4727 Storage_Size_Variable (Parent_Type));
4728 Set_Relative_Deadline_Variable (Derived_Type,
4729 Relative_Deadline_Variable (Parent_Type));
4730 end if;
4732 if Present (Discriminant_Specifications (N)) then
4733 Push_Scope (Derived_Type);
4734 Check_Or_Process_Discriminants (N, Derived_Type);
4735 End_Scope;
4737 elsif Constraint_Present then
4739 -- Build constrained subtype and derive from it
4741 declare
4742 Loc : constant Source_Ptr := Sloc (N);
4743 Anon : constant Entity_Id :=
4744 Make_Defining_Identifier (Loc,
4745 New_External_Name (Chars (Derived_Type), 'T'));
4746 Decl : Node_Id;
4748 begin
4749 Decl :=
4750 Make_Subtype_Declaration (Loc,
4751 Defining_Identifier => Anon,
4752 Subtype_Indication =>
4753 Subtype_Indication (Type_Definition (N)));
4754 Insert_Before (N, Decl);
4755 Analyze (Decl);
4757 Rewrite (Subtype_Indication (Type_Definition (N)),
4758 New_Occurrence_Of (Anon, Loc));
4759 Set_Analyzed (Derived_Type, False);
4760 Analyze (N);
4761 return;
4762 end;
4763 end if;
4765 -- All attributes are inherited from parent. In particular,
4766 -- entries and the corresponding record type are the same.
4767 -- Discriminants may be renamed, and must be treated separately.
4769 Set_Has_Discriminants
4770 (Derived_Type, Has_Discriminants (Parent_Type));
4771 Set_Corresponding_Record_Type
4772 (Derived_Type, Corresponding_Record_Type (Parent_Type));
4774 -- Is_Constrained is set according the parent subtype, but is set to
4775 -- False if the derived type is declared with new discriminants.
4777 Set_Is_Constrained
4778 (Derived_Type,
4779 (Is_Constrained (Parent_Type) or else Constraint_Present)
4780 and then not Present (Discriminant_Specifications (N)));
4782 if Constraint_Present then
4783 if not Has_Discriminants (Parent_Type) then
4784 Error_Msg_N ("untagged parent must have discriminants", N);
4786 elsif Present (Discriminant_Specifications (N)) then
4788 -- Verify that new discriminants are used to constrain old ones
4790 D_Constraint :=
4791 First
4792 (Constraints
4793 (Constraint (Subtype_Indication (Type_Definition (N)))));
4795 Old_Disc := First_Discriminant (Parent_Type);
4796 New_Disc := First_Discriminant (Derived_Type);
4797 Disc_Spec := First (Discriminant_Specifications (N));
4798 while Present (Old_Disc) and then Present (Disc_Spec) loop
4799 if Nkind (Discriminant_Type (Disc_Spec)) /=
4800 N_Access_Definition
4801 then
4802 Analyze (Discriminant_Type (Disc_Spec));
4804 if not Subtypes_Statically_Compatible (
4805 Etype (Discriminant_Type (Disc_Spec)),
4806 Etype (Old_Disc))
4807 then
4808 Error_Msg_N
4809 ("not statically compatible with parent discriminant",
4810 Discriminant_Type (Disc_Spec));
4811 end if;
4812 end if;
4814 if Nkind (D_Constraint) = N_Identifier
4815 and then Chars (D_Constraint) /=
4816 Chars (Defining_Identifier (Disc_Spec))
4817 then
4818 Error_Msg_N ("new discriminants must constrain old ones",
4819 D_Constraint);
4820 else
4821 Set_Corresponding_Discriminant (New_Disc, Old_Disc);
4822 end if;
4824 Next_Discriminant (Old_Disc);
4825 Next_Discriminant (New_Disc);
4826 Next (Disc_Spec);
4827 end loop;
4829 if Present (Old_Disc) or else Present (Disc_Spec) then
4830 Error_Msg_N ("discriminant mismatch in derivation", N);
4831 end if;
4833 end if;
4835 elsif Present (Discriminant_Specifications (N)) then
4836 Error_Msg_N
4837 ("missing discriminant constraint in untagged derivation",
4839 end if;
4841 if Present (Discriminant_Specifications (N)) then
4842 Old_Disc := First_Discriminant (Parent_Type);
4843 while Present (Old_Disc) loop
4845 if No (Next_Entity (Old_Disc))
4846 or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
4847 then
4848 Set_Next_Entity (Last_Entity (Derived_Type),
4849 Next_Entity (Old_Disc));
4850 exit;
4851 end if;
4853 Next_Discriminant (Old_Disc);
4854 end loop;
4856 else
4857 Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
4858 if Has_Discriminants (Parent_Type) then
4859 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
4860 Set_Discriminant_Constraint (
4861 Derived_Type, Discriminant_Constraint (Parent_Type));
4862 end if;
4863 end if;
4865 Set_Last_Entity (Derived_Type, Last_Entity (Parent_Type));
4867 Set_Has_Completion (Derived_Type);
4868 end Build_Derived_Concurrent_Type;
4870 ------------------------------------
4871 -- Build_Derived_Enumeration_Type --
4872 ------------------------------------
4874 procedure Build_Derived_Enumeration_Type
4875 (N : Node_Id;
4876 Parent_Type : Entity_Id;
4877 Derived_Type : Entity_Id)
4879 Loc : constant Source_Ptr := Sloc (N);
4880 Def : constant Node_Id := Type_Definition (N);
4881 Indic : constant Node_Id := Subtype_Indication (Def);
4882 Implicit_Base : Entity_Id;
4883 Literal : Entity_Id;
4884 New_Lit : Entity_Id;
4885 Literals_List : List_Id;
4886 Type_Decl : Node_Id;
4887 Hi, Lo : Node_Id;
4888 Rang_Expr : Node_Id;
4890 begin
4891 -- Since types Standard.Character and Standard.Wide_Character do
4892 -- not have explicit literals lists we need to process types derived
4893 -- from them specially. This is handled by Derived_Standard_Character.
4894 -- If the parent type is a generic type, there are no literals either,
4895 -- and we construct the same skeletal representation as for the generic
4896 -- parent type.
4898 if Is_Standard_Character_Type (Parent_Type) then
4899 Derived_Standard_Character (N, Parent_Type, Derived_Type);
4901 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
4902 declare
4903 Lo : Node_Id;
4904 Hi : Node_Id;
4906 begin
4907 Lo :=
4908 Make_Attribute_Reference (Loc,
4909 Attribute_Name => Name_First,
4910 Prefix => New_Reference_To (Derived_Type, Loc));
4911 Set_Etype (Lo, Derived_Type);
4913 Hi :=
4914 Make_Attribute_Reference (Loc,
4915 Attribute_Name => Name_Last,
4916 Prefix => New_Reference_To (Derived_Type, Loc));
4917 Set_Etype (Hi, Derived_Type);
4919 Set_Scalar_Range (Derived_Type,
4920 Make_Range (Loc,
4921 Low_Bound => Lo,
4922 High_Bound => Hi));
4923 end;
4925 else
4926 -- If a constraint is present, analyze the bounds to catch
4927 -- premature usage of the derived literals.
4929 if Nkind (Indic) = N_Subtype_Indication
4930 and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
4931 then
4932 Analyze (Low_Bound (Range_Expression (Constraint (Indic))));
4933 Analyze (High_Bound (Range_Expression (Constraint (Indic))));
4934 end if;
4936 -- Introduce an implicit base type for the derived type even if there
4937 -- is no constraint attached to it, since this seems closer to the
4938 -- Ada semantics. Build a full type declaration tree for the derived
4939 -- type using the implicit base type as the defining identifier. The
4940 -- build a subtype declaration tree which applies the constraint (if
4941 -- any) have it replace the derived type declaration.
4943 Literal := First_Literal (Parent_Type);
4944 Literals_List := New_List;
4945 while Present (Literal)
4946 and then Ekind (Literal) = E_Enumeration_Literal
4947 loop
4948 -- Literals of the derived type have the same representation as
4949 -- those of the parent type, but this representation can be
4950 -- overridden by an explicit representation clause. Indicate
4951 -- that there is no explicit representation given yet. These
4952 -- derived literals are implicit operations of the new type,
4953 -- and can be overridden by explicit ones.
4955 if Nkind (Literal) = N_Defining_Character_Literal then
4956 New_Lit :=
4957 Make_Defining_Character_Literal (Loc, Chars (Literal));
4958 else
4959 New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
4960 end if;
4962 Set_Ekind (New_Lit, E_Enumeration_Literal);
4963 Set_Enumeration_Pos (New_Lit, Enumeration_Pos (Literal));
4964 Set_Enumeration_Rep (New_Lit, Enumeration_Rep (Literal));
4965 Set_Enumeration_Rep_Expr (New_Lit, Empty);
4966 Set_Alias (New_Lit, Literal);
4967 Set_Is_Known_Valid (New_Lit, True);
4969 Append (New_Lit, Literals_List);
4970 Next_Literal (Literal);
4971 end loop;
4973 Implicit_Base :=
4974 Make_Defining_Identifier (Sloc (Derived_Type),
4975 New_External_Name (Chars (Derived_Type), 'B'));
4977 -- Indicate the proper nature of the derived type. This must be done
4978 -- before analysis of the literals, to recognize cases when a literal
4979 -- may be hidden by a previous explicit function definition (cf.
4980 -- c83031a).
4982 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
4983 Set_Etype (Derived_Type, Implicit_Base);
4985 Type_Decl :=
4986 Make_Full_Type_Declaration (Loc,
4987 Defining_Identifier => Implicit_Base,
4988 Discriminant_Specifications => No_List,
4989 Type_Definition =>
4990 Make_Enumeration_Type_Definition (Loc, Literals_List));
4992 Mark_Rewrite_Insertion (Type_Decl);
4993 Insert_Before (N, Type_Decl);
4994 Analyze (Type_Decl);
4996 -- After the implicit base is analyzed its Etype needs to be changed
4997 -- to reflect the fact that it is derived from the parent type which
4998 -- was ignored during analysis. We also set the size at this point.
5000 Set_Etype (Implicit_Base, Parent_Type);
5002 Set_Size_Info (Implicit_Base, Parent_Type);
5003 Set_RM_Size (Implicit_Base, RM_Size (Parent_Type));
5004 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
5006 Set_Has_Non_Standard_Rep
5007 (Implicit_Base, Has_Non_Standard_Rep
5008 (Parent_Type));
5009 Set_Has_Delayed_Freeze (Implicit_Base);
5011 -- Process the subtype indication including a validation check on the
5012 -- constraint, if any. If a constraint is given, its bounds must be
5013 -- implicitly converted to the new type.
5015 if Nkind (Indic) = N_Subtype_Indication then
5016 declare
5017 R : constant Node_Id :=
5018 Range_Expression (Constraint (Indic));
5020 begin
5021 if Nkind (R) = N_Range then
5022 Hi := Build_Scalar_Bound
5023 (High_Bound (R), Parent_Type, Implicit_Base);
5024 Lo := Build_Scalar_Bound
5025 (Low_Bound (R), Parent_Type, Implicit_Base);
5027 else
5028 -- Constraint is a Range attribute. Replace with explicit
5029 -- mention of the bounds of the prefix, which must be a
5030 -- subtype.
5032 Analyze (Prefix (R));
5033 Hi :=
5034 Convert_To (Implicit_Base,
5035 Make_Attribute_Reference (Loc,
5036 Attribute_Name => Name_Last,
5037 Prefix =>
5038 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
5040 Lo :=
5041 Convert_To (Implicit_Base,
5042 Make_Attribute_Reference (Loc,
5043 Attribute_Name => Name_First,
5044 Prefix =>
5045 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
5046 end if;
5047 end;
5049 else
5050 Hi :=
5051 Build_Scalar_Bound
5052 (Type_High_Bound (Parent_Type),
5053 Parent_Type, Implicit_Base);
5054 Lo :=
5055 Build_Scalar_Bound
5056 (Type_Low_Bound (Parent_Type),
5057 Parent_Type, Implicit_Base);
5058 end if;
5060 Rang_Expr :=
5061 Make_Range (Loc,
5062 Low_Bound => Lo,
5063 High_Bound => Hi);
5065 -- If we constructed a default range for the case where no range
5066 -- was given, then the expressions in the range must not freeze
5067 -- since they do not correspond to expressions in the source.
5069 if Nkind (Indic) /= N_Subtype_Indication then
5070 Set_Must_Not_Freeze (Lo);
5071 Set_Must_Not_Freeze (Hi);
5072 Set_Must_Not_Freeze (Rang_Expr);
5073 end if;
5075 Rewrite (N,
5076 Make_Subtype_Declaration (Loc,
5077 Defining_Identifier => Derived_Type,
5078 Subtype_Indication =>
5079 Make_Subtype_Indication (Loc,
5080 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
5081 Constraint =>
5082 Make_Range_Constraint (Loc,
5083 Range_Expression => Rang_Expr))));
5085 Analyze (N);
5087 -- If pragma Discard_Names applies on the first subtype of the parent
5088 -- type, then it must be applied on this subtype as well.
5090 if Einfo.Discard_Names (First_Subtype (Parent_Type)) then
5091 Set_Discard_Names (Derived_Type);
5092 end if;
5094 -- Apply a range check. Since this range expression doesn't have an
5095 -- Etype, we have to specifically pass the Source_Typ parameter. Is
5096 -- this right???
5098 if Nkind (Indic) = N_Subtype_Indication then
5099 Apply_Range_Check (Range_Expression (Constraint (Indic)),
5100 Parent_Type,
5101 Source_Typ => Entity (Subtype_Mark (Indic)));
5102 end if;
5103 end if;
5104 end Build_Derived_Enumeration_Type;
5106 --------------------------------
5107 -- Build_Derived_Numeric_Type --
5108 --------------------------------
5110 procedure Build_Derived_Numeric_Type
5111 (N : Node_Id;
5112 Parent_Type : Entity_Id;
5113 Derived_Type : Entity_Id)
5115 Loc : constant Source_Ptr := Sloc (N);
5116 Tdef : constant Node_Id := Type_Definition (N);
5117 Indic : constant Node_Id := Subtype_Indication (Tdef);
5118 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
5119 No_Constraint : constant Boolean := Nkind (Indic) /=
5120 N_Subtype_Indication;
5121 Implicit_Base : Entity_Id;
5123 Lo : Node_Id;
5124 Hi : Node_Id;
5126 begin
5127 -- Process the subtype indication including a validation check on
5128 -- the constraint if any.
5130 Discard_Node (Process_Subtype (Indic, N));
5132 -- Introduce an implicit base type for the derived type even if there
5133 -- is no constraint attached to it, since this seems closer to the Ada
5134 -- semantics.
5136 Implicit_Base :=
5137 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
5139 Set_Etype (Implicit_Base, Parent_Base);
5140 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
5141 Set_Size_Info (Implicit_Base, Parent_Base);
5142 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
5143 Set_Parent (Implicit_Base, Parent (Derived_Type));
5145 -- Set RM Size for discrete type or decimal fixed-point type
5146 -- Ordinary fixed-point is excluded, why???
5148 if Is_Discrete_Type (Parent_Base)
5149 or else Is_Decimal_Fixed_Point_Type (Parent_Base)
5150 then
5151 Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
5152 end if;
5154 Set_Has_Delayed_Freeze (Implicit_Base);
5156 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
5157 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
5159 Set_Scalar_Range (Implicit_Base,
5160 Make_Range (Loc,
5161 Low_Bound => Lo,
5162 High_Bound => Hi));
5164 if Has_Infinities (Parent_Base) then
5165 Set_Includes_Infinities (Scalar_Range (Implicit_Base));
5166 end if;
5168 -- The Derived_Type, which is the entity of the declaration, is a
5169 -- subtype of the implicit base. Its Ekind is a subtype, even in the
5170 -- absence of an explicit constraint.
5172 Set_Etype (Derived_Type, Implicit_Base);
5174 -- If we did not have a constraint, then the Ekind is set from the
5175 -- parent type (otherwise Process_Subtype has set the bounds)
5177 if No_Constraint then
5178 Set_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
5179 end if;
5181 -- If we did not have a range constraint, then set the range from the
5182 -- parent type. Otherwise, the call to Process_Subtype has set the
5183 -- bounds.
5185 if No_Constraint
5186 or else not Has_Range_Constraint (Indic)
5187 then
5188 Set_Scalar_Range (Derived_Type,
5189 Make_Range (Loc,
5190 Low_Bound => New_Copy_Tree (Type_Low_Bound (Parent_Type)),
5191 High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
5192 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
5194 if Has_Infinities (Parent_Type) then
5195 Set_Includes_Infinities (Scalar_Range (Derived_Type));
5196 end if;
5197 end if;
5199 Set_Is_Descendent_Of_Address (Derived_Type,
5200 Is_Descendent_Of_Address (Parent_Type));
5201 Set_Is_Descendent_Of_Address (Implicit_Base,
5202 Is_Descendent_Of_Address (Parent_Type));
5204 -- Set remaining type-specific fields, depending on numeric type
5206 if Is_Modular_Integer_Type (Parent_Type) then
5207 Set_Modulus (Implicit_Base, Modulus (Parent_Base));
5209 Set_Non_Binary_Modulus
5210 (Implicit_Base, Non_Binary_Modulus (Parent_Base));
5212 elsif Is_Floating_Point_Type (Parent_Type) then
5214 -- Digits of base type is always copied from the digits value of
5215 -- the parent base type, but the digits of the derived type will
5216 -- already have been set if there was a constraint present.
5218 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
5219 Set_Vax_Float (Implicit_Base, Vax_Float (Parent_Base));
5221 if No_Constraint then
5222 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
5223 end if;
5225 elsif Is_Fixed_Point_Type (Parent_Type) then
5227 -- Small of base type and derived type are always copied from the
5228 -- parent base type, since smalls never change. The delta of the
5229 -- base type is also copied from the parent base type. However the
5230 -- delta of the derived type will have been set already if a
5231 -- constraint was present.
5233 Set_Small_Value (Derived_Type, Small_Value (Parent_Base));
5234 Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
5235 Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
5237 if No_Constraint then
5238 Set_Delta_Value (Derived_Type, Delta_Value (Parent_Type));
5239 end if;
5241 -- The scale and machine radix in the decimal case are always
5242 -- copied from the parent base type.
5244 if Is_Decimal_Fixed_Point_Type (Parent_Type) then
5245 Set_Scale_Value (Derived_Type, Scale_Value (Parent_Base));
5246 Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
5248 Set_Machine_Radix_10
5249 (Derived_Type, Machine_Radix_10 (Parent_Base));
5250 Set_Machine_Radix_10
5251 (Implicit_Base, Machine_Radix_10 (Parent_Base));
5253 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
5255 if No_Constraint then
5256 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
5258 else
5259 -- the analysis of the subtype_indication sets the
5260 -- digits value of the derived type.
5262 null;
5263 end if;
5264 end if;
5265 end if;
5267 -- The type of the bounds is that of the parent type, and they
5268 -- must be converted to the derived type.
5270 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
5272 -- The implicit_base should be frozen when the derived type is frozen,
5273 -- but note that it is used in the conversions of the bounds. For fixed
5274 -- types we delay the determination of the bounds until the proper
5275 -- freezing point. For other numeric types this is rejected by GCC, for
5276 -- reasons that are currently unclear (???), so we choose to freeze the
5277 -- implicit base now. In the case of integers and floating point types
5278 -- this is harmless because subsequent representation clauses cannot
5279 -- affect anything, but it is still baffling that we cannot use the
5280 -- same mechanism for all derived numeric types.
5282 -- There is a further complication: actually *some* representation
5283 -- clauses can affect the implicit base type. Namely, attribute
5284 -- definition clauses for stream-oriented attributes need to set the
5285 -- corresponding TSS entries on the base type, and this normally cannot
5286 -- be done after the base type is frozen, so the circuitry in
5287 -- Sem_Ch13.New_Stream_Subprogram must account for this possibility and
5288 -- not use Set_TSS in this case.
5290 if Is_Fixed_Point_Type (Parent_Type) then
5291 Conditional_Delay (Implicit_Base, Parent_Type);
5292 else
5293 Freeze_Before (N, Implicit_Base);
5294 end if;
5295 end Build_Derived_Numeric_Type;
5297 --------------------------------
5298 -- Build_Derived_Private_Type --
5299 --------------------------------
5301 procedure Build_Derived_Private_Type
5302 (N : Node_Id;
5303 Parent_Type : Entity_Id;
5304 Derived_Type : Entity_Id;
5305 Is_Completion : Boolean;
5306 Derive_Subps : Boolean := True)
5308 Der_Base : Entity_Id;
5309 Discr : Entity_Id;
5310 Full_Decl : Node_Id := Empty;
5311 Full_Der : Entity_Id;
5312 Full_P : Entity_Id;
5313 Last_Discr : Entity_Id;
5314 Par_Scope : constant Entity_Id := Scope (Base_Type (Parent_Type));
5315 Swapped : Boolean := False;
5317 procedure Copy_And_Build;
5318 -- Copy derived type declaration, replace parent with its full view,
5319 -- and analyze new declaration.
5321 --------------------
5322 -- Copy_And_Build --
5323 --------------------
5325 procedure Copy_And_Build is
5326 Full_N : Node_Id;
5328 begin
5329 if Ekind (Parent_Type) in Record_Kind
5330 or else
5331 (Ekind (Parent_Type) in Enumeration_Kind
5332 and then not Is_Standard_Character_Type (Parent_Type)
5333 and then not Is_Generic_Type (Root_Type (Parent_Type)))
5334 then
5335 Full_N := New_Copy_Tree (N);
5336 Insert_After (N, Full_N);
5337 Build_Derived_Type (
5338 Full_N, Parent_Type, Full_Der, True, Derive_Subps => False);
5340 else
5341 Build_Derived_Type (
5342 N, Parent_Type, Full_Der, True, Derive_Subps => False);
5343 end if;
5344 end Copy_And_Build;
5346 -- Start of processing for Build_Derived_Private_Type
5348 begin
5349 if Is_Tagged_Type (Parent_Type) then
5350 Build_Derived_Record_Type
5351 (N, Parent_Type, Derived_Type, Derive_Subps);
5352 return;
5354 elsif Has_Discriminants (Parent_Type) then
5355 if Present (Full_View (Parent_Type)) then
5356 if not Is_Completion then
5358 -- Copy declaration for subsequent analysis, to provide a
5359 -- completion for what is a private declaration. Indicate that
5360 -- the full type is internally generated.
5362 Full_Decl := New_Copy_Tree (N);
5363 Full_Der := New_Copy (Derived_Type);
5364 Set_Comes_From_Source (Full_Decl, False);
5365 Set_Comes_From_Source (Full_Der, False);
5367 Insert_After (N, Full_Decl);
5369 else
5370 -- If this is a completion, the full view being built is
5371 -- itself private. We build a subtype of the parent with
5372 -- the same constraints as this full view, to convey to the
5373 -- back end the constrained components and the size of this
5374 -- subtype. If the parent is constrained, its full view can
5375 -- serve as the underlying full view of the derived type.
5377 if No (Discriminant_Specifications (N)) then
5378 if Nkind (Subtype_Indication (Type_Definition (N))) =
5379 N_Subtype_Indication
5380 then
5381 Build_Underlying_Full_View (N, Derived_Type, Parent_Type);
5383 elsif Is_Constrained (Full_View (Parent_Type)) then
5384 Set_Underlying_Full_View (Derived_Type,
5385 Full_View (Parent_Type));
5386 end if;
5388 else
5389 -- If there are new discriminants, the parent subtype is
5390 -- constrained by them, but it is not clear how to build
5391 -- the underlying_full_view in this case ???
5393 null;
5394 end if;
5395 end if;
5396 end if;
5398 -- Build partial view of derived type from partial view of parent
5400 Build_Derived_Record_Type
5401 (N, Parent_Type, Derived_Type, Derive_Subps);
5403 if Present (Full_View (Parent_Type))
5404 and then not Is_Completion
5405 then
5406 if not In_Open_Scopes (Par_Scope)
5407 or else not In_Same_Source_Unit (N, Parent_Type)
5408 then
5409 -- Swap partial and full views temporarily
5411 Install_Private_Declarations (Par_Scope);
5412 Install_Visible_Declarations (Par_Scope);
5413 Swapped := True;
5414 end if;
5416 -- Build full view of derived type from full view of parent which
5417 -- is now installed. Subprograms have been derived on the partial
5418 -- view, the completion does not derive them anew.
5420 if not Is_Tagged_Type (Parent_Type) then
5422 -- If the parent is itself derived from another private type,
5423 -- installing the private declarations has not affected its
5424 -- privacy status, so use its own full view explicitly.
5426 if Is_Private_Type (Parent_Type) then
5427 Build_Derived_Record_Type
5428 (Full_Decl, Full_View (Parent_Type), Full_Der, False);
5429 else
5430 Build_Derived_Record_Type
5431 (Full_Decl, Parent_Type, Full_Der, False);
5432 end if;
5434 else
5435 -- If full view of parent is tagged, the completion
5436 -- inherits the proper primitive operations.
5438 Set_Defining_Identifier (Full_Decl, Full_Der);
5439 Build_Derived_Record_Type
5440 (Full_Decl, Parent_Type, Full_Der, Derive_Subps);
5441 Set_Analyzed (Full_Decl);
5442 end if;
5444 if Swapped then
5445 Uninstall_Declarations (Par_Scope);
5447 if In_Open_Scopes (Par_Scope) then
5448 Install_Visible_Declarations (Par_Scope);
5449 end if;
5450 end if;
5452 Der_Base := Base_Type (Derived_Type);
5453 Set_Full_View (Derived_Type, Full_Der);
5454 Set_Full_View (Der_Base, Base_Type (Full_Der));
5456 -- Copy the discriminant list from full view to the partial views
5457 -- (base type and its subtype). Gigi requires that the partial
5458 -- and full views have the same discriminants.
5460 -- Note that since the partial view is pointing to discriminants
5461 -- in the full view, their scope will be that of the full view.
5462 -- This might cause some front end problems and need
5463 -- adjustment???
5465 Discr := First_Discriminant (Base_Type (Full_Der));
5466 Set_First_Entity (Der_Base, Discr);
5468 loop
5469 Last_Discr := Discr;
5470 Next_Discriminant (Discr);
5471 exit when No (Discr);
5472 end loop;
5474 Set_Last_Entity (Der_Base, Last_Discr);
5476 Set_First_Entity (Derived_Type, First_Entity (Der_Base));
5477 Set_Last_Entity (Derived_Type, Last_Entity (Der_Base));
5478 Set_Stored_Constraint (Full_Der, Stored_Constraint (Derived_Type));
5480 else
5481 -- If this is a completion, the derived type stays private
5482 -- and there is no need to create a further full view, except
5483 -- in the unusual case when the derivation is nested within a
5484 -- child unit, see below.
5486 null;
5487 end if;
5489 elsif Present (Full_View (Parent_Type))
5490 and then Has_Discriminants (Full_View (Parent_Type))
5491 then
5492 if Has_Unknown_Discriminants (Parent_Type)
5493 and then Nkind (Subtype_Indication (Type_Definition (N))) =
5494 N_Subtype_Indication
5495 then
5496 Error_Msg_N
5497 ("cannot constrain type with unknown discriminants",
5498 Subtype_Indication (Type_Definition (N)));
5499 return;
5500 end if;
5502 -- If full view of parent is a record type, Build full view as
5503 -- a derivation from the parent's full view. Partial view remains
5504 -- private. For code generation and linking, the full view must
5505 -- have the same public status as the partial one. This full view
5506 -- is only needed if the parent type is in an enclosing scope, so
5507 -- that the full view may actually become visible, e.g. in a child
5508 -- unit. This is both more efficient, and avoids order of freezing
5509 -- problems with the added entities.
5511 if not Is_Private_Type (Full_View (Parent_Type))
5512 and then (In_Open_Scopes (Scope (Parent_Type)))
5513 then
5514 Full_Der := Make_Defining_Identifier (Sloc (Derived_Type),
5515 Chars (Derived_Type));
5516 Set_Is_Itype (Full_Der);
5517 Set_Has_Private_Declaration (Full_Der);
5518 Set_Has_Private_Declaration (Derived_Type);
5519 Set_Associated_Node_For_Itype (Full_Der, N);
5520 Set_Parent (Full_Der, Parent (Derived_Type));
5521 Set_Full_View (Derived_Type, Full_Der);
5522 Set_Is_Public (Full_Der, Is_Public (Derived_Type));
5523 Full_P := Full_View (Parent_Type);
5524 Exchange_Declarations (Parent_Type);
5525 Copy_And_Build;
5526 Exchange_Declarations (Full_P);
5528 else
5529 Build_Derived_Record_Type
5530 (N, Full_View (Parent_Type), Derived_Type,
5531 Derive_Subps => False);
5532 end if;
5534 -- In any case, the primitive operations are inherited from
5535 -- the parent type, not from the internal full view.
5537 Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
5539 if Derive_Subps then
5540 Derive_Subprograms (Parent_Type, Derived_Type);
5541 end if;
5543 else
5544 -- Untagged type, No discriminants on either view
5546 if Nkind (Subtype_Indication (Type_Definition (N))) =
5547 N_Subtype_Indication
5548 then
5549 Error_Msg_N
5550 ("illegal constraint on type without discriminants", N);
5551 end if;
5553 if Present (Discriminant_Specifications (N))
5554 and then Present (Full_View (Parent_Type))
5555 and then not Is_Tagged_Type (Full_View (Parent_Type))
5556 then
5557 Error_Msg_N
5558 ("cannot add discriminants to untagged type", N);
5559 end if;
5561 Set_Stored_Constraint (Derived_Type, No_Elist);
5562 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
5563 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
5564 Set_Has_Controlled_Component
5565 (Derived_Type, Has_Controlled_Component
5566 (Parent_Type));
5568 -- Direct controlled types do not inherit Finalize_Storage_Only flag
5570 if not Is_Controlled (Parent_Type) then
5571 Set_Finalize_Storage_Only
5572 (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
5573 end if;
5575 -- Construct the implicit full view by deriving from full view of
5576 -- the parent type. In order to get proper visibility, we install
5577 -- the parent scope and its declarations.
5579 -- ??? if the parent is untagged private and its completion is
5580 -- tagged, this mechanism will not work because we cannot derive
5581 -- from the tagged full view unless we have an extension
5583 if Present (Full_View (Parent_Type))
5584 and then not Is_Tagged_Type (Full_View (Parent_Type))
5585 and then not Is_Completion
5586 then
5587 Full_Der :=
5588 Make_Defining_Identifier (Sloc (Derived_Type),
5589 Chars => Chars (Derived_Type));
5590 Set_Is_Itype (Full_Der);
5591 Set_Has_Private_Declaration (Full_Der);
5592 Set_Has_Private_Declaration (Derived_Type);
5593 Set_Associated_Node_For_Itype (Full_Der, N);
5594 Set_Parent (Full_Der, Parent (Derived_Type));
5595 Set_Full_View (Derived_Type, Full_Der);
5597 if not In_Open_Scopes (Par_Scope) then
5598 Install_Private_Declarations (Par_Scope);
5599 Install_Visible_Declarations (Par_Scope);
5600 Copy_And_Build;
5601 Uninstall_Declarations (Par_Scope);
5603 -- If parent scope is open and in another unit, and parent has a
5604 -- completion, then the derivation is taking place in the visible
5605 -- part of a child unit. In that case retrieve the full view of
5606 -- the parent momentarily.
5608 elsif not In_Same_Source_Unit (N, Parent_Type) then
5609 Full_P := Full_View (Parent_Type);
5610 Exchange_Declarations (Parent_Type);
5611 Copy_And_Build;
5612 Exchange_Declarations (Full_P);
5614 -- Otherwise it is a local derivation
5616 else
5617 Copy_And_Build;
5618 end if;
5620 Set_Scope (Full_Der, Current_Scope);
5621 Set_Is_First_Subtype (Full_Der,
5622 Is_First_Subtype (Derived_Type));
5623 Set_Has_Size_Clause (Full_Der, False);
5624 Set_Has_Alignment_Clause (Full_Der, False);
5625 Set_Next_Entity (Full_Der, Empty);
5626 Set_Has_Delayed_Freeze (Full_Der);
5627 Set_Is_Frozen (Full_Der, False);
5628 Set_Freeze_Node (Full_Der, Empty);
5629 Set_Depends_On_Private (Full_Der,
5630 Has_Private_Component (Full_Der));
5631 Set_Public_Status (Full_Der);
5632 end if;
5633 end if;
5635 Set_Has_Unknown_Discriminants (Derived_Type,
5636 Has_Unknown_Discriminants (Parent_Type));
5638 if Is_Private_Type (Derived_Type) then
5639 Set_Private_Dependents (Derived_Type, New_Elmt_List);
5640 end if;
5642 if Is_Private_Type (Parent_Type)
5643 and then Base_Type (Parent_Type) = Parent_Type
5644 and then In_Open_Scopes (Scope (Parent_Type))
5645 then
5646 Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
5648 if Is_Child_Unit (Scope (Current_Scope))
5649 and then Is_Completion
5650 and then In_Private_Part (Current_Scope)
5651 and then Scope (Parent_Type) /= Current_Scope
5652 then
5653 -- This is the unusual case where a type completed by a private
5654 -- derivation occurs within a package nested in a child unit,
5655 -- and the parent is declared in an ancestor. In this case, the
5656 -- full view of the parent type will become visible in the body
5657 -- of the enclosing child, and only then will the current type
5658 -- be possibly non-private. We build a underlying full view that
5659 -- will be installed when the enclosing child body is compiled.
5661 Full_Der :=
5662 Make_Defining_Identifier (Sloc (Derived_Type),
5663 Chars => Chars (Derived_Type));
5664 Set_Is_Itype (Full_Der);
5665 Build_Itype_Reference (Full_Der, N);
5667 -- The full view will be used to swap entities on entry/exit to
5668 -- the body, and must appear in the entity list for the package.
5670 Append_Entity (Full_Der, Scope (Derived_Type));
5671 Set_Has_Private_Declaration (Full_Der);
5672 Set_Has_Private_Declaration (Derived_Type);
5673 Set_Associated_Node_For_Itype (Full_Der, N);
5674 Set_Parent (Full_Der, Parent (Derived_Type));
5675 Full_P := Full_View (Parent_Type);
5676 Exchange_Declarations (Parent_Type);
5677 Copy_And_Build;
5678 Exchange_Declarations (Full_P);
5679 Set_Underlying_Full_View (Derived_Type, Full_Der);
5680 end if;
5681 end if;
5682 end Build_Derived_Private_Type;
5684 -------------------------------
5685 -- Build_Derived_Record_Type --
5686 -------------------------------
5688 -- 1. INTRODUCTION
5690 -- Ideally we would like to use the same model of type derivation for
5691 -- tagged and untagged record types. Unfortunately this is not quite
5692 -- possible because the semantics of representation clauses is different
5693 -- for tagged and untagged records under inheritance. Consider the
5694 -- following:
5696 -- type R (...) is [tagged] record ... end record;
5697 -- type T (...) is new R (...) [with ...];
5699 -- The representation clauses for T can specify a completely different
5700 -- record layout from R's. Hence the same component can be placed in two
5701 -- very different positions in objects of type T and R. If R and are tagged
5702 -- types, representation clauses for T can only specify the layout of non
5703 -- inherited components, thus components that are common in R and T have
5704 -- the same position in objects of type R and T.
5706 -- This has two implications. The first is that the entire tree for R's
5707 -- declaration needs to be copied for T in the untagged case, so that T
5708 -- can be viewed as a record type of its own with its own representation
5709 -- clauses. The second implication is the way we handle discriminants.
5710 -- Specifically, in the untagged case we need a way to communicate to Gigi
5711 -- what are the real discriminants in the record, while for the semantics
5712 -- we need to consider those introduced by the user to rename the
5713 -- discriminants in the parent type. This is handled by introducing the
5714 -- notion of stored discriminants. See below for more.
5716 -- Fortunately the way regular components are inherited can be handled in
5717 -- the same way in tagged and untagged types.
5719 -- To complicate things a bit more the private view of a private extension
5720 -- cannot be handled in the same way as the full view (for one thing the
5721 -- semantic rules are somewhat different). We will explain what differs
5722 -- below.
5724 -- 2. DISCRIMINANTS UNDER INHERITANCE
5726 -- The semantic rules governing the discriminants of derived types are
5727 -- quite subtle.
5729 -- type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
5730 -- [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
5732 -- If parent type has discriminants, then the discriminants that are
5733 -- declared in the derived type are [3.4 (11)]:
5735 -- o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
5736 -- there is one;
5738 -- o Otherwise, each discriminant of the parent type (implicitly declared
5739 -- in the same order with the same specifications). In this case, the
5740 -- discriminants are said to be "inherited", or if unknown in the parent
5741 -- are also unknown in the derived type.
5743 -- Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
5745 -- o The parent subtype shall be constrained;
5747 -- o If the parent type is not a tagged type, then each discriminant of
5748 -- the derived type shall be used in the constraint defining a parent
5749 -- subtype. [Implementation note: This ensures that the new discriminant
5750 -- can share storage with an existing discriminant.]
5752 -- For the derived type each discriminant of the parent type is either
5753 -- inherited, constrained to equal some new discriminant of the derived
5754 -- type, or constrained to the value of an expression.
5756 -- When inherited or constrained to equal some new discriminant, the
5757 -- parent discriminant and the discriminant of the derived type are said
5758 -- to "correspond".
5760 -- If a discriminant of the parent type is constrained to a specific value
5761 -- in the derived type definition, then the discriminant is said to be
5762 -- "specified" by that derived type definition.
5764 -- 3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
5766 -- We have spoken about stored discriminants in point 1 (introduction)
5767 -- above. There are two sort of stored discriminants: implicit and
5768 -- explicit. As long as the derived type inherits the same discriminants as
5769 -- the root record type, stored discriminants are the same as regular
5770 -- discriminants, and are said to be implicit. However, if any discriminant
5771 -- in the root type was renamed in the derived type, then the derived
5772 -- type will contain explicit stored discriminants. Explicit stored
5773 -- discriminants are discriminants in addition to the semantically visible
5774 -- discriminants defined for the derived type. Stored discriminants are
5775 -- used by Gigi to figure out what are the physical discriminants in
5776 -- objects of the derived type (see precise definition in einfo.ads).
5777 -- As an example, consider the following:
5779 -- type R (D1, D2, D3 : Int) is record ... end record;
5780 -- type T1 is new R;
5781 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
5782 -- type T3 is new T2;
5783 -- type T4 (Y : Int) is new T3 (Y, 99);
5785 -- The following table summarizes the discriminants and stored
5786 -- discriminants in R and T1 through T4.
5788 -- Type Discrim Stored Discrim Comment
5789 -- R (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in R
5790 -- T1 (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in T1
5791 -- T2 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T2
5792 -- T3 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T3
5793 -- T4 (Y) (D1, D2, D3) Girder discrims EXPLICIT in T4
5795 -- Field Corresponding_Discriminant (abbreviated CD below) allows us to
5796 -- find the corresponding discriminant in the parent type, while
5797 -- Original_Record_Component (abbreviated ORC below), the actual physical
5798 -- component that is renamed. Finally the field Is_Completely_Hidden
5799 -- (abbreviated ICH below) is set for all explicit stored discriminants
5800 -- (see einfo.ads for more info). For the above example this gives:
5802 -- Discrim CD ORC ICH
5803 -- ^^^^^^^ ^^ ^^^ ^^^
5804 -- D1 in R empty itself no
5805 -- D2 in R empty itself no
5806 -- D3 in R empty itself no
5808 -- D1 in T1 D1 in R itself no
5809 -- D2 in T1 D2 in R itself no
5810 -- D3 in T1 D3 in R itself no
5812 -- X1 in T2 D3 in T1 D3 in T2 no
5813 -- X2 in T2 D1 in T1 D1 in T2 no
5814 -- D1 in T2 empty itself yes
5815 -- D2 in T2 empty itself yes
5816 -- D3 in T2 empty itself yes
5818 -- X1 in T3 X1 in T2 D3 in T3 no
5819 -- X2 in T3 X2 in T2 D1 in T3 no
5820 -- D1 in T3 empty itself yes
5821 -- D2 in T3 empty itself yes
5822 -- D3 in T3 empty itself yes
5824 -- Y in T4 X1 in T3 D3 in T3 no
5825 -- D1 in T3 empty itself yes
5826 -- D2 in T3 empty itself yes
5827 -- D3 in T3 empty itself yes
5829 -- 4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
5831 -- Type derivation for tagged types is fairly straightforward. If no
5832 -- discriminants are specified by the derived type, these are inherited
5833 -- from the parent. No explicit stored discriminants are ever necessary.
5834 -- The only manipulation that is done to the tree is that of adding a
5835 -- _parent field with parent type and constrained to the same constraint
5836 -- specified for the parent in the derived type definition. For instance:
5838 -- type R (D1, D2, D3 : Int) is tagged record ... end record;
5839 -- type T1 is new R with null record;
5840 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
5842 -- are changed into:
5844 -- type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
5845 -- _parent : R (D1, D2, D3);
5846 -- end record;
5848 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
5849 -- _parent : T1 (X2, 88, X1);
5850 -- end record;
5852 -- The discriminants actually present in R, T1 and T2 as well as their CD,
5853 -- ORC and ICH fields are:
5855 -- Discrim CD ORC ICH
5856 -- ^^^^^^^ ^^ ^^^ ^^^
5857 -- D1 in R empty itself no
5858 -- D2 in R empty itself no
5859 -- D3 in R empty itself no
5861 -- D1 in T1 D1 in R D1 in R no
5862 -- D2 in T1 D2 in R D2 in R no
5863 -- D3 in T1 D3 in R D3 in R no
5865 -- X1 in T2 D3 in T1 D3 in R no
5866 -- X2 in T2 D1 in T1 D1 in R no
5868 -- 5. FIRST TRANSFORMATION FOR DERIVED RECORDS
5870 -- Regardless of whether we dealing with a tagged or untagged type
5871 -- we will transform all derived type declarations of the form
5873 -- type T is new R (...) [with ...];
5874 -- or
5875 -- subtype S is R (...);
5876 -- type T is new S [with ...];
5877 -- into
5878 -- type BT is new R [with ...];
5879 -- subtype T is BT (...);
5881 -- That is, the base derived type is constrained only if it has no
5882 -- discriminants. The reason for doing this is that GNAT's semantic model
5883 -- assumes that a base type with discriminants is unconstrained.
5885 -- Note that, strictly speaking, the above transformation is not always
5886 -- correct. Consider for instance the following excerpt from ACVC b34011a:
5888 -- procedure B34011A is
5889 -- type REC (D : integer := 0) is record
5890 -- I : Integer;
5891 -- end record;
5893 -- package P is
5894 -- type T6 is new Rec;
5895 -- function F return T6;
5896 -- end P;
5898 -- use P;
5899 -- package Q6 is
5900 -- type U is new T6 (Q6.F.I); -- ERROR: Q6.F.
5901 -- end Q6;
5903 -- The definition of Q6.U is illegal. However transforming Q6.U into
5905 -- type BaseU is new T6;
5906 -- subtype U is BaseU (Q6.F.I)
5908 -- turns U into a legal subtype, which is incorrect. To avoid this problem
5909 -- we always analyze the constraint (in this case (Q6.F.I)) before applying
5910 -- the transformation described above.
5912 -- There is another instance where the above transformation is incorrect.
5913 -- Consider:
5915 -- package Pack is
5916 -- type Base (D : Integer) is tagged null record;
5917 -- procedure P (X : Base);
5919 -- type Der is new Base (2) with null record;
5920 -- procedure P (X : Der);
5921 -- end Pack;
5923 -- Then the above transformation turns this into
5925 -- type Der_Base is new Base with null record;
5926 -- -- procedure P (X : Base) is implicitly inherited here
5927 -- -- as procedure P (X : Der_Base).
5929 -- subtype Der is Der_Base (2);
5930 -- procedure P (X : Der);
5931 -- -- The overriding of P (X : Der_Base) is illegal since we
5932 -- -- have a parameter conformance problem.
5934 -- To get around this problem, after having semantically processed Der_Base
5935 -- and the rewritten subtype declaration for Der, we copy Der_Base field
5936 -- Discriminant_Constraint from Der so that when parameter conformance is
5937 -- checked when P is overridden, no semantic errors are flagged.
5939 -- 6. SECOND TRANSFORMATION FOR DERIVED RECORDS
5941 -- Regardless of whether we are dealing with a tagged or untagged type
5942 -- we will transform all derived type declarations of the form
5944 -- type R (D1, .., Dn : ...) is [tagged] record ...;
5945 -- type T is new R [with ...];
5946 -- into
5947 -- type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
5949 -- The reason for such transformation is that it allows us to implement a
5950 -- very clean form of component inheritance as explained below.
5952 -- Note that this transformation is not achieved by direct tree rewriting
5953 -- and manipulation, but rather by redoing the semantic actions that the
5954 -- above transformation will entail. This is done directly in routine
5955 -- Inherit_Components.
5957 -- 7. TYPE DERIVATION AND COMPONENT INHERITANCE
5959 -- In both tagged and untagged derived types, regular non discriminant
5960 -- components are inherited in the derived type from the parent type. In
5961 -- the absence of discriminants component, inheritance is straightforward
5962 -- as components can simply be copied from the parent.
5964 -- If the parent has discriminants, inheriting components constrained with
5965 -- these discriminants requires caution. Consider the following example:
5967 -- type R (D1, D2 : Positive) is [tagged] record
5968 -- S : String (D1 .. D2);
5969 -- end record;
5971 -- type T1 is new R [with null record];
5972 -- type T2 (X : positive) is new R (1, X) [with null record];
5974 -- As explained in 6. above, T1 is rewritten as
5975 -- type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
5976 -- which makes the treatment for T1 and T2 identical.
5978 -- What we want when inheriting S, is that references to D1 and D2 in R are
5979 -- replaced with references to their correct constraints, i.e. D1 and D2 in
5980 -- T1 and 1 and X in T2. So all R's discriminant references are replaced
5981 -- with either discriminant references in the derived type or expressions.
5982 -- This replacement is achieved as follows: before inheriting R's
5983 -- components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
5984 -- created in the scope of T1 (resp. scope of T2) so that discriminants D1
5985 -- and D2 of T1 are visible (resp. discriminant X of T2 is visible).
5986 -- For T2, for instance, this has the effect of replacing String (D1 .. D2)
5987 -- by String (1 .. X).
5989 -- 8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
5991 -- We explain here the rules governing private type extensions relevant to
5992 -- type derivation. These rules are explained on the following example:
5994 -- type D [(...)] is new A [(...)] with private; <-- partial view
5995 -- type D [(...)] is new P [(...)] with null record; <-- full view
5997 -- Type A is called the ancestor subtype of the private extension.
5998 -- Type P is the parent type of the full view of the private extension. It
5999 -- must be A or a type derived from A.
6001 -- The rules concerning the discriminants of private type extensions are
6002 -- [7.3(10-13)]:
6004 -- o If a private extension inherits known discriminants from the ancestor
6005 -- subtype, then the full view shall also inherit its discriminants from
6006 -- the ancestor subtype and the parent subtype of the full view shall be
6007 -- constrained if and only if the ancestor subtype is constrained.
6009 -- o If a partial view has unknown discriminants, then the full view may
6010 -- define a definite or an indefinite subtype, with or without
6011 -- discriminants.
6013 -- o If a partial view has neither known nor unknown discriminants, then
6014 -- the full view shall define a definite subtype.
6016 -- o If the ancestor subtype of a private extension has constrained
6017 -- discriminants, then the parent subtype of the full view shall impose a
6018 -- statically matching constraint on those discriminants.
6020 -- This means that only the following forms of private extensions are
6021 -- allowed:
6023 -- type D is new A with private; <-- partial view
6024 -- type D is new P with null record; <-- full view
6026 -- If A has no discriminants than P has no discriminants, otherwise P must
6027 -- inherit A's discriminants.
6029 -- type D is new A (...) with private; <-- partial view
6030 -- type D is new P (:::) with null record; <-- full view
6032 -- P must inherit A's discriminants and (...) and (:::) must statically
6033 -- match.
6035 -- subtype A is R (...);
6036 -- type D is new A with private; <-- partial view
6037 -- type D is new P with null record; <-- full view
6039 -- P must have inherited R's discriminants and must be derived from A or
6040 -- any of its subtypes.
6042 -- type D (..) is new A with private; <-- partial view
6043 -- type D (..) is new P [(:::)] with null record; <-- full view
6045 -- No specific constraints on P's discriminants or constraint (:::).
6046 -- Note that A can be unconstrained, but the parent subtype P must either
6047 -- be constrained or (:::) must be present.
6049 -- type D (..) is new A [(...)] with private; <-- partial view
6050 -- type D (..) is new P [(:::)] with null record; <-- full view
6052 -- P's constraints on A's discriminants must statically match those
6053 -- imposed by (...).
6055 -- 9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
6057 -- The full view of a private extension is handled exactly as described
6058 -- above. The model chose for the private view of a private extension is
6059 -- the same for what concerns discriminants (i.e. they receive the same
6060 -- treatment as in the tagged case). However, the private view of the
6061 -- private extension always inherits the components of the parent base,
6062 -- without replacing any discriminant reference. Strictly speaking this is
6063 -- incorrect. However, Gigi never uses this view to generate code so this
6064 -- is a purely semantic issue. In theory, a set of transformations similar
6065 -- to those given in 5. and 6. above could be applied to private views of
6066 -- private extensions to have the same model of component inheritance as
6067 -- for non private extensions. However, this is not done because it would
6068 -- further complicate private type processing. Semantically speaking, this
6069 -- leaves us in an uncomfortable situation. As an example consider:
6071 -- package Pack is
6072 -- type R (D : integer) is tagged record
6073 -- S : String (1 .. D);
6074 -- end record;
6075 -- procedure P (X : R);
6076 -- type T is new R (1) with private;
6077 -- private
6078 -- type T is new R (1) with null record;
6079 -- end;
6081 -- This is transformed into:
6083 -- package Pack is
6084 -- type R (D : integer) is tagged record
6085 -- S : String (1 .. D);
6086 -- end record;
6087 -- procedure P (X : R);
6088 -- type T is new R (1) with private;
6089 -- private
6090 -- type BaseT is new R with null record;
6091 -- subtype T is BaseT (1);
6092 -- end;
6094 -- (strictly speaking the above is incorrect Ada)
6096 -- From the semantic standpoint the private view of private extension T
6097 -- should be flagged as constrained since one can clearly have
6099 -- Obj : T;
6101 -- in a unit withing Pack. However, when deriving subprograms for the
6102 -- private view of private extension T, T must be seen as unconstrained
6103 -- since T has discriminants (this is a constraint of the current
6104 -- subprogram derivation model). Thus, when processing the private view of
6105 -- a private extension such as T, we first mark T as unconstrained, we
6106 -- process it, we perform program derivation and just before returning from
6107 -- Build_Derived_Record_Type we mark T as constrained.
6109 -- ??? Are there are other uncomfortable cases that we will have to
6110 -- deal with.
6112 -- 10. RECORD_TYPE_WITH_PRIVATE complications
6114 -- Types that are derived from a visible record type and have a private
6115 -- extension present other peculiarities. They behave mostly like private
6116 -- types, but if they have primitive operations defined, these will not
6117 -- have the proper signatures for further inheritance, because other
6118 -- primitive operations will use the implicit base that we define for
6119 -- private derivations below. This affect subprogram inheritance (see
6120 -- Derive_Subprograms for details). We also derive the implicit base from
6121 -- the base type of the full view, so that the implicit base is a record
6122 -- type and not another private type, This avoids infinite loops.
6124 procedure Build_Derived_Record_Type
6125 (N : Node_Id;
6126 Parent_Type : Entity_Id;
6127 Derived_Type : Entity_Id;
6128 Derive_Subps : Boolean := True)
6130 Loc : constant Source_Ptr := Sloc (N);
6131 Parent_Base : Entity_Id;
6132 Type_Def : Node_Id;
6133 Indic : Node_Id;
6134 Discrim : Entity_Id;
6135 Last_Discrim : Entity_Id;
6136 Constrs : Elist_Id;
6138 Discs : Elist_Id := New_Elmt_List;
6139 -- An empty Discs list means that there were no constraints in the
6140 -- subtype indication or that there was an error processing it.
6142 Assoc_List : Elist_Id;
6143 New_Discrs : Elist_Id;
6144 New_Base : Entity_Id;
6145 New_Decl : Node_Id;
6146 New_Indic : Node_Id;
6148 Is_Tagged : constant Boolean := Is_Tagged_Type (Parent_Type);
6149 Discriminant_Specs : constant Boolean :=
6150 Present (Discriminant_Specifications (N));
6151 Private_Extension : constant Boolean :=
6152 Nkind (N) = N_Private_Extension_Declaration;
6154 Constraint_Present : Boolean;
6155 Inherit_Discrims : Boolean := False;
6156 Save_Etype : Entity_Id;
6157 Save_Discr_Constr : Elist_Id;
6158 Save_Next_Entity : Entity_Id;
6160 begin
6161 if Ekind (Parent_Type) = E_Record_Type_With_Private
6162 and then Present (Full_View (Parent_Type))
6163 and then Has_Discriminants (Parent_Type)
6164 then
6165 Parent_Base := Base_Type (Full_View (Parent_Type));
6166 else
6167 Parent_Base := Base_Type (Parent_Type);
6168 end if;
6170 -- Before we start the previously documented transformations, here is
6171 -- little fix for size and alignment of tagged types. Normally when we
6172 -- derive type D from type P, we copy the size and alignment of P as the
6173 -- default for D, and in the absence of explicit representation clauses
6174 -- for D, the size and alignment are indeed the same as the parent.
6176 -- But this is wrong for tagged types, since fields may be added, and
6177 -- the default size may need to be larger, and the default alignment may
6178 -- need to be larger.
6180 -- We therefore reset the size and alignment fields in the tagged case.
6181 -- Note that the size and alignment will in any case be at least as
6182 -- large as the parent type (since the derived type has a copy of the
6183 -- parent type in the _parent field)
6185 -- The type is also marked as being tagged here, which is needed when
6186 -- processing components with a self-referential anonymous access type
6187 -- in the call to Check_Anonymous_Access_Components below. Note that
6188 -- this flag is also set later on for completeness.
6190 if Is_Tagged then
6191 Set_Is_Tagged_Type (Derived_Type);
6192 Init_Size_Align (Derived_Type);
6193 end if;
6195 -- STEP 0a: figure out what kind of derived type declaration we have
6197 if Private_Extension then
6198 Type_Def := N;
6199 Set_Ekind (Derived_Type, E_Record_Type_With_Private);
6201 else
6202 Type_Def := Type_Definition (N);
6204 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
6205 -- Parent_Base can be a private type or private extension. However,
6206 -- for tagged types with an extension the newly added fields are
6207 -- visible and hence the Derived_Type is always an E_Record_Type.
6208 -- (except that the parent may have its own private fields).
6209 -- For untagged types we preserve the Ekind of the Parent_Base.
6211 if Present (Record_Extension_Part (Type_Def)) then
6212 Set_Ekind (Derived_Type, E_Record_Type);
6214 -- Create internal access types for components with anonymous
6215 -- access types.
6217 if Ada_Version >= Ada_05 then
6218 Check_Anonymous_Access_Components
6219 (N, Derived_Type, Derived_Type,
6220 Component_List (Record_Extension_Part (Type_Def)));
6221 end if;
6223 else
6224 Set_Ekind (Derived_Type, Ekind (Parent_Base));
6225 end if;
6226 end if;
6228 -- Indic can either be an N_Identifier if the subtype indication
6229 -- contains no constraint or an N_Subtype_Indication if the subtype
6230 -- indication has a constraint.
6232 Indic := Subtype_Indication (Type_Def);
6233 Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
6235 -- Check that the type has visible discriminants. The type may be
6236 -- a private type with unknown discriminants whose full view has
6237 -- discriminants which are invisible.
6239 if Constraint_Present then
6240 if not Has_Discriminants (Parent_Base)
6241 or else
6242 (Has_Unknown_Discriminants (Parent_Base)
6243 and then Is_Private_Type (Parent_Base))
6244 then
6245 Error_Msg_N
6246 ("invalid constraint: type has no discriminant",
6247 Constraint (Indic));
6249 Constraint_Present := False;
6250 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
6252 elsif Is_Constrained (Parent_Type) then
6253 Error_Msg_N
6254 ("invalid constraint: parent type is already constrained",
6255 Constraint (Indic));
6257 Constraint_Present := False;
6258 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
6259 end if;
6260 end if;
6262 -- STEP 0b: If needed, apply transformation given in point 5. above
6264 if not Private_Extension
6265 and then Has_Discriminants (Parent_Type)
6266 and then not Discriminant_Specs
6267 and then (Is_Constrained (Parent_Type) or else Constraint_Present)
6268 then
6269 -- First, we must analyze the constraint (see comment in point 5.)
6271 if Constraint_Present then
6272 New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
6274 if Has_Discriminants (Derived_Type)
6275 and then Has_Private_Declaration (Derived_Type)
6276 and then Present (Discriminant_Constraint (Derived_Type))
6277 then
6278 -- Verify that constraints of the full view statically match
6279 -- those given in the partial view.
6281 declare
6282 C1, C2 : Elmt_Id;
6284 begin
6285 C1 := First_Elmt (New_Discrs);
6286 C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
6287 while Present (C1) and then Present (C2) loop
6288 if Fully_Conformant_Expressions (Node (C1), Node (C2))
6289 or else
6290 (Is_OK_Static_Expression (Node (C1))
6291 and then
6292 Is_OK_Static_Expression (Node (C2))
6293 and then
6294 Expr_Value (Node (C1)) = Expr_Value (Node (C2)))
6295 then
6296 null;
6298 else
6299 Error_Msg_N (
6300 "constraint not conformant to previous declaration",
6301 Node (C1));
6302 end if;
6304 Next_Elmt (C1);
6305 Next_Elmt (C2);
6306 end loop;
6307 end;
6308 end if;
6309 end if;
6311 -- Insert and analyze the declaration for the unconstrained base type
6313 New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
6315 New_Decl :=
6316 Make_Full_Type_Declaration (Loc,
6317 Defining_Identifier => New_Base,
6318 Type_Definition =>
6319 Make_Derived_Type_Definition (Loc,
6320 Abstract_Present => Abstract_Present (Type_Def),
6321 Subtype_Indication =>
6322 New_Occurrence_Of (Parent_Base, Loc),
6323 Record_Extension_Part =>
6324 Relocate_Node (Record_Extension_Part (Type_Def))));
6326 Set_Parent (New_Decl, Parent (N));
6327 Mark_Rewrite_Insertion (New_Decl);
6328 Insert_Before (N, New_Decl);
6330 -- Note that this call passes False for the Derive_Subps parameter
6331 -- because subprogram derivation is deferred until after creating
6332 -- the subtype (see below).
6334 Build_Derived_Type
6335 (New_Decl, Parent_Base, New_Base,
6336 Is_Completion => True, Derive_Subps => False);
6338 -- ??? This needs re-examination to determine whether the
6339 -- above call can simply be replaced by a call to Analyze.
6341 Set_Analyzed (New_Decl);
6343 -- Insert and analyze the declaration for the constrained subtype
6345 if Constraint_Present then
6346 New_Indic :=
6347 Make_Subtype_Indication (Loc,
6348 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
6349 Constraint => Relocate_Node (Constraint (Indic)));
6351 else
6352 declare
6353 Constr_List : constant List_Id := New_List;
6354 C : Elmt_Id;
6355 Expr : Node_Id;
6357 begin
6358 C := First_Elmt (Discriminant_Constraint (Parent_Type));
6359 while Present (C) loop
6360 Expr := Node (C);
6362 -- It is safe here to call New_Copy_Tree since
6363 -- Force_Evaluation was called on each constraint in
6364 -- Build_Discriminant_Constraints.
6366 Append (New_Copy_Tree (Expr), To => Constr_List);
6368 Next_Elmt (C);
6369 end loop;
6371 New_Indic :=
6372 Make_Subtype_Indication (Loc,
6373 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
6374 Constraint =>
6375 Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
6376 end;
6377 end if;
6379 Rewrite (N,
6380 Make_Subtype_Declaration (Loc,
6381 Defining_Identifier => Derived_Type,
6382 Subtype_Indication => New_Indic));
6384 Analyze (N);
6386 -- Derivation of subprograms must be delayed until the full subtype
6387 -- has been established to ensure proper overriding of subprograms
6388 -- inherited by full types. If the derivations occurred as part of
6389 -- the call to Build_Derived_Type above, then the check for type
6390 -- conformance would fail because earlier primitive subprograms
6391 -- could still refer to the full type prior the change to the new
6392 -- subtype and hence would not match the new base type created here.
6394 Derive_Subprograms (Parent_Type, Derived_Type);
6396 -- For tagged types the Discriminant_Constraint of the new base itype
6397 -- is inherited from the first subtype so that no subtype conformance
6398 -- problem arise when the first subtype overrides primitive
6399 -- operations inherited by the implicit base type.
6401 if Is_Tagged then
6402 Set_Discriminant_Constraint
6403 (New_Base, Discriminant_Constraint (Derived_Type));
6404 end if;
6406 return;
6407 end if;
6409 -- If we get here Derived_Type will have no discriminants or it will be
6410 -- a discriminated unconstrained base type.
6412 -- STEP 1a: perform preliminary actions/checks for derived tagged types
6414 if Is_Tagged then
6416 -- The parent type is frozen for non-private extensions (RM 13.14(7))
6417 -- The declaration of a specific descendant of an interface type
6418 -- freezes the interface type (RM 13.14).
6420 if not Private_Extension
6421 or else Is_Interface (Parent_Base)
6422 then
6423 Freeze_Before (N, Parent_Type);
6424 end if;
6426 -- In Ada 2005 (AI-344), the restriction that a derived tagged type
6427 -- cannot be declared at a deeper level than its parent type is
6428 -- removed. The check on derivation within a generic body is also
6429 -- relaxed, but there's a restriction that a derived tagged type
6430 -- cannot be declared in a generic body if it's derived directly
6431 -- or indirectly from a formal type of that generic.
6433 if Ada_Version >= Ada_05 then
6434 if Present (Enclosing_Generic_Body (Derived_Type)) then
6435 declare
6436 Ancestor_Type : Entity_Id;
6438 begin
6439 -- Check to see if any ancestor of the derived type is a
6440 -- formal type.
6442 Ancestor_Type := Parent_Type;
6443 while not Is_Generic_Type (Ancestor_Type)
6444 and then Etype (Ancestor_Type) /= Ancestor_Type
6445 loop
6446 Ancestor_Type := Etype (Ancestor_Type);
6447 end loop;
6449 -- If the derived type does have a formal type as an
6450 -- ancestor, then it's an error if the derived type is
6451 -- declared within the body of the generic unit that
6452 -- declares the formal type in its generic formal part. It's
6453 -- sufficient to check whether the ancestor type is declared
6454 -- inside the same generic body as the derived type (such as
6455 -- within a nested generic spec), in which case the
6456 -- derivation is legal. If the formal type is declared
6457 -- outside of that generic body, then it's guaranteed that
6458 -- the derived type is declared within the generic body of
6459 -- the generic unit declaring the formal type.
6461 if Is_Generic_Type (Ancestor_Type)
6462 and then Enclosing_Generic_Body (Ancestor_Type) /=
6463 Enclosing_Generic_Body (Derived_Type)
6464 then
6465 Error_Msg_NE
6466 ("parent type of& must not be descendant of formal type"
6467 & " of an enclosing generic body",
6468 Indic, Derived_Type);
6469 end if;
6470 end;
6471 end if;
6473 elsif Type_Access_Level (Derived_Type) /=
6474 Type_Access_Level (Parent_Type)
6475 and then not Is_Generic_Type (Derived_Type)
6476 then
6477 if Is_Controlled (Parent_Type) then
6478 Error_Msg_N
6479 ("controlled type must be declared at the library level",
6480 Indic);
6481 else
6482 Error_Msg_N
6483 ("type extension at deeper accessibility level than parent",
6484 Indic);
6485 end if;
6487 else
6488 declare
6489 GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
6491 begin
6492 if Present (GB)
6493 and then GB /= Enclosing_Generic_Body (Parent_Base)
6494 then
6495 Error_Msg_NE
6496 ("parent type of& must not be outside generic body"
6497 & " (RM 3.9.1(4))",
6498 Indic, Derived_Type);
6499 end if;
6500 end;
6501 end if;
6502 end if;
6504 -- Ada 2005 (AI-251)
6506 if Ada_Version = Ada_05
6507 and then Is_Tagged
6508 then
6509 -- "The declaration of a specific descendant of an interface type
6510 -- freezes the interface type" (RM 13.14).
6512 declare
6513 Iface : Node_Id;
6514 begin
6515 if Is_Non_Empty_List (Interface_List (Type_Def)) then
6516 Iface := First (Interface_List (Type_Def));
6517 while Present (Iface) loop
6518 Freeze_Before (N, Etype (Iface));
6519 Next (Iface);
6520 end loop;
6521 end if;
6522 end;
6523 end if;
6525 -- STEP 1b : preliminary cleanup of the full view of private types
6527 -- If the type is already marked as having discriminants, then it's the
6528 -- completion of a private type or private extension and we need to
6529 -- retain the discriminants from the partial view if the current
6530 -- declaration has Discriminant_Specifications so that we can verify
6531 -- conformance. However, we must remove any existing components that
6532 -- were inherited from the parent (and attached in Copy_And_Swap)
6533 -- because the full type inherits all appropriate components anyway, and
6534 -- we do not want the partial view's components interfering.
6536 if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
6537 Discrim := First_Discriminant (Derived_Type);
6538 loop
6539 Last_Discrim := Discrim;
6540 Next_Discriminant (Discrim);
6541 exit when No (Discrim);
6542 end loop;
6544 Set_Last_Entity (Derived_Type, Last_Discrim);
6546 -- In all other cases wipe out the list of inherited components (even
6547 -- inherited discriminants), it will be properly rebuilt here.
6549 else
6550 Set_First_Entity (Derived_Type, Empty);
6551 Set_Last_Entity (Derived_Type, Empty);
6552 end if;
6554 -- STEP 1c: Initialize some flags for the Derived_Type
6556 -- The following flags must be initialized here so that
6557 -- Process_Discriminants can check that discriminants of tagged types do
6558 -- not have a default initial value and that access discriminants are
6559 -- only specified for limited records. For completeness, these flags are
6560 -- also initialized along with all the other flags below.
6562 -- AI-419: Limitedness is not inherited from an interface parent, so to
6563 -- be limited in that case the type must be explicitly declared as
6564 -- limited. However, task and protected interfaces are always limited.
6566 if Limited_Present (Type_Def) then
6567 Set_Is_Limited_Record (Derived_Type);
6569 elsif Is_Limited_Record (Parent_Type)
6570 or else (Present (Full_View (Parent_Type))
6571 and then Is_Limited_Record (Full_View (Parent_Type)))
6572 then
6573 if not Is_Interface (Parent_Type)
6574 or else Is_Synchronized_Interface (Parent_Type)
6575 or else Is_Protected_Interface (Parent_Type)
6576 or else Is_Task_Interface (Parent_Type)
6577 then
6578 Set_Is_Limited_Record (Derived_Type);
6579 end if;
6580 end if;
6582 -- STEP 2a: process discriminants of derived type if any
6584 Push_Scope (Derived_Type);
6586 if Discriminant_Specs then
6587 Set_Has_Unknown_Discriminants (Derived_Type, False);
6589 -- The following call initializes fields Has_Discriminants and
6590 -- Discriminant_Constraint, unless we are processing the completion
6591 -- of a private type declaration.
6593 Check_Or_Process_Discriminants (N, Derived_Type);
6595 -- For non-tagged types the constraint on the Parent_Type must be
6596 -- present and is used to rename the discriminants.
6598 if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
6599 Error_Msg_N ("untagged parent must have discriminants", Indic);
6601 elsif not Is_Tagged and then not Constraint_Present then
6602 Error_Msg_N
6603 ("discriminant constraint needed for derived untagged records",
6604 Indic);
6606 -- Otherwise the parent subtype must be constrained unless we have a
6607 -- private extension.
6609 elsif not Constraint_Present
6610 and then not Private_Extension
6611 and then not Is_Constrained (Parent_Type)
6612 then
6613 Error_Msg_N
6614 ("unconstrained type not allowed in this context", Indic);
6616 elsif Constraint_Present then
6617 -- The following call sets the field Corresponding_Discriminant
6618 -- for the discriminants in the Derived_Type.
6620 Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
6622 -- For untagged types all new discriminants must rename
6623 -- discriminants in the parent. For private extensions new
6624 -- discriminants cannot rename old ones (implied by [7.3(13)]).
6626 Discrim := First_Discriminant (Derived_Type);
6627 while Present (Discrim) loop
6628 if not Is_Tagged
6629 and then No (Corresponding_Discriminant (Discrim))
6630 then
6631 Error_Msg_N
6632 ("new discriminants must constrain old ones", Discrim);
6634 elsif Private_Extension
6635 and then Present (Corresponding_Discriminant (Discrim))
6636 then
6637 Error_Msg_N
6638 ("only static constraints allowed for parent"
6639 & " discriminants in the partial view", Indic);
6640 exit;
6641 end if;
6643 -- If a new discriminant is used in the constraint, then its
6644 -- subtype must be statically compatible with the parent
6645 -- discriminant's subtype (3.7(15)).
6647 if Present (Corresponding_Discriminant (Discrim))
6648 and then
6649 not Subtypes_Statically_Compatible
6650 (Etype (Discrim),
6651 Etype (Corresponding_Discriminant (Discrim)))
6652 then
6653 Error_Msg_N
6654 ("subtype must be compatible with parent discriminant",
6655 Discrim);
6656 end if;
6658 Next_Discriminant (Discrim);
6659 end loop;
6661 -- Check whether the constraints of the full view statically
6662 -- match those imposed by the parent subtype [7.3(13)].
6664 if Present (Stored_Constraint (Derived_Type)) then
6665 declare
6666 C1, C2 : Elmt_Id;
6668 begin
6669 C1 := First_Elmt (Discs);
6670 C2 := First_Elmt (Stored_Constraint (Derived_Type));
6671 while Present (C1) and then Present (C2) loop
6672 if not
6673 Fully_Conformant_Expressions (Node (C1), Node (C2))
6674 then
6675 Error_Msg_N
6676 ("not conformant with previous declaration",
6677 Node (C1));
6678 end if;
6680 Next_Elmt (C1);
6681 Next_Elmt (C2);
6682 end loop;
6683 end;
6684 end if;
6685 end if;
6687 -- STEP 2b: No new discriminants, inherit discriminants if any
6689 else
6690 if Private_Extension then
6691 Set_Has_Unknown_Discriminants
6692 (Derived_Type,
6693 Has_Unknown_Discriminants (Parent_Type)
6694 or else Unknown_Discriminants_Present (N));
6696 -- The partial view of the parent may have unknown discriminants,
6697 -- but if the full view has discriminants and the parent type is
6698 -- in scope they must be inherited.
6700 elsif Has_Unknown_Discriminants (Parent_Type)
6701 and then
6702 (not Has_Discriminants (Parent_Type)
6703 or else not In_Open_Scopes (Scope (Parent_Type)))
6704 then
6705 Set_Has_Unknown_Discriminants (Derived_Type);
6706 end if;
6708 if not Has_Unknown_Discriminants (Derived_Type)
6709 and then not Has_Unknown_Discriminants (Parent_Base)
6710 and then Has_Discriminants (Parent_Type)
6711 then
6712 Inherit_Discrims := True;
6713 Set_Has_Discriminants
6714 (Derived_Type, True);
6715 Set_Discriminant_Constraint
6716 (Derived_Type, Discriminant_Constraint (Parent_Base));
6717 end if;
6719 -- The following test is true for private types (remember
6720 -- transformation 5. is not applied to those) and in an error
6721 -- situation.
6723 if Constraint_Present then
6724 Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
6725 end if;
6727 -- For now mark a new derived type as constrained only if it has no
6728 -- discriminants. At the end of Build_Derived_Record_Type we properly
6729 -- set this flag in the case of private extensions. See comments in
6730 -- point 9. just before body of Build_Derived_Record_Type.
6732 Set_Is_Constrained
6733 (Derived_Type,
6734 not (Inherit_Discrims
6735 or else Has_Unknown_Discriminants (Derived_Type)));
6736 end if;
6738 -- STEP 3: initialize fields of derived type
6740 Set_Is_Tagged_Type (Derived_Type, Is_Tagged);
6741 Set_Stored_Constraint (Derived_Type, No_Elist);
6743 -- Ada 2005 (AI-251): Private type-declarations can implement interfaces
6744 -- but cannot be interfaces
6746 if not Private_Extension
6747 and then Ekind (Derived_Type) /= E_Private_Type
6748 and then Ekind (Derived_Type) /= E_Limited_Private_Type
6749 then
6750 if Interface_Present (Type_Def) then
6751 Analyze_Interface_Declaration (Derived_Type, Type_Def);
6752 end if;
6754 Set_Interfaces (Derived_Type, No_Elist);
6755 end if;
6757 -- Fields inherited from the Parent_Type
6759 Set_Discard_Names
6760 (Derived_Type, Einfo.Discard_Names (Parent_Type));
6761 Set_Has_Specified_Layout
6762 (Derived_Type, Has_Specified_Layout (Parent_Type));
6763 Set_Is_Limited_Composite
6764 (Derived_Type, Is_Limited_Composite (Parent_Type));
6765 Set_Is_Private_Composite
6766 (Derived_Type, Is_Private_Composite (Parent_Type));
6768 -- Fields inherited from the Parent_Base
6770 Set_Has_Controlled_Component
6771 (Derived_Type, Has_Controlled_Component (Parent_Base));
6772 Set_Has_Non_Standard_Rep
6773 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
6774 Set_Has_Primitive_Operations
6775 (Derived_Type, Has_Primitive_Operations (Parent_Base));
6777 -- Fields inherited from the Parent_Base in the non-private case
6779 if Ekind (Derived_Type) = E_Record_Type then
6780 Set_Has_Complex_Representation
6781 (Derived_Type, Has_Complex_Representation (Parent_Base));
6782 end if;
6784 -- Fields inherited from the Parent_Base for record types
6786 if Is_Record_Type (Derived_Type) then
6787 Set_OK_To_Reorder_Components
6788 (Derived_Type, OK_To_Reorder_Components (Parent_Base));
6789 Set_Reverse_Bit_Order
6790 (Derived_Type, Reverse_Bit_Order (Parent_Base));
6791 end if;
6793 -- Direct controlled types do not inherit Finalize_Storage_Only flag
6795 if not Is_Controlled (Parent_Type) then
6796 Set_Finalize_Storage_Only
6797 (Derived_Type, Finalize_Storage_Only (Parent_Type));
6798 end if;
6800 -- Set fields for private derived types
6802 if Is_Private_Type (Derived_Type) then
6803 Set_Depends_On_Private (Derived_Type, True);
6804 Set_Private_Dependents (Derived_Type, New_Elmt_List);
6806 -- Inherit fields from non private record types. If this is the
6807 -- completion of a derivation from a private type, the parent itself
6808 -- is private, and the attributes come from its full view, which must
6809 -- be present.
6811 else
6812 if Is_Private_Type (Parent_Base)
6813 and then not Is_Record_Type (Parent_Base)
6814 then
6815 Set_Component_Alignment
6816 (Derived_Type, Component_Alignment (Full_View (Parent_Base)));
6817 Set_C_Pass_By_Copy
6818 (Derived_Type, C_Pass_By_Copy (Full_View (Parent_Base)));
6819 else
6820 Set_Component_Alignment
6821 (Derived_Type, Component_Alignment (Parent_Base));
6823 Set_C_Pass_By_Copy
6824 (Derived_Type, C_Pass_By_Copy (Parent_Base));
6825 end if;
6826 end if;
6828 -- Set fields for tagged types
6830 if Is_Tagged then
6831 Set_Primitive_Operations (Derived_Type, New_Elmt_List);
6833 -- All tagged types defined in Ada.Finalization are controlled
6835 if Chars (Scope (Derived_Type)) = Name_Finalization
6836 and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
6837 and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
6838 then
6839 Set_Is_Controlled (Derived_Type);
6840 else
6841 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Base));
6842 end if;
6844 Make_Class_Wide_Type (Derived_Type);
6845 Set_Is_Abstract_Type (Derived_Type, Abstract_Present (Type_Def));
6847 if Has_Discriminants (Derived_Type)
6848 and then Constraint_Present
6849 then
6850 Set_Stored_Constraint
6851 (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
6852 end if;
6854 if Ada_Version >= Ada_05 then
6855 declare
6856 Ifaces_List : Elist_Id;
6858 begin
6859 -- Checks rules 3.9.4 (13/2 and 14/2)
6861 if Comes_From_Source (Derived_Type)
6862 and then not Is_Private_Type (Derived_Type)
6863 and then Is_Interface (Parent_Type)
6864 and then not Is_Interface (Derived_Type)
6865 then
6866 if Is_Task_Interface (Parent_Type) then
6867 Error_Msg_N
6868 ("(Ada 2005) task type required (RM 3.9.4 (13.2))",
6869 Derived_Type);
6871 elsif Is_Protected_Interface (Parent_Type) then
6872 Error_Msg_N
6873 ("(Ada 2005) protected type required (RM 3.9.4 (14.2))",
6874 Derived_Type);
6875 end if;
6876 end if;
6878 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
6880 Check_Interfaces (N, Type_Def);
6882 -- Ada 2005 (AI-251): Collect the list of progenitors that are
6883 -- not already in the parents.
6885 Collect_Interfaces
6886 (T => Derived_Type,
6887 Ifaces_List => Ifaces_List,
6888 Exclude_Parents => True);
6890 Set_Interfaces (Derived_Type, Ifaces_List);
6891 end;
6892 end if;
6894 else
6895 Set_Is_Packed (Derived_Type, Is_Packed (Parent_Base));
6896 Set_Has_Non_Standard_Rep
6897 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
6898 end if;
6900 -- STEP 4: Inherit components from the parent base and constrain them.
6901 -- Apply the second transformation described in point 6. above.
6903 if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
6904 or else not Has_Discriminants (Parent_Type)
6905 or else not Is_Constrained (Parent_Type)
6906 then
6907 Constrs := Discs;
6908 else
6909 Constrs := Discriminant_Constraint (Parent_Type);
6910 end if;
6912 Assoc_List :=
6913 Inherit_Components
6914 (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
6916 -- STEP 5a: Copy the parent record declaration for untagged types
6918 if not Is_Tagged then
6920 -- Discriminant_Constraint (Derived_Type) has been properly
6921 -- constructed. Save it and temporarily set it to Empty because we
6922 -- do not want the call to New_Copy_Tree below to mess this list.
6924 if Has_Discriminants (Derived_Type) then
6925 Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
6926 Set_Discriminant_Constraint (Derived_Type, No_Elist);
6927 else
6928 Save_Discr_Constr := No_Elist;
6929 end if;
6931 -- Save the Etype field of Derived_Type. It is correctly set now,
6932 -- but the call to New_Copy tree may remap it to point to itself,
6933 -- which is not what we want. Ditto for the Next_Entity field.
6935 Save_Etype := Etype (Derived_Type);
6936 Save_Next_Entity := Next_Entity (Derived_Type);
6938 -- Assoc_List maps all stored discriminants in the Parent_Base to
6939 -- stored discriminants in the Derived_Type. It is fundamental that
6940 -- no types or itypes with discriminants other than the stored
6941 -- discriminants appear in the entities declared inside
6942 -- Derived_Type, since the back end cannot deal with it.
6944 New_Decl :=
6945 New_Copy_Tree
6946 (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
6948 -- Restore the fields saved prior to the New_Copy_Tree call
6949 -- and compute the stored constraint.
6951 Set_Etype (Derived_Type, Save_Etype);
6952 Set_Next_Entity (Derived_Type, Save_Next_Entity);
6954 if Has_Discriminants (Derived_Type) then
6955 Set_Discriminant_Constraint
6956 (Derived_Type, Save_Discr_Constr);
6957 Set_Stored_Constraint
6958 (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
6959 Replace_Components (Derived_Type, New_Decl);
6960 end if;
6962 -- Insert the new derived type declaration
6964 Rewrite (N, New_Decl);
6966 -- STEP 5b: Complete the processing for record extensions in generics
6968 -- There is no completion for record extensions declared in the
6969 -- parameter part of a generic, so we need to complete processing for
6970 -- these generic record extensions here. The Record_Type_Definition call
6971 -- will change the Ekind of the components from E_Void to E_Component.
6973 elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
6974 Record_Type_Definition (Empty, Derived_Type);
6976 -- STEP 5c: Process the record extension for non private tagged types
6978 elsif not Private_Extension then
6980 -- Add the _parent field in the derived type
6982 Expand_Record_Extension (Derived_Type, Type_Def);
6984 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
6985 -- implemented interfaces if we are in expansion mode
6987 if Expander_Active
6988 and then Has_Interfaces (Derived_Type)
6989 then
6990 Add_Interface_Tag_Components (N, Derived_Type);
6991 end if;
6993 -- Analyze the record extension
6995 Record_Type_Definition
6996 (Record_Extension_Part (Type_Def), Derived_Type);
6997 end if;
6999 End_Scope;
7001 -- Nothing else to do if there is an error in the derivation.
7002 -- An unusual case: the full view may be derived from a type in an
7003 -- instance, when the partial view was used illegally as an actual
7004 -- in that instance, leading to a circular definition.
7006 if Etype (Derived_Type) = Any_Type
7007 or else Etype (Parent_Type) = Derived_Type
7008 then
7009 return;
7010 end if;
7012 -- Set delayed freeze and then derive subprograms, we need to do
7013 -- this in this order so that derived subprograms inherit the
7014 -- derived freeze if necessary.
7016 Set_Has_Delayed_Freeze (Derived_Type);
7018 if Derive_Subps then
7019 Derive_Subprograms (Parent_Type, Derived_Type);
7020 end if;
7022 -- If we have a private extension which defines a constrained derived
7023 -- type mark as constrained here after we have derived subprograms. See
7024 -- comment on point 9. just above the body of Build_Derived_Record_Type.
7026 if Private_Extension and then Inherit_Discrims then
7027 if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
7028 Set_Is_Constrained (Derived_Type, True);
7029 Set_Discriminant_Constraint (Derived_Type, Discs);
7031 elsif Is_Constrained (Parent_Type) then
7032 Set_Is_Constrained
7033 (Derived_Type, True);
7034 Set_Discriminant_Constraint
7035 (Derived_Type, Discriminant_Constraint (Parent_Type));
7036 end if;
7037 end if;
7039 -- Update the class_wide type, which shares the now-completed
7040 -- entity list with its specific type.
7042 if Is_Tagged then
7043 Set_First_Entity
7044 (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
7045 Set_Last_Entity
7046 (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
7047 end if;
7049 -- Update the scope of anonymous access types of discriminants and other
7050 -- components, to prevent scope anomalies in gigi, when the derivation
7051 -- appears in a scope nested within that of the parent.
7053 declare
7054 D : Entity_Id;
7056 begin
7057 D := First_Entity (Derived_Type);
7058 while Present (D) loop
7059 if Ekind (D) = E_Discriminant
7060 or else Ekind (D) = E_Component
7061 then
7062 if Is_Itype (Etype (D))
7063 and then Ekind (Etype (D)) = E_Anonymous_Access_Type
7064 then
7065 Set_Scope (Etype (D), Current_Scope);
7066 end if;
7067 end if;
7069 Next_Entity (D);
7070 end loop;
7071 end;
7072 end Build_Derived_Record_Type;
7074 ------------------------
7075 -- Build_Derived_Type --
7076 ------------------------
7078 procedure Build_Derived_Type
7079 (N : Node_Id;
7080 Parent_Type : Entity_Id;
7081 Derived_Type : Entity_Id;
7082 Is_Completion : Boolean;
7083 Derive_Subps : Boolean := True)
7085 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
7087 begin
7088 -- Set common attributes
7090 Set_Scope (Derived_Type, Current_Scope);
7092 Set_Ekind (Derived_Type, Ekind (Parent_Base));
7093 Set_Etype (Derived_Type, Parent_Base);
7094 Set_Has_Task (Derived_Type, Has_Task (Parent_Base));
7096 Set_Size_Info (Derived_Type, Parent_Type);
7097 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
7098 Set_Convention (Derived_Type, Convention (Parent_Type));
7099 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
7101 -- The derived type inherits the representation clauses of the parent.
7102 -- However, for a private type that is completed by a derivation, there
7103 -- may be operation attributes that have been specified already (stream
7104 -- attributes and External_Tag) and those must be provided. Finally,
7105 -- if the partial view is a private extension, the representation items
7106 -- of the parent have been inherited already, and should not be chained
7107 -- twice to the derived type.
7109 if Is_Tagged_Type (Parent_Type)
7110 and then Present (First_Rep_Item (Derived_Type))
7111 then
7112 -- The existing items are either operational items or items inherited
7113 -- from a private extension declaration.
7115 declare
7116 Rep : Node_Id;
7117 -- Used to iterate over representation items of the derived type
7119 Last_Rep : Node_Id;
7120 -- Last representation item of the (non-empty) representation
7121 -- item list of the derived type.
7123 Found : Boolean := False;
7125 begin
7126 Rep := First_Rep_Item (Derived_Type);
7127 Last_Rep := Rep;
7128 while Present (Rep) loop
7129 if Rep = First_Rep_Item (Parent_Type) then
7130 Found := True;
7131 exit;
7133 else
7134 Rep := Next_Rep_Item (Rep);
7136 if Present (Rep) then
7137 Last_Rep := Rep;
7138 end if;
7139 end if;
7140 end loop;
7142 -- Here if we either encountered the parent type's first rep
7143 -- item on the derived type's rep item list (in which case
7144 -- Found is True, and we have nothing else to do), or if we
7145 -- reached the last rep item of the derived type, which is
7146 -- Last_Rep, in which case we further chain the parent type's
7147 -- rep items to those of the derived type.
7149 if not Found then
7150 Set_Next_Rep_Item (Last_Rep, First_Rep_Item (Parent_Type));
7151 end if;
7152 end;
7154 else
7155 Set_First_Rep_Item (Derived_Type, First_Rep_Item (Parent_Type));
7156 end if;
7158 case Ekind (Parent_Type) is
7159 when Numeric_Kind =>
7160 Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
7162 when Array_Kind =>
7163 Build_Derived_Array_Type (N, Parent_Type, Derived_Type);
7165 when E_Record_Type
7166 | E_Record_Subtype
7167 | Class_Wide_Kind =>
7168 Build_Derived_Record_Type
7169 (N, Parent_Type, Derived_Type, Derive_Subps);
7170 return;
7172 when Enumeration_Kind =>
7173 Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
7175 when Access_Kind =>
7176 Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
7178 when Incomplete_Or_Private_Kind =>
7179 Build_Derived_Private_Type
7180 (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
7182 -- For discriminated types, the derivation includes deriving
7183 -- primitive operations. For others it is done below.
7185 if Is_Tagged_Type (Parent_Type)
7186 or else Has_Discriminants (Parent_Type)
7187 or else (Present (Full_View (Parent_Type))
7188 and then Has_Discriminants (Full_View (Parent_Type)))
7189 then
7190 return;
7191 end if;
7193 when Concurrent_Kind =>
7194 Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
7196 when others =>
7197 raise Program_Error;
7198 end case;
7200 if Etype (Derived_Type) = Any_Type then
7201 return;
7202 end if;
7204 -- Set delayed freeze and then derive subprograms, we need to do this
7205 -- in this order so that derived subprograms inherit the derived freeze
7206 -- if necessary.
7208 Set_Has_Delayed_Freeze (Derived_Type);
7209 if Derive_Subps then
7210 Derive_Subprograms (Parent_Type, Derived_Type);
7211 end if;
7213 Set_Has_Primitive_Operations
7214 (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
7215 end Build_Derived_Type;
7217 -----------------------
7218 -- Build_Discriminal --
7219 -----------------------
7221 procedure Build_Discriminal (Discrim : Entity_Id) is
7222 D_Minal : Entity_Id;
7223 CR_Disc : Entity_Id;
7225 begin
7226 -- A discriminal has the same name as the discriminant
7228 D_Minal :=
7229 Make_Defining_Identifier (Sloc (Discrim),
7230 Chars => Chars (Discrim));
7232 Set_Ekind (D_Minal, E_In_Parameter);
7233 Set_Mechanism (D_Minal, Default_Mechanism);
7234 Set_Etype (D_Minal, Etype (Discrim));
7236 Set_Discriminal (Discrim, D_Minal);
7237 Set_Discriminal_Link (D_Minal, Discrim);
7239 -- For task types, build at once the discriminants of the corresponding
7240 -- record, which are needed if discriminants are used in entry defaults
7241 -- and in family bounds.
7243 if Is_Concurrent_Type (Current_Scope)
7244 or else Is_Limited_Type (Current_Scope)
7245 then
7246 CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
7248 Set_Ekind (CR_Disc, E_In_Parameter);
7249 Set_Mechanism (CR_Disc, Default_Mechanism);
7250 Set_Etype (CR_Disc, Etype (Discrim));
7251 Set_Discriminal_Link (CR_Disc, Discrim);
7252 Set_CR_Discriminant (Discrim, CR_Disc);
7253 end if;
7254 end Build_Discriminal;
7256 ------------------------------------
7257 -- Build_Discriminant_Constraints --
7258 ------------------------------------
7260 function Build_Discriminant_Constraints
7261 (T : Entity_Id;
7262 Def : Node_Id;
7263 Derived_Def : Boolean := False) return Elist_Id
7265 C : constant Node_Id := Constraint (Def);
7266 Nb_Discr : constant Nat := Number_Discriminants (T);
7268 Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
7269 -- Saves the expression corresponding to a given discriminant in T
7271 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
7272 -- Return the Position number within array Discr_Expr of a discriminant
7273 -- D within the discriminant list of the discriminated type T.
7275 ------------------
7276 -- Pos_Of_Discr --
7277 ------------------
7279 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
7280 Disc : Entity_Id;
7282 begin
7283 Disc := First_Discriminant (T);
7284 for J in Discr_Expr'Range loop
7285 if Disc = D then
7286 return J;
7287 end if;
7289 Next_Discriminant (Disc);
7290 end loop;
7292 -- Note: Since this function is called on discriminants that are
7293 -- known to belong to the discriminated type, falling through the
7294 -- loop with no match signals an internal compiler error.
7296 raise Program_Error;
7297 end Pos_Of_Discr;
7299 -- Declarations local to Build_Discriminant_Constraints
7301 Discr : Entity_Id;
7302 E : Entity_Id;
7303 Elist : constant Elist_Id := New_Elmt_List;
7305 Constr : Node_Id;
7306 Expr : Node_Id;
7307 Id : Node_Id;
7308 Position : Nat;
7309 Found : Boolean;
7311 Discrim_Present : Boolean := False;
7313 -- Start of processing for Build_Discriminant_Constraints
7315 begin
7316 -- The following loop will process positional associations only.
7317 -- For a positional association, the (single) discriminant is
7318 -- implicitly specified by position, in textual order (RM 3.7.2).
7320 Discr := First_Discriminant (T);
7321 Constr := First (Constraints (C));
7322 for D in Discr_Expr'Range loop
7323 exit when Nkind (Constr) = N_Discriminant_Association;
7325 if No (Constr) then
7326 Error_Msg_N ("too few discriminants given in constraint", C);
7327 return New_Elmt_List;
7329 elsif Nkind (Constr) = N_Range
7330 or else (Nkind (Constr) = N_Attribute_Reference
7331 and then
7332 Attribute_Name (Constr) = Name_Range)
7333 then
7334 Error_Msg_N
7335 ("a range is not a valid discriminant constraint", Constr);
7336 Discr_Expr (D) := Error;
7338 else
7339 Analyze_And_Resolve (Constr, Base_Type (Etype (Discr)));
7340 Discr_Expr (D) := Constr;
7341 end if;
7343 Next_Discriminant (Discr);
7344 Next (Constr);
7345 end loop;
7347 if No (Discr) and then Present (Constr) then
7348 Error_Msg_N ("too many discriminants given in constraint", Constr);
7349 return New_Elmt_List;
7350 end if;
7352 -- Named associations can be given in any order, but if both positional
7353 -- and named associations are used in the same discriminant constraint,
7354 -- then positional associations must occur first, at their normal
7355 -- position. Hence once a named association is used, the rest of the
7356 -- discriminant constraint must use only named associations.
7358 while Present (Constr) loop
7360 -- Positional association forbidden after a named association
7362 if Nkind (Constr) /= N_Discriminant_Association then
7363 Error_Msg_N ("positional association follows named one", Constr);
7364 return New_Elmt_List;
7366 -- Otherwise it is a named association
7368 else
7369 -- E records the type of the discriminants in the named
7370 -- association. All the discriminants specified in the same name
7371 -- association must have the same type.
7373 E := Empty;
7375 -- Search the list of discriminants in T to see if the simple name
7376 -- given in the constraint matches any of them.
7378 Id := First (Selector_Names (Constr));
7379 while Present (Id) loop
7380 Found := False;
7382 -- If Original_Discriminant is present, we are processing a
7383 -- generic instantiation and this is an instance node. We need
7384 -- to find the name of the corresponding discriminant in the
7385 -- actual record type T and not the name of the discriminant in
7386 -- the generic formal. Example:
7388 -- generic
7389 -- type G (D : int) is private;
7390 -- package P is
7391 -- subtype W is G (D => 1);
7392 -- end package;
7393 -- type Rec (X : int) is record ... end record;
7394 -- package Q is new P (G => Rec);
7396 -- At the point of the instantiation, formal type G is Rec
7397 -- and therefore when reanalyzing "subtype W is G (D => 1);"
7398 -- which really looks like "subtype W is Rec (D => 1);" at
7399 -- the point of instantiation, we want to find the discriminant
7400 -- that corresponds to D in Rec, i.e. X.
7402 if Present (Original_Discriminant (Id)) then
7403 Discr := Find_Corresponding_Discriminant (Id, T);
7404 Found := True;
7406 else
7407 Discr := First_Discriminant (T);
7408 while Present (Discr) loop
7409 if Chars (Discr) = Chars (Id) then
7410 Found := True;
7411 exit;
7412 end if;
7414 Next_Discriminant (Discr);
7415 end loop;
7417 if not Found then
7418 Error_Msg_N ("& does not match any discriminant", Id);
7419 return New_Elmt_List;
7421 -- The following is only useful for the benefit of generic
7422 -- instances but it does not interfere with other
7423 -- processing for the non-generic case so we do it in all
7424 -- cases (for generics this statement is executed when
7425 -- processing the generic definition, see comment at the
7426 -- beginning of this if statement).
7428 else
7429 Set_Original_Discriminant (Id, Discr);
7430 end if;
7431 end if;
7433 Position := Pos_Of_Discr (T, Discr);
7435 if Present (Discr_Expr (Position)) then
7436 Error_Msg_N ("duplicate constraint for discriminant&", Id);
7438 else
7439 -- Each discriminant specified in the same named association
7440 -- must be associated with a separate copy of the
7441 -- corresponding expression.
7443 if Present (Next (Id)) then
7444 Expr := New_Copy_Tree (Expression (Constr));
7445 Set_Parent (Expr, Parent (Expression (Constr)));
7446 else
7447 Expr := Expression (Constr);
7448 end if;
7450 Discr_Expr (Position) := Expr;
7451 Analyze_And_Resolve (Expr, Base_Type (Etype (Discr)));
7452 end if;
7454 -- A discriminant association with more than one discriminant
7455 -- name is only allowed if the named discriminants are all of
7456 -- the same type (RM 3.7.1(8)).
7458 if E = Empty then
7459 E := Base_Type (Etype (Discr));
7461 elsif Base_Type (Etype (Discr)) /= E then
7462 Error_Msg_N
7463 ("all discriminants in an association " &
7464 "must have the same type", Id);
7465 end if;
7467 Next (Id);
7468 end loop;
7469 end if;
7471 Next (Constr);
7472 end loop;
7474 -- A discriminant constraint must provide exactly one value for each
7475 -- discriminant of the type (RM 3.7.1(8)).
7477 for J in Discr_Expr'Range loop
7478 if No (Discr_Expr (J)) then
7479 Error_Msg_N ("too few discriminants given in constraint", C);
7480 return New_Elmt_List;
7481 end if;
7482 end loop;
7484 -- Determine if there are discriminant expressions in the constraint
7486 for J in Discr_Expr'Range loop
7487 if Denotes_Discriminant
7488 (Discr_Expr (J), Check_Concurrent => True)
7489 then
7490 Discrim_Present := True;
7491 end if;
7492 end loop;
7494 -- Build an element list consisting of the expressions given in the
7495 -- discriminant constraint and apply the appropriate checks. The list
7496 -- is constructed after resolving any named discriminant associations
7497 -- and therefore the expressions appear in the textual order of the
7498 -- discriminants.
7500 Discr := First_Discriminant (T);
7501 for J in Discr_Expr'Range loop
7502 if Discr_Expr (J) /= Error then
7503 Append_Elmt (Discr_Expr (J), Elist);
7505 -- If any of the discriminant constraints is given by a
7506 -- discriminant and we are in a derived type declaration we
7507 -- have a discriminant renaming. Establish link between new
7508 -- and old discriminant.
7510 if Denotes_Discriminant (Discr_Expr (J)) then
7511 if Derived_Def then
7512 Set_Corresponding_Discriminant
7513 (Entity (Discr_Expr (J)), Discr);
7514 end if;
7516 -- Force the evaluation of non-discriminant expressions.
7517 -- If we have found a discriminant in the constraint 3.4(26)
7518 -- and 3.8(18) demand that no range checks are performed are
7519 -- after evaluation. If the constraint is for a component
7520 -- definition that has a per-object constraint, expressions are
7521 -- evaluated but not checked either. In all other cases perform
7522 -- a range check.
7524 else
7525 if Discrim_Present then
7526 null;
7528 elsif Nkind (Parent (Parent (Def))) = N_Component_Declaration
7529 and then
7530 Has_Per_Object_Constraint
7531 (Defining_Identifier (Parent (Parent (Def))))
7532 then
7533 null;
7535 elsif Is_Access_Type (Etype (Discr)) then
7536 Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
7538 else
7539 Apply_Range_Check (Discr_Expr (J), Etype (Discr));
7540 end if;
7542 Force_Evaluation (Discr_Expr (J));
7543 end if;
7545 -- Check that the designated type of an access discriminant's
7546 -- expression is not a class-wide type unless the discriminant's
7547 -- designated type is also class-wide.
7549 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
7550 and then not Is_Class_Wide_Type
7551 (Designated_Type (Etype (Discr)))
7552 and then Etype (Discr_Expr (J)) /= Any_Type
7553 and then Is_Class_Wide_Type
7554 (Designated_Type (Etype (Discr_Expr (J))))
7555 then
7556 Wrong_Type (Discr_Expr (J), Etype (Discr));
7557 end if;
7558 end if;
7560 Next_Discriminant (Discr);
7561 end loop;
7563 return Elist;
7564 end Build_Discriminant_Constraints;
7566 ---------------------------------
7567 -- Build_Discriminated_Subtype --
7568 ---------------------------------
7570 procedure Build_Discriminated_Subtype
7571 (T : Entity_Id;
7572 Def_Id : Entity_Id;
7573 Elist : Elist_Id;
7574 Related_Nod : Node_Id;
7575 For_Access : Boolean := False)
7577 Has_Discrs : constant Boolean := Has_Discriminants (T);
7578 Constrained : constant Boolean :=
7579 (Has_Discrs
7580 and then not Is_Empty_Elmt_List (Elist)
7581 and then not Is_Class_Wide_Type (T))
7582 or else Is_Constrained (T);
7584 begin
7585 if Ekind (T) = E_Record_Type then
7586 if For_Access then
7587 Set_Ekind (Def_Id, E_Private_Subtype);
7588 Set_Is_For_Access_Subtype (Def_Id, True);
7589 else
7590 Set_Ekind (Def_Id, E_Record_Subtype);
7591 end if;
7593 -- Inherit preelaboration flag from base, for types for which it
7594 -- may have been set: records, private types, protected types.
7596 Set_Known_To_Have_Preelab_Init
7597 (Def_Id, Known_To_Have_Preelab_Init (T));
7599 elsif Ekind (T) = E_Task_Type then
7600 Set_Ekind (Def_Id, E_Task_Subtype);
7602 elsif Ekind (T) = E_Protected_Type then
7603 Set_Ekind (Def_Id, E_Protected_Subtype);
7604 Set_Known_To_Have_Preelab_Init
7605 (Def_Id, Known_To_Have_Preelab_Init (T));
7607 elsif Is_Private_Type (T) then
7608 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
7609 Set_Known_To_Have_Preelab_Init
7610 (Def_Id, Known_To_Have_Preelab_Init (T));
7612 elsif Is_Class_Wide_Type (T) then
7613 Set_Ekind (Def_Id, E_Class_Wide_Subtype);
7615 else
7616 -- Incomplete type. Attach subtype to list of dependents, to be
7617 -- completed with full view of parent type, unless is it the
7618 -- designated subtype of a record component within an init_proc.
7619 -- This last case arises for a component of an access type whose
7620 -- designated type is incomplete (e.g. a Taft Amendment type).
7621 -- The designated subtype is within an inner scope, and needs no
7622 -- elaboration, because only the access type is needed in the
7623 -- initialization procedure.
7625 Set_Ekind (Def_Id, Ekind (T));
7627 if For_Access and then Within_Init_Proc then
7628 null;
7629 else
7630 Append_Elmt (Def_Id, Private_Dependents (T));
7631 end if;
7632 end if;
7634 Set_Etype (Def_Id, T);
7635 Init_Size_Align (Def_Id);
7636 Set_Has_Discriminants (Def_Id, Has_Discrs);
7637 Set_Is_Constrained (Def_Id, Constrained);
7639 Set_First_Entity (Def_Id, First_Entity (T));
7640 Set_Last_Entity (Def_Id, Last_Entity (T));
7642 -- If the subtype is the completion of a private declaration, there may
7643 -- have been representation clauses for the partial view, and they must
7644 -- be preserved. Build_Derived_Type chains the inherited clauses with
7645 -- the ones appearing on the extension. If this comes from a subtype
7646 -- declaration, all clauses are inherited.
7648 if No (First_Rep_Item (Def_Id)) then
7649 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
7650 end if;
7652 if Is_Tagged_Type (T) then
7653 Set_Is_Tagged_Type (Def_Id);
7654 Make_Class_Wide_Type (Def_Id);
7655 end if;
7657 Set_Stored_Constraint (Def_Id, No_Elist);
7659 if Has_Discrs then
7660 Set_Discriminant_Constraint (Def_Id, Elist);
7661 Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
7662 end if;
7664 if Is_Tagged_Type (T) then
7666 -- Ada 2005 (AI-251): In case of concurrent types we inherit the
7667 -- concurrent record type (which has the list of primitive
7668 -- operations).
7670 if Ada_Version >= Ada_05
7671 and then Is_Concurrent_Type (T)
7672 then
7673 Set_Corresponding_Record_Type (Def_Id,
7674 Corresponding_Record_Type (T));
7675 else
7676 Set_Primitive_Operations (Def_Id, Primitive_Operations (T));
7677 end if;
7679 Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
7680 end if;
7682 -- Subtypes introduced by component declarations do not need to be
7683 -- marked as delayed, and do not get freeze nodes, because the semantics
7684 -- verifies that the parents of the subtypes are frozen before the
7685 -- enclosing record is frozen.
7687 if not Is_Type (Scope (Def_Id)) then
7688 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
7690 if Is_Private_Type (T)
7691 and then Present (Full_View (T))
7692 then
7693 Conditional_Delay (Def_Id, Full_View (T));
7694 else
7695 Conditional_Delay (Def_Id, T);
7696 end if;
7697 end if;
7699 if Is_Record_Type (T) then
7700 Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
7702 if Has_Discrs
7703 and then not Is_Empty_Elmt_List (Elist)
7704 and then not For_Access
7705 then
7706 Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
7707 elsif not For_Access then
7708 Set_Cloned_Subtype (Def_Id, T);
7709 end if;
7710 end if;
7711 end Build_Discriminated_Subtype;
7713 ---------------------------
7714 -- Build_Itype_Reference --
7715 ---------------------------
7717 procedure Build_Itype_Reference
7718 (Ityp : Entity_Id;
7719 Nod : Node_Id)
7721 IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
7722 begin
7723 Set_Itype (IR, Ityp);
7724 Insert_After (Nod, IR);
7725 end Build_Itype_Reference;
7727 ------------------------
7728 -- Build_Scalar_Bound --
7729 ------------------------
7731 function Build_Scalar_Bound
7732 (Bound : Node_Id;
7733 Par_T : Entity_Id;
7734 Der_T : Entity_Id) return Node_Id
7736 New_Bound : Entity_Id;
7738 begin
7739 -- Note: not clear why this is needed, how can the original bound
7740 -- be unanalyzed at this point? and if it is, what business do we
7741 -- have messing around with it? and why is the base type of the
7742 -- parent type the right type for the resolution. It probably is
7743 -- not! It is OK for the new bound we are creating, but not for
7744 -- the old one??? Still if it never happens, no problem!
7746 Analyze_And_Resolve (Bound, Base_Type (Par_T));
7748 if Nkind_In (Bound, N_Integer_Literal, N_Real_Literal) then
7749 New_Bound := New_Copy (Bound);
7750 Set_Etype (New_Bound, Der_T);
7751 Set_Analyzed (New_Bound);
7753 elsif Is_Entity_Name (Bound) then
7754 New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
7756 -- The following is almost certainly wrong. What business do we have
7757 -- relocating a node (Bound) that is presumably still attached to
7758 -- the tree elsewhere???
7760 else
7761 New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
7762 end if;
7764 Set_Etype (New_Bound, Der_T);
7765 return New_Bound;
7766 end Build_Scalar_Bound;
7768 --------------------------------
7769 -- Build_Underlying_Full_View --
7770 --------------------------------
7772 procedure Build_Underlying_Full_View
7773 (N : Node_Id;
7774 Typ : Entity_Id;
7775 Par : Entity_Id)
7777 Loc : constant Source_Ptr := Sloc (N);
7778 Subt : constant Entity_Id :=
7779 Make_Defining_Identifier
7780 (Loc, New_External_Name (Chars (Typ), 'S'));
7782 Constr : Node_Id;
7783 Indic : Node_Id;
7784 C : Node_Id;
7785 Id : Node_Id;
7787 procedure Set_Discriminant_Name (Id : Node_Id);
7788 -- If the derived type has discriminants, they may rename discriminants
7789 -- of the parent. When building the full view of the parent, we need to
7790 -- recover the names of the original discriminants if the constraint is
7791 -- given by named associations.
7793 ---------------------------
7794 -- Set_Discriminant_Name --
7795 ---------------------------
7797 procedure Set_Discriminant_Name (Id : Node_Id) is
7798 Disc : Entity_Id;
7800 begin
7801 Set_Original_Discriminant (Id, Empty);
7803 if Has_Discriminants (Typ) then
7804 Disc := First_Discriminant (Typ);
7805 while Present (Disc) loop
7806 if Chars (Disc) = Chars (Id)
7807 and then Present (Corresponding_Discriminant (Disc))
7808 then
7809 Set_Chars (Id, Chars (Corresponding_Discriminant (Disc)));
7810 end if;
7811 Next_Discriminant (Disc);
7812 end loop;
7813 end if;
7814 end Set_Discriminant_Name;
7816 -- Start of processing for Build_Underlying_Full_View
7818 begin
7819 if Nkind (N) = N_Full_Type_Declaration then
7820 Constr := Constraint (Subtype_Indication (Type_Definition (N)));
7822 elsif Nkind (N) = N_Subtype_Declaration then
7823 Constr := New_Copy_Tree (Constraint (Subtype_Indication (N)));
7825 elsif Nkind (N) = N_Component_Declaration then
7826 Constr :=
7827 New_Copy_Tree
7828 (Constraint (Subtype_Indication (Component_Definition (N))));
7830 else
7831 raise Program_Error;
7832 end if;
7834 C := First (Constraints (Constr));
7835 while Present (C) loop
7836 if Nkind (C) = N_Discriminant_Association then
7837 Id := First (Selector_Names (C));
7838 while Present (Id) loop
7839 Set_Discriminant_Name (Id);
7840 Next (Id);
7841 end loop;
7842 end if;
7844 Next (C);
7845 end loop;
7847 Indic :=
7848 Make_Subtype_Declaration (Loc,
7849 Defining_Identifier => Subt,
7850 Subtype_Indication =>
7851 Make_Subtype_Indication (Loc,
7852 Subtype_Mark => New_Reference_To (Par, Loc),
7853 Constraint => New_Copy_Tree (Constr)));
7855 -- If this is a component subtype for an outer itype, it is not
7856 -- a list member, so simply set the parent link for analysis: if
7857 -- the enclosing type does not need to be in a declarative list,
7858 -- neither do the components.
7860 if Is_List_Member (N)
7861 and then Nkind (N) /= N_Component_Declaration
7862 then
7863 Insert_Before (N, Indic);
7864 else
7865 Set_Parent (Indic, Parent (N));
7866 end if;
7868 Analyze (Indic);
7869 Set_Underlying_Full_View (Typ, Full_View (Subt));
7870 end Build_Underlying_Full_View;
7872 -------------------------------
7873 -- Check_Abstract_Overriding --
7874 -------------------------------
7876 procedure Check_Abstract_Overriding (T : Entity_Id) is
7877 Alias_Subp : Entity_Id;
7878 Elmt : Elmt_Id;
7879 Op_List : Elist_Id;
7880 Subp : Entity_Id;
7881 Type_Def : Node_Id;
7883 begin
7884 Op_List := Primitive_Operations (T);
7886 -- Loop to check primitive operations
7888 Elmt := First_Elmt (Op_List);
7889 while Present (Elmt) loop
7890 Subp := Node (Elmt);
7891 Alias_Subp := Alias (Subp);
7893 -- Inherited subprograms are identified by the fact that they do not
7894 -- come from source, and the associated source location is the
7895 -- location of the first subtype of the derived type.
7897 -- Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
7898 -- subprograms that "require overriding".
7900 -- Special exception, do not complain about failure to override the
7901 -- stream routines _Input and _Output, as well as the primitive
7902 -- operations used in dispatching selects since we always provide
7903 -- automatic overridings for these subprograms.
7905 -- Also ignore this rule for convention CIL since .NET libraries
7906 -- do bizarre things with interfaces???
7908 -- The partial view of T may have been a private extension, for
7909 -- which inherited functions dispatching on result are abstract.
7910 -- If the full view is a null extension, there is no need for
7911 -- overriding in Ada2005, but wrappers need to be built for them
7912 -- (see exp_ch3, Build_Controlling_Function_Wrappers).
7914 if Is_Null_Extension (T)
7915 and then Has_Controlling_Result (Subp)
7916 and then Ada_Version >= Ada_05
7917 and then Present (Alias_Subp)
7918 and then not Comes_From_Source (Subp)
7919 and then not Is_Abstract_Subprogram (Alias_Subp)
7920 and then not Is_Access_Type (Etype (Subp))
7921 then
7922 null;
7924 -- Ada 2005 (AI-251): Internal entities of interfaces need no
7925 -- processing because this check is done with the aliased
7926 -- entity
7928 elsif Present (Interface_Alias (Subp)) then
7929 null;
7931 elsif (Is_Abstract_Subprogram (Subp)
7932 or else Requires_Overriding (Subp)
7933 or else
7934 (Has_Controlling_Result (Subp)
7935 and then Present (Alias_Subp)
7936 and then not Comes_From_Source (Subp)
7937 and then Sloc (Subp) = Sloc (First_Subtype (T))))
7938 and then not Is_TSS (Subp, TSS_Stream_Input)
7939 and then not Is_TSS (Subp, TSS_Stream_Output)
7940 and then not Is_Abstract_Type (T)
7941 and then Convention (T) /= Convention_CIL
7942 and then not Is_Predefined_Interface_Primitive (Subp)
7944 -- Ada 2005 (AI-251): Do not consider hidden entities associated
7945 -- with abstract interface types because the check will be done
7946 -- with the aliased entity (otherwise we generate a duplicated
7947 -- error message).
7949 and then not Present (Interface_Alias (Subp))
7950 then
7951 if Present (Alias_Subp) then
7953 -- Only perform the check for a derived subprogram when the
7954 -- type has an explicit record extension. This avoids incorrect
7955 -- flagging of abstract subprograms for the case of a type
7956 -- without an extension that is derived from a formal type
7957 -- with a tagged actual (can occur within a private part).
7959 -- Ada 2005 (AI-391): In the case of an inherited function with
7960 -- a controlling result of the type, the rule does not apply if
7961 -- the type is a null extension (unless the parent function
7962 -- itself is abstract, in which case the function must still be
7963 -- be overridden). The expander will generate an overriding
7964 -- wrapper function calling the parent subprogram (see
7965 -- Exp_Ch3.Make_Controlling_Wrapper_Functions).
7967 Type_Def := Type_Definition (Parent (T));
7969 if Nkind (Type_Def) = N_Derived_Type_Definition
7970 and then Present (Record_Extension_Part (Type_Def))
7971 and then
7972 (Ada_Version < Ada_05
7973 or else not Is_Null_Extension (T)
7974 or else Ekind (Subp) = E_Procedure
7975 or else not Has_Controlling_Result (Subp)
7976 or else Is_Abstract_Subprogram (Alias_Subp)
7977 or else Requires_Overriding (Subp)
7978 or else Is_Access_Type (Etype (Subp)))
7979 then
7980 -- Avoid reporting error in case of abstract predefined
7981 -- primitive inherited from interface type because the
7982 -- body of internally generated predefined primitives
7983 -- of tagged types are generated later by Freeze_Type
7985 if Is_Interface (Root_Type (T))
7986 and then Is_Abstract_Subprogram (Subp)
7987 and then Is_Predefined_Dispatching_Operation (Subp)
7988 and then not Comes_From_Source (Ultimate_Alias (Subp))
7989 then
7990 null;
7992 else
7993 Error_Msg_NE
7994 ("type must be declared abstract or & overridden",
7995 T, Subp);
7997 -- Traverse the whole chain of aliased subprograms to
7998 -- complete the error notification. This is especially
7999 -- useful for traceability of the chain of entities when
8000 -- the subprogram corresponds with an interface
8001 -- subprogram (which may be defined in another package).
8003 if Present (Alias_Subp) then
8004 declare
8005 E : Entity_Id;
8007 begin
8008 E := Subp;
8009 while Present (Alias (E)) loop
8010 Error_Msg_Sloc := Sloc (E);
8011 Error_Msg_NE
8012 ("\& has been inherited #", T, Subp);
8013 E := Alias (E);
8014 end loop;
8016 Error_Msg_Sloc := Sloc (E);
8017 Error_Msg_NE
8018 ("\& has been inherited from subprogram #",
8019 T, Subp);
8020 end;
8021 end if;
8022 end if;
8024 -- Ada 2005 (AI-345): Protected or task type implementing
8025 -- abstract interfaces.
8027 elsif Is_Concurrent_Record_Type (T)
8028 and then Present (Interfaces (T))
8029 then
8030 -- The controlling formal of Subp must be of mode "out",
8031 -- "in out" or an access-to-variable to be overridden.
8033 -- Error message below needs rewording (remember comma
8034 -- in -gnatj mode) ???
8036 if Ekind (First_Formal (Subp)) = E_In_Parameter then
8037 if not Is_Predefined_Dispatching_Operation (Subp) then
8038 Error_Msg_NE
8039 ("first formal of & must be of mode `OUT`, " &
8040 "`IN OUT` or access-to-variable", T, Subp);
8041 Error_Msg_N
8042 ("\to be overridden by protected procedure or " &
8043 "entry (RM 9.4(11.9/2))", T);
8044 end if;
8046 -- Some other kind of overriding failure
8048 else
8049 Error_Msg_NE
8050 ("interface subprogram & must be overridden",
8051 T, Subp);
8052 end if;
8053 end if;
8055 else
8056 Error_Msg_Node_2 := T;
8057 Error_Msg_N
8058 ("abstract subprogram& not allowed for type&", Subp);
8060 -- Also post unconditional warning on the type (unconditional
8061 -- so that if there are more than one of these cases, we get
8062 -- them all, and not just the first one).
8064 Error_Msg_Node_2 := Subp;
8065 Error_Msg_N
8066 ("nonabstract type& has abstract subprogram&!", T);
8067 end if;
8068 end if;
8070 -- Ada 2005 (AI05-0030): Inspect hidden subprograms which provide
8071 -- the mapping between interface and implementing type primitives.
8072 -- If the interface alias is marked as Implemented_By_Entry, the
8073 -- alias must be an entry wrapper.
8075 if Ada_Version >= Ada_05
8076 and then Is_Hidden (Subp)
8077 and then Present (Interface_Alias (Subp))
8078 and then Implemented_By_Entry (Interface_Alias (Subp))
8079 and then Present (Alias_Subp)
8080 and then
8081 (not Is_Primitive_Wrapper (Alias_Subp)
8082 or else Ekind (Wrapped_Entity (Alias_Subp)) /= E_Entry)
8083 then
8084 declare
8085 Error_Ent : Entity_Id := T;
8087 begin
8088 if Is_Concurrent_Record_Type (Error_Ent) then
8089 Error_Ent := Corresponding_Concurrent_Type (Error_Ent);
8090 end if;
8092 Error_Msg_Node_2 := Interface_Alias (Subp);
8093 Error_Msg_NE
8094 ("type & must implement abstract subprogram & with an entry",
8095 Error_Ent, Error_Ent);
8096 end;
8097 end if;
8099 Next_Elmt (Elmt);
8100 end loop;
8101 end Check_Abstract_Overriding;
8103 ------------------------------------------------
8104 -- Check_Access_Discriminant_Requires_Limited --
8105 ------------------------------------------------
8107 procedure Check_Access_Discriminant_Requires_Limited
8108 (D : Node_Id;
8109 Loc : Node_Id)
8111 begin
8112 -- A discriminant_specification for an access discriminant shall appear
8113 -- only in the declaration for a task or protected type, or for a type
8114 -- with the reserved word 'limited' in its definition or in one of its
8115 -- ancestors. (RM 3.7(10))
8117 if Nkind (Discriminant_Type (D)) = N_Access_Definition
8118 and then not Is_Concurrent_Type (Current_Scope)
8119 and then not Is_Concurrent_Record_Type (Current_Scope)
8120 and then not Is_Limited_Record (Current_Scope)
8121 and then Ekind (Current_Scope) /= E_Limited_Private_Type
8122 then
8123 Error_Msg_N
8124 ("access discriminants allowed only for limited types", Loc);
8125 end if;
8126 end Check_Access_Discriminant_Requires_Limited;
8128 -----------------------------------
8129 -- Check_Aliased_Component_Types --
8130 -----------------------------------
8132 procedure Check_Aliased_Component_Types (T : Entity_Id) is
8133 C : Entity_Id;
8135 begin
8136 -- ??? Also need to check components of record extensions, but not
8137 -- components of protected types (which are always limited).
8139 -- Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
8140 -- types to be unconstrained. This is safe because it is illegal to
8141 -- create access subtypes to such types with explicit discriminant
8142 -- constraints.
8144 if not Is_Limited_Type (T) then
8145 if Ekind (T) = E_Record_Type then
8146 C := First_Component (T);
8147 while Present (C) loop
8148 if Is_Aliased (C)
8149 and then Has_Discriminants (Etype (C))
8150 and then not Is_Constrained (Etype (C))
8151 and then not In_Instance_Body
8152 and then Ada_Version < Ada_05
8153 then
8154 Error_Msg_N
8155 ("aliased component must be constrained (RM 3.6(11))",
8157 end if;
8159 Next_Component (C);
8160 end loop;
8162 elsif Ekind (T) = E_Array_Type then
8163 if Has_Aliased_Components (T)
8164 and then Has_Discriminants (Component_Type (T))
8165 and then not Is_Constrained (Component_Type (T))
8166 and then not In_Instance_Body
8167 and then Ada_Version < Ada_05
8168 then
8169 Error_Msg_N
8170 ("aliased component type must be constrained (RM 3.6(11))",
8172 end if;
8173 end if;
8174 end if;
8175 end Check_Aliased_Component_Types;
8177 ----------------------
8178 -- Check_Completion --
8179 ----------------------
8181 procedure Check_Completion (Body_Id : Node_Id := Empty) is
8182 E : Entity_Id;
8184 procedure Post_Error;
8185 -- Post error message for lack of completion for entity E
8187 ----------------
8188 -- Post_Error --
8189 ----------------
8191 procedure Post_Error is
8192 begin
8193 if not Comes_From_Source (E) then
8195 if Ekind (E) = E_Task_Type
8196 or else Ekind (E) = E_Protected_Type
8197 then
8198 -- It may be an anonymous protected type created for a
8199 -- single variable. Post error on variable, if present.
8201 declare
8202 Var : Entity_Id;
8204 begin
8205 Var := First_Entity (Current_Scope);
8206 while Present (Var) loop
8207 exit when Etype (Var) = E
8208 and then Comes_From_Source (Var);
8210 Next_Entity (Var);
8211 end loop;
8213 if Present (Var) then
8214 E := Var;
8215 end if;
8216 end;
8217 end if;
8218 end if;
8220 -- If a generated entity has no completion, then either previous
8221 -- semantic errors have disabled the expansion phase, or else we had
8222 -- missing subunits, or else we are compiling without expansion,
8223 -- or else something is very wrong.
8225 if not Comes_From_Source (E) then
8226 pragma Assert
8227 (Serious_Errors_Detected > 0
8228 or else Configurable_Run_Time_Violations > 0
8229 or else Subunits_Missing
8230 or else not Expander_Active);
8231 return;
8233 -- Here for source entity
8235 else
8236 -- Here if no body to post the error message, so we post the error
8237 -- on the declaration that has no completion. This is not really
8238 -- the right place to post it, think about this later ???
8240 if No (Body_Id) then
8241 if Is_Type (E) then
8242 Error_Msg_NE
8243 ("missing full declaration for }", Parent (E), E);
8244 else
8245 Error_Msg_NE
8246 ("missing body for &", Parent (E), E);
8247 end if;
8249 -- Package body has no completion for a declaration that appears
8250 -- in the corresponding spec. Post error on the body, with a
8251 -- reference to the non-completed declaration.
8253 else
8254 Error_Msg_Sloc := Sloc (E);
8256 if Is_Type (E) then
8257 Error_Msg_NE
8258 ("missing full declaration for }!", Body_Id, E);
8260 elsif Is_Overloadable (E)
8261 and then Current_Entity_In_Scope (E) /= E
8262 then
8263 -- It may be that the completion is mistyped and appears as
8264 -- a distinct overloading of the entity.
8266 declare
8267 Candidate : constant Entity_Id :=
8268 Current_Entity_In_Scope (E);
8269 Decl : constant Node_Id :=
8270 Unit_Declaration_Node (Candidate);
8272 begin
8273 if Is_Overloadable (Candidate)
8274 and then Ekind (Candidate) = Ekind (E)
8275 and then Nkind (Decl) = N_Subprogram_Body
8276 and then Acts_As_Spec (Decl)
8277 then
8278 Check_Type_Conformant (Candidate, E);
8280 else
8281 Error_Msg_NE ("missing body for & declared#!",
8282 Body_Id, E);
8283 end if;
8284 end;
8285 else
8286 Error_Msg_NE ("missing body for & declared#!",
8287 Body_Id, E);
8288 end if;
8289 end if;
8290 end if;
8291 end Post_Error;
8293 -- Start processing for Check_Completion
8295 begin
8296 E := First_Entity (Current_Scope);
8297 while Present (E) loop
8298 if Is_Intrinsic_Subprogram (E) then
8299 null;
8301 -- The following situation requires special handling: a child unit
8302 -- that appears in the context clause of the body of its parent:
8304 -- procedure Parent.Child (...);
8306 -- with Parent.Child;
8307 -- package body Parent is
8309 -- Here Parent.Child appears as a local entity, but should not be
8310 -- flagged as requiring completion, because it is a compilation
8311 -- unit.
8313 -- Ignore missing completion for a subprogram that does not come from
8314 -- source (including the _Call primitive operation of RAS types,
8315 -- which has to have the flag Comes_From_Source for other purposes):
8316 -- we assume that the expander will provide the missing completion.
8318 elsif Ekind (E) = E_Function
8319 or else Ekind (E) = E_Procedure
8320 or else Ekind (E) = E_Generic_Function
8321 or else Ekind (E) = E_Generic_Procedure
8322 then
8323 if not Has_Completion (E)
8324 and then not (Is_Subprogram (E)
8325 and then Is_Abstract_Subprogram (E))
8326 and then not (Is_Subprogram (E)
8327 and then
8328 (not Comes_From_Source (E)
8329 or else Chars (E) = Name_uCall))
8330 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
8331 N_Compilation_Unit
8332 and then Chars (E) /= Name_uSize
8333 then
8334 Post_Error;
8335 end if;
8337 elsif Is_Entry (E) then
8338 if not Has_Completion (E) and then
8339 (Ekind (Scope (E)) = E_Protected_Object
8340 or else Ekind (Scope (E)) = E_Protected_Type)
8341 then
8342 Post_Error;
8343 end if;
8345 elsif Is_Package_Or_Generic_Package (E) then
8346 if Unit_Requires_Body (E) then
8347 if not Has_Completion (E)
8348 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
8349 N_Compilation_Unit
8350 then
8351 Post_Error;
8352 end if;
8354 elsif not Is_Child_Unit (E) then
8355 May_Need_Implicit_Body (E);
8356 end if;
8358 elsif Ekind (E) = E_Incomplete_Type
8359 and then No (Underlying_Type (E))
8360 then
8361 Post_Error;
8363 elsif (Ekind (E) = E_Task_Type or else
8364 Ekind (E) = E_Protected_Type)
8365 and then not Has_Completion (E)
8366 then
8367 Post_Error;
8369 -- A single task declared in the current scope is a constant, verify
8370 -- that the body of its anonymous type is in the same scope. If the
8371 -- task is defined elsewhere, this may be a renaming declaration for
8372 -- which no completion is needed.
8374 elsif Ekind (E) = E_Constant
8375 and then Ekind (Etype (E)) = E_Task_Type
8376 and then not Has_Completion (Etype (E))
8377 and then Scope (Etype (E)) = Current_Scope
8378 then
8379 Post_Error;
8381 elsif Ekind (E) = E_Protected_Object
8382 and then not Has_Completion (Etype (E))
8383 then
8384 Post_Error;
8386 elsif Ekind (E) = E_Record_Type then
8387 if Is_Tagged_Type (E) then
8388 Check_Abstract_Overriding (E);
8389 Check_Conventions (E);
8390 end if;
8392 Check_Aliased_Component_Types (E);
8394 elsif Ekind (E) = E_Array_Type then
8395 Check_Aliased_Component_Types (E);
8397 end if;
8399 Next_Entity (E);
8400 end loop;
8401 end Check_Completion;
8403 ----------------------------
8404 -- Check_Delta_Expression --
8405 ----------------------------
8407 procedure Check_Delta_Expression (E : Node_Id) is
8408 begin
8409 if not (Is_Real_Type (Etype (E))) then
8410 Wrong_Type (E, Any_Real);
8412 elsif not Is_OK_Static_Expression (E) then
8413 Flag_Non_Static_Expr
8414 ("non-static expression used for delta value!", E);
8416 elsif not UR_Is_Positive (Expr_Value_R (E)) then
8417 Error_Msg_N ("delta expression must be positive", E);
8419 else
8420 return;
8421 end if;
8423 -- If any of above errors occurred, then replace the incorrect
8424 -- expression by the real 0.1, which should prevent further errors.
8426 Rewrite (E,
8427 Make_Real_Literal (Sloc (E), Ureal_Tenth));
8428 Analyze_And_Resolve (E, Standard_Float);
8429 end Check_Delta_Expression;
8431 -----------------------------
8432 -- Check_Digits_Expression --
8433 -----------------------------
8435 procedure Check_Digits_Expression (E : Node_Id) is
8436 begin
8437 if not (Is_Integer_Type (Etype (E))) then
8438 Wrong_Type (E, Any_Integer);
8440 elsif not Is_OK_Static_Expression (E) then
8441 Flag_Non_Static_Expr
8442 ("non-static expression used for digits value!", E);
8444 elsif Expr_Value (E) <= 0 then
8445 Error_Msg_N ("digits value must be greater than zero", E);
8447 else
8448 return;
8449 end if;
8451 -- If any of above errors occurred, then replace the incorrect
8452 -- expression by the integer 1, which should prevent further errors.
8454 Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
8455 Analyze_And_Resolve (E, Standard_Integer);
8457 end Check_Digits_Expression;
8459 --------------------------
8460 -- Check_Initialization --
8461 --------------------------
8463 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
8464 begin
8465 if Is_Limited_Type (T)
8466 and then not In_Instance
8467 and then not In_Inlined_Body
8468 then
8469 if not OK_For_Limited_Init (Exp) then
8471 -- In GNAT mode, this is just a warning, to allow it to be evilly
8472 -- turned off. Otherwise it is a real error.
8474 if GNAT_Mode then
8475 Error_Msg_N
8476 ("?cannot initialize entities of limited type!", Exp);
8478 elsif Ada_Version < Ada_05 then
8479 Error_Msg_N
8480 ("cannot initialize entities of limited type", Exp);
8481 Explain_Limited_Type (T, Exp);
8483 else
8484 -- Specialize error message according to kind of illegal
8485 -- initial expression.
8487 if Nkind (Exp) = N_Type_Conversion
8488 and then Nkind (Expression (Exp)) = N_Function_Call
8489 then
8490 Error_Msg_N
8491 ("illegal context for call"
8492 & " to function with limited result", Exp);
8494 else
8495 Error_Msg_N
8496 ("initialization of limited object requires aggregate "
8497 & "or function call", Exp);
8498 end if;
8499 end if;
8500 end if;
8501 end if;
8502 end Check_Initialization;
8504 ----------------------
8505 -- Check_Interfaces --
8506 ----------------------
8508 procedure Check_Interfaces (N : Node_Id; Def : Node_Id) is
8509 Parent_Type : constant Entity_Id := Etype (Defining_Identifier (N));
8511 Iface : Node_Id;
8512 Iface_Def : Node_Id;
8513 Iface_Typ : Entity_Id;
8514 Parent_Node : Node_Id;
8516 Is_Task : Boolean := False;
8517 -- Set True if parent type or any progenitor is a task interface
8519 Is_Protected : Boolean := False;
8520 -- Set True if parent type or any progenitor is a protected interface
8522 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
8523 -- Check that a progenitor is compatible with declaration.
8524 -- Error is posted on Error_Node.
8526 ------------------
8527 -- Check_Ifaces --
8528 ------------------
8530 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
8531 Iface_Id : constant Entity_Id :=
8532 Defining_Identifier (Parent (Iface_Def));
8533 Type_Def : Node_Id;
8535 begin
8536 if Nkind (N) = N_Private_Extension_Declaration then
8537 Type_Def := N;
8538 else
8539 Type_Def := Type_Definition (N);
8540 end if;
8542 if Is_Task_Interface (Iface_Id) then
8543 Is_Task := True;
8545 elsif Is_Protected_Interface (Iface_Id) then
8546 Is_Protected := True;
8547 end if;
8549 -- Check that the characteristics of the progenitor are compatible
8550 -- with the explicit qualifier in the declaration.
8551 -- The check only applies to qualifiers that come from source.
8552 -- Limited_Present also appears in the declaration of corresponding
8553 -- records, and the check does not apply to them.
8555 if Limited_Present (Type_Def)
8556 and then not
8557 Is_Concurrent_Record_Type (Defining_Identifier (N))
8558 then
8559 if Is_Limited_Interface (Parent_Type)
8560 and then not Is_Limited_Interface (Iface_Id)
8561 then
8562 Error_Msg_NE
8563 ("progenitor& must be limited interface",
8564 Error_Node, Iface_Id);
8566 elsif
8567 (Task_Present (Iface_Def)
8568 or else Protected_Present (Iface_Def)
8569 or else Synchronized_Present (Iface_Def))
8570 and then Nkind (N) /= N_Private_Extension_Declaration
8571 then
8572 Error_Msg_NE
8573 ("progenitor& must be limited interface",
8574 Error_Node, Iface_Id);
8575 end if;
8577 -- Protected interfaces can only inherit from limited, synchronized
8578 -- or protected interfaces.
8580 elsif Nkind (N) = N_Full_Type_Declaration
8581 and then Protected_Present (Type_Def)
8582 then
8583 if Limited_Present (Iface_Def)
8584 or else Synchronized_Present (Iface_Def)
8585 or else Protected_Present (Iface_Def)
8586 then
8587 null;
8589 elsif Task_Present (Iface_Def) then
8590 Error_Msg_N ("(Ada 2005) protected interface cannot inherit"
8591 & " from task interface", Error_Node);
8593 else
8594 Error_Msg_N ("(Ada 2005) protected interface cannot inherit"
8595 & " from non-limited interface", Error_Node);
8596 end if;
8598 -- Ada 2005 (AI-345): Synchronized interfaces can only inherit from
8599 -- limited and synchronized.
8601 elsif Synchronized_Present (Type_Def) then
8602 if Limited_Present (Iface_Def)
8603 or else Synchronized_Present (Iface_Def)
8604 then
8605 null;
8607 elsif Protected_Present (Iface_Def)
8608 and then Nkind (N) /= N_Private_Extension_Declaration
8609 then
8610 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
8611 & " from protected interface", Error_Node);
8613 elsif Task_Present (Iface_Def)
8614 and then Nkind (N) /= N_Private_Extension_Declaration
8615 then
8616 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
8617 & " from task interface", Error_Node);
8619 elsif not Is_Limited_Interface (Iface_Id) then
8620 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
8621 & " from non-limited interface", Error_Node);
8622 end if;
8624 -- Ada 2005 (AI-345): Task interfaces can only inherit from limited,
8625 -- synchronized or task interfaces.
8627 elsif Nkind (N) = N_Full_Type_Declaration
8628 and then Task_Present (Type_Def)
8629 then
8630 if Limited_Present (Iface_Def)
8631 or else Synchronized_Present (Iface_Def)
8632 or else Task_Present (Iface_Def)
8633 then
8634 null;
8636 elsif Protected_Present (Iface_Def) then
8637 Error_Msg_N ("(Ada 2005) task interface cannot inherit from"
8638 & " protected interface", Error_Node);
8640 else
8641 Error_Msg_N ("(Ada 2005) task interface cannot inherit from"
8642 & " non-limited interface", Error_Node);
8643 end if;
8644 end if;
8645 end Check_Ifaces;
8647 -- Start of processing for Check_Interfaces
8649 begin
8650 if Is_Interface (Parent_Type) then
8651 if Is_Task_Interface (Parent_Type) then
8652 Is_Task := True;
8654 elsif Is_Protected_Interface (Parent_Type) then
8655 Is_Protected := True;
8656 end if;
8657 end if;
8659 if Nkind (N) = N_Private_Extension_Declaration then
8661 -- Check that progenitors are compatible with declaration
8663 Iface := First (Interface_List (Def));
8664 while Present (Iface) loop
8665 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
8667 Parent_Node := Parent (Base_Type (Iface_Typ));
8668 Iface_Def := Type_Definition (Parent_Node);
8670 if not Is_Interface (Iface_Typ) then
8671 Diagnose_Interface (Iface, Iface_Typ);
8673 else
8674 Check_Ifaces (Iface_Def, Iface);
8675 end if;
8677 Next (Iface);
8678 end loop;
8680 if Is_Task and Is_Protected then
8681 Error_Msg_N
8682 ("type cannot derive from task and protected interface", N);
8683 end if;
8685 return;
8686 end if;
8688 -- Full type declaration of derived type.
8689 -- Check compatibility with parent if it is interface type
8691 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
8692 and then Is_Interface (Parent_Type)
8693 then
8694 Parent_Node := Parent (Parent_Type);
8696 -- More detailed checks for interface varieties
8698 Check_Ifaces
8699 (Iface_Def => Type_Definition (Parent_Node),
8700 Error_Node => Subtype_Indication (Type_Definition (N)));
8701 end if;
8703 Iface := First (Interface_List (Def));
8704 while Present (Iface) loop
8705 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
8707 Parent_Node := Parent (Base_Type (Iface_Typ));
8708 Iface_Def := Type_Definition (Parent_Node);
8710 if not Is_Interface (Iface_Typ) then
8711 Diagnose_Interface (Iface, Iface_Typ);
8713 else
8714 -- "The declaration of a specific descendant of an interface
8715 -- type freezes the interface type" RM 13.14
8717 Freeze_Before (N, Iface_Typ);
8718 Check_Ifaces (Iface_Def, Error_Node => Iface);
8719 end if;
8721 Next (Iface);
8722 end loop;
8724 if Is_Task and Is_Protected then
8725 Error_Msg_N
8726 ("type cannot derive from task and protected interface", N);
8727 end if;
8728 end Check_Interfaces;
8730 ------------------------------------
8731 -- Check_Or_Process_Discriminants --
8732 ------------------------------------
8734 -- If an incomplete or private type declaration was already given for the
8735 -- type, the discriminants may have already been processed if they were
8736 -- present on the incomplete declaration. In this case a full conformance
8737 -- check is performed otherwise just process them.
8739 procedure Check_Or_Process_Discriminants
8740 (N : Node_Id;
8741 T : Entity_Id;
8742 Prev : Entity_Id := Empty)
8744 begin
8745 if Has_Discriminants (T) then
8747 -- Make the discriminants visible to component declarations
8749 declare
8750 D : Entity_Id;
8751 Prev : Entity_Id;
8753 begin
8754 D := First_Discriminant (T);
8755 while Present (D) loop
8756 Prev := Current_Entity (D);
8757 Set_Current_Entity (D);
8758 Set_Is_Immediately_Visible (D);
8759 Set_Homonym (D, Prev);
8761 -- Ada 2005 (AI-230): Access discriminant allowed in
8762 -- non-limited record types.
8764 if Ada_Version < Ada_05 then
8766 -- This restriction gets applied to the full type here. It
8767 -- has already been applied earlier to the partial view.
8769 Check_Access_Discriminant_Requires_Limited (Parent (D), N);
8770 end if;
8772 Next_Discriminant (D);
8773 end loop;
8774 end;
8776 elsif Present (Discriminant_Specifications (N)) then
8777 Process_Discriminants (N, Prev);
8778 end if;
8779 end Check_Or_Process_Discriminants;
8781 ----------------------
8782 -- Check_Real_Bound --
8783 ----------------------
8785 procedure Check_Real_Bound (Bound : Node_Id) is
8786 begin
8787 if not Is_Real_Type (Etype (Bound)) then
8788 Error_Msg_N
8789 ("bound in real type definition must be of real type", Bound);
8791 elsif not Is_OK_Static_Expression (Bound) then
8792 Flag_Non_Static_Expr
8793 ("non-static expression used for real type bound!", Bound);
8795 else
8796 return;
8797 end if;
8799 Rewrite
8800 (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
8801 Analyze (Bound);
8802 Resolve (Bound, Standard_Float);
8803 end Check_Real_Bound;
8805 ------------------------------
8806 -- Complete_Private_Subtype --
8807 ------------------------------
8809 procedure Complete_Private_Subtype
8810 (Priv : Entity_Id;
8811 Full : Entity_Id;
8812 Full_Base : Entity_Id;
8813 Related_Nod : Node_Id)
8815 Save_Next_Entity : Entity_Id;
8816 Save_Homonym : Entity_Id;
8818 begin
8819 -- Set semantic attributes for (implicit) private subtype completion.
8820 -- If the full type has no discriminants, then it is a copy of the full
8821 -- view of the base. Otherwise, it is a subtype of the base with a
8822 -- possible discriminant constraint. Save and restore the original
8823 -- Next_Entity field of full to ensure that the calls to Copy_Node
8824 -- do not corrupt the entity chain.
8826 -- Note that the type of the full view is the same entity as the type of
8827 -- the partial view. In this fashion, the subtype has access to the
8828 -- correct view of the parent.
8830 Save_Next_Entity := Next_Entity (Full);
8831 Save_Homonym := Homonym (Priv);
8833 case Ekind (Full_Base) is
8834 when E_Record_Type |
8835 E_Record_Subtype |
8836 Class_Wide_Kind |
8837 Private_Kind |
8838 Task_Kind |
8839 Protected_Kind =>
8840 Copy_Node (Priv, Full);
8842 Set_Has_Discriminants (Full, Has_Discriminants (Full_Base));
8843 Set_First_Entity (Full, First_Entity (Full_Base));
8844 Set_Last_Entity (Full, Last_Entity (Full_Base));
8846 when others =>
8847 Copy_Node (Full_Base, Full);
8848 Set_Chars (Full, Chars (Priv));
8849 Conditional_Delay (Full, Priv);
8850 Set_Sloc (Full, Sloc (Priv));
8851 end case;
8853 Set_Next_Entity (Full, Save_Next_Entity);
8854 Set_Homonym (Full, Save_Homonym);
8855 Set_Associated_Node_For_Itype (Full, Related_Nod);
8857 -- Set common attributes for all subtypes
8859 Set_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
8861 -- The Etype of the full view is inconsistent. Gigi needs to see the
8862 -- structural full view, which is what the current scheme gives:
8863 -- the Etype of the full view is the etype of the full base. However,
8864 -- if the full base is a derived type, the full view then looks like
8865 -- a subtype of the parent, not a subtype of the full base. If instead
8866 -- we write:
8868 -- Set_Etype (Full, Full_Base);
8870 -- then we get inconsistencies in the front-end (confusion between
8871 -- views). Several outstanding bugs are related to this ???
8873 Set_Is_First_Subtype (Full, False);
8874 Set_Scope (Full, Scope (Priv));
8875 Set_Size_Info (Full, Full_Base);
8876 Set_RM_Size (Full, RM_Size (Full_Base));
8877 Set_Is_Itype (Full);
8879 -- A subtype of a private-type-without-discriminants, whose full-view
8880 -- has discriminants with default expressions, is not constrained!
8882 if not Has_Discriminants (Priv) then
8883 Set_Is_Constrained (Full, Is_Constrained (Full_Base));
8885 if Has_Discriminants (Full_Base) then
8886 Set_Discriminant_Constraint
8887 (Full, Discriminant_Constraint (Full_Base));
8889 -- The partial view may have been indefinite, the full view
8890 -- might not be.
8892 Set_Has_Unknown_Discriminants
8893 (Full, Has_Unknown_Discriminants (Full_Base));
8894 end if;
8895 end if;
8897 Set_First_Rep_Item (Full, First_Rep_Item (Full_Base));
8898 Set_Depends_On_Private (Full, Has_Private_Component (Full));
8900 -- Freeze the private subtype entity if its parent is delayed, and not
8901 -- already frozen. We skip this processing if the type is an anonymous
8902 -- subtype of a record component, or is the corresponding record of a
8903 -- protected type, since ???
8905 if not Is_Type (Scope (Full)) then
8906 Set_Has_Delayed_Freeze (Full,
8907 Has_Delayed_Freeze (Full_Base)
8908 and then (not Is_Frozen (Full_Base)));
8909 end if;
8911 Set_Freeze_Node (Full, Empty);
8912 Set_Is_Frozen (Full, False);
8913 Set_Full_View (Priv, Full);
8915 if Has_Discriminants (Full) then
8916 Set_Stored_Constraint_From_Discriminant_Constraint (Full);
8917 Set_Stored_Constraint (Priv, Stored_Constraint (Full));
8919 if Has_Unknown_Discriminants (Full) then
8920 Set_Discriminant_Constraint (Full, No_Elist);
8921 end if;
8922 end if;
8924 if Ekind (Full_Base) = E_Record_Type
8925 and then Has_Discriminants (Full_Base)
8926 and then Has_Discriminants (Priv) -- might not, if errors
8927 and then not Has_Unknown_Discriminants (Priv)
8928 and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
8929 then
8930 Create_Constrained_Components
8931 (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
8933 -- If the full base is itself derived from private, build a congruent
8934 -- subtype of its underlying type, for use by the back end. For a
8935 -- constrained record component, the declaration cannot be placed on
8936 -- the component list, but it must nevertheless be built an analyzed, to
8937 -- supply enough information for Gigi to compute the size of component.
8939 elsif Ekind (Full_Base) in Private_Kind
8940 and then Is_Derived_Type (Full_Base)
8941 and then Has_Discriminants (Full_Base)
8942 and then (Ekind (Current_Scope) /= E_Record_Subtype)
8943 then
8944 if not Is_Itype (Priv)
8945 and then
8946 Nkind (Subtype_Indication (Parent (Priv))) = N_Subtype_Indication
8947 then
8948 Build_Underlying_Full_View
8949 (Parent (Priv), Full, Etype (Full_Base));
8951 elsif Nkind (Related_Nod) = N_Component_Declaration then
8952 Build_Underlying_Full_View (Related_Nod, Full, Etype (Full_Base));
8953 end if;
8955 elsif Is_Record_Type (Full_Base) then
8957 -- Show Full is simply a renaming of Full_Base
8959 Set_Cloned_Subtype (Full, Full_Base);
8960 end if;
8962 -- It is unsafe to share to bounds of a scalar type, because the Itype
8963 -- is elaborated on demand, and if a bound is non-static then different
8964 -- orders of elaboration in different units will lead to different
8965 -- external symbols.
8967 if Is_Scalar_Type (Full_Base) then
8968 Set_Scalar_Range (Full,
8969 Make_Range (Sloc (Related_Nod),
8970 Low_Bound =>
8971 Duplicate_Subexpr_No_Checks (Type_Low_Bound (Full_Base)),
8972 High_Bound =>
8973 Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
8975 -- This completion inherits the bounds of the full parent, but if
8976 -- the parent is an unconstrained floating point type, so is the
8977 -- completion.
8979 if Is_Floating_Point_Type (Full_Base) then
8980 Set_Includes_Infinities
8981 (Scalar_Range (Full), Has_Infinities (Full_Base));
8982 end if;
8983 end if;
8985 -- ??? It seems that a lot of fields are missing that should be copied
8986 -- from Full_Base to Full. Here are some that are introduced in a
8987 -- non-disruptive way but a cleanup is necessary.
8989 if Is_Tagged_Type (Full_Base) then
8990 Set_Is_Tagged_Type (Full);
8991 Set_Primitive_Operations (Full, Primitive_Operations (Full_Base));
8992 Set_Class_Wide_Type (Full, Class_Wide_Type (Full_Base));
8994 -- If this is a subtype of a protected or task type, constrain its
8995 -- corresponding record, unless this is a subtype without constraints,
8996 -- i.e. a simple renaming as with an actual subtype in an instance.
8998 elsif Is_Concurrent_Type (Full_Base) then
8999 if Has_Discriminants (Full)
9000 and then Present (Corresponding_Record_Type (Full_Base))
9001 and then
9002 not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
9003 then
9004 Set_Corresponding_Record_Type (Full,
9005 Constrain_Corresponding_Record
9006 (Full, Corresponding_Record_Type (Full_Base),
9007 Related_Nod, Full_Base));
9009 else
9010 Set_Corresponding_Record_Type (Full,
9011 Corresponding_Record_Type (Full_Base));
9012 end if;
9013 end if;
9014 end Complete_Private_Subtype;
9016 ----------------------------
9017 -- Constant_Redeclaration --
9018 ----------------------------
9020 procedure Constant_Redeclaration
9021 (Id : Entity_Id;
9022 N : Node_Id;
9023 T : out Entity_Id)
9025 Prev : constant Entity_Id := Current_Entity_In_Scope (Id);
9026 Obj_Def : constant Node_Id := Object_Definition (N);
9027 New_T : Entity_Id;
9029 procedure Check_Possible_Deferred_Completion
9030 (Prev_Id : Entity_Id;
9031 Prev_Obj_Def : Node_Id;
9032 Curr_Obj_Def : Node_Id);
9033 -- Determine whether the two object definitions describe the partial
9034 -- and the full view of a constrained deferred constant. Generate
9035 -- a subtype for the full view and verify that it statically matches
9036 -- the subtype of the partial view.
9038 procedure Check_Recursive_Declaration (Typ : Entity_Id);
9039 -- If deferred constant is an access type initialized with an allocator,
9040 -- check whether there is an illegal recursion in the definition,
9041 -- through a default value of some record subcomponent. This is normally
9042 -- detected when generating init procs, but requires this additional
9043 -- mechanism when expansion is disabled.
9045 ----------------------------------------
9046 -- Check_Possible_Deferred_Completion --
9047 ----------------------------------------
9049 procedure Check_Possible_Deferred_Completion
9050 (Prev_Id : Entity_Id;
9051 Prev_Obj_Def : Node_Id;
9052 Curr_Obj_Def : Node_Id)
9054 begin
9055 if Nkind (Prev_Obj_Def) = N_Subtype_Indication
9056 and then Present (Constraint (Prev_Obj_Def))
9057 and then Nkind (Curr_Obj_Def) = N_Subtype_Indication
9058 and then Present (Constraint (Curr_Obj_Def))
9059 then
9060 declare
9061 Loc : constant Source_Ptr := Sloc (N);
9062 Def_Id : constant Entity_Id :=
9063 Make_Defining_Identifier (Loc,
9064 New_Internal_Name ('S'));
9065 Decl : constant Node_Id :=
9066 Make_Subtype_Declaration (Loc,
9067 Defining_Identifier =>
9068 Def_Id,
9069 Subtype_Indication =>
9070 Relocate_Node (Curr_Obj_Def));
9072 begin
9073 Insert_Before_And_Analyze (N, Decl);
9074 Set_Etype (Id, Def_Id);
9076 if not Subtypes_Statically_Match (Etype (Prev_Id), Def_Id) then
9077 Error_Msg_Sloc := Sloc (Prev_Id);
9078 Error_Msg_N ("subtype does not statically match deferred " &
9079 "declaration#", N);
9080 end if;
9081 end;
9082 end if;
9083 end Check_Possible_Deferred_Completion;
9085 ---------------------------------
9086 -- Check_Recursive_Declaration --
9087 ---------------------------------
9089 procedure Check_Recursive_Declaration (Typ : Entity_Id) is
9090 Comp : Entity_Id;
9092 begin
9093 if Is_Record_Type (Typ) then
9094 Comp := First_Component (Typ);
9095 while Present (Comp) loop
9096 if Comes_From_Source (Comp) then
9097 if Present (Expression (Parent (Comp)))
9098 and then Is_Entity_Name (Expression (Parent (Comp)))
9099 and then Entity (Expression (Parent (Comp))) = Prev
9100 then
9101 Error_Msg_Sloc := Sloc (Parent (Comp));
9102 Error_Msg_NE
9103 ("illegal circularity with declaration for&#",
9104 N, Comp);
9105 return;
9107 elsif Is_Record_Type (Etype (Comp)) then
9108 Check_Recursive_Declaration (Etype (Comp));
9109 end if;
9110 end if;
9112 Next_Component (Comp);
9113 end loop;
9114 end if;
9115 end Check_Recursive_Declaration;
9117 -- Start of processing for Constant_Redeclaration
9119 begin
9120 if Nkind (Parent (Prev)) = N_Object_Declaration then
9121 if Nkind (Object_Definition
9122 (Parent (Prev))) = N_Subtype_Indication
9123 then
9124 -- Find type of new declaration. The constraints of the two
9125 -- views must match statically, but there is no point in
9126 -- creating an itype for the full view.
9128 if Nkind (Obj_Def) = N_Subtype_Indication then
9129 Find_Type (Subtype_Mark (Obj_Def));
9130 New_T := Entity (Subtype_Mark (Obj_Def));
9132 else
9133 Find_Type (Obj_Def);
9134 New_T := Entity (Obj_Def);
9135 end if;
9137 T := Etype (Prev);
9139 else
9140 -- The full view may impose a constraint, even if the partial
9141 -- view does not, so construct the subtype.
9143 New_T := Find_Type_Of_Object (Obj_Def, N);
9144 T := New_T;
9145 end if;
9147 else
9148 -- Current declaration is illegal, diagnosed below in Enter_Name
9150 T := Empty;
9151 New_T := Any_Type;
9152 end if;
9154 -- If previous full declaration exists, or if a homograph is present,
9155 -- let Enter_Name handle it, either with an error, or with the removal
9156 -- of an overridden implicit subprogram.
9158 if Ekind (Prev) /= E_Constant
9159 or else Present (Expression (Parent (Prev)))
9160 or else Present (Full_View (Prev))
9161 then
9162 Enter_Name (Id);
9164 -- Verify that types of both declarations match, or else that both types
9165 -- are anonymous access types whose designated subtypes statically match
9166 -- (as allowed in Ada 2005 by AI-385).
9168 elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
9169 and then
9170 (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
9171 or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
9172 or else Is_Access_Constant (Etype (New_T)) /=
9173 Is_Access_Constant (Etype (Prev))
9174 or else Can_Never_Be_Null (Etype (New_T)) /=
9175 Can_Never_Be_Null (Etype (Prev))
9176 or else Null_Exclusion_Present (Parent (Prev)) /=
9177 Null_Exclusion_Present (Parent (Id))
9178 or else not Subtypes_Statically_Match
9179 (Designated_Type (Etype (Prev)),
9180 Designated_Type (Etype (New_T))))
9181 then
9182 Error_Msg_Sloc := Sloc (Prev);
9183 Error_Msg_N ("type does not match declaration#", N);
9184 Set_Full_View (Prev, Id);
9185 Set_Etype (Id, Any_Type);
9187 elsif
9188 Null_Exclusion_Present (Parent (Prev))
9189 and then not Null_Exclusion_Present (N)
9190 then
9191 Error_Msg_Sloc := Sloc (Prev);
9192 Error_Msg_N ("null-exclusion does not match declaration#", N);
9193 Set_Full_View (Prev, Id);
9194 Set_Etype (Id, Any_Type);
9196 -- If so, process the full constant declaration
9198 else
9199 -- RM 7.4 (6): If the subtype defined by the subtype_indication in
9200 -- the deferred declaration is constrained, then the subtype defined
9201 -- by the subtype_indication in the full declaration shall match it
9202 -- statically.
9204 Check_Possible_Deferred_Completion
9205 (Prev_Id => Prev,
9206 Prev_Obj_Def => Object_Definition (Parent (Prev)),
9207 Curr_Obj_Def => Obj_Def);
9209 Set_Full_View (Prev, Id);
9210 Set_Is_Public (Id, Is_Public (Prev));
9211 Set_Is_Internal (Id);
9212 Append_Entity (Id, Current_Scope);
9214 -- Check ALIASED present if present before (RM 7.4(7))
9216 if Is_Aliased (Prev)
9217 and then not Aliased_Present (N)
9218 then
9219 Error_Msg_Sloc := Sloc (Prev);
9220 Error_Msg_N ("ALIASED required (see declaration#)", N);
9221 end if;
9223 -- Allow incomplete declaration of tags (used to handle forward
9224 -- references to tags). The check on Ada_Tags avoids circularities
9225 -- when rebuilding the compiler.
9227 if RTU_Loaded (Ada_Tags)
9228 and then T = RTE (RE_Tag)
9229 then
9230 null;
9232 -- Check that placement is in private part and that the incomplete
9233 -- declaration appeared in the visible part.
9235 elsif Ekind (Current_Scope) = E_Package
9236 and then not In_Private_Part (Current_Scope)
9237 then
9238 Error_Msg_Sloc := Sloc (Prev);
9239 Error_Msg_N ("full constant for declaration#"
9240 & " must be in private part", N);
9242 elsif Ekind (Current_Scope) = E_Package
9243 and then List_Containing (Parent (Prev))
9244 /= Visible_Declarations
9245 (Specification (Unit_Declaration_Node (Current_Scope)))
9246 then
9247 Error_Msg_N
9248 ("deferred constant must be declared in visible part",
9249 Parent (Prev));
9250 end if;
9252 if Is_Access_Type (T)
9253 and then Nkind (Expression (N)) = N_Allocator
9254 then
9255 Check_Recursive_Declaration (Designated_Type (T));
9256 end if;
9257 end if;
9258 end Constant_Redeclaration;
9260 ----------------------
9261 -- Constrain_Access --
9262 ----------------------
9264 procedure Constrain_Access
9265 (Def_Id : in out Entity_Id;
9266 S : Node_Id;
9267 Related_Nod : Node_Id)
9269 T : constant Entity_Id := Entity (Subtype_Mark (S));
9270 Desig_Type : constant Entity_Id := Designated_Type (T);
9271 Desig_Subtype : Entity_Id := Create_Itype (E_Void, Related_Nod);
9272 Constraint_OK : Boolean := True;
9274 function Has_Defaulted_Discriminants (Typ : Entity_Id) return Boolean;
9275 -- Simple predicate to test for defaulted discriminants
9276 -- Shouldn't this be in sem_util???
9278 ---------------------------------
9279 -- Has_Defaulted_Discriminants --
9280 ---------------------------------
9282 function Has_Defaulted_Discriminants (Typ : Entity_Id) return Boolean is
9283 begin
9284 return Has_Discriminants (Typ)
9285 and then Present (First_Discriminant (Typ))
9286 and then Present
9287 (Discriminant_Default_Value (First_Discriminant (Typ)));
9288 end Has_Defaulted_Discriminants;
9290 -- Start of processing for Constrain_Access
9292 begin
9293 if Is_Array_Type (Desig_Type) then
9294 Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
9296 elsif (Is_Record_Type (Desig_Type)
9297 or else Is_Incomplete_Or_Private_Type (Desig_Type))
9298 and then not Is_Constrained (Desig_Type)
9299 then
9300 -- ??? The following code is a temporary kludge to ignore a
9301 -- discriminant constraint on access type if it is constraining
9302 -- the current record. Avoid creating the implicit subtype of the
9303 -- record we are currently compiling since right now, we cannot
9304 -- handle these. For now, just return the access type itself.
9306 if Desig_Type = Current_Scope
9307 and then No (Def_Id)
9308 then
9309 Set_Ekind (Desig_Subtype, E_Record_Subtype);
9310 Def_Id := Entity (Subtype_Mark (S));
9312 -- This call added to ensure that the constraint is analyzed
9313 -- (needed for a B test). Note that we still return early from
9314 -- this procedure to avoid recursive processing. ???
9316 Constrain_Discriminated_Type
9317 (Desig_Subtype, S, Related_Nod, For_Access => True);
9318 return;
9319 end if;
9321 if (Ekind (T) = E_General_Access_Type
9322 or else Ada_Version >= Ada_05)
9323 and then Has_Private_Declaration (Desig_Type)
9324 and then In_Open_Scopes (Scope (Desig_Type))
9325 and then Has_Discriminants (Desig_Type)
9326 then
9327 -- Enforce rule that the constraint is illegal if there is
9328 -- an unconstrained view of the designated type. This means
9329 -- that the partial view (either a private type declaration or
9330 -- a derivation from a private type) has no discriminants.
9331 -- (Defect Report 8652/0008, Technical Corrigendum 1, checked
9332 -- by ACATS B371001).
9334 -- Rule updated for Ada 2005: the private type is said to have
9335 -- a constrained partial view, given that objects of the type
9336 -- can be declared. Furthermore, the rule applies to all access
9337 -- types, unlike the rule concerning default discriminants.
9339 declare
9340 Pack : constant Node_Id :=
9341 Unit_Declaration_Node (Scope (Desig_Type));
9342 Decls : List_Id;
9343 Decl : Node_Id;
9345 begin
9346 if Nkind (Pack) = N_Package_Declaration then
9347 Decls := Visible_Declarations (Specification (Pack));
9348 Decl := First (Decls);
9349 while Present (Decl) loop
9350 if (Nkind (Decl) = N_Private_Type_Declaration
9351 and then
9352 Chars (Defining_Identifier (Decl)) =
9353 Chars (Desig_Type))
9355 or else
9356 (Nkind (Decl) = N_Full_Type_Declaration
9357 and then
9358 Chars (Defining_Identifier (Decl)) =
9359 Chars (Desig_Type)
9360 and then Is_Derived_Type (Desig_Type)
9361 and then
9362 Has_Private_Declaration (Etype (Desig_Type)))
9363 then
9364 if No (Discriminant_Specifications (Decl)) then
9365 Error_Msg_N
9366 ("cannot constrain general access type if " &
9367 "designated type has constrained partial view",
9369 end if;
9371 exit;
9372 end if;
9374 Next (Decl);
9375 end loop;
9376 end if;
9377 end;
9378 end if;
9380 Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
9381 For_Access => True);
9383 elsif (Is_Task_Type (Desig_Type)
9384 or else Is_Protected_Type (Desig_Type))
9385 and then not Is_Constrained (Desig_Type)
9386 then
9387 Constrain_Concurrent
9388 (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
9390 else
9391 Error_Msg_N ("invalid constraint on access type", S);
9392 Desig_Subtype := Desig_Type; -- Ignore invalid constraint.
9393 Constraint_OK := False;
9394 end if;
9396 if No (Def_Id) then
9397 Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
9398 else
9399 Set_Ekind (Def_Id, E_Access_Subtype);
9400 end if;
9402 if Constraint_OK then
9403 Set_Etype (Def_Id, Base_Type (T));
9405 if Is_Private_Type (Desig_Type) then
9406 Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
9407 end if;
9408 else
9409 Set_Etype (Def_Id, Any_Type);
9410 end if;
9412 Set_Size_Info (Def_Id, T);
9413 Set_Is_Constrained (Def_Id, Constraint_OK);
9414 Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
9415 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
9416 Set_Is_Access_Constant (Def_Id, Is_Access_Constant (T));
9418 Conditional_Delay (Def_Id, T);
9420 -- AI-363 : Subtypes of general access types whose designated types have
9421 -- default discriminants are disallowed. In instances, the rule has to
9422 -- be checked against the actual, of which T is the subtype. In a
9423 -- generic body, the rule is checked assuming that the actual type has
9424 -- defaulted discriminants.
9426 if Ada_Version >= Ada_05 or else Warn_On_Ada_2005_Compatibility then
9427 if Ekind (Base_Type (T)) = E_General_Access_Type
9428 and then Has_Defaulted_Discriminants (Desig_Type)
9429 then
9430 if Ada_Version < Ada_05 then
9431 Error_Msg_N
9432 ("access subtype of general access type would not " &
9433 "be allowed in Ada 2005?", S);
9434 else
9435 Error_Msg_N
9436 ("access subype of general access type not allowed", S);
9437 end if;
9439 Error_Msg_N ("\discriminants have defaults", S);
9441 elsif Is_Access_Type (T)
9442 and then Is_Generic_Type (Desig_Type)
9443 and then Has_Discriminants (Desig_Type)
9444 and then In_Package_Body (Current_Scope)
9445 then
9446 if Ada_Version < Ada_05 then
9447 Error_Msg_N
9448 ("access subtype would not be allowed in generic body " &
9449 "in Ada 2005?", S);
9450 else
9451 Error_Msg_N
9452 ("access subtype not allowed in generic body", S);
9453 end if;
9455 Error_Msg_N
9456 ("\designated type is a discriminated formal", S);
9457 end if;
9458 end if;
9459 end Constrain_Access;
9461 ---------------------
9462 -- Constrain_Array --
9463 ---------------------
9465 procedure Constrain_Array
9466 (Def_Id : in out Entity_Id;
9467 SI : Node_Id;
9468 Related_Nod : Node_Id;
9469 Related_Id : Entity_Id;
9470 Suffix : Character)
9472 C : constant Node_Id := Constraint (SI);
9473 Number_Of_Constraints : Nat := 0;
9474 Index : Node_Id;
9475 S, T : Entity_Id;
9476 Constraint_OK : Boolean := True;
9478 begin
9479 T := Entity (Subtype_Mark (SI));
9481 if Ekind (T) in Access_Kind then
9482 T := Designated_Type (T);
9483 end if;
9485 -- If an index constraint follows a subtype mark in a subtype indication
9486 -- then the type or subtype denoted by the subtype mark must not already
9487 -- impose an index constraint. The subtype mark must denote either an
9488 -- unconstrained array type or an access type whose designated type
9489 -- is such an array type... (RM 3.6.1)
9491 if Is_Constrained (T) then
9492 Error_Msg_N
9493 ("array type is already constrained", Subtype_Mark (SI));
9494 Constraint_OK := False;
9496 else
9497 S := First (Constraints (C));
9498 while Present (S) loop
9499 Number_Of_Constraints := Number_Of_Constraints + 1;
9500 Next (S);
9501 end loop;
9503 -- In either case, the index constraint must provide a discrete
9504 -- range for each index of the array type and the type of each
9505 -- discrete range must be the same as that of the corresponding
9506 -- index. (RM 3.6.1)
9508 if Number_Of_Constraints /= Number_Dimensions (T) then
9509 Error_Msg_NE ("incorrect number of index constraints for }", C, T);
9510 Constraint_OK := False;
9512 else
9513 S := First (Constraints (C));
9514 Index := First_Index (T);
9515 Analyze (Index);
9517 -- Apply constraints to each index type
9519 for J in 1 .. Number_Of_Constraints loop
9520 Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
9521 Next (Index);
9522 Next (S);
9523 end loop;
9525 end if;
9526 end if;
9528 if No (Def_Id) then
9529 Def_Id :=
9530 Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
9531 Set_Parent (Def_Id, Related_Nod);
9533 else
9534 Set_Ekind (Def_Id, E_Array_Subtype);
9535 end if;
9537 Set_Size_Info (Def_Id, (T));
9538 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
9539 Set_Etype (Def_Id, Base_Type (T));
9541 if Constraint_OK then
9542 Set_First_Index (Def_Id, First (Constraints (C)));
9543 else
9544 Set_First_Index (Def_Id, First_Index (T));
9545 end if;
9547 Set_Is_Constrained (Def_Id, True);
9548 Set_Is_Aliased (Def_Id, Is_Aliased (T));
9549 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
9551 Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
9552 Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
9554 -- A subtype does not inherit the packed_array_type of is parent. We
9555 -- need to initialize the attribute because if Def_Id is previously
9556 -- analyzed through a limited_with clause, it will have the attributes
9557 -- of an incomplete type, one of which is an Elist that overlaps the
9558 -- Packed_Array_Type field.
9560 Set_Packed_Array_Type (Def_Id, Empty);
9562 -- Build a freeze node if parent still needs one. Also make sure that
9563 -- the Depends_On_Private status is set because the subtype will need
9564 -- reprocessing at the time the base type does, and also we must set a
9565 -- conditional delay.
9567 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
9568 Conditional_Delay (Def_Id, T);
9569 end Constrain_Array;
9571 ------------------------------
9572 -- Constrain_Component_Type --
9573 ------------------------------
9575 function Constrain_Component_Type
9576 (Comp : Entity_Id;
9577 Constrained_Typ : Entity_Id;
9578 Related_Node : Node_Id;
9579 Typ : Entity_Id;
9580 Constraints : Elist_Id) return Entity_Id
9582 Loc : constant Source_Ptr := Sloc (Constrained_Typ);
9583 Compon_Type : constant Entity_Id := Etype (Comp);
9585 function Build_Constrained_Array_Type
9586 (Old_Type : Entity_Id) return Entity_Id;
9587 -- If Old_Type is an array type, one of whose indices is constrained
9588 -- by a discriminant, build an Itype whose constraint replaces the
9589 -- discriminant with its value in the constraint.
9591 function Build_Constrained_Discriminated_Type
9592 (Old_Type : Entity_Id) return Entity_Id;
9593 -- Ditto for record components
9595 function Build_Constrained_Access_Type
9596 (Old_Type : Entity_Id) return Entity_Id;
9597 -- Ditto for access types. Makes use of previous two functions, to
9598 -- constrain designated type.
9600 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id;
9601 -- T is an array or discriminated type, C is a list of constraints
9602 -- that apply to T. This routine builds the constrained subtype.
9604 function Is_Discriminant (Expr : Node_Id) return Boolean;
9605 -- Returns True if Expr is a discriminant
9607 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id;
9608 -- Find the value of discriminant Discrim in Constraint
9610 -----------------------------------
9611 -- Build_Constrained_Access_Type --
9612 -----------------------------------
9614 function Build_Constrained_Access_Type
9615 (Old_Type : Entity_Id) return Entity_Id
9617 Desig_Type : constant Entity_Id := Designated_Type (Old_Type);
9618 Itype : Entity_Id;
9619 Desig_Subtype : Entity_Id;
9620 Scop : Entity_Id;
9622 begin
9623 -- if the original access type was not embedded in the enclosing
9624 -- type definition, there is no need to produce a new access
9625 -- subtype. In fact every access type with an explicit constraint
9626 -- generates an itype whose scope is the enclosing record.
9628 if not Is_Type (Scope (Old_Type)) then
9629 return Old_Type;
9631 elsif Is_Array_Type (Desig_Type) then
9632 Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
9634 elsif Has_Discriminants (Desig_Type) then
9636 -- This may be an access type to an enclosing record type for
9637 -- which we are constructing the constrained components. Return
9638 -- the enclosing record subtype. This is not always correct,
9639 -- but avoids infinite recursion. ???
9641 Desig_Subtype := Any_Type;
9643 for J in reverse 0 .. Scope_Stack.Last loop
9644 Scop := Scope_Stack.Table (J).Entity;
9646 if Is_Type (Scop)
9647 and then Base_Type (Scop) = Base_Type (Desig_Type)
9648 then
9649 Desig_Subtype := Scop;
9650 end if;
9652 exit when not Is_Type (Scop);
9653 end loop;
9655 if Desig_Subtype = Any_Type then
9656 Desig_Subtype :=
9657 Build_Constrained_Discriminated_Type (Desig_Type);
9658 end if;
9660 else
9661 return Old_Type;
9662 end if;
9664 if Desig_Subtype /= Desig_Type then
9666 -- The Related_Node better be here or else we won't be able
9667 -- to attach new itypes to a node in the tree.
9669 pragma Assert (Present (Related_Node));
9671 Itype := Create_Itype (E_Access_Subtype, Related_Node);
9673 Set_Etype (Itype, Base_Type (Old_Type));
9674 Set_Size_Info (Itype, (Old_Type));
9675 Set_Directly_Designated_Type (Itype, Desig_Subtype);
9676 Set_Depends_On_Private (Itype, Has_Private_Component
9677 (Old_Type));
9678 Set_Is_Access_Constant (Itype, Is_Access_Constant
9679 (Old_Type));
9681 -- The new itype needs freezing when it depends on a not frozen
9682 -- type and the enclosing subtype needs freezing.
9684 if Has_Delayed_Freeze (Constrained_Typ)
9685 and then not Is_Frozen (Constrained_Typ)
9686 then
9687 Conditional_Delay (Itype, Base_Type (Old_Type));
9688 end if;
9690 return Itype;
9692 else
9693 return Old_Type;
9694 end if;
9695 end Build_Constrained_Access_Type;
9697 ----------------------------------
9698 -- Build_Constrained_Array_Type --
9699 ----------------------------------
9701 function Build_Constrained_Array_Type
9702 (Old_Type : Entity_Id) return Entity_Id
9704 Lo_Expr : Node_Id;
9705 Hi_Expr : Node_Id;
9706 Old_Index : Node_Id;
9707 Range_Node : Node_Id;
9708 Constr_List : List_Id;
9710 Need_To_Create_Itype : Boolean := False;
9712 begin
9713 Old_Index := First_Index (Old_Type);
9714 while Present (Old_Index) loop
9715 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
9717 if Is_Discriminant (Lo_Expr)
9718 or else Is_Discriminant (Hi_Expr)
9719 then
9720 Need_To_Create_Itype := True;
9721 end if;
9723 Next_Index (Old_Index);
9724 end loop;
9726 if Need_To_Create_Itype then
9727 Constr_List := New_List;
9729 Old_Index := First_Index (Old_Type);
9730 while Present (Old_Index) loop
9731 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
9733 if Is_Discriminant (Lo_Expr) then
9734 Lo_Expr := Get_Discr_Value (Lo_Expr);
9735 end if;
9737 if Is_Discriminant (Hi_Expr) then
9738 Hi_Expr := Get_Discr_Value (Hi_Expr);
9739 end if;
9741 Range_Node :=
9742 Make_Range
9743 (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
9745 Append (Range_Node, To => Constr_List);
9747 Next_Index (Old_Index);
9748 end loop;
9750 return Build_Subtype (Old_Type, Constr_List);
9752 else
9753 return Old_Type;
9754 end if;
9755 end Build_Constrained_Array_Type;
9757 ------------------------------------------
9758 -- Build_Constrained_Discriminated_Type --
9759 ------------------------------------------
9761 function Build_Constrained_Discriminated_Type
9762 (Old_Type : Entity_Id) return Entity_Id
9764 Expr : Node_Id;
9765 Constr_List : List_Id;
9766 Old_Constraint : Elmt_Id;
9768 Need_To_Create_Itype : Boolean := False;
9770 begin
9771 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
9772 while Present (Old_Constraint) loop
9773 Expr := Node (Old_Constraint);
9775 if Is_Discriminant (Expr) then
9776 Need_To_Create_Itype := True;
9777 end if;
9779 Next_Elmt (Old_Constraint);
9780 end loop;
9782 if Need_To_Create_Itype then
9783 Constr_List := New_List;
9785 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
9786 while Present (Old_Constraint) loop
9787 Expr := Node (Old_Constraint);
9789 if Is_Discriminant (Expr) then
9790 Expr := Get_Discr_Value (Expr);
9791 end if;
9793 Append (New_Copy_Tree (Expr), To => Constr_List);
9795 Next_Elmt (Old_Constraint);
9796 end loop;
9798 return Build_Subtype (Old_Type, Constr_List);
9800 else
9801 return Old_Type;
9802 end if;
9803 end Build_Constrained_Discriminated_Type;
9805 -------------------
9806 -- Build_Subtype --
9807 -------------------
9809 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id is
9810 Indic : Node_Id;
9811 Subtyp_Decl : Node_Id;
9812 Def_Id : Entity_Id;
9813 Btyp : Entity_Id := Base_Type (T);
9815 begin
9816 -- The Related_Node better be here or else we won't be able to
9817 -- attach new itypes to a node in the tree.
9819 pragma Assert (Present (Related_Node));
9821 -- If the view of the component's type is incomplete or private
9822 -- with unknown discriminants, then the constraint must be applied
9823 -- to the full type.
9825 if Has_Unknown_Discriminants (Btyp)
9826 and then Present (Underlying_Type (Btyp))
9827 then
9828 Btyp := Underlying_Type (Btyp);
9829 end if;
9831 Indic :=
9832 Make_Subtype_Indication (Loc,
9833 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
9834 Constraint => Make_Index_Or_Discriminant_Constraint (Loc, C));
9836 Def_Id := Create_Itype (Ekind (T), Related_Node);
9838 Subtyp_Decl :=
9839 Make_Subtype_Declaration (Loc,
9840 Defining_Identifier => Def_Id,
9841 Subtype_Indication => Indic);
9843 Set_Parent (Subtyp_Decl, Parent (Related_Node));
9845 -- Itypes must be analyzed with checks off (see package Itypes)
9847 Analyze (Subtyp_Decl, Suppress => All_Checks);
9849 return Def_Id;
9850 end Build_Subtype;
9852 ---------------------
9853 -- Get_Discr_Value --
9854 ---------------------
9856 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id is
9857 D : Entity_Id;
9858 E : Elmt_Id;
9860 begin
9861 -- The discriminant may be declared for the type, in which case we
9862 -- find it by iterating over the list of discriminants. If the
9863 -- discriminant is inherited from a parent type, it appears as the
9864 -- corresponding discriminant of the current type. This will be the
9865 -- case when constraining an inherited component whose constraint is
9866 -- given by a discriminant of the parent.
9868 D := First_Discriminant (Typ);
9869 E := First_Elmt (Constraints);
9871 while Present (D) loop
9872 if D = Entity (Discrim)
9873 or else D = CR_Discriminant (Entity (Discrim))
9874 or else Corresponding_Discriminant (D) = Entity (Discrim)
9875 then
9876 return Node (E);
9877 end if;
9879 Next_Discriminant (D);
9880 Next_Elmt (E);
9881 end loop;
9883 -- The corresponding_Discriminant mechanism is incomplete, because
9884 -- the correspondence between new and old discriminants is not one
9885 -- to one: one new discriminant can constrain several old ones. In
9886 -- that case, scan sequentially the stored_constraint, the list of
9887 -- discriminants of the parents, and the constraints.
9888 -- Previous code checked for the present of the Stored_Constraint
9889 -- list for the derived type, but did not use it at all. Should it
9890 -- be present when the component is a discriminated task type?
9892 if Is_Derived_Type (Typ)
9893 and then Scope (Entity (Discrim)) = Etype (Typ)
9894 then
9895 D := First_Discriminant (Etype (Typ));
9896 E := First_Elmt (Constraints);
9897 while Present (D) loop
9898 if D = Entity (Discrim) then
9899 return Node (E);
9900 end if;
9902 Next_Discriminant (D);
9903 Next_Elmt (E);
9904 end loop;
9905 end if;
9907 -- Something is wrong if we did not find the value
9909 raise Program_Error;
9910 end Get_Discr_Value;
9912 ---------------------
9913 -- Is_Discriminant --
9914 ---------------------
9916 function Is_Discriminant (Expr : Node_Id) return Boolean is
9917 Discrim_Scope : Entity_Id;
9919 begin
9920 if Denotes_Discriminant (Expr) then
9921 Discrim_Scope := Scope (Entity (Expr));
9923 -- Either we have a reference to one of Typ's discriminants,
9925 pragma Assert (Discrim_Scope = Typ
9927 -- or to the discriminants of the parent type, in the case
9928 -- of a derivation of a tagged type with variants.
9930 or else Discrim_Scope = Etype (Typ)
9931 or else Full_View (Discrim_Scope) = Etype (Typ)
9933 -- or same as above for the case where the discriminants
9934 -- were declared in Typ's private view.
9936 or else (Is_Private_Type (Discrim_Scope)
9937 and then Chars (Discrim_Scope) = Chars (Typ))
9939 -- or else we are deriving from the full view and the
9940 -- discriminant is declared in the private entity.
9942 or else (Is_Private_Type (Typ)
9943 and then Chars (Discrim_Scope) = Chars (Typ))
9945 -- Or we are constrained the corresponding record of a
9946 -- synchronized type that completes a private declaration.
9948 or else (Is_Concurrent_Record_Type (Typ)
9949 and then
9950 Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
9952 -- or we have a class-wide type, in which case make sure the
9953 -- discriminant found belongs to the root type.
9955 or else (Is_Class_Wide_Type (Typ)
9956 and then Etype (Typ) = Discrim_Scope));
9958 return True;
9959 end if;
9961 -- In all other cases we have something wrong
9963 return False;
9964 end Is_Discriminant;
9966 -- Start of processing for Constrain_Component_Type
9968 begin
9969 if Nkind (Parent (Comp)) = N_Component_Declaration
9970 and then Comes_From_Source (Parent (Comp))
9971 and then Comes_From_Source
9972 (Subtype_Indication (Component_Definition (Parent (Comp))))
9973 and then
9974 Is_Entity_Name
9975 (Subtype_Indication (Component_Definition (Parent (Comp))))
9976 then
9977 return Compon_Type;
9979 elsif Is_Array_Type (Compon_Type) then
9980 return Build_Constrained_Array_Type (Compon_Type);
9982 elsif Has_Discriminants (Compon_Type) then
9983 return Build_Constrained_Discriminated_Type (Compon_Type);
9985 elsif Is_Access_Type (Compon_Type) then
9986 return Build_Constrained_Access_Type (Compon_Type);
9988 else
9989 return Compon_Type;
9990 end if;
9991 end Constrain_Component_Type;
9993 --------------------------
9994 -- Constrain_Concurrent --
9995 --------------------------
9997 -- For concurrent types, the associated record value type carries the same
9998 -- discriminants, so when we constrain a concurrent type, we must constrain
9999 -- the corresponding record type as well.
10001 procedure Constrain_Concurrent
10002 (Def_Id : in out Entity_Id;
10003 SI : Node_Id;
10004 Related_Nod : Node_Id;
10005 Related_Id : Entity_Id;
10006 Suffix : Character)
10008 T_Ent : Entity_Id := Entity (Subtype_Mark (SI));
10009 T_Val : Entity_Id;
10011 begin
10012 if Ekind (T_Ent) in Access_Kind then
10013 T_Ent := Designated_Type (T_Ent);
10014 end if;
10016 T_Val := Corresponding_Record_Type (T_Ent);
10018 if Present (T_Val) then
10020 if No (Def_Id) then
10021 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
10022 end if;
10024 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
10026 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
10027 Set_Corresponding_Record_Type (Def_Id,
10028 Constrain_Corresponding_Record
10029 (Def_Id, T_Val, Related_Nod, Related_Id));
10031 else
10032 -- If there is no associated record, expansion is disabled and this
10033 -- is a generic context. Create a subtype in any case, so that
10034 -- semantic analysis can proceed.
10036 if No (Def_Id) then
10037 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
10038 end if;
10040 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
10041 end if;
10042 end Constrain_Concurrent;
10044 ------------------------------------
10045 -- Constrain_Corresponding_Record --
10046 ------------------------------------
10048 function Constrain_Corresponding_Record
10049 (Prot_Subt : Entity_Id;
10050 Corr_Rec : Entity_Id;
10051 Related_Nod : Node_Id;
10052 Related_Id : Entity_Id) return Entity_Id
10054 T_Sub : constant Entity_Id :=
10055 Create_Itype (E_Record_Subtype, Related_Nod, Related_Id, 'V');
10057 begin
10058 Set_Etype (T_Sub, Corr_Rec);
10059 Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
10060 Set_Is_Constrained (T_Sub, True);
10061 Set_First_Entity (T_Sub, First_Entity (Corr_Rec));
10062 Set_Last_Entity (T_Sub, Last_Entity (Corr_Rec));
10064 -- As elsewhere, we do not want to create a freeze node for this itype
10065 -- if it is created for a constrained component of an enclosing record
10066 -- because references to outer discriminants will appear out of scope.
10068 if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
10069 Conditional_Delay (T_Sub, Corr_Rec);
10070 else
10071 Set_Is_Frozen (T_Sub);
10072 end if;
10074 if Has_Discriminants (Prot_Subt) then -- False only if errors.
10075 Set_Discriminant_Constraint
10076 (T_Sub, Discriminant_Constraint (Prot_Subt));
10077 Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
10078 Create_Constrained_Components
10079 (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
10080 end if;
10082 Set_Depends_On_Private (T_Sub, Has_Private_Component (T_Sub));
10084 return T_Sub;
10085 end Constrain_Corresponding_Record;
10087 -----------------------
10088 -- Constrain_Decimal --
10089 -----------------------
10091 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id) is
10092 T : constant Entity_Id := Entity (Subtype_Mark (S));
10093 C : constant Node_Id := Constraint (S);
10094 Loc : constant Source_Ptr := Sloc (C);
10095 Range_Expr : Node_Id;
10096 Digits_Expr : Node_Id;
10097 Digits_Val : Uint;
10098 Bound_Val : Ureal;
10100 begin
10101 Set_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
10103 if Nkind (C) = N_Range_Constraint then
10104 Range_Expr := Range_Expression (C);
10105 Digits_Val := Digits_Value (T);
10107 else
10108 pragma Assert (Nkind (C) = N_Digits_Constraint);
10109 Digits_Expr := Digits_Expression (C);
10110 Analyze_And_Resolve (Digits_Expr, Any_Integer);
10112 Check_Digits_Expression (Digits_Expr);
10113 Digits_Val := Expr_Value (Digits_Expr);
10115 if Digits_Val > Digits_Value (T) then
10116 Error_Msg_N
10117 ("digits expression is incompatible with subtype", C);
10118 Digits_Val := Digits_Value (T);
10119 end if;
10121 if Present (Range_Constraint (C)) then
10122 Range_Expr := Range_Expression (Range_Constraint (C));
10123 else
10124 Range_Expr := Empty;
10125 end if;
10126 end if;
10128 Set_Etype (Def_Id, Base_Type (T));
10129 Set_Size_Info (Def_Id, (T));
10130 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10131 Set_Delta_Value (Def_Id, Delta_Value (T));
10132 Set_Scale_Value (Def_Id, Scale_Value (T));
10133 Set_Small_Value (Def_Id, Small_Value (T));
10134 Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
10135 Set_Digits_Value (Def_Id, Digits_Val);
10137 -- Manufacture range from given digits value if no range present
10139 if No (Range_Expr) then
10140 Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
10141 Range_Expr :=
10142 Make_Range (Loc,
10143 Low_Bound =>
10144 Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
10145 High_Bound =>
10146 Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
10147 end if;
10149 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
10150 Set_Discrete_RM_Size (Def_Id);
10152 -- Unconditionally delay the freeze, since we cannot set size
10153 -- information in all cases correctly until the freeze point.
10155 Set_Has_Delayed_Freeze (Def_Id);
10156 end Constrain_Decimal;
10158 ----------------------------------
10159 -- Constrain_Discriminated_Type --
10160 ----------------------------------
10162 procedure Constrain_Discriminated_Type
10163 (Def_Id : Entity_Id;
10164 S : Node_Id;
10165 Related_Nod : Node_Id;
10166 For_Access : Boolean := False)
10168 E : constant Entity_Id := Entity (Subtype_Mark (S));
10169 T : Entity_Id;
10170 C : Node_Id;
10171 Elist : Elist_Id := New_Elmt_List;
10173 procedure Fixup_Bad_Constraint;
10174 -- This is called after finding a bad constraint, and after having
10175 -- posted an appropriate error message. The mission is to leave the
10176 -- entity T in as reasonable state as possible!
10178 --------------------------
10179 -- Fixup_Bad_Constraint --
10180 --------------------------
10182 procedure Fixup_Bad_Constraint is
10183 begin
10184 -- Set a reasonable Ekind for the entity. For an incomplete type,
10185 -- we can't do much, but for other types, we can set the proper
10186 -- corresponding subtype kind.
10188 if Ekind (T) = E_Incomplete_Type then
10189 Set_Ekind (Def_Id, Ekind (T));
10190 else
10191 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
10192 end if;
10194 -- Set Etype to the known type, to reduce chances of cascaded errors
10196 Set_Etype (Def_Id, E);
10197 Set_Error_Posted (Def_Id);
10198 end Fixup_Bad_Constraint;
10200 -- Start of processing for Constrain_Discriminated_Type
10202 begin
10203 C := Constraint (S);
10205 -- A discriminant constraint is only allowed in a subtype indication,
10206 -- after a subtype mark. This subtype mark must denote either a type
10207 -- with discriminants, or an access type whose designated type is a
10208 -- type with discriminants. A discriminant constraint specifies the
10209 -- values of these discriminants (RM 3.7.2(5)).
10211 T := Base_Type (Entity (Subtype_Mark (S)));
10213 if Ekind (T) in Access_Kind then
10214 T := Designated_Type (T);
10215 end if;
10217 -- Ada 2005 (AI-412): Constrained incomplete subtypes are illegal.
10218 -- Avoid generating an error for access-to-incomplete subtypes.
10220 if Ada_Version >= Ada_05
10221 and then Ekind (T) = E_Incomplete_Type
10222 and then Nkind (Parent (S)) = N_Subtype_Declaration
10223 and then not Is_Itype (Def_Id)
10224 then
10225 -- A little sanity check, emit an error message if the type
10226 -- has discriminants to begin with. Type T may be a regular
10227 -- incomplete type or imported via a limited with clause.
10229 if Has_Discriminants (T)
10230 or else
10231 (From_With_Type (T)
10232 and then Present (Non_Limited_View (T))
10233 and then Nkind (Parent (Non_Limited_View (T))) =
10234 N_Full_Type_Declaration
10235 and then Present (Discriminant_Specifications
10236 (Parent (Non_Limited_View (T)))))
10237 then
10238 Error_Msg_N
10239 ("(Ada 2005) incomplete subtype may not be constrained", C);
10240 else
10241 Error_Msg_N
10242 ("invalid constraint: type has no discriminant", C);
10243 end if;
10245 Fixup_Bad_Constraint;
10246 return;
10248 -- Check that the type has visible discriminants. The type may be
10249 -- a private type with unknown discriminants whose full view has
10250 -- discriminants which are invisible.
10252 elsif not Has_Discriminants (T)
10253 or else
10254 (Has_Unknown_Discriminants (T)
10255 and then Is_Private_Type (T))
10256 then
10257 Error_Msg_N ("invalid constraint: type has no discriminant", C);
10258 Fixup_Bad_Constraint;
10259 return;
10261 elsif Is_Constrained (E)
10262 or else (Ekind (E) = E_Class_Wide_Subtype
10263 and then Present (Discriminant_Constraint (E)))
10264 then
10265 Error_Msg_N ("type is already constrained", Subtype_Mark (S));
10266 Fixup_Bad_Constraint;
10267 return;
10268 end if;
10270 -- T may be an unconstrained subtype (e.g. a generic actual).
10271 -- Constraint applies to the base type.
10273 T := Base_Type (T);
10275 Elist := Build_Discriminant_Constraints (T, S);
10277 -- If the list returned was empty we had an error in building the
10278 -- discriminant constraint. We have also already signalled an error
10279 -- in the incomplete type case
10281 if Is_Empty_Elmt_List (Elist) then
10282 Fixup_Bad_Constraint;
10283 return;
10284 end if;
10286 Build_Discriminated_Subtype (T, Def_Id, Elist, Related_Nod, For_Access);
10287 end Constrain_Discriminated_Type;
10289 ---------------------------
10290 -- Constrain_Enumeration --
10291 ---------------------------
10293 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id) is
10294 T : constant Entity_Id := Entity (Subtype_Mark (S));
10295 C : constant Node_Id := Constraint (S);
10297 begin
10298 Set_Ekind (Def_Id, E_Enumeration_Subtype);
10300 Set_First_Literal (Def_Id, First_Literal (Base_Type (T)));
10302 Set_Etype (Def_Id, Base_Type (T));
10303 Set_Size_Info (Def_Id, (T));
10304 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10305 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
10307 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
10309 Set_Discrete_RM_Size (Def_Id);
10310 end Constrain_Enumeration;
10312 ----------------------
10313 -- Constrain_Float --
10314 ----------------------
10316 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id) is
10317 T : constant Entity_Id := Entity (Subtype_Mark (S));
10318 C : Node_Id;
10319 D : Node_Id;
10320 Rais : Node_Id;
10322 begin
10323 Set_Ekind (Def_Id, E_Floating_Point_Subtype);
10325 Set_Etype (Def_Id, Base_Type (T));
10326 Set_Size_Info (Def_Id, (T));
10327 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10329 -- Process the constraint
10331 C := Constraint (S);
10333 -- Digits constraint present
10335 if Nkind (C) = N_Digits_Constraint then
10336 Check_Restriction (No_Obsolescent_Features, C);
10338 if Warn_On_Obsolescent_Feature then
10339 Error_Msg_N
10340 ("subtype digits constraint is an " &
10341 "obsolescent feature (RM J.3(8))?", C);
10342 end if;
10344 D := Digits_Expression (C);
10345 Analyze_And_Resolve (D, Any_Integer);
10346 Check_Digits_Expression (D);
10347 Set_Digits_Value (Def_Id, Expr_Value (D));
10349 -- Check that digits value is in range. Obviously we can do this
10350 -- at compile time, but it is strictly a runtime check, and of
10351 -- course there is an ACVC test that checks this!
10353 if Digits_Value (Def_Id) > Digits_Value (T) then
10354 Error_Msg_Uint_1 := Digits_Value (T);
10355 Error_Msg_N ("?digits value is too large, maximum is ^", D);
10356 Rais :=
10357 Make_Raise_Constraint_Error (Sloc (D),
10358 Reason => CE_Range_Check_Failed);
10359 Insert_Action (Declaration_Node (Def_Id), Rais);
10360 end if;
10362 C := Range_Constraint (C);
10364 -- No digits constraint present
10366 else
10367 Set_Digits_Value (Def_Id, Digits_Value (T));
10368 end if;
10370 -- Range constraint present
10372 if Nkind (C) = N_Range_Constraint then
10373 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
10375 -- No range constraint present
10377 else
10378 pragma Assert (No (C));
10379 Set_Scalar_Range (Def_Id, Scalar_Range (T));
10380 end if;
10382 Set_Is_Constrained (Def_Id);
10383 end Constrain_Float;
10385 ---------------------
10386 -- Constrain_Index --
10387 ---------------------
10389 procedure Constrain_Index
10390 (Index : Node_Id;
10391 S : Node_Id;
10392 Related_Nod : Node_Id;
10393 Related_Id : Entity_Id;
10394 Suffix : Character;
10395 Suffix_Index : Nat)
10397 Def_Id : Entity_Id;
10398 R : Node_Id := Empty;
10399 T : constant Entity_Id := Etype (Index);
10401 begin
10402 if Nkind (S) = N_Range
10403 or else
10404 (Nkind (S) = N_Attribute_Reference
10405 and then Attribute_Name (S) = Name_Range)
10406 then
10407 -- A Range attribute will transformed into N_Range by Resolve
10409 Analyze (S);
10410 Set_Etype (S, T);
10411 R := S;
10413 Process_Range_Expr_In_Decl (R, T, Empty_List);
10415 if not Error_Posted (S)
10416 and then
10417 (Nkind (S) /= N_Range
10418 or else not Covers (T, (Etype (Low_Bound (S))))
10419 or else not Covers (T, (Etype (High_Bound (S)))))
10420 then
10421 if Base_Type (T) /= Any_Type
10422 and then Etype (Low_Bound (S)) /= Any_Type
10423 and then Etype (High_Bound (S)) /= Any_Type
10424 then
10425 Error_Msg_N ("range expected", S);
10426 end if;
10427 end if;
10429 elsif Nkind (S) = N_Subtype_Indication then
10431 -- The parser has verified that this is a discrete indication
10433 Resolve_Discrete_Subtype_Indication (S, T);
10434 R := Range_Expression (Constraint (S));
10436 elsif Nkind (S) = N_Discriminant_Association then
10438 -- Syntactically valid in subtype indication
10440 Error_Msg_N ("invalid index constraint", S);
10441 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
10442 return;
10444 -- Subtype_Mark case, no anonymous subtypes to construct
10446 else
10447 Analyze (S);
10449 if Is_Entity_Name (S) then
10450 if not Is_Type (Entity (S)) then
10451 Error_Msg_N ("expect subtype mark for index constraint", S);
10453 elsif Base_Type (Entity (S)) /= Base_Type (T) then
10454 Wrong_Type (S, Base_Type (T));
10455 end if;
10457 return;
10459 else
10460 Error_Msg_N ("invalid index constraint", S);
10461 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
10462 return;
10463 end if;
10464 end if;
10466 Def_Id :=
10467 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
10469 Set_Etype (Def_Id, Base_Type (T));
10471 if Is_Modular_Integer_Type (T) then
10472 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
10474 elsif Is_Integer_Type (T) then
10475 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
10477 else
10478 Set_Ekind (Def_Id, E_Enumeration_Subtype);
10479 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
10480 end if;
10482 Set_Size_Info (Def_Id, (T));
10483 Set_RM_Size (Def_Id, RM_Size (T));
10484 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10486 Set_Scalar_Range (Def_Id, R);
10488 Set_Etype (S, Def_Id);
10489 Set_Discrete_RM_Size (Def_Id);
10490 end Constrain_Index;
10492 -----------------------
10493 -- Constrain_Integer --
10494 -----------------------
10496 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id) is
10497 T : constant Entity_Id := Entity (Subtype_Mark (S));
10498 C : constant Node_Id := Constraint (S);
10500 begin
10501 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
10503 if Is_Modular_Integer_Type (T) then
10504 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
10505 else
10506 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
10507 end if;
10509 Set_Etype (Def_Id, Base_Type (T));
10510 Set_Size_Info (Def_Id, (T));
10511 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10512 Set_Discrete_RM_Size (Def_Id);
10513 end Constrain_Integer;
10515 ------------------------------
10516 -- Constrain_Ordinary_Fixed --
10517 ------------------------------
10519 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id) is
10520 T : constant Entity_Id := Entity (Subtype_Mark (S));
10521 C : Node_Id;
10522 D : Node_Id;
10523 Rais : Node_Id;
10525 begin
10526 Set_Ekind (Def_Id, E_Ordinary_Fixed_Point_Subtype);
10527 Set_Etype (Def_Id, Base_Type (T));
10528 Set_Size_Info (Def_Id, (T));
10529 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10530 Set_Small_Value (Def_Id, Small_Value (T));
10532 -- Process the constraint
10534 C := Constraint (S);
10536 -- Delta constraint present
10538 if Nkind (C) = N_Delta_Constraint then
10539 Check_Restriction (No_Obsolescent_Features, C);
10541 if Warn_On_Obsolescent_Feature then
10542 Error_Msg_S
10543 ("subtype delta constraint is an " &
10544 "obsolescent feature (RM J.3(7))?");
10545 end if;
10547 D := Delta_Expression (C);
10548 Analyze_And_Resolve (D, Any_Real);
10549 Check_Delta_Expression (D);
10550 Set_Delta_Value (Def_Id, Expr_Value_R (D));
10552 -- Check that delta value is in range. Obviously we can do this
10553 -- at compile time, but it is strictly a runtime check, and of
10554 -- course there is an ACVC test that checks this!
10556 if Delta_Value (Def_Id) < Delta_Value (T) then
10557 Error_Msg_N ("?delta value is too small", D);
10558 Rais :=
10559 Make_Raise_Constraint_Error (Sloc (D),
10560 Reason => CE_Range_Check_Failed);
10561 Insert_Action (Declaration_Node (Def_Id), Rais);
10562 end if;
10564 C := Range_Constraint (C);
10566 -- No delta constraint present
10568 else
10569 Set_Delta_Value (Def_Id, Delta_Value (T));
10570 end if;
10572 -- Range constraint present
10574 if Nkind (C) = N_Range_Constraint then
10575 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
10577 -- No range constraint present
10579 else
10580 pragma Assert (No (C));
10581 Set_Scalar_Range (Def_Id, Scalar_Range (T));
10583 end if;
10585 Set_Discrete_RM_Size (Def_Id);
10587 -- Unconditionally delay the freeze, since we cannot set size
10588 -- information in all cases correctly until the freeze point.
10590 Set_Has_Delayed_Freeze (Def_Id);
10591 end Constrain_Ordinary_Fixed;
10593 -----------------------
10594 -- Contain_Interface --
10595 -----------------------
10597 function Contain_Interface
10598 (Iface : Entity_Id;
10599 Ifaces : Elist_Id) return Boolean
10601 Iface_Elmt : Elmt_Id;
10603 begin
10604 if Present (Ifaces) then
10605 Iface_Elmt := First_Elmt (Ifaces);
10606 while Present (Iface_Elmt) loop
10607 if Node (Iface_Elmt) = Iface then
10608 return True;
10609 end if;
10611 Next_Elmt (Iface_Elmt);
10612 end loop;
10613 end if;
10615 return False;
10616 end Contain_Interface;
10618 ---------------------------
10619 -- Convert_Scalar_Bounds --
10620 ---------------------------
10622 procedure Convert_Scalar_Bounds
10623 (N : Node_Id;
10624 Parent_Type : Entity_Id;
10625 Derived_Type : Entity_Id;
10626 Loc : Source_Ptr)
10628 Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
10630 Lo : Node_Id;
10631 Hi : Node_Id;
10632 Rng : Node_Id;
10634 begin
10635 Lo := Build_Scalar_Bound
10636 (Type_Low_Bound (Derived_Type),
10637 Parent_Type, Implicit_Base);
10639 Hi := Build_Scalar_Bound
10640 (Type_High_Bound (Derived_Type),
10641 Parent_Type, Implicit_Base);
10643 Rng :=
10644 Make_Range (Loc,
10645 Low_Bound => Lo,
10646 High_Bound => Hi);
10648 Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
10650 Set_Parent (Rng, N);
10651 Set_Scalar_Range (Derived_Type, Rng);
10653 -- Analyze the bounds
10655 Analyze_And_Resolve (Lo, Implicit_Base);
10656 Analyze_And_Resolve (Hi, Implicit_Base);
10658 -- Analyze the range itself, except that we do not analyze it if
10659 -- the bounds are real literals, and we have a fixed-point type.
10660 -- The reason for this is that we delay setting the bounds in this
10661 -- case till we know the final Small and Size values (see circuit
10662 -- in Freeze.Freeze_Fixed_Point_Type for further details).
10664 if Is_Fixed_Point_Type (Parent_Type)
10665 and then Nkind (Lo) = N_Real_Literal
10666 and then Nkind (Hi) = N_Real_Literal
10667 then
10668 return;
10670 -- Here we do the analysis of the range
10672 -- Note: we do this manually, since if we do a normal Analyze and
10673 -- Resolve call, there are problems with the conversions used for
10674 -- the derived type range.
10676 else
10677 Set_Etype (Rng, Implicit_Base);
10678 Set_Analyzed (Rng, True);
10679 end if;
10680 end Convert_Scalar_Bounds;
10682 -------------------
10683 -- Copy_And_Swap --
10684 -------------------
10686 procedure Copy_And_Swap (Priv, Full : Entity_Id) is
10687 begin
10688 -- Initialize new full declaration entity by copying the pertinent
10689 -- fields of the corresponding private declaration entity.
10691 -- We temporarily set Ekind to a value appropriate for a type to
10692 -- avoid assert failures in Einfo from checking for setting type
10693 -- attributes on something that is not a type. Ekind (Priv) is an
10694 -- appropriate choice, since it allowed the attributes to be set
10695 -- in the first place. This Ekind value will be modified later.
10697 Set_Ekind (Full, Ekind (Priv));
10699 -- Also set Etype temporarily to Any_Type, again, in the absence
10700 -- of errors, it will be properly reset, and if there are errors,
10701 -- then we want a value of Any_Type to remain.
10703 Set_Etype (Full, Any_Type);
10705 -- Now start copying attributes
10707 Set_Has_Discriminants (Full, Has_Discriminants (Priv));
10709 if Has_Discriminants (Full) then
10710 Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
10711 Set_Stored_Constraint (Full, Stored_Constraint (Priv));
10712 end if;
10714 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
10715 Set_Homonym (Full, Homonym (Priv));
10716 Set_Is_Immediately_Visible (Full, Is_Immediately_Visible (Priv));
10717 Set_Is_Public (Full, Is_Public (Priv));
10718 Set_Is_Pure (Full, Is_Pure (Priv));
10719 Set_Is_Tagged_Type (Full, Is_Tagged_Type (Priv));
10720 Set_Has_Pragma_Unreferenced (Full, Has_Pragma_Unreferenced (Priv));
10721 Set_Has_Pragma_Unreferenced_Objects
10722 (Full, Has_Pragma_Unreferenced_Objects
10723 (Priv));
10725 Conditional_Delay (Full, Priv);
10727 if Is_Tagged_Type (Full) then
10728 Set_Primitive_Operations (Full, Primitive_Operations (Priv));
10730 if Priv = Base_Type (Priv) then
10731 Set_Class_Wide_Type (Full, Class_Wide_Type (Priv));
10732 end if;
10733 end if;
10735 Set_Is_Volatile (Full, Is_Volatile (Priv));
10736 Set_Treat_As_Volatile (Full, Treat_As_Volatile (Priv));
10737 Set_Scope (Full, Scope (Priv));
10738 Set_Next_Entity (Full, Next_Entity (Priv));
10739 Set_First_Entity (Full, First_Entity (Priv));
10740 Set_Last_Entity (Full, Last_Entity (Priv));
10742 -- If access types have been recorded for later handling, keep them in
10743 -- the full view so that they get handled when the full view freeze
10744 -- node is expanded.
10746 if Present (Freeze_Node (Priv))
10747 and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
10748 then
10749 Ensure_Freeze_Node (Full);
10750 Set_Access_Types_To_Process
10751 (Freeze_Node (Full),
10752 Access_Types_To_Process (Freeze_Node (Priv)));
10753 end if;
10755 -- Swap the two entities. Now Privat is the full type entity and
10756 -- Full is the private one. They will be swapped back at the end
10757 -- of the private part. This swapping ensures that the entity that
10758 -- is visible in the private part is the full declaration.
10760 Exchange_Entities (Priv, Full);
10761 Append_Entity (Full, Scope (Full));
10762 end Copy_And_Swap;
10764 -------------------------------------
10765 -- Copy_Array_Base_Type_Attributes --
10766 -------------------------------------
10768 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
10769 begin
10770 Set_Component_Alignment (T1, Component_Alignment (T2));
10771 Set_Component_Type (T1, Component_Type (T2));
10772 Set_Component_Size (T1, Component_Size (T2));
10773 Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
10774 Set_Finalize_Storage_Only (T1, Finalize_Storage_Only (T2));
10775 Set_Has_Non_Standard_Rep (T1, Has_Non_Standard_Rep (T2));
10776 Set_Has_Task (T1, Has_Task (T2));
10777 Set_Is_Packed (T1, Is_Packed (T2));
10778 Set_Has_Aliased_Components (T1, Has_Aliased_Components (T2));
10779 Set_Has_Atomic_Components (T1, Has_Atomic_Components (T2));
10780 Set_Has_Volatile_Components (T1, Has_Volatile_Components (T2));
10781 end Copy_Array_Base_Type_Attributes;
10783 -----------------------------------
10784 -- Copy_Array_Subtype_Attributes --
10785 -----------------------------------
10787 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
10788 begin
10789 Set_Size_Info (T1, T2);
10791 Set_First_Index (T1, First_Index (T2));
10792 Set_Is_Aliased (T1, Is_Aliased (T2));
10793 Set_Is_Atomic (T1, Is_Atomic (T2));
10794 Set_Is_Volatile (T1, Is_Volatile (T2));
10795 Set_Treat_As_Volatile (T1, Treat_As_Volatile (T2));
10796 Set_Is_Constrained (T1, Is_Constrained (T2));
10797 Set_Depends_On_Private (T1, Has_Private_Component (T2));
10798 Set_First_Rep_Item (T1, First_Rep_Item (T2));
10799 Set_Convention (T1, Convention (T2));
10800 Set_Is_Limited_Composite (T1, Is_Limited_Composite (T2));
10801 Set_Is_Private_Composite (T1, Is_Private_Composite (T2));
10802 end Copy_Array_Subtype_Attributes;
10804 -----------------------------------
10805 -- Create_Constrained_Components --
10806 -----------------------------------
10808 procedure Create_Constrained_Components
10809 (Subt : Entity_Id;
10810 Decl_Node : Node_Id;
10811 Typ : Entity_Id;
10812 Constraints : Elist_Id)
10814 Loc : constant Source_Ptr := Sloc (Subt);
10815 Comp_List : constant Elist_Id := New_Elmt_List;
10816 Parent_Type : constant Entity_Id := Etype (Typ);
10817 Assoc_List : constant List_Id := New_List;
10818 Discr_Val : Elmt_Id;
10819 Errors : Boolean;
10820 New_C : Entity_Id;
10821 Old_C : Entity_Id;
10822 Is_Static : Boolean := True;
10824 procedure Collect_Fixed_Components (Typ : Entity_Id);
10825 -- Collect parent type components that do not appear in a variant part
10827 procedure Create_All_Components;
10828 -- Iterate over Comp_List to create the components of the subtype
10830 function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
10831 -- Creates a new component from Old_Compon, copying all the fields from
10832 -- it, including its Etype, inserts the new component in the Subt entity
10833 -- chain and returns the new component.
10835 function Is_Variant_Record (T : Entity_Id) return Boolean;
10836 -- If true, and discriminants are static, collect only components from
10837 -- variants selected by discriminant values.
10839 ------------------------------
10840 -- Collect_Fixed_Components --
10841 ------------------------------
10843 procedure Collect_Fixed_Components (Typ : Entity_Id) is
10844 begin
10845 -- Build association list for discriminants, and find components of the
10846 -- variant part selected by the values of the discriminants.
10848 Old_C := First_Discriminant (Typ);
10849 Discr_Val := First_Elmt (Constraints);
10850 while Present (Old_C) loop
10851 Append_To (Assoc_List,
10852 Make_Component_Association (Loc,
10853 Choices => New_List (New_Occurrence_Of (Old_C, Loc)),
10854 Expression => New_Copy (Node (Discr_Val))));
10856 Next_Elmt (Discr_Val);
10857 Next_Discriminant (Old_C);
10858 end loop;
10860 -- The tag, and the possible parent and controller components
10861 -- are unconditionally in the subtype.
10863 if Is_Tagged_Type (Typ)
10864 or else Has_Controlled_Component (Typ)
10865 then
10866 Old_C := First_Component (Typ);
10867 while Present (Old_C) loop
10868 if Chars ((Old_C)) = Name_uTag
10869 or else Chars ((Old_C)) = Name_uParent
10870 or else Chars ((Old_C)) = Name_uController
10871 then
10872 Append_Elmt (Old_C, Comp_List);
10873 end if;
10875 Next_Component (Old_C);
10876 end loop;
10877 end if;
10878 end Collect_Fixed_Components;
10880 ---------------------------
10881 -- Create_All_Components --
10882 ---------------------------
10884 procedure Create_All_Components is
10885 Comp : Elmt_Id;
10887 begin
10888 Comp := First_Elmt (Comp_List);
10889 while Present (Comp) loop
10890 Old_C := Node (Comp);
10891 New_C := Create_Component (Old_C);
10893 Set_Etype
10894 (New_C,
10895 Constrain_Component_Type
10896 (Old_C, Subt, Decl_Node, Typ, Constraints));
10897 Set_Is_Public (New_C, Is_Public (Subt));
10899 Next_Elmt (Comp);
10900 end loop;
10901 end Create_All_Components;
10903 ----------------------
10904 -- Create_Component --
10905 ----------------------
10907 function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
10908 New_Compon : constant Entity_Id := New_Copy (Old_Compon);
10910 begin
10911 if Ekind (Old_Compon) = E_Discriminant
10912 and then Is_Completely_Hidden (Old_Compon)
10913 then
10914 -- This is a shadow discriminant created for a discriminant of
10915 -- the parent type that is one of several renamed by the same
10916 -- new discriminant. Give the shadow discriminant an internal
10917 -- name that cannot conflict with that of visible components.
10919 Set_Chars (New_Compon, New_Internal_Name ('C'));
10920 end if;
10922 -- Set the parent so we have a proper link for freezing etc. This is
10923 -- not a real parent pointer, since of course our parent does not own
10924 -- up to us and reference us, we are an illegitimate child of the
10925 -- original parent!
10927 Set_Parent (New_Compon, Parent (Old_Compon));
10929 -- If the old component's Esize was already determined and is a
10930 -- static value, then the new component simply inherits it. Otherwise
10931 -- the old component's size may require run-time determination, but
10932 -- the new component's size still might be statically determinable
10933 -- (if, for example it has a static constraint). In that case we want
10934 -- Layout_Type to recompute the component's size, so we reset its
10935 -- size and positional fields.
10937 if Frontend_Layout_On_Target
10938 and then not Known_Static_Esize (Old_Compon)
10939 then
10940 Set_Esize (New_Compon, Uint_0);
10941 Init_Normalized_First_Bit (New_Compon);
10942 Init_Normalized_Position (New_Compon);
10943 Init_Normalized_Position_Max (New_Compon);
10944 end if;
10946 -- We do not want this node marked as Comes_From_Source, since
10947 -- otherwise it would get first class status and a separate cross-
10948 -- reference line would be generated. Illegitimate children do not
10949 -- rate such recognition.
10951 Set_Comes_From_Source (New_Compon, False);
10953 -- But it is a real entity, and a birth certificate must be properly
10954 -- registered by entering it into the entity list.
10956 Enter_Name (New_Compon);
10958 return New_Compon;
10959 end Create_Component;
10961 -----------------------
10962 -- Is_Variant_Record --
10963 -----------------------
10965 function Is_Variant_Record (T : Entity_Id) return Boolean is
10966 begin
10967 return Nkind (Parent (T)) = N_Full_Type_Declaration
10968 and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
10969 and then Present (Component_List (Type_Definition (Parent (T))))
10970 and then
10971 Present
10972 (Variant_Part (Component_List (Type_Definition (Parent (T)))));
10973 end Is_Variant_Record;
10975 -- Start of processing for Create_Constrained_Components
10977 begin
10978 pragma Assert (Subt /= Base_Type (Subt));
10979 pragma Assert (Typ = Base_Type (Typ));
10981 Set_First_Entity (Subt, Empty);
10982 Set_Last_Entity (Subt, Empty);
10984 -- Check whether constraint is fully static, in which case we can
10985 -- optimize the list of components.
10987 Discr_Val := First_Elmt (Constraints);
10988 while Present (Discr_Val) loop
10989 if not Is_OK_Static_Expression (Node (Discr_Val)) then
10990 Is_Static := False;
10991 exit;
10992 end if;
10994 Next_Elmt (Discr_Val);
10995 end loop;
10997 Set_Has_Static_Discriminants (Subt, Is_Static);
10999 Push_Scope (Subt);
11001 -- Inherit the discriminants of the parent type
11003 Add_Discriminants : declare
11004 Num_Disc : Int;
11005 Num_Gird : Int;
11007 begin
11008 Num_Disc := 0;
11009 Old_C := First_Discriminant (Typ);
11011 while Present (Old_C) loop
11012 Num_Disc := Num_Disc + 1;
11013 New_C := Create_Component (Old_C);
11014 Set_Is_Public (New_C, Is_Public (Subt));
11015 Next_Discriminant (Old_C);
11016 end loop;
11018 -- For an untagged derived subtype, the number of discriminants may
11019 -- be smaller than the number of inherited discriminants, because
11020 -- several of them may be renamed by a single new discriminant.
11021 -- In this case, add the hidden discriminants back into the subtype,
11022 -- because otherwise the size of the subtype is computed incorrectly
11023 -- in GCC 4.1.
11025 Num_Gird := 0;
11027 if Is_Derived_Type (Typ)
11028 and then not Is_Tagged_Type (Typ)
11029 then
11030 Old_C := First_Stored_Discriminant (Typ);
11032 while Present (Old_C) loop
11033 Num_Gird := Num_Gird + 1;
11034 Next_Stored_Discriminant (Old_C);
11035 end loop;
11036 end if;
11038 if Num_Gird > Num_Disc then
11040 -- Find out multiple uses of new discriminants, and add hidden
11041 -- components for the extra renamed discriminants. We recognize
11042 -- multiple uses through the Corresponding_Discriminant of a
11043 -- new discriminant: if it constrains several old discriminants,
11044 -- this field points to the last one in the parent type. The
11045 -- stored discriminants of the derived type have the same name
11046 -- as those of the parent.
11048 declare
11049 Constr : Elmt_Id;
11050 New_Discr : Entity_Id;
11051 Old_Discr : Entity_Id;
11053 begin
11054 Constr := First_Elmt (Stored_Constraint (Typ));
11055 Old_Discr := First_Stored_Discriminant (Typ);
11056 while Present (Constr) loop
11057 if Is_Entity_Name (Node (Constr))
11058 and then Ekind (Entity (Node (Constr))) = E_Discriminant
11059 then
11060 New_Discr := Entity (Node (Constr));
11062 if Chars (Corresponding_Discriminant (New_Discr)) /=
11063 Chars (Old_Discr)
11064 then
11065 -- The new discriminant has been used to rename a
11066 -- subsequent old discriminant. Introduce a shadow
11067 -- component for the current old discriminant.
11069 New_C := Create_Component (Old_Discr);
11070 Set_Original_Record_Component (New_C, Old_Discr);
11071 end if;
11072 end if;
11074 Next_Elmt (Constr);
11075 Next_Stored_Discriminant (Old_Discr);
11076 end loop;
11077 end;
11078 end if;
11079 end Add_Discriminants;
11081 if Is_Static
11082 and then Is_Variant_Record (Typ)
11083 then
11084 Collect_Fixed_Components (Typ);
11086 Gather_Components (
11087 Typ,
11088 Component_List (Type_Definition (Parent (Typ))),
11089 Governed_By => Assoc_List,
11090 Into => Comp_List,
11091 Report_Errors => Errors);
11092 pragma Assert (not Errors);
11094 Create_All_Components;
11096 -- If the subtype declaration is created for a tagged type derivation
11097 -- with constraints, we retrieve the record definition of the parent
11098 -- type to select the components of the proper variant.
11100 elsif Is_Static
11101 and then Is_Tagged_Type (Typ)
11102 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
11103 and then
11104 Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
11105 and then Is_Variant_Record (Parent_Type)
11106 then
11107 Collect_Fixed_Components (Typ);
11109 Gather_Components (
11110 Typ,
11111 Component_List (Type_Definition (Parent (Parent_Type))),
11112 Governed_By => Assoc_List,
11113 Into => Comp_List,
11114 Report_Errors => Errors);
11115 pragma Assert (not Errors);
11117 -- If the tagged derivation has a type extension, collect all the
11118 -- new components therein.
11120 if Present
11121 (Record_Extension_Part (Type_Definition (Parent (Typ))))
11122 then
11123 Old_C := First_Component (Typ);
11124 while Present (Old_C) loop
11125 if Original_Record_Component (Old_C) = Old_C
11126 and then Chars (Old_C) /= Name_uTag
11127 and then Chars (Old_C) /= Name_uParent
11128 and then Chars (Old_C) /= Name_uController
11129 then
11130 Append_Elmt (Old_C, Comp_List);
11131 end if;
11133 Next_Component (Old_C);
11134 end loop;
11135 end if;
11137 Create_All_Components;
11139 else
11140 -- If discriminants are not static, or if this is a multi-level type
11141 -- extension, we have to include all components of the parent type.
11143 Old_C := First_Component (Typ);
11144 while Present (Old_C) loop
11145 New_C := Create_Component (Old_C);
11147 Set_Etype
11148 (New_C,
11149 Constrain_Component_Type
11150 (Old_C, Subt, Decl_Node, Typ, Constraints));
11151 Set_Is_Public (New_C, Is_Public (Subt));
11153 Next_Component (Old_C);
11154 end loop;
11155 end if;
11157 End_Scope;
11158 end Create_Constrained_Components;
11160 ------------------------------------------
11161 -- Decimal_Fixed_Point_Type_Declaration --
11162 ------------------------------------------
11164 procedure Decimal_Fixed_Point_Type_Declaration
11165 (T : Entity_Id;
11166 Def : Node_Id)
11168 Loc : constant Source_Ptr := Sloc (Def);
11169 Digs_Expr : constant Node_Id := Digits_Expression (Def);
11170 Delta_Expr : constant Node_Id := Delta_Expression (Def);
11171 Implicit_Base : Entity_Id;
11172 Digs_Val : Uint;
11173 Delta_Val : Ureal;
11174 Scale_Val : Uint;
11175 Bound_Val : Ureal;
11177 begin
11178 Check_Restriction (No_Fixed_Point, Def);
11180 -- Create implicit base type
11182 Implicit_Base :=
11183 Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
11184 Set_Etype (Implicit_Base, Implicit_Base);
11186 -- Analyze and process delta expression
11188 Analyze_And_Resolve (Delta_Expr, Universal_Real);
11190 Check_Delta_Expression (Delta_Expr);
11191 Delta_Val := Expr_Value_R (Delta_Expr);
11193 -- Check delta is power of 10, and determine scale value from it
11195 declare
11196 Val : Ureal;
11198 begin
11199 Scale_Val := Uint_0;
11200 Val := Delta_Val;
11202 if Val < Ureal_1 then
11203 while Val < Ureal_1 loop
11204 Val := Val * Ureal_10;
11205 Scale_Val := Scale_Val + 1;
11206 end loop;
11208 if Scale_Val > 18 then
11209 Error_Msg_N ("scale exceeds maximum value of 18", Def);
11210 Scale_Val := UI_From_Int (+18);
11211 end if;
11213 else
11214 while Val > Ureal_1 loop
11215 Val := Val / Ureal_10;
11216 Scale_Val := Scale_Val - 1;
11217 end loop;
11219 if Scale_Val < -18 then
11220 Error_Msg_N ("scale is less than minimum value of -18", Def);
11221 Scale_Val := UI_From_Int (-18);
11222 end if;
11223 end if;
11225 if Val /= Ureal_1 then
11226 Error_Msg_N ("delta expression must be a power of 10", Def);
11227 Delta_Val := Ureal_10 ** (-Scale_Val);
11228 end if;
11229 end;
11231 -- Set delta, scale and small (small = delta for decimal type)
11233 Set_Delta_Value (Implicit_Base, Delta_Val);
11234 Set_Scale_Value (Implicit_Base, Scale_Val);
11235 Set_Small_Value (Implicit_Base, Delta_Val);
11237 -- Analyze and process digits expression
11239 Analyze_And_Resolve (Digs_Expr, Any_Integer);
11240 Check_Digits_Expression (Digs_Expr);
11241 Digs_Val := Expr_Value (Digs_Expr);
11243 if Digs_Val > 18 then
11244 Digs_Val := UI_From_Int (+18);
11245 Error_Msg_N ("digits value out of range, maximum is 18", Digs_Expr);
11246 end if;
11248 Set_Digits_Value (Implicit_Base, Digs_Val);
11249 Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
11251 -- Set range of base type from digits value for now. This will be
11252 -- expanded to represent the true underlying base range by Freeze.
11254 Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
11256 -- Note: We leave size as zero for now, size will be set at freeze
11257 -- time. We have to do this for ordinary fixed-point, because the size
11258 -- depends on the specified small, and we might as well do the same for
11259 -- decimal fixed-point.
11261 pragma Assert (Esize (Implicit_Base) = Uint_0);
11263 -- If there are bounds given in the declaration use them as the
11264 -- bounds of the first named subtype.
11266 if Present (Real_Range_Specification (Def)) then
11267 declare
11268 RRS : constant Node_Id := Real_Range_Specification (Def);
11269 Low : constant Node_Id := Low_Bound (RRS);
11270 High : constant Node_Id := High_Bound (RRS);
11271 Low_Val : Ureal;
11272 High_Val : Ureal;
11274 begin
11275 Analyze_And_Resolve (Low, Any_Real);
11276 Analyze_And_Resolve (High, Any_Real);
11277 Check_Real_Bound (Low);
11278 Check_Real_Bound (High);
11279 Low_Val := Expr_Value_R (Low);
11280 High_Val := Expr_Value_R (High);
11282 if Low_Val < (-Bound_Val) then
11283 Error_Msg_N
11284 ("range low bound too small for digits value", Low);
11285 Low_Val := -Bound_Val;
11286 end if;
11288 if High_Val > Bound_Val then
11289 Error_Msg_N
11290 ("range high bound too large for digits value", High);
11291 High_Val := Bound_Val;
11292 end if;
11294 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
11295 end;
11297 -- If no explicit range, use range that corresponds to given
11298 -- digits value. This will end up as the final range for the
11299 -- first subtype.
11301 else
11302 Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
11303 end if;
11305 -- Complete entity for first subtype
11307 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
11308 Set_Etype (T, Implicit_Base);
11309 Set_Size_Info (T, Implicit_Base);
11310 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
11311 Set_Digits_Value (T, Digs_Val);
11312 Set_Delta_Value (T, Delta_Val);
11313 Set_Small_Value (T, Delta_Val);
11314 Set_Scale_Value (T, Scale_Val);
11315 Set_Is_Constrained (T);
11316 end Decimal_Fixed_Point_Type_Declaration;
11318 -----------------------------------
11319 -- Derive_Progenitor_Subprograms --
11320 -----------------------------------
11322 procedure Derive_Progenitor_Subprograms
11323 (Parent_Type : Entity_Id;
11324 Tagged_Type : Entity_Id)
11326 E : Entity_Id;
11327 Elmt : Elmt_Id;
11328 Iface : Entity_Id;
11329 Iface_Elmt : Elmt_Id;
11330 Iface_Subp : Entity_Id;
11331 New_Subp : Entity_Id := Empty;
11332 Prim_Elmt : Elmt_Id;
11333 Subp : Entity_Id;
11334 Typ : Entity_Id;
11336 begin
11337 pragma Assert (Ada_Version >= Ada_05
11338 and then Is_Record_Type (Tagged_Type)
11339 and then Is_Tagged_Type (Tagged_Type)
11340 and then Has_Interfaces (Tagged_Type));
11342 -- Step 1: Transfer to the full-view primitives asociated with the
11343 -- partial-view that cover interface primitives. Conceptually this
11344 -- work should be done later by Process_Full_View; done here to
11345 -- simplify its implementation at later stages. It can be safely
11346 -- done here because interfaces must be visible in the partial and
11347 -- private view (RM 7.3(7.3/2)).
11349 -- Small optimization: This work is only required if the parent is
11350 -- abstract. If the tagged type is not abstract, it cannot have
11351 -- abstract primitives (the only entities in the list of primitives of
11352 -- non-abstract tagged types that can reference abstract primitives
11353 -- through its Alias attribute are the internal entities that have
11354 -- attribute Interface_Alias, and these entities are generated later
11355 -- by Freeze_Record_Type).
11357 if In_Private_Part (Current_Scope)
11358 and then Is_Abstract_Type (Parent_Type)
11359 then
11360 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
11361 while Present (Elmt) loop
11362 Subp := Node (Elmt);
11364 -- At this stage it is not possible to have entities in the list
11365 -- of primitives that have attribute Interface_Alias
11367 pragma Assert (No (Interface_Alias (Subp)));
11369 Typ := Find_Dispatching_Type (Ultimate_Alias (Subp));
11371 if Is_Interface (Typ) then
11372 E := Find_Primitive_Covering_Interface
11373 (Tagged_Type => Tagged_Type,
11374 Iface_Prim => Subp);
11376 if Present (E)
11377 and then Find_Dispatching_Type (Ultimate_Alias (E)) /= Typ
11378 then
11379 Replace_Elmt (Elmt, E);
11380 Remove_Homonym (Subp);
11381 end if;
11382 end if;
11384 Next_Elmt (Elmt);
11385 end loop;
11386 end if;
11388 -- Step 2: Add primitives of progenitors that are not implemented by
11389 -- parents of Tagged_Type
11391 if Present (Interfaces (Tagged_Type)) then
11392 Iface_Elmt := First_Elmt (Interfaces (Tagged_Type));
11393 while Present (Iface_Elmt) loop
11394 Iface := Node (Iface_Elmt);
11396 Prim_Elmt := First_Elmt (Primitive_Operations (Iface));
11397 while Present (Prim_Elmt) loop
11398 Iface_Subp := Node (Prim_Elmt);
11400 -- Exclude derivation of predefined primitives except those
11401 -- that come from source. Required to catch declarations of
11402 -- equality operators of interfaces. For example:
11404 -- type Iface is interface;
11405 -- function "=" (Left, Right : Iface) return Boolean;
11407 if not Is_Predefined_Dispatching_Operation (Iface_Subp)
11408 or else Comes_From_Source (Iface_Subp)
11409 then
11410 E := Find_Primitive_Covering_Interface
11411 (Tagged_Type => Tagged_Type,
11412 Iface_Prim => Iface_Subp);
11414 -- If not found we derive a new primitive leaving its alias
11415 -- attribute referencing the interface primitive
11417 if No (E) then
11418 Derive_Subprogram
11419 (New_Subp, Iface_Subp, Tagged_Type, Iface);
11421 -- Propagate to the full view interface entities associated
11422 -- with the partial view
11424 elsif In_Private_Part (Current_Scope)
11425 and then Present (Alias (E))
11426 and then Alias (E) = Iface_Subp
11427 and then
11428 List_Containing (Parent (E)) /=
11429 Private_Declarations
11430 (Specification
11431 (Unit_Declaration_Node (Current_Scope)))
11432 then
11433 Append_Elmt (E, Primitive_Operations (Tagged_Type));
11434 end if;
11435 end if;
11437 Next_Elmt (Prim_Elmt);
11438 end loop;
11440 Next_Elmt (Iface_Elmt);
11441 end loop;
11442 end if;
11443 end Derive_Progenitor_Subprograms;
11445 -----------------------
11446 -- Derive_Subprogram --
11447 -----------------------
11449 procedure Derive_Subprogram
11450 (New_Subp : in out Entity_Id;
11451 Parent_Subp : Entity_Id;
11452 Derived_Type : Entity_Id;
11453 Parent_Type : Entity_Id;
11454 Actual_Subp : Entity_Id := Empty)
11456 Formal : Entity_Id;
11457 -- Formal parameter of parent primitive operation
11459 Formal_Of_Actual : Entity_Id;
11460 -- Formal parameter of actual operation, when the derivation is to
11461 -- create a renaming for a primitive operation of an actual in an
11462 -- instantiation.
11464 New_Formal : Entity_Id;
11465 -- Formal of inherited operation
11467 Visible_Subp : Entity_Id := Parent_Subp;
11469 function Is_Private_Overriding return Boolean;
11470 -- If Subp is a private overriding of a visible operation, the inherited
11471 -- operation derives from the overridden op (even though its body is the
11472 -- overriding one) and the inherited operation is visible now. See
11473 -- sem_disp to see the full details of the handling of the overridden
11474 -- subprogram, which is removed from the list of primitive operations of
11475 -- the type. The overridden subprogram is saved locally in Visible_Subp,
11476 -- and used to diagnose abstract operations that need overriding in the
11477 -- derived type.
11479 procedure Replace_Type (Id, New_Id : Entity_Id);
11480 -- When the type is an anonymous access type, create a new access type
11481 -- designating the derived type.
11483 procedure Set_Derived_Name;
11484 -- This procedure sets the appropriate Chars name for New_Subp. This
11485 -- is normally just a copy of the parent name. An exception arises for
11486 -- type support subprograms, where the name is changed to reflect the
11487 -- name of the derived type, e.g. if type foo is derived from type bar,
11488 -- then a procedure barDA is derived with a name fooDA.
11490 ---------------------------
11491 -- Is_Private_Overriding --
11492 ---------------------------
11494 function Is_Private_Overriding return Boolean is
11495 Prev : Entity_Id;
11497 begin
11498 -- If the parent is not a dispatching operation there is no
11499 -- need to investigate overridings
11501 if not Is_Dispatching_Operation (Parent_Subp) then
11502 return False;
11503 end if;
11505 -- The visible operation that is overridden is a homonym of the
11506 -- parent subprogram. We scan the homonym chain to find the one
11507 -- whose alias is the subprogram we are deriving.
11509 Prev := Current_Entity (Parent_Subp);
11510 while Present (Prev) loop
11511 if Ekind (Prev) = Ekind (Parent_Subp)
11512 and then Alias (Prev) = Parent_Subp
11513 and then Scope (Parent_Subp) = Scope (Prev)
11514 and then not Is_Hidden (Prev)
11515 then
11516 Visible_Subp := Prev;
11517 return True;
11518 end if;
11520 Prev := Homonym (Prev);
11521 end loop;
11523 return False;
11524 end Is_Private_Overriding;
11526 ------------------
11527 -- Replace_Type --
11528 ------------------
11530 procedure Replace_Type (Id, New_Id : Entity_Id) is
11531 Acc_Type : Entity_Id;
11532 Par : constant Node_Id := Parent (Derived_Type);
11534 begin
11535 -- When the type is an anonymous access type, create a new access
11536 -- type designating the derived type. This itype must be elaborated
11537 -- at the point of the derivation, not on subsequent calls that may
11538 -- be out of the proper scope for Gigi, so we insert a reference to
11539 -- it after the derivation.
11541 if Ekind (Etype (Id)) = E_Anonymous_Access_Type then
11542 declare
11543 Desig_Typ : Entity_Id := Designated_Type (Etype (Id));
11545 begin
11546 if Ekind (Desig_Typ) = E_Record_Type_With_Private
11547 and then Present (Full_View (Desig_Typ))
11548 and then not Is_Private_Type (Parent_Type)
11549 then
11550 Desig_Typ := Full_View (Desig_Typ);
11551 end if;
11553 if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
11555 -- Ada 2005 (AI-251): Handle also derivations of abstract
11556 -- interface primitives.
11558 or else (Is_Interface (Desig_Typ)
11559 and then not Is_Class_Wide_Type (Desig_Typ))
11560 then
11561 Acc_Type := New_Copy (Etype (Id));
11562 Set_Etype (Acc_Type, Acc_Type);
11563 Set_Scope (Acc_Type, New_Subp);
11565 -- Compute size of anonymous access type
11567 if Is_Array_Type (Desig_Typ)
11568 and then not Is_Constrained (Desig_Typ)
11569 then
11570 Init_Size (Acc_Type, 2 * System_Address_Size);
11571 else
11572 Init_Size (Acc_Type, System_Address_Size);
11573 end if;
11575 Init_Alignment (Acc_Type);
11576 Set_Directly_Designated_Type (Acc_Type, Derived_Type);
11578 Set_Etype (New_Id, Acc_Type);
11579 Set_Scope (New_Id, New_Subp);
11581 -- Create a reference to it
11582 Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
11584 else
11585 Set_Etype (New_Id, Etype (Id));
11586 end if;
11587 end;
11589 elsif Base_Type (Etype (Id)) = Base_Type (Parent_Type)
11590 or else
11591 (Ekind (Etype (Id)) = E_Record_Type_With_Private
11592 and then Present (Full_View (Etype (Id)))
11593 and then
11594 Base_Type (Full_View (Etype (Id))) = Base_Type (Parent_Type))
11595 then
11596 -- Constraint checks on formals are generated during expansion,
11597 -- based on the signature of the original subprogram. The bounds
11598 -- of the derived type are not relevant, and thus we can use
11599 -- the base type for the formals. However, the return type may be
11600 -- used in a context that requires that the proper static bounds
11601 -- be used (a case statement, for example) and for those cases
11602 -- we must use the derived type (first subtype), not its base.
11604 -- If the derived_type_definition has no constraints, we know that
11605 -- the derived type has the same constraints as the first subtype
11606 -- of the parent, and we can also use it rather than its base,
11607 -- which can lead to more efficient code.
11609 if Etype (Id) = Parent_Type then
11610 if Is_Scalar_Type (Parent_Type)
11611 and then
11612 Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
11613 then
11614 Set_Etype (New_Id, Derived_Type);
11616 elsif Nkind (Par) = N_Full_Type_Declaration
11617 and then
11618 Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
11619 and then
11620 Is_Entity_Name
11621 (Subtype_Indication (Type_Definition (Par)))
11622 then
11623 Set_Etype (New_Id, Derived_Type);
11625 else
11626 Set_Etype (New_Id, Base_Type (Derived_Type));
11627 end if;
11629 else
11630 Set_Etype (New_Id, Base_Type (Derived_Type));
11631 end if;
11633 -- Ada 2005 (AI-251): Handle derivations of abstract interface
11634 -- primitives.
11636 elsif Is_Interface (Etype (Id))
11637 and then not Is_Class_Wide_Type (Etype (Id))
11638 and then Is_Progenitor (Etype (Id), Derived_Type)
11639 then
11640 Set_Etype (New_Id, Derived_Type);
11642 else
11643 Set_Etype (New_Id, Etype (Id));
11644 end if;
11645 end Replace_Type;
11647 ----------------------
11648 -- Set_Derived_Name --
11649 ----------------------
11651 procedure Set_Derived_Name is
11652 Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
11653 begin
11654 if Nm = TSS_Null then
11655 Set_Chars (New_Subp, Chars (Parent_Subp));
11656 else
11657 Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
11658 end if;
11659 end Set_Derived_Name;
11661 -- Local variables
11663 Parent_Overrides_Interface_Primitive : Boolean := False;
11665 -- Start of processing for Derive_Subprogram
11667 begin
11668 New_Subp :=
11669 New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
11670 Set_Ekind (New_Subp, Ekind (Parent_Subp));
11672 -- Check whether the parent overrides an interface primitive
11674 if Is_Overriding_Operation (Parent_Subp) then
11675 declare
11676 E : Entity_Id := Parent_Subp;
11677 begin
11678 while Present (Overridden_Operation (E)) loop
11679 E := Ultimate_Alias (Overridden_Operation (E));
11680 end loop;
11682 Parent_Overrides_Interface_Primitive :=
11683 Is_Dispatching_Operation (E)
11684 and then Present (Find_Dispatching_Type (E))
11685 and then Is_Interface (Find_Dispatching_Type (E));
11686 end;
11687 end if;
11689 -- Check whether the inherited subprogram is a private operation that
11690 -- should be inherited but not yet made visible. Such subprograms can
11691 -- become visible at a later point (e.g., the private part of a public
11692 -- child unit) via Declare_Inherited_Private_Subprograms. If the
11693 -- following predicate is true, then this is not such a private
11694 -- operation and the subprogram simply inherits the name of the parent
11695 -- subprogram. Note the special check for the names of controlled
11696 -- operations, which are currently exempted from being inherited with
11697 -- a hidden name because they must be findable for generation of
11698 -- implicit run-time calls.
11700 if not Is_Hidden (Parent_Subp)
11701 or else Is_Internal (Parent_Subp)
11702 or else Is_Private_Overriding
11703 or else Is_Internal_Name (Chars (Parent_Subp))
11704 or else Chars (Parent_Subp) = Name_Initialize
11705 or else Chars (Parent_Subp) = Name_Adjust
11706 or else Chars (Parent_Subp) = Name_Finalize
11707 then
11708 Set_Derived_Name;
11710 -- If parent is hidden, this can be a regular derivation if the
11711 -- parent is immediately visible in a non-instantiating context,
11712 -- or if we are in the private part of an instance. This test
11713 -- should still be refined ???
11715 -- The test for In_Instance_Not_Visible avoids inheriting the derived
11716 -- operation as a non-visible operation in cases where the parent
11717 -- subprogram might not be visible now, but was visible within the
11718 -- original generic, so it would be wrong to make the inherited
11719 -- subprogram non-visible now. (Not clear if this test is fully
11720 -- correct; are there any cases where we should declare the inherited
11721 -- operation as not visible to avoid it being overridden, e.g., when
11722 -- the parent type is a generic actual with private primitives ???)
11724 -- (they should be treated the same as other private inherited
11725 -- subprograms, but it's not clear how to do this cleanly). ???
11727 elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
11728 and then Is_Immediately_Visible (Parent_Subp)
11729 and then not In_Instance)
11730 or else In_Instance_Not_Visible
11731 then
11732 Set_Derived_Name;
11734 -- Ada 2005 (AI-251): Regular derivation if the parent subprogram
11735 -- overrides an interface primitive because interface primitives
11736 -- must be visible in the partial view of the parent (RM 7.3 (7.3/2))
11738 elsif Parent_Overrides_Interface_Primitive then
11739 Set_Derived_Name;
11741 -- The type is inheriting a private operation, so enter
11742 -- it with a special name so it can't be overridden.
11744 else
11745 Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
11746 end if;
11748 Set_Parent (New_Subp, Parent (Derived_Type));
11750 if Present (Actual_Subp) then
11751 Replace_Type (Actual_Subp, New_Subp);
11752 else
11753 Replace_Type (Parent_Subp, New_Subp);
11754 end if;
11756 Conditional_Delay (New_Subp, Parent_Subp);
11758 -- If we are creating a renaming for a primitive operation of an
11759 -- actual of a generic derived type, we must examine the signature
11760 -- of the actual primitive, not that of the generic formal, which for
11761 -- example may be an interface. However the name and initial value
11762 -- of the inherited operation are those of the formal primitive.
11764 Formal := First_Formal (Parent_Subp);
11766 if Present (Actual_Subp) then
11767 Formal_Of_Actual := First_Formal (Actual_Subp);
11768 else
11769 Formal_Of_Actual := Empty;
11770 end if;
11772 while Present (Formal) loop
11773 New_Formal := New_Copy (Formal);
11775 -- Normally we do not go copying parents, but in the case of
11776 -- formals, we need to link up to the declaration (which is the
11777 -- parameter specification), and it is fine to link up to the
11778 -- original formal's parameter specification in this case.
11780 Set_Parent (New_Formal, Parent (Formal));
11781 Append_Entity (New_Formal, New_Subp);
11783 if Present (Formal_Of_Actual) then
11784 Replace_Type (Formal_Of_Actual, New_Formal);
11785 Next_Formal (Formal_Of_Actual);
11786 else
11787 Replace_Type (Formal, New_Formal);
11788 end if;
11790 Next_Formal (Formal);
11791 end loop;
11793 -- If this derivation corresponds to a tagged generic actual, then
11794 -- primitive operations rename those of the actual. Otherwise the
11795 -- primitive operations rename those of the parent type, If the parent
11796 -- renames an intrinsic operator, so does the new subprogram. We except
11797 -- concatenation, which is always properly typed, and does not get
11798 -- expanded as other intrinsic operations.
11800 if No (Actual_Subp) then
11801 if Is_Intrinsic_Subprogram (Parent_Subp) then
11802 Set_Is_Intrinsic_Subprogram (New_Subp);
11804 if Present (Alias (Parent_Subp))
11805 and then Chars (Parent_Subp) /= Name_Op_Concat
11806 then
11807 Set_Alias (New_Subp, Alias (Parent_Subp));
11808 else
11809 Set_Alias (New_Subp, Parent_Subp);
11810 end if;
11812 else
11813 Set_Alias (New_Subp, Parent_Subp);
11814 end if;
11816 else
11817 Set_Alias (New_Subp, Actual_Subp);
11818 end if;
11820 -- Derived subprograms of a tagged type must inherit the convention
11821 -- of the parent subprogram (a requirement of AI-117). Derived
11822 -- subprograms of untagged types simply get convention Ada by default.
11824 if Is_Tagged_Type (Derived_Type) then
11825 Set_Convention (New_Subp, Convention (Parent_Subp));
11826 end if;
11828 Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
11829 Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
11831 if Ekind (Parent_Subp) = E_Procedure then
11832 Set_Is_Valued_Procedure
11833 (New_Subp, Is_Valued_Procedure (Parent_Subp));
11834 end if;
11836 -- No_Return must be inherited properly. If this is overridden in the
11837 -- case of a dispatching operation, then a check is made in Sem_Disp
11838 -- that the overriding operation is also No_Return (no such check is
11839 -- required for the case of non-dispatching operation.
11841 Set_No_Return (New_Subp, No_Return (Parent_Subp));
11843 -- A derived function with a controlling result is abstract. If the
11844 -- Derived_Type is a nonabstract formal generic derived type, then
11845 -- inherited operations are not abstract: the required check is done at
11846 -- instantiation time. If the derivation is for a generic actual, the
11847 -- function is not abstract unless the actual is.
11849 if Is_Generic_Type (Derived_Type)
11850 and then not Is_Abstract_Type (Derived_Type)
11851 then
11852 null;
11854 -- Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
11855 -- properties of the subprogram, as defined in RM-3.9.3(4/2-6/2).
11857 elsif Ada_Version >= Ada_05
11858 and then (Is_Abstract_Subprogram (Alias (New_Subp))
11859 or else (Is_Tagged_Type (Derived_Type)
11860 and then Etype (New_Subp) = Derived_Type
11861 and then not Is_Null_Extension (Derived_Type))
11862 or else (Is_Tagged_Type (Derived_Type)
11863 and then Ekind (Etype (New_Subp)) =
11864 E_Anonymous_Access_Type
11865 and then Designated_Type (Etype (New_Subp)) =
11866 Derived_Type
11867 and then not Is_Null_Extension (Derived_Type)))
11868 and then No (Actual_Subp)
11869 then
11870 if not Is_Tagged_Type (Derived_Type)
11871 or else Is_Abstract_Type (Derived_Type)
11872 or else Is_Abstract_Subprogram (Alias (New_Subp))
11873 then
11874 Set_Is_Abstract_Subprogram (New_Subp);
11875 else
11876 Set_Requires_Overriding (New_Subp);
11877 end if;
11879 elsif Ada_Version < Ada_05
11880 and then (Is_Abstract_Subprogram (Alias (New_Subp))
11881 or else (Is_Tagged_Type (Derived_Type)
11882 and then Etype (New_Subp) = Derived_Type
11883 and then No (Actual_Subp)))
11884 then
11885 Set_Is_Abstract_Subprogram (New_Subp);
11887 -- Finally, if the parent type is abstract we must verify that all
11888 -- inherited operations are either non-abstract or overridden, or that
11889 -- the derived type itself is abstract (this check is performed at the
11890 -- end of a package declaration, in Check_Abstract_Overriding). A
11891 -- private overriding in the parent type will not be visible in the
11892 -- derivation if we are not in an inner package or in a child unit of
11893 -- the parent type, in which case the abstractness of the inherited
11894 -- operation is carried to the new subprogram.
11896 elsif Is_Abstract_Type (Parent_Type)
11897 and then not In_Open_Scopes (Scope (Parent_Type))
11898 and then Is_Private_Overriding
11899 and then Is_Abstract_Subprogram (Visible_Subp)
11900 then
11901 if No (Actual_Subp) then
11902 Set_Alias (New_Subp, Visible_Subp);
11903 Set_Is_Abstract_Subprogram
11904 (New_Subp, True);
11905 else
11906 -- If this is a derivation for an instance of a formal derived
11907 -- type, abstractness comes from the primitive operation of the
11908 -- actual, not from the operation inherited from the ancestor.
11910 Set_Is_Abstract_Subprogram
11911 (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
11912 end if;
11913 end if;
11915 New_Overloaded_Entity (New_Subp, Derived_Type);
11917 -- Check for case of a derived subprogram for the instantiation of a
11918 -- formal derived tagged type, if so mark the subprogram as dispatching
11919 -- and inherit the dispatching attributes of the parent subprogram. The
11920 -- derived subprogram is effectively renaming of the actual subprogram,
11921 -- so it needs to have the same attributes as the actual.
11923 if Present (Actual_Subp)
11924 and then Is_Dispatching_Operation (Parent_Subp)
11925 then
11926 Set_Is_Dispatching_Operation (New_Subp);
11928 if Present (DTC_Entity (Parent_Subp)) then
11929 Set_DTC_Entity (New_Subp, DTC_Entity (Parent_Subp));
11930 Set_DT_Position (New_Subp, DT_Position (Parent_Subp));
11931 end if;
11932 end if;
11934 -- Indicate that a derived subprogram does not require a body and that
11935 -- it does not require processing of default expressions.
11937 Set_Has_Completion (New_Subp);
11938 Set_Default_Expressions_Processed (New_Subp);
11940 if Ekind (New_Subp) = E_Function then
11941 Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
11942 end if;
11943 end Derive_Subprogram;
11945 ------------------------
11946 -- Derive_Subprograms --
11947 ------------------------
11949 procedure Derive_Subprograms
11950 (Parent_Type : Entity_Id;
11951 Derived_Type : Entity_Id;
11952 Generic_Actual : Entity_Id := Empty)
11954 Op_List : constant Elist_Id :=
11955 Collect_Primitive_Operations (Parent_Type);
11957 function Check_Derived_Type return Boolean;
11958 -- Check that all primitive inherited from Parent_Type are found in
11959 -- the list of primitives of Derived_Type exactly in the same order.
11961 function Check_Derived_Type return Boolean is
11962 E : Entity_Id;
11963 Elmt : Elmt_Id;
11964 List : Elist_Id;
11965 New_Subp : Entity_Id;
11966 Op_Elmt : Elmt_Id;
11967 Subp : Entity_Id;
11969 begin
11970 -- Traverse list of entities in the current scope searching for
11971 -- an incomplete type whose full-view is derived type
11973 E := First_Entity (Scope (Derived_Type));
11974 while Present (E)
11975 and then E /= Derived_Type
11976 loop
11977 if Ekind (E) = E_Incomplete_Type
11978 and then Present (Full_View (E))
11979 and then Full_View (E) = Derived_Type
11980 then
11981 -- Disable this test if Derived_Type completes an incomplete
11982 -- type because in such case more primitives can be added
11983 -- later to the list of primitives of Derived_Type by routine
11984 -- Process_Incomplete_Dependents
11986 return True;
11987 end if;
11989 E := Next_Entity (E);
11990 end loop;
11992 List := Collect_Primitive_Operations (Derived_Type);
11993 Elmt := First_Elmt (List);
11995 Op_Elmt := First_Elmt (Op_List);
11996 while Present (Op_Elmt) loop
11997 Subp := Node (Op_Elmt);
11998 New_Subp := Node (Elmt);
12000 -- At this early stage Derived_Type has no entities with attribute
12001 -- Interface_Alias. In addition, such primitives are always
12002 -- located at the end of the list of primitives of Parent_Type.
12003 -- Therefore, if found we can safely stop processing pending
12004 -- entities.
12006 exit when Present (Interface_Alias (Subp));
12008 -- Handle hidden entities
12010 if not Is_Predefined_Dispatching_Operation (Subp)
12011 and then Is_Hidden (Subp)
12012 then
12013 if Present (New_Subp)
12014 and then Primitive_Names_Match (Subp, New_Subp)
12015 then
12016 Next_Elmt (Elmt);
12017 end if;
12019 else
12020 if not Present (New_Subp)
12021 or else Ekind (Subp) /= Ekind (New_Subp)
12022 or else not Primitive_Names_Match (Subp, New_Subp)
12023 then
12024 return False;
12025 end if;
12027 Next_Elmt (Elmt);
12028 end if;
12030 Next_Elmt (Op_Elmt);
12031 end loop;
12033 return True;
12034 end Check_Derived_Type;
12036 -- Local variables
12038 Alias_Subp : Entity_Id;
12039 Act_List : Elist_Id;
12040 Act_Elmt : Elmt_Id := No_Elmt;
12041 Act_Subp : Entity_Id := Empty;
12042 Elmt : Elmt_Id;
12043 Need_Search : Boolean := False;
12044 New_Subp : Entity_Id := Empty;
12045 Parent_Base : Entity_Id;
12046 Subp : Entity_Id;
12048 -- Start of processing for Derive_Subprograms
12050 begin
12051 if Ekind (Parent_Type) = E_Record_Type_With_Private
12052 and then Has_Discriminants (Parent_Type)
12053 and then Present (Full_View (Parent_Type))
12054 then
12055 Parent_Base := Full_View (Parent_Type);
12056 else
12057 Parent_Base := Parent_Type;
12058 end if;
12060 if Present (Generic_Actual) then
12061 Act_List := Collect_Primitive_Operations (Generic_Actual);
12062 Act_Elmt := First_Elmt (Act_List);
12063 end if;
12065 -- Derive primitives inherited from the parent. Note that if the generic
12066 -- actual is present, this is not really a type derivation, it is a
12067 -- completion within an instance.
12069 -- Case 1: Derived_Type does not implement interfaces
12071 if not Is_Tagged_Type (Derived_Type)
12072 or else (not Has_Interfaces (Derived_Type)
12073 and then not (Present (Generic_Actual)
12074 and then
12075 Has_Interfaces (Generic_Actual)))
12076 then
12077 Elmt := First_Elmt (Op_List);
12078 while Present (Elmt) loop
12079 Subp := Node (Elmt);
12081 -- Literals are derived earlier in the process of building the
12082 -- derived type, and are skipped here.
12084 if Ekind (Subp) = E_Enumeration_Literal then
12085 null;
12087 -- The actual is a direct descendant and the common primitive
12088 -- operations appear in the same order.
12090 -- If the generic parent type is present, the derived type is an
12091 -- instance of a formal derived type, and within the instance its
12092 -- operations are those of the actual. We derive from the formal
12093 -- type but make the inherited operations aliases of the
12094 -- corresponding operations of the actual.
12096 else
12097 Derive_Subprogram
12098 (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
12100 if Present (Act_Elmt) then
12101 Next_Elmt (Act_Elmt);
12102 end if;
12103 end if;
12105 Next_Elmt (Elmt);
12106 end loop;
12108 -- Case 2: Derived_Type implements interfaces
12110 else
12111 -- If the parent type has no predefined primitives we remove
12112 -- predefined primitives from the list of primitives of generic
12113 -- actual to simplify the complexity of this algorithm.
12115 if Present (Generic_Actual) then
12116 declare
12117 Has_Predefined_Primitives : Boolean := False;
12119 begin
12120 -- Check if the parent type has predefined primitives
12122 Elmt := First_Elmt (Op_List);
12123 while Present (Elmt) loop
12124 Subp := Node (Elmt);
12126 if Is_Predefined_Dispatching_Operation (Subp)
12127 and then not Comes_From_Source (Ultimate_Alias (Subp))
12128 then
12129 Has_Predefined_Primitives := True;
12130 exit;
12131 end if;
12133 Next_Elmt (Elmt);
12134 end loop;
12136 -- Remove predefined primitives of Generic_Actual. We must use
12137 -- an auxiliary list because in case of tagged types the value
12138 -- returned by Collect_Primitive_Operations is the value stored
12139 -- in its Primitive_Operations attribute (and we don't want to
12140 -- modify its current contents).
12142 if not Has_Predefined_Primitives then
12143 declare
12144 Aux_List : constant Elist_Id := New_Elmt_List;
12146 begin
12147 Elmt := First_Elmt (Act_List);
12148 while Present (Elmt) loop
12149 Subp := Node (Elmt);
12151 if not Is_Predefined_Dispatching_Operation (Subp)
12152 or else Comes_From_Source (Subp)
12153 then
12154 Append_Elmt (Subp, Aux_List);
12155 end if;
12157 Next_Elmt (Elmt);
12158 end loop;
12160 Act_List := Aux_List;
12161 end;
12162 end if;
12164 Act_Elmt := First_Elmt (Act_List);
12165 Act_Subp := Node (Act_Elmt);
12166 end;
12167 end if;
12169 -- Stage 1: If the generic actual is not present we derive the
12170 -- primitives inherited from the parent type. If the generic parent
12171 -- type is present, the derived type is an instance of a formal
12172 -- derived type, and within the instance its operations are those of
12173 -- the actual. We derive from the formal type but make the inherited
12174 -- operations aliases of the corresponding operations of the actual.
12176 Elmt := First_Elmt (Op_List);
12177 while Present (Elmt) loop
12178 Subp := Node (Elmt);
12179 Alias_Subp := Ultimate_Alias (Subp);
12181 -- At this early stage Derived_Type has no entities with attribute
12182 -- Interface_Alias. In addition, such primitives are always
12183 -- located at the end of the list of primitives of Parent_Type.
12184 -- Therefore, if found we can safely stop processing pending
12185 -- entities.
12187 exit when Present (Interface_Alias (Subp));
12189 -- If the generic actual is present find the corresponding
12190 -- operation in the generic actual. If the parent type is a
12191 -- direct ancestor of the derived type then, even if it is an
12192 -- interface, the operations are inherited from the primary
12193 -- dispatch table and are in the proper order. If we detect here
12194 -- that primitives are not in the same order we traverse the list
12195 -- of primitive operations of the actual to find the one that
12196 -- implements the interface primitive.
12198 if Need_Search
12199 or else
12200 (Present (Generic_Actual)
12201 and then Present (Act_Subp)
12202 and then not Primitive_Names_Match (Subp, Act_Subp))
12203 then
12204 pragma Assert (not Is_Ancestor (Parent_Base, Generic_Actual));
12205 pragma Assert (Is_Interface (Parent_Base));
12207 -- Remember that we need searching for all the pending
12208 -- primitives
12210 Need_Search := True;
12212 -- Handle entities associated with interface primitives
12214 if Present (Alias (Subp))
12215 and then Is_Interface (Find_Dispatching_Type (Alias (Subp)))
12216 and then not Is_Predefined_Dispatching_Operation (Subp)
12217 then
12218 Act_Subp :=
12219 Find_Primitive_Covering_Interface
12220 (Tagged_Type => Generic_Actual,
12221 Iface_Prim => Subp);
12223 -- Handle predefined primitives plus the rest of user-defined
12224 -- primitives
12226 else
12227 Act_Elmt := First_Elmt (Act_List);
12228 while Present (Act_Elmt) loop
12229 Act_Subp := Node (Act_Elmt);
12231 exit when Primitive_Names_Match (Subp, Act_Subp)
12232 and then Type_Conformant (Subp, Act_Subp,
12233 Skip_Controlling_Formals => True)
12234 and then No (Interface_Alias (Act_Subp));
12236 Next_Elmt (Act_Elmt);
12237 end loop;
12238 end if;
12239 end if;
12241 -- Case 1: If the parent is a limited interface then it has the
12242 -- predefined primitives of synchronized interfaces. However, the
12243 -- actual type may be a non-limited type and hence it does not
12244 -- have such primitives.
12246 if Present (Generic_Actual)
12247 and then not Present (Act_Subp)
12248 and then Is_Limited_Interface (Parent_Base)
12249 and then Is_Predefined_Interface_Primitive (Subp)
12250 then
12251 null;
12253 -- Case 2: Inherit entities associated with interfaces that
12254 -- were not covered by the parent type. We exclude here null
12255 -- interface primitives because they do not need special
12256 -- management.
12258 elsif Present (Alias (Subp))
12259 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
12260 and then not
12261 (Nkind (Parent (Alias_Subp)) = N_Procedure_Specification
12262 and then Null_Present (Parent (Alias_Subp)))
12263 then
12264 Derive_Subprogram
12265 (New_Subp => New_Subp,
12266 Parent_Subp => Alias_Subp,
12267 Derived_Type => Derived_Type,
12268 Parent_Type => Find_Dispatching_Type (Alias_Subp),
12269 Actual_Subp => Act_Subp);
12271 if No (Generic_Actual) then
12272 Set_Alias (New_Subp, Subp);
12273 end if;
12275 -- Case 3: Common derivation
12277 else
12278 Derive_Subprogram
12279 (New_Subp => New_Subp,
12280 Parent_Subp => Subp,
12281 Derived_Type => Derived_Type,
12282 Parent_Type => Parent_Base,
12283 Actual_Subp => Act_Subp);
12284 end if;
12286 -- No need to update Act_Elm if we must search for the
12287 -- corresponding operation in the generic actual
12289 if not Need_Search
12290 and then Present (Act_Elmt)
12291 then
12292 Next_Elmt (Act_Elmt);
12293 Act_Subp := Node (Act_Elmt);
12294 end if;
12296 Next_Elmt (Elmt);
12297 end loop;
12299 -- Inherit additional operations from progenitors. If the derived
12300 -- type is a generic actual, there are not new primitive operations
12301 -- for the type because it has those of the actual, and therefore
12302 -- nothing needs to be done. The renamings generated above are not
12303 -- primitive operations, and their purpose is simply to make the
12304 -- proper operations visible within an instantiation.
12306 if No (Generic_Actual) then
12307 Derive_Progenitor_Subprograms (Parent_Base, Derived_Type);
12308 end if;
12309 end if;
12311 -- Final check: Direct descendants must have their primitives in the
12312 -- same order. We exclude from this test non-tagged types and instances
12313 -- of formal derived types. We skip this test if we have already
12314 -- reported serious errors in the sources.
12316 pragma Assert (not Is_Tagged_Type (Derived_Type)
12317 or else Present (Generic_Actual)
12318 or else Serious_Errors_Detected > 0
12319 or else Check_Derived_Type);
12320 end Derive_Subprograms;
12322 --------------------------------
12323 -- Derived_Standard_Character --
12324 --------------------------------
12326 procedure Derived_Standard_Character
12327 (N : Node_Id;
12328 Parent_Type : Entity_Id;
12329 Derived_Type : Entity_Id)
12331 Loc : constant Source_Ptr := Sloc (N);
12332 Def : constant Node_Id := Type_Definition (N);
12333 Indic : constant Node_Id := Subtype_Indication (Def);
12334 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
12335 Implicit_Base : constant Entity_Id :=
12336 Create_Itype
12337 (E_Enumeration_Type, N, Derived_Type, 'B');
12339 Lo : Node_Id;
12340 Hi : Node_Id;
12342 begin
12343 Discard_Node (Process_Subtype (Indic, N));
12345 Set_Etype (Implicit_Base, Parent_Base);
12346 Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
12347 Set_RM_Size (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
12349 Set_Is_Character_Type (Implicit_Base, True);
12350 Set_Has_Delayed_Freeze (Implicit_Base);
12352 -- The bounds of the implicit base are the bounds of the parent base.
12353 -- Note that their type is the parent base.
12355 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
12356 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
12358 Set_Scalar_Range (Implicit_Base,
12359 Make_Range (Loc,
12360 Low_Bound => Lo,
12361 High_Bound => Hi));
12363 Conditional_Delay (Derived_Type, Parent_Type);
12365 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
12366 Set_Etype (Derived_Type, Implicit_Base);
12367 Set_Size_Info (Derived_Type, Parent_Type);
12369 if Unknown_RM_Size (Derived_Type) then
12370 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
12371 end if;
12373 Set_Is_Character_Type (Derived_Type, True);
12375 if Nkind (Indic) /= N_Subtype_Indication then
12377 -- If no explicit constraint, the bounds are those
12378 -- of the parent type.
12380 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Type));
12381 Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
12382 Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
12383 end if;
12385 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
12387 -- Because the implicit base is used in the conversion of the bounds, we
12388 -- have to freeze it now. This is similar to what is done for numeric
12389 -- types, and it equally suspicious, but otherwise a non-static bound
12390 -- will have a reference to an unfrozen type, which is rejected by Gigi
12391 -- (???). This requires specific care for definition of stream
12392 -- attributes. For details, see comments at the end of
12393 -- Build_Derived_Numeric_Type.
12395 Freeze_Before (N, Implicit_Base);
12396 end Derived_Standard_Character;
12398 ------------------------------
12399 -- Derived_Type_Declaration --
12400 ------------------------------
12402 procedure Derived_Type_Declaration
12403 (T : Entity_Id;
12404 N : Node_Id;
12405 Is_Completion : Boolean)
12407 Parent_Type : Entity_Id;
12409 function Comes_From_Generic (Typ : Entity_Id) return Boolean;
12410 -- Check whether the parent type is a generic formal, or derives
12411 -- directly or indirectly from one.
12413 ------------------------
12414 -- Comes_From_Generic --
12415 ------------------------
12417 function Comes_From_Generic (Typ : Entity_Id) return Boolean is
12418 begin
12419 if Is_Generic_Type (Typ) then
12420 return True;
12422 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
12423 return True;
12425 elsif Is_Private_Type (Typ)
12426 and then Present (Full_View (Typ))
12427 and then Is_Generic_Type (Root_Type (Full_View (Typ)))
12428 then
12429 return True;
12431 elsif Is_Generic_Actual_Type (Typ) then
12432 return True;
12434 else
12435 return False;
12436 end if;
12437 end Comes_From_Generic;
12439 -- Local variables
12441 Def : constant Node_Id := Type_Definition (N);
12442 Iface_Def : Node_Id;
12443 Indic : constant Node_Id := Subtype_Indication (Def);
12444 Extension : constant Node_Id := Record_Extension_Part (Def);
12445 Parent_Node : Node_Id;
12446 Parent_Scope : Entity_Id;
12447 Taggd : Boolean;
12449 -- Start of processing for Derived_Type_Declaration
12451 begin
12452 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
12454 -- Ada 2005 (AI-251): In case of interface derivation check that the
12455 -- parent is also an interface.
12457 if Interface_Present (Def) then
12458 if not Is_Interface (Parent_Type) then
12459 Diagnose_Interface (Indic, Parent_Type);
12461 else
12462 Parent_Node := Parent (Base_Type (Parent_Type));
12463 Iface_Def := Type_Definition (Parent_Node);
12465 -- Ada 2005 (AI-251): Limited interfaces can only inherit from
12466 -- other limited interfaces.
12468 if Limited_Present (Def) then
12469 if Limited_Present (Iface_Def) then
12470 null;
12472 elsif Protected_Present (Iface_Def) then
12473 Error_Msg_N
12474 ("(Ada 2005) limited interface cannot "
12475 & "inherit from protected interface", Indic);
12477 elsif Synchronized_Present (Iface_Def) then
12478 Error_Msg_N
12479 ("(Ada 2005) limited interface cannot "
12480 & "inherit from synchronized interface", Indic);
12482 elsif Task_Present (Iface_Def) then
12483 Error_Msg_N
12484 ("(Ada 2005) limited interface cannot "
12485 & "inherit from task interface", Indic);
12487 else
12488 Error_Msg_N
12489 ("(Ada 2005) limited interface cannot "
12490 & "inherit from non-limited interface", Indic);
12491 end if;
12493 -- Ada 2005 (AI-345): Non-limited interfaces can only inherit
12494 -- from non-limited or limited interfaces.
12496 elsif not Protected_Present (Def)
12497 and then not Synchronized_Present (Def)
12498 and then not Task_Present (Def)
12499 then
12500 if Limited_Present (Iface_Def) then
12501 null;
12503 elsif Protected_Present (Iface_Def) then
12504 Error_Msg_N
12505 ("(Ada 2005) non-limited interface cannot "
12506 & "inherit from protected interface", Indic);
12508 elsif Synchronized_Present (Iface_Def) then
12509 Error_Msg_N
12510 ("(Ada 2005) non-limited interface cannot "
12511 & "inherit from synchronized interface", Indic);
12513 elsif Task_Present (Iface_Def) then
12514 Error_Msg_N
12515 ("(Ada 2005) non-limited interface cannot "
12516 & "inherit from task interface", Indic);
12518 else
12519 null;
12520 end if;
12521 end if;
12522 end if;
12523 end if;
12525 if Is_Tagged_Type (Parent_Type)
12526 and then Is_Concurrent_Type (Parent_Type)
12527 and then not Is_Interface (Parent_Type)
12528 then
12529 Error_Msg_N
12530 ("parent type of a record extension cannot be "
12531 & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
12532 Set_Etype (T, Any_Type);
12533 return;
12534 end if;
12536 -- Ada 2005 (AI-251): Decorate all the names in the list of ancestor
12537 -- interfaces
12539 if Is_Tagged_Type (Parent_Type)
12540 and then Is_Non_Empty_List (Interface_List (Def))
12541 then
12542 declare
12543 Intf : Node_Id;
12544 T : Entity_Id;
12546 begin
12547 Intf := First (Interface_List (Def));
12548 while Present (Intf) loop
12549 T := Find_Type_Of_Subtype_Indic (Intf);
12551 if not Is_Interface (T) then
12552 Diagnose_Interface (Intf, T);
12554 -- Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
12555 -- a limited type from having a nonlimited progenitor.
12557 elsif (Limited_Present (Def)
12558 or else (not Is_Interface (Parent_Type)
12559 and then Is_Limited_Type (Parent_Type)))
12560 and then not Is_Limited_Interface (T)
12561 then
12562 Error_Msg_NE
12563 ("progenitor interface& of limited type must be limited",
12564 N, T);
12565 end if;
12567 Next (Intf);
12568 end loop;
12569 end;
12570 end if;
12572 if Parent_Type = Any_Type
12573 or else Etype (Parent_Type) = Any_Type
12574 or else (Is_Class_Wide_Type (Parent_Type)
12575 and then Etype (Parent_Type) = T)
12576 then
12577 -- If Parent_Type is undefined or illegal, make new type into a
12578 -- subtype of Any_Type, and set a few attributes to prevent cascaded
12579 -- errors. If this is a self-definition, emit error now.
12581 if T = Parent_Type
12582 or else T = Etype (Parent_Type)
12583 then
12584 Error_Msg_N ("type cannot be used in its own definition", Indic);
12585 end if;
12587 Set_Ekind (T, Ekind (Parent_Type));
12588 Set_Etype (T, Any_Type);
12589 Set_Scalar_Range (T, Scalar_Range (Any_Type));
12591 if Is_Tagged_Type (T) then
12592 Set_Primitive_Operations (T, New_Elmt_List);
12593 end if;
12595 return;
12596 end if;
12598 -- Ada 2005 (AI-251): The case in which the parent of the full-view is
12599 -- an interface is special because the list of interfaces in the full
12600 -- view can be given in any order. For example:
12602 -- type A is interface;
12603 -- type B is interface and A;
12604 -- type D is new B with private;
12605 -- private
12606 -- type D is new A and B with null record; -- 1 --
12608 -- In this case we perform the following transformation of -1-:
12610 -- type D is new B and A with null record;
12612 -- If the parent of the full-view covers the parent of the partial-view
12613 -- we have two possible cases:
12615 -- 1) They have the same parent
12616 -- 2) The parent of the full-view implements some further interfaces
12618 -- In both cases we do not need to perform the transformation. In the
12619 -- first case the source program is correct and the transformation is
12620 -- not needed; in the second case the source program does not fulfill
12621 -- the no-hidden interfaces rule (AI-396) and the error will be reported
12622 -- later.
12624 -- This transformation not only simplifies the rest of the analysis of
12625 -- this type declaration but also simplifies the correct generation of
12626 -- the object layout to the expander.
12628 if In_Private_Part (Current_Scope)
12629 and then Is_Interface (Parent_Type)
12630 then
12631 declare
12632 Iface : Node_Id;
12633 Partial_View : Entity_Id;
12634 Partial_View_Parent : Entity_Id;
12635 New_Iface : Node_Id;
12637 begin
12638 -- Look for the associated private type declaration
12640 Partial_View := First_Entity (Current_Scope);
12641 loop
12642 exit when No (Partial_View)
12643 or else (Has_Private_Declaration (Partial_View)
12644 and then Full_View (Partial_View) = T);
12646 Next_Entity (Partial_View);
12647 end loop;
12649 -- If the partial view was not found then the source code has
12650 -- errors and the transformation is not needed.
12652 if Present (Partial_View) then
12653 Partial_View_Parent := Etype (Partial_View);
12655 -- If the parent of the full-view covers the parent of the
12656 -- partial-view we have nothing else to do.
12658 if Interface_Present_In_Ancestor
12659 (Parent_Type, Partial_View_Parent)
12660 then
12661 null;
12663 -- Traverse the list of interfaces of the full-view to look
12664 -- for the parent of the partial-view and perform the tree
12665 -- transformation.
12667 else
12668 Iface := First (Interface_List (Def));
12669 while Present (Iface) loop
12670 if Etype (Iface) = Etype (Partial_View) then
12671 Rewrite (Subtype_Indication (Def),
12672 New_Copy (Subtype_Indication
12673 (Parent (Partial_View))));
12675 New_Iface := Make_Identifier (Sloc (N),
12676 Chars (Parent_Type));
12677 Append (New_Iface, Interface_List (Def));
12679 -- Analyze the transformed code
12681 Derived_Type_Declaration (T, N, Is_Completion);
12682 return;
12683 end if;
12685 Next (Iface);
12686 end loop;
12687 end if;
12688 end if;
12689 end;
12690 end if;
12692 -- Only composite types other than array types are allowed to have
12693 -- discriminants.
12695 if Present (Discriminant_Specifications (N))
12696 and then (Is_Elementary_Type (Parent_Type)
12697 or else Is_Array_Type (Parent_Type))
12698 and then not Error_Posted (N)
12699 then
12700 Error_Msg_N
12701 ("elementary or array type cannot have discriminants",
12702 Defining_Identifier (First (Discriminant_Specifications (N))));
12703 Set_Has_Discriminants (T, False);
12704 end if;
12706 -- In Ada 83, a derived type defined in a package specification cannot
12707 -- be used for further derivation until the end of its visible part.
12708 -- Note that derivation in the private part of the package is allowed.
12710 if Ada_Version = Ada_83
12711 and then Is_Derived_Type (Parent_Type)
12712 and then In_Visible_Part (Scope (Parent_Type))
12713 then
12714 if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
12715 Error_Msg_N
12716 ("(Ada 83): premature use of type for derivation", Indic);
12717 end if;
12718 end if;
12720 -- Check for early use of incomplete or private type
12722 if Ekind (Parent_Type) = E_Void
12723 or else Ekind (Parent_Type) = E_Incomplete_Type
12724 then
12725 Error_Msg_N ("premature derivation of incomplete type", Indic);
12726 return;
12728 elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
12729 and then not Comes_From_Generic (Parent_Type))
12730 or else Has_Private_Component (Parent_Type)
12731 then
12732 -- The ancestor type of a formal type can be incomplete, in which
12733 -- case only the operations of the partial view are available in
12734 -- the generic. Subsequent checks may be required when the full
12735 -- view is analyzed, to verify that derivation from a tagged type
12736 -- has an extension.
12738 if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
12739 null;
12741 elsif No (Underlying_Type (Parent_Type))
12742 or else Has_Private_Component (Parent_Type)
12743 then
12744 Error_Msg_N
12745 ("premature derivation of derived or private type", Indic);
12747 -- Flag the type itself as being in error, this prevents some
12748 -- nasty problems with subsequent uses of the malformed type.
12750 Set_Error_Posted (T);
12752 -- Check that within the immediate scope of an untagged partial
12753 -- view it's illegal to derive from the partial view if the
12754 -- full view is tagged. (7.3(7))
12756 -- We verify that the Parent_Type is a partial view by checking
12757 -- that it is not a Full_Type_Declaration (i.e. a private type or
12758 -- private extension declaration), to distinguish a partial view
12759 -- from a derivation from a private type which also appears as
12760 -- E_Private_Type.
12762 elsif Present (Full_View (Parent_Type))
12763 and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
12764 and then not Is_Tagged_Type (Parent_Type)
12765 and then Is_Tagged_Type (Full_View (Parent_Type))
12766 then
12767 Parent_Scope := Scope (T);
12768 while Present (Parent_Scope)
12769 and then Parent_Scope /= Standard_Standard
12770 loop
12771 if Parent_Scope = Scope (Parent_Type) then
12772 Error_Msg_N
12773 ("premature derivation from type with tagged full view",
12774 Indic);
12775 end if;
12777 Parent_Scope := Scope (Parent_Scope);
12778 end loop;
12779 end if;
12780 end if;
12782 -- Check that form of derivation is appropriate
12784 Taggd := Is_Tagged_Type (Parent_Type);
12786 -- Perhaps the parent type should be changed to the class-wide type's
12787 -- specific type in this case to prevent cascading errors ???
12789 if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
12790 Error_Msg_N ("parent type must not be a class-wide type", Indic);
12791 return;
12792 end if;
12794 if Present (Extension) and then not Taggd then
12795 Error_Msg_N
12796 ("type derived from untagged type cannot have extension", Indic);
12798 elsif No (Extension) and then Taggd then
12800 -- If this declaration is within a private part (or body) of a
12801 -- generic instantiation then the derivation is allowed (the parent
12802 -- type can only appear tagged in this case if it's a generic actual
12803 -- type, since it would otherwise have been rejected in the analysis
12804 -- of the generic template).
12806 if not Is_Generic_Actual_Type (Parent_Type)
12807 or else In_Visible_Part (Scope (Parent_Type))
12808 then
12809 Error_Msg_N
12810 ("type derived from tagged type must have extension", Indic);
12811 end if;
12812 end if;
12814 -- AI-443: Synchronized formal derived types require a private
12815 -- extension. There is no point in checking the ancestor type or
12816 -- the progenitors since the construct is wrong to begin with.
12818 if Ada_Version >= Ada_05
12819 and then Is_Generic_Type (T)
12820 and then Present (Original_Node (N))
12821 then
12822 declare
12823 Decl : constant Node_Id := Original_Node (N);
12825 begin
12826 if Nkind (Decl) = N_Formal_Type_Declaration
12827 and then Nkind (Formal_Type_Definition (Decl)) =
12828 N_Formal_Derived_Type_Definition
12829 and then Synchronized_Present (Formal_Type_Definition (Decl))
12830 and then No (Extension)
12832 -- Avoid emitting a duplicate error message
12834 and then not Error_Posted (Indic)
12835 then
12836 Error_Msg_N
12837 ("synchronized derived type must have extension", N);
12838 end if;
12839 end;
12840 end if;
12842 Build_Derived_Type (N, Parent_Type, T, Is_Completion);
12844 -- AI-419: The parent type of an explicitly limited derived type must
12845 -- be a limited type or a limited interface.
12847 if Limited_Present (Def) then
12848 Set_Is_Limited_Record (T);
12850 if Is_Interface (T) then
12851 Set_Is_Limited_Interface (T);
12852 end if;
12854 if not Is_Limited_Type (Parent_Type)
12855 and then
12856 (not Is_Interface (Parent_Type)
12857 or else not Is_Limited_Interface (Parent_Type))
12858 then
12859 Error_Msg_NE ("parent type& of limited type must be limited",
12860 N, Parent_Type);
12861 end if;
12862 end if;
12863 end Derived_Type_Declaration;
12865 ------------------------
12866 -- Diagnose_Interface --
12867 ------------------------
12869 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id) is
12870 begin
12871 if not Is_Interface (E)
12872 and then E /= Any_Type
12873 then
12874 Error_Msg_NE ("(Ada 2005) & must be an interface", N, E);
12875 end if;
12876 end Diagnose_Interface;
12878 ----------------------------------
12879 -- Enumeration_Type_Declaration --
12880 ----------------------------------
12882 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
12883 Ev : Uint;
12884 L : Node_Id;
12885 R_Node : Node_Id;
12886 B_Node : Node_Id;
12888 begin
12889 -- Create identifier node representing lower bound
12891 B_Node := New_Node (N_Identifier, Sloc (Def));
12892 L := First (Literals (Def));
12893 Set_Chars (B_Node, Chars (L));
12894 Set_Entity (B_Node, L);
12895 Set_Etype (B_Node, T);
12896 Set_Is_Static_Expression (B_Node, True);
12898 R_Node := New_Node (N_Range, Sloc (Def));
12899 Set_Low_Bound (R_Node, B_Node);
12901 Set_Ekind (T, E_Enumeration_Type);
12902 Set_First_Literal (T, L);
12903 Set_Etype (T, T);
12904 Set_Is_Constrained (T);
12906 Ev := Uint_0;
12908 -- Loop through literals of enumeration type setting pos and rep values
12909 -- except that if the Ekind is already set, then it means that the
12910 -- literal was already constructed (case of a derived type declaration
12911 -- and we should not disturb the Pos and Rep values.
12913 while Present (L) loop
12914 if Ekind (L) /= E_Enumeration_Literal then
12915 Set_Ekind (L, E_Enumeration_Literal);
12916 Set_Enumeration_Pos (L, Ev);
12917 Set_Enumeration_Rep (L, Ev);
12918 Set_Is_Known_Valid (L, True);
12919 end if;
12921 Set_Etype (L, T);
12922 New_Overloaded_Entity (L);
12923 Generate_Definition (L);
12924 Set_Convention (L, Convention_Intrinsic);
12926 if Nkind (L) = N_Defining_Character_Literal then
12927 Set_Is_Character_Type (T, True);
12928 end if;
12930 Ev := Ev + 1;
12931 Next (L);
12932 end loop;
12934 -- Now create a node representing upper bound
12936 B_Node := New_Node (N_Identifier, Sloc (Def));
12937 Set_Chars (B_Node, Chars (Last (Literals (Def))));
12938 Set_Entity (B_Node, Last (Literals (Def)));
12939 Set_Etype (B_Node, T);
12940 Set_Is_Static_Expression (B_Node, True);
12942 Set_High_Bound (R_Node, B_Node);
12944 -- Initialize various fields of the type. Some of this information
12945 -- may be overwritten later through rep.clauses.
12947 Set_Scalar_Range (T, R_Node);
12948 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
12949 Set_Enum_Esize (T);
12950 Set_Enum_Pos_To_Rep (T, Empty);
12952 -- Set Discard_Names if configuration pragma set, or if there is
12953 -- a parameterless pragma in the current declarative region
12955 if Global_Discard_Names
12956 or else Discard_Names (Scope (T))
12957 then
12958 Set_Discard_Names (T);
12959 end if;
12961 -- Process end label if there is one
12963 if Present (Def) then
12964 Process_End_Label (Def, 'e', T);
12965 end if;
12966 end Enumeration_Type_Declaration;
12968 ---------------------------------
12969 -- Expand_To_Stored_Constraint --
12970 ---------------------------------
12972 function Expand_To_Stored_Constraint
12973 (Typ : Entity_Id;
12974 Constraint : Elist_Id) return Elist_Id
12976 Explicitly_Discriminated_Type : Entity_Id;
12977 Expansion : Elist_Id;
12978 Discriminant : Entity_Id;
12980 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
12981 -- Find the nearest type that actually specifies discriminants
12983 ---------------------------------
12984 -- Type_With_Explicit_Discrims --
12985 ---------------------------------
12987 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
12988 Typ : constant E := Base_Type (Id);
12990 begin
12991 if Ekind (Typ) in Incomplete_Or_Private_Kind then
12992 if Present (Full_View (Typ)) then
12993 return Type_With_Explicit_Discrims (Full_View (Typ));
12994 end if;
12996 else
12997 if Has_Discriminants (Typ) then
12998 return Typ;
12999 end if;
13000 end if;
13002 if Etype (Typ) = Typ then
13003 return Empty;
13004 elsif Has_Discriminants (Typ) then
13005 return Typ;
13006 else
13007 return Type_With_Explicit_Discrims (Etype (Typ));
13008 end if;
13010 end Type_With_Explicit_Discrims;
13012 -- Start of processing for Expand_To_Stored_Constraint
13014 begin
13015 if No (Constraint)
13016 or else Is_Empty_Elmt_List (Constraint)
13017 then
13018 return No_Elist;
13019 end if;
13021 Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
13023 if No (Explicitly_Discriminated_Type) then
13024 return No_Elist;
13025 end if;
13027 Expansion := New_Elmt_List;
13029 Discriminant :=
13030 First_Stored_Discriminant (Explicitly_Discriminated_Type);
13031 while Present (Discriminant) loop
13032 Append_Elmt (
13033 Get_Discriminant_Value (
13034 Discriminant, Explicitly_Discriminated_Type, Constraint),
13035 Expansion);
13036 Next_Stored_Discriminant (Discriminant);
13037 end loop;
13039 return Expansion;
13040 end Expand_To_Stored_Constraint;
13042 ---------------------------
13043 -- Find_Hidden_Interface --
13044 ---------------------------
13046 function Find_Hidden_Interface
13047 (Src : Elist_Id;
13048 Dest : Elist_Id) return Entity_Id
13050 Iface : Entity_Id;
13051 Iface_Elmt : Elmt_Id;
13053 begin
13054 if Present (Src) and then Present (Dest) then
13055 Iface_Elmt := First_Elmt (Src);
13056 while Present (Iface_Elmt) loop
13057 Iface := Node (Iface_Elmt);
13059 if Is_Interface (Iface)
13060 and then not Contain_Interface (Iface, Dest)
13061 then
13062 return Iface;
13063 end if;
13065 Next_Elmt (Iface_Elmt);
13066 end loop;
13067 end if;
13069 return Empty;
13070 end Find_Hidden_Interface;
13072 --------------------
13073 -- Find_Type_Name --
13074 --------------------
13076 function Find_Type_Name (N : Node_Id) return Entity_Id is
13077 Id : constant Entity_Id := Defining_Identifier (N);
13078 Prev : Entity_Id;
13079 New_Id : Entity_Id;
13080 Prev_Par : Node_Id;
13082 procedure Tag_Mismatch;
13083 -- Diagnose a tagged partial view whose full view is untagged.
13084 -- We post the message on the full view, with a reference to
13085 -- the previous partial view. The partial view can be private
13086 -- or incomplete, and these are handled in a different manner,
13087 -- so we determine the position of the error message from the
13088 -- respective slocs of both.
13090 ------------------
13091 -- Tag_Mismatch --
13092 ------------------
13094 procedure Tag_Mismatch is
13095 begin
13096 if Sloc (Prev) < Sloc (Id) then
13097 Error_Msg_NE
13098 ("full declaration of } must be a tagged type ", Id, Prev);
13099 else
13100 Error_Msg_NE
13101 ("full declaration of } must be a tagged type ", Prev, Id);
13102 end if;
13103 end Tag_Mismatch;
13105 -- Start processing for Find_Type_Name
13107 begin
13108 -- Find incomplete declaration, if one was given
13110 Prev := Current_Entity_In_Scope (Id);
13112 if Present (Prev) then
13114 -- Previous declaration exists. Error if not incomplete/private case
13115 -- except if previous declaration is implicit, etc. Enter_Name will
13116 -- emit error if appropriate.
13118 Prev_Par := Parent (Prev);
13120 if not Is_Incomplete_Or_Private_Type (Prev) then
13121 Enter_Name (Id);
13122 New_Id := Id;
13124 elsif not Nkind_In (N, N_Full_Type_Declaration,
13125 N_Task_Type_Declaration,
13126 N_Protected_Type_Declaration)
13127 then
13128 -- Completion must be a full type declarations (RM 7.3(4))
13130 Error_Msg_Sloc := Sloc (Prev);
13131 Error_Msg_NE ("invalid completion of }", Id, Prev);
13133 -- Set scope of Id to avoid cascaded errors. Entity is never
13134 -- examined again, except when saving globals in generics.
13136 Set_Scope (Id, Current_Scope);
13137 New_Id := Id;
13139 -- Case of full declaration of incomplete type
13141 elsif Ekind (Prev) = E_Incomplete_Type then
13143 -- Indicate that the incomplete declaration has a matching full
13144 -- declaration. The defining occurrence of the incomplete
13145 -- declaration remains the visible one, and the procedure
13146 -- Get_Full_View dereferences it whenever the type is used.
13148 if Present (Full_View (Prev)) then
13149 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
13150 end if;
13152 Set_Full_View (Prev, Id);
13153 Append_Entity (Id, Current_Scope);
13154 Set_Is_Public (Id, Is_Public (Prev));
13155 Set_Is_Internal (Id);
13156 New_Id := Prev;
13158 -- Case of full declaration of private type
13160 else
13161 if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
13162 if Etype (Prev) /= Prev then
13164 -- Prev is a private subtype or a derived type, and needs
13165 -- no completion.
13167 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
13168 New_Id := Id;
13170 elsif Ekind (Prev) = E_Private_Type
13171 and then Nkind_In (N, N_Task_Type_Declaration,
13172 N_Protected_Type_Declaration)
13173 then
13174 Error_Msg_N
13175 ("completion of nonlimited type cannot be limited", N);
13177 elsif Ekind (Prev) = E_Record_Type_With_Private
13178 and then Nkind_In (N, N_Task_Type_Declaration,
13179 N_Protected_Type_Declaration)
13180 then
13181 if not Is_Limited_Record (Prev) then
13182 Error_Msg_N
13183 ("completion of nonlimited type cannot be limited", N);
13185 elsif No (Interface_List (N)) then
13186 Error_Msg_N
13187 ("completion of tagged private type must be tagged",
13189 end if;
13190 end if;
13192 -- Ada 2005 (AI-251): Private extension declaration of a task
13193 -- type or a protected type. This case arises when covering
13194 -- interface types.
13196 elsif Nkind_In (N, N_Task_Type_Declaration,
13197 N_Protected_Type_Declaration)
13198 then
13199 null;
13201 elsif Nkind (N) /= N_Full_Type_Declaration
13202 or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
13203 then
13204 Error_Msg_N
13205 ("full view of private extension must be an extension", N);
13207 elsif not (Abstract_Present (Parent (Prev)))
13208 and then Abstract_Present (Type_Definition (N))
13209 then
13210 Error_Msg_N
13211 ("full view of non-abstract extension cannot be abstract", N);
13212 end if;
13214 if not In_Private_Part (Current_Scope) then
13215 Error_Msg_N
13216 ("declaration of full view must appear in private part", N);
13217 end if;
13219 Copy_And_Swap (Prev, Id);
13220 Set_Has_Private_Declaration (Prev);
13221 Set_Has_Private_Declaration (Id);
13223 -- If no error, propagate freeze_node from private to full view.
13224 -- It may have been generated for an early operational item.
13226 if Present (Freeze_Node (Id))
13227 and then Serious_Errors_Detected = 0
13228 and then No (Full_View (Id))
13229 then
13230 Set_Freeze_Node (Prev, Freeze_Node (Id));
13231 Set_Freeze_Node (Id, Empty);
13232 Set_First_Rep_Item (Prev, First_Rep_Item (Id));
13233 end if;
13235 Set_Full_View (Id, Prev);
13236 New_Id := Prev;
13237 end if;
13239 -- Verify that full declaration conforms to partial one
13241 if Is_Incomplete_Or_Private_Type (Prev)
13242 and then Present (Discriminant_Specifications (Prev_Par))
13243 then
13244 if Present (Discriminant_Specifications (N)) then
13245 if Ekind (Prev) = E_Incomplete_Type then
13246 Check_Discriminant_Conformance (N, Prev, Prev);
13247 else
13248 Check_Discriminant_Conformance (N, Prev, Id);
13249 end if;
13251 else
13252 Error_Msg_N
13253 ("missing discriminants in full type declaration", N);
13255 -- To avoid cascaded errors on subsequent use, share the
13256 -- discriminants of the partial view.
13258 Set_Discriminant_Specifications (N,
13259 Discriminant_Specifications (Prev_Par));
13260 end if;
13261 end if;
13263 -- A prior untagged partial view can have an associated class-wide
13264 -- type due to use of the class attribute, and in this case the full
13265 -- type must also be tagged. This Ada 95 usage is deprecated in favor
13266 -- of incomplete tagged declarations, but we check for it.
13268 if Is_Type (Prev)
13269 and then (Is_Tagged_Type (Prev)
13270 or else Present (Class_Wide_Type (Prev)))
13271 then
13272 -- The full declaration is either a tagged type (including
13273 -- a synchronized type that implements interfaces) or a
13274 -- type extension, otherwise this is an error.
13276 if Nkind_In (N, N_Task_Type_Declaration,
13277 N_Protected_Type_Declaration)
13278 then
13279 if No (Interface_List (N))
13280 and then not Error_Posted (N)
13281 then
13282 Tag_Mismatch;
13283 end if;
13285 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
13287 -- Indicate that the previous declaration (tagged incomplete
13288 -- or private declaration) requires the same on the full one.
13290 if not Tagged_Present (Type_Definition (N)) then
13291 Tag_Mismatch;
13292 Set_Is_Tagged_Type (Id);
13293 Set_Primitive_Operations (Id, New_Elmt_List);
13294 end if;
13296 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
13297 if No (Record_Extension_Part (Type_Definition (N))) then
13298 Error_Msg_NE (
13299 "full declaration of } must be a record extension",
13300 Prev, Id);
13301 Set_Is_Tagged_Type (Id);
13302 Set_Primitive_Operations (Id, New_Elmt_List);
13303 end if;
13305 else
13306 Tag_Mismatch;
13307 end if;
13308 end if;
13310 return New_Id;
13312 else
13313 -- New type declaration
13315 Enter_Name (Id);
13316 return Id;
13317 end if;
13318 end Find_Type_Name;
13320 -------------------------
13321 -- Find_Type_Of_Object --
13322 -------------------------
13324 function Find_Type_Of_Object
13325 (Obj_Def : Node_Id;
13326 Related_Nod : Node_Id) return Entity_Id
13328 Def_Kind : constant Node_Kind := Nkind (Obj_Def);
13329 P : Node_Id := Parent (Obj_Def);
13330 T : Entity_Id;
13331 Nam : Name_Id;
13333 begin
13334 -- If the parent is a component_definition node we climb to the
13335 -- component_declaration node
13337 if Nkind (P) = N_Component_Definition then
13338 P := Parent (P);
13339 end if;
13341 -- Case of an anonymous array subtype
13343 if Nkind_In (Def_Kind, N_Constrained_Array_Definition,
13344 N_Unconstrained_Array_Definition)
13345 then
13346 T := Empty;
13347 Array_Type_Declaration (T, Obj_Def);
13349 -- Create an explicit subtype whenever possible
13351 elsif Nkind (P) /= N_Component_Declaration
13352 and then Def_Kind = N_Subtype_Indication
13353 then
13354 -- Base name of subtype on object name, which will be unique in
13355 -- the current scope.
13357 -- If this is a duplicate declaration, return base type, to avoid
13358 -- generating duplicate anonymous types.
13360 if Error_Posted (P) then
13361 Analyze (Subtype_Mark (Obj_Def));
13362 return Entity (Subtype_Mark (Obj_Def));
13363 end if;
13365 Nam :=
13366 New_External_Name
13367 (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
13369 T := Make_Defining_Identifier (Sloc (P), Nam);
13371 Insert_Action (Obj_Def,
13372 Make_Subtype_Declaration (Sloc (P),
13373 Defining_Identifier => T,
13374 Subtype_Indication => Relocate_Node (Obj_Def)));
13376 -- This subtype may need freezing, and this will not be done
13377 -- automatically if the object declaration is not in declarative
13378 -- part. Since this is an object declaration, the type cannot always
13379 -- be frozen here. Deferred constants do not freeze their type
13380 -- (which often enough will be private).
13382 if Nkind (P) = N_Object_Declaration
13383 and then Constant_Present (P)
13384 and then No (Expression (P))
13385 then
13386 null;
13387 else
13388 Insert_Actions (Obj_Def, Freeze_Entity (T, Sloc (P)));
13389 end if;
13391 -- Ada 2005 AI-406: the object definition in an object declaration
13392 -- can be an access definition.
13394 elsif Def_Kind = N_Access_Definition then
13395 T := Access_Definition (Related_Nod, Obj_Def);
13396 Set_Is_Local_Anonymous_Access (T);
13398 -- Otherwise, the object definition is just a subtype_mark
13400 else
13401 T := Process_Subtype (Obj_Def, Related_Nod);
13402 end if;
13404 return T;
13405 end Find_Type_Of_Object;
13407 --------------------------------
13408 -- Find_Type_Of_Subtype_Indic --
13409 --------------------------------
13411 function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
13412 Typ : Entity_Id;
13414 begin
13415 -- Case of subtype mark with a constraint
13417 if Nkind (S) = N_Subtype_Indication then
13418 Find_Type (Subtype_Mark (S));
13419 Typ := Entity (Subtype_Mark (S));
13421 if not
13422 Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
13423 then
13424 Error_Msg_N
13425 ("incorrect constraint for this kind of type", Constraint (S));
13426 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
13427 end if;
13429 -- Otherwise we have a subtype mark without a constraint
13431 elsif Error_Posted (S) then
13432 Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
13433 return Any_Type;
13435 else
13436 Find_Type (S);
13437 Typ := Entity (S);
13438 end if;
13440 -- Check No_Wide_Characters restriction
13442 if Typ = Standard_Wide_Character
13443 or else Typ = Standard_Wide_Wide_Character
13444 or else Typ = Standard_Wide_String
13445 or else Typ = Standard_Wide_Wide_String
13446 then
13447 Check_Restriction (No_Wide_Characters, S);
13448 end if;
13450 return Typ;
13451 end Find_Type_Of_Subtype_Indic;
13453 -------------------------------------
13454 -- Floating_Point_Type_Declaration --
13455 -------------------------------------
13457 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
13458 Digs : constant Node_Id := Digits_Expression (Def);
13459 Digs_Val : Uint;
13460 Base_Typ : Entity_Id;
13461 Implicit_Base : Entity_Id;
13462 Bound : Node_Id;
13464 function Can_Derive_From (E : Entity_Id) return Boolean;
13465 -- Find if given digits value allows derivation from specified type
13467 ---------------------
13468 -- Can_Derive_From --
13469 ---------------------
13471 function Can_Derive_From (E : Entity_Id) return Boolean is
13472 Spec : constant Entity_Id := Real_Range_Specification (Def);
13474 begin
13475 if Digs_Val > Digits_Value (E) then
13476 return False;
13477 end if;
13479 if Present (Spec) then
13480 if Expr_Value_R (Type_Low_Bound (E)) >
13481 Expr_Value_R (Low_Bound (Spec))
13482 then
13483 return False;
13484 end if;
13486 if Expr_Value_R (Type_High_Bound (E)) <
13487 Expr_Value_R (High_Bound (Spec))
13488 then
13489 return False;
13490 end if;
13491 end if;
13493 return True;
13494 end Can_Derive_From;
13496 -- Start of processing for Floating_Point_Type_Declaration
13498 begin
13499 Check_Restriction (No_Floating_Point, Def);
13501 -- Create an implicit base type
13503 Implicit_Base :=
13504 Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
13506 -- Analyze and verify digits value
13508 Analyze_And_Resolve (Digs, Any_Integer);
13509 Check_Digits_Expression (Digs);
13510 Digs_Val := Expr_Value (Digs);
13512 -- Process possible range spec and find correct type to derive from
13514 Process_Real_Range_Specification (Def);
13516 if Can_Derive_From (Standard_Short_Float) then
13517 Base_Typ := Standard_Short_Float;
13518 elsif Can_Derive_From (Standard_Float) then
13519 Base_Typ := Standard_Float;
13520 elsif Can_Derive_From (Standard_Long_Float) then
13521 Base_Typ := Standard_Long_Float;
13522 elsif Can_Derive_From (Standard_Long_Long_Float) then
13523 Base_Typ := Standard_Long_Long_Float;
13525 -- If we can't derive from any existing type, use long_long_float
13526 -- and give appropriate message explaining the problem.
13528 else
13529 Base_Typ := Standard_Long_Long_Float;
13531 if Digs_Val >= Digits_Value (Standard_Long_Long_Float) then
13532 Error_Msg_Uint_1 := Digits_Value (Standard_Long_Long_Float);
13533 Error_Msg_N ("digits value out of range, maximum is ^", Digs);
13535 else
13536 Error_Msg_N
13537 ("range too large for any predefined type",
13538 Real_Range_Specification (Def));
13539 end if;
13540 end if;
13542 -- If there are bounds given in the declaration use them as the bounds
13543 -- of the type, otherwise use the bounds of the predefined base type
13544 -- that was chosen based on the Digits value.
13546 if Present (Real_Range_Specification (Def)) then
13547 Set_Scalar_Range (T, Real_Range_Specification (Def));
13548 Set_Is_Constrained (T);
13550 -- The bounds of this range must be converted to machine numbers
13551 -- in accordance with RM 4.9(38).
13553 Bound := Type_Low_Bound (T);
13555 if Nkind (Bound) = N_Real_Literal then
13556 Set_Realval
13557 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
13558 Set_Is_Machine_Number (Bound);
13559 end if;
13561 Bound := Type_High_Bound (T);
13563 if Nkind (Bound) = N_Real_Literal then
13564 Set_Realval
13565 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
13566 Set_Is_Machine_Number (Bound);
13567 end if;
13569 else
13570 Set_Scalar_Range (T, Scalar_Range (Base_Typ));
13571 end if;
13573 -- Complete definition of implicit base and declared first subtype
13575 Set_Etype (Implicit_Base, Base_Typ);
13577 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
13578 Set_Size_Info (Implicit_Base, (Base_Typ));
13579 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
13580 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
13581 Set_Digits_Value (Implicit_Base, Digits_Value (Base_Typ));
13582 Set_Vax_Float (Implicit_Base, Vax_Float (Base_Typ));
13584 Set_Ekind (T, E_Floating_Point_Subtype);
13585 Set_Etype (T, Implicit_Base);
13587 Set_Size_Info (T, (Implicit_Base));
13588 Set_RM_Size (T, RM_Size (Implicit_Base));
13589 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
13590 Set_Digits_Value (T, Digs_Val);
13591 end Floating_Point_Type_Declaration;
13593 ----------------------------
13594 -- Get_Discriminant_Value --
13595 ----------------------------
13597 -- This is the situation:
13599 -- There is a non-derived type
13601 -- type T0 (Dx, Dy, Dz...)
13603 -- There are zero or more levels of derivation, with each derivation
13604 -- either purely inheriting the discriminants, or defining its own.
13606 -- type Ti is new Ti-1
13607 -- or
13608 -- type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
13609 -- or
13610 -- subtype Ti is ...
13612 -- The subtype issue is avoided by the use of Original_Record_Component,
13613 -- and the fact that derived subtypes also derive the constraints.
13615 -- This chain leads back from
13617 -- Typ_For_Constraint
13619 -- Typ_For_Constraint has discriminants, and the value for each
13620 -- discriminant is given by its corresponding Elmt of Constraints.
13622 -- Discriminant is some discriminant in this hierarchy
13624 -- We need to return its value
13626 -- We do this by recursively searching each level, and looking for
13627 -- Discriminant. Once we get to the bottom, we start backing up
13628 -- returning the value for it which may in turn be a discriminant
13629 -- further up, so on the backup we continue the substitution.
13631 function Get_Discriminant_Value
13632 (Discriminant : Entity_Id;
13633 Typ_For_Constraint : Entity_Id;
13634 Constraint : Elist_Id) return Node_Id
13636 function Search_Derivation_Levels
13637 (Ti : Entity_Id;
13638 Discrim_Values : Elist_Id;
13639 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
13640 -- This is the routine that performs the recursive search of levels
13641 -- as described above.
13643 ------------------------------
13644 -- Search_Derivation_Levels --
13645 ------------------------------
13647 function Search_Derivation_Levels
13648 (Ti : Entity_Id;
13649 Discrim_Values : Elist_Id;
13650 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
13652 Assoc : Elmt_Id;
13653 Disc : Entity_Id;
13654 Result : Node_Or_Entity_Id;
13655 Result_Entity : Node_Id;
13657 begin
13658 -- If inappropriate type, return Error, this happens only in
13659 -- cascaded error situations, and we want to avoid a blow up.
13661 if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
13662 return Error;
13663 end if;
13665 -- Look deeper if possible. Use Stored_Constraints only for
13666 -- untagged types. For tagged types use the given constraint.
13667 -- This asymmetry needs explanation???
13669 if not Stored_Discrim_Values
13670 and then Present (Stored_Constraint (Ti))
13671 and then not Is_Tagged_Type (Ti)
13672 then
13673 Result :=
13674 Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
13675 else
13676 declare
13677 Td : constant Entity_Id := Etype (Ti);
13679 begin
13680 if Td = Ti then
13681 Result := Discriminant;
13683 else
13684 if Present (Stored_Constraint (Ti)) then
13685 Result :=
13686 Search_Derivation_Levels
13687 (Td, Stored_Constraint (Ti), True);
13688 else
13689 Result :=
13690 Search_Derivation_Levels
13691 (Td, Discrim_Values, Stored_Discrim_Values);
13692 end if;
13693 end if;
13694 end;
13695 end if;
13697 -- Extra underlying places to search, if not found above. For
13698 -- concurrent types, the relevant discriminant appears in the
13699 -- corresponding record. For a type derived from a private type
13700 -- without discriminant, the full view inherits the discriminants
13701 -- of the full view of the parent.
13703 if Result = Discriminant then
13704 if Is_Concurrent_Type (Ti)
13705 and then Present (Corresponding_Record_Type (Ti))
13706 then
13707 Result :=
13708 Search_Derivation_Levels (
13709 Corresponding_Record_Type (Ti),
13710 Discrim_Values,
13711 Stored_Discrim_Values);
13713 elsif Is_Private_Type (Ti)
13714 and then not Has_Discriminants (Ti)
13715 and then Present (Full_View (Ti))
13716 and then Etype (Full_View (Ti)) /= Ti
13717 then
13718 Result :=
13719 Search_Derivation_Levels (
13720 Full_View (Ti),
13721 Discrim_Values,
13722 Stored_Discrim_Values);
13723 end if;
13724 end if;
13726 -- If Result is not a (reference to a) discriminant, return it,
13727 -- otherwise set Result_Entity to the discriminant.
13729 if Nkind (Result) = N_Defining_Identifier then
13730 pragma Assert (Result = Discriminant);
13731 Result_Entity := Result;
13733 else
13734 if not Denotes_Discriminant (Result) then
13735 return Result;
13736 end if;
13738 Result_Entity := Entity (Result);
13739 end if;
13741 -- See if this level of derivation actually has discriminants
13742 -- because tagged derivations can add them, hence the lower
13743 -- levels need not have any.
13745 if not Has_Discriminants (Ti) then
13746 return Result;
13747 end if;
13749 -- Scan Ti's discriminants for Result_Entity,
13750 -- and return its corresponding value, if any.
13752 Result_Entity := Original_Record_Component (Result_Entity);
13754 Assoc := First_Elmt (Discrim_Values);
13756 if Stored_Discrim_Values then
13757 Disc := First_Stored_Discriminant (Ti);
13758 else
13759 Disc := First_Discriminant (Ti);
13760 end if;
13762 while Present (Disc) loop
13763 pragma Assert (Present (Assoc));
13765 if Original_Record_Component (Disc) = Result_Entity then
13766 return Node (Assoc);
13767 end if;
13769 Next_Elmt (Assoc);
13771 if Stored_Discrim_Values then
13772 Next_Stored_Discriminant (Disc);
13773 else
13774 Next_Discriminant (Disc);
13775 end if;
13776 end loop;
13778 -- Could not find it
13780 return Result;
13781 end Search_Derivation_Levels;
13783 -- Local Variables
13785 Result : Node_Or_Entity_Id;
13787 -- Start of processing for Get_Discriminant_Value
13789 begin
13790 -- ??? This routine is a gigantic mess and will be deleted. For the
13791 -- time being just test for the trivial case before calling recurse.
13793 if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
13794 declare
13795 D : Entity_Id;
13796 E : Elmt_Id;
13798 begin
13799 D := First_Discriminant (Typ_For_Constraint);
13800 E := First_Elmt (Constraint);
13801 while Present (D) loop
13802 if Chars (D) = Chars (Discriminant) then
13803 return Node (E);
13804 end if;
13806 Next_Discriminant (D);
13807 Next_Elmt (E);
13808 end loop;
13809 end;
13810 end if;
13812 Result := Search_Derivation_Levels
13813 (Typ_For_Constraint, Constraint, False);
13815 -- ??? hack to disappear when this routine is gone
13817 if Nkind (Result) = N_Defining_Identifier then
13818 declare
13819 D : Entity_Id;
13820 E : Elmt_Id;
13822 begin
13823 D := First_Discriminant (Typ_For_Constraint);
13824 E := First_Elmt (Constraint);
13825 while Present (D) loop
13826 if Corresponding_Discriminant (D) = Discriminant then
13827 return Node (E);
13828 end if;
13830 Next_Discriminant (D);
13831 Next_Elmt (E);
13832 end loop;
13833 end;
13834 end if;
13836 pragma Assert (Nkind (Result) /= N_Defining_Identifier);
13837 return Result;
13838 end Get_Discriminant_Value;
13840 --------------------------
13841 -- Has_Range_Constraint --
13842 --------------------------
13844 function Has_Range_Constraint (N : Node_Id) return Boolean is
13845 C : constant Node_Id := Constraint (N);
13847 begin
13848 if Nkind (C) = N_Range_Constraint then
13849 return True;
13851 elsif Nkind (C) = N_Digits_Constraint then
13852 return
13853 Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
13854 or else
13855 Present (Range_Constraint (C));
13857 elsif Nkind (C) = N_Delta_Constraint then
13858 return Present (Range_Constraint (C));
13860 else
13861 return False;
13862 end if;
13863 end Has_Range_Constraint;
13865 ------------------------
13866 -- Inherit_Components --
13867 ------------------------
13869 function Inherit_Components
13870 (N : Node_Id;
13871 Parent_Base : Entity_Id;
13872 Derived_Base : Entity_Id;
13873 Is_Tagged : Boolean;
13874 Inherit_Discr : Boolean;
13875 Discs : Elist_Id) return Elist_Id
13877 Assoc_List : constant Elist_Id := New_Elmt_List;
13879 procedure Inherit_Component
13880 (Old_C : Entity_Id;
13881 Plain_Discrim : Boolean := False;
13882 Stored_Discrim : Boolean := False);
13883 -- Inherits component Old_C from Parent_Base to the Derived_Base. If
13884 -- Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
13885 -- True, Old_C is a stored discriminant. If they are both false then
13886 -- Old_C is a regular component.
13888 -----------------------
13889 -- Inherit_Component --
13890 -----------------------
13892 procedure Inherit_Component
13893 (Old_C : Entity_Id;
13894 Plain_Discrim : Boolean := False;
13895 Stored_Discrim : Boolean := False)
13897 New_C : constant Entity_Id := New_Copy (Old_C);
13899 Discrim : Entity_Id;
13900 Corr_Discrim : Entity_Id;
13902 begin
13903 pragma Assert (not Is_Tagged or else not Stored_Discrim);
13905 Set_Parent (New_C, Parent (Old_C));
13907 -- Regular discriminants and components must be inserted in the scope
13908 -- of the Derived_Base. Do it here.
13910 if not Stored_Discrim then
13911 Enter_Name (New_C);
13912 end if;
13914 -- For tagged types the Original_Record_Component must point to
13915 -- whatever this field was pointing to in the parent type. This has
13916 -- already been achieved by the call to New_Copy above.
13918 if not Is_Tagged then
13919 Set_Original_Record_Component (New_C, New_C);
13920 end if;
13922 -- If we have inherited a component then see if its Etype contains
13923 -- references to Parent_Base discriminants. In this case, replace
13924 -- these references with the constraints given in Discs. We do not
13925 -- do this for the partial view of private types because this is
13926 -- not needed (only the components of the full view will be used
13927 -- for code generation) and cause problem. We also avoid this
13928 -- transformation in some error situations.
13930 if Ekind (New_C) = E_Component then
13931 if (Is_Private_Type (Derived_Base)
13932 and then not Is_Generic_Type (Derived_Base))
13933 or else (Is_Empty_Elmt_List (Discs)
13934 and then not Expander_Active)
13935 then
13936 Set_Etype (New_C, Etype (Old_C));
13938 else
13939 -- The current component introduces a circularity of the
13940 -- following kind:
13942 -- limited with Pack_2;
13943 -- package Pack_1 is
13944 -- type T_1 is tagged record
13945 -- Comp : access Pack_2.T_2;
13946 -- ...
13947 -- end record;
13948 -- end Pack_1;
13950 -- with Pack_1;
13951 -- package Pack_2 is
13952 -- type T_2 is new Pack_1.T_1 with ...;
13953 -- end Pack_2;
13955 Set_Etype
13956 (New_C,
13957 Constrain_Component_Type
13958 (Old_C, Derived_Base, N, Parent_Base, Discs));
13959 end if;
13960 end if;
13962 -- In derived tagged types it is illegal to reference a non
13963 -- discriminant component in the parent type. To catch this, mark
13964 -- these components with an Ekind of E_Void. This will be reset in
13965 -- Record_Type_Definition after processing the record extension of
13966 -- the derived type.
13968 -- If the declaration is a private extension, there is no further
13969 -- record extension to process, and the components retain their
13970 -- current kind, because they are visible at this point.
13972 if Is_Tagged and then Ekind (New_C) = E_Component
13973 and then Nkind (N) /= N_Private_Extension_Declaration
13974 then
13975 Set_Ekind (New_C, E_Void);
13976 end if;
13978 if Plain_Discrim then
13979 Set_Corresponding_Discriminant (New_C, Old_C);
13980 Build_Discriminal (New_C);
13982 -- If we are explicitly inheriting a stored discriminant it will be
13983 -- completely hidden.
13985 elsif Stored_Discrim then
13986 Set_Corresponding_Discriminant (New_C, Empty);
13987 Set_Discriminal (New_C, Empty);
13988 Set_Is_Completely_Hidden (New_C);
13990 -- Set the Original_Record_Component of each discriminant in the
13991 -- derived base to point to the corresponding stored that we just
13992 -- created.
13994 Discrim := First_Discriminant (Derived_Base);
13995 while Present (Discrim) loop
13996 Corr_Discrim := Corresponding_Discriminant (Discrim);
13998 -- Corr_Discrim could be missing in an error situation
14000 if Present (Corr_Discrim)
14001 and then Original_Record_Component (Corr_Discrim) = Old_C
14002 then
14003 Set_Original_Record_Component (Discrim, New_C);
14004 end if;
14006 Next_Discriminant (Discrim);
14007 end loop;
14009 Append_Entity (New_C, Derived_Base);
14010 end if;
14012 if not Is_Tagged then
14013 Append_Elmt (Old_C, Assoc_List);
14014 Append_Elmt (New_C, Assoc_List);
14015 end if;
14016 end Inherit_Component;
14018 -- Variables local to Inherit_Component
14020 Loc : constant Source_Ptr := Sloc (N);
14022 Parent_Discrim : Entity_Id;
14023 Stored_Discrim : Entity_Id;
14024 D : Entity_Id;
14025 Component : Entity_Id;
14027 -- Start of processing for Inherit_Components
14029 begin
14030 if not Is_Tagged then
14031 Append_Elmt (Parent_Base, Assoc_List);
14032 Append_Elmt (Derived_Base, Assoc_List);
14033 end if;
14035 -- Inherit parent discriminants if needed
14037 if Inherit_Discr then
14038 Parent_Discrim := First_Discriminant (Parent_Base);
14039 while Present (Parent_Discrim) loop
14040 Inherit_Component (Parent_Discrim, Plain_Discrim => True);
14041 Next_Discriminant (Parent_Discrim);
14042 end loop;
14043 end if;
14045 -- Create explicit stored discrims for untagged types when necessary
14047 if not Has_Unknown_Discriminants (Derived_Base)
14048 and then Has_Discriminants (Parent_Base)
14049 and then not Is_Tagged
14050 and then
14051 (not Inherit_Discr
14052 or else First_Discriminant (Parent_Base) /=
14053 First_Stored_Discriminant (Parent_Base))
14054 then
14055 Stored_Discrim := First_Stored_Discriminant (Parent_Base);
14056 while Present (Stored_Discrim) loop
14057 Inherit_Component (Stored_Discrim, Stored_Discrim => True);
14058 Next_Stored_Discriminant (Stored_Discrim);
14059 end loop;
14060 end if;
14062 -- See if we can apply the second transformation for derived types, as
14063 -- explained in point 6. in the comments above Build_Derived_Record_Type
14064 -- This is achieved by appending Derived_Base discriminants into Discs,
14065 -- which has the side effect of returning a non empty Discs list to the
14066 -- caller of Inherit_Components, which is what we want. This must be
14067 -- done for private derived types if there are explicit stored
14068 -- discriminants, to ensure that we can retrieve the values of the
14069 -- constraints provided in the ancestors.
14071 if Inherit_Discr
14072 and then Is_Empty_Elmt_List (Discs)
14073 and then Present (First_Discriminant (Derived_Base))
14074 and then
14075 (not Is_Private_Type (Derived_Base)
14076 or else Is_Completely_Hidden
14077 (First_Stored_Discriminant (Derived_Base))
14078 or else Is_Generic_Type (Derived_Base))
14079 then
14080 D := First_Discriminant (Derived_Base);
14081 while Present (D) loop
14082 Append_Elmt (New_Reference_To (D, Loc), Discs);
14083 Next_Discriminant (D);
14084 end loop;
14085 end if;
14087 -- Finally, inherit non-discriminant components unless they are not
14088 -- visible because defined or inherited from the full view of the
14089 -- parent. Don't inherit the _parent field of the parent type.
14091 Component := First_Entity (Parent_Base);
14092 while Present (Component) loop
14094 -- Ada 2005 (AI-251): Do not inherit components associated with
14095 -- secondary tags of the parent.
14097 if Ekind (Component) = E_Component
14098 and then Present (Related_Type (Component))
14099 then
14100 null;
14102 elsif Ekind (Component) /= E_Component
14103 or else Chars (Component) = Name_uParent
14104 then
14105 null;
14107 -- If the derived type is within the parent type's declarative
14108 -- region, then the components can still be inherited even though
14109 -- they aren't visible at this point. This can occur for cases
14110 -- such as within public child units where the components must
14111 -- become visible upon entering the child unit's private part.
14113 elsif not Is_Visible_Component (Component)
14114 and then not In_Open_Scopes (Scope (Parent_Base))
14115 then
14116 null;
14118 elsif Ekind (Derived_Base) = E_Private_Type
14119 or else Ekind (Derived_Base) = E_Limited_Private_Type
14120 then
14121 null;
14123 else
14124 Inherit_Component (Component);
14125 end if;
14127 Next_Entity (Component);
14128 end loop;
14130 -- For tagged derived types, inherited discriminants cannot be used in
14131 -- component declarations of the record extension part. To achieve this
14132 -- we mark the inherited discriminants as not visible.
14134 if Is_Tagged and then Inherit_Discr then
14135 D := First_Discriminant (Derived_Base);
14136 while Present (D) loop
14137 Set_Is_Immediately_Visible (D, False);
14138 Next_Discriminant (D);
14139 end loop;
14140 end if;
14142 return Assoc_List;
14143 end Inherit_Components;
14145 -----------------------
14146 -- Is_Null_Extension --
14147 -----------------------
14149 function Is_Null_Extension (T : Entity_Id) return Boolean is
14150 Type_Decl : constant Node_Id := Parent (T);
14151 Comp_List : Node_Id;
14152 Comp : Node_Id;
14154 begin
14155 if Nkind (Type_Decl) /= N_Full_Type_Declaration
14156 or else not Is_Tagged_Type (T)
14157 or else Nkind (Type_Definition (Type_Decl)) /=
14158 N_Derived_Type_Definition
14159 or else No (Record_Extension_Part (Type_Definition (Type_Decl)))
14160 then
14161 return False;
14162 end if;
14164 Comp_List :=
14165 Component_List (Record_Extension_Part (Type_Definition (Type_Decl)));
14167 if Present (Discriminant_Specifications (Type_Decl)) then
14168 return False;
14170 elsif Present (Comp_List)
14171 and then Is_Non_Empty_List (Component_Items (Comp_List))
14172 then
14173 Comp := First (Component_Items (Comp_List));
14175 -- Only user-defined components are relevant. The component list
14176 -- may also contain a parent component and internal components
14177 -- corresponding to secondary tags, but these do not determine
14178 -- whether this is a null extension.
14180 while Present (Comp) loop
14181 if Comes_From_Source (Comp) then
14182 return False;
14183 end if;
14185 Next (Comp);
14186 end loop;
14188 return True;
14189 else
14190 return True;
14191 end if;
14192 end Is_Null_Extension;
14194 --------------------
14195 -- Is_Progenitor --
14196 --------------------
14198 function Is_Progenitor
14199 (Iface : Entity_Id;
14200 Typ : Entity_Id) return Boolean
14202 begin
14203 return Implements_Interface (Typ, Iface,
14204 Exclude_Parents => True);
14205 end Is_Progenitor;
14207 ------------------------------
14208 -- Is_Valid_Constraint_Kind --
14209 ------------------------------
14211 function Is_Valid_Constraint_Kind
14212 (T_Kind : Type_Kind;
14213 Constraint_Kind : Node_Kind) return Boolean
14215 begin
14216 case T_Kind is
14217 when Enumeration_Kind |
14218 Integer_Kind =>
14219 return Constraint_Kind = N_Range_Constraint;
14221 when Decimal_Fixed_Point_Kind =>
14222 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
14223 N_Range_Constraint);
14225 when Ordinary_Fixed_Point_Kind =>
14226 return Nkind_In (Constraint_Kind, N_Delta_Constraint,
14227 N_Range_Constraint);
14229 when Float_Kind =>
14230 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
14231 N_Range_Constraint);
14233 when Access_Kind |
14234 Array_Kind |
14235 E_Record_Type |
14236 E_Record_Subtype |
14237 Class_Wide_Kind |
14238 E_Incomplete_Type |
14239 Private_Kind |
14240 Concurrent_Kind =>
14241 return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
14243 when others =>
14244 return True; -- Error will be detected later
14245 end case;
14246 end Is_Valid_Constraint_Kind;
14248 --------------------------
14249 -- Is_Visible_Component --
14250 --------------------------
14252 function Is_Visible_Component (C : Entity_Id) return Boolean is
14253 Original_Comp : Entity_Id := Empty;
14254 Original_Scope : Entity_Id;
14255 Type_Scope : Entity_Id;
14257 function Is_Local_Type (Typ : Entity_Id) return Boolean;
14258 -- Check whether parent type of inherited component is declared locally,
14259 -- possibly within a nested package or instance. The current scope is
14260 -- the derived record itself.
14262 -------------------
14263 -- Is_Local_Type --
14264 -------------------
14266 function Is_Local_Type (Typ : Entity_Id) return Boolean is
14267 Scop : Entity_Id;
14269 begin
14270 Scop := Scope (Typ);
14271 while Present (Scop)
14272 and then Scop /= Standard_Standard
14273 loop
14274 if Scop = Scope (Current_Scope) then
14275 return True;
14276 end if;
14278 Scop := Scope (Scop);
14279 end loop;
14281 return False;
14282 end Is_Local_Type;
14284 -- Start of processing for Is_Visible_Component
14286 begin
14287 if Ekind (C) = E_Component
14288 or else Ekind (C) = E_Discriminant
14289 then
14290 Original_Comp := Original_Record_Component (C);
14291 end if;
14293 if No (Original_Comp) then
14295 -- Premature usage, or previous error
14297 return False;
14299 else
14300 Original_Scope := Scope (Original_Comp);
14301 Type_Scope := Scope (Base_Type (Scope (C)));
14302 end if;
14304 -- This test only concerns tagged types
14306 if not Is_Tagged_Type (Original_Scope) then
14307 return True;
14309 -- If it is _Parent or _Tag, there is no visibility issue
14311 elsif not Comes_From_Source (Original_Comp) then
14312 return True;
14314 -- If we are in the body of an instantiation, the component is visible
14315 -- even when the parent type (possibly defined in an enclosing unit or
14316 -- in a parent unit) might not.
14318 elsif In_Instance_Body then
14319 return True;
14321 -- Discriminants are always visible
14323 elsif Ekind (Original_Comp) = E_Discriminant
14324 and then not Has_Unknown_Discriminants (Original_Scope)
14325 then
14326 return True;
14328 -- If the component has been declared in an ancestor which is currently
14329 -- a private type, then it is not visible. The same applies if the
14330 -- component's containing type is not in an open scope and the original
14331 -- component's enclosing type is a visible full view of a private type
14332 -- (which can occur in cases where an attempt is being made to reference
14333 -- a component in a sibling package that is inherited from a visible
14334 -- component of a type in an ancestor package; the component in the
14335 -- sibling package should not be visible even though the component it
14336 -- inherited from is visible). This does not apply however in the case
14337 -- where the scope of the type is a private child unit, or when the
14338 -- parent comes from a local package in which the ancestor is currently
14339 -- visible. The latter suppression of visibility is needed for cases
14340 -- that are tested in B730006.
14342 elsif Is_Private_Type (Original_Scope)
14343 or else
14344 (not Is_Private_Descendant (Type_Scope)
14345 and then not In_Open_Scopes (Type_Scope)
14346 and then Has_Private_Declaration (Original_Scope))
14347 then
14348 -- If the type derives from an entity in a formal package, there
14349 -- are no additional visible components.
14351 if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
14352 N_Formal_Package_Declaration
14353 then
14354 return False;
14356 -- if we are not in the private part of the current package, there
14357 -- are no additional visible components.
14359 elsif Ekind (Scope (Current_Scope)) = E_Package
14360 and then not In_Private_Part (Scope (Current_Scope))
14361 then
14362 return False;
14363 else
14364 return
14365 Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
14366 and then In_Open_Scopes (Scope (Original_Scope))
14367 and then Is_Local_Type (Type_Scope);
14368 end if;
14370 -- There is another weird way in which a component may be invisible
14371 -- when the private and the full view are not derived from the same
14372 -- ancestor. Here is an example :
14374 -- type A1 is tagged record F1 : integer; end record;
14375 -- type A2 is new A1 with record F2 : integer; end record;
14376 -- type T is new A1 with private;
14377 -- private
14378 -- type T is new A2 with null record;
14380 -- In this case, the full view of T inherits F1 and F2 but the private
14381 -- view inherits only F1
14383 else
14384 declare
14385 Ancestor : Entity_Id := Scope (C);
14387 begin
14388 loop
14389 if Ancestor = Original_Scope then
14390 return True;
14391 elsif Ancestor = Etype (Ancestor) then
14392 return False;
14393 end if;
14395 Ancestor := Etype (Ancestor);
14396 end loop;
14397 end;
14398 end if;
14399 end Is_Visible_Component;
14401 --------------------------
14402 -- Make_Class_Wide_Type --
14403 --------------------------
14405 procedure Make_Class_Wide_Type (T : Entity_Id) is
14406 CW_Type : Entity_Id;
14407 CW_Name : Name_Id;
14408 Next_E : Entity_Id;
14410 begin
14411 -- The class wide type can have been defined by the partial view, in
14412 -- which case everything is already done.
14414 if Present (Class_Wide_Type (T)) then
14415 return;
14416 end if;
14418 CW_Type :=
14419 New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
14421 -- Inherit root type characteristics
14423 CW_Name := Chars (CW_Type);
14424 Next_E := Next_Entity (CW_Type);
14425 Copy_Node (T, CW_Type);
14426 Set_Comes_From_Source (CW_Type, False);
14427 Set_Chars (CW_Type, CW_Name);
14428 Set_Parent (CW_Type, Parent (T));
14429 Set_Next_Entity (CW_Type, Next_E);
14431 -- Ensure we have a new freeze node for the class-wide type. The partial
14432 -- view may have freeze action of its own, requiring a proper freeze
14433 -- node, and the same freeze node cannot be shared between the two
14434 -- types.
14436 Set_Has_Delayed_Freeze (CW_Type);
14437 Set_Freeze_Node (CW_Type, Empty);
14439 -- Customize the class-wide type: It has no prim. op., it cannot be
14440 -- abstract and its Etype points back to the specific root type.
14442 Set_Ekind (CW_Type, E_Class_Wide_Type);
14443 Set_Is_Tagged_Type (CW_Type, True);
14444 Set_Primitive_Operations (CW_Type, New_Elmt_List);
14445 Set_Is_Abstract_Type (CW_Type, False);
14446 Set_Is_Constrained (CW_Type, False);
14447 Set_Is_First_Subtype (CW_Type, Is_First_Subtype (T));
14449 if Ekind (T) = E_Class_Wide_Subtype then
14450 Set_Etype (CW_Type, Etype (Base_Type (T)));
14451 else
14452 Set_Etype (CW_Type, T);
14453 end if;
14455 -- If this is the class_wide type of a constrained subtype, it does
14456 -- not have discriminants.
14458 Set_Has_Discriminants (CW_Type,
14459 Has_Discriminants (T) and then not Is_Constrained (T));
14461 Set_Has_Unknown_Discriminants (CW_Type, True);
14462 Set_Class_Wide_Type (T, CW_Type);
14463 Set_Equivalent_Type (CW_Type, Empty);
14465 -- The class-wide type of a class-wide type is itself (RM 3.9(14))
14467 Set_Class_Wide_Type (CW_Type, CW_Type);
14468 end Make_Class_Wide_Type;
14470 ----------------
14471 -- Make_Index --
14472 ----------------
14474 procedure Make_Index
14475 (I : Node_Id;
14476 Related_Nod : Node_Id;
14477 Related_Id : Entity_Id := Empty;
14478 Suffix_Index : Nat := 1)
14480 R : Node_Id;
14481 T : Entity_Id;
14482 Def_Id : Entity_Id := Empty;
14483 Found : Boolean := False;
14485 begin
14486 -- For a discrete range used in a constrained array definition and
14487 -- defined by a range, an implicit conversion to the predefined type
14488 -- INTEGER is assumed if each bound is either a numeric literal, a named
14489 -- number, or an attribute, and the type of both bounds (prior to the
14490 -- implicit conversion) is the type universal_integer. Otherwise, both
14491 -- bounds must be of the same discrete type, other than universal
14492 -- integer; this type must be determinable independently of the
14493 -- context, but using the fact that the type must be discrete and that
14494 -- both bounds must have the same type.
14496 -- Character literals also have a universal type in the absence of
14497 -- of additional context, and are resolved to Standard_Character.
14499 if Nkind (I) = N_Range then
14501 -- The index is given by a range constraint. The bounds are known
14502 -- to be of a consistent type.
14504 if not Is_Overloaded (I) then
14505 T := Etype (I);
14507 -- For universal bounds, choose the specific predefined type
14509 if T = Universal_Integer then
14510 T := Standard_Integer;
14512 elsif T = Any_Character then
14513 Ambiguous_Character (Low_Bound (I));
14515 T := Standard_Character;
14516 end if;
14518 -- The node may be overloaded because some user-defined operators
14519 -- are available, but if a universal interpretation exists it is
14520 -- also the selected one.
14522 elsif Universal_Interpretation (I) = Universal_Integer then
14523 T := Standard_Integer;
14525 else
14526 T := Any_Type;
14528 declare
14529 Ind : Interp_Index;
14530 It : Interp;
14532 begin
14533 Get_First_Interp (I, Ind, It);
14534 while Present (It.Typ) loop
14535 if Is_Discrete_Type (It.Typ) then
14537 if Found
14538 and then not Covers (It.Typ, T)
14539 and then not Covers (T, It.Typ)
14540 then
14541 Error_Msg_N ("ambiguous bounds in discrete range", I);
14542 exit;
14543 else
14544 T := It.Typ;
14545 Found := True;
14546 end if;
14547 end if;
14549 Get_Next_Interp (Ind, It);
14550 end loop;
14552 if T = Any_Type then
14553 Error_Msg_N ("discrete type required for range", I);
14554 Set_Etype (I, Any_Type);
14555 return;
14557 elsif T = Universal_Integer then
14558 T := Standard_Integer;
14559 end if;
14560 end;
14561 end if;
14563 if not Is_Discrete_Type (T) then
14564 Error_Msg_N ("discrete type required for range", I);
14565 Set_Etype (I, Any_Type);
14566 return;
14567 end if;
14569 if Nkind (Low_Bound (I)) = N_Attribute_Reference
14570 and then Attribute_Name (Low_Bound (I)) = Name_First
14571 and then Is_Entity_Name (Prefix (Low_Bound (I)))
14572 and then Is_Type (Entity (Prefix (Low_Bound (I))))
14573 and then Is_Discrete_Type (Entity (Prefix (Low_Bound (I))))
14574 then
14575 -- The type of the index will be the type of the prefix, as long
14576 -- as the upper bound is 'Last of the same type.
14578 Def_Id := Entity (Prefix (Low_Bound (I)));
14580 if Nkind (High_Bound (I)) /= N_Attribute_Reference
14581 or else Attribute_Name (High_Bound (I)) /= Name_Last
14582 or else not Is_Entity_Name (Prefix (High_Bound (I)))
14583 or else Entity (Prefix (High_Bound (I))) /= Def_Id
14584 then
14585 Def_Id := Empty;
14586 end if;
14587 end if;
14589 R := I;
14590 Process_Range_Expr_In_Decl (R, T);
14592 elsif Nkind (I) = N_Subtype_Indication then
14594 -- The index is given by a subtype with a range constraint
14596 T := Base_Type (Entity (Subtype_Mark (I)));
14598 if not Is_Discrete_Type (T) then
14599 Error_Msg_N ("discrete type required for range", I);
14600 Set_Etype (I, Any_Type);
14601 return;
14602 end if;
14604 R := Range_Expression (Constraint (I));
14606 Resolve (R, T);
14607 Process_Range_Expr_In_Decl (R, Entity (Subtype_Mark (I)));
14609 elsif Nkind (I) = N_Attribute_Reference then
14611 -- The parser guarantees that the attribute is a RANGE attribute
14613 -- If the node denotes the range of a type mark, that is also the
14614 -- resulting type, and we do no need to create an Itype for it.
14616 if Is_Entity_Name (Prefix (I))
14617 and then Comes_From_Source (I)
14618 and then Is_Type (Entity (Prefix (I)))
14619 and then Is_Discrete_Type (Entity (Prefix (I)))
14620 then
14621 Def_Id := Entity (Prefix (I));
14622 end if;
14624 Analyze_And_Resolve (I);
14625 T := Etype (I);
14626 R := I;
14628 -- If none of the above, must be a subtype. We convert this to a
14629 -- range attribute reference because in the case of declared first
14630 -- named subtypes, the types in the range reference can be different
14631 -- from the type of the entity. A range attribute normalizes the
14632 -- reference and obtains the correct types for the bounds.
14634 -- This transformation is in the nature of an expansion, is only
14635 -- done if expansion is active. In particular, it is not done on
14636 -- formal generic types, because we need to retain the name of the
14637 -- original index for instantiation purposes.
14639 else
14640 if not Is_Entity_Name (I) or else not Is_Type (Entity (I)) then
14641 Error_Msg_N ("invalid subtype mark in discrete range ", I);
14642 Set_Etype (I, Any_Integer);
14643 return;
14645 else
14646 -- The type mark may be that of an incomplete type. It is only
14647 -- now that we can get the full view, previous analysis does
14648 -- not look specifically for a type mark.
14650 Set_Entity (I, Get_Full_View (Entity (I)));
14651 Set_Etype (I, Entity (I));
14652 Def_Id := Entity (I);
14654 if not Is_Discrete_Type (Def_Id) then
14655 Error_Msg_N ("discrete type required for index", I);
14656 Set_Etype (I, Any_Type);
14657 return;
14658 end if;
14659 end if;
14661 if Expander_Active then
14662 Rewrite (I,
14663 Make_Attribute_Reference (Sloc (I),
14664 Attribute_Name => Name_Range,
14665 Prefix => Relocate_Node (I)));
14667 -- The original was a subtype mark that does not freeze. This
14668 -- means that the rewritten version must not freeze either.
14670 Set_Must_Not_Freeze (I);
14671 Set_Must_Not_Freeze (Prefix (I));
14673 -- Is order critical??? if so, document why, if not
14674 -- use Analyze_And_Resolve
14676 Analyze_And_Resolve (I);
14677 T := Etype (I);
14678 R := I;
14680 -- If expander is inactive, type is legal, nothing else to construct
14682 else
14683 return;
14684 end if;
14685 end if;
14687 if not Is_Discrete_Type (T) then
14688 Error_Msg_N ("discrete type required for range", I);
14689 Set_Etype (I, Any_Type);
14690 return;
14692 elsif T = Any_Type then
14693 Set_Etype (I, Any_Type);
14694 return;
14695 end if;
14697 -- We will now create the appropriate Itype to describe the range, but
14698 -- first a check. If we originally had a subtype, then we just label
14699 -- the range with this subtype. Not only is there no need to construct
14700 -- a new subtype, but it is wrong to do so for two reasons:
14702 -- 1. A legality concern, if we have a subtype, it must not freeze,
14703 -- and the Itype would cause freezing incorrectly
14705 -- 2. An efficiency concern, if we created an Itype, it would not be
14706 -- recognized as the same type for the purposes of eliminating
14707 -- checks in some circumstances.
14709 -- We signal this case by setting the subtype entity in Def_Id
14711 if No (Def_Id) then
14712 Def_Id :=
14713 Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
14714 Set_Etype (Def_Id, Base_Type (T));
14716 if Is_Signed_Integer_Type (T) then
14717 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
14719 elsif Is_Modular_Integer_Type (T) then
14720 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
14722 else
14723 Set_Ekind (Def_Id, E_Enumeration_Subtype);
14724 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
14725 Set_First_Literal (Def_Id, First_Literal (T));
14726 end if;
14728 Set_Size_Info (Def_Id, (T));
14729 Set_RM_Size (Def_Id, RM_Size (T));
14730 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14732 Set_Scalar_Range (Def_Id, R);
14733 Conditional_Delay (Def_Id, T);
14735 -- In the subtype indication case, if the immediate parent of the
14736 -- new subtype is non-static, then the subtype we create is non-
14737 -- static, even if its bounds are static.
14739 if Nkind (I) = N_Subtype_Indication
14740 and then not Is_Static_Subtype (Entity (Subtype_Mark (I)))
14741 then
14742 Set_Is_Non_Static_Subtype (Def_Id);
14743 end if;
14744 end if;
14746 -- Final step is to label the index with this constructed type
14748 Set_Etype (I, Def_Id);
14749 end Make_Index;
14751 ------------------------------
14752 -- Modular_Type_Declaration --
14753 ------------------------------
14755 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
14756 Mod_Expr : constant Node_Id := Expression (Def);
14757 M_Val : Uint;
14759 procedure Set_Modular_Size (Bits : Int);
14760 -- Sets RM_Size to Bits, and Esize to normal word size above this
14762 ----------------------
14763 -- Set_Modular_Size --
14764 ----------------------
14766 procedure Set_Modular_Size (Bits : Int) is
14767 begin
14768 Set_RM_Size (T, UI_From_Int (Bits));
14770 if Bits <= 8 then
14771 Init_Esize (T, 8);
14773 elsif Bits <= 16 then
14774 Init_Esize (T, 16);
14776 elsif Bits <= 32 then
14777 Init_Esize (T, 32);
14779 else
14780 Init_Esize (T, System_Max_Binary_Modulus_Power);
14781 end if;
14782 end Set_Modular_Size;
14784 -- Start of processing for Modular_Type_Declaration
14786 begin
14787 Analyze_And_Resolve (Mod_Expr, Any_Integer);
14788 Set_Etype (T, T);
14789 Set_Ekind (T, E_Modular_Integer_Type);
14790 Init_Alignment (T);
14791 Set_Is_Constrained (T);
14793 if not Is_OK_Static_Expression (Mod_Expr) then
14794 Flag_Non_Static_Expr
14795 ("non-static expression used for modular type bound!", Mod_Expr);
14796 M_Val := 2 ** System_Max_Binary_Modulus_Power;
14797 else
14798 M_Val := Expr_Value (Mod_Expr);
14799 end if;
14801 if M_Val < 1 then
14802 Error_Msg_N ("modulus value must be positive", Mod_Expr);
14803 M_Val := 2 ** System_Max_Binary_Modulus_Power;
14804 end if;
14806 Set_Modulus (T, M_Val);
14808 -- Create bounds for the modular type based on the modulus given in
14809 -- the type declaration and then analyze and resolve those bounds.
14811 Set_Scalar_Range (T,
14812 Make_Range (Sloc (Mod_Expr),
14813 Low_Bound =>
14814 Make_Integer_Literal (Sloc (Mod_Expr), 0),
14815 High_Bound =>
14816 Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
14818 -- Properly analyze the literals for the range. We do this manually
14819 -- because we can't go calling Resolve, since we are resolving these
14820 -- bounds with the type, and this type is certainly not complete yet!
14822 Set_Etype (Low_Bound (Scalar_Range (T)), T);
14823 Set_Etype (High_Bound (Scalar_Range (T)), T);
14824 Set_Is_Static_Expression (Low_Bound (Scalar_Range (T)));
14825 Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
14827 -- Loop through powers of two to find number of bits required
14829 for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
14831 -- Binary case
14833 if M_Val = 2 ** Bits then
14834 Set_Modular_Size (Bits);
14835 return;
14837 -- Non-binary case
14839 elsif M_Val < 2 ** Bits then
14840 Set_Non_Binary_Modulus (T);
14842 if Bits > System_Max_Nonbinary_Modulus_Power then
14843 Error_Msg_Uint_1 :=
14844 UI_From_Int (System_Max_Nonbinary_Modulus_Power);
14845 Error_Msg_F
14846 ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
14847 Set_Modular_Size (System_Max_Binary_Modulus_Power);
14848 return;
14850 else
14851 -- In the non-binary case, set size as per RM 13.3(55)
14853 Set_Modular_Size (Bits);
14854 return;
14855 end if;
14856 end if;
14858 end loop;
14860 -- If we fall through, then the size exceed System.Max_Binary_Modulus
14861 -- so we just signal an error and set the maximum size.
14863 Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
14864 Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
14866 Set_Modular_Size (System_Max_Binary_Modulus_Power);
14867 Init_Alignment (T);
14869 end Modular_Type_Declaration;
14871 --------------------------
14872 -- New_Concatenation_Op --
14873 --------------------------
14875 procedure New_Concatenation_Op (Typ : Entity_Id) is
14876 Loc : constant Source_Ptr := Sloc (Typ);
14877 Op : Entity_Id;
14879 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
14880 -- Create abbreviated declaration for the formal of a predefined
14881 -- Operator 'Op' of type 'Typ'
14883 --------------------
14884 -- Make_Op_Formal --
14885 --------------------
14887 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
14888 Formal : Entity_Id;
14889 begin
14890 Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
14891 Set_Etype (Formal, Typ);
14892 Set_Mechanism (Formal, Default_Mechanism);
14893 return Formal;
14894 end Make_Op_Formal;
14896 -- Start of processing for New_Concatenation_Op
14898 begin
14899 Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
14901 Set_Ekind (Op, E_Operator);
14902 Set_Scope (Op, Current_Scope);
14903 Set_Etype (Op, Typ);
14904 Set_Homonym (Op, Get_Name_Entity_Id (Name_Op_Concat));
14905 Set_Is_Immediately_Visible (Op);
14906 Set_Is_Intrinsic_Subprogram (Op);
14907 Set_Has_Completion (Op);
14908 Append_Entity (Op, Current_Scope);
14910 Set_Name_Entity_Id (Name_Op_Concat, Op);
14912 Append_Entity (Make_Op_Formal (Typ, Op), Op);
14913 Append_Entity (Make_Op_Formal (Typ, Op), Op);
14914 end New_Concatenation_Op;
14916 -------------------------
14917 -- OK_For_Limited_Init --
14918 -------------------------
14920 -- ???Check all calls of this, and compare the conditions under which it's
14921 -- called.
14923 function OK_For_Limited_Init (Exp : Node_Id) return Boolean is
14924 begin
14925 return Ada_Version >= Ada_05
14926 and then not Debug_Flag_Dot_L
14927 and then OK_For_Limited_Init_In_05 (Exp);
14928 end OK_For_Limited_Init;
14930 -------------------------------
14931 -- OK_For_Limited_Init_In_05 --
14932 -------------------------------
14934 function OK_For_Limited_Init_In_05 (Exp : Node_Id) return Boolean is
14935 begin
14936 -- Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
14937 -- case of limited aggregates (including extension aggregates), and
14938 -- function calls. The function call may have been give in prefixed
14939 -- notation, in which case the original node is an indexed component.
14941 case Nkind (Original_Node (Exp)) is
14942 when N_Aggregate | N_Extension_Aggregate | N_Function_Call | N_Op =>
14943 return True;
14945 when N_Qualified_Expression =>
14946 return
14947 OK_For_Limited_Init_In_05 (Expression (Original_Node (Exp)));
14949 -- Ada 2005 (AI-251): If a class-wide interface object is initialized
14950 -- with a function call, the expander has rewritten the call into an
14951 -- N_Type_Conversion node to force displacement of the pointer to
14952 -- reference the component containing the secondary dispatch table.
14953 -- Otherwise a type conversion is not a legal context.
14955 when N_Type_Conversion =>
14956 return not Comes_From_Source (Exp)
14957 and then
14958 OK_For_Limited_Init_In_05 (Expression (Original_Node (Exp)));
14960 when N_Indexed_Component | N_Selected_Component =>
14961 return Nkind (Exp) = N_Function_Call;
14963 -- A use of 'Input is a function call, hence allowed. Normally the
14964 -- attribute will be changed to a call, but the attribute by itself
14965 -- can occur with -gnatc.
14967 when N_Attribute_Reference =>
14968 return Attribute_Name (Original_Node (Exp)) = Name_Input;
14970 when others =>
14971 return False;
14972 end case;
14973 end OK_For_Limited_Init_In_05;
14975 -------------------------------------------
14976 -- Ordinary_Fixed_Point_Type_Declaration --
14977 -------------------------------------------
14979 procedure Ordinary_Fixed_Point_Type_Declaration
14980 (T : Entity_Id;
14981 Def : Node_Id)
14983 Loc : constant Source_Ptr := Sloc (Def);
14984 Delta_Expr : constant Node_Id := Delta_Expression (Def);
14985 RRS : constant Node_Id := Real_Range_Specification (Def);
14986 Implicit_Base : Entity_Id;
14987 Delta_Val : Ureal;
14988 Small_Val : Ureal;
14989 Low_Val : Ureal;
14990 High_Val : Ureal;
14992 begin
14993 Check_Restriction (No_Fixed_Point, Def);
14995 -- Create implicit base type
14997 Implicit_Base :=
14998 Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
14999 Set_Etype (Implicit_Base, Implicit_Base);
15001 -- Analyze and process delta expression
15003 Analyze_And_Resolve (Delta_Expr, Any_Real);
15005 Check_Delta_Expression (Delta_Expr);
15006 Delta_Val := Expr_Value_R (Delta_Expr);
15008 Set_Delta_Value (Implicit_Base, Delta_Val);
15010 -- Compute default small from given delta, which is the largest power
15011 -- of two that does not exceed the given delta value.
15013 declare
15014 Tmp : Ureal;
15015 Scale : Int;
15017 begin
15018 Tmp := Ureal_1;
15019 Scale := 0;
15021 if Delta_Val < Ureal_1 then
15022 while Delta_Val < Tmp loop
15023 Tmp := Tmp / Ureal_2;
15024 Scale := Scale + 1;
15025 end loop;
15027 else
15028 loop
15029 Tmp := Tmp * Ureal_2;
15030 exit when Tmp > Delta_Val;
15031 Scale := Scale - 1;
15032 end loop;
15033 end if;
15035 Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
15036 end;
15038 Set_Small_Value (Implicit_Base, Small_Val);
15040 -- If no range was given, set a dummy range
15042 if RRS <= Empty_Or_Error then
15043 Low_Val := -Small_Val;
15044 High_Val := Small_Val;
15046 -- Otherwise analyze and process given range
15048 else
15049 declare
15050 Low : constant Node_Id := Low_Bound (RRS);
15051 High : constant Node_Id := High_Bound (RRS);
15053 begin
15054 Analyze_And_Resolve (Low, Any_Real);
15055 Analyze_And_Resolve (High, Any_Real);
15056 Check_Real_Bound (Low);
15057 Check_Real_Bound (High);
15059 -- Obtain and set the range
15061 Low_Val := Expr_Value_R (Low);
15062 High_Val := Expr_Value_R (High);
15064 if Low_Val > High_Val then
15065 Error_Msg_NE ("?fixed point type& has null range", Def, T);
15066 end if;
15067 end;
15068 end if;
15070 -- The range for both the implicit base and the declared first subtype
15071 -- cannot be set yet, so we use the special routine Set_Fixed_Range to
15072 -- set a temporary range in place. Note that the bounds of the base
15073 -- type will be widened to be symmetrical and to fill the available
15074 -- bits when the type is frozen.
15076 -- We could do this with all discrete types, and probably should, but
15077 -- we absolutely have to do it for fixed-point, since the end-points
15078 -- of the range and the size are determined by the small value, which
15079 -- could be reset before the freeze point.
15081 Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
15082 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
15084 -- Complete definition of first subtype
15086 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
15087 Set_Etype (T, Implicit_Base);
15088 Init_Size_Align (T);
15089 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
15090 Set_Small_Value (T, Small_Val);
15091 Set_Delta_Value (T, Delta_Val);
15092 Set_Is_Constrained (T);
15094 end Ordinary_Fixed_Point_Type_Declaration;
15096 ----------------------------------------
15097 -- Prepare_Private_Subtype_Completion --
15098 ----------------------------------------
15100 procedure Prepare_Private_Subtype_Completion
15101 (Id : Entity_Id;
15102 Related_Nod : Node_Id)
15104 Id_B : constant Entity_Id := Base_Type (Id);
15105 Full_B : constant Entity_Id := Full_View (Id_B);
15106 Full : Entity_Id;
15108 begin
15109 if Present (Full_B) then
15111 -- The Base_Type is already completed, we can complete the subtype
15112 -- now. We have to create a new entity with the same name, Thus we
15113 -- can't use Create_Itype.
15115 -- This is messy, should be fixed ???
15117 Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
15118 Set_Is_Itype (Full);
15119 Set_Associated_Node_For_Itype (Full, Related_Nod);
15120 Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
15121 end if;
15123 -- The parent subtype may be private, but the base might not, in some
15124 -- nested instances. In that case, the subtype does not need to be
15125 -- exchanged. It would still be nice to make private subtypes and their
15126 -- bases consistent at all times ???
15128 if Is_Private_Type (Id_B) then
15129 Append_Elmt (Id, Private_Dependents (Id_B));
15130 end if;
15132 end Prepare_Private_Subtype_Completion;
15134 ---------------------------
15135 -- Process_Discriminants --
15136 ---------------------------
15138 procedure Process_Discriminants
15139 (N : Node_Id;
15140 Prev : Entity_Id := Empty)
15142 Elist : constant Elist_Id := New_Elmt_List;
15143 Id : Node_Id;
15144 Discr : Node_Id;
15145 Discr_Number : Uint;
15146 Discr_Type : Entity_Id;
15147 Default_Present : Boolean := False;
15148 Default_Not_Present : Boolean := False;
15150 begin
15151 -- A composite type other than an array type can have discriminants.
15152 -- On entry, the current scope is the composite type.
15154 -- The discriminants are initially entered into the scope of the type
15155 -- via Enter_Name with the default Ekind of E_Void to prevent premature
15156 -- use, as explained at the end of this procedure.
15158 Discr := First (Discriminant_Specifications (N));
15159 while Present (Discr) loop
15160 Enter_Name (Defining_Identifier (Discr));
15162 -- For navigation purposes we add a reference to the discriminant
15163 -- in the entity for the type. If the current declaration is a
15164 -- completion, place references on the partial view. Otherwise the
15165 -- type is the current scope.
15167 if Present (Prev) then
15169 -- The references go on the partial view, if present. If the
15170 -- partial view has discriminants, the references have been
15171 -- generated already.
15173 if not Has_Discriminants (Prev) then
15174 Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
15175 end if;
15176 else
15177 Generate_Reference
15178 (Current_Scope, Defining_Identifier (Discr), 'd');
15179 end if;
15181 if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
15182 Discr_Type := Access_Definition (Discr, Discriminant_Type (Discr));
15184 -- Ada 2005 (AI-254)
15186 if Present (Access_To_Subprogram_Definition
15187 (Discriminant_Type (Discr)))
15188 and then Protected_Present (Access_To_Subprogram_Definition
15189 (Discriminant_Type (Discr)))
15190 then
15191 Discr_Type :=
15192 Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
15193 end if;
15195 else
15196 Find_Type (Discriminant_Type (Discr));
15197 Discr_Type := Etype (Discriminant_Type (Discr));
15199 if Error_Posted (Discriminant_Type (Discr)) then
15200 Discr_Type := Any_Type;
15201 end if;
15202 end if;
15204 if Is_Access_Type (Discr_Type) then
15206 -- Ada 2005 (AI-230): Access discriminant allowed in non-limited
15207 -- record types
15209 if Ada_Version < Ada_05 then
15210 Check_Access_Discriminant_Requires_Limited
15211 (Discr, Discriminant_Type (Discr));
15212 end if;
15214 if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
15215 Error_Msg_N
15216 ("(Ada 83) access discriminant not allowed", Discr);
15217 end if;
15219 elsif not Is_Discrete_Type (Discr_Type) then
15220 Error_Msg_N ("discriminants must have a discrete or access type",
15221 Discriminant_Type (Discr));
15222 end if;
15224 Set_Etype (Defining_Identifier (Discr), Discr_Type);
15226 -- If a discriminant specification includes the assignment compound
15227 -- delimiter followed by an expression, the expression is the default
15228 -- expression of the discriminant; the default expression must be of
15229 -- the type of the discriminant. (RM 3.7.1) Since this expression is
15230 -- a default expression, we do the special preanalysis, since this
15231 -- expression does not freeze (see "Handling of Default and Per-
15232 -- Object Expressions" in spec of package Sem).
15234 if Present (Expression (Discr)) then
15235 Preanalyze_Spec_Expression (Expression (Discr), Discr_Type);
15237 if Nkind (N) = N_Formal_Type_Declaration then
15238 Error_Msg_N
15239 ("discriminant defaults not allowed for formal type",
15240 Expression (Discr));
15242 -- Tagged types cannot have defaulted discriminants, but a
15243 -- non-tagged private type with defaulted discriminants
15244 -- can have a tagged completion.
15246 elsif Is_Tagged_Type (Current_Scope)
15247 and then Comes_From_Source (N)
15248 then
15249 Error_Msg_N
15250 ("discriminants of tagged type cannot have defaults",
15251 Expression (Discr));
15253 else
15254 Default_Present := True;
15255 Append_Elmt (Expression (Discr), Elist);
15257 -- Tag the defining identifiers for the discriminants with
15258 -- their corresponding default expressions from the tree.
15260 Set_Discriminant_Default_Value
15261 (Defining_Identifier (Discr), Expression (Discr));
15262 end if;
15264 else
15265 Default_Not_Present := True;
15266 end if;
15268 -- Ada 2005 (AI-231): Create an Itype that is a duplicate of
15269 -- Discr_Type but with the null-exclusion attribute
15271 if Ada_Version >= Ada_05 then
15273 -- Ada 2005 (AI-231): Static checks
15275 if Can_Never_Be_Null (Discr_Type) then
15276 Null_Exclusion_Static_Checks (Discr);
15278 elsif Is_Access_Type (Discr_Type)
15279 and then Null_Exclusion_Present (Discr)
15281 -- No need to check itypes because in their case this check
15282 -- was done at their point of creation
15284 and then not Is_Itype (Discr_Type)
15285 then
15286 if Can_Never_Be_Null (Discr_Type) then
15287 Error_Msg_NE
15288 ("`NOT NULL` not allowed (& already excludes null)",
15289 Discr,
15290 Discr_Type);
15291 end if;
15293 Set_Etype (Defining_Identifier (Discr),
15294 Create_Null_Excluding_Itype
15295 (T => Discr_Type,
15296 Related_Nod => Discr));
15297 end if;
15299 -- Ada 2005 (AI-402): access discriminants of nonlimited types
15300 -- can't have defaults. Synchronized types, or types that are
15301 -- explicitly limited are fine, but special tests apply to derived
15302 -- types in generics: in a generic body we have to assume the
15303 -- worst, and therefore defaults are not allowed if the parent is
15304 -- a generic formal private type (see ACATS B370001).
15306 if Is_Access_Type (Discr_Type) then
15307 if Ekind (Discr_Type) /= E_Anonymous_Access_Type
15308 or else not Default_Present
15309 or else Is_Limited_Record (Current_Scope)
15310 or else Is_Concurrent_Type (Current_Scope)
15311 or else Is_Concurrent_Record_Type (Current_Scope)
15312 or else Ekind (Current_Scope) = E_Limited_Private_Type
15313 then
15314 if not Is_Derived_Type (Current_Scope)
15315 or else not Is_Generic_Type (Etype (Current_Scope))
15316 or else not In_Package_Body (Scope (Etype (Current_Scope)))
15317 or else Limited_Present
15318 (Type_Definition (Parent (Current_Scope)))
15319 then
15320 null;
15322 else
15323 Error_Msg_N ("access discriminants of nonlimited types",
15324 Expression (Discr));
15325 Error_Msg_N ("\cannot have defaults", Expression (Discr));
15326 end if;
15328 elsif Present (Expression (Discr)) then
15329 Error_Msg_N
15330 ("(Ada 2005) access discriminants of nonlimited types",
15331 Expression (Discr));
15332 Error_Msg_N ("\cannot have defaults", Expression (Discr));
15333 end if;
15334 end if;
15335 end if;
15337 Next (Discr);
15338 end loop;
15340 -- An element list consisting of the default expressions of the
15341 -- discriminants is constructed in the above loop and used to set
15342 -- the Discriminant_Constraint attribute for the type. If an object
15343 -- is declared of this (record or task) type without any explicit
15344 -- discriminant constraint given, this element list will form the
15345 -- actual parameters for the corresponding initialization procedure
15346 -- for the type.
15348 Set_Discriminant_Constraint (Current_Scope, Elist);
15349 Set_Stored_Constraint (Current_Scope, No_Elist);
15351 -- Default expressions must be provided either for all or for none
15352 -- of the discriminants of a discriminant part. (RM 3.7.1)
15354 if Default_Present and then Default_Not_Present then
15355 Error_Msg_N
15356 ("incomplete specification of defaults for discriminants", N);
15357 end if;
15359 -- The use of the name of a discriminant is not allowed in default
15360 -- expressions of a discriminant part if the specification of the
15361 -- discriminant is itself given in the discriminant part. (RM 3.7.1)
15363 -- To detect this, the discriminant names are entered initially with an
15364 -- Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
15365 -- attempt to use a void entity (for example in an expression that is
15366 -- type-checked) produces the error message: premature usage. Now after
15367 -- completing the semantic analysis of the discriminant part, we can set
15368 -- the Ekind of all the discriminants appropriately.
15370 Discr := First (Discriminant_Specifications (N));
15371 Discr_Number := Uint_1;
15372 while Present (Discr) loop
15373 Id := Defining_Identifier (Discr);
15374 Set_Ekind (Id, E_Discriminant);
15375 Init_Component_Location (Id);
15376 Init_Esize (Id);
15377 Set_Discriminant_Number (Id, Discr_Number);
15379 -- Make sure this is always set, even in illegal programs
15381 Set_Corresponding_Discriminant (Id, Empty);
15383 -- Initialize the Original_Record_Component to the entity itself.
15384 -- Inherit_Components will propagate the right value to
15385 -- discriminants in derived record types.
15387 Set_Original_Record_Component (Id, Id);
15389 -- Create the discriminal for the discriminant
15391 Build_Discriminal (Id);
15393 Next (Discr);
15394 Discr_Number := Discr_Number + 1;
15395 end loop;
15397 Set_Has_Discriminants (Current_Scope);
15398 end Process_Discriminants;
15400 -----------------------
15401 -- Process_Full_View --
15402 -----------------------
15404 procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
15405 Priv_Parent : Entity_Id;
15406 Full_Parent : Entity_Id;
15407 Full_Indic : Node_Id;
15409 procedure Collect_Implemented_Interfaces
15410 (Typ : Entity_Id;
15411 Ifaces : Elist_Id);
15412 -- Ada 2005: Gather all the interfaces that Typ directly or
15413 -- inherently implements. Duplicate entries are not added to
15414 -- the list Ifaces.
15416 ------------------------------------
15417 -- Collect_Implemented_Interfaces --
15418 ------------------------------------
15420 procedure Collect_Implemented_Interfaces
15421 (Typ : Entity_Id;
15422 Ifaces : Elist_Id)
15424 Iface : Entity_Id;
15425 Iface_Elmt : Elmt_Id;
15427 begin
15428 -- Abstract interfaces are only associated with tagged record types
15430 if not Is_Tagged_Type (Typ)
15431 or else not Is_Record_Type (Typ)
15432 then
15433 return;
15434 end if;
15436 -- Recursively climb to the ancestors
15438 if Etype (Typ) /= Typ
15440 -- Protect the frontend against wrong cyclic declarations like:
15442 -- type B is new A with private;
15443 -- type C is new A with private;
15444 -- private
15445 -- type B is new C with null record;
15446 -- type C is new B with null record;
15448 and then Etype (Typ) /= Priv_T
15449 and then Etype (Typ) /= Full_T
15450 then
15451 -- Keep separate the management of private type declarations
15453 if Ekind (Typ) = E_Record_Type_With_Private then
15455 -- Handle the following erronous case:
15456 -- type Private_Type is tagged private;
15457 -- private
15458 -- type Private_Type is new Type_Implementing_Iface;
15460 if Present (Full_View (Typ))
15461 and then Etype (Typ) /= Full_View (Typ)
15462 then
15463 if Is_Interface (Etype (Typ)) then
15464 Append_Unique_Elmt (Etype (Typ), Ifaces);
15465 end if;
15467 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
15468 end if;
15470 -- Non-private types
15472 else
15473 if Is_Interface (Etype (Typ)) then
15474 Append_Unique_Elmt (Etype (Typ), Ifaces);
15475 end if;
15477 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
15478 end if;
15479 end if;
15481 -- Handle entities in the list of abstract interfaces
15483 if Present (Interfaces (Typ)) then
15484 Iface_Elmt := First_Elmt (Interfaces (Typ));
15485 while Present (Iface_Elmt) loop
15486 Iface := Node (Iface_Elmt);
15488 pragma Assert (Is_Interface (Iface));
15490 if not Contain_Interface (Iface, Ifaces) then
15491 Append_Elmt (Iface, Ifaces);
15492 Collect_Implemented_Interfaces (Iface, Ifaces);
15493 end if;
15495 Next_Elmt (Iface_Elmt);
15496 end loop;
15497 end if;
15498 end Collect_Implemented_Interfaces;
15500 -- Start of processing for Process_Full_View
15502 begin
15503 -- First some sanity checks that must be done after semantic
15504 -- decoration of the full view and thus cannot be placed with other
15505 -- similar checks in Find_Type_Name
15507 if not Is_Limited_Type (Priv_T)
15508 and then (Is_Limited_Type (Full_T)
15509 or else Is_Limited_Composite (Full_T))
15510 then
15511 Error_Msg_N
15512 ("completion of nonlimited type cannot be limited", Full_T);
15513 Explain_Limited_Type (Full_T, Full_T);
15515 elsif Is_Abstract_Type (Full_T)
15516 and then not Is_Abstract_Type (Priv_T)
15517 then
15518 Error_Msg_N
15519 ("completion of nonabstract type cannot be abstract", Full_T);
15521 elsif Is_Tagged_Type (Priv_T)
15522 and then Is_Limited_Type (Priv_T)
15523 and then not Is_Limited_Type (Full_T)
15524 then
15525 -- If pragma CPP_Class was applied to the private declaration
15526 -- propagate the limitedness to the full-view
15528 if Is_CPP_Class (Priv_T) then
15529 Set_Is_Limited_Record (Full_T);
15531 -- GNAT allow its own definition of Limited_Controlled to disobey
15532 -- this rule in order in ease the implementation. The next test is
15533 -- safe because Root_Controlled is defined in a private system child
15535 elsif Etype (Full_T) = Full_View (RTE (RE_Root_Controlled)) then
15536 Set_Is_Limited_Composite (Full_T);
15537 else
15538 Error_Msg_N
15539 ("completion of limited tagged type must be limited", Full_T);
15540 end if;
15542 elsif Is_Generic_Type (Priv_T) then
15543 Error_Msg_N ("generic type cannot have a completion", Full_T);
15544 end if;
15546 -- Check that ancestor interfaces of private and full views are
15547 -- consistent. We omit this check for synchronized types because
15548 -- they are performed on the corresponding record type when frozen.
15550 if Ada_Version >= Ada_05
15551 and then Is_Tagged_Type (Priv_T)
15552 and then Is_Tagged_Type (Full_T)
15553 and then not Is_Concurrent_Type (Full_T)
15554 then
15555 declare
15556 Iface : Entity_Id;
15557 Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
15558 Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
15560 begin
15561 Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
15562 Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
15564 -- Ada 2005 (AI-251): The partial view shall be a descendant of
15565 -- an interface type if and only if the full type is descendant
15566 -- of the interface type (AARM 7.3 (7.3/2).
15568 Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
15570 if Present (Iface) then
15571 Error_Msg_NE ("interface & not implemented by full type " &
15572 "(RM-2005 7.3 (7.3/2))", Priv_T, Iface);
15573 end if;
15575 Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
15577 if Present (Iface) then
15578 Error_Msg_NE ("interface & not implemented by partial view " &
15579 "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
15580 end if;
15581 end;
15582 end if;
15584 if Is_Tagged_Type (Priv_T)
15585 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
15586 and then Is_Derived_Type (Full_T)
15587 then
15588 Priv_Parent := Etype (Priv_T);
15590 -- The full view of a private extension may have been transformed
15591 -- into an unconstrained derived type declaration and a subtype
15592 -- declaration (see build_derived_record_type for details).
15594 if Nkind (N) = N_Subtype_Declaration then
15595 Full_Indic := Subtype_Indication (N);
15596 Full_Parent := Etype (Base_Type (Full_T));
15597 else
15598 Full_Indic := Subtype_Indication (Type_Definition (N));
15599 Full_Parent := Etype (Full_T);
15600 end if;
15602 -- Check that the parent type of the full type is a descendant of
15603 -- the ancestor subtype given in the private extension. If either
15604 -- entity has an Etype equal to Any_Type then we had some previous
15605 -- error situation [7.3(8)].
15607 if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
15608 return;
15610 -- Ada 2005 (AI-251): Interfaces in the full-typ can be given in
15611 -- any order. Therefore we don't have to check that its parent must
15612 -- be a descendant of the parent of the private type declaration.
15614 elsif Is_Interface (Priv_Parent)
15615 and then Is_Interface (Full_Parent)
15616 then
15617 null;
15619 -- Ada 2005 (AI-251): If the parent of the private type declaration
15620 -- is an interface there is no need to check that it is an ancestor
15621 -- of the associated full type declaration. The required tests for
15622 -- this case case are performed by Build_Derived_Record_Type.
15624 elsif not Is_Interface (Base_Type (Priv_Parent))
15625 and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
15626 then
15627 Error_Msg_N
15628 ("parent of full type must descend from parent"
15629 & " of private extension", Full_Indic);
15631 -- Check the rules of 7.3(10): if the private extension inherits
15632 -- known discriminants, then the full type must also inherit those
15633 -- discriminants from the same (ancestor) type, and the parent
15634 -- subtype of the full type must be constrained if and only if
15635 -- the ancestor subtype of the private extension is constrained.
15637 elsif No (Discriminant_Specifications (Parent (Priv_T)))
15638 and then not Has_Unknown_Discriminants (Priv_T)
15639 and then Has_Discriminants (Base_Type (Priv_Parent))
15640 then
15641 declare
15642 Priv_Indic : constant Node_Id :=
15643 Subtype_Indication (Parent (Priv_T));
15645 Priv_Constr : constant Boolean :=
15646 Is_Constrained (Priv_Parent)
15647 or else
15648 Nkind (Priv_Indic) = N_Subtype_Indication
15649 or else Is_Constrained (Entity (Priv_Indic));
15651 Full_Constr : constant Boolean :=
15652 Is_Constrained (Full_Parent)
15653 or else
15654 Nkind (Full_Indic) = N_Subtype_Indication
15655 or else Is_Constrained (Entity (Full_Indic));
15657 Priv_Discr : Entity_Id;
15658 Full_Discr : Entity_Id;
15660 begin
15661 Priv_Discr := First_Discriminant (Priv_Parent);
15662 Full_Discr := First_Discriminant (Full_Parent);
15663 while Present (Priv_Discr) and then Present (Full_Discr) loop
15664 if Original_Record_Component (Priv_Discr) =
15665 Original_Record_Component (Full_Discr)
15666 or else
15667 Corresponding_Discriminant (Priv_Discr) =
15668 Corresponding_Discriminant (Full_Discr)
15669 then
15670 null;
15671 else
15672 exit;
15673 end if;
15675 Next_Discriminant (Priv_Discr);
15676 Next_Discriminant (Full_Discr);
15677 end loop;
15679 if Present (Priv_Discr) or else Present (Full_Discr) then
15680 Error_Msg_N
15681 ("full view must inherit discriminants of the parent type"
15682 & " used in the private extension", Full_Indic);
15684 elsif Priv_Constr and then not Full_Constr then
15685 Error_Msg_N
15686 ("parent subtype of full type must be constrained",
15687 Full_Indic);
15689 elsif Full_Constr and then not Priv_Constr then
15690 Error_Msg_N
15691 ("parent subtype of full type must be unconstrained",
15692 Full_Indic);
15693 end if;
15694 end;
15696 -- Check the rules of 7.3(12): if a partial view has neither known
15697 -- or unknown discriminants, then the full type declaration shall
15698 -- define a definite subtype.
15700 elsif not Has_Unknown_Discriminants (Priv_T)
15701 and then not Has_Discriminants (Priv_T)
15702 and then not Is_Constrained (Full_T)
15703 then
15704 Error_Msg_N
15705 ("full view must define a constrained type if partial view"
15706 & " has no discriminants", Full_T);
15707 end if;
15709 -- ??????? Do we implement the following properly ?????
15710 -- If the ancestor subtype of a private extension has constrained
15711 -- discriminants, then the parent subtype of the full view shall
15712 -- impose a statically matching constraint on those discriminants
15713 -- [7.3(13)].
15715 else
15716 -- For untagged types, verify that a type without discriminants
15717 -- is not completed with an unconstrained type.
15719 if not Is_Indefinite_Subtype (Priv_T)
15720 and then Is_Indefinite_Subtype (Full_T)
15721 then
15722 Error_Msg_N ("full view of type must be definite subtype", Full_T);
15723 end if;
15724 end if;
15726 -- AI-419: verify that the use of "limited" is consistent
15728 declare
15729 Orig_Decl : constant Node_Id := Original_Node (N);
15731 begin
15732 if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
15733 and then not Limited_Present (Parent (Priv_T))
15734 and then not Synchronized_Present (Parent (Priv_T))
15735 and then Nkind (Orig_Decl) = N_Full_Type_Declaration
15736 and then Nkind
15737 (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
15738 and then Limited_Present (Type_Definition (Orig_Decl))
15739 then
15740 Error_Msg_N
15741 ("full view of non-limited extension cannot be limited", N);
15742 end if;
15743 end;
15745 -- Ada 2005 (AI-443): A synchronized private extension must be
15746 -- completed by a task or protected type.
15748 if Ada_Version >= Ada_05
15749 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
15750 and then Synchronized_Present (Parent (Priv_T))
15751 and then not Is_Concurrent_Type (Full_T)
15752 then
15753 Error_Msg_N ("full view of synchronized extension must " &
15754 "be synchronized type", N);
15755 end if;
15757 -- Ada 2005 AI-363: if the full view has discriminants with
15758 -- defaults, it is illegal to declare constrained access subtypes
15759 -- whose designated type is the current type. This allows objects
15760 -- of the type that are declared in the heap to be unconstrained.
15762 if not Has_Unknown_Discriminants (Priv_T)
15763 and then not Has_Discriminants (Priv_T)
15764 and then Has_Discriminants (Full_T)
15765 and then
15766 Present (Discriminant_Default_Value (First_Discriminant (Full_T)))
15767 then
15768 Set_Has_Constrained_Partial_View (Full_T);
15769 Set_Has_Constrained_Partial_View (Priv_T);
15770 end if;
15772 -- Create a full declaration for all its subtypes recorded in
15773 -- Private_Dependents and swap them similarly to the base type. These
15774 -- are subtypes that have been define before the full declaration of
15775 -- the private type. We also swap the entry in Private_Dependents list
15776 -- so we can properly restore the private view on exit from the scope.
15778 declare
15779 Priv_Elmt : Elmt_Id;
15780 Priv : Entity_Id;
15781 Full : Entity_Id;
15783 begin
15784 Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
15785 while Present (Priv_Elmt) loop
15786 Priv := Node (Priv_Elmt);
15788 if Ekind (Priv) = E_Private_Subtype
15789 or else Ekind (Priv) = E_Limited_Private_Subtype
15790 or else Ekind (Priv) = E_Record_Subtype_With_Private
15791 then
15792 Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
15793 Set_Is_Itype (Full);
15794 Set_Parent (Full, Parent (Priv));
15795 Set_Associated_Node_For_Itype (Full, N);
15797 -- Now we need to complete the private subtype, but since the
15798 -- base type has already been swapped, we must also swap the
15799 -- subtypes (and thus, reverse the arguments in the call to
15800 -- Complete_Private_Subtype).
15802 Copy_And_Swap (Priv, Full);
15803 Complete_Private_Subtype (Full, Priv, Full_T, N);
15804 Replace_Elmt (Priv_Elmt, Full);
15805 end if;
15807 Next_Elmt (Priv_Elmt);
15808 end loop;
15809 end;
15811 -- If the private view was tagged, copy the new primitive operations
15812 -- from the private view to the full view.
15814 -- Note: Subprograms covering interface primitives were previously
15815 -- propagated to the full view by Derive_Progenitor_Primitives
15817 if Is_Tagged_Type (Full_T)
15818 and then not Is_Concurrent_Type (Full_T)
15819 then
15820 declare
15821 Priv_List : Elist_Id;
15822 Full_List : constant Elist_Id := Primitive_Operations (Full_T);
15823 P1, P2 : Elmt_Id;
15824 Prim : Entity_Id;
15825 D_Type : Entity_Id;
15827 begin
15828 if Is_Tagged_Type (Priv_T) then
15829 Priv_List := Primitive_Operations (Priv_T);
15831 P1 := First_Elmt (Priv_List);
15832 while Present (P1) loop
15833 Prim := Node (P1);
15835 -- Transfer explicit primitives, not those inherited from
15836 -- parent of partial view, which will be re-inherited on
15837 -- the full view.
15839 if Comes_From_Source (Prim) then
15840 P2 := First_Elmt (Full_List);
15841 while Present (P2) and then Node (P2) /= Prim loop
15842 Next_Elmt (P2);
15843 end loop;
15845 -- If not found, that is a new one
15847 if No (P2) then
15848 Append_Elmt (Prim, Full_List);
15849 end if;
15850 end if;
15852 Next_Elmt (P1);
15853 end loop;
15855 else
15856 -- In this case the partial view is untagged, so here we locate
15857 -- all of the earlier primitives that need to be treated as
15858 -- dispatching (those that appear between the two views). Note
15859 -- that these additional operations must all be new operations
15860 -- (any earlier operations that override inherited operations
15861 -- of the full view will already have been inserted in the
15862 -- primitives list, marked by Check_Operation_From_Private_View
15863 -- as dispatching. Note that implicit "/=" operators are
15864 -- excluded from being added to the primitives list since they
15865 -- shouldn't be treated as dispatching (tagged "/=" is handled
15866 -- specially).
15868 Prim := Next_Entity (Full_T);
15869 while Present (Prim) and then Prim /= Priv_T loop
15870 if Ekind (Prim) = E_Procedure
15871 or else
15872 Ekind (Prim) = E_Function
15873 then
15875 D_Type := Find_Dispatching_Type (Prim);
15877 if D_Type = Full_T
15878 and then (Chars (Prim) /= Name_Op_Ne
15879 or else Comes_From_Source (Prim))
15880 then
15881 Check_Controlling_Formals (Full_T, Prim);
15883 if not Is_Dispatching_Operation (Prim) then
15884 Append_Elmt (Prim, Full_List);
15885 Set_Is_Dispatching_Operation (Prim, True);
15886 Set_DT_Position (Prim, No_Uint);
15887 end if;
15889 elsif Is_Dispatching_Operation (Prim)
15890 and then D_Type /= Full_T
15891 then
15893 -- Verify that it is not otherwise controlled by a
15894 -- formal or a return value of type T.
15896 Check_Controlling_Formals (D_Type, Prim);
15897 end if;
15898 end if;
15900 Next_Entity (Prim);
15901 end loop;
15902 end if;
15904 -- For the tagged case, the two views can share the same
15905 -- Primitive Operation list and the same class wide type.
15906 -- Update attributes of the class-wide type which depend on
15907 -- the full declaration.
15909 if Is_Tagged_Type (Priv_T) then
15910 Set_Primitive_Operations (Priv_T, Full_List);
15911 Set_Class_Wide_Type
15912 (Base_Type (Full_T), Class_Wide_Type (Priv_T));
15914 Set_Has_Task (Class_Wide_Type (Priv_T), Has_Task (Full_T));
15915 end if;
15916 end;
15917 end if;
15919 -- Ada 2005 AI 161: Check preelaboratable initialization consistency
15921 if Known_To_Have_Preelab_Init (Priv_T) then
15923 -- Case where there is a pragma Preelaborable_Initialization. We
15924 -- always allow this in predefined units, which is a bit of a kludge,
15925 -- but it means we don't have to struggle to meet the requirements in
15926 -- the RM for having Preelaborable Initialization. Otherwise we
15927 -- require that the type meets the RM rules. But we can't check that
15928 -- yet, because of the rule about overriding Ininitialize, so we
15929 -- simply set a flag that will be checked at freeze time.
15931 if not In_Predefined_Unit (Full_T) then
15932 Set_Must_Have_Preelab_Init (Full_T);
15933 end if;
15934 end if;
15936 -- If pragma CPP_Class was applied to the private type declaration,
15937 -- propagate it now to the full type declaration.
15939 if Is_CPP_Class (Priv_T) then
15940 Set_Is_CPP_Class (Full_T);
15941 Set_Convention (Full_T, Convention_CPP);
15942 end if;
15943 end Process_Full_View;
15945 -----------------------------------
15946 -- Process_Incomplete_Dependents --
15947 -----------------------------------
15949 procedure Process_Incomplete_Dependents
15950 (N : Node_Id;
15951 Full_T : Entity_Id;
15952 Inc_T : Entity_Id)
15954 Inc_Elmt : Elmt_Id;
15955 Priv_Dep : Entity_Id;
15956 New_Subt : Entity_Id;
15958 Disc_Constraint : Elist_Id;
15960 begin
15961 if No (Private_Dependents (Inc_T)) then
15962 return;
15963 end if;
15965 -- Itypes that may be generated by the completion of an incomplete
15966 -- subtype are not used by the back-end and not attached to the tree.
15967 -- They are created only for constraint-checking purposes.
15969 Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
15970 while Present (Inc_Elmt) loop
15971 Priv_Dep := Node (Inc_Elmt);
15973 if Ekind (Priv_Dep) = E_Subprogram_Type then
15975 -- An Access_To_Subprogram type may have a return type or a
15976 -- parameter type that is incomplete. Replace with the full view.
15978 if Etype (Priv_Dep) = Inc_T then
15979 Set_Etype (Priv_Dep, Full_T);
15980 end if;
15982 declare
15983 Formal : Entity_Id;
15985 begin
15986 Formal := First_Formal (Priv_Dep);
15987 while Present (Formal) loop
15988 if Etype (Formal) = Inc_T then
15989 Set_Etype (Formal, Full_T);
15990 end if;
15992 Next_Formal (Formal);
15993 end loop;
15994 end;
15996 elsif Is_Overloadable (Priv_Dep) then
15998 -- A protected operation is never dispatching: only its
15999 -- wrapper operation (which has convention Ada) is.
16001 if Is_Tagged_Type (Full_T)
16002 and then Convention (Priv_Dep) /= Convention_Protected
16003 then
16005 -- Subprogram has an access parameter whose designated type
16006 -- was incomplete. Reexamine declaration now, because it may
16007 -- be a primitive operation of the full type.
16009 Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
16010 Set_Is_Dispatching_Operation (Priv_Dep);
16011 Check_Controlling_Formals (Full_T, Priv_Dep);
16012 end if;
16014 elsif Ekind (Priv_Dep) = E_Subprogram_Body then
16016 -- Can happen during processing of a body before the completion
16017 -- of a TA type. Ignore, because spec is also on dependent list.
16019 return;
16021 -- Ada 2005 (AI-412): Transform a regular incomplete subtype into a
16022 -- corresponding subtype of the full view.
16024 elsif Ekind (Priv_Dep) = E_Incomplete_Subtype then
16025 Set_Subtype_Indication
16026 (Parent (Priv_Dep), New_Reference_To (Full_T, Sloc (Priv_Dep)));
16027 Set_Etype (Priv_Dep, Full_T);
16028 Set_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
16029 Set_Analyzed (Parent (Priv_Dep), False);
16031 -- Reanalyze the declaration, suppressing the call to
16032 -- Enter_Name to avoid duplicate names.
16034 Analyze_Subtype_Declaration
16035 (N => Parent (Priv_Dep),
16036 Skip => True);
16038 -- Dependent is a subtype
16040 else
16041 -- We build a new subtype indication using the full view of the
16042 -- incomplete parent. The discriminant constraints have been
16043 -- elaborated already at the point of the subtype declaration.
16045 New_Subt := Create_Itype (E_Void, N);
16047 if Has_Discriminants (Full_T) then
16048 Disc_Constraint := Discriminant_Constraint (Priv_Dep);
16049 else
16050 Disc_Constraint := No_Elist;
16051 end if;
16053 Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
16054 Set_Full_View (Priv_Dep, New_Subt);
16055 end if;
16057 Next_Elmt (Inc_Elmt);
16058 end loop;
16059 end Process_Incomplete_Dependents;
16061 --------------------------------
16062 -- Process_Range_Expr_In_Decl --
16063 --------------------------------
16065 procedure Process_Range_Expr_In_Decl
16066 (R : Node_Id;
16067 T : Entity_Id;
16068 Check_List : List_Id := Empty_List;
16069 R_Check_Off : Boolean := False)
16071 Lo, Hi : Node_Id;
16072 R_Checks : Check_Result;
16073 Type_Decl : Node_Id;
16074 Def_Id : Entity_Id;
16076 begin
16077 Analyze_And_Resolve (R, Base_Type (T));
16079 if Nkind (R) = N_Range then
16080 Lo := Low_Bound (R);
16081 Hi := High_Bound (R);
16083 -- We need to ensure validity of the bounds here, because if we
16084 -- go ahead and do the expansion, then the expanded code will get
16085 -- analyzed with range checks suppressed and we miss the check.
16087 Validity_Check_Range (R);
16089 -- If there were errors in the declaration, try and patch up some
16090 -- common mistakes in the bounds. The cases handled are literals
16091 -- which are Integer where the expected type is Real and vice versa.
16092 -- These corrections allow the compilation process to proceed further
16093 -- along since some basic assumptions of the format of the bounds
16094 -- are guaranteed.
16096 if Etype (R) = Any_Type then
16098 if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
16099 Rewrite (Lo,
16100 Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
16102 elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
16103 Rewrite (Hi,
16104 Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
16106 elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
16107 Rewrite (Lo,
16108 Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
16110 elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
16111 Rewrite (Hi,
16112 Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
16113 end if;
16115 Set_Etype (Lo, T);
16116 Set_Etype (Hi, T);
16117 end if;
16119 -- If the bounds of the range have been mistakenly given as string
16120 -- literals (perhaps in place of character literals), then an error
16121 -- has already been reported, but we rewrite the string literal as a
16122 -- bound of the range's type to avoid blowups in later processing
16123 -- that looks at static values.
16125 if Nkind (Lo) = N_String_Literal then
16126 Rewrite (Lo,
16127 Make_Attribute_Reference (Sloc (Lo),
16128 Attribute_Name => Name_First,
16129 Prefix => New_Reference_To (T, Sloc (Lo))));
16130 Analyze_And_Resolve (Lo);
16131 end if;
16133 if Nkind (Hi) = N_String_Literal then
16134 Rewrite (Hi,
16135 Make_Attribute_Reference (Sloc (Hi),
16136 Attribute_Name => Name_First,
16137 Prefix => New_Reference_To (T, Sloc (Hi))));
16138 Analyze_And_Resolve (Hi);
16139 end if;
16141 -- If bounds aren't scalar at this point then exit, avoiding
16142 -- problems with further processing of the range in this procedure.
16144 if not Is_Scalar_Type (Etype (Lo)) then
16145 return;
16146 end if;
16148 -- Resolve (actually Sem_Eval) has checked that the bounds are in
16149 -- then range of the base type. Here we check whether the bounds
16150 -- are in the range of the subtype itself. Note that if the bounds
16151 -- represent the null range the Constraint_Error exception should
16152 -- not be raised.
16154 -- ??? The following code should be cleaned up as follows
16156 -- 1. The Is_Null_Range (Lo, Hi) test should disappear since it
16157 -- is done in the call to Range_Check (R, T); below
16159 -- 2. The use of R_Check_Off should be investigated and possibly
16160 -- removed, this would clean up things a bit.
16162 if Is_Null_Range (Lo, Hi) then
16163 null;
16165 else
16166 -- Capture values of bounds and generate temporaries for them
16167 -- if needed, before applying checks, since checks may cause
16168 -- duplication of the expression without forcing evaluation.
16170 if Expander_Active then
16171 Force_Evaluation (Lo);
16172 Force_Evaluation (Hi);
16173 end if;
16175 -- We use a flag here instead of suppressing checks on the
16176 -- type because the type we check against isn't necessarily
16177 -- the place where we put the check.
16179 if not R_Check_Off then
16180 R_Checks := Get_Range_Checks (R, T);
16182 -- Look up tree to find an appropriate insertion point.
16183 -- This seems really junk code, and very brittle, couldn't
16184 -- we just use an insert actions call of some kind ???
16186 Type_Decl := Parent (R);
16187 while Present (Type_Decl) and then not
16188 (Nkind_In (Type_Decl, N_Full_Type_Declaration,
16189 N_Subtype_Declaration,
16190 N_Loop_Statement,
16191 N_Task_Type_Declaration)
16192 or else
16193 Nkind_In (Type_Decl, N_Single_Task_Declaration,
16194 N_Protected_Type_Declaration,
16195 N_Single_Protected_Declaration))
16196 loop
16197 Type_Decl := Parent (Type_Decl);
16198 end loop;
16200 -- Why would Type_Decl not be present??? Without this test,
16201 -- short regression tests fail.
16203 if Present (Type_Decl) then
16205 -- Case of loop statement (more comments ???)
16207 if Nkind (Type_Decl) = N_Loop_Statement then
16208 declare
16209 Indic : Node_Id;
16211 begin
16212 Indic := Parent (R);
16213 while Present (Indic)
16214 and then Nkind (Indic) /= N_Subtype_Indication
16215 loop
16216 Indic := Parent (Indic);
16217 end loop;
16219 if Present (Indic) then
16220 Def_Id := Etype (Subtype_Mark (Indic));
16222 Insert_Range_Checks
16223 (R_Checks,
16224 Type_Decl,
16225 Def_Id,
16226 Sloc (Type_Decl),
16228 Do_Before => True);
16229 end if;
16230 end;
16232 -- All other cases (more comments ???)
16234 else
16235 Def_Id := Defining_Identifier (Type_Decl);
16237 if (Ekind (Def_Id) = E_Record_Type
16238 and then Depends_On_Discriminant (R))
16239 or else
16240 (Ekind (Def_Id) = E_Protected_Type
16241 and then Has_Discriminants (Def_Id))
16242 then
16243 Append_Range_Checks
16244 (R_Checks, Check_List, Def_Id, Sloc (Type_Decl), R);
16246 else
16247 Insert_Range_Checks
16248 (R_Checks, Type_Decl, Def_Id, Sloc (Type_Decl), R);
16250 end if;
16251 end if;
16252 end if;
16253 end if;
16254 end if;
16256 elsif Expander_Active then
16257 Get_Index_Bounds (R, Lo, Hi);
16258 Force_Evaluation (Lo);
16259 Force_Evaluation (Hi);
16260 end if;
16261 end Process_Range_Expr_In_Decl;
16263 --------------------------------------
16264 -- Process_Real_Range_Specification --
16265 --------------------------------------
16267 procedure Process_Real_Range_Specification (Def : Node_Id) is
16268 Spec : constant Node_Id := Real_Range_Specification (Def);
16269 Lo : Node_Id;
16270 Hi : Node_Id;
16271 Err : Boolean := False;
16273 procedure Analyze_Bound (N : Node_Id);
16274 -- Analyze and check one bound
16276 -------------------
16277 -- Analyze_Bound --
16278 -------------------
16280 procedure Analyze_Bound (N : Node_Id) is
16281 begin
16282 Analyze_And_Resolve (N, Any_Real);
16284 if not Is_OK_Static_Expression (N) then
16285 Flag_Non_Static_Expr
16286 ("bound in real type definition is not static!", N);
16287 Err := True;
16288 end if;
16289 end Analyze_Bound;
16291 -- Start of processing for Process_Real_Range_Specification
16293 begin
16294 if Present (Spec) then
16295 Lo := Low_Bound (Spec);
16296 Hi := High_Bound (Spec);
16297 Analyze_Bound (Lo);
16298 Analyze_Bound (Hi);
16300 -- If error, clear away junk range specification
16302 if Err then
16303 Set_Real_Range_Specification (Def, Empty);
16304 end if;
16305 end if;
16306 end Process_Real_Range_Specification;
16308 ---------------------
16309 -- Process_Subtype --
16310 ---------------------
16312 function Process_Subtype
16313 (S : Node_Id;
16314 Related_Nod : Node_Id;
16315 Related_Id : Entity_Id := Empty;
16316 Suffix : Character := ' ') return Entity_Id
16318 P : Node_Id;
16319 Def_Id : Entity_Id;
16320 Error_Node : Node_Id;
16321 Full_View_Id : Entity_Id;
16322 Subtype_Mark_Id : Entity_Id;
16324 May_Have_Null_Exclusion : Boolean;
16326 procedure Check_Incomplete (T : Entity_Id);
16327 -- Called to verify that an incomplete type is not used prematurely
16329 ----------------------
16330 -- Check_Incomplete --
16331 ----------------------
16333 procedure Check_Incomplete (T : Entity_Id) is
16334 begin
16335 -- Ada 2005 (AI-412): Incomplete subtypes are legal
16337 if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
16338 and then
16339 not (Ada_Version >= Ada_05
16340 and then
16341 (Nkind (Parent (T)) = N_Subtype_Declaration
16342 or else
16343 (Nkind (Parent (T)) = N_Subtype_Indication
16344 and then Nkind (Parent (Parent (T))) =
16345 N_Subtype_Declaration)))
16346 then
16347 Error_Msg_N ("invalid use of type before its full declaration", T);
16348 end if;
16349 end Check_Incomplete;
16351 -- Start of processing for Process_Subtype
16353 begin
16354 -- Case of no constraints present
16356 if Nkind (S) /= N_Subtype_Indication then
16357 Find_Type (S);
16358 Check_Incomplete (S);
16359 P := Parent (S);
16361 -- Ada 2005 (AI-231): Static check
16363 if Ada_Version >= Ada_05
16364 and then Present (P)
16365 and then Null_Exclusion_Present (P)
16366 and then Nkind (P) /= N_Access_To_Object_Definition
16367 and then not Is_Access_Type (Entity (S))
16368 then
16369 Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
16370 end if;
16372 -- The following is ugly, can't we have a range or even a flag???
16374 May_Have_Null_Exclusion :=
16375 Nkind_In (P, N_Access_Definition,
16376 N_Access_Function_Definition,
16377 N_Access_Procedure_Definition,
16378 N_Access_To_Object_Definition,
16379 N_Allocator,
16380 N_Component_Definition)
16381 or else
16382 Nkind_In (P, N_Derived_Type_Definition,
16383 N_Discriminant_Specification,
16384 N_Object_Declaration,
16385 N_Parameter_Specification,
16386 N_Subtype_Declaration);
16388 -- Create an Itype that is a duplicate of Entity (S) but with the
16389 -- null-exclusion attribute
16391 if May_Have_Null_Exclusion
16392 and then Is_Access_Type (Entity (S))
16393 and then Null_Exclusion_Present (P)
16395 -- No need to check the case of an access to object definition.
16396 -- It is correct to define double not-null pointers.
16398 -- Example:
16399 -- type Not_Null_Int_Ptr is not null access Integer;
16400 -- type Acc is not null access Not_Null_Int_Ptr;
16402 and then Nkind (P) /= N_Access_To_Object_Definition
16403 then
16404 if Can_Never_Be_Null (Entity (S)) then
16405 case Nkind (Related_Nod) is
16406 when N_Full_Type_Declaration =>
16407 if Nkind (Type_Definition (Related_Nod))
16408 in N_Array_Type_Definition
16409 then
16410 Error_Node :=
16411 Subtype_Indication
16412 (Component_Definition
16413 (Type_Definition (Related_Nod)));
16414 else
16415 Error_Node :=
16416 Subtype_Indication (Type_Definition (Related_Nod));
16417 end if;
16419 when N_Subtype_Declaration =>
16420 Error_Node := Subtype_Indication (Related_Nod);
16422 when N_Object_Declaration =>
16423 Error_Node := Object_Definition (Related_Nod);
16425 when N_Component_Declaration =>
16426 Error_Node :=
16427 Subtype_Indication (Component_Definition (Related_Nod));
16429 when others =>
16430 pragma Assert (False);
16431 Error_Node := Related_Nod;
16432 end case;
16434 Error_Msg_NE
16435 ("`NOT NULL` not allowed (& already excludes null)",
16436 Error_Node,
16437 Entity (S));
16438 end if;
16440 Set_Etype (S,
16441 Create_Null_Excluding_Itype
16442 (T => Entity (S),
16443 Related_Nod => P));
16444 Set_Entity (S, Etype (S));
16445 end if;
16447 return Entity (S);
16449 -- Case of constraint present, so that we have an N_Subtype_Indication
16450 -- node (this node is created only if constraints are present).
16452 else
16453 Find_Type (Subtype_Mark (S));
16455 if Nkind (Parent (S)) /= N_Access_To_Object_Definition
16456 and then not
16457 (Nkind (Parent (S)) = N_Subtype_Declaration
16458 and then Is_Itype (Defining_Identifier (Parent (S))))
16459 then
16460 Check_Incomplete (Subtype_Mark (S));
16461 end if;
16463 P := Parent (S);
16464 Subtype_Mark_Id := Entity (Subtype_Mark (S));
16466 -- Explicit subtype declaration case
16468 if Nkind (P) = N_Subtype_Declaration then
16469 Def_Id := Defining_Identifier (P);
16471 -- Explicit derived type definition case
16473 elsif Nkind (P) = N_Derived_Type_Definition then
16474 Def_Id := Defining_Identifier (Parent (P));
16476 -- Implicit case, the Def_Id must be created as an implicit type.
16477 -- The one exception arises in the case of concurrent types, array
16478 -- and access types, where other subsidiary implicit types may be
16479 -- created and must appear before the main implicit type. In these
16480 -- cases we leave Def_Id set to Empty as a signal that Create_Itype
16481 -- has not yet been called to create Def_Id.
16483 else
16484 if Is_Array_Type (Subtype_Mark_Id)
16485 or else Is_Concurrent_Type (Subtype_Mark_Id)
16486 or else Is_Access_Type (Subtype_Mark_Id)
16487 then
16488 Def_Id := Empty;
16490 -- For the other cases, we create a new unattached Itype,
16491 -- and set the indication to ensure it gets attached later.
16493 else
16494 Def_Id :=
16495 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
16496 end if;
16497 end if;
16499 -- If the kind of constraint is invalid for this kind of type,
16500 -- then give an error, and then pretend no constraint was given.
16502 if not Is_Valid_Constraint_Kind
16503 (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
16504 then
16505 Error_Msg_N
16506 ("incorrect constraint for this kind of type", Constraint (S));
16508 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
16510 -- Set Ekind of orphan itype, to prevent cascaded errors
16512 if Present (Def_Id) then
16513 Set_Ekind (Def_Id, Ekind (Any_Type));
16514 end if;
16516 -- Make recursive call, having got rid of the bogus constraint
16518 return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
16519 end if;
16521 -- Remaining processing depends on type
16523 case Ekind (Subtype_Mark_Id) is
16524 when Access_Kind =>
16525 Constrain_Access (Def_Id, S, Related_Nod);
16527 if Expander_Active
16528 and then Is_Itype (Designated_Type (Def_Id))
16529 and then Nkind (Related_Nod) = N_Subtype_Declaration
16530 and then not Is_Incomplete_Type (Designated_Type (Def_Id))
16531 then
16532 Build_Itype_Reference
16533 (Designated_Type (Def_Id), Related_Nod);
16534 end if;
16536 when Array_Kind =>
16537 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
16539 when Decimal_Fixed_Point_Kind =>
16540 Constrain_Decimal (Def_Id, S);
16542 when Enumeration_Kind =>
16543 Constrain_Enumeration (Def_Id, S);
16545 when Ordinary_Fixed_Point_Kind =>
16546 Constrain_Ordinary_Fixed (Def_Id, S);
16548 when Float_Kind =>
16549 Constrain_Float (Def_Id, S);
16551 when Integer_Kind =>
16552 Constrain_Integer (Def_Id, S);
16554 when E_Record_Type |
16555 E_Record_Subtype |
16556 Class_Wide_Kind |
16557 E_Incomplete_Type =>
16558 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
16560 when Private_Kind =>
16561 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
16562 Set_Private_Dependents (Def_Id, New_Elmt_List);
16564 -- In case of an invalid constraint prevent further processing
16565 -- since the type constructed is missing expected fields.
16567 if Etype (Def_Id) = Any_Type then
16568 return Def_Id;
16569 end if;
16571 -- If the full view is that of a task with discriminants,
16572 -- we must constrain both the concurrent type and its
16573 -- corresponding record type. Otherwise we will just propagate
16574 -- the constraint to the full view, if available.
16576 if Present (Full_View (Subtype_Mark_Id))
16577 and then Has_Discriminants (Subtype_Mark_Id)
16578 and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
16579 then
16580 Full_View_Id :=
16581 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
16583 Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
16584 Constrain_Concurrent (Full_View_Id, S,
16585 Related_Nod, Related_Id, Suffix);
16586 Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
16587 Set_Full_View (Def_Id, Full_View_Id);
16589 -- Introduce an explicit reference to the private subtype,
16590 -- to prevent scope anomalies in gigi if first use appears
16591 -- in a nested context, e.g. a later function body.
16592 -- Should this be generated in other contexts than a full
16593 -- type declaration?
16595 if Is_Itype (Def_Id)
16596 and then
16597 Nkind (Parent (P)) = N_Full_Type_Declaration
16598 then
16599 Build_Itype_Reference (Def_Id, Parent (P));
16600 end if;
16602 else
16603 Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
16604 end if;
16606 when Concurrent_Kind =>
16607 Constrain_Concurrent (Def_Id, S,
16608 Related_Nod, Related_Id, Suffix);
16610 when others =>
16611 Error_Msg_N ("invalid subtype mark in subtype indication", S);
16612 end case;
16614 -- Size and Convention are always inherited from the base type
16616 Set_Size_Info (Def_Id, (Subtype_Mark_Id));
16617 Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
16619 return Def_Id;
16620 end if;
16621 end Process_Subtype;
16623 ---------------------------------------
16624 -- Check_Anonymous_Access_Components --
16625 ---------------------------------------
16627 procedure Check_Anonymous_Access_Components
16628 (Typ_Decl : Node_Id;
16629 Typ : Entity_Id;
16630 Prev : Entity_Id;
16631 Comp_List : Node_Id)
16633 Loc : constant Source_Ptr := Sloc (Typ_Decl);
16634 Anon_Access : Entity_Id;
16635 Acc_Def : Node_Id;
16636 Comp : Node_Id;
16637 Comp_Def : Node_Id;
16638 Decl : Node_Id;
16639 Type_Def : Node_Id;
16641 procedure Build_Incomplete_Type_Declaration;
16642 -- If the record type contains components that include an access to the
16643 -- current record, then create an incomplete type declaration for the
16644 -- record, to be used as the designated type of the anonymous access.
16645 -- This is done only once, and only if there is no previous partial
16646 -- view of the type.
16648 function Designates_T (Subt : Node_Id) return Boolean;
16649 -- Check whether a node designates the enclosing record type, or 'Class
16650 -- of that type
16652 function Mentions_T (Acc_Def : Node_Id) return Boolean;
16653 -- Check whether an access definition includes a reference to
16654 -- the enclosing record type. The reference can be a subtype mark
16655 -- in the access definition itself, a 'Class attribute reference, or
16656 -- recursively a reference appearing in a parameter specification
16657 -- or result definition of an access_to_subprogram definition.
16659 --------------------------------------
16660 -- Build_Incomplete_Type_Declaration --
16661 --------------------------------------
16663 procedure Build_Incomplete_Type_Declaration is
16664 Decl : Node_Id;
16665 Inc_T : Entity_Id;
16666 H : Entity_Id;
16668 -- Is_Tagged indicates whether the type is tagged. It is tagged if
16669 -- it's "is new ... with record" or else "is tagged record ...".
16671 Is_Tagged : constant Boolean :=
16672 (Nkind (Type_Definition (Typ_Decl)) = N_Derived_Type_Definition
16673 and then
16674 Present
16675 (Record_Extension_Part (Type_Definition (Typ_Decl))))
16676 or else
16677 (Nkind (Type_Definition (Typ_Decl)) = N_Record_Definition
16678 and then Tagged_Present (Type_Definition (Typ_Decl)));
16680 begin
16681 -- If there is a previous partial view, no need to create a new one
16682 -- If the partial view, given by Prev, is incomplete, If Prev is
16683 -- a private declaration, full declaration is flagged accordingly.
16685 if Prev /= Typ then
16686 if Is_Tagged then
16687 Make_Class_Wide_Type (Prev);
16688 Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
16689 Set_Etype (Class_Wide_Type (Typ), Typ);
16690 end if;
16692 return;
16694 elsif Has_Private_Declaration (Typ) then
16696 -- If we refer to T'Class inside T, and T is the completion of a
16697 -- private type, then we need to make sure the class-wide type
16698 -- exists.
16700 if Is_Tagged then
16701 Make_Class_Wide_Type (Typ);
16702 end if;
16704 return;
16706 -- If there was a previous anonymous access type, the incomplete
16707 -- type declaration will have been created already.
16709 elsif Present (Current_Entity (Typ))
16710 and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
16711 and then Full_View (Current_Entity (Typ)) = Typ
16712 then
16713 return;
16715 else
16716 Inc_T := Make_Defining_Identifier (Loc, Chars (Typ));
16717 Decl := Make_Incomplete_Type_Declaration (Loc, Inc_T);
16719 -- Type has already been inserted into the current scope.
16720 -- Remove it, and add incomplete declaration for type, so
16721 -- that subsequent anonymous access types can use it.
16722 -- The entity is unchained from the homonym list and from
16723 -- immediate visibility. After analysis, the entity in the
16724 -- incomplete declaration becomes immediately visible in the
16725 -- record declaration that follows.
16727 H := Current_Entity (Typ);
16729 if H = Typ then
16730 Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
16731 else
16732 while Present (H)
16733 and then Homonym (H) /= Typ
16734 loop
16735 H := Homonym (Typ);
16736 end loop;
16738 Set_Homonym (H, Homonym (Typ));
16739 end if;
16741 Insert_Before (Typ_Decl, Decl);
16742 Analyze (Decl);
16743 Set_Full_View (Inc_T, Typ);
16745 if Is_Tagged then
16746 -- Create a common class-wide type for both views, and set
16747 -- the Etype of the class-wide type to the full view.
16749 Make_Class_Wide_Type (Inc_T);
16750 Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
16751 Set_Etype (Class_Wide_Type (Typ), Typ);
16752 end if;
16753 end if;
16754 end Build_Incomplete_Type_Declaration;
16756 ------------------
16757 -- Designates_T --
16758 ------------------
16760 function Designates_T (Subt : Node_Id) return Boolean is
16761 Type_Id : constant Name_Id := Chars (Typ);
16763 function Names_T (Nam : Node_Id) return Boolean;
16764 -- The record type has not been introduced in the current scope
16765 -- yet, so we must examine the name of the type itself, either
16766 -- an identifier T, or an expanded name of the form P.T, where
16767 -- P denotes the current scope.
16769 -------------
16770 -- Names_T --
16771 -------------
16773 function Names_T (Nam : Node_Id) return Boolean is
16774 begin
16775 if Nkind (Nam) = N_Identifier then
16776 return Chars (Nam) = Type_Id;
16778 elsif Nkind (Nam) = N_Selected_Component then
16779 if Chars (Selector_Name (Nam)) = Type_Id then
16780 if Nkind (Prefix (Nam)) = N_Identifier then
16781 return Chars (Prefix (Nam)) = Chars (Current_Scope);
16783 elsif Nkind (Prefix (Nam)) = N_Selected_Component then
16784 return Chars (Selector_Name (Prefix (Nam))) =
16785 Chars (Current_Scope);
16786 else
16787 return False;
16788 end if;
16790 else
16791 return False;
16792 end if;
16794 else
16795 return False;
16796 end if;
16797 end Names_T;
16799 -- Start of processing for Designates_T
16801 begin
16802 if Nkind (Subt) = N_Identifier then
16803 return Chars (Subt) = Type_Id;
16805 -- Reference can be through an expanded name which has not been
16806 -- analyzed yet, and which designates enclosing scopes.
16808 elsif Nkind (Subt) = N_Selected_Component then
16809 if Names_T (Subt) then
16810 return True;
16812 -- Otherwise it must denote an entity that is already visible.
16813 -- The access definition may name a subtype of the enclosing
16814 -- type, if there is a previous incomplete declaration for it.
16816 else
16817 Find_Selected_Component (Subt);
16818 return
16819 Is_Entity_Name (Subt)
16820 and then Scope (Entity (Subt)) = Current_Scope
16821 and then
16822 (Chars (Base_Type (Entity (Subt))) = Type_Id
16823 or else
16824 (Is_Class_Wide_Type (Entity (Subt))
16825 and then
16826 Chars (Etype (Base_Type (Entity (Subt)))) =
16827 Type_Id));
16828 end if;
16830 -- A reference to the current type may appear as the prefix of
16831 -- a 'Class attribute.
16833 elsif Nkind (Subt) = N_Attribute_Reference
16834 and then Attribute_Name (Subt) = Name_Class
16835 then
16836 return Names_T (Prefix (Subt));
16838 else
16839 return False;
16840 end if;
16841 end Designates_T;
16843 ----------------
16844 -- Mentions_T --
16845 ----------------
16847 function Mentions_T (Acc_Def : Node_Id) return Boolean is
16848 Param_Spec : Node_Id;
16850 Acc_Subprg : constant Node_Id :=
16851 Access_To_Subprogram_Definition (Acc_Def);
16853 begin
16854 if No (Acc_Subprg) then
16855 return Designates_T (Subtype_Mark (Acc_Def));
16856 end if;
16858 -- Component is an access_to_subprogram: examine its formals,
16859 -- and result definition in the case of an access_to_function.
16861 Param_Spec := First (Parameter_Specifications (Acc_Subprg));
16862 while Present (Param_Spec) loop
16863 if Nkind (Parameter_Type (Param_Spec)) = N_Access_Definition
16864 and then Mentions_T (Parameter_Type (Param_Spec))
16865 then
16866 return True;
16868 elsif Designates_T (Parameter_Type (Param_Spec)) then
16869 return True;
16870 end if;
16872 Next (Param_Spec);
16873 end loop;
16875 if Nkind (Acc_Subprg) = N_Access_Function_Definition then
16876 if Nkind (Result_Definition (Acc_Subprg)) =
16877 N_Access_Definition
16878 then
16879 return Mentions_T (Result_Definition (Acc_Subprg));
16880 else
16881 return Designates_T (Result_Definition (Acc_Subprg));
16882 end if;
16883 end if;
16885 return False;
16886 end Mentions_T;
16888 -- Start of processing for Check_Anonymous_Access_Components
16890 begin
16891 if No (Comp_List) then
16892 return;
16893 end if;
16895 Comp := First (Component_Items (Comp_List));
16896 while Present (Comp) loop
16897 if Nkind (Comp) = N_Component_Declaration
16898 and then Present
16899 (Access_Definition (Component_Definition (Comp)))
16900 and then
16901 Mentions_T (Access_Definition (Component_Definition (Comp)))
16902 then
16903 Comp_Def := Component_Definition (Comp);
16904 Acc_Def :=
16905 Access_To_Subprogram_Definition
16906 (Access_Definition (Comp_Def));
16908 Build_Incomplete_Type_Declaration;
16909 Anon_Access :=
16910 Make_Defining_Identifier (Loc,
16911 Chars => New_Internal_Name ('S'));
16913 -- Create a declaration for the anonymous access type: either
16914 -- an access_to_object or an access_to_subprogram.
16916 if Present (Acc_Def) then
16917 if Nkind (Acc_Def) = N_Access_Function_Definition then
16918 Type_Def :=
16919 Make_Access_Function_Definition (Loc,
16920 Parameter_Specifications =>
16921 Parameter_Specifications (Acc_Def),
16922 Result_Definition => Result_Definition (Acc_Def));
16923 else
16924 Type_Def :=
16925 Make_Access_Procedure_Definition (Loc,
16926 Parameter_Specifications =>
16927 Parameter_Specifications (Acc_Def));
16928 end if;
16930 else
16931 Type_Def :=
16932 Make_Access_To_Object_Definition (Loc,
16933 Subtype_Indication =>
16934 Relocate_Node
16935 (Subtype_Mark
16936 (Access_Definition (Comp_Def))));
16938 Set_Constant_Present
16939 (Type_Def, Constant_Present (Access_Definition (Comp_Def)));
16940 Set_All_Present
16941 (Type_Def, All_Present (Access_Definition (Comp_Def)));
16942 end if;
16944 Set_Null_Exclusion_Present
16945 (Type_Def,
16946 Null_Exclusion_Present (Access_Definition (Comp_Def)));
16948 Decl :=
16949 Make_Full_Type_Declaration (Loc,
16950 Defining_Identifier => Anon_Access,
16951 Type_Definition => Type_Def);
16953 Insert_Before (Typ_Decl, Decl);
16954 Analyze (Decl);
16956 -- If an access to object, Preserve entity of designated type,
16957 -- for ASIS use, before rewriting the component definition.
16959 if No (Acc_Def) then
16960 declare
16961 Desig : Entity_Id;
16963 begin
16964 Desig := Entity (Subtype_Indication (Type_Def));
16966 -- If the access definition is to the current record,
16967 -- the visible entity at this point is an incomplete
16968 -- type. Retrieve the full view to simplify ASIS queries
16970 if Ekind (Desig) = E_Incomplete_Type then
16971 Desig := Full_View (Desig);
16972 end if;
16974 Set_Entity
16975 (Subtype_Mark (Access_Definition (Comp_Def)), Desig);
16976 end;
16977 end if;
16979 Rewrite (Comp_Def,
16980 Make_Component_Definition (Loc,
16981 Subtype_Indication =>
16982 New_Occurrence_Of (Anon_Access, Loc)));
16984 if Ekind (Designated_Type (Anon_Access)) = E_Subprogram_Type then
16985 Set_Ekind (Anon_Access, E_Anonymous_Access_Subprogram_Type);
16986 else
16987 Set_Ekind (Anon_Access, E_Anonymous_Access_Type);
16988 end if;
16990 Set_Is_Local_Anonymous_Access (Anon_Access);
16991 end if;
16993 Next (Comp);
16994 end loop;
16996 if Present (Variant_Part (Comp_List)) then
16997 declare
16998 V : Node_Id;
16999 begin
17000 V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
17001 while Present (V) loop
17002 Check_Anonymous_Access_Components
17003 (Typ_Decl, Typ, Prev, Component_List (V));
17004 Next_Non_Pragma (V);
17005 end loop;
17006 end;
17007 end if;
17008 end Check_Anonymous_Access_Components;
17010 --------------------------------
17011 -- Preanalyze_Spec_Expression --
17012 --------------------------------
17014 procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
17015 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
17016 begin
17017 In_Spec_Expression := True;
17018 Preanalyze_And_Resolve (N, T);
17019 In_Spec_Expression := Save_In_Spec_Expression;
17020 end Preanalyze_Spec_Expression;
17022 -----------------------------
17023 -- Record_Type_Declaration --
17024 -----------------------------
17026 procedure Record_Type_Declaration
17027 (T : Entity_Id;
17028 N : Node_Id;
17029 Prev : Entity_Id)
17031 Def : constant Node_Id := Type_Definition (N);
17032 Is_Tagged : Boolean;
17033 Tag_Comp : Entity_Id;
17035 begin
17036 -- These flags must be initialized before calling Process_Discriminants
17037 -- because this routine makes use of them.
17039 Set_Ekind (T, E_Record_Type);
17040 Set_Etype (T, T);
17041 Init_Size_Align (T);
17042 Set_Interfaces (T, No_Elist);
17043 Set_Stored_Constraint (T, No_Elist);
17045 -- Normal case
17047 if Ada_Version < Ada_05
17048 or else not Interface_Present (Def)
17049 then
17050 -- The flag Is_Tagged_Type might have already been set by
17051 -- Find_Type_Name if it detected an error for declaration T. This
17052 -- arises in the case of private tagged types where the full view
17053 -- omits the word tagged.
17055 Is_Tagged :=
17056 Tagged_Present (Def)
17057 or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
17059 Set_Is_Tagged_Type (T, Is_Tagged);
17060 Set_Is_Limited_Record (T, Limited_Present (Def));
17062 -- Type is abstract if full declaration carries keyword, or if
17063 -- previous partial view did.
17065 Set_Is_Abstract_Type (T, Is_Abstract_Type (T)
17066 or else Abstract_Present (Def));
17068 else
17069 Is_Tagged := True;
17070 Analyze_Interface_Declaration (T, Def);
17072 if Present (Discriminant_Specifications (N)) then
17073 Error_Msg_N
17074 ("interface types cannot have discriminants",
17075 Defining_Identifier
17076 (First (Discriminant_Specifications (N))));
17077 end if;
17078 end if;
17080 -- First pass: if there are self-referential access components,
17081 -- create the required anonymous access type declarations, and if
17082 -- need be an incomplete type declaration for T itself.
17084 Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
17086 if Ada_Version >= Ada_05
17087 and then Present (Interface_List (Def))
17088 then
17089 Check_Interfaces (N, Def);
17091 declare
17092 Ifaces_List : Elist_Id;
17094 begin
17095 -- Ada 2005 (AI-251): Collect the list of progenitors that are not
17096 -- already in the parents.
17098 Collect_Interfaces
17099 (T => T,
17100 Ifaces_List => Ifaces_List,
17101 Exclude_Parents => True);
17103 Set_Interfaces (T, Ifaces_List);
17104 end;
17105 end if;
17107 -- Records constitute a scope for the component declarations within.
17108 -- The scope is created prior to the processing of these declarations.
17109 -- Discriminants are processed first, so that they are visible when
17110 -- processing the other components. The Ekind of the record type itself
17111 -- is set to E_Record_Type (subtypes appear as E_Record_Subtype).
17113 -- Enter record scope
17115 Push_Scope (T);
17117 -- If an incomplete or private type declaration was already given for
17118 -- the type, then this scope already exists, and the discriminants have
17119 -- been declared within. We must verify that the full declaration
17120 -- matches the incomplete one.
17122 Check_Or_Process_Discriminants (N, T, Prev);
17124 Set_Is_Constrained (T, not Has_Discriminants (T));
17125 Set_Has_Delayed_Freeze (T, True);
17127 -- For tagged types add a manually analyzed component corresponding
17128 -- to the component _tag, the corresponding piece of tree will be
17129 -- expanded as part of the freezing actions if it is not a CPP_Class.
17131 if Is_Tagged then
17133 -- Do not add the tag unless we are in expansion mode
17135 if Expander_Active then
17136 Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
17137 Enter_Name (Tag_Comp);
17139 Set_Ekind (Tag_Comp, E_Component);
17140 Set_Is_Tag (Tag_Comp);
17141 Set_Is_Aliased (Tag_Comp);
17142 Set_Etype (Tag_Comp, RTE (RE_Tag));
17143 Set_DT_Entry_Count (Tag_Comp, No_Uint);
17144 Set_Original_Record_Component (Tag_Comp, Tag_Comp);
17145 Init_Component_Location (Tag_Comp);
17147 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
17148 -- implemented interfaces.
17150 if Has_Interfaces (T) then
17151 Add_Interface_Tag_Components (N, T);
17152 end if;
17153 end if;
17155 Make_Class_Wide_Type (T);
17156 Set_Primitive_Operations (T, New_Elmt_List);
17157 end if;
17159 -- We must suppress range checks when processing the components
17160 -- of a record in the presence of discriminants, since we don't
17161 -- want spurious checks to be generated during their analysis, but
17162 -- must reset the Suppress_Range_Checks flags after having processed
17163 -- the record definition.
17165 -- Note: this is the only use of Kill_Range_Checks, and is a bit odd,
17166 -- couldn't we just use the normal range check suppression method here.
17167 -- That would seem cleaner ???
17169 if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
17170 Set_Kill_Range_Checks (T, True);
17171 Record_Type_Definition (Def, Prev);
17172 Set_Kill_Range_Checks (T, False);
17173 else
17174 Record_Type_Definition (Def, Prev);
17175 end if;
17177 -- Exit from record scope
17179 End_Scope;
17181 -- Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
17182 -- the implemented interfaces and associate them an aliased entity.
17184 if Is_Tagged
17185 and then not Is_Empty_List (Interface_List (Def))
17186 then
17187 Derive_Progenitor_Subprograms (T, T);
17188 end if;
17189 end Record_Type_Declaration;
17191 ----------------------------
17192 -- Record_Type_Definition --
17193 ----------------------------
17195 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
17196 Component : Entity_Id;
17197 Ctrl_Components : Boolean := False;
17198 Final_Storage_Only : Boolean;
17199 T : Entity_Id;
17201 begin
17202 if Ekind (Prev_T) = E_Incomplete_Type then
17203 T := Full_View (Prev_T);
17204 else
17205 T := Prev_T;
17206 end if;
17208 Final_Storage_Only := not Is_Controlled (T);
17210 -- Ada 2005: check whether an explicit Limited is present in a derived
17211 -- type declaration.
17213 if Nkind (Parent (Def)) = N_Derived_Type_Definition
17214 and then Limited_Present (Parent (Def))
17215 then
17216 Set_Is_Limited_Record (T);
17217 end if;
17219 -- If the component list of a record type is defined by the reserved
17220 -- word null and there is no discriminant part, then the record type has
17221 -- no components and all records of the type are null records (RM 3.7)
17222 -- This procedure is also called to process the extension part of a
17223 -- record extension, in which case the current scope may have inherited
17224 -- components.
17226 if No (Def)
17227 or else No (Component_List (Def))
17228 or else Null_Present (Component_List (Def))
17229 then
17230 null;
17232 else
17233 Analyze_Declarations (Component_Items (Component_List (Def)));
17235 if Present (Variant_Part (Component_List (Def))) then
17236 Analyze (Variant_Part (Component_List (Def)));
17237 end if;
17238 end if;
17240 -- After completing the semantic analysis of the record definition,
17241 -- record components, both new and inherited, are accessible. Set their
17242 -- kind accordingly. Exclude malformed itypes from illegal declarations,
17243 -- whose Ekind may be void.
17245 Component := First_Entity (Current_Scope);
17246 while Present (Component) loop
17247 if Ekind (Component) = E_Void
17248 and then not Is_Itype (Component)
17249 then
17250 Set_Ekind (Component, E_Component);
17251 Init_Component_Location (Component);
17252 end if;
17254 if Has_Task (Etype (Component)) then
17255 Set_Has_Task (T);
17256 end if;
17258 if Ekind (Component) /= E_Component then
17259 null;
17261 elsif Has_Controlled_Component (Etype (Component))
17262 or else (Chars (Component) /= Name_uParent
17263 and then Is_Controlled (Etype (Component)))
17264 then
17265 Set_Has_Controlled_Component (T, True);
17266 Final_Storage_Only :=
17267 Final_Storage_Only
17268 and then Finalize_Storage_Only (Etype (Component));
17269 Ctrl_Components := True;
17270 end if;
17272 Next_Entity (Component);
17273 end loop;
17275 -- A Type is Finalize_Storage_Only only if all its controlled components
17276 -- are also.
17278 if Ctrl_Components then
17279 Set_Finalize_Storage_Only (T, Final_Storage_Only);
17280 end if;
17282 -- Place reference to end record on the proper entity, which may
17283 -- be a partial view.
17285 if Present (Def) then
17286 Process_End_Label (Def, 'e', Prev_T);
17287 end if;
17288 end Record_Type_Definition;
17290 ------------------------
17291 -- Replace_Components --
17292 ------------------------
17294 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id) is
17295 function Process (N : Node_Id) return Traverse_Result;
17297 -------------
17298 -- Process --
17299 -------------
17301 function Process (N : Node_Id) return Traverse_Result is
17302 Comp : Entity_Id;
17304 begin
17305 if Nkind (N) = N_Discriminant_Specification then
17306 Comp := First_Discriminant (Typ);
17307 while Present (Comp) loop
17308 if Chars (Comp) = Chars (Defining_Identifier (N)) then
17309 Set_Defining_Identifier (N, Comp);
17310 exit;
17311 end if;
17313 Next_Discriminant (Comp);
17314 end loop;
17316 elsif Nkind (N) = N_Component_Declaration then
17317 Comp := First_Component (Typ);
17318 while Present (Comp) loop
17319 if Chars (Comp) = Chars (Defining_Identifier (N)) then
17320 Set_Defining_Identifier (N, Comp);
17321 exit;
17322 end if;
17324 Next_Component (Comp);
17325 end loop;
17326 end if;
17328 return OK;
17329 end Process;
17331 procedure Replace is new Traverse_Proc (Process);
17333 -- Start of processing for Replace_Components
17335 begin
17336 Replace (Decl);
17337 end Replace_Components;
17339 -------------------------------
17340 -- Set_Completion_Referenced --
17341 -------------------------------
17343 procedure Set_Completion_Referenced (E : Entity_Id) is
17344 begin
17345 -- If in main unit, mark entity that is a completion as referenced,
17346 -- warnings go on the partial view when needed.
17348 if In_Extended_Main_Source_Unit (E) then
17349 Set_Referenced (E);
17350 end if;
17351 end Set_Completion_Referenced;
17353 ---------------------
17354 -- Set_Fixed_Range --
17355 ---------------------
17357 -- The range for fixed-point types is complicated by the fact that we
17358 -- do not know the exact end points at the time of the declaration. This
17359 -- is true for three reasons:
17361 -- A size clause may affect the fudging of the end-points
17362 -- A small clause may affect the values of the end-points
17363 -- We try to include the end-points if it does not affect the size
17365 -- This means that the actual end-points must be established at the point
17366 -- when the type is frozen. Meanwhile, we first narrow the range as
17367 -- permitted (so that it will fit if necessary in a small specified size),
17368 -- and then build a range subtree with these narrowed bounds.
17370 -- Set_Fixed_Range constructs the range from real literal values, and sets
17371 -- the range as the Scalar_Range of the given fixed-point type entity.
17373 -- The parent of this range is set to point to the entity so that it is
17374 -- properly hooked into the tree (unlike normal Scalar_Range entries for
17375 -- other scalar types, which are just pointers to the range in the
17376 -- original tree, this would otherwise be an orphan).
17378 -- The tree is left unanalyzed. When the type is frozen, the processing
17379 -- in Freeze.Freeze_Fixed_Point_Type notices that the range is not
17380 -- analyzed, and uses this as an indication that it should complete
17381 -- work on the range (it will know the final small and size values).
17383 procedure Set_Fixed_Range
17384 (E : Entity_Id;
17385 Loc : Source_Ptr;
17386 Lo : Ureal;
17387 Hi : Ureal)
17389 S : constant Node_Id :=
17390 Make_Range (Loc,
17391 Low_Bound => Make_Real_Literal (Loc, Lo),
17392 High_Bound => Make_Real_Literal (Loc, Hi));
17393 begin
17394 Set_Scalar_Range (E, S);
17395 Set_Parent (S, E);
17396 end Set_Fixed_Range;
17398 ----------------------------------
17399 -- Set_Scalar_Range_For_Subtype --
17400 ----------------------------------
17402 procedure Set_Scalar_Range_For_Subtype
17403 (Def_Id : Entity_Id;
17404 R : Node_Id;
17405 Subt : Entity_Id)
17407 Kind : constant Entity_Kind := Ekind (Def_Id);
17409 begin
17410 Set_Scalar_Range (Def_Id, R);
17412 -- We need to link the range into the tree before resolving it so
17413 -- that types that are referenced, including importantly the subtype
17414 -- itself, are properly frozen (Freeze_Expression requires that the
17415 -- expression be properly linked into the tree). Of course if it is
17416 -- already linked in, then we do not disturb the current link.
17418 if No (Parent (R)) then
17419 Set_Parent (R, Def_Id);
17420 end if;
17422 -- Reset the kind of the subtype during analysis of the range, to
17423 -- catch possible premature use in the bounds themselves.
17425 Set_Ekind (Def_Id, E_Void);
17426 Process_Range_Expr_In_Decl (R, Subt);
17427 Set_Ekind (Def_Id, Kind);
17428 end Set_Scalar_Range_For_Subtype;
17430 --------------------------------------------------------
17431 -- Set_Stored_Constraint_From_Discriminant_Constraint --
17432 --------------------------------------------------------
17434 procedure Set_Stored_Constraint_From_Discriminant_Constraint
17435 (E : Entity_Id)
17437 begin
17438 -- Make sure set if encountered during Expand_To_Stored_Constraint
17440 Set_Stored_Constraint (E, No_Elist);
17442 -- Give it the right value
17444 if Is_Constrained (E) and then Has_Discriminants (E) then
17445 Set_Stored_Constraint (E,
17446 Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
17447 end if;
17448 end Set_Stored_Constraint_From_Discriminant_Constraint;
17450 -------------------------------------
17451 -- Signed_Integer_Type_Declaration --
17452 -------------------------------------
17454 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
17455 Implicit_Base : Entity_Id;
17456 Base_Typ : Entity_Id;
17457 Lo_Val : Uint;
17458 Hi_Val : Uint;
17459 Errs : Boolean := False;
17460 Lo : Node_Id;
17461 Hi : Node_Id;
17463 function Can_Derive_From (E : Entity_Id) return Boolean;
17464 -- Determine whether given bounds allow derivation from specified type
17466 procedure Check_Bound (Expr : Node_Id);
17467 -- Check bound to make sure it is integral and static. If not, post
17468 -- appropriate error message and set Errs flag
17470 ---------------------
17471 -- Can_Derive_From --
17472 ---------------------
17474 -- Note we check both bounds against both end values, to deal with
17475 -- strange types like ones with a range of 0 .. -12341234.
17477 function Can_Derive_From (E : Entity_Id) return Boolean is
17478 Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
17479 Hi : constant Uint := Expr_Value (Type_High_Bound (E));
17480 begin
17481 return Lo <= Lo_Val and then Lo_Val <= Hi
17482 and then
17483 Lo <= Hi_Val and then Hi_Val <= Hi;
17484 end Can_Derive_From;
17486 -----------------
17487 -- Check_Bound --
17488 -----------------
17490 procedure Check_Bound (Expr : Node_Id) is
17491 begin
17492 -- If a range constraint is used as an integer type definition, each
17493 -- bound of the range must be defined by a static expression of some
17494 -- integer type, but the two bounds need not have the same integer
17495 -- type (Negative bounds are allowed.) (RM 3.5.4)
17497 if not Is_Integer_Type (Etype (Expr)) then
17498 Error_Msg_N
17499 ("integer type definition bounds must be of integer type", Expr);
17500 Errs := True;
17502 elsif not Is_OK_Static_Expression (Expr) then
17503 Flag_Non_Static_Expr
17504 ("non-static expression used for integer type bound!", Expr);
17505 Errs := True;
17507 -- The bounds are folded into literals, and we set their type to be
17508 -- universal, to avoid typing difficulties: we cannot set the type
17509 -- of the literal to the new type, because this would be a forward
17510 -- reference for the back end, and if the original type is user-
17511 -- defined this can lead to spurious semantic errors (e.g. 2928-003).
17513 else
17514 if Is_Entity_Name (Expr) then
17515 Fold_Uint (Expr, Expr_Value (Expr), True);
17516 end if;
17518 Set_Etype (Expr, Universal_Integer);
17519 end if;
17520 end Check_Bound;
17522 -- Start of processing for Signed_Integer_Type_Declaration
17524 begin
17525 -- Create an anonymous base type
17527 Implicit_Base :=
17528 Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
17530 -- Analyze and check the bounds, they can be of any integer type
17532 Lo := Low_Bound (Def);
17533 Hi := High_Bound (Def);
17535 -- Arbitrarily use Integer as the type if either bound had an error
17537 if Hi = Error or else Lo = Error then
17538 Base_Typ := Any_Integer;
17539 Set_Error_Posted (T, True);
17541 -- Here both bounds are OK expressions
17543 else
17544 Analyze_And_Resolve (Lo, Any_Integer);
17545 Analyze_And_Resolve (Hi, Any_Integer);
17547 Check_Bound (Lo);
17548 Check_Bound (Hi);
17550 if Errs then
17551 Hi := Type_High_Bound (Standard_Long_Long_Integer);
17552 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
17553 end if;
17555 -- Find type to derive from
17557 Lo_Val := Expr_Value (Lo);
17558 Hi_Val := Expr_Value (Hi);
17560 if Can_Derive_From (Standard_Short_Short_Integer) then
17561 Base_Typ := Base_Type (Standard_Short_Short_Integer);
17563 elsif Can_Derive_From (Standard_Short_Integer) then
17564 Base_Typ := Base_Type (Standard_Short_Integer);
17566 elsif Can_Derive_From (Standard_Integer) then
17567 Base_Typ := Base_Type (Standard_Integer);
17569 elsif Can_Derive_From (Standard_Long_Integer) then
17570 Base_Typ := Base_Type (Standard_Long_Integer);
17572 elsif Can_Derive_From (Standard_Long_Long_Integer) then
17573 Base_Typ := Base_Type (Standard_Long_Long_Integer);
17575 else
17576 Base_Typ := Base_Type (Standard_Long_Long_Integer);
17577 Error_Msg_N ("integer type definition bounds out of range", Def);
17578 Hi := Type_High_Bound (Standard_Long_Long_Integer);
17579 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
17580 end if;
17581 end if;
17583 -- Complete both implicit base and declared first subtype entities
17585 Set_Etype (Implicit_Base, Base_Typ);
17586 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
17587 Set_Size_Info (Implicit_Base, (Base_Typ));
17588 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
17589 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
17591 Set_Ekind (T, E_Signed_Integer_Subtype);
17592 Set_Etype (T, Implicit_Base);
17594 Set_Size_Info (T, (Implicit_Base));
17595 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
17596 Set_Scalar_Range (T, Def);
17597 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
17598 Set_Is_Constrained (T);
17599 end Signed_Integer_Type_Declaration;
17601 end Sem_Ch3;