Make more use of in_hard_reg_set_p
[official-gcc.git] / gcc / ada / sem_ch3.adb
blob46d83494883c46b535decf7d9e867619f48dad6e
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-2017, 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 Contracts; use Contracts;
30 with Debug; use Debug;
31 with Elists; use Elists;
32 with Einfo; use Einfo;
33 with Errout; use Errout;
34 with Eval_Fat; use Eval_Fat;
35 with Exp_Ch3; use Exp_Ch3;
36 with Exp_Ch9; use Exp_Ch9;
37 with Exp_Disp; use Exp_Disp;
38 with Exp_Dist; use Exp_Dist;
39 with Exp_Tss; use Exp_Tss;
40 with Exp_Util; use Exp_Util;
41 with Freeze; use Freeze;
42 with Ghost; use Ghost;
43 with Itypes; use Itypes;
44 with Layout; use Layout;
45 with Lib; use Lib;
46 with Lib.Xref; use Lib.Xref;
47 with Namet; use Namet;
48 with Nmake; use Nmake;
49 with Opt; use Opt;
50 with Restrict; use Restrict;
51 with Rident; use Rident;
52 with Rtsfind; use Rtsfind;
53 with Sem; use Sem;
54 with Sem_Aux; use Sem_Aux;
55 with Sem_Case; use Sem_Case;
56 with Sem_Cat; use Sem_Cat;
57 with Sem_Ch6; use Sem_Ch6;
58 with Sem_Ch7; use Sem_Ch7;
59 with Sem_Ch8; use Sem_Ch8;
60 with Sem_Ch13; use Sem_Ch13;
61 with Sem_Dim; use Sem_Dim;
62 with Sem_Disp; use Sem_Disp;
63 with Sem_Dist; use Sem_Dist;
64 with Sem_Elim; use Sem_Elim;
65 with Sem_Eval; use Sem_Eval;
66 with Sem_Mech; use Sem_Mech;
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 used for tagged and untagged record types
173 -- by Build_Derived_Type and Analyze_Private_Extension_Declaration.
174 -- All parameters are as in Build_Derived_Type except that N, in
175 -- 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 be
178 -- derived from the parent type. The only case where Derive_Subps is False
179 -- is for an implicit derived full type for a type derived from a private
180 -- 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 constraints,
353 -- Constraints, for Typ and a component Comp of Typ, create and return the
354 -- type corresponding to Etype (Comp) where all discriminant references
355 -- are replaced with the corresponding constraint. If Etype (Comp) contains
356 -- no discriminant references then it is returned as-is. Constrained_Typ
357 -- is the final constrained subtype to which the constrained component
358 -- belongs. Related_Node is the node where we attach all created itypes.
360 procedure Constrain_Access
361 (Def_Id : in out Entity_Id;
362 S : Node_Id;
363 Related_Nod : Node_Id);
364 -- Apply a list of constraints to an access type. If Def_Id is empty, it is
365 -- an anonymous type created for a subtype indication. In that case it is
366 -- created in the procedure and attached to Related_Nod.
368 procedure Constrain_Array
369 (Def_Id : in out Entity_Id;
370 SI : Node_Id;
371 Related_Nod : Node_Id;
372 Related_Id : Entity_Id;
373 Suffix : Character);
374 -- Apply a list of index constraints to an unconstrained array type. The
375 -- first parameter is the entity for the resulting subtype. A value of
376 -- Empty for Def_Id indicates that an implicit type must be created, but
377 -- creation is delayed (and must be done by this procedure) because other
378 -- subsidiary implicit types must be created first (which is why Def_Id
379 -- is an in/out parameter). The second parameter is a subtype indication
380 -- node for the constrained array to be created (e.g. something of the
381 -- form string (1 .. 10)). Related_Nod gives the place where this type
382 -- has to be inserted in the tree. The Related_Id and Suffix parameters
383 -- are used to build the associated Implicit type name.
385 procedure Constrain_Concurrent
386 (Def_Id : in out Entity_Id;
387 SI : Node_Id;
388 Related_Nod : Node_Id;
389 Related_Id : Entity_Id;
390 Suffix : Character);
391 -- Apply list of discriminant constraints to an unconstrained concurrent
392 -- type.
394 -- SI is the N_Subtype_Indication node containing the constraint and
395 -- the unconstrained type to constrain.
397 -- Def_Id is the entity for the resulting constrained subtype. A value
398 -- of Empty for Def_Id indicates that an implicit type must be created,
399 -- but creation is delayed (and must be done by this procedure) because
400 -- other subsidiary implicit types must be created first (which is why
401 -- Def_Id is an in/out parameter).
403 -- Related_Nod gives the place where this type has to be inserted
404 -- in the tree.
406 -- The last two arguments are used to create its external name if needed.
408 function Constrain_Corresponding_Record
409 (Prot_Subt : Entity_Id;
410 Corr_Rec : Entity_Id;
411 Related_Nod : Node_Id) return Entity_Id;
412 -- When constraining a protected type or task type with discriminants,
413 -- constrain the corresponding record with the same discriminant values.
415 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id);
416 -- Constrain a decimal fixed point type with a digits constraint and/or a
417 -- range constraint, and build E_Decimal_Fixed_Point_Subtype entity.
419 procedure Constrain_Discriminated_Type
420 (Def_Id : Entity_Id;
421 S : Node_Id;
422 Related_Nod : Node_Id;
423 For_Access : Boolean := False);
424 -- Process discriminant constraints of composite type. Verify that values
425 -- have been provided for all discriminants, that the original type is
426 -- unconstrained, and that the types of the supplied expressions match
427 -- the discriminant types. The first three parameters are like in routine
428 -- Constrain_Concurrent. See Build_Discriminated_Subtype for an explanation
429 -- of For_Access.
431 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id);
432 -- Constrain an enumeration type with a range constraint. This is identical
433 -- to Constrain_Integer, but for the Ekind of the resulting subtype.
435 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id);
436 -- Constrain a floating point type with either a digits constraint
437 -- and/or a range constraint, building a E_Floating_Point_Subtype.
439 procedure Constrain_Index
440 (Index : Node_Id;
441 S : Node_Id;
442 Related_Nod : Node_Id;
443 Related_Id : Entity_Id;
444 Suffix : Character;
445 Suffix_Index : Nat);
446 -- Process an index constraint S in a constrained array declaration. The
447 -- constraint can be a subtype name, or a range with or without an explicit
448 -- subtype mark. The index is the corresponding index of the unconstrained
449 -- array. The Related_Id and Suffix parameters are used to build the
450 -- associated Implicit type name.
452 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id);
453 -- Build subtype of a signed or modular integer type
455 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id);
456 -- Constrain an ordinary fixed point type with a range constraint, and
457 -- build an E_Ordinary_Fixed_Point_Subtype entity.
459 procedure Copy_And_Swap (Priv, Full : Entity_Id);
460 -- Copy the Priv entity into the entity of its full declaration then swap
461 -- the two entities in such a manner that the former private type is now
462 -- seen as a full type.
464 procedure Decimal_Fixed_Point_Type_Declaration
465 (T : Entity_Id;
466 Def : Node_Id);
467 -- Create a new decimal fixed point type, and apply the constraint to
468 -- obtain a subtype of this new type.
470 procedure Complete_Private_Subtype
471 (Priv : Entity_Id;
472 Full : Entity_Id;
473 Full_Base : Entity_Id;
474 Related_Nod : Node_Id);
475 -- Complete the implicit full view of a private subtype by setting the
476 -- appropriate semantic fields. If the full view of the parent is a record
477 -- type, build constrained components of subtype.
479 procedure Derive_Progenitor_Subprograms
480 (Parent_Type : Entity_Id;
481 Tagged_Type : Entity_Id);
482 -- Ada 2005 (AI-251): To complete type derivation, collect the primitive
483 -- operations of progenitors of Tagged_Type, and replace the subsidiary
484 -- subtypes with Tagged_Type, to build the specs of the inherited interface
485 -- primitives. The derived primitives are aliased to those of the
486 -- interface. This routine takes care also of transferring to the full view
487 -- subprograms associated with the partial view of Tagged_Type that cover
488 -- interface primitives.
490 procedure Derived_Standard_Character
491 (N : Node_Id;
492 Parent_Type : Entity_Id;
493 Derived_Type : Entity_Id);
494 -- Subsidiary procedure to Build_Derived_Enumeration_Type which handles
495 -- derivations from types Standard.Character and Standard.Wide_Character.
497 procedure Derived_Type_Declaration
498 (T : Entity_Id;
499 N : Node_Id;
500 Is_Completion : Boolean);
501 -- Process a derived type declaration. Build_Derived_Type is invoked
502 -- to process the actual derived type definition. Parameters N and
503 -- Is_Completion have the same meaning as in Build_Derived_Type.
504 -- T is the N_Defining_Identifier for the entity defined in the
505 -- N_Full_Type_Declaration node N, that is T is the derived type.
507 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id);
508 -- Insert each literal in symbol table, as an overloadable identifier. Each
509 -- enumeration type is mapped into a sequence of integers, and each literal
510 -- is defined as a constant with integer value. If any of the literals are
511 -- character literals, the type is a character type, which means that
512 -- strings are legal aggregates for arrays of components of the type.
514 function Expand_To_Stored_Constraint
515 (Typ : Entity_Id;
516 Constraint : Elist_Id) return Elist_Id;
517 -- Given a constraint (i.e. a list of expressions) on the discriminants of
518 -- Typ, expand it into a constraint on the stored discriminants and return
519 -- the new list of expressions constraining the stored discriminants.
521 function Find_Type_Of_Object
522 (Obj_Def : Node_Id;
523 Related_Nod : Node_Id) return Entity_Id;
524 -- Get type entity for object referenced by Obj_Def, attaching the implicit
525 -- types generated to Related_Nod.
527 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id);
528 -- Create a new float and apply the constraint to obtain subtype of it
530 function Has_Range_Constraint (N : Node_Id) return Boolean;
531 -- Given an N_Subtype_Indication node N, return True if a range constraint
532 -- is present, either directly, or as part of a digits or delta constraint.
533 -- In addition, a digits constraint in the decimal case returns True, since
534 -- it establishes a default range if no explicit range is present.
536 function Inherit_Components
537 (N : Node_Id;
538 Parent_Base : Entity_Id;
539 Derived_Base : Entity_Id;
540 Is_Tagged : Boolean;
541 Inherit_Discr : Boolean;
542 Discs : Elist_Id) return Elist_Id;
543 -- Called from Build_Derived_Record_Type to inherit the components of
544 -- Parent_Base (a base type) into the Derived_Base (the derived base type).
545 -- For more information on derived types and component inheritance please
546 -- consult the comment above the body of Build_Derived_Record_Type.
548 -- N is the original derived type declaration
550 -- Is_Tagged is set if we are dealing with tagged types
552 -- If Inherit_Discr is set, Derived_Base inherits its discriminants from
553 -- Parent_Base, otherwise no discriminants are inherited.
555 -- Discs gives the list of constraints that apply to Parent_Base in the
556 -- derived type declaration. If Discs is set to No_Elist, then we have
557 -- the following situation:
559 -- type Parent (D1..Dn : ..) is [tagged] record ...;
560 -- type Derived is new Parent [with ...];
562 -- which gets treated as
564 -- type Derived (D1..Dn : ..) is new Parent (D1,..,Dn) [with ...];
566 -- For untagged types the returned value is an association list. The list
567 -- starts from the association (Parent_Base => Derived_Base), and then it
568 -- contains a sequence of the associations of the form
570 -- (Old_Component => New_Component),
572 -- where Old_Component is the Entity_Id of a component in Parent_Base and
573 -- New_Component is the Entity_Id of the corresponding component in
574 -- Derived_Base. For untagged records, this association list is needed when
575 -- copying the record declaration for the derived base. In the tagged case
576 -- the value returned is irrelevant.
578 procedure Inherit_Predicate_Flags (Subt, Par : Entity_Id);
579 -- Propagate static and dynamic predicate flags from a parent to the
580 -- subtype in a subtype declaration with and without constraints.
582 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean;
583 -- Subsidiary to Check_Abstract_Overriding and Derive_Subprogram.
584 -- Determine whether subprogram Subp is a procedure subject to pragma
585 -- Extensions_Visible with value False and has at least one controlling
586 -- parameter of mode OUT.
588 function Is_Valid_Constraint_Kind
589 (T_Kind : Type_Kind;
590 Constraint_Kind : Node_Kind) return Boolean;
591 -- Returns True if it is legal to apply the given kind of constraint to the
592 -- given kind of type (index constraint to an array type, for example).
594 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id);
595 -- Create new modular type. Verify that modulus is in bounds
597 procedure New_Concatenation_Op (Typ : Entity_Id);
598 -- Create an abbreviated declaration for an operator in order to
599 -- materialize concatenation on array types.
601 procedure Ordinary_Fixed_Point_Type_Declaration
602 (T : Entity_Id;
603 Def : Node_Id);
604 -- Create a new ordinary fixed point type, and apply the constraint to
605 -- obtain subtype of it.
607 procedure Prepare_Private_Subtype_Completion
608 (Id : Entity_Id;
609 Related_Nod : Node_Id);
610 -- Id is a subtype of some private type. Creates the full declaration
611 -- associated with Id whenever possible, i.e. when the full declaration
612 -- of the base type is already known. Records each subtype into
613 -- Private_Dependents of the base type.
615 procedure Process_Incomplete_Dependents
616 (N : Node_Id;
617 Full_T : Entity_Id;
618 Inc_T : Entity_Id);
619 -- Process all entities that depend on an incomplete type. There include
620 -- subtypes, subprogram types that mention the incomplete type in their
621 -- profiles, and subprogram with access parameters that designate the
622 -- incomplete type.
624 -- Inc_T is the defining identifier of an incomplete type declaration, its
625 -- Ekind is E_Incomplete_Type.
627 -- N is the corresponding N_Full_Type_Declaration for Inc_T.
629 -- Full_T is N's defining identifier.
631 -- Subtypes of incomplete types with discriminants are completed when the
632 -- parent type is. This is simpler than private subtypes, because they can
633 -- only appear in the same scope, and there is no need to exchange views.
634 -- Similarly, access_to_subprogram types may have a parameter or a return
635 -- type that is an incomplete type, and that must be replaced with the
636 -- full type.
638 -- If the full type is tagged, subprogram with access parameters that
639 -- designated the incomplete may be primitive operations of the full type,
640 -- and have to be processed accordingly.
642 procedure Process_Real_Range_Specification (Def : Node_Id);
643 -- Given the type definition for a real type, this procedure processes and
644 -- checks the real range specification of this type definition if one is
645 -- present. If errors are found, error messages are posted, and the
646 -- Real_Range_Specification of Def is reset to Empty.
648 procedure Record_Type_Declaration
649 (T : Entity_Id;
650 N : Node_Id;
651 Prev : Entity_Id);
652 -- Process a record type declaration (for both untagged and tagged
653 -- records). Parameters T and N are exactly like in procedure
654 -- Derived_Type_Declaration, except that no flag Is_Completion is needed
655 -- for this routine. If this is the completion of an incomplete type
656 -- declaration, Prev is the entity of the incomplete declaration, used for
657 -- cross-referencing. Otherwise Prev = T.
659 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id);
660 -- This routine is used to process the actual record type definition (both
661 -- for untagged and tagged records). Def is a record type definition node.
662 -- This procedure analyzes the components in this record type definition.
663 -- Prev_T is the entity for the enclosing record type. It is provided so
664 -- that its Has_Task flag can be set if any of the component have Has_Task
665 -- set. If the declaration is the completion of an incomplete type
666 -- declaration, Prev_T is the original incomplete type, whose full view is
667 -- the record type.
669 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id);
670 -- Subsidiary to Build_Derived_Record_Type. For untagged records, we
671 -- build a copy of the declaration tree of the parent, and we create
672 -- independently the list of components for the derived type. Semantic
673 -- information uses the component entities, but record representation
674 -- clauses are validated on the declaration tree. This procedure replaces
675 -- discriminants and components in the declaration with those that have
676 -- been created by Inherit_Components.
678 procedure Set_Fixed_Range
679 (E : Entity_Id;
680 Loc : Source_Ptr;
681 Lo : Ureal;
682 Hi : Ureal);
683 -- Build a range node with the given bounds and set it as the Scalar_Range
684 -- of the given fixed-point type entity. Loc is the source location used
685 -- for the constructed range. See body for further details.
687 procedure Set_Scalar_Range_For_Subtype
688 (Def_Id : Entity_Id;
689 R : Node_Id;
690 Subt : Entity_Id);
691 -- This routine is used to set the scalar range field for a subtype given
692 -- Def_Id, the entity for the subtype, and R, the range expression for the
693 -- scalar range. Subt provides the parent subtype to be used to analyze,
694 -- resolve, and check the given range.
696 procedure Set_Default_SSO (T : Entity_Id);
697 -- T is the entity for an array or record being declared. This procedure
698 -- sets the flags SSO_Set_Low_By_Default/SSO_Set_High_By_Default according
699 -- to the setting of Opt.Default_SSO.
701 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id);
702 -- Create a new signed integer entity, and apply the constraint to obtain
703 -- the required first named subtype of this type.
705 procedure Set_Stored_Constraint_From_Discriminant_Constraint
706 (E : Entity_Id);
707 -- E is some record type. This routine computes E's Stored_Constraint
708 -- from its Discriminant_Constraint.
710 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id);
711 -- Check that an entity in a list of progenitors is an interface,
712 -- emit error otherwise.
714 -----------------------
715 -- Access_Definition --
716 -----------------------
718 function Access_Definition
719 (Related_Nod : Node_Id;
720 N : Node_Id) return Entity_Id
722 Anon_Type : Entity_Id;
723 Anon_Scope : Entity_Id;
724 Desig_Type : Entity_Id;
725 Enclosing_Prot_Type : Entity_Id := Empty;
727 begin
728 Check_SPARK_05_Restriction ("access type is not allowed", N);
730 if Is_Entry (Current_Scope)
731 and then Is_Task_Type (Etype (Scope (Current_Scope)))
732 then
733 Error_Msg_N ("task entries cannot have access parameters", N);
734 return Empty;
735 end if;
737 -- Ada 2005: For an object declaration the corresponding anonymous
738 -- type is declared in the current scope.
740 -- If the access definition is the return type of another access to
741 -- function, scope is the current one, because it is the one of the
742 -- current type declaration, except for the pathological case below.
744 if Nkind_In (Related_Nod, N_Object_Declaration,
745 N_Access_Function_Definition)
746 then
747 Anon_Scope := Current_Scope;
749 -- A pathological case: function returning access functions that
750 -- return access functions, etc. Each anonymous access type created
751 -- is in the enclosing scope of the outermost function.
753 declare
754 Par : Node_Id;
756 begin
757 Par := Related_Nod;
758 while Nkind_In (Par, N_Access_Function_Definition,
759 N_Access_Definition)
760 loop
761 Par := Parent (Par);
762 end loop;
764 if Nkind (Par) = N_Function_Specification then
765 Anon_Scope := Scope (Defining_Entity (Par));
766 end if;
767 end;
769 -- For the anonymous function result case, retrieve the scope of the
770 -- function specification's associated entity rather than using the
771 -- current scope. The current scope will be the function itself if the
772 -- formal part is currently being analyzed, but will be the parent scope
773 -- in the case of a parameterless function, and we always want to use
774 -- the function's parent scope. Finally, if the function is a child
775 -- unit, we must traverse the tree to retrieve the proper entity.
777 elsif Nkind (Related_Nod) = N_Function_Specification
778 and then Nkind (Parent (N)) /= N_Parameter_Specification
779 then
780 -- If the current scope is a protected type, the anonymous access
781 -- is associated with one of the protected operations, and must
782 -- be available in the scope that encloses the protected declaration.
783 -- Otherwise the type is in the scope enclosing the subprogram.
785 -- If the function has formals, The return type of a subprogram
786 -- declaration is analyzed in the scope of the subprogram (see
787 -- Process_Formals) and thus the protected type, if present, is
788 -- the scope of the current function scope.
790 if Ekind (Current_Scope) = E_Protected_Type then
791 Enclosing_Prot_Type := Current_Scope;
793 elsif Ekind (Current_Scope) = E_Function
794 and then Ekind (Scope (Current_Scope)) = E_Protected_Type
795 then
796 Enclosing_Prot_Type := Scope (Current_Scope);
797 end if;
799 if Present (Enclosing_Prot_Type) then
800 Anon_Scope := Scope (Enclosing_Prot_Type);
802 else
803 Anon_Scope := Scope (Defining_Entity (Related_Nod));
804 end if;
806 -- For an access type definition, if the current scope is a child
807 -- unit it is the scope of the type.
809 elsif Is_Compilation_Unit (Current_Scope) then
810 Anon_Scope := Current_Scope;
812 -- For access formals, access components, and access discriminants, the
813 -- scope is that of the enclosing declaration,
815 else
816 Anon_Scope := Scope (Current_Scope);
817 end if;
819 Anon_Type :=
820 Create_Itype
821 (E_Anonymous_Access_Type, Related_Nod, Scope_Id => Anon_Scope);
823 if All_Present (N)
824 and then Ada_Version >= Ada_2005
825 then
826 Error_Msg_N ("ALL is not permitted for anonymous access types", N);
827 end if;
829 -- Ada 2005 (AI-254): In case of anonymous access to subprograms call
830 -- the corresponding semantic routine
832 if Present (Access_To_Subprogram_Definition (N)) then
834 -- Compiler runtime units are compiled in Ada 2005 mode when building
835 -- the runtime library but must also be compilable in Ada 95 mode
836 -- (when bootstrapping the compiler).
838 Check_Compiler_Unit ("anonymous access to subprogram", N);
840 Access_Subprogram_Declaration
841 (T_Name => Anon_Type,
842 T_Def => Access_To_Subprogram_Definition (N));
844 if Ekind (Anon_Type) = E_Access_Protected_Subprogram_Type then
845 Set_Ekind
846 (Anon_Type, E_Anonymous_Access_Protected_Subprogram_Type);
847 else
848 Set_Ekind (Anon_Type, E_Anonymous_Access_Subprogram_Type);
849 end if;
851 Set_Can_Use_Internal_Rep
852 (Anon_Type, not Always_Compatible_Rep_On_Target);
854 -- If the anonymous access is associated with a protected operation,
855 -- create a reference to it after the enclosing protected definition
856 -- because the itype will be used in the subsequent bodies.
858 -- If the anonymous access itself is protected, a full type
859 -- declaratiton will be created for it, so that the equivalent
860 -- record type can be constructed. For further details, see
861 -- Replace_Anonymous_Access_To_Protected-Subprogram.
863 if Ekind (Current_Scope) = E_Protected_Type
864 and then not Protected_Present (Access_To_Subprogram_Definition (N))
865 then
866 Build_Itype_Reference (Anon_Type, Parent (Current_Scope));
867 end if;
869 return Anon_Type;
870 end if;
872 Find_Type (Subtype_Mark (N));
873 Desig_Type := Entity (Subtype_Mark (N));
875 Set_Directly_Designated_Type (Anon_Type, Desig_Type);
876 Set_Etype (Anon_Type, Anon_Type);
878 -- Make sure the anonymous access type has size and alignment fields
879 -- set, as required by gigi. This is necessary in the case of the
880 -- Task_Body_Procedure.
882 if not Has_Private_Component (Desig_Type) then
883 Layout_Type (Anon_Type);
884 end if;
886 -- Ada 2005 (AI-231): Ada 2005 semantics for anonymous access differs
887 -- from Ada 95 semantics. In Ada 2005, anonymous access must specify if
888 -- the null value is allowed. In Ada 95 the null value is never allowed.
890 if Ada_Version >= Ada_2005 then
891 Set_Can_Never_Be_Null (Anon_Type, Null_Exclusion_Present (N));
892 else
893 Set_Can_Never_Be_Null (Anon_Type, True);
894 end if;
896 -- The anonymous access type is as public as the discriminated type or
897 -- subprogram that defines it. It is imported (for back-end purposes)
898 -- if the designated type is.
900 Set_Is_Public (Anon_Type, Is_Public (Scope (Anon_Type)));
902 -- Ada 2005 (AI-231): Propagate the access-constant attribute
904 Set_Is_Access_Constant (Anon_Type, Constant_Present (N));
906 -- The context is either a subprogram declaration, object declaration,
907 -- or an access discriminant, in a private or a full type declaration.
908 -- In the case of a subprogram, if the designated type is incomplete,
909 -- the operation will be a primitive operation of the full type, to be
910 -- updated subsequently. If the type is imported through a limited_with
911 -- clause, the subprogram is not a primitive operation of the type
912 -- (which is declared elsewhere in some other scope).
914 if Ekind (Desig_Type) = E_Incomplete_Type
915 and then not From_Limited_With (Desig_Type)
916 and then Is_Overloadable (Current_Scope)
917 then
918 Append_Elmt (Current_Scope, Private_Dependents (Desig_Type));
919 Set_Has_Delayed_Freeze (Current_Scope);
920 end if;
922 -- Ada 2005: If the designated type is an interface that may contain
923 -- tasks, create a Master entity for the declaration. This must be done
924 -- before expansion of the full declaration, because the declaration may
925 -- include an expression that is an allocator, whose expansion needs the
926 -- proper Master for the created tasks.
928 if Nkind (Related_Nod) = N_Object_Declaration and then Expander_Active
929 then
930 if Is_Interface (Desig_Type) and then Is_Limited_Record (Desig_Type)
931 then
932 Build_Class_Wide_Master (Anon_Type);
934 -- Similarly, if the type is an anonymous access that designates
935 -- tasks, create a master entity for it in the current context.
937 elsif Has_Task (Desig_Type) and then Comes_From_Source (Related_Nod)
938 then
939 Build_Master_Entity (Defining_Identifier (Related_Nod));
940 Build_Master_Renaming (Anon_Type);
941 end if;
942 end if;
944 -- For a private component of a protected type, it is imperative that
945 -- the back-end elaborate the type immediately after the protected
946 -- declaration, because this type will be used in the declarations
947 -- created for the component within each protected body, so we must
948 -- create an itype reference for it now.
950 if Nkind (Parent (Related_Nod)) = N_Protected_Definition then
951 Build_Itype_Reference (Anon_Type, Parent (Parent (Related_Nod)));
953 -- Similarly, if the access definition is the return result of a
954 -- function, create an itype reference for it because it will be used
955 -- within the function body. For a regular function that is not a
956 -- compilation unit, insert reference after the declaration. For a
957 -- protected operation, insert it after the enclosing protected type
958 -- declaration. In either case, do not create a reference for a type
959 -- obtained through a limited_with clause, because this would introduce
960 -- semantic dependencies.
962 -- Similarly, do not create a reference if the designated type is a
963 -- generic formal, because no use of it will reach the backend.
965 elsif Nkind (Related_Nod) = N_Function_Specification
966 and then not From_Limited_With (Desig_Type)
967 and then not Is_Generic_Type (Desig_Type)
968 then
969 if Present (Enclosing_Prot_Type) then
970 Build_Itype_Reference (Anon_Type, Parent (Enclosing_Prot_Type));
972 elsif Is_List_Member (Parent (Related_Nod))
973 and then Nkind (Parent (N)) /= N_Parameter_Specification
974 then
975 Build_Itype_Reference (Anon_Type, Parent (Related_Nod));
976 end if;
978 -- Finally, create an itype reference for an object declaration of an
979 -- anonymous access type. This is strictly necessary only for deferred
980 -- constants, but in any case will avoid out-of-scope problems in the
981 -- back-end.
983 elsif Nkind (Related_Nod) = N_Object_Declaration then
984 Build_Itype_Reference (Anon_Type, Related_Nod);
985 end if;
987 return Anon_Type;
988 end Access_Definition;
990 -----------------------------------
991 -- Access_Subprogram_Declaration --
992 -----------------------------------
994 procedure Access_Subprogram_Declaration
995 (T_Name : Entity_Id;
996 T_Def : Node_Id)
998 procedure Check_For_Premature_Usage (Def : Node_Id);
999 -- Check that type T_Name is not used, directly or recursively, as a
1000 -- parameter or a return type in Def. Def is either a subtype, an
1001 -- access_definition, or an access_to_subprogram_definition.
1003 -------------------------------
1004 -- Check_For_Premature_Usage --
1005 -------------------------------
1007 procedure Check_For_Premature_Usage (Def : Node_Id) is
1008 Param : Node_Id;
1010 begin
1011 -- Check for a subtype mark
1013 if Nkind (Def) in N_Has_Etype then
1014 if Etype (Def) = T_Name then
1015 Error_Msg_N
1016 ("type& cannot be used before end of its declaration", Def);
1017 end if;
1019 -- If this is not a subtype, then this is an access_definition
1021 elsif Nkind (Def) = N_Access_Definition then
1022 if Present (Access_To_Subprogram_Definition (Def)) then
1023 Check_For_Premature_Usage
1024 (Access_To_Subprogram_Definition (Def));
1025 else
1026 Check_For_Premature_Usage (Subtype_Mark (Def));
1027 end if;
1029 -- The only cases left are N_Access_Function_Definition and
1030 -- N_Access_Procedure_Definition.
1032 else
1033 if Present (Parameter_Specifications (Def)) then
1034 Param := First (Parameter_Specifications (Def));
1035 while Present (Param) loop
1036 Check_For_Premature_Usage (Parameter_Type (Param));
1037 Param := Next (Param);
1038 end loop;
1039 end if;
1041 if Nkind (Def) = N_Access_Function_Definition then
1042 Check_For_Premature_Usage (Result_Definition (Def));
1043 end if;
1044 end if;
1045 end Check_For_Premature_Usage;
1047 -- Local variables
1049 Formals : constant List_Id := Parameter_Specifications (T_Def);
1050 Formal : Entity_Id;
1051 D_Ityp : Node_Id;
1052 Desig_Type : constant Entity_Id :=
1053 Create_Itype (E_Subprogram_Type, Parent (T_Def));
1055 -- Start of processing for Access_Subprogram_Declaration
1057 begin
1058 Check_SPARK_05_Restriction ("access type is not allowed", T_Def);
1060 -- Associate the Itype node with the inner full-type declaration or
1061 -- subprogram spec or entry body. This is required to handle nested
1062 -- anonymous declarations. For example:
1064 -- procedure P
1065 -- (X : access procedure
1066 -- (Y : access procedure
1067 -- (Z : access T)))
1069 D_Ityp := Associated_Node_For_Itype (Desig_Type);
1070 while not (Nkind_In (D_Ityp, N_Full_Type_Declaration,
1071 N_Private_Type_Declaration,
1072 N_Private_Extension_Declaration,
1073 N_Procedure_Specification,
1074 N_Function_Specification,
1075 N_Entry_Body)
1077 or else
1078 Nkind_In (D_Ityp, N_Object_Declaration,
1079 N_Object_Renaming_Declaration,
1080 N_Formal_Object_Declaration,
1081 N_Formal_Type_Declaration,
1082 N_Task_Type_Declaration,
1083 N_Protected_Type_Declaration))
1084 loop
1085 D_Ityp := Parent (D_Ityp);
1086 pragma Assert (D_Ityp /= Empty);
1087 end loop;
1089 Set_Associated_Node_For_Itype (Desig_Type, D_Ityp);
1091 if Nkind_In (D_Ityp, N_Procedure_Specification,
1092 N_Function_Specification)
1093 then
1094 Set_Scope (Desig_Type, Scope (Defining_Entity (D_Ityp)));
1096 elsif Nkind_In (D_Ityp, N_Full_Type_Declaration,
1097 N_Object_Declaration,
1098 N_Object_Renaming_Declaration,
1099 N_Formal_Type_Declaration)
1100 then
1101 Set_Scope (Desig_Type, Scope (Defining_Identifier (D_Ityp)));
1102 end if;
1104 if Nkind (T_Def) = N_Access_Function_Definition then
1105 if Nkind (Result_Definition (T_Def)) = N_Access_Definition then
1106 declare
1107 Acc : constant Node_Id := Result_Definition (T_Def);
1109 begin
1110 if Present (Access_To_Subprogram_Definition (Acc))
1111 and then
1112 Protected_Present (Access_To_Subprogram_Definition (Acc))
1113 then
1114 Set_Etype
1115 (Desig_Type,
1116 Replace_Anonymous_Access_To_Protected_Subprogram
1117 (T_Def));
1119 else
1120 Set_Etype
1121 (Desig_Type,
1122 Access_Definition (T_Def, Result_Definition (T_Def)));
1123 end if;
1124 end;
1126 else
1127 Analyze (Result_Definition (T_Def));
1129 declare
1130 Typ : constant Entity_Id := Entity (Result_Definition (T_Def));
1132 begin
1133 -- If a null exclusion is imposed on the result type, then
1134 -- create a null-excluding itype (an access subtype) and use
1135 -- it as the function's Etype.
1137 if Is_Access_Type (Typ)
1138 and then Null_Exclusion_In_Return_Present (T_Def)
1139 then
1140 Set_Etype (Desig_Type,
1141 Create_Null_Excluding_Itype
1142 (T => Typ,
1143 Related_Nod => T_Def,
1144 Scope_Id => Current_Scope));
1146 else
1147 if From_Limited_With (Typ) then
1149 -- AI05-151: Incomplete types are allowed in all basic
1150 -- declarations, including access to subprograms.
1152 if Ada_Version >= Ada_2012 then
1153 null;
1155 else
1156 Error_Msg_NE
1157 ("illegal use of incomplete type&",
1158 Result_Definition (T_Def), Typ);
1159 end if;
1161 elsif Ekind (Current_Scope) = E_Package
1162 and then In_Private_Part (Current_Scope)
1163 then
1164 if Ekind (Typ) = E_Incomplete_Type then
1165 Append_Elmt (Desig_Type, Private_Dependents (Typ));
1167 elsif Is_Class_Wide_Type (Typ)
1168 and then Ekind (Etype (Typ)) = E_Incomplete_Type
1169 then
1170 Append_Elmt
1171 (Desig_Type, Private_Dependents (Etype (Typ)));
1172 end if;
1173 end if;
1175 Set_Etype (Desig_Type, Typ);
1176 end if;
1177 end;
1178 end if;
1180 if not (Is_Type (Etype (Desig_Type))) then
1181 Error_Msg_N
1182 ("expect type in function specification",
1183 Result_Definition (T_Def));
1184 end if;
1186 else
1187 Set_Etype (Desig_Type, Standard_Void_Type);
1188 end if;
1190 if Present (Formals) then
1191 Push_Scope (Desig_Type);
1193 -- Some special tests here. These special tests can be removed
1194 -- if and when Itypes always have proper parent pointers to their
1195 -- declarations???
1197 -- Special test 1) Link defining_identifier of formals. Required by
1198 -- First_Formal to provide its functionality.
1200 declare
1201 F : Node_Id;
1203 begin
1204 F := First (Formals);
1206 -- In ASIS mode, the access_to_subprogram may be analyzed twice,
1207 -- when it is part of an unconstrained type and subtype expansion
1208 -- is disabled. To avoid back-end problems with shared profiles,
1209 -- use previous subprogram type as the designated type, and then
1210 -- remove scope added above.
1212 if ASIS_Mode and then Present (Scope (Defining_Identifier (F)))
1213 then
1214 Set_Etype (T_Name, T_Name);
1215 Init_Size_Align (T_Name);
1216 Set_Directly_Designated_Type (T_Name,
1217 Scope (Defining_Identifier (F)));
1218 End_Scope;
1219 return;
1220 end if;
1222 while Present (F) loop
1223 if No (Parent (Defining_Identifier (F))) then
1224 Set_Parent (Defining_Identifier (F), F);
1225 end if;
1227 Next (F);
1228 end loop;
1229 end;
1231 Process_Formals (Formals, Parent (T_Def));
1233 -- Special test 2) End_Scope requires that the parent pointer be set
1234 -- to something reasonable, but Itypes don't have parent pointers. So
1235 -- we set it and then unset it ???
1237 Set_Parent (Desig_Type, T_Name);
1238 End_Scope;
1239 Set_Parent (Desig_Type, Empty);
1240 end if;
1242 -- Check for premature usage of the type being defined
1244 Check_For_Premature_Usage (T_Def);
1246 -- The return type and/or any parameter type may be incomplete. Mark the
1247 -- subprogram_type as depending on the incomplete type, so that it can
1248 -- be updated when the full type declaration is seen. This only applies
1249 -- to incomplete types declared in some enclosing scope, not to limited
1250 -- views from other packages.
1252 -- Prior to Ada 2012, access to functions can only have in_parameters.
1254 if Present (Formals) then
1255 Formal := First_Formal (Desig_Type);
1256 while Present (Formal) loop
1257 if Ekind (Formal) /= E_In_Parameter
1258 and then Nkind (T_Def) = N_Access_Function_Definition
1259 and then Ada_Version < Ada_2012
1260 then
1261 Error_Msg_N ("functions can only have IN parameters", Formal);
1262 end if;
1264 if Ekind (Etype (Formal)) = E_Incomplete_Type
1265 and then In_Open_Scopes (Scope (Etype (Formal)))
1266 then
1267 Append_Elmt (Desig_Type, Private_Dependents (Etype (Formal)));
1268 Set_Has_Delayed_Freeze (Desig_Type);
1269 end if;
1271 Next_Formal (Formal);
1272 end loop;
1273 end if;
1275 -- Check whether an indirect call without actuals may be possible. This
1276 -- is used when resolving calls whose result is then indexed.
1278 May_Need_Actuals (Desig_Type);
1280 -- If the return type is incomplete, this is legal as long as the type
1281 -- is declared in the current scope and will be completed in it (rather
1282 -- than being part of limited view).
1284 if Ekind (Etype (Desig_Type)) = E_Incomplete_Type
1285 and then not Has_Delayed_Freeze (Desig_Type)
1286 and then In_Open_Scopes (Scope (Etype (Desig_Type)))
1287 then
1288 Append_Elmt (Desig_Type, Private_Dependents (Etype (Desig_Type)));
1289 Set_Has_Delayed_Freeze (Desig_Type);
1290 end if;
1292 Check_Delayed_Subprogram (Desig_Type);
1294 if Protected_Present (T_Def) then
1295 Set_Ekind (T_Name, E_Access_Protected_Subprogram_Type);
1296 Set_Convention (Desig_Type, Convention_Protected);
1297 else
1298 Set_Ekind (T_Name, E_Access_Subprogram_Type);
1299 end if;
1301 Set_Can_Use_Internal_Rep (T_Name, not Always_Compatible_Rep_On_Target);
1303 Set_Etype (T_Name, T_Name);
1304 Init_Size_Align (T_Name);
1305 Set_Directly_Designated_Type (T_Name, Desig_Type);
1307 Generate_Reference_To_Formals (T_Name);
1309 -- Ada 2005 (AI-231): Propagate the null-excluding attribute
1311 Set_Can_Never_Be_Null (T_Name, Null_Exclusion_Present (T_Def));
1313 Check_Restriction (No_Access_Subprograms, T_Def);
1314 end Access_Subprogram_Declaration;
1316 ----------------------------
1317 -- Access_Type_Declaration --
1318 ----------------------------
1320 procedure Access_Type_Declaration (T : Entity_Id; Def : Node_Id) is
1321 P : constant Node_Id := Parent (Def);
1322 S : constant Node_Id := Subtype_Indication (Def);
1324 Full_Desig : Entity_Id;
1326 begin
1327 Check_SPARK_05_Restriction ("access type is not allowed", Def);
1329 -- Check for permissible use of incomplete type
1331 if Nkind (S) /= N_Subtype_Indication then
1332 Analyze (S);
1334 if Present (Entity (S))
1335 and then Ekind (Root_Type (Entity (S))) = E_Incomplete_Type
1336 then
1337 Set_Directly_Designated_Type (T, Entity (S));
1339 -- If the designated type is a limited view, we cannot tell if
1340 -- the full view contains tasks, and there is no way to handle
1341 -- that full view in a client. We create a master entity for the
1342 -- scope, which will be used when a client determines that one
1343 -- is needed.
1345 if From_Limited_With (Entity (S))
1346 and then not Is_Class_Wide_Type (Entity (S))
1347 then
1348 Set_Ekind (T, E_Access_Type);
1349 Build_Master_Entity (T);
1350 Build_Master_Renaming (T);
1351 end if;
1353 else
1354 Set_Directly_Designated_Type (T, Process_Subtype (S, P, T, 'P'));
1355 end if;
1357 -- If the access definition is of the form: ACCESS NOT NULL ..
1358 -- the subtype indication must be of an access type. Create
1359 -- a null-excluding subtype of it.
1361 if Null_Excluding_Subtype (Def) then
1362 if not Is_Access_Type (Entity (S)) then
1363 Error_Msg_N ("null exclusion must apply to access type", Def);
1365 else
1366 declare
1367 Loc : constant Source_Ptr := Sloc (S);
1368 Decl : Node_Id;
1369 Nam : constant Entity_Id := Make_Temporary (Loc, 'S');
1371 begin
1372 Decl :=
1373 Make_Subtype_Declaration (Loc,
1374 Defining_Identifier => Nam,
1375 Subtype_Indication =>
1376 New_Occurrence_Of (Entity (S), Loc));
1377 Set_Null_Exclusion_Present (Decl);
1378 Insert_Before (Parent (Def), Decl);
1379 Analyze (Decl);
1380 Set_Entity (S, Nam);
1381 end;
1382 end if;
1383 end if;
1385 else
1386 Set_Directly_Designated_Type (T,
1387 Process_Subtype (S, P, T, 'P'));
1388 end if;
1390 if All_Present (Def) or Constant_Present (Def) then
1391 Set_Ekind (T, E_General_Access_Type);
1392 else
1393 Set_Ekind (T, E_Access_Type);
1394 end if;
1396 Full_Desig := Designated_Type (T);
1398 if Base_Type (Full_Desig) = T then
1399 Error_Msg_N ("access type cannot designate itself", S);
1401 -- In Ada 2005, the type may have a limited view through some unit in
1402 -- its own context, allowing the following circularity that cannot be
1403 -- detected earlier.
1405 elsif Is_Class_Wide_Type (Full_Desig) and then Etype (Full_Desig) = T
1406 then
1407 Error_Msg_N
1408 ("access type cannot designate its own class-wide type", S);
1410 -- Clean up indication of tagged status to prevent cascaded errors
1412 Set_Is_Tagged_Type (T, False);
1413 end if;
1415 Set_Etype (T, T);
1417 -- If the type has appeared already in a with_type clause, it is frozen
1418 -- and the pointer size is already set. Else, initialize.
1420 if not From_Limited_With (T) then
1421 Init_Size_Align (T);
1422 end if;
1424 -- Note that Has_Task is always false, since the access type itself
1425 -- is not a task type. See Einfo for more description on this point.
1426 -- Exactly the same consideration applies to Has_Controlled_Component
1427 -- and to Has_Protected.
1429 Set_Has_Task (T, False);
1430 Set_Has_Protected (T, False);
1431 Set_Has_Timing_Event (T, False);
1432 Set_Has_Controlled_Component (T, False);
1434 -- Initialize field Finalization_Master explicitly to Empty, to avoid
1435 -- problems where an incomplete view of this entity has been previously
1436 -- established by a limited with and an overlaid version of this field
1437 -- (Stored_Constraint) was initialized for the incomplete view.
1439 -- This reset is performed in most cases except where the access type
1440 -- has been created for the purposes of allocating or deallocating a
1441 -- build-in-place object. Such access types have explicitly set pools
1442 -- and finalization masters.
1444 if No (Associated_Storage_Pool (T)) then
1445 Set_Finalization_Master (T, Empty);
1446 end if;
1448 -- Ada 2005 (AI-231): Propagate the null-excluding and access-constant
1449 -- attributes
1451 Set_Can_Never_Be_Null (T, Null_Exclusion_Present (Def));
1452 Set_Is_Access_Constant (T, Constant_Present (Def));
1453 end Access_Type_Declaration;
1455 ----------------------------------
1456 -- Add_Interface_Tag_Components --
1457 ----------------------------------
1459 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id) is
1460 Loc : constant Source_Ptr := Sloc (N);
1461 L : List_Id;
1462 Last_Tag : Node_Id;
1464 procedure Add_Tag (Iface : Entity_Id);
1465 -- Add tag for one of the progenitor interfaces
1467 -------------
1468 -- Add_Tag --
1469 -------------
1471 procedure Add_Tag (Iface : Entity_Id) is
1472 Decl : Node_Id;
1473 Def : Node_Id;
1474 Tag : Entity_Id;
1475 Offset : Entity_Id;
1477 begin
1478 pragma Assert (Is_Tagged_Type (Iface) and then Is_Interface (Iface));
1480 -- This is a reasonable place to propagate predicates
1482 if Has_Predicates (Iface) then
1483 Set_Has_Predicates (Typ);
1484 end if;
1486 Def :=
1487 Make_Component_Definition (Loc,
1488 Aliased_Present => True,
1489 Subtype_Indication =>
1490 New_Occurrence_Of (RTE (RE_Interface_Tag), Loc));
1492 Tag := Make_Temporary (Loc, 'V');
1494 Decl :=
1495 Make_Component_Declaration (Loc,
1496 Defining_Identifier => Tag,
1497 Component_Definition => Def);
1499 Analyze_Component_Declaration (Decl);
1501 Set_Analyzed (Decl);
1502 Set_Ekind (Tag, E_Component);
1503 Set_Is_Tag (Tag);
1504 Set_Is_Aliased (Tag);
1505 Set_Related_Type (Tag, Iface);
1506 Init_Component_Location (Tag);
1508 pragma Assert (Is_Frozen (Iface));
1510 Set_DT_Entry_Count (Tag,
1511 DT_Entry_Count (First_Entity (Iface)));
1513 if No (Last_Tag) then
1514 Prepend (Decl, L);
1515 else
1516 Insert_After (Last_Tag, Decl);
1517 end if;
1519 Last_Tag := Decl;
1521 -- If the ancestor has discriminants we need to give special support
1522 -- to store the offset_to_top value of the secondary dispatch tables.
1523 -- For this purpose we add a supplementary component just after the
1524 -- field that contains the tag associated with each secondary DT.
1526 if Typ /= Etype (Typ) and then Has_Discriminants (Etype (Typ)) then
1527 Def :=
1528 Make_Component_Definition (Loc,
1529 Subtype_Indication =>
1530 New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
1532 Offset := Make_Temporary (Loc, 'V');
1534 Decl :=
1535 Make_Component_Declaration (Loc,
1536 Defining_Identifier => Offset,
1537 Component_Definition => Def);
1539 Analyze_Component_Declaration (Decl);
1541 Set_Analyzed (Decl);
1542 Set_Ekind (Offset, E_Component);
1543 Set_Is_Aliased (Offset);
1544 Set_Related_Type (Offset, Iface);
1545 Init_Component_Location (Offset);
1546 Insert_After (Last_Tag, Decl);
1547 Last_Tag := Decl;
1548 end if;
1549 end Add_Tag;
1551 -- Local variables
1553 Elmt : Elmt_Id;
1554 Ext : Node_Id;
1555 Comp : Node_Id;
1557 -- Start of processing for Add_Interface_Tag_Components
1559 begin
1560 if not RTE_Available (RE_Interface_Tag) then
1561 Error_Msg
1562 ("(Ada 2005) interface types not supported by this run-time!",
1563 Sloc (N));
1564 return;
1565 end if;
1567 if Ekind (Typ) /= E_Record_Type
1568 or else (Is_Concurrent_Record_Type (Typ)
1569 and then Is_Empty_List (Abstract_Interface_List (Typ)))
1570 or else (not Is_Concurrent_Record_Type (Typ)
1571 and then No (Interfaces (Typ))
1572 and then Is_Empty_Elmt_List (Interfaces (Typ)))
1573 then
1574 return;
1575 end if;
1577 -- Find the current last tag
1579 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1580 Ext := Record_Extension_Part (Type_Definition (N));
1581 else
1582 pragma Assert (Nkind (Type_Definition (N)) = N_Record_Definition);
1583 Ext := Type_Definition (N);
1584 end if;
1586 Last_Tag := Empty;
1588 if not (Present (Component_List (Ext))) then
1589 Set_Null_Present (Ext, False);
1590 L := New_List;
1591 Set_Component_List (Ext,
1592 Make_Component_List (Loc,
1593 Component_Items => L,
1594 Null_Present => False));
1595 else
1596 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1597 L := Component_Items
1598 (Component_List
1599 (Record_Extension_Part
1600 (Type_Definition (N))));
1601 else
1602 L := Component_Items
1603 (Component_List
1604 (Type_Definition (N)));
1605 end if;
1607 -- Find the last tag component
1609 Comp := First (L);
1610 while Present (Comp) loop
1611 if Nkind (Comp) = N_Component_Declaration
1612 and then Is_Tag (Defining_Identifier (Comp))
1613 then
1614 Last_Tag := Comp;
1615 end if;
1617 Next (Comp);
1618 end loop;
1619 end if;
1621 -- At this point L references the list of components and Last_Tag
1622 -- references the current last tag (if any). Now we add the tag
1623 -- corresponding with all the interfaces that are not implemented
1624 -- by the parent.
1626 if Present (Interfaces (Typ)) then
1627 Elmt := First_Elmt (Interfaces (Typ));
1628 while Present (Elmt) loop
1629 Add_Tag (Node (Elmt));
1630 Next_Elmt (Elmt);
1631 end loop;
1632 end if;
1633 end Add_Interface_Tag_Components;
1635 -------------------------------------
1636 -- Add_Internal_Interface_Entities --
1637 -------------------------------------
1639 procedure Add_Internal_Interface_Entities (Tagged_Type : Entity_Id) is
1640 Elmt : Elmt_Id;
1641 Iface : Entity_Id;
1642 Iface_Elmt : Elmt_Id;
1643 Iface_Prim : Entity_Id;
1644 Ifaces_List : Elist_Id;
1645 New_Subp : Entity_Id := Empty;
1646 Prim : Entity_Id;
1647 Restore_Scope : Boolean := False;
1649 begin
1650 pragma Assert (Ada_Version >= Ada_2005
1651 and then Is_Record_Type (Tagged_Type)
1652 and then Is_Tagged_Type (Tagged_Type)
1653 and then Has_Interfaces (Tagged_Type)
1654 and then not Is_Interface (Tagged_Type));
1656 -- Ensure that the internal entities are added to the scope of the type
1658 if Scope (Tagged_Type) /= Current_Scope then
1659 Push_Scope (Scope (Tagged_Type));
1660 Restore_Scope := True;
1661 end if;
1663 Collect_Interfaces (Tagged_Type, Ifaces_List);
1665 Iface_Elmt := First_Elmt (Ifaces_List);
1666 while Present (Iface_Elmt) loop
1667 Iface := Node (Iface_Elmt);
1669 -- Originally we excluded here from this processing interfaces that
1670 -- are parents of Tagged_Type because their primitives are located
1671 -- in the primary dispatch table (and hence no auxiliary internal
1672 -- entities are required to handle secondary dispatch tables in such
1673 -- case). However, these auxiliary entities are also required to
1674 -- handle derivations of interfaces in formals of generics (see
1675 -- Derive_Subprograms).
1677 Elmt := First_Elmt (Primitive_Operations (Iface));
1678 while Present (Elmt) loop
1679 Iface_Prim := Node (Elmt);
1681 if not Is_Predefined_Dispatching_Operation (Iface_Prim) then
1682 Prim :=
1683 Find_Primitive_Covering_Interface
1684 (Tagged_Type => Tagged_Type,
1685 Iface_Prim => Iface_Prim);
1687 if No (Prim) and then Serious_Errors_Detected > 0 then
1688 goto Continue;
1689 end if;
1691 pragma Assert (Present (Prim));
1693 -- Ada 2012 (AI05-0197): If the name of the covering primitive
1694 -- differs from the name of the interface primitive then it is
1695 -- a private primitive inherited from a parent type. In such
1696 -- case, given that Tagged_Type covers the interface, the
1697 -- inherited private primitive becomes visible. For such
1698 -- purpose we add a new entity that renames the inherited
1699 -- private primitive.
1701 if Chars (Prim) /= Chars (Iface_Prim) then
1702 pragma Assert (Has_Suffix (Prim, 'P'));
1703 Derive_Subprogram
1704 (New_Subp => New_Subp,
1705 Parent_Subp => Iface_Prim,
1706 Derived_Type => Tagged_Type,
1707 Parent_Type => Iface);
1708 Set_Alias (New_Subp, Prim);
1709 Set_Is_Abstract_Subprogram
1710 (New_Subp, Is_Abstract_Subprogram (Prim));
1711 end if;
1713 Derive_Subprogram
1714 (New_Subp => New_Subp,
1715 Parent_Subp => Iface_Prim,
1716 Derived_Type => Tagged_Type,
1717 Parent_Type => Iface);
1719 declare
1720 Anc : Entity_Id;
1721 begin
1722 if Is_Inherited_Operation (Prim)
1723 and then Present (Alias (Prim))
1724 then
1725 Anc := Alias (Prim);
1726 else
1727 Anc := Overridden_Operation (Prim);
1728 end if;
1730 -- Apply legality checks in RM 6.1.1 (10-13) concerning
1731 -- nonconforming preconditions in both an ancestor and
1732 -- a progenitor operation.
1734 if Present (Anc)
1735 and then Has_Non_Trivial_Precondition (Anc)
1736 and then Has_Non_Trivial_Precondition (Iface_Prim)
1737 then
1738 if Is_Abstract_Subprogram (Prim)
1739 or else
1740 (Ekind (Prim) = E_Procedure
1741 and then Nkind (Parent (Prim)) =
1742 N_Procedure_Specification
1743 and then Null_Present (Parent (Prim)))
1744 then
1745 null;
1747 -- The inherited operation must be overridden
1749 elsif not Comes_From_Source (Prim) then
1750 Error_Msg_NE
1751 ("&inherits non-conforming preconditions and must "
1752 & "be overridden (RM 6.1.1 (10-16)",
1753 Parent (Tagged_Type), Prim);
1754 end if;
1755 end if;
1756 end;
1758 -- Ada 2005 (AI-251): Decorate internal entity Iface_Subp
1759 -- associated with interface types. These entities are
1760 -- only registered in the list of primitives of its
1761 -- corresponding tagged type because they are only used
1762 -- to fill the contents of the secondary dispatch tables.
1763 -- Therefore they are removed from the homonym chains.
1765 Set_Is_Hidden (New_Subp);
1766 Set_Is_Internal (New_Subp);
1767 Set_Alias (New_Subp, Prim);
1768 Set_Is_Abstract_Subprogram
1769 (New_Subp, Is_Abstract_Subprogram (Prim));
1770 Set_Interface_Alias (New_Subp, Iface_Prim);
1772 -- If the returned type is an interface then propagate it to
1773 -- the returned type. Needed by the thunk to generate the code
1774 -- which displaces "this" to reference the corresponding
1775 -- secondary dispatch table in the returned object.
1777 if Is_Interface (Etype (Iface_Prim)) then
1778 Set_Etype (New_Subp, Etype (Iface_Prim));
1779 end if;
1781 -- Internal entities associated with interface types are only
1782 -- registered in the list of primitives of the tagged type.
1783 -- They are only used to fill the contents of the secondary
1784 -- dispatch tables. Therefore they are not needed in the
1785 -- homonym chains.
1787 Remove_Homonym (New_Subp);
1789 -- Hidden entities associated with interfaces must have set
1790 -- the Has_Delay_Freeze attribute to ensure that, in case
1791 -- of locally defined tagged types (or compiling with static
1792 -- dispatch tables generation disabled) the corresponding
1793 -- entry of the secondary dispatch table is filled when such
1794 -- an entity is frozen. This is an expansion activity that must
1795 -- be suppressed for ASIS because it leads to gigi elaboration
1796 -- issues in annotate mode.
1798 if not ASIS_Mode then
1799 Set_Has_Delayed_Freeze (New_Subp);
1800 end if;
1801 end if;
1803 <<Continue>>
1804 Next_Elmt (Elmt);
1805 end loop;
1807 Next_Elmt (Iface_Elmt);
1808 end loop;
1810 if Restore_Scope then
1811 Pop_Scope;
1812 end if;
1813 end Add_Internal_Interface_Entities;
1815 -----------------------------------
1816 -- Analyze_Component_Declaration --
1817 -----------------------------------
1819 procedure Analyze_Component_Declaration (N : Node_Id) is
1820 Loc : constant Source_Ptr := Sloc (Component_Definition (N));
1821 Id : constant Entity_Id := Defining_Identifier (N);
1822 E : constant Node_Id := Expression (N);
1823 Typ : constant Node_Id :=
1824 Subtype_Indication (Component_Definition (N));
1825 T : Entity_Id;
1826 P : Entity_Id;
1828 function Contains_POC (Constr : Node_Id) return Boolean;
1829 -- Determines whether a constraint uses the discriminant of a record
1830 -- type thus becoming a per-object constraint (POC).
1832 function Is_Known_Limited (Typ : Entity_Id) return Boolean;
1833 -- Typ is the type of the current component, check whether this type is
1834 -- a limited type. Used to validate declaration against that of
1835 -- enclosing record.
1837 ------------------
1838 -- Contains_POC --
1839 ------------------
1841 function Contains_POC (Constr : Node_Id) return Boolean is
1842 begin
1843 -- Prevent cascaded errors
1845 if Error_Posted (Constr) then
1846 return False;
1847 end if;
1849 case Nkind (Constr) is
1850 when N_Attribute_Reference =>
1851 return Attribute_Name (Constr) = Name_Access
1852 and then Prefix (Constr) = Scope (Entity (Prefix (Constr)));
1854 when N_Discriminant_Association =>
1855 return Denotes_Discriminant (Expression (Constr));
1857 when N_Identifier =>
1858 return Denotes_Discriminant (Constr);
1860 when N_Index_Or_Discriminant_Constraint =>
1861 declare
1862 IDC : Node_Id;
1864 begin
1865 IDC := First (Constraints (Constr));
1866 while Present (IDC) loop
1868 -- One per-object constraint is sufficient
1870 if Contains_POC (IDC) then
1871 return True;
1872 end if;
1874 Next (IDC);
1875 end loop;
1877 return False;
1878 end;
1880 when N_Range =>
1881 return Denotes_Discriminant (Low_Bound (Constr))
1882 or else
1883 Denotes_Discriminant (High_Bound (Constr));
1885 when N_Range_Constraint =>
1886 return Denotes_Discriminant (Range_Expression (Constr));
1888 when others =>
1889 return False;
1890 end case;
1891 end Contains_POC;
1893 ----------------------
1894 -- Is_Known_Limited --
1895 ----------------------
1897 function Is_Known_Limited (Typ : Entity_Id) return Boolean is
1898 P : constant Entity_Id := Etype (Typ);
1899 R : constant Entity_Id := Root_Type (Typ);
1901 begin
1902 if Is_Limited_Record (Typ) then
1903 return True;
1905 -- If the root type is limited (and not a limited interface)
1906 -- so is the current type
1908 elsif Is_Limited_Record (R)
1909 and then (not Is_Interface (R) or else not Is_Limited_Interface (R))
1910 then
1911 return True;
1913 -- Else the type may have a limited interface progenitor, but a
1914 -- limited record parent.
1916 elsif R /= P and then Is_Limited_Record (P) then
1917 return True;
1919 else
1920 return False;
1921 end if;
1922 end Is_Known_Limited;
1924 -- Start of processing for Analyze_Component_Declaration
1926 begin
1927 Generate_Definition (Id);
1928 Enter_Name (Id);
1930 if Present (Typ) then
1931 T := Find_Type_Of_Object
1932 (Subtype_Indication (Component_Definition (N)), N);
1934 if not Nkind_In (Typ, N_Identifier, N_Expanded_Name) then
1935 Check_SPARK_05_Restriction ("subtype mark required", Typ);
1936 end if;
1938 -- Ada 2005 (AI-230): Access Definition case
1940 else
1941 pragma Assert (Present
1942 (Access_Definition (Component_Definition (N))));
1944 T := Access_Definition
1945 (Related_Nod => N,
1946 N => Access_Definition (Component_Definition (N)));
1947 Set_Is_Local_Anonymous_Access (T);
1949 -- Ada 2005 (AI-254)
1951 if Present (Access_To_Subprogram_Definition
1952 (Access_Definition (Component_Definition (N))))
1953 and then Protected_Present (Access_To_Subprogram_Definition
1954 (Access_Definition
1955 (Component_Definition (N))))
1956 then
1957 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1958 end if;
1959 end if;
1961 -- If the subtype is a constrained subtype of the enclosing record,
1962 -- (which must have a partial view) the back-end does not properly
1963 -- handle the recursion. Rewrite the component declaration with an
1964 -- explicit subtype indication, which is acceptable to Gigi. We can copy
1965 -- the tree directly because side effects have already been removed from
1966 -- discriminant constraints.
1968 if Ekind (T) = E_Access_Subtype
1969 and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
1970 and then Comes_From_Source (T)
1971 and then Nkind (Parent (T)) = N_Subtype_Declaration
1972 and then Etype (Directly_Designated_Type (T)) = Current_Scope
1973 then
1974 Rewrite
1975 (Subtype_Indication (Component_Definition (N)),
1976 New_Copy_Tree (Subtype_Indication (Parent (T))));
1977 T := Find_Type_Of_Object
1978 (Subtype_Indication (Component_Definition (N)), N);
1979 end if;
1981 -- If the component declaration includes a default expression, then we
1982 -- check that the component is not of a limited type (RM 3.7(5)),
1983 -- and do the special preanalysis of the expression (see section on
1984 -- "Handling of Default and Per-Object Expressions" in the spec of
1985 -- package Sem).
1987 if Present (E) then
1988 Check_SPARK_05_Restriction ("default expression is not allowed", E);
1989 Preanalyze_Default_Expression (E, T);
1990 Check_Initialization (T, E);
1992 if Ada_Version >= Ada_2005
1993 and then Ekind (T) = E_Anonymous_Access_Type
1994 and then Etype (E) /= Any_Type
1995 then
1996 -- Check RM 3.9.2(9): "if the expected type for an expression is
1997 -- an anonymous access-to-specific tagged type, then the object
1998 -- designated by the expression shall not be dynamically tagged
1999 -- unless it is a controlling operand in a call on a dispatching
2000 -- operation"
2002 if Is_Tagged_Type (Directly_Designated_Type (T))
2003 and then
2004 Ekind (Directly_Designated_Type (T)) /= E_Class_Wide_Type
2005 and then
2006 Ekind (Directly_Designated_Type (Etype (E))) =
2007 E_Class_Wide_Type
2008 then
2009 Error_Msg_N
2010 ("access to specific tagged type required (RM 3.9.2(9))", E);
2011 end if;
2013 -- (Ada 2005: AI-230): Accessibility check for anonymous
2014 -- components
2016 if Type_Access_Level (Etype (E)) >
2017 Deepest_Type_Access_Level (T)
2018 then
2019 Error_Msg_N
2020 ("expression has deeper access level than component " &
2021 "(RM 3.10.2 (12.2))", E);
2022 end if;
2024 -- The initialization expression is a reference to an access
2025 -- discriminant. The type of the discriminant is always deeper
2026 -- than any access type.
2028 if Ekind (Etype (E)) = E_Anonymous_Access_Type
2029 and then Is_Entity_Name (E)
2030 and then Ekind (Entity (E)) = E_In_Parameter
2031 and then Present (Discriminal_Link (Entity (E)))
2032 then
2033 Error_Msg_N
2034 ("discriminant has deeper accessibility level than target",
2036 end if;
2037 end if;
2038 end if;
2040 -- The parent type may be a private view with unknown discriminants,
2041 -- and thus unconstrained. Regular components must be constrained.
2043 if not Is_Definite_Subtype (T) and then Chars (Id) /= Name_uParent then
2044 if Is_Class_Wide_Type (T) then
2045 Error_Msg_N
2046 ("class-wide subtype with unknown discriminants" &
2047 " in component declaration",
2048 Subtype_Indication (Component_Definition (N)));
2049 else
2050 Error_Msg_N
2051 ("unconstrained subtype in component declaration",
2052 Subtype_Indication (Component_Definition (N)));
2053 end if;
2055 -- Components cannot be abstract, except for the special case of
2056 -- the _Parent field (case of extending an abstract tagged type)
2058 elsif Is_Abstract_Type (T) and then Chars (Id) /= Name_uParent then
2059 Error_Msg_N ("type of a component cannot be abstract", N);
2060 end if;
2062 Set_Etype (Id, T);
2063 Set_Is_Aliased (Id, Aliased_Present (Component_Definition (N)));
2065 -- The component declaration may have a per-object constraint, set
2066 -- the appropriate flag in the defining identifier of the subtype.
2068 if Present (Subtype_Indication (Component_Definition (N))) then
2069 declare
2070 Sindic : constant Node_Id :=
2071 Subtype_Indication (Component_Definition (N));
2072 begin
2073 if Nkind (Sindic) = N_Subtype_Indication
2074 and then Present (Constraint (Sindic))
2075 and then Contains_POC (Constraint (Sindic))
2076 then
2077 Set_Has_Per_Object_Constraint (Id);
2078 end if;
2079 end;
2080 end if;
2082 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
2083 -- out some static checks.
2085 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (T) then
2086 Null_Exclusion_Static_Checks (N);
2087 end if;
2089 -- If this component is private (or depends on a private type), flag the
2090 -- record type to indicate that some operations are not available.
2092 P := Private_Component (T);
2094 if Present (P) then
2096 -- Check for circular definitions
2098 if P = Any_Type then
2099 Set_Etype (Id, Any_Type);
2101 -- There is a gap in the visibility of operations only if the
2102 -- component type is not defined in the scope of the record type.
2104 elsif Scope (P) = Scope (Current_Scope) then
2105 null;
2107 elsif Is_Limited_Type (P) then
2108 Set_Is_Limited_Composite (Current_Scope);
2110 else
2111 Set_Is_Private_Composite (Current_Scope);
2112 end if;
2113 end if;
2115 if P /= Any_Type
2116 and then Is_Limited_Type (T)
2117 and then Chars (Id) /= Name_uParent
2118 and then Is_Tagged_Type (Current_Scope)
2119 then
2120 if Is_Derived_Type (Current_Scope)
2121 and then not Is_Known_Limited (Current_Scope)
2122 then
2123 Error_Msg_N
2124 ("extension of nonlimited type cannot have limited components",
2127 if Is_Interface (Root_Type (Current_Scope)) then
2128 Error_Msg_N
2129 ("\limitedness is not inherited from limited interface", N);
2130 Error_Msg_N ("\add LIMITED to type indication", N);
2131 end if;
2133 Explain_Limited_Type (T, N);
2134 Set_Etype (Id, Any_Type);
2135 Set_Is_Limited_Composite (Current_Scope, False);
2137 elsif not Is_Derived_Type (Current_Scope)
2138 and then not Is_Limited_Record (Current_Scope)
2139 and then not Is_Concurrent_Type (Current_Scope)
2140 then
2141 Error_Msg_N
2142 ("nonlimited tagged type cannot have limited components", N);
2143 Explain_Limited_Type (T, N);
2144 Set_Etype (Id, Any_Type);
2145 Set_Is_Limited_Composite (Current_Scope, False);
2146 end if;
2147 end if;
2149 -- If the component is an unconstrained task or protected type with
2150 -- discriminants, the component and the enclosing record are limited
2151 -- and the component is constrained by its default values. Compute
2152 -- its actual subtype, else it may be allocated the maximum size by
2153 -- the backend, and possibly overflow.
2155 if Is_Concurrent_Type (T)
2156 and then not Is_Constrained (T)
2157 and then Has_Discriminants (T)
2158 and then not Has_Discriminants (Current_Scope)
2159 then
2160 declare
2161 Act_T : constant Entity_Id := Build_Default_Subtype (T, N);
2163 begin
2164 Set_Etype (Id, Act_T);
2166 -- Rewrite component definition to use the constrained subtype
2168 Rewrite (Component_Definition (N),
2169 Make_Component_Definition (Loc,
2170 Subtype_Indication => New_Occurrence_Of (Act_T, Loc)));
2171 end;
2172 end if;
2174 Set_Original_Record_Component (Id, Id);
2176 if Has_Aspects (N) then
2177 Analyze_Aspect_Specifications (N, Id);
2178 end if;
2180 Analyze_Dimension (N);
2181 end Analyze_Component_Declaration;
2183 --------------------------
2184 -- Analyze_Declarations --
2185 --------------------------
2187 procedure Analyze_Declarations (L : List_Id) is
2188 Decl : Node_Id;
2190 procedure Adjust_Decl;
2191 -- Adjust Decl not to include implicit label declarations, since these
2192 -- have strange Sloc values that result in elaboration check problems.
2193 -- (They have the sloc of the label as found in the source, and that
2194 -- is ahead of the current declarative part).
2196 procedure Build_Assertion_Bodies (Decls : List_Id; Context : Node_Id);
2197 -- Create the subprogram bodies which verify the run-time semantics of
2198 -- the pragmas listed below for each elibigle type found in declarative
2199 -- list Decls. The pragmas are:
2201 -- Default_Initial_Condition
2202 -- Invariant
2203 -- Type_Invariant
2205 -- Context denotes the owner of the declarative list.
2207 procedure Check_Entry_Contracts;
2208 -- Perform a pre-analysis of the pre- and postconditions of an entry
2209 -- declaration. This must be done before full resolution and creation
2210 -- of the parameter block, etc. to catch illegal uses within the
2211 -- contract expression. Full analysis of the expression is done when
2212 -- the contract is processed.
2214 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id);
2215 -- Determine whether Body_Decl denotes the body of a late controlled
2216 -- primitive (either Initialize, Adjust or Finalize). If this is the
2217 -- case, add a proper spec if the body lacks one. The spec is inserted
2218 -- before Body_Decl and immediately analyzed.
2220 procedure Remove_Partial_Visible_Refinements (Spec_Id : Entity_Id);
2221 -- Spec_Id is the entity of a package that may define abstract states,
2222 -- and in the case of a child unit, whose ancestors may define abstract
2223 -- states. If the states have partial visible refinement, remove the
2224 -- partial visibility of each constituent at the end of the package
2225 -- spec and body declarations.
2227 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id);
2228 -- Spec_Id is the entity of a package that may define abstract states.
2229 -- If the states have visible refinement, remove the visibility of each
2230 -- constituent at the end of the package body declaration.
2232 procedure Resolve_Aspects;
2233 -- Utility to resolve the expressions of aspects at the end of a list of
2234 -- declarations.
2236 function Uses_Unseen_Lib_Unit_Priv (Pkg : Entity_Id) return Boolean;
2237 -- Check if an inner package has entities within it that rely on library
2238 -- level private types where the full view has not been seen.
2240 -----------------
2241 -- Adjust_Decl --
2242 -----------------
2244 procedure Adjust_Decl is
2245 begin
2246 while Present (Prev (Decl))
2247 and then Nkind (Decl) = N_Implicit_Label_Declaration
2248 loop
2249 Prev (Decl);
2250 end loop;
2251 end Adjust_Decl;
2253 ----------------------------
2254 -- Build_Assertion_Bodies --
2255 ----------------------------
2257 procedure Build_Assertion_Bodies (Decls : List_Id; Context : Node_Id) is
2258 procedure Build_Assertion_Bodies_For_Type (Typ : Entity_Id);
2259 -- Create the subprogram bodies which verify the run-time semantics
2260 -- of the pragmas listed below for type Typ. The pragmas are:
2262 -- Default_Initial_Condition
2263 -- Invariant
2264 -- Type_Invariant
2266 -------------------------------------
2267 -- Build_Assertion_Bodies_For_Type --
2268 -------------------------------------
2270 procedure Build_Assertion_Bodies_For_Type (Typ : Entity_Id) is
2271 begin
2272 -- Preanalyze and resolve the Default_Initial_Condition assertion
2273 -- expression at the end of the declarations to catch any errors.
2275 if Has_DIC (Typ) then
2276 Build_DIC_Procedure_Body (Typ);
2277 end if;
2279 if Nkind (Context) = N_Package_Specification then
2281 -- Preanalyze and resolve the class-wide invariants of an
2282 -- interface at the end of whichever declarative part has the
2283 -- interface type. Note that an interface may be declared in
2284 -- any non-package declarative part, but reaching the end of
2285 -- such a declarative part will always freeze the type and
2286 -- generate the invariant procedure (see Freeze_Type).
2288 if Is_Interface (Typ) then
2290 -- Interfaces are treated as the partial view of a private
2291 -- type, in order to achieve uniformity with the general
2292 -- case. As a result, an interface receives only a "partial"
2293 -- invariant procedure, which is never called.
2295 if Has_Own_Invariants (Typ) then
2296 Build_Invariant_Procedure_Body
2297 (Typ => Typ,
2298 Partial_Invariant => True);
2299 end if;
2301 -- Preanalyze and resolve the invariants of a private type
2302 -- at the end of the visible declarations to catch potential
2303 -- errors. Inherited class-wide invariants are not included
2304 -- because they have already been resolved.
2306 elsif Decls = Visible_Declarations (Context)
2307 and then Ekind_In (Typ, E_Limited_Private_Type,
2308 E_Private_Type,
2309 E_Record_Type_With_Private)
2310 and then Has_Own_Invariants (Typ)
2311 then
2312 Build_Invariant_Procedure_Body
2313 (Typ => Typ,
2314 Partial_Invariant => True);
2316 -- Preanalyze and resolve the invariants of a private type's
2317 -- full view at the end of the private declarations to catch
2318 -- potential errors.
2320 elsif Decls = Private_Declarations (Context)
2321 and then not Is_Private_Type (Typ)
2322 and then Has_Private_Declaration (Typ)
2323 and then Has_Invariants (Typ)
2324 then
2325 Build_Invariant_Procedure_Body (Typ);
2326 end if;
2327 end if;
2328 end Build_Assertion_Bodies_For_Type;
2330 -- Local variables
2332 Decl : Node_Id;
2333 Decl_Id : Entity_Id;
2335 -- Start of processing for Build_Assertion_Bodies
2337 begin
2338 Decl := First (Decls);
2339 while Present (Decl) loop
2340 if Is_Declaration (Decl) then
2341 Decl_Id := Defining_Entity (Decl);
2343 if Is_Type (Decl_Id) then
2344 Build_Assertion_Bodies_For_Type (Decl_Id);
2345 end if;
2346 end if;
2348 Next (Decl);
2349 end loop;
2350 end Build_Assertion_Bodies;
2352 ---------------------------
2353 -- Check_Entry_Contracts --
2354 ---------------------------
2356 procedure Check_Entry_Contracts is
2357 ASN : Node_Id;
2358 Ent : Entity_Id;
2359 Exp : Node_Id;
2361 begin
2362 Ent := First_Entity (Current_Scope);
2363 while Present (Ent) loop
2365 -- This only concerns entries with pre/postconditions
2367 if Ekind (Ent) = E_Entry
2368 and then Present (Contract (Ent))
2369 and then Present (Pre_Post_Conditions (Contract (Ent)))
2370 then
2371 ASN := Pre_Post_Conditions (Contract (Ent));
2372 Push_Scope (Ent);
2373 Install_Formals (Ent);
2375 -- Pre/postconditions are rewritten as Check pragmas. Analysis
2376 -- is performed on a copy of the pragma expression, to prevent
2377 -- modifying the original expression.
2379 while Present (ASN) loop
2380 if Nkind (ASN) = N_Pragma then
2381 Exp :=
2382 New_Copy_Tree
2383 (Expression
2384 (First (Pragma_Argument_Associations (ASN))));
2385 Set_Parent (Exp, ASN);
2387 Preanalyze_Assert_Expression (Exp, Standard_Boolean);
2388 end if;
2390 ASN := Next_Pragma (ASN);
2391 end loop;
2393 End_Scope;
2394 end if;
2396 Next_Entity (Ent);
2397 end loop;
2398 end Check_Entry_Contracts;
2400 --------------------------------------
2401 -- Handle_Late_Controlled_Primitive --
2402 --------------------------------------
2404 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id) is
2405 Body_Spec : constant Node_Id := Specification (Body_Decl);
2406 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2407 Loc : constant Source_Ptr := Sloc (Body_Id);
2408 Params : constant List_Id :=
2409 Parameter_Specifications (Body_Spec);
2410 Spec : Node_Id;
2411 Spec_Id : Entity_Id;
2412 Typ : Node_Id;
2414 begin
2415 -- Consider only procedure bodies whose name matches one of the three
2416 -- controlled primitives.
2418 if Nkind (Body_Spec) /= N_Procedure_Specification
2419 or else not Nam_In (Chars (Body_Id), Name_Adjust,
2420 Name_Finalize,
2421 Name_Initialize)
2422 then
2423 return;
2425 -- A controlled primitive must have exactly one formal which is not
2426 -- an anonymous access type.
2428 elsif List_Length (Params) /= 1 then
2429 return;
2430 end if;
2432 Typ := Parameter_Type (First (Params));
2434 if Nkind (Typ) = N_Access_Definition then
2435 return;
2436 end if;
2438 Find_Type (Typ);
2440 -- The type of the formal must be derived from [Limited_]Controlled
2442 if not Is_Controlled (Entity (Typ)) then
2443 return;
2444 end if;
2446 -- Check whether a specification exists for this body. We do not
2447 -- analyze the spec of the body in full, because it will be analyzed
2448 -- again when the body is properly analyzed, and we cannot create
2449 -- duplicate entries in the formals chain. We look for an explicit
2450 -- specification because the body may be an overriding operation and
2451 -- an inherited spec may be present.
2453 Spec_Id := Current_Entity (Body_Id);
2455 while Present (Spec_Id) loop
2456 if Ekind_In (Spec_Id, E_Procedure, E_Generic_Procedure)
2457 and then Scope (Spec_Id) = Current_Scope
2458 and then Present (First_Formal (Spec_Id))
2459 and then No (Next_Formal (First_Formal (Spec_Id)))
2460 and then Etype (First_Formal (Spec_Id)) = Entity (Typ)
2461 and then Comes_From_Source (Spec_Id)
2462 then
2463 return;
2464 end if;
2466 Spec_Id := Homonym (Spec_Id);
2467 end loop;
2469 -- At this point the body is known to be a late controlled primitive.
2470 -- Generate a matching spec and insert it before the body. Note the
2471 -- use of Copy_Separate_Tree - we want an entirely separate semantic
2472 -- tree in this case.
2474 Spec := Copy_Separate_Tree (Body_Spec);
2476 -- Ensure that the subprogram declaration does not inherit the null
2477 -- indicator from the body as we now have a proper spec/body pair.
2479 Set_Null_Present (Spec, False);
2481 -- Ensure that the freeze node is inserted after the declaration of
2482 -- the primitive since its expansion will freeze the primitive.
2484 Decl := Make_Subprogram_Declaration (Loc, Specification => Spec);
2486 Insert_Before_And_Analyze (Body_Decl, Decl);
2487 end Handle_Late_Controlled_Primitive;
2489 ----------------------------------------
2490 -- Remove_Partial_Visible_Refinements --
2491 ----------------------------------------
2493 procedure Remove_Partial_Visible_Refinements (Spec_Id : Entity_Id) is
2494 State_Elmt : Elmt_Id;
2495 begin
2496 if Present (Abstract_States (Spec_Id)) then
2497 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2498 while Present (State_Elmt) loop
2499 Set_Has_Partial_Visible_Refinement (Node (State_Elmt), False);
2500 Next_Elmt (State_Elmt);
2501 end loop;
2502 end if;
2504 -- For a child unit, also hide the partial state refinement from
2505 -- ancestor packages.
2507 if Is_Child_Unit (Spec_Id) then
2508 Remove_Partial_Visible_Refinements (Scope (Spec_Id));
2509 end if;
2510 end Remove_Partial_Visible_Refinements;
2512 --------------------------------
2513 -- Remove_Visible_Refinements --
2514 --------------------------------
2516 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id) is
2517 State_Elmt : Elmt_Id;
2518 begin
2519 if Present (Abstract_States (Spec_Id)) then
2520 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2521 while Present (State_Elmt) loop
2522 Set_Has_Visible_Refinement (Node (State_Elmt), False);
2523 Next_Elmt (State_Elmt);
2524 end loop;
2525 end if;
2526 end Remove_Visible_Refinements;
2528 ---------------------
2529 -- Resolve_Aspects --
2530 ---------------------
2532 procedure Resolve_Aspects is
2533 E : Entity_Id;
2535 begin
2536 E := First_Entity (Current_Scope);
2537 while Present (E) loop
2538 Resolve_Aspect_Expressions (E);
2539 Next_Entity (E);
2540 end loop;
2541 end Resolve_Aspects;
2543 -------------------------------
2544 -- Uses_Unseen_Lib_Unit_Priv --
2545 -------------------------------
2547 function Uses_Unseen_Lib_Unit_Priv (Pkg : Entity_Id) return Boolean is
2548 Curr : Entity_Id;
2550 begin
2551 -- Avoid looking through scopes that do not meet the precondition of
2552 -- Pkg not being within a library unit spec.
2554 if not Is_Compilation_Unit (Pkg)
2555 and then not Is_Generic_Instance (Pkg)
2556 and then not In_Package_Body (Enclosing_Lib_Unit_Entity (Pkg))
2557 then
2558 -- Loop through all entities in the current scope to identify
2559 -- an entity that depends on a private type.
2561 Curr := First_Entity (Pkg);
2562 loop
2563 if Nkind (Curr) in N_Entity
2564 and then Depends_On_Private (Curr)
2565 then
2566 return True;
2567 end if;
2569 exit when Last_Entity (Current_Scope) = Curr;
2570 Curr := Next_Entity (Curr);
2571 end loop;
2572 end if;
2574 return False;
2575 end Uses_Unseen_Lib_Unit_Priv;
2577 -- Local variables
2579 Context : Node_Id := Empty;
2580 Freeze_From : Entity_Id := Empty;
2581 Next_Decl : Node_Id;
2583 Body_Seen : Boolean := False;
2584 -- Flag set when the first body [stub] is encountered
2586 -- Start of processing for Analyze_Declarations
2588 begin
2589 if Restriction_Check_Required (SPARK_05) then
2590 Check_Later_Vs_Basic_Declarations (L, During_Parsing => False);
2591 end if;
2593 Decl := First (L);
2594 while Present (Decl) loop
2596 -- Package spec cannot contain a package declaration in SPARK
2598 if Nkind (Decl) = N_Package_Declaration
2599 and then Nkind (Parent (L)) = N_Package_Specification
2600 then
2601 Check_SPARK_05_Restriction
2602 ("package specification cannot contain a package declaration",
2603 Decl);
2604 end if;
2606 -- Complete analysis of declaration
2608 Analyze (Decl);
2609 Next_Decl := Next (Decl);
2611 if No (Freeze_From) then
2612 Freeze_From := First_Entity (Current_Scope);
2613 end if;
2615 -- At the end of a declarative part, freeze remaining entities
2616 -- declared in it. The end of the visible declarations of package
2617 -- specification is not the end of a declarative part if private
2618 -- declarations are present. The end of a package declaration is a
2619 -- freezing point only if it a library package. A task definition or
2620 -- protected type definition is not a freeze point either. Finally,
2621 -- we do not freeze entities in generic scopes, because there is no
2622 -- code generated for them and freeze nodes will be generated for
2623 -- the instance.
2625 -- The end of a package instantiation is not a freeze point, but
2626 -- for now we make it one, because the generic body is inserted
2627 -- (currently) immediately after. Generic instantiations will not
2628 -- be a freeze point once delayed freezing of bodies is implemented.
2629 -- (This is needed in any case for early instantiations ???).
2631 if No (Next_Decl) then
2632 if Nkind (Parent (L)) = N_Component_List then
2633 null;
2635 elsif Nkind_In (Parent (L), N_Protected_Definition,
2636 N_Task_Definition)
2637 then
2638 Check_Entry_Contracts;
2640 elsif Nkind (Parent (L)) /= N_Package_Specification then
2641 if Nkind (Parent (L)) = N_Package_Body then
2642 Freeze_From := First_Entity (Current_Scope);
2643 end if;
2645 -- There may have been several freezing points previously,
2646 -- for example object declarations or subprogram bodies, but
2647 -- at the end of a declarative part we check freezing from
2648 -- the beginning, even though entities may already be frozen,
2649 -- in order to perform visibility checks on delayed aspects.
2651 Adjust_Decl;
2653 -- If the current scope is a generic subprogram body. Skip the
2654 -- generic formal parameters that are not frozen here.
2656 if Is_Subprogram (Current_Scope)
2657 and then Nkind (Unit_Declaration_Node (Current_Scope)) =
2658 N_Generic_Subprogram_Declaration
2659 and then Present (First_Entity (Current_Scope))
2660 then
2661 while Is_Generic_Formal (Freeze_From) loop
2662 Freeze_From := Next_Entity (Freeze_From);
2663 end loop;
2665 Freeze_All (Freeze_From, Decl);
2666 Freeze_From := Last_Entity (Current_Scope);
2668 else
2669 Freeze_All (First_Entity (Current_Scope), Decl);
2670 Freeze_From := Last_Entity (Current_Scope);
2671 end if;
2673 -- Current scope is a package specification
2675 elsif Scope (Current_Scope) /= Standard_Standard
2676 and then not Is_Child_Unit (Current_Scope)
2677 and then No (Generic_Parent (Parent (L)))
2678 then
2679 -- ARM rule 13.1.1(11/3): usage names in aspect definitions are
2680 -- resolved at the end of the immediately enclosing declaration
2681 -- list (AI05-0183-1).
2683 Resolve_Aspects;
2685 elsif L /= Visible_Declarations (Parent (L))
2686 or else No (Private_Declarations (Parent (L)))
2687 or else Is_Empty_List (Private_Declarations (Parent (L)))
2688 then
2689 Adjust_Decl;
2691 -- End of a package declaration
2693 -- In compilation mode the expansion of freeze node takes care
2694 -- of resolving expressions of all aspects in the list. In ASIS
2695 -- mode this must be done explicitly.
2697 if ASIS_Mode
2698 and then Scope (Current_Scope) = Standard_Standard
2699 then
2700 Resolve_Aspects;
2701 end if;
2703 -- This is a freeze point because it is the end of a
2704 -- compilation unit.
2706 Freeze_All (First_Entity (Current_Scope), Decl);
2707 Freeze_From := Last_Entity (Current_Scope);
2709 -- At the end of the visible declarations the expressions in
2710 -- aspects of all entities declared so far must be resolved.
2711 -- The entities themselves might be frozen later, and the
2712 -- generated pragmas and attribute definition clauses analyzed
2713 -- in full at that point, but name resolution must take place
2714 -- now.
2715 -- In addition to being the proper semantics, this is mandatory
2716 -- within generic units, because global name capture requires
2717 -- those expressions to be analyzed, given that the generated
2718 -- pragmas do not appear in the original generic tree.
2720 elsif Serious_Errors_Detected = 0 then
2721 Resolve_Aspects;
2722 end if;
2724 -- If next node is a body then freeze all types before the body.
2725 -- An exception occurs for some expander-generated bodies. If these
2726 -- are generated at places where in general language rules would not
2727 -- allow a freeze point, then we assume that the expander has
2728 -- explicitly checked that all required types are properly frozen,
2729 -- and we do not cause general freezing here. This special circuit
2730 -- is used when the encountered body is marked as having already
2731 -- been analyzed.
2733 -- In all other cases (bodies that come from source, and expander
2734 -- generated bodies that have not been analyzed yet), freeze all
2735 -- types now. Note that in the latter case, the expander must take
2736 -- care to attach the bodies at a proper place in the tree so as to
2737 -- not cause unwanted freezing at that point.
2739 -- It is also necessary to check for a case where both an expression
2740 -- function is used and the current scope depends on an unseen
2741 -- private type from a library unit, otherwise premature freezing of
2742 -- the private type will occur.
2744 elsif not Analyzed (Next_Decl) and then Is_Body (Next_Decl)
2745 and then ((Nkind (Next_Decl) /= N_Subprogram_Body
2746 or else not Was_Expression_Function (Next_Decl))
2747 or else not Uses_Unseen_Lib_Unit_Priv (Current_Scope))
2748 then
2749 -- When a controlled type is frozen, the expander generates stream
2750 -- and controlled-type support routines. If the freeze is caused
2751 -- by the stand-alone body of Initialize, Adjust, or Finalize, the
2752 -- expander will end up using the wrong version of these routines,
2753 -- as the body has not been processed yet. To remedy this, detect
2754 -- a late controlled primitive and create a proper spec for it.
2755 -- This ensures that the primitive will override its inherited
2756 -- counterpart before the freeze takes place.
2758 -- If the declaration we just processed is a body, do not attempt
2759 -- to examine Next_Decl as the late primitive idiom can only apply
2760 -- to the first encountered body.
2762 -- The spec of the late primitive is not generated in ASIS mode to
2763 -- ensure a consistent list of primitives that indicates the true
2764 -- semantic structure of the program (which is not relevant when
2765 -- generating executable code).
2767 -- ??? A cleaner approach may be possible and/or this solution
2768 -- could be extended to general-purpose late primitives, TBD.
2770 if not ASIS_Mode
2771 and then not Body_Seen
2772 and then not Is_Body (Decl)
2773 then
2774 Body_Seen := True;
2776 if Nkind (Next_Decl) = N_Subprogram_Body then
2777 Handle_Late_Controlled_Primitive (Next_Decl);
2778 end if;
2779 end if;
2781 Adjust_Decl;
2783 -- The generated body of an expression function does not freeze,
2784 -- unless it is a completion, in which case only the expression
2785 -- itself freezes. This is handled when the body itself is
2786 -- analyzed (see Freeze_Expr_Types, sem_ch6.adb).
2788 Freeze_All (Freeze_From, Decl);
2789 Freeze_From := Last_Entity (Current_Scope);
2790 end if;
2792 Decl := Next_Decl;
2793 end loop;
2795 -- Post-freezing actions
2797 if Present (L) then
2798 Context := Parent (L);
2800 -- Analyze the contracts of packages and their bodies
2802 if Nkind (Context) = N_Package_Specification then
2804 -- When a package has private declarations, its contract must be
2805 -- analyzed at the end of the said declarations. This way both the
2806 -- analysis and freeze actions are properly synchronized in case
2807 -- of private type use within the contract.
2809 if L = Private_Declarations (Context) then
2810 Analyze_Package_Contract (Defining_Entity (Context));
2812 -- Otherwise the contract is analyzed at the end of the visible
2813 -- declarations.
2815 elsif L = Visible_Declarations (Context)
2816 and then No (Private_Declarations (Context))
2817 then
2818 Analyze_Package_Contract (Defining_Entity (Context));
2819 end if;
2821 elsif Nkind (Context) = N_Package_Body then
2822 Analyze_Package_Body_Contract (Defining_Entity (Context));
2823 end if;
2825 -- Analyze the contracts of various constructs now due to the delayed
2826 -- visibility needs of their aspects and pragmas.
2828 Analyze_Contracts (L);
2830 if Nkind (Context) = N_Package_Body then
2832 -- Ensure that all abstract states and objects declared in the
2833 -- state space of a package body are utilized as constituents.
2835 Check_Unused_Body_States (Defining_Entity (Context));
2837 -- State refinements are visible up to the end of the package body
2838 -- declarations. Hide the state refinements from visibility to
2839 -- restore the original state conditions.
2841 Remove_Visible_Refinements (Corresponding_Spec (Context));
2842 Remove_Partial_Visible_Refinements (Corresponding_Spec (Context));
2844 elsif Nkind (Context) = N_Package_Declaration then
2846 -- Partial state refinements are visible up to the end of the
2847 -- package spec declarations. Hide the partial state refinements
2848 -- from visibility to restore the original state conditions.
2850 Remove_Partial_Visible_Refinements (Corresponding_Spec (Context));
2851 end if;
2853 -- Verify that all abstract states found in any package declared in
2854 -- the input declarative list have proper refinements. The check is
2855 -- performed only when the context denotes a block, entry, package,
2856 -- protected, subprogram, or task body (SPARK RM 7.2.2(3)).
2858 Check_State_Refinements (Context);
2860 -- Create the subprogram bodies which verify the run-time semantics
2861 -- of pragmas Default_Initial_Condition and [Type_]Invariant for all
2862 -- types within the current declarative list. This ensures that all
2863 -- assertion expressions are preanalyzed and resolved at the end of
2864 -- the declarative part. Note that the resolution happens even when
2865 -- freezing does not take place.
2867 Build_Assertion_Bodies (L, Context);
2868 end if;
2869 end Analyze_Declarations;
2871 -----------------------------------
2872 -- Analyze_Full_Type_Declaration --
2873 -----------------------------------
2875 procedure Analyze_Full_Type_Declaration (N : Node_Id) is
2876 Def : constant Node_Id := Type_Definition (N);
2877 Def_Id : constant Entity_Id := Defining_Identifier (N);
2878 T : Entity_Id;
2879 Prev : Entity_Id;
2881 Is_Remote : constant Boolean :=
2882 (Is_Remote_Types (Current_Scope)
2883 or else Is_Remote_Call_Interface (Current_Scope))
2884 and then not (In_Private_Part (Current_Scope)
2885 or else In_Package_Body (Current_Scope));
2887 procedure Check_Nonoverridable_Aspects;
2888 -- Apply the rule in RM 13.1.1(18.4/4) on iterator aspects that cannot
2889 -- be overridden, and can only be confirmed on derivation.
2891 procedure Check_Ops_From_Incomplete_Type;
2892 -- If there is a tagged incomplete partial view of the type, traverse
2893 -- the primitives of the incomplete view and change the type of any
2894 -- controlling formals and result to indicate the full view. The
2895 -- primitives will be added to the full type's primitive operations
2896 -- list later in Sem_Disp.Check_Operation_From_Incomplete_Type (which
2897 -- is called from Process_Incomplete_Dependents).
2899 ----------------------------------
2900 -- Check_Nonoverridable_Aspects --
2901 ----------------------------------
2903 procedure Check_Nonoverridable_Aspects is
2904 function Get_Aspect_Spec
2905 (Specs : List_Id;
2906 Aspect_Name : Name_Id) return Node_Id;
2907 -- Check whether a list of aspect specifications includes an entry
2908 -- for a specific aspect. The list is either that of a partial or
2909 -- a full view.
2911 ---------------------
2912 -- Get_Aspect_Spec --
2913 ---------------------
2915 function Get_Aspect_Spec
2916 (Specs : List_Id;
2917 Aspect_Name : Name_Id) return Node_Id
2919 Spec : Node_Id;
2921 begin
2922 Spec := First (Specs);
2923 while Present (Spec) loop
2924 if Chars (Identifier (Spec)) = Aspect_Name then
2925 return Spec;
2926 end if;
2927 Next (Spec);
2928 end loop;
2930 return Empty;
2931 end Get_Aspect_Spec;
2933 -- Local variables
2935 Prev_Aspects : constant List_Id :=
2936 Aspect_Specifications (Parent (Def_Id));
2937 Par_Type : Entity_Id;
2938 Prev_Aspect : Node_Id;
2940 -- Start of processing for Check_Nonoverridable_Aspects
2942 begin
2943 -- Get parent type of derived type. Note that Prev is the entity in
2944 -- the partial declaration, but its contents are now those of full
2945 -- view, while Def_Id reflects the partial view.
2947 if Is_Private_Type (Def_Id) then
2948 Par_Type := Etype (Full_View (Def_Id));
2949 else
2950 Par_Type := Etype (Def_Id);
2951 end if;
2953 -- If there is an inherited Implicit_Dereference, verify that it is
2954 -- made explicit in the partial view.
2956 if Has_Discriminants (Base_Type (Par_Type))
2957 and then Nkind (Parent (Prev)) = N_Full_Type_Declaration
2958 and then Present (Discriminant_Specifications (Parent (Prev)))
2959 and then Present (Get_Reference_Discriminant (Par_Type))
2960 then
2961 Prev_Aspect :=
2962 Get_Aspect_Spec (Prev_Aspects, Name_Implicit_Dereference);
2964 if No (Prev_Aspect)
2965 and then Present
2966 (Discriminant_Specifications
2967 (Original_Node (Parent (Prev))))
2968 then
2969 Error_Msg_N
2970 ("type does not inherit implicit dereference", Prev);
2972 else
2973 -- If one of the views has the aspect specified, verify that it
2974 -- is consistent with that of the parent.
2976 declare
2977 Par_Discr : constant Entity_Id :=
2978 Get_Reference_Discriminant (Par_Type);
2979 Cur_Discr : constant Entity_Id :=
2980 Get_Reference_Discriminant (Prev);
2982 begin
2983 if Corresponding_Discriminant (Cur_Discr) /= Par_Discr then
2984 Error_Msg_N ("aspect incosistent with that of parent", N);
2985 end if;
2987 -- Check that specification in partial view matches the
2988 -- inherited aspect. Compare names directly because aspect
2989 -- expression may not be analyzed.
2991 if Present (Prev_Aspect)
2992 and then Nkind (Expression (Prev_Aspect)) = N_Identifier
2993 and then Chars (Expression (Prev_Aspect)) /=
2994 Chars (Cur_Discr)
2995 then
2996 Error_Msg_N
2997 ("aspect incosistent with that of parent", N);
2998 end if;
2999 end;
3000 end if;
3001 end if;
3003 -- TBD : other nonoverridable aspects.
3004 end Check_Nonoverridable_Aspects;
3006 ------------------------------------
3007 -- Check_Ops_From_Incomplete_Type --
3008 ------------------------------------
3010 procedure Check_Ops_From_Incomplete_Type is
3011 Elmt : Elmt_Id;
3012 Formal : Entity_Id;
3013 Op : Entity_Id;
3015 begin
3016 if Prev /= T
3017 and then Ekind (Prev) = E_Incomplete_Type
3018 and then Is_Tagged_Type (Prev)
3019 and then Is_Tagged_Type (T)
3020 then
3021 Elmt := First_Elmt (Primitive_Operations (Prev));
3022 while Present (Elmt) loop
3023 Op := Node (Elmt);
3025 Formal := First_Formal (Op);
3026 while Present (Formal) loop
3027 if Etype (Formal) = Prev then
3028 Set_Etype (Formal, T);
3029 end if;
3031 Next_Formal (Formal);
3032 end loop;
3034 if Etype (Op) = Prev then
3035 Set_Etype (Op, T);
3036 end if;
3038 Next_Elmt (Elmt);
3039 end loop;
3040 end if;
3041 end Check_Ops_From_Incomplete_Type;
3043 -- Start of processing for Analyze_Full_Type_Declaration
3045 begin
3046 Prev := Find_Type_Name (N);
3048 -- The full view, if present, now points to the current type. If there
3049 -- is an incomplete partial view, set a link to it, to simplify the
3050 -- retrieval of primitive operations of the type.
3052 -- Ada 2005 (AI-50217): If the type was previously decorated when
3053 -- imported through a LIMITED WITH clause, it appears as incomplete
3054 -- but has no full view.
3056 if Ekind (Prev) = E_Incomplete_Type
3057 and then Present (Full_View (Prev))
3058 then
3059 T := Full_View (Prev);
3060 Set_Incomplete_View (N, Parent (Prev));
3061 else
3062 T := Prev;
3063 end if;
3065 Set_Is_Pure (T, Is_Pure (Current_Scope));
3067 -- We set the flag Is_First_Subtype here. It is needed to set the
3068 -- corresponding flag for the Implicit class-wide-type created
3069 -- during tagged types processing.
3071 Set_Is_First_Subtype (T, True);
3073 -- Only composite types other than array types are allowed to have
3074 -- discriminants.
3076 case Nkind (Def) is
3078 -- For derived types, the rule will be checked once we've figured
3079 -- out the parent type.
3081 when N_Derived_Type_Definition =>
3082 null;
3084 -- For record types, discriminants are allowed, unless we are in
3085 -- SPARK.
3087 when N_Record_Definition =>
3088 if Present (Discriminant_Specifications (N)) then
3089 Check_SPARK_05_Restriction
3090 ("discriminant type is not allowed",
3091 Defining_Identifier
3092 (First (Discriminant_Specifications (N))));
3093 end if;
3095 when others =>
3096 if Present (Discriminant_Specifications (N)) then
3097 Error_Msg_N
3098 ("elementary or array type cannot have discriminants",
3099 Defining_Identifier
3100 (First (Discriminant_Specifications (N))));
3101 end if;
3102 end case;
3104 -- Elaborate the type definition according to kind, and generate
3105 -- subsidiary (implicit) subtypes where needed. We skip this if it was
3106 -- already done (this happens during the reanalysis that follows a call
3107 -- to the high level optimizer).
3109 if not Analyzed (T) then
3110 Set_Analyzed (T);
3112 case Nkind (Def) is
3113 when N_Access_To_Subprogram_Definition =>
3114 Access_Subprogram_Declaration (T, Def);
3116 -- If this is a remote access to subprogram, we must create the
3117 -- equivalent fat pointer type, and related subprograms.
3119 if Is_Remote then
3120 Process_Remote_AST_Declaration (N);
3121 end if;
3123 -- Validate categorization rule against access type declaration
3124 -- usually a violation in Pure unit, Shared_Passive unit.
3126 Validate_Access_Type_Declaration (T, N);
3128 when N_Access_To_Object_Definition =>
3129 Access_Type_Declaration (T, Def);
3131 -- Validate categorization rule against access type declaration
3132 -- usually a violation in Pure unit, Shared_Passive unit.
3134 Validate_Access_Type_Declaration (T, N);
3136 -- If we are in a Remote_Call_Interface package and define a
3137 -- RACW, then calling stubs and specific stream attributes
3138 -- must be added.
3140 if Is_Remote
3141 and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
3142 then
3143 Add_RACW_Features (Def_Id);
3144 end if;
3146 when N_Array_Type_Definition =>
3147 Array_Type_Declaration (T, Def);
3149 when N_Derived_Type_Definition =>
3150 Derived_Type_Declaration (T, N, T /= Def_Id);
3152 -- Inherit predicates from parent, and protect against illegal
3153 -- derivations.
3155 if Is_Type (T) and then Has_Predicates (T) then
3156 Set_Has_Predicates (Def_Id);
3157 end if;
3159 when N_Enumeration_Type_Definition =>
3160 Enumeration_Type_Declaration (T, Def);
3162 when N_Floating_Point_Definition =>
3163 Floating_Point_Type_Declaration (T, Def);
3165 when N_Decimal_Fixed_Point_Definition =>
3166 Decimal_Fixed_Point_Type_Declaration (T, Def);
3168 when N_Ordinary_Fixed_Point_Definition =>
3169 Ordinary_Fixed_Point_Type_Declaration (T, Def);
3171 when N_Signed_Integer_Type_Definition =>
3172 Signed_Integer_Type_Declaration (T, Def);
3174 when N_Modular_Type_Definition =>
3175 Modular_Type_Declaration (T, Def);
3177 when N_Record_Definition =>
3178 Record_Type_Declaration (T, N, Prev);
3180 -- If declaration has a parse error, nothing to elaborate.
3182 when N_Error =>
3183 null;
3185 when others =>
3186 raise Program_Error;
3187 end case;
3188 end if;
3190 if Etype (T) = Any_Type then
3191 return;
3192 end if;
3194 -- Controlled type is not allowed in SPARK
3196 if Is_Visibly_Controlled (T) then
3197 Check_SPARK_05_Restriction ("controlled type is not allowed", N);
3198 end if;
3200 -- Some common processing for all types
3202 Set_Depends_On_Private (T, Has_Private_Component (T));
3203 Check_Ops_From_Incomplete_Type;
3205 -- Both the declared entity, and its anonymous base type if one was
3206 -- created, need freeze nodes allocated.
3208 declare
3209 B : constant Entity_Id := Base_Type (T);
3211 begin
3212 -- In the case where the base type differs from the first subtype, we
3213 -- pre-allocate a freeze node, and set the proper link to the first
3214 -- subtype. Freeze_Entity will use this preallocated freeze node when
3215 -- it freezes the entity.
3217 -- This does not apply if the base type is a generic type, whose
3218 -- declaration is independent of the current derived definition.
3220 if B /= T and then not Is_Generic_Type (B) then
3221 Ensure_Freeze_Node (B);
3222 Set_First_Subtype_Link (Freeze_Node (B), T);
3223 end if;
3225 -- A type that is imported through a limited_with clause cannot
3226 -- generate any code, and thus need not be frozen. However, an access
3227 -- type with an imported designated type needs a finalization list,
3228 -- which may be referenced in some other package that has non-limited
3229 -- visibility on the designated type. Thus we must create the
3230 -- finalization list at the point the access type is frozen, to
3231 -- prevent unsatisfied references at link time.
3233 if not From_Limited_With (T) or else Is_Access_Type (T) then
3234 Set_Has_Delayed_Freeze (T);
3235 end if;
3236 end;
3238 -- Case where T is the full declaration of some private type which has
3239 -- been swapped in Defining_Identifier (N).
3241 if T /= Def_Id and then Is_Private_Type (Def_Id) then
3242 Process_Full_View (N, T, Def_Id);
3244 -- Record the reference. The form of this is a little strange, since
3245 -- the full declaration has been swapped in. So the first parameter
3246 -- here represents the entity to which a reference is made which is
3247 -- the "real" entity, i.e. the one swapped in, and the second
3248 -- parameter provides the reference location.
3250 -- Also, we want to kill Has_Pragma_Unreferenced temporarily here
3251 -- since we don't want a complaint about the full type being an
3252 -- unwanted reference to the private type
3254 declare
3255 B : constant Boolean := Has_Pragma_Unreferenced (T);
3256 begin
3257 Set_Has_Pragma_Unreferenced (T, False);
3258 Generate_Reference (T, T, 'c');
3259 Set_Has_Pragma_Unreferenced (T, B);
3260 end;
3262 Set_Completion_Referenced (Def_Id);
3264 -- For completion of incomplete type, process incomplete dependents
3265 -- and always mark the full type as referenced (it is the incomplete
3266 -- type that we get for any real reference).
3268 elsif Ekind (Prev) = E_Incomplete_Type then
3269 Process_Incomplete_Dependents (N, T, Prev);
3270 Generate_Reference (Prev, Def_Id, 'c');
3271 Set_Completion_Referenced (Def_Id);
3273 -- If not private type or incomplete type completion, this is a real
3274 -- definition of a new entity, so record it.
3276 else
3277 Generate_Definition (Def_Id);
3278 end if;
3280 -- Propagate any pending access types whose finalization masters need to
3281 -- be fully initialized from the partial to the full view. Guard against
3282 -- an illegal full view that remains unanalyzed.
3284 if Is_Type (Def_Id) and then Is_Incomplete_Or_Private_Type (Prev) then
3285 Set_Pending_Access_Types (Def_Id, Pending_Access_Types (Prev));
3286 end if;
3288 if Chars (Scope (Def_Id)) = Name_System
3289 and then Chars (Def_Id) = Name_Address
3290 and then In_Predefined_Unit (N)
3291 then
3292 Set_Is_Descendant_Of_Address (Def_Id);
3293 Set_Is_Descendant_Of_Address (Base_Type (Def_Id));
3294 Set_Is_Descendant_Of_Address (Prev);
3295 end if;
3297 Set_Optimize_Alignment_Flags (Def_Id);
3298 Check_Eliminated (Def_Id);
3300 -- If the declaration is a completion and aspects are present, apply
3301 -- them to the entity for the type which is currently the partial
3302 -- view, but which is the one that will be frozen.
3304 if Has_Aspects (N) then
3306 -- In most cases the partial view is a private type, and both views
3307 -- appear in different declarative parts. In the unusual case where
3308 -- the partial view is incomplete, perform the analysis on the
3309 -- full view, to prevent freezing anomalies with the corresponding
3310 -- class-wide type, which otherwise might be frozen before the
3311 -- dispatch table is built.
3313 if Prev /= Def_Id
3314 and then Ekind (Prev) /= E_Incomplete_Type
3315 then
3316 Analyze_Aspect_Specifications (N, Prev);
3318 -- Normal case
3320 else
3321 Analyze_Aspect_Specifications (N, Def_Id);
3322 end if;
3323 end if;
3325 if Is_Derived_Type (Prev)
3326 and then Def_Id /= Prev
3327 then
3328 Check_Nonoverridable_Aspects;
3329 end if;
3330 end Analyze_Full_Type_Declaration;
3332 ----------------------------------
3333 -- Analyze_Incomplete_Type_Decl --
3334 ----------------------------------
3336 procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
3337 F : constant Boolean := Is_Pure (Current_Scope);
3338 T : Entity_Id;
3340 begin
3341 Check_SPARK_05_Restriction ("incomplete type is not allowed", N);
3343 Generate_Definition (Defining_Identifier (N));
3345 -- Process an incomplete declaration. The identifier must not have been
3346 -- declared already in the scope. However, an incomplete declaration may
3347 -- appear in the private part of a package, for a private type that has
3348 -- already been declared.
3350 -- In this case, the discriminants (if any) must match
3352 T := Find_Type_Name (N);
3354 Set_Ekind (T, E_Incomplete_Type);
3355 Init_Size_Align (T);
3356 Set_Is_First_Subtype (T, True);
3357 Set_Etype (T, T);
3359 -- Ada 2005 (AI-326): Minimum decoration to give support to tagged
3360 -- incomplete types.
3362 if Tagged_Present (N) then
3363 Set_Is_Tagged_Type (T, True);
3364 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
3365 Make_Class_Wide_Type (T);
3366 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3367 end if;
3369 Set_Stored_Constraint (T, No_Elist);
3371 if Present (Discriminant_Specifications (N)) then
3372 Push_Scope (T);
3373 Process_Discriminants (N);
3374 End_Scope;
3375 end if;
3377 -- If the type has discriminants, nontrivial subtypes may be declared
3378 -- before the full view of the type. The full views of those subtypes
3379 -- will be built after the full view of the type.
3381 Set_Private_Dependents (T, New_Elmt_List);
3382 Set_Is_Pure (T, F);
3383 end Analyze_Incomplete_Type_Decl;
3385 -----------------------------------
3386 -- Analyze_Interface_Declaration --
3387 -----------------------------------
3389 procedure Analyze_Interface_Declaration (T : Entity_Id; Def : Node_Id) is
3390 CW : constant Entity_Id := Class_Wide_Type (T);
3392 begin
3393 Set_Is_Tagged_Type (T);
3394 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
3396 Set_Is_Limited_Record (T, Limited_Present (Def)
3397 or else Task_Present (Def)
3398 or else Protected_Present (Def)
3399 or else Synchronized_Present (Def));
3401 -- Type is abstract if full declaration carries keyword, or if previous
3402 -- partial view did.
3404 Set_Is_Abstract_Type (T);
3405 Set_Is_Interface (T);
3407 -- Type is a limited interface if it includes the keyword limited, task,
3408 -- protected, or synchronized.
3410 Set_Is_Limited_Interface
3411 (T, Limited_Present (Def)
3412 or else Protected_Present (Def)
3413 or else Synchronized_Present (Def)
3414 or else Task_Present (Def));
3416 Set_Interfaces (T, New_Elmt_List);
3417 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3419 -- Complete the decoration of the class-wide entity if it was already
3420 -- built (i.e. during the creation of the limited view)
3422 if Present (CW) then
3423 Set_Is_Interface (CW);
3424 Set_Is_Limited_Interface (CW, Is_Limited_Interface (T));
3425 end if;
3427 -- Check runtime support for synchronized interfaces
3429 if (Is_Task_Interface (T)
3430 or else Is_Protected_Interface (T)
3431 or else Is_Synchronized_Interface (T))
3432 and then not RTE_Available (RE_Select_Specific_Data)
3433 then
3434 Error_Msg_CRT ("synchronized interfaces", T);
3435 end if;
3436 end Analyze_Interface_Declaration;
3438 -----------------------------
3439 -- Analyze_Itype_Reference --
3440 -----------------------------
3442 -- Nothing to do. This node is placed in the tree only for the benefit of
3443 -- back end processing, and has no effect on the semantic processing.
3445 procedure Analyze_Itype_Reference (N : Node_Id) is
3446 begin
3447 pragma Assert (Is_Itype (Itype (N)));
3448 null;
3449 end Analyze_Itype_Reference;
3451 --------------------------------
3452 -- Analyze_Number_Declaration --
3453 --------------------------------
3455 procedure Analyze_Number_Declaration (N : Node_Id) is
3456 E : constant Node_Id := Expression (N);
3457 Id : constant Entity_Id := Defining_Identifier (N);
3458 Index : Interp_Index;
3459 It : Interp;
3460 T : Entity_Id;
3462 begin
3463 Generate_Definition (Id);
3464 Enter_Name (Id);
3466 -- This is an optimization of a common case of an integer literal
3468 if Nkind (E) = N_Integer_Literal then
3469 Set_Is_Static_Expression (E, True);
3470 Set_Etype (E, Universal_Integer);
3472 Set_Etype (Id, Universal_Integer);
3473 Set_Ekind (Id, E_Named_Integer);
3474 Set_Is_Frozen (Id, True);
3475 return;
3476 end if;
3478 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3480 -- Process expression, replacing error by integer zero, to avoid
3481 -- cascaded errors or aborts further along in the processing
3483 -- Replace Error by integer zero, which seems least likely to cause
3484 -- cascaded errors.
3486 if E = Error then
3487 Rewrite (E, Make_Integer_Literal (Sloc (E), Uint_0));
3488 Set_Error_Posted (E);
3489 end if;
3491 Analyze (E);
3493 -- Verify that the expression is static and numeric. If
3494 -- the expression is overloaded, we apply the preference
3495 -- rule that favors root numeric types.
3497 if not Is_Overloaded (E) then
3498 T := Etype (E);
3499 if Has_Dynamic_Predicate_Aspect (T) then
3500 Error_Msg_N
3501 ("subtype has dynamic predicate, "
3502 & "not allowed in number declaration", N);
3503 end if;
3505 else
3506 T := Any_Type;
3508 Get_First_Interp (E, Index, It);
3509 while Present (It.Typ) loop
3510 if (Is_Integer_Type (It.Typ) or else Is_Real_Type (It.Typ))
3511 and then (Scope (Base_Type (It.Typ))) = Standard_Standard
3512 then
3513 if T = Any_Type then
3514 T := It.Typ;
3516 elsif It.Typ = Universal_Real
3517 or else
3518 It.Typ = Universal_Integer
3519 then
3520 -- Choose universal interpretation over any other
3522 T := It.Typ;
3523 exit;
3524 end if;
3525 end if;
3527 Get_Next_Interp (Index, It);
3528 end loop;
3529 end if;
3531 if Is_Integer_Type (T) then
3532 Resolve (E, T);
3533 Set_Etype (Id, Universal_Integer);
3534 Set_Ekind (Id, E_Named_Integer);
3536 elsif Is_Real_Type (T) then
3538 -- Because the real value is converted to universal_real, this is a
3539 -- legal context for a universal fixed expression.
3541 if T = Universal_Fixed then
3542 declare
3543 Loc : constant Source_Ptr := Sloc (N);
3544 Conv : constant Node_Id := Make_Type_Conversion (Loc,
3545 Subtype_Mark =>
3546 New_Occurrence_Of (Universal_Real, Loc),
3547 Expression => Relocate_Node (E));
3549 begin
3550 Rewrite (E, Conv);
3551 Analyze (E);
3552 end;
3554 elsif T = Any_Fixed then
3555 Error_Msg_N ("illegal context for mixed mode operation", E);
3557 -- Expression is of the form : universal_fixed * integer. Try to
3558 -- resolve as universal_real.
3560 T := Universal_Real;
3561 Set_Etype (E, T);
3562 end if;
3564 Resolve (E, T);
3565 Set_Etype (Id, Universal_Real);
3566 Set_Ekind (Id, E_Named_Real);
3568 else
3569 Wrong_Type (E, Any_Numeric);
3570 Resolve (E, T);
3572 Set_Etype (Id, T);
3573 Set_Ekind (Id, E_Constant);
3574 Set_Never_Set_In_Source (Id, True);
3575 Set_Is_True_Constant (Id, True);
3576 return;
3577 end if;
3579 if Nkind_In (E, N_Integer_Literal, N_Real_Literal) then
3580 Set_Etype (E, Etype (Id));
3581 end if;
3583 if not Is_OK_Static_Expression (E) then
3584 Flag_Non_Static_Expr
3585 ("non-static expression used in number declaration!", E);
3586 Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
3587 Set_Etype (E, Any_Type);
3588 end if;
3590 Analyze_Dimension (N);
3591 end Analyze_Number_Declaration;
3593 --------------------------------
3594 -- Analyze_Object_Declaration --
3595 --------------------------------
3597 -- WARNING: This routine manages Ghost regions. Return statements must be
3598 -- replaced by gotos which jump to the end of the routine and restore the
3599 -- Ghost mode.
3601 procedure Analyze_Object_Declaration (N : Node_Id) is
3602 Loc : constant Source_Ptr := Sloc (N);
3603 Id : constant Entity_Id := Defining_Identifier (N);
3604 Act_T : Entity_Id;
3605 T : Entity_Id;
3607 E : Node_Id := Expression (N);
3608 -- E is set to Expression (N) throughout this routine. When Expression
3609 -- (N) is modified, E is changed accordingly.
3611 Prev_Entity : Entity_Id := Empty;
3613 procedure Check_Dynamic_Object (Typ : Entity_Id);
3614 -- A library-level object with non-static discriminant constraints may
3615 -- require dynamic allocation. The declaration is illegal if the
3616 -- profile includes the restriction No_Implicit_Heap_Allocations.
3618 procedure Check_For_Null_Excluding_Components
3619 (Obj_Typ : Entity_Id;
3620 Obj_Decl : Node_Id);
3621 -- Verify that each null-excluding component of object declaration
3622 -- Obj_Decl carrying type Obj_Typ has explicit initialization. Emit
3623 -- a compile-time warning if this is not the case.
3625 function Count_Tasks (T : Entity_Id) return Uint;
3626 -- This function is called when a non-generic library level object of a
3627 -- task type is declared. Its function is to count the static number of
3628 -- tasks declared within the type (it is only called if Has_Task is set
3629 -- for T). As a side effect, if an array of tasks with non-static bounds
3630 -- or a variant record type is encountered, Check_Restriction is called
3631 -- indicating the count is unknown.
3633 function Delayed_Aspect_Present return Boolean;
3634 -- If the declaration has an expression that is an aggregate, and it
3635 -- has aspects that require delayed analysis, the resolution of the
3636 -- aggregate must be deferred to the freeze point of the objet. This
3637 -- special processing was created for address clauses, but it must
3638 -- also apply to Alignment. This must be done before the aspect
3639 -- specifications are analyzed because we must handle the aggregate
3640 -- before the analysis of the object declaration is complete.
3642 -- Any other relevant delayed aspects on object declarations ???
3644 --------------------------
3645 -- Check_Dynamic_Object --
3646 --------------------------
3648 procedure Check_Dynamic_Object (Typ : Entity_Id) is
3649 Comp : Entity_Id;
3650 Obj_Type : Entity_Id;
3652 begin
3653 Obj_Type := Typ;
3655 if Is_Private_Type (Obj_Type)
3656 and then Present (Full_View (Obj_Type))
3657 then
3658 Obj_Type := Full_View (Obj_Type);
3659 end if;
3661 if Known_Static_Esize (Obj_Type) then
3662 return;
3663 end if;
3665 if Restriction_Active (No_Implicit_Heap_Allocations)
3666 and then Expander_Active
3667 and then Has_Discriminants (Obj_Type)
3668 then
3669 Comp := First_Component (Obj_Type);
3670 while Present (Comp) loop
3671 if Known_Static_Esize (Etype (Comp))
3672 or else Size_Known_At_Compile_Time (Etype (Comp))
3673 then
3674 null;
3676 elsif not Discriminated_Size (Comp)
3677 and then Comes_From_Source (Comp)
3678 then
3679 Error_Msg_NE
3680 ("component& of non-static size will violate restriction "
3681 & "No_Implicit_Heap_Allocation?", N, Comp);
3683 elsif Is_Record_Type (Etype (Comp)) then
3684 Check_Dynamic_Object (Etype (Comp));
3685 end if;
3687 Next_Component (Comp);
3688 end loop;
3689 end if;
3690 end Check_Dynamic_Object;
3692 -----------------------------------------
3693 -- Check_For_Null_Excluding_Components --
3694 -----------------------------------------
3696 procedure Check_For_Null_Excluding_Components
3697 (Obj_Typ : Entity_Id;
3698 Obj_Decl : Node_Id)
3700 procedure Check_Component
3701 (Comp_Typ : Entity_Id;
3702 Comp_Decl : Node_Id := Empty;
3703 Array_Comp : Boolean := False);
3704 -- Apply a compile-time null-exclusion check on a component denoted
3705 -- by its declaration Comp_Decl and type Comp_Typ, and all of its
3706 -- subcomponents (if any).
3708 ---------------------
3709 -- Check_Component --
3710 ---------------------
3712 procedure Check_Component
3713 (Comp_Typ : Entity_Id;
3714 Comp_Decl : Node_Id := Empty;
3715 Array_Comp : Boolean := False)
3717 Comp : Entity_Id;
3718 T : Entity_Id;
3720 begin
3721 -- Do not consider internally-generated components or those that
3722 -- are already initialized.
3724 if Present (Comp_Decl)
3725 and then (not Comes_From_Source (Comp_Decl)
3726 or else Present (Expression (Comp_Decl)))
3727 then
3728 return;
3729 end if;
3731 if Is_Incomplete_Or_Private_Type (Comp_Typ)
3732 and then Present (Full_View (Comp_Typ))
3733 then
3734 T := Full_View (Comp_Typ);
3735 else
3736 T := Comp_Typ;
3737 end if;
3739 -- Verify a component of a null-excluding access type
3741 if Is_Access_Type (T)
3742 and then Can_Never_Be_Null (T)
3743 then
3744 if Comp_Decl = Obj_Decl then
3745 Null_Exclusion_Static_Checks
3746 (N => Obj_Decl,
3747 Comp => Empty,
3748 Array_Comp => Array_Comp);
3750 else
3751 Null_Exclusion_Static_Checks
3752 (N => Obj_Decl,
3753 Comp => Comp_Decl,
3754 Array_Comp => Array_Comp);
3755 end if;
3757 -- Check array components
3759 elsif Is_Array_Type (T) then
3761 -- There is no suitable component when the object is of an
3762 -- array type. However, a namable component may appear at some
3763 -- point during the recursive inspection, but not at the top
3764 -- level. At the top level just indicate array component case.
3766 if Comp_Decl = Obj_Decl then
3767 Check_Component (Component_Type (T), Array_Comp => True);
3768 else
3769 Check_Component (Component_Type (T), Comp_Decl);
3770 end if;
3772 -- Verify all components of type T
3774 -- Note: No checks are performed on types with discriminants due
3775 -- to complexities involving variants. ???
3777 elsif (Is_Concurrent_Type (T)
3778 or else Is_Incomplete_Or_Private_Type (T)
3779 or else Is_Record_Type (T))
3780 and then not Has_Discriminants (T)
3781 then
3782 Comp := First_Component (T);
3783 while Present (Comp) loop
3784 Check_Component (Etype (Comp), Parent (Comp));
3786 Comp := Next_Component (Comp);
3787 end loop;
3788 end if;
3789 end Check_Component;
3791 -- Start processing for Check_For_Null_Excluding_Components
3793 begin
3794 Check_Component (Obj_Typ, Obj_Decl);
3795 end Check_For_Null_Excluding_Components;
3797 -----------------
3798 -- Count_Tasks --
3799 -----------------
3801 function Count_Tasks (T : Entity_Id) return Uint is
3802 C : Entity_Id;
3803 X : Node_Id;
3804 V : Uint;
3806 begin
3807 if Is_Task_Type (T) then
3808 return Uint_1;
3810 elsif Is_Record_Type (T) then
3811 if Has_Discriminants (T) then
3812 Check_Restriction (Max_Tasks, N);
3813 return Uint_0;
3815 else
3816 V := Uint_0;
3817 C := First_Component (T);
3818 while Present (C) loop
3819 V := V + Count_Tasks (Etype (C));
3820 Next_Component (C);
3821 end loop;
3823 return V;
3824 end if;
3826 elsif Is_Array_Type (T) then
3827 X := First_Index (T);
3828 V := Count_Tasks (Component_Type (T));
3829 while Present (X) loop
3830 C := Etype (X);
3832 if not Is_OK_Static_Subtype (C) then
3833 Check_Restriction (Max_Tasks, N);
3834 return Uint_0;
3835 else
3836 V := V * (UI_Max (Uint_0,
3837 Expr_Value (Type_High_Bound (C)) -
3838 Expr_Value (Type_Low_Bound (C)) + Uint_1));
3839 end if;
3841 Next_Index (X);
3842 end loop;
3844 return V;
3846 else
3847 return Uint_0;
3848 end if;
3849 end Count_Tasks;
3851 ----------------------------
3852 -- Delayed_Aspect_Present --
3853 ----------------------------
3855 function Delayed_Aspect_Present return Boolean is
3856 A : Node_Id;
3857 A_Id : Aspect_Id;
3859 begin
3860 if Present (Aspect_Specifications (N)) then
3861 A := First (Aspect_Specifications (N));
3862 A_Id := Get_Aspect_Id (Chars (Identifier (A)));
3863 while Present (A) loop
3864 if A_Id = Aspect_Alignment or else A_Id = Aspect_Address then
3865 return True;
3866 end if;
3868 Next (A);
3869 end loop;
3870 end if;
3872 return False;
3873 end Delayed_Aspect_Present;
3875 -- Local variables
3877 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
3878 -- Save the Ghost mode to restore on exit
3880 Related_Id : Entity_Id;
3882 -- Start of processing for Analyze_Object_Declaration
3884 begin
3885 -- There are three kinds of implicit types generated by an
3886 -- object declaration:
3888 -- 1. Those generated by the original Object Definition
3890 -- 2. Those generated by the Expression
3892 -- 3. Those used to constrain the Object Definition with the
3893 -- expression constraints when the definition is unconstrained.
3895 -- They must be generated in this order to avoid order of elaboration
3896 -- issues. Thus the first step (after entering the name) is to analyze
3897 -- the object definition.
3899 if Constant_Present (N) then
3900 Prev_Entity := Current_Entity_In_Scope (Id);
3902 if Present (Prev_Entity)
3903 and then
3904 -- If the homograph is an implicit subprogram, it is overridden
3905 -- by the current declaration.
3907 ((Is_Overloadable (Prev_Entity)
3908 and then Is_Inherited_Operation (Prev_Entity))
3910 -- The current object is a discriminal generated for an entry
3911 -- family index. Even though the index is a constant, in this
3912 -- particular context there is no true constant redeclaration.
3913 -- Enter_Name will handle the visibility.
3915 or else
3916 (Is_Discriminal (Id)
3917 and then Ekind (Discriminal_Link (Id)) =
3918 E_Entry_Index_Parameter)
3920 -- The current object is the renaming for a generic declared
3921 -- within the instance.
3923 or else
3924 (Ekind (Prev_Entity) = E_Package
3925 and then Nkind (Parent (Prev_Entity)) =
3926 N_Package_Renaming_Declaration
3927 and then not Comes_From_Source (Prev_Entity)
3928 and then
3929 Is_Generic_Instance (Renamed_Entity (Prev_Entity)))
3931 -- The entity may be a homonym of a private component of the
3932 -- enclosing protected object, for which we create a local
3933 -- renaming declaration. The declaration is legal, even if
3934 -- useless when it just captures that component.
3936 or else
3937 (Ekind (Scope (Current_Scope)) = E_Protected_Type
3938 and then Nkind (Parent (Prev_Entity)) =
3939 N_Object_Renaming_Declaration))
3940 then
3941 Prev_Entity := Empty;
3942 end if;
3943 end if;
3945 if Present (Prev_Entity) then
3947 -- The object declaration is Ghost when it completes a deferred Ghost
3948 -- constant.
3950 Mark_And_Set_Ghost_Completion (N, Prev_Entity);
3952 Constant_Redeclaration (Id, N, T);
3954 Generate_Reference (Prev_Entity, Id, 'c');
3955 Set_Completion_Referenced (Id);
3957 if Error_Posted (N) then
3959 -- Type mismatch or illegal redeclaration; do not analyze
3960 -- expression to avoid cascaded errors.
3962 T := Find_Type_Of_Object (Object_Definition (N), N);
3963 Set_Etype (Id, T);
3964 Set_Ekind (Id, E_Variable);
3965 goto Leave;
3966 end if;
3968 -- In the normal case, enter identifier at the start to catch premature
3969 -- usage in the initialization expression.
3971 else
3972 Generate_Definition (Id);
3973 Enter_Name (Id);
3975 Mark_Coextensions (N, Object_Definition (N));
3977 T := Find_Type_Of_Object (Object_Definition (N), N);
3979 if Nkind (Object_Definition (N)) = N_Access_Definition
3980 and then Present
3981 (Access_To_Subprogram_Definition (Object_Definition (N)))
3982 and then Protected_Present
3983 (Access_To_Subprogram_Definition (Object_Definition (N)))
3984 then
3985 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
3986 end if;
3988 if Error_Posted (Id) then
3989 Set_Etype (Id, T);
3990 Set_Ekind (Id, E_Variable);
3991 goto Leave;
3992 end if;
3993 end if;
3995 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
3996 -- out some static checks.
3998 if Ada_Version >= Ada_2005 then
4000 -- In case of aggregates we must also take care of the correct
4001 -- initialization of nested aggregates bug this is done at the
4002 -- point of the analysis of the aggregate (see sem_aggr.adb) ???
4004 if Can_Never_Be_Null (T) then
4005 if Present (Expression (N))
4006 and then Nkind (Expression (N)) = N_Aggregate
4007 then
4008 null;
4010 else
4011 declare
4012 Save_Typ : constant Entity_Id := Etype (Id);
4013 begin
4014 Set_Etype (Id, T); -- Temp. decoration for static checks
4015 Null_Exclusion_Static_Checks (N);
4016 Set_Etype (Id, Save_Typ);
4017 end;
4018 end if;
4020 -- We might be dealing with an object of a composite type containing
4021 -- null-excluding components without an aggregate, so we must verify
4022 -- that such components have default initialization.
4024 else
4025 Check_For_Null_Excluding_Components (T, N);
4026 end if;
4027 end if;
4029 -- Object is marked pure if it is in a pure scope
4031 Set_Is_Pure (Id, Is_Pure (Current_Scope));
4033 -- If deferred constant, make sure context is appropriate. We detect
4034 -- a deferred constant as a constant declaration with no expression.
4035 -- A deferred constant can appear in a package body if its completion
4036 -- is by means of an interface pragma.
4038 if Constant_Present (N) and then No (E) then
4040 -- A deferred constant may appear in the declarative part of the
4041 -- following constructs:
4043 -- blocks
4044 -- entry bodies
4045 -- extended return statements
4046 -- package specs
4047 -- package bodies
4048 -- subprogram bodies
4049 -- task bodies
4051 -- When declared inside a package spec, a deferred constant must be
4052 -- completed by a full constant declaration or pragma Import. In all
4053 -- other cases, the only proper completion is pragma Import. Extended
4054 -- return statements are flagged as invalid contexts because they do
4055 -- not have a declarative part and so cannot accommodate the pragma.
4057 if Ekind (Current_Scope) = E_Return_Statement then
4058 Error_Msg_N
4059 ("invalid context for deferred constant declaration (RM 7.4)",
4061 Error_Msg_N
4062 ("\declaration requires an initialization expression",
4064 Set_Constant_Present (N, False);
4066 -- In Ada 83, deferred constant must be of private type
4068 elsif not Is_Private_Type (T) then
4069 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
4070 Error_Msg_N
4071 ("(Ada 83) deferred constant must be private type", N);
4072 end if;
4073 end if;
4075 -- If not a deferred constant, then the object declaration freezes
4076 -- its type, unless the object is of an anonymous type and has delayed
4077 -- aspects. In that case the type is frozen when the object itself is.
4079 else
4080 Check_Fully_Declared (T, N);
4082 if Has_Delayed_Aspects (Id)
4083 and then Is_Array_Type (T)
4084 and then Is_Itype (T)
4085 then
4086 Set_Has_Delayed_Freeze (T);
4087 else
4088 Freeze_Before (N, T);
4089 end if;
4090 end if;
4092 -- If the object was created by a constrained array definition, then
4093 -- set the link in both the anonymous base type and anonymous subtype
4094 -- that are built to represent the array type to point to the object.
4096 if Nkind (Object_Definition (Declaration_Node (Id))) =
4097 N_Constrained_Array_Definition
4098 then
4099 Set_Related_Array_Object (T, Id);
4100 Set_Related_Array_Object (Base_Type (T), Id);
4101 end if;
4103 -- Special checks for protected objects not at library level
4105 if Has_Protected (T) and then not Is_Library_Level_Entity (Id) then
4106 Check_Restriction (No_Local_Protected_Objects, Id);
4108 -- Protected objects with interrupt handlers must be at library level
4110 -- Ada 2005: This test is not needed (and the corresponding clause
4111 -- in the RM is removed) because accessibility checks are sufficient
4112 -- to make handlers not at the library level illegal.
4114 -- AI05-0303: The AI is in fact a binding interpretation, and thus
4115 -- applies to the '95 version of the language as well.
4117 if Is_Protected_Type (T)
4118 and then Has_Interrupt_Handler (T)
4119 and then Ada_Version < Ada_95
4120 then
4121 Error_Msg_N
4122 ("interrupt object can only be declared at library level", Id);
4123 end if;
4124 end if;
4126 -- Check for violation of No_Local_Timing_Events
4128 if Has_Timing_Event (T) and then not Is_Library_Level_Entity (Id) then
4129 Check_Restriction (No_Local_Timing_Events, Id);
4130 end if;
4132 -- The actual subtype of the object is the nominal subtype, unless
4133 -- the nominal one is unconstrained and obtained from the expression.
4135 Act_T := T;
4137 -- These checks should be performed before the initialization expression
4138 -- is considered, so that the Object_Definition node is still the same
4139 -- as in source code.
4141 -- In SPARK, the nominal subtype is always given by a subtype mark
4142 -- and must not be unconstrained. (The only exception to this is the
4143 -- acceptance of declarations of constants of type String.)
4145 if not Nkind_In (Object_Definition (N), N_Expanded_Name, N_Identifier)
4146 then
4147 Check_SPARK_05_Restriction
4148 ("subtype mark required", Object_Definition (N));
4150 elsif Is_Array_Type (T)
4151 and then not Is_Constrained (T)
4152 and then T /= Standard_String
4153 then
4154 Check_SPARK_05_Restriction
4155 ("subtype mark of constrained type expected",
4156 Object_Definition (N));
4157 end if;
4159 if Is_Library_Level_Entity (Id) then
4160 Check_Dynamic_Object (T);
4161 end if;
4163 -- There are no aliased objects in SPARK
4165 if Aliased_Present (N) then
4166 Check_SPARK_05_Restriction ("aliased object is not allowed", N);
4167 end if;
4169 -- Process initialization expression if present and not in error
4171 if Present (E) and then E /= Error then
4173 -- Generate an error in case of CPP class-wide object initialization.
4174 -- Required because otherwise the expansion of the class-wide
4175 -- assignment would try to use 'size to initialize the object
4176 -- (primitive that is not available in CPP tagged types).
4178 if Is_Class_Wide_Type (Act_T)
4179 and then
4180 (Is_CPP_Class (Root_Type (Etype (Act_T)))
4181 or else
4182 (Present (Full_View (Root_Type (Etype (Act_T))))
4183 and then
4184 Is_CPP_Class (Full_View (Root_Type (Etype (Act_T))))))
4185 then
4186 Error_Msg_N
4187 ("predefined assignment not available for 'C'P'P tagged types",
4189 end if;
4191 Mark_Coextensions (N, E);
4192 Analyze (E);
4194 -- In case of errors detected in the analysis of the expression,
4195 -- decorate it with the expected type to avoid cascaded errors
4197 if No (Etype (E)) then
4198 Set_Etype (E, T);
4199 end if;
4201 -- If an initialization expression is present, then we set the
4202 -- Is_True_Constant flag. It will be reset if this is a variable
4203 -- and it is indeed modified.
4205 Set_Is_True_Constant (Id, True);
4207 -- If we are analyzing a constant declaration, set its completion
4208 -- flag after analyzing and resolving the expression.
4210 if Constant_Present (N) then
4211 Set_Has_Completion (Id);
4212 end if;
4214 -- Set type and resolve (type may be overridden later on). Note:
4215 -- Ekind (Id) must still be E_Void at this point so that incorrect
4216 -- early usage within E is properly diagnosed.
4218 Set_Etype (Id, T);
4220 -- If the expression is an aggregate we must look ahead to detect
4221 -- the possible presence of an address clause, and defer resolution
4222 -- and expansion of the aggregate to the freeze point of the entity.
4224 -- This is not always legal because the aggregate may contain other
4225 -- references that need freezing, e.g. references to other entities
4226 -- with address clauses. In any case, when compiling with -gnatI the
4227 -- presence of the address clause must be ignored.
4229 if Comes_From_Source (N)
4230 and then Expander_Active
4231 and then Nkind (E) = N_Aggregate
4232 and then
4233 ((Present (Following_Address_Clause (N))
4234 and then not Ignore_Rep_Clauses)
4235 or else Delayed_Aspect_Present)
4236 then
4237 Set_Etype (E, T);
4239 else
4240 Resolve (E, T);
4241 end if;
4243 -- No further action needed if E is a call to an inlined function
4244 -- which returns an unconstrained type and it has been expanded into
4245 -- a procedure call. In that case N has been replaced by an object
4246 -- declaration without initializing expression and it has been
4247 -- analyzed (see Expand_Inlined_Call).
4249 if Back_End_Inlining
4250 and then Expander_Active
4251 and then Nkind (E) = N_Function_Call
4252 and then Nkind (Name (E)) in N_Has_Entity
4253 and then Is_Inlined (Entity (Name (E)))
4254 and then not Is_Constrained (Etype (E))
4255 and then Analyzed (N)
4256 and then No (Expression (N))
4257 then
4258 goto Leave;
4259 end if;
4261 -- If E is null and has been replaced by an N_Raise_Constraint_Error
4262 -- node (which was marked already-analyzed), we need to set the type
4263 -- to something other than Any_Access in order to keep gigi happy.
4265 if Etype (E) = Any_Access then
4266 Set_Etype (E, T);
4267 end if;
4269 -- If the object is an access to variable, the initialization
4270 -- expression cannot be an access to constant.
4272 if Is_Access_Type (T)
4273 and then not Is_Access_Constant (T)
4274 and then Is_Access_Type (Etype (E))
4275 and then Is_Access_Constant (Etype (E))
4276 then
4277 Error_Msg_N
4278 ("access to variable cannot be initialized with an "
4279 & "access-to-constant expression", E);
4280 end if;
4282 if not Assignment_OK (N) then
4283 Check_Initialization (T, E);
4284 end if;
4286 Check_Unset_Reference (E);
4288 -- If this is a variable, then set current value. If this is a
4289 -- declared constant of a scalar type with a static expression,
4290 -- indicate that it is always valid.
4292 if not Constant_Present (N) then
4293 if Compile_Time_Known_Value (E) then
4294 Set_Current_Value (Id, E);
4295 end if;
4297 elsif Is_Scalar_Type (T) and then Is_OK_Static_Expression (E) then
4298 Set_Is_Known_Valid (Id);
4299 end if;
4301 -- Deal with setting of null flags
4303 if Is_Access_Type (T) then
4304 if Known_Non_Null (E) then
4305 Set_Is_Known_Non_Null (Id, True);
4306 elsif Known_Null (E) and then not Can_Never_Be_Null (Id) then
4307 Set_Is_Known_Null (Id, True);
4308 end if;
4309 end if;
4311 -- Check incorrect use of dynamically tagged expressions
4313 if Is_Tagged_Type (T) then
4314 Check_Dynamically_Tagged_Expression
4315 (Expr => E,
4316 Typ => T,
4317 Related_Nod => N);
4318 end if;
4320 Apply_Scalar_Range_Check (E, T);
4321 Apply_Static_Length_Check (E, T);
4323 if Nkind (Original_Node (N)) = N_Object_Declaration
4324 and then Comes_From_Source (Original_Node (N))
4326 -- Only call test if needed
4328 and then Restriction_Check_Required (SPARK_05)
4329 and then not Is_SPARK_05_Initialization_Expr (Original_Node (E))
4330 then
4331 Check_SPARK_05_Restriction
4332 ("initialization expression is not appropriate", E);
4333 end if;
4335 -- A formal parameter of a specific tagged type whose related
4336 -- subprogram is subject to pragma Extensions_Visible with value
4337 -- "False" cannot be implicitly converted to a class-wide type by
4338 -- means of an initialization expression (SPARK RM 6.1.7(3)). Do
4339 -- not consider internally generated expressions.
4341 if Is_Class_Wide_Type (T)
4342 and then Comes_From_Source (E)
4343 and then Is_EVF_Expression (E)
4344 then
4345 Error_Msg_N
4346 ("formal parameter cannot be implicitly converted to "
4347 & "class-wide type when Extensions_Visible is False", E);
4348 end if;
4349 end if;
4351 -- If the No_Streams restriction is set, check that the type of the
4352 -- object is not, and does not contain, any subtype derived from
4353 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
4354 -- Has_Stream just for efficiency reasons. There is no point in
4355 -- spending time on a Has_Stream check if the restriction is not set.
4357 if Restriction_Check_Required (No_Streams) then
4358 if Has_Stream (T) then
4359 Check_Restriction (No_Streams, N);
4360 end if;
4361 end if;
4363 -- Deal with predicate check before we start to do major rewriting. It
4364 -- is OK to initialize and then check the initialized value, since the
4365 -- object goes out of scope if we get a predicate failure. Note that we
4366 -- do this in the analyzer and not the expander because the analyzer
4367 -- does some substantial rewriting in some cases.
4369 -- We need a predicate check if the type has predicates that are not
4370 -- ignored, and if either there is an initializing expression, or for
4371 -- default initialization when we have at least one case of an explicit
4372 -- default initial value and then this is not an internal declaration
4373 -- whose initialization comes later (as for an aggregate expansion).
4375 if not Suppress_Assignment_Checks (N)
4376 and then Present (Predicate_Function (T))
4377 and then not Predicates_Ignored (T)
4378 and then not No_Initialization (N)
4379 and then
4380 (Present (E)
4381 or else
4382 Is_Partially_Initialized_Type (T, Include_Implicit => False))
4383 then
4384 -- If the type has a static predicate and the expression is known at
4385 -- compile time, see if the expression satisfies the predicate.
4387 if Present (E) then
4388 Check_Expression_Against_Static_Predicate (E, T);
4389 end if;
4391 -- If the type is a null record and there is no explicit initial
4392 -- expression, no predicate check applies.
4394 if No (E) and then Is_Null_Record_Type (T) then
4395 null;
4397 -- Do not generate a predicate check if the initialization expression
4398 -- is a type conversion because the conversion has been subjected to
4399 -- the same check. This is a small optimization which avoid redundant
4400 -- checks.
4402 elsif Present (E) and then Nkind (E) = N_Type_Conversion then
4403 null;
4405 else
4406 Insert_After (N,
4407 Make_Predicate_Check (T, New_Occurrence_Of (Id, Loc)));
4408 end if;
4409 end if;
4411 -- Case of unconstrained type
4413 if not Is_Definite_Subtype (T) then
4415 -- In SPARK, a declaration of unconstrained type is allowed
4416 -- only for constants of type string.
4418 if Is_String_Type (T) and then not Constant_Present (N) then
4419 Check_SPARK_05_Restriction
4420 ("declaration of object of unconstrained type not allowed", N);
4421 end if;
4423 -- Nothing to do in deferred constant case
4425 if Constant_Present (N) and then No (E) then
4426 null;
4428 -- Case of no initialization present
4430 elsif No (E) then
4431 if No_Initialization (N) then
4432 null;
4434 elsif Is_Class_Wide_Type (T) then
4435 Error_Msg_N
4436 ("initialization required in class-wide declaration ", N);
4438 else
4439 Error_Msg_N
4440 ("unconstrained subtype not allowed (need initialization)",
4441 Object_Definition (N));
4443 if Is_Record_Type (T) and then Has_Discriminants (T) then
4444 Error_Msg_N
4445 ("\provide initial value or explicit discriminant values",
4446 Object_Definition (N));
4448 Error_Msg_NE
4449 ("\or give default discriminant values for type&",
4450 Object_Definition (N), T);
4452 elsif Is_Array_Type (T) then
4453 Error_Msg_N
4454 ("\provide initial value or explicit array bounds",
4455 Object_Definition (N));
4456 end if;
4457 end if;
4459 -- Case of initialization present but in error. Set initial
4460 -- expression as absent (but do not make above complaints)
4462 elsif E = Error then
4463 Set_Expression (N, Empty);
4464 E := Empty;
4466 -- Case of initialization present
4468 else
4469 -- Check restrictions in Ada 83
4471 if not Constant_Present (N) then
4473 -- Unconstrained variables not allowed in Ada 83 mode
4475 if Ada_Version = Ada_83
4476 and then Comes_From_Source (Object_Definition (N))
4477 then
4478 Error_Msg_N
4479 ("(Ada 83) unconstrained variable not allowed",
4480 Object_Definition (N));
4481 end if;
4482 end if;
4484 -- Now we constrain the variable from the initializing expression
4486 -- If the expression is an aggregate, it has been expanded into
4487 -- individual assignments. Retrieve the actual type from the
4488 -- expanded construct.
4490 if Is_Array_Type (T)
4491 and then No_Initialization (N)
4492 and then Nkind (Original_Node (E)) = N_Aggregate
4493 then
4494 Act_T := Etype (E);
4496 -- In case of class-wide interface object declarations we delay
4497 -- the generation of the equivalent record type declarations until
4498 -- its expansion because there are cases in they are not required.
4500 elsif Is_Interface (T) then
4501 null;
4503 -- In GNATprove mode, Expand_Subtype_From_Expr does nothing. Thus,
4504 -- we should prevent the generation of another Itype with the
4505 -- same name as the one already generated, or we end up with
4506 -- two identical types in GNATprove.
4508 elsif GNATprove_Mode then
4509 null;
4511 -- If the type is an unchecked union, no subtype can be built from
4512 -- the expression. Rewrite declaration as a renaming, which the
4513 -- back-end can handle properly. This is a rather unusual case,
4514 -- because most unchecked_union declarations have default values
4515 -- for discriminants and are thus not indefinite.
4517 elsif Is_Unchecked_Union (T) then
4518 if Constant_Present (N) or else Nkind (E) = N_Function_Call then
4519 Set_Ekind (Id, E_Constant);
4520 else
4521 Set_Ekind (Id, E_Variable);
4522 end if;
4524 Rewrite (N,
4525 Make_Object_Renaming_Declaration (Loc,
4526 Defining_Identifier => Id,
4527 Subtype_Mark => New_Occurrence_Of (T, Loc),
4528 Name => E));
4530 Set_Renamed_Object (Id, E);
4531 Freeze_Before (N, T);
4532 Set_Is_Frozen (Id);
4533 goto Leave;
4535 else
4536 -- Ensure that the generated subtype has a unique external name
4537 -- when the related object is public. This guarantees that the
4538 -- subtype and its bounds will not be affected by switches or
4539 -- pragmas that may offset the internal counter due to extra
4540 -- generated code.
4542 if Is_Public (Id) then
4543 Related_Id := Id;
4544 else
4545 Related_Id := Empty;
4546 end if;
4548 Expand_Subtype_From_Expr
4549 (N => N,
4550 Unc_Type => T,
4551 Subtype_Indic => Object_Definition (N),
4552 Exp => E,
4553 Related_Id => Related_Id);
4555 Act_T := Find_Type_Of_Object (Object_Definition (N), N);
4556 end if;
4558 Set_Is_Constr_Subt_For_U_Nominal (Act_T);
4560 if Aliased_Present (N) then
4561 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4562 end if;
4564 Freeze_Before (N, Act_T);
4565 Freeze_Before (N, T);
4566 end if;
4568 elsif Is_Array_Type (T)
4569 and then No_Initialization (N)
4570 and then (Nkind (Original_Node (E)) = N_Aggregate
4571 or else (Nkind (Original_Node (E)) = N_Qualified_Expression
4572 and then Nkind (Original_Node (Expression
4573 (Original_Node (E)))) = N_Aggregate))
4574 then
4575 if not Is_Entity_Name (Object_Definition (N)) then
4576 Act_T := Etype (E);
4577 Check_Compile_Time_Size (Act_T);
4579 if Aliased_Present (N) then
4580 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4581 end if;
4582 end if;
4584 -- When the given object definition and the aggregate are specified
4585 -- independently, and their lengths might differ do a length check.
4586 -- This cannot happen if the aggregate is of the form (others =>...)
4588 if not Is_Constrained (T) then
4589 null;
4591 elsif Nkind (E) = N_Raise_Constraint_Error then
4593 -- Aggregate is statically illegal. Place back in declaration
4595 Set_Expression (N, E);
4596 Set_No_Initialization (N, False);
4598 elsif T = Etype (E) then
4599 null;
4601 elsif Nkind (E) = N_Aggregate
4602 and then Present (Component_Associations (E))
4603 and then Present (Choice_List (First (Component_Associations (E))))
4604 and then
4605 Nkind (First (Choice_List (First (Component_Associations (E))))) =
4606 N_Others_Choice
4607 then
4608 null;
4610 else
4611 Apply_Length_Check (E, T);
4612 end if;
4614 -- If the type is limited unconstrained with defaulted discriminants and
4615 -- there is no expression, then the object is constrained by the
4616 -- defaults, so it is worthwhile building the corresponding subtype.
4618 elsif (Is_Limited_Record (T) or else Is_Concurrent_Type (T))
4619 and then not Is_Constrained (T)
4620 and then Has_Discriminants (T)
4621 then
4622 if No (E) then
4623 Act_T := Build_Default_Subtype (T, N);
4624 else
4625 -- Ada 2005: A limited object may be initialized by means of an
4626 -- aggregate. If the type has default discriminants it has an
4627 -- unconstrained nominal type, Its actual subtype will be obtained
4628 -- from the aggregate, and not from the default discriminants.
4630 Act_T := Etype (E);
4631 end if;
4633 Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
4635 elsif Nkind (E) = N_Function_Call
4636 and then Constant_Present (N)
4637 and then Has_Unconstrained_Elements (Etype (E))
4638 then
4639 -- The back-end has problems with constants of a discriminated type
4640 -- with defaults, if the initial value is a function call. We
4641 -- generate an intermediate temporary that will receive a reference
4642 -- to the result of the call. The initialization expression then
4643 -- becomes a dereference of that temporary.
4645 Remove_Side_Effects (E);
4647 -- If this is a constant declaration of an unconstrained type and
4648 -- the initialization is an aggregate, we can use the subtype of the
4649 -- aggregate for the declared entity because it is immutable.
4651 elsif not Is_Constrained (T)
4652 and then Has_Discriminants (T)
4653 and then Constant_Present (N)
4654 and then not Has_Unchecked_Union (T)
4655 and then Nkind (E) = N_Aggregate
4656 then
4657 Act_T := Etype (E);
4658 end if;
4660 -- Check No_Wide_Characters restriction
4662 Check_Wide_Character_Restriction (T, Object_Definition (N));
4664 -- Indicate this is not set in source. Certainly true for constants, and
4665 -- true for variables so far (will be reset for a variable if and when
4666 -- we encounter a modification in the source).
4668 Set_Never_Set_In_Source (Id);
4670 -- Now establish the proper kind and type of the object
4672 if Constant_Present (N) then
4673 Set_Ekind (Id, E_Constant);
4674 Set_Is_True_Constant (Id);
4676 else
4677 Set_Ekind (Id, E_Variable);
4679 -- A variable is set as shared passive if it appears in a shared
4680 -- passive package, and is at the outer level. This is not done for
4681 -- entities generated during expansion, because those are always
4682 -- manipulated locally.
4684 if Is_Shared_Passive (Current_Scope)
4685 and then Is_Library_Level_Entity (Id)
4686 and then Comes_From_Source (Id)
4687 then
4688 Set_Is_Shared_Passive (Id);
4689 Check_Shared_Var (Id, T, N);
4690 end if;
4692 -- Set Has_Initial_Value if initializing expression present. Note
4693 -- that if there is no initializing expression, we leave the state
4694 -- of this flag unchanged (usually it will be False, but notably in
4695 -- the case of exception choice variables, it will already be true).
4697 if Present (E) then
4698 Set_Has_Initial_Value (Id);
4699 end if;
4700 end if;
4702 -- Initialize alignment and size and capture alignment setting
4704 Init_Alignment (Id);
4705 Init_Esize (Id);
4706 Set_Optimize_Alignment_Flags (Id);
4708 -- Deal with aliased case
4710 if Aliased_Present (N) then
4711 Set_Is_Aliased (Id);
4713 -- If the object is aliased and the type is unconstrained with
4714 -- defaulted discriminants and there is no expression, then the
4715 -- object is constrained by the defaults, so it is worthwhile
4716 -- building the corresponding subtype.
4718 -- Ada 2005 (AI-363): If the aliased object is discriminated and
4719 -- unconstrained, then only establish an actual subtype if the
4720 -- nominal subtype is indefinite. In definite cases the object is
4721 -- unconstrained in Ada 2005.
4723 if No (E)
4724 and then Is_Record_Type (T)
4725 and then not Is_Constrained (T)
4726 and then Has_Discriminants (T)
4727 and then (Ada_Version < Ada_2005
4728 or else not Is_Definite_Subtype (T))
4729 then
4730 Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
4731 end if;
4732 end if;
4734 -- Now we can set the type of the object
4736 Set_Etype (Id, Act_T);
4738 -- Non-constant object is marked to be treated as volatile if type is
4739 -- volatile and we clear the Current_Value setting that may have been
4740 -- set above. Doing so for constants isn't required and might interfere
4741 -- with possible uses of the object as a static expression in contexts
4742 -- incompatible with volatility (e.g. as a case-statement alternative).
4744 if Ekind (Id) /= E_Constant and then Treat_As_Volatile (Etype (Id)) then
4745 Set_Treat_As_Volatile (Id);
4746 Set_Current_Value (Id, Empty);
4747 end if;
4749 -- Deal with controlled types
4751 if Has_Controlled_Component (Etype (Id))
4752 or else Is_Controlled (Etype (Id))
4753 then
4754 if not Is_Library_Level_Entity (Id) then
4755 Check_Restriction (No_Nested_Finalization, N);
4756 else
4757 Validate_Controlled_Object (Id);
4758 end if;
4759 end if;
4761 if Has_Task (Etype (Id)) then
4762 Check_Restriction (No_Tasking, N);
4764 -- Deal with counting max tasks
4766 -- Nothing to do if inside a generic
4768 if Inside_A_Generic then
4769 null;
4771 -- If library level entity, then count tasks
4773 elsif Is_Library_Level_Entity (Id) then
4774 Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
4776 -- If not library level entity, then indicate we don't know max
4777 -- tasks and also check task hierarchy restriction and blocking
4778 -- operation (since starting a task is definitely blocking).
4780 else
4781 Check_Restriction (Max_Tasks, N);
4782 Check_Restriction (No_Task_Hierarchy, N);
4783 Check_Potentially_Blocking_Operation (N);
4784 end if;
4786 -- A rather specialized test. If we see two tasks being declared
4787 -- of the same type in the same object declaration, and the task
4788 -- has an entry with an address clause, we know that program error
4789 -- will be raised at run time since we can't have two tasks with
4790 -- entries at the same address.
4792 if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
4793 declare
4794 E : Entity_Id;
4796 begin
4797 E := First_Entity (Etype (Id));
4798 while Present (E) loop
4799 if Ekind (E) = E_Entry
4800 and then Present (Get_Attribute_Definition_Clause
4801 (E, Attribute_Address))
4802 then
4803 Error_Msg_Warn := SPARK_Mode /= On;
4804 Error_Msg_N
4805 ("more than one task with same entry address<<", N);
4806 Error_Msg_N ("\Program_Error [<<", N);
4807 Insert_Action (N,
4808 Make_Raise_Program_Error (Loc,
4809 Reason => PE_Duplicated_Entry_Address));
4810 exit;
4811 end if;
4813 Next_Entity (E);
4814 end loop;
4815 end;
4816 end if;
4817 end if;
4819 -- Some simple constant-propagation: if the expression is a constant
4820 -- string initialized with a literal, share the literal. This avoids
4821 -- a run-time copy.
4823 if Present (E)
4824 and then Is_Entity_Name (E)
4825 and then Ekind (Entity (E)) = E_Constant
4826 and then Base_Type (Etype (E)) = Standard_String
4827 then
4828 declare
4829 Val : constant Node_Id := Constant_Value (Entity (E));
4830 begin
4831 if Present (Val) and then Nkind (Val) = N_String_Literal then
4832 Rewrite (E, New_Copy (Val));
4833 end if;
4834 end;
4835 end if;
4837 -- Another optimization: if the nominal subtype is unconstrained and
4838 -- the expression is a function call that returns an unconstrained
4839 -- type, rewrite the declaration as a renaming of the result of the
4840 -- call. The exceptions below are cases where the copy is expected,
4841 -- either by the back end (Aliased case) or by the semantics, as for
4842 -- initializing controlled types or copying tags for class-wide types.
4844 if Present (E)
4845 and then Nkind (E) = N_Explicit_Dereference
4846 and then Nkind (Original_Node (E)) = N_Function_Call
4847 and then not Is_Library_Level_Entity (Id)
4848 and then not Is_Constrained (Underlying_Type (T))
4849 and then not Is_Aliased (Id)
4850 and then not Is_Class_Wide_Type (T)
4851 and then not Is_Controlled_Active (T)
4852 and then not Has_Controlled_Component (Base_Type (T))
4853 and then Expander_Active
4854 then
4855 Rewrite (N,
4856 Make_Object_Renaming_Declaration (Loc,
4857 Defining_Identifier => Id,
4858 Access_Definition => Empty,
4859 Subtype_Mark => New_Occurrence_Of
4860 (Base_Type (Etype (Id)), Loc),
4861 Name => E));
4863 Set_Renamed_Object (Id, E);
4865 -- Force generation of debugging information for the constant and for
4866 -- the renamed function call.
4868 Set_Debug_Info_Needed (Id);
4869 Set_Debug_Info_Needed (Entity (Prefix (E)));
4870 end if;
4872 if Present (Prev_Entity)
4873 and then Is_Frozen (Prev_Entity)
4874 and then not Error_Posted (Id)
4875 then
4876 Error_Msg_N ("full constant declaration appears too late", N);
4877 end if;
4879 Check_Eliminated (Id);
4881 -- Deal with setting In_Private_Part flag if in private part
4883 if Ekind (Scope (Id)) = E_Package
4884 and then In_Private_Part (Scope (Id))
4885 then
4886 Set_In_Private_Part (Id);
4887 end if;
4889 <<Leave>>
4890 -- Initialize the refined state of a variable here because this is a
4891 -- common destination for legal and illegal object declarations.
4893 if Ekind (Id) = E_Variable then
4894 Set_Encapsulating_State (Id, Empty);
4895 end if;
4897 if Has_Aspects (N) then
4898 Analyze_Aspect_Specifications (N, Id);
4899 end if;
4901 Analyze_Dimension (N);
4903 -- Verify whether the object declaration introduces an illegal hidden
4904 -- state within a package subject to a null abstract state.
4906 if Ekind (Id) = E_Variable then
4907 Check_No_Hidden_State (Id);
4908 end if;
4910 Restore_Ghost_Mode (Saved_GM);
4911 end Analyze_Object_Declaration;
4913 ---------------------------
4914 -- Analyze_Others_Choice --
4915 ---------------------------
4917 -- Nothing to do for the others choice node itself, the semantic analysis
4918 -- of the others choice will occur as part of the processing of the parent
4920 procedure Analyze_Others_Choice (N : Node_Id) is
4921 pragma Warnings (Off, N);
4922 begin
4923 null;
4924 end Analyze_Others_Choice;
4926 -------------------------------------------
4927 -- Analyze_Private_Extension_Declaration --
4928 -------------------------------------------
4930 procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
4931 Indic : constant Node_Id := Subtype_Indication (N);
4932 T : constant Entity_Id := Defining_Identifier (N);
4933 Iface : Entity_Id;
4934 Iface_Elmt : Elmt_Id;
4935 Parent_Base : Entity_Id;
4936 Parent_Type : Entity_Id;
4938 begin
4939 -- Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
4941 if Is_Non_Empty_List (Interface_List (N)) then
4942 declare
4943 Intf : Node_Id;
4944 T : Entity_Id;
4946 begin
4947 Intf := First (Interface_List (N));
4948 while Present (Intf) loop
4949 T := Find_Type_Of_Subtype_Indic (Intf);
4951 Diagnose_Interface (Intf, T);
4952 Next (Intf);
4953 end loop;
4954 end;
4955 end if;
4957 Generate_Definition (T);
4959 -- For other than Ada 2012, just enter the name in the current scope
4961 if Ada_Version < Ada_2012 then
4962 Enter_Name (T);
4964 -- Ada 2012 (AI05-0162): Enter the name in the current scope handling
4965 -- case of private type that completes an incomplete type.
4967 else
4968 declare
4969 Prev : Entity_Id;
4971 begin
4972 Prev := Find_Type_Name (N);
4974 pragma Assert (Prev = T
4975 or else (Ekind (Prev) = E_Incomplete_Type
4976 and then Present (Full_View (Prev))
4977 and then Full_View (Prev) = T));
4978 end;
4979 end if;
4981 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
4982 Parent_Base := Base_Type (Parent_Type);
4984 if Parent_Type = Any_Type or else Etype (Parent_Type) = Any_Type then
4985 Set_Ekind (T, Ekind (Parent_Type));
4986 Set_Etype (T, Any_Type);
4987 goto Leave;
4989 elsif not Is_Tagged_Type (Parent_Type) then
4990 Error_Msg_N
4991 ("parent of type extension must be a tagged type ", Indic);
4992 goto Leave;
4994 elsif Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
4995 Error_Msg_N ("premature derivation of incomplete type", Indic);
4996 goto Leave;
4998 elsif Is_Concurrent_Type (Parent_Type) then
4999 Error_Msg_N
5000 ("parent type of a private extension cannot be a synchronized "
5001 & "tagged type (RM 3.9.1 (3/1))", N);
5003 Set_Etype (T, Any_Type);
5004 Set_Ekind (T, E_Limited_Private_Type);
5005 Set_Private_Dependents (T, New_Elmt_List);
5006 Set_Error_Posted (T);
5007 goto Leave;
5008 end if;
5010 -- Perhaps the parent type should be changed to the class-wide type's
5011 -- specific type in this case to prevent cascading errors ???
5013 if Is_Class_Wide_Type (Parent_Type) then
5014 Error_Msg_N
5015 ("parent of type extension must not be a class-wide type", Indic);
5016 goto Leave;
5017 end if;
5019 if (not Is_Package_Or_Generic_Package (Current_Scope)
5020 and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
5021 or else In_Private_Part (Current_Scope)
5022 then
5023 Error_Msg_N ("invalid context for private extension", N);
5024 end if;
5026 -- Set common attributes
5028 Set_Is_Pure (T, Is_Pure (Current_Scope));
5029 Set_Scope (T, Current_Scope);
5030 Set_Ekind (T, E_Record_Type_With_Private);
5031 Init_Size_Align (T);
5032 Set_Default_SSO (T);
5033 Set_No_Reordering (T, No_Component_Reordering);
5035 Set_Etype (T, Parent_Base);
5036 Propagate_Concurrent_Flags (T, Parent_Base);
5038 Set_Convention (T, Convention (Parent_Type));
5039 Set_First_Rep_Item (T, First_Rep_Item (Parent_Type));
5040 Set_Is_First_Subtype (T);
5041 Make_Class_Wide_Type (T);
5043 if Unknown_Discriminants_Present (N) then
5044 Set_Discriminant_Constraint (T, No_Elist);
5045 end if;
5047 Build_Derived_Record_Type (N, Parent_Type, T);
5049 -- A private extension inherits the Default_Initial_Condition pragma
5050 -- coming from any parent type within the derivation chain.
5052 if Has_DIC (Parent_Type) then
5053 Set_Has_Inherited_DIC (T);
5054 end if;
5056 -- A private extension inherits any class-wide invariants coming from a
5057 -- parent type or an interface. Note that the invariant procedure of the
5058 -- parent type should not be inherited because the private extension may
5059 -- define invariants of its own.
5061 if Has_Inherited_Invariants (Parent_Type)
5062 or else Has_Inheritable_Invariants (Parent_Type)
5063 then
5064 Set_Has_Inherited_Invariants (T);
5066 elsif Present (Interfaces (T)) then
5067 Iface_Elmt := First_Elmt (Interfaces (T));
5068 while Present (Iface_Elmt) loop
5069 Iface := Node (Iface_Elmt);
5071 if Has_Inheritable_Invariants (Iface) then
5072 Set_Has_Inherited_Invariants (T);
5073 exit;
5074 end if;
5076 Next_Elmt (Iface_Elmt);
5077 end loop;
5078 end if;
5080 -- Ada 2005 (AI-443): Synchronized private extension or a rewritten
5081 -- synchronized formal derived type.
5083 if Ada_Version >= Ada_2005 and then Synchronized_Present (N) then
5084 Set_Is_Limited_Record (T);
5086 -- Formal derived type case
5088 if Is_Generic_Type (T) then
5090 -- The parent must be a tagged limited type or a synchronized
5091 -- interface.
5093 if (not Is_Tagged_Type (Parent_Type)
5094 or else not Is_Limited_Type (Parent_Type))
5095 and then
5096 (not Is_Interface (Parent_Type)
5097 or else not Is_Synchronized_Interface (Parent_Type))
5098 then
5099 Error_Msg_NE
5100 ("parent type of & must be tagged limited or synchronized",
5101 N, T);
5102 end if;
5104 -- The progenitors (if any) must be limited or synchronized
5105 -- interfaces.
5107 if Present (Interfaces (T)) then
5108 Iface_Elmt := First_Elmt (Interfaces (T));
5109 while Present (Iface_Elmt) loop
5110 Iface := Node (Iface_Elmt);
5112 if not Is_Limited_Interface (Iface)
5113 and then not Is_Synchronized_Interface (Iface)
5114 then
5115 Error_Msg_NE
5116 ("progenitor & must be limited or synchronized",
5117 N, Iface);
5118 end if;
5120 Next_Elmt (Iface_Elmt);
5121 end loop;
5122 end if;
5124 -- Regular derived extension, the parent must be a limited or
5125 -- synchronized interface.
5127 else
5128 if not Is_Interface (Parent_Type)
5129 or else (not Is_Limited_Interface (Parent_Type)
5130 and then not Is_Synchronized_Interface (Parent_Type))
5131 then
5132 Error_Msg_NE
5133 ("parent type of & must be limited interface", N, T);
5134 end if;
5135 end if;
5137 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
5138 -- extension with a synchronized parent must be explicitly declared
5139 -- synchronized, because the full view will be a synchronized type.
5140 -- This must be checked before the check for limited types below,
5141 -- to ensure that types declared limited are not allowed to extend
5142 -- synchronized interfaces.
5144 elsif Is_Interface (Parent_Type)
5145 and then Is_Synchronized_Interface (Parent_Type)
5146 and then not Synchronized_Present (N)
5147 then
5148 Error_Msg_NE
5149 ("private extension of& must be explicitly synchronized",
5150 N, Parent_Type);
5152 elsif Limited_Present (N) then
5153 Set_Is_Limited_Record (T);
5155 if not Is_Limited_Type (Parent_Type)
5156 and then
5157 (not Is_Interface (Parent_Type)
5158 or else not Is_Limited_Interface (Parent_Type))
5159 then
5160 Error_Msg_NE ("parent type& of limited extension must be limited",
5161 N, Parent_Type);
5162 end if;
5163 end if;
5165 -- Remember that its parent type has a private extension. Used to warn
5166 -- on public primitives of the parent type defined after its private
5167 -- extensions (see Check_Dispatching_Operation).
5169 Set_Has_Private_Extension (Parent_Type);
5171 <<Leave>>
5172 if Has_Aspects (N) then
5173 Analyze_Aspect_Specifications (N, T);
5174 end if;
5175 end Analyze_Private_Extension_Declaration;
5177 ---------------------------------
5178 -- Analyze_Subtype_Declaration --
5179 ---------------------------------
5181 procedure Analyze_Subtype_Declaration
5182 (N : Node_Id;
5183 Skip : Boolean := False)
5185 Id : constant Entity_Id := Defining_Identifier (N);
5186 R_Checks : Check_Result;
5187 T : Entity_Id;
5189 begin
5190 Generate_Definition (Id);
5191 Set_Is_Pure (Id, Is_Pure (Current_Scope));
5192 Init_Size_Align (Id);
5194 -- The following guard condition on Enter_Name is to handle cases where
5195 -- the defining identifier has already been entered into the scope but
5196 -- the declaration as a whole needs to be analyzed.
5198 -- This case in particular happens for derived enumeration types. The
5199 -- derived enumeration type is processed as an inserted enumeration type
5200 -- declaration followed by a rewritten subtype declaration. The defining
5201 -- identifier, however, is entered into the name scope very early in the
5202 -- processing of the original type declaration and therefore needs to be
5203 -- avoided here, when the created subtype declaration is analyzed. (See
5204 -- Build_Derived_Types)
5206 -- This also happens when the full view of a private type is derived
5207 -- type with constraints. In this case the entity has been introduced
5208 -- in the private declaration.
5210 -- Finally this happens in some complex cases when validity checks are
5211 -- enabled, where the same subtype declaration may be analyzed twice.
5212 -- This can happen if the subtype is created by the pre-analysis of
5213 -- an attribute tht gives the range of a loop statement, and the loop
5214 -- itself appears within an if_statement that will be rewritten during
5215 -- expansion.
5217 if Skip
5218 or else (Present (Etype (Id))
5219 and then (Is_Private_Type (Etype (Id))
5220 or else Is_Task_Type (Etype (Id))
5221 or else Is_Rewrite_Substitution (N)))
5222 then
5223 null;
5225 elsif Current_Entity (Id) = Id then
5226 null;
5228 else
5229 Enter_Name (Id);
5230 end if;
5232 T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
5234 -- Class-wide equivalent types of records with unknown discriminants
5235 -- involve the generation of an itype which serves as the private view
5236 -- of a constrained record subtype. In such cases the base type of the
5237 -- current subtype we are processing is the private itype. Use the full
5238 -- of the private itype when decorating various attributes.
5240 if Is_Itype (T)
5241 and then Is_Private_Type (T)
5242 and then Present (Full_View (T))
5243 then
5244 T := Full_View (T);
5245 end if;
5247 -- Inherit common attributes
5249 Set_Is_Volatile (Id, Is_Volatile (T));
5250 Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
5251 Set_Is_Generic_Type (Id, Is_Generic_Type (Base_Type (T)));
5252 Set_Convention (Id, Convention (T));
5254 -- If ancestor has predicates then so does the subtype, and in addition
5255 -- we must delay the freeze to properly arrange predicate inheritance.
5257 -- The Ancestor_Type test is really unpleasant, there seem to be cases
5258 -- in which T = ID, so the above tests and assignments do nothing???
5260 if Has_Predicates (T)
5261 or else (Present (Ancestor_Subtype (T))
5262 and then Has_Predicates (Ancestor_Subtype (T)))
5263 then
5264 Set_Has_Predicates (Id);
5265 Set_Has_Delayed_Freeze (Id);
5267 -- Generated subtypes inherit the predicate function from the parent
5268 -- (no aspects to examine on the generated declaration).
5270 if not Comes_From_Source (N) then
5271 Set_Ekind (Id, Ekind (T));
5273 if Present (Predicate_Function (T)) then
5274 Set_Predicate_Function (Id, Predicate_Function (T));
5276 elsif Present (Ancestor_Subtype (T))
5277 and then Has_Predicates (Ancestor_Subtype (T))
5278 and then Present (Predicate_Function (Ancestor_Subtype (T)))
5279 then
5280 Set_Predicate_Function (Id,
5281 Predicate_Function (Ancestor_Subtype (T)));
5282 end if;
5283 end if;
5284 end if;
5286 -- Subtype of Boolean cannot have a constraint in SPARK
5288 if Is_Boolean_Type (T)
5289 and then Nkind (Subtype_Indication (N)) = N_Subtype_Indication
5290 then
5291 Check_SPARK_05_Restriction
5292 ("subtype of Boolean cannot have constraint", N);
5293 end if;
5295 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5296 declare
5297 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
5298 One_Cstr : Node_Id;
5299 Low : Node_Id;
5300 High : Node_Id;
5302 begin
5303 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint then
5304 One_Cstr := First (Constraints (Cstr));
5305 while Present (One_Cstr) loop
5307 -- Index or discriminant constraint in SPARK must be a
5308 -- subtype mark.
5310 if not
5311 Nkind_In (One_Cstr, N_Identifier, N_Expanded_Name)
5312 then
5313 Check_SPARK_05_Restriction
5314 ("subtype mark required", One_Cstr);
5316 -- String subtype must have a lower bound of 1 in SPARK.
5317 -- Note that we do not need to test for the non-static case
5318 -- here, since that was already taken care of in
5319 -- Process_Range_Expr_In_Decl.
5321 elsif Base_Type (T) = Standard_String then
5322 Get_Index_Bounds (One_Cstr, Low, High);
5324 if Is_OK_Static_Expression (Low)
5325 and then Expr_Value (Low) /= 1
5326 then
5327 Check_SPARK_05_Restriction
5328 ("String subtype must have lower bound of 1", N);
5329 end if;
5330 end if;
5332 Next (One_Cstr);
5333 end loop;
5334 end if;
5335 end;
5336 end if;
5338 -- In the case where there is no constraint given in the subtype
5339 -- indication, Process_Subtype just returns the Subtype_Mark, so its
5340 -- semantic attributes must be established here.
5342 if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
5343 Set_Etype (Id, Base_Type (T));
5345 -- Subtype of unconstrained array without constraint is not allowed
5346 -- in SPARK.
5348 if Is_Array_Type (T) and then not Is_Constrained (T) then
5349 Check_SPARK_05_Restriction
5350 ("subtype of unconstrained array must have constraint", N);
5351 end if;
5353 case Ekind (T) is
5354 when Array_Kind =>
5355 Set_Ekind (Id, E_Array_Subtype);
5356 Copy_Array_Subtype_Attributes (Id, T);
5358 when Decimal_Fixed_Point_Kind =>
5359 Set_Ekind (Id, E_Decimal_Fixed_Point_Subtype);
5360 Set_Digits_Value (Id, Digits_Value (T));
5361 Set_Delta_Value (Id, Delta_Value (T));
5362 Set_Scale_Value (Id, Scale_Value (T));
5363 Set_Small_Value (Id, Small_Value (T));
5364 Set_Scalar_Range (Id, Scalar_Range (T));
5365 Set_Machine_Radix_10 (Id, Machine_Radix_10 (T));
5366 Set_Is_Constrained (Id, Is_Constrained (T));
5367 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5368 Set_RM_Size (Id, RM_Size (T));
5370 when Enumeration_Kind =>
5371 Set_Ekind (Id, E_Enumeration_Subtype);
5372 Set_First_Literal (Id, First_Literal (Base_Type (T)));
5373 Set_Scalar_Range (Id, Scalar_Range (T));
5374 Set_Is_Character_Type (Id, Is_Character_Type (T));
5375 Set_Is_Constrained (Id, Is_Constrained (T));
5376 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5377 Set_RM_Size (Id, RM_Size (T));
5378 Inherit_Predicate_Flags (Id, T);
5380 when Ordinary_Fixed_Point_Kind =>
5381 Set_Ekind (Id, E_Ordinary_Fixed_Point_Subtype);
5382 Set_Scalar_Range (Id, Scalar_Range (T));
5383 Set_Small_Value (Id, Small_Value (T));
5384 Set_Delta_Value (Id, Delta_Value (T));
5385 Set_Is_Constrained (Id, Is_Constrained (T));
5386 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5387 Set_RM_Size (Id, RM_Size (T));
5389 when Float_Kind =>
5390 Set_Ekind (Id, E_Floating_Point_Subtype);
5391 Set_Scalar_Range (Id, Scalar_Range (T));
5392 Set_Digits_Value (Id, Digits_Value (T));
5393 Set_Is_Constrained (Id, Is_Constrained (T));
5395 -- If the floating point type has dimensions, these will be
5396 -- inherited subsequently when Analyze_Dimensions is called.
5398 when Signed_Integer_Kind =>
5399 Set_Ekind (Id, E_Signed_Integer_Subtype);
5400 Set_Scalar_Range (Id, Scalar_Range (T));
5401 Set_Is_Constrained (Id, Is_Constrained (T));
5402 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5403 Set_RM_Size (Id, RM_Size (T));
5404 Inherit_Predicate_Flags (Id, T);
5406 when Modular_Integer_Kind =>
5407 Set_Ekind (Id, E_Modular_Integer_Subtype);
5408 Set_Scalar_Range (Id, Scalar_Range (T));
5409 Set_Is_Constrained (Id, Is_Constrained (T));
5410 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5411 Set_RM_Size (Id, RM_Size (T));
5412 Inherit_Predicate_Flags (Id, T);
5414 when Class_Wide_Kind =>
5415 Set_Ekind (Id, E_Class_Wide_Subtype);
5416 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5417 Set_Cloned_Subtype (Id, T);
5418 Set_Is_Tagged_Type (Id, True);
5419 Set_Has_Unknown_Discriminants
5420 (Id, True);
5421 Set_No_Tagged_Streams_Pragma
5422 (Id, No_Tagged_Streams_Pragma (T));
5424 if Ekind (T) = E_Class_Wide_Subtype then
5425 Set_Equivalent_Type (Id, Equivalent_Type (T));
5426 end if;
5428 when E_Record_Subtype
5429 | E_Record_Type
5431 Set_Ekind (Id, E_Record_Subtype);
5433 if Ekind (T) = E_Record_Subtype
5434 and then Present (Cloned_Subtype (T))
5435 then
5436 Set_Cloned_Subtype (Id, Cloned_Subtype (T));
5437 else
5438 Set_Cloned_Subtype (Id, T);
5439 end if;
5441 Set_First_Entity (Id, First_Entity (T));
5442 Set_Last_Entity (Id, Last_Entity (T));
5443 Set_Has_Discriminants (Id, Has_Discriminants (T));
5444 Set_Is_Constrained (Id, Is_Constrained (T));
5445 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5446 Set_Has_Implicit_Dereference
5447 (Id, Has_Implicit_Dereference (T));
5448 Set_Has_Unknown_Discriminants
5449 (Id, Has_Unknown_Discriminants (T));
5451 if Has_Discriminants (T) then
5452 Set_Discriminant_Constraint
5453 (Id, Discriminant_Constraint (T));
5454 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5456 elsif Has_Unknown_Discriminants (Id) then
5457 Set_Discriminant_Constraint (Id, No_Elist);
5458 end if;
5460 if Is_Tagged_Type (T) then
5461 Set_Is_Tagged_Type (Id, True);
5462 Set_No_Tagged_Streams_Pragma
5463 (Id, No_Tagged_Streams_Pragma (T));
5464 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5465 Set_Direct_Primitive_Operations
5466 (Id, Direct_Primitive_Operations (T));
5467 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5469 if Is_Interface (T) then
5470 Set_Is_Interface (Id);
5471 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
5472 end if;
5473 end if;
5475 when Private_Kind =>
5476 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
5477 Set_Has_Discriminants (Id, Has_Discriminants (T));
5478 Set_Is_Constrained (Id, Is_Constrained (T));
5479 Set_First_Entity (Id, First_Entity (T));
5480 Set_Last_Entity (Id, Last_Entity (T));
5481 Set_Private_Dependents (Id, New_Elmt_List);
5482 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5483 Set_Has_Implicit_Dereference
5484 (Id, Has_Implicit_Dereference (T));
5485 Set_Has_Unknown_Discriminants
5486 (Id, Has_Unknown_Discriminants (T));
5487 Set_Known_To_Have_Preelab_Init
5488 (Id, Known_To_Have_Preelab_Init (T));
5490 if Is_Tagged_Type (T) then
5491 Set_Is_Tagged_Type (Id);
5492 Set_No_Tagged_Streams_Pragma (Id,
5493 No_Tagged_Streams_Pragma (T));
5494 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5495 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5496 Set_Direct_Primitive_Operations (Id,
5497 Direct_Primitive_Operations (T));
5498 end if;
5500 -- In general the attributes of the subtype of a private type
5501 -- are the attributes of the partial view of parent. However,
5502 -- the full view may be a discriminated type, and the subtype
5503 -- must share the discriminant constraint to generate correct
5504 -- calls to initialization procedures.
5506 if Has_Discriminants (T) then
5507 Set_Discriminant_Constraint
5508 (Id, Discriminant_Constraint (T));
5509 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5511 elsif Present (Full_View (T))
5512 and then Has_Discriminants (Full_View (T))
5513 then
5514 Set_Discriminant_Constraint
5515 (Id, Discriminant_Constraint (Full_View (T)));
5516 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5518 -- This would seem semantically correct, but apparently
5519 -- generates spurious errors about missing components ???
5521 -- Set_Has_Discriminants (Id);
5522 end if;
5524 Prepare_Private_Subtype_Completion (Id, N);
5526 -- If this is the subtype of a constrained private type with
5527 -- discriminants that has got a full view and we also have
5528 -- built a completion just above, show that the completion
5529 -- is a clone of the full view to the back-end.
5531 if Has_Discriminants (T)
5532 and then not Has_Unknown_Discriminants (T)
5533 and then not Is_Empty_Elmt_List (Discriminant_Constraint (T))
5534 and then Present (Full_View (T))
5535 and then Present (Full_View (Id))
5536 then
5537 Set_Cloned_Subtype (Full_View (Id), Full_View (T));
5538 end if;
5540 when Access_Kind =>
5541 Set_Ekind (Id, E_Access_Subtype);
5542 Set_Is_Constrained (Id, Is_Constrained (T));
5543 Set_Is_Access_Constant
5544 (Id, Is_Access_Constant (T));
5545 Set_Directly_Designated_Type
5546 (Id, Designated_Type (T));
5547 Set_Can_Never_Be_Null (Id, Can_Never_Be_Null (T));
5549 -- A Pure library_item must not contain the declaration of a
5550 -- named access type, except within a subprogram, generic
5551 -- subprogram, task unit, or protected unit, or if it has
5552 -- a specified Storage_Size of zero (RM05-10.2.1(15.4-15.5)).
5554 if Comes_From_Source (Id)
5555 and then In_Pure_Unit
5556 and then not In_Subprogram_Task_Protected_Unit
5557 and then not No_Pool_Assigned (Id)
5558 then
5559 Error_Msg_N
5560 ("named access types not allowed in pure unit", N);
5561 end if;
5563 when Concurrent_Kind =>
5564 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
5565 Set_Corresponding_Record_Type (Id,
5566 Corresponding_Record_Type (T));
5567 Set_First_Entity (Id, First_Entity (T));
5568 Set_First_Private_Entity (Id, First_Private_Entity (T));
5569 Set_Has_Discriminants (Id, Has_Discriminants (T));
5570 Set_Is_Constrained (Id, Is_Constrained (T));
5571 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5572 Set_Last_Entity (Id, Last_Entity (T));
5574 if Is_Tagged_Type (T) then
5575 Set_No_Tagged_Streams_Pragma
5576 (Id, No_Tagged_Streams_Pragma (T));
5577 end if;
5579 if Has_Discriminants (T) then
5580 Set_Discriminant_Constraint
5581 (Id, Discriminant_Constraint (T));
5582 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5583 end if;
5585 when Incomplete_Kind =>
5586 if Ada_Version >= Ada_2005 then
5588 -- In Ada 2005 an incomplete type can be explicitly tagged:
5589 -- propagate indication. Note that we also have to include
5590 -- subtypes for Ada 2012 extended use of incomplete types.
5592 Set_Ekind (Id, E_Incomplete_Subtype);
5593 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5594 Set_Private_Dependents (Id, New_Elmt_List);
5596 if Is_Tagged_Type (Id) then
5597 Set_No_Tagged_Streams_Pragma
5598 (Id, No_Tagged_Streams_Pragma (T));
5599 Set_Direct_Primitive_Operations (Id, New_Elmt_List);
5600 end if;
5602 -- Ada 2005 (AI-412): Decorate an incomplete subtype of an
5603 -- incomplete type visible through a limited with clause.
5605 if From_Limited_With (T)
5606 and then Present (Non_Limited_View (T))
5607 then
5608 Set_From_Limited_With (Id);
5609 Set_Non_Limited_View (Id, Non_Limited_View (T));
5611 -- Ada 2005 (AI-412): Add the regular incomplete subtype
5612 -- to the private dependents of the original incomplete
5613 -- type for future transformation.
5615 else
5616 Append_Elmt (Id, Private_Dependents (T));
5617 end if;
5619 -- If the subtype name denotes an incomplete type an error
5620 -- was already reported by Process_Subtype.
5622 else
5623 Set_Etype (Id, Any_Type);
5624 end if;
5626 when others =>
5627 raise Program_Error;
5628 end case;
5629 end if;
5631 if Etype (Id) = Any_Type then
5632 goto Leave;
5633 end if;
5635 -- Some common processing on all types
5637 Set_Size_Info (Id, T);
5638 Set_First_Rep_Item (Id, First_Rep_Item (T));
5640 -- If the parent type is a generic actual, so is the subtype. This may
5641 -- happen in a nested instance. Why Comes_From_Source test???
5643 if not Comes_From_Source (N) then
5644 Set_Is_Generic_Actual_Type (Id, Is_Generic_Actual_Type (T));
5645 end if;
5647 -- If this is a subtype declaration for an actual in an instance,
5648 -- inherit static and dynamic predicates if any.
5650 -- If declaration has no aspect specifications, inherit predicate
5651 -- info as well. Unclear how to handle the case of both specified
5652 -- and inherited predicates ??? Other inherited aspects, such as
5653 -- invariants, should be OK, but the combination with later pragmas
5654 -- may also require special merging.
5656 if Has_Predicates (T)
5657 and then Present (Predicate_Function (T))
5658 and then
5659 ((In_Instance and then not Comes_From_Source (N))
5660 or else No (Aspect_Specifications (N)))
5661 then
5662 Set_Subprograms_For_Type (Id, Subprograms_For_Type (T));
5664 if Has_Static_Predicate (T) then
5665 Set_Has_Static_Predicate (Id);
5666 Set_Static_Discrete_Predicate (Id, Static_Discrete_Predicate (T));
5667 end if;
5668 end if;
5670 -- Remaining processing depends on characteristics of base type
5672 T := Etype (Id);
5674 Set_Is_Immediately_Visible (Id, True);
5675 Set_Depends_On_Private (Id, Has_Private_Component (T));
5676 Set_Is_Descendant_Of_Address (Id, Is_Descendant_Of_Address (T));
5678 if Is_Interface (T) then
5679 Set_Is_Interface (Id);
5680 end if;
5682 if Present (Generic_Parent_Type (N))
5683 and then
5684 (Nkind (Parent (Generic_Parent_Type (N))) /=
5685 N_Formal_Type_Declaration
5686 or else Nkind (Formal_Type_Definition
5687 (Parent (Generic_Parent_Type (N)))) /=
5688 N_Formal_Private_Type_Definition)
5689 then
5690 if Is_Tagged_Type (Id) then
5692 -- If this is a generic actual subtype for a synchronized type,
5693 -- the primitive operations are those of the corresponding record
5694 -- for which there is a separate subtype declaration.
5696 if Is_Concurrent_Type (Id) then
5697 null;
5698 elsif Is_Class_Wide_Type (Id) then
5699 Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
5700 else
5701 Derive_Subprograms (Generic_Parent_Type (N), Id, T);
5702 end if;
5704 elsif Scope (Etype (Id)) /= Standard_Standard then
5705 Derive_Subprograms (Generic_Parent_Type (N), Id);
5706 end if;
5707 end if;
5709 if Is_Private_Type (T) and then Present (Full_View (T)) then
5710 Conditional_Delay (Id, Full_View (T));
5712 -- The subtypes of components or subcomponents of protected types
5713 -- do not need freeze nodes, which would otherwise appear in the
5714 -- wrong scope (before the freeze node for the protected type). The
5715 -- proper subtypes are those of the subcomponents of the corresponding
5716 -- record.
5718 elsif Ekind (Scope (Id)) /= E_Protected_Type
5719 and then Present (Scope (Scope (Id))) -- error defense
5720 and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
5721 then
5722 Conditional_Delay (Id, T);
5723 end if;
5725 -- If we have a subtype of an incomplete type whose full type is a
5726 -- derived numeric type, we need to have a freeze node for the subtype.
5727 -- Otherwise gigi will complain while computing the (static) bounds of
5728 -- the subtype.
5730 if Is_Itype (T)
5731 and then Is_Elementary_Type (Id)
5732 and then Etype (Id) /= Id
5733 then
5734 declare
5735 Partial : constant Entity_Id :=
5736 Incomplete_Or_Partial_View (First_Subtype (Id));
5737 begin
5738 if Present (Partial)
5739 and then Ekind (Partial) = E_Incomplete_Type
5740 then
5741 Set_Has_Delayed_Freeze (Id);
5742 end if;
5743 end;
5744 end if;
5746 -- Check that Constraint_Error is raised for a scalar subtype indication
5747 -- when the lower or upper bound of a non-null range lies outside the
5748 -- range of the type mark.
5750 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5751 if Is_Scalar_Type (Etype (Id))
5752 and then Scalar_Range (Id) /=
5753 Scalar_Range
5754 (Etype (Subtype_Mark (Subtype_Indication (N))))
5755 then
5756 Apply_Range_Check
5757 (Scalar_Range (Id),
5758 Etype (Subtype_Mark (Subtype_Indication (N))));
5760 -- In the array case, check compatibility for each index
5762 elsif Is_Array_Type (Etype (Id)) and then Present (First_Index (Id))
5763 then
5764 -- This really should be a subprogram that finds the indications
5765 -- to check???
5767 declare
5768 Subt_Index : Node_Id := First_Index (Id);
5769 Target_Index : Node_Id :=
5770 First_Index (Etype
5771 (Subtype_Mark (Subtype_Indication (N))));
5772 Has_Dyn_Chk : Boolean := Has_Dynamic_Range_Check (N);
5774 begin
5775 while Present (Subt_Index) loop
5776 if ((Nkind (Subt_Index) = N_Identifier
5777 and then Ekind (Entity (Subt_Index)) in Scalar_Kind)
5778 or else Nkind (Subt_Index) = N_Subtype_Indication)
5779 and then
5780 Nkind (Scalar_Range (Etype (Subt_Index))) = N_Range
5781 then
5782 declare
5783 Target_Typ : constant Entity_Id :=
5784 Etype (Target_Index);
5785 begin
5786 R_Checks :=
5787 Get_Range_Checks
5788 (Scalar_Range (Etype (Subt_Index)),
5789 Target_Typ,
5790 Etype (Subt_Index),
5791 Defining_Identifier (N));
5793 -- Reset Has_Dynamic_Range_Check on the subtype to
5794 -- prevent elision of the index check due to a dynamic
5795 -- check generated for a preceding index (needed since
5796 -- Insert_Range_Checks tries to avoid generating
5797 -- redundant checks on a given declaration).
5799 Set_Has_Dynamic_Range_Check (N, False);
5801 Insert_Range_Checks
5802 (R_Checks,
5804 Target_Typ,
5805 Sloc (Defining_Identifier (N)));
5807 -- Record whether this index involved a dynamic check
5809 Has_Dyn_Chk :=
5810 Has_Dyn_Chk or else Has_Dynamic_Range_Check (N);
5811 end;
5812 end if;
5814 Next_Index (Subt_Index);
5815 Next_Index (Target_Index);
5816 end loop;
5818 -- Finally, mark whether the subtype involves dynamic checks
5820 Set_Has_Dynamic_Range_Check (N, Has_Dyn_Chk);
5821 end;
5822 end if;
5823 end if;
5825 Set_Optimize_Alignment_Flags (Id);
5826 Check_Eliminated (Id);
5828 <<Leave>>
5829 if Has_Aspects (N) then
5830 Analyze_Aspect_Specifications (N, Id);
5831 end if;
5833 Analyze_Dimension (N);
5835 -- Check No_Dynamic_Sized_Objects restriction, which disallows subtype
5836 -- indications on composite types where the constraints are dynamic.
5837 -- Note that object declarations and aggregates generate implicit
5838 -- subtype declarations, which this covers. One special case is that the
5839 -- implicitly generated "=" for discriminated types includes an
5840 -- offending subtype declaration, which is harmless, so we ignore it
5841 -- here.
5843 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5844 declare
5845 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
5846 begin
5847 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint
5848 and then not (Is_Internal (Id)
5849 and then Is_TSS (Scope (Id),
5850 TSS_Composite_Equality))
5851 and then not Within_Init_Proc
5852 and then not All_Composite_Constraints_Static (Cstr)
5853 then
5854 Check_Restriction (No_Dynamic_Sized_Objects, Cstr);
5855 end if;
5856 end;
5857 end if;
5858 end Analyze_Subtype_Declaration;
5860 --------------------------------
5861 -- Analyze_Subtype_Indication --
5862 --------------------------------
5864 procedure Analyze_Subtype_Indication (N : Node_Id) is
5865 T : constant Entity_Id := Subtype_Mark (N);
5866 R : constant Node_Id := Range_Expression (Constraint (N));
5868 begin
5869 Analyze (T);
5871 if R /= Error then
5872 Analyze (R);
5873 Set_Etype (N, Etype (R));
5874 Resolve (R, Entity (T));
5875 else
5876 Set_Error_Posted (R);
5877 Set_Error_Posted (T);
5878 end if;
5879 end Analyze_Subtype_Indication;
5881 --------------------------
5882 -- Analyze_Variant_Part --
5883 --------------------------
5885 procedure Analyze_Variant_Part (N : Node_Id) is
5886 Discr_Name : Node_Id;
5887 Discr_Type : Entity_Id;
5889 procedure Process_Variant (A : Node_Id);
5890 -- Analyze declarations for a single variant
5892 package Analyze_Variant_Choices is
5893 new Generic_Analyze_Choices (Process_Variant);
5894 use Analyze_Variant_Choices;
5896 ---------------------
5897 -- Process_Variant --
5898 ---------------------
5900 procedure Process_Variant (A : Node_Id) is
5901 CL : constant Node_Id := Component_List (A);
5902 begin
5903 if not Null_Present (CL) then
5904 Analyze_Declarations (Component_Items (CL));
5906 if Present (Variant_Part (CL)) then
5907 Analyze (Variant_Part (CL));
5908 end if;
5909 end if;
5910 end Process_Variant;
5912 -- Start of processing for Analyze_Variant_Part
5914 begin
5915 Discr_Name := Name (N);
5916 Analyze (Discr_Name);
5918 -- If Discr_Name bad, get out (prevent cascaded errors)
5920 if Etype (Discr_Name) = Any_Type then
5921 return;
5922 end if;
5924 -- Check invalid discriminant in variant part
5926 if Ekind (Entity (Discr_Name)) /= E_Discriminant then
5927 Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
5928 end if;
5930 Discr_Type := Etype (Entity (Discr_Name));
5932 if not Is_Discrete_Type (Discr_Type) then
5933 Error_Msg_N
5934 ("discriminant in a variant part must be of a discrete type",
5935 Name (N));
5936 return;
5937 end if;
5939 -- Now analyze the choices, which also analyzes the declarations that
5940 -- are associated with each choice.
5942 Analyze_Choices (Variants (N), Discr_Type);
5944 -- Note: we used to instantiate and call Check_Choices here to check
5945 -- that the choices covered the discriminant, but it's too early to do
5946 -- that because of statically predicated subtypes, whose analysis may
5947 -- be deferred to their freeze point which may be as late as the freeze
5948 -- point of the containing record. So this call is now to be found in
5949 -- Freeze_Record_Declaration.
5951 end Analyze_Variant_Part;
5953 ----------------------------
5954 -- Array_Type_Declaration --
5955 ----------------------------
5957 procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
5958 Component_Def : constant Node_Id := Component_Definition (Def);
5959 Component_Typ : constant Node_Id := Subtype_Indication (Component_Def);
5960 P : constant Node_Id := Parent (Def);
5961 Element_Type : Entity_Id;
5962 Implicit_Base : Entity_Id;
5963 Index : Node_Id;
5964 Nb_Index : Nat;
5965 Priv : Entity_Id;
5966 Related_Id : Entity_Id := Empty;
5968 begin
5969 if Nkind (Def) = N_Constrained_Array_Definition then
5970 Index := First (Discrete_Subtype_Definitions (Def));
5971 else
5972 Index := First (Subtype_Marks (Def));
5973 end if;
5975 -- Find proper names for the implicit types which may be public. In case
5976 -- of anonymous arrays we use the name of the first object of that type
5977 -- as prefix.
5979 if No (T) then
5980 Related_Id := Defining_Identifier (P);
5981 else
5982 Related_Id := T;
5983 end if;
5985 Nb_Index := 1;
5986 while Present (Index) loop
5987 Analyze (Index);
5989 -- Test for odd case of trying to index a type by the type itself
5991 if Is_Entity_Name (Index) and then Entity (Index) = T then
5992 Error_Msg_N ("type& cannot be indexed by itself", Index);
5993 Set_Entity (Index, Standard_Boolean);
5994 Set_Etype (Index, Standard_Boolean);
5995 end if;
5997 -- Check SPARK restriction requiring a subtype mark
5999 if not Nkind_In (Index, N_Identifier, N_Expanded_Name) then
6000 Check_SPARK_05_Restriction ("subtype mark required", Index);
6001 end if;
6003 -- Add a subtype declaration for each index of private array type
6004 -- declaration whose etype is also private. For example:
6006 -- package Pkg is
6007 -- type Index is private;
6008 -- private
6009 -- type Table is array (Index) of ...
6010 -- end;
6012 -- This is currently required by the expander for the internally
6013 -- generated equality subprogram of records with variant parts in
6014 -- which the etype of some component is such private type.
6016 if Ekind (Current_Scope) = E_Package
6017 and then In_Private_Part (Current_Scope)
6018 and then Has_Private_Declaration (Etype (Index))
6019 then
6020 declare
6021 Loc : constant Source_Ptr := Sloc (Def);
6022 Decl : Entity_Id;
6023 New_E : Entity_Id;
6025 begin
6026 New_E := Make_Temporary (Loc, 'T');
6027 Set_Is_Internal (New_E);
6029 Decl :=
6030 Make_Subtype_Declaration (Loc,
6031 Defining_Identifier => New_E,
6032 Subtype_Indication =>
6033 New_Occurrence_Of (Etype (Index), Loc));
6035 Insert_Before (Parent (Def), Decl);
6036 Analyze (Decl);
6037 Set_Etype (Index, New_E);
6039 -- If the index is a range or a subtype indication it carries
6040 -- no entity. Example:
6042 -- package Pkg is
6043 -- type T is private;
6044 -- private
6045 -- type T is new Natural;
6046 -- Table : array (T(1) .. T(10)) of Boolean;
6047 -- end Pkg;
6049 -- Otherwise the type of the reference is its entity.
6051 if Is_Entity_Name (Index) then
6052 Set_Entity (Index, New_E);
6053 end if;
6054 end;
6055 end if;
6057 Make_Index (Index, P, Related_Id, Nb_Index);
6059 -- Check error of subtype with predicate for index type
6061 Bad_Predicated_Subtype_Use
6062 ("subtype& has predicate, not allowed as index subtype",
6063 Index, Etype (Index));
6065 -- Move to next index
6067 Next_Index (Index);
6068 Nb_Index := Nb_Index + 1;
6069 end loop;
6071 -- Process subtype indication if one is present
6073 if Present (Component_Typ) then
6074 Element_Type := Process_Subtype (Component_Typ, P, Related_Id, 'C');
6076 Set_Etype (Component_Typ, Element_Type);
6078 if not Nkind_In (Component_Typ, N_Identifier, N_Expanded_Name) then
6079 Check_SPARK_05_Restriction
6080 ("subtype mark required", Component_Typ);
6081 end if;
6083 -- Ada 2005 (AI-230): Access Definition case
6085 else pragma Assert (Present (Access_Definition (Component_Def)));
6087 -- Indicate that the anonymous access type is created by the
6088 -- array type declaration.
6090 Element_Type := Access_Definition
6091 (Related_Nod => P,
6092 N => Access_Definition (Component_Def));
6093 Set_Is_Local_Anonymous_Access (Element_Type);
6095 -- Propagate the parent. This field is needed if we have to generate
6096 -- the master_id associated with an anonymous access to task type
6097 -- component (see Expand_N_Full_Type_Declaration.Build_Master)
6099 Set_Parent (Element_Type, Parent (T));
6101 -- Ada 2005 (AI-230): In case of components that are anonymous access
6102 -- types the level of accessibility depends on the enclosing type
6103 -- declaration
6105 Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
6107 -- Ada 2005 (AI-254)
6109 declare
6110 CD : constant Node_Id :=
6111 Access_To_Subprogram_Definition
6112 (Access_Definition (Component_Def));
6113 begin
6114 if Present (CD) and then Protected_Present (CD) then
6115 Element_Type :=
6116 Replace_Anonymous_Access_To_Protected_Subprogram (Def);
6117 end if;
6118 end;
6119 end if;
6121 -- Constrained array case
6123 if No (T) then
6124 T := Create_Itype (E_Void, P, Related_Id, 'T');
6125 end if;
6127 if Nkind (Def) = N_Constrained_Array_Definition then
6129 -- Establish Implicit_Base as unconstrained base type
6131 Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
6133 Set_Etype (Implicit_Base, Implicit_Base);
6134 Set_Scope (Implicit_Base, Current_Scope);
6135 Set_Has_Delayed_Freeze (Implicit_Base);
6136 Set_Default_SSO (Implicit_Base);
6138 -- The constrained array type is a subtype of the unconstrained one
6140 Set_Ekind (T, E_Array_Subtype);
6141 Init_Size_Align (T);
6142 Set_Etype (T, Implicit_Base);
6143 Set_Scope (T, Current_Scope);
6144 Set_Is_Constrained (T);
6145 Set_First_Index (T,
6146 First (Discrete_Subtype_Definitions (Def)));
6147 Set_Has_Delayed_Freeze (T);
6149 -- Complete setup of implicit base type
6151 Set_Component_Size (Implicit_Base, Uint_0);
6152 Set_Component_Type (Implicit_Base, Element_Type);
6153 Set_Finalize_Storage_Only
6154 (Implicit_Base,
6155 Finalize_Storage_Only (Element_Type));
6156 Set_First_Index (Implicit_Base, First_Index (T));
6157 Set_Has_Controlled_Component
6158 (Implicit_Base,
6159 Has_Controlled_Component (Element_Type)
6160 or else Is_Controlled_Active (Element_Type));
6161 Set_Packed_Array_Impl_Type
6162 (Implicit_Base, Empty);
6164 Propagate_Concurrent_Flags (Implicit_Base, Element_Type);
6166 -- Unconstrained array case
6168 else
6169 Set_Ekind (T, E_Array_Type);
6170 Init_Size_Align (T);
6171 Set_Etype (T, T);
6172 Set_Scope (T, Current_Scope);
6173 Set_Component_Size (T, Uint_0);
6174 Set_Is_Constrained (T, False);
6175 Set_First_Index (T, First (Subtype_Marks (Def)));
6176 Set_Has_Delayed_Freeze (T, True);
6177 Propagate_Concurrent_Flags (T, Element_Type);
6178 Set_Has_Controlled_Component (T, Has_Controlled_Component
6179 (Element_Type)
6180 or else
6181 Is_Controlled_Active (Element_Type));
6182 Set_Finalize_Storage_Only (T, Finalize_Storage_Only
6183 (Element_Type));
6184 Set_Default_SSO (T);
6185 end if;
6187 -- Common attributes for both cases
6189 Set_Component_Type (Base_Type (T), Element_Type);
6190 Set_Packed_Array_Impl_Type (T, Empty);
6192 if Aliased_Present (Component_Definition (Def)) then
6193 Check_SPARK_05_Restriction
6194 ("aliased is not allowed", Component_Definition (Def));
6195 Set_Has_Aliased_Components (Etype (T));
6196 end if;
6198 -- Ada 2005 (AI-231): Propagate the null-excluding attribute to the
6199 -- array type to ensure that objects of this type are initialized.
6201 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (Element_Type) then
6202 Set_Can_Never_Be_Null (T);
6204 if Null_Exclusion_Present (Component_Definition (Def))
6206 -- No need to check itypes because in their case this check was
6207 -- done at their point of creation
6209 and then not Is_Itype (Element_Type)
6210 then
6211 Error_Msg_N
6212 ("`NOT NULL` not allowed (null already excluded)",
6213 Subtype_Indication (Component_Definition (Def)));
6214 end if;
6215 end if;
6217 Priv := Private_Component (Element_Type);
6219 if Present (Priv) then
6221 -- Check for circular definitions
6223 if Priv = Any_Type then
6224 Set_Component_Type (Etype (T), Any_Type);
6226 -- There is a gap in the visibility of operations on the composite
6227 -- type only if the component type is defined in a different scope.
6229 elsif Scope (Priv) = Current_Scope then
6230 null;
6232 elsif Is_Limited_Type (Priv) then
6233 Set_Is_Limited_Composite (Etype (T));
6234 Set_Is_Limited_Composite (T);
6235 else
6236 Set_Is_Private_Composite (Etype (T));
6237 Set_Is_Private_Composite (T);
6238 end if;
6239 end if;
6241 -- A syntax error in the declaration itself may lead to an empty index
6242 -- list, in which case do a minimal patch.
6244 if No (First_Index (T)) then
6245 Error_Msg_N ("missing index definition in array type declaration", T);
6247 declare
6248 Indexes : constant List_Id :=
6249 New_List (New_Occurrence_Of (Any_Id, Sloc (T)));
6250 begin
6251 Set_Discrete_Subtype_Definitions (Def, Indexes);
6252 Set_First_Index (T, First (Indexes));
6253 return;
6254 end;
6255 end if;
6257 -- Create a concatenation operator for the new type. Internal array
6258 -- types created for packed entities do not need such, they are
6259 -- compatible with the user-defined type.
6261 if Number_Dimensions (T) = 1
6262 and then not Is_Packed_Array_Impl_Type (T)
6263 then
6264 New_Concatenation_Op (T);
6265 end if;
6267 -- In the case of an unconstrained array the parser has already verified
6268 -- that all the indexes are unconstrained but we still need to make sure
6269 -- that the element type is constrained.
6271 if not Is_Definite_Subtype (Element_Type) then
6272 Error_Msg_N
6273 ("unconstrained element type in array declaration",
6274 Subtype_Indication (Component_Def));
6276 elsif Is_Abstract_Type (Element_Type) then
6277 Error_Msg_N
6278 ("the type of a component cannot be abstract",
6279 Subtype_Indication (Component_Def));
6280 end if;
6282 -- There may be an invariant declared for the component type, but
6283 -- the construction of the component invariant checking procedure
6284 -- takes place during expansion.
6285 end Array_Type_Declaration;
6287 ------------------------------------------------------
6288 -- Replace_Anonymous_Access_To_Protected_Subprogram --
6289 ------------------------------------------------------
6291 function Replace_Anonymous_Access_To_Protected_Subprogram
6292 (N : Node_Id) return Entity_Id
6294 Loc : constant Source_Ptr := Sloc (N);
6296 Curr_Scope : constant Scope_Stack_Entry :=
6297 Scope_Stack.Table (Scope_Stack.Last);
6299 Anon : constant Entity_Id := Make_Temporary (Loc, 'S');
6301 Acc : Node_Id;
6302 -- Access definition in declaration
6304 Comp : Node_Id;
6305 -- Object definition or formal definition with an access definition
6307 Decl : Node_Id;
6308 -- Declaration of anonymous access to subprogram type
6310 Spec : Node_Id;
6311 -- Original specification in access to subprogram
6313 P : Node_Id;
6315 begin
6316 Set_Is_Internal (Anon);
6318 case Nkind (N) is
6319 when N_Constrained_Array_Definition
6320 | N_Component_Declaration
6321 | N_Unconstrained_Array_Definition
6323 Comp := Component_Definition (N);
6324 Acc := Access_Definition (Comp);
6326 when N_Discriminant_Specification =>
6327 Comp := Discriminant_Type (N);
6328 Acc := Comp;
6330 when N_Parameter_Specification =>
6331 Comp := Parameter_Type (N);
6332 Acc := Comp;
6334 when N_Access_Function_Definition =>
6335 Comp := Result_Definition (N);
6336 Acc := Comp;
6338 when N_Object_Declaration =>
6339 Comp := Object_Definition (N);
6340 Acc := Comp;
6342 when N_Function_Specification =>
6343 Comp := Result_Definition (N);
6344 Acc := Comp;
6346 when others =>
6347 raise Program_Error;
6348 end case;
6350 Spec := Access_To_Subprogram_Definition (Acc);
6352 Decl :=
6353 Make_Full_Type_Declaration (Loc,
6354 Defining_Identifier => Anon,
6355 Type_Definition => Copy_Separate_Tree (Spec));
6357 Mark_Rewrite_Insertion (Decl);
6359 -- In ASIS mode, analyze the profile on the original node, because
6360 -- the separate copy does not provide enough links to recover the
6361 -- original tree. Analysis is limited to type annotations, within
6362 -- a temporary scope that serves as an anonymous subprogram to collect
6363 -- otherwise useless temporaries and itypes.
6365 if ASIS_Mode then
6366 declare
6367 Typ : constant Entity_Id := Make_Temporary (Loc, 'S');
6369 begin
6370 if Nkind (Spec) = N_Access_Function_Definition then
6371 Set_Ekind (Typ, E_Function);
6372 else
6373 Set_Ekind (Typ, E_Procedure);
6374 end if;
6376 Set_Parent (Typ, N);
6377 Set_Scope (Typ, Current_Scope);
6378 Push_Scope (Typ);
6380 -- Nothing to do if procedure is parameterless
6382 if Present (Parameter_Specifications (Spec)) then
6383 Process_Formals (Parameter_Specifications (Spec), Spec);
6384 end if;
6386 if Nkind (Spec) = N_Access_Function_Definition then
6387 declare
6388 Def : constant Node_Id := Result_Definition (Spec);
6390 begin
6391 -- The result might itself be an anonymous access type, so
6392 -- have to recurse.
6394 if Nkind (Def) = N_Access_Definition then
6395 if Present (Access_To_Subprogram_Definition (Def)) then
6396 Set_Etype
6397 (Def,
6398 Replace_Anonymous_Access_To_Protected_Subprogram
6399 (Spec));
6400 else
6401 Find_Type (Subtype_Mark (Def));
6402 end if;
6404 else
6405 Find_Type (Def);
6406 end if;
6407 end;
6408 end if;
6410 End_Scope;
6411 end;
6412 end if;
6414 -- Insert the new declaration in the nearest enclosing scope. If the
6415 -- parent is a body and N is its return type, the declaration belongs
6416 -- in the enclosing scope. Likewise if N is the type of a parameter.
6418 P := Parent (N);
6420 if Nkind (N) = N_Function_Specification
6421 and then Nkind (P) = N_Subprogram_Body
6422 then
6423 P := Parent (P);
6424 elsif Nkind (N) = N_Parameter_Specification
6425 and then Nkind (P) in N_Subprogram_Specification
6426 and then Nkind (Parent (P)) = N_Subprogram_Body
6427 then
6428 P := Parent (Parent (P));
6429 end if;
6431 while Present (P) and then not Has_Declarations (P) loop
6432 P := Parent (P);
6433 end loop;
6435 pragma Assert (Present (P));
6437 if Nkind (P) = N_Package_Specification then
6438 Prepend (Decl, Visible_Declarations (P));
6439 else
6440 Prepend (Decl, Declarations (P));
6441 end if;
6443 -- Replace the anonymous type with an occurrence of the new declaration.
6444 -- In all cases the rewritten node does not have the null-exclusion
6445 -- attribute because (if present) it was already inherited by the
6446 -- anonymous entity (Anon). Thus, in case of components we do not
6447 -- inherit this attribute.
6449 if Nkind (N) = N_Parameter_Specification then
6450 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6451 Set_Etype (Defining_Identifier (N), Anon);
6452 Set_Null_Exclusion_Present (N, False);
6454 elsif Nkind (N) = N_Object_Declaration then
6455 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6456 Set_Etype (Defining_Identifier (N), Anon);
6458 elsif Nkind (N) = N_Access_Function_Definition then
6459 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6461 elsif Nkind (N) = N_Function_Specification then
6462 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6463 Set_Etype (Defining_Unit_Name (N), Anon);
6465 else
6466 Rewrite (Comp,
6467 Make_Component_Definition (Loc,
6468 Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
6469 end if;
6471 Mark_Rewrite_Insertion (Comp);
6473 if Nkind_In (N, N_Object_Declaration, N_Access_Function_Definition)
6474 or else (Nkind (Parent (N)) = N_Full_Type_Declaration
6475 and then not Is_Type (Current_Scope))
6476 then
6478 -- Declaration can be analyzed in the current scope.
6480 Analyze (Decl);
6482 else
6483 -- Temporarily remove the current scope (record or subprogram) from
6484 -- the stack to add the new declarations to the enclosing scope.
6485 -- The anonymous entity is an Itype with the proper attributes.
6487 Scope_Stack.Decrement_Last;
6488 Analyze (Decl);
6489 Set_Is_Itype (Anon);
6490 Set_Associated_Node_For_Itype (Anon, N);
6491 Scope_Stack.Append (Curr_Scope);
6492 end if;
6494 Set_Ekind (Anon, E_Anonymous_Access_Protected_Subprogram_Type);
6495 Set_Can_Use_Internal_Rep (Anon, not Always_Compatible_Rep_On_Target);
6496 return Anon;
6497 end Replace_Anonymous_Access_To_Protected_Subprogram;
6499 -------------------------------
6500 -- Build_Derived_Access_Type --
6501 -------------------------------
6503 procedure Build_Derived_Access_Type
6504 (N : Node_Id;
6505 Parent_Type : Entity_Id;
6506 Derived_Type : Entity_Id)
6508 S : constant Node_Id := Subtype_Indication (Type_Definition (N));
6510 Desig_Type : Entity_Id;
6511 Discr : Entity_Id;
6512 Discr_Con_Elist : Elist_Id;
6513 Discr_Con_El : Elmt_Id;
6514 Subt : Entity_Id;
6516 begin
6517 -- Set the designated type so it is available in case this is an access
6518 -- to a self-referential type, e.g. a standard list type with a next
6519 -- pointer. Will be reset after subtype is built.
6521 Set_Directly_Designated_Type
6522 (Derived_Type, Designated_Type (Parent_Type));
6524 Subt := Process_Subtype (S, N);
6526 if Nkind (S) /= N_Subtype_Indication
6527 and then Subt /= Base_Type (Subt)
6528 then
6529 Set_Ekind (Derived_Type, E_Access_Subtype);
6530 end if;
6532 if Ekind (Derived_Type) = E_Access_Subtype then
6533 declare
6534 Pbase : constant Entity_Id := Base_Type (Parent_Type);
6535 Ibase : constant Entity_Id :=
6536 Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
6537 Svg_Chars : constant Name_Id := Chars (Ibase);
6538 Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
6540 begin
6541 Copy_Node (Pbase, Ibase);
6543 -- Restore Itype status after Copy_Node
6545 Set_Is_Itype (Ibase);
6546 Set_Associated_Node_For_Itype (Ibase, N);
6548 Set_Chars (Ibase, Svg_Chars);
6549 Set_Next_Entity (Ibase, Svg_Next_E);
6550 Set_Sloc (Ibase, Sloc (Derived_Type));
6551 Set_Scope (Ibase, Scope (Derived_Type));
6552 Set_Freeze_Node (Ibase, Empty);
6553 Set_Is_Frozen (Ibase, False);
6554 Set_Comes_From_Source (Ibase, False);
6555 Set_Is_First_Subtype (Ibase, False);
6557 Set_Etype (Ibase, Pbase);
6558 Set_Etype (Derived_Type, Ibase);
6559 end;
6560 end if;
6562 Set_Directly_Designated_Type
6563 (Derived_Type, Designated_Type (Subt));
6565 Set_Is_Constrained (Derived_Type, Is_Constrained (Subt));
6566 Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
6567 Set_Size_Info (Derived_Type, Parent_Type);
6568 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
6569 Set_Depends_On_Private (Derived_Type,
6570 Has_Private_Component (Derived_Type));
6571 Conditional_Delay (Derived_Type, Subt);
6573 -- Ada 2005 (AI-231): Set the null-exclusion attribute, and verify
6574 -- that it is not redundant.
6576 if Null_Exclusion_Present (Type_Definition (N)) then
6577 Set_Can_Never_Be_Null (Derived_Type);
6579 elsif Can_Never_Be_Null (Parent_Type) then
6580 Set_Can_Never_Be_Null (Derived_Type);
6581 end if;
6583 -- Note: we do not copy the Storage_Size_Variable, since we always go to
6584 -- the root type for this information.
6586 -- Apply range checks to discriminants for derived record case
6587 -- ??? THIS CODE SHOULD NOT BE HERE REALLY.
6589 Desig_Type := Designated_Type (Derived_Type);
6591 if Is_Composite_Type (Desig_Type)
6592 and then (not Is_Array_Type (Desig_Type))
6593 and then Has_Discriminants (Desig_Type)
6594 and then Base_Type (Desig_Type) /= Desig_Type
6595 then
6596 Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
6597 Discr_Con_El := First_Elmt (Discr_Con_Elist);
6599 Discr := First_Discriminant (Base_Type (Desig_Type));
6600 while Present (Discr_Con_El) loop
6601 Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
6602 Next_Elmt (Discr_Con_El);
6603 Next_Discriminant (Discr);
6604 end loop;
6605 end if;
6606 end Build_Derived_Access_Type;
6608 ------------------------------
6609 -- Build_Derived_Array_Type --
6610 ------------------------------
6612 procedure Build_Derived_Array_Type
6613 (N : Node_Id;
6614 Parent_Type : Entity_Id;
6615 Derived_Type : Entity_Id)
6617 Loc : constant Source_Ptr := Sloc (N);
6618 Tdef : constant Node_Id := Type_Definition (N);
6619 Indic : constant Node_Id := Subtype_Indication (Tdef);
6620 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
6621 Implicit_Base : Entity_Id;
6622 New_Indic : Node_Id;
6624 procedure Make_Implicit_Base;
6625 -- If the parent subtype is constrained, the derived type is a subtype
6626 -- of an implicit base type derived from the parent base.
6628 ------------------------
6629 -- Make_Implicit_Base --
6630 ------------------------
6632 procedure Make_Implicit_Base is
6633 begin
6634 Implicit_Base :=
6635 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
6637 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
6638 Set_Etype (Implicit_Base, Parent_Base);
6640 Copy_Array_Subtype_Attributes (Implicit_Base, Parent_Base);
6641 Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
6643 Set_Has_Delayed_Freeze (Implicit_Base, True);
6644 end Make_Implicit_Base;
6646 -- Start of processing for Build_Derived_Array_Type
6648 begin
6649 if not Is_Constrained (Parent_Type) then
6650 if Nkind (Indic) /= N_Subtype_Indication then
6651 Set_Ekind (Derived_Type, E_Array_Type);
6653 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6654 Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
6656 Set_Has_Delayed_Freeze (Derived_Type, True);
6658 else
6659 Make_Implicit_Base;
6660 Set_Etype (Derived_Type, Implicit_Base);
6662 New_Indic :=
6663 Make_Subtype_Declaration (Loc,
6664 Defining_Identifier => Derived_Type,
6665 Subtype_Indication =>
6666 Make_Subtype_Indication (Loc,
6667 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
6668 Constraint => Constraint (Indic)));
6670 Rewrite (N, New_Indic);
6671 Analyze (N);
6672 end if;
6674 else
6675 if Nkind (Indic) /= N_Subtype_Indication then
6676 Make_Implicit_Base;
6678 Set_Ekind (Derived_Type, Ekind (Parent_Type));
6679 Set_Etype (Derived_Type, Implicit_Base);
6680 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6682 else
6683 Error_Msg_N ("illegal constraint on constrained type", Indic);
6684 end if;
6685 end if;
6687 -- If parent type is not a derived type itself, and is declared in
6688 -- closed scope (e.g. a subprogram), then we must explicitly introduce
6689 -- the new type's concatenation operator since Derive_Subprograms
6690 -- will not inherit the parent's operator. If the parent type is
6691 -- unconstrained, the operator is of the unconstrained base type.
6693 if Number_Dimensions (Parent_Type) = 1
6694 and then not Is_Limited_Type (Parent_Type)
6695 and then not Is_Derived_Type (Parent_Type)
6696 and then not Is_Package_Or_Generic_Package
6697 (Scope (Base_Type (Parent_Type)))
6698 then
6699 if not Is_Constrained (Parent_Type)
6700 and then Is_Constrained (Derived_Type)
6701 then
6702 New_Concatenation_Op (Implicit_Base);
6703 else
6704 New_Concatenation_Op (Derived_Type);
6705 end if;
6706 end if;
6707 end Build_Derived_Array_Type;
6709 -----------------------------------
6710 -- Build_Derived_Concurrent_Type --
6711 -----------------------------------
6713 procedure Build_Derived_Concurrent_Type
6714 (N : Node_Id;
6715 Parent_Type : Entity_Id;
6716 Derived_Type : Entity_Id)
6718 Loc : constant Source_Ptr := Sloc (N);
6720 Corr_Record : constant Entity_Id := Make_Temporary (Loc, 'C');
6721 Corr_Decl : Node_Id;
6722 Corr_Decl_Needed : Boolean;
6723 -- If the derived type has fewer discriminants than its parent, the
6724 -- corresponding record is also a derived type, in order to account for
6725 -- the bound discriminants. We create a full type declaration for it in
6726 -- this case.
6728 Constraint_Present : constant Boolean :=
6729 Nkind (Subtype_Indication (Type_Definition (N))) =
6730 N_Subtype_Indication;
6732 D_Constraint : Node_Id;
6733 New_Constraint : Elist_Id;
6734 Old_Disc : Entity_Id;
6735 New_Disc : Entity_Id;
6736 New_N : Node_Id;
6738 begin
6739 Set_Stored_Constraint (Derived_Type, No_Elist);
6740 Corr_Decl_Needed := False;
6741 Old_Disc := Empty;
6743 if Present (Discriminant_Specifications (N))
6744 and then Constraint_Present
6745 then
6746 Old_Disc := First_Discriminant (Parent_Type);
6747 New_Disc := First (Discriminant_Specifications (N));
6748 while Present (New_Disc) and then Present (Old_Disc) loop
6749 Next_Discriminant (Old_Disc);
6750 Next (New_Disc);
6751 end loop;
6752 end if;
6754 if Present (Old_Disc) and then Expander_Active then
6756 -- The new type has fewer discriminants, so we need to create a new
6757 -- corresponding record, which is derived from the corresponding
6758 -- record of the parent, and has a stored constraint that captures
6759 -- the values of the discriminant constraints. The corresponding
6760 -- record is needed only if expander is active and code generation is
6761 -- enabled.
6763 -- The type declaration for the derived corresponding record has the
6764 -- same discriminant part and constraints as the current declaration.
6765 -- Copy the unanalyzed tree to build declaration.
6767 Corr_Decl_Needed := True;
6768 New_N := Copy_Separate_Tree (N);
6770 Corr_Decl :=
6771 Make_Full_Type_Declaration (Loc,
6772 Defining_Identifier => Corr_Record,
6773 Discriminant_Specifications =>
6774 Discriminant_Specifications (New_N),
6775 Type_Definition =>
6776 Make_Derived_Type_Definition (Loc,
6777 Subtype_Indication =>
6778 Make_Subtype_Indication (Loc,
6779 Subtype_Mark =>
6780 New_Occurrence_Of
6781 (Corresponding_Record_Type (Parent_Type), Loc),
6782 Constraint =>
6783 Constraint
6784 (Subtype_Indication (Type_Definition (New_N))))));
6785 end if;
6787 -- Copy Storage_Size and Relative_Deadline variables if task case
6789 if Is_Task_Type (Parent_Type) then
6790 Set_Storage_Size_Variable (Derived_Type,
6791 Storage_Size_Variable (Parent_Type));
6792 Set_Relative_Deadline_Variable (Derived_Type,
6793 Relative_Deadline_Variable (Parent_Type));
6794 end if;
6796 if Present (Discriminant_Specifications (N)) then
6797 Push_Scope (Derived_Type);
6798 Check_Or_Process_Discriminants (N, Derived_Type);
6800 if Constraint_Present then
6801 New_Constraint :=
6802 Expand_To_Stored_Constraint
6803 (Parent_Type,
6804 Build_Discriminant_Constraints
6805 (Parent_Type,
6806 Subtype_Indication (Type_Definition (N)), True));
6807 end if;
6809 End_Scope;
6811 elsif Constraint_Present then
6813 -- Build constrained subtype, copying the constraint, and derive
6814 -- from it to create a derived constrained type.
6816 declare
6817 Loc : constant Source_Ptr := Sloc (N);
6818 Anon : constant Entity_Id :=
6819 Make_Defining_Identifier (Loc,
6820 Chars => New_External_Name (Chars (Derived_Type), 'T'));
6821 Decl : Node_Id;
6823 begin
6824 Decl :=
6825 Make_Subtype_Declaration (Loc,
6826 Defining_Identifier => Anon,
6827 Subtype_Indication =>
6828 New_Copy_Tree (Subtype_Indication (Type_Definition (N))));
6829 Insert_Before (N, Decl);
6830 Analyze (Decl);
6832 Rewrite (Subtype_Indication (Type_Definition (N)),
6833 New_Occurrence_Of (Anon, Loc));
6834 Set_Analyzed (Derived_Type, False);
6835 Analyze (N);
6836 return;
6837 end;
6838 end if;
6840 -- By default, operations and private data are inherited from parent.
6841 -- However, in the presence of bound discriminants, a new corresponding
6842 -- record will be created, see below.
6844 Set_Has_Discriminants
6845 (Derived_Type, Has_Discriminants (Parent_Type));
6846 Set_Corresponding_Record_Type
6847 (Derived_Type, Corresponding_Record_Type (Parent_Type));
6849 -- Is_Constrained is set according the parent subtype, but is set to
6850 -- False if the derived type is declared with new discriminants.
6852 Set_Is_Constrained
6853 (Derived_Type,
6854 (Is_Constrained (Parent_Type) or else Constraint_Present)
6855 and then not Present (Discriminant_Specifications (N)));
6857 if Constraint_Present then
6858 if not Has_Discriminants (Parent_Type) then
6859 Error_Msg_N ("untagged parent must have discriminants", N);
6861 elsif Present (Discriminant_Specifications (N)) then
6863 -- Verify that new discriminants are used to constrain old ones
6865 D_Constraint :=
6866 First
6867 (Constraints
6868 (Constraint (Subtype_Indication (Type_Definition (N)))));
6870 Old_Disc := First_Discriminant (Parent_Type);
6872 while Present (D_Constraint) loop
6873 if Nkind (D_Constraint) /= N_Discriminant_Association then
6875 -- Positional constraint. If it is a reference to a new
6876 -- discriminant, it constrains the corresponding old one.
6878 if Nkind (D_Constraint) = N_Identifier then
6879 New_Disc := First_Discriminant (Derived_Type);
6880 while Present (New_Disc) loop
6881 exit when Chars (New_Disc) = Chars (D_Constraint);
6882 Next_Discriminant (New_Disc);
6883 end loop;
6885 if Present (New_Disc) then
6886 Set_Corresponding_Discriminant (New_Disc, Old_Disc);
6887 end if;
6888 end if;
6890 Next_Discriminant (Old_Disc);
6892 -- if this is a named constraint, search by name for the old
6893 -- discriminants constrained by the new one.
6895 elsif Nkind (Expression (D_Constraint)) = N_Identifier then
6897 -- Find new discriminant with that name
6899 New_Disc := First_Discriminant (Derived_Type);
6900 while Present (New_Disc) loop
6901 exit when
6902 Chars (New_Disc) = Chars (Expression (D_Constraint));
6903 Next_Discriminant (New_Disc);
6904 end loop;
6906 if Present (New_Disc) then
6908 -- Verify that new discriminant renames some discriminant
6909 -- of the parent type, and associate the new discriminant
6910 -- with one or more old ones that it renames.
6912 declare
6913 Selector : Node_Id;
6915 begin
6916 Selector := First (Selector_Names (D_Constraint));
6917 while Present (Selector) loop
6918 Old_Disc := First_Discriminant (Parent_Type);
6919 while Present (Old_Disc) loop
6920 exit when Chars (Old_Disc) = Chars (Selector);
6921 Next_Discriminant (Old_Disc);
6922 end loop;
6924 if Present (Old_Disc) then
6925 Set_Corresponding_Discriminant
6926 (New_Disc, Old_Disc);
6927 end if;
6929 Next (Selector);
6930 end loop;
6931 end;
6932 end if;
6933 end if;
6935 Next (D_Constraint);
6936 end loop;
6938 New_Disc := First_Discriminant (Derived_Type);
6939 while Present (New_Disc) loop
6940 if No (Corresponding_Discriminant (New_Disc)) then
6941 Error_Msg_NE
6942 ("new discriminant& must constrain old one", N, New_Disc);
6944 elsif not
6945 Subtypes_Statically_Compatible
6946 (Etype (New_Disc),
6947 Etype (Corresponding_Discriminant (New_Disc)))
6948 then
6949 Error_Msg_NE
6950 ("& not statically compatible with parent discriminant",
6951 N, New_Disc);
6952 end if;
6954 Next_Discriminant (New_Disc);
6955 end loop;
6956 end if;
6958 elsif Present (Discriminant_Specifications (N)) then
6959 Error_Msg_N
6960 ("missing discriminant constraint in untagged derivation", N);
6961 end if;
6963 -- The entity chain of the derived type includes the new discriminants
6964 -- but shares operations with the parent.
6966 if Present (Discriminant_Specifications (N)) then
6967 Old_Disc := First_Discriminant (Parent_Type);
6968 while Present (Old_Disc) loop
6969 if No (Next_Entity (Old_Disc))
6970 or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
6971 then
6972 Set_Next_Entity
6973 (Last_Entity (Derived_Type), Next_Entity (Old_Disc));
6974 exit;
6975 end if;
6977 Next_Discriminant (Old_Disc);
6978 end loop;
6980 else
6981 Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
6982 if Has_Discriminants (Parent_Type) then
6983 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
6984 Set_Discriminant_Constraint (
6985 Derived_Type, Discriminant_Constraint (Parent_Type));
6986 end if;
6987 end if;
6989 Set_Last_Entity (Derived_Type, Last_Entity (Parent_Type));
6991 Set_Has_Completion (Derived_Type);
6993 if Corr_Decl_Needed then
6994 Set_Stored_Constraint (Derived_Type, New_Constraint);
6995 Insert_After (N, Corr_Decl);
6996 Analyze (Corr_Decl);
6997 Set_Corresponding_Record_Type (Derived_Type, Corr_Record);
6998 end if;
6999 end Build_Derived_Concurrent_Type;
7001 ------------------------------------
7002 -- Build_Derived_Enumeration_Type --
7003 ------------------------------------
7005 procedure Build_Derived_Enumeration_Type
7006 (N : Node_Id;
7007 Parent_Type : Entity_Id;
7008 Derived_Type : Entity_Id)
7010 Loc : constant Source_Ptr := Sloc (N);
7011 Def : constant Node_Id := Type_Definition (N);
7012 Indic : constant Node_Id := Subtype_Indication (Def);
7013 Implicit_Base : Entity_Id;
7014 Literal : Entity_Id;
7015 New_Lit : Entity_Id;
7016 Literals_List : List_Id;
7017 Type_Decl : Node_Id;
7018 Hi, Lo : Node_Id;
7019 Rang_Expr : Node_Id;
7021 begin
7022 -- Since types Standard.Character and Standard.[Wide_]Wide_Character do
7023 -- not have explicit literals lists we need to process types derived
7024 -- from them specially. This is handled by Derived_Standard_Character.
7025 -- If the parent type is a generic type, there are no literals either,
7026 -- and we construct the same skeletal representation as for the generic
7027 -- parent type.
7029 if Is_Standard_Character_Type (Parent_Type) then
7030 Derived_Standard_Character (N, Parent_Type, Derived_Type);
7032 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
7033 declare
7034 Lo : Node_Id;
7035 Hi : Node_Id;
7037 begin
7038 if Nkind (Indic) /= N_Subtype_Indication then
7039 Lo :=
7040 Make_Attribute_Reference (Loc,
7041 Attribute_Name => Name_First,
7042 Prefix => New_Occurrence_Of (Derived_Type, Loc));
7043 Set_Etype (Lo, Derived_Type);
7045 Hi :=
7046 Make_Attribute_Reference (Loc,
7047 Attribute_Name => Name_Last,
7048 Prefix => New_Occurrence_Of (Derived_Type, Loc));
7049 Set_Etype (Hi, Derived_Type);
7051 Set_Scalar_Range (Derived_Type,
7052 Make_Range (Loc,
7053 Low_Bound => Lo,
7054 High_Bound => Hi));
7055 else
7057 -- Analyze subtype indication and verify compatibility
7058 -- with parent type.
7060 if Base_Type (Process_Subtype (Indic, N)) /=
7061 Base_Type (Parent_Type)
7062 then
7063 Error_Msg_N
7064 ("illegal constraint for formal discrete type", N);
7065 end if;
7066 end if;
7067 end;
7069 else
7070 -- If a constraint is present, analyze the bounds to catch
7071 -- premature usage of the derived literals.
7073 if Nkind (Indic) = N_Subtype_Indication
7074 and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
7075 then
7076 Analyze (Low_Bound (Range_Expression (Constraint (Indic))));
7077 Analyze (High_Bound (Range_Expression (Constraint (Indic))));
7078 end if;
7080 -- Introduce an implicit base type for the derived type even if there
7081 -- is no constraint attached to it, since this seems closer to the
7082 -- Ada semantics. Build a full type declaration tree for the derived
7083 -- type using the implicit base type as the defining identifier. The
7084 -- build a subtype declaration tree which applies the constraint (if
7085 -- any) have it replace the derived type declaration.
7087 Literal := First_Literal (Parent_Type);
7088 Literals_List := New_List;
7089 while Present (Literal)
7090 and then Ekind (Literal) = E_Enumeration_Literal
7091 loop
7092 -- Literals of the derived type have the same representation as
7093 -- those of the parent type, but this representation can be
7094 -- overridden by an explicit representation clause. Indicate
7095 -- that there is no explicit representation given yet. These
7096 -- derived literals are implicit operations of the new type,
7097 -- and can be overridden by explicit ones.
7099 if Nkind (Literal) = N_Defining_Character_Literal then
7100 New_Lit :=
7101 Make_Defining_Character_Literal (Loc, Chars (Literal));
7102 else
7103 New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
7104 end if;
7106 Set_Ekind (New_Lit, E_Enumeration_Literal);
7107 Set_Enumeration_Pos (New_Lit, Enumeration_Pos (Literal));
7108 Set_Enumeration_Rep (New_Lit, Enumeration_Rep (Literal));
7109 Set_Enumeration_Rep_Expr (New_Lit, Empty);
7110 Set_Alias (New_Lit, Literal);
7111 Set_Is_Known_Valid (New_Lit, True);
7113 Append (New_Lit, Literals_List);
7114 Next_Literal (Literal);
7115 end loop;
7117 Implicit_Base :=
7118 Make_Defining_Identifier (Sloc (Derived_Type),
7119 Chars => New_External_Name (Chars (Derived_Type), 'B'));
7121 -- Indicate the proper nature of the derived type. This must be done
7122 -- before analysis of the literals, to recognize cases when a literal
7123 -- may be hidden by a previous explicit function definition (cf.
7124 -- c83031a).
7126 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
7127 Set_Etype (Derived_Type, Implicit_Base);
7129 Type_Decl :=
7130 Make_Full_Type_Declaration (Loc,
7131 Defining_Identifier => Implicit_Base,
7132 Discriminant_Specifications => No_List,
7133 Type_Definition =>
7134 Make_Enumeration_Type_Definition (Loc, Literals_List));
7136 Mark_Rewrite_Insertion (Type_Decl);
7137 Insert_Before (N, Type_Decl);
7138 Analyze (Type_Decl);
7140 -- The anonymous base now has a full declaration, but this base
7141 -- is not a first subtype.
7143 Set_Is_First_Subtype (Implicit_Base, False);
7145 -- After the implicit base is analyzed its Etype needs to be changed
7146 -- to reflect the fact that it is derived from the parent type which
7147 -- was ignored during analysis. We also set the size at this point.
7149 Set_Etype (Implicit_Base, Parent_Type);
7151 Set_Size_Info (Implicit_Base, Parent_Type);
7152 Set_RM_Size (Implicit_Base, RM_Size (Parent_Type));
7153 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
7155 -- Copy other flags from parent type
7157 Set_Has_Non_Standard_Rep
7158 (Implicit_Base, Has_Non_Standard_Rep
7159 (Parent_Type));
7160 Set_Has_Pragma_Ordered
7161 (Implicit_Base, Has_Pragma_Ordered
7162 (Parent_Type));
7163 Set_Has_Delayed_Freeze (Implicit_Base);
7165 -- Process the subtype indication including a validation check on the
7166 -- constraint, if any. If a constraint is given, its bounds must be
7167 -- implicitly converted to the new type.
7169 if Nkind (Indic) = N_Subtype_Indication then
7170 declare
7171 R : constant Node_Id :=
7172 Range_Expression (Constraint (Indic));
7174 begin
7175 if Nkind (R) = N_Range then
7176 Hi := Build_Scalar_Bound
7177 (High_Bound (R), Parent_Type, Implicit_Base);
7178 Lo := Build_Scalar_Bound
7179 (Low_Bound (R), Parent_Type, Implicit_Base);
7181 else
7182 -- Constraint is a Range attribute. Replace with explicit
7183 -- mention of the bounds of the prefix, which must be a
7184 -- subtype.
7186 Analyze (Prefix (R));
7187 Hi :=
7188 Convert_To (Implicit_Base,
7189 Make_Attribute_Reference (Loc,
7190 Attribute_Name => Name_Last,
7191 Prefix =>
7192 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
7194 Lo :=
7195 Convert_To (Implicit_Base,
7196 Make_Attribute_Reference (Loc,
7197 Attribute_Name => Name_First,
7198 Prefix =>
7199 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
7200 end if;
7201 end;
7203 else
7204 Hi :=
7205 Build_Scalar_Bound
7206 (Type_High_Bound (Parent_Type),
7207 Parent_Type, Implicit_Base);
7208 Lo :=
7209 Build_Scalar_Bound
7210 (Type_Low_Bound (Parent_Type),
7211 Parent_Type, Implicit_Base);
7212 end if;
7214 Rang_Expr :=
7215 Make_Range (Loc,
7216 Low_Bound => Lo,
7217 High_Bound => Hi);
7219 -- If we constructed a default range for the case where no range
7220 -- was given, then the expressions in the range must not freeze
7221 -- since they do not correspond to expressions in the source.
7222 -- However, if the type inherits predicates the expressions will
7223 -- be elaborated earlier and must freeze.
7225 if Nkind (Indic) /= N_Subtype_Indication
7226 and then not Has_Predicates (Derived_Type)
7227 then
7228 Set_Must_Not_Freeze (Lo);
7229 Set_Must_Not_Freeze (Hi);
7230 Set_Must_Not_Freeze (Rang_Expr);
7231 end if;
7233 Rewrite (N,
7234 Make_Subtype_Declaration (Loc,
7235 Defining_Identifier => Derived_Type,
7236 Subtype_Indication =>
7237 Make_Subtype_Indication (Loc,
7238 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
7239 Constraint =>
7240 Make_Range_Constraint (Loc,
7241 Range_Expression => Rang_Expr))));
7243 Analyze (N);
7245 -- Propagate the aspects from the original type declaration to the
7246 -- declaration of the implicit base.
7248 Move_Aspects (From => Original_Node (N), To => Type_Decl);
7250 -- Apply a range check. Since this range expression doesn't have an
7251 -- Etype, we have to specifically pass the Source_Typ parameter. Is
7252 -- this right???
7254 if Nkind (Indic) = N_Subtype_Indication then
7255 Apply_Range_Check
7256 (Range_Expression (Constraint (Indic)), Parent_Type,
7257 Source_Typ => Entity (Subtype_Mark (Indic)));
7258 end if;
7259 end if;
7260 end Build_Derived_Enumeration_Type;
7262 --------------------------------
7263 -- Build_Derived_Numeric_Type --
7264 --------------------------------
7266 procedure Build_Derived_Numeric_Type
7267 (N : Node_Id;
7268 Parent_Type : Entity_Id;
7269 Derived_Type : Entity_Id)
7271 Loc : constant Source_Ptr := Sloc (N);
7272 Tdef : constant Node_Id := Type_Definition (N);
7273 Indic : constant Node_Id := Subtype_Indication (Tdef);
7274 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
7275 No_Constraint : constant Boolean := Nkind (Indic) /=
7276 N_Subtype_Indication;
7277 Implicit_Base : Entity_Id;
7279 Lo : Node_Id;
7280 Hi : Node_Id;
7282 begin
7283 -- Process the subtype indication including a validation check on
7284 -- the constraint if any.
7286 Discard_Node (Process_Subtype (Indic, N));
7288 -- Introduce an implicit base type for the derived type even if there
7289 -- is no constraint attached to it, since this seems closer to the Ada
7290 -- semantics.
7292 Implicit_Base :=
7293 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
7295 Set_Etype (Implicit_Base, Parent_Base);
7296 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
7297 Set_Size_Info (Implicit_Base, Parent_Base);
7298 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
7299 Set_Parent (Implicit_Base, Parent (Derived_Type));
7300 Set_Is_Known_Valid (Implicit_Base, Is_Known_Valid (Parent_Base));
7302 -- Set RM Size for discrete type or decimal fixed-point type
7303 -- Ordinary fixed-point is excluded, why???
7305 if Is_Discrete_Type (Parent_Base)
7306 or else Is_Decimal_Fixed_Point_Type (Parent_Base)
7307 then
7308 Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
7309 end if;
7311 Set_Has_Delayed_Freeze (Implicit_Base);
7313 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
7314 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
7316 Set_Scalar_Range (Implicit_Base,
7317 Make_Range (Loc,
7318 Low_Bound => Lo,
7319 High_Bound => Hi));
7321 if Has_Infinities (Parent_Base) then
7322 Set_Includes_Infinities (Scalar_Range (Implicit_Base));
7323 end if;
7325 -- The Derived_Type, which is the entity of the declaration, is a
7326 -- subtype of the implicit base. Its Ekind is a subtype, even in the
7327 -- absence of an explicit constraint.
7329 Set_Etype (Derived_Type, Implicit_Base);
7331 -- If we did not have a constraint, then the Ekind is set from the
7332 -- parent type (otherwise Process_Subtype has set the bounds)
7334 if No_Constraint then
7335 Set_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
7336 end if;
7338 -- If we did not have a range constraint, then set the range from the
7339 -- parent type. Otherwise, the Process_Subtype call has set the bounds.
7341 if No_Constraint or else not Has_Range_Constraint (Indic) then
7342 Set_Scalar_Range (Derived_Type,
7343 Make_Range (Loc,
7344 Low_Bound => New_Copy_Tree (Type_Low_Bound (Parent_Type)),
7345 High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
7346 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7348 if Has_Infinities (Parent_Type) then
7349 Set_Includes_Infinities (Scalar_Range (Derived_Type));
7350 end if;
7352 Set_Is_Known_Valid (Derived_Type, Is_Known_Valid (Parent_Type));
7353 end if;
7355 Set_Is_Descendant_Of_Address (Derived_Type,
7356 Is_Descendant_Of_Address (Parent_Type));
7357 Set_Is_Descendant_Of_Address (Implicit_Base,
7358 Is_Descendant_Of_Address (Parent_Type));
7360 -- Set remaining type-specific fields, depending on numeric type
7362 if Is_Modular_Integer_Type (Parent_Type) then
7363 Set_Modulus (Implicit_Base, Modulus (Parent_Base));
7365 Set_Non_Binary_Modulus
7366 (Implicit_Base, Non_Binary_Modulus (Parent_Base));
7368 Set_Is_Known_Valid
7369 (Implicit_Base, Is_Known_Valid (Parent_Base));
7371 elsif Is_Floating_Point_Type (Parent_Type) then
7373 -- Digits of base type is always copied from the digits value of
7374 -- the parent base type, but the digits of the derived type will
7375 -- already have been set if there was a constraint present.
7377 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
7378 Set_Float_Rep (Implicit_Base, Float_Rep (Parent_Base));
7380 if No_Constraint then
7381 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
7382 end if;
7384 elsif Is_Fixed_Point_Type (Parent_Type) then
7386 -- Small of base type and derived type are always copied from the
7387 -- parent base type, since smalls never change. The delta of the
7388 -- base type is also copied from the parent base type. However the
7389 -- delta of the derived type will have been set already if a
7390 -- constraint was present.
7392 Set_Small_Value (Derived_Type, Small_Value (Parent_Base));
7393 Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
7394 Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
7396 if No_Constraint then
7397 Set_Delta_Value (Derived_Type, Delta_Value (Parent_Type));
7398 end if;
7400 -- The scale and machine radix in the decimal case are always
7401 -- copied from the parent base type.
7403 if Is_Decimal_Fixed_Point_Type (Parent_Type) then
7404 Set_Scale_Value (Derived_Type, Scale_Value (Parent_Base));
7405 Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
7407 Set_Machine_Radix_10
7408 (Derived_Type, Machine_Radix_10 (Parent_Base));
7409 Set_Machine_Radix_10
7410 (Implicit_Base, Machine_Radix_10 (Parent_Base));
7412 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
7414 if No_Constraint then
7415 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
7417 else
7418 -- the analysis of the subtype_indication sets the
7419 -- digits value of the derived type.
7421 null;
7422 end if;
7423 end if;
7424 end if;
7426 if Is_Integer_Type (Parent_Type) then
7427 Set_Has_Shift_Operator
7428 (Implicit_Base, Has_Shift_Operator (Parent_Type));
7429 end if;
7431 -- The type of the bounds is that of the parent type, and they
7432 -- must be converted to the derived type.
7434 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
7436 -- The implicit_base should be frozen when the derived type is frozen,
7437 -- but note that it is used in the conversions of the bounds. For fixed
7438 -- types we delay the determination of the bounds until the proper
7439 -- freezing point. For other numeric types this is rejected by GCC, for
7440 -- reasons that are currently unclear (???), so we choose to freeze the
7441 -- implicit base now. In the case of integers and floating point types
7442 -- this is harmless because subsequent representation clauses cannot
7443 -- affect anything, but it is still baffling that we cannot use the
7444 -- same mechanism for all derived numeric types.
7446 -- There is a further complication: actually some representation
7447 -- clauses can affect the implicit base type. For example, attribute
7448 -- definition clauses for stream-oriented attributes need to set the
7449 -- corresponding TSS entries on the base type, and this normally
7450 -- cannot be done after the base type is frozen, so the circuitry in
7451 -- Sem_Ch13.New_Stream_Subprogram must account for this possibility
7452 -- and not use Set_TSS in this case.
7454 -- There are also consequences for the case of delayed representation
7455 -- aspects for some cases. For example, a Size aspect is delayed and
7456 -- should not be evaluated to the freeze point. This early freezing
7457 -- means that the size attribute evaluation happens too early???
7459 if Is_Fixed_Point_Type (Parent_Type) then
7460 Conditional_Delay (Implicit_Base, Parent_Type);
7461 else
7462 Freeze_Before (N, Implicit_Base);
7463 end if;
7464 end Build_Derived_Numeric_Type;
7466 --------------------------------
7467 -- Build_Derived_Private_Type --
7468 --------------------------------
7470 procedure Build_Derived_Private_Type
7471 (N : Node_Id;
7472 Parent_Type : Entity_Id;
7473 Derived_Type : Entity_Id;
7474 Is_Completion : Boolean;
7475 Derive_Subps : Boolean := True)
7477 Loc : constant Source_Ptr := Sloc (N);
7478 Par_Base : constant Entity_Id := Base_Type (Parent_Type);
7479 Par_Scope : constant Entity_Id := Scope (Par_Base);
7480 Full_N : constant Node_Id := New_Copy_Tree (N);
7481 Full_Der : Entity_Id := New_Copy (Derived_Type);
7482 Full_P : Entity_Id;
7484 procedure Build_Full_Derivation;
7485 -- Build full derivation, i.e. derive from the full view
7487 procedure Copy_And_Build;
7488 -- Copy derived type declaration, replace parent with its full view,
7489 -- and build derivation
7491 ---------------------------
7492 -- Build_Full_Derivation --
7493 ---------------------------
7495 procedure Build_Full_Derivation is
7496 begin
7497 -- If parent scope is not open, install the declarations
7499 if not In_Open_Scopes (Par_Scope) then
7500 Install_Private_Declarations (Par_Scope);
7501 Install_Visible_Declarations (Par_Scope);
7502 Copy_And_Build;
7503 Uninstall_Declarations (Par_Scope);
7505 -- If parent scope is open and in another unit, and parent has a
7506 -- completion, then the derivation is taking place in the visible
7507 -- part of a child unit. In that case retrieve the full view of
7508 -- the parent momentarily.
7510 elsif not In_Same_Source_Unit (N, Parent_Type) then
7511 Full_P := Full_View (Parent_Type);
7512 Exchange_Declarations (Parent_Type);
7513 Copy_And_Build;
7514 Exchange_Declarations (Full_P);
7516 -- Otherwise it is a local derivation
7518 else
7519 Copy_And_Build;
7520 end if;
7521 end Build_Full_Derivation;
7523 --------------------
7524 -- Copy_And_Build --
7525 --------------------
7527 procedure Copy_And_Build is
7528 Full_Parent : Entity_Id := Parent_Type;
7530 begin
7531 -- If the parent is itself derived from another private type,
7532 -- installing the private declarations has not affected its
7533 -- privacy status, so use its own full view explicitly.
7535 if Is_Private_Type (Full_Parent)
7536 and then Present (Full_View (Full_Parent))
7537 then
7538 Full_Parent := Full_View (Full_Parent);
7539 end if;
7541 -- And its underlying full view if necessary
7543 if Is_Private_Type (Full_Parent)
7544 and then Present (Underlying_Full_View (Full_Parent))
7545 then
7546 Full_Parent := Underlying_Full_View (Full_Parent);
7547 end if;
7549 -- For record, access and most enumeration types, derivation from
7550 -- the full view requires a fully-fledged declaration. In the other
7551 -- cases, just use an itype.
7553 if Ekind (Full_Parent) in Record_Kind
7554 or else Ekind (Full_Parent) in Access_Kind
7555 or else
7556 (Ekind (Full_Parent) in Enumeration_Kind
7557 and then not Is_Standard_Character_Type (Full_Parent)
7558 and then not Is_Generic_Type (Root_Type (Full_Parent)))
7559 then
7560 -- Copy and adjust declaration to provide a completion for what
7561 -- is originally a private declaration. Indicate that full view
7562 -- is internally generated.
7564 Set_Comes_From_Source (Full_N, False);
7565 Set_Comes_From_Source (Full_Der, False);
7566 Set_Parent (Full_Der, Full_N);
7567 Set_Defining_Identifier (Full_N, Full_Der);
7569 -- If there are no constraints, adjust the subtype mark
7571 if Nkind (Subtype_Indication (Type_Definition (Full_N))) /=
7572 N_Subtype_Indication
7573 then
7574 Set_Subtype_Indication
7575 (Type_Definition (Full_N),
7576 New_Occurrence_Of (Full_Parent, Sloc (Full_N)));
7577 end if;
7579 Insert_After (N, Full_N);
7581 -- Build full view of derived type from full view of parent which
7582 -- is now installed. Subprograms have been derived on the partial
7583 -- view, the completion does not derive them anew.
7585 if Ekind (Full_Parent) in Record_Kind then
7587 -- If parent type is tagged, the completion inherits the proper
7588 -- primitive operations.
7590 if Is_Tagged_Type (Parent_Type) then
7591 Build_Derived_Record_Type
7592 (Full_N, Full_Parent, Full_Der, Derive_Subps);
7593 else
7594 Build_Derived_Record_Type
7595 (Full_N, Full_Parent, Full_Der, Derive_Subps => False);
7596 end if;
7598 else
7599 Build_Derived_Type
7600 (Full_N, Full_Parent, Full_Der,
7601 Is_Completion => False, Derive_Subps => False);
7602 end if;
7604 -- The full declaration has been introduced into the tree and
7605 -- processed in the step above. It should not be analyzed again
7606 -- (when encountered later in the current list of declarations)
7607 -- to prevent spurious name conflicts. The full entity remains
7608 -- invisible.
7610 Set_Analyzed (Full_N);
7612 else
7613 Full_Der :=
7614 Make_Defining_Identifier (Sloc (Derived_Type),
7615 Chars => Chars (Derived_Type));
7616 Set_Is_Itype (Full_Der);
7617 Set_Associated_Node_For_Itype (Full_Der, N);
7618 Set_Parent (Full_Der, N);
7619 Build_Derived_Type
7620 (N, Full_Parent, Full_Der,
7621 Is_Completion => False, Derive_Subps => False);
7622 end if;
7624 Set_Has_Private_Declaration (Full_Der);
7625 Set_Has_Private_Declaration (Derived_Type);
7627 Set_Scope (Full_Der, Scope (Derived_Type));
7628 Set_Is_First_Subtype (Full_Der, Is_First_Subtype (Derived_Type));
7629 Set_Has_Size_Clause (Full_Der, False);
7630 Set_Has_Alignment_Clause (Full_Der, False);
7631 Set_Has_Delayed_Freeze (Full_Der);
7632 Set_Is_Frozen (Full_Der, False);
7633 Set_Freeze_Node (Full_Der, Empty);
7634 Set_Depends_On_Private (Full_Der, Has_Private_Component (Full_Der));
7635 Set_Is_Public (Full_Der, Is_Public (Derived_Type));
7637 -- The convention on the base type may be set in the private part
7638 -- and not propagated to the subtype until later, so we obtain the
7639 -- convention from the base type of the parent.
7641 Set_Convention (Full_Der, Convention (Base_Type (Full_Parent)));
7642 end Copy_And_Build;
7644 -- Start of processing for Build_Derived_Private_Type
7646 begin
7647 if Is_Tagged_Type (Parent_Type) then
7648 Full_P := Full_View (Parent_Type);
7650 -- A type extension of a type with unknown discriminants is an
7651 -- indefinite type that the back-end cannot handle directly.
7652 -- We treat it as a private type, and build a completion that is
7653 -- derived from the full view of the parent, and hopefully has
7654 -- known discriminants.
7656 -- If the full view of the parent type has an underlying record view,
7657 -- use it to generate the underlying record view of this derived type
7658 -- (required for chains of derivations with unknown discriminants).
7660 -- Minor optimization: we avoid the generation of useless underlying
7661 -- record view entities if the private type declaration has unknown
7662 -- discriminants but its corresponding full view has no
7663 -- discriminants.
7665 if Has_Unknown_Discriminants (Parent_Type)
7666 and then Present (Full_P)
7667 and then (Has_Discriminants (Full_P)
7668 or else Present (Underlying_Record_View (Full_P)))
7669 and then not In_Open_Scopes (Par_Scope)
7670 and then Expander_Active
7671 then
7672 declare
7673 Full_Der : constant Entity_Id := Make_Temporary (Loc, 'T');
7674 New_Ext : constant Node_Id :=
7675 Copy_Separate_Tree
7676 (Record_Extension_Part (Type_Definition (N)));
7677 Decl : Node_Id;
7679 begin
7680 Build_Derived_Record_Type
7681 (N, Parent_Type, Derived_Type, Derive_Subps);
7683 -- Build anonymous completion, as a derivation from the full
7684 -- view of the parent. This is not a completion in the usual
7685 -- sense, because the current type is not private.
7687 Decl :=
7688 Make_Full_Type_Declaration (Loc,
7689 Defining_Identifier => Full_Der,
7690 Type_Definition =>
7691 Make_Derived_Type_Definition (Loc,
7692 Subtype_Indication =>
7693 New_Copy_Tree
7694 (Subtype_Indication (Type_Definition (N))),
7695 Record_Extension_Part => New_Ext));
7697 -- If the parent type has an underlying record view, use it
7698 -- here to build the new underlying record view.
7700 if Present (Underlying_Record_View (Full_P)) then
7701 pragma Assert
7702 (Nkind (Subtype_Indication (Type_Definition (Decl)))
7703 = N_Identifier);
7704 Set_Entity (Subtype_Indication (Type_Definition (Decl)),
7705 Underlying_Record_View (Full_P));
7706 end if;
7708 Install_Private_Declarations (Par_Scope);
7709 Install_Visible_Declarations (Par_Scope);
7710 Insert_Before (N, Decl);
7712 -- Mark entity as an underlying record view before analysis,
7713 -- to avoid generating the list of its primitive operations
7714 -- (which is not really required for this entity) and thus
7715 -- prevent spurious errors associated with missing overriding
7716 -- of abstract primitives (overridden only for Derived_Type).
7718 Set_Ekind (Full_Der, E_Record_Type);
7719 Set_Is_Underlying_Record_View (Full_Der);
7720 Set_Default_SSO (Full_Der);
7721 Set_No_Reordering (Full_Der, No_Component_Reordering);
7723 Analyze (Decl);
7725 pragma Assert (Has_Discriminants (Full_Der)
7726 and then not Has_Unknown_Discriminants (Full_Der));
7728 Uninstall_Declarations (Par_Scope);
7730 -- Freeze the underlying record view, to prevent generation of
7731 -- useless dispatching information, which is simply shared with
7732 -- the real derived type.
7734 Set_Is_Frozen (Full_Der);
7736 -- If the derived type has access discriminants, create
7737 -- references to their anonymous types now, to prevent
7738 -- back-end problems when their first use is in generated
7739 -- bodies of primitives.
7741 declare
7742 E : Entity_Id;
7744 begin
7745 E := First_Entity (Full_Der);
7747 while Present (E) loop
7748 if Ekind (E) = E_Discriminant
7749 and then Ekind (Etype (E)) = E_Anonymous_Access_Type
7750 then
7751 Build_Itype_Reference (Etype (E), Decl);
7752 end if;
7754 Next_Entity (E);
7755 end loop;
7756 end;
7758 -- Set up links between real entity and underlying record view
7760 Set_Underlying_Record_View (Derived_Type, Base_Type (Full_Der));
7761 Set_Underlying_Record_View (Base_Type (Full_Der), Derived_Type);
7762 end;
7764 -- If discriminants are known, build derived record
7766 else
7767 Build_Derived_Record_Type
7768 (N, Parent_Type, Derived_Type, Derive_Subps);
7769 end if;
7771 return;
7773 elsif Has_Discriminants (Parent_Type) then
7775 -- Build partial view of derived type from partial view of parent.
7776 -- This must be done before building the full derivation because the
7777 -- second derivation will modify the discriminants of the first and
7778 -- the discriminants are chained with the rest of the components in
7779 -- the full derivation.
7781 Build_Derived_Record_Type
7782 (N, Parent_Type, Derived_Type, Derive_Subps);
7784 -- Build the full derivation if this is not the anonymous derived
7785 -- base type created by Build_Derived_Record_Type in the constrained
7786 -- case (see point 5. of its head comment) since we build it for the
7787 -- derived subtype. And skip it for protected types altogether, as
7788 -- gigi does not use these types directly.
7790 if Present (Full_View (Parent_Type))
7791 and then not Is_Itype (Derived_Type)
7792 and then not (Ekind (Full_View (Parent_Type)) in Protected_Kind)
7793 then
7794 declare
7795 Der_Base : constant Entity_Id := Base_Type (Derived_Type);
7796 Discr : Entity_Id;
7797 Last_Discr : Entity_Id;
7799 begin
7800 -- If this is not a completion, construct the implicit full
7801 -- view by deriving from the full view of the parent type.
7802 -- But if this is a completion, the derived private type
7803 -- being built is a full view and the full derivation can
7804 -- only be its underlying full view.
7806 Build_Full_Derivation;
7808 if not Is_Completion then
7809 Set_Full_View (Derived_Type, Full_Der);
7810 else
7811 Set_Underlying_Full_View (Derived_Type, Full_Der);
7812 Set_Is_Underlying_Full_View (Full_Der);
7813 end if;
7815 if not Is_Base_Type (Derived_Type) then
7816 Set_Full_View (Der_Base, Base_Type (Full_Der));
7817 end if;
7819 -- Copy the discriminant list from full view to the partial
7820 -- view (base type and its subtype). Gigi requires that the
7821 -- partial and full views have the same discriminants.
7823 -- Note that since the partial view points to discriminants
7824 -- in the full view, their scope will be that of the full
7825 -- view. This might cause some front end problems and need
7826 -- adjustment???
7828 Discr := First_Discriminant (Base_Type (Full_Der));
7829 Set_First_Entity (Der_Base, Discr);
7831 loop
7832 Last_Discr := Discr;
7833 Next_Discriminant (Discr);
7834 exit when No (Discr);
7835 end loop;
7837 Set_Last_Entity (Der_Base, Last_Discr);
7838 Set_First_Entity (Derived_Type, First_Entity (Der_Base));
7839 Set_Last_Entity (Derived_Type, Last_Entity (Der_Base));
7840 end;
7841 end if;
7843 elsif Present (Full_View (Parent_Type))
7844 and then Has_Discriminants (Full_View (Parent_Type))
7845 then
7846 if Has_Unknown_Discriminants (Parent_Type)
7847 and then Nkind (Subtype_Indication (Type_Definition (N))) =
7848 N_Subtype_Indication
7849 then
7850 Error_Msg_N
7851 ("cannot constrain type with unknown discriminants",
7852 Subtype_Indication (Type_Definition (N)));
7853 return;
7854 end if;
7856 -- If this is not a completion, construct the implicit full view by
7857 -- deriving from the full view of the parent type. But if this is a
7858 -- completion, the derived private type being built is a full view
7859 -- and the full derivation can only be its underlying full view.
7861 Build_Full_Derivation;
7863 if not Is_Completion then
7864 Set_Full_View (Derived_Type, Full_Der);
7865 else
7866 Set_Underlying_Full_View (Derived_Type, Full_Der);
7867 Set_Is_Underlying_Full_View (Full_Der);
7868 end if;
7870 -- In any case, the primitive operations are inherited from the
7871 -- parent type, not from the internal full view.
7873 Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
7875 if Derive_Subps then
7876 Derive_Subprograms (Parent_Type, Derived_Type);
7877 end if;
7879 Set_Stored_Constraint (Derived_Type, No_Elist);
7880 Set_Is_Constrained
7881 (Derived_Type, Is_Constrained (Full_View (Parent_Type)));
7883 else
7884 -- Untagged type, No discriminants on either view
7886 if Nkind (Subtype_Indication (Type_Definition (N))) =
7887 N_Subtype_Indication
7888 then
7889 Error_Msg_N
7890 ("illegal constraint on type without discriminants", N);
7891 end if;
7893 if Present (Discriminant_Specifications (N))
7894 and then Present (Full_View (Parent_Type))
7895 and then not Is_Tagged_Type (Full_View (Parent_Type))
7896 then
7897 Error_Msg_N ("cannot add discriminants to untagged type", N);
7898 end if;
7900 Set_Stored_Constraint (Derived_Type, No_Elist);
7901 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7902 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
7903 Set_Disable_Controlled (Derived_Type, Disable_Controlled
7904 (Parent_Type));
7905 Set_Has_Controlled_Component
7906 (Derived_Type, Has_Controlled_Component
7907 (Parent_Type));
7909 -- Direct controlled types do not inherit Finalize_Storage_Only flag
7911 if not Is_Controlled_Active (Parent_Type) then
7912 Set_Finalize_Storage_Only
7913 (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
7914 end if;
7916 -- If this is not a completion, construct the implicit full view by
7917 -- deriving from the full view of the parent type.
7919 -- ??? If the parent is untagged private and its completion is
7920 -- tagged, this mechanism will not work because we cannot derive from
7921 -- the tagged full view unless we have an extension.
7923 if Present (Full_View (Parent_Type))
7924 and then not Is_Tagged_Type (Full_View (Parent_Type))
7925 and then not Is_Completion
7926 then
7927 Build_Full_Derivation;
7928 Set_Full_View (Derived_Type, Full_Der);
7929 end if;
7930 end if;
7932 Set_Has_Unknown_Discriminants (Derived_Type,
7933 Has_Unknown_Discriminants (Parent_Type));
7935 if Is_Private_Type (Derived_Type) then
7936 Set_Private_Dependents (Derived_Type, New_Elmt_List);
7937 end if;
7939 -- If the parent base type is in scope, add the derived type to its
7940 -- list of private dependents, because its full view may become
7941 -- visible subsequently (in a nested private part, a body, or in a
7942 -- further child unit).
7944 if Is_Private_Type (Par_Base) and then In_Open_Scopes (Par_Scope) then
7945 Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
7947 -- Check for unusual case where a type completed by a private
7948 -- derivation occurs within a package nested in a child unit, and
7949 -- the parent is declared in an ancestor.
7951 if Is_Child_Unit (Scope (Current_Scope))
7952 and then Is_Completion
7953 and then In_Private_Part (Current_Scope)
7954 and then Scope (Parent_Type) /= Current_Scope
7956 -- Note that if the parent has a completion in the private part,
7957 -- (which is itself a derivation from some other private type)
7958 -- it is that completion that is visible, there is no full view
7959 -- available, and no special processing is needed.
7961 and then Present (Full_View (Parent_Type))
7962 then
7963 -- In this case, the full view of the parent type will become
7964 -- visible in the body of the enclosing child, and only then will
7965 -- the current type be possibly non-private. Build an underlying
7966 -- full view that will be installed when the enclosing child body
7967 -- is compiled.
7969 if Present (Underlying_Full_View (Derived_Type)) then
7970 Full_Der := Underlying_Full_View (Derived_Type);
7971 else
7972 Build_Full_Derivation;
7973 Set_Underlying_Full_View (Derived_Type, Full_Der);
7974 Set_Is_Underlying_Full_View (Full_Der);
7975 end if;
7977 -- The full view will be used to swap entities on entry/exit to
7978 -- the body, and must appear in the entity list for the package.
7980 Append_Entity (Full_Der, Scope (Derived_Type));
7981 end if;
7982 end if;
7983 end Build_Derived_Private_Type;
7985 -------------------------------
7986 -- Build_Derived_Record_Type --
7987 -------------------------------
7989 -- 1. INTRODUCTION
7991 -- Ideally we would like to use the same model of type derivation for
7992 -- tagged and untagged record types. Unfortunately this is not quite
7993 -- possible because the semantics of representation clauses is different
7994 -- for tagged and untagged records under inheritance. Consider the
7995 -- following:
7997 -- type R (...) is [tagged] record ... end record;
7998 -- type T (...) is new R (...) [with ...];
8000 -- The representation clauses for T can specify a completely different
8001 -- record layout from R's. Hence the same component can be placed in two
8002 -- very different positions in objects of type T and R. If R and T are
8003 -- tagged types, representation clauses for T can only specify the layout
8004 -- of non inherited components, thus components that are common in R and T
8005 -- have the same position in objects of type R and T.
8007 -- This has two implications. The first is that the entire tree for R's
8008 -- declaration needs to be copied for T in the untagged case, so that T
8009 -- can be viewed as a record type of its own with its own representation
8010 -- clauses. The second implication is the way we handle discriminants.
8011 -- Specifically, in the untagged case we need a way to communicate to Gigi
8012 -- what are the real discriminants in the record, while for the semantics
8013 -- we need to consider those introduced by the user to rename the
8014 -- discriminants in the parent type. This is handled by introducing the
8015 -- notion of stored discriminants. See below for more.
8017 -- Fortunately the way regular components are inherited can be handled in
8018 -- the same way in tagged and untagged types.
8020 -- To complicate things a bit more the private view of a private extension
8021 -- cannot be handled in the same way as the full view (for one thing the
8022 -- semantic rules are somewhat different). We will explain what differs
8023 -- below.
8025 -- 2. DISCRIMINANTS UNDER INHERITANCE
8027 -- The semantic rules governing the discriminants of derived types are
8028 -- quite subtle.
8030 -- type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
8031 -- [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
8033 -- If parent type has discriminants, then the discriminants that are
8034 -- declared in the derived type are [3.4 (11)]:
8036 -- o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
8037 -- there is one;
8039 -- o Otherwise, each discriminant of the parent type (implicitly declared
8040 -- in the same order with the same specifications). In this case, the
8041 -- discriminants are said to be "inherited", or if unknown in the parent
8042 -- are also unknown in the derived type.
8044 -- Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
8046 -- o The parent subtype must be constrained;
8048 -- o If the parent type is not a tagged type, then each discriminant of
8049 -- the derived type must be used in the constraint defining a parent
8050 -- subtype. [Implementation note: This ensures that the new discriminant
8051 -- can share storage with an existing discriminant.]
8053 -- For the derived type each discriminant of the parent type is either
8054 -- inherited, constrained to equal some new discriminant of the derived
8055 -- type, or constrained to the value of an expression.
8057 -- When inherited or constrained to equal some new discriminant, the
8058 -- parent discriminant and the discriminant of the derived type are said
8059 -- to "correspond".
8061 -- If a discriminant of the parent type is constrained to a specific value
8062 -- in the derived type definition, then the discriminant is said to be
8063 -- "specified" by that derived type definition.
8065 -- 3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
8067 -- We have spoken about stored discriminants in point 1 (introduction)
8068 -- above. There are two sorts of stored discriminants: implicit and
8069 -- explicit. As long as the derived type inherits the same discriminants as
8070 -- the root record type, stored discriminants are the same as regular
8071 -- discriminants, and are said to be implicit. However, if any discriminant
8072 -- in the root type was renamed in the derived type, then the derived
8073 -- type will contain explicit stored discriminants. Explicit stored
8074 -- discriminants are discriminants in addition to the semantically visible
8075 -- discriminants defined for the derived type. Stored discriminants are
8076 -- used by Gigi to figure out what are the physical discriminants in
8077 -- objects of the derived type (see precise definition in einfo.ads).
8078 -- As an example, consider the following:
8080 -- type R (D1, D2, D3 : Int) is record ... end record;
8081 -- type T1 is new R;
8082 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
8083 -- type T3 is new T2;
8084 -- type T4 (Y : Int) is new T3 (Y, 99);
8086 -- The following table summarizes the discriminants and stored
8087 -- discriminants in R and T1 through T4:
8089 -- Type Discrim Stored Discrim Comment
8090 -- R (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in R
8091 -- T1 (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in T1
8092 -- T2 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T2
8093 -- T3 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T3
8094 -- T4 (Y) (D1, D2, D3) Girder discrims EXPLICIT in T4
8096 -- Field Corresponding_Discriminant (abbreviated CD below) allows us to
8097 -- find the corresponding discriminant in the parent type, while
8098 -- Original_Record_Component (abbreviated ORC below) the actual physical
8099 -- component that is renamed. Finally the field Is_Completely_Hidden
8100 -- (abbreviated ICH below) is set for all explicit stored discriminants
8101 -- (see einfo.ads for more info). For the above example this gives:
8103 -- Discrim CD ORC ICH
8104 -- ^^^^^^^ ^^ ^^^ ^^^
8105 -- D1 in R empty itself no
8106 -- D2 in R empty itself no
8107 -- D3 in R empty itself no
8109 -- D1 in T1 D1 in R itself no
8110 -- D2 in T1 D2 in R itself no
8111 -- D3 in T1 D3 in R itself no
8113 -- X1 in T2 D3 in T1 D3 in T2 no
8114 -- X2 in T2 D1 in T1 D1 in T2 no
8115 -- D1 in T2 empty itself yes
8116 -- D2 in T2 empty itself yes
8117 -- D3 in T2 empty itself yes
8119 -- X1 in T3 X1 in T2 D3 in T3 no
8120 -- X2 in T3 X2 in T2 D1 in T3 no
8121 -- D1 in T3 empty itself yes
8122 -- D2 in T3 empty itself yes
8123 -- D3 in T3 empty itself yes
8125 -- Y in T4 X1 in T3 D3 in T4 no
8126 -- D1 in T4 empty itself yes
8127 -- D2 in T4 empty itself yes
8128 -- D3 in T4 empty itself yes
8130 -- 4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
8132 -- Type derivation for tagged types is fairly straightforward. If no
8133 -- discriminants are specified by the derived type, these are inherited
8134 -- from the parent. No explicit stored discriminants are ever necessary.
8135 -- The only manipulation that is done to the tree is that of adding a
8136 -- _parent field with parent type and constrained to the same constraint
8137 -- specified for the parent in the derived type definition. For instance:
8139 -- type R (D1, D2, D3 : Int) is tagged record ... end record;
8140 -- type T1 is new R with null record;
8141 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
8143 -- are changed into:
8145 -- type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
8146 -- _parent : R (D1, D2, D3);
8147 -- end record;
8149 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
8150 -- _parent : T1 (X2, 88, X1);
8151 -- end record;
8153 -- The discriminants actually present in R, T1 and T2 as well as their CD,
8154 -- ORC and ICH fields are:
8156 -- Discrim CD ORC ICH
8157 -- ^^^^^^^ ^^ ^^^ ^^^
8158 -- D1 in R empty itself no
8159 -- D2 in R empty itself no
8160 -- D3 in R empty itself no
8162 -- D1 in T1 D1 in R D1 in R no
8163 -- D2 in T1 D2 in R D2 in R no
8164 -- D3 in T1 D3 in R D3 in R no
8166 -- X1 in T2 D3 in T1 D3 in R no
8167 -- X2 in T2 D1 in T1 D1 in R no
8169 -- 5. FIRST TRANSFORMATION FOR DERIVED RECORDS
8171 -- Regardless of whether we dealing with a tagged or untagged type
8172 -- we will transform all derived type declarations of the form
8174 -- type T is new R (...) [with ...];
8175 -- or
8176 -- subtype S is R (...);
8177 -- type T is new S [with ...];
8178 -- into
8179 -- type BT is new R [with ...];
8180 -- subtype T is BT (...);
8182 -- That is, the base derived type is constrained only if it has no
8183 -- discriminants. The reason for doing this is that GNAT's semantic model
8184 -- assumes that a base type with discriminants is unconstrained.
8186 -- Note that, strictly speaking, the above transformation is not always
8187 -- correct. Consider for instance the following excerpt from ACVC b34011a:
8189 -- procedure B34011A is
8190 -- type REC (D : integer := 0) is record
8191 -- I : Integer;
8192 -- end record;
8194 -- package P is
8195 -- type T6 is new Rec;
8196 -- function F return T6;
8197 -- end P;
8199 -- use P;
8200 -- package Q6 is
8201 -- type U is new T6 (Q6.F.I); -- ERROR: Q6.F.
8202 -- end Q6;
8204 -- The definition of Q6.U is illegal. However transforming Q6.U into
8206 -- type BaseU is new T6;
8207 -- subtype U is BaseU (Q6.F.I)
8209 -- turns U into a legal subtype, which is incorrect. To avoid this problem
8210 -- we always analyze the constraint (in this case (Q6.F.I)) before applying
8211 -- the transformation described above.
8213 -- There is another instance where the above transformation is incorrect.
8214 -- Consider:
8216 -- package Pack is
8217 -- type Base (D : Integer) is tagged null record;
8218 -- procedure P (X : Base);
8220 -- type Der is new Base (2) with null record;
8221 -- procedure P (X : Der);
8222 -- end Pack;
8224 -- Then the above transformation turns this into
8226 -- type Der_Base is new Base with null record;
8227 -- -- procedure P (X : Base) is implicitly inherited here
8228 -- -- as procedure P (X : Der_Base).
8230 -- subtype Der is Der_Base (2);
8231 -- procedure P (X : Der);
8232 -- -- The overriding of P (X : Der_Base) is illegal since we
8233 -- -- have a parameter conformance problem.
8235 -- To get around this problem, after having semantically processed Der_Base
8236 -- and the rewritten subtype declaration for Der, we copy Der_Base field
8237 -- Discriminant_Constraint from Der so that when parameter conformance is
8238 -- checked when P is overridden, no semantic errors are flagged.
8240 -- 6. SECOND TRANSFORMATION FOR DERIVED RECORDS
8242 -- Regardless of whether we are dealing with a tagged or untagged type
8243 -- we will transform all derived type declarations of the form
8245 -- type R (D1, .., Dn : ...) is [tagged] record ...;
8246 -- type T is new R [with ...];
8247 -- into
8248 -- type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
8250 -- The reason for such transformation is that it allows us to implement a
8251 -- very clean form of component inheritance as explained below.
8253 -- Note that this transformation is not achieved by direct tree rewriting
8254 -- and manipulation, but rather by redoing the semantic actions that the
8255 -- above transformation will entail. This is done directly in routine
8256 -- Inherit_Components.
8258 -- 7. TYPE DERIVATION AND COMPONENT INHERITANCE
8260 -- In both tagged and untagged derived types, regular non discriminant
8261 -- components are inherited in the derived type from the parent type. In
8262 -- the absence of discriminants component, inheritance is straightforward
8263 -- as components can simply be copied from the parent.
8265 -- If the parent has discriminants, inheriting components constrained with
8266 -- these discriminants requires caution. Consider the following example:
8268 -- type R (D1, D2 : Positive) is [tagged] record
8269 -- S : String (D1 .. D2);
8270 -- end record;
8272 -- type T1 is new R [with null record];
8273 -- type T2 (X : positive) is new R (1, X) [with null record];
8275 -- As explained in 6. above, T1 is rewritten as
8276 -- type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
8277 -- which makes the treatment for T1 and T2 identical.
8279 -- What we want when inheriting S, is that references to D1 and D2 in R are
8280 -- replaced with references to their correct constraints, i.e. D1 and D2 in
8281 -- T1 and 1 and X in T2. So all R's discriminant references are replaced
8282 -- with either discriminant references in the derived type or expressions.
8283 -- This replacement is achieved as follows: before inheriting R's
8284 -- components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
8285 -- created in the scope of T1 (resp. scope of T2) so that discriminants D1
8286 -- and D2 of T1 are visible (resp. discriminant X of T2 is visible).
8287 -- For T2, for instance, this has the effect of replacing String (D1 .. D2)
8288 -- by String (1 .. X).
8290 -- 8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
8292 -- We explain here the rules governing private type extensions relevant to
8293 -- type derivation. These rules are explained on the following example:
8295 -- type D [(...)] is new A [(...)] with private; <-- partial view
8296 -- type D [(...)] is new P [(...)] with null record; <-- full view
8298 -- Type A is called the ancestor subtype of the private extension.
8299 -- Type P is the parent type of the full view of the private extension. It
8300 -- must be A or a type derived from A.
8302 -- The rules concerning the discriminants of private type extensions are
8303 -- [7.3(10-13)]:
8305 -- o If a private extension inherits known discriminants from the ancestor
8306 -- subtype, then the full view must also inherit its discriminants from
8307 -- the ancestor subtype and the parent subtype of the full view must be
8308 -- constrained if and only if the ancestor subtype is constrained.
8310 -- o If a partial view has unknown discriminants, then the full view may
8311 -- define a definite or an indefinite subtype, with or without
8312 -- discriminants.
8314 -- o If a partial view has neither known nor unknown discriminants, then
8315 -- the full view must define a definite subtype.
8317 -- o If the ancestor subtype of a private extension has constrained
8318 -- discriminants, then the parent subtype of the full view must impose a
8319 -- statically matching constraint on those discriminants.
8321 -- This means that only the following forms of private extensions are
8322 -- allowed:
8324 -- type D is new A with private; <-- partial view
8325 -- type D is new P with null record; <-- full view
8327 -- If A has no discriminants than P has no discriminants, otherwise P must
8328 -- inherit A's discriminants.
8330 -- type D is new A (...) with private; <-- partial view
8331 -- type D is new P (:::) with null record; <-- full view
8333 -- P must inherit A's discriminants and (...) and (:::) must statically
8334 -- match.
8336 -- subtype A is R (...);
8337 -- type D is new A with private; <-- partial view
8338 -- type D is new P with null record; <-- full view
8340 -- P must have inherited R's discriminants and must be derived from A or
8341 -- any of its subtypes.
8343 -- type D (..) is new A with private; <-- partial view
8344 -- type D (..) is new P [(:::)] with null record; <-- full view
8346 -- No specific constraints on P's discriminants or constraint (:::).
8347 -- Note that A can be unconstrained, but the parent subtype P must either
8348 -- be constrained or (:::) must be present.
8350 -- type D (..) is new A [(...)] with private; <-- partial view
8351 -- type D (..) is new P [(:::)] with null record; <-- full view
8353 -- P's constraints on A's discriminants must statically match those
8354 -- imposed by (...).
8356 -- 9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
8358 -- The full view of a private extension is handled exactly as described
8359 -- above. The model chose for the private view of a private extension is
8360 -- the same for what concerns discriminants (i.e. they receive the same
8361 -- treatment as in the tagged case). However, the private view of the
8362 -- private extension always inherits the components of the parent base,
8363 -- without replacing any discriminant reference. Strictly speaking this is
8364 -- incorrect. However, Gigi never uses this view to generate code so this
8365 -- is a purely semantic issue. In theory, a set of transformations similar
8366 -- to those given in 5. and 6. above could be applied to private views of
8367 -- private extensions to have the same model of component inheritance as
8368 -- for non private extensions. However, this is not done because it would
8369 -- further complicate private type processing. Semantically speaking, this
8370 -- leaves us in an uncomfortable situation. As an example consider:
8372 -- package Pack is
8373 -- type R (D : integer) is tagged record
8374 -- S : String (1 .. D);
8375 -- end record;
8376 -- procedure P (X : R);
8377 -- type T is new R (1) with private;
8378 -- private
8379 -- type T is new R (1) with null record;
8380 -- end;
8382 -- This is transformed into:
8384 -- package Pack is
8385 -- type R (D : integer) is tagged record
8386 -- S : String (1 .. D);
8387 -- end record;
8388 -- procedure P (X : R);
8389 -- type T is new R (1) with private;
8390 -- private
8391 -- type BaseT is new R with null record;
8392 -- subtype T is BaseT (1);
8393 -- end;
8395 -- (strictly speaking the above is incorrect Ada)
8397 -- From the semantic standpoint the private view of private extension T
8398 -- should be flagged as constrained since one can clearly have
8400 -- Obj : T;
8402 -- in a unit withing Pack. However, when deriving subprograms for the
8403 -- private view of private extension T, T must be seen as unconstrained
8404 -- since T has discriminants (this is a constraint of the current
8405 -- subprogram derivation model). Thus, when processing the private view of
8406 -- a private extension such as T, we first mark T as unconstrained, we
8407 -- process it, we perform program derivation and just before returning from
8408 -- Build_Derived_Record_Type we mark T as constrained.
8410 -- ??? Are there are other uncomfortable cases that we will have to
8411 -- deal with.
8413 -- 10. RECORD_TYPE_WITH_PRIVATE complications
8415 -- Types that are derived from a visible record type and have a private
8416 -- extension present other peculiarities. They behave mostly like private
8417 -- types, but if they have primitive operations defined, these will not
8418 -- have the proper signatures for further inheritance, because other
8419 -- primitive operations will use the implicit base that we define for
8420 -- private derivations below. This affect subprogram inheritance (see
8421 -- Derive_Subprograms for details). We also derive the implicit base from
8422 -- the base type of the full view, so that the implicit base is a record
8423 -- type and not another private type, This avoids infinite loops.
8425 procedure Build_Derived_Record_Type
8426 (N : Node_Id;
8427 Parent_Type : Entity_Id;
8428 Derived_Type : Entity_Id;
8429 Derive_Subps : Boolean := True)
8431 Discriminant_Specs : constant Boolean :=
8432 Present (Discriminant_Specifications (N));
8433 Is_Tagged : constant Boolean := Is_Tagged_Type (Parent_Type);
8434 Loc : constant Source_Ptr := Sloc (N);
8435 Private_Extension : constant Boolean :=
8436 Nkind (N) = N_Private_Extension_Declaration;
8437 Assoc_List : Elist_Id;
8438 Constraint_Present : Boolean;
8439 Constrs : Elist_Id;
8440 Discrim : Entity_Id;
8441 Indic : Node_Id;
8442 Inherit_Discrims : Boolean := False;
8443 Last_Discrim : Entity_Id;
8444 New_Base : Entity_Id;
8445 New_Decl : Node_Id;
8446 New_Discrs : Elist_Id;
8447 New_Indic : Node_Id;
8448 Parent_Base : Entity_Id;
8449 Save_Etype : Entity_Id;
8450 Save_Discr_Constr : Elist_Id;
8451 Save_Next_Entity : Entity_Id;
8452 Type_Def : Node_Id;
8454 Discs : Elist_Id := New_Elmt_List;
8455 -- An empty Discs list means that there were no constraints in the
8456 -- subtype indication or that there was an error processing it.
8458 begin
8459 if Ekind (Parent_Type) = E_Record_Type_With_Private
8460 and then Present (Full_View (Parent_Type))
8461 and then Has_Discriminants (Parent_Type)
8462 then
8463 Parent_Base := Base_Type (Full_View (Parent_Type));
8464 else
8465 Parent_Base := Base_Type (Parent_Type);
8466 end if;
8468 -- AI05-0115 : if this is a derivation from a private type in some
8469 -- other scope that may lead to invisible components for the derived
8470 -- type, mark it accordingly.
8472 if Is_Private_Type (Parent_Type) then
8473 if Scope (Parent_Type) = Scope (Derived_Type) then
8474 null;
8476 elsif In_Open_Scopes (Scope (Parent_Type))
8477 and then In_Private_Part (Scope (Parent_Type))
8478 then
8479 null;
8481 else
8482 Set_Has_Private_Ancestor (Derived_Type);
8483 end if;
8485 else
8486 Set_Has_Private_Ancestor
8487 (Derived_Type, Has_Private_Ancestor (Parent_Type));
8488 end if;
8490 -- Before we start the previously documented transformations, here is
8491 -- little fix for size and alignment of tagged types. Normally when we
8492 -- derive type D from type P, we copy the size and alignment of P as the
8493 -- default for D, and in the absence of explicit representation clauses
8494 -- for D, the size and alignment are indeed the same as the parent.
8496 -- But this is wrong for tagged types, since fields may be added, and
8497 -- the default size may need to be larger, and the default alignment may
8498 -- need to be larger.
8500 -- We therefore reset the size and alignment fields in the tagged case.
8501 -- Note that the size and alignment will in any case be at least as
8502 -- large as the parent type (since the derived type has a copy of the
8503 -- parent type in the _parent field)
8505 -- The type is also marked as being tagged here, which is needed when
8506 -- processing components with a self-referential anonymous access type
8507 -- in the call to Check_Anonymous_Access_Components below. Note that
8508 -- this flag is also set later on for completeness.
8510 if Is_Tagged then
8511 Set_Is_Tagged_Type (Derived_Type);
8512 Init_Size_Align (Derived_Type);
8513 end if;
8515 -- STEP 0a: figure out what kind of derived type declaration we have
8517 if Private_Extension then
8518 Type_Def := N;
8519 Set_Ekind (Derived_Type, E_Record_Type_With_Private);
8520 Set_Default_SSO (Derived_Type);
8521 Set_No_Reordering (Derived_Type, No_Component_Reordering);
8523 else
8524 Type_Def := Type_Definition (N);
8526 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
8527 -- Parent_Base can be a private type or private extension. However,
8528 -- for tagged types with an extension the newly added fields are
8529 -- visible and hence the Derived_Type is always an E_Record_Type.
8530 -- (except that the parent may have its own private fields).
8531 -- For untagged types we preserve the Ekind of the Parent_Base.
8533 if Present (Record_Extension_Part (Type_Def)) then
8534 Set_Ekind (Derived_Type, E_Record_Type);
8535 Set_Default_SSO (Derived_Type);
8536 Set_No_Reordering (Derived_Type, No_Component_Reordering);
8538 -- Create internal access types for components with anonymous
8539 -- access types.
8541 if Ada_Version >= Ada_2005 then
8542 Check_Anonymous_Access_Components
8543 (N, Derived_Type, Derived_Type,
8544 Component_List (Record_Extension_Part (Type_Def)));
8545 end if;
8547 else
8548 Set_Ekind (Derived_Type, Ekind (Parent_Base));
8549 end if;
8550 end if;
8552 -- Indic can either be an N_Identifier if the subtype indication
8553 -- contains no constraint or an N_Subtype_Indication if the subtype
8554 -- indication has a constraint.
8556 Indic := Subtype_Indication (Type_Def);
8557 Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
8559 -- Check that the type has visible discriminants. The type may be
8560 -- a private type with unknown discriminants whose full view has
8561 -- discriminants which are invisible.
8563 if Constraint_Present then
8564 if not Has_Discriminants (Parent_Base)
8565 or else
8566 (Has_Unknown_Discriminants (Parent_Base)
8567 and then Is_Private_Type (Parent_Base))
8568 then
8569 Error_Msg_N
8570 ("invalid constraint: type has no discriminant",
8571 Constraint (Indic));
8573 Constraint_Present := False;
8574 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
8576 elsif Is_Constrained (Parent_Type) then
8577 Error_Msg_N
8578 ("invalid constraint: parent type is already constrained",
8579 Constraint (Indic));
8581 Constraint_Present := False;
8582 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
8583 end if;
8584 end if;
8586 -- STEP 0b: If needed, apply transformation given in point 5. above
8588 if not Private_Extension
8589 and then Has_Discriminants (Parent_Type)
8590 and then not Discriminant_Specs
8591 and then (Is_Constrained (Parent_Type) or else Constraint_Present)
8592 then
8593 -- First, we must analyze the constraint (see comment in point 5.)
8594 -- The constraint may come from the subtype indication of the full
8595 -- declaration.
8597 if Constraint_Present then
8598 New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
8600 -- If there is no explicit constraint, there might be one that is
8601 -- inherited from a constrained parent type. In that case verify that
8602 -- it conforms to the constraint in the partial view. In perverse
8603 -- cases the parent subtypes of the partial and full view can have
8604 -- different constraints.
8606 elsif Present (Stored_Constraint (Parent_Type)) then
8607 New_Discrs := Stored_Constraint (Parent_Type);
8609 else
8610 New_Discrs := No_Elist;
8611 end if;
8613 if Has_Discriminants (Derived_Type)
8614 and then Has_Private_Declaration (Derived_Type)
8615 and then Present (Discriminant_Constraint (Derived_Type))
8616 and then Present (New_Discrs)
8617 then
8618 -- Verify that constraints of the full view statically match
8619 -- those given in the partial view.
8621 declare
8622 C1, C2 : Elmt_Id;
8624 begin
8625 C1 := First_Elmt (New_Discrs);
8626 C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
8627 while Present (C1) and then Present (C2) loop
8628 if Fully_Conformant_Expressions (Node (C1), Node (C2))
8629 or else
8630 (Is_OK_Static_Expression (Node (C1))
8631 and then Is_OK_Static_Expression (Node (C2))
8632 and then
8633 Expr_Value (Node (C1)) = Expr_Value (Node (C2)))
8634 then
8635 null;
8637 else
8638 if Constraint_Present then
8639 Error_Msg_N
8640 ("constraint not conformant to previous declaration",
8641 Node (C1));
8642 else
8643 Error_Msg_N
8644 ("constraint of full view is incompatible "
8645 & "with partial view", N);
8646 end if;
8647 end if;
8649 Next_Elmt (C1);
8650 Next_Elmt (C2);
8651 end loop;
8652 end;
8653 end if;
8655 -- Insert and analyze the declaration for the unconstrained base type
8657 New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
8659 New_Decl :=
8660 Make_Full_Type_Declaration (Loc,
8661 Defining_Identifier => New_Base,
8662 Type_Definition =>
8663 Make_Derived_Type_Definition (Loc,
8664 Abstract_Present => Abstract_Present (Type_Def),
8665 Limited_Present => Limited_Present (Type_Def),
8666 Subtype_Indication =>
8667 New_Occurrence_Of (Parent_Base, Loc),
8668 Record_Extension_Part =>
8669 Relocate_Node (Record_Extension_Part (Type_Def)),
8670 Interface_List => Interface_List (Type_Def)));
8672 Set_Parent (New_Decl, Parent (N));
8673 Mark_Rewrite_Insertion (New_Decl);
8674 Insert_Before (N, New_Decl);
8676 -- In the extension case, make sure ancestor is frozen appropriately
8677 -- (see also non-discriminated case below).
8679 if Present (Record_Extension_Part (Type_Def))
8680 or else Is_Interface (Parent_Base)
8681 then
8682 Freeze_Before (New_Decl, Parent_Type);
8683 end if;
8685 -- Note that this call passes False for the Derive_Subps parameter
8686 -- because subprogram derivation is deferred until after creating
8687 -- the subtype (see below).
8689 Build_Derived_Type
8690 (New_Decl, Parent_Base, New_Base,
8691 Is_Completion => False, Derive_Subps => False);
8693 -- ??? This needs re-examination to determine whether the
8694 -- above call can simply be replaced by a call to Analyze.
8696 Set_Analyzed (New_Decl);
8698 -- Insert and analyze the declaration for the constrained subtype
8700 if Constraint_Present then
8701 New_Indic :=
8702 Make_Subtype_Indication (Loc,
8703 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
8704 Constraint => Relocate_Node (Constraint (Indic)));
8706 else
8707 declare
8708 Constr_List : constant List_Id := New_List;
8709 C : Elmt_Id;
8710 Expr : Node_Id;
8712 begin
8713 C := First_Elmt (Discriminant_Constraint (Parent_Type));
8714 while Present (C) loop
8715 Expr := Node (C);
8717 -- It is safe here to call New_Copy_Tree since we called
8718 -- Force_Evaluation on each constraint previously
8719 -- in Build_Discriminant_Constraints.
8721 Append (New_Copy_Tree (Expr), To => Constr_List);
8723 Next_Elmt (C);
8724 end loop;
8726 New_Indic :=
8727 Make_Subtype_Indication (Loc,
8728 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
8729 Constraint =>
8730 Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
8731 end;
8732 end if;
8734 Rewrite (N,
8735 Make_Subtype_Declaration (Loc,
8736 Defining_Identifier => Derived_Type,
8737 Subtype_Indication => New_Indic));
8739 Analyze (N);
8741 -- Derivation of subprograms must be delayed until the full subtype
8742 -- has been established, to ensure proper overriding of subprograms
8743 -- inherited by full types. If the derivations occurred as part of
8744 -- the call to Build_Derived_Type above, then the check for type
8745 -- conformance would fail because earlier primitive subprograms
8746 -- could still refer to the full type prior the change to the new
8747 -- subtype and hence would not match the new base type created here.
8748 -- Subprograms are not derived, however, when Derive_Subps is False
8749 -- (since otherwise there could be redundant derivations).
8751 if Derive_Subps then
8752 Derive_Subprograms (Parent_Type, Derived_Type);
8753 end if;
8755 -- For tagged types the Discriminant_Constraint of the new base itype
8756 -- is inherited from the first subtype so that no subtype conformance
8757 -- problem arise when the first subtype overrides primitive
8758 -- operations inherited by the implicit base type.
8760 if Is_Tagged then
8761 Set_Discriminant_Constraint
8762 (New_Base, Discriminant_Constraint (Derived_Type));
8763 end if;
8765 return;
8766 end if;
8768 -- If we get here Derived_Type will have no discriminants or it will be
8769 -- a discriminated unconstrained base type.
8771 -- STEP 1a: perform preliminary actions/checks for derived tagged types
8773 if Is_Tagged then
8775 -- The parent type is frozen for non-private extensions (RM 13.14(7))
8776 -- The declaration of a specific descendant of an interface type
8777 -- freezes the interface type (RM 13.14).
8779 if not Private_Extension or else Is_Interface (Parent_Base) then
8780 Freeze_Before (N, Parent_Type);
8781 end if;
8783 -- In Ada 2005 (AI-344), the restriction that a derived tagged type
8784 -- cannot be declared at a deeper level than its parent type is
8785 -- removed. The check on derivation within a generic body is also
8786 -- relaxed, but there's a restriction that a derived tagged type
8787 -- cannot be declared in a generic body if it's derived directly
8788 -- or indirectly from a formal type of that generic.
8790 if Ada_Version >= Ada_2005 then
8791 if Present (Enclosing_Generic_Body (Derived_Type)) then
8792 declare
8793 Ancestor_Type : Entity_Id;
8795 begin
8796 -- Check to see if any ancestor of the derived type is a
8797 -- formal type.
8799 Ancestor_Type := Parent_Type;
8800 while not Is_Generic_Type (Ancestor_Type)
8801 and then Etype (Ancestor_Type) /= Ancestor_Type
8802 loop
8803 Ancestor_Type := Etype (Ancestor_Type);
8804 end loop;
8806 -- If the derived type does have a formal type as an
8807 -- ancestor, then it's an error if the derived type is
8808 -- declared within the body of the generic unit that
8809 -- declares the formal type in its generic formal part. It's
8810 -- sufficient to check whether the ancestor type is declared
8811 -- inside the same generic body as the derived type (such as
8812 -- within a nested generic spec), in which case the
8813 -- derivation is legal. If the formal type is declared
8814 -- outside of that generic body, then it's guaranteed that
8815 -- the derived type is declared within the generic body of
8816 -- the generic unit declaring the formal type.
8818 if Is_Generic_Type (Ancestor_Type)
8819 and then Enclosing_Generic_Body (Ancestor_Type) /=
8820 Enclosing_Generic_Body (Derived_Type)
8821 then
8822 Error_Msg_NE
8823 ("parent type of& must not be descendant of formal type"
8824 & " of an enclosing generic body",
8825 Indic, Derived_Type);
8826 end if;
8827 end;
8828 end if;
8830 elsif Type_Access_Level (Derived_Type) /=
8831 Type_Access_Level (Parent_Type)
8832 and then not Is_Generic_Type (Derived_Type)
8833 then
8834 if Is_Controlled (Parent_Type) then
8835 Error_Msg_N
8836 ("controlled type must be declared at the library level",
8837 Indic);
8838 else
8839 Error_Msg_N
8840 ("type extension at deeper accessibility level than parent",
8841 Indic);
8842 end if;
8844 else
8845 declare
8846 GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
8847 begin
8848 if Present (GB)
8849 and then GB /= Enclosing_Generic_Body (Parent_Base)
8850 then
8851 Error_Msg_NE
8852 ("parent type of& must not be outside generic body"
8853 & " (RM 3.9.1(4))",
8854 Indic, Derived_Type);
8855 end if;
8856 end;
8857 end if;
8858 end if;
8860 -- Ada 2005 (AI-251)
8862 if Ada_Version >= Ada_2005 and then Is_Tagged then
8864 -- "The declaration of a specific descendant of an interface type
8865 -- freezes the interface type" (RM 13.14).
8867 declare
8868 Iface : Node_Id;
8869 begin
8870 if Is_Non_Empty_List (Interface_List (Type_Def)) then
8871 Iface := First (Interface_List (Type_Def));
8872 while Present (Iface) loop
8873 Freeze_Before (N, Etype (Iface));
8874 Next (Iface);
8875 end loop;
8876 end if;
8877 end;
8878 end if;
8880 -- STEP 1b : preliminary cleanup of the full view of private types
8882 -- If the type is already marked as having discriminants, then it's the
8883 -- completion of a private type or private extension and we need to
8884 -- retain the discriminants from the partial view if the current
8885 -- declaration has Discriminant_Specifications so that we can verify
8886 -- conformance. However, we must remove any existing components that
8887 -- were inherited from the parent (and attached in Copy_And_Swap)
8888 -- because the full type inherits all appropriate components anyway, and
8889 -- we do not want the partial view's components interfering.
8891 if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
8892 Discrim := First_Discriminant (Derived_Type);
8893 loop
8894 Last_Discrim := Discrim;
8895 Next_Discriminant (Discrim);
8896 exit when No (Discrim);
8897 end loop;
8899 Set_Last_Entity (Derived_Type, Last_Discrim);
8901 -- In all other cases wipe out the list of inherited components (even
8902 -- inherited discriminants), it will be properly rebuilt here.
8904 else
8905 Set_First_Entity (Derived_Type, Empty);
8906 Set_Last_Entity (Derived_Type, Empty);
8907 end if;
8909 -- STEP 1c: Initialize some flags for the Derived_Type
8911 -- The following flags must be initialized here so that
8912 -- Process_Discriminants can check that discriminants of tagged types do
8913 -- not have a default initial value and that access discriminants are
8914 -- only specified for limited records. For completeness, these flags are
8915 -- also initialized along with all the other flags below.
8917 -- AI-419: Limitedness is not inherited from an interface parent, so to
8918 -- be limited in that case the type must be explicitly declared as
8919 -- limited. However, task and protected interfaces are always limited.
8921 if Limited_Present (Type_Def) then
8922 Set_Is_Limited_Record (Derived_Type);
8924 elsif Is_Limited_Record (Parent_Type)
8925 or else (Present (Full_View (Parent_Type))
8926 and then Is_Limited_Record (Full_View (Parent_Type)))
8927 then
8928 if not Is_Interface (Parent_Type)
8929 or else Is_Synchronized_Interface (Parent_Type)
8930 or else Is_Protected_Interface (Parent_Type)
8931 or else Is_Task_Interface (Parent_Type)
8932 then
8933 Set_Is_Limited_Record (Derived_Type);
8934 end if;
8935 end if;
8937 -- STEP 2a: process discriminants of derived type if any
8939 Push_Scope (Derived_Type);
8941 if Discriminant_Specs then
8942 Set_Has_Unknown_Discriminants (Derived_Type, False);
8944 -- The following call initializes fields Has_Discriminants and
8945 -- Discriminant_Constraint, unless we are processing the completion
8946 -- of a private type declaration.
8948 Check_Or_Process_Discriminants (N, Derived_Type);
8950 -- For untagged types, the constraint on the Parent_Type must be
8951 -- present and is used to rename the discriminants.
8953 if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
8954 Error_Msg_N ("untagged parent must have discriminants", Indic);
8956 elsif not Is_Tagged and then not Constraint_Present then
8957 Error_Msg_N
8958 ("discriminant constraint needed for derived untagged records",
8959 Indic);
8961 -- Otherwise the parent subtype must be constrained unless we have a
8962 -- private extension.
8964 elsif not Constraint_Present
8965 and then not Private_Extension
8966 and then not Is_Constrained (Parent_Type)
8967 then
8968 Error_Msg_N
8969 ("unconstrained type not allowed in this context", Indic);
8971 elsif Constraint_Present then
8972 -- The following call sets the field Corresponding_Discriminant
8973 -- for the discriminants in the Derived_Type.
8975 Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
8977 -- For untagged types all new discriminants must rename
8978 -- discriminants in the parent. For private extensions new
8979 -- discriminants cannot rename old ones (implied by [7.3(13)]).
8981 Discrim := First_Discriminant (Derived_Type);
8982 while Present (Discrim) loop
8983 if not Is_Tagged
8984 and then No (Corresponding_Discriminant (Discrim))
8985 then
8986 Error_Msg_N
8987 ("new discriminants must constrain old ones", Discrim);
8989 elsif Private_Extension
8990 and then Present (Corresponding_Discriminant (Discrim))
8991 then
8992 Error_Msg_N
8993 ("only static constraints allowed for parent"
8994 & " discriminants in the partial view", Indic);
8995 exit;
8996 end if;
8998 -- If a new discriminant is used in the constraint, then its
8999 -- subtype must be statically compatible with the parent
9000 -- discriminant's subtype (3.7(15)).
9002 -- However, if the record contains an array constrained by
9003 -- the discriminant but with some different bound, the compiler
9004 -- tries to create a smaller range for the discriminant type.
9005 -- (See exp_ch3.Adjust_Discriminants). In this case, where
9006 -- the discriminant type is a scalar type, the check must use
9007 -- the original discriminant type in the parent declaration.
9009 declare
9010 Corr_Disc : constant Entity_Id :=
9011 Corresponding_Discriminant (Discrim);
9012 Disc_Type : constant Entity_Id := Etype (Discrim);
9013 Corr_Type : Entity_Id;
9015 begin
9016 if Present (Corr_Disc) then
9017 if Is_Scalar_Type (Disc_Type) then
9018 Corr_Type :=
9019 Entity (Discriminant_Type (Parent (Corr_Disc)));
9020 else
9021 Corr_Type := Etype (Corr_Disc);
9022 end if;
9024 if not
9025 Subtypes_Statically_Compatible (Disc_Type, Corr_Type)
9026 then
9027 Error_Msg_N
9028 ("subtype must be compatible "
9029 & "with parent discriminant",
9030 Discrim);
9031 end if;
9032 end if;
9033 end;
9035 Next_Discriminant (Discrim);
9036 end loop;
9038 -- Check whether the constraints of the full view statically
9039 -- match those imposed by the parent subtype [7.3(13)].
9041 if Present (Stored_Constraint (Derived_Type)) then
9042 declare
9043 C1, C2 : Elmt_Id;
9045 begin
9046 C1 := First_Elmt (Discs);
9047 C2 := First_Elmt (Stored_Constraint (Derived_Type));
9048 while Present (C1) and then Present (C2) loop
9049 if not
9050 Fully_Conformant_Expressions (Node (C1), Node (C2))
9051 then
9052 Error_Msg_N
9053 ("not conformant with previous declaration",
9054 Node (C1));
9055 end if;
9057 Next_Elmt (C1);
9058 Next_Elmt (C2);
9059 end loop;
9060 end;
9061 end if;
9062 end if;
9064 -- STEP 2b: No new discriminants, inherit discriminants if any
9066 else
9067 if Private_Extension then
9068 Set_Has_Unknown_Discriminants
9069 (Derived_Type,
9070 Has_Unknown_Discriminants (Parent_Type)
9071 or else Unknown_Discriminants_Present (N));
9073 -- The partial view of the parent may have unknown discriminants,
9074 -- but if the full view has discriminants and the parent type is
9075 -- in scope they must be inherited.
9077 elsif Has_Unknown_Discriminants (Parent_Type)
9078 and then
9079 (not Has_Discriminants (Parent_Type)
9080 or else not In_Open_Scopes (Scope (Parent_Type)))
9081 then
9082 Set_Has_Unknown_Discriminants (Derived_Type);
9083 end if;
9085 if not Has_Unknown_Discriminants (Derived_Type)
9086 and then not Has_Unknown_Discriminants (Parent_Base)
9087 and then Has_Discriminants (Parent_Type)
9088 then
9089 Inherit_Discrims := True;
9090 Set_Has_Discriminants
9091 (Derived_Type, True);
9092 Set_Discriminant_Constraint
9093 (Derived_Type, Discriminant_Constraint (Parent_Base));
9094 end if;
9096 -- The following test is true for private types (remember
9097 -- transformation 5. is not applied to those) and in an error
9098 -- situation.
9100 if Constraint_Present then
9101 Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
9102 end if;
9104 -- For now mark a new derived type as constrained only if it has no
9105 -- discriminants. At the end of Build_Derived_Record_Type we properly
9106 -- set this flag in the case of private extensions. See comments in
9107 -- point 9. just before body of Build_Derived_Record_Type.
9109 Set_Is_Constrained
9110 (Derived_Type,
9111 not (Inherit_Discrims
9112 or else Has_Unknown_Discriminants (Derived_Type)));
9113 end if;
9115 -- STEP 3: initialize fields of derived type
9117 Set_Is_Tagged_Type (Derived_Type, Is_Tagged);
9118 Set_Stored_Constraint (Derived_Type, No_Elist);
9120 -- Ada 2005 (AI-251): Private type-declarations can implement interfaces
9121 -- but cannot be interfaces
9123 if not Private_Extension
9124 and then Ekind (Derived_Type) /= E_Private_Type
9125 and then Ekind (Derived_Type) /= E_Limited_Private_Type
9126 then
9127 if Interface_Present (Type_Def) then
9128 Analyze_Interface_Declaration (Derived_Type, Type_Def);
9129 end if;
9131 Set_Interfaces (Derived_Type, No_Elist);
9132 end if;
9134 -- Fields inherited from the Parent_Type
9136 Set_Has_Specified_Layout
9137 (Derived_Type, Has_Specified_Layout (Parent_Type));
9138 Set_Is_Limited_Composite
9139 (Derived_Type, Is_Limited_Composite (Parent_Type));
9140 Set_Is_Private_Composite
9141 (Derived_Type, Is_Private_Composite (Parent_Type));
9143 if Is_Tagged_Type (Parent_Type) then
9144 Set_No_Tagged_Streams_Pragma
9145 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
9146 end if;
9148 -- Fields inherited from the Parent_Base
9150 Set_Has_Controlled_Component
9151 (Derived_Type, Has_Controlled_Component (Parent_Base));
9152 Set_Has_Non_Standard_Rep
9153 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
9154 Set_Has_Primitive_Operations
9155 (Derived_Type, Has_Primitive_Operations (Parent_Base));
9157 -- Set fields for private derived types
9159 if Is_Private_Type (Derived_Type) then
9160 Set_Depends_On_Private (Derived_Type, True);
9161 Set_Private_Dependents (Derived_Type, New_Elmt_List);
9162 end if;
9164 -- Inherit fields for non-private types. If this is the completion of a
9165 -- derivation from a private type, the parent itself is private and the
9166 -- attributes come from its full view, which must be present.
9168 if Is_Record_Type (Derived_Type) then
9169 declare
9170 Parent_Full : Entity_Id;
9172 begin
9173 if Is_Private_Type (Parent_Base)
9174 and then not Is_Record_Type (Parent_Base)
9175 then
9176 Parent_Full := Full_View (Parent_Base);
9177 else
9178 Parent_Full := Parent_Base;
9179 end if;
9181 Set_Component_Alignment
9182 (Derived_Type, Component_Alignment (Parent_Full));
9183 Set_C_Pass_By_Copy
9184 (Derived_Type, C_Pass_By_Copy (Parent_Full));
9185 Set_Has_Complex_Representation
9186 (Derived_Type, Has_Complex_Representation (Parent_Full));
9188 -- For untagged types, inherit the layout by default to avoid
9189 -- costly changes of representation for type conversions.
9191 if not Is_Tagged then
9192 Set_Is_Packed (Derived_Type, Is_Packed (Parent_Full));
9193 Set_No_Reordering (Derived_Type, No_Reordering (Parent_Full));
9194 end if;
9195 end;
9196 end if;
9198 -- Set fields for tagged types
9200 if Is_Tagged then
9201 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
9203 -- All tagged types defined in Ada.Finalization are controlled
9205 if Chars (Scope (Derived_Type)) = Name_Finalization
9206 and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
9207 and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
9208 then
9209 Set_Is_Controlled (Derived_Type);
9210 else
9211 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Base));
9212 end if;
9214 -- Minor optimization: there is no need to generate the class-wide
9215 -- entity associated with an underlying record view.
9217 if not Is_Underlying_Record_View (Derived_Type) then
9218 Make_Class_Wide_Type (Derived_Type);
9219 end if;
9221 Set_Is_Abstract_Type (Derived_Type, Abstract_Present (Type_Def));
9223 if Has_Discriminants (Derived_Type)
9224 and then Constraint_Present
9225 then
9226 Set_Stored_Constraint
9227 (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
9228 end if;
9230 if Ada_Version >= Ada_2005 then
9231 declare
9232 Ifaces_List : Elist_Id;
9234 begin
9235 -- Checks rules 3.9.4 (13/2 and 14/2)
9237 if Comes_From_Source (Derived_Type)
9238 and then not Is_Private_Type (Derived_Type)
9239 and then Is_Interface (Parent_Type)
9240 and then not Is_Interface (Derived_Type)
9241 then
9242 if Is_Task_Interface (Parent_Type) then
9243 Error_Msg_N
9244 ("(Ada 2005) task type required (RM 3.9.4 (13.2))",
9245 Derived_Type);
9247 elsif Is_Protected_Interface (Parent_Type) then
9248 Error_Msg_N
9249 ("(Ada 2005) protected type required (RM 3.9.4 (14.2))",
9250 Derived_Type);
9251 end if;
9252 end if;
9254 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
9256 Check_Interfaces (N, Type_Def);
9258 -- Ada 2005 (AI-251): Collect the list of progenitors that are
9259 -- not already in the parents.
9261 Collect_Interfaces
9262 (T => Derived_Type,
9263 Ifaces_List => Ifaces_List,
9264 Exclude_Parents => True);
9266 Set_Interfaces (Derived_Type, Ifaces_List);
9268 -- If the derived type is the anonymous type created for
9269 -- a declaration whose parent has a constraint, propagate
9270 -- the interface list to the source type. This must be done
9271 -- prior to the completion of the analysis of the source type
9272 -- because the components in the extension may contain current
9273 -- instances whose legality depends on some ancestor.
9275 if Is_Itype (Derived_Type) then
9276 declare
9277 Def : constant Node_Id :=
9278 Associated_Node_For_Itype (Derived_Type);
9279 begin
9280 if Present (Def)
9281 and then Nkind (Def) = N_Full_Type_Declaration
9282 then
9283 Set_Interfaces
9284 (Defining_Identifier (Def), Ifaces_List);
9285 end if;
9286 end;
9287 end if;
9289 -- A type extension is automatically Ghost when one of its
9290 -- progenitors is Ghost (SPARK RM 6.9(9)). This property is
9291 -- also inherited when the parent type is Ghost, but this is
9292 -- done in Build_Derived_Type as the mechanism also handles
9293 -- untagged derivations.
9295 if Implements_Ghost_Interface (Derived_Type) then
9296 Set_Is_Ghost_Entity (Derived_Type);
9297 end if;
9298 end;
9299 end if;
9300 end if;
9302 -- STEP 4: Inherit components from the parent base and constrain them.
9303 -- Apply the second transformation described in point 6. above.
9305 if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
9306 or else not Has_Discriminants (Parent_Type)
9307 or else not Is_Constrained (Parent_Type)
9308 then
9309 Constrs := Discs;
9310 else
9311 Constrs := Discriminant_Constraint (Parent_Type);
9312 end if;
9314 Assoc_List :=
9315 Inherit_Components
9316 (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
9318 -- STEP 5a: Copy the parent record declaration for untagged types
9320 Set_Has_Implicit_Dereference
9321 (Derived_Type, Has_Implicit_Dereference (Parent_Type));
9323 if not Is_Tagged then
9325 -- Discriminant_Constraint (Derived_Type) has been properly
9326 -- constructed. Save it and temporarily set it to Empty because we
9327 -- do not want the call to New_Copy_Tree below to mess this list.
9329 if Has_Discriminants (Derived_Type) then
9330 Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
9331 Set_Discriminant_Constraint (Derived_Type, No_Elist);
9332 else
9333 Save_Discr_Constr := No_Elist;
9334 end if;
9336 -- Save the Etype field of Derived_Type. It is correctly set now,
9337 -- but the call to New_Copy tree may remap it to point to itself,
9338 -- which is not what we want. Ditto for the Next_Entity field.
9340 Save_Etype := Etype (Derived_Type);
9341 Save_Next_Entity := Next_Entity (Derived_Type);
9343 -- Assoc_List maps all stored discriminants in the Parent_Base to
9344 -- stored discriminants in the Derived_Type. It is fundamental that
9345 -- no types or itypes with discriminants other than the stored
9346 -- discriminants appear in the entities declared inside
9347 -- Derived_Type, since the back end cannot deal with it.
9349 New_Decl :=
9350 New_Copy_Tree
9351 (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
9352 Copy_Dimensions_Of_Components (Derived_Type);
9354 -- Restore the fields saved prior to the New_Copy_Tree call
9355 -- and compute the stored constraint.
9357 Set_Etype (Derived_Type, Save_Etype);
9358 Set_Next_Entity (Derived_Type, Save_Next_Entity);
9360 if Has_Discriminants (Derived_Type) then
9361 Set_Discriminant_Constraint
9362 (Derived_Type, Save_Discr_Constr);
9363 Set_Stored_Constraint
9364 (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
9365 Replace_Components (Derived_Type, New_Decl);
9366 end if;
9368 -- Insert the new derived type declaration
9370 Rewrite (N, New_Decl);
9372 -- STEP 5b: Complete the processing for record extensions in generics
9374 -- There is no completion for record extensions declared in the
9375 -- parameter part of a generic, so we need to complete processing for
9376 -- these generic record extensions here. The Record_Type_Definition call
9377 -- will change the Ekind of the components from E_Void to E_Component.
9379 elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
9380 Record_Type_Definition (Empty, Derived_Type);
9382 -- STEP 5c: Process the record extension for non private tagged types
9384 elsif not Private_Extension then
9385 Expand_Record_Extension (Derived_Type, Type_Def);
9387 -- Note : previously in ASIS mode we set the Parent_Subtype of the
9388 -- derived type to propagate some semantic information. This led
9389 -- to other ASIS failures and has been removed.
9391 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
9392 -- implemented interfaces if we are in expansion mode
9394 if Expander_Active
9395 and then Has_Interfaces (Derived_Type)
9396 then
9397 Add_Interface_Tag_Components (N, Derived_Type);
9398 end if;
9400 -- Analyze the record extension
9402 Record_Type_Definition
9403 (Record_Extension_Part (Type_Def), Derived_Type);
9404 end if;
9406 End_Scope;
9408 -- Nothing else to do if there is an error in the derivation.
9409 -- An unusual case: the full view may be derived from a type in an
9410 -- instance, when the partial view was used illegally as an actual
9411 -- in that instance, leading to a circular definition.
9413 if Etype (Derived_Type) = Any_Type
9414 or else Etype (Parent_Type) = Derived_Type
9415 then
9416 return;
9417 end if;
9419 -- Set delayed freeze and then derive subprograms, we need to do
9420 -- this in this order so that derived subprograms inherit the
9421 -- derived freeze if necessary.
9423 Set_Has_Delayed_Freeze (Derived_Type);
9425 if Derive_Subps then
9426 Derive_Subprograms (Parent_Type, Derived_Type);
9427 end if;
9429 -- If we have a private extension which defines a constrained derived
9430 -- type mark as constrained here after we have derived subprograms. See
9431 -- comment on point 9. just above the body of Build_Derived_Record_Type.
9433 if Private_Extension and then Inherit_Discrims then
9434 if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
9435 Set_Is_Constrained (Derived_Type, True);
9436 Set_Discriminant_Constraint (Derived_Type, Discs);
9438 elsif Is_Constrained (Parent_Type) then
9439 Set_Is_Constrained
9440 (Derived_Type, True);
9441 Set_Discriminant_Constraint
9442 (Derived_Type, Discriminant_Constraint (Parent_Type));
9443 end if;
9444 end if;
9446 -- Update the class-wide type, which shares the now-completed entity
9447 -- list with its specific type. In case of underlying record views,
9448 -- we do not generate the corresponding class wide entity.
9450 if Is_Tagged
9451 and then not Is_Underlying_Record_View (Derived_Type)
9452 then
9453 Set_First_Entity
9454 (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
9455 Set_Last_Entity
9456 (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
9457 end if;
9459 Check_Function_Writable_Actuals (N);
9460 end Build_Derived_Record_Type;
9462 ------------------------
9463 -- Build_Derived_Type --
9464 ------------------------
9466 procedure Build_Derived_Type
9467 (N : Node_Id;
9468 Parent_Type : Entity_Id;
9469 Derived_Type : Entity_Id;
9470 Is_Completion : Boolean;
9471 Derive_Subps : Boolean := True)
9473 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
9475 begin
9476 -- Set common attributes
9478 Set_Scope (Derived_Type, Current_Scope);
9480 Set_Etype (Derived_Type, Parent_Base);
9481 Set_Ekind (Derived_Type, Ekind (Parent_Base));
9482 Propagate_Concurrent_Flags (Derived_Type, Parent_Base);
9484 Set_Size_Info (Derived_Type, Parent_Type);
9485 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
9486 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
9487 Set_Disable_Controlled (Derived_Type, Disable_Controlled (Parent_Type));
9489 Set_Is_Tagged_Type (Derived_Type, Is_Tagged_Type (Parent_Type));
9490 Set_Is_Volatile (Derived_Type, Is_Volatile (Parent_Type));
9492 if Is_Tagged_Type (Derived_Type) then
9493 Set_No_Tagged_Streams_Pragma
9494 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
9495 end if;
9497 -- If the parent has primitive routines, set the derived type link
9499 if Has_Primitive_Operations (Parent_Type) then
9500 Set_Derived_Type_Link (Parent_Base, Derived_Type);
9501 end if;
9503 -- If the parent type is a private subtype, the convention on the base
9504 -- type may be set in the private part, and not propagated to the
9505 -- subtype until later, so we obtain the convention from the base type.
9507 Set_Convention (Derived_Type, Convention (Parent_Base));
9509 -- Set SSO default for record or array type
9511 if (Is_Array_Type (Derived_Type) or else Is_Record_Type (Derived_Type))
9512 and then Is_Base_Type (Derived_Type)
9513 then
9514 Set_Default_SSO (Derived_Type);
9515 end if;
9517 -- A derived type inherits the Default_Initial_Condition pragma coming
9518 -- from any parent type within the derivation chain.
9520 if Has_DIC (Parent_Type) then
9521 Set_Has_Inherited_DIC (Derived_Type);
9522 end if;
9524 -- A derived type inherits any class-wide invariants coming from a
9525 -- parent type or an interface. Note that the invariant procedure of
9526 -- the parent type should not be inherited because the derived type may
9527 -- define invariants of its own.
9529 if not Is_Interface (Derived_Type) then
9530 if Has_Inherited_Invariants (Parent_Type)
9531 or else Has_Inheritable_Invariants (Parent_Type)
9532 then
9533 Set_Has_Inherited_Invariants (Derived_Type);
9535 elsif Is_Concurrent_Type (Derived_Type)
9536 or else Is_Tagged_Type (Derived_Type)
9537 then
9538 declare
9539 Iface : Entity_Id;
9540 Ifaces : Elist_Id;
9541 Iface_Elmt : Elmt_Id;
9543 begin
9544 Collect_Interfaces
9545 (T => Derived_Type,
9546 Ifaces_List => Ifaces,
9547 Exclude_Parents => True);
9549 if Present (Ifaces) then
9550 Iface_Elmt := First_Elmt (Ifaces);
9551 while Present (Iface_Elmt) loop
9552 Iface := Node (Iface_Elmt);
9554 if Has_Inheritable_Invariants (Iface) then
9555 Set_Has_Inherited_Invariants (Derived_Type);
9556 exit;
9557 end if;
9559 Next_Elmt (Iface_Elmt);
9560 end loop;
9561 end if;
9562 end;
9563 end if;
9564 end if;
9566 -- We similarly inherit predicates. Note that for scalar derived types
9567 -- the predicate is inherited from the first subtype, and not from its
9568 -- (anonymous) base type.
9570 if Has_Predicates (Parent_Type)
9571 or else Has_Predicates (First_Subtype (Parent_Type))
9572 then
9573 Set_Has_Predicates (Derived_Type);
9574 end if;
9576 -- The derived type inherits representation clauses from the parent
9577 -- type, and from any interfaces.
9579 Inherit_Rep_Item_Chain (Derived_Type, Parent_Type);
9581 declare
9582 Iface : Node_Id := First (Abstract_Interface_List (Derived_Type));
9583 begin
9584 while Present (Iface) loop
9585 Inherit_Rep_Item_Chain (Derived_Type, Entity (Iface));
9586 Next (Iface);
9587 end loop;
9588 end;
9590 -- If the parent type has delayed rep aspects, then mark the derived
9591 -- type as possibly inheriting a delayed rep aspect.
9593 if Has_Delayed_Rep_Aspects (Parent_Type) then
9594 Set_May_Inherit_Delayed_Rep_Aspects (Derived_Type);
9595 end if;
9597 -- A derived type becomes Ghost when its parent type is also Ghost
9598 -- (SPARK RM 6.9(9)). Note that the Ghost-related attributes are not
9599 -- directly inherited because the Ghost policy in effect may differ.
9601 if Is_Ghost_Entity (Parent_Type) then
9602 Set_Is_Ghost_Entity (Derived_Type);
9603 end if;
9605 -- Type dependent processing
9607 case Ekind (Parent_Type) is
9608 when Numeric_Kind =>
9609 Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
9611 when Array_Kind =>
9612 Build_Derived_Array_Type (N, Parent_Type, Derived_Type);
9614 when Class_Wide_Kind
9615 | E_Record_Subtype
9616 | E_Record_Type
9618 Build_Derived_Record_Type
9619 (N, Parent_Type, Derived_Type, Derive_Subps);
9620 return;
9622 when Enumeration_Kind =>
9623 Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
9625 when Access_Kind =>
9626 Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
9628 when Incomplete_Or_Private_Kind =>
9629 Build_Derived_Private_Type
9630 (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
9632 -- For discriminated types, the derivation includes deriving
9633 -- primitive operations. For others it is done below.
9635 if Is_Tagged_Type (Parent_Type)
9636 or else Has_Discriminants (Parent_Type)
9637 or else (Present (Full_View (Parent_Type))
9638 and then Has_Discriminants (Full_View (Parent_Type)))
9639 then
9640 return;
9641 end if;
9643 when Concurrent_Kind =>
9644 Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
9646 when others =>
9647 raise Program_Error;
9648 end case;
9650 -- Nothing more to do if some error occurred
9652 if Etype (Derived_Type) = Any_Type then
9653 return;
9654 end if;
9656 -- Set delayed freeze and then derive subprograms, we need to do this
9657 -- in this order so that derived subprograms inherit the derived freeze
9658 -- if necessary.
9660 Set_Has_Delayed_Freeze (Derived_Type);
9662 if Derive_Subps then
9663 Derive_Subprograms (Parent_Type, Derived_Type);
9664 end if;
9666 Set_Has_Primitive_Operations
9667 (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
9668 end Build_Derived_Type;
9670 -----------------------
9671 -- Build_Discriminal --
9672 -----------------------
9674 procedure Build_Discriminal (Discrim : Entity_Id) is
9675 D_Minal : Entity_Id;
9676 CR_Disc : Entity_Id;
9678 begin
9679 -- A discriminal has the same name as the discriminant
9681 D_Minal := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9683 Set_Ekind (D_Minal, E_In_Parameter);
9684 Set_Mechanism (D_Minal, Default_Mechanism);
9685 Set_Etype (D_Minal, Etype (Discrim));
9686 Set_Scope (D_Minal, Current_Scope);
9687 Set_Parent (D_Minal, Parent (Discrim));
9689 Set_Discriminal (Discrim, D_Minal);
9690 Set_Discriminal_Link (D_Minal, Discrim);
9692 -- For task types, build at once the discriminants of the corresponding
9693 -- record, which are needed if discriminants are used in entry defaults
9694 -- and in family bounds.
9696 if Is_Concurrent_Type (Current_Scope)
9697 or else
9698 Is_Limited_Type (Current_Scope)
9699 then
9700 CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9702 Set_Ekind (CR_Disc, E_In_Parameter);
9703 Set_Mechanism (CR_Disc, Default_Mechanism);
9704 Set_Etype (CR_Disc, Etype (Discrim));
9705 Set_Scope (CR_Disc, Current_Scope);
9706 Set_Discriminal_Link (CR_Disc, Discrim);
9707 Set_CR_Discriminant (Discrim, CR_Disc);
9708 end if;
9709 end Build_Discriminal;
9711 ------------------------------------
9712 -- Build_Discriminant_Constraints --
9713 ------------------------------------
9715 function Build_Discriminant_Constraints
9716 (T : Entity_Id;
9717 Def : Node_Id;
9718 Derived_Def : Boolean := False) return Elist_Id
9720 C : constant Node_Id := Constraint (Def);
9721 Nb_Discr : constant Nat := Number_Discriminants (T);
9723 Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
9724 -- Saves the expression corresponding to a given discriminant in T
9726 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
9727 -- Return the Position number within array Discr_Expr of a discriminant
9728 -- D within the discriminant list of the discriminated type T.
9730 procedure Process_Discriminant_Expression
9731 (Expr : Node_Id;
9732 D : Entity_Id);
9733 -- If this is a discriminant constraint on a partial view, do not
9734 -- generate an overflow check on the discriminant expression. The check
9735 -- will be generated when constraining the full view. Otherwise the
9736 -- backend creates duplicate symbols for the temporaries corresponding
9737 -- to the expressions to be checked, causing spurious assembler errors.
9739 ------------------
9740 -- Pos_Of_Discr --
9741 ------------------
9743 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
9744 Disc : Entity_Id;
9746 begin
9747 Disc := First_Discriminant (T);
9748 for J in Discr_Expr'Range loop
9749 if Disc = D then
9750 return J;
9751 end if;
9753 Next_Discriminant (Disc);
9754 end loop;
9756 -- Note: Since this function is called on discriminants that are
9757 -- known to belong to the discriminated type, falling through the
9758 -- loop with no match signals an internal compiler error.
9760 raise Program_Error;
9761 end Pos_Of_Discr;
9763 -------------------------------------
9764 -- Process_Discriminant_Expression --
9765 -------------------------------------
9767 procedure Process_Discriminant_Expression
9768 (Expr : Node_Id;
9769 D : Entity_Id)
9771 BDT : constant Entity_Id := Base_Type (Etype (D));
9773 begin
9774 -- If this is a discriminant constraint on a partial view, do
9775 -- not generate an overflow on the discriminant expression. The
9776 -- check will be generated when constraining the full view.
9778 if Is_Private_Type (T)
9779 and then Present (Full_View (T))
9780 then
9781 Analyze_And_Resolve (Expr, BDT, Suppress => Overflow_Check);
9782 else
9783 Analyze_And_Resolve (Expr, BDT);
9784 end if;
9785 end Process_Discriminant_Expression;
9787 -- Declarations local to Build_Discriminant_Constraints
9789 Discr : Entity_Id;
9790 E : Entity_Id;
9791 Elist : constant Elist_Id := New_Elmt_List;
9793 Constr : Node_Id;
9794 Expr : Node_Id;
9795 Id : Node_Id;
9796 Position : Nat;
9797 Found : Boolean;
9799 Discrim_Present : Boolean := False;
9801 -- Start of processing for Build_Discriminant_Constraints
9803 begin
9804 -- The following loop will process positional associations only.
9805 -- For a positional association, the (single) discriminant is
9806 -- implicitly specified by position, in textual order (RM 3.7.2).
9808 Discr := First_Discriminant (T);
9809 Constr := First (Constraints (C));
9810 for D in Discr_Expr'Range loop
9811 exit when Nkind (Constr) = N_Discriminant_Association;
9813 if No (Constr) then
9814 Error_Msg_N ("too few discriminants given in constraint", C);
9815 return New_Elmt_List;
9817 elsif Nkind (Constr) = N_Range
9818 or else (Nkind (Constr) = N_Attribute_Reference
9819 and then Attribute_Name (Constr) = Name_Range)
9820 then
9821 Error_Msg_N
9822 ("a range is not a valid discriminant constraint", Constr);
9823 Discr_Expr (D) := Error;
9825 else
9826 Process_Discriminant_Expression (Constr, Discr);
9827 Discr_Expr (D) := Constr;
9828 end if;
9830 Next_Discriminant (Discr);
9831 Next (Constr);
9832 end loop;
9834 if No (Discr) and then Present (Constr) then
9835 Error_Msg_N ("too many discriminants given in constraint", Constr);
9836 return New_Elmt_List;
9837 end if;
9839 -- Named associations can be given in any order, but if both positional
9840 -- and named associations are used in the same discriminant constraint,
9841 -- then positional associations must occur first, at their normal
9842 -- position. Hence once a named association is used, the rest of the
9843 -- discriminant constraint must use only named associations.
9845 while Present (Constr) loop
9847 -- Positional association forbidden after a named association
9849 if Nkind (Constr) /= N_Discriminant_Association then
9850 Error_Msg_N ("positional association follows named one", Constr);
9851 return New_Elmt_List;
9853 -- Otherwise it is a named association
9855 else
9856 -- E records the type of the discriminants in the named
9857 -- association. All the discriminants specified in the same name
9858 -- association must have the same type.
9860 E := Empty;
9862 -- Search the list of discriminants in T to see if the simple name
9863 -- given in the constraint matches any of them.
9865 Id := First (Selector_Names (Constr));
9866 while Present (Id) loop
9867 Found := False;
9869 -- If Original_Discriminant is present, we are processing a
9870 -- generic instantiation and this is an instance node. We need
9871 -- to find the name of the corresponding discriminant in the
9872 -- actual record type T and not the name of the discriminant in
9873 -- the generic formal. Example:
9875 -- generic
9876 -- type G (D : int) is private;
9877 -- package P is
9878 -- subtype W is G (D => 1);
9879 -- end package;
9880 -- type Rec (X : int) is record ... end record;
9881 -- package Q is new P (G => Rec);
9883 -- At the point of the instantiation, formal type G is Rec
9884 -- and therefore when reanalyzing "subtype W is G (D => 1);"
9885 -- which really looks like "subtype W is Rec (D => 1);" at
9886 -- the point of instantiation, we want to find the discriminant
9887 -- that corresponds to D in Rec, i.e. X.
9889 if Present (Original_Discriminant (Id))
9890 and then In_Instance
9891 then
9892 Discr := Find_Corresponding_Discriminant (Id, T);
9893 Found := True;
9895 else
9896 Discr := First_Discriminant (T);
9897 while Present (Discr) loop
9898 if Chars (Discr) = Chars (Id) then
9899 Found := True;
9900 exit;
9901 end if;
9903 Next_Discriminant (Discr);
9904 end loop;
9906 if not Found then
9907 Error_Msg_N ("& does not match any discriminant", Id);
9908 return New_Elmt_List;
9910 -- If the parent type is a generic formal, preserve the
9911 -- name of the discriminant for subsequent instances.
9912 -- see comment at the beginning of this if statement.
9914 elsif Is_Generic_Type (Root_Type (T)) then
9915 Set_Original_Discriminant (Id, Discr);
9916 end if;
9917 end if;
9919 Position := Pos_Of_Discr (T, Discr);
9921 if Present (Discr_Expr (Position)) then
9922 Error_Msg_N ("duplicate constraint for discriminant&", Id);
9924 else
9925 -- Each discriminant specified in the same named association
9926 -- must be associated with a separate copy of the
9927 -- corresponding expression.
9929 if Present (Next (Id)) then
9930 Expr := New_Copy_Tree (Expression (Constr));
9931 Set_Parent (Expr, Parent (Expression (Constr)));
9932 else
9933 Expr := Expression (Constr);
9934 end if;
9936 Discr_Expr (Position) := Expr;
9937 Process_Discriminant_Expression (Expr, Discr);
9938 end if;
9940 -- A discriminant association with more than one discriminant
9941 -- name is only allowed if the named discriminants are all of
9942 -- the same type (RM 3.7.1(8)).
9944 if E = Empty then
9945 E := Base_Type (Etype (Discr));
9947 elsif Base_Type (Etype (Discr)) /= E then
9948 Error_Msg_N
9949 ("all discriminants in an association " &
9950 "must have the same type", Id);
9951 end if;
9953 Next (Id);
9954 end loop;
9955 end if;
9957 Next (Constr);
9958 end loop;
9960 -- A discriminant constraint must provide exactly one value for each
9961 -- discriminant of the type (RM 3.7.1(8)).
9963 for J in Discr_Expr'Range loop
9964 if No (Discr_Expr (J)) then
9965 Error_Msg_N ("too few discriminants given in constraint", C);
9966 return New_Elmt_List;
9967 end if;
9968 end loop;
9970 -- Determine if there are discriminant expressions in the constraint
9972 for J in Discr_Expr'Range loop
9973 if Denotes_Discriminant
9974 (Discr_Expr (J), Check_Concurrent => True)
9975 then
9976 Discrim_Present := True;
9977 end if;
9978 end loop;
9980 -- Build an element list consisting of the expressions given in the
9981 -- discriminant constraint and apply the appropriate checks. The list
9982 -- is constructed after resolving any named discriminant associations
9983 -- and therefore the expressions appear in the textual order of the
9984 -- discriminants.
9986 Discr := First_Discriminant (T);
9987 for J in Discr_Expr'Range loop
9988 if Discr_Expr (J) /= Error then
9989 Append_Elmt (Discr_Expr (J), Elist);
9991 -- If any of the discriminant constraints is given by a
9992 -- discriminant and we are in a derived type declaration we
9993 -- have a discriminant renaming. Establish link between new
9994 -- and old discriminant. The new discriminant has an implicit
9995 -- dereference if the old one does.
9997 if Denotes_Discriminant (Discr_Expr (J)) then
9998 if Derived_Def then
9999 declare
10000 New_Discr : constant Entity_Id := Entity (Discr_Expr (J));
10002 begin
10003 Set_Corresponding_Discriminant (New_Discr, Discr);
10004 Set_Has_Implicit_Dereference (New_Discr,
10005 Has_Implicit_Dereference (Discr));
10006 end;
10007 end if;
10009 -- Force the evaluation of non-discriminant expressions.
10010 -- If we have found a discriminant in the constraint 3.4(26)
10011 -- and 3.8(18) demand that no range checks are performed are
10012 -- after evaluation. If the constraint is for a component
10013 -- definition that has a per-object constraint, expressions are
10014 -- evaluated but not checked either. In all other cases perform
10015 -- a range check.
10017 else
10018 if Discrim_Present then
10019 null;
10021 elsif Nkind (Parent (Parent (Def))) = N_Component_Declaration
10022 and then Has_Per_Object_Constraint
10023 (Defining_Identifier (Parent (Parent (Def))))
10024 then
10025 null;
10027 elsif Is_Access_Type (Etype (Discr)) then
10028 Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
10030 else
10031 Apply_Range_Check (Discr_Expr (J), Etype (Discr));
10032 end if;
10034 Force_Evaluation (Discr_Expr (J));
10035 end if;
10037 -- Check that the designated type of an access discriminant's
10038 -- expression is not a class-wide type unless the discriminant's
10039 -- designated type is also class-wide.
10041 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
10042 and then not Is_Class_Wide_Type
10043 (Designated_Type (Etype (Discr)))
10044 and then Etype (Discr_Expr (J)) /= Any_Type
10045 and then Is_Class_Wide_Type
10046 (Designated_Type (Etype (Discr_Expr (J))))
10047 then
10048 Wrong_Type (Discr_Expr (J), Etype (Discr));
10050 elsif Is_Access_Type (Etype (Discr))
10051 and then not Is_Access_Constant (Etype (Discr))
10052 and then Is_Access_Type (Etype (Discr_Expr (J)))
10053 and then Is_Access_Constant (Etype (Discr_Expr (J)))
10054 then
10055 Error_Msg_NE
10056 ("constraint for discriminant& must be access to variable",
10057 Def, Discr);
10058 end if;
10059 end if;
10061 Next_Discriminant (Discr);
10062 end loop;
10064 return Elist;
10065 end Build_Discriminant_Constraints;
10067 ---------------------------------
10068 -- Build_Discriminated_Subtype --
10069 ---------------------------------
10071 procedure Build_Discriminated_Subtype
10072 (T : Entity_Id;
10073 Def_Id : Entity_Id;
10074 Elist : Elist_Id;
10075 Related_Nod : Node_Id;
10076 For_Access : Boolean := False)
10078 Has_Discrs : constant Boolean := Has_Discriminants (T);
10079 Constrained : constant Boolean :=
10080 (Has_Discrs
10081 and then not Is_Empty_Elmt_List (Elist)
10082 and then not Is_Class_Wide_Type (T))
10083 or else Is_Constrained (T);
10085 begin
10086 if Ekind (T) = E_Record_Type then
10087 if For_Access then
10088 Set_Ekind (Def_Id, E_Private_Subtype);
10089 Set_Is_For_Access_Subtype (Def_Id, True);
10090 else
10091 Set_Ekind (Def_Id, E_Record_Subtype);
10092 end if;
10094 -- Inherit preelaboration flag from base, for types for which it
10095 -- may have been set: records, private types, protected types.
10097 Set_Known_To_Have_Preelab_Init
10098 (Def_Id, Known_To_Have_Preelab_Init (T));
10100 elsif Ekind (T) = E_Task_Type then
10101 Set_Ekind (Def_Id, E_Task_Subtype);
10103 elsif Ekind (T) = E_Protected_Type then
10104 Set_Ekind (Def_Id, E_Protected_Subtype);
10105 Set_Known_To_Have_Preelab_Init
10106 (Def_Id, Known_To_Have_Preelab_Init (T));
10108 elsif Is_Private_Type (T) then
10109 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
10110 Set_Known_To_Have_Preelab_Init
10111 (Def_Id, Known_To_Have_Preelab_Init (T));
10113 -- Private subtypes may have private dependents
10115 Set_Private_Dependents (Def_Id, New_Elmt_List);
10117 elsif Is_Class_Wide_Type (T) then
10118 Set_Ekind (Def_Id, E_Class_Wide_Subtype);
10120 else
10121 -- Incomplete type. Attach subtype to list of dependents, to be
10122 -- completed with full view of parent type, unless is it the
10123 -- designated subtype of a record component within an init_proc.
10124 -- This last case arises for a component of an access type whose
10125 -- designated type is incomplete (e.g. a Taft Amendment type).
10126 -- The designated subtype is within an inner scope, and needs no
10127 -- elaboration, because only the access type is needed in the
10128 -- initialization procedure.
10130 if Ekind (T) = E_Incomplete_Type then
10131 Set_Ekind (Def_Id, E_Incomplete_Subtype);
10132 else
10133 Set_Ekind (Def_Id, Ekind (T));
10134 end if;
10136 if For_Access and then Within_Init_Proc then
10137 null;
10138 else
10139 Append_Elmt (Def_Id, Private_Dependents (T));
10140 end if;
10141 end if;
10143 Set_Etype (Def_Id, T);
10144 Init_Size_Align (Def_Id);
10145 Set_Has_Discriminants (Def_Id, Has_Discrs);
10146 Set_Is_Constrained (Def_Id, Constrained);
10148 Set_First_Entity (Def_Id, First_Entity (T));
10149 Set_Last_Entity (Def_Id, Last_Entity (T));
10150 Set_Has_Implicit_Dereference
10151 (Def_Id, Has_Implicit_Dereference (T));
10152 Set_Has_Pragma_Unreferenced_Objects
10153 (Def_Id, Has_Pragma_Unreferenced_Objects (T));
10155 -- If the subtype is the completion of a private declaration, there may
10156 -- have been representation clauses for the partial view, and they must
10157 -- be preserved. Build_Derived_Type chains the inherited clauses with
10158 -- the ones appearing on the extension. If this comes from a subtype
10159 -- declaration, all clauses are inherited.
10161 if No (First_Rep_Item (Def_Id)) then
10162 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10163 end if;
10165 if Is_Tagged_Type (T) then
10166 Set_Is_Tagged_Type (Def_Id);
10167 Set_No_Tagged_Streams_Pragma (Def_Id, No_Tagged_Streams_Pragma (T));
10168 Make_Class_Wide_Type (Def_Id);
10169 end if;
10171 Set_Stored_Constraint (Def_Id, No_Elist);
10173 if Has_Discrs then
10174 Set_Discriminant_Constraint (Def_Id, Elist);
10175 Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
10176 end if;
10178 if Is_Tagged_Type (T) then
10180 -- Ada 2005 (AI-251): In case of concurrent types we inherit the
10181 -- concurrent record type (which has the list of primitive
10182 -- operations).
10184 if Ada_Version >= Ada_2005
10185 and then Is_Concurrent_Type (T)
10186 then
10187 Set_Corresponding_Record_Type (Def_Id,
10188 Corresponding_Record_Type (T));
10189 else
10190 Set_Direct_Primitive_Operations (Def_Id,
10191 Direct_Primitive_Operations (T));
10192 end if;
10194 Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
10195 end if;
10197 -- Subtypes introduced by component declarations do not need to be
10198 -- marked as delayed, and do not get freeze nodes, because the semantics
10199 -- verifies that the parents of the subtypes are frozen before the
10200 -- enclosing record is frozen.
10202 if not Is_Type (Scope (Def_Id)) then
10203 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
10205 if Is_Private_Type (T)
10206 and then Present (Full_View (T))
10207 then
10208 Conditional_Delay (Def_Id, Full_View (T));
10209 else
10210 Conditional_Delay (Def_Id, T);
10211 end if;
10212 end if;
10214 if Is_Record_Type (T) then
10215 Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
10217 if Has_Discrs
10218 and then not Is_Empty_Elmt_List (Elist)
10219 and then not For_Access
10220 then
10221 Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
10222 elsif not For_Access then
10223 Set_Cloned_Subtype (Def_Id, T);
10224 end if;
10225 end if;
10226 end Build_Discriminated_Subtype;
10228 ---------------------------
10229 -- Build_Itype_Reference --
10230 ---------------------------
10232 procedure Build_Itype_Reference
10233 (Ityp : Entity_Id;
10234 Nod : Node_Id)
10236 IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
10237 begin
10239 -- Itype references are only created for use by the back-end
10241 if Inside_A_Generic then
10242 return;
10243 else
10244 Set_Itype (IR, Ityp);
10245 Insert_After (Nod, IR);
10246 end if;
10247 end Build_Itype_Reference;
10249 ------------------------
10250 -- Build_Scalar_Bound --
10251 ------------------------
10253 function Build_Scalar_Bound
10254 (Bound : Node_Id;
10255 Par_T : Entity_Id;
10256 Der_T : Entity_Id) return Node_Id
10258 New_Bound : Entity_Id;
10260 begin
10261 -- Note: not clear why this is needed, how can the original bound
10262 -- be unanalyzed at this point? and if it is, what business do we
10263 -- have messing around with it? and why is the base type of the
10264 -- parent type the right type for the resolution. It probably is
10265 -- not. It is OK for the new bound we are creating, but not for
10266 -- the old one??? Still if it never happens, no problem.
10268 Analyze_And_Resolve (Bound, Base_Type (Par_T));
10270 if Nkind_In (Bound, N_Integer_Literal, N_Real_Literal) then
10271 New_Bound := New_Copy (Bound);
10272 Set_Etype (New_Bound, Der_T);
10273 Set_Analyzed (New_Bound);
10275 elsif Is_Entity_Name (Bound) then
10276 New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
10278 -- The following is almost certainly wrong. What business do we have
10279 -- relocating a node (Bound) that is presumably still attached to
10280 -- the tree elsewhere???
10282 else
10283 New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
10284 end if;
10286 Set_Etype (New_Bound, Der_T);
10287 return New_Bound;
10288 end Build_Scalar_Bound;
10290 --------------------------------
10291 -- Build_Underlying_Full_View --
10292 --------------------------------
10294 procedure Build_Underlying_Full_View
10295 (N : Node_Id;
10296 Typ : Entity_Id;
10297 Par : Entity_Id)
10299 Loc : constant Source_Ptr := Sloc (N);
10300 Subt : constant Entity_Id :=
10301 Make_Defining_Identifier
10302 (Loc, New_External_Name (Chars (Typ), 'S'));
10304 Constr : Node_Id;
10305 Indic : Node_Id;
10306 C : Node_Id;
10307 Id : Node_Id;
10309 procedure Set_Discriminant_Name (Id : Node_Id);
10310 -- If the derived type has discriminants, they may rename discriminants
10311 -- of the parent. When building the full view of the parent, we need to
10312 -- recover the names of the original discriminants if the constraint is
10313 -- given by named associations.
10315 ---------------------------
10316 -- Set_Discriminant_Name --
10317 ---------------------------
10319 procedure Set_Discriminant_Name (Id : Node_Id) is
10320 Disc : Entity_Id;
10322 begin
10323 Set_Original_Discriminant (Id, Empty);
10325 if Has_Discriminants (Typ) then
10326 Disc := First_Discriminant (Typ);
10327 while Present (Disc) loop
10328 if Chars (Disc) = Chars (Id)
10329 and then Present (Corresponding_Discriminant (Disc))
10330 then
10331 Set_Chars (Id, Chars (Corresponding_Discriminant (Disc)));
10332 end if;
10333 Next_Discriminant (Disc);
10334 end loop;
10335 end if;
10336 end Set_Discriminant_Name;
10338 -- Start of processing for Build_Underlying_Full_View
10340 begin
10341 if Nkind (N) = N_Full_Type_Declaration then
10342 Constr := Constraint (Subtype_Indication (Type_Definition (N)));
10344 elsif Nkind (N) = N_Subtype_Declaration then
10345 Constr := New_Copy_Tree (Constraint (Subtype_Indication (N)));
10347 elsif Nkind (N) = N_Component_Declaration then
10348 Constr :=
10349 New_Copy_Tree
10350 (Constraint (Subtype_Indication (Component_Definition (N))));
10352 else
10353 raise Program_Error;
10354 end if;
10356 C := First (Constraints (Constr));
10357 while Present (C) loop
10358 if Nkind (C) = N_Discriminant_Association then
10359 Id := First (Selector_Names (C));
10360 while Present (Id) loop
10361 Set_Discriminant_Name (Id);
10362 Next (Id);
10363 end loop;
10364 end if;
10366 Next (C);
10367 end loop;
10369 Indic :=
10370 Make_Subtype_Declaration (Loc,
10371 Defining_Identifier => Subt,
10372 Subtype_Indication =>
10373 Make_Subtype_Indication (Loc,
10374 Subtype_Mark => New_Occurrence_Of (Par, Loc),
10375 Constraint => New_Copy_Tree (Constr)));
10377 -- If this is a component subtype for an outer itype, it is not
10378 -- a list member, so simply set the parent link for analysis: if
10379 -- the enclosing type does not need to be in a declarative list,
10380 -- neither do the components.
10382 if Is_List_Member (N)
10383 and then Nkind (N) /= N_Component_Declaration
10384 then
10385 Insert_Before (N, Indic);
10386 else
10387 Set_Parent (Indic, Parent (N));
10388 end if;
10390 Analyze (Indic);
10391 Set_Underlying_Full_View (Typ, Full_View (Subt));
10392 Set_Is_Underlying_Full_View (Full_View (Subt));
10393 end Build_Underlying_Full_View;
10395 -------------------------------
10396 -- Check_Abstract_Overriding --
10397 -------------------------------
10399 procedure Check_Abstract_Overriding (T : Entity_Id) is
10400 Alias_Subp : Entity_Id;
10401 Elmt : Elmt_Id;
10402 Op_List : Elist_Id;
10403 Subp : Entity_Id;
10404 Type_Def : Node_Id;
10406 procedure Check_Pragma_Implemented (Subp : Entity_Id);
10407 -- Ada 2012 (AI05-0030): Subprogram Subp overrides an interface routine
10408 -- which has pragma Implemented already set. Check whether Subp's entity
10409 -- kind conforms to the implementation kind of the overridden routine.
10411 procedure Check_Pragma_Implemented
10412 (Subp : Entity_Id;
10413 Iface_Subp : Entity_Id);
10414 -- Ada 2012 (AI05-0030): Subprogram Subp overrides interface routine
10415 -- Iface_Subp and both entities have pragma Implemented already set on
10416 -- them. Check whether the two implementation kinds are conforming.
10418 procedure Inherit_Pragma_Implemented
10419 (Subp : Entity_Id;
10420 Iface_Subp : Entity_Id);
10421 -- Ada 2012 (AI05-0030): Interface primitive Subp overrides interface
10422 -- subprogram Iface_Subp which has been marked by pragma Implemented.
10423 -- Propagate the implementation kind of Iface_Subp to Subp.
10425 ------------------------------
10426 -- Check_Pragma_Implemented --
10427 ------------------------------
10429 procedure Check_Pragma_Implemented (Subp : Entity_Id) is
10430 Iface_Alias : constant Entity_Id := Interface_Alias (Subp);
10431 Impl_Kind : constant Name_Id := Implementation_Kind (Iface_Alias);
10432 Subp_Alias : constant Entity_Id := Alias (Subp);
10433 Contr_Typ : Entity_Id;
10434 Impl_Subp : Entity_Id;
10436 begin
10437 -- Subp must have an alias since it is a hidden entity used to link
10438 -- an interface subprogram to its overriding counterpart.
10440 pragma Assert (Present (Subp_Alias));
10442 -- Handle aliases to synchronized wrappers
10444 Impl_Subp := Subp_Alias;
10446 if Is_Primitive_Wrapper (Impl_Subp) then
10447 Impl_Subp := Wrapped_Entity (Impl_Subp);
10448 end if;
10450 -- Extract the type of the controlling formal
10452 Contr_Typ := Etype (First_Formal (Subp_Alias));
10454 if Is_Concurrent_Record_Type (Contr_Typ) then
10455 Contr_Typ := Corresponding_Concurrent_Type (Contr_Typ);
10456 end if;
10458 -- An interface subprogram whose implementation kind is By_Entry must
10459 -- be implemented by an entry.
10461 if Impl_Kind = Name_By_Entry
10462 and then Ekind (Impl_Subp) /= E_Entry
10463 then
10464 Error_Msg_Node_2 := Iface_Alias;
10465 Error_Msg_NE
10466 ("type & must implement abstract subprogram & with an entry",
10467 Subp_Alias, Contr_Typ);
10469 elsif Impl_Kind = Name_By_Protected_Procedure then
10471 -- An interface subprogram whose implementation kind is By_
10472 -- Protected_Procedure cannot be implemented by a primitive
10473 -- procedure of a task type.
10475 if Ekind (Contr_Typ) /= E_Protected_Type then
10476 Error_Msg_Node_2 := Contr_Typ;
10477 Error_Msg_NE
10478 ("interface subprogram & cannot be implemented by a " &
10479 "primitive procedure of task type &", Subp_Alias,
10480 Iface_Alias);
10482 -- An interface subprogram whose implementation kind is By_
10483 -- Protected_Procedure must be implemented by a procedure.
10485 elsif Ekind (Impl_Subp) /= E_Procedure then
10486 Error_Msg_Node_2 := Iface_Alias;
10487 Error_Msg_NE
10488 ("type & must implement abstract subprogram & with a " &
10489 "procedure", Subp_Alias, Contr_Typ);
10491 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
10492 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
10493 then
10494 Error_Msg_Name_1 := Impl_Kind;
10495 Error_Msg_N
10496 ("overriding operation& must have synchronization%",
10497 Subp_Alias);
10498 end if;
10500 -- If primitive has Optional synchronization, overriding operation
10501 -- must match if it has an explicit synchronization..
10503 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
10504 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
10505 then
10506 Error_Msg_Name_1 := Impl_Kind;
10507 Error_Msg_N
10508 ("overriding operation& must have syncrhonization%",
10509 Subp_Alias);
10510 end if;
10511 end Check_Pragma_Implemented;
10513 ------------------------------
10514 -- Check_Pragma_Implemented --
10515 ------------------------------
10517 procedure Check_Pragma_Implemented
10518 (Subp : Entity_Id;
10519 Iface_Subp : Entity_Id)
10521 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
10522 Subp_Kind : constant Name_Id := Implementation_Kind (Subp);
10524 begin
10525 -- Ada 2012 (AI05-0030): The implementation kinds of an overridden
10526 -- and overriding subprogram are different. In general this is an
10527 -- error except when the implementation kind of the overridden
10528 -- subprograms is By_Any or Optional.
10530 if Iface_Kind /= Subp_Kind
10531 and then Iface_Kind /= Name_By_Any
10532 and then Iface_Kind /= Name_Optional
10533 then
10534 if Iface_Kind = Name_By_Entry then
10535 Error_Msg_N
10536 ("incompatible implementation kind, overridden subprogram " &
10537 "is marked By_Entry", Subp);
10538 else
10539 Error_Msg_N
10540 ("incompatible implementation kind, overridden subprogram " &
10541 "is marked By_Protected_Procedure", Subp);
10542 end if;
10543 end if;
10544 end Check_Pragma_Implemented;
10546 --------------------------------
10547 -- Inherit_Pragma_Implemented --
10548 --------------------------------
10550 procedure Inherit_Pragma_Implemented
10551 (Subp : Entity_Id;
10552 Iface_Subp : Entity_Id)
10554 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
10555 Loc : constant Source_Ptr := Sloc (Subp);
10556 Impl_Prag : Node_Id;
10558 begin
10559 -- Since the implementation kind is stored as a representation item
10560 -- rather than a flag, create a pragma node.
10562 Impl_Prag :=
10563 Make_Pragma (Loc,
10564 Chars => Name_Implemented,
10565 Pragma_Argument_Associations => New_List (
10566 Make_Pragma_Argument_Association (Loc,
10567 Expression => New_Occurrence_Of (Subp, Loc)),
10569 Make_Pragma_Argument_Association (Loc,
10570 Expression => Make_Identifier (Loc, Iface_Kind))));
10572 -- The pragma doesn't need to be analyzed because it is internally
10573 -- built. It is safe to directly register it as a rep item since we
10574 -- are only interested in the characters of the implementation kind.
10576 Record_Rep_Item (Subp, Impl_Prag);
10577 end Inherit_Pragma_Implemented;
10579 -- Start of processing for Check_Abstract_Overriding
10581 begin
10582 Op_List := Primitive_Operations (T);
10584 -- Loop to check primitive operations
10586 Elmt := First_Elmt (Op_List);
10587 while Present (Elmt) loop
10588 Subp := Node (Elmt);
10589 Alias_Subp := Alias (Subp);
10591 -- Inherited subprograms are identified by the fact that they do not
10592 -- come from source, and the associated source location is the
10593 -- location of the first subtype of the derived type.
10595 -- Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
10596 -- subprograms that "require overriding".
10598 -- Special exception, do not complain about failure to override the
10599 -- stream routines _Input and _Output, as well as the primitive
10600 -- operations used in dispatching selects since we always provide
10601 -- automatic overridings for these subprograms.
10603 -- The partial view of T may have been a private extension, for
10604 -- which inherited functions dispatching on result are abstract.
10605 -- If the full view is a null extension, there is no need for
10606 -- overriding in Ada 2005, but wrappers need to be built for them
10607 -- (see exp_ch3, Build_Controlling_Function_Wrappers).
10609 if Is_Null_Extension (T)
10610 and then Has_Controlling_Result (Subp)
10611 and then Ada_Version >= Ada_2005
10612 and then Present (Alias_Subp)
10613 and then not Comes_From_Source (Subp)
10614 and then not Is_Abstract_Subprogram (Alias_Subp)
10615 and then not Is_Access_Type (Etype (Subp))
10616 then
10617 null;
10619 -- Ada 2005 (AI-251): Internal entities of interfaces need no
10620 -- processing because this check is done with the aliased
10621 -- entity
10623 elsif Present (Interface_Alias (Subp)) then
10624 null;
10626 elsif (Is_Abstract_Subprogram (Subp)
10627 or else Requires_Overriding (Subp)
10628 or else
10629 (Has_Controlling_Result (Subp)
10630 and then Present (Alias_Subp)
10631 and then not Comes_From_Source (Subp)
10632 and then Sloc (Subp) = Sloc (First_Subtype (T))))
10633 and then not Is_TSS (Subp, TSS_Stream_Input)
10634 and then not Is_TSS (Subp, TSS_Stream_Output)
10635 and then not Is_Abstract_Type (T)
10636 and then not Is_Predefined_Interface_Primitive (Subp)
10638 -- Ada 2005 (AI-251): Do not consider hidden entities associated
10639 -- with abstract interface types because the check will be done
10640 -- with the aliased entity (otherwise we generate a duplicated
10641 -- error message).
10643 and then not Present (Interface_Alias (Subp))
10644 then
10645 if Present (Alias_Subp) then
10647 -- Only perform the check for a derived subprogram when the
10648 -- type has an explicit record extension. This avoids incorrect
10649 -- flagging of abstract subprograms for the case of a type
10650 -- without an extension that is derived from a formal type
10651 -- with a tagged actual (can occur within a private part).
10653 -- Ada 2005 (AI-391): In the case of an inherited function with
10654 -- a controlling result of the type, the rule does not apply if
10655 -- the type is a null extension (unless the parent function
10656 -- itself is abstract, in which case the function must still be
10657 -- be overridden). The expander will generate an overriding
10658 -- wrapper function calling the parent subprogram (see
10659 -- Exp_Ch3.Make_Controlling_Wrapper_Functions).
10661 Type_Def := Type_Definition (Parent (T));
10663 if Nkind (Type_Def) = N_Derived_Type_Definition
10664 and then Present (Record_Extension_Part (Type_Def))
10665 and then
10666 (Ada_Version < Ada_2005
10667 or else not Is_Null_Extension (T)
10668 or else Ekind (Subp) = E_Procedure
10669 or else not Has_Controlling_Result (Subp)
10670 or else Is_Abstract_Subprogram (Alias_Subp)
10671 or else Requires_Overriding (Subp)
10672 or else Is_Access_Type (Etype (Subp)))
10673 then
10674 -- Avoid reporting error in case of abstract predefined
10675 -- primitive inherited from interface type because the
10676 -- body of internally generated predefined primitives
10677 -- of tagged types are generated later by Freeze_Type
10679 if Is_Interface (Root_Type (T))
10680 and then Is_Abstract_Subprogram (Subp)
10681 and then Is_Predefined_Dispatching_Operation (Subp)
10682 and then not Comes_From_Source (Ultimate_Alias (Subp))
10683 then
10684 null;
10686 -- A null extension is not obliged to override an inherited
10687 -- procedure subject to pragma Extensions_Visible with value
10688 -- False and at least one controlling OUT parameter
10689 -- (SPARK RM 6.1.7(6)).
10691 elsif Is_Null_Extension (T)
10692 and then Is_EVF_Procedure (Subp)
10693 then
10694 null;
10696 else
10697 Error_Msg_NE
10698 ("type must be declared abstract or & overridden",
10699 T, Subp);
10701 -- Traverse the whole chain of aliased subprograms to
10702 -- complete the error notification. This is especially
10703 -- useful for traceability of the chain of entities when
10704 -- the subprogram corresponds with an interface
10705 -- subprogram (which may be defined in another package).
10707 if Present (Alias_Subp) then
10708 declare
10709 E : Entity_Id;
10711 begin
10712 E := Subp;
10713 while Present (Alias (E)) loop
10715 -- Avoid reporting redundant errors on entities
10716 -- inherited from interfaces
10718 if Sloc (E) /= Sloc (T) then
10719 Error_Msg_Sloc := Sloc (E);
10720 Error_Msg_NE
10721 ("\& has been inherited #", T, Subp);
10722 end if;
10724 E := Alias (E);
10725 end loop;
10727 Error_Msg_Sloc := Sloc (E);
10729 -- AI05-0068: report if there is an overriding
10730 -- non-abstract subprogram that is invisible.
10732 if Is_Hidden (E)
10733 and then not Is_Abstract_Subprogram (E)
10734 then
10735 Error_Msg_NE
10736 ("\& subprogram# is not visible",
10737 T, Subp);
10739 -- Clarify the case where a non-null extension must
10740 -- override inherited procedure subject to pragma
10741 -- Extensions_Visible with value False and at least
10742 -- one controlling OUT param.
10744 elsif Is_EVF_Procedure (E) then
10745 Error_Msg_NE
10746 ("\& # is subject to Extensions_Visible False",
10747 T, Subp);
10749 else
10750 Error_Msg_NE
10751 ("\& has been inherited from subprogram #",
10752 T, Subp);
10753 end if;
10754 end;
10755 end if;
10756 end if;
10758 -- Ada 2005 (AI-345): Protected or task type implementing
10759 -- abstract interfaces.
10761 elsif Is_Concurrent_Record_Type (T)
10762 and then Present (Interfaces (T))
10763 then
10764 -- There is no need to check here RM 9.4(11.9/3) since we
10765 -- are processing the corresponding record type and the
10766 -- mode of the overriding subprograms was verified by
10767 -- Check_Conformance when the corresponding concurrent
10768 -- type declaration was analyzed.
10770 Error_Msg_NE
10771 ("interface subprogram & must be overridden", T, Subp);
10773 -- Examine primitive operations of synchronized type to find
10774 -- homonyms that have the wrong profile.
10776 declare
10777 Prim : Entity_Id;
10779 begin
10780 Prim := First_Entity (Corresponding_Concurrent_Type (T));
10781 while Present (Prim) loop
10782 if Chars (Prim) = Chars (Subp) then
10783 Error_Msg_NE
10784 ("profile is not type conformant with prefixed "
10785 & "view profile of inherited operation&",
10786 Prim, Subp);
10787 end if;
10789 Next_Entity (Prim);
10790 end loop;
10791 end;
10792 end if;
10794 else
10795 Error_Msg_Node_2 := T;
10796 Error_Msg_N
10797 ("abstract subprogram& not allowed for type&", Subp);
10799 -- Also post unconditional warning on the type (unconditional
10800 -- so that if there are more than one of these cases, we get
10801 -- them all, and not just the first one).
10803 Error_Msg_Node_2 := Subp;
10804 Error_Msg_N ("nonabstract type& has abstract subprogram&!", T);
10805 end if;
10807 -- A subprogram subject to pragma Extensions_Visible with value
10808 -- "True" cannot override a subprogram subject to the same pragma
10809 -- with value "False" (SPARK RM 6.1.7(5)).
10811 elsif Extensions_Visible_Status (Subp) = Extensions_Visible_True
10812 and then Present (Overridden_Operation (Subp))
10813 and then Extensions_Visible_Status (Overridden_Operation (Subp)) =
10814 Extensions_Visible_False
10815 then
10816 Error_Msg_Sloc := Sloc (Overridden_Operation (Subp));
10817 Error_Msg_N
10818 ("subprogram & with Extensions_Visible True cannot override "
10819 & "subprogram # with Extensions_Visible False", Subp);
10820 end if;
10822 -- Ada 2012 (AI05-0030): Perform checks related to pragma Implemented
10824 -- Subp is an expander-generated procedure which maps an interface
10825 -- alias to a protected wrapper. The interface alias is flagged by
10826 -- pragma Implemented. Ensure that Subp is a procedure when the
10827 -- implementation kind is By_Protected_Procedure or an entry when
10828 -- By_Entry.
10830 if Ada_Version >= Ada_2012
10831 and then Is_Hidden (Subp)
10832 and then Present (Interface_Alias (Subp))
10833 and then Has_Rep_Pragma (Interface_Alias (Subp), Name_Implemented)
10834 then
10835 Check_Pragma_Implemented (Subp);
10836 end if;
10838 -- Subp is an interface primitive which overrides another interface
10839 -- primitive marked with pragma Implemented.
10841 if Ada_Version >= Ada_2012
10842 and then Present (Overridden_Operation (Subp))
10843 and then Has_Rep_Pragma
10844 (Overridden_Operation (Subp), Name_Implemented)
10845 then
10846 -- If the overriding routine is also marked by Implemented, check
10847 -- that the two implementation kinds are conforming.
10849 if Has_Rep_Pragma (Subp, Name_Implemented) then
10850 Check_Pragma_Implemented
10851 (Subp => Subp,
10852 Iface_Subp => Overridden_Operation (Subp));
10854 -- Otherwise the overriding routine inherits the implementation
10855 -- kind from the overridden subprogram.
10857 else
10858 Inherit_Pragma_Implemented
10859 (Subp => Subp,
10860 Iface_Subp => Overridden_Operation (Subp));
10861 end if;
10862 end if;
10864 -- If the operation is a wrapper for a synchronized primitive, it
10865 -- may be called indirectly through a dispatching select. We assume
10866 -- that it will be referenced elsewhere indirectly, and suppress
10867 -- warnings about an unused entity.
10869 if Is_Primitive_Wrapper (Subp)
10870 and then Present (Wrapped_Entity (Subp))
10871 then
10872 Set_Referenced (Wrapped_Entity (Subp));
10873 end if;
10875 Next_Elmt (Elmt);
10876 end loop;
10877 end Check_Abstract_Overriding;
10879 ------------------------------------------------
10880 -- Check_Access_Discriminant_Requires_Limited --
10881 ------------------------------------------------
10883 procedure Check_Access_Discriminant_Requires_Limited
10884 (D : Node_Id;
10885 Loc : Node_Id)
10887 begin
10888 -- A discriminant_specification for an access discriminant shall appear
10889 -- only in the declaration for a task or protected type, or for a type
10890 -- with the reserved word 'limited' in its definition or in one of its
10891 -- ancestors (RM 3.7(10)).
10893 -- AI-0063: The proper condition is that type must be immutably limited,
10894 -- or else be a partial view.
10896 if Nkind (Discriminant_Type (D)) = N_Access_Definition then
10897 if Is_Limited_View (Current_Scope)
10898 or else
10899 (Nkind (Parent (Current_Scope)) = N_Private_Type_Declaration
10900 and then Limited_Present (Parent (Current_Scope)))
10901 then
10902 null;
10904 else
10905 Error_Msg_N
10906 ("access discriminants allowed only for limited types", Loc);
10907 end if;
10908 end if;
10909 end Check_Access_Discriminant_Requires_Limited;
10911 -----------------------------------
10912 -- Check_Aliased_Component_Types --
10913 -----------------------------------
10915 procedure Check_Aliased_Component_Types (T : Entity_Id) is
10916 C : Entity_Id;
10918 begin
10919 -- ??? Also need to check components of record extensions, but not
10920 -- components of protected types (which are always limited).
10922 -- Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
10923 -- types to be unconstrained. This is safe because it is illegal to
10924 -- create access subtypes to such types with explicit discriminant
10925 -- constraints.
10927 if not Is_Limited_Type (T) then
10928 if Ekind (T) = E_Record_Type then
10929 C := First_Component (T);
10930 while Present (C) loop
10931 if Is_Aliased (C)
10932 and then Has_Discriminants (Etype (C))
10933 and then not Is_Constrained (Etype (C))
10934 and then not In_Instance_Body
10935 and then Ada_Version < Ada_2005
10936 then
10937 Error_Msg_N
10938 ("aliased component must be constrained (RM 3.6(11))",
10940 end if;
10942 Next_Component (C);
10943 end loop;
10945 elsif Ekind (T) = E_Array_Type then
10946 if Has_Aliased_Components (T)
10947 and then Has_Discriminants (Component_Type (T))
10948 and then not Is_Constrained (Component_Type (T))
10949 and then not In_Instance_Body
10950 and then Ada_Version < Ada_2005
10951 then
10952 Error_Msg_N
10953 ("aliased component type must be constrained (RM 3.6(11))",
10955 end if;
10956 end if;
10957 end if;
10958 end Check_Aliased_Component_Types;
10960 ---------------------------------------
10961 -- Check_Anonymous_Access_Components --
10962 ---------------------------------------
10964 procedure Check_Anonymous_Access_Components
10965 (Typ_Decl : Node_Id;
10966 Typ : Entity_Id;
10967 Prev : Entity_Id;
10968 Comp_List : Node_Id)
10970 Loc : constant Source_Ptr := Sloc (Typ_Decl);
10971 Anon_Access : Entity_Id;
10972 Acc_Def : Node_Id;
10973 Comp : Node_Id;
10974 Comp_Def : Node_Id;
10975 Decl : Node_Id;
10976 Type_Def : Node_Id;
10978 procedure Build_Incomplete_Type_Declaration;
10979 -- If the record type contains components that include an access to the
10980 -- current record, then create an incomplete type declaration for the
10981 -- record, to be used as the designated type of the anonymous access.
10982 -- This is done only once, and only if there is no previous partial
10983 -- view of the type.
10985 function Designates_T (Subt : Node_Id) return Boolean;
10986 -- Check whether a node designates the enclosing record type, or 'Class
10987 -- of that type
10989 function Mentions_T (Acc_Def : Node_Id) return Boolean;
10990 -- Check whether an access definition includes a reference to
10991 -- the enclosing record type. The reference can be a subtype mark
10992 -- in the access definition itself, a 'Class attribute reference, or
10993 -- recursively a reference appearing in a parameter specification
10994 -- or result definition of an access_to_subprogram definition.
10996 --------------------------------------
10997 -- Build_Incomplete_Type_Declaration --
10998 --------------------------------------
11000 procedure Build_Incomplete_Type_Declaration is
11001 Decl : Node_Id;
11002 Inc_T : Entity_Id;
11003 H : Entity_Id;
11005 -- Is_Tagged indicates whether the type is tagged. It is tagged if
11006 -- it's "is new ... with record" or else "is tagged record ...".
11008 Is_Tagged : constant Boolean :=
11009 (Nkind (Type_Definition (Typ_Decl)) = N_Derived_Type_Definition
11010 and then
11011 Present (Record_Extension_Part (Type_Definition (Typ_Decl))))
11012 or else
11013 (Nkind (Type_Definition (Typ_Decl)) = N_Record_Definition
11014 and then Tagged_Present (Type_Definition (Typ_Decl)));
11016 begin
11017 -- If there is a previous partial view, no need to create a new one
11018 -- If the partial view, given by Prev, is incomplete, If Prev is
11019 -- a private declaration, full declaration is flagged accordingly.
11021 if Prev /= Typ then
11022 if Is_Tagged then
11023 Make_Class_Wide_Type (Prev);
11024 Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
11025 Set_Etype (Class_Wide_Type (Typ), Typ);
11026 end if;
11028 return;
11030 elsif Has_Private_Declaration (Typ) then
11032 -- If we refer to T'Class inside T, and T is the completion of a
11033 -- private type, then make sure the class-wide type exists.
11035 if Is_Tagged then
11036 Make_Class_Wide_Type (Typ);
11037 end if;
11039 return;
11041 -- If there was a previous anonymous access type, the incomplete
11042 -- type declaration will have been created already.
11044 elsif Present (Current_Entity (Typ))
11045 and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
11046 and then Full_View (Current_Entity (Typ)) = Typ
11047 then
11048 if Is_Tagged
11049 and then Comes_From_Source (Current_Entity (Typ))
11050 and then not Is_Tagged_Type (Current_Entity (Typ))
11051 then
11052 Make_Class_Wide_Type (Typ);
11053 Error_Msg_N
11054 ("incomplete view of tagged type should be declared tagged??",
11055 Parent (Current_Entity (Typ)));
11056 end if;
11057 return;
11059 else
11060 Inc_T := Make_Defining_Identifier (Loc, Chars (Typ));
11061 Decl := Make_Incomplete_Type_Declaration (Loc, Inc_T);
11063 -- Type has already been inserted into the current scope. Remove
11064 -- it, and add incomplete declaration for type, so that subsequent
11065 -- anonymous access types can use it. The entity is unchained from
11066 -- the homonym list and from immediate visibility. After analysis,
11067 -- the entity in the incomplete declaration becomes immediately
11068 -- visible in the record declaration that follows.
11070 H := Current_Entity (Typ);
11072 if H = Typ then
11073 Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
11074 else
11075 while Present (H)
11076 and then Homonym (H) /= Typ
11077 loop
11078 H := Homonym (Typ);
11079 end loop;
11081 Set_Homonym (H, Homonym (Typ));
11082 end if;
11084 Insert_Before (Typ_Decl, Decl);
11085 Analyze (Decl);
11086 Set_Full_View (Inc_T, Typ);
11088 if Is_Tagged then
11090 -- Create a common class-wide type for both views, and set the
11091 -- Etype of the class-wide type to the full view.
11093 Make_Class_Wide_Type (Inc_T);
11094 Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
11095 Set_Etype (Class_Wide_Type (Typ), Typ);
11096 end if;
11097 end if;
11098 end Build_Incomplete_Type_Declaration;
11100 ------------------
11101 -- Designates_T --
11102 ------------------
11104 function Designates_T (Subt : Node_Id) return Boolean is
11105 Type_Id : constant Name_Id := Chars (Typ);
11107 function Names_T (Nam : Node_Id) return Boolean;
11108 -- The record type has not been introduced in the current scope
11109 -- yet, so we must examine the name of the type itself, either
11110 -- an identifier T, or an expanded name of the form P.T, where
11111 -- P denotes the current scope.
11113 -------------
11114 -- Names_T --
11115 -------------
11117 function Names_T (Nam : Node_Id) return Boolean is
11118 begin
11119 if Nkind (Nam) = N_Identifier then
11120 return Chars (Nam) = Type_Id;
11122 elsif Nkind (Nam) = N_Selected_Component then
11123 if Chars (Selector_Name (Nam)) = Type_Id then
11124 if Nkind (Prefix (Nam)) = N_Identifier then
11125 return Chars (Prefix (Nam)) = Chars (Current_Scope);
11127 elsif Nkind (Prefix (Nam)) = N_Selected_Component then
11128 return Chars (Selector_Name (Prefix (Nam))) =
11129 Chars (Current_Scope);
11130 else
11131 return False;
11132 end if;
11134 else
11135 return False;
11136 end if;
11138 else
11139 return False;
11140 end if;
11141 end Names_T;
11143 -- Start of processing for Designates_T
11145 begin
11146 if Nkind (Subt) = N_Identifier then
11147 return Chars (Subt) = Type_Id;
11149 -- Reference can be through an expanded name which has not been
11150 -- analyzed yet, and which designates enclosing scopes.
11152 elsif Nkind (Subt) = N_Selected_Component then
11153 if Names_T (Subt) then
11154 return True;
11156 -- Otherwise it must denote an entity that is already visible.
11157 -- The access definition may name a subtype of the enclosing
11158 -- type, if there is a previous incomplete declaration for it.
11160 else
11161 Find_Selected_Component (Subt);
11162 return
11163 Is_Entity_Name (Subt)
11164 and then Scope (Entity (Subt)) = Current_Scope
11165 and then
11166 (Chars (Base_Type (Entity (Subt))) = Type_Id
11167 or else
11168 (Is_Class_Wide_Type (Entity (Subt))
11169 and then
11170 Chars (Etype (Base_Type (Entity (Subt)))) =
11171 Type_Id));
11172 end if;
11174 -- A reference to the current type may appear as the prefix of
11175 -- a 'Class attribute.
11177 elsif Nkind (Subt) = N_Attribute_Reference
11178 and then Attribute_Name (Subt) = Name_Class
11179 then
11180 return Names_T (Prefix (Subt));
11182 else
11183 return False;
11184 end if;
11185 end Designates_T;
11187 ----------------
11188 -- Mentions_T --
11189 ----------------
11191 function Mentions_T (Acc_Def : Node_Id) return Boolean is
11192 Param_Spec : Node_Id;
11194 Acc_Subprg : constant Node_Id :=
11195 Access_To_Subprogram_Definition (Acc_Def);
11197 begin
11198 if No (Acc_Subprg) then
11199 return Designates_T (Subtype_Mark (Acc_Def));
11200 end if;
11202 -- Component is an access_to_subprogram: examine its formals,
11203 -- and result definition in the case of an access_to_function.
11205 Param_Spec := First (Parameter_Specifications (Acc_Subprg));
11206 while Present (Param_Spec) loop
11207 if Nkind (Parameter_Type (Param_Spec)) = N_Access_Definition
11208 and then Mentions_T (Parameter_Type (Param_Spec))
11209 then
11210 return True;
11212 elsif Designates_T (Parameter_Type (Param_Spec)) then
11213 return True;
11214 end if;
11216 Next (Param_Spec);
11217 end loop;
11219 if Nkind (Acc_Subprg) = N_Access_Function_Definition then
11220 if Nkind (Result_Definition (Acc_Subprg)) =
11221 N_Access_Definition
11222 then
11223 return Mentions_T (Result_Definition (Acc_Subprg));
11224 else
11225 return Designates_T (Result_Definition (Acc_Subprg));
11226 end if;
11227 end if;
11229 return False;
11230 end Mentions_T;
11232 -- Start of processing for Check_Anonymous_Access_Components
11234 begin
11235 if No (Comp_List) then
11236 return;
11237 end if;
11239 Comp := First (Component_Items (Comp_List));
11240 while Present (Comp) loop
11241 if Nkind (Comp) = N_Component_Declaration
11242 and then Present
11243 (Access_Definition (Component_Definition (Comp)))
11244 and then
11245 Mentions_T (Access_Definition (Component_Definition (Comp)))
11246 then
11247 Comp_Def := Component_Definition (Comp);
11248 Acc_Def :=
11249 Access_To_Subprogram_Definition (Access_Definition (Comp_Def));
11251 Build_Incomplete_Type_Declaration;
11252 Anon_Access := Make_Temporary (Loc, 'S');
11254 -- Create a declaration for the anonymous access type: either
11255 -- an access_to_object or an access_to_subprogram.
11257 if Present (Acc_Def) then
11258 if Nkind (Acc_Def) = N_Access_Function_Definition then
11259 Type_Def :=
11260 Make_Access_Function_Definition (Loc,
11261 Parameter_Specifications =>
11262 Parameter_Specifications (Acc_Def),
11263 Result_Definition => Result_Definition (Acc_Def));
11264 else
11265 Type_Def :=
11266 Make_Access_Procedure_Definition (Loc,
11267 Parameter_Specifications =>
11268 Parameter_Specifications (Acc_Def));
11269 end if;
11271 else
11272 Type_Def :=
11273 Make_Access_To_Object_Definition (Loc,
11274 Subtype_Indication =>
11275 Relocate_Node
11276 (Subtype_Mark (Access_Definition (Comp_Def))));
11278 Set_Constant_Present
11279 (Type_Def, Constant_Present (Access_Definition (Comp_Def)));
11280 Set_All_Present
11281 (Type_Def, All_Present (Access_Definition (Comp_Def)));
11282 end if;
11284 Set_Null_Exclusion_Present
11285 (Type_Def,
11286 Null_Exclusion_Present (Access_Definition (Comp_Def)));
11288 Decl :=
11289 Make_Full_Type_Declaration (Loc,
11290 Defining_Identifier => Anon_Access,
11291 Type_Definition => Type_Def);
11293 Insert_Before (Typ_Decl, Decl);
11294 Analyze (Decl);
11296 -- If an access to subprogram, create the extra formals
11298 if Present (Acc_Def) then
11299 Create_Extra_Formals (Designated_Type (Anon_Access));
11301 -- If an access to object, preserve entity of designated type,
11302 -- for ASIS use, before rewriting the component definition.
11304 else
11305 declare
11306 Desig : Entity_Id;
11308 begin
11309 Desig := Entity (Subtype_Indication (Type_Def));
11311 -- If the access definition is to the current record,
11312 -- the visible entity at this point is an incomplete
11313 -- type. Retrieve the full view to simplify ASIS queries
11315 if Ekind (Desig) = E_Incomplete_Type then
11316 Desig := Full_View (Desig);
11317 end if;
11319 Set_Entity
11320 (Subtype_Mark (Access_Definition (Comp_Def)), Desig);
11321 end;
11322 end if;
11324 Rewrite (Comp_Def,
11325 Make_Component_Definition (Loc,
11326 Subtype_Indication =>
11327 New_Occurrence_Of (Anon_Access, Loc)));
11329 if Ekind (Designated_Type (Anon_Access)) = E_Subprogram_Type then
11330 Set_Ekind (Anon_Access, E_Anonymous_Access_Subprogram_Type);
11331 else
11332 Set_Ekind (Anon_Access, E_Anonymous_Access_Type);
11333 end if;
11335 Set_Is_Local_Anonymous_Access (Anon_Access);
11336 end if;
11338 Next (Comp);
11339 end loop;
11341 if Present (Variant_Part (Comp_List)) then
11342 declare
11343 V : Node_Id;
11344 begin
11345 V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
11346 while Present (V) loop
11347 Check_Anonymous_Access_Components
11348 (Typ_Decl, Typ, Prev, Component_List (V));
11349 Next_Non_Pragma (V);
11350 end loop;
11351 end;
11352 end if;
11353 end Check_Anonymous_Access_Components;
11355 ----------------------
11356 -- Check_Completion --
11357 ----------------------
11359 procedure Check_Completion (Body_Id : Node_Id := Empty) is
11360 E : Entity_Id;
11362 procedure Post_Error;
11363 -- Post error message for lack of completion for entity E
11365 ----------------
11366 -- Post_Error --
11367 ----------------
11369 procedure Post_Error is
11370 procedure Missing_Body;
11371 -- Output missing body message
11373 ------------------
11374 -- Missing_Body --
11375 ------------------
11377 procedure Missing_Body is
11378 begin
11379 -- Spec is in same unit, so we can post on spec
11381 if In_Same_Source_Unit (Body_Id, E) then
11382 Error_Msg_N ("missing body for &", E);
11384 -- Spec is in a separate unit, so we have to post on the body
11386 else
11387 Error_Msg_NE ("missing body for & declared#!", Body_Id, E);
11388 end if;
11389 end Missing_Body;
11391 -- Start of processing for Post_Error
11393 begin
11394 if not Comes_From_Source (E) then
11395 if Ekind_In (E, E_Task_Type, E_Protected_Type) then
11397 -- It may be an anonymous protected type created for a
11398 -- single variable. Post error on variable, if present.
11400 declare
11401 Var : Entity_Id;
11403 begin
11404 Var := First_Entity (Current_Scope);
11405 while Present (Var) loop
11406 exit when Etype (Var) = E
11407 and then Comes_From_Source (Var);
11409 Next_Entity (Var);
11410 end loop;
11412 if Present (Var) then
11413 E := Var;
11414 end if;
11415 end;
11416 end if;
11417 end if;
11419 -- If a generated entity has no completion, then either previous
11420 -- semantic errors have disabled the expansion phase, or else we had
11421 -- missing subunits, or else we are compiling without expansion,
11422 -- or else something is very wrong.
11424 if not Comes_From_Source (E) then
11425 pragma Assert
11426 (Serious_Errors_Detected > 0
11427 or else Configurable_Run_Time_Violations > 0
11428 or else Subunits_Missing
11429 or else not Expander_Active);
11430 return;
11432 -- Here for source entity
11434 else
11435 -- Here if no body to post the error message, so we post the error
11436 -- on the declaration that has no completion. This is not really
11437 -- the right place to post it, think about this later ???
11439 if No (Body_Id) then
11440 if Is_Type (E) then
11441 Error_Msg_NE
11442 ("missing full declaration for }", Parent (E), E);
11443 else
11444 Error_Msg_NE ("missing body for &", Parent (E), E);
11445 end if;
11447 -- Package body has no completion for a declaration that appears
11448 -- in the corresponding spec. Post error on the body, with a
11449 -- reference to the non-completed declaration.
11451 else
11452 Error_Msg_Sloc := Sloc (E);
11454 if Is_Type (E) then
11455 Error_Msg_NE ("missing full declaration for }!", Body_Id, E);
11457 elsif Is_Overloadable (E)
11458 and then Current_Entity_In_Scope (E) /= E
11459 then
11460 -- It may be that the completion is mistyped and appears as
11461 -- a distinct overloading of the entity.
11463 declare
11464 Candidate : constant Entity_Id :=
11465 Current_Entity_In_Scope (E);
11466 Decl : constant Node_Id :=
11467 Unit_Declaration_Node (Candidate);
11469 begin
11470 if Is_Overloadable (Candidate)
11471 and then Ekind (Candidate) = Ekind (E)
11472 and then Nkind (Decl) = N_Subprogram_Body
11473 and then Acts_As_Spec (Decl)
11474 then
11475 Check_Type_Conformant (Candidate, E);
11477 else
11478 Missing_Body;
11479 end if;
11480 end;
11482 else
11483 Missing_Body;
11484 end if;
11485 end if;
11486 end if;
11487 end Post_Error;
11489 -- Local variables
11491 Pack_Id : constant Entity_Id := Current_Scope;
11493 -- Start of processing for Check_Completion
11495 begin
11496 E := First_Entity (Pack_Id);
11497 while Present (E) loop
11498 if Is_Intrinsic_Subprogram (E) then
11499 null;
11501 -- The following situation requires special handling: a child unit
11502 -- that appears in the context clause of the body of its parent:
11504 -- procedure Parent.Child (...);
11506 -- with Parent.Child;
11507 -- package body Parent is
11509 -- Here Parent.Child appears as a local entity, but should not be
11510 -- flagged as requiring completion, because it is a compilation
11511 -- unit.
11513 -- Ignore missing completion for a subprogram that does not come from
11514 -- source (including the _Call primitive operation of RAS types,
11515 -- which has to have the flag Comes_From_Source for other purposes):
11516 -- we assume that the expander will provide the missing completion.
11517 -- In case of previous errors, other expansion actions that provide
11518 -- bodies for null procedures with not be invoked, so inhibit message
11519 -- in those cases.
11521 -- Note that E_Operator is not in the list that follows, because
11522 -- this kind is reserved for predefined operators, that are
11523 -- intrinsic and do not need completion.
11525 elsif Ekind_In (E, E_Function,
11526 E_Procedure,
11527 E_Generic_Function,
11528 E_Generic_Procedure)
11529 then
11530 if Has_Completion (E) then
11531 null;
11533 elsif Is_Subprogram (E) and then Is_Abstract_Subprogram (E) then
11534 null;
11536 elsif Is_Subprogram (E)
11537 and then (not Comes_From_Source (E)
11538 or else Chars (E) = Name_uCall)
11539 then
11540 null;
11542 elsif
11543 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
11544 then
11545 null;
11547 elsif Nkind (Parent (E)) = N_Procedure_Specification
11548 and then Null_Present (Parent (E))
11549 and then Serious_Errors_Detected > 0
11550 then
11551 null;
11553 else
11554 Post_Error;
11555 end if;
11557 elsif Is_Entry (E) then
11558 if not Has_Completion (E) and then
11559 (Ekind (Scope (E)) = E_Protected_Object
11560 or else Ekind (Scope (E)) = E_Protected_Type)
11561 then
11562 Post_Error;
11563 end if;
11565 elsif Is_Package_Or_Generic_Package (E) then
11566 if Unit_Requires_Body (E) then
11567 if not Has_Completion (E)
11568 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
11569 N_Compilation_Unit
11570 then
11571 Post_Error;
11572 end if;
11574 elsif not Is_Child_Unit (E) then
11575 May_Need_Implicit_Body (E);
11576 end if;
11578 -- A formal incomplete type (Ada 2012) does not require a completion;
11579 -- other incomplete type declarations do.
11581 elsif Ekind (E) = E_Incomplete_Type
11582 and then No (Underlying_Type (E))
11583 and then not Is_Generic_Type (E)
11584 then
11585 Post_Error;
11587 elsif Ekind_In (E, E_Task_Type, E_Protected_Type)
11588 and then not Has_Completion (E)
11589 then
11590 Post_Error;
11592 -- A single task declared in the current scope is a constant, verify
11593 -- that the body of its anonymous type is in the same scope. If the
11594 -- task is defined elsewhere, this may be a renaming declaration for
11595 -- which no completion is needed.
11597 elsif Ekind (E) = E_Constant
11598 and then Ekind (Etype (E)) = E_Task_Type
11599 and then not Has_Completion (Etype (E))
11600 and then Scope (Etype (E)) = Current_Scope
11601 then
11602 Post_Error;
11604 elsif Ekind (E) = E_Protected_Object
11605 and then not Has_Completion (Etype (E))
11606 then
11607 Post_Error;
11609 elsif Ekind (E) = E_Record_Type then
11610 if Is_Tagged_Type (E) then
11611 Check_Abstract_Overriding (E);
11612 Check_Conventions (E);
11613 end if;
11615 Check_Aliased_Component_Types (E);
11617 elsif Ekind (E) = E_Array_Type then
11618 Check_Aliased_Component_Types (E);
11620 end if;
11622 Next_Entity (E);
11623 end loop;
11624 end Check_Completion;
11626 ------------------------------------
11627 -- Check_CPP_Type_Has_No_Defaults --
11628 ------------------------------------
11630 procedure Check_CPP_Type_Has_No_Defaults (T : Entity_Id) is
11631 Tdef : constant Node_Id := Type_Definition (Declaration_Node (T));
11632 Clist : Node_Id;
11633 Comp : Node_Id;
11635 begin
11636 -- Obtain the component list
11638 if Nkind (Tdef) = N_Record_Definition then
11639 Clist := Component_List (Tdef);
11640 else pragma Assert (Nkind (Tdef) = N_Derived_Type_Definition);
11641 Clist := Component_List (Record_Extension_Part (Tdef));
11642 end if;
11644 -- Check all components to ensure no default expressions
11646 if Present (Clist) then
11647 Comp := First (Component_Items (Clist));
11648 while Present (Comp) loop
11649 if Present (Expression (Comp)) then
11650 Error_Msg_N
11651 ("component of imported 'C'P'P type cannot have "
11652 & "default expression", Expression (Comp));
11653 end if;
11655 Next (Comp);
11656 end loop;
11657 end if;
11658 end Check_CPP_Type_Has_No_Defaults;
11660 ----------------------------
11661 -- Check_Delta_Expression --
11662 ----------------------------
11664 procedure Check_Delta_Expression (E : Node_Id) is
11665 begin
11666 if not (Is_Real_Type (Etype (E))) then
11667 Wrong_Type (E, Any_Real);
11669 elsif not Is_OK_Static_Expression (E) then
11670 Flag_Non_Static_Expr
11671 ("non-static expression used for delta value!", E);
11673 elsif not UR_Is_Positive (Expr_Value_R (E)) then
11674 Error_Msg_N ("delta expression must be positive", E);
11676 else
11677 return;
11678 end if;
11680 -- If any of above errors occurred, then replace the incorrect
11681 -- expression by the real 0.1, which should prevent further errors.
11683 Rewrite (E,
11684 Make_Real_Literal (Sloc (E), Ureal_Tenth));
11685 Analyze_And_Resolve (E, Standard_Float);
11686 end Check_Delta_Expression;
11688 -----------------------------
11689 -- Check_Digits_Expression --
11690 -----------------------------
11692 procedure Check_Digits_Expression (E : Node_Id) is
11693 begin
11694 if not (Is_Integer_Type (Etype (E))) then
11695 Wrong_Type (E, Any_Integer);
11697 elsif not Is_OK_Static_Expression (E) then
11698 Flag_Non_Static_Expr
11699 ("non-static expression used for digits value!", E);
11701 elsif Expr_Value (E) <= 0 then
11702 Error_Msg_N ("digits value must be greater than zero", E);
11704 else
11705 return;
11706 end if;
11708 -- If any of above errors occurred, then replace the incorrect
11709 -- expression by the integer 1, which should prevent further errors.
11711 Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
11712 Analyze_And_Resolve (E, Standard_Integer);
11714 end Check_Digits_Expression;
11716 --------------------------
11717 -- Check_Initialization --
11718 --------------------------
11720 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
11721 begin
11722 -- Special processing for limited types
11724 if Is_Limited_Type (T)
11725 and then not In_Instance
11726 and then not In_Inlined_Body
11727 then
11728 if not OK_For_Limited_Init (T, Exp) then
11730 -- In GNAT mode, this is just a warning, to allow it to be evilly
11731 -- turned off. Otherwise it is a real error.
11733 if GNAT_Mode then
11734 Error_Msg_N
11735 ("??cannot initialize entities of limited type!", Exp);
11737 elsif Ada_Version < Ada_2005 then
11739 -- The side effect removal machinery may generate illegal Ada
11740 -- code to avoid the usage of access types and 'reference in
11741 -- SPARK mode. Since this is legal code with respect to theorem
11742 -- proving, do not emit the error.
11744 if GNATprove_Mode
11745 and then Nkind (Exp) = N_Function_Call
11746 and then Nkind (Parent (Exp)) = N_Object_Declaration
11747 and then not Comes_From_Source
11748 (Defining_Identifier (Parent (Exp)))
11749 then
11750 null;
11752 else
11753 Error_Msg_N
11754 ("cannot initialize entities of limited type", Exp);
11755 Explain_Limited_Type (T, Exp);
11756 end if;
11758 else
11759 -- Specialize error message according to kind of illegal
11760 -- initial expression.
11762 if Nkind (Exp) = N_Type_Conversion
11763 and then Nkind (Expression (Exp)) = N_Function_Call
11764 then
11765 Error_Msg_N
11766 ("illegal context for call"
11767 & " to function with limited result", Exp);
11769 else
11770 Error_Msg_N
11771 ("initialization of limited object requires aggregate "
11772 & "or function call", Exp);
11773 end if;
11774 end if;
11775 end if;
11776 end if;
11778 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag gets
11779 -- set unless we can be sure that no range check is required.
11781 if (GNATprove_Mode or not Expander_Active)
11782 and then Is_Scalar_Type (T)
11783 and then not Is_In_Range (Exp, T, Assume_Valid => True)
11784 then
11785 Set_Do_Range_Check (Exp);
11786 end if;
11787 end Check_Initialization;
11789 ----------------------
11790 -- Check_Interfaces --
11791 ----------------------
11793 procedure Check_Interfaces (N : Node_Id; Def : Node_Id) is
11794 Parent_Type : constant Entity_Id := Etype (Defining_Identifier (N));
11796 Iface : Node_Id;
11797 Iface_Def : Node_Id;
11798 Iface_Typ : Entity_Id;
11799 Parent_Node : Node_Id;
11801 Is_Task : Boolean := False;
11802 -- Set True if parent type or any progenitor is a task interface
11804 Is_Protected : Boolean := False;
11805 -- Set True if parent type or any progenitor is a protected interface
11807 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
11808 -- Check that a progenitor is compatible with declaration. If an error
11809 -- message is output, it is posted on Error_Node.
11811 ------------------
11812 -- Check_Ifaces --
11813 ------------------
11815 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
11816 Iface_Id : constant Entity_Id :=
11817 Defining_Identifier (Parent (Iface_Def));
11818 Type_Def : Node_Id;
11820 begin
11821 if Nkind (N) = N_Private_Extension_Declaration then
11822 Type_Def := N;
11823 else
11824 Type_Def := Type_Definition (N);
11825 end if;
11827 if Is_Task_Interface (Iface_Id) then
11828 Is_Task := True;
11830 elsif Is_Protected_Interface (Iface_Id) then
11831 Is_Protected := True;
11832 end if;
11834 if Is_Synchronized_Interface (Iface_Id) then
11836 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
11837 -- extension derived from a synchronized interface must explicitly
11838 -- be declared synchronized, because the full view will be a
11839 -- synchronized type.
11841 if Nkind (N) = N_Private_Extension_Declaration then
11842 if not Synchronized_Present (N) then
11843 Error_Msg_NE
11844 ("private extension of& must be explicitly synchronized",
11845 N, Iface_Id);
11846 end if;
11848 -- However, by 3.9.4(16/2), a full type that is a record extension
11849 -- is never allowed to derive from a synchronized interface (note
11850 -- that interfaces must be excluded from this check, because those
11851 -- are represented by derived type definitions in some cases).
11853 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition
11854 and then not Interface_Present (Type_Definition (N))
11855 then
11856 Error_Msg_N ("record extension cannot derive from synchronized "
11857 & "interface", Error_Node);
11858 end if;
11859 end if;
11861 -- Check that the characteristics of the progenitor are compatible
11862 -- with the explicit qualifier in the declaration.
11863 -- The check only applies to qualifiers that come from source.
11864 -- Limited_Present also appears in the declaration of corresponding
11865 -- records, and the check does not apply to them.
11867 if Limited_Present (Type_Def)
11868 and then not
11869 Is_Concurrent_Record_Type (Defining_Identifier (N))
11870 then
11871 if Is_Limited_Interface (Parent_Type)
11872 and then not Is_Limited_Interface (Iface_Id)
11873 then
11874 Error_Msg_NE
11875 ("progenitor & must be limited interface",
11876 Error_Node, Iface_Id);
11878 elsif
11879 (Task_Present (Iface_Def)
11880 or else Protected_Present (Iface_Def)
11881 or else Synchronized_Present (Iface_Def))
11882 and then Nkind (N) /= N_Private_Extension_Declaration
11883 and then not Error_Posted (N)
11884 then
11885 Error_Msg_NE
11886 ("progenitor & must be limited interface",
11887 Error_Node, Iface_Id);
11888 end if;
11890 -- Protected interfaces can only inherit from limited, synchronized
11891 -- or protected interfaces.
11893 elsif Nkind (N) = N_Full_Type_Declaration
11894 and then Protected_Present (Type_Def)
11895 then
11896 if Limited_Present (Iface_Def)
11897 or else Synchronized_Present (Iface_Def)
11898 or else Protected_Present (Iface_Def)
11899 then
11900 null;
11902 elsif Task_Present (Iface_Def) then
11903 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
11904 & "from task interface", Error_Node);
11906 else
11907 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
11908 & "from non-limited interface", Error_Node);
11909 end if;
11911 -- Ada 2005 (AI-345): Synchronized interfaces can only inherit from
11912 -- limited and synchronized.
11914 elsif Synchronized_Present (Type_Def) then
11915 if Limited_Present (Iface_Def)
11916 or else Synchronized_Present (Iface_Def)
11917 then
11918 null;
11920 elsif Protected_Present (Iface_Def)
11921 and then Nkind (N) /= N_Private_Extension_Declaration
11922 then
11923 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11924 & "from protected interface", Error_Node);
11926 elsif Task_Present (Iface_Def)
11927 and then Nkind (N) /= N_Private_Extension_Declaration
11928 then
11929 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11930 & "from task interface", Error_Node);
11932 elsif not Is_Limited_Interface (Iface_Id) then
11933 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11934 & "from non-limited interface", Error_Node);
11935 end if;
11937 -- Ada 2005 (AI-345): Task interfaces can only inherit from limited,
11938 -- synchronized or task interfaces.
11940 elsif Nkind (N) = N_Full_Type_Declaration
11941 and then Task_Present (Type_Def)
11942 then
11943 if Limited_Present (Iface_Def)
11944 or else Synchronized_Present (Iface_Def)
11945 or else Task_Present (Iface_Def)
11946 then
11947 null;
11949 elsif Protected_Present (Iface_Def) then
11950 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
11951 & "protected interface", Error_Node);
11953 else
11954 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
11955 & "non-limited interface", Error_Node);
11956 end if;
11957 end if;
11958 end Check_Ifaces;
11960 -- Start of processing for Check_Interfaces
11962 begin
11963 if Is_Interface (Parent_Type) then
11964 if Is_Task_Interface (Parent_Type) then
11965 Is_Task := True;
11967 elsif Is_Protected_Interface (Parent_Type) then
11968 Is_Protected := True;
11969 end if;
11970 end if;
11972 if Nkind (N) = N_Private_Extension_Declaration then
11974 -- Check that progenitors are compatible with declaration
11976 Iface := First (Interface_List (Def));
11977 while Present (Iface) loop
11978 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
11980 Parent_Node := Parent (Base_Type (Iface_Typ));
11981 Iface_Def := Type_Definition (Parent_Node);
11983 if not Is_Interface (Iface_Typ) then
11984 Diagnose_Interface (Iface, Iface_Typ);
11985 else
11986 Check_Ifaces (Iface_Def, Iface);
11987 end if;
11989 Next (Iface);
11990 end loop;
11992 if Is_Task and Is_Protected then
11993 Error_Msg_N
11994 ("type cannot derive from task and protected interface", N);
11995 end if;
11997 return;
11998 end if;
12000 -- Full type declaration of derived type.
12001 -- Check compatibility with parent if it is interface type
12003 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
12004 and then Is_Interface (Parent_Type)
12005 then
12006 Parent_Node := Parent (Parent_Type);
12008 -- More detailed checks for interface varieties
12010 Check_Ifaces
12011 (Iface_Def => Type_Definition (Parent_Node),
12012 Error_Node => Subtype_Indication (Type_Definition (N)));
12013 end if;
12015 Iface := First (Interface_List (Def));
12016 while Present (Iface) loop
12017 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
12019 Parent_Node := Parent (Base_Type (Iface_Typ));
12020 Iface_Def := Type_Definition (Parent_Node);
12022 if not Is_Interface (Iface_Typ) then
12023 Diagnose_Interface (Iface, Iface_Typ);
12025 else
12026 -- "The declaration of a specific descendant of an interface
12027 -- type freezes the interface type" RM 13.14
12029 Freeze_Before (N, Iface_Typ);
12030 Check_Ifaces (Iface_Def, Error_Node => Iface);
12031 end if;
12033 Next (Iface);
12034 end loop;
12036 if Is_Task and Is_Protected then
12037 Error_Msg_N
12038 ("type cannot derive from task and protected interface", N);
12039 end if;
12040 end Check_Interfaces;
12042 ------------------------------------
12043 -- Check_Or_Process_Discriminants --
12044 ------------------------------------
12046 -- If an incomplete or private type declaration was already given for the
12047 -- type, the discriminants may have already been processed if they were
12048 -- present on the incomplete declaration. In this case a full conformance
12049 -- check has been performed in Find_Type_Name, and we then recheck here
12050 -- some properties that can't be checked on the partial view alone.
12051 -- Otherwise we call Process_Discriminants.
12053 procedure Check_Or_Process_Discriminants
12054 (N : Node_Id;
12055 T : Entity_Id;
12056 Prev : Entity_Id := Empty)
12058 begin
12059 if Has_Discriminants (T) then
12061 -- Discriminants are already set on T if they were already present
12062 -- on the partial view. Make them visible to component declarations.
12064 declare
12065 D : Entity_Id;
12066 -- Discriminant on T (full view) referencing expr on partial view
12068 Prev_D : Entity_Id;
12069 -- Entity of corresponding discriminant on partial view
12071 New_D : Node_Id;
12072 -- Discriminant specification for full view, expression is
12073 -- the syntactic copy on full view (which has been checked for
12074 -- conformance with partial view), only used here to post error
12075 -- message.
12077 begin
12078 D := First_Discriminant (T);
12079 New_D := First (Discriminant_Specifications (N));
12080 while Present (D) loop
12081 Prev_D := Current_Entity (D);
12082 Set_Current_Entity (D);
12083 Set_Is_Immediately_Visible (D);
12084 Set_Homonym (D, Prev_D);
12086 -- Handle the case where there is an untagged partial view and
12087 -- the full view is tagged: must disallow discriminants with
12088 -- defaults, unless compiling for Ada 2012, which allows a
12089 -- limited tagged type to have defaulted discriminants (see
12090 -- AI05-0214). However, suppress error here if it was already
12091 -- reported on the default expression of the partial view.
12093 if Is_Tagged_Type (T)
12094 and then Present (Expression (Parent (D)))
12095 and then (not Is_Limited_Type (Current_Scope)
12096 or else Ada_Version < Ada_2012)
12097 and then not Error_Posted (Expression (Parent (D)))
12098 then
12099 if Ada_Version >= Ada_2012 then
12100 Error_Msg_N
12101 ("discriminants of nonlimited tagged type cannot have "
12102 & "defaults",
12103 Expression (New_D));
12104 else
12105 Error_Msg_N
12106 ("discriminants of tagged type cannot have defaults",
12107 Expression (New_D));
12108 end if;
12109 end if;
12111 -- Ada 2005 (AI-230): Access discriminant allowed in
12112 -- non-limited record types.
12114 if Ada_Version < Ada_2005 then
12116 -- This restriction gets applied to the full type here. It
12117 -- has already been applied earlier to the partial view.
12119 Check_Access_Discriminant_Requires_Limited (Parent (D), N);
12120 end if;
12122 Next_Discriminant (D);
12123 Next (New_D);
12124 end loop;
12125 end;
12127 elsif Present (Discriminant_Specifications (N)) then
12128 Process_Discriminants (N, Prev);
12129 end if;
12130 end Check_Or_Process_Discriminants;
12132 ----------------------
12133 -- Check_Real_Bound --
12134 ----------------------
12136 procedure Check_Real_Bound (Bound : Node_Id) is
12137 begin
12138 if not Is_Real_Type (Etype (Bound)) then
12139 Error_Msg_N
12140 ("bound in real type definition must be of real type", Bound);
12142 elsif not Is_OK_Static_Expression (Bound) then
12143 Flag_Non_Static_Expr
12144 ("non-static expression used for real type bound!", Bound);
12146 else
12147 return;
12148 end if;
12150 Rewrite
12151 (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
12152 Analyze (Bound);
12153 Resolve (Bound, Standard_Float);
12154 end Check_Real_Bound;
12156 ------------------------------
12157 -- Complete_Private_Subtype --
12158 ------------------------------
12160 procedure Complete_Private_Subtype
12161 (Priv : Entity_Id;
12162 Full : Entity_Id;
12163 Full_Base : Entity_Id;
12164 Related_Nod : Node_Id)
12166 Save_Next_Entity : Entity_Id;
12167 Save_Homonym : Entity_Id;
12169 begin
12170 -- Set semantic attributes for (implicit) private subtype completion.
12171 -- If the full type has no discriminants, then it is a copy of the
12172 -- full view of the base. Otherwise, it is a subtype of the base with
12173 -- a possible discriminant constraint. Save and restore the original
12174 -- Next_Entity field of full to ensure that the calls to Copy_Node do
12175 -- not corrupt the entity chain.
12177 -- Note that the type of the full view is the same entity as the type
12178 -- of the partial view. In this fashion, the subtype has access to the
12179 -- correct view of the parent.
12181 Save_Next_Entity := Next_Entity (Full);
12182 Save_Homonym := Homonym (Priv);
12184 case Ekind (Full_Base) is
12185 when Class_Wide_Kind
12186 | Private_Kind
12187 | Protected_Kind
12188 | Task_Kind
12189 | E_Record_Subtype
12190 | E_Record_Type
12192 Copy_Node (Priv, Full);
12194 Set_Has_Discriminants
12195 (Full, Has_Discriminants (Full_Base));
12196 Set_Has_Unknown_Discriminants
12197 (Full, Has_Unknown_Discriminants (Full_Base));
12198 Set_First_Entity (Full, First_Entity (Full_Base));
12199 Set_Last_Entity (Full, Last_Entity (Full_Base));
12201 -- If the underlying base type is constrained, we know that the
12202 -- full view of the subtype is constrained as well (the converse
12203 -- is not necessarily true).
12205 if Is_Constrained (Full_Base) then
12206 Set_Is_Constrained (Full);
12207 end if;
12209 when others =>
12210 Copy_Node (Full_Base, Full);
12212 Set_Chars (Full, Chars (Priv));
12213 Conditional_Delay (Full, Priv);
12214 Set_Sloc (Full, Sloc (Priv));
12215 end case;
12217 Set_Next_Entity (Full, Save_Next_Entity);
12218 Set_Homonym (Full, Save_Homonym);
12219 Set_Associated_Node_For_Itype (Full, Related_Nod);
12221 -- Set common attributes for all subtypes: kind, convention, etc.
12223 Set_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
12224 Set_Convention (Full, Convention (Full_Base));
12226 -- The Etype of the full view is inconsistent. Gigi needs to see the
12227 -- structural full view, which is what the current scheme gives: the
12228 -- Etype of the full view is the etype of the full base. However, if the
12229 -- full base is a derived type, the full view then looks like a subtype
12230 -- of the parent, not a subtype of the full base. If instead we write:
12232 -- Set_Etype (Full, Full_Base);
12234 -- then we get inconsistencies in the front-end (confusion between
12235 -- views). Several outstanding bugs are related to this ???
12237 Set_Is_First_Subtype (Full, False);
12238 Set_Scope (Full, Scope (Priv));
12239 Set_Size_Info (Full, Full_Base);
12240 Set_RM_Size (Full, RM_Size (Full_Base));
12241 Set_Is_Itype (Full);
12243 -- A subtype of a private-type-without-discriminants, whose full-view
12244 -- has discriminants with default expressions, is not constrained.
12246 if not Has_Discriminants (Priv) then
12247 Set_Is_Constrained (Full, Is_Constrained (Full_Base));
12249 if Has_Discriminants (Full_Base) then
12250 Set_Discriminant_Constraint
12251 (Full, Discriminant_Constraint (Full_Base));
12253 -- The partial view may have been indefinite, the full view
12254 -- might not be.
12256 Set_Has_Unknown_Discriminants
12257 (Full, Has_Unknown_Discriminants (Full_Base));
12258 end if;
12259 end if;
12261 Set_First_Rep_Item (Full, First_Rep_Item (Full_Base));
12262 Set_Depends_On_Private (Full, Has_Private_Component (Full));
12264 -- Freeze the private subtype entity if its parent is delayed, and not
12265 -- already frozen. We skip this processing if the type is an anonymous
12266 -- subtype of a record component, or is the corresponding record of a
12267 -- protected type, since these are processed when the enclosing type
12268 -- is frozen. If the parent type is declared in a nested package then
12269 -- the freezing of the private and full views also happens later.
12271 if not Is_Type (Scope (Full)) then
12272 if Is_Itype (Priv)
12273 and then In_Same_Source_Unit (Full, Full_Base)
12274 and then Scope (Full_Base) /= Scope (Full)
12275 then
12276 Set_Has_Delayed_Freeze (Full);
12277 Set_Has_Delayed_Freeze (Priv);
12279 else
12280 Set_Has_Delayed_Freeze (Full,
12281 Has_Delayed_Freeze (Full_Base)
12282 and then not Is_Frozen (Full_Base));
12283 end if;
12284 end if;
12286 Set_Freeze_Node (Full, Empty);
12287 Set_Is_Frozen (Full, False);
12288 Set_Full_View (Priv, Full);
12290 if Has_Discriminants (Full) then
12291 Set_Stored_Constraint_From_Discriminant_Constraint (Full);
12292 Set_Stored_Constraint (Priv, Stored_Constraint (Full));
12294 if Has_Unknown_Discriminants (Full) then
12295 Set_Discriminant_Constraint (Full, No_Elist);
12296 end if;
12297 end if;
12299 if Ekind (Full_Base) = E_Record_Type
12300 and then Has_Discriminants (Full_Base)
12301 and then Has_Discriminants (Priv) -- might not, if errors
12302 and then not Has_Unknown_Discriminants (Priv)
12303 and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
12304 then
12305 Create_Constrained_Components
12306 (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
12308 -- If the full base is itself derived from private, build a congruent
12309 -- subtype of its underlying type, for use by the back end. For a
12310 -- constrained record component, the declaration cannot be placed on
12311 -- the component list, but it must nevertheless be built an analyzed, to
12312 -- supply enough information for Gigi to compute the size of component.
12314 elsif Ekind (Full_Base) in Private_Kind
12315 and then Is_Derived_Type (Full_Base)
12316 and then Has_Discriminants (Full_Base)
12317 and then (Ekind (Current_Scope) /= E_Record_Subtype)
12318 then
12319 if not Is_Itype (Priv)
12320 and then
12321 Nkind (Subtype_Indication (Parent (Priv))) = N_Subtype_Indication
12322 then
12323 Build_Underlying_Full_View
12324 (Parent (Priv), Full, Etype (Full_Base));
12326 elsif Nkind (Related_Nod) = N_Component_Declaration then
12327 Build_Underlying_Full_View (Related_Nod, Full, Etype (Full_Base));
12328 end if;
12330 elsif Is_Record_Type (Full_Base) then
12332 -- Show Full is simply a renaming of Full_Base
12334 Set_Cloned_Subtype (Full, Full_Base);
12335 end if;
12337 -- It is unsafe to share the bounds of a scalar type, because the Itype
12338 -- is elaborated on demand, and if a bound is non-static then different
12339 -- orders of elaboration in different units will lead to different
12340 -- external symbols.
12342 if Is_Scalar_Type (Full_Base) then
12343 Set_Scalar_Range (Full,
12344 Make_Range (Sloc (Related_Nod),
12345 Low_Bound =>
12346 Duplicate_Subexpr_No_Checks (Type_Low_Bound (Full_Base)),
12347 High_Bound =>
12348 Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
12350 -- This completion inherits the bounds of the full parent, but if
12351 -- the parent is an unconstrained floating point type, so is the
12352 -- completion.
12354 if Is_Floating_Point_Type (Full_Base) then
12355 Set_Includes_Infinities
12356 (Scalar_Range (Full), Has_Infinities (Full_Base));
12357 end if;
12358 end if;
12360 -- ??? It seems that a lot of fields are missing that should be copied
12361 -- from Full_Base to Full. Here are some that are introduced in a
12362 -- non-disruptive way but a cleanup is necessary.
12364 if Is_Tagged_Type (Full_Base) then
12365 Set_Is_Tagged_Type (Full);
12366 Set_Direct_Primitive_Operations
12367 (Full, Direct_Primitive_Operations (Full_Base));
12368 Set_No_Tagged_Streams_Pragma
12369 (Full, No_Tagged_Streams_Pragma (Full_Base));
12371 -- Inherit class_wide type of full_base in case the partial view was
12372 -- not tagged. Otherwise it has already been created when the private
12373 -- subtype was analyzed.
12375 if No (Class_Wide_Type (Full)) then
12376 Set_Class_Wide_Type (Full, Class_Wide_Type (Full_Base));
12377 end if;
12379 -- If this is a subtype of a protected or task type, constrain its
12380 -- corresponding record, unless this is a subtype without constraints,
12381 -- i.e. a simple renaming as with an actual subtype in an instance.
12383 elsif Is_Concurrent_Type (Full_Base) then
12384 if Has_Discriminants (Full)
12385 and then Present (Corresponding_Record_Type (Full_Base))
12386 and then
12387 not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
12388 then
12389 Set_Corresponding_Record_Type (Full,
12390 Constrain_Corresponding_Record
12391 (Full, Corresponding_Record_Type (Full_Base), Related_Nod));
12393 else
12394 Set_Corresponding_Record_Type (Full,
12395 Corresponding_Record_Type (Full_Base));
12396 end if;
12397 end if;
12399 -- Link rep item chain, and also setting of Has_Predicates from private
12400 -- subtype to full subtype, since we will need these on the full subtype
12401 -- to create the predicate function. Note that the full subtype may
12402 -- already have rep items, inherited from the full view of the base
12403 -- type, so we must be sure not to overwrite these entries.
12405 declare
12406 Append : Boolean;
12407 Item : Node_Id;
12408 Next_Item : Node_Id;
12409 Priv_Item : Node_Id;
12411 begin
12412 Item := First_Rep_Item (Full);
12413 Priv_Item := First_Rep_Item (Priv);
12415 -- If no existing rep items on full type, we can just link directly
12416 -- to the list of items on the private type, if any exist.. Same if
12417 -- the rep items are only those inherited from the base
12419 if (No (Item)
12420 or else Nkind (Item) /= N_Aspect_Specification
12421 or else Entity (Item) = Full_Base)
12422 and then Present (First_Rep_Item (Priv))
12423 then
12424 Set_First_Rep_Item (Full, Priv_Item);
12426 -- Otherwise, search to the end of items currently linked to the full
12427 -- subtype and append the private items to the end. However, if Priv
12428 -- and Full already have the same list of rep items, then the append
12429 -- is not done, as that would create a circularity.
12431 -- The partial view may have a predicate and the rep item lists of
12432 -- both views agree when inherited from the same ancestor. In that
12433 -- case, simply propagate the list from one view to the other.
12434 -- A more complex analysis needed here ???
12436 elsif Present (Priv_Item)
12437 and then Item = Next_Rep_Item (Priv_Item)
12438 then
12439 Set_First_Rep_Item (Full, Priv_Item);
12441 elsif Item /= Priv_Item then
12442 Append := True;
12443 loop
12444 Next_Item := Next_Rep_Item (Item);
12445 exit when No (Next_Item);
12446 Item := Next_Item;
12448 -- If the private view has aspect specifications, the full view
12449 -- inherits them. Since these aspects may already have been
12450 -- attached to the full view during derivation, do not append
12451 -- them if already present.
12453 if Item = First_Rep_Item (Priv) then
12454 Append := False;
12455 exit;
12456 end if;
12457 end loop;
12459 -- And link the private type items at the end of the chain
12461 if Append then
12462 Set_Next_Rep_Item (Item, First_Rep_Item (Priv));
12463 end if;
12464 end if;
12465 end;
12467 -- Make sure Has_Predicates is set on full type if it is set on the
12468 -- private type. Note that it may already be set on the full type and
12469 -- if so, we don't want to unset it. Similarly, propagate information
12470 -- about delayed aspects, because the corresponding pragmas must be
12471 -- analyzed when one of the views is frozen. This last step is needed
12472 -- in particular when the full type is a scalar type for which an
12473 -- anonymous base type is constructed.
12475 -- The predicate functions are generated either at the freeze point
12476 -- of the type or at the end of the visible part, and we must avoid
12477 -- generating them twice.
12479 if Has_Predicates (Priv) then
12480 Set_Has_Predicates (Full);
12482 if Present (Predicate_Function (Priv))
12483 and then No (Predicate_Function (Full))
12484 then
12485 Set_Predicate_Function (Full, Predicate_Function (Priv));
12486 end if;
12487 end if;
12489 if Has_Delayed_Aspects (Priv) then
12490 Set_Has_Delayed_Aspects (Full);
12491 end if;
12492 end Complete_Private_Subtype;
12494 ----------------------------
12495 -- Constant_Redeclaration --
12496 ----------------------------
12498 procedure Constant_Redeclaration
12499 (Id : Entity_Id;
12500 N : Node_Id;
12501 T : out Entity_Id)
12503 Prev : constant Entity_Id := Current_Entity_In_Scope (Id);
12504 Obj_Def : constant Node_Id := Object_Definition (N);
12505 New_T : Entity_Id;
12507 procedure Check_Possible_Deferred_Completion
12508 (Prev_Id : Entity_Id;
12509 Prev_Obj_Def : Node_Id;
12510 Curr_Obj_Def : Node_Id);
12511 -- Determine whether the two object definitions describe the partial
12512 -- and the full view of a constrained deferred constant. Generate
12513 -- a subtype for the full view and verify that it statically matches
12514 -- the subtype of the partial view.
12516 procedure Check_Recursive_Declaration (Typ : Entity_Id);
12517 -- If deferred constant is an access type initialized with an allocator,
12518 -- check whether there is an illegal recursion in the definition,
12519 -- through a default value of some record subcomponent. This is normally
12520 -- detected when generating init procs, but requires this additional
12521 -- mechanism when expansion is disabled.
12523 ----------------------------------------
12524 -- Check_Possible_Deferred_Completion --
12525 ----------------------------------------
12527 procedure Check_Possible_Deferred_Completion
12528 (Prev_Id : Entity_Id;
12529 Prev_Obj_Def : Node_Id;
12530 Curr_Obj_Def : Node_Id)
12532 begin
12533 if Nkind (Prev_Obj_Def) = N_Subtype_Indication
12534 and then Present (Constraint (Prev_Obj_Def))
12535 and then Nkind (Curr_Obj_Def) = N_Subtype_Indication
12536 and then Present (Constraint (Curr_Obj_Def))
12537 then
12538 declare
12539 Loc : constant Source_Ptr := Sloc (N);
12540 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
12541 Decl : constant Node_Id :=
12542 Make_Subtype_Declaration (Loc,
12543 Defining_Identifier => Def_Id,
12544 Subtype_Indication =>
12545 Relocate_Node (Curr_Obj_Def));
12547 begin
12548 Insert_Before_And_Analyze (N, Decl);
12549 Set_Etype (Id, Def_Id);
12551 if not Subtypes_Statically_Match (Etype (Prev_Id), Def_Id) then
12552 Error_Msg_Sloc := Sloc (Prev_Id);
12553 Error_Msg_N ("subtype does not statically match deferred "
12554 & "declaration #", N);
12555 end if;
12556 end;
12557 end if;
12558 end Check_Possible_Deferred_Completion;
12560 ---------------------------------
12561 -- Check_Recursive_Declaration --
12562 ---------------------------------
12564 procedure Check_Recursive_Declaration (Typ : Entity_Id) is
12565 Comp : Entity_Id;
12567 begin
12568 if Is_Record_Type (Typ) then
12569 Comp := First_Component (Typ);
12570 while Present (Comp) loop
12571 if Comes_From_Source (Comp) then
12572 if Present (Expression (Parent (Comp)))
12573 and then Is_Entity_Name (Expression (Parent (Comp)))
12574 and then Entity (Expression (Parent (Comp))) = Prev
12575 then
12576 Error_Msg_Sloc := Sloc (Parent (Comp));
12577 Error_Msg_NE
12578 ("illegal circularity with declaration for & #",
12579 N, Comp);
12580 return;
12582 elsif Is_Record_Type (Etype (Comp)) then
12583 Check_Recursive_Declaration (Etype (Comp));
12584 end if;
12585 end if;
12587 Next_Component (Comp);
12588 end loop;
12589 end if;
12590 end Check_Recursive_Declaration;
12592 -- Start of processing for Constant_Redeclaration
12594 begin
12595 if Nkind (Parent (Prev)) = N_Object_Declaration then
12596 if Nkind (Object_Definition
12597 (Parent (Prev))) = N_Subtype_Indication
12598 then
12599 -- Find type of new declaration. The constraints of the two
12600 -- views must match statically, but there is no point in
12601 -- creating an itype for the full view.
12603 if Nkind (Obj_Def) = N_Subtype_Indication then
12604 Find_Type (Subtype_Mark (Obj_Def));
12605 New_T := Entity (Subtype_Mark (Obj_Def));
12607 else
12608 Find_Type (Obj_Def);
12609 New_T := Entity (Obj_Def);
12610 end if;
12612 T := Etype (Prev);
12614 else
12615 -- The full view may impose a constraint, even if the partial
12616 -- view does not, so construct the subtype.
12618 New_T := Find_Type_Of_Object (Obj_Def, N);
12619 T := New_T;
12620 end if;
12622 else
12623 -- Current declaration is illegal, diagnosed below in Enter_Name
12625 T := Empty;
12626 New_T := Any_Type;
12627 end if;
12629 -- If previous full declaration or a renaming declaration exists, or if
12630 -- a homograph is present, let Enter_Name handle it, either with an
12631 -- error or with the removal of an overridden implicit subprogram.
12632 -- The previous one is a full declaration if it has an expression
12633 -- (which in the case of an aggregate is indicated by the Init flag).
12635 if Ekind (Prev) /= E_Constant
12636 or else Nkind (Parent (Prev)) = N_Object_Renaming_Declaration
12637 or else Present (Expression (Parent (Prev)))
12638 or else Has_Init_Expression (Parent (Prev))
12639 or else Present (Full_View (Prev))
12640 then
12641 Enter_Name (Id);
12643 -- Verify that types of both declarations match, or else that both types
12644 -- are anonymous access types whose designated subtypes statically match
12645 -- (as allowed in Ada 2005 by AI-385).
12647 elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
12648 and then
12649 (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
12650 or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
12651 or else Is_Access_Constant (Etype (New_T)) /=
12652 Is_Access_Constant (Etype (Prev))
12653 or else Can_Never_Be_Null (Etype (New_T)) /=
12654 Can_Never_Be_Null (Etype (Prev))
12655 or else Null_Exclusion_Present (Parent (Prev)) /=
12656 Null_Exclusion_Present (Parent (Id))
12657 or else not Subtypes_Statically_Match
12658 (Designated_Type (Etype (Prev)),
12659 Designated_Type (Etype (New_T))))
12660 then
12661 Error_Msg_Sloc := Sloc (Prev);
12662 Error_Msg_N ("type does not match declaration#", N);
12663 Set_Full_View (Prev, Id);
12664 Set_Etype (Id, Any_Type);
12666 -- A deferred constant whose type is an anonymous array is always
12667 -- illegal (unless imported). A detailed error message might be
12668 -- helpful for Ada beginners.
12670 if Nkind (Object_Definition (Parent (Prev)))
12671 = N_Constrained_Array_Definition
12672 and then Nkind (Object_Definition (N))
12673 = N_Constrained_Array_Definition
12674 then
12675 Error_Msg_N ("\each anonymous array is a distinct type", N);
12676 Error_Msg_N ("a deferred constant must have a named type",
12677 Object_Definition (Parent (Prev)));
12678 end if;
12680 elsif
12681 Null_Exclusion_Present (Parent (Prev))
12682 and then not Null_Exclusion_Present (N)
12683 then
12684 Error_Msg_Sloc := Sloc (Prev);
12685 Error_Msg_N ("null-exclusion does not match declaration#", N);
12686 Set_Full_View (Prev, Id);
12687 Set_Etype (Id, Any_Type);
12689 -- If so, process the full constant declaration
12691 else
12692 -- RM 7.4 (6): If the subtype defined by the subtype_indication in
12693 -- the deferred declaration is constrained, then the subtype defined
12694 -- by the subtype_indication in the full declaration shall match it
12695 -- statically.
12697 Check_Possible_Deferred_Completion
12698 (Prev_Id => Prev,
12699 Prev_Obj_Def => Object_Definition (Parent (Prev)),
12700 Curr_Obj_Def => Obj_Def);
12702 Set_Full_View (Prev, Id);
12703 Set_Is_Public (Id, Is_Public (Prev));
12704 Set_Is_Internal (Id);
12705 Append_Entity (Id, Current_Scope);
12707 -- Check ALIASED present if present before (RM 7.4(7))
12709 if Is_Aliased (Prev)
12710 and then not Aliased_Present (N)
12711 then
12712 Error_Msg_Sloc := Sloc (Prev);
12713 Error_Msg_N ("ALIASED required (see declaration #)", N);
12714 end if;
12716 -- Check that placement is in private part and that the incomplete
12717 -- declaration appeared in the visible part.
12719 if Ekind (Current_Scope) = E_Package
12720 and then not In_Private_Part (Current_Scope)
12721 then
12722 Error_Msg_Sloc := Sloc (Prev);
12723 Error_Msg_N
12724 ("full constant for declaration # must be in private part", N);
12726 elsif Ekind (Current_Scope) = E_Package
12727 and then
12728 List_Containing (Parent (Prev)) /=
12729 Visible_Declarations (Package_Specification (Current_Scope))
12730 then
12731 Error_Msg_N
12732 ("deferred constant must be declared in visible part",
12733 Parent (Prev));
12734 end if;
12736 if Is_Access_Type (T)
12737 and then Nkind (Expression (N)) = N_Allocator
12738 then
12739 Check_Recursive_Declaration (Designated_Type (T));
12740 end if;
12742 -- A deferred constant is a visible entity. If type has invariants,
12743 -- verify that the initial value satisfies them.
12745 if Has_Invariants (T) and then Present (Invariant_Procedure (T)) then
12746 Insert_After (N,
12747 Make_Invariant_Call (New_Occurrence_Of (Prev, Sloc (N))));
12748 end if;
12749 end if;
12750 end Constant_Redeclaration;
12752 ----------------------
12753 -- Constrain_Access --
12754 ----------------------
12756 procedure Constrain_Access
12757 (Def_Id : in out Entity_Id;
12758 S : Node_Id;
12759 Related_Nod : Node_Id)
12761 T : constant Entity_Id := Entity (Subtype_Mark (S));
12762 Desig_Type : constant Entity_Id := Designated_Type (T);
12763 Desig_Subtype : Entity_Id := Create_Itype (E_Void, Related_Nod);
12764 Constraint_OK : Boolean := True;
12766 begin
12767 if Is_Array_Type (Desig_Type) then
12768 Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
12770 elsif (Is_Record_Type (Desig_Type)
12771 or else Is_Incomplete_Or_Private_Type (Desig_Type))
12772 and then not Is_Constrained (Desig_Type)
12773 then
12774 -- ??? The following code is a temporary bypass to ignore a
12775 -- discriminant constraint on access type if it is constraining
12776 -- the current record. Avoid creating the implicit subtype of the
12777 -- record we are currently compiling since right now, we cannot
12778 -- handle these. For now, just return the access type itself.
12780 if Desig_Type = Current_Scope
12781 and then No (Def_Id)
12782 then
12783 Set_Ekind (Desig_Subtype, E_Record_Subtype);
12784 Def_Id := Entity (Subtype_Mark (S));
12786 -- This call added to ensure that the constraint is analyzed
12787 -- (needed for a B test). Note that we still return early from
12788 -- this procedure to avoid recursive processing. ???
12790 Constrain_Discriminated_Type
12791 (Desig_Subtype, S, Related_Nod, For_Access => True);
12792 return;
12793 end if;
12795 -- Enforce rule that the constraint is illegal if there is an
12796 -- unconstrained view of the designated type. This means that the
12797 -- partial view (either a private type declaration or a derivation
12798 -- from a private type) has no discriminants. (Defect Report
12799 -- 8652/0008, Technical Corrigendum 1, checked by ACATS B371001).
12801 -- Rule updated for Ada 2005: The private type is said to have
12802 -- a constrained partial view, given that objects of the type
12803 -- can be declared. Furthermore, the rule applies to all access
12804 -- types, unlike the rule concerning default discriminants (see
12805 -- RM 3.7.1(7/3))
12807 if (Ekind (T) = E_General_Access_Type or else Ada_Version >= Ada_2005)
12808 and then Has_Private_Declaration (Desig_Type)
12809 and then In_Open_Scopes (Scope (Desig_Type))
12810 and then Has_Discriminants (Desig_Type)
12811 then
12812 declare
12813 Pack : constant Node_Id :=
12814 Unit_Declaration_Node (Scope (Desig_Type));
12815 Decls : List_Id;
12816 Decl : Node_Id;
12818 begin
12819 if Nkind (Pack) = N_Package_Declaration then
12820 Decls := Visible_Declarations (Specification (Pack));
12821 Decl := First (Decls);
12822 while Present (Decl) loop
12823 if (Nkind (Decl) = N_Private_Type_Declaration
12824 and then Chars (Defining_Identifier (Decl)) =
12825 Chars (Desig_Type))
12827 or else
12828 (Nkind (Decl) = N_Full_Type_Declaration
12829 and then
12830 Chars (Defining_Identifier (Decl)) =
12831 Chars (Desig_Type)
12832 and then Is_Derived_Type (Desig_Type)
12833 and then
12834 Has_Private_Declaration (Etype (Desig_Type)))
12835 then
12836 if No (Discriminant_Specifications (Decl)) then
12837 Error_Msg_N
12838 ("cannot constrain access type if designated "
12839 & "type has constrained partial view", S);
12840 end if;
12842 exit;
12843 end if;
12845 Next (Decl);
12846 end loop;
12847 end if;
12848 end;
12849 end if;
12851 Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
12852 For_Access => True);
12854 elsif Is_Concurrent_Type (Desig_Type)
12855 and then not Is_Constrained (Desig_Type)
12856 then
12857 Constrain_Concurrent (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
12859 else
12860 Error_Msg_N ("invalid constraint on access type", S);
12862 -- We simply ignore an invalid constraint
12864 Desig_Subtype := Desig_Type;
12865 Constraint_OK := False;
12866 end if;
12868 if No (Def_Id) then
12869 Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
12870 else
12871 Set_Ekind (Def_Id, E_Access_Subtype);
12872 end if;
12874 if Constraint_OK then
12875 Set_Etype (Def_Id, Base_Type (T));
12877 if Is_Private_Type (Desig_Type) then
12878 Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
12879 end if;
12880 else
12881 Set_Etype (Def_Id, Any_Type);
12882 end if;
12884 Set_Size_Info (Def_Id, T);
12885 Set_Is_Constrained (Def_Id, Constraint_OK);
12886 Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
12887 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
12888 Set_Is_Access_Constant (Def_Id, Is_Access_Constant (T));
12890 Conditional_Delay (Def_Id, T);
12892 -- AI-363 : Subtypes of general access types whose designated types have
12893 -- default discriminants are disallowed. In instances, the rule has to
12894 -- be checked against the actual, of which T is the subtype. In a
12895 -- generic body, the rule is checked assuming that the actual type has
12896 -- defaulted discriminants.
12898 if Ada_Version >= Ada_2005 or else Warn_On_Ada_2005_Compatibility then
12899 if Ekind (Base_Type (T)) = E_General_Access_Type
12900 and then Has_Defaulted_Discriminants (Desig_Type)
12901 then
12902 if Ada_Version < Ada_2005 then
12903 Error_Msg_N
12904 ("access subtype of general access type would not " &
12905 "be allowed in Ada 2005?y?", S);
12906 else
12907 Error_Msg_N
12908 ("access subtype of general access type not allowed", S);
12909 end if;
12911 Error_Msg_N ("\discriminants have defaults", S);
12913 elsif Is_Access_Type (T)
12914 and then Is_Generic_Type (Desig_Type)
12915 and then Has_Discriminants (Desig_Type)
12916 and then In_Package_Body (Current_Scope)
12917 then
12918 if Ada_Version < Ada_2005 then
12919 Error_Msg_N
12920 ("access subtype would not be allowed in generic body "
12921 & "in Ada 2005?y?", S);
12922 else
12923 Error_Msg_N
12924 ("access subtype not allowed in generic body", S);
12925 end if;
12927 Error_Msg_N
12928 ("\designated type is a discriminated formal", S);
12929 end if;
12930 end if;
12931 end Constrain_Access;
12933 ---------------------
12934 -- Constrain_Array --
12935 ---------------------
12937 procedure Constrain_Array
12938 (Def_Id : in out Entity_Id;
12939 SI : Node_Id;
12940 Related_Nod : Node_Id;
12941 Related_Id : Entity_Id;
12942 Suffix : Character)
12944 C : constant Node_Id := Constraint (SI);
12945 Number_Of_Constraints : Nat := 0;
12946 Index : Node_Id;
12947 S, T : Entity_Id;
12948 Constraint_OK : Boolean := True;
12950 begin
12951 T := Entity (Subtype_Mark (SI));
12953 if Is_Access_Type (T) then
12954 T := Designated_Type (T);
12955 end if;
12957 -- If an index constraint follows a subtype mark in a subtype indication
12958 -- then the type or subtype denoted by the subtype mark must not already
12959 -- impose an index constraint. The subtype mark must denote either an
12960 -- unconstrained array type or an access type whose designated type
12961 -- is such an array type... (RM 3.6.1)
12963 if Is_Constrained (T) then
12964 Error_Msg_N ("array type is already constrained", Subtype_Mark (SI));
12965 Constraint_OK := False;
12967 else
12968 S := First (Constraints (C));
12969 while Present (S) loop
12970 Number_Of_Constraints := Number_Of_Constraints + 1;
12971 Next (S);
12972 end loop;
12974 -- In either case, the index constraint must provide a discrete
12975 -- range for each index of the array type and the type of each
12976 -- discrete range must be the same as that of the corresponding
12977 -- index. (RM 3.6.1)
12979 if Number_Of_Constraints /= Number_Dimensions (T) then
12980 Error_Msg_NE ("incorrect number of index constraints for }", C, T);
12981 Constraint_OK := False;
12983 else
12984 S := First (Constraints (C));
12985 Index := First_Index (T);
12986 Analyze (Index);
12988 -- Apply constraints to each index type
12990 for J in 1 .. Number_Of_Constraints loop
12991 Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
12992 Next (Index);
12993 Next (S);
12994 end loop;
12996 end if;
12997 end if;
12999 if No (Def_Id) then
13000 Def_Id :=
13001 Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
13002 Set_Parent (Def_Id, Related_Nod);
13004 else
13005 Set_Ekind (Def_Id, E_Array_Subtype);
13006 end if;
13008 Set_Size_Info (Def_Id, (T));
13009 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13010 Set_Etype (Def_Id, Base_Type (T));
13012 if Constraint_OK then
13013 Set_First_Index (Def_Id, First (Constraints (C)));
13014 else
13015 Set_First_Index (Def_Id, First_Index (T));
13016 end if;
13018 Set_Is_Constrained (Def_Id, True);
13019 Set_Is_Aliased (Def_Id, Is_Aliased (T));
13020 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
13022 Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
13023 Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
13025 -- A subtype does not inherit the Packed_Array_Impl_Type of is parent.
13026 -- We need to initialize the attribute because if Def_Id is previously
13027 -- analyzed through a limited_with clause, it will have the attributes
13028 -- of an incomplete type, one of which is an Elist that overlaps the
13029 -- Packed_Array_Impl_Type field.
13031 Set_Packed_Array_Impl_Type (Def_Id, Empty);
13033 -- Build a freeze node if parent still needs one. Also make sure that
13034 -- the Depends_On_Private status is set because the subtype will need
13035 -- reprocessing at the time the base type does, and also we must set a
13036 -- conditional delay.
13038 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
13039 Conditional_Delay (Def_Id, T);
13040 end Constrain_Array;
13042 ------------------------------
13043 -- Constrain_Component_Type --
13044 ------------------------------
13046 function Constrain_Component_Type
13047 (Comp : Entity_Id;
13048 Constrained_Typ : Entity_Id;
13049 Related_Node : Node_Id;
13050 Typ : Entity_Id;
13051 Constraints : Elist_Id) return Entity_Id
13053 Loc : constant Source_Ptr := Sloc (Constrained_Typ);
13054 Compon_Type : constant Entity_Id := Etype (Comp);
13056 function Build_Constrained_Array_Type
13057 (Old_Type : Entity_Id) return Entity_Id;
13058 -- If Old_Type is an array type, one of whose indexes is constrained
13059 -- by a discriminant, build an Itype whose constraint replaces the
13060 -- discriminant with its value in the constraint.
13062 function Build_Constrained_Discriminated_Type
13063 (Old_Type : Entity_Id) return Entity_Id;
13064 -- Ditto for record components
13066 function Build_Constrained_Access_Type
13067 (Old_Type : Entity_Id) return Entity_Id;
13068 -- Ditto for access types. Makes use of previous two functions, to
13069 -- constrain designated type.
13071 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id;
13072 -- T is an array or discriminated type, C is a list of constraints
13073 -- that apply to T. This routine builds the constrained subtype.
13075 function Is_Discriminant (Expr : Node_Id) return Boolean;
13076 -- Returns True if Expr is a discriminant
13078 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id;
13079 -- Find the value of discriminant Discrim in Constraint
13081 -----------------------------------
13082 -- Build_Constrained_Access_Type --
13083 -----------------------------------
13085 function Build_Constrained_Access_Type
13086 (Old_Type : Entity_Id) return Entity_Id
13088 Desig_Type : constant Entity_Id := Designated_Type (Old_Type);
13089 Itype : Entity_Id;
13090 Desig_Subtype : Entity_Id;
13091 Scop : Entity_Id;
13093 begin
13094 -- if the original access type was not embedded in the enclosing
13095 -- type definition, there is no need to produce a new access
13096 -- subtype. In fact every access type with an explicit constraint
13097 -- generates an itype whose scope is the enclosing record.
13099 if not Is_Type (Scope (Old_Type)) then
13100 return Old_Type;
13102 elsif Is_Array_Type (Desig_Type) then
13103 Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
13105 elsif Has_Discriminants (Desig_Type) then
13107 -- This may be an access type to an enclosing record type for
13108 -- which we are constructing the constrained components. Return
13109 -- the enclosing record subtype. This is not always correct,
13110 -- but avoids infinite recursion. ???
13112 Desig_Subtype := Any_Type;
13114 for J in reverse 0 .. Scope_Stack.Last loop
13115 Scop := Scope_Stack.Table (J).Entity;
13117 if Is_Type (Scop)
13118 and then Base_Type (Scop) = Base_Type (Desig_Type)
13119 then
13120 Desig_Subtype := Scop;
13121 end if;
13123 exit when not Is_Type (Scop);
13124 end loop;
13126 if Desig_Subtype = Any_Type then
13127 Desig_Subtype :=
13128 Build_Constrained_Discriminated_Type (Desig_Type);
13129 end if;
13131 else
13132 return Old_Type;
13133 end if;
13135 if Desig_Subtype /= Desig_Type then
13137 -- The Related_Node better be here or else we won't be able
13138 -- to attach new itypes to a node in the tree.
13140 pragma Assert (Present (Related_Node));
13142 Itype := Create_Itype (E_Access_Subtype, Related_Node);
13144 Set_Etype (Itype, Base_Type (Old_Type));
13145 Set_Size_Info (Itype, (Old_Type));
13146 Set_Directly_Designated_Type (Itype, Desig_Subtype);
13147 Set_Depends_On_Private (Itype, Has_Private_Component
13148 (Old_Type));
13149 Set_Is_Access_Constant (Itype, Is_Access_Constant
13150 (Old_Type));
13152 -- The new itype needs freezing when it depends on a not frozen
13153 -- type and the enclosing subtype needs freezing.
13155 if Has_Delayed_Freeze (Constrained_Typ)
13156 and then not Is_Frozen (Constrained_Typ)
13157 then
13158 Conditional_Delay (Itype, Base_Type (Old_Type));
13159 end if;
13161 return Itype;
13163 else
13164 return Old_Type;
13165 end if;
13166 end Build_Constrained_Access_Type;
13168 ----------------------------------
13169 -- Build_Constrained_Array_Type --
13170 ----------------------------------
13172 function Build_Constrained_Array_Type
13173 (Old_Type : Entity_Id) return Entity_Id
13175 Lo_Expr : Node_Id;
13176 Hi_Expr : Node_Id;
13177 Old_Index : Node_Id;
13178 Range_Node : Node_Id;
13179 Constr_List : List_Id;
13181 Need_To_Create_Itype : Boolean := False;
13183 begin
13184 Old_Index := First_Index (Old_Type);
13185 while Present (Old_Index) loop
13186 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
13188 if Is_Discriminant (Lo_Expr)
13189 or else
13190 Is_Discriminant (Hi_Expr)
13191 then
13192 Need_To_Create_Itype := True;
13193 end if;
13195 Next_Index (Old_Index);
13196 end loop;
13198 if Need_To_Create_Itype then
13199 Constr_List := New_List;
13201 Old_Index := First_Index (Old_Type);
13202 while Present (Old_Index) loop
13203 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
13205 if Is_Discriminant (Lo_Expr) then
13206 Lo_Expr := Get_Discr_Value (Lo_Expr);
13207 end if;
13209 if Is_Discriminant (Hi_Expr) then
13210 Hi_Expr := Get_Discr_Value (Hi_Expr);
13211 end if;
13213 Range_Node :=
13214 Make_Range
13215 (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
13217 Append (Range_Node, To => Constr_List);
13219 Next_Index (Old_Index);
13220 end loop;
13222 return Build_Subtype (Old_Type, Constr_List);
13224 else
13225 return Old_Type;
13226 end if;
13227 end Build_Constrained_Array_Type;
13229 ------------------------------------------
13230 -- Build_Constrained_Discriminated_Type --
13231 ------------------------------------------
13233 function Build_Constrained_Discriminated_Type
13234 (Old_Type : Entity_Id) return Entity_Id
13236 Expr : Node_Id;
13237 Constr_List : List_Id;
13238 Old_Constraint : Elmt_Id;
13240 Need_To_Create_Itype : Boolean := False;
13242 begin
13243 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
13244 while Present (Old_Constraint) loop
13245 Expr := Node (Old_Constraint);
13247 if Is_Discriminant (Expr) then
13248 Need_To_Create_Itype := True;
13249 end if;
13251 Next_Elmt (Old_Constraint);
13252 end loop;
13254 if Need_To_Create_Itype then
13255 Constr_List := New_List;
13257 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
13258 while Present (Old_Constraint) loop
13259 Expr := Node (Old_Constraint);
13261 if Is_Discriminant (Expr) then
13262 Expr := Get_Discr_Value (Expr);
13263 end if;
13265 Append (New_Copy_Tree (Expr), To => Constr_List);
13267 Next_Elmt (Old_Constraint);
13268 end loop;
13270 return Build_Subtype (Old_Type, Constr_List);
13272 else
13273 return Old_Type;
13274 end if;
13275 end Build_Constrained_Discriminated_Type;
13277 -------------------
13278 -- Build_Subtype --
13279 -------------------
13281 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id is
13282 Indic : Node_Id;
13283 Subtyp_Decl : Node_Id;
13284 Def_Id : Entity_Id;
13285 Btyp : Entity_Id := Base_Type (T);
13287 begin
13288 -- The Related_Node better be here or else we won't be able to
13289 -- attach new itypes to a node in the tree.
13291 pragma Assert (Present (Related_Node));
13293 -- If the view of the component's type is incomplete or private
13294 -- with unknown discriminants, then the constraint must be applied
13295 -- to the full type.
13297 if Has_Unknown_Discriminants (Btyp)
13298 and then Present (Underlying_Type (Btyp))
13299 then
13300 Btyp := Underlying_Type (Btyp);
13301 end if;
13303 Indic :=
13304 Make_Subtype_Indication (Loc,
13305 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
13306 Constraint => Make_Index_Or_Discriminant_Constraint (Loc, C));
13308 Def_Id := Create_Itype (Ekind (T), Related_Node);
13310 Subtyp_Decl :=
13311 Make_Subtype_Declaration (Loc,
13312 Defining_Identifier => Def_Id,
13313 Subtype_Indication => Indic);
13315 Set_Parent (Subtyp_Decl, Parent (Related_Node));
13317 -- Itypes must be analyzed with checks off (see package Itypes)
13319 Analyze (Subtyp_Decl, Suppress => All_Checks);
13321 return Def_Id;
13322 end Build_Subtype;
13324 ---------------------
13325 -- Get_Discr_Value --
13326 ---------------------
13328 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id is
13329 D : Entity_Id;
13330 E : Elmt_Id;
13332 begin
13333 -- The discriminant may be declared for the type, in which case we
13334 -- find it by iterating over the list of discriminants. If the
13335 -- discriminant is inherited from a parent type, it appears as the
13336 -- corresponding discriminant of the current type. This will be the
13337 -- case when constraining an inherited component whose constraint is
13338 -- given by a discriminant of the parent.
13340 D := First_Discriminant (Typ);
13341 E := First_Elmt (Constraints);
13343 while Present (D) loop
13344 if D = Entity (Discrim)
13345 or else D = CR_Discriminant (Entity (Discrim))
13346 or else Corresponding_Discriminant (D) = Entity (Discrim)
13347 then
13348 return Node (E);
13349 end if;
13351 Next_Discriminant (D);
13352 Next_Elmt (E);
13353 end loop;
13355 -- The Corresponding_Discriminant mechanism is incomplete, because
13356 -- the correspondence between new and old discriminants is not one
13357 -- to one: one new discriminant can constrain several old ones. In
13358 -- that case, scan sequentially the stored_constraint, the list of
13359 -- discriminants of the parents, and the constraints.
13361 -- Previous code checked for the present of the Stored_Constraint
13362 -- list for the derived type, but did not use it at all. Should it
13363 -- be present when the component is a discriminated task type?
13365 if Is_Derived_Type (Typ)
13366 and then Scope (Entity (Discrim)) = Etype (Typ)
13367 then
13368 D := First_Discriminant (Etype (Typ));
13369 E := First_Elmt (Constraints);
13370 while Present (D) loop
13371 if D = Entity (Discrim) then
13372 return Node (E);
13373 end if;
13375 Next_Discriminant (D);
13376 Next_Elmt (E);
13377 end loop;
13378 end if;
13380 -- Something is wrong if we did not find the value
13382 raise Program_Error;
13383 end Get_Discr_Value;
13385 ---------------------
13386 -- Is_Discriminant --
13387 ---------------------
13389 function Is_Discriminant (Expr : Node_Id) return Boolean is
13390 Discrim_Scope : Entity_Id;
13392 begin
13393 if Denotes_Discriminant (Expr) then
13394 Discrim_Scope := Scope (Entity (Expr));
13396 -- Either we have a reference to one of Typ's discriminants,
13398 pragma Assert (Discrim_Scope = Typ
13400 -- or to the discriminants of the parent type, in the case
13401 -- of a derivation of a tagged type with variants.
13403 or else Discrim_Scope = Etype (Typ)
13404 or else Full_View (Discrim_Scope) = Etype (Typ)
13406 -- or same as above for the case where the discriminants
13407 -- were declared in Typ's private view.
13409 or else (Is_Private_Type (Discrim_Scope)
13410 and then Chars (Discrim_Scope) = Chars (Typ))
13412 -- or else we are deriving from the full view and the
13413 -- discriminant is declared in the private entity.
13415 or else (Is_Private_Type (Typ)
13416 and then Chars (Discrim_Scope) = Chars (Typ))
13418 -- Or we are constrained the corresponding record of a
13419 -- synchronized type that completes a private declaration.
13421 or else (Is_Concurrent_Record_Type (Typ)
13422 and then
13423 Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
13425 -- or we have a class-wide type, in which case make sure the
13426 -- discriminant found belongs to the root type.
13428 or else (Is_Class_Wide_Type (Typ)
13429 and then Etype (Typ) = Discrim_Scope));
13431 return True;
13432 end if;
13434 -- In all other cases we have something wrong
13436 return False;
13437 end Is_Discriminant;
13439 -- Start of processing for Constrain_Component_Type
13441 begin
13442 if Nkind (Parent (Comp)) = N_Component_Declaration
13443 and then Comes_From_Source (Parent (Comp))
13444 and then Comes_From_Source
13445 (Subtype_Indication (Component_Definition (Parent (Comp))))
13446 and then
13447 Is_Entity_Name
13448 (Subtype_Indication (Component_Definition (Parent (Comp))))
13449 then
13450 return Compon_Type;
13452 elsif Is_Array_Type (Compon_Type) then
13453 return Build_Constrained_Array_Type (Compon_Type);
13455 elsif Has_Discriminants (Compon_Type) then
13456 return Build_Constrained_Discriminated_Type (Compon_Type);
13458 elsif Is_Access_Type (Compon_Type) then
13459 return Build_Constrained_Access_Type (Compon_Type);
13461 else
13462 return Compon_Type;
13463 end if;
13464 end Constrain_Component_Type;
13466 --------------------------
13467 -- Constrain_Concurrent --
13468 --------------------------
13470 -- For concurrent types, the associated record value type carries the same
13471 -- discriminants, so when we constrain a concurrent type, we must constrain
13472 -- the corresponding record type as well.
13474 procedure Constrain_Concurrent
13475 (Def_Id : in out Entity_Id;
13476 SI : Node_Id;
13477 Related_Nod : Node_Id;
13478 Related_Id : Entity_Id;
13479 Suffix : Character)
13481 -- Retrieve Base_Type to ensure getting to the concurrent type in the
13482 -- case of a private subtype (needed when only doing semantic analysis).
13484 T_Ent : Entity_Id := Base_Type (Entity (Subtype_Mark (SI)));
13485 T_Val : Entity_Id;
13487 begin
13488 if Is_Access_Type (T_Ent) then
13489 T_Ent := Designated_Type (T_Ent);
13490 end if;
13492 T_Val := Corresponding_Record_Type (T_Ent);
13494 if Present (T_Val) then
13496 if No (Def_Id) then
13497 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
13499 -- Elaborate itype now, as it may be used in a subsequent
13500 -- synchronized operation in another scope.
13502 if Nkind (Related_Nod) = N_Full_Type_Declaration then
13503 Build_Itype_Reference (Def_Id, Related_Nod);
13504 end if;
13505 end if;
13507 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
13509 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
13510 Set_Corresponding_Record_Type (Def_Id,
13511 Constrain_Corresponding_Record (Def_Id, T_Val, Related_Nod));
13513 else
13514 -- If there is no associated record, expansion is disabled and this
13515 -- is a generic context. Create a subtype in any case, so that
13516 -- semantic analysis can proceed.
13518 if No (Def_Id) then
13519 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
13520 end if;
13522 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
13523 end if;
13524 end Constrain_Concurrent;
13526 ------------------------------------
13527 -- Constrain_Corresponding_Record --
13528 ------------------------------------
13530 function Constrain_Corresponding_Record
13531 (Prot_Subt : Entity_Id;
13532 Corr_Rec : Entity_Id;
13533 Related_Nod : Node_Id) return Entity_Id
13535 T_Sub : constant Entity_Id :=
13536 Create_Itype (E_Record_Subtype, Related_Nod, Corr_Rec, 'C');
13538 begin
13539 Set_Etype (T_Sub, Corr_Rec);
13540 Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
13541 Set_Is_Constrained (T_Sub, True);
13542 Set_First_Entity (T_Sub, First_Entity (Corr_Rec));
13543 Set_Last_Entity (T_Sub, Last_Entity (Corr_Rec));
13545 if Has_Discriminants (Prot_Subt) then -- False only if errors.
13546 Set_Discriminant_Constraint
13547 (T_Sub, Discriminant_Constraint (Prot_Subt));
13548 Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
13549 Create_Constrained_Components
13550 (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
13551 end if;
13553 Set_Depends_On_Private (T_Sub, Has_Private_Component (T_Sub));
13555 if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
13556 Conditional_Delay (T_Sub, Corr_Rec);
13558 else
13559 -- This is a component subtype: it will be frozen in the context of
13560 -- the enclosing record's init_proc, so that discriminant references
13561 -- are resolved to discriminals. (Note: we used to skip freezing
13562 -- altogether in that case, which caused errors downstream for
13563 -- components of a bit packed array type).
13565 Set_Has_Delayed_Freeze (T_Sub);
13566 end if;
13568 return T_Sub;
13569 end Constrain_Corresponding_Record;
13571 -----------------------
13572 -- Constrain_Decimal --
13573 -----------------------
13575 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id) is
13576 T : constant Entity_Id := Entity (Subtype_Mark (S));
13577 C : constant Node_Id := Constraint (S);
13578 Loc : constant Source_Ptr := Sloc (C);
13579 Range_Expr : Node_Id;
13580 Digits_Expr : Node_Id;
13581 Digits_Val : Uint;
13582 Bound_Val : Ureal;
13584 begin
13585 Set_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
13587 if Nkind (C) = N_Range_Constraint then
13588 Range_Expr := Range_Expression (C);
13589 Digits_Val := Digits_Value (T);
13591 else
13592 pragma Assert (Nkind (C) = N_Digits_Constraint);
13594 Check_SPARK_05_Restriction ("digits constraint is not allowed", S);
13596 Digits_Expr := Digits_Expression (C);
13597 Analyze_And_Resolve (Digits_Expr, Any_Integer);
13599 Check_Digits_Expression (Digits_Expr);
13600 Digits_Val := Expr_Value (Digits_Expr);
13602 if Digits_Val > Digits_Value (T) then
13603 Error_Msg_N
13604 ("digits expression is incompatible with subtype", C);
13605 Digits_Val := Digits_Value (T);
13606 end if;
13608 if Present (Range_Constraint (C)) then
13609 Range_Expr := Range_Expression (Range_Constraint (C));
13610 else
13611 Range_Expr := Empty;
13612 end if;
13613 end if;
13615 Set_Etype (Def_Id, Base_Type (T));
13616 Set_Size_Info (Def_Id, (T));
13617 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13618 Set_Delta_Value (Def_Id, Delta_Value (T));
13619 Set_Scale_Value (Def_Id, Scale_Value (T));
13620 Set_Small_Value (Def_Id, Small_Value (T));
13621 Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
13622 Set_Digits_Value (Def_Id, Digits_Val);
13624 -- Manufacture range from given digits value if no range present
13626 if No (Range_Expr) then
13627 Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
13628 Range_Expr :=
13629 Make_Range (Loc,
13630 Low_Bound =>
13631 Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
13632 High_Bound =>
13633 Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
13634 end if;
13636 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
13637 Set_Discrete_RM_Size (Def_Id);
13639 -- Unconditionally delay the freeze, since we cannot set size
13640 -- information in all cases correctly until the freeze point.
13642 Set_Has_Delayed_Freeze (Def_Id);
13643 end Constrain_Decimal;
13645 ----------------------------------
13646 -- Constrain_Discriminated_Type --
13647 ----------------------------------
13649 procedure Constrain_Discriminated_Type
13650 (Def_Id : Entity_Id;
13651 S : Node_Id;
13652 Related_Nod : Node_Id;
13653 For_Access : Boolean := False)
13655 E : Entity_Id := Entity (Subtype_Mark (S));
13656 T : Entity_Id;
13658 procedure Fixup_Bad_Constraint;
13659 -- Called after finding a bad constraint, and after having posted an
13660 -- appropriate error message. The goal is to leave type Def_Id in as
13661 -- reasonable state as possible.
13663 --------------------------
13664 -- Fixup_Bad_Constraint --
13665 --------------------------
13667 procedure Fixup_Bad_Constraint is
13668 begin
13669 -- Set a reasonable Ekind for the entity, including incomplete types.
13671 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
13673 -- Set Etype to the known type, to reduce chances of cascaded errors
13675 Set_Etype (Def_Id, E);
13676 Set_Error_Posted (Def_Id);
13677 end Fixup_Bad_Constraint;
13679 -- Local variables
13681 C : Node_Id;
13682 Constr : Elist_Id := New_Elmt_List;
13684 -- Start of processing for Constrain_Discriminated_Type
13686 begin
13687 C := Constraint (S);
13689 -- A discriminant constraint is only allowed in a subtype indication,
13690 -- after a subtype mark. This subtype mark must denote either a type
13691 -- with discriminants, or an access type whose designated type is a
13692 -- type with discriminants. A discriminant constraint specifies the
13693 -- values of these discriminants (RM 3.7.2(5)).
13695 T := Base_Type (Entity (Subtype_Mark (S)));
13697 if Is_Access_Type (T) then
13698 T := Designated_Type (T);
13699 end if;
13701 -- In an instance it may be necessary to retrieve the full view of a
13702 -- type with unknown discriminants, or a full view with defaulted
13703 -- discriminants. In other contexts the constraint is illegal.
13705 if In_Instance
13706 and then Is_Private_Type (T)
13707 and then Present (Full_View (T))
13708 and then
13709 (Has_Unknown_Discriminants (T)
13710 or else
13711 (not Has_Discriminants (T)
13712 and then Has_Discriminants (Full_View (T))
13713 and then Present (Discriminant_Default_Value
13714 (First_Discriminant (Full_View (T))))))
13715 then
13716 T := Full_View (T);
13717 E := Full_View (E);
13718 end if;
13720 -- Ada 2005 (AI-412): Constrained incomplete subtypes are illegal. Avoid
13721 -- generating an error for access-to-incomplete subtypes.
13723 if Ada_Version >= Ada_2005
13724 and then Ekind (T) = E_Incomplete_Type
13725 and then Nkind (Parent (S)) = N_Subtype_Declaration
13726 and then not Is_Itype (Def_Id)
13727 then
13728 -- A little sanity check: emit an error message if the type has
13729 -- discriminants to begin with. Type T may be a regular incomplete
13730 -- type or imported via a limited with clause.
13732 if Has_Discriminants (T)
13733 or else (From_Limited_With (T)
13734 and then Present (Non_Limited_View (T))
13735 and then Nkind (Parent (Non_Limited_View (T))) =
13736 N_Full_Type_Declaration
13737 and then Present (Discriminant_Specifications
13738 (Parent (Non_Limited_View (T)))))
13739 then
13740 Error_Msg_N
13741 ("(Ada 2005) incomplete subtype may not be constrained", C);
13742 else
13743 Error_Msg_N ("invalid constraint: type has no discriminant", C);
13744 end if;
13746 Fixup_Bad_Constraint;
13747 return;
13749 -- Check that the type has visible discriminants. The type may be
13750 -- a private type with unknown discriminants whose full view has
13751 -- discriminants which are invisible.
13753 elsif not Has_Discriminants (T)
13754 or else
13755 (Has_Unknown_Discriminants (T)
13756 and then Is_Private_Type (T))
13757 then
13758 Error_Msg_N ("invalid constraint: type has no discriminant", C);
13759 Fixup_Bad_Constraint;
13760 return;
13762 elsif Is_Constrained (E)
13763 or else (Ekind (E) = E_Class_Wide_Subtype
13764 and then Present (Discriminant_Constraint (E)))
13765 then
13766 Error_Msg_N ("type is already constrained", Subtype_Mark (S));
13767 Fixup_Bad_Constraint;
13768 return;
13769 end if;
13771 -- T may be an unconstrained subtype (e.g. a generic actual). Constraint
13772 -- applies to the base type.
13774 T := Base_Type (T);
13776 Constr := Build_Discriminant_Constraints (T, S);
13778 -- If the list returned was empty we had an error in building the
13779 -- discriminant constraint. We have also already signalled an error
13780 -- in the incomplete type case
13782 if Is_Empty_Elmt_List (Constr) then
13783 Fixup_Bad_Constraint;
13784 return;
13785 end if;
13787 Build_Discriminated_Subtype (T, Def_Id, Constr, Related_Nod, For_Access);
13788 end Constrain_Discriminated_Type;
13790 ---------------------------
13791 -- Constrain_Enumeration --
13792 ---------------------------
13794 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id) is
13795 T : constant Entity_Id := Entity (Subtype_Mark (S));
13796 C : constant Node_Id := Constraint (S);
13798 begin
13799 Set_Ekind (Def_Id, E_Enumeration_Subtype);
13801 Set_First_Literal (Def_Id, First_Literal (Base_Type (T)));
13803 Set_Etype (Def_Id, Base_Type (T));
13804 Set_Size_Info (Def_Id, (T));
13805 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13806 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
13808 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13810 Set_Discrete_RM_Size (Def_Id);
13811 end Constrain_Enumeration;
13813 ----------------------
13814 -- Constrain_Float --
13815 ----------------------
13817 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id) is
13818 T : constant Entity_Id := Entity (Subtype_Mark (S));
13819 C : Node_Id;
13820 D : Node_Id;
13821 Rais : Node_Id;
13823 begin
13824 Set_Ekind (Def_Id, E_Floating_Point_Subtype);
13826 Set_Etype (Def_Id, Base_Type (T));
13827 Set_Size_Info (Def_Id, (T));
13828 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13830 -- Process the constraint
13832 C := Constraint (S);
13834 -- Digits constraint present
13836 if Nkind (C) = N_Digits_Constraint then
13838 Check_SPARK_05_Restriction ("digits constraint is not allowed", S);
13839 Check_Restriction (No_Obsolescent_Features, C);
13841 if Warn_On_Obsolescent_Feature then
13842 Error_Msg_N
13843 ("subtype digits constraint is an " &
13844 "obsolescent feature (RM J.3(8))?j?", C);
13845 end if;
13847 D := Digits_Expression (C);
13848 Analyze_And_Resolve (D, Any_Integer);
13849 Check_Digits_Expression (D);
13850 Set_Digits_Value (Def_Id, Expr_Value (D));
13852 -- Check that digits value is in range. Obviously we can do this
13853 -- at compile time, but it is strictly a runtime check, and of
13854 -- course there is an ACVC test that checks this.
13856 if Digits_Value (Def_Id) > Digits_Value (T) then
13857 Error_Msg_Uint_1 := Digits_Value (T);
13858 Error_Msg_N ("??digits value is too large, maximum is ^", D);
13859 Rais :=
13860 Make_Raise_Constraint_Error (Sloc (D),
13861 Reason => CE_Range_Check_Failed);
13862 Insert_Action (Declaration_Node (Def_Id), Rais);
13863 end if;
13865 C := Range_Constraint (C);
13867 -- No digits constraint present
13869 else
13870 Set_Digits_Value (Def_Id, Digits_Value (T));
13871 end if;
13873 -- Range constraint present
13875 if Nkind (C) = N_Range_Constraint then
13876 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13878 -- No range constraint present
13880 else
13881 pragma Assert (No (C));
13882 Set_Scalar_Range (Def_Id, Scalar_Range (T));
13883 end if;
13885 Set_Is_Constrained (Def_Id);
13886 end Constrain_Float;
13888 ---------------------
13889 -- Constrain_Index --
13890 ---------------------
13892 procedure Constrain_Index
13893 (Index : Node_Id;
13894 S : Node_Id;
13895 Related_Nod : Node_Id;
13896 Related_Id : Entity_Id;
13897 Suffix : Character;
13898 Suffix_Index : Nat)
13900 Def_Id : Entity_Id;
13901 R : Node_Id := Empty;
13902 T : constant Entity_Id := Etype (Index);
13904 begin
13905 Def_Id :=
13906 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
13907 Set_Etype (Def_Id, Base_Type (T));
13909 if Nkind (S) = N_Range
13910 or else
13911 (Nkind (S) = N_Attribute_Reference
13912 and then Attribute_Name (S) = Name_Range)
13913 then
13914 -- A Range attribute will be transformed into N_Range by Resolve
13916 Analyze (S);
13917 Set_Etype (S, T);
13918 R := S;
13920 Process_Range_Expr_In_Decl (R, T);
13922 if not Error_Posted (S)
13923 and then
13924 (Nkind (S) /= N_Range
13925 or else not Covers (T, (Etype (Low_Bound (S))))
13926 or else not Covers (T, (Etype (High_Bound (S)))))
13927 then
13928 if Base_Type (T) /= Any_Type
13929 and then Etype (Low_Bound (S)) /= Any_Type
13930 and then Etype (High_Bound (S)) /= Any_Type
13931 then
13932 Error_Msg_N ("range expected", S);
13933 end if;
13934 end if;
13936 elsif Nkind (S) = N_Subtype_Indication then
13938 -- The parser has verified that this is a discrete indication
13940 Resolve_Discrete_Subtype_Indication (S, T);
13941 Bad_Predicated_Subtype_Use
13942 ("subtype& has predicate, not allowed in index constraint",
13943 S, Entity (Subtype_Mark (S)));
13945 R := Range_Expression (Constraint (S));
13947 -- Capture values of bounds and generate temporaries for them if
13948 -- needed, since checks may cause duplication of the expressions
13949 -- which must not be reevaluated.
13951 -- The forced evaluation removes side effects from expressions, which
13952 -- should occur also in GNATprove mode. Otherwise, we end up with
13953 -- unexpected insertions of actions at places where this is not
13954 -- supposed to occur, e.g. on default parameters of a call.
13956 if Expander_Active or GNATprove_Mode then
13957 Force_Evaluation
13958 (Low_Bound (R), Related_Id => Def_Id, Is_Low_Bound => True);
13959 Force_Evaluation
13960 (High_Bound (R), Related_Id => Def_Id, Is_High_Bound => True);
13961 end if;
13963 elsif Nkind (S) = N_Discriminant_Association then
13965 -- Syntactically valid in subtype indication
13967 Error_Msg_N ("invalid index constraint", S);
13968 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
13969 return;
13971 -- Subtype_Mark case, no anonymous subtypes to construct
13973 else
13974 Analyze (S);
13976 if Is_Entity_Name (S) then
13977 if not Is_Type (Entity (S)) then
13978 Error_Msg_N ("expect subtype mark for index constraint", S);
13980 elsif Base_Type (Entity (S)) /= Base_Type (T) then
13981 Wrong_Type (S, Base_Type (T));
13983 -- Check error of subtype with predicate in index constraint
13985 else
13986 Bad_Predicated_Subtype_Use
13987 ("subtype& has predicate, not allowed in index constraint",
13988 S, Entity (S));
13989 end if;
13991 return;
13993 else
13994 Error_Msg_N ("invalid index constraint", S);
13995 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
13996 return;
13997 end if;
13998 end if;
14000 -- Complete construction of the Itype
14002 if Is_Modular_Integer_Type (T) then
14003 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
14005 elsif Is_Integer_Type (T) then
14006 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
14008 else
14009 Set_Ekind (Def_Id, E_Enumeration_Subtype);
14010 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
14011 Set_First_Literal (Def_Id, First_Literal (T));
14012 end if;
14014 Set_Size_Info (Def_Id, (T));
14015 Set_RM_Size (Def_Id, RM_Size (T));
14016 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14018 Set_Scalar_Range (Def_Id, R);
14020 Set_Etype (S, Def_Id);
14021 Set_Discrete_RM_Size (Def_Id);
14022 end Constrain_Index;
14024 -----------------------
14025 -- Constrain_Integer --
14026 -----------------------
14028 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id) is
14029 T : constant Entity_Id := Entity (Subtype_Mark (S));
14030 C : constant Node_Id := Constraint (S);
14032 begin
14033 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14035 if Is_Modular_Integer_Type (T) then
14036 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
14037 else
14038 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
14039 end if;
14041 Set_Etype (Def_Id, Base_Type (T));
14042 Set_Size_Info (Def_Id, (T));
14043 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14044 Set_Discrete_RM_Size (Def_Id);
14045 end Constrain_Integer;
14047 ------------------------------
14048 -- Constrain_Ordinary_Fixed --
14049 ------------------------------
14051 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id) is
14052 T : constant Entity_Id := Entity (Subtype_Mark (S));
14053 C : Node_Id;
14054 D : Node_Id;
14055 Rais : Node_Id;
14057 begin
14058 Set_Ekind (Def_Id, E_Ordinary_Fixed_Point_Subtype);
14059 Set_Etype (Def_Id, Base_Type (T));
14060 Set_Size_Info (Def_Id, (T));
14061 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14062 Set_Small_Value (Def_Id, Small_Value (T));
14064 -- Process the constraint
14066 C := Constraint (S);
14068 -- Delta constraint present
14070 if Nkind (C) = N_Delta_Constraint then
14072 Check_SPARK_05_Restriction ("delta constraint is not allowed", S);
14073 Check_Restriction (No_Obsolescent_Features, C);
14075 if Warn_On_Obsolescent_Feature then
14076 Error_Msg_S
14077 ("subtype delta constraint is an " &
14078 "obsolescent feature (RM J.3(7))?j?");
14079 end if;
14081 D := Delta_Expression (C);
14082 Analyze_And_Resolve (D, Any_Real);
14083 Check_Delta_Expression (D);
14084 Set_Delta_Value (Def_Id, Expr_Value_R (D));
14086 -- Check that delta value is in range. Obviously we can do this
14087 -- at compile time, but it is strictly a runtime check, and of
14088 -- course there is an ACVC test that checks this.
14090 if Delta_Value (Def_Id) < Delta_Value (T) then
14091 Error_Msg_N ("??delta value is too small", D);
14092 Rais :=
14093 Make_Raise_Constraint_Error (Sloc (D),
14094 Reason => CE_Range_Check_Failed);
14095 Insert_Action (Declaration_Node (Def_Id), Rais);
14096 end if;
14098 C := Range_Constraint (C);
14100 -- No delta constraint present
14102 else
14103 Set_Delta_Value (Def_Id, Delta_Value (T));
14104 end if;
14106 -- Range constraint present
14108 if Nkind (C) = N_Range_Constraint then
14109 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14111 -- No range constraint present
14113 else
14114 pragma Assert (No (C));
14115 Set_Scalar_Range (Def_Id, Scalar_Range (T));
14116 end if;
14118 Set_Discrete_RM_Size (Def_Id);
14120 -- Unconditionally delay the freeze, since we cannot set size
14121 -- information in all cases correctly until the freeze point.
14123 Set_Has_Delayed_Freeze (Def_Id);
14124 end Constrain_Ordinary_Fixed;
14126 -----------------------
14127 -- Contain_Interface --
14128 -----------------------
14130 function Contain_Interface
14131 (Iface : Entity_Id;
14132 Ifaces : Elist_Id) return Boolean
14134 Iface_Elmt : Elmt_Id;
14136 begin
14137 if Present (Ifaces) then
14138 Iface_Elmt := First_Elmt (Ifaces);
14139 while Present (Iface_Elmt) loop
14140 if Node (Iface_Elmt) = Iface then
14141 return True;
14142 end if;
14144 Next_Elmt (Iface_Elmt);
14145 end loop;
14146 end if;
14148 return False;
14149 end Contain_Interface;
14151 ---------------------------
14152 -- Convert_Scalar_Bounds --
14153 ---------------------------
14155 procedure Convert_Scalar_Bounds
14156 (N : Node_Id;
14157 Parent_Type : Entity_Id;
14158 Derived_Type : Entity_Id;
14159 Loc : Source_Ptr)
14161 Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
14163 Lo : Node_Id;
14164 Hi : Node_Id;
14165 Rng : Node_Id;
14167 begin
14168 -- Defend against previous errors
14170 if No (Scalar_Range (Derived_Type)) then
14171 Check_Error_Detected;
14172 return;
14173 end if;
14175 Lo := Build_Scalar_Bound
14176 (Type_Low_Bound (Derived_Type),
14177 Parent_Type, Implicit_Base);
14179 Hi := Build_Scalar_Bound
14180 (Type_High_Bound (Derived_Type),
14181 Parent_Type, Implicit_Base);
14183 Rng :=
14184 Make_Range (Loc,
14185 Low_Bound => Lo,
14186 High_Bound => Hi);
14188 Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
14190 Set_Parent (Rng, N);
14191 Set_Scalar_Range (Derived_Type, Rng);
14193 -- Analyze the bounds
14195 Analyze_And_Resolve (Lo, Implicit_Base);
14196 Analyze_And_Resolve (Hi, Implicit_Base);
14198 -- Analyze the range itself, except that we do not analyze it if
14199 -- the bounds are real literals, and we have a fixed-point type.
14200 -- The reason for this is that we delay setting the bounds in this
14201 -- case till we know the final Small and Size values (see circuit
14202 -- in Freeze.Freeze_Fixed_Point_Type for further details).
14204 if Is_Fixed_Point_Type (Parent_Type)
14205 and then Nkind (Lo) = N_Real_Literal
14206 and then Nkind (Hi) = N_Real_Literal
14207 then
14208 return;
14210 -- Here we do the analysis of the range
14212 -- Note: we do this manually, since if we do a normal Analyze and
14213 -- Resolve call, there are problems with the conversions used for
14214 -- the derived type range.
14216 else
14217 Set_Etype (Rng, Implicit_Base);
14218 Set_Analyzed (Rng, True);
14219 end if;
14220 end Convert_Scalar_Bounds;
14222 -------------------
14223 -- Copy_And_Swap --
14224 -------------------
14226 procedure Copy_And_Swap (Priv, Full : Entity_Id) is
14227 begin
14228 -- Initialize new full declaration entity by copying the pertinent
14229 -- fields of the corresponding private declaration entity.
14231 -- We temporarily set Ekind to a value appropriate for a type to
14232 -- avoid assert failures in Einfo from checking for setting type
14233 -- attributes on something that is not a type. Ekind (Priv) is an
14234 -- appropriate choice, since it allowed the attributes to be set
14235 -- in the first place. This Ekind value will be modified later.
14237 Set_Ekind (Full, Ekind (Priv));
14239 -- Also set Etype temporarily to Any_Type, again, in the absence
14240 -- of errors, it will be properly reset, and if there are errors,
14241 -- then we want a value of Any_Type to remain.
14243 Set_Etype (Full, Any_Type);
14245 -- Now start copying attributes
14247 Set_Has_Discriminants (Full, Has_Discriminants (Priv));
14249 if Has_Discriminants (Full) then
14250 Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
14251 Set_Stored_Constraint (Full, Stored_Constraint (Priv));
14252 end if;
14254 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
14255 Set_Homonym (Full, Homonym (Priv));
14256 Set_Is_Immediately_Visible (Full, Is_Immediately_Visible (Priv));
14257 Set_Is_Public (Full, Is_Public (Priv));
14258 Set_Is_Pure (Full, Is_Pure (Priv));
14259 Set_Is_Tagged_Type (Full, Is_Tagged_Type (Priv));
14260 Set_Has_Pragma_Unmodified (Full, Has_Pragma_Unmodified (Priv));
14261 Set_Has_Pragma_Unreferenced (Full, Has_Pragma_Unreferenced (Priv));
14262 Set_Has_Pragma_Unreferenced_Objects
14263 (Full, Has_Pragma_Unreferenced_Objects
14264 (Priv));
14266 Conditional_Delay (Full, Priv);
14268 if Is_Tagged_Type (Full) then
14269 Set_Direct_Primitive_Operations
14270 (Full, Direct_Primitive_Operations (Priv));
14271 Set_No_Tagged_Streams_Pragma
14272 (Full, No_Tagged_Streams_Pragma (Priv));
14274 if Is_Base_Type (Priv) then
14275 Set_Class_Wide_Type (Full, Class_Wide_Type (Priv));
14276 end if;
14277 end if;
14279 Set_Is_Volatile (Full, Is_Volatile (Priv));
14280 Set_Treat_As_Volatile (Full, Treat_As_Volatile (Priv));
14281 Set_Scope (Full, Scope (Priv));
14282 Set_Next_Entity (Full, Next_Entity (Priv));
14283 Set_First_Entity (Full, First_Entity (Priv));
14284 Set_Last_Entity (Full, Last_Entity (Priv));
14286 -- If access types have been recorded for later handling, keep them in
14287 -- the full view so that they get handled when the full view freeze
14288 -- node is expanded.
14290 if Present (Freeze_Node (Priv))
14291 and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
14292 then
14293 Ensure_Freeze_Node (Full);
14294 Set_Access_Types_To_Process
14295 (Freeze_Node (Full),
14296 Access_Types_To_Process (Freeze_Node (Priv)));
14297 end if;
14299 -- Swap the two entities. Now Private is the full type entity and Full
14300 -- is the private one. They will be swapped back at the end of the
14301 -- private part. This swapping ensures that the entity that is visible
14302 -- in the private part is the full declaration.
14304 Exchange_Entities (Priv, Full);
14305 Append_Entity (Full, Scope (Full));
14306 end Copy_And_Swap;
14308 -------------------------------------
14309 -- Copy_Array_Base_Type_Attributes --
14310 -------------------------------------
14312 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
14313 begin
14314 Set_Component_Alignment (T1, Component_Alignment (T2));
14315 Set_Component_Type (T1, Component_Type (T2));
14316 Set_Component_Size (T1, Component_Size (T2));
14317 Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
14318 Set_Has_Non_Standard_Rep (T1, Has_Non_Standard_Rep (T2));
14319 Propagate_Concurrent_Flags (T1, T2);
14320 Set_Is_Packed (T1, Is_Packed (T2));
14321 Set_Has_Aliased_Components (T1, Has_Aliased_Components (T2));
14322 Set_Has_Atomic_Components (T1, Has_Atomic_Components (T2));
14323 Set_Has_Volatile_Components (T1, Has_Volatile_Components (T2));
14324 end Copy_Array_Base_Type_Attributes;
14326 -----------------------------------
14327 -- Copy_Array_Subtype_Attributes --
14328 -----------------------------------
14330 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
14331 begin
14332 Set_Size_Info (T1, T2);
14334 Set_First_Index (T1, First_Index (T2));
14335 Set_Is_Aliased (T1, Is_Aliased (T2));
14336 Set_Is_Volatile (T1, Is_Volatile (T2));
14337 Set_Treat_As_Volatile (T1, Treat_As_Volatile (T2));
14338 Set_Is_Constrained (T1, Is_Constrained (T2));
14339 Set_Depends_On_Private (T1, Has_Private_Component (T2));
14340 Inherit_Rep_Item_Chain (T1, T2);
14341 Set_Convention (T1, Convention (T2));
14342 Set_Is_Limited_Composite (T1, Is_Limited_Composite (T2));
14343 Set_Is_Private_Composite (T1, Is_Private_Composite (T2));
14344 Set_Packed_Array_Impl_Type (T1, Packed_Array_Impl_Type (T2));
14345 end Copy_Array_Subtype_Attributes;
14347 -----------------------------------
14348 -- Create_Constrained_Components --
14349 -----------------------------------
14351 procedure Create_Constrained_Components
14352 (Subt : Entity_Id;
14353 Decl_Node : Node_Id;
14354 Typ : Entity_Id;
14355 Constraints : Elist_Id)
14357 Loc : constant Source_Ptr := Sloc (Subt);
14358 Comp_List : constant Elist_Id := New_Elmt_List;
14359 Parent_Type : constant Entity_Id := Etype (Typ);
14360 Assoc_List : constant List_Id := New_List;
14361 Discr_Val : Elmt_Id;
14362 Errors : Boolean;
14363 New_C : Entity_Id;
14364 Old_C : Entity_Id;
14365 Is_Static : Boolean := True;
14367 procedure Collect_Fixed_Components (Typ : Entity_Id);
14368 -- Collect parent type components that do not appear in a variant part
14370 procedure Create_All_Components;
14371 -- Iterate over Comp_List to create the components of the subtype
14373 function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
14374 -- Creates a new component from Old_Compon, copying all the fields from
14375 -- it, including its Etype, inserts the new component in the Subt entity
14376 -- chain and returns the new component.
14378 function Is_Variant_Record (T : Entity_Id) return Boolean;
14379 -- If true, and discriminants are static, collect only components from
14380 -- variants selected by discriminant values.
14382 ------------------------------
14383 -- Collect_Fixed_Components --
14384 ------------------------------
14386 procedure Collect_Fixed_Components (Typ : Entity_Id) is
14387 begin
14388 -- Build association list for discriminants, and find components of the
14389 -- variant part selected by the values of the discriminants.
14391 Old_C := First_Discriminant (Typ);
14392 Discr_Val := First_Elmt (Constraints);
14393 while Present (Old_C) loop
14394 Append_To (Assoc_List,
14395 Make_Component_Association (Loc,
14396 Choices => New_List (New_Occurrence_Of (Old_C, Loc)),
14397 Expression => New_Copy (Node (Discr_Val))));
14399 Next_Elmt (Discr_Val);
14400 Next_Discriminant (Old_C);
14401 end loop;
14403 -- The tag and the possible parent component are unconditionally in
14404 -- the subtype.
14406 if Is_Tagged_Type (Typ) or else Has_Controlled_Component (Typ) then
14407 Old_C := First_Component (Typ);
14408 while Present (Old_C) loop
14409 if Nam_In (Chars (Old_C), Name_uTag, Name_uParent) then
14410 Append_Elmt (Old_C, Comp_List);
14411 end if;
14413 Next_Component (Old_C);
14414 end loop;
14415 end if;
14416 end Collect_Fixed_Components;
14418 ---------------------------
14419 -- Create_All_Components --
14420 ---------------------------
14422 procedure Create_All_Components is
14423 Comp : Elmt_Id;
14425 begin
14426 Comp := First_Elmt (Comp_List);
14427 while Present (Comp) loop
14428 Old_C := Node (Comp);
14429 New_C := Create_Component (Old_C);
14431 Set_Etype
14432 (New_C,
14433 Constrain_Component_Type
14434 (Old_C, Subt, Decl_Node, Typ, Constraints));
14435 Set_Is_Public (New_C, Is_Public (Subt));
14437 Next_Elmt (Comp);
14438 end loop;
14439 end Create_All_Components;
14441 ----------------------
14442 -- Create_Component --
14443 ----------------------
14445 function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
14446 New_Compon : constant Entity_Id := New_Copy (Old_Compon);
14448 begin
14449 if Ekind (Old_Compon) = E_Discriminant
14450 and then Is_Completely_Hidden (Old_Compon)
14451 then
14452 -- This is a shadow discriminant created for a discriminant of
14453 -- the parent type, which needs to be present in the subtype.
14454 -- Give the shadow discriminant an internal name that cannot
14455 -- conflict with that of visible components.
14457 Set_Chars (New_Compon, New_Internal_Name ('C'));
14458 end if;
14460 -- Set the parent so we have a proper link for freezing etc. This is
14461 -- not a real parent pointer, since of course our parent does not own
14462 -- up to us and reference us, we are an illegitimate child of the
14463 -- original parent.
14465 Set_Parent (New_Compon, Parent (Old_Compon));
14467 -- We do not want this node marked as Comes_From_Source, since
14468 -- otherwise it would get first class status and a separate cross-
14469 -- reference line would be generated. Illegitimate children do not
14470 -- rate such recognition.
14472 Set_Comes_From_Source (New_Compon, False);
14474 -- But it is a real entity, and a birth certificate must be properly
14475 -- registered by entering it into the entity list.
14477 Enter_Name (New_Compon);
14479 return New_Compon;
14480 end Create_Component;
14482 -----------------------
14483 -- Is_Variant_Record --
14484 -----------------------
14486 function Is_Variant_Record (T : Entity_Id) return Boolean is
14487 begin
14488 return Nkind (Parent (T)) = N_Full_Type_Declaration
14489 and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
14490 and then Present (Component_List (Type_Definition (Parent (T))))
14491 and then
14492 Present
14493 (Variant_Part (Component_List (Type_Definition (Parent (T)))));
14494 end Is_Variant_Record;
14496 -- Start of processing for Create_Constrained_Components
14498 begin
14499 pragma Assert (Subt /= Base_Type (Subt));
14500 pragma Assert (Typ = Base_Type (Typ));
14502 Set_First_Entity (Subt, Empty);
14503 Set_Last_Entity (Subt, Empty);
14505 -- Check whether constraint is fully static, in which case we can
14506 -- optimize the list of components.
14508 Discr_Val := First_Elmt (Constraints);
14509 while Present (Discr_Val) loop
14510 if not Is_OK_Static_Expression (Node (Discr_Val)) then
14511 Is_Static := False;
14512 exit;
14513 end if;
14515 Next_Elmt (Discr_Val);
14516 end loop;
14518 Set_Has_Static_Discriminants (Subt, Is_Static);
14520 Push_Scope (Subt);
14522 -- Inherit the discriminants of the parent type
14524 Add_Discriminants : declare
14525 Num_Disc : Nat;
14526 Num_Gird : Nat;
14528 begin
14529 Num_Disc := 0;
14530 Old_C := First_Discriminant (Typ);
14532 while Present (Old_C) loop
14533 Num_Disc := Num_Disc + 1;
14534 New_C := Create_Component (Old_C);
14535 Set_Is_Public (New_C, Is_Public (Subt));
14536 Next_Discriminant (Old_C);
14537 end loop;
14539 -- For an untagged derived subtype, the number of discriminants may
14540 -- be smaller than the number of inherited discriminants, because
14541 -- several of them may be renamed by a single new discriminant or
14542 -- constrained. In this case, add the hidden discriminants back into
14543 -- the subtype, because they need to be present if the optimizer of
14544 -- the GCC 4.x back-end decides to break apart assignments between
14545 -- objects using the parent view into member-wise assignments.
14547 Num_Gird := 0;
14549 if Is_Derived_Type (Typ)
14550 and then not Is_Tagged_Type (Typ)
14551 then
14552 Old_C := First_Stored_Discriminant (Typ);
14554 while Present (Old_C) loop
14555 Num_Gird := Num_Gird + 1;
14556 Next_Stored_Discriminant (Old_C);
14557 end loop;
14558 end if;
14560 if Num_Gird > Num_Disc then
14562 -- Find out multiple uses of new discriminants, and add hidden
14563 -- components for the extra renamed discriminants. We recognize
14564 -- multiple uses through the Corresponding_Discriminant of a
14565 -- new discriminant: if it constrains several old discriminants,
14566 -- this field points to the last one in the parent type. The
14567 -- stored discriminants of the derived type have the same name
14568 -- as those of the parent.
14570 declare
14571 Constr : Elmt_Id;
14572 New_Discr : Entity_Id;
14573 Old_Discr : Entity_Id;
14575 begin
14576 Constr := First_Elmt (Stored_Constraint (Typ));
14577 Old_Discr := First_Stored_Discriminant (Typ);
14578 while Present (Constr) loop
14579 if Is_Entity_Name (Node (Constr))
14580 and then Ekind (Entity (Node (Constr))) = E_Discriminant
14581 then
14582 New_Discr := Entity (Node (Constr));
14584 if Chars (Corresponding_Discriminant (New_Discr)) /=
14585 Chars (Old_Discr)
14586 then
14587 -- The new discriminant has been used to rename a
14588 -- subsequent old discriminant. Introduce a shadow
14589 -- component for the current old discriminant.
14591 New_C := Create_Component (Old_Discr);
14592 Set_Original_Record_Component (New_C, Old_Discr);
14593 end if;
14595 else
14596 -- The constraint has eliminated the old discriminant.
14597 -- Introduce a shadow component.
14599 New_C := Create_Component (Old_Discr);
14600 Set_Original_Record_Component (New_C, Old_Discr);
14601 end if;
14603 Next_Elmt (Constr);
14604 Next_Stored_Discriminant (Old_Discr);
14605 end loop;
14606 end;
14607 end if;
14608 end Add_Discriminants;
14610 if Is_Static
14611 and then Is_Variant_Record (Typ)
14612 then
14613 Collect_Fixed_Components (Typ);
14615 Gather_Components (
14616 Typ,
14617 Component_List (Type_Definition (Parent (Typ))),
14618 Governed_By => Assoc_List,
14619 Into => Comp_List,
14620 Report_Errors => Errors);
14621 pragma Assert (not Errors
14622 or else Serious_Errors_Detected > 0);
14624 Create_All_Components;
14626 -- If the subtype declaration is created for a tagged type derivation
14627 -- with constraints, we retrieve the record definition of the parent
14628 -- type to select the components of the proper variant.
14630 elsif Is_Static
14631 and then Is_Tagged_Type (Typ)
14632 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
14633 and then
14634 Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
14635 and then Is_Variant_Record (Parent_Type)
14636 then
14637 Collect_Fixed_Components (Typ);
14639 Gather_Components
14640 (Typ,
14641 Component_List (Type_Definition (Parent (Parent_Type))),
14642 Governed_By => Assoc_List,
14643 Into => Comp_List,
14644 Report_Errors => Errors);
14646 -- Note: previously there was a check at this point that no errors
14647 -- were detected. As a consequence of AI05-220 there may be an error
14648 -- if an inherited discriminant that controls a variant has a non-
14649 -- static constraint.
14651 -- If the tagged derivation has a type extension, collect all the
14652 -- new components therein.
14654 if Present (Record_Extension_Part (Type_Definition (Parent (Typ))))
14655 then
14656 Old_C := First_Component (Typ);
14657 while Present (Old_C) loop
14658 if Original_Record_Component (Old_C) = Old_C
14659 and then Chars (Old_C) /= Name_uTag
14660 and then Chars (Old_C) /= Name_uParent
14661 then
14662 Append_Elmt (Old_C, Comp_List);
14663 end if;
14665 Next_Component (Old_C);
14666 end loop;
14667 end if;
14669 Create_All_Components;
14671 else
14672 -- If discriminants are not static, or if this is a multi-level type
14673 -- extension, we have to include all components of the parent type.
14675 Old_C := First_Component (Typ);
14676 while Present (Old_C) loop
14677 New_C := Create_Component (Old_C);
14679 Set_Etype
14680 (New_C,
14681 Constrain_Component_Type
14682 (Old_C, Subt, Decl_Node, Typ, Constraints));
14683 Set_Is_Public (New_C, Is_Public (Subt));
14685 Next_Component (Old_C);
14686 end loop;
14687 end if;
14689 End_Scope;
14690 end Create_Constrained_Components;
14692 ------------------------------------------
14693 -- Decimal_Fixed_Point_Type_Declaration --
14694 ------------------------------------------
14696 procedure Decimal_Fixed_Point_Type_Declaration
14697 (T : Entity_Id;
14698 Def : Node_Id)
14700 Loc : constant Source_Ptr := Sloc (Def);
14701 Digs_Expr : constant Node_Id := Digits_Expression (Def);
14702 Delta_Expr : constant Node_Id := Delta_Expression (Def);
14703 Implicit_Base : Entity_Id;
14704 Digs_Val : Uint;
14705 Delta_Val : Ureal;
14706 Scale_Val : Uint;
14707 Bound_Val : Ureal;
14709 begin
14710 Check_SPARK_05_Restriction
14711 ("decimal fixed point type is not allowed", Def);
14712 Check_Restriction (No_Fixed_Point, Def);
14714 -- Create implicit base type
14716 Implicit_Base :=
14717 Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
14718 Set_Etype (Implicit_Base, Implicit_Base);
14720 -- Analyze and process delta expression
14722 Analyze_And_Resolve (Delta_Expr, Universal_Real);
14724 Check_Delta_Expression (Delta_Expr);
14725 Delta_Val := Expr_Value_R (Delta_Expr);
14727 -- Check delta is power of 10, and determine scale value from it
14729 declare
14730 Val : Ureal;
14732 begin
14733 Scale_Val := Uint_0;
14734 Val := Delta_Val;
14736 if Val < Ureal_1 then
14737 while Val < Ureal_1 loop
14738 Val := Val * Ureal_10;
14739 Scale_Val := Scale_Val + 1;
14740 end loop;
14742 if Scale_Val > 18 then
14743 Error_Msg_N ("scale exceeds maximum value of 18", Def);
14744 Scale_Val := UI_From_Int (+18);
14745 end if;
14747 else
14748 while Val > Ureal_1 loop
14749 Val := Val / Ureal_10;
14750 Scale_Val := Scale_Val - 1;
14751 end loop;
14753 if Scale_Val < -18 then
14754 Error_Msg_N ("scale is less than minimum value of -18", Def);
14755 Scale_Val := UI_From_Int (-18);
14756 end if;
14757 end if;
14759 if Val /= Ureal_1 then
14760 Error_Msg_N ("delta expression must be a power of 10", Def);
14761 Delta_Val := Ureal_10 ** (-Scale_Val);
14762 end if;
14763 end;
14765 -- Set delta, scale and small (small = delta for decimal type)
14767 Set_Delta_Value (Implicit_Base, Delta_Val);
14768 Set_Scale_Value (Implicit_Base, Scale_Val);
14769 Set_Small_Value (Implicit_Base, Delta_Val);
14771 -- Analyze and process digits expression
14773 Analyze_And_Resolve (Digs_Expr, Any_Integer);
14774 Check_Digits_Expression (Digs_Expr);
14775 Digs_Val := Expr_Value (Digs_Expr);
14777 if Digs_Val > 18 then
14778 Digs_Val := UI_From_Int (+18);
14779 Error_Msg_N ("digits value out of range, maximum is 18", Digs_Expr);
14780 end if;
14782 Set_Digits_Value (Implicit_Base, Digs_Val);
14783 Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
14785 -- Set range of base type from digits value for now. This will be
14786 -- expanded to represent the true underlying base range by Freeze.
14788 Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
14790 -- Note: We leave size as zero for now, size will be set at freeze
14791 -- time. We have to do this for ordinary fixed-point, because the size
14792 -- depends on the specified small, and we might as well do the same for
14793 -- decimal fixed-point.
14795 pragma Assert (Esize (Implicit_Base) = Uint_0);
14797 -- If there are bounds given in the declaration use them as the
14798 -- bounds of the first named subtype.
14800 if Present (Real_Range_Specification (Def)) then
14801 declare
14802 RRS : constant Node_Id := Real_Range_Specification (Def);
14803 Low : constant Node_Id := Low_Bound (RRS);
14804 High : constant Node_Id := High_Bound (RRS);
14805 Low_Val : Ureal;
14806 High_Val : Ureal;
14808 begin
14809 Analyze_And_Resolve (Low, Any_Real);
14810 Analyze_And_Resolve (High, Any_Real);
14811 Check_Real_Bound (Low);
14812 Check_Real_Bound (High);
14813 Low_Val := Expr_Value_R (Low);
14814 High_Val := Expr_Value_R (High);
14816 if Low_Val < (-Bound_Val) then
14817 Error_Msg_N
14818 ("range low bound too small for digits value", Low);
14819 Low_Val := -Bound_Val;
14820 end if;
14822 if High_Val > Bound_Val then
14823 Error_Msg_N
14824 ("range high bound too large for digits value", High);
14825 High_Val := Bound_Val;
14826 end if;
14828 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
14829 end;
14831 -- If no explicit range, use range that corresponds to given
14832 -- digits value. This will end up as the final range for the
14833 -- first subtype.
14835 else
14836 Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
14837 end if;
14839 -- Complete entity for first subtype. The inheritance of the rep item
14840 -- chain ensures that SPARK-related pragmas are not clobbered when the
14841 -- decimal fixed point type acts as a full view of a private type.
14843 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
14844 Set_Etype (T, Implicit_Base);
14845 Set_Size_Info (T, Implicit_Base);
14846 Inherit_Rep_Item_Chain (T, Implicit_Base);
14847 Set_Digits_Value (T, Digs_Val);
14848 Set_Delta_Value (T, Delta_Val);
14849 Set_Small_Value (T, Delta_Val);
14850 Set_Scale_Value (T, Scale_Val);
14851 Set_Is_Constrained (T);
14852 end Decimal_Fixed_Point_Type_Declaration;
14854 -----------------------------------
14855 -- Derive_Progenitor_Subprograms --
14856 -----------------------------------
14858 procedure Derive_Progenitor_Subprograms
14859 (Parent_Type : Entity_Id;
14860 Tagged_Type : Entity_Id)
14862 E : Entity_Id;
14863 Elmt : Elmt_Id;
14864 Iface : Entity_Id;
14865 Iface_Elmt : Elmt_Id;
14866 Iface_Subp : Entity_Id;
14867 New_Subp : Entity_Id := Empty;
14868 Prim_Elmt : Elmt_Id;
14869 Subp : Entity_Id;
14870 Typ : Entity_Id;
14872 begin
14873 pragma Assert (Ada_Version >= Ada_2005
14874 and then Is_Record_Type (Tagged_Type)
14875 and then Is_Tagged_Type (Tagged_Type)
14876 and then Has_Interfaces (Tagged_Type));
14878 -- Step 1: Transfer to the full-view primitives associated with the
14879 -- partial-view that cover interface primitives. Conceptually this
14880 -- work should be done later by Process_Full_View; done here to
14881 -- simplify its implementation at later stages. It can be safely
14882 -- done here because interfaces must be visible in the partial and
14883 -- private view (RM 7.3(7.3/2)).
14885 -- Small optimization: This work is only required if the parent may
14886 -- have entities whose Alias attribute reference an interface primitive.
14887 -- Such a situation may occur if the parent is an abstract type and the
14888 -- primitive has not been yet overridden or if the parent is a generic
14889 -- formal type covering interfaces.
14891 -- If the tagged type is not abstract, it cannot have abstract
14892 -- primitives (the only entities in the list of primitives of
14893 -- non-abstract tagged types that can reference abstract primitives
14894 -- through its Alias attribute are the internal entities that have
14895 -- attribute Interface_Alias, and these entities are generated later
14896 -- by Add_Internal_Interface_Entities).
14898 if In_Private_Part (Current_Scope)
14899 and then (Is_Abstract_Type (Parent_Type)
14900 or else
14901 Is_Generic_Type (Parent_Type))
14902 then
14903 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
14904 while Present (Elmt) loop
14905 Subp := Node (Elmt);
14907 -- At this stage it is not possible to have entities in the list
14908 -- of primitives that have attribute Interface_Alias.
14910 pragma Assert (No (Interface_Alias (Subp)));
14912 Typ := Find_Dispatching_Type (Ultimate_Alias (Subp));
14914 if Is_Interface (Typ) then
14915 E := Find_Primitive_Covering_Interface
14916 (Tagged_Type => Tagged_Type,
14917 Iface_Prim => Subp);
14919 if Present (E)
14920 and then Find_Dispatching_Type (Ultimate_Alias (E)) /= Typ
14921 then
14922 Replace_Elmt (Elmt, E);
14923 Remove_Homonym (Subp);
14924 end if;
14925 end if;
14927 Next_Elmt (Elmt);
14928 end loop;
14929 end if;
14931 -- Step 2: Add primitives of progenitors that are not implemented by
14932 -- parents of Tagged_Type.
14934 if Present (Interfaces (Base_Type (Tagged_Type))) then
14935 Iface_Elmt := First_Elmt (Interfaces (Base_Type (Tagged_Type)));
14936 while Present (Iface_Elmt) loop
14937 Iface := Node (Iface_Elmt);
14939 Prim_Elmt := First_Elmt (Primitive_Operations (Iface));
14940 while Present (Prim_Elmt) loop
14941 Iface_Subp := Node (Prim_Elmt);
14943 -- Exclude derivation of predefined primitives except those
14944 -- that come from source, or are inherited from one that comes
14945 -- from source. Required to catch declarations of equality
14946 -- operators of interfaces. For example:
14948 -- type Iface is interface;
14949 -- function "=" (Left, Right : Iface) return Boolean;
14951 if not Is_Predefined_Dispatching_Operation (Iface_Subp)
14952 or else Comes_From_Source (Ultimate_Alias (Iface_Subp))
14953 then
14954 E := Find_Primitive_Covering_Interface
14955 (Tagged_Type => Tagged_Type,
14956 Iface_Prim => Iface_Subp);
14958 -- If not found we derive a new primitive leaving its alias
14959 -- attribute referencing the interface primitive.
14961 if No (E) then
14962 Derive_Subprogram
14963 (New_Subp, Iface_Subp, Tagged_Type, Iface);
14965 -- Ada 2012 (AI05-0197): If the covering primitive's name
14966 -- differs from the name of the interface primitive then it
14967 -- is a private primitive inherited from a parent type. In
14968 -- such case, given that Tagged_Type covers the interface,
14969 -- the inherited private primitive becomes visible. For such
14970 -- purpose we add a new entity that renames the inherited
14971 -- private primitive.
14973 elsif Chars (E) /= Chars (Iface_Subp) then
14974 pragma Assert (Has_Suffix (E, 'P'));
14975 Derive_Subprogram
14976 (New_Subp, Iface_Subp, Tagged_Type, Iface);
14977 Set_Alias (New_Subp, E);
14978 Set_Is_Abstract_Subprogram (New_Subp,
14979 Is_Abstract_Subprogram (E));
14981 -- Propagate to the full view interface entities associated
14982 -- with the partial view.
14984 elsif In_Private_Part (Current_Scope)
14985 and then Present (Alias (E))
14986 and then Alias (E) = Iface_Subp
14987 and then
14988 List_Containing (Parent (E)) /=
14989 Private_Declarations
14990 (Specification
14991 (Unit_Declaration_Node (Current_Scope)))
14992 then
14993 Append_Elmt (E, Primitive_Operations (Tagged_Type));
14994 end if;
14995 end if;
14997 Next_Elmt (Prim_Elmt);
14998 end loop;
15000 Next_Elmt (Iface_Elmt);
15001 end loop;
15002 end if;
15003 end Derive_Progenitor_Subprograms;
15005 -----------------------
15006 -- Derive_Subprogram --
15007 -----------------------
15009 procedure Derive_Subprogram
15010 (New_Subp : out Entity_Id;
15011 Parent_Subp : Entity_Id;
15012 Derived_Type : Entity_Id;
15013 Parent_Type : Entity_Id;
15014 Actual_Subp : Entity_Id := Empty)
15016 Formal : Entity_Id;
15017 -- Formal parameter of parent primitive operation
15019 Formal_Of_Actual : Entity_Id;
15020 -- Formal parameter of actual operation, when the derivation is to
15021 -- create a renaming for a primitive operation of an actual in an
15022 -- instantiation.
15024 New_Formal : Entity_Id;
15025 -- Formal of inherited operation
15027 Visible_Subp : Entity_Id := Parent_Subp;
15029 function Is_Private_Overriding return Boolean;
15030 -- If Subp is a private overriding of a visible operation, the inherited
15031 -- operation derives from the overridden op (even though its body is the
15032 -- overriding one) and the inherited operation is visible now. See
15033 -- sem_disp to see the full details of the handling of the overridden
15034 -- subprogram, which is removed from the list of primitive operations of
15035 -- the type. The overridden subprogram is saved locally in Visible_Subp,
15036 -- and used to diagnose abstract operations that need overriding in the
15037 -- derived type.
15039 procedure Replace_Type (Id, New_Id : Entity_Id);
15040 -- When the type is an anonymous access type, create a new access type
15041 -- designating the derived type.
15043 procedure Set_Derived_Name;
15044 -- This procedure sets the appropriate Chars name for New_Subp. This
15045 -- is normally just a copy of the parent name. An exception arises for
15046 -- type support subprograms, where the name is changed to reflect the
15047 -- name of the derived type, e.g. if type foo is derived from type bar,
15048 -- then a procedure barDA is derived with a name fooDA.
15050 ---------------------------
15051 -- Is_Private_Overriding --
15052 ---------------------------
15054 function Is_Private_Overriding return Boolean is
15055 Prev : Entity_Id;
15057 begin
15058 -- If the parent is not a dispatching operation there is no
15059 -- need to investigate overridings
15061 if not Is_Dispatching_Operation (Parent_Subp) then
15062 return False;
15063 end if;
15065 -- The visible operation that is overridden is a homonym of the
15066 -- parent subprogram. We scan the homonym chain to find the one
15067 -- whose alias is the subprogram we are deriving.
15069 Prev := Current_Entity (Parent_Subp);
15070 while Present (Prev) loop
15071 if Ekind (Prev) = Ekind (Parent_Subp)
15072 and then Alias (Prev) = Parent_Subp
15073 and then Scope (Parent_Subp) = Scope (Prev)
15074 and then not Is_Hidden (Prev)
15075 then
15076 Visible_Subp := Prev;
15077 return True;
15078 end if;
15080 Prev := Homonym (Prev);
15081 end loop;
15083 return False;
15084 end Is_Private_Overriding;
15086 ------------------
15087 -- Replace_Type --
15088 ------------------
15090 procedure Replace_Type (Id, New_Id : Entity_Id) is
15091 Id_Type : constant Entity_Id := Etype (Id);
15092 Acc_Type : Entity_Id;
15093 Par : constant Node_Id := Parent (Derived_Type);
15095 begin
15096 -- When the type is an anonymous access type, create a new access
15097 -- type designating the derived type. This itype must be elaborated
15098 -- at the point of the derivation, not on subsequent calls that may
15099 -- be out of the proper scope for Gigi, so we insert a reference to
15100 -- it after the derivation.
15102 if Ekind (Id_Type) = E_Anonymous_Access_Type then
15103 declare
15104 Desig_Typ : Entity_Id := Designated_Type (Id_Type);
15106 begin
15107 if Ekind (Desig_Typ) = E_Record_Type_With_Private
15108 and then Present (Full_View (Desig_Typ))
15109 and then not Is_Private_Type (Parent_Type)
15110 then
15111 Desig_Typ := Full_View (Desig_Typ);
15112 end if;
15114 if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
15116 -- Ada 2005 (AI-251): Handle also derivations of abstract
15117 -- interface primitives.
15119 or else (Is_Interface (Desig_Typ)
15120 and then not Is_Class_Wide_Type (Desig_Typ))
15121 then
15122 Acc_Type := New_Copy (Id_Type);
15123 Set_Etype (Acc_Type, Acc_Type);
15124 Set_Scope (Acc_Type, New_Subp);
15126 -- Set size of anonymous access type. If we have an access
15127 -- to an unconstrained array, this is a fat pointer, so it
15128 -- is sizes at twice addtress size.
15130 if Is_Array_Type (Desig_Typ)
15131 and then not Is_Constrained (Desig_Typ)
15132 then
15133 Init_Size (Acc_Type, 2 * System_Address_Size);
15135 -- Other cases use a thin pointer
15137 else
15138 Init_Size (Acc_Type, System_Address_Size);
15139 end if;
15141 -- Set remaining characterstics of anonymous access type
15143 Init_Alignment (Acc_Type);
15144 Set_Directly_Designated_Type (Acc_Type, Derived_Type);
15146 Set_Etype (New_Id, Acc_Type);
15147 Set_Scope (New_Id, New_Subp);
15149 -- Create a reference to it
15151 Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
15153 else
15154 Set_Etype (New_Id, Id_Type);
15155 end if;
15156 end;
15158 -- In Ada2012, a formal may have an incomplete type but the type
15159 -- derivation that inherits the primitive follows the full view.
15161 elsif Base_Type (Id_Type) = Base_Type (Parent_Type)
15162 or else
15163 (Ekind (Id_Type) = E_Record_Type_With_Private
15164 and then Present (Full_View (Id_Type))
15165 and then
15166 Base_Type (Full_View (Id_Type)) = Base_Type (Parent_Type))
15167 or else
15168 (Ada_Version >= Ada_2012
15169 and then Ekind (Id_Type) = E_Incomplete_Type
15170 and then Full_View (Id_Type) = Parent_Type)
15171 then
15172 -- Constraint checks on formals are generated during expansion,
15173 -- based on the signature of the original subprogram. The bounds
15174 -- of the derived type are not relevant, and thus we can use
15175 -- the base type for the formals. However, the return type may be
15176 -- used in a context that requires that the proper static bounds
15177 -- be used (a case statement, for example) and for those cases
15178 -- we must use the derived type (first subtype), not its base.
15180 -- If the derived_type_definition has no constraints, we know that
15181 -- the derived type has the same constraints as the first subtype
15182 -- of the parent, and we can also use it rather than its base,
15183 -- which can lead to more efficient code.
15185 if Etype (Id) = Parent_Type then
15186 if Is_Scalar_Type (Parent_Type)
15187 and then
15188 Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
15189 then
15190 Set_Etype (New_Id, Derived_Type);
15192 elsif Nkind (Par) = N_Full_Type_Declaration
15193 and then
15194 Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
15195 and then
15196 Is_Entity_Name
15197 (Subtype_Indication (Type_Definition (Par)))
15198 then
15199 Set_Etype (New_Id, Derived_Type);
15201 else
15202 Set_Etype (New_Id, Base_Type (Derived_Type));
15203 end if;
15205 else
15206 Set_Etype (New_Id, Base_Type (Derived_Type));
15207 end if;
15209 else
15210 Set_Etype (New_Id, Etype (Id));
15211 end if;
15212 end Replace_Type;
15214 ----------------------
15215 -- Set_Derived_Name --
15216 ----------------------
15218 procedure Set_Derived_Name is
15219 Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
15220 begin
15221 if Nm = TSS_Null then
15222 Set_Chars (New_Subp, Chars (Parent_Subp));
15223 else
15224 Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
15225 end if;
15226 end Set_Derived_Name;
15228 -- Start of processing for Derive_Subprogram
15230 begin
15231 New_Subp := New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
15232 Set_Ekind (New_Subp, Ekind (Parent_Subp));
15234 -- Check whether the inherited subprogram is a private operation that
15235 -- should be inherited but not yet made visible. Such subprograms can
15236 -- become visible at a later point (e.g., the private part of a public
15237 -- child unit) via Declare_Inherited_Private_Subprograms. If the
15238 -- following predicate is true, then this is not such a private
15239 -- operation and the subprogram simply inherits the name of the parent
15240 -- subprogram. Note the special check for the names of controlled
15241 -- operations, which are currently exempted from being inherited with
15242 -- a hidden name because they must be findable for generation of
15243 -- implicit run-time calls.
15245 if not Is_Hidden (Parent_Subp)
15246 or else Is_Internal (Parent_Subp)
15247 or else Is_Private_Overriding
15248 or else Is_Internal_Name (Chars (Parent_Subp))
15249 or else (Is_Controlled (Parent_Type)
15250 and then Nam_In (Chars (Parent_Subp), Name_Adjust,
15251 Name_Finalize,
15252 Name_Initialize))
15253 then
15254 Set_Derived_Name;
15256 -- An inherited dispatching equality will be overridden by an internally
15257 -- generated one, or by an explicit one, so preserve its name and thus
15258 -- its entry in the dispatch table. Otherwise, if Parent_Subp is a
15259 -- private operation it may become invisible if the full view has
15260 -- progenitors, and the dispatch table will be malformed.
15261 -- We check that the type is limited to handle the anomalous declaration
15262 -- of Limited_Controlled, which is derived from a non-limited type, and
15263 -- which is handled specially elsewhere as well.
15265 elsif Chars (Parent_Subp) = Name_Op_Eq
15266 and then Is_Dispatching_Operation (Parent_Subp)
15267 and then Etype (Parent_Subp) = Standard_Boolean
15268 and then not Is_Limited_Type (Etype (First_Formal (Parent_Subp)))
15269 and then
15270 Etype (First_Formal (Parent_Subp)) =
15271 Etype (Next_Formal (First_Formal (Parent_Subp)))
15272 then
15273 Set_Derived_Name;
15275 -- If parent is hidden, this can be a regular derivation if the
15276 -- parent is immediately visible in a non-instantiating context,
15277 -- or if we are in the private part of an instance. This test
15278 -- should still be refined ???
15280 -- The test for In_Instance_Not_Visible avoids inheriting the derived
15281 -- operation as a non-visible operation in cases where the parent
15282 -- subprogram might not be visible now, but was visible within the
15283 -- original generic, so it would be wrong to make the inherited
15284 -- subprogram non-visible now. (Not clear if this test is fully
15285 -- correct; are there any cases where we should declare the inherited
15286 -- operation as not visible to avoid it being overridden, e.g., when
15287 -- the parent type is a generic actual with private primitives ???)
15289 -- (they should be treated the same as other private inherited
15290 -- subprograms, but it's not clear how to do this cleanly). ???
15292 elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
15293 and then Is_Immediately_Visible (Parent_Subp)
15294 and then not In_Instance)
15295 or else In_Instance_Not_Visible
15296 then
15297 Set_Derived_Name;
15299 -- Ada 2005 (AI-251): Regular derivation if the parent subprogram
15300 -- overrides an interface primitive because interface primitives
15301 -- must be visible in the partial view of the parent (RM 7.3 (7.3/2))
15303 elsif Ada_Version >= Ada_2005
15304 and then Is_Dispatching_Operation (Parent_Subp)
15305 and then Present (Covered_Interface_Op (Parent_Subp))
15306 then
15307 Set_Derived_Name;
15309 -- Otherwise, the type is inheriting a private operation, so enter it
15310 -- with a special name so it can't be overridden.
15312 else
15313 Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
15314 end if;
15316 Set_Parent (New_Subp, Parent (Derived_Type));
15318 if Present (Actual_Subp) then
15319 Replace_Type (Actual_Subp, New_Subp);
15320 else
15321 Replace_Type (Parent_Subp, New_Subp);
15322 end if;
15324 Conditional_Delay (New_Subp, Parent_Subp);
15326 -- If we are creating a renaming for a primitive operation of an
15327 -- actual of a generic derived type, we must examine the signature
15328 -- of the actual primitive, not that of the generic formal, which for
15329 -- example may be an interface. However the name and initial value
15330 -- of the inherited operation are those of the formal primitive.
15332 Formal := First_Formal (Parent_Subp);
15334 if Present (Actual_Subp) then
15335 Formal_Of_Actual := First_Formal (Actual_Subp);
15336 else
15337 Formal_Of_Actual := Empty;
15338 end if;
15340 while Present (Formal) loop
15341 New_Formal := New_Copy (Formal);
15343 -- Normally we do not go copying parents, but in the case of
15344 -- formals, we need to link up to the declaration (which is the
15345 -- parameter specification), and it is fine to link up to the
15346 -- original formal's parameter specification in this case.
15348 Set_Parent (New_Formal, Parent (Formal));
15349 Append_Entity (New_Formal, New_Subp);
15351 if Present (Formal_Of_Actual) then
15352 Replace_Type (Formal_Of_Actual, New_Formal);
15353 Next_Formal (Formal_Of_Actual);
15354 else
15355 Replace_Type (Formal, New_Formal);
15356 end if;
15358 Next_Formal (Formal);
15359 end loop;
15361 -- If this derivation corresponds to a tagged generic actual, then
15362 -- primitive operations rename those of the actual. Otherwise the
15363 -- primitive operations rename those of the parent type, If the parent
15364 -- renames an intrinsic operator, so does the new subprogram. We except
15365 -- concatenation, which is always properly typed, and does not get
15366 -- expanded as other intrinsic operations.
15368 if No (Actual_Subp) then
15369 if Is_Intrinsic_Subprogram (Parent_Subp) then
15370 Set_Is_Intrinsic_Subprogram (New_Subp);
15372 if Present (Alias (Parent_Subp))
15373 and then Chars (Parent_Subp) /= Name_Op_Concat
15374 then
15375 Set_Alias (New_Subp, Alias (Parent_Subp));
15376 else
15377 Set_Alias (New_Subp, Parent_Subp);
15378 end if;
15380 else
15381 Set_Alias (New_Subp, Parent_Subp);
15382 end if;
15384 else
15385 Set_Alias (New_Subp, Actual_Subp);
15386 end if;
15388 -- Derived subprograms of a tagged type must inherit the convention
15389 -- of the parent subprogram (a requirement of AI-117). Derived
15390 -- subprograms of untagged types simply get convention Ada by default.
15392 -- If the derived type is a tagged generic formal type with unknown
15393 -- discriminants, its convention is intrinsic (RM 6.3.1 (8)).
15395 -- However, if the type is derived from a generic formal, the further
15396 -- inherited subprogram has the convention of the non-generic ancestor.
15397 -- Otherwise there would be no way to override the operation.
15398 -- (This is subject to forthcoming ARG discussions).
15400 if Is_Tagged_Type (Derived_Type) then
15401 if Is_Generic_Type (Derived_Type)
15402 and then Has_Unknown_Discriminants (Derived_Type)
15403 then
15404 Set_Convention (New_Subp, Convention_Intrinsic);
15406 else
15407 if Is_Generic_Type (Parent_Type)
15408 and then Has_Unknown_Discriminants (Parent_Type)
15409 then
15410 Set_Convention (New_Subp, Convention (Alias (Parent_Subp)));
15411 else
15412 Set_Convention (New_Subp, Convention (Parent_Subp));
15413 end if;
15414 end if;
15415 end if;
15417 -- Predefined controlled operations retain their name even if the parent
15418 -- is hidden (see above), but they are not primitive operations if the
15419 -- ancestor is not visible, for example if the parent is a private
15420 -- extension completed with a controlled extension. Note that a full
15421 -- type that is controlled can break privacy: the flag Is_Controlled is
15422 -- set on both views of the type.
15424 if Is_Controlled (Parent_Type)
15425 and then Nam_In (Chars (Parent_Subp), Name_Initialize,
15426 Name_Adjust,
15427 Name_Finalize)
15428 and then Is_Hidden (Parent_Subp)
15429 and then not Is_Visibly_Controlled (Parent_Type)
15430 then
15431 Set_Is_Hidden (New_Subp);
15432 end if;
15434 Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
15435 Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
15437 if Ekind (Parent_Subp) = E_Procedure then
15438 Set_Is_Valued_Procedure
15439 (New_Subp, Is_Valued_Procedure (Parent_Subp));
15440 else
15441 Set_Has_Controlling_Result
15442 (New_Subp, Has_Controlling_Result (Parent_Subp));
15443 end if;
15445 -- No_Return must be inherited properly. If this is overridden in the
15446 -- case of a dispatching operation, then a check is made in Sem_Disp
15447 -- that the overriding operation is also No_Return (no such check is
15448 -- required for the case of non-dispatching operation.
15450 Set_No_Return (New_Subp, No_Return (Parent_Subp));
15452 -- A derived function with a controlling result is abstract. If the
15453 -- Derived_Type is a nonabstract formal generic derived type, then
15454 -- inherited operations are not abstract: the required check is done at
15455 -- instantiation time. If the derivation is for a generic actual, the
15456 -- function is not abstract unless the actual is.
15458 if Is_Generic_Type (Derived_Type)
15459 and then not Is_Abstract_Type (Derived_Type)
15460 then
15461 null;
15463 -- Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
15464 -- properties of the subprogram, as defined in RM-3.9.3(4/2-6/2).
15466 -- A subprogram subject to pragma Extensions_Visible with value False
15467 -- requires overriding if the subprogram has at least one controlling
15468 -- OUT parameter (SPARK RM 6.1.7(6)).
15470 elsif Ada_Version >= Ada_2005
15471 and then (Is_Abstract_Subprogram (Alias (New_Subp))
15472 or else (Is_Tagged_Type (Derived_Type)
15473 and then Etype (New_Subp) = Derived_Type
15474 and then not Is_Null_Extension (Derived_Type))
15475 or else (Is_Tagged_Type (Derived_Type)
15476 and then Ekind (Etype (New_Subp)) =
15477 E_Anonymous_Access_Type
15478 and then Designated_Type (Etype (New_Subp)) =
15479 Derived_Type
15480 and then not Is_Null_Extension (Derived_Type))
15481 or else (Comes_From_Source (Alias (New_Subp))
15482 and then Is_EVF_Procedure (Alias (New_Subp))))
15483 and then No (Actual_Subp)
15484 then
15485 if not Is_Tagged_Type (Derived_Type)
15486 or else Is_Abstract_Type (Derived_Type)
15487 or else Is_Abstract_Subprogram (Alias (New_Subp))
15488 then
15489 Set_Is_Abstract_Subprogram (New_Subp);
15490 else
15491 Set_Requires_Overriding (New_Subp);
15492 end if;
15494 elsif Ada_Version < Ada_2005
15495 and then (Is_Abstract_Subprogram (Alias (New_Subp))
15496 or else (Is_Tagged_Type (Derived_Type)
15497 and then Etype (New_Subp) = Derived_Type
15498 and then No (Actual_Subp)))
15499 then
15500 Set_Is_Abstract_Subprogram (New_Subp);
15502 -- AI05-0097 : an inherited operation that dispatches on result is
15503 -- abstract if the derived type is abstract, even if the parent type
15504 -- is concrete and the derived type is a null extension.
15506 elsif Has_Controlling_Result (Alias (New_Subp))
15507 and then Is_Abstract_Type (Etype (New_Subp))
15508 then
15509 Set_Is_Abstract_Subprogram (New_Subp);
15511 -- Finally, if the parent type is abstract we must verify that all
15512 -- inherited operations are either non-abstract or overridden, or that
15513 -- the derived type itself is abstract (this check is performed at the
15514 -- end of a package declaration, in Check_Abstract_Overriding). A
15515 -- private overriding in the parent type will not be visible in the
15516 -- derivation if we are not in an inner package or in a child unit of
15517 -- the parent type, in which case the abstractness of the inherited
15518 -- operation is carried to the new subprogram.
15520 elsif Is_Abstract_Type (Parent_Type)
15521 and then not In_Open_Scopes (Scope (Parent_Type))
15522 and then Is_Private_Overriding
15523 and then Is_Abstract_Subprogram (Visible_Subp)
15524 then
15525 if No (Actual_Subp) then
15526 Set_Alias (New_Subp, Visible_Subp);
15527 Set_Is_Abstract_Subprogram (New_Subp, True);
15529 else
15530 -- If this is a derivation for an instance of a formal derived
15531 -- type, abstractness comes from the primitive operation of the
15532 -- actual, not from the operation inherited from the ancestor.
15534 Set_Is_Abstract_Subprogram
15535 (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
15536 end if;
15537 end if;
15539 New_Overloaded_Entity (New_Subp, Derived_Type);
15541 -- Ada RM 6.1.1 (15): If a subprogram inherits nonconforming class-wide
15542 -- preconditions and the derived type is abstract, the derived operation
15543 -- is abstract as well if parent subprogram is not abstract or null.
15545 if Is_Abstract_Type (Derived_Type)
15546 and then Has_Non_Trivial_Precondition (Parent_Subp)
15547 and then Present (Interfaces (Derived_Type))
15548 then
15550 -- Add useful attributes of subprogram before the freeze point,
15551 -- in case freezing is delayed or there are previous errors.
15553 Set_Is_Dispatching_Operation (New_Subp);
15555 declare
15556 Iface_Prim : constant Entity_Id := Covered_Interface_Op (New_Subp);
15558 begin
15559 if Present (Iface_Prim)
15560 and then Has_Non_Trivial_Precondition (Iface_Prim)
15561 then
15562 Set_Is_Abstract_Subprogram (New_Subp);
15563 end if;
15564 end;
15565 end if;
15567 -- Check for case of a derived subprogram for the instantiation of a
15568 -- formal derived tagged type, if so mark the subprogram as dispatching
15569 -- and inherit the dispatching attributes of the actual subprogram. The
15570 -- derived subprogram is effectively renaming of the actual subprogram,
15571 -- so it needs to have the same attributes as the actual.
15573 if Present (Actual_Subp)
15574 and then Is_Dispatching_Operation (Actual_Subp)
15575 then
15576 Set_Is_Dispatching_Operation (New_Subp);
15578 if Present (DTC_Entity (Actual_Subp)) then
15579 Set_DTC_Entity (New_Subp, DTC_Entity (Actual_Subp));
15580 Set_DT_Position_Value (New_Subp, DT_Position (Actual_Subp));
15581 end if;
15582 end if;
15584 -- Indicate that a derived subprogram does not require a body and that
15585 -- it does not require processing of default expressions.
15587 Set_Has_Completion (New_Subp);
15588 Set_Default_Expressions_Processed (New_Subp);
15590 if Ekind (New_Subp) = E_Function then
15591 Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
15592 end if;
15593 end Derive_Subprogram;
15595 ------------------------
15596 -- Derive_Subprograms --
15597 ------------------------
15599 procedure Derive_Subprograms
15600 (Parent_Type : Entity_Id;
15601 Derived_Type : Entity_Id;
15602 Generic_Actual : Entity_Id := Empty)
15604 Op_List : constant Elist_Id :=
15605 Collect_Primitive_Operations (Parent_Type);
15607 function Check_Derived_Type return Boolean;
15608 -- Check that all the entities derived from Parent_Type are found in
15609 -- the list of primitives of Derived_Type exactly in the same order.
15611 procedure Derive_Interface_Subprogram
15612 (New_Subp : out Entity_Id;
15613 Subp : Entity_Id;
15614 Actual_Subp : Entity_Id);
15615 -- Derive New_Subp from the ultimate alias of the parent subprogram Subp
15616 -- (which is an interface primitive). If Generic_Actual is present then
15617 -- Actual_Subp is the actual subprogram corresponding with the generic
15618 -- subprogram Subp.
15620 ------------------------
15621 -- Check_Derived_Type --
15622 ------------------------
15624 function Check_Derived_Type return Boolean is
15625 E : Entity_Id;
15626 Elmt : Elmt_Id;
15627 List : Elist_Id;
15628 New_Subp : Entity_Id;
15629 Op_Elmt : Elmt_Id;
15630 Subp : Entity_Id;
15632 begin
15633 -- Traverse list of entities in the current scope searching for
15634 -- an incomplete type whose full-view is derived type.
15636 E := First_Entity (Scope (Derived_Type));
15637 while Present (E) and then E /= Derived_Type loop
15638 if Ekind (E) = E_Incomplete_Type
15639 and then Present (Full_View (E))
15640 and then Full_View (E) = Derived_Type
15641 then
15642 -- Disable this test if Derived_Type completes an incomplete
15643 -- type because in such case more primitives can be added
15644 -- later to the list of primitives of Derived_Type by routine
15645 -- Process_Incomplete_Dependents
15647 return True;
15648 end if;
15650 E := Next_Entity (E);
15651 end loop;
15653 List := Collect_Primitive_Operations (Derived_Type);
15654 Elmt := First_Elmt (List);
15656 Op_Elmt := First_Elmt (Op_List);
15657 while Present (Op_Elmt) loop
15658 Subp := Node (Op_Elmt);
15659 New_Subp := Node (Elmt);
15661 -- At this early stage Derived_Type has no entities with attribute
15662 -- Interface_Alias. In addition, such primitives are always
15663 -- located at the end of the list of primitives of Parent_Type.
15664 -- Therefore, if found we can safely stop processing pending
15665 -- entities.
15667 exit when Present (Interface_Alias (Subp));
15669 -- Handle hidden entities
15671 if not Is_Predefined_Dispatching_Operation (Subp)
15672 and then Is_Hidden (Subp)
15673 then
15674 if Present (New_Subp)
15675 and then Primitive_Names_Match (Subp, New_Subp)
15676 then
15677 Next_Elmt (Elmt);
15678 end if;
15680 else
15681 if not Present (New_Subp)
15682 or else Ekind (Subp) /= Ekind (New_Subp)
15683 or else not Primitive_Names_Match (Subp, New_Subp)
15684 then
15685 return False;
15686 end if;
15688 Next_Elmt (Elmt);
15689 end if;
15691 Next_Elmt (Op_Elmt);
15692 end loop;
15694 return True;
15695 end Check_Derived_Type;
15697 ---------------------------------
15698 -- Derive_Interface_Subprogram --
15699 ---------------------------------
15701 procedure Derive_Interface_Subprogram
15702 (New_Subp : out Entity_Id;
15703 Subp : Entity_Id;
15704 Actual_Subp : Entity_Id)
15706 Iface_Subp : constant Entity_Id := Ultimate_Alias (Subp);
15707 Iface_Type : constant Entity_Id := Find_Dispatching_Type (Iface_Subp);
15709 begin
15710 pragma Assert (Is_Interface (Iface_Type));
15712 Derive_Subprogram
15713 (New_Subp => New_Subp,
15714 Parent_Subp => Iface_Subp,
15715 Derived_Type => Derived_Type,
15716 Parent_Type => Iface_Type,
15717 Actual_Subp => Actual_Subp);
15719 -- Given that this new interface entity corresponds with a primitive
15720 -- of the parent that was not overridden we must leave it associated
15721 -- with its parent primitive to ensure that it will share the same
15722 -- dispatch table slot when overridden. We must set the Alias to Subp
15723 -- (instead of Iface_Subp), and we must fix Is_Abstract_Subprogram
15724 -- (in case we inherited Subp from Iface_Type via a nonabstract
15725 -- generic formal type).
15727 if No (Actual_Subp) then
15728 Set_Alias (New_Subp, Subp);
15730 declare
15731 T : Entity_Id := Find_Dispatching_Type (Subp);
15732 begin
15733 while Etype (T) /= T loop
15734 if Is_Generic_Type (T) and then not Is_Abstract_Type (T) then
15735 Set_Is_Abstract_Subprogram (New_Subp, False);
15736 exit;
15737 end if;
15739 T := Etype (T);
15740 end loop;
15741 end;
15743 -- For instantiations this is not needed since the previous call to
15744 -- Derive_Subprogram leaves the entity well decorated.
15746 else
15747 pragma Assert (Alias (New_Subp) = Actual_Subp);
15748 null;
15749 end if;
15750 end Derive_Interface_Subprogram;
15752 -- Local variables
15754 Alias_Subp : Entity_Id;
15755 Act_List : Elist_Id;
15756 Act_Elmt : Elmt_Id;
15757 Act_Subp : Entity_Id := Empty;
15758 Elmt : Elmt_Id;
15759 Need_Search : Boolean := False;
15760 New_Subp : Entity_Id := Empty;
15761 Parent_Base : Entity_Id;
15762 Subp : Entity_Id;
15764 -- Start of processing for Derive_Subprograms
15766 begin
15767 if Ekind (Parent_Type) = E_Record_Type_With_Private
15768 and then Has_Discriminants (Parent_Type)
15769 and then Present (Full_View (Parent_Type))
15770 then
15771 Parent_Base := Full_View (Parent_Type);
15772 else
15773 Parent_Base := Parent_Type;
15774 end if;
15776 if Present (Generic_Actual) then
15777 Act_List := Collect_Primitive_Operations (Generic_Actual);
15778 Act_Elmt := First_Elmt (Act_List);
15779 else
15780 Act_List := No_Elist;
15781 Act_Elmt := No_Elmt;
15782 end if;
15784 -- Derive primitives inherited from the parent. Note that if the generic
15785 -- actual is present, this is not really a type derivation, it is a
15786 -- completion within an instance.
15788 -- Case 1: Derived_Type does not implement interfaces
15790 if not Is_Tagged_Type (Derived_Type)
15791 or else (not Has_Interfaces (Derived_Type)
15792 and then not (Present (Generic_Actual)
15793 and then Has_Interfaces (Generic_Actual)))
15794 then
15795 Elmt := First_Elmt (Op_List);
15796 while Present (Elmt) loop
15797 Subp := Node (Elmt);
15799 -- Literals are derived earlier in the process of building the
15800 -- derived type, and are skipped here.
15802 if Ekind (Subp) = E_Enumeration_Literal then
15803 null;
15805 -- The actual is a direct descendant and the common primitive
15806 -- operations appear in the same order.
15808 -- If the generic parent type is present, the derived type is an
15809 -- instance of a formal derived type, and within the instance its
15810 -- operations are those of the actual. We derive from the formal
15811 -- type but make the inherited operations aliases of the
15812 -- corresponding operations of the actual.
15814 else
15815 pragma Assert (No (Node (Act_Elmt))
15816 or else (Primitive_Names_Match (Subp, Node (Act_Elmt))
15817 and then
15818 Type_Conformant
15819 (Subp, Node (Act_Elmt),
15820 Skip_Controlling_Formals => True)));
15822 Derive_Subprogram
15823 (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
15825 if Present (Act_Elmt) then
15826 Next_Elmt (Act_Elmt);
15827 end if;
15828 end if;
15830 Next_Elmt (Elmt);
15831 end loop;
15833 -- Case 2: Derived_Type implements interfaces
15835 else
15836 -- If the parent type has no predefined primitives we remove
15837 -- predefined primitives from the list of primitives of generic
15838 -- actual to simplify the complexity of this algorithm.
15840 if Present (Generic_Actual) then
15841 declare
15842 Has_Predefined_Primitives : Boolean := False;
15844 begin
15845 -- Check if the parent type has predefined primitives
15847 Elmt := First_Elmt (Op_List);
15848 while Present (Elmt) loop
15849 Subp := Node (Elmt);
15851 if Is_Predefined_Dispatching_Operation (Subp)
15852 and then not Comes_From_Source (Ultimate_Alias (Subp))
15853 then
15854 Has_Predefined_Primitives := True;
15855 exit;
15856 end if;
15858 Next_Elmt (Elmt);
15859 end loop;
15861 -- Remove predefined primitives of Generic_Actual. We must use
15862 -- an auxiliary list because in case of tagged types the value
15863 -- returned by Collect_Primitive_Operations is the value stored
15864 -- in its Primitive_Operations attribute (and we don't want to
15865 -- modify its current contents).
15867 if not Has_Predefined_Primitives then
15868 declare
15869 Aux_List : constant Elist_Id := New_Elmt_List;
15871 begin
15872 Elmt := First_Elmt (Act_List);
15873 while Present (Elmt) loop
15874 Subp := Node (Elmt);
15876 if not Is_Predefined_Dispatching_Operation (Subp)
15877 or else Comes_From_Source (Subp)
15878 then
15879 Append_Elmt (Subp, Aux_List);
15880 end if;
15882 Next_Elmt (Elmt);
15883 end loop;
15885 Act_List := Aux_List;
15886 end;
15887 end if;
15889 Act_Elmt := First_Elmt (Act_List);
15890 Act_Subp := Node (Act_Elmt);
15891 end;
15892 end if;
15894 -- Stage 1: If the generic actual is not present we derive the
15895 -- primitives inherited from the parent type. If the generic parent
15896 -- type is present, the derived type is an instance of a formal
15897 -- derived type, and within the instance its operations are those of
15898 -- the actual. We derive from the formal type but make the inherited
15899 -- operations aliases of the corresponding operations of the actual.
15901 Elmt := First_Elmt (Op_List);
15902 while Present (Elmt) loop
15903 Subp := Node (Elmt);
15904 Alias_Subp := Ultimate_Alias (Subp);
15906 -- Do not derive internal entities of the parent that link
15907 -- interface primitives with their covering primitive. These
15908 -- entities will be added to this type when frozen.
15910 if Present (Interface_Alias (Subp)) then
15911 goto Continue;
15912 end if;
15914 -- If the generic actual is present find the corresponding
15915 -- operation in the generic actual. If the parent type is a
15916 -- direct ancestor of the derived type then, even if it is an
15917 -- interface, the operations are inherited from the primary
15918 -- dispatch table and are in the proper order. If we detect here
15919 -- that primitives are not in the same order we traverse the list
15920 -- of primitive operations of the actual to find the one that
15921 -- implements the interface primitive.
15923 if Need_Search
15924 or else
15925 (Present (Generic_Actual)
15926 and then Present (Act_Subp)
15927 and then not
15928 (Primitive_Names_Match (Subp, Act_Subp)
15929 and then
15930 Type_Conformant (Subp, Act_Subp,
15931 Skip_Controlling_Formals => True)))
15932 then
15933 pragma Assert (not Is_Ancestor (Parent_Base, Generic_Actual,
15934 Use_Full_View => True));
15936 -- Remember that we need searching for all pending primitives
15938 Need_Search := True;
15940 -- Handle entities associated with interface primitives
15942 if Present (Alias_Subp)
15943 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
15944 and then not Is_Predefined_Dispatching_Operation (Subp)
15945 then
15946 -- Search for the primitive in the homonym chain
15948 Act_Subp :=
15949 Find_Primitive_Covering_Interface
15950 (Tagged_Type => Generic_Actual,
15951 Iface_Prim => Alias_Subp);
15953 -- Previous search may not locate primitives covering
15954 -- interfaces defined in generics units or instantiations.
15955 -- (it fails if the covering primitive has formals whose
15956 -- type is also defined in generics or instantiations).
15957 -- In such case we search in the list of primitives of the
15958 -- generic actual for the internal entity that links the
15959 -- interface primitive and the covering primitive.
15961 if No (Act_Subp)
15962 and then Is_Generic_Type (Parent_Type)
15963 then
15964 -- This code has been designed to handle only generic
15965 -- formals that implement interfaces that are defined
15966 -- in a generic unit or instantiation. If this code is
15967 -- needed for other cases we must review it because
15968 -- (given that it relies on Original_Location to locate
15969 -- the primitive of Generic_Actual that covers the
15970 -- interface) it could leave linked through attribute
15971 -- Alias entities of unrelated instantiations).
15973 pragma Assert
15974 (Is_Generic_Unit
15975 (Scope (Find_Dispatching_Type (Alias_Subp)))
15976 or else
15977 Instantiation_Depth
15978 (Sloc (Find_Dispatching_Type (Alias_Subp))) > 0);
15980 declare
15981 Iface_Prim_Loc : constant Source_Ptr :=
15982 Original_Location (Sloc (Alias_Subp));
15984 Elmt : Elmt_Id;
15985 Prim : Entity_Id;
15987 begin
15988 Elmt :=
15989 First_Elmt (Primitive_Operations (Generic_Actual));
15991 Search : while Present (Elmt) loop
15992 Prim := Node (Elmt);
15994 if Present (Interface_Alias (Prim))
15995 and then Original_Location
15996 (Sloc (Interface_Alias (Prim))) =
15997 Iface_Prim_Loc
15998 then
15999 Act_Subp := Alias (Prim);
16000 exit Search;
16001 end if;
16003 Next_Elmt (Elmt);
16004 end loop Search;
16005 end;
16006 end if;
16008 pragma Assert (Present (Act_Subp)
16009 or else Is_Abstract_Type (Generic_Actual)
16010 or else Serious_Errors_Detected > 0);
16012 -- Handle predefined primitives plus the rest of user-defined
16013 -- primitives
16015 else
16016 Act_Elmt := First_Elmt (Act_List);
16017 while Present (Act_Elmt) loop
16018 Act_Subp := Node (Act_Elmt);
16020 exit when Primitive_Names_Match (Subp, Act_Subp)
16021 and then Type_Conformant
16022 (Subp, Act_Subp,
16023 Skip_Controlling_Formals => True)
16024 and then No (Interface_Alias (Act_Subp));
16026 Next_Elmt (Act_Elmt);
16027 end loop;
16029 if No (Act_Elmt) then
16030 Act_Subp := Empty;
16031 end if;
16032 end if;
16033 end if;
16035 -- Case 1: If the parent is a limited interface then it has the
16036 -- predefined primitives of synchronized interfaces. However, the
16037 -- actual type may be a non-limited type and hence it does not
16038 -- have such primitives.
16040 if Present (Generic_Actual)
16041 and then not Present (Act_Subp)
16042 and then Is_Limited_Interface (Parent_Base)
16043 and then Is_Predefined_Interface_Primitive (Subp)
16044 then
16045 null;
16047 -- Case 2: Inherit entities associated with interfaces that were
16048 -- not covered by the parent type. We exclude here null interface
16049 -- primitives because they do not need special management.
16051 -- We also exclude interface operations that are renamings. If the
16052 -- subprogram is an explicit renaming of an interface primitive,
16053 -- it is a regular primitive operation, and the presence of its
16054 -- alias is not relevant: it has to be derived like any other
16055 -- primitive.
16057 elsif Present (Alias (Subp))
16058 and then Nkind (Unit_Declaration_Node (Subp)) /=
16059 N_Subprogram_Renaming_Declaration
16060 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
16061 and then not
16062 (Nkind (Parent (Alias_Subp)) = N_Procedure_Specification
16063 and then Null_Present (Parent (Alias_Subp)))
16064 then
16065 -- If this is an abstract private type then we transfer the
16066 -- derivation of the interface primitive from the partial view
16067 -- to the full view. This is safe because all the interfaces
16068 -- must be visible in the partial view. Done to avoid adding
16069 -- a new interface derivation to the private part of the
16070 -- enclosing package; otherwise this new derivation would be
16071 -- decorated as hidden when the analysis of the enclosing
16072 -- package completes.
16074 if Is_Abstract_Type (Derived_Type)
16075 and then In_Private_Part (Current_Scope)
16076 and then Has_Private_Declaration (Derived_Type)
16077 then
16078 declare
16079 Partial_View : Entity_Id;
16080 Elmt : Elmt_Id;
16081 Ent : Entity_Id;
16083 begin
16084 Partial_View := First_Entity (Current_Scope);
16085 loop
16086 exit when No (Partial_View)
16087 or else (Has_Private_Declaration (Partial_View)
16088 and then
16089 Full_View (Partial_View) = Derived_Type);
16091 Next_Entity (Partial_View);
16092 end loop;
16094 -- If the partial view was not found then the source code
16095 -- has errors and the derivation is not needed.
16097 if Present (Partial_View) then
16098 Elmt :=
16099 First_Elmt (Primitive_Operations (Partial_View));
16100 while Present (Elmt) loop
16101 Ent := Node (Elmt);
16103 if Present (Alias (Ent))
16104 and then Ultimate_Alias (Ent) = Alias (Subp)
16105 then
16106 Append_Elmt
16107 (Ent, Primitive_Operations (Derived_Type));
16108 exit;
16109 end if;
16111 Next_Elmt (Elmt);
16112 end loop;
16114 -- If the interface primitive was not found in the
16115 -- partial view then this interface primitive was
16116 -- overridden. We add a derivation to activate in
16117 -- Derive_Progenitor_Subprograms the machinery to
16118 -- search for it.
16120 if No (Elmt) then
16121 Derive_Interface_Subprogram
16122 (New_Subp => New_Subp,
16123 Subp => Subp,
16124 Actual_Subp => Act_Subp);
16125 end if;
16126 end if;
16127 end;
16128 else
16129 Derive_Interface_Subprogram
16130 (New_Subp => New_Subp,
16131 Subp => Subp,
16132 Actual_Subp => Act_Subp);
16133 end if;
16135 -- Case 3: Common derivation
16137 else
16138 Derive_Subprogram
16139 (New_Subp => New_Subp,
16140 Parent_Subp => Subp,
16141 Derived_Type => Derived_Type,
16142 Parent_Type => Parent_Base,
16143 Actual_Subp => Act_Subp);
16144 end if;
16146 -- No need to update Act_Elm if we must search for the
16147 -- corresponding operation in the generic actual
16149 if not Need_Search
16150 and then Present (Act_Elmt)
16151 then
16152 Next_Elmt (Act_Elmt);
16153 Act_Subp := Node (Act_Elmt);
16154 end if;
16156 <<Continue>>
16157 Next_Elmt (Elmt);
16158 end loop;
16160 -- Inherit additional operations from progenitors. If the derived
16161 -- type is a generic actual, there are not new primitive operations
16162 -- for the type because it has those of the actual, and therefore
16163 -- nothing needs to be done. The renamings generated above are not
16164 -- primitive operations, and their purpose is simply to make the
16165 -- proper operations visible within an instantiation.
16167 if No (Generic_Actual) then
16168 Derive_Progenitor_Subprograms (Parent_Base, Derived_Type);
16169 end if;
16170 end if;
16172 -- Final check: Direct descendants must have their primitives in the
16173 -- same order. We exclude from this test untagged types and instances
16174 -- of formal derived types. We skip this test if we have already
16175 -- reported serious errors in the sources.
16177 pragma Assert (not Is_Tagged_Type (Derived_Type)
16178 or else Present (Generic_Actual)
16179 or else Serious_Errors_Detected > 0
16180 or else Check_Derived_Type);
16181 end Derive_Subprograms;
16183 --------------------------------
16184 -- Derived_Standard_Character --
16185 --------------------------------
16187 procedure Derived_Standard_Character
16188 (N : Node_Id;
16189 Parent_Type : Entity_Id;
16190 Derived_Type : Entity_Id)
16192 Loc : constant Source_Ptr := Sloc (N);
16193 Def : constant Node_Id := Type_Definition (N);
16194 Indic : constant Node_Id := Subtype_Indication (Def);
16195 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
16196 Implicit_Base : constant Entity_Id :=
16197 Create_Itype
16198 (E_Enumeration_Type, N, Derived_Type, 'B');
16200 Lo : Node_Id;
16201 Hi : Node_Id;
16203 begin
16204 Discard_Node (Process_Subtype (Indic, N));
16206 Set_Etype (Implicit_Base, Parent_Base);
16207 Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
16208 Set_RM_Size (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
16210 Set_Is_Character_Type (Implicit_Base, True);
16211 Set_Has_Delayed_Freeze (Implicit_Base);
16213 -- The bounds of the implicit base are the bounds of the parent base.
16214 -- Note that their type is the parent base.
16216 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
16217 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
16219 Set_Scalar_Range (Implicit_Base,
16220 Make_Range (Loc,
16221 Low_Bound => Lo,
16222 High_Bound => Hi));
16224 Conditional_Delay (Derived_Type, Parent_Type);
16226 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
16227 Set_Etype (Derived_Type, Implicit_Base);
16228 Set_Size_Info (Derived_Type, Parent_Type);
16230 if Unknown_RM_Size (Derived_Type) then
16231 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
16232 end if;
16234 Set_Is_Character_Type (Derived_Type, True);
16236 if Nkind (Indic) /= N_Subtype_Indication then
16238 -- If no explicit constraint, the bounds are those
16239 -- of the parent type.
16241 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Type));
16242 Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
16243 Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
16244 end if;
16246 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
16248 -- Because the implicit base is used in the conversion of the bounds, we
16249 -- have to freeze it now. This is similar to what is done for numeric
16250 -- types, and it equally suspicious, but otherwise a non-static bound
16251 -- will have a reference to an unfrozen type, which is rejected by Gigi
16252 -- (???). This requires specific care for definition of stream
16253 -- attributes. For details, see comments at the end of
16254 -- Build_Derived_Numeric_Type.
16256 Freeze_Before (N, Implicit_Base);
16257 end Derived_Standard_Character;
16259 ------------------------------
16260 -- Derived_Type_Declaration --
16261 ------------------------------
16263 procedure Derived_Type_Declaration
16264 (T : Entity_Id;
16265 N : Node_Id;
16266 Is_Completion : Boolean)
16268 Parent_Type : Entity_Id;
16270 function Comes_From_Generic (Typ : Entity_Id) return Boolean;
16271 -- Check whether the parent type is a generic formal, or derives
16272 -- directly or indirectly from one.
16274 ------------------------
16275 -- Comes_From_Generic --
16276 ------------------------
16278 function Comes_From_Generic (Typ : Entity_Id) return Boolean is
16279 begin
16280 if Is_Generic_Type (Typ) then
16281 return True;
16283 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
16284 return True;
16286 elsif Is_Private_Type (Typ)
16287 and then Present (Full_View (Typ))
16288 and then Is_Generic_Type (Root_Type (Full_View (Typ)))
16289 then
16290 return True;
16292 elsif Is_Generic_Actual_Type (Typ) then
16293 return True;
16295 else
16296 return False;
16297 end if;
16298 end Comes_From_Generic;
16300 -- Local variables
16302 Def : constant Node_Id := Type_Definition (N);
16303 Iface_Def : Node_Id;
16304 Indic : constant Node_Id := Subtype_Indication (Def);
16305 Extension : constant Node_Id := Record_Extension_Part (Def);
16306 Parent_Node : Node_Id;
16307 Taggd : Boolean;
16309 -- Start of processing for Derived_Type_Declaration
16311 begin
16312 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
16314 if SPARK_Mode = On
16315 and then Is_Tagged_Type (Parent_Type)
16316 then
16317 declare
16318 Partial_View : constant Entity_Id :=
16319 Incomplete_Or_Partial_View (Parent_Type);
16321 begin
16322 -- If the partial view was not found then the parent type is not
16323 -- a private type. Otherwise check if the partial view is a tagged
16324 -- private type.
16326 if Present (Partial_View)
16327 and then Is_Private_Type (Partial_View)
16328 and then not Is_Tagged_Type (Partial_View)
16329 then
16330 Error_Msg_NE
16331 ("cannot derive from & declared as untagged private "
16332 & "(SPARK RM 3.4(1))", N, Partial_View);
16333 end if;
16334 end;
16335 end if;
16337 -- Ada 2005 (AI-251): In case of interface derivation check that the
16338 -- parent is also an interface.
16340 if Interface_Present (Def) then
16341 Check_SPARK_05_Restriction ("interface is not allowed", Def);
16343 if not Is_Interface (Parent_Type) then
16344 Diagnose_Interface (Indic, Parent_Type);
16346 else
16347 Parent_Node := Parent (Base_Type (Parent_Type));
16348 Iface_Def := Type_Definition (Parent_Node);
16350 -- Ada 2005 (AI-251): Limited interfaces can only inherit from
16351 -- other limited interfaces.
16353 if Limited_Present (Def) then
16354 if Limited_Present (Iface_Def) then
16355 null;
16357 elsif Protected_Present (Iface_Def) then
16358 Error_Msg_NE
16359 ("descendant of & must be declared as a protected "
16360 & "interface", N, Parent_Type);
16362 elsif Synchronized_Present (Iface_Def) then
16363 Error_Msg_NE
16364 ("descendant of & must be declared as a synchronized "
16365 & "interface", N, Parent_Type);
16367 elsif Task_Present (Iface_Def) then
16368 Error_Msg_NE
16369 ("descendant of & must be declared as a task interface",
16370 N, Parent_Type);
16372 else
16373 Error_Msg_N
16374 ("(Ada 2005) limited interface cannot inherit from "
16375 & "non-limited interface", Indic);
16376 end if;
16378 -- Ada 2005 (AI-345): Non-limited interfaces can only inherit
16379 -- from non-limited or limited interfaces.
16381 elsif not Protected_Present (Def)
16382 and then not Synchronized_Present (Def)
16383 and then not Task_Present (Def)
16384 then
16385 if Limited_Present (Iface_Def) then
16386 null;
16388 elsif Protected_Present (Iface_Def) then
16389 Error_Msg_NE
16390 ("descendant of & must be declared as a protected "
16391 & "interface", N, Parent_Type);
16393 elsif Synchronized_Present (Iface_Def) then
16394 Error_Msg_NE
16395 ("descendant of & must be declared as a synchronized "
16396 & "interface", N, Parent_Type);
16398 elsif Task_Present (Iface_Def) then
16399 Error_Msg_NE
16400 ("descendant of & must be declared as a task interface",
16401 N, Parent_Type);
16402 else
16403 null;
16404 end if;
16405 end if;
16406 end if;
16407 end if;
16409 if Is_Tagged_Type (Parent_Type)
16410 and then Is_Concurrent_Type (Parent_Type)
16411 and then not Is_Interface (Parent_Type)
16412 then
16413 Error_Msg_N
16414 ("parent type of a record extension cannot be a synchronized "
16415 & "tagged type (RM 3.9.1 (3/1))", N);
16416 Set_Etype (T, Any_Type);
16417 return;
16418 end if;
16420 -- Ada 2005 (AI-251): Decorate all the names in the list of ancestor
16421 -- interfaces
16423 if Is_Tagged_Type (Parent_Type)
16424 and then Is_Non_Empty_List (Interface_List (Def))
16425 then
16426 declare
16427 Intf : Node_Id;
16428 T : Entity_Id;
16430 begin
16431 Intf := First (Interface_List (Def));
16432 while Present (Intf) loop
16433 T := Find_Type_Of_Subtype_Indic (Intf);
16435 if not Is_Interface (T) then
16436 Diagnose_Interface (Intf, T);
16438 -- Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
16439 -- a limited type from having a nonlimited progenitor.
16441 elsif (Limited_Present (Def)
16442 or else (not Is_Interface (Parent_Type)
16443 and then Is_Limited_Type (Parent_Type)))
16444 and then not Is_Limited_Interface (T)
16445 then
16446 Error_Msg_NE
16447 ("progenitor interface& of limited type must be limited",
16448 N, T);
16449 end if;
16451 Next (Intf);
16452 end loop;
16453 end;
16454 end if;
16456 if Parent_Type = Any_Type
16457 or else Etype (Parent_Type) = Any_Type
16458 or else (Is_Class_Wide_Type (Parent_Type)
16459 and then Etype (Parent_Type) = T)
16460 then
16461 -- If Parent_Type is undefined or illegal, make new type into a
16462 -- subtype of Any_Type, and set a few attributes to prevent cascaded
16463 -- errors. If this is a self-definition, emit error now.
16465 if T = Parent_Type or else T = Etype (Parent_Type) then
16466 Error_Msg_N ("type cannot be used in its own definition", Indic);
16467 end if;
16469 Set_Ekind (T, Ekind (Parent_Type));
16470 Set_Etype (T, Any_Type);
16471 Set_Scalar_Range (T, Scalar_Range (Any_Type));
16473 if Is_Tagged_Type (T)
16474 and then Is_Record_Type (T)
16475 then
16476 Set_Direct_Primitive_Operations (T, New_Elmt_List);
16477 end if;
16479 return;
16480 end if;
16482 -- Ada 2005 (AI-251): The case in which the parent of the full-view is
16483 -- an interface is special because the list of interfaces in the full
16484 -- view can be given in any order. For example:
16486 -- type A is interface;
16487 -- type B is interface and A;
16488 -- type D is new B with private;
16489 -- private
16490 -- type D is new A and B with null record; -- 1 --
16492 -- In this case we perform the following transformation of -1-:
16494 -- type D is new B and A with null record;
16496 -- If the parent of the full-view covers the parent of the partial-view
16497 -- we have two possible cases:
16499 -- 1) They have the same parent
16500 -- 2) The parent of the full-view implements some further interfaces
16502 -- In both cases we do not need to perform the transformation. In the
16503 -- first case the source program is correct and the transformation is
16504 -- not needed; in the second case the source program does not fulfill
16505 -- the no-hidden interfaces rule (AI-396) and the error will be reported
16506 -- later.
16508 -- This transformation not only simplifies the rest of the analysis of
16509 -- this type declaration but also simplifies the correct generation of
16510 -- the object layout to the expander.
16512 if In_Private_Part (Current_Scope)
16513 and then Is_Interface (Parent_Type)
16514 then
16515 declare
16516 Iface : Node_Id;
16517 Partial_View : Entity_Id;
16518 Partial_View_Parent : Entity_Id;
16519 New_Iface : Node_Id;
16521 begin
16522 -- Look for the associated private type declaration
16524 Partial_View := Incomplete_Or_Partial_View (T);
16526 -- If the partial view was not found then the source code has
16527 -- errors and the transformation is not needed.
16529 if Present (Partial_View) then
16530 Partial_View_Parent := Etype (Partial_View);
16532 -- If the parent of the full-view covers the parent of the
16533 -- partial-view we have nothing else to do.
16535 if Interface_Present_In_Ancestor
16536 (Parent_Type, Partial_View_Parent)
16537 then
16538 null;
16540 -- Traverse the list of interfaces of the full-view to look
16541 -- for the parent of the partial-view and perform the tree
16542 -- transformation.
16544 else
16545 Iface := First (Interface_List (Def));
16546 while Present (Iface) loop
16547 if Etype (Iface) = Etype (Partial_View) then
16548 Rewrite (Subtype_Indication (Def),
16549 New_Copy (Subtype_Indication
16550 (Parent (Partial_View))));
16552 New_Iface :=
16553 Make_Identifier (Sloc (N), Chars (Parent_Type));
16554 Append (New_Iface, Interface_List (Def));
16556 -- Analyze the transformed code
16558 Derived_Type_Declaration (T, N, Is_Completion);
16559 return;
16560 end if;
16562 Next (Iface);
16563 end loop;
16564 end if;
16565 end if;
16566 end;
16567 end if;
16569 -- Only composite types other than array types are allowed to have
16570 -- discriminants.
16572 if Present (Discriminant_Specifications (N)) then
16573 if (Is_Elementary_Type (Parent_Type)
16574 or else
16575 Is_Array_Type (Parent_Type))
16576 and then not Error_Posted (N)
16577 then
16578 Error_Msg_N
16579 ("elementary or array type cannot have discriminants",
16580 Defining_Identifier (First (Discriminant_Specifications (N))));
16581 Set_Has_Discriminants (T, False);
16583 -- The type is allowed to have discriminants
16585 else
16586 Check_SPARK_05_Restriction ("discriminant type is not allowed", N);
16587 end if;
16588 end if;
16590 -- In Ada 83, a derived type defined in a package specification cannot
16591 -- be used for further derivation until the end of its visible part.
16592 -- Note that derivation in the private part of the package is allowed.
16594 if Ada_Version = Ada_83
16595 and then Is_Derived_Type (Parent_Type)
16596 and then In_Visible_Part (Scope (Parent_Type))
16597 then
16598 if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
16599 Error_Msg_N
16600 ("(Ada 83): premature use of type for derivation", Indic);
16601 end if;
16602 end if;
16604 -- Check for early use of incomplete or private type
16606 if Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
16607 Error_Msg_N ("premature derivation of incomplete type", Indic);
16608 return;
16610 elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
16611 and then not Comes_From_Generic (Parent_Type))
16612 or else Has_Private_Component (Parent_Type)
16613 then
16614 -- The ancestor type of a formal type can be incomplete, in which
16615 -- case only the operations of the partial view are available in the
16616 -- generic. Subsequent checks may be required when the full view is
16617 -- analyzed to verify that a derivation from a tagged type has an
16618 -- extension.
16620 if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
16621 null;
16623 elsif No (Underlying_Type (Parent_Type))
16624 or else Has_Private_Component (Parent_Type)
16625 then
16626 Error_Msg_N
16627 ("premature derivation of derived or private type", Indic);
16629 -- Flag the type itself as being in error, this prevents some
16630 -- nasty problems with subsequent uses of the malformed type.
16632 Set_Error_Posted (T);
16634 -- Check that within the immediate scope of an untagged partial
16635 -- view it's illegal to derive from the partial view if the
16636 -- full view is tagged. (7.3(7))
16638 -- We verify that the Parent_Type is a partial view by checking
16639 -- that it is not a Full_Type_Declaration (i.e. a private type or
16640 -- private extension declaration), to distinguish a partial view
16641 -- from a derivation from a private type which also appears as
16642 -- E_Private_Type. If the parent base type is not declared in an
16643 -- enclosing scope there is no need to check.
16645 elsif Present (Full_View (Parent_Type))
16646 and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
16647 and then not Is_Tagged_Type (Parent_Type)
16648 and then Is_Tagged_Type (Full_View (Parent_Type))
16649 and then In_Open_Scopes (Scope (Base_Type (Parent_Type)))
16650 then
16651 Error_Msg_N
16652 ("premature derivation from type with tagged full view",
16653 Indic);
16654 end if;
16655 end if;
16657 -- Check that form of derivation is appropriate
16659 Taggd := Is_Tagged_Type (Parent_Type);
16661 -- Set the parent type to the class-wide type's specific type in this
16662 -- case to prevent cascading errors
16664 if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
16665 Error_Msg_N ("parent type must not be a class-wide type", Indic);
16666 Set_Etype (T, Etype (Parent_Type));
16667 return;
16668 end if;
16670 if Present (Extension) and then not Taggd then
16671 Error_Msg_N
16672 ("type derived from untagged type cannot have extension", Indic);
16674 elsif No (Extension) and then Taggd then
16676 -- If this declaration is within a private part (or body) of a
16677 -- generic instantiation then the derivation is allowed (the parent
16678 -- type can only appear tagged in this case if it's a generic actual
16679 -- type, since it would otherwise have been rejected in the analysis
16680 -- of the generic template).
16682 if not Is_Generic_Actual_Type (Parent_Type)
16683 or else In_Visible_Part (Scope (Parent_Type))
16684 then
16685 if Is_Class_Wide_Type (Parent_Type) then
16686 Error_Msg_N
16687 ("parent type must not be a class-wide type", Indic);
16689 -- Use specific type to prevent cascaded errors.
16691 Parent_Type := Etype (Parent_Type);
16693 else
16694 Error_Msg_N
16695 ("type derived from tagged type must have extension", Indic);
16696 end if;
16697 end if;
16698 end if;
16700 -- AI-443: Synchronized formal derived types require a private
16701 -- extension. There is no point in checking the ancestor type or
16702 -- the progenitors since the construct is wrong to begin with.
16704 if Ada_Version >= Ada_2005
16705 and then Is_Generic_Type (T)
16706 and then Present (Original_Node (N))
16707 then
16708 declare
16709 Decl : constant Node_Id := Original_Node (N);
16711 begin
16712 if Nkind (Decl) = N_Formal_Type_Declaration
16713 and then Nkind (Formal_Type_Definition (Decl)) =
16714 N_Formal_Derived_Type_Definition
16715 and then Synchronized_Present (Formal_Type_Definition (Decl))
16716 and then No (Extension)
16718 -- Avoid emitting a duplicate error message
16720 and then not Error_Posted (Indic)
16721 then
16722 Error_Msg_N
16723 ("synchronized derived type must have extension", N);
16724 end if;
16725 end;
16726 end if;
16728 if Null_Exclusion_Present (Def)
16729 and then not Is_Access_Type (Parent_Type)
16730 then
16731 Error_Msg_N ("null exclusion can only apply to an access type", N);
16732 end if;
16734 -- Avoid deriving parent primitives of underlying record views
16736 Build_Derived_Type (N, Parent_Type, T, Is_Completion,
16737 Derive_Subps => not Is_Underlying_Record_View (T));
16739 -- AI-419: The parent type of an explicitly limited derived type must
16740 -- be a limited type or a limited interface.
16742 if Limited_Present (Def) then
16743 Set_Is_Limited_Record (T);
16745 if Is_Interface (T) then
16746 Set_Is_Limited_Interface (T);
16747 end if;
16749 if not Is_Limited_Type (Parent_Type)
16750 and then
16751 (not Is_Interface (Parent_Type)
16752 or else not Is_Limited_Interface (Parent_Type))
16753 then
16754 -- AI05-0096: a derivation in the private part of an instance is
16755 -- legal if the generic formal is untagged limited, and the actual
16756 -- is non-limited.
16758 if Is_Generic_Actual_Type (Parent_Type)
16759 and then In_Private_Part (Current_Scope)
16760 and then
16761 not Is_Tagged_Type
16762 (Generic_Parent_Type (Parent (Parent_Type)))
16763 then
16764 null;
16766 else
16767 Error_Msg_NE
16768 ("parent type& of limited type must be limited",
16769 N, Parent_Type);
16770 end if;
16771 end if;
16772 end if;
16774 -- In SPARK, there are no derived type definitions other than type
16775 -- extensions of tagged record types.
16777 if No (Extension) then
16778 Check_SPARK_05_Restriction
16779 ("derived type is not allowed", Original_Node (N));
16780 end if;
16781 end Derived_Type_Declaration;
16783 ------------------------
16784 -- Diagnose_Interface --
16785 ------------------------
16787 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id) is
16788 begin
16789 if not Is_Interface (E) and then E /= Any_Type then
16790 Error_Msg_NE ("(Ada 2005) & must be an interface", N, E);
16791 end if;
16792 end Diagnose_Interface;
16794 ----------------------------------
16795 -- Enumeration_Type_Declaration --
16796 ----------------------------------
16798 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
16799 Ev : Uint;
16800 L : Node_Id;
16801 R_Node : Node_Id;
16802 B_Node : Node_Id;
16804 begin
16805 -- Create identifier node representing lower bound
16807 B_Node := New_Node (N_Identifier, Sloc (Def));
16808 L := First (Literals (Def));
16809 Set_Chars (B_Node, Chars (L));
16810 Set_Entity (B_Node, L);
16811 Set_Etype (B_Node, T);
16812 Set_Is_Static_Expression (B_Node, True);
16814 R_Node := New_Node (N_Range, Sloc (Def));
16815 Set_Low_Bound (R_Node, B_Node);
16817 Set_Ekind (T, E_Enumeration_Type);
16818 Set_First_Literal (T, L);
16819 Set_Etype (T, T);
16820 Set_Is_Constrained (T);
16822 Ev := Uint_0;
16824 -- Loop through literals of enumeration type setting pos and rep values
16825 -- except that if the Ekind is already set, then it means the literal
16826 -- was already constructed (case of a derived type declaration and we
16827 -- should not disturb the Pos and Rep values.
16829 while Present (L) loop
16830 if Ekind (L) /= E_Enumeration_Literal then
16831 Set_Ekind (L, E_Enumeration_Literal);
16832 Set_Enumeration_Pos (L, Ev);
16833 Set_Enumeration_Rep (L, Ev);
16834 Set_Is_Known_Valid (L, True);
16835 end if;
16837 Set_Etype (L, T);
16838 New_Overloaded_Entity (L);
16839 Generate_Definition (L);
16840 Set_Convention (L, Convention_Intrinsic);
16842 -- Case of character literal
16844 if Nkind (L) = N_Defining_Character_Literal then
16845 Set_Is_Character_Type (T, True);
16847 -- Check violation of No_Wide_Characters
16849 if Restriction_Check_Required (No_Wide_Characters) then
16850 Get_Name_String (Chars (L));
16852 if Name_Len >= 3 and then Name_Buffer (1 .. 2) = "QW" then
16853 Check_Restriction (No_Wide_Characters, L);
16854 end if;
16855 end if;
16856 end if;
16858 Ev := Ev + 1;
16859 Next (L);
16860 end loop;
16862 -- Now create a node representing upper bound
16864 B_Node := New_Node (N_Identifier, Sloc (Def));
16865 Set_Chars (B_Node, Chars (Last (Literals (Def))));
16866 Set_Entity (B_Node, Last (Literals (Def)));
16867 Set_Etype (B_Node, T);
16868 Set_Is_Static_Expression (B_Node, True);
16870 Set_High_Bound (R_Node, B_Node);
16872 -- Initialize various fields of the type. Some of this information
16873 -- may be overwritten later through rep.clauses.
16875 Set_Scalar_Range (T, R_Node);
16876 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
16877 Set_Enum_Esize (T);
16878 Set_Enum_Pos_To_Rep (T, Empty);
16880 -- Set Discard_Names if configuration pragma set, or if there is
16881 -- a parameterless pragma in the current declarative region
16883 if Global_Discard_Names or else Discard_Names (Scope (T)) then
16884 Set_Discard_Names (T);
16885 end if;
16887 -- Process end label if there is one
16889 if Present (Def) then
16890 Process_End_Label (Def, 'e', T);
16891 end if;
16892 end Enumeration_Type_Declaration;
16894 ---------------------------------
16895 -- Expand_To_Stored_Constraint --
16896 ---------------------------------
16898 function Expand_To_Stored_Constraint
16899 (Typ : Entity_Id;
16900 Constraint : Elist_Id) return Elist_Id
16902 Explicitly_Discriminated_Type : Entity_Id;
16903 Expansion : Elist_Id;
16904 Discriminant : Entity_Id;
16906 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
16907 -- Find the nearest type that actually specifies discriminants
16909 ---------------------------------
16910 -- Type_With_Explicit_Discrims --
16911 ---------------------------------
16913 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
16914 Typ : constant E := Base_Type (Id);
16916 begin
16917 if Ekind (Typ) in Incomplete_Or_Private_Kind then
16918 if Present (Full_View (Typ)) then
16919 return Type_With_Explicit_Discrims (Full_View (Typ));
16920 end if;
16922 else
16923 if Has_Discriminants (Typ) then
16924 return Typ;
16925 end if;
16926 end if;
16928 if Etype (Typ) = Typ then
16929 return Empty;
16930 elsif Has_Discriminants (Typ) then
16931 return Typ;
16932 else
16933 return Type_With_Explicit_Discrims (Etype (Typ));
16934 end if;
16936 end Type_With_Explicit_Discrims;
16938 -- Start of processing for Expand_To_Stored_Constraint
16940 begin
16941 if No (Constraint) or else Is_Empty_Elmt_List (Constraint) then
16942 return No_Elist;
16943 end if;
16945 Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
16947 if No (Explicitly_Discriminated_Type) then
16948 return No_Elist;
16949 end if;
16951 Expansion := New_Elmt_List;
16953 Discriminant :=
16954 First_Stored_Discriminant (Explicitly_Discriminated_Type);
16955 while Present (Discriminant) loop
16956 Append_Elmt
16957 (Get_Discriminant_Value
16958 (Discriminant, Explicitly_Discriminated_Type, Constraint),
16959 To => Expansion);
16960 Next_Stored_Discriminant (Discriminant);
16961 end loop;
16963 return Expansion;
16964 end Expand_To_Stored_Constraint;
16966 ---------------------------
16967 -- Find_Hidden_Interface --
16968 ---------------------------
16970 function Find_Hidden_Interface
16971 (Src : Elist_Id;
16972 Dest : Elist_Id) return Entity_Id
16974 Iface : Entity_Id;
16975 Iface_Elmt : Elmt_Id;
16977 begin
16978 if Present (Src) and then Present (Dest) then
16979 Iface_Elmt := First_Elmt (Src);
16980 while Present (Iface_Elmt) loop
16981 Iface := Node (Iface_Elmt);
16983 if Is_Interface (Iface)
16984 and then not Contain_Interface (Iface, Dest)
16985 then
16986 return Iface;
16987 end if;
16989 Next_Elmt (Iface_Elmt);
16990 end loop;
16991 end if;
16993 return Empty;
16994 end Find_Hidden_Interface;
16996 --------------------
16997 -- Find_Type_Name --
16998 --------------------
17000 function Find_Type_Name (N : Node_Id) return Entity_Id is
17001 Id : constant Entity_Id := Defining_Identifier (N);
17002 New_Id : Entity_Id;
17003 Prev : Entity_Id;
17004 Prev_Par : Node_Id;
17006 procedure Check_Duplicate_Aspects;
17007 -- Check that aspects specified in a completion have not been specified
17008 -- already in the partial view.
17010 procedure Tag_Mismatch;
17011 -- Diagnose a tagged partial view whose full view is untagged. We post
17012 -- the message on the full view, with a reference to the previous
17013 -- partial view. The partial view can be private or incomplete, and
17014 -- these are handled in a different manner, so we determine the position
17015 -- of the error message from the respective slocs of both.
17017 -----------------------------
17018 -- Check_Duplicate_Aspects --
17019 -----------------------------
17021 procedure Check_Duplicate_Aspects is
17022 function Get_Partial_View_Aspect (Asp : Node_Id) return Node_Id;
17023 -- Return the corresponding aspect of the partial view which matches
17024 -- the aspect id of Asp. Return Empty is no such aspect exists.
17026 -----------------------------
17027 -- Get_Partial_View_Aspect --
17028 -----------------------------
17030 function Get_Partial_View_Aspect (Asp : Node_Id) return Node_Id is
17031 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp);
17032 Prev_Asps : constant List_Id := Aspect_Specifications (Prev_Par);
17033 Prev_Asp : Node_Id;
17035 begin
17036 if Present (Prev_Asps) then
17037 Prev_Asp := First (Prev_Asps);
17038 while Present (Prev_Asp) loop
17039 if Get_Aspect_Id (Prev_Asp) = Asp_Id then
17040 return Prev_Asp;
17041 end if;
17043 Next (Prev_Asp);
17044 end loop;
17045 end if;
17047 return Empty;
17048 end Get_Partial_View_Aspect;
17050 -- Local variables
17052 Full_Asps : constant List_Id := Aspect_Specifications (N);
17053 Full_Asp : Node_Id;
17054 Part_Asp : Node_Id;
17056 -- Start of processing for Check_Duplicate_Aspects
17058 begin
17059 if Present (Full_Asps) then
17060 Full_Asp := First (Full_Asps);
17061 while Present (Full_Asp) loop
17062 Part_Asp := Get_Partial_View_Aspect (Full_Asp);
17064 -- An aspect and its class-wide counterpart are two distinct
17065 -- aspects and may apply to both views of an entity.
17067 if Present (Part_Asp)
17068 and then Class_Present (Part_Asp) = Class_Present (Full_Asp)
17069 then
17070 Error_Msg_N
17071 ("aspect already specified in private declaration",
17072 Full_Asp);
17074 Remove (Full_Asp);
17075 return;
17076 end if;
17078 if Has_Discriminants (Prev)
17079 and then not Has_Unknown_Discriminants (Prev)
17080 and then Get_Aspect_Id (Full_Asp) =
17081 Aspect_Implicit_Dereference
17082 then
17083 Error_Msg_N
17084 ("cannot specify aspect if partial view has known "
17085 & "discriminants", Full_Asp);
17086 end if;
17088 Next (Full_Asp);
17089 end loop;
17090 end if;
17091 end Check_Duplicate_Aspects;
17093 ------------------
17094 -- Tag_Mismatch --
17095 ------------------
17097 procedure Tag_Mismatch is
17098 begin
17099 if Sloc (Prev) < Sloc (Id) then
17100 if Ada_Version >= Ada_2012
17101 and then Nkind (N) = N_Private_Type_Declaration
17102 then
17103 Error_Msg_NE
17104 ("declaration of private } must be a tagged type ", Id, Prev);
17105 else
17106 Error_Msg_NE
17107 ("full declaration of } must be a tagged type ", Id, Prev);
17108 end if;
17110 else
17111 if Ada_Version >= Ada_2012
17112 and then Nkind (N) = N_Private_Type_Declaration
17113 then
17114 Error_Msg_NE
17115 ("declaration of private } must be a tagged type ", Prev, Id);
17116 else
17117 Error_Msg_NE
17118 ("full declaration of } must be a tagged type ", Prev, Id);
17119 end if;
17120 end if;
17121 end Tag_Mismatch;
17123 -- Start of processing for Find_Type_Name
17125 begin
17126 -- Find incomplete declaration, if one was given
17128 Prev := Current_Entity_In_Scope (Id);
17130 -- New type declaration
17132 if No (Prev) then
17133 Enter_Name (Id);
17134 return Id;
17136 -- Previous declaration exists
17138 else
17139 Prev_Par := Parent (Prev);
17141 -- Error if not incomplete/private case except if previous
17142 -- declaration is implicit, etc. Enter_Name will emit error if
17143 -- appropriate.
17145 if not Is_Incomplete_Or_Private_Type (Prev) then
17146 Enter_Name (Id);
17147 New_Id := Id;
17149 -- Check invalid completion of private or incomplete type
17151 elsif not Nkind_In (N, N_Full_Type_Declaration,
17152 N_Task_Type_Declaration,
17153 N_Protected_Type_Declaration)
17154 and then
17155 (Ada_Version < Ada_2012
17156 or else not Is_Incomplete_Type (Prev)
17157 or else not Nkind_In (N, N_Private_Type_Declaration,
17158 N_Private_Extension_Declaration))
17159 then
17160 -- Completion must be a full type declarations (RM 7.3(4))
17162 Error_Msg_Sloc := Sloc (Prev);
17163 Error_Msg_NE ("invalid completion of }", Id, Prev);
17165 -- Set scope of Id to avoid cascaded errors. Entity is never
17166 -- examined again, except when saving globals in generics.
17168 Set_Scope (Id, Current_Scope);
17169 New_Id := Id;
17171 -- If this is a repeated incomplete declaration, no further
17172 -- checks are possible.
17174 if Nkind (N) = N_Incomplete_Type_Declaration then
17175 return Prev;
17176 end if;
17178 -- Case of full declaration of incomplete type
17180 elsif Ekind (Prev) = E_Incomplete_Type
17181 and then (Ada_Version < Ada_2012
17182 or else No (Full_View (Prev))
17183 or else not Is_Private_Type (Full_View (Prev)))
17184 then
17185 -- Indicate that the incomplete declaration has a matching full
17186 -- declaration. The defining occurrence of the incomplete
17187 -- declaration remains the visible one, and the procedure
17188 -- Get_Full_View dereferences it whenever the type is used.
17190 if Present (Full_View (Prev)) then
17191 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
17192 end if;
17194 Set_Full_View (Prev, Id);
17195 Append_Entity (Id, Current_Scope);
17196 Set_Is_Public (Id, Is_Public (Prev));
17197 Set_Is_Internal (Id);
17198 New_Id := Prev;
17200 -- If the incomplete view is tagged, a class_wide type has been
17201 -- created already. Use it for the private type as well, in order
17202 -- to prevent multiple incompatible class-wide types that may be
17203 -- created for self-referential anonymous access components.
17205 if Is_Tagged_Type (Prev)
17206 and then Present (Class_Wide_Type (Prev))
17207 then
17208 Set_Ekind (Id, Ekind (Prev)); -- will be reset later
17209 Set_Class_Wide_Type (Id, Class_Wide_Type (Prev));
17211 -- Type of the class-wide type is the current Id. Previously
17212 -- this was not done for private declarations because of order-
17213 -- of-elaboration issues in the back end, but gigi now handles
17214 -- this properly.
17216 Set_Etype (Class_Wide_Type (Id), Id);
17217 end if;
17219 -- Case of full declaration of private type
17221 else
17222 -- If the private type was a completion of an incomplete type then
17223 -- update Prev to reference the private type
17225 if Ada_Version >= Ada_2012
17226 and then Ekind (Prev) = E_Incomplete_Type
17227 and then Present (Full_View (Prev))
17228 and then Is_Private_Type (Full_View (Prev))
17229 then
17230 Prev := Full_View (Prev);
17231 Prev_Par := Parent (Prev);
17232 end if;
17234 if Nkind (N) = N_Full_Type_Declaration
17235 and then Nkind_In
17236 (Type_Definition (N), N_Record_Definition,
17237 N_Derived_Type_Definition)
17238 and then Interface_Present (Type_Definition (N))
17239 then
17240 Error_Msg_N
17241 ("completion of private type cannot be an interface", N);
17242 end if;
17244 if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
17245 if Etype (Prev) /= Prev then
17247 -- Prev is a private subtype or a derived type, and needs
17248 -- no completion.
17250 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
17251 New_Id := Id;
17253 elsif Ekind (Prev) = E_Private_Type
17254 and then Nkind_In (N, N_Task_Type_Declaration,
17255 N_Protected_Type_Declaration)
17256 then
17257 Error_Msg_N
17258 ("completion of nonlimited type cannot be limited", N);
17260 elsif Ekind (Prev) = E_Record_Type_With_Private
17261 and then Nkind_In (N, N_Task_Type_Declaration,
17262 N_Protected_Type_Declaration)
17263 then
17264 if not Is_Limited_Record (Prev) then
17265 Error_Msg_N
17266 ("completion of nonlimited type cannot be limited", N);
17268 elsif No (Interface_List (N)) then
17269 Error_Msg_N
17270 ("completion of tagged private type must be tagged",
17272 end if;
17273 end if;
17275 -- Ada 2005 (AI-251): Private extension declaration of a task
17276 -- type or a protected type. This case arises when covering
17277 -- interface types.
17279 elsif Nkind_In (N, N_Task_Type_Declaration,
17280 N_Protected_Type_Declaration)
17281 then
17282 null;
17284 elsif Nkind (N) /= N_Full_Type_Declaration
17285 or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
17286 then
17287 Error_Msg_N
17288 ("full view of private extension must be an extension", N);
17290 elsif not (Abstract_Present (Parent (Prev)))
17291 and then Abstract_Present (Type_Definition (N))
17292 then
17293 Error_Msg_N
17294 ("full view of non-abstract extension cannot be abstract", N);
17295 end if;
17297 if not In_Private_Part (Current_Scope) then
17298 Error_Msg_N
17299 ("declaration of full view must appear in private part", N);
17300 end if;
17302 if Ada_Version >= Ada_2012 then
17303 Check_Duplicate_Aspects;
17304 end if;
17306 Copy_And_Swap (Prev, Id);
17307 Set_Has_Private_Declaration (Prev);
17308 Set_Has_Private_Declaration (Id);
17310 -- AI12-0133: Indicate whether we have a partial view with
17311 -- unknown discriminants, in which case initialization of objects
17312 -- of the type do not receive an invariant check.
17314 Set_Partial_View_Has_Unknown_Discr
17315 (Prev, Has_Unknown_Discriminants (Id));
17317 -- Preserve aspect and iterator flags that may have been set on
17318 -- the partial view.
17320 Set_Has_Delayed_Aspects (Prev, Has_Delayed_Aspects (Id));
17321 Set_Has_Implicit_Dereference (Prev, Has_Implicit_Dereference (Id));
17323 -- If no error, propagate freeze_node from private to full view.
17324 -- It may have been generated for an early operational item.
17326 if Present (Freeze_Node (Id))
17327 and then Serious_Errors_Detected = 0
17328 and then No (Full_View (Id))
17329 then
17330 Set_Freeze_Node (Prev, Freeze_Node (Id));
17331 Set_Freeze_Node (Id, Empty);
17332 Set_First_Rep_Item (Prev, First_Rep_Item (Id));
17333 end if;
17335 Set_Full_View (Id, Prev);
17336 New_Id := Prev;
17337 end if;
17339 -- Verify that full declaration conforms to partial one
17341 if Is_Incomplete_Or_Private_Type (Prev)
17342 and then Present (Discriminant_Specifications (Prev_Par))
17343 then
17344 if Present (Discriminant_Specifications (N)) then
17345 if Ekind (Prev) = E_Incomplete_Type then
17346 Check_Discriminant_Conformance (N, Prev, Prev);
17347 else
17348 Check_Discriminant_Conformance (N, Prev, Id);
17349 end if;
17351 else
17352 Error_Msg_N
17353 ("missing discriminants in full type declaration", N);
17355 -- To avoid cascaded errors on subsequent use, share the
17356 -- discriminants of the partial view.
17358 Set_Discriminant_Specifications (N,
17359 Discriminant_Specifications (Prev_Par));
17360 end if;
17361 end if;
17363 -- A prior untagged partial view can have an associated class-wide
17364 -- type due to use of the class attribute, and in this case the full
17365 -- type must also be tagged. This Ada 95 usage is deprecated in favor
17366 -- of incomplete tagged declarations, but we check for it.
17368 if Is_Type (Prev)
17369 and then (Is_Tagged_Type (Prev)
17370 or else Present (Class_Wide_Type (Prev)))
17371 then
17372 -- Ada 2012 (AI05-0162): A private type may be the completion of
17373 -- an incomplete type.
17375 if Ada_Version >= Ada_2012
17376 and then Is_Incomplete_Type (Prev)
17377 and then Nkind_In (N, N_Private_Type_Declaration,
17378 N_Private_Extension_Declaration)
17379 then
17380 -- No need to check private extensions since they are tagged
17382 if Nkind (N) = N_Private_Type_Declaration
17383 and then not Tagged_Present (N)
17384 then
17385 Tag_Mismatch;
17386 end if;
17388 -- The full declaration is either a tagged type (including
17389 -- a synchronized type that implements interfaces) or a
17390 -- type extension, otherwise this is an error.
17392 elsif Nkind_In (N, N_Task_Type_Declaration,
17393 N_Protected_Type_Declaration)
17394 then
17395 if No (Interface_List (N)) and then not Error_Posted (N) then
17396 Tag_Mismatch;
17397 end if;
17399 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
17401 -- Indicate that the previous declaration (tagged incomplete
17402 -- or private declaration) requires the same on the full one.
17404 if not Tagged_Present (Type_Definition (N)) then
17405 Tag_Mismatch;
17406 Set_Is_Tagged_Type (Id);
17407 end if;
17409 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
17410 if No (Record_Extension_Part (Type_Definition (N))) then
17411 Error_Msg_NE
17412 ("full declaration of } must be a record extension",
17413 Prev, Id);
17415 -- Set some attributes to produce a usable full view
17417 Set_Is_Tagged_Type (Id);
17418 end if;
17420 else
17421 Tag_Mismatch;
17422 end if;
17423 end if;
17425 if Present (Prev)
17426 and then Nkind (Parent (Prev)) = N_Incomplete_Type_Declaration
17427 and then Present (Premature_Use (Parent (Prev)))
17428 then
17429 Error_Msg_Sloc := Sloc (N);
17430 Error_Msg_N
17431 ("\full declaration #", Premature_Use (Parent (Prev)));
17432 end if;
17434 return New_Id;
17435 end if;
17436 end Find_Type_Name;
17438 -------------------------
17439 -- Find_Type_Of_Object --
17440 -------------------------
17442 function Find_Type_Of_Object
17443 (Obj_Def : Node_Id;
17444 Related_Nod : Node_Id) return Entity_Id
17446 Def_Kind : constant Node_Kind := Nkind (Obj_Def);
17447 P : Node_Id := Parent (Obj_Def);
17448 T : Entity_Id;
17449 Nam : Name_Id;
17451 begin
17452 -- If the parent is a component_definition node we climb to the
17453 -- component_declaration node
17455 if Nkind (P) = N_Component_Definition then
17456 P := Parent (P);
17457 end if;
17459 -- Case of an anonymous array subtype
17461 if Nkind_In (Def_Kind, N_Constrained_Array_Definition,
17462 N_Unconstrained_Array_Definition)
17463 then
17464 T := Empty;
17465 Array_Type_Declaration (T, Obj_Def);
17467 -- Create an explicit subtype whenever possible
17469 elsif Nkind (P) /= N_Component_Declaration
17470 and then Def_Kind = N_Subtype_Indication
17471 then
17472 -- Base name of subtype on object name, which will be unique in
17473 -- the current scope.
17475 -- If this is a duplicate declaration, return base type, to avoid
17476 -- generating duplicate anonymous types.
17478 if Error_Posted (P) then
17479 Analyze (Subtype_Mark (Obj_Def));
17480 return Entity (Subtype_Mark (Obj_Def));
17481 end if;
17483 Nam :=
17484 New_External_Name
17485 (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
17487 T := Make_Defining_Identifier (Sloc (P), Nam);
17489 Insert_Action (Obj_Def,
17490 Make_Subtype_Declaration (Sloc (P),
17491 Defining_Identifier => T,
17492 Subtype_Indication => Relocate_Node (Obj_Def)));
17494 -- This subtype may need freezing, and this will not be done
17495 -- automatically if the object declaration is not in declarative
17496 -- part. Since this is an object declaration, the type cannot always
17497 -- be frozen here. Deferred constants do not freeze their type
17498 -- (which often enough will be private).
17500 if Nkind (P) = N_Object_Declaration
17501 and then Constant_Present (P)
17502 and then No (Expression (P))
17503 then
17504 null;
17506 -- Here we freeze the base type of object type to catch premature use
17507 -- of discriminated private type without a full view.
17509 else
17510 Insert_Actions (Obj_Def, Freeze_Entity (Base_Type (T), P));
17511 end if;
17513 -- Ada 2005 AI-406: the object definition in an object declaration
17514 -- can be an access definition.
17516 elsif Def_Kind = N_Access_Definition then
17517 T := Access_Definition (Related_Nod, Obj_Def);
17519 Set_Is_Local_Anonymous_Access
17521 V => (Ada_Version < Ada_2012)
17522 or else (Nkind (P) /= N_Object_Declaration)
17523 or else Is_Library_Level_Entity (Defining_Identifier (P)));
17525 -- Otherwise, the object definition is just a subtype_mark
17527 else
17528 T := Process_Subtype (Obj_Def, Related_Nod);
17530 -- If expansion is disabled an object definition that is an aggregate
17531 -- will not get expanded and may lead to scoping problems in the back
17532 -- end, if the object is referenced in an inner scope. In that case
17533 -- create an itype reference for the object definition now. This
17534 -- may be redundant in some cases, but harmless.
17536 if Is_Itype (T)
17537 and then Nkind (Related_Nod) = N_Object_Declaration
17538 and then ASIS_Mode
17539 then
17540 Build_Itype_Reference (T, Related_Nod);
17541 end if;
17542 end if;
17544 return T;
17545 end Find_Type_Of_Object;
17547 --------------------------------
17548 -- Find_Type_Of_Subtype_Indic --
17549 --------------------------------
17551 function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
17552 Typ : Entity_Id;
17554 begin
17555 -- Case of subtype mark with a constraint
17557 if Nkind (S) = N_Subtype_Indication then
17558 Find_Type (Subtype_Mark (S));
17559 Typ := Entity (Subtype_Mark (S));
17561 if not
17562 Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
17563 then
17564 Error_Msg_N
17565 ("incorrect constraint for this kind of type", Constraint (S));
17566 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
17567 end if;
17569 -- Otherwise we have a subtype mark without a constraint
17571 elsif Error_Posted (S) then
17572 Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
17573 return Any_Type;
17575 else
17576 Find_Type (S);
17577 Typ := Entity (S);
17578 end if;
17580 -- Check No_Wide_Characters restriction
17582 Check_Wide_Character_Restriction (Typ, S);
17584 return Typ;
17585 end Find_Type_Of_Subtype_Indic;
17587 -------------------------------------
17588 -- Floating_Point_Type_Declaration --
17589 -------------------------------------
17591 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
17592 Digs : constant Node_Id := Digits_Expression (Def);
17593 Max_Digs_Val : constant Uint := Digits_Value (Standard_Long_Long_Float);
17594 Digs_Val : Uint;
17595 Base_Typ : Entity_Id;
17596 Implicit_Base : Entity_Id;
17597 Bound : Node_Id;
17599 function Can_Derive_From (E : Entity_Id) return Boolean;
17600 -- Find if given digits value, and possibly a specified range, allows
17601 -- derivation from specified type
17603 function Find_Base_Type return Entity_Id;
17604 -- Find a predefined base type that Def can derive from, or generate
17605 -- an error and substitute Long_Long_Float if none exists.
17607 ---------------------
17608 -- Can_Derive_From --
17609 ---------------------
17611 function Can_Derive_From (E : Entity_Id) return Boolean is
17612 Spec : constant Entity_Id := Real_Range_Specification (Def);
17614 begin
17615 -- Check specified "digits" constraint
17617 if Digs_Val > Digits_Value (E) then
17618 return False;
17619 end if;
17621 -- Check for matching range, if specified
17623 if Present (Spec) then
17624 if Expr_Value_R (Type_Low_Bound (E)) >
17625 Expr_Value_R (Low_Bound (Spec))
17626 then
17627 return False;
17628 end if;
17630 if Expr_Value_R (Type_High_Bound (E)) <
17631 Expr_Value_R (High_Bound (Spec))
17632 then
17633 return False;
17634 end if;
17635 end if;
17637 return True;
17638 end Can_Derive_From;
17640 --------------------
17641 -- Find_Base_Type --
17642 --------------------
17644 function Find_Base_Type return Entity_Id is
17645 Choice : Elmt_Id := First_Elmt (Predefined_Float_Types);
17647 begin
17648 -- Iterate over the predefined types in order, returning the first
17649 -- one that Def can derive from.
17651 while Present (Choice) loop
17652 if Can_Derive_From (Node (Choice)) then
17653 return Node (Choice);
17654 end if;
17656 Next_Elmt (Choice);
17657 end loop;
17659 -- If we can't derive from any existing type, use Long_Long_Float
17660 -- and give appropriate message explaining the problem.
17662 if Digs_Val > Max_Digs_Val then
17663 -- It might be the case that there is a type with the requested
17664 -- range, just not the combination of digits and range.
17666 Error_Msg_N
17667 ("no predefined type has requested range and precision",
17668 Real_Range_Specification (Def));
17670 else
17671 Error_Msg_N
17672 ("range too large for any predefined type",
17673 Real_Range_Specification (Def));
17674 end if;
17676 return Standard_Long_Long_Float;
17677 end Find_Base_Type;
17679 -- Start of processing for Floating_Point_Type_Declaration
17681 begin
17682 Check_Restriction (No_Floating_Point, Def);
17684 -- Create an implicit base type
17686 Implicit_Base :=
17687 Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
17689 -- Analyze and verify digits value
17691 Analyze_And_Resolve (Digs, Any_Integer);
17692 Check_Digits_Expression (Digs);
17693 Digs_Val := Expr_Value (Digs);
17695 -- Process possible range spec and find correct type to derive from
17697 Process_Real_Range_Specification (Def);
17699 -- Check that requested number of digits is not too high.
17701 if Digs_Val > Max_Digs_Val then
17703 -- The check for Max_Base_Digits may be somewhat expensive, as it
17704 -- requires reading System, so only do it when necessary.
17706 declare
17707 Max_Base_Digits : constant Uint :=
17708 Expr_Value
17709 (Expression
17710 (Parent (RTE (RE_Max_Base_Digits))));
17712 begin
17713 if Digs_Val > Max_Base_Digits then
17714 Error_Msg_Uint_1 := Max_Base_Digits;
17715 Error_Msg_N ("digits value out of range, maximum is ^", Digs);
17717 elsif No (Real_Range_Specification (Def)) then
17718 Error_Msg_Uint_1 := Max_Digs_Val;
17719 Error_Msg_N ("types with more than ^ digits need range spec "
17720 & "(RM 3.5.7(6))", Digs);
17721 end if;
17722 end;
17723 end if;
17725 -- Find a suitable type to derive from or complain and use a substitute
17727 Base_Typ := Find_Base_Type;
17729 -- If there are bounds given in the declaration use them as the bounds
17730 -- of the type, otherwise use the bounds of the predefined base type
17731 -- that was chosen based on the Digits value.
17733 if Present (Real_Range_Specification (Def)) then
17734 Set_Scalar_Range (T, Real_Range_Specification (Def));
17735 Set_Is_Constrained (T);
17737 -- The bounds of this range must be converted to machine numbers
17738 -- in accordance with RM 4.9(38).
17740 Bound := Type_Low_Bound (T);
17742 if Nkind (Bound) = N_Real_Literal then
17743 Set_Realval
17744 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
17745 Set_Is_Machine_Number (Bound);
17746 end if;
17748 Bound := Type_High_Bound (T);
17750 if Nkind (Bound) = N_Real_Literal then
17751 Set_Realval
17752 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
17753 Set_Is_Machine_Number (Bound);
17754 end if;
17756 else
17757 Set_Scalar_Range (T, Scalar_Range (Base_Typ));
17758 end if;
17760 -- Complete definition of implicit base and declared first subtype. The
17761 -- inheritance of the rep item chain ensures that SPARK-related pragmas
17762 -- are not clobbered when the floating point type acts as a full view of
17763 -- a private type.
17765 Set_Etype (Implicit_Base, Base_Typ);
17766 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
17767 Set_Size_Info (Implicit_Base, Base_Typ);
17768 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
17769 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
17770 Set_Digits_Value (Implicit_Base, Digits_Value (Base_Typ));
17771 Set_Float_Rep (Implicit_Base, Float_Rep (Base_Typ));
17773 Set_Ekind (T, E_Floating_Point_Subtype);
17774 Set_Etype (T, Implicit_Base);
17775 Set_Size_Info (T, Implicit_Base);
17776 Set_RM_Size (T, RM_Size (Implicit_Base));
17777 Inherit_Rep_Item_Chain (T, Implicit_Base);
17778 Set_Digits_Value (T, Digs_Val);
17779 end Floating_Point_Type_Declaration;
17781 ----------------------------
17782 -- Get_Discriminant_Value --
17783 ----------------------------
17785 -- This is the situation:
17787 -- There is a non-derived type
17789 -- type T0 (Dx, Dy, Dz...)
17791 -- There are zero or more levels of derivation, with each derivation
17792 -- either purely inheriting the discriminants, or defining its own.
17794 -- type Ti is new Ti-1
17795 -- or
17796 -- type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
17797 -- or
17798 -- subtype Ti is ...
17800 -- The subtype issue is avoided by the use of Original_Record_Component,
17801 -- and the fact that derived subtypes also derive the constraints.
17803 -- This chain leads back from
17805 -- Typ_For_Constraint
17807 -- Typ_For_Constraint has discriminants, and the value for each
17808 -- discriminant is given by its corresponding Elmt of Constraints.
17810 -- Discriminant is some discriminant in this hierarchy
17812 -- We need to return its value
17814 -- We do this by recursively searching each level, and looking for
17815 -- Discriminant. Once we get to the bottom, we start backing up
17816 -- returning the value for it which may in turn be a discriminant
17817 -- further up, so on the backup we continue the substitution.
17819 function Get_Discriminant_Value
17820 (Discriminant : Entity_Id;
17821 Typ_For_Constraint : Entity_Id;
17822 Constraint : Elist_Id) return Node_Id
17824 function Root_Corresponding_Discriminant
17825 (Discr : Entity_Id) return Entity_Id;
17826 -- Given a discriminant, traverse the chain of inherited discriminants
17827 -- and return the topmost discriminant.
17829 function Search_Derivation_Levels
17830 (Ti : Entity_Id;
17831 Discrim_Values : Elist_Id;
17832 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
17833 -- This is the routine that performs the recursive search of levels
17834 -- as described above.
17836 -------------------------------------
17837 -- Root_Corresponding_Discriminant --
17838 -------------------------------------
17840 function Root_Corresponding_Discriminant
17841 (Discr : Entity_Id) return Entity_Id
17843 D : Entity_Id;
17845 begin
17846 D := Discr;
17847 while Present (Corresponding_Discriminant (D)) loop
17848 D := Corresponding_Discriminant (D);
17849 end loop;
17851 return D;
17852 end Root_Corresponding_Discriminant;
17854 ------------------------------
17855 -- Search_Derivation_Levels --
17856 ------------------------------
17858 function Search_Derivation_Levels
17859 (Ti : Entity_Id;
17860 Discrim_Values : Elist_Id;
17861 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
17863 Assoc : Elmt_Id;
17864 Disc : Entity_Id;
17865 Result : Node_Or_Entity_Id;
17866 Result_Entity : Node_Id;
17868 begin
17869 -- If inappropriate type, return Error, this happens only in
17870 -- cascaded error situations, and we want to avoid a blow up.
17872 if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
17873 return Error;
17874 end if;
17876 -- Look deeper if possible. Use Stored_Constraints only for
17877 -- untagged types. For tagged types use the given constraint.
17878 -- This asymmetry needs explanation???
17880 if not Stored_Discrim_Values
17881 and then Present (Stored_Constraint (Ti))
17882 and then not Is_Tagged_Type (Ti)
17883 then
17884 Result :=
17885 Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
17886 else
17887 declare
17888 Td : constant Entity_Id := Etype (Ti);
17890 begin
17891 if Td = Ti then
17892 Result := Discriminant;
17894 else
17895 if Present (Stored_Constraint (Ti)) then
17896 Result :=
17897 Search_Derivation_Levels
17898 (Td, Stored_Constraint (Ti), True);
17899 else
17900 Result :=
17901 Search_Derivation_Levels
17902 (Td, Discrim_Values, Stored_Discrim_Values);
17903 end if;
17904 end if;
17905 end;
17906 end if;
17908 -- Extra underlying places to search, if not found above. For
17909 -- concurrent types, the relevant discriminant appears in the
17910 -- corresponding record. For a type derived from a private type
17911 -- without discriminant, the full view inherits the discriminants
17912 -- of the full view of the parent.
17914 if Result = Discriminant then
17915 if Is_Concurrent_Type (Ti)
17916 and then Present (Corresponding_Record_Type (Ti))
17917 then
17918 Result :=
17919 Search_Derivation_Levels (
17920 Corresponding_Record_Type (Ti),
17921 Discrim_Values,
17922 Stored_Discrim_Values);
17924 elsif Is_Private_Type (Ti)
17925 and then not Has_Discriminants (Ti)
17926 and then Present (Full_View (Ti))
17927 and then Etype (Full_View (Ti)) /= Ti
17928 then
17929 Result :=
17930 Search_Derivation_Levels (
17931 Full_View (Ti),
17932 Discrim_Values,
17933 Stored_Discrim_Values);
17934 end if;
17935 end if;
17937 -- If Result is not a (reference to a) discriminant, return it,
17938 -- otherwise set Result_Entity to the discriminant.
17940 if Nkind (Result) = N_Defining_Identifier then
17941 pragma Assert (Result = Discriminant);
17942 Result_Entity := Result;
17944 else
17945 if not Denotes_Discriminant (Result) then
17946 return Result;
17947 end if;
17949 Result_Entity := Entity (Result);
17950 end if;
17952 -- See if this level of derivation actually has discriminants because
17953 -- tagged derivations can add them, hence the lower levels need not
17954 -- have any.
17956 if not Has_Discriminants (Ti) then
17957 return Result;
17958 end if;
17960 -- Scan Ti's discriminants for Result_Entity, and return its
17961 -- corresponding value, if any.
17963 Result_Entity := Original_Record_Component (Result_Entity);
17965 Assoc := First_Elmt (Discrim_Values);
17967 if Stored_Discrim_Values then
17968 Disc := First_Stored_Discriminant (Ti);
17969 else
17970 Disc := First_Discriminant (Ti);
17971 end if;
17973 while Present (Disc) loop
17975 -- If no further associations return the discriminant, value will
17976 -- be found on the second pass.
17978 if No (Assoc) then
17979 return Result;
17980 end if;
17982 if Original_Record_Component (Disc) = Result_Entity then
17983 return Node (Assoc);
17984 end if;
17986 Next_Elmt (Assoc);
17988 if Stored_Discrim_Values then
17989 Next_Stored_Discriminant (Disc);
17990 else
17991 Next_Discriminant (Disc);
17992 end if;
17993 end loop;
17995 -- Could not find it
17997 return Result;
17998 end Search_Derivation_Levels;
18000 -- Local Variables
18002 Result : Node_Or_Entity_Id;
18004 -- Start of processing for Get_Discriminant_Value
18006 begin
18007 -- ??? This routine is a gigantic mess and will be deleted. For the
18008 -- time being just test for the trivial case before calling recurse.
18010 -- We are now celebrating the 20th anniversary of this comment!
18012 if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
18013 declare
18014 D : Entity_Id;
18015 E : Elmt_Id;
18017 begin
18018 D := First_Discriminant (Typ_For_Constraint);
18019 E := First_Elmt (Constraint);
18020 while Present (D) loop
18021 if Chars (D) = Chars (Discriminant) then
18022 return Node (E);
18023 end if;
18025 Next_Discriminant (D);
18026 Next_Elmt (E);
18027 end loop;
18028 end;
18029 end if;
18031 Result := Search_Derivation_Levels
18032 (Typ_For_Constraint, Constraint, False);
18034 -- ??? hack to disappear when this routine is gone
18036 if Nkind (Result) = N_Defining_Identifier then
18037 declare
18038 D : Entity_Id;
18039 E : Elmt_Id;
18041 begin
18042 D := First_Discriminant (Typ_For_Constraint);
18043 E := First_Elmt (Constraint);
18044 while Present (D) loop
18045 if Root_Corresponding_Discriminant (D) = Discriminant then
18046 return Node (E);
18047 end if;
18049 Next_Discriminant (D);
18050 Next_Elmt (E);
18051 end loop;
18052 end;
18053 end if;
18055 pragma Assert (Nkind (Result) /= N_Defining_Identifier);
18056 return Result;
18057 end Get_Discriminant_Value;
18059 --------------------------
18060 -- Has_Range_Constraint --
18061 --------------------------
18063 function Has_Range_Constraint (N : Node_Id) return Boolean is
18064 C : constant Node_Id := Constraint (N);
18066 begin
18067 if Nkind (C) = N_Range_Constraint then
18068 return True;
18070 elsif Nkind (C) = N_Digits_Constraint then
18071 return
18072 Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
18073 or else Present (Range_Constraint (C));
18075 elsif Nkind (C) = N_Delta_Constraint then
18076 return Present (Range_Constraint (C));
18078 else
18079 return False;
18080 end if;
18081 end Has_Range_Constraint;
18083 ------------------------
18084 -- Inherit_Components --
18085 ------------------------
18087 function Inherit_Components
18088 (N : Node_Id;
18089 Parent_Base : Entity_Id;
18090 Derived_Base : Entity_Id;
18091 Is_Tagged : Boolean;
18092 Inherit_Discr : Boolean;
18093 Discs : Elist_Id) return Elist_Id
18095 Assoc_List : constant Elist_Id := New_Elmt_List;
18097 procedure Inherit_Component
18098 (Old_C : Entity_Id;
18099 Plain_Discrim : Boolean := False;
18100 Stored_Discrim : Boolean := False);
18101 -- Inherits component Old_C from Parent_Base to the Derived_Base. If
18102 -- Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
18103 -- True, Old_C is a stored discriminant. If they are both false then
18104 -- Old_C is a regular component.
18106 -----------------------
18107 -- Inherit_Component --
18108 -----------------------
18110 procedure Inherit_Component
18111 (Old_C : Entity_Id;
18112 Plain_Discrim : Boolean := False;
18113 Stored_Discrim : Boolean := False)
18115 procedure Set_Anonymous_Type (Id : Entity_Id);
18116 -- Id denotes the entity of an access discriminant or anonymous
18117 -- access component. Set the type of Id to either the same type of
18118 -- Old_C or create a new one depending on whether the parent and
18119 -- the child types are in the same scope.
18121 ------------------------
18122 -- Set_Anonymous_Type --
18123 ------------------------
18125 procedure Set_Anonymous_Type (Id : Entity_Id) is
18126 Old_Typ : constant Entity_Id := Etype (Old_C);
18128 begin
18129 if Scope (Parent_Base) = Scope (Derived_Base) then
18130 Set_Etype (Id, Old_Typ);
18132 -- The parent and the derived type are in two different scopes.
18133 -- Reuse the type of the original discriminant / component by
18134 -- copying it in order to preserve all attributes.
18136 else
18137 declare
18138 Typ : constant Entity_Id := New_Copy (Old_Typ);
18140 begin
18141 Set_Etype (Id, Typ);
18143 -- Since we do not generate component declarations for
18144 -- inherited components, associate the itype with the
18145 -- derived type.
18147 Set_Associated_Node_For_Itype (Typ, Parent (Derived_Base));
18148 Set_Scope (Typ, Derived_Base);
18149 end;
18150 end if;
18151 end Set_Anonymous_Type;
18153 -- Local variables and constants
18155 New_C : constant Entity_Id := New_Copy (Old_C);
18157 Corr_Discrim : Entity_Id;
18158 Discrim : Entity_Id;
18160 -- Start of processing for Inherit_Component
18162 begin
18163 pragma Assert (not Is_Tagged or not Stored_Discrim);
18165 Set_Parent (New_C, Parent (Old_C));
18167 -- Regular discriminants and components must be inserted in the scope
18168 -- of the Derived_Base. Do it here.
18170 if not Stored_Discrim then
18171 Enter_Name (New_C);
18172 end if;
18174 -- For tagged types the Original_Record_Component must point to
18175 -- whatever this field was pointing to in the parent type. This has
18176 -- already been achieved by the call to New_Copy above.
18178 if not Is_Tagged then
18179 Set_Original_Record_Component (New_C, New_C);
18180 Set_Corresponding_Record_Component (New_C, Old_C);
18181 end if;
18183 -- Set the proper type of an access discriminant
18185 if Ekind (New_C) = E_Discriminant
18186 and then Ekind (Etype (New_C)) = E_Anonymous_Access_Type
18187 then
18188 Set_Anonymous_Type (New_C);
18189 end if;
18191 -- If we have inherited a component then see if its Etype contains
18192 -- references to Parent_Base discriminants. In this case, replace
18193 -- these references with the constraints given in Discs. We do not
18194 -- do this for the partial view of private types because this is
18195 -- not needed (only the components of the full view will be used
18196 -- for code generation) and cause problem. We also avoid this
18197 -- transformation in some error situations.
18199 if Ekind (New_C) = E_Component then
18201 -- Set the proper type of an anonymous access component
18203 if Ekind (Etype (New_C)) = E_Anonymous_Access_Type then
18204 Set_Anonymous_Type (New_C);
18206 elsif (Is_Private_Type (Derived_Base)
18207 and then not Is_Generic_Type (Derived_Base))
18208 or else (Is_Empty_Elmt_List (Discs)
18209 and then not Expander_Active)
18210 then
18211 Set_Etype (New_C, Etype (Old_C));
18213 else
18214 -- The current component introduces a circularity of the
18215 -- following kind:
18217 -- limited with Pack_2;
18218 -- package Pack_1 is
18219 -- type T_1 is tagged record
18220 -- Comp : access Pack_2.T_2;
18221 -- ...
18222 -- end record;
18223 -- end Pack_1;
18225 -- with Pack_1;
18226 -- package Pack_2 is
18227 -- type T_2 is new Pack_1.T_1 with ...;
18228 -- end Pack_2;
18230 Set_Etype
18231 (New_C,
18232 Constrain_Component_Type
18233 (Old_C, Derived_Base, N, Parent_Base, Discs));
18234 end if;
18235 end if;
18237 -- In derived tagged types it is illegal to reference a non
18238 -- discriminant component in the parent type. To catch this, mark
18239 -- these components with an Ekind of E_Void. This will be reset in
18240 -- Record_Type_Definition after processing the record extension of
18241 -- the derived type.
18243 -- If the declaration is a private extension, there is no further
18244 -- record extension to process, and the components retain their
18245 -- current kind, because they are visible at this point.
18247 if Is_Tagged and then Ekind (New_C) = E_Component
18248 and then Nkind (N) /= N_Private_Extension_Declaration
18249 then
18250 Set_Ekind (New_C, E_Void);
18251 end if;
18253 if Plain_Discrim then
18254 Set_Corresponding_Discriminant (New_C, Old_C);
18255 Build_Discriminal (New_C);
18257 -- If we are explicitly inheriting a stored discriminant it will be
18258 -- completely hidden.
18260 elsif Stored_Discrim then
18261 Set_Corresponding_Discriminant (New_C, Empty);
18262 Set_Discriminal (New_C, Empty);
18263 Set_Is_Completely_Hidden (New_C);
18265 -- Set the Original_Record_Component of each discriminant in the
18266 -- derived base to point to the corresponding stored that we just
18267 -- created.
18269 Discrim := First_Discriminant (Derived_Base);
18270 while Present (Discrim) loop
18271 Corr_Discrim := Corresponding_Discriminant (Discrim);
18273 -- Corr_Discrim could be missing in an error situation
18275 if Present (Corr_Discrim)
18276 and then Original_Record_Component (Corr_Discrim) = Old_C
18277 then
18278 Set_Original_Record_Component (Discrim, New_C);
18279 Set_Corresponding_Record_Component (Discrim, Empty);
18280 end if;
18282 Next_Discriminant (Discrim);
18283 end loop;
18285 Append_Entity (New_C, Derived_Base);
18286 end if;
18288 if not Is_Tagged then
18289 Append_Elmt (Old_C, Assoc_List);
18290 Append_Elmt (New_C, Assoc_List);
18291 end if;
18292 end Inherit_Component;
18294 -- Variables local to Inherit_Component
18296 Loc : constant Source_Ptr := Sloc (N);
18298 Parent_Discrim : Entity_Id;
18299 Stored_Discrim : Entity_Id;
18300 D : Entity_Id;
18301 Component : Entity_Id;
18303 -- Start of processing for Inherit_Components
18305 begin
18306 if not Is_Tagged then
18307 Append_Elmt (Parent_Base, Assoc_List);
18308 Append_Elmt (Derived_Base, Assoc_List);
18309 end if;
18311 -- Inherit parent discriminants if needed
18313 if Inherit_Discr then
18314 Parent_Discrim := First_Discriminant (Parent_Base);
18315 while Present (Parent_Discrim) loop
18316 Inherit_Component (Parent_Discrim, Plain_Discrim => True);
18317 Next_Discriminant (Parent_Discrim);
18318 end loop;
18319 end if;
18321 -- Create explicit stored discrims for untagged types when necessary
18323 if not Has_Unknown_Discriminants (Derived_Base)
18324 and then Has_Discriminants (Parent_Base)
18325 and then not Is_Tagged
18326 and then
18327 (not Inherit_Discr
18328 or else First_Discriminant (Parent_Base) /=
18329 First_Stored_Discriminant (Parent_Base))
18330 then
18331 Stored_Discrim := First_Stored_Discriminant (Parent_Base);
18332 while Present (Stored_Discrim) loop
18333 Inherit_Component (Stored_Discrim, Stored_Discrim => True);
18334 Next_Stored_Discriminant (Stored_Discrim);
18335 end loop;
18336 end if;
18338 -- See if we can apply the second transformation for derived types, as
18339 -- explained in point 6. in the comments above Build_Derived_Record_Type
18340 -- This is achieved by appending Derived_Base discriminants into Discs,
18341 -- which has the side effect of returning a non empty Discs list to the
18342 -- caller of Inherit_Components, which is what we want. This must be
18343 -- done for private derived types if there are explicit stored
18344 -- discriminants, to ensure that we can retrieve the values of the
18345 -- constraints provided in the ancestors.
18347 if Inherit_Discr
18348 and then Is_Empty_Elmt_List (Discs)
18349 and then Present (First_Discriminant (Derived_Base))
18350 and then
18351 (not Is_Private_Type (Derived_Base)
18352 or else Is_Completely_Hidden
18353 (First_Stored_Discriminant (Derived_Base))
18354 or else Is_Generic_Type (Derived_Base))
18355 then
18356 D := First_Discriminant (Derived_Base);
18357 while Present (D) loop
18358 Append_Elmt (New_Occurrence_Of (D, Loc), Discs);
18359 Next_Discriminant (D);
18360 end loop;
18361 end if;
18363 -- Finally, inherit non-discriminant components unless they are not
18364 -- visible because defined or inherited from the full view of the
18365 -- parent. Don't inherit the _parent field of the parent type.
18367 Component := First_Entity (Parent_Base);
18368 while Present (Component) loop
18370 -- Ada 2005 (AI-251): Do not inherit components associated with
18371 -- secondary tags of the parent.
18373 if Ekind (Component) = E_Component
18374 and then Present (Related_Type (Component))
18375 then
18376 null;
18378 elsif Ekind (Component) /= E_Component
18379 or else Chars (Component) = Name_uParent
18380 then
18381 null;
18383 -- If the derived type is within the parent type's declarative
18384 -- region, then the components can still be inherited even though
18385 -- they aren't visible at this point. This can occur for cases
18386 -- such as within public child units where the components must
18387 -- become visible upon entering the child unit's private part.
18389 elsif not Is_Visible_Component (Component)
18390 and then not In_Open_Scopes (Scope (Parent_Base))
18391 then
18392 null;
18394 elsif Ekind_In (Derived_Base, E_Private_Type,
18395 E_Limited_Private_Type)
18396 then
18397 null;
18399 else
18400 Inherit_Component (Component);
18401 end if;
18403 Next_Entity (Component);
18404 end loop;
18406 -- For tagged derived types, inherited discriminants cannot be used in
18407 -- component declarations of the record extension part. To achieve this
18408 -- we mark the inherited discriminants as not visible.
18410 if Is_Tagged and then Inherit_Discr then
18411 D := First_Discriminant (Derived_Base);
18412 while Present (D) loop
18413 Set_Is_Immediately_Visible (D, False);
18414 Next_Discriminant (D);
18415 end loop;
18416 end if;
18418 return Assoc_List;
18419 end Inherit_Components;
18421 -----------------------------
18422 -- Inherit_Predicate_Flags --
18423 -----------------------------
18425 procedure Inherit_Predicate_Flags (Subt, Par : Entity_Id) is
18426 begin
18427 Set_Has_Predicates (Subt, Has_Predicates (Par));
18428 Set_Has_Static_Predicate_Aspect
18429 (Subt, Has_Static_Predicate_Aspect (Par));
18430 Set_Has_Dynamic_Predicate_Aspect
18431 (Subt, Has_Dynamic_Predicate_Aspect (Par));
18433 -- A named subtype does not inherit the predicate function of its
18434 -- parent but an itype declared for a loop index needs the discrete
18435 -- predicate information of its parent to execute the loop properly.
18437 if Is_Itype (Subt) and then Present (Predicate_Function (Par)) then
18438 Set_Subprograms_For_Type (Subt, Subprograms_For_Type (Par));
18440 if Has_Static_Predicate (Par) then
18441 Set_Static_Discrete_Predicate
18442 (Subt, Static_Discrete_Predicate (Par));
18443 end if;
18444 end if;
18445 end Inherit_Predicate_Flags;
18447 ----------------------
18448 -- Is_EVF_Procedure --
18449 ----------------------
18451 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean is
18452 Formal : Entity_Id;
18454 begin
18455 -- Examine the formals of an Extensions_Visible False procedure looking
18456 -- for a controlling OUT parameter.
18458 if Ekind (Subp) = E_Procedure
18459 and then Extensions_Visible_Status (Subp) = Extensions_Visible_False
18460 then
18461 Formal := First_Formal (Subp);
18462 while Present (Formal) loop
18463 if Ekind (Formal) = E_Out_Parameter
18464 and then Is_Controlling_Formal (Formal)
18465 then
18466 return True;
18467 end if;
18469 Next_Formal (Formal);
18470 end loop;
18471 end if;
18473 return False;
18474 end Is_EVF_Procedure;
18476 -----------------------
18477 -- Is_Null_Extension --
18478 -----------------------
18480 function Is_Null_Extension (T : Entity_Id) return Boolean is
18481 Type_Decl : constant Node_Id := Parent (Base_Type (T));
18482 Comp_List : Node_Id;
18483 Comp : Node_Id;
18485 begin
18486 if Nkind (Type_Decl) /= N_Full_Type_Declaration
18487 or else not Is_Tagged_Type (T)
18488 or else Nkind (Type_Definition (Type_Decl)) /=
18489 N_Derived_Type_Definition
18490 or else No (Record_Extension_Part (Type_Definition (Type_Decl)))
18491 then
18492 return False;
18493 end if;
18495 Comp_List :=
18496 Component_List (Record_Extension_Part (Type_Definition (Type_Decl)));
18498 if Present (Discriminant_Specifications (Type_Decl)) then
18499 return False;
18501 elsif Present (Comp_List)
18502 and then Is_Non_Empty_List (Component_Items (Comp_List))
18503 then
18504 Comp := First (Component_Items (Comp_List));
18506 -- Only user-defined components are relevant. The component list
18507 -- may also contain a parent component and internal components
18508 -- corresponding to secondary tags, but these do not determine
18509 -- whether this is a null extension.
18511 while Present (Comp) loop
18512 if Comes_From_Source (Comp) then
18513 return False;
18514 end if;
18516 Next (Comp);
18517 end loop;
18519 return True;
18521 else
18522 return True;
18523 end if;
18524 end Is_Null_Extension;
18526 ------------------------------
18527 -- Is_Valid_Constraint_Kind --
18528 ------------------------------
18530 function Is_Valid_Constraint_Kind
18531 (T_Kind : Type_Kind;
18532 Constraint_Kind : Node_Kind) return Boolean
18534 begin
18535 case T_Kind is
18536 when Enumeration_Kind
18537 | Integer_Kind
18539 return Constraint_Kind = N_Range_Constraint;
18541 when Decimal_Fixed_Point_Kind =>
18542 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
18543 N_Range_Constraint);
18545 when Ordinary_Fixed_Point_Kind =>
18546 return Nkind_In (Constraint_Kind, N_Delta_Constraint,
18547 N_Range_Constraint);
18549 when Float_Kind =>
18550 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
18551 N_Range_Constraint);
18553 when Access_Kind
18554 | Array_Kind
18555 | Class_Wide_Kind
18556 | Concurrent_Kind
18557 | Private_Kind
18558 | E_Incomplete_Type
18559 | E_Record_Subtype
18560 | E_Record_Type
18562 return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
18564 when others =>
18565 return True; -- Error will be detected later
18566 end case;
18567 end Is_Valid_Constraint_Kind;
18569 --------------------------
18570 -- Is_Visible_Component --
18571 --------------------------
18573 function Is_Visible_Component
18574 (C : Entity_Id;
18575 N : Node_Id := Empty) return Boolean
18577 Original_Comp : Entity_Id := Empty;
18578 Original_Type : Entity_Id;
18579 Type_Scope : Entity_Id;
18581 function Is_Local_Type (Typ : Entity_Id) return Boolean;
18582 -- Check whether parent type of inherited component is declared locally,
18583 -- possibly within a nested package or instance. The current scope is
18584 -- the derived record itself.
18586 -------------------
18587 -- Is_Local_Type --
18588 -------------------
18590 function Is_Local_Type (Typ : Entity_Id) return Boolean is
18591 Scop : Entity_Id;
18593 begin
18594 Scop := Scope (Typ);
18595 while Present (Scop)
18596 and then Scop /= Standard_Standard
18597 loop
18598 if Scop = Scope (Current_Scope) then
18599 return True;
18600 end if;
18602 Scop := Scope (Scop);
18603 end loop;
18605 return False;
18606 end Is_Local_Type;
18608 -- Start of processing for Is_Visible_Component
18610 begin
18611 if Ekind_In (C, E_Component, E_Discriminant) then
18612 Original_Comp := Original_Record_Component (C);
18613 end if;
18615 if No (Original_Comp) then
18617 -- Premature usage, or previous error
18619 return False;
18621 else
18622 Original_Type := Scope (Original_Comp);
18623 Type_Scope := Scope (Base_Type (Scope (C)));
18624 end if;
18626 -- This test only concerns tagged types
18628 if not Is_Tagged_Type (Original_Type) then
18629 return True;
18631 -- If it is _Parent or _Tag, there is no visibility issue
18633 elsif not Comes_From_Source (Original_Comp) then
18634 return True;
18636 -- Discriminants are visible unless the (private) type has unknown
18637 -- discriminants. If the discriminant reference is inserted for a
18638 -- discriminant check on a full view it is also visible.
18640 elsif Ekind (Original_Comp) = E_Discriminant
18641 and then
18642 (not Has_Unknown_Discriminants (Original_Type)
18643 or else (Present (N)
18644 and then Nkind (N) = N_Selected_Component
18645 and then Nkind (Prefix (N)) = N_Type_Conversion
18646 and then not Comes_From_Source (Prefix (N))))
18647 then
18648 return True;
18650 -- In the body of an instantiation, check the visibility of a component
18651 -- in case it has a homograph that is a primitive operation of a private
18652 -- type which was not visible in the generic unit.
18654 -- Should Is_Prefixed_Call be propagated from template to instance???
18656 elsif In_Instance_Body then
18657 if not Is_Tagged_Type (Original_Type)
18658 or else not Is_Private_Type (Original_Type)
18659 then
18660 return True;
18662 else
18663 declare
18664 Subp_Elmt : Elmt_Id;
18666 begin
18667 Subp_Elmt := First_Elmt (Primitive_Operations (Original_Type));
18668 while Present (Subp_Elmt) loop
18670 -- The component is hidden by a primitive operation
18672 if Chars (Node (Subp_Elmt)) = Chars (C) then
18673 return False;
18674 end if;
18676 Next_Elmt (Subp_Elmt);
18677 end loop;
18679 return True;
18680 end;
18681 end if;
18683 -- If the component has been declared in an ancestor which is currently
18684 -- a private type, then it is not visible. The same applies if the
18685 -- component's containing type is not in an open scope and the original
18686 -- component's enclosing type is a visible full view of a private type
18687 -- (which can occur in cases where an attempt is being made to reference
18688 -- a component in a sibling package that is inherited from a visible
18689 -- component of a type in an ancestor package; the component in the
18690 -- sibling package should not be visible even though the component it
18691 -- inherited from is visible). This does not apply however in the case
18692 -- where the scope of the type is a private child unit, or when the
18693 -- parent comes from a local package in which the ancestor is currently
18694 -- visible. The latter suppression of visibility is needed for cases
18695 -- that are tested in B730006.
18697 elsif Is_Private_Type (Original_Type)
18698 or else
18699 (not Is_Private_Descendant (Type_Scope)
18700 and then not In_Open_Scopes (Type_Scope)
18701 and then Has_Private_Declaration (Original_Type))
18702 then
18703 -- If the type derives from an entity in a formal package, there
18704 -- are no additional visible components.
18706 if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
18707 N_Formal_Package_Declaration
18708 then
18709 return False;
18711 -- if we are not in the private part of the current package, there
18712 -- are no additional visible components.
18714 elsif Ekind (Scope (Current_Scope)) = E_Package
18715 and then not In_Private_Part (Scope (Current_Scope))
18716 then
18717 return False;
18718 else
18719 return
18720 Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
18721 and then In_Open_Scopes (Scope (Original_Type))
18722 and then Is_Local_Type (Type_Scope);
18723 end if;
18725 -- There is another weird way in which a component may be invisible when
18726 -- the private and the full view are not derived from the same ancestor.
18727 -- Here is an example :
18729 -- type A1 is tagged record F1 : integer; end record;
18730 -- type A2 is new A1 with record F2 : integer; end record;
18731 -- type T is new A1 with private;
18732 -- private
18733 -- type T is new A2 with null record;
18735 -- In this case, the full view of T inherits F1 and F2 but the private
18736 -- view inherits only F1
18738 else
18739 declare
18740 Ancestor : Entity_Id := Scope (C);
18742 begin
18743 loop
18744 if Ancestor = Original_Type then
18745 return True;
18747 -- The ancestor may have a partial view of the original type,
18748 -- but if the full view is in scope, as in a child body, the
18749 -- component is visible.
18751 elsif In_Private_Part (Scope (Original_Type))
18752 and then Full_View (Ancestor) = Original_Type
18753 then
18754 return True;
18756 elsif Ancestor = Etype (Ancestor) then
18758 -- No further ancestors to examine
18760 return False;
18761 end if;
18763 Ancestor := Etype (Ancestor);
18764 end loop;
18765 end;
18766 end if;
18767 end Is_Visible_Component;
18769 --------------------------
18770 -- Make_Class_Wide_Type --
18771 --------------------------
18773 procedure Make_Class_Wide_Type (T : Entity_Id) is
18774 CW_Type : Entity_Id;
18775 CW_Name : Name_Id;
18776 Next_E : Entity_Id;
18778 begin
18779 if Present (Class_Wide_Type (T)) then
18781 -- The class-wide type is a partially decorated entity created for a
18782 -- unanalyzed tagged type referenced through a limited with clause.
18783 -- When the tagged type is analyzed, its class-wide type needs to be
18784 -- redecorated. Note that we reuse the entity created by Decorate_
18785 -- Tagged_Type in order to preserve all links.
18787 if Materialize_Entity (Class_Wide_Type (T)) then
18788 CW_Type := Class_Wide_Type (T);
18789 Set_Materialize_Entity (CW_Type, False);
18791 -- The class wide type can have been defined by the partial view, in
18792 -- which case everything is already done.
18794 else
18795 return;
18796 end if;
18798 -- Default case, we need to create a new class-wide type
18800 else
18801 CW_Type :=
18802 New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
18803 end if;
18805 -- Inherit root type characteristics
18807 CW_Name := Chars (CW_Type);
18808 Next_E := Next_Entity (CW_Type);
18809 Copy_Node (T, CW_Type);
18810 Set_Comes_From_Source (CW_Type, False);
18811 Set_Chars (CW_Type, CW_Name);
18812 Set_Parent (CW_Type, Parent (T));
18813 Set_Next_Entity (CW_Type, Next_E);
18815 -- Ensure we have a new freeze node for the class-wide type. The partial
18816 -- view may have freeze action of its own, requiring a proper freeze
18817 -- node, and the same freeze node cannot be shared between the two
18818 -- types.
18820 Set_Has_Delayed_Freeze (CW_Type);
18821 Set_Freeze_Node (CW_Type, Empty);
18823 -- Customize the class-wide type: It has no prim. op., it cannot be
18824 -- abstract, its Etype points back to the specific root type, and it
18825 -- cannot have any invariants.
18827 Set_Ekind (CW_Type, E_Class_Wide_Type);
18828 Set_Is_Tagged_Type (CW_Type, True);
18829 Set_Direct_Primitive_Operations (CW_Type, New_Elmt_List);
18830 Set_Is_Abstract_Type (CW_Type, False);
18831 Set_Is_Constrained (CW_Type, False);
18832 Set_Is_First_Subtype (CW_Type, Is_First_Subtype (T));
18833 Set_Default_SSO (CW_Type);
18834 Set_Has_Inheritable_Invariants (CW_Type, False);
18835 Set_Has_Inherited_Invariants (CW_Type, False);
18836 Set_Has_Own_Invariants (CW_Type, False);
18838 if Ekind (T) = E_Class_Wide_Subtype then
18839 Set_Etype (CW_Type, Etype (Base_Type (T)));
18840 else
18841 Set_Etype (CW_Type, T);
18842 end if;
18844 Set_No_Tagged_Streams_Pragma (CW_Type, No_Tagged_Streams);
18846 -- If this is the class_wide type of a constrained subtype, it does
18847 -- not have discriminants.
18849 Set_Has_Discriminants (CW_Type,
18850 Has_Discriminants (T) and then not Is_Constrained (T));
18852 Set_Has_Unknown_Discriminants (CW_Type, True);
18853 Set_Class_Wide_Type (T, CW_Type);
18854 Set_Equivalent_Type (CW_Type, Empty);
18856 -- The class-wide type of a class-wide type is itself (RM 3.9(14))
18858 Set_Class_Wide_Type (CW_Type, CW_Type);
18859 end Make_Class_Wide_Type;
18861 ----------------
18862 -- Make_Index --
18863 ----------------
18865 procedure Make_Index
18866 (N : Node_Id;
18867 Related_Nod : Node_Id;
18868 Related_Id : Entity_Id := Empty;
18869 Suffix_Index : Nat := 1;
18870 In_Iter_Schm : Boolean := False)
18872 R : Node_Id;
18873 T : Entity_Id;
18874 Def_Id : Entity_Id := Empty;
18875 Found : Boolean := False;
18877 begin
18878 -- For a discrete range used in a constrained array definition and
18879 -- defined by a range, an implicit conversion to the predefined type
18880 -- INTEGER is assumed if each bound is either a numeric literal, a named
18881 -- number, or an attribute, and the type of both bounds (prior to the
18882 -- implicit conversion) is the type universal_integer. Otherwise, both
18883 -- bounds must be of the same discrete type, other than universal
18884 -- integer; this type must be determinable independently of the
18885 -- context, but using the fact that the type must be discrete and that
18886 -- both bounds must have the same type.
18888 -- Character literals also have a universal type in the absence of
18889 -- of additional context, and are resolved to Standard_Character.
18891 if Nkind (N) = N_Range then
18893 -- The index is given by a range constraint. The bounds are known
18894 -- to be of a consistent type.
18896 if not Is_Overloaded (N) then
18897 T := Etype (N);
18899 -- For universal bounds, choose the specific predefined type
18901 if T = Universal_Integer then
18902 T := Standard_Integer;
18904 elsif T = Any_Character then
18905 Ambiguous_Character (Low_Bound (N));
18907 T := Standard_Character;
18908 end if;
18910 -- The node may be overloaded because some user-defined operators
18911 -- are available, but if a universal interpretation exists it is
18912 -- also the selected one.
18914 elsif Universal_Interpretation (N) = Universal_Integer then
18915 T := Standard_Integer;
18917 else
18918 T := Any_Type;
18920 declare
18921 Ind : Interp_Index;
18922 It : Interp;
18924 begin
18925 Get_First_Interp (N, Ind, It);
18926 while Present (It.Typ) loop
18927 if Is_Discrete_Type (It.Typ) then
18929 if Found
18930 and then not Covers (It.Typ, T)
18931 and then not Covers (T, It.Typ)
18932 then
18933 Error_Msg_N ("ambiguous bounds in discrete range", N);
18934 exit;
18935 else
18936 T := It.Typ;
18937 Found := True;
18938 end if;
18939 end if;
18941 Get_Next_Interp (Ind, It);
18942 end loop;
18944 if T = Any_Type then
18945 Error_Msg_N ("discrete type required for range", N);
18946 Set_Etype (N, Any_Type);
18947 return;
18949 elsif T = Universal_Integer then
18950 T := Standard_Integer;
18951 end if;
18952 end;
18953 end if;
18955 if not Is_Discrete_Type (T) then
18956 Error_Msg_N ("discrete type required for range", N);
18957 Set_Etype (N, Any_Type);
18958 return;
18959 end if;
18961 if Nkind (Low_Bound (N)) = N_Attribute_Reference
18962 and then Attribute_Name (Low_Bound (N)) = Name_First
18963 and then Is_Entity_Name (Prefix (Low_Bound (N)))
18964 and then Is_Type (Entity (Prefix (Low_Bound (N))))
18965 and then Is_Discrete_Type (Entity (Prefix (Low_Bound (N))))
18966 then
18967 -- The type of the index will be the type of the prefix, as long
18968 -- as the upper bound is 'Last of the same type.
18970 Def_Id := Entity (Prefix (Low_Bound (N)));
18972 if Nkind (High_Bound (N)) /= N_Attribute_Reference
18973 or else Attribute_Name (High_Bound (N)) /= Name_Last
18974 or else not Is_Entity_Name (Prefix (High_Bound (N)))
18975 or else Entity (Prefix (High_Bound (N))) /= Def_Id
18976 then
18977 Def_Id := Empty;
18978 end if;
18979 end if;
18981 R := N;
18982 Process_Range_Expr_In_Decl (R, T, In_Iter_Schm => In_Iter_Schm);
18984 elsif Nkind (N) = N_Subtype_Indication then
18986 -- The index is given by a subtype with a range constraint
18988 T := Base_Type (Entity (Subtype_Mark (N)));
18990 if not Is_Discrete_Type (T) then
18991 Error_Msg_N ("discrete type required for range", N);
18992 Set_Etype (N, Any_Type);
18993 return;
18994 end if;
18996 R := Range_Expression (Constraint (N));
18998 Resolve (R, T);
18999 Process_Range_Expr_In_Decl
19000 (R, Entity (Subtype_Mark (N)), In_Iter_Schm => In_Iter_Schm);
19002 elsif Nkind (N) = N_Attribute_Reference then
19004 -- Catch beginner's error (use of attribute other than 'Range)
19006 if Attribute_Name (N) /= Name_Range then
19007 Error_Msg_N ("expect attribute ''Range", N);
19008 Set_Etype (N, Any_Type);
19009 return;
19010 end if;
19012 -- If the node denotes the range of a type mark, that is also the
19013 -- resulting type, and we do not need to create an Itype for it.
19015 if Is_Entity_Name (Prefix (N))
19016 and then Comes_From_Source (N)
19017 and then Is_Type (Entity (Prefix (N)))
19018 and then Is_Discrete_Type (Entity (Prefix (N)))
19019 then
19020 Def_Id := Entity (Prefix (N));
19021 end if;
19023 Analyze_And_Resolve (N);
19024 T := Etype (N);
19025 R := N;
19027 -- If none of the above, must be a subtype. We convert this to a
19028 -- range attribute reference because in the case of declared first
19029 -- named subtypes, the types in the range reference can be different
19030 -- from the type of the entity. A range attribute normalizes the
19031 -- reference and obtains the correct types for the bounds.
19033 -- This transformation is in the nature of an expansion, is only
19034 -- done if expansion is active. In particular, it is not done on
19035 -- formal generic types, because we need to retain the name of the
19036 -- original index for instantiation purposes.
19038 else
19039 if not Is_Entity_Name (N) or else not Is_Type (Entity (N)) then
19040 Error_Msg_N ("invalid subtype mark in discrete range ", N);
19041 Set_Etype (N, Any_Integer);
19042 return;
19044 else
19045 -- The type mark may be that of an incomplete type. It is only
19046 -- now that we can get the full view, previous analysis does
19047 -- not look specifically for a type mark.
19049 Set_Entity (N, Get_Full_View (Entity (N)));
19050 Set_Etype (N, Entity (N));
19051 Def_Id := Entity (N);
19053 if not Is_Discrete_Type (Def_Id) then
19054 Error_Msg_N ("discrete type required for index", N);
19055 Set_Etype (N, Any_Type);
19056 return;
19057 end if;
19058 end if;
19060 if Expander_Active then
19061 Rewrite (N,
19062 Make_Attribute_Reference (Sloc (N),
19063 Attribute_Name => Name_Range,
19064 Prefix => Relocate_Node (N)));
19066 -- The original was a subtype mark that does not freeze. This
19067 -- means that the rewritten version must not freeze either.
19069 Set_Must_Not_Freeze (N);
19070 Set_Must_Not_Freeze (Prefix (N));
19071 Analyze_And_Resolve (N);
19072 T := Etype (N);
19073 R := N;
19075 -- If expander is inactive, type is legal, nothing else to construct
19077 else
19078 return;
19079 end if;
19080 end if;
19082 if not Is_Discrete_Type (T) then
19083 Error_Msg_N ("discrete type required for range", N);
19084 Set_Etype (N, Any_Type);
19085 return;
19087 elsif T = Any_Type then
19088 Set_Etype (N, Any_Type);
19089 return;
19090 end if;
19092 -- We will now create the appropriate Itype to describe the range, but
19093 -- first a check. If we originally had a subtype, then we just label
19094 -- the range with this subtype. Not only is there no need to construct
19095 -- a new subtype, but it is wrong to do so for two reasons:
19097 -- 1. A legality concern, if we have a subtype, it must not freeze,
19098 -- and the Itype would cause freezing incorrectly
19100 -- 2. An efficiency concern, if we created an Itype, it would not be
19101 -- recognized as the same type for the purposes of eliminating
19102 -- checks in some circumstances.
19104 -- We signal this case by setting the subtype entity in Def_Id
19106 if No (Def_Id) then
19107 Def_Id :=
19108 Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
19109 Set_Etype (Def_Id, Base_Type (T));
19111 if Is_Signed_Integer_Type (T) then
19112 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
19114 elsif Is_Modular_Integer_Type (T) then
19115 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
19117 else
19118 Set_Ekind (Def_Id, E_Enumeration_Subtype);
19119 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
19120 Set_First_Literal (Def_Id, First_Literal (T));
19121 end if;
19123 Set_Size_Info (Def_Id, (T));
19124 Set_RM_Size (Def_Id, RM_Size (T));
19125 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
19127 Set_Scalar_Range (Def_Id, R);
19128 Conditional_Delay (Def_Id, T);
19130 if Nkind (N) = N_Subtype_Indication then
19131 Inherit_Predicate_Flags (Def_Id, Entity (Subtype_Mark (N)));
19132 end if;
19134 -- In the subtype indication case, if the immediate parent of the
19135 -- new subtype is non-static, then the subtype we create is non-
19136 -- static, even if its bounds are static.
19138 if Nkind (N) = N_Subtype_Indication
19139 and then not Is_OK_Static_Subtype (Entity (Subtype_Mark (N)))
19140 then
19141 Set_Is_Non_Static_Subtype (Def_Id);
19142 end if;
19143 end if;
19145 -- Final step is to label the index with this constructed type
19147 Set_Etype (N, Def_Id);
19148 end Make_Index;
19150 ------------------------------
19151 -- Modular_Type_Declaration --
19152 ------------------------------
19154 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
19155 Mod_Expr : constant Node_Id := Expression (Def);
19156 M_Val : Uint;
19158 procedure Set_Modular_Size (Bits : Int);
19159 -- Sets RM_Size to Bits, and Esize to normal word size above this
19161 ----------------------
19162 -- Set_Modular_Size --
19163 ----------------------
19165 procedure Set_Modular_Size (Bits : Int) is
19166 begin
19167 Set_RM_Size (T, UI_From_Int (Bits));
19169 if Bits <= 8 then
19170 Init_Esize (T, 8);
19172 elsif Bits <= 16 then
19173 Init_Esize (T, 16);
19175 elsif Bits <= 32 then
19176 Init_Esize (T, 32);
19178 else
19179 Init_Esize (T, System_Max_Binary_Modulus_Power);
19180 end if;
19182 if not Non_Binary_Modulus (T) and then Esize (T) = RM_Size (T) then
19183 Set_Is_Known_Valid (T);
19184 end if;
19185 end Set_Modular_Size;
19187 -- Start of processing for Modular_Type_Declaration
19189 begin
19190 -- If the mod expression is (exactly) 2 * literal, where literal is
19191 -- 64 or less,then almost certainly the * was meant to be **. Warn.
19193 if Warn_On_Suspicious_Modulus_Value
19194 and then Nkind (Mod_Expr) = N_Op_Multiply
19195 and then Nkind (Left_Opnd (Mod_Expr)) = N_Integer_Literal
19196 and then Intval (Left_Opnd (Mod_Expr)) = Uint_2
19197 and then Nkind (Right_Opnd (Mod_Expr)) = N_Integer_Literal
19198 and then Intval (Right_Opnd (Mod_Expr)) <= Uint_64
19199 then
19200 Error_Msg_N
19201 ("suspicious MOD value, was '*'* intended'??M?", Mod_Expr);
19202 end if;
19204 -- Proceed with analysis of mod expression
19206 Analyze_And_Resolve (Mod_Expr, Any_Integer);
19207 Set_Etype (T, T);
19208 Set_Ekind (T, E_Modular_Integer_Type);
19209 Init_Alignment (T);
19210 Set_Is_Constrained (T);
19212 if not Is_OK_Static_Expression (Mod_Expr) then
19213 Flag_Non_Static_Expr
19214 ("non-static expression used for modular type bound!", Mod_Expr);
19215 M_Val := 2 ** System_Max_Binary_Modulus_Power;
19216 else
19217 M_Val := Expr_Value (Mod_Expr);
19218 end if;
19220 if M_Val < 1 then
19221 Error_Msg_N ("modulus value must be positive", Mod_Expr);
19222 M_Val := 2 ** System_Max_Binary_Modulus_Power;
19223 end if;
19225 if M_Val > 2 ** Standard_Long_Integer_Size then
19226 Check_Restriction (No_Long_Long_Integers, Mod_Expr);
19227 end if;
19229 Set_Modulus (T, M_Val);
19231 -- Create bounds for the modular type based on the modulus given in
19232 -- the type declaration and then analyze and resolve those bounds.
19234 Set_Scalar_Range (T,
19235 Make_Range (Sloc (Mod_Expr),
19236 Low_Bound => Make_Integer_Literal (Sloc (Mod_Expr), 0),
19237 High_Bound => Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
19239 -- Properly analyze the literals for the range. We do this manually
19240 -- because we can't go calling Resolve, since we are resolving these
19241 -- bounds with the type, and this type is certainly not complete yet.
19243 Set_Etype (Low_Bound (Scalar_Range (T)), T);
19244 Set_Etype (High_Bound (Scalar_Range (T)), T);
19245 Set_Is_Static_Expression (Low_Bound (Scalar_Range (T)));
19246 Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
19248 -- Loop through powers of two to find number of bits required
19250 for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
19252 -- Binary case
19254 if M_Val = 2 ** Bits then
19255 Set_Modular_Size (Bits);
19256 return;
19258 -- Nonbinary case
19260 elsif M_Val < 2 ** Bits then
19261 Check_SPARK_05_Restriction ("modulus should be a power of 2", T);
19262 Set_Non_Binary_Modulus (T);
19264 if Bits > System_Max_Nonbinary_Modulus_Power then
19265 Error_Msg_Uint_1 :=
19266 UI_From_Int (System_Max_Nonbinary_Modulus_Power);
19267 Error_Msg_F
19268 ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
19269 Set_Modular_Size (System_Max_Binary_Modulus_Power);
19270 return;
19272 else
19273 -- In the nonbinary case, set size as per RM 13.3(55)
19275 Set_Modular_Size (Bits);
19276 return;
19277 end if;
19278 end if;
19280 end loop;
19282 -- If we fall through, then the size exceed System.Max_Binary_Modulus
19283 -- so we just signal an error and set the maximum size.
19285 Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
19286 Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
19288 Set_Modular_Size (System_Max_Binary_Modulus_Power);
19289 Init_Alignment (T);
19291 end Modular_Type_Declaration;
19293 --------------------------
19294 -- New_Concatenation_Op --
19295 --------------------------
19297 procedure New_Concatenation_Op (Typ : Entity_Id) is
19298 Loc : constant Source_Ptr := Sloc (Typ);
19299 Op : Entity_Id;
19301 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
19302 -- Create abbreviated declaration for the formal of a predefined
19303 -- Operator 'Op' of type 'Typ'
19305 --------------------
19306 -- Make_Op_Formal --
19307 --------------------
19309 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
19310 Formal : Entity_Id;
19311 begin
19312 Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
19313 Set_Etype (Formal, Typ);
19314 Set_Mechanism (Formal, Default_Mechanism);
19315 return Formal;
19316 end Make_Op_Formal;
19318 -- Start of processing for New_Concatenation_Op
19320 begin
19321 Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
19323 Set_Ekind (Op, E_Operator);
19324 Set_Scope (Op, Current_Scope);
19325 Set_Etype (Op, Typ);
19326 Set_Homonym (Op, Get_Name_Entity_Id (Name_Op_Concat));
19327 Set_Is_Immediately_Visible (Op);
19328 Set_Is_Intrinsic_Subprogram (Op);
19329 Set_Has_Completion (Op);
19330 Append_Entity (Op, Current_Scope);
19332 Set_Name_Entity_Id (Name_Op_Concat, Op);
19334 Append_Entity (Make_Op_Formal (Typ, Op), Op);
19335 Append_Entity (Make_Op_Formal (Typ, Op), Op);
19336 end New_Concatenation_Op;
19338 -------------------------
19339 -- OK_For_Limited_Init --
19340 -------------------------
19342 -- ???Check all calls of this, and compare the conditions under which it's
19343 -- called.
19345 function OK_For_Limited_Init
19346 (Typ : Entity_Id;
19347 Exp : Node_Id) return Boolean
19349 begin
19350 return Is_CPP_Constructor_Call (Exp)
19351 or else (Ada_Version >= Ada_2005
19352 and then not Debug_Flag_Dot_L
19353 and then OK_For_Limited_Init_In_05 (Typ, Exp));
19354 end OK_For_Limited_Init;
19356 -------------------------------
19357 -- OK_For_Limited_Init_In_05 --
19358 -------------------------------
19360 function OK_For_Limited_Init_In_05
19361 (Typ : Entity_Id;
19362 Exp : Node_Id) return Boolean
19364 begin
19365 -- An object of a limited interface type can be initialized with any
19366 -- expression of a nonlimited descendant type. However this does not
19367 -- apply if this is a view conversion of some other expression. This
19368 -- is checked below.
19370 if Is_Class_Wide_Type (Typ)
19371 and then Is_Limited_Interface (Typ)
19372 and then not Is_Limited_Type (Etype (Exp))
19373 and then Nkind (Exp) /= N_Type_Conversion
19374 then
19375 return True;
19376 end if;
19378 -- Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
19379 -- case of limited aggregates (including extension aggregates), and
19380 -- function calls. The function call may have been given in prefixed
19381 -- notation, in which case the original node is an indexed component.
19382 -- If the function is parameterless, the original node was an explicit
19383 -- dereference. The function may also be parameterless, in which case
19384 -- the source node is just an identifier.
19386 -- A branch of a conditional expression may have been removed if the
19387 -- condition is statically known. This happens during expansion, and
19388 -- thus will not happen if previous errors were encountered. The check
19389 -- will have been performed on the chosen branch, which replaces the
19390 -- original conditional expression.
19392 if No (Exp) then
19393 return True;
19394 end if;
19396 case Nkind (Original_Node (Exp)) is
19397 when N_Aggregate
19398 | N_Extension_Aggregate
19399 | N_Function_Call
19400 | N_Op
19402 return True;
19404 when N_Identifier =>
19405 return Present (Entity (Original_Node (Exp)))
19406 and then Ekind (Entity (Original_Node (Exp))) = E_Function;
19408 when N_Qualified_Expression =>
19409 return
19410 OK_For_Limited_Init_In_05
19411 (Typ, Expression (Original_Node (Exp)));
19413 -- Ada 2005 (AI-251): If a class-wide interface object is initialized
19414 -- with a function call, the expander has rewritten the call into an
19415 -- N_Type_Conversion node to force displacement of the pointer to
19416 -- reference the component containing the secondary dispatch table.
19417 -- Otherwise a type conversion is not a legal context.
19418 -- A return statement for a build-in-place function returning a
19419 -- synchronized type also introduces an unchecked conversion.
19421 when N_Type_Conversion
19422 | N_Unchecked_Type_Conversion
19424 return not Comes_From_Source (Exp)
19425 and then
19426 OK_For_Limited_Init_In_05
19427 (Typ, Expression (Original_Node (Exp)));
19429 when N_Explicit_Dereference
19430 | N_Indexed_Component
19431 | N_Selected_Component
19433 return Nkind (Exp) = N_Function_Call;
19435 -- A use of 'Input is a function call, hence allowed. Normally the
19436 -- attribute will be changed to a call, but the attribute by itself
19437 -- can occur with -gnatc.
19439 when N_Attribute_Reference =>
19440 return Attribute_Name (Original_Node (Exp)) = Name_Input;
19442 -- "return raise ..." is OK
19444 when N_Raise_Expression =>
19445 return True;
19447 -- For a case expression, all dependent expressions must be legal
19449 when N_Case_Expression =>
19450 declare
19451 Alt : Node_Id;
19453 begin
19454 Alt := First (Alternatives (Original_Node (Exp)));
19455 while Present (Alt) loop
19456 if not OK_For_Limited_Init_In_05 (Typ, Expression (Alt)) then
19457 return False;
19458 end if;
19460 Next (Alt);
19461 end loop;
19463 return True;
19464 end;
19466 -- For an if expression, all dependent expressions must be legal
19468 when N_If_Expression =>
19469 declare
19470 Then_Expr : constant Node_Id :=
19471 Next (First (Expressions (Original_Node (Exp))));
19472 Else_Expr : constant Node_Id := Next (Then_Expr);
19473 begin
19474 return OK_For_Limited_Init_In_05 (Typ, Then_Expr)
19475 and then
19476 OK_For_Limited_Init_In_05 (Typ, Else_Expr);
19477 end;
19479 when others =>
19480 return False;
19481 end case;
19482 end OK_For_Limited_Init_In_05;
19484 -------------------------------------------
19485 -- Ordinary_Fixed_Point_Type_Declaration --
19486 -------------------------------------------
19488 procedure Ordinary_Fixed_Point_Type_Declaration
19489 (T : Entity_Id;
19490 Def : Node_Id)
19492 Loc : constant Source_Ptr := Sloc (Def);
19493 Delta_Expr : constant Node_Id := Delta_Expression (Def);
19494 RRS : constant Node_Id := Real_Range_Specification (Def);
19495 Implicit_Base : Entity_Id;
19496 Delta_Val : Ureal;
19497 Small_Val : Ureal;
19498 Low_Val : Ureal;
19499 High_Val : Ureal;
19501 begin
19502 Check_Restriction (No_Fixed_Point, Def);
19504 -- Create implicit base type
19506 Implicit_Base :=
19507 Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
19508 Set_Etype (Implicit_Base, Implicit_Base);
19510 -- Analyze and process delta expression
19512 Analyze_And_Resolve (Delta_Expr, Any_Real);
19514 Check_Delta_Expression (Delta_Expr);
19515 Delta_Val := Expr_Value_R (Delta_Expr);
19517 Set_Delta_Value (Implicit_Base, Delta_Val);
19519 -- Compute default small from given delta, which is the largest power
19520 -- of two that does not exceed the given delta value.
19522 declare
19523 Tmp : Ureal;
19524 Scale : Int;
19526 begin
19527 Tmp := Ureal_1;
19528 Scale := 0;
19530 if Delta_Val < Ureal_1 then
19531 while Delta_Val < Tmp loop
19532 Tmp := Tmp / Ureal_2;
19533 Scale := Scale + 1;
19534 end loop;
19536 else
19537 loop
19538 Tmp := Tmp * Ureal_2;
19539 exit when Tmp > Delta_Val;
19540 Scale := Scale - 1;
19541 end loop;
19542 end if;
19544 Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
19545 end;
19547 Set_Small_Value (Implicit_Base, Small_Val);
19549 -- If no range was given, set a dummy range
19551 if RRS <= Empty_Or_Error then
19552 Low_Val := -Small_Val;
19553 High_Val := Small_Val;
19555 -- Otherwise analyze and process given range
19557 else
19558 declare
19559 Low : constant Node_Id := Low_Bound (RRS);
19560 High : constant Node_Id := High_Bound (RRS);
19562 begin
19563 Analyze_And_Resolve (Low, Any_Real);
19564 Analyze_And_Resolve (High, Any_Real);
19565 Check_Real_Bound (Low);
19566 Check_Real_Bound (High);
19568 -- Obtain and set the range
19570 Low_Val := Expr_Value_R (Low);
19571 High_Val := Expr_Value_R (High);
19573 if Low_Val > High_Val then
19574 Error_Msg_NE ("??fixed point type& has null range", Def, T);
19575 end if;
19576 end;
19577 end if;
19579 -- The range for both the implicit base and the declared first subtype
19580 -- cannot be set yet, so we use the special routine Set_Fixed_Range to
19581 -- set a temporary range in place. Note that the bounds of the base
19582 -- type will be widened to be symmetrical and to fill the available
19583 -- bits when the type is frozen.
19585 -- We could do this with all discrete types, and probably should, but
19586 -- we absolutely have to do it for fixed-point, since the end-points
19587 -- of the range and the size are determined by the small value, which
19588 -- could be reset before the freeze point.
19590 Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
19591 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
19593 -- Complete definition of first subtype. The inheritance of the rep item
19594 -- chain ensures that SPARK-related pragmas are not clobbered when the
19595 -- ordinary fixed point type acts as a full view of a private type.
19597 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
19598 Set_Etype (T, Implicit_Base);
19599 Init_Size_Align (T);
19600 Inherit_Rep_Item_Chain (T, Implicit_Base);
19601 Set_Small_Value (T, Small_Val);
19602 Set_Delta_Value (T, Delta_Val);
19603 Set_Is_Constrained (T);
19604 end Ordinary_Fixed_Point_Type_Declaration;
19606 ----------------------------------
19607 -- Preanalyze_Assert_Expression --
19608 ----------------------------------
19610 procedure Preanalyze_Assert_Expression (N : Node_Id; T : Entity_Id) is
19611 begin
19612 In_Assertion_Expr := In_Assertion_Expr + 1;
19613 Preanalyze_Spec_Expression (N, T);
19614 In_Assertion_Expr := In_Assertion_Expr - 1;
19615 end Preanalyze_Assert_Expression;
19617 -----------------------------------
19618 -- Preanalyze_Default_Expression --
19619 -----------------------------------
19621 procedure Preanalyze_Default_Expression (N : Node_Id; T : Entity_Id) is
19622 Save_In_Default_Expr : constant Boolean := In_Default_Expr;
19623 begin
19624 In_Default_Expr := True;
19625 Preanalyze_Spec_Expression (N, T);
19626 In_Default_Expr := Save_In_Default_Expr;
19627 end Preanalyze_Default_Expression;
19629 --------------------------------
19630 -- Preanalyze_Spec_Expression --
19631 --------------------------------
19633 procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
19634 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
19635 begin
19636 In_Spec_Expression := True;
19637 Preanalyze_And_Resolve (N, T);
19638 In_Spec_Expression := Save_In_Spec_Expression;
19639 end Preanalyze_Spec_Expression;
19641 ----------------------------------------
19642 -- Prepare_Private_Subtype_Completion --
19643 ----------------------------------------
19645 procedure Prepare_Private_Subtype_Completion
19646 (Id : Entity_Id;
19647 Related_Nod : Node_Id)
19649 Id_B : constant Entity_Id := Base_Type (Id);
19650 Full_B : Entity_Id := Full_View (Id_B);
19651 Full : Entity_Id;
19653 begin
19654 if Present (Full_B) then
19656 -- Get to the underlying full view if necessary
19658 if Is_Private_Type (Full_B)
19659 and then Present (Underlying_Full_View (Full_B))
19660 then
19661 Full_B := Underlying_Full_View (Full_B);
19662 end if;
19664 -- The Base_Type is already completed, we can complete the subtype
19665 -- now. We have to create a new entity with the same name, Thus we
19666 -- can't use Create_Itype.
19668 Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
19669 Set_Is_Itype (Full);
19670 Set_Associated_Node_For_Itype (Full, Related_Nod);
19671 Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
19672 end if;
19674 -- The parent subtype may be private, but the base might not, in some
19675 -- nested instances. In that case, the subtype does not need to be
19676 -- exchanged. It would still be nice to make private subtypes and their
19677 -- bases consistent at all times ???
19679 if Is_Private_Type (Id_B) then
19680 Append_Elmt (Id, Private_Dependents (Id_B));
19681 end if;
19682 end Prepare_Private_Subtype_Completion;
19684 ---------------------------
19685 -- Process_Discriminants --
19686 ---------------------------
19688 procedure Process_Discriminants
19689 (N : Node_Id;
19690 Prev : Entity_Id := Empty)
19692 Elist : constant Elist_Id := New_Elmt_List;
19693 Id : Node_Id;
19694 Discr : Node_Id;
19695 Discr_Number : Uint;
19696 Discr_Type : Entity_Id;
19697 Default_Present : Boolean := False;
19698 Default_Not_Present : Boolean := False;
19700 begin
19701 -- A composite type other than an array type can have discriminants.
19702 -- On entry, the current scope is the composite type.
19704 -- The discriminants are initially entered into the scope of the type
19705 -- via Enter_Name with the default Ekind of E_Void to prevent premature
19706 -- use, as explained at the end of this procedure.
19708 Discr := First (Discriminant_Specifications (N));
19709 while Present (Discr) loop
19710 Enter_Name (Defining_Identifier (Discr));
19712 -- For navigation purposes we add a reference to the discriminant
19713 -- in the entity for the type. If the current declaration is a
19714 -- completion, place references on the partial view. Otherwise the
19715 -- type is the current scope.
19717 if Present (Prev) then
19719 -- The references go on the partial view, if present. If the
19720 -- partial view has discriminants, the references have been
19721 -- generated already.
19723 if not Has_Discriminants (Prev) then
19724 Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
19725 end if;
19726 else
19727 Generate_Reference
19728 (Current_Scope, Defining_Identifier (Discr), 'd');
19729 end if;
19731 if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
19732 Discr_Type := Access_Definition (Discr, Discriminant_Type (Discr));
19734 -- Ada 2005 (AI-254)
19736 if Present (Access_To_Subprogram_Definition
19737 (Discriminant_Type (Discr)))
19738 and then Protected_Present (Access_To_Subprogram_Definition
19739 (Discriminant_Type (Discr)))
19740 then
19741 Discr_Type :=
19742 Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
19743 end if;
19745 else
19746 Find_Type (Discriminant_Type (Discr));
19747 Discr_Type := Etype (Discriminant_Type (Discr));
19749 if Error_Posted (Discriminant_Type (Discr)) then
19750 Discr_Type := Any_Type;
19751 end if;
19752 end if;
19754 -- Handling of discriminants that are access types
19756 if Is_Access_Type (Discr_Type) then
19758 -- Ada 2005 (AI-230): Access discriminant allowed in non-
19759 -- limited record types
19761 if Ada_Version < Ada_2005 then
19762 Check_Access_Discriminant_Requires_Limited
19763 (Discr, Discriminant_Type (Discr));
19764 end if;
19766 if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
19767 Error_Msg_N
19768 ("(Ada 83) access discriminant not allowed", Discr);
19769 end if;
19771 -- If not access type, must be a discrete type
19773 elsif not Is_Discrete_Type (Discr_Type) then
19774 Error_Msg_N
19775 ("discriminants must have a discrete or access type",
19776 Discriminant_Type (Discr));
19777 end if;
19779 Set_Etype (Defining_Identifier (Discr), Discr_Type);
19781 -- If a discriminant specification includes the assignment compound
19782 -- delimiter followed by an expression, the expression is the default
19783 -- expression of the discriminant; the default expression must be of
19784 -- the type of the discriminant. (RM 3.7.1) Since this expression is
19785 -- a default expression, we do the special preanalysis, since this
19786 -- expression does not freeze (see section "Handling of Default and
19787 -- Per-Object Expressions" in spec of package Sem).
19789 if Present (Expression (Discr)) then
19790 Preanalyze_Spec_Expression (Expression (Discr), Discr_Type);
19792 -- Legaity checks
19794 if Nkind (N) = N_Formal_Type_Declaration then
19795 Error_Msg_N
19796 ("discriminant defaults not allowed for formal type",
19797 Expression (Discr));
19799 -- Flag an error for a tagged type with defaulted discriminants,
19800 -- excluding limited tagged types when compiling for Ada 2012
19801 -- (see AI05-0214).
19803 elsif Is_Tagged_Type (Current_Scope)
19804 and then (not Is_Limited_Type (Current_Scope)
19805 or else Ada_Version < Ada_2012)
19806 and then Comes_From_Source (N)
19807 then
19808 -- Note: see similar test in Check_Or_Process_Discriminants, to
19809 -- handle the (illegal) case of the completion of an untagged
19810 -- view with discriminants with defaults by a tagged full view.
19811 -- We skip the check if Discr does not come from source, to
19812 -- account for the case of an untagged derived type providing
19813 -- defaults for a renamed discriminant from a private untagged
19814 -- ancestor with a tagged full view (ACATS B460006).
19816 if Ada_Version >= Ada_2012 then
19817 Error_Msg_N
19818 ("discriminants of nonlimited tagged type cannot have"
19819 & " defaults",
19820 Expression (Discr));
19821 else
19822 Error_Msg_N
19823 ("discriminants of tagged type cannot have defaults",
19824 Expression (Discr));
19825 end if;
19827 else
19828 Default_Present := True;
19829 Append_Elmt (Expression (Discr), Elist);
19831 -- Tag the defining identifiers for the discriminants with
19832 -- their corresponding default expressions from the tree.
19834 Set_Discriminant_Default_Value
19835 (Defining_Identifier (Discr), Expression (Discr));
19836 end if;
19838 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag
19839 -- gets set unless we can be sure that no range check is required.
19841 if (GNATprove_Mode or not Expander_Active)
19842 and then not
19843 Is_In_Range
19844 (Expression (Discr), Discr_Type, Assume_Valid => True)
19845 then
19846 Set_Do_Range_Check (Expression (Discr));
19847 end if;
19849 -- No default discriminant value given
19851 else
19852 Default_Not_Present := True;
19853 end if;
19855 -- Ada 2005 (AI-231): Create an Itype that is a duplicate of
19856 -- Discr_Type but with the null-exclusion attribute
19858 if Ada_Version >= Ada_2005 then
19860 -- Ada 2005 (AI-231): Static checks
19862 if Can_Never_Be_Null (Discr_Type) then
19863 Null_Exclusion_Static_Checks (Discr);
19865 elsif Is_Access_Type (Discr_Type)
19866 and then Null_Exclusion_Present (Discr)
19868 -- No need to check itypes because in their case this check
19869 -- was done at their point of creation
19871 and then not Is_Itype (Discr_Type)
19872 then
19873 if Can_Never_Be_Null (Discr_Type) then
19874 Error_Msg_NE
19875 ("`NOT NULL` not allowed (& already excludes null)",
19876 Discr,
19877 Discr_Type);
19878 end if;
19880 Set_Etype (Defining_Identifier (Discr),
19881 Create_Null_Excluding_Itype
19882 (T => Discr_Type,
19883 Related_Nod => Discr));
19885 -- Check for improper null exclusion if the type is otherwise
19886 -- legal for a discriminant.
19888 elsif Null_Exclusion_Present (Discr)
19889 and then Is_Discrete_Type (Discr_Type)
19890 then
19891 Error_Msg_N
19892 ("null exclusion can only apply to an access type", Discr);
19893 end if;
19895 -- Ada 2005 (AI-402): access discriminants of nonlimited types
19896 -- can't have defaults. Synchronized types, or types that are
19897 -- explicitly limited are fine, but special tests apply to derived
19898 -- types in generics: in a generic body we have to assume the
19899 -- worst, and therefore defaults are not allowed if the parent is
19900 -- a generic formal private type (see ACATS B370001).
19902 if Is_Access_Type (Discr_Type) and then Default_Present then
19903 if Ekind (Discr_Type) /= E_Anonymous_Access_Type
19904 or else Is_Limited_Record (Current_Scope)
19905 or else Is_Concurrent_Type (Current_Scope)
19906 or else Is_Concurrent_Record_Type (Current_Scope)
19907 or else Ekind (Current_Scope) = E_Limited_Private_Type
19908 then
19909 if not Is_Derived_Type (Current_Scope)
19910 or else not Is_Generic_Type (Etype (Current_Scope))
19911 or else not In_Package_Body (Scope (Etype (Current_Scope)))
19912 or else Limited_Present
19913 (Type_Definition (Parent (Current_Scope)))
19914 then
19915 null;
19917 else
19918 Error_Msg_N
19919 ("access discriminants of nonlimited types cannot "
19920 & "have defaults", Expression (Discr));
19921 end if;
19923 elsif Present (Expression (Discr)) then
19924 Error_Msg_N
19925 ("(Ada 2005) access discriminants of nonlimited types "
19926 & "cannot have defaults", Expression (Discr));
19927 end if;
19928 end if;
19929 end if;
19931 -- A discriminant cannot be effectively volatile (SPARK RM 7.1.3(6)).
19932 -- This check is relevant only when SPARK_Mode is on as it is not a
19933 -- standard Ada legality rule.
19935 if SPARK_Mode = On
19936 and then Is_Effectively_Volatile (Defining_Identifier (Discr))
19937 then
19938 Error_Msg_N ("discriminant cannot be volatile", Discr);
19939 end if;
19941 Next (Discr);
19942 end loop;
19944 -- An element list consisting of the default expressions of the
19945 -- discriminants is constructed in the above loop and used to set
19946 -- the Discriminant_Constraint attribute for the type. If an object
19947 -- is declared of this (record or task) type without any explicit
19948 -- discriminant constraint given, this element list will form the
19949 -- actual parameters for the corresponding initialization procedure
19950 -- for the type.
19952 Set_Discriminant_Constraint (Current_Scope, Elist);
19953 Set_Stored_Constraint (Current_Scope, No_Elist);
19955 -- Default expressions must be provided either for all or for none
19956 -- of the discriminants of a discriminant part. (RM 3.7.1)
19958 if Default_Present and then Default_Not_Present then
19959 Error_Msg_N
19960 ("incomplete specification of defaults for discriminants", N);
19961 end if;
19963 -- The use of the name of a discriminant is not allowed in default
19964 -- expressions of a discriminant part if the specification of the
19965 -- discriminant is itself given in the discriminant part. (RM 3.7.1)
19967 -- To detect this, the discriminant names are entered initially with an
19968 -- Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
19969 -- attempt to use a void entity (for example in an expression that is
19970 -- type-checked) produces the error message: premature usage. Now after
19971 -- completing the semantic analysis of the discriminant part, we can set
19972 -- the Ekind of all the discriminants appropriately.
19974 Discr := First (Discriminant_Specifications (N));
19975 Discr_Number := Uint_1;
19976 while Present (Discr) loop
19977 Id := Defining_Identifier (Discr);
19978 Set_Ekind (Id, E_Discriminant);
19979 Init_Component_Location (Id);
19980 Init_Esize (Id);
19981 Set_Discriminant_Number (Id, Discr_Number);
19983 -- Make sure this is always set, even in illegal programs
19985 Set_Corresponding_Discriminant (Id, Empty);
19987 -- Initialize the Original_Record_Component to the entity itself.
19988 -- Inherit_Components will propagate the right value to
19989 -- discriminants in derived record types.
19991 Set_Original_Record_Component (Id, Id);
19993 -- Create the discriminal for the discriminant
19995 Build_Discriminal (Id);
19997 Next (Discr);
19998 Discr_Number := Discr_Number + 1;
19999 end loop;
20001 Set_Has_Discriminants (Current_Scope);
20002 end Process_Discriminants;
20004 -----------------------
20005 -- Process_Full_View --
20006 -----------------------
20008 -- WARNING: This routine manages Ghost regions. Return statements must be
20009 -- replaced by gotos which jump to the end of the routine and restore the
20010 -- Ghost mode.
20012 procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
20013 procedure Collect_Implemented_Interfaces
20014 (Typ : Entity_Id;
20015 Ifaces : Elist_Id);
20016 -- Ada 2005: Gather all the interfaces that Typ directly or
20017 -- inherently implements. Duplicate entries are not added to
20018 -- the list Ifaces.
20020 ------------------------------------
20021 -- Collect_Implemented_Interfaces --
20022 ------------------------------------
20024 procedure Collect_Implemented_Interfaces
20025 (Typ : Entity_Id;
20026 Ifaces : Elist_Id)
20028 Iface : Entity_Id;
20029 Iface_Elmt : Elmt_Id;
20031 begin
20032 -- Abstract interfaces are only associated with tagged record types
20034 if not Is_Tagged_Type (Typ) or else not Is_Record_Type (Typ) then
20035 return;
20036 end if;
20038 -- Recursively climb to the ancestors
20040 if Etype (Typ) /= Typ
20042 -- Protect the frontend against wrong cyclic declarations like:
20044 -- type B is new A with private;
20045 -- type C is new A with private;
20046 -- private
20047 -- type B is new C with null record;
20048 -- type C is new B with null record;
20050 and then Etype (Typ) /= Priv_T
20051 and then Etype (Typ) /= Full_T
20052 then
20053 -- Keep separate the management of private type declarations
20055 if Ekind (Typ) = E_Record_Type_With_Private then
20057 -- Handle the following illegal usage:
20058 -- type Private_Type is tagged private;
20059 -- private
20060 -- type Private_Type is new Type_Implementing_Iface;
20062 if Present (Full_View (Typ))
20063 and then Etype (Typ) /= Full_View (Typ)
20064 then
20065 if Is_Interface (Etype (Typ)) then
20066 Append_Unique_Elmt (Etype (Typ), Ifaces);
20067 end if;
20069 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
20070 end if;
20072 -- Non-private types
20074 else
20075 if Is_Interface (Etype (Typ)) then
20076 Append_Unique_Elmt (Etype (Typ), Ifaces);
20077 end if;
20079 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
20080 end if;
20081 end if;
20083 -- Handle entities in the list of abstract interfaces
20085 if Present (Interfaces (Typ)) then
20086 Iface_Elmt := First_Elmt (Interfaces (Typ));
20087 while Present (Iface_Elmt) loop
20088 Iface := Node (Iface_Elmt);
20090 pragma Assert (Is_Interface (Iface));
20092 if not Contain_Interface (Iface, Ifaces) then
20093 Append_Elmt (Iface, Ifaces);
20094 Collect_Implemented_Interfaces (Iface, Ifaces);
20095 end if;
20097 Next_Elmt (Iface_Elmt);
20098 end loop;
20099 end if;
20100 end Collect_Implemented_Interfaces;
20102 -- Local variables
20104 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
20106 Full_Indic : Node_Id;
20107 Full_Parent : Entity_Id;
20108 Priv_Parent : Entity_Id;
20110 -- Start of processing for Process_Full_View
20112 begin
20113 Mark_And_Set_Ghost_Completion (N, Priv_T);
20115 -- First some sanity checks that must be done after semantic
20116 -- decoration of the full view and thus cannot be placed with other
20117 -- similar checks in Find_Type_Name
20119 if not Is_Limited_Type (Priv_T)
20120 and then (Is_Limited_Type (Full_T)
20121 or else Is_Limited_Composite (Full_T))
20122 then
20123 if In_Instance then
20124 null;
20125 else
20126 Error_Msg_N
20127 ("completion of nonlimited type cannot be limited", Full_T);
20128 Explain_Limited_Type (Full_T, Full_T);
20129 end if;
20131 elsif Is_Abstract_Type (Full_T)
20132 and then not Is_Abstract_Type (Priv_T)
20133 then
20134 Error_Msg_N
20135 ("completion of nonabstract type cannot be abstract", Full_T);
20137 elsif Is_Tagged_Type (Priv_T)
20138 and then Is_Limited_Type (Priv_T)
20139 and then not Is_Limited_Type (Full_T)
20140 then
20141 -- If pragma CPP_Class was applied to the private declaration
20142 -- propagate the limitedness to the full-view
20144 if Is_CPP_Class (Priv_T) then
20145 Set_Is_Limited_Record (Full_T);
20147 -- GNAT allow its own definition of Limited_Controlled to disobey
20148 -- this rule in order in ease the implementation. This test is safe
20149 -- because Root_Controlled is defined in a child of System that
20150 -- normal programs are not supposed to use.
20152 elsif Is_RTE (Etype (Full_T), RE_Root_Controlled) then
20153 Set_Is_Limited_Composite (Full_T);
20154 else
20155 Error_Msg_N
20156 ("completion of limited tagged type must be limited", Full_T);
20157 end if;
20159 elsif Is_Generic_Type (Priv_T) then
20160 Error_Msg_N ("generic type cannot have a completion", Full_T);
20161 end if;
20163 -- Check that ancestor interfaces of private and full views are
20164 -- consistent. We omit this check for synchronized types because
20165 -- they are performed on the corresponding record type when frozen.
20167 if Ada_Version >= Ada_2005
20168 and then Is_Tagged_Type (Priv_T)
20169 and then Is_Tagged_Type (Full_T)
20170 and then not Is_Concurrent_Type (Full_T)
20171 then
20172 declare
20173 Iface : Entity_Id;
20174 Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
20175 Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
20177 begin
20178 Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
20179 Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
20181 -- Ada 2005 (AI-251): The partial view shall be a descendant of
20182 -- an interface type if and only if the full type is descendant
20183 -- of the interface type (AARM 7.3 (7.3/2)).
20185 Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
20187 if Present (Iface) then
20188 Error_Msg_NE
20189 ("interface in partial view& not implemented by full type "
20190 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
20191 end if;
20193 Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
20195 if Present (Iface) then
20196 Error_Msg_NE
20197 ("interface & not implemented by partial view "
20198 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
20199 end if;
20200 end;
20201 end if;
20203 if Is_Tagged_Type (Priv_T)
20204 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
20205 and then Is_Derived_Type (Full_T)
20206 then
20207 Priv_Parent := Etype (Priv_T);
20209 -- The full view of a private extension may have been transformed
20210 -- into an unconstrained derived type declaration and a subtype
20211 -- declaration (see build_derived_record_type for details).
20213 if Nkind (N) = N_Subtype_Declaration then
20214 Full_Indic := Subtype_Indication (N);
20215 Full_Parent := Etype (Base_Type (Full_T));
20216 else
20217 Full_Indic := Subtype_Indication (Type_Definition (N));
20218 Full_Parent := Etype (Full_T);
20219 end if;
20221 -- Check that the parent type of the full type is a descendant of
20222 -- the ancestor subtype given in the private extension. If either
20223 -- entity has an Etype equal to Any_Type then we had some previous
20224 -- error situation [7.3(8)].
20226 if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
20227 goto Leave;
20229 -- Ada 2005 (AI-251): Interfaces in the full type can be given in
20230 -- any order. Therefore we don't have to check that its parent must
20231 -- be a descendant of the parent of the private type declaration.
20233 elsif Is_Interface (Priv_Parent)
20234 and then Is_Interface (Full_Parent)
20235 then
20236 null;
20238 -- Ada 2005 (AI-251): If the parent of the private type declaration
20239 -- is an interface there is no need to check that it is an ancestor
20240 -- of the associated full type declaration. The required tests for
20241 -- this case are performed by Build_Derived_Record_Type.
20243 elsif not Is_Interface (Base_Type (Priv_Parent))
20244 and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
20245 then
20246 Error_Msg_N
20247 ("parent of full type must descend from parent of private "
20248 & "extension", Full_Indic);
20250 -- First check a formal restriction, and then proceed with checking
20251 -- Ada rules. Since the formal restriction is not a serious error, we
20252 -- don't prevent further error detection for this check, hence the
20253 -- ELSE.
20255 else
20256 -- In formal mode, when completing a private extension the type
20257 -- named in the private part must be exactly the same as that
20258 -- named in the visible part.
20260 if Priv_Parent /= Full_Parent then
20261 Error_Msg_Name_1 := Chars (Priv_Parent);
20262 Check_SPARK_05_Restriction ("% expected", Full_Indic);
20263 end if;
20265 -- Check the rules of 7.3(10): if the private extension inherits
20266 -- known discriminants, then the full type must also inherit those
20267 -- discriminants from the same (ancestor) type, and the parent
20268 -- subtype of the full type must be constrained if and only if
20269 -- the ancestor subtype of the private extension is constrained.
20271 if No (Discriminant_Specifications (Parent (Priv_T)))
20272 and then not Has_Unknown_Discriminants (Priv_T)
20273 and then Has_Discriminants (Base_Type (Priv_Parent))
20274 then
20275 declare
20276 Priv_Indic : constant Node_Id :=
20277 Subtype_Indication (Parent (Priv_T));
20279 Priv_Constr : constant Boolean :=
20280 Is_Constrained (Priv_Parent)
20281 or else
20282 Nkind (Priv_Indic) = N_Subtype_Indication
20283 or else
20284 Is_Constrained (Entity (Priv_Indic));
20286 Full_Constr : constant Boolean :=
20287 Is_Constrained (Full_Parent)
20288 or else
20289 Nkind (Full_Indic) = N_Subtype_Indication
20290 or else
20291 Is_Constrained (Entity (Full_Indic));
20293 Priv_Discr : Entity_Id;
20294 Full_Discr : Entity_Id;
20296 begin
20297 Priv_Discr := First_Discriminant (Priv_Parent);
20298 Full_Discr := First_Discriminant (Full_Parent);
20299 while Present (Priv_Discr) and then Present (Full_Discr) loop
20300 if Original_Record_Component (Priv_Discr) =
20301 Original_Record_Component (Full_Discr)
20302 or else
20303 Corresponding_Discriminant (Priv_Discr) =
20304 Corresponding_Discriminant (Full_Discr)
20305 then
20306 null;
20307 else
20308 exit;
20309 end if;
20311 Next_Discriminant (Priv_Discr);
20312 Next_Discriminant (Full_Discr);
20313 end loop;
20315 if Present (Priv_Discr) or else Present (Full_Discr) then
20316 Error_Msg_N
20317 ("full view must inherit discriminants of the parent "
20318 & "type used in the private extension", Full_Indic);
20320 elsif Priv_Constr and then not Full_Constr then
20321 Error_Msg_N
20322 ("parent subtype of full type must be constrained",
20323 Full_Indic);
20325 elsif Full_Constr and then not Priv_Constr then
20326 Error_Msg_N
20327 ("parent subtype of full type must be unconstrained",
20328 Full_Indic);
20329 end if;
20330 end;
20332 -- Check the rules of 7.3(12): if a partial view has neither
20333 -- known or unknown discriminants, then the full type
20334 -- declaration shall define a definite subtype.
20336 elsif not Has_Unknown_Discriminants (Priv_T)
20337 and then not Has_Discriminants (Priv_T)
20338 and then not Is_Constrained (Full_T)
20339 then
20340 Error_Msg_N
20341 ("full view must define a constrained type if partial view "
20342 & "has no discriminants", Full_T);
20343 end if;
20345 -- ??????? Do we implement the following properly ?????
20346 -- If the ancestor subtype of a private extension has constrained
20347 -- discriminants, then the parent subtype of the full view shall
20348 -- impose a statically matching constraint on those discriminants
20349 -- [7.3(13)].
20350 end if;
20352 else
20353 -- For untagged types, verify that a type without discriminants is
20354 -- not completed with an unconstrained type. A separate error message
20355 -- is produced if the full type has defaulted discriminants.
20357 if Is_Definite_Subtype (Priv_T)
20358 and then not Is_Definite_Subtype (Full_T)
20359 then
20360 Error_Msg_Sloc := Sloc (Parent (Priv_T));
20361 Error_Msg_NE
20362 ("full view of& not compatible with declaration#",
20363 Full_T, Priv_T);
20365 if not Is_Tagged_Type (Full_T) then
20366 Error_Msg_N
20367 ("\one is constrained, the other unconstrained", Full_T);
20368 end if;
20369 end if;
20370 end if;
20372 -- AI-419: verify that the use of "limited" is consistent
20374 declare
20375 Orig_Decl : constant Node_Id := Original_Node (N);
20377 begin
20378 if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
20379 and then Nkind (Orig_Decl) = N_Full_Type_Declaration
20380 and then Nkind
20381 (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
20382 then
20383 if not Limited_Present (Parent (Priv_T))
20384 and then not Synchronized_Present (Parent (Priv_T))
20385 and then Limited_Present (Type_Definition (Orig_Decl))
20386 then
20387 Error_Msg_N
20388 ("full view of non-limited extension cannot be limited", N);
20390 -- Conversely, if the partial view carries the limited keyword,
20391 -- the full view must as well, even if it may be redundant.
20393 elsif Limited_Present (Parent (Priv_T))
20394 and then not Limited_Present (Type_Definition (Orig_Decl))
20395 then
20396 Error_Msg_N
20397 ("full view of limited extension must be explicitly limited",
20399 end if;
20400 end if;
20401 end;
20403 -- Ada 2005 (AI-443): A synchronized private extension must be
20404 -- completed by a task or protected type.
20406 if Ada_Version >= Ada_2005
20407 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
20408 and then Synchronized_Present (Parent (Priv_T))
20409 and then not Is_Concurrent_Type (Full_T)
20410 then
20411 Error_Msg_N ("full view of synchronized extension must " &
20412 "be synchronized type", N);
20413 end if;
20415 -- Ada 2005 AI-363: if the full view has discriminants with
20416 -- defaults, it is illegal to declare constrained access subtypes
20417 -- whose designated type is the current type. This allows objects
20418 -- of the type that are declared in the heap to be unconstrained.
20420 if not Has_Unknown_Discriminants (Priv_T)
20421 and then not Has_Discriminants (Priv_T)
20422 and then Has_Discriminants (Full_T)
20423 and then
20424 Present (Discriminant_Default_Value (First_Discriminant (Full_T)))
20425 then
20426 Set_Has_Constrained_Partial_View (Full_T);
20427 Set_Has_Constrained_Partial_View (Priv_T);
20428 end if;
20430 -- Create a full declaration for all its subtypes recorded in
20431 -- Private_Dependents and swap them similarly to the base type. These
20432 -- are subtypes that have been define before the full declaration of
20433 -- the private type. We also swap the entry in Private_Dependents list
20434 -- so we can properly restore the private view on exit from the scope.
20436 declare
20437 Priv_Elmt : Elmt_Id;
20438 Priv_Scop : Entity_Id;
20439 Priv : Entity_Id;
20440 Full : Entity_Id;
20442 begin
20443 Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
20444 while Present (Priv_Elmt) loop
20445 Priv := Node (Priv_Elmt);
20446 Priv_Scop := Scope (Priv);
20448 if Ekind_In (Priv, E_Private_Subtype,
20449 E_Limited_Private_Subtype,
20450 E_Record_Subtype_With_Private)
20451 then
20452 Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
20453 Set_Is_Itype (Full);
20454 Set_Parent (Full, Parent (Priv));
20455 Set_Associated_Node_For_Itype (Full, N);
20457 -- Now we need to complete the private subtype, but since the
20458 -- base type has already been swapped, we must also swap the
20459 -- subtypes (and thus, reverse the arguments in the call to
20460 -- Complete_Private_Subtype). Also note that we may need to
20461 -- re-establish the scope of the private subtype.
20463 Copy_And_Swap (Priv, Full);
20465 if not In_Open_Scopes (Priv_Scop) then
20466 Push_Scope (Priv_Scop);
20468 else
20469 -- Reset Priv_Scop to Empty to indicate no scope was pushed
20471 Priv_Scop := Empty;
20472 end if;
20474 Complete_Private_Subtype (Full, Priv, Full_T, N);
20476 if Present (Priv_Scop) then
20477 Pop_Scope;
20478 end if;
20480 Replace_Elmt (Priv_Elmt, Full);
20481 end if;
20483 Next_Elmt (Priv_Elmt);
20484 end loop;
20485 end;
20487 -- If the private view was tagged, copy the new primitive operations
20488 -- from the private view to the full view.
20490 if Is_Tagged_Type (Full_T) then
20491 declare
20492 Disp_Typ : Entity_Id;
20493 Full_List : Elist_Id;
20494 Prim : Entity_Id;
20495 Prim_Elmt : Elmt_Id;
20496 Priv_List : Elist_Id;
20498 function Contains
20499 (E : Entity_Id;
20500 L : Elist_Id) return Boolean;
20501 -- Determine whether list L contains element E
20503 --------------
20504 -- Contains --
20505 --------------
20507 function Contains
20508 (E : Entity_Id;
20509 L : Elist_Id) return Boolean
20511 List_Elmt : Elmt_Id;
20513 begin
20514 List_Elmt := First_Elmt (L);
20515 while Present (List_Elmt) loop
20516 if Node (List_Elmt) = E then
20517 return True;
20518 end if;
20520 Next_Elmt (List_Elmt);
20521 end loop;
20523 return False;
20524 end Contains;
20526 -- Start of processing
20528 begin
20529 if Is_Tagged_Type (Priv_T) then
20530 Priv_List := Primitive_Operations (Priv_T);
20531 Prim_Elmt := First_Elmt (Priv_List);
20533 -- In the case of a concurrent type completing a private tagged
20534 -- type, primitives may have been declared in between the two
20535 -- views. These subprograms need to be wrapped the same way
20536 -- entries and protected procedures are handled because they
20537 -- cannot be directly shared by the two views.
20539 if Is_Concurrent_Type (Full_T) then
20540 declare
20541 Conc_Typ : constant Entity_Id :=
20542 Corresponding_Record_Type (Full_T);
20543 Curr_Nod : Node_Id := Parent (Conc_Typ);
20544 Wrap_Spec : Node_Id;
20546 begin
20547 while Present (Prim_Elmt) loop
20548 Prim := Node (Prim_Elmt);
20550 if Comes_From_Source (Prim)
20551 and then not Is_Abstract_Subprogram (Prim)
20552 then
20553 Wrap_Spec :=
20554 Make_Subprogram_Declaration (Sloc (Prim),
20555 Specification =>
20556 Build_Wrapper_Spec
20557 (Subp_Id => Prim,
20558 Obj_Typ => Conc_Typ,
20559 Formals =>
20560 Parameter_Specifications
20561 (Parent (Prim))));
20563 Insert_After (Curr_Nod, Wrap_Spec);
20564 Curr_Nod := Wrap_Spec;
20566 Analyze (Wrap_Spec);
20568 -- Remove the wrapper from visibility to avoid
20569 -- spurious conflict with the wrapped entity.
20571 Set_Is_Immediately_Visible
20572 (Defining_Entity (Specification (Wrap_Spec)),
20573 False);
20574 end if;
20576 Next_Elmt (Prim_Elmt);
20577 end loop;
20579 goto Leave;
20580 end;
20582 -- For non-concurrent types, transfer explicit primitives, but
20583 -- omit those inherited from the parent of the private view
20584 -- since they will be re-inherited later on.
20586 else
20587 Full_List := Primitive_Operations (Full_T);
20589 while Present (Prim_Elmt) loop
20590 Prim := Node (Prim_Elmt);
20592 if Comes_From_Source (Prim)
20593 and then not Contains (Prim, Full_List)
20594 then
20595 Append_Elmt (Prim, Full_List);
20596 end if;
20598 Next_Elmt (Prim_Elmt);
20599 end loop;
20600 end if;
20602 -- Untagged private view
20604 else
20605 Full_List := Primitive_Operations (Full_T);
20607 -- In this case the partial view is untagged, so here we locate
20608 -- all of the earlier primitives that need to be treated as
20609 -- dispatching (those that appear between the two views). Note
20610 -- that these additional operations must all be new operations
20611 -- (any earlier operations that override inherited operations
20612 -- of the full view will already have been inserted in the
20613 -- primitives list, marked by Check_Operation_From_Private_View
20614 -- as dispatching. Note that implicit "/=" operators are
20615 -- excluded from being added to the primitives list since they
20616 -- shouldn't be treated as dispatching (tagged "/=" is handled
20617 -- specially).
20619 Prim := Next_Entity (Full_T);
20620 while Present (Prim) and then Prim /= Priv_T loop
20621 if Ekind_In (Prim, E_Procedure, E_Function) then
20622 Disp_Typ := Find_Dispatching_Type (Prim);
20624 if Disp_Typ = Full_T
20625 and then (Chars (Prim) /= Name_Op_Ne
20626 or else Comes_From_Source (Prim))
20627 then
20628 Check_Controlling_Formals (Full_T, Prim);
20630 if not Is_Dispatching_Operation (Prim) then
20631 Append_Elmt (Prim, Full_List);
20632 Set_Is_Dispatching_Operation (Prim, True);
20633 Set_DT_Position_Value (Prim, No_Uint);
20634 end if;
20636 elsif Is_Dispatching_Operation (Prim)
20637 and then Disp_Typ /= Full_T
20638 then
20640 -- Verify that it is not otherwise controlled by a
20641 -- formal or a return value of type T.
20643 Check_Controlling_Formals (Disp_Typ, Prim);
20644 end if;
20645 end if;
20647 Next_Entity (Prim);
20648 end loop;
20649 end if;
20651 -- For the tagged case, the two views can share the same primitive
20652 -- operations list and the same class-wide type. Update attributes
20653 -- of the class-wide type which depend on the full declaration.
20655 if Is_Tagged_Type (Priv_T) then
20656 Set_Direct_Primitive_Operations (Priv_T, Full_List);
20657 Set_Class_Wide_Type
20658 (Base_Type (Full_T), Class_Wide_Type (Priv_T));
20660 Propagate_Concurrent_Flags (Class_Wide_Type (Priv_T), Full_T);
20661 end if;
20662 end;
20663 end if;
20665 -- Ada 2005 AI 161: Check preelaborable initialization consistency
20667 if Known_To_Have_Preelab_Init (Priv_T) then
20669 -- Case where there is a pragma Preelaborable_Initialization. We
20670 -- always allow this in predefined units, which is cheating a bit,
20671 -- but it means we don't have to struggle to meet the requirements in
20672 -- the RM for having Preelaborable Initialization. Otherwise we
20673 -- require that the type meets the RM rules. But we can't check that
20674 -- yet, because of the rule about overriding Initialize, so we simply
20675 -- set a flag that will be checked at freeze time.
20677 if not In_Predefined_Unit (Full_T) then
20678 Set_Must_Have_Preelab_Init (Full_T);
20679 end if;
20680 end if;
20682 -- If pragma CPP_Class was applied to the private type declaration,
20683 -- propagate it now to the full type declaration.
20685 if Is_CPP_Class (Priv_T) then
20686 Set_Is_CPP_Class (Full_T);
20687 Set_Convention (Full_T, Convention_CPP);
20689 -- Check that components of imported CPP types do not have default
20690 -- expressions.
20692 Check_CPP_Type_Has_No_Defaults (Full_T);
20693 end if;
20695 -- If the private view has user specified stream attributes, then so has
20696 -- the full view.
20698 -- Why the test, how could these flags be already set in Full_T ???
20700 if Has_Specified_Stream_Read (Priv_T) then
20701 Set_Has_Specified_Stream_Read (Full_T);
20702 end if;
20704 if Has_Specified_Stream_Write (Priv_T) then
20705 Set_Has_Specified_Stream_Write (Full_T);
20706 end if;
20708 if Has_Specified_Stream_Input (Priv_T) then
20709 Set_Has_Specified_Stream_Input (Full_T);
20710 end if;
20712 if Has_Specified_Stream_Output (Priv_T) then
20713 Set_Has_Specified_Stream_Output (Full_T);
20714 end if;
20716 -- Propagate Default_Initial_Condition-related attributes from the
20717 -- partial view to the full view and its base type.
20719 Propagate_DIC_Attributes (Full_T, From_Typ => Priv_T);
20720 Propagate_DIC_Attributes (Base_Type (Full_T), From_Typ => Priv_T);
20722 -- Propagate invariant-related attributes from the partial view to the
20723 -- full view and its base type.
20725 Propagate_Invariant_Attributes (Full_T, From_Typ => Priv_T);
20726 Propagate_Invariant_Attributes (Base_Type (Full_T), From_Typ => Priv_T);
20728 -- AI12-0041: Detect an attempt to inherit a class-wide type invariant
20729 -- in the full view without advertising the inheritance in the partial
20730 -- view. This can only occur when the partial view has no parent type
20731 -- and the full view has an interface as a parent. Any other scenarios
20732 -- are illegal because implemented interfaces must match between the
20733 -- two views.
20735 if Is_Tagged_Type (Priv_T) and then Is_Tagged_Type (Full_T) then
20736 declare
20737 Full_Par : constant Entity_Id := Etype (Full_T);
20738 Priv_Par : constant Entity_Id := Etype (Priv_T);
20740 begin
20741 if not Is_Interface (Priv_Par)
20742 and then Is_Interface (Full_Par)
20743 and then Has_Inheritable_Invariants (Full_Par)
20744 then
20745 Error_Msg_N
20746 ("hidden inheritance of class-wide type invariants not "
20747 & "allowed", N);
20748 end if;
20749 end;
20750 end if;
20752 -- Propagate predicates to full type, and predicate function if already
20753 -- defined. It is not clear that this can actually happen? the partial
20754 -- view cannot be frozen yet, and the predicate function has not been
20755 -- built. Still it is a cheap check and seems safer to make it.
20757 if Has_Predicates (Priv_T) then
20758 Set_Has_Predicates (Full_T);
20760 if Present (Predicate_Function (Priv_T)) then
20761 Set_Predicate_Function (Full_T, Predicate_Function (Priv_T));
20762 end if;
20763 end if;
20765 <<Leave>>
20766 Restore_Ghost_Mode (Saved_GM);
20767 end Process_Full_View;
20769 -----------------------------------
20770 -- Process_Incomplete_Dependents --
20771 -----------------------------------
20773 procedure Process_Incomplete_Dependents
20774 (N : Node_Id;
20775 Full_T : Entity_Id;
20776 Inc_T : Entity_Id)
20778 Inc_Elmt : Elmt_Id;
20779 Priv_Dep : Entity_Id;
20780 New_Subt : Entity_Id;
20782 Disc_Constraint : Elist_Id;
20784 begin
20785 if No (Private_Dependents (Inc_T)) then
20786 return;
20787 end if;
20789 -- Itypes that may be generated by the completion of an incomplete
20790 -- subtype are not used by the back-end and not attached to the tree.
20791 -- They are created only for constraint-checking purposes.
20793 Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
20794 while Present (Inc_Elmt) loop
20795 Priv_Dep := Node (Inc_Elmt);
20797 if Ekind (Priv_Dep) = E_Subprogram_Type then
20799 -- An Access_To_Subprogram type may have a return type or a
20800 -- parameter type that is incomplete. Replace with the full view.
20802 if Etype (Priv_Dep) = Inc_T then
20803 Set_Etype (Priv_Dep, Full_T);
20804 end if;
20806 declare
20807 Formal : Entity_Id;
20809 begin
20810 Formal := First_Formal (Priv_Dep);
20811 while Present (Formal) loop
20812 if Etype (Formal) = Inc_T then
20813 Set_Etype (Formal, Full_T);
20814 end if;
20816 Next_Formal (Formal);
20817 end loop;
20818 end;
20820 elsif Is_Overloadable (Priv_Dep) then
20822 -- If a subprogram in the incomplete dependents list is primitive
20823 -- for a tagged full type then mark it as a dispatching operation,
20824 -- check whether it overrides an inherited subprogram, and check
20825 -- restrictions on its controlling formals. Note that a protected
20826 -- operation is never dispatching: only its wrapper operation
20827 -- (which has convention Ada) is.
20829 if Is_Tagged_Type (Full_T)
20830 and then Is_Primitive (Priv_Dep)
20831 and then Convention (Priv_Dep) /= Convention_Protected
20832 then
20833 Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
20834 Set_Is_Dispatching_Operation (Priv_Dep);
20835 Check_Controlling_Formals (Full_T, Priv_Dep);
20836 end if;
20838 elsif Ekind (Priv_Dep) = E_Subprogram_Body then
20840 -- Can happen during processing of a body before the completion
20841 -- of a TA type. Ignore, because spec is also on dependent list.
20843 return;
20845 -- Ada 2005 (AI-412): Transform a regular incomplete subtype into a
20846 -- corresponding subtype of the full view.
20848 elsif Ekind (Priv_Dep) = E_Incomplete_Subtype
20849 and then Comes_From_Source (Priv_Dep)
20850 then
20851 Set_Subtype_Indication
20852 (Parent (Priv_Dep), New_Occurrence_Of (Full_T, Sloc (Priv_Dep)));
20853 Set_Etype (Priv_Dep, Full_T);
20854 Set_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
20855 Set_Analyzed (Parent (Priv_Dep), False);
20857 -- Reanalyze the declaration, suppressing the call to Enter_Name
20858 -- to avoid duplicate names.
20860 Analyze_Subtype_Declaration
20861 (N => Parent (Priv_Dep),
20862 Skip => True);
20864 -- Dependent is a subtype
20866 else
20867 -- We build a new subtype indication using the full view of the
20868 -- incomplete parent. The discriminant constraints have been
20869 -- elaborated already at the point of the subtype declaration.
20871 New_Subt := Create_Itype (E_Void, N);
20873 if Has_Discriminants (Full_T) then
20874 Disc_Constraint := Discriminant_Constraint (Priv_Dep);
20875 else
20876 Disc_Constraint := No_Elist;
20877 end if;
20879 Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
20880 Set_Full_View (Priv_Dep, New_Subt);
20881 end if;
20883 Next_Elmt (Inc_Elmt);
20884 end loop;
20885 end Process_Incomplete_Dependents;
20887 --------------------------------
20888 -- Process_Range_Expr_In_Decl --
20889 --------------------------------
20891 procedure Process_Range_Expr_In_Decl
20892 (R : Node_Id;
20893 T : Entity_Id;
20894 Subtyp : Entity_Id := Empty;
20895 Check_List : List_Id := Empty_List;
20896 R_Check_Off : Boolean := False;
20897 In_Iter_Schm : Boolean := False)
20899 Lo, Hi : Node_Id;
20900 R_Checks : Check_Result;
20901 Insert_Node : Node_Id;
20902 Def_Id : Entity_Id;
20904 begin
20905 Analyze_And_Resolve (R, Base_Type (T));
20907 if Nkind (R) = N_Range then
20909 -- In SPARK, all ranges should be static, with the exception of the
20910 -- discrete type definition of a loop parameter specification.
20912 if not In_Iter_Schm
20913 and then not Is_OK_Static_Range (R)
20914 then
20915 Check_SPARK_05_Restriction ("range should be static", R);
20916 end if;
20918 Lo := Low_Bound (R);
20919 Hi := High_Bound (R);
20921 -- Validity checks on the range of a quantified expression are
20922 -- delayed until the construct is transformed into a loop.
20924 if Nkind (Parent (R)) = N_Loop_Parameter_Specification
20925 and then Nkind (Parent (Parent (R))) = N_Quantified_Expression
20926 then
20927 null;
20929 -- We need to ensure validity of the bounds here, because if we
20930 -- go ahead and do the expansion, then the expanded code will get
20931 -- analyzed with range checks suppressed and we miss the check.
20933 -- WARNING: The capture of the range bounds with xxx_FIRST/_LAST and
20934 -- the temporaries generated by routine Remove_Side_Effects by means
20935 -- of validity checks must use the same names. When a range appears
20936 -- in the parent of a generic, the range is processed with checks
20937 -- disabled as part of the generic context and with checks enabled
20938 -- for code generation purposes. This leads to link issues as the
20939 -- generic contains references to xxx_FIRST/_LAST, but the inlined
20940 -- template sees the temporaries generated by Remove_Side_Effects.
20942 else
20943 Validity_Check_Range (R, Subtyp);
20944 end if;
20946 -- If there were errors in the declaration, try and patch up some
20947 -- common mistakes in the bounds. The cases handled are literals
20948 -- which are Integer where the expected type is Real and vice versa.
20949 -- These corrections allow the compilation process to proceed further
20950 -- along since some basic assumptions of the format of the bounds
20951 -- are guaranteed.
20953 if Etype (R) = Any_Type then
20954 if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
20955 Rewrite (Lo,
20956 Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
20958 elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
20959 Rewrite (Hi,
20960 Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
20962 elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
20963 Rewrite (Lo,
20964 Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
20966 elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
20967 Rewrite (Hi,
20968 Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
20969 end if;
20971 Set_Etype (Lo, T);
20972 Set_Etype (Hi, T);
20973 end if;
20975 -- If the bounds of the range have been mistakenly given as string
20976 -- literals (perhaps in place of character literals), then an error
20977 -- has already been reported, but we rewrite the string literal as a
20978 -- bound of the range's type to avoid blowups in later processing
20979 -- that looks at static values.
20981 if Nkind (Lo) = N_String_Literal then
20982 Rewrite (Lo,
20983 Make_Attribute_Reference (Sloc (Lo),
20984 Prefix => New_Occurrence_Of (T, Sloc (Lo)),
20985 Attribute_Name => Name_First));
20986 Analyze_And_Resolve (Lo);
20987 end if;
20989 if Nkind (Hi) = N_String_Literal then
20990 Rewrite (Hi,
20991 Make_Attribute_Reference (Sloc (Hi),
20992 Prefix => New_Occurrence_Of (T, Sloc (Hi)),
20993 Attribute_Name => Name_First));
20994 Analyze_And_Resolve (Hi);
20995 end if;
20997 -- If bounds aren't scalar at this point then exit, avoiding
20998 -- problems with further processing of the range in this procedure.
21000 if not Is_Scalar_Type (Etype (Lo)) then
21001 return;
21002 end if;
21004 -- Resolve (actually Sem_Eval) has checked that the bounds are in
21005 -- then range of the base type. Here we check whether the bounds
21006 -- are in the range of the subtype itself. Note that if the bounds
21007 -- represent the null range the Constraint_Error exception should
21008 -- not be raised.
21010 -- ??? The following code should be cleaned up as follows
21012 -- 1. The Is_Null_Range (Lo, Hi) test should disappear since it
21013 -- is done in the call to Range_Check (R, T); below
21015 -- 2. The use of R_Check_Off should be investigated and possibly
21016 -- removed, this would clean up things a bit.
21018 if Is_Null_Range (Lo, Hi) then
21019 null;
21021 else
21022 -- Capture values of bounds and generate temporaries for them
21023 -- if needed, before applying checks, since checks may cause
21024 -- duplication of the expression without forcing evaluation.
21026 -- The forced evaluation removes side effects from expressions,
21027 -- which should occur also in GNATprove mode. Otherwise, we end up
21028 -- with unexpected insertions of actions at places where this is
21029 -- not supposed to occur, e.g. on default parameters of a call.
21031 if Expander_Active or GNATprove_Mode then
21033 -- Call Force_Evaluation to create declarations as needed to
21034 -- deal with side effects, and also create typ_FIRST/LAST
21035 -- entities for bounds if we have a subtype name.
21037 -- Note: we do this transformation even if expansion is not
21038 -- active if we are in GNATprove_Mode since the transformation
21039 -- is in general required to ensure that the resulting tree has
21040 -- proper Ada semantics.
21042 Force_Evaluation
21043 (Lo, Related_Id => Subtyp, Is_Low_Bound => True);
21044 Force_Evaluation
21045 (Hi, Related_Id => Subtyp, Is_High_Bound => True);
21046 end if;
21048 -- We use a flag here instead of suppressing checks on the type
21049 -- because the type we check against isn't necessarily the place
21050 -- where we put the check.
21052 if not R_Check_Off then
21053 R_Checks := Get_Range_Checks (R, T);
21055 -- Look up tree to find an appropriate insertion point. We
21056 -- can't just use insert_actions because later processing
21057 -- depends on the insertion node. Prior to Ada 2012 the
21058 -- insertion point could only be a declaration or a loop, but
21059 -- quantified expressions can appear within any context in an
21060 -- expression, and the insertion point can be any statement,
21061 -- pragma, or declaration.
21063 Insert_Node := Parent (R);
21064 while Present (Insert_Node) loop
21065 exit when
21066 Nkind (Insert_Node) in N_Declaration
21067 and then
21068 not Nkind_In
21069 (Insert_Node, N_Component_Declaration,
21070 N_Loop_Parameter_Specification,
21071 N_Function_Specification,
21072 N_Procedure_Specification);
21074 exit when Nkind (Insert_Node) in N_Later_Decl_Item
21075 or else Nkind (Insert_Node) in
21076 N_Statement_Other_Than_Procedure_Call
21077 or else Nkind_In (Insert_Node, N_Procedure_Call_Statement,
21078 N_Pragma);
21080 Insert_Node := Parent (Insert_Node);
21081 end loop;
21083 -- Why would Type_Decl not be present??? Without this test,
21084 -- short regression tests fail.
21086 if Present (Insert_Node) then
21088 -- Case of loop statement. Verify that the range is part
21089 -- of the subtype indication of the iteration scheme.
21091 if Nkind (Insert_Node) = N_Loop_Statement then
21092 declare
21093 Indic : Node_Id;
21095 begin
21096 Indic := Parent (R);
21097 while Present (Indic)
21098 and then Nkind (Indic) /= N_Subtype_Indication
21099 loop
21100 Indic := Parent (Indic);
21101 end loop;
21103 if Present (Indic) then
21104 Def_Id := Etype (Subtype_Mark (Indic));
21106 Insert_Range_Checks
21107 (R_Checks,
21108 Insert_Node,
21109 Def_Id,
21110 Sloc (Insert_Node),
21112 Do_Before => True);
21113 end if;
21114 end;
21116 -- Insertion before a declaration. If the declaration
21117 -- includes discriminants, the list of applicable checks
21118 -- is given by the caller.
21120 elsif Nkind (Insert_Node) in N_Declaration then
21121 Def_Id := Defining_Identifier (Insert_Node);
21123 if (Ekind (Def_Id) = E_Record_Type
21124 and then Depends_On_Discriminant (R))
21125 or else
21126 (Ekind (Def_Id) = E_Protected_Type
21127 and then Has_Discriminants (Def_Id))
21128 then
21129 Append_Range_Checks
21130 (R_Checks,
21131 Check_List, Def_Id, Sloc (Insert_Node), R);
21133 else
21134 Insert_Range_Checks
21135 (R_Checks,
21136 Insert_Node, Def_Id, Sloc (Insert_Node), R);
21138 end if;
21140 -- Insertion before a statement. Range appears in the
21141 -- context of a quantified expression. Insertion will
21142 -- take place when expression is expanded.
21144 else
21145 null;
21146 end if;
21147 end if;
21148 end if;
21149 end if;
21151 -- Case of other than an explicit N_Range node
21153 -- The forced evaluation removes side effects from expressions, which
21154 -- should occur also in GNATprove mode. Otherwise, we end up with
21155 -- unexpected insertions of actions at places where this is not
21156 -- supposed to occur, e.g. on default parameters of a call.
21158 elsif Expander_Active or GNATprove_Mode then
21159 Get_Index_Bounds (R, Lo, Hi);
21160 Force_Evaluation (Lo);
21161 Force_Evaluation (Hi);
21162 end if;
21163 end Process_Range_Expr_In_Decl;
21165 --------------------------------------
21166 -- Process_Real_Range_Specification --
21167 --------------------------------------
21169 procedure Process_Real_Range_Specification (Def : Node_Id) is
21170 Spec : constant Node_Id := Real_Range_Specification (Def);
21171 Lo : Node_Id;
21172 Hi : Node_Id;
21173 Err : Boolean := False;
21175 procedure Analyze_Bound (N : Node_Id);
21176 -- Analyze and check one bound
21178 -------------------
21179 -- Analyze_Bound --
21180 -------------------
21182 procedure Analyze_Bound (N : Node_Id) is
21183 begin
21184 Analyze_And_Resolve (N, Any_Real);
21186 if not Is_OK_Static_Expression (N) then
21187 Flag_Non_Static_Expr
21188 ("bound in real type definition is not static!", N);
21189 Err := True;
21190 end if;
21191 end Analyze_Bound;
21193 -- Start of processing for Process_Real_Range_Specification
21195 begin
21196 if Present (Spec) then
21197 Lo := Low_Bound (Spec);
21198 Hi := High_Bound (Spec);
21199 Analyze_Bound (Lo);
21200 Analyze_Bound (Hi);
21202 -- If error, clear away junk range specification
21204 if Err then
21205 Set_Real_Range_Specification (Def, Empty);
21206 end if;
21207 end if;
21208 end Process_Real_Range_Specification;
21210 ---------------------
21211 -- Process_Subtype --
21212 ---------------------
21214 function Process_Subtype
21215 (S : Node_Id;
21216 Related_Nod : Node_Id;
21217 Related_Id : Entity_Id := Empty;
21218 Suffix : Character := ' ') return Entity_Id
21220 P : Node_Id;
21221 Def_Id : Entity_Id;
21222 Error_Node : Node_Id;
21223 Full_View_Id : Entity_Id;
21224 Subtype_Mark_Id : Entity_Id;
21226 May_Have_Null_Exclusion : Boolean;
21228 procedure Check_Incomplete (T : Node_Id);
21229 -- Called to verify that an incomplete type is not used prematurely
21231 ----------------------
21232 -- Check_Incomplete --
21233 ----------------------
21235 procedure Check_Incomplete (T : Node_Id) is
21236 begin
21237 -- Ada 2005 (AI-412): Incomplete subtypes are legal
21239 if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
21240 and then
21241 not (Ada_Version >= Ada_2005
21242 and then
21243 (Nkind (Parent (T)) = N_Subtype_Declaration
21244 or else (Nkind (Parent (T)) = N_Subtype_Indication
21245 and then Nkind (Parent (Parent (T))) =
21246 N_Subtype_Declaration)))
21247 then
21248 Error_Msg_N ("invalid use of type before its full declaration", T);
21249 end if;
21250 end Check_Incomplete;
21252 -- Start of processing for Process_Subtype
21254 begin
21255 -- Case of no constraints present
21257 if Nkind (S) /= N_Subtype_Indication then
21258 Find_Type (S);
21259 Check_Incomplete (S);
21260 P := Parent (S);
21262 -- Ada 2005 (AI-231): Static check
21264 if Ada_Version >= Ada_2005
21265 and then Present (P)
21266 and then Null_Exclusion_Present (P)
21267 and then Nkind (P) /= N_Access_To_Object_Definition
21268 and then not Is_Access_Type (Entity (S))
21269 then
21270 Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
21271 end if;
21273 -- The following is ugly, can't we have a range or even a flag???
21275 May_Have_Null_Exclusion :=
21276 Nkind_In (P, N_Access_Definition,
21277 N_Access_Function_Definition,
21278 N_Access_Procedure_Definition,
21279 N_Access_To_Object_Definition,
21280 N_Allocator,
21281 N_Component_Definition)
21282 or else
21283 Nkind_In (P, N_Derived_Type_Definition,
21284 N_Discriminant_Specification,
21285 N_Formal_Object_Declaration,
21286 N_Object_Declaration,
21287 N_Object_Renaming_Declaration,
21288 N_Parameter_Specification,
21289 N_Subtype_Declaration);
21291 -- Create an Itype that is a duplicate of Entity (S) but with the
21292 -- null-exclusion attribute.
21294 if May_Have_Null_Exclusion
21295 and then Is_Access_Type (Entity (S))
21296 and then Null_Exclusion_Present (P)
21298 -- No need to check the case of an access to object definition.
21299 -- It is correct to define double not-null pointers.
21301 -- Example:
21302 -- type Not_Null_Int_Ptr is not null access Integer;
21303 -- type Acc is not null access Not_Null_Int_Ptr;
21305 and then Nkind (P) /= N_Access_To_Object_Definition
21306 then
21307 if Can_Never_Be_Null (Entity (S)) then
21308 case Nkind (Related_Nod) is
21309 when N_Full_Type_Declaration =>
21310 if Nkind (Type_Definition (Related_Nod))
21311 in N_Array_Type_Definition
21312 then
21313 Error_Node :=
21314 Subtype_Indication
21315 (Component_Definition
21316 (Type_Definition (Related_Nod)));
21317 else
21318 Error_Node :=
21319 Subtype_Indication (Type_Definition (Related_Nod));
21320 end if;
21322 when N_Subtype_Declaration =>
21323 Error_Node := Subtype_Indication (Related_Nod);
21325 when N_Object_Declaration =>
21326 Error_Node := Object_Definition (Related_Nod);
21328 when N_Component_Declaration =>
21329 Error_Node :=
21330 Subtype_Indication (Component_Definition (Related_Nod));
21332 when N_Allocator =>
21333 Error_Node := Expression (Related_Nod);
21335 when others =>
21336 pragma Assert (False);
21337 Error_Node := Related_Nod;
21338 end case;
21340 Error_Msg_NE
21341 ("`NOT NULL` not allowed (& already excludes null)",
21342 Error_Node,
21343 Entity (S));
21344 end if;
21346 Set_Etype (S,
21347 Create_Null_Excluding_Itype
21348 (T => Entity (S),
21349 Related_Nod => P));
21350 Set_Entity (S, Etype (S));
21351 end if;
21353 return Entity (S);
21355 -- Case of constraint present, so that we have an N_Subtype_Indication
21356 -- node (this node is created only if constraints are present).
21358 else
21359 Find_Type (Subtype_Mark (S));
21361 if Nkind (Parent (S)) /= N_Access_To_Object_Definition
21362 and then not
21363 (Nkind (Parent (S)) = N_Subtype_Declaration
21364 and then Is_Itype (Defining_Identifier (Parent (S))))
21365 then
21366 Check_Incomplete (Subtype_Mark (S));
21367 end if;
21369 P := Parent (S);
21370 Subtype_Mark_Id := Entity (Subtype_Mark (S));
21372 -- Explicit subtype declaration case
21374 if Nkind (P) = N_Subtype_Declaration then
21375 Def_Id := Defining_Identifier (P);
21377 -- Explicit derived type definition case
21379 elsif Nkind (P) = N_Derived_Type_Definition then
21380 Def_Id := Defining_Identifier (Parent (P));
21382 -- Implicit case, the Def_Id must be created as an implicit type.
21383 -- The one exception arises in the case of concurrent types, array
21384 -- and access types, where other subsidiary implicit types may be
21385 -- created and must appear before the main implicit type. In these
21386 -- cases we leave Def_Id set to Empty as a signal that Create_Itype
21387 -- has not yet been called to create Def_Id.
21389 else
21390 if Is_Array_Type (Subtype_Mark_Id)
21391 or else Is_Concurrent_Type (Subtype_Mark_Id)
21392 or else Is_Access_Type (Subtype_Mark_Id)
21393 then
21394 Def_Id := Empty;
21396 -- For the other cases, we create a new unattached Itype,
21397 -- and set the indication to ensure it gets attached later.
21399 else
21400 Def_Id :=
21401 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
21402 end if;
21403 end if;
21405 -- If the kind of constraint is invalid for this kind of type,
21406 -- then give an error, and then pretend no constraint was given.
21408 if not Is_Valid_Constraint_Kind
21409 (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
21410 then
21411 Error_Msg_N
21412 ("incorrect constraint for this kind of type", Constraint (S));
21414 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
21416 -- Set Ekind of orphan itype, to prevent cascaded errors
21418 if Present (Def_Id) then
21419 Set_Ekind (Def_Id, Ekind (Any_Type));
21420 end if;
21422 -- Make recursive call, having got rid of the bogus constraint
21424 return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
21425 end if;
21427 -- Remaining processing depends on type. Select on Base_Type kind to
21428 -- ensure getting to the concrete type kind in the case of a private
21429 -- subtype (needed when only doing semantic analysis).
21431 case Ekind (Base_Type (Subtype_Mark_Id)) is
21432 when Access_Kind =>
21434 -- If this is a constraint on a class-wide type, discard it.
21435 -- There is currently no way to express a partial discriminant
21436 -- constraint on a type with unknown discriminants. This is
21437 -- a pathology that the ACATS wisely decides not to test.
21439 if Is_Class_Wide_Type (Designated_Type (Subtype_Mark_Id)) then
21440 if Comes_From_Source (S) then
21441 Error_Msg_N
21442 ("constraint on class-wide type ignored??",
21443 Constraint (S));
21444 end if;
21446 if Nkind (P) = N_Subtype_Declaration then
21447 Set_Subtype_Indication (P,
21448 New_Occurrence_Of (Subtype_Mark_Id, Sloc (S)));
21449 end if;
21451 return Subtype_Mark_Id;
21452 end if;
21454 Constrain_Access (Def_Id, S, Related_Nod);
21456 if Expander_Active
21457 and then Is_Itype (Designated_Type (Def_Id))
21458 and then Nkind (Related_Nod) = N_Subtype_Declaration
21459 and then not Is_Incomplete_Type (Designated_Type (Def_Id))
21460 then
21461 Build_Itype_Reference
21462 (Designated_Type (Def_Id), Related_Nod);
21463 end if;
21465 when Array_Kind =>
21466 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
21468 when Decimal_Fixed_Point_Kind =>
21469 Constrain_Decimal (Def_Id, S);
21471 when Enumeration_Kind =>
21472 Constrain_Enumeration (Def_Id, S);
21473 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
21475 when Ordinary_Fixed_Point_Kind =>
21476 Constrain_Ordinary_Fixed (Def_Id, S);
21478 when Float_Kind =>
21479 Constrain_Float (Def_Id, S);
21481 when Integer_Kind =>
21482 Constrain_Integer (Def_Id, S);
21483 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
21485 when Class_Wide_Kind
21486 | E_Incomplete_Type
21487 | E_Record_Subtype
21488 | E_Record_Type
21490 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
21492 if Ekind (Def_Id) = E_Incomplete_Type then
21493 Set_Private_Dependents (Def_Id, New_Elmt_List);
21494 end if;
21496 when Private_Kind =>
21497 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
21499 -- The base type may be private but Def_Id may be a full view
21500 -- in an instance.
21502 if Is_Private_Type (Def_Id) then
21503 Set_Private_Dependents (Def_Id, New_Elmt_List);
21504 end if;
21506 -- In case of an invalid constraint prevent further processing
21507 -- since the type constructed is missing expected fields.
21509 if Etype (Def_Id) = Any_Type then
21510 return Def_Id;
21511 end if;
21513 -- If the full view is that of a task with discriminants,
21514 -- we must constrain both the concurrent type and its
21515 -- corresponding record type. Otherwise we will just propagate
21516 -- the constraint to the full view, if available.
21518 if Present (Full_View (Subtype_Mark_Id))
21519 and then Has_Discriminants (Subtype_Mark_Id)
21520 and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
21521 then
21522 Full_View_Id :=
21523 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
21525 Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
21526 Constrain_Concurrent (Full_View_Id, S,
21527 Related_Nod, Related_Id, Suffix);
21528 Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
21529 Set_Full_View (Def_Id, Full_View_Id);
21531 -- Introduce an explicit reference to the private subtype,
21532 -- to prevent scope anomalies in gigi if first use appears
21533 -- in a nested context, e.g. a later function body.
21534 -- Should this be generated in other contexts than a full
21535 -- type declaration?
21537 if Is_Itype (Def_Id)
21538 and then
21539 Nkind (Parent (P)) = N_Full_Type_Declaration
21540 then
21541 Build_Itype_Reference (Def_Id, Parent (P));
21542 end if;
21544 else
21545 Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
21546 end if;
21548 when Concurrent_Kind =>
21549 Constrain_Concurrent (Def_Id, S,
21550 Related_Nod, Related_Id, Suffix);
21552 when others =>
21553 Error_Msg_N ("invalid subtype mark in subtype indication", S);
21554 end case;
21556 -- Size, Alignment, Representation aspects and Convention are always
21557 -- inherited from the base type.
21559 Set_Size_Info (Def_Id, (Subtype_Mark_Id));
21560 Set_Rep_Info (Def_Id, (Subtype_Mark_Id));
21561 Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
21563 return Def_Id;
21564 end if;
21565 end Process_Subtype;
21567 -----------------------------
21568 -- Record_Type_Declaration --
21569 -----------------------------
21571 procedure Record_Type_Declaration
21572 (T : Entity_Id;
21573 N : Node_Id;
21574 Prev : Entity_Id)
21576 Def : constant Node_Id := Type_Definition (N);
21577 Is_Tagged : Boolean;
21578 Tag_Comp : Entity_Id;
21580 begin
21581 -- These flags must be initialized before calling Process_Discriminants
21582 -- because this routine makes use of them.
21584 Set_Ekind (T, E_Record_Type);
21585 Set_Etype (T, T);
21586 Init_Size_Align (T);
21587 Set_Interfaces (T, No_Elist);
21588 Set_Stored_Constraint (T, No_Elist);
21589 Set_Default_SSO (T);
21590 Set_No_Reordering (T, No_Component_Reordering);
21592 -- Normal case
21594 if Ada_Version < Ada_2005 or else not Interface_Present (Def) then
21595 if Limited_Present (Def) then
21596 Check_SPARK_05_Restriction ("limited is not allowed", N);
21597 end if;
21599 if Abstract_Present (Def) then
21600 Check_SPARK_05_Restriction ("abstract is not allowed", N);
21601 end if;
21603 -- The flag Is_Tagged_Type might have already been set by
21604 -- Find_Type_Name if it detected an error for declaration T. This
21605 -- arises in the case of private tagged types where the full view
21606 -- omits the word tagged.
21608 Is_Tagged :=
21609 Tagged_Present (Def)
21610 or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
21612 Set_Is_Limited_Record (T, Limited_Present (Def));
21614 if Is_Tagged then
21615 Set_Is_Tagged_Type (T, True);
21616 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
21617 end if;
21619 -- Type is abstract if full declaration carries keyword, or if
21620 -- previous partial view did.
21622 Set_Is_Abstract_Type (T, Is_Abstract_Type (T)
21623 or else Abstract_Present (Def));
21625 else
21626 Check_SPARK_05_Restriction ("interface is not allowed", N);
21628 Is_Tagged := True;
21629 Analyze_Interface_Declaration (T, Def);
21631 if Present (Discriminant_Specifications (N)) then
21632 Error_Msg_N
21633 ("interface types cannot have discriminants",
21634 Defining_Identifier
21635 (First (Discriminant_Specifications (N))));
21636 end if;
21637 end if;
21639 -- First pass: if there are self-referential access components,
21640 -- create the required anonymous access type declarations, and if
21641 -- need be an incomplete type declaration for T itself.
21643 Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
21645 if Ada_Version >= Ada_2005
21646 and then Present (Interface_List (Def))
21647 then
21648 Check_Interfaces (N, Def);
21650 declare
21651 Ifaces_List : Elist_Id;
21653 begin
21654 -- Ada 2005 (AI-251): Collect the list of progenitors that are not
21655 -- already in the parents.
21657 Collect_Interfaces
21658 (T => T,
21659 Ifaces_List => Ifaces_List,
21660 Exclude_Parents => True);
21662 Set_Interfaces (T, Ifaces_List);
21663 end;
21664 end if;
21666 -- Records constitute a scope for the component declarations within.
21667 -- The scope is created prior to the processing of these declarations.
21668 -- Discriminants are processed first, so that they are visible when
21669 -- processing the other components. The Ekind of the record type itself
21670 -- is set to E_Record_Type (subtypes appear as E_Record_Subtype).
21672 -- Enter record scope
21674 Push_Scope (T);
21676 -- If an incomplete or private type declaration was already given for
21677 -- the type, then this scope already exists, and the discriminants have
21678 -- been declared within. We must verify that the full declaration
21679 -- matches the incomplete one.
21681 Check_Or_Process_Discriminants (N, T, Prev);
21683 Set_Is_Constrained (T, not Has_Discriminants (T));
21684 Set_Has_Delayed_Freeze (T, True);
21686 -- For tagged types add a manually analyzed component corresponding
21687 -- to the component _tag, the corresponding piece of tree will be
21688 -- expanded as part of the freezing actions if it is not a CPP_Class.
21690 if Is_Tagged then
21692 -- Do not add the tag unless we are in expansion mode
21694 if Expander_Active then
21695 Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
21696 Enter_Name (Tag_Comp);
21698 Set_Ekind (Tag_Comp, E_Component);
21699 Set_Is_Tag (Tag_Comp);
21700 Set_Is_Aliased (Tag_Comp);
21701 Set_Etype (Tag_Comp, RTE (RE_Tag));
21702 Set_DT_Entry_Count (Tag_Comp, No_Uint);
21703 Set_Original_Record_Component (Tag_Comp, Tag_Comp);
21704 Init_Component_Location (Tag_Comp);
21706 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
21707 -- implemented interfaces.
21709 if Has_Interfaces (T) then
21710 Add_Interface_Tag_Components (N, T);
21711 end if;
21712 end if;
21714 Make_Class_Wide_Type (T);
21715 Set_Direct_Primitive_Operations (T, New_Elmt_List);
21716 end if;
21718 -- We must suppress range checks when processing record components in
21719 -- the presence of discriminants, since we don't want spurious checks to
21720 -- be generated during their analysis, but Suppress_Range_Checks flags
21721 -- must be reset the after processing the record definition.
21723 -- Note: this is the only use of Kill_Range_Checks, and is a bit odd,
21724 -- couldn't we just use the normal range check suppression method here.
21725 -- That would seem cleaner ???
21727 if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
21728 Set_Kill_Range_Checks (T, True);
21729 Record_Type_Definition (Def, Prev);
21730 Set_Kill_Range_Checks (T, False);
21731 else
21732 Record_Type_Definition (Def, Prev);
21733 end if;
21735 -- Exit from record scope
21737 End_Scope;
21739 -- Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
21740 -- the implemented interfaces and associate them an aliased entity.
21742 if Is_Tagged
21743 and then not Is_Empty_List (Interface_List (Def))
21744 then
21745 Derive_Progenitor_Subprograms (T, T);
21746 end if;
21748 Check_Function_Writable_Actuals (N);
21749 end Record_Type_Declaration;
21751 ----------------------------
21752 -- Record_Type_Definition --
21753 ----------------------------
21755 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
21756 Component : Entity_Id;
21757 Ctrl_Components : Boolean := False;
21758 Final_Storage_Only : Boolean;
21759 T : Entity_Id;
21761 begin
21762 if Ekind (Prev_T) = E_Incomplete_Type then
21763 T := Full_View (Prev_T);
21764 else
21765 T := Prev_T;
21766 end if;
21768 -- In SPARK, tagged types and type extensions may only be declared in
21769 -- the specification of library unit packages.
21771 if Present (Def) and then Is_Tagged_Type (T) then
21772 declare
21773 Typ : Node_Id;
21774 Ctxt : Node_Id;
21776 begin
21777 if Nkind (Parent (Def)) = N_Full_Type_Declaration then
21778 Typ := Parent (Def);
21779 else
21780 pragma Assert
21781 (Nkind (Parent (Def)) = N_Derived_Type_Definition);
21782 Typ := Parent (Parent (Def));
21783 end if;
21785 Ctxt := Parent (Typ);
21787 if Nkind (Ctxt) = N_Package_Body
21788 and then Nkind (Parent (Ctxt)) = N_Compilation_Unit
21789 then
21790 Check_SPARK_05_Restriction
21791 ("type should be defined in package specification", Typ);
21793 elsif Nkind (Ctxt) /= N_Package_Specification
21794 or else Nkind (Parent (Parent (Ctxt))) /= N_Compilation_Unit
21795 then
21796 Check_SPARK_05_Restriction
21797 ("type should be defined in library unit package", Typ);
21798 end if;
21799 end;
21800 end if;
21802 Final_Storage_Only := not Is_Controlled_Active (T);
21804 -- Ada 2005: Check whether an explicit Limited is present in a derived
21805 -- type declaration.
21807 if Nkind (Parent (Def)) = N_Derived_Type_Definition
21808 and then Limited_Present (Parent (Def))
21809 then
21810 Set_Is_Limited_Record (T);
21811 end if;
21813 -- If the component list of a record type is defined by the reserved
21814 -- word null and there is no discriminant part, then the record type has
21815 -- no components and all records of the type are null records (RM 3.7)
21816 -- This procedure is also called to process the extension part of a
21817 -- record extension, in which case the current scope may have inherited
21818 -- components.
21820 if No (Def)
21821 or else No (Component_List (Def))
21822 or else Null_Present (Component_List (Def))
21823 then
21824 if not Is_Tagged_Type (T) then
21825 Check_SPARK_05_Restriction ("untagged record cannot be null", Def);
21826 end if;
21828 else
21829 Analyze_Declarations (Component_Items (Component_List (Def)));
21831 if Present (Variant_Part (Component_List (Def))) then
21832 Check_SPARK_05_Restriction ("variant part is not allowed", Def);
21833 Analyze (Variant_Part (Component_List (Def)));
21834 end if;
21835 end if;
21837 -- After completing the semantic analysis of the record definition,
21838 -- record components, both new and inherited, are accessible. Set their
21839 -- kind accordingly. Exclude malformed itypes from illegal declarations,
21840 -- whose Ekind may be void.
21842 Component := First_Entity (Current_Scope);
21843 while Present (Component) loop
21844 if Ekind (Component) = E_Void
21845 and then not Is_Itype (Component)
21846 then
21847 Set_Ekind (Component, E_Component);
21848 Init_Component_Location (Component);
21849 end if;
21851 Propagate_Concurrent_Flags (T, Etype (Component));
21853 if Ekind (Component) /= E_Component then
21854 null;
21856 -- Do not set Has_Controlled_Component on a class-wide equivalent
21857 -- type. See Make_CW_Equivalent_Type.
21859 elsif not Is_Class_Wide_Equivalent_Type (T)
21860 and then (Has_Controlled_Component (Etype (Component))
21861 or else (Chars (Component) /= Name_uParent
21862 and then Is_Controlled_Active
21863 (Etype (Component))))
21864 then
21865 Set_Has_Controlled_Component (T, True);
21866 Final_Storage_Only :=
21867 Final_Storage_Only
21868 and then Finalize_Storage_Only (Etype (Component));
21869 Ctrl_Components := True;
21870 end if;
21872 Next_Entity (Component);
21873 end loop;
21875 -- A Type is Finalize_Storage_Only only if all its controlled components
21876 -- are also.
21878 if Ctrl_Components then
21879 Set_Finalize_Storage_Only (T, Final_Storage_Only);
21880 end if;
21882 -- Place reference to end record on the proper entity, which may
21883 -- be a partial view.
21885 if Present (Def) then
21886 Process_End_Label (Def, 'e', Prev_T);
21887 end if;
21888 end Record_Type_Definition;
21890 ------------------------
21891 -- Replace_Components --
21892 ------------------------
21894 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id) is
21895 function Process (N : Node_Id) return Traverse_Result;
21897 -------------
21898 -- Process --
21899 -------------
21901 function Process (N : Node_Id) return Traverse_Result is
21902 Comp : Entity_Id;
21904 begin
21905 if Nkind (N) = N_Discriminant_Specification then
21906 Comp := First_Discriminant (Typ);
21907 while Present (Comp) loop
21908 if Chars (Comp) = Chars (Defining_Identifier (N)) then
21909 Set_Defining_Identifier (N, Comp);
21910 exit;
21911 end if;
21913 Next_Discriminant (Comp);
21914 end loop;
21916 elsif Nkind (N) = N_Component_Declaration then
21917 Comp := First_Component (Typ);
21918 while Present (Comp) loop
21919 if Chars (Comp) = Chars (Defining_Identifier (N)) then
21920 Set_Defining_Identifier (N, Comp);
21921 exit;
21922 end if;
21924 Next_Component (Comp);
21925 end loop;
21926 end if;
21928 return OK;
21929 end Process;
21931 procedure Replace is new Traverse_Proc (Process);
21933 -- Start of processing for Replace_Components
21935 begin
21936 Replace (Decl);
21937 end Replace_Components;
21939 -------------------------------
21940 -- Set_Completion_Referenced --
21941 -------------------------------
21943 procedure Set_Completion_Referenced (E : Entity_Id) is
21944 begin
21945 -- If in main unit, mark entity that is a completion as referenced,
21946 -- warnings go on the partial view when needed.
21948 if In_Extended_Main_Source_Unit (E) then
21949 Set_Referenced (E);
21950 end if;
21951 end Set_Completion_Referenced;
21953 ---------------------
21954 -- Set_Default_SSO --
21955 ---------------------
21957 procedure Set_Default_SSO (T : Entity_Id) is
21958 begin
21959 case Opt.Default_SSO is
21960 when ' ' =>
21961 null;
21962 when 'L' =>
21963 Set_SSO_Set_Low_By_Default (T, True);
21964 when 'H' =>
21965 Set_SSO_Set_High_By_Default (T, True);
21966 when others =>
21967 raise Program_Error;
21968 end case;
21969 end Set_Default_SSO;
21971 ---------------------
21972 -- Set_Fixed_Range --
21973 ---------------------
21975 -- The range for fixed-point types is complicated by the fact that we
21976 -- do not know the exact end points at the time of the declaration. This
21977 -- is true for three reasons:
21979 -- A size clause may affect the fudging of the end-points.
21980 -- A small clause may affect the values of the end-points.
21981 -- We try to include the end-points if it does not affect the size.
21983 -- This means that the actual end-points must be established at the
21984 -- point when the type is frozen. Meanwhile, we first narrow the range
21985 -- as permitted (so that it will fit if necessary in a small specified
21986 -- size), and then build a range subtree with these narrowed bounds.
21987 -- Set_Fixed_Range constructs the range from real literal values, and
21988 -- sets the range as the Scalar_Range of the given fixed-point type entity.
21990 -- The parent of this range is set to point to the entity so that it is
21991 -- properly hooked into the tree (unlike normal Scalar_Range entries for
21992 -- other scalar types, which are just pointers to the range in the
21993 -- original tree, this would otherwise be an orphan).
21995 -- The tree is left unanalyzed. When the type is frozen, the processing
21996 -- in Freeze.Freeze_Fixed_Point_Type notices that the range is not
21997 -- analyzed, and uses this as an indication that it should complete
21998 -- work on the range (it will know the final small and size values).
22000 procedure Set_Fixed_Range
22001 (E : Entity_Id;
22002 Loc : Source_Ptr;
22003 Lo : Ureal;
22004 Hi : Ureal)
22006 S : constant Node_Id :=
22007 Make_Range (Loc,
22008 Low_Bound => Make_Real_Literal (Loc, Lo),
22009 High_Bound => Make_Real_Literal (Loc, Hi));
22010 begin
22011 Set_Scalar_Range (E, S);
22012 Set_Parent (S, E);
22014 -- Before the freeze point, the bounds of a fixed point are universal
22015 -- and carry the corresponding type.
22017 Set_Etype (Low_Bound (S), Universal_Real);
22018 Set_Etype (High_Bound (S), Universal_Real);
22019 end Set_Fixed_Range;
22021 ----------------------------------
22022 -- Set_Scalar_Range_For_Subtype --
22023 ----------------------------------
22025 procedure Set_Scalar_Range_For_Subtype
22026 (Def_Id : Entity_Id;
22027 R : Node_Id;
22028 Subt : Entity_Id)
22030 Kind : constant Entity_Kind := Ekind (Def_Id);
22032 begin
22033 -- Defend against previous error
22035 if Nkind (R) = N_Error then
22036 return;
22037 end if;
22039 Set_Scalar_Range (Def_Id, R);
22041 -- We need to link the range into the tree before resolving it so
22042 -- that types that are referenced, including importantly the subtype
22043 -- itself, are properly frozen (Freeze_Expression requires that the
22044 -- expression be properly linked into the tree). Of course if it is
22045 -- already linked in, then we do not disturb the current link.
22047 if No (Parent (R)) then
22048 Set_Parent (R, Def_Id);
22049 end if;
22051 -- Reset the kind of the subtype during analysis of the range, to
22052 -- catch possible premature use in the bounds themselves.
22054 Set_Ekind (Def_Id, E_Void);
22055 Process_Range_Expr_In_Decl (R, Subt, Subtyp => Def_Id);
22056 Set_Ekind (Def_Id, Kind);
22057 end Set_Scalar_Range_For_Subtype;
22059 --------------------------------------------------------
22060 -- Set_Stored_Constraint_From_Discriminant_Constraint --
22061 --------------------------------------------------------
22063 procedure Set_Stored_Constraint_From_Discriminant_Constraint
22064 (E : Entity_Id)
22066 begin
22067 -- Make sure set if encountered during Expand_To_Stored_Constraint
22069 Set_Stored_Constraint (E, No_Elist);
22071 -- Give it the right value
22073 if Is_Constrained (E) and then Has_Discriminants (E) then
22074 Set_Stored_Constraint (E,
22075 Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
22076 end if;
22077 end Set_Stored_Constraint_From_Discriminant_Constraint;
22079 -------------------------------------
22080 -- Signed_Integer_Type_Declaration --
22081 -------------------------------------
22083 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
22084 Implicit_Base : Entity_Id;
22085 Base_Typ : Entity_Id;
22086 Lo_Val : Uint;
22087 Hi_Val : Uint;
22088 Errs : Boolean := False;
22089 Lo : Node_Id;
22090 Hi : Node_Id;
22092 function Can_Derive_From (E : Entity_Id) return Boolean;
22093 -- Determine whether given bounds allow derivation from specified type
22095 procedure Check_Bound (Expr : Node_Id);
22096 -- Check bound to make sure it is integral and static. If not, post
22097 -- appropriate error message and set Errs flag
22099 ---------------------
22100 -- Can_Derive_From --
22101 ---------------------
22103 -- Note we check both bounds against both end values, to deal with
22104 -- strange types like ones with a range of 0 .. -12341234.
22106 function Can_Derive_From (E : Entity_Id) return Boolean is
22107 Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
22108 Hi : constant Uint := Expr_Value (Type_High_Bound (E));
22109 begin
22110 return Lo <= Lo_Val and then Lo_Val <= Hi
22111 and then
22112 Lo <= Hi_Val and then Hi_Val <= Hi;
22113 end Can_Derive_From;
22115 -----------------
22116 -- Check_Bound --
22117 -----------------
22119 procedure Check_Bound (Expr : Node_Id) is
22120 begin
22121 -- If a range constraint is used as an integer type definition, each
22122 -- bound of the range must be defined by a static expression of some
22123 -- integer type, but the two bounds need not have the same integer
22124 -- type (Negative bounds are allowed.) (RM 3.5.4)
22126 if not Is_Integer_Type (Etype (Expr)) then
22127 Error_Msg_N
22128 ("integer type definition bounds must be of integer type", Expr);
22129 Errs := True;
22131 elsif not Is_OK_Static_Expression (Expr) then
22132 Flag_Non_Static_Expr
22133 ("non-static expression used for integer type bound!", Expr);
22134 Errs := True;
22136 -- The bounds are folded into literals, and we set their type to be
22137 -- universal, to avoid typing difficulties: we cannot set the type
22138 -- of the literal to the new type, because this would be a forward
22139 -- reference for the back end, and if the original type is user-
22140 -- defined this can lead to spurious semantic errors (e.g. 2928-003).
22142 else
22143 if Is_Entity_Name (Expr) then
22144 Fold_Uint (Expr, Expr_Value (Expr), True);
22145 end if;
22147 Set_Etype (Expr, Universal_Integer);
22148 end if;
22149 end Check_Bound;
22151 -- Start of processing for Signed_Integer_Type_Declaration
22153 begin
22154 -- Create an anonymous base type
22156 Implicit_Base :=
22157 Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
22159 -- Analyze and check the bounds, they can be of any integer type
22161 Lo := Low_Bound (Def);
22162 Hi := High_Bound (Def);
22164 -- Arbitrarily use Integer as the type if either bound had an error
22166 if Hi = Error or else Lo = Error then
22167 Base_Typ := Any_Integer;
22168 Set_Error_Posted (T, True);
22170 -- Here both bounds are OK expressions
22172 else
22173 Analyze_And_Resolve (Lo, Any_Integer);
22174 Analyze_And_Resolve (Hi, Any_Integer);
22176 Check_Bound (Lo);
22177 Check_Bound (Hi);
22179 if Errs then
22180 Hi := Type_High_Bound (Standard_Long_Long_Integer);
22181 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
22182 end if;
22184 -- Find type to derive from
22186 Lo_Val := Expr_Value (Lo);
22187 Hi_Val := Expr_Value (Hi);
22189 if Can_Derive_From (Standard_Short_Short_Integer) then
22190 Base_Typ := Base_Type (Standard_Short_Short_Integer);
22192 elsif Can_Derive_From (Standard_Short_Integer) then
22193 Base_Typ := Base_Type (Standard_Short_Integer);
22195 elsif Can_Derive_From (Standard_Integer) then
22196 Base_Typ := Base_Type (Standard_Integer);
22198 elsif Can_Derive_From (Standard_Long_Integer) then
22199 Base_Typ := Base_Type (Standard_Long_Integer);
22201 elsif Can_Derive_From (Standard_Long_Long_Integer) then
22202 Check_Restriction (No_Long_Long_Integers, Def);
22203 Base_Typ := Base_Type (Standard_Long_Long_Integer);
22205 else
22206 Base_Typ := Base_Type (Standard_Long_Long_Integer);
22207 Error_Msg_N ("integer type definition bounds out of range", Def);
22208 Hi := Type_High_Bound (Standard_Long_Long_Integer);
22209 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
22210 end if;
22211 end if;
22213 -- Complete both implicit base and declared first subtype entities. The
22214 -- inheritance of the rep item chain ensures that SPARK-related pragmas
22215 -- are not clobbered when the signed integer type acts as a full view of
22216 -- a private type.
22218 Set_Etype (Implicit_Base, Base_Typ);
22219 Set_Size_Info (Implicit_Base, Base_Typ);
22220 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
22221 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
22222 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
22224 Set_Ekind (T, E_Signed_Integer_Subtype);
22225 Set_Etype (T, Implicit_Base);
22226 Set_Size_Info (T, Implicit_Base);
22227 Inherit_Rep_Item_Chain (T, Implicit_Base);
22228 Set_Scalar_Range (T, Def);
22229 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
22230 Set_Is_Constrained (T);
22231 end Signed_Integer_Type_Declaration;
22233 end Sem_Ch3;