cfgloopmanip.c (copy_loop_info): New function.
[official-gcc.git] / gcc / ada / sem_ch3.adb
blob7dd808c0d0d62f5f1b20b4cdc589d4460dc9e348
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ C H 3 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2012, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Debug; use Debug;
30 with Elists; use Elists;
31 with Einfo; use Einfo;
32 with Errout; use Errout;
33 with Eval_Fat; use Eval_Fat;
34 with Exp_Ch3; use Exp_Ch3;
35 with Exp_Ch9; use Exp_Ch9;
36 with Exp_Disp; use Exp_Disp;
37 with Exp_Dist; use Exp_Dist;
38 with Exp_Tss; use Exp_Tss;
39 with Exp_Util; use Exp_Util;
40 with Fname; use Fname;
41 with Freeze; use Freeze;
42 with Itypes; use Itypes;
43 with Layout; use Layout;
44 with Lib; use Lib;
45 with Lib.Xref; use Lib.Xref;
46 with Namet; use Namet;
47 with Nmake; use Nmake;
48 with Opt; use Opt;
49 with Restrict; use Restrict;
50 with Rident; use Rident;
51 with Rtsfind; use Rtsfind;
52 with Sem; use Sem;
53 with Sem_Aux; use Sem_Aux;
54 with Sem_Case; use Sem_Case;
55 with Sem_Cat; use Sem_Cat;
56 with Sem_Ch6; use Sem_Ch6;
57 with Sem_Ch7; use Sem_Ch7;
58 with Sem_Ch8; use Sem_Ch8;
59 with Sem_Ch13; use Sem_Ch13;
60 with Sem_Dim; use Sem_Dim;
61 with Sem_Disp; use Sem_Disp;
62 with Sem_Dist; use Sem_Dist;
63 with Sem_Elim; use Sem_Elim;
64 with Sem_Eval; use Sem_Eval;
65 with Sem_Mech; use Sem_Mech;
66 with Sem_Prag; use Sem_Prag;
67 with Sem_Res; use Sem_Res;
68 with Sem_Smem; use Sem_Smem;
69 with Sem_Type; use Sem_Type;
70 with Sem_Util; use Sem_Util;
71 with Sem_Warn; use Sem_Warn;
72 with Stand; use Stand;
73 with Sinfo; use Sinfo;
74 with Sinput; use Sinput;
75 with Snames; use Snames;
76 with Targparm; use Targparm;
77 with Tbuild; use Tbuild;
78 with Ttypes; use Ttypes;
79 with Uintp; use Uintp;
80 with Urealp; use Urealp;
82 package body Sem_Ch3 is
84 -----------------------
85 -- Local Subprograms --
86 -----------------------
88 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id);
89 -- Ada 2005 (AI-251): Add the tag components corresponding to all the
90 -- abstract interface types implemented by a record type or a derived
91 -- record type.
93 procedure Build_Derived_Type
94 (N : Node_Id;
95 Parent_Type : Entity_Id;
96 Derived_Type : Entity_Id;
97 Is_Completion : Boolean;
98 Derive_Subps : Boolean := True);
99 -- Create and decorate a Derived_Type given the Parent_Type entity. N is
100 -- the N_Full_Type_Declaration node containing the derived type definition.
101 -- Parent_Type is the entity for the parent type in the derived type
102 -- definition and Derived_Type the actual derived type. Is_Completion must
103 -- be set to False if Derived_Type is the N_Defining_Identifier node in N
104 -- (i.e. Derived_Type = Defining_Identifier (N)). In this case N is not the
105 -- completion of a private type declaration. If Is_Completion is set to
106 -- True, N is the completion of a private type declaration and Derived_Type
107 -- is different from the defining identifier inside N (i.e. Derived_Type /=
108 -- Defining_Identifier (N)). Derive_Subps indicates whether the parent
109 -- subprograms should be derived. The only case where this parameter is
110 -- False is when Build_Derived_Type is recursively called to process an
111 -- implicit derived full type for a type derived from a private type (in
112 -- that case the subprograms must only be derived for the private view of
113 -- the type).
115 -- ??? These flags need a bit of re-examination and re-documentation:
116 -- ??? are they both necessary (both seem related to the recursion)?
118 procedure Build_Derived_Access_Type
119 (N : Node_Id;
120 Parent_Type : Entity_Id;
121 Derived_Type : Entity_Id);
122 -- Subsidiary procedure to Build_Derived_Type. For a derived access type,
123 -- create an implicit base if the parent type is constrained or if the
124 -- subtype indication has a constraint.
126 procedure Build_Derived_Array_Type
127 (N : Node_Id;
128 Parent_Type : Entity_Id;
129 Derived_Type : Entity_Id);
130 -- Subsidiary procedure to Build_Derived_Type. For a derived array type,
131 -- create an implicit base if the parent type is constrained or if the
132 -- subtype indication has a constraint.
134 procedure Build_Derived_Concurrent_Type
135 (N : Node_Id;
136 Parent_Type : Entity_Id;
137 Derived_Type : Entity_Id);
138 -- Subsidiary procedure to Build_Derived_Type. For a derived task or
139 -- protected type, inherit entries and protected subprograms, check
140 -- legality of discriminant constraints if any.
142 procedure Build_Derived_Enumeration_Type
143 (N : Node_Id;
144 Parent_Type : Entity_Id;
145 Derived_Type : Entity_Id);
146 -- Subsidiary procedure to Build_Derived_Type. For a derived enumeration
147 -- type, we must create a new list of literals. Types derived from
148 -- Character and [Wide_]Wide_Character are special-cased.
150 procedure Build_Derived_Numeric_Type
151 (N : Node_Id;
152 Parent_Type : Entity_Id;
153 Derived_Type : Entity_Id);
154 -- Subsidiary procedure to Build_Derived_Type. For numeric types, create
155 -- an anonymous base type, and propagate constraint to subtype if needed.
157 procedure Build_Derived_Private_Type
158 (N : Node_Id;
159 Parent_Type : Entity_Id;
160 Derived_Type : Entity_Id;
161 Is_Completion : Boolean;
162 Derive_Subps : Boolean := True);
163 -- Subsidiary procedure to Build_Derived_Type. This procedure is complex
164 -- because the parent may or may not have a completion, and the derivation
165 -- may itself be a completion.
167 procedure Build_Derived_Record_Type
168 (N : Node_Id;
169 Parent_Type : Entity_Id;
170 Derived_Type : Entity_Id;
171 Derive_Subps : Boolean := True);
172 -- Subsidiary procedure for Build_Derived_Type and
173 -- Analyze_Private_Extension_Declaration used for tagged and untagged
174 -- record types. All parameters are as in Build_Derived_Type except that
175 -- N, in addition to being an N_Full_Type_Declaration node, can also be an
176 -- N_Private_Extension_Declaration node. See the definition of this routine
177 -- for much more info. Derive_Subps indicates whether subprograms should
178 -- be derived from the parent type. The only case where Derive_Subps is
179 -- False is for an implicit derived full type for a type derived from a
180 -- private type (see Build_Derived_Type).
182 procedure Build_Discriminal (Discrim : Entity_Id);
183 -- Create the discriminal corresponding to discriminant Discrim, that is
184 -- the parameter corresponding to Discrim to be used in initialization
185 -- procedures for the type where Discrim is a discriminant. Discriminals
186 -- are not used during semantic analysis, and are not fully defined
187 -- entities until expansion. Thus they are not given a scope until
188 -- initialization procedures are built.
190 function Build_Discriminant_Constraints
191 (T : Entity_Id;
192 Def : Node_Id;
193 Derived_Def : Boolean := False) return Elist_Id;
194 -- Validate discriminant constraints and return the list of the constraints
195 -- in order of discriminant declarations, where T is the discriminated
196 -- unconstrained type. Def is the N_Subtype_Indication node where the
197 -- discriminants constraints for T are specified. Derived_Def is True
198 -- when building the discriminant constraints in a derived type definition
199 -- of the form "type D (...) is new T (xxx)". In this case T is the parent
200 -- type and Def is the constraint "(xxx)" on T and this routine sets the
201 -- Corresponding_Discriminant field of the discriminants in the derived
202 -- type D to point to the corresponding discriminants in the parent type T.
204 procedure Build_Discriminated_Subtype
205 (T : Entity_Id;
206 Def_Id : Entity_Id;
207 Elist : Elist_Id;
208 Related_Nod : Node_Id;
209 For_Access : Boolean := False);
210 -- Subsidiary procedure to Constrain_Discriminated_Type and to
211 -- Process_Incomplete_Dependents. Given
213 -- T (a possibly discriminated base type)
214 -- Def_Id (a very partially built subtype for T),
216 -- the call completes Def_Id to be the appropriate E_*_Subtype.
218 -- The Elist is the list of discriminant constraints if any (it is set
219 -- to No_Elist if T is not a discriminated type, and to an empty list if
220 -- T has discriminants but there are no discriminant constraints). The
221 -- Related_Nod is the same as Decl_Node in Create_Constrained_Components.
222 -- The For_Access says whether or not this subtype is really constraining
223 -- an access type. That is its sole purpose is the designated type of an
224 -- access type -- in which case a Private_Subtype Is_For_Access_Subtype
225 -- is built to avoid freezing T when the access subtype is frozen.
227 function Build_Scalar_Bound
228 (Bound : Node_Id;
229 Par_T : Entity_Id;
230 Der_T : Entity_Id) return Node_Id;
231 -- The bounds of a derived scalar type are conversions of the bounds of
232 -- the parent type. Optimize the representation if the bounds are literals.
233 -- Needs a more complete spec--what are the parameters exactly, and what
234 -- exactly is the returned value, and how is Bound affected???
236 procedure Build_Underlying_Full_View
237 (N : Node_Id;
238 Typ : Entity_Id;
239 Par : Entity_Id);
240 -- If the completion of a private type is itself derived from a private
241 -- type, or if the full view of a private subtype is itself private, the
242 -- back-end has no way to compute the actual size of this type. We build
243 -- an internal subtype declaration of the proper parent type to convey
244 -- this information. This extra mechanism is needed because a full
245 -- view cannot itself have a full view (it would get clobbered during
246 -- view exchanges).
248 procedure Check_Access_Discriminant_Requires_Limited
249 (D : Node_Id;
250 Loc : Node_Id);
251 -- Check the restriction that the type to which an access discriminant
252 -- belongs must be a concurrent type or a descendant of a type with
253 -- the reserved word 'limited' in its declaration.
255 procedure Check_Anonymous_Access_Components
256 (Typ_Decl : Node_Id;
257 Typ : Entity_Id;
258 Prev : Entity_Id;
259 Comp_List : Node_Id);
260 -- Ada 2005 AI-382: an access component in a record definition can refer to
261 -- the enclosing record, in which case it denotes the type itself, and not
262 -- the current instance of the type. We create an anonymous access type for
263 -- the component, and flag it as an access to a component, so accessibility
264 -- checks are properly performed on it. The declaration of the access type
265 -- is placed ahead of that of the record to prevent order-of-elaboration
266 -- circularity issues in Gigi. We create an incomplete type for the record
267 -- declaration, which is the designated type of the anonymous access.
269 procedure Check_Delta_Expression (E : Node_Id);
270 -- Check that the expression represented by E is suitable for use as a
271 -- delta expression, i.e. it is of real type and is static.
273 procedure Check_Digits_Expression (E : Node_Id);
274 -- Check that the expression represented by E is suitable for use as a
275 -- digits expression, i.e. it is of integer type, positive and static.
277 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id);
278 -- Validate the initialization of an object declaration. T is the required
279 -- type, and Exp is the initialization expression.
281 procedure Check_Interfaces (N : Node_Id; Def : Node_Id);
282 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
284 procedure Check_Or_Process_Discriminants
285 (N : Node_Id;
286 T : Entity_Id;
287 Prev : Entity_Id := Empty);
288 -- If N is the full declaration of the completion T of an incomplete or
289 -- private type, check its discriminants (which are already known to be
290 -- conformant with those of the partial view, see Find_Type_Name),
291 -- otherwise process them. Prev is the entity of the partial declaration,
292 -- if any.
294 procedure Check_Real_Bound (Bound : Node_Id);
295 -- Check given bound for being of real type and static. If not, post an
296 -- appropriate message, and rewrite the bound with the real literal zero.
298 procedure Constant_Redeclaration
299 (Id : Entity_Id;
300 N : Node_Id;
301 T : out Entity_Id);
302 -- Various checks on legality of full declaration of deferred constant.
303 -- Id is the entity for the redeclaration, N is the N_Object_Declaration,
304 -- node. The caller has not yet set any attributes of this entity.
306 function Contain_Interface
307 (Iface : Entity_Id;
308 Ifaces : Elist_Id) return Boolean;
309 -- Ada 2005: Determine whether Iface is present in the list Ifaces
311 procedure Convert_Scalar_Bounds
312 (N : Node_Id;
313 Parent_Type : Entity_Id;
314 Derived_Type : Entity_Id;
315 Loc : Source_Ptr);
316 -- For derived scalar types, convert the bounds in the type definition to
317 -- the derived type, and complete their analysis. Given a constraint of the
318 -- form ".. new T range Lo .. Hi", Lo and Hi are analyzed and resolved with
319 -- T'Base, the parent_type. The bounds of the derived type (the anonymous
320 -- base) are copies of Lo and Hi. Finally, the bounds of the derived
321 -- subtype are conversions of those bounds to the derived_type, so that
322 -- their typing is consistent.
324 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id);
325 -- Copies attributes from array base type T2 to array base type T1. Copies
326 -- only attributes that apply to base types, but not subtypes.
328 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id);
329 -- Copies attributes from array subtype T2 to array subtype T1. Copies
330 -- attributes that apply to both subtypes and base types.
332 procedure Create_Constrained_Components
333 (Subt : Entity_Id;
334 Decl_Node : Node_Id;
335 Typ : Entity_Id;
336 Constraints : Elist_Id);
337 -- Build the list of entities for a constrained discriminated record
338 -- subtype. If a component depends on a discriminant, replace its subtype
339 -- using the discriminant values in the discriminant constraint. Subt
340 -- is the defining identifier for the subtype whose list of constrained
341 -- entities we will create. Decl_Node is the type declaration node where
342 -- we will attach all the itypes created. Typ is the base discriminated
343 -- type for the subtype Subt. Constraints is the list of discriminant
344 -- constraints for Typ.
346 function Constrain_Component_Type
347 (Comp : Entity_Id;
348 Constrained_Typ : Entity_Id;
349 Related_Node : Node_Id;
350 Typ : Entity_Id;
351 Constraints : Elist_Id) return Entity_Id;
352 -- Given a discriminated base type Typ, a list of discriminant constraint
353 -- Constraints for Typ and a component of Typ, with type Compon_Type,
354 -- create and return the type corresponding to Compon_type where all
355 -- discriminant references are replaced with the corresponding constraint.
356 -- If no discriminant references occur in Compon_Typ then return it as is.
357 -- Constrained_Typ is the final constrained subtype to which the
358 -- constrained Compon_Type belongs. Related_Node is the node where we will
359 -- attach all the itypes created.
361 -- Above description is confused, what is Compon_Type???
363 procedure Constrain_Access
364 (Def_Id : in out Entity_Id;
365 S : Node_Id;
366 Related_Nod : Node_Id);
367 -- Apply a list of constraints to an access type. If Def_Id is empty, it is
368 -- an anonymous type created for a subtype indication. In that case it is
369 -- created in the procedure and attached to Related_Nod.
371 procedure Constrain_Array
372 (Def_Id : in out Entity_Id;
373 SI : Node_Id;
374 Related_Nod : Node_Id;
375 Related_Id : Entity_Id;
376 Suffix : Character);
377 -- Apply a list of index constraints to an unconstrained array type. The
378 -- first parameter is the entity for the resulting subtype. A value of
379 -- Empty for Def_Id indicates that an implicit type must be created, but
380 -- creation is delayed (and must be done by this procedure) because other
381 -- subsidiary implicit types must be created first (which is why Def_Id
382 -- is an in/out parameter). The second parameter is a subtype indication
383 -- node for the constrained array to be created (e.g. something of the
384 -- form string (1 .. 10)). Related_Nod gives the place where this type
385 -- has to be inserted in the tree. The Related_Id and Suffix parameters
386 -- are used to build the associated Implicit type name.
388 procedure Constrain_Concurrent
389 (Def_Id : in out Entity_Id;
390 SI : Node_Id;
391 Related_Nod : Node_Id;
392 Related_Id : Entity_Id;
393 Suffix : Character);
394 -- Apply list of discriminant constraints to an unconstrained concurrent
395 -- type.
397 -- SI is the N_Subtype_Indication node containing the constraint and
398 -- the unconstrained type to constrain.
400 -- Def_Id is the entity for the resulting constrained subtype. A value
401 -- of Empty for Def_Id indicates that an implicit type must be created,
402 -- but creation is delayed (and must be done by this procedure) because
403 -- other subsidiary implicit types must be created first (which is why
404 -- Def_Id is an in/out parameter).
406 -- Related_Nod gives the place where this type has to be inserted
407 -- in the tree
409 -- The last two arguments are used to create its external name if needed.
411 function Constrain_Corresponding_Record
412 (Prot_Subt : Entity_Id;
413 Corr_Rec : Entity_Id;
414 Related_Nod : Node_Id;
415 Related_Id : Entity_Id) return Entity_Id;
416 -- When constraining a protected type or task type with discriminants,
417 -- constrain the corresponding record with the same discriminant values.
419 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id);
420 -- Constrain a decimal fixed point type with a digits constraint and/or a
421 -- range constraint, and build E_Decimal_Fixed_Point_Subtype entity.
423 procedure Constrain_Discriminated_Type
424 (Def_Id : Entity_Id;
425 S : Node_Id;
426 Related_Nod : Node_Id;
427 For_Access : Boolean := False);
428 -- Process discriminant constraints of composite type. Verify that values
429 -- have been provided for all discriminants, that the original type is
430 -- unconstrained, and that the types of the supplied expressions match
431 -- the discriminant types. The first three parameters are like in routine
432 -- Constrain_Concurrent. See Build_Discriminated_Subtype for an explanation
433 -- of For_Access.
435 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id);
436 -- Constrain an enumeration type with a range constraint. This is identical
437 -- to Constrain_Integer, but for the Ekind of the resulting subtype.
439 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id);
440 -- Constrain a floating point type with either a digits constraint
441 -- and/or a range constraint, building a E_Floating_Point_Subtype.
443 procedure Constrain_Index
444 (Index : Node_Id;
445 S : Node_Id;
446 Related_Nod : Node_Id;
447 Related_Id : Entity_Id;
448 Suffix : Character;
449 Suffix_Index : Nat);
450 -- Process an index constraint S in a constrained array declaration. The
451 -- constraint can be a subtype name, or a range with or without an explicit
452 -- subtype mark. The index is the corresponding index of the unconstrained
453 -- array. The Related_Id and Suffix parameters are used to build the
454 -- associated Implicit type name.
456 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id);
457 -- Build subtype of a signed or modular integer type
459 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id);
460 -- Constrain an ordinary fixed point type with a range constraint, and
461 -- build an E_Ordinary_Fixed_Point_Subtype entity.
463 procedure Copy_And_Swap (Priv, Full : Entity_Id);
464 -- Copy the Priv entity into the entity of its full declaration then swap
465 -- the two entities in such a manner that the former private type is now
466 -- seen as a full type.
468 procedure Decimal_Fixed_Point_Type_Declaration
469 (T : Entity_Id;
470 Def : Node_Id);
471 -- Create a new decimal fixed point type, and apply the constraint to
472 -- obtain a subtype of this new type.
474 procedure Complete_Private_Subtype
475 (Priv : Entity_Id;
476 Full : Entity_Id;
477 Full_Base : Entity_Id;
478 Related_Nod : Node_Id);
479 -- Complete the implicit full view of a private subtype by setting the
480 -- appropriate semantic fields. If the full view of the parent is a record
481 -- type, build constrained components of subtype.
483 procedure Derive_Progenitor_Subprograms
484 (Parent_Type : Entity_Id;
485 Tagged_Type : Entity_Id);
486 -- Ada 2005 (AI-251): To complete type derivation, collect the primitive
487 -- operations of progenitors of Tagged_Type, and replace the subsidiary
488 -- subtypes with Tagged_Type, to build the specs of the inherited interface
489 -- primitives. The derived primitives are aliased to those of the
490 -- interface. This routine takes care also of transferring to the full view
491 -- subprograms associated with the partial view of Tagged_Type that cover
492 -- interface primitives.
494 procedure Derived_Standard_Character
495 (N : Node_Id;
496 Parent_Type : Entity_Id;
497 Derived_Type : Entity_Id);
498 -- Subsidiary procedure to Build_Derived_Enumeration_Type which handles
499 -- derivations from types Standard.Character and Standard.Wide_Character.
501 procedure Derived_Type_Declaration
502 (T : Entity_Id;
503 N : Node_Id;
504 Is_Completion : Boolean);
505 -- Process a derived type declaration. Build_Derived_Type is invoked
506 -- to process the actual derived type definition. Parameters N and
507 -- Is_Completion have the same meaning as in Build_Derived_Type.
508 -- T is the N_Defining_Identifier for the entity defined in the
509 -- N_Full_Type_Declaration node N, that is T is the derived type.
511 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id);
512 -- Insert each literal in symbol table, as an overloadable identifier. Each
513 -- enumeration type is mapped into a sequence of integers, and each literal
514 -- is defined as a constant with integer value. If any of the literals are
515 -- character literals, the type is a character type, which means that
516 -- strings are legal aggregates for arrays of components of the type.
518 function Expand_To_Stored_Constraint
519 (Typ : Entity_Id;
520 Constraint : Elist_Id) return Elist_Id;
521 -- Given a constraint (i.e. a list of expressions) on the discriminants of
522 -- Typ, expand it into a constraint on the stored discriminants and return
523 -- the new list of expressions constraining the stored discriminants.
525 function Find_Type_Of_Object
526 (Obj_Def : Node_Id;
527 Related_Nod : Node_Id) return Entity_Id;
528 -- Get type entity for object referenced by Obj_Def, attaching the
529 -- implicit types generated to Related_Nod
531 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id);
532 -- Create a new float and apply the constraint to obtain subtype of it
534 function Has_Range_Constraint (N : Node_Id) return Boolean;
535 -- Given an N_Subtype_Indication node N, return True if a range constraint
536 -- is present, either directly, or as part of a digits or delta constraint.
537 -- In addition, a digits constraint in the decimal case returns True, since
538 -- it establishes a default range if no explicit range is present.
540 function Inherit_Components
541 (N : Node_Id;
542 Parent_Base : Entity_Id;
543 Derived_Base : Entity_Id;
544 Is_Tagged : Boolean;
545 Inherit_Discr : Boolean;
546 Discs : Elist_Id) return Elist_Id;
547 -- Called from Build_Derived_Record_Type to inherit the components of
548 -- Parent_Base (a base type) into the Derived_Base (the derived base type).
549 -- For more information on derived types and component inheritance please
550 -- consult the comment above the body of Build_Derived_Record_Type.
552 -- N is the original derived type declaration
554 -- Is_Tagged is set if we are dealing with tagged types
556 -- If Inherit_Discr is set, Derived_Base inherits its discriminants from
557 -- Parent_Base, otherwise no discriminants are inherited.
559 -- Discs gives the list of constraints that apply to Parent_Base in the
560 -- derived type declaration. If Discs is set to No_Elist, then we have
561 -- the following situation:
563 -- type Parent (D1..Dn : ..) is [tagged] record ...;
564 -- type Derived is new Parent [with ...];
566 -- which gets treated as
568 -- type Derived (D1..Dn : ..) is new Parent (D1,..,Dn) [with ...];
570 -- For untagged types the returned value is an association list. The list
571 -- starts from the association (Parent_Base => Derived_Base), and then it
572 -- contains a sequence of the associations of the form
574 -- (Old_Component => New_Component),
576 -- where Old_Component is the Entity_Id of a component in Parent_Base and
577 -- New_Component is the Entity_Id of the corresponding component in
578 -- Derived_Base. For untagged records, this association list is needed when
579 -- copying the record declaration for the derived base. In the tagged case
580 -- the value returned is irrelevant.
582 function Is_Valid_Constraint_Kind
583 (T_Kind : Type_Kind;
584 Constraint_Kind : Node_Kind) return Boolean;
585 -- Returns True if it is legal to apply the given kind of constraint to the
586 -- given kind of type (index constraint to an array type, for example).
588 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id);
589 -- Create new modular type. Verify that modulus is in bounds
591 procedure New_Concatenation_Op (Typ : Entity_Id);
592 -- Create an abbreviated declaration for an operator in order to
593 -- materialize concatenation on array types.
595 procedure Ordinary_Fixed_Point_Type_Declaration
596 (T : Entity_Id;
597 Def : Node_Id);
598 -- Create a new ordinary fixed point type, and apply the constraint to
599 -- obtain subtype of it.
601 procedure Prepare_Private_Subtype_Completion
602 (Id : Entity_Id;
603 Related_Nod : Node_Id);
604 -- Id is a subtype of some private type. Creates the full declaration
605 -- associated with Id whenever possible, i.e. when the full declaration
606 -- of the base type is already known. Records each subtype into
607 -- Private_Dependents of the base type.
609 procedure Process_Incomplete_Dependents
610 (N : Node_Id;
611 Full_T : Entity_Id;
612 Inc_T : Entity_Id);
613 -- Process all entities that depend on an incomplete type. There include
614 -- subtypes, subprogram types that mention the incomplete type in their
615 -- profiles, and subprogram with access parameters that designate the
616 -- incomplete type.
618 -- Inc_T is the defining identifier of an incomplete type declaration, its
619 -- Ekind is E_Incomplete_Type.
621 -- N is the corresponding N_Full_Type_Declaration for Inc_T.
623 -- Full_T is N's defining identifier.
625 -- Subtypes of incomplete types with discriminants are completed when the
626 -- parent type is. This is simpler than private subtypes, because they can
627 -- only appear in the same scope, and there is no need to exchange views.
628 -- Similarly, access_to_subprogram types may have a parameter or a return
629 -- type that is an incomplete type, and that must be replaced with the
630 -- full type.
632 -- If the full type is tagged, subprogram with access parameters that
633 -- designated the incomplete may be primitive operations of the full type,
634 -- and have to be processed accordingly.
636 procedure Process_Real_Range_Specification (Def : Node_Id);
637 -- Given the type definition for a real type, this procedure processes and
638 -- checks the real range specification of this type definition if one is
639 -- present. If errors are found, error messages are posted, and the
640 -- Real_Range_Specification of Def is reset to Empty.
642 procedure Record_Type_Declaration
643 (T : Entity_Id;
644 N : Node_Id;
645 Prev : Entity_Id);
646 -- Process a record type declaration (for both untagged and tagged
647 -- records). Parameters T and N are exactly like in procedure
648 -- Derived_Type_Declaration, except that no flag Is_Completion is needed
649 -- for this routine. If this is the completion of an incomplete type
650 -- declaration, Prev is the entity of the incomplete declaration, used for
651 -- cross-referencing. Otherwise Prev = T.
653 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id);
654 -- This routine is used to process the actual record type definition (both
655 -- for untagged and tagged records). Def is a record type definition node.
656 -- This procedure analyzes the components in this record type definition.
657 -- Prev_T is the entity for the enclosing record type. It is provided so
658 -- that its Has_Task flag can be set if any of the component have Has_Task
659 -- set. If the declaration is the completion of an incomplete type
660 -- declaration, Prev_T is the original incomplete type, whose full view is
661 -- the record type.
663 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id);
664 -- Subsidiary to Build_Derived_Record_Type. For untagged records, we
665 -- build a copy of the declaration tree of the parent, and we create
666 -- independently the list of components for the derived type. Semantic
667 -- information uses the component entities, but record representation
668 -- clauses are validated on the declaration tree. This procedure replaces
669 -- discriminants and components in the declaration with those that have
670 -- been created by Inherit_Components.
672 procedure Set_Fixed_Range
673 (E : Entity_Id;
674 Loc : Source_Ptr;
675 Lo : Ureal;
676 Hi : Ureal);
677 -- Build a range node with the given bounds and set it as the Scalar_Range
678 -- of the given fixed-point type entity. Loc is the source location used
679 -- for the constructed range. See body for further details.
681 procedure Set_Scalar_Range_For_Subtype
682 (Def_Id : Entity_Id;
683 R : Node_Id;
684 Subt : Entity_Id);
685 -- This routine is used to set the scalar range field for a subtype given
686 -- Def_Id, the entity for the subtype, and R, the range expression for the
687 -- scalar range. Subt provides the parent subtype to be used to analyze,
688 -- resolve, and check the given range.
690 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id);
691 -- Create a new signed integer entity, and apply the constraint to obtain
692 -- the required first named subtype of this type.
694 procedure Set_Stored_Constraint_From_Discriminant_Constraint
695 (E : Entity_Id);
696 -- E is some record type. This routine computes E's Stored_Constraint
697 -- from its Discriminant_Constraint.
699 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id);
700 -- Check that an entity in a list of progenitors is an interface,
701 -- emit error otherwise.
703 -----------------------
704 -- Access_Definition --
705 -----------------------
707 function Access_Definition
708 (Related_Nod : Node_Id;
709 N : Node_Id) return Entity_Id
711 Anon_Type : Entity_Id;
712 Anon_Scope : Entity_Id;
713 Desig_Type : Entity_Id;
714 Enclosing_Prot_Type : Entity_Id := Empty;
716 begin
717 Check_SPARK_Restriction ("access type is not allowed", N);
719 if Is_Entry (Current_Scope)
720 and then Is_Task_Type (Etype (Scope (Current_Scope)))
721 then
722 Error_Msg_N ("task entries cannot have access parameters", N);
723 return Empty;
724 end if;
726 -- Ada 2005: for an object declaration the corresponding anonymous
727 -- type is declared in the current scope.
729 -- If the access definition is the return type of another access to
730 -- function, scope is the current one, because it is the one of the
731 -- current type declaration, except for the pathological case below.
733 if Nkind_In (Related_Nod, N_Object_Declaration,
734 N_Access_Function_Definition)
735 then
736 Anon_Scope := Current_Scope;
738 -- A pathological case: function returning access functions that
739 -- return access functions, etc. Each anonymous access type created
740 -- is in the enclosing scope of the outermost function.
742 declare
743 Par : Node_Id;
745 begin
746 Par := Related_Nod;
747 while Nkind_In (Par, N_Access_Function_Definition,
748 N_Access_Definition)
749 loop
750 Par := Parent (Par);
751 end loop;
753 if Nkind (Par) = N_Function_Specification then
754 Anon_Scope := Scope (Defining_Entity (Par));
755 end if;
756 end;
758 -- For the anonymous function result case, retrieve the scope of the
759 -- function specification's associated entity rather than using the
760 -- current scope. The current scope will be the function itself if the
761 -- formal part is currently being analyzed, but will be the parent scope
762 -- in the case of a parameterless function, and we always want to use
763 -- the function's parent scope. Finally, if the function is a child
764 -- unit, we must traverse the tree to retrieve the proper entity.
766 elsif Nkind (Related_Nod) = N_Function_Specification
767 and then Nkind (Parent (N)) /= N_Parameter_Specification
768 then
769 -- If the current scope is a protected type, the anonymous access
770 -- is associated with one of the protected operations, and must
771 -- be available in the scope that encloses the protected declaration.
772 -- Otherwise the type is in the scope enclosing the subprogram.
774 -- If the function has formals, The return type of a subprogram
775 -- declaration is analyzed in the scope of the subprogram (see
776 -- Process_Formals) and thus the protected type, if present, is
777 -- the scope of the current function scope.
779 if Ekind (Current_Scope) = E_Protected_Type then
780 Enclosing_Prot_Type := Current_Scope;
782 elsif Ekind (Current_Scope) = E_Function
783 and then Ekind (Scope (Current_Scope)) = E_Protected_Type
784 then
785 Enclosing_Prot_Type := Scope (Current_Scope);
786 end if;
788 if Present (Enclosing_Prot_Type) then
789 Anon_Scope := Scope (Enclosing_Prot_Type);
791 else
792 Anon_Scope := Scope (Defining_Entity (Related_Nod));
793 end if;
795 -- For an access type definition, if the current scope is a child
796 -- unit it is the scope of the type.
798 elsif Is_Compilation_Unit (Current_Scope) then
799 Anon_Scope := Current_Scope;
801 -- For access formals, access components, and access discriminants, the
802 -- scope is that of the enclosing declaration,
804 else
805 Anon_Scope := Scope (Current_Scope);
806 end if;
808 Anon_Type :=
809 Create_Itype
810 (E_Anonymous_Access_Type, Related_Nod, Scope_Id => Anon_Scope);
812 if All_Present (N)
813 and then Ada_Version >= Ada_2005
814 then
815 Error_Msg_N ("ALL is not permitted for anonymous access types", N);
816 end if;
818 -- Ada 2005 (AI-254): In case of anonymous access to subprograms call
819 -- the corresponding semantic routine
821 if Present (Access_To_Subprogram_Definition (N)) then
823 -- Compiler runtime units are compiled in Ada 2005 mode when building
824 -- the runtime library but must also be compilable in Ada 95 mode
825 -- (when bootstrapping the compiler).
827 Check_Compiler_Unit (N);
829 Access_Subprogram_Declaration
830 (T_Name => Anon_Type,
831 T_Def => Access_To_Subprogram_Definition (N));
833 if Ekind (Anon_Type) = E_Access_Protected_Subprogram_Type then
834 Set_Ekind
835 (Anon_Type, E_Anonymous_Access_Protected_Subprogram_Type);
836 else
837 Set_Ekind
838 (Anon_Type, E_Anonymous_Access_Subprogram_Type);
839 end if;
841 Set_Can_Use_Internal_Rep
842 (Anon_Type, not Always_Compatible_Rep_On_Target);
844 -- If the anonymous access is associated with a protected operation,
845 -- create a reference to it after the enclosing protected definition
846 -- because the itype will be used in the subsequent bodies.
848 if Ekind (Current_Scope) = E_Protected_Type then
849 Build_Itype_Reference (Anon_Type, Parent (Current_Scope));
850 end if;
852 return Anon_Type;
853 end if;
855 Find_Type (Subtype_Mark (N));
856 Desig_Type := Entity (Subtype_Mark (N));
858 Set_Directly_Designated_Type (Anon_Type, Desig_Type);
859 Set_Etype (Anon_Type, Anon_Type);
861 -- Make sure the anonymous access type has size and alignment fields
862 -- set, as required by gigi. This is necessary in the case of the
863 -- Task_Body_Procedure.
865 if not Has_Private_Component (Desig_Type) then
866 Layout_Type (Anon_Type);
867 end if;
869 -- Ada 2005 (AI-231): Ada 2005 semantics for anonymous access differs
870 -- from Ada 95 semantics. In Ada 2005, anonymous access must specify if
871 -- the null value is allowed. In Ada 95 the null value is never allowed.
873 if Ada_Version >= Ada_2005 then
874 Set_Can_Never_Be_Null (Anon_Type, Null_Exclusion_Present (N));
875 else
876 Set_Can_Never_Be_Null (Anon_Type, True);
877 end if;
879 -- The anonymous access type is as public as the discriminated type or
880 -- subprogram that defines it. It is imported (for back-end purposes)
881 -- if the designated type is.
883 Set_Is_Public (Anon_Type, Is_Public (Scope (Anon_Type)));
885 -- Ada 2005 (AI-231): Propagate the access-constant attribute
887 Set_Is_Access_Constant (Anon_Type, Constant_Present (N));
889 -- The context is either a subprogram declaration, object declaration,
890 -- or an access discriminant, in a private or a full type declaration.
891 -- In the case of a subprogram, if the designated type is incomplete,
892 -- the operation will be a primitive operation of the full type, to be
893 -- updated subsequently. If the type is imported through a limited_with
894 -- clause, the subprogram is not a primitive operation of the type
895 -- (which is declared elsewhere in some other scope).
897 if Ekind (Desig_Type) = E_Incomplete_Type
898 and then not From_With_Type (Desig_Type)
899 and then Is_Overloadable (Current_Scope)
900 then
901 Append_Elmt (Current_Scope, Private_Dependents (Desig_Type));
902 Set_Has_Delayed_Freeze (Current_Scope);
903 end if;
905 -- Ada 2005: if the designated type is an interface that may contain
906 -- tasks, create a Master entity for the declaration. This must be done
907 -- before expansion of the full declaration, because the declaration may
908 -- include an expression that is an allocator, whose expansion needs the
909 -- proper Master for the created tasks.
911 if Nkind (Related_Nod) = N_Object_Declaration
912 and then Expander_Active
913 then
914 if Is_Interface (Desig_Type)
915 and then Is_Limited_Record (Desig_Type)
916 then
917 Build_Class_Wide_Master (Anon_Type);
919 -- Similarly, if the type is an anonymous access that designates
920 -- tasks, create a master entity for it in the current context.
922 elsif Has_Task (Desig_Type)
923 and then Comes_From_Source (Related_Nod)
924 then
925 Build_Master_Entity (Defining_Identifier (Related_Nod));
926 Build_Master_Renaming (Anon_Type);
927 end if;
928 end if;
930 -- For a private component of a protected type, it is imperative that
931 -- the back-end elaborate the type immediately after the protected
932 -- declaration, because this type will be used in the declarations
933 -- created for the component within each protected body, so we must
934 -- create an itype reference for it now.
936 if Nkind (Parent (Related_Nod)) = N_Protected_Definition then
937 Build_Itype_Reference (Anon_Type, Parent (Parent (Related_Nod)));
939 -- Similarly, if the access definition is the return result of a
940 -- function, create an itype reference for it because it will be used
941 -- within the function body. For a regular function that is not a
942 -- compilation unit, insert reference after the declaration. For a
943 -- protected operation, insert it after the enclosing protected type
944 -- declaration. In either case, do not create a reference for a type
945 -- obtained through a limited_with clause, because this would introduce
946 -- semantic dependencies.
948 -- Similarly, do not create a reference if the designated type is a
949 -- generic formal, because no use of it will reach the backend.
951 elsif Nkind (Related_Nod) = N_Function_Specification
952 and then not From_With_Type (Desig_Type)
953 and then not Is_Generic_Type (Desig_Type)
954 then
955 if Present (Enclosing_Prot_Type) then
956 Build_Itype_Reference (Anon_Type, Parent (Enclosing_Prot_Type));
958 elsif Is_List_Member (Parent (Related_Nod))
959 and then Nkind (Parent (N)) /= N_Parameter_Specification
960 then
961 Build_Itype_Reference (Anon_Type, Parent (Related_Nod));
962 end if;
964 -- Finally, create an itype reference for an object declaration of an
965 -- anonymous access type. This is strictly necessary only for deferred
966 -- constants, but in any case will avoid out-of-scope problems in the
967 -- back-end.
969 elsif Nkind (Related_Nod) = N_Object_Declaration then
970 Build_Itype_Reference (Anon_Type, Related_Nod);
971 end if;
973 return Anon_Type;
974 end Access_Definition;
976 -----------------------------------
977 -- Access_Subprogram_Declaration --
978 -----------------------------------
980 procedure Access_Subprogram_Declaration
981 (T_Name : Entity_Id;
982 T_Def : Node_Id)
985 procedure Check_For_Premature_Usage (Def : Node_Id);
986 -- Check that type T_Name is not used, directly or recursively, as a
987 -- parameter or a return type in Def. Def is either a subtype, an
988 -- access_definition, or an access_to_subprogram_definition.
990 -------------------------------
991 -- Check_For_Premature_Usage --
992 -------------------------------
994 procedure Check_For_Premature_Usage (Def : Node_Id) is
995 Param : Node_Id;
997 begin
998 -- Check for a subtype mark
1000 if Nkind (Def) in N_Has_Etype then
1001 if Etype (Def) = T_Name then
1002 Error_Msg_N
1003 ("type& cannot be used before end of its declaration", Def);
1004 end if;
1006 -- If this is not a subtype, then this is an access_definition
1008 elsif Nkind (Def) = N_Access_Definition then
1009 if Present (Access_To_Subprogram_Definition (Def)) then
1010 Check_For_Premature_Usage
1011 (Access_To_Subprogram_Definition (Def));
1012 else
1013 Check_For_Premature_Usage (Subtype_Mark (Def));
1014 end if;
1016 -- The only cases left are N_Access_Function_Definition and
1017 -- N_Access_Procedure_Definition.
1019 else
1020 if Present (Parameter_Specifications (Def)) then
1021 Param := First (Parameter_Specifications (Def));
1022 while Present (Param) loop
1023 Check_For_Premature_Usage (Parameter_Type (Param));
1024 Param := Next (Param);
1025 end loop;
1026 end if;
1028 if Nkind (Def) = N_Access_Function_Definition then
1029 Check_For_Premature_Usage (Result_Definition (Def));
1030 end if;
1031 end if;
1032 end Check_For_Premature_Usage;
1034 -- Local variables
1036 Formals : constant List_Id := Parameter_Specifications (T_Def);
1037 Formal : Entity_Id;
1038 D_Ityp : Node_Id;
1039 Desig_Type : constant Entity_Id :=
1040 Create_Itype (E_Subprogram_Type, Parent (T_Def));
1042 -- Start of processing for Access_Subprogram_Declaration
1044 begin
1045 Check_SPARK_Restriction ("access type is not allowed", T_Def);
1047 -- Associate the Itype node with the inner full-type declaration or
1048 -- subprogram spec or entry body. This is required to handle nested
1049 -- anonymous declarations. For example:
1051 -- procedure P
1052 -- (X : access procedure
1053 -- (Y : access procedure
1054 -- (Z : access T)))
1056 D_Ityp := Associated_Node_For_Itype (Desig_Type);
1057 while not (Nkind_In (D_Ityp, N_Full_Type_Declaration,
1058 N_Private_Type_Declaration,
1059 N_Private_Extension_Declaration,
1060 N_Procedure_Specification,
1061 N_Function_Specification,
1062 N_Entry_Body)
1064 or else
1065 Nkind_In (D_Ityp, N_Object_Declaration,
1066 N_Object_Renaming_Declaration,
1067 N_Formal_Object_Declaration,
1068 N_Formal_Type_Declaration,
1069 N_Task_Type_Declaration,
1070 N_Protected_Type_Declaration))
1071 loop
1072 D_Ityp := Parent (D_Ityp);
1073 pragma Assert (D_Ityp /= Empty);
1074 end loop;
1076 Set_Associated_Node_For_Itype (Desig_Type, D_Ityp);
1078 if Nkind_In (D_Ityp, N_Procedure_Specification,
1079 N_Function_Specification)
1080 then
1081 Set_Scope (Desig_Type, Scope (Defining_Entity (D_Ityp)));
1083 elsif Nkind_In (D_Ityp, N_Full_Type_Declaration,
1084 N_Object_Declaration,
1085 N_Object_Renaming_Declaration,
1086 N_Formal_Type_Declaration)
1087 then
1088 Set_Scope (Desig_Type, Scope (Defining_Identifier (D_Ityp)));
1089 end if;
1091 if Nkind (T_Def) = N_Access_Function_Definition then
1092 if Nkind (Result_Definition (T_Def)) = N_Access_Definition then
1093 declare
1094 Acc : constant Node_Id := Result_Definition (T_Def);
1096 begin
1097 if Present (Access_To_Subprogram_Definition (Acc))
1098 and then
1099 Protected_Present (Access_To_Subprogram_Definition (Acc))
1100 then
1101 Set_Etype
1102 (Desig_Type,
1103 Replace_Anonymous_Access_To_Protected_Subprogram
1104 (T_Def));
1106 else
1107 Set_Etype
1108 (Desig_Type,
1109 Access_Definition (T_Def, Result_Definition (T_Def)));
1110 end if;
1111 end;
1113 else
1114 Analyze (Result_Definition (T_Def));
1116 declare
1117 Typ : constant Entity_Id := Entity (Result_Definition (T_Def));
1119 begin
1120 -- If a null exclusion is imposed on the result type, then
1121 -- create a null-excluding itype (an access subtype) and use
1122 -- it as the function's Etype.
1124 if Is_Access_Type (Typ)
1125 and then Null_Exclusion_In_Return_Present (T_Def)
1126 then
1127 Set_Etype (Desig_Type,
1128 Create_Null_Excluding_Itype
1129 (T => Typ,
1130 Related_Nod => T_Def,
1131 Scope_Id => Current_Scope));
1133 else
1134 if From_With_Type (Typ) then
1136 -- AI05-151: Incomplete types are allowed in all basic
1137 -- declarations, including access to subprograms.
1139 if Ada_Version >= Ada_2012 then
1140 null;
1142 else
1143 Error_Msg_NE
1144 ("illegal use of incomplete type&",
1145 Result_Definition (T_Def), Typ);
1146 end if;
1148 elsif Ekind (Current_Scope) = E_Package
1149 and then In_Private_Part (Current_Scope)
1150 then
1151 if Ekind (Typ) = E_Incomplete_Type then
1152 Append_Elmt (Desig_Type, Private_Dependents (Typ));
1154 elsif Is_Class_Wide_Type (Typ)
1155 and then Ekind (Etype (Typ)) = E_Incomplete_Type
1156 then
1157 Append_Elmt
1158 (Desig_Type, Private_Dependents (Etype (Typ)));
1159 end if;
1160 end if;
1162 Set_Etype (Desig_Type, Typ);
1163 end if;
1164 end;
1165 end if;
1167 if not (Is_Type (Etype (Desig_Type))) then
1168 Error_Msg_N
1169 ("expect type in function specification",
1170 Result_Definition (T_Def));
1171 end if;
1173 else
1174 Set_Etype (Desig_Type, Standard_Void_Type);
1175 end if;
1177 if Present (Formals) then
1178 Push_Scope (Desig_Type);
1180 -- A bit of a kludge here. These kludges will be removed when Itypes
1181 -- have proper parent pointers to their declarations???
1183 -- Kludge 1) Link defining_identifier of formals. Required by
1184 -- First_Formal to provide its functionality.
1186 declare
1187 F : Node_Id;
1189 begin
1190 F := First (Formals);
1192 -- In ASIS mode, the access_to_subprogram may be analyzed twice,
1193 -- when it is part of an unconstrained type and subtype expansion
1194 -- is disabled. To avoid back-end problems with shared profiles,
1195 -- use previous subprogram type as the designated type.
1197 if ASIS_Mode
1198 and then Present (Scope (Defining_Identifier (F)))
1199 then
1200 Set_Etype (T_Name, T_Name);
1201 Init_Size_Align (T_Name);
1202 Set_Directly_Designated_Type (T_Name,
1203 Scope (Defining_Identifier (F)));
1204 return;
1205 end if;
1207 while Present (F) loop
1208 if No (Parent (Defining_Identifier (F))) then
1209 Set_Parent (Defining_Identifier (F), F);
1210 end if;
1212 Next (F);
1213 end loop;
1214 end;
1216 Process_Formals (Formals, Parent (T_Def));
1218 -- Kludge 2) End_Scope requires that the parent pointer be set to
1219 -- something reasonable, but Itypes don't have parent pointers. So
1220 -- we set it and then unset it ???
1222 Set_Parent (Desig_Type, T_Name);
1223 End_Scope;
1224 Set_Parent (Desig_Type, Empty);
1225 end if;
1227 -- Check for premature usage of the type being defined
1229 Check_For_Premature_Usage (T_Def);
1231 -- The return type and/or any parameter type may be incomplete. Mark
1232 -- the subprogram_type as depending on the incomplete type, so that
1233 -- it can be updated when the full type declaration is seen. This
1234 -- only applies to incomplete types declared in some enclosing scope,
1235 -- not to limited views from other packages.
1237 if Present (Formals) then
1238 Formal := First_Formal (Desig_Type);
1239 while Present (Formal) loop
1240 if Ekind (Formal) /= E_In_Parameter
1241 and then Nkind (T_Def) = N_Access_Function_Definition
1242 then
1243 Error_Msg_N ("functions can only have IN parameters", Formal);
1244 end if;
1246 if Ekind (Etype (Formal)) = E_Incomplete_Type
1247 and then In_Open_Scopes (Scope (Etype (Formal)))
1248 then
1249 Append_Elmt (Desig_Type, Private_Dependents (Etype (Formal)));
1250 Set_Has_Delayed_Freeze (Desig_Type);
1251 end if;
1253 Next_Formal (Formal);
1254 end loop;
1255 end if;
1257 -- If the return type is incomplete, this is legal as long as the
1258 -- type is declared in the current scope and will be completed in
1259 -- it (rather than being part of limited view).
1261 if Ekind (Etype (Desig_Type)) = E_Incomplete_Type
1262 and then not Has_Delayed_Freeze (Desig_Type)
1263 and then In_Open_Scopes (Scope (Etype (Desig_Type)))
1264 then
1265 Append_Elmt (Desig_Type, Private_Dependents (Etype (Desig_Type)));
1266 Set_Has_Delayed_Freeze (Desig_Type);
1267 end if;
1269 Check_Delayed_Subprogram (Desig_Type);
1271 if Protected_Present (T_Def) then
1272 Set_Ekind (T_Name, E_Access_Protected_Subprogram_Type);
1273 Set_Convention (Desig_Type, Convention_Protected);
1274 else
1275 Set_Ekind (T_Name, E_Access_Subprogram_Type);
1276 end if;
1278 Set_Can_Use_Internal_Rep (T_Name, not Always_Compatible_Rep_On_Target);
1280 Set_Etype (T_Name, T_Name);
1281 Init_Size_Align (T_Name);
1282 Set_Directly_Designated_Type (T_Name, Desig_Type);
1284 -- Ada 2005 (AI-231): Propagate the null-excluding attribute
1286 Set_Can_Never_Be_Null (T_Name, Null_Exclusion_Present (T_Def));
1288 Check_Restriction (No_Access_Subprograms, T_Def);
1289 end Access_Subprogram_Declaration;
1291 ----------------------------
1292 -- Access_Type_Declaration --
1293 ----------------------------
1295 procedure Access_Type_Declaration (T : Entity_Id; Def : Node_Id) is
1296 P : constant Node_Id := Parent (Def);
1297 S : constant Node_Id := Subtype_Indication (Def);
1299 Full_Desig : Entity_Id;
1301 begin
1302 Check_SPARK_Restriction ("access type is not allowed", Def);
1304 -- Check for permissible use of incomplete type
1306 if Nkind (S) /= N_Subtype_Indication then
1307 Analyze (S);
1309 if Ekind (Root_Type (Entity (S))) = E_Incomplete_Type then
1310 Set_Directly_Designated_Type (T, Entity (S));
1311 else
1312 Set_Directly_Designated_Type (T,
1313 Process_Subtype (S, P, T, 'P'));
1314 end if;
1316 else
1317 Set_Directly_Designated_Type (T,
1318 Process_Subtype (S, P, T, 'P'));
1319 end if;
1321 if All_Present (Def) or Constant_Present (Def) then
1322 Set_Ekind (T, E_General_Access_Type);
1323 else
1324 Set_Ekind (T, E_Access_Type);
1325 end if;
1327 Full_Desig := Designated_Type (T);
1329 if Base_Type (Full_Desig) = T then
1330 Error_Msg_N ("access type cannot designate itself", S);
1332 -- In Ada 2005, the type may have a limited view through some unit
1333 -- in its own context, allowing the following circularity that cannot
1334 -- be detected earlier
1336 elsif Is_Class_Wide_Type (Full_Desig)
1337 and then Etype (Full_Desig) = T
1338 then
1339 Error_Msg_N
1340 ("access type cannot designate its own classwide type", S);
1342 -- Clean up indication of tagged status to prevent cascaded errors
1344 Set_Is_Tagged_Type (T, False);
1345 end if;
1347 Set_Etype (T, T);
1349 -- If the type has appeared already in a with_type clause, it is
1350 -- frozen and the pointer size is already set. Else, initialize.
1352 if not From_With_Type (T) then
1353 Init_Size_Align (T);
1354 end if;
1356 -- Note that Has_Task is always false, since the access type itself
1357 -- is not a task type. See Einfo for more description on this point.
1358 -- Exactly the same consideration applies to Has_Controlled_Component.
1360 Set_Has_Task (T, False);
1361 Set_Has_Controlled_Component (T, False);
1363 -- Initialize field Finalization_Master explicitly to Empty, to avoid
1364 -- problems where an incomplete view of this entity has been previously
1365 -- established by a limited with and an overlaid version of this field
1366 -- (Stored_Constraint) was initialized for the incomplete view.
1368 -- This reset is performed in most cases except where the access type
1369 -- has been created for the purposes of allocating or deallocating a
1370 -- build-in-place object. Such access types have explicitly set pools
1371 -- and finalization masters.
1373 if No (Associated_Storage_Pool (T)) then
1374 Set_Finalization_Master (T, Empty);
1375 end if;
1377 -- Ada 2005 (AI-231): Propagate the null-excluding and access-constant
1378 -- attributes
1380 Set_Can_Never_Be_Null (T, Null_Exclusion_Present (Def));
1381 Set_Is_Access_Constant (T, Constant_Present (Def));
1382 end Access_Type_Declaration;
1384 ----------------------------------
1385 -- Add_Interface_Tag_Components --
1386 ----------------------------------
1388 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id) is
1389 Loc : constant Source_Ptr := Sloc (N);
1390 L : List_Id;
1391 Last_Tag : Node_Id;
1393 procedure Add_Tag (Iface : Entity_Id);
1394 -- Add tag for one of the progenitor interfaces
1396 -------------
1397 -- Add_Tag --
1398 -------------
1400 procedure Add_Tag (Iface : Entity_Id) is
1401 Decl : Node_Id;
1402 Def : Node_Id;
1403 Tag : Entity_Id;
1404 Offset : Entity_Id;
1406 begin
1407 pragma Assert (Is_Tagged_Type (Iface)
1408 and then Is_Interface (Iface));
1410 -- This is a reasonable place to propagate predicates
1412 if Has_Predicates (Iface) then
1413 Set_Has_Predicates (Typ);
1414 end if;
1416 Def :=
1417 Make_Component_Definition (Loc,
1418 Aliased_Present => True,
1419 Subtype_Indication =>
1420 New_Occurrence_Of (RTE (RE_Interface_Tag), Loc));
1422 Tag := Make_Temporary (Loc, 'V');
1424 Decl :=
1425 Make_Component_Declaration (Loc,
1426 Defining_Identifier => Tag,
1427 Component_Definition => Def);
1429 Analyze_Component_Declaration (Decl);
1431 Set_Analyzed (Decl);
1432 Set_Ekind (Tag, E_Component);
1433 Set_Is_Tag (Tag);
1434 Set_Is_Aliased (Tag);
1435 Set_Related_Type (Tag, Iface);
1436 Init_Component_Location (Tag);
1438 pragma Assert (Is_Frozen (Iface));
1440 Set_DT_Entry_Count (Tag,
1441 DT_Entry_Count (First_Entity (Iface)));
1443 if No (Last_Tag) then
1444 Prepend (Decl, L);
1445 else
1446 Insert_After (Last_Tag, Decl);
1447 end if;
1449 Last_Tag := Decl;
1451 -- If the ancestor has discriminants we need to give special support
1452 -- to store the offset_to_top value of the secondary dispatch tables.
1453 -- For this purpose we add a supplementary component just after the
1454 -- field that contains the tag associated with each secondary DT.
1456 if Typ /= Etype (Typ)
1457 and then Has_Discriminants (Etype (Typ))
1458 then
1459 Def :=
1460 Make_Component_Definition (Loc,
1461 Subtype_Indication =>
1462 New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
1464 Offset := Make_Temporary (Loc, 'V');
1466 Decl :=
1467 Make_Component_Declaration (Loc,
1468 Defining_Identifier => Offset,
1469 Component_Definition => Def);
1471 Analyze_Component_Declaration (Decl);
1473 Set_Analyzed (Decl);
1474 Set_Ekind (Offset, E_Component);
1475 Set_Is_Aliased (Offset);
1476 Set_Related_Type (Offset, Iface);
1477 Init_Component_Location (Offset);
1478 Insert_After (Last_Tag, Decl);
1479 Last_Tag := Decl;
1480 end if;
1481 end Add_Tag;
1483 -- Local variables
1485 Elmt : Elmt_Id;
1486 Ext : Node_Id;
1487 Comp : Node_Id;
1489 -- Start of processing for Add_Interface_Tag_Components
1491 begin
1492 if not RTE_Available (RE_Interface_Tag) then
1493 Error_Msg
1494 ("(Ada 2005) interface types not supported by this run-time!",
1495 Sloc (N));
1496 return;
1497 end if;
1499 if Ekind (Typ) /= E_Record_Type
1500 or else (Is_Concurrent_Record_Type (Typ)
1501 and then Is_Empty_List (Abstract_Interface_List (Typ)))
1502 or else (not Is_Concurrent_Record_Type (Typ)
1503 and then No (Interfaces (Typ))
1504 and then Is_Empty_Elmt_List (Interfaces (Typ)))
1505 then
1506 return;
1507 end if;
1509 -- Find the current last tag
1511 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1512 Ext := Record_Extension_Part (Type_Definition (N));
1513 else
1514 pragma Assert (Nkind (Type_Definition (N)) = N_Record_Definition);
1515 Ext := Type_Definition (N);
1516 end if;
1518 Last_Tag := Empty;
1520 if not (Present (Component_List (Ext))) then
1521 Set_Null_Present (Ext, False);
1522 L := New_List;
1523 Set_Component_List (Ext,
1524 Make_Component_List (Loc,
1525 Component_Items => L,
1526 Null_Present => False));
1527 else
1528 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1529 L := Component_Items
1530 (Component_List
1531 (Record_Extension_Part
1532 (Type_Definition (N))));
1533 else
1534 L := Component_Items
1535 (Component_List
1536 (Type_Definition (N)));
1537 end if;
1539 -- Find the last tag component
1541 Comp := First (L);
1542 while Present (Comp) loop
1543 if Nkind (Comp) = N_Component_Declaration
1544 and then Is_Tag (Defining_Identifier (Comp))
1545 then
1546 Last_Tag := Comp;
1547 end if;
1549 Next (Comp);
1550 end loop;
1551 end if;
1553 -- At this point L references the list of components and Last_Tag
1554 -- references the current last tag (if any). Now we add the tag
1555 -- corresponding with all the interfaces that are not implemented
1556 -- by the parent.
1558 if Present (Interfaces (Typ)) then
1559 Elmt := First_Elmt (Interfaces (Typ));
1560 while Present (Elmt) loop
1561 Add_Tag (Node (Elmt));
1562 Next_Elmt (Elmt);
1563 end loop;
1564 end if;
1565 end Add_Interface_Tag_Components;
1567 -------------------------------------
1568 -- Add_Internal_Interface_Entities --
1569 -------------------------------------
1571 procedure Add_Internal_Interface_Entities (Tagged_Type : Entity_Id) is
1572 Elmt : Elmt_Id;
1573 Iface : Entity_Id;
1574 Iface_Elmt : Elmt_Id;
1575 Iface_Prim : Entity_Id;
1576 Ifaces_List : Elist_Id;
1577 New_Subp : Entity_Id := Empty;
1578 Prim : Entity_Id;
1579 Restore_Scope : Boolean := False;
1581 begin
1582 pragma Assert (Ada_Version >= Ada_2005
1583 and then Is_Record_Type (Tagged_Type)
1584 and then Is_Tagged_Type (Tagged_Type)
1585 and then Has_Interfaces (Tagged_Type)
1586 and then not Is_Interface (Tagged_Type));
1588 -- Ensure that the internal entities are added to the scope of the type
1590 if Scope (Tagged_Type) /= Current_Scope then
1591 Push_Scope (Scope (Tagged_Type));
1592 Restore_Scope := True;
1593 end if;
1595 Collect_Interfaces (Tagged_Type, Ifaces_List);
1597 Iface_Elmt := First_Elmt (Ifaces_List);
1598 while Present (Iface_Elmt) loop
1599 Iface := Node (Iface_Elmt);
1601 -- Originally we excluded here from this processing interfaces that
1602 -- are parents of Tagged_Type because their primitives are located
1603 -- in the primary dispatch table (and hence no auxiliary internal
1604 -- entities are required to handle secondary dispatch tables in such
1605 -- case). However, these auxiliary entities are also required to
1606 -- handle derivations of interfaces in formals of generics (see
1607 -- Derive_Subprograms).
1609 Elmt := First_Elmt (Primitive_Operations (Iface));
1610 while Present (Elmt) loop
1611 Iface_Prim := Node (Elmt);
1613 if not Is_Predefined_Dispatching_Operation (Iface_Prim) then
1614 Prim :=
1615 Find_Primitive_Covering_Interface
1616 (Tagged_Type => Tagged_Type,
1617 Iface_Prim => Iface_Prim);
1619 if No (Prim) and then Serious_Errors_Detected > 0 then
1620 goto Continue;
1621 end if;
1623 pragma Assert (Present (Prim));
1625 -- Ada 2012 (AI05-0197): If the name of the covering primitive
1626 -- differs from the name of the interface primitive then it is
1627 -- a private primitive inherited from a parent type. In such
1628 -- case, given that Tagged_Type covers the interface, the
1629 -- inherited private primitive becomes visible. For such
1630 -- purpose we add a new entity that renames the inherited
1631 -- private primitive.
1633 if Chars (Prim) /= Chars (Iface_Prim) then
1634 pragma Assert (Has_Suffix (Prim, 'P'));
1635 Derive_Subprogram
1636 (New_Subp => New_Subp,
1637 Parent_Subp => Iface_Prim,
1638 Derived_Type => Tagged_Type,
1639 Parent_Type => Iface);
1640 Set_Alias (New_Subp, Prim);
1641 Set_Is_Abstract_Subprogram
1642 (New_Subp, Is_Abstract_Subprogram (Prim));
1643 end if;
1645 Derive_Subprogram
1646 (New_Subp => New_Subp,
1647 Parent_Subp => Iface_Prim,
1648 Derived_Type => Tagged_Type,
1649 Parent_Type => Iface);
1651 -- Ada 2005 (AI-251): Decorate internal entity Iface_Subp
1652 -- associated with interface types. These entities are
1653 -- only registered in the list of primitives of its
1654 -- corresponding tagged type because they are only used
1655 -- to fill the contents of the secondary dispatch tables.
1656 -- Therefore they are removed from the homonym chains.
1658 Set_Is_Hidden (New_Subp);
1659 Set_Is_Internal (New_Subp);
1660 Set_Alias (New_Subp, Prim);
1661 Set_Is_Abstract_Subprogram
1662 (New_Subp, Is_Abstract_Subprogram (Prim));
1663 Set_Interface_Alias (New_Subp, Iface_Prim);
1665 -- Internal entities associated with interface types are
1666 -- only registered in the list of primitives of the tagged
1667 -- type. They are only used to fill the contents of the
1668 -- secondary dispatch tables. Therefore they are not needed
1669 -- in the homonym chains.
1671 Remove_Homonym (New_Subp);
1673 -- Hidden entities associated with interfaces must have set
1674 -- the Has_Delay_Freeze attribute to ensure that, in case of
1675 -- locally defined tagged types (or compiling with static
1676 -- dispatch tables generation disabled) the corresponding
1677 -- entry of the secondary dispatch table is filled when
1678 -- such an entity is frozen.
1680 Set_Has_Delayed_Freeze (New_Subp);
1681 end if;
1683 <<Continue>>
1684 Next_Elmt (Elmt);
1685 end loop;
1687 Next_Elmt (Iface_Elmt);
1688 end loop;
1690 if Restore_Scope then
1691 Pop_Scope;
1692 end if;
1693 end Add_Internal_Interface_Entities;
1695 -----------------------------------
1696 -- Analyze_Component_Declaration --
1697 -----------------------------------
1699 procedure Analyze_Component_Declaration (N : Node_Id) is
1700 Id : constant Entity_Id := Defining_Identifier (N);
1701 E : constant Node_Id := Expression (N);
1702 Typ : constant Node_Id :=
1703 Subtype_Indication (Component_Definition (N));
1704 T : Entity_Id;
1705 P : Entity_Id;
1707 function Contains_POC (Constr : Node_Id) return Boolean;
1708 -- Determines whether a constraint uses the discriminant of a record
1709 -- type thus becoming a per-object constraint (POC).
1711 function Is_Known_Limited (Typ : Entity_Id) return Boolean;
1712 -- Typ is the type of the current component, check whether this type is
1713 -- a limited type. Used to validate declaration against that of
1714 -- enclosing record.
1716 ------------------
1717 -- Contains_POC --
1718 ------------------
1720 function Contains_POC (Constr : Node_Id) return Boolean is
1721 begin
1722 -- Prevent cascaded errors
1724 if Error_Posted (Constr) then
1725 return False;
1726 end if;
1728 case Nkind (Constr) is
1729 when N_Attribute_Reference =>
1730 return
1731 Attribute_Name (Constr) = Name_Access
1732 and then Prefix (Constr) = Scope (Entity (Prefix (Constr)));
1734 when N_Discriminant_Association =>
1735 return Denotes_Discriminant (Expression (Constr));
1737 when N_Identifier =>
1738 return Denotes_Discriminant (Constr);
1740 when N_Index_Or_Discriminant_Constraint =>
1741 declare
1742 IDC : Node_Id;
1744 begin
1745 IDC := First (Constraints (Constr));
1746 while Present (IDC) loop
1748 -- One per-object constraint is sufficient
1750 if Contains_POC (IDC) then
1751 return True;
1752 end if;
1754 Next (IDC);
1755 end loop;
1757 return False;
1758 end;
1760 when N_Range =>
1761 return Denotes_Discriminant (Low_Bound (Constr))
1762 or else
1763 Denotes_Discriminant (High_Bound (Constr));
1765 when N_Range_Constraint =>
1766 return Denotes_Discriminant (Range_Expression (Constr));
1768 when others =>
1769 return False;
1771 end case;
1772 end Contains_POC;
1774 ----------------------
1775 -- Is_Known_Limited --
1776 ----------------------
1778 function Is_Known_Limited (Typ : Entity_Id) return Boolean is
1779 P : constant Entity_Id := Etype (Typ);
1780 R : constant Entity_Id := Root_Type (Typ);
1782 begin
1783 if Is_Limited_Record (Typ) then
1784 return True;
1786 -- If the root type is limited (and not a limited interface)
1787 -- so is the current type
1789 elsif Is_Limited_Record (R)
1790 and then
1791 (not Is_Interface (R)
1792 or else not Is_Limited_Interface (R))
1793 then
1794 return True;
1796 -- Else the type may have a limited interface progenitor, but a
1797 -- limited record parent.
1799 elsif R /= P
1800 and then Is_Limited_Record (P)
1801 then
1802 return True;
1804 else
1805 return False;
1806 end if;
1807 end Is_Known_Limited;
1809 -- Start of processing for Analyze_Component_Declaration
1811 begin
1812 Generate_Definition (Id);
1813 Enter_Name (Id);
1815 if Present (Typ) then
1816 T := Find_Type_Of_Object
1817 (Subtype_Indication (Component_Definition (N)), N);
1819 if not Nkind_In (Typ, N_Identifier, N_Expanded_Name) then
1820 Check_SPARK_Restriction ("subtype mark required", Typ);
1821 end if;
1823 -- Ada 2005 (AI-230): Access Definition case
1825 else
1826 pragma Assert (Present
1827 (Access_Definition (Component_Definition (N))));
1829 T := Access_Definition
1830 (Related_Nod => N,
1831 N => Access_Definition (Component_Definition (N)));
1832 Set_Is_Local_Anonymous_Access (T);
1834 -- Ada 2005 (AI-254)
1836 if Present (Access_To_Subprogram_Definition
1837 (Access_Definition (Component_Definition (N))))
1838 and then Protected_Present (Access_To_Subprogram_Definition
1839 (Access_Definition
1840 (Component_Definition (N))))
1841 then
1842 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1843 end if;
1844 end if;
1846 -- If the subtype is a constrained subtype of the enclosing record,
1847 -- (which must have a partial view) the back-end does not properly
1848 -- handle the recursion. Rewrite the component declaration with an
1849 -- explicit subtype indication, which is acceptable to Gigi. We can copy
1850 -- the tree directly because side effects have already been removed from
1851 -- discriminant constraints.
1853 if Ekind (T) = E_Access_Subtype
1854 and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
1855 and then Comes_From_Source (T)
1856 and then Nkind (Parent (T)) = N_Subtype_Declaration
1857 and then Etype (Directly_Designated_Type (T)) = Current_Scope
1858 then
1859 Rewrite
1860 (Subtype_Indication (Component_Definition (N)),
1861 New_Copy_Tree (Subtype_Indication (Parent (T))));
1862 T := Find_Type_Of_Object
1863 (Subtype_Indication (Component_Definition (N)), N);
1864 end if;
1866 -- If the component declaration includes a default expression, then we
1867 -- check that the component is not of a limited type (RM 3.7(5)),
1868 -- and do the special preanalysis of the expression (see section on
1869 -- "Handling of Default and Per-Object Expressions" in the spec of
1870 -- package Sem).
1872 if Present (E) then
1873 Check_SPARK_Restriction ("default expression is not allowed", E);
1874 Preanalyze_Spec_Expression (E, T);
1875 Check_Initialization (T, E);
1877 if Ada_Version >= Ada_2005
1878 and then Ekind (T) = E_Anonymous_Access_Type
1879 and then Etype (E) /= Any_Type
1880 then
1881 -- Check RM 3.9.2(9): "if the expected type for an expression is
1882 -- an anonymous access-to-specific tagged type, then the object
1883 -- designated by the expression shall not be dynamically tagged
1884 -- unless it is a controlling operand in a call on a dispatching
1885 -- operation"
1887 if Is_Tagged_Type (Directly_Designated_Type (T))
1888 and then
1889 Ekind (Directly_Designated_Type (T)) /= E_Class_Wide_Type
1890 and then
1891 Ekind (Directly_Designated_Type (Etype (E))) =
1892 E_Class_Wide_Type
1893 then
1894 Error_Msg_N
1895 ("access to specific tagged type required (RM 3.9.2(9))", E);
1896 end if;
1898 -- (Ada 2005: AI-230): Accessibility check for anonymous
1899 -- components
1901 if Type_Access_Level (Etype (E)) >
1902 Deepest_Type_Access_Level (T)
1903 then
1904 Error_Msg_N
1905 ("expression has deeper access level than component " &
1906 "(RM 3.10.2 (12.2))", E);
1907 end if;
1909 -- The initialization expression is a reference to an access
1910 -- discriminant. The type of the discriminant is always deeper
1911 -- than any access type.
1913 if Ekind (Etype (E)) = E_Anonymous_Access_Type
1914 and then Is_Entity_Name (E)
1915 and then Ekind (Entity (E)) = E_In_Parameter
1916 and then Present (Discriminal_Link (Entity (E)))
1917 then
1918 Error_Msg_N
1919 ("discriminant has deeper accessibility level than target",
1921 end if;
1922 end if;
1923 end if;
1925 -- The parent type may be a private view with unknown discriminants,
1926 -- and thus unconstrained. Regular components must be constrained.
1928 if Is_Indefinite_Subtype (T) and then Chars (Id) /= Name_uParent then
1929 if Is_Class_Wide_Type (T) then
1930 Error_Msg_N
1931 ("class-wide subtype with unknown discriminants" &
1932 " in component declaration",
1933 Subtype_Indication (Component_Definition (N)));
1934 else
1935 Error_Msg_N
1936 ("unconstrained subtype in component declaration",
1937 Subtype_Indication (Component_Definition (N)));
1938 end if;
1940 -- Components cannot be abstract, except for the special case of
1941 -- the _Parent field (case of extending an abstract tagged type)
1943 elsif Is_Abstract_Type (T) and then Chars (Id) /= Name_uParent then
1944 Error_Msg_N ("type of a component cannot be abstract", N);
1945 end if;
1947 Set_Etype (Id, T);
1948 Set_Is_Aliased (Id, Aliased_Present (Component_Definition (N)));
1950 -- The component declaration may have a per-object constraint, set
1951 -- the appropriate flag in the defining identifier of the subtype.
1953 if Present (Subtype_Indication (Component_Definition (N))) then
1954 declare
1955 Sindic : constant Node_Id :=
1956 Subtype_Indication (Component_Definition (N));
1957 begin
1958 if Nkind (Sindic) = N_Subtype_Indication
1959 and then Present (Constraint (Sindic))
1960 and then Contains_POC (Constraint (Sindic))
1961 then
1962 Set_Has_Per_Object_Constraint (Id);
1963 end if;
1964 end;
1965 end if;
1967 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
1968 -- out some static checks.
1970 if Ada_Version >= Ada_2005
1971 and then Can_Never_Be_Null (T)
1972 then
1973 Null_Exclusion_Static_Checks (N);
1974 end if;
1976 -- If this component is private (or depends on a private type), flag the
1977 -- record type to indicate that some operations are not available.
1979 P := Private_Component (T);
1981 if Present (P) then
1983 -- Check for circular definitions
1985 if P = Any_Type then
1986 Set_Etype (Id, Any_Type);
1988 -- There is a gap in the visibility of operations only if the
1989 -- component type is not defined in the scope of the record type.
1991 elsif Scope (P) = Scope (Current_Scope) then
1992 null;
1994 elsif Is_Limited_Type (P) then
1995 Set_Is_Limited_Composite (Current_Scope);
1997 else
1998 Set_Is_Private_Composite (Current_Scope);
1999 end if;
2000 end if;
2002 if P /= Any_Type
2003 and then Is_Limited_Type (T)
2004 and then Chars (Id) /= Name_uParent
2005 and then Is_Tagged_Type (Current_Scope)
2006 then
2007 if Is_Derived_Type (Current_Scope)
2008 and then not Is_Known_Limited (Current_Scope)
2009 then
2010 Error_Msg_N
2011 ("extension of nonlimited type cannot have limited components",
2014 if Is_Interface (Root_Type (Current_Scope)) then
2015 Error_Msg_N
2016 ("\limitedness is not inherited from limited interface", N);
2017 Error_Msg_N ("\add LIMITED to type indication", N);
2018 end if;
2020 Explain_Limited_Type (T, N);
2021 Set_Etype (Id, Any_Type);
2022 Set_Is_Limited_Composite (Current_Scope, False);
2024 elsif not Is_Derived_Type (Current_Scope)
2025 and then not Is_Limited_Record (Current_Scope)
2026 and then not Is_Concurrent_Type (Current_Scope)
2027 then
2028 Error_Msg_N
2029 ("nonlimited tagged type cannot have limited components", N);
2030 Explain_Limited_Type (T, N);
2031 Set_Etype (Id, Any_Type);
2032 Set_Is_Limited_Composite (Current_Scope, False);
2033 end if;
2034 end if;
2036 Set_Original_Record_Component (Id, Id);
2038 if Has_Aspects (N) then
2039 Analyze_Aspect_Specifications (N, Id);
2040 end if;
2042 Analyze_Dimension (N);
2043 end Analyze_Component_Declaration;
2045 --------------------------
2046 -- Analyze_Declarations --
2047 --------------------------
2049 procedure Analyze_Declarations (L : List_Id) is
2050 D : Node_Id;
2051 Freeze_From : Entity_Id := Empty;
2052 Next_Node : Node_Id;
2054 procedure Adjust_D;
2055 -- Adjust D not to include implicit label declarations, since these
2056 -- have strange Sloc values that result in elaboration check problems.
2057 -- (They have the sloc of the label as found in the source, and that
2058 -- is ahead of the current declarative part).
2060 --------------
2061 -- Adjust_D --
2062 --------------
2064 procedure Adjust_D is
2065 begin
2066 while Present (Prev (D))
2067 and then Nkind (D) = N_Implicit_Label_Declaration
2068 loop
2069 Prev (D);
2070 end loop;
2071 end Adjust_D;
2073 -- Start of processing for Analyze_Declarations
2075 begin
2076 if Restriction_Check_Required (SPARK) then
2077 Check_Later_Vs_Basic_Declarations (L, During_Parsing => False);
2078 end if;
2080 D := First (L);
2081 while Present (D) loop
2083 -- Package spec cannot contain a package declaration in SPARK
2085 if Nkind (D) = N_Package_Declaration
2086 and then Nkind (Parent (L)) = N_Package_Specification
2087 then
2088 Check_SPARK_Restriction
2089 ("package specification cannot contain a package declaration",
2091 end if;
2093 -- Complete analysis of declaration
2095 Analyze (D);
2096 Next_Node := Next (D);
2098 if No (Freeze_From) then
2099 Freeze_From := First_Entity (Current_Scope);
2100 end if;
2102 -- At the end of a declarative part, freeze remaining entities
2103 -- declared in it. The end of the visible declarations of package
2104 -- specification is not the end of a declarative part if private
2105 -- declarations are present. The end of a package declaration is a
2106 -- freezing point only if it a library package. A task definition or
2107 -- protected type definition is not a freeze point either. Finally,
2108 -- we do not freeze entities in generic scopes, because there is no
2109 -- code generated for them and freeze nodes will be generated for
2110 -- the instance.
2112 -- The end of a package instantiation is not a freeze point, but
2113 -- for now we make it one, because the generic body is inserted
2114 -- (currently) immediately after. Generic instantiations will not
2115 -- be a freeze point once delayed freezing of bodies is implemented.
2116 -- (This is needed in any case for early instantiations ???).
2118 if No (Next_Node) then
2119 if Nkind_In (Parent (L), N_Component_List,
2120 N_Task_Definition,
2121 N_Protected_Definition)
2122 then
2123 null;
2125 elsif Nkind (Parent (L)) /= N_Package_Specification then
2126 if Nkind (Parent (L)) = N_Package_Body then
2127 Freeze_From := First_Entity (Current_Scope);
2128 end if;
2130 Adjust_D;
2131 Freeze_All (Freeze_From, D);
2132 Freeze_From := Last_Entity (Current_Scope);
2134 elsif Scope (Current_Scope) /= Standard_Standard
2135 and then not Is_Child_Unit (Current_Scope)
2136 and then No (Generic_Parent (Parent (L)))
2137 then
2138 null;
2140 elsif L /= Visible_Declarations (Parent (L))
2141 or else No (Private_Declarations (Parent (L)))
2142 or else Is_Empty_List (Private_Declarations (Parent (L)))
2143 then
2144 Adjust_D;
2145 Freeze_All (Freeze_From, D);
2146 Freeze_From := Last_Entity (Current_Scope);
2147 end if;
2149 -- If next node is a body then freeze all types before the body.
2150 -- An exception occurs for some expander-generated bodies. If these
2151 -- are generated at places where in general language rules would not
2152 -- allow a freeze point, then we assume that the expander has
2153 -- explicitly checked that all required types are properly frozen,
2154 -- and we do not cause general freezing here. This special circuit
2155 -- is used when the encountered body is marked as having already
2156 -- been analyzed.
2158 -- In all other cases (bodies that come from source, and expander
2159 -- generated bodies that have not been analyzed yet), freeze all
2160 -- types now. Note that in the latter case, the expander must take
2161 -- care to attach the bodies at a proper place in the tree so as to
2162 -- not cause unwanted freezing at that point.
2164 elsif not Analyzed (Next_Node)
2165 and then (Nkind_In (Next_Node, N_Subprogram_Body,
2166 N_Entry_Body,
2167 N_Package_Body,
2168 N_Protected_Body,
2169 N_Task_Body)
2170 or else
2171 Nkind (Next_Node) in N_Body_Stub)
2172 then
2173 Adjust_D;
2174 Freeze_All (Freeze_From, D);
2175 Freeze_From := Last_Entity (Current_Scope);
2176 end if;
2178 D := Next_Node;
2179 end loop;
2181 -- One more thing to do, we need to scan the declarations to check
2182 -- for any precondition/postcondition pragmas (Pre/Post aspects have
2183 -- by this stage been converted into corresponding pragmas). It is
2184 -- at this point that we analyze the expressions in such pragmas,
2185 -- to implement the delayed visibility requirement.
2187 declare
2188 Decl : Node_Id;
2189 Spec : Node_Id;
2190 Sent : Entity_Id;
2191 Prag : Node_Id;
2193 begin
2194 Decl := First (L);
2195 while Present (Decl) loop
2196 if Nkind (Original_Node (Decl)) = N_Subprogram_Declaration then
2197 Spec := Specification (Original_Node (Decl));
2198 Sent := Defining_Unit_Name (Spec);
2200 -- Analyze preconditions and postconditions
2202 Prag := Spec_PPC_List (Contract (Sent));
2203 while Present (Prag) loop
2204 Analyze_PPC_In_Decl_Part (Prag, Sent);
2205 Prag := Next_Pragma (Prag);
2206 end loop;
2208 -- Analyze contract-cases and test-cases
2210 Prag := Spec_CTC_List (Contract (Sent));
2211 while Present (Prag) loop
2212 Analyze_CTC_In_Decl_Part (Prag, Sent);
2213 Prag := Next_Pragma (Prag);
2214 end loop;
2216 -- At this point, entities have been attached to identifiers.
2217 -- This is required to be able to detect suspicious contracts.
2219 Check_Subprogram_Contract (Sent);
2220 end if;
2222 Next (Decl);
2223 end loop;
2224 end;
2225 end Analyze_Declarations;
2227 -----------------------------------
2228 -- Analyze_Full_Type_Declaration --
2229 -----------------------------------
2231 procedure Analyze_Full_Type_Declaration (N : Node_Id) is
2232 Def : constant Node_Id := Type_Definition (N);
2233 Def_Id : constant Entity_Id := Defining_Identifier (N);
2234 T : Entity_Id;
2235 Prev : Entity_Id;
2237 Is_Remote : constant Boolean :=
2238 (Is_Remote_Types (Current_Scope)
2239 or else Is_Remote_Call_Interface (Current_Scope))
2240 and then not (In_Private_Part (Current_Scope)
2241 or else In_Package_Body (Current_Scope));
2243 procedure Check_Ops_From_Incomplete_Type;
2244 -- If there is a tagged incomplete partial view of the type, traverse
2245 -- the primitives of the incomplete view and change the type of any
2246 -- controlling formals and result to indicate the full view. The
2247 -- primitives will be added to the full type's primitive operations
2248 -- list later in Sem_Disp.Check_Operation_From_Incomplete_Type (which
2249 -- is called from Process_Incomplete_Dependents).
2251 ------------------------------------
2252 -- Check_Ops_From_Incomplete_Type --
2253 ------------------------------------
2255 procedure Check_Ops_From_Incomplete_Type is
2256 Elmt : Elmt_Id;
2257 Formal : Entity_Id;
2258 Op : Entity_Id;
2260 begin
2261 if Prev /= T
2262 and then Ekind (Prev) = E_Incomplete_Type
2263 and then Is_Tagged_Type (Prev)
2264 and then Is_Tagged_Type (T)
2265 then
2266 Elmt := First_Elmt (Primitive_Operations (Prev));
2267 while Present (Elmt) loop
2268 Op := Node (Elmt);
2270 Formal := First_Formal (Op);
2271 while Present (Formal) loop
2272 if Etype (Formal) = Prev then
2273 Set_Etype (Formal, T);
2274 end if;
2276 Next_Formal (Formal);
2277 end loop;
2279 if Etype (Op) = Prev then
2280 Set_Etype (Op, T);
2281 end if;
2283 Next_Elmt (Elmt);
2284 end loop;
2285 end if;
2286 end Check_Ops_From_Incomplete_Type;
2288 -- Start of processing for Analyze_Full_Type_Declaration
2290 begin
2291 Prev := Find_Type_Name (N);
2293 -- The full view, if present, now points to the current type
2295 -- Ada 2005 (AI-50217): If the type was previously decorated when
2296 -- imported through a LIMITED WITH clause, it appears as incomplete
2297 -- but has no full view.
2299 if Ekind (Prev) = E_Incomplete_Type
2300 and then Present (Full_View (Prev))
2301 then
2302 T := Full_View (Prev);
2303 else
2304 T := Prev;
2305 end if;
2307 Set_Is_Pure (T, Is_Pure (Current_Scope));
2309 -- We set the flag Is_First_Subtype here. It is needed to set the
2310 -- corresponding flag for the Implicit class-wide-type created
2311 -- during tagged types processing.
2313 Set_Is_First_Subtype (T, True);
2315 -- Only composite types other than array types are allowed to have
2316 -- discriminants.
2318 case Nkind (Def) is
2320 -- For derived types, the rule will be checked once we've figured
2321 -- out the parent type.
2323 when N_Derived_Type_Definition =>
2324 null;
2326 -- For record types, discriminants are allowed, unless we are in
2327 -- SPARK.
2329 when N_Record_Definition =>
2330 if Present (Discriminant_Specifications (N)) then
2331 Check_SPARK_Restriction
2332 ("discriminant type is not allowed",
2333 Defining_Identifier
2334 (First (Discriminant_Specifications (N))));
2335 end if;
2337 when others =>
2338 if Present (Discriminant_Specifications (N)) then
2339 Error_Msg_N
2340 ("elementary or array type cannot have discriminants",
2341 Defining_Identifier
2342 (First (Discriminant_Specifications (N))));
2343 end if;
2344 end case;
2346 -- Elaborate the type definition according to kind, and generate
2347 -- subsidiary (implicit) subtypes where needed. We skip this if it was
2348 -- already done (this happens during the reanalysis that follows a call
2349 -- to the high level optimizer).
2351 if not Analyzed (T) then
2352 Set_Analyzed (T);
2354 case Nkind (Def) is
2356 when N_Access_To_Subprogram_Definition =>
2357 Access_Subprogram_Declaration (T, Def);
2359 -- If this is a remote access to subprogram, we must create the
2360 -- equivalent fat pointer type, and related subprograms.
2362 if Is_Remote then
2363 Process_Remote_AST_Declaration (N);
2364 end if;
2366 -- Validate categorization rule against access type declaration
2367 -- usually a violation in Pure unit, Shared_Passive unit.
2369 Validate_Access_Type_Declaration (T, N);
2371 when N_Access_To_Object_Definition =>
2372 Access_Type_Declaration (T, Def);
2374 -- Validate categorization rule against access type declaration
2375 -- usually a violation in Pure unit, Shared_Passive unit.
2377 Validate_Access_Type_Declaration (T, N);
2379 -- If we are in a Remote_Call_Interface package and define a
2380 -- RACW, then calling stubs and specific stream attributes
2381 -- must be added.
2383 if Is_Remote
2384 and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
2385 then
2386 Add_RACW_Features (Def_Id);
2387 end if;
2389 -- Set no strict aliasing flag if config pragma seen
2391 if Opt.No_Strict_Aliasing then
2392 Set_No_Strict_Aliasing (Base_Type (Def_Id));
2393 end if;
2395 when N_Array_Type_Definition =>
2396 Array_Type_Declaration (T, Def);
2398 when N_Derived_Type_Definition =>
2399 Derived_Type_Declaration (T, N, T /= Def_Id);
2401 when N_Enumeration_Type_Definition =>
2402 Enumeration_Type_Declaration (T, Def);
2404 when N_Floating_Point_Definition =>
2405 Floating_Point_Type_Declaration (T, Def);
2407 when N_Decimal_Fixed_Point_Definition =>
2408 Decimal_Fixed_Point_Type_Declaration (T, Def);
2410 when N_Ordinary_Fixed_Point_Definition =>
2411 Ordinary_Fixed_Point_Type_Declaration (T, Def);
2413 when N_Signed_Integer_Type_Definition =>
2414 Signed_Integer_Type_Declaration (T, Def);
2416 when N_Modular_Type_Definition =>
2417 Modular_Type_Declaration (T, Def);
2419 when N_Record_Definition =>
2420 Record_Type_Declaration (T, N, Prev);
2422 -- If declaration has a parse error, nothing to elaborate.
2424 when N_Error =>
2425 null;
2427 when others =>
2428 raise Program_Error;
2430 end case;
2431 end if;
2433 if Etype (T) = Any_Type then
2434 return;
2435 end if;
2437 -- Controlled type is not allowed in SPARK
2439 if Is_Visibly_Controlled (T) then
2440 Check_SPARK_Restriction ("controlled type is not allowed", N);
2441 end if;
2443 -- Some common processing for all types
2445 Set_Depends_On_Private (T, Has_Private_Component (T));
2446 Check_Ops_From_Incomplete_Type;
2448 -- Both the declared entity, and its anonymous base type if one
2449 -- was created, need freeze nodes allocated.
2451 declare
2452 B : constant Entity_Id := Base_Type (T);
2454 begin
2455 -- In the case where the base type differs from the first subtype, we
2456 -- pre-allocate a freeze node, and set the proper link to the first
2457 -- subtype. Freeze_Entity will use this preallocated freeze node when
2458 -- it freezes the entity.
2460 -- This does not apply if the base type is a generic type, whose
2461 -- declaration is independent of the current derived definition.
2463 if B /= T and then not Is_Generic_Type (B) then
2464 Ensure_Freeze_Node (B);
2465 Set_First_Subtype_Link (Freeze_Node (B), T);
2466 end if;
2468 -- A type that is imported through a limited_with clause cannot
2469 -- generate any code, and thus need not be frozen. However, an access
2470 -- type with an imported designated type needs a finalization list,
2471 -- which may be referenced in some other package that has non-limited
2472 -- visibility on the designated type. Thus we must create the
2473 -- finalization list at the point the access type is frozen, to
2474 -- prevent unsatisfied references at link time.
2476 if not From_With_Type (T) or else Is_Access_Type (T) then
2477 Set_Has_Delayed_Freeze (T);
2478 end if;
2479 end;
2481 -- Case where T is the full declaration of some private type which has
2482 -- been swapped in Defining_Identifier (N).
2484 if T /= Def_Id and then Is_Private_Type (Def_Id) then
2485 Process_Full_View (N, T, Def_Id);
2487 -- Record the reference. The form of this is a little strange, since
2488 -- the full declaration has been swapped in. So the first parameter
2489 -- here represents the entity to which a reference is made which is
2490 -- the "real" entity, i.e. the one swapped in, and the second
2491 -- parameter provides the reference location.
2493 -- Also, we want to kill Has_Pragma_Unreferenced temporarily here
2494 -- since we don't want a complaint about the full type being an
2495 -- unwanted reference to the private type
2497 declare
2498 B : constant Boolean := Has_Pragma_Unreferenced (T);
2499 begin
2500 Set_Has_Pragma_Unreferenced (T, False);
2501 Generate_Reference (T, T, 'c');
2502 Set_Has_Pragma_Unreferenced (T, B);
2503 end;
2505 Set_Completion_Referenced (Def_Id);
2507 -- For completion of incomplete type, process incomplete dependents
2508 -- and always mark the full type as referenced (it is the incomplete
2509 -- type that we get for any real reference).
2511 elsif Ekind (Prev) = E_Incomplete_Type then
2512 Process_Incomplete_Dependents (N, T, Prev);
2513 Generate_Reference (Prev, Def_Id, 'c');
2514 Set_Completion_Referenced (Def_Id);
2516 -- If not private type or incomplete type completion, this is a real
2517 -- definition of a new entity, so record it.
2519 else
2520 Generate_Definition (Def_Id);
2521 end if;
2523 if Chars (Scope (Def_Id)) = Name_System
2524 and then Chars (Def_Id) = Name_Address
2525 and then Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (N)))
2526 then
2527 Set_Is_Descendent_Of_Address (Def_Id);
2528 Set_Is_Descendent_Of_Address (Base_Type (Def_Id));
2529 Set_Is_Descendent_Of_Address (Prev);
2530 end if;
2532 Set_Optimize_Alignment_Flags (Def_Id);
2533 Check_Eliminated (Def_Id);
2535 -- If the declaration is a completion and aspects are present, apply
2536 -- them to the entity for the type which is currently the partial
2537 -- view, but which is the one that will be frozen.
2539 if Has_Aspects (N) then
2540 if Prev /= Def_Id then
2541 Analyze_Aspect_Specifications (N, Prev);
2542 else
2543 Analyze_Aspect_Specifications (N, Def_Id);
2544 end if;
2545 end if;
2546 end Analyze_Full_Type_Declaration;
2548 ----------------------------------
2549 -- Analyze_Incomplete_Type_Decl --
2550 ----------------------------------
2552 procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
2553 F : constant Boolean := Is_Pure (Current_Scope);
2554 T : Entity_Id;
2556 begin
2557 Check_SPARK_Restriction ("incomplete type is not allowed", N);
2559 Generate_Definition (Defining_Identifier (N));
2561 -- Process an incomplete declaration. The identifier must not have been
2562 -- declared already in the scope. However, an incomplete declaration may
2563 -- appear in the private part of a package, for a private type that has
2564 -- already been declared.
2566 -- In this case, the discriminants (if any) must match
2568 T := Find_Type_Name (N);
2570 Set_Ekind (T, E_Incomplete_Type);
2571 Init_Size_Align (T);
2572 Set_Is_First_Subtype (T, True);
2573 Set_Etype (T, T);
2575 -- Ada 2005 (AI-326): Minimum decoration to give support to tagged
2576 -- incomplete types.
2578 if Tagged_Present (N) then
2579 Set_Is_Tagged_Type (T);
2580 Make_Class_Wide_Type (T);
2581 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2582 end if;
2584 Push_Scope (T);
2586 Set_Stored_Constraint (T, No_Elist);
2588 if Present (Discriminant_Specifications (N)) then
2589 Process_Discriminants (N);
2590 end if;
2592 End_Scope;
2594 -- If the type has discriminants, non-trivial subtypes may be
2595 -- declared before the full view of the type. The full views of those
2596 -- subtypes will be built after the full view of the type.
2598 Set_Private_Dependents (T, New_Elmt_List);
2599 Set_Is_Pure (T, F);
2600 end Analyze_Incomplete_Type_Decl;
2602 -----------------------------------
2603 -- Analyze_Interface_Declaration --
2604 -----------------------------------
2606 procedure Analyze_Interface_Declaration (T : Entity_Id; Def : Node_Id) is
2607 CW : constant Entity_Id := Class_Wide_Type (T);
2609 begin
2610 Set_Is_Tagged_Type (T);
2612 Set_Is_Limited_Record (T, Limited_Present (Def)
2613 or else Task_Present (Def)
2614 or else Protected_Present (Def)
2615 or else Synchronized_Present (Def));
2617 -- Type is abstract if full declaration carries keyword, or if previous
2618 -- partial view did.
2620 Set_Is_Abstract_Type (T);
2621 Set_Is_Interface (T);
2623 -- Type is a limited interface if it includes the keyword limited, task,
2624 -- protected, or synchronized.
2626 Set_Is_Limited_Interface
2627 (T, Limited_Present (Def)
2628 or else Protected_Present (Def)
2629 or else Synchronized_Present (Def)
2630 or else Task_Present (Def));
2632 Set_Interfaces (T, New_Elmt_List);
2633 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2635 -- Complete the decoration of the class-wide entity if it was already
2636 -- built (i.e. during the creation of the limited view)
2638 if Present (CW) then
2639 Set_Is_Interface (CW);
2640 Set_Is_Limited_Interface (CW, Is_Limited_Interface (T));
2641 end if;
2643 -- Check runtime support for synchronized interfaces
2645 if VM_Target = No_VM
2646 and then (Is_Task_Interface (T)
2647 or else Is_Protected_Interface (T)
2648 or else Is_Synchronized_Interface (T))
2649 and then not RTE_Available (RE_Select_Specific_Data)
2650 then
2651 Error_Msg_CRT ("synchronized interfaces", T);
2652 end if;
2653 end Analyze_Interface_Declaration;
2655 -----------------------------
2656 -- Analyze_Itype_Reference --
2657 -----------------------------
2659 -- Nothing to do. This node is placed in the tree only for the benefit of
2660 -- back end processing, and has no effect on the semantic processing.
2662 procedure Analyze_Itype_Reference (N : Node_Id) is
2663 begin
2664 pragma Assert (Is_Itype (Itype (N)));
2665 null;
2666 end Analyze_Itype_Reference;
2668 --------------------------------
2669 -- Analyze_Number_Declaration --
2670 --------------------------------
2672 procedure Analyze_Number_Declaration (N : Node_Id) is
2673 Id : constant Entity_Id := Defining_Identifier (N);
2674 E : constant Node_Id := Expression (N);
2675 T : Entity_Id;
2676 Index : Interp_Index;
2677 It : Interp;
2679 begin
2680 Generate_Definition (Id);
2681 Enter_Name (Id);
2683 -- This is an optimization of a common case of an integer literal
2685 if Nkind (E) = N_Integer_Literal then
2686 Set_Is_Static_Expression (E, True);
2687 Set_Etype (E, Universal_Integer);
2689 Set_Etype (Id, Universal_Integer);
2690 Set_Ekind (Id, E_Named_Integer);
2691 Set_Is_Frozen (Id, True);
2692 return;
2693 end if;
2695 Set_Is_Pure (Id, Is_Pure (Current_Scope));
2697 -- Process expression, replacing error by integer zero, to avoid
2698 -- cascaded errors or aborts further along in the processing
2700 -- Replace Error by integer zero, which seems least likely to cause
2701 -- cascaded errors.
2703 if E = Error then
2704 Rewrite (E, Make_Integer_Literal (Sloc (E), Uint_0));
2705 Set_Error_Posted (E);
2706 end if;
2708 Analyze (E);
2710 -- Verify that the expression is static and numeric. If
2711 -- the expression is overloaded, we apply the preference
2712 -- rule that favors root numeric types.
2714 if not Is_Overloaded (E) then
2715 T := Etype (E);
2717 else
2718 T := Any_Type;
2720 Get_First_Interp (E, Index, It);
2721 while Present (It.Typ) loop
2722 if (Is_Integer_Type (It.Typ)
2723 or else Is_Real_Type (It.Typ))
2724 and then (Scope (Base_Type (It.Typ))) = Standard_Standard
2725 then
2726 if T = Any_Type then
2727 T := It.Typ;
2729 elsif It.Typ = Universal_Real
2730 or else It.Typ = Universal_Integer
2731 then
2732 -- Choose universal interpretation over any other
2734 T := It.Typ;
2735 exit;
2736 end if;
2737 end if;
2739 Get_Next_Interp (Index, It);
2740 end loop;
2741 end if;
2743 if Is_Integer_Type (T) then
2744 Resolve (E, T);
2745 Set_Etype (Id, Universal_Integer);
2746 Set_Ekind (Id, E_Named_Integer);
2748 elsif Is_Real_Type (T) then
2750 -- Because the real value is converted to universal_real, this is a
2751 -- legal context for a universal fixed expression.
2753 if T = Universal_Fixed then
2754 declare
2755 Loc : constant Source_Ptr := Sloc (N);
2756 Conv : constant Node_Id := Make_Type_Conversion (Loc,
2757 Subtype_Mark =>
2758 New_Occurrence_Of (Universal_Real, Loc),
2759 Expression => Relocate_Node (E));
2761 begin
2762 Rewrite (E, Conv);
2763 Analyze (E);
2764 end;
2766 elsif T = Any_Fixed then
2767 Error_Msg_N ("illegal context for mixed mode operation", E);
2769 -- Expression is of the form : universal_fixed * integer. Try to
2770 -- resolve as universal_real.
2772 T := Universal_Real;
2773 Set_Etype (E, T);
2774 end if;
2776 Resolve (E, T);
2777 Set_Etype (Id, Universal_Real);
2778 Set_Ekind (Id, E_Named_Real);
2780 else
2781 Wrong_Type (E, Any_Numeric);
2782 Resolve (E, T);
2784 Set_Etype (Id, T);
2785 Set_Ekind (Id, E_Constant);
2786 Set_Never_Set_In_Source (Id, True);
2787 Set_Is_True_Constant (Id, True);
2788 return;
2789 end if;
2791 if Nkind_In (E, N_Integer_Literal, N_Real_Literal) then
2792 Set_Etype (E, Etype (Id));
2793 end if;
2795 if not Is_OK_Static_Expression (E) then
2796 Flag_Non_Static_Expr
2797 ("non-static expression used in number declaration!", E);
2798 Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
2799 Set_Etype (E, Any_Type);
2800 end if;
2801 end Analyze_Number_Declaration;
2803 --------------------------------
2804 -- Analyze_Object_Declaration --
2805 --------------------------------
2807 procedure Analyze_Object_Declaration (N : Node_Id) is
2808 Loc : constant Source_Ptr := Sloc (N);
2809 Id : constant Entity_Id := Defining_Identifier (N);
2810 T : Entity_Id;
2811 Act_T : Entity_Id;
2813 E : Node_Id := Expression (N);
2814 -- E is set to Expression (N) throughout this routine. When
2815 -- Expression (N) is modified, E is changed accordingly.
2817 Prev_Entity : Entity_Id := Empty;
2819 function Count_Tasks (T : Entity_Id) return Uint;
2820 -- This function is called when a non-generic library level object of a
2821 -- task type is declared. Its function is to count the static number of
2822 -- tasks declared within the type (it is only called if Has_Tasks is set
2823 -- for T). As a side effect, if an array of tasks with non-static bounds
2824 -- or a variant record type is encountered, Check_Restrictions is called
2825 -- indicating the count is unknown.
2827 -----------------
2828 -- Count_Tasks --
2829 -----------------
2831 function Count_Tasks (T : Entity_Id) return Uint is
2832 C : Entity_Id;
2833 X : Node_Id;
2834 V : Uint;
2836 begin
2837 if Is_Task_Type (T) then
2838 return Uint_1;
2840 elsif Is_Record_Type (T) then
2841 if Has_Discriminants (T) then
2842 Check_Restriction (Max_Tasks, N);
2843 return Uint_0;
2845 else
2846 V := Uint_0;
2847 C := First_Component (T);
2848 while Present (C) loop
2849 V := V + Count_Tasks (Etype (C));
2850 Next_Component (C);
2851 end loop;
2853 return V;
2854 end if;
2856 elsif Is_Array_Type (T) then
2857 X := First_Index (T);
2858 V := Count_Tasks (Component_Type (T));
2859 while Present (X) loop
2860 C := Etype (X);
2862 if not Is_Static_Subtype (C) then
2863 Check_Restriction (Max_Tasks, N);
2864 return Uint_0;
2865 else
2866 V := V * (UI_Max (Uint_0,
2867 Expr_Value (Type_High_Bound (C)) -
2868 Expr_Value (Type_Low_Bound (C)) + Uint_1));
2869 end if;
2871 Next_Index (X);
2872 end loop;
2874 return V;
2876 else
2877 return Uint_0;
2878 end if;
2879 end Count_Tasks;
2881 -- Start of processing for Analyze_Object_Declaration
2883 begin
2884 -- There are three kinds of implicit types generated by an
2885 -- object declaration:
2887 -- 1. Those generated by the original Object Definition
2889 -- 2. Those generated by the Expression
2891 -- 3. Those used to constrain the Object Definition with the
2892 -- expression constraints when the definition is unconstrained.
2894 -- They must be generated in this order to avoid order of elaboration
2895 -- issues. Thus the first step (after entering the name) is to analyze
2896 -- the object definition.
2898 if Constant_Present (N) then
2899 Prev_Entity := Current_Entity_In_Scope (Id);
2901 if Present (Prev_Entity)
2902 and then
2904 -- If the homograph is an implicit subprogram, it is overridden
2905 -- by the current declaration.
2907 ((Is_Overloadable (Prev_Entity)
2908 and then Is_Inherited_Operation (Prev_Entity))
2910 -- The current object is a discriminal generated for an entry
2911 -- family index. Even though the index is a constant, in this
2912 -- particular context there is no true constant redeclaration.
2913 -- Enter_Name will handle the visibility.
2915 or else
2916 (Is_Discriminal (Id)
2917 and then Ekind (Discriminal_Link (Id)) =
2918 E_Entry_Index_Parameter)
2920 -- The current object is the renaming for a generic declared
2921 -- within the instance.
2923 or else
2924 (Ekind (Prev_Entity) = E_Package
2925 and then Nkind (Parent (Prev_Entity)) =
2926 N_Package_Renaming_Declaration
2927 and then not Comes_From_Source (Prev_Entity)
2928 and then Is_Generic_Instance (Renamed_Entity (Prev_Entity))))
2929 then
2930 Prev_Entity := Empty;
2931 end if;
2932 end if;
2934 if Present (Prev_Entity) then
2935 Constant_Redeclaration (Id, N, T);
2937 Generate_Reference (Prev_Entity, Id, 'c');
2938 Set_Completion_Referenced (Id);
2940 if Error_Posted (N) then
2942 -- Type mismatch or illegal redeclaration, Do not analyze
2943 -- expression to avoid cascaded errors.
2945 T := Find_Type_Of_Object (Object_Definition (N), N);
2946 Set_Etype (Id, T);
2947 Set_Ekind (Id, E_Variable);
2948 goto Leave;
2949 end if;
2951 -- In the normal case, enter identifier at the start to catch premature
2952 -- usage in the initialization expression.
2954 else
2955 Generate_Definition (Id);
2956 Enter_Name (Id);
2958 Mark_Coextensions (N, Object_Definition (N));
2960 T := Find_Type_Of_Object (Object_Definition (N), N);
2962 if Nkind (Object_Definition (N)) = N_Access_Definition
2963 and then Present
2964 (Access_To_Subprogram_Definition (Object_Definition (N)))
2965 and then Protected_Present
2966 (Access_To_Subprogram_Definition (Object_Definition (N)))
2967 then
2968 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
2969 end if;
2971 if Error_Posted (Id) then
2972 Set_Etype (Id, T);
2973 Set_Ekind (Id, E_Variable);
2974 goto Leave;
2975 end if;
2976 end if;
2978 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
2979 -- out some static checks
2981 if Ada_Version >= Ada_2005
2982 and then Can_Never_Be_Null (T)
2983 then
2984 -- In case of aggregates we must also take care of the correct
2985 -- initialization of nested aggregates bug this is done at the
2986 -- point of the analysis of the aggregate (see sem_aggr.adb)
2988 if Present (Expression (N))
2989 and then Nkind (Expression (N)) = N_Aggregate
2990 then
2991 null;
2993 else
2994 declare
2995 Save_Typ : constant Entity_Id := Etype (Id);
2996 begin
2997 Set_Etype (Id, T); -- Temp. decoration for static checks
2998 Null_Exclusion_Static_Checks (N);
2999 Set_Etype (Id, Save_Typ);
3000 end;
3001 end if;
3002 end if;
3004 -- Object is marked pure if it is in a pure scope
3006 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3008 -- If deferred constant, make sure context is appropriate. We detect
3009 -- a deferred constant as a constant declaration with no expression.
3010 -- A deferred constant can appear in a package body if its completion
3011 -- is by means of an interface pragma.
3013 if Constant_Present (N)
3014 and then No (E)
3015 then
3016 -- A deferred constant may appear in the declarative part of the
3017 -- following constructs:
3019 -- blocks
3020 -- entry bodies
3021 -- extended return statements
3022 -- package specs
3023 -- package bodies
3024 -- subprogram bodies
3025 -- task bodies
3027 -- When declared inside a package spec, a deferred constant must be
3028 -- completed by a full constant declaration or pragma Import. In all
3029 -- other cases, the only proper completion is pragma Import. Extended
3030 -- return statements are flagged as invalid contexts because they do
3031 -- not have a declarative part and so cannot accommodate the pragma.
3033 if Ekind (Current_Scope) = E_Return_Statement then
3034 Error_Msg_N
3035 ("invalid context for deferred constant declaration (RM 7.4)",
3037 Error_Msg_N
3038 ("\declaration requires an initialization expression",
3040 Set_Constant_Present (N, False);
3042 -- In Ada 83, deferred constant must be of private type
3044 elsif not Is_Private_Type (T) then
3045 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
3046 Error_Msg_N
3047 ("(Ada 83) deferred constant must be private type", N);
3048 end if;
3049 end if;
3051 -- If not a deferred constant, then object declaration freezes its type
3053 else
3054 Check_Fully_Declared (T, N);
3055 Freeze_Before (N, T);
3056 end if;
3058 -- If the object was created by a constrained array definition, then
3059 -- set the link in both the anonymous base type and anonymous subtype
3060 -- that are built to represent the array type to point to the object.
3062 if Nkind (Object_Definition (Declaration_Node (Id))) =
3063 N_Constrained_Array_Definition
3064 then
3065 Set_Related_Array_Object (T, Id);
3066 Set_Related_Array_Object (Base_Type (T), Id);
3067 end if;
3069 -- Special checks for protected objects not at library level
3071 if Is_Protected_Type (T)
3072 and then not Is_Library_Level_Entity (Id)
3073 then
3074 Check_Restriction (No_Local_Protected_Objects, Id);
3076 -- Protected objects with interrupt handlers must be at library level
3078 -- Ada 2005: this test is not needed (and the corresponding clause
3079 -- in the RM is removed) because accessibility checks are sufficient
3080 -- to make handlers not at the library level illegal.
3082 -- AI05-0303: the AI is in fact a binding interpretation, and thus
3083 -- applies to the '95 version of the language as well.
3085 if Has_Interrupt_Handler (T)
3086 and then Ada_Version < Ada_95
3087 then
3088 Error_Msg_N
3089 ("interrupt object can only be declared at library level", Id);
3090 end if;
3091 end if;
3093 -- The actual subtype of the object is the nominal subtype, unless
3094 -- the nominal one is unconstrained and obtained from the expression.
3096 Act_T := T;
3098 -- These checks should be performed before the initialization expression
3099 -- is considered, so that the Object_Definition node is still the same
3100 -- as in source code.
3102 -- In SPARK, the nominal subtype shall be given by a subtype mark and
3103 -- shall not be unconstrained. (The only exception to this is the
3104 -- admission of declarations of constants of type String.)
3106 if not
3107 Nkind_In (Object_Definition (N), N_Identifier, N_Expanded_Name)
3108 then
3109 Check_SPARK_Restriction
3110 ("subtype mark required", Object_Definition (N));
3112 elsif Is_Array_Type (T)
3113 and then not Is_Constrained (T)
3114 and then T /= Standard_String
3115 then
3116 Check_SPARK_Restriction
3117 ("subtype mark of constrained type expected",
3118 Object_Definition (N));
3119 end if;
3121 -- There are no aliased objects in SPARK
3123 if Aliased_Present (N) then
3124 Check_SPARK_Restriction ("aliased object is not allowed", N);
3125 end if;
3127 -- Process initialization expression if present and not in error
3129 if Present (E) and then E /= Error then
3131 -- Generate an error in case of CPP class-wide object initialization.
3132 -- Required because otherwise the expansion of the class-wide
3133 -- assignment would try to use 'size to initialize the object
3134 -- (primitive that is not available in CPP tagged types).
3136 if Is_Class_Wide_Type (Act_T)
3137 and then
3138 (Is_CPP_Class (Root_Type (Etype (Act_T)))
3139 or else
3140 (Present (Full_View (Root_Type (Etype (Act_T))))
3141 and then
3142 Is_CPP_Class (Full_View (Root_Type (Etype (Act_T))))))
3143 then
3144 Error_Msg_N
3145 ("predefined assignment not available for 'C'P'P tagged types",
3147 end if;
3149 Mark_Coextensions (N, E);
3150 Analyze (E);
3152 -- In case of errors detected in the analysis of the expression,
3153 -- decorate it with the expected type to avoid cascaded errors
3155 if No (Etype (E)) then
3156 Set_Etype (E, T);
3157 end if;
3159 -- If an initialization expression is present, then we set the
3160 -- Is_True_Constant flag. It will be reset if this is a variable
3161 -- and it is indeed modified.
3163 Set_Is_True_Constant (Id, True);
3165 -- If we are analyzing a constant declaration, set its completion
3166 -- flag after analyzing and resolving the expression.
3168 if Constant_Present (N) then
3169 Set_Has_Completion (Id);
3170 end if;
3172 -- Set type and resolve (type may be overridden later on)
3174 Set_Etype (Id, T);
3175 Resolve (E, T);
3177 -- No further action needed if E is a call to an inlined function
3178 -- which returns an unconstrained type and it has been expanded into
3179 -- a procedure call. In that case N has been replaced by an object
3180 -- declaration without initializing expression and it has been
3181 -- analyzed (see Expand_Inlined_Call).
3183 if Debug_Flag_Dot_K
3184 and then Expander_Active
3185 and then Nkind (E) = N_Function_Call
3186 and then Nkind (Name (E)) in N_Has_Entity
3187 and then Is_Inlined (Entity (Name (E)))
3188 and then not Is_Constrained (Etype (E))
3189 and then Analyzed (N)
3190 and then No (Expression (N))
3191 then
3192 return;
3193 end if;
3195 -- If E is null and has been replaced by an N_Raise_Constraint_Error
3196 -- node (which was marked already-analyzed), we need to set the type
3197 -- to something other than Any_Access in order to keep gigi happy.
3199 if Etype (E) = Any_Access then
3200 Set_Etype (E, T);
3201 end if;
3203 -- If the object is an access to variable, the initialization
3204 -- expression cannot be an access to constant.
3206 if Is_Access_Type (T)
3207 and then not Is_Access_Constant (T)
3208 and then Is_Access_Type (Etype (E))
3209 and then Is_Access_Constant (Etype (E))
3210 then
3211 Error_Msg_N
3212 ("access to variable cannot be initialized "
3213 & "with an access-to-constant expression", E);
3214 end if;
3216 if not Assignment_OK (N) then
3217 Check_Initialization (T, E);
3218 end if;
3220 Check_Unset_Reference (E);
3222 -- If this is a variable, then set current value. If this is a
3223 -- declared constant of a scalar type with a static expression,
3224 -- indicate that it is always valid.
3226 if not Constant_Present (N) then
3227 if Compile_Time_Known_Value (E) then
3228 Set_Current_Value (Id, E);
3229 end if;
3231 elsif Is_Scalar_Type (T)
3232 and then Is_OK_Static_Expression (E)
3233 then
3234 Set_Is_Known_Valid (Id);
3235 end if;
3237 -- Deal with setting of null flags
3239 if Is_Access_Type (T) then
3240 if Known_Non_Null (E) then
3241 Set_Is_Known_Non_Null (Id, True);
3242 elsif Known_Null (E)
3243 and then not Can_Never_Be_Null (Id)
3244 then
3245 Set_Is_Known_Null (Id, True);
3246 end if;
3247 end if;
3249 -- Check incorrect use of dynamically tagged expressions.
3251 if Is_Tagged_Type (T) then
3252 Check_Dynamically_Tagged_Expression
3253 (Expr => E,
3254 Typ => T,
3255 Related_Nod => N);
3256 end if;
3258 Apply_Scalar_Range_Check (E, T);
3259 Apply_Static_Length_Check (E, T);
3261 if Nkind (Original_Node (N)) = N_Object_Declaration
3262 and then Comes_From_Source (Original_Node (N))
3264 -- Only call test if needed
3266 and then Restriction_Check_Required (SPARK)
3267 and then not Is_SPARK_Initialization_Expr (E)
3268 then
3269 Check_SPARK_Restriction
3270 ("initialization expression is not appropriate", E);
3271 end if;
3272 end if;
3274 -- If the No_Streams restriction is set, check that the type of the
3275 -- object is not, and does not contain, any subtype derived from
3276 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
3277 -- Has_Stream just for efficiency reasons. There is no point in
3278 -- spending time on a Has_Stream check if the restriction is not set.
3280 if Restriction_Check_Required (No_Streams) then
3281 if Has_Stream (T) then
3282 Check_Restriction (No_Streams, N);
3283 end if;
3284 end if;
3286 -- Deal with predicate check before we start to do major rewriting.
3287 -- it is OK to initialize and then check the initialized value, since
3288 -- the object goes out of scope if we get a predicate failure. Note
3289 -- that we do this in the analyzer and not the expander because the
3290 -- analyzer does some substantial rewriting in some cases.
3292 -- We need a predicate check if the type has predicates, and if either
3293 -- there is an initializing expression, or for default initialization
3294 -- when we have at least one case of an explicit default initial value.
3296 if not Suppress_Assignment_Checks (N)
3297 and then Present (Predicate_Function (T))
3298 and then
3299 (Present (E)
3300 or else
3301 Is_Partially_Initialized_Type (T, Include_Implicit => False))
3302 then
3303 Insert_After (N,
3304 Make_Predicate_Check (T, New_Occurrence_Of (Id, Loc)));
3305 end if;
3307 -- Case of unconstrained type
3309 if Is_Indefinite_Subtype (T) then
3311 -- In SPARK, a declaration of unconstrained type is allowed
3312 -- only for constants of type string.
3314 if Is_String_Type (T) and then not Constant_Present (N) then
3315 Check_SPARK_Restriction
3316 ("declaration of object of unconstrained type not allowed",
3318 end if;
3320 -- Nothing to do in deferred constant case
3322 if Constant_Present (N) and then No (E) then
3323 null;
3325 -- Case of no initialization present
3327 elsif No (E) then
3328 if No_Initialization (N) then
3329 null;
3331 elsif Is_Class_Wide_Type (T) then
3332 Error_Msg_N
3333 ("initialization required in class-wide declaration ", N);
3335 else
3336 Error_Msg_N
3337 ("unconstrained subtype not allowed (need initialization)",
3338 Object_Definition (N));
3340 if Is_Record_Type (T) and then Has_Discriminants (T) then
3341 Error_Msg_N
3342 ("\provide initial value or explicit discriminant values",
3343 Object_Definition (N));
3345 Error_Msg_NE
3346 ("\or give default discriminant values for type&",
3347 Object_Definition (N), T);
3349 elsif Is_Array_Type (T) then
3350 Error_Msg_N
3351 ("\provide initial value or explicit array bounds",
3352 Object_Definition (N));
3353 end if;
3354 end if;
3356 -- Case of initialization present but in error. Set initial
3357 -- expression as absent (but do not make above complaints)
3359 elsif E = Error then
3360 Set_Expression (N, Empty);
3361 E := Empty;
3363 -- Case of initialization present
3365 else
3366 -- Check restrictions in Ada 83
3368 if not Constant_Present (N) then
3370 -- Unconstrained variables not allowed in Ada 83 mode
3372 if Ada_Version = Ada_83
3373 and then Comes_From_Source (Object_Definition (N))
3374 then
3375 Error_Msg_N
3376 ("(Ada 83) unconstrained variable not allowed",
3377 Object_Definition (N));
3378 end if;
3379 end if;
3381 -- Now we constrain the variable from the initializing expression
3383 -- If the expression is an aggregate, it has been expanded into
3384 -- individual assignments. Retrieve the actual type from the
3385 -- expanded construct.
3387 if Is_Array_Type (T)
3388 and then No_Initialization (N)
3389 and then Nkind (Original_Node (E)) = N_Aggregate
3390 then
3391 Act_T := Etype (E);
3393 -- In case of class-wide interface object declarations we delay
3394 -- the generation of the equivalent record type declarations until
3395 -- its expansion because there are cases in they are not required.
3397 elsif Is_Interface (T) then
3398 null;
3400 else
3401 Expand_Subtype_From_Expr (N, T, Object_Definition (N), E);
3402 Act_T := Find_Type_Of_Object (Object_Definition (N), N);
3403 end if;
3405 Set_Is_Constr_Subt_For_U_Nominal (Act_T);
3407 if Aliased_Present (N) then
3408 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
3409 end if;
3411 Freeze_Before (N, Act_T);
3412 Freeze_Before (N, T);
3413 end if;
3415 elsif Is_Array_Type (T)
3416 and then No_Initialization (N)
3417 and then Nkind (Original_Node (E)) = N_Aggregate
3418 then
3419 if not Is_Entity_Name (Object_Definition (N)) then
3420 Act_T := Etype (E);
3421 Check_Compile_Time_Size (Act_T);
3423 if Aliased_Present (N) then
3424 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
3425 end if;
3426 end if;
3428 -- When the given object definition and the aggregate are specified
3429 -- independently, and their lengths might differ do a length check.
3430 -- This cannot happen if the aggregate is of the form (others =>...)
3432 if not Is_Constrained (T) then
3433 null;
3435 elsif Nkind (E) = N_Raise_Constraint_Error then
3437 -- Aggregate is statically illegal. Place back in declaration
3439 Set_Expression (N, E);
3440 Set_No_Initialization (N, False);
3442 elsif T = Etype (E) then
3443 null;
3445 elsif Nkind (E) = N_Aggregate
3446 and then Present (Component_Associations (E))
3447 and then Present (Choices (First (Component_Associations (E))))
3448 and then Nkind (First
3449 (Choices (First (Component_Associations (E))))) = N_Others_Choice
3450 then
3451 null;
3453 else
3454 Apply_Length_Check (E, T);
3455 end if;
3457 -- If the type is limited unconstrained with defaulted discriminants and
3458 -- there is no expression, then the object is constrained by the
3459 -- defaults, so it is worthwhile building the corresponding subtype.
3461 elsif (Is_Limited_Record (T) or else Is_Concurrent_Type (T))
3462 and then not Is_Constrained (T)
3463 and then Has_Discriminants (T)
3464 then
3465 if No (E) then
3466 Act_T := Build_Default_Subtype (T, N);
3467 else
3468 -- Ada 2005: a limited object may be initialized by means of an
3469 -- aggregate. If the type has default discriminants it has an
3470 -- unconstrained nominal type, Its actual subtype will be obtained
3471 -- from the aggregate, and not from the default discriminants.
3473 Act_T := Etype (E);
3474 end if;
3476 Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
3478 elsif Present (Underlying_Type (T))
3479 and then not Is_Constrained (Underlying_Type (T))
3480 and then Has_Discriminants (Underlying_Type (T))
3481 and then Nkind (E) = N_Function_Call
3482 and then Constant_Present (N)
3483 then
3484 -- The back-end has problems with constants of a discriminated type
3485 -- with defaults, if the initial value is a function call. We
3486 -- generate an intermediate temporary for the result of the call.
3487 -- It is unclear why this should make it acceptable to gcc. ???
3489 Remove_Side_Effects (E);
3491 -- If this is a constant declaration of an unconstrained type and
3492 -- the initialization is an aggregate, we can use the subtype of the
3493 -- aggregate for the declared entity because it is immutable.
3495 elsif not Is_Constrained (T)
3496 and then Has_Discriminants (T)
3497 and then Constant_Present (N)
3498 and then not Has_Unchecked_Union (T)
3499 and then Nkind (E) = N_Aggregate
3500 then
3501 Act_T := Etype (E);
3502 end if;
3504 -- Check No_Wide_Characters restriction
3506 Check_Wide_Character_Restriction (T, Object_Definition (N));
3508 -- Indicate this is not set in source. Certainly true for constants, and
3509 -- true for variables so far (will be reset for a variable if and when
3510 -- we encounter a modification in the source).
3512 Set_Never_Set_In_Source (Id, True);
3514 -- Now establish the proper kind and type of the object
3516 if Constant_Present (N) then
3517 Set_Ekind (Id, E_Constant);
3518 Set_Is_True_Constant (Id, True);
3520 else
3521 Set_Ekind (Id, E_Variable);
3523 -- A variable is set as shared passive if it appears in a shared
3524 -- passive package, and is at the outer level. This is not done for
3525 -- entities generated during expansion, because those are always
3526 -- manipulated locally.
3528 if Is_Shared_Passive (Current_Scope)
3529 and then Is_Library_Level_Entity (Id)
3530 and then Comes_From_Source (Id)
3531 then
3532 Set_Is_Shared_Passive (Id);
3533 Check_Shared_Var (Id, T, N);
3534 end if;
3536 -- Set Has_Initial_Value if initializing expression present. Note
3537 -- that if there is no initializing expression, we leave the state
3538 -- of this flag unchanged (usually it will be False, but notably in
3539 -- the case of exception choice variables, it will already be true).
3541 if Present (E) then
3542 Set_Has_Initial_Value (Id, True);
3543 end if;
3544 end if;
3546 -- Initialize alignment and size and capture alignment setting
3548 Init_Alignment (Id);
3549 Init_Esize (Id);
3550 Set_Optimize_Alignment_Flags (Id);
3552 -- Deal with aliased case
3554 if Aliased_Present (N) then
3555 Set_Is_Aliased (Id);
3557 -- If the object is aliased and the type is unconstrained with
3558 -- defaulted discriminants and there is no expression, then the
3559 -- object is constrained by the defaults, so it is worthwhile
3560 -- building the corresponding subtype.
3562 -- Ada 2005 (AI-363): If the aliased object is discriminated and
3563 -- unconstrained, then only establish an actual subtype if the
3564 -- nominal subtype is indefinite. In definite cases the object is
3565 -- unconstrained in Ada 2005.
3567 if No (E)
3568 and then Is_Record_Type (T)
3569 and then not Is_Constrained (T)
3570 and then Has_Discriminants (T)
3571 and then (Ada_Version < Ada_2005 or else Is_Indefinite_Subtype (T))
3572 then
3573 Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
3574 end if;
3575 end if;
3577 -- Now we can set the type of the object
3579 Set_Etype (Id, Act_T);
3581 -- Object is marked to be treated as volatile if type is volatile and
3582 -- we clear the Current_Value setting that may have been set above.
3584 if Treat_As_Volatile (Etype (Id)) then
3585 Set_Treat_As_Volatile (Id);
3586 Set_Current_Value (Id, Empty);
3587 end if;
3589 -- Deal with controlled types
3591 if Has_Controlled_Component (Etype (Id))
3592 or else Is_Controlled (Etype (Id))
3593 then
3594 if not Is_Library_Level_Entity (Id) then
3595 Check_Restriction (No_Nested_Finalization, N);
3596 else
3597 Validate_Controlled_Object (Id);
3598 end if;
3599 end if;
3601 if Has_Task (Etype (Id)) then
3602 Check_Restriction (No_Tasking, N);
3604 -- Deal with counting max tasks
3606 -- Nothing to do if inside a generic
3608 if Inside_A_Generic then
3609 null;
3611 -- If library level entity, then count tasks
3613 elsif Is_Library_Level_Entity (Id) then
3614 Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
3616 -- If not library level entity, then indicate we don't know max
3617 -- tasks and also check task hierarchy restriction and blocking
3618 -- operation (since starting a task is definitely blocking!)
3620 else
3621 Check_Restriction (Max_Tasks, N);
3622 Check_Restriction (No_Task_Hierarchy, N);
3623 Check_Potentially_Blocking_Operation (N);
3624 end if;
3626 -- A rather specialized test. If we see two tasks being declared
3627 -- of the same type in the same object declaration, and the task
3628 -- has an entry with an address clause, we know that program error
3629 -- will be raised at run time since we can't have two tasks with
3630 -- entries at the same address.
3632 if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
3633 declare
3634 E : Entity_Id;
3636 begin
3637 E := First_Entity (Etype (Id));
3638 while Present (E) loop
3639 if Ekind (E) = E_Entry
3640 and then Present (Get_Attribute_Definition_Clause
3641 (E, Attribute_Address))
3642 then
3643 Error_Msg_N
3644 ("?more than one task with same entry address", N);
3645 Error_Msg_N
3646 ("\?Program_Error will be raised at run time", N);
3647 Insert_Action (N,
3648 Make_Raise_Program_Error (Loc,
3649 Reason => PE_Duplicated_Entry_Address));
3650 exit;
3651 end if;
3653 Next_Entity (E);
3654 end loop;
3655 end;
3656 end if;
3657 end if;
3659 -- Some simple constant-propagation: if the expression is a constant
3660 -- string initialized with a literal, share the literal. This avoids
3661 -- a run-time copy.
3663 if Present (E)
3664 and then Is_Entity_Name (E)
3665 and then Ekind (Entity (E)) = E_Constant
3666 and then Base_Type (Etype (E)) = Standard_String
3667 then
3668 declare
3669 Val : constant Node_Id := Constant_Value (Entity (E));
3670 begin
3671 if Present (Val)
3672 and then Nkind (Val) = N_String_Literal
3673 then
3674 Rewrite (E, New_Copy (Val));
3675 end if;
3676 end;
3677 end if;
3679 -- Another optimization: if the nominal subtype is unconstrained and
3680 -- the expression is a function call that returns an unconstrained
3681 -- type, rewrite the declaration as a renaming of the result of the
3682 -- call. The exceptions below are cases where the copy is expected,
3683 -- either by the back end (Aliased case) or by the semantics, as for
3684 -- initializing controlled types or copying tags for classwide types.
3686 if Present (E)
3687 and then Nkind (E) = N_Explicit_Dereference
3688 and then Nkind (Original_Node (E)) = N_Function_Call
3689 and then not Is_Library_Level_Entity (Id)
3690 and then not Is_Constrained (Underlying_Type (T))
3691 and then not Is_Aliased (Id)
3692 and then not Is_Class_Wide_Type (T)
3693 and then not Is_Controlled (T)
3694 and then not Has_Controlled_Component (Base_Type (T))
3695 and then Expander_Active
3696 then
3697 Rewrite (N,
3698 Make_Object_Renaming_Declaration (Loc,
3699 Defining_Identifier => Id,
3700 Access_Definition => Empty,
3701 Subtype_Mark => New_Occurrence_Of
3702 (Base_Type (Etype (Id)), Loc),
3703 Name => E));
3705 Set_Renamed_Object (Id, E);
3707 -- Force generation of debugging information for the constant and for
3708 -- the renamed function call.
3710 Set_Debug_Info_Needed (Id);
3711 Set_Debug_Info_Needed (Entity (Prefix (E)));
3712 end if;
3714 if Present (Prev_Entity)
3715 and then Is_Frozen (Prev_Entity)
3716 and then not Error_Posted (Id)
3717 then
3718 Error_Msg_N ("full constant declaration appears too late", N);
3719 end if;
3721 Check_Eliminated (Id);
3723 -- Deal with setting In_Private_Part flag if in private part
3725 if Ekind (Scope (Id)) = E_Package
3726 and then In_Private_Part (Scope (Id))
3727 then
3728 Set_In_Private_Part (Id);
3729 end if;
3731 -- Check for violation of No_Local_Timing_Events
3733 if Restriction_Check_Required (No_Local_Timing_Events)
3734 and then not Is_Library_Level_Entity (Id)
3735 and then Is_RTE (Etype (Id), RE_Timing_Event)
3736 then
3737 Check_Restriction (No_Local_Timing_Events, N);
3738 end if;
3740 <<Leave>>
3741 if Has_Aspects (N) then
3742 Analyze_Aspect_Specifications (N, Id);
3743 end if;
3745 Analyze_Dimension (N);
3746 end Analyze_Object_Declaration;
3748 ---------------------------
3749 -- Analyze_Others_Choice --
3750 ---------------------------
3752 -- Nothing to do for the others choice node itself, the semantic analysis
3753 -- of the others choice will occur as part of the processing of the parent
3755 procedure Analyze_Others_Choice (N : Node_Id) is
3756 pragma Warnings (Off, N);
3757 begin
3758 null;
3759 end Analyze_Others_Choice;
3761 -------------------------------------------
3762 -- Analyze_Private_Extension_Declaration --
3763 -------------------------------------------
3765 procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
3766 T : constant Entity_Id := Defining_Identifier (N);
3767 Indic : constant Node_Id := Subtype_Indication (N);
3768 Parent_Type : Entity_Id;
3769 Parent_Base : Entity_Id;
3771 begin
3772 -- Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
3774 if Is_Non_Empty_List (Interface_List (N)) then
3775 declare
3776 Intf : Node_Id;
3777 T : Entity_Id;
3779 begin
3780 Intf := First (Interface_List (N));
3781 while Present (Intf) loop
3782 T := Find_Type_Of_Subtype_Indic (Intf);
3784 Diagnose_Interface (Intf, T);
3785 Next (Intf);
3786 end loop;
3787 end;
3788 end if;
3790 Generate_Definition (T);
3792 -- For other than Ada 2012, just enter the name in the current scope
3794 if Ada_Version < Ada_2012 then
3795 Enter_Name (T);
3797 -- Ada 2012 (AI05-0162): Enter the name in the current scope handling
3798 -- case of private type that completes an incomplete type.
3800 else
3801 declare
3802 Prev : Entity_Id;
3804 begin
3805 Prev := Find_Type_Name (N);
3807 pragma Assert (Prev = T
3808 or else (Ekind (Prev) = E_Incomplete_Type
3809 and then Present (Full_View (Prev))
3810 and then Full_View (Prev) = T));
3811 end;
3812 end if;
3814 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
3815 Parent_Base := Base_Type (Parent_Type);
3817 if Parent_Type = Any_Type
3818 or else Etype (Parent_Type) = Any_Type
3819 then
3820 Set_Ekind (T, Ekind (Parent_Type));
3821 Set_Etype (T, Any_Type);
3822 goto Leave;
3824 elsif not Is_Tagged_Type (Parent_Type) then
3825 Error_Msg_N
3826 ("parent of type extension must be a tagged type ", Indic);
3827 goto Leave;
3829 elsif Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
3830 Error_Msg_N ("premature derivation of incomplete type", Indic);
3831 goto Leave;
3833 elsif Is_Concurrent_Type (Parent_Type) then
3834 Error_Msg_N
3835 ("parent type of a private extension cannot be "
3836 & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
3838 Set_Etype (T, Any_Type);
3839 Set_Ekind (T, E_Limited_Private_Type);
3840 Set_Private_Dependents (T, New_Elmt_List);
3841 Set_Error_Posted (T);
3842 goto Leave;
3843 end if;
3845 -- Perhaps the parent type should be changed to the class-wide type's
3846 -- specific type in this case to prevent cascading errors ???
3848 if Is_Class_Wide_Type (Parent_Type) then
3849 Error_Msg_N
3850 ("parent of type extension must not be a class-wide type", Indic);
3851 goto Leave;
3852 end if;
3854 if (not Is_Package_Or_Generic_Package (Current_Scope)
3855 and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
3856 or else In_Private_Part (Current_Scope)
3858 then
3859 Error_Msg_N ("invalid context for private extension", N);
3860 end if;
3862 -- Set common attributes
3864 Set_Is_Pure (T, Is_Pure (Current_Scope));
3865 Set_Scope (T, Current_Scope);
3866 Set_Ekind (T, E_Record_Type_With_Private);
3867 Init_Size_Align (T);
3869 Set_Etype (T, Parent_Base);
3870 Set_Has_Task (T, Has_Task (Parent_Base));
3872 Set_Convention (T, Convention (Parent_Type));
3873 Set_First_Rep_Item (T, First_Rep_Item (Parent_Type));
3874 Set_Is_First_Subtype (T);
3875 Make_Class_Wide_Type (T);
3877 if Unknown_Discriminants_Present (N) then
3878 Set_Discriminant_Constraint (T, No_Elist);
3879 end if;
3881 Build_Derived_Record_Type (N, Parent_Type, T);
3883 -- Propagate inherited invariant information. The new type has
3884 -- invariants, if the parent type has inheritable invariants,
3885 -- and these invariants can in turn be inherited.
3887 if Has_Inheritable_Invariants (Parent_Type) then
3888 Set_Has_Inheritable_Invariants (T);
3889 Set_Has_Invariants (T);
3890 end if;
3892 -- Ada 2005 (AI-443): Synchronized private extension or a rewritten
3893 -- synchronized formal derived type.
3895 if Ada_Version >= Ada_2005
3896 and then Synchronized_Present (N)
3897 then
3898 Set_Is_Limited_Record (T);
3900 -- Formal derived type case
3902 if Is_Generic_Type (T) then
3904 -- The parent must be a tagged limited type or a synchronized
3905 -- interface.
3907 if (not Is_Tagged_Type (Parent_Type)
3908 or else not Is_Limited_Type (Parent_Type))
3909 and then
3910 (not Is_Interface (Parent_Type)
3911 or else not Is_Synchronized_Interface (Parent_Type))
3912 then
3913 Error_Msg_NE ("parent type of & must be tagged limited " &
3914 "or synchronized", N, T);
3915 end if;
3917 -- The progenitors (if any) must be limited or synchronized
3918 -- interfaces.
3920 if Present (Interfaces (T)) then
3921 declare
3922 Iface : Entity_Id;
3923 Iface_Elmt : Elmt_Id;
3925 begin
3926 Iface_Elmt := First_Elmt (Interfaces (T));
3927 while Present (Iface_Elmt) loop
3928 Iface := Node (Iface_Elmt);
3930 if not Is_Limited_Interface (Iface)
3931 and then not Is_Synchronized_Interface (Iface)
3932 then
3933 Error_Msg_NE ("progenitor & must be limited " &
3934 "or synchronized", N, Iface);
3935 end if;
3937 Next_Elmt (Iface_Elmt);
3938 end loop;
3939 end;
3940 end if;
3942 -- Regular derived extension, the parent must be a limited or
3943 -- synchronized interface.
3945 else
3946 if not Is_Interface (Parent_Type)
3947 or else (not Is_Limited_Interface (Parent_Type)
3948 and then
3949 not Is_Synchronized_Interface (Parent_Type))
3950 then
3951 Error_Msg_NE
3952 ("parent type of & must be limited interface", N, T);
3953 end if;
3954 end if;
3956 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
3957 -- extension with a synchronized parent must be explicitly declared
3958 -- synchronized, because the full view will be a synchronized type.
3959 -- This must be checked before the check for limited types below,
3960 -- to ensure that types declared limited are not allowed to extend
3961 -- synchronized interfaces.
3963 elsif Is_Interface (Parent_Type)
3964 and then Is_Synchronized_Interface (Parent_Type)
3965 and then not Synchronized_Present (N)
3966 then
3967 Error_Msg_NE
3968 ("private extension of& must be explicitly synchronized",
3969 N, Parent_Type);
3971 elsif Limited_Present (N) then
3972 Set_Is_Limited_Record (T);
3974 if not Is_Limited_Type (Parent_Type)
3975 and then
3976 (not Is_Interface (Parent_Type)
3977 or else not Is_Limited_Interface (Parent_Type))
3978 then
3979 Error_Msg_NE ("parent type& of limited extension must be limited",
3980 N, Parent_Type);
3981 end if;
3982 end if;
3984 <<Leave>>
3985 if Has_Aspects (N) then
3986 Analyze_Aspect_Specifications (N, T);
3987 end if;
3988 end Analyze_Private_Extension_Declaration;
3990 ---------------------------------
3991 -- Analyze_Subtype_Declaration --
3992 ---------------------------------
3994 procedure Analyze_Subtype_Declaration
3995 (N : Node_Id;
3996 Skip : Boolean := False)
3998 Id : constant Entity_Id := Defining_Identifier (N);
3999 T : Entity_Id;
4000 R_Checks : Check_Result;
4002 begin
4003 Generate_Definition (Id);
4004 Set_Is_Pure (Id, Is_Pure (Current_Scope));
4005 Init_Size_Align (Id);
4007 -- The following guard condition on Enter_Name is to handle cases where
4008 -- the defining identifier has already been entered into the scope but
4009 -- the declaration as a whole needs to be analyzed.
4011 -- This case in particular happens for derived enumeration types. The
4012 -- derived enumeration type is processed as an inserted enumeration type
4013 -- declaration followed by a rewritten subtype declaration. The defining
4014 -- identifier, however, is entered into the name scope very early in the
4015 -- processing of the original type declaration and therefore needs to be
4016 -- avoided here, when the created subtype declaration is analyzed. (See
4017 -- Build_Derived_Types)
4019 -- This also happens when the full view of a private type is derived
4020 -- type with constraints. In this case the entity has been introduced
4021 -- in the private declaration.
4023 if Skip
4024 or else (Present (Etype (Id))
4025 and then (Is_Private_Type (Etype (Id))
4026 or else Is_Task_Type (Etype (Id))
4027 or else Is_Rewrite_Substitution (N)))
4028 then
4029 null;
4031 else
4032 Enter_Name (Id);
4033 end if;
4035 T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
4037 -- Class-wide equivalent types of records with unknown discriminants
4038 -- involve the generation of an itype which serves as the private view
4039 -- of a constrained record subtype. In such cases the base type of the
4040 -- current subtype we are processing is the private itype. Use the full
4041 -- of the private itype when decorating various attributes.
4043 if Is_Itype (T)
4044 and then Is_Private_Type (T)
4045 and then Present (Full_View (T))
4046 then
4047 T := Full_View (T);
4048 end if;
4050 -- Inherit common attributes
4052 Set_Is_Volatile (Id, Is_Volatile (T));
4053 Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
4054 Set_Is_Generic_Type (Id, Is_Generic_Type (Base_Type (T)));
4055 Set_Convention (Id, Convention (T));
4057 -- If ancestor has predicates then so does the subtype, and in addition
4058 -- we must delay the freeze to properly arrange predicate inheritance.
4060 -- The Ancestor_Type test is a big kludge, there seem to be cases in
4061 -- which T = ID, so the above tests and assignments do nothing???
4063 if Has_Predicates (T)
4064 or else (Present (Ancestor_Subtype (T))
4065 and then Has_Predicates (Ancestor_Subtype (T)))
4066 then
4067 Set_Has_Predicates (Id);
4068 Set_Has_Delayed_Freeze (Id);
4069 end if;
4071 -- Subtype of Boolean cannot have a constraint in SPARK
4073 if Is_Boolean_Type (T)
4074 and then Nkind (Subtype_Indication (N)) = N_Subtype_Indication
4075 then
4076 Check_SPARK_Restriction
4077 ("subtype of Boolean cannot have constraint", N);
4078 end if;
4080 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
4081 declare
4082 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
4083 One_Cstr : Node_Id;
4084 Low : Node_Id;
4085 High : Node_Id;
4087 begin
4088 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint then
4089 One_Cstr := First (Constraints (Cstr));
4090 while Present (One_Cstr) loop
4092 -- Index or discriminant constraint in SPARK must be a
4093 -- subtype mark.
4095 if not
4096 Nkind_In (One_Cstr, N_Identifier, N_Expanded_Name)
4097 then
4098 Check_SPARK_Restriction
4099 ("subtype mark required", One_Cstr);
4101 -- String subtype must have a lower bound of 1 in SPARK.
4102 -- Note that we do not need to test for the non-static case
4103 -- here, since that was already taken care of in
4104 -- Process_Range_Expr_In_Decl.
4106 elsif Base_Type (T) = Standard_String then
4107 Get_Index_Bounds (One_Cstr, Low, High);
4109 if Is_OK_Static_Expression (Low)
4110 and then Expr_Value (Low) /= 1
4111 then
4112 Check_SPARK_Restriction
4113 ("String subtype must have lower bound of 1", N);
4114 end if;
4115 end if;
4117 Next (One_Cstr);
4118 end loop;
4119 end if;
4120 end;
4121 end if;
4123 -- In the case where there is no constraint given in the subtype
4124 -- indication, Process_Subtype just returns the Subtype_Mark, so its
4125 -- semantic attributes must be established here.
4127 if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
4128 Set_Etype (Id, Base_Type (T));
4130 -- Subtype of unconstrained array without constraint is not allowed
4131 -- in SPARK.
4133 if Is_Array_Type (T)
4134 and then not Is_Constrained (T)
4135 then
4136 Check_SPARK_Restriction
4137 ("subtype of unconstrained array must have constraint", N);
4138 end if;
4140 case Ekind (T) is
4141 when Array_Kind =>
4142 Set_Ekind (Id, E_Array_Subtype);
4143 Copy_Array_Subtype_Attributes (Id, T);
4145 when Decimal_Fixed_Point_Kind =>
4146 Set_Ekind (Id, E_Decimal_Fixed_Point_Subtype);
4147 Set_Digits_Value (Id, Digits_Value (T));
4148 Set_Delta_Value (Id, Delta_Value (T));
4149 Set_Scale_Value (Id, Scale_Value (T));
4150 Set_Small_Value (Id, Small_Value (T));
4151 Set_Scalar_Range (Id, Scalar_Range (T));
4152 Set_Machine_Radix_10 (Id, Machine_Radix_10 (T));
4153 Set_Is_Constrained (Id, Is_Constrained (T));
4154 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4155 Set_RM_Size (Id, RM_Size (T));
4157 when Enumeration_Kind =>
4158 Set_Ekind (Id, E_Enumeration_Subtype);
4159 Set_First_Literal (Id, First_Literal (Base_Type (T)));
4160 Set_Scalar_Range (Id, Scalar_Range (T));
4161 Set_Is_Character_Type (Id, Is_Character_Type (T));
4162 Set_Is_Constrained (Id, Is_Constrained (T));
4163 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4164 Set_RM_Size (Id, RM_Size (T));
4166 when Ordinary_Fixed_Point_Kind =>
4167 Set_Ekind (Id, E_Ordinary_Fixed_Point_Subtype);
4168 Set_Scalar_Range (Id, Scalar_Range (T));
4169 Set_Small_Value (Id, Small_Value (T));
4170 Set_Delta_Value (Id, Delta_Value (T));
4171 Set_Is_Constrained (Id, Is_Constrained (T));
4172 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4173 Set_RM_Size (Id, RM_Size (T));
4175 when Float_Kind =>
4176 Set_Ekind (Id, E_Floating_Point_Subtype);
4177 Set_Scalar_Range (Id, Scalar_Range (T));
4178 Set_Digits_Value (Id, Digits_Value (T));
4179 Set_Is_Constrained (Id, Is_Constrained (T));
4181 when Signed_Integer_Kind =>
4182 Set_Ekind (Id, E_Signed_Integer_Subtype);
4183 Set_Scalar_Range (Id, Scalar_Range (T));
4184 Set_Is_Constrained (Id, Is_Constrained (T));
4185 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4186 Set_RM_Size (Id, RM_Size (T));
4188 when Modular_Integer_Kind =>
4189 Set_Ekind (Id, E_Modular_Integer_Subtype);
4190 Set_Scalar_Range (Id, Scalar_Range (T));
4191 Set_Is_Constrained (Id, Is_Constrained (T));
4192 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4193 Set_RM_Size (Id, RM_Size (T));
4195 when Class_Wide_Kind =>
4196 Set_Ekind (Id, E_Class_Wide_Subtype);
4197 Set_First_Entity (Id, First_Entity (T));
4198 Set_Last_Entity (Id, Last_Entity (T));
4199 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
4200 Set_Cloned_Subtype (Id, T);
4201 Set_Is_Tagged_Type (Id, True);
4202 Set_Has_Unknown_Discriminants
4203 (Id, True);
4205 if Ekind (T) = E_Class_Wide_Subtype then
4206 Set_Equivalent_Type (Id, Equivalent_Type (T));
4207 end if;
4209 when E_Record_Type | E_Record_Subtype =>
4210 Set_Ekind (Id, E_Record_Subtype);
4212 if Ekind (T) = E_Record_Subtype
4213 and then Present (Cloned_Subtype (T))
4214 then
4215 Set_Cloned_Subtype (Id, Cloned_Subtype (T));
4216 else
4217 Set_Cloned_Subtype (Id, T);
4218 end if;
4220 Set_First_Entity (Id, First_Entity (T));
4221 Set_Last_Entity (Id, Last_Entity (T));
4222 Set_Has_Discriminants (Id, Has_Discriminants (T));
4223 Set_Is_Constrained (Id, Is_Constrained (T));
4224 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
4225 Set_Has_Implicit_Dereference
4226 (Id, Has_Implicit_Dereference (T));
4227 Set_Has_Unknown_Discriminants
4228 (Id, Has_Unknown_Discriminants (T));
4230 if Has_Discriminants (T) then
4231 Set_Discriminant_Constraint
4232 (Id, Discriminant_Constraint (T));
4233 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
4235 elsif Has_Unknown_Discriminants (Id) then
4236 Set_Discriminant_Constraint (Id, No_Elist);
4237 end if;
4239 if Is_Tagged_Type (T) then
4240 Set_Is_Tagged_Type (Id);
4241 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
4242 Set_Direct_Primitive_Operations
4243 (Id, Direct_Primitive_Operations (T));
4244 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
4246 if Is_Interface (T) then
4247 Set_Is_Interface (Id);
4248 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
4249 end if;
4250 end if;
4252 when Private_Kind =>
4253 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
4254 Set_Has_Discriminants (Id, Has_Discriminants (T));
4255 Set_Is_Constrained (Id, Is_Constrained (T));
4256 Set_First_Entity (Id, First_Entity (T));
4257 Set_Last_Entity (Id, Last_Entity (T));
4258 Set_Private_Dependents (Id, New_Elmt_List);
4259 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
4260 Set_Has_Implicit_Dereference
4261 (Id, Has_Implicit_Dereference (T));
4262 Set_Has_Unknown_Discriminants
4263 (Id, Has_Unknown_Discriminants (T));
4264 Set_Known_To_Have_Preelab_Init
4265 (Id, Known_To_Have_Preelab_Init (T));
4267 if Is_Tagged_Type (T) then
4268 Set_Is_Tagged_Type (Id);
4269 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
4270 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
4271 Set_Direct_Primitive_Operations (Id,
4272 Direct_Primitive_Operations (T));
4273 end if;
4275 -- In general the attributes of the subtype of a private type
4276 -- are the attributes of the partial view of parent. However,
4277 -- the full view may be a discriminated type, and the subtype
4278 -- must share the discriminant constraint to generate correct
4279 -- calls to initialization procedures.
4281 if Has_Discriminants (T) then
4282 Set_Discriminant_Constraint
4283 (Id, Discriminant_Constraint (T));
4284 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
4286 elsif Present (Full_View (T))
4287 and then Has_Discriminants (Full_View (T))
4288 then
4289 Set_Discriminant_Constraint
4290 (Id, Discriminant_Constraint (Full_View (T)));
4291 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
4293 -- This would seem semantically correct, but apparently
4294 -- confuses the back-end. To be explained and checked with
4295 -- current version ???
4297 -- Set_Has_Discriminants (Id);
4298 end if;
4300 Prepare_Private_Subtype_Completion (Id, N);
4302 when Access_Kind =>
4303 Set_Ekind (Id, E_Access_Subtype);
4304 Set_Is_Constrained (Id, Is_Constrained (T));
4305 Set_Is_Access_Constant
4306 (Id, Is_Access_Constant (T));
4307 Set_Directly_Designated_Type
4308 (Id, Designated_Type (T));
4309 Set_Can_Never_Be_Null (Id, Can_Never_Be_Null (T));
4311 -- A Pure library_item must not contain the declaration of a
4312 -- named access type, except within a subprogram, generic
4313 -- subprogram, task unit, or protected unit, or if it has
4314 -- a specified Storage_Size of zero (RM05-10.2.1(15.4-15.5)).
4316 if Comes_From_Source (Id)
4317 and then In_Pure_Unit
4318 and then not In_Subprogram_Task_Protected_Unit
4319 and then not No_Pool_Assigned (Id)
4320 then
4321 Error_Msg_N
4322 ("named access types not allowed in pure unit", N);
4323 end if;
4325 when Concurrent_Kind =>
4326 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
4327 Set_Corresponding_Record_Type (Id,
4328 Corresponding_Record_Type (T));
4329 Set_First_Entity (Id, First_Entity (T));
4330 Set_First_Private_Entity (Id, First_Private_Entity (T));
4331 Set_Has_Discriminants (Id, Has_Discriminants (T));
4332 Set_Is_Constrained (Id, Is_Constrained (T));
4333 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
4334 Set_Last_Entity (Id, Last_Entity (T));
4336 if Has_Discriminants (T) then
4337 Set_Discriminant_Constraint (Id,
4338 Discriminant_Constraint (T));
4339 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
4340 end if;
4342 when E_Incomplete_Type =>
4343 if Ada_Version >= Ada_2005 then
4345 -- In Ada 2005 an incomplete type can be explicitly tagged:
4346 -- propagate indication.
4348 Set_Ekind (Id, E_Incomplete_Subtype);
4349 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
4350 Set_Private_Dependents (Id, New_Elmt_List);
4352 -- Ada 2005 (AI-412): Decorate an incomplete subtype of an
4353 -- incomplete type visible through a limited with clause.
4355 if From_With_Type (T)
4356 and then Present (Non_Limited_View (T))
4357 then
4358 Set_From_With_Type (Id);
4359 Set_Non_Limited_View (Id, Non_Limited_View (T));
4361 -- Ada 2005 (AI-412): Add the regular incomplete subtype
4362 -- to the private dependents of the original incomplete
4363 -- type for future transformation.
4365 else
4366 Append_Elmt (Id, Private_Dependents (T));
4367 end if;
4369 -- If the subtype name denotes an incomplete type an error
4370 -- was already reported by Process_Subtype.
4372 else
4373 Set_Etype (Id, Any_Type);
4374 end if;
4376 when others =>
4377 raise Program_Error;
4378 end case;
4379 end if;
4381 if Etype (Id) = Any_Type then
4382 goto Leave;
4383 end if;
4385 -- Some common processing on all types
4387 Set_Size_Info (Id, T);
4388 Set_First_Rep_Item (Id, First_Rep_Item (T));
4390 T := Etype (Id);
4392 Set_Is_Immediately_Visible (Id, True);
4393 Set_Depends_On_Private (Id, Has_Private_Component (T));
4394 Set_Is_Descendent_Of_Address (Id, Is_Descendent_Of_Address (T));
4396 if Is_Interface (T) then
4397 Set_Is_Interface (Id);
4398 end if;
4400 if Present (Generic_Parent_Type (N))
4401 and then
4402 (Nkind
4403 (Parent (Generic_Parent_Type (N))) /= N_Formal_Type_Declaration
4404 or else Nkind
4405 (Formal_Type_Definition (Parent (Generic_Parent_Type (N))))
4406 /= N_Formal_Private_Type_Definition)
4407 then
4408 if Is_Tagged_Type (Id) then
4410 -- If this is a generic actual subtype for a synchronized type,
4411 -- the primitive operations are those of the corresponding record
4412 -- for which there is a separate subtype declaration.
4414 if Is_Concurrent_Type (Id) then
4415 null;
4416 elsif Is_Class_Wide_Type (Id) then
4417 Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
4418 else
4419 Derive_Subprograms (Generic_Parent_Type (N), Id, T);
4420 end if;
4422 elsif Scope (Etype (Id)) /= Standard_Standard then
4423 Derive_Subprograms (Generic_Parent_Type (N), Id);
4424 end if;
4425 end if;
4427 if Is_Private_Type (T)
4428 and then Present (Full_View (T))
4429 then
4430 Conditional_Delay (Id, Full_View (T));
4432 -- The subtypes of components or subcomponents of protected types
4433 -- do not need freeze nodes, which would otherwise appear in the
4434 -- wrong scope (before the freeze node for the protected type). The
4435 -- proper subtypes are those of the subcomponents of the corresponding
4436 -- record.
4438 elsif Ekind (Scope (Id)) /= E_Protected_Type
4439 and then Present (Scope (Scope (Id))) -- error defense!
4440 and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
4441 then
4442 Conditional_Delay (Id, T);
4443 end if;
4445 -- Check that Constraint_Error is raised for a scalar subtype indication
4446 -- when the lower or upper bound of a non-null range lies outside the
4447 -- range of the type mark.
4449 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
4450 if Is_Scalar_Type (Etype (Id))
4451 and then Scalar_Range (Id) /=
4452 Scalar_Range (Etype (Subtype_Mark
4453 (Subtype_Indication (N))))
4454 then
4455 Apply_Range_Check
4456 (Scalar_Range (Id),
4457 Etype (Subtype_Mark (Subtype_Indication (N))));
4459 -- In the array case, check compatibility for each index
4461 elsif Is_Array_Type (Etype (Id))
4462 and then Present (First_Index (Id))
4463 then
4464 -- This really should be a subprogram that finds the indications
4465 -- to check???
4467 declare
4468 Subt_Index : Node_Id := First_Index (Id);
4469 Target_Index : Node_Id :=
4470 First_Index (Etype
4471 (Subtype_Mark (Subtype_Indication (N))));
4472 Has_Dyn_Chk : Boolean := Has_Dynamic_Range_Check (N);
4474 begin
4475 while Present (Subt_Index) loop
4476 if ((Nkind (Subt_Index) = N_Identifier
4477 and then Ekind (Entity (Subt_Index)) in Scalar_Kind)
4478 or else Nkind (Subt_Index) = N_Subtype_Indication)
4479 and then
4480 Nkind (Scalar_Range (Etype (Subt_Index))) = N_Range
4481 then
4482 declare
4483 Target_Typ : constant Entity_Id :=
4484 Etype (Target_Index);
4485 begin
4486 R_Checks :=
4487 Get_Range_Checks
4488 (Scalar_Range (Etype (Subt_Index)),
4489 Target_Typ,
4490 Etype (Subt_Index),
4491 Defining_Identifier (N));
4493 -- Reset Has_Dynamic_Range_Check on the subtype to
4494 -- prevent elision of the index check due to a dynamic
4495 -- check generated for a preceding index (needed since
4496 -- Insert_Range_Checks tries to avoid generating
4497 -- redundant checks on a given declaration).
4499 Set_Has_Dynamic_Range_Check (N, False);
4501 Insert_Range_Checks
4502 (R_Checks,
4504 Target_Typ,
4505 Sloc (Defining_Identifier (N)));
4507 -- Record whether this index involved a dynamic check
4509 Has_Dyn_Chk :=
4510 Has_Dyn_Chk or else Has_Dynamic_Range_Check (N);
4511 end;
4512 end if;
4514 Next_Index (Subt_Index);
4515 Next_Index (Target_Index);
4516 end loop;
4518 -- Finally, mark whether the subtype involves dynamic checks
4520 Set_Has_Dynamic_Range_Check (N, Has_Dyn_Chk);
4521 end;
4522 end if;
4523 end if;
4525 -- Make sure that generic actual types are properly frozen. The subtype
4526 -- is marked as a generic actual type when the enclosing instance is
4527 -- analyzed, so here we identify the subtype from the tree structure.
4529 if Expander_Active
4530 and then Is_Generic_Actual_Type (Id)
4531 and then In_Instance
4532 and then not Comes_From_Source (N)
4533 and then Nkind (Subtype_Indication (N)) /= N_Subtype_Indication
4534 and then Is_Frozen (T)
4535 then
4536 Freeze_Before (N, Id);
4537 end if;
4539 Set_Optimize_Alignment_Flags (Id);
4540 Check_Eliminated (Id);
4542 <<Leave>>
4543 if Has_Aspects (N) then
4544 Analyze_Aspect_Specifications (N, Id);
4545 end if;
4547 Analyze_Dimension (N);
4548 end Analyze_Subtype_Declaration;
4550 --------------------------------
4551 -- Analyze_Subtype_Indication --
4552 --------------------------------
4554 procedure Analyze_Subtype_Indication (N : Node_Id) is
4555 T : constant Entity_Id := Subtype_Mark (N);
4556 R : constant Node_Id := Range_Expression (Constraint (N));
4558 begin
4559 Analyze (T);
4561 if R /= Error then
4562 Analyze (R);
4563 Set_Etype (N, Etype (R));
4564 Resolve (R, Entity (T));
4565 else
4566 Set_Error_Posted (R);
4567 Set_Error_Posted (T);
4568 end if;
4569 end Analyze_Subtype_Indication;
4571 --------------------------
4572 -- Analyze_Variant_Part --
4573 --------------------------
4575 procedure Analyze_Variant_Part (N : Node_Id) is
4577 procedure Non_Static_Choice_Error (Choice : Node_Id);
4578 -- Error routine invoked by the generic instantiation below when the
4579 -- variant part has a non static choice.
4581 procedure Process_Declarations (Variant : Node_Id);
4582 -- Analyzes all the declarations associated with a Variant. Needed by
4583 -- the generic instantiation below.
4585 package Variant_Choices_Processing is new
4586 Generic_Choices_Processing
4587 (Get_Alternatives => Variants,
4588 Get_Choices => Discrete_Choices,
4589 Process_Empty_Choice => No_OP,
4590 Process_Non_Static_Choice => Non_Static_Choice_Error,
4591 Process_Associated_Node => Process_Declarations);
4592 use Variant_Choices_Processing;
4593 -- Instantiation of the generic choice processing package
4595 -----------------------------
4596 -- Non_Static_Choice_Error --
4597 -----------------------------
4599 procedure Non_Static_Choice_Error (Choice : Node_Id) is
4600 begin
4601 Flag_Non_Static_Expr
4602 ("choice given in variant part is not static!", Choice);
4603 end Non_Static_Choice_Error;
4605 --------------------------
4606 -- Process_Declarations --
4607 --------------------------
4609 procedure Process_Declarations (Variant : Node_Id) is
4610 begin
4611 if not Null_Present (Component_List (Variant)) then
4612 Analyze_Declarations (Component_Items (Component_List (Variant)));
4614 if Present (Variant_Part (Component_List (Variant))) then
4615 Analyze (Variant_Part (Component_List (Variant)));
4616 end if;
4617 end if;
4618 end Process_Declarations;
4620 -- Local Variables
4622 Discr_Name : Node_Id;
4623 Discr_Type : Entity_Id;
4625 Dont_Care : Boolean;
4626 Others_Present : Boolean := False;
4628 pragma Warnings (Off, Dont_Care);
4629 pragma Warnings (Off, Others_Present);
4630 -- We don't care about the assigned values of any of these
4632 -- Start of processing for Analyze_Variant_Part
4634 begin
4635 Discr_Name := Name (N);
4636 Analyze (Discr_Name);
4638 -- If Discr_Name bad, get out (prevent cascaded errors)
4640 if Etype (Discr_Name) = Any_Type then
4641 return;
4642 end if;
4644 -- Check invalid discriminant in variant part
4646 if Ekind (Entity (Discr_Name)) /= E_Discriminant then
4647 Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
4648 end if;
4650 Discr_Type := Etype (Entity (Discr_Name));
4652 if not Is_Discrete_Type (Discr_Type) then
4653 Error_Msg_N
4654 ("discriminant in a variant part must be of a discrete type",
4655 Name (N));
4656 return;
4657 end if;
4659 -- Call the instantiated Analyze_Choices which does the rest of the work
4661 Analyze_Choices (N, Discr_Type, Dont_Care, Others_Present);
4662 end Analyze_Variant_Part;
4664 ----------------------------
4665 -- Array_Type_Declaration --
4666 ----------------------------
4668 procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
4669 Component_Def : constant Node_Id := Component_Definition (Def);
4670 Component_Typ : constant Node_Id := Subtype_Indication (Component_Def);
4671 Element_Type : Entity_Id;
4672 Implicit_Base : Entity_Id;
4673 Index : Node_Id;
4674 Related_Id : Entity_Id := Empty;
4675 Nb_Index : Nat;
4676 P : constant Node_Id := Parent (Def);
4677 Priv : Entity_Id;
4679 begin
4680 if Nkind (Def) = N_Constrained_Array_Definition then
4681 Index := First (Discrete_Subtype_Definitions (Def));
4682 else
4683 Index := First (Subtype_Marks (Def));
4684 end if;
4686 -- Find proper names for the implicit types which may be public. In case
4687 -- of anonymous arrays we use the name of the first object of that type
4688 -- as prefix.
4690 if No (T) then
4691 Related_Id := Defining_Identifier (P);
4692 else
4693 Related_Id := T;
4694 end if;
4696 Nb_Index := 1;
4697 while Present (Index) loop
4698 Analyze (Index);
4700 if not Nkind_In (Index, N_Identifier, N_Expanded_Name) then
4701 Check_SPARK_Restriction ("subtype mark required", Index);
4702 end if;
4704 -- Add a subtype declaration for each index of private array type
4705 -- declaration whose etype is also private. For example:
4707 -- package Pkg is
4708 -- type Index is private;
4709 -- private
4710 -- type Table is array (Index) of ...
4711 -- end;
4713 -- This is currently required by the expander for the internally
4714 -- generated equality subprogram of records with variant parts in
4715 -- which the etype of some component is such private type.
4717 if Ekind (Current_Scope) = E_Package
4718 and then In_Private_Part (Current_Scope)
4719 and then Has_Private_Declaration (Etype (Index))
4720 then
4721 declare
4722 Loc : constant Source_Ptr := Sloc (Def);
4723 New_E : Entity_Id;
4724 Decl : Entity_Id;
4726 begin
4727 New_E := Make_Temporary (Loc, 'T');
4728 Set_Is_Internal (New_E);
4730 Decl :=
4731 Make_Subtype_Declaration (Loc,
4732 Defining_Identifier => New_E,
4733 Subtype_Indication =>
4734 New_Occurrence_Of (Etype (Index), Loc));
4736 Insert_Before (Parent (Def), Decl);
4737 Analyze (Decl);
4738 Set_Etype (Index, New_E);
4740 -- If the index is a range the Entity attribute is not
4741 -- available. Example:
4743 -- package Pkg is
4744 -- type T is private;
4745 -- private
4746 -- type T is new Natural;
4747 -- Table : array (T(1) .. T(10)) of Boolean;
4748 -- end Pkg;
4750 if Nkind (Index) /= N_Range then
4751 Set_Entity (Index, New_E);
4752 end if;
4753 end;
4754 end if;
4756 Make_Index (Index, P, Related_Id, Nb_Index);
4758 -- Check error of subtype with predicate for index type
4760 Bad_Predicated_Subtype_Use
4761 ("subtype& has predicate, not allowed as index subtype",
4762 Index, Etype (Index));
4764 -- Move to next index
4766 Next_Index (Index);
4767 Nb_Index := Nb_Index + 1;
4768 end loop;
4770 -- Process subtype indication if one is present
4772 if Present (Component_Typ) then
4773 Element_Type := Process_Subtype (Component_Typ, P, Related_Id, 'C');
4775 Set_Etype (Component_Typ, Element_Type);
4777 if not Nkind_In (Component_Typ, N_Identifier, N_Expanded_Name) then
4778 Check_SPARK_Restriction ("subtype mark required", Component_Typ);
4779 end if;
4781 -- Ada 2005 (AI-230): Access Definition case
4783 else pragma Assert (Present (Access_Definition (Component_Def)));
4785 -- Indicate that the anonymous access type is created by the
4786 -- array type declaration.
4788 Element_Type := Access_Definition
4789 (Related_Nod => P,
4790 N => Access_Definition (Component_Def));
4791 Set_Is_Local_Anonymous_Access (Element_Type);
4793 -- Propagate the parent. This field is needed if we have to generate
4794 -- the master_id associated with an anonymous access to task type
4795 -- component (see Expand_N_Full_Type_Declaration.Build_Master)
4797 Set_Parent (Element_Type, Parent (T));
4799 -- Ada 2005 (AI-230): In case of components that are anonymous access
4800 -- types the level of accessibility depends on the enclosing type
4801 -- declaration
4803 Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
4805 -- Ada 2005 (AI-254)
4807 declare
4808 CD : constant Node_Id :=
4809 Access_To_Subprogram_Definition
4810 (Access_Definition (Component_Def));
4811 begin
4812 if Present (CD) and then Protected_Present (CD) then
4813 Element_Type :=
4814 Replace_Anonymous_Access_To_Protected_Subprogram (Def);
4815 end if;
4816 end;
4817 end if;
4819 -- Constrained array case
4821 if No (T) then
4822 T := Create_Itype (E_Void, P, Related_Id, 'T');
4823 end if;
4825 if Nkind (Def) = N_Constrained_Array_Definition then
4827 -- Establish Implicit_Base as unconstrained base type
4829 Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
4831 Set_Etype (Implicit_Base, Implicit_Base);
4832 Set_Scope (Implicit_Base, Current_Scope);
4833 Set_Has_Delayed_Freeze (Implicit_Base);
4835 -- The constrained array type is a subtype of the unconstrained one
4837 Set_Ekind (T, E_Array_Subtype);
4838 Init_Size_Align (T);
4839 Set_Etype (T, Implicit_Base);
4840 Set_Scope (T, Current_Scope);
4841 Set_Is_Constrained (T, True);
4842 Set_First_Index (T, First (Discrete_Subtype_Definitions (Def)));
4843 Set_Has_Delayed_Freeze (T);
4845 -- Complete setup of implicit base type
4847 Set_First_Index (Implicit_Base, First_Index (T));
4848 Set_Component_Type (Implicit_Base, Element_Type);
4849 Set_Has_Task (Implicit_Base, Has_Task (Element_Type));
4850 Set_Component_Size (Implicit_Base, Uint_0);
4851 Set_Packed_Array_Type (Implicit_Base, Empty);
4852 Set_Has_Controlled_Component
4853 (Implicit_Base, Has_Controlled_Component
4854 (Element_Type)
4855 or else Is_Controlled
4856 (Element_Type));
4857 Set_Finalize_Storage_Only
4858 (Implicit_Base, Finalize_Storage_Only
4859 (Element_Type));
4861 -- Unconstrained array case
4863 else
4864 Set_Ekind (T, E_Array_Type);
4865 Init_Size_Align (T);
4866 Set_Etype (T, T);
4867 Set_Scope (T, Current_Scope);
4868 Set_Component_Size (T, Uint_0);
4869 Set_Is_Constrained (T, False);
4870 Set_First_Index (T, First (Subtype_Marks (Def)));
4871 Set_Has_Delayed_Freeze (T, True);
4872 Set_Has_Task (T, Has_Task (Element_Type));
4873 Set_Has_Controlled_Component (T, Has_Controlled_Component
4874 (Element_Type)
4875 or else
4876 Is_Controlled (Element_Type));
4877 Set_Finalize_Storage_Only (T, Finalize_Storage_Only
4878 (Element_Type));
4879 end if;
4881 -- Common attributes for both cases
4883 Set_Component_Type (Base_Type (T), Element_Type);
4884 Set_Packed_Array_Type (T, Empty);
4886 if Aliased_Present (Component_Definition (Def)) then
4887 Check_SPARK_Restriction
4888 ("aliased is not allowed", Component_Definition (Def));
4889 Set_Has_Aliased_Components (Etype (T));
4890 end if;
4892 -- Ada 2005 (AI-231): Propagate the null-excluding attribute to the
4893 -- array type to ensure that objects of this type are initialized.
4895 if Ada_Version >= Ada_2005
4896 and then Can_Never_Be_Null (Element_Type)
4897 then
4898 Set_Can_Never_Be_Null (T);
4900 if Null_Exclusion_Present (Component_Definition (Def))
4902 -- No need to check itypes because in their case this check was
4903 -- done at their point of creation
4905 and then not Is_Itype (Element_Type)
4906 then
4907 Error_Msg_N
4908 ("`NOT NULL` not allowed (null already excluded)",
4909 Subtype_Indication (Component_Definition (Def)));
4910 end if;
4911 end if;
4913 Priv := Private_Component (Element_Type);
4915 if Present (Priv) then
4917 -- Check for circular definitions
4919 if Priv = Any_Type then
4920 Set_Component_Type (Etype (T), Any_Type);
4922 -- There is a gap in the visibility of operations on the composite
4923 -- type only if the component type is defined in a different scope.
4925 elsif Scope (Priv) = Current_Scope then
4926 null;
4928 elsif Is_Limited_Type (Priv) then
4929 Set_Is_Limited_Composite (Etype (T));
4930 Set_Is_Limited_Composite (T);
4931 else
4932 Set_Is_Private_Composite (Etype (T));
4933 Set_Is_Private_Composite (T);
4934 end if;
4935 end if;
4937 -- A syntax error in the declaration itself may lead to an empty index
4938 -- list, in which case do a minimal patch.
4940 if No (First_Index (T)) then
4941 Error_Msg_N ("missing index definition in array type declaration", T);
4943 declare
4944 Indexes : constant List_Id :=
4945 New_List (New_Occurrence_Of (Any_Id, Sloc (T)));
4946 begin
4947 Set_Discrete_Subtype_Definitions (Def, Indexes);
4948 Set_First_Index (T, First (Indexes));
4949 return;
4950 end;
4951 end if;
4953 -- Create a concatenation operator for the new type. Internal array
4954 -- types created for packed entities do not need such, they are
4955 -- compatible with the user-defined type.
4957 if Number_Dimensions (T) = 1
4958 and then not Is_Packed_Array_Type (T)
4959 then
4960 New_Concatenation_Op (T);
4961 end if;
4963 -- In the case of an unconstrained array the parser has already verified
4964 -- that all the indexes are unconstrained but we still need to make sure
4965 -- that the element type is constrained.
4967 if Is_Indefinite_Subtype (Element_Type) then
4968 Error_Msg_N
4969 ("unconstrained element type in array declaration",
4970 Subtype_Indication (Component_Def));
4972 elsif Is_Abstract_Type (Element_Type) then
4973 Error_Msg_N
4974 ("the type of a component cannot be abstract",
4975 Subtype_Indication (Component_Def));
4976 end if;
4978 -- There may be an invariant declared for the component type, but
4979 -- the construction of the component invariant checking procedure
4980 -- takes place during expansion.
4981 end Array_Type_Declaration;
4983 ------------------------------------------------------
4984 -- Replace_Anonymous_Access_To_Protected_Subprogram --
4985 ------------------------------------------------------
4987 function Replace_Anonymous_Access_To_Protected_Subprogram
4988 (N : Node_Id) return Entity_Id
4990 Loc : constant Source_Ptr := Sloc (N);
4992 Curr_Scope : constant Scope_Stack_Entry :=
4993 Scope_Stack.Table (Scope_Stack.Last);
4995 Anon : constant Entity_Id := Make_Temporary (Loc, 'S');
4996 Acc : Node_Id;
4997 Comp : Node_Id;
4998 Decl : Node_Id;
4999 P : Node_Id;
5001 begin
5002 Set_Is_Internal (Anon);
5004 case Nkind (N) is
5005 when N_Component_Declaration |
5006 N_Unconstrained_Array_Definition |
5007 N_Constrained_Array_Definition =>
5008 Comp := Component_Definition (N);
5009 Acc := Access_Definition (Comp);
5011 when N_Discriminant_Specification =>
5012 Comp := Discriminant_Type (N);
5013 Acc := Comp;
5015 when N_Parameter_Specification =>
5016 Comp := Parameter_Type (N);
5017 Acc := Comp;
5019 when N_Access_Function_Definition =>
5020 Comp := Result_Definition (N);
5021 Acc := Comp;
5023 when N_Object_Declaration =>
5024 Comp := Object_Definition (N);
5025 Acc := Comp;
5027 when N_Function_Specification =>
5028 Comp := Result_Definition (N);
5029 Acc := Comp;
5031 when others =>
5032 raise Program_Error;
5033 end case;
5035 Decl := Make_Full_Type_Declaration (Loc,
5036 Defining_Identifier => Anon,
5037 Type_Definition =>
5038 Copy_Separate_Tree (Access_To_Subprogram_Definition (Acc)));
5040 Mark_Rewrite_Insertion (Decl);
5042 -- Insert the new declaration in the nearest enclosing scope. If the
5043 -- node is a body and N is its return type, the declaration belongs in
5044 -- the enclosing scope.
5046 P := Parent (N);
5048 if Nkind (P) = N_Subprogram_Body
5049 and then Nkind (N) = N_Function_Specification
5050 then
5051 P := Parent (P);
5052 end if;
5054 while Present (P) and then not Has_Declarations (P) loop
5055 P := Parent (P);
5056 end loop;
5058 pragma Assert (Present (P));
5060 if Nkind (P) = N_Package_Specification then
5061 Prepend (Decl, Visible_Declarations (P));
5062 else
5063 Prepend (Decl, Declarations (P));
5064 end if;
5066 -- Replace the anonymous type with an occurrence of the new declaration.
5067 -- In all cases the rewritten node does not have the null-exclusion
5068 -- attribute because (if present) it was already inherited by the
5069 -- anonymous entity (Anon). Thus, in case of components we do not
5070 -- inherit this attribute.
5072 if Nkind (N) = N_Parameter_Specification then
5073 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5074 Set_Etype (Defining_Identifier (N), Anon);
5075 Set_Null_Exclusion_Present (N, False);
5077 elsif Nkind (N) = N_Object_Declaration then
5078 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5079 Set_Etype (Defining_Identifier (N), Anon);
5081 elsif Nkind (N) = N_Access_Function_Definition then
5082 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5084 elsif Nkind (N) = N_Function_Specification then
5085 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5086 Set_Etype (Defining_Unit_Name (N), Anon);
5088 else
5089 Rewrite (Comp,
5090 Make_Component_Definition (Loc,
5091 Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
5092 end if;
5094 Mark_Rewrite_Insertion (Comp);
5096 if Nkind_In (N, N_Object_Declaration, N_Access_Function_Definition) then
5097 Analyze (Decl);
5099 else
5100 -- Temporarily remove the current scope (record or subprogram) from
5101 -- the stack to add the new declarations to the enclosing scope.
5103 Scope_Stack.Decrement_Last;
5104 Analyze (Decl);
5105 Set_Is_Itype (Anon);
5106 Scope_Stack.Append (Curr_Scope);
5107 end if;
5109 Set_Ekind (Anon, E_Anonymous_Access_Protected_Subprogram_Type);
5110 Set_Can_Use_Internal_Rep (Anon, not Always_Compatible_Rep_On_Target);
5111 return Anon;
5112 end Replace_Anonymous_Access_To_Protected_Subprogram;
5114 -------------------------------
5115 -- Build_Derived_Access_Type --
5116 -------------------------------
5118 procedure Build_Derived_Access_Type
5119 (N : Node_Id;
5120 Parent_Type : Entity_Id;
5121 Derived_Type : Entity_Id)
5123 S : constant Node_Id := Subtype_Indication (Type_Definition (N));
5125 Desig_Type : Entity_Id;
5126 Discr : Entity_Id;
5127 Discr_Con_Elist : Elist_Id;
5128 Discr_Con_El : Elmt_Id;
5129 Subt : Entity_Id;
5131 begin
5132 -- Set the designated type so it is available in case this is an access
5133 -- to a self-referential type, e.g. a standard list type with a next
5134 -- pointer. Will be reset after subtype is built.
5136 Set_Directly_Designated_Type
5137 (Derived_Type, Designated_Type (Parent_Type));
5139 Subt := Process_Subtype (S, N);
5141 if Nkind (S) /= N_Subtype_Indication
5142 and then Subt /= Base_Type (Subt)
5143 then
5144 Set_Ekind (Derived_Type, E_Access_Subtype);
5145 end if;
5147 if Ekind (Derived_Type) = E_Access_Subtype then
5148 declare
5149 Pbase : constant Entity_Id := Base_Type (Parent_Type);
5150 Ibase : constant Entity_Id :=
5151 Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
5152 Svg_Chars : constant Name_Id := Chars (Ibase);
5153 Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
5155 begin
5156 Copy_Node (Pbase, Ibase);
5158 Set_Chars (Ibase, Svg_Chars);
5159 Set_Next_Entity (Ibase, Svg_Next_E);
5160 Set_Sloc (Ibase, Sloc (Derived_Type));
5161 Set_Scope (Ibase, Scope (Derived_Type));
5162 Set_Freeze_Node (Ibase, Empty);
5163 Set_Is_Frozen (Ibase, False);
5164 Set_Comes_From_Source (Ibase, False);
5165 Set_Is_First_Subtype (Ibase, False);
5167 Set_Etype (Ibase, Pbase);
5168 Set_Etype (Derived_Type, Ibase);
5169 end;
5170 end if;
5172 Set_Directly_Designated_Type
5173 (Derived_Type, Designated_Type (Subt));
5175 Set_Is_Constrained (Derived_Type, Is_Constrained (Subt));
5176 Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
5177 Set_Size_Info (Derived_Type, Parent_Type);
5178 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
5179 Set_Depends_On_Private (Derived_Type,
5180 Has_Private_Component (Derived_Type));
5181 Conditional_Delay (Derived_Type, Subt);
5183 -- Ada 2005 (AI-231): Set the null-exclusion attribute, and verify
5184 -- that it is not redundant.
5186 if Null_Exclusion_Present (Type_Definition (N)) then
5187 Set_Can_Never_Be_Null (Derived_Type);
5189 if Can_Never_Be_Null (Parent_Type)
5190 and then False
5191 then
5192 Error_Msg_NE
5193 ("`NOT NULL` not allowed (& already excludes null)",
5194 N, Parent_Type);
5195 end if;
5197 elsif Can_Never_Be_Null (Parent_Type) then
5198 Set_Can_Never_Be_Null (Derived_Type);
5199 end if;
5201 -- Note: we do not copy the Storage_Size_Variable, since we always go to
5202 -- the root type for this information.
5204 -- Apply range checks to discriminants for derived record case
5205 -- ??? THIS CODE SHOULD NOT BE HERE REALLY.
5207 Desig_Type := Designated_Type (Derived_Type);
5208 if Is_Composite_Type (Desig_Type)
5209 and then (not Is_Array_Type (Desig_Type))
5210 and then Has_Discriminants (Desig_Type)
5211 and then Base_Type (Desig_Type) /= Desig_Type
5212 then
5213 Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
5214 Discr_Con_El := First_Elmt (Discr_Con_Elist);
5216 Discr := First_Discriminant (Base_Type (Desig_Type));
5217 while Present (Discr_Con_El) loop
5218 Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
5219 Next_Elmt (Discr_Con_El);
5220 Next_Discriminant (Discr);
5221 end loop;
5222 end if;
5223 end Build_Derived_Access_Type;
5225 ------------------------------
5226 -- Build_Derived_Array_Type --
5227 ------------------------------
5229 procedure Build_Derived_Array_Type
5230 (N : Node_Id;
5231 Parent_Type : Entity_Id;
5232 Derived_Type : Entity_Id)
5234 Loc : constant Source_Ptr := Sloc (N);
5235 Tdef : constant Node_Id := Type_Definition (N);
5236 Indic : constant Node_Id := Subtype_Indication (Tdef);
5237 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
5238 Implicit_Base : Entity_Id;
5239 New_Indic : Node_Id;
5241 procedure Make_Implicit_Base;
5242 -- If the parent subtype is constrained, the derived type is a subtype
5243 -- of an implicit base type derived from the parent base.
5245 ------------------------
5246 -- Make_Implicit_Base --
5247 ------------------------
5249 procedure Make_Implicit_Base is
5250 begin
5251 Implicit_Base :=
5252 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
5254 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
5255 Set_Etype (Implicit_Base, Parent_Base);
5257 Copy_Array_Subtype_Attributes (Implicit_Base, Parent_Base);
5258 Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
5260 Set_Has_Delayed_Freeze (Implicit_Base, True);
5261 end Make_Implicit_Base;
5263 -- Start of processing for Build_Derived_Array_Type
5265 begin
5266 if not Is_Constrained (Parent_Type) then
5267 if Nkind (Indic) /= N_Subtype_Indication then
5268 Set_Ekind (Derived_Type, E_Array_Type);
5270 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
5271 Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
5273 Set_Has_Delayed_Freeze (Derived_Type, True);
5275 else
5276 Make_Implicit_Base;
5277 Set_Etype (Derived_Type, Implicit_Base);
5279 New_Indic :=
5280 Make_Subtype_Declaration (Loc,
5281 Defining_Identifier => Derived_Type,
5282 Subtype_Indication =>
5283 Make_Subtype_Indication (Loc,
5284 Subtype_Mark => New_Reference_To (Implicit_Base, Loc),
5285 Constraint => Constraint (Indic)));
5287 Rewrite (N, New_Indic);
5288 Analyze (N);
5289 end if;
5291 else
5292 if Nkind (Indic) /= N_Subtype_Indication then
5293 Make_Implicit_Base;
5295 Set_Ekind (Derived_Type, Ekind (Parent_Type));
5296 Set_Etype (Derived_Type, Implicit_Base);
5297 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
5299 else
5300 Error_Msg_N ("illegal constraint on constrained type", Indic);
5301 end if;
5302 end if;
5304 -- If parent type is not a derived type itself, and is declared in
5305 -- closed scope (e.g. a subprogram), then we must explicitly introduce
5306 -- the new type's concatenation operator since Derive_Subprograms
5307 -- will not inherit the parent's operator. If the parent type is
5308 -- unconstrained, the operator is of the unconstrained base type.
5310 if Number_Dimensions (Parent_Type) = 1
5311 and then not Is_Limited_Type (Parent_Type)
5312 and then not Is_Derived_Type (Parent_Type)
5313 and then not Is_Package_Or_Generic_Package
5314 (Scope (Base_Type (Parent_Type)))
5315 then
5316 if not Is_Constrained (Parent_Type)
5317 and then Is_Constrained (Derived_Type)
5318 then
5319 New_Concatenation_Op (Implicit_Base);
5320 else
5321 New_Concatenation_Op (Derived_Type);
5322 end if;
5323 end if;
5324 end Build_Derived_Array_Type;
5326 -----------------------------------
5327 -- Build_Derived_Concurrent_Type --
5328 -----------------------------------
5330 procedure Build_Derived_Concurrent_Type
5331 (N : Node_Id;
5332 Parent_Type : Entity_Id;
5333 Derived_Type : Entity_Id)
5335 Loc : constant Source_Ptr := Sloc (N);
5337 Corr_Record : constant Entity_Id := Make_Temporary (Loc, 'C');
5338 Corr_Decl : Node_Id;
5339 Corr_Decl_Needed : Boolean;
5340 -- If the derived type has fewer discriminants than its parent, the
5341 -- corresponding record is also a derived type, in order to account for
5342 -- the bound discriminants. We create a full type declaration for it in
5343 -- this case.
5345 Constraint_Present : constant Boolean :=
5346 Nkind (Subtype_Indication (Type_Definition (N))) =
5347 N_Subtype_Indication;
5349 D_Constraint : Node_Id;
5350 New_Constraint : Elist_Id;
5351 Old_Disc : Entity_Id;
5352 New_Disc : Entity_Id;
5353 New_N : Node_Id;
5355 begin
5356 Set_Stored_Constraint (Derived_Type, No_Elist);
5357 Corr_Decl_Needed := False;
5358 Old_Disc := Empty;
5360 if Present (Discriminant_Specifications (N))
5361 and then Constraint_Present
5362 then
5363 Old_Disc := First_Discriminant (Parent_Type);
5364 New_Disc := First (Discriminant_Specifications (N));
5365 while Present (New_Disc) and then Present (Old_Disc) loop
5366 Next_Discriminant (Old_Disc);
5367 Next (New_Disc);
5368 end loop;
5369 end if;
5371 if Present (Old_Disc) and then Expander_Active then
5373 -- The new type has fewer discriminants, so we need to create a new
5374 -- corresponding record, which is derived from the corresponding
5375 -- record of the parent, and has a stored constraint that captures
5376 -- the values of the discriminant constraints. The corresponding
5377 -- record is needed only if expander is active and code generation is
5378 -- enabled.
5380 -- The type declaration for the derived corresponding record has the
5381 -- same discriminant part and constraints as the current declaration.
5382 -- Copy the unanalyzed tree to build declaration.
5384 Corr_Decl_Needed := True;
5385 New_N := Copy_Separate_Tree (N);
5387 Corr_Decl :=
5388 Make_Full_Type_Declaration (Loc,
5389 Defining_Identifier => Corr_Record,
5390 Discriminant_Specifications =>
5391 Discriminant_Specifications (New_N),
5392 Type_Definition =>
5393 Make_Derived_Type_Definition (Loc,
5394 Subtype_Indication =>
5395 Make_Subtype_Indication (Loc,
5396 Subtype_Mark =>
5397 New_Occurrence_Of
5398 (Corresponding_Record_Type (Parent_Type), Loc),
5399 Constraint =>
5400 Constraint
5401 (Subtype_Indication (Type_Definition (New_N))))));
5402 end if;
5404 -- Copy Storage_Size and Relative_Deadline variables if task case
5406 if Is_Task_Type (Parent_Type) then
5407 Set_Storage_Size_Variable (Derived_Type,
5408 Storage_Size_Variable (Parent_Type));
5409 Set_Relative_Deadline_Variable (Derived_Type,
5410 Relative_Deadline_Variable (Parent_Type));
5411 end if;
5413 if Present (Discriminant_Specifications (N)) then
5414 Push_Scope (Derived_Type);
5415 Check_Or_Process_Discriminants (N, Derived_Type);
5417 if Constraint_Present then
5418 New_Constraint :=
5419 Expand_To_Stored_Constraint
5420 (Parent_Type,
5421 Build_Discriminant_Constraints
5422 (Parent_Type,
5423 Subtype_Indication (Type_Definition (N)), True));
5424 end if;
5426 End_Scope;
5428 elsif Constraint_Present then
5430 -- Build constrained subtype, copying the constraint, and derive
5431 -- from it to create a derived constrained type.
5433 declare
5434 Loc : constant Source_Ptr := Sloc (N);
5435 Anon : constant Entity_Id :=
5436 Make_Defining_Identifier (Loc,
5437 Chars => New_External_Name (Chars (Derived_Type), 'T'));
5438 Decl : Node_Id;
5440 begin
5441 Decl :=
5442 Make_Subtype_Declaration (Loc,
5443 Defining_Identifier => Anon,
5444 Subtype_Indication =>
5445 New_Copy_Tree (Subtype_Indication (Type_Definition (N))));
5446 Insert_Before (N, Decl);
5447 Analyze (Decl);
5449 Rewrite (Subtype_Indication (Type_Definition (N)),
5450 New_Occurrence_Of (Anon, Loc));
5451 Set_Analyzed (Derived_Type, False);
5452 Analyze (N);
5453 return;
5454 end;
5455 end if;
5457 -- By default, operations and private data are inherited from parent.
5458 -- However, in the presence of bound discriminants, a new corresponding
5459 -- record will be created, see below.
5461 Set_Has_Discriminants
5462 (Derived_Type, Has_Discriminants (Parent_Type));
5463 Set_Corresponding_Record_Type
5464 (Derived_Type, Corresponding_Record_Type (Parent_Type));
5466 -- Is_Constrained is set according the parent subtype, but is set to
5467 -- False if the derived type is declared with new discriminants.
5469 Set_Is_Constrained
5470 (Derived_Type,
5471 (Is_Constrained (Parent_Type) or else Constraint_Present)
5472 and then not Present (Discriminant_Specifications (N)));
5474 if Constraint_Present then
5475 if not Has_Discriminants (Parent_Type) then
5476 Error_Msg_N ("untagged parent must have discriminants", N);
5478 elsif Present (Discriminant_Specifications (N)) then
5480 -- Verify that new discriminants are used to constrain old ones
5482 D_Constraint :=
5483 First
5484 (Constraints
5485 (Constraint (Subtype_Indication (Type_Definition (N)))));
5487 Old_Disc := First_Discriminant (Parent_Type);
5489 while Present (D_Constraint) loop
5490 if Nkind (D_Constraint) /= N_Discriminant_Association then
5492 -- Positional constraint. If it is a reference to a new
5493 -- discriminant, it constrains the corresponding old one.
5495 if Nkind (D_Constraint) = N_Identifier then
5496 New_Disc := First_Discriminant (Derived_Type);
5497 while Present (New_Disc) loop
5498 exit when Chars (New_Disc) = Chars (D_Constraint);
5499 Next_Discriminant (New_Disc);
5500 end loop;
5502 if Present (New_Disc) then
5503 Set_Corresponding_Discriminant (New_Disc, Old_Disc);
5504 end if;
5505 end if;
5507 Next_Discriminant (Old_Disc);
5509 -- if this is a named constraint, search by name for the old
5510 -- discriminants constrained by the new one.
5512 elsif Nkind (Expression (D_Constraint)) = N_Identifier then
5514 -- Find new discriminant with that name
5516 New_Disc := First_Discriminant (Derived_Type);
5517 while Present (New_Disc) loop
5518 exit when
5519 Chars (New_Disc) = Chars (Expression (D_Constraint));
5520 Next_Discriminant (New_Disc);
5521 end loop;
5523 if Present (New_Disc) then
5525 -- Verify that new discriminant renames some discriminant
5526 -- of the parent type, and associate the new discriminant
5527 -- with one or more old ones that it renames.
5529 declare
5530 Selector : Node_Id;
5532 begin
5533 Selector := First (Selector_Names (D_Constraint));
5534 while Present (Selector) loop
5535 Old_Disc := First_Discriminant (Parent_Type);
5536 while Present (Old_Disc) loop
5537 exit when Chars (Old_Disc) = Chars (Selector);
5538 Next_Discriminant (Old_Disc);
5539 end loop;
5541 if Present (Old_Disc) then
5542 Set_Corresponding_Discriminant
5543 (New_Disc, Old_Disc);
5544 end if;
5546 Next (Selector);
5547 end loop;
5548 end;
5549 end if;
5550 end if;
5552 Next (D_Constraint);
5553 end loop;
5555 New_Disc := First_Discriminant (Derived_Type);
5556 while Present (New_Disc) loop
5557 if No (Corresponding_Discriminant (New_Disc)) then
5558 Error_Msg_NE
5559 ("new discriminant& must constrain old one", N, New_Disc);
5561 elsif not
5562 Subtypes_Statically_Compatible
5563 (Etype (New_Disc),
5564 Etype (Corresponding_Discriminant (New_Disc)))
5565 then
5566 Error_Msg_NE
5567 ("& not statically compatible with parent discriminant",
5568 N, New_Disc);
5569 end if;
5571 Next_Discriminant (New_Disc);
5572 end loop;
5573 end if;
5575 elsif Present (Discriminant_Specifications (N)) then
5576 Error_Msg_N
5577 ("missing discriminant constraint in untagged derivation", N);
5578 end if;
5580 -- The entity chain of the derived type includes the new discriminants
5581 -- but shares operations with the parent.
5583 if Present (Discriminant_Specifications (N)) then
5584 Old_Disc := First_Discriminant (Parent_Type);
5585 while Present (Old_Disc) loop
5586 if No (Next_Entity (Old_Disc))
5587 or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
5588 then
5589 Set_Next_Entity
5590 (Last_Entity (Derived_Type), Next_Entity (Old_Disc));
5591 exit;
5592 end if;
5594 Next_Discriminant (Old_Disc);
5595 end loop;
5597 else
5598 Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
5599 if Has_Discriminants (Parent_Type) then
5600 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
5601 Set_Discriminant_Constraint (
5602 Derived_Type, Discriminant_Constraint (Parent_Type));
5603 end if;
5604 end if;
5606 Set_Last_Entity (Derived_Type, Last_Entity (Parent_Type));
5608 Set_Has_Completion (Derived_Type);
5610 if Corr_Decl_Needed then
5611 Set_Stored_Constraint (Derived_Type, New_Constraint);
5612 Insert_After (N, Corr_Decl);
5613 Analyze (Corr_Decl);
5614 Set_Corresponding_Record_Type (Derived_Type, Corr_Record);
5615 end if;
5616 end Build_Derived_Concurrent_Type;
5618 ------------------------------------
5619 -- Build_Derived_Enumeration_Type --
5620 ------------------------------------
5622 procedure Build_Derived_Enumeration_Type
5623 (N : Node_Id;
5624 Parent_Type : Entity_Id;
5625 Derived_Type : Entity_Id)
5627 Loc : constant Source_Ptr := Sloc (N);
5628 Def : constant Node_Id := Type_Definition (N);
5629 Indic : constant Node_Id := Subtype_Indication (Def);
5630 Implicit_Base : Entity_Id;
5631 Literal : Entity_Id;
5632 New_Lit : Entity_Id;
5633 Literals_List : List_Id;
5634 Type_Decl : Node_Id;
5635 Hi, Lo : Node_Id;
5636 Rang_Expr : Node_Id;
5638 begin
5639 -- Since types Standard.Character and Standard.[Wide_]Wide_Character do
5640 -- not have explicit literals lists we need to process types derived
5641 -- from them specially. This is handled by Derived_Standard_Character.
5642 -- If the parent type is a generic type, there are no literals either,
5643 -- and we construct the same skeletal representation as for the generic
5644 -- parent type.
5646 if Is_Standard_Character_Type (Parent_Type) then
5647 Derived_Standard_Character (N, Parent_Type, Derived_Type);
5649 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
5650 declare
5651 Lo : Node_Id;
5652 Hi : Node_Id;
5654 begin
5655 if Nkind (Indic) /= N_Subtype_Indication then
5656 Lo :=
5657 Make_Attribute_Reference (Loc,
5658 Attribute_Name => Name_First,
5659 Prefix => New_Reference_To (Derived_Type, Loc));
5660 Set_Etype (Lo, Derived_Type);
5662 Hi :=
5663 Make_Attribute_Reference (Loc,
5664 Attribute_Name => Name_Last,
5665 Prefix => New_Reference_To (Derived_Type, Loc));
5666 Set_Etype (Hi, Derived_Type);
5668 Set_Scalar_Range (Derived_Type,
5669 Make_Range (Loc,
5670 Low_Bound => Lo,
5671 High_Bound => Hi));
5672 else
5674 -- Analyze subtype indication and verify compatibility
5675 -- with parent type.
5677 if Base_Type (Process_Subtype (Indic, N)) /=
5678 Base_Type (Parent_Type)
5679 then
5680 Error_Msg_N
5681 ("illegal constraint for formal discrete type", N);
5682 end if;
5683 end if;
5684 end;
5686 else
5687 -- If a constraint is present, analyze the bounds to catch
5688 -- premature usage of the derived literals.
5690 if Nkind (Indic) = N_Subtype_Indication
5691 and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
5692 then
5693 Analyze (Low_Bound (Range_Expression (Constraint (Indic))));
5694 Analyze (High_Bound (Range_Expression (Constraint (Indic))));
5695 end if;
5697 -- Introduce an implicit base type for the derived type even if there
5698 -- is no constraint attached to it, since this seems closer to the
5699 -- Ada semantics. Build a full type declaration tree for the derived
5700 -- type using the implicit base type as the defining identifier. The
5701 -- build a subtype declaration tree which applies the constraint (if
5702 -- any) have it replace the derived type declaration.
5704 Literal := First_Literal (Parent_Type);
5705 Literals_List := New_List;
5706 while Present (Literal)
5707 and then Ekind (Literal) = E_Enumeration_Literal
5708 loop
5709 -- Literals of the derived type have the same representation as
5710 -- those of the parent type, but this representation can be
5711 -- overridden by an explicit representation clause. Indicate
5712 -- that there is no explicit representation given yet. These
5713 -- derived literals are implicit operations of the new type,
5714 -- and can be overridden by explicit ones.
5716 if Nkind (Literal) = N_Defining_Character_Literal then
5717 New_Lit :=
5718 Make_Defining_Character_Literal (Loc, Chars (Literal));
5719 else
5720 New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
5721 end if;
5723 Set_Ekind (New_Lit, E_Enumeration_Literal);
5724 Set_Enumeration_Pos (New_Lit, Enumeration_Pos (Literal));
5725 Set_Enumeration_Rep (New_Lit, Enumeration_Rep (Literal));
5726 Set_Enumeration_Rep_Expr (New_Lit, Empty);
5727 Set_Alias (New_Lit, Literal);
5728 Set_Is_Known_Valid (New_Lit, True);
5730 Append (New_Lit, Literals_List);
5731 Next_Literal (Literal);
5732 end loop;
5734 Implicit_Base :=
5735 Make_Defining_Identifier (Sloc (Derived_Type),
5736 Chars => New_External_Name (Chars (Derived_Type), 'B'));
5738 -- Indicate the proper nature of the derived type. This must be done
5739 -- before analysis of the literals, to recognize cases when a literal
5740 -- may be hidden by a previous explicit function definition (cf.
5741 -- c83031a).
5743 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
5744 Set_Etype (Derived_Type, Implicit_Base);
5746 Type_Decl :=
5747 Make_Full_Type_Declaration (Loc,
5748 Defining_Identifier => Implicit_Base,
5749 Discriminant_Specifications => No_List,
5750 Type_Definition =>
5751 Make_Enumeration_Type_Definition (Loc, Literals_List));
5753 Mark_Rewrite_Insertion (Type_Decl);
5754 Insert_Before (N, Type_Decl);
5755 Analyze (Type_Decl);
5757 -- After the implicit base is analyzed its Etype needs to be changed
5758 -- to reflect the fact that it is derived from the parent type which
5759 -- was ignored during analysis. We also set the size at this point.
5761 Set_Etype (Implicit_Base, Parent_Type);
5763 Set_Size_Info (Implicit_Base, Parent_Type);
5764 Set_RM_Size (Implicit_Base, RM_Size (Parent_Type));
5765 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
5767 -- Copy other flags from parent type
5769 Set_Has_Non_Standard_Rep
5770 (Implicit_Base, Has_Non_Standard_Rep
5771 (Parent_Type));
5772 Set_Has_Pragma_Ordered
5773 (Implicit_Base, Has_Pragma_Ordered
5774 (Parent_Type));
5775 Set_Has_Delayed_Freeze (Implicit_Base);
5777 -- Process the subtype indication including a validation check on the
5778 -- constraint, if any. If a constraint is given, its bounds must be
5779 -- implicitly converted to the new type.
5781 if Nkind (Indic) = N_Subtype_Indication then
5782 declare
5783 R : constant Node_Id :=
5784 Range_Expression (Constraint (Indic));
5786 begin
5787 if Nkind (R) = N_Range then
5788 Hi := Build_Scalar_Bound
5789 (High_Bound (R), Parent_Type, Implicit_Base);
5790 Lo := Build_Scalar_Bound
5791 (Low_Bound (R), Parent_Type, Implicit_Base);
5793 else
5794 -- Constraint is a Range attribute. Replace with explicit
5795 -- mention of the bounds of the prefix, which must be a
5796 -- subtype.
5798 Analyze (Prefix (R));
5799 Hi :=
5800 Convert_To (Implicit_Base,
5801 Make_Attribute_Reference (Loc,
5802 Attribute_Name => Name_Last,
5803 Prefix =>
5804 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
5806 Lo :=
5807 Convert_To (Implicit_Base,
5808 Make_Attribute_Reference (Loc,
5809 Attribute_Name => Name_First,
5810 Prefix =>
5811 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
5812 end if;
5813 end;
5815 else
5816 Hi :=
5817 Build_Scalar_Bound
5818 (Type_High_Bound (Parent_Type),
5819 Parent_Type, Implicit_Base);
5820 Lo :=
5821 Build_Scalar_Bound
5822 (Type_Low_Bound (Parent_Type),
5823 Parent_Type, Implicit_Base);
5824 end if;
5826 Rang_Expr :=
5827 Make_Range (Loc,
5828 Low_Bound => Lo,
5829 High_Bound => Hi);
5831 -- If we constructed a default range for the case where no range
5832 -- was given, then the expressions in the range must not freeze
5833 -- since they do not correspond to expressions in the source.
5835 if Nkind (Indic) /= N_Subtype_Indication then
5836 Set_Must_Not_Freeze (Lo);
5837 Set_Must_Not_Freeze (Hi);
5838 Set_Must_Not_Freeze (Rang_Expr);
5839 end if;
5841 Rewrite (N,
5842 Make_Subtype_Declaration (Loc,
5843 Defining_Identifier => Derived_Type,
5844 Subtype_Indication =>
5845 Make_Subtype_Indication (Loc,
5846 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
5847 Constraint =>
5848 Make_Range_Constraint (Loc,
5849 Range_Expression => Rang_Expr))));
5851 Analyze (N);
5853 -- Apply a range check. Since this range expression doesn't have an
5854 -- Etype, we have to specifically pass the Source_Typ parameter. Is
5855 -- this right???
5857 if Nkind (Indic) = N_Subtype_Indication then
5858 Apply_Range_Check (Range_Expression (Constraint (Indic)),
5859 Parent_Type,
5860 Source_Typ => Entity (Subtype_Mark (Indic)));
5861 end if;
5862 end if;
5863 end Build_Derived_Enumeration_Type;
5865 --------------------------------
5866 -- Build_Derived_Numeric_Type --
5867 --------------------------------
5869 procedure Build_Derived_Numeric_Type
5870 (N : Node_Id;
5871 Parent_Type : Entity_Id;
5872 Derived_Type : Entity_Id)
5874 Loc : constant Source_Ptr := Sloc (N);
5875 Tdef : constant Node_Id := Type_Definition (N);
5876 Indic : constant Node_Id := Subtype_Indication (Tdef);
5877 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
5878 No_Constraint : constant Boolean := Nkind (Indic) /=
5879 N_Subtype_Indication;
5880 Implicit_Base : Entity_Id;
5882 Lo : Node_Id;
5883 Hi : Node_Id;
5885 begin
5886 -- Process the subtype indication including a validation check on
5887 -- the constraint if any.
5889 Discard_Node (Process_Subtype (Indic, N));
5891 -- Introduce an implicit base type for the derived type even if there
5892 -- is no constraint attached to it, since this seems closer to the Ada
5893 -- semantics.
5895 Implicit_Base :=
5896 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
5898 Set_Etype (Implicit_Base, Parent_Base);
5899 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
5900 Set_Size_Info (Implicit_Base, Parent_Base);
5901 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
5902 Set_Parent (Implicit_Base, Parent (Derived_Type));
5903 Set_Is_Known_Valid (Implicit_Base, Is_Known_Valid (Parent_Base));
5905 -- Set RM Size for discrete type or decimal fixed-point type
5906 -- Ordinary fixed-point is excluded, why???
5908 if Is_Discrete_Type (Parent_Base)
5909 or else Is_Decimal_Fixed_Point_Type (Parent_Base)
5910 then
5911 Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
5912 end if;
5914 Set_Has_Delayed_Freeze (Implicit_Base);
5916 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
5917 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
5919 Set_Scalar_Range (Implicit_Base,
5920 Make_Range (Loc,
5921 Low_Bound => Lo,
5922 High_Bound => Hi));
5924 if Has_Infinities (Parent_Base) then
5925 Set_Includes_Infinities (Scalar_Range (Implicit_Base));
5926 end if;
5928 -- The Derived_Type, which is the entity of the declaration, is a
5929 -- subtype of the implicit base. Its Ekind is a subtype, even in the
5930 -- absence of an explicit constraint.
5932 Set_Etype (Derived_Type, Implicit_Base);
5934 -- If we did not have a constraint, then the Ekind is set from the
5935 -- parent type (otherwise Process_Subtype has set the bounds)
5937 if No_Constraint then
5938 Set_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
5939 end if;
5941 -- If we did not have a range constraint, then set the range from the
5942 -- parent type. Otherwise, the Process_Subtype call has set the bounds.
5944 if No_Constraint
5945 or else not Has_Range_Constraint (Indic)
5946 then
5947 Set_Scalar_Range (Derived_Type,
5948 Make_Range (Loc,
5949 Low_Bound => New_Copy_Tree (Type_Low_Bound (Parent_Type)),
5950 High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
5951 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
5953 if Has_Infinities (Parent_Type) then
5954 Set_Includes_Infinities (Scalar_Range (Derived_Type));
5955 end if;
5957 Set_Is_Known_Valid (Derived_Type, Is_Known_Valid (Parent_Type));
5958 end if;
5960 Set_Is_Descendent_Of_Address (Derived_Type,
5961 Is_Descendent_Of_Address (Parent_Type));
5962 Set_Is_Descendent_Of_Address (Implicit_Base,
5963 Is_Descendent_Of_Address (Parent_Type));
5965 -- Set remaining type-specific fields, depending on numeric type
5967 if Is_Modular_Integer_Type (Parent_Type) then
5968 Set_Modulus (Implicit_Base, Modulus (Parent_Base));
5970 Set_Non_Binary_Modulus
5971 (Implicit_Base, Non_Binary_Modulus (Parent_Base));
5973 Set_Is_Known_Valid
5974 (Implicit_Base, Is_Known_Valid (Parent_Base));
5976 elsif Is_Floating_Point_Type (Parent_Type) then
5978 -- Digits of base type is always copied from the digits value of
5979 -- the parent base type, but the digits of the derived type will
5980 -- already have been set if there was a constraint present.
5982 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
5983 Set_Float_Rep (Implicit_Base, Float_Rep (Parent_Base));
5985 if No_Constraint then
5986 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
5987 end if;
5989 elsif Is_Fixed_Point_Type (Parent_Type) then
5991 -- Small of base type and derived type are always copied from the
5992 -- parent base type, since smalls never change. The delta of the
5993 -- base type is also copied from the parent base type. However the
5994 -- delta of the derived type will have been set already if a
5995 -- constraint was present.
5997 Set_Small_Value (Derived_Type, Small_Value (Parent_Base));
5998 Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
5999 Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
6001 if No_Constraint then
6002 Set_Delta_Value (Derived_Type, Delta_Value (Parent_Type));
6003 end if;
6005 -- The scale and machine radix in the decimal case are always
6006 -- copied from the parent base type.
6008 if Is_Decimal_Fixed_Point_Type (Parent_Type) then
6009 Set_Scale_Value (Derived_Type, Scale_Value (Parent_Base));
6010 Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
6012 Set_Machine_Radix_10
6013 (Derived_Type, Machine_Radix_10 (Parent_Base));
6014 Set_Machine_Radix_10
6015 (Implicit_Base, Machine_Radix_10 (Parent_Base));
6017 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
6019 if No_Constraint then
6020 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
6022 else
6023 -- the analysis of the subtype_indication sets the
6024 -- digits value of the derived type.
6026 null;
6027 end if;
6028 end if;
6029 end if;
6031 -- The type of the bounds is that of the parent type, and they
6032 -- must be converted to the derived type.
6034 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
6036 -- The implicit_base should be frozen when the derived type is frozen,
6037 -- but note that it is used in the conversions of the bounds. For fixed
6038 -- types we delay the determination of the bounds until the proper
6039 -- freezing point. For other numeric types this is rejected by GCC, for
6040 -- reasons that are currently unclear (???), so we choose to freeze the
6041 -- implicit base now. In the case of integers and floating point types
6042 -- this is harmless because subsequent representation clauses cannot
6043 -- affect anything, but it is still baffling that we cannot use the
6044 -- same mechanism for all derived numeric types.
6046 -- There is a further complication: actually *some* representation
6047 -- clauses can affect the implicit base type. Namely, attribute
6048 -- definition clauses for stream-oriented attributes need to set the
6049 -- corresponding TSS entries on the base type, and this normally cannot
6050 -- be done after the base type is frozen, so the circuitry in
6051 -- Sem_Ch13.New_Stream_Subprogram must account for this possibility and
6052 -- not use Set_TSS in this case.
6054 if Is_Fixed_Point_Type (Parent_Type) then
6055 Conditional_Delay (Implicit_Base, Parent_Type);
6056 else
6057 Freeze_Before (N, Implicit_Base);
6058 end if;
6059 end Build_Derived_Numeric_Type;
6061 --------------------------------
6062 -- Build_Derived_Private_Type --
6063 --------------------------------
6065 procedure Build_Derived_Private_Type
6066 (N : Node_Id;
6067 Parent_Type : Entity_Id;
6068 Derived_Type : Entity_Id;
6069 Is_Completion : Boolean;
6070 Derive_Subps : Boolean := True)
6072 Loc : constant Source_Ptr := Sloc (N);
6073 Der_Base : Entity_Id;
6074 Discr : Entity_Id;
6075 Full_Decl : Node_Id := Empty;
6076 Full_Der : Entity_Id;
6077 Full_P : Entity_Id;
6078 Last_Discr : Entity_Id;
6079 Par_Scope : constant Entity_Id := Scope (Base_Type (Parent_Type));
6080 Swapped : Boolean := False;
6082 procedure Copy_And_Build;
6083 -- Copy derived type declaration, replace parent with its full view,
6084 -- and analyze new declaration.
6086 --------------------
6087 -- Copy_And_Build --
6088 --------------------
6090 procedure Copy_And_Build is
6091 Full_N : Node_Id;
6093 begin
6094 if Ekind (Parent_Type) in Record_Kind
6095 or else
6096 (Ekind (Parent_Type) in Enumeration_Kind
6097 and then not Is_Standard_Character_Type (Parent_Type)
6098 and then not Is_Generic_Type (Root_Type (Parent_Type)))
6099 then
6100 Full_N := New_Copy_Tree (N);
6101 Insert_After (N, Full_N);
6102 Build_Derived_Type (
6103 Full_N, Parent_Type, Full_Der, True, Derive_Subps => False);
6105 else
6106 Build_Derived_Type (
6107 N, Parent_Type, Full_Der, True, Derive_Subps => False);
6108 end if;
6109 end Copy_And_Build;
6111 -- Start of processing for Build_Derived_Private_Type
6113 begin
6114 if Is_Tagged_Type (Parent_Type) then
6115 Full_P := Full_View (Parent_Type);
6117 -- A type extension of a type with unknown discriminants is an
6118 -- indefinite type that the back-end cannot handle directly.
6119 -- We treat it as a private type, and build a completion that is
6120 -- derived from the full view of the parent, and hopefully has
6121 -- known discriminants.
6123 -- If the full view of the parent type has an underlying record view,
6124 -- use it to generate the underlying record view of this derived type
6125 -- (required for chains of derivations with unknown discriminants).
6127 -- Minor optimization: we avoid the generation of useless underlying
6128 -- record view entities if the private type declaration has unknown
6129 -- discriminants but its corresponding full view has no
6130 -- discriminants.
6132 if Has_Unknown_Discriminants (Parent_Type)
6133 and then Present (Full_P)
6134 and then (Has_Discriminants (Full_P)
6135 or else Present (Underlying_Record_View (Full_P)))
6136 and then not In_Open_Scopes (Par_Scope)
6137 and then Expander_Active
6138 then
6139 declare
6140 Full_Der : constant Entity_Id := Make_Temporary (Loc, 'T');
6141 New_Ext : constant Node_Id :=
6142 Copy_Separate_Tree
6143 (Record_Extension_Part (Type_Definition (N)));
6144 Decl : Node_Id;
6146 begin
6147 Build_Derived_Record_Type
6148 (N, Parent_Type, Derived_Type, Derive_Subps);
6150 -- Build anonymous completion, as a derivation from the full
6151 -- view of the parent. This is not a completion in the usual
6152 -- sense, because the current type is not private.
6154 Decl :=
6155 Make_Full_Type_Declaration (Loc,
6156 Defining_Identifier => Full_Der,
6157 Type_Definition =>
6158 Make_Derived_Type_Definition (Loc,
6159 Subtype_Indication =>
6160 New_Copy_Tree
6161 (Subtype_Indication (Type_Definition (N))),
6162 Record_Extension_Part => New_Ext));
6164 -- If the parent type has an underlying record view, use it
6165 -- here to build the new underlying record view.
6167 if Present (Underlying_Record_View (Full_P)) then
6168 pragma Assert
6169 (Nkind (Subtype_Indication (Type_Definition (Decl)))
6170 = N_Identifier);
6171 Set_Entity (Subtype_Indication (Type_Definition (Decl)),
6172 Underlying_Record_View (Full_P));
6173 end if;
6175 Install_Private_Declarations (Par_Scope);
6176 Install_Visible_Declarations (Par_Scope);
6177 Insert_Before (N, Decl);
6179 -- Mark entity as an underlying record view before analysis,
6180 -- to avoid generating the list of its primitive operations
6181 -- (which is not really required for this entity) and thus
6182 -- prevent spurious errors associated with missing overriding
6183 -- of abstract primitives (overridden only for Derived_Type).
6185 Set_Ekind (Full_Der, E_Record_Type);
6186 Set_Is_Underlying_Record_View (Full_Der);
6188 Analyze (Decl);
6190 pragma Assert (Has_Discriminants (Full_Der)
6191 and then not Has_Unknown_Discriminants (Full_Der));
6193 Uninstall_Declarations (Par_Scope);
6195 -- Freeze the underlying record view, to prevent generation of
6196 -- useless dispatching information, which is simply shared with
6197 -- the real derived type.
6199 Set_Is_Frozen (Full_Der);
6201 -- Set up links between real entity and underlying record view
6203 Set_Underlying_Record_View (Derived_Type, Base_Type (Full_Der));
6204 Set_Underlying_Record_View (Base_Type (Full_Der), Derived_Type);
6205 end;
6207 -- If discriminants are known, build derived record
6209 else
6210 Build_Derived_Record_Type
6211 (N, Parent_Type, Derived_Type, Derive_Subps);
6212 end if;
6214 return;
6216 elsif Has_Discriminants (Parent_Type) then
6217 if Present (Full_View (Parent_Type)) then
6218 if not Is_Completion then
6220 -- Copy declaration for subsequent analysis, to provide a
6221 -- completion for what is a private declaration. Indicate that
6222 -- the full type is internally generated.
6224 Full_Decl := New_Copy_Tree (N);
6225 Full_Der := New_Copy (Derived_Type);
6226 Set_Comes_From_Source (Full_Decl, False);
6227 Set_Comes_From_Source (Full_Der, False);
6228 Set_Parent (Full_Der, Full_Decl);
6230 Insert_After (N, Full_Decl);
6232 else
6233 -- If this is a completion, the full view being built is itself
6234 -- private. We build a subtype of the parent with the same
6235 -- constraints as this full view, to convey to the back end the
6236 -- constrained components and the size of this subtype. If the
6237 -- parent is constrained, its full view can serve as the
6238 -- underlying full view of the derived type.
6240 if No (Discriminant_Specifications (N)) then
6241 if Nkind (Subtype_Indication (Type_Definition (N))) =
6242 N_Subtype_Indication
6243 then
6244 Build_Underlying_Full_View (N, Derived_Type, Parent_Type);
6246 elsif Is_Constrained (Full_View (Parent_Type)) then
6247 Set_Underlying_Full_View
6248 (Derived_Type, Full_View (Parent_Type));
6249 end if;
6251 else
6252 -- If there are new discriminants, the parent subtype is
6253 -- constrained by them, but it is not clear how to build
6254 -- the Underlying_Full_View in this case???
6256 null;
6257 end if;
6258 end if;
6259 end if;
6261 -- Build partial view of derived type from partial view of parent
6263 Build_Derived_Record_Type
6264 (N, Parent_Type, Derived_Type, Derive_Subps);
6266 if Present (Full_View (Parent_Type)) and then not Is_Completion then
6267 if not In_Open_Scopes (Par_Scope)
6268 or else not In_Same_Source_Unit (N, Parent_Type)
6269 then
6270 -- Swap partial and full views temporarily
6272 Install_Private_Declarations (Par_Scope);
6273 Install_Visible_Declarations (Par_Scope);
6274 Swapped := True;
6275 end if;
6277 -- Build full view of derived type from full view of parent which
6278 -- is now installed. Subprograms have been derived on the partial
6279 -- view, the completion does not derive them anew.
6281 if not Is_Tagged_Type (Parent_Type) then
6283 -- If the parent is itself derived from another private type,
6284 -- installing the private declarations has not affected its
6285 -- privacy status, so use its own full view explicitly.
6287 if Is_Private_Type (Parent_Type) then
6288 Build_Derived_Record_Type
6289 (Full_Decl, Full_View (Parent_Type), Full_Der, False);
6290 else
6291 Build_Derived_Record_Type
6292 (Full_Decl, Parent_Type, Full_Der, False);
6293 end if;
6295 else
6296 -- If full view of parent is tagged, the completion inherits
6297 -- the proper primitive operations.
6299 Set_Defining_Identifier (Full_Decl, Full_Der);
6300 Build_Derived_Record_Type
6301 (Full_Decl, Parent_Type, Full_Der, Derive_Subps);
6302 end if;
6304 -- The full declaration has been introduced into the tree and
6305 -- processed in the step above. It should not be analyzed again
6306 -- (when encountered later in the current list of declarations)
6307 -- to prevent spurious name conflicts. The full entity remains
6308 -- invisible.
6310 Set_Analyzed (Full_Decl);
6312 if Swapped then
6313 Uninstall_Declarations (Par_Scope);
6315 if In_Open_Scopes (Par_Scope) then
6316 Install_Visible_Declarations (Par_Scope);
6317 end if;
6318 end if;
6320 Der_Base := Base_Type (Derived_Type);
6321 Set_Full_View (Derived_Type, Full_Der);
6322 Set_Full_View (Der_Base, Base_Type (Full_Der));
6324 -- Copy the discriminant list from full view to the partial views
6325 -- (base type and its subtype). Gigi requires that the partial and
6326 -- full views have the same discriminants.
6328 -- Note that since the partial view is pointing to discriminants
6329 -- in the full view, their scope will be that of the full view.
6330 -- This might cause some front end problems and need adjustment???
6332 Discr := First_Discriminant (Base_Type (Full_Der));
6333 Set_First_Entity (Der_Base, Discr);
6335 loop
6336 Last_Discr := Discr;
6337 Next_Discriminant (Discr);
6338 exit when No (Discr);
6339 end loop;
6341 Set_Last_Entity (Der_Base, Last_Discr);
6343 Set_First_Entity (Derived_Type, First_Entity (Der_Base));
6344 Set_Last_Entity (Derived_Type, Last_Entity (Der_Base));
6345 Set_Stored_Constraint (Full_Der, Stored_Constraint (Derived_Type));
6347 else
6348 -- If this is a completion, the derived type stays private and
6349 -- there is no need to create a further full view, except in the
6350 -- unusual case when the derivation is nested within a child unit,
6351 -- see below.
6353 null;
6354 end if;
6356 elsif Present (Full_View (Parent_Type))
6357 and then Has_Discriminants (Full_View (Parent_Type))
6358 then
6359 if Has_Unknown_Discriminants (Parent_Type)
6360 and then Nkind (Subtype_Indication (Type_Definition (N))) =
6361 N_Subtype_Indication
6362 then
6363 Error_Msg_N
6364 ("cannot constrain type with unknown discriminants",
6365 Subtype_Indication (Type_Definition (N)));
6366 return;
6367 end if;
6369 -- If full view of parent is a record type, build full view as a
6370 -- derivation from the parent's full view. Partial view remains
6371 -- private. For code generation and linking, the full view must have
6372 -- the same public status as the partial one. This full view is only
6373 -- needed if the parent type is in an enclosing scope, so that the
6374 -- full view may actually become visible, e.g. in a child unit. This
6375 -- is both more efficient, and avoids order of freezing problems with
6376 -- the added entities.
6378 if not Is_Private_Type (Full_View (Parent_Type))
6379 and then (In_Open_Scopes (Scope (Parent_Type)))
6380 then
6381 Full_Der :=
6382 Make_Defining_Identifier
6383 (Sloc (Derived_Type), Chars (Derived_Type));
6384 Set_Is_Itype (Full_Der);
6385 Set_Has_Private_Declaration (Full_Der);
6386 Set_Has_Private_Declaration (Derived_Type);
6387 Set_Associated_Node_For_Itype (Full_Der, N);
6388 Set_Parent (Full_Der, Parent (Derived_Type));
6389 Set_Full_View (Derived_Type, Full_Der);
6390 Set_Is_Public (Full_Der, Is_Public (Derived_Type));
6391 Full_P := Full_View (Parent_Type);
6392 Exchange_Declarations (Parent_Type);
6393 Copy_And_Build;
6394 Exchange_Declarations (Full_P);
6396 else
6397 Build_Derived_Record_Type
6398 (N, Full_View (Parent_Type), Derived_Type,
6399 Derive_Subps => False);
6400 end if;
6402 -- In any case, the primitive operations are inherited from the
6403 -- parent type, not from the internal full view.
6405 Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
6407 if Derive_Subps then
6408 Derive_Subprograms (Parent_Type, Derived_Type);
6409 end if;
6411 else
6412 -- Untagged type, No discriminants on either view
6414 if Nkind (Subtype_Indication (Type_Definition (N))) =
6415 N_Subtype_Indication
6416 then
6417 Error_Msg_N
6418 ("illegal constraint on type without discriminants", N);
6419 end if;
6421 if Present (Discriminant_Specifications (N))
6422 and then Present (Full_View (Parent_Type))
6423 and then not Is_Tagged_Type (Full_View (Parent_Type))
6424 then
6425 Error_Msg_N ("cannot add discriminants to untagged type", N);
6426 end if;
6428 Set_Stored_Constraint (Derived_Type, No_Elist);
6429 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
6430 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
6431 Set_Has_Controlled_Component
6432 (Derived_Type, Has_Controlled_Component
6433 (Parent_Type));
6435 -- Direct controlled types do not inherit Finalize_Storage_Only flag
6437 if not Is_Controlled (Parent_Type) then
6438 Set_Finalize_Storage_Only
6439 (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
6440 end if;
6442 -- Construct the implicit full view by deriving from full view of the
6443 -- parent type. In order to get proper visibility, we install the
6444 -- parent scope and its declarations.
6446 -- ??? If the parent is untagged private and its completion is
6447 -- tagged, this mechanism will not work because we cannot derive from
6448 -- the tagged full view unless we have an extension.
6450 if Present (Full_View (Parent_Type))
6451 and then not Is_Tagged_Type (Full_View (Parent_Type))
6452 and then not Is_Completion
6453 then
6454 Full_Der :=
6455 Make_Defining_Identifier
6456 (Sloc (Derived_Type), Chars (Derived_Type));
6457 Set_Is_Itype (Full_Der);
6458 Set_Has_Private_Declaration (Full_Der);
6459 Set_Has_Private_Declaration (Derived_Type);
6460 Set_Associated_Node_For_Itype (Full_Der, N);
6461 Set_Parent (Full_Der, Parent (Derived_Type));
6462 Set_Full_View (Derived_Type, Full_Der);
6464 if not In_Open_Scopes (Par_Scope) then
6465 Install_Private_Declarations (Par_Scope);
6466 Install_Visible_Declarations (Par_Scope);
6467 Copy_And_Build;
6468 Uninstall_Declarations (Par_Scope);
6470 -- If parent scope is open and in another unit, and parent has a
6471 -- completion, then the derivation is taking place in the visible
6472 -- part of a child unit. In that case retrieve the full view of
6473 -- the parent momentarily.
6475 elsif not In_Same_Source_Unit (N, Parent_Type) then
6476 Full_P := Full_View (Parent_Type);
6477 Exchange_Declarations (Parent_Type);
6478 Copy_And_Build;
6479 Exchange_Declarations (Full_P);
6481 -- Otherwise it is a local derivation
6483 else
6484 Copy_And_Build;
6485 end if;
6487 Set_Scope (Full_Der, Current_Scope);
6488 Set_Is_First_Subtype (Full_Der,
6489 Is_First_Subtype (Derived_Type));
6490 Set_Has_Size_Clause (Full_Der, False);
6491 Set_Has_Alignment_Clause (Full_Der, False);
6492 Set_Next_Entity (Full_Der, Empty);
6493 Set_Has_Delayed_Freeze (Full_Der);
6494 Set_Is_Frozen (Full_Der, False);
6495 Set_Freeze_Node (Full_Der, Empty);
6496 Set_Depends_On_Private (Full_Der,
6497 Has_Private_Component (Full_Der));
6498 Set_Public_Status (Full_Der);
6499 end if;
6500 end if;
6502 Set_Has_Unknown_Discriminants (Derived_Type,
6503 Has_Unknown_Discriminants (Parent_Type));
6505 if Is_Private_Type (Derived_Type) then
6506 Set_Private_Dependents (Derived_Type, New_Elmt_List);
6507 end if;
6509 if Is_Private_Type (Parent_Type)
6510 and then Base_Type (Parent_Type) = Parent_Type
6511 and then In_Open_Scopes (Scope (Parent_Type))
6512 then
6513 Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
6515 if Is_Child_Unit (Scope (Current_Scope))
6516 and then Is_Completion
6517 and then In_Private_Part (Current_Scope)
6518 and then Scope (Parent_Type) /= Current_Scope
6519 then
6520 -- This is the unusual case where a type completed by a private
6521 -- derivation occurs within a package nested in a child unit, and
6522 -- the parent is declared in an ancestor. In this case, the full
6523 -- view of the parent type will become visible in the body of
6524 -- the enclosing child, and only then will the current type be
6525 -- possibly non-private. We build a underlying full view that
6526 -- will be installed when the enclosing child body is compiled.
6528 Full_Der :=
6529 Make_Defining_Identifier
6530 (Sloc (Derived_Type), Chars (Derived_Type));
6531 Set_Is_Itype (Full_Der);
6532 Build_Itype_Reference (Full_Der, N);
6534 -- The full view will be used to swap entities on entry/exit to
6535 -- the body, and must appear in the entity list for the package.
6537 Append_Entity (Full_Der, Scope (Derived_Type));
6538 Set_Has_Private_Declaration (Full_Der);
6539 Set_Has_Private_Declaration (Derived_Type);
6540 Set_Associated_Node_For_Itype (Full_Der, N);
6541 Set_Parent (Full_Der, Parent (Derived_Type));
6542 Full_P := Full_View (Parent_Type);
6543 Exchange_Declarations (Parent_Type);
6544 Copy_And_Build;
6545 Exchange_Declarations (Full_P);
6546 Set_Underlying_Full_View (Derived_Type, Full_Der);
6547 end if;
6548 end if;
6549 end Build_Derived_Private_Type;
6551 -------------------------------
6552 -- Build_Derived_Record_Type --
6553 -------------------------------
6555 -- 1. INTRODUCTION
6557 -- Ideally we would like to use the same model of type derivation for
6558 -- tagged and untagged record types. Unfortunately this is not quite
6559 -- possible because the semantics of representation clauses is different
6560 -- for tagged and untagged records under inheritance. Consider the
6561 -- following:
6563 -- type R (...) is [tagged] record ... end record;
6564 -- type T (...) is new R (...) [with ...];
6566 -- The representation clauses for T can specify a completely different
6567 -- record layout from R's. Hence the same component can be placed in two
6568 -- very different positions in objects of type T and R. If R and T are
6569 -- tagged types, representation clauses for T can only specify the layout
6570 -- of non inherited components, thus components that are common in R and T
6571 -- have the same position in objects of type R and T.
6573 -- This has two implications. The first is that the entire tree for R's
6574 -- declaration needs to be copied for T in the untagged case, so that T
6575 -- can be viewed as a record type of its own with its own representation
6576 -- clauses. The second implication is the way we handle discriminants.
6577 -- Specifically, in the untagged case we need a way to communicate to Gigi
6578 -- what are the real discriminants in the record, while for the semantics
6579 -- we need to consider those introduced by the user to rename the
6580 -- discriminants in the parent type. This is handled by introducing the
6581 -- notion of stored discriminants. See below for more.
6583 -- Fortunately the way regular components are inherited can be handled in
6584 -- the same way in tagged and untagged types.
6586 -- To complicate things a bit more the private view of a private extension
6587 -- cannot be handled in the same way as the full view (for one thing the
6588 -- semantic rules are somewhat different). We will explain what differs
6589 -- below.
6591 -- 2. DISCRIMINANTS UNDER INHERITANCE
6593 -- The semantic rules governing the discriminants of derived types are
6594 -- quite subtle.
6596 -- type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
6597 -- [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
6599 -- If parent type has discriminants, then the discriminants that are
6600 -- declared in the derived type are [3.4 (11)]:
6602 -- o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
6603 -- there is one;
6605 -- o Otherwise, each discriminant of the parent type (implicitly declared
6606 -- in the same order with the same specifications). In this case, the
6607 -- discriminants are said to be "inherited", or if unknown in the parent
6608 -- are also unknown in the derived type.
6610 -- Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
6612 -- o The parent subtype shall be constrained;
6614 -- o If the parent type is not a tagged type, then each discriminant of
6615 -- the derived type shall be used in the constraint defining a parent
6616 -- subtype. [Implementation note: This ensures that the new discriminant
6617 -- can share storage with an existing discriminant.]
6619 -- For the derived type each discriminant of the parent type is either
6620 -- inherited, constrained to equal some new discriminant of the derived
6621 -- type, or constrained to the value of an expression.
6623 -- When inherited or constrained to equal some new discriminant, the
6624 -- parent discriminant and the discriminant of the derived type are said
6625 -- to "correspond".
6627 -- If a discriminant of the parent type is constrained to a specific value
6628 -- in the derived type definition, then the discriminant is said to be
6629 -- "specified" by that derived type definition.
6631 -- 3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
6633 -- We have spoken about stored discriminants in point 1 (introduction)
6634 -- above. There are two sort of stored discriminants: implicit and
6635 -- explicit. As long as the derived type inherits the same discriminants as
6636 -- the root record type, stored discriminants are the same as regular
6637 -- discriminants, and are said to be implicit. However, if any discriminant
6638 -- in the root type was renamed in the derived type, then the derived
6639 -- type will contain explicit stored discriminants. Explicit stored
6640 -- discriminants are discriminants in addition to the semantically visible
6641 -- discriminants defined for the derived type. Stored discriminants are
6642 -- used by Gigi to figure out what are the physical discriminants in
6643 -- objects of the derived type (see precise definition in einfo.ads).
6644 -- As an example, consider the following:
6646 -- type R (D1, D2, D3 : Int) is record ... end record;
6647 -- type T1 is new R;
6648 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
6649 -- type T3 is new T2;
6650 -- type T4 (Y : Int) is new T3 (Y, 99);
6652 -- The following table summarizes the discriminants and stored
6653 -- discriminants in R and T1 through T4.
6655 -- Type Discrim Stored Discrim Comment
6656 -- R (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in R
6657 -- T1 (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in T1
6658 -- T2 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T2
6659 -- T3 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T3
6660 -- T4 (Y) (D1, D2, D3) Girder discrims EXPLICIT in T4
6662 -- Field Corresponding_Discriminant (abbreviated CD below) allows us to
6663 -- find the corresponding discriminant in the parent type, while
6664 -- Original_Record_Component (abbreviated ORC below), the actual physical
6665 -- component that is renamed. Finally the field Is_Completely_Hidden
6666 -- (abbreviated ICH below) is set for all explicit stored discriminants
6667 -- (see einfo.ads for more info). For the above example this gives:
6669 -- Discrim CD ORC ICH
6670 -- ^^^^^^^ ^^ ^^^ ^^^
6671 -- D1 in R empty itself no
6672 -- D2 in R empty itself no
6673 -- D3 in R empty itself no
6675 -- D1 in T1 D1 in R itself no
6676 -- D2 in T1 D2 in R itself no
6677 -- D3 in T1 D3 in R itself no
6679 -- X1 in T2 D3 in T1 D3 in T2 no
6680 -- X2 in T2 D1 in T1 D1 in T2 no
6681 -- D1 in T2 empty itself yes
6682 -- D2 in T2 empty itself yes
6683 -- D3 in T2 empty itself yes
6685 -- X1 in T3 X1 in T2 D3 in T3 no
6686 -- X2 in T3 X2 in T2 D1 in T3 no
6687 -- D1 in T3 empty itself yes
6688 -- D2 in T3 empty itself yes
6689 -- D3 in T3 empty itself yes
6691 -- Y in T4 X1 in T3 D3 in T3 no
6692 -- D1 in T3 empty itself yes
6693 -- D2 in T3 empty itself yes
6694 -- D3 in T3 empty itself yes
6696 -- 4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
6698 -- Type derivation for tagged types is fairly straightforward. If no
6699 -- discriminants are specified by the derived type, these are inherited
6700 -- from the parent. No explicit stored discriminants are ever necessary.
6701 -- The only manipulation that is done to the tree is that of adding a
6702 -- _parent field with parent type and constrained to the same constraint
6703 -- specified for the parent in the derived type definition. For instance:
6705 -- type R (D1, D2, D3 : Int) is tagged record ... end record;
6706 -- type T1 is new R with null record;
6707 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
6709 -- are changed into:
6711 -- type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
6712 -- _parent : R (D1, D2, D3);
6713 -- end record;
6715 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
6716 -- _parent : T1 (X2, 88, X1);
6717 -- end record;
6719 -- The discriminants actually present in R, T1 and T2 as well as their CD,
6720 -- ORC and ICH fields are:
6722 -- Discrim CD ORC ICH
6723 -- ^^^^^^^ ^^ ^^^ ^^^
6724 -- D1 in R empty itself no
6725 -- D2 in R empty itself no
6726 -- D3 in R empty itself no
6728 -- D1 in T1 D1 in R D1 in R no
6729 -- D2 in T1 D2 in R D2 in R no
6730 -- D3 in T1 D3 in R D3 in R no
6732 -- X1 in T2 D3 in T1 D3 in R no
6733 -- X2 in T2 D1 in T1 D1 in R no
6735 -- 5. FIRST TRANSFORMATION FOR DERIVED RECORDS
6737 -- Regardless of whether we dealing with a tagged or untagged type
6738 -- we will transform all derived type declarations of the form
6740 -- type T is new R (...) [with ...];
6741 -- or
6742 -- subtype S is R (...);
6743 -- type T is new S [with ...];
6744 -- into
6745 -- type BT is new R [with ...];
6746 -- subtype T is BT (...);
6748 -- That is, the base derived type is constrained only if it has no
6749 -- discriminants. The reason for doing this is that GNAT's semantic model
6750 -- assumes that a base type with discriminants is unconstrained.
6752 -- Note that, strictly speaking, the above transformation is not always
6753 -- correct. Consider for instance the following excerpt from ACVC b34011a:
6755 -- procedure B34011A is
6756 -- type REC (D : integer := 0) is record
6757 -- I : Integer;
6758 -- end record;
6760 -- package P is
6761 -- type T6 is new Rec;
6762 -- function F return T6;
6763 -- end P;
6765 -- use P;
6766 -- package Q6 is
6767 -- type U is new T6 (Q6.F.I); -- ERROR: Q6.F.
6768 -- end Q6;
6770 -- The definition of Q6.U is illegal. However transforming Q6.U into
6772 -- type BaseU is new T6;
6773 -- subtype U is BaseU (Q6.F.I)
6775 -- turns U into a legal subtype, which is incorrect. To avoid this problem
6776 -- we always analyze the constraint (in this case (Q6.F.I)) before applying
6777 -- the transformation described above.
6779 -- There is another instance where the above transformation is incorrect.
6780 -- Consider:
6782 -- package Pack is
6783 -- type Base (D : Integer) is tagged null record;
6784 -- procedure P (X : Base);
6786 -- type Der is new Base (2) with null record;
6787 -- procedure P (X : Der);
6788 -- end Pack;
6790 -- Then the above transformation turns this into
6792 -- type Der_Base is new Base with null record;
6793 -- -- procedure P (X : Base) is implicitly inherited here
6794 -- -- as procedure P (X : Der_Base).
6796 -- subtype Der is Der_Base (2);
6797 -- procedure P (X : Der);
6798 -- -- The overriding of P (X : Der_Base) is illegal since we
6799 -- -- have a parameter conformance problem.
6801 -- To get around this problem, after having semantically processed Der_Base
6802 -- and the rewritten subtype declaration for Der, we copy Der_Base field
6803 -- Discriminant_Constraint from Der so that when parameter conformance is
6804 -- checked when P is overridden, no semantic errors are flagged.
6806 -- 6. SECOND TRANSFORMATION FOR DERIVED RECORDS
6808 -- Regardless of whether we are dealing with a tagged or untagged type
6809 -- we will transform all derived type declarations of the form
6811 -- type R (D1, .., Dn : ...) is [tagged] record ...;
6812 -- type T is new R [with ...];
6813 -- into
6814 -- type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
6816 -- The reason for such transformation is that it allows us to implement a
6817 -- very clean form of component inheritance as explained below.
6819 -- Note that this transformation is not achieved by direct tree rewriting
6820 -- and manipulation, but rather by redoing the semantic actions that the
6821 -- above transformation will entail. This is done directly in routine
6822 -- Inherit_Components.
6824 -- 7. TYPE DERIVATION AND COMPONENT INHERITANCE
6826 -- In both tagged and untagged derived types, regular non discriminant
6827 -- components are inherited in the derived type from the parent type. In
6828 -- the absence of discriminants component, inheritance is straightforward
6829 -- as components can simply be copied from the parent.
6831 -- If the parent has discriminants, inheriting components constrained with
6832 -- these discriminants requires caution. Consider the following example:
6834 -- type R (D1, D2 : Positive) is [tagged] record
6835 -- S : String (D1 .. D2);
6836 -- end record;
6838 -- type T1 is new R [with null record];
6839 -- type T2 (X : positive) is new R (1, X) [with null record];
6841 -- As explained in 6. above, T1 is rewritten as
6842 -- type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
6843 -- which makes the treatment for T1 and T2 identical.
6845 -- What we want when inheriting S, is that references to D1 and D2 in R are
6846 -- replaced with references to their correct constraints, i.e. D1 and D2 in
6847 -- T1 and 1 and X in T2. So all R's discriminant references are replaced
6848 -- with either discriminant references in the derived type or expressions.
6849 -- This replacement is achieved as follows: before inheriting R's
6850 -- components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
6851 -- created in the scope of T1 (resp. scope of T2) so that discriminants D1
6852 -- and D2 of T1 are visible (resp. discriminant X of T2 is visible).
6853 -- For T2, for instance, this has the effect of replacing String (D1 .. D2)
6854 -- by String (1 .. X).
6856 -- 8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
6858 -- We explain here the rules governing private type extensions relevant to
6859 -- type derivation. These rules are explained on the following example:
6861 -- type D [(...)] is new A [(...)] with private; <-- partial view
6862 -- type D [(...)] is new P [(...)] with null record; <-- full view
6864 -- Type A is called the ancestor subtype of the private extension.
6865 -- Type P is the parent type of the full view of the private extension. It
6866 -- must be A or a type derived from A.
6868 -- The rules concerning the discriminants of private type extensions are
6869 -- [7.3(10-13)]:
6871 -- o If a private extension inherits known discriminants from the ancestor
6872 -- subtype, then the full view shall also inherit its discriminants from
6873 -- the ancestor subtype and the parent subtype of the full view shall be
6874 -- constrained if and only if the ancestor subtype is constrained.
6876 -- o If a partial view has unknown discriminants, then the full view may
6877 -- define a definite or an indefinite subtype, with or without
6878 -- discriminants.
6880 -- o If a partial view has neither known nor unknown discriminants, then
6881 -- the full view shall define a definite subtype.
6883 -- o If the ancestor subtype of a private extension has constrained
6884 -- discriminants, then the parent subtype of the full view shall impose a
6885 -- statically matching constraint on those discriminants.
6887 -- This means that only the following forms of private extensions are
6888 -- allowed:
6890 -- type D is new A with private; <-- partial view
6891 -- type D is new P with null record; <-- full view
6893 -- If A has no discriminants than P has no discriminants, otherwise P must
6894 -- inherit A's discriminants.
6896 -- type D is new A (...) with private; <-- partial view
6897 -- type D is new P (:::) with null record; <-- full view
6899 -- P must inherit A's discriminants and (...) and (:::) must statically
6900 -- match.
6902 -- subtype A is R (...);
6903 -- type D is new A with private; <-- partial view
6904 -- type D is new P with null record; <-- full view
6906 -- P must have inherited R's discriminants and must be derived from A or
6907 -- any of its subtypes.
6909 -- type D (..) is new A with private; <-- partial view
6910 -- type D (..) is new P [(:::)] with null record; <-- full view
6912 -- No specific constraints on P's discriminants or constraint (:::).
6913 -- Note that A can be unconstrained, but the parent subtype P must either
6914 -- be constrained or (:::) must be present.
6916 -- type D (..) is new A [(...)] with private; <-- partial view
6917 -- type D (..) is new P [(:::)] with null record; <-- full view
6919 -- P's constraints on A's discriminants must statically match those
6920 -- imposed by (...).
6922 -- 9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
6924 -- The full view of a private extension is handled exactly as described
6925 -- above. The model chose for the private view of a private extension is
6926 -- the same for what concerns discriminants (i.e. they receive the same
6927 -- treatment as in the tagged case). However, the private view of the
6928 -- private extension always inherits the components of the parent base,
6929 -- without replacing any discriminant reference. Strictly speaking this is
6930 -- incorrect. However, Gigi never uses this view to generate code so this
6931 -- is a purely semantic issue. In theory, a set of transformations similar
6932 -- to those given in 5. and 6. above could be applied to private views of
6933 -- private extensions to have the same model of component inheritance as
6934 -- for non private extensions. However, this is not done because it would
6935 -- further complicate private type processing. Semantically speaking, this
6936 -- leaves us in an uncomfortable situation. As an example consider:
6938 -- package Pack is
6939 -- type R (D : integer) is tagged record
6940 -- S : String (1 .. D);
6941 -- end record;
6942 -- procedure P (X : R);
6943 -- type T is new R (1) with private;
6944 -- private
6945 -- type T is new R (1) with null record;
6946 -- end;
6948 -- This is transformed into:
6950 -- package Pack is
6951 -- type R (D : integer) is tagged record
6952 -- S : String (1 .. D);
6953 -- end record;
6954 -- procedure P (X : R);
6955 -- type T is new R (1) with private;
6956 -- private
6957 -- type BaseT is new R with null record;
6958 -- subtype T is BaseT (1);
6959 -- end;
6961 -- (strictly speaking the above is incorrect Ada)
6963 -- From the semantic standpoint the private view of private extension T
6964 -- should be flagged as constrained since one can clearly have
6966 -- Obj : T;
6968 -- in a unit withing Pack. However, when deriving subprograms for the
6969 -- private view of private extension T, T must be seen as unconstrained
6970 -- since T has discriminants (this is a constraint of the current
6971 -- subprogram derivation model). Thus, when processing the private view of
6972 -- a private extension such as T, we first mark T as unconstrained, we
6973 -- process it, we perform program derivation and just before returning from
6974 -- Build_Derived_Record_Type we mark T as constrained.
6976 -- ??? Are there are other uncomfortable cases that we will have to
6977 -- deal with.
6979 -- 10. RECORD_TYPE_WITH_PRIVATE complications
6981 -- Types that are derived from a visible record type and have a private
6982 -- extension present other peculiarities. They behave mostly like private
6983 -- types, but if they have primitive operations defined, these will not
6984 -- have the proper signatures for further inheritance, because other
6985 -- primitive operations will use the implicit base that we define for
6986 -- private derivations below. This affect subprogram inheritance (see
6987 -- Derive_Subprograms for details). We also derive the implicit base from
6988 -- the base type of the full view, so that the implicit base is a record
6989 -- type and not another private type, This avoids infinite loops.
6991 procedure Build_Derived_Record_Type
6992 (N : Node_Id;
6993 Parent_Type : Entity_Id;
6994 Derived_Type : Entity_Id;
6995 Derive_Subps : Boolean := True)
6997 Discriminant_Specs : constant Boolean :=
6998 Present (Discriminant_Specifications (N));
6999 Is_Tagged : constant Boolean := Is_Tagged_Type (Parent_Type);
7000 Loc : constant Source_Ptr := Sloc (N);
7001 Private_Extension : constant Boolean :=
7002 Nkind (N) = N_Private_Extension_Declaration;
7003 Assoc_List : Elist_Id;
7004 Constraint_Present : Boolean;
7005 Constrs : Elist_Id;
7006 Discrim : Entity_Id;
7007 Indic : Node_Id;
7008 Inherit_Discrims : Boolean := False;
7009 Last_Discrim : Entity_Id;
7010 New_Base : Entity_Id;
7011 New_Decl : Node_Id;
7012 New_Discrs : Elist_Id;
7013 New_Indic : Node_Id;
7014 Parent_Base : Entity_Id;
7015 Save_Etype : Entity_Id;
7016 Save_Discr_Constr : Elist_Id;
7017 Save_Next_Entity : Entity_Id;
7018 Type_Def : Node_Id;
7020 Discs : Elist_Id := New_Elmt_List;
7021 -- An empty Discs list means that there were no constraints in the
7022 -- subtype indication or that there was an error processing it.
7024 begin
7025 if Ekind (Parent_Type) = E_Record_Type_With_Private
7026 and then Present (Full_View (Parent_Type))
7027 and then Has_Discriminants (Parent_Type)
7028 then
7029 Parent_Base := Base_Type (Full_View (Parent_Type));
7030 else
7031 Parent_Base := Base_Type (Parent_Type);
7032 end if;
7034 -- AI05-0115 : if this is a derivation from a private type in some
7035 -- other scope that may lead to invisible components for the derived
7036 -- type, mark it accordingly.
7038 if Is_Private_Type (Parent_Type) then
7039 if Scope (Parent_Type) = Scope (Derived_Type) then
7040 null;
7042 elsif In_Open_Scopes (Scope (Parent_Type))
7043 and then In_Private_Part (Scope (Parent_Type))
7044 then
7045 null;
7047 else
7048 Set_Has_Private_Ancestor (Derived_Type);
7049 end if;
7051 else
7052 Set_Has_Private_Ancestor
7053 (Derived_Type, Has_Private_Ancestor (Parent_Type));
7054 end if;
7056 -- Before we start the previously documented transformations, here is
7057 -- little fix for size and alignment of tagged types. Normally when we
7058 -- derive type D from type P, we copy the size and alignment of P as the
7059 -- default for D, and in the absence of explicit representation clauses
7060 -- for D, the size and alignment are indeed the same as the parent.
7062 -- But this is wrong for tagged types, since fields may be added, and
7063 -- the default size may need to be larger, and the default alignment may
7064 -- need to be larger.
7066 -- We therefore reset the size and alignment fields in the tagged case.
7067 -- Note that the size and alignment will in any case be at least as
7068 -- large as the parent type (since the derived type has a copy of the
7069 -- parent type in the _parent field)
7071 -- The type is also marked as being tagged here, which is needed when
7072 -- processing components with a self-referential anonymous access type
7073 -- in the call to Check_Anonymous_Access_Components below. Note that
7074 -- this flag is also set later on for completeness.
7076 if Is_Tagged then
7077 Set_Is_Tagged_Type (Derived_Type);
7078 Init_Size_Align (Derived_Type);
7079 end if;
7081 -- STEP 0a: figure out what kind of derived type declaration we have
7083 if Private_Extension then
7084 Type_Def := N;
7085 Set_Ekind (Derived_Type, E_Record_Type_With_Private);
7087 else
7088 Type_Def := Type_Definition (N);
7090 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
7091 -- Parent_Base can be a private type or private extension. However,
7092 -- for tagged types with an extension the newly added fields are
7093 -- visible and hence the Derived_Type is always an E_Record_Type.
7094 -- (except that the parent may have its own private fields).
7095 -- For untagged types we preserve the Ekind of the Parent_Base.
7097 if Present (Record_Extension_Part (Type_Def)) then
7098 Set_Ekind (Derived_Type, E_Record_Type);
7100 -- Create internal access types for components with anonymous
7101 -- access types.
7103 if Ada_Version >= Ada_2005 then
7104 Check_Anonymous_Access_Components
7105 (N, Derived_Type, Derived_Type,
7106 Component_List (Record_Extension_Part (Type_Def)));
7107 end if;
7109 else
7110 Set_Ekind (Derived_Type, Ekind (Parent_Base));
7111 end if;
7112 end if;
7114 -- Indic can either be an N_Identifier if the subtype indication
7115 -- contains no constraint or an N_Subtype_Indication if the subtype
7116 -- indication has a constraint.
7118 Indic := Subtype_Indication (Type_Def);
7119 Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
7121 -- Check that the type has visible discriminants. The type may be
7122 -- a private type with unknown discriminants whose full view has
7123 -- discriminants which are invisible.
7125 if Constraint_Present then
7126 if not Has_Discriminants (Parent_Base)
7127 or else
7128 (Has_Unknown_Discriminants (Parent_Base)
7129 and then Is_Private_Type (Parent_Base))
7130 then
7131 Error_Msg_N
7132 ("invalid constraint: type has no discriminant",
7133 Constraint (Indic));
7135 Constraint_Present := False;
7136 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
7138 elsif Is_Constrained (Parent_Type) then
7139 Error_Msg_N
7140 ("invalid constraint: parent type is already constrained",
7141 Constraint (Indic));
7143 Constraint_Present := False;
7144 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
7145 end if;
7146 end if;
7148 -- STEP 0b: If needed, apply transformation given in point 5. above
7150 if not Private_Extension
7151 and then Has_Discriminants (Parent_Type)
7152 and then not Discriminant_Specs
7153 and then (Is_Constrained (Parent_Type) or else Constraint_Present)
7154 then
7155 -- First, we must analyze the constraint (see comment in point 5.)
7157 if Constraint_Present then
7158 New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
7160 if Has_Discriminants (Derived_Type)
7161 and then Has_Private_Declaration (Derived_Type)
7162 and then Present (Discriminant_Constraint (Derived_Type))
7163 then
7164 -- Verify that constraints of the full view statically match
7165 -- those given in the partial view.
7167 declare
7168 C1, C2 : Elmt_Id;
7170 begin
7171 C1 := First_Elmt (New_Discrs);
7172 C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
7173 while Present (C1) and then Present (C2) loop
7174 if Fully_Conformant_Expressions (Node (C1), Node (C2))
7175 or else
7176 (Is_OK_Static_Expression (Node (C1))
7177 and then
7178 Is_OK_Static_Expression (Node (C2))
7179 and then
7180 Expr_Value (Node (C1)) = Expr_Value (Node (C2)))
7181 then
7182 null;
7184 else
7185 Error_Msg_N (
7186 "constraint not conformant to previous declaration",
7187 Node (C1));
7188 end if;
7190 Next_Elmt (C1);
7191 Next_Elmt (C2);
7192 end loop;
7193 end;
7194 end if;
7195 end if;
7197 -- Insert and analyze the declaration for the unconstrained base type
7199 New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
7201 New_Decl :=
7202 Make_Full_Type_Declaration (Loc,
7203 Defining_Identifier => New_Base,
7204 Type_Definition =>
7205 Make_Derived_Type_Definition (Loc,
7206 Abstract_Present => Abstract_Present (Type_Def),
7207 Limited_Present => Limited_Present (Type_Def),
7208 Subtype_Indication =>
7209 New_Occurrence_Of (Parent_Base, Loc),
7210 Record_Extension_Part =>
7211 Relocate_Node (Record_Extension_Part (Type_Def)),
7212 Interface_List => Interface_List (Type_Def)));
7214 Set_Parent (New_Decl, Parent (N));
7215 Mark_Rewrite_Insertion (New_Decl);
7216 Insert_Before (N, New_Decl);
7218 -- In the extension case, make sure ancestor is frozen appropriately
7219 -- (see also non-discriminated case below).
7221 if Present (Record_Extension_Part (Type_Def))
7222 or else Is_Interface (Parent_Base)
7223 then
7224 Freeze_Before (New_Decl, Parent_Type);
7225 end if;
7227 -- Note that this call passes False for the Derive_Subps parameter
7228 -- because subprogram derivation is deferred until after creating
7229 -- the subtype (see below).
7231 Build_Derived_Type
7232 (New_Decl, Parent_Base, New_Base,
7233 Is_Completion => True, Derive_Subps => False);
7235 -- ??? This needs re-examination to determine whether the
7236 -- above call can simply be replaced by a call to Analyze.
7238 Set_Analyzed (New_Decl);
7240 -- Insert and analyze the declaration for the constrained subtype
7242 if Constraint_Present then
7243 New_Indic :=
7244 Make_Subtype_Indication (Loc,
7245 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
7246 Constraint => Relocate_Node (Constraint (Indic)));
7248 else
7249 declare
7250 Constr_List : constant List_Id := New_List;
7251 C : Elmt_Id;
7252 Expr : Node_Id;
7254 begin
7255 C := First_Elmt (Discriminant_Constraint (Parent_Type));
7256 while Present (C) loop
7257 Expr := Node (C);
7259 -- It is safe here to call New_Copy_Tree since
7260 -- Force_Evaluation was called on each constraint in
7261 -- Build_Discriminant_Constraints.
7263 Append (New_Copy_Tree (Expr), To => Constr_List);
7265 Next_Elmt (C);
7266 end loop;
7268 New_Indic :=
7269 Make_Subtype_Indication (Loc,
7270 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
7271 Constraint =>
7272 Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
7273 end;
7274 end if;
7276 Rewrite (N,
7277 Make_Subtype_Declaration (Loc,
7278 Defining_Identifier => Derived_Type,
7279 Subtype_Indication => New_Indic));
7281 Analyze (N);
7283 -- Derivation of subprograms must be delayed until the full subtype
7284 -- has been established, to ensure proper overriding of subprograms
7285 -- inherited by full types. If the derivations occurred as part of
7286 -- the call to Build_Derived_Type above, then the check for type
7287 -- conformance would fail because earlier primitive subprograms
7288 -- could still refer to the full type prior the change to the new
7289 -- subtype and hence would not match the new base type created here.
7290 -- Subprograms are not derived, however, when Derive_Subps is False
7291 -- (since otherwise there could be redundant derivations).
7293 if Derive_Subps then
7294 Derive_Subprograms (Parent_Type, Derived_Type);
7295 end if;
7297 -- For tagged types the Discriminant_Constraint of the new base itype
7298 -- is inherited from the first subtype so that no subtype conformance
7299 -- problem arise when the first subtype overrides primitive
7300 -- operations inherited by the implicit base type.
7302 if Is_Tagged then
7303 Set_Discriminant_Constraint
7304 (New_Base, Discriminant_Constraint (Derived_Type));
7305 end if;
7307 return;
7308 end if;
7310 -- If we get here Derived_Type will have no discriminants or it will be
7311 -- a discriminated unconstrained base type.
7313 -- STEP 1a: perform preliminary actions/checks for derived tagged types
7315 if Is_Tagged then
7317 -- The parent type is frozen for non-private extensions (RM 13.14(7))
7318 -- The declaration of a specific descendant of an interface type
7319 -- freezes the interface type (RM 13.14).
7321 if not Private_Extension or else Is_Interface (Parent_Base) then
7322 Freeze_Before (N, Parent_Type);
7323 end if;
7325 -- In Ada 2005 (AI-344), the restriction that a derived tagged type
7326 -- cannot be declared at a deeper level than its parent type is
7327 -- removed. The check on derivation within a generic body is also
7328 -- relaxed, but there's a restriction that a derived tagged type
7329 -- cannot be declared in a generic body if it's derived directly
7330 -- or indirectly from a formal type of that generic.
7332 if Ada_Version >= Ada_2005 then
7333 if Present (Enclosing_Generic_Body (Derived_Type)) then
7334 declare
7335 Ancestor_Type : Entity_Id;
7337 begin
7338 -- Check to see if any ancestor of the derived type is a
7339 -- formal type.
7341 Ancestor_Type := Parent_Type;
7342 while not Is_Generic_Type (Ancestor_Type)
7343 and then Etype (Ancestor_Type) /= Ancestor_Type
7344 loop
7345 Ancestor_Type := Etype (Ancestor_Type);
7346 end loop;
7348 -- If the derived type does have a formal type as an
7349 -- ancestor, then it's an error if the derived type is
7350 -- declared within the body of the generic unit that
7351 -- declares the formal type in its generic formal part. It's
7352 -- sufficient to check whether the ancestor type is declared
7353 -- inside the same generic body as the derived type (such as
7354 -- within a nested generic spec), in which case the
7355 -- derivation is legal. If the formal type is declared
7356 -- outside of that generic body, then it's guaranteed that
7357 -- the derived type is declared within the generic body of
7358 -- the generic unit declaring the formal type.
7360 if Is_Generic_Type (Ancestor_Type)
7361 and then Enclosing_Generic_Body (Ancestor_Type) /=
7362 Enclosing_Generic_Body (Derived_Type)
7363 then
7364 Error_Msg_NE
7365 ("parent type of& must not be descendant of formal type"
7366 & " of an enclosing generic body",
7367 Indic, Derived_Type);
7368 end if;
7369 end;
7370 end if;
7372 elsif Type_Access_Level (Derived_Type) /=
7373 Type_Access_Level (Parent_Type)
7374 and then not Is_Generic_Type (Derived_Type)
7375 then
7376 if Is_Controlled (Parent_Type) then
7377 Error_Msg_N
7378 ("controlled type must be declared at the library level",
7379 Indic);
7380 else
7381 Error_Msg_N
7382 ("type extension at deeper accessibility level than parent",
7383 Indic);
7384 end if;
7386 else
7387 declare
7388 GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
7390 begin
7391 if Present (GB)
7392 and then GB /= Enclosing_Generic_Body (Parent_Base)
7393 then
7394 Error_Msg_NE
7395 ("parent type of& must not be outside generic body"
7396 & " (RM 3.9.1(4))",
7397 Indic, Derived_Type);
7398 end if;
7399 end;
7400 end if;
7401 end if;
7403 -- Ada 2005 (AI-251)
7405 if Ada_Version >= Ada_2005 and then Is_Tagged then
7407 -- "The declaration of a specific descendant of an interface type
7408 -- freezes the interface type" (RM 13.14).
7410 declare
7411 Iface : Node_Id;
7412 begin
7413 if Is_Non_Empty_List (Interface_List (Type_Def)) then
7414 Iface := First (Interface_List (Type_Def));
7415 while Present (Iface) loop
7416 Freeze_Before (N, Etype (Iface));
7417 Next (Iface);
7418 end loop;
7419 end if;
7420 end;
7421 end if;
7423 -- STEP 1b : preliminary cleanup of the full view of private types
7425 -- If the type is already marked as having discriminants, then it's the
7426 -- completion of a private type or private extension and we need to
7427 -- retain the discriminants from the partial view if the current
7428 -- declaration has Discriminant_Specifications so that we can verify
7429 -- conformance. However, we must remove any existing components that
7430 -- were inherited from the parent (and attached in Copy_And_Swap)
7431 -- because the full type inherits all appropriate components anyway, and
7432 -- we do not want the partial view's components interfering.
7434 if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
7435 Discrim := First_Discriminant (Derived_Type);
7436 loop
7437 Last_Discrim := Discrim;
7438 Next_Discriminant (Discrim);
7439 exit when No (Discrim);
7440 end loop;
7442 Set_Last_Entity (Derived_Type, Last_Discrim);
7444 -- In all other cases wipe out the list of inherited components (even
7445 -- inherited discriminants), it will be properly rebuilt here.
7447 else
7448 Set_First_Entity (Derived_Type, Empty);
7449 Set_Last_Entity (Derived_Type, Empty);
7450 end if;
7452 -- STEP 1c: Initialize some flags for the Derived_Type
7454 -- The following flags must be initialized here so that
7455 -- Process_Discriminants can check that discriminants of tagged types do
7456 -- not have a default initial value and that access discriminants are
7457 -- only specified for limited records. For completeness, these flags are
7458 -- also initialized along with all the other flags below.
7460 -- AI-419: Limitedness is not inherited from an interface parent, so to
7461 -- be limited in that case the type must be explicitly declared as
7462 -- limited. However, task and protected interfaces are always limited.
7464 if Limited_Present (Type_Def) then
7465 Set_Is_Limited_Record (Derived_Type);
7467 elsif Is_Limited_Record (Parent_Type)
7468 or else (Present (Full_View (Parent_Type))
7469 and then Is_Limited_Record (Full_View (Parent_Type)))
7470 then
7471 if not Is_Interface (Parent_Type)
7472 or else Is_Synchronized_Interface (Parent_Type)
7473 or else Is_Protected_Interface (Parent_Type)
7474 or else Is_Task_Interface (Parent_Type)
7475 then
7476 Set_Is_Limited_Record (Derived_Type);
7477 end if;
7478 end if;
7480 -- STEP 2a: process discriminants of derived type if any
7482 Push_Scope (Derived_Type);
7484 if Discriminant_Specs then
7485 Set_Has_Unknown_Discriminants (Derived_Type, False);
7487 -- The following call initializes fields Has_Discriminants and
7488 -- Discriminant_Constraint, unless we are processing the completion
7489 -- of a private type declaration.
7491 Check_Or_Process_Discriminants (N, Derived_Type);
7493 -- For untagged types, the constraint on the Parent_Type must be
7494 -- present and is used to rename the discriminants.
7496 if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
7497 Error_Msg_N ("untagged parent must have discriminants", Indic);
7499 elsif not Is_Tagged and then not Constraint_Present then
7500 Error_Msg_N
7501 ("discriminant constraint needed for derived untagged records",
7502 Indic);
7504 -- Otherwise the parent subtype must be constrained unless we have a
7505 -- private extension.
7507 elsif not Constraint_Present
7508 and then not Private_Extension
7509 and then not Is_Constrained (Parent_Type)
7510 then
7511 Error_Msg_N
7512 ("unconstrained type not allowed in this context", Indic);
7514 elsif Constraint_Present then
7515 -- The following call sets the field Corresponding_Discriminant
7516 -- for the discriminants in the Derived_Type.
7518 Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
7520 -- For untagged types all new discriminants must rename
7521 -- discriminants in the parent. For private extensions new
7522 -- discriminants cannot rename old ones (implied by [7.3(13)]).
7524 Discrim := First_Discriminant (Derived_Type);
7525 while Present (Discrim) loop
7526 if not Is_Tagged
7527 and then No (Corresponding_Discriminant (Discrim))
7528 then
7529 Error_Msg_N
7530 ("new discriminants must constrain old ones", Discrim);
7532 elsif Private_Extension
7533 and then Present (Corresponding_Discriminant (Discrim))
7534 then
7535 Error_Msg_N
7536 ("only static constraints allowed for parent"
7537 & " discriminants in the partial view", Indic);
7538 exit;
7539 end if;
7541 -- If a new discriminant is used in the constraint, then its
7542 -- subtype must be statically compatible with the parent
7543 -- discriminant's subtype (3.7(15)).
7545 -- However, if the record contains an array constrained by
7546 -- the discriminant but with some different bound, the compiler
7547 -- attemps to create a smaller range for the discriminant type.
7548 -- (See exp_ch3.Adjust_Discriminants). In this case, where
7549 -- the discriminant type is a scalar type, the check must use
7550 -- the original discriminant type in the parent declaration.
7552 declare
7553 Corr_Disc : constant Entity_Id :=
7554 Corresponding_Discriminant (Discrim);
7555 Disc_Type : constant Entity_Id := Etype (Discrim);
7556 Corr_Type : Entity_Id;
7558 begin
7559 if Present (Corr_Disc) then
7560 if Is_Scalar_Type (Disc_Type) then
7561 Corr_Type :=
7562 Entity (Discriminant_Type (Parent (Corr_Disc)));
7563 else
7564 Corr_Type := Etype (Corr_Disc);
7565 end if;
7567 if not
7568 Subtypes_Statically_Compatible (Disc_Type, Corr_Type)
7569 then
7570 Error_Msg_N
7571 ("subtype must be compatible "
7572 & "with parent discriminant",
7573 Discrim);
7574 end if;
7575 end if;
7576 end;
7578 Next_Discriminant (Discrim);
7579 end loop;
7581 -- Check whether the constraints of the full view statically
7582 -- match those imposed by the parent subtype [7.3(13)].
7584 if Present (Stored_Constraint (Derived_Type)) then
7585 declare
7586 C1, C2 : Elmt_Id;
7588 begin
7589 C1 := First_Elmt (Discs);
7590 C2 := First_Elmt (Stored_Constraint (Derived_Type));
7591 while Present (C1) and then Present (C2) loop
7592 if not
7593 Fully_Conformant_Expressions (Node (C1), Node (C2))
7594 then
7595 Error_Msg_N
7596 ("not conformant with previous declaration",
7597 Node (C1));
7598 end if;
7600 Next_Elmt (C1);
7601 Next_Elmt (C2);
7602 end loop;
7603 end;
7604 end if;
7605 end if;
7607 -- STEP 2b: No new discriminants, inherit discriminants if any
7609 else
7610 if Private_Extension then
7611 Set_Has_Unknown_Discriminants
7612 (Derived_Type,
7613 Has_Unknown_Discriminants (Parent_Type)
7614 or else Unknown_Discriminants_Present (N));
7616 -- The partial view of the parent may have unknown discriminants,
7617 -- but if the full view has discriminants and the parent type is
7618 -- in scope they must be inherited.
7620 elsif Has_Unknown_Discriminants (Parent_Type)
7621 and then
7622 (not Has_Discriminants (Parent_Type)
7623 or else not In_Open_Scopes (Scope (Parent_Type)))
7624 then
7625 Set_Has_Unknown_Discriminants (Derived_Type);
7626 end if;
7628 if not Has_Unknown_Discriminants (Derived_Type)
7629 and then not Has_Unknown_Discriminants (Parent_Base)
7630 and then Has_Discriminants (Parent_Type)
7631 then
7632 Inherit_Discrims := True;
7633 Set_Has_Discriminants
7634 (Derived_Type, True);
7635 Set_Discriminant_Constraint
7636 (Derived_Type, Discriminant_Constraint (Parent_Base));
7637 end if;
7639 -- The following test is true for private types (remember
7640 -- transformation 5. is not applied to those) and in an error
7641 -- situation.
7643 if Constraint_Present then
7644 Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
7645 end if;
7647 -- For now mark a new derived type as constrained only if it has no
7648 -- discriminants. At the end of Build_Derived_Record_Type we properly
7649 -- set this flag in the case of private extensions. See comments in
7650 -- point 9. just before body of Build_Derived_Record_Type.
7652 Set_Is_Constrained
7653 (Derived_Type,
7654 not (Inherit_Discrims
7655 or else Has_Unknown_Discriminants (Derived_Type)));
7656 end if;
7658 -- STEP 3: initialize fields of derived type
7660 Set_Is_Tagged_Type (Derived_Type, Is_Tagged);
7661 Set_Stored_Constraint (Derived_Type, No_Elist);
7663 -- Ada 2005 (AI-251): Private type-declarations can implement interfaces
7664 -- but cannot be interfaces
7666 if not Private_Extension
7667 and then Ekind (Derived_Type) /= E_Private_Type
7668 and then Ekind (Derived_Type) /= E_Limited_Private_Type
7669 then
7670 if Interface_Present (Type_Def) then
7671 Analyze_Interface_Declaration (Derived_Type, Type_Def);
7672 end if;
7674 Set_Interfaces (Derived_Type, No_Elist);
7675 end if;
7677 -- Fields inherited from the Parent_Type
7679 Set_Has_Specified_Layout
7680 (Derived_Type, Has_Specified_Layout (Parent_Type));
7681 Set_Is_Limited_Composite
7682 (Derived_Type, Is_Limited_Composite (Parent_Type));
7683 Set_Is_Private_Composite
7684 (Derived_Type, Is_Private_Composite (Parent_Type));
7686 -- Fields inherited from the Parent_Base
7688 Set_Has_Controlled_Component
7689 (Derived_Type, Has_Controlled_Component (Parent_Base));
7690 Set_Has_Non_Standard_Rep
7691 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
7692 Set_Has_Primitive_Operations
7693 (Derived_Type, Has_Primitive_Operations (Parent_Base));
7695 -- Fields inherited from the Parent_Base in the non-private case
7697 if Ekind (Derived_Type) = E_Record_Type then
7698 Set_Has_Complex_Representation
7699 (Derived_Type, Has_Complex_Representation (Parent_Base));
7700 end if;
7702 -- Fields inherited from the Parent_Base for record types
7704 if Is_Record_Type (Derived_Type) then
7706 declare
7707 Parent_Full : Entity_Id;
7709 begin
7710 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
7711 -- Parent_Base can be a private type or private extension. Go
7712 -- to the full view here to get the E_Record_Type specific flags.
7714 if Present (Full_View (Parent_Base)) then
7715 Parent_Full := Full_View (Parent_Base);
7716 else
7717 Parent_Full := Parent_Base;
7718 end if;
7720 Set_OK_To_Reorder_Components
7721 (Derived_Type, OK_To_Reorder_Components (Parent_Full));
7722 end;
7723 end if;
7725 -- Set fields for private derived types
7727 if Is_Private_Type (Derived_Type) then
7728 Set_Depends_On_Private (Derived_Type, True);
7729 Set_Private_Dependents (Derived_Type, New_Elmt_List);
7731 -- Inherit fields from non private record types. If this is the
7732 -- completion of a derivation from a private type, the parent itself
7733 -- is private, and the attributes come from its full view, which must
7734 -- be present.
7736 else
7737 if Is_Private_Type (Parent_Base)
7738 and then not Is_Record_Type (Parent_Base)
7739 then
7740 Set_Component_Alignment
7741 (Derived_Type, Component_Alignment (Full_View (Parent_Base)));
7742 Set_C_Pass_By_Copy
7743 (Derived_Type, C_Pass_By_Copy (Full_View (Parent_Base)));
7744 else
7745 Set_Component_Alignment
7746 (Derived_Type, Component_Alignment (Parent_Base));
7747 Set_C_Pass_By_Copy
7748 (Derived_Type, C_Pass_By_Copy (Parent_Base));
7749 end if;
7750 end if;
7752 -- Set fields for tagged types
7754 if Is_Tagged then
7755 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
7757 -- All tagged types defined in Ada.Finalization are controlled
7759 if Chars (Scope (Derived_Type)) = Name_Finalization
7760 and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
7761 and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
7762 then
7763 Set_Is_Controlled (Derived_Type);
7764 else
7765 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Base));
7766 end if;
7768 -- Minor optimization: there is no need to generate the class-wide
7769 -- entity associated with an underlying record view.
7771 if not Is_Underlying_Record_View (Derived_Type) then
7772 Make_Class_Wide_Type (Derived_Type);
7773 end if;
7775 Set_Is_Abstract_Type (Derived_Type, Abstract_Present (Type_Def));
7777 if Has_Discriminants (Derived_Type)
7778 and then Constraint_Present
7779 then
7780 Set_Stored_Constraint
7781 (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
7782 end if;
7784 if Ada_Version >= Ada_2005 then
7785 declare
7786 Ifaces_List : Elist_Id;
7788 begin
7789 -- Checks rules 3.9.4 (13/2 and 14/2)
7791 if Comes_From_Source (Derived_Type)
7792 and then not Is_Private_Type (Derived_Type)
7793 and then Is_Interface (Parent_Type)
7794 and then not Is_Interface (Derived_Type)
7795 then
7796 if Is_Task_Interface (Parent_Type) then
7797 Error_Msg_N
7798 ("(Ada 2005) task type required (RM 3.9.4 (13.2))",
7799 Derived_Type);
7801 elsif Is_Protected_Interface (Parent_Type) then
7802 Error_Msg_N
7803 ("(Ada 2005) protected type required (RM 3.9.4 (14.2))",
7804 Derived_Type);
7805 end if;
7806 end if;
7808 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
7810 Check_Interfaces (N, Type_Def);
7812 -- Ada 2005 (AI-251): Collect the list of progenitors that are
7813 -- not already in the parents.
7815 Collect_Interfaces
7816 (T => Derived_Type,
7817 Ifaces_List => Ifaces_List,
7818 Exclude_Parents => True);
7820 Set_Interfaces (Derived_Type, Ifaces_List);
7822 -- If the derived type is the anonymous type created for
7823 -- a declaration whose parent has a constraint, propagate
7824 -- the interface list to the source type. This must be done
7825 -- prior to the completion of the analysis of the source type
7826 -- because the components in the extension may contain current
7827 -- instances whose legality depends on some ancestor.
7829 if Is_Itype (Derived_Type) then
7830 declare
7831 Def : constant Node_Id :=
7832 Associated_Node_For_Itype (Derived_Type);
7833 begin
7834 if Present (Def)
7835 and then Nkind (Def) = N_Full_Type_Declaration
7836 then
7837 Set_Interfaces
7838 (Defining_Identifier (Def), Ifaces_List);
7839 end if;
7840 end;
7841 end if;
7842 end;
7843 end if;
7845 else
7846 Set_Is_Packed (Derived_Type, Is_Packed (Parent_Base));
7847 Set_Has_Non_Standard_Rep
7848 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
7849 end if;
7851 -- STEP 4: Inherit components from the parent base and constrain them.
7852 -- Apply the second transformation described in point 6. above.
7854 if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
7855 or else not Has_Discriminants (Parent_Type)
7856 or else not Is_Constrained (Parent_Type)
7857 then
7858 Constrs := Discs;
7859 else
7860 Constrs := Discriminant_Constraint (Parent_Type);
7861 end if;
7863 Assoc_List :=
7864 Inherit_Components
7865 (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
7867 -- STEP 5a: Copy the parent record declaration for untagged types
7869 if not Is_Tagged then
7871 -- Discriminant_Constraint (Derived_Type) has been properly
7872 -- constructed. Save it and temporarily set it to Empty because we
7873 -- do not want the call to New_Copy_Tree below to mess this list.
7875 if Has_Discriminants (Derived_Type) then
7876 Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
7877 Set_Discriminant_Constraint (Derived_Type, No_Elist);
7878 else
7879 Save_Discr_Constr := No_Elist;
7880 end if;
7882 -- Save the Etype field of Derived_Type. It is correctly set now,
7883 -- but the call to New_Copy tree may remap it to point to itself,
7884 -- which is not what we want. Ditto for the Next_Entity field.
7886 Save_Etype := Etype (Derived_Type);
7887 Save_Next_Entity := Next_Entity (Derived_Type);
7889 -- Assoc_List maps all stored discriminants in the Parent_Base to
7890 -- stored discriminants in the Derived_Type. It is fundamental that
7891 -- no types or itypes with discriminants other than the stored
7892 -- discriminants appear in the entities declared inside
7893 -- Derived_Type, since the back end cannot deal with it.
7895 New_Decl :=
7896 New_Copy_Tree
7897 (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
7899 -- Restore the fields saved prior to the New_Copy_Tree call
7900 -- and compute the stored constraint.
7902 Set_Etype (Derived_Type, Save_Etype);
7903 Set_Next_Entity (Derived_Type, Save_Next_Entity);
7905 if Has_Discriminants (Derived_Type) then
7906 Set_Discriminant_Constraint
7907 (Derived_Type, Save_Discr_Constr);
7908 Set_Stored_Constraint
7909 (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
7910 Replace_Components (Derived_Type, New_Decl);
7911 Set_Has_Implicit_Dereference
7912 (Derived_Type, Has_Implicit_Dereference (Parent_Type));
7913 end if;
7915 -- Insert the new derived type declaration
7917 Rewrite (N, New_Decl);
7919 -- STEP 5b: Complete the processing for record extensions in generics
7921 -- There is no completion for record extensions declared in the
7922 -- parameter part of a generic, so we need to complete processing for
7923 -- these generic record extensions here. The Record_Type_Definition call
7924 -- will change the Ekind of the components from E_Void to E_Component.
7926 elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
7927 Record_Type_Definition (Empty, Derived_Type);
7929 -- STEP 5c: Process the record extension for non private tagged types
7931 elsif not Private_Extension then
7933 -- Add the _parent field in the derived type
7935 Expand_Record_Extension (Derived_Type, Type_Def);
7937 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
7938 -- implemented interfaces if we are in expansion mode
7940 if Expander_Active
7941 and then Has_Interfaces (Derived_Type)
7942 then
7943 Add_Interface_Tag_Components (N, Derived_Type);
7944 end if;
7946 -- Analyze the record extension
7948 Record_Type_Definition
7949 (Record_Extension_Part (Type_Def), Derived_Type);
7950 end if;
7952 End_Scope;
7954 -- Nothing else to do if there is an error in the derivation.
7955 -- An unusual case: the full view may be derived from a type in an
7956 -- instance, when the partial view was used illegally as an actual
7957 -- in that instance, leading to a circular definition.
7959 if Etype (Derived_Type) = Any_Type
7960 or else Etype (Parent_Type) = Derived_Type
7961 then
7962 return;
7963 end if;
7965 -- Set delayed freeze and then derive subprograms, we need to do
7966 -- this in this order so that derived subprograms inherit the
7967 -- derived freeze if necessary.
7969 Set_Has_Delayed_Freeze (Derived_Type);
7971 if Derive_Subps then
7972 Derive_Subprograms (Parent_Type, Derived_Type);
7973 end if;
7975 -- If we have a private extension which defines a constrained derived
7976 -- type mark as constrained here after we have derived subprograms. See
7977 -- comment on point 9. just above the body of Build_Derived_Record_Type.
7979 if Private_Extension and then Inherit_Discrims then
7980 if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
7981 Set_Is_Constrained (Derived_Type, True);
7982 Set_Discriminant_Constraint (Derived_Type, Discs);
7984 elsif Is_Constrained (Parent_Type) then
7985 Set_Is_Constrained
7986 (Derived_Type, True);
7987 Set_Discriminant_Constraint
7988 (Derived_Type, Discriminant_Constraint (Parent_Type));
7989 end if;
7990 end if;
7992 -- Update the class-wide type, which shares the now-completed entity
7993 -- list with its specific type. In case of underlying record views,
7994 -- we do not generate the corresponding class wide entity.
7996 if Is_Tagged
7997 and then not Is_Underlying_Record_View (Derived_Type)
7998 then
7999 Set_First_Entity
8000 (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
8001 Set_Last_Entity
8002 (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
8003 end if;
8004 end Build_Derived_Record_Type;
8006 ------------------------
8007 -- Build_Derived_Type --
8008 ------------------------
8010 procedure Build_Derived_Type
8011 (N : Node_Id;
8012 Parent_Type : Entity_Id;
8013 Derived_Type : Entity_Id;
8014 Is_Completion : Boolean;
8015 Derive_Subps : Boolean := True)
8017 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
8019 begin
8020 -- Set common attributes
8022 Set_Scope (Derived_Type, Current_Scope);
8024 Set_Ekind (Derived_Type, Ekind (Parent_Base));
8025 Set_Etype (Derived_Type, Parent_Base);
8026 Set_Has_Task (Derived_Type, Has_Task (Parent_Base));
8028 Set_Size_Info (Derived_Type, Parent_Type);
8029 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
8030 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
8031 Set_Is_Tagged_Type (Derived_Type, Is_Tagged_Type (Parent_Type));
8033 -- If the parent type is a private subtype, the convention on the base
8034 -- type may be set in the private part, and not propagated to the
8035 -- subtype until later, so we obtain the convention from the base type.
8037 Set_Convention (Derived_Type, Convention (Parent_Base));
8039 -- Propagate invariant information. The new type has invariants if
8040 -- they are inherited from the parent type, and these invariants can
8041 -- be further inherited, so both flags are set.
8043 -- We similarly inherit predicates
8045 if Has_Predicates (Parent_Type) then
8046 Set_Has_Predicates (Derived_Type);
8047 end if;
8049 -- The derived type inherits the representation clauses of the parent.
8050 -- However, for a private type that is completed by a derivation, there
8051 -- may be operation attributes that have been specified already (stream
8052 -- attributes and External_Tag) and those must be provided. Finally,
8053 -- if the partial view is a private extension, the representation items
8054 -- of the parent have been inherited already, and should not be chained
8055 -- twice to the derived type.
8057 if Is_Tagged_Type (Parent_Type)
8058 and then Present (First_Rep_Item (Derived_Type))
8059 then
8060 -- The existing items are either operational items or items inherited
8061 -- from a private extension declaration.
8063 declare
8064 Rep : Node_Id;
8065 -- Used to iterate over representation items of the derived type
8067 Last_Rep : Node_Id;
8068 -- Last representation item of the (non-empty) representation
8069 -- item list of the derived type.
8071 Found : Boolean := False;
8073 begin
8074 Rep := First_Rep_Item (Derived_Type);
8075 Last_Rep := Rep;
8076 while Present (Rep) loop
8077 if Rep = First_Rep_Item (Parent_Type) then
8078 Found := True;
8079 exit;
8081 else
8082 Rep := Next_Rep_Item (Rep);
8084 if Present (Rep) then
8085 Last_Rep := Rep;
8086 end if;
8087 end if;
8088 end loop;
8090 -- Here if we either encountered the parent type's first rep
8091 -- item on the derived type's rep item list (in which case
8092 -- Found is True, and we have nothing else to do), or if we
8093 -- reached the last rep item of the derived type, which is
8094 -- Last_Rep, in which case we further chain the parent type's
8095 -- rep items to those of the derived type.
8097 if not Found then
8098 Set_Next_Rep_Item (Last_Rep, First_Rep_Item (Parent_Type));
8099 end if;
8100 end;
8102 else
8103 Set_First_Rep_Item (Derived_Type, First_Rep_Item (Parent_Type));
8104 end if;
8106 case Ekind (Parent_Type) is
8107 when Numeric_Kind =>
8108 Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
8110 when Array_Kind =>
8111 Build_Derived_Array_Type (N, Parent_Type, Derived_Type);
8113 when E_Record_Type
8114 | E_Record_Subtype
8115 | Class_Wide_Kind =>
8116 Build_Derived_Record_Type
8117 (N, Parent_Type, Derived_Type, Derive_Subps);
8118 return;
8120 when Enumeration_Kind =>
8121 Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
8123 when Access_Kind =>
8124 Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
8126 when Incomplete_Or_Private_Kind =>
8127 Build_Derived_Private_Type
8128 (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
8130 -- For discriminated types, the derivation includes deriving
8131 -- primitive operations. For others it is done below.
8133 if Is_Tagged_Type (Parent_Type)
8134 or else Has_Discriminants (Parent_Type)
8135 or else (Present (Full_View (Parent_Type))
8136 and then Has_Discriminants (Full_View (Parent_Type)))
8137 then
8138 return;
8139 end if;
8141 when Concurrent_Kind =>
8142 Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
8144 when others =>
8145 raise Program_Error;
8146 end case;
8148 if Etype (Derived_Type) = Any_Type then
8149 return;
8150 end if;
8152 -- Set delayed freeze and then derive subprograms, we need to do this
8153 -- in this order so that derived subprograms inherit the derived freeze
8154 -- if necessary.
8156 Set_Has_Delayed_Freeze (Derived_Type);
8157 if Derive_Subps then
8158 Derive_Subprograms (Parent_Type, Derived_Type);
8159 end if;
8161 Set_Has_Primitive_Operations
8162 (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
8163 end Build_Derived_Type;
8165 -----------------------
8166 -- Build_Discriminal --
8167 -----------------------
8169 procedure Build_Discriminal (Discrim : Entity_Id) is
8170 D_Minal : Entity_Id;
8171 CR_Disc : Entity_Id;
8173 begin
8174 -- A discriminal has the same name as the discriminant
8176 D_Minal := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
8178 Set_Ekind (D_Minal, E_In_Parameter);
8179 Set_Mechanism (D_Minal, Default_Mechanism);
8180 Set_Etype (D_Minal, Etype (Discrim));
8181 Set_Scope (D_Minal, Current_Scope);
8183 Set_Discriminal (Discrim, D_Minal);
8184 Set_Discriminal_Link (D_Minal, Discrim);
8186 -- For task types, build at once the discriminants of the corresponding
8187 -- record, which are needed if discriminants are used in entry defaults
8188 -- and in family bounds.
8190 if Is_Concurrent_Type (Current_Scope)
8191 or else Is_Limited_Type (Current_Scope)
8192 then
8193 CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
8195 Set_Ekind (CR_Disc, E_In_Parameter);
8196 Set_Mechanism (CR_Disc, Default_Mechanism);
8197 Set_Etype (CR_Disc, Etype (Discrim));
8198 Set_Scope (CR_Disc, Current_Scope);
8199 Set_Discriminal_Link (CR_Disc, Discrim);
8200 Set_CR_Discriminant (Discrim, CR_Disc);
8201 end if;
8202 end Build_Discriminal;
8204 ------------------------------------
8205 -- Build_Discriminant_Constraints --
8206 ------------------------------------
8208 function Build_Discriminant_Constraints
8209 (T : Entity_Id;
8210 Def : Node_Id;
8211 Derived_Def : Boolean := False) return Elist_Id
8213 C : constant Node_Id := Constraint (Def);
8214 Nb_Discr : constant Nat := Number_Discriminants (T);
8216 Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
8217 -- Saves the expression corresponding to a given discriminant in T
8219 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
8220 -- Return the Position number within array Discr_Expr of a discriminant
8221 -- D within the discriminant list of the discriminated type T.
8223 ------------------
8224 -- Pos_Of_Discr --
8225 ------------------
8227 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
8228 Disc : Entity_Id;
8230 begin
8231 Disc := First_Discriminant (T);
8232 for J in Discr_Expr'Range loop
8233 if Disc = D then
8234 return J;
8235 end if;
8237 Next_Discriminant (Disc);
8238 end loop;
8240 -- Note: Since this function is called on discriminants that are
8241 -- known to belong to the discriminated type, falling through the
8242 -- loop with no match signals an internal compiler error.
8244 raise Program_Error;
8245 end Pos_Of_Discr;
8247 -- Declarations local to Build_Discriminant_Constraints
8249 Discr : Entity_Id;
8250 E : Entity_Id;
8251 Elist : constant Elist_Id := New_Elmt_List;
8253 Constr : Node_Id;
8254 Expr : Node_Id;
8255 Id : Node_Id;
8256 Position : Nat;
8257 Found : Boolean;
8259 Discrim_Present : Boolean := False;
8261 -- Start of processing for Build_Discriminant_Constraints
8263 begin
8264 -- The following loop will process positional associations only.
8265 -- For a positional association, the (single) discriminant is
8266 -- implicitly specified by position, in textual order (RM 3.7.2).
8268 Discr := First_Discriminant (T);
8269 Constr := First (Constraints (C));
8270 for D in Discr_Expr'Range loop
8271 exit when Nkind (Constr) = N_Discriminant_Association;
8273 if No (Constr) then
8274 Error_Msg_N ("too few discriminants given in constraint", C);
8275 return New_Elmt_List;
8277 elsif Nkind (Constr) = N_Range
8278 or else (Nkind (Constr) = N_Attribute_Reference
8279 and then
8280 Attribute_Name (Constr) = Name_Range)
8281 then
8282 Error_Msg_N
8283 ("a range is not a valid discriminant constraint", Constr);
8284 Discr_Expr (D) := Error;
8286 else
8287 Analyze_And_Resolve (Constr, Base_Type (Etype (Discr)));
8288 Discr_Expr (D) := Constr;
8289 end if;
8291 Next_Discriminant (Discr);
8292 Next (Constr);
8293 end loop;
8295 if No (Discr) and then Present (Constr) then
8296 Error_Msg_N ("too many discriminants given in constraint", Constr);
8297 return New_Elmt_List;
8298 end if;
8300 -- Named associations can be given in any order, but if both positional
8301 -- and named associations are used in the same discriminant constraint,
8302 -- then positional associations must occur first, at their normal
8303 -- position. Hence once a named association is used, the rest of the
8304 -- discriminant constraint must use only named associations.
8306 while Present (Constr) loop
8308 -- Positional association forbidden after a named association
8310 if Nkind (Constr) /= N_Discriminant_Association then
8311 Error_Msg_N ("positional association follows named one", Constr);
8312 return New_Elmt_List;
8314 -- Otherwise it is a named association
8316 else
8317 -- E records the type of the discriminants in the named
8318 -- association. All the discriminants specified in the same name
8319 -- association must have the same type.
8321 E := Empty;
8323 -- Search the list of discriminants in T to see if the simple name
8324 -- given in the constraint matches any of them.
8326 Id := First (Selector_Names (Constr));
8327 while Present (Id) loop
8328 Found := False;
8330 -- If Original_Discriminant is present, we are processing a
8331 -- generic instantiation and this is an instance node. We need
8332 -- to find the name of the corresponding discriminant in the
8333 -- actual record type T and not the name of the discriminant in
8334 -- the generic formal. Example:
8336 -- generic
8337 -- type G (D : int) is private;
8338 -- package P is
8339 -- subtype W is G (D => 1);
8340 -- end package;
8341 -- type Rec (X : int) is record ... end record;
8342 -- package Q is new P (G => Rec);
8344 -- At the point of the instantiation, formal type G is Rec
8345 -- and therefore when reanalyzing "subtype W is G (D => 1);"
8346 -- which really looks like "subtype W is Rec (D => 1);" at
8347 -- the point of instantiation, we want to find the discriminant
8348 -- that corresponds to D in Rec, i.e. X.
8350 if Present (Original_Discriminant (Id))
8351 and then In_Instance
8352 then
8353 Discr := Find_Corresponding_Discriminant (Id, T);
8354 Found := True;
8356 else
8357 Discr := First_Discriminant (T);
8358 while Present (Discr) loop
8359 if Chars (Discr) = Chars (Id) then
8360 Found := True;
8361 exit;
8362 end if;
8364 Next_Discriminant (Discr);
8365 end loop;
8367 if not Found then
8368 Error_Msg_N ("& does not match any discriminant", Id);
8369 return New_Elmt_List;
8371 -- If the parent type is a generic formal, preserve the
8372 -- name of the discriminant for subsequent instances.
8373 -- see comment at the beginning of this if statement.
8375 elsif Is_Generic_Type (Root_Type (T)) then
8376 Set_Original_Discriminant (Id, Discr);
8377 end if;
8378 end if;
8380 Position := Pos_Of_Discr (T, Discr);
8382 if Present (Discr_Expr (Position)) then
8383 Error_Msg_N ("duplicate constraint for discriminant&", Id);
8385 else
8386 -- Each discriminant specified in the same named association
8387 -- must be associated with a separate copy of the
8388 -- corresponding expression.
8390 if Present (Next (Id)) then
8391 Expr := New_Copy_Tree (Expression (Constr));
8392 Set_Parent (Expr, Parent (Expression (Constr)));
8393 else
8394 Expr := Expression (Constr);
8395 end if;
8397 Discr_Expr (Position) := Expr;
8398 Analyze_And_Resolve (Expr, Base_Type (Etype (Discr)));
8399 end if;
8401 -- A discriminant association with more than one discriminant
8402 -- name is only allowed if the named discriminants are all of
8403 -- the same type (RM 3.7.1(8)).
8405 if E = Empty then
8406 E := Base_Type (Etype (Discr));
8408 elsif Base_Type (Etype (Discr)) /= E then
8409 Error_Msg_N
8410 ("all discriminants in an association " &
8411 "must have the same type", Id);
8412 end if;
8414 Next (Id);
8415 end loop;
8416 end if;
8418 Next (Constr);
8419 end loop;
8421 -- A discriminant constraint must provide exactly one value for each
8422 -- discriminant of the type (RM 3.7.1(8)).
8424 for J in Discr_Expr'Range loop
8425 if No (Discr_Expr (J)) then
8426 Error_Msg_N ("too few discriminants given in constraint", C);
8427 return New_Elmt_List;
8428 end if;
8429 end loop;
8431 -- Determine if there are discriminant expressions in the constraint
8433 for J in Discr_Expr'Range loop
8434 if Denotes_Discriminant
8435 (Discr_Expr (J), Check_Concurrent => True)
8436 then
8437 Discrim_Present := True;
8438 end if;
8439 end loop;
8441 -- Build an element list consisting of the expressions given in the
8442 -- discriminant constraint and apply the appropriate checks. The list
8443 -- is constructed after resolving any named discriminant associations
8444 -- and therefore the expressions appear in the textual order of the
8445 -- discriminants.
8447 Discr := First_Discriminant (T);
8448 for J in Discr_Expr'Range loop
8449 if Discr_Expr (J) /= Error then
8450 Append_Elmt (Discr_Expr (J), Elist);
8452 -- If any of the discriminant constraints is given by a
8453 -- discriminant and we are in a derived type declaration we
8454 -- have a discriminant renaming. Establish link between new
8455 -- and old discriminant.
8457 if Denotes_Discriminant (Discr_Expr (J)) then
8458 if Derived_Def then
8459 Set_Corresponding_Discriminant
8460 (Entity (Discr_Expr (J)), Discr);
8461 end if;
8463 -- Force the evaluation of non-discriminant expressions.
8464 -- If we have found a discriminant in the constraint 3.4(26)
8465 -- and 3.8(18) demand that no range checks are performed are
8466 -- after evaluation. If the constraint is for a component
8467 -- definition that has a per-object constraint, expressions are
8468 -- evaluated but not checked either. In all other cases perform
8469 -- a range check.
8471 else
8472 if Discrim_Present then
8473 null;
8475 elsif Nkind (Parent (Parent (Def))) = N_Component_Declaration
8476 and then
8477 Has_Per_Object_Constraint
8478 (Defining_Identifier (Parent (Parent (Def))))
8479 then
8480 null;
8482 elsif Is_Access_Type (Etype (Discr)) then
8483 Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
8485 else
8486 Apply_Range_Check (Discr_Expr (J), Etype (Discr));
8487 end if;
8489 Force_Evaluation (Discr_Expr (J));
8490 end if;
8492 -- Check that the designated type of an access discriminant's
8493 -- expression is not a class-wide type unless the discriminant's
8494 -- designated type is also class-wide.
8496 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
8497 and then not Is_Class_Wide_Type
8498 (Designated_Type (Etype (Discr)))
8499 and then Etype (Discr_Expr (J)) /= Any_Type
8500 and then Is_Class_Wide_Type
8501 (Designated_Type (Etype (Discr_Expr (J))))
8502 then
8503 Wrong_Type (Discr_Expr (J), Etype (Discr));
8505 elsif Is_Access_Type (Etype (Discr))
8506 and then not Is_Access_Constant (Etype (Discr))
8507 and then Is_Access_Type (Etype (Discr_Expr (J)))
8508 and then Is_Access_Constant (Etype (Discr_Expr (J)))
8509 then
8510 Error_Msg_NE
8511 ("constraint for discriminant& must be access to variable",
8512 Def, Discr);
8513 end if;
8514 end if;
8516 Next_Discriminant (Discr);
8517 end loop;
8519 return Elist;
8520 end Build_Discriminant_Constraints;
8522 ---------------------------------
8523 -- Build_Discriminated_Subtype --
8524 ---------------------------------
8526 procedure Build_Discriminated_Subtype
8527 (T : Entity_Id;
8528 Def_Id : Entity_Id;
8529 Elist : Elist_Id;
8530 Related_Nod : Node_Id;
8531 For_Access : Boolean := False)
8533 Has_Discrs : constant Boolean := Has_Discriminants (T);
8534 Constrained : constant Boolean :=
8535 (Has_Discrs
8536 and then not Is_Empty_Elmt_List (Elist)
8537 and then not Is_Class_Wide_Type (T))
8538 or else Is_Constrained (T);
8540 begin
8541 if Ekind (T) = E_Record_Type then
8542 if For_Access then
8543 Set_Ekind (Def_Id, E_Private_Subtype);
8544 Set_Is_For_Access_Subtype (Def_Id, True);
8545 else
8546 Set_Ekind (Def_Id, E_Record_Subtype);
8547 end if;
8549 -- Inherit preelaboration flag from base, for types for which it
8550 -- may have been set: records, private types, protected types.
8552 Set_Known_To_Have_Preelab_Init
8553 (Def_Id, Known_To_Have_Preelab_Init (T));
8555 elsif Ekind (T) = E_Task_Type then
8556 Set_Ekind (Def_Id, E_Task_Subtype);
8558 elsif Ekind (T) = E_Protected_Type then
8559 Set_Ekind (Def_Id, E_Protected_Subtype);
8560 Set_Known_To_Have_Preelab_Init
8561 (Def_Id, Known_To_Have_Preelab_Init (T));
8563 elsif Is_Private_Type (T) then
8564 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
8565 Set_Known_To_Have_Preelab_Init
8566 (Def_Id, Known_To_Have_Preelab_Init (T));
8568 elsif Is_Class_Wide_Type (T) then
8569 Set_Ekind (Def_Id, E_Class_Wide_Subtype);
8571 else
8572 -- Incomplete type. Attach subtype to list of dependents, to be
8573 -- completed with full view of parent type, unless is it the
8574 -- designated subtype of a record component within an init_proc.
8575 -- This last case arises for a component of an access type whose
8576 -- designated type is incomplete (e.g. a Taft Amendment type).
8577 -- The designated subtype is within an inner scope, and needs no
8578 -- elaboration, because only the access type is needed in the
8579 -- initialization procedure.
8581 Set_Ekind (Def_Id, Ekind (T));
8583 if For_Access and then Within_Init_Proc then
8584 null;
8585 else
8586 Append_Elmt (Def_Id, Private_Dependents (T));
8587 end if;
8588 end if;
8590 Set_Etype (Def_Id, T);
8591 Init_Size_Align (Def_Id);
8592 Set_Has_Discriminants (Def_Id, Has_Discrs);
8593 Set_Is_Constrained (Def_Id, Constrained);
8595 Set_First_Entity (Def_Id, First_Entity (T));
8596 Set_Last_Entity (Def_Id, Last_Entity (T));
8597 Set_Has_Implicit_Dereference
8598 (Def_Id, Has_Implicit_Dereference (T));
8600 -- If the subtype is the completion of a private declaration, there may
8601 -- have been representation clauses for the partial view, and they must
8602 -- be preserved. Build_Derived_Type chains the inherited clauses with
8603 -- the ones appearing on the extension. If this comes from a subtype
8604 -- declaration, all clauses are inherited.
8606 if No (First_Rep_Item (Def_Id)) then
8607 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
8608 end if;
8610 if Is_Tagged_Type (T) then
8611 Set_Is_Tagged_Type (Def_Id);
8612 Make_Class_Wide_Type (Def_Id);
8613 end if;
8615 Set_Stored_Constraint (Def_Id, No_Elist);
8617 if Has_Discrs then
8618 Set_Discriminant_Constraint (Def_Id, Elist);
8619 Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
8620 end if;
8622 if Is_Tagged_Type (T) then
8624 -- Ada 2005 (AI-251): In case of concurrent types we inherit the
8625 -- concurrent record type (which has the list of primitive
8626 -- operations).
8628 if Ada_Version >= Ada_2005
8629 and then Is_Concurrent_Type (T)
8630 then
8631 Set_Corresponding_Record_Type (Def_Id,
8632 Corresponding_Record_Type (T));
8633 else
8634 Set_Direct_Primitive_Operations (Def_Id,
8635 Direct_Primitive_Operations (T));
8636 end if;
8638 Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
8639 end if;
8641 -- Subtypes introduced by component declarations do not need to be
8642 -- marked as delayed, and do not get freeze nodes, because the semantics
8643 -- verifies that the parents of the subtypes are frozen before the
8644 -- enclosing record is frozen.
8646 if not Is_Type (Scope (Def_Id)) then
8647 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
8649 if Is_Private_Type (T)
8650 and then Present (Full_View (T))
8651 then
8652 Conditional_Delay (Def_Id, Full_View (T));
8653 else
8654 Conditional_Delay (Def_Id, T);
8655 end if;
8656 end if;
8658 if Is_Record_Type (T) then
8659 Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
8661 if Has_Discrs
8662 and then not Is_Empty_Elmt_List (Elist)
8663 and then not For_Access
8664 then
8665 Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
8666 elsif not For_Access then
8667 Set_Cloned_Subtype (Def_Id, T);
8668 end if;
8669 end if;
8670 end Build_Discriminated_Subtype;
8672 ---------------------------
8673 -- Build_Itype_Reference --
8674 ---------------------------
8676 procedure Build_Itype_Reference
8677 (Ityp : Entity_Id;
8678 Nod : Node_Id)
8680 IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
8681 begin
8683 -- Itype references are only created for use by the back-end
8685 if Inside_A_Generic then
8686 return;
8687 else
8688 Set_Itype (IR, Ityp);
8689 Insert_After (Nod, IR);
8690 end if;
8691 end Build_Itype_Reference;
8693 ------------------------
8694 -- Build_Scalar_Bound --
8695 ------------------------
8697 function Build_Scalar_Bound
8698 (Bound : Node_Id;
8699 Par_T : Entity_Id;
8700 Der_T : Entity_Id) return Node_Id
8702 New_Bound : Entity_Id;
8704 begin
8705 -- Note: not clear why this is needed, how can the original bound
8706 -- be unanalyzed at this point? and if it is, what business do we
8707 -- have messing around with it? and why is the base type of the
8708 -- parent type the right type for the resolution. It probably is
8709 -- not! It is OK for the new bound we are creating, but not for
8710 -- the old one??? Still if it never happens, no problem!
8712 Analyze_And_Resolve (Bound, Base_Type (Par_T));
8714 if Nkind_In (Bound, N_Integer_Literal, N_Real_Literal) then
8715 New_Bound := New_Copy (Bound);
8716 Set_Etype (New_Bound, Der_T);
8717 Set_Analyzed (New_Bound);
8719 elsif Is_Entity_Name (Bound) then
8720 New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
8722 -- The following is almost certainly wrong. What business do we have
8723 -- relocating a node (Bound) that is presumably still attached to
8724 -- the tree elsewhere???
8726 else
8727 New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
8728 end if;
8730 Set_Etype (New_Bound, Der_T);
8731 return New_Bound;
8732 end Build_Scalar_Bound;
8734 --------------------------------
8735 -- Build_Underlying_Full_View --
8736 --------------------------------
8738 procedure Build_Underlying_Full_View
8739 (N : Node_Id;
8740 Typ : Entity_Id;
8741 Par : Entity_Id)
8743 Loc : constant Source_Ptr := Sloc (N);
8744 Subt : constant Entity_Id :=
8745 Make_Defining_Identifier
8746 (Loc, New_External_Name (Chars (Typ), 'S'));
8748 Constr : Node_Id;
8749 Indic : Node_Id;
8750 C : Node_Id;
8751 Id : Node_Id;
8753 procedure Set_Discriminant_Name (Id : Node_Id);
8754 -- If the derived type has discriminants, they may rename discriminants
8755 -- of the parent. When building the full view of the parent, we need to
8756 -- recover the names of the original discriminants if the constraint is
8757 -- given by named associations.
8759 ---------------------------
8760 -- Set_Discriminant_Name --
8761 ---------------------------
8763 procedure Set_Discriminant_Name (Id : Node_Id) is
8764 Disc : Entity_Id;
8766 begin
8767 Set_Original_Discriminant (Id, Empty);
8769 if Has_Discriminants (Typ) then
8770 Disc := First_Discriminant (Typ);
8771 while Present (Disc) loop
8772 if Chars (Disc) = Chars (Id)
8773 and then Present (Corresponding_Discriminant (Disc))
8774 then
8775 Set_Chars (Id, Chars (Corresponding_Discriminant (Disc)));
8776 end if;
8777 Next_Discriminant (Disc);
8778 end loop;
8779 end if;
8780 end Set_Discriminant_Name;
8782 -- Start of processing for Build_Underlying_Full_View
8784 begin
8785 if Nkind (N) = N_Full_Type_Declaration then
8786 Constr := Constraint (Subtype_Indication (Type_Definition (N)));
8788 elsif Nkind (N) = N_Subtype_Declaration then
8789 Constr := New_Copy_Tree (Constraint (Subtype_Indication (N)));
8791 elsif Nkind (N) = N_Component_Declaration then
8792 Constr :=
8793 New_Copy_Tree
8794 (Constraint (Subtype_Indication (Component_Definition (N))));
8796 else
8797 raise Program_Error;
8798 end if;
8800 C := First (Constraints (Constr));
8801 while Present (C) loop
8802 if Nkind (C) = N_Discriminant_Association then
8803 Id := First (Selector_Names (C));
8804 while Present (Id) loop
8805 Set_Discriminant_Name (Id);
8806 Next (Id);
8807 end loop;
8808 end if;
8810 Next (C);
8811 end loop;
8813 Indic :=
8814 Make_Subtype_Declaration (Loc,
8815 Defining_Identifier => Subt,
8816 Subtype_Indication =>
8817 Make_Subtype_Indication (Loc,
8818 Subtype_Mark => New_Reference_To (Par, Loc),
8819 Constraint => New_Copy_Tree (Constr)));
8821 -- If this is a component subtype for an outer itype, it is not
8822 -- a list member, so simply set the parent link for analysis: if
8823 -- the enclosing type does not need to be in a declarative list,
8824 -- neither do the components.
8826 if Is_List_Member (N)
8827 and then Nkind (N) /= N_Component_Declaration
8828 then
8829 Insert_Before (N, Indic);
8830 else
8831 Set_Parent (Indic, Parent (N));
8832 end if;
8834 Analyze (Indic);
8835 Set_Underlying_Full_View (Typ, Full_View (Subt));
8836 end Build_Underlying_Full_View;
8838 -------------------------------
8839 -- Check_Abstract_Overriding --
8840 -------------------------------
8842 procedure Check_Abstract_Overriding (T : Entity_Id) is
8843 Alias_Subp : Entity_Id;
8844 Elmt : Elmt_Id;
8845 Op_List : Elist_Id;
8846 Subp : Entity_Id;
8847 Type_Def : Node_Id;
8849 procedure Check_Pragma_Implemented (Subp : Entity_Id);
8850 -- Ada 2012 (AI05-0030): Subprogram Subp overrides an interface routine
8851 -- which has pragma Implemented already set. Check whether Subp's entity
8852 -- kind conforms to the implementation kind of the overridden routine.
8854 procedure Check_Pragma_Implemented
8855 (Subp : Entity_Id;
8856 Iface_Subp : Entity_Id);
8857 -- Ada 2012 (AI05-0030): Subprogram Subp overrides interface routine
8858 -- Iface_Subp and both entities have pragma Implemented already set on
8859 -- them. Check whether the two implementation kinds are conforming.
8861 procedure Inherit_Pragma_Implemented
8862 (Subp : Entity_Id;
8863 Iface_Subp : Entity_Id);
8864 -- Ada 2012 (AI05-0030): Interface primitive Subp overrides interface
8865 -- subprogram Iface_Subp which has been marked by pragma Implemented.
8866 -- Propagate the implementation kind of Iface_Subp to Subp.
8868 ------------------------------
8869 -- Check_Pragma_Implemented --
8870 ------------------------------
8872 procedure Check_Pragma_Implemented (Subp : Entity_Id) is
8873 Iface_Alias : constant Entity_Id := Interface_Alias (Subp);
8874 Impl_Kind : constant Name_Id := Implementation_Kind (Iface_Alias);
8875 Subp_Alias : constant Entity_Id := Alias (Subp);
8876 Contr_Typ : Entity_Id;
8877 Impl_Subp : Entity_Id;
8879 begin
8880 -- Subp must have an alias since it is a hidden entity used to link
8881 -- an interface subprogram to its overriding counterpart.
8883 pragma Assert (Present (Subp_Alias));
8885 -- Handle aliases to synchronized wrappers
8887 Impl_Subp := Subp_Alias;
8889 if Is_Primitive_Wrapper (Impl_Subp) then
8890 Impl_Subp := Wrapped_Entity (Impl_Subp);
8891 end if;
8893 -- Extract the type of the controlling formal
8895 Contr_Typ := Etype (First_Formal (Subp_Alias));
8897 if Is_Concurrent_Record_Type (Contr_Typ) then
8898 Contr_Typ := Corresponding_Concurrent_Type (Contr_Typ);
8899 end if;
8901 -- An interface subprogram whose implementation kind is By_Entry must
8902 -- be implemented by an entry.
8904 if Impl_Kind = Name_By_Entry
8905 and then Ekind (Impl_Subp) /= E_Entry
8906 then
8907 Error_Msg_Node_2 := Iface_Alias;
8908 Error_Msg_NE
8909 ("type & must implement abstract subprogram & with an entry",
8910 Subp_Alias, Contr_Typ);
8912 elsif Impl_Kind = Name_By_Protected_Procedure then
8914 -- An interface subprogram whose implementation kind is By_
8915 -- Protected_Procedure cannot be implemented by a primitive
8916 -- procedure of a task type.
8918 if Ekind (Contr_Typ) /= E_Protected_Type then
8919 Error_Msg_Node_2 := Contr_Typ;
8920 Error_Msg_NE
8921 ("interface subprogram & cannot be implemented by a " &
8922 "primitive procedure of task type &", Subp_Alias,
8923 Iface_Alias);
8925 -- An interface subprogram whose implementation kind is By_
8926 -- Protected_Procedure must be implemented by a procedure.
8928 elsif Ekind (Impl_Subp) /= E_Procedure then
8929 Error_Msg_Node_2 := Iface_Alias;
8930 Error_Msg_NE
8931 ("type & must implement abstract subprogram & with a " &
8932 "procedure", Subp_Alias, Contr_Typ);
8933 end if;
8934 end if;
8935 end Check_Pragma_Implemented;
8937 ------------------------------
8938 -- Check_Pragma_Implemented --
8939 ------------------------------
8941 procedure Check_Pragma_Implemented
8942 (Subp : Entity_Id;
8943 Iface_Subp : Entity_Id)
8945 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
8946 Subp_Kind : constant Name_Id := Implementation_Kind (Subp);
8948 begin
8949 -- Ada 2012 (AI05-0030): The implementation kinds of an overridden
8950 -- and overriding subprogram are different. In general this is an
8951 -- error except when the implementation kind of the overridden
8952 -- subprograms is By_Any or Optional.
8954 if Iface_Kind /= Subp_Kind
8955 and then Iface_Kind /= Name_By_Any
8956 and then Iface_Kind /= Name_Optional
8957 then
8958 if Iface_Kind = Name_By_Entry then
8959 Error_Msg_N
8960 ("incompatible implementation kind, overridden subprogram " &
8961 "is marked By_Entry", Subp);
8962 else
8963 Error_Msg_N
8964 ("incompatible implementation kind, overridden subprogram " &
8965 "is marked By_Protected_Procedure", Subp);
8966 end if;
8967 end if;
8968 end Check_Pragma_Implemented;
8970 --------------------------------
8971 -- Inherit_Pragma_Implemented --
8972 --------------------------------
8974 procedure Inherit_Pragma_Implemented
8975 (Subp : Entity_Id;
8976 Iface_Subp : Entity_Id)
8978 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
8979 Loc : constant Source_Ptr := Sloc (Subp);
8980 Impl_Prag : Node_Id;
8982 begin
8983 -- Since the implementation kind is stored as a representation item
8984 -- rather than a flag, create a pragma node.
8986 Impl_Prag :=
8987 Make_Pragma (Loc,
8988 Chars => Name_Implemented,
8989 Pragma_Argument_Associations => New_List (
8990 Make_Pragma_Argument_Association (Loc,
8991 Expression =>
8992 New_Reference_To (Subp, Loc)),
8994 Make_Pragma_Argument_Association (Loc,
8995 Expression => Make_Identifier (Loc, Iface_Kind))));
8997 -- The pragma doesn't need to be analyzed because it is internally
8998 -- build. It is safe to directly register it as a rep item since we
8999 -- are only interested in the characters of the implementation kind.
9001 Record_Rep_Item (Subp, Impl_Prag);
9002 end Inherit_Pragma_Implemented;
9004 -- Start of processing for Check_Abstract_Overriding
9006 begin
9007 Op_List := Primitive_Operations (T);
9009 -- Loop to check primitive operations
9011 Elmt := First_Elmt (Op_List);
9012 while Present (Elmt) loop
9013 Subp := Node (Elmt);
9014 Alias_Subp := Alias (Subp);
9016 -- Inherited subprograms are identified by the fact that they do not
9017 -- come from source, and the associated source location is the
9018 -- location of the first subtype of the derived type.
9020 -- Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
9021 -- subprograms that "require overriding".
9023 -- Special exception, do not complain about failure to override the
9024 -- stream routines _Input and _Output, as well as the primitive
9025 -- operations used in dispatching selects since we always provide
9026 -- automatic overridings for these subprograms.
9028 -- Also ignore this rule for convention CIL since .NET libraries
9029 -- do bizarre things with interfaces???
9031 -- The partial view of T may have been a private extension, for
9032 -- which inherited functions dispatching on result are abstract.
9033 -- If the full view is a null extension, there is no need for
9034 -- overriding in Ada 2005, but wrappers need to be built for them
9035 -- (see exp_ch3, Build_Controlling_Function_Wrappers).
9037 if Is_Null_Extension (T)
9038 and then Has_Controlling_Result (Subp)
9039 and then Ada_Version >= Ada_2005
9040 and then Present (Alias_Subp)
9041 and then not Comes_From_Source (Subp)
9042 and then not Is_Abstract_Subprogram (Alias_Subp)
9043 and then not Is_Access_Type (Etype (Subp))
9044 then
9045 null;
9047 -- Ada 2005 (AI-251): Internal entities of interfaces need no
9048 -- processing because this check is done with the aliased
9049 -- entity
9051 elsif Present (Interface_Alias (Subp)) then
9052 null;
9054 elsif (Is_Abstract_Subprogram (Subp)
9055 or else Requires_Overriding (Subp)
9056 or else
9057 (Has_Controlling_Result (Subp)
9058 and then Present (Alias_Subp)
9059 and then not Comes_From_Source (Subp)
9060 and then Sloc (Subp) = Sloc (First_Subtype (T))))
9061 and then not Is_TSS (Subp, TSS_Stream_Input)
9062 and then not Is_TSS (Subp, TSS_Stream_Output)
9063 and then not Is_Abstract_Type (T)
9064 and then Convention (T) /= Convention_CIL
9065 and then not Is_Predefined_Interface_Primitive (Subp)
9067 -- Ada 2005 (AI-251): Do not consider hidden entities associated
9068 -- with abstract interface types because the check will be done
9069 -- with the aliased entity (otherwise we generate a duplicated
9070 -- error message).
9072 and then not Present (Interface_Alias (Subp))
9073 then
9074 if Present (Alias_Subp) then
9076 -- Only perform the check for a derived subprogram when the
9077 -- type has an explicit record extension. This avoids incorrect
9078 -- flagging of abstract subprograms for the case of a type
9079 -- without an extension that is derived from a formal type
9080 -- with a tagged actual (can occur within a private part).
9082 -- Ada 2005 (AI-391): In the case of an inherited function with
9083 -- a controlling result of the type, the rule does not apply if
9084 -- the type is a null extension (unless the parent function
9085 -- itself is abstract, in which case the function must still be
9086 -- be overridden). The expander will generate an overriding
9087 -- wrapper function calling the parent subprogram (see
9088 -- Exp_Ch3.Make_Controlling_Wrapper_Functions).
9090 Type_Def := Type_Definition (Parent (T));
9092 if Nkind (Type_Def) = N_Derived_Type_Definition
9093 and then Present (Record_Extension_Part (Type_Def))
9094 and then
9095 (Ada_Version < Ada_2005
9096 or else not Is_Null_Extension (T)
9097 or else Ekind (Subp) = E_Procedure
9098 or else not Has_Controlling_Result (Subp)
9099 or else Is_Abstract_Subprogram (Alias_Subp)
9100 or else Requires_Overriding (Subp)
9101 or else Is_Access_Type (Etype (Subp)))
9102 then
9103 -- Avoid reporting error in case of abstract predefined
9104 -- primitive inherited from interface type because the
9105 -- body of internally generated predefined primitives
9106 -- of tagged types are generated later by Freeze_Type
9108 if Is_Interface (Root_Type (T))
9109 and then Is_Abstract_Subprogram (Subp)
9110 and then Is_Predefined_Dispatching_Operation (Subp)
9111 and then not Comes_From_Source (Ultimate_Alias (Subp))
9112 then
9113 null;
9115 else
9116 Error_Msg_NE
9117 ("type must be declared abstract or & overridden",
9118 T, Subp);
9120 -- Traverse the whole chain of aliased subprograms to
9121 -- complete the error notification. This is especially
9122 -- useful for traceability of the chain of entities when
9123 -- the subprogram corresponds with an interface
9124 -- subprogram (which may be defined in another package).
9126 if Present (Alias_Subp) then
9127 declare
9128 E : Entity_Id;
9130 begin
9131 E := Subp;
9132 while Present (Alias (E)) loop
9134 -- Avoid reporting redundant errors on entities
9135 -- inherited from interfaces
9137 if Sloc (E) /= Sloc (T) then
9138 Error_Msg_Sloc := Sloc (E);
9139 Error_Msg_NE
9140 ("\& has been inherited #", T, Subp);
9141 end if;
9143 E := Alias (E);
9144 end loop;
9146 Error_Msg_Sloc := Sloc (E);
9148 -- AI05-0068: report if there is an overriding
9149 -- non-abstract subprogram that is invisible.
9151 if Is_Hidden (E)
9152 and then not Is_Abstract_Subprogram (E)
9153 then
9154 Error_Msg_NE
9155 ("\& subprogram# is not visible",
9156 T, Subp);
9158 else
9159 Error_Msg_NE
9160 ("\& has been inherited from subprogram #",
9161 T, Subp);
9162 end if;
9163 end;
9164 end if;
9165 end if;
9167 -- Ada 2005 (AI-345): Protected or task type implementing
9168 -- abstract interfaces.
9170 elsif Is_Concurrent_Record_Type (T)
9171 and then Present (Interfaces (T))
9172 then
9173 -- The controlling formal of Subp must be of mode "out",
9174 -- "in out" or an access-to-variable to be overridden.
9176 if Ekind (First_Formal (Subp)) = E_In_Parameter
9177 and then Ekind (Subp) /= E_Function
9178 then
9179 if not Is_Predefined_Dispatching_Operation (Subp)
9180 and then Is_Protected_Type
9181 (Corresponding_Concurrent_Type (T))
9182 then
9183 Error_Msg_PT (T, Subp);
9184 end if;
9186 -- Some other kind of overriding failure
9188 else
9189 Error_Msg_NE
9190 ("interface subprogram & must be overridden",
9191 T, Subp);
9193 -- Examine primitive operations of synchronized type,
9194 -- to find homonyms that have the wrong profile.
9196 declare
9197 Prim : Entity_Id;
9199 begin
9200 Prim :=
9201 First_Entity (Corresponding_Concurrent_Type (T));
9202 while Present (Prim) loop
9203 if Chars (Prim) = Chars (Subp) then
9204 Error_Msg_NE
9205 ("profile is not type conformant with "
9206 & "prefixed view profile of "
9207 & "inherited operation&", Prim, Subp);
9208 end if;
9210 Next_Entity (Prim);
9211 end loop;
9212 end;
9213 end if;
9214 end if;
9216 else
9217 Error_Msg_Node_2 := T;
9218 Error_Msg_N
9219 ("abstract subprogram& not allowed for type&", Subp);
9221 -- Also post unconditional warning on the type (unconditional
9222 -- so that if there are more than one of these cases, we get
9223 -- them all, and not just the first one).
9225 Error_Msg_Node_2 := Subp;
9226 Error_Msg_N ("nonabstract type& has abstract subprogram&!", T);
9227 end if;
9228 end if;
9230 -- Ada 2012 (AI05-0030): Perform some checks related to pragma
9231 -- Implemented
9233 -- Subp is an expander-generated procedure which maps an interface
9234 -- alias to a protected wrapper. The interface alias is flagged by
9235 -- pragma Implemented. Ensure that Subp is a procedure when the
9236 -- implementation kind is By_Protected_Procedure or an entry when
9237 -- By_Entry.
9239 if Ada_Version >= Ada_2012
9240 and then Is_Hidden (Subp)
9241 and then Present (Interface_Alias (Subp))
9242 and then Has_Rep_Pragma (Interface_Alias (Subp), Name_Implemented)
9243 then
9244 Check_Pragma_Implemented (Subp);
9245 end if;
9247 -- Subp is an interface primitive which overrides another interface
9248 -- primitive marked with pragma Implemented.
9250 if Ada_Version >= Ada_2012
9251 and then Present (Overridden_Operation (Subp))
9252 and then Has_Rep_Pragma
9253 (Overridden_Operation (Subp), Name_Implemented)
9254 then
9255 -- If the overriding routine is also marked by Implemented, check
9256 -- that the two implementation kinds are conforming.
9258 if Has_Rep_Pragma (Subp, Name_Implemented) then
9259 Check_Pragma_Implemented
9260 (Subp => Subp,
9261 Iface_Subp => Overridden_Operation (Subp));
9263 -- Otherwise the overriding routine inherits the implementation
9264 -- kind from the overridden subprogram.
9266 else
9267 Inherit_Pragma_Implemented
9268 (Subp => Subp,
9269 Iface_Subp => Overridden_Operation (Subp));
9270 end if;
9271 end if;
9273 Next_Elmt (Elmt);
9274 end loop;
9275 end Check_Abstract_Overriding;
9277 ------------------------------------------------
9278 -- Check_Access_Discriminant_Requires_Limited --
9279 ------------------------------------------------
9281 procedure Check_Access_Discriminant_Requires_Limited
9282 (D : Node_Id;
9283 Loc : Node_Id)
9285 begin
9286 -- A discriminant_specification for an access discriminant shall appear
9287 -- only in the declaration for a task or protected type, or for a type
9288 -- with the reserved word 'limited' in its definition or in one of its
9289 -- ancestors (RM 3.7(10)).
9291 -- AI-0063: The proper condition is that type must be immutably limited,
9292 -- or else be a partial view.
9294 if Nkind (Discriminant_Type (D)) = N_Access_Definition then
9295 if Is_Immutably_Limited_Type (Current_Scope)
9296 or else
9297 (Nkind (Parent (Current_Scope)) = N_Private_Type_Declaration
9298 and then Limited_Present (Parent (Current_Scope)))
9299 then
9300 null;
9302 else
9303 Error_Msg_N
9304 ("access discriminants allowed only for limited types", Loc);
9305 end if;
9306 end if;
9307 end Check_Access_Discriminant_Requires_Limited;
9309 -----------------------------------
9310 -- Check_Aliased_Component_Types --
9311 -----------------------------------
9313 procedure Check_Aliased_Component_Types (T : Entity_Id) is
9314 C : Entity_Id;
9316 begin
9317 -- ??? Also need to check components of record extensions, but not
9318 -- components of protected types (which are always limited).
9320 -- Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
9321 -- types to be unconstrained. This is safe because it is illegal to
9322 -- create access subtypes to such types with explicit discriminant
9323 -- constraints.
9325 if not Is_Limited_Type (T) then
9326 if Ekind (T) = E_Record_Type then
9327 C := First_Component (T);
9328 while Present (C) loop
9329 if Is_Aliased (C)
9330 and then Has_Discriminants (Etype (C))
9331 and then not Is_Constrained (Etype (C))
9332 and then not In_Instance_Body
9333 and then Ada_Version < Ada_2005
9334 then
9335 Error_Msg_N
9336 ("aliased component must be constrained (RM 3.6(11))",
9338 end if;
9340 Next_Component (C);
9341 end loop;
9343 elsif Ekind (T) = E_Array_Type then
9344 if Has_Aliased_Components (T)
9345 and then Has_Discriminants (Component_Type (T))
9346 and then not Is_Constrained (Component_Type (T))
9347 and then not In_Instance_Body
9348 and then Ada_Version < Ada_2005
9349 then
9350 Error_Msg_N
9351 ("aliased component type must be constrained (RM 3.6(11))",
9353 end if;
9354 end if;
9355 end if;
9356 end Check_Aliased_Component_Types;
9358 ----------------------
9359 -- Check_Completion --
9360 ----------------------
9362 procedure Check_Completion (Body_Id : Node_Id := Empty) is
9363 E : Entity_Id;
9365 procedure Post_Error;
9366 -- Post error message for lack of completion for entity E
9368 ----------------
9369 -- Post_Error --
9370 ----------------
9372 procedure Post_Error is
9374 procedure Missing_Body;
9375 -- Output missing body message
9377 ------------------
9378 -- Missing_Body --
9379 ------------------
9381 procedure Missing_Body is
9382 begin
9383 -- Spec is in same unit, so we can post on spec
9385 if In_Same_Source_Unit (Body_Id, E) then
9386 Error_Msg_N ("missing body for &", E);
9388 -- Spec is in a separate unit, so we have to post on the body
9390 else
9391 Error_Msg_NE ("missing body for & declared#!", Body_Id, E);
9392 end if;
9393 end Missing_Body;
9395 -- Start of processing for Post_Error
9397 begin
9398 if not Comes_From_Source (E) then
9400 if Ekind_In (E, E_Task_Type, E_Protected_Type) then
9401 -- It may be an anonymous protected type created for a
9402 -- single variable. Post error on variable, if present.
9404 declare
9405 Var : Entity_Id;
9407 begin
9408 Var := First_Entity (Current_Scope);
9409 while Present (Var) loop
9410 exit when Etype (Var) = E
9411 and then Comes_From_Source (Var);
9413 Next_Entity (Var);
9414 end loop;
9416 if Present (Var) then
9417 E := Var;
9418 end if;
9419 end;
9420 end if;
9421 end if;
9423 -- If a generated entity has no completion, then either previous
9424 -- semantic errors have disabled the expansion phase, or else we had
9425 -- missing subunits, or else we are compiling without expansion,
9426 -- or else something is very wrong.
9428 if not Comes_From_Source (E) then
9429 pragma Assert
9430 (Serious_Errors_Detected > 0
9431 or else Configurable_Run_Time_Violations > 0
9432 or else Subunits_Missing
9433 or else not Expander_Active);
9434 return;
9436 -- Here for source entity
9438 else
9439 -- Here if no body to post the error message, so we post the error
9440 -- on the declaration that has no completion. This is not really
9441 -- the right place to post it, think about this later ???
9443 if No (Body_Id) then
9444 if Is_Type (E) then
9445 Error_Msg_NE
9446 ("missing full declaration for }", Parent (E), E);
9447 else
9448 Error_Msg_NE ("missing body for &", Parent (E), E);
9449 end if;
9451 -- Package body has no completion for a declaration that appears
9452 -- in the corresponding spec. Post error on the body, with a
9453 -- reference to the non-completed declaration.
9455 else
9456 Error_Msg_Sloc := Sloc (E);
9458 if Is_Type (E) then
9459 Error_Msg_NE ("missing full declaration for }!", Body_Id, E);
9461 elsif Is_Overloadable (E)
9462 and then Current_Entity_In_Scope (E) /= E
9463 then
9464 -- It may be that the completion is mistyped and appears as
9465 -- a distinct overloading of the entity.
9467 declare
9468 Candidate : constant Entity_Id :=
9469 Current_Entity_In_Scope (E);
9470 Decl : constant Node_Id :=
9471 Unit_Declaration_Node (Candidate);
9473 begin
9474 if Is_Overloadable (Candidate)
9475 and then Ekind (Candidate) = Ekind (E)
9476 and then Nkind (Decl) = N_Subprogram_Body
9477 and then Acts_As_Spec (Decl)
9478 then
9479 Check_Type_Conformant (Candidate, E);
9481 else
9482 Missing_Body;
9483 end if;
9484 end;
9486 else
9487 Missing_Body;
9488 end if;
9489 end if;
9490 end if;
9491 end Post_Error;
9493 -- Start of processing for Check_Completion
9495 begin
9496 E := First_Entity (Current_Scope);
9497 while Present (E) loop
9498 if Is_Intrinsic_Subprogram (E) then
9499 null;
9501 -- The following situation requires special handling: a child unit
9502 -- that appears in the context clause of the body of its parent:
9504 -- procedure Parent.Child (...);
9506 -- with Parent.Child;
9507 -- package body Parent is
9509 -- Here Parent.Child appears as a local entity, but should not be
9510 -- flagged as requiring completion, because it is a compilation
9511 -- unit.
9513 -- Ignore missing completion for a subprogram that does not come from
9514 -- source (including the _Call primitive operation of RAS types,
9515 -- which has to have the flag Comes_From_Source for other purposes):
9516 -- we assume that the expander will provide the missing completion.
9517 -- In case of previous errors, other expansion actions that provide
9518 -- bodies for null procedures with not be invoked, so inhibit message
9519 -- in those cases.
9521 -- Note that E_Operator is not in the list that follows, because
9522 -- this kind is reserved for predefined operators, that are
9523 -- intrinsic and do not need completion.
9525 elsif Ekind (E) = E_Function
9526 or else Ekind (E) = E_Procedure
9527 or else Ekind (E) = E_Generic_Function
9528 or else Ekind (E) = E_Generic_Procedure
9529 then
9530 if Has_Completion (E) then
9531 null;
9533 elsif Is_Subprogram (E) and then Is_Abstract_Subprogram (E) then
9534 null;
9536 elsif Is_Subprogram (E)
9537 and then (not Comes_From_Source (E)
9538 or else Chars (E) = Name_uCall)
9539 then
9540 null;
9542 elsif
9543 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
9544 then
9545 null;
9547 elsif Nkind (Parent (E)) = N_Procedure_Specification
9548 and then Null_Present (Parent (E))
9549 and then Serious_Errors_Detected > 0
9550 then
9551 null;
9553 else
9554 Post_Error;
9555 end if;
9557 elsif Is_Entry (E) then
9558 if not Has_Completion (E) and then
9559 (Ekind (Scope (E)) = E_Protected_Object
9560 or else Ekind (Scope (E)) = E_Protected_Type)
9561 then
9562 Post_Error;
9563 end if;
9565 elsif Is_Package_Or_Generic_Package (E) then
9566 if Unit_Requires_Body (E) then
9567 if not Has_Completion (E)
9568 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
9569 N_Compilation_Unit
9570 then
9571 Post_Error;
9572 end if;
9574 elsif not Is_Child_Unit (E) then
9575 May_Need_Implicit_Body (E);
9576 end if;
9578 -- A formal incomplete type (Ada 2012) does not require a completion;
9579 -- other incomplete type declarations do.
9581 elsif Ekind (E) = E_Incomplete_Type
9582 and then No (Underlying_Type (E))
9583 and then not Is_Generic_Type (E)
9584 then
9585 Post_Error;
9587 elsif (Ekind (E) = E_Task_Type or else
9588 Ekind (E) = E_Protected_Type)
9589 and then not Has_Completion (E)
9590 then
9591 Post_Error;
9593 -- A single task declared in the current scope is a constant, verify
9594 -- that the body of its anonymous type is in the same scope. If the
9595 -- task is defined elsewhere, this may be a renaming declaration for
9596 -- which no completion is needed.
9598 elsif Ekind (E) = E_Constant
9599 and then Ekind (Etype (E)) = E_Task_Type
9600 and then not Has_Completion (Etype (E))
9601 and then Scope (Etype (E)) = Current_Scope
9602 then
9603 Post_Error;
9605 elsif Ekind (E) = E_Protected_Object
9606 and then not Has_Completion (Etype (E))
9607 then
9608 Post_Error;
9610 elsif Ekind (E) = E_Record_Type then
9611 if Is_Tagged_Type (E) then
9612 Check_Abstract_Overriding (E);
9613 Check_Conventions (E);
9614 end if;
9616 Check_Aliased_Component_Types (E);
9618 elsif Ekind (E) = E_Array_Type then
9619 Check_Aliased_Component_Types (E);
9621 end if;
9623 Next_Entity (E);
9624 end loop;
9625 end Check_Completion;
9627 ------------------------------------
9628 -- Check_CPP_Type_Has_No_Defaults --
9629 ------------------------------------
9631 procedure Check_CPP_Type_Has_No_Defaults (T : Entity_Id) is
9632 Tdef : constant Node_Id := Type_Definition (Declaration_Node (T));
9633 Clist : Node_Id;
9634 Comp : Node_Id;
9636 begin
9637 -- Obtain the component list
9639 if Nkind (Tdef) = N_Record_Definition then
9640 Clist := Component_List (Tdef);
9641 else pragma Assert (Nkind (Tdef) = N_Derived_Type_Definition);
9642 Clist := Component_List (Record_Extension_Part (Tdef));
9643 end if;
9645 -- Check all components to ensure no default expressions
9647 if Present (Clist) then
9648 Comp := First (Component_Items (Clist));
9649 while Present (Comp) loop
9650 if Present (Expression (Comp)) then
9651 Error_Msg_N
9652 ("component of imported 'C'P'P type cannot have "
9653 & "default expression", Expression (Comp));
9654 end if;
9656 Next (Comp);
9657 end loop;
9658 end if;
9659 end Check_CPP_Type_Has_No_Defaults;
9661 ----------------------------
9662 -- Check_Delta_Expression --
9663 ----------------------------
9665 procedure Check_Delta_Expression (E : Node_Id) is
9666 begin
9667 if not (Is_Real_Type (Etype (E))) then
9668 Wrong_Type (E, Any_Real);
9670 elsif not Is_OK_Static_Expression (E) then
9671 Flag_Non_Static_Expr
9672 ("non-static expression used for delta value!", E);
9674 elsif not UR_Is_Positive (Expr_Value_R (E)) then
9675 Error_Msg_N ("delta expression must be positive", E);
9677 else
9678 return;
9679 end if;
9681 -- If any of above errors occurred, then replace the incorrect
9682 -- expression by the real 0.1, which should prevent further errors.
9684 Rewrite (E,
9685 Make_Real_Literal (Sloc (E), Ureal_Tenth));
9686 Analyze_And_Resolve (E, Standard_Float);
9687 end Check_Delta_Expression;
9689 -----------------------------
9690 -- Check_Digits_Expression --
9691 -----------------------------
9693 procedure Check_Digits_Expression (E : Node_Id) is
9694 begin
9695 if not (Is_Integer_Type (Etype (E))) then
9696 Wrong_Type (E, Any_Integer);
9698 elsif not Is_OK_Static_Expression (E) then
9699 Flag_Non_Static_Expr
9700 ("non-static expression used for digits value!", E);
9702 elsif Expr_Value (E) <= 0 then
9703 Error_Msg_N ("digits value must be greater than zero", E);
9705 else
9706 return;
9707 end if;
9709 -- If any of above errors occurred, then replace the incorrect
9710 -- expression by the integer 1, which should prevent further errors.
9712 Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
9713 Analyze_And_Resolve (E, Standard_Integer);
9715 end Check_Digits_Expression;
9717 --------------------------
9718 -- Check_Initialization --
9719 --------------------------
9721 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
9722 begin
9723 if Is_Limited_Type (T)
9724 and then not In_Instance
9725 and then not In_Inlined_Body
9726 then
9727 if not OK_For_Limited_Init (T, Exp) then
9729 -- In GNAT mode, this is just a warning, to allow it to be evilly
9730 -- turned off. Otherwise it is a real error.
9732 if GNAT_Mode then
9733 Error_Msg_N
9734 ("?cannot initialize entities of limited type!", Exp);
9736 elsif Ada_Version < Ada_2005 then
9738 -- The side effect removal machinery may generate illegal Ada
9739 -- code to avoid the usage of access types and 'reference in
9740 -- Alfa mode. Since this is legal code with respect to theorem
9741 -- proving, do not emit the error.
9743 if Alfa_Mode
9744 and then Nkind (Exp) = N_Function_Call
9745 and then Nkind (Parent (Exp)) = N_Object_Declaration
9746 and then not Comes_From_Source
9747 (Defining_Identifier (Parent (Exp)))
9748 then
9749 null;
9751 else
9752 Error_Msg_N
9753 ("cannot initialize entities of limited type", Exp);
9754 Explain_Limited_Type (T, Exp);
9755 end if;
9757 else
9758 -- Specialize error message according to kind of illegal
9759 -- initial expression.
9761 if Nkind (Exp) = N_Type_Conversion
9762 and then Nkind (Expression (Exp)) = N_Function_Call
9763 then
9764 Error_Msg_N
9765 ("illegal context for call"
9766 & " to function with limited result", Exp);
9768 else
9769 Error_Msg_N
9770 ("initialization of limited object requires aggregate "
9771 & "or function call", Exp);
9772 end if;
9773 end if;
9774 end if;
9775 end if;
9776 end Check_Initialization;
9778 ----------------------
9779 -- Check_Interfaces --
9780 ----------------------
9782 procedure Check_Interfaces (N : Node_Id; Def : Node_Id) is
9783 Parent_Type : constant Entity_Id := Etype (Defining_Identifier (N));
9785 Iface : Node_Id;
9786 Iface_Def : Node_Id;
9787 Iface_Typ : Entity_Id;
9788 Parent_Node : Node_Id;
9790 Is_Task : Boolean := False;
9791 -- Set True if parent type or any progenitor is a task interface
9793 Is_Protected : Boolean := False;
9794 -- Set True if parent type or any progenitor is a protected interface
9796 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
9797 -- Check that a progenitor is compatible with declaration.
9798 -- Error is posted on Error_Node.
9800 ------------------
9801 -- Check_Ifaces --
9802 ------------------
9804 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
9805 Iface_Id : constant Entity_Id :=
9806 Defining_Identifier (Parent (Iface_Def));
9807 Type_Def : Node_Id;
9809 begin
9810 if Nkind (N) = N_Private_Extension_Declaration then
9811 Type_Def := N;
9812 else
9813 Type_Def := Type_Definition (N);
9814 end if;
9816 if Is_Task_Interface (Iface_Id) then
9817 Is_Task := True;
9819 elsif Is_Protected_Interface (Iface_Id) then
9820 Is_Protected := True;
9821 end if;
9823 if Is_Synchronized_Interface (Iface_Id) then
9825 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
9826 -- extension derived from a synchronized interface must explicitly
9827 -- be declared synchronized, because the full view will be a
9828 -- synchronized type.
9830 if Nkind (N) = N_Private_Extension_Declaration then
9831 if not Synchronized_Present (N) then
9832 Error_Msg_NE
9833 ("private extension of& must be explicitly synchronized",
9834 N, Iface_Id);
9835 end if;
9837 -- However, by 3.9.4(16/2), a full type that is a record extension
9838 -- is never allowed to derive from a synchronized interface (note
9839 -- that interfaces must be excluded from this check, because those
9840 -- are represented by derived type definitions in some cases).
9842 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition
9843 and then not Interface_Present (Type_Definition (N))
9844 then
9845 Error_Msg_N ("record extension cannot derive from synchronized"
9846 & " interface", Error_Node);
9847 end if;
9848 end if;
9850 -- Check that the characteristics of the progenitor are compatible
9851 -- with the explicit qualifier in the declaration.
9852 -- The check only applies to qualifiers that come from source.
9853 -- Limited_Present also appears in the declaration of corresponding
9854 -- records, and the check does not apply to them.
9856 if Limited_Present (Type_Def)
9857 and then not
9858 Is_Concurrent_Record_Type (Defining_Identifier (N))
9859 then
9860 if Is_Limited_Interface (Parent_Type)
9861 and then not Is_Limited_Interface (Iface_Id)
9862 then
9863 Error_Msg_NE
9864 ("progenitor& must be limited interface",
9865 Error_Node, Iface_Id);
9867 elsif
9868 (Task_Present (Iface_Def)
9869 or else Protected_Present (Iface_Def)
9870 or else Synchronized_Present (Iface_Def))
9871 and then Nkind (N) /= N_Private_Extension_Declaration
9872 and then not Error_Posted (N)
9873 then
9874 Error_Msg_NE
9875 ("progenitor& must be limited interface",
9876 Error_Node, Iface_Id);
9877 end if;
9879 -- Protected interfaces can only inherit from limited, synchronized
9880 -- or protected interfaces.
9882 elsif Nkind (N) = N_Full_Type_Declaration
9883 and then Protected_Present (Type_Def)
9884 then
9885 if Limited_Present (Iface_Def)
9886 or else Synchronized_Present (Iface_Def)
9887 or else Protected_Present (Iface_Def)
9888 then
9889 null;
9891 elsif Task_Present (Iface_Def) then
9892 Error_Msg_N ("(Ada 2005) protected interface cannot inherit"
9893 & " from task interface", Error_Node);
9895 else
9896 Error_Msg_N ("(Ada 2005) protected interface cannot inherit"
9897 & " from non-limited interface", Error_Node);
9898 end if;
9900 -- Ada 2005 (AI-345): Synchronized interfaces can only inherit from
9901 -- limited and synchronized.
9903 elsif Synchronized_Present (Type_Def) then
9904 if Limited_Present (Iface_Def)
9905 or else Synchronized_Present (Iface_Def)
9906 then
9907 null;
9909 elsif Protected_Present (Iface_Def)
9910 and then Nkind (N) /= N_Private_Extension_Declaration
9911 then
9912 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
9913 & " from protected interface", Error_Node);
9915 elsif Task_Present (Iface_Def)
9916 and then Nkind (N) /= N_Private_Extension_Declaration
9917 then
9918 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
9919 & " from task interface", Error_Node);
9921 elsif not Is_Limited_Interface (Iface_Id) then
9922 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
9923 & " from non-limited interface", Error_Node);
9924 end if;
9926 -- Ada 2005 (AI-345): Task interfaces can only inherit from limited,
9927 -- synchronized or task interfaces.
9929 elsif Nkind (N) = N_Full_Type_Declaration
9930 and then Task_Present (Type_Def)
9931 then
9932 if Limited_Present (Iface_Def)
9933 or else Synchronized_Present (Iface_Def)
9934 or else Task_Present (Iface_Def)
9935 then
9936 null;
9938 elsif Protected_Present (Iface_Def) then
9939 Error_Msg_N ("(Ada 2005) task interface cannot inherit from"
9940 & " protected interface", Error_Node);
9942 else
9943 Error_Msg_N ("(Ada 2005) task interface cannot inherit from"
9944 & " non-limited interface", Error_Node);
9945 end if;
9946 end if;
9947 end Check_Ifaces;
9949 -- Start of processing for Check_Interfaces
9951 begin
9952 if Is_Interface (Parent_Type) then
9953 if Is_Task_Interface (Parent_Type) then
9954 Is_Task := True;
9956 elsif Is_Protected_Interface (Parent_Type) then
9957 Is_Protected := True;
9958 end if;
9959 end if;
9961 if Nkind (N) = N_Private_Extension_Declaration then
9963 -- Check that progenitors are compatible with declaration
9965 Iface := First (Interface_List (Def));
9966 while Present (Iface) loop
9967 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
9969 Parent_Node := Parent (Base_Type (Iface_Typ));
9970 Iface_Def := Type_Definition (Parent_Node);
9972 if not Is_Interface (Iface_Typ) then
9973 Diagnose_Interface (Iface, Iface_Typ);
9975 else
9976 Check_Ifaces (Iface_Def, Iface);
9977 end if;
9979 Next (Iface);
9980 end loop;
9982 if Is_Task and Is_Protected then
9983 Error_Msg_N
9984 ("type cannot derive from task and protected interface", N);
9985 end if;
9987 return;
9988 end if;
9990 -- Full type declaration of derived type.
9991 -- Check compatibility with parent if it is interface type
9993 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
9994 and then Is_Interface (Parent_Type)
9995 then
9996 Parent_Node := Parent (Parent_Type);
9998 -- More detailed checks for interface varieties
10000 Check_Ifaces
10001 (Iface_Def => Type_Definition (Parent_Node),
10002 Error_Node => Subtype_Indication (Type_Definition (N)));
10003 end if;
10005 Iface := First (Interface_List (Def));
10006 while Present (Iface) loop
10007 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
10009 Parent_Node := Parent (Base_Type (Iface_Typ));
10010 Iface_Def := Type_Definition (Parent_Node);
10012 if not Is_Interface (Iface_Typ) then
10013 Diagnose_Interface (Iface, Iface_Typ);
10015 else
10016 -- "The declaration of a specific descendant of an interface
10017 -- type freezes the interface type" RM 13.14
10019 Freeze_Before (N, Iface_Typ);
10020 Check_Ifaces (Iface_Def, Error_Node => Iface);
10021 end if;
10023 Next (Iface);
10024 end loop;
10026 if Is_Task and Is_Protected then
10027 Error_Msg_N
10028 ("type cannot derive from task and protected interface", N);
10029 end if;
10030 end Check_Interfaces;
10032 ------------------------------------
10033 -- Check_Or_Process_Discriminants --
10034 ------------------------------------
10036 -- If an incomplete or private type declaration was already given for the
10037 -- type, the discriminants may have already been processed if they were
10038 -- present on the incomplete declaration. In this case a full conformance
10039 -- check has been performed in Find_Type_Name, and we then recheck here
10040 -- some properties that can't be checked on the partial view alone.
10041 -- Otherwise we call Process_Discriminants.
10043 procedure Check_Or_Process_Discriminants
10044 (N : Node_Id;
10045 T : Entity_Id;
10046 Prev : Entity_Id := Empty)
10048 begin
10049 if Has_Discriminants (T) then
10051 -- Discriminants are already set on T if they were already present
10052 -- on the partial view. Make them visible to component declarations.
10054 declare
10055 D : Entity_Id;
10056 -- Discriminant on T (full view) referencing expr on partial view
10058 Prev_D : Entity_Id;
10059 -- Entity of corresponding discriminant on partial view
10061 New_D : Node_Id;
10062 -- Discriminant specification for full view, expression is the
10063 -- syntactic copy on full view (which has been checked for
10064 -- conformance with partial view), only used here to post error
10065 -- message.
10067 begin
10068 D := First_Discriminant (T);
10069 New_D := First (Discriminant_Specifications (N));
10070 while Present (D) loop
10071 Prev_D := Current_Entity (D);
10072 Set_Current_Entity (D);
10073 Set_Is_Immediately_Visible (D);
10074 Set_Homonym (D, Prev_D);
10076 -- Handle the case where there is an untagged partial view and
10077 -- the full view is tagged: must disallow discriminants with
10078 -- defaults, unless compiling for Ada 2012, which allows a
10079 -- limited tagged type to have defaulted discriminants (see
10080 -- AI05-0214). However, suppress the error here if it was
10081 -- already reported on the default expression of the partial
10082 -- view.
10084 if Is_Tagged_Type (T)
10085 and then Present (Expression (Parent (D)))
10086 and then (not Is_Limited_Type (Current_Scope)
10087 or else Ada_Version < Ada_2012)
10088 and then not Error_Posted (Expression (Parent (D)))
10089 then
10090 if Ada_Version >= Ada_2012 then
10091 Error_Msg_N
10092 ("discriminants of nonlimited tagged type cannot have"
10093 & " defaults",
10094 Expression (New_D));
10095 else
10096 Error_Msg_N
10097 ("discriminants of tagged type cannot have defaults",
10098 Expression (New_D));
10099 end if;
10100 end if;
10102 -- Ada 2005 (AI-230): Access discriminant allowed in
10103 -- non-limited record types.
10105 if Ada_Version < Ada_2005 then
10107 -- This restriction gets applied to the full type here. It
10108 -- has already been applied earlier to the partial view.
10110 Check_Access_Discriminant_Requires_Limited (Parent (D), N);
10111 end if;
10113 Next_Discriminant (D);
10114 Next (New_D);
10115 end loop;
10116 end;
10118 elsif Present (Discriminant_Specifications (N)) then
10119 Process_Discriminants (N, Prev);
10120 end if;
10121 end Check_Or_Process_Discriminants;
10123 ----------------------
10124 -- Check_Real_Bound --
10125 ----------------------
10127 procedure Check_Real_Bound (Bound : Node_Id) is
10128 begin
10129 if not Is_Real_Type (Etype (Bound)) then
10130 Error_Msg_N
10131 ("bound in real type definition must be of real type", Bound);
10133 elsif not Is_OK_Static_Expression (Bound) then
10134 Flag_Non_Static_Expr
10135 ("non-static expression used for real type bound!", Bound);
10137 else
10138 return;
10139 end if;
10141 Rewrite
10142 (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
10143 Analyze (Bound);
10144 Resolve (Bound, Standard_Float);
10145 end Check_Real_Bound;
10147 ------------------------------
10148 -- Complete_Private_Subtype --
10149 ------------------------------
10151 procedure Complete_Private_Subtype
10152 (Priv : Entity_Id;
10153 Full : Entity_Id;
10154 Full_Base : Entity_Id;
10155 Related_Nod : Node_Id)
10157 Save_Next_Entity : Entity_Id;
10158 Save_Homonym : Entity_Id;
10160 begin
10161 -- Set semantic attributes for (implicit) private subtype completion.
10162 -- If the full type has no discriminants, then it is a copy of the full
10163 -- view of the base. Otherwise, it is a subtype of the base with a
10164 -- possible discriminant constraint. Save and restore the original
10165 -- Next_Entity field of full to ensure that the calls to Copy_Node
10166 -- do not corrupt the entity chain.
10168 -- Note that the type of the full view is the same entity as the type of
10169 -- the partial view. In this fashion, the subtype has access to the
10170 -- correct view of the parent.
10172 Save_Next_Entity := Next_Entity (Full);
10173 Save_Homonym := Homonym (Priv);
10175 case Ekind (Full_Base) is
10176 when E_Record_Type |
10177 E_Record_Subtype |
10178 Class_Wide_Kind |
10179 Private_Kind |
10180 Task_Kind |
10181 Protected_Kind =>
10182 Copy_Node (Priv, Full);
10184 Set_Has_Discriminants (Full, Has_Discriminants (Full_Base));
10185 Set_First_Entity (Full, First_Entity (Full_Base));
10186 Set_Last_Entity (Full, Last_Entity (Full_Base));
10188 when others =>
10189 Copy_Node (Full_Base, Full);
10190 Set_Chars (Full, Chars (Priv));
10191 Conditional_Delay (Full, Priv);
10192 Set_Sloc (Full, Sloc (Priv));
10193 end case;
10195 Set_Next_Entity (Full, Save_Next_Entity);
10196 Set_Homonym (Full, Save_Homonym);
10197 Set_Associated_Node_For_Itype (Full, Related_Nod);
10199 -- Set common attributes for all subtypes: kind, convention, etc.
10201 Set_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
10202 Set_Convention (Full, Convention (Full_Base));
10204 -- The Etype of the full view is inconsistent. Gigi needs to see the
10205 -- structural full view, which is what the current scheme gives:
10206 -- the Etype of the full view is the etype of the full base. However,
10207 -- if the full base is a derived type, the full view then looks like
10208 -- a subtype of the parent, not a subtype of the full base. If instead
10209 -- we write:
10211 -- Set_Etype (Full, Full_Base);
10213 -- then we get inconsistencies in the front-end (confusion between
10214 -- views). Several outstanding bugs are related to this ???
10216 Set_Is_First_Subtype (Full, False);
10217 Set_Scope (Full, Scope (Priv));
10218 Set_Size_Info (Full, Full_Base);
10219 Set_RM_Size (Full, RM_Size (Full_Base));
10220 Set_Is_Itype (Full);
10222 -- A subtype of a private-type-without-discriminants, whose full-view
10223 -- has discriminants with default expressions, is not constrained!
10225 if not Has_Discriminants (Priv) then
10226 Set_Is_Constrained (Full, Is_Constrained (Full_Base));
10228 if Has_Discriminants (Full_Base) then
10229 Set_Discriminant_Constraint
10230 (Full, Discriminant_Constraint (Full_Base));
10232 -- The partial view may have been indefinite, the full view
10233 -- might not be.
10235 Set_Has_Unknown_Discriminants
10236 (Full, Has_Unknown_Discriminants (Full_Base));
10237 end if;
10238 end if;
10240 Set_First_Rep_Item (Full, First_Rep_Item (Full_Base));
10241 Set_Depends_On_Private (Full, Has_Private_Component (Full));
10243 -- Freeze the private subtype entity if its parent is delayed, and not
10244 -- already frozen. We skip this processing if the type is an anonymous
10245 -- subtype of a record component, or is the corresponding record of a
10246 -- protected type, since ???
10248 if not Is_Type (Scope (Full)) then
10249 Set_Has_Delayed_Freeze (Full,
10250 Has_Delayed_Freeze (Full_Base)
10251 and then (not Is_Frozen (Full_Base)));
10252 end if;
10254 Set_Freeze_Node (Full, Empty);
10255 Set_Is_Frozen (Full, False);
10256 Set_Full_View (Priv, Full);
10258 if Has_Discriminants (Full) then
10259 Set_Stored_Constraint_From_Discriminant_Constraint (Full);
10260 Set_Stored_Constraint (Priv, Stored_Constraint (Full));
10262 if Has_Unknown_Discriminants (Full) then
10263 Set_Discriminant_Constraint (Full, No_Elist);
10264 end if;
10265 end if;
10267 if Ekind (Full_Base) = E_Record_Type
10268 and then Has_Discriminants (Full_Base)
10269 and then Has_Discriminants (Priv) -- might not, if errors
10270 and then not Has_Unknown_Discriminants (Priv)
10271 and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
10272 then
10273 Create_Constrained_Components
10274 (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
10276 -- If the full base is itself derived from private, build a congruent
10277 -- subtype of its underlying type, for use by the back end. For a
10278 -- constrained record component, the declaration cannot be placed on
10279 -- the component list, but it must nevertheless be built an analyzed, to
10280 -- supply enough information for Gigi to compute the size of component.
10282 elsif Ekind (Full_Base) in Private_Kind
10283 and then Is_Derived_Type (Full_Base)
10284 and then Has_Discriminants (Full_Base)
10285 and then (Ekind (Current_Scope) /= E_Record_Subtype)
10286 then
10287 if not Is_Itype (Priv)
10288 and then
10289 Nkind (Subtype_Indication (Parent (Priv))) = N_Subtype_Indication
10290 then
10291 Build_Underlying_Full_View
10292 (Parent (Priv), Full, Etype (Full_Base));
10294 elsif Nkind (Related_Nod) = N_Component_Declaration then
10295 Build_Underlying_Full_View (Related_Nod, Full, Etype (Full_Base));
10296 end if;
10298 elsif Is_Record_Type (Full_Base) then
10300 -- Show Full is simply a renaming of Full_Base
10302 Set_Cloned_Subtype (Full, Full_Base);
10303 end if;
10305 -- It is unsafe to share to bounds of a scalar type, because the Itype
10306 -- is elaborated on demand, and if a bound is non-static then different
10307 -- orders of elaboration in different units will lead to different
10308 -- external symbols.
10310 if Is_Scalar_Type (Full_Base) then
10311 Set_Scalar_Range (Full,
10312 Make_Range (Sloc (Related_Nod),
10313 Low_Bound =>
10314 Duplicate_Subexpr_No_Checks (Type_Low_Bound (Full_Base)),
10315 High_Bound =>
10316 Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
10318 -- This completion inherits the bounds of the full parent, but if
10319 -- the parent is an unconstrained floating point type, so is the
10320 -- completion.
10322 if Is_Floating_Point_Type (Full_Base) then
10323 Set_Includes_Infinities
10324 (Scalar_Range (Full), Has_Infinities (Full_Base));
10325 end if;
10326 end if;
10328 -- ??? It seems that a lot of fields are missing that should be copied
10329 -- from Full_Base to Full. Here are some that are introduced in a
10330 -- non-disruptive way but a cleanup is necessary.
10332 if Is_Tagged_Type (Full_Base) then
10333 Set_Is_Tagged_Type (Full);
10334 Set_Direct_Primitive_Operations (Full,
10335 Direct_Primitive_Operations (Full_Base));
10337 -- Inherit class_wide type of full_base in case the partial view was
10338 -- not tagged. Otherwise it has already been created when the private
10339 -- subtype was analyzed.
10341 if No (Class_Wide_Type (Full)) then
10342 Set_Class_Wide_Type (Full, Class_Wide_Type (Full_Base));
10343 end if;
10345 -- If this is a subtype of a protected or task type, constrain its
10346 -- corresponding record, unless this is a subtype without constraints,
10347 -- i.e. a simple renaming as with an actual subtype in an instance.
10349 elsif Is_Concurrent_Type (Full_Base) then
10350 if Has_Discriminants (Full)
10351 and then Present (Corresponding_Record_Type (Full_Base))
10352 and then
10353 not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
10354 then
10355 Set_Corresponding_Record_Type (Full,
10356 Constrain_Corresponding_Record
10357 (Full, Corresponding_Record_Type (Full_Base),
10358 Related_Nod, Full_Base));
10360 else
10361 Set_Corresponding_Record_Type (Full,
10362 Corresponding_Record_Type (Full_Base));
10363 end if;
10364 end if;
10366 -- Link rep item chain, and also setting of Has_Predicates from private
10367 -- subtype to full subtype, since we will need these on the full subtype
10368 -- to create the predicate function. Note that the full subtype may
10369 -- already have rep items, inherited from the full view of the base
10370 -- type, so we must be sure not to overwrite these entries.
10372 declare
10373 Append : Boolean;
10374 Item : Node_Id;
10375 Next_Item : Node_Id;
10377 begin
10378 Item := First_Rep_Item (Full);
10380 -- If no existing rep items on full type, we can just link directly
10381 -- to the list of items on the private type.
10383 if No (Item) then
10384 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
10386 -- Otherwise, search to the end of items currently linked to the full
10387 -- subtype and append the private items to the end. However, if Priv
10388 -- and Full already have the same list of rep items, then the append
10389 -- is not done, as that would create a circularity.
10391 elsif Item /= First_Rep_Item (Priv) then
10392 Append := True;
10394 loop
10395 Next_Item := Next_Rep_Item (Item);
10396 exit when No (Next_Item);
10397 Item := Next_Item;
10399 -- If the private view has aspect specifications, the full view
10400 -- inherits them. Since these aspects may already have been
10401 -- attached to the full view during derivation, do not append
10402 -- them if already present.
10404 if Item = First_Rep_Item (Priv) then
10405 Append := False;
10406 exit;
10407 end if;
10408 end loop;
10410 -- And link the private type items at the end of the chain
10412 if Append then
10413 Set_Next_Rep_Item (Item, First_Rep_Item (Priv));
10414 end if;
10415 end if;
10416 end;
10418 -- Make sure Has_Predicates is set on full type if it is set on the
10419 -- private type. Note that it may already be set on the full type and
10420 -- if so, we don't want to unset it.
10422 if Has_Predicates (Priv) then
10423 Set_Has_Predicates (Full);
10424 end if;
10425 end Complete_Private_Subtype;
10427 ----------------------------
10428 -- Constant_Redeclaration --
10429 ----------------------------
10431 procedure Constant_Redeclaration
10432 (Id : Entity_Id;
10433 N : Node_Id;
10434 T : out Entity_Id)
10436 Prev : constant Entity_Id := Current_Entity_In_Scope (Id);
10437 Obj_Def : constant Node_Id := Object_Definition (N);
10438 New_T : Entity_Id;
10440 procedure Check_Possible_Deferred_Completion
10441 (Prev_Id : Entity_Id;
10442 Prev_Obj_Def : Node_Id;
10443 Curr_Obj_Def : Node_Id);
10444 -- Determine whether the two object definitions describe the partial
10445 -- and the full view of a constrained deferred constant. Generate
10446 -- a subtype for the full view and verify that it statically matches
10447 -- the subtype of the partial view.
10449 procedure Check_Recursive_Declaration (Typ : Entity_Id);
10450 -- If deferred constant is an access type initialized with an allocator,
10451 -- check whether there is an illegal recursion in the definition,
10452 -- through a default value of some record subcomponent. This is normally
10453 -- detected when generating init procs, but requires this additional
10454 -- mechanism when expansion is disabled.
10456 ----------------------------------------
10457 -- Check_Possible_Deferred_Completion --
10458 ----------------------------------------
10460 procedure Check_Possible_Deferred_Completion
10461 (Prev_Id : Entity_Id;
10462 Prev_Obj_Def : Node_Id;
10463 Curr_Obj_Def : Node_Id)
10465 begin
10466 if Nkind (Prev_Obj_Def) = N_Subtype_Indication
10467 and then Present (Constraint (Prev_Obj_Def))
10468 and then Nkind (Curr_Obj_Def) = N_Subtype_Indication
10469 and then Present (Constraint (Curr_Obj_Def))
10470 then
10471 declare
10472 Loc : constant Source_Ptr := Sloc (N);
10473 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
10474 Decl : constant Node_Id :=
10475 Make_Subtype_Declaration (Loc,
10476 Defining_Identifier => Def_Id,
10477 Subtype_Indication =>
10478 Relocate_Node (Curr_Obj_Def));
10480 begin
10481 Insert_Before_And_Analyze (N, Decl);
10482 Set_Etype (Id, Def_Id);
10484 if not Subtypes_Statically_Match (Etype (Prev_Id), Def_Id) then
10485 Error_Msg_Sloc := Sloc (Prev_Id);
10486 Error_Msg_N ("subtype does not statically match deferred " &
10487 "declaration#", N);
10488 end if;
10489 end;
10490 end if;
10491 end Check_Possible_Deferred_Completion;
10493 ---------------------------------
10494 -- Check_Recursive_Declaration --
10495 ---------------------------------
10497 procedure Check_Recursive_Declaration (Typ : Entity_Id) is
10498 Comp : Entity_Id;
10500 begin
10501 if Is_Record_Type (Typ) then
10502 Comp := First_Component (Typ);
10503 while Present (Comp) loop
10504 if Comes_From_Source (Comp) then
10505 if Present (Expression (Parent (Comp)))
10506 and then Is_Entity_Name (Expression (Parent (Comp)))
10507 and then Entity (Expression (Parent (Comp))) = Prev
10508 then
10509 Error_Msg_Sloc := Sloc (Parent (Comp));
10510 Error_Msg_NE
10511 ("illegal circularity with declaration for&#",
10512 N, Comp);
10513 return;
10515 elsif Is_Record_Type (Etype (Comp)) then
10516 Check_Recursive_Declaration (Etype (Comp));
10517 end if;
10518 end if;
10520 Next_Component (Comp);
10521 end loop;
10522 end if;
10523 end Check_Recursive_Declaration;
10525 -- Start of processing for Constant_Redeclaration
10527 begin
10528 if Nkind (Parent (Prev)) = N_Object_Declaration then
10529 if Nkind (Object_Definition
10530 (Parent (Prev))) = N_Subtype_Indication
10531 then
10532 -- Find type of new declaration. The constraints of the two
10533 -- views must match statically, but there is no point in
10534 -- creating an itype for the full view.
10536 if Nkind (Obj_Def) = N_Subtype_Indication then
10537 Find_Type (Subtype_Mark (Obj_Def));
10538 New_T := Entity (Subtype_Mark (Obj_Def));
10540 else
10541 Find_Type (Obj_Def);
10542 New_T := Entity (Obj_Def);
10543 end if;
10545 T := Etype (Prev);
10547 else
10548 -- The full view may impose a constraint, even if the partial
10549 -- view does not, so construct the subtype.
10551 New_T := Find_Type_Of_Object (Obj_Def, N);
10552 T := New_T;
10553 end if;
10555 else
10556 -- Current declaration is illegal, diagnosed below in Enter_Name
10558 T := Empty;
10559 New_T := Any_Type;
10560 end if;
10562 -- If previous full declaration or a renaming declaration exists, or if
10563 -- a homograph is present, let Enter_Name handle it, either with an
10564 -- error or with the removal of an overridden implicit subprogram.
10566 if Ekind (Prev) /= E_Constant
10567 or else Nkind (Parent (Prev)) = N_Object_Renaming_Declaration
10568 or else Present (Expression (Parent (Prev)))
10569 or else Present (Full_View (Prev))
10570 then
10571 Enter_Name (Id);
10573 -- Verify that types of both declarations match, or else that both types
10574 -- are anonymous access types whose designated subtypes statically match
10575 -- (as allowed in Ada 2005 by AI-385).
10577 elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
10578 and then
10579 (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
10580 or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
10581 or else Is_Access_Constant (Etype (New_T)) /=
10582 Is_Access_Constant (Etype (Prev))
10583 or else Can_Never_Be_Null (Etype (New_T)) /=
10584 Can_Never_Be_Null (Etype (Prev))
10585 or else Null_Exclusion_Present (Parent (Prev)) /=
10586 Null_Exclusion_Present (Parent (Id))
10587 or else not Subtypes_Statically_Match
10588 (Designated_Type (Etype (Prev)),
10589 Designated_Type (Etype (New_T))))
10590 then
10591 Error_Msg_Sloc := Sloc (Prev);
10592 Error_Msg_N ("type does not match declaration#", N);
10593 Set_Full_View (Prev, Id);
10594 Set_Etype (Id, Any_Type);
10596 elsif
10597 Null_Exclusion_Present (Parent (Prev))
10598 and then not Null_Exclusion_Present (N)
10599 then
10600 Error_Msg_Sloc := Sloc (Prev);
10601 Error_Msg_N ("null-exclusion does not match declaration#", N);
10602 Set_Full_View (Prev, Id);
10603 Set_Etype (Id, Any_Type);
10605 -- If so, process the full constant declaration
10607 else
10608 -- RM 7.4 (6): If the subtype defined by the subtype_indication in
10609 -- the deferred declaration is constrained, then the subtype defined
10610 -- by the subtype_indication in the full declaration shall match it
10611 -- statically.
10613 Check_Possible_Deferred_Completion
10614 (Prev_Id => Prev,
10615 Prev_Obj_Def => Object_Definition (Parent (Prev)),
10616 Curr_Obj_Def => Obj_Def);
10618 Set_Full_View (Prev, Id);
10619 Set_Is_Public (Id, Is_Public (Prev));
10620 Set_Is_Internal (Id);
10621 Append_Entity (Id, Current_Scope);
10623 -- Check ALIASED present if present before (RM 7.4(7))
10625 if Is_Aliased (Prev)
10626 and then not Aliased_Present (N)
10627 then
10628 Error_Msg_Sloc := Sloc (Prev);
10629 Error_Msg_N ("ALIASED required (see declaration#)", N);
10630 end if;
10632 -- Check that placement is in private part and that the incomplete
10633 -- declaration appeared in the visible part.
10635 if Ekind (Current_Scope) = E_Package
10636 and then not In_Private_Part (Current_Scope)
10637 then
10638 Error_Msg_Sloc := Sloc (Prev);
10639 Error_Msg_N
10640 ("full constant for declaration#"
10641 & " must be in private part", N);
10643 elsif Ekind (Current_Scope) = E_Package
10644 and then
10645 List_Containing (Parent (Prev)) /=
10646 Visible_Declarations
10647 (Specification (Unit_Declaration_Node (Current_Scope)))
10648 then
10649 Error_Msg_N
10650 ("deferred constant must be declared in visible part",
10651 Parent (Prev));
10652 end if;
10654 if Is_Access_Type (T)
10655 and then Nkind (Expression (N)) = N_Allocator
10656 then
10657 Check_Recursive_Declaration (Designated_Type (T));
10658 end if;
10659 end if;
10660 end Constant_Redeclaration;
10662 ----------------------
10663 -- Constrain_Access --
10664 ----------------------
10666 procedure Constrain_Access
10667 (Def_Id : in out Entity_Id;
10668 S : Node_Id;
10669 Related_Nod : Node_Id)
10671 T : constant Entity_Id := Entity (Subtype_Mark (S));
10672 Desig_Type : constant Entity_Id := Designated_Type (T);
10673 Desig_Subtype : Entity_Id := Create_Itype (E_Void, Related_Nod);
10674 Constraint_OK : Boolean := True;
10676 function Has_Defaulted_Discriminants (Typ : Entity_Id) return Boolean;
10677 -- Simple predicate to test for defaulted discriminants
10678 -- Shouldn't this be in sem_util???
10680 ---------------------------------
10681 -- Has_Defaulted_Discriminants --
10682 ---------------------------------
10684 function Has_Defaulted_Discriminants (Typ : Entity_Id) return Boolean is
10685 begin
10686 return Has_Discriminants (Typ)
10687 and then Present (First_Discriminant (Typ))
10688 and then Present
10689 (Discriminant_Default_Value (First_Discriminant (Typ)));
10690 end Has_Defaulted_Discriminants;
10692 -- Start of processing for Constrain_Access
10694 begin
10695 if Is_Array_Type (Desig_Type) then
10696 Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
10698 elsif (Is_Record_Type (Desig_Type)
10699 or else Is_Incomplete_Or_Private_Type (Desig_Type))
10700 and then not Is_Constrained (Desig_Type)
10701 then
10702 -- ??? The following code is a temporary kludge to ignore a
10703 -- discriminant constraint on access type if it is constraining
10704 -- the current record. Avoid creating the implicit subtype of the
10705 -- record we are currently compiling since right now, we cannot
10706 -- handle these. For now, just return the access type itself.
10708 if Desig_Type = Current_Scope
10709 and then No (Def_Id)
10710 then
10711 Set_Ekind (Desig_Subtype, E_Record_Subtype);
10712 Def_Id := Entity (Subtype_Mark (S));
10714 -- This call added to ensure that the constraint is analyzed
10715 -- (needed for a B test). Note that we still return early from
10716 -- this procedure to avoid recursive processing. ???
10718 Constrain_Discriminated_Type
10719 (Desig_Subtype, S, Related_Nod, For_Access => True);
10720 return;
10721 end if;
10723 -- Enforce rule that the constraint is illegal if there is an
10724 -- unconstrained view of the designated type. This means that the
10725 -- partial view (either a private type declaration or a derivation
10726 -- from a private type) has no discriminants. (Defect Report
10727 -- 8652/0008, Technical Corrigendum 1, checked by ACATS B371001).
10729 -- Rule updated for Ada 2005: the private type is said to have
10730 -- a constrained partial view, given that objects of the type
10731 -- can be declared. Furthermore, the rule applies to all access
10732 -- types, unlike the rule concerning default discriminants (see
10733 -- RM 3.7.1(7/3))
10735 if (Ekind (T) = E_General_Access_Type
10736 or else Ada_Version >= Ada_2005)
10737 and then Has_Private_Declaration (Desig_Type)
10738 and then In_Open_Scopes (Scope (Desig_Type))
10739 and then Has_Discriminants (Desig_Type)
10740 then
10741 declare
10742 Pack : constant Node_Id :=
10743 Unit_Declaration_Node (Scope (Desig_Type));
10744 Decls : List_Id;
10745 Decl : Node_Id;
10747 begin
10748 if Nkind (Pack) = N_Package_Declaration then
10749 Decls := Visible_Declarations (Specification (Pack));
10750 Decl := First (Decls);
10751 while Present (Decl) loop
10752 if (Nkind (Decl) = N_Private_Type_Declaration
10753 and then
10754 Chars (Defining_Identifier (Decl)) =
10755 Chars (Desig_Type))
10757 or else
10758 (Nkind (Decl) = N_Full_Type_Declaration
10759 and then
10760 Chars (Defining_Identifier (Decl)) =
10761 Chars (Desig_Type)
10762 and then Is_Derived_Type (Desig_Type)
10763 and then
10764 Has_Private_Declaration (Etype (Desig_Type)))
10765 then
10766 if No (Discriminant_Specifications (Decl)) then
10767 Error_Msg_N
10768 ("cannot constrain access type if designated " &
10769 "type has constrained partial view", S);
10770 end if;
10772 exit;
10773 end if;
10775 Next (Decl);
10776 end loop;
10777 end if;
10778 end;
10779 end if;
10781 Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
10782 For_Access => True);
10784 elsif (Is_Task_Type (Desig_Type)
10785 or else Is_Protected_Type (Desig_Type))
10786 and then not Is_Constrained (Desig_Type)
10787 then
10788 Constrain_Concurrent
10789 (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
10791 else
10792 Error_Msg_N ("invalid constraint on access type", S);
10793 Desig_Subtype := Desig_Type; -- Ignore invalid constraint.
10794 Constraint_OK := False;
10795 end if;
10797 if No (Def_Id) then
10798 Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
10799 else
10800 Set_Ekind (Def_Id, E_Access_Subtype);
10801 end if;
10803 if Constraint_OK then
10804 Set_Etype (Def_Id, Base_Type (T));
10806 if Is_Private_Type (Desig_Type) then
10807 Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
10808 end if;
10809 else
10810 Set_Etype (Def_Id, Any_Type);
10811 end if;
10813 Set_Size_Info (Def_Id, T);
10814 Set_Is_Constrained (Def_Id, Constraint_OK);
10815 Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
10816 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
10817 Set_Is_Access_Constant (Def_Id, Is_Access_Constant (T));
10819 Conditional_Delay (Def_Id, T);
10821 -- AI-363 : Subtypes of general access types whose designated types have
10822 -- default discriminants are disallowed. In instances, the rule has to
10823 -- be checked against the actual, of which T is the subtype. In a
10824 -- generic body, the rule is checked assuming that the actual type has
10825 -- defaulted discriminants.
10827 if Ada_Version >= Ada_2005 or else Warn_On_Ada_2005_Compatibility then
10828 if Ekind (Base_Type (T)) = E_General_Access_Type
10829 and then Has_Defaulted_Discriminants (Desig_Type)
10830 then
10831 if Ada_Version < Ada_2005 then
10832 Error_Msg_N
10833 ("access subtype of general access type would not " &
10834 "be allowed in Ada 2005?", S);
10835 else
10836 Error_Msg_N
10837 ("access subtype of general access type not allowed", S);
10838 end if;
10840 Error_Msg_N ("\discriminants have defaults", S);
10842 elsif Is_Access_Type (T)
10843 and then Is_Generic_Type (Desig_Type)
10844 and then Has_Discriminants (Desig_Type)
10845 and then In_Package_Body (Current_Scope)
10846 then
10847 if Ada_Version < Ada_2005 then
10848 Error_Msg_N
10849 ("access subtype would not be allowed in generic body " &
10850 "in Ada 2005?", S);
10851 else
10852 Error_Msg_N
10853 ("access subtype not allowed in generic body", S);
10854 end if;
10856 Error_Msg_N
10857 ("\designated type is a discriminated formal", S);
10858 end if;
10859 end if;
10860 end Constrain_Access;
10862 ---------------------
10863 -- Constrain_Array --
10864 ---------------------
10866 procedure Constrain_Array
10867 (Def_Id : in out Entity_Id;
10868 SI : Node_Id;
10869 Related_Nod : Node_Id;
10870 Related_Id : Entity_Id;
10871 Suffix : Character)
10873 C : constant Node_Id := Constraint (SI);
10874 Number_Of_Constraints : Nat := 0;
10875 Index : Node_Id;
10876 S, T : Entity_Id;
10877 Constraint_OK : Boolean := True;
10879 begin
10880 T := Entity (Subtype_Mark (SI));
10882 if Ekind (T) in Access_Kind then
10883 T := Designated_Type (T);
10884 end if;
10886 -- If an index constraint follows a subtype mark in a subtype indication
10887 -- then the type or subtype denoted by the subtype mark must not already
10888 -- impose an index constraint. The subtype mark must denote either an
10889 -- unconstrained array type or an access type whose designated type
10890 -- is such an array type... (RM 3.6.1)
10892 if Is_Constrained (T) then
10893 Error_Msg_N ("array type is already constrained", Subtype_Mark (SI));
10894 Constraint_OK := False;
10896 else
10897 S := First (Constraints (C));
10898 while Present (S) loop
10899 Number_Of_Constraints := Number_Of_Constraints + 1;
10900 Next (S);
10901 end loop;
10903 -- In either case, the index constraint must provide a discrete
10904 -- range for each index of the array type and the type of each
10905 -- discrete range must be the same as that of the corresponding
10906 -- index. (RM 3.6.1)
10908 if Number_Of_Constraints /= Number_Dimensions (T) then
10909 Error_Msg_NE ("incorrect number of index constraints for }", C, T);
10910 Constraint_OK := False;
10912 else
10913 S := First (Constraints (C));
10914 Index := First_Index (T);
10915 Analyze (Index);
10917 -- Apply constraints to each index type
10919 for J in 1 .. Number_Of_Constraints loop
10920 Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
10921 Next (Index);
10922 Next (S);
10923 end loop;
10925 end if;
10926 end if;
10928 if No (Def_Id) then
10929 Def_Id :=
10930 Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
10931 Set_Parent (Def_Id, Related_Nod);
10933 else
10934 Set_Ekind (Def_Id, E_Array_Subtype);
10935 end if;
10937 Set_Size_Info (Def_Id, (T));
10938 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10939 Set_Etype (Def_Id, Base_Type (T));
10941 if Constraint_OK then
10942 Set_First_Index (Def_Id, First (Constraints (C)));
10943 else
10944 Set_First_Index (Def_Id, First_Index (T));
10945 end if;
10947 Set_Is_Constrained (Def_Id, True);
10948 Set_Is_Aliased (Def_Id, Is_Aliased (T));
10949 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
10951 Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
10952 Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
10954 -- A subtype does not inherit the packed_array_type of is parent. We
10955 -- need to initialize the attribute because if Def_Id is previously
10956 -- analyzed through a limited_with clause, it will have the attributes
10957 -- of an incomplete type, one of which is an Elist that overlaps the
10958 -- Packed_Array_Type field.
10960 Set_Packed_Array_Type (Def_Id, Empty);
10962 -- Build a freeze node if parent still needs one. Also make sure that
10963 -- the Depends_On_Private status is set because the subtype will need
10964 -- reprocessing at the time the base type does, and also we must set a
10965 -- conditional delay.
10967 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
10968 Conditional_Delay (Def_Id, T);
10969 end Constrain_Array;
10971 ------------------------------
10972 -- Constrain_Component_Type --
10973 ------------------------------
10975 function Constrain_Component_Type
10976 (Comp : Entity_Id;
10977 Constrained_Typ : Entity_Id;
10978 Related_Node : Node_Id;
10979 Typ : Entity_Id;
10980 Constraints : Elist_Id) return Entity_Id
10982 Loc : constant Source_Ptr := Sloc (Constrained_Typ);
10983 Compon_Type : constant Entity_Id := Etype (Comp);
10985 function Build_Constrained_Array_Type
10986 (Old_Type : Entity_Id) return Entity_Id;
10987 -- If Old_Type is an array type, one of whose indexes is constrained
10988 -- by a discriminant, build an Itype whose constraint replaces the
10989 -- discriminant with its value in the constraint.
10991 function Build_Constrained_Discriminated_Type
10992 (Old_Type : Entity_Id) return Entity_Id;
10993 -- Ditto for record components
10995 function Build_Constrained_Access_Type
10996 (Old_Type : Entity_Id) return Entity_Id;
10997 -- Ditto for access types. Makes use of previous two functions, to
10998 -- constrain designated type.
11000 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id;
11001 -- T is an array or discriminated type, C is a list of constraints
11002 -- that apply to T. This routine builds the constrained subtype.
11004 function Is_Discriminant (Expr : Node_Id) return Boolean;
11005 -- Returns True if Expr is a discriminant
11007 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id;
11008 -- Find the value of discriminant Discrim in Constraint
11010 -----------------------------------
11011 -- Build_Constrained_Access_Type --
11012 -----------------------------------
11014 function Build_Constrained_Access_Type
11015 (Old_Type : Entity_Id) return Entity_Id
11017 Desig_Type : constant Entity_Id := Designated_Type (Old_Type);
11018 Itype : Entity_Id;
11019 Desig_Subtype : Entity_Id;
11020 Scop : Entity_Id;
11022 begin
11023 -- if the original access type was not embedded in the enclosing
11024 -- type definition, there is no need to produce a new access
11025 -- subtype. In fact every access type with an explicit constraint
11026 -- generates an itype whose scope is the enclosing record.
11028 if not Is_Type (Scope (Old_Type)) then
11029 return Old_Type;
11031 elsif Is_Array_Type (Desig_Type) then
11032 Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
11034 elsif Has_Discriminants (Desig_Type) then
11036 -- This may be an access type to an enclosing record type for
11037 -- which we are constructing the constrained components. Return
11038 -- the enclosing record subtype. This is not always correct,
11039 -- but avoids infinite recursion. ???
11041 Desig_Subtype := Any_Type;
11043 for J in reverse 0 .. Scope_Stack.Last loop
11044 Scop := Scope_Stack.Table (J).Entity;
11046 if Is_Type (Scop)
11047 and then Base_Type (Scop) = Base_Type (Desig_Type)
11048 then
11049 Desig_Subtype := Scop;
11050 end if;
11052 exit when not Is_Type (Scop);
11053 end loop;
11055 if Desig_Subtype = Any_Type then
11056 Desig_Subtype :=
11057 Build_Constrained_Discriminated_Type (Desig_Type);
11058 end if;
11060 else
11061 return Old_Type;
11062 end if;
11064 if Desig_Subtype /= Desig_Type then
11066 -- The Related_Node better be here or else we won't be able
11067 -- to attach new itypes to a node in the tree.
11069 pragma Assert (Present (Related_Node));
11071 Itype := Create_Itype (E_Access_Subtype, Related_Node);
11073 Set_Etype (Itype, Base_Type (Old_Type));
11074 Set_Size_Info (Itype, (Old_Type));
11075 Set_Directly_Designated_Type (Itype, Desig_Subtype);
11076 Set_Depends_On_Private (Itype, Has_Private_Component
11077 (Old_Type));
11078 Set_Is_Access_Constant (Itype, Is_Access_Constant
11079 (Old_Type));
11081 -- The new itype needs freezing when it depends on a not frozen
11082 -- type and the enclosing subtype needs freezing.
11084 if Has_Delayed_Freeze (Constrained_Typ)
11085 and then not Is_Frozen (Constrained_Typ)
11086 then
11087 Conditional_Delay (Itype, Base_Type (Old_Type));
11088 end if;
11090 return Itype;
11092 else
11093 return Old_Type;
11094 end if;
11095 end Build_Constrained_Access_Type;
11097 ----------------------------------
11098 -- Build_Constrained_Array_Type --
11099 ----------------------------------
11101 function Build_Constrained_Array_Type
11102 (Old_Type : Entity_Id) return Entity_Id
11104 Lo_Expr : Node_Id;
11105 Hi_Expr : Node_Id;
11106 Old_Index : Node_Id;
11107 Range_Node : Node_Id;
11108 Constr_List : List_Id;
11110 Need_To_Create_Itype : Boolean := False;
11112 begin
11113 Old_Index := First_Index (Old_Type);
11114 while Present (Old_Index) loop
11115 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
11117 if Is_Discriminant (Lo_Expr)
11118 or else Is_Discriminant (Hi_Expr)
11119 then
11120 Need_To_Create_Itype := True;
11121 end if;
11123 Next_Index (Old_Index);
11124 end loop;
11126 if Need_To_Create_Itype then
11127 Constr_List := New_List;
11129 Old_Index := First_Index (Old_Type);
11130 while Present (Old_Index) loop
11131 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
11133 if Is_Discriminant (Lo_Expr) then
11134 Lo_Expr := Get_Discr_Value (Lo_Expr);
11135 end if;
11137 if Is_Discriminant (Hi_Expr) then
11138 Hi_Expr := Get_Discr_Value (Hi_Expr);
11139 end if;
11141 Range_Node :=
11142 Make_Range
11143 (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
11145 Append (Range_Node, To => Constr_List);
11147 Next_Index (Old_Index);
11148 end loop;
11150 return Build_Subtype (Old_Type, Constr_List);
11152 else
11153 return Old_Type;
11154 end if;
11155 end Build_Constrained_Array_Type;
11157 ------------------------------------------
11158 -- Build_Constrained_Discriminated_Type --
11159 ------------------------------------------
11161 function Build_Constrained_Discriminated_Type
11162 (Old_Type : Entity_Id) return Entity_Id
11164 Expr : Node_Id;
11165 Constr_List : List_Id;
11166 Old_Constraint : Elmt_Id;
11168 Need_To_Create_Itype : Boolean := False;
11170 begin
11171 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
11172 while Present (Old_Constraint) loop
11173 Expr := Node (Old_Constraint);
11175 if Is_Discriminant (Expr) then
11176 Need_To_Create_Itype := True;
11177 end if;
11179 Next_Elmt (Old_Constraint);
11180 end loop;
11182 if Need_To_Create_Itype then
11183 Constr_List := New_List;
11185 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
11186 while Present (Old_Constraint) loop
11187 Expr := Node (Old_Constraint);
11189 if Is_Discriminant (Expr) then
11190 Expr := Get_Discr_Value (Expr);
11191 end if;
11193 Append (New_Copy_Tree (Expr), To => Constr_List);
11195 Next_Elmt (Old_Constraint);
11196 end loop;
11198 return Build_Subtype (Old_Type, Constr_List);
11200 else
11201 return Old_Type;
11202 end if;
11203 end Build_Constrained_Discriminated_Type;
11205 -------------------
11206 -- Build_Subtype --
11207 -------------------
11209 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id is
11210 Indic : Node_Id;
11211 Subtyp_Decl : Node_Id;
11212 Def_Id : Entity_Id;
11213 Btyp : Entity_Id := Base_Type (T);
11215 begin
11216 -- The Related_Node better be here or else we won't be able to
11217 -- attach new itypes to a node in the tree.
11219 pragma Assert (Present (Related_Node));
11221 -- If the view of the component's type is incomplete or private
11222 -- with unknown discriminants, then the constraint must be applied
11223 -- to the full type.
11225 if Has_Unknown_Discriminants (Btyp)
11226 and then Present (Underlying_Type (Btyp))
11227 then
11228 Btyp := Underlying_Type (Btyp);
11229 end if;
11231 Indic :=
11232 Make_Subtype_Indication (Loc,
11233 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
11234 Constraint => Make_Index_Or_Discriminant_Constraint (Loc, C));
11236 Def_Id := Create_Itype (Ekind (T), Related_Node);
11238 Subtyp_Decl :=
11239 Make_Subtype_Declaration (Loc,
11240 Defining_Identifier => Def_Id,
11241 Subtype_Indication => Indic);
11243 Set_Parent (Subtyp_Decl, Parent (Related_Node));
11245 -- Itypes must be analyzed with checks off (see package Itypes)
11247 Analyze (Subtyp_Decl, Suppress => All_Checks);
11249 return Def_Id;
11250 end Build_Subtype;
11252 ---------------------
11253 -- Get_Discr_Value --
11254 ---------------------
11256 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id is
11257 D : Entity_Id;
11258 E : Elmt_Id;
11260 begin
11261 -- The discriminant may be declared for the type, in which case we
11262 -- find it by iterating over the list of discriminants. If the
11263 -- discriminant is inherited from a parent type, it appears as the
11264 -- corresponding discriminant of the current type. This will be the
11265 -- case when constraining an inherited component whose constraint is
11266 -- given by a discriminant of the parent.
11268 D := First_Discriminant (Typ);
11269 E := First_Elmt (Constraints);
11271 while Present (D) loop
11272 if D = Entity (Discrim)
11273 or else D = CR_Discriminant (Entity (Discrim))
11274 or else Corresponding_Discriminant (D) = Entity (Discrim)
11275 then
11276 return Node (E);
11277 end if;
11279 Next_Discriminant (D);
11280 Next_Elmt (E);
11281 end loop;
11283 -- The Corresponding_Discriminant mechanism is incomplete, because
11284 -- the correspondence between new and old discriminants is not one
11285 -- to one: one new discriminant can constrain several old ones. In
11286 -- that case, scan sequentially the stored_constraint, the list of
11287 -- discriminants of the parents, and the constraints.
11288 -- Previous code checked for the present of the Stored_Constraint
11289 -- list for the derived type, but did not use it at all. Should it
11290 -- be present when the component is a discriminated task type?
11292 if Is_Derived_Type (Typ)
11293 and then Scope (Entity (Discrim)) = Etype (Typ)
11294 then
11295 D := First_Discriminant (Etype (Typ));
11296 E := First_Elmt (Constraints);
11297 while Present (D) loop
11298 if D = Entity (Discrim) then
11299 return Node (E);
11300 end if;
11302 Next_Discriminant (D);
11303 Next_Elmt (E);
11304 end loop;
11305 end if;
11307 -- Something is wrong if we did not find the value
11309 raise Program_Error;
11310 end Get_Discr_Value;
11312 ---------------------
11313 -- Is_Discriminant --
11314 ---------------------
11316 function Is_Discriminant (Expr : Node_Id) return Boolean is
11317 Discrim_Scope : Entity_Id;
11319 begin
11320 if Denotes_Discriminant (Expr) then
11321 Discrim_Scope := Scope (Entity (Expr));
11323 -- Either we have a reference to one of Typ's discriminants,
11325 pragma Assert (Discrim_Scope = Typ
11327 -- or to the discriminants of the parent type, in the case
11328 -- of a derivation of a tagged type with variants.
11330 or else Discrim_Scope = Etype (Typ)
11331 or else Full_View (Discrim_Scope) = Etype (Typ)
11333 -- or same as above for the case where the discriminants
11334 -- were declared in Typ's private view.
11336 or else (Is_Private_Type (Discrim_Scope)
11337 and then Chars (Discrim_Scope) = Chars (Typ))
11339 -- or else we are deriving from the full view and the
11340 -- discriminant is declared in the private entity.
11342 or else (Is_Private_Type (Typ)
11343 and then Chars (Discrim_Scope) = Chars (Typ))
11345 -- Or we are constrained the corresponding record of a
11346 -- synchronized type that completes a private declaration.
11348 or else (Is_Concurrent_Record_Type (Typ)
11349 and then
11350 Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
11352 -- or we have a class-wide type, in which case make sure the
11353 -- discriminant found belongs to the root type.
11355 or else (Is_Class_Wide_Type (Typ)
11356 and then Etype (Typ) = Discrim_Scope));
11358 return True;
11359 end if;
11361 -- In all other cases we have something wrong
11363 return False;
11364 end Is_Discriminant;
11366 -- Start of processing for Constrain_Component_Type
11368 begin
11369 if Nkind (Parent (Comp)) = N_Component_Declaration
11370 and then Comes_From_Source (Parent (Comp))
11371 and then Comes_From_Source
11372 (Subtype_Indication (Component_Definition (Parent (Comp))))
11373 and then
11374 Is_Entity_Name
11375 (Subtype_Indication (Component_Definition (Parent (Comp))))
11376 then
11377 return Compon_Type;
11379 elsif Is_Array_Type (Compon_Type) then
11380 return Build_Constrained_Array_Type (Compon_Type);
11382 elsif Has_Discriminants (Compon_Type) then
11383 return Build_Constrained_Discriminated_Type (Compon_Type);
11385 elsif Is_Access_Type (Compon_Type) then
11386 return Build_Constrained_Access_Type (Compon_Type);
11388 else
11389 return Compon_Type;
11390 end if;
11391 end Constrain_Component_Type;
11393 --------------------------
11394 -- Constrain_Concurrent --
11395 --------------------------
11397 -- For concurrent types, the associated record value type carries the same
11398 -- discriminants, so when we constrain a concurrent type, we must constrain
11399 -- the corresponding record type as well.
11401 procedure Constrain_Concurrent
11402 (Def_Id : in out Entity_Id;
11403 SI : Node_Id;
11404 Related_Nod : Node_Id;
11405 Related_Id : Entity_Id;
11406 Suffix : Character)
11408 -- Retrieve Base_Type to ensure getting to the concurrent type in the
11409 -- case of a private subtype (needed when only doing semantic analysis).
11411 T_Ent : Entity_Id := Base_Type (Entity (Subtype_Mark (SI)));
11412 T_Val : Entity_Id;
11414 begin
11415 if Ekind (T_Ent) in Access_Kind then
11416 T_Ent := Designated_Type (T_Ent);
11417 end if;
11419 T_Val := Corresponding_Record_Type (T_Ent);
11421 if Present (T_Val) then
11423 if No (Def_Id) then
11424 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
11425 end if;
11427 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
11429 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
11430 Set_Corresponding_Record_Type (Def_Id,
11431 Constrain_Corresponding_Record
11432 (Def_Id, T_Val, Related_Nod, Related_Id));
11434 else
11435 -- If there is no associated record, expansion is disabled and this
11436 -- is a generic context. Create a subtype in any case, so that
11437 -- semantic analysis can proceed.
11439 if No (Def_Id) then
11440 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
11441 end if;
11443 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
11444 end if;
11445 end Constrain_Concurrent;
11447 ------------------------------------
11448 -- Constrain_Corresponding_Record --
11449 ------------------------------------
11451 function Constrain_Corresponding_Record
11452 (Prot_Subt : Entity_Id;
11453 Corr_Rec : Entity_Id;
11454 Related_Nod : Node_Id;
11455 Related_Id : Entity_Id) return Entity_Id
11457 T_Sub : constant Entity_Id :=
11458 Create_Itype (E_Record_Subtype, Related_Nod, Related_Id, 'V');
11460 begin
11461 Set_Etype (T_Sub, Corr_Rec);
11462 Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
11463 Set_Is_Constrained (T_Sub, True);
11464 Set_First_Entity (T_Sub, First_Entity (Corr_Rec));
11465 Set_Last_Entity (T_Sub, Last_Entity (Corr_Rec));
11467 -- As elsewhere, we do not want to create a freeze node for this itype
11468 -- if it is created for a constrained component of an enclosing record
11469 -- because references to outer discriminants will appear out of scope.
11471 if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
11472 Conditional_Delay (T_Sub, Corr_Rec);
11473 else
11474 Set_Is_Frozen (T_Sub);
11475 end if;
11477 if Has_Discriminants (Prot_Subt) then -- False only if errors.
11478 Set_Discriminant_Constraint
11479 (T_Sub, Discriminant_Constraint (Prot_Subt));
11480 Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
11481 Create_Constrained_Components
11482 (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
11483 end if;
11485 Set_Depends_On_Private (T_Sub, Has_Private_Component (T_Sub));
11487 return T_Sub;
11488 end Constrain_Corresponding_Record;
11490 -----------------------
11491 -- Constrain_Decimal --
11492 -----------------------
11494 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id) is
11495 T : constant Entity_Id := Entity (Subtype_Mark (S));
11496 C : constant Node_Id := Constraint (S);
11497 Loc : constant Source_Ptr := Sloc (C);
11498 Range_Expr : Node_Id;
11499 Digits_Expr : Node_Id;
11500 Digits_Val : Uint;
11501 Bound_Val : Ureal;
11503 begin
11504 Set_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
11506 if Nkind (C) = N_Range_Constraint then
11507 Range_Expr := Range_Expression (C);
11508 Digits_Val := Digits_Value (T);
11510 else
11511 pragma Assert (Nkind (C) = N_Digits_Constraint);
11513 Check_SPARK_Restriction ("digits constraint is not allowed", S);
11515 Digits_Expr := Digits_Expression (C);
11516 Analyze_And_Resolve (Digits_Expr, Any_Integer);
11518 Check_Digits_Expression (Digits_Expr);
11519 Digits_Val := Expr_Value (Digits_Expr);
11521 if Digits_Val > Digits_Value (T) then
11522 Error_Msg_N
11523 ("digits expression is incompatible with subtype", C);
11524 Digits_Val := Digits_Value (T);
11525 end if;
11527 if Present (Range_Constraint (C)) then
11528 Range_Expr := Range_Expression (Range_Constraint (C));
11529 else
11530 Range_Expr := Empty;
11531 end if;
11532 end if;
11534 Set_Etype (Def_Id, Base_Type (T));
11535 Set_Size_Info (Def_Id, (T));
11536 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
11537 Set_Delta_Value (Def_Id, Delta_Value (T));
11538 Set_Scale_Value (Def_Id, Scale_Value (T));
11539 Set_Small_Value (Def_Id, Small_Value (T));
11540 Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
11541 Set_Digits_Value (Def_Id, Digits_Val);
11543 -- Manufacture range from given digits value if no range present
11545 if No (Range_Expr) then
11546 Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
11547 Range_Expr :=
11548 Make_Range (Loc,
11549 Low_Bound =>
11550 Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
11551 High_Bound =>
11552 Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
11553 end if;
11555 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
11556 Set_Discrete_RM_Size (Def_Id);
11558 -- Unconditionally delay the freeze, since we cannot set size
11559 -- information in all cases correctly until the freeze point.
11561 Set_Has_Delayed_Freeze (Def_Id);
11562 end Constrain_Decimal;
11564 ----------------------------------
11565 -- Constrain_Discriminated_Type --
11566 ----------------------------------
11568 procedure Constrain_Discriminated_Type
11569 (Def_Id : Entity_Id;
11570 S : Node_Id;
11571 Related_Nod : Node_Id;
11572 For_Access : Boolean := False)
11574 E : constant Entity_Id := Entity (Subtype_Mark (S));
11575 T : Entity_Id;
11576 C : Node_Id;
11577 Elist : Elist_Id := New_Elmt_List;
11579 procedure Fixup_Bad_Constraint;
11580 -- This is called after finding a bad constraint, and after having
11581 -- posted an appropriate error message. The mission is to leave the
11582 -- entity T in as reasonable state as possible!
11584 --------------------------
11585 -- Fixup_Bad_Constraint --
11586 --------------------------
11588 procedure Fixup_Bad_Constraint is
11589 begin
11590 -- Set a reasonable Ekind for the entity. For an incomplete type,
11591 -- we can't do much, but for other types, we can set the proper
11592 -- corresponding subtype kind.
11594 if Ekind (T) = E_Incomplete_Type then
11595 Set_Ekind (Def_Id, Ekind (T));
11596 else
11597 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
11598 end if;
11600 -- Set Etype to the known type, to reduce chances of cascaded errors
11602 Set_Etype (Def_Id, E);
11603 Set_Error_Posted (Def_Id);
11604 end Fixup_Bad_Constraint;
11606 -- Start of processing for Constrain_Discriminated_Type
11608 begin
11609 C := Constraint (S);
11611 -- A discriminant constraint is only allowed in a subtype indication,
11612 -- after a subtype mark. This subtype mark must denote either a type
11613 -- with discriminants, or an access type whose designated type is a
11614 -- type with discriminants. A discriminant constraint specifies the
11615 -- values of these discriminants (RM 3.7.2(5)).
11617 T := Base_Type (Entity (Subtype_Mark (S)));
11619 if Ekind (T) in Access_Kind then
11620 T := Designated_Type (T);
11621 end if;
11623 -- Ada 2005 (AI-412): Constrained incomplete subtypes are illegal.
11624 -- Avoid generating an error for access-to-incomplete subtypes.
11626 if Ada_Version >= Ada_2005
11627 and then Ekind (T) = E_Incomplete_Type
11628 and then Nkind (Parent (S)) = N_Subtype_Declaration
11629 and then not Is_Itype (Def_Id)
11630 then
11631 -- A little sanity check, emit an error message if the type
11632 -- has discriminants to begin with. Type T may be a regular
11633 -- incomplete type or imported via a limited with clause.
11635 if Has_Discriminants (T)
11636 or else
11637 (From_With_Type (T)
11638 and then Present (Non_Limited_View (T))
11639 and then Nkind (Parent (Non_Limited_View (T))) =
11640 N_Full_Type_Declaration
11641 and then Present (Discriminant_Specifications
11642 (Parent (Non_Limited_View (T)))))
11643 then
11644 Error_Msg_N
11645 ("(Ada 2005) incomplete subtype may not be constrained", C);
11646 else
11647 Error_Msg_N ("invalid constraint: type has no discriminant", C);
11648 end if;
11650 Fixup_Bad_Constraint;
11651 return;
11653 -- Check that the type has visible discriminants. The type may be
11654 -- a private type with unknown discriminants whose full view has
11655 -- discriminants which are invisible.
11657 elsif not Has_Discriminants (T)
11658 or else
11659 (Has_Unknown_Discriminants (T)
11660 and then Is_Private_Type (T))
11661 then
11662 Error_Msg_N ("invalid constraint: type has no discriminant", C);
11663 Fixup_Bad_Constraint;
11664 return;
11666 elsif Is_Constrained (E)
11667 or else (Ekind (E) = E_Class_Wide_Subtype
11668 and then Present (Discriminant_Constraint (E)))
11669 then
11670 Error_Msg_N ("type is already constrained", Subtype_Mark (S));
11671 Fixup_Bad_Constraint;
11672 return;
11673 end if;
11675 -- T may be an unconstrained subtype (e.g. a generic actual).
11676 -- Constraint applies to the base type.
11678 T := Base_Type (T);
11680 Elist := Build_Discriminant_Constraints (T, S);
11682 -- If the list returned was empty we had an error in building the
11683 -- discriminant constraint. We have also already signalled an error
11684 -- in the incomplete type case
11686 if Is_Empty_Elmt_List (Elist) then
11687 Fixup_Bad_Constraint;
11688 return;
11689 end if;
11691 Build_Discriminated_Subtype (T, Def_Id, Elist, Related_Nod, For_Access);
11692 end Constrain_Discriminated_Type;
11694 ---------------------------
11695 -- Constrain_Enumeration --
11696 ---------------------------
11698 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id) is
11699 T : constant Entity_Id := Entity (Subtype_Mark (S));
11700 C : constant Node_Id := Constraint (S);
11702 begin
11703 Set_Ekind (Def_Id, E_Enumeration_Subtype);
11705 Set_First_Literal (Def_Id, First_Literal (Base_Type (T)));
11707 Set_Etype (Def_Id, Base_Type (T));
11708 Set_Size_Info (Def_Id, (T));
11709 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
11710 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
11712 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
11714 Set_Discrete_RM_Size (Def_Id);
11715 end Constrain_Enumeration;
11717 ----------------------
11718 -- Constrain_Float --
11719 ----------------------
11721 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id) is
11722 T : constant Entity_Id := Entity (Subtype_Mark (S));
11723 C : Node_Id;
11724 D : Node_Id;
11725 Rais : Node_Id;
11727 begin
11728 Set_Ekind (Def_Id, E_Floating_Point_Subtype);
11730 Set_Etype (Def_Id, Base_Type (T));
11731 Set_Size_Info (Def_Id, (T));
11732 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
11734 -- Process the constraint
11736 C := Constraint (S);
11738 -- Digits constraint present
11740 if Nkind (C) = N_Digits_Constraint then
11742 Check_SPARK_Restriction ("digits constraint is not allowed", S);
11743 Check_Restriction (No_Obsolescent_Features, C);
11745 if Warn_On_Obsolescent_Feature then
11746 Error_Msg_N
11747 ("subtype digits constraint is an " &
11748 "obsolescent feature (RM J.3(8))?", C);
11749 end if;
11751 D := Digits_Expression (C);
11752 Analyze_And_Resolve (D, Any_Integer);
11753 Check_Digits_Expression (D);
11754 Set_Digits_Value (Def_Id, Expr_Value (D));
11756 -- Check that digits value is in range. Obviously we can do this
11757 -- at compile time, but it is strictly a runtime check, and of
11758 -- course there is an ACVC test that checks this!
11760 if Digits_Value (Def_Id) > Digits_Value (T) then
11761 Error_Msg_Uint_1 := Digits_Value (T);
11762 Error_Msg_N ("?digits value is too large, maximum is ^", D);
11763 Rais :=
11764 Make_Raise_Constraint_Error (Sloc (D),
11765 Reason => CE_Range_Check_Failed);
11766 Insert_Action (Declaration_Node (Def_Id), Rais);
11767 end if;
11769 C := Range_Constraint (C);
11771 -- No digits constraint present
11773 else
11774 Set_Digits_Value (Def_Id, Digits_Value (T));
11775 end if;
11777 -- Range constraint present
11779 if Nkind (C) = N_Range_Constraint then
11780 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
11782 -- No range constraint present
11784 else
11785 pragma Assert (No (C));
11786 Set_Scalar_Range (Def_Id, Scalar_Range (T));
11787 end if;
11789 Set_Is_Constrained (Def_Id);
11790 end Constrain_Float;
11792 ---------------------
11793 -- Constrain_Index --
11794 ---------------------
11796 procedure Constrain_Index
11797 (Index : Node_Id;
11798 S : Node_Id;
11799 Related_Nod : Node_Id;
11800 Related_Id : Entity_Id;
11801 Suffix : Character;
11802 Suffix_Index : Nat)
11804 Def_Id : Entity_Id;
11805 R : Node_Id := Empty;
11806 T : constant Entity_Id := Etype (Index);
11808 begin
11809 if Nkind (S) = N_Range
11810 or else
11811 (Nkind (S) = N_Attribute_Reference
11812 and then Attribute_Name (S) = Name_Range)
11813 then
11814 -- A Range attribute will be transformed into N_Range by Resolve
11816 Analyze (S);
11817 Set_Etype (S, T);
11818 R := S;
11820 Process_Range_Expr_In_Decl (R, T, Empty_List);
11822 if not Error_Posted (S)
11823 and then
11824 (Nkind (S) /= N_Range
11825 or else not Covers (T, (Etype (Low_Bound (S))))
11826 or else not Covers (T, (Etype (High_Bound (S)))))
11827 then
11828 if Base_Type (T) /= Any_Type
11829 and then Etype (Low_Bound (S)) /= Any_Type
11830 and then Etype (High_Bound (S)) /= Any_Type
11831 then
11832 Error_Msg_N ("range expected", S);
11833 end if;
11834 end if;
11836 elsif Nkind (S) = N_Subtype_Indication then
11838 -- The parser has verified that this is a discrete indication
11840 Resolve_Discrete_Subtype_Indication (S, T);
11841 R := Range_Expression (Constraint (S));
11843 -- Capture values of bounds and generate temporaries for them if
11844 -- needed, since checks may cause duplication of the expressions
11845 -- which must not be reevaluated.
11847 -- The forced evaluation removes side effects from expressions,
11848 -- which should occur also in Alfa mode. Otherwise, we end up with
11849 -- unexpected insertions of actions at places where this is not
11850 -- supposed to occur, e.g. on default parameters of a call.
11852 if Expander_Active then
11853 Force_Evaluation (Low_Bound (R));
11854 Force_Evaluation (High_Bound (R));
11855 end if;
11857 elsif Nkind (S) = N_Discriminant_Association then
11859 -- Syntactically valid in subtype indication
11861 Error_Msg_N ("invalid index constraint", S);
11862 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
11863 return;
11865 -- Subtype_Mark case, no anonymous subtypes to construct
11867 else
11868 Analyze (S);
11870 if Is_Entity_Name (S) then
11871 if not Is_Type (Entity (S)) then
11872 Error_Msg_N ("expect subtype mark for index constraint", S);
11874 elsif Base_Type (Entity (S)) /= Base_Type (T) then
11875 Wrong_Type (S, Base_Type (T));
11877 -- Check error of subtype with predicate in index constraint
11879 else
11880 Bad_Predicated_Subtype_Use
11881 ("subtype& has predicate, not allowed in index constraint",
11882 S, Entity (S));
11883 end if;
11885 return;
11887 else
11888 Error_Msg_N ("invalid index constraint", S);
11889 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
11890 return;
11891 end if;
11892 end if;
11894 Def_Id :=
11895 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
11897 Set_Etype (Def_Id, Base_Type (T));
11899 if Is_Modular_Integer_Type (T) then
11900 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
11902 elsif Is_Integer_Type (T) then
11903 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
11905 else
11906 Set_Ekind (Def_Id, E_Enumeration_Subtype);
11907 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
11908 Set_First_Literal (Def_Id, First_Literal (T));
11909 end if;
11911 Set_Size_Info (Def_Id, (T));
11912 Set_RM_Size (Def_Id, RM_Size (T));
11913 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
11915 Set_Scalar_Range (Def_Id, R);
11917 Set_Etype (S, Def_Id);
11918 Set_Discrete_RM_Size (Def_Id);
11919 end Constrain_Index;
11921 -----------------------
11922 -- Constrain_Integer --
11923 -----------------------
11925 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id) is
11926 T : constant Entity_Id := Entity (Subtype_Mark (S));
11927 C : constant Node_Id := Constraint (S);
11929 begin
11930 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
11932 if Is_Modular_Integer_Type (T) then
11933 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
11934 else
11935 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
11936 end if;
11938 Set_Etype (Def_Id, Base_Type (T));
11939 Set_Size_Info (Def_Id, (T));
11940 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
11941 Set_Discrete_RM_Size (Def_Id);
11942 end Constrain_Integer;
11944 ------------------------------
11945 -- Constrain_Ordinary_Fixed --
11946 ------------------------------
11948 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id) is
11949 T : constant Entity_Id := Entity (Subtype_Mark (S));
11950 C : Node_Id;
11951 D : Node_Id;
11952 Rais : Node_Id;
11954 begin
11955 Set_Ekind (Def_Id, E_Ordinary_Fixed_Point_Subtype);
11956 Set_Etype (Def_Id, Base_Type (T));
11957 Set_Size_Info (Def_Id, (T));
11958 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
11959 Set_Small_Value (Def_Id, Small_Value (T));
11961 -- Process the constraint
11963 C := Constraint (S);
11965 -- Delta constraint present
11967 if Nkind (C) = N_Delta_Constraint then
11969 Check_SPARK_Restriction ("delta constraint is not allowed", S);
11970 Check_Restriction (No_Obsolescent_Features, C);
11972 if Warn_On_Obsolescent_Feature then
11973 Error_Msg_S
11974 ("subtype delta constraint is an " &
11975 "obsolescent feature (RM J.3(7))?");
11976 end if;
11978 D := Delta_Expression (C);
11979 Analyze_And_Resolve (D, Any_Real);
11980 Check_Delta_Expression (D);
11981 Set_Delta_Value (Def_Id, Expr_Value_R (D));
11983 -- Check that delta value is in range. Obviously we can do this
11984 -- at compile time, but it is strictly a runtime check, and of
11985 -- course there is an ACVC test that checks this!
11987 if Delta_Value (Def_Id) < Delta_Value (T) then
11988 Error_Msg_N ("?delta value is too small", D);
11989 Rais :=
11990 Make_Raise_Constraint_Error (Sloc (D),
11991 Reason => CE_Range_Check_Failed);
11992 Insert_Action (Declaration_Node (Def_Id), Rais);
11993 end if;
11995 C := Range_Constraint (C);
11997 -- No delta constraint present
11999 else
12000 Set_Delta_Value (Def_Id, Delta_Value (T));
12001 end if;
12003 -- Range constraint present
12005 if Nkind (C) = N_Range_Constraint then
12006 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
12008 -- No range constraint present
12010 else
12011 pragma Assert (No (C));
12012 Set_Scalar_Range (Def_Id, Scalar_Range (T));
12014 end if;
12016 Set_Discrete_RM_Size (Def_Id);
12018 -- Unconditionally delay the freeze, since we cannot set size
12019 -- information in all cases correctly until the freeze point.
12021 Set_Has_Delayed_Freeze (Def_Id);
12022 end Constrain_Ordinary_Fixed;
12024 -----------------------
12025 -- Contain_Interface --
12026 -----------------------
12028 function Contain_Interface
12029 (Iface : Entity_Id;
12030 Ifaces : Elist_Id) return Boolean
12032 Iface_Elmt : Elmt_Id;
12034 begin
12035 if Present (Ifaces) then
12036 Iface_Elmt := First_Elmt (Ifaces);
12037 while Present (Iface_Elmt) loop
12038 if Node (Iface_Elmt) = Iface then
12039 return True;
12040 end if;
12042 Next_Elmt (Iface_Elmt);
12043 end loop;
12044 end if;
12046 return False;
12047 end Contain_Interface;
12049 ---------------------------
12050 -- Convert_Scalar_Bounds --
12051 ---------------------------
12053 procedure Convert_Scalar_Bounds
12054 (N : Node_Id;
12055 Parent_Type : Entity_Id;
12056 Derived_Type : Entity_Id;
12057 Loc : Source_Ptr)
12059 Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
12061 Lo : Node_Id;
12062 Hi : Node_Id;
12063 Rng : Node_Id;
12065 begin
12066 -- Defend against previous errors
12068 if No (Scalar_Range (Derived_Type)) then
12069 return;
12070 end if;
12072 Lo := Build_Scalar_Bound
12073 (Type_Low_Bound (Derived_Type),
12074 Parent_Type, Implicit_Base);
12076 Hi := Build_Scalar_Bound
12077 (Type_High_Bound (Derived_Type),
12078 Parent_Type, Implicit_Base);
12080 Rng :=
12081 Make_Range (Loc,
12082 Low_Bound => Lo,
12083 High_Bound => Hi);
12085 Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
12087 Set_Parent (Rng, N);
12088 Set_Scalar_Range (Derived_Type, Rng);
12090 -- Analyze the bounds
12092 Analyze_And_Resolve (Lo, Implicit_Base);
12093 Analyze_And_Resolve (Hi, Implicit_Base);
12095 -- Analyze the range itself, except that we do not analyze it if
12096 -- the bounds are real literals, and we have a fixed-point type.
12097 -- The reason for this is that we delay setting the bounds in this
12098 -- case till we know the final Small and Size values (see circuit
12099 -- in Freeze.Freeze_Fixed_Point_Type for further details).
12101 if Is_Fixed_Point_Type (Parent_Type)
12102 and then Nkind (Lo) = N_Real_Literal
12103 and then Nkind (Hi) = N_Real_Literal
12104 then
12105 return;
12107 -- Here we do the analysis of the range
12109 -- Note: we do this manually, since if we do a normal Analyze and
12110 -- Resolve call, there are problems with the conversions used for
12111 -- the derived type range.
12113 else
12114 Set_Etype (Rng, Implicit_Base);
12115 Set_Analyzed (Rng, True);
12116 end if;
12117 end Convert_Scalar_Bounds;
12119 -------------------
12120 -- Copy_And_Swap --
12121 -------------------
12123 procedure Copy_And_Swap (Priv, Full : Entity_Id) is
12124 begin
12125 -- Initialize new full declaration entity by copying the pertinent
12126 -- fields of the corresponding private declaration entity.
12128 -- We temporarily set Ekind to a value appropriate for a type to
12129 -- avoid assert failures in Einfo from checking for setting type
12130 -- attributes on something that is not a type. Ekind (Priv) is an
12131 -- appropriate choice, since it allowed the attributes to be set
12132 -- in the first place. This Ekind value will be modified later.
12134 Set_Ekind (Full, Ekind (Priv));
12136 -- Also set Etype temporarily to Any_Type, again, in the absence
12137 -- of errors, it will be properly reset, and if there are errors,
12138 -- then we want a value of Any_Type to remain.
12140 Set_Etype (Full, Any_Type);
12142 -- Now start copying attributes
12144 Set_Has_Discriminants (Full, Has_Discriminants (Priv));
12146 if Has_Discriminants (Full) then
12147 Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
12148 Set_Stored_Constraint (Full, Stored_Constraint (Priv));
12149 end if;
12151 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
12152 Set_Homonym (Full, Homonym (Priv));
12153 Set_Is_Immediately_Visible (Full, Is_Immediately_Visible (Priv));
12154 Set_Is_Public (Full, Is_Public (Priv));
12155 Set_Is_Pure (Full, Is_Pure (Priv));
12156 Set_Is_Tagged_Type (Full, Is_Tagged_Type (Priv));
12157 Set_Has_Pragma_Unmodified (Full, Has_Pragma_Unmodified (Priv));
12158 Set_Has_Pragma_Unreferenced (Full, Has_Pragma_Unreferenced (Priv));
12159 Set_Has_Pragma_Unreferenced_Objects
12160 (Full, Has_Pragma_Unreferenced_Objects
12161 (Priv));
12163 Conditional_Delay (Full, Priv);
12165 if Is_Tagged_Type (Full) then
12166 Set_Direct_Primitive_Operations (Full,
12167 Direct_Primitive_Operations (Priv));
12169 if Is_Base_Type (Priv) then
12170 Set_Class_Wide_Type (Full, Class_Wide_Type (Priv));
12171 end if;
12172 end if;
12174 Set_Is_Volatile (Full, Is_Volatile (Priv));
12175 Set_Treat_As_Volatile (Full, Treat_As_Volatile (Priv));
12176 Set_Scope (Full, Scope (Priv));
12177 Set_Next_Entity (Full, Next_Entity (Priv));
12178 Set_First_Entity (Full, First_Entity (Priv));
12179 Set_Last_Entity (Full, Last_Entity (Priv));
12181 -- If access types have been recorded for later handling, keep them in
12182 -- the full view so that they get handled when the full view freeze
12183 -- node is expanded.
12185 if Present (Freeze_Node (Priv))
12186 and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
12187 then
12188 Ensure_Freeze_Node (Full);
12189 Set_Access_Types_To_Process
12190 (Freeze_Node (Full),
12191 Access_Types_To_Process (Freeze_Node (Priv)));
12192 end if;
12194 -- Swap the two entities. Now Private is the full type entity and Full
12195 -- is the private one. They will be swapped back at the end of the
12196 -- private part. This swapping ensures that the entity that is visible
12197 -- in the private part is the full declaration.
12199 Exchange_Entities (Priv, Full);
12200 Append_Entity (Full, Scope (Full));
12201 end Copy_And_Swap;
12203 -------------------------------------
12204 -- Copy_Array_Base_Type_Attributes --
12205 -------------------------------------
12207 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
12208 begin
12209 Set_Component_Alignment (T1, Component_Alignment (T2));
12210 Set_Component_Type (T1, Component_Type (T2));
12211 Set_Component_Size (T1, Component_Size (T2));
12212 Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
12213 Set_Has_Non_Standard_Rep (T1, Has_Non_Standard_Rep (T2));
12214 Set_Has_Task (T1, Has_Task (T2));
12215 Set_Is_Packed (T1, Is_Packed (T2));
12216 Set_Has_Aliased_Components (T1, Has_Aliased_Components (T2));
12217 Set_Has_Atomic_Components (T1, Has_Atomic_Components (T2));
12218 Set_Has_Volatile_Components (T1, Has_Volatile_Components (T2));
12219 end Copy_Array_Base_Type_Attributes;
12221 -----------------------------------
12222 -- Copy_Array_Subtype_Attributes --
12223 -----------------------------------
12225 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
12226 begin
12227 Set_Size_Info (T1, T2);
12229 Set_First_Index (T1, First_Index (T2));
12230 Set_Is_Aliased (T1, Is_Aliased (T2));
12231 Set_Is_Volatile (T1, Is_Volatile (T2));
12232 Set_Treat_As_Volatile (T1, Treat_As_Volatile (T2));
12233 Set_Is_Constrained (T1, Is_Constrained (T2));
12234 Set_Depends_On_Private (T1, Has_Private_Component (T2));
12235 Set_First_Rep_Item (T1, First_Rep_Item (T2));
12236 Set_Convention (T1, Convention (T2));
12237 Set_Is_Limited_Composite (T1, Is_Limited_Composite (T2));
12238 Set_Is_Private_Composite (T1, Is_Private_Composite (T2));
12239 Set_Packed_Array_Type (T1, Packed_Array_Type (T2));
12240 end Copy_Array_Subtype_Attributes;
12242 -----------------------------------
12243 -- Create_Constrained_Components --
12244 -----------------------------------
12246 procedure Create_Constrained_Components
12247 (Subt : Entity_Id;
12248 Decl_Node : Node_Id;
12249 Typ : Entity_Id;
12250 Constraints : Elist_Id)
12252 Loc : constant Source_Ptr := Sloc (Subt);
12253 Comp_List : constant Elist_Id := New_Elmt_List;
12254 Parent_Type : constant Entity_Id := Etype (Typ);
12255 Assoc_List : constant List_Id := New_List;
12256 Discr_Val : Elmt_Id;
12257 Errors : Boolean;
12258 New_C : Entity_Id;
12259 Old_C : Entity_Id;
12260 Is_Static : Boolean := True;
12262 procedure Collect_Fixed_Components (Typ : Entity_Id);
12263 -- Collect parent type components that do not appear in a variant part
12265 procedure Create_All_Components;
12266 -- Iterate over Comp_List to create the components of the subtype
12268 function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
12269 -- Creates a new component from Old_Compon, copying all the fields from
12270 -- it, including its Etype, inserts the new component in the Subt entity
12271 -- chain and returns the new component.
12273 function Is_Variant_Record (T : Entity_Id) return Boolean;
12274 -- If true, and discriminants are static, collect only components from
12275 -- variants selected by discriminant values.
12277 ------------------------------
12278 -- Collect_Fixed_Components --
12279 ------------------------------
12281 procedure Collect_Fixed_Components (Typ : Entity_Id) is
12282 begin
12283 -- Build association list for discriminants, and find components of the
12284 -- variant part selected by the values of the discriminants.
12286 Old_C := First_Discriminant (Typ);
12287 Discr_Val := First_Elmt (Constraints);
12288 while Present (Old_C) loop
12289 Append_To (Assoc_List,
12290 Make_Component_Association (Loc,
12291 Choices => New_List (New_Occurrence_Of (Old_C, Loc)),
12292 Expression => New_Copy (Node (Discr_Val))));
12294 Next_Elmt (Discr_Val);
12295 Next_Discriminant (Old_C);
12296 end loop;
12298 -- The tag and the possible parent component are unconditionally in
12299 -- the subtype.
12301 if Is_Tagged_Type (Typ)
12302 or else Has_Controlled_Component (Typ)
12303 then
12304 Old_C := First_Component (Typ);
12305 while Present (Old_C) loop
12306 if Chars ((Old_C)) = Name_uTag
12307 or else Chars ((Old_C)) = Name_uParent
12308 then
12309 Append_Elmt (Old_C, Comp_List);
12310 end if;
12312 Next_Component (Old_C);
12313 end loop;
12314 end if;
12315 end Collect_Fixed_Components;
12317 ---------------------------
12318 -- Create_All_Components --
12319 ---------------------------
12321 procedure Create_All_Components is
12322 Comp : Elmt_Id;
12324 begin
12325 Comp := First_Elmt (Comp_List);
12326 while Present (Comp) loop
12327 Old_C := Node (Comp);
12328 New_C := Create_Component (Old_C);
12330 Set_Etype
12331 (New_C,
12332 Constrain_Component_Type
12333 (Old_C, Subt, Decl_Node, Typ, Constraints));
12334 Set_Is_Public (New_C, Is_Public (Subt));
12336 Next_Elmt (Comp);
12337 end loop;
12338 end Create_All_Components;
12340 ----------------------
12341 -- Create_Component --
12342 ----------------------
12344 function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
12345 New_Compon : constant Entity_Id := New_Copy (Old_Compon);
12347 begin
12348 if Ekind (Old_Compon) = E_Discriminant
12349 and then Is_Completely_Hidden (Old_Compon)
12350 then
12351 -- This is a shadow discriminant created for a discriminant of
12352 -- the parent type, which needs to be present in the subtype.
12353 -- Give the shadow discriminant an internal name that cannot
12354 -- conflict with that of visible components.
12356 Set_Chars (New_Compon, New_Internal_Name ('C'));
12357 end if;
12359 -- Set the parent so we have a proper link for freezing etc. This is
12360 -- not a real parent pointer, since of course our parent does not own
12361 -- up to us and reference us, we are an illegitimate child of the
12362 -- original parent!
12364 Set_Parent (New_Compon, Parent (Old_Compon));
12366 -- If the old component's Esize was already determined and is a
12367 -- static value, then the new component simply inherits it. Otherwise
12368 -- the old component's size may require run-time determination, but
12369 -- the new component's size still might be statically determinable
12370 -- (if, for example it has a static constraint). In that case we want
12371 -- Layout_Type to recompute the component's size, so we reset its
12372 -- size and positional fields.
12374 if Frontend_Layout_On_Target
12375 and then not Known_Static_Esize (Old_Compon)
12376 then
12377 Set_Esize (New_Compon, Uint_0);
12378 Init_Normalized_First_Bit (New_Compon);
12379 Init_Normalized_Position (New_Compon);
12380 Init_Normalized_Position_Max (New_Compon);
12381 end if;
12383 -- We do not want this node marked as Comes_From_Source, since
12384 -- otherwise it would get first class status and a separate cross-
12385 -- reference line would be generated. Illegitimate children do not
12386 -- rate such recognition.
12388 Set_Comes_From_Source (New_Compon, False);
12390 -- But it is a real entity, and a birth certificate must be properly
12391 -- registered by entering it into the entity list.
12393 Enter_Name (New_Compon);
12395 return New_Compon;
12396 end Create_Component;
12398 -----------------------
12399 -- Is_Variant_Record --
12400 -----------------------
12402 function Is_Variant_Record (T : Entity_Id) return Boolean is
12403 begin
12404 return Nkind (Parent (T)) = N_Full_Type_Declaration
12405 and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
12406 and then Present (Component_List (Type_Definition (Parent (T))))
12407 and then
12408 Present
12409 (Variant_Part (Component_List (Type_Definition (Parent (T)))));
12410 end Is_Variant_Record;
12412 -- Start of processing for Create_Constrained_Components
12414 begin
12415 pragma Assert (Subt /= Base_Type (Subt));
12416 pragma Assert (Typ = Base_Type (Typ));
12418 Set_First_Entity (Subt, Empty);
12419 Set_Last_Entity (Subt, Empty);
12421 -- Check whether constraint is fully static, in which case we can
12422 -- optimize the list of components.
12424 Discr_Val := First_Elmt (Constraints);
12425 while Present (Discr_Val) loop
12426 if not Is_OK_Static_Expression (Node (Discr_Val)) then
12427 Is_Static := False;
12428 exit;
12429 end if;
12431 Next_Elmt (Discr_Val);
12432 end loop;
12434 Set_Has_Static_Discriminants (Subt, Is_Static);
12436 Push_Scope (Subt);
12438 -- Inherit the discriminants of the parent type
12440 Add_Discriminants : declare
12441 Num_Disc : Int;
12442 Num_Gird : Int;
12444 begin
12445 Num_Disc := 0;
12446 Old_C := First_Discriminant (Typ);
12448 while Present (Old_C) loop
12449 Num_Disc := Num_Disc + 1;
12450 New_C := Create_Component (Old_C);
12451 Set_Is_Public (New_C, Is_Public (Subt));
12452 Next_Discriminant (Old_C);
12453 end loop;
12455 -- For an untagged derived subtype, the number of discriminants may
12456 -- be smaller than the number of inherited discriminants, because
12457 -- several of them may be renamed by a single new discriminant or
12458 -- constrained. In this case, add the hidden discriminants back into
12459 -- the subtype, because they need to be present if the optimizer of
12460 -- the GCC 4.x back-end decides to break apart assignments between
12461 -- objects using the parent view into member-wise assignments.
12463 Num_Gird := 0;
12465 if Is_Derived_Type (Typ)
12466 and then not Is_Tagged_Type (Typ)
12467 then
12468 Old_C := First_Stored_Discriminant (Typ);
12470 while Present (Old_C) loop
12471 Num_Gird := Num_Gird + 1;
12472 Next_Stored_Discriminant (Old_C);
12473 end loop;
12474 end if;
12476 if Num_Gird > Num_Disc then
12478 -- Find out multiple uses of new discriminants, and add hidden
12479 -- components for the extra renamed discriminants. We recognize
12480 -- multiple uses through the Corresponding_Discriminant of a
12481 -- new discriminant: if it constrains several old discriminants,
12482 -- this field points to the last one in the parent type. The
12483 -- stored discriminants of the derived type have the same name
12484 -- as those of the parent.
12486 declare
12487 Constr : Elmt_Id;
12488 New_Discr : Entity_Id;
12489 Old_Discr : Entity_Id;
12491 begin
12492 Constr := First_Elmt (Stored_Constraint (Typ));
12493 Old_Discr := First_Stored_Discriminant (Typ);
12494 while Present (Constr) loop
12495 if Is_Entity_Name (Node (Constr))
12496 and then Ekind (Entity (Node (Constr))) = E_Discriminant
12497 then
12498 New_Discr := Entity (Node (Constr));
12500 if Chars (Corresponding_Discriminant (New_Discr)) /=
12501 Chars (Old_Discr)
12502 then
12503 -- The new discriminant has been used to rename a
12504 -- subsequent old discriminant. Introduce a shadow
12505 -- component for the current old discriminant.
12507 New_C := Create_Component (Old_Discr);
12508 Set_Original_Record_Component (New_C, Old_Discr);
12509 end if;
12511 else
12512 -- The constraint has eliminated the old discriminant.
12513 -- Introduce a shadow component.
12515 New_C := Create_Component (Old_Discr);
12516 Set_Original_Record_Component (New_C, Old_Discr);
12517 end if;
12519 Next_Elmt (Constr);
12520 Next_Stored_Discriminant (Old_Discr);
12521 end loop;
12522 end;
12523 end if;
12524 end Add_Discriminants;
12526 if Is_Static
12527 and then Is_Variant_Record (Typ)
12528 then
12529 Collect_Fixed_Components (Typ);
12531 Gather_Components (
12532 Typ,
12533 Component_List (Type_Definition (Parent (Typ))),
12534 Governed_By => Assoc_List,
12535 Into => Comp_List,
12536 Report_Errors => Errors);
12537 pragma Assert (not Errors);
12539 Create_All_Components;
12541 -- If the subtype declaration is created for a tagged type derivation
12542 -- with constraints, we retrieve the record definition of the parent
12543 -- type to select the components of the proper variant.
12545 elsif Is_Static
12546 and then Is_Tagged_Type (Typ)
12547 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
12548 and then
12549 Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
12550 and then Is_Variant_Record (Parent_Type)
12551 then
12552 Collect_Fixed_Components (Typ);
12554 Gather_Components (
12555 Typ,
12556 Component_List (Type_Definition (Parent (Parent_Type))),
12557 Governed_By => Assoc_List,
12558 Into => Comp_List,
12559 Report_Errors => Errors);
12560 pragma Assert (not Errors);
12562 -- If the tagged derivation has a type extension, collect all the
12563 -- new components therein.
12565 if Present
12566 (Record_Extension_Part (Type_Definition (Parent (Typ))))
12567 then
12568 Old_C := First_Component (Typ);
12569 while Present (Old_C) loop
12570 if Original_Record_Component (Old_C) = Old_C
12571 and then Chars (Old_C) /= Name_uTag
12572 and then Chars (Old_C) /= Name_uParent
12573 then
12574 Append_Elmt (Old_C, Comp_List);
12575 end if;
12577 Next_Component (Old_C);
12578 end loop;
12579 end if;
12581 Create_All_Components;
12583 else
12584 -- If discriminants are not static, or if this is a multi-level type
12585 -- extension, we have to include all components of the parent type.
12587 Old_C := First_Component (Typ);
12588 while Present (Old_C) loop
12589 New_C := Create_Component (Old_C);
12591 Set_Etype
12592 (New_C,
12593 Constrain_Component_Type
12594 (Old_C, Subt, Decl_Node, Typ, Constraints));
12595 Set_Is_Public (New_C, Is_Public (Subt));
12597 Next_Component (Old_C);
12598 end loop;
12599 end if;
12601 End_Scope;
12602 end Create_Constrained_Components;
12604 ------------------------------------------
12605 -- Decimal_Fixed_Point_Type_Declaration --
12606 ------------------------------------------
12608 procedure Decimal_Fixed_Point_Type_Declaration
12609 (T : Entity_Id;
12610 Def : Node_Id)
12612 Loc : constant Source_Ptr := Sloc (Def);
12613 Digs_Expr : constant Node_Id := Digits_Expression (Def);
12614 Delta_Expr : constant Node_Id := Delta_Expression (Def);
12615 Implicit_Base : Entity_Id;
12616 Digs_Val : Uint;
12617 Delta_Val : Ureal;
12618 Scale_Val : Uint;
12619 Bound_Val : Ureal;
12621 begin
12622 Check_SPARK_Restriction
12623 ("decimal fixed point type is not allowed", Def);
12624 Check_Restriction (No_Fixed_Point, Def);
12626 -- Create implicit base type
12628 Implicit_Base :=
12629 Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
12630 Set_Etype (Implicit_Base, Implicit_Base);
12632 -- Analyze and process delta expression
12634 Analyze_And_Resolve (Delta_Expr, Universal_Real);
12636 Check_Delta_Expression (Delta_Expr);
12637 Delta_Val := Expr_Value_R (Delta_Expr);
12639 -- Check delta is power of 10, and determine scale value from it
12641 declare
12642 Val : Ureal;
12644 begin
12645 Scale_Val := Uint_0;
12646 Val := Delta_Val;
12648 if Val < Ureal_1 then
12649 while Val < Ureal_1 loop
12650 Val := Val * Ureal_10;
12651 Scale_Val := Scale_Val + 1;
12652 end loop;
12654 if Scale_Val > 18 then
12655 Error_Msg_N ("scale exceeds maximum value of 18", Def);
12656 Scale_Val := UI_From_Int (+18);
12657 end if;
12659 else
12660 while Val > Ureal_1 loop
12661 Val := Val / Ureal_10;
12662 Scale_Val := Scale_Val - 1;
12663 end loop;
12665 if Scale_Val < -18 then
12666 Error_Msg_N ("scale is less than minimum value of -18", Def);
12667 Scale_Val := UI_From_Int (-18);
12668 end if;
12669 end if;
12671 if Val /= Ureal_1 then
12672 Error_Msg_N ("delta expression must be a power of 10", Def);
12673 Delta_Val := Ureal_10 ** (-Scale_Val);
12674 end if;
12675 end;
12677 -- Set delta, scale and small (small = delta for decimal type)
12679 Set_Delta_Value (Implicit_Base, Delta_Val);
12680 Set_Scale_Value (Implicit_Base, Scale_Val);
12681 Set_Small_Value (Implicit_Base, Delta_Val);
12683 -- Analyze and process digits expression
12685 Analyze_And_Resolve (Digs_Expr, Any_Integer);
12686 Check_Digits_Expression (Digs_Expr);
12687 Digs_Val := Expr_Value (Digs_Expr);
12689 if Digs_Val > 18 then
12690 Digs_Val := UI_From_Int (+18);
12691 Error_Msg_N ("digits value out of range, maximum is 18", Digs_Expr);
12692 end if;
12694 Set_Digits_Value (Implicit_Base, Digs_Val);
12695 Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
12697 -- Set range of base type from digits value for now. This will be
12698 -- expanded to represent the true underlying base range by Freeze.
12700 Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
12702 -- Note: We leave size as zero for now, size will be set at freeze
12703 -- time. We have to do this for ordinary fixed-point, because the size
12704 -- depends on the specified small, and we might as well do the same for
12705 -- decimal fixed-point.
12707 pragma Assert (Esize (Implicit_Base) = Uint_0);
12709 -- If there are bounds given in the declaration use them as the
12710 -- bounds of the first named subtype.
12712 if Present (Real_Range_Specification (Def)) then
12713 declare
12714 RRS : constant Node_Id := Real_Range_Specification (Def);
12715 Low : constant Node_Id := Low_Bound (RRS);
12716 High : constant Node_Id := High_Bound (RRS);
12717 Low_Val : Ureal;
12718 High_Val : Ureal;
12720 begin
12721 Analyze_And_Resolve (Low, Any_Real);
12722 Analyze_And_Resolve (High, Any_Real);
12723 Check_Real_Bound (Low);
12724 Check_Real_Bound (High);
12725 Low_Val := Expr_Value_R (Low);
12726 High_Val := Expr_Value_R (High);
12728 if Low_Val < (-Bound_Val) then
12729 Error_Msg_N
12730 ("range low bound too small for digits value", Low);
12731 Low_Val := -Bound_Val;
12732 end if;
12734 if High_Val > Bound_Val then
12735 Error_Msg_N
12736 ("range high bound too large for digits value", High);
12737 High_Val := Bound_Val;
12738 end if;
12740 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
12741 end;
12743 -- If no explicit range, use range that corresponds to given
12744 -- digits value. This will end up as the final range for the
12745 -- first subtype.
12747 else
12748 Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
12749 end if;
12751 -- Complete entity for first subtype
12753 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
12754 Set_Etype (T, Implicit_Base);
12755 Set_Size_Info (T, Implicit_Base);
12756 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
12757 Set_Digits_Value (T, Digs_Val);
12758 Set_Delta_Value (T, Delta_Val);
12759 Set_Small_Value (T, Delta_Val);
12760 Set_Scale_Value (T, Scale_Val);
12761 Set_Is_Constrained (T);
12762 end Decimal_Fixed_Point_Type_Declaration;
12764 -----------------------------------
12765 -- Derive_Progenitor_Subprograms --
12766 -----------------------------------
12768 procedure Derive_Progenitor_Subprograms
12769 (Parent_Type : Entity_Id;
12770 Tagged_Type : Entity_Id)
12772 E : Entity_Id;
12773 Elmt : Elmt_Id;
12774 Iface : Entity_Id;
12775 Iface_Elmt : Elmt_Id;
12776 Iface_Subp : Entity_Id;
12777 New_Subp : Entity_Id := Empty;
12778 Prim_Elmt : Elmt_Id;
12779 Subp : Entity_Id;
12780 Typ : Entity_Id;
12782 begin
12783 pragma Assert (Ada_Version >= Ada_2005
12784 and then Is_Record_Type (Tagged_Type)
12785 and then Is_Tagged_Type (Tagged_Type)
12786 and then Has_Interfaces (Tagged_Type));
12788 -- Step 1: Transfer to the full-view primitives associated with the
12789 -- partial-view that cover interface primitives. Conceptually this
12790 -- work should be done later by Process_Full_View; done here to
12791 -- simplify its implementation at later stages. It can be safely
12792 -- done here because interfaces must be visible in the partial and
12793 -- private view (RM 7.3(7.3/2)).
12795 -- Small optimization: This work is only required if the parent is
12796 -- abstract. If the tagged type is not abstract, it cannot have
12797 -- abstract primitives (the only entities in the list of primitives of
12798 -- non-abstract tagged types that can reference abstract primitives
12799 -- through its Alias attribute are the internal entities that have
12800 -- attribute Interface_Alias, and these entities are generated later
12801 -- by Add_Internal_Interface_Entities).
12803 if In_Private_Part (Current_Scope)
12804 and then Is_Abstract_Type (Parent_Type)
12805 then
12806 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
12807 while Present (Elmt) loop
12808 Subp := Node (Elmt);
12810 -- At this stage it is not possible to have entities in the list
12811 -- of primitives that have attribute Interface_Alias
12813 pragma Assert (No (Interface_Alias (Subp)));
12815 Typ := Find_Dispatching_Type (Ultimate_Alias (Subp));
12817 if Is_Interface (Typ) then
12818 E := Find_Primitive_Covering_Interface
12819 (Tagged_Type => Tagged_Type,
12820 Iface_Prim => Subp);
12822 if Present (E)
12823 and then Find_Dispatching_Type (Ultimate_Alias (E)) /= Typ
12824 then
12825 Replace_Elmt (Elmt, E);
12826 Remove_Homonym (Subp);
12827 end if;
12828 end if;
12830 Next_Elmt (Elmt);
12831 end loop;
12832 end if;
12834 -- Step 2: Add primitives of progenitors that are not implemented by
12835 -- parents of Tagged_Type
12837 if Present (Interfaces (Base_Type (Tagged_Type))) then
12838 Iface_Elmt := First_Elmt (Interfaces (Base_Type (Tagged_Type)));
12839 while Present (Iface_Elmt) loop
12840 Iface := Node (Iface_Elmt);
12842 Prim_Elmt := First_Elmt (Primitive_Operations (Iface));
12843 while Present (Prim_Elmt) loop
12844 Iface_Subp := Node (Prim_Elmt);
12846 -- Exclude derivation of predefined primitives except those
12847 -- that come from source, or are inherited from one that comes
12848 -- from source. Required to catch declarations of equality
12849 -- operators of interfaces. For example:
12851 -- type Iface is interface;
12852 -- function "=" (Left, Right : Iface) return Boolean;
12854 if not Is_Predefined_Dispatching_Operation (Iface_Subp)
12855 or else Comes_From_Source (Ultimate_Alias (Iface_Subp))
12856 then
12857 E := Find_Primitive_Covering_Interface
12858 (Tagged_Type => Tagged_Type,
12859 Iface_Prim => Iface_Subp);
12861 -- If not found we derive a new primitive leaving its alias
12862 -- attribute referencing the interface primitive
12864 if No (E) then
12865 Derive_Subprogram
12866 (New_Subp, Iface_Subp, Tagged_Type, Iface);
12868 -- Ada 2012 (AI05-0197): If the covering primitive's name
12869 -- differs from the name of the interface primitive then it
12870 -- is a private primitive inherited from a parent type. In
12871 -- such case, given that Tagged_Type covers the interface,
12872 -- the inherited private primitive becomes visible. For such
12873 -- purpose we add a new entity that renames the inherited
12874 -- private primitive.
12876 elsif Chars (E) /= Chars (Iface_Subp) then
12877 pragma Assert (Has_Suffix (E, 'P'));
12878 Derive_Subprogram
12879 (New_Subp, Iface_Subp, Tagged_Type, Iface);
12880 Set_Alias (New_Subp, E);
12881 Set_Is_Abstract_Subprogram (New_Subp,
12882 Is_Abstract_Subprogram (E));
12884 -- Propagate to the full view interface entities associated
12885 -- with the partial view
12887 elsif In_Private_Part (Current_Scope)
12888 and then Present (Alias (E))
12889 and then Alias (E) = Iface_Subp
12890 and then
12891 List_Containing (Parent (E)) /=
12892 Private_Declarations
12893 (Specification
12894 (Unit_Declaration_Node (Current_Scope)))
12895 then
12896 Append_Elmt (E, Primitive_Operations (Tagged_Type));
12897 end if;
12898 end if;
12900 Next_Elmt (Prim_Elmt);
12901 end loop;
12903 Next_Elmt (Iface_Elmt);
12904 end loop;
12905 end if;
12906 end Derive_Progenitor_Subprograms;
12908 -----------------------
12909 -- Derive_Subprogram --
12910 -----------------------
12912 procedure Derive_Subprogram
12913 (New_Subp : in out Entity_Id;
12914 Parent_Subp : Entity_Id;
12915 Derived_Type : Entity_Id;
12916 Parent_Type : Entity_Id;
12917 Actual_Subp : Entity_Id := Empty)
12919 Formal : Entity_Id;
12920 -- Formal parameter of parent primitive operation
12922 Formal_Of_Actual : Entity_Id;
12923 -- Formal parameter of actual operation, when the derivation is to
12924 -- create a renaming for a primitive operation of an actual in an
12925 -- instantiation.
12927 New_Formal : Entity_Id;
12928 -- Formal of inherited operation
12930 Visible_Subp : Entity_Id := Parent_Subp;
12932 function Is_Private_Overriding return Boolean;
12933 -- If Subp is a private overriding of a visible operation, the inherited
12934 -- operation derives from the overridden op (even though its body is the
12935 -- overriding one) and the inherited operation is visible now. See
12936 -- sem_disp to see the full details of the handling of the overridden
12937 -- subprogram, which is removed from the list of primitive operations of
12938 -- the type. The overridden subprogram is saved locally in Visible_Subp,
12939 -- and used to diagnose abstract operations that need overriding in the
12940 -- derived type.
12942 procedure Replace_Type (Id, New_Id : Entity_Id);
12943 -- When the type is an anonymous access type, create a new access type
12944 -- designating the derived type.
12946 procedure Set_Derived_Name;
12947 -- This procedure sets the appropriate Chars name for New_Subp. This
12948 -- is normally just a copy of the parent name. An exception arises for
12949 -- type support subprograms, where the name is changed to reflect the
12950 -- name of the derived type, e.g. if type foo is derived from type bar,
12951 -- then a procedure barDA is derived with a name fooDA.
12953 ---------------------------
12954 -- Is_Private_Overriding --
12955 ---------------------------
12957 function Is_Private_Overriding return Boolean is
12958 Prev : Entity_Id;
12960 begin
12961 -- If the parent is not a dispatching operation there is no
12962 -- need to investigate overridings
12964 if not Is_Dispatching_Operation (Parent_Subp) then
12965 return False;
12966 end if;
12968 -- The visible operation that is overridden is a homonym of the
12969 -- parent subprogram. We scan the homonym chain to find the one
12970 -- whose alias is the subprogram we are deriving.
12972 Prev := Current_Entity (Parent_Subp);
12973 while Present (Prev) loop
12974 if Ekind (Prev) = Ekind (Parent_Subp)
12975 and then Alias (Prev) = Parent_Subp
12976 and then Scope (Parent_Subp) = Scope (Prev)
12977 and then not Is_Hidden (Prev)
12978 then
12979 Visible_Subp := Prev;
12980 return True;
12981 end if;
12983 Prev := Homonym (Prev);
12984 end loop;
12986 return False;
12987 end Is_Private_Overriding;
12989 ------------------
12990 -- Replace_Type --
12991 ------------------
12993 procedure Replace_Type (Id, New_Id : Entity_Id) is
12994 Acc_Type : Entity_Id;
12995 Par : constant Node_Id := Parent (Derived_Type);
12997 begin
12998 -- When the type is an anonymous access type, create a new access
12999 -- type designating the derived type. This itype must be elaborated
13000 -- at the point of the derivation, not on subsequent calls that may
13001 -- be out of the proper scope for Gigi, so we insert a reference to
13002 -- it after the derivation.
13004 if Ekind (Etype (Id)) = E_Anonymous_Access_Type then
13005 declare
13006 Desig_Typ : Entity_Id := Designated_Type (Etype (Id));
13008 begin
13009 if Ekind (Desig_Typ) = E_Record_Type_With_Private
13010 and then Present (Full_View (Desig_Typ))
13011 and then not Is_Private_Type (Parent_Type)
13012 then
13013 Desig_Typ := Full_View (Desig_Typ);
13014 end if;
13016 if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
13018 -- Ada 2005 (AI-251): Handle also derivations of abstract
13019 -- interface primitives.
13021 or else (Is_Interface (Desig_Typ)
13022 and then not Is_Class_Wide_Type (Desig_Typ))
13023 then
13024 Acc_Type := New_Copy (Etype (Id));
13025 Set_Etype (Acc_Type, Acc_Type);
13026 Set_Scope (Acc_Type, New_Subp);
13028 -- Compute size of anonymous access type
13030 if Is_Array_Type (Desig_Typ)
13031 and then not Is_Constrained (Desig_Typ)
13032 then
13033 Init_Size (Acc_Type, 2 * System_Address_Size);
13034 else
13035 Init_Size (Acc_Type, System_Address_Size);
13036 end if;
13038 Init_Alignment (Acc_Type);
13039 Set_Directly_Designated_Type (Acc_Type, Derived_Type);
13041 Set_Etype (New_Id, Acc_Type);
13042 Set_Scope (New_Id, New_Subp);
13044 -- Create a reference to it
13045 Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
13047 else
13048 Set_Etype (New_Id, Etype (Id));
13049 end if;
13050 end;
13052 elsif Base_Type (Etype (Id)) = Base_Type (Parent_Type)
13053 or else
13054 (Ekind (Etype (Id)) = E_Record_Type_With_Private
13055 and then Present (Full_View (Etype (Id)))
13056 and then
13057 Base_Type (Full_View (Etype (Id))) = Base_Type (Parent_Type))
13058 then
13059 -- Constraint checks on formals are generated during expansion,
13060 -- based on the signature of the original subprogram. The bounds
13061 -- of the derived type are not relevant, and thus we can use
13062 -- the base type for the formals. However, the return type may be
13063 -- used in a context that requires that the proper static bounds
13064 -- be used (a case statement, for example) and for those cases
13065 -- we must use the derived type (first subtype), not its base.
13067 -- If the derived_type_definition has no constraints, we know that
13068 -- the derived type has the same constraints as the first subtype
13069 -- of the parent, and we can also use it rather than its base,
13070 -- which can lead to more efficient code.
13072 if Etype (Id) = Parent_Type then
13073 if Is_Scalar_Type (Parent_Type)
13074 and then
13075 Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
13076 then
13077 Set_Etype (New_Id, Derived_Type);
13079 elsif Nkind (Par) = N_Full_Type_Declaration
13080 and then
13081 Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
13082 and then
13083 Is_Entity_Name
13084 (Subtype_Indication (Type_Definition (Par)))
13085 then
13086 Set_Etype (New_Id, Derived_Type);
13088 else
13089 Set_Etype (New_Id, Base_Type (Derived_Type));
13090 end if;
13092 else
13093 Set_Etype (New_Id, Base_Type (Derived_Type));
13094 end if;
13096 else
13097 Set_Etype (New_Id, Etype (Id));
13098 end if;
13099 end Replace_Type;
13101 ----------------------
13102 -- Set_Derived_Name --
13103 ----------------------
13105 procedure Set_Derived_Name is
13106 Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
13107 begin
13108 if Nm = TSS_Null then
13109 Set_Chars (New_Subp, Chars (Parent_Subp));
13110 else
13111 Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
13112 end if;
13113 end Set_Derived_Name;
13115 -- Start of processing for Derive_Subprogram
13117 begin
13118 New_Subp :=
13119 New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
13120 Set_Ekind (New_Subp, Ekind (Parent_Subp));
13121 Set_Contract (New_Subp, Make_Contract (Sloc (New_Subp)));
13123 -- Check whether the inherited subprogram is a private operation that
13124 -- should be inherited but not yet made visible. Such subprograms can
13125 -- become visible at a later point (e.g., the private part of a public
13126 -- child unit) via Declare_Inherited_Private_Subprograms. If the
13127 -- following predicate is true, then this is not such a private
13128 -- operation and the subprogram simply inherits the name of the parent
13129 -- subprogram. Note the special check for the names of controlled
13130 -- operations, which are currently exempted from being inherited with
13131 -- a hidden name because they must be findable for generation of
13132 -- implicit run-time calls.
13134 if not Is_Hidden (Parent_Subp)
13135 or else Is_Internal (Parent_Subp)
13136 or else Is_Private_Overriding
13137 or else Is_Internal_Name (Chars (Parent_Subp))
13138 or else Chars (Parent_Subp) = Name_Initialize
13139 or else Chars (Parent_Subp) = Name_Adjust
13140 or else Chars (Parent_Subp) = Name_Finalize
13141 then
13142 Set_Derived_Name;
13144 -- An inherited dispatching equality will be overridden by an internally
13145 -- generated one, or by an explicit one, so preserve its name and thus
13146 -- its entry in the dispatch table. Otherwise, if Parent_Subp is a
13147 -- private operation it may become invisible if the full view has
13148 -- progenitors, and the dispatch table will be malformed.
13149 -- We check that the type is limited to handle the anomalous declaration
13150 -- of Limited_Controlled, which is derived from a non-limited type, and
13151 -- which is handled specially elsewhere as well.
13153 elsif Chars (Parent_Subp) = Name_Op_Eq
13154 and then Is_Dispatching_Operation (Parent_Subp)
13155 and then Etype (Parent_Subp) = Standard_Boolean
13156 and then not Is_Limited_Type (Etype (First_Formal (Parent_Subp)))
13157 and then
13158 Etype (First_Formal (Parent_Subp)) =
13159 Etype (Next_Formal (First_Formal (Parent_Subp)))
13160 then
13161 Set_Derived_Name;
13163 -- If parent is hidden, this can be a regular derivation if the
13164 -- parent is immediately visible in a non-instantiating context,
13165 -- or if we are in the private part of an instance. This test
13166 -- should still be refined ???
13168 -- The test for In_Instance_Not_Visible avoids inheriting the derived
13169 -- operation as a non-visible operation in cases where the parent
13170 -- subprogram might not be visible now, but was visible within the
13171 -- original generic, so it would be wrong to make the inherited
13172 -- subprogram non-visible now. (Not clear if this test is fully
13173 -- correct; are there any cases where we should declare the inherited
13174 -- operation as not visible to avoid it being overridden, e.g., when
13175 -- the parent type is a generic actual with private primitives ???)
13177 -- (they should be treated the same as other private inherited
13178 -- subprograms, but it's not clear how to do this cleanly). ???
13180 elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
13181 and then Is_Immediately_Visible (Parent_Subp)
13182 and then not In_Instance)
13183 or else In_Instance_Not_Visible
13184 then
13185 Set_Derived_Name;
13187 -- Ada 2005 (AI-251): Regular derivation if the parent subprogram
13188 -- overrides an interface primitive because interface primitives
13189 -- must be visible in the partial view of the parent (RM 7.3 (7.3/2))
13191 elsif Ada_Version >= Ada_2005
13192 and then Is_Dispatching_Operation (Parent_Subp)
13193 and then Covers_Some_Interface (Parent_Subp)
13194 then
13195 Set_Derived_Name;
13197 -- Otherwise, the type is inheriting a private operation, so enter
13198 -- it with a special name so it can't be overridden.
13200 else
13201 Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
13202 end if;
13204 Set_Parent (New_Subp, Parent (Derived_Type));
13206 if Present (Actual_Subp) then
13207 Replace_Type (Actual_Subp, New_Subp);
13208 else
13209 Replace_Type (Parent_Subp, New_Subp);
13210 end if;
13212 Conditional_Delay (New_Subp, Parent_Subp);
13214 -- If we are creating a renaming for a primitive operation of an
13215 -- actual of a generic derived type, we must examine the signature
13216 -- of the actual primitive, not that of the generic formal, which for
13217 -- example may be an interface. However the name and initial value
13218 -- of the inherited operation are those of the formal primitive.
13220 Formal := First_Formal (Parent_Subp);
13222 if Present (Actual_Subp) then
13223 Formal_Of_Actual := First_Formal (Actual_Subp);
13224 else
13225 Formal_Of_Actual := Empty;
13226 end if;
13228 while Present (Formal) loop
13229 New_Formal := New_Copy (Formal);
13231 -- Normally we do not go copying parents, but in the case of
13232 -- formals, we need to link up to the declaration (which is the
13233 -- parameter specification), and it is fine to link up to the
13234 -- original formal's parameter specification in this case.
13236 Set_Parent (New_Formal, Parent (Formal));
13237 Append_Entity (New_Formal, New_Subp);
13239 if Present (Formal_Of_Actual) then
13240 Replace_Type (Formal_Of_Actual, New_Formal);
13241 Next_Formal (Formal_Of_Actual);
13242 else
13243 Replace_Type (Formal, New_Formal);
13244 end if;
13246 Next_Formal (Formal);
13247 end loop;
13249 -- If this derivation corresponds to a tagged generic actual, then
13250 -- primitive operations rename those of the actual. Otherwise the
13251 -- primitive operations rename those of the parent type, If the parent
13252 -- renames an intrinsic operator, so does the new subprogram. We except
13253 -- concatenation, which is always properly typed, and does not get
13254 -- expanded as other intrinsic operations.
13256 if No (Actual_Subp) then
13257 if Is_Intrinsic_Subprogram (Parent_Subp) then
13258 Set_Is_Intrinsic_Subprogram (New_Subp);
13260 if Present (Alias (Parent_Subp))
13261 and then Chars (Parent_Subp) /= Name_Op_Concat
13262 then
13263 Set_Alias (New_Subp, Alias (Parent_Subp));
13264 else
13265 Set_Alias (New_Subp, Parent_Subp);
13266 end if;
13268 else
13269 Set_Alias (New_Subp, Parent_Subp);
13270 end if;
13272 else
13273 Set_Alias (New_Subp, Actual_Subp);
13274 end if;
13276 -- Derived subprograms of a tagged type must inherit the convention
13277 -- of the parent subprogram (a requirement of AI-117). Derived
13278 -- subprograms of untagged types simply get convention Ada by default.
13280 if Is_Tagged_Type (Derived_Type) then
13281 Set_Convention (New_Subp, Convention (Parent_Subp));
13282 end if;
13284 -- Predefined controlled operations retain their name even if the parent
13285 -- is hidden (see above), but they are not primitive operations if the
13286 -- ancestor is not visible, for example if the parent is a private
13287 -- extension completed with a controlled extension. Note that a full
13288 -- type that is controlled can break privacy: the flag Is_Controlled is
13289 -- set on both views of the type.
13291 if Is_Controlled (Parent_Type)
13292 and then
13293 (Chars (Parent_Subp) = Name_Initialize
13294 or else Chars (Parent_Subp) = Name_Adjust
13295 or else Chars (Parent_Subp) = Name_Finalize)
13296 and then Is_Hidden (Parent_Subp)
13297 and then not Is_Visibly_Controlled (Parent_Type)
13298 then
13299 Set_Is_Hidden (New_Subp);
13300 end if;
13302 Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
13303 Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
13305 if Ekind (Parent_Subp) = E_Procedure then
13306 Set_Is_Valued_Procedure
13307 (New_Subp, Is_Valued_Procedure (Parent_Subp));
13308 else
13309 Set_Has_Controlling_Result
13310 (New_Subp, Has_Controlling_Result (Parent_Subp));
13311 end if;
13313 -- No_Return must be inherited properly. If this is overridden in the
13314 -- case of a dispatching operation, then a check is made in Sem_Disp
13315 -- that the overriding operation is also No_Return (no such check is
13316 -- required for the case of non-dispatching operation.
13318 Set_No_Return (New_Subp, No_Return (Parent_Subp));
13320 -- A derived function with a controlling result is abstract. If the
13321 -- Derived_Type is a nonabstract formal generic derived type, then
13322 -- inherited operations are not abstract: the required check is done at
13323 -- instantiation time. If the derivation is for a generic actual, the
13324 -- function is not abstract unless the actual is.
13326 if Is_Generic_Type (Derived_Type)
13327 and then not Is_Abstract_Type (Derived_Type)
13328 then
13329 null;
13331 -- Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
13332 -- properties of the subprogram, as defined in RM-3.9.3(4/2-6/2).
13334 elsif Ada_Version >= Ada_2005
13335 and then (Is_Abstract_Subprogram (Alias (New_Subp))
13336 or else (Is_Tagged_Type (Derived_Type)
13337 and then Etype (New_Subp) = Derived_Type
13338 and then not Is_Null_Extension (Derived_Type))
13339 or else (Is_Tagged_Type (Derived_Type)
13340 and then Ekind (Etype (New_Subp)) =
13341 E_Anonymous_Access_Type
13342 and then Designated_Type (Etype (New_Subp)) =
13343 Derived_Type
13344 and then not Is_Null_Extension (Derived_Type)))
13345 and then No (Actual_Subp)
13346 then
13347 if not Is_Tagged_Type (Derived_Type)
13348 or else Is_Abstract_Type (Derived_Type)
13349 or else Is_Abstract_Subprogram (Alias (New_Subp))
13350 then
13351 Set_Is_Abstract_Subprogram (New_Subp);
13352 else
13353 Set_Requires_Overriding (New_Subp);
13354 end if;
13356 elsif Ada_Version < Ada_2005
13357 and then (Is_Abstract_Subprogram (Alias (New_Subp))
13358 or else (Is_Tagged_Type (Derived_Type)
13359 and then Etype (New_Subp) = Derived_Type
13360 and then No (Actual_Subp)))
13361 then
13362 Set_Is_Abstract_Subprogram (New_Subp);
13364 -- AI05-0097 : an inherited operation that dispatches on result is
13365 -- abstract if the derived type is abstract, even if the parent type
13366 -- is concrete and the derived type is a null extension.
13368 elsif Has_Controlling_Result (Alias (New_Subp))
13369 and then Is_Abstract_Type (Etype (New_Subp))
13370 then
13371 Set_Is_Abstract_Subprogram (New_Subp);
13373 -- Finally, if the parent type is abstract we must verify that all
13374 -- inherited operations are either non-abstract or overridden, or that
13375 -- the derived type itself is abstract (this check is performed at the
13376 -- end of a package declaration, in Check_Abstract_Overriding). A
13377 -- private overriding in the parent type will not be visible in the
13378 -- derivation if we are not in an inner package or in a child unit of
13379 -- the parent type, in which case the abstractness of the inherited
13380 -- operation is carried to the new subprogram.
13382 elsif Is_Abstract_Type (Parent_Type)
13383 and then not In_Open_Scopes (Scope (Parent_Type))
13384 and then Is_Private_Overriding
13385 and then Is_Abstract_Subprogram (Visible_Subp)
13386 then
13387 if No (Actual_Subp) then
13388 Set_Alias (New_Subp, Visible_Subp);
13389 Set_Is_Abstract_Subprogram (New_Subp, True);
13391 else
13392 -- If this is a derivation for an instance of a formal derived
13393 -- type, abstractness comes from the primitive operation of the
13394 -- actual, not from the operation inherited from the ancestor.
13396 Set_Is_Abstract_Subprogram
13397 (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
13398 end if;
13399 end if;
13401 New_Overloaded_Entity (New_Subp, Derived_Type);
13403 -- Check for case of a derived subprogram for the instantiation of a
13404 -- formal derived tagged type, if so mark the subprogram as dispatching
13405 -- and inherit the dispatching attributes of the actual subprogram. The
13406 -- derived subprogram is effectively renaming of the actual subprogram,
13407 -- so it needs to have the same attributes as the actual.
13409 if Present (Actual_Subp)
13410 and then Is_Dispatching_Operation (Actual_Subp)
13411 then
13412 Set_Is_Dispatching_Operation (New_Subp);
13414 if Present (DTC_Entity (Actual_Subp)) then
13415 Set_DTC_Entity (New_Subp, DTC_Entity (Actual_Subp));
13416 Set_DT_Position (New_Subp, DT_Position (Actual_Subp));
13417 end if;
13418 end if;
13420 -- Indicate that a derived subprogram does not require a body and that
13421 -- it does not require processing of default expressions.
13423 Set_Has_Completion (New_Subp);
13424 Set_Default_Expressions_Processed (New_Subp);
13426 if Ekind (New_Subp) = E_Function then
13427 Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
13428 end if;
13429 end Derive_Subprogram;
13431 ------------------------
13432 -- Derive_Subprograms --
13433 ------------------------
13435 procedure Derive_Subprograms
13436 (Parent_Type : Entity_Id;
13437 Derived_Type : Entity_Id;
13438 Generic_Actual : Entity_Id := Empty)
13440 Op_List : constant Elist_Id :=
13441 Collect_Primitive_Operations (Parent_Type);
13443 function Check_Derived_Type return Boolean;
13444 -- Check that all the entities derived from Parent_Type are found in
13445 -- the list of primitives of Derived_Type exactly in the same order.
13447 procedure Derive_Interface_Subprogram
13448 (New_Subp : in out Entity_Id;
13449 Subp : Entity_Id;
13450 Actual_Subp : Entity_Id);
13451 -- Derive New_Subp from the ultimate alias of the parent subprogram Subp
13452 -- (which is an interface primitive). If Generic_Actual is present then
13453 -- Actual_Subp is the actual subprogram corresponding with the generic
13454 -- subprogram Subp.
13456 function Check_Derived_Type return Boolean is
13457 E : Entity_Id;
13458 Elmt : Elmt_Id;
13459 List : Elist_Id;
13460 New_Subp : Entity_Id;
13461 Op_Elmt : Elmt_Id;
13462 Subp : Entity_Id;
13464 begin
13465 -- Traverse list of entities in the current scope searching for
13466 -- an incomplete type whose full-view is derived type
13468 E := First_Entity (Scope (Derived_Type));
13469 while Present (E)
13470 and then E /= Derived_Type
13471 loop
13472 if Ekind (E) = E_Incomplete_Type
13473 and then Present (Full_View (E))
13474 and then Full_View (E) = Derived_Type
13475 then
13476 -- Disable this test if Derived_Type completes an incomplete
13477 -- type because in such case more primitives can be added
13478 -- later to the list of primitives of Derived_Type by routine
13479 -- Process_Incomplete_Dependents
13481 return True;
13482 end if;
13484 E := Next_Entity (E);
13485 end loop;
13487 List := Collect_Primitive_Operations (Derived_Type);
13488 Elmt := First_Elmt (List);
13490 Op_Elmt := First_Elmt (Op_List);
13491 while Present (Op_Elmt) loop
13492 Subp := Node (Op_Elmt);
13493 New_Subp := Node (Elmt);
13495 -- At this early stage Derived_Type has no entities with attribute
13496 -- Interface_Alias. In addition, such primitives are always
13497 -- located at the end of the list of primitives of Parent_Type.
13498 -- Therefore, if found we can safely stop processing pending
13499 -- entities.
13501 exit when Present (Interface_Alias (Subp));
13503 -- Handle hidden entities
13505 if not Is_Predefined_Dispatching_Operation (Subp)
13506 and then Is_Hidden (Subp)
13507 then
13508 if Present (New_Subp)
13509 and then Primitive_Names_Match (Subp, New_Subp)
13510 then
13511 Next_Elmt (Elmt);
13512 end if;
13514 else
13515 if not Present (New_Subp)
13516 or else Ekind (Subp) /= Ekind (New_Subp)
13517 or else not Primitive_Names_Match (Subp, New_Subp)
13518 then
13519 return False;
13520 end if;
13522 Next_Elmt (Elmt);
13523 end if;
13525 Next_Elmt (Op_Elmt);
13526 end loop;
13528 return True;
13529 end Check_Derived_Type;
13531 ---------------------------------
13532 -- Derive_Interface_Subprogram --
13533 ---------------------------------
13535 procedure Derive_Interface_Subprogram
13536 (New_Subp : in out Entity_Id;
13537 Subp : Entity_Id;
13538 Actual_Subp : Entity_Id)
13540 Iface_Subp : constant Entity_Id := Ultimate_Alias (Subp);
13541 Iface_Type : constant Entity_Id := Find_Dispatching_Type (Iface_Subp);
13543 begin
13544 pragma Assert (Is_Interface (Iface_Type));
13546 Derive_Subprogram
13547 (New_Subp => New_Subp,
13548 Parent_Subp => Iface_Subp,
13549 Derived_Type => Derived_Type,
13550 Parent_Type => Iface_Type,
13551 Actual_Subp => Actual_Subp);
13553 -- Given that this new interface entity corresponds with a primitive
13554 -- of the parent that was not overridden we must leave it associated
13555 -- with its parent primitive to ensure that it will share the same
13556 -- dispatch table slot when overridden.
13558 if No (Actual_Subp) then
13559 Set_Alias (New_Subp, Subp);
13561 -- For instantiations this is not needed since the previous call to
13562 -- Derive_Subprogram leaves the entity well decorated.
13564 else
13565 pragma Assert (Alias (New_Subp) = Actual_Subp);
13566 null;
13567 end if;
13568 end Derive_Interface_Subprogram;
13570 -- Local variables
13572 Alias_Subp : Entity_Id;
13573 Act_List : Elist_Id;
13574 Act_Elmt : Elmt_Id := No_Elmt;
13575 Act_Subp : Entity_Id := Empty;
13576 Elmt : Elmt_Id;
13577 Need_Search : Boolean := False;
13578 New_Subp : Entity_Id := Empty;
13579 Parent_Base : Entity_Id;
13580 Subp : Entity_Id;
13582 -- Start of processing for Derive_Subprograms
13584 begin
13585 if Ekind (Parent_Type) = E_Record_Type_With_Private
13586 and then Has_Discriminants (Parent_Type)
13587 and then Present (Full_View (Parent_Type))
13588 then
13589 Parent_Base := Full_View (Parent_Type);
13590 else
13591 Parent_Base := Parent_Type;
13592 end if;
13594 if Present (Generic_Actual) then
13595 Act_List := Collect_Primitive_Operations (Generic_Actual);
13596 Act_Elmt := First_Elmt (Act_List);
13597 end if;
13599 -- Derive primitives inherited from the parent. Note that if the generic
13600 -- actual is present, this is not really a type derivation, it is a
13601 -- completion within an instance.
13603 -- Case 1: Derived_Type does not implement interfaces
13605 if not Is_Tagged_Type (Derived_Type)
13606 or else (not Has_Interfaces (Derived_Type)
13607 and then not (Present (Generic_Actual)
13608 and then
13609 Has_Interfaces (Generic_Actual)))
13610 then
13611 Elmt := First_Elmt (Op_List);
13612 while Present (Elmt) loop
13613 Subp := Node (Elmt);
13615 -- Literals are derived earlier in the process of building the
13616 -- derived type, and are skipped here.
13618 if Ekind (Subp) = E_Enumeration_Literal then
13619 null;
13621 -- The actual is a direct descendant and the common primitive
13622 -- operations appear in the same order.
13624 -- If the generic parent type is present, the derived type is an
13625 -- instance of a formal derived type, and within the instance its
13626 -- operations are those of the actual. We derive from the formal
13627 -- type but make the inherited operations aliases of the
13628 -- corresponding operations of the actual.
13630 else
13631 pragma Assert (No (Node (Act_Elmt))
13632 or else (Primitive_Names_Match (Subp, Node (Act_Elmt))
13633 and then
13634 Type_Conformant (Subp, Node (Act_Elmt),
13635 Skip_Controlling_Formals => True)));
13637 Derive_Subprogram
13638 (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
13640 if Present (Act_Elmt) then
13641 Next_Elmt (Act_Elmt);
13642 end if;
13643 end if;
13645 Next_Elmt (Elmt);
13646 end loop;
13648 -- Case 2: Derived_Type implements interfaces
13650 else
13651 -- If the parent type has no predefined primitives we remove
13652 -- predefined primitives from the list of primitives of generic
13653 -- actual to simplify the complexity of this algorithm.
13655 if Present (Generic_Actual) then
13656 declare
13657 Has_Predefined_Primitives : Boolean := False;
13659 begin
13660 -- Check if the parent type has predefined primitives
13662 Elmt := First_Elmt (Op_List);
13663 while Present (Elmt) loop
13664 Subp := Node (Elmt);
13666 if Is_Predefined_Dispatching_Operation (Subp)
13667 and then not Comes_From_Source (Ultimate_Alias (Subp))
13668 then
13669 Has_Predefined_Primitives := True;
13670 exit;
13671 end if;
13673 Next_Elmt (Elmt);
13674 end loop;
13676 -- Remove predefined primitives of Generic_Actual. We must use
13677 -- an auxiliary list because in case of tagged types the value
13678 -- returned by Collect_Primitive_Operations is the value stored
13679 -- in its Primitive_Operations attribute (and we don't want to
13680 -- modify its current contents).
13682 if not Has_Predefined_Primitives then
13683 declare
13684 Aux_List : constant Elist_Id := New_Elmt_List;
13686 begin
13687 Elmt := First_Elmt (Act_List);
13688 while Present (Elmt) loop
13689 Subp := Node (Elmt);
13691 if not Is_Predefined_Dispatching_Operation (Subp)
13692 or else Comes_From_Source (Subp)
13693 then
13694 Append_Elmt (Subp, Aux_List);
13695 end if;
13697 Next_Elmt (Elmt);
13698 end loop;
13700 Act_List := Aux_List;
13701 end;
13702 end if;
13704 Act_Elmt := First_Elmt (Act_List);
13705 Act_Subp := Node (Act_Elmt);
13706 end;
13707 end if;
13709 -- Stage 1: If the generic actual is not present we derive the
13710 -- primitives inherited from the parent type. If the generic parent
13711 -- type is present, the derived type is an instance of a formal
13712 -- derived type, and within the instance its operations are those of
13713 -- the actual. We derive from the formal type but make the inherited
13714 -- operations aliases of the corresponding operations of the actual.
13716 Elmt := First_Elmt (Op_List);
13717 while Present (Elmt) loop
13718 Subp := Node (Elmt);
13719 Alias_Subp := Ultimate_Alias (Subp);
13721 -- Do not derive internal entities of the parent that link
13722 -- interface primitives with their covering primitive. These
13723 -- entities will be added to this type when frozen.
13725 if Present (Interface_Alias (Subp)) then
13726 goto Continue;
13727 end if;
13729 -- If the generic actual is present find the corresponding
13730 -- operation in the generic actual. If the parent type is a
13731 -- direct ancestor of the derived type then, even if it is an
13732 -- interface, the operations are inherited from the primary
13733 -- dispatch table and are in the proper order. If we detect here
13734 -- that primitives are not in the same order we traverse the list
13735 -- of primitive operations of the actual to find the one that
13736 -- implements the interface primitive.
13738 if Need_Search
13739 or else
13740 (Present (Generic_Actual)
13741 and then Present (Act_Subp)
13742 and then not
13743 (Primitive_Names_Match (Subp, Act_Subp)
13744 and then
13745 Type_Conformant (Subp, Act_Subp,
13746 Skip_Controlling_Formals => True)))
13747 then
13748 pragma Assert (not Is_Ancestor (Parent_Base, Generic_Actual,
13749 Use_Full_View => True));
13751 -- Remember that we need searching for all pending primitives
13753 Need_Search := True;
13755 -- Handle entities associated with interface primitives
13757 if Present (Alias_Subp)
13758 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
13759 and then not Is_Predefined_Dispatching_Operation (Subp)
13760 then
13761 -- Search for the primitive in the homonym chain
13763 Act_Subp :=
13764 Find_Primitive_Covering_Interface
13765 (Tagged_Type => Generic_Actual,
13766 Iface_Prim => Alias_Subp);
13768 -- Previous search may not locate primitives covering
13769 -- interfaces defined in generics units or instantiations.
13770 -- (it fails if the covering primitive has formals whose
13771 -- type is also defined in generics or instantiations).
13772 -- In such case we search in the list of primitives of the
13773 -- generic actual for the internal entity that links the
13774 -- interface primitive and the covering primitive.
13776 if No (Act_Subp)
13777 and then Is_Generic_Type (Parent_Type)
13778 then
13779 -- This code has been designed to handle only generic
13780 -- formals that implement interfaces that are defined
13781 -- in a generic unit or instantiation. If this code is
13782 -- needed for other cases we must review it because
13783 -- (given that it relies on Original_Location to locate
13784 -- the primitive of Generic_Actual that covers the
13785 -- interface) it could leave linked through attribute
13786 -- Alias entities of unrelated instantiations).
13788 pragma Assert
13789 (Is_Generic_Unit
13790 (Scope (Find_Dispatching_Type (Alias_Subp)))
13791 or else
13792 Instantiation_Depth
13793 (Sloc (Find_Dispatching_Type (Alias_Subp))) > 0);
13795 declare
13796 Iface_Prim_Loc : constant Source_Ptr :=
13797 Original_Location (Sloc (Alias_Subp));
13798 Elmt : Elmt_Id;
13799 Prim : Entity_Id;
13800 begin
13801 Elmt :=
13802 First_Elmt (Primitive_Operations (Generic_Actual));
13804 Search : while Present (Elmt) loop
13805 Prim := Node (Elmt);
13807 if Present (Interface_Alias (Prim))
13808 and then Original_Location
13809 (Sloc (Interface_Alias (Prim)))
13810 = Iface_Prim_Loc
13811 then
13812 Act_Subp := Alias (Prim);
13813 exit Search;
13814 end if;
13816 Next_Elmt (Elmt);
13817 end loop Search;
13818 end;
13819 end if;
13821 pragma Assert (Present (Act_Subp)
13822 or else Is_Abstract_Type (Generic_Actual)
13823 or else Serious_Errors_Detected > 0);
13825 -- Handle predefined primitives plus the rest of user-defined
13826 -- primitives
13828 else
13829 Act_Elmt := First_Elmt (Act_List);
13830 while Present (Act_Elmt) loop
13831 Act_Subp := Node (Act_Elmt);
13833 exit when Primitive_Names_Match (Subp, Act_Subp)
13834 and then Type_Conformant
13835 (Subp, Act_Subp,
13836 Skip_Controlling_Formals => True)
13837 and then No (Interface_Alias (Act_Subp));
13839 Next_Elmt (Act_Elmt);
13840 end loop;
13842 if No (Act_Elmt) then
13843 Act_Subp := Empty;
13844 end if;
13845 end if;
13846 end if;
13848 -- Case 1: If the parent is a limited interface then it has the
13849 -- predefined primitives of synchronized interfaces. However, the
13850 -- actual type may be a non-limited type and hence it does not
13851 -- have such primitives.
13853 if Present (Generic_Actual)
13854 and then not Present (Act_Subp)
13855 and then Is_Limited_Interface (Parent_Base)
13856 and then Is_Predefined_Interface_Primitive (Subp)
13857 then
13858 null;
13860 -- Case 2: Inherit entities associated with interfaces that were
13861 -- not covered by the parent type. We exclude here null interface
13862 -- primitives because they do not need special management.
13864 -- We also exclude interface operations that are renamings. If the
13865 -- subprogram is an explicit renaming of an interface primitive,
13866 -- it is a regular primitive operation, and the presence of its
13867 -- alias is not relevant: it has to be derived like any other
13868 -- primitive.
13870 elsif Present (Alias (Subp))
13871 and then Nkind (Unit_Declaration_Node (Subp)) /=
13872 N_Subprogram_Renaming_Declaration
13873 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
13874 and then not
13875 (Nkind (Parent (Alias_Subp)) = N_Procedure_Specification
13876 and then Null_Present (Parent (Alias_Subp)))
13877 then
13878 -- If this is an abstract private type then we transfer the
13879 -- derivation of the interface primitive from the partial view
13880 -- to the full view. This is safe because all the interfaces
13881 -- must be visible in the partial view. Done to avoid adding
13882 -- a new interface derivation to the private part of the
13883 -- enclosing package; otherwise this new derivation would be
13884 -- decorated as hidden when the analysis of the enclosing
13885 -- package completes.
13887 if Is_Abstract_Type (Derived_Type)
13888 and then In_Private_Part (Current_Scope)
13889 and then Has_Private_Declaration (Derived_Type)
13890 then
13891 declare
13892 Partial_View : Entity_Id;
13893 Elmt : Elmt_Id;
13894 Ent : Entity_Id;
13896 begin
13897 Partial_View := First_Entity (Current_Scope);
13898 loop
13899 exit when No (Partial_View)
13900 or else (Has_Private_Declaration (Partial_View)
13901 and then
13902 Full_View (Partial_View) = Derived_Type);
13904 Next_Entity (Partial_View);
13905 end loop;
13907 -- If the partial view was not found then the source code
13908 -- has errors and the derivation is not needed.
13910 if Present (Partial_View) then
13911 Elmt :=
13912 First_Elmt (Primitive_Operations (Partial_View));
13913 while Present (Elmt) loop
13914 Ent := Node (Elmt);
13916 if Present (Alias (Ent))
13917 and then Ultimate_Alias (Ent) = Alias (Subp)
13918 then
13919 Append_Elmt
13920 (Ent, Primitive_Operations (Derived_Type));
13921 exit;
13922 end if;
13924 Next_Elmt (Elmt);
13925 end loop;
13927 -- If the interface primitive was not found in the
13928 -- partial view then this interface primitive was
13929 -- overridden. We add a derivation to activate in
13930 -- Derive_Progenitor_Subprograms the machinery to
13931 -- search for it.
13933 if No (Elmt) then
13934 Derive_Interface_Subprogram
13935 (New_Subp => New_Subp,
13936 Subp => Subp,
13937 Actual_Subp => Act_Subp);
13938 end if;
13939 end if;
13940 end;
13941 else
13942 Derive_Interface_Subprogram
13943 (New_Subp => New_Subp,
13944 Subp => Subp,
13945 Actual_Subp => Act_Subp);
13946 end if;
13948 -- Case 3: Common derivation
13950 else
13951 Derive_Subprogram
13952 (New_Subp => New_Subp,
13953 Parent_Subp => Subp,
13954 Derived_Type => Derived_Type,
13955 Parent_Type => Parent_Base,
13956 Actual_Subp => Act_Subp);
13957 end if;
13959 -- No need to update Act_Elm if we must search for the
13960 -- corresponding operation in the generic actual
13962 if not Need_Search
13963 and then Present (Act_Elmt)
13964 then
13965 Next_Elmt (Act_Elmt);
13966 Act_Subp := Node (Act_Elmt);
13967 end if;
13969 <<Continue>>
13970 Next_Elmt (Elmt);
13971 end loop;
13973 -- Inherit additional operations from progenitors. If the derived
13974 -- type is a generic actual, there are not new primitive operations
13975 -- for the type because it has those of the actual, and therefore
13976 -- nothing needs to be done. The renamings generated above are not
13977 -- primitive operations, and their purpose is simply to make the
13978 -- proper operations visible within an instantiation.
13980 if No (Generic_Actual) then
13981 Derive_Progenitor_Subprograms (Parent_Base, Derived_Type);
13982 end if;
13983 end if;
13985 -- Final check: Direct descendants must have their primitives in the
13986 -- same order. We exclude from this test untagged types and instances
13987 -- of formal derived types. We skip this test if we have already
13988 -- reported serious errors in the sources.
13990 pragma Assert (not Is_Tagged_Type (Derived_Type)
13991 or else Present (Generic_Actual)
13992 or else Serious_Errors_Detected > 0
13993 or else Check_Derived_Type);
13994 end Derive_Subprograms;
13996 --------------------------------
13997 -- Derived_Standard_Character --
13998 --------------------------------
14000 procedure Derived_Standard_Character
14001 (N : Node_Id;
14002 Parent_Type : Entity_Id;
14003 Derived_Type : Entity_Id)
14005 Loc : constant Source_Ptr := Sloc (N);
14006 Def : constant Node_Id := Type_Definition (N);
14007 Indic : constant Node_Id := Subtype_Indication (Def);
14008 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
14009 Implicit_Base : constant Entity_Id :=
14010 Create_Itype
14011 (E_Enumeration_Type, N, Derived_Type, 'B');
14013 Lo : Node_Id;
14014 Hi : Node_Id;
14016 begin
14017 Discard_Node (Process_Subtype (Indic, N));
14019 Set_Etype (Implicit_Base, Parent_Base);
14020 Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
14021 Set_RM_Size (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
14023 Set_Is_Character_Type (Implicit_Base, True);
14024 Set_Has_Delayed_Freeze (Implicit_Base);
14026 -- The bounds of the implicit base are the bounds of the parent base.
14027 -- Note that their type is the parent base.
14029 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
14030 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
14032 Set_Scalar_Range (Implicit_Base,
14033 Make_Range (Loc,
14034 Low_Bound => Lo,
14035 High_Bound => Hi));
14037 Conditional_Delay (Derived_Type, Parent_Type);
14039 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
14040 Set_Etype (Derived_Type, Implicit_Base);
14041 Set_Size_Info (Derived_Type, Parent_Type);
14043 if Unknown_RM_Size (Derived_Type) then
14044 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
14045 end if;
14047 Set_Is_Character_Type (Derived_Type, True);
14049 if Nkind (Indic) /= N_Subtype_Indication then
14051 -- If no explicit constraint, the bounds are those
14052 -- of the parent type.
14054 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Type));
14055 Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
14056 Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
14057 end if;
14059 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
14061 -- Because the implicit base is used in the conversion of the bounds, we
14062 -- have to freeze it now. This is similar to what is done for numeric
14063 -- types, and it equally suspicious, but otherwise a non-static bound
14064 -- will have a reference to an unfrozen type, which is rejected by Gigi
14065 -- (???). This requires specific care for definition of stream
14066 -- attributes. For details, see comments at the end of
14067 -- Build_Derived_Numeric_Type.
14069 Freeze_Before (N, Implicit_Base);
14070 end Derived_Standard_Character;
14072 ------------------------------
14073 -- Derived_Type_Declaration --
14074 ------------------------------
14076 procedure Derived_Type_Declaration
14077 (T : Entity_Id;
14078 N : Node_Id;
14079 Is_Completion : Boolean)
14081 Parent_Type : Entity_Id;
14083 function Comes_From_Generic (Typ : Entity_Id) return Boolean;
14084 -- Check whether the parent type is a generic formal, or derives
14085 -- directly or indirectly from one.
14087 ------------------------
14088 -- Comes_From_Generic --
14089 ------------------------
14091 function Comes_From_Generic (Typ : Entity_Id) return Boolean is
14092 begin
14093 if Is_Generic_Type (Typ) then
14094 return True;
14096 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
14097 return True;
14099 elsif Is_Private_Type (Typ)
14100 and then Present (Full_View (Typ))
14101 and then Is_Generic_Type (Root_Type (Full_View (Typ)))
14102 then
14103 return True;
14105 elsif Is_Generic_Actual_Type (Typ) then
14106 return True;
14108 else
14109 return False;
14110 end if;
14111 end Comes_From_Generic;
14113 -- Local variables
14115 Def : constant Node_Id := Type_Definition (N);
14116 Iface_Def : Node_Id;
14117 Indic : constant Node_Id := Subtype_Indication (Def);
14118 Extension : constant Node_Id := Record_Extension_Part (Def);
14119 Parent_Node : Node_Id;
14120 Taggd : Boolean;
14122 -- Start of processing for Derived_Type_Declaration
14124 begin
14125 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
14127 -- Ada 2005 (AI-251): In case of interface derivation check that the
14128 -- parent is also an interface.
14130 if Interface_Present (Def) then
14131 Check_SPARK_Restriction ("interface is not allowed", Def);
14133 if not Is_Interface (Parent_Type) then
14134 Diagnose_Interface (Indic, Parent_Type);
14136 else
14137 Parent_Node := Parent (Base_Type (Parent_Type));
14138 Iface_Def := Type_Definition (Parent_Node);
14140 -- Ada 2005 (AI-251): Limited interfaces can only inherit from
14141 -- other limited interfaces.
14143 if Limited_Present (Def) then
14144 if Limited_Present (Iface_Def) then
14145 null;
14147 elsif Protected_Present (Iface_Def) then
14148 Error_Msg_NE
14149 ("descendant of& must be declared"
14150 & " as a protected interface",
14151 N, Parent_Type);
14153 elsif Synchronized_Present (Iface_Def) then
14154 Error_Msg_NE
14155 ("descendant of& must be declared"
14156 & " as a synchronized interface",
14157 N, Parent_Type);
14159 elsif Task_Present (Iface_Def) then
14160 Error_Msg_NE
14161 ("descendant of& must be declared as a task interface",
14162 N, Parent_Type);
14164 else
14165 Error_Msg_N
14166 ("(Ada 2005) limited interface cannot "
14167 & "inherit from non-limited interface", Indic);
14168 end if;
14170 -- Ada 2005 (AI-345): Non-limited interfaces can only inherit
14171 -- from non-limited or limited interfaces.
14173 elsif not Protected_Present (Def)
14174 and then not Synchronized_Present (Def)
14175 and then not Task_Present (Def)
14176 then
14177 if Limited_Present (Iface_Def) then
14178 null;
14180 elsif Protected_Present (Iface_Def) then
14181 Error_Msg_NE
14182 ("descendant of& must be declared"
14183 & " as a protected interface",
14184 N, Parent_Type);
14186 elsif Synchronized_Present (Iface_Def) then
14187 Error_Msg_NE
14188 ("descendant of& must be declared"
14189 & " as a synchronized interface",
14190 N, Parent_Type);
14192 elsif Task_Present (Iface_Def) then
14193 Error_Msg_NE
14194 ("descendant of& must be declared as a task interface",
14195 N, Parent_Type);
14196 else
14197 null;
14198 end if;
14199 end if;
14200 end if;
14201 end if;
14203 if Is_Tagged_Type (Parent_Type)
14204 and then Is_Concurrent_Type (Parent_Type)
14205 and then not Is_Interface (Parent_Type)
14206 then
14207 Error_Msg_N
14208 ("parent type of a record extension cannot be "
14209 & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
14210 Set_Etype (T, Any_Type);
14211 return;
14212 end if;
14214 -- Ada 2005 (AI-251): Decorate all the names in the list of ancestor
14215 -- interfaces
14217 if Is_Tagged_Type (Parent_Type)
14218 and then Is_Non_Empty_List (Interface_List (Def))
14219 then
14220 declare
14221 Intf : Node_Id;
14222 T : Entity_Id;
14224 begin
14225 Intf := First (Interface_List (Def));
14226 while Present (Intf) loop
14227 T := Find_Type_Of_Subtype_Indic (Intf);
14229 if not Is_Interface (T) then
14230 Diagnose_Interface (Intf, T);
14232 -- Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
14233 -- a limited type from having a nonlimited progenitor.
14235 elsif (Limited_Present (Def)
14236 or else (not Is_Interface (Parent_Type)
14237 and then Is_Limited_Type (Parent_Type)))
14238 and then not Is_Limited_Interface (T)
14239 then
14240 Error_Msg_NE
14241 ("progenitor interface& of limited type must be limited",
14242 N, T);
14243 end if;
14245 Next (Intf);
14246 end loop;
14247 end;
14248 end if;
14250 if Parent_Type = Any_Type
14251 or else Etype (Parent_Type) = Any_Type
14252 or else (Is_Class_Wide_Type (Parent_Type)
14253 and then Etype (Parent_Type) = T)
14254 then
14255 -- If Parent_Type is undefined or illegal, make new type into a
14256 -- subtype of Any_Type, and set a few attributes to prevent cascaded
14257 -- errors. If this is a self-definition, emit error now.
14259 if T = Parent_Type
14260 or else T = Etype (Parent_Type)
14261 then
14262 Error_Msg_N ("type cannot be used in its own definition", Indic);
14263 end if;
14265 Set_Ekind (T, Ekind (Parent_Type));
14266 Set_Etype (T, Any_Type);
14267 Set_Scalar_Range (T, Scalar_Range (Any_Type));
14269 if Is_Tagged_Type (T)
14270 and then Is_Record_Type (T)
14271 then
14272 Set_Direct_Primitive_Operations (T, New_Elmt_List);
14273 end if;
14275 return;
14276 end if;
14278 -- Ada 2005 (AI-251): The case in which the parent of the full-view is
14279 -- an interface is special because the list of interfaces in the full
14280 -- view can be given in any order. For example:
14282 -- type A is interface;
14283 -- type B is interface and A;
14284 -- type D is new B with private;
14285 -- private
14286 -- type D is new A and B with null record; -- 1 --
14288 -- In this case we perform the following transformation of -1-:
14290 -- type D is new B and A with null record;
14292 -- If the parent of the full-view covers the parent of the partial-view
14293 -- we have two possible cases:
14295 -- 1) They have the same parent
14296 -- 2) The parent of the full-view implements some further interfaces
14298 -- In both cases we do not need to perform the transformation. In the
14299 -- first case the source program is correct and the transformation is
14300 -- not needed; in the second case the source program does not fulfill
14301 -- the no-hidden interfaces rule (AI-396) and the error will be reported
14302 -- later.
14304 -- This transformation not only simplifies the rest of the analysis of
14305 -- this type declaration but also simplifies the correct generation of
14306 -- the object layout to the expander.
14308 if In_Private_Part (Current_Scope)
14309 and then Is_Interface (Parent_Type)
14310 then
14311 declare
14312 Iface : Node_Id;
14313 Partial_View : Entity_Id;
14314 Partial_View_Parent : Entity_Id;
14315 New_Iface : Node_Id;
14317 begin
14318 -- Look for the associated private type declaration
14320 Partial_View := First_Entity (Current_Scope);
14321 loop
14322 exit when No (Partial_View)
14323 or else (Has_Private_Declaration (Partial_View)
14324 and then Full_View (Partial_View) = T);
14326 Next_Entity (Partial_View);
14327 end loop;
14329 -- If the partial view was not found then the source code has
14330 -- errors and the transformation is not needed.
14332 if Present (Partial_View) then
14333 Partial_View_Parent := Etype (Partial_View);
14335 -- If the parent of the full-view covers the parent of the
14336 -- partial-view we have nothing else to do.
14338 if Interface_Present_In_Ancestor
14339 (Parent_Type, Partial_View_Parent)
14340 then
14341 null;
14343 -- Traverse the list of interfaces of the full-view to look
14344 -- for the parent of the partial-view and perform the tree
14345 -- transformation.
14347 else
14348 Iface := First (Interface_List (Def));
14349 while Present (Iface) loop
14350 if Etype (Iface) = Etype (Partial_View) then
14351 Rewrite (Subtype_Indication (Def),
14352 New_Copy (Subtype_Indication
14353 (Parent (Partial_View))));
14355 New_Iface :=
14356 Make_Identifier (Sloc (N), Chars (Parent_Type));
14357 Append (New_Iface, Interface_List (Def));
14359 -- Analyze the transformed code
14361 Derived_Type_Declaration (T, N, Is_Completion);
14362 return;
14363 end if;
14365 Next (Iface);
14366 end loop;
14367 end if;
14368 end if;
14369 end;
14370 end if;
14372 -- Only composite types other than array types are allowed to have
14373 -- discriminants. In SPARK, no types are allowed to have discriminants.
14375 if Present (Discriminant_Specifications (N)) then
14376 if (Is_Elementary_Type (Parent_Type)
14377 or else Is_Array_Type (Parent_Type))
14378 and then not Error_Posted (N)
14379 then
14380 Error_Msg_N
14381 ("elementary or array type cannot have discriminants",
14382 Defining_Identifier (First (Discriminant_Specifications (N))));
14383 Set_Has_Discriminants (T, False);
14384 else
14385 Check_SPARK_Restriction ("discriminant type is not allowed", N);
14386 end if;
14387 end if;
14389 -- In Ada 83, a derived type defined in a package specification cannot
14390 -- be used for further derivation until the end of its visible part.
14391 -- Note that derivation in the private part of the package is allowed.
14393 if Ada_Version = Ada_83
14394 and then Is_Derived_Type (Parent_Type)
14395 and then In_Visible_Part (Scope (Parent_Type))
14396 then
14397 if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
14398 Error_Msg_N
14399 ("(Ada 83): premature use of type for derivation", Indic);
14400 end if;
14401 end if;
14403 -- Check for early use of incomplete or private type
14405 if Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
14406 Error_Msg_N ("premature derivation of incomplete type", Indic);
14407 return;
14409 elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
14410 and then not Comes_From_Generic (Parent_Type))
14411 or else Has_Private_Component (Parent_Type)
14412 then
14413 -- The ancestor type of a formal type can be incomplete, in which
14414 -- case only the operations of the partial view are available in the
14415 -- generic. Subsequent checks may be required when the full view is
14416 -- analyzed to verify that a derivation from a tagged type has an
14417 -- extension.
14419 if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
14420 null;
14422 elsif No (Underlying_Type (Parent_Type))
14423 or else Has_Private_Component (Parent_Type)
14424 then
14425 Error_Msg_N
14426 ("premature derivation of derived or private type", Indic);
14428 -- Flag the type itself as being in error, this prevents some
14429 -- nasty problems with subsequent uses of the malformed type.
14431 Set_Error_Posted (T);
14433 -- Check that within the immediate scope of an untagged partial
14434 -- view it's illegal to derive from the partial view if the
14435 -- full view is tagged. (7.3(7))
14437 -- We verify that the Parent_Type is a partial view by checking
14438 -- that it is not a Full_Type_Declaration (i.e. a private type or
14439 -- private extension declaration), to distinguish a partial view
14440 -- from a derivation from a private type which also appears as
14441 -- E_Private_Type. If the parent base type is not declared in an
14442 -- enclosing scope there is no need to check.
14444 elsif Present (Full_View (Parent_Type))
14445 and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
14446 and then not Is_Tagged_Type (Parent_Type)
14447 and then Is_Tagged_Type (Full_View (Parent_Type))
14448 and then In_Open_Scopes (Scope (Base_Type (Parent_Type)))
14449 then
14450 Error_Msg_N
14451 ("premature derivation from type with tagged full view",
14452 Indic);
14453 end if;
14454 end if;
14456 -- Check that form of derivation is appropriate
14458 Taggd := Is_Tagged_Type (Parent_Type);
14460 -- Perhaps the parent type should be changed to the class-wide type's
14461 -- specific type in this case to prevent cascading errors ???
14463 if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
14464 Error_Msg_N ("parent type must not be a class-wide type", Indic);
14465 return;
14466 end if;
14468 if Present (Extension) and then not Taggd then
14469 Error_Msg_N
14470 ("type derived from untagged type cannot have extension", Indic);
14472 elsif No (Extension) and then Taggd then
14474 -- If this declaration is within a private part (or body) of a
14475 -- generic instantiation then the derivation is allowed (the parent
14476 -- type can only appear tagged in this case if it's a generic actual
14477 -- type, since it would otherwise have been rejected in the analysis
14478 -- of the generic template).
14480 if not Is_Generic_Actual_Type (Parent_Type)
14481 or else In_Visible_Part (Scope (Parent_Type))
14482 then
14483 if Is_Class_Wide_Type (Parent_Type) then
14484 Error_Msg_N
14485 ("parent type must not be a class-wide type", Indic);
14487 -- Use specific type to prevent cascaded errors.
14489 Parent_Type := Etype (Parent_Type);
14491 else
14492 Error_Msg_N
14493 ("type derived from tagged type must have extension", Indic);
14494 end if;
14495 end if;
14496 end if;
14498 -- AI-443: Synchronized formal derived types require a private
14499 -- extension. There is no point in checking the ancestor type or
14500 -- the progenitors since the construct is wrong to begin with.
14502 if Ada_Version >= Ada_2005
14503 and then Is_Generic_Type (T)
14504 and then Present (Original_Node (N))
14505 then
14506 declare
14507 Decl : constant Node_Id := Original_Node (N);
14509 begin
14510 if Nkind (Decl) = N_Formal_Type_Declaration
14511 and then Nkind (Formal_Type_Definition (Decl)) =
14512 N_Formal_Derived_Type_Definition
14513 and then Synchronized_Present (Formal_Type_Definition (Decl))
14514 and then No (Extension)
14516 -- Avoid emitting a duplicate error message
14518 and then not Error_Posted (Indic)
14519 then
14520 Error_Msg_N
14521 ("synchronized derived type must have extension", N);
14522 end if;
14523 end;
14524 end if;
14526 if Null_Exclusion_Present (Def)
14527 and then not Is_Access_Type (Parent_Type)
14528 then
14529 Error_Msg_N ("null exclusion can only apply to an access type", N);
14530 end if;
14532 -- Avoid deriving parent primitives of underlying record views
14534 Build_Derived_Type (N, Parent_Type, T, Is_Completion,
14535 Derive_Subps => not Is_Underlying_Record_View (T));
14537 -- AI-419: The parent type of an explicitly limited derived type must
14538 -- be a limited type or a limited interface.
14540 if Limited_Present (Def) then
14541 Set_Is_Limited_Record (T);
14543 if Is_Interface (T) then
14544 Set_Is_Limited_Interface (T);
14545 end if;
14547 if not Is_Limited_Type (Parent_Type)
14548 and then
14549 (not Is_Interface (Parent_Type)
14550 or else not Is_Limited_Interface (Parent_Type))
14551 then
14552 -- AI05-0096: a derivation in the private part of an instance is
14553 -- legal if the generic formal is untagged limited, and the actual
14554 -- is non-limited.
14556 if Is_Generic_Actual_Type (Parent_Type)
14557 and then In_Private_Part (Current_Scope)
14558 and then
14559 not Is_Tagged_Type
14560 (Generic_Parent_Type (Parent (Parent_Type)))
14561 then
14562 null;
14564 else
14565 Error_Msg_NE
14566 ("parent type& of limited type must be limited",
14567 N, Parent_Type);
14568 end if;
14569 end if;
14570 end if;
14572 -- In SPARK, there are no derived type definitions other than type
14573 -- extensions of tagged record types.
14575 if No (Extension) then
14576 Check_SPARK_Restriction ("derived type is not allowed", N);
14577 end if;
14578 end Derived_Type_Declaration;
14580 ------------------------
14581 -- Diagnose_Interface --
14582 ------------------------
14584 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id) is
14585 begin
14586 if not Is_Interface (E)
14587 and then E /= Any_Type
14588 then
14589 Error_Msg_NE ("(Ada 2005) & must be an interface", N, E);
14590 end if;
14591 end Diagnose_Interface;
14593 ----------------------------------
14594 -- Enumeration_Type_Declaration --
14595 ----------------------------------
14597 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
14598 Ev : Uint;
14599 L : Node_Id;
14600 R_Node : Node_Id;
14601 B_Node : Node_Id;
14603 begin
14604 -- Create identifier node representing lower bound
14606 B_Node := New_Node (N_Identifier, Sloc (Def));
14607 L := First (Literals (Def));
14608 Set_Chars (B_Node, Chars (L));
14609 Set_Entity (B_Node, L);
14610 Set_Etype (B_Node, T);
14611 Set_Is_Static_Expression (B_Node, True);
14613 R_Node := New_Node (N_Range, Sloc (Def));
14614 Set_Low_Bound (R_Node, B_Node);
14616 Set_Ekind (T, E_Enumeration_Type);
14617 Set_First_Literal (T, L);
14618 Set_Etype (T, T);
14619 Set_Is_Constrained (T);
14621 Ev := Uint_0;
14623 -- Loop through literals of enumeration type setting pos and rep values
14624 -- except that if the Ekind is already set, then it means the literal
14625 -- was already constructed (case of a derived type declaration and we
14626 -- should not disturb the Pos and Rep values.
14628 while Present (L) loop
14629 if Ekind (L) /= E_Enumeration_Literal then
14630 Set_Ekind (L, E_Enumeration_Literal);
14631 Set_Enumeration_Pos (L, Ev);
14632 Set_Enumeration_Rep (L, Ev);
14633 Set_Is_Known_Valid (L, True);
14634 end if;
14636 Set_Etype (L, T);
14637 New_Overloaded_Entity (L);
14638 Generate_Definition (L);
14639 Set_Convention (L, Convention_Intrinsic);
14641 -- Case of character literal
14643 if Nkind (L) = N_Defining_Character_Literal then
14644 Set_Is_Character_Type (T, True);
14646 -- Check violation of No_Wide_Characters
14648 if Restriction_Check_Required (No_Wide_Characters) then
14649 Get_Name_String (Chars (L));
14651 if Name_Len >= 3 and then Name_Buffer (1 .. 2) = "QW" then
14652 Check_Restriction (No_Wide_Characters, L);
14653 end if;
14654 end if;
14655 end if;
14657 Ev := Ev + 1;
14658 Next (L);
14659 end loop;
14661 -- Now create a node representing upper bound
14663 B_Node := New_Node (N_Identifier, Sloc (Def));
14664 Set_Chars (B_Node, Chars (Last (Literals (Def))));
14665 Set_Entity (B_Node, Last (Literals (Def)));
14666 Set_Etype (B_Node, T);
14667 Set_Is_Static_Expression (B_Node, True);
14669 Set_High_Bound (R_Node, B_Node);
14671 -- Initialize various fields of the type. Some of this information
14672 -- may be overwritten later through rep.clauses.
14674 Set_Scalar_Range (T, R_Node);
14675 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
14676 Set_Enum_Esize (T);
14677 Set_Enum_Pos_To_Rep (T, Empty);
14679 -- Set Discard_Names if configuration pragma set, or if there is
14680 -- a parameterless pragma in the current declarative region
14682 if Global_Discard_Names
14683 or else Discard_Names (Scope (T))
14684 then
14685 Set_Discard_Names (T);
14686 end if;
14688 -- Process end label if there is one
14690 if Present (Def) then
14691 Process_End_Label (Def, 'e', T);
14692 end if;
14693 end Enumeration_Type_Declaration;
14695 ---------------------------------
14696 -- Expand_To_Stored_Constraint --
14697 ---------------------------------
14699 function Expand_To_Stored_Constraint
14700 (Typ : Entity_Id;
14701 Constraint : Elist_Id) return Elist_Id
14703 Explicitly_Discriminated_Type : Entity_Id;
14704 Expansion : Elist_Id;
14705 Discriminant : Entity_Id;
14707 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
14708 -- Find the nearest type that actually specifies discriminants
14710 ---------------------------------
14711 -- Type_With_Explicit_Discrims --
14712 ---------------------------------
14714 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
14715 Typ : constant E := Base_Type (Id);
14717 begin
14718 if Ekind (Typ) in Incomplete_Or_Private_Kind then
14719 if Present (Full_View (Typ)) then
14720 return Type_With_Explicit_Discrims (Full_View (Typ));
14721 end if;
14723 else
14724 if Has_Discriminants (Typ) then
14725 return Typ;
14726 end if;
14727 end if;
14729 if Etype (Typ) = Typ then
14730 return Empty;
14731 elsif Has_Discriminants (Typ) then
14732 return Typ;
14733 else
14734 return Type_With_Explicit_Discrims (Etype (Typ));
14735 end if;
14737 end Type_With_Explicit_Discrims;
14739 -- Start of processing for Expand_To_Stored_Constraint
14741 begin
14742 if No (Constraint)
14743 or else Is_Empty_Elmt_List (Constraint)
14744 then
14745 return No_Elist;
14746 end if;
14748 Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
14750 if No (Explicitly_Discriminated_Type) then
14751 return No_Elist;
14752 end if;
14754 Expansion := New_Elmt_List;
14756 Discriminant :=
14757 First_Stored_Discriminant (Explicitly_Discriminated_Type);
14758 while Present (Discriminant) loop
14759 Append_Elmt (
14760 Get_Discriminant_Value (
14761 Discriminant, Explicitly_Discriminated_Type, Constraint),
14762 Expansion);
14763 Next_Stored_Discriminant (Discriminant);
14764 end loop;
14766 return Expansion;
14767 end Expand_To_Stored_Constraint;
14769 ---------------------------
14770 -- Find_Hidden_Interface --
14771 ---------------------------
14773 function Find_Hidden_Interface
14774 (Src : Elist_Id;
14775 Dest : Elist_Id) return Entity_Id
14777 Iface : Entity_Id;
14778 Iface_Elmt : Elmt_Id;
14780 begin
14781 if Present (Src) and then Present (Dest) then
14782 Iface_Elmt := First_Elmt (Src);
14783 while Present (Iface_Elmt) loop
14784 Iface := Node (Iface_Elmt);
14786 if Is_Interface (Iface)
14787 and then not Contain_Interface (Iface, Dest)
14788 then
14789 return Iface;
14790 end if;
14792 Next_Elmt (Iface_Elmt);
14793 end loop;
14794 end if;
14796 return Empty;
14797 end Find_Hidden_Interface;
14799 --------------------
14800 -- Find_Type_Name --
14801 --------------------
14803 function Find_Type_Name (N : Node_Id) return Entity_Id is
14804 Id : constant Entity_Id := Defining_Identifier (N);
14805 Prev : Entity_Id;
14806 New_Id : Entity_Id;
14807 Prev_Par : Node_Id;
14809 procedure Check_Duplicate_Aspects;
14810 -- Check that aspects specified in a completion have not been specified
14811 -- already in the partial view. Type_Invariant and others can be
14812 -- specified on either view but never on both.
14814 procedure Tag_Mismatch;
14815 -- Diagnose a tagged partial view whose full view is untagged.
14816 -- We post the message on the full view, with a reference to
14817 -- the previous partial view. The partial view can be private
14818 -- or incomplete, and these are handled in a different manner,
14819 -- so we determine the position of the error message from the
14820 -- respective slocs of both.
14822 -----------------------------
14823 -- Check_Duplicate_Aspects --
14824 -----------------------------
14825 procedure Check_Duplicate_Aspects is
14826 Prev_Aspects : constant List_Id := Aspect_Specifications (Prev_Par);
14827 Full_Aspects : constant List_Id := Aspect_Specifications (N);
14828 F_Spec, P_Spec : Node_Id;
14830 begin
14831 if Present (Prev_Aspects) and then Present (Full_Aspects) then
14832 F_Spec := First (Full_Aspects);
14833 while Present (F_Spec) loop
14834 P_Spec := First (Prev_Aspects);
14835 while Present (P_Spec) loop
14837 Chars (Identifier (P_Spec)) = Chars (Identifier (F_Spec))
14838 then
14839 Error_Msg_N
14840 ("aspect already specified in private declaration",
14841 F_Spec);
14842 Remove (F_Spec);
14843 return;
14844 end if;
14846 Next (P_Spec);
14847 end loop;
14849 Next (F_Spec);
14850 end loop;
14851 end if;
14852 end Check_Duplicate_Aspects;
14854 ------------------
14855 -- Tag_Mismatch --
14856 ------------------
14858 procedure Tag_Mismatch is
14859 begin
14860 if Sloc (Prev) < Sloc (Id) then
14861 if Ada_Version >= Ada_2012
14862 and then Nkind (N) = N_Private_Type_Declaration
14863 then
14864 Error_Msg_NE
14865 ("declaration of private } must be a tagged type ", Id, Prev);
14866 else
14867 Error_Msg_NE
14868 ("full declaration of } must be a tagged type ", Id, Prev);
14869 end if;
14870 else
14871 if Ada_Version >= Ada_2012
14872 and then Nkind (N) = N_Private_Type_Declaration
14873 then
14874 Error_Msg_NE
14875 ("declaration of private } must be a tagged type ", Prev, Id);
14876 else
14877 Error_Msg_NE
14878 ("full declaration of } must be a tagged type ", Prev, Id);
14879 end if;
14880 end if;
14881 end Tag_Mismatch;
14883 -- Start of processing for Find_Type_Name
14885 begin
14886 -- Find incomplete declaration, if one was given
14888 Prev := Current_Entity_In_Scope (Id);
14890 -- New type declaration
14892 if No (Prev) then
14893 Enter_Name (Id);
14894 return Id;
14896 -- Previous declaration exists
14898 else
14899 Prev_Par := Parent (Prev);
14901 -- Error if not incomplete/private case except if previous
14902 -- declaration is implicit, etc. Enter_Name will emit error if
14903 -- appropriate.
14905 if not Is_Incomplete_Or_Private_Type (Prev) then
14906 Enter_Name (Id);
14907 New_Id := Id;
14909 -- Check invalid completion of private or incomplete type
14911 elsif not Nkind_In (N, N_Full_Type_Declaration,
14912 N_Task_Type_Declaration,
14913 N_Protected_Type_Declaration)
14914 and then
14915 (Ada_Version < Ada_2012
14916 or else not Is_Incomplete_Type (Prev)
14917 or else not Nkind_In (N, N_Private_Type_Declaration,
14918 N_Private_Extension_Declaration))
14919 then
14920 -- Completion must be a full type declarations (RM 7.3(4))
14922 Error_Msg_Sloc := Sloc (Prev);
14923 Error_Msg_NE ("invalid completion of }", Id, Prev);
14925 -- Set scope of Id to avoid cascaded errors. Entity is never
14926 -- examined again, except when saving globals in generics.
14928 Set_Scope (Id, Current_Scope);
14929 New_Id := Id;
14931 -- If this is a repeated incomplete declaration, no further
14932 -- checks are possible.
14934 if Nkind (N) = N_Incomplete_Type_Declaration then
14935 return Prev;
14936 end if;
14938 -- Case of full declaration of incomplete type
14940 elsif Ekind (Prev) = E_Incomplete_Type
14941 and then (Ada_Version < Ada_2012
14942 or else No (Full_View (Prev))
14943 or else not Is_Private_Type (Full_View (Prev)))
14944 then
14946 -- Indicate that the incomplete declaration has a matching full
14947 -- declaration. The defining occurrence of the incomplete
14948 -- declaration remains the visible one, and the procedure
14949 -- Get_Full_View dereferences it whenever the type is used.
14951 if Present (Full_View (Prev)) then
14952 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
14953 end if;
14955 Set_Full_View (Prev, Id);
14956 Append_Entity (Id, Current_Scope);
14957 Set_Is_Public (Id, Is_Public (Prev));
14958 Set_Is_Internal (Id);
14959 New_Id := Prev;
14961 -- If the incomplete view is tagged, a class_wide type has been
14962 -- created already. Use it for the private type as well, in order
14963 -- to prevent multiple incompatible class-wide types that may be
14964 -- created for self-referential anonymous access components.
14966 if Is_Tagged_Type (Prev)
14967 and then Present (Class_Wide_Type (Prev))
14968 then
14969 Set_Ekind (Id, Ekind (Prev)); -- will be reset later
14970 Set_Class_Wide_Type (Id, Class_Wide_Type (Prev));
14972 -- If the incomplete type is completed by a private declaration
14973 -- the class-wide type remains associated with the incomplete
14974 -- type, to prevent order-of-elaboration issues in gigi, else
14975 -- we associate the class-wide type with the known full view.
14977 if Nkind (N) /= N_Private_Type_Declaration then
14978 Set_Etype (Class_Wide_Type (Id), Id);
14979 end if;
14980 end if;
14982 -- Case of full declaration of private type
14984 else
14985 -- If the private type was a completion of an incomplete type then
14986 -- update Prev to reference the private type
14988 if Ada_Version >= Ada_2012
14989 and then Ekind (Prev) = E_Incomplete_Type
14990 and then Present (Full_View (Prev))
14991 and then Is_Private_Type (Full_View (Prev))
14992 then
14993 Prev := Full_View (Prev);
14994 Prev_Par := Parent (Prev);
14995 end if;
14997 if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
14998 if Etype (Prev) /= Prev then
15000 -- Prev is a private subtype or a derived type, and needs
15001 -- no completion.
15003 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
15004 New_Id := Id;
15006 elsif Ekind (Prev) = E_Private_Type
15007 and then Nkind_In (N, N_Task_Type_Declaration,
15008 N_Protected_Type_Declaration)
15009 then
15010 Error_Msg_N
15011 ("completion of nonlimited type cannot be limited", N);
15013 elsif Ekind (Prev) = E_Record_Type_With_Private
15014 and then Nkind_In (N, N_Task_Type_Declaration,
15015 N_Protected_Type_Declaration)
15016 then
15017 if not Is_Limited_Record (Prev) then
15018 Error_Msg_N
15019 ("completion of nonlimited type cannot be limited", N);
15021 elsif No (Interface_List (N)) then
15022 Error_Msg_N
15023 ("completion of tagged private type must be tagged",
15025 end if;
15027 elsif Nkind (N) = N_Full_Type_Declaration
15028 and then
15029 Nkind (Type_Definition (N)) = N_Record_Definition
15030 and then Interface_Present (Type_Definition (N))
15031 then
15032 Error_Msg_N
15033 ("completion of private type cannot be an interface", N);
15034 end if;
15036 -- Ada 2005 (AI-251): Private extension declaration of a task
15037 -- type or a protected type. This case arises when covering
15038 -- interface types.
15040 elsif Nkind_In (N, N_Task_Type_Declaration,
15041 N_Protected_Type_Declaration)
15042 then
15043 null;
15045 elsif Nkind (N) /= N_Full_Type_Declaration
15046 or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
15047 then
15048 Error_Msg_N
15049 ("full view of private extension must be an extension", N);
15051 elsif not (Abstract_Present (Parent (Prev)))
15052 and then Abstract_Present (Type_Definition (N))
15053 then
15054 Error_Msg_N
15055 ("full view of non-abstract extension cannot be abstract", N);
15056 end if;
15058 if not In_Private_Part (Current_Scope) then
15059 Error_Msg_N
15060 ("declaration of full view must appear in private part", N);
15061 end if;
15063 if Ada_Version >= Ada_2012 then
15064 Check_Duplicate_Aspects;
15065 end if;
15067 Copy_And_Swap (Prev, Id);
15068 Set_Has_Private_Declaration (Prev);
15069 Set_Has_Private_Declaration (Id);
15071 -- Preserve aspect and iterator flags that may have been set on
15072 -- the partial view.
15074 Set_Has_Delayed_Aspects (Prev, Has_Delayed_Aspects (Id));
15075 Set_Has_Implicit_Dereference (Prev, Has_Implicit_Dereference (Id));
15077 -- If no error, propagate freeze_node from private to full view.
15078 -- It may have been generated for an early operational item.
15080 if Present (Freeze_Node (Id))
15081 and then Serious_Errors_Detected = 0
15082 and then No (Full_View (Id))
15083 then
15084 Set_Freeze_Node (Prev, Freeze_Node (Id));
15085 Set_Freeze_Node (Id, Empty);
15086 Set_First_Rep_Item (Prev, First_Rep_Item (Id));
15087 end if;
15089 Set_Full_View (Id, Prev);
15090 New_Id := Prev;
15091 end if;
15093 -- Verify that full declaration conforms to partial one
15095 if Is_Incomplete_Or_Private_Type (Prev)
15096 and then Present (Discriminant_Specifications (Prev_Par))
15097 then
15098 if Present (Discriminant_Specifications (N)) then
15099 if Ekind (Prev) = E_Incomplete_Type then
15100 Check_Discriminant_Conformance (N, Prev, Prev);
15101 else
15102 Check_Discriminant_Conformance (N, Prev, Id);
15103 end if;
15105 else
15106 Error_Msg_N
15107 ("missing discriminants in full type declaration", N);
15109 -- To avoid cascaded errors on subsequent use, share the
15110 -- discriminants of the partial view.
15112 Set_Discriminant_Specifications (N,
15113 Discriminant_Specifications (Prev_Par));
15114 end if;
15115 end if;
15117 -- A prior untagged partial view can have an associated class-wide
15118 -- type due to use of the class attribute, and in this case the full
15119 -- type must also be tagged. This Ada 95 usage is deprecated in favor
15120 -- of incomplete tagged declarations, but we check for it.
15122 if Is_Type (Prev)
15123 and then (Is_Tagged_Type (Prev)
15124 or else Present (Class_Wide_Type (Prev)))
15125 then
15126 -- Ada 2012 (AI05-0162): A private type may be the completion of
15127 -- an incomplete type
15129 if Ada_Version >= Ada_2012
15130 and then Is_Incomplete_Type (Prev)
15131 and then Nkind_In (N, N_Private_Type_Declaration,
15132 N_Private_Extension_Declaration)
15133 then
15134 -- No need to check private extensions since they are tagged
15136 if Nkind (N) = N_Private_Type_Declaration
15137 and then not Tagged_Present (N)
15138 then
15139 Tag_Mismatch;
15140 end if;
15142 -- The full declaration is either a tagged type (including
15143 -- a synchronized type that implements interfaces) or a
15144 -- type extension, otherwise this is an error.
15146 elsif Nkind_In (N, N_Task_Type_Declaration,
15147 N_Protected_Type_Declaration)
15148 then
15149 if No (Interface_List (N))
15150 and then not Error_Posted (N)
15151 then
15152 Tag_Mismatch;
15153 end if;
15155 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
15157 -- Indicate that the previous declaration (tagged incomplete
15158 -- or private declaration) requires the same on the full one.
15160 if not Tagged_Present (Type_Definition (N)) then
15161 Tag_Mismatch;
15162 Set_Is_Tagged_Type (Id);
15163 end if;
15165 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
15166 if No (Record_Extension_Part (Type_Definition (N))) then
15167 Error_Msg_NE
15168 ("full declaration of } must be a record extension",
15169 Prev, Id);
15171 -- Set some attributes to produce a usable full view
15173 Set_Is_Tagged_Type (Id);
15174 end if;
15176 else
15177 Tag_Mismatch;
15178 end if;
15179 end if;
15181 if Present (Prev)
15182 and then Nkind (Parent (Prev)) = N_Incomplete_Type_Declaration
15183 and then Present (Premature_Use (Parent (Prev)))
15184 then
15185 Error_Msg_Sloc := Sloc (N);
15186 Error_Msg_N
15187 ("\full declaration #", Premature_Use (Parent (Prev)));
15188 end if;
15190 return New_Id;
15191 end if;
15192 end Find_Type_Name;
15194 -------------------------
15195 -- Find_Type_Of_Object --
15196 -------------------------
15198 function Find_Type_Of_Object
15199 (Obj_Def : Node_Id;
15200 Related_Nod : Node_Id) return Entity_Id
15202 Def_Kind : constant Node_Kind := Nkind (Obj_Def);
15203 P : Node_Id := Parent (Obj_Def);
15204 T : Entity_Id;
15205 Nam : Name_Id;
15207 begin
15208 -- If the parent is a component_definition node we climb to the
15209 -- component_declaration node
15211 if Nkind (P) = N_Component_Definition then
15212 P := Parent (P);
15213 end if;
15215 -- Case of an anonymous array subtype
15217 if Nkind_In (Def_Kind, N_Constrained_Array_Definition,
15218 N_Unconstrained_Array_Definition)
15219 then
15220 T := Empty;
15221 Array_Type_Declaration (T, Obj_Def);
15223 -- Create an explicit subtype whenever possible
15225 elsif Nkind (P) /= N_Component_Declaration
15226 and then Def_Kind = N_Subtype_Indication
15227 then
15228 -- Base name of subtype on object name, which will be unique in
15229 -- the current scope.
15231 -- If this is a duplicate declaration, return base type, to avoid
15232 -- generating duplicate anonymous types.
15234 if Error_Posted (P) then
15235 Analyze (Subtype_Mark (Obj_Def));
15236 return Entity (Subtype_Mark (Obj_Def));
15237 end if;
15239 Nam :=
15240 New_External_Name
15241 (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
15243 T := Make_Defining_Identifier (Sloc (P), Nam);
15245 Insert_Action (Obj_Def,
15246 Make_Subtype_Declaration (Sloc (P),
15247 Defining_Identifier => T,
15248 Subtype_Indication => Relocate_Node (Obj_Def)));
15250 -- This subtype may need freezing, and this will not be done
15251 -- automatically if the object declaration is not in declarative
15252 -- part. Since this is an object declaration, the type cannot always
15253 -- be frozen here. Deferred constants do not freeze their type
15254 -- (which often enough will be private).
15256 if Nkind (P) = N_Object_Declaration
15257 and then Constant_Present (P)
15258 and then No (Expression (P))
15259 then
15260 null;
15261 else
15262 Insert_Actions (Obj_Def, Freeze_Entity (T, P));
15263 end if;
15265 -- Ada 2005 AI-406: the object definition in an object declaration
15266 -- can be an access definition.
15268 elsif Def_Kind = N_Access_Definition then
15269 T := Access_Definition (Related_Nod, Obj_Def);
15271 Set_Is_Local_Anonymous_Access
15273 V => (Ada_Version < Ada_2012)
15274 or else (Nkind (P) /= N_Object_Declaration)
15275 or else Is_Library_Level_Entity (Defining_Identifier (P)));
15277 -- Otherwise, the object definition is just a subtype_mark
15279 else
15280 T := Process_Subtype (Obj_Def, Related_Nod);
15282 -- If expansion is disabled an object definition that is an aggregate
15283 -- will not get expanded and may lead to scoping problems in the back
15284 -- end, if the object is referenced in an inner scope. In that case
15285 -- create an itype reference for the object definition now. This
15286 -- may be redundant in some cases, but harmless.
15288 if Is_Itype (T)
15289 and then Nkind (Related_Nod) = N_Object_Declaration
15290 and then ASIS_Mode
15291 then
15292 Build_Itype_Reference (T, Related_Nod);
15293 end if;
15294 end if;
15296 return T;
15297 end Find_Type_Of_Object;
15299 --------------------------------
15300 -- Find_Type_Of_Subtype_Indic --
15301 --------------------------------
15303 function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
15304 Typ : Entity_Id;
15306 begin
15307 -- Case of subtype mark with a constraint
15309 if Nkind (S) = N_Subtype_Indication then
15310 Find_Type (Subtype_Mark (S));
15311 Typ := Entity (Subtype_Mark (S));
15313 if not
15314 Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
15315 then
15316 Error_Msg_N
15317 ("incorrect constraint for this kind of type", Constraint (S));
15318 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
15319 end if;
15321 -- Otherwise we have a subtype mark without a constraint
15323 elsif Error_Posted (S) then
15324 Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
15325 return Any_Type;
15327 else
15328 Find_Type (S);
15329 Typ := Entity (S);
15330 end if;
15332 -- Check No_Wide_Characters restriction
15334 Check_Wide_Character_Restriction (Typ, S);
15336 return Typ;
15337 end Find_Type_Of_Subtype_Indic;
15339 -------------------------------------
15340 -- Floating_Point_Type_Declaration --
15341 -------------------------------------
15343 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
15344 Digs : constant Node_Id := Digits_Expression (Def);
15345 Max_Digs_Val : constant Uint := Digits_Value (Standard_Long_Long_Float);
15346 Digs_Val : Uint;
15347 Base_Typ : Entity_Id;
15348 Implicit_Base : Entity_Id;
15349 Bound : Node_Id;
15351 function Can_Derive_From (E : Entity_Id) return Boolean;
15352 -- Find if given digits value, and possibly a specified range, allows
15353 -- derivation from specified type
15355 function Find_Base_Type return Entity_Id;
15356 -- Find a predefined base type that Def can derive from, or generate
15357 -- an error and substitute Long_Long_Float if none exists.
15359 ---------------------
15360 -- Can_Derive_From --
15361 ---------------------
15363 function Can_Derive_From (E : Entity_Id) return Boolean is
15364 Spec : constant Entity_Id := Real_Range_Specification (Def);
15366 begin
15367 -- Check specified "digits" constraint
15369 if Digs_Val > Digits_Value (E) then
15370 return False;
15371 end if;
15373 -- Avoid types not matching pragma Float_Representation, if present
15375 if (Opt.Float_Format = 'I' and then Float_Rep (E) /= IEEE_Binary)
15376 or else
15377 (Opt.Float_Format = 'V' and then Float_Rep (E) /= VAX_Native)
15378 then
15379 return False;
15380 end if;
15382 -- Check for matching range, if specified
15384 if Present (Spec) then
15385 if Expr_Value_R (Type_Low_Bound (E)) >
15386 Expr_Value_R (Low_Bound (Spec))
15387 then
15388 return False;
15389 end if;
15391 if Expr_Value_R (Type_High_Bound (E)) <
15392 Expr_Value_R (High_Bound (Spec))
15393 then
15394 return False;
15395 end if;
15396 end if;
15398 return True;
15399 end Can_Derive_From;
15401 --------------------
15402 -- Find_Base_Type --
15403 --------------------
15405 function Find_Base_Type return Entity_Id is
15406 Choice : Elmt_Id := First_Elmt (Predefined_Float_Types);
15408 begin
15409 -- Iterate over the predefined types in order, returning the first
15410 -- one that Def can derive from.
15412 while Present (Choice) loop
15413 if Can_Derive_From (Node (Choice)) then
15414 return Node (Choice);
15415 end if;
15417 Next_Elmt (Choice);
15418 end loop;
15420 -- If we can't derive from any existing type, use Long_Long_Float
15421 -- and give appropriate message explaining the problem.
15423 if Digs_Val > Max_Digs_Val then
15424 -- It might be the case that there is a type with the requested
15425 -- range, just not the combination of digits and range.
15427 Error_Msg_N
15428 ("no predefined type has requested range and precision",
15429 Real_Range_Specification (Def));
15431 else
15432 Error_Msg_N
15433 ("range too large for any predefined type",
15434 Real_Range_Specification (Def));
15435 end if;
15437 return Standard_Long_Long_Float;
15438 end Find_Base_Type;
15440 -- Start of processing for Floating_Point_Type_Declaration
15442 begin
15443 Check_Restriction (No_Floating_Point, Def);
15445 -- Create an implicit base type
15447 Implicit_Base :=
15448 Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
15450 -- Analyze and verify digits value
15452 Analyze_And_Resolve (Digs, Any_Integer);
15453 Check_Digits_Expression (Digs);
15454 Digs_Val := Expr_Value (Digs);
15456 -- Process possible range spec and find correct type to derive from
15458 Process_Real_Range_Specification (Def);
15460 -- Check that requested number of digits is not too high.
15462 if Digs_Val > Max_Digs_Val then
15463 -- The check for Max_Base_Digits may be somewhat expensive, as it
15464 -- requires reading System, so only do it when necessary.
15466 declare
15467 Max_Base_Digits : constant Uint :=
15468 Expr_Value
15469 (Expression
15470 (Parent (RTE (RE_Max_Base_Digits))));
15472 begin
15473 if Digs_Val > Max_Base_Digits then
15474 Error_Msg_Uint_1 := Max_Base_Digits;
15475 Error_Msg_N ("digits value out of range, maximum is ^", Digs);
15477 elsif No (Real_Range_Specification (Def)) then
15478 Error_Msg_Uint_1 := Max_Digs_Val;
15479 Error_Msg_N ("types with more than ^ digits need range spec "
15480 & "(RM 3.5.7(6))", Digs);
15481 end if;
15482 end;
15483 end if;
15485 -- Find a suitable type to derive from or complain and use a substitute
15487 Base_Typ := Find_Base_Type;
15489 -- If there are bounds given in the declaration use them as the bounds
15490 -- of the type, otherwise use the bounds of the predefined base type
15491 -- that was chosen based on the Digits value.
15493 if Present (Real_Range_Specification (Def)) then
15494 Set_Scalar_Range (T, Real_Range_Specification (Def));
15495 Set_Is_Constrained (T);
15497 -- The bounds of this range must be converted to machine numbers
15498 -- in accordance with RM 4.9(38).
15500 Bound := Type_Low_Bound (T);
15502 if Nkind (Bound) = N_Real_Literal then
15503 Set_Realval
15504 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
15505 Set_Is_Machine_Number (Bound);
15506 end if;
15508 Bound := Type_High_Bound (T);
15510 if Nkind (Bound) = N_Real_Literal then
15511 Set_Realval
15512 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
15513 Set_Is_Machine_Number (Bound);
15514 end if;
15516 else
15517 Set_Scalar_Range (T, Scalar_Range (Base_Typ));
15518 end if;
15520 -- Complete definition of implicit base and declared first subtype
15522 Set_Etype (Implicit_Base, Base_Typ);
15524 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
15525 Set_Size_Info (Implicit_Base, (Base_Typ));
15526 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
15527 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
15528 Set_Digits_Value (Implicit_Base, Digits_Value (Base_Typ));
15529 Set_Float_Rep (Implicit_Base, Float_Rep (Base_Typ));
15531 Set_Ekind (T, E_Floating_Point_Subtype);
15532 Set_Etype (T, Implicit_Base);
15534 Set_Size_Info (T, (Implicit_Base));
15535 Set_RM_Size (T, RM_Size (Implicit_Base));
15536 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
15537 Set_Digits_Value (T, Digs_Val);
15538 end Floating_Point_Type_Declaration;
15540 ----------------------------
15541 -- Get_Discriminant_Value --
15542 ----------------------------
15544 -- This is the situation:
15546 -- There is a non-derived type
15548 -- type T0 (Dx, Dy, Dz...)
15550 -- There are zero or more levels of derivation, with each derivation
15551 -- either purely inheriting the discriminants, or defining its own.
15553 -- type Ti is new Ti-1
15554 -- or
15555 -- type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
15556 -- or
15557 -- subtype Ti is ...
15559 -- The subtype issue is avoided by the use of Original_Record_Component,
15560 -- and the fact that derived subtypes also derive the constraints.
15562 -- This chain leads back from
15564 -- Typ_For_Constraint
15566 -- Typ_For_Constraint has discriminants, and the value for each
15567 -- discriminant is given by its corresponding Elmt of Constraints.
15569 -- Discriminant is some discriminant in this hierarchy
15571 -- We need to return its value
15573 -- We do this by recursively searching each level, and looking for
15574 -- Discriminant. Once we get to the bottom, we start backing up
15575 -- returning the value for it which may in turn be a discriminant
15576 -- further up, so on the backup we continue the substitution.
15578 function Get_Discriminant_Value
15579 (Discriminant : Entity_Id;
15580 Typ_For_Constraint : Entity_Id;
15581 Constraint : Elist_Id) return Node_Id
15583 function Root_Corresponding_Discriminant
15584 (Discr : Entity_Id) return Entity_Id;
15585 -- Given a discriminant, traverse the chain of inherited discriminants
15586 -- and return the topmost discriminant.
15588 function Search_Derivation_Levels
15589 (Ti : Entity_Id;
15590 Discrim_Values : Elist_Id;
15591 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
15592 -- This is the routine that performs the recursive search of levels
15593 -- as described above.
15595 -------------------------------------
15596 -- Root_Corresponding_Discriminant --
15597 -------------------------------------
15599 function Root_Corresponding_Discriminant
15600 (Discr : Entity_Id) return Entity_Id
15602 D : Entity_Id;
15604 begin
15605 D := Discr;
15606 while Present (Corresponding_Discriminant (D)) loop
15607 D := Corresponding_Discriminant (D);
15608 end loop;
15610 return D;
15611 end Root_Corresponding_Discriminant;
15613 ------------------------------
15614 -- Search_Derivation_Levels --
15615 ------------------------------
15617 function Search_Derivation_Levels
15618 (Ti : Entity_Id;
15619 Discrim_Values : Elist_Id;
15620 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
15622 Assoc : Elmt_Id;
15623 Disc : Entity_Id;
15624 Result : Node_Or_Entity_Id;
15625 Result_Entity : Node_Id;
15627 begin
15628 -- If inappropriate type, return Error, this happens only in
15629 -- cascaded error situations, and we want to avoid a blow up.
15631 if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
15632 return Error;
15633 end if;
15635 -- Look deeper if possible. Use Stored_Constraints only for
15636 -- untagged types. For tagged types use the given constraint.
15637 -- This asymmetry needs explanation???
15639 if not Stored_Discrim_Values
15640 and then Present (Stored_Constraint (Ti))
15641 and then not Is_Tagged_Type (Ti)
15642 then
15643 Result :=
15644 Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
15645 else
15646 declare
15647 Td : constant Entity_Id := Etype (Ti);
15649 begin
15650 if Td = Ti then
15651 Result := Discriminant;
15653 else
15654 if Present (Stored_Constraint (Ti)) then
15655 Result :=
15656 Search_Derivation_Levels
15657 (Td, Stored_Constraint (Ti), True);
15658 else
15659 Result :=
15660 Search_Derivation_Levels
15661 (Td, Discrim_Values, Stored_Discrim_Values);
15662 end if;
15663 end if;
15664 end;
15665 end if;
15667 -- Extra underlying places to search, if not found above. For
15668 -- concurrent types, the relevant discriminant appears in the
15669 -- corresponding record. For a type derived from a private type
15670 -- without discriminant, the full view inherits the discriminants
15671 -- of the full view of the parent.
15673 if Result = Discriminant then
15674 if Is_Concurrent_Type (Ti)
15675 and then Present (Corresponding_Record_Type (Ti))
15676 then
15677 Result :=
15678 Search_Derivation_Levels (
15679 Corresponding_Record_Type (Ti),
15680 Discrim_Values,
15681 Stored_Discrim_Values);
15683 elsif Is_Private_Type (Ti)
15684 and then not Has_Discriminants (Ti)
15685 and then Present (Full_View (Ti))
15686 and then Etype (Full_View (Ti)) /= Ti
15687 then
15688 Result :=
15689 Search_Derivation_Levels (
15690 Full_View (Ti),
15691 Discrim_Values,
15692 Stored_Discrim_Values);
15693 end if;
15694 end if;
15696 -- If Result is not a (reference to a) discriminant, return it,
15697 -- otherwise set Result_Entity to the discriminant.
15699 if Nkind (Result) = N_Defining_Identifier then
15700 pragma Assert (Result = Discriminant);
15701 Result_Entity := Result;
15703 else
15704 if not Denotes_Discriminant (Result) then
15705 return Result;
15706 end if;
15708 Result_Entity := Entity (Result);
15709 end if;
15711 -- See if this level of derivation actually has discriminants
15712 -- because tagged derivations can add them, hence the lower
15713 -- levels need not have any.
15715 if not Has_Discriminants (Ti) then
15716 return Result;
15717 end if;
15719 -- Scan Ti's discriminants for Result_Entity,
15720 -- and return its corresponding value, if any.
15722 Result_Entity := Original_Record_Component (Result_Entity);
15724 Assoc := First_Elmt (Discrim_Values);
15726 if Stored_Discrim_Values then
15727 Disc := First_Stored_Discriminant (Ti);
15728 else
15729 Disc := First_Discriminant (Ti);
15730 end if;
15732 while Present (Disc) loop
15733 pragma Assert (Present (Assoc));
15735 if Original_Record_Component (Disc) = Result_Entity then
15736 return Node (Assoc);
15737 end if;
15739 Next_Elmt (Assoc);
15741 if Stored_Discrim_Values then
15742 Next_Stored_Discriminant (Disc);
15743 else
15744 Next_Discriminant (Disc);
15745 end if;
15746 end loop;
15748 -- Could not find it
15750 return Result;
15751 end Search_Derivation_Levels;
15753 -- Local Variables
15755 Result : Node_Or_Entity_Id;
15757 -- Start of processing for Get_Discriminant_Value
15759 begin
15760 -- ??? This routine is a gigantic mess and will be deleted. For the
15761 -- time being just test for the trivial case before calling recurse.
15763 if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
15764 declare
15765 D : Entity_Id;
15766 E : Elmt_Id;
15768 begin
15769 D := First_Discriminant (Typ_For_Constraint);
15770 E := First_Elmt (Constraint);
15771 while Present (D) loop
15772 if Chars (D) = Chars (Discriminant) then
15773 return Node (E);
15774 end if;
15776 Next_Discriminant (D);
15777 Next_Elmt (E);
15778 end loop;
15779 end;
15780 end if;
15782 Result := Search_Derivation_Levels
15783 (Typ_For_Constraint, Constraint, False);
15785 -- ??? hack to disappear when this routine is gone
15787 if Nkind (Result) = N_Defining_Identifier then
15788 declare
15789 D : Entity_Id;
15790 E : Elmt_Id;
15792 begin
15793 D := First_Discriminant (Typ_For_Constraint);
15794 E := First_Elmt (Constraint);
15795 while Present (D) loop
15796 if Root_Corresponding_Discriminant (D) = Discriminant then
15797 return Node (E);
15798 end if;
15800 Next_Discriminant (D);
15801 Next_Elmt (E);
15802 end loop;
15803 end;
15804 end if;
15806 pragma Assert (Nkind (Result) /= N_Defining_Identifier);
15807 return Result;
15808 end Get_Discriminant_Value;
15810 --------------------------
15811 -- Has_Range_Constraint --
15812 --------------------------
15814 function Has_Range_Constraint (N : Node_Id) return Boolean is
15815 C : constant Node_Id := Constraint (N);
15817 begin
15818 if Nkind (C) = N_Range_Constraint then
15819 return True;
15821 elsif Nkind (C) = N_Digits_Constraint then
15822 return
15823 Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
15824 or else
15825 Present (Range_Constraint (C));
15827 elsif Nkind (C) = N_Delta_Constraint then
15828 return Present (Range_Constraint (C));
15830 else
15831 return False;
15832 end if;
15833 end Has_Range_Constraint;
15835 ------------------------
15836 -- Inherit_Components --
15837 ------------------------
15839 function Inherit_Components
15840 (N : Node_Id;
15841 Parent_Base : Entity_Id;
15842 Derived_Base : Entity_Id;
15843 Is_Tagged : Boolean;
15844 Inherit_Discr : Boolean;
15845 Discs : Elist_Id) return Elist_Id
15847 Assoc_List : constant Elist_Id := New_Elmt_List;
15849 procedure Inherit_Component
15850 (Old_C : Entity_Id;
15851 Plain_Discrim : Boolean := False;
15852 Stored_Discrim : Boolean := False);
15853 -- Inherits component Old_C from Parent_Base to the Derived_Base. If
15854 -- Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
15855 -- True, Old_C is a stored discriminant. If they are both false then
15856 -- Old_C is a regular component.
15858 -----------------------
15859 -- Inherit_Component --
15860 -----------------------
15862 procedure Inherit_Component
15863 (Old_C : Entity_Id;
15864 Plain_Discrim : Boolean := False;
15865 Stored_Discrim : Boolean := False)
15867 procedure Set_Anonymous_Type (Id : Entity_Id);
15868 -- Id denotes the entity of an access discriminant or anonymous
15869 -- access component. Set the type of Id to either the same type of
15870 -- Old_C or create a new one depending on whether the parent and
15871 -- the child types are in the same scope.
15873 ------------------------
15874 -- Set_Anonymous_Type --
15875 ------------------------
15877 procedure Set_Anonymous_Type (Id : Entity_Id) is
15878 Old_Typ : constant Entity_Id := Etype (Old_C);
15880 begin
15881 if Scope (Parent_Base) = Scope (Derived_Base) then
15882 Set_Etype (Id, Old_Typ);
15884 -- The parent and the derived type are in two different scopes.
15885 -- Reuse the type of the original discriminant / component by
15886 -- copying it in order to preserve all attributes.
15888 else
15889 declare
15890 Typ : constant Entity_Id := New_Copy (Old_Typ);
15892 begin
15893 Set_Etype (Id, Typ);
15895 -- Since we do not generate component declarations for
15896 -- inherited components, associate the itype with the
15897 -- derived type.
15899 Set_Associated_Node_For_Itype (Typ, Parent (Derived_Base));
15900 Set_Scope (Typ, Derived_Base);
15901 end;
15902 end if;
15903 end Set_Anonymous_Type;
15905 -- Local variables and constants
15907 New_C : constant Entity_Id := New_Copy (Old_C);
15909 Corr_Discrim : Entity_Id;
15910 Discrim : Entity_Id;
15912 -- Start of processing for Inherit_Component
15914 begin
15915 pragma Assert (not Is_Tagged or else not Stored_Discrim);
15917 Set_Parent (New_C, Parent (Old_C));
15919 -- Regular discriminants and components must be inserted in the scope
15920 -- of the Derived_Base. Do it here.
15922 if not Stored_Discrim then
15923 Enter_Name (New_C);
15924 end if;
15926 -- For tagged types the Original_Record_Component must point to
15927 -- whatever this field was pointing to in the parent type. This has
15928 -- already been achieved by the call to New_Copy above.
15930 if not Is_Tagged then
15931 Set_Original_Record_Component (New_C, New_C);
15932 end if;
15934 -- Set the proper type of an access discriminant
15936 if Ekind (New_C) = E_Discriminant
15937 and then Ekind (Etype (New_C)) = E_Anonymous_Access_Type
15938 then
15939 Set_Anonymous_Type (New_C);
15940 end if;
15942 -- If we have inherited a component then see if its Etype contains
15943 -- references to Parent_Base discriminants. In this case, replace
15944 -- these references with the constraints given in Discs. We do not
15945 -- do this for the partial view of private types because this is
15946 -- not needed (only the components of the full view will be used
15947 -- for code generation) and cause problem. We also avoid this
15948 -- transformation in some error situations.
15950 if Ekind (New_C) = E_Component then
15952 -- Set the proper type of an anonymous access component
15954 if Ekind (Etype (New_C)) = E_Anonymous_Access_Type then
15955 Set_Anonymous_Type (New_C);
15957 elsif (Is_Private_Type (Derived_Base)
15958 and then not Is_Generic_Type (Derived_Base))
15959 or else (Is_Empty_Elmt_List (Discs)
15960 and then not Expander_Active)
15961 then
15962 Set_Etype (New_C, Etype (Old_C));
15964 else
15965 -- The current component introduces a circularity of the
15966 -- following kind:
15968 -- limited with Pack_2;
15969 -- package Pack_1 is
15970 -- type T_1 is tagged record
15971 -- Comp : access Pack_2.T_2;
15972 -- ...
15973 -- end record;
15974 -- end Pack_1;
15976 -- with Pack_1;
15977 -- package Pack_2 is
15978 -- type T_2 is new Pack_1.T_1 with ...;
15979 -- end Pack_2;
15981 Set_Etype
15982 (New_C,
15983 Constrain_Component_Type
15984 (Old_C, Derived_Base, N, Parent_Base, Discs));
15985 end if;
15986 end if;
15988 -- In derived tagged types it is illegal to reference a non
15989 -- discriminant component in the parent type. To catch this, mark
15990 -- these components with an Ekind of E_Void. This will be reset in
15991 -- Record_Type_Definition after processing the record extension of
15992 -- the derived type.
15994 -- If the declaration is a private extension, there is no further
15995 -- record extension to process, and the components retain their
15996 -- current kind, because they are visible at this point.
15998 if Is_Tagged and then Ekind (New_C) = E_Component
15999 and then Nkind (N) /= N_Private_Extension_Declaration
16000 then
16001 Set_Ekind (New_C, E_Void);
16002 end if;
16004 if Plain_Discrim then
16005 Set_Corresponding_Discriminant (New_C, Old_C);
16006 Build_Discriminal (New_C);
16008 -- If we are explicitly inheriting a stored discriminant it will be
16009 -- completely hidden.
16011 elsif Stored_Discrim then
16012 Set_Corresponding_Discriminant (New_C, Empty);
16013 Set_Discriminal (New_C, Empty);
16014 Set_Is_Completely_Hidden (New_C);
16016 -- Set the Original_Record_Component of each discriminant in the
16017 -- derived base to point to the corresponding stored that we just
16018 -- created.
16020 Discrim := First_Discriminant (Derived_Base);
16021 while Present (Discrim) loop
16022 Corr_Discrim := Corresponding_Discriminant (Discrim);
16024 -- Corr_Discrim could be missing in an error situation
16026 if Present (Corr_Discrim)
16027 and then Original_Record_Component (Corr_Discrim) = Old_C
16028 then
16029 Set_Original_Record_Component (Discrim, New_C);
16030 end if;
16032 Next_Discriminant (Discrim);
16033 end loop;
16035 Append_Entity (New_C, Derived_Base);
16036 end if;
16038 if not Is_Tagged then
16039 Append_Elmt (Old_C, Assoc_List);
16040 Append_Elmt (New_C, Assoc_List);
16041 end if;
16042 end Inherit_Component;
16044 -- Variables local to Inherit_Component
16046 Loc : constant Source_Ptr := Sloc (N);
16048 Parent_Discrim : Entity_Id;
16049 Stored_Discrim : Entity_Id;
16050 D : Entity_Id;
16051 Component : Entity_Id;
16053 -- Start of processing for Inherit_Components
16055 begin
16056 if not Is_Tagged then
16057 Append_Elmt (Parent_Base, Assoc_List);
16058 Append_Elmt (Derived_Base, Assoc_List);
16059 end if;
16061 -- Inherit parent discriminants if needed
16063 if Inherit_Discr then
16064 Parent_Discrim := First_Discriminant (Parent_Base);
16065 while Present (Parent_Discrim) loop
16066 Inherit_Component (Parent_Discrim, Plain_Discrim => True);
16067 Next_Discriminant (Parent_Discrim);
16068 end loop;
16069 end if;
16071 -- Create explicit stored discrims for untagged types when necessary
16073 if not Has_Unknown_Discriminants (Derived_Base)
16074 and then Has_Discriminants (Parent_Base)
16075 and then not Is_Tagged
16076 and then
16077 (not Inherit_Discr
16078 or else First_Discriminant (Parent_Base) /=
16079 First_Stored_Discriminant (Parent_Base))
16080 then
16081 Stored_Discrim := First_Stored_Discriminant (Parent_Base);
16082 while Present (Stored_Discrim) loop
16083 Inherit_Component (Stored_Discrim, Stored_Discrim => True);
16084 Next_Stored_Discriminant (Stored_Discrim);
16085 end loop;
16086 end if;
16088 -- See if we can apply the second transformation for derived types, as
16089 -- explained in point 6. in the comments above Build_Derived_Record_Type
16090 -- This is achieved by appending Derived_Base discriminants into Discs,
16091 -- which has the side effect of returning a non empty Discs list to the
16092 -- caller of Inherit_Components, which is what we want. This must be
16093 -- done for private derived types if there are explicit stored
16094 -- discriminants, to ensure that we can retrieve the values of the
16095 -- constraints provided in the ancestors.
16097 if Inherit_Discr
16098 and then Is_Empty_Elmt_List (Discs)
16099 and then Present (First_Discriminant (Derived_Base))
16100 and then
16101 (not Is_Private_Type (Derived_Base)
16102 or else Is_Completely_Hidden
16103 (First_Stored_Discriminant (Derived_Base))
16104 or else Is_Generic_Type (Derived_Base))
16105 then
16106 D := First_Discriminant (Derived_Base);
16107 while Present (D) loop
16108 Append_Elmt (New_Reference_To (D, Loc), Discs);
16109 Next_Discriminant (D);
16110 end loop;
16111 end if;
16113 -- Finally, inherit non-discriminant components unless they are not
16114 -- visible because defined or inherited from the full view of the
16115 -- parent. Don't inherit the _parent field of the parent type.
16117 Component := First_Entity (Parent_Base);
16118 while Present (Component) loop
16120 -- Ada 2005 (AI-251): Do not inherit components associated with
16121 -- secondary tags of the parent.
16123 if Ekind (Component) = E_Component
16124 and then Present (Related_Type (Component))
16125 then
16126 null;
16128 elsif Ekind (Component) /= E_Component
16129 or else Chars (Component) = Name_uParent
16130 then
16131 null;
16133 -- If the derived type is within the parent type's declarative
16134 -- region, then the components can still be inherited even though
16135 -- they aren't visible at this point. This can occur for cases
16136 -- such as within public child units where the components must
16137 -- become visible upon entering the child unit's private part.
16139 elsif not Is_Visible_Component (Component)
16140 and then not In_Open_Scopes (Scope (Parent_Base))
16141 then
16142 null;
16144 elsif Ekind_In (Derived_Base, E_Private_Type,
16145 E_Limited_Private_Type)
16146 then
16147 null;
16149 else
16150 Inherit_Component (Component);
16151 end if;
16153 Next_Entity (Component);
16154 end loop;
16156 -- For tagged derived types, inherited discriminants cannot be used in
16157 -- component declarations of the record extension part. To achieve this
16158 -- we mark the inherited discriminants as not visible.
16160 if Is_Tagged and then Inherit_Discr then
16161 D := First_Discriminant (Derived_Base);
16162 while Present (D) loop
16163 Set_Is_Immediately_Visible (D, False);
16164 Next_Discriminant (D);
16165 end loop;
16166 end if;
16168 return Assoc_List;
16169 end Inherit_Components;
16171 -----------------------
16172 -- Is_Constant_Bound --
16173 -----------------------
16175 function Is_Constant_Bound (Exp : Node_Id) return Boolean is
16176 begin
16177 if Compile_Time_Known_Value (Exp) then
16178 return True;
16180 elsif Is_Entity_Name (Exp)
16181 and then Present (Entity (Exp))
16182 then
16183 return Is_Constant_Object (Entity (Exp))
16184 or else Ekind (Entity (Exp)) = E_Enumeration_Literal;
16186 elsif Nkind (Exp) in N_Binary_Op then
16187 return Is_Constant_Bound (Left_Opnd (Exp))
16188 and then Is_Constant_Bound (Right_Opnd (Exp))
16189 and then Scope (Entity (Exp)) = Standard_Standard;
16191 else
16192 return False;
16193 end if;
16194 end Is_Constant_Bound;
16196 -----------------------
16197 -- Is_Null_Extension --
16198 -----------------------
16200 function Is_Null_Extension (T : Entity_Id) return Boolean is
16201 Type_Decl : constant Node_Id := Parent (Base_Type (T));
16202 Comp_List : Node_Id;
16203 Comp : Node_Id;
16205 begin
16206 if Nkind (Type_Decl) /= N_Full_Type_Declaration
16207 or else not Is_Tagged_Type (T)
16208 or else Nkind (Type_Definition (Type_Decl)) /=
16209 N_Derived_Type_Definition
16210 or else No (Record_Extension_Part (Type_Definition (Type_Decl)))
16211 then
16212 return False;
16213 end if;
16215 Comp_List :=
16216 Component_List (Record_Extension_Part (Type_Definition (Type_Decl)));
16218 if Present (Discriminant_Specifications (Type_Decl)) then
16219 return False;
16221 elsif Present (Comp_List)
16222 and then Is_Non_Empty_List (Component_Items (Comp_List))
16223 then
16224 Comp := First (Component_Items (Comp_List));
16226 -- Only user-defined components are relevant. The component list
16227 -- may also contain a parent component and internal components
16228 -- corresponding to secondary tags, but these do not determine
16229 -- whether this is a null extension.
16231 while Present (Comp) loop
16232 if Comes_From_Source (Comp) then
16233 return False;
16234 end if;
16236 Next (Comp);
16237 end loop;
16239 return True;
16240 else
16241 return True;
16242 end if;
16243 end Is_Null_Extension;
16245 ------------------------------
16246 -- Is_Valid_Constraint_Kind --
16247 ------------------------------
16249 function Is_Valid_Constraint_Kind
16250 (T_Kind : Type_Kind;
16251 Constraint_Kind : Node_Kind) return Boolean
16253 begin
16254 case T_Kind is
16255 when Enumeration_Kind |
16256 Integer_Kind =>
16257 return Constraint_Kind = N_Range_Constraint;
16259 when Decimal_Fixed_Point_Kind =>
16260 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
16261 N_Range_Constraint);
16263 when Ordinary_Fixed_Point_Kind =>
16264 return Nkind_In (Constraint_Kind, N_Delta_Constraint,
16265 N_Range_Constraint);
16267 when Float_Kind =>
16268 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
16269 N_Range_Constraint);
16271 when Access_Kind |
16272 Array_Kind |
16273 E_Record_Type |
16274 E_Record_Subtype |
16275 Class_Wide_Kind |
16276 E_Incomplete_Type |
16277 Private_Kind |
16278 Concurrent_Kind =>
16279 return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
16281 when others =>
16282 return True; -- Error will be detected later
16283 end case;
16284 end Is_Valid_Constraint_Kind;
16286 --------------------------
16287 -- Is_Visible_Component --
16288 --------------------------
16290 function Is_Visible_Component (C : Entity_Id) return Boolean is
16291 Original_Comp : Entity_Id := Empty;
16292 Original_Scope : Entity_Id;
16293 Type_Scope : Entity_Id;
16295 function Is_Local_Type (Typ : Entity_Id) return Boolean;
16296 -- Check whether parent type of inherited component is declared locally,
16297 -- possibly within a nested package or instance. The current scope is
16298 -- the derived record itself.
16300 -------------------
16301 -- Is_Local_Type --
16302 -------------------
16304 function Is_Local_Type (Typ : Entity_Id) return Boolean is
16305 Scop : Entity_Id;
16307 begin
16308 Scop := Scope (Typ);
16309 while Present (Scop)
16310 and then Scop /= Standard_Standard
16311 loop
16312 if Scop = Scope (Current_Scope) then
16313 return True;
16314 end if;
16316 Scop := Scope (Scop);
16317 end loop;
16319 return False;
16320 end Is_Local_Type;
16322 -- Start of processing for Is_Visible_Component
16324 begin
16325 if Ekind_In (C, E_Component, E_Discriminant) then
16326 Original_Comp := Original_Record_Component (C);
16327 end if;
16329 if No (Original_Comp) then
16331 -- Premature usage, or previous error
16333 return False;
16335 else
16336 Original_Scope := Scope (Original_Comp);
16337 Type_Scope := Scope (Base_Type (Scope (C)));
16338 end if;
16340 -- This test only concerns tagged types
16342 if not Is_Tagged_Type (Original_Scope) then
16343 return True;
16345 -- If it is _Parent or _Tag, there is no visibility issue
16347 elsif not Comes_From_Source (Original_Comp) then
16348 return True;
16350 -- Discriminants are always visible
16352 elsif Ekind (Original_Comp) = E_Discriminant
16353 and then not Has_Unknown_Discriminants (Original_Scope)
16354 then
16355 return True;
16357 -- In the body of an instantiation, no need to check for the visibility
16358 -- of a component.
16360 elsif In_Instance_Body then
16361 return True;
16363 -- If the component has been declared in an ancestor which is currently
16364 -- a private type, then it is not visible. The same applies if the
16365 -- component's containing type is not in an open scope and the original
16366 -- component's enclosing type is a visible full view of a private type
16367 -- (which can occur in cases where an attempt is being made to reference
16368 -- a component in a sibling package that is inherited from a visible
16369 -- component of a type in an ancestor package; the component in the
16370 -- sibling package should not be visible even though the component it
16371 -- inherited from is visible). This does not apply however in the case
16372 -- where the scope of the type is a private child unit, or when the
16373 -- parent comes from a local package in which the ancestor is currently
16374 -- visible. The latter suppression of visibility is needed for cases
16375 -- that are tested in B730006.
16377 elsif Is_Private_Type (Original_Scope)
16378 or else
16379 (not Is_Private_Descendant (Type_Scope)
16380 and then not In_Open_Scopes (Type_Scope)
16381 and then Has_Private_Declaration (Original_Scope))
16382 then
16383 -- If the type derives from an entity in a formal package, there
16384 -- are no additional visible components.
16386 if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
16387 N_Formal_Package_Declaration
16388 then
16389 return False;
16391 -- if we are not in the private part of the current package, there
16392 -- are no additional visible components.
16394 elsif Ekind (Scope (Current_Scope)) = E_Package
16395 and then not In_Private_Part (Scope (Current_Scope))
16396 then
16397 return False;
16398 else
16399 return
16400 Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
16401 and then In_Open_Scopes (Scope (Original_Scope))
16402 and then Is_Local_Type (Type_Scope);
16403 end if;
16405 -- There is another weird way in which a component may be invisible
16406 -- when the private and the full view are not derived from the same
16407 -- ancestor. Here is an example :
16409 -- type A1 is tagged record F1 : integer; end record;
16410 -- type A2 is new A1 with record F2 : integer; end record;
16411 -- type T is new A1 with private;
16412 -- private
16413 -- type T is new A2 with null record;
16415 -- In this case, the full view of T inherits F1 and F2 but the private
16416 -- view inherits only F1
16418 else
16419 declare
16420 Ancestor : Entity_Id := Scope (C);
16422 begin
16423 loop
16424 if Ancestor = Original_Scope then
16425 return True;
16426 elsif Ancestor = Etype (Ancestor) then
16427 return False;
16428 end if;
16430 Ancestor := Etype (Ancestor);
16431 end loop;
16432 end;
16433 end if;
16434 end Is_Visible_Component;
16436 --------------------------
16437 -- Make_Class_Wide_Type --
16438 --------------------------
16440 procedure Make_Class_Wide_Type (T : Entity_Id) is
16441 CW_Type : Entity_Id;
16442 CW_Name : Name_Id;
16443 Next_E : Entity_Id;
16445 begin
16446 if Present (Class_Wide_Type (T)) then
16448 -- The class-wide type is a partially decorated entity created for a
16449 -- unanalyzed tagged type referenced through a limited with clause.
16450 -- When the tagged type is analyzed, its class-wide type needs to be
16451 -- redecorated. Note that we reuse the entity created by Decorate_
16452 -- Tagged_Type in order to preserve all links.
16454 if Materialize_Entity (Class_Wide_Type (T)) then
16455 CW_Type := Class_Wide_Type (T);
16456 Set_Materialize_Entity (CW_Type, False);
16458 -- The class wide type can have been defined by the partial view, in
16459 -- which case everything is already done.
16461 else
16462 return;
16463 end if;
16465 -- Default case, we need to create a new class-wide type
16467 else
16468 CW_Type :=
16469 New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
16470 end if;
16472 -- Inherit root type characteristics
16474 CW_Name := Chars (CW_Type);
16475 Next_E := Next_Entity (CW_Type);
16476 Copy_Node (T, CW_Type);
16477 Set_Comes_From_Source (CW_Type, False);
16478 Set_Chars (CW_Type, CW_Name);
16479 Set_Parent (CW_Type, Parent (T));
16480 Set_Next_Entity (CW_Type, Next_E);
16482 -- Ensure we have a new freeze node for the class-wide type. The partial
16483 -- view may have freeze action of its own, requiring a proper freeze
16484 -- node, and the same freeze node cannot be shared between the two
16485 -- types.
16487 Set_Has_Delayed_Freeze (CW_Type);
16488 Set_Freeze_Node (CW_Type, Empty);
16490 -- Customize the class-wide type: It has no prim. op., it cannot be
16491 -- abstract and its Etype points back to the specific root type.
16493 Set_Ekind (CW_Type, E_Class_Wide_Type);
16494 Set_Is_Tagged_Type (CW_Type, True);
16495 Set_Direct_Primitive_Operations (CW_Type, New_Elmt_List);
16496 Set_Is_Abstract_Type (CW_Type, False);
16497 Set_Is_Constrained (CW_Type, False);
16498 Set_Is_First_Subtype (CW_Type, Is_First_Subtype (T));
16500 if Ekind (T) = E_Class_Wide_Subtype then
16501 Set_Etype (CW_Type, Etype (Base_Type (T)));
16502 else
16503 Set_Etype (CW_Type, T);
16504 end if;
16506 -- If this is the class_wide type of a constrained subtype, it does
16507 -- not have discriminants.
16509 Set_Has_Discriminants (CW_Type,
16510 Has_Discriminants (T) and then not Is_Constrained (T));
16512 Set_Has_Unknown_Discriminants (CW_Type, True);
16513 Set_Class_Wide_Type (T, CW_Type);
16514 Set_Equivalent_Type (CW_Type, Empty);
16516 -- The class-wide type of a class-wide type is itself (RM 3.9(14))
16518 Set_Class_Wide_Type (CW_Type, CW_Type);
16519 end Make_Class_Wide_Type;
16521 ----------------
16522 -- Make_Index --
16523 ----------------
16525 procedure Make_Index
16526 (I : Node_Id;
16527 Related_Nod : Node_Id;
16528 Related_Id : Entity_Id := Empty;
16529 Suffix_Index : Nat := 1;
16530 In_Iter_Schm : Boolean := False)
16532 R : Node_Id;
16533 T : Entity_Id;
16534 Def_Id : Entity_Id := Empty;
16535 Found : Boolean := False;
16537 begin
16538 -- For a discrete range used in a constrained array definition and
16539 -- defined by a range, an implicit conversion to the predefined type
16540 -- INTEGER is assumed if each bound is either a numeric literal, a named
16541 -- number, or an attribute, and the type of both bounds (prior to the
16542 -- implicit conversion) is the type universal_integer. Otherwise, both
16543 -- bounds must be of the same discrete type, other than universal
16544 -- integer; this type must be determinable independently of the
16545 -- context, but using the fact that the type must be discrete and that
16546 -- both bounds must have the same type.
16548 -- Character literals also have a universal type in the absence of
16549 -- of additional context, and are resolved to Standard_Character.
16551 if Nkind (I) = N_Range then
16553 -- The index is given by a range constraint. The bounds are known
16554 -- to be of a consistent type.
16556 if not Is_Overloaded (I) then
16557 T := Etype (I);
16559 -- For universal bounds, choose the specific predefined type
16561 if T = Universal_Integer then
16562 T := Standard_Integer;
16564 elsif T = Any_Character then
16565 Ambiguous_Character (Low_Bound (I));
16567 T := Standard_Character;
16568 end if;
16570 -- The node may be overloaded because some user-defined operators
16571 -- are available, but if a universal interpretation exists it is
16572 -- also the selected one.
16574 elsif Universal_Interpretation (I) = Universal_Integer then
16575 T := Standard_Integer;
16577 else
16578 T := Any_Type;
16580 declare
16581 Ind : Interp_Index;
16582 It : Interp;
16584 begin
16585 Get_First_Interp (I, Ind, It);
16586 while Present (It.Typ) loop
16587 if Is_Discrete_Type (It.Typ) then
16589 if Found
16590 and then not Covers (It.Typ, T)
16591 and then not Covers (T, It.Typ)
16592 then
16593 Error_Msg_N ("ambiguous bounds in discrete range", I);
16594 exit;
16595 else
16596 T := It.Typ;
16597 Found := True;
16598 end if;
16599 end if;
16601 Get_Next_Interp (Ind, It);
16602 end loop;
16604 if T = Any_Type then
16605 Error_Msg_N ("discrete type required for range", I);
16606 Set_Etype (I, Any_Type);
16607 return;
16609 elsif T = Universal_Integer then
16610 T := Standard_Integer;
16611 end if;
16612 end;
16613 end if;
16615 if not Is_Discrete_Type (T) then
16616 Error_Msg_N ("discrete type required for range", I);
16617 Set_Etype (I, Any_Type);
16618 return;
16619 end if;
16621 if Nkind (Low_Bound (I)) = N_Attribute_Reference
16622 and then Attribute_Name (Low_Bound (I)) = Name_First
16623 and then Is_Entity_Name (Prefix (Low_Bound (I)))
16624 and then Is_Type (Entity (Prefix (Low_Bound (I))))
16625 and then Is_Discrete_Type (Entity (Prefix (Low_Bound (I))))
16626 then
16627 -- The type of the index will be the type of the prefix, as long
16628 -- as the upper bound is 'Last of the same type.
16630 Def_Id := Entity (Prefix (Low_Bound (I)));
16632 if Nkind (High_Bound (I)) /= N_Attribute_Reference
16633 or else Attribute_Name (High_Bound (I)) /= Name_Last
16634 or else not Is_Entity_Name (Prefix (High_Bound (I)))
16635 or else Entity (Prefix (High_Bound (I))) /= Def_Id
16636 then
16637 Def_Id := Empty;
16638 end if;
16639 end if;
16641 R := I;
16642 Process_Range_Expr_In_Decl (R, T, In_Iter_Schm => In_Iter_Schm);
16644 elsif Nkind (I) = N_Subtype_Indication then
16646 -- The index is given by a subtype with a range constraint
16648 T := Base_Type (Entity (Subtype_Mark (I)));
16650 if not Is_Discrete_Type (T) then
16651 Error_Msg_N ("discrete type required for range", I);
16652 Set_Etype (I, Any_Type);
16653 return;
16654 end if;
16656 R := Range_Expression (Constraint (I));
16658 Resolve (R, T);
16659 Process_Range_Expr_In_Decl
16660 (R, Entity (Subtype_Mark (I)), In_Iter_Schm => In_Iter_Schm);
16662 elsif Nkind (I) = N_Attribute_Reference then
16664 -- The parser guarantees that the attribute is a RANGE attribute
16666 -- If the node denotes the range of a type mark, that is also the
16667 -- resulting type, and we do no need to create an Itype for it.
16669 if Is_Entity_Name (Prefix (I))
16670 and then Comes_From_Source (I)
16671 and then Is_Type (Entity (Prefix (I)))
16672 and then Is_Discrete_Type (Entity (Prefix (I)))
16673 then
16674 Def_Id := Entity (Prefix (I));
16675 end if;
16677 Analyze_And_Resolve (I);
16678 T := Etype (I);
16679 R := I;
16681 -- If none of the above, must be a subtype. We convert this to a
16682 -- range attribute reference because in the case of declared first
16683 -- named subtypes, the types in the range reference can be different
16684 -- from the type of the entity. A range attribute normalizes the
16685 -- reference and obtains the correct types for the bounds.
16687 -- This transformation is in the nature of an expansion, is only
16688 -- done if expansion is active. In particular, it is not done on
16689 -- formal generic types, because we need to retain the name of the
16690 -- original index for instantiation purposes.
16692 else
16693 if not Is_Entity_Name (I) or else not Is_Type (Entity (I)) then
16694 Error_Msg_N ("invalid subtype mark in discrete range ", I);
16695 Set_Etype (I, Any_Integer);
16696 return;
16698 else
16699 -- The type mark may be that of an incomplete type. It is only
16700 -- now that we can get the full view, previous analysis does
16701 -- not look specifically for a type mark.
16703 Set_Entity (I, Get_Full_View (Entity (I)));
16704 Set_Etype (I, Entity (I));
16705 Def_Id := Entity (I);
16707 if not Is_Discrete_Type (Def_Id) then
16708 Error_Msg_N ("discrete type required for index", I);
16709 Set_Etype (I, Any_Type);
16710 return;
16711 end if;
16712 end if;
16714 if Expander_Active then
16715 Rewrite (I,
16716 Make_Attribute_Reference (Sloc (I),
16717 Attribute_Name => Name_Range,
16718 Prefix => Relocate_Node (I)));
16720 -- The original was a subtype mark that does not freeze. This
16721 -- means that the rewritten version must not freeze either.
16723 Set_Must_Not_Freeze (I);
16724 Set_Must_Not_Freeze (Prefix (I));
16726 -- Is order critical??? if so, document why, if not
16727 -- use Analyze_And_Resolve
16729 Analyze_And_Resolve (I);
16730 T := Etype (I);
16731 R := I;
16733 -- If expander is inactive, type is legal, nothing else to construct
16735 else
16736 return;
16737 end if;
16738 end if;
16740 if not Is_Discrete_Type (T) then
16741 Error_Msg_N ("discrete type required for range", I);
16742 Set_Etype (I, Any_Type);
16743 return;
16745 elsif T = Any_Type then
16746 Set_Etype (I, Any_Type);
16747 return;
16748 end if;
16750 -- We will now create the appropriate Itype to describe the range, but
16751 -- first a check. If we originally had a subtype, then we just label
16752 -- the range with this subtype. Not only is there no need to construct
16753 -- a new subtype, but it is wrong to do so for two reasons:
16755 -- 1. A legality concern, if we have a subtype, it must not freeze,
16756 -- and the Itype would cause freezing incorrectly
16758 -- 2. An efficiency concern, if we created an Itype, it would not be
16759 -- recognized as the same type for the purposes of eliminating
16760 -- checks in some circumstances.
16762 -- We signal this case by setting the subtype entity in Def_Id
16764 if No (Def_Id) then
16765 Def_Id :=
16766 Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
16767 Set_Etype (Def_Id, Base_Type (T));
16769 if Is_Signed_Integer_Type (T) then
16770 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
16772 elsif Is_Modular_Integer_Type (T) then
16773 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
16775 else
16776 Set_Ekind (Def_Id, E_Enumeration_Subtype);
16777 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
16778 Set_First_Literal (Def_Id, First_Literal (T));
16779 end if;
16781 Set_Size_Info (Def_Id, (T));
16782 Set_RM_Size (Def_Id, RM_Size (T));
16783 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
16785 Set_Scalar_Range (Def_Id, R);
16786 Conditional_Delay (Def_Id, T);
16788 -- In the subtype indication case, if the immediate parent of the
16789 -- new subtype is non-static, then the subtype we create is non-
16790 -- static, even if its bounds are static.
16792 if Nkind (I) = N_Subtype_Indication
16793 and then not Is_Static_Subtype (Entity (Subtype_Mark (I)))
16794 then
16795 Set_Is_Non_Static_Subtype (Def_Id);
16796 end if;
16797 end if;
16799 -- Final step is to label the index with this constructed type
16801 Set_Etype (I, Def_Id);
16802 end Make_Index;
16804 ------------------------------
16805 -- Modular_Type_Declaration --
16806 ------------------------------
16808 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
16809 Mod_Expr : constant Node_Id := Expression (Def);
16810 M_Val : Uint;
16812 procedure Set_Modular_Size (Bits : Int);
16813 -- Sets RM_Size to Bits, and Esize to normal word size above this
16815 ----------------------
16816 -- Set_Modular_Size --
16817 ----------------------
16819 procedure Set_Modular_Size (Bits : Int) is
16820 begin
16821 Set_RM_Size (T, UI_From_Int (Bits));
16823 if Bits <= 8 then
16824 Init_Esize (T, 8);
16826 elsif Bits <= 16 then
16827 Init_Esize (T, 16);
16829 elsif Bits <= 32 then
16830 Init_Esize (T, 32);
16832 else
16833 Init_Esize (T, System_Max_Binary_Modulus_Power);
16834 end if;
16836 if not Non_Binary_Modulus (T)
16837 and then Esize (T) = RM_Size (T)
16838 then
16839 Set_Is_Known_Valid (T);
16840 end if;
16841 end Set_Modular_Size;
16843 -- Start of processing for Modular_Type_Declaration
16845 begin
16846 -- If the mod expression is (exactly) 2 * literal, where literal is
16847 -- 64 or less,then almost certainly the * was meant to be **. Warn!
16849 if Warn_On_Suspicious_Modulus_Value
16850 and then Nkind (Mod_Expr) = N_Op_Multiply
16851 and then Nkind (Left_Opnd (Mod_Expr)) = N_Integer_Literal
16852 and then Intval (Left_Opnd (Mod_Expr)) = Uint_2
16853 and then Nkind (Right_Opnd (Mod_Expr)) = N_Integer_Literal
16854 and then Intval (Right_Opnd (Mod_Expr)) <= Uint_64
16855 then
16856 Error_Msg_N ("suspicious MOD value, was '*'* intended'??", Mod_Expr);
16857 end if;
16859 -- Proceed with analysis of mod expression
16861 Analyze_And_Resolve (Mod_Expr, Any_Integer);
16862 Set_Etype (T, T);
16863 Set_Ekind (T, E_Modular_Integer_Type);
16864 Init_Alignment (T);
16865 Set_Is_Constrained (T);
16867 if not Is_OK_Static_Expression (Mod_Expr) then
16868 Flag_Non_Static_Expr
16869 ("non-static expression used for modular type bound!", Mod_Expr);
16870 M_Val := 2 ** System_Max_Binary_Modulus_Power;
16871 else
16872 M_Val := Expr_Value (Mod_Expr);
16873 end if;
16875 if M_Val < 1 then
16876 Error_Msg_N ("modulus value must be positive", Mod_Expr);
16877 M_Val := 2 ** System_Max_Binary_Modulus_Power;
16878 end if;
16880 Set_Modulus (T, M_Val);
16882 -- Create bounds for the modular type based on the modulus given in
16883 -- the type declaration and then analyze and resolve those bounds.
16885 Set_Scalar_Range (T,
16886 Make_Range (Sloc (Mod_Expr),
16887 Low_Bound => Make_Integer_Literal (Sloc (Mod_Expr), 0),
16888 High_Bound => Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
16890 -- Properly analyze the literals for the range. We do this manually
16891 -- because we can't go calling Resolve, since we are resolving these
16892 -- bounds with the type, and this type is certainly not complete yet!
16894 Set_Etype (Low_Bound (Scalar_Range (T)), T);
16895 Set_Etype (High_Bound (Scalar_Range (T)), T);
16896 Set_Is_Static_Expression (Low_Bound (Scalar_Range (T)));
16897 Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
16899 -- Loop through powers of two to find number of bits required
16901 for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
16903 -- Binary case
16905 if M_Val = 2 ** Bits then
16906 Set_Modular_Size (Bits);
16907 return;
16909 -- Non-binary case
16911 elsif M_Val < 2 ** Bits then
16912 Check_SPARK_Restriction ("modulus should be a power of 2", T);
16913 Set_Non_Binary_Modulus (T);
16915 if Bits > System_Max_Nonbinary_Modulus_Power then
16916 Error_Msg_Uint_1 :=
16917 UI_From_Int (System_Max_Nonbinary_Modulus_Power);
16918 Error_Msg_F
16919 ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
16920 Set_Modular_Size (System_Max_Binary_Modulus_Power);
16921 return;
16923 else
16924 -- In the non-binary case, set size as per RM 13.3(55)
16926 Set_Modular_Size (Bits);
16927 return;
16928 end if;
16929 end if;
16931 end loop;
16933 -- If we fall through, then the size exceed System.Max_Binary_Modulus
16934 -- so we just signal an error and set the maximum size.
16936 Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
16937 Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
16939 Set_Modular_Size (System_Max_Binary_Modulus_Power);
16940 Init_Alignment (T);
16942 end Modular_Type_Declaration;
16944 --------------------------
16945 -- New_Concatenation_Op --
16946 --------------------------
16948 procedure New_Concatenation_Op (Typ : Entity_Id) is
16949 Loc : constant Source_Ptr := Sloc (Typ);
16950 Op : Entity_Id;
16952 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
16953 -- Create abbreviated declaration for the formal of a predefined
16954 -- Operator 'Op' of type 'Typ'
16956 --------------------
16957 -- Make_Op_Formal --
16958 --------------------
16960 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
16961 Formal : Entity_Id;
16962 begin
16963 Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
16964 Set_Etype (Formal, Typ);
16965 Set_Mechanism (Formal, Default_Mechanism);
16966 return Formal;
16967 end Make_Op_Formal;
16969 -- Start of processing for New_Concatenation_Op
16971 begin
16972 Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
16974 Set_Ekind (Op, E_Operator);
16975 Set_Scope (Op, Current_Scope);
16976 Set_Etype (Op, Typ);
16977 Set_Homonym (Op, Get_Name_Entity_Id (Name_Op_Concat));
16978 Set_Is_Immediately_Visible (Op);
16979 Set_Is_Intrinsic_Subprogram (Op);
16980 Set_Has_Completion (Op);
16981 Append_Entity (Op, Current_Scope);
16983 Set_Name_Entity_Id (Name_Op_Concat, Op);
16985 Append_Entity (Make_Op_Formal (Typ, Op), Op);
16986 Append_Entity (Make_Op_Formal (Typ, Op), Op);
16987 end New_Concatenation_Op;
16989 -------------------------
16990 -- OK_For_Limited_Init --
16991 -------------------------
16993 -- ???Check all calls of this, and compare the conditions under which it's
16994 -- called.
16996 function OK_For_Limited_Init
16997 (Typ : Entity_Id;
16998 Exp : Node_Id) return Boolean
17000 begin
17001 return Is_CPP_Constructor_Call (Exp)
17002 or else (Ada_Version >= Ada_2005
17003 and then not Debug_Flag_Dot_L
17004 and then OK_For_Limited_Init_In_05 (Typ, Exp));
17005 end OK_For_Limited_Init;
17007 -------------------------------
17008 -- OK_For_Limited_Init_In_05 --
17009 -------------------------------
17011 function OK_For_Limited_Init_In_05
17012 (Typ : Entity_Id;
17013 Exp : Node_Id) return Boolean
17015 begin
17016 -- An object of a limited interface type can be initialized with any
17017 -- expression of a nonlimited descendant type.
17019 if Is_Class_Wide_Type (Typ)
17020 and then Is_Limited_Interface (Typ)
17021 and then not Is_Limited_Type (Etype (Exp))
17022 then
17023 return True;
17024 end if;
17026 -- Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
17027 -- case of limited aggregates (including extension aggregates), and
17028 -- function calls. The function call may have been given in prefixed
17029 -- notation, in which case the original node is an indexed component.
17030 -- If the function is parameterless, the original node was an explicit
17031 -- dereference. The function may also be parameterless, in which case
17032 -- the source node is just an identifier.
17034 case Nkind (Original_Node (Exp)) is
17035 when N_Aggregate | N_Extension_Aggregate | N_Function_Call | N_Op =>
17036 return True;
17038 when N_Identifier =>
17039 return Present (Entity (Original_Node (Exp)))
17040 and then Ekind (Entity (Original_Node (Exp))) = E_Function;
17042 when N_Qualified_Expression =>
17043 return
17044 OK_For_Limited_Init_In_05
17045 (Typ, Expression (Original_Node (Exp)));
17047 -- Ada 2005 (AI-251): If a class-wide interface object is initialized
17048 -- with a function call, the expander has rewritten the call into an
17049 -- N_Type_Conversion node to force displacement of the pointer to
17050 -- reference the component containing the secondary dispatch table.
17051 -- Otherwise a type conversion is not a legal context.
17052 -- A return statement for a build-in-place function returning a
17053 -- synchronized type also introduces an unchecked conversion.
17055 when N_Type_Conversion |
17056 N_Unchecked_Type_Conversion =>
17057 return not Comes_From_Source (Exp)
17058 and then
17059 OK_For_Limited_Init_In_05
17060 (Typ, Expression (Original_Node (Exp)));
17062 when N_Indexed_Component |
17063 N_Selected_Component |
17064 N_Explicit_Dereference =>
17065 return Nkind (Exp) = N_Function_Call;
17067 -- A use of 'Input is a function call, hence allowed. Normally the
17068 -- attribute will be changed to a call, but the attribute by itself
17069 -- can occur with -gnatc.
17071 when N_Attribute_Reference =>
17072 return Attribute_Name (Original_Node (Exp)) = Name_Input;
17074 -- For a case expression, all dependent expressions must be legal
17076 when N_Case_Expression =>
17077 declare
17078 Alt : Node_Id;
17080 begin
17081 Alt := First (Alternatives (Original_Node (Exp)));
17082 while Present (Alt) loop
17083 if not OK_For_Limited_Init_In_05 (Typ, Expression (Alt)) then
17084 return False;
17085 end if;
17087 Next (Alt);
17088 end loop;
17090 return True;
17091 end;
17093 -- For an if expression, all dependent expressions must be legal
17095 when N_If_Expression =>
17096 declare
17097 Then_Expr : constant Node_Id :=
17098 Next (First (Expressions (Original_Node (Exp))));
17099 Else_Expr : constant Node_Id := Next (Then_Expr);
17100 begin
17101 return OK_For_Limited_Init_In_05 (Typ, Then_Expr)
17102 and then
17103 OK_For_Limited_Init_In_05 (Typ, Else_Expr);
17104 end;
17106 when others =>
17107 return False;
17108 end case;
17109 end OK_For_Limited_Init_In_05;
17111 -------------------------------------------
17112 -- Ordinary_Fixed_Point_Type_Declaration --
17113 -------------------------------------------
17115 procedure Ordinary_Fixed_Point_Type_Declaration
17116 (T : Entity_Id;
17117 Def : Node_Id)
17119 Loc : constant Source_Ptr := Sloc (Def);
17120 Delta_Expr : constant Node_Id := Delta_Expression (Def);
17121 RRS : constant Node_Id := Real_Range_Specification (Def);
17122 Implicit_Base : Entity_Id;
17123 Delta_Val : Ureal;
17124 Small_Val : Ureal;
17125 Low_Val : Ureal;
17126 High_Val : Ureal;
17128 begin
17129 Check_Restriction (No_Fixed_Point, Def);
17131 -- Create implicit base type
17133 Implicit_Base :=
17134 Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
17135 Set_Etype (Implicit_Base, Implicit_Base);
17137 -- Analyze and process delta expression
17139 Analyze_And_Resolve (Delta_Expr, Any_Real);
17141 Check_Delta_Expression (Delta_Expr);
17142 Delta_Val := Expr_Value_R (Delta_Expr);
17144 Set_Delta_Value (Implicit_Base, Delta_Val);
17146 -- Compute default small from given delta, which is the largest power
17147 -- of two that does not exceed the given delta value.
17149 declare
17150 Tmp : Ureal;
17151 Scale : Int;
17153 begin
17154 Tmp := Ureal_1;
17155 Scale := 0;
17157 if Delta_Val < Ureal_1 then
17158 while Delta_Val < Tmp loop
17159 Tmp := Tmp / Ureal_2;
17160 Scale := Scale + 1;
17161 end loop;
17163 else
17164 loop
17165 Tmp := Tmp * Ureal_2;
17166 exit when Tmp > Delta_Val;
17167 Scale := Scale - 1;
17168 end loop;
17169 end if;
17171 Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
17172 end;
17174 Set_Small_Value (Implicit_Base, Small_Val);
17176 -- If no range was given, set a dummy range
17178 if RRS <= Empty_Or_Error then
17179 Low_Val := -Small_Val;
17180 High_Val := Small_Val;
17182 -- Otherwise analyze and process given range
17184 else
17185 declare
17186 Low : constant Node_Id := Low_Bound (RRS);
17187 High : constant Node_Id := High_Bound (RRS);
17189 begin
17190 Analyze_And_Resolve (Low, Any_Real);
17191 Analyze_And_Resolve (High, Any_Real);
17192 Check_Real_Bound (Low);
17193 Check_Real_Bound (High);
17195 -- Obtain and set the range
17197 Low_Val := Expr_Value_R (Low);
17198 High_Val := Expr_Value_R (High);
17200 if Low_Val > High_Val then
17201 Error_Msg_NE ("?fixed point type& has null range", Def, T);
17202 end if;
17203 end;
17204 end if;
17206 -- The range for both the implicit base and the declared first subtype
17207 -- cannot be set yet, so we use the special routine Set_Fixed_Range to
17208 -- set a temporary range in place. Note that the bounds of the base
17209 -- type will be widened to be symmetrical and to fill the available
17210 -- bits when the type is frozen.
17212 -- We could do this with all discrete types, and probably should, but
17213 -- we absolutely have to do it for fixed-point, since the end-points
17214 -- of the range and the size are determined by the small value, which
17215 -- could be reset before the freeze point.
17217 Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
17218 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
17220 -- Complete definition of first subtype
17222 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
17223 Set_Etype (T, Implicit_Base);
17224 Init_Size_Align (T);
17225 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
17226 Set_Small_Value (T, Small_Val);
17227 Set_Delta_Value (T, Delta_Val);
17228 Set_Is_Constrained (T);
17230 end Ordinary_Fixed_Point_Type_Declaration;
17232 ----------------------------------------
17233 -- Prepare_Private_Subtype_Completion --
17234 ----------------------------------------
17236 procedure Prepare_Private_Subtype_Completion
17237 (Id : Entity_Id;
17238 Related_Nod : Node_Id)
17240 Id_B : constant Entity_Id := Base_Type (Id);
17241 Full_B : constant Entity_Id := Full_View (Id_B);
17242 Full : Entity_Id;
17244 begin
17245 if Present (Full_B) then
17247 -- The Base_Type is already completed, we can complete the subtype
17248 -- now. We have to create a new entity with the same name, Thus we
17249 -- can't use Create_Itype.
17251 -- This is messy, should be fixed ???
17253 Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
17254 Set_Is_Itype (Full);
17255 Set_Associated_Node_For_Itype (Full, Related_Nod);
17256 Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
17257 end if;
17259 -- The parent subtype may be private, but the base might not, in some
17260 -- nested instances. In that case, the subtype does not need to be
17261 -- exchanged. It would still be nice to make private subtypes and their
17262 -- bases consistent at all times ???
17264 if Is_Private_Type (Id_B) then
17265 Append_Elmt (Id, Private_Dependents (Id_B));
17266 end if;
17268 end Prepare_Private_Subtype_Completion;
17270 ---------------------------
17271 -- Process_Discriminants --
17272 ---------------------------
17274 procedure Process_Discriminants
17275 (N : Node_Id;
17276 Prev : Entity_Id := Empty)
17278 Elist : constant Elist_Id := New_Elmt_List;
17279 Id : Node_Id;
17280 Discr : Node_Id;
17281 Discr_Number : Uint;
17282 Discr_Type : Entity_Id;
17283 Default_Present : Boolean := False;
17284 Default_Not_Present : Boolean := False;
17286 begin
17287 -- A composite type other than an array type can have discriminants.
17288 -- On entry, the current scope is the composite type.
17290 -- The discriminants are initially entered into the scope of the type
17291 -- via Enter_Name with the default Ekind of E_Void to prevent premature
17292 -- use, as explained at the end of this procedure.
17294 Discr := First (Discriminant_Specifications (N));
17295 while Present (Discr) loop
17296 Enter_Name (Defining_Identifier (Discr));
17298 -- For navigation purposes we add a reference to the discriminant
17299 -- in the entity for the type. If the current declaration is a
17300 -- completion, place references on the partial view. Otherwise the
17301 -- type is the current scope.
17303 if Present (Prev) then
17305 -- The references go on the partial view, if present. If the
17306 -- partial view has discriminants, the references have been
17307 -- generated already.
17309 if not Has_Discriminants (Prev) then
17310 Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
17311 end if;
17312 else
17313 Generate_Reference
17314 (Current_Scope, Defining_Identifier (Discr), 'd');
17315 end if;
17317 if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
17318 Discr_Type := Access_Definition (Discr, Discriminant_Type (Discr));
17320 -- Ada 2005 (AI-254)
17322 if Present (Access_To_Subprogram_Definition
17323 (Discriminant_Type (Discr)))
17324 and then Protected_Present (Access_To_Subprogram_Definition
17325 (Discriminant_Type (Discr)))
17326 then
17327 Discr_Type :=
17328 Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
17329 end if;
17331 else
17332 Find_Type (Discriminant_Type (Discr));
17333 Discr_Type := Etype (Discriminant_Type (Discr));
17335 if Error_Posted (Discriminant_Type (Discr)) then
17336 Discr_Type := Any_Type;
17337 end if;
17338 end if;
17340 if Is_Access_Type (Discr_Type) then
17342 -- Ada 2005 (AI-230): Access discriminant allowed in non-limited
17343 -- record types
17345 if Ada_Version < Ada_2005 then
17346 Check_Access_Discriminant_Requires_Limited
17347 (Discr, Discriminant_Type (Discr));
17348 end if;
17350 if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
17351 Error_Msg_N
17352 ("(Ada 83) access discriminant not allowed", Discr);
17353 end if;
17355 elsif not Is_Discrete_Type (Discr_Type) then
17356 Error_Msg_N ("discriminants must have a discrete or access type",
17357 Discriminant_Type (Discr));
17358 end if;
17360 Set_Etype (Defining_Identifier (Discr), Discr_Type);
17362 -- If a discriminant specification includes the assignment compound
17363 -- delimiter followed by an expression, the expression is the default
17364 -- expression of the discriminant; the default expression must be of
17365 -- the type of the discriminant. (RM 3.7.1) Since this expression is
17366 -- a default expression, we do the special preanalysis, since this
17367 -- expression does not freeze (see "Handling of Default and Per-
17368 -- Object Expressions" in spec of package Sem).
17370 if Present (Expression (Discr)) then
17371 Preanalyze_Spec_Expression (Expression (Discr), Discr_Type);
17373 if Nkind (N) = N_Formal_Type_Declaration then
17374 Error_Msg_N
17375 ("discriminant defaults not allowed for formal type",
17376 Expression (Discr));
17378 -- Flag an error for a tagged type with defaulted discriminants,
17379 -- excluding limited tagged types when compiling for Ada 2012
17380 -- (see AI05-0214).
17382 elsif Is_Tagged_Type (Current_Scope)
17383 and then (not Is_Limited_Type (Current_Scope)
17384 or else Ada_Version < Ada_2012)
17385 and then Comes_From_Source (N)
17386 then
17387 -- Note: see similar test in Check_Or_Process_Discriminants, to
17388 -- handle the (illegal) case of the completion of an untagged
17389 -- view with discriminants with defaults by a tagged full view.
17390 -- We skip the check if Discr does not come from source, to
17391 -- account for the case of an untagged derived type providing
17392 -- defaults for a renamed discriminant from a private untagged
17393 -- ancestor with a tagged full view (ACATS B460006).
17395 if Ada_Version >= Ada_2012 then
17396 Error_Msg_N
17397 ("discriminants of nonlimited tagged type cannot have"
17398 & " defaults",
17399 Expression (Discr));
17400 else
17401 Error_Msg_N
17402 ("discriminants of tagged type cannot have defaults",
17403 Expression (Discr));
17404 end if;
17406 else
17407 Default_Present := True;
17408 Append_Elmt (Expression (Discr), Elist);
17410 -- Tag the defining identifiers for the discriminants with
17411 -- their corresponding default expressions from the tree.
17413 Set_Discriminant_Default_Value
17414 (Defining_Identifier (Discr), Expression (Discr));
17415 end if;
17417 else
17418 Default_Not_Present := True;
17419 end if;
17421 -- Ada 2005 (AI-231): Create an Itype that is a duplicate of
17422 -- Discr_Type but with the null-exclusion attribute
17424 if Ada_Version >= Ada_2005 then
17426 -- Ada 2005 (AI-231): Static checks
17428 if Can_Never_Be_Null (Discr_Type) then
17429 Null_Exclusion_Static_Checks (Discr);
17431 elsif Is_Access_Type (Discr_Type)
17432 and then Null_Exclusion_Present (Discr)
17434 -- No need to check itypes because in their case this check
17435 -- was done at their point of creation
17437 and then not Is_Itype (Discr_Type)
17438 then
17439 if Can_Never_Be_Null (Discr_Type) then
17440 Error_Msg_NE
17441 ("`NOT NULL` not allowed (& already excludes null)",
17442 Discr,
17443 Discr_Type);
17444 end if;
17446 Set_Etype (Defining_Identifier (Discr),
17447 Create_Null_Excluding_Itype
17448 (T => Discr_Type,
17449 Related_Nod => Discr));
17451 -- Check for improper null exclusion if the type is otherwise
17452 -- legal for a discriminant.
17454 elsif Null_Exclusion_Present (Discr)
17455 and then Is_Discrete_Type (Discr_Type)
17456 then
17457 Error_Msg_N
17458 ("null exclusion can only apply to an access type", Discr);
17459 end if;
17461 -- Ada 2005 (AI-402): access discriminants of nonlimited types
17462 -- can't have defaults. Synchronized types, or types that are
17463 -- explicitly limited are fine, but special tests apply to derived
17464 -- types in generics: in a generic body we have to assume the
17465 -- worst, and therefore defaults are not allowed if the parent is
17466 -- a generic formal private type (see ACATS B370001).
17468 if Is_Access_Type (Discr_Type) and then Default_Present then
17469 if Ekind (Discr_Type) /= E_Anonymous_Access_Type
17470 or else Is_Limited_Record (Current_Scope)
17471 or else Is_Concurrent_Type (Current_Scope)
17472 or else Is_Concurrent_Record_Type (Current_Scope)
17473 or else Ekind (Current_Scope) = E_Limited_Private_Type
17474 then
17475 if not Is_Derived_Type (Current_Scope)
17476 or else not Is_Generic_Type (Etype (Current_Scope))
17477 or else not In_Package_Body (Scope (Etype (Current_Scope)))
17478 or else Limited_Present
17479 (Type_Definition (Parent (Current_Scope)))
17480 then
17481 null;
17483 else
17484 Error_Msg_N ("access discriminants of nonlimited types",
17485 Expression (Discr));
17486 Error_Msg_N ("\cannot have defaults", Expression (Discr));
17487 end if;
17489 elsif Present (Expression (Discr)) then
17490 Error_Msg_N
17491 ("(Ada 2005) access discriminants of nonlimited types",
17492 Expression (Discr));
17493 Error_Msg_N ("\cannot have defaults", Expression (Discr));
17494 end if;
17495 end if;
17496 end if;
17498 Next (Discr);
17499 end loop;
17501 -- An element list consisting of the default expressions of the
17502 -- discriminants is constructed in the above loop and used to set
17503 -- the Discriminant_Constraint attribute for the type. If an object
17504 -- is declared of this (record or task) type without any explicit
17505 -- discriminant constraint given, this element list will form the
17506 -- actual parameters for the corresponding initialization procedure
17507 -- for the type.
17509 Set_Discriminant_Constraint (Current_Scope, Elist);
17510 Set_Stored_Constraint (Current_Scope, No_Elist);
17512 -- Default expressions must be provided either for all or for none
17513 -- of the discriminants of a discriminant part. (RM 3.7.1)
17515 if Default_Present and then Default_Not_Present then
17516 Error_Msg_N
17517 ("incomplete specification of defaults for discriminants", N);
17518 end if;
17520 -- The use of the name of a discriminant is not allowed in default
17521 -- expressions of a discriminant part if the specification of the
17522 -- discriminant is itself given in the discriminant part. (RM 3.7.1)
17524 -- To detect this, the discriminant names are entered initially with an
17525 -- Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
17526 -- attempt to use a void entity (for example in an expression that is
17527 -- type-checked) produces the error message: premature usage. Now after
17528 -- completing the semantic analysis of the discriminant part, we can set
17529 -- the Ekind of all the discriminants appropriately.
17531 Discr := First (Discriminant_Specifications (N));
17532 Discr_Number := Uint_1;
17533 while Present (Discr) loop
17534 Id := Defining_Identifier (Discr);
17535 Set_Ekind (Id, E_Discriminant);
17536 Init_Component_Location (Id);
17537 Init_Esize (Id);
17538 Set_Discriminant_Number (Id, Discr_Number);
17540 -- Make sure this is always set, even in illegal programs
17542 Set_Corresponding_Discriminant (Id, Empty);
17544 -- Initialize the Original_Record_Component to the entity itself.
17545 -- Inherit_Components will propagate the right value to
17546 -- discriminants in derived record types.
17548 Set_Original_Record_Component (Id, Id);
17550 -- Create the discriminal for the discriminant
17552 Build_Discriminal (Id);
17554 Next (Discr);
17555 Discr_Number := Discr_Number + 1;
17556 end loop;
17558 Set_Has_Discriminants (Current_Scope);
17559 end Process_Discriminants;
17561 -----------------------
17562 -- Process_Full_View --
17563 -----------------------
17565 procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
17566 Priv_Parent : Entity_Id;
17567 Full_Parent : Entity_Id;
17568 Full_Indic : Node_Id;
17570 procedure Collect_Implemented_Interfaces
17571 (Typ : Entity_Id;
17572 Ifaces : Elist_Id);
17573 -- Ada 2005: Gather all the interfaces that Typ directly or
17574 -- inherently implements. Duplicate entries are not added to
17575 -- the list Ifaces.
17577 ------------------------------------
17578 -- Collect_Implemented_Interfaces --
17579 ------------------------------------
17581 procedure Collect_Implemented_Interfaces
17582 (Typ : Entity_Id;
17583 Ifaces : Elist_Id)
17585 Iface : Entity_Id;
17586 Iface_Elmt : Elmt_Id;
17588 begin
17589 -- Abstract interfaces are only associated with tagged record types
17591 if not Is_Tagged_Type (Typ)
17592 or else not Is_Record_Type (Typ)
17593 then
17594 return;
17595 end if;
17597 -- Recursively climb to the ancestors
17599 if Etype (Typ) /= Typ
17601 -- Protect the frontend against wrong cyclic declarations like:
17603 -- type B is new A with private;
17604 -- type C is new A with private;
17605 -- private
17606 -- type B is new C with null record;
17607 -- type C is new B with null record;
17609 and then Etype (Typ) /= Priv_T
17610 and then Etype (Typ) /= Full_T
17611 then
17612 -- Keep separate the management of private type declarations
17614 if Ekind (Typ) = E_Record_Type_With_Private then
17616 -- Handle the following erroneous case:
17617 -- type Private_Type is tagged private;
17618 -- private
17619 -- type Private_Type is new Type_Implementing_Iface;
17621 if Present (Full_View (Typ))
17622 and then Etype (Typ) /= Full_View (Typ)
17623 then
17624 if Is_Interface (Etype (Typ)) then
17625 Append_Unique_Elmt (Etype (Typ), Ifaces);
17626 end if;
17628 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
17629 end if;
17631 -- Non-private types
17633 else
17634 if Is_Interface (Etype (Typ)) then
17635 Append_Unique_Elmt (Etype (Typ), Ifaces);
17636 end if;
17638 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
17639 end if;
17640 end if;
17642 -- Handle entities in the list of abstract interfaces
17644 if Present (Interfaces (Typ)) then
17645 Iface_Elmt := First_Elmt (Interfaces (Typ));
17646 while Present (Iface_Elmt) loop
17647 Iface := Node (Iface_Elmt);
17649 pragma Assert (Is_Interface (Iface));
17651 if not Contain_Interface (Iface, Ifaces) then
17652 Append_Elmt (Iface, Ifaces);
17653 Collect_Implemented_Interfaces (Iface, Ifaces);
17654 end if;
17656 Next_Elmt (Iface_Elmt);
17657 end loop;
17658 end if;
17659 end Collect_Implemented_Interfaces;
17661 -- Start of processing for Process_Full_View
17663 begin
17664 -- First some sanity checks that must be done after semantic
17665 -- decoration of the full view and thus cannot be placed with other
17666 -- similar checks in Find_Type_Name
17668 if not Is_Limited_Type (Priv_T)
17669 and then (Is_Limited_Type (Full_T)
17670 or else Is_Limited_Composite (Full_T))
17671 then
17672 if In_Instance then
17673 null;
17674 else
17675 Error_Msg_N
17676 ("completion of nonlimited type cannot be limited", Full_T);
17677 Explain_Limited_Type (Full_T, Full_T);
17678 end if;
17680 elsif Is_Abstract_Type (Full_T)
17681 and then not Is_Abstract_Type (Priv_T)
17682 then
17683 Error_Msg_N
17684 ("completion of nonabstract type cannot be abstract", Full_T);
17686 elsif Is_Tagged_Type (Priv_T)
17687 and then Is_Limited_Type (Priv_T)
17688 and then not Is_Limited_Type (Full_T)
17689 then
17690 -- If pragma CPP_Class was applied to the private declaration
17691 -- propagate the limitedness to the full-view
17693 if Is_CPP_Class (Priv_T) then
17694 Set_Is_Limited_Record (Full_T);
17696 -- GNAT allow its own definition of Limited_Controlled to disobey
17697 -- this rule in order in ease the implementation. This test is safe
17698 -- because Root_Controlled is defined in a child of System that
17699 -- normal programs are not supposed to use.
17701 elsif Is_RTE (Etype (Full_T), RE_Root_Controlled) then
17702 Set_Is_Limited_Composite (Full_T);
17703 else
17704 Error_Msg_N
17705 ("completion of limited tagged type must be limited", Full_T);
17706 end if;
17708 elsif Is_Generic_Type (Priv_T) then
17709 Error_Msg_N ("generic type cannot have a completion", Full_T);
17710 end if;
17712 -- Check that ancestor interfaces of private and full views are
17713 -- consistent. We omit this check for synchronized types because
17714 -- they are performed on the corresponding record type when frozen.
17716 if Ada_Version >= Ada_2005
17717 and then Is_Tagged_Type (Priv_T)
17718 and then Is_Tagged_Type (Full_T)
17719 and then not Is_Concurrent_Type (Full_T)
17720 then
17721 declare
17722 Iface : Entity_Id;
17723 Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
17724 Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
17726 begin
17727 Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
17728 Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
17730 -- Ada 2005 (AI-251): The partial view shall be a descendant of
17731 -- an interface type if and only if the full type is descendant
17732 -- of the interface type (AARM 7.3 (7.3/2)).
17734 Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
17736 if Present (Iface) then
17737 Error_Msg_NE
17738 ("interface & not implemented by full type " &
17739 "(RM-2005 7.3 (7.3/2))", Priv_T, Iface);
17740 end if;
17742 Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
17744 if Present (Iface) then
17745 Error_Msg_NE
17746 ("interface & not implemented by partial view " &
17747 "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
17748 end if;
17749 end;
17750 end if;
17752 if Is_Tagged_Type (Priv_T)
17753 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
17754 and then Is_Derived_Type (Full_T)
17755 then
17756 Priv_Parent := Etype (Priv_T);
17758 -- The full view of a private extension may have been transformed
17759 -- into an unconstrained derived type declaration and a subtype
17760 -- declaration (see build_derived_record_type for details).
17762 if Nkind (N) = N_Subtype_Declaration then
17763 Full_Indic := Subtype_Indication (N);
17764 Full_Parent := Etype (Base_Type (Full_T));
17765 else
17766 Full_Indic := Subtype_Indication (Type_Definition (N));
17767 Full_Parent := Etype (Full_T);
17768 end if;
17770 -- Check that the parent type of the full type is a descendant of
17771 -- the ancestor subtype given in the private extension. If either
17772 -- entity has an Etype equal to Any_Type then we had some previous
17773 -- error situation [7.3(8)].
17775 if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
17776 return;
17778 -- Ada 2005 (AI-251): Interfaces in the full-typ can be given in
17779 -- any order. Therefore we don't have to check that its parent must
17780 -- be a descendant of the parent of the private type declaration.
17782 elsif Is_Interface (Priv_Parent)
17783 and then Is_Interface (Full_Parent)
17784 then
17785 null;
17787 -- Ada 2005 (AI-251): If the parent of the private type declaration
17788 -- is an interface there is no need to check that it is an ancestor
17789 -- of the associated full type declaration. The required tests for
17790 -- this case are performed by Build_Derived_Record_Type.
17792 elsif not Is_Interface (Base_Type (Priv_Parent))
17793 and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
17794 then
17795 Error_Msg_N
17796 ("parent of full type must descend from parent"
17797 & " of private extension", Full_Indic);
17799 -- First check a formal restriction, and then proceed with checking
17800 -- Ada rules. Since the formal restriction is not a serious error, we
17801 -- don't prevent further error detection for this check, hence the
17802 -- ELSE.
17804 else
17806 -- In formal mode, when completing a private extension the type
17807 -- named in the private part must be exactly the same as that
17808 -- named in the visible part.
17810 if Priv_Parent /= Full_Parent then
17811 Error_Msg_Name_1 := Chars (Priv_Parent);
17812 Check_SPARK_Restriction ("% expected", Full_Indic);
17813 end if;
17815 -- Check the rules of 7.3(10): if the private extension inherits
17816 -- known discriminants, then the full type must also inherit those
17817 -- discriminants from the same (ancestor) type, and the parent
17818 -- subtype of the full type must be constrained if and only if
17819 -- the ancestor subtype of the private extension is constrained.
17821 if No (Discriminant_Specifications (Parent (Priv_T)))
17822 and then not Has_Unknown_Discriminants (Priv_T)
17823 and then Has_Discriminants (Base_Type (Priv_Parent))
17824 then
17825 declare
17826 Priv_Indic : constant Node_Id :=
17827 Subtype_Indication (Parent (Priv_T));
17829 Priv_Constr : constant Boolean :=
17830 Is_Constrained (Priv_Parent)
17831 or else
17832 Nkind (Priv_Indic) = N_Subtype_Indication
17833 or else
17834 Is_Constrained (Entity (Priv_Indic));
17836 Full_Constr : constant Boolean :=
17837 Is_Constrained (Full_Parent)
17838 or else
17839 Nkind (Full_Indic) = N_Subtype_Indication
17840 or else
17841 Is_Constrained (Entity (Full_Indic));
17843 Priv_Discr : Entity_Id;
17844 Full_Discr : Entity_Id;
17846 begin
17847 Priv_Discr := First_Discriminant (Priv_Parent);
17848 Full_Discr := First_Discriminant (Full_Parent);
17849 while Present (Priv_Discr) and then Present (Full_Discr) loop
17850 if Original_Record_Component (Priv_Discr) =
17851 Original_Record_Component (Full_Discr)
17852 or else
17853 Corresponding_Discriminant (Priv_Discr) =
17854 Corresponding_Discriminant (Full_Discr)
17855 then
17856 null;
17857 else
17858 exit;
17859 end if;
17861 Next_Discriminant (Priv_Discr);
17862 Next_Discriminant (Full_Discr);
17863 end loop;
17865 if Present (Priv_Discr) or else Present (Full_Discr) then
17866 Error_Msg_N
17867 ("full view must inherit discriminants of the parent"
17868 & " type used in the private extension", Full_Indic);
17870 elsif Priv_Constr and then not Full_Constr then
17871 Error_Msg_N
17872 ("parent subtype of full type must be constrained",
17873 Full_Indic);
17875 elsif Full_Constr and then not Priv_Constr then
17876 Error_Msg_N
17877 ("parent subtype of full type must be unconstrained",
17878 Full_Indic);
17879 end if;
17880 end;
17882 -- Check the rules of 7.3(12): if a partial view has neither
17883 -- known or unknown discriminants, then the full type
17884 -- declaration shall define a definite subtype.
17886 elsif not Has_Unknown_Discriminants (Priv_T)
17887 and then not Has_Discriminants (Priv_T)
17888 and then not Is_Constrained (Full_T)
17889 then
17890 Error_Msg_N
17891 ("full view must define a constrained type if partial view"
17892 & " has no discriminants", Full_T);
17893 end if;
17895 -- ??????? Do we implement the following properly ?????
17896 -- If the ancestor subtype of a private extension has constrained
17897 -- discriminants, then the parent subtype of the full view shall
17898 -- impose a statically matching constraint on those discriminants
17899 -- [7.3(13)].
17900 end if;
17902 else
17903 -- For untagged types, verify that a type without discriminants
17904 -- is not completed with an unconstrained type.
17906 if not Is_Indefinite_Subtype (Priv_T)
17907 and then Is_Indefinite_Subtype (Full_T)
17908 then
17909 Error_Msg_N ("full view of type must be definite subtype", Full_T);
17910 end if;
17911 end if;
17913 -- AI-419: verify that the use of "limited" is consistent
17915 declare
17916 Orig_Decl : constant Node_Id := Original_Node (N);
17918 begin
17919 if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
17920 and then not Limited_Present (Parent (Priv_T))
17921 and then not Synchronized_Present (Parent (Priv_T))
17922 and then Nkind (Orig_Decl) = N_Full_Type_Declaration
17923 and then Nkind
17924 (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
17925 and then Limited_Present (Type_Definition (Orig_Decl))
17926 then
17927 Error_Msg_N
17928 ("full view of non-limited extension cannot be limited", N);
17929 end if;
17930 end;
17932 -- Ada 2005 (AI-443): A synchronized private extension must be
17933 -- completed by a task or protected type.
17935 if Ada_Version >= Ada_2005
17936 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
17937 and then Synchronized_Present (Parent (Priv_T))
17938 and then not Is_Concurrent_Type (Full_T)
17939 then
17940 Error_Msg_N ("full view of synchronized extension must " &
17941 "be synchronized type", N);
17942 end if;
17944 -- Ada 2005 AI-363: if the full view has discriminants with
17945 -- defaults, it is illegal to declare constrained access subtypes
17946 -- whose designated type is the current type. This allows objects
17947 -- of the type that are declared in the heap to be unconstrained.
17949 if not Has_Unknown_Discriminants (Priv_T)
17950 and then not Has_Discriminants (Priv_T)
17951 and then Has_Discriminants (Full_T)
17952 and then
17953 Present (Discriminant_Default_Value (First_Discriminant (Full_T)))
17954 then
17955 Set_Has_Constrained_Partial_View (Full_T);
17956 Set_Has_Constrained_Partial_View (Priv_T);
17957 end if;
17959 -- Create a full declaration for all its subtypes recorded in
17960 -- Private_Dependents and swap them similarly to the base type. These
17961 -- are subtypes that have been define before the full declaration of
17962 -- the private type. We also swap the entry in Private_Dependents list
17963 -- so we can properly restore the private view on exit from the scope.
17965 declare
17966 Priv_Elmt : Elmt_Id;
17967 Priv : Entity_Id;
17968 Full : Entity_Id;
17970 begin
17971 Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
17972 while Present (Priv_Elmt) loop
17973 Priv := Node (Priv_Elmt);
17975 if Ekind_In (Priv, E_Private_Subtype,
17976 E_Limited_Private_Subtype,
17977 E_Record_Subtype_With_Private)
17978 then
17979 Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
17980 Set_Is_Itype (Full);
17981 Set_Parent (Full, Parent (Priv));
17982 Set_Associated_Node_For_Itype (Full, N);
17984 -- Now we need to complete the private subtype, but since the
17985 -- base type has already been swapped, we must also swap the
17986 -- subtypes (and thus, reverse the arguments in the call to
17987 -- Complete_Private_Subtype).
17989 Copy_And_Swap (Priv, Full);
17990 Complete_Private_Subtype (Full, Priv, Full_T, N);
17991 Replace_Elmt (Priv_Elmt, Full);
17992 end if;
17994 Next_Elmt (Priv_Elmt);
17995 end loop;
17996 end;
17998 -- If the private view was tagged, copy the new primitive operations
17999 -- from the private view to the full view.
18001 if Is_Tagged_Type (Full_T) then
18002 declare
18003 Disp_Typ : Entity_Id;
18004 Full_List : Elist_Id;
18005 Prim : Entity_Id;
18006 Prim_Elmt : Elmt_Id;
18007 Priv_List : Elist_Id;
18009 function Contains
18010 (E : Entity_Id;
18011 L : Elist_Id) return Boolean;
18012 -- Determine whether list L contains element E
18014 --------------
18015 -- Contains --
18016 --------------
18018 function Contains
18019 (E : Entity_Id;
18020 L : Elist_Id) return Boolean
18022 List_Elmt : Elmt_Id;
18024 begin
18025 List_Elmt := First_Elmt (L);
18026 while Present (List_Elmt) loop
18027 if Node (List_Elmt) = E then
18028 return True;
18029 end if;
18031 Next_Elmt (List_Elmt);
18032 end loop;
18034 return False;
18035 end Contains;
18037 -- Start of processing
18039 begin
18040 if Is_Tagged_Type (Priv_T) then
18041 Priv_List := Primitive_Operations (Priv_T);
18042 Prim_Elmt := First_Elmt (Priv_List);
18044 -- In the case of a concurrent type completing a private tagged
18045 -- type, primitives may have been declared in between the two
18046 -- views. These subprograms need to be wrapped the same way
18047 -- entries and protected procedures are handled because they
18048 -- cannot be directly shared by the two views.
18050 if Is_Concurrent_Type (Full_T) then
18051 declare
18052 Conc_Typ : constant Entity_Id :=
18053 Corresponding_Record_Type (Full_T);
18054 Curr_Nod : Node_Id := Parent (Conc_Typ);
18055 Wrap_Spec : Node_Id;
18057 begin
18058 while Present (Prim_Elmt) loop
18059 Prim := Node (Prim_Elmt);
18061 if Comes_From_Source (Prim)
18062 and then not Is_Abstract_Subprogram (Prim)
18063 then
18064 Wrap_Spec :=
18065 Make_Subprogram_Declaration (Sloc (Prim),
18066 Specification =>
18067 Build_Wrapper_Spec
18068 (Subp_Id => Prim,
18069 Obj_Typ => Conc_Typ,
18070 Formals =>
18071 Parameter_Specifications (
18072 Parent (Prim))));
18074 Insert_After (Curr_Nod, Wrap_Spec);
18075 Curr_Nod := Wrap_Spec;
18077 Analyze (Wrap_Spec);
18078 end if;
18080 Next_Elmt (Prim_Elmt);
18081 end loop;
18083 return;
18084 end;
18086 -- For non-concurrent types, transfer explicit primitives, but
18087 -- omit those inherited from the parent of the private view
18088 -- since they will be re-inherited later on.
18090 else
18091 Full_List := Primitive_Operations (Full_T);
18093 while Present (Prim_Elmt) loop
18094 Prim := Node (Prim_Elmt);
18096 if Comes_From_Source (Prim)
18097 and then not Contains (Prim, Full_List)
18098 then
18099 Append_Elmt (Prim, Full_List);
18100 end if;
18102 Next_Elmt (Prim_Elmt);
18103 end loop;
18104 end if;
18106 -- Untagged private view
18108 else
18109 Full_List := Primitive_Operations (Full_T);
18111 -- In this case the partial view is untagged, so here we locate
18112 -- all of the earlier primitives that need to be treated as
18113 -- dispatching (those that appear between the two views). Note
18114 -- that these additional operations must all be new operations
18115 -- (any earlier operations that override inherited operations
18116 -- of the full view will already have been inserted in the
18117 -- primitives list, marked by Check_Operation_From_Private_View
18118 -- as dispatching. Note that implicit "/=" operators are
18119 -- excluded from being added to the primitives list since they
18120 -- shouldn't be treated as dispatching (tagged "/=" is handled
18121 -- specially).
18123 Prim := Next_Entity (Full_T);
18124 while Present (Prim) and then Prim /= Priv_T loop
18125 if Ekind_In (Prim, E_Procedure, E_Function) then
18126 Disp_Typ := Find_Dispatching_Type (Prim);
18128 if Disp_Typ = Full_T
18129 and then (Chars (Prim) /= Name_Op_Ne
18130 or else Comes_From_Source (Prim))
18131 then
18132 Check_Controlling_Formals (Full_T, Prim);
18134 if not Is_Dispatching_Operation (Prim) then
18135 Append_Elmt (Prim, Full_List);
18136 Set_Is_Dispatching_Operation (Prim, True);
18137 Set_DT_Position (Prim, No_Uint);
18138 end if;
18140 elsif Is_Dispatching_Operation (Prim)
18141 and then Disp_Typ /= Full_T
18142 then
18144 -- Verify that it is not otherwise controlled by a
18145 -- formal or a return value of type T.
18147 Check_Controlling_Formals (Disp_Typ, Prim);
18148 end if;
18149 end if;
18151 Next_Entity (Prim);
18152 end loop;
18153 end if;
18155 -- For the tagged case, the two views can share the same primitive
18156 -- operations list and the same class-wide type. Update attributes
18157 -- of the class-wide type which depend on the full declaration.
18159 if Is_Tagged_Type (Priv_T) then
18160 Set_Direct_Primitive_Operations (Priv_T, Full_List);
18161 Set_Class_Wide_Type
18162 (Base_Type (Full_T), Class_Wide_Type (Priv_T));
18164 Set_Has_Task (Class_Wide_Type (Priv_T), Has_Task (Full_T));
18165 end if;
18166 end;
18167 end if;
18169 -- Ada 2005 AI 161: Check preelaboratable initialization consistency
18171 if Known_To_Have_Preelab_Init (Priv_T) then
18173 -- Case where there is a pragma Preelaborable_Initialization. We
18174 -- always allow this in predefined units, which is a bit of a kludge,
18175 -- but it means we don't have to struggle to meet the requirements in
18176 -- the RM for having Preelaborable Initialization. Otherwise we
18177 -- require that the type meets the RM rules. But we can't check that
18178 -- yet, because of the rule about overriding Initialize, so we simply
18179 -- set a flag that will be checked at freeze time.
18181 if not In_Predefined_Unit (Full_T) then
18182 Set_Must_Have_Preelab_Init (Full_T);
18183 end if;
18184 end if;
18186 -- If pragma CPP_Class was applied to the private type declaration,
18187 -- propagate it now to the full type declaration.
18189 if Is_CPP_Class (Priv_T) then
18190 Set_Is_CPP_Class (Full_T);
18191 Set_Convention (Full_T, Convention_CPP);
18193 -- Check that components of imported CPP types do not have default
18194 -- expressions.
18196 Check_CPP_Type_Has_No_Defaults (Full_T);
18197 end if;
18199 -- If the private view has user specified stream attributes, then so has
18200 -- the full view.
18202 -- Why the test, how could these flags be already set in Full_T ???
18204 if Has_Specified_Stream_Read (Priv_T) then
18205 Set_Has_Specified_Stream_Read (Full_T);
18206 end if;
18208 if Has_Specified_Stream_Write (Priv_T) then
18209 Set_Has_Specified_Stream_Write (Full_T);
18210 end if;
18212 if Has_Specified_Stream_Input (Priv_T) then
18213 Set_Has_Specified_Stream_Input (Full_T);
18214 end if;
18216 if Has_Specified_Stream_Output (Priv_T) then
18217 Set_Has_Specified_Stream_Output (Full_T);
18218 end if;
18220 -- Propagate invariants to full type
18222 if Has_Invariants (Priv_T) then
18223 Set_Has_Invariants (Full_T);
18224 Set_Invariant_Procedure (Full_T, Invariant_Procedure (Priv_T));
18225 end if;
18227 if Has_Inheritable_Invariants (Priv_T) then
18228 Set_Has_Inheritable_Invariants (Full_T);
18229 end if;
18231 -- Propagate predicates to full type
18233 if Has_Predicates (Priv_T) then
18234 Set_Predicate_Function (Priv_T, Predicate_Function (Full_T));
18235 Set_Has_Predicates (Full_T);
18236 end if;
18237 end Process_Full_View;
18239 -----------------------------------
18240 -- Process_Incomplete_Dependents --
18241 -----------------------------------
18243 procedure Process_Incomplete_Dependents
18244 (N : Node_Id;
18245 Full_T : Entity_Id;
18246 Inc_T : Entity_Id)
18248 Inc_Elmt : Elmt_Id;
18249 Priv_Dep : Entity_Id;
18250 New_Subt : Entity_Id;
18252 Disc_Constraint : Elist_Id;
18254 begin
18255 if No (Private_Dependents (Inc_T)) then
18256 return;
18257 end if;
18259 -- Itypes that may be generated by the completion of an incomplete
18260 -- subtype are not used by the back-end and not attached to the tree.
18261 -- They are created only for constraint-checking purposes.
18263 Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
18264 while Present (Inc_Elmt) loop
18265 Priv_Dep := Node (Inc_Elmt);
18267 if Ekind (Priv_Dep) = E_Subprogram_Type then
18269 -- An Access_To_Subprogram type may have a return type or a
18270 -- parameter type that is incomplete. Replace with the full view.
18272 if Etype (Priv_Dep) = Inc_T then
18273 Set_Etype (Priv_Dep, Full_T);
18274 end if;
18276 declare
18277 Formal : Entity_Id;
18279 begin
18280 Formal := First_Formal (Priv_Dep);
18281 while Present (Formal) loop
18282 if Etype (Formal) = Inc_T then
18283 Set_Etype (Formal, Full_T);
18284 end if;
18286 Next_Formal (Formal);
18287 end loop;
18288 end;
18290 elsif Is_Overloadable (Priv_Dep) then
18292 -- If a subprogram in the incomplete dependents list is primitive
18293 -- for a tagged full type then mark it as a dispatching operation,
18294 -- check whether it overrides an inherited subprogram, and check
18295 -- restrictions on its controlling formals. Note that a protected
18296 -- operation is never dispatching: only its wrapper operation
18297 -- (which has convention Ada) is.
18299 if Is_Tagged_Type (Full_T)
18300 and then Is_Primitive (Priv_Dep)
18301 and then Convention (Priv_Dep) /= Convention_Protected
18302 then
18303 Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
18304 Set_Is_Dispatching_Operation (Priv_Dep);
18305 Check_Controlling_Formals (Full_T, Priv_Dep);
18306 end if;
18308 elsif Ekind (Priv_Dep) = E_Subprogram_Body then
18310 -- Can happen during processing of a body before the completion
18311 -- of a TA type. Ignore, because spec is also on dependent list.
18313 return;
18315 -- Ada 2005 (AI-412): Transform a regular incomplete subtype into a
18316 -- corresponding subtype of the full view.
18318 elsif Ekind (Priv_Dep) = E_Incomplete_Subtype then
18319 Set_Subtype_Indication
18320 (Parent (Priv_Dep), New_Reference_To (Full_T, Sloc (Priv_Dep)));
18321 Set_Etype (Priv_Dep, Full_T);
18322 Set_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
18323 Set_Analyzed (Parent (Priv_Dep), False);
18325 -- Reanalyze the declaration, suppressing the call to
18326 -- Enter_Name to avoid duplicate names.
18328 Analyze_Subtype_Declaration
18329 (N => Parent (Priv_Dep),
18330 Skip => True);
18332 -- Dependent is a subtype
18334 else
18335 -- We build a new subtype indication using the full view of the
18336 -- incomplete parent. The discriminant constraints have been
18337 -- elaborated already at the point of the subtype declaration.
18339 New_Subt := Create_Itype (E_Void, N);
18341 if Has_Discriminants (Full_T) then
18342 Disc_Constraint := Discriminant_Constraint (Priv_Dep);
18343 else
18344 Disc_Constraint := No_Elist;
18345 end if;
18347 Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
18348 Set_Full_View (Priv_Dep, New_Subt);
18349 end if;
18351 Next_Elmt (Inc_Elmt);
18352 end loop;
18353 end Process_Incomplete_Dependents;
18355 --------------------------------
18356 -- Process_Range_Expr_In_Decl --
18357 --------------------------------
18359 procedure Process_Range_Expr_In_Decl
18360 (R : Node_Id;
18361 T : Entity_Id;
18362 Check_List : List_Id := Empty_List;
18363 R_Check_Off : Boolean := False;
18364 In_Iter_Schm : Boolean := False)
18366 Lo, Hi : Node_Id;
18367 R_Checks : Check_Result;
18368 Insert_Node : Node_Id;
18369 Def_Id : Entity_Id;
18371 begin
18372 Analyze_And_Resolve (R, Base_Type (T));
18374 if Nkind (R) = N_Range then
18376 -- In SPARK, all ranges should be static, with the exception of the
18377 -- discrete type definition of a loop parameter specification.
18379 if not In_Iter_Schm
18380 and then not Is_Static_Range (R)
18381 then
18382 Check_SPARK_Restriction ("range should be static", R);
18383 end if;
18385 Lo := Low_Bound (R);
18386 Hi := High_Bound (R);
18388 -- We need to ensure validity of the bounds here, because if we
18389 -- go ahead and do the expansion, then the expanded code will get
18390 -- analyzed with range checks suppressed and we miss the check.
18392 Validity_Check_Range (R);
18394 -- If there were errors in the declaration, try and patch up some
18395 -- common mistakes in the bounds. The cases handled are literals
18396 -- which are Integer where the expected type is Real and vice versa.
18397 -- These corrections allow the compilation process to proceed further
18398 -- along since some basic assumptions of the format of the bounds
18399 -- are guaranteed.
18401 if Etype (R) = Any_Type then
18403 if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
18404 Rewrite (Lo,
18405 Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
18407 elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
18408 Rewrite (Hi,
18409 Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
18411 elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
18412 Rewrite (Lo,
18413 Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
18415 elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
18416 Rewrite (Hi,
18417 Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
18418 end if;
18420 Set_Etype (Lo, T);
18421 Set_Etype (Hi, T);
18422 end if;
18424 -- If the bounds of the range have been mistakenly given as string
18425 -- literals (perhaps in place of character literals), then an error
18426 -- has already been reported, but we rewrite the string literal as a
18427 -- bound of the range's type to avoid blowups in later processing
18428 -- that looks at static values.
18430 if Nkind (Lo) = N_String_Literal then
18431 Rewrite (Lo,
18432 Make_Attribute_Reference (Sloc (Lo),
18433 Attribute_Name => Name_First,
18434 Prefix => New_Reference_To (T, Sloc (Lo))));
18435 Analyze_And_Resolve (Lo);
18436 end if;
18438 if Nkind (Hi) = N_String_Literal then
18439 Rewrite (Hi,
18440 Make_Attribute_Reference (Sloc (Hi),
18441 Attribute_Name => Name_First,
18442 Prefix => New_Reference_To (T, Sloc (Hi))));
18443 Analyze_And_Resolve (Hi);
18444 end if;
18446 -- If bounds aren't scalar at this point then exit, avoiding
18447 -- problems with further processing of the range in this procedure.
18449 if not Is_Scalar_Type (Etype (Lo)) then
18450 return;
18451 end if;
18453 -- Resolve (actually Sem_Eval) has checked that the bounds are in
18454 -- then range of the base type. Here we check whether the bounds
18455 -- are in the range of the subtype itself. Note that if the bounds
18456 -- represent the null range the Constraint_Error exception should
18457 -- not be raised.
18459 -- ??? The following code should be cleaned up as follows
18461 -- 1. The Is_Null_Range (Lo, Hi) test should disappear since it
18462 -- is done in the call to Range_Check (R, T); below
18464 -- 2. The use of R_Check_Off should be investigated and possibly
18465 -- removed, this would clean up things a bit.
18467 if Is_Null_Range (Lo, Hi) then
18468 null;
18470 else
18471 -- Capture values of bounds and generate temporaries for them
18472 -- if needed, before applying checks, since checks may cause
18473 -- duplication of the expression without forcing evaluation.
18475 -- The forced evaluation removes side effects from expressions,
18476 -- which should occur also in Alfa mode. Otherwise, we end up with
18477 -- unexpected insertions of actions at places where this is not
18478 -- supposed to occur, e.g. on default parameters of a call.
18480 if Expander_Active then
18481 Force_Evaluation (Lo);
18482 Force_Evaluation (Hi);
18483 end if;
18485 -- We use a flag here instead of suppressing checks on the
18486 -- type because the type we check against isn't necessarily
18487 -- the place where we put the check.
18489 if not R_Check_Off then
18490 R_Checks := Get_Range_Checks (R, T);
18492 -- Look up tree to find an appropriate insertion point. We
18493 -- can't just use insert_actions because later processing
18494 -- depends on the insertion node. Prior to Ada 2012 the
18495 -- insertion point could only be a declaration or a loop, but
18496 -- quantified expressions can appear within any context in an
18497 -- expression, and the insertion point can be any statement,
18498 -- pragma, or declaration.
18500 Insert_Node := Parent (R);
18501 while Present (Insert_Node) loop
18502 exit when
18503 Nkind (Insert_Node) in N_Declaration
18504 and then
18505 not Nkind_In
18506 (Insert_Node, N_Component_Declaration,
18507 N_Loop_Parameter_Specification,
18508 N_Function_Specification,
18509 N_Procedure_Specification);
18511 exit when Nkind (Insert_Node) in N_Later_Decl_Item
18512 or else Nkind (Insert_Node) in
18513 N_Statement_Other_Than_Procedure_Call
18514 or else Nkind_In (Insert_Node, N_Procedure_Call_Statement,
18515 N_Pragma);
18517 Insert_Node := Parent (Insert_Node);
18518 end loop;
18520 -- Why would Type_Decl not be present??? Without this test,
18521 -- short regression tests fail.
18523 if Present (Insert_Node) then
18525 -- Case of loop statement. Verify that the range is part
18526 -- of the subtype indication of the iteration scheme.
18528 if Nkind (Insert_Node) = N_Loop_Statement then
18529 declare
18530 Indic : Node_Id;
18532 begin
18533 Indic := Parent (R);
18534 while Present (Indic)
18535 and then Nkind (Indic) /= N_Subtype_Indication
18536 loop
18537 Indic := Parent (Indic);
18538 end loop;
18540 if Present (Indic) then
18541 Def_Id := Etype (Subtype_Mark (Indic));
18543 Insert_Range_Checks
18544 (R_Checks,
18545 Insert_Node,
18546 Def_Id,
18547 Sloc (Insert_Node),
18549 Do_Before => True);
18550 end if;
18551 end;
18553 -- Insertion before a declaration. If the declaration
18554 -- includes discriminants, the list of applicable checks
18555 -- is given by the caller.
18557 elsif Nkind (Insert_Node) in N_Declaration then
18558 Def_Id := Defining_Identifier (Insert_Node);
18560 if (Ekind (Def_Id) = E_Record_Type
18561 and then Depends_On_Discriminant (R))
18562 or else
18563 (Ekind (Def_Id) = E_Protected_Type
18564 and then Has_Discriminants (Def_Id))
18565 then
18566 Append_Range_Checks
18567 (R_Checks,
18568 Check_List, Def_Id, Sloc (Insert_Node), R);
18570 else
18571 Insert_Range_Checks
18572 (R_Checks,
18573 Insert_Node, Def_Id, Sloc (Insert_Node), R);
18575 end if;
18577 -- Insertion before a statement. Range appears in the
18578 -- context of a quantified expression. Insertion will
18579 -- take place when expression is expanded.
18581 else
18582 null;
18583 end if;
18584 end if;
18585 end if;
18586 end if;
18588 -- Case of other than an explicit N_Range node
18590 -- The forced evaluation removes side effects from expressions, which
18591 -- should occur also in Alfa mode. Otherwise, we end up with unexpected
18592 -- insertions of actions at places where this is not supposed to occur,
18593 -- e.g. on default parameters of a call.
18595 elsif Expander_Active then
18596 Get_Index_Bounds (R, Lo, Hi);
18597 Force_Evaluation (Lo);
18598 Force_Evaluation (Hi);
18599 end if;
18600 end Process_Range_Expr_In_Decl;
18602 --------------------------------------
18603 -- Process_Real_Range_Specification --
18604 --------------------------------------
18606 procedure Process_Real_Range_Specification (Def : Node_Id) is
18607 Spec : constant Node_Id := Real_Range_Specification (Def);
18608 Lo : Node_Id;
18609 Hi : Node_Id;
18610 Err : Boolean := False;
18612 procedure Analyze_Bound (N : Node_Id);
18613 -- Analyze and check one bound
18615 -------------------
18616 -- Analyze_Bound --
18617 -------------------
18619 procedure Analyze_Bound (N : Node_Id) is
18620 begin
18621 Analyze_And_Resolve (N, Any_Real);
18623 if not Is_OK_Static_Expression (N) then
18624 Flag_Non_Static_Expr
18625 ("bound in real type definition is not static!", N);
18626 Err := True;
18627 end if;
18628 end Analyze_Bound;
18630 -- Start of processing for Process_Real_Range_Specification
18632 begin
18633 if Present (Spec) then
18634 Lo := Low_Bound (Spec);
18635 Hi := High_Bound (Spec);
18636 Analyze_Bound (Lo);
18637 Analyze_Bound (Hi);
18639 -- If error, clear away junk range specification
18641 if Err then
18642 Set_Real_Range_Specification (Def, Empty);
18643 end if;
18644 end if;
18645 end Process_Real_Range_Specification;
18647 ---------------------
18648 -- Process_Subtype --
18649 ---------------------
18651 function Process_Subtype
18652 (S : Node_Id;
18653 Related_Nod : Node_Id;
18654 Related_Id : Entity_Id := Empty;
18655 Suffix : Character := ' ') return Entity_Id
18657 P : Node_Id;
18658 Def_Id : Entity_Id;
18659 Error_Node : Node_Id;
18660 Full_View_Id : Entity_Id;
18661 Subtype_Mark_Id : Entity_Id;
18663 May_Have_Null_Exclusion : Boolean;
18665 procedure Check_Incomplete (T : Entity_Id);
18666 -- Called to verify that an incomplete type is not used prematurely
18668 ----------------------
18669 -- Check_Incomplete --
18670 ----------------------
18672 procedure Check_Incomplete (T : Entity_Id) is
18673 begin
18674 -- Ada 2005 (AI-412): Incomplete subtypes are legal
18676 if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
18677 and then
18678 not (Ada_Version >= Ada_2005
18679 and then
18680 (Nkind (Parent (T)) = N_Subtype_Declaration
18681 or else
18682 (Nkind (Parent (T)) = N_Subtype_Indication
18683 and then Nkind (Parent (Parent (T))) =
18684 N_Subtype_Declaration)))
18685 then
18686 Error_Msg_N ("invalid use of type before its full declaration", T);
18687 end if;
18688 end Check_Incomplete;
18690 -- Start of processing for Process_Subtype
18692 begin
18693 -- Case of no constraints present
18695 if Nkind (S) /= N_Subtype_Indication then
18696 Find_Type (S);
18697 Check_Incomplete (S);
18698 P := Parent (S);
18700 -- Ada 2005 (AI-231): Static check
18702 if Ada_Version >= Ada_2005
18703 and then Present (P)
18704 and then Null_Exclusion_Present (P)
18705 and then Nkind (P) /= N_Access_To_Object_Definition
18706 and then not Is_Access_Type (Entity (S))
18707 then
18708 Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
18709 end if;
18711 -- The following is ugly, can't we have a range or even a flag???
18713 May_Have_Null_Exclusion :=
18714 Nkind_In (P, N_Access_Definition,
18715 N_Access_Function_Definition,
18716 N_Access_Procedure_Definition,
18717 N_Access_To_Object_Definition,
18718 N_Allocator,
18719 N_Component_Definition)
18720 or else
18721 Nkind_In (P, N_Derived_Type_Definition,
18722 N_Discriminant_Specification,
18723 N_Formal_Object_Declaration,
18724 N_Object_Declaration,
18725 N_Object_Renaming_Declaration,
18726 N_Parameter_Specification,
18727 N_Subtype_Declaration);
18729 -- Create an Itype that is a duplicate of Entity (S) but with the
18730 -- null-exclusion attribute.
18732 if May_Have_Null_Exclusion
18733 and then Is_Access_Type (Entity (S))
18734 and then Null_Exclusion_Present (P)
18736 -- No need to check the case of an access to object definition.
18737 -- It is correct to define double not-null pointers.
18739 -- Example:
18740 -- type Not_Null_Int_Ptr is not null access Integer;
18741 -- type Acc is not null access Not_Null_Int_Ptr;
18743 and then Nkind (P) /= N_Access_To_Object_Definition
18744 then
18745 if Can_Never_Be_Null (Entity (S)) then
18746 case Nkind (Related_Nod) is
18747 when N_Full_Type_Declaration =>
18748 if Nkind (Type_Definition (Related_Nod))
18749 in N_Array_Type_Definition
18750 then
18751 Error_Node :=
18752 Subtype_Indication
18753 (Component_Definition
18754 (Type_Definition (Related_Nod)));
18755 else
18756 Error_Node :=
18757 Subtype_Indication (Type_Definition (Related_Nod));
18758 end if;
18760 when N_Subtype_Declaration =>
18761 Error_Node := Subtype_Indication (Related_Nod);
18763 when N_Object_Declaration =>
18764 Error_Node := Object_Definition (Related_Nod);
18766 when N_Component_Declaration =>
18767 Error_Node :=
18768 Subtype_Indication (Component_Definition (Related_Nod));
18770 when N_Allocator =>
18771 Error_Node := Expression (Related_Nod);
18773 when others =>
18774 pragma Assert (False);
18775 Error_Node := Related_Nod;
18776 end case;
18778 Error_Msg_NE
18779 ("`NOT NULL` not allowed (& already excludes null)",
18780 Error_Node,
18781 Entity (S));
18782 end if;
18784 Set_Etype (S,
18785 Create_Null_Excluding_Itype
18786 (T => Entity (S),
18787 Related_Nod => P));
18788 Set_Entity (S, Etype (S));
18789 end if;
18791 return Entity (S);
18793 -- Case of constraint present, so that we have an N_Subtype_Indication
18794 -- node (this node is created only if constraints are present).
18796 else
18797 Find_Type (Subtype_Mark (S));
18799 if Nkind (Parent (S)) /= N_Access_To_Object_Definition
18800 and then not
18801 (Nkind (Parent (S)) = N_Subtype_Declaration
18802 and then Is_Itype (Defining_Identifier (Parent (S))))
18803 then
18804 Check_Incomplete (Subtype_Mark (S));
18805 end if;
18807 P := Parent (S);
18808 Subtype_Mark_Id := Entity (Subtype_Mark (S));
18810 -- Explicit subtype declaration case
18812 if Nkind (P) = N_Subtype_Declaration then
18813 Def_Id := Defining_Identifier (P);
18815 -- Explicit derived type definition case
18817 elsif Nkind (P) = N_Derived_Type_Definition then
18818 Def_Id := Defining_Identifier (Parent (P));
18820 -- Implicit case, the Def_Id must be created as an implicit type.
18821 -- The one exception arises in the case of concurrent types, array
18822 -- and access types, where other subsidiary implicit types may be
18823 -- created and must appear before the main implicit type. In these
18824 -- cases we leave Def_Id set to Empty as a signal that Create_Itype
18825 -- has not yet been called to create Def_Id.
18827 else
18828 if Is_Array_Type (Subtype_Mark_Id)
18829 or else Is_Concurrent_Type (Subtype_Mark_Id)
18830 or else Is_Access_Type (Subtype_Mark_Id)
18831 then
18832 Def_Id := Empty;
18834 -- For the other cases, we create a new unattached Itype,
18835 -- and set the indication to ensure it gets attached later.
18837 else
18838 Def_Id :=
18839 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
18840 end if;
18841 end if;
18843 -- If the kind of constraint is invalid for this kind of type,
18844 -- then give an error, and then pretend no constraint was given.
18846 if not Is_Valid_Constraint_Kind
18847 (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
18848 then
18849 Error_Msg_N
18850 ("incorrect constraint for this kind of type", Constraint (S));
18852 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
18854 -- Set Ekind of orphan itype, to prevent cascaded errors
18856 if Present (Def_Id) then
18857 Set_Ekind (Def_Id, Ekind (Any_Type));
18858 end if;
18860 -- Make recursive call, having got rid of the bogus constraint
18862 return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
18863 end if;
18865 -- Remaining processing depends on type. Select on Base_Type kind to
18866 -- ensure getting to the concrete type kind in the case of a private
18867 -- subtype (needed when only doing semantic analysis).
18869 case Ekind (Base_Type (Subtype_Mark_Id)) is
18870 when Access_Kind =>
18871 Constrain_Access (Def_Id, S, Related_Nod);
18873 if Expander_Active
18874 and then Is_Itype (Designated_Type (Def_Id))
18875 and then Nkind (Related_Nod) = N_Subtype_Declaration
18876 and then not Is_Incomplete_Type (Designated_Type (Def_Id))
18877 then
18878 Build_Itype_Reference
18879 (Designated_Type (Def_Id), Related_Nod);
18880 end if;
18882 when Array_Kind =>
18883 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
18885 when Decimal_Fixed_Point_Kind =>
18886 Constrain_Decimal (Def_Id, S);
18888 when Enumeration_Kind =>
18889 Constrain_Enumeration (Def_Id, S);
18891 when Ordinary_Fixed_Point_Kind =>
18892 Constrain_Ordinary_Fixed (Def_Id, S);
18894 when Float_Kind =>
18895 Constrain_Float (Def_Id, S);
18897 when Integer_Kind =>
18898 Constrain_Integer (Def_Id, S);
18900 when E_Record_Type |
18901 E_Record_Subtype |
18902 Class_Wide_Kind |
18903 E_Incomplete_Type =>
18904 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
18906 if Ekind (Def_Id) = E_Incomplete_Type then
18907 Set_Private_Dependents (Def_Id, New_Elmt_List);
18908 end if;
18910 when Private_Kind =>
18911 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
18912 Set_Private_Dependents (Def_Id, New_Elmt_List);
18914 -- In case of an invalid constraint prevent further processing
18915 -- since the type constructed is missing expected fields.
18917 if Etype (Def_Id) = Any_Type then
18918 return Def_Id;
18919 end if;
18921 -- If the full view is that of a task with discriminants,
18922 -- we must constrain both the concurrent type and its
18923 -- corresponding record type. Otherwise we will just propagate
18924 -- the constraint to the full view, if available.
18926 if Present (Full_View (Subtype_Mark_Id))
18927 and then Has_Discriminants (Subtype_Mark_Id)
18928 and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
18929 then
18930 Full_View_Id :=
18931 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
18933 Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
18934 Constrain_Concurrent (Full_View_Id, S,
18935 Related_Nod, Related_Id, Suffix);
18936 Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
18937 Set_Full_View (Def_Id, Full_View_Id);
18939 -- Introduce an explicit reference to the private subtype,
18940 -- to prevent scope anomalies in gigi if first use appears
18941 -- in a nested context, e.g. a later function body.
18942 -- Should this be generated in other contexts than a full
18943 -- type declaration?
18945 if Is_Itype (Def_Id)
18946 and then
18947 Nkind (Parent (P)) = N_Full_Type_Declaration
18948 then
18949 Build_Itype_Reference (Def_Id, Parent (P));
18950 end if;
18952 else
18953 Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
18954 end if;
18956 when Concurrent_Kind =>
18957 Constrain_Concurrent (Def_Id, S,
18958 Related_Nod, Related_Id, Suffix);
18960 when others =>
18961 Error_Msg_N ("invalid subtype mark in subtype indication", S);
18962 end case;
18964 -- Size and Convention are always inherited from the base type
18966 Set_Size_Info (Def_Id, (Subtype_Mark_Id));
18967 Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
18969 return Def_Id;
18970 end if;
18971 end Process_Subtype;
18973 ---------------------------------------
18974 -- Check_Anonymous_Access_Components --
18975 ---------------------------------------
18977 procedure Check_Anonymous_Access_Components
18978 (Typ_Decl : Node_Id;
18979 Typ : Entity_Id;
18980 Prev : Entity_Id;
18981 Comp_List : Node_Id)
18983 Loc : constant Source_Ptr := Sloc (Typ_Decl);
18984 Anon_Access : Entity_Id;
18985 Acc_Def : Node_Id;
18986 Comp : Node_Id;
18987 Comp_Def : Node_Id;
18988 Decl : Node_Id;
18989 Type_Def : Node_Id;
18991 procedure Build_Incomplete_Type_Declaration;
18992 -- If the record type contains components that include an access to the
18993 -- current record, then create an incomplete type declaration for the
18994 -- record, to be used as the designated type of the anonymous access.
18995 -- This is done only once, and only if there is no previous partial
18996 -- view of the type.
18998 function Designates_T (Subt : Node_Id) return Boolean;
18999 -- Check whether a node designates the enclosing record type, or 'Class
19000 -- of that type
19002 function Mentions_T (Acc_Def : Node_Id) return Boolean;
19003 -- Check whether an access definition includes a reference to
19004 -- the enclosing record type. The reference can be a subtype mark
19005 -- in the access definition itself, a 'Class attribute reference, or
19006 -- recursively a reference appearing in a parameter specification
19007 -- or result definition of an access_to_subprogram definition.
19009 --------------------------------------
19010 -- Build_Incomplete_Type_Declaration --
19011 --------------------------------------
19013 procedure Build_Incomplete_Type_Declaration is
19014 Decl : Node_Id;
19015 Inc_T : Entity_Id;
19016 H : Entity_Id;
19018 -- Is_Tagged indicates whether the type is tagged. It is tagged if
19019 -- it's "is new ... with record" or else "is tagged record ...".
19021 Is_Tagged : constant Boolean :=
19022 (Nkind (Type_Definition (Typ_Decl)) = N_Derived_Type_Definition
19023 and then
19024 Present
19025 (Record_Extension_Part (Type_Definition (Typ_Decl))))
19026 or else
19027 (Nkind (Type_Definition (Typ_Decl)) = N_Record_Definition
19028 and then Tagged_Present (Type_Definition (Typ_Decl)));
19030 begin
19031 -- If there is a previous partial view, no need to create a new one
19032 -- If the partial view, given by Prev, is incomplete, If Prev is
19033 -- a private declaration, full declaration is flagged accordingly.
19035 if Prev /= Typ then
19036 if Is_Tagged then
19037 Make_Class_Wide_Type (Prev);
19038 Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
19039 Set_Etype (Class_Wide_Type (Typ), Typ);
19040 end if;
19042 return;
19044 elsif Has_Private_Declaration (Typ) then
19046 -- If we refer to T'Class inside T, and T is the completion of a
19047 -- private type, then we need to make sure the class-wide type
19048 -- exists.
19050 if Is_Tagged then
19051 Make_Class_Wide_Type (Typ);
19052 end if;
19054 return;
19056 -- If there was a previous anonymous access type, the incomplete
19057 -- type declaration will have been created already.
19059 elsif Present (Current_Entity (Typ))
19060 and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
19061 and then Full_View (Current_Entity (Typ)) = Typ
19062 then
19063 if Is_Tagged
19064 and then Comes_From_Source (Current_Entity (Typ))
19065 and then not Is_Tagged_Type (Current_Entity (Typ))
19066 then
19067 Make_Class_Wide_Type (Typ);
19068 Error_Msg_N
19069 ("incomplete view of tagged type should be declared tagged?",
19070 Parent (Current_Entity (Typ)));
19071 end if;
19072 return;
19074 else
19075 Inc_T := Make_Defining_Identifier (Loc, Chars (Typ));
19076 Decl := Make_Incomplete_Type_Declaration (Loc, Inc_T);
19078 -- Type has already been inserted into the current scope. Remove
19079 -- it, and add incomplete declaration for type, so that subsequent
19080 -- anonymous access types can use it. The entity is unchained from
19081 -- the homonym list and from immediate visibility. After analysis,
19082 -- the entity in the incomplete declaration becomes immediately
19083 -- visible in the record declaration that follows.
19085 H := Current_Entity (Typ);
19087 if H = Typ then
19088 Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
19089 else
19090 while Present (H)
19091 and then Homonym (H) /= Typ
19092 loop
19093 H := Homonym (Typ);
19094 end loop;
19096 Set_Homonym (H, Homonym (Typ));
19097 end if;
19099 Insert_Before (Typ_Decl, Decl);
19100 Analyze (Decl);
19101 Set_Full_View (Inc_T, Typ);
19103 if Is_Tagged then
19105 -- Create a common class-wide type for both views, and set the
19106 -- Etype of the class-wide type to the full view.
19108 Make_Class_Wide_Type (Inc_T);
19109 Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
19110 Set_Etype (Class_Wide_Type (Typ), Typ);
19111 end if;
19112 end if;
19113 end Build_Incomplete_Type_Declaration;
19115 ------------------
19116 -- Designates_T --
19117 ------------------
19119 function Designates_T (Subt : Node_Id) return Boolean is
19120 Type_Id : constant Name_Id := Chars (Typ);
19122 function Names_T (Nam : Node_Id) return Boolean;
19123 -- The record type has not been introduced in the current scope
19124 -- yet, so we must examine the name of the type itself, either
19125 -- an identifier T, or an expanded name of the form P.T, where
19126 -- P denotes the current scope.
19128 -------------
19129 -- Names_T --
19130 -------------
19132 function Names_T (Nam : Node_Id) return Boolean is
19133 begin
19134 if Nkind (Nam) = N_Identifier then
19135 return Chars (Nam) = Type_Id;
19137 elsif Nkind (Nam) = N_Selected_Component then
19138 if Chars (Selector_Name (Nam)) = Type_Id then
19139 if Nkind (Prefix (Nam)) = N_Identifier then
19140 return Chars (Prefix (Nam)) = Chars (Current_Scope);
19142 elsif Nkind (Prefix (Nam)) = N_Selected_Component then
19143 return Chars (Selector_Name (Prefix (Nam))) =
19144 Chars (Current_Scope);
19145 else
19146 return False;
19147 end if;
19149 else
19150 return False;
19151 end if;
19153 else
19154 return False;
19155 end if;
19156 end Names_T;
19158 -- Start of processing for Designates_T
19160 begin
19161 if Nkind (Subt) = N_Identifier then
19162 return Chars (Subt) = Type_Id;
19164 -- Reference can be through an expanded name which has not been
19165 -- analyzed yet, and which designates enclosing scopes.
19167 elsif Nkind (Subt) = N_Selected_Component then
19168 if Names_T (Subt) then
19169 return True;
19171 -- Otherwise it must denote an entity that is already visible.
19172 -- The access definition may name a subtype of the enclosing
19173 -- type, if there is a previous incomplete declaration for it.
19175 else
19176 Find_Selected_Component (Subt);
19177 return
19178 Is_Entity_Name (Subt)
19179 and then Scope (Entity (Subt)) = Current_Scope
19180 and then
19181 (Chars (Base_Type (Entity (Subt))) = Type_Id
19182 or else
19183 (Is_Class_Wide_Type (Entity (Subt))
19184 and then
19185 Chars (Etype (Base_Type (Entity (Subt)))) =
19186 Type_Id));
19187 end if;
19189 -- A reference to the current type may appear as the prefix of
19190 -- a 'Class attribute.
19192 elsif Nkind (Subt) = N_Attribute_Reference
19193 and then Attribute_Name (Subt) = Name_Class
19194 then
19195 return Names_T (Prefix (Subt));
19197 else
19198 return False;
19199 end if;
19200 end Designates_T;
19202 ----------------
19203 -- Mentions_T --
19204 ----------------
19206 function Mentions_T (Acc_Def : Node_Id) return Boolean is
19207 Param_Spec : Node_Id;
19209 Acc_Subprg : constant Node_Id :=
19210 Access_To_Subprogram_Definition (Acc_Def);
19212 begin
19213 if No (Acc_Subprg) then
19214 return Designates_T (Subtype_Mark (Acc_Def));
19215 end if;
19217 -- Component is an access_to_subprogram: examine its formals,
19218 -- and result definition in the case of an access_to_function.
19220 Param_Spec := First (Parameter_Specifications (Acc_Subprg));
19221 while Present (Param_Spec) loop
19222 if Nkind (Parameter_Type (Param_Spec)) = N_Access_Definition
19223 and then Mentions_T (Parameter_Type (Param_Spec))
19224 then
19225 return True;
19227 elsif Designates_T (Parameter_Type (Param_Spec)) then
19228 return True;
19229 end if;
19231 Next (Param_Spec);
19232 end loop;
19234 if Nkind (Acc_Subprg) = N_Access_Function_Definition then
19235 if Nkind (Result_Definition (Acc_Subprg)) =
19236 N_Access_Definition
19237 then
19238 return Mentions_T (Result_Definition (Acc_Subprg));
19239 else
19240 return Designates_T (Result_Definition (Acc_Subprg));
19241 end if;
19242 end if;
19244 return False;
19245 end Mentions_T;
19247 -- Start of processing for Check_Anonymous_Access_Components
19249 begin
19250 if No (Comp_List) then
19251 return;
19252 end if;
19254 Comp := First (Component_Items (Comp_List));
19255 while Present (Comp) loop
19256 if Nkind (Comp) = N_Component_Declaration
19257 and then Present
19258 (Access_Definition (Component_Definition (Comp)))
19259 and then
19260 Mentions_T (Access_Definition (Component_Definition (Comp)))
19261 then
19262 Comp_Def := Component_Definition (Comp);
19263 Acc_Def :=
19264 Access_To_Subprogram_Definition
19265 (Access_Definition (Comp_Def));
19267 Build_Incomplete_Type_Declaration;
19268 Anon_Access := Make_Temporary (Loc, 'S');
19270 -- Create a declaration for the anonymous access type: either
19271 -- an access_to_object or an access_to_subprogram.
19273 if Present (Acc_Def) then
19274 if Nkind (Acc_Def) = N_Access_Function_Definition then
19275 Type_Def :=
19276 Make_Access_Function_Definition (Loc,
19277 Parameter_Specifications =>
19278 Parameter_Specifications (Acc_Def),
19279 Result_Definition => Result_Definition (Acc_Def));
19280 else
19281 Type_Def :=
19282 Make_Access_Procedure_Definition (Loc,
19283 Parameter_Specifications =>
19284 Parameter_Specifications (Acc_Def));
19285 end if;
19287 else
19288 Type_Def :=
19289 Make_Access_To_Object_Definition (Loc,
19290 Subtype_Indication =>
19291 Relocate_Node
19292 (Subtype_Mark
19293 (Access_Definition (Comp_Def))));
19295 Set_Constant_Present
19296 (Type_Def, Constant_Present (Access_Definition (Comp_Def)));
19297 Set_All_Present
19298 (Type_Def, All_Present (Access_Definition (Comp_Def)));
19299 end if;
19301 Set_Null_Exclusion_Present
19302 (Type_Def,
19303 Null_Exclusion_Present (Access_Definition (Comp_Def)));
19305 Decl :=
19306 Make_Full_Type_Declaration (Loc,
19307 Defining_Identifier => Anon_Access,
19308 Type_Definition => Type_Def);
19310 Insert_Before (Typ_Decl, Decl);
19311 Analyze (Decl);
19313 -- If an access to subprogram, create the extra formals
19315 if Present (Acc_Def) then
19316 Create_Extra_Formals (Designated_Type (Anon_Access));
19318 -- If an access to object, preserve entity of designated type,
19319 -- for ASIS use, before rewriting the component definition.
19321 else
19322 declare
19323 Desig : Entity_Id;
19325 begin
19326 Desig := Entity (Subtype_Indication (Type_Def));
19328 -- If the access definition is to the current record,
19329 -- the visible entity at this point is an incomplete
19330 -- type. Retrieve the full view to simplify ASIS queries
19332 if Ekind (Desig) = E_Incomplete_Type then
19333 Desig := Full_View (Desig);
19334 end if;
19336 Set_Entity
19337 (Subtype_Mark (Access_Definition (Comp_Def)), Desig);
19338 end;
19339 end if;
19341 Rewrite (Comp_Def,
19342 Make_Component_Definition (Loc,
19343 Subtype_Indication =>
19344 New_Occurrence_Of (Anon_Access, Loc)));
19346 if Ekind (Designated_Type (Anon_Access)) = E_Subprogram_Type then
19347 Set_Ekind (Anon_Access, E_Anonymous_Access_Subprogram_Type);
19348 else
19349 Set_Ekind (Anon_Access, E_Anonymous_Access_Type);
19350 end if;
19352 Set_Is_Local_Anonymous_Access (Anon_Access);
19353 end if;
19355 Next (Comp);
19356 end loop;
19358 if Present (Variant_Part (Comp_List)) then
19359 declare
19360 V : Node_Id;
19361 begin
19362 V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
19363 while Present (V) loop
19364 Check_Anonymous_Access_Components
19365 (Typ_Decl, Typ, Prev, Component_List (V));
19366 Next_Non_Pragma (V);
19367 end loop;
19368 end;
19369 end if;
19370 end Check_Anonymous_Access_Components;
19372 ----------------------------------
19373 -- Preanalyze_Assert_Expression --
19374 ----------------------------------
19376 procedure Preanalyze_Assert_Expression (N : Node_Id; T : Entity_Id) is
19377 begin
19378 In_Assertion_Expr := In_Assertion_Expr + 1;
19379 Preanalyze_Spec_Expression (N, T);
19380 In_Assertion_Expr := In_Assertion_Expr - 1;
19381 end Preanalyze_Assert_Expression;
19383 --------------------------------
19384 -- Preanalyze_Spec_Expression --
19385 --------------------------------
19387 procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
19388 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
19389 begin
19390 In_Spec_Expression := True;
19391 Preanalyze_And_Resolve (N, T);
19392 In_Spec_Expression := Save_In_Spec_Expression;
19393 end Preanalyze_Spec_Expression;
19395 -----------------------------
19396 -- Record_Type_Declaration --
19397 -----------------------------
19399 procedure Record_Type_Declaration
19400 (T : Entity_Id;
19401 N : Node_Id;
19402 Prev : Entity_Id)
19404 Def : constant Node_Id := Type_Definition (N);
19405 Is_Tagged : Boolean;
19406 Tag_Comp : Entity_Id;
19408 begin
19409 -- These flags must be initialized before calling Process_Discriminants
19410 -- because this routine makes use of them.
19412 Set_Ekind (T, E_Record_Type);
19413 Set_Etype (T, T);
19414 Init_Size_Align (T);
19415 Set_Interfaces (T, No_Elist);
19416 Set_Stored_Constraint (T, No_Elist);
19418 -- Normal case
19420 if Ada_Version < Ada_2005
19421 or else not Interface_Present (Def)
19422 then
19423 if Limited_Present (Def) then
19424 Check_SPARK_Restriction ("limited is not allowed", N);
19425 end if;
19427 if Abstract_Present (Def) then
19428 Check_SPARK_Restriction ("abstract is not allowed", N);
19429 end if;
19431 -- The flag Is_Tagged_Type might have already been set by
19432 -- Find_Type_Name if it detected an error for declaration T. This
19433 -- arises in the case of private tagged types where the full view
19434 -- omits the word tagged.
19436 Is_Tagged :=
19437 Tagged_Present (Def)
19438 or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
19440 Set_Is_Tagged_Type (T, Is_Tagged);
19441 Set_Is_Limited_Record (T, Limited_Present (Def));
19443 -- Type is abstract if full declaration carries keyword, or if
19444 -- previous partial view did.
19446 Set_Is_Abstract_Type (T, Is_Abstract_Type (T)
19447 or else Abstract_Present (Def));
19449 else
19450 Check_SPARK_Restriction ("interface is not allowed", N);
19452 Is_Tagged := True;
19453 Analyze_Interface_Declaration (T, Def);
19455 if Present (Discriminant_Specifications (N)) then
19456 Error_Msg_N
19457 ("interface types cannot have discriminants",
19458 Defining_Identifier
19459 (First (Discriminant_Specifications (N))));
19460 end if;
19461 end if;
19463 -- First pass: if there are self-referential access components,
19464 -- create the required anonymous access type declarations, and if
19465 -- need be an incomplete type declaration for T itself.
19467 Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
19469 if Ada_Version >= Ada_2005
19470 and then Present (Interface_List (Def))
19471 then
19472 Check_Interfaces (N, Def);
19474 declare
19475 Ifaces_List : Elist_Id;
19477 begin
19478 -- Ada 2005 (AI-251): Collect the list of progenitors that are not
19479 -- already in the parents.
19481 Collect_Interfaces
19482 (T => T,
19483 Ifaces_List => Ifaces_List,
19484 Exclude_Parents => True);
19486 Set_Interfaces (T, Ifaces_List);
19487 end;
19488 end if;
19490 -- Records constitute a scope for the component declarations within.
19491 -- The scope is created prior to the processing of these declarations.
19492 -- Discriminants are processed first, so that they are visible when
19493 -- processing the other components. The Ekind of the record type itself
19494 -- is set to E_Record_Type (subtypes appear as E_Record_Subtype).
19496 -- Enter record scope
19498 Push_Scope (T);
19500 -- If an incomplete or private type declaration was already given for
19501 -- the type, then this scope already exists, and the discriminants have
19502 -- been declared within. We must verify that the full declaration
19503 -- matches the incomplete one.
19505 Check_Or_Process_Discriminants (N, T, Prev);
19507 Set_Is_Constrained (T, not Has_Discriminants (T));
19508 Set_Has_Delayed_Freeze (T, True);
19510 -- For tagged types add a manually analyzed component corresponding
19511 -- to the component _tag, the corresponding piece of tree will be
19512 -- expanded as part of the freezing actions if it is not a CPP_Class.
19514 if Is_Tagged then
19516 -- Do not add the tag unless we are in expansion mode
19518 if Expander_Active then
19519 Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
19520 Enter_Name (Tag_Comp);
19522 Set_Ekind (Tag_Comp, E_Component);
19523 Set_Is_Tag (Tag_Comp);
19524 Set_Is_Aliased (Tag_Comp);
19525 Set_Etype (Tag_Comp, RTE (RE_Tag));
19526 Set_DT_Entry_Count (Tag_Comp, No_Uint);
19527 Set_Original_Record_Component (Tag_Comp, Tag_Comp);
19528 Init_Component_Location (Tag_Comp);
19530 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
19531 -- implemented interfaces.
19533 if Has_Interfaces (T) then
19534 Add_Interface_Tag_Components (N, T);
19535 end if;
19536 end if;
19538 Make_Class_Wide_Type (T);
19539 Set_Direct_Primitive_Operations (T, New_Elmt_List);
19540 end if;
19542 -- We must suppress range checks when processing record components in
19543 -- the presence of discriminants, since we don't want spurious checks to
19544 -- be generated during their analysis, but Suppress_Range_Checks flags
19545 -- must be reset the after processing the record definition.
19547 -- Note: this is the only use of Kill_Range_Checks, and is a bit odd,
19548 -- couldn't we just use the normal range check suppression method here.
19549 -- That would seem cleaner ???
19551 if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
19552 Set_Kill_Range_Checks (T, True);
19553 Record_Type_Definition (Def, Prev);
19554 Set_Kill_Range_Checks (T, False);
19555 else
19556 Record_Type_Definition (Def, Prev);
19557 end if;
19559 -- Exit from record scope
19561 End_Scope;
19563 -- Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
19564 -- the implemented interfaces and associate them an aliased entity.
19566 if Is_Tagged
19567 and then not Is_Empty_List (Interface_List (Def))
19568 then
19569 Derive_Progenitor_Subprograms (T, T);
19570 end if;
19571 end Record_Type_Declaration;
19573 ----------------------------
19574 -- Record_Type_Definition --
19575 ----------------------------
19577 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
19578 Component : Entity_Id;
19579 Ctrl_Components : Boolean := False;
19580 Final_Storage_Only : Boolean;
19581 T : Entity_Id;
19583 begin
19584 if Ekind (Prev_T) = E_Incomplete_Type then
19585 T := Full_View (Prev_T);
19586 else
19587 T := Prev_T;
19588 end if;
19590 -- In SPARK, tagged types and type extensions may only be declared in
19591 -- the specification of library unit packages.
19593 if Present (Def) and then Is_Tagged_Type (T) then
19594 declare
19595 Typ : Node_Id;
19596 Ctxt : Node_Id;
19598 begin
19599 if Nkind (Parent (Def)) = N_Full_Type_Declaration then
19600 Typ := Parent (Def);
19601 else
19602 pragma Assert
19603 (Nkind (Parent (Def)) = N_Derived_Type_Definition);
19604 Typ := Parent (Parent (Def));
19605 end if;
19607 Ctxt := Parent (Typ);
19609 if Nkind (Ctxt) = N_Package_Body
19610 and then Nkind (Parent (Ctxt)) = N_Compilation_Unit
19611 then
19612 Check_SPARK_Restriction
19613 ("type should be defined in package specification", Typ);
19615 elsif Nkind (Ctxt) /= N_Package_Specification
19616 or else Nkind (Parent (Parent (Ctxt))) /= N_Compilation_Unit
19617 then
19618 Check_SPARK_Restriction
19619 ("type should be defined in library unit package", Typ);
19620 end if;
19621 end;
19622 end if;
19624 Final_Storage_Only := not Is_Controlled (T);
19626 -- Ada 2005: check whether an explicit Limited is present in a derived
19627 -- type declaration.
19629 if Nkind (Parent (Def)) = N_Derived_Type_Definition
19630 and then Limited_Present (Parent (Def))
19631 then
19632 Set_Is_Limited_Record (T);
19633 end if;
19635 -- If the component list of a record type is defined by the reserved
19636 -- word null and there is no discriminant part, then the record type has
19637 -- no components and all records of the type are null records (RM 3.7)
19638 -- This procedure is also called to process the extension part of a
19639 -- record extension, in which case the current scope may have inherited
19640 -- components.
19642 if No (Def)
19643 or else No (Component_List (Def))
19644 or else Null_Present (Component_List (Def))
19645 then
19646 if not Is_Tagged_Type (T) then
19647 Check_SPARK_Restriction ("non-tagged record cannot be null", Def);
19648 end if;
19650 else
19651 Analyze_Declarations (Component_Items (Component_List (Def)));
19653 if Present (Variant_Part (Component_List (Def))) then
19654 Check_SPARK_Restriction ("variant part is not allowed", Def);
19655 Analyze (Variant_Part (Component_List (Def)));
19656 end if;
19657 end if;
19659 -- After completing the semantic analysis of the record definition,
19660 -- record components, both new and inherited, are accessible. Set their
19661 -- kind accordingly. Exclude malformed itypes from illegal declarations,
19662 -- whose Ekind may be void.
19664 Component := First_Entity (Current_Scope);
19665 while Present (Component) loop
19666 if Ekind (Component) = E_Void
19667 and then not Is_Itype (Component)
19668 then
19669 Set_Ekind (Component, E_Component);
19670 Init_Component_Location (Component);
19671 end if;
19673 if Has_Task (Etype (Component)) then
19674 Set_Has_Task (T);
19675 end if;
19677 if Ekind (Component) /= E_Component then
19678 null;
19680 -- Do not set Has_Controlled_Component on a class-wide equivalent
19681 -- type. See Make_CW_Equivalent_Type.
19683 elsif not Is_Class_Wide_Equivalent_Type (T)
19684 and then (Has_Controlled_Component (Etype (Component))
19685 or else (Chars (Component) /= Name_uParent
19686 and then Is_Controlled (Etype (Component))))
19687 then
19688 Set_Has_Controlled_Component (T, True);
19689 Final_Storage_Only :=
19690 Final_Storage_Only
19691 and then Finalize_Storage_Only (Etype (Component));
19692 Ctrl_Components := True;
19693 end if;
19695 Next_Entity (Component);
19696 end loop;
19698 -- A Type is Finalize_Storage_Only only if all its controlled components
19699 -- are also.
19701 if Ctrl_Components then
19702 Set_Finalize_Storage_Only (T, Final_Storage_Only);
19703 end if;
19705 -- Place reference to end record on the proper entity, which may
19706 -- be a partial view.
19708 if Present (Def) then
19709 Process_End_Label (Def, 'e', Prev_T);
19710 end if;
19711 end Record_Type_Definition;
19713 ------------------------
19714 -- Replace_Components --
19715 ------------------------
19717 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id) is
19718 function Process (N : Node_Id) return Traverse_Result;
19720 -------------
19721 -- Process --
19722 -------------
19724 function Process (N : Node_Id) return Traverse_Result is
19725 Comp : Entity_Id;
19727 begin
19728 if Nkind (N) = N_Discriminant_Specification then
19729 Comp := First_Discriminant (Typ);
19730 while Present (Comp) loop
19731 if Chars (Comp) = Chars (Defining_Identifier (N)) then
19732 Set_Defining_Identifier (N, Comp);
19733 exit;
19734 end if;
19736 Next_Discriminant (Comp);
19737 end loop;
19739 elsif Nkind (N) = N_Component_Declaration then
19740 Comp := First_Component (Typ);
19741 while Present (Comp) loop
19742 if Chars (Comp) = Chars (Defining_Identifier (N)) then
19743 Set_Defining_Identifier (N, Comp);
19744 exit;
19745 end if;
19747 Next_Component (Comp);
19748 end loop;
19749 end if;
19751 return OK;
19752 end Process;
19754 procedure Replace is new Traverse_Proc (Process);
19756 -- Start of processing for Replace_Components
19758 begin
19759 Replace (Decl);
19760 end Replace_Components;
19762 -------------------------------
19763 -- Set_Completion_Referenced --
19764 -------------------------------
19766 procedure Set_Completion_Referenced (E : Entity_Id) is
19767 begin
19768 -- If in main unit, mark entity that is a completion as referenced,
19769 -- warnings go on the partial view when needed.
19771 if In_Extended_Main_Source_Unit (E) then
19772 Set_Referenced (E);
19773 end if;
19774 end Set_Completion_Referenced;
19776 ---------------------
19777 -- Set_Fixed_Range --
19778 ---------------------
19780 -- The range for fixed-point types is complicated by the fact that we
19781 -- do not know the exact end points at the time of the declaration. This
19782 -- is true for three reasons:
19784 -- A size clause may affect the fudging of the end-points.
19785 -- A small clause may affect the values of the end-points.
19786 -- We try to include the end-points if it does not affect the size.
19788 -- This means that the actual end-points must be established at the
19789 -- point when the type is frozen. Meanwhile, we first narrow the range
19790 -- as permitted (so that it will fit if necessary in a small specified
19791 -- size), and then build a range subtree with these narrowed bounds.
19792 -- Set_Fixed_Range constructs the range from real literal values, and
19793 -- sets the range as the Scalar_Range of the given fixed-point type entity.
19795 -- The parent of this range is set to point to the entity so that it is
19796 -- properly hooked into the tree (unlike normal Scalar_Range entries for
19797 -- other scalar types, which are just pointers to the range in the
19798 -- original tree, this would otherwise be an orphan).
19800 -- The tree is left unanalyzed. When the type is frozen, the processing
19801 -- in Freeze.Freeze_Fixed_Point_Type notices that the range is not
19802 -- analyzed, and uses this as an indication that it should complete
19803 -- work on the range (it will know the final small and size values).
19805 procedure Set_Fixed_Range
19806 (E : Entity_Id;
19807 Loc : Source_Ptr;
19808 Lo : Ureal;
19809 Hi : Ureal)
19811 S : constant Node_Id :=
19812 Make_Range (Loc,
19813 Low_Bound => Make_Real_Literal (Loc, Lo),
19814 High_Bound => Make_Real_Literal (Loc, Hi));
19815 begin
19816 Set_Scalar_Range (E, S);
19817 Set_Parent (S, E);
19819 -- Before the freeze point, the bounds of a fixed point are universal
19820 -- and carry the corresponding type.
19822 Set_Etype (Low_Bound (S), Universal_Real);
19823 Set_Etype (High_Bound (S), Universal_Real);
19824 end Set_Fixed_Range;
19826 ----------------------------------
19827 -- Set_Scalar_Range_For_Subtype --
19828 ----------------------------------
19830 procedure Set_Scalar_Range_For_Subtype
19831 (Def_Id : Entity_Id;
19832 R : Node_Id;
19833 Subt : Entity_Id)
19835 Kind : constant Entity_Kind := Ekind (Def_Id);
19837 begin
19838 -- Defend against previous error
19840 if Nkind (R) = N_Error then
19841 return;
19842 end if;
19844 Set_Scalar_Range (Def_Id, R);
19846 -- We need to link the range into the tree before resolving it so
19847 -- that types that are referenced, including importantly the subtype
19848 -- itself, are properly frozen (Freeze_Expression requires that the
19849 -- expression be properly linked into the tree). Of course if it is
19850 -- already linked in, then we do not disturb the current link.
19852 if No (Parent (R)) then
19853 Set_Parent (R, Def_Id);
19854 end if;
19856 -- Reset the kind of the subtype during analysis of the range, to
19857 -- catch possible premature use in the bounds themselves.
19859 Set_Ekind (Def_Id, E_Void);
19860 Process_Range_Expr_In_Decl (R, Subt);
19861 Set_Ekind (Def_Id, Kind);
19862 end Set_Scalar_Range_For_Subtype;
19864 --------------------------------------------------------
19865 -- Set_Stored_Constraint_From_Discriminant_Constraint --
19866 --------------------------------------------------------
19868 procedure Set_Stored_Constraint_From_Discriminant_Constraint
19869 (E : Entity_Id)
19871 begin
19872 -- Make sure set if encountered during Expand_To_Stored_Constraint
19874 Set_Stored_Constraint (E, No_Elist);
19876 -- Give it the right value
19878 if Is_Constrained (E) and then Has_Discriminants (E) then
19879 Set_Stored_Constraint (E,
19880 Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
19881 end if;
19882 end Set_Stored_Constraint_From_Discriminant_Constraint;
19884 -------------------------------------
19885 -- Signed_Integer_Type_Declaration --
19886 -------------------------------------
19888 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
19889 Implicit_Base : Entity_Id;
19890 Base_Typ : Entity_Id;
19891 Lo_Val : Uint;
19892 Hi_Val : Uint;
19893 Errs : Boolean := False;
19894 Lo : Node_Id;
19895 Hi : Node_Id;
19897 function Can_Derive_From (E : Entity_Id) return Boolean;
19898 -- Determine whether given bounds allow derivation from specified type
19900 procedure Check_Bound (Expr : Node_Id);
19901 -- Check bound to make sure it is integral and static. If not, post
19902 -- appropriate error message and set Errs flag
19904 ---------------------
19905 -- Can_Derive_From --
19906 ---------------------
19908 -- Note we check both bounds against both end values, to deal with
19909 -- strange types like ones with a range of 0 .. -12341234.
19911 function Can_Derive_From (E : Entity_Id) return Boolean is
19912 Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
19913 Hi : constant Uint := Expr_Value (Type_High_Bound (E));
19914 begin
19915 return Lo <= Lo_Val and then Lo_Val <= Hi
19916 and then
19917 Lo <= Hi_Val and then Hi_Val <= Hi;
19918 end Can_Derive_From;
19920 -----------------
19921 -- Check_Bound --
19922 -----------------
19924 procedure Check_Bound (Expr : Node_Id) is
19925 begin
19926 -- If a range constraint is used as an integer type definition, each
19927 -- bound of the range must be defined by a static expression of some
19928 -- integer type, but the two bounds need not have the same integer
19929 -- type (Negative bounds are allowed.) (RM 3.5.4)
19931 if not Is_Integer_Type (Etype (Expr)) then
19932 Error_Msg_N
19933 ("integer type definition bounds must be of integer type", Expr);
19934 Errs := True;
19936 elsif not Is_OK_Static_Expression (Expr) then
19937 Flag_Non_Static_Expr
19938 ("non-static expression used for integer type bound!", Expr);
19939 Errs := True;
19941 -- The bounds are folded into literals, and we set their type to be
19942 -- universal, to avoid typing difficulties: we cannot set the type
19943 -- of the literal to the new type, because this would be a forward
19944 -- reference for the back end, and if the original type is user-
19945 -- defined this can lead to spurious semantic errors (e.g. 2928-003).
19947 else
19948 if Is_Entity_Name (Expr) then
19949 Fold_Uint (Expr, Expr_Value (Expr), True);
19950 end if;
19952 Set_Etype (Expr, Universal_Integer);
19953 end if;
19954 end Check_Bound;
19956 -- Start of processing for Signed_Integer_Type_Declaration
19958 begin
19959 -- Create an anonymous base type
19961 Implicit_Base :=
19962 Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
19964 -- Analyze and check the bounds, they can be of any integer type
19966 Lo := Low_Bound (Def);
19967 Hi := High_Bound (Def);
19969 -- Arbitrarily use Integer as the type if either bound had an error
19971 if Hi = Error or else Lo = Error then
19972 Base_Typ := Any_Integer;
19973 Set_Error_Posted (T, True);
19975 -- Here both bounds are OK expressions
19977 else
19978 Analyze_And_Resolve (Lo, Any_Integer);
19979 Analyze_And_Resolve (Hi, Any_Integer);
19981 Check_Bound (Lo);
19982 Check_Bound (Hi);
19984 if Errs then
19985 Hi := Type_High_Bound (Standard_Long_Long_Integer);
19986 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
19987 end if;
19989 -- Find type to derive from
19991 Lo_Val := Expr_Value (Lo);
19992 Hi_Val := Expr_Value (Hi);
19994 if Can_Derive_From (Standard_Short_Short_Integer) then
19995 Base_Typ := Base_Type (Standard_Short_Short_Integer);
19997 elsif Can_Derive_From (Standard_Short_Integer) then
19998 Base_Typ := Base_Type (Standard_Short_Integer);
20000 elsif Can_Derive_From (Standard_Integer) then
20001 Base_Typ := Base_Type (Standard_Integer);
20003 elsif Can_Derive_From (Standard_Long_Integer) then
20004 Base_Typ := Base_Type (Standard_Long_Integer);
20006 elsif Can_Derive_From (Standard_Long_Long_Integer) then
20007 Base_Typ := Base_Type (Standard_Long_Long_Integer);
20009 else
20010 Base_Typ := Base_Type (Standard_Long_Long_Integer);
20011 Error_Msg_N ("integer type definition bounds out of range", Def);
20012 Hi := Type_High_Bound (Standard_Long_Long_Integer);
20013 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
20014 end if;
20015 end if;
20017 -- Complete both implicit base and declared first subtype entities
20019 Set_Etype (Implicit_Base, Base_Typ);
20020 Set_Size_Info (Implicit_Base, (Base_Typ));
20021 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
20022 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
20024 Set_Ekind (T, E_Signed_Integer_Subtype);
20025 Set_Etype (T, Implicit_Base);
20027 -- In formal verification mode, restrict the base type's range to the
20028 -- minimum allowed by RM 3.5.4, namely the smallest symmetric range
20029 -- around zero with a possible extra negative value that contains the
20030 -- subtype range. Keep Size, RM_Size and First_Rep_Item info, which
20031 -- should not be relied upon in formal verification.
20033 if Strict_Alfa_Mode then
20034 declare
20035 Sym_Hi_Val : Uint;
20036 Sym_Lo_Val : Uint;
20037 Dloc : constant Source_Ptr := Sloc (Def);
20038 Lbound : Node_Id;
20039 Ubound : Node_Id;
20040 Bounds : Node_Id;
20042 begin
20043 -- If the subtype range is empty, the smallest base type range
20044 -- is the symmetric range around zero containing Lo_Val and
20045 -- Hi_Val.
20047 if UI_Gt (Lo_Val, Hi_Val) then
20048 Sym_Hi_Val := UI_Max (UI_Abs (Lo_Val), UI_Abs (Hi_Val));
20049 Sym_Lo_Val := UI_Negate (Sym_Hi_Val);
20051 -- Otherwise, if the subtype range is not empty and Hi_Val has
20052 -- the largest absolute value, Hi_Val is non negative and the
20053 -- smallest base type range is the symmetric range around zero
20054 -- containing Hi_Val.
20056 elsif UI_Le (UI_Abs (Lo_Val), UI_Abs (Hi_Val)) then
20057 Sym_Hi_Val := Hi_Val;
20058 Sym_Lo_Val := UI_Negate (Hi_Val);
20060 -- Otherwise, the subtype range is not empty, Lo_Val has the
20061 -- strictly largest absolute value, Lo_Val is negative and the
20062 -- smallest base type range is the symmetric range around zero
20063 -- with an extra negative value Lo_Val.
20065 else
20066 Sym_Lo_Val := Lo_Val;
20067 Sym_Hi_Val := UI_Sub (UI_Negate (Lo_Val), Uint_1);
20068 end if;
20070 Lbound := Make_Integer_Literal (Dloc, Sym_Lo_Val);
20071 Ubound := Make_Integer_Literal (Dloc, Sym_Hi_Val);
20072 Set_Is_Static_Expression (Lbound);
20073 Set_Is_Static_Expression (Ubound);
20074 Analyze_And_Resolve (Lbound, Any_Integer);
20075 Analyze_And_Resolve (Ubound, Any_Integer);
20077 Bounds := Make_Range (Dloc, Lbound, Ubound);
20078 Set_Etype (Bounds, Base_Typ);
20080 Set_Scalar_Range (Implicit_Base, Bounds);
20081 end;
20083 else
20084 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
20085 end if;
20087 Set_Size_Info (T, (Implicit_Base));
20088 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
20089 Set_Scalar_Range (T, Def);
20090 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
20091 Set_Is_Constrained (T);
20092 end Signed_Integer_Type_Declaration;
20094 end Sem_Ch3;