2015-03-13 Robert Dewar <dewar@adacore.com>
[official-gcc.git] / gcc / ada / sem_ch3.adb
blob3ec9ab523aa5608e572ed1ce97a8b2d8ae7068a4
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-2015, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Debug; use Debug;
30 with Elists; use Elists;
31 with Einfo; use Einfo;
32 with Errout; use Errout;
33 with Eval_Fat; use Eval_Fat;
34 with Exp_Ch3; use Exp_Ch3;
35 with Exp_Ch9; use Exp_Ch9;
36 with Exp_Disp; use Exp_Disp;
37 with Exp_Dist; use Exp_Dist;
38 with Exp_Tss; use Exp_Tss;
39 with Exp_Util; use Exp_Util;
40 with Fname; use Fname;
41 with Freeze; use Freeze;
42 with 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_Ch10; use Sem_Ch10;
61 with Sem_Ch13; use Sem_Ch13;
62 with Sem_Dim; use Sem_Dim;
63 with Sem_Disp; use Sem_Disp;
64 with Sem_Dist; use Sem_Dist;
65 with Sem_Elim; use Sem_Elim;
66 with Sem_Eval; use Sem_Eval;
67 with Sem_Mech; use Sem_Mech;
68 with Sem_Prag; use Sem_Prag;
69 with Sem_Res; use Sem_Res;
70 with Sem_Smem; use Sem_Smem;
71 with Sem_Type; use Sem_Type;
72 with Sem_Util; use Sem_Util;
73 with Sem_Warn; use Sem_Warn;
74 with Stand; use Stand;
75 with Sinfo; use Sinfo;
76 with Sinput; use Sinput;
77 with Snames; use Snames;
78 with Targparm; use Targparm;
79 with Tbuild; use Tbuild;
80 with Ttypes; use Ttypes;
81 with Uintp; use Uintp;
82 with Urealp; use Urealp;
84 package body Sem_Ch3 is
86 -----------------------
87 -- Local Subprograms --
88 -----------------------
90 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id);
91 -- Ada 2005 (AI-251): Add the tag components corresponding to all the
92 -- abstract interface types implemented by a record type or a derived
93 -- record type.
95 procedure Analyze_Object_Contract (Obj_Id : Entity_Id);
96 -- Analyze all delayed pragmas chained on the contract of object Obj_Id as
97 -- if they appeared at the end of the declarative region. The pragmas to be
98 -- considered are:
99 -- Async_Readers
100 -- Async_Writers
101 -- Effective_Reads
102 -- Effective_Writes
103 -- Part_Of
105 procedure Build_Derived_Type
106 (N : Node_Id;
107 Parent_Type : Entity_Id;
108 Derived_Type : Entity_Id;
109 Is_Completion : Boolean;
110 Derive_Subps : Boolean := True);
111 -- Create and decorate a Derived_Type given the Parent_Type entity. N is
112 -- the N_Full_Type_Declaration node containing the derived type definition.
113 -- Parent_Type is the entity for the parent type in the derived type
114 -- definition and Derived_Type the actual derived type. Is_Completion must
115 -- be set to False if Derived_Type is the N_Defining_Identifier node in N
116 -- (i.e. Derived_Type = Defining_Identifier (N)). In this case N is not the
117 -- completion of a private type declaration. If Is_Completion is set to
118 -- True, N is the completion of a private type declaration and Derived_Type
119 -- is different from the defining identifier inside N (i.e. Derived_Type /=
120 -- Defining_Identifier (N)). Derive_Subps indicates whether the parent
121 -- subprograms should be derived. The only case where this parameter is
122 -- False is when Build_Derived_Type is recursively called to process an
123 -- implicit derived full type for a type derived from a private type (in
124 -- that case the subprograms must only be derived for the private view of
125 -- the type).
127 -- ??? These flags need a bit of re-examination and re-documentation:
128 -- ??? are they both necessary (both seem related to the recursion)?
130 procedure Build_Derived_Access_Type
131 (N : Node_Id;
132 Parent_Type : Entity_Id;
133 Derived_Type : Entity_Id);
134 -- Subsidiary procedure to Build_Derived_Type. For a derived access type,
135 -- create an implicit base if the parent type is constrained or if the
136 -- subtype indication has a constraint.
138 procedure Build_Derived_Array_Type
139 (N : Node_Id;
140 Parent_Type : Entity_Id;
141 Derived_Type : Entity_Id);
142 -- Subsidiary procedure to Build_Derived_Type. For a derived array type,
143 -- create an implicit base if the parent type is constrained or if the
144 -- subtype indication has a constraint.
146 procedure Build_Derived_Concurrent_Type
147 (N : Node_Id;
148 Parent_Type : Entity_Id;
149 Derived_Type : Entity_Id);
150 -- Subsidiary procedure to Build_Derived_Type. For a derived task or
151 -- protected type, inherit entries and protected subprograms, check
152 -- legality of discriminant constraints if any.
154 procedure Build_Derived_Enumeration_Type
155 (N : Node_Id;
156 Parent_Type : Entity_Id;
157 Derived_Type : Entity_Id);
158 -- Subsidiary procedure to Build_Derived_Type. For a derived enumeration
159 -- type, we must create a new list of literals. Types derived from
160 -- Character and [Wide_]Wide_Character are special-cased.
162 procedure Build_Derived_Numeric_Type
163 (N : Node_Id;
164 Parent_Type : Entity_Id;
165 Derived_Type : Entity_Id);
166 -- Subsidiary procedure to Build_Derived_Type. For numeric types, create
167 -- an anonymous base type, and propagate constraint to subtype if needed.
169 procedure Build_Derived_Private_Type
170 (N : Node_Id;
171 Parent_Type : Entity_Id;
172 Derived_Type : Entity_Id;
173 Is_Completion : Boolean;
174 Derive_Subps : Boolean := True);
175 -- Subsidiary procedure to Build_Derived_Type. This procedure is complex
176 -- because the parent may or may not have a completion, and the derivation
177 -- may itself be a completion.
179 procedure Build_Derived_Record_Type
180 (N : Node_Id;
181 Parent_Type : Entity_Id;
182 Derived_Type : Entity_Id;
183 Derive_Subps : Boolean := True);
184 -- Subsidiary procedure used for tagged and untagged record types
185 -- by Build_Derived_Type and Analyze_Private_Extension_Declaration.
186 -- All parameters are as in Build_Derived_Type except that N, in
187 -- addition to being an N_Full_Type_Declaration node, can also be an
188 -- N_Private_Extension_Declaration node. See the definition of this routine
189 -- for much more info. Derive_Subps indicates whether subprograms should be
190 -- derived from the parent type. The only case where Derive_Subps is False
191 -- is for an implicit derived full type for a type derived from a private
192 -- type (see Build_Derived_Type).
194 procedure Build_Discriminal (Discrim : Entity_Id);
195 -- Create the discriminal corresponding to discriminant Discrim, that is
196 -- the parameter corresponding to Discrim to be used in initialization
197 -- procedures for the type where Discrim is a discriminant. Discriminals
198 -- are not used during semantic analysis, and are not fully defined
199 -- entities until expansion. Thus they are not given a scope until
200 -- initialization procedures are built.
202 function Build_Discriminant_Constraints
203 (T : Entity_Id;
204 Def : Node_Id;
205 Derived_Def : Boolean := False) return Elist_Id;
206 -- Validate discriminant constraints and return the list of the constraints
207 -- in order of discriminant declarations, where T is the discriminated
208 -- unconstrained type. Def is the N_Subtype_Indication node where the
209 -- discriminants constraints for T are specified. Derived_Def is True
210 -- when building the discriminant constraints in a derived type definition
211 -- of the form "type D (...) is new T (xxx)". In this case T is the parent
212 -- type and Def is the constraint "(xxx)" on T and this routine sets the
213 -- Corresponding_Discriminant field of the discriminants in the derived
214 -- type D to point to the corresponding discriminants in the parent type T.
216 procedure Build_Discriminated_Subtype
217 (T : Entity_Id;
218 Def_Id : Entity_Id;
219 Elist : Elist_Id;
220 Related_Nod : Node_Id;
221 For_Access : Boolean := False);
222 -- Subsidiary procedure to Constrain_Discriminated_Type and to
223 -- Process_Incomplete_Dependents. Given
225 -- T (a possibly discriminated base type)
226 -- Def_Id (a very partially built subtype for T),
228 -- the call completes Def_Id to be the appropriate E_*_Subtype.
230 -- The Elist is the list of discriminant constraints if any (it is set
231 -- to No_Elist if T is not a discriminated type, and to an empty list if
232 -- T has discriminants but there are no discriminant constraints). The
233 -- Related_Nod is the same as Decl_Node in Create_Constrained_Components.
234 -- The For_Access says whether or not this subtype is really constraining
235 -- an access type. That is its sole purpose is the designated type of an
236 -- access type -- in which case a Private_Subtype Is_For_Access_Subtype
237 -- is built to avoid freezing T when the access subtype is frozen.
239 function Build_Scalar_Bound
240 (Bound : Node_Id;
241 Par_T : Entity_Id;
242 Der_T : Entity_Id) return Node_Id;
243 -- The bounds of a derived scalar type are conversions of the bounds of
244 -- the parent type. Optimize the representation if the bounds are literals.
245 -- Needs a more complete spec--what are the parameters exactly, and what
246 -- exactly is the returned value, and how is Bound affected???
248 procedure Build_Underlying_Full_View
249 (N : Node_Id;
250 Typ : Entity_Id;
251 Par : Entity_Id);
252 -- If the completion of a private type is itself derived from a private
253 -- type, or if the full view of a private subtype is itself private, the
254 -- back-end has no way to compute the actual size of this type. We build
255 -- an internal subtype declaration of the proper parent type to convey
256 -- this information. This extra mechanism is needed because a full
257 -- view cannot itself have a full view (it would get clobbered during
258 -- view exchanges).
260 procedure Check_Access_Discriminant_Requires_Limited
261 (D : Node_Id;
262 Loc : Node_Id);
263 -- Check the restriction that the type to which an access discriminant
264 -- belongs must be a concurrent type or a descendant of a type with
265 -- the reserved word 'limited' in its declaration.
267 procedure Check_Anonymous_Access_Components
268 (Typ_Decl : Node_Id;
269 Typ : Entity_Id;
270 Prev : Entity_Id;
271 Comp_List : Node_Id);
272 -- Ada 2005 AI-382: an access component in a record definition can refer to
273 -- the enclosing record, in which case it denotes the type itself, and not
274 -- the current instance of the type. We create an anonymous access type for
275 -- the component, and flag it as an access to a component, so accessibility
276 -- checks are properly performed on it. The declaration of the access type
277 -- is placed ahead of that of the record to prevent order-of-elaboration
278 -- circularity issues in Gigi. We create an incomplete type for the record
279 -- declaration, which is the designated type of the anonymous access.
281 procedure Check_Delta_Expression (E : Node_Id);
282 -- Check that the expression represented by E is suitable for use as a
283 -- delta expression, i.e. it is of real type and is static.
285 procedure Check_Digits_Expression (E : Node_Id);
286 -- Check that the expression represented by E is suitable for use as a
287 -- digits expression, i.e. it is of integer type, positive and static.
289 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id);
290 -- Validate the initialization of an object declaration. T is the required
291 -- type, and Exp is the initialization expression.
293 procedure Check_Interfaces (N : Node_Id; Def : Node_Id);
294 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
296 procedure Check_Or_Process_Discriminants
297 (N : Node_Id;
298 T : Entity_Id;
299 Prev : Entity_Id := Empty);
300 -- If N is the full declaration of the completion T of an incomplete or
301 -- private type, check its discriminants (which are already known to be
302 -- conformant with those of the partial view, see Find_Type_Name),
303 -- otherwise process them. Prev is the entity of the partial declaration,
304 -- if any.
306 procedure Check_Real_Bound (Bound : Node_Id);
307 -- Check given bound for being of real type and static. If not, post an
308 -- appropriate message, and rewrite the bound with the real literal zero.
310 procedure Constant_Redeclaration
311 (Id : Entity_Id;
312 N : Node_Id;
313 T : out Entity_Id);
314 -- Various checks on legality of full declaration of deferred constant.
315 -- Id is the entity for the redeclaration, N is the N_Object_Declaration,
316 -- node. The caller has not yet set any attributes of this entity.
318 function Contain_Interface
319 (Iface : Entity_Id;
320 Ifaces : Elist_Id) return Boolean;
321 -- Ada 2005: Determine whether Iface is present in the list Ifaces
323 procedure Convert_Scalar_Bounds
324 (N : Node_Id;
325 Parent_Type : Entity_Id;
326 Derived_Type : Entity_Id;
327 Loc : Source_Ptr);
328 -- For derived scalar types, convert the bounds in the type definition to
329 -- the derived type, and complete their analysis. Given a constraint of the
330 -- form ".. new T range Lo .. Hi", Lo and Hi are analyzed and resolved with
331 -- T'Base, the parent_type. The bounds of the derived type (the anonymous
332 -- base) are copies of Lo and Hi. Finally, the bounds of the derived
333 -- subtype are conversions of those bounds to the derived_type, so that
334 -- their typing is consistent.
336 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id);
337 -- Copies attributes from array base type T2 to array base type T1. Copies
338 -- only attributes that apply to base types, but not subtypes.
340 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id);
341 -- Copies attributes from array subtype T2 to array subtype T1. Copies
342 -- attributes that apply to both subtypes and base types.
344 procedure Create_Constrained_Components
345 (Subt : Entity_Id;
346 Decl_Node : Node_Id;
347 Typ : Entity_Id;
348 Constraints : Elist_Id);
349 -- Build the list of entities for a constrained discriminated record
350 -- subtype. If a component depends on a discriminant, replace its subtype
351 -- using the discriminant values in the discriminant constraint. Subt
352 -- is the defining identifier for the subtype whose list of constrained
353 -- entities we will create. Decl_Node is the type declaration node where
354 -- we will attach all the itypes created. Typ is the base discriminated
355 -- type for the subtype Subt. Constraints is the list of discriminant
356 -- constraints for Typ.
358 function Constrain_Component_Type
359 (Comp : Entity_Id;
360 Constrained_Typ : Entity_Id;
361 Related_Node : Node_Id;
362 Typ : Entity_Id;
363 Constraints : Elist_Id) return Entity_Id;
364 -- Given a discriminated base type Typ, a list of discriminant constraints,
365 -- Constraints, for Typ and a component Comp of Typ, create and return the
366 -- type corresponding to Etype (Comp) where all discriminant references
367 -- are replaced with the corresponding constraint. If Etype (Comp) contains
368 -- no discriminant references then it is returned as-is. Constrained_Typ
369 -- is the final constrained subtype to which the constrained component
370 -- belongs. Related_Node is the node where we attach all created itypes.
372 procedure Constrain_Access
373 (Def_Id : in out Entity_Id;
374 S : Node_Id;
375 Related_Nod : Node_Id);
376 -- Apply a list of constraints to an access type. If Def_Id is empty, it is
377 -- an anonymous type created for a subtype indication. In that case it is
378 -- created in the procedure and attached to Related_Nod.
380 procedure Constrain_Array
381 (Def_Id : in out Entity_Id;
382 SI : Node_Id;
383 Related_Nod : Node_Id;
384 Related_Id : Entity_Id;
385 Suffix : Character);
386 -- Apply a list of index constraints to an unconstrained array type. The
387 -- first parameter is the entity for the resulting subtype. A value of
388 -- Empty for Def_Id indicates that an implicit type must be created, but
389 -- creation is delayed (and must be done by this procedure) because other
390 -- subsidiary implicit types must be created first (which is why Def_Id
391 -- is an in/out parameter). The second parameter is a subtype indication
392 -- node for the constrained array to be created (e.g. something of the
393 -- form string (1 .. 10)). Related_Nod gives the place where this type
394 -- has to be inserted in the tree. The Related_Id and Suffix parameters
395 -- are used to build the associated Implicit type name.
397 procedure Constrain_Concurrent
398 (Def_Id : in out Entity_Id;
399 SI : Node_Id;
400 Related_Nod : Node_Id;
401 Related_Id : Entity_Id;
402 Suffix : Character);
403 -- Apply list of discriminant constraints to an unconstrained concurrent
404 -- type.
406 -- SI is the N_Subtype_Indication node containing the constraint and
407 -- the unconstrained type to constrain.
409 -- Def_Id is the entity for the resulting constrained subtype. A value
410 -- of Empty for Def_Id indicates that an implicit type must be created,
411 -- but creation is delayed (and must be done by this procedure) because
412 -- other subsidiary implicit types must be created first (which is why
413 -- Def_Id is an in/out parameter).
415 -- Related_Nod gives the place where this type has to be inserted
416 -- in the tree.
418 -- The last two arguments are used to create its external name if needed.
420 function Constrain_Corresponding_Record
421 (Prot_Subt : Entity_Id;
422 Corr_Rec : Entity_Id;
423 Related_Nod : Node_Id) return Entity_Id;
424 -- When constraining a protected type or task type with discriminants,
425 -- constrain the corresponding record with the same discriminant values.
427 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id);
428 -- Constrain a decimal fixed point type with a digits constraint and/or a
429 -- range constraint, and build E_Decimal_Fixed_Point_Subtype entity.
431 procedure Constrain_Discriminated_Type
432 (Def_Id : Entity_Id;
433 S : Node_Id;
434 Related_Nod : Node_Id;
435 For_Access : Boolean := False);
436 -- Process discriminant constraints of composite type. Verify that values
437 -- have been provided for all discriminants, that the original type is
438 -- unconstrained, and that the types of the supplied expressions match
439 -- the discriminant types. The first three parameters are like in routine
440 -- Constrain_Concurrent. See Build_Discriminated_Subtype for an explanation
441 -- of For_Access.
443 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id);
444 -- Constrain an enumeration type with a range constraint. This is identical
445 -- to Constrain_Integer, but for the Ekind of the resulting subtype.
447 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id);
448 -- Constrain a floating point type with either a digits constraint
449 -- and/or a range constraint, building a E_Floating_Point_Subtype.
451 procedure Constrain_Index
452 (Index : Node_Id;
453 S : Node_Id;
454 Related_Nod : Node_Id;
455 Related_Id : Entity_Id;
456 Suffix : Character;
457 Suffix_Index : Nat);
458 -- Process an index constraint S in a constrained array declaration. The
459 -- constraint can be a subtype name, or a range with or without an explicit
460 -- subtype mark. The index is the corresponding index of the unconstrained
461 -- array. The Related_Id and Suffix parameters are used to build the
462 -- associated Implicit type name.
464 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id);
465 -- Build subtype of a signed or modular integer type
467 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id);
468 -- Constrain an ordinary fixed point type with a range constraint, and
469 -- build an E_Ordinary_Fixed_Point_Subtype entity.
471 procedure Copy_And_Swap (Priv, Full : Entity_Id);
472 -- Copy the Priv entity into the entity of its full declaration then swap
473 -- the two entities in such a manner that the former private type is now
474 -- seen as a full type.
476 procedure Decimal_Fixed_Point_Type_Declaration
477 (T : Entity_Id;
478 Def : Node_Id);
479 -- Create a new decimal fixed point type, and apply the constraint to
480 -- obtain a subtype of this new type.
482 procedure Complete_Private_Subtype
483 (Priv : Entity_Id;
484 Full : Entity_Id;
485 Full_Base : Entity_Id;
486 Related_Nod : Node_Id);
487 -- Complete the implicit full view of a private subtype by setting the
488 -- appropriate semantic fields. If the full view of the parent is a record
489 -- type, build constrained components of subtype.
491 procedure Derive_Progenitor_Subprograms
492 (Parent_Type : Entity_Id;
493 Tagged_Type : Entity_Id);
494 -- Ada 2005 (AI-251): To complete type derivation, collect the primitive
495 -- operations of progenitors of Tagged_Type, and replace the subsidiary
496 -- subtypes with Tagged_Type, to build the specs of the inherited interface
497 -- primitives. The derived primitives are aliased to those of the
498 -- interface. This routine takes care also of transferring to the full view
499 -- subprograms associated with the partial view of Tagged_Type that cover
500 -- interface primitives.
502 procedure Derived_Standard_Character
503 (N : Node_Id;
504 Parent_Type : Entity_Id;
505 Derived_Type : Entity_Id);
506 -- Subsidiary procedure to Build_Derived_Enumeration_Type which handles
507 -- derivations from types Standard.Character and Standard.Wide_Character.
509 procedure Derived_Type_Declaration
510 (T : Entity_Id;
511 N : Node_Id;
512 Is_Completion : Boolean);
513 -- Process a derived type declaration. Build_Derived_Type is invoked
514 -- to process the actual derived type definition. Parameters N and
515 -- Is_Completion have the same meaning as in Build_Derived_Type.
516 -- T is the N_Defining_Identifier for the entity defined in the
517 -- N_Full_Type_Declaration node N, that is T is the derived type.
519 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id);
520 -- Insert each literal in symbol table, as an overloadable identifier. Each
521 -- enumeration type is mapped into a sequence of integers, and each literal
522 -- is defined as a constant with integer value. If any of the literals are
523 -- character literals, the type is a character type, which means that
524 -- strings are legal aggregates for arrays of components of the type.
526 function Expand_To_Stored_Constraint
527 (Typ : Entity_Id;
528 Constraint : Elist_Id) return Elist_Id;
529 -- Given a constraint (i.e. a list of expressions) on the discriminants of
530 -- Typ, expand it into a constraint on the stored discriminants and return
531 -- the new list of expressions constraining the stored discriminants.
533 function Find_Type_Of_Object
534 (Obj_Def : Node_Id;
535 Related_Nod : Node_Id) return Entity_Id;
536 -- Get type entity for object referenced by Obj_Def, attaching the implicit
537 -- types generated to Related_Nod.
539 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id);
540 -- Create a new float and apply the constraint to obtain subtype of it
542 function Has_Range_Constraint (N : Node_Id) return Boolean;
543 -- Given an N_Subtype_Indication node N, return True if a range constraint
544 -- is present, either directly, or as part of a digits or delta constraint.
545 -- In addition, a digits constraint in the decimal case returns True, since
546 -- it establishes a default range if no explicit range is present.
548 function Inherit_Components
549 (N : Node_Id;
550 Parent_Base : Entity_Id;
551 Derived_Base : Entity_Id;
552 Is_Tagged : Boolean;
553 Inherit_Discr : Boolean;
554 Discs : Elist_Id) return Elist_Id;
555 -- Called from Build_Derived_Record_Type to inherit the components of
556 -- Parent_Base (a base type) into the Derived_Base (the derived base type).
557 -- For more information on derived types and component inheritance please
558 -- consult the comment above the body of Build_Derived_Record_Type.
560 -- N is the original derived type declaration
562 -- Is_Tagged is set if we are dealing with tagged types
564 -- If Inherit_Discr is set, Derived_Base inherits its discriminants from
565 -- Parent_Base, otherwise no discriminants are inherited.
567 -- Discs gives the list of constraints that apply to Parent_Base in the
568 -- derived type declaration. If Discs is set to No_Elist, then we have
569 -- the following situation:
571 -- type Parent (D1..Dn : ..) is [tagged] record ...;
572 -- type Derived is new Parent [with ...];
574 -- which gets treated as
576 -- type Derived (D1..Dn : ..) is new Parent (D1,..,Dn) [with ...];
578 -- For untagged types the returned value is an association list. The list
579 -- starts from the association (Parent_Base => Derived_Base), and then it
580 -- contains a sequence of the associations of the form
582 -- (Old_Component => New_Component),
584 -- where Old_Component is the Entity_Id of a component in Parent_Base and
585 -- New_Component is the Entity_Id of the corresponding component in
586 -- Derived_Base. For untagged records, this association list is needed when
587 -- copying the record declaration for the derived base. In the tagged case
588 -- the value returned is irrelevant.
590 procedure Inherit_Predicate_Flags (Subt, Par : Entity_Id);
591 -- Propagate static and dynamic predicate flags from a parent to the
592 -- subtype in a subtype declaration with and without constraints.
594 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean;
595 -- Subsidiary to Check_Abstract_Overriding and Derive_Subprogram.
596 -- Determine whether subprogram Subp is a procedure subject to pragma
597 -- Extensions_Visible with value False and has at least one controlling
598 -- parameter of mode OUT.
600 function Is_Valid_Constraint_Kind
601 (T_Kind : Type_Kind;
602 Constraint_Kind : Node_Kind) return Boolean;
603 -- Returns True if it is legal to apply the given kind of constraint to the
604 -- given kind of type (index constraint to an array type, for example).
606 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id);
607 -- Create new modular type. Verify that modulus is in bounds
609 procedure New_Concatenation_Op (Typ : Entity_Id);
610 -- Create an abbreviated declaration for an operator in order to
611 -- materialize concatenation on array types.
613 procedure Ordinary_Fixed_Point_Type_Declaration
614 (T : Entity_Id;
615 Def : Node_Id);
616 -- Create a new ordinary fixed point type, and apply the constraint to
617 -- obtain subtype of it.
619 procedure Prepare_Private_Subtype_Completion
620 (Id : Entity_Id;
621 Related_Nod : Node_Id);
622 -- Id is a subtype of some private type. Creates the full declaration
623 -- associated with Id whenever possible, i.e. when the full declaration
624 -- of the base type is already known. Records each subtype into
625 -- Private_Dependents of the base type.
627 procedure Process_Incomplete_Dependents
628 (N : Node_Id;
629 Full_T : Entity_Id;
630 Inc_T : Entity_Id);
631 -- Process all entities that depend on an incomplete type. There include
632 -- subtypes, subprogram types that mention the incomplete type in their
633 -- profiles, and subprogram with access parameters that designate the
634 -- incomplete type.
636 -- Inc_T is the defining identifier of an incomplete type declaration, its
637 -- Ekind is E_Incomplete_Type.
639 -- N is the corresponding N_Full_Type_Declaration for Inc_T.
641 -- Full_T is N's defining identifier.
643 -- Subtypes of incomplete types with discriminants are completed when the
644 -- parent type is. This is simpler than private subtypes, because they can
645 -- only appear in the same scope, and there is no need to exchange views.
646 -- Similarly, access_to_subprogram types may have a parameter or a return
647 -- type that is an incomplete type, and that must be replaced with the
648 -- full type.
650 -- If the full type is tagged, subprogram with access parameters that
651 -- designated the incomplete may be primitive operations of the full type,
652 -- and have to be processed accordingly.
654 procedure Process_Real_Range_Specification (Def : Node_Id);
655 -- Given the type definition for a real type, this procedure processes and
656 -- checks the real range specification of this type definition if one is
657 -- present. If errors are found, error messages are posted, and the
658 -- Real_Range_Specification of Def is reset to Empty.
660 procedure Propagate_Default_Init_Cond_Attributes
661 (From_Typ : Entity_Id;
662 To_Typ : Entity_Id;
663 Parent_To_Derivation : Boolean := False;
664 Private_To_Full_View : Boolean := False);
665 -- Subsidiary to routines Build_Derived_Type and Process_Full_View. Inherit
666 -- all attributes related to pragma Default_Initial_Condition from From_Typ
667 -- to To_Typ. Flag Parent_To_Derivation should be set when the context is
668 -- the creation of a derived type. Flag Private_To_Full_View should be set
669 -- when processing both views of a private type.
671 procedure Record_Type_Declaration
672 (T : Entity_Id;
673 N : Node_Id;
674 Prev : Entity_Id);
675 -- Process a record type declaration (for both untagged and tagged
676 -- records). Parameters T and N are exactly like in procedure
677 -- Derived_Type_Declaration, except that no flag Is_Completion is needed
678 -- for this routine. If this is the completion of an incomplete type
679 -- declaration, Prev is the entity of the incomplete declaration, used for
680 -- cross-referencing. Otherwise Prev = T.
682 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id);
683 -- This routine is used to process the actual record type definition (both
684 -- for untagged and tagged records). Def is a record type definition node.
685 -- This procedure analyzes the components in this record type definition.
686 -- Prev_T is the entity for the enclosing record type. It is provided so
687 -- that its Has_Task flag can be set if any of the component have Has_Task
688 -- set. If the declaration is the completion of an incomplete type
689 -- declaration, Prev_T is the original incomplete type, whose full view is
690 -- the record type.
692 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id);
693 -- Subsidiary to Build_Derived_Record_Type. For untagged records, we
694 -- build a copy of the declaration tree of the parent, and we create
695 -- independently the list of components for the derived type. Semantic
696 -- information uses the component entities, but record representation
697 -- clauses are validated on the declaration tree. This procedure replaces
698 -- discriminants and components in the declaration with those that have
699 -- been created by Inherit_Components.
701 procedure Set_Fixed_Range
702 (E : Entity_Id;
703 Loc : Source_Ptr;
704 Lo : Ureal;
705 Hi : Ureal);
706 -- Build a range node with the given bounds and set it as the Scalar_Range
707 -- of the given fixed-point type entity. Loc is the source location used
708 -- for the constructed range. See body for further details.
710 procedure Set_Scalar_Range_For_Subtype
711 (Def_Id : Entity_Id;
712 R : Node_Id;
713 Subt : Entity_Id);
714 -- This routine is used to set the scalar range field for a subtype given
715 -- Def_Id, the entity for the subtype, and R, the range expression for the
716 -- scalar range. Subt provides the parent subtype to be used to analyze,
717 -- resolve, and check the given range.
719 procedure Set_Default_SSO (T : Entity_Id);
720 -- T is the entity for an array or record being declared. This procedure
721 -- sets the flags SSO_Set_Low_By_Default/SSO_Set_High_By_Default according
722 -- to the setting of Opt.Default_SSO.
724 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id);
725 -- Create a new signed integer entity, and apply the constraint to obtain
726 -- the required first named subtype of this type.
728 procedure Set_Stored_Constraint_From_Discriminant_Constraint
729 (E : Entity_Id);
730 -- E is some record type. This routine computes E's Stored_Constraint
731 -- from its Discriminant_Constraint.
733 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id);
734 -- Check that an entity in a list of progenitors is an interface,
735 -- emit error otherwise.
737 -----------------------
738 -- Access_Definition --
739 -----------------------
741 function Access_Definition
742 (Related_Nod : Node_Id;
743 N : Node_Id) return Entity_Id
745 Anon_Type : Entity_Id;
746 Anon_Scope : Entity_Id;
747 Desig_Type : Entity_Id;
748 Enclosing_Prot_Type : Entity_Id := Empty;
750 begin
751 Check_SPARK_05_Restriction ("access type is not allowed", N);
753 if Is_Entry (Current_Scope)
754 and then Is_Task_Type (Etype (Scope (Current_Scope)))
755 then
756 Error_Msg_N ("task entries cannot have access parameters", N);
757 return Empty;
758 end if;
760 -- Ada 2005: For an object declaration the corresponding anonymous
761 -- type is declared in the current scope.
763 -- If the access definition is the return type of another access to
764 -- function, scope is the current one, because it is the one of the
765 -- current type declaration, except for the pathological case below.
767 if Nkind_In (Related_Nod, N_Object_Declaration,
768 N_Access_Function_Definition)
769 then
770 Anon_Scope := Current_Scope;
772 -- A pathological case: function returning access functions that
773 -- return access functions, etc. Each anonymous access type created
774 -- is in the enclosing scope of the outermost function.
776 declare
777 Par : Node_Id;
779 begin
780 Par := Related_Nod;
781 while Nkind_In (Par, N_Access_Function_Definition,
782 N_Access_Definition)
783 loop
784 Par := Parent (Par);
785 end loop;
787 if Nkind (Par) = N_Function_Specification then
788 Anon_Scope := Scope (Defining_Entity (Par));
789 end if;
790 end;
792 -- For the anonymous function result case, retrieve the scope of the
793 -- function specification's associated entity rather than using the
794 -- current scope. The current scope will be the function itself if the
795 -- formal part is currently being analyzed, but will be the parent scope
796 -- in the case of a parameterless function, and we always want to use
797 -- the function's parent scope. Finally, if the function is a child
798 -- unit, we must traverse the tree to retrieve the proper entity.
800 elsif Nkind (Related_Nod) = N_Function_Specification
801 and then Nkind (Parent (N)) /= N_Parameter_Specification
802 then
803 -- If the current scope is a protected type, the anonymous access
804 -- is associated with one of the protected operations, and must
805 -- be available in the scope that encloses the protected declaration.
806 -- Otherwise the type is in the scope enclosing the subprogram.
808 -- If the function has formals, The return type of a subprogram
809 -- declaration is analyzed in the scope of the subprogram (see
810 -- Process_Formals) and thus the protected type, if present, is
811 -- the scope of the current function scope.
813 if Ekind (Current_Scope) = E_Protected_Type then
814 Enclosing_Prot_Type := Current_Scope;
816 elsif Ekind (Current_Scope) = E_Function
817 and then Ekind (Scope (Current_Scope)) = E_Protected_Type
818 then
819 Enclosing_Prot_Type := Scope (Current_Scope);
820 end if;
822 if Present (Enclosing_Prot_Type) then
823 Anon_Scope := Scope (Enclosing_Prot_Type);
825 else
826 Anon_Scope := Scope (Defining_Entity (Related_Nod));
827 end if;
829 -- For an access type definition, if the current scope is a child
830 -- unit it is the scope of the type.
832 elsif Is_Compilation_Unit (Current_Scope) then
833 Anon_Scope := Current_Scope;
835 -- For access formals, access components, and access discriminants, the
836 -- scope is that of the enclosing declaration,
838 else
839 Anon_Scope := Scope (Current_Scope);
840 end if;
842 Anon_Type :=
843 Create_Itype
844 (E_Anonymous_Access_Type, Related_Nod, Scope_Id => Anon_Scope);
846 if All_Present (N)
847 and then Ada_Version >= Ada_2005
848 then
849 Error_Msg_N ("ALL is not permitted for anonymous access types", N);
850 end if;
852 -- Ada 2005 (AI-254): In case of anonymous access to subprograms call
853 -- the corresponding semantic routine
855 if Present (Access_To_Subprogram_Definition (N)) then
857 -- Compiler runtime units are compiled in Ada 2005 mode when building
858 -- the runtime library but must also be compilable in Ada 95 mode
859 -- (when bootstrapping the compiler).
861 Check_Compiler_Unit ("anonymous access to subprogram", N);
863 Access_Subprogram_Declaration
864 (T_Name => Anon_Type,
865 T_Def => Access_To_Subprogram_Definition (N));
867 if Ekind (Anon_Type) = E_Access_Protected_Subprogram_Type then
868 Set_Ekind
869 (Anon_Type, E_Anonymous_Access_Protected_Subprogram_Type);
870 else
871 Set_Ekind (Anon_Type, E_Anonymous_Access_Subprogram_Type);
872 end if;
874 Set_Can_Use_Internal_Rep
875 (Anon_Type, not Always_Compatible_Rep_On_Target);
877 -- If the anonymous access is associated with a protected operation,
878 -- create a reference to it after the enclosing protected definition
879 -- because the itype will be used in the subsequent bodies.
881 -- If the anonymous access itself is protected, a full type
882 -- declaratiton will be created for it, so that the equivalent
883 -- record type can be constructed. For further details, see
884 -- Replace_Anonymous_Access_To_Protected-Subprogram.
886 if Ekind (Current_Scope) = E_Protected_Type
887 and then not Protected_Present (Access_To_Subprogram_Definition (N))
888 then
889 Build_Itype_Reference (Anon_Type, Parent (Current_Scope));
890 end if;
892 return Anon_Type;
893 end if;
895 Find_Type (Subtype_Mark (N));
896 Desig_Type := Entity (Subtype_Mark (N));
898 Set_Directly_Designated_Type (Anon_Type, Desig_Type);
899 Set_Etype (Anon_Type, Anon_Type);
901 -- Make sure the anonymous access type has size and alignment fields
902 -- set, as required by gigi. This is necessary in the case of the
903 -- Task_Body_Procedure.
905 if not Has_Private_Component (Desig_Type) then
906 Layout_Type (Anon_Type);
907 end if;
909 -- Ada 2005 (AI-231): Ada 2005 semantics for anonymous access differs
910 -- from Ada 95 semantics. In Ada 2005, anonymous access must specify if
911 -- the null value is allowed. In Ada 95 the null value is never allowed.
913 if Ada_Version >= Ada_2005 then
914 Set_Can_Never_Be_Null (Anon_Type, Null_Exclusion_Present (N));
915 else
916 Set_Can_Never_Be_Null (Anon_Type, True);
917 end if;
919 -- The anonymous access type is as public as the discriminated type or
920 -- subprogram that defines it. It is imported (for back-end purposes)
921 -- if the designated type is.
923 Set_Is_Public (Anon_Type, Is_Public (Scope (Anon_Type)));
925 -- Ada 2005 (AI-231): Propagate the access-constant attribute
927 Set_Is_Access_Constant (Anon_Type, Constant_Present (N));
929 -- The context is either a subprogram declaration, object declaration,
930 -- or an access discriminant, in a private or a full type declaration.
931 -- In the case of a subprogram, if the designated type is incomplete,
932 -- the operation will be a primitive operation of the full type, to be
933 -- updated subsequently. If the type is imported through a limited_with
934 -- clause, the subprogram is not a primitive operation of the type
935 -- (which is declared elsewhere in some other scope).
937 if Ekind (Desig_Type) = E_Incomplete_Type
938 and then not From_Limited_With (Desig_Type)
939 and then Is_Overloadable (Current_Scope)
940 then
941 Append_Elmt (Current_Scope, Private_Dependents (Desig_Type));
942 Set_Has_Delayed_Freeze (Current_Scope);
943 end if;
945 -- Ada 2005: If the designated type is an interface that may contain
946 -- tasks, create a Master entity for the declaration. This must be done
947 -- before expansion of the full declaration, because the declaration may
948 -- include an expression that is an allocator, whose expansion needs the
949 -- proper Master for the created tasks.
951 if Nkind (Related_Nod) = N_Object_Declaration and then Expander_Active
952 then
953 if Is_Interface (Desig_Type) and then Is_Limited_Record (Desig_Type)
954 then
955 Build_Class_Wide_Master (Anon_Type);
957 -- Similarly, if the type is an anonymous access that designates
958 -- tasks, create a master entity for it in the current context.
960 elsif Has_Task (Desig_Type) and then Comes_From_Source (Related_Nod)
961 then
962 Build_Master_Entity (Defining_Identifier (Related_Nod));
963 Build_Master_Renaming (Anon_Type);
964 end if;
965 end if;
967 -- For a private component of a protected type, it is imperative that
968 -- the back-end elaborate the type immediately after the protected
969 -- declaration, because this type will be used in the declarations
970 -- created for the component within each protected body, so we must
971 -- create an itype reference for it now.
973 if Nkind (Parent (Related_Nod)) = N_Protected_Definition then
974 Build_Itype_Reference (Anon_Type, Parent (Parent (Related_Nod)));
976 -- Similarly, if the access definition is the return result of a
977 -- function, create an itype reference for it because it will be used
978 -- within the function body. For a regular function that is not a
979 -- compilation unit, insert reference after the declaration. For a
980 -- protected operation, insert it after the enclosing protected type
981 -- declaration. In either case, do not create a reference for a type
982 -- obtained through a limited_with clause, because this would introduce
983 -- semantic dependencies.
985 -- Similarly, do not create a reference if the designated type is a
986 -- generic formal, because no use of it will reach the backend.
988 elsif Nkind (Related_Nod) = N_Function_Specification
989 and then not From_Limited_With (Desig_Type)
990 and then not Is_Generic_Type (Desig_Type)
991 then
992 if Present (Enclosing_Prot_Type) then
993 Build_Itype_Reference (Anon_Type, Parent (Enclosing_Prot_Type));
995 elsif Is_List_Member (Parent (Related_Nod))
996 and then Nkind (Parent (N)) /= N_Parameter_Specification
997 then
998 Build_Itype_Reference (Anon_Type, Parent (Related_Nod));
999 end if;
1001 -- Finally, create an itype reference for an object declaration of an
1002 -- anonymous access type. This is strictly necessary only for deferred
1003 -- constants, but in any case will avoid out-of-scope problems in the
1004 -- back-end.
1006 elsif Nkind (Related_Nod) = N_Object_Declaration then
1007 Build_Itype_Reference (Anon_Type, Related_Nod);
1008 end if;
1010 return Anon_Type;
1011 end Access_Definition;
1013 -----------------------------------
1014 -- Access_Subprogram_Declaration --
1015 -----------------------------------
1017 procedure Access_Subprogram_Declaration
1018 (T_Name : Entity_Id;
1019 T_Def : Node_Id)
1021 procedure Check_For_Premature_Usage (Def : Node_Id);
1022 -- Check that type T_Name is not used, directly or recursively, as a
1023 -- parameter or a return type in Def. Def is either a subtype, an
1024 -- access_definition, or an access_to_subprogram_definition.
1026 -------------------------------
1027 -- Check_For_Premature_Usage --
1028 -------------------------------
1030 procedure Check_For_Premature_Usage (Def : Node_Id) is
1031 Param : Node_Id;
1033 begin
1034 -- Check for a subtype mark
1036 if Nkind (Def) in N_Has_Etype then
1037 if Etype (Def) = T_Name then
1038 Error_Msg_N
1039 ("type& cannot be used before end of its declaration", Def);
1040 end if;
1042 -- If this is not a subtype, then this is an access_definition
1044 elsif Nkind (Def) = N_Access_Definition then
1045 if Present (Access_To_Subprogram_Definition (Def)) then
1046 Check_For_Premature_Usage
1047 (Access_To_Subprogram_Definition (Def));
1048 else
1049 Check_For_Premature_Usage (Subtype_Mark (Def));
1050 end if;
1052 -- The only cases left are N_Access_Function_Definition and
1053 -- N_Access_Procedure_Definition.
1055 else
1056 if Present (Parameter_Specifications (Def)) then
1057 Param := First (Parameter_Specifications (Def));
1058 while Present (Param) loop
1059 Check_For_Premature_Usage (Parameter_Type (Param));
1060 Param := Next (Param);
1061 end loop;
1062 end if;
1064 if Nkind (Def) = N_Access_Function_Definition then
1065 Check_For_Premature_Usage (Result_Definition (Def));
1066 end if;
1067 end if;
1068 end Check_For_Premature_Usage;
1070 -- Local variables
1072 Formals : constant List_Id := Parameter_Specifications (T_Def);
1073 Formal : Entity_Id;
1074 D_Ityp : Node_Id;
1075 Desig_Type : constant Entity_Id :=
1076 Create_Itype (E_Subprogram_Type, Parent (T_Def));
1078 -- Start of processing for Access_Subprogram_Declaration
1080 begin
1081 Check_SPARK_05_Restriction ("access type is not allowed", T_Def);
1083 -- Associate the Itype node with the inner full-type declaration or
1084 -- subprogram spec or entry body. This is required to handle nested
1085 -- anonymous declarations. For example:
1087 -- procedure P
1088 -- (X : access procedure
1089 -- (Y : access procedure
1090 -- (Z : access T)))
1092 D_Ityp := Associated_Node_For_Itype (Desig_Type);
1093 while not (Nkind_In (D_Ityp, N_Full_Type_Declaration,
1094 N_Private_Type_Declaration,
1095 N_Private_Extension_Declaration,
1096 N_Procedure_Specification,
1097 N_Function_Specification,
1098 N_Entry_Body)
1100 or else
1101 Nkind_In (D_Ityp, N_Object_Declaration,
1102 N_Object_Renaming_Declaration,
1103 N_Formal_Object_Declaration,
1104 N_Formal_Type_Declaration,
1105 N_Task_Type_Declaration,
1106 N_Protected_Type_Declaration))
1107 loop
1108 D_Ityp := Parent (D_Ityp);
1109 pragma Assert (D_Ityp /= Empty);
1110 end loop;
1112 Set_Associated_Node_For_Itype (Desig_Type, D_Ityp);
1114 if Nkind_In (D_Ityp, N_Procedure_Specification,
1115 N_Function_Specification)
1116 then
1117 Set_Scope (Desig_Type, Scope (Defining_Entity (D_Ityp)));
1119 elsif Nkind_In (D_Ityp, N_Full_Type_Declaration,
1120 N_Object_Declaration,
1121 N_Object_Renaming_Declaration,
1122 N_Formal_Type_Declaration)
1123 then
1124 Set_Scope (Desig_Type, Scope (Defining_Identifier (D_Ityp)));
1125 end if;
1127 if Nkind (T_Def) = N_Access_Function_Definition then
1128 if Nkind (Result_Definition (T_Def)) = N_Access_Definition then
1129 declare
1130 Acc : constant Node_Id := Result_Definition (T_Def);
1132 begin
1133 if Present (Access_To_Subprogram_Definition (Acc))
1134 and then
1135 Protected_Present (Access_To_Subprogram_Definition (Acc))
1136 then
1137 Set_Etype
1138 (Desig_Type,
1139 Replace_Anonymous_Access_To_Protected_Subprogram
1140 (T_Def));
1142 else
1143 Set_Etype
1144 (Desig_Type,
1145 Access_Definition (T_Def, Result_Definition (T_Def)));
1146 end if;
1147 end;
1149 else
1150 Analyze (Result_Definition (T_Def));
1152 declare
1153 Typ : constant Entity_Id := Entity (Result_Definition (T_Def));
1155 begin
1156 -- If a null exclusion is imposed on the result type, then
1157 -- create a null-excluding itype (an access subtype) and use
1158 -- it as the function's Etype.
1160 if Is_Access_Type (Typ)
1161 and then Null_Exclusion_In_Return_Present (T_Def)
1162 then
1163 Set_Etype (Desig_Type,
1164 Create_Null_Excluding_Itype
1165 (T => Typ,
1166 Related_Nod => T_Def,
1167 Scope_Id => Current_Scope));
1169 else
1170 if From_Limited_With (Typ) then
1172 -- AI05-151: Incomplete types are allowed in all basic
1173 -- declarations, including access to subprograms.
1175 if Ada_Version >= Ada_2012 then
1176 null;
1178 else
1179 Error_Msg_NE
1180 ("illegal use of incomplete type&",
1181 Result_Definition (T_Def), Typ);
1182 end if;
1184 elsif Ekind (Current_Scope) = E_Package
1185 and then In_Private_Part (Current_Scope)
1186 then
1187 if Ekind (Typ) = E_Incomplete_Type then
1188 Append_Elmt (Desig_Type, Private_Dependents (Typ));
1190 elsif Is_Class_Wide_Type (Typ)
1191 and then Ekind (Etype (Typ)) = E_Incomplete_Type
1192 then
1193 Append_Elmt
1194 (Desig_Type, Private_Dependents (Etype (Typ)));
1195 end if;
1196 end if;
1198 Set_Etype (Desig_Type, Typ);
1199 end if;
1200 end;
1201 end if;
1203 if not (Is_Type (Etype (Desig_Type))) then
1204 Error_Msg_N
1205 ("expect type in function specification",
1206 Result_Definition (T_Def));
1207 end if;
1209 else
1210 Set_Etype (Desig_Type, Standard_Void_Type);
1211 end if;
1213 if Present (Formals) then
1214 Push_Scope (Desig_Type);
1216 -- Some special tests here. These special tests can be removed
1217 -- if and when Itypes always have proper parent pointers to their
1218 -- declarations???
1220 -- Special test 1) Link defining_identifier of formals. Required by
1221 -- First_Formal to provide its functionality.
1223 declare
1224 F : Node_Id;
1226 begin
1227 F := First (Formals);
1229 -- In ASIS mode, the access_to_subprogram may be analyzed twice,
1230 -- when it is part of an unconstrained type and subtype expansion
1231 -- is disabled. To avoid back-end problems with shared profiles,
1232 -- use previous subprogram type as the designated type, and then
1233 -- remove scope added above.
1235 if ASIS_Mode and then Present (Scope (Defining_Identifier (F)))
1236 then
1237 Set_Etype (T_Name, T_Name);
1238 Init_Size_Align (T_Name);
1239 Set_Directly_Designated_Type (T_Name,
1240 Scope (Defining_Identifier (F)));
1241 End_Scope;
1242 return;
1243 end if;
1245 while Present (F) loop
1246 if No (Parent (Defining_Identifier (F))) then
1247 Set_Parent (Defining_Identifier (F), F);
1248 end if;
1250 Next (F);
1251 end loop;
1252 end;
1254 Process_Formals (Formals, Parent (T_Def));
1256 -- Special test 2) End_Scope requires that the parent pointer be set
1257 -- to something reasonable, but Itypes don't have parent pointers. So
1258 -- we set it and then unset it ???
1260 Set_Parent (Desig_Type, T_Name);
1261 End_Scope;
1262 Set_Parent (Desig_Type, Empty);
1263 end if;
1265 -- Check for premature usage of the type being defined
1267 Check_For_Premature_Usage (T_Def);
1269 -- The return type and/or any parameter type may be incomplete. Mark the
1270 -- subprogram_type as depending on the incomplete type, so that it can
1271 -- be updated when the full type declaration is seen. This only applies
1272 -- to incomplete types declared in some enclosing scope, not to limited
1273 -- views from other packages.
1275 -- Prior to Ada 2012, access to functions can only have in_parameters.
1277 if Present (Formals) then
1278 Formal := First_Formal (Desig_Type);
1279 while Present (Formal) loop
1280 if Ekind (Formal) /= E_In_Parameter
1281 and then Nkind (T_Def) = N_Access_Function_Definition
1282 and then Ada_Version < Ada_2012
1283 then
1284 Error_Msg_N ("functions can only have IN parameters", Formal);
1285 end if;
1287 if Ekind (Etype (Formal)) = E_Incomplete_Type
1288 and then In_Open_Scopes (Scope (Etype (Formal)))
1289 then
1290 Append_Elmt (Desig_Type, Private_Dependents (Etype (Formal)));
1291 Set_Has_Delayed_Freeze (Desig_Type);
1292 end if;
1294 Next_Formal (Formal);
1295 end loop;
1296 end if;
1298 -- Check whether an indirect call without actuals may be possible. This
1299 -- is used when resolving calls whose result is then indexed.
1301 May_Need_Actuals (Desig_Type);
1303 -- If the return type is incomplete, this is legal as long as the type
1304 -- is declared in the current scope and will be completed in it (rather
1305 -- than being part of limited view).
1307 if Ekind (Etype (Desig_Type)) = E_Incomplete_Type
1308 and then not Has_Delayed_Freeze (Desig_Type)
1309 and then In_Open_Scopes (Scope (Etype (Desig_Type)))
1310 then
1311 Append_Elmt (Desig_Type, Private_Dependents (Etype (Desig_Type)));
1312 Set_Has_Delayed_Freeze (Desig_Type);
1313 end if;
1315 Check_Delayed_Subprogram (Desig_Type);
1317 if Protected_Present (T_Def) then
1318 Set_Ekind (T_Name, E_Access_Protected_Subprogram_Type);
1319 Set_Convention (Desig_Type, Convention_Protected);
1320 else
1321 Set_Ekind (T_Name, E_Access_Subprogram_Type);
1322 end if;
1324 Set_Can_Use_Internal_Rep (T_Name, not Always_Compatible_Rep_On_Target);
1326 Set_Etype (T_Name, T_Name);
1327 Init_Size_Align (T_Name);
1328 Set_Directly_Designated_Type (T_Name, Desig_Type);
1330 Generate_Reference_To_Formals (T_Name);
1332 -- Ada 2005 (AI-231): Propagate the null-excluding attribute
1334 Set_Can_Never_Be_Null (T_Name, Null_Exclusion_Present (T_Def));
1336 Check_Restriction (No_Access_Subprograms, T_Def);
1337 end Access_Subprogram_Declaration;
1339 ----------------------------
1340 -- Access_Type_Declaration --
1341 ----------------------------
1343 procedure Access_Type_Declaration (T : Entity_Id; Def : Node_Id) is
1344 P : constant Node_Id := Parent (Def);
1345 S : constant Node_Id := Subtype_Indication (Def);
1347 Full_Desig : Entity_Id;
1349 begin
1350 Check_SPARK_05_Restriction ("access type is not allowed", Def);
1352 -- Check for permissible use of incomplete type
1354 if Nkind (S) /= N_Subtype_Indication then
1355 Analyze (S);
1357 if Ekind (Root_Type (Entity (S))) = E_Incomplete_Type then
1358 Set_Directly_Designated_Type (T, Entity (S));
1360 -- If the designated type is a limited view, we cannot tell if
1361 -- the full view contains tasks, and there is no way to handle
1362 -- that full view in a client. We create a master entity for the
1363 -- scope, which will be used when a client determines that one
1364 -- is needed.
1366 if From_Limited_With (Entity (S))
1367 and then not Is_Class_Wide_Type (Entity (S))
1368 then
1369 Set_Ekind (T, E_Access_Type);
1370 Build_Master_Entity (T);
1371 Build_Master_Renaming (T);
1372 end if;
1374 else
1375 Set_Directly_Designated_Type (T, Process_Subtype (S, P, T, 'P'));
1376 end if;
1378 -- If the access definition is of the form: ACCESS NOT NULL ..
1379 -- the subtype indication must be of an access type. Create
1380 -- a null-excluding subtype of it.
1382 if Null_Excluding_Subtype (Def) then
1383 if not Is_Access_Type (Entity (S)) then
1384 Error_Msg_N ("null exclusion must apply to access type", Def);
1386 else
1387 declare
1388 Loc : constant Source_Ptr := Sloc (S);
1389 Decl : Node_Id;
1390 Nam : constant Entity_Id := Make_Temporary (Loc, 'S');
1392 begin
1393 Decl :=
1394 Make_Subtype_Declaration (Loc,
1395 Defining_Identifier => Nam,
1396 Subtype_Indication =>
1397 New_Occurrence_Of (Entity (S), Loc));
1398 Set_Null_Exclusion_Present (Decl);
1399 Insert_Before (Parent (Def), Decl);
1400 Analyze (Decl);
1401 Set_Entity (S, Nam);
1402 end;
1403 end if;
1404 end if;
1406 else
1407 Set_Directly_Designated_Type (T,
1408 Process_Subtype (S, P, T, 'P'));
1409 end if;
1411 if All_Present (Def) or Constant_Present (Def) then
1412 Set_Ekind (T, E_General_Access_Type);
1413 else
1414 Set_Ekind (T, E_Access_Type);
1415 end if;
1417 Full_Desig := Designated_Type (T);
1419 if Base_Type (Full_Desig) = T then
1420 Error_Msg_N ("access type cannot designate itself", S);
1422 -- In Ada 2005, the type may have a limited view through some unit in
1423 -- its own context, allowing the following circularity that cannot be
1424 -- detected earlier.
1426 elsif Is_Class_Wide_Type (Full_Desig) and then Etype (Full_Desig) = T
1427 then
1428 Error_Msg_N
1429 ("access type cannot designate its own classwide type", S);
1431 -- Clean up indication of tagged status to prevent cascaded errors
1433 Set_Is_Tagged_Type (T, False);
1434 end if;
1436 Set_Etype (T, T);
1438 -- If the type has appeared already in a with_type clause, it is frozen
1439 -- and the pointer size is already set. Else, initialize.
1441 if not From_Limited_With (T) then
1442 Init_Size_Align (T);
1443 end if;
1445 -- Note that Has_Task is always false, since the access type itself
1446 -- is not a task type. See Einfo for more description on this point.
1447 -- Exactly the same consideration applies to Has_Controlled_Component
1448 -- and to Has_Protected.
1450 Set_Has_Task (T, False);
1451 Set_Has_Controlled_Component (T, False);
1452 Set_Has_Protected (T, False);
1454 -- Initialize field Finalization_Master explicitly to Empty, to avoid
1455 -- problems where an incomplete view of this entity has been previously
1456 -- established by a limited with and an overlaid version of this field
1457 -- (Stored_Constraint) was initialized for the incomplete view.
1459 -- This reset is performed in most cases except where the access type
1460 -- has been created for the purposes of allocating or deallocating a
1461 -- build-in-place object. Such access types have explicitly set pools
1462 -- and finalization masters.
1464 if No (Associated_Storage_Pool (T)) then
1465 Set_Finalization_Master (T, Empty);
1466 end if;
1468 -- Ada 2005 (AI-231): Propagate the null-excluding and access-constant
1469 -- attributes
1471 Set_Can_Never_Be_Null (T, Null_Exclusion_Present (Def));
1472 Set_Is_Access_Constant (T, Constant_Present (Def));
1473 end Access_Type_Declaration;
1475 ----------------------------------
1476 -- Add_Interface_Tag_Components --
1477 ----------------------------------
1479 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id) is
1480 Loc : constant Source_Ptr := Sloc (N);
1481 L : List_Id;
1482 Last_Tag : Node_Id;
1484 procedure Add_Tag (Iface : Entity_Id);
1485 -- Add tag for one of the progenitor interfaces
1487 -------------
1488 -- Add_Tag --
1489 -------------
1491 procedure Add_Tag (Iface : Entity_Id) is
1492 Decl : Node_Id;
1493 Def : Node_Id;
1494 Tag : Entity_Id;
1495 Offset : Entity_Id;
1497 begin
1498 pragma Assert (Is_Tagged_Type (Iface) and then Is_Interface (Iface));
1500 -- This is a reasonable place to propagate predicates
1502 if Has_Predicates (Iface) then
1503 Set_Has_Predicates (Typ);
1504 end if;
1506 Def :=
1507 Make_Component_Definition (Loc,
1508 Aliased_Present => True,
1509 Subtype_Indication =>
1510 New_Occurrence_Of (RTE (RE_Interface_Tag), Loc));
1512 Tag := Make_Temporary (Loc, 'V');
1514 Decl :=
1515 Make_Component_Declaration (Loc,
1516 Defining_Identifier => Tag,
1517 Component_Definition => Def);
1519 Analyze_Component_Declaration (Decl);
1521 Set_Analyzed (Decl);
1522 Set_Ekind (Tag, E_Component);
1523 Set_Is_Tag (Tag);
1524 Set_Is_Aliased (Tag);
1525 Set_Related_Type (Tag, Iface);
1526 Init_Component_Location (Tag);
1528 pragma Assert (Is_Frozen (Iface));
1530 Set_DT_Entry_Count (Tag,
1531 DT_Entry_Count (First_Entity (Iface)));
1533 if No (Last_Tag) then
1534 Prepend (Decl, L);
1535 else
1536 Insert_After (Last_Tag, Decl);
1537 end if;
1539 Last_Tag := Decl;
1541 -- If the ancestor has discriminants we need to give special support
1542 -- to store the offset_to_top value of the secondary dispatch tables.
1543 -- For this purpose we add a supplementary component just after the
1544 -- field that contains the tag associated with each secondary DT.
1546 if Typ /= Etype (Typ) and then Has_Discriminants (Etype (Typ)) then
1547 Def :=
1548 Make_Component_Definition (Loc,
1549 Subtype_Indication =>
1550 New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
1552 Offset := Make_Temporary (Loc, 'V');
1554 Decl :=
1555 Make_Component_Declaration (Loc,
1556 Defining_Identifier => Offset,
1557 Component_Definition => Def);
1559 Analyze_Component_Declaration (Decl);
1561 Set_Analyzed (Decl);
1562 Set_Ekind (Offset, E_Component);
1563 Set_Is_Aliased (Offset);
1564 Set_Related_Type (Offset, Iface);
1565 Init_Component_Location (Offset);
1566 Insert_After (Last_Tag, Decl);
1567 Last_Tag := Decl;
1568 end if;
1569 end Add_Tag;
1571 -- Local variables
1573 Elmt : Elmt_Id;
1574 Ext : Node_Id;
1575 Comp : Node_Id;
1577 -- Start of processing for Add_Interface_Tag_Components
1579 begin
1580 if not RTE_Available (RE_Interface_Tag) then
1581 Error_Msg
1582 ("(Ada 2005) interface types not supported by this run-time!",
1583 Sloc (N));
1584 return;
1585 end if;
1587 if Ekind (Typ) /= E_Record_Type
1588 or else (Is_Concurrent_Record_Type (Typ)
1589 and then Is_Empty_List (Abstract_Interface_List (Typ)))
1590 or else (not Is_Concurrent_Record_Type (Typ)
1591 and then No (Interfaces (Typ))
1592 and then Is_Empty_Elmt_List (Interfaces (Typ)))
1593 then
1594 return;
1595 end if;
1597 -- Find the current last tag
1599 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1600 Ext := Record_Extension_Part (Type_Definition (N));
1601 else
1602 pragma Assert (Nkind (Type_Definition (N)) = N_Record_Definition);
1603 Ext := Type_Definition (N);
1604 end if;
1606 Last_Tag := Empty;
1608 if not (Present (Component_List (Ext))) then
1609 Set_Null_Present (Ext, False);
1610 L := New_List;
1611 Set_Component_List (Ext,
1612 Make_Component_List (Loc,
1613 Component_Items => L,
1614 Null_Present => False));
1615 else
1616 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1617 L := Component_Items
1618 (Component_List
1619 (Record_Extension_Part
1620 (Type_Definition (N))));
1621 else
1622 L := Component_Items
1623 (Component_List
1624 (Type_Definition (N)));
1625 end if;
1627 -- Find the last tag component
1629 Comp := First (L);
1630 while Present (Comp) loop
1631 if Nkind (Comp) = N_Component_Declaration
1632 and then Is_Tag (Defining_Identifier (Comp))
1633 then
1634 Last_Tag := Comp;
1635 end if;
1637 Next (Comp);
1638 end loop;
1639 end if;
1641 -- At this point L references the list of components and Last_Tag
1642 -- references the current last tag (if any). Now we add the tag
1643 -- corresponding with all the interfaces that are not implemented
1644 -- by the parent.
1646 if Present (Interfaces (Typ)) then
1647 Elmt := First_Elmt (Interfaces (Typ));
1648 while Present (Elmt) loop
1649 Add_Tag (Node (Elmt));
1650 Next_Elmt (Elmt);
1651 end loop;
1652 end if;
1653 end Add_Interface_Tag_Components;
1655 -------------------------------------
1656 -- Add_Internal_Interface_Entities --
1657 -------------------------------------
1659 procedure Add_Internal_Interface_Entities (Tagged_Type : Entity_Id) is
1660 Elmt : Elmt_Id;
1661 Iface : Entity_Id;
1662 Iface_Elmt : Elmt_Id;
1663 Iface_Prim : Entity_Id;
1664 Ifaces_List : Elist_Id;
1665 New_Subp : Entity_Id := Empty;
1666 Prim : Entity_Id;
1667 Restore_Scope : Boolean := False;
1669 begin
1670 pragma Assert (Ada_Version >= Ada_2005
1671 and then Is_Record_Type (Tagged_Type)
1672 and then Is_Tagged_Type (Tagged_Type)
1673 and then Has_Interfaces (Tagged_Type)
1674 and then not Is_Interface (Tagged_Type));
1676 -- Ensure that the internal entities are added to the scope of the type
1678 if Scope (Tagged_Type) /= Current_Scope then
1679 Push_Scope (Scope (Tagged_Type));
1680 Restore_Scope := True;
1681 end if;
1683 Collect_Interfaces (Tagged_Type, Ifaces_List);
1685 Iface_Elmt := First_Elmt (Ifaces_List);
1686 while Present (Iface_Elmt) loop
1687 Iface := Node (Iface_Elmt);
1689 -- Originally we excluded here from this processing interfaces that
1690 -- are parents of Tagged_Type because their primitives are located
1691 -- in the primary dispatch table (and hence no auxiliary internal
1692 -- entities are required to handle secondary dispatch tables in such
1693 -- case). However, these auxiliary entities are also required to
1694 -- handle derivations of interfaces in formals of generics (see
1695 -- Derive_Subprograms).
1697 Elmt := First_Elmt (Primitive_Operations (Iface));
1698 while Present (Elmt) loop
1699 Iface_Prim := Node (Elmt);
1701 if not Is_Predefined_Dispatching_Operation (Iface_Prim) then
1702 Prim :=
1703 Find_Primitive_Covering_Interface
1704 (Tagged_Type => Tagged_Type,
1705 Iface_Prim => Iface_Prim);
1707 if No (Prim) and then Serious_Errors_Detected > 0 then
1708 goto Continue;
1709 end if;
1711 pragma Assert (Present (Prim));
1713 -- Ada 2012 (AI05-0197): If the name of the covering primitive
1714 -- differs from the name of the interface primitive then it is
1715 -- a private primitive inherited from a parent type. In such
1716 -- case, given that Tagged_Type covers the interface, the
1717 -- inherited private primitive becomes visible. For such
1718 -- purpose we add a new entity that renames the inherited
1719 -- private primitive.
1721 if Chars (Prim) /= Chars (Iface_Prim) then
1722 pragma Assert (Has_Suffix (Prim, 'P'));
1723 Derive_Subprogram
1724 (New_Subp => New_Subp,
1725 Parent_Subp => Iface_Prim,
1726 Derived_Type => Tagged_Type,
1727 Parent_Type => Iface);
1728 Set_Alias (New_Subp, Prim);
1729 Set_Is_Abstract_Subprogram
1730 (New_Subp, Is_Abstract_Subprogram (Prim));
1731 end if;
1733 Derive_Subprogram
1734 (New_Subp => New_Subp,
1735 Parent_Subp => Iface_Prim,
1736 Derived_Type => Tagged_Type,
1737 Parent_Type => Iface);
1739 -- Ada 2005 (AI-251): Decorate internal entity Iface_Subp
1740 -- associated with interface types. These entities are
1741 -- only registered in the list of primitives of its
1742 -- corresponding tagged type because they are only used
1743 -- to fill the contents of the secondary dispatch tables.
1744 -- Therefore they are removed from the homonym chains.
1746 Set_Is_Hidden (New_Subp);
1747 Set_Is_Internal (New_Subp);
1748 Set_Alias (New_Subp, Prim);
1749 Set_Is_Abstract_Subprogram
1750 (New_Subp, Is_Abstract_Subprogram (Prim));
1751 Set_Interface_Alias (New_Subp, Iface_Prim);
1753 -- If the returned type is an interface then propagate it to
1754 -- the returned type. Needed by the thunk to generate the code
1755 -- which displaces "this" to reference the corresponding
1756 -- secondary dispatch table in the returned object.
1758 if Is_Interface (Etype (Iface_Prim)) then
1759 Set_Etype (New_Subp, Etype (Iface_Prim));
1760 end if;
1762 -- Internal entities associated with interface types are
1763 -- only registered in the list of primitives of the tagged
1764 -- type. They are only used to fill the contents of the
1765 -- secondary dispatch tables. Therefore they are not needed
1766 -- in the homonym chains.
1768 Remove_Homonym (New_Subp);
1770 -- Hidden entities associated with interfaces must have set
1771 -- the Has_Delay_Freeze attribute to ensure that, in case of
1772 -- locally defined tagged types (or compiling with static
1773 -- dispatch tables generation disabled) the corresponding
1774 -- entry of the secondary dispatch table is filled when
1775 -- such an entity is frozen.
1777 Set_Has_Delayed_Freeze (New_Subp);
1778 end if;
1780 <<Continue>>
1781 Next_Elmt (Elmt);
1782 end loop;
1784 Next_Elmt (Iface_Elmt);
1785 end loop;
1787 if Restore_Scope then
1788 Pop_Scope;
1789 end if;
1790 end Add_Internal_Interface_Entities;
1792 -----------------------------------
1793 -- Analyze_Component_Declaration --
1794 -----------------------------------
1796 procedure Analyze_Component_Declaration (N : Node_Id) is
1797 Id : constant Entity_Id := Defining_Identifier (N);
1798 E : constant Node_Id := Expression (N);
1799 Typ : constant Node_Id :=
1800 Subtype_Indication (Component_Definition (N));
1801 T : Entity_Id;
1802 P : Entity_Id;
1804 function Contains_POC (Constr : Node_Id) return Boolean;
1805 -- Determines whether a constraint uses the discriminant of a record
1806 -- type thus becoming a per-object constraint (POC).
1808 function Is_Known_Limited (Typ : Entity_Id) return Boolean;
1809 -- Typ is the type of the current component, check whether this type is
1810 -- a limited type. Used to validate declaration against that of
1811 -- enclosing record.
1813 ------------------
1814 -- Contains_POC --
1815 ------------------
1817 function Contains_POC (Constr : Node_Id) return Boolean is
1818 begin
1819 -- Prevent cascaded errors
1821 if Error_Posted (Constr) then
1822 return False;
1823 end if;
1825 case Nkind (Constr) is
1826 when N_Attribute_Reference =>
1827 return Attribute_Name (Constr) = Name_Access
1828 and then Prefix (Constr) = Scope (Entity (Prefix (Constr)));
1830 when N_Discriminant_Association =>
1831 return Denotes_Discriminant (Expression (Constr));
1833 when N_Identifier =>
1834 return Denotes_Discriminant (Constr);
1836 when N_Index_Or_Discriminant_Constraint =>
1837 declare
1838 IDC : Node_Id;
1840 begin
1841 IDC := First (Constraints (Constr));
1842 while Present (IDC) loop
1844 -- One per-object constraint is sufficient
1846 if Contains_POC (IDC) then
1847 return True;
1848 end if;
1850 Next (IDC);
1851 end loop;
1853 return False;
1854 end;
1856 when N_Range =>
1857 return Denotes_Discriminant (Low_Bound (Constr))
1858 or else
1859 Denotes_Discriminant (High_Bound (Constr));
1861 when N_Range_Constraint =>
1862 return Denotes_Discriminant (Range_Expression (Constr));
1864 when others =>
1865 return False;
1867 end case;
1868 end Contains_POC;
1870 ----------------------
1871 -- Is_Known_Limited --
1872 ----------------------
1874 function Is_Known_Limited (Typ : Entity_Id) return Boolean is
1875 P : constant Entity_Id := Etype (Typ);
1876 R : constant Entity_Id := Root_Type (Typ);
1878 begin
1879 if Is_Limited_Record (Typ) then
1880 return True;
1882 -- If the root type is limited (and not a limited interface)
1883 -- so is the current type
1885 elsif Is_Limited_Record (R)
1886 and then (not Is_Interface (R) or else not Is_Limited_Interface (R))
1887 then
1888 return True;
1890 -- Else the type may have a limited interface progenitor, but a
1891 -- limited record parent.
1893 elsif R /= P and then Is_Limited_Record (P) then
1894 return True;
1896 else
1897 return False;
1898 end if;
1899 end Is_Known_Limited;
1901 -- Start of processing for Analyze_Component_Declaration
1903 begin
1904 Generate_Definition (Id);
1905 Enter_Name (Id);
1907 if Present (Typ) then
1908 T := Find_Type_Of_Object
1909 (Subtype_Indication (Component_Definition (N)), N);
1911 if not Nkind_In (Typ, N_Identifier, N_Expanded_Name) then
1912 Check_SPARK_05_Restriction ("subtype mark required", Typ);
1913 end if;
1915 -- Ada 2005 (AI-230): Access Definition case
1917 else
1918 pragma Assert (Present
1919 (Access_Definition (Component_Definition (N))));
1921 T := Access_Definition
1922 (Related_Nod => N,
1923 N => Access_Definition (Component_Definition (N)));
1924 Set_Is_Local_Anonymous_Access (T);
1926 -- Ada 2005 (AI-254)
1928 if Present (Access_To_Subprogram_Definition
1929 (Access_Definition (Component_Definition (N))))
1930 and then Protected_Present (Access_To_Subprogram_Definition
1931 (Access_Definition
1932 (Component_Definition (N))))
1933 then
1934 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1935 end if;
1936 end if;
1938 -- If the subtype is a constrained subtype of the enclosing record,
1939 -- (which must have a partial view) the back-end does not properly
1940 -- handle the recursion. Rewrite the component declaration with an
1941 -- explicit subtype indication, which is acceptable to Gigi. We can copy
1942 -- the tree directly because side effects have already been removed from
1943 -- discriminant constraints.
1945 if Ekind (T) = E_Access_Subtype
1946 and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
1947 and then Comes_From_Source (T)
1948 and then Nkind (Parent (T)) = N_Subtype_Declaration
1949 and then Etype (Directly_Designated_Type (T)) = Current_Scope
1950 then
1951 Rewrite
1952 (Subtype_Indication (Component_Definition (N)),
1953 New_Copy_Tree (Subtype_Indication (Parent (T))));
1954 T := Find_Type_Of_Object
1955 (Subtype_Indication (Component_Definition (N)), N);
1956 end if;
1958 -- If the component declaration includes a default expression, then we
1959 -- check that the component is not of a limited type (RM 3.7(5)),
1960 -- and do the special preanalysis of the expression (see section on
1961 -- "Handling of Default and Per-Object Expressions" in the spec of
1962 -- package Sem).
1964 if Present (E) then
1965 Check_SPARK_05_Restriction ("default expression is not allowed", E);
1966 Preanalyze_Default_Expression (E, T);
1967 Check_Initialization (T, E);
1969 if Ada_Version >= Ada_2005
1970 and then Ekind (T) = E_Anonymous_Access_Type
1971 and then Etype (E) /= Any_Type
1972 then
1973 -- Check RM 3.9.2(9): "if the expected type for an expression is
1974 -- an anonymous access-to-specific tagged type, then the object
1975 -- designated by the expression shall not be dynamically tagged
1976 -- unless it is a controlling operand in a call on a dispatching
1977 -- operation"
1979 if Is_Tagged_Type (Directly_Designated_Type (T))
1980 and then
1981 Ekind (Directly_Designated_Type (T)) /= E_Class_Wide_Type
1982 and then
1983 Ekind (Directly_Designated_Type (Etype (E))) =
1984 E_Class_Wide_Type
1985 then
1986 Error_Msg_N
1987 ("access to specific tagged type required (RM 3.9.2(9))", E);
1988 end if;
1990 -- (Ada 2005: AI-230): Accessibility check for anonymous
1991 -- components
1993 if Type_Access_Level (Etype (E)) >
1994 Deepest_Type_Access_Level (T)
1995 then
1996 Error_Msg_N
1997 ("expression has deeper access level than component " &
1998 "(RM 3.10.2 (12.2))", E);
1999 end if;
2001 -- The initialization expression is a reference to an access
2002 -- discriminant. The type of the discriminant is always deeper
2003 -- than any access type.
2005 if Ekind (Etype (E)) = E_Anonymous_Access_Type
2006 and then Is_Entity_Name (E)
2007 and then Ekind (Entity (E)) = E_In_Parameter
2008 and then Present (Discriminal_Link (Entity (E)))
2009 then
2010 Error_Msg_N
2011 ("discriminant has deeper accessibility level than target",
2013 end if;
2014 end if;
2015 end if;
2017 -- The parent type may be a private view with unknown discriminants,
2018 -- and thus unconstrained. Regular components must be constrained.
2020 if Is_Indefinite_Subtype (T) and then Chars (Id) /= Name_uParent then
2021 if Is_Class_Wide_Type (T) then
2022 Error_Msg_N
2023 ("class-wide subtype with unknown discriminants" &
2024 " in component declaration",
2025 Subtype_Indication (Component_Definition (N)));
2026 else
2027 Error_Msg_N
2028 ("unconstrained subtype in component declaration",
2029 Subtype_Indication (Component_Definition (N)));
2030 end if;
2032 -- Components cannot be abstract, except for the special case of
2033 -- the _Parent field (case of extending an abstract tagged type)
2035 elsif Is_Abstract_Type (T) and then Chars (Id) /= Name_uParent then
2036 Error_Msg_N ("type of a component cannot be abstract", N);
2037 end if;
2039 Set_Etype (Id, T);
2040 Set_Is_Aliased (Id, Aliased_Present (Component_Definition (N)));
2042 -- The component declaration may have a per-object constraint, set
2043 -- the appropriate flag in the defining identifier of the subtype.
2045 if Present (Subtype_Indication (Component_Definition (N))) then
2046 declare
2047 Sindic : constant Node_Id :=
2048 Subtype_Indication (Component_Definition (N));
2049 begin
2050 if Nkind (Sindic) = N_Subtype_Indication
2051 and then Present (Constraint (Sindic))
2052 and then Contains_POC (Constraint (Sindic))
2053 then
2054 Set_Has_Per_Object_Constraint (Id);
2055 end if;
2056 end;
2057 end if;
2059 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
2060 -- out some static checks.
2062 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (T) then
2063 Null_Exclusion_Static_Checks (N);
2064 end if;
2066 -- If this component is private (or depends on a private type), flag the
2067 -- record type to indicate that some operations are not available.
2069 P := Private_Component (T);
2071 if Present (P) then
2073 -- Check for circular definitions
2075 if P = Any_Type then
2076 Set_Etype (Id, Any_Type);
2078 -- There is a gap in the visibility of operations only if the
2079 -- component type is not defined in the scope of the record type.
2081 elsif Scope (P) = Scope (Current_Scope) then
2082 null;
2084 elsif Is_Limited_Type (P) then
2085 Set_Is_Limited_Composite (Current_Scope);
2087 else
2088 Set_Is_Private_Composite (Current_Scope);
2089 end if;
2090 end if;
2092 if P /= Any_Type
2093 and then Is_Limited_Type (T)
2094 and then Chars (Id) /= Name_uParent
2095 and then Is_Tagged_Type (Current_Scope)
2096 then
2097 if Is_Derived_Type (Current_Scope)
2098 and then not Is_Known_Limited (Current_Scope)
2099 then
2100 Error_Msg_N
2101 ("extension of nonlimited type cannot have limited components",
2104 if Is_Interface (Root_Type (Current_Scope)) then
2105 Error_Msg_N
2106 ("\limitedness is not inherited from limited interface", N);
2107 Error_Msg_N ("\add LIMITED to type indication", N);
2108 end if;
2110 Explain_Limited_Type (T, N);
2111 Set_Etype (Id, Any_Type);
2112 Set_Is_Limited_Composite (Current_Scope, False);
2114 elsif not Is_Derived_Type (Current_Scope)
2115 and then not Is_Limited_Record (Current_Scope)
2116 and then not Is_Concurrent_Type (Current_Scope)
2117 then
2118 Error_Msg_N
2119 ("nonlimited tagged type cannot have limited components", N);
2120 Explain_Limited_Type (T, N);
2121 Set_Etype (Id, Any_Type);
2122 Set_Is_Limited_Composite (Current_Scope, False);
2123 end if;
2124 end if;
2126 Set_Original_Record_Component (Id, Id);
2128 if Has_Aspects (N) then
2129 Analyze_Aspect_Specifications (N, Id);
2130 end if;
2132 Analyze_Dimension (N);
2133 end Analyze_Component_Declaration;
2135 --------------------------
2136 -- Analyze_Declarations --
2137 --------------------------
2139 procedure Analyze_Declarations (L : List_Id) is
2140 Decl : Node_Id;
2142 procedure Adjust_Decl;
2143 -- Adjust Decl not to include implicit label declarations, since these
2144 -- have strange Sloc values that result in elaboration check problems.
2145 -- (They have the sloc of the label as found in the source, and that
2146 -- is ahead of the current declarative part).
2148 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id);
2149 -- Determine whether Body_Decl denotes the body of a late controlled
2150 -- primitive (either Initialize, Adjust or Finalize). If this is the
2151 -- case, add a proper spec if the body lacks one. The spec is inserted
2152 -- before Body_Decl and immedately analyzed.
2154 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id);
2155 -- Spec_Id is the entity of a package that may define abstract states.
2156 -- If the states have visible refinement, remove the visibility of each
2157 -- constituent at the end of the package body declarations.
2159 -----------------
2160 -- Adjust_Decl --
2161 -----------------
2163 procedure Adjust_Decl is
2164 begin
2165 while Present (Prev (Decl))
2166 and then Nkind (Decl) = N_Implicit_Label_Declaration
2167 loop
2168 Prev (Decl);
2169 end loop;
2170 end Adjust_Decl;
2172 --------------------------------------
2173 -- Handle_Late_Controlled_Primitive --
2174 --------------------------------------
2176 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id) is
2177 Body_Spec : constant Node_Id := Specification (Body_Decl);
2178 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2179 Loc : constant Source_Ptr := Sloc (Body_Id);
2180 Params : constant List_Id :=
2181 Parameter_Specifications (Body_Spec);
2182 Spec : Node_Id;
2183 Spec_Id : Entity_Id;
2184 Typ : Node_Id;
2186 begin
2187 -- Consider only procedure bodies whose name matches one of the three
2188 -- controlled primitives.
2190 if Nkind (Body_Spec) /= N_Procedure_Specification
2191 or else not Nam_In (Chars (Body_Id), Name_Adjust,
2192 Name_Finalize,
2193 Name_Initialize)
2194 then
2195 return;
2197 -- A controlled primitive must have exactly one formal which is not
2198 -- an anonymous access type.
2200 elsif List_Length (Params) /= 1 then
2201 return;
2202 end if;
2204 Typ := Parameter_Type (First (Params));
2206 if Nkind (Typ) = N_Access_Definition then
2207 return;
2208 end if;
2210 Find_Type (Typ);
2212 -- The type of the formal must be derived from [Limited_]Controlled
2214 if not Is_Controlled (Entity (Typ)) then
2215 return;
2216 end if;
2218 -- Check whether a specification exists for this body. We do not
2219 -- analyze the spec of the body in full, because it will be analyzed
2220 -- again when the body is properly analyzed, and we cannot create
2221 -- duplicate entries in the formals chain. We look for an explicit
2222 -- specification because the body may be an overriding operation and
2223 -- an inherited spec may be present.
2225 Spec_Id := Current_Entity (Body_Id);
2227 while Present (Spec_Id) loop
2228 if Ekind_In (Spec_Id, E_Procedure, E_Generic_Procedure)
2229 and then Scope (Spec_Id) = Current_Scope
2230 and then Present (First_Formal (Spec_Id))
2231 and then No (Next_Formal (First_Formal (Spec_Id)))
2232 and then Etype (First_Formal (Spec_Id)) = Entity (Typ)
2233 and then Comes_From_Source (Spec_Id)
2234 then
2235 return;
2236 end if;
2238 Spec_Id := Homonym (Spec_Id);
2239 end loop;
2241 -- At this point the body is known to be a late controlled primitive.
2242 -- Generate a matching spec and insert it before the body. Note the
2243 -- use of Copy_Separate_Tree - we want an entirely separate semantic
2244 -- tree in this case.
2246 Spec := Copy_Separate_Tree (Body_Spec);
2248 -- Ensure that the subprogram declaration does not inherit the null
2249 -- indicator from the body as we now have a proper spec/body pair.
2251 Set_Null_Present (Spec, False);
2253 Insert_Before_And_Analyze (Body_Decl,
2254 Make_Subprogram_Declaration (Loc, Specification => Spec));
2255 end Handle_Late_Controlled_Primitive;
2257 --------------------------------
2258 -- Remove_Visible_Refinements --
2259 --------------------------------
2261 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id) is
2262 State_Elmt : Elmt_Id;
2263 begin
2264 if Present (Abstract_States (Spec_Id)) then
2265 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2266 while Present (State_Elmt) loop
2267 Set_Has_Visible_Refinement (Node (State_Elmt), False);
2268 Next_Elmt (State_Elmt);
2269 end loop;
2270 end if;
2271 end Remove_Visible_Refinements;
2273 -- Local variables
2275 Context : Node_Id;
2276 Freeze_From : Entity_Id := Empty;
2277 Next_Decl : Node_Id;
2278 Spec_Id : Entity_Id;
2280 Body_Seen : Boolean := False;
2281 -- Flag set when the first body [stub] is encountered
2283 In_Package_Body : Boolean := False;
2284 -- Flag set when the current declaration list belongs to a package body
2286 -- Start of processing for Analyze_Declarations
2288 begin
2289 if Restriction_Check_Required (SPARK_05) then
2290 Check_Later_Vs_Basic_Declarations (L, During_Parsing => False);
2291 end if;
2293 Decl := First (L);
2294 while Present (Decl) loop
2296 -- Package spec cannot contain a package declaration in SPARK
2298 if Nkind (Decl) = N_Package_Declaration
2299 and then Nkind (Parent (L)) = N_Package_Specification
2300 then
2301 Check_SPARK_05_Restriction
2302 ("package specification cannot contain a package declaration",
2303 Decl);
2304 end if;
2306 -- Complete analysis of declaration
2308 Analyze (Decl);
2309 Next_Decl := Next (Decl);
2311 if No (Freeze_From) then
2312 Freeze_From := First_Entity (Current_Scope);
2313 end if;
2315 -- At the end of a declarative part, freeze remaining entities
2316 -- declared in it. The end of the visible declarations of package
2317 -- specification is not the end of a declarative part if private
2318 -- declarations are present. The end of a package declaration is a
2319 -- freezing point only if it a library package. A task definition or
2320 -- protected type definition is not a freeze point either. Finally,
2321 -- we do not freeze entities in generic scopes, because there is no
2322 -- code generated for them and freeze nodes will be generated for
2323 -- the instance.
2325 -- The end of a package instantiation is not a freeze point, but
2326 -- for now we make it one, because the generic body is inserted
2327 -- (currently) immediately after. Generic instantiations will not
2328 -- be a freeze point once delayed freezing of bodies is implemented.
2329 -- (This is needed in any case for early instantiations ???).
2331 if No (Next_Decl) then
2332 if Nkind_In (Parent (L), N_Component_List,
2333 N_Task_Definition,
2334 N_Protected_Definition)
2335 then
2336 null;
2338 elsif Nkind (Parent (L)) /= N_Package_Specification then
2339 if Nkind (Parent (L)) = N_Package_Body then
2340 Freeze_From := First_Entity (Current_Scope);
2341 end if;
2343 -- There may have been several freezing points previously,
2344 -- for example object declarations or subprogram bodies, but
2345 -- at the end of a declarative part we check freezing from
2346 -- the beginning, even though entities may already be frozen,
2347 -- in order to perform visibility checks on delayed aspects.
2349 Adjust_Decl;
2350 Freeze_All (First_Entity (Current_Scope), Decl);
2351 Freeze_From := Last_Entity (Current_Scope);
2353 elsif Scope (Current_Scope) /= Standard_Standard
2354 and then not Is_Child_Unit (Current_Scope)
2355 and then No (Generic_Parent (Parent (L)))
2356 then
2357 null;
2359 elsif L /= Visible_Declarations (Parent (L))
2360 or else No (Private_Declarations (Parent (L)))
2361 or else Is_Empty_List (Private_Declarations (Parent (L)))
2362 then
2363 Adjust_Decl;
2364 Freeze_All (First_Entity (Current_Scope), Decl);
2365 Freeze_From := Last_Entity (Current_Scope);
2366 end if;
2368 -- If next node is a body then freeze all types before the body.
2369 -- An exception occurs for some expander-generated bodies. If these
2370 -- are generated at places where in general language rules would not
2371 -- allow a freeze point, then we assume that the expander has
2372 -- explicitly checked that all required types are properly frozen,
2373 -- and we do not cause general freezing here. This special circuit
2374 -- is used when the encountered body is marked as having already
2375 -- been analyzed.
2377 -- In all other cases (bodies that come from source, and expander
2378 -- generated bodies that have not been analyzed yet), freeze all
2379 -- types now. Note that in the latter case, the expander must take
2380 -- care to attach the bodies at a proper place in the tree so as to
2381 -- not cause unwanted freezing at that point.
2383 elsif not Analyzed (Next_Decl) and then Is_Body (Next_Decl) then
2385 -- When a controlled type is frozen, the expander generates stream
2386 -- and controlled type support routines. If the freeze is caused
2387 -- by the stand alone body of Initialize, Adjust and Finalize, the
2388 -- expander will end up using the wrong version of these routines
2389 -- as the body has not been processed yet. To remedy this, detect
2390 -- a late controlled primitive and create a proper spec for it.
2391 -- This ensures that the primitive will override its inherited
2392 -- counterpart before the freeze takes place.
2394 -- If the declaration we just processed is a body, do not attempt
2395 -- to examine Next_Decl as the late primitive idiom can only apply
2396 -- to the first encountered body.
2398 -- The spec of the late primitive is not generated in ASIS mode to
2399 -- ensure a consistent list of primitives that indicates the true
2400 -- semantic structure of the program (which is not relevant when
2401 -- generating executable code.
2403 -- ??? a cleaner approach may be possible and/or this solution
2404 -- could be extended to general-purpose late primitives, TBD.
2406 if not ASIS_Mode and then not Body_Seen and then not Is_Body (Decl)
2407 then
2408 Body_Seen := True;
2410 if Nkind (Next_Decl) = N_Subprogram_Body then
2411 Handle_Late_Controlled_Primitive (Next_Decl);
2412 end if;
2413 end if;
2415 Adjust_Decl;
2416 Freeze_All (Freeze_From, Decl);
2417 Freeze_From := Last_Entity (Current_Scope);
2418 end if;
2420 Decl := Next_Decl;
2421 end loop;
2423 -- Analyze the contracts of packages and their bodies
2425 if Present (L) then
2426 Context := Parent (L);
2428 if Nkind (Context) = N_Package_Specification then
2430 -- When a package has private declarations, its contract must be
2431 -- analyzed at the end of the said declarations. This way both the
2432 -- analysis and freeze actions are properly synchronized in case
2433 -- of private type use within the contract.
2435 if L = Private_Declarations (Context) then
2436 Analyze_Package_Contract (Defining_Entity (Context));
2438 -- Build the bodies of the default initial condition procedures
2439 -- for all types subject to pragma Default_Initial_Condition.
2440 -- From a purely Ada stand point, this is a freezing activity,
2441 -- however freezing is not available under GNATprove_Mode. To
2442 -- accomodate both scenarios, the bodies are build at the end
2443 -- of private declaration analysis.
2445 Build_Default_Init_Cond_Procedure_Bodies (L);
2447 -- Otherwise the contract is analyzed at the end of the visible
2448 -- declarations.
2450 elsif L = Visible_Declarations (Context)
2451 and then No (Private_Declarations (Context))
2452 then
2453 Analyze_Package_Contract (Defining_Entity (Context));
2454 end if;
2456 elsif Nkind (Context) = N_Package_Body then
2457 In_Package_Body := True;
2458 Spec_Id := Corresponding_Spec (Context);
2460 Analyze_Package_Body_Contract (Defining_Entity (Context));
2461 end if;
2462 end if;
2464 -- Analyze the contracts of subprogram declarations, subprogram bodies
2465 -- and variables now due to the delayed visibility requirements of their
2466 -- aspects.
2468 Decl := First (L);
2469 while Present (Decl) loop
2470 if Nkind (Decl) = N_Object_Declaration then
2471 Analyze_Object_Contract (Defining_Entity (Decl));
2473 elsif Nkind_In (Decl, N_Abstract_Subprogram_Declaration,
2474 N_Generic_Subprogram_Declaration,
2475 N_Subprogram_Declaration)
2476 then
2477 Analyze_Subprogram_Contract (Defining_Entity (Decl));
2479 elsif Nkind (Decl) = N_Subprogram_Body then
2480 Analyze_Subprogram_Body_Contract (Defining_Entity (Decl));
2482 elsif Nkind (Decl) = N_Subprogram_Body_Stub then
2483 Analyze_Subprogram_Body_Stub_Contract (Defining_Entity (Decl));
2484 end if;
2486 Next (Decl);
2487 end loop;
2489 -- State refinements are visible upto the end the of the package body
2490 -- declarations. Hide the refinements from visibility to restore the
2491 -- original state conditions.
2493 if In_Package_Body then
2494 Remove_Visible_Refinements (Spec_Id);
2495 end if;
2496 end Analyze_Declarations;
2498 -----------------------------------
2499 -- Analyze_Full_Type_Declaration --
2500 -----------------------------------
2502 procedure Analyze_Full_Type_Declaration (N : Node_Id) is
2503 Def : constant Node_Id := Type_Definition (N);
2504 Def_Id : constant Entity_Id := Defining_Identifier (N);
2505 T : Entity_Id;
2506 Prev : Entity_Id;
2508 Is_Remote : constant Boolean :=
2509 (Is_Remote_Types (Current_Scope)
2510 or else Is_Remote_Call_Interface (Current_Scope))
2511 and then not (In_Private_Part (Current_Scope)
2512 or else In_Package_Body (Current_Scope));
2514 procedure Check_Ops_From_Incomplete_Type;
2515 -- If there is a tagged incomplete partial view of the type, traverse
2516 -- the primitives of the incomplete view and change the type of any
2517 -- controlling formals and result to indicate the full view. The
2518 -- primitives will be added to the full type's primitive operations
2519 -- list later in Sem_Disp.Check_Operation_From_Incomplete_Type (which
2520 -- is called from Process_Incomplete_Dependents).
2522 ------------------------------------
2523 -- Check_Ops_From_Incomplete_Type --
2524 ------------------------------------
2526 procedure Check_Ops_From_Incomplete_Type is
2527 Elmt : Elmt_Id;
2528 Formal : Entity_Id;
2529 Op : Entity_Id;
2531 begin
2532 if Prev /= T
2533 and then Ekind (Prev) = E_Incomplete_Type
2534 and then Is_Tagged_Type (Prev)
2535 and then Is_Tagged_Type (T)
2536 then
2537 Elmt := First_Elmt (Primitive_Operations (Prev));
2538 while Present (Elmt) loop
2539 Op := Node (Elmt);
2541 Formal := First_Formal (Op);
2542 while Present (Formal) loop
2543 if Etype (Formal) = Prev then
2544 Set_Etype (Formal, T);
2545 end if;
2547 Next_Formal (Formal);
2548 end loop;
2550 if Etype (Op) = Prev then
2551 Set_Etype (Op, T);
2552 end if;
2554 Next_Elmt (Elmt);
2555 end loop;
2556 end if;
2557 end Check_Ops_From_Incomplete_Type;
2559 -- Start of processing for Analyze_Full_Type_Declaration
2561 begin
2562 Prev := Find_Type_Name (N);
2564 -- The type declaration may be subject to pragma Ghost with policy
2565 -- Ignore. Set the mode now to ensure that any nodes generated during
2566 -- analysis and expansion are properly flagged as ignored Ghost.
2568 Set_Ghost_Mode (N, Prev);
2570 -- The full view, if present, now points to the current type. If there
2571 -- is an incomplete partial view, set a link to it, to simplify the
2572 -- retrieval of primitive operations of the type.
2574 -- Ada 2005 (AI-50217): If the type was previously decorated when
2575 -- imported through a LIMITED WITH clause, it appears as incomplete
2576 -- but has no full view.
2578 if Ekind (Prev) = E_Incomplete_Type
2579 and then Present (Full_View (Prev))
2580 then
2581 T := Full_View (Prev);
2582 Set_Incomplete_View (N, Parent (Prev));
2583 else
2584 T := Prev;
2585 end if;
2587 Set_Is_Pure (T, Is_Pure (Current_Scope));
2589 -- We set the flag Is_First_Subtype here. It is needed to set the
2590 -- corresponding flag for the Implicit class-wide-type created
2591 -- during tagged types processing.
2593 Set_Is_First_Subtype (T, True);
2595 -- Only composite types other than array types are allowed to have
2596 -- discriminants.
2598 case Nkind (Def) is
2600 -- For derived types, the rule will be checked once we've figured
2601 -- out the parent type.
2603 when N_Derived_Type_Definition =>
2604 null;
2606 -- For record types, discriminants are allowed, unless we are in
2607 -- SPARK.
2609 when N_Record_Definition =>
2610 if Present (Discriminant_Specifications (N)) then
2611 Check_SPARK_05_Restriction
2612 ("discriminant type is not allowed",
2613 Defining_Identifier
2614 (First (Discriminant_Specifications (N))));
2615 end if;
2617 when others =>
2618 if Present (Discriminant_Specifications (N)) then
2619 Error_Msg_N
2620 ("elementary or array type cannot have discriminants",
2621 Defining_Identifier
2622 (First (Discriminant_Specifications (N))));
2623 end if;
2624 end case;
2626 -- Elaborate the type definition according to kind, and generate
2627 -- subsidiary (implicit) subtypes where needed. We skip this if it was
2628 -- already done (this happens during the reanalysis that follows a call
2629 -- to the high level optimizer).
2631 if not Analyzed (T) then
2632 Set_Analyzed (T);
2634 case Nkind (Def) is
2635 when N_Access_To_Subprogram_Definition =>
2636 Access_Subprogram_Declaration (T, Def);
2638 -- If this is a remote access to subprogram, we must create the
2639 -- equivalent fat pointer type, and related subprograms.
2641 if Is_Remote then
2642 Process_Remote_AST_Declaration (N);
2643 end if;
2645 -- Validate categorization rule against access type declaration
2646 -- usually a violation in Pure unit, Shared_Passive unit.
2648 Validate_Access_Type_Declaration (T, N);
2650 when N_Access_To_Object_Definition =>
2651 Access_Type_Declaration (T, Def);
2653 -- Validate categorization rule against access type declaration
2654 -- usually a violation in Pure unit, Shared_Passive unit.
2656 Validate_Access_Type_Declaration (T, N);
2658 -- If we are in a Remote_Call_Interface package and define a
2659 -- RACW, then calling stubs and specific stream attributes
2660 -- must be added.
2662 if Is_Remote
2663 and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
2664 then
2665 Add_RACW_Features (Def_Id);
2666 end if;
2668 when N_Array_Type_Definition =>
2669 Array_Type_Declaration (T, Def);
2671 when N_Derived_Type_Definition =>
2672 Derived_Type_Declaration (T, N, T /= Def_Id);
2674 when N_Enumeration_Type_Definition =>
2675 Enumeration_Type_Declaration (T, Def);
2677 when N_Floating_Point_Definition =>
2678 Floating_Point_Type_Declaration (T, Def);
2680 when N_Decimal_Fixed_Point_Definition =>
2681 Decimal_Fixed_Point_Type_Declaration (T, Def);
2683 when N_Ordinary_Fixed_Point_Definition =>
2684 Ordinary_Fixed_Point_Type_Declaration (T, Def);
2686 when N_Signed_Integer_Type_Definition =>
2687 Signed_Integer_Type_Declaration (T, Def);
2689 when N_Modular_Type_Definition =>
2690 Modular_Type_Declaration (T, Def);
2692 when N_Record_Definition =>
2693 Record_Type_Declaration (T, N, Prev);
2695 -- If declaration has a parse error, nothing to elaborate.
2697 when N_Error =>
2698 null;
2700 when others =>
2701 raise Program_Error;
2703 end case;
2704 end if;
2706 if Etype (T) = Any_Type then
2707 return;
2708 end if;
2710 -- Controlled type is not allowed in SPARK
2712 if Is_Visibly_Controlled (T) then
2713 Check_SPARK_05_Restriction ("controlled type is not allowed", N);
2714 end if;
2716 -- A type declared within a Ghost region is automatically Ghost
2717 -- (SPARK RM 6.9(2)).
2719 if Comes_From_Source (T) and then Ghost_Mode > None then
2720 Set_Is_Ghost_Entity (T);
2721 end if;
2723 -- Some common processing for all types
2725 Set_Depends_On_Private (T, Has_Private_Component (T));
2726 Check_Ops_From_Incomplete_Type;
2728 -- Both the declared entity, and its anonymous base type if one was
2729 -- created, need freeze nodes allocated.
2731 declare
2732 B : constant Entity_Id := Base_Type (T);
2734 begin
2735 -- In the case where the base type differs from the first subtype, we
2736 -- pre-allocate a freeze node, and set the proper link to the first
2737 -- subtype. Freeze_Entity will use this preallocated freeze node when
2738 -- it freezes the entity.
2740 -- This does not apply if the base type is a generic type, whose
2741 -- declaration is independent of the current derived definition.
2743 if B /= T and then not Is_Generic_Type (B) then
2744 Ensure_Freeze_Node (B);
2745 Set_First_Subtype_Link (Freeze_Node (B), T);
2746 end if;
2748 -- A type that is imported through a limited_with clause cannot
2749 -- generate any code, and thus need not be frozen. However, an access
2750 -- type with an imported designated type needs a finalization list,
2751 -- which may be referenced in some other package that has non-limited
2752 -- visibility on the designated type. Thus we must create the
2753 -- finalization list at the point the access type is frozen, to
2754 -- prevent unsatisfied references at link time.
2756 if not From_Limited_With (T) or else Is_Access_Type (T) then
2757 Set_Has_Delayed_Freeze (T);
2758 end if;
2759 end;
2761 -- Case where T is the full declaration of some private type which has
2762 -- been swapped in Defining_Identifier (N).
2764 if T /= Def_Id and then Is_Private_Type (Def_Id) then
2765 Process_Full_View (N, T, Def_Id);
2767 -- Record the reference. The form of this is a little strange, since
2768 -- the full declaration has been swapped in. So the first parameter
2769 -- here represents the entity to which a reference is made which is
2770 -- the "real" entity, i.e. the one swapped in, and the second
2771 -- parameter provides the reference location.
2773 -- Also, we want to kill Has_Pragma_Unreferenced temporarily here
2774 -- since we don't want a complaint about the full type being an
2775 -- unwanted reference to the private type
2777 declare
2778 B : constant Boolean := Has_Pragma_Unreferenced (T);
2779 begin
2780 Set_Has_Pragma_Unreferenced (T, False);
2781 Generate_Reference (T, T, 'c');
2782 Set_Has_Pragma_Unreferenced (T, B);
2783 end;
2785 Set_Completion_Referenced (Def_Id);
2787 -- For completion of incomplete type, process incomplete dependents
2788 -- and always mark the full type as referenced (it is the incomplete
2789 -- type that we get for any real reference).
2791 elsif Ekind (Prev) = E_Incomplete_Type then
2792 Process_Incomplete_Dependents (N, T, Prev);
2793 Generate_Reference (Prev, Def_Id, 'c');
2794 Set_Completion_Referenced (Def_Id);
2796 -- If not private type or incomplete type completion, this is a real
2797 -- definition of a new entity, so record it.
2799 else
2800 Generate_Definition (Def_Id);
2801 end if;
2803 -- Propagate any pending access types whose finalization masters need to
2804 -- be fully initialized from the partial to the full view. Guard against
2805 -- an illegal full view that remains unanalyzed.
2807 if Is_Type (Def_Id) and then Is_Incomplete_Or_Private_Type (Prev) then
2808 Set_Pending_Access_Types (Def_Id, Pending_Access_Types (Prev));
2809 end if;
2811 if Chars (Scope (Def_Id)) = Name_System
2812 and then Chars (Def_Id) = Name_Address
2813 and then Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (N)))
2814 then
2815 Set_Is_Descendent_Of_Address (Def_Id);
2816 Set_Is_Descendent_Of_Address (Base_Type (Def_Id));
2817 Set_Is_Descendent_Of_Address (Prev);
2818 end if;
2820 Set_Optimize_Alignment_Flags (Def_Id);
2821 Check_Eliminated (Def_Id);
2823 -- If the declaration is a completion and aspects are present, apply
2824 -- them to the entity for the type which is currently the partial
2825 -- view, but which is the one that will be frozen.
2827 if Has_Aspects (N) then
2829 -- In most cases the partial view is a private type, and both views
2830 -- appear in different declarative parts. In the unusual case where
2831 -- the partial view is incomplete, perform the analysis on the
2832 -- full view, to prevent freezing anomalies with the corresponding
2833 -- class-wide type, which otherwise might be frozen before the
2834 -- dispatch table is built.
2836 if Prev /= Def_Id
2837 and then Ekind (Prev) /= E_Incomplete_Type
2838 then
2839 Analyze_Aspect_Specifications (N, Prev);
2841 -- Normal case
2843 else
2844 Analyze_Aspect_Specifications (N, Def_Id);
2845 end if;
2846 end if;
2847 end Analyze_Full_Type_Declaration;
2849 ----------------------------------
2850 -- Analyze_Incomplete_Type_Decl --
2851 ----------------------------------
2853 procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
2854 F : constant Boolean := Is_Pure (Current_Scope);
2855 T : Entity_Id;
2857 begin
2858 Check_SPARK_05_Restriction ("incomplete type is not allowed", N);
2860 Generate_Definition (Defining_Identifier (N));
2862 -- Process an incomplete declaration. The identifier must not have been
2863 -- declared already in the scope. However, an incomplete declaration may
2864 -- appear in the private part of a package, for a private type that has
2865 -- already been declared.
2867 -- In this case, the discriminants (if any) must match
2869 T := Find_Type_Name (N);
2871 Set_Ekind (T, E_Incomplete_Type);
2872 Init_Size_Align (T);
2873 Set_Is_First_Subtype (T, True);
2874 Set_Etype (T, T);
2876 -- An incomplete type declared within a Ghost region is automatically
2877 -- Ghost (SPARK RM 6.9(2)).
2879 if Ghost_Mode > None then
2880 Set_Is_Ghost_Entity (T);
2881 end if;
2883 -- Ada 2005 (AI-326): Minimum decoration to give support to tagged
2884 -- incomplete types.
2886 if Tagged_Present (N) then
2887 Set_Is_Tagged_Type (T, True);
2888 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
2889 Make_Class_Wide_Type (T);
2890 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2891 end if;
2893 Push_Scope (T);
2895 Set_Stored_Constraint (T, No_Elist);
2897 if Present (Discriminant_Specifications (N)) then
2898 Process_Discriminants (N);
2899 end if;
2901 End_Scope;
2903 -- If the type has discriminants, non-trivial subtypes may be
2904 -- declared before the full view of the type. The full views of those
2905 -- subtypes will be built after the full view of the type.
2907 Set_Private_Dependents (T, New_Elmt_List);
2908 Set_Is_Pure (T, F);
2909 end Analyze_Incomplete_Type_Decl;
2911 -----------------------------------
2912 -- Analyze_Interface_Declaration --
2913 -----------------------------------
2915 procedure Analyze_Interface_Declaration (T : Entity_Id; Def : Node_Id) is
2916 CW : constant Entity_Id := Class_Wide_Type (T);
2918 begin
2919 Set_Is_Tagged_Type (T);
2920 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
2922 Set_Is_Limited_Record (T, Limited_Present (Def)
2923 or else Task_Present (Def)
2924 or else Protected_Present (Def)
2925 or else Synchronized_Present (Def));
2927 -- Type is abstract if full declaration carries keyword, or if previous
2928 -- partial view did.
2930 Set_Is_Abstract_Type (T);
2931 Set_Is_Interface (T);
2933 -- Type is a limited interface if it includes the keyword limited, task,
2934 -- protected, or synchronized.
2936 Set_Is_Limited_Interface
2937 (T, Limited_Present (Def)
2938 or else Protected_Present (Def)
2939 or else Synchronized_Present (Def)
2940 or else Task_Present (Def));
2942 Set_Interfaces (T, New_Elmt_List);
2943 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2945 -- Complete the decoration of the class-wide entity if it was already
2946 -- built (i.e. during the creation of the limited view)
2948 if Present (CW) then
2949 Set_Is_Interface (CW);
2950 Set_Is_Limited_Interface (CW, Is_Limited_Interface (T));
2951 end if;
2953 -- Check runtime support for synchronized interfaces
2955 if VM_Target = No_VM
2956 and then (Is_Task_Interface (T)
2957 or else Is_Protected_Interface (T)
2958 or else Is_Synchronized_Interface (T))
2959 and then not RTE_Available (RE_Select_Specific_Data)
2960 then
2961 Error_Msg_CRT ("synchronized interfaces", T);
2962 end if;
2963 end Analyze_Interface_Declaration;
2965 -----------------------------
2966 -- Analyze_Itype_Reference --
2967 -----------------------------
2969 -- Nothing to do. This node is placed in the tree only for the benefit of
2970 -- back end processing, and has no effect on the semantic processing.
2972 procedure Analyze_Itype_Reference (N : Node_Id) is
2973 begin
2974 pragma Assert (Is_Itype (Itype (N)));
2975 null;
2976 end Analyze_Itype_Reference;
2978 --------------------------------
2979 -- Analyze_Number_Declaration --
2980 --------------------------------
2982 procedure Analyze_Number_Declaration (N : Node_Id) is
2983 Id : constant Entity_Id := Defining_Identifier (N);
2984 E : constant Node_Id := Expression (N);
2985 T : Entity_Id;
2986 Index : Interp_Index;
2987 It : Interp;
2989 begin
2990 -- The number declaration may be subject to pragma Ghost with policy
2991 -- Ignore. Set the mode now to ensure that any nodes generated during
2992 -- analysis and expansion are properly flagged as ignored Ghost.
2994 Set_Ghost_Mode (N);
2996 Generate_Definition (Id);
2997 Enter_Name (Id);
2999 -- A number declared within a Ghost region is automatically Ghost
3000 -- (SPARK RM 6.9(2)).
3002 if Ghost_Mode > None then
3003 Set_Is_Ghost_Entity (Id);
3004 end if;
3006 -- This is an optimization of a common case of an integer literal
3008 if Nkind (E) = N_Integer_Literal then
3009 Set_Is_Static_Expression (E, True);
3010 Set_Etype (E, Universal_Integer);
3012 Set_Etype (Id, Universal_Integer);
3013 Set_Ekind (Id, E_Named_Integer);
3014 Set_Is_Frozen (Id, True);
3015 return;
3016 end if;
3018 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3020 -- Process expression, replacing error by integer zero, to avoid
3021 -- cascaded errors or aborts further along in the processing
3023 -- Replace Error by integer zero, which seems least likely to cause
3024 -- cascaded errors.
3026 if E = Error then
3027 Rewrite (E, Make_Integer_Literal (Sloc (E), Uint_0));
3028 Set_Error_Posted (E);
3029 end if;
3031 Analyze (E);
3033 -- Verify that the expression is static and numeric. If
3034 -- the expression is overloaded, we apply the preference
3035 -- rule that favors root numeric types.
3037 if not Is_Overloaded (E) then
3038 T := Etype (E);
3039 if Has_Dynamic_Predicate_Aspect (T) then
3040 Error_Msg_N
3041 ("subtype has dynamic predicate, "
3042 & "not allowed in number declaration", N);
3043 end if;
3045 else
3046 T := Any_Type;
3048 Get_First_Interp (E, Index, It);
3049 while Present (It.Typ) loop
3050 if (Is_Integer_Type (It.Typ) or else Is_Real_Type (It.Typ))
3051 and then (Scope (Base_Type (It.Typ))) = Standard_Standard
3052 then
3053 if T = Any_Type then
3054 T := It.Typ;
3056 elsif It.Typ = Universal_Real
3057 or else
3058 It.Typ = Universal_Integer
3059 then
3060 -- Choose universal interpretation over any other
3062 T := It.Typ;
3063 exit;
3064 end if;
3065 end if;
3067 Get_Next_Interp (Index, It);
3068 end loop;
3069 end if;
3071 if Is_Integer_Type (T) then
3072 Resolve (E, T);
3073 Set_Etype (Id, Universal_Integer);
3074 Set_Ekind (Id, E_Named_Integer);
3076 elsif Is_Real_Type (T) then
3078 -- Because the real value is converted to universal_real, this is a
3079 -- legal context for a universal fixed expression.
3081 if T = Universal_Fixed then
3082 declare
3083 Loc : constant Source_Ptr := Sloc (N);
3084 Conv : constant Node_Id := Make_Type_Conversion (Loc,
3085 Subtype_Mark =>
3086 New_Occurrence_Of (Universal_Real, Loc),
3087 Expression => Relocate_Node (E));
3089 begin
3090 Rewrite (E, Conv);
3091 Analyze (E);
3092 end;
3094 elsif T = Any_Fixed then
3095 Error_Msg_N ("illegal context for mixed mode operation", E);
3097 -- Expression is of the form : universal_fixed * integer. Try to
3098 -- resolve as universal_real.
3100 T := Universal_Real;
3101 Set_Etype (E, T);
3102 end if;
3104 Resolve (E, T);
3105 Set_Etype (Id, Universal_Real);
3106 Set_Ekind (Id, E_Named_Real);
3108 else
3109 Wrong_Type (E, Any_Numeric);
3110 Resolve (E, T);
3112 Set_Etype (Id, T);
3113 Set_Ekind (Id, E_Constant);
3114 Set_Never_Set_In_Source (Id, True);
3115 Set_Is_True_Constant (Id, True);
3116 return;
3117 end if;
3119 if Nkind_In (E, N_Integer_Literal, N_Real_Literal) then
3120 Set_Etype (E, Etype (Id));
3121 end if;
3123 if not Is_OK_Static_Expression (E) then
3124 Flag_Non_Static_Expr
3125 ("non-static expression used in number declaration!", E);
3126 Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
3127 Set_Etype (E, Any_Type);
3128 end if;
3129 end Analyze_Number_Declaration;
3131 -----------------------------
3132 -- Analyze_Object_Contract --
3133 -----------------------------
3135 procedure Analyze_Object_Contract (Obj_Id : Entity_Id) is
3136 Obj_Typ : constant Entity_Id := Etype (Obj_Id);
3137 AR_Val : Boolean := False;
3138 AW_Val : Boolean := False;
3139 ER_Val : Boolean := False;
3140 EW_Val : Boolean := False;
3141 Prag : Node_Id;
3142 Seen : Boolean := False;
3144 begin
3145 -- The loop parameter in an element iterator over a formal container
3146 -- is declared with an object declaration but no contracts apply.
3148 if Ekind (Obj_Id) = E_Loop_Parameter then
3149 return;
3150 end if;
3152 if Ekind (Obj_Id) = E_Constant then
3154 -- A constant cannot be effectively volatile. This check is only
3155 -- relevant with SPARK_Mode on as it is not a standard Ada legality
3156 -- rule. Do not flag internally-generated constants that map generic
3157 -- formals to actuals in instantiations (SPARK RM 7.1.3(6)).
3159 if SPARK_Mode = On
3160 and then Is_Effectively_Volatile (Obj_Id)
3161 and then No (Corresponding_Generic_Association (Parent (Obj_Id)))
3162 then
3163 Error_Msg_N ("constant cannot be volatile", Obj_Id);
3164 end if;
3166 else pragma Assert (Ekind (Obj_Id) = E_Variable);
3168 -- The following checks are only relevant when SPARK_Mode is on as
3169 -- they are not standard Ada legality rules. Internally generated
3170 -- temporaries are ignored.
3172 if SPARK_Mode = On and then Comes_From_Source (Obj_Id) then
3173 if Is_Effectively_Volatile (Obj_Id) then
3175 -- The declaration of an effectively volatile object must
3176 -- appear at the library level (SPARK RM 7.1.3(7), C.6(6)).
3178 if not Is_Library_Level_Entity (Obj_Id) then
3179 Error_Msg_N
3180 ("volatile variable & must be declared at library level",
3181 Obj_Id);
3183 -- An object of a discriminated type cannot be effectively
3184 -- volatile (SPARK RM C.6(4)).
3186 elsif Has_Discriminants (Obj_Typ) then
3187 Error_Msg_N
3188 ("discriminated object & cannot be volatile", Obj_Id);
3190 -- An object of a tagged type cannot be effectively volatile
3191 -- (SPARK RM C.6(5)).
3193 elsif Is_Tagged_Type (Obj_Typ) then
3194 Error_Msg_N ("tagged object & cannot be volatile", Obj_Id);
3195 end if;
3197 -- The object is not effectively volatile
3199 else
3200 -- A non-effectively volatile object cannot have effectively
3201 -- volatile components (SPARK RM 7.1.3(7)).
3203 if not Is_Effectively_Volatile (Obj_Id)
3204 and then Has_Volatile_Component (Obj_Typ)
3205 then
3206 Error_Msg_N
3207 ("non-volatile object & cannot have volatile components",
3208 Obj_Id);
3209 end if;
3210 end if;
3211 end if;
3213 if Is_Ghost_Entity (Obj_Id) then
3215 -- A Ghost object cannot be effectively volatile (SPARK RM 6.9(8))
3217 if Is_Effectively_Volatile (Obj_Id) then
3218 Error_Msg_N ("ghost variable & cannot be volatile", Obj_Id);
3220 -- A Ghost object cannot be imported or exported (SPARK RM 6.9(8))
3222 elsif Is_Imported (Obj_Id) then
3223 Error_Msg_N ("ghost object & cannot be imported", Obj_Id);
3225 elsif Is_Exported (Obj_Id) then
3226 Error_Msg_N ("ghost object & cannot be exported", Obj_Id);
3227 end if;
3228 end if;
3230 -- Analyze all external properties
3232 Prag := Get_Pragma (Obj_Id, Pragma_Async_Readers);
3234 if Present (Prag) then
3235 Analyze_External_Property_In_Decl_Part (Prag, AR_Val);
3236 Seen := True;
3237 end if;
3239 Prag := Get_Pragma (Obj_Id, Pragma_Async_Writers);
3241 if Present (Prag) then
3242 Analyze_External_Property_In_Decl_Part (Prag, AW_Val);
3243 Seen := True;
3244 end if;
3246 Prag := Get_Pragma (Obj_Id, Pragma_Effective_Reads);
3248 if Present (Prag) then
3249 Analyze_External_Property_In_Decl_Part (Prag, ER_Val);
3250 Seen := True;
3251 end if;
3253 Prag := Get_Pragma (Obj_Id, Pragma_Effective_Writes);
3255 if Present (Prag) then
3256 Analyze_External_Property_In_Decl_Part (Prag, EW_Val);
3257 Seen := True;
3258 end if;
3260 -- Verify the mutual interaction of the various external properties
3262 if Seen then
3263 Check_External_Properties (Obj_Id, AR_Val, AW_Val, ER_Val, EW_Val);
3264 end if;
3266 -- Check whether the lack of indicator Part_Of agrees with the
3267 -- placement of the variable with respect to the state space.
3269 Prag := Get_Pragma (Obj_Id, Pragma_Part_Of);
3271 if No (Prag) then
3272 Check_Missing_Part_Of (Obj_Id);
3273 end if;
3274 end if;
3276 -- A ghost object cannot be imported or exported (SPARK RM 6.9(8))
3278 if Is_Ghost_Entity (Obj_Id) then
3279 if Is_Exported (Obj_Id) then
3280 Error_Msg_N ("ghost object & cannot be exported", Obj_Id);
3282 elsif Is_Imported (Obj_Id) then
3283 Error_Msg_N ("ghost object & cannot be imported", Obj_Id);
3284 end if;
3285 end if;
3286 end Analyze_Object_Contract;
3288 --------------------------------
3289 -- Analyze_Object_Declaration --
3290 --------------------------------
3292 procedure Analyze_Object_Declaration (N : Node_Id) is
3293 Loc : constant Source_Ptr := Sloc (N);
3294 Id : constant Entity_Id := Defining_Identifier (N);
3295 T : Entity_Id;
3296 Act_T : Entity_Id;
3298 E : Node_Id := Expression (N);
3299 -- E is set to Expression (N) throughout this routine. When
3300 -- Expression (N) is modified, E is changed accordingly.
3302 Prev_Entity : Entity_Id := Empty;
3304 function Count_Tasks (T : Entity_Id) return Uint;
3305 -- This function is called when a non-generic library level object of a
3306 -- task type is declared. Its function is to count the static number of
3307 -- tasks declared within the type (it is only called if Has_Tasks is set
3308 -- for T). As a side effect, if an array of tasks with non-static bounds
3309 -- or a variant record type is encountered, Check_Restrictions is called
3310 -- indicating the count is unknown.
3312 -----------------
3313 -- Count_Tasks --
3314 -----------------
3316 function Count_Tasks (T : Entity_Id) return Uint is
3317 C : Entity_Id;
3318 X : Node_Id;
3319 V : Uint;
3321 begin
3322 if Is_Task_Type (T) then
3323 return Uint_1;
3325 elsif Is_Record_Type (T) then
3326 if Has_Discriminants (T) then
3327 Check_Restriction (Max_Tasks, N);
3328 return Uint_0;
3330 else
3331 V := Uint_0;
3332 C := First_Component (T);
3333 while Present (C) loop
3334 V := V + Count_Tasks (Etype (C));
3335 Next_Component (C);
3336 end loop;
3338 return V;
3339 end if;
3341 elsif Is_Array_Type (T) then
3342 X := First_Index (T);
3343 V := Count_Tasks (Component_Type (T));
3344 while Present (X) loop
3345 C := Etype (X);
3347 if not Is_OK_Static_Subtype (C) then
3348 Check_Restriction (Max_Tasks, N);
3349 return Uint_0;
3350 else
3351 V := V * (UI_Max (Uint_0,
3352 Expr_Value (Type_High_Bound (C)) -
3353 Expr_Value (Type_Low_Bound (C)) + Uint_1));
3354 end if;
3356 Next_Index (X);
3357 end loop;
3359 return V;
3361 else
3362 return Uint_0;
3363 end if;
3364 end Count_Tasks;
3366 -- Start of processing for Analyze_Object_Declaration
3368 begin
3369 -- There are three kinds of implicit types generated by an
3370 -- object declaration:
3372 -- 1. Those generated by the original Object Definition
3374 -- 2. Those generated by the Expression
3376 -- 3. Those used to constrain the Object Definition with the
3377 -- expression constraints when the definition is unconstrained.
3379 -- They must be generated in this order to avoid order of elaboration
3380 -- issues. Thus the first step (after entering the name) is to analyze
3381 -- the object definition.
3383 if Constant_Present (N) then
3384 Prev_Entity := Current_Entity_In_Scope (Id);
3386 if Present (Prev_Entity)
3387 and then
3388 -- If the homograph is an implicit subprogram, it is overridden
3389 -- by the current declaration.
3391 ((Is_Overloadable (Prev_Entity)
3392 and then Is_Inherited_Operation (Prev_Entity))
3394 -- The current object is a discriminal generated for an entry
3395 -- family index. Even though the index is a constant, in this
3396 -- particular context there is no true constant redeclaration.
3397 -- Enter_Name will handle the visibility.
3399 or else
3400 (Is_Discriminal (Id)
3401 and then Ekind (Discriminal_Link (Id)) =
3402 E_Entry_Index_Parameter)
3404 -- The current object is the renaming for a generic declared
3405 -- within the instance.
3407 or else
3408 (Ekind (Prev_Entity) = E_Package
3409 and then Nkind (Parent (Prev_Entity)) =
3410 N_Package_Renaming_Declaration
3411 and then not Comes_From_Source (Prev_Entity)
3412 and then
3413 Is_Generic_Instance (Renamed_Entity (Prev_Entity))))
3414 then
3415 Prev_Entity := Empty;
3416 end if;
3417 end if;
3419 -- The object declaration may be subject to pragma Ghost with policy
3420 -- Ignore. Set the mode now to ensure that any nodes generated during
3421 -- analysis and expansion are properly flagged as ignored Ghost.
3423 Set_Ghost_Mode (N, Prev_Entity);
3425 if Present (Prev_Entity) then
3426 Constant_Redeclaration (Id, N, T);
3428 Generate_Reference (Prev_Entity, Id, 'c');
3429 Set_Completion_Referenced (Id);
3431 if Error_Posted (N) then
3433 -- Type mismatch or illegal redeclaration, Do not analyze
3434 -- expression to avoid cascaded errors.
3436 T := Find_Type_Of_Object (Object_Definition (N), N);
3437 Set_Etype (Id, T);
3438 Set_Ekind (Id, E_Variable);
3439 goto Leave;
3440 end if;
3442 -- In the normal case, enter identifier at the start to catch premature
3443 -- usage in the initialization expression.
3445 else
3446 Generate_Definition (Id);
3447 Enter_Name (Id);
3449 Mark_Coextensions (N, Object_Definition (N));
3451 T := Find_Type_Of_Object (Object_Definition (N), N);
3453 if Nkind (Object_Definition (N)) = N_Access_Definition
3454 and then Present
3455 (Access_To_Subprogram_Definition (Object_Definition (N)))
3456 and then Protected_Present
3457 (Access_To_Subprogram_Definition (Object_Definition (N)))
3458 then
3459 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
3460 end if;
3462 if Error_Posted (Id) then
3463 Set_Etype (Id, T);
3464 Set_Ekind (Id, E_Variable);
3465 goto Leave;
3466 end if;
3467 end if;
3469 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
3470 -- out some static checks
3472 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (T) then
3474 -- In case of aggregates we must also take care of the correct
3475 -- initialization of nested aggregates bug this is done at the
3476 -- point of the analysis of the aggregate (see sem_aggr.adb).
3478 if Present (Expression (N))
3479 and then Nkind (Expression (N)) = N_Aggregate
3480 then
3481 null;
3483 else
3484 declare
3485 Save_Typ : constant Entity_Id := Etype (Id);
3486 begin
3487 Set_Etype (Id, T); -- Temp. decoration for static checks
3488 Null_Exclusion_Static_Checks (N);
3489 Set_Etype (Id, Save_Typ);
3490 end;
3491 end if;
3492 end if;
3494 -- Object is marked pure if it is in a pure scope
3496 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3498 -- If deferred constant, make sure context is appropriate. We detect
3499 -- a deferred constant as a constant declaration with no expression.
3500 -- A deferred constant can appear in a package body if its completion
3501 -- is by means of an interface pragma.
3503 if Constant_Present (N) and then No (E) then
3505 -- A deferred constant may appear in the declarative part of the
3506 -- following constructs:
3508 -- blocks
3509 -- entry bodies
3510 -- extended return statements
3511 -- package specs
3512 -- package bodies
3513 -- subprogram bodies
3514 -- task bodies
3516 -- When declared inside a package spec, a deferred constant must be
3517 -- completed by a full constant declaration or pragma Import. In all
3518 -- other cases, the only proper completion is pragma Import. Extended
3519 -- return statements are flagged as invalid contexts because they do
3520 -- not have a declarative part and so cannot accommodate the pragma.
3522 if Ekind (Current_Scope) = E_Return_Statement then
3523 Error_Msg_N
3524 ("invalid context for deferred constant declaration (RM 7.4)",
3526 Error_Msg_N
3527 ("\declaration requires an initialization expression",
3529 Set_Constant_Present (N, False);
3531 -- In Ada 83, deferred constant must be of private type
3533 elsif not Is_Private_Type (T) then
3534 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
3535 Error_Msg_N
3536 ("(Ada 83) deferred constant must be private type", N);
3537 end if;
3538 end if;
3540 -- If not a deferred constant, then the object declaration freezes
3541 -- its type, unless the object is of an anonymous type and has delayed
3542 -- aspects. In that case the type is frozen when the object itself is.
3544 else
3545 Check_Fully_Declared (T, N);
3547 if Has_Delayed_Aspects (Id)
3548 and then Is_Array_Type (T)
3549 and then Is_Itype (T)
3550 then
3551 Set_Has_Delayed_Freeze (T);
3552 else
3553 Freeze_Before (N, T);
3554 end if;
3555 end if;
3557 -- If the object was created by a constrained array definition, then
3558 -- set the link in both the anonymous base type and anonymous subtype
3559 -- that are built to represent the array type to point to the object.
3561 if Nkind (Object_Definition (Declaration_Node (Id))) =
3562 N_Constrained_Array_Definition
3563 then
3564 Set_Related_Array_Object (T, Id);
3565 Set_Related_Array_Object (Base_Type (T), Id);
3566 end if;
3568 -- Special checks for protected objects not at library level
3570 if Is_Protected_Type (T)
3571 and then not Is_Library_Level_Entity (Id)
3572 then
3573 Check_Restriction (No_Local_Protected_Objects, Id);
3575 -- Protected objects with interrupt handlers must be at library level
3577 -- Ada 2005: This test is not needed (and the corresponding clause
3578 -- in the RM is removed) because accessibility checks are sufficient
3579 -- to make handlers not at the library level illegal.
3581 -- AI05-0303: The AI is in fact a binding interpretation, and thus
3582 -- applies to the '95 version of the language as well.
3584 if Has_Interrupt_Handler (T) and then Ada_Version < Ada_95 then
3585 Error_Msg_N
3586 ("interrupt object can only be declared at library level", Id);
3587 end if;
3588 end if;
3590 -- The actual subtype of the object is the nominal subtype, unless
3591 -- the nominal one is unconstrained and obtained from the expression.
3593 Act_T := T;
3595 -- These checks should be performed before the initialization expression
3596 -- is considered, so that the Object_Definition node is still the same
3597 -- as in source code.
3599 -- In SPARK, the nominal subtype is always given by a subtype mark
3600 -- and must not be unconstrained. (The only exception to this is the
3601 -- acceptance of declarations of constants of type String.)
3603 if not Nkind_In (Object_Definition (N), N_Expanded_Name, N_Identifier)
3604 then
3605 Check_SPARK_05_Restriction
3606 ("subtype mark required", Object_Definition (N));
3608 elsif Is_Array_Type (T)
3609 and then not Is_Constrained (T)
3610 and then T /= Standard_String
3611 then
3612 Check_SPARK_05_Restriction
3613 ("subtype mark of constrained type expected",
3614 Object_Definition (N));
3615 end if;
3617 -- There are no aliased objects in SPARK
3619 if Aliased_Present (N) then
3620 Check_SPARK_05_Restriction ("aliased object is not allowed", N);
3621 end if;
3623 -- Process initialization expression if present and not in error
3625 if Present (E) and then E /= Error then
3627 -- Generate an error in case of CPP class-wide object initialization.
3628 -- Required because otherwise the expansion of the class-wide
3629 -- assignment would try to use 'size to initialize the object
3630 -- (primitive that is not available in CPP tagged types).
3632 if Is_Class_Wide_Type (Act_T)
3633 and then
3634 (Is_CPP_Class (Root_Type (Etype (Act_T)))
3635 or else
3636 (Present (Full_View (Root_Type (Etype (Act_T))))
3637 and then
3638 Is_CPP_Class (Full_View (Root_Type (Etype (Act_T))))))
3639 then
3640 Error_Msg_N
3641 ("predefined assignment not available for 'C'P'P tagged types",
3643 end if;
3645 Mark_Coextensions (N, E);
3646 Analyze (E);
3648 -- In case of errors detected in the analysis of the expression,
3649 -- decorate it with the expected type to avoid cascaded errors
3651 if No (Etype (E)) then
3652 Set_Etype (E, T);
3653 end if;
3655 -- If an initialization expression is present, then we set the
3656 -- Is_True_Constant flag. It will be reset if this is a variable
3657 -- and it is indeed modified.
3659 Set_Is_True_Constant (Id, True);
3661 -- If we are analyzing a constant declaration, set its completion
3662 -- flag after analyzing and resolving the expression.
3664 if Constant_Present (N) then
3665 Set_Has_Completion (Id);
3666 end if;
3668 -- Set type and resolve (type may be overridden later on). Note:
3669 -- Ekind (Id) must still be E_Void at this point so that incorrect
3670 -- early usage within E is properly diagnosed.
3672 Set_Etype (Id, T);
3674 -- If the expression is an aggregate we must look ahead to detect
3675 -- the possible presence of an address clause, and defer resolution
3676 -- and expansion of the aggregate to the freeze point of the entity.
3678 if Comes_From_Source (N)
3679 and then Expander_Active
3680 and then Nkind (E) = N_Aggregate
3681 and then Present (Following_Address_Clause (N))
3682 then
3683 Set_Etype (E, T);
3685 else
3686 Resolve (E, T);
3687 end if;
3689 -- No further action needed if E is a call to an inlined function
3690 -- which returns an unconstrained type and it has been expanded into
3691 -- a procedure call. In that case N has been replaced by an object
3692 -- declaration without initializing expression and it has been
3693 -- analyzed (see Expand_Inlined_Call).
3695 if Back_End_Inlining
3696 and then Expander_Active
3697 and then Nkind (E) = N_Function_Call
3698 and then Nkind (Name (E)) in N_Has_Entity
3699 and then Is_Inlined (Entity (Name (E)))
3700 and then not Is_Constrained (Etype (E))
3701 and then Analyzed (N)
3702 and then No (Expression (N))
3703 then
3704 return;
3705 end if;
3707 -- If E is null and has been replaced by an N_Raise_Constraint_Error
3708 -- node (which was marked already-analyzed), we need to set the type
3709 -- to something other than Any_Access in order to keep gigi happy.
3711 if Etype (E) = Any_Access then
3712 Set_Etype (E, T);
3713 end if;
3715 -- If the object is an access to variable, the initialization
3716 -- expression cannot be an access to constant.
3718 if Is_Access_Type (T)
3719 and then not Is_Access_Constant (T)
3720 and then Is_Access_Type (Etype (E))
3721 and then Is_Access_Constant (Etype (E))
3722 then
3723 Error_Msg_N
3724 ("access to variable cannot be initialized with an "
3725 & "access-to-constant expression", E);
3726 end if;
3728 if not Assignment_OK (N) then
3729 Check_Initialization (T, E);
3730 end if;
3732 Check_Unset_Reference (E);
3734 -- If this is a variable, then set current value. If this is a
3735 -- declared constant of a scalar type with a static expression,
3736 -- indicate that it is always valid.
3738 if not Constant_Present (N) then
3739 if Compile_Time_Known_Value (E) then
3740 Set_Current_Value (Id, E);
3741 end if;
3743 elsif Is_Scalar_Type (T) and then Is_OK_Static_Expression (E) then
3744 Set_Is_Known_Valid (Id);
3745 end if;
3747 -- Deal with setting of null flags
3749 if Is_Access_Type (T) then
3750 if Known_Non_Null (E) then
3751 Set_Is_Known_Non_Null (Id, True);
3752 elsif Known_Null (E) and then not Can_Never_Be_Null (Id) then
3753 Set_Is_Known_Null (Id, True);
3754 end if;
3755 end if;
3757 -- Check incorrect use of dynamically tagged expressions
3759 if Is_Tagged_Type (T) then
3760 Check_Dynamically_Tagged_Expression
3761 (Expr => E,
3762 Typ => T,
3763 Related_Nod => N);
3764 end if;
3766 Apply_Scalar_Range_Check (E, T);
3767 Apply_Static_Length_Check (E, T);
3769 if Nkind (Original_Node (N)) = N_Object_Declaration
3770 and then Comes_From_Source (Original_Node (N))
3772 -- Only call test if needed
3774 and then Restriction_Check_Required (SPARK_05)
3775 and then not Is_SPARK_05_Initialization_Expr (Original_Node (E))
3776 then
3777 Check_SPARK_05_Restriction
3778 ("initialization expression is not appropriate", E);
3779 end if;
3781 -- A formal parameter of a specific tagged type whose related
3782 -- subprogram is subject to pragma Extensions_Visible with value
3783 -- "False" cannot be implicitly converted to a class-wide type by
3784 -- means of an initialization expression (SPARK RM 6.1.7(3)).
3786 if Is_Class_Wide_Type (T) and then Is_EVF_Expression (E) then
3787 Error_Msg_N
3788 ("formal parameter with Extensions_Visible False cannot be "
3789 & "implicitly converted to class-wide type", E);
3790 end if;
3791 end if;
3793 -- If the No_Streams restriction is set, check that the type of the
3794 -- object is not, and does not contain, any subtype derived from
3795 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
3796 -- Has_Stream just for efficiency reasons. There is no point in
3797 -- spending time on a Has_Stream check if the restriction is not set.
3799 if Restriction_Check_Required (No_Streams) then
3800 if Has_Stream (T) then
3801 Check_Restriction (No_Streams, N);
3802 end if;
3803 end if;
3805 -- Deal with predicate check before we start to do major rewriting. It
3806 -- is OK to initialize and then check the initialized value, since the
3807 -- object goes out of scope if we get a predicate failure. Note that we
3808 -- do this in the analyzer and not the expander because the analyzer
3809 -- does some substantial rewriting in some cases.
3811 -- We need a predicate check if the type has predicates, and if either
3812 -- there is an initializing expression, or for default initialization
3813 -- when we have at least one case of an explicit default initial value
3814 -- and then this is not an internal declaration whose initialization
3815 -- comes later (as for an aggregate expansion).
3817 if not Suppress_Assignment_Checks (N)
3818 and then Present (Predicate_Function (T))
3819 and then not No_Initialization (N)
3820 and then
3821 (Present (E)
3822 or else
3823 Is_Partially_Initialized_Type (T, Include_Implicit => False))
3824 then
3825 -- If the type has a static predicate and the expression is known at
3826 -- compile time, see if the expression satisfies the predicate.
3828 if Present (E) then
3829 Check_Expression_Against_Static_Predicate (E, T);
3830 end if;
3832 Insert_After (N,
3833 Make_Predicate_Check (T, New_Occurrence_Of (Id, Loc)));
3834 end if;
3836 -- Case of unconstrained type
3838 if Is_Indefinite_Subtype (T) then
3840 -- In SPARK, a declaration of unconstrained type is allowed
3841 -- only for constants of type string.
3843 if Is_String_Type (T) and then not Constant_Present (N) then
3844 Check_SPARK_05_Restriction
3845 ("declaration of object of unconstrained type not allowed", N);
3846 end if;
3848 -- Nothing to do in deferred constant case
3850 if Constant_Present (N) and then No (E) then
3851 null;
3853 -- Case of no initialization present
3855 elsif No (E) then
3856 if No_Initialization (N) then
3857 null;
3859 elsif Is_Class_Wide_Type (T) then
3860 Error_Msg_N
3861 ("initialization required in class-wide declaration ", N);
3863 else
3864 Error_Msg_N
3865 ("unconstrained subtype not allowed (need initialization)",
3866 Object_Definition (N));
3868 if Is_Record_Type (T) and then Has_Discriminants (T) then
3869 Error_Msg_N
3870 ("\provide initial value or explicit discriminant values",
3871 Object_Definition (N));
3873 Error_Msg_NE
3874 ("\or give default discriminant values for type&",
3875 Object_Definition (N), T);
3877 elsif Is_Array_Type (T) then
3878 Error_Msg_N
3879 ("\provide initial value or explicit array bounds",
3880 Object_Definition (N));
3881 end if;
3882 end if;
3884 -- Case of initialization present but in error. Set initial
3885 -- expression as absent (but do not make above complaints)
3887 elsif E = Error then
3888 Set_Expression (N, Empty);
3889 E := Empty;
3891 -- Case of initialization present
3893 else
3894 -- Check restrictions in Ada 83
3896 if not Constant_Present (N) then
3898 -- Unconstrained variables not allowed in Ada 83 mode
3900 if Ada_Version = Ada_83
3901 and then Comes_From_Source (Object_Definition (N))
3902 then
3903 Error_Msg_N
3904 ("(Ada 83) unconstrained variable not allowed",
3905 Object_Definition (N));
3906 end if;
3907 end if;
3909 -- Now we constrain the variable from the initializing expression
3911 -- If the expression is an aggregate, it has been expanded into
3912 -- individual assignments. Retrieve the actual type from the
3913 -- expanded construct.
3915 if Is_Array_Type (T)
3916 and then No_Initialization (N)
3917 and then Nkind (Original_Node (E)) = N_Aggregate
3918 then
3919 Act_T := Etype (E);
3921 -- In case of class-wide interface object declarations we delay
3922 -- the generation of the equivalent record type declarations until
3923 -- its expansion because there are cases in they are not required.
3925 elsif Is_Interface (T) then
3926 null;
3928 -- In GNATprove mode, Expand_Subtype_From_Expr does nothing. Thus,
3929 -- we should prevent the generation of another Itype with the
3930 -- same name as the one already generated, or we end up with
3931 -- two identical types in GNATprove.
3933 elsif GNATprove_Mode then
3934 null;
3936 -- If the type is an unchecked union, no subtype can be built from
3937 -- the expression. Rewrite declaration as a renaming, which the
3938 -- back-end can handle properly. This is a rather unusual case,
3939 -- because most unchecked_union declarations have default values
3940 -- for discriminants and are thus not indefinite.
3942 elsif Is_Unchecked_Union (T) then
3943 if Constant_Present (N) or else Nkind (E) = N_Function_Call then
3944 Set_Ekind (Id, E_Constant);
3945 else
3946 Set_Ekind (Id, E_Variable);
3947 end if;
3949 -- An object declared within a Ghost region is automatically
3950 -- Ghost (SPARK RM 6.9(2)).
3952 if Comes_From_Source (Id) and then Ghost_Mode > None then
3953 Set_Is_Ghost_Entity (Id);
3955 -- The Ghost policy in effect at the point of declaration
3956 -- and at the point of completion must match
3957 -- (SPARK RM 6.9(15)).
3959 if Present (Prev_Entity)
3960 and then Is_Ghost_Entity (Prev_Entity)
3961 then
3962 Check_Ghost_Completion (Prev_Entity, Id);
3963 end if;
3964 end if;
3966 Rewrite (N,
3967 Make_Object_Renaming_Declaration (Loc,
3968 Defining_Identifier => Id,
3969 Subtype_Mark => New_Occurrence_Of (T, Loc),
3970 Name => E));
3972 Set_Renamed_Object (Id, E);
3973 Freeze_Before (N, T);
3974 Set_Is_Frozen (Id);
3975 return;
3977 else
3978 Expand_Subtype_From_Expr (N, T, Object_Definition (N), E);
3979 Act_T := Find_Type_Of_Object (Object_Definition (N), N);
3980 end if;
3982 Set_Is_Constr_Subt_For_U_Nominal (Act_T);
3984 if Aliased_Present (N) then
3985 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
3986 end if;
3988 Freeze_Before (N, Act_T);
3989 Freeze_Before (N, T);
3990 end if;
3992 elsif Is_Array_Type (T)
3993 and then No_Initialization (N)
3994 and then Nkind (Original_Node (E)) = N_Aggregate
3995 then
3996 if not Is_Entity_Name (Object_Definition (N)) then
3997 Act_T := Etype (E);
3998 Check_Compile_Time_Size (Act_T);
4000 if Aliased_Present (N) then
4001 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4002 end if;
4003 end if;
4005 -- When the given object definition and the aggregate are specified
4006 -- independently, and their lengths might differ do a length check.
4007 -- This cannot happen if the aggregate is of the form (others =>...)
4009 if not Is_Constrained (T) then
4010 null;
4012 elsif Nkind (E) = N_Raise_Constraint_Error then
4014 -- Aggregate is statically illegal. Place back in declaration
4016 Set_Expression (N, E);
4017 Set_No_Initialization (N, False);
4019 elsif T = Etype (E) then
4020 null;
4022 elsif Nkind (E) = N_Aggregate
4023 and then Present (Component_Associations (E))
4024 and then Present (Choices (First (Component_Associations (E))))
4025 and then Nkind (First
4026 (Choices (First (Component_Associations (E))))) = N_Others_Choice
4027 then
4028 null;
4030 else
4031 Apply_Length_Check (E, T);
4032 end if;
4034 -- If the type is limited unconstrained with defaulted discriminants and
4035 -- there is no expression, then the object is constrained by the
4036 -- defaults, so it is worthwhile building the corresponding subtype.
4038 elsif (Is_Limited_Record (T) or else Is_Concurrent_Type (T))
4039 and then not Is_Constrained (T)
4040 and then Has_Discriminants (T)
4041 then
4042 if No (E) then
4043 Act_T := Build_Default_Subtype (T, N);
4044 else
4045 -- Ada 2005: A limited object may be initialized by means of an
4046 -- aggregate. If the type has default discriminants it has an
4047 -- unconstrained nominal type, Its actual subtype will be obtained
4048 -- from the aggregate, and not from the default discriminants.
4050 Act_T := Etype (E);
4051 end if;
4053 Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
4055 elsif Nkind (E) = N_Function_Call
4056 and then Constant_Present (N)
4057 and then Has_Unconstrained_Elements (Etype (E))
4058 then
4059 -- The back-end has problems with constants of a discriminated type
4060 -- with defaults, if the initial value is a function call. We
4061 -- generate an intermediate temporary that will receive a reference
4062 -- to the result of the call. The initialization expression then
4063 -- becomes a dereference of that temporary.
4065 Remove_Side_Effects (E);
4067 -- If this is a constant declaration of an unconstrained type and
4068 -- the initialization is an aggregate, we can use the subtype of the
4069 -- aggregate for the declared entity because it is immutable.
4071 elsif not Is_Constrained (T)
4072 and then Has_Discriminants (T)
4073 and then Constant_Present (N)
4074 and then not Has_Unchecked_Union (T)
4075 and then Nkind (E) = N_Aggregate
4076 then
4077 Act_T := Etype (E);
4078 end if;
4080 -- Check No_Wide_Characters restriction
4082 Check_Wide_Character_Restriction (T, Object_Definition (N));
4084 -- Indicate this is not set in source. Certainly true for constants, and
4085 -- true for variables so far (will be reset for a variable if and when
4086 -- we encounter a modification in the source).
4088 Set_Never_Set_In_Source (Id);
4090 -- Now establish the proper kind and type of the object
4092 if Constant_Present (N) then
4093 Set_Ekind (Id, E_Constant);
4094 Set_Is_True_Constant (Id);
4096 else
4097 Set_Ekind (Id, E_Variable);
4099 -- A variable is set as shared passive if it appears in a shared
4100 -- passive package, and is at the outer level. This is not done for
4101 -- entities generated during expansion, because those are always
4102 -- manipulated locally.
4104 if Is_Shared_Passive (Current_Scope)
4105 and then Is_Library_Level_Entity (Id)
4106 and then Comes_From_Source (Id)
4107 then
4108 Set_Is_Shared_Passive (Id);
4109 Check_Shared_Var (Id, T, N);
4110 end if;
4112 -- Set Has_Initial_Value if initializing expression present. Note
4113 -- that if there is no initializing expression, we leave the state
4114 -- of this flag unchanged (usually it will be False, but notably in
4115 -- the case of exception choice variables, it will already be true).
4117 if Present (E) then
4118 Set_Has_Initial_Value (Id);
4119 end if;
4120 end if;
4122 -- Initialize alignment and size and capture alignment setting
4124 Init_Alignment (Id);
4125 Init_Esize (Id);
4126 Set_Optimize_Alignment_Flags (Id);
4128 -- An object declared within a Ghost region is automatically Ghost
4129 -- (SPARK RM 6.9(2)).
4131 if Comes_From_Source (Id)
4132 and then (Ghost_Mode > None
4133 or else (Present (Prev_Entity)
4134 and then Is_Ghost_Entity (Prev_Entity)))
4135 then
4136 Set_Is_Ghost_Entity (Id);
4138 -- The Ghost policy in effect at the point of declaration and at the
4139 -- point of completion must match (SPARK RM 6.9(16)).
4141 if Present (Prev_Entity) and then Is_Ghost_Entity (Prev_Entity) then
4142 Check_Ghost_Completion (Prev_Entity, Id);
4143 end if;
4144 end if;
4146 -- Deal with aliased case
4148 if Aliased_Present (N) then
4149 Set_Is_Aliased (Id);
4151 -- If the object is aliased and the type is unconstrained with
4152 -- defaulted discriminants and there is no expression, then the
4153 -- object is constrained by the defaults, so it is worthwhile
4154 -- building the corresponding subtype.
4156 -- Ada 2005 (AI-363): If the aliased object is discriminated and
4157 -- unconstrained, then only establish an actual subtype if the
4158 -- nominal subtype is indefinite. In definite cases the object is
4159 -- unconstrained in Ada 2005.
4161 if No (E)
4162 and then Is_Record_Type (T)
4163 and then not Is_Constrained (T)
4164 and then Has_Discriminants (T)
4165 and then (Ada_Version < Ada_2005 or else Is_Indefinite_Subtype (T))
4166 then
4167 Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
4168 end if;
4169 end if;
4171 -- Now we can set the type of the object
4173 Set_Etype (Id, Act_T);
4175 -- Non-constant object is marked to be treated as volatile if type is
4176 -- volatile and we clear the Current_Value setting that may have been
4177 -- set above. Doing so for constants isn't required and might interfere
4178 -- with possible uses of the object as a static expression in contexts
4179 -- incompatible with volatility (e.g. as a case-statement alternative).
4181 if Ekind (Id) /= E_Constant and then Treat_As_Volatile (Etype (Id)) then
4182 Set_Treat_As_Volatile (Id);
4183 Set_Current_Value (Id, Empty);
4184 end if;
4186 -- Deal with controlled types
4188 if Has_Controlled_Component (Etype (Id))
4189 or else Is_Controlled (Etype (Id))
4190 then
4191 if not Is_Library_Level_Entity (Id) then
4192 Check_Restriction (No_Nested_Finalization, N);
4193 else
4194 Validate_Controlled_Object (Id);
4195 end if;
4196 end if;
4198 if Has_Task (Etype (Id)) then
4199 Check_Restriction (No_Tasking, N);
4201 -- Deal with counting max tasks
4203 -- Nothing to do if inside a generic
4205 if Inside_A_Generic then
4206 null;
4208 -- If library level entity, then count tasks
4210 elsif Is_Library_Level_Entity (Id) then
4211 Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
4213 -- If not library level entity, then indicate we don't know max
4214 -- tasks and also check task hierarchy restriction and blocking
4215 -- operation (since starting a task is definitely blocking).
4217 else
4218 Check_Restriction (Max_Tasks, N);
4219 Check_Restriction (No_Task_Hierarchy, N);
4220 Check_Potentially_Blocking_Operation (N);
4221 end if;
4223 -- A rather specialized test. If we see two tasks being declared
4224 -- of the same type in the same object declaration, and the task
4225 -- has an entry with an address clause, we know that program error
4226 -- will be raised at run time since we can't have two tasks with
4227 -- entries at the same address.
4229 if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
4230 declare
4231 E : Entity_Id;
4233 begin
4234 E := First_Entity (Etype (Id));
4235 while Present (E) loop
4236 if Ekind (E) = E_Entry
4237 and then Present (Get_Attribute_Definition_Clause
4238 (E, Attribute_Address))
4239 then
4240 Error_Msg_Warn := SPARK_Mode /= On;
4241 Error_Msg_N
4242 ("more than one task with same entry address<<", N);
4243 Error_Msg_N ("\Program_Error [<<", N);
4244 Insert_Action (N,
4245 Make_Raise_Program_Error (Loc,
4246 Reason => PE_Duplicated_Entry_Address));
4247 exit;
4248 end if;
4250 Next_Entity (E);
4251 end loop;
4252 end;
4253 end if;
4254 end if;
4256 -- Some simple constant-propagation: if the expression is a constant
4257 -- string initialized with a literal, share the literal. This avoids
4258 -- a run-time copy.
4260 if Present (E)
4261 and then Is_Entity_Name (E)
4262 and then Ekind (Entity (E)) = E_Constant
4263 and then Base_Type (Etype (E)) = Standard_String
4264 then
4265 declare
4266 Val : constant Node_Id := Constant_Value (Entity (E));
4267 begin
4268 if Present (Val) and then Nkind (Val) = N_String_Literal then
4269 Rewrite (E, New_Copy (Val));
4270 end if;
4271 end;
4272 end if;
4274 -- Another optimization: if the nominal subtype is unconstrained and
4275 -- the expression is a function call that returns an unconstrained
4276 -- type, rewrite the declaration as a renaming of the result of the
4277 -- call. The exceptions below are cases where the copy is expected,
4278 -- either by the back end (Aliased case) or by the semantics, as for
4279 -- initializing controlled types or copying tags for classwide types.
4281 if Present (E)
4282 and then Nkind (E) = N_Explicit_Dereference
4283 and then Nkind (Original_Node (E)) = N_Function_Call
4284 and then not Is_Library_Level_Entity (Id)
4285 and then not Is_Constrained (Underlying_Type (T))
4286 and then not Is_Aliased (Id)
4287 and then not Is_Class_Wide_Type (T)
4288 and then not Is_Controlled (T)
4289 and then not Has_Controlled_Component (Base_Type (T))
4290 and then Expander_Active
4291 then
4292 Rewrite (N,
4293 Make_Object_Renaming_Declaration (Loc,
4294 Defining_Identifier => Id,
4295 Access_Definition => Empty,
4296 Subtype_Mark => New_Occurrence_Of
4297 (Base_Type (Etype (Id)), Loc),
4298 Name => E));
4300 Set_Renamed_Object (Id, E);
4302 -- Force generation of debugging information for the constant and for
4303 -- the renamed function call.
4305 Set_Debug_Info_Needed (Id);
4306 Set_Debug_Info_Needed (Entity (Prefix (E)));
4307 end if;
4309 if Present (Prev_Entity)
4310 and then Is_Frozen (Prev_Entity)
4311 and then not Error_Posted (Id)
4312 then
4313 Error_Msg_N ("full constant declaration appears too late", N);
4314 end if;
4316 Check_Eliminated (Id);
4318 -- Deal with setting In_Private_Part flag if in private part
4320 if Ekind (Scope (Id)) = E_Package and then In_Private_Part (Scope (Id))
4321 then
4322 Set_In_Private_Part (Id);
4323 end if;
4325 -- Check for violation of No_Local_Timing_Events
4327 if Restriction_Check_Required (No_Local_Timing_Events)
4328 and then not Is_Library_Level_Entity (Id)
4329 and then Is_RTE (Etype (Id), RE_Timing_Event)
4330 then
4331 Check_Restriction (No_Local_Timing_Events, N);
4332 end if;
4334 <<Leave>>
4335 -- Initialize the refined state of a variable here because this is a
4336 -- common destination for legal and illegal object declarations.
4338 if Ekind (Id) = E_Variable then
4339 Set_Encapsulating_State (Id, Empty);
4340 end if;
4342 if Has_Aspects (N) then
4343 Analyze_Aspect_Specifications (N, Id);
4344 end if;
4346 Analyze_Dimension (N);
4348 -- Verify whether the object declaration introduces an illegal hidden
4349 -- state within a package subject to a null abstract state.
4351 if Ekind (Id) = E_Variable then
4352 Check_No_Hidden_State (Id);
4353 end if;
4354 end Analyze_Object_Declaration;
4356 ---------------------------
4357 -- Analyze_Others_Choice --
4358 ---------------------------
4360 -- Nothing to do for the others choice node itself, the semantic analysis
4361 -- of the others choice will occur as part of the processing of the parent
4363 procedure Analyze_Others_Choice (N : Node_Id) is
4364 pragma Warnings (Off, N);
4365 begin
4366 null;
4367 end Analyze_Others_Choice;
4369 -------------------------------------------
4370 -- Analyze_Private_Extension_Declaration --
4371 -------------------------------------------
4373 procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
4374 T : constant Entity_Id := Defining_Identifier (N);
4375 Indic : constant Node_Id := Subtype_Indication (N);
4376 Parent_Type : Entity_Id;
4377 Parent_Base : Entity_Id;
4379 begin
4380 -- The private extension declaration may be subject to pragma Ghost with
4381 -- policy Ignore. Set the mode now to ensure that any nodes generated
4382 -- during analysis and expansion are properly flagged as ignored Ghost.
4384 Set_Ghost_Mode (N);
4386 -- Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
4388 if Is_Non_Empty_List (Interface_List (N)) then
4389 declare
4390 Intf : Node_Id;
4391 T : Entity_Id;
4393 begin
4394 Intf := First (Interface_List (N));
4395 while Present (Intf) loop
4396 T := Find_Type_Of_Subtype_Indic (Intf);
4398 Diagnose_Interface (Intf, T);
4399 Next (Intf);
4400 end loop;
4401 end;
4402 end if;
4404 Generate_Definition (T);
4406 -- For other than Ada 2012, just enter the name in the current scope
4408 if Ada_Version < Ada_2012 then
4409 Enter_Name (T);
4411 -- Ada 2012 (AI05-0162): Enter the name in the current scope handling
4412 -- case of private type that completes an incomplete type.
4414 else
4415 declare
4416 Prev : Entity_Id;
4418 begin
4419 Prev := Find_Type_Name (N);
4421 pragma Assert (Prev = T
4422 or else (Ekind (Prev) = E_Incomplete_Type
4423 and then Present (Full_View (Prev))
4424 and then Full_View (Prev) = T));
4425 end;
4426 end if;
4428 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
4429 Parent_Base := Base_Type (Parent_Type);
4431 if Parent_Type = Any_Type or else Etype (Parent_Type) = Any_Type then
4432 Set_Ekind (T, Ekind (Parent_Type));
4433 Set_Etype (T, Any_Type);
4434 goto Leave;
4436 elsif not Is_Tagged_Type (Parent_Type) then
4437 Error_Msg_N
4438 ("parent of type extension must be a tagged type ", Indic);
4439 goto Leave;
4441 elsif Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
4442 Error_Msg_N ("premature derivation of incomplete type", Indic);
4443 goto Leave;
4445 elsif Is_Concurrent_Type (Parent_Type) then
4446 Error_Msg_N
4447 ("parent type of a private extension cannot be "
4448 & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
4450 Set_Etype (T, Any_Type);
4451 Set_Ekind (T, E_Limited_Private_Type);
4452 Set_Private_Dependents (T, New_Elmt_List);
4453 Set_Error_Posted (T);
4454 goto Leave;
4455 end if;
4457 -- Perhaps the parent type should be changed to the class-wide type's
4458 -- specific type in this case to prevent cascading errors ???
4460 if Is_Class_Wide_Type (Parent_Type) then
4461 Error_Msg_N
4462 ("parent of type extension must not be a class-wide type", Indic);
4463 goto Leave;
4464 end if;
4466 if (not Is_Package_Or_Generic_Package (Current_Scope)
4467 and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
4468 or else In_Private_Part (Current_Scope)
4470 then
4471 Error_Msg_N ("invalid context for private extension", N);
4472 end if;
4474 -- Set common attributes
4476 Set_Is_Pure (T, Is_Pure (Current_Scope));
4477 Set_Scope (T, Current_Scope);
4478 Set_Ekind (T, E_Record_Type_With_Private);
4479 Init_Size_Align (T);
4480 Set_Default_SSO (T);
4482 Set_Etype (T, Parent_Base);
4483 Set_Has_Task (T, Has_Task (Parent_Base));
4484 Set_Has_Protected (T, Has_Task (Parent_Base));
4486 Set_Convention (T, Convention (Parent_Type));
4487 Set_First_Rep_Item (T, First_Rep_Item (Parent_Type));
4488 Set_Is_First_Subtype (T);
4489 Make_Class_Wide_Type (T);
4491 if Unknown_Discriminants_Present (N) then
4492 Set_Discriminant_Constraint (T, No_Elist);
4493 end if;
4495 Build_Derived_Record_Type (N, Parent_Type, T);
4497 -- Propagate inherited invariant information. The new type has
4498 -- invariants, if the parent type has inheritable invariants,
4499 -- and these invariants can in turn be inherited.
4501 if Has_Inheritable_Invariants (Parent_Type) then
4502 Set_Has_Inheritable_Invariants (T);
4503 Set_Has_Invariants (T);
4504 end if;
4506 -- Ada 2005 (AI-443): Synchronized private extension or a rewritten
4507 -- synchronized formal derived type.
4509 if Ada_Version >= Ada_2005 and then Synchronized_Present (N) then
4510 Set_Is_Limited_Record (T);
4512 -- Formal derived type case
4514 if Is_Generic_Type (T) then
4516 -- The parent must be a tagged limited type or a synchronized
4517 -- interface.
4519 if (not Is_Tagged_Type (Parent_Type)
4520 or else not Is_Limited_Type (Parent_Type))
4521 and then
4522 (not Is_Interface (Parent_Type)
4523 or else not Is_Synchronized_Interface (Parent_Type))
4524 then
4525 Error_Msg_NE ("parent type of & must be tagged limited " &
4526 "or synchronized", N, T);
4527 end if;
4529 -- The progenitors (if any) must be limited or synchronized
4530 -- interfaces.
4532 if Present (Interfaces (T)) then
4533 declare
4534 Iface : Entity_Id;
4535 Iface_Elmt : Elmt_Id;
4537 begin
4538 Iface_Elmt := First_Elmt (Interfaces (T));
4539 while Present (Iface_Elmt) loop
4540 Iface := Node (Iface_Elmt);
4542 if not Is_Limited_Interface (Iface)
4543 and then not Is_Synchronized_Interface (Iface)
4544 then
4545 Error_Msg_NE ("progenitor & must be limited " &
4546 "or synchronized", N, Iface);
4547 end if;
4549 Next_Elmt (Iface_Elmt);
4550 end loop;
4551 end;
4552 end if;
4554 -- Regular derived extension, the parent must be a limited or
4555 -- synchronized interface.
4557 else
4558 if not Is_Interface (Parent_Type)
4559 or else (not Is_Limited_Interface (Parent_Type)
4560 and then not Is_Synchronized_Interface (Parent_Type))
4561 then
4562 Error_Msg_NE
4563 ("parent type of & must be limited interface", N, T);
4564 end if;
4565 end if;
4567 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
4568 -- extension with a synchronized parent must be explicitly declared
4569 -- synchronized, because the full view will be a synchronized type.
4570 -- This must be checked before the check for limited types below,
4571 -- to ensure that types declared limited are not allowed to extend
4572 -- synchronized interfaces.
4574 elsif Is_Interface (Parent_Type)
4575 and then Is_Synchronized_Interface (Parent_Type)
4576 and then not Synchronized_Present (N)
4577 then
4578 Error_Msg_NE
4579 ("private extension of& must be explicitly synchronized",
4580 N, Parent_Type);
4582 elsif Limited_Present (N) then
4583 Set_Is_Limited_Record (T);
4585 if not Is_Limited_Type (Parent_Type)
4586 and then
4587 (not Is_Interface (Parent_Type)
4588 or else not Is_Limited_Interface (Parent_Type))
4589 then
4590 Error_Msg_NE ("parent type& of limited extension must be limited",
4591 N, Parent_Type);
4592 end if;
4593 end if;
4595 <<Leave>>
4596 if Has_Aspects (N) then
4597 Analyze_Aspect_Specifications (N, T);
4598 end if;
4599 end Analyze_Private_Extension_Declaration;
4601 ---------------------------------
4602 -- Analyze_Subtype_Declaration --
4603 ---------------------------------
4605 procedure Analyze_Subtype_Declaration
4606 (N : Node_Id;
4607 Skip : Boolean := False)
4609 Id : constant Entity_Id := Defining_Identifier (N);
4610 T : Entity_Id;
4611 R_Checks : Check_Result;
4613 begin
4614 -- The subtype declaration may be subject to pragma Ghost with policy
4615 -- Ignore. Set the mode now to ensure that any nodes generated during
4616 -- analysis and expansion are properly flagged as ignored Ghost.
4618 Set_Ghost_Mode (N);
4620 Generate_Definition (Id);
4621 Set_Is_Pure (Id, Is_Pure (Current_Scope));
4622 Init_Size_Align (Id);
4624 -- The following guard condition on Enter_Name is to handle cases where
4625 -- the defining identifier has already been entered into the scope but
4626 -- the declaration as a whole needs to be analyzed.
4628 -- This case in particular happens for derived enumeration types. The
4629 -- derived enumeration type is processed as an inserted enumeration type
4630 -- declaration followed by a rewritten subtype declaration. The defining
4631 -- identifier, however, is entered into the name scope very early in the
4632 -- processing of the original type declaration and therefore needs to be
4633 -- avoided here, when the created subtype declaration is analyzed. (See
4634 -- Build_Derived_Types)
4636 -- This also happens when the full view of a private type is derived
4637 -- type with constraints. In this case the entity has been introduced
4638 -- in the private declaration.
4640 -- Finally this happens in some complex cases when validity checks are
4641 -- enabled, where the same subtype declaration may be analyzed twice.
4642 -- This can happen if the subtype is created by the pre-analysis of
4643 -- an attribute tht gives the range of a loop statement, and the loop
4644 -- itself appears within an if_statement that will be rewritten during
4645 -- expansion.
4647 if Skip
4648 or else (Present (Etype (Id))
4649 and then (Is_Private_Type (Etype (Id))
4650 or else Is_Task_Type (Etype (Id))
4651 or else Is_Rewrite_Substitution (N)))
4652 then
4653 null;
4655 elsif Current_Entity (Id) = Id then
4656 null;
4658 else
4659 Enter_Name (Id);
4660 end if;
4662 T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
4664 -- Class-wide equivalent types of records with unknown discriminants
4665 -- involve the generation of an itype which serves as the private view
4666 -- of a constrained record subtype. In such cases the base type of the
4667 -- current subtype we are processing is the private itype. Use the full
4668 -- of the private itype when decorating various attributes.
4670 if Is_Itype (T)
4671 and then Is_Private_Type (T)
4672 and then Present (Full_View (T))
4673 then
4674 T := Full_View (T);
4675 end if;
4677 -- Inherit common attributes
4679 Set_Is_Volatile (Id, Is_Volatile (T));
4680 Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
4681 Set_Is_Generic_Type (Id, Is_Generic_Type (Base_Type (T)));
4682 Set_Convention (Id, Convention (T));
4684 -- If ancestor has predicates then so does the subtype, and in addition
4685 -- we must delay the freeze to properly arrange predicate inheritance.
4687 -- The Ancestor_Type test is really unpleasant, there seem to be cases
4688 -- in which T = ID, so the above tests and assignments do nothing???
4690 if Has_Predicates (T)
4691 or else (Present (Ancestor_Subtype (T))
4692 and then Has_Predicates (Ancestor_Subtype (T)))
4693 then
4694 Set_Has_Predicates (Id);
4695 Set_Has_Delayed_Freeze (Id);
4696 end if;
4698 -- Subtype of Boolean cannot have a constraint in SPARK
4700 if Is_Boolean_Type (T)
4701 and then Nkind (Subtype_Indication (N)) = N_Subtype_Indication
4702 then
4703 Check_SPARK_05_Restriction
4704 ("subtype of Boolean cannot have constraint", N);
4705 end if;
4707 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
4708 declare
4709 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
4710 One_Cstr : Node_Id;
4711 Low : Node_Id;
4712 High : Node_Id;
4714 begin
4715 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint then
4716 One_Cstr := First (Constraints (Cstr));
4717 while Present (One_Cstr) loop
4719 -- Index or discriminant constraint in SPARK must be a
4720 -- subtype mark.
4722 if not
4723 Nkind_In (One_Cstr, N_Identifier, N_Expanded_Name)
4724 then
4725 Check_SPARK_05_Restriction
4726 ("subtype mark required", One_Cstr);
4728 -- String subtype must have a lower bound of 1 in SPARK.
4729 -- Note that we do not need to test for the non-static case
4730 -- here, since that was already taken care of in
4731 -- Process_Range_Expr_In_Decl.
4733 elsif Base_Type (T) = Standard_String then
4734 Get_Index_Bounds (One_Cstr, Low, High);
4736 if Is_OK_Static_Expression (Low)
4737 and then Expr_Value (Low) /= 1
4738 then
4739 Check_SPARK_05_Restriction
4740 ("String subtype must have lower bound of 1", N);
4741 end if;
4742 end if;
4744 Next (One_Cstr);
4745 end loop;
4746 end if;
4747 end;
4748 end if;
4750 -- In the case where there is no constraint given in the subtype
4751 -- indication, Process_Subtype just returns the Subtype_Mark, so its
4752 -- semantic attributes must be established here.
4754 if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
4755 Set_Etype (Id, Base_Type (T));
4757 -- Subtype of unconstrained array without constraint is not allowed
4758 -- in SPARK.
4760 if Is_Array_Type (T) and then not Is_Constrained (T) then
4761 Check_SPARK_05_Restriction
4762 ("subtype of unconstrained array must have constraint", N);
4763 end if;
4765 case Ekind (T) is
4766 when Array_Kind =>
4767 Set_Ekind (Id, E_Array_Subtype);
4768 Copy_Array_Subtype_Attributes (Id, T);
4770 when Decimal_Fixed_Point_Kind =>
4771 Set_Ekind (Id, E_Decimal_Fixed_Point_Subtype);
4772 Set_Digits_Value (Id, Digits_Value (T));
4773 Set_Delta_Value (Id, Delta_Value (T));
4774 Set_Scale_Value (Id, Scale_Value (T));
4775 Set_Small_Value (Id, Small_Value (T));
4776 Set_Scalar_Range (Id, Scalar_Range (T));
4777 Set_Machine_Radix_10 (Id, Machine_Radix_10 (T));
4778 Set_Is_Constrained (Id, Is_Constrained (T));
4779 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4780 Set_RM_Size (Id, RM_Size (T));
4782 when Enumeration_Kind =>
4783 Set_Ekind (Id, E_Enumeration_Subtype);
4784 Set_First_Literal (Id, First_Literal (Base_Type (T)));
4785 Set_Scalar_Range (Id, Scalar_Range (T));
4786 Set_Is_Character_Type (Id, Is_Character_Type (T));
4787 Set_Is_Constrained (Id, Is_Constrained (T));
4788 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4789 Set_RM_Size (Id, RM_Size (T));
4790 Inherit_Predicate_Flags (Id, T);
4792 when Ordinary_Fixed_Point_Kind =>
4793 Set_Ekind (Id, E_Ordinary_Fixed_Point_Subtype);
4794 Set_Scalar_Range (Id, Scalar_Range (T));
4795 Set_Small_Value (Id, Small_Value (T));
4796 Set_Delta_Value (Id, Delta_Value (T));
4797 Set_Is_Constrained (Id, Is_Constrained (T));
4798 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4799 Set_RM_Size (Id, RM_Size (T));
4801 when Float_Kind =>
4802 Set_Ekind (Id, E_Floating_Point_Subtype);
4803 Set_Scalar_Range (Id, Scalar_Range (T));
4804 Set_Digits_Value (Id, Digits_Value (T));
4805 Set_Is_Constrained (Id, Is_Constrained (T));
4807 when Signed_Integer_Kind =>
4808 Set_Ekind (Id, E_Signed_Integer_Subtype);
4809 Set_Scalar_Range (Id, Scalar_Range (T));
4810 Set_Is_Constrained (Id, Is_Constrained (T));
4811 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4812 Set_RM_Size (Id, RM_Size (T));
4813 Inherit_Predicate_Flags (Id, T);
4815 when Modular_Integer_Kind =>
4816 Set_Ekind (Id, E_Modular_Integer_Subtype);
4817 Set_Scalar_Range (Id, Scalar_Range (T));
4818 Set_Is_Constrained (Id, Is_Constrained (T));
4819 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4820 Set_RM_Size (Id, RM_Size (T));
4821 Inherit_Predicate_Flags (Id, T);
4823 when Class_Wide_Kind =>
4824 Set_Ekind (Id, E_Class_Wide_Subtype);
4825 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
4826 Set_Cloned_Subtype (Id, T);
4827 Set_Is_Tagged_Type (Id, True);
4828 Set_Has_Unknown_Discriminants
4829 (Id, True);
4830 Set_No_Tagged_Streams_Pragma
4831 (Id, No_Tagged_Streams_Pragma (T));
4833 if Ekind (T) = E_Class_Wide_Subtype then
4834 Set_Equivalent_Type (Id, Equivalent_Type (T));
4835 end if;
4837 when E_Record_Type | E_Record_Subtype =>
4838 Set_Ekind (Id, E_Record_Subtype);
4840 if Ekind (T) = E_Record_Subtype
4841 and then Present (Cloned_Subtype (T))
4842 then
4843 Set_Cloned_Subtype (Id, Cloned_Subtype (T));
4844 else
4845 Set_Cloned_Subtype (Id, T);
4846 end if;
4848 Set_First_Entity (Id, First_Entity (T));
4849 Set_Last_Entity (Id, Last_Entity (T));
4850 Set_Has_Discriminants (Id, Has_Discriminants (T));
4851 Set_Is_Constrained (Id, Is_Constrained (T));
4852 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
4853 Set_Has_Implicit_Dereference
4854 (Id, Has_Implicit_Dereference (T));
4855 Set_Has_Unknown_Discriminants
4856 (Id, Has_Unknown_Discriminants (T));
4858 if Has_Discriminants (T) then
4859 Set_Discriminant_Constraint
4860 (Id, Discriminant_Constraint (T));
4861 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
4863 elsif Has_Unknown_Discriminants (Id) then
4864 Set_Discriminant_Constraint (Id, No_Elist);
4865 end if;
4867 if Is_Tagged_Type (T) then
4868 Set_Is_Tagged_Type (Id, True);
4869 Set_No_Tagged_Streams_Pragma
4870 (Id, No_Tagged_Streams_Pragma (T));
4871 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
4872 Set_Direct_Primitive_Operations
4873 (Id, Direct_Primitive_Operations (T));
4874 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
4876 if Is_Interface (T) then
4877 Set_Is_Interface (Id);
4878 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
4879 end if;
4880 end if;
4882 when Private_Kind =>
4883 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
4884 Set_Has_Discriminants (Id, Has_Discriminants (T));
4885 Set_Is_Constrained (Id, Is_Constrained (T));
4886 Set_First_Entity (Id, First_Entity (T));
4887 Set_Last_Entity (Id, Last_Entity (T));
4888 Set_Private_Dependents (Id, New_Elmt_List);
4889 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
4890 Set_Has_Implicit_Dereference
4891 (Id, Has_Implicit_Dereference (T));
4892 Set_Has_Unknown_Discriminants
4893 (Id, Has_Unknown_Discriminants (T));
4894 Set_Known_To_Have_Preelab_Init
4895 (Id, Known_To_Have_Preelab_Init (T));
4897 if Is_Tagged_Type (T) then
4898 Set_Is_Tagged_Type (Id);
4899 Set_No_Tagged_Streams_Pragma (Id,
4900 No_Tagged_Streams_Pragma (T));
4901 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
4902 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
4903 Set_Direct_Primitive_Operations (Id,
4904 Direct_Primitive_Operations (T));
4905 end if;
4907 -- In general the attributes of the subtype of a private type
4908 -- are the attributes of the partial view of parent. However,
4909 -- the full view may be a discriminated type, and the subtype
4910 -- must share the discriminant constraint to generate correct
4911 -- calls to initialization procedures.
4913 if Has_Discriminants (T) then
4914 Set_Discriminant_Constraint
4915 (Id, Discriminant_Constraint (T));
4916 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
4918 elsif Present (Full_View (T))
4919 and then Has_Discriminants (Full_View (T))
4920 then
4921 Set_Discriminant_Constraint
4922 (Id, Discriminant_Constraint (Full_View (T)));
4923 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
4925 -- This would seem semantically correct, but apparently
4926 -- generates spurious errors about missing components ???
4928 -- Set_Has_Discriminants (Id);
4929 end if;
4931 Prepare_Private_Subtype_Completion (Id, N);
4933 -- If this is the subtype of a constrained private type with
4934 -- discriminants that has got a full view and we also have
4935 -- built a completion just above, show that the completion
4936 -- is a clone of the full view to the back-end.
4938 if Has_Discriminants (T)
4939 and then not Has_Unknown_Discriminants (T)
4940 and then not Is_Empty_Elmt_List (Discriminant_Constraint (T))
4941 and then Present (Full_View (T))
4942 and then Present (Full_View (Id))
4943 then
4944 Set_Cloned_Subtype (Full_View (Id), Full_View (T));
4945 end if;
4947 when Access_Kind =>
4948 Set_Ekind (Id, E_Access_Subtype);
4949 Set_Is_Constrained (Id, Is_Constrained (T));
4950 Set_Is_Access_Constant
4951 (Id, Is_Access_Constant (T));
4952 Set_Directly_Designated_Type
4953 (Id, Designated_Type (T));
4954 Set_Can_Never_Be_Null (Id, Can_Never_Be_Null (T));
4956 -- A Pure library_item must not contain the declaration of a
4957 -- named access type, except within a subprogram, generic
4958 -- subprogram, task unit, or protected unit, or if it has
4959 -- a specified Storage_Size of zero (RM05-10.2.1(15.4-15.5)).
4961 if Comes_From_Source (Id)
4962 and then In_Pure_Unit
4963 and then not In_Subprogram_Task_Protected_Unit
4964 and then not No_Pool_Assigned (Id)
4965 then
4966 Error_Msg_N
4967 ("named access types not allowed in pure unit", N);
4968 end if;
4970 when Concurrent_Kind =>
4971 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
4972 Set_Corresponding_Record_Type (Id,
4973 Corresponding_Record_Type (T));
4974 Set_First_Entity (Id, First_Entity (T));
4975 Set_First_Private_Entity (Id, First_Private_Entity (T));
4976 Set_Has_Discriminants (Id, Has_Discriminants (T));
4977 Set_Is_Constrained (Id, Is_Constrained (T));
4978 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
4979 Set_Last_Entity (Id, Last_Entity (T));
4981 if Is_Tagged_Type (T) then
4982 Set_No_Tagged_Streams_Pragma
4983 (Id, No_Tagged_Streams_Pragma (T));
4984 end if;
4986 if Has_Discriminants (T) then
4987 Set_Discriminant_Constraint
4988 (Id, Discriminant_Constraint (T));
4989 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
4990 end if;
4992 when Incomplete_Kind =>
4993 if Ada_Version >= Ada_2005 then
4995 -- In Ada 2005 an incomplete type can be explicitly tagged:
4996 -- propagate indication. Note that we also have to include
4997 -- subtypes for Ada 2012 extended use of incomplete types.
4999 Set_Ekind (Id, E_Incomplete_Subtype);
5000 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5001 Set_Private_Dependents (Id, New_Elmt_List);
5003 if Is_Tagged_Type (Id) then
5004 Set_No_Tagged_Streams_Pragma
5005 (Id, No_Tagged_Streams_Pragma (T));
5006 Set_Direct_Primitive_Operations (Id, New_Elmt_List);
5007 end if;
5009 -- Ada 2005 (AI-412): Decorate an incomplete subtype of an
5010 -- incomplete type visible through a limited with clause.
5012 if From_Limited_With (T)
5013 and then Present (Non_Limited_View (T))
5014 then
5015 Set_From_Limited_With (Id);
5016 Set_Non_Limited_View (Id, Non_Limited_View (T));
5018 -- Ada 2005 (AI-412): Add the regular incomplete subtype
5019 -- to the private dependents of the original incomplete
5020 -- type for future transformation.
5022 else
5023 Append_Elmt (Id, Private_Dependents (T));
5024 end if;
5026 -- If the subtype name denotes an incomplete type an error
5027 -- was already reported by Process_Subtype.
5029 else
5030 Set_Etype (Id, Any_Type);
5031 end if;
5033 when others =>
5034 raise Program_Error;
5035 end case;
5036 end if;
5038 if Etype (Id) = Any_Type then
5039 goto Leave;
5040 end if;
5042 -- Some common processing on all types
5044 Set_Size_Info (Id, T);
5045 Set_First_Rep_Item (Id, First_Rep_Item (T));
5047 -- If the parent type is a generic actual, so is the subtype. This may
5048 -- happen in a nested instance. Why Comes_From_Source test???
5050 if not Comes_From_Source (N) then
5051 Set_Is_Generic_Actual_Type (Id, Is_Generic_Actual_Type (T));
5052 end if;
5054 T := Etype (Id);
5056 Set_Is_Immediately_Visible (Id, True);
5057 Set_Depends_On_Private (Id, Has_Private_Component (T));
5058 Set_Is_Descendent_Of_Address (Id, Is_Descendent_Of_Address (T));
5060 if Is_Interface (T) then
5061 Set_Is_Interface (Id);
5062 end if;
5064 if Present (Generic_Parent_Type (N))
5065 and then
5066 (Nkind (Parent (Generic_Parent_Type (N))) /=
5067 N_Formal_Type_Declaration
5068 or else Nkind (Formal_Type_Definition
5069 (Parent (Generic_Parent_Type (N)))) /=
5070 N_Formal_Private_Type_Definition)
5071 then
5072 if Is_Tagged_Type (Id) then
5074 -- If this is a generic actual subtype for a synchronized type,
5075 -- the primitive operations are those of the corresponding record
5076 -- for which there is a separate subtype declaration.
5078 if Is_Concurrent_Type (Id) then
5079 null;
5080 elsif Is_Class_Wide_Type (Id) then
5081 Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
5082 else
5083 Derive_Subprograms (Generic_Parent_Type (N), Id, T);
5084 end if;
5086 elsif Scope (Etype (Id)) /= Standard_Standard then
5087 Derive_Subprograms (Generic_Parent_Type (N), Id);
5088 end if;
5089 end if;
5091 if Is_Private_Type (T) and then Present (Full_View (T)) then
5092 Conditional_Delay (Id, Full_View (T));
5094 -- The subtypes of components or subcomponents of protected types
5095 -- do not need freeze nodes, which would otherwise appear in the
5096 -- wrong scope (before the freeze node for the protected type). The
5097 -- proper subtypes are those of the subcomponents of the corresponding
5098 -- record.
5100 elsif Ekind (Scope (Id)) /= E_Protected_Type
5101 and then Present (Scope (Scope (Id))) -- error defense
5102 and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
5103 then
5104 Conditional_Delay (Id, T);
5105 end if;
5107 -- Check that Constraint_Error is raised for a scalar subtype indication
5108 -- when the lower or upper bound of a non-null range lies outside the
5109 -- range of the type mark.
5111 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5112 if Is_Scalar_Type (Etype (Id))
5113 and then Scalar_Range (Id) /=
5114 Scalar_Range (Etype (Subtype_Mark
5115 (Subtype_Indication (N))))
5116 then
5117 Apply_Range_Check
5118 (Scalar_Range (Id),
5119 Etype (Subtype_Mark (Subtype_Indication (N))));
5121 -- In the array case, check compatibility for each index
5123 elsif Is_Array_Type (Etype (Id)) and then Present (First_Index (Id))
5124 then
5125 -- This really should be a subprogram that finds the indications
5126 -- to check???
5128 declare
5129 Subt_Index : Node_Id := First_Index (Id);
5130 Target_Index : Node_Id :=
5131 First_Index (Etype
5132 (Subtype_Mark (Subtype_Indication (N))));
5133 Has_Dyn_Chk : Boolean := Has_Dynamic_Range_Check (N);
5135 begin
5136 while Present (Subt_Index) loop
5137 if ((Nkind (Subt_Index) = N_Identifier
5138 and then Ekind (Entity (Subt_Index)) in Scalar_Kind)
5139 or else Nkind (Subt_Index) = N_Subtype_Indication)
5140 and then
5141 Nkind (Scalar_Range (Etype (Subt_Index))) = N_Range
5142 then
5143 declare
5144 Target_Typ : constant Entity_Id :=
5145 Etype (Target_Index);
5146 begin
5147 R_Checks :=
5148 Get_Range_Checks
5149 (Scalar_Range (Etype (Subt_Index)),
5150 Target_Typ,
5151 Etype (Subt_Index),
5152 Defining_Identifier (N));
5154 -- Reset Has_Dynamic_Range_Check on the subtype to
5155 -- prevent elision of the index check due to a dynamic
5156 -- check generated for a preceding index (needed since
5157 -- Insert_Range_Checks tries to avoid generating
5158 -- redundant checks on a given declaration).
5160 Set_Has_Dynamic_Range_Check (N, False);
5162 Insert_Range_Checks
5163 (R_Checks,
5165 Target_Typ,
5166 Sloc (Defining_Identifier (N)));
5168 -- Record whether this index involved a dynamic check
5170 Has_Dyn_Chk :=
5171 Has_Dyn_Chk or else Has_Dynamic_Range_Check (N);
5172 end;
5173 end if;
5175 Next_Index (Subt_Index);
5176 Next_Index (Target_Index);
5177 end loop;
5179 -- Finally, mark whether the subtype involves dynamic checks
5181 Set_Has_Dynamic_Range_Check (N, Has_Dyn_Chk);
5182 end;
5183 end if;
5184 end if;
5186 -- A type invariant applies to any subtype in its scope, in particular
5187 -- to a generic actual.
5189 if Has_Invariants (T) and then In_Open_Scopes (Scope (T)) then
5190 Set_Has_Invariants (Id);
5191 Set_Invariant_Procedure (Id, Invariant_Procedure (T));
5192 end if;
5194 -- Make sure that generic actual types are properly frozen. The subtype
5195 -- is marked as a generic actual type when the enclosing instance is
5196 -- analyzed, so here we identify the subtype from the tree structure.
5198 if Expander_Active
5199 and then Is_Generic_Actual_Type (Id)
5200 and then In_Instance
5201 and then not Comes_From_Source (N)
5202 and then Nkind (Subtype_Indication (N)) /= N_Subtype_Indication
5203 and then Is_Frozen (T)
5204 then
5205 Freeze_Before (N, Id);
5206 end if;
5208 Set_Optimize_Alignment_Flags (Id);
5209 Check_Eliminated (Id);
5211 <<Leave>>
5212 if Has_Aspects (N) then
5213 Analyze_Aspect_Specifications (N, Id);
5214 end if;
5216 Analyze_Dimension (N);
5217 end Analyze_Subtype_Declaration;
5219 --------------------------------
5220 -- Analyze_Subtype_Indication --
5221 --------------------------------
5223 procedure Analyze_Subtype_Indication (N : Node_Id) is
5224 T : constant Entity_Id := Subtype_Mark (N);
5225 R : constant Node_Id := Range_Expression (Constraint (N));
5227 begin
5228 Analyze (T);
5230 if R /= Error then
5231 Analyze (R);
5232 Set_Etype (N, Etype (R));
5233 Resolve (R, Entity (T));
5234 else
5235 Set_Error_Posted (R);
5236 Set_Error_Posted (T);
5237 end if;
5238 end Analyze_Subtype_Indication;
5240 --------------------------
5241 -- Analyze_Variant_Part --
5242 --------------------------
5244 procedure Analyze_Variant_Part (N : Node_Id) is
5245 Discr_Name : Node_Id;
5246 Discr_Type : Entity_Id;
5248 procedure Process_Variant (A : Node_Id);
5249 -- Analyze declarations for a single variant
5251 package Analyze_Variant_Choices is
5252 new Generic_Analyze_Choices (Process_Variant);
5253 use Analyze_Variant_Choices;
5255 ---------------------
5256 -- Process_Variant --
5257 ---------------------
5259 procedure Process_Variant (A : Node_Id) is
5260 CL : constant Node_Id := Component_List (A);
5261 begin
5262 if not Null_Present (CL) then
5263 Analyze_Declarations (Component_Items (CL));
5265 if Present (Variant_Part (CL)) then
5266 Analyze (Variant_Part (CL));
5267 end if;
5268 end if;
5269 end Process_Variant;
5271 -- Start of processing for Analyze_Variant_Part
5273 begin
5274 Discr_Name := Name (N);
5275 Analyze (Discr_Name);
5277 -- If Discr_Name bad, get out (prevent cascaded errors)
5279 if Etype (Discr_Name) = Any_Type then
5280 return;
5281 end if;
5283 -- Check invalid discriminant in variant part
5285 if Ekind (Entity (Discr_Name)) /= E_Discriminant then
5286 Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
5287 end if;
5289 Discr_Type := Etype (Entity (Discr_Name));
5291 if not Is_Discrete_Type (Discr_Type) then
5292 Error_Msg_N
5293 ("discriminant in a variant part must be of a discrete type",
5294 Name (N));
5295 return;
5296 end if;
5298 -- Now analyze the choices, which also analyzes the declarations that
5299 -- are associated with each choice.
5301 Analyze_Choices (Variants (N), Discr_Type);
5303 -- Note: we used to instantiate and call Check_Choices here to check
5304 -- that the choices covered the discriminant, but it's too early to do
5305 -- that because of statically predicated subtypes, whose analysis may
5306 -- be deferred to their freeze point which may be as late as the freeze
5307 -- point of the containing record. So this call is now to be found in
5308 -- Freeze_Record_Declaration.
5310 end Analyze_Variant_Part;
5312 ----------------------------
5313 -- Array_Type_Declaration --
5314 ----------------------------
5316 procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
5317 Component_Def : constant Node_Id := Component_Definition (Def);
5318 Component_Typ : constant Node_Id := Subtype_Indication (Component_Def);
5319 Element_Type : Entity_Id;
5320 Implicit_Base : Entity_Id;
5321 Index : Node_Id;
5322 Related_Id : Entity_Id := Empty;
5323 Nb_Index : Nat;
5324 P : constant Node_Id := Parent (Def);
5325 Priv : Entity_Id;
5327 begin
5328 if Nkind (Def) = N_Constrained_Array_Definition then
5329 Index := First (Discrete_Subtype_Definitions (Def));
5330 else
5331 Index := First (Subtype_Marks (Def));
5332 end if;
5334 -- Find proper names for the implicit types which may be public. In case
5335 -- of anonymous arrays we use the name of the first object of that type
5336 -- as prefix.
5338 if No (T) then
5339 Related_Id := Defining_Identifier (P);
5340 else
5341 Related_Id := T;
5342 end if;
5344 Nb_Index := 1;
5345 while Present (Index) loop
5346 Analyze (Index);
5348 -- Test for odd case of trying to index a type by the type itself
5350 if Is_Entity_Name (Index) and then Entity (Index) = T then
5351 Error_Msg_N ("type& cannot be indexed by itself", Index);
5352 Set_Entity (Index, Standard_Boolean);
5353 Set_Etype (Index, Standard_Boolean);
5354 end if;
5356 -- Check SPARK restriction requiring a subtype mark
5358 if not Nkind_In (Index, N_Identifier, N_Expanded_Name) then
5359 Check_SPARK_05_Restriction ("subtype mark required", Index);
5360 end if;
5362 -- Add a subtype declaration for each index of private array type
5363 -- declaration whose etype is also private. For example:
5365 -- package Pkg is
5366 -- type Index is private;
5367 -- private
5368 -- type Table is array (Index) of ...
5369 -- end;
5371 -- This is currently required by the expander for the internally
5372 -- generated equality subprogram of records with variant parts in
5373 -- which the etype of some component is such private type.
5375 if Ekind (Current_Scope) = E_Package
5376 and then In_Private_Part (Current_Scope)
5377 and then Has_Private_Declaration (Etype (Index))
5378 then
5379 declare
5380 Loc : constant Source_Ptr := Sloc (Def);
5381 New_E : Entity_Id;
5382 Decl : Entity_Id;
5384 begin
5385 New_E := Make_Temporary (Loc, 'T');
5386 Set_Is_Internal (New_E);
5388 Decl :=
5389 Make_Subtype_Declaration (Loc,
5390 Defining_Identifier => New_E,
5391 Subtype_Indication =>
5392 New_Occurrence_Of (Etype (Index), Loc));
5394 Insert_Before (Parent (Def), Decl);
5395 Analyze (Decl);
5396 Set_Etype (Index, New_E);
5398 -- If the index is a range the Entity attribute is not
5399 -- available. Example:
5401 -- package Pkg is
5402 -- type T is private;
5403 -- private
5404 -- type T is new Natural;
5405 -- Table : array (T(1) .. T(10)) of Boolean;
5406 -- end Pkg;
5408 if Nkind (Index) /= N_Range then
5409 Set_Entity (Index, New_E);
5410 end if;
5411 end;
5412 end if;
5414 Make_Index (Index, P, Related_Id, Nb_Index);
5416 -- Check error of subtype with predicate for index type
5418 Bad_Predicated_Subtype_Use
5419 ("subtype& has predicate, not allowed as index subtype",
5420 Index, Etype (Index));
5422 -- Move to next index
5424 Next_Index (Index);
5425 Nb_Index := Nb_Index + 1;
5426 end loop;
5428 -- Process subtype indication if one is present
5430 if Present (Component_Typ) then
5431 Element_Type := Process_Subtype (Component_Typ, P, Related_Id, 'C');
5433 Set_Etype (Component_Typ, Element_Type);
5435 if not Nkind_In (Component_Typ, N_Identifier, N_Expanded_Name) then
5436 Check_SPARK_05_Restriction
5437 ("subtype mark required", Component_Typ);
5438 end if;
5440 -- Ada 2005 (AI-230): Access Definition case
5442 else pragma Assert (Present (Access_Definition (Component_Def)));
5444 -- Indicate that the anonymous access type is created by the
5445 -- array type declaration.
5447 Element_Type := Access_Definition
5448 (Related_Nod => P,
5449 N => Access_Definition (Component_Def));
5450 Set_Is_Local_Anonymous_Access (Element_Type);
5452 -- Propagate the parent. This field is needed if we have to generate
5453 -- the master_id associated with an anonymous access to task type
5454 -- component (see Expand_N_Full_Type_Declaration.Build_Master)
5456 Set_Parent (Element_Type, Parent (T));
5458 -- Ada 2005 (AI-230): In case of components that are anonymous access
5459 -- types the level of accessibility depends on the enclosing type
5460 -- declaration
5462 Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
5464 -- Ada 2005 (AI-254)
5466 declare
5467 CD : constant Node_Id :=
5468 Access_To_Subprogram_Definition
5469 (Access_Definition (Component_Def));
5470 begin
5471 if Present (CD) and then Protected_Present (CD) then
5472 Element_Type :=
5473 Replace_Anonymous_Access_To_Protected_Subprogram (Def);
5474 end if;
5475 end;
5476 end if;
5478 -- Constrained array case
5480 if No (T) then
5481 T := Create_Itype (E_Void, P, Related_Id, 'T');
5482 end if;
5484 if Nkind (Def) = N_Constrained_Array_Definition then
5486 -- Establish Implicit_Base as unconstrained base type
5488 Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
5490 Set_Etype (Implicit_Base, Implicit_Base);
5491 Set_Scope (Implicit_Base, Current_Scope);
5492 Set_Has_Delayed_Freeze (Implicit_Base);
5493 Set_Default_SSO (Implicit_Base);
5495 -- The constrained array type is a subtype of the unconstrained one
5497 Set_Ekind (T, E_Array_Subtype);
5498 Init_Size_Align (T);
5499 Set_Etype (T, Implicit_Base);
5500 Set_Scope (T, Current_Scope);
5501 Set_Is_Constrained (T);
5502 Set_First_Index (T,
5503 First (Discrete_Subtype_Definitions (Def)));
5504 Set_Has_Delayed_Freeze (T);
5506 -- Complete setup of implicit base type
5508 Set_First_Index (Implicit_Base, First_Index (T));
5509 Set_Component_Type (Implicit_Base, Element_Type);
5510 Set_Has_Task (Implicit_Base, Has_Task (Element_Type));
5511 Set_Has_Protected (Implicit_Base, Has_Protected (Element_Type));
5512 Set_Component_Size (Implicit_Base, Uint_0);
5513 Set_Packed_Array_Impl_Type (Implicit_Base, Empty);
5514 Set_Has_Controlled_Component (Implicit_Base,
5515 Has_Controlled_Component (Element_Type)
5516 or else Is_Controlled (Element_Type));
5517 Set_Finalize_Storage_Only (Implicit_Base,
5518 Finalize_Storage_Only (Element_Type));
5520 -- Inherit the "ghostness" from the constrained array type
5522 if Is_Ghost_Entity (T) or else Ghost_Mode > None then
5523 Set_Is_Ghost_Entity (Implicit_Base);
5524 end if;
5526 -- Unconstrained array case
5528 else
5529 Set_Ekind (T, E_Array_Type);
5530 Init_Size_Align (T);
5531 Set_Etype (T, T);
5532 Set_Scope (T, Current_Scope);
5533 Set_Component_Size (T, Uint_0);
5534 Set_Is_Constrained (T, False);
5535 Set_First_Index (T, First (Subtype_Marks (Def)));
5536 Set_Has_Delayed_Freeze (T, True);
5537 Set_Has_Task (T, Has_Task (Element_Type));
5538 Set_Has_Protected (T, Has_Protected (Element_Type));
5539 Set_Has_Controlled_Component (T, Has_Controlled_Component
5540 (Element_Type)
5541 or else
5542 Is_Controlled (Element_Type));
5543 Set_Finalize_Storage_Only (T, Finalize_Storage_Only
5544 (Element_Type));
5545 Set_Default_SSO (T);
5546 end if;
5548 -- Common attributes for both cases
5550 Set_Component_Type (Base_Type (T), Element_Type);
5551 Set_Packed_Array_Impl_Type (T, Empty);
5553 if Aliased_Present (Component_Definition (Def)) then
5554 Check_SPARK_05_Restriction
5555 ("aliased is not allowed", Component_Definition (Def));
5556 Set_Has_Aliased_Components (Etype (T));
5557 end if;
5559 -- Ada 2005 (AI-231): Propagate the null-excluding attribute to the
5560 -- array type to ensure that objects of this type are initialized.
5562 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (Element_Type) then
5563 Set_Can_Never_Be_Null (T);
5565 if Null_Exclusion_Present (Component_Definition (Def))
5567 -- No need to check itypes because in their case this check was
5568 -- done at their point of creation
5570 and then not Is_Itype (Element_Type)
5571 then
5572 Error_Msg_N
5573 ("`NOT NULL` not allowed (null already excluded)",
5574 Subtype_Indication (Component_Definition (Def)));
5575 end if;
5576 end if;
5578 Priv := Private_Component (Element_Type);
5580 if Present (Priv) then
5582 -- Check for circular definitions
5584 if Priv = Any_Type then
5585 Set_Component_Type (Etype (T), Any_Type);
5587 -- There is a gap in the visibility of operations on the composite
5588 -- type only if the component type is defined in a different scope.
5590 elsif Scope (Priv) = Current_Scope then
5591 null;
5593 elsif Is_Limited_Type (Priv) then
5594 Set_Is_Limited_Composite (Etype (T));
5595 Set_Is_Limited_Composite (T);
5596 else
5597 Set_Is_Private_Composite (Etype (T));
5598 Set_Is_Private_Composite (T);
5599 end if;
5600 end if;
5602 -- A syntax error in the declaration itself may lead to an empty index
5603 -- list, in which case do a minimal patch.
5605 if No (First_Index (T)) then
5606 Error_Msg_N ("missing index definition in array type declaration", T);
5608 declare
5609 Indexes : constant List_Id :=
5610 New_List (New_Occurrence_Of (Any_Id, Sloc (T)));
5611 begin
5612 Set_Discrete_Subtype_Definitions (Def, Indexes);
5613 Set_First_Index (T, First (Indexes));
5614 return;
5615 end;
5616 end if;
5618 -- Create a concatenation operator for the new type. Internal array
5619 -- types created for packed entities do not need such, they are
5620 -- compatible with the user-defined type.
5622 if Number_Dimensions (T) = 1
5623 and then not Is_Packed_Array_Impl_Type (T)
5624 then
5625 New_Concatenation_Op (T);
5626 end if;
5628 -- In the case of an unconstrained array the parser has already verified
5629 -- that all the indexes are unconstrained but we still need to make sure
5630 -- that the element type is constrained.
5632 if Is_Indefinite_Subtype (Element_Type) then
5633 Error_Msg_N
5634 ("unconstrained element type in array declaration",
5635 Subtype_Indication (Component_Def));
5637 elsif Is_Abstract_Type (Element_Type) then
5638 Error_Msg_N
5639 ("the type of a component cannot be abstract",
5640 Subtype_Indication (Component_Def));
5641 end if;
5643 -- There may be an invariant declared for the component type, but
5644 -- the construction of the component invariant checking procedure
5645 -- takes place during expansion.
5646 end Array_Type_Declaration;
5648 ------------------------------------------------------
5649 -- Replace_Anonymous_Access_To_Protected_Subprogram --
5650 ------------------------------------------------------
5652 function Replace_Anonymous_Access_To_Protected_Subprogram
5653 (N : Node_Id) return Entity_Id
5655 Loc : constant Source_Ptr := Sloc (N);
5657 Curr_Scope : constant Scope_Stack_Entry :=
5658 Scope_Stack.Table (Scope_Stack.Last);
5660 Anon : constant Entity_Id := Make_Temporary (Loc, 'S');
5662 Acc : Node_Id;
5663 -- Access definition in declaration
5665 Comp : Node_Id;
5666 -- Object definition or formal definition with an access definition
5668 Decl : Node_Id;
5669 -- Declaration of anonymous access to subprogram type
5671 Spec : Node_Id;
5672 -- Original specification in access to subprogram
5674 P : Node_Id;
5676 begin
5677 Set_Is_Internal (Anon);
5679 case Nkind (N) is
5680 when N_Component_Declaration |
5681 N_Unconstrained_Array_Definition |
5682 N_Constrained_Array_Definition =>
5683 Comp := Component_Definition (N);
5684 Acc := Access_Definition (Comp);
5686 when N_Discriminant_Specification =>
5687 Comp := Discriminant_Type (N);
5688 Acc := Comp;
5690 when N_Parameter_Specification =>
5691 Comp := Parameter_Type (N);
5692 Acc := Comp;
5694 when N_Access_Function_Definition =>
5695 Comp := Result_Definition (N);
5696 Acc := Comp;
5698 when N_Object_Declaration =>
5699 Comp := Object_Definition (N);
5700 Acc := Comp;
5702 when N_Function_Specification =>
5703 Comp := Result_Definition (N);
5704 Acc := Comp;
5706 when others =>
5707 raise Program_Error;
5708 end case;
5710 Spec := Access_To_Subprogram_Definition (Acc);
5712 Decl :=
5713 Make_Full_Type_Declaration (Loc,
5714 Defining_Identifier => Anon,
5715 Type_Definition => Copy_Separate_Tree (Spec));
5717 Mark_Rewrite_Insertion (Decl);
5719 -- In ASIS mode, analyze the profile on the original node, because
5720 -- the separate copy does not provide enough links to recover the
5721 -- original tree. Analysis is limited to type annotations, within
5722 -- a temporary scope that serves as an anonymous subprogram to collect
5723 -- otherwise useless temporaries and itypes.
5725 if ASIS_Mode then
5726 declare
5727 Typ : constant Entity_Id := Make_Temporary (Loc, 'S');
5729 begin
5730 if Nkind (Spec) = N_Access_Function_Definition then
5731 Set_Ekind (Typ, E_Function);
5732 else
5733 Set_Ekind (Typ, E_Procedure);
5734 end if;
5736 Set_Parent (Typ, N);
5737 Set_Scope (Typ, Current_Scope);
5738 Push_Scope (Typ);
5740 Process_Formals (Parameter_Specifications (Spec), Spec);
5742 if Nkind (Spec) = N_Access_Function_Definition then
5743 declare
5744 Def : constant Node_Id := Result_Definition (Spec);
5746 begin
5747 -- The result might itself be an anonymous access type, so
5748 -- have to recurse.
5750 if Nkind (Def) = N_Access_Definition then
5751 if Present (Access_To_Subprogram_Definition (Def)) then
5752 Set_Etype
5753 (Def,
5754 Replace_Anonymous_Access_To_Protected_Subprogram
5755 (Spec));
5756 else
5757 Find_Type (Subtype_Mark (Def));
5758 end if;
5760 else
5761 Find_Type (Def);
5762 end if;
5763 end;
5764 end if;
5766 End_Scope;
5767 end;
5768 end if;
5770 -- Insert the new declaration in the nearest enclosing scope. If the
5771 -- node is a body and N is its return type, the declaration belongs in
5772 -- the enclosing scope.
5774 P := Parent (N);
5776 if Nkind (P) = N_Subprogram_Body
5777 and then Nkind (N) = N_Function_Specification
5778 then
5779 P := Parent (P);
5780 end if;
5782 while Present (P) and then not Has_Declarations (P) loop
5783 P := Parent (P);
5784 end loop;
5786 pragma Assert (Present (P));
5788 if Nkind (P) = N_Package_Specification then
5789 Prepend (Decl, Visible_Declarations (P));
5790 else
5791 Prepend (Decl, Declarations (P));
5792 end if;
5794 -- Replace the anonymous type with an occurrence of the new declaration.
5795 -- In all cases the rewritten node does not have the null-exclusion
5796 -- attribute because (if present) it was already inherited by the
5797 -- anonymous entity (Anon). Thus, in case of components we do not
5798 -- inherit this attribute.
5800 if Nkind (N) = N_Parameter_Specification then
5801 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5802 Set_Etype (Defining_Identifier (N), Anon);
5803 Set_Null_Exclusion_Present (N, False);
5805 elsif Nkind (N) = N_Object_Declaration then
5806 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5807 Set_Etype (Defining_Identifier (N), Anon);
5809 elsif Nkind (N) = N_Access_Function_Definition then
5810 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5812 elsif Nkind (N) = N_Function_Specification then
5813 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5814 Set_Etype (Defining_Unit_Name (N), Anon);
5816 else
5817 Rewrite (Comp,
5818 Make_Component_Definition (Loc,
5819 Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
5820 end if;
5822 Mark_Rewrite_Insertion (Comp);
5824 if Nkind_In (N, N_Object_Declaration, N_Access_Function_Definition) then
5825 Analyze (Decl);
5827 else
5828 -- Temporarily remove the current scope (record or subprogram) from
5829 -- the stack to add the new declarations to the enclosing scope.
5831 Scope_Stack.Decrement_Last;
5832 Analyze (Decl);
5833 Set_Is_Itype (Anon);
5834 Scope_Stack.Append (Curr_Scope);
5835 end if;
5837 Set_Ekind (Anon, E_Anonymous_Access_Protected_Subprogram_Type);
5838 Set_Can_Use_Internal_Rep (Anon, not Always_Compatible_Rep_On_Target);
5839 return Anon;
5840 end Replace_Anonymous_Access_To_Protected_Subprogram;
5842 -------------------------------
5843 -- Build_Derived_Access_Type --
5844 -------------------------------
5846 procedure Build_Derived_Access_Type
5847 (N : Node_Id;
5848 Parent_Type : Entity_Id;
5849 Derived_Type : Entity_Id)
5851 S : constant Node_Id := Subtype_Indication (Type_Definition (N));
5853 Desig_Type : Entity_Id;
5854 Discr : Entity_Id;
5855 Discr_Con_Elist : Elist_Id;
5856 Discr_Con_El : Elmt_Id;
5857 Subt : Entity_Id;
5859 begin
5860 -- Set the designated type so it is available in case this is an access
5861 -- to a self-referential type, e.g. a standard list type with a next
5862 -- pointer. Will be reset after subtype is built.
5864 Set_Directly_Designated_Type
5865 (Derived_Type, Designated_Type (Parent_Type));
5867 Subt := Process_Subtype (S, N);
5869 if Nkind (S) /= N_Subtype_Indication
5870 and then Subt /= Base_Type (Subt)
5871 then
5872 Set_Ekind (Derived_Type, E_Access_Subtype);
5873 end if;
5875 if Ekind (Derived_Type) = E_Access_Subtype then
5876 declare
5877 Pbase : constant Entity_Id := Base_Type (Parent_Type);
5878 Ibase : constant Entity_Id :=
5879 Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
5880 Svg_Chars : constant Name_Id := Chars (Ibase);
5881 Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
5883 begin
5884 Copy_Node (Pbase, Ibase);
5886 Set_Chars (Ibase, Svg_Chars);
5887 Set_Next_Entity (Ibase, Svg_Next_E);
5888 Set_Sloc (Ibase, Sloc (Derived_Type));
5889 Set_Scope (Ibase, Scope (Derived_Type));
5890 Set_Freeze_Node (Ibase, Empty);
5891 Set_Is_Frozen (Ibase, False);
5892 Set_Comes_From_Source (Ibase, False);
5893 Set_Is_First_Subtype (Ibase, False);
5895 Set_Etype (Ibase, Pbase);
5896 Set_Etype (Derived_Type, Ibase);
5897 end;
5898 end if;
5900 Set_Directly_Designated_Type
5901 (Derived_Type, Designated_Type (Subt));
5903 Set_Is_Constrained (Derived_Type, Is_Constrained (Subt));
5904 Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
5905 Set_Size_Info (Derived_Type, Parent_Type);
5906 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
5907 Set_Depends_On_Private (Derived_Type,
5908 Has_Private_Component (Derived_Type));
5909 Conditional_Delay (Derived_Type, Subt);
5911 -- Ada 2005 (AI-231): Set the null-exclusion attribute, and verify
5912 -- that it is not redundant.
5914 if Null_Exclusion_Present (Type_Definition (N)) then
5915 Set_Can_Never_Be_Null (Derived_Type);
5917 -- What is with the "AND THEN FALSE" here ???
5919 if Can_Never_Be_Null (Parent_Type)
5920 and then False
5921 then
5922 Error_Msg_NE
5923 ("`NOT NULL` not allowed (& already excludes null)",
5924 N, Parent_Type);
5925 end if;
5927 elsif Can_Never_Be_Null (Parent_Type) then
5928 Set_Can_Never_Be_Null (Derived_Type);
5929 end if;
5931 -- Note: we do not copy the Storage_Size_Variable, since we always go to
5932 -- the root type for this information.
5934 -- Apply range checks to discriminants for derived record case
5935 -- ??? THIS CODE SHOULD NOT BE HERE REALLY.
5937 Desig_Type := Designated_Type (Derived_Type);
5938 if Is_Composite_Type (Desig_Type)
5939 and then (not Is_Array_Type (Desig_Type))
5940 and then Has_Discriminants (Desig_Type)
5941 and then Base_Type (Desig_Type) /= Desig_Type
5942 then
5943 Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
5944 Discr_Con_El := First_Elmt (Discr_Con_Elist);
5946 Discr := First_Discriminant (Base_Type (Desig_Type));
5947 while Present (Discr_Con_El) loop
5948 Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
5949 Next_Elmt (Discr_Con_El);
5950 Next_Discriminant (Discr);
5951 end loop;
5952 end if;
5953 end Build_Derived_Access_Type;
5955 ------------------------------
5956 -- Build_Derived_Array_Type --
5957 ------------------------------
5959 procedure Build_Derived_Array_Type
5960 (N : Node_Id;
5961 Parent_Type : Entity_Id;
5962 Derived_Type : Entity_Id)
5964 Loc : constant Source_Ptr := Sloc (N);
5965 Tdef : constant Node_Id := Type_Definition (N);
5966 Indic : constant Node_Id := Subtype_Indication (Tdef);
5967 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
5968 Implicit_Base : Entity_Id;
5969 New_Indic : Node_Id;
5971 procedure Make_Implicit_Base;
5972 -- If the parent subtype is constrained, the derived type is a subtype
5973 -- of an implicit base type derived from the parent base.
5975 ------------------------
5976 -- Make_Implicit_Base --
5977 ------------------------
5979 procedure Make_Implicit_Base is
5980 begin
5981 Implicit_Base :=
5982 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
5984 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
5985 Set_Etype (Implicit_Base, Parent_Base);
5987 Copy_Array_Subtype_Attributes (Implicit_Base, Parent_Base);
5988 Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
5990 Set_Has_Delayed_Freeze (Implicit_Base, True);
5992 -- Inherit the "ghostness" from the parent base type
5994 if Is_Ghost_Entity (Parent_Base) or else Ghost_Mode > None then
5995 Set_Is_Ghost_Entity (Implicit_Base);
5996 end if;
5997 end Make_Implicit_Base;
5999 -- Start of processing for Build_Derived_Array_Type
6001 begin
6002 if not Is_Constrained (Parent_Type) then
6003 if Nkind (Indic) /= N_Subtype_Indication then
6004 Set_Ekind (Derived_Type, E_Array_Type);
6006 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6007 Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
6009 Set_Has_Delayed_Freeze (Derived_Type, True);
6011 else
6012 Make_Implicit_Base;
6013 Set_Etype (Derived_Type, Implicit_Base);
6015 New_Indic :=
6016 Make_Subtype_Declaration (Loc,
6017 Defining_Identifier => Derived_Type,
6018 Subtype_Indication =>
6019 Make_Subtype_Indication (Loc,
6020 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
6021 Constraint => Constraint (Indic)));
6023 Rewrite (N, New_Indic);
6024 Analyze (N);
6025 end if;
6027 else
6028 if Nkind (Indic) /= N_Subtype_Indication then
6029 Make_Implicit_Base;
6031 Set_Ekind (Derived_Type, Ekind (Parent_Type));
6032 Set_Etype (Derived_Type, Implicit_Base);
6033 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6035 else
6036 Error_Msg_N ("illegal constraint on constrained type", Indic);
6037 end if;
6038 end if;
6040 -- If parent type is not a derived type itself, and is declared in
6041 -- closed scope (e.g. a subprogram), then we must explicitly introduce
6042 -- the new type's concatenation operator since Derive_Subprograms
6043 -- will not inherit the parent's operator. If the parent type is
6044 -- unconstrained, the operator is of the unconstrained base type.
6046 if Number_Dimensions (Parent_Type) = 1
6047 and then not Is_Limited_Type (Parent_Type)
6048 and then not Is_Derived_Type (Parent_Type)
6049 and then not Is_Package_Or_Generic_Package
6050 (Scope (Base_Type (Parent_Type)))
6051 then
6052 if not Is_Constrained (Parent_Type)
6053 and then Is_Constrained (Derived_Type)
6054 then
6055 New_Concatenation_Op (Implicit_Base);
6056 else
6057 New_Concatenation_Op (Derived_Type);
6058 end if;
6059 end if;
6060 end Build_Derived_Array_Type;
6062 -----------------------------------
6063 -- Build_Derived_Concurrent_Type --
6064 -----------------------------------
6066 procedure Build_Derived_Concurrent_Type
6067 (N : Node_Id;
6068 Parent_Type : Entity_Id;
6069 Derived_Type : Entity_Id)
6071 Loc : constant Source_Ptr := Sloc (N);
6073 Corr_Record : constant Entity_Id := Make_Temporary (Loc, 'C');
6074 Corr_Decl : Node_Id;
6075 Corr_Decl_Needed : Boolean;
6076 -- If the derived type has fewer discriminants than its parent, the
6077 -- corresponding record is also a derived type, in order to account for
6078 -- the bound discriminants. We create a full type declaration for it in
6079 -- this case.
6081 Constraint_Present : constant Boolean :=
6082 Nkind (Subtype_Indication (Type_Definition (N))) =
6083 N_Subtype_Indication;
6085 D_Constraint : Node_Id;
6086 New_Constraint : Elist_Id;
6087 Old_Disc : Entity_Id;
6088 New_Disc : Entity_Id;
6089 New_N : Node_Id;
6091 begin
6092 Set_Stored_Constraint (Derived_Type, No_Elist);
6093 Corr_Decl_Needed := False;
6094 Old_Disc := Empty;
6096 if Present (Discriminant_Specifications (N))
6097 and then Constraint_Present
6098 then
6099 Old_Disc := First_Discriminant (Parent_Type);
6100 New_Disc := First (Discriminant_Specifications (N));
6101 while Present (New_Disc) and then Present (Old_Disc) loop
6102 Next_Discriminant (Old_Disc);
6103 Next (New_Disc);
6104 end loop;
6105 end if;
6107 if Present (Old_Disc) and then Expander_Active then
6109 -- The new type has fewer discriminants, so we need to create a new
6110 -- corresponding record, which is derived from the corresponding
6111 -- record of the parent, and has a stored constraint that captures
6112 -- the values of the discriminant constraints. The corresponding
6113 -- record is needed only if expander is active and code generation is
6114 -- enabled.
6116 -- The type declaration for the derived corresponding record has the
6117 -- same discriminant part and constraints as the current declaration.
6118 -- Copy the unanalyzed tree to build declaration.
6120 Corr_Decl_Needed := True;
6121 New_N := Copy_Separate_Tree (N);
6123 Corr_Decl :=
6124 Make_Full_Type_Declaration (Loc,
6125 Defining_Identifier => Corr_Record,
6126 Discriminant_Specifications =>
6127 Discriminant_Specifications (New_N),
6128 Type_Definition =>
6129 Make_Derived_Type_Definition (Loc,
6130 Subtype_Indication =>
6131 Make_Subtype_Indication (Loc,
6132 Subtype_Mark =>
6133 New_Occurrence_Of
6134 (Corresponding_Record_Type (Parent_Type), Loc),
6135 Constraint =>
6136 Constraint
6137 (Subtype_Indication (Type_Definition (New_N))))));
6138 end if;
6140 -- Copy Storage_Size and Relative_Deadline variables if task case
6142 if Is_Task_Type (Parent_Type) then
6143 Set_Storage_Size_Variable (Derived_Type,
6144 Storage_Size_Variable (Parent_Type));
6145 Set_Relative_Deadline_Variable (Derived_Type,
6146 Relative_Deadline_Variable (Parent_Type));
6147 end if;
6149 if Present (Discriminant_Specifications (N)) then
6150 Push_Scope (Derived_Type);
6151 Check_Or_Process_Discriminants (N, Derived_Type);
6153 if Constraint_Present then
6154 New_Constraint :=
6155 Expand_To_Stored_Constraint
6156 (Parent_Type,
6157 Build_Discriminant_Constraints
6158 (Parent_Type,
6159 Subtype_Indication (Type_Definition (N)), True));
6160 end if;
6162 End_Scope;
6164 elsif Constraint_Present then
6166 -- Build constrained subtype, copying the constraint, and derive
6167 -- from it to create a derived constrained type.
6169 declare
6170 Loc : constant Source_Ptr := Sloc (N);
6171 Anon : constant Entity_Id :=
6172 Make_Defining_Identifier (Loc,
6173 Chars => New_External_Name (Chars (Derived_Type), 'T'));
6174 Decl : Node_Id;
6176 begin
6177 Decl :=
6178 Make_Subtype_Declaration (Loc,
6179 Defining_Identifier => Anon,
6180 Subtype_Indication =>
6181 New_Copy_Tree (Subtype_Indication (Type_Definition (N))));
6182 Insert_Before (N, Decl);
6183 Analyze (Decl);
6185 Rewrite (Subtype_Indication (Type_Definition (N)),
6186 New_Occurrence_Of (Anon, Loc));
6187 Set_Analyzed (Derived_Type, False);
6188 Analyze (N);
6189 return;
6190 end;
6191 end if;
6193 -- By default, operations and private data are inherited from parent.
6194 -- However, in the presence of bound discriminants, a new corresponding
6195 -- record will be created, see below.
6197 Set_Has_Discriminants
6198 (Derived_Type, Has_Discriminants (Parent_Type));
6199 Set_Corresponding_Record_Type
6200 (Derived_Type, Corresponding_Record_Type (Parent_Type));
6202 -- Is_Constrained is set according the parent subtype, but is set to
6203 -- False if the derived type is declared with new discriminants.
6205 Set_Is_Constrained
6206 (Derived_Type,
6207 (Is_Constrained (Parent_Type) or else Constraint_Present)
6208 and then not Present (Discriminant_Specifications (N)));
6210 if Constraint_Present then
6211 if not Has_Discriminants (Parent_Type) then
6212 Error_Msg_N ("untagged parent must have discriminants", N);
6214 elsif Present (Discriminant_Specifications (N)) then
6216 -- Verify that new discriminants are used to constrain old ones
6218 D_Constraint :=
6219 First
6220 (Constraints
6221 (Constraint (Subtype_Indication (Type_Definition (N)))));
6223 Old_Disc := First_Discriminant (Parent_Type);
6225 while Present (D_Constraint) loop
6226 if Nkind (D_Constraint) /= N_Discriminant_Association then
6228 -- Positional constraint. If it is a reference to a new
6229 -- discriminant, it constrains the corresponding old one.
6231 if Nkind (D_Constraint) = N_Identifier then
6232 New_Disc := First_Discriminant (Derived_Type);
6233 while Present (New_Disc) loop
6234 exit when Chars (New_Disc) = Chars (D_Constraint);
6235 Next_Discriminant (New_Disc);
6236 end loop;
6238 if Present (New_Disc) then
6239 Set_Corresponding_Discriminant (New_Disc, Old_Disc);
6240 end if;
6241 end if;
6243 Next_Discriminant (Old_Disc);
6245 -- if this is a named constraint, search by name for the old
6246 -- discriminants constrained by the new one.
6248 elsif Nkind (Expression (D_Constraint)) = N_Identifier then
6250 -- Find new discriminant with that name
6252 New_Disc := First_Discriminant (Derived_Type);
6253 while Present (New_Disc) loop
6254 exit when
6255 Chars (New_Disc) = Chars (Expression (D_Constraint));
6256 Next_Discriminant (New_Disc);
6257 end loop;
6259 if Present (New_Disc) then
6261 -- Verify that new discriminant renames some discriminant
6262 -- of the parent type, and associate the new discriminant
6263 -- with one or more old ones that it renames.
6265 declare
6266 Selector : Node_Id;
6268 begin
6269 Selector := First (Selector_Names (D_Constraint));
6270 while Present (Selector) loop
6271 Old_Disc := First_Discriminant (Parent_Type);
6272 while Present (Old_Disc) loop
6273 exit when Chars (Old_Disc) = Chars (Selector);
6274 Next_Discriminant (Old_Disc);
6275 end loop;
6277 if Present (Old_Disc) then
6278 Set_Corresponding_Discriminant
6279 (New_Disc, Old_Disc);
6280 end if;
6282 Next (Selector);
6283 end loop;
6284 end;
6285 end if;
6286 end if;
6288 Next (D_Constraint);
6289 end loop;
6291 New_Disc := First_Discriminant (Derived_Type);
6292 while Present (New_Disc) loop
6293 if No (Corresponding_Discriminant (New_Disc)) then
6294 Error_Msg_NE
6295 ("new discriminant& must constrain old one", N, New_Disc);
6297 elsif not
6298 Subtypes_Statically_Compatible
6299 (Etype (New_Disc),
6300 Etype (Corresponding_Discriminant (New_Disc)))
6301 then
6302 Error_Msg_NE
6303 ("& not statically compatible with parent discriminant",
6304 N, New_Disc);
6305 end if;
6307 Next_Discriminant (New_Disc);
6308 end loop;
6309 end if;
6311 elsif Present (Discriminant_Specifications (N)) then
6312 Error_Msg_N
6313 ("missing discriminant constraint in untagged derivation", N);
6314 end if;
6316 -- The entity chain of the derived type includes the new discriminants
6317 -- but shares operations with the parent.
6319 if Present (Discriminant_Specifications (N)) then
6320 Old_Disc := First_Discriminant (Parent_Type);
6321 while Present (Old_Disc) loop
6322 if No (Next_Entity (Old_Disc))
6323 or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
6324 then
6325 Set_Next_Entity
6326 (Last_Entity (Derived_Type), Next_Entity (Old_Disc));
6327 exit;
6328 end if;
6330 Next_Discriminant (Old_Disc);
6331 end loop;
6333 else
6334 Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
6335 if Has_Discriminants (Parent_Type) then
6336 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
6337 Set_Discriminant_Constraint (
6338 Derived_Type, Discriminant_Constraint (Parent_Type));
6339 end if;
6340 end if;
6342 Set_Last_Entity (Derived_Type, Last_Entity (Parent_Type));
6344 Set_Has_Completion (Derived_Type);
6346 if Corr_Decl_Needed then
6347 Set_Stored_Constraint (Derived_Type, New_Constraint);
6348 Insert_After (N, Corr_Decl);
6349 Analyze (Corr_Decl);
6350 Set_Corresponding_Record_Type (Derived_Type, Corr_Record);
6351 end if;
6352 end Build_Derived_Concurrent_Type;
6354 ------------------------------------
6355 -- Build_Derived_Enumeration_Type --
6356 ------------------------------------
6358 procedure Build_Derived_Enumeration_Type
6359 (N : Node_Id;
6360 Parent_Type : Entity_Id;
6361 Derived_Type : Entity_Id)
6363 Loc : constant Source_Ptr := Sloc (N);
6364 Def : constant Node_Id := Type_Definition (N);
6365 Indic : constant Node_Id := Subtype_Indication (Def);
6366 Implicit_Base : Entity_Id;
6367 Literal : Entity_Id;
6368 New_Lit : Entity_Id;
6369 Literals_List : List_Id;
6370 Type_Decl : Node_Id;
6371 Hi, Lo : Node_Id;
6372 Rang_Expr : Node_Id;
6374 begin
6375 -- Since types Standard.Character and Standard.[Wide_]Wide_Character do
6376 -- not have explicit literals lists we need to process types derived
6377 -- from them specially. This is handled by Derived_Standard_Character.
6378 -- If the parent type is a generic type, there are no literals either,
6379 -- and we construct the same skeletal representation as for the generic
6380 -- parent type.
6382 if Is_Standard_Character_Type (Parent_Type) then
6383 Derived_Standard_Character (N, Parent_Type, Derived_Type);
6385 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
6386 declare
6387 Lo : Node_Id;
6388 Hi : Node_Id;
6390 begin
6391 if Nkind (Indic) /= N_Subtype_Indication then
6392 Lo :=
6393 Make_Attribute_Reference (Loc,
6394 Attribute_Name => Name_First,
6395 Prefix => New_Occurrence_Of (Derived_Type, Loc));
6396 Set_Etype (Lo, Derived_Type);
6398 Hi :=
6399 Make_Attribute_Reference (Loc,
6400 Attribute_Name => Name_Last,
6401 Prefix => New_Occurrence_Of (Derived_Type, Loc));
6402 Set_Etype (Hi, Derived_Type);
6404 Set_Scalar_Range (Derived_Type,
6405 Make_Range (Loc,
6406 Low_Bound => Lo,
6407 High_Bound => Hi));
6408 else
6410 -- Analyze subtype indication and verify compatibility
6411 -- with parent type.
6413 if Base_Type (Process_Subtype (Indic, N)) /=
6414 Base_Type (Parent_Type)
6415 then
6416 Error_Msg_N
6417 ("illegal constraint for formal discrete type", N);
6418 end if;
6419 end if;
6420 end;
6422 else
6423 -- If a constraint is present, analyze the bounds to catch
6424 -- premature usage of the derived literals.
6426 if Nkind (Indic) = N_Subtype_Indication
6427 and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
6428 then
6429 Analyze (Low_Bound (Range_Expression (Constraint (Indic))));
6430 Analyze (High_Bound (Range_Expression (Constraint (Indic))));
6431 end if;
6433 -- Introduce an implicit base type for the derived type even if there
6434 -- is no constraint attached to it, since this seems closer to the
6435 -- Ada semantics. Build a full type declaration tree for the derived
6436 -- type using the implicit base type as the defining identifier. The
6437 -- build a subtype declaration tree which applies the constraint (if
6438 -- any) have it replace the derived type declaration.
6440 Literal := First_Literal (Parent_Type);
6441 Literals_List := New_List;
6442 while Present (Literal)
6443 and then Ekind (Literal) = E_Enumeration_Literal
6444 loop
6445 -- Literals of the derived type have the same representation as
6446 -- those of the parent type, but this representation can be
6447 -- overridden by an explicit representation clause. Indicate
6448 -- that there is no explicit representation given yet. These
6449 -- derived literals are implicit operations of the new type,
6450 -- and can be overridden by explicit ones.
6452 if Nkind (Literal) = N_Defining_Character_Literal then
6453 New_Lit :=
6454 Make_Defining_Character_Literal (Loc, Chars (Literal));
6455 else
6456 New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
6457 end if;
6459 Set_Ekind (New_Lit, E_Enumeration_Literal);
6460 Set_Enumeration_Pos (New_Lit, Enumeration_Pos (Literal));
6461 Set_Enumeration_Rep (New_Lit, Enumeration_Rep (Literal));
6462 Set_Enumeration_Rep_Expr (New_Lit, Empty);
6463 Set_Alias (New_Lit, Literal);
6464 Set_Is_Known_Valid (New_Lit, True);
6466 Append (New_Lit, Literals_List);
6467 Next_Literal (Literal);
6468 end loop;
6470 Implicit_Base :=
6471 Make_Defining_Identifier (Sloc (Derived_Type),
6472 Chars => New_External_Name (Chars (Derived_Type), 'B'));
6474 -- Indicate the proper nature of the derived type. This must be done
6475 -- before analysis of the literals, to recognize cases when a literal
6476 -- may be hidden by a previous explicit function definition (cf.
6477 -- c83031a).
6479 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
6480 Set_Etype (Derived_Type, Implicit_Base);
6482 Type_Decl :=
6483 Make_Full_Type_Declaration (Loc,
6484 Defining_Identifier => Implicit_Base,
6485 Discriminant_Specifications => No_List,
6486 Type_Definition =>
6487 Make_Enumeration_Type_Definition (Loc, Literals_List));
6489 Mark_Rewrite_Insertion (Type_Decl);
6490 Insert_Before (N, Type_Decl);
6491 Analyze (Type_Decl);
6493 -- After the implicit base is analyzed its Etype needs to be changed
6494 -- to reflect the fact that it is derived from the parent type which
6495 -- was ignored during analysis. We also set the size at this point.
6497 Set_Etype (Implicit_Base, Parent_Type);
6499 Set_Size_Info (Implicit_Base, Parent_Type);
6500 Set_RM_Size (Implicit_Base, RM_Size (Parent_Type));
6501 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
6503 -- Copy other flags from parent type
6505 Set_Has_Non_Standard_Rep
6506 (Implicit_Base, Has_Non_Standard_Rep
6507 (Parent_Type));
6508 Set_Has_Pragma_Ordered
6509 (Implicit_Base, Has_Pragma_Ordered
6510 (Parent_Type));
6511 Set_Has_Delayed_Freeze (Implicit_Base);
6513 -- Process the subtype indication including a validation check on the
6514 -- constraint, if any. If a constraint is given, its bounds must be
6515 -- implicitly converted to the new type.
6517 if Nkind (Indic) = N_Subtype_Indication then
6518 declare
6519 R : constant Node_Id :=
6520 Range_Expression (Constraint (Indic));
6522 begin
6523 if Nkind (R) = N_Range then
6524 Hi := Build_Scalar_Bound
6525 (High_Bound (R), Parent_Type, Implicit_Base);
6526 Lo := Build_Scalar_Bound
6527 (Low_Bound (R), Parent_Type, Implicit_Base);
6529 else
6530 -- Constraint is a Range attribute. Replace with explicit
6531 -- mention of the bounds of the prefix, which must be a
6532 -- subtype.
6534 Analyze (Prefix (R));
6535 Hi :=
6536 Convert_To (Implicit_Base,
6537 Make_Attribute_Reference (Loc,
6538 Attribute_Name => Name_Last,
6539 Prefix =>
6540 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
6542 Lo :=
6543 Convert_To (Implicit_Base,
6544 Make_Attribute_Reference (Loc,
6545 Attribute_Name => Name_First,
6546 Prefix =>
6547 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
6548 end if;
6549 end;
6551 else
6552 Hi :=
6553 Build_Scalar_Bound
6554 (Type_High_Bound (Parent_Type),
6555 Parent_Type, Implicit_Base);
6556 Lo :=
6557 Build_Scalar_Bound
6558 (Type_Low_Bound (Parent_Type),
6559 Parent_Type, Implicit_Base);
6560 end if;
6562 Rang_Expr :=
6563 Make_Range (Loc,
6564 Low_Bound => Lo,
6565 High_Bound => Hi);
6567 -- If we constructed a default range for the case where no range
6568 -- was given, then the expressions in the range must not freeze
6569 -- since they do not correspond to expressions in the source.
6571 if Nkind (Indic) /= N_Subtype_Indication then
6572 Set_Must_Not_Freeze (Lo);
6573 Set_Must_Not_Freeze (Hi);
6574 Set_Must_Not_Freeze (Rang_Expr);
6575 end if;
6577 Rewrite (N,
6578 Make_Subtype_Declaration (Loc,
6579 Defining_Identifier => Derived_Type,
6580 Subtype_Indication =>
6581 Make_Subtype_Indication (Loc,
6582 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
6583 Constraint =>
6584 Make_Range_Constraint (Loc,
6585 Range_Expression => Rang_Expr))));
6587 Analyze (N);
6589 -- Propagate the aspects from the original type declaration to the
6590 -- declaration of the implicit base.
6592 Move_Aspects (From => Original_Node (N), To => Type_Decl);
6594 -- Apply a range check. Since this range expression doesn't have an
6595 -- Etype, we have to specifically pass the Source_Typ parameter. Is
6596 -- this right???
6598 if Nkind (Indic) = N_Subtype_Indication then
6599 Apply_Range_Check
6600 (Range_Expression (Constraint (Indic)), Parent_Type,
6601 Source_Typ => Entity (Subtype_Mark (Indic)));
6602 end if;
6603 end if;
6604 end Build_Derived_Enumeration_Type;
6606 --------------------------------
6607 -- Build_Derived_Numeric_Type --
6608 --------------------------------
6610 procedure Build_Derived_Numeric_Type
6611 (N : Node_Id;
6612 Parent_Type : Entity_Id;
6613 Derived_Type : Entity_Id)
6615 Loc : constant Source_Ptr := Sloc (N);
6616 Tdef : constant Node_Id := Type_Definition (N);
6617 Indic : constant Node_Id := Subtype_Indication (Tdef);
6618 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
6619 No_Constraint : constant Boolean := Nkind (Indic) /=
6620 N_Subtype_Indication;
6621 Implicit_Base : Entity_Id;
6623 Lo : Node_Id;
6624 Hi : Node_Id;
6626 begin
6627 -- Process the subtype indication including a validation check on
6628 -- the constraint if any.
6630 Discard_Node (Process_Subtype (Indic, N));
6632 -- Introduce an implicit base type for the derived type even if there
6633 -- is no constraint attached to it, since this seems closer to the Ada
6634 -- semantics.
6636 Implicit_Base :=
6637 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
6639 Set_Etype (Implicit_Base, Parent_Base);
6640 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
6641 Set_Size_Info (Implicit_Base, Parent_Base);
6642 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
6643 Set_Parent (Implicit_Base, Parent (Derived_Type));
6644 Set_Is_Known_Valid (Implicit_Base, Is_Known_Valid (Parent_Base));
6646 -- Set RM Size for discrete type or decimal fixed-point type
6647 -- Ordinary fixed-point is excluded, why???
6649 if Is_Discrete_Type (Parent_Base)
6650 or else Is_Decimal_Fixed_Point_Type (Parent_Base)
6651 then
6652 Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
6653 end if;
6655 Set_Has_Delayed_Freeze (Implicit_Base);
6657 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
6658 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
6660 Set_Scalar_Range (Implicit_Base,
6661 Make_Range (Loc,
6662 Low_Bound => Lo,
6663 High_Bound => Hi));
6665 if Has_Infinities (Parent_Base) then
6666 Set_Includes_Infinities (Scalar_Range (Implicit_Base));
6667 end if;
6669 -- The Derived_Type, which is the entity of the declaration, is a
6670 -- subtype of the implicit base. Its Ekind is a subtype, even in the
6671 -- absence of an explicit constraint.
6673 Set_Etype (Derived_Type, Implicit_Base);
6675 -- If we did not have a constraint, then the Ekind is set from the
6676 -- parent type (otherwise Process_Subtype has set the bounds)
6678 if No_Constraint then
6679 Set_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
6680 end if;
6682 -- If we did not have a range constraint, then set the range from the
6683 -- parent type. Otherwise, the Process_Subtype call has set the bounds.
6685 if No_Constraint or else not Has_Range_Constraint (Indic) then
6686 Set_Scalar_Range (Derived_Type,
6687 Make_Range (Loc,
6688 Low_Bound => New_Copy_Tree (Type_Low_Bound (Parent_Type)),
6689 High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
6690 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
6692 if Has_Infinities (Parent_Type) then
6693 Set_Includes_Infinities (Scalar_Range (Derived_Type));
6694 end if;
6696 Set_Is_Known_Valid (Derived_Type, Is_Known_Valid (Parent_Type));
6697 end if;
6699 Set_Is_Descendent_Of_Address (Derived_Type,
6700 Is_Descendent_Of_Address (Parent_Type));
6701 Set_Is_Descendent_Of_Address (Implicit_Base,
6702 Is_Descendent_Of_Address (Parent_Type));
6704 -- Set remaining type-specific fields, depending on numeric type
6706 if Is_Modular_Integer_Type (Parent_Type) then
6707 Set_Modulus (Implicit_Base, Modulus (Parent_Base));
6709 Set_Non_Binary_Modulus
6710 (Implicit_Base, Non_Binary_Modulus (Parent_Base));
6712 Set_Is_Known_Valid
6713 (Implicit_Base, Is_Known_Valid (Parent_Base));
6715 elsif Is_Floating_Point_Type (Parent_Type) then
6717 -- Digits of base type is always copied from the digits value of
6718 -- the parent base type, but the digits of the derived type will
6719 -- already have been set if there was a constraint present.
6721 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
6722 Set_Float_Rep (Implicit_Base, Float_Rep (Parent_Base));
6724 if No_Constraint then
6725 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
6726 end if;
6728 elsif Is_Fixed_Point_Type (Parent_Type) then
6730 -- Small of base type and derived type are always copied from the
6731 -- parent base type, since smalls never change. The delta of the
6732 -- base type is also copied from the parent base type. However the
6733 -- delta of the derived type will have been set already if a
6734 -- constraint was present.
6736 Set_Small_Value (Derived_Type, Small_Value (Parent_Base));
6737 Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
6738 Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
6740 if No_Constraint then
6741 Set_Delta_Value (Derived_Type, Delta_Value (Parent_Type));
6742 end if;
6744 -- The scale and machine radix in the decimal case are always
6745 -- copied from the parent base type.
6747 if Is_Decimal_Fixed_Point_Type (Parent_Type) then
6748 Set_Scale_Value (Derived_Type, Scale_Value (Parent_Base));
6749 Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
6751 Set_Machine_Radix_10
6752 (Derived_Type, Machine_Radix_10 (Parent_Base));
6753 Set_Machine_Radix_10
6754 (Implicit_Base, Machine_Radix_10 (Parent_Base));
6756 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
6758 if No_Constraint then
6759 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
6761 else
6762 -- the analysis of the subtype_indication sets the
6763 -- digits value of the derived type.
6765 null;
6766 end if;
6767 end if;
6768 end if;
6770 if Is_Integer_Type (Parent_Type) then
6771 Set_Has_Shift_Operator
6772 (Implicit_Base, Has_Shift_Operator (Parent_Type));
6773 end if;
6775 -- The type of the bounds is that of the parent type, and they
6776 -- must be converted to the derived type.
6778 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
6780 -- The implicit_base should be frozen when the derived type is frozen,
6781 -- but note that it is used in the conversions of the bounds. For fixed
6782 -- types we delay the determination of the bounds until the proper
6783 -- freezing point. For other numeric types this is rejected by GCC, for
6784 -- reasons that are currently unclear (???), so we choose to freeze the
6785 -- implicit base now. In the case of integers and floating point types
6786 -- this is harmless because subsequent representation clauses cannot
6787 -- affect anything, but it is still baffling that we cannot use the
6788 -- same mechanism for all derived numeric types.
6790 -- There is a further complication: actually some representation
6791 -- clauses can affect the implicit base type. For example, attribute
6792 -- definition clauses for stream-oriented attributes need to set the
6793 -- corresponding TSS entries on the base type, and this normally
6794 -- cannot be done after the base type is frozen, so the circuitry in
6795 -- Sem_Ch13.New_Stream_Subprogram must account for this possibility
6796 -- and not use Set_TSS in this case.
6798 -- There are also consequences for the case of delayed representation
6799 -- aspects for some cases. For example, a Size aspect is delayed and
6800 -- should not be evaluated to the freeze point. This early freezing
6801 -- means that the size attribute evaluation happens too early???
6803 if Is_Fixed_Point_Type (Parent_Type) then
6804 Conditional_Delay (Implicit_Base, Parent_Type);
6805 else
6806 Freeze_Before (N, Implicit_Base);
6807 end if;
6808 end Build_Derived_Numeric_Type;
6810 --------------------------------
6811 -- Build_Derived_Private_Type --
6812 --------------------------------
6814 procedure Build_Derived_Private_Type
6815 (N : Node_Id;
6816 Parent_Type : Entity_Id;
6817 Derived_Type : Entity_Id;
6818 Is_Completion : Boolean;
6819 Derive_Subps : Boolean := True)
6821 Loc : constant Source_Ptr := Sloc (N);
6822 Par_Base : constant Entity_Id := Base_Type (Parent_Type);
6823 Par_Scope : constant Entity_Id := Scope (Par_Base);
6824 Full_N : constant Node_Id := New_Copy_Tree (N);
6825 Full_Der : Entity_Id := New_Copy (Derived_Type);
6826 Full_P : Entity_Id;
6828 procedure Build_Full_Derivation;
6829 -- Build full derivation, i.e. derive from the full view
6831 procedure Copy_And_Build;
6832 -- Copy derived type declaration, replace parent with its full view,
6833 -- and build derivation
6835 ---------------------------
6836 -- Build_Full_Derivation --
6837 ---------------------------
6839 procedure Build_Full_Derivation is
6840 begin
6841 -- If parent scope is not open, install the declarations
6843 if not In_Open_Scopes (Par_Scope) then
6844 Install_Private_Declarations (Par_Scope);
6845 Install_Visible_Declarations (Par_Scope);
6846 Copy_And_Build;
6847 Uninstall_Declarations (Par_Scope);
6849 -- If parent scope is open and in another unit, and parent has a
6850 -- completion, then the derivation is taking place in the visible
6851 -- part of a child unit. In that case retrieve the full view of
6852 -- the parent momentarily.
6854 elsif not In_Same_Source_Unit (N, Parent_Type) then
6855 Full_P := Full_View (Parent_Type);
6856 Exchange_Declarations (Parent_Type);
6857 Copy_And_Build;
6858 Exchange_Declarations (Full_P);
6860 -- Otherwise it is a local derivation
6862 else
6863 Copy_And_Build;
6864 end if;
6865 end Build_Full_Derivation;
6867 --------------------
6868 -- Copy_And_Build --
6869 --------------------
6871 procedure Copy_And_Build is
6872 Full_Parent : Entity_Id := Parent_Type;
6874 begin
6875 -- If the parent is itself derived from another private type,
6876 -- installing the private declarations has not affected its
6877 -- privacy status, so use its own full view explicitly.
6879 if Is_Private_Type (Full_Parent)
6880 and then Present (Full_View (Full_Parent))
6881 then
6882 Full_Parent := Full_View (Full_Parent);
6883 end if;
6885 -- And its underlying full view if necessary
6887 if Is_Private_Type (Full_Parent)
6888 and then Present (Underlying_Full_View (Full_Parent))
6889 then
6890 Full_Parent := Underlying_Full_View (Full_Parent);
6891 end if;
6893 -- For record, access and most enumeration types, derivation from
6894 -- the full view requires a fully-fledged declaration. In the other
6895 -- cases, just use an itype.
6897 if Ekind (Full_Parent) in Record_Kind
6898 or else Ekind (Full_Parent) in Access_Kind
6899 or else
6900 (Ekind (Full_Parent) in Enumeration_Kind
6901 and then not Is_Standard_Character_Type (Full_Parent)
6902 and then not Is_Generic_Type (Root_Type (Full_Parent)))
6903 then
6904 -- Copy and adjust declaration to provide a completion for what
6905 -- is originally a private declaration. Indicate that full view
6906 -- is internally generated.
6908 Set_Comes_From_Source (Full_N, False);
6909 Set_Comes_From_Source (Full_Der, False);
6910 Set_Parent (Full_Der, Full_N);
6911 Set_Defining_Identifier (Full_N, Full_Der);
6913 -- If there are no constraints, adjust the subtype mark
6915 if Nkind (Subtype_Indication (Type_Definition (Full_N))) /=
6916 N_Subtype_Indication
6917 then
6918 Set_Subtype_Indication
6919 (Type_Definition (Full_N),
6920 New_Occurrence_Of (Full_Parent, Sloc (Full_N)));
6921 end if;
6923 Insert_After (N, Full_N);
6925 -- Build full view of derived type from full view of parent which
6926 -- is now installed. Subprograms have been derived on the partial
6927 -- view, the completion does not derive them anew.
6929 if Ekind (Full_Parent) in Record_Kind then
6931 -- If parent type is tagged, the completion inherits the proper
6932 -- primitive operations.
6934 if Is_Tagged_Type (Parent_Type) then
6935 Build_Derived_Record_Type
6936 (Full_N, Full_Parent, Full_Der, Derive_Subps);
6937 else
6938 Build_Derived_Record_Type
6939 (Full_N, Full_Parent, Full_Der, Derive_Subps => False);
6940 end if;
6942 else
6943 Build_Derived_Type
6944 (Full_N, Full_Parent, Full_Der,
6945 Is_Completion => False, Derive_Subps => False);
6946 end if;
6948 -- The full declaration has been introduced into the tree and
6949 -- processed in the step above. It should not be analyzed again
6950 -- (when encountered later in the current list of declarations)
6951 -- to prevent spurious name conflicts. The full entity remains
6952 -- invisible.
6954 Set_Analyzed (Full_N);
6956 else
6957 Full_Der :=
6958 Make_Defining_Identifier (Sloc (Derived_Type),
6959 Chars => Chars (Derived_Type));
6960 Set_Is_Itype (Full_Der);
6961 Set_Associated_Node_For_Itype (Full_Der, N);
6962 Set_Parent (Full_Der, N);
6963 Build_Derived_Type
6964 (N, Full_Parent, Full_Der,
6965 Is_Completion => False, Derive_Subps => False);
6966 end if;
6968 Set_Has_Private_Declaration (Full_Der);
6969 Set_Has_Private_Declaration (Derived_Type);
6971 Set_Scope (Full_Der, Scope (Derived_Type));
6972 Set_Is_First_Subtype (Full_Der, Is_First_Subtype (Derived_Type));
6973 Set_Has_Size_Clause (Full_Der, False);
6974 Set_Has_Alignment_Clause (Full_Der, False);
6975 Set_Has_Delayed_Freeze (Full_Der);
6976 Set_Is_Frozen (Full_Der, False);
6977 Set_Freeze_Node (Full_Der, Empty);
6978 Set_Depends_On_Private (Full_Der, Has_Private_Component (Full_Der));
6979 Set_Is_Public (Full_Der, Is_Public (Derived_Type));
6981 -- The convention on the base type may be set in the private part
6982 -- and not propagated to the subtype until later, so we obtain the
6983 -- convention from the base type of the parent.
6985 Set_Convention (Full_Der, Convention (Base_Type (Full_Parent)));
6986 end Copy_And_Build;
6988 -- Start of processing for Build_Derived_Private_Type
6990 begin
6991 if Is_Tagged_Type (Parent_Type) then
6992 Full_P := Full_View (Parent_Type);
6994 -- A type extension of a type with unknown discriminants is an
6995 -- indefinite type that the back-end cannot handle directly.
6996 -- We treat it as a private type, and build a completion that is
6997 -- derived from the full view of the parent, and hopefully has
6998 -- known discriminants.
7000 -- If the full view of the parent type has an underlying record view,
7001 -- use it to generate the underlying record view of this derived type
7002 -- (required for chains of derivations with unknown discriminants).
7004 -- Minor optimization: we avoid the generation of useless underlying
7005 -- record view entities if the private type declaration has unknown
7006 -- discriminants but its corresponding full view has no
7007 -- discriminants.
7009 if Has_Unknown_Discriminants (Parent_Type)
7010 and then Present (Full_P)
7011 and then (Has_Discriminants (Full_P)
7012 or else Present (Underlying_Record_View (Full_P)))
7013 and then not In_Open_Scopes (Par_Scope)
7014 and then Expander_Active
7015 then
7016 declare
7017 Full_Der : constant Entity_Id := Make_Temporary (Loc, 'T');
7018 New_Ext : constant Node_Id :=
7019 Copy_Separate_Tree
7020 (Record_Extension_Part (Type_Definition (N)));
7021 Decl : Node_Id;
7023 begin
7024 Build_Derived_Record_Type
7025 (N, Parent_Type, Derived_Type, Derive_Subps);
7027 -- Build anonymous completion, as a derivation from the full
7028 -- view of the parent. This is not a completion in the usual
7029 -- sense, because the current type is not private.
7031 Decl :=
7032 Make_Full_Type_Declaration (Loc,
7033 Defining_Identifier => Full_Der,
7034 Type_Definition =>
7035 Make_Derived_Type_Definition (Loc,
7036 Subtype_Indication =>
7037 New_Copy_Tree
7038 (Subtype_Indication (Type_Definition (N))),
7039 Record_Extension_Part => New_Ext));
7041 -- If the parent type has an underlying record view, use it
7042 -- here to build the new underlying record view.
7044 if Present (Underlying_Record_View (Full_P)) then
7045 pragma Assert
7046 (Nkind (Subtype_Indication (Type_Definition (Decl)))
7047 = N_Identifier);
7048 Set_Entity (Subtype_Indication (Type_Definition (Decl)),
7049 Underlying_Record_View (Full_P));
7050 end if;
7052 Install_Private_Declarations (Par_Scope);
7053 Install_Visible_Declarations (Par_Scope);
7054 Insert_Before (N, Decl);
7056 -- Mark entity as an underlying record view before analysis,
7057 -- to avoid generating the list of its primitive operations
7058 -- (which is not really required for this entity) and thus
7059 -- prevent spurious errors associated with missing overriding
7060 -- of abstract primitives (overridden only for Derived_Type).
7062 Set_Ekind (Full_Der, E_Record_Type);
7063 Set_Is_Underlying_Record_View (Full_Der);
7064 Set_Default_SSO (Full_Der);
7066 Analyze (Decl);
7068 pragma Assert (Has_Discriminants (Full_Der)
7069 and then not Has_Unknown_Discriminants (Full_Der));
7071 Uninstall_Declarations (Par_Scope);
7073 -- Freeze the underlying record view, to prevent generation of
7074 -- useless dispatching information, which is simply shared with
7075 -- the real derived type.
7077 Set_Is_Frozen (Full_Der);
7079 -- If the derived type has access discriminants, create
7080 -- references to their anonymous types now, to prevent
7081 -- back-end problems when their first use is in generated
7082 -- bodies of primitives.
7084 declare
7085 E : Entity_Id;
7087 begin
7088 E := First_Entity (Full_Der);
7090 while Present (E) loop
7091 if Ekind (E) = E_Discriminant
7092 and then Ekind (Etype (E)) = E_Anonymous_Access_Type
7093 then
7094 Build_Itype_Reference (Etype (E), Decl);
7095 end if;
7097 Next_Entity (E);
7098 end loop;
7099 end;
7101 -- Set up links between real entity and underlying record view
7103 Set_Underlying_Record_View (Derived_Type, Base_Type (Full_Der));
7104 Set_Underlying_Record_View (Base_Type (Full_Der), Derived_Type);
7105 end;
7107 -- If discriminants are known, build derived record
7109 else
7110 Build_Derived_Record_Type
7111 (N, Parent_Type, Derived_Type, Derive_Subps);
7112 end if;
7114 return;
7116 elsif Has_Discriminants (Parent_Type) then
7118 -- Build partial view of derived type from partial view of parent.
7119 -- This must be done before building the full derivation because the
7120 -- second derivation will modify the discriminants of the first and
7121 -- the discriminants are chained with the rest of the components in
7122 -- the full derivation.
7124 Build_Derived_Record_Type
7125 (N, Parent_Type, Derived_Type, Derive_Subps);
7127 -- Build the full derivation if this is not the anonymous derived
7128 -- base type created by Build_Derived_Record_Type in the constrained
7129 -- case (see point 5. of its head comment) since we build it for the
7130 -- derived subtype. And skip it for protected types altogether, as
7131 -- gigi does not use these types directly.
7133 if Present (Full_View (Parent_Type))
7134 and then not Is_Itype (Derived_Type)
7135 and then not (Ekind (Full_View (Parent_Type)) in Protected_Kind)
7136 then
7137 declare
7138 Der_Base : constant Entity_Id := Base_Type (Derived_Type);
7139 Discr : Entity_Id;
7140 Last_Discr : Entity_Id;
7142 begin
7143 -- If this is not a completion, construct the implicit full
7144 -- view by deriving from the full view of the parent type.
7145 -- But if this is a completion, the derived private type
7146 -- being built is a full view and the full derivation can
7147 -- only be its underlying full view.
7149 Build_Full_Derivation;
7151 if not Is_Completion then
7152 Set_Full_View (Derived_Type, Full_Der);
7153 else
7154 Set_Underlying_Full_View (Derived_Type, Full_Der);
7155 end if;
7157 if not Is_Base_Type (Derived_Type) then
7158 Set_Full_View (Der_Base, Base_Type (Full_Der));
7159 end if;
7161 -- Copy the discriminant list from full view to the partial
7162 -- view (base type and its subtype). Gigi requires that the
7163 -- partial and full views have the same discriminants.
7165 -- Note that since the partial view points to discriminants
7166 -- in the full view, their scope will be that of the full
7167 -- view. This might cause some front end problems and need
7168 -- adjustment???
7170 Discr := First_Discriminant (Base_Type (Full_Der));
7171 Set_First_Entity (Der_Base, Discr);
7173 loop
7174 Last_Discr := Discr;
7175 Next_Discriminant (Discr);
7176 exit when No (Discr);
7177 end loop;
7179 Set_Last_Entity (Der_Base, Last_Discr);
7180 Set_First_Entity (Derived_Type, First_Entity (Der_Base));
7181 Set_Last_Entity (Derived_Type, Last_Entity (Der_Base));
7183 Set_Stored_Constraint
7184 (Full_Der, Stored_Constraint (Derived_Type));
7185 end;
7186 end if;
7188 elsif Present (Full_View (Parent_Type))
7189 and then Has_Discriminants (Full_View (Parent_Type))
7190 then
7191 if Has_Unknown_Discriminants (Parent_Type)
7192 and then Nkind (Subtype_Indication (Type_Definition (N))) =
7193 N_Subtype_Indication
7194 then
7195 Error_Msg_N
7196 ("cannot constrain type with unknown discriminants",
7197 Subtype_Indication (Type_Definition (N)));
7198 return;
7199 end if;
7201 -- If this is not a completion, construct the implicit full view by
7202 -- deriving from the full view of the parent type. But if this is a
7203 -- completion, the derived private type being built is a full view
7204 -- and the full derivation can only be its underlying full view.
7206 Build_Full_Derivation;
7208 if not Is_Completion then
7209 Set_Full_View (Derived_Type, Full_Der);
7210 else
7211 Set_Underlying_Full_View (Derived_Type, Full_Der);
7212 end if;
7214 -- In any case, the primitive operations are inherited from the
7215 -- parent type, not from the internal full view.
7217 Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
7219 if Derive_Subps then
7220 Derive_Subprograms (Parent_Type, Derived_Type);
7221 end if;
7223 Set_Stored_Constraint (Derived_Type, No_Elist);
7224 Set_Is_Constrained
7225 (Derived_Type, Is_Constrained (Full_View (Parent_Type)));
7227 else
7228 -- Untagged type, No discriminants on either view
7230 if Nkind (Subtype_Indication (Type_Definition (N))) =
7231 N_Subtype_Indication
7232 then
7233 Error_Msg_N
7234 ("illegal constraint on type without discriminants", N);
7235 end if;
7237 if Present (Discriminant_Specifications (N))
7238 and then Present (Full_View (Parent_Type))
7239 and then not Is_Tagged_Type (Full_View (Parent_Type))
7240 then
7241 Error_Msg_N ("cannot add discriminants to untagged type", N);
7242 end if;
7244 Set_Stored_Constraint (Derived_Type, No_Elist);
7245 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7246 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
7247 Set_Has_Controlled_Component
7248 (Derived_Type, Has_Controlled_Component
7249 (Parent_Type));
7251 -- Direct controlled types do not inherit Finalize_Storage_Only flag
7253 if not Is_Controlled (Parent_Type) then
7254 Set_Finalize_Storage_Only
7255 (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
7256 end if;
7258 -- If this is not a completion, construct the implicit full view by
7259 -- deriving from the full view of the parent type.
7261 -- ??? If the parent is untagged private and its completion is
7262 -- tagged, this mechanism will not work because we cannot derive from
7263 -- the tagged full view unless we have an extension.
7265 if Present (Full_View (Parent_Type))
7266 and then not Is_Tagged_Type (Full_View (Parent_Type))
7267 and then not Is_Completion
7268 then
7269 Build_Full_Derivation;
7270 Set_Full_View (Derived_Type, Full_Der);
7271 end if;
7272 end if;
7274 Set_Has_Unknown_Discriminants (Derived_Type,
7275 Has_Unknown_Discriminants (Parent_Type));
7277 if Is_Private_Type (Derived_Type) then
7278 Set_Private_Dependents (Derived_Type, New_Elmt_List);
7279 end if;
7281 -- If the parent base type is in scope, add the derived type to its
7282 -- list of private dependents, because its full view may become
7283 -- visible subsequently (in a nested private part, a body, or in a
7284 -- further child unit).
7286 if Is_Private_Type (Par_Base) and then In_Open_Scopes (Par_Scope) then
7287 Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
7289 -- Check for unusual case where a type completed by a private
7290 -- derivation occurs within a package nested in a child unit, and
7291 -- the parent is declared in an ancestor.
7293 if Is_Child_Unit (Scope (Current_Scope))
7294 and then Is_Completion
7295 and then In_Private_Part (Current_Scope)
7296 and then Scope (Parent_Type) /= Current_Scope
7298 -- Note that if the parent has a completion in the private part,
7299 -- (which is itself a derivation from some other private type)
7300 -- it is that completion that is visible, there is no full view
7301 -- available, and no special processing is needed.
7303 and then Present (Full_View (Parent_Type))
7304 then
7305 -- In this case, the full view of the parent type will become
7306 -- visible in the body of the enclosing child, and only then will
7307 -- the current type be possibly non-private. Build an underlying
7308 -- full view that will be installed when the enclosing child body
7309 -- is compiled.
7311 if Present (Underlying_Full_View (Derived_Type)) then
7312 Full_Der := Underlying_Full_View (Derived_Type);
7313 else
7314 Build_Full_Derivation;
7315 Set_Underlying_Full_View (Derived_Type, Full_Der);
7316 end if;
7318 -- The full view will be used to swap entities on entry/exit to
7319 -- the body, and must appear in the entity list for the package.
7321 Append_Entity (Full_Der, Scope (Derived_Type));
7322 end if;
7323 end if;
7324 end Build_Derived_Private_Type;
7326 -------------------------------
7327 -- Build_Derived_Record_Type --
7328 -------------------------------
7330 -- 1. INTRODUCTION
7332 -- Ideally we would like to use the same model of type derivation for
7333 -- tagged and untagged record types. Unfortunately this is not quite
7334 -- possible because the semantics of representation clauses is different
7335 -- for tagged and untagged records under inheritance. Consider the
7336 -- following:
7338 -- type R (...) is [tagged] record ... end record;
7339 -- type T (...) is new R (...) [with ...];
7341 -- The representation clauses for T can specify a completely different
7342 -- record layout from R's. Hence the same component can be placed in two
7343 -- very different positions in objects of type T and R. If R and T are
7344 -- tagged types, representation clauses for T can only specify the layout
7345 -- of non inherited components, thus components that are common in R and T
7346 -- have the same position in objects of type R and T.
7348 -- This has two implications. The first is that the entire tree for R's
7349 -- declaration needs to be copied for T in the untagged case, so that T
7350 -- can be viewed as a record type of its own with its own representation
7351 -- clauses. The second implication is the way we handle discriminants.
7352 -- Specifically, in the untagged case we need a way to communicate to Gigi
7353 -- what are the real discriminants in the record, while for the semantics
7354 -- we need to consider those introduced by the user to rename the
7355 -- discriminants in the parent type. This is handled by introducing the
7356 -- notion of stored discriminants. See below for more.
7358 -- Fortunately the way regular components are inherited can be handled in
7359 -- the same way in tagged and untagged types.
7361 -- To complicate things a bit more the private view of a private extension
7362 -- cannot be handled in the same way as the full view (for one thing the
7363 -- semantic rules are somewhat different). We will explain what differs
7364 -- below.
7366 -- 2. DISCRIMINANTS UNDER INHERITANCE
7368 -- The semantic rules governing the discriminants of derived types are
7369 -- quite subtle.
7371 -- type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
7372 -- [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
7374 -- If parent type has discriminants, then the discriminants that are
7375 -- declared in the derived type are [3.4 (11)]:
7377 -- o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
7378 -- there is one;
7380 -- o Otherwise, each discriminant of the parent type (implicitly declared
7381 -- in the same order with the same specifications). In this case, the
7382 -- discriminants are said to be "inherited", or if unknown in the parent
7383 -- are also unknown in the derived type.
7385 -- Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
7387 -- o The parent subtype must be constrained;
7389 -- o If the parent type is not a tagged type, then each discriminant of
7390 -- the derived type must be used in the constraint defining a parent
7391 -- subtype. [Implementation note: This ensures that the new discriminant
7392 -- can share storage with an existing discriminant.]
7394 -- For the derived type each discriminant of the parent type is either
7395 -- inherited, constrained to equal some new discriminant of the derived
7396 -- type, or constrained to the value of an expression.
7398 -- When inherited or constrained to equal some new discriminant, the
7399 -- parent discriminant and the discriminant of the derived type are said
7400 -- to "correspond".
7402 -- If a discriminant of the parent type is constrained to a specific value
7403 -- in the derived type definition, then the discriminant is said to be
7404 -- "specified" by that derived type definition.
7406 -- 3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
7408 -- We have spoken about stored discriminants in point 1 (introduction)
7409 -- above. There are two sort of stored discriminants: implicit and
7410 -- explicit. As long as the derived type inherits the same discriminants as
7411 -- the root record type, stored discriminants are the same as regular
7412 -- discriminants, and are said to be implicit. However, if any discriminant
7413 -- in the root type was renamed in the derived type, then the derived
7414 -- type will contain explicit stored discriminants. Explicit stored
7415 -- discriminants are discriminants in addition to the semantically visible
7416 -- discriminants defined for the derived type. Stored discriminants are
7417 -- used by Gigi to figure out what are the physical discriminants in
7418 -- objects of the derived type (see precise definition in einfo.ads).
7419 -- As an example, consider the following:
7421 -- type R (D1, D2, D3 : Int) is record ... end record;
7422 -- type T1 is new R;
7423 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
7424 -- type T3 is new T2;
7425 -- type T4 (Y : Int) is new T3 (Y, 99);
7427 -- The following table summarizes the discriminants and stored
7428 -- discriminants in R and T1 through T4.
7430 -- Type Discrim Stored Discrim Comment
7431 -- R (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in R
7432 -- T1 (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in T1
7433 -- T2 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T2
7434 -- T3 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T3
7435 -- T4 (Y) (D1, D2, D3) Girder discrims EXPLICIT in T4
7437 -- Field Corresponding_Discriminant (abbreviated CD below) allows us to
7438 -- find the corresponding discriminant in the parent type, while
7439 -- Original_Record_Component (abbreviated ORC below), the actual physical
7440 -- component that is renamed. Finally the field Is_Completely_Hidden
7441 -- (abbreviated ICH below) is set for all explicit stored discriminants
7442 -- (see einfo.ads for more info). For the above example this gives:
7444 -- Discrim CD ORC ICH
7445 -- ^^^^^^^ ^^ ^^^ ^^^
7446 -- D1 in R empty itself no
7447 -- D2 in R empty itself no
7448 -- D3 in R empty itself no
7450 -- D1 in T1 D1 in R itself no
7451 -- D2 in T1 D2 in R itself no
7452 -- D3 in T1 D3 in R itself no
7454 -- X1 in T2 D3 in T1 D3 in T2 no
7455 -- X2 in T2 D1 in T1 D1 in T2 no
7456 -- D1 in T2 empty itself yes
7457 -- D2 in T2 empty itself yes
7458 -- D3 in T2 empty itself yes
7460 -- X1 in T3 X1 in T2 D3 in T3 no
7461 -- X2 in T3 X2 in T2 D1 in T3 no
7462 -- D1 in T3 empty itself yes
7463 -- D2 in T3 empty itself yes
7464 -- D3 in T3 empty itself yes
7466 -- Y in T4 X1 in T3 D3 in T3 no
7467 -- D1 in T3 empty itself yes
7468 -- D2 in T3 empty itself yes
7469 -- D3 in T3 empty itself yes
7471 -- 4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
7473 -- Type derivation for tagged types is fairly straightforward. If no
7474 -- discriminants are specified by the derived type, these are inherited
7475 -- from the parent. No explicit stored discriminants are ever necessary.
7476 -- The only manipulation that is done to the tree is that of adding a
7477 -- _parent field with parent type and constrained to the same constraint
7478 -- specified for the parent in the derived type definition. For instance:
7480 -- type R (D1, D2, D3 : Int) is tagged record ... end record;
7481 -- type T1 is new R with null record;
7482 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
7484 -- are changed into:
7486 -- type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
7487 -- _parent : R (D1, D2, D3);
7488 -- end record;
7490 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
7491 -- _parent : T1 (X2, 88, X1);
7492 -- end record;
7494 -- The discriminants actually present in R, T1 and T2 as well as their CD,
7495 -- ORC and ICH fields are:
7497 -- Discrim CD ORC ICH
7498 -- ^^^^^^^ ^^ ^^^ ^^^
7499 -- D1 in R empty itself no
7500 -- D2 in R empty itself no
7501 -- D3 in R empty itself no
7503 -- D1 in T1 D1 in R D1 in R no
7504 -- D2 in T1 D2 in R D2 in R no
7505 -- D3 in T1 D3 in R D3 in R no
7507 -- X1 in T2 D3 in T1 D3 in R no
7508 -- X2 in T2 D1 in T1 D1 in R no
7510 -- 5. FIRST TRANSFORMATION FOR DERIVED RECORDS
7512 -- Regardless of whether we dealing with a tagged or untagged type
7513 -- we will transform all derived type declarations of the form
7515 -- type T is new R (...) [with ...];
7516 -- or
7517 -- subtype S is R (...);
7518 -- type T is new S [with ...];
7519 -- into
7520 -- type BT is new R [with ...];
7521 -- subtype T is BT (...);
7523 -- That is, the base derived type is constrained only if it has no
7524 -- discriminants. The reason for doing this is that GNAT's semantic model
7525 -- assumes that a base type with discriminants is unconstrained.
7527 -- Note that, strictly speaking, the above transformation is not always
7528 -- correct. Consider for instance the following excerpt from ACVC b34011a:
7530 -- procedure B34011A is
7531 -- type REC (D : integer := 0) is record
7532 -- I : Integer;
7533 -- end record;
7535 -- package P is
7536 -- type T6 is new Rec;
7537 -- function F return T6;
7538 -- end P;
7540 -- use P;
7541 -- package Q6 is
7542 -- type U is new T6 (Q6.F.I); -- ERROR: Q6.F.
7543 -- end Q6;
7545 -- The definition of Q6.U is illegal. However transforming Q6.U into
7547 -- type BaseU is new T6;
7548 -- subtype U is BaseU (Q6.F.I)
7550 -- turns U into a legal subtype, which is incorrect. To avoid this problem
7551 -- we always analyze the constraint (in this case (Q6.F.I)) before applying
7552 -- the transformation described above.
7554 -- There is another instance where the above transformation is incorrect.
7555 -- Consider:
7557 -- package Pack is
7558 -- type Base (D : Integer) is tagged null record;
7559 -- procedure P (X : Base);
7561 -- type Der is new Base (2) with null record;
7562 -- procedure P (X : Der);
7563 -- end Pack;
7565 -- Then the above transformation turns this into
7567 -- type Der_Base is new Base with null record;
7568 -- -- procedure P (X : Base) is implicitly inherited here
7569 -- -- as procedure P (X : Der_Base).
7571 -- subtype Der is Der_Base (2);
7572 -- procedure P (X : Der);
7573 -- -- The overriding of P (X : Der_Base) is illegal since we
7574 -- -- have a parameter conformance problem.
7576 -- To get around this problem, after having semantically processed Der_Base
7577 -- and the rewritten subtype declaration for Der, we copy Der_Base field
7578 -- Discriminant_Constraint from Der so that when parameter conformance is
7579 -- checked when P is overridden, no semantic errors are flagged.
7581 -- 6. SECOND TRANSFORMATION FOR DERIVED RECORDS
7583 -- Regardless of whether we are dealing with a tagged or untagged type
7584 -- we will transform all derived type declarations of the form
7586 -- type R (D1, .., Dn : ...) is [tagged] record ...;
7587 -- type T is new R [with ...];
7588 -- into
7589 -- type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
7591 -- The reason for such transformation is that it allows us to implement a
7592 -- very clean form of component inheritance as explained below.
7594 -- Note that this transformation is not achieved by direct tree rewriting
7595 -- and manipulation, but rather by redoing the semantic actions that the
7596 -- above transformation will entail. This is done directly in routine
7597 -- Inherit_Components.
7599 -- 7. TYPE DERIVATION AND COMPONENT INHERITANCE
7601 -- In both tagged and untagged derived types, regular non discriminant
7602 -- components are inherited in the derived type from the parent type. In
7603 -- the absence of discriminants component, inheritance is straightforward
7604 -- as components can simply be copied from the parent.
7606 -- If the parent has discriminants, inheriting components constrained with
7607 -- these discriminants requires caution. Consider the following example:
7609 -- type R (D1, D2 : Positive) is [tagged] record
7610 -- S : String (D1 .. D2);
7611 -- end record;
7613 -- type T1 is new R [with null record];
7614 -- type T2 (X : positive) is new R (1, X) [with null record];
7616 -- As explained in 6. above, T1 is rewritten as
7617 -- type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
7618 -- which makes the treatment for T1 and T2 identical.
7620 -- What we want when inheriting S, is that references to D1 and D2 in R are
7621 -- replaced with references to their correct constraints, i.e. D1 and D2 in
7622 -- T1 and 1 and X in T2. So all R's discriminant references are replaced
7623 -- with either discriminant references in the derived type or expressions.
7624 -- This replacement is achieved as follows: before inheriting R's
7625 -- components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
7626 -- created in the scope of T1 (resp. scope of T2) so that discriminants D1
7627 -- and D2 of T1 are visible (resp. discriminant X of T2 is visible).
7628 -- For T2, for instance, this has the effect of replacing String (D1 .. D2)
7629 -- by String (1 .. X).
7631 -- 8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
7633 -- We explain here the rules governing private type extensions relevant to
7634 -- type derivation. These rules are explained on the following example:
7636 -- type D [(...)] is new A [(...)] with private; <-- partial view
7637 -- type D [(...)] is new P [(...)] with null record; <-- full view
7639 -- Type A is called the ancestor subtype of the private extension.
7640 -- Type P is the parent type of the full view of the private extension. It
7641 -- must be A or a type derived from A.
7643 -- The rules concerning the discriminants of private type extensions are
7644 -- [7.3(10-13)]:
7646 -- o If a private extension inherits known discriminants from the ancestor
7647 -- subtype, then the full view must also inherit its discriminants from
7648 -- the ancestor subtype and the parent subtype of the full view must be
7649 -- constrained if and only if the ancestor subtype is constrained.
7651 -- o If a partial view has unknown discriminants, then the full view may
7652 -- define a definite or an indefinite subtype, with or without
7653 -- discriminants.
7655 -- o If a partial view has neither known nor unknown discriminants, then
7656 -- the full view must define a definite subtype.
7658 -- o If the ancestor subtype of a private extension has constrained
7659 -- discriminants, then the parent subtype of the full view must impose a
7660 -- statically matching constraint on those discriminants.
7662 -- This means that only the following forms of private extensions are
7663 -- allowed:
7665 -- type D is new A with private; <-- partial view
7666 -- type D is new P with null record; <-- full view
7668 -- If A has no discriminants than P has no discriminants, otherwise P must
7669 -- inherit A's discriminants.
7671 -- type D is new A (...) with private; <-- partial view
7672 -- type D is new P (:::) with null record; <-- full view
7674 -- P must inherit A's discriminants and (...) and (:::) must statically
7675 -- match.
7677 -- subtype A is R (...);
7678 -- type D is new A with private; <-- partial view
7679 -- type D is new P with null record; <-- full view
7681 -- P must have inherited R's discriminants and must be derived from A or
7682 -- any of its subtypes.
7684 -- type D (..) is new A with private; <-- partial view
7685 -- type D (..) is new P [(:::)] with null record; <-- full view
7687 -- No specific constraints on P's discriminants or constraint (:::).
7688 -- Note that A can be unconstrained, but the parent subtype P must either
7689 -- be constrained or (:::) must be present.
7691 -- type D (..) is new A [(...)] with private; <-- partial view
7692 -- type D (..) is new P [(:::)] with null record; <-- full view
7694 -- P's constraints on A's discriminants must statically match those
7695 -- imposed by (...).
7697 -- 9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
7699 -- The full view of a private extension is handled exactly as described
7700 -- above. The model chose for the private view of a private extension is
7701 -- the same for what concerns discriminants (i.e. they receive the same
7702 -- treatment as in the tagged case). However, the private view of the
7703 -- private extension always inherits the components of the parent base,
7704 -- without replacing any discriminant reference. Strictly speaking this is
7705 -- incorrect. However, Gigi never uses this view to generate code so this
7706 -- is a purely semantic issue. In theory, a set of transformations similar
7707 -- to those given in 5. and 6. above could be applied to private views of
7708 -- private extensions to have the same model of component inheritance as
7709 -- for non private extensions. However, this is not done because it would
7710 -- further complicate private type processing. Semantically speaking, this
7711 -- leaves us in an uncomfortable situation. As an example consider:
7713 -- package Pack is
7714 -- type R (D : integer) is tagged record
7715 -- S : String (1 .. D);
7716 -- end record;
7717 -- procedure P (X : R);
7718 -- type T is new R (1) with private;
7719 -- private
7720 -- type T is new R (1) with null record;
7721 -- end;
7723 -- This is transformed into:
7725 -- package Pack is
7726 -- type R (D : integer) is tagged record
7727 -- S : String (1 .. D);
7728 -- end record;
7729 -- procedure P (X : R);
7730 -- type T is new R (1) with private;
7731 -- private
7732 -- type BaseT is new R with null record;
7733 -- subtype T is BaseT (1);
7734 -- end;
7736 -- (strictly speaking the above is incorrect Ada)
7738 -- From the semantic standpoint the private view of private extension T
7739 -- should be flagged as constrained since one can clearly have
7741 -- Obj : T;
7743 -- in a unit withing Pack. However, when deriving subprograms for the
7744 -- private view of private extension T, T must be seen as unconstrained
7745 -- since T has discriminants (this is a constraint of the current
7746 -- subprogram derivation model). Thus, when processing the private view of
7747 -- a private extension such as T, we first mark T as unconstrained, we
7748 -- process it, we perform program derivation and just before returning from
7749 -- Build_Derived_Record_Type we mark T as constrained.
7751 -- ??? Are there are other uncomfortable cases that we will have to
7752 -- deal with.
7754 -- 10. RECORD_TYPE_WITH_PRIVATE complications
7756 -- Types that are derived from a visible record type and have a private
7757 -- extension present other peculiarities. They behave mostly like private
7758 -- types, but if they have primitive operations defined, these will not
7759 -- have the proper signatures for further inheritance, because other
7760 -- primitive operations will use the implicit base that we define for
7761 -- private derivations below. This affect subprogram inheritance (see
7762 -- Derive_Subprograms for details). We also derive the implicit base from
7763 -- the base type of the full view, so that the implicit base is a record
7764 -- type and not another private type, This avoids infinite loops.
7766 procedure Build_Derived_Record_Type
7767 (N : Node_Id;
7768 Parent_Type : Entity_Id;
7769 Derived_Type : Entity_Id;
7770 Derive_Subps : Boolean := True)
7772 Discriminant_Specs : constant Boolean :=
7773 Present (Discriminant_Specifications (N));
7774 Is_Tagged : constant Boolean := Is_Tagged_Type (Parent_Type);
7775 Loc : constant Source_Ptr := Sloc (N);
7776 Private_Extension : constant Boolean :=
7777 Nkind (N) = N_Private_Extension_Declaration;
7778 Assoc_List : Elist_Id;
7779 Constraint_Present : Boolean;
7780 Constrs : Elist_Id;
7781 Discrim : Entity_Id;
7782 Indic : Node_Id;
7783 Inherit_Discrims : Boolean := False;
7784 Last_Discrim : Entity_Id;
7785 New_Base : Entity_Id;
7786 New_Decl : Node_Id;
7787 New_Discrs : Elist_Id;
7788 New_Indic : Node_Id;
7789 Parent_Base : Entity_Id;
7790 Save_Etype : Entity_Id;
7791 Save_Discr_Constr : Elist_Id;
7792 Save_Next_Entity : Entity_Id;
7793 Type_Def : Node_Id;
7795 Discs : Elist_Id := New_Elmt_List;
7796 -- An empty Discs list means that there were no constraints in the
7797 -- subtype indication or that there was an error processing it.
7799 begin
7800 if Ekind (Parent_Type) = E_Record_Type_With_Private
7801 and then Present (Full_View (Parent_Type))
7802 and then Has_Discriminants (Parent_Type)
7803 then
7804 Parent_Base := Base_Type (Full_View (Parent_Type));
7805 else
7806 Parent_Base := Base_Type (Parent_Type);
7807 end if;
7809 -- AI05-0115 : if this is a derivation from a private type in some
7810 -- other scope that may lead to invisible components for the derived
7811 -- type, mark it accordingly.
7813 if Is_Private_Type (Parent_Type) then
7814 if Scope (Parent_Type) = Scope (Derived_Type) then
7815 null;
7817 elsif In_Open_Scopes (Scope (Parent_Type))
7818 and then In_Private_Part (Scope (Parent_Type))
7819 then
7820 null;
7822 else
7823 Set_Has_Private_Ancestor (Derived_Type);
7824 end if;
7826 else
7827 Set_Has_Private_Ancestor
7828 (Derived_Type, Has_Private_Ancestor (Parent_Type));
7829 end if;
7831 -- Before we start the previously documented transformations, here is
7832 -- little fix for size and alignment of tagged types. Normally when we
7833 -- derive type D from type P, we copy the size and alignment of P as the
7834 -- default for D, and in the absence of explicit representation clauses
7835 -- for D, the size and alignment are indeed the same as the parent.
7837 -- But this is wrong for tagged types, since fields may be added, and
7838 -- the default size may need to be larger, and the default alignment may
7839 -- need to be larger.
7841 -- We therefore reset the size and alignment fields in the tagged case.
7842 -- Note that the size and alignment will in any case be at least as
7843 -- large as the parent type (since the derived type has a copy of the
7844 -- parent type in the _parent field)
7846 -- The type is also marked as being tagged here, which is needed when
7847 -- processing components with a self-referential anonymous access type
7848 -- in the call to Check_Anonymous_Access_Components below. Note that
7849 -- this flag is also set later on for completeness.
7851 if Is_Tagged then
7852 Set_Is_Tagged_Type (Derived_Type);
7853 Init_Size_Align (Derived_Type);
7854 end if;
7856 -- STEP 0a: figure out what kind of derived type declaration we have
7858 if Private_Extension then
7859 Type_Def := N;
7860 Set_Ekind (Derived_Type, E_Record_Type_With_Private);
7861 Set_Default_SSO (Derived_Type);
7863 else
7864 Type_Def := Type_Definition (N);
7866 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
7867 -- Parent_Base can be a private type or private extension. However,
7868 -- for tagged types with an extension the newly added fields are
7869 -- visible and hence the Derived_Type is always an E_Record_Type.
7870 -- (except that the parent may have its own private fields).
7871 -- For untagged types we preserve the Ekind of the Parent_Base.
7873 if Present (Record_Extension_Part (Type_Def)) then
7874 Set_Ekind (Derived_Type, E_Record_Type);
7875 Set_Default_SSO (Derived_Type);
7877 -- Create internal access types for components with anonymous
7878 -- access types.
7880 if Ada_Version >= Ada_2005 then
7881 Check_Anonymous_Access_Components
7882 (N, Derived_Type, Derived_Type,
7883 Component_List (Record_Extension_Part (Type_Def)));
7884 end if;
7886 else
7887 Set_Ekind (Derived_Type, Ekind (Parent_Base));
7888 end if;
7889 end if;
7891 -- Indic can either be an N_Identifier if the subtype indication
7892 -- contains no constraint or an N_Subtype_Indication if the subtype
7893 -- indication has a constraint.
7895 Indic := Subtype_Indication (Type_Def);
7896 Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
7898 -- Check that the type has visible discriminants. The type may be
7899 -- a private type with unknown discriminants whose full view has
7900 -- discriminants which are invisible.
7902 if Constraint_Present then
7903 if not Has_Discriminants (Parent_Base)
7904 or else
7905 (Has_Unknown_Discriminants (Parent_Base)
7906 and then Is_Private_Type (Parent_Base))
7907 then
7908 Error_Msg_N
7909 ("invalid constraint: type has no discriminant",
7910 Constraint (Indic));
7912 Constraint_Present := False;
7913 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
7915 elsif Is_Constrained (Parent_Type) then
7916 Error_Msg_N
7917 ("invalid constraint: parent type is already constrained",
7918 Constraint (Indic));
7920 Constraint_Present := False;
7921 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
7922 end if;
7923 end if;
7925 -- STEP 0b: If needed, apply transformation given in point 5. above
7927 if not Private_Extension
7928 and then Has_Discriminants (Parent_Type)
7929 and then not Discriminant_Specs
7930 and then (Is_Constrained (Parent_Type) or else Constraint_Present)
7931 then
7932 -- First, we must analyze the constraint (see comment in point 5.)
7933 -- The constraint may come from the subtype indication of the full
7934 -- declaration.
7936 if Constraint_Present then
7937 New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
7939 -- If there is no explicit constraint, there might be one that is
7940 -- inherited from a constrained parent type. In that case verify that
7941 -- it conforms to the constraint in the partial view. In perverse
7942 -- cases the parent subtypes of the partial and full view can have
7943 -- different constraints.
7945 elsif Present (Stored_Constraint (Parent_Type)) then
7946 New_Discrs := Stored_Constraint (Parent_Type);
7948 else
7949 New_Discrs := No_Elist;
7950 end if;
7952 if Has_Discriminants (Derived_Type)
7953 and then Has_Private_Declaration (Derived_Type)
7954 and then Present (Discriminant_Constraint (Derived_Type))
7955 and then Present (New_Discrs)
7956 then
7957 -- Verify that constraints of the full view statically match
7958 -- those given in the partial view.
7960 declare
7961 C1, C2 : Elmt_Id;
7963 begin
7964 C1 := First_Elmt (New_Discrs);
7965 C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
7966 while Present (C1) and then Present (C2) loop
7967 if Fully_Conformant_Expressions (Node (C1), Node (C2))
7968 or else
7969 (Is_OK_Static_Expression (Node (C1))
7970 and then Is_OK_Static_Expression (Node (C2))
7971 and then
7972 Expr_Value (Node (C1)) = Expr_Value (Node (C2)))
7973 then
7974 null;
7976 else
7977 if Constraint_Present then
7978 Error_Msg_N
7979 ("constraint not conformant to previous declaration",
7980 Node (C1));
7981 else
7982 Error_Msg_N
7983 ("constraint of full view is incompatible "
7984 & "with partial view", N);
7985 end if;
7986 end if;
7988 Next_Elmt (C1);
7989 Next_Elmt (C2);
7990 end loop;
7991 end;
7992 end if;
7994 -- Insert and analyze the declaration for the unconstrained base type
7996 New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
7998 New_Decl :=
7999 Make_Full_Type_Declaration (Loc,
8000 Defining_Identifier => New_Base,
8001 Type_Definition =>
8002 Make_Derived_Type_Definition (Loc,
8003 Abstract_Present => Abstract_Present (Type_Def),
8004 Limited_Present => Limited_Present (Type_Def),
8005 Subtype_Indication =>
8006 New_Occurrence_Of (Parent_Base, Loc),
8007 Record_Extension_Part =>
8008 Relocate_Node (Record_Extension_Part (Type_Def)),
8009 Interface_List => Interface_List (Type_Def)));
8011 Set_Parent (New_Decl, Parent (N));
8012 Mark_Rewrite_Insertion (New_Decl);
8013 Insert_Before (N, New_Decl);
8015 -- In the extension case, make sure ancestor is frozen appropriately
8016 -- (see also non-discriminated case below).
8018 if Present (Record_Extension_Part (Type_Def))
8019 or else Is_Interface (Parent_Base)
8020 then
8021 Freeze_Before (New_Decl, Parent_Type);
8022 end if;
8024 -- Note that this call passes False for the Derive_Subps parameter
8025 -- because subprogram derivation is deferred until after creating
8026 -- the subtype (see below).
8028 Build_Derived_Type
8029 (New_Decl, Parent_Base, New_Base,
8030 Is_Completion => False, Derive_Subps => False);
8032 -- ??? This needs re-examination to determine whether the
8033 -- above call can simply be replaced by a call to Analyze.
8035 Set_Analyzed (New_Decl);
8037 -- Insert and analyze the declaration for the constrained subtype
8039 if Constraint_Present then
8040 New_Indic :=
8041 Make_Subtype_Indication (Loc,
8042 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
8043 Constraint => Relocate_Node (Constraint (Indic)));
8045 else
8046 declare
8047 Constr_List : constant List_Id := New_List;
8048 C : Elmt_Id;
8049 Expr : Node_Id;
8051 begin
8052 C := First_Elmt (Discriminant_Constraint (Parent_Type));
8053 while Present (C) loop
8054 Expr := Node (C);
8056 -- It is safe here to call New_Copy_Tree since we called
8057 -- Force_Evaluation on each constraint previously
8058 -- in Build_Discriminant_Constraints.
8060 Append (New_Copy_Tree (Expr), To => Constr_List);
8062 Next_Elmt (C);
8063 end loop;
8065 New_Indic :=
8066 Make_Subtype_Indication (Loc,
8067 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
8068 Constraint =>
8069 Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
8070 end;
8071 end if;
8073 Rewrite (N,
8074 Make_Subtype_Declaration (Loc,
8075 Defining_Identifier => Derived_Type,
8076 Subtype_Indication => New_Indic));
8078 Analyze (N);
8080 -- Derivation of subprograms must be delayed until the full subtype
8081 -- has been established, to ensure proper overriding of subprograms
8082 -- inherited by full types. If the derivations occurred as part of
8083 -- the call to Build_Derived_Type above, then the check for type
8084 -- conformance would fail because earlier primitive subprograms
8085 -- could still refer to the full type prior the change to the new
8086 -- subtype and hence would not match the new base type created here.
8087 -- Subprograms are not derived, however, when Derive_Subps is False
8088 -- (since otherwise there could be redundant derivations).
8090 if Derive_Subps then
8091 Derive_Subprograms (Parent_Type, Derived_Type);
8092 end if;
8094 -- For tagged types the Discriminant_Constraint of the new base itype
8095 -- is inherited from the first subtype so that no subtype conformance
8096 -- problem arise when the first subtype overrides primitive
8097 -- operations inherited by the implicit base type.
8099 if Is_Tagged then
8100 Set_Discriminant_Constraint
8101 (New_Base, Discriminant_Constraint (Derived_Type));
8102 end if;
8104 return;
8105 end if;
8107 -- If we get here Derived_Type will have no discriminants or it will be
8108 -- a discriminated unconstrained base type.
8110 -- STEP 1a: perform preliminary actions/checks for derived tagged types
8112 if Is_Tagged then
8114 -- The parent type is frozen for non-private extensions (RM 13.14(7))
8115 -- The declaration of a specific descendant of an interface type
8116 -- freezes the interface type (RM 13.14).
8118 if not Private_Extension or else Is_Interface (Parent_Base) then
8119 Freeze_Before (N, Parent_Type);
8120 end if;
8122 -- In Ada 2005 (AI-344), the restriction that a derived tagged type
8123 -- cannot be declared at a deeper level than its parent type is
8124 -- removed. The check on derivation within a generic body is also
8125 -- relaxed, but there's a restriction that a derived tagged type
8126 -- cannot be declared in a generic body if it's derived directly
8127 -- or indirectly from a formal type of that generic.
8129 if Ada_Version >= Ada_2005 then
8130 if Present (Enclosing_Generic_Body (Derived_Type)) then
8131 declare
8132 Ancestor_Type : Entity_Id;
8134 begin
8135 -- Check to see if any ancestor of the derived type is a
8136 -- formal type.
8138 Ancestor_Type := Parent_Type;
8139 while not Is_Generic_Type (Ancestor_Type)
8140 and then Etype (Ancestor_Type) /= Ancestor_Type
8141 loop
8142 Ancestor_Type := Etype (Ancestor_Type);
8143 end loop;
8145 -- If the derived type does have a formal type as an
8146 -- ancestor, then it's an error if the derived type is
8147 -- declared within the body of the generic unit that
8148 -- declares the formal type in its generic formal part. It's
8149 -- sufficient to check whether the ancestor type is declared
8150 -- inside the same generic body as the derived type (such as
8151 -- within a nested generic spec), in which case the
8152 -- derivation is legal. If the formal type is declared
8153 -- outside of that generic body, then it's guaranteed that
8154 -- the derived type is declared within the generic body of
8155 -- the generic unit declaring the formal type.
8157 if Is_Generic_Type (Ancestor_Type)
8158 and then Enclosing_Generic_Body (Ancestor_Type) /=
8159 Enclosing_Generic_Body (Derived_Type)
8160 then
8161 Error_Msg_NE
8162 ("parent type of& must not be descendant of formal type"
8163 & " of an enclosing generic body",
8164 Indic, Derived_Type);
8165 end if;
8166 end;
8167 end if;
8169 elsif Type_Access_Level (Derived_Type) /=
8170 Type_Access_Level (Parent_Type)
8171 and then not Is_Generic_Type (Derived_Type)
8172 then
8173 if Is_Controlled (Parent_Type) then
8174 Error_Msg_N
8175 ("controlled type must be declared at the library level",
8176 Indic);
8177 else
8178 Error_Msg_N
8179 ("type extension at deeper accessibility level than parent",
8180 Indic);
8181 end if;
8183 else
8184 declare
8185 GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
8186 begin
8187 if Present (GB)
8188 and then GB /= Enclosing_Generic_Body (Parent_Base)
8189 then
8190 Error_Msg_NE
8191 ("parent type of& must not be outside generic body"
8192 & " (RM 3.9.1(4))",
8193 Indic, Derived_Type);
8194 end if;
8195 end;
8196 end if;
8197 end if;
8199 -- Ada 2005 (AI-251)
8201 if Ada_Version >= Ada_2005 and then Is_Tagged then
8203 -- "The declaration of a specific descendant of an interface type
8204 -- freezes the interface type" (RM 13.14).
8206 declare
8207 Iface : Node_Id;
8208 begin
8209 if Is_Non_Empty_List (Interface_List (Type_Def)) then
8210 Iface := First (Interface_List (Type_Def));
8211 while Present (Iface) loop
8212 Freeze_Before (N, Etype (Iface));
8213 Next (Iface);
8214 end loop;
8215 end if;
8216 end;
8217 end if;
8219 -- STEP 1b : preliminary cleanup of the full view of private types
8221 -- If the type is already marked as having discriminants, then it's the
8222 -- completion of a private type or private extension and we need to
8223 -- retain the discriminants from the partial view if the current
8224 -- declaration has Discriminant_Specifications so that we can verify
8225 -- conformance. However, we must remove any existing components that
8226 -- were inherited from the parent (and attached in Copy_And_Swap)
8227 -- because the full type inherits all appropriate components anyway, and
8228 -- we do not want the partial view's components interfering.
8230 if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
8231 Discrim := First_Discriminant (Derived_Type);
8232 loop
8233 Last_Discrim := Discrim;
8234 Next_Discriminant (Discrim);
8235 exit when No (Discrim);
8236 end loop;
8238 Set_Last_Entity (Derived_Type, Last_Discrim);
8240 -- In all other cases wipe out the list of inherited components (even
8241 -- inherited discriminants), it will be properly rebuilt here.
8243 else
8244 Set_First_Entity (Derived_Type, Empty);
8245 Set_Last_Entity (Derived_Type, Empty);
8246 end if;
8248 -- STEP 1c: Initialize some flags for the Derived_Type
8250 -- The following flags must be initialized here so that
8251 -- Process_Discriminants can check that discriminants of tagged types do
8252 -- not have a default initial value and that access discriminants are
8253 -- only specified for limited records. For completeness, these flags are
8254 -- also initialized along with all the other flags below.
8256 -- AI-419: Limitedness is not inherited from an interface parent, so to
8257 -- be limited in that case the type must be explicitly declared as
8258 -- limited. However, task and protected interfaces are always limited.
8260 if Limited_Present (Type_Def) then
8261 Set_Is_Limited_Record (Derived_Type);
8263 elsif Is_Limited_Record (Parent_Type)
8264 or else (Present (Full_View (Parent_Type))
8265 and then Is_Limited_Record (Full_View (Parent_Type)))
8266 then
8267 if not Is_Interface (Parent_Type)
8268 or else Is_Synchronized_Interface (Parent_Type)
8269 or else Is_Protected_Interface (Parent_Type)
8270 or else Is_Task_Interface (Parent_Type)
8271 then
8272 Set_Is_Limited_Record (Derived_Type);
8273 end if;
8274 end if;
8276 -- STEP 2a: process discriminants of derived type if any
8278 Push_Scope (Derived_Type);
8280 if Discriminant_Specs then
8281 Set_Has_Unknown_Discriminants (Derived_Type, False);
8283 -- The following call initializes fields Has_Discriminants and
8284 -- Discriminant_Constraint, unless we are processing the completion
8285 -- of a private type declaration.
8287 Check_Or_Process_Discriminants (N, Derived_Type);
8289 -- For untagged types, the constraint on the Parent_Type must be
8290 -- present and is used to rename the discriminants.
8292 if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
8293 Error_Msg_N ("untagged parent must have discriminants", Indic);
8295 elsif not Is_Tagged and then not Constraint_Present then
8296 Error_Msg_N
8297 ("discriminant constraint needed for derived untagged records",
8298 Indic);
8300 -- Otherwise the parent subtype must be constrained unless we have a
8301 -- private extension.
8303 elsif not Constraint_Present
8304 and then not Private_Extension
8305 and then not Is_Constrained (Parent_Type)
8306 then
8307 Error_Msg_N
8308 ("unconstrained type not allowed in this context", Indic);
8310 elsif Constraint_Present then
8311 -- The following call sets the field Corresponding_Discriminant
8312 -- for the discriminants in the Derived_Type.
8314 Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
8316 -- For untagged types all new discriminants must rename
8317 -- discriminants in the parent. For private extensions new
8318 -- discriminants cannot rename old ones (implied by [7.3(13)]).
8320 Discrim := First_Discriminant (Derived_Type);
8321 while Present (Discrim) loop
8322 if not Is_Tagged
8323 and then No (Corresponding_Discriminant (Discrim))
8324 then
8325 Error_Msg_N
8326 ("new discriminants must constrain old ones", Discrim);
8328 elsif Private_Extension
8329 and then Present (Corresponding_Discriminant (Discrim))
8330 then
8331 Error_Msg_N
8332 ("only static constraints allowed for parent"
8333 & " discriminants in the partial view", Indic);
8334 exit;
8335 end if;
8337 -- If a new discriminant is used in the constraint, then its
8338 -- subtype must be statically compatible with the parent
8339 -- discriminant's subtype (3.7(15)).
8341 -- However, if the record contains an array constrained by
8342 -- the discriminant but with some different bound, the compiler
8343 -- attemps to create a smaller range for the discriminant type.
8344 -- (See exp_ch3.Adjust_Discriminants). In this case, where
8345 -- the discriminant type is a scalar type, the check must use
8346 -- the original discriminant type in the parent declaration.
8348 declare
8349 Corr_Disc : constant Entity_Id :=
8350 Corresponding_Discriminant (Discrim);
8351 Disc_Type : constant Entity_Id := Etype (Discrim);
8352 Corr_Type : Entity_Id;
8354 begin
8355 if Present (Corr_Disc) then
8356 if Is_Scalar_Type (Disc_Type) then
8357 Corr_Type :=
8358 Entity (Discriminant_Type (Parent (Corr_Disc)));
8359 else
8360 Corr_Type := Etype (Corr_Disc);
8361 end if;
8363 if not
8364 Subtypes_Statically_Compatible (Disc_Type, Corr_Type)
8365 then
8366 Error_Msg_N
8367 ("subtype must be compatible "
8368 & "with parent discriminant",
8369 Discrim);
8370 end if;
8371 end if;
8372 end;
8374 Next_Discriminant (Discrim);
8375 end loop;
8377 -- Check whether the constraints of the full view statically
8378 -- match those imposed by the parent subtype [7.3(13)].
8380 if Present (Stored_Constraint (Derived_Type)) then
8381 declare
8382 C1, C2 : Elmt_Id;
8384 begin
8385 C1 := First_Elmt (Discs);
8386 C2 := First_Elmt (Stored_Constraint (Derived_Type));
8387 while Present (C1) and then Present (C2) loop
8388 if not
8389 Fully_Conformant_Expressions (Node (C1), Node (C2))
8390 then
8391 Error_Msg_N
8392 ("not conformant with previous declaration",
8393 Node (C1));
8394 end if;
8396 Next_Elmt (C1);
8397 Next_Elmt (C2);
8398 end loop;
8399 end;
8400 end if;
8401 end if;
8403 -- STEP 2b: No new discriminants, inherit discriminants if any
8405 else
8406 if Private_Extension then
8407 Set_Has_Unknown_Discriminants
8408 (Derived_Type,
8409 Has_Unknown_Discriminants (Parent_Type)
8410 or else Unknown_Discriminants_Present (N));
8412 -- The partial view of the parent may have unknown discriminants,
8413 -- but if the full view has discriminants and the parent type is
8414 -- in scope they must be inherited.
8416 elsif Has_Unknown_Discriminants (Parent_Type)
8417 and then
8418 (not Has_Discriminants (Parent_Type)
8419 or else not In_Open_Scopes (Scope (Parent_Type)))
8420 then
8421 Set_Has_Unknown_Discriminants (Derived_Type);
8422 end if;
8424 if not Has_Unknown_Discriminants (Derived_Type)
8425 and then not Has_Unknown_Discriminants (Parent_Base)
8426 and then Has_Discriminants (Parent_Type)
8427 then
8428 Inherit_Discrims := True;
8429 Set_Has_Discriminants
8430 (Derived_Type, True);
8431 Set_Discriminant_Constraint
8432 (Derived_Type, Discriminant_Constraint (Parent_Base));
8433 end if;
8435 -- The following test is true for private types (remember
8436 -- transformation 5. is not applied to those) and in an error
8437 -- situation.
8439 if Constraint_Present then
8440 Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
8441 end if;
8443 -- For now mark a new derived type as constrained only if it has no
8444 -- discriminants. At the end of Build_Derived_Record_Type we properly
8445 -- set this flag in the case of private extensions. See comments in
8446 -- point 9. just before body of Build_Derived_Record_Type.
8448 Set_Is_Constrained
8449 (Derived_Type,
8450 not (Inherit_Discrims
8451 or else Has_Unknown_Discriminants (Derived_Type)));
8452 end if;
8454 -- STEP 3: initialize fields of derived type
8456 Set_Is_Tagged_Type (Derived_Type, Is_Tagged);
8457 Set_Stored_Constraint (Derived_Type, No_Elist);
8459 -- Ada 2005 (AI-251): Private type-declarations can implement interfaces
8460 -- but cannot be interfaces
8462 if not Private_Extension
8463 and then Ekind (Derived_Type) /= E_Private_Type
8464 and then Ekind (Derived_Type) /= E_Limited_Private_Type
8465 then
8466 if Interface_Present (Type_Def) then
8467 Analyze_Interface_Declaration (Derived_Type, Type_Def);
8468 end if;
8470 Set_Interfaces (Derived_Type, No_Elist);
8471 end if;
8473 -- Fields inherited from the Parent_Type
8475 Set_Has_Specified_Layout
8476 (Derived_Type, Has_Specified_Layout (Parent_Type));
8477 Set_Is_Limited_Composite
8478 (Derived_Type, Is_Limited_Composite (Parent_Type));
8479 Set_Is_Private_Composite
8480 (Derived_Type, Is_Private_Composite (Parent_Type));
8482 if Is_Tagged_Type (Parent_Type) then
8483 Set_No_Tagged_Streams_Pragma
8484 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
8485 end if;
8487 -- Fields inherited from the Parent_Base
8489 Set_Has_Controlled_Component
8490 (Derived_Type, Has_Controlled_Component (Parent_Base));
8491 Set_Has_Non_Standard_Rep
8492 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
8493 Set_Has_Primitive_Operations
8494 (Derived_Type, Has_Primitive_Operations (Parent_Base));
8496 -- Fields inherited from the Parent_Base in the non-private case
8498 if Ekind (Derived_Type) = E_Record_Type then
8499 Set_Has_Complex_Representation
8500 (Derived_Type, Has_Complex_Representation (Parent_Base));
8501 end if;
8503 -- Fields inherited from the Parent_Base for record types
8505 if Is_Record_Type (Derived_Type) then
8506 declare
8507 Parent_Full : Entity_Id;
8509 begin
8510 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
8511 -- Parent_Base can be a private type or private extension. Go
8512 -- to the full view here to get the E_Record_Type specific flags.
8514 if Present (Full_View (Parent_Base)) then
8515 Parent_Full := Full_View (Parent_Base);
8516 else
8517 Parent_Full := Parent_Base;
8518 end if;
8520 Set_OK_To_Reorder_Components
8521 (Derived_Type, OK_To_Reorder_Components (Parent_Full));
8522 end;
8523 end if;
8525 -- Set fields for private derived types
8527 if Is_Private_Type (Derived_Type) then
8528 Set_Depends_On_Private (Derived_Type, True);
8529 Set_Private_Dependents (Derived_Type, New_Elmt_List);
8531 -- Inherit fields from non private record types. If this is the
8532 -- completion of a derivation from a private type, the parent itself
8533 -- is private, and the attributes come from its full view, which must
8534 -- be present.
8536 else
8537 if Is_Private_Type (Parent_Base)
8538 and then not Is_Record_Type (Parent_Base)
8539 then
8540 Set_Component_Alignment
8541 (Derived_Type, Component_Alignment (Full_View (Parent_Base)));
8542 Set_C_Pass_By_Copy
8543 (Derived_Type, C_Pass_By_Copy (Full_View (Parent_Base)));
8544 else
8545 Set_Component_Alignment
8546 (Derived_Type, Component_Alignment (Parent_Base));
8547 Set_C_Pass_By_Copy
8548 (Derived_Type, C_Pass_By_Copy (Parent_Base));
8549 end if;
8550 end if;
8552 -- Set fields for tagged types
8554 if Is_Tagged then
8555 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
8557 -- All tagged types defined in Ada.Finalization are controlled
8559 if Chars (Scope (Derived_Type)) = Name_Finalization
8560 and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
8561 and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
8562 then
8563 Set_Is_Controlled (Derived_Type);
8564 else
8565 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Base));
8566 end if;
8568 -- Minor optimization: there is no need to generate the class-wide
8569 -- entity associated with an underlying record view.
8571 if not Is_Underlying_Record_View (Derived_Type) then
8572 Make_Class_Wide_Type (Derived_Type);
8573 end if;
8575 Set_Is_Abstract_Type (Derived_Type, Abstract_Present (Type_Def));
8577 if Has_Discriminants (Derived_Type)
8578 and then Constraint_Present
8579 then
8580 Set_Stored_Constraint
8581 (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
8582 end if;
8584 if Ada_Version >= Ada_2005 then
8585 declare
8586 Ifaces_List : Elist_Id;
8588 begin
8589 -- Checks rules 3.9.4 (13/2 and 14/2)
8591 if Comes_From_Source (Derived_Type)
8592 and then not Is_Private_Type (Derived_Type)
8593 and then Is_Interface (Parent_Type)
8594 and then not Is_Interface (Derived_Type)
8595 then
8596 if Is_Task_Interface (Parent_Type) then
8597 Error_Msg_N
8598 ("(Ada 2005) task type required (RM 3.9.4 (13.2))",
8599 Derived_Type);
8601 elsif Is_Protected_Interface (Parent_Type) then
8602 Error_Msg_N
8603 ("(Ada 2005) protected type required (RM 3.9.4 (14.2))",
8604 Derived_Type);
8605 end if;
8606 end if;
8608 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
8610 Check_Interfaces (N, Type_Def);
8612 -- Ada 2005 (AI-251): Collect the list of progenitors that are
8613 -- not already in the parents.
8615 Collect_Interfaces
8616 (T => Derived_Type,
8617 Ifaces_List => Ifaces_List,
8618 Exclude_Parents => True);
8620 Set_Interfaces (Derived_Type, Ifaces_List);
8622 -- If the derived type is the anonymous type created for
8623 -- a declaration whose parent has a constraint, propagate
8624 -- the interface list to the source type. This must be done
8625 -- prior to the completion of the analysis of the source type
8626 -- because the components in the extension may contain current
8627 -- instances whose legality depends on some ancestor.
8629 if Is_Itype (Derived_Type) then
8630 declare
8631 Def : constant Node_Id :=
8632 Associated_Node_For_Itype (Derived_Type);
8633 begin
8634 if Present (Def)
8635 and then Nkind (Def) = N_Full_Type_Declaration
8636 then
8637 Set_Interfaces
8638 (Defining_Identifier (Def), Ifaces_List);
8639 end if;
8640 end;
8641 end if;
8643 -- Propagate inherited invariant information of parents
8644 -- and progenitors
8646 if Ada_Version >= Ada_2012
8647 and then not Is_Interface (Derived_Type)
8648 then
8649 if Has_Inheritable_Invariants (Parent_Type) then
8650 Set_Has_Invariants (Derived_Type);
8651 Set_Has_Inheritable_Invariants (Derived_Type);
8653 elsif not Is_Empty_Elmt_List (Ifaces_List) then
8654 declare
8655 AI : Elmt_Id;
8657 begin
8658 AI := First_Elmt (Ifaces_List);
8659 while Present (AI) loop
8660 if Has_Inheritable_Invariants (Node (AI)) then
8661 Set_Has_Invariants (Derived_Type);
8662 Set_Has_Inheritable_Invariants (Derived_Type);
8664 exit;
8665 end if;
8667 Next_Elmt (AI);
8668 end loop;
8669 end;
8670 end if;
8671 end if;
8673 -- A type extension is automatically Ghost when one of its
8674 -- progenitors is Ghost (SPARK RM 6.9(9)). This property is
8675 -- also inherited when the parent type is Ghost, but this is
8676 -- done in Build_Derived_Type as the mechanism also handles
8677 -- untagged derivations.
8679 if Implements_Ghost_Interface (Derived_Type) then
8680 Set_Is_Ghost_Entity (Derived_Type);
8681 end if;
8682 end;
8683 end if;
8685 else
8686 Set_Is_Packed (Derived_Type, Is_Packed (Parent_Base));
8687 Set_Has_Non_Standard_Rep
8688 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
8689 end if;
8691 -- STEP 4: Inherit components from the parent base and constrain them.
8692 -- Apply the second transformation described in point 6. above.
8694 if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
8695 or else not Has_Discriminants (Parent_Type)
8696 or else not Is_Constrained (Parent_Type)
8697 then
8698 Constrs := Discs;
8699 else
8700 Constrs := Discriminant_Constraint (Parent_Type);
8701 end if;
8703 Assoc_List :=
8704 Inherit_Components
8705 (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
8707 -- STEP 5a: Copy the parent record declaration for untagged types
8709 if not Is_Tagged then
8711 -- Discriminant_Constraint (Derived_Type) has been properly
8712 -- constructed. Save it and temporarily set it to Empty because we
8713 -- do not want the call to New_Copy_Tree below to mess this list.
8715 if Has_Discriminants (Derived_Type) then
8716 Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
8717 Set_Discriminant_Constraint (Derived_Type, No_Elist);
8718 else
8719 Save_Discr_Constr := No_Elist;
8720 end if;
8722 -- Save the Etype field of Derived_Type. It is correctly set now,
8723 -- but the call to New_Copy tree may remap it to point to itself,
8724 -- which is not what we want. Ditto for the Next_Entity field.
8726 Save_Etype := Etype (Derived_Type);
8727 Save_Next_Entity := Next_Entity (Derived_Type);
8729 -- Assoc_List maps all stored discriminants in the Parent_Base to
8730 -- stored discriminants in the Derived_Type. It is fundamental that
8731 -- no types or itypes with discriminants other than the stored
8732 -- discriminants appear in the entities declared inside
8733 -- Derived_Type, since the back end cannot deal with it.
8735 New_Decl :=
8736 New_Copy_Tree
8737 (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
8739 -- Restore the fields saved prior to the New_Copy_Tree call
8740 -- and compute the stored constraint.
8742 Set_Etype (Derived_Type, Save_Etype);
8743 Set_Next_Entity (Derived_Type, Save_Next_Entity);
8745 if Has_Discriminants (Derived_Type) then
8746 Set_Discriminant_Constraint
8747 (Derived_Type, Save_Discr_Constr);
8748 Set_Stored_Constraint
8749 (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
8750 Replace_Components (Derived_Type, New_Decl);
8751 Set_Has_Implicit_Dereference
8752 (Derived_Type, Has_Implicit_Dereference (Parent_Type));
8753 end if;
8755 -- Insert the new derived type declaration
8757 Rewrite (N, New_Decl);
8759 -- STEP 5b: Complete the processing for record extensions in generics
8761 -- There is no completion for record extensions declared in the
8762 -- parameter part of a generic, so we need to complete processing for
8763 -- these generic record extensions here. The Record_Type_Definition call
8764 -- will change the Ekind of the components from E_Void to E_Component.
8766 elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
8767 Record_Type_Definition (Empty, Derived_Type);
8769 -- STEP 5c: Process the record extension for non private tagged types
8771 elsif not Private_Extension then
8772 Expand_Record_Extension (Derived_Type, Type_Def);
8774 -- Note : previously in ASIS mode we set the Parent_Subtype of the
8775 -- derived type to propagate some semantic information. This led
8776 -- to other ASIS failures and has been removed.
8778 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
8779 -- implemented interfaces if we are in expansion mode
8781 if Expander_Active
8782 and then Has_Interfaces (Derived_Type)
8783 then
8784 Add_Interface_Tag_Components (N, Derived_Type);
8785 end if;
8787 -- Analyze the record extension
8789 Record_Type_Definition
8790 (Record_Extension_Part (Type_Def), Derived_Type);
8791 end if;
8793 End_Scope;
8795 -- Nothing else to do if there is an error in the derivation.
8796 -- An unusual case: the full view may be derived from a type in an
8797 -- instance, when the partial view was used illegally as an actual
8798 -- in that instance, leading to a circular definition.
8800 if Etype (Derived_Type) = Any_Type
8801 or else Etype (Parent_Type) = Derived_Type
8802 then
8803 return;
8804 end if;
8806 -- Set delayed freeze and then derive subprograms, we need to do
8807 -- this in this order so that derived subprograms inherit the
8808 -- derived freeze if necessary.
8810 Set_Has_Delayed_Freeze (Derived_Type);
8812 if Derive_Subps then
8813 Derive_Subprograms (Parent_Type, Derived_Type);
8814 end if;
8816 -- If we have a private extension which defines a constrained derived
8817 -- type mark as constrained here after we have derived subprograms. See
8818 -- comment on point 9. just above the body of Build_Derived_Record_Type.
8820 if Private_Extension and then Inherit_Discrims then
8821 if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
8822 Set_Is_Constrained (Derived_Type, True);
8823 Set_Discriminant_Constraint (Derived_Type, Discs);
8825 elsif Is_Constrained (Parent_Type) then
8826 Set_Is_Constrained
8827 (Derived_Type, True);
8828 Set_Discriminant_Constraint
8829 (Derived_Type, Discriminant_Constraint (Parent_Type));
8830 end if;
8831 end if;
8833 -- Update the class-wide type, which shares the now-completed entity
8834 -- list with its specific type. In case of underlying record views,
8835 -- we do not generate the corresponding class wide entity.
8837 if Is_Tagged
8838 and then not Is_Underlying_Record_View (Derived_Type)
8839 then
8840 Set_First_Entity
8841 (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
8842 Set_Last_Entity
8843 (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
8844 end if;
8846 Check_Function_Writable_Actuals (N);
8847 end Build_Derived_Record_Type;
8849 ------------------------
8850 -- Build_Derived_Type --
8851 ------------------------
8853 procedure Build_Derived_Type
8854 (N : Node_Id;
8855 Parent_Type : Entity_Id;
8856 Derived_Type : Entity_Id;
8857 Is_Completion : Boolean;
8858 Derive_Subps : Boolean := True)
8860 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
8862 begin
8863 -- Set common attributes
8865 Set_Scope (Derived_Type, Current_Scope);
8867 Set_Etype (Derived_Type, Parent_Base);
8868 Set_Ekind (Derived_Type, Ekind (Parent_Base));
8869 Set_Has_Task (Derived_Type, Has_Task (Parent_Base));
8870 Set_Has_Protected (Derived_Type, Has_Protected (Parent_Base));
8872 Set_Size_Info (Derived_Type, Parent_Type);
8873 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
8874 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
8875 Set_Is_Tagged_Type (Derived_Type, Is_Tagged_Type (Parent_Type));
8876 Set_Is_Volatile (Derived_Type, Is_Volatile (Parent_Type));
8878 if Is_Tagged_Type (Derived_Type) then
8879 Set_No_Tagged_Streams_Pragma
8880 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
8881 end if;
8883 -- If the parent has primitive routines, set the derived type link
8885 if Has_Primitive_Operations (Parent_Type) then
8886 Set_Derived_Type_Link (Parent_Base, Derived_Type);
8887 end if;
8889 -- If the parent type is a private subtype, the convention on the base
8890 -- type may be set in the private part, and not propagated to the
8891 -- subtype until later, so we obtain the convention from the base type.
8893 Set_Convention (Derived_Type, Convention (Parent_Base));
8895 -- Set SSO default for record or array type
8897 if (Is_Array_Type (Derived_Type) or else Is_Record_Type (Derived_Type))
8898 and then Is_Base_Type (Derived_Type)
8899 then
8900 Set_Default_SSO (Derived_Type);
8901 end if;
8903 -- Propagate invariant information. The new type has invariants if
8904 -- they are inherited from the parent type, and these invariants can
8905 -- be further inherited, so both flags are set.
8907 -- We similarly inherit predicates
8909 if Has_Predicates (Parent_Type) then
8910 Set_Has_Predicates (Derived_Type);
8911 end if;
8913 -- The derived type inherits the representation clauses of the parent
8915 Inherit_Rep_Item_Chain (Derived_Type, Parent_Type);
8917 -- Propagate the attributes related to pragma Default_Initial_Condition
8918 -- from the parent type to the private extension. A derived type always
8919 -- inherits the default initial condition flag from the parent type. If
8920 -- the derived type carries its own Default_Initial_Condition pragma,
8921 -- the flag is later reset in Analyze_Pragma. Note that both flags are
8922 -- mutually exclusive.
8924 Propagate_Default_Init_Cond_Attributes
8925 (From_Typ => Parent_Type,
8926 To_Typ => Derived_Type,
8927 Parent_To_Derivation => True);
8929 -- If the parent type has delayed rep aspects, then mark the derived
8930 -- type as possibly inheriting a delayed rep aspect.
8932 if Has_Delayed_Rep_Aspects (Parent_Type) then
8933 Set_May_Inherit_Delayed_Rep_Aspects (Derived_Type);
8934 end if;
8936 -- Propagate the attributes related to pragma Ghost from the parent type
8937 -- to the derived type or type extension (SPARK RM 6.9(9)).
8939 if Is_Ghost_Entity (Parent_Type) then
8940 Set_Is_Ghost_Entity (Derived_Type);
8941 end if;
8943 -- Type dependent processing
8945 case Ekind (Parent_Type) is
8946 when Numeric_Kind =>
8947 Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
8949 when Array_Kind =>
8950 Build_Derived_Array_Type (N, Parent_Type, Derived_Type);
8952 when E_Record_Type
8953 | E_Record_Subtype
8954 | Class_Wide_Kind =>
8955 Build_Derived_Record_Type
8956 (N, Parent_Type, Derived_Type, Derive_Subps);
8957 return;
8959 when Enumeration_Kind =>
8960 Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
8962 when Access_Kind =>
8963 Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
8965 when Incomplete_Or_Private_Kind =>
8966 Build_Derived_Private_Type
8967 (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
8969 -- For discriminated types, the derivation includes deriving
8970 -- primitive operations. For others it is done below.
8972 if Is_Tagged_Type (Parent_Type)
8973 or else Has_Discriminants (Parent_Type)
8974 or else (Present (Full_View (Parent_Type))
8975 and then Has_Discriminants (Full_View (Parent_Type)))
8976 then
8977 return;
8978 end if;
8980 when Concurrent_Kind =>
8981 Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
8983 when others =>
8984 raise Program_Error;
8985 end case;
8987 -- Nothing more to do if some error occurred
8989 if Etype (Derived_Type) = Any_Type then
8990 return;
8991 end if;
8993 -- Set delayed freeze and then derive subprograms, we need to do this
8994 -- in this order so that derived subprograms inherit the derived freeze
8995 -- if necessary.
8997 Set_Has_Delayed_Freeze (Derived_Type);
8999 if Derive_Subps then
9000 Derive_Subprograms (Parent_Type, Derived_Type);
9001 end if;
9003 Set_Has_Primitive_Operations
9004 (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
9005 end Build_Derived_Type;
9007 -----------------------
9008 -- Build_Discriminal --
9009 -----------------------
9011 procedure Build_Discriminal (Discrim : Entity_Id) is
9012 D_Minal : Entity_Id;
9013 CR_Disc : Entity_Id;
9015 begin
9016 -- A discriminal has the same name as the discriminant
9018 D_Minal := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9020 Set_Ekind (D_Minal, E_In_Parameter);
9021 Set_Mechanism (D_Minal, Default_Mechanism);
9022 Set_Etype (D_Minal, Etype (Discrim));
9023 Set_Scope (D_Minal, Current_Scope);
9025 Set_Discriminal (Discrim, D_Minal);
9026 Set_Discriminal_Link (D_Minal, Discrim);
9028 -- For task types, build at once the discriminants of the corresponding
9029 -- record, which are needed if discriminants are used in entry defaults
9030 -- and in family bounds.
9032 if Is_Concurrent_Type (Current_Scope)
9033 or else
9034 Is_Limited_Type (Current_Scope)
9035 then
9036 CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9038 Set_Ekind (CR_Disc, E_In_Parameter);
9039 Set_Mechanism (CR_Disc, Default_Mechanism);
9040 Set_Etype (CR_Disc, Etype (Discrim));
9041 Set_Scope (CR_Disc, Current_Scope);
9042 Set_Discriminal_Link (CR_Disc, Discrim);
9043 Set_CR_Discriminant (Discrim, CR_Disc);
9044 end if;
9045 end Build_Discriminal;
9047 ------------------------------------
9048 -- Build_Discriminant_Constraints --
9049 ------------------------------------
9051 function Build_Discriminant_Constraints
9052 (T : Entity_Id;
9053 Def : Node_Id;
9054 Derived_Def : Boolean := False) return Elist_Id
9056 C : constant Node_Id := Constraint (Def);
9057 Nb_Discr : constant Nat := Number_Discriminants (T);
9059 Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
9060 -- Saves the expression corresponding to a given discriminant in T
9062 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
9063 -- Return the Position number within array Discr_Expr of a discriminant
9064 -- D within the discriminant list of the discriminated type T.
9066 procedure Process_Discriminant_Expression
9067 (Expr : Node_Id;
9068 D : Entity_Id);
9069 -- If this is a discriminant constraint on a partial view, do not
9070 -- generate an overflow check on the discriminant expression. The check
9071 -- will be generated when constraining the full view. Otherwise the
9072 -- backend creates duplicate symbols for the temporaries corresponding
9073 -- to the expressions to be checked, causing spurious assembler errors.
9075 ------------------
9076 -- Pos_Of_Discr --
9077 ------------------
9079 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
9080 Disc : Entity_Id;
9082 begin
9083 Disc := First_Discriminant (T);
9084 for J in Discr_Expr'Range loop
9085 if Disc = D then
9086 return J;
9087 end if;
9089 Next_Discriminant (Disc);
9090 end loop;
9092 -- Note: Since this function is called on discriminants that are
9093 -- known to belong to the discriminated type, falling through the
9094 -- loop with no match signals an internal compiler error.
9096 raise Program_Error;
9097 end Pos_Of_Discr;
9099 -------------------------------------
9100 -- Process_Discriminant_Expression --
9101 -------------------------------------
9103 procedure Process_Discriminant_Expression
9104 (Expr : Node_Id;
9105 D : Entity_Id)
9107 BDT : constant Entity_Id := Base_Type (Etype (D));
9109 begin
9110 -- If this is a discriminant constraint on a partial view, do
9111 -- not generate an overflow on the discriminant expression. The
9112 -- check will be generated when constraining the full view.
9114 if Is_Private_Type (T)
9115 and then Present (Full_View (T))
9116 then
9117 Analyze_And_Resolve (Expr, BDT, Suppress => Overflow_Check);
9118 else
9119 Analyze_And_Resolve (Expr, BDT);
9120 end if;
9121 end Process_Discriminant_Expression;
9123 -- Declarations local to Build_Discriminant_Constraints
9125 Discr : Entity_Id;
9126 E : Entity_Id;
9127 Elist : constant Elist_Id := New_Elmt_List;
9129 Constr : Node_Id;
9130 Expr : Node_Id;
9131 Id : Node_Id;
9132 Position : Nat;
9133 Found : Boolean;
9135 Discrim_Present : Boolean := False;
9137 -- Start of processing for Build_Discriminant_Constraints
9139 begin
9140 -- The following loop will process positional associations only.
9141 -- For a positional association, the (single) discriminant is
9142 -- implicitly specified by position, in textual order (RM 3.7.2).
9144 Discr := First_Discriminant (T);
9145 Constr := First (Constraints (C));
9146 for D in Discr_Expr'Range loop
9147 exit when Nkind (Constr) = N_Discriminant_Association;
9149 if No (Constr) then
9150 Error_Msg_N ("too few discriminants given in constraint", C);
9151 return New_Elmt_List;
9153 elsif Nkind (Constr) = N_Range
9154 or else (Nkind (Constr) = N_Attribute_Reference
9155 and then Attribute_Name (Constr) = Name_Range)
9156 then
9157 Error_Msg_N
9158 ("a range is not a valid discriminant constraint", Constr);
9159 Discr_Expr (D) := Error;
9161 else
9162 Process_Discriminant_Expression (Constr, Discr);
9163 Discr_Expr (D) := Constr;
9164 end if;
9166 Next_Discriminant (Discr);
9167 Next (Constr);
9168 end loop;
9170 if No (Discr) and then Present (Constr) then
9171 Error_Msg_N ("too many discriminants given in constraint", Constr);
9172 return New_Elmt_List;
9173 end if;
9175 -- Named associations can be given in any order, but if both positional
9176 -- and named associations are used in the same discriminant constraint,
9177 -- then positional associations must occur first, at their normal
9178 -- position. Hence once a named association is used, the rest of the
9179 -- discriminant constraint must use only named associations.
9181 while Present (Constr) loop
9183 -- Positional association forbidden after a named association
9185 if Nkind (Constr) /= N_Discriminant_Association then
9186 Error_Msg_N ("positional association follows named one", Constr);
9187 return New_Elmt_List;
9189 -- Otherwise it is a named association
9191 else
9192 -- E records the type of the discriminants in the named
9193 -- association. All the discriminants specified in the same name
9194 -- association must have the same type.
9196 E := Empty;
9198 -- Search the list of discriminants in T to see if the simple name
9199 -- given in the constraint matches any of them.
9201 Id := First (Selector_Names (Constr));
9202 while Present (Id) loop
9203 Found := False;
9205 -- If Original_Discriminant is present, we are processing a
9206 -- generic instantiation and this is an instance node. We need
9207 -- to find the name of the corresponding discriminant in the
9208 -- actual record type T and not the name of the discriminant in
9209 -- the generic formal. Example:
9211 -- generic
9212 -- type G (D : int) is private;
9213 -- package P is
9214 -- subtype W is G (D => 1);
9215 -- end package;
9216 -- type Rec (X : int) is record ... end record;
9217 -- package Q is new P (G => Rec);
9219 -- At the point of the instantiation, formal type G is Rec
9220 -- and therefore when reanalyzing "subtype W is G (D => 1);"
9221 -- which really looks like "subtype W is Rec (D => 1);" at
9222 -- the point of instantiation, we want to find the discriminant
9223 -- that corresponds to D in Rec, i.e. X.
9225 if Present (Original_Discriminant (Id))
9226 and then In_Instance
9227 then
9228 Discr := Find_Corresponding_Discriminant (Id, T);
9229 Found := True;
9231 else
9232 Discr := First_Discriminant (T);
9233 while Present (Discr) loop
9234 if Chars (Discr) = Chars (Id) then
9235 Found := True;
9236 exit;
9237 end if;
9239 Next_Discriminant (Discr);
9240 end loop;
9242 if not Found then
9243 Error_Msg_N ("& does not match any discriminant", Id);
9244 return New_Elmt_List;
9246 -- If the parent type is a generic formal, preserve the
9247 -- name of the discriminant for subsequent instances.
9248 -- see comment at the beginning of this if statement.
9250 elsif Is_Generic_Type (Root_Type (T)) then
9251 Set_Original_Discriminant (Id, Discr);
9252 end if;
9253 end if;
9255 Position := Pos_Of_Discr (T, Discr);
9257 if Present (Discr_Expr (Position)) then
9258 Error_Msg_N ("duplicate constraint for discriminant&", Id);
9260 else
9261 -- Each discriminant specified in the same named association
9262 -- must be associated with a separate copy of the
9263 -- corresponding expression.
9265 if Present (Next (Id)) then
9266 Expr := New_Copy_Tree (Expression (Constr));
9267 Set_Parent (Expr, Parent (Expression (Constr)));
9268 else
9269 Expr := Expression (Constr);
9270 end if;
9272 Discr_Expr (Position) := Expr;
9273 Process_Discriminant_Expression (Expr, Discr);
9274 end if;
9276 -- A discriminant association with more than one discriminant
9277 -- name is only allowed if the named discriminants are all of
9278 -- the same type (RM 3.7.1(8)).
9280 if E = Empty then
9281 E := Base_Type (Etype (Discr));
9283 elsif Base_Type (Etype (Discr)) /= E then
9284 Error_Msg_N
9285 ("all discriminants in an association " &
9286 "must have the same type", Id);
9287 end if;
9289 Next (Id);
9290 end loop;
9291 end if;
9293 Next (Constr);
9294 end loop;
9296 -- A discriminant constraint must provide exactly one value for each
9297 -- discriminant of the type (RM 3.7.1(8)).
9299 for J in Discr_Expr'Range loop
9300 if No (Discr_Expr (J)) then
9301 Error_Msg_N ("too few discriminants given in constraint", C);
9302 return New_Elmt_List;
9303 end if;
9304 end loop;
9306 -- Determine if there are discriminant expressions in the constraint
9308 for J in Discr_Expr'Range loop
9309 if Denotes_Discriminant
9310 (Discr_Expr (J), Check_Concurrent => True)
9311 then
9312 Discrim_Present := True;
9313 end if;
9314 end loop;
9316 -- Build an element list consisting of the expressions given in the
9317 -- discriminant constraint and apply the appropriate checks. The list
9318 -- is constructed after resolving any named discriminant associations
9319 -- and therefore the expressions appear in the textual order of the
9320 -- discriminants.
9322 Discr := First_Discriminant (T);
9323 for J in Discr_Expr'Range loop
9324 if Discr_Expr (J) /= Error then
9325 Append_Elmt (Discr_Expr (J), Elist);
9327 -- If any of the discriminant constraints is given by a
9328 -- discriminant and we are in a derived type declaration we
9329 -- have a discriminant renaming. Establish link between new
9330 -- and old discriminant.
9332 if Denotes_Discriminant (Discr_Expr (J)) then
9333 if Derived_Def then
9334 Set_Corresponding_Discriminant
9335 (Entity (Discr_Expr (J)), Discr);
9336 end if;
9338 -- Force the evaluation of non-discriminant expressions.
9339 -- If we have found a discriminant in the constraint 3.4(26)
9340 -- and 3.8(18) demand that no range checks are performed are
9341 -- after evaluation. If the constraint is for a component
9342 -- definition that has a per-object constraint, expressions are
9343 -- evaluated but not checked either. In all other cases perform
9344 -- a range check.
9346 else
9347 if Discrim_Present then
9348 null;
9350 elsif Nkind (Parent (Parent (Def))) = N_Component_Declaration
9351 and then
9352 Has_Per_Object_Constraint
9353 (Defining_Identifier (Parent (Parent (Def))))
9354 then
9355 null;
9357 elsif Is_Access_Type (Etype (Discr)) then
9358 Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
9360 else
9361 Apply_Range_Check (Discr_Expr (J), Etype (Discr));
9362 end if;
9364 Force_Evaluation (Discr_Expr (J));
9365 end if;
9367 -- Check that the designated type of an access discriminant's
9368 -- expression is not a class-wide type unless the discriminant's
9369 -- designated type is also class-wide.
9371 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
9372 and then not Is_Class_Wide_Type
9373 (Designated_Type (Etype (Discr)))
9374 and then Etype (Discr_Expr (J)) /= Any_Type
9375 and then Is_Class_Wide_Type
9376 (Designated_Type (Etype (Discr_Expr (J))))
9377 then
9378 Wrong_Type (Discr_Expr (J), Etype (Discr));
9380 elsif Is_Access_Type (Etype (Discr))
9381 and then not Is_Access_Constant (Etype (Discr))
9382 and then Is_Access_Type (Etype (Discr_Expr (J)))
9383 and then Is_Access_Constant (Etype (Discr_Expr (J)))
9384 then
9385 Error_Msg_NE
9386 ("constraint for discriminant& must be access to variable",
9387 Def, Discr);
9388 end if;
9389 end if;
9391 Next_Discriminant (Discr);
9392 end loop;
9394 return Elist;
9395 end Build_Discriminant_Constraints;
9397 ---------------------------------
9398 -- Build_Discriminated_Subtype --
9399 ---------------------------------
9401 procedure Build_Discriminated_Subtype
9402 (T : Entity_Id;
9403 Def_Id : Entity_Id;
9404 Elist : Elist_Id;
9405 Related_Nod : Node_Id;
9406 For_Access : Boolean := False)
9408 Has_Discrs : constant Boolean := Has_Discriminants (T);
9409 Constrained : constant Boolean :=
9410 (Has_Discrs
9411 and then not Is_Empty_Elmt_List (Elist)
9412 and then not Is_Class_Wide_Type (T))
9413 or else Is_Constrained (T);
9415 begin
9416 if Ekind (T) = E_Record_Type then
9417 if For_Access then
9418 Set_Ekind (Def_Id, E_Private_Subtype);
9419 Set_Is_For_Access_Subtype (Def_Id, True);
9420 else
9421 Set_Ekind (Def_Id, E_Record_Subtype);
9422 end if;
9424 -- Inherit preelaboration flag from base, for types for which it
9425 -- may have been set: records, private types, protected types.
9427 Set_Known_To_Have_Preelab_Init
9428 (Def_Id, Known_To_Have_Preelab_Init (T));
9430 elsif Ekind (T) = E_Task_Type then
9431 Set_Ekind (Def_Id, E_Task_Subtype);
9433 elsif Ekind (T) = E_Protected_Type then
9434 Set_Ekind (Def_Id, E_Protected_Subtype);
9435 Set_Known_To_Have_Preelab_Init
9436 (Def_Id, Known_To_Have_Preelab_Init (T));
9438 elsif Is_Private_Type (T) then
9439 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
9440 Set_Known_To_Have_Preelab_Init
9441 (Def_Id, Known_To_Have_Preelab_Init (T));
9443 -- Private subtypes may have private dependents
9445 Set_Private_Dependents (Def_Id, New_Elmt_List);
9447 elsif Is_Class_Wide_Type (T) then
9448 Set_Ekind (Def_Id, E_Class_Wide_Subtype);
9450 else
9451 -- Incomplete type. Attach subtype to list of dependents, to be
9452 -- completed with full view of parent type, unless is it the
9453 -- designated subtype of a record component within an init_proc.
9454 -- This last case arises for a component of an access type whose
9455 -- designated type is incomplete (e.g. a Taft Amendment type).
9456 -- The designated subtype is within an inner scope, and needs no
9457 -- elaboration, because only the access type is needed in the
9458 -- initialization procedure.
9460 Set_Ekind (Def_Id, Ekind (T));
9462 if For_Access and then Within_Init_Proc then
9463 null;
9464 else
9465 Append_Elmt (Def_Id, Private_Dependents (T));
9466 end if;
9467 end if;
9469 Set_Etype (Def_Id, T);
9470 Init_Size_Align (Def_Id);
9471 Set_Has_Discriminants (Def_Id, Has_Discrs);
9472 Set_Is_Constrained (Def_Id, Constrained);
9474 Set_First_Entity (Def_Id, First_Entity (T));
9475 Set_Last_Entity (Def_Id, Last_Entity (T));
9476 Set_Has_Implicit_Dereference
9477 (Def_Id, Has_Implicit_Dereference (T));
9479 -- If the subtype is the completion of a private declaration, there may
9480 -- have been representation clauses for the partial view, and they must
9481 -- be preserved. Build_Derived_Type chains the inherited clauses with
9482 -- the ones appearing on the extension. If this comes from a subtype
9483 -- declaration, all clauses are inherited.
9485 if No (First_Rep_Item (Def_Id)) then
9486 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
9487 end if;
9489 if Is_Tagged_Type (T) then
9490 Set_Is_Tagged_Type (Def_Id);
9491 Set_No_Tagged_Streams_Pragma (Def_Id, No_Tagged_Streams_Pragma (T));
9492 Make_Class_Wide_Type (Def_Id);
9493 end if;
9495 Set_Stored_Constraint (Def_Id, No_Elist);
9497 if Has_Discrs then
9498 Set_Discriminant_Constraint (Def_Id, Elist);
9499 Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
9500 end if;
9502 if Is_Tagged_Type (T) then
9504 -- Ada 2005 (AI-251): In case of concurrent types we inherit the
9505 -- concurrent record type (which has the list of primitive
9506 -- operations).
9508 if Ada_Version >= Ada_2005
9509 and then Is_Concurrent_Type (T)
9510 then
9511 Set_Corresponding_Record_Type (Def_Id,
9512 Corresponding_Record_Type (T));
9513 else
9514 Set_Direct_Primitive_Operations (Def_Id,
9515 Direct_Primitive_Operations (T));
9516 end if;
9518 Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
9519 end if;
9521 -- Subtypes introduced by component declarations do not need to be
9522 -- marked as delayed, and do not get freeze nodes, because the semantics
9523 -- verifies that the parents of the subtypes are frozen before the
9524 -- enclosing record is frozen.
9526 if not Is_Type (Scope (Def_Id)) then
9527 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
9529 if Is_Private_Type (T)
9530 and then Present (Full_View (T))
9531 then
9532 Conditional_Delay (Def_Id, Full_View (T));
9533 else
9534 Conditional_Delay (Def_Id, T);
9535 end if;
9536 end if;
9538 if Is_Record_Type (T) then
9539 Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
9541 if Has_Discrs
9542 and then not Is_Empty_Elmt_List (Elist)
9543 and then not For_Access
9544 then
9545 Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
9546 elsif not For_Access then
9547 Set_Cloned_Subtype (Def_Id, T);
9548 end if;
9549 end if;
9550 end Build_Discriminated_Subtype;
9552 ---------------------------
9553 -- Build_Itype_Reference --
9554 ---------------------------
9556 procedure Build_Itype_Reference
9557 (Ityp : Entity_Id;
9558 Nod : Node_Id)
9560 IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
9561 begin
9563 -- Itype references are only created for use by the back-end
9565 if Inside_A_Generic then
9566 return;
9567 else
9568 Set_Itype (IR, Ityp);
9569 Insert_After (Nod, IR);
9570 end if;
9571 end Build_Itype_Reference;
9573 ------------------------
9574 -- Build_Scalar_Bound --
9575 ------------------------
9577 function Build_Scalar_Bound
9578 (Bound : Node_Id;
9579 Par_T : Entity_Id;
9580 Der_T : Entity_Id) return Node_Id
9582 New_Bound : Entity_Id;
9584 begin
9585 -- Note: not clear why this is needed, how can the original bound
9586 -- be unanalyzed at this point? and if it is, what business do we
9587 -- have messing around with it? and why is the base type of the
9588 -- parent type the right type for the resolution. It probably is
9589 -- not. It is OK for the new bound we are creating, but not for
9590 -- the old one??? Still if it never happens, no problem.
9592 Analyze_And_Resolve (Bound, Base_Type (Par_T));
9594 if Nkind_In (Bound, N_Integer_Literal, N_Real_Literal) then
9595 New_Bound := New_Copy (Bound);
9596 Set_Etype (New_Bound, Der_T);
9597 Set_Analyzed (New_Bound);
9599 elsif Is_Entity_Name (Bound) then
9600 New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
9602 -- The following is almost certainly wrong. What business do we have
9603 -- relocating a node (Bound) that is presumably still attached to
9604 -- the tree elsewhere???
9606 else
9607 New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
9608 end if;
9610 Set_Etype (New_Bound, Der_T);
9611 return New_Bound;
9612 end Build_Scalar_Bound;
9614 --------------------------------
9615 -- Build_Underlying_Full_View --
9616 --------------------------------
9618 procedure Build_Underlying_Full_View
9619 (N : Node_Id;
9620 Typ : Entity_Id;
9621 Par : Entity_Id)
9623 Loc : constant Source_Ptr := Sloc (N);
9624 Subt : constant Entity_Id :=
9625 Make_Defining_Identifier
9626 (Loc, New_External_Name (Chars (Typ), 'S'));
9628 Constr : Node_Id;
9629 Indic : Node_Id;
9630 C : Node_Id;
9631 Id : Node_Id;
9633 procedure Set_Discriminant_Name (Id : Node_Id);
9634 -- If the derived type has discriminants, they may rename discriminants
9635 -- of the parent. When building the full view of the parent, we need to
9636 -- recover the names of the original discriminants if the constraint is
9637 -- given by named associations.
9639 ---------------------------
9640 -- Set_Discriminant_Name --
9641 ---------------------------
9643 procedure Set_Discriminant_Name (Id : Node_Id) is
9644 Disc : Entity_Id;
9646 begin
9647 Set_Original_Discriminant (Id, Empty);
9649 if Has_Discriminants (Typ) then
9650 Disc := First_Discriminant (Typ);
9651 while Present (Disc) loop
9652 if Chars (Disc) = Chars (Id)
9653 and then Present (Corresponding_Discriminant (Disc))
9654 then
9655 Set_Chars (Id, Chars (Corresponding_Discriminant (Disc)));
9656 end if;
9657 Next_Discriminant (Disc);
9658 end loop;
9659 end if;
9660 end Set_Discriminant_Name;
9662 -- Start of processing for Build_Underlying_Full_View
9664 begin
9665 if Nkind (N) = N_Full_Type_Declaration then
9666 Constr := Constraint (Subtype_Indication (Type_Definition (N)));
9668 elsif Nkind (N) = N_Subtype_Declaration then
9669 Constr := New_Copy_Tree (Constraint (Subtype_Indication (N)));
9671 elsif Nkind (N) = N_Component_Declaration then
9672 Constr :=
9673 New_Copy_Tree
9674 (Constraint (Subtype_Indication (Component_Definition (N))));
9676 else
9677 raise Program_Error;
9678 end if;
9680 C := First (Constraints (Constr));
9681 while Present (C) loop
9682 if Nkind (C) = N_Discriminant_Association then
9683 Id := First (Selector_Names (C));
9684 while Present (Id) loop
9685 Set_Discriminant_Name (Id);
9686 Next (Id);
9687 end loop;
9688 end if;
9690 Next (C);
9691 end loop;
9693 Indic :=
9694 Make_Subtype_Declaration (Loc,
9695 Defining_Identifier => Subt,
9696 Subtype_Indication =>
9697 Make_Subtype_Indication (Loc,
9698 Subtype_Mark => New_Occurrence_Of (Par, Loc),
9699 Constraint => New_Copy_Tree (Constr)));
9701 -- If this is a component subtype for an outer itype, it is not
9702 -- a list member, so simply set the parent link for analysis: if
9703 -- the enclosing type does not need to be in a declarative list,
9704 -- neither do the components.
9706 if Is_List_Member (N)
9707 and then Nkind (N) /= N_Component_Declaration
9708 then
9709 Insert_Before (N, Indic);
9710 else
9711 Set_Parent (Indic, Parent (N));
9712 end if;
9714 Analyze (Indic);
9715 Set_Underlying_Full_View (Typ, Full_View (Subt));
9716 end Build_Underlying_Full_View;
9718 -------------------------------
9719 -- Check_Abstract_Overriding --
9720 -------------------------------
9722 procedure Check_Abstract_Overriding (T : Entity_Id) is
9723 Alias_Subp : Entity_Id;
9724 Elmt : Elmt_Id;
9725 Op_List : Elist_Id;
9726 Subp : Entity_Id;
9727 Type_Def : Node_Id;
9729 procedure Check_Pragma_Implemented (Subp : Entity_Id);
9730 -- Ada 2012 (AI05-0030): Subprogram Subp overrides an interface routine
9731 -- which has pragma Implemented already set. Check whether Subp's entity
9732 -- kind conforms to the implementation kind of the overridden routine.
9734 procedure Check_Pragma_Implemented
9735 (Subp : Entity_Id;
9736 Iface_Subp : Entity_Id);
9737 -- Ada 2012 (AI05-0030): Subprogram Subp overrides interface routine
9738 -- Iface_Subp and both entities have pragma Implemented already set on
9739 -- them. Check whether the two implementation kinds are conforming.
9741 procedure Inherit_Pragma_Implemented
9742 (Subp : Entity_Id;
9743 Iface_Subp : Entity_Id);
9744 -- Ada 2012 (AI05-0030): Interface primitive Subp overrides interface
9745 -- subprogram Iface_Subp which has been marked by pragma Implemented.
9746 -- Propagate the implementation kind of Iface_Subp to Subp.
9748 ------------------------------
9749 -- Check_Pragma_Implemented --
9750 ------------------------------
9752 procedure Check_Pragma_Implemented (Subp : Entity_Id) is
9753 Iface_Alias : constant Entity_Id := Interface_Alias (Subp);
9754 Impl_Kind : constant Name_Id := Implementation_Kind (Iface_Alias);
9755 Subp_Alias : constant Entity_Id := Alias (Subp);
9756 Contr_Typ : Entity_Id;
9757 Impl_Subp : Entity_Id;
9759 begin
9760 -- Subp must have an alias since it is a hidden entity used to link
9761 -- an interface subprogram to its overriding counterpart.
9763 pragma Assert (Present (Subp_Alias));
9765 -- Handle aliases to synchronized wrappers
9767 Impl_Subp := Subp_Alias;
9769 if Is_Primitive_Wrapper (Impl_Subp) then
9770 Impl_Subp := Wrapped_Entity (Impl_Subp);
9771 end if;
9773 -- Extract the type of the controlling formal
9775 Contr_Typ := Etype (First_Formal (Subp_Alias));
9777 if Is_Concurrent_Record_Type (Contr_Typ) then
9778 Contr_Typ := Corresponding_Concurrent_Type (Contr_Typ);
9779 end if;
9781 -- An interface subprogram whose implementation kind is By_Entry must
9782 -- be implemented by an entry.
9784 if Impl_Kind = Name_By_Entry
9785 and then Ekind (Impl_Subp) /= E_Entry
9786 then
9787 Error_Msg_Node_2 := Iface_Alias;
9788 Error_Msg_NE
9789 ("type & must implement abstract subprogram & with an entry",
9790 Subp_Alias, Contr_Typ);
9792 elsif Impl_Kind = Name_By_Protected_Procedure then
9794 -- An interface subprogram whose implementation kind is By_
9795 -- Protected_Procedure cannot be implemented by a primitive
9796 -- procedure of a task type.
9798 if Ekind (Contr_Typ) /= E_Protected_Type then
9799 Error_Msg_Node_2 := Contr_Typ;
9800 Error_Msg_NE
9801 ("interface subprogram & cannot be implemented by a " &
9802 "primitive procedure of task type &", Subp_Alias,
9803 Iface_Alias);
9805 -- An interface subprogram whose implementation kind is By_
9806 -- Protected_Procedure must be implemented by a procedure.
9808 elsif Ekind (Impl_Subp) /= E_Procedure then
9809 Error_Msg_Node_2 := Iface_Alias;
9810 Error_Msg_NE
9811 ("type & must implement abstract subprogram & with a " &
9812 "procedure", Subp_Alias, Contr_Typ);
9814 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
9815 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
9816 then
9817 Error_Msg_Name_1 := Impl_Kind;
9818 Error_Msg_N
9819 ("overriding operation& must have synchronization%",
9820 Subp_Alias);
9821 end if;
9823 -- If primitive has Optional synchronization, overriding operation
9824 -- must match if it has an explicit synchronization..
9826 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
9827 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
9828 then
9829 Error_Msg_Name_1 := Impl_Kind;
9830 Error_Msg_N
9831 ("overriding operation& must have syncrhonization%",
9832 Subp_Alias);
9833 end if;
9834 end Check_Pragma_Implemented;
9836 ------------------------------
9837 -- Check_Pragma_Implemented --
9838 ------------------------------
9840 procedure Check_Pragma_Implemented
9841 (Subp : Entity_Id;
9842 Iface_Subp : Entity_Id)
9844 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
9845 Subp_Kind : constant Name_Id := Implementation_Kind (Subp);
9847 begin
9848 -- Ada 2012 (AI05-0030): The implementation kinds of an overridden
9849 -- and overriding subprogram are different. In general this is an
9850 -- error except when the implementation kind of the overridden
9851 -- subprograms is By_Any or Optional.
9853 if Iface_Kind /= Subp_Kind
9854 and then Iface_Kind /= Name_By_Any
9855 and then Iface_Kind /= Name_Optional
9856 then
9857 if Iface_Kind = Name_By_Entry then
9858 Error_Msg_N
9859 ("incompatible implementation kind, overridden subprogram " &
9860 "is marked By_Entry", Subp);
9861 else
9862 Error_Msg_N
9863 ("incompatible implementation kind, overridden subprogram " &
9864 "is marked By_Protected_Procedure", Subp);
9865 end if;
9866 end if;
9867 end Check_Pragma_Implemented;
9869 --------------------------------
9870 -- Inherit_Pragma_Implemented --
9871 --------------------------------
9873 procedure Inherit_Pragma_Implemented
9874 (Subp : Entity_Id;
9875 Iface_Subp : Entity_Id)
9877 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
9878 Loc : constant Source_Ptr := Sloc (Subp);
9879 Impl_Prag : Node_Id;
9881 begin
9882 -- Since the implementation kind is stored as a representation item
9883 -- rather than a flag, create a pragma node.
9885 Impl_Prag :=
9886 Make_Pragma (Loc,
9887 Chars => Name_Implemented,
9888 Pragma_Argument_Associations => New_List (
9889 Make_Pragma_Argument_Association (Loc,
9890 Expression => New_Occurrence_Of (Subp, Loc)),
9892 Make_Pragma_Argument_Association (Loc,
9893 Expression => Make_Identifier (Loc, Iface_Kind))));
9895 -- The pragma doesn't need to be analyzed because it is internally
9896 -- built. It is safe to directly register it as a rep item since we
9897 -- are only interested in the characters of the implementation kind.
9899 Record_Rep_Item (Subp, Impl_Prag);
9900 end Inherit_Pragma_Implemented;
9902 -- Start of processing for Check_Abstract_Overriding
9904 begin
9905 Op_List := Primitive_Operations (T);
9907 -- Loop to check primitive operations
9909 Elmt := First_Elmt (Op_List);
9910 while Present (Elmt) loop
9911 Subp := Node (Elmt);
9912 Alias_Subp := Alias (Subp);
9914 -- Inherited subprograms are identified by the fact that they do not
9915 -- come from source, and the associated source location is the
9916 -- location of the first subtype of the derived type.
9918 -- Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
9919 -- subprograms that "require overriding".
9921 -- Special exception, do not complain about failure to override the
9922 -- stream routines _Input and _Output, as well as the primitive
9923 -- operations used in dispatching selects since we always provide
9924 -- automatic overridings for these subprograms.
9926 -- Also ignore this rule for convention CIL since .NET libraries
9927 -- do bizarre things with interfaces???
9929 -- The partial view of T may have been a private extension, for
9930 -- which inherited functions dispatching on result are abstract.
9931 -- If the full view is a null extension, there is no need for
9932 -- overriding in Ada 2005, but wrappers need to be built for them
9933 -- (see exp_ch3, Build_Controlling_Function_Wrappers).
9935 if Is_Null_Extension (T)
9936 and then Has_Controlling_Result (Subp)
9937 and then Ada_Version >= Ada_2005
9938 and then Present (Alias_Subp)
9939 and then not Comes_From_Source (Subp)
9940 and then not Is_Abstract_Subprogram (Alias_Subp)
9941 and then not Is_Access_Type (Etype (Subp))
9942 then
9943 null;
9945 -- Ada 2005 (AI-251): Internal entities of interfaces need no
9946 -- processing because this check is done with the aliased
9947 -- entity
9949 elsif Present (Interface_Alias (Subp)) then
9950 null;
9952 elsif (Is_Abstract_Subprogram (Subp)
9953 or else Requires_Overriding (Subp)
9954 or else
9955 (Has_Controlling_Result (Subp)
9956 and then Present (Alias_Subp)
9957 and then not Comes_From_Source (Subp)
9958 and then Sloc (Subp) = Sloc (First_Subtype (T))))
9959 and then not Is_TSS (Subp, TSS_Stream_Input)
9960 and then not Is_TSS (Subp, TSS_Stream_Output)
9961 and then not Is_Abstract_Type (T)
9962 and then Convention (T) /= Convention_CIL
9963 and then not Is_Predefined_Interface_Primitive (Subp)
9965 -- Ada 2005 (AI-251): Do not consider hidden entities associated
9966 -- with abstract interface types because the check will be done
9967 -- with the aliased entity (otherwise we generate a duplicated
9968 -- error message).
9970 and then not Present (Interface_Alias (Subp))
9971 then
9972 if Present (Alias_Subp) then
9974 -- Only perform the check for a derived subprogram when the
9975 -- type has an explicit record extension. This avoids incorrect
9976 -- flagging of abstract subprograms for the case of a type
9977 -- without an extension that is derived from a formal type
9978 -- with a tagged actual (can occur within a private part).
9980 -- Ada 2005 (AI-391): In the case of an inherited function with
9981 -- a controlling result of the type, the rule does not apply if
9982 -- the type is a null extension (unless the parent function
9983 -- itself is abstract, in which case the function must still be
9984 -- be overridden). The expander will generate an overriding
9985 -- wrapper function calling the parent subprogram (see
9986 -- Exp_Ch3.Make_Controlling_Wrapper_Functions).
9988 Type_Def := Type_Definition (Parent (T));
9990 if Nkind (Type_Def) = N_Derived_Type_Definition
9991 and then Present (Record_Extension_Part (Type_Def))
9992 and then
9993 (Ada_Version < Ada_2005
9994 or else not Is_Null_Extension (T)
9995 or else Ekind (Subp) = E_Procedure
9996 or else not Has_Controlling_Result (Subp)
9997 or else Is_Abstract_Subprogram (Alias_Subp)
9998 or else Requires_Overriding (Subp)
9999 or else Is_Access_Type (Etype (Subp)))
10000 then
10001 -- Avoid reporting error in case of abstract predefined
10002 -- primitive inherited from interface type because the
10003 -- body of internally generated predefined primitives
10004 -- of tagged types are generated later by Freeze_Type
10006 if Is_Interface (Root_Type (T))
10007 and then Is_Abstract_Subprogram (Subp)
10008 and then Is_Predefined_Dispatching_Operation (Subp)
10009 and then not Comes_From_Source (Ultimate_Alias (Subp))
10010 then
10011 null;
10013 -- A null extension is not obliged to override an inherited
10014 -- procedure subject to pragma Extensions_Visible with value
10015 -- False and at least one controlling OUT parameter
10016 -- (SPARK RM 6.1.7(6)).
10018 elsif Is_Null_Extension (T)
10019 and then Is_EVF_Procedure (Subp)
10020 then
10021 null;
10023 else
10024 Error_Msg_NE
10025 ("type must be declared abstract or & overridden",
10026 T, Subp);
10028 -- Traverse the whole chain of aliased subprograms to
10029 -- complete the error notification. This is especially
10030 -- useful for traceability of the chain of entities when
10031 -- the subprogram corresponds with an interface
10032 -- subprogram (which may be defined in another package).
10034 if Present (Alias_Subp) then
10035 declare
10036 E : Entity_Id;
10038 begin
10039 E := Subp;
10040 while Present (Alias (E)) loop
10042 -- Avoid reporting redundant errors on entities
10043 -- inherited from interfaces
10045 if Sloc (E) /= Sloc (T) then
10046 Error_Msg_Sloc := Sloc (E);
10047 Error_Msg_NE
10048 ("\& has been inherited #", T, Subp);
10049 end if;
10051 E := Alias (E);
10052 end loop;
10054 Error_Msg_Sloc := Sloc (E);
10056 -- AI05-0068: report if there is an overriding
10057 -- non-abstract subprogram that is invisible.
10059 if Is_Hidden (E)
10060 and then not Is_Abstract_Subprogram (E)
10061 then
10062 Error_Msg_NE
10063 ("\& subprogram# is not visible",
10064 T, Subp);
10066 -- Clarify the case where a non-null extension must
10067 -- override inherited procedure subject to pragma
10068 -- Extensions_Visible with value False and at least
10069 -- one controlling OUT param.
10071 elsif Is_EVF_Procedure (E) then
10072 Error_Msg_NE
10073 ("\& # is subject to Extensions_Visible False",
10074 T, Subp);
10076 else
10077 Error_Msg_NE
10078 ("\& has been inherited from subprogram #",
10079 T, Subp);
10080 end if;
10081 end;
10082 end if;
10083 end if;
10085 -- Ada 2005 (AI-345): Protected or task type implementing
10086 -- abstract interfaces.
10088 elsif Is_Concurrent_Record_Type (T)
10089 and then Present (Interfaces (T))
10090 then
10091 -- There is no need to check here RM 9.4(11.9/3) since we
10092 -- are processing the corresponding record type and the
10093 -- mode of the overriding subprograms was verified by
10094 -- Check_Conformance when the corresponding concurrent
10095 -- type declaration was analyzed.
10097 Error_Msg_NE
10098 ("interface subprogram & must be overridden", T, Subp);
10100 -- Examine primitive operations of synchronized type to find
10101 -- homonyms that have the wrong profile.
10103 declare
10104 Prim : Entity_Id;
10106 begin
10107 Prim := First_Entity (Corresponding_Concurrent_Type (T));
10108 while Present (Prim) loop
10109 if Chars (Prim) = Chars (Subp) then
10110 Error_Msg_NE
10111 ("profile is not type conformant with prefixed "
10112 & "view profile of inherited operation&",
10113 Prim, Subp);
10114 end if;
10116 Next_Entity (Prim);
10117 end loop;
10118 end;
10119 end if;
10121 else
10122 Error_Msg_Node_2 := T;
10123 Error_Msg_N
10124 ("abstract subprogram& not allowed for type&", Subp);
10126 -- Also post unconditional warning on the type (unconditional
10127 -- so that if there are more than one of these cases, we get
10128 -- them all, and not just the first one).
10130 Error_Msg_Node_2 := Subp;
10131 Error_Msg_N ("nonabstract type& has abstract subprogram&!", T);
10132 end if;
10134 -- A subprogram subject to pragma Extensions_Visible with value
10135 -- "True" cannot override a subprogram subject to the same pragma
10136 -- with value "False" (SPARK RM 6.1.7(5)).
10138 elsif Extensions_Visible_Status (Subp) = Extensions_Visible_True
10139 and then Present (Overridden_Operation (Subp))
10140 and then Extensions_Visible_Status (Overridden_Operation (Subp)) =
10141 Extensions_Visible_False
10142 then
10143 Error_Msg_Sloc := Sloc (Overridden_Operation (Subp));
10144 Error_Msg_N
10145 ("subprogram & with Extensions_Visible True cannot override "
10146 & "subprogram # with Extensions_Visible False", Subp);
10147 end if;
10149 -- Ada 2012 (AI05-0030): Perform checks related to pragma Implemented
10151 -- Subp is an expander-generated procedure which maps an interface
10152 -- alias to a protected wrapper. The interface alias is flagged by
10153 -- pragma Implemented. Ensure that Subp is a procedure when the
10154 -- implementation kind is By_Protected_Procedure or an entry when
10155 -- By_Entry.
10157 if Ada_Version >= Ada_2012
10158 and then Is_Hidden (Subp)
10159 and then Present (Interface_Alias (Subp))
10160 and then Has_Rep_Pragma (Interface_Alias (Subp), Name_Implemented)
10161 then
10162 Check_Pragma_Implemented (Subp);
10163 end if;
10165 -- Subp is an interface primitive which overrides another interface
10166 -- primitive marked with pragma Implemented.
10168 if Ada_Version >= Ada_2012
10169 and then Present (Overridden_Operation (Subp))
10170 and then Has_Rep_Pragma
10171 (Overridden_Operation (Subp), Name_Implemented)
10172 then
10173 -- If the overriding routine is also marked by Implemented, check
10174 -- that the two implementation kinds are conforming.
10176 if Has_Rep_Pragma (Subp, Name_Implemented) then
10177 Check_Pragma_Implemented
10178 (Subp => Subp,
10179 Iface_Subp => Overridden_Operation (Subp));
10181 -- Otherwise the overriding routine inherits the implementation
10182 -- kind from the overridden subprogram.
10184 else
10185 Inherit_Pragma_Implemented
10186 (Subp => Subp,
10187 Iface_Subp => Overridden_Operation (Subp));
10188 end if;
10189 end if;
10191 -- If the operation is a wrapper for a synchronized primitive, it
10192 -- may be called indirectly through a dispatching select. We assume
10193 -- that it will be referenced elsewhere indirectly, and suppress
10194 -- warnings about an unused entity.
10196 if Is_Primitive_Wrapper (Subp)
10197 and then Present (Wrapped_Entity (Subp))
10198 then
10199 Set_Referenced (Wrapped_Entity (Subp));
10200 end if;
10202 Next_Elmt (Elmt);
10203 end loop;
10204 end Check_Abstract_Overriding;
10206 ------------------------------------------------
10207 -- Check_Access_Discriminant_Requires_Limited --
10208 ------------------------------------------------
10210 procedure Check_Access_Discriminant_Requires_Limited
10211 (D : Node_Id;
10212 Loc : Node_Id)
10214 begin
10215 -- A discriminant_specification for an access discriminant shall appear
10216 -- only in the declaration for a task or protected type, or for a type
10217 -- with the reserved word 'limited' in its definition or in one of its
10218 -- ancestors (RM 3.7(10)).
10220 -- AI-0063: The proper condition is that type must be immutably limited,
10221 -- or else be a partial view.
10223 if Nkind (Discriminant_Type (D)) = N_Access_Definition then
10224 if Is_Limited_View (Current_Scope)
10225 or else
10226 (Nkind (Parent (Current_Scope)) = N_Private_Type_Declaration
10227 and then Limited_Present (Parent (Current_Scope)))
10228 then
10229 null;
10231 else
10232 Error_Msg_N
10233 ("access discriminants allowed only for limited types", Loc);
10234 end if;
10235 end if;
10236 end Check_Access_Discriminant_Requires_Limited;
10238 -----------------------------------
10239 -- Check_Aliased_Component_Types --
10240 -----------------------------------
10242 procedure Check_Aliased_Component_Types (T : Entity_Id) is
10243 C : Entity_Id;
10245 begin
10246 -- ??? Also need to check components of record extensions, but not
10247 -- components of protected types (which are always limited).
10249 -- Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
10250 -- types to be unconstrained. This is safe because it is illegal to
10251 -- create access subtypes to such types with explicit discriminant
10252 -- constraints.
10254 if not Is_Limited_Type (T) then
10255 if Ekind (T) = E_Record_Type then
10256 C := First_Component (T);
10257 while Present (C) loop
10258 if Is_Aliased (C)
10259 and then Has_Discriminants (Etype (C))
10260 and then not Is_Constrained (Etype (C))
10261 and then not In_Instance_Body
10262 and then Ada_Version < Ada_2005
10263 then
10264 Error_Msg_N
10265 ("aliased component must be constrained (RM 3.6(11))",
10267 end if;
10269 Next_Component (C);
10270 end loop;
10272 elsif Ekind (T) = E_Array_Type then
10273 if Has_Aliased_Components (T)
10274 and then Has_Discriminants (Component_Type (T))
10275 and then not Is_Constrained (Component_Type (T))
10276 and then not In_Instance_Body
10277 and then Ada_Version < Ada_2005
10278 then
10279 Error_Msg_N
10280 ("aliased component type must be constrained (RM 3.6(11))",
10282 end if;
10283 end if;
10284 end if;
10285 end Check_Aliased_Component_Types;
10287 ---------------------------------------
10288 -- Check_Anonymous_Access_Components --
10289 ---------------------------------------
10291 procedure Check_Anonymous_Access_Components
10292 (Typ_Decl : Node_Id;
10293 Typ : Entity_Id;
10294 Prev : Entity_Id;
10295 Comp_List : Node_Id)
10297 Loc : constant Source_Ptr := Sloc (Typ_Decl);
10298 Anon_Access : Entity_Id;
10299 Acc_Def : Node_Id;
10300 Comp : Node_Id;
10301 Comp_Def : Node_Id;
10302 Decl : Node_Id;
10303 Type_Def : Node_Id;
10305 procedure Build_Incomplete_Type_Declaration;
10306 -- If the record type contains components that include an access to the
10307 -- current record, then create an incomplete type declaration for the
10308 -- record, to be used as the designated type of the anonymous access.
10309 -- This is done only once, and only if there is no previous partial
10310 -- view of the type.
10312 function Designates_T (Subt : Node_Id) return Boolean;
10313 -- Check whether a node designates the enclosing record type, or 'Class
10314 -- of that type
10316 function Mentions_T (Acc_Def : Node_Id) return Boolean;
10317 -- Check whether an access definition includes a reference to
10318 -- the enclosing record type. The reference can be a subtype mark
10319 -- in the access definition itself, a 'Class attribute reference, or
10320 -- recursively a reference appearing in a parameter specification
10321 -- or result definition of an access_to_subprogram definition.
10323 --------------------------------------
10324 -- Build_Incomplete_Type_Declaration --
10325 --------------------------------------
10327 procedure Build_Incomplete_Type_Declaration is
10328 Decl : Node_Id;
10329 Inc_T : Entity_Id;
10330 H : Entity_Id;
10332 -- Is_Tagged indicates whether the type is tagged. It is tagged if
10333 -- it's "is new ... with record" or else "is tagged record ...".
10335 Is_Tagged : constant Boolean :=
10336 (Nkind (Type_Definition (Typ_Decl)) = N_Derived_Type_Definition
10337 and then
10338 Present (Record_Extension_Part (Type_Definition (Typ_Decl))))
10339 or else
10340 (Nkind (Type_Definition (Typ_Decl)) = N_Record_Definition
10341 and then Tagged_Present (Type_Definition (Typ_Decl)));
10343 begin
10344 -- If there is a previous partial view, no need to create a new one
10345 -- If the partial view, given by Prev, is incomplete, If Prev is
10346 -- a private declaration, full declaration is flagged accordingly.
10348 if Prev /= Typ then
10349 if Is_Tagged then
10350 Make_Class_Wide_Type (Prev);
10351 Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
10352 Set_Etype (Class_Wide_Type (Typ), Typ);
10353 end if;
10355 return;
10357 elsif Has_Private_Declaration (Typ) then
10359 -- If we refer to T'Class inside T, and T is the completion of a
10360 -- private type, then make sure the class-wide type exists.
10362 if Is_Tagged then
10363 Make_Class_Wide_Type (Typ);
10364 end if;
10366 return;
10368 -- If there was a previous anonymous access type, the incomplete
10369 -- type declaration will have been created already.
10371 elsif Present (Current_Entity (Typ))
10372 and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
10373 and then Full_View (Current_Entity (Typ)) = Typ
10374 then
10375 if Is_Tagged
10376 and then Comes_From_Source (Current_Entity (Typ))
10377 and then not Is_Tagged_Type (Current_Entity (Typ))
10378 then
10379 Make_Class_Wide_Type (Typ);
10380 Error_Msg_N
10381 ("incomplete view of tagged type should be declared tagged??",
10382 Parent (Current_Entity (Typ)));
10383 end if;
10384 return;
10386 else
10387 Inc_T := Make_Defining_Identifier (Loc, Chars (Typ));
10388 Decl := Make_Incomplete_Type_Declaration (Loc, Inc_T);
10390 -- Type has already been inserted into the current scope. Remove
10391 -- it, and add incomplete declaration for type, so that subsequent
10392 -- anonymous access types can use it. The entity is unchained from
10393 -- the homonym list and from immediate visibility. After analysis,
10394 -- the entity in the incomplete declaration becomes immediately
10395 -- visible in the record declaration that follows.
10397 H := Current_Entity (Typ);
10399 if H = Typ then
10400 Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
10401 else
10402 while Present (H)
10403 and then Homonym (H) /= Typ
10404 loop
10405 H := Homonym (Typ);
10406 end loop;
10408 Set_Homonym (H, Homonym (Typ));
10409 end if;
10411 Insert_Before (Typ_Decl, Decl);
10412 Analyze (Decl);
10413 Set_Full_View (Inc_T, Typ);
10415 if Is_Tagged then
10417 -- Create a common class-wide type for both views, and set the
10418 -- Etype of the class-wide type to the full view.
10420 Make_Class_Wide_Type (Inc_T);
10421 Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
10422 Set_Etype (Class_Wide_Type (Typ), Typ);
10423 end if;
10424 end if;
10425 end Build_Incomplete_Type_Declaration;
10427 ------------------
10428 -- Designates_T --
10429 ------------------
10431 function Designates_T (Subt : Node_Id) return Boolean is
10432 Type_Id : constant Name_Id := Chars (Typ);
10434 function Names_T (Nam : Node_Id) return Boolean;
10435 -- The record type has not been introduced in the current scope
10436 -- yet, so we must examine the name of the type itself, either
10437 -- an identifier T, or an expanded name of the form P.T, where
10438 -- P denotes the current scope.
10440 -------------
10441 -- Names_T --
10442 -------------
10444 function Names_T (Nam : Node_Id) return Boolean is
10445 begin
10446 if Nkind (Nam) = N_Identifier then
10447 return Chars (Nam) = Type_Id;
10449 elsif Nkind (Nam) = N_Selected_Component then
10450 if Chars (Selector_Name (Nam)) = Type_Id then
10451 if Nkind (Prefix (Nam)) = N_Identifier then
10452 return Chars (Prefix (Nam)) = Chars (Current_Scope);
10454 elsif Nkind (Prefix (Nam)) = N_Selected_Component then
10455 return Chars (Selector_Name (Prefix (Nam))) =
10456 Chars (Current_Scope);
10457 else
10458 return False;
10459 end if;
10461 else
10462 return False;
10463 end if;
10465 else
10466 return False;
10467 end if;
10468 end Names_T;
10470 -- Start of processing for Designates_T
10472 begin
10473 if Nkind (Subt) = N_Identifier then
10474 return Chars (Subt) = Type_Id;
10476 -- Reference can be through an expanded name which has not been
10477 -- analyzed yet, and which designates enclosing scopes.
10479 elsif Nkind (Subt) = N_Selected_Component then
10480 if Names_T (Subt) then
10481 return True;
10483 -- Otherwise it must denote an entity that is already visible.
10484 -- The access definition may name a subtype of the enclosing
10485 -- type, if there is a previous incomplete declaration for it.
10487 else
10488 Find_Selected_Component (Subt);
10489 return
10490 Is_Entity_Name (Subt)
10491 and then Scope (Entity (Subt)) = Current_Scope
10492 and then
10493 (Chars (Base_Type (Entity (Subt))) = Type_Id
10494 or else
10495 (Is_Class_Wide_Type (Entity (Subt))
10496 and then
10497 Chars (Etype (Base_Type (Entity (Subt)))) =
10498 Type_Id));
10499 end if;
10501 -- A reference to the current type may appear as the prefix of
10502 -- a 'Class attribute.
10504 elsif Nkind (Subt) = N_Attribute_Reference
10505 and then Attribute_Name (Subt) = Name_Class
10506 then
10507 return Names_T (Prefix (Subt));
10509 else
10510 return False;
10511 end if;
10512 end Designates_T;
10514 ----------------
10515 -- Mentions_T --
10516 ----------------
10518 function Mentions_T (Acc_Def : Node_Id) return Boolean is
10519 Param_Spec : Node_Id;
10521 Acc_Subprg : constant Node_Id :=
10522 Access_To_Subprogram_Definition (Acc_Def);
10524 begin
10525 if No (Acc_Subprg) then
10526 return Designates_T (Subtype_Mark (Acc_Def));
10527 end if;
10529 -- Component is an access_to_subprogram: examine its formals,
10530 -- and result definition in the case of an access_to_function.
10532 Param_Spec := First (Parameter_Specifications (Acc_Subprg));
10533 while Present (Param_Spec) loop
10534 if Nkind (Parameter_Type (Param_Spec)) = N_Access_Definition
10535 and then Mentions_T (Parameter_Type (Param_Spec))
10536 then
10537 return True;
10539 elsif Designates_T (Parameter_Type (Param_Spec)) then
10540 return True;
10541 end if;
10543 Next (Param_Spec);
10544 end loop;
10546 if Nkind (Acc_Subprg) = N_Access_Function_Definition then
10547 if Nkind (Result_Definition (Acc_Subprg)) =
10548 N_Access_Definition
10549 then
10550 return Mentions_T (Result_Definition (Acc_Subprg));
10551 else
10552 return Designates_T (Result_Definition (Acc_Subprg));
10553 end if;
10554 end if;
10556 return False;
10557 end Mentions_T;
10559 -- Start of processing for Check_Anonymous_Access_Components
10561 begin
10562 if No (Comp_List) then
10563 return;
10564 end if;
10566 Comp := First (Component_Items (Comp_List));
10567 while Present (Comp) loop
10568 if Nkind (Comp) = N_Component_Declaration
10569 and then Present
10570 (Access_Definition (Component_Definition (Comp)))
10571 and then
10572 Mentions_T (Access_Definition (Component_Definition (Comp)))
10573 then
10574 Comp_Def := Component_Definition (Comp);
10575 Acc_Def :=
10576 Access_To_Subprogram_Definition (Access_Definition (Comp_Def));
10578 Build_Incomplete_Type_Declaration;
10579 Anon_Access := Make_Temporary (Loc, 'S');
10581 -- Create a declaration for the anonymous access type: either
10582 -- an access_to_object or an access_to_subprogram.
10584 if Present (Acc_Def) then
10585 if Nkind (Acc_Def) = N_Access_Function_Definition then
10586 Type_Def :=
10587 Make_Access_Function_Definition (Loc,
10588 Parameter_Specifications =>
10589 Parameter_Specifications (Acc_Def),
10590 Result_Definition => Result_Definition (Acc_Def));
10591 else
10592 Type_Def :=
10593 Make_Access_Procedure_Definition (Loc,
10594 Parameter_Specifications =>
10595 Parameter_Specifications (Acc_Def));
10596 end if;
10598 else
10599 Type_Def :=
10600 Make_Access_To_Object_Definition (Loc,
10601 Subtype_Indication =>
10602 Relocate_Node
10603 (Subtype_Mark (Access_Definition (Comp_Def))));
10605 Set_Constant_Present
10606 (Type_Def, Constant_Present (Access_Definition (Comp_Def)));
10607 Set_All_Present
10608 (Type_Def, All_Present (Access_Definition (Comp_Def)));
10609 end if;
10611 Set_Null_Exclusion_Present
10612 (Type_Def,
10613 Null_Exclusion_Present (Access_Definition (Comp_Def)));
10615 Decl :=
10616 Make_Full_Type_Declaration (Loc,
10617 Defining_Identifier => Anon_Access,
10618 Type_Definition => Type_Def);
10620 Insert_Before (Typ_Decl, Decl);
10621 Analyze (Decl);
10623 -- If an access to subprogram, create the extra formals
10625 if Present (Acc_Def) then
10626 Create_Extra_Formals (Designated_Type (Anon_Access));
10628 -- If an access to object, preserve entity of designated type,
10629 -- for ASIS use, before rewriting the component definition.
10631 else
10632 declare
10633 Desig : Entity_Id;
10635 begin
10636 Desig := Entity (Subtype_Indication (Type_Def));
10638 -- If the access definition is to the current record,
10639 -- the visible entity at this point is an incomplete
10640 -- type. Retrieve the full view to simplify ASIS queries
10642 if Ekind (Desig) = E_Incomplete_Type then
10643 Desig := Full_View (Desig);
10644 end if;
10646 Set_Entity
10647 (Subtype_Mark (Access_Definition (Comp_Def)), Desig);
10648 end;
10649 end if;
10651 Rewrite (Comp_Def,
10652 Make_Component_Definition (Loc,
10653 Subtype_Indication =>
10654 New_Occurrence_Of (Anon_Access, Loc)));
10656 if Ekind (Designated_Type (Anon_Access)) = E_Subprogram_Type then
10657 Set_Ekind (Anon_Access, E_Anonymous_Access_Subprogram_Type);
10658 else
10659 Set_Ekind (Anon_Access, E_Anonymous_Access_Type);
10660 end if;
10662 Set_Is_Local_Anonymous_Access (Anon_Access);
10663 end if;
10665 Next (Comp);
10666 end loop;
10668 if Present (Variant_Part (Comp_List)) then
10669 declare
10670 V : Node_Id;
10671 begin
10672 V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
10673 while Present (V) loop
10674 Check_Anonymous_Access_Components
10675 (Typ_Decl, Typ, Prev, Component_List (V));
10676 Next_Non_Pragma (V);
10677 end loop;
10678 end;
10679 end if;
10680 end Check_Anonymous_Access_Components;
10682 ----------------------
10683 -- Check_Completion --
10684 ----------------------
10686 procedure Check_Completion (Body_Id : Node_Id := Empty) is
10687 E : Entity_Id;
10689 procedure Post_Error;
10690 -- Post error message for lack of completion for entity E
10692 ----------------
10693 -- Post_Error --
10694 ----------------
10696 procedure Post_Error is
10698 procedure Missing_Body;
10699 -- Output missing body message
10701 ------------------
10702 -- Missing_Body --
10703 ------------------
10705 procedure Missing_Body is
10706 begin
10707 -- Spec is in same unit, so we can post on spec
10709 if In_Same_Source_Unit (Body_Id, E) then
10710 Error_Msg_N ("missing body for &", E);
10712 -- Spec is in a separate unit, so we have to post on the body
10714 else
10715 Error_Msg_NE ("missing body for & declared#!", Body_Id, E);
10716 end if;
10717 end Missing_Body;
10719 -- Start of processing for Post_Error
10721 begin
10722 if not Comes_From_Source (E) then
10724 if Ekind_In (E, E_Task_Type, E_Protected_Type) then
10726 -- It may be an anonymous protected type created for a
10727 -- single variable. Post error on variable, if present.
10729 declare
10730 Var : Entity_Id;
10732 begin
10733 Var := First_Entity (Current_Scope);
10734 while Present (Var) loop
10735 exit when Etype (Var) = E
10736 and then Comes_From_Source (Var);
10738 Next_Entity (Var);
10739 end loop;
10741 if Present (Var) then
10742 E := Var;
10743 end if;
10744 end;
10745 end if;
10746 end if;
10748 -- If a generated entity has no completion, then either previous
10749 -- semantic errors have disabled the expansion phase, or else we had
10750 -- missing subunits, or else we are compiling without expansion,
10751 -- or else something is very wrong.
10753 if not Comes_From_Source (E) then
10754 pragma Assert
10755 (Serious_Errors_Detected > 0
10756 or else Configurable_Run_Time_Violations > 0
10757 or else Subunits_Missing
10758 or else not Expander_Active);
10759 return;
10761 -- Here for source entity
10763 else
10764 -- Here if no body to post the error message, so we post the error
10765 -- on the declaration that has no completion. This is not really
10766 -- the right place to post it, think about this later ???
10768 if No (Body_Id) then
10769 if Is_Type (E) then
10770 Error_Msg_NE
10771 ("missing full declaration for }", Parent (E), E);
10772 else
10773 Error_Msg_NE ("missing body for &", Parent (E), E);
10774 end if;
10776 -- Package body has no completion for a declaration that appears
10777 -- in the corresponding spec. Post error on the body, with a
10778 -- reference to the non-completed declaration.
10780 else
10781 Error_Msg_Sloc := Sloc (E);
10783 if Is_Type (E) then
10784 Error_Msg_NE ("missing full declaration for }!", Body_Id, E);
10786 elsif Is_Overloadable (E)
10787 and then Current_Entity_In_Scope (E) /= E
10788 then
10789 -- It may be that the completion is mistyped and appears as
10790 -- a distinct overloading of the entity.
10792 declare
10793 Candidate : constant Entity_Id :=
10794 Current_Entity_In_Scope (E);
10795 Decl : constant Node_Id :=
10796 Unit_Declaration_Node (Candidate);
10798 begin
10799 if Is_Overloadable (Candidate)
10800 and then Ekind (Candidate) = Ekind (E)
10801 and then Nkind (Decl) = N_Subprogram_Body
10802 and then Acts_As_Spec (Decl)
10803 then
10804 Check_Type_Conformant (Candidate, E);
10806 else
10807 Missing_Body;
10808 end if;
10809 end;
10811 else
10812 Missing_Body;
10813 end if;
10814 end if;
10815 end if;
10816 end Post_Error;
10818 -- Local variables
10820 Pack_Id : constant Entity_Id := Current_Scope;
10822 -- Start of processing for Check_Completion
10824 begin
10825 E := First_Entity (Pack_Id);
10826 while Present (E) loop
10827 if Is_Intrinsic_Subprogram (E) then
10828 null;
10830 -- A Ghost entity declared in a non-Ghost package does not force the
10831 -- need for a body (SPARK RM 6.9(11)).
10833 elsif not Is_Ghost_Entity (Pack_Id) and then Is_Ghost_Entity (E) then
10834 null;
10836 -- The following situation requires special handling: a child unit
10837 -- that appears in the context clause of the body of its parent:
10839 -- procedure Parent.Child (...);
10841 -- with Parent.Child;
10842 -- package body Parent is
10844 -- Here Parent.Child appears as a local entity, but should not be
10845 -- flagged as requiring completion, because it is a compilation
10846 -- unit.
10848 -- Ignore missing completion for a subprogram that does not come from
10849 -- source (including the _Call primitive operation of RAS types,
10850 -- which has to have the flag Comes_From_Source for other purposes):
10851 -- we assume that the expander will provide the missing completion.
10852 -- In case of previous errors, other expansion actions that provide
10853 -- bodies for null procedures with not be invoked, so inhibit message
10854 -- in those cases.
10856 -- Note that E_Operator is not in the list that follows, because
10857 -- this kind is reserved for predefined operators, that are
10858 -- intrinsic and do not need completion.
10860 elsif Ekind_In (E, E_Function,
10861 E_Procedure,
10862 E_Generic_Function,
10863 E_Generic_Procedure)
10864 then
10865 if Has_Completion (E) then
10866 null;
10868 elsif Is_Subprogram (E) and then Is_Abstract_Subprogram (E) then
10869 null;
10871 elsif Is_Subprogram (E)
10872 and then (not Comes_From_Source (E)
10873 or else Chars (E) = Name_uCall)
10874 then
10875 null;
10877 elsif
10878 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
10879 then
10880 null;
10882 elsif Nkind (Parent (E)) = N_Procedure_Specification
10883 and then Null_Present (Parent (E))
10884 and then Serious_Errors_Detected > 0
10885 then
10886 null;
10888 else
10889 Post_Error;
10890 end if;
10892 elsif Is_Entry (E) then
10893 if not Has_Completion (E) and then
10894 (Ekind (Scope (E)) = E_Protected_Object
10895 or else Ekind (Scope (E)) = E_Protected_Type)
10896 then
10897 Post_Error;
10898 end if;
10900 elsif Is_Package_Or_Generic_Package (E) then
10901 if Unit_Requires_Body (E) then
10902 if not Has_Completion (E)
10903 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
10904 N_Compilation_Unit
10905 then
10906 Post_Error;
10907 end if;
10909 elsif not Is_Child_Unit (E) then
10910 May_Need_Implicit_Body (E);
10911 end if;
10913 -- A formal incomplete type (Ada 2012) does not require a completion;
10914 -- other incomplete type declarations do.
10916 elsif Ekind (E) = E_Incomplete_Type
10917 and then No (Underlying_Type (E))
10918 and then not Is_Generic_Type (E)
10919 then
10920 Post_Error;
10922 elsif Ekind_In (E, E_Task_Type, E_Protected_Type)
10923 and then not Has_Completion (E)
10924 then
10925 Post_Error;
10927 -- A single task declared in the current scope is a constant, verify
10928 -- that the body of its anonymous type is in the same scope. If the
10929 -- task is defined elsewhere, this may be a renaming declaration for
10930 -- which no completion is needed.
10932 elsif Ekind (E) = E_Constant
10933 and then Ekind (Etype (E)) = E_Task_Type
10934 and then not Has_Completion (Etype (E))
10935 and then Scope (Etype (E)) = Current_Scope
10936 then
10937 Post_Error;
10939 elsif Ekind (E) = E_Protected_Object
10940 and then not Has_Completion (Etype (E))
10941 then
10942 Post_Error;
10944 elsif Ekind (E) = E_Record_Type then
10945 if Is_Tagged_Type (E) then
10946 Check_Abstract_Overriding (E);
10947 Check_Conventions (E);
10948 end if;
10950 Check_Aliased_Component_Types (E);
10952 elsif Ekind (E) = E_Array_Type then
10953 Check_Aliased_Component_Types (E);
10955 end if;
10957 Next_Entity (E);
10958 end loop;
10959 end Check_Completion;
10961 ------------------------------------
10962 -- Check_CPP_Type_Has_No_Defaults --
10963 ------------------------------------
10965 procedure Check_CPP_Type_Has_No_Defaults (T : Entity_Id) is
10966 Tdef : constant Node_Id := Type_Definition (Declaration_Node (T));
10967 Clist : Node_Id;
10968 Comp : Node_Id;
10970 begin
10971 -- Obtain the component list
10973 if Nkind (Tdef) = N_Record_Definition then
10974 Clist := Component_List (Tdef);
10975 else pragma Assert (Nkind (Tdef) = N_Derived_Type_Definition);
10976 Clist := Component_List (Record_Extension_Part (Tdef));
10977 end if;
10979 -- Check all components to ensure no default expressions
10981 if Present (Clist) then
10982 Comp := First (Component_Items (Clist));
10983 while Present (Comp) loop
10984 if Present (Expression (Comp)) then
10985 Error_Msg_N
10986 ("component of imported 'C'P'P type cannot have "
10987 & "default expression", Expression (Comp));
10988 end if;
10990 Next (Comp);
10991 end loop;
10992 end if;
10993 end Check_CPP_Type_Has_No_Defaults;
10995 ----------------------------
10996 -- Check_Delta_Expression --
10997 ----------------------------
10999 procedure Check_Delta_Expression (E : Node_Id) is
11000 begin
11001 if not (Is_Real_Type (Etype (E))) then
11002 Wrong_Type (E, Any_Real);
11004 elsif not Is_OK_Static_Expression (E) then
11005 Flag_Non_Static_Expr
11006 ("non-static expression used for delta value!", E);
11008 elsif not UR_Is_Positive (Expr_Value_R (E)) then
11009 Error_Msg_N ("delta expression must be positive", E);
11011 else
11012 return;
11013 end if;
11015 -- If any of above errors occurred, then replace the incorrect
11016 -- expression by the real 0.1, which should prevent further errors.
11018 Rewrite (E,
11019 Make_Real_Literal (Sloc (E), Ureal_Tenth));
11020 Analyze_And_Resolve (E, Standard_Float);
11021 end Check_Delta_Expression;
11023 -----------------------------
11024 -- Check_Digits_Expression --
11025 -----------------------------
11027 procedure Check_Digits_Expression (E : Node_Id) is
11028 begin
11029 if not (Is_Integer_Type (Etype (E))) then
11030 Wrong_Type (E, Any_Integer);
11032 elsif not Is_OK_Static_Expression (E) then
11033 Flag_Non_Static_Expr
11034 ("non-static expression used for digits value!", E);
11036 elsif Expr_Value (E) <= 0 then
11037 Error_Msg_N ("digits value must be greater than zero", E);
11039 else
11040 return;
11041 end if;
11043 -- If any of above errors occurred, then replace the incorrect
11044 -- expression by the integer 1, which should prevent further errors.
11046 Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
11047 Analyze_And_Resolve (E, Standard_Integer);
11049 end Check_Digits_Expression;
11051 --------------------------
11052 -- Check_Initialization --
11053 --------------------------
11055 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
11056 begin
11057 -- Special processing for limited types
11059 if Is_Limited_Type (T)
11060 and then not In_Instance
11061 and then not In_Inlined_Body
11062 then
11063 if not OK_For_Limited_Init (T, Exp) then
11065 -- In GNAT mode, this is just a warning, to allow it to be evilly
11066 -- turned off. Otherwise it is a real error.
11068 if GNAT_Mode then
11069 Error_Msg_N
11070 ("??cannot initialize entities of limited type!", Exp);
11072 elsif Ada_Version < Ada_2005 then
11074 -- The side effect removal machinery may generate illegal Ada
11075 -- code to avoid the usage of access types and 'reference in
11076 -- SPARK mode. Since this is legal code with respect to theorem
11077 -- proving, do not emit the error.
11079 if GNATprove_Mode
11080 and then Nkind (Exp) = N_Function_Call
11081 and then Nkind (Parent (Exp)) = N_Object_Declaration
11082 and then not Comes_From_Source
11083 (Defining_Identifier (Parent (Exp)))
11084 then
11085 null;
11087 else
11088 Error_Msg_N
11089 ("cannot initialize entities of limited type", Exp);
11090 Explain_Limited_Type (T, Exp);
11091 end if;
11093 else
11094 -- Specialize error message according to kind of illegal
11095 -- initial expression.
11097 if Nkind (Exp) = N_Type_Conversion
11098 and then Nkind (Expression (Exp)) = N_Function_Call
11099 then
11100 Error_Msg_N
11101 ("illegal context for call"
11102 & " to function with limited result", Exp);
11104 else
11105 Error_Msg_N
11106 ("initialization of limited object requires aggregate "
11107 & "or function call", Exp);
11108 end if;
11109 end if;
11110 end if;
11111 end if;
11113 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag gets
11114 -- set unless we can be sure that no range check is required.
11116 if (GNATprove_Mode or not Expander_Active)
11117 and then Is_Scalar_Type (T)
11118 and then not Is_In_Range (Exp, T, Assume_Valid => True)
11119 then
11120 Set_Do_Range_Check (Exp);
11121 end if;
11122 end Check_Initialization;
11124 ----------------------
11125 -- Check_Interfaces --
11126 ----------------------
11128 procedure Check_Interfaces (N : Node_Id; Def : Node_Id) is
11129 Parent_Type : constant Entity_Id := Etype (Defining_Identifier (N));
11131 Iface : Node_Id;
11132 Iface_Def : Node_Id;
11133 Iface_Typ : Entity_Id;
11134 Parent_Node : Node_Id;
11136 Is_Task : Boolean := False;
11137 -- Set True if parent type or any progenitor is a task interface
11139 Is_Protected : Boolean := False;
11140 -- Set True if parent type or any progenitor is a protected interface
11142 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
11143 -- Check that a progenitor is compatible with declaration. If an error
11144 -- message is output, it is posted on Error_Node.
11146 ------------------
11147 -- Check_Ifaces --
11148 ------------------
11150 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
11151 Iface_Id : constant Entity_Id :=
11152 Defining_Identifier (Parent (Iface_Def));
11153 Type_Def : Node_Id;
11155 begin
11156 if Nkind (N) = N_Private_Extension_Declaration then
11157 Type_Def := N;
11158 else
11159 Type_Def := Type_Definition (N);
11160 end if;
11162 if Is_Task_Interface (Iface_Id) then
11163 Is_Task := True;
11165 elsif Is_Protected_Interface (Iface_Id) then
11166 Is_Protected := True;
11167 end if;
11169 if Is_Synchronized_Interface (Iface_Id) then
11171 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
11172 -- extension derived from a synchronized interface must explicitly
11173 -- be declared synchronized, because the full view will be a
11174 -- synchronized type.
11176 if Nkind (N) = N_Private_Extension_Declaration then
11177 if not Synchronized_Present (N) then
11178 Error_Msg_NE
11179 ("private extension of& must be explicitly synchronized",
11180 N, Iface_Id);
11181 end if;
11183 -- However, by 3.9.4(16/2), a full type that is a record extension
11184 -- is never allowed to derive from a synchronized interface (note
11185 -- that interfaces must be excluded from this check, because those
11186 -- are represented by derived type definitions in some cases).
11188 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition
11189 and then not Interface_Present (Type_Definition (N))
11190 then
11191 Error_Msg_N ("record extension cannot derive from synchronized "
11192 & "interface", Error_Node);
11193 end if;
11194 end if;
11196 -- Check that the characteristics of the progenitor are compatible
11197 -- with the explicit qualifier in the declaration.
11198 -- The check only applies to qualifiers that come from source.
11199 -- Limited_Present also appears in the declaration of corresponding
11200 -- records, and the check does not apply to them.
11202 if Limited_Present (Type_Def)
11203 and then not
11204 Is_Concurrent_Record_Type (Defining_Identifier (N))
11205 then
11206 if Is_Limited_Interface (Parent_Type)
11207 and then not Is_Limited_Interface (Iface_Id)
11208 then
11209 Error_Msg_NE
11210 ("progenitor & must be limited interface",
11211 Error_Node, Iface_Id);
11213 elsif
11214 (Task_Present (Iface_Def)
11215 or else Protected_Present (Iface_Def)
11216 or else Synchronized_Present (Iface_Def))
11217 and then Nkind (N) /= N_Private_Extension_Declaration
11218 and then not Error_Posted (N)
11219 then
11220 Error_Msg_NE
11221 ("progenitor & must be limited interface",
11222 Error_Node, Iface_Id);
11223 end if;
11225 -- Protected interfaces can only inherit from limited, synchronized
11226 -- or protected interfaces.
11228 elsif Nkind (N) = N_Full_Type_Declaration
11229 and then Protected_Present (Type_Def)
11230 then
11231 if Limited_Present (Iface_Def)
11232 or else Synchronized_Present (Iface_Def)
11233 or else Protected_Present (Iface_Def)
11234 then
11235 null;
11237 elsif Task_Present (Iface_Def) then
11238 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
11239 & "from task interface", Error_Node);
11241 else
11242 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
11243 & "from non-limited interface", Error_Node);
11244 end if;
11246 -- Ada 2005 (AI-345): Synchronized interfaces can only inherit from
11247 -- limited and synchronized.
11249 elsif Synchronized_Present (Type_Def) then
11250 if Limited_Present (Iface_Def)
11251 or else Synchronized_Present (Iface_Def)
11252 then
11253 null;
11255 elsif Protected_Present (Iface_Def)
11256 and then Nkind (N) /= N_Private_Extension_Declaration
11257 then
11258 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11259 & "from protected interface", Error_Node);
11261 elsif Task_Present (Iface_Def)
11262 and then Nkind (N) /= N_Private_Extension_Declaration
11263 then
11264 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11265 & "from task interface", Error_Node);
11267 elsif not Is_Limited_Interface (Iface_Id) then
11268 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11269 & "from non-limited interface", Error_Node);
11270 end if;
11272 -- Ada 2005 (AI-345): Task interfaces can only inherit from limited,
11273 -- synchronized or task interfaces.
11275 elsif Nkind (N) = N_Full_Type_Declaration
11276 and then Task_Present (Type_Def)
11277 then
11278 if Limited_Present (Iface_Def)
11279 or else Synchronized_Present (Iface_Def)
11280 or else Task_Present (Iface_Def)
11281 then
11282 null;
11284 elsif Protected_Present (Iface_Def) then
11285 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
11286 & "protected interface", Error_Node);
11288 else
11289 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
11290 & "non-limited interface", Error_Node);
11291 end if;
11292 end if;
11293 end Check_Ifaces;
11295 -- Start of processing for Check_Interfaces
11297 begin
11298 if Is_Interface (Parent_Type) then
11299 if Is_Task_Interface (Parent_Type) then
11300 Is_Task := True;
11302 elsif Is_Protected_Interface (Parent_Type) then
11303 Is_Protected := True;
11304 end if;
11305 end if;
11307 if Nkind (N) = N_Private_Extension_Declaration then
11309 -- Check that progenitors are compatible with declaration
11311 Iface := First (Interface_List (Def));
11312 while Present (Iface) loop
11313 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
11315 Parent_Node := Parent (Base_Type (Iface_Typ));
11316 Iface_Def := Type_Definition (Parent_Node);
11318 if not Is_Interface (Iface_Typ) then
11319 Diagnose_Interface (Iface, Iface_Typ);
11320 else
11321 Check_Ifaces (Iface_Def, Iface);
11322 end if;
11324 Next (Iface);
11325 end loop;
11327 if Is_Task and Is_Protected then
11328 Error_Msg_N
11329 ("type cannot derive from task and protected interface", N);
11330 end if;
11332 return;
11333 end if;
11335 -- Full type declaration of derived type.
11336 -- Check compatibility with parent if it is interface type
11338 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
11339 and then Is_Interface (Parent_Type)
11340 then
11341 Parent_Node := Parent (Parent_Type);
11343 -- More detailed checks for interface varieties
11345 Check_Ifaces
11346 (Iface_Def => Type_Definition (Parent_Node),
11347 Error_Node => Subtype_Indication (Type_Definition (N)));
11348 end if;
11350 Iface := First (Interface_List (Def));
11351 while Present (Iface) loop
11352 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
11354 Parent_Node := Parent (Base_Type (Iface_Typ));
11355 Iface_Def := Type_Definition (Parent_Node);
11357 if not Is_Interface (Iface_Typ) then
11358 Diagnose_Interface (Iface, Iface_Typ);
11360 else
11361 -- "The declaration of a specific descendant of an interface
11362 -- type freezes the interface type" RM 13.14
11364 Freeze_Before (N, Iface_Typ);
11365 Check_Ifaces (Iface_Def, Error_Node => Iface);
11366 end if;
11368 Next (Iface);
11369 end loop;
11371 if Is_Task and Is_Protected then
11372 Error_Msg_N
11373 ("type cannot derive from task and protected interface", N);
11374 end if;
11375 end Check_Interfaces;
11377 ------------------------------------
11378 -- Check_Or_Process_Discriminants --
11379 ------------------------------------
11381 -- If an incomplete or private type declaration was already given for the
11382 -- type, the discriminants may have already been processed if they were
11383 -- present on the incomplete declaration. In this case a full conformance
11384 -- check has been performed in Find_Type_Name, and we then recheck here
11385 -- some properties that can't be checked on the partial view alone.
11386 -- Otherwise we call Process_Discriminants.
11388 procedure Check_Or_Process_Discriminants
11389 (N : Node_Id;
11390 T : Entity_Id;
11391 Prev : Entity_Id := Empty)
11393 begin
11394 if Has_Discriminants (T) then
11396 -- Discriminants are already set on T if they were already present
11397 -- on the partial view. Make them visible to component declarations.
11399 declare
11400 D : Entity_Id;
11401 -- Discriminant on T (full view) referencing expr on partial view
11403 Prev_D : Entity_Id;
11404 -- Entity of corresponding discriminant on partial view
11406 New_D : Node_Id;
11407 -- Discriminant specification for full view, expression is
11408 -- the syntactic copy on full view (which has been checked for
11409 -- conformance with partial view), only used here to post error
11410 -- message.
11412 begin
11413 D := First_Discriminant (T);
11414 New_D := First (Discriminant_Specifications (N));
11415 while Present (D) loop
11416 Prev_D := Current_Entity (D);
11417 Set_Current_Entity (D);
11418 Set_Is_Immediately_Visible (D);
11419 Set_Homonym (D, Prev_D);
11421 -- Handle the case where there is an untagged partial view and
11422 -- the full view is tagged: must disallow discriminants with
11423 -- defaults, unless compiling for Ada 2012, which allows a
11424 -- limited tagged type to have defaulted discriminants (see
11425 -- AI05-0214). However, suppress error here if it was already
11426 -- reported on the default expression of the partial view.
11428 if Is_Tagged_Type (T)
11429 and then Present (Expression (Parent (D)))
11430 and then (not Is_Limited_Type (Current_Scope)
11431 or else Ada_Version < Ada_2012)
11432 and then not Error_Posted (Expression (Parent (D)))
11433 then
11434 if Ada_Version >= Ada_2012 then
11435 Error_Msg_N
11436 ("discriminants of nonlimited tagged type cannot have "
11437 & "defaults",
11438 Expression (New_D));
11439 else
11440 Error_Msg_N
11441 ("discriminants of tagged type cannot have defaults",
11442 Expression (New_D));
11443 end if;
11444 end if;
11446 -- Ada 2005 (AI-230): Access discriminant allowed in
11447 -- non-limited record types.
11449 if Ada_Version < Ada_2005 then
11451 -- This restriction gets applied to the full type here. It
11452 -- has already been applied earlier to the partial view.
11454 Check_Access_Discriminant_Requires_Limited (Parent (D), N);
11455 end if;
11457 Next_Discriminant (D);
11458 Next (New_D);
11459 end loop;
11460 end;
11462 elsif Present (Discriminant_Specifications (N)) then
11463 Process_Discriminants (N, Prev);
11464 end if;
11465 end Check_Or_Process_Discriminants;
11467 ----------------------
11468 -- Check_Real_Bound --
11469 ----------------------
11471 procedure Check_Real_Bound (Bound : Node_Id) is
11472 begin
11473 if not Is_Real_Type (Etype (Bound)) then
11474 Error_Msg_N
11475 ("bound in real type definition must be of real type", Bound);
11477 elsif not Is_OK_Static_Expression (Bound) then
11478 Flag_Non_Static_Expr
11479 ("non-static expression used for real type bound!", Bound);
11481 else
11482 return;
11483 end if;
11485 Rewrite
11486 (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
11487 Analyze (Bound);
11488 Resolve (Bound, Standard_Float);
11489 end Check_Real_Bound;
11491 ------------------------------
11492 -- Complete_Private_Subtype --
11493 ------------------------------
11495 procedure Complete_Private_Subtype
11496 (Priv : Entity_Id;
11497 Full : Entity_Id;
11498 Full_Base : Entity_Id;
11499 Related_Nod : Node_Id)
11501 Save_Next_Entity : Entity_Id;
11502 Save_Homonym : Entity_Id;
11504 begin
11505 -- Set semantic attributes for (implicit) private subtype completion.
11506 -- If the full type has no discriminants, then it is a copy of the
11507 -- full view of the base. Otherwise, it is a subtype of the base with
11508 -- a possible discriminant constraint. Save and restore the original
11509 -- Next_Entity field of full to ensure that the calls to Copy_Node do
11510 -- not corrupt the entity chain.
11512 -- Note that the type of the full view is the same entity as the type
11513 -- of the partial view. In this fashion, the subtype has access to the
11514 -- correct view of the parent.
11516 Save_Next_Entity := Next_Entity (Full);
11517 Save_Homonym := Homonym (Priv);
11519 case Ekind (Full_Base) is
11520 when E_Record_Type |
11521 E_Record_Subtype |
11522 Class_Wide_Kind |
11523 Private_Kind |
11524 Task_Kind |
11525 Protected_Kind =>
11526 Copy_Node (Priv, Full);
11528 Set_Has_Discriminants
11529 (Full, Has_Discriminants (Full_Base));
11530 Set_Has_Unknown_Discriminants
11531 (Full, Has_Unknown_Discriminants (Full_Base));
11532 Set_First_Entity (Full, First_Entity (Full_Base));
11533 Set_Last_Entity (Full, Last_Entity (Full_Base));
11535 -- If the underlying base type is constrained, we know that the
11536 -- full view of the subtype is constrained as well (the converse
11537 -- is not necessarily true).
11539 if Is_Constrained (Full_Base) then
11540 Set_Is_Constrained (Full);
11541 end if;
11543 when others =>
11544 Copy_Node (Full_Base, Full);
11546 Set_Chars (Full, Chars (Priv));
11547 Conditional_Delay (Full, Priv);
11548 Set_Sloc (Full, Sloc (Priv));
11549 end case;
11551 Set_Next_Entity (Full, Save_Next_Entity);
11552 Set_Homonym (Full, Save_Homonym);
11553 Set_Associated_Node_For_Itype (Full, Related_Nod);
11555 -- Set common attributes for all subtypes: kind, convention, etc.
11557 Set_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
11558 Set_Convention (Full, Convention (Full_Base));
11560 -- The Etype of the full view is inconsistent. Gigi needs to see the
11561 -- structural full view, which is what the current scheme gives: the
11562 -- Etype of the full view is the etype of the full base. However, if the
11563 -- full base is a derived type, the full view then looks like a subtype
11564 -- of the parent, not a subtype of the full base. If instead we write:
11566 -- Set_Etype (Full, Full_Base);
11568 -- then we get inconsistencies in the front-end (confusion between
11569 -- views). Several outstanding bugs are related to this ???
11571 Set_Is_First_Subtype (Full, False);
11572 Set_Scope (Full, Scope (Priv));
11573 Set_Size_Info (Full, Full_Base);
11574 Set_RM_Size (Full, RM_Size (Full_Base));
11575 Set_Is_Itype (Full);
11577 -- A subtype of a private-type-without-discriminants, whose full-view
11578 -- has discriminants with default expressions, is not constrained.
11580 if not Has_Discriminants (Priv) then
11581 Set_Is_Constrained (Full, Is_Constrained (Full_Base));
11583 if Has_Discriminants (Full_Base) then
11584 Set_Discriminant_Constraint
11585 (Full, Discriminant_Constraint (Full_Base));
11587 -- The partial view may have been indefinite, the full view
11588 -- might not be.
11590 Set_Has_Unknown_Discriminants
11591 (Full, Has_Unknown_Discriminants (Full_Base));
11592 end if;
11593 end if;
11595 Set_First_Rep_Item (Full, First_Rep_Item (Full_Base));
11596 Set_Depends_On_Private (Full, Has_Private_Component (Full));
11598 -- Freeze the private subtype entity if its parent is delayed, and not
11599 -- already frozen. We skip this processing if the type is an anonymous
11600 -- subtype of a record component, or is the corresponding record of a
11601 -- protected type, since ???
11603 if not Is_Type (Scope (Full)) then
11604 Set_Has_Delayed_Freeze (Full,
11605 Has_Delayed_Freeze (Full_Base)
11606 and then (not Is_Frozen (Full_Base)));
11607 end if;
11609 Set_Freeze_Node (Full, Empty);
11610 Set_Is_Frozen (Full, False);
11611 Set_Full_View (Priv, Full);
11613 if Has_Discriminants (Full) then
11614 Set_Stored_Constraint_From_Discriminant_Constraint (Full);
11615 Set_Stored_Constraint (Priv, Stored_Constraint (Full));
11617 if Has_Unknown_Discriminants (Full) then
11618 Set_Discriminant_Constraint (Full, No_Elist);
11619 end if;
11620 end if;
11622 if Ekind (Full_Base) = E_Record_Type
11623 and then Has_Discriminants (Full_Base)
11624 and then Has_Discriminants (Priv) -- might not, if errors
11625 and then not Has_Unknown_Discriminants (Priv)
11626 and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
11627 then
11628 Create_Constrained_Components
11629 (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
11631 -- If the full base is itself derived from private, build a congruent
11632 -- subtype of its underlying type, for use by the back end. For a
11633 -- constrained record component, the declaration cannot be placed on
11634 -- the component list, but it must nevertheless be built an analyzed, to
11635 -- supply enough information for Gigi to compute the size of component.
11637 elsif Ekind (Full_Base) in Private_Kind
11638 and then Is_Derived_Type (Full_Base)
11639 and then Has_Discriminants (Full_Base)
11640 and then (Ekind (Current_Scope) /= E_Record_Subtype)
11641 then
11642 if not Is_Itype (Priv)
11643 and then
11644 Nkind (Subtype_Indication (Parent (Priv))) = N_Subtype_Indication
11645 then
11646 Build_Underlying_Full_View
11647 (Parent (Priv), Full, Etype (Full_Base));
11649 elsif Nkind (Related_Nod) = N_Component_Declaration then
11650 Build_Underlying_Full_View (Related_Nod, Full, Etype (Full_Base));
11651 end if;
11653 elsif Is_Record_Type (Full_Base) then
11655 -- Show Full is simply a renaming of Full_Base
11657 Set_Cloned_Subtype (Full, Full_Base);
11658 end if;
11660 -- It is unsafe to share the bounds of a scalar type, because the Itype
11661 -- is elaborated on demand, and if a bound is non-static then different
11662 -- orders of elaboration in different units will lead to different
11663 -- external symbols.
11665 if Is_Scalar_Type (Full_Base) then
11666 Set_Scalar_Range (Full,
11667 Make_Range (Sloc (Related_Nod),
11668 Low_Bound =>
11669 Duplicate_Subexpr_No_Checks (Type_Low_Bound (Full_Base)),
11670 High_Bound =>
11671 Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
11673 -- This completion inherits the bounds of the full parent, but if
11674 -- the parent is an unconstrained floating point type, so is the
11675 -- completion.
11677 if Is_Floating_Point_Type (Full_Base) then
11678 Set_Includes_Infinities
11679 (Scalar_Range (Full), Has_Infinities (Full_Base));
11680 end if;
11681 end if;
11683 -- ??? It seems that a lot of fields are missing that should be copied
11684 -- from Full_Base to Full. Here are some that are introduced in a
11685 -- non-disruptive way but a cleanup is necessary.
11687 if Is_Tagged_Type (Full_Base) then
11688 Set_Is_Tagged_Type (Full);
11689 Set_Direct_Primitive_Operations
11690 (Full, Direct_Primitive_Operations (Full_Base));
11691 Set_No_Tagged_Streams_Pragma
11692 (Full, No_Tagged_Streams_Pragma (Full_Base));
11694 -- Inherit class_wide type of full_base in case the partial view was
11695 -- not tagged. Otherwise it has already been created when the private
11696 -- subtype was analyzed.
11698 if No (Class_Wide_Type (Full)) then
11699 Set_Class_Wide_Type (Full, Class_Wide_Type (Full_Base));
11700 end if;
11702 -- If this is a subtype of a protected or task type, constrain its
11703 -- corresponding record, unless this is a subtype without constraints,
11704 -- i.e. a simple renaming as with an actual subtype in an instance.
11706 elsif Is_Concurrent_Type (Full_Base) then
11707 if Has_Discriminants (Full)
11708 and then Present (Corresponding_Record_Type (Full_Base))
11709 and then
11710 not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
11711 then
11712 Set_Corresponding_Record_Type (Full,
11713 Constrain_Corresponding_Record
11714 (Full, Corresponding_Record_Type (Full_Base), Related_Nod));
11716 else
11717 Set_Corresponding_Record_Type (Full,
11718 Corresponding_Record_Type (Full_Base));
11719 end if;
11720 end if;
11722 -- Link rep item chain, and also setting of Has_Predicates from private
11723 -- subtype to full subtype, since we will need these on the full subtype
11724 -- to create the predicate function. Note that the full subtype may
11725 -- already have rep items, inherited from the full view of the base
11726 -- type, so we must be sure not to overwrite these entries.
11728 declare
11729 Append : Boolean;
11730 Item : Node_Id;
11731 Next_Item : Node_Id;
11733 begin
11734 Item := First_Rep_Item (Full);
11736 -- If no existing rep items on full type, we can just link directly
11737 -- to the list of items on the private type, if any exist.. Same if
11738 -- the rep items are only those inherited from the base
11740 if (No (Item)
11741 or else Nkind (Item) /= N_Aspect_Specification
11742 or else Entity (Item) = Full_Base)
11743 and then Present (First_Rep_Item (Priv))
11744 then
11745 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
11747 -- Otherwise, search to the end of items currently linked to the full
11748 -- subtype and append the private items to the end. However, if Priv
11749 -- and Full already have the same list of rep items, then the append
11750 -- is not done, as that would create a circularity.
11752 elsif Item /= First_Rep_Item (Priv) then
11753 Append := True;
11754 loop
11755 Next_Item := Next_Rep_Item (Item);
11756 exit when No (Next_Item);
11757 Item := Next_Item;
11759 -- If the private view has aspect specifications, the full view
11760 -- inherits them. Since these aspects may already have been
11761 -- attached to the full view during derivation, do not append
11762 -- them if already present.
11764 if Item = First_Rep_Item (Priv) then
11765 Append := False;
11766 exit;
11767 end if;
11768 end loop;
11770 -- And link the private type items at the end of the chain
11772 if Append then
11773 Set_Next_Rep_Item (Item, First_Rep_Item (Priv));
11774 end if;
11775 end if;
11776 end;
11778 -- Make sure Has_Predicates is set on full type if it is set on the
11779 -- private type. Note that it may already be set on the full type and
11780 -- if so, we don't want to unset it.
11782 if Has_Predicates (Priv) then
11783 Set_Has_Predicates (Full);
11784 end if;
11785 end Complete_Private_Subtype;
11787 ----------------------------
11788 -- Constant_Redeclaration --
11789 ----------------------------
11791 procedure Constant_Redeclaration
11792 (Id : Entity_Id;
11793 N : Node_Id;
11794 T : out Entity_Id)
11796 Prev : constant Entity_Id := Current_Entity_In_Scope (Id);
11797 Obj_Def : constant Node_Id := Object_Definition (N);
11798 New_T : Entity_Id;
11800 procedure Check_Possible_Deferred_Completion
11801 (Prev_Id : Entity_Id;
11802 Prev_Obj_Def : Node_Id;
11803 Curr_Obj_Def : Node_Id);
11804 -- Determine whether the two object definitions describe the partial
11805 -- and the full view of a constrained deferred constant. Generate
11806 -- a subtype for the full view and verify that it statically matches
11807 -- the subtype of the partial view.
11809 procedure Check_Recursive_Declaration (Typ : Entity_Id);
11810 -- If deferred constant is an access type initialized with an allocator,
11811 -- check whether there is an illegal recursion in the definition,
11812 -- through a default value of some record subcomponent. This is normally
11813 -- detected when generating init procs, but requires this additional
11814 -- mechanism when expansion is disabled.
11816 ----------------------------------------
11817 -- Check_Possible_Deferred_Completion --
11818 ----------------------------------------
11820 procedure Check_Possible_Deferred_Completion
11821 (Prev_Id : Entity_Id;
11822 Prev_Obj_Def : Node_Id;
11823 Curr_Obj_Def : Node_Id)
11825 begin
11826 if Nkind (Prev_Obj_Def) = N_Subtype_Indication
11827 and then Present (Constraint (Prev_Obj_Def))
11828 and then Nkind (Curr_Obj_Def) = N_Subtype_Indication
11829 and then Present (Constraint (Curr_Obj_Def))
11830 then
11831 declare
11832 Loc : constant Source_Ptr := Sloc (N);
11833 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
11834 Decl : constant Node_Id :=
11835 Make_Subtype_Declaration (Loc,
11836 Defining_Identifier => Def_Id,
11837 Subtype_Indication =>
11838 Relocate_Node (Curr_Obj_Def));
11840 begin
11841 Insert_Before_And_Analyze (N, Decl);
11842 Set_Etype (Id, Def_Id);
11844 if not Subtypes_Statically_Match (Etype (Prev_Id), Def_Id) then
11845 Error_Msg_Sloc := Sloc (Prev_Id);
11846 Error_Msg_N ("subtype does not statically match deferred "
11847 & "declaration #", N);
11848 end if;
11849 end;
11850 end if;
11851 end Check_Possible_Deferred_Completion;
11853 ---------------------------------
11854 -- Check_Recursive_Declaration --
11855 ---------------------------------
11857 procedure Check_Recursive_Declaration (Typ : Entity_Id) is
11858 Comp : Entity_Id;
11860 begin
11861 if Is_Record_Type (Typ) then
11862 Comp := First_Component (Typ);
11863 while Present (Comp) loop
11864 if Comes_From_Source (Comp) then
11865 if Present (Expression (Parent (Comp)))
11866 and then Is_Entity_Name (Expression (Parent (Comp)))
11867 and then Entity (Expression (Parent (Comp))) = Prev
11868 then
11869 Error_Msg_Sloc := Sloc (Parent (Comp));
11870 Error_Msg_NE
11871 ("illegal circularity with declaration for & #",
11872 N, Comp);
11873 return;
11875 elsif Is_Record_Type (Etype (Comp)) then
11876 Check_Recursive_Declaration (Etype (Comp));
11877 end if;
11878 end if;
11880 Next_Component (Comp);
11881 end loop;
11882 end if;
11883 end Check_Recursive_Declaration;
11885 -- Start of processing for Constant_Redeclaration
11887 begin
11888 if Nkind (Parent (Prev)) = N_Object_Declaration then
11889 if Nkind (Object_Definition
11890 (Parent (Prev))) = N_Subtype_Indication
11891 then
11892 -- Find type of new declaration. The constraints of the two
11893 -- views must match statically, but there is no point in
11894 -- creating an itype for the full view.
11896 if Nkind (Obj_Def) = N_Subtype_Indication then
11897 Find_Type (Subtype_Mark (Obj_Def));
11898 New_T := Entity (Subtype_Mark (Obj_Def));
11900 else
11901 Find_Type (Obj_Def);
11902 New_T := Entity (Obj_Def);
11903 end if;
11905 T := Etype (Prev);
11907 else
11908 -- The full view may impose a constraint, even if the partial
11909 -- view does not, so construct the subtype.
11911 New_T := Find_Type_Of_Object (Obj_Def, N);
11912 T := New_T;
11913 end if;
11915 else
11916 -- Current declaration is illegal, diagnosed below in Enter_Name
11918 T := Empty;
11919 New_T := Any_Type;
11920 end if;
11922 -- If previous full declaration or a renaming declaration exists, or if
11923 -- a homograph is present, let Enter_Name handle it, either with an
11924 -- error or with the removal of an overridden implicit subprogram.
11925 -- The previous one is a full declaration if it has an expression
11926 -- (which in the case of an aggregate is indicated by the Init flag).
11928 if Ekind (Prev) /= E_Constant
11929 or else Nkind (Parent (Prev)) = N_Object_Renaming_Declaration
11930 or else Present (Expression (Parent (Prev)))
11931 or else Has_Init_Expression (Parent (Prev))
11932 or else Present (Full_View (Prev))
11933 then
11934 Enter_Name (Id);
11936 -- Verify that types of both declarations match, or else that both types
11937 -- are anonymous access types whose designated subtypes statically match
11938 -- (as allowed in Ada 2005 by AI-385).
11940 elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
11941 and then
11942 (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
11943 or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
11944 or else Is_Access_Constant (Etype (New_T)) /=
11945 Is_Access_Constant (Etype (Prev))
11946 or else Can_Never_Be_Null (Etype (New_T)) /=
11947 Can_Never_Be_Null (Etype (Prev))
11948 or else Null_Exclusion_Present (Parent (Prev)) /=
11949 Null_Exclusion_Present (Parent (Id))
11950 or else not Subtypes_Statically_Match
11951 (Designated_Type (Etype (Prev)),
11952 Designated_Type (Etype (New_T))))
11953 then
11954 Error_Msg_Sloc := Sloc (Prev);
11955 Error_Msg_N ("type does not match declaration#", N);
11956 Set_Full_View (Prev, Id);
11957 Set_Etype (Id, Any_Type);
11959 -- A deferred constant whose type is an anonymous array is always
11960 -- illegal (unless imported). A detailed error message might be
11961 -- helpful for Ada beginners.
11963 if Nkind (Object_Definition (Parent (Prev)))
11964 = N_Constrained_Array_Definition
11965 and then Nkind (Object_Definition (N))
11966 = N_Constrained_Array_Definition
11967 then
11968 Error_Msg_N ("\each anonymous array is a distinct type", N);
11969 Error_Msg_N ("a deferred constant must have a named type",
11970 Object_Definition (Parent (Prev)));
11971 end if;
11973 elsif
11974 Null_Exclusion_Present (Parent (Prev))
11975 and then not Null_Exclusion_Present (N)
11976 then
11977 Error_Msg_Sloc := Sloc (Prev);
11978 Error_Msg_N ("null-exclusion does not match declaration#", N);
11979 Set_Full_View (Prev, Id);
11980 Set_Etype (Id, Any_Type);
11982 -- If so, process the full constant declaration
11984 else
11985 -- RM 7.4 (6): If the subtype defined by the subtype_indication in
11986 -- the deferred declaration is constrained, then the subtype defined
11987 -- by the subtype_indication in the full declaration shall match it
11988 -- statically.
11990 Check_Possible_Deferred_Completion
11991 (Prev_Id => Prev,
11992 Prev_Obj_Def => Object_Definition (Parent (Prev)),
11993 Curr_Obj_Def => Obj_Def);
11995 Set_Full_View (Prev, Id);
11996 Set_Is_Public (Id, Is_Public (Prev));
11997 Set_Is_Internal (Id);
11998 Append_Entity (Id, Current_Scope);
12000 -- Check ALIASED present if present before (RM 7.4(7))
12002 if Is_Aliased (Prev)
12003 and then not Aliased_Present (N)
12004 then
12005 Error_Msg_Sloc := Sloc (Prev);
12006 Error_Msg_N ("ALIASED required (see declaration #)", N);
12007 end if;
12009 -- Check that placement is in private part and that the incomplete
12010 -- declaration appeared in the visible part.
12012 if Ekind (Current_Scope) = E_Package
12013 and then not In_Private_Part (Current_Scope)
12014 then
12015 Error_Msg_Sloc := Sloc (Prev);
12016 Error_Msg_N
12017 ("full constant for declaration # must be in private part", N);
12019 elsif Ekind (Current_Scope) = E_Package
12020 and then
12021 List_Containing (Parent (Prev)) /=
12022 Visible_Declarations (Package_Specification (Current_Scope))
12023 then
12024 Error_Msg_N
12025 ("deferred constant must be declared in visible part",
12026 Parent (Prev));
12027 end if;
12029 if Is_Access_Type (T)
12030 and then Nkind (Expression (N)) = N_Allocator
12031 then
12032 Check_Recursive_Declaration (Designated_Type (T));
12033 end if;
12035 -- A deferred constant is a visible entity. If type has invariants,
12036 -- verify that the initial value satisfies them.
12038 if Has_Invariants (T) and then Present (Invariant_Procedure (T)) then
12039 Insert_After (N,
12040 Make_Invariant_Call (New_Occurrence_Of (Prev, Sloc (N))));
12041 end if;
12042 end if;
12043 end Constant_Redeclaration;
12045 ----------------------
12046 -- Constrain_Access --
12047 ----------------------
12049 procedure Constrain_Access
12050 (Def_Id : in out Entity_Id;
12051 S : Node_Id;
12052 Related_Nod : Node_Id)
12054 T : constant Entity_Id := Entity (Subtype_Mark (S));
12055 Desig_Type : constant Entity_Id := Designated_Type (T);
12056 Desig_Subtype : Entity_Id := Create_Itype (E_Void, Related_Nod);
12057 Constraint_OK : Boolean := True;
12059 begin
12060 if Is_Array_Type (Desig_Type) then
12061 Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
12063 elsif (Is_Record_Type (Desig_Type)
12064 or else Is_Incomplete_Or_Private_Type (Desig_Type))
12065 and then not Is_Constrained (Desig_Type)
12066 then
12067 -- ??? The following code is a temporary bypass to ignore a
12068 -- discriminant constraint on access type if it is constraining
12069 -- the current record. Avoid creating the implicit subtype of the
12070 -- record we are currently compiling since right now, we cannot
12071 -- handle these. For now, just return the access type itself.
12073 if Desig_Type = Current_Scope
12074 and then No (Def_Id)
12075 then
12076 Set_Ekind (Desig_Subtype, E_Record_Subtype);
12077 Def_Id := Entity (Subtype_Mark (S));
12079 -- This call added to ensure that the constraint is analyzed
12080 -- (needed for a B test). Note that we still return early from
12081 -- this procedure to avoid recursive processing. ???
12083 Constrain_Discriminated_Type
12084 (Desig_Subtype, S, Related_Nod, For_Access => True);
12085 return;
12086 end if;
12088 -- Enforce rule that the constraint is illegal if there is an
12089 -- unconstrained view of the designated type. This means that the
12090 -- partial view (either a private type declaration or a derivation
12091 -- from a private type) has no discriminants. (Defect Report
12092 -- 8652/0008, Technical Corrigendum 1, checked by ACATS B371001).
12094 -- Rule updated for Ada 2005: The private type is said to have
12095 -- a constrained partial view, given that objects of the type
12096 -- can be declared. Furthermore, the rule applies to all access
12097 -- types, unlike the rule concerning default discriminants (see
12098 -- RM 3.7.1(7/3))
12100 if (Ekind (T) = E_General_Access_Type or else Ada_Version >= Ada_2005)
12101 and then Has_Private_Declaration (Desig_Type)
12102 and then In_Open_Scopes (Scope (Desig_Type))
12103 and then Has_Discriminants (Desig_Type)
12104 then
12105 declare
12106 Pack : constant Node_Id :=
12107 Unit_Declaration_Node (Scope (Desig_Type));
12108 Decls : List_Id;
12109 Decl : Node_Id;
12111 begin
12112 if Nkind (Pack) = N_Package_Declaration then
12113 Decls := Visible_Declarations (Specification (Pack));
12114 Decl := First (Decls);
12115 while Present (Decl) loop
12116 if (Nkind (Decl) = N_Private_Type_Declaration
12117 and then Chars (Defining_Identifier (Decl)) =
12118 Chars (Desig_Type))
12120 or else
12121 (Nkind (Decl) = N_Full_Type_Declaration
12122 and then
12123 Chars (Defining_Identifier (Decl)) =
12124 Chars (Desig_Type)
12125 and then Is_Derived_Type (Desig_Type)
12126 and then
12127 Has_Private_Declaration (Etype (Desig_Type)))
12128 then
12129 if No (Discriminant_Specifications (Decl)) then
12130 Error_Msg_N
12131 ("cannot constrain access type if designated "
12132 & "type has constrained partial view", S);
12133 end if;
12135 exit;
12136 end if;
12138 Next (Decl);
12139 end loop;
12140 end if;
12141 end;
12142 end if;
12144 Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
12145 For_Access => True);
12147 elsif Is_Concurrent_Type (Desig_Type)
12148 and then not Is_Constrained (Desig_Type)
12149 then
12150 Constrain_Concurrent (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
12152 else
12153 Error_Msg_N ("invalid constraint on access type", S);
12155 -- We simply ignore an invalid constraint
12157 Desig_Subtype := Desig_Type;
12158 Constraint_OK := False;
12159 end if;
12161 if No (Def_Id) then
12162 Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
12163 else
12164 Set_Ekind (Def_Id, E_Access_Subtype);
12165 end if;
12167 if Constraint_OK then
12168 Set_Etype (Def_Id, Base_Type (T));
12170 if Is_Private_Type (Desig_Type) then
12171 Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
12172 end if;
12173 else
12174 Set_Etype (Def_Id, Any_Type);
12175 end if;
12177 Set_Size_Info (Def_Id, T);
12178 Set_Is_Constrained (Def_Id, Constraint_OK);
12179 Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
12180 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
12181 Set_Is_Access_Constant (Def_Id, Is_Access_Constant (T));
12183 Conditional_Delay (Def_Id, T);
12185 -- AI-363 : Subtypes of general access types whose designated types have
12186 -- default discriminants are disallowed. In instances, the rule has to
12187 -- be checked against the actual, of which T is the subtype. In a
12188 -- generic body, the rule is checked assuming that the actual type has
12189 -- defaulted discriminants.
12191 if Ada_Version >= Ada_2005 or else Warn_On_Ada_2005_Compatibility then
12192 if Ekind (Base_Type (T)) = E_General_Access_Type
12193 and then Has_Defaulted_Discriminants (Desig_Type)
12194 then
12195 if Ada_Version < Ada_2005 then
12196 Error_Msg_N
12197 ("access subtype of general access type would not " &
12198 "be allowed in Ada 2005?y?", S);
12199 else
12200 Error_Msg_N
12201 ("access subtype of general access type not allowed", S);
12202 end if;
12204 Error_Msg_N ("\discriminants have defaults", S);
12206 elsif Is_Access_Type (T)
12207 and then Is_Generic_Type (Desig_Type)
12208 and then Has_Discriminants (Desig_Type)
12209 and then In_Package_Body (Current_Scope)
12210 then
12211 if Ada_Version < Ada_2005 then
12212 Error_Msg_N
12213 ("access subtype would not be allowed in generic body "
12214 & "in Ada 2005?y?", S);
12215 else
12216 Error_Msg_N
12217 ("access subtype not allowed in generic body", S);
12218 end if;
12220 Error_Msg_N
12221 ("\designated type is a discriminated formal", S);
12222 end if;
12223 end if;
12224 end Constrain_Access;
12226 ---------------------
12227 -- Constrain_Array --
12228 ---------------------
12230 procedure Constrain_Array
12231 (Def_Id : in out Entity_Id;
12232 SI : Node_Id;
12233 Related_Nod : Node_Id;
12234 Related_Id : Entity_Id;
12235 Suffix : Character)
12237 C : constant Node_Id := Constraint (SI);
12238 Number_Of_Constraints : Nat := 0;
12239 Index : Node_Id;
12240 S, T : Entity_Id;
12241 Constraint_OK : Boolean := True;
12243 begin
12244 T := Entity (Subtype_Mark (SI));
12246 if Is_Access_Type (T) then
12247 T := Designated_Type (T);
12248 end if;
12250 -- If an index constraint follows a subtype mark in a subtype indication
12251 -- then the type or subtype denoted by the subtype mark must not already
12252 -- impose an index constraint. The subtype mark must denote either an
12253 -- unconstrained array type or an access type whose designated type
12254 -- is such an array type... (RM 3.6.1)
12256 if Is_Constrained (T) then
12257 Error_Msg_N ("array type is already constrained", Subtype_Mark (SI));
12258 Constraint_OK := False;
12260 else
12261 S := First (Constraints (C));
12262 while Present (S) loop
12263 Number_Of_Constraints := Number_Of_Constraints + 1;
12264 Next (S);
12265 end loop;
12267 -- In either case, the index constraint must provide a discrete
12268 -- range for each index of the array type and the type of each
12269 -- discrete range must be the same as that of the corresponding
12270 -- index. (RM 3.6.1)
12272 if Number_Of_Constraints /= Number_Dimensions (T) then
12273 Error_Msg_NE ("incorrect number of index constraints for }", C, T);
12274 Constraint_OK := False;
12276 else
12277 S := First (Constraints (C));
12278 Index := First_Index (T);
12279 Analyze (Index);
12281 -- Apply constraints to each index type
12283 for J in 1 .. Number_Of_Constraints loop
12284 Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
12285 Next (Index);
12286 Next (S);
12287 end loop;
12289 end if;
12290 end if;
12292 if No (Def_Id) then
12293 Def_Id :=
12294 Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
12295 Set_Parent (Def_Id, Related_Nod);
12297 else
12298 Set_Ekind (Def_Id, E_Array_Subtype);
12299 end if;
12301 Set_Size_Info (Def_Id, (T));
12302 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
12303 Set_Etype (Def_Id, Base_Type (T));
12305 if Constraint_OK then
12306 Set_First_Index (Def_Id, First (Constraints (C)));
12307 else
12308 Set_First_Index (Def_Id, First_Index (T));
12309 end if;
12311 Set_Is_Constrained (Def_Id, True);
12312 Set_Is_Aliased (Def_Id, Is_Aliased (T));
12313 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
12315 Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
12316 Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
12318 -- A subtype does not inherit the Packed_Array_Impl_Type of is parent.
12319 -- We need to initialize the attribute because if Def_Id is previously
12320 -- analyzed through a limited_with clause, it will have the attributes
12321 -- of an incomplete type, one of which is an Elist that overlaps the
12322 -- Packed_Array_Impl_Type field.
12324 Set_Packed_Array_Impl_Type (Def_Id, Empty);
12326 -- Build a freeze node if parent still needs one. Also make sure that
12327 -- the Depends_On_Private status is set because the subtype will need
12328 -- reprocessing at the time the base type does, and also we must set a
12329 -- conditional delay.
12331 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
12332 Conditional_Delay (Def_Id, T);
12333 end Constrain_Array;
12335 ------------------------------
12336 -- Constrain_Component_Type --
12337 ------------------------------
12339 function Constrain_Component_Type
12340 (Comp : Entity_Id;
12341 Constrained_Typ : Entity_Id;
12342 Related_Node : Node_Id;
12343 Typ : Entity_Id;
12344 Constraints : Elist_Id) return Entity_Id
12346 Loc : constant Source_Ptr := Sloc (Constrained_Typ);
12347 Compon_Type : constant Entity_Id := Etype (Comp);
12349 function Build_Constrained_Array_Type
12350 (Old_Type : Entity_Id) return Entity_Id;
12351 -- If Old_Type is an array type, one of whose indexes is constrained
12352 -- by a discriminant, build an Itype whose constraint replaces the
12353 -- discriminant with its value in the constraint.
12355 function Build_Constrained_Discriminated_Type
12356 (Old_Type : Entity_Id) return Entity_Id;
12357 -- Ditto for record components
12359 function Build_Constrained_Access_Type
12360 (Old_Type : Entity_Id) return Entity_Id;
12361 -- Ditto for access types. Makes use of previous two functions, to
12362 -- constrain designated type.
12364 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id;
12365 -- T is an array or discriminated type, C is a list of constraints
12366 -- that apply to T. This routine builds the constrained subtype.
12368 function Is_Discriminant (Expr : Node_Id) return Boolean;
12369 -- Returns True if Expr is a discriminant
12371 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id;
12372 -- Find the value of discriminant Discrim in Constraint
12374 -----------------------------------
12375 -- Build_Constrained_Access_Type --
12376 -----------------------------------
12378 function Build_Constrained_Access_Type
12379 (Old_Type : Entity_Id) return Entity_Id
12381 Desig_Type : constant Entity_Id := Designated_Type (Old_Type);
12382 Itype : Entity_Id;
12383 Desig_Subtype : Entity_Id;
12384 Scop : Entity_Id;
12386 begin
12387 -- if the original access type was not embedded in the enclosing
12388 -- type definition, there is no need to produce a new access
12389 -- subtype. In fact every access type with an explicit constraint
12390 -- generates an itype whose scope is the enclosing record.
12392 if not Is_Type (Scope (Old_Type)) then
12393 return Old_Type;
12395 elsif Is_Array_Type (Desig_Type) then
12396 Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
12398 elsif Has_Discriminants (Desig_Type) then
12400 -- This may be an access type to an enclosing record type for
12401 -- which we are constructing the constrained components. Return
12402 -- the enclosing record subtype. This is not always correct,
12403 -- but avoids infinite recursion. ???
12405 Desig_Subtype := Any_Type;
12407 for J in reverse 0 .. Scope_Stack.Last loop
12408 Scop := Scope_Stack.Table (J).Entity;
12410 if Is_Type (Scop)
12411 and then Base_Type (Scop) = Base_Type (Desig_Type)
12412 then
12413 Desig_Subtype := Scop;
12414 end if;
12416 exit when not Is_Type (Scop);
12417 end loop;
12419 if Desig_Subtype = Any_Type then
12420 Desig_Subtype :=
12421 Build_Constrained_Discriminated_Type (Desig_Type);
12422 end if;
12424 else
12425 return Old_Type;
12426 end if;
12428 if Desig_Subtype /= Desig_Type then
12430 -- The Related_Node better be here or else we won't be able
12431 -- to attach new itypes to a node in the tree.
12433 pragma Assert (Present (Related_Node));
12435 Itype := Create_Itype (E_Access_Subtype, Related_Node);
12437 Set_Etype (Itype, Base_Type (Old_Type));
12438 Set_Size_Info (Itype, (Old_Type));
12439 Set_Directly_Designated_Type (Itype, Desig_Subtype);
12440 Set_Depends_On_Private (Itype, Has_Private_Component
12441 (Old_Type));
12442 Set_Is_Access_Constant (Itype, Is_Access_Constant
12443 (Old_Type));
12445 -- The new itype needs freezing when it depends on a not frozen
12446 -- type and the enclosing subtype needs freezing.
12448 if Has_Delayed_Freeze (Constrained_Typ)
12449 and then not Is_Frozen (Constrained_Typ)
12450 then
12451 Conditional_Delay (Itype, Base_Type (Old_Type));
12452 end if;
12454 return Itype;
12456 else
12457 return Old_Type;
12458 end if;
12459 end Build_Constrained_Access_Type;
12461 ----------------------------------
12462 -- Build_Constrained_Array_Type --
12463 ----------------------------------
12465 function Build_Constrained_Array_Type
12466 (Old_Type : Entity_Id) return Entity_Id
12468 Lo_Expr : Node_Id;
12469 Hi_Expr : Node_Id;
12470 Old_Index : Node_Id;
12471 Range_Node : Node_Id;
12472 Constr_List : List_Id;
12474 Need_To_Create_Itype : Boolean := False;
12476 begin
12477 Old_Index := First_Index (Old_Type);
12478 while Present (Old_Index) loop
12479 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
12481 if Is_Discriminant (Lo_Expr)
12482 or else
12483 Is_Discriminant (Hi_Expr)
12484 then
12485 Need_To_Create_Itype := True;
12486 end if;
12488 Next_Index (Old_Index);
12489 end loop;
12491 if Need_To_Create_Itype then
12492 Constr_List := New_List;
12494 Old_Index := First_Index (Old_Type);
12495 while Present (Old_Index) loop
12496 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
12498 if Is_Discriminant (Lo_Expr) then
12499 Lo_Expr := Get_Discr_Value (Lo_Expr);
12500 end if;
12502 if Is_Discriminant (Hi_Expr) then
12503 Hi_Expr := Get_Discr_Value (Hi_Expr);
12504 end if;
12506 Range_Node :=
12507 Make_Range
12508 (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
12510 Append (Range_Node, To => Constr_List);
12512 Next_Index (Old_Index);
12513 end loop;
12515 return Build_Subtype (Old_Type, Constr_List);
12517 else
12518 return Old_Type;
12519 end if;
12520 end Build_Constrained_Array_Type;
12522 ------------------------------------------
12523 -- Build_Constrained_Discriminated_Type --
12524 ------------------------------------------
12526 function Build_Constrained_Discriminated_Type
12527 (Old_Type : Entity_Id) return Entity_Id
12529 Expr : Node_Id;
12530 Constr_List : List_Id;
12531 Old_Constraint : Elmt_Id;
12533 Need_To_Create_Itype : Boolean := False;
12535 begin
12536 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
12537 while Present (Old_Constraint) loop
12538 Expr := Node (Old_Constraint);
12540 if Is_Discriminant (Expr) then
12541 Need_To_Create_Itype := True;
12542 end if;
12544 Next_Elmt (Old_Constraint);
12545 end loop;
12547 if Need_To_Create_Itype then
12548 Constr_List := New_List;
12550 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
12551 while Present (Old_Constraint) loop
12552 Expr := Node (Old_Constraint);
12554 if Is_Discriminant (Expr) then
12555 Expr := Get_Discr_Value (Expr);
12556 end if;
12558 Append (New_Copy_Tree (Expr), To => Constr_List);
12560 Next_Elmt (Old_Constraint);
12561 end loop;
12563 return Build_Subtype (Old_Type, Constr_List);
12565 else
12566 return Old_Type;
12567 end if;
12568 end Build_Constrained_Discriminated_Type;
12570 -------------------
12571 -- Build_Subtype --
12572 -------------------
12574 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id is
12575 Indic : Node_Id;
12576 Subtyp_Decl : Node_Id;
12577 Def_Id : Entity_Id;
12578 Btyp : Entity_Id := Base_Type (T);
12580 begin
12581 -- The Related_Node better be here or else we won't be able to
12582 -- attach new itypes to a node in the tree.
12584 pragma Assert (Present (Related_Node));
12586 -- If the view of the component's type is incomplete or private
12587 -- with unknown discriminants, then the constraint must be applied
12588 -- to the full type.
12590 if Has_Unknown_Discriminants (Btyp)
12591 and then Present (Underlying_Type (Btyp))
12592 then
12593 Btyp := Underlying_Type (Btyp);
12594 end if;
12596 Indic :=
12597 Make_Subtype_Indication (Loc,
12598 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
12599 Constraint => Make_Index_Or_Discriminant_Constraint (Loc, C));
12601 Def_Id := Create_Itype (Ekind (T), Related_Node);
12603 Subtyp_Decl :=
12604 Make_Subtype_Declaration (Loc,
12605 Defining_Identifier => Def_Id,
12606 Subtype_Indication => Indic);
12608 Set_Parent (Subtyp_Decl, Parent (Related_Node));
12610 -- Itypes must be analyzed with checks off (see package Itypes)
12612 Analyze (Subtyp_Decl, Suppress => All_Checks);
12614 return Def_Id;
12615 end Build_Subtype;
12617 ---------------------
12618 -- Get_Discr_Value --
12619 ---------------------
12621 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id is
12622 D : Entity_Id;
12623 E : Elmt_Id;
12625 begin
12626 -- The discriminant may be declared for the type, in which case we
12627 -- find it by iterating over the list of discriminants. If the
12628 -- discriminant is inherited from a parent type, it appears as the
12629 -- corresponding discriminant of the current type. This will be the
12630 -- case when constraining an inherited component whose constraint is
12631 -- given by a discriminant of the parent.
12633 D := First_Discriminant (Typ);
12634 E := First_Elmt (Constraints);
12636 while Present (D) loop
12637 if D = Entity (Discrim)
12638 or else D = CR_Discriminant (Entity (Discrim))
12639 or else Corresponding_Discriminant (D) = Entity (Discrim)
12640 then
12641 return Node (E);
12642 end if;
12644 Next_Discriminant (D);
12645 Next_Elmt (E);
12646 end loop;
12648 -- The Corresponding_Discriminant mechanism is incomplete, because
12649 -- the correspondence between new and old discriminants is not one
12650 -- to one: one new discriminant can constrain several old ones. In
12651 -- that case, scan sequentially the stored_constraint, the list of
12652 -- discriminants of the parents, and the constraints.
12654 -- Previous code checked for the present of the Stored_Constraint
12655 -- list for the derived type, but did not use it at all. Should it
12656 -- be present when the component is a discriminated task type?
12658 if Is_Derived_Type (Typ)
12659 and then Scope (Entity (Discrim)) = Etype (Typ)
12660 then
12661 D := First_Discriminant (Etype (Typ));
12662 E := First_Elmt (Constraints);
12663 while Present (D) loop
12664 if D = Entity (Discrim) then
12665 return Node (E);
12666 end if;
12668 Next_Discriminant (D);
12669 Next_Elmt (E);
12670 end loop;
12671 end if;
12673 -- Something is wrong if we did not find the value
12675 raise Program_Error;
12676 end Get_Discr_Value;
12678 ---------------------
12679 -- Is_Discriminant --
12680 ---------------------
12682 function Is_Discriminant (Expr : Node_Id) return Boolean is
12683 Discrim_Scope : Entity_Id;
12685 begin
12686 if Denotes_Discriminant (Expr) then
12687 Discrim_Scope := Scope (Entity (Expr));
12689 -- Either we have a reference to one of Typ's discriminants,
12691 pragma Assert (Discrim_Scope = Typ
12693 -- or to the discriminants of the parent type, in the case
12694 -- of a derivation of a tagged type with variants.
12696 or else Discrim_Scope = Etype (Typ)
12697 or else Full_View (Discrim_Scope) = Etype (Typ)
12699 -- or same as above for the case where the discriminants
12700 -- were declared in Typ's private view.
12702 or else (Is_Private_Type (Discrim_Scope)
12703 and then Chars (Discrim_Scope) = Chars (Typ))
12705 -- or else we are deriving from the full view and the
12706 -- discriminant is declared in the private entity.
12708 or else (Is_Private_Type (Typ)
12709 and then Chars (Discrim_Scope) = Chars (Typ))
12711 -- Or we are constrained the corresponding record of a
12712 -- synchronized type that completes a private declaration.
12714 or else (Is_Concurrent_Record_Type (Typ)
12715 and then
12716 Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
12718 -- or we have a class-wide type, in which case make sure the
12719 -- discriminant found belongs to the root type.
12721 or else (Is_Class_Wide_Type (Typ)
12722 and then Etype (Typ) = Discrim_Scope));
12724 return True;
12725 end if;
12727 -- In all other cases we have something wrong
12729 return False;
12730 end Is_Discriminant;
12732 -- Start of processing for Constrain_Component_Type
12734 begin
12735 if Nkind (Parent (Comp)) = N_Component_Declaration
12736 and then Comes_From_Source (Parent (Comp))
12737 and then Comes_From_Source
12738 (Subtype_Indication (Component_Definition (Parent (Comp))))
12739 and then
12740 Is_Entity_Name
12741 (Subtype_Indication (Component_Definition (Parent (Comp))))
12742 then
12743 return Compon_Type;
12745 elsif Is_Array_Type (Compon_Type) then
12746 return Build_Constrained_Array_Type (Compon_Type);
12748 elsif Has_Discriminants (Compon_Type) then
12749 return Build_Constrained_Discriminated_Type (Compon_Type);
12751 elsif Is_Access_Type (Compon_Type) then
12752 return Build_Constrained_Access_Type (Compon_Type);
12754 else
12755 return Compon_Type;
12756 end if;
12757 end Constrain_Component_Type;
12759 --------------------------
12760 -- Constrain_Concurrent --
12761 --------------------------
12763 -- For concurrent types, the associated record value type carries the same
12764 -- discriminants, so when we constrain a concurrent type, we must constrain
12765 -- the corresponding record type as well.
12767 procedure Constrain_Concurrent
12768 (Def_Id : in out Entity_Id;
12769 SI : Node_Id;
12770 Related_Nod : Node_Id;
12771 Related_Id : Entity_Id;
12772 Suffix : Character)
12774 -- Retrieve Base_Type to ensure getting to the concurrent type in the
12775 -- case of a private subtype (needed when only doing semantic analysis).
12777 T_Ent : Entity_Id := Base_Type (Entity (Subtype_Mark (SI)));
12778 T_Val : Entity_Id;
12780 begin
12781 if Is_Access_Type (T_Ent) then
12782 T_Ent := Designated_Type (T_Ent);
12783 end if;
12785 T_Val := Corresponding_Record_Type (T_Ent);
12787 if Present (T_Val) then
12789 if No (Def_Id) then
12790 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
12791 end if;
12793 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
12795 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
12796 Set_Corresponding_Record_Type (Def_Id,
12797 Constrain_Corresponding_Record (Def_Id, T_Val, Related_Nod));
12799 else
12800 -- If there is no associated record, expansion is disabled and this
12801 -- is a generic context. Create a subtype in any case, so that
12802 -- semantic analysis can proceed.
12804 if No (Def_Id) then
12805 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
12806 end if;
12808 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
12809 end if;
12810 end Constrain_Concurrent;
12812 ------------------------------------
12813 -- Constrain_Corresponding_Record --
12814 ------------------------------------
12816 function Constrain_Corresponding_Record
12817 (Prot_Subt : Entity_Id;
12818 Corr_Rec : Entity_Id;
12819 Related_Nod : Node_Id) return Entity_Id
12821 T_Sub : constant Entity_Id :=
12822 Create_Itype (E_Record_Subtype, Related_Nod, Corr_Rec, 'C');
12824 begin
12825 Set_Etype (T_Sub, Corr_Rec);
12826 Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
12827 Set_Is_Constrained (T_Sub, True);
12828 Set_First_Entity (T_Sub, First_Entity (Corr_Rec));
12829 Set_Last_Entity (T_Sub, Last_Entity (Corr_Rec));
12831 if Has_Discriminants (Prot_Subt) then -- False only if errors.
12832 Set_Discriminant_Constraint
12833 (T_Sub, Discriminant_Constraint (Prot_Subt));
12834 Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
12835 Create_Constrained_Components
12836 (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
12837 end if;
12839 Set_Depends_On_Private (T_Sub, Has_Private_Component (T_Sub));
12841 if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
12842 Conditional_Delay (T_Sub, Corr_Rec);
12844 else
12845 -- This is a component subtype: it will be frozen in the context of
12846 -- the enclosing record's init_proc, so that discriminant references
12847 -- are resolved to discriminals. (Note: we used to skip freezing
12848 -- altogether in that case, which caused errors downstream for
12849 -- components of a bit packed array type).
12851 Set_Has_Delayed_Freeze (T_Sub);
12852 end if;
12854 return T_Sub;
12855 end Constrain_Corresponding_Record;
12857 -----------------------
12858 -- Constrain_Decimal --
12859 -----------------------
12861 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id) is
12862 T : constant Entity_Id := Entity (Subtype_Mark (S));
12863 C : constant Node_Id := Constraint (S);
12864 Loc : constant Source_Ptr := Sloc (C);
12865 Range_Expr : Node_Id;
12866 Digits_Expr : Node_Id;
12867 Digits_Val : Uint;
12868 Bound_Val : Ureal;
12870 begin
12871 Set_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
12873 if Nkind (C) = N_Range_Constraint then
12874 Range_Expr := Range_Expression (C);
12875 Digits_Val := Digits_Value (T);
12877 else
12878 pragma Assert (Nkind (C) = N_Digits_Constraint);
12880 Check_SPARK_05_Restriction ("digits constraint is not allowed", S);
12882 Digits_Expr := Digits_Expression (C);
12883 Analyze_And_Resolve (Digits_Expr, Any_Integer);
12885 Check_Digits_Expression (Digits_Expr);
12886 Digits_Val := Expr_Value (Digits_Expr);
12888 if Digits_Val > Digits_Value (T) then
12889 Error_Msg_N
12890 ("digits expression is incompatible with subtype", C);
12891 Digits_Val := Digits_Value (T);
12892 end if;
12894 if Present (Range_Constraint (C)) then
12895 Range_Expr := Range_Expression (Range_Constraint (C));
12896 else
12897 Range_Expr := Empty;
12898 end if;
12899 end if;
12901 Set_Etype (Def_Id, Base_Type (T));
12902 Set_Size_Info (Def_Id, (T));
12903 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
12904 Set_Delta_Value (Def_Id, Delta_Value (T));
12905 Set_Scale_Value (Def_Id, Scale_Value (T));
12906 Set_Small_Value (Def_Id, Small_Value (T));
12907 Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
12908 Set_Digits_Value (Def_Id, Digits_Val);
12910 -- Manufacture range from given digits value if no range present
12912 if No (Range_Expr) then
12913 Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
12914 Range_Expr :=
12915 Make_Range (Loc,
12916 Low_Bound =>
12917 Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
12918 High_Bound =>
12919 Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
12920 end if;
12922 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
12923 Set_Discrete_RM_Size (Def_Id);
12925 -- Unconditionally delay the freeze, since we cannot set size
12926 -- information in all cases correctly until the freeze point.
12928 Set_Has_Delayed_Freeze (Def_Id);
12929 end Constrain_Decimal;
12931 ----------------------------------
12932 -- Constrain_Discriminated_Type --
12933 ----------------------------------
12935 procedure Constrain_Discriminated_Type
12936 (Def_Id : Entity_Id;
12937 S : Node_Id;
12938 Related_Nod : Node_Id;
12939 For_Access : Boolean := False)
12941 E : constant Entity_Id := Entity (Subtype_Mark (S));
12942 T : Entity_Id;
12943 C : Node_Id;
12944 Elist : Elist_Id := New_Elmt_List;
12946 procedure Fixup_Bad_Constraint;
12947 -- This is called after finding a bad constraint, and after having
12948 -- posted an appropriate error message. The mission is to leave the
12949 -- entity T in as reasonable state as possible.
12951 --------------------------
12952 -- Fixup_Bad_Constraint --
12953 --------------------------
12955 procedure Fixup_Bad_Constraint is
12956 begin
12957 -- Set a reasonable Ekind for the entity. For an incomplete type,
12958 -- we can't do much, but for other types, we can set the proper
12959 -- corresponding subtype kind.
12961 if Ekind (T) = E_Incomplete_Type then
12962 Set_Ekind (Def_Id, Ekind (T));
12963 else
12964 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
12965 end if;
12967 -- Set Etype to the known type, to reduce chances of cascaded errors
12969 Set_Etype (Def_Id, E);
12970 Set_Error_Posted (Def_Id);
12971 end Fixup_Bad_Constraint;
12973 -- Start of processing for Constrain_Discriminated_Type
12975 begin
12976 C := Constraint (S);
12978 -- A discriminant constraint is only allowed in a subtype indication,
12979 -- after a subtype mark. This subtype mark must denote either a type
12980 -- with discriminants, or an access type whose designated type is a
12981 -- type with discriminants. A discriminant constraint specifies the
12982 -- values of these discriminants (RM 3.7.2(5)).
12984 T := Base_Type (Entity (Subtype_Mark (S)));
12986 if Is_Access_Type (T) then
12987 T := Designated_Type (T);
12988 end if;
12990 -- Ada 2005 (AI-412): Constrained incomplete subtypes are illegal.
12991 -- Avoid generating an error for access-to-incomplete subtypes.
12993 if Ada_Version >= Ada_2005
12994 and then Ekind (T) = E_Incomplete_Type
12995 and then Nkind (Parent (S)) = N_Subtype_Declaration
12996 and then not Is_Itype (Def_Id)
12997 then
12998 -- A little sanity check, emit an error message if the type
12999 -- has discriminants to begin with. Type T may be a regular
13000 -- incomplete type or imported via a limited with clause.
13002 if Has_Discriminants (T)
13003 or else (From_Limited_With (T)
13004 and then Present (Non_Limited_View (T))
13005 and then Nkind (Parent (Non_Limited_View (T))) =
13006 N_Full_Type_Declaration
13007 and then Present (Discriminant_Specifications
13008 (Parent (Non_Limited_View (T)))))
13009 then
13010 Error_Msg_N
13011 ("(Ada 2005) incomplete subtype may not be constrained", C);
13012 else
13013 Error_Msg_N ("invalid constraint: type has no discriminant", C);
13014 end if;
13016 Fixup_Bad_Constraint;
13017 return;
13019 -- Check that the type has visible discriminants. The type may be
13020 -- a private type with unknown discriminants whose full view has
13021 -- discriminants which are invisible.
13023 elsif not Has_Discriminants (T)
13024 or else
13025 (Has_Unknown_Discriminants (T)
13026 and then Is_Private_Type (T))
13027 then
13028 Error_Msg_N ("invalid constraint: type has no discriminant", C);
13029 Fixup_Bad_Constraint;
13030 return;
13032 elsif Is_Constrained (E)
13033 or else (Ekind (E) = E_Class_Wide_Subtype
13034 and then Present (Discriminant_Constraint (E)))
13035 then
13036 Error_Msg_N ("type is already constrained", Subtype_Mark (S));
13037 Fixup_Bad_Constraint;
13038 return;
13039 end if;
13041 -- T may be an unconstrained subtype (e.g. a generic actual).
13042 -- Constraint applies to the base type.
13044 T := Base_Type (T);
13046 Elist := Build_Discriminant_Constraints (T, S);
13048 -- If the list returned was empty we had an error in building the
13049 -- discriminant constraint. We have also already signalled an error
13050 -- in the incomplete type case
13052 if Is_Empty_Elmt_List (Elist) then
13053 Fixup_Bad_Constraint;
13054 return;
13055 end if;
13057 Build_Discriminated_Subtype (T, Def_Id, Elist, Related_Nod, For_Access);
13058 end Constrain_Discriminated_Type;
13060 ---------------------------
13061 -- Constrain_Enumeration --
13062 ---------------------------
13064 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id) is
13065 T : constant Entity_Id := Entity (Subtype_Mark (S));
13066 C : constant Node_Id := Constraint (S);
13068 begin
13069 Set_Ekind (Def_Id, E_Enumeration_Subtype);
13071 Set_First_Literal (Def_Id, First_Literal (Base_Type (T)));
13073 Set_Etype (Def_Id, Base_Type (T));
13074 Set_Size_Info (Def_Id, (T));
13075 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13076 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
13078 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13080 Set_Discrete_RM_Size (Def_Id);
13081 end Constrain_Enumeration;
13083 ----------------------
13084 -- Constrain_Float --
13085 ----------------------
13087 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id) is
13088 T : constant Entity_Id := Entity (Subtype_Mark (S));
13089 C : Node_Id;
13090 D : Node_Id;
13091 Rais : Node_Id;
13093 begin
13094 Set_Ekind (Def_Id, E_Floating_Point_Subtype);
13096 Set_Etype (Def_Id, Base_Type (T));
13097 Set_Size_Info (Def_Id, (T));
13098 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13100 -- Process the constraint
13102 C := Constraint (S);
13104 -- Digits constraint present
13106 if Nkind (C) = N_Digits_Constraint then
13108 Check_SPARK_05_Restriction ("digits constraint is not allowed", S);
13109 Check_Restriction (No_Obsolescent_Features, C);
13111 if Warn_On_Obsolescent_Feature then
13112 Error_Msg_N
13113 ("subtype digits constraint is an " &
13114 "obsolescent feature (RM J.3(8))?j?", C);
13115 end if;
13117 D := Digits_Expression (C);
13118 Analyze_And_Resolve (D, Any_Integer);
13119 Check_Digits_Expression (D);
13120 Set_Digits_Value (Def_Id, Expr_Value (D));
13122 -- Check that digits value is in range. Obviously we can do this
13123 -- at compile time, but it is strictly a runtime check, and of
13124 -- course there is an ACVC test that checks this.
13126 if Digits_Value (Def_Id) > Digits_Value (T) then
13127 Error_Msg_Uint_1 := Digits_Value (T);
13128 Error_Msg_N ("??digits value is too large, maximum is ^", D);
13129 Rais :=
13130 Make_Raise_Constraint_Error (Sloc (D),
13131 Reason => CE_Range_Check_Failed);
13132 Insert_Action (Declaration_Node (Def_Id), Rais);
13133 end if;
13135 C := Range_Constraint (C);
13137 -- No digits constraint present
13139 else
13140 Set_Digits_Value (Def_Id, Digits_Value (T));
13141 end if;
13143 -- Range constraint present
13145 if Nkind (C) = N_Range_Constraint then
13146 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13148 -- No range constraint present
13150 else
13151 pragma Assert (No (C));
13152 Set_Scalar_Range (Def_Id, Scalar_Range (T));
13153 end if;
13155 Set_Is_Constrained (Def_Id);
13156 end Constrain_Float;
13158 ---------------------
13159 -- Constrain_Index --
13160 ---------------------
13162 procedure Constrain_Index
13163 (Index : Node_Id;
13164 S : Node_Id;
13165 Related_Nod : Node_Id;
13166 Related_Id : Entity_Id;
13167 Suffix : Character;
13168 Suffix_Index : Nat)
13170 Def_Id : Entity_Id;
13171 R : Node_Id := Empty;
13172 T : constant Entity_Id := Etype (Index);
13174 begin
13175 if Nkind (S) = N_Range
13176 or else
13177 (Nkind (S) = N_Attribute_Reference
13178 and then Attribute_Name (S) = Name_Range)
13179 then
13180 -- A Range attribute will be transformed into N_Range by Resolve
13182 Analyze (S);
13183 Set_Etype (S, T);
13184 R := S;
13186 Process_Range_Expr_In_Decl (R, T);
13188 if not Error_Posted (S)
13189 and then
13190 (Nkind (S) /= N_Range
13191 or else not Covers (T, (Etype (Low_Bound (S))))
13192 or else not Covers (T, (Etype (High_Bound (S)))))
13193 then
13194 if Base_Type (T) /= Any_Type
13195 and then Etype (Low_Bound (S)) /= Any_Type
13196 and then Etype (High_Bound (S)) /= Any_Type
13197 then
13198 Error_Msg_N ("range expected", S);
13199 end if;
13200 end if;
13202 elsif Nkind (S) = N_Subtype_Indication then
13204 -- The parser has verified that this is a discrete indication
13206 Resolve_Discrete_Subtype_Indication (S, T);
13207 Bad_Predicated_Subtype_Use
13208 ("subtype& has predicate, not allowed in index constraint",
13209 S, Entity (Subtype_Mark (S)));
13211 R := Range_Expression (Constraint (S));
13213 -- Capture values of bounds and generate temporaries for them if
13214 -- needed, since checks may cause duplication of the expressions
13215 -- which must not be reevaluated.
13217 -- The forced evaluation removes side effects from expressions, which
13218 -- should occur also in GNATprove mode. Otherwise, we end up with
13219 -- unexpected insertions of actions at places where this is not
13220 -- supposed to occur, e.g. on default parameters of a call.
13222 if Expander_Active or GNATprove_Mode then
13223 Force_Evaluation
13224 (Low_Bound (R), Related_Id => Related_Id, Is_Low_Bound => True);
13225 Force_Evaluation
13226 (High_Bound (R), Related_Id => Related_Id, Is_Low_Bound => True);
13227 end if;
13229 elsif Nkind (S) = N_Discriminant_Association then
13231 -- Syntactically valid in subtype indication
13233 Error_Msg_N ("invalid index constraint", S);
13234 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
13235 return;
13237 -- Subtype_Mark case, no anonymous subtypes to construct
13239 else
13240 Analyze (S);
13242 if Is_Entity_Name (S) then
13243 if not Is_Type (Entity (S)) then
13244 Error_Msg_N ("expect subtype mark for index constraint", S);
13246 elsif Base_Type (Entity (S)) /= Base_Type (T) then
13247 Wrong_Type (S, Base_Type (T));
13249 -- Check error of subtype with predicate in index constraint
13251 else
13252 Bad_Predicated_Subtype_Use
13253 ("subtype& has predicate, not allowed in index constraint",
13254 S, Entity (S));
13255 end if;
13257 return;
13259 else
13260 Error_Msg_N ("invalid index constraint", S);
13261 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
13262 return;
13263 end if;
13264 end if;
13266 Def_Id :=
13267 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
13269 Set_Etype (Def_Id, Base_Type (T));
13271 if Is_Modular_Integer_Type (T) then
13272 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
13274 elsif Is_Integer_Type (T) then
13275 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
13277 else
13278 Set_Ekind (Def_Id, E_Enumeration_Subtype);
13279 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
13280 Set_First_Literal (Def_Id, First_Literal (T));
13281 end if;
13283 Set_Size_Info (Def_Id, (T));
13284 Set_RM_Size (Def_Id, RM_Size (T));
13285 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13287 Set_Scalar_Range (Def_Id, R);
13289 Set_Etype (S, Def_Id);
13290 Set_Discrete_RM_Size (Def_Id);
13291 end Constrain_Index;
13293 -----------------------
13294 -- Constrain_Integer --
13295 -----------------------
13297 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id) is
13298 T : constant Entity_Id := Entity (Subtype_Mark (S));
13299 C : constant Node_Id := Constraint (S);
13301 begin
13302 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13304 if Is_Modular_Integer_Type (T) then
13305 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
13306 else
13307 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
13308 end if;
13310 Set_Etype (Def_Id, Base_Type (T));
13311 Set_Size_Info (Def_Id, (T));
13312 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13313 Set_Discrete_RM_Size (Def_Id);
13314 end Constrain_Integer;
13316 ------------------------------
13317 -- Constrain_Ordinary_Fixed --
13318 ------------------------------
13320 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id) is
13321 T : constant Entity_Id := Entity (Subtype_Mark (S));
13322 C : Node_Id;
13323 D : Node_Id;
13324 Rais : Node_Id;
13326 begin
13327 Set_Ekind (Def_Id, E_Ordinary_Fixed_Point_Subtype);
13328 Set_Etype (Def_Id, Base_Type (T));
13329 Set_Size_Info (Def_Id, (T));
13330 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13331 Set_Small_Value (Def_Id, Small_Value (T));
13333 -- Process the constraint
13335 C := Constraint (S);
13337 -- Delta constraint present
13339 if Nkind (C) = N_Delta_Constraint then
13341 Check_SPARK_05_Restriction ("delta constraint is not allowed", S);
13342 Check_Restriction (No_Obsolescent_Features, C);
13344 if Warn_On_Obsolescent_Feature then
13345 Error_Msg_S
13346 ("subtype delta constraint is an " &
13347 "obsolescent feature (RM J.3(7))?j?");
13348 end if;
13350 D := Delta_Expression (C);
13351 Analyze_And_Resolve (D, Any_Real);
13352 Check_Delta_Expression (D);
13353 Set_Delta_Value (Def_Id, Expr_Value_R (D));
13355 -- Check that delta value is in range. Obviously we can do this
13356 -- at compile time, but it is strictly a runtime check, and of
13357 -- course there is an ACVC test that checks this.
13359 if Delta_Value (Def_Id) < Delta_Value (T) then
13360 Error_Msg_N ("??delta value is too small", D);
13361 Rais :=
13362 Make_Raise_Constraint_Error (Sloc (D),
13363 Reason => CE_Range_Check_Failed);
13364 Insert_Action (Declaration_Node (Def_Id), Rais);
13365 end if;
13367 C := Range_Constraint (C);
13369 -- No delta constraint present
13371 else
13372 Set_Delta_Value (Def_Id, Delta_Value (T));
13373 end if;
13375 -- Range constraint present
13377 if Nkind (C) = N_Range_Constraint then
13378 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13380 -- No range constraint present
13382 else
13383 pragma Assert (No (C));
13384 Set_Scalar_Range (Def_Id, Scalar_Range (T));
13386 end if;
13388 Set_Discrete_RM_Size (Def_Id);
13390 -- Unconditionally delay the freeze, since we cannot set size
13391 -- information in all cases correctly until the freeze point.
13393 Set_Has_Delayed_Freeze (Def_Id);
13394 end Constrain_Ordinary_Fixed;
13396 -----------------------
13397 -- Contain_Interface --
13398 -----------------------
13400 function Contain_Interface
13401 (Iface : Entity_Id;
13402 Ifaces : Elist_Id) return Boolean
13404 Iface_Elmt : Elmt_Id;
13406 begin
13407 if Present (Ifaces) then
13408 Iface_Elmt := First_Elmt (Ifaces);
13409 while Present (Iface_Elmt) loop
13410 if Node (Iface_Elmt) = Iface then
13411 return True;
13412 end if;
13414 Next_Elmt (Iface_Elmt);
13415 end loop;
13416 end if;
13418 return False;
13419 end Contain_Interface;
13421 ---------------------------
13422 -- Convert_Scalar_Bounds --
13423 ---------------------------
13425 procedure Convert_Scalar_Bounds
13426 (N : Node_Id;
13427 Parent_Type : Entity_Id;
13428 Derived_Type : Entity_Id;
13429 Loc : Source_Ptr)
13431 Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
13433 Lo : Node_Id;
13434 Hi : Node_Id;
13435 Rng : Node_Id;
13437 begin
13438 -- Defend against previous errors
13440 if No (Scalar_Range (Derived_Type)) then
13441 Check_Error_Detected;
13442 return;
13443 end if;
13445 Lo := Build_Scalar_Bound
13446 (Type_Low_Bound (Derived_Type),
13447 Parent_Type, Implicit_Base);
13449 Hi := Build_Scalar_Bound
13450 (Type_High_Bound (Derived_Type),
13451 Parent_Type, Implicit_Base);
13453 Rng :=
13454 Make_Range (Loc,
13455 Low_Bound => Lo,
13456 High_Bound => Hi);
13458 Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
13460 Set_Parent (Rng, N);
13461 Set_Scalar_Range (Derived_Type, Rng);
13463 -- Analyze the bounds
13465 Analyze_And_Resolve (Lo, Implicit_Base);
13466 Analyze_And_Resolve (Hi, Implicit_Base);
13468 -- Analyze the range itself, except that we do not analyze it if
13469 -- the bounds are real literals, and we have a fixed-point type.
13470 -- The reason for this is that we delay setting the bounds in this
13471 -- case till we know the final Small and Size values (see circuit
13472 -- in Freeze.Freeze_Fixed_Point_Type for further details).
13474 if Is_Fixed_Point_Type (Parent_Type)
13475 and then Nkind (Lo) = N_Real_Literal
13476 and then Nkind (Hi) = N_Real_Literal
13477 then
13478 return;
13480 -- Here we do the analysis of the range
13482 -- Note: we do this manually, since if we do a normal Analyze and
13483 -- Resolve call, there are problems with the conversions used for
13484 -- the derived type range.
13486 else
13487 Set_Etype (Rng, Implicit_Base);
13488 Set_Analyzed (Rng, True);
13489 end if;
13490 end Convert_Scalar_Bounds;
13492 -------------------
13493 -- Copy_And_Swap --
13494 -------------------
13496 procedure Copy_And_Swap (Priv, Full : Entity_Id) is
13497 begin
13498 -- Initialize new full declaration entity by copying the pertinent
13499 -- fields of the corresponding private declaration entity.
13501 -- We temporarily set Ekind to a value appropriate for a type to
13502 -- avoid assert failures in Einfo from checking for setting type
13503 -- attributes on something that is not a type. Ekind (Priv) is an
13504 -- appropriate choice, since it allowed the attributes to be set
13505 -- in the first place. This Ekind value will be modified later.
13507 Set_Ekind (Full, Ekind (Priv));
13509 -- Also set Etype temporarily to Any_Type, again, in the absence
13510 -- of errors, it will be properly reset, and if there are errors,
13511 -- then we want a value of Any_Type to remain.
13513 Set_Etype (Full, Any_Type);
13515 -- Now start copying attributes
13517 Set_Has_Discriminants (Full, Has_Discriminants (Priv));
13519 if Has_Discriminants (Full) then
13520 Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
13521 Set_Stored_Constraint (Full, Stored_Constraint (Priv));
13522 end if;
13524 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
13525 Set_Homonym (Full, Homonym (Priv));
13526 Set_Is_Immediately_Visible (Full, Is_Immediately_Visible (Priv));
13527 Set_Is_Public (Full, Is_Public (Priv));
13528 Set_Is_Pure (Full, Is_Pure (Priv));
13529 Set_Is_Tagged_Type (Full, Is_Tagged_Type (Priv));
13530 Set_Has_Pragma_Unmodified (Full, Has_Pragma_Unmodified (Priv));
13531 Set_Has_Pragma_Unreferenced (Full, Has_Pragma_Unreferenced (Priv));
13532 Set_Has_Pragma_Unreferenced_Objects
13533 (Full, Has_Pragma_Unreferenced_Objects
13534 (Priv));
13536 Conditional_Delay (Full, Priv);
13538 if Is_Tagged_Type (Full) then
13539 Set_Direct_Primitive_Operations
13540 (Full, Direct_Primitive_Operations (Priv));
13541 Set_No_Tagged_Streams_Pragma
13542 (Full, No_Tagged_Streams_Pragma (Priv));
13544 if Is_Base_Type (Priv) then
13545 Set_Class_Wide_Type (Full, Class_Wide_Type (Priv));
13546 end if;
13547 end if;
13549 Set_Is_Volatile (Full, Is_Volatile (Priv));
13550 Set_Treat_As_Volatile (Full, Treat_As_Volatile (Priv));
13551 Set_Scope (Full, Scope (Priv));
13552 Set_Next_Entity (Full, Next_Entity (Priv));
13553 Set_First_Entity (Full, First_Entity (Priv));
13554 Set_Last_Entity (Full, Last_Entity (Priv));
13556 -- If access types have been recorded for later handling, keep them in
13557 -- the full view so that they get handled when the full view freeze
13558 -- node is expanded.
13560 if Present (Freeze_Node (Priv))
13561 and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
13562 then
13563 Ensure_Freeze_Node (Full);
13564 Set_Access_Types_To_Process
13565 (Freeze_Node (Full),
13566 Access_Types_To_Process (Freeze_Node (Priv)));
13567 end if;
13569 -- Swap the two entities. Now Private is the full type entity and Full
13570 -- is the private one. They will be swapped back at the end of the
13571 -- private part. This swapping ensures that the entity that is visible
13572 -- in the private part is the full declaration.
13574 Exchange_Entities (Priv, Full);
13575 Append_Entity (Full, Scope (Full));
13576 end Copy_And_Swap;
13578 -------------------------------------
13579 -- Copy_Array_Base_Type_Attributes --
13580 -------------------------------------
13582 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
13583 begin
13584 Set_Component_Alignment (T1, Component_Alignment (T2));
13585 Set_Component_Type (T1, Component_Type (T2));
13586 Set_Component_Size (T1, Component_Size (T2));
13587 Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
13588 Set_Has_Non_Standard_Rep (T1, Has_Non_Standard_Rep (T2));
13589 Set_Has_Protected (T1, Has_Protected (T2));
13590 Set_Has_Task (T1, Has_Task (T2));
13591 Set_Is_Packed (T1, Is_Packed (T2));
13592 Set_Has_Aliased_Components (T1, Has_Aliased_Components (T2));
13593 Set_Has_Atomic_Components (T1, Has_Atomic_Components (T2));
13594 Set_Has_Volatile_Components (T1, Has_Volatile_Components (T2));
13595 end Copy_Array_Base_Type_Attributes;
13597 -----------------------------------
13598 -- Copy_Array_Subtype_Attributes --
13599 -----------------------------------
13601 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
13602 begin
13603 Set_Size_Info (T1, T2);
13605 Set_First_Index (T1, First_Index (T2));
13606 Set_Is_Aliased (T1, Is_Aliased (T2));
13607 Set_Is_Volatile (T1, Is_Volatile (T2));
13608 Set_Treat_As_Volatile (T1, Treat_As_Volatile (T2));
13609 Set_Is_Constrained (T1, Is_Constrained (T2));
13610 Set_Depends_On_Private (T1, Has_Private_Component (T2));
13611 Inherit_Rep_Item_Chain (T1, T2);
13612 Set_Convention (T1, Convention (T2));
13613 Set_Is_Limited_Composite (T1, Is_Limited_Composite (T2));
13614 Set_Is_Private_Composite (T1, Is_Private_Composite (T2));
13615 Set_Packed_Array_Impl_Type (T1, Packed_Array_Impl_Type (T2));
13616 end Copy_Array_Subtype_Attributes;
13618 -----------------------------------
13619 -- Create_Constrained_Components --
13620 -----------------------------------
13622 procedure Create_Constrained_Components
13623 (Subt : Entity_Id;
13624 Decl_Node : Node_Id;
13625 Typ : Entity_Id;
13626 Constraints : Elist_Id)
13628 Loc : constant Source_Ptr := Sloc (Subt);
13629 Comp_List : constant Elist_Id := New_Elmt_List;
13630 Parent_Type : constant Entity_Id := Etype (Typ);
13631 Assoc_List : constant List_Id := New_List;
13632 Discr_Val : Elmt_Id;
13633 Errors : Boolean;
13634 New_C : Entity_Id;
13635 Old_C : Entity_Id;
13636 Is_Static : Boolean := True;
13638 procedure Collect_Fixed_Components (Typ : Entity_Id);
13639 -- Collect parent type components that do not appear in a variant part
13641 procedure Create_All_Components;
13642 -- Iterate over Comp_List to create the components of the subtype
13644 function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
13645 -- Creates a new component from Old_Compon, copying all the fields from
13646 -- it, including its Etype, inserts the new component in the Subt entity
13647 -- chain and returns the new component.
13649 function Is_Variant_Record (T : Entity_Id) return Boolean;
13650 -- If true, and discriminants are static, collect only components from
13651 -- variants selected by discriminant values.
13653 ------------------------------
13654 -- Collect_Fixed_Components --
13655 ------------------------------
13657 procedure Collect_Fixed_Components (Typ : Entity_Id) is
13658 begin
13659 -- Build association list for discriminants, and find components of the
13660 -- variant part selected by the values of the discriminants.
13662 Old_C := First_Discriminant (Typ);
13663 Discr_Val := First_Elmt (Constraints);
13664 while Present (Old_C) loop
13665 Append_To (Assoc_List,
13666 Make_Component_Association (Loc,
13667 Choices => New_List (New_Occurrence_Of (Old_C, Loc)),
13668 Expression => New_Copy (Node (Discr_Val))));
13670 Next_Elmt (Discr_Val);
13671 Next_Discriminant (Old_C);
13672 end loop;
13674 -- The tag and the possible parent component are unconditionally in
13675 -- the subtype.
13677 if Is_Tagged_Type (Typ) or else Has_Controlled_Component (Typ) then
13678 Old_C := First_Component (Typ);
13679 while Present (Old_C) loop
13680 if Nam_In (Chars (Old_C), Name_uTag, Name_uParent) then
13681 Append_Elmt (Old_C, Comp_List);
13682 end if;
13684 Next_Component (Old_C);
13685 end loop;
13686 end if;
13687 end Collect_Fixed_Components;
13689 ---------------------------
13690 -- Create_All_Components --
13691 ---------------------------
13693 procedure Create_All_Components is
13694 Comp : Elmt_Id;
13696 begin
13697 Comp := First_Elmt (Comp_List);
13698 while Present (Comp) loop
13699 Old_C := Node (Comp);
13700 New_C := Create_Component (Old_C);
13702 Set_Etype
13703 (New_C,
13704 Constrain_Component_Type
13705 (Old_C, Subt, Decl_Node, Typ, Constraints));
13706 Set_Is_Public (New_C, Is_Public (Subt));
13708 Next_Elmt (Comp);
13709 end loop;
13710 end Create_All_Components;
13712 ----------------------
13713 -- Create_Component --
13714 ----------------------
13716 function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
13717 New_Compon : constant Entity_Id := New_Copy (Old_Compon);
13719 begin
13720 if Ekind (Old_Compon) = E_Discriminant
13721 and then Is_Completely_Hidden (Old_Compon)
13722 then
13723 -- This is a shadow discriminant created for a discriminant of
13724 -- the parent type, which needs to be present in the subtype.
13725 -- Give the shadow discriminant an internal name that cannot
13726 -- conflict with that of visible components.
13728 Set_Chars (New_Compon, New_Internal_Name ('C'));
13729 end if;
13731 -- Set the parent so we have a proper link for freezing etc. This is
13732 -- not a real parent pointer, since of course our parent does not own
13733 -- up to us and reference us, we are an illegitimate child of the
13734 -- original parent.
13736 Set_Parent (New_Compon, Parent (Old_Compon));
13738 -- If the old component's Esize was already determined and is a
13739 -- static value, then the new component simply inherits it. Otherwise
13740 -- the old component's size may require run-time determination, but
13741 -- the new component's size still might be statically determinable
13742 -- (if, for example it has a static constraint). In that case we want
13743 -- Layout_Type to recompute the component's size, so we reset its
13744 -- size and positional fields.
13746 if Frontend_Layout_On_Target
13747 and then not Known_Static_Esize (Old_Compon)
13748 then
13749 Set_Esize (New_Compon, Uint_0);
13750 Init_Normalized_First_Bit (New_Compon);
13751 Init_Normalized_Position (New_Compon);
13752 Init_Normalized_Position_Max (New_Compon);
13753 end if;
13755 -- We do not want this node marked as Comes_From_Source, since
13756 -- otherwise it would get first class status and a separate cross-
13757 -- reference line would be generated. Illegitimate children do not
13758 -- rate such recognition.
13760 Set_Comes_From_Source (New_Compon, False);
13762 -- But it is a real entity, and a birth certificate must be properly
13763 -- registered by entering it into the entity list.
13765 Enter_Name (New_Compon);
13767 return New_Compon;
13768 end Create_Component;
13770 -----------------------
13771 -- Is_Variant_Record --
13772 -----------------------
13774 function Is_Variant_Record (T : Entity_Id) return Boolean is
13775 begin
13776 return Nkind (Parent (T)) = N_Full_Type_Declaration
13777 and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
13778 and then Present (Component_List (Type_Definition (Parent (T))))
13779 and then
13780 Present
13781 (Variant_Part (Component_List (Type_Definition (Parent (T)))));
13782 end Is_Variant_Record;
13784 -- Start of processing for Create_Constrained_Components
13786 begin
13787 pragma Assert (Subt /= Base_Type (Subt));
13788 pragma Assert (Typ = Base_Type (Typ));
13790 Set_First_Entity (Subt, Empty);
13791 Set_Last_Entity (Subt, Empty);
13793 -- Check whether constraint is fully static, in which case we can
13794 -- optimize the list of components.
13796 Discr_Val := First_Elmt (Constraints);
13797 while Present (Discr_Val) loop
13798 if not Is_OK_Static_Expression (Node (Discr_Val)) then
13799 Is_Static := False;
13800 exit;
13801 end if;
13803 Next_Elmt (Discr_Val);
13804 end loop;
13806 Set_Has_Static_Discriminants (Subt, Is_Static);
13808 Push_Scope (Subt);
13810 -- Inherit the discriminants of the parent type
13812 Add_Discriminants : declare
13813 Num_Disc : Int;
13814 Num_Gird : Int;
13816 begin
13817 Num_Disc := 0;
13818 Old_C := First_Discriminant (Typ);
13820 while Present (Old_C) loop
13821 Num_Disc := Num_Disc + 1;
13822 New_C := Create_Component (Old_C);
13823 Set_Is_Public (New_C, Is_Public (Subt));
13824 Next_Discriminant (Old_C);
13825 end loop;
13827 -- For an untagged derived subtype, the number of discriminants may
13828 -- be smaller than the number of inherited discriminants, because
13829 -- several of them may be renamed by a single new discriminant or
13830 -- constrained. In this case, add the hidden discriminants back into
13831 -- the subtype, because they need to be present if the optimizer of
13832 -- the GCC 4.x back-end decides to break apart assignments between
13833 -- objects using the parent view into member-wise assignments.
13835 Num_Gird := 0;
13837 if Is_Derived_Type (Typ)
13838 and then not Is_Tagged_Type (Typ)
13839 then
13840 Old_C := First_Stored_Discriminant (Typ);
13842 while Present (Old_C) loop
13843 Num_Gird := Num_Gird + 1;
13844 Next_Stored_Discriminant (Old_C);
13845 end loop;
13846 end if;
13848 if Num_Gird > Num_Disc then
13850 -- Find out multiple uses of new discriminants, and add hidden
13851 -- components for the extra renamed discriminants. We recognize
13852 -- multiple uses through the Corresponding_Discriminant of a
13853 -- new discriminant: if it constrains several old discriminants,
13854 -- this field points to the last one in the parent type. The
13855 -- stored discriminants of the derived type have the same name
13856 -- as those of the parent.
13858 declare
13859 Constr : Elmt_Id;
13860 New_Discr : Entity_Id;
13861 Old_Discr : Entity_Id;
13863 begin
13864 Constr := First_Elmt (Stored_Constraint (Typ));
13865 Old_Discr := First_Stored_Discriminant (Typ);
13866 while Present (Constr) loop
13867 if Is_Entity_Name (Node (Constr))
13868 and then Ekind (Entity (Node (Constr))) = E_Discriminant
13869 then
13870 New_Discr := Entity (Node (Constr));
13872 if Chars (Corresponding_Discriminant (New_Discr)) /=
13873 Chars (Old_Discr)
13874 then
13875 -- The new discriminant has been used to rename a
13876 -- subsequent old discriminant. Introduce a shadow
13877 -- component for the current old discriminant.
13879 New_C := Create_Component (Old_Discr);
13880 Set_Original_Record_Component (New_C, Old_Discr);
13881 end if;
13883 else
13884 -- The constraint has eliminated the old discriminant.
13885 -- Introduce a shadow component.
13887 New_C := Create_Component (Old_Discr);
13888 Set_Original_Record_Component (New_C, Old_Discr);
13889 end if;
13891 Next_Elmt (Constr);
13892 Next_Stored_Discriminant (Old_Discr);
13893 end loop;
13894 end;
13895 end if;
13896 end Add_Discriminants;
13898 if Is_Static
13899 and then Is_Variant_Record (Typ)
13900 then
13901 Collect_Fixed_Components (Typ);
13903 Gather_Components (
13904 Typ,
13905 Component_List (Type_Definition (Parent (Typ))),
13906 Governed_By => Assoc_List,
13907 Into => Comp_List,
13908 Report_Errors => Errors);
13909 pragma Assert (not Errors);
13911 Create_All_Components;
13913 -- If the subtype declaration is created for a tagged type derivation
13914 -- with constraints, we retrieve the record definition of the parent
13915 -- type to select the components of the proper variant.
13917 elsif Is_Static
13918 and then Is_Tagged_Type (Typ)
13919 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
13920 and then
13921 Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
13922 and then Is_Variant_Record (Parent_Type)
13923 then
13924 Collect_Fixed_Components (Typ);
13926 Gather_Components
13927 (Typ,
13928 Component_List (Type_Definition (Parent (Parent_Type))),
13929 Governed_By => Assoc_List,
13930 Into => Comp_List,
13931 Report_Errors => Errors);
13933 -- Note: previously there was a check at this point that no errors
13934 -- were detected. As a consequence of AI05-220 there may be an error
13935 -- if an inherited discriminant that controls a variant has a non-
13936 -- static constraint.
13938 -- If the tagged derivation has a type extension, collect all the
13939 -- new components therein.
13941 if Present (Record_Extension_Part (Type_Definition (Parent (Typ))))
13942 then
13943 Old_C := First_Component (Typ);
13944 while Present (Old_C) loop
13945 if Original_Record_Component (Old_C) = Old_C
13946 and then Chars (Old_C) /= Name_uTag
13947 and then Chars (Old_C) /= Name_uParent
13948 then
13949 Append_Elmt (Old_C, Comp_List);
13950 end if;
13952 Next_Component (Old_C);
13953 end loop;
13954 end if;
13956 Create_All_Components;
13958 else
13959 -- If discriminants are not static, or if this is a multi-level type
13960 -- extension, we have to include all components of the parent type.
13962 Old_C := First_Component (Typ);
13963 while Present (Old_C) loop
13964 New_C := Create_Component (Old_C);
13966 Set_Etype
13967 (New_C,
13968 Constrain_Component_Type
13969 (Old_C, Subt, Decl_Node, Typ, Constraints));
13970 Set_Is_Public (New_C, Is_Public (Subt));
13972 Next_Component (Old_C);
13973 end loop;
13974 end if;
13976 End_Scope;
13977 end Create_Constrained_Components;
13979 ------------------------------------------
13980 -- Decimal_Fixed_Point_Type_Declaration --
13981 ------------------------------------------
13983 procedure Decimal_Fixed_Point_Type_Declaration
13984 (T : Entity_Id;
13985 Def : Node_Id)
13987 Loc : constant Source_Ptr := Sloc (Def);
13988 Digs_Expr : constant Node_Id := Digits_Expression (Def);
13989 Delta_Expr : constant Node_Id := Delta_Expression (Def);
13990 Implicit_Base : Entity_Id;
13991 Digs_Val : Uint;
13992 Delta_Val : Ureal;
13993 Scale_Val : Uint;
13994 Bound_Val : Ureal;
13996 begin
13997 Check_SPARK_05_Restriction
13998 ("decimal fixed point type is not allowed", Def);
13999 Check_Restriction (No_Fixed_Point, Def);
14001 -- Create implicit base type
14003 Implicit_Base :=
14004 Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
14005 Set_Etype (Implicit_Base, Implicit_Base);
14007 -- Analyze and process delta expression
14009 Analyze_And_Resolve (Delta_Expr, Universal_Real);
14011 Check_Delta_Expression (Delta_Expr);
14012 Delta_Val := Expr_Value_R (Delta_Expr);
14014 -- Check delta is power of 10, and determine scale value from it
14016 declare
14017 Val : Ureal;
14019 begin
14020 Scale_Val := Uint_0;
14021 Val := Delta_Val;
14023 if Val < Ureal_1 then
14024 while Val < Ureal_1 loop
14025 Val := Val * Ureal_10;
14026 Scale_Val := Scale_Val + 1;
14027 end loop;
14029 if Scale_Val > 18 then
14030 Error_Msg_N ("scale exceeds maximum value of 18", Def);
14031 Scale_Val := UI_From_Int (+18);
14032 end if;
14034 else
14035 while Val > Ureal_1 loop
14036 Val := Val / Ureal_10;
14037 Scale_Val := Scale_Val - 1;
14038 end loop;
14040 if Scale_Val < -18 then
14041 Error_Msg_N ("scale is less than minimum value of -18", Def);
14042 Scale_Val := UI_From_Int (-18);
14043 end if;
14044 end if;
14046 if Val /= Ureal_1 then
14047 Error_Msg_N ("delta expression must be a power of 10", Def);
14048 Delta_Val := Ureal_10 ** (-Scale_Val);
14049 end if;
14050 end;
14052 -- Set delta, scale and small (small = delta for decimal type)
14054 Set_Delta_Value (Implicit_Base, Delta_Val);
14055 Set_Scale_Value (Implicit_Base, Scale_Val);
14056 Set_Small_Value (Implicit_Base, Delta_Val);
14058 -- Analyze and process digits expression
14060 Analyze_And_Resolve (Digs_Expr, Any_Integer);
14061 Check_Digits_Expression (Digs_Expr);
14062 Digs_Val := Expr_Value (Digs_Expr);
14064 if Digs_Val > 18 then
14065 Digs_Val := UI_From_Int (+18);
14066 Error_Msg_N ("digits value out of range, maximum is 18", Digs_Expr);
14067 end if;
14069 Set_Digits_Value (Implicit_Base, Digs_Val);
14070 Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
14072 -- Set range of base type from digits value for now. This will be
14073 -- expanded to represent the true underlying base range by Freeze.
14075 Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
14077 -- Note: We leave size as zero for now, size will be set at freeze
14078 -- time. We have to do this for ordinary fixed-point, because the size
14079 -- depends on the specified small, and we might as well do the same for
14080 -- decimal fixed-point.
14082 pragma Assert (Esize (Implicit_Base) = Uint_0);
14084 -- If there are bounds given in the declaration use them as the
14085 -- bounds of the first named subtype.
14087 if Present (Real_Range_Specification (Def)) then
14088 declare
14089 RRS : constant Node_Id := Real_Range_Specification (Def);
14090 Low : constant Node_Id := Low_Bound (RRS);
14091 High : constant Node_Id := High_Bound (RRS);
14092 Low_Val : Ureal;
14093 High_Val : Ureal;
14095 begin
14096 Analyze_And_Resolve (Low, Any_Real);
14097 Analyze_And_Resolve (High, Any_Real);
14098 Check_Real_Bound (Low);
14099 Check_Real_Bound (High);
14100 Low_Val := Expr_Value_R (Low);
14101 High_Val := Expr_Value_R (High);
14103 if Low_Val < (-Bound_Val) then
14104 Error_Msg_N
14105 ("range low bound too small for digits value", Low);
14106 Low_Val := -Bound_Val;
14107 end if;
14109 if High_Val > Bound_Val then
14110 Error_Msg_N
14111 ("range high bound too large for digits value", High);
14112 High_Val := Bound_Val;
14113 end if;
14115 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
14116 end;
14118 -- If no explicit range, use range that corresponds to given
14119 -- digits value. This will end up as the final range for the
14120 -- first subtype.
14122 else
14123 Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
14124 end if;
14126 -- Complete entity for first subtype. The inheritance of the rep item
14127 -- chain ensures that SPARK-related pragmas are not clobbered when the
14128 -- decimal fixed point type acts as a full view of a private type.
14130 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
14131 Set_Etype (T, Implicit_Base);
14132 Set_Size_Info (T, Implicit_Base);
14133 Inherit_Rep_Item_Chain (T, Implicit_Base);
14134 Set_Digits_Value (T, Digs_Val);
14135 Set_Delta_Value (T, Delta_Val);
14136 Set_Small_Value (T, Delta_Val);
14137 Set_Scale_Value (T, Scale_Val);
14138 Set_Is_Constrained (T);
14139 end Decimal_Fixed_Point_Type_Declaration;
14141 -----------------------------------
14142 -- Derive_Progenitor_Subprograms --
14143 -----------------------------------
14145 procedure Derive_Progenitor_Subprograms
14146 (Parent_Type : Entity_Id;
14147 Tagged_Type : Entity_Id)
14149 E : Entity_Id;
14150 Elmt : Elmt_Id;
14151 Iface : Entity_Id;
14152 Iface_Elmt : Elmt_Id;
14153 Iface_Subp : Entity_Id;
14154 New_Subp : Entity_Id := Empty;
14155 Prim_Elmt : Elmt_Id;
14156 Subp : Entity_Id;
14157 Typ : Entity_Id;
14159 begin
14160 pragma Assert (Ada_Version >= Ada_2005
14161 and then Is_Record_Type (Tagged_Type)
14162 and then Is_Tagged_Type (Tagged_Type)
14163 and then Has_Interfaces (Tagged_Type));
14165 -- Step 1: Transfer to the full-view primitives associated with the
14166 -- partial-view that cover interface primitives. Conceptually this
14167 -- work should be done later by Process_Full_View; done here to
14168 -- simplify its implementation at later stages. It can be safely
14169 -- done here because interfaces must be visible in the partial and
14170 -- private view (RM 7.3(7.3/2)).
14172 -- Small optimization: This work is only required if the parent may
14173 -- have entities whose Alias attribute reference an interface primitive.
14174 -- Such a situation may occur if the parent is an abstract type and the
14175 -- primitive has not been yet overridden or if the parent is a generic
14176 -- formal type covering interfaces.
14178 -- If the tagged type is not abstract, it cannot have abstract
14179 -- primitives (the only entities in the list of primitives of
14180 -- non-abstract tagged types that can reference abstract primitives
14181 -- through its Alias attribute are the internal entities that have
14182 -- attribute Interface_Alias, and these entities are generated later
14183 -- by Add_Internal_Interface_Entities).
14185 if In_Private_Part (Current_Scope)
14186 and then (Is_Abstract_Type (Parent_Type)
14187 or else
14188 Is_Generic_Type (Parent_Type))
14189 then
14190 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
14191 while Present (Elmt) loop
14192 Subp := Node (Elmt);
14194 -- At this stage it is not possible to have entities in the list
14195 -- of primitives that have attribute Interface_Alias.
14197 pragma Assert (No (Interface_Alias (Subp)));
14199 Typ := Find_Dispatching_Type (Ultimate_Alias (Subp));
14201 if Is_Interface (Typ) then
14202 E := Find_Primitive_Covering_Interface
14203 (Tagged_Type => Tagged_Type,
14204 Iface_Prim => Subp);
14206 if Present (E)
14207 and then Find_Dispatching_Type (Ultimate_Alias (E)) /= Typ
14208 then
14209 Replace_Elmt (Elmt, E);
14210 Remove_Homonym (Subp);
14211 end if;
14212 end if;
14214 Next_Elmt (Elmt);
14215 end loop;
14216 end if;
14218 -- Step 2: Add primitives of progenitors that are not implemented by
14219 -- parents of Tagged_Type.
14221 if Present (Interfaces (Base_Type (Tagged_Type))) then
14222 Iface_Elmt := First_Elmt (Interfaces (Base_Type (Tagged_Type)));
14223 while Present (Iface_Elmt) loop
14224 Iface := Node (Iface_Elmt);
14226 Prim_Elmt := First_Elmt (Primitive_Operations (Iface));
14227 while Present (Prim_Elmt) loop
14228 Iface_Subp := Node (Prim_Elmt);
14230 -- Exclude derivation of predefined primitives except those
14231 -- that come from source, or are inherited from one that comes
14232 -- from source. Required to catch declarations of equality
14233 -- operators of interfaces. For example:
14235 -- type Iface is interface;
14236 -- function "=" (Left, Right : Iface) return Boolean;
14238 if not Is_Predefined_Dispatching_Operation (Iface_Subp)
14239 or else Comes_From_Source (Ultimate_Alias (Iface_Subp))
14240 then
14241 E := Find_Primitive_Covering_Interface
14242 (Tagged_Type => Tagged_Type,
14243 Iface_Prim => Iface_Subp);
14245 -- If not found we derive a new primitive leaving its alias
14246 -- attribute referencing the interface primitive.
14248 if No (E) then
14249 Derive_Subprogram
14250 (New_Subp, Iface_Subp, Tagged_Type, Iface);
14252 -- Ada 2012 (AI05-0197): If the covering primitive's name
14253 -- differs from the name of the interface primitive then it
14254 -- is a private primitive inherited from a parent type. In
14255 -- such case, given that Tagged_Type covers the interface,
14256 -- the inherited private primitive becomes visible. For such
14257 -- purpose we add a new entity that renames the inherited
14258 -- private primitive.
14260 elsif Chars (E) /= Chars (Iface_Subp) then
14261 pragma Assert (Has_Suffix (E, 'P'));
14262 Derive_Subprogram
14263 (New_Subp, Iface_Subp, Tagged_Type, Iface);
14264 Set_Alias (New_Subp, E);
14265 Set_Is_Abstract_Subprogram (New_Subp,
14266 Is_Abstract_Subprogram (E));
14268 -- Propagate to the full view interface entities associated
14269 -- with the partial view.
14271 elsif In_Private_Part (Current_Scope)
14272 and then Present (Alias (E))
14273 and then Alias (E) = Iface_Subp
14274 and then
14275 List_Containing (Parent (E)) /=
14276 Private_Declarations
14277 (Specification
14278 (Unit_Declaration_Node (Current_Scope)))
14279 then
14280 Append_Elmt (E, Primitive_Operations (Tagged_Type));
14281 end if;
14282 end if;
14284 Next_Elmt (Prim_Elmt);
14285 end loop;
14287 Next_Elmt (Iface_Elmt);
14288 end loop;
14289 end if;
14290 end Derive_Progenitor_Subprograms;
14292 -----------------------
14293 -- Derive_Subprogram --
14294 -----------------------
14296 procedure Derive_Subprogram
14297 (New_Subp : in out Entity_Id;
14298 Parent_Subp : Entity_Id;
14299 Derived_Type : Entity_Id;
14300 Parent_Type : Entity_Id;
14301 Actual_Subp : Entity_Id := Empty)
14303 Formal : Entity_Id;
14304 -- Formal parameter of parent primitive operation
14306 Formal_Of_Actual : Entity_Id;
14307 -- Formal parameter of actual operation, when the derivation is to
14308 -- create a renaming for a primitive operation of an actual in an
14309 -- instantiation.
14311 New_Formal : Entity_Id;
14312 -- Formal of inherited operation
14314 Visible_Subp : Entity_Id := Parent_Subp;
14316 function Is_Private_Overriding return Boolean;
14317 -- If Subp is a private overriding of a visible operation, the inherited
14318 -- operation derives from the overridden op (even though its body is the
14319 -- overriding one) and the inherited operation is visible now. See
14320 -- sem_disp to see the full details of the handling of the overridden
14321 -- subprogram, which is removed from the list of primitive operations of
14322 -- the type. The overridden subprogram is saved locally in Visible_Subp,
14323 -- and used to diagnose abstract operations that need overriding in the
14324 -- derived type.
14326 procedure Replace_Type (Id, New_Id : Entity_Id);
14327 -- When the type is an anonymous access type, create a new access type
14328 -- designating the derived type.
14330 procedure Set_Derived_Name;
14331 -- This procedure sets the appropriate Chars name for New_Subp. This
14332 -- is normally just a copy of the parent name. An exception arises for
14333 -- type support subprograms, where the name is changed to reflect the
14334 -- name of the derived type, e.g. if type foo is derived from type bar,
14335 -- then a procedure barDA is derived with a name fooDA.
14337 ---------------------------
14338 -- Is_Private_Overriding --
14339 ---------------------------
14341 function Is_Private_Overriding return Boolean is
14342 Prev : Entity_Id;
14344 begin
14345 -- If the parent is not a dispatching operation there is no
14346 -- need to investigate overridings
14348 if not Is_Dispatching_Operation (Parent_Subp) then
14349 return False;
14350 end if;
14352 -- The visible operation that is overridden is a homonym of the
14353 -- parent subprogram. We scan the homonym chain to find the one
14354 -- whose alias is the subprogram we are deriving.
14356 Prev := Current_Entity (Parent_Subp);
14357 while Present (Prev) loop
14358 if Ekind (Prev) = Ekind (Parent_Subp)
14359 and then Alias (Prev) = Parent_Subp
14360 and then Scope (Parent_Subp) = Scope (Prev)
14361 and then not Is_Hidden (Prev)
14362 then
14363 Visible_Subp := Prev;
14364 return True;
14365 end if;
14367 Prev := Homonym (Prev);
14368 end loop;
14370 return False;
14371 end Is_Private_Overriding;
14373 ------------------
14374 -- Replace_Type --
14375 ------------------
14377 procedure Replace_Type (Id, New_Id : Entity_Id) is
14378 Id_Type : constant Entity_Id := Etype (Id);
14379 Acc_Type : Entity_Id;
14380 Par : constant Node_Id := Parent (Derived_Type);
14382 begin
14383 -- When the type is an anonymous access type, create a new access
14384 -- type designating the derived type. This itype must be elaborated
14385 -- at the point of the derivation, not on subsequent calls that may
14386 -- be out of the proper scope for Gigi, so we insert a reference to
14387 -- it after the derivation.
14389 if Ekind (Id_Type) = E_Anonymous_Access_Type then
14390 declare
14391 Desig_Typ : Entity_Id := Designated_Type (Id_Type);
14393 begin
14394 if Ekind (Desig_Typ) = E_Record_Type_With_Private
14395 and then Present (Full_View (Desig_Typ))
14396 and then not Is_Private_Type (Parent_Type)
14397 then
14398 Desig_Typ := Full_View (Desig_Typ);
14399 end if;
14401 if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
14403 -- Ada 2005 (AI-251): Handle also derivations of abstract
14404 -- interface primitives.
14406 or else (Is_Interface (Desig_Typ)
14407 and then not Is_Class_Wide_Type (Desig_Typ))
14408 then
14409 Acc_Type := New_Copy (Id_Type);
14410 Set_Etype (Acc_Type, Acc_Type);
14411 Set_Scope (Acc_Type, New_Subp);
14413 -- Set size of anonymous access type. If we have an access
14414 -- to an unconstrained array, this is a fat pointer, so it
14415 -- is sizes at twice addtress size.
14417 if Is_Array_Type (Desig_Typ)
14418 and then not Is_Constrained (Desig_Typ)
14419 then
14420 Init_Size (Acc_Type, 2 * System_Address_Size);
14422 -- Other cases use a thin pointer
14424 else
14425 Init_Size (Acc_Type, System_Address_Size);
14426 end if;
14428 -- Set remaining characterstics of anonymous access type
14430 Init_Alignment (Acc_Type);
14431 Set_Directly_Designated_Type (Acc_Type, Derived_Type);
14433 Set_Etype (New_Id, Acc_Type);
14434 Set_Scope (New_Id, New_Subp);
14436 -- Create a reference to it
14438 Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
14440 else
14441 Set_Etype (New_Id, Id_Type);
14442 end if;
14443 end;
14445 -- In Ada2012, a formal may have an incomplete type but the type
14446 -- derivation that inherits the primitive follows the full view.
14448 elsif Base_Type (Id_Type) = Base_Type (Parent_Type)
14449 or else
14450 (Ekind (Id_Type) = E_Record_Type_With_Private
14451 and then Present (Full_View (Id_Type))
14452 and then
14453 Base_Type (Full_View (Id_Type)) = Base_Type (Parent_Type))
14454 or else
14455 (Ada_Version >= Ada_2012
14456 and then Ekind (Id_Type) = E_Incomplete_Type
14457 and then Full_View (Id_Type) = Parent_Type)
14458 then
14459 -- Constraint checks on formals are generated during expansion,
14460 -- based on the signature of the original subprogram. The bounds
14461 -- of the derived type are not relevant, and thus we can use
14462 -- the base type for the formals. However, the return type may be
14463 -- used in a context that requires that the proper static bounds
14464 -- be used (a case statement, for example) and for those cases
14465 -- we must use the derived type (first subtype), not its base.
14467 -- If the derived_type_definition has no constraints, we know that
14468 -- the derived type has the same constraints as the first subtype
14469 -- of the parent, and we can also use it rather than its base,
14470 -- which can lead to more efficient code.
14472 if Etype (Id) = Parent_Type then
14473 if Is_Scalar_Type (Parent_Type)
14474 and then
14475 Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
14476 then
14477 Set_Etype (New_Id, Derived_Type);
14479 elsif Nkind (Par) = N_Full_Type_Declaration
14480 and then
14481 Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
14482 and then
14483 Is_Entity_Name
14484 (Subtype_Indication (Type_Definition (Par)))
14485 then
14486 Set_Etype (New_Id, Derived_Type);
14488 else
14489 Set_Etype (New_Id, Base_Type (Derived_Type));
14490 end if;
14492 else
14493 Set_Etype (New_Id, Base_Type (Derived_Type));
14494 end if;
14496 else
14497 Set_Etype (New_Id, Etype (Id));
14498 end if;
14499 end Replace_Type;
14501 ----------------------
14502 -- Set_Derived_Name --
14503 ----------------------
14505 procedure Set_Derived_Name is
14506 Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
14507 begin
14508 if Nm = TSS_Null then
14509 Set_Chars (New_Subp, Chars (Parent_Subp));
14510 else
14511 Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
14512 end if;
14513 end Set_Derived_Name;
14515 -- Start of processing for Derive_Subprogram
14517 begin
14518 New_Subp := New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
14519 Set_Ekind (New_Subp, Ekind (Parent_Subp));
14521 -- Check whether the inherited subprogram is a private operation that
14522 -- should be inherited but not yet made visible. Such subprograms can
14523 -- become visible at a later point (e.g., the private part of a public
14524 -- child unit) via Declare_Inherited_Private_Subprograms. If the
14525 -- following predicate is true, then this is not such a private
14526 -- operation and the subprogram simply inherits the name of the parent
14527 -- subprogram. Note the special check for the names of controlled
14528 -- operations, which are currently exempted from being inherited with
14529 -- a hidden name because they must be findable for generation of
14530 -- implicit run-time calls.
14532 if not Is_Hidden (Parent_Subp)
14533 or else Is_Internal (Parent_Subp)
14534 or else Is_Private_Overriding
14535 or else Is_Internal_Name (Chars (Parent_Subp))
14536 or else Nam_In (Chars (Parent_Subp), Name_Initialize,
14537 Name_Adjust,
14538 Name_Finalize)
14539 then
14540 Set_Derived_Name;
14542 -- An inherited dispatching equality will be overridden by an internally
14543 -- generated one, or by an explicit one, so preserve its name and thus
14544 -- its entry in the dispatch table. Otherwise, if Parent_Subp is a
14545 -- private operation it may become invisible if the full view has
14546 -- progenitors, and the dispatch table will be malformed.
14547 -- We check that the type is limited to handle the anomalous declaration
14548 -- of Limited_Controlled, which is derived from a non-limited type, and
14549 -- which is handled specially elsewhere as well.
14551 elsif Chars (Parent_Subp) = Name_Op_Eq
14552 and then Is_Dispatching_Operation (Parent_Subp)
14553 and then Etype (Parent_Subp) = Standard_Boolean
14554 and then not Is_Limited_Type (Etype (First_Formal (Parent_Subp)))
14555 and then
14556 Etype (First_Formal (Parent_Subp)) =
14557 Etype (Next_Formal (First_Formal (Parent_Subp)))
14558 then
14559 Set_Derived_Name;
14561 -- If parent is hidden, this can be a regular derivation if the
14562 -- parent is immediately visible in a non-instantiating context,
14563 -- or if we are in the private part of an instance. This test
14564 -- should still be refined ???
14566 -- The test for In_Instance_Not_Visible avoids inheriting the derived
14567 -- operation as a non-visible operation in cases where the parent
14568 -- subprogram might not be visible now, but was visible within the
14569 -- original generic, so it would be wrong to make the inherited
14570 -- subprogram non-visible now. (Not clear if this test is fully
14571 -- correct; are there any cases where we should declare the inherited
14572 -- operation as not visible to avoid it being overridden, e.g., when
14573 -- the parent type is a generic actual with private primitives ???)
14575 -- (they should be treated the same as other private inherited
14576 -- subprograms, but it's not clear how to do this cleanly). ???
14578 elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
14579 and then Is_Immediately_Visible (Parent_Subp)
14580 and then not In_Instance)
14581 or else In_Instance_Not_Visible
14582 then
14583 Set_Derived_Name;
14585 -- Ada 2005 (AI-251): Regular derivation if the parent subprogram
14586 -- overrides an interface primitive because interface primitives
14587 -- must be visible in the partial view of the parent (RM 7.3 (7.3/2))
14589 elsif Ada_Version >= Ada_2005
14590 and then Is_Dispatching_Operation (Parent_Subp)
14591 and then Covers_Some_Interface (Parent_Subp)
14592 then
14593 Set_Derived_Name;
14595 -- Otherwise, the type is inheriting a private operation, so enter
14596 -- it with a special name so it can't be overridden.
14598 else
14599 Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
14600 end if;
14602 Set_Parent (New_Subp, Parent (Derived_Type));
14604 if Present (Actual_Subp) then
14605 Replace_Type (Actual_Subp, New_Subp);
14606 else
14607 Replace_Type (Parent_Subp, New_Subp);
14608 end if;
14610 Conditional_Delay (New_Subp, Parent_Subp);
14612 -- If we are creating a renaming for a primitive operation of an
14613 -- actual of a generic derived type, we must examine the signature
14614 -- of the actual primitive, not that of the generic formal, which for
14615 -- example may be an interface. However the name and initial value
14616 -- of the inherited operation are those of the formal primitive.
14618 Formal := First_Formal (Parent_Subp);
14620 if Present (Actual_Subp) then
14621 Formal_Of_Actual := First_Formal (Actual_Subp);
14622 else
14623 Formal_Of_Actual := Empty;
14624 end if;
14626 while Present (Formal) loop
14627 New_Formal := New_Copy (Formal);
14629 -- Normally we do not go copying parents, but in the case of
14630 -- formals, we need to link up to the declaration (which is the
14631 -- parameter specification), and it is fine to link up to the
14632 -- original formal's parameter specification in this case.
14634 Set_Parent (New_Formal, Parent (Formal));
14635 Append_Entity (New_Formal, New_Subp);
14637 if Present (Formal_Of_Actual) then
14638 Replace_Type (Formal_Of_Actual, New_Formal);
14639 Next_Formal (Formal_Of_Actual);
14640 else
14641 Replace_Type (Formal, New_Formal);
14642 end if;
14644 Next_Formal (Formal);
14645 end loop;
14647 -- If this derivation corresponds to a tagged generic actual, then
14648 -- primitive operations rename those of the actual. Otherwise the
14649 -- primitive operations rename those of the parent type, If the parent
14650 -- renames an intrinsic operator, so does the new subprogram. We except
14651 -- concatenation, which is always properly typed, and does not get
14652 -- expanded as other intrinsic operations.
14654 if No (Actual_Subp) then
14655 if Is_Intrinsic_Subprogram (Parent_Subp) then
14656 Set_Is_Intrinsic_Subprogram (New_Subp);
14658 if Present (Alias (Parent_Subp))
14659 and then Chars (Parent_Subp) /= Name_Op_Concat
14660 then
14661 Set_Alias (New_Subp, Alias (Parent_Subp));
14662 else
14663 Set_Alias (New_Subp, Parent_Subp);
14664 end if;
14666 else
14667 Set_Alias (New_Subp, Parent_Subp);
14668 end if;
14670 else
14671 Set_Alias (New_Subp, Actual_Subp);
14672 end if;
14674 -- Inherit the "ghostness" from the parent subprogram
14676 if Is_Ghost_Entity (Alias (New_Subp)) then
14677 Set_Is_Ghost_Entity (New_Subp);
14678 end if;
14680 -- Derived subprograms of a tagged type must inherit the convention
14681 -- of the parent subprogram (a requirement of AI-117). Derived
14682 -- subprograms of untagged types simply get convention Ada by default.
14684 -- If the derived type is a tagged generic formal type with unknown
14685 -- discriminants, its convention is intrinsic (RM 6.3.1 (8)).
14687 -- However, if the type is derived from a generic formal, the further
14688 -- inherited subprogram has the convention of the non-generic ancestor.
14689 -- Otherwise there would be no way to override the operation.
14690 -- (This is subject to forthcoming ARG discussions).
14692 if Is_Tagged_Type (Derived_Type) then
14693 if Is_Generic_Type (Derived_Type)
14694 and then Has_Unknown_Discriminants (Derived_Type)
14695 then
14696 Set_Convention (New_Subp, Convention_Intrinsic);
14698 else
14699 if Is_Generic_Type (Parent_Type)
14700 and then Has_Unknown_Discriminants (Parent_Type)
14701 then
14702 Set_Convention (New_Subp, Convention (Alias (Parent_Subp)));
14703 else
14704 Set_Convention (New_Subp, Convention (Parent_Subp));
14705 end if;
14706 end if;
14707 end if;
14709 -- Predefined controlled operations retain their name even if the parent
14710 -- is hidden (see above), but they are not primitive operations if the
14711 -- ancestor is not visible, for example if the parent is a private
14712 -- extension completed with a controlled extension. Note that a full
14713 -- type that is controlled can break privacy: the flag Is_Controlled is
14714 -- set on both views of the type.
14716 if Is_Controlled (Parent_Type)
14717 and then Nam_In (Chars (Parent_Subp), Name_Initialize,
14718 Name_Adjust,
14719 Name_Finalize)
14720 and then Is_Hidden (Parent_Subp)
14721 and then not Is_Visibly_Controlled (Parent_Type)
14722 then
14723 Set_Is_Hidden (New_Subp);
14724 end if;
14726 Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
14727 Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
14729 if Ekind (Parent_Subp) = E_Procedure then
14730 Set_Is_Valued_Procedure
14731 (New_Subp, Is_Valued_Procedure (Parent_Subp));
14732 else
14733 Set_Has_Controlling_Result
14734 (New_Subp, Has_Controlling_Result (Parent_Subp));
14735 end if;
14737 -- No_Return must be inherited properly. If this is overridden in the
14738 -- case of a dispatching operation, then a check is made in Sem_Disp
14739 -- that the overriding operation is also No_Return (no such check is
14740 -- required for the case of non-dispatching operation.
14742 Set_No_Return (New_Subp, No_Return (Parent_Subp));
14744 -- A derived function with a controlling result is abstract. If the
14745 -- Derived_Type is a nonabstract formal generic derived type, then
14746 -- inherited operations are not abstract: the required check is done at
14747 -- instantiation time. If the derivation is for a generic actual, the
14748 -- function is not abstract unless the actual is.
14750 if Is_Generic_Type (Derived_Type)
14751 and then not Is_Abstract_Type (Derived_Type)
14752 then
14753 null;
14755 -- Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
14756 -- properties of the subprogram, as defined in RM-3.9.3(4/2-6/2).
14758 -- A subprogram subject to pragma Extensions_Visible with value False
14759 -- requires overriding if the subprogram has at least one controlling
14760 -- OUT parameter (SPARK RM 6.1.7(6)).
14762 elsif Ada_Version >= Ada_2005
14763 and then (Is_Abstract_Subprogram (Alias (New_Subp))
14764 or else (Is_Tagged_Type (Derived_Type)
14765 and then Etype (New_Subp) = Derived_Type
14766 and then not Is_Null_Extension (Derived_Type))
14767 or else (Is_Tagged_Type (Derived_Type)
14768 and then Ekind (Etype (New_Subp)) =
14769 E_Anonymous_Access_Type
14770 and then Designated_Type (Etype (New_Subp)) =
14771 Derived_Type
14772 and then not Is_Null_Extension (Derived_Type))
14773 or else (Comes_From_Source (Alias (New_Subp))
14774 and then Is_EVF_Procedure (Alias (New_Subp))))
14775 and then No (Actual_Subp)
14776 then
14777 if not Is_Tagged_Type (Derived_Type)
14778 or else Is_Abstract_Type (Derived_Type)
14779 or else Is_Abstract_Subprogram (Alias (New_Subp))
14780 then
14781 Set_Is_Abstract_Subprogram (New_Subp);
14782 else
14783 Set_Requires_Overriding (New_Subp);
14784 end if;
14786 elsif Ada_Version < Ada_2005
14787 and then (Is_Abstract_Subprogram (Alias (New_Subp))
14788 or else (Is_Tagged_Type (Derived_Type)
14789 and then Etype (New_Subp) = Derived_Type
14790 and then No (Actual_Subp)))
14791 then
14792 Set_Is_Abstract_Subprogram (New_Subp);
14794 -- AI05-0097 : an inherited operation that dispatches on result is
14795 -- abstract if the derived type is abstract, even if the parent type
14796 -- is concrete and the derived type is a null extension.
14798 elsif Has_Controlling_Result (Alias (New_Subp))
14799 and then Is_Abstract_Type (Etype (New_Subp))
14800 then
14801 Set_Is_Abstract_Subprogram (New_Subp);
14803 -- Finally, if the parent type is abstract we must verify that all
14804 -- inherited operations are either non-abstract or overridden, or that
14805 -- the derived type itself is abstract (this check is performed at the
14806 -- end of a package declaration, in Check_Abstract_Overriding). A
14807 -- private overriding in the parent type will not be visible in the
14808 -- derivation if we are not in an inner package or in a child unit of
14809 -- the parent type, in which case the abstractness of the inherited
14810 -- operation is carried to the new subprogram.
14812 elsif Is_Abstract_Type (Parent_Type)
14813 and then not In_Open_Scopes (Scope (Parent_Type))
14814 and then Is_Private_Overriding
14815 and then Is_Abstract_Subprogram (Visible_Subp)
14816 then
14817 if No (Actual_Subp) then
14818 Set_Alias (New_Subp, Visible_Subp);
14819 Set_Is_Abstract_Subprogram (New_Subp, True);
14821 else
14822 -- If this is a derivation for an instance of a formal derived
14823 -- type, abstractness comes from the primitive operation of the
14824 -- actual, not from the operation inherited from the ancestor.
14826 Set_Is_Abstract_Subprogram
14827 (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
14828 end if;
14829 end if;
14831 New_Overloaded_Entity (New_Subp, Derived_Type);
14833 -- Check for case of a derived subprogram for the instantiation of a
14834 -- formal derived tagged type, if so mark the subprogram as dispatching
14835 -- and inherit the dispatching attributes of the actual subprogram. The
14836 -- derived subprogram is effectively renaming of the actual subprogram,
14837 -- so it needs to have the same attributes as the actual.
14839 if Present (Actual_Subp)
14840 and then Is_Dispatching_Operation (Actual_Subp)
14841 then
14842 Set_Is_Dispatching_Operation (New_Subp);
14844 if Present (DTC_Entity (Actual_Subp)) then
14845 Set_DTC_Entity (New_Subp, DTC_Entity (Actual_Subp));
14846 Set_DT_Position_Value (New_Subp, DT_Position (Actual_Subp));
14847 end if;
14848 end if;
14850 -- Indicate that a derived subprogram does not require a body and that
14851 -- it does not require processing of default expressions.
14853 Set_Has_Completion (New_Subp);
14854 Set_Default_Expressions_Processed (New_Subp);
14856 if Ekind (New_Subp) = E_Function then
14857 Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
14858 end if;
14859 end Derive_Subprogram;
14861 ------------------------
14862 -- Derive_Subprograms --
14863 ------------------------
14865 procedure Derive_Subprograms
14866 (Parent_Type : Entity_Id;
14867 Derived_Type : Entity_Id;
14868 Generic_Actual : Entity_Id := Empty)
14870 Op_List : constant Elist_Id :=
14871 Collect_Primitive_Operations (Parent_Type);
14873 function Check_Derived_Type return Boolean;
14874 -- Check that all the entities derived from Parent_Type are found in
14875 -- the list of primitives of Derived_Type exactly in the same order.
14877 procedure Derive_Interface_Subprogram
14878 (New_Subp : in out Entity_Id;
14879 Subp : Entity_Id;
14880 Actual_Subp : Entity_Id);
14881 -- Derive New_Subp from the ultimate alias of the parent subprogram Subp
14882 -- (which is an interface primitive). If Generic_Actual is present then
14883 -- Actual_Subp is the actual subprogram corresponding with the generic
14884 -- subprogram Subp.
14886 function Check_Derived_Type return Boolean is
14887 E : Entity_Id;
14888 Elmt : Elmt_Id;
14889 List : Elist_Id;
14890 New_Subp : Entity_Id;
14891 Op_Elmt : Elmt_Id;
14892 Subp : Entity_Id;
14894 begin
14895 -- Traverse list of entities in the current scope searching for
14896 -- an incomplete type whose full-view is derived type
14898 E := First_Entity (Scope (Derived_Type));
14899 while Present (E) and then E /= Derived_Type loop
14900 if Ekind (E) = E_Incomplete_Type
14901 and then Present (Full_View (E))
14902 and then Full_View (E) = Derived_Type
14903 then
14904 -- Disable this test if Derived_Type completes an incomplete
14905 -- type because in such case more primitives can be added
14906 -- later to the list of primitives of Derived_Type by routine
14907 -- Process_Incomplete_Dependents
14909 return True;
14910 end if;
14912 E := Next_Entity (E);
14913 end loop;
14915 List := Collect_Primitive_Operations (Derived_Type);
14916 Elmt := First_Elmt (List);
14918 Op_Elmt := First_Elmt (Op_List);
14919 while Present (Op_Elmt) loop
14920 Subp := Node (Op_Elmt);
14921 New_Subp := Node (Elmt);
14923 -- At this early stage Derived_Type has no entities with attribute
14924 -- Interface_Alias. In addition, such primitives are always
14925 -- located at the end of the list of primitives of Parent_Type.
14926 -- Therefore, if found we can safely stop processing pending
14927 -- entities.
14929 exit when Present (Interface_Alias (Subp));
14931 -- Handle hidden entities
14933 if not Is_Predefined_Dispatching_Operation (Subp)
14934 and then Is_Hidden (Subp)
14935 then
14936 if Present (New_Subp)
14937 and then Primitive_Names_Match (Subp, New_Subp)
14938 then
14939 Next_Elmt (Elmt);
14940 end if;
14942 else
14943 if not Present (New_Subp)
14944 or else Ekind (Subp) /= Ekind (New_Subp)
14945 or else not Primitive_Names_Match (Subp, New_Subp)
14946 then
14947 return False;
14948 end if;
14950 Next_Elmt (Elmt);
14951 end if;
14953 Next_Elmt (Op_Elmt);
14954 end loop;
14956 return True;
14957 end Check_Derived_Type;
14959 ---------------------------------
14960 -- Derive_Interface_Subprogram --
14961 ---------------------------------
14963 procedure Derive_Interface_Subprogram
14964 (New_Subp : in out Entity_Id;
14965 Subp : Entity_Id;
14966 Actual_Subp : Entity_Id)
14968 Iface_Subp : constant Entity_Id := Ultimate_Alias (Subp);
14969 Iface_Type : constant Entity_Id := Find_Dispatching_Type (Iface_Subp);
14971 begin
14972 pragma Assert (Is_Interface (Iface_Type));
14974 Derive_Subprogram
14975 (New_Subp => New_Subp,
14976 Parent_Subp => Iface_Subp,
14977 Derived_Type => Derived_Type,
14978 Parent_Type => Iface_Type,
14979 Actual_Subp => Actual_Subp);
14981 -- Given that this new interface entity corresponds with a primitive
14982 -- of the parent that was not overridden we must leave it associated
14983 -- with its parent primitive to ensure that it will share the same
14984 -- dispatch table slot when overridden.
14986 if No (Actual_Subp) then
14987 Set_Alias (New_Subp, Subp);
14989 -- For instantiations this is not needed since the previous call to
14990 -- Derive_Subprogram leaves the entity well decorated.
14992 else
14993 pragma Assert (Alias (New_Subp) = Actual_Subp);
14994 null;
14995 end if;
14996 end Derive_Interface_Subprogram;
14998 -- Local variables
15000 Alias_Subp : Entity_Id;
15001 Act_List : Elist_Id;
15002 Act_Elmt : Elmt_Id;
15003 Act_Subp : Entity_Id := Empty;
15004 Elmt : Elmt_Id;
15005 Need_Search : Boolean := False;
15006 New_Subp : Entity_Id := Empty;
15007 Parent_Base : Entity_Id;
15008 Subp : Entity_Id;
15010 -- Start of processing for Derive_Subprograms
15012 begin
15013 if Ekind (Parent_Type) = E_Record_Type_With_Private
15014 and then Has_Discriminants (Parent_Type)
15015 and then Present (Full_View (Parent_Type))
15016 then
15017 Parent_Base := Full_View (Parent_Type);
15018 else
15019 Parent_Base := Parent_Type;
15020 end if;
15022 if Present (Generic_Actual) then
15023 Act_List := Collect_Primitive_Operations (Generic_Actual);
15024 Act_Elmt := First_Elmt (Act_List);
15025 else
15026 Act_List := No_Elist;
15027 Act_Elmt := No_Elmt;
15028 end if;
15030 -- Derive primitives inherited from the parent. Note that if the generic
15031 -- actual is present, this is not really a type derivation, it is a
15032 -- completion within an instance.
15034 -- Case 1: Derived_Type does not implement interfaces
15036 if not Is_Tagged_Type (Derived_Type)
15037 or else (not Has_Interfaces (Derived_Type)
15038 and then not (Present (Generic_Actual)
15039 and then Has_Interfaces (Generic_Actual)))
15040 then
15041 Elmt := First_Elmt (Op_List);
15042 while Present (Elmt) loop
15043 Subp := Node (Elmt);
15045 -- Literals are derived earlier in the process of building the
15046 -- derived type, and are skipped here.
15048 if Ekind (Subp) = E_Enumeration_Literal then
15049 null;
15051 -- The actual is a direct descendant and the common primitive
15052 -- operations appear in the same order.
15054 -- If the generic parent type is present, the derived type is an
15055 -- instance of a formal derived type, and within the instance its
15056 -- operations are those of the actual. We derive from the formal
15057 -- type but make the inherited operations aliases of the
15058 -- corresponding operations of the actual.
15060 else
15061 pragma Assert (No (Node (Act_Elmt))
15062 or else (Primitive_Names_Match (Subp, Node (Act_Elmt))
15063 and then
15064 Type_Conformant
15065 (Subp, Node (Act_Elmt),
15066 Skip_Controlling_Formals => True)));
15068 Derive_Subprogram
15069 (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
15071 if Present (Act_Elmt) then
15072 Next_Elmt (Act_Elmt);
15073 end if;
15074 end if;
15076 Next_Elmt (Elmt);
15077 end loop;
15079 -- Case 2: Derived_Type implements interfaces
15081 else
15082 -- If the parent type has no predefined primitives we remove
15083 -- predefined primitives from the list of primitives of generic
15084 -- actual to simplify the complexity of this algorithm.
15086 if Present (Generic_Actual) then
15087 declare
15088 Has_Predefined_Primitives : Boolean := False;
15090 begin
15091 -- Check if the parent type has predefined primitives
15093 Elmt := First_Elmt (Op_List);
15094 while Present (Elmt) loop
15095 Subp := Node (Elmt);
15097 if Is_Predefined_Dispatching_Operation (Subp)
15098 and then not Comes_From_Source (Ultimate_Alias (Subp))
15099 then
15100 Has_Predefined_Primitives := True;
15101 exit;
15102 end if;
15104 Next_Elmt (Elmt);
15105 end loop;
15107 -- Remove predefined primitives of Generic_Actual. We must use
15108 -- an auxiliary list because in case of tagged types the value
15109 -- returned by Collect_Primitive_Operations is the value stored
15110 -- in its Primitive_Operations attribute (and we don't want to
15111 -- modify its current contents).
15113 if not Has_Predefined_Primitives then
15114 declare
15115 Aux_List : constant Elist_Id := New_Elmt_List;
15117 begin
15118 Elmt := First_Elmt (Act_List);
15119 while Present (Elmt) loop
15120 Subp := Node (Elmt);
15122 if not Is_Predefined_Dispatching_Operation (Subp)
15123 or else Comes_From_Source (Subp)
15124 then
15125 Append_Elmt (Subp, Aux_List);
15126 end if;
15128 Next_Elmt (Elmt);
15129 end loop;
15131 Act_List := Aux_List;
15132 end;
15133 end if;
15135 Act_Elmt := First_Elmt (Act_List);
15136 Act_Subp := Node (Act_Elmt);
15137 end;
15138 end if;
15140 -- Stage 1: If the generic actual is not present we derive the
15141 -- primitives inherited from the parent type. If the generic parent
15142 -- type is present, the derived type is an instance of a formal
15143 -- derived type, and within the instance its operations are those of
15144 -- the actual. We derive from the formal type but make the inherited
15145 -- operations aliases of the corresponding operations of the actual.
15147 Elmt := First_Elmt (Op_List);
15148 while Present (Elmt) loop
15149 Subp := Node (Elmt);
15150 Alias_Subp := Ultimate_Alias (Subp);
15152 -- Do not derive internal entities of the parent that link
15153 -- interface primitives with their covering primitive. These
15154 -- entities will be added to this type when frozen.
15156 if Present (Interface_Alias (Subp)) then
15157 goto Continue;
15158 end if;
15160 -- If the generic actual is present find the corresponding
15161 -- operation in the generic actual. If the parent type is a
15162 -- direct ancestor of the derived type then, even if it is an
15163 -- interface, the operations are inherited from the primary
15164 -- dispatch table and are in the proper order. If we detect here
15165 -- that primitives are not in the same order we traverse the list
15166 -- of primitive operations of the actual to find the one that
15167 -- implements the interface primitive.
15169 if Need_Search
15170 or else
15171 (Present (Generic_Actual)
15172 and then Present (Act_Subp)
15173 and then not
15174 (Primitive_Names_Match (Subp, Act_Subp)
15175 and then
15176 Type_Conformant (Subp, Act_Subp,
15177 Skip_Controlling_Formals => True)))
15178 then
15179 pragma Assert (not Is_Ancestor (Parent_Base, Generic_Actual,
15180 Use_Full_View => True));
15182 -- Remember that we need searching for all pending primitives
15184 Need_Search := True;
15186 -- Handle entities associated with interface primitives
15188 if Present (Alias_Subp)
15189 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
15190 and then not Is_Predefined_Dispatching_Operation (Subp)
15191 then
15192 -- Search for the primitive in the homonym chain
15194 Act_Subp :=
15195 Find_Primitive_Covering_Interface
15196 (Tagged_Type => Generic_Actual,
15197 Iface_Prim => Alias_Subp);
15199 -- Previous search may not locate primitives covering
15200 -- interfaces defined in generics units or instantiations.
15201 -- (it fails if the covering primitive has formals whose
15202 -- type is also defined in generics or instantiations).
15203 -- In such case we search in the list of primitives of the
15204 -- generic actual for the internal entity that links the
15205 -- interface primitive and the covering primitive.
15207 if No (Act_Subp)
15208 and then Is_Generic_Type (Parent_Type)
15209 then
15210 -- This code has been designed to handle only generic
15211 -- formals that implement interfaces that are defined
15212 -- in a generic unit or instantiation. If this code is
15213 -- needed for other cases we must review it because
15214 -- (given that it relies on Original_Location to locate
15215 -- the primitive of Generic_Actual that covers the
15216 -- interface) it could leave linked through attribute
15217 -- Alias entities of unrelated instantiations).
15219 pragma Assert
15220 (Is_Generic_Unit
15221 (Scope (Find_Dispatching_Type (Alias_Subp)))
15222 or else
15223 Instantiation_Depth
15224 (Sloc (Find_Dispatching_Type (Alias_Subp))) > 0);
15226 declare
15227 Iface_Prim_Loc : constant Source_Ptr :=
15228 Original_Location (Sloc (Alias_Subp));
15230 Elmt : Elmt_Id;
15231 Prim : Entity_Id;
15233 begin
15234 Elmt :=
15235 First_Elmt (Primitive_Operations (Generic_Actual));
15237 Search : while Present (Elmt) loop
15238 Prim := Node (Elmt);
15240 if Present (Interface_Alias (Prim))
15241 and then Original_Location
15242 (Sloc (Interface_Alias (Prim))) =
15243 Iface_Prim_Loc
15244 then
15245 Act_Subp := Alias (Prim);
15246 exit Search;
15247 end if;
15249 Next_Elmt (Elmt);
15250 end loop Search;
15251 end;
15252 end if;
15254 pragma Assert (Present (Act_Subp)
15255 or else Is_Abstract_Type (Generic_Actual)
15256 or else Serious_Errors_Detected > 0);
15258 -- Handle predefined primitives plus the rest of user-defined
15259 -- primitives
15261 else
15262 Act_Elmt := First_Elmt (Act_List);
15263 while Present (Act_Elmt) loop
15264 Act_Subp := Node (Act_Elmt);
15266 exit when Primitive_Names_Match (Subp, Act_Subp)
15267 and then Type_Conformant
15268 (Subp, Act_Subp,
15269 Skip_Controlling_Formals => True)
15270 and then No (Interface_Alias (Act_Subp));
15272 Next_Elmt (Act_Elmt);
15273 end loop;
15275 if No (Act_Elmt) then
15276 Act_Subp := Empty;
15277 end if;
15278 end if;
15279 end if;
15281 -- Case 1: If the parent is a limited interface then it has the
15282 -- predefined primitives of synchronized interfaces. However, the
15283 -- actual type may be a non-limited type and hence it does not
15284 -- have such primitives.
15286 if Present (Generic_Actual)
15287 and then not Present (Act_Subp)
15288 and then Is_Limited_Interface (Parent_Base)
15289 and then Is_Predefined_Interface_Primitive (Subp)
15290 then
15291 null;
15293 -- Case 2: Inherit entities associated with interfaces that were
15294 -- not covered by the parent type. We exclude here null interface
15295 -- primitives because they do not need special management.
15297 -- We also exclude interface operations that are renamings. If the
15298 -- subprogram is an explicit renaming of an interface primitive,
15299 -- it is a regular primitive operation, and the presence of its
15300 -- alias is not relevant: it has to be derived like any other
15301 -- primitive.
15303 elsif Present (Alias (Subp))
15304 and then Nkind (Unit_Declaration_Node (Subp)) /=
15305 N_Subprogram_Renaming_Declaration
15306 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
15307 and then not
15308 (Nkind (Parent (Alias_Subp)) = N_Procedure_Specification
15309 and then Null_Present (Parent (Alias_Subp)))
15310 then
15311 -- If this is an abstract private type then we transfer the
15312 -- derivation of the interface primitive from the partial view
15313 -- to the full view. This is safe because all the interfaces
15314 -- must be visible in the partial view. Done to avoid adding
15315 -- a new interface derivation to the private part of the
15316 -- enclosing package; otherwise this new derivation would be
15317 -- decorated as hidden when the analysis of the enclosing
15318 -- package completes.
15320 if Is_Abstract_Type (Derived_Type)
15321 and then In_Private_Part (Current_Scope)
15322 and then Has_Private_Declaration (Derived_Type)
15323 then
15324 declare
15325 Partial_View : Entity_Id;
15326 Elmt : Elmt_Id;
15327 Ent : Entity_Id;
15329 begin
15330 Partial_View := First_Entity (Current_Scope);
15331 loop
15332 exit when No (Partial_View)
15333 or else (Has_Private_Declaration (Partial_View)
15334 and then
15335 Full_View (Partial_View) = Derived_Type);
15337 Next_Entity (Partial_View);
15338 end loop;
15340 -- If the partial view was not found then the source code
15341 -- has errors and the derivation is not needed.
15343 if Present (Partial_View) then
15344 Elmt :=
15345 First_Elmt (Primitive_Operations (Partial_View));
15346 while Present (Elmt) loop
15347 Ent := Node (Elmt);
15349 if Present (Alias (Ent))
15350 and then Ultimate_Alias (Ent) = Alias (Subp)
15351 then
15352 Append_Elmt
15353 (Ent, Primitive_Operations (Derived_Type));
15354 exit;
15355 end if;
15357 Next_Elmt (Elmt);
15358 end loop;
15360 -- If the interface primitive was not found in the
15361 -- partial view then this interface primitive was
15362 -- overridden. We add a derivation to activate in
15363 -- Derive_Progenitor_Subprograms the machinery to
15364 -- search for it.
15366 if No (Elmt) then
15367 Derive_Interface_Subprogram
15368 (New_Subp => New_Subp,
15369 Subp => Subp,
15370 Actual_Subp => Act_Subp);
15371 end if;
15372 end if;
15373 end;
15374 else
15375 Derive_Interface_Subprogram
15376 (New_Subp => New_Subp,
15377 Subp => Subp,
15378 Actual_Subp => Act_Subp);
15379 end if;
15381 -- Case 3: Common derivation
15383 else
15384 Derive_Subprogram
15385 (New_Subp => New_Subp,
15386 Parent_Subp => Subp,
15387 Derived_Type => Derived_Type,
15388 Parent_Type => Parent_Base,
15389 Actual_Subp => Act_Subp);
15390 end if;
15392 -- No need to update Act_Elm if we must search for the
15393 -- corresponding operation in the generic actual
15395 if not Need_Search
15396 and then Present (Act_Elmt)
15397 then
15398 Next_Elmt (Act_Elmt);
15399 Act_Subp := Node (Act_Elmt);
15400 end if;
15402 <<Continue>>
15403 Next_Elmt (Elmt);
15404 end loop;
15406 -- Inherit additional operations from progenitors. If the derived
15407 -- type is a generic actual, there are not new primitive operations
15408 -- for the type because it has those of the actual, and therefore
15409 -- nothing needs to be done. The renamings generated above are not
15410 -- primitive operations, and their purpose is simply to make the
15411 -- proper operations visible within an instantiation.
15413 if No (Generic_Actual) then
15414 Derive_Progenitor_Subprograms (Parent_Base, Derived_Type);
15415 end if;
15416 end if;
15418 -- Final check: Direct descendants must have their primitives in the
15419 -- same order. We exclude from this test untagged types and instances
15420 -- of formal derived types. We skip this test if we have already
15421 -- reported serious errors in the sources.
15423 pragma Assert (not Is_Tagged_Type (Derived_Type)
15424 or else Present (Generic_Actual)
15425 or else Serious_Errors_Detected > 0
15426 or else Check_Derived_Type);
15427 end Derive_Subprograms;
15429 --------------------------------
15430 -- Derived_Standard_Character --
15431 --------------------------------
15433 procedure Derived_Standard_Character
15434 (N : Node_Id;
15435 Parent_Type : Entity_Id;
15436 Derived_Type : Entity_Id)
15438 Loc : constant Source_Ptr := Sloc (N);
15439 Def : constant Node_Id := Type_Definition (N);
15440 Indic : constant Node_Id := Subtype_Indication (Def);
15441 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
15442 Implicit_Base : constant Entity_Id :=
15443 Create_Itype
15444 (E_Enumeration_Type, N, Derived_Type, 'B');
15446 Lo : Node_Id;
15447 Hi : Node_Id;
15449 begin
15450 Discard_Node (Process_Subtype (Indic, N));
15452 Set_Etype (Implicit_Base, Parent_Base);
15453 Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
15454 Set_RM_Size (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
15456 Set_Is_Character_Type (Implicit_Base, True);
15457 Set_Has_Delayed_Freeze (Implicit_Base);
15459 -- The bounds of the implicit base are the bounds of the parent base.
15460 -- Note that their type is the parent base.
15462 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
15463 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
15465 Set_Scalar_Range (Implicit_Base,
15466 Make_Range (Loc,
15467 Low_Bound => Lo,
15468 High_Bound => Hi));
15470 Conditional_Delay (Derived_Type, Parent_Type);
15472 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
15473 Set_Etype (Derived_Type, Implicit_Base);
15474 Set_Size_Info (Derived_Type, Parent_Type);
15476 if Unknown_RM_Size (Derived_Type) then
15477 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
15478 end if;
15480 Set_Is_Character_Type (Derived_Type, True);
15482 if Nkind (Indic) /= N_Subtype_Indication then
15484 -- If no explicit constraint, the bounds are those
15485 -- of the parent type.
15487 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Type));
15488 Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
15489 Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
15490 end if;
15492 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
15494 -- Because the implicit base is used in the conversion of the bounds, we
15495 -- have to freeze it now. This is similar to what is done for numeric
15496 -- types, and it equally suspicious, but otherwise a non-static bound
15497 -- will have a reference to an unfrozen type, which is rejected by Gigi
15498 -- (???). This requires specific care for definition of stream
15499 -- attributes. For details, see comments at the end of
15500 -- Build_Derived_Numeric_Type.
15502 Freeze_Before (N, Implicit_Base);
15503 end Derived_Standard_Character;
15505 ------------------------------
15506 -- Derived_Type_Declaration --
15507 ------------------------------
15509 procedure Derived_Type_Declaration
15510 (T : Entity_Id;
15511 N : Node_Id;
15512 Is_Completion : Boolean)
15514 Parent_Type : Entity_Id;
15516 function Comes_From_Generic (Typ : Entity_Id) return Boolean;
15517 -- Check whether the parent type is a generic formal, or derives
15518 -- directly or indirectly from one.
15520 ------------------------
15521 -- Comes_From_Generic --
15522 ------------------------
15524 function Comes_From_Generic (Typ : Entity_Id) return Boolean is
15525 begin
15526 if Is_Generic_Type (Typ) then
15527 return True;
15529 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
15530 return True;
15532 elsif Is_Private_Type (Typ)
15533 and then Present (Full_View (Typ))
15534 and then Is_Generic_Type (Root_Type (Full_View (Typ)))
15535 then
15536 return True;
15538 elsif Is_Generic_Actual_Type (Typ) then
15539 return True;
15541 else
15542 return False;
15543 end if;
15544 end Comes_From_Generic;
15546 -- Local variables
15548 Def : constant Node_Id := Type_Definition (N);
15549 Iface_Def : Node_Id;
15550 Indic : constant Node_Id := Subtype_Indication (Def);
15551 Extension : constant Node_Id := Record_Extension_Part (Def);
15552 Parent_Node : Node_Id;
15553 Taggd : Boolean;
15555 -- Start of processing for Derived_Type_Declaration
15557 begin
15558 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
15560 -- Ada 2005 (AI-251): In case of interface derivation check that the
15561 -- parent is also an interface.
15563 if Interface_Present (Def) then
15564 Check_SPARK_05_Restriction ("interface is not allowed", Def);
15566 if not Is_Interface (Parent_Type) then
15567 Diagnose_Interface (Indic, Parent_Type);
15569 else
15570 Parent_Node := Parent (Base_Type (Parent_Type));
15571 Iface_Def := Type_Definition (Parent_Node);
15573 -- Ada 2005 (AI-251): Limited interfaces can only inherit from
15574 -- other limited interfaces.
15576 if Limited_Present (Def) then
15577 if Limited_Present (Iface_Def) then
15578 null;
15580 elsif Protected_Present (Iface_Def) then
15581 Error_Msg_NE
15582 ("descendant of& must be declared"
15583 & " as a protected interface",
15584 N, Parent_Type);
15586 elsif Synchronized_Present (Iface_Def) then
15587 Error_Msg_NE
15588 ("descendant of& must be declared"
15589 & " as a synchronized interface",
15590 N, Parent_Type);
15592 elsif Task_Present (Iface_Def) then
15593 Error_Msg_NE
15594 ("descendant of& must be declared as a task interface",
15595 N, Parent_Type);
15597 else
15598 Error_Msg_N
15599 ("(Ada 2005) limited interface cannot "
15600 & "inherit from non-limited interface", Indic);
15601 end if;
15603 -- Ada 2005 (AI-345): Non-limited interfaces can only inherit
15604 -- from non-limited or limited interfaces.
15606 elsif not Protected_Present (Def)
15607 and then not Synchronized_Present (Def)
15608 and then not Task_Present (Def)
15609 then
15610 if Limited_Present (Iface_Def) then
15611 null;
15613 elsif Protected_Present (Iface_Def) then
15614 Error_Msg_NE
15615 ("descendant of& must be declared"
15616 & " as a protected interface",
15617 N, Parent_Type);
15619 elsif Synchronized_Present (Iface_Def) then
15620 Error_Msg_NE
15621 ("descendant of& must be declared"
15622 & " as a synchronized interface",
15623 N, Parent_Type);
15625 elsif Task_Present (Iface_Def) then
15626 Error_Msg_NE
15627 ("descendant of& must be declared as a task interface",
15628 N, Parent_Type);
15629 else
15630 null;
15631 end if;
15632 end if;
15633 end if;
15634 end if;
15636 if Is_Tagged_Type (Parent_Type)
15637 and then Is_Concurrent_Type (Parent_Type)
15638 and then not Is_Interface (Parent_Type)
15639 then
15640 Error_Msg_N
15641 ("parent type of a record extension cannot be "
15642 & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
15643 Set_Etype (T, Any_Type);
15644 return;
15645 end if;
15647 -- Ada 2005 (AI-251): Decorate all the names in the list of ancestor
15648 -- interfaces
15650 if Is_Tagged_Type (Parent_Type)
15651 and then Is_Non_Empty_List (Interface_List (Def))
15652 then
15653 declare
15654 Intf : Node_Id;
15655 T : Entity_Id;
15657 begin
15658 Intf := First (Interface_List (Def));
15659 while Present (Intf) loop
15660 T := Find_Type_Of_Subtype_Indic (Intf);
15662 if not Is_Interface (T) then
15663 Diagnose_Interface (Intf, T);
15665 -- Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
15666 -- a limited type from having a nonlimited progenitor.
15668 elsif (Limited_Present (Def)
15669 or else (not Is_Interface (Parent_Type)
15670 and then Is_Limited_Type (Parent_Type)))
15671 and then not Is_Limited_Interface (T)
15672 then
15673 Error_Msg_NE
15674 ("progenitor interface& of limited type must be limited",
15675 N, T);
15676 end if;
15678 Next (Intf);
15679 end loop;
15680 end;
15681 end if;
15683 if Parent_Type = Any_Type
15684 or else Etype (Parent_Type) = Any_Type
15685 or else (Is_Class_Wide_Type (Parent_Type)
15686 and then Etype (Parent_Type) = T)
15687 then
15688 -- If Parent_Type is undefined or illegal, make new type into a
15689 -- subtype of Any_Type, and set a few attributes to prevent cascaded
15690 -- errors. If this is a self-definition, emit error now.
15692 if T = Parent_Type or else T = Etype (Parent_Type) then
15693 Error_Msg_N ("type cannot be used in its own definition", Indic);
15694 end if;
15696 Set_Ekind (T, Ekind (Parent_Type));
15697 Set_Etype (T, Any_Type);
15698 Set_Scalar_Range (T, Scalar_Range (Any_Type));
15700 if Is_Tagged_Type (T)
15701 and then Is_Record_Type (T)
15702 then
15703 Set_Direct_Primitive_Operations (T, New_Elmt_List);
15704 end if;
15706 return;
15707 end if;
15709 -- Ada 2005 (AI-251): The case in which the parent of the full-view is
15710 -- an interface is special because the list of interfaces in the full
15711 -- view can be given in any order. For example:
15713 -- type A is interface;
15714 -- type B is interface and A;
15715 -- type D is new B with private;
15716 -- private
15717 -- type D is new A and B with null record; -- 1 --
15719 -- In this case we perform the following transformation of -1-:
15721 -- type D is new B and A with null record;
15723 -- If the parent of the full-view covers the parent of the partial-view
15724 -- we have two possible cases:
15726 -- 1) They have the same parent
15727 -- 2) The parent of the full-view implements some further interfaces
15729 -- In both cases we do not need to perform the transformation. In the
15730 -- first case the source program is correct and the transformation is
15731 -- not needed; in the second case the source program does not fulfill
15732 -- the no-hidden interfaces rule (AI-396) and the error will be reported
15733 -- later.
15735 -- This transformation not only simplifies the rest of the analysis of
15736 -- this type declaration but also simplifies the correct generation of
15737 -- the object layout to the expander.
15739 if In_Private_Part (Current_Scope)
15740 and then Is_Interface (Parent_Type)
15741 then
15742 declare
15743 Iface : Node_Id;
15744 Partial_View : Entity_Id;
15745 Partial_View_Parent : Entity_Id;
15746 New_Iface : Node_Id;
15748 begin
15749 -- Look for the associated private type declaration
15751 Partial_View := First_Entity (Current_Scope);
15752 loop
15753 exit when No (Partial_View)
15754 or else (Has_Private_Declaration (Partial_View)
15755 and then Full_View (Partial_View) = T);
15757 Next_Entity (Partial_View);
15758 end loop;
15760 -- If the partial view was not found then the source code has
15761 -- errors and the transformation is not needed.
15763 if Present (Partial_View) then
15764 Partial_View_Parent := Etype (Partial_View);
15766 -- If the parent of the full-view covers the parent of the
15767 -- partial-view we have nothing else to do.
15769 if Interface_Present_In_Ancestor
15770 (Parent_Type, Partial_View_Parent)
15771 then
15772 null;
15774 -- Traverse the list of interfaces of the full-view to look
15775 -- for the parent of the partial-view and perform the tree
15776 -- transformation.
15778 else
15779 Iface := First (Interface_List (Def));
15780 while Present (Iface) loop
15781 if Etype (Iface) = Etype (Partial_View) then
15782 Rewrite (Subtype_Indication (Def),
15783 New_Copy (Subtype_Indication
15784 (Parent (Partial_View))));
15786 New_Iface :=
15787 Make_Identifier (Sloc (N), Chars (Parent_Type));
15788 Append (New_Iface, Interface_List (Def));
15790 -- Analyze the transformed code
15792 Derived_Type_Declaration (T, N, Is_Completion);
15793 return;
15794 end if;
15796 Next (Iface);
15797 end loop;
15798 end if;
15799 end if;
15800 end;
15801 end if;
15803 -- Only composite types other than array types are allowed to have
15804 -- discriminants.
15806 if Present (Discriminant_Specifications (N)) then
15807 if (Is_Elementary_Type (Parent_Type)
15808 or else
15809 Is_Array_Type (Parent_Type))
15810 and then not Error_Posted (N)
15811 then
15812 Error_Msg_N
15813 ("elementary or array type cannot have discriminants",
15814 Defining_Identifier (First (Discriminant_Specifications (N))));
15815 Set_Has_Discriminants (T, False);
15817 -- The type is allowed to have discriminants
15819 else
15820 Check_SPARK_05_Restriction ("discriminant type is not allowed", N);
15821 end if;
15822 end if;
15824 -- In Ada 83, a derived type defined in a package specification cannot
15825 -- be used for further derivation until the end of its visible part.
15826 -- Note that derivation in the private part of the package is allowed.
15828 if Ada_Version = Ada_83
15829 and then Is_Derived_Type (Parent_Type)
15830 and then In_Visible_Part (Scope (Parent_Type))
15831 then
15832 if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
15833 Error_Msg_N
15834 ("(Ada 83): premature use of type for derivation", Indic);
15835 end if;
15836 end if;
15838 -- Check for early use of incomplete or private type
15840 if Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
15841 Error_Msg_N ("premature derivation of incomplete type", Indic);
15842 return;
15844 elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
15845 and then not Comes_From_Generic (Parent_Type))
15846 or else Has_Private_Component (Parent_Type)
15847 then
15848 -- The ancestor type of a formal type can be incomplete, in which
15849 -- case only the operations of the partial view are available in the
15850 -- generic. Subsequent checks may be required when the full view is
15851 -- analyzed to verify that a derivation from a tagged type has an
15852 -- extension.
15854 if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
15855 null;
15857 elsif No (Underlying_Type (Parent_Type))
15858 or else Has_Private_Component (Parent_Type)
15859 then
15860 Error_Msg_N
15861 ("premature derivation of derived or private type", Indic);
15863 -- Flag the type itself as being in error, this prevents some
15864 -- nasty problems with subsequent uses of the malformed type.
15866 Set_Error_Posted (T);
15868 -- Check that within the immediate scope of an untagged partial
15869 -- view it's illegal to derive from the partial view if the
15870 -- full view is tagged. (7.3(7))
15872 -- We verify that the Parent_Type is a partial view by checking
15873 -- that it is not a Full_Type_Declaration (i.e. a private type or
15874 -- private extension declaration), to distinguish a partial view
15875 -- from a derivation from a private type which also appears as
15876 -- E_Private_Type. If the parent base type is not declared in an
15877 -- enclosing scope there is no need to check.
15879 elsif Present (Full_View (Parent_Type))
15880 and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
15881 and then not Is_Tagged_Type (Parent_Type)
15882 and then Is_Tagged_Type (Full_View (Parent_Type))
15883 and then In_Open_Scopes (Scope (Base_Type (Parent_Type)))
15884 then
15885 Error_Msg_N
15886 ("premature derivation from type with tagged full view",
15887 Indic);
15888 end if;
15889 end if;
15891 -- Check that form of derivation is appropriate
15893 Taggd := Is_Tagged_Type (Parent_Type);
15895 -- Set the parent type to the class-wide type's specific type in this
15896 -- case to prevent cascading errors
15898 if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
15899 Error_Msg_N ("parent type must not be a class-wide type", Indic);
15900 Set_Etype (T, Etype (Parent_Type));
15901 return;
15902 end if;
15904 if Present (Extension) and then not Taggd then
15905 Error_Msg_N
15906 ("type derived from untagged type cannot have extension", Indic);
15908 elsif No (Extension) and then Taggd then
15910 -- If this declaration is within a private part (or body) of a
15911 -- generic instantiation then the derivation is allowed (the parent
15912 -- type can only appear tagged in this case if it's a generic actual
15913 -- type, since it would otherwise have been rejected in the analysis
15914 -- of the generic template).
15916 if not Is_Generic_Actual_Type (Parent_Type)
15917 or else In_Visible_Part (Scope (Parent_Type))
15918 then
15919 if Is_Class_Wide_Type (Parent_Type) then
15920 Error_Msg_N
15921 ("parent type must not be a class-wide type", Indic);
15923 -- Use specific type to prevent cascaded errors.
15925 Parent_Type := Etype (Parent_Type);
15927 else
15928 Error_Msg_N
15929 ("type derived from tagged type must have extension", Indic);
15930 end if;
15931 end if;
15932 end if;
15934 -- AI-443: Synchronized formal derived types require a private
15935 -- extension. There is no point in checking the ancestor type or
15936 -- the progenitors since the construct is wrong to begin with.
15938 if Ada_Version >= Ada_2005
15939 and then Is_Generic_Type (T)
15940 and then Present (Original_Node (N))
15941 then
15942 declare
15943 Decl : constant Node_Id := Original_Node (N);
15945 begin
15946 if Nkind (Decl) = N_Formal_Type_Declaration
15947 and then Nkind (Formal_Type_Definition (Decl)) =
15948 N_Formal_Derived_Type_Definition
15949 and then Synchronized_Present (Formal_Type_Definition (Decl))
15950 and then No (Extension)
15952 -- Avoid emitting a duplicate error message
15954 and then not Error_Posted (Indic)
15955 then
15956 Error_Msg_N
15957 ("synchronized derived type must have extension", N);
15958 end if;
15959 end;
15960 end if;
15962 if Null_Exclusion_Present (Def)
15963 and then not Is_Access_Type (Parent_Type)
15964 then
15965 Error_Msg_N ("null exclusion can only apply to an access type", N);
15966 end if;
15968 -- Avoid deriving parent primitives of underlying record views
15970 Build_Derived_Type (N, Parent_Type, T, Is_Completion,
15971 Derive_Subps => not Is_Underlying_Record_View (T));
15973 -- AI-419: The parent type of an explicitly limited derived type must
15974 -- be a limited type or a limited interface.
15976 if Limited_Present (Def) then
15977 Set_Is_Limited_Record (T);
15979 if Is_Interface (T) then
15980 Set_Is_Limited_Interface (T);
15981 end if;
15983 if not Is_Limited_Type (Parent_Type)
15984 and then
15985 (not Is_Interface (Parent_Type)
15986 or else not Is_Limited_Interface (Parent_Type))
15987 then
15988 -- AI05-0096: a derivation in the private part of an instance is
15989 -- legal if the generic formal is untagged limited, and the actual
15990 -- is non-limited.
15992 if Is_Generic_Actual_Type (Parent_Type)
15993 and then In_Private_Part (Current_Scope)
15994 and then
15995 not Is_Tagged_Type
15996 (Generic_Parent_Type (Parent (Parent_Type)))
15997 then
15998 null;
16000 else
16001 Error_Msg_NE
16002 ("parent type& of limited type must be limited",
16003 N, Parent_Type);
16004 end if;
16005 end if;
16006 end if;
16008 -- In SPARK, there are no derived type definitions other than type
16009 -- extensions of tagged record types.
16011 if No (Extension) then
16012 Check_SPARK_05_Restriction
16013 ("derived type is not allowed", Original_Node (N));
16014 end if;
16015 end Derived_Type_Declaration;
16017 ------------------------
16018 -- Diagnose_Interface --
16019 ------------------------
16021 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id) is
16022 begin
16023 if not Is_Interface (E) and then E /= Any_Type then
16024 Error_Msg_NE ("(Ada 2005) & must be an interface", N, E);
16025 end if;
16026 end Diagnose_Interface;
16028 ----------------------------------
16029 -- Enumeration_Type_Declaration --
16030 ----------------------------------
16032 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
16033 Ev : Uint;
16034 L : Node_Id;
16035 R_Node : Node_Id;
16036 B_Node : Node_Id;
16038 begin
16039 -- Create identifier node representing lower bound
16041 B_Node := New_Node (N_Identifier, Sloc (Def));
16042 L := First (Literals (Def));
16043 Set_Chars (B_Node, Chars (L));
16044 Set_Entity (B_Node, L);
16045 Set_Etype (B_Node, T);
16046 Set_Is_Static_Expression (B_Node, True);
16048 R_Node := New_Node (N_Range, Sloc (Def));
16049 Set_Low_Bound (R_Node, B_Node);
16051 Set_Ekind (T, E_Enumeration_Type);
16052 Set_First_Literal (T, L);
16053 Set_Etype (T, T);
16054 Set_Is_Constrained (T);
16056 Ev := Uint_0;
16058 -- Loop through literals of enumeration type setting pos and rep values
16059 -- except that if the Ekind is already set, then it means the literal
16060 -- was already constructed (case of a derived type declaration and we
16061 -- should not disturb the Pos and Rep values.
16063 while Present (L) loop
16064 if Ekind (L) /= E_Enumeration_Literal then
16065 Set_Ekind (L, E_Enumeration_Literal);
16066 Set_Enumeration_Pos (L, Ev);
16067 Set_Enumeration_Rep (L, Ev);
16068 Set_Is_Known_Valid (L, True);
16069 end if;
16071 Set_Etype (L, T);
16072 New_Overloaded_Entity (L);
16073 Generate_Definition (L);
16074 Set_Convention (L, Convention_Intrinsic);
16076 -- Case of character literal
16078 if Nkind (L) = N_Defining_Character_Literal then
16079 Set_Is_Character_Type (T, True);
16081 -- Check violation of No_Wide_Characters
16083 if Restriction_Check_Required (No_Wide_Characters) then
16084 Get_Name_String (Chars (L));
16086 if Name_Len >= 3 and then Name_Buffer (1 .. 2) = "QW" then
16087 Check_Restriction (No_Wide_Characters, L);
16088 end if;
16089 end if;
16090 end if;
16092 Ev := Ev + 1;
16093 Next (L);
16094 end loop;
16096 -- Now create a node representing upper bound
16098 B_Node := New_Node (N_Identifier, Sloc (Def));
16099 Set_Chars (B_Node, Chars (Last (Literals (Def))));
16100 Set_Entity (B_Node, Last (Literals (Def)));
16101 Set_Etype (B_Node, T);
16102 Set_Is_Static_Expression (B_Node, True);
16104 Set_High_Bound (R_Node, B_Node);
16106 -- Initialize various fields of the type. Some of this information
16107 -- may be overwritten later through rep.clauses.
16109 Set_Scalar_Range (T, R_Node);
16110 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
16111 Set_Enum_Esize (T);
16112 Set_Enum_Pos_To_Rep (T, Empty);
16114 -- Set Discard_Names if configuration pragma set, or if there is
16115 -- a parameterless pragma in the current declarative region
16117 if Global_Discard_Names or else Discard_Names (Scope (T)) then
16118 Set_Discard_Names (T);
16119 end if;
16121 -- Process end label if there is one
16123 if Present (Def) then
16124 Process_End_Label (Def, 'e', T);
16125 end if;
16126 end Enumeration_Type_Declaration;
16128 ---------------------------------
16129 -- Expand_To_Stored_Constraint --
16130 ---------------------------------
16132 function Expand_To_Stored_Constraint
16133 (Typ : Entity_Id;
16134 Constraint : Elist_Id) return Elist_Id
16136 Explicitly_Discriminated_Type : Entity_Id;
16137 Expansion : Elist_Id;
16138 Discriminant : Entity_Id;
16140 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
16141 -- Find the nearest type that actually specifies discriminants
16143 ---------------------------------
16144 -- Type_With_Explicit_Discrims --
16145 ---------------------------------
16147 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
16148 Typ : constant E := Base_Type (Id);
16150 begin
16151 if Ekind (Typ) in Incomplete_Or_Private_Kind then
16152 if Present (Full_View (Typ)) then
16153 return Type_With_Explicit_Discrims (Full_View (Typ));
16154 end if;
16156 else
16157 if Has_Discriminants (Typ) then
16158 return Typ;
16159 end if;
16160 end if;
16162 if Etype (Typ) = Typ then
16163 return Empty;
16164 elsif Has_Discriminants (Typ) then
16165 return Typ;
16166 else
16167 return Type_With_Explicit_Discrims (Etype (Typ));
16168 end if;
16170 end Type_With_Explicit_Discrims;
16172 -- Start of processing for Expand_To_Stored_Constraint
16174 begin
16175 if No (Constraint) or else Is_Empty_Elmt_List (Constraint) then
16176 return No_Elist;
16177 end if;
16179 Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
16181 if No (Explicitly_Discriminated_Type) then
16182 return No_Elist;
16183 end if;
16185 Expansion := New_Elmt_List;
16187 Discriminant :=
16188 First_Stored_Discriminant (Explicitly_Discriminated_Type);
16189 while Present (Discriminant) loop
16190 Append_Elmt
16191 (Get_Discriminant_Value
16192 (Discriminant, Explicitly_Discriminated_Type, Constraint),
16193 To => Expansion);
16194 Next_Stored_Discriminant (Discriminant);
16195 end loop;
16197 return Expansion;
16198 end Expand_To_Stored_Constraint;
16200 ---------------------------
16201 -- Find_Hidden_Interface --
16202 ---------------------------
16204 function Find_Hidden_Interface
16205 (Src : Elist_Id;
16206 Dest : Elist_Id) return Entity_Id
16208 Iface : Entity_Id;
16209 Iface_Elmt : Elmt_Id;
16211 begin
16212 if Present (Src) and then Present (Dest) then
16213 Iface_Elmt := First_Elmt (Src);
16214 while Present (Iface_Elmt) loop
16215 Iface := Node (Iface_Elmt);
16217 if Is_Interface (Iface)
16218 and then not Contain_Interface (Iface, Dest)
16219 then
16220 return Iface;
16221 end if;
16223 Next_Elmt (Iface_Elmt);
16224 end loop;
16225 end if;
16227 return Empty;
16228 end Find_Hidden_Interface;
16230 --------------------
16231 -- Find_Type_Name --
16232 --------------------
16234 function Find_Type_Name (N : Node_Id) return Entity_Id is
16235 Id : constant Entity_Id := Defining_Identifier (N);
16236 Prev : Entity_Id;
16237 New_Id : Entity_Id;
16238 Prev_Par : Node_Id;
16240 procedure Check_Duplicate_Aspects;
16241 -- Check that aspects specified in a completion have not been specified
16242 -- already in the partial view. Type_Invariant and others can be
16243 -- specified on either view but never on both.
16245 procedure Tag_Mismatch;
16246 -- Diagnose a tagged partial view whose full view is untagged.
16247 -- We post the message on the full view, with a reference to
16248 -- the previous partial view. The partial view can be private
16249 -- or incomplete, and these are handled in a different manner,
16250 -- so we determine the position of the error message from the
16251 -- respective slocs of both.
16253 -----------------------------
16254 -- Check_Duplicate_Aspects --
16255 -----------------------------
16256 procedure Check_Duplicate_Aspects is
16257 Prev_Aspects : constant List_Id := Aspect_Specifications (Prev_Par);
16258 Full_Aspects : constant List_Id := Aspect_Specifications (N);
16259 F_Spec, P_Spec : Node_Id;
16261 begin
16262 if Present (Prev_Aspects) and then Present (Full_Aspects) then
16263 F_Spec := First (Full_Aspects);
16264 while Present (F_Spec) loop
16265 P_Spec := First (Prev_Aspects);
16266 while Present (P_Spec) loop
16267 if Chars (Identifier (P_Spec)) = Chars (Identifier (F_Spec))
16268 then
16269 Error_Msg_N
16270 ("aspect already specified in private declaration",
16271 F_Spec);
16272 Remove (F_Spec);
16273 return;
16274 end if;
16276 Next (P_Spec);
16277 end loop;
16279 Next (F_Spec);
16280 end loop;
16281 end if;
16282 end Check_Duplicate_Aspects;
16284 ------------------
16285 -- Tag_Mismatch --
16286 ------------------
16288 procedure Tag_Mismatch is
16289 begin
16290 if Sloc (Prev) < Sloc (Id) then
16291 if Ada_Version >= Ada_2012
16292 and then Nkind (N) = N_Private_Type_Declaration
16293 then
16294 Error_Msg_NE
16295 ("declaration of private } must be a tagged type ", Id, Prev);
16296 else
16297 Error_Msg_NE
16298 ("full declaration of } must be a tagged type ", Id, Prev);
16299 end if;
16301 else
16302 if Ada_Version >= Ada_2012
16303 and then Nkind (N) = N_Private_Type_Declaration
16304 then
16305 Error_Msg_NE
16306 ("declaration of private } must be a tagged type ", Prev, Id);
16307 else
16308 Error_Msg_NE
16309 ("full declaration of } must be a tagged type ", Prev, Id);
16310 end if;
16311 end if;
16312 end Tag_Mismatch;
16314 -- Start of processing for Find_Type_Name
16316 begin
16317 -- Find incomplete declaration, if one was given
16319 Prev := Current_Entity_In_Scope (Id);
16321 -- New type declaration
16323 if No (Prev) then
16324 Enter_Name (Id);
16325 return Id;
16327 -- Previous declaration exists
16329 else
16330 Prev_Par := Parent (Prev);
16332 -- Error if not incomplete/private case except if previous
16333 -- declaration is implicit, etc. Enter_Name will emit error if
16334 -- appropriate.
16336 if not Is_Incomplete_Or_Private_Type (Prev) then
16337 Enter_Name (Id);
16338 New_Id := Id;
16340 -- Check invalid completion of private or incomplete type
16342 elsif not Nkind_In (N, N_Full_Type_Declaration,
16343 N_Task_Type_Declaration,
16344 N_Protected_Type_Declaration)
16345 and then
16346 (Ada_Version < Ada_2012
16347 or else not Is_Incomplete_Type (Prev)
16348 or else not Nkind_In (N, N_Private_Type_Declaration,
16349 N_Private_Extension_Declaration))
16350 then
16351 -- Completion must be a full type declarations (RM 7.3(4))
16353 Error_Msg_Sloc := Sloc (Prev);
16354 Error_Msg_NE ("invalid completion of }", Id, Prev);
16356 -- Set scope of Id to avoid cascaded errors. Entity is never
16357 -- examined again, except when saving globals in generics.
16359 Set_Scope (Id, Current_Scope);
16360 New_Id := Id;
16362 -- If this is a repeated incomplete declaration, no further
16363 -- checks are possible.
16365 if Nkind (N) = N_Incomplete_Type_Declaration then
16366 return Prev;
16367 end if;
16369 -- Case of full declaration of incomplete type
16371 elsif Ekind (Prev) = E_Incomplete_Type
16372 and then (Ada_Version < Ada_2012
16373 or else No (Full_View (Prev))
16374 or else not Is_Private_Type (Full_View (Prev)))
16375 then
16376 -- Indicate that the incomplete declaration has a matching full
16377 -- declaration. The defining occurrence of the incomplete
16378 -- declaration remains the visible one, and the procedure
16379 -- Get_Full_View dereferences it whenever the type is used.
16381 if Present (Full_View (Prev)) then
16382 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
16383 end if;
16385 Set_Full_View (Prev, Id);
16386 Append_Entity (Id, Current_Scope);
16387 Set_Is_Public (Id, Is_Public (Prev));
16388 Set_Is_Internal (Id);
16389 New_Id := Prev;
16391 -- If the incomplete view is tagged, a class_wide type has been
16392 -- created already. Use it for the private type as well, in order
16393 -- to prevent multiple incompatible class-wide types that may be
16394 -- created for self-referential anonymous access components.
16396 if Is_Tagged_Type (Prev)
16397 and then Present (Class_Wide_Type (Prev))
16398 then
16399 Set_Ekind (Id, Ekind (Prev)); -- will be reset later
16400 Set_Class_Wide_Type (Id, Class_Wide_Type (Prev));
16402 -- The type of the classwide type is the current Id. Previously
16403 -- this was not done for private declarations because of order-
16404 -- of elaboration issues in the back-end, but gigi now handles
16405 -- this properly.
16407 Set_Etype (Class_Wide_Type (Id), Id);
16408 end if;
16410 -- Case of full declaration of private type
16412 else
16413 -- If the private type was a completion of an incomplete type then
16414 -- update Prev to reference the private type
16416 if Ada_Version >= Ada_2012
16417 and then Ekind (Prev) = E_Incomplete_Type
16418 and then Present (Full_View (Prev))
16419 and then Is_Private_Type (Full_View (Prev))
16420 then
16421 Prev := Full_View (Prev);
16422 Prev_Par := Parent (Prev);
16423 end if;
16425 if Nkind (N) = N_Full_Type_Declaration
16426 and then Nkind_In
16427 (Type_Definition (N), N_Record_Definition,
16428 N_Derived_Type_Definition)
16429 and then Interface_Present (Type_Definition (N))
16430 then
16431 Error_Msg_N
16432 ("completion of private type cannot be an interface", N);
16433 end if;
16435 if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
16436 if Etype (Prev) /= Prev then
16438 -- Prev is a private subtype or a derived type, and needs
16439 -- no completion.
16441 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
16442 New_Id := Id;
16444 elsif Ekind (Prev) = E_Private_Type
16445 and then Nkind_In (N, N_Task_Type_Declaration,
16446 N_Protected_Type_Declaration)
16447 then
16448 Error_Msg_N
16449 ("completion of nonlimited type cannot be limited", N);
16451 elsif Ekind (Prev) = E_Record_Type_With_Private
16452 and then Nkind_In (N, N_Task_Type_Declaration,
16453 N_Protected_Type_Declaration)
16454 then
16455 if not Is_Limited_Record (Prev) then
16456 Error_Msg_N
16457 ("completion of nonlimited type cannot be limited", N);
16459 elsif No (Interface_List (N)) then
16460 Error_Msg_N
16461 ("completion of tagged private type must be tagged",
16463 end if;
16464 end if;
16466 -- Ada 2005 (AI-251): Private extension declaration of a task
16467 -- type or a protected type. This case arises when covering
16468 -- interface types.
16470 elsif Nkind_In (N, N_Task_Type_Declaration,
16471 N_Protected_Type_Declaration)
16472 then
16473 null;
16475 elsif Nkind (N) /= N_Full_Type_Declaration
16476 or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
16477 then
16478 Error_Msg_N
16479 ("full view of private extension must be an extension", N);
16481 elsif not (Abstract_Present (Parent (Prev)))
16482 and then Abstract_Present (Type_Definition (N))
16483 then
16484 Error_Msg_N
16485 ("full view of non-abstract extension cannot be abstract", N);
16486 end if;
16488 if not In_Private_Part (Current_Scope) then
16489 Error_Msg_N
16490 ("declaration of full view must appear in private part", N);
16491 end if;
16493 if Ada_Version >= Ada_2012 then
16494 Check_Duplicate_Aspects;
16495 end if;
16497 Copy_And_Swap (Prev, Id);
16498 Set_Has_Private_Declaration (Prev);
16499 Set_Has_Private_Declaration (Id);
16501 -- AI12-0133: Indicate whether we have a partial view with
16502 -- unknown discriminants, in which case initialization of objects
16503 -- of the type do not receive an invariant check.
16505 Set_Partial_View_Has_Unknown_Discr
16506 (Prev, Has_Unknown_Discriminants (Id));
16508 -- Preserve aspect and iterator flags that may have been set on
16509 -- the partial view.
16511 Set_Has_Delayed_Aspects (Prev, Has_Delayed_Aspects (Id));
16512 Set_Has_Implicit_Dereference (Prev, Has_Implicit_Dereference (Id));
16514 -- If no error, propagate freeze_node from private to full view.
16515 -- It may have been generated for an early operational item.
16517 if Present (Freeze_Node (Id))
16518 and then Serious_Errors_Detected = 0
16519 and then No (Full_View (Id))
16520 then
16521 Set_Freeze_Node (Prev, Freeze_Node (Id));
16522 Set_Freeze_Node (Id, Empty);
16523 Set_First_Rep_Item (Prev, First_Rep_Item (Id));
16524 end if;
16526 Set_Full_View (Id, Prev);
16527 New_Id := Prev;
16528 end if;
16530 -- Verify that full declaration conforms to partial one
16532 if Is_Incomplete_Or_Private_Type (Prev)
16533 and then Present (Discriminant_Specifications (Prev_Par))
16534 then
16535 if Present (Discriminant_Specifications (N)) then
16536 if Ekind (Prev) = E_Incomplete_Type then
16537 Check_Discriminant_Conformance (N, Prev, Prev);
16538 else
16539 Check_Discriminant_Conformance (N, Prev, Id);
16540 end if;
16542 else
16543 Error_Msg_N
16544 ("missing discriminants in full type declaration", N);
16546 -- To avoid cascaded errors on subsequent use, share the
16547 -- discriminants of the partial view.
16549 Set_Discriminant_Specifications (N,
16550 Discriminant_Specifications (Prev_Par));
16551 end if;
16552 end if;
16554 -- A prior untagged partial view can have an associated class-wide
16555 -- type due to use of the class attribute, and in this case the full
16556 -- type must also be tagged. This Ada 95 usage is deprecated in favor
16557 -- of incomplete tagged declarations, but we check for it.
16559 if Is_Type (Prev)
16560 and then (Is_Tagged_Type (Prev)
16561 or else Present (Class_Wide_Type (Prev)))
16562 then
16563 -- Ada 2012 (AI05-0162): A private type may be the completion of
16564 -- an incomplete type.
16566 if Ada_Version >= Ada_2012
16567 and then Is_Incomplete_Type (Prev)
16568 and then Nkind_In (N, N_Private_Type_Declaration,
16569 N_Private_Extension_Declaration)
16570 then
16571 -- No need to check private extensions since they are tagged
16573 if Nkind (N) = N_Private_Type_Declaration
16574 and then not Tagged_Present (N)
16575 then
16576 Tag_Mismatch;
16577 end if;
16579 -- The full declaration is either a tagged type (including
16580 -- a synchronized type that implements interfaces) or a
16581 -- type extension, otherwise this is an error.
16583 elsif Nkind_In (N, N_Task_Type_Declaration,
16584 N_Protected_Type_Declaration)
16585 then
16586 if No (Interface_List (N)) and then not Error_Posted (N) then
16587 Tag_Mismatch;
16588 end if;
16590 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
16592 -- Indicate that the previous declaration (tagged incomplete
16593 -- or private declaration) requires the same on the full one.
16595 if not Tagged_Present (Type_Definition (N)) then
16596 Tag_Mismatch;
16597 Set_Is_Tagged_Type (Id);
16598 end if;
16600 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
16601 if No (Record_Extension_Part (Type_Definition (N))) then
16602 Error_Msg_NE
16603 ("full declaration of } must be a record extension",
16604 Prev, Id);
16606 -- Set some attributes to produce a usable full view
16608 Set_Is_Tagged_Type (Id);
16609 end if;
16611 else
16612 Tag_Mismatch;
16613 end if;
16614 end if;
16616 if Present (Prev)
16617 and then Nkind (Parent (Prev)) = N_Incomplete_Type_Declaration
16618 and then Present (Premature_Use (Parent (Prev)))
16619 then
16620 Error_Msg_Sloc := Sloc (N);
16621 Error_Msg_N
16622 ("\full declaration #", Premature_Use (Parent (Prev)));
16623 end if;
16625 return New_Id;
16626 end if;
16627 end Find_Type_Name;
16629 -------------------------
16630 -- Find_Type_Of_Object --
16631 -------------------------
16633 function Find_Type_Of_Object
16634 (Obj_Def : Node_Id;
16635 Related_Nod : Node_Id) return Entity_Id
16637 Def_Kind : constant Node_Kind := Nkind (Obj_Def);
16638 P : Node_Id := Parent (Obj_Def);
16639 T : Entity_Id;
16640 Nam : Name_Id;
16642 begin
16643 -- If the parent is a component_definition node we climb to the
16644 -- component_declaration node
16646 if Nkind (P) = N_Component_Definition then
16647 P := Parent (P);
16648 end if;
16650 -- Case of an anonymous array subtype
16652 if Nkind_In (Def_Kind, N_Constrained_Array_Definition,
16653 N_Unconstrained_Array_Definition)
16654 then
16655 T := Empty;
16656 Array_Type_Declaration (T, Obj_Def);
16658 -- Create an explicit subtype whenever possible
16660 elsif Nkind (P) /= N_Component_Declaration
16661 and then Def_Kind = N_Subtype_Indication
16662 then
16663 -- Base name of subtype on object name, which will be unique in
16664 -- the current scope.
16666 -- If this is a duplicate declaration, return base type, to avoid
16667 -- generating duplicate anonymous types.
16669 if Error_Posted (P) then
16670 Analyze (Subtype_Mark (Obj_Def));
16671 return Entity (Subtype_Mark (Obj_Def));
16672 end if;
16674 Nam :=
16675 New_External_Name
16676 (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
16678 T := Make_Defining_Identifier (Sloc (P), Nam);
16680 Insert_Action (Obj_Def,
16681 Make_Subtype_Declaration (Sloc (P),
16682 Defining_Identifier => T,
16683 Subtype_Indication => Relocate_Node (Obj_Def)));
16685 -- This subtype may need freezing, and this will not be done
16686 -- automatically if the object declaration is not in declarative
16687 -- part. Since this is an object declaration, the type cannot always
16688 -- be frozen here. Deferred constants do not freeze their type
16689 -- (which often enough will be private).
16691 if Nkind (P) = N_Object_Declaration
16692 and then Constant_Present (P)
16693 and then No (Expression (P))
16694 then
16695 null;
16697 -- Here we freeze the base type of object type to catch premature use
16698 -- of discriminated private type without a full view.
16700 else
16701 Insert_Actions (Obj_Def, Freeze_Entity (Base_Type (T), P));
16702 end if;
16704 -- Ada 2005 AI-406: the object definition in an object declaration
16705 -- can be an access definition.
16707 elsif Def_Kind = N_Access_Definition then
16708 T := Access_Definition (Related_Nod, Obj_Def);
16710 Set_Is_Local_Anonymous_Access
16712 V => (Ada_Version < Ada_2012)
16713 or else (Nkind (P) /= N_Object_Declaration)
16714 or else Is_Library_Level_Entity (Defining_Identifier (P)));
16716 -- Otherwise, the object definition is just a subtype_mark
16718 else
16719 T := Process_Subtype (Obj_Def, Related_Nod);
16721 -- If expansion is disabled an object definition that is an aggregate
16722 -- will not get expanded and may lead to scoping problems in the back
16723 -- end, if the object is referenced in an inner scope. In that case
16724 -- create an itype reference for the object definition now. This
16725 -- may be redundant in some cases, but harmless.
16727 if Is_Itype (T)
16728 and then Nkind (Related_Nod) = N_Object_Declaration
16729 and then ASIS_Mode
16730 then
16731 Build_Itype_Reference (T, Related_Nod);
16732 end if;
16733 end if;
16735 return T;
16736 end Find_Type_Of_Object;
16738 --------------------------------
16739 -- Find_Type_Of_Subtype_Indic --
16740 --------------------------------
16742 function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
16743 Typ : Entity_Id;
16745 begin
16746 -- Case of subtype mark with a constraint
16748 if Nkind (S) = N_Subtype_Indication then
16749 Find_Type (Subtype_Mark (S));
16750 Typ := Entity (Subtype_Mark (S));
16752 if not
16753 Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
16754 then
16755 Error_Msg_N
16756 ("incorrect constraint for this kind of type", Constraint (S));
16757 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
16758 end if;
16760 -- Otherwise we have a subtype mark without a constraint
16762 elsif Error_Posted (S) then
16763 Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
16764 return Any_Type;
16766 else
16767 Find_Type (S);
16768 Typ := Entity (S);
16769 end if;
16771 -- Check No_Wide_Characters restriction
16773 Check_Wide_Character_Restriction (Typ, S);
16775 return Typ;
16776 end Find_Type_Of_Subtype_Indic;
16778 -------------------------------------
16779 -- Floating_Point_Type_Declaration --
16780 -------------------------------------
16782 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
16783 Digs : constant Node_Id := Digits_Expression (Def);
16784 Max_Digs_Val : constant Uint := Digits_Value (Standard_Long_Long_Float);
16785 Digs_Val : Uint;
16786 Base_Typ : Entity_Id;
16787 Implicit_Base : Entity_Id;
16788 Bound : Node_Id;
16790 function Can_Derive_From (E : Entity_Id) return Boolean;
16791 -- Find if given digits value, and possibly a specified range, allows
16792 -- derivation from specified type
16794 function Find_Base_Type return Entity_Id;
16795 -- Find a predefined base type that Def can derive from, or generate
16796 -- an error and substitute Long_Long_Float if none exists.
16798 ---------------------
16799 -- Can_Derive_From --
16800 ---------------------
16802 function Can_Derive_From (E : Entity_Id) return Boolean is
16803 Spec : constant Entity_Id := Real_Range_Specification (Def);
16805 begin
16806 -- Check specified "digits" constraint
16808 if Digs_Val > Digits_Value (E) then
16809 return False;
16810 end if;
16812 -- Check for matching range, if specified
16814 if Present (Spec) then
16815 if Expr_Value_R (Type_Low_Bound (E)) >
16816 Expr_Value_R (Low_Bound (Spec))
16817 then
16818 return False;
16819 end if;
16821 if Expr_Value_R (Type_High_Bound (E)) <
16822 Expr_Value_R (High_Bound (Spec))
16823 then
16824 return False;
16825 end if;
16826 end if;
16828 return True;
16829 end Can_Derive_From;
16831 --------------------
16832 -- Find_Base_Type --
16833 --------------------
16835 function Find_Base_Type return Entity_Id is
16836 Choice : Elmt_Id := First_Elmt (Predefined_Float_Types);
16838 begin
16839 -- Iterate over the predefined types in order, returning the first
16840 -- one that Def can derive from.
16842 while Present (Choice) loop
16843 if Can_Derive_From (Node (Choice)) then
16844 return Node (Choice);
16845 end if;
16847 Next_Elmt (Choice);
16848 end loop;
16850 -- If we can't derive from any existing type, use Long_Long_Float
16851 -- and give appropriate message explaining the problem.
16853 if Digs_Val > Max_Digs_Val then
16854 -- It might be the case that there is a type with the requested
16855 -- range, just not the combination of digits and range.
16857 Error_Msg_N
16858 ("no predefined type has requested range and precision",
16859 Real_Range_Specification (Def));
16861 else
16862 Error_Msg_N
16863 ("range too large for any predefined type",
16864 Real_Range_Specification (Def));
16865 end if;
16867 return Standard_Long_Long_Float;
16868 end Find_Base_Type;
16870 -- Start of processing for Floating_Point_Type_Declaration
16872 begin
16873 Check_Restriction (No_Floating_Point, Def);
16875 -- Create an implicit base type
16877 Implicit_Base :=
16878 Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
16880 -- Analyze and verify digits value
16882 Analyze_And_Resolve (Digs, Any_Integer);
16883 Check_Digits_Expression (Digs);
16884 Digs_Val := Expr_Value (Digs);
16886 -- Process possible range spec and find correct type to derive from
16888 Process_Real_Range_Specification (Def);
16890 -- Check that requested number of digits is not too high.
16892 if Digs_Val > Max_Digs_Val then
16894 -- The check for Max_Base_Digits may be somewhat expensive, as it
16895 -- requires reading System, so only do it when necessary.
16897 declare
16898 Max_Base_Digits : constant Uint :=
16899 Expr_Value
16900 (Expression
16901 (Parent (RTE (RE_Max_Base_Digits))));
16903 begin
16904 if Digs_Val > Max_Base_Digits then
16905 Error_Msg_Uint_1 := Max_Base_Digits;
16906 Error_Msg_N ("digits value out of range, maximum is ^", Digs);
16908 elsif No (Real_Range_Specification (Def)) then
16909 Error_Msg_Uint_1 := Max_Digs_Val;
16910 Error_Msg_N ("types with more than ^ digits need range spec "
16911 & "(RM 3.5.7(6))", Digs);
16912 end if;
16913 end;
16914 end if;
16916 -- Find a suitable type to derive from or complain and use a substitute
16918 Base_Typ := Find_Base_Type;
16920 -- If there are bounds given in the declaration use them as the bounds
16921 -- of the type, otherwise use the bounds of the predefined base type
16922 -- that was chosen based on the Digits value.
16924 if Present (Real_Range_Specification (Def)) then
16925 Set_Scalar_Range (T, Real_Range_Specification (Def));
16926 Set_Is_Constrained (T);
16928 -- The bounds of this range must be converted to machine numbers
16929 -- in accordance with RM 4.9(38).
16931 Bound := Type_Low_Bound (T);
16933 if Nkind (Bound) = N_Real_Literal then
16934 Set_Realval
16935 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
16936 Set_Is_Machine_Number (Bound);
16937 end if;
16939 Bound := Type_High_Bound (T);
16941 if Nkind (Bound) = N_Real_Literal then
16942 Set_Realval
16943 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
16944 Set_Is_Machine_Number (Bound);
16945 end if;
16947 else
16948 Set_Scalar_Range (T, Scalar_Range (Base_Typ));
16949 end if;
16951 -- Complete definition of implicit base and declared first subtype. The
16952 -- inheritance of the rep item chain ensures that SPARK-related pragmas
16953 -- are not clobbered when the floating point type acts as a full view of
16954 -- a private type.
16956 Set_Etype (Implicit_Base, Base_Typ);
16957 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
16958 Set_Size_Info (Implicit_Base, Base_Typ);
16959 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
16960 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
16961 Set_Digits_Value (Implicit_Base, Digits_Value (Base_Typ));
16962 Set_Float_Rep (Implicit_Base, Float_Rep (Base_Typ));
16964 Set_Ekind (T, E_Floating_Point_Subtype);
16965 Set_Etype (T, Implicit_Base);
16966 Set_Size_Info (T, Implicit_Base);
16967 Set_RM_Size (T, RM_Size (Implicit_Base));
16968 Inherit_Rep_Item_Chain (T, Implicit_Base);
16969 Set_Digits_Value (T, Digs_Val);
16970 end Floating_Point_Type_Declaration;
16972 ----------------------------
16973 -- Get_Discriminant_Value --
16974 ----------------------------
16976 -- This is the situation:
16978 -- There is a non-derived type
16980 -- type T0 (Dx, Dy, Dz...)
16982 -- There are zero or more levels of derivation, with each derivation
16983 -- either purely inheriting the discriminants, or defining its own.
16985 -- type Ti is new Ti-1
16986 -- or
16987 -- type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
16988 -- or
16989 -- subtype Ti is ...
16991 -- The subtype issue is avoided by the use of Original_Record_Component,
16992 -- and the fact that derived subtypes also derive the constraints.
16994 -- This chain leads back from
16996 -- Typ_For_Constraint
16998 -- Typ_For_Constraint has discriminants, and the value for each
16999 -- discriminant is given by its corresponding Elmt of Constraints.
17001 -- Discriminant is some discriminant in this hierarchy
17003 -- We need to return its value
17005 -- We do this by recursively searching each level, and looking for
17006 -- Discriminant. Once we get to the bottom, we start backing up
17007 -- returning the value for it which may in turn be a discriminant
17008 -- further up, so on the backup we continue the substitution.
17010 function Get_Discriminant_Value
17011 (Discriminant : Entity_Id;
17012 Typ_For_Constraint : Entity_Id;
17013 Constraint : Elist_Id) return Node_Id
17015 function Root_Corresponding_Discriminant
17016 (Discr : Entity_Id) return Entity_Id;
17017 -- Given a discriminant, traverse the chain of inherited discriminants
17018 -- and return the topmost discriminant.
17020 function Search_Derivation_Levels
17021 (Ti : Entity_Id;
17022 Discrim_Values : Elist_Id;
17023 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
17024 -- This is the routine that performs the recursive search of levels
17025 -- as described above.
17027 -------------------------------------
17028 -- Root_Corresponding_Discriminant --
17029 -------------------------------------
17031 function Root_Corresponding_Discriminant
17032 (Discr : Entity_Id) return Entity_Id
17034 D : Entity_Id;
17036 begin
17037 D := Discr;
17038 while Present (Corresponding_Discriminant (D)) loop
17039 D := Corresponding_Discriminant (D);
17040 end loop;
17042 return D;
17043 end Root_Corresponding_Discriminant;
17045 ------------------------------
17046 -- Search_Derivation_Levels --
17047 ------------------------------
17049 function Search_Derivation_Levels
17050 (Ti : Entity_Id;
17051 Discrim_Values : Elist_Id;
17052 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
17054 Assoc : Elmt_Id;
17055 Disc : Entity_Id;
17056 Result : Node_Or_Entity_Id;
17057 Result_Entity : Node_Id;
17059 begin
17060 -- If inappropriate type, return Error, this happens only in
17061 -- cascaded error situations, and we want to avoid a blow up.
17063 if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
17064 return Error;
17065 end if;
17067 -- Look deeper if possible. Use Stored_Constraints only for
17068 -- untagged types. For tagged types use the given constraint.
17069 -- This asymmetry needs explanation???
17071 if not Stored_Discrim_Values
17072 and then Present (Stored_Constraint (Ti))
17073 and then not Is_Tagged_Type (Ti)
17074 then
17075 Result :=
17076 Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
17077 else
17078 declare
17079 Td : constant Entity_Id := Etype (Ti);
17081 begin
17082 if Td = Ti then
17083 Result := Discriminant;
17085 else
17086 if Present (Stored_Constraint (Ti)) then
17087 Result :=
17088 Search_Derivation_Levels
17089 (Td, Stored_Constraint (Ti), True);
17090 else
17091 Result :=
17092 Search_Derivation_Levels
17093 (Td, Discrim_Values, Stored_Discrim_Values);
17094 end if;
17095 end if;
17096 end;
17097 end if;
17099 -- Extra underlying places to search, if not found above. For
17100 -- concurrent types, the relevant discriminant appears in the
17101 -- corresponding record. For a type derived from a private type
17102 -- without discriminant, the full view inherits the discriminants
17103 -- of the full view of the parent.
17105 if Result = Discriminant then
17106 if Is_Concurrent_Type (Ti)
17107 and then Present (Corresponding_Record_Type (Ti))
17108 then
17109 Result :=
17110 Search_Derivation_Levels (
17111 Corresponding_Record_Type (Ti),
17112 Discrim_Values,
17113 Stored_Discrim_Values);
17115 elsif Is_Private_Type (Ti)
17116 and then not Has_Discriminants (Ti)
17117 and then Present (Full_View (Ti))
17118 and then Etype (Full_View (Ti)) /= Ti
17119 then
17120 Result :=
17121 Search_Derivation_Levels (
17122 Full_View (Ti),
17123 Discrim_Values,
17124 Stored_Discrim_Values);
17125 end if;
17126 end if;
17128 -- If Result is not a (reference to a) discriminant, return it,
17129 -- otherwise set Result_Entity to the discriminant.
17131 if Nkind (Result) = N_Defining_Identifier then
17132 pragma Assert (Result = Discriminant);
17133 Result_Entity := Result;
17135 else
17136 if not Denotes_Discriminant (Result) then
17137 return Result;
17138 end if;
17140 Result_Entity := Entity (Result);
17141 end if;
17143 -- See if this level of derivation actually has discriminants because
17144 -- tagged derivations can add them, hence the lower levels need not
17145 -- have any.
17147 if not Has_Discriminants (Ti) then
17148 return Result;
17149 end if;
17151 -- Scan Ti's discriminants for Result_Entity, and return its
17152 -- corresponding value, if any.
17154 Result_Entity := Original_Record_Component (Result_Entity);
17156 Assoc := First_Elmt (Discrim_Values);
17158 if Stored_Discrim_Values then
17159 Disc := First_Stored_Discriminant (Ti);
17160 else
17161 Disc := First_Discriminant (Ti);
17162 end if;
17164 while Present (Disc) loop
17165 pragma Assert (Present (Assoc));
17167 if Original_Record_Component (Disc) = Result_Entity then
17168 return Node (Assoc);
17169 end if;
17171 Next_Elmt (Assoc);
17173 if Stored_Discrim_Values then
17174 Next_Stored_Discriminant (Disc);
17175 else
17176 Next_Discriminant (Disc);
17177 end if;
17178 end loop;
17180 -- Could not find it
17182 return Result;
17183 end Search_Derivation_Levels;
17185 -- Local Variables
17187 Result : Node_Or_Entity_Id;
17189 -- Start of processing for Get_Discriminant_Value
17191 begin
17192 -- ??? This routine is a gigantic mess and will be deleted. For the
17193 -- time being just test for the trivial case before calling recurse.
17195 if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
17196 declare
17197 D : Entity_Id;
17198 E : Elmt_Id;
17200 begin
17201 D := First_Discriminant (Typ_For_Constraint);
17202 E := First_Elmt (Constraint);
17203 while Present (D) loop
17204 if Chars (D) = Chars (Discriminant) then
17205 return Node (E);
17206 end if;
17208 Next_Discriminant (D);
17209 Next_Elmt (E);
17210 end loop;
17211 end;
17212 end if;
17214 Result := Search_Derivation_Levels
17215 (Typ_For_Constraint, Constraint, False);
17217 -- ??? hack to disappear when this routine is gone
17219 if Nkind (Result) = N_Defining_Identifier then
17220 declare
17221 D : Entity_Id;
17222 E : Elmt_Id;
17224 begin
17225 D := First_Discriminant (Typ_For_Constraint);
17226 E := First_Elmt (Constraint);
17227 while Present (D) loop
17228 if Root_Corresponding_Discriminant (D) = Discriminant then
17229 return Node (E);
17230 end if;
17232 Next_Discriminant (D);
17233 Next_Elmt (E);
17234 end loop;
17235 end;
17236 end if;
17238 pragma Assert (Nkind (Result) /= N_Defining_Identifier);
17239 return Result;
17240 end Get_Discriminant_Value;
17242 --------------------------
17243 -- Has_Range_Constraint --
17244 --------------------------
17246 function Has_Range_Constraint (N : Node_Id) return Boolean is
17247 C : constant Node_Id := Constraint (N);
17249 begin
17250 if Nkind (C) = N_Range_Constraint then
17251 return True;
17253 elsif Nkind (C) = N_Digits_Constraint then
17254 return
17255 Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
17256 or else Present (Range_Constraint (C));
17258 elsif Nkind (C) = N_Delta_Constraint then
17259 return Present (Range_Constraint (C));
17261 else
17262 return False;
17263 end if;
17264 end Has_Range_Constraint;
17266 ------------------------
17267 -- Inherit_Components --
17268 ------------------------
17270 function Inherit_Components
17271 (N : Node_Id;
17272 Parent_Base : Entity_Id;
17273 Derived_Base : Entity_Id;
17274 Is_Tagged : Boolean;
17275 Inherit_Discr : Boolean;
17276 Discs : Elist_Id) return Elist_Id
17278 Assoc_List : constant Elist_Id := New_Elmt_List;
17280 procedure Inherit_Component
17281 (Old_C : Entity_Id;
17282 Plain_Discrim : Boolean := False;
17283 Stored_Discrim : Boolean := False);
17284 -- Inherits component Old_C from Parent_Base to the Derived_Base. If
17285 -- Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
17286 -- True, Old_C is a stored discriminant. If they are both false then
17287 -- Old_C is a regular component.
17289 -----------------------
17290 -- Inherit_Component --
17291 -----------------------
17293 procedure Inherit_Component
17294 (Old_C : Entity_Id;
17295 Plain_Discrim : Boolean := False;
17296 Stored_Discrim : Boolean := False)
17298 procedure Set_Anonymous_Type (Id : Entity_Id);
17299 -- Id denotes the entity of an access discriminant or anonymous
17300 -- access component. Set the type of Id to either the same type of
17301 -- Old_C or create a new one depending on whether the parent and
17302 -- the child types are in the same scope.
17304 ------------------------
17305 -- Set_Anonymous_Type --
17306 ------------------------
17308 procedure Set_Anonymous_Type (Id : Entity_Id) is
17309 Old_Typ : constant Entity_Id := Etype (Old_C);
17311 begin
17312 if Scope (Parent_Base) = Scope (Derived_Base) then
17313 Set_Etype (Id, Old_Typ);
17315 -- The parent and the derived type are in two different scopes.
17316 -- Reuse the type of the original discriminant / component by
17317 -- copying it in order to preserve all attributes.
17319 else
17320 declare
17321 Typ : constant Entity_Id := New_Copy (Old_Typ);
17323 begin
17324 Set_Etype (Id, Typ);
17326 -- Since we do not generate component declarations for
17327 -- inherited components, associate the itype with the
17328 -- derived type.
17330 Set_Associated_Node_For_Itype (Typ, Parent (Derived_Base));
17331 Set_Scope (Typ, Derived_Base);
17332 end;
17333 end if;
17334 end Set_Anonymous_Type;
17336 -- Local variables and constants
17338 New_C : constant Entity_Id := New_Copy (Old_C);
17340 Corr_Discrim : Entity_Id;
17341 Discrim : Entity_Id;
17343 -- Start of processing for Inherit_Component
17345 begin
17346 pragma Assert (not Is_Tagged or not Stored_Discrim);
17348 Set_Parent (New_C, Parent (Old_C));
17350 -- Regular discriminants and components must be inserted in the scope
17351 -- of the Derived_Base. Do it here.
17353 if not Stored_Discrim then
17354 Enter_Name (New_C);
17355 end if;
17357 -- For tagged types the Original_Record_Component must point to
17358 -- whatever this field was pointing to in the parent type. This has
17359 -- already been achieved by the call to New_Copy above.
17361 if not Is_Tagged then
17362 Set_Original_Record_Component (New_C, New_C);
17363 end if;
17365 -- Set the proper type of an access discriminant
17367 if Ekind (New_C) = E_Discriminant
17368 and then Ekind (Etype (New_C)) = E_Anonymous_Access_Type
17369 then
17370 Set_Anonymous_Type (New_C);
17371 end if;
17373 -- If we have inherited a component then see if its Etype contains
17374 -- references to Parent_Base discriminants. In this case, replace
17375 -- these references with the constraints given in Discs. We do not
17376 -- do this for the partial view of private types because this is
17377 -- not needed (only the components of the full view will be used
17378 -- for code generation) and cause problem. We also avoid this
17379 -- transformation in some error situations.
17381 if Ekind (New_C) = E_Component then
17383 -- Set the proper type of an anonymous access component
17385 if Ekind (Etype (New_C)) = E_Anonymous_Access_Type then
17386 Set_Anonymous_Type (New_C);
17388 elsif (Is_Private_Type (Derived_Base)
17389 and then not Is_Generic_Type (Derived_Base))
17390 or else (Is_Empty_Elmt_List (Discs)
17391 and then not Expander_Active)
17392 then
17393 Set_Etype (New_C, Etype (Old_C));
17395 else
17396 -- The current component introduces a circularity of the
17397 -- following kind:
17399 -- limited with Pack_2;
17400 -- package Pack_1 is
17401 -- type T_1 is tagged record
17402 -- Comp : access Pack_2.T_2;
17403 -- ...
17404 -- end record;
17405 -- end Pack_1;
17407 -- with Pack_1;
17408 -- package Pack_2 is
17409 -- type T_2 is new Pack_1.T_1 with ...;
17410 -- end Pack_2;
17412 Set_Etype
17413 (New_C,
17414 Constrain_Component_Type
17415 (Old_C, Derived_Base, N, Parent_Base, Discs));
17416 end if;
17417 end if;
17419 -- In derived tagged types it is illegal to reference a non
17420 -- discriminant component in the parent type. To catch this, mark
17421 -- these components with an Ekind of E_Void. This will be reset in
17422 -- Record_Type_Definition after processing the record extension of
17423 -- the derived type.
17425 -- If the declaration is a private extension, there is no further
17426 -- record extension to process, and the components retain their
17427 -- current kind, because they are visible at this point.
17429 if Is_Tagged and then Ekind (New_C) = E_Component
17430 and then Nkind (N) /= N_Private_Extension_Declaration
17431 then
17432 Set_Ekind (New_C, E_Void);
17433 end if;
17435 if Plain_Discrim then
17436 Set_Corresponding_Discriminant (New_C, Old_C);
17437 Build_Discriminal (New_C);
17439 -- If we are explicitly inheriting a stored discriminant it will be
17440 -- completely hidden.
17442 elsif Stored_Discrim then
17443 Set_Corresponding_Discriminant (New_C, Empty);
17444 Set_Discriminal (New_C, Empty);
17445 Set_Is_Completely_Hidden (New_C);
17447 -- Set the Original_Record_Component of each discriminant in the
17448 -- derived base to point to the corresponding stored that we just
17449 -- created.
17451 Discrim := First_Discriminant (Derived_Base);
17452 while Present (Discrim) loop
17453 Corr_Discrim := Corresponding_Discriminant (Discrim);
17455 -- Corr_Discrim could be missing in an error situation
17457 if Present (Corr_Discrim)
17458 and then Original_Record_Component (Corr_Discrim) = Old_C
17459 then
17460 Set_Original_Record_Component (Discrim, New_C);
17461 end if;
17463 Next_Discriminant (Discrim);
17464 end loop;
17466 Append_Entity (New_C, Derived_Base);
17467 end if;
17469 if not Is_Tagged then
17470 Append_Elmt (Old_C, Assoc_List);
17471 Append_Elmt (New_C, Assoc_List);
17472 end if;
17473 end Inherit_Component;
17475 -- Variables local to Inherit_Component
17477 Loc : constant Source_Ptr := Sloc (N);
17479 Parent_Discrim : Entity_Id;
17480 Stored_Discrim : Entity_Id;
17481 D : Entity_Id;
17482 Component : Entity_Id;
17484 -- Start of processing for Inherit_Components
17486 begin
17487 if not Is_Tagged then
17488 Append_Elmt (Parent_Base, Assoc_List);
17489 Append_Elmt (Derived_Base, Assoc_List);
17490 end if;
17492 -- Inherit parent discriminants if needed
17494 if Inherit_Discr then
17495 Parent_Discrim := First_Discriminant (Parent_Base);
17496 while Present (Parent_Discrim) loop
17497 Inherit_Component (Parent_Discrim, Plain_Discrim => True);
17498 Next_Discriminant (Parent_Discrim);
17499 end loop;
17500 end if;
17502 -- Create explicit stored discrims for untagged types when necessary
17504 if not Has_Unknown_Discriminants (Derived_Base)
17505 and then Has_Discriminants (Parent_Base)
17506 and then not Is_Tagged
17507 and then
17508 (not Inherit_Discr
17509 or else First_Discriminant (Parent_Base) /=
17510 First_Stored_Discriminant (Parent_Base))
17511 then
17512 Stored_Discrim := First_Stored_Discriminant (Parent_Base);
17513 while Present (Stored_Discrim) loop
17514 Inherit_Component (Stored_Discrim, Stored_Discrim => True);
17515 Next_Stored_Discriminant (Stored_Discrim);
17516 end loop;
17517 end if;
17519 -- See if we can apply the second transformation for derived types, as
17520 -- explained in point 6. in the comments above Build_Derived_Record_Type
17521 -- This is achieved by appending Derived_Base discriminants into Discs,
17522 -- which has the side effect of returning a non empty Discs list to the
17523 -- caller of Inherit_Components, which is what we want. This must be
17524 -- done for private derived types if there are explicit stored
17525 -- discriminants, to ensure that we can retrieve the values of the
17526 -- constraints provided in the ancestors.
17528 if Inherit_Discr
17529 and then Is_Empty_Elmt_List (Discs)
17530 and then Present (First_Discriminant (Derived_Base))
17531 and then
17532 (not Is_Private_Type (Derived_Base)
17533 or else Is_Completely_Hidden
17534 (First_Stored_Discriminant (Derived_Base))
17535 or else Is_Generic_Type (Derived_Base))
17536 then
17537 D := First_Discriminant (Derived_Base);
17538 while Present (D) loop
17539 Append_Elmt (New_Occurrence_Of (D, Loc), Discs);
17540 Next_Discriminant (D);
17541 end loop;
17542 end if;
17544 -- Finally, inherit non-discriminant components unless they are not
17545 -- visible because defined or inherited from the full view of the
17546 -- parent. Don't inherit the _parent field of the parent type.
17548 Component := First_Entity (Parent_Base);
17549 while Present (Component) loop
17551 -- Ada 2005 (AI-251): Do not inherit components associated with
17552 -- secondary tags of the parent.
17554 if Ekind (Component) = E_Component
17555 and then Present (Related_Type (Component))
17556 then
17557 null;
17559 elsif Ekind (Component) /= E_Component
17560 or else Chars (Component) = Name_uParent
17561 then
17562 null;
17564 -- If the derived type is within the parent type's declarative
17565 -- region, then the components can still be inherited even though
17566 -- they aren't visible at this point. This can occur for cases
17567 -- such as within public child units where the components must
17568 -- become visible upon entering the child unit's private part.
17570 elsif not Is_Visible_Component (Component)
17571 and then not In_Open_Scopes (Scope (Parent_Base))
17572 then
17573 null;
17575 elsif Ekind_In (Derived_Base, E_Private_Type,
17576 E_Limited_Private_Type)
17577 then
17578 null;
17580 else
17581 Inherit_Component (Component);
17582 end if;
17584 Next_Entity (Component);
17585 end loop;
17587 -- For tagged derived types, inherited discriminants cannot be used in
17588 -- component declarations of the record extension part. To achieve this
17589 -- we mark the inherited discriminants as not visible.
17591 if Is_Tagged and then Inherit_Discr then
17592 D := First_Discriminant (Derived_Base);
17593 while Present (D) loop
17594 Set_Is_Immediately_Visible (D, False);
17595 Next_Discriminant (D);
17596 end loop;
17597 end if;
17599 return Assoc_List;
17600 end Inherit_Components;
17602 -----------------------------
17603 -- Inherit_Predicate_Flags --
17604 -----------------------------
17606 procedure Inherit_Predicate_Flags (Subt, Par : Entity_Id) is
17607 begin
17608 Set_Has_Predicates (Subt, Has_Predicates (Par));
17609 Set_Has_Static_Predicate_Aspect
17610 (Subt, Has_Static_Predicate_Aspect (Par));
17611 Set_Has_Dynamic_Predicate_Aspect
17612 (Subt, Has_Dynamic_Predicate_Aspect (Par));
17613 end Inherit_Predicate_Flags;
17615 ----------------------
17616 -- Is_EVF_Procedure --
17617 ----------------------
17619 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean is
17620 Formal : Entity_Id;
17622 begin
17623 -- Examine the formals of an Extensions_Visible False procedure looking
17624 -- for a controlling OUT parameter.
17626 if Ekind (Subp) = E_Procedure
17627 and then Extensions_Visible_Status (Subp) = Extensions_Visible_False
17628 then
17629 Formal := First_Formal (Subp);
17630 while Present (Formal) loop
17631 if Ekind (Formal) = E_Out_Parameter
17632 and then Is_Controlling_Formal (Formal)
17633 then
17634 return True;
17635 end if;
17637 Next_Formal (Formal);
17638 end loop;
17639 end if;
17641 return False;
17642 end Is_EVF_Procedure;
17644 -----------------------
17645 -- Is_Null_Extension --
17646 -----------------------
17648 function Is_Null_Extension (T : Entity_Id) return Boolean is
17649 Type_Decl : constant Node_Id := Parent (Base_Type (T));
17650 Comp_List : Node_Id;
17651 Comp : Node_Id;
17653 begin
17654 if Nkind (Type_Decl) /= N_Full_Type_Declaration
17655 or else not Is_Tagged_Type (T)
17656 or else Nkind (Type_Definition (Type_Decl)) /=
17657 N_Derived_Type_Definition
17658 or else No (Record_Extension_Part (Type_Definition (Type_Decl)))
17659 then
17660 return False;
17661 end if;
17663 Comp_List :=
17664 Component_List (Record_Extension_Part (Type_Definition (Type_Decl)));
17666 if Present (Discriminant_Specifications (Type_Decl)) then
17667 return False;
17669 elsif Present (Comp_List)
17670 and then Is_Non_Empty_List (Component_Items (Comp_List))
17671 then
17672 Comp := First (Component_Items (Comp_List));
17674 -- Only user-defined components are relevant. The component list
17675 -- may also contain a parent component and internal components
17676 -- corresponding to secondary tags, but these do not determine
17677 -- whether this is a null extension.
17679 while Present (Comp) loop
17680 if Comes_From_Source (Comp) then
17681 return False;
17682 end if;
17684 Next (Comp);
17685 end loop;
17687 return True;
17689 else
17690 return True;
17691 end if;
17692 end Is_Null_Extension;
17694 ------------------------------
17695 -- Is_Valid_Constraint_Kind --
17696 ------------------------------
17698 function Is_Valid_Constraint_Kind
17699 (T_Kind : Type_Kind;
17700 Constraint_Kind : Node_Kind) return Boolean
17702 begin
17703 case T_Kind is
17704 when Enumeration_Kind |
17705 Integer_Kind =>
17706 return Constraint_Kind = N_Range_Constraint;
17708 when Decimal_Fixed_Point_Kind =>
17709 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
17710 N_Range_Constraint);
17712 when Ordinary_Fixed_Point_Kind =>
17713 return Nkind_In (Constraint_Kind, N_Delta_Constraint,
17714 N_Range_Constraint);
17716 when Float_Kind =>
17717 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
17718 N_Range_Constraint);
17720 when Access_Kind |
17721 Array_Kind |
17722 E_Record_Type |
17723 E_Record_Subtype |
17724 Class_Wide_Kind |
17725 E_Incomplete_Type |
17726 Private_Kind |
17727 Concurrent_Kind =>
17728 return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
17730 when others =>
17731 return True; -- Error will be detected later
17732 end case;
17733 end Is_Valid_Constraint_Kind;
17735 --------------------------
17736 -- Is_Visible_Component --
17737 --------------------------
17739 function Is_Visible_Component
17740 (C : Entity_Id;
17741 N : Node_Id := Empty) return Boolean
17743 Original_Comp : Entity_Id := Empty;
17744 Original_Scope : Entity_Id;
17745 Type_Scope : Entity_Id;
17747 function Is_Local_Type (Typ : Entity_Id) return Boolean;
17748 -- Check whether parent type of inherited component is declared locally,
17749 -- possibly within a nested package or instance. The current scope is
17750 -- the derived record itself.
17752 -------------------
17753 -- Is_Local_Type --
17754 -------------------
17756 function Is_Local_Type (Typ : Entity_Id) return Boolean is
17757 Scop : Entity_Id;
17759 begin
17760 Scop := Scope (Typ);
17761 while Present (Scop)
17762 and then Scop /= Standard_Standard
17763 loop
17764 if Scop = Scope (Current_Scope) then
17765 return True;
17766 end if;
17768 Scop := Scope (Scop);
17769 end loop;
17771 return False;
17772 end Is_Local_Type;
17774 -- Start of processing for Is_Visible_Component
17776 begin
17777 if Ekind_In (C, E_Component, E_Discriminant) then
17778 Original_Comp := Original_Record_Component (C);
17779 end if;
17781 if No (Original_Comp) then
17783 -- Premature usage, or previous error
17785 return False;
17787 else
17788 Original_Scope := Scope (Original_Comp);
17789 Type_Scope := Scope (Base_Type (Scope (C)));
17790 end if;
17792 -- This test only concerns tagged types
17794 if not Is_Tagged_Type (Original_Scope) then
17795 return True;
17797 -- If it is _Parent or _Tag, there is no visibility issue
17799 elsif not Comes_From_Source (Original_Comp) then
17800 return True;
17802 -- Discriminants are visible unless the (private) type has unknown
17803 -- discriminants. If the discriminant reference is inserted for a
17804 -- discriminant check on a full view it is also visible.
17806 elsif Ekind (Original_Comp) = E_Discriminant
17807 and then
17808 (not Has_Unknown_Discriminants (Original_Scope)
17809 or else (Present (N)
17810 and then Nkind (N) = N_Selected_Component
17811 and then Nkind (Prefix (N)) = N_Type_Conversion
17812 and then not Comes_From_Source (Prefix (N))))
17813 then
17814 return True;
17816 -- In the body of an instantiation, no need to check for the visibility
17817 -- of a component.
17819 elsif In_Instance_Body then
17820 return True;
17822 -- If the component has been declared in an ancestor which is currently
17823 -- a private type, then it is not visible. The same applies if the
17824 -- component's containing type is not in an open scope and the original
17825 -- component's enclosing type is a visible full view of a private type
17826 -- (which can occur in cases where an attempt is being made to reference
17827 -- a component in a sibling package that is inherited from a visible
17828 -- component of a type in an ancestor package; the component in the
17829 -- sibling package should not be visible even though the component it
17830 -- inherited from is visible). This does not apply however in the case
17831 -- where the scope of the type is a private child unit, or when the
17832 -- parent comes from a local package in which the ancestor is currently
17833 -- visible. The latter suppression of visibility is needed for cases
17834 -- that are tested in B730006.
17836 elsif Is_Private_Type (Original_Scope)
17837 or else
17838 (not Is_Private_Descendant (Type_Scope)
17839 and then not In_Open_Scopes (Type_Scope)
17840 and then Has_Private_Declaration (Original_Scope))
17841 then
17842 -- If the type derives from an entity in a formal package, there
17843 -- are no additional visible components.
17845 if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
17846 N_Formal_Package_Declaration
17847 then
17848 return False;
17850 -- if we are not in the private part of the current package, there
17851 -- are no additional visible components.
17853 elsif Ekind (Scope (Current_Scope)) = E_Package
17854 and then not In_Private_Part (Scope (Current_Scope))
17855 then
17856 return False;
17857 else
17858 return
17859 Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
17860 and then In_Open_Scopes (Scope (Original_Scope))
17861 and then Is_Local_Type (Type_Scope);
17862 end if;
17864 -- There is another weird way in which a component may be invisible when
17865 -- the private and the full view are not derived from the same ancestor.
17866 -- Here is an example :
17868 -- type A1 is tagged record F1 : integer; end record;
17869 -- type A2 is new A1 with record F2 : integer; end record;
17870 -- type T is new A1 with private;
17871 -- private
17872 -- type T is new A2 with null record;
17874 -- In this case, the full view of T inherits F1 and F2 but the private
17875 -- view inherits only F1
17877 else
17878 declare
17879 Ancestor : Entity_Id := Scope (C);
17881 begin
17882 loop
17883 if Ancestor = Original_Scope then
17884 return True;
17885 elsif Ancestor = Etype (Ancestor) then
17886 return False;
17887 end if;
17889 Ancestor := Etype (Ancestor);
17890 end loop;
17891 end;
17892 end if;
17893 end Is_Visible_Component;
17895 --------------------------
17896 -- Make_Class_Wide_Type --
17897 --------------------------
17899 procedure Make_Class_Wide_Type (T : Entity_Id) is
17900 CW_Type : Entity_Id;
17901 CW_Name : Name_Id;
17902 Next_E : Entity_Id;
17904 begin
17905 if Present (Class_Wide_Type (T)) then
17907 -- The class-wide type is a partially decorated entity created for a
17908 -- unanalyzed tagged type referenced through a limited with clause.
17909 -- When the tagged type is analyzed, its class-wide type needs to be
17910 -- redecorated. Note that we reuse the entity created by Decorate_
17911 -- Tagged_Type in order to preserve all links.
17913 if Materialize_Entity (Class_Wide_Type (T)) then
17914 CW_Type := Class_Wide_Type (T);
17915 Set_Materialize_Entity (CW_Type, False);
17917 -- The class wide type can have been defined by the partial view, in
17918 -- which case everything is already done.
17920 else
17921 return;
17922 end if;
17924 -- Default case, we need to create a new class-wide type
17926 else
17927 CW_Type :=
17928 New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
17929 end if;
17931 -- Inherit root type characteristics
17933 CW_Name := Chars (CW_Type);
17934 Next_E := Next_Entity (CW_Type);
17935 Copy_Node (T, CW_Type);
17936 Set_Comes_From_Source (CW_Type, False);
17937 Set_Chars (CW_Type, CW_Name);
17938 Set_Parent (CW_Type, Parent (T));
17939 Set_Next_Entity (CW_Type, Next_E);
17941 -- Ensure we have a new freeze node for the class-wide type. The partial
17942 -- view may have freeze action of its own, requiring a proper freeze
17943 -- node, and the same freeze node cannot be shared between the two
17944 -- types.
17946 Set_Has_Delayed_Freeze (CW_Type);
17947 Set_Freeze_Node (CW_Type, Empty);
17949 -- Customize the class-wide type: It has no prim. op., it cannot be
17950 -- abstract and its Etype points back to the specific root type.
17952 Set_Ekind (CW_Type, E_Class_Wide_Type);
17953 Set_Is_Tagged_Type (CW_Type, True);
17954 Set_Direct_Primitive_Operations (CW_Type, New_Elmt_List);
17955 Set_Is_Abstract_Type (CW_Type, False);
17956 Set_Is_Constrained (CW_Type, False);
17957 Set_Is_First_Subtype (CW_Type, Is_First_Subtype (T));
17958 Set_Default_SSO (CW_Type);
17960 if Ekind (T) = E_Class_Wide_Subtype then
17961 Set_Etype (CW_Type, Etype (Base_Type (T)));
17962 else
17963 Set_Etype (CW_Type, T);
17964 end if;
17966 Set_No_Tagged_Streams_Pragma (CW_Type, No_Tagged_Streams);
17968 -- If this is the class_wide type of a constrained subtype, it does
17969 -- not have discriminants.
17971 Set_Has_Discriminants (CW_Type,
17972 Has_Discriminants (T) and then not Is_Constrained (T));
17974 Set_Has_Unknown_Discriminants (CW_Type, True);
17975 Set_Class_Wide_Type (T, CW_Type);
17976 Set_Equivalent_Type (CW_Type, Empty);
17978 -- The class-wide type of a class-wide type is itself (RM 3.9(14))
17980 Set_Class_Wide_Type (CW_Type, CW_Type);
17981 end Make_Class_Wide_Type;
17983 ----------------
17984 -- Make_Index --
17985 ----------------
17987 procedure Make_Index
17988 (N : Node_Id;
17989 Related_Nod : Node_Id;
17990 Related_Id : Entity_Id := Empty;
17991 Suffix_Index : Nat := 1;
17992 In_Iter_Schm : Boolean := False)
17994 R : Node_Id;
17995 T : Entity_Id;
17996 Def_Id : Entity_Id := Empty;
17997 Found : Boolean := False;
17999 begin
18000 -- For a discrete range used in a constrained array definition and
18001 -- defined by a range, an implicit conversion to the predefined type
18002 -- INTEGER is assumed if each bound is either a numeric literal, a named
18003 -- number, or an attribute, and the type of both bounds (prior to the
18004 -- implicit conversion) is the type universal_integer. Otherwise, both
18005 -- bounds must be of the same discrete type, other than universal
18006 -- integer; this type must be determinable independently of the
18007 -- context, but using the fact that the type must be discrete and that
18008 -- both bounds must have the same type.
18010 -- Character literals also have a universal type in the absence of
18011 -- of additional context, and are resolved to Standard_Character.
18013 if Nkind (N) = N_Range then
18015 -- The index is given by a range constraint. The bounds are known
18016 -- to be of a consistent type.
18018 if not Is_Overloaded (N) then
18019 T := Etype (N);
18021 -- For universal bounds, choose the specific predefined type
18023 if T = Universal_Integer then
18024 T := Standard_Integer;
18026 elsif T = Any_Character then
18027 Ambiguous_Character (Low_Bound (N));
18029 T := Standard_Character;
18030 end if;
18032 -- The node may be overloaded because some user-defined operators
18033 -- are available, but if a universal interpretation exists it is
18034 -- also the selected one.
18036 elsif Universal_Interpretation (N) = Universal_Integer then
18037 T := Standard_Integer;
18039 else
18040 T := Any_Type;
18042 declare
18043 Ind : Interp_Index;
18044 It : Interp;
18046 begin
18047 Get_First_Interp (N, Ind, It);
18048 while Present (It.Typ) loop
18049 if Is_Discrete_Type (It.Typ) then
18051 if Found
18052 and then not Covers (It.Typ, T)
18053 and then not Covers (T, It.Typ)
18054 then
18055 Error_Msg_N ("ambiguous bounds in discrete range", N);
18056 exit;
18057 else
18058 T := It.Typ;
18059 Found := True;
18060 end if;
18061 end if;
18063 Get_Next_Interp (Ind, It);
18064 end loop;
18066 if T = Any_Type then
18067 Error_Msg_N ("discrete type required for range", N);
18068 Set_Etype (N, Any_Type);
18069 return;
18071 elsif T = Universal_Integer then
18072 T := Standard_Integer;
18073 end if;
18074 end;
18075 end if;
18077 if not Is_Discrete_Type (T) then
18078 Error_Msg_N ("discrete type required for range", N);
18079 Set_Etype (N, Any_Type);
18080 return;
18081 end if;
18083 if Nkind (Low_Bound (N)) = N_Attribute_Reference
18084 and then Attribute_Name (Low_Bound (N)) = Name_First
18085 and then Is_Entity_Name (Prefix (Low_Bound (N)))
18086 and then Is_Type (Entity (Prefix (Low_Bound (N))))
18087 and then Is_Discrete_Type (Entity (Prefix (Low_Bound (N))))
18088 then
18089 -- The type of the index will be the type of the prefix, as long
18090 -- as the upper bound is 'Last of the same type.
18092 Def_Id := Entity (Prefix (Low_Bound (N)));
18094 if Nkind (High_Bound (N)) /= N_Attribute_Reference
18095 or else Attribute_Name (High_Bound (N)) /= Name_Last
18096 or else not Is_Entity_Name (Prefix (High_Bound (N)))
18097 or else Entity (Prefix (High_Bound (N))) /= Def_Id
18098 then
18099 Def_Id := Empty;
18100 end if;
18101 end if;
18103 R := N;
18104 Process_Range_Expr_In_Decl (R, T, In_Iter_Schm => In_Iter_Schm);
18106 elsif Nkind (N) = N_Subtype_Indication then
18108 -- The index is given by a subtype with a range constraint
18110 T := Base_Type (Entity (Subtype_Mark (N)));
18112 if not Is_Discrete_Type (T) then
18113 Error_Msg_N ("discrete type required for range", N);
18114 Set_Etype (N, Any_Type);
18115 return;
18116 end if;
18118 R := Range_Expression (Constraint (N));
18120 Resolve (R, T);
18121 Process_Range_Expr_In_Decl
18122 (R, Entity (Subtype_Mark (N)), In_Iter_Schm => In_Iter_Schm);
18124 elsif Nkind (N) = N_Attribute_Reference then
18126 -- Catch beginner's error (use of attribute other than 'Range)
18128 if Attribute_Name (N) /= Name_Range then
18129 Error_Msg_N ("expect attribute ''Range", N);
18130 Set_Etype (N, Any_Type);
18131 return;
18132 end if;
18134 -- If the node denotes the range of a type mark, that is also the
18135 -- resulting type, and we do not need to create an Itype for it.
18137 if Is_Entity_Name (Prefix (N))
18138 and then Comes_From_Source (N)
18139 and then Is_Type (Entity (Prefix (N)))
18140 and then Is_Discrete_Type (Entity (Prefix (N)))
18141 then
18142 Def_Id := Entity (Prefix (N));
18143 end if;
18145 Analyze_And_Resolve (N);
18146 T := Etype (N);
18147 R := N;
18149 -- If none of the above, must be a subtype. We convert this to a
18150 -- range attribute reference because in the case of declared first
18151 -- named subtypes, the types in the range reference can be different
18152 -- from the type of the entity. A range attribute normalizes the
18153 -- reference and obtains the correct types for the bounds.
18155 -- This transformation is in the nature of an expansion, is only
18156 -- done if expansion is active. In particular, it is not done on
18157 -- formal generic types, because we need to retain the name of the
18158 -- original index for instantiation purposes.
18160 else
18161 if not Is_Entity_Name (N) or else not Is_Type (Entity (N)) then
18162 Error_Msg_N ("invalid subtype mark in discrete range ", N);
18163 Set_Etype (N, Any_Integer);
18164 return;
18166 else
18167 -- The type mark may be that of an incomplete type. It is only
18168 -- now that we can get the full view, previous analysis does
18169 -- not look specifically for a type mark.
18171 Set_Entity (N, Get_Full_View (Entity (N)));
18172 Set_Etype (N, Entity (N));
18173 Def_Id := Entity (N);
18175 if not Is_Discrete_Type (Def_Id) then
18176 Error_Msg_N ("discrete type required for index", N);
18177 Set_Etype (N, Any_Type);
18178 return;
18179 end if;
18180 end if;
18182 if Expander_Active then
18183 Rewrite (N,
18184 Make_Attribute_Reference (Sloc (N),
18185 Attribute_Name => Name_Range,
18186 Prefix => Relocate_Node (N)));
18188 -- The original was a subtype mark that does not freeze. This
18189 -- means that the rewritten version must not freeze either.
18191 Set_Must_Not_Freeze (N);
18192 Set_Must_Not_Freeze (Prefix (N));
18193 Analyze_And_Resolve (N);
18194 T := Etype (N);
18195 R := N;
18197 -- If expander is inactive, type is legal, nothing else to construct
18199 else
18200 return;
18201 end if;
18202 end if;
18204 if not Is_Discrete_Type (T) then
18205 Error_Msg_N ("discrete type required for range", N);
18206 Set_Etype (N, Any_Type);
18207 return;
18209 elsif T = Any_Type then
18210 Set_Etype (N, Any_Type);
18211 return;
18212 end if;
18214 -- We will now create the appropriate Itype to describe the range, but
18215 -- first a check. If we originally had a subtype, then we just label
18216 -- the range with this subtype. Not only is there no need to construct
18217 -- a new subtype, but it is wrong to do so for two reasons:
18219 -- 1. A legality concern, if we have a subtype, it must not freeze,
18220 -- and the Itype would cause freezing incorrectly
18222 -- 2. An efficiency concern, if we created an Itype, it would not be
18223 -- recognized as the same type for the purposes of eliminating
18224 -- checks in some circumstances.
18226 -- We signal this case by setting the subtype entity in Def_Id
18228 if No (Def_Id) then
18229 Def_Id :=
18230 Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
18231 Set_Etype (Def_Id, Base_Type (T));
18233 if Is_Signed_Integer_Type (T) then
18234 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
18236 elsif Is_Modular_Integer_Type (T) then
18237 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
18239 else
18240 Set_Ekind (Def_Id, E_Enumeration_Subtype);
18241 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
18242 Set_First_Literal (Def_Id, First_Literal (T));
18243 end if;
18245 Set_Size_Info (Def_Id, (T));
18246 Set_RM_Size (Def_Id, RM_Size (T));
18247 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
18249 Set_Scalar_Range (Def_Id, R);
18250 Conditional_Delay (Def_Id, T);
18252 if Nkind (N) = N_Subtype_Indication then
18253 Inherit_Predicate_Flags (Def_Id, Entity (Subtype_Mark (N)));
18254 end if;
18256 -- In the subtype indication case, if the immediate parent of the
18257 -- new subtype is non-static, then the subtype we create is non-
18258 -- static, even if its bounds are static.
18260 if Nkind (N) = N_Subtype_Indication
18261 and then not Is_OK_Static_Subtype (Entity (Subtype_Mark (N)))
18262 then
18263 Set_Is_Non_Static_Subtype (Def_Id);
18264 end if;
18265 end if;
18267 -- Final step is to label the index with this constructed type
18269 Set_Etype (N, Def_Id);
18270 end Make_Index;
18272 ------------------------------
18273 -- Modular_Type_Declaration --
18274 ------------------------------
18276 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
18277 Mod_Expr : constant Node_Id := Expression (Def);
18278 M_Val : Uint;
18280 procedure Set_Modular_Size (Bits : Int);
18281 -- Sets RM_Size to Bits, and Esize to normal word size above this
18283 ----------------------
18284 -- Set_Modular_Size --
18285 ----------------------
18287 procedure Set_Modular_Size (Bits : Int) is
18288 begin
18289 Set_RM_Size (T, UI_From_Int (Bits));
18291 if Bits <= 8 then
18292 Init_Esize (T, 8);
18294 elsif Bits <= 16 then
18295 Init_Esize (T, 16);
18297 elsif Bits <= 32 then
18298 Init_Esize (T, 32);
18300 else
18301 Init_Esize (T, System_Max_Binary_Modulus_Power);
18302 end if;
18304 if not Non_Binary_Modulus (T) and then Esize (T) = RM_Size (T) then
18305 Set_Is_Known_Valid (T);
18306 end if;
18307 end Set_Modular_Size;
18309 -- Start of processing for Modular_Type_Declaration
18311 begin
18312 -- If the mod expression is (exactly) 2 * literal, where literal is
18313 -- 64 or less,then almost certainly the * was meant to be **. Warn.
18315 if Warn_On_Suspicious_Modulus_Value
18316 and then Nkind (Mod_Expr) = N_Op_Multiply
18317 and then Nkind (Left_Opnd (Mod_Expr)) = N_Integer_Literal
18318 and then Intval (Left_Opnd (Mod_Expr)) = Uint_2
18319 and then Nkind (Right_Opnd (Mod_Expr)) = N_Integer_Literal
18320 and then Intval (Right_Opnd (Mod_Expr)) <= Uint_64
18321 then
18322 Error_Msg_N
18323 ("suspicious MOD value, was '*'* intended'??M?", Mod_Expr);
18324 end if;
18326 -- Proceed with analysis of mod expression
18328 Analyze_And_Resolve (Mod_Expr, Any_Integer);
18329 Set_Etype (T, T);
18330 Set_Ekind (T, E_Modular_Integer_Type);
18331 Init_Alignment (T);
18332 Set_Is_Constrained (T);
18334 if not Is_OK_Static_Expression (Mod_Expr) then
18335 Flag_Non_Static_Expr
18336 ("non-static expression used for modular type bound!", Mod_Expr);
18337 M_Val := 2 ** System_Max_Binary_Modulus_Power;
18338 else
18339 M_Val := Expr_Value (Mod_Expr);
18340 end if;
18342 if M_Val < 1 then
18343 Error_Msg_N ("modulus value must be positive", Mod_Expr);
18344 M_Val := 2 ** System_Max_Binary_Modulus_Power;
18345 end if;
18347 if M_Val > 2 ** Standard_Long_Integer_Size then
18348 Check_Restriction (No_Long_Long_Integers, Mod_Expr);
18349 end if;
18351 Set_Modulus (T, M_Val);
18353 -- Create bounds for the modular type based on the modulus given in
18354 -- the type declaration and then analyze and resolve those bounds.
18356 Set_Scalar_Range (T,
18357 Make_Range (Sloc (Mod_Expr),
18358 Low_Bound => Make_Integer_Literal (Sloc (Mod_Expr), 0),
18359 High_Bound => Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
18361 -- Properly analyze the literals for the range. We do this manually
18362 -- because we can't go calling Resolve, since we are resolving these
18363 -- bounds with the type, and this type is certainly not complete yet.
18365 Set_Etype (Low_Bound (Scalar_Range (T)), T);
18366 Set_Etype (High_Bound (Scalar_Range (T)), T);
18367 Set_Is_Static_Expression (Low_Bound (Scalar_Range (T)));
18368 Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
18370 -- Loop through powers of two to find number of bits required
18372 for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
18374 -- Binary case
18376 if M_Val = 2 ** Bits then
18377 Set_Modular_Size (Bits);
18378 return;
18380 -- Non-binary case
18382 elsif M_Val < 2 ** Bits then
18383 Check_SPARK_05_Restriction ("modulus should be a power of 2", T);
18384 Set_Non_Binary_Modulus (T);
18386 if Bits > System_Max_Nonbinary_Modulus_Power then
18387 Error_Msg_Uint_1 :=
18388 UI_From_Int (System_Max_Nonbinary_Modulus_Power);
18389 Error_Msg_F
18390 ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
18391 Set_Modular_Size (System_Max_Binary_Modulus_Power);
18392 return;
18394 else
18395 -- In the non-binary case, set size as per RM 13.3(55)
18397 Set_Modular_Size (Bits);
18398 return;
18399 end if;
18400 end if;
18402 end loop;
18404 -- If we fall through, then the size exceed System.Max_Binary_Modulus
18405 -- so we just signal an error and set the maximum size.
18407 Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
18408 Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
18410 Set_Modular_Size (System_Max_Binary_Modulus_Power);
18411 Init_Alignment (T);
18413 end Modular_Type_Declaration;
18415 --------------------------
18416 -- New_Concatenation_Op --
18417 --------------------------
18419 procedure New_Concatenation_Op (Typ : Entity_Id) is
18420 Loc : constant Source_Ptr := Sloc (Typ);
18421 Op : Entity_Id;
18423 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
18424 -- Create abbreviated declaration for the formal of a predefined
18425 -- Operator 'Op' of type 'Typ'
18427 --------------------
18428 -- Make_Op_Formal --
18429 --------------------
18431 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
18432 Formal : Entity_Id;
18433 begin
18434 Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
18435 Set_Etype (Formal, Typ);
18436 Set_Mechanism (Formal, Default_Mechanism);
18437 return Formal;
18438 end Make_Op_Formal;
18440 -- Start of processing for New_Concatenation_Op
18442 begin
18443 Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
18445 Set_Ekind (Op, E_Operator);
18446 Set_Scope (Op, Current_Scope);
18447 Set_Etype (Op, Typ);
18448 Set_Homonym (Op, Get_Name_Entity_Id (Name_Op_Concat));
18449 Set_Is_Immediately_Visible (Op);
18450 Set_Is_Intrinsic_Subprogram (Op);
18451 Set_Has_Completion (Op);
18452 Append_Entity (Op, Current_Scope);
18454 Set_Name_Entity_Id (Name_Op_Concat, Op);
18456 Append_Entity (Make_Op_Formal (Typ, Op), Op);
18457 Append_Entity (Make_Op_Formal (Typ, Op), Op);
18458 end New_Concatenation_Op;
18460 -------------------------
18461 -- OK_For_Limited_Init --
18462 -------------------------
18464 -- ???Check all calls of this, and compare the conditions under which it's
18465 -- called.
18467 function OK_For_Limited_Init
18468 (Typ : Entity_Id;
18469 Exp : Node_Id) return Boolean
18471 begin
18472 return Is_CPP_Constructor_Call (Exp)
18473 or else (Ada_Version >= Ada_2005
18474 and then not Debug_Flag_Dot_L
18475 and then OK_For_Limited_Init_In_05 (Typ, Exp));
18476 end OK_For_Limited_Init;
18478 -------------------------------
18479 -- OK_For_Limited_Init_In_05 --
18480 -------------------------------
18482 function OK_For_Limited_Init_In_05
18483 (Typ : Entity_Id;
18484 Exp : Node_Id) return Boolean
18486 begin
18487 -- An object of a limited interface type can be initialized with any
18488 -- expression of a nonlimited descendant type.
18490 if Is_Class_Wide_Type (Typ)
18491 and then Is_Limited_Interface (Typ)
18492 and then not Is_Limited_Type (Etype (Exp))
18493 then
18494 return True;
18495 end if;
18497 -- Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
18498 -- case of limited aggregates (including extension aggregates), and
18499 -- function calls. The function call may have been given in prefixed
18500 -- notation, in which case the original node is an indexed component.
18501 -- If the function is parameterless, the original node was an explicit
18502 -- dereference. The function may also be parameterless, in which case
18503 -- the source node is just an identifier.
18505 case Nkind (Original_Node (Exp)) is
18506 when N_Aggregate | N_Extension_Aggregate | N_Function_Call | N_Op =>
18507 return True;
18509 when N_Identifier =>
18510 return Present (Entity (Original_Node (Exp)))
18511 and then Ekind (Entity (Original_Node (Exp))) = E_Function;
18513 when N_Qualified_Expression =>
18514 return
18515 OK_For_Limited_Init_In_05
18516 (Typ, Expression (Original_Node (Exp)));
18518 -- Ada 2005 (AI-251): If a class-wide interface object is initialized
18519 -- with a function call, the expander has rewritten the call into an
18520 -- N_Type_Conversion node to force displacement of the pointer to
18521 -- reference the component containing the secondary dispatch table.
18522 -- Otherwise a type conversion is not a legal context.
18523 -- A return statement for a build-in-place function returning a
18524 -- synchronized type also introduces an unchecked conversion.
18526 when N_Type_Conversion |
18527 N_Unchecked_Type_Conversion =>
18528 return not Comes_From_Source (Exp)
18529 and then
18530 OK_For_Limited_Init_In_05
18531 (Typ, Expression (Original_Node (Exp)));
18533 when N_Indexed_Component |
18534 N_Selected_Component |
18535 N_Explicit_Dereference =>
18536 return Nkind (Exp) = N_Function_Call;
18538 -- A use of 'Input is a function call, hence allowed. Normally the
18539 -- attribute will be changed to a call, but the attribute by itself
18540 -- can occur with -gnatc.
18542 when N_Attribute_Reference =>
18543 return Attribute_Name (Original_Node (Exp)) = Name_Input;
18545 -- For a case expression, all dependent expressions must be legal
18547 when N_Case_Expression =>
18548 declare
18549 Alt : Node_Id;
18551 begin
18552 Alt := First (Alternatives (Original_Node (Exp)));
18553 while Present (Alt) loop
18554 if not OK_For_Limited_Init_In_05 (Typ, Expression (Alt)) then
18555 return False;
18556 end if;
18558 Next (Alt);
18559 end loop;
18561 return True;
18562 end;
18564 -- For an if expression, all dependent expressions must be legal
18566 when N_If_Expression =>
18567 declare
18568 Then_Expr : constant Node_Id :=
18569 Next (First (Expressions (Original_Node (Exp))));
18570 Else_Expr : constant Node_Id := Next (Then_Expr);
18571 begin
18572 return OK_For_Limited_Init_In_05 (Typ, Then_Expr)
18573 and then
18574 OK_For_Limited_Init_In_05 (Typ, Else_Expr);
18575 end;
18577 when others =>
18578 return False;
18579 end case;
18580 end OK_For_Limited_Init_In_05;
18582 -------------------------------------------
18583 -- Ordinary_Fixed_Point_Type_Declaration --
18584 -------------------------------------------
18586 procedure Ordinary_Fixed_Point_Type_Declaration
18587 (T : Entity_Id;
18588 Def : Node_Id)
18590 Loc : constant Source_Ptr := Sloc (Def);
18591 Delta_Expr : constant Node_Id := Delta_Expression (Def);
18592 RRS : constant Node_Id := Real_Range_Specification (Def);
18593 Implicit_Base : Entity_Id;
18594 Delta_Val : Ureal;
18595 Small_Val : Ureal;
18596 Low_Val : Ureal;
18597 High_Val : Ureal;
18599 begin
18600 Check_Restriction (No_Fixed_Point, Def);
18602 -- Create implicit base type
18604 Implicit_Base :=
18605 Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
18606 Set_Etype (Implicit_Base, Implicit_Base);
18608 -- Analyze and process delta expression
18610 Analyze_And_Resolve (Delta_Expr, Any_Real);
18612 Check_Delta_Expression (Delta_Expr);
18613 Delta_Val := Expr_Value_R (Delta_Expr);
18615 Set_Delta_Value (Implicit_Base, Delta_Val);
18617 -- Compute default small from given delta, which is the largest power
18618 -- of two that does not exceed the given delta value.
18620 declare
18621 Tmp : Ureal;
18622 Scale : Int;
18624 begin
18625 Tmp := Ureal_1;
18626 Scale := 0;
18628 if Delta_Val < Ureal_1 then
18629 while Delta_Val < Tmp loop
18630 Tmp := Tmp / Ureal_2;
18631 Scale := Scale + 1;
18632 end loop;
18634 else
18635 loop
18636 Tmp := Tmp * Ureal_2;
18637 exit when Tmp > Delta_Val;
18638 Scale := Scale - 1;
18639 end loop;
18640 end if;
18642 Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
18643 end;
18645 Set_Small_Value (Implicit_Base, Small_Val);
18647 -- If no range was given, set a dummy range
18649 if RRS <= Empty_Or_Error then
18650 Low_Val := -Small_Val;
18651 High_Val := Small_Val;
18653 -- Otherwise analyze and process given range
18655 else
18656 declare
18657 Low : constant Node_Id := Low_Bound (RRS);
18658 High : constant Node_Id := High_Bound (RRS);
18660 begin
18661 Analyze_And_Resolve (Low, Any_Real);
18662 Analyze_And_Resolve (High, Any_Real);
18663 Check_Real_Bound (Low);
18664 Check_Real_Bound (High);
18666 -- Obtain and set the range
18668 Low_Val := Expr_Value_R (Low);
18669 High_Val := Expr_Value_R (High);
18671 if Low_Val > High_Val then
18672 Error_Msg_NE ("??fixed point type& has null range", Def, T);
18673 end if;
18674 end;
18675 end if;
18677 -- The range for both the implicit base and the declared first subtype
18678 -- cannot be set yet, so we use the special routine Set_Fixed_Range to
18679 -- set a temporary range in place. Note that the bounds of the base
18680 -- type will be widened to be symmetrical and to fill the available
18681 -- bits when the type is frozen.
18683 -- We could do this with all discrete types, and probably should, but
18684 -- we absolutely have to do it for fixed-point, since the end-points
18685 -- of the range and the size are determined by the small value, which
18686 -- could be reset before the freeze point.
18688 Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
18689 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
18691 -- Complete definition of first subtype. The inheritance of the rep item
18692 -- chain ensures that SPARK-related pragmas are not clobbered when the
18693 -- ordinary fixed point type acts as a full view of a private type.
18695 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
18696 Set_Etype (T, Implicit_Base);
18697 Init_Size_Align (T);
18698 Inherit_Rep_Item_Chain (T, Implicit_Base);
18699 Set_Small_Value (T, Small_Val);
18700 Set_Delta_Value (T, Delta_Val);
18701 Set_Is_Constrained (T);
18702 end Ordinary_Fixed_Point_Type_Declaration;
18704 ----------------------------------
18705 -- Preanalyze_Assert_Expression --
18706 ----------------------------------
18708 procedure Preanalyze_Assert_Expression (N : Node_Id; T : Entity_Id) is
18709 begin
18710 In_Assertion_Expr := In_Assertion_Expr + 1;
18711 Preanalyze_Spec_Expression (N, T);
18712 In_Assertion_Expr := In_Assertion_Expr - 1;
18713 end Preanalyze_Assert_Expression;
18715 -----------------------------------
18716 -- Preanalyze_Default_Expression --
18717 -----------------------------------
18719 procedure Preanalyze_Default_Expression (N : Node_Id; T : Entity_Id) is
18720 Save_In_Default_Expr : constant Boolean := In_Default_Expr;
18721 begin
18722 In_Default_Expr := True;
18723 Preanalyze_Spec_Expression (N, T);
18724 In_Default_Expr := Save_In_Default_Expr;
18725 end Preanalyze_Default_Expression;
18727 --------------------------------
18728 -- Preanalyze_Spec_Expression --
18729 --------------------------------
18731 procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
18732 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
18733 begin
18734 In_Spec_Expression := True;
18735 Preanalyze_And_Resolve (N, T);
18736 In_Spec_Expression := Save_In_Spec_Expression;
18737 end Preanalyze_Spec_Expression;
18739 ----------------------------------------
18740 -- Prepare_Private_Subtype_Completion --
18741 ----------------------------------------
18743 procedure Prepare_Private_Subtype_Completion
18744 (Id : Entity_Id;
18745 Related_Nod : Node_Id)
18747 Id_B : constant Entity_Id := Base_Type (Id);
18748 Full_B : Entity_Id := Full_View (Id_B);
18749 Full : Entity_Id;
18751 begin
18752 if Present (Full_B) then
18754 -- Get to the underlying full view if necessary
18756 if Is_Private_Type (Full_B)
18757 and then Present (Underlying_Full_View (Full_B))
18758 then
18759 Full_B := Underlying_Full_View (Full_B);
18760 end if;
18762 -- The Base_Type is already completed, we can complete the subtype
18763 -- now. We have to create a new entity with the same name, Thus we
18764 -- can't use Create_Itype.
18766 Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
18767 Set_Is_Itype (Full);
18768 Set_Associated_Node_For_Itype (Full, Related_Nod);
18769 Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
18770 end if;
18772 -- The parent subtype may be private, but the base might not, in some
18773 -- nested instances. In that case, the subtype does not need to be
18774 -- exchanged. It would still be nice to make private subtypes and their
18775 -- bases consistent at all times ???
18777 if Is_Private_Type (Id_B) then
18778 Append_Elmt (Id, Private_Dependents (Id_B));
18779 end if;
18780 end Prepare_Private_Subtype_Completion;
18782 ---------------------------
18783 -- Process_Discriminants --
18784 ---------------------------
18786 procedure Process_Discriminants
18787 (N : Node_Id;
18788 Prev : Entity_Id := Empty)
18790 Elist : constant Elist_Id := New_Elmt_List;
18791 Id : Node_Id;
18792 Discr : Node_Id;
18793 Discr_Number : Uint;
18794 Discr_Type : Entity_Id;
18795 Default_Present : Boolean := False;
18796 Default_Not_Present : Boolean := False;
18798 begin
18799 -- A composite type other than an array type can have discriminants.
18800 -- On entry, the current scope is the composite type.
18802 -- The discriminants are initially entered into the scope of the type
18803 -- via Enter_Name with the default Ekind of E_Void to prevent premature
18804 -- use, as explained at the end of this procedure.
18806 Discr := First (Discriminant_Specifications (N));
18807 while Present (Discr) loop
18808 Enter_Name (Defining_Identifier (Discr));
18810 -- For navigation purposes we add a reference to the discriminant
18811 -- in the entity for the type. If the current declaration is a
18812 -- completion, place references on the partial view. Otherwise the
18813 -- type is the current scope.
18815 if Present (Prev) then
18817 -- The references go on the partial view, if present. If the
18818 -- partial view has discriminants, the references have been
18819 -- generated already.
18821 if not Has_Discriminants (Prev) then
18822 Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
18823 end if;
18824 else
18825 Generate_Reference
18826 (Current_Scope, Defining_Identifier (Discr), 'd');
18827 end if;
18829 if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
18830 Discr_Type := Access_Definition (Discr, Discriminant_Type (Discr));
18832 -- Ada 2005 (AI-254)
18834 if Present (Access_To_Subprogram_Definition
18835 (Discriminant_Type (Discr)))
18836 and then Protected_Present (Access_To_Subprogram_Definition
18837 (Discriminant_Type (Discr)))
18838 then
18839 Discr_Type :=
18840 Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
18841 end if;
18843 else
18844 Find_Type (Discriminant_Type (Discr));
18845 Discr_Type := Etype (Discriminant_Type (Discr));
18847 if Error_Posted (Discriminant_Type (Discr)) then
18848 Discr_Type := Any_Type;
18849 end if;
18850 end if;
18852 -- Handling of discriminants that are access types
18854 if Is_Access_Type (Discr_Type) then
18856 -- Ada 2005 (AI-230): Access discriminant allowed in non-
18857 -- limited record types
18859 if Ada_Version < Ada_2005 then
18860 Check_Access_Discriminant_Requires_Limited
18861 (Discr, Discriminant_Type (Discr));
18862 end if;
18864 if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
18865 Error_Msg_N
18866 ("(Ada 83) access discriminant not allowed", Discr);
18867 end if;
18869 -- If not access type, must be a discrete type
18871 elsif not Is_Discrete_Type (Discr_Type) then
18872 Error_Msg_N
18873 ("discriminants must have a discrete or access type",
18874 Discriminant_Type (Discr));
18875 end if;
18877 Set_Etype (Defining_Identifier (Discr), Discr_Type);
18879 -- If a discriminant specification includes the assignment compound
18880 -- delimiter followed by an expression, the expression is the default
18881 -- expression of the discriminant; the default expression must be of
18882 -- the type of the discriminant. (RM 3.7.1) Since this expression is
18883 -- a default expression, we do the special preanalysis, since this
18884 -- expression does not freeze (see section "Handling of Default and
18885 -- Per-Object Expressions" in spec of package Sem).
18887 if Present (Expression (Discr)) then
18888 Preanalyze_Spec_Expression (Expression (Discr), Discr_Type);
18890 -- Legaity checks
18892 if Nkind (N) = N_Formal_Type_Declaration then
18893 Error_Msg_N
18894 ("discriminant defaults not allowed for formal type",
18895 Expression (Discr));
18897 -- Flag an error for a tagged type with defaulted discriminants,
18898 -- excluding limited tagged types when compiling for Ada 2012
18899 -- (see AI05-0214).
18901 elsif Is_Tagged_Type (Current_Scope)
18902 and then (not Is_Limited_Type (Current_Scope)
18903 or else Ada_Version < Ada_2012)
18904 and then Comes_From_Source (N)
18905 then
18906 -- Note: see similar test in Check_Or_Process_Discriminants, to
18907 -- handle the (illegal) case of the completion of an untagged
18908 -- view with discriminants with defaults by a tagged full view.
18909 -- We skip the check if Discr does not come from source, to
18910 -- account for the case of an untagged derived type providing
18911 -- defaults for a renamed discriminant from a private untagged
18912 -- ancestor with a tagged full view (ACATS B460006).
18914 if Ada_Version >= Ada_2012 then
18915 Error_Msg_N
18916 ("discriminants of nonlimited tagged type cannot have"
18917 & " defaults",
18918 Expression (Discr));
18919 else
18920 Error_Msg_N
18921 ("discriminants of tagged type cannot have defaults",
18922 Expression (Discr));
18923 end if;
18925 else
18926 Default_Present := True;
18927 Append_Elmt (Expression (Discr), Elist);
18929 -- Tag the defining identifiers for the discriminants with
18930 -- their corresponding default expressions from the tree.
18932 Set_Discriminant_Default_Value
18933 (Defining_Identifier (Discr), Expression (Discr));
18934 end if;
18936 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag
18937 -- gets set unless we can be sure that no range check is required.
18939 if (GNATprove_Mode or not Expander_Active)
18940 and then not
18941 Is_In_Range
18942 (Expression (Discr), Discr_Type, Assume_Valid => True)
18943 then
18944 Set_Do_Range_Check (Expression (Discr));
18945 end if;
18947 -- No default discriminant value given
18949 else
18950 Default_Not_Present := True;
18951 end if;
18953 -- Ada 2005 (AI-231): Create an Itype that is a duplicate of
18954 -- Discr_Type but with the null-exclusion attribute
18956 if Ada_Version >= Ada_2005 then
18958 -- Ada 2005 (AI-231): Static checks
18960 if Can_Never_Be_Null (Discr_Type) then
18961 Null_Exclusion_Static_Checks (Discr);
18963 elsif Is_Access_Type (Discr_Type)
18964 and then Null_Exclusion_Present (Discr)
18966 -- No need to check itypes because in their case this check
18967 -- was done at their point of creation
18969 and then not Is_Itype (Discr_Type)
18970 then
18971 if Can_Never_Be_Null (Discr_Type) then
18972 Error_Msg_NE
18973 ("`NOT NULL` not allowed (& already excludes null)",
18974 Discr,
18975 Discr_Type);
18976 end if;
18978 Set_Etype (Defining_Identifier (Discr),
18979 Create_Null_Excluding_Itype
18980 (T => Discr_Type,
18981 Related_Nod => Discr));
18983 -- Check for improper null exclusion if the type is otherwise
18984 -- legal for a discriminant.
18986 elsif Null_Exclusion_Present (Discr)
18987 and then Is_Discrete_Type (Discr_Type)
18988 then
18989 Error_Msg_N
18990 ("null exclusion can only apply to an access type", Discr);
18991 end if;
18993 -- Ada 2005 (AI-402): access discriminants of nonlimited types
18994 -- can't have defaults. Synchronized types, or types that are
18995 -- explicitly limited are fine, but special tests apply to derived
18996 -- types in generics: in a generic body we have to assume the
18997 -- worst, and therefore defaults are not allowed if the parent is
18998 -- a generic formal private type (see ACATS B370001).
19000 if Is_Access_Type (Discr_Type) and then Default_Present then
19001 if Ekind (Discr_Type) /= E_Anonymous_Access_Type
19002 or else Is_Limited_Record (Current_Scope)
19003 or else Is_Concurrent_Type (Current_Scope)
19004 or else Is_Concurrent_Record_Type (Current_Scope)
19005 or else Ekind (Current_Scope) = E_Limited_Private_Type
19006 then
19007 if not Is_Derived_Type (Current_Scope)
19008 or else not Is_Generic_Type (Etype (Current_Scope))
19009 or else not In_Package_Body (Scope (Etype (Current_Scope)))
19010 or else Limited_Present
19011 (Type_Definition (Parent (Current_Scope)))
19012 then
19013 null;
19015 else
19016 Error_Msg_N
19017 ("access discriminants of nonlimited types cannot "
19018 & "have defaults", Expression (Discr));
19019 end if;
19021 elsif Present (Expression (Discr)) then
19022 Error_Msg_N
19023 ("(Ada 2005) access discriminants of nonlimited types "
19024 & "cannot have defaults", Expression (Discr));
19025 end if;
19026 end if;
19027 end if;
19029 -- A discriminant cannot be effectively volatile. This check is only
19030 -- relevant when SPARK_Mode is on as it is not standard Ada legality
19031 -- rule (SPARK RM 7.1.3(6)).
19033 if SPARK_Mode = On
19034 and then Is_Effectively_Volatile (Defining_Identifier (Discr))
19035 then
19036 Error_Msg_N ("discriminant cannot be volatile", Discr);
19037 end if;
19039 Next (Discr);
19040 end loop;
19042 -- An element list consisting of the default expressions of the
19043 -- discriminants is constructed in the above loop and used to set
19044 -- the Discriminant_Constraint attribute for the type. If an object
19045 -- is declared of this (record or task) type without any explicit
19046 -- discriminant constraint given, this element list will form the
19047 -- actual parameters for the corresponding initialization procedure
19048 -- for the type.
19050 Set_Discriminant_Constraint (Current_Scope, Elist);
19051 Set_Stored_Constraint (Current_Scope, No_Elist);
19053 -- Default expressions must be provided either for all or for none
19054 -- of the discriminants of a discriminant part. (RM 3.7.1)
19056 if Default_Present and then Default_Not_Present then
19057 Error_Msg_N
19058 ("incomplete specification of defaults for discriminants", N);
19059 end if;
19061 -- The use of the name of a discriminant is not allowed in default
19062 -- expressions of a discriminant part if the specification of the
19063 -- discriminant is itself given in the discriminant part. (RM 3.7.1)
19065 -- To detect this, the discriminant names are entered initially with an
19066 -- Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
19067 -- attempt to use a void entity (for example in an expression that is
19068 -- type-checked) produces the error message: premature usage. Now after
19069 -- completing the semantic analysis of the discriminant part, we can set
19070 -- the Ekind of all the discriminants appropriately.
19072 Discr := First (Discriminant_Specifications (N));
19073 Discr_Number := Uint_1;
19074 while Present (Discr) loop
19075 Id := Defining_Identifier (Discr);
19076 Set_Ekind (Id, E_Discriminant);
19077 Init_Component_Location (Id);
19078 Init_Esize (Id);
19079 Set_Discriminant_Number (Id, Discr_Number);
19081 -- Make sure this is always set, even in illegal programs
19083 Set_Corresponding_Discriminant (Id, Empty);
19085 -- Initialize the Original_Record_Component to the entity itself.
19086 -- Inherit_Components will propagate the right value to
19087 -- discriminants in derived record types.
19089 Set_Original_Record_Component (Id, Id);
19091 -- Create the discriminal for the discriminant
19093 Build_Discriminal (Id);
19095 Next (Discr);
19096 Discr_Number := Discr_Number + 1;
19097 end loop;
19099 Set_Has_Discriminants (Current_Scope);
19100 end Process_Discriminants;
19102 -----------------------
19103 -- Process_Full_View --
19104 -----------------------
19106 procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
19107 procedure Collect_Implemented_Interfaces
19108 (Typ : Entity_Id;
19109 Ifaces : Elist_Id);
19110 -- Ada 2005: Gather all the interfaces that Typ directly or
19111 -- inherently implements. Duplicate entries are not added to
19112 -- the list Ifaces.
19114 ------------------------------------
19115 -- Collect_Implemented_Interfaces --
19116 ------------------------------------
19118 procedure Collect_Implemented_Interfaces
19119 (Typ : Entity_Id;
19120 Ifaces : Elist_Id)
19122 Iface : Entity_Id;
19123 Iface_Elmt : Elmt_Id;
19125 begin
19126 -- Abstract interfaces are only associated with tagged record types
19128 if not Is_Tagged_Type (Typ) or else not Is_Record_Type (Typ) then
19129 return;
19130 end if;
19132 -- Recursively climb to the ancestors
19134 if Etype (Typ) /= Typ
19136 -- Protect the frontend against wrong cyclic declarations like:
19138 -- type B is new A with private;
19139 -- type C is new A with private;
19140 -- private
19141 -- type B is new C with null record;
19142 -- type C is new B with null record;
19144 and then Etype (Typ) /= Priv_T
19145 and then Etype (Typ) /= Full_T
19146 then
19147 -- Keep separate the management of private type declarations
19149 if Ekind (Typ) = E_Record_Type_With_Private then
19151 -- Handle the following illegal usage:
19152 -- type Private_Type is tagged private;
19153 -- private
19154 -- type Private_Type is new Type_Implementing_Iface;
19156 if Present (Full_View (Typ))
19157 and then Etype (Typ) /= Full_View (Typ)
19158 then
19159 if Is_Interface (Etype (Typ)) then
19160 Append_Unique_Elmt (Etype (Typ), Ifaces);
19161 end if;
19163 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
19164 end if;
19166 -- Non-private types
19168 else
19169 if Is_Interface (Etype (Typ)) then
19170 Append_Unique_Elmt (Etype (Typ), Ifaces);
19171 end if;
19173 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
19174 end if;
19175 end if;
19177 -- Handle entities in the list of abstract interfaces
19179 if Present (Interfaces (Typ)) then
19180 Iface_Elmt := First_Elmt (Interfaces (Typ));
19181 while Present (Iface_Elmt) loop
19182 Iface := Node (Iface_Elmt);
19184 pragma Assert (Is_Interface (Iface));
19186 if not Contain_Interface (Iface, Ifaces) then
19187 Append_Elmt (Iface, Ifaces);
19188 Collect_Implemented_Interfaces (Iface, Ifaces);
19189 end if;
19191 Next_Elmt (Iface_Elmt);
19192 end loop;
19193 end if;
19194 end Collect_Implemented_Interfaces;
19196 -- Local variables
19198 Full_Indic : Node_Id;
19199 Full_Parent : Entity_Id;
19200 Priv_Parent : Entity_Id;
19202 -- Start of processing for Process_Full_View
19204 begin
19205 -- First some sanity checks that must be done after semantic
19206 -- decoration of the full view and thus cannot be placed with other
19207 -- similar checks in Find_Type_Name
19209 if not Is_Limited_Type (Priv_T)
19210 and then (Is_Limited_Type (Full_T)
19211 or else Is_Limited_Composite (Full_T))
19212 then
19213 if In_Instance then
19214 null;
19215 else
19216 Error_Msg_N
19217 ("completion of nonlimited type cannot be limited", Full_T);
19218 Explain_Limited_Type (Full_T, Full_T);
19219 end if;
19221 elsif Is_Abstract_Type (Full_T)
19222 and then not Is_Abstract_Type (Priv_T)
19223 then
19224 Error_Msg_N
19225 ("completion of nonabstract type cannot be abstract", Full_T);
19227 elsif Is_Tagged_Type (Priv_T)
19228 and then Is_Limited_Type (Priv_T)
19229 and then not Is_Limited_Type (Full_T)
19230 then
19231 -- If pragma CPP_Class was applied to the private declaration
19232 -- propagate the limitedness to the full-view
19234 if Is_CPP_Class (Priv_T) then
19235 Set_Is_Limited_Record (Full_T);
19237 -- GNAT allow its own definition of Limited_Controlled to disobey
19238 -- this rule in order in ease the implementation. This test is safe
19239 -- because Root_Controlled is defined in a child of System that
19240 -- normal programs are not supposed to use.
19242 elsif Is_RTE (Etype (Full_T), RE_Root_Controlled) then
19243 Set_Is_Limited_Composite (Full_T);
19244 else
19245 Error_Msg_N
19246 ("completion of limited tagged type must be limited", Full_T);
19247 end if;
19249 elsif Is_Generic_Type (Priv_T) then
19250 Error_Msg_N ("generic type cannot have a completion", Full_T);
19251 end if;
19253 -- Check that ancestor interfaces of private and full views are
19254 -- consistent. We omit this check for synchronized types because
19255 -- they are performed on the corresponding record type when frozen.
19257 if Ada_Version >= Ada_2005
19258 and then Is_Tagged_Type (Priv_T)
19259 and then Is_Tagged_Type (Full_T)
19260 and then not Is_Concurrent_Type (Full_T)
19261 then
19262 declare
19263 Iface : Entity_Id;
19264 Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
19265 Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
19267 begin
19268 Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
19269 Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
19271 -- Ada 2005 (AI-251): The partial view shall be a descendant of
19272 -- an interface type if and only if the full type is descendant
19273 -- of the interface type (AARM 7.3 (7.3/2)).
19275 Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
19277 if Present (Iface) then
19278 Error_Msg_NE
19279 ("interface in partial view& not implemented by full type "
19280 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
19281 end if;
19283 Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
19285 if Present (Iface) then
19286 Error_Msg_NE
19287 ("interface & not implemented by partial view "
19288 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
19289 end if;
19290 end;
19291 end if;
19293 if Is_Tagged_Type (Priv_T)
19294 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
19295 and then Is_Derived_Type (Full_T)
19296 then
19297 Priv_Parent := Etype (Priv_T);
19299 -- The full view of a private extension may have been transformed
19300 -- into an unconstrained derived type declaration and a subtype
19301 -- declaration (see build_derived_record_type for details).
19303 if Nkind (N) = N_Subtype_Declaration then
19304 Full_Indic := Subtype_Indication (N);
19305 Full_Parent := Etype (Base_Type (Full_T));
19306 else
19307 Full_Indic := Subtype_Indication (Type_Definition (N));
19308 Full_Parent := Etype (Full_T);
19309 end if;
19311 -- Check that the parent type of the full type is a descendant of
19312 -- the ancestor subtype given in the private extension. If either
19313 -- entity has an Etype equal to Any_Type then we had some previous
19314 -- error situation [7.3(8)].
19316 if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
19317 return;
19319 -- Ada 2005 (AI-251): Interfaces in the full type can be given in
19320 -- any order. Therefore we don't have to check that its parent must
19321 -- be a descendant of the parent of the private type declaration.
19323 elsif Is_Interface (Priv_Parent)
19324 and then Is_Interface (Full_Parent)
19325 then
19326 null;
19328 -- Ada 2005 (AI-251): If the parent of the private type declaration
19329 -- is an interface there is no need to check that it is an ancestor
19330 -- of the associated full type declaration. The required tests for
19331 -- this case are performed by Build_Derived_Record_Type.
19333 elsif not Is_Interface (Base_Type (Priv_Parent))
19334 and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
19335 then
19336 Error_Msg_N
19337 ("parent of full type must descend from parent"
19338 & " of private extension", Full_Indic);
19340 -- First check a formal restriction, and then proceed with checking
19341 -- Ada rules. Since the formal restriction is not a serious error, we
19342 -- don't prevent further error detection for this check, hence the
19343 -- ELSE.
19345 else
19346 -- In formal mode, when completing a private extension the type
19347 -- named in the private part must be exactly the same as that
19348 -- named in the visible part.
19350 if Priv_Parent /= Full_Parent then
19351 Error_Msg_Name_1 := Chars (Priv_Parent);
19352 Check_SPARK_05_Restriction ("% expected", Full_Indic);
19353 end if;
19355 -- Check the rules of 7.3(10): if the private extension inherits
19356 -- known discriminants, then the full type must also inherit those
19357 -- discriminants from the same (ancestor) type, and the parent
19358 -- subtype of the full type must be constrained if and only if
19359 -- the ancestor subtype of the private extension is constrained.
19361 if No (Discriminant_Specifications (Parent (Priv_T)))
19362 and then not Has_Unknown_Discriminants (Priv_T)
19363 and then Has_Discriminants (Base_Type (Priv_Parent))
19364 then
19365 declare
19366 Priv_Indic : constant Node_Id :=
19367 Subtype_Indication (Parent (Priv_T));
19369 Priv_Constr : constant Boolean :=
19370 Is_Constrained (Priv_Parent)
19371 or else
19372 Nkind (Priv_Indic) = N_Subtype_Indication
19373 or else
19374 Is_Constrained (Entity (Priv_Indic));
19376 Full_Constr : constant Boolean :=
19377 Is_Constrained (Full_Parent)
19378 or else
19379 Nkind (Full_Indic) = N_Subtype_Indication
19380 or else
19381 Is_Constrained (Entity (Full_Indic));
19383 Priv_Discr : Entity_Id;
19384 Full_Discr : Entity_Id;
19386 begin
19387 Priv_Discr := First_Discriminant (Priv_Parent);
19388 Full_Discr := First_Discriminant (Full_Parent);
19389 while Present (Priv_Discr) and then Present (Full_Discr) loop
19390 if Original_Record_Component (Priv_Discr) =
19391 Original_Record_Component (Full_Discr)
19392 or else
19393 Corresponding_Discriminant (Priv_Discr) =
19394 Corresponding_Discriminant (Full_Discr)
19395 then
19396 null;
19397 else
19398 exit;
19399 end if;
19401 Next_Discriminant (Priv_Discr);
19402 Next_Discriminant (Full_Discr);
19403 end loop;
19405 if Present (Priv_Discr) or else Present (Full_Discr) then
19406 Error_Msg_N
19407 ("full view must inherit discriminants of the parent"
19408 & " type used in the private extension", Full_Indic);
19410 elsif Priv_Constr and then not Full_Constr then
19411 Error_Msg_N
19412 ("parent subtype of full type must be constrained",
19413 Full_Indic);
19415 elsif Full_Constr and then not Priv_Constr then
19416 Error_Msg_N
19417 ("parent subtype of full type must be unconstrained",
19418 Full_Indic);
19419 end if;
19420 end;
19422 -- Check the rules of 7.3(12): if a partial view has neither
19423 -- known or unknown discriminants, then the full type
19424 -- declaration shall define a definite subtype.
19426 elsif not Has_Unknown_Discriminants (Priv_T)
19427 and then not Has_Discriminants (Priv_T)
19428 and then not Is_Constrained (Full_T)
19429 then
19430 Error_Msg_N
19431 ("full view must define a constrained type if partial view"
19432 & " has no discriminants", Full_T);
19433 end if;
19435 -- ??????? Do we implement the following properly ?????
19436 -- If the ancestor subtype of a private extension has constrained
19437 -- discriminants, then the parent subtype of the full view shall
19438 -- impose a statically matching constraint on those discriminants
19439 -- [7.3(13)].
19440 end if;
19442 else
19443 -- For untagged types, verify that a type without discriminants is
19444 -- not completed with an unconstrained type. A separate error message
19445 -- is produced if the full type has defaulted discriminants.
19447 if not Is_Indefinite_Subtype (Priv_T)
19448 and then Is_Indefinite_Subtype (Full_T)
19449 then
19450 Error_Msg_Sloc := Sloc (Parent (Priv_T));
19451 Error_Msg_NE
19452 ("full view of& not compatible with declaration#",
19453 Full_T, Priv_T);
19455 if not Is_Tagged_Type (Full_T) then
19456 Error_Msg_N
19457 ("\one is constrained, the other unconstrained", Full_T);
19458 end if;
19459 end if;
19460 end if;
19462 -- AI-419: verify that the use of "limited" is consistent
19464 declare
19465 Orig_Decl : constant Node_Id := Original_Node (N);
19467 begin
19468 if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
19469 and then Nkind (Orig_Decl) = N_Full_Type_Declaration
19470 and then Nkind
19471 (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
19472 then
19473 if not Limited_Present (Parent (Priv_T))
19474 and then not Synchronized_Present (Parent (Priv_T))
19475 and then Limited_Present (Type_Definition (Orig_Decl))
19476 then
19477 Error_Msg_N
19478 ("full view of non-limited extension cannot be limited", N);
19480 -- Conversely, if the partial view carries the limited keyword,
19481 -- the full view must as well, even if it may be redundant.
19483 elsif Limited_Present (Parent (Priv_T))
19484 and then not Limited_Present (Type_Definition (Orig_Decl))
19485 then
19486 Error_Msg_N
19487 ("full view of limited extension must be explicitly limited",
19489 end if;
19490 end if;
19491 end;
19493 -- Ada 2005 (AI-443): A synchronized private extension must be
19494 -- completed by a task or protected type.
19496 if Ada_Version >= Ada_2005
19497 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
19498 and then Synchronized_Present (Parent (Priv_T))
19499 and then not Is_Concurrent_Type (Full_T)
19500 then
19501 Error_Msg_N ("full view of synchronized extension must " &
19502 "be synchronized type", N);
19503 end if;
19505 -- Ada 2005 AI-363: if the full view has discriminants with
19506 -- defaults, it is illegal to declare constrained access subtypes
19507 -- whose designated type is the current type. This allows objects
19508 -- of the type that are declared in the heap to be unconstrained.
19510 if not Has_Unknown_Discriminants (Priv_T)
19511 and then not Has_Discriminants (Priv_T)
19512 and then Has_Discriminants (Full_T)
19513 and then
19514 Present (Discriminant_Default_Value (First_Discriminant (Full_T)))
19515 then
19516 Set_Has_Constrained_Partial_View (Full_T);
19517 Set_Has_Constrained_Partial_View (Priv_T);
19518 end if;
19520 -- Create a full declaration for all its subtypes recorded in
19521 -- Private_Dependents and swap them similarly to the base type. These
19522 -- are subtypes that have been define before the full declaration of
19523 -- the private type. We also swap the entry in Private_Dependents list
19524 -- so we can properly restore the private view on exit from the scope.
19526 declare
19527 Priv_Elmt : Elmt_Id;
19528 Priv_Scop : Entity_Id;
19529 Priv : Entity_Id;
19530 Full : Entity_Id;
19532 begin
19533 Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
19534 while Present (Priv_Elmt) loop
19535 Priv := Node (Priv_Elmt);
19536 Priv_Scop := Scope (Priv);
19538 if Ekind_In (Priv, E_Private_Subtype,
19539 E_Limited_Private_Subtype,
19540 E_Record_Subtype_With_Private)
19541 then
19542 Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
19543 Set_Is_Itype (Full);
19544 Set_Parent (Full, Parent (Priv));
19545 Set_Associated_Node_For_Itype (Full, N);
19547 -- Now we need to complete the private subtype, but since the
19548 -- base type has already been swapped, we must also swap the
19549 -- subtypes (and thus, reverse the arguments in the call to
19550 -- Complete_Private_Subtype). Also note that we may need to
19551 -- re-establish the scope of the private subtype.
19553 Copy_And_Swap (Priv, Full);
19555 if not In_Open_Scopes (Priv_Scop) then
19556 Push_Scope (Priv_Scop);
19558 else
19559 -- Reset Priv_Scop to Empty to indicate no scope was pushed
19561 Priv_Scop := Empty;
19562 end if;
19564 Complete_Private_Subtype (Full, Priv, Full_T, N);
19566 if Present (Priv_Scop) then
19567 Pop_Scope;
19568 end if;
19570 Replace_Elmt (Priv_Elmt, Full);
19571 end if;
19573 Next_Elmt (Priv_Elmt);
19574 end loop;
19575 end;
19577 -- If the private view was tagged, copy the new primitive operations
19578 -- from the private view to the full view.
19580 if Is_Tagged_Type (Full_T) then
19581 declare
19582 Disp_Typ : Entity_Id;
19583 Full_List : Elist_Id;
19584 Prim : Entity_Id;
19585 Prim_Elmt : Elmt_Id;
19586 Priv_List : Elist_Id;
19588 function Contains
19589 (E : Entity_Id;
19590 L : Elist_Id) return Boolean;
19591 -- Determine whether list L contains element E
19593 --------------
19594 -- Contains --
19595 --------------
19597 function Contains
19598 (E : Entity_Id;
19599 L : Elist_Id) return Boolean
19601 List_Elmt : Elmt_Id;
19603 begin
19604 List_Elmt := First_Elmt (L);
19605 while Present (List_Elmt) loop
19606 if Node (List_Elmt) = E then
19607 return True;
19608 end if;
19610 Next_Elmt (List_Elmt);
19611 end loop;
19613 return False;
19614 end Contains;
19616 -- Start of processing
19618 begin
19619 if Is_Tagged_Type (Priv_T) then
19620 Priv_List := Primitive_Operations (Priv_T);
19621 Prim_Elmt := First_Elmt (Priv_List);
19623 -- In the case of a concurrent type completing a private tagged
19624 -- type, primitives may have been declared in between the two
19625 -- views. These subprograms need to be wrapped the same way
19626 -- entries and protected procedures are handled because they
19627 -- cannot be directly shared by the two views.
19629 if Is_Concurrent_Type (Full_T) then
19630 declare
19631 Conc_Typ : constant Entity_Id :=
19632 Corresponding_Record_Type (Full_T);
19633 Curr_Nod : Node_Id := Parent (Conc_Typ);
19634 Wrap_Spec : Node_Id;
19636 begin
19637 while Present (Prim_Elmt) loop
19638 Prim := Node (Prim_Elmt);
19640 if Comes_From_Source (Prim)
19641 and then not Is_Abstract_Subprogram (Prim)
19642 then
19643 Wrap_Spec :=
19644 Make_Subprogram_Declaration (Sloc (Prim),
19645 Specification =>
19646 Build_Wrapper_Spec
19647 (Subp_Id => Prim,
19648 Obj_Typ => Conc_Typ,
19649 Formals =>
19650 Parameter_Specifications (
19651 Parent (Prim))));
19653 Insert_After (Curr_Nod, Wrap_Spec);
19654 Curr_Nod := Wrap_Spec;
19656 Analyze (Wrap_Spec);
19657 end if;
19659 Next_Elmt (Prim_Elmt);
19660 end loop;
19662 return;
19663 end;
19665 -- For non-concurrent types, transfer explicit primitives, but
19666 -- omit those inherited from the parent of the private view
19667 -- since they will be re-inherited later on.
19669 else
19670 Full_List := Primitive_Operations (Full_T);
19672 while Present (Prim_Elmt) loop
19673 Prim := Node (Prim_Elmt);
19675 if Comes_From_Source (Prim)
19676 and then not Contains (Prim, Full_List)
19677 then
19678 Append_Elmt (Prim, Full_List);
19679 end if;
19681 Next_Elmt (Prim_Elmt);
19682 end loop;
19683 end if;
19685 -- Untagged private view
19687 else
19688 Full_List := Primitive_Operations (Full_T);
19690 -- In this case the partial view is untagged, so here we locate
19691 -- all of the earlier primitives that need to be treated as
19692 -- dispatching (those that appear between the two views). Note
19693 -- that these additional operations must all be new operations
19694 -- (any earlier operations that override inherited operations
19695 -- of the full view will already have been inserted in the
19696 -- primitives list, marked by Check_Operation_From_Private_View
19697 -- as dispatching. Note that implicit "/=" operators are
19698 -- excluded from being added to the primitives list since they
19699 -- shouldn't be treated as dispatching (tagged "/=" is handled
19700 -- specially).
19702 Prim := Next_Entity (Full_T);
19703 while Present (Prim) and then Prim /= Priv_T loop
19704 if Ekind_In (Prim, E_Procedure, E_Function) then
19705 Disp_Typ := Find_Dispatching_Type (Prim);
19707 if Disp_Typ = Full_T
19708 and then (Chars (Prim) /= Name_Op_Ne
19709 or else Comes_From_Source (Prim))
19710 then
19711 Check_Controlling_Formals (Full_T, Prim);
19713 if not Is_Dispatching_Operation (Prim) then
19714 Append_Elmt (Prim, Full_List);
19715 Set_Is_Dispatching_Operation (Prim, True);
19716 Set_DT_Position_Value (Prim, No_Uint);
19717 end if;
19719 elsif Is_Dispatching_Operation (Prim)
19720 and then Disp_Typ /= Full_T
19721 then
19723 -- Verify that it is not otherwise controlled by a
19724 -- formal or a return value of type T.
19726 Check_Controlling_Formals (Disp_Typ, Prim);
19727 end if;
19728 end if;
19730 Next_Entity (Prim);
19731 end loop;
19732 end if;
19734 -- For the tagged case, the two views can share the same primitive
19735 -- operations list and the same class-wide type. Update attributes
19736 -- of the class-wide type which depend on the full declaration.
19738 if Is_Tagged_Type (Priv_T) then
19739 Set_Direct_Primitive_Operations (Priv_T, Full_List);
19740 Set_Class_Wide_Type
19741 (Base_Type (Full_T), Class_Wide_Type (Priv_T));
19743 Set_Has_Task (Class_Wide_Type (Priv_T), Has_Task (Full_T));
19744 Set_Has_Protected
19745 (Class_Wide_Type (Priv_T), Has_Protected (Full_T));
19746 end if;
19747 end;
19748 end if;
19750 -- Ada 2005 AI 161: Check preelaborable initialization consistency
19752 if Known_To_Have_Preelab_Init (Priv_T) then
19754 -- Case where there is a pragma Preelaborable_Initialization. We
19755 -- always allow this in predefined units, which is cheating a bit,
19756 -- but it means we don't have to struggle to meet the requirements in
19757 -- the RM for having Preelaborable Initialization. Otherwise we
19758 -- require that the type meets the RM rules. But we can't check that
19759 -- yet, because of the rule about overriding Initialize, so we simply
19760 -- set a flag that will be checked at freeze time.
19762 if not In_Predefined_Unit (Full_T) then
19763 Set_Must_Have_Preelab_Init (Full_T);
19764 end if;
19765 end if;
19767 -- If pragma CPP_Class was applied to the private type declaration,
19768 -- propagate it now to the full type declaration.
19770 if Is_CPP_Class (Priv_T) then
19771 Set_Is_CPP_Class (Full_T);
19772 Set_Convention (Full_T, Convention_CPP);
19774 -- Check that components of imported CPP types do not have default
19775 -- expressions.
19777 Check_CPP_Type_Has_No_Defaults (Full_T);
19778 end if;
19780 -- If the private view has user specified stream attributes, then so has
19781 -- the full view.
19783 -- Why the test, how could these flags be already set in Full_T ???
19785 if Has_Specified_Stream_Read (Priv_T) then
19786 Set_Has_Specified_Stream_Read (Full_T);
19787 end if;
19789 if Has_Specified_Stream_Write (Priv_T) then
19790 Set_Has_Specified_Stream_Write (Full_T);
19791 end if;
19793 if Has_Specified_Stream_Input (Priv_T) then
19794 Set_Has_Specified_Stream_Input (Full_T);
19795 end if;
19797 if Has_Specified_Stream_Output (Priv_T) then
19798 Set_Has_Specified_Stream_Output (Full_T);
19799 end if;
19801 -- Propagate the attributes related to pragma Default_Initial_Condition
19802 -- from the private to the full view. Note that both flags are mutually
19803 -- exclusive.
19805 if Has_Default_Init_Cond (Priv_T)
19806 or else Has_Inherited_Default_Init_Cond (Priv_T)
19807 then
19808 Propagate_Default_Init_Cond_Attributes
19809 (From_Typ => Priv_T,
19810 To_Typ => Full_T,
19811 Private_To_Full_View => True);
19813 -- In the case where the full view is derived from another private type,
19814 -- the attributes related to pragma Default_Initial_Condition must be
19815 -- propagated from the full to the private view to maintain consistency
19816 -- of views.
19818 -- package Pack is
19819 -- type Parent_Typ is private
19820 -- with Default_Initial_Condition ...;
19821 -- private
19822 -- type Parent_Typ is ...;
19823 -- end Pack;
19825 -- with Pack; use Pack;
19826 -- package Pack_2 is
19827 -- type Deriv_Typ is private; -- must inherit
19828 -- private
19829 -- type Deriv_Typ is new Parent_Typ; -- must inherit
19830 -- end Pack_2;
19832 elsif Has_Default_Init_Cond (Full_T)
19833 or else Has_Inherited_Default_Init_Cond (Full_T)
19834 then
19835 Propagate_Default_Init_Cond_Attributes
19836 (From_Typ => Full_T,
19837 To_Typ => Priv_T,
19838 Private_To_Full_View => True);
19839 end if;
19841 -- Propagate the attributes related to pragma Ghost from the private to
19842 -- the full view.
19844 if Is_Ghost_Entity (Priv_T) then
19845 Set_Is_Ghost_Entity (Full_T);
19847 -- The Ghost policy in effect at the point of declaration and at the
19848 -- point of completion must match (SPARK RM 6.9(15)).
19850 Check_Ghost_Completion (Priv_T, Full_T);
19852 -- In the case where the private view of a tagged type lacks a parent
19853 -- type and is subject to pragma Ghost, ensure that the parent type
19854 -- specified by the full view is also Ghost (SPARK RM 6.9(9)).
19856 if Is_Derived_Type (Full_T) then
19857 Check_Ghost_Derivation (Full_T);
19858 end if;
19859 end if;
19861 -- Propagate invariants to full type
19863 if Has_Invariants (Priv_T) then
19864 Set_Has_Invariants (Full_T);
19865 Set_Invariant_Procedure (Full_T, Invariant_Procedure (Priv_T));
19866 end if;
19868 if Has_Inheritable_Invariants (Priv_T) then
19869 Set_Has_Inheritable_Invariants (Full_T);
19870 end if;
19872 -- Check hidden inheritance of class-wide type invariants
19874 if Ada_Version >= Ada_2012
19875 and then not Has_Inheritable_Invariants (Full_T)
19876 and then In_Private_Part (Current_Scope)
19877 and then Has_Interfaces (Full_T)
19878 then
19879 declare
19880 Ifaces : Elist_Id;
19881 AI : Elmt_Id;
19883 begin
19884 Collect_Interfaces (Full_T, Ifaces, Exclude_Parents => True);
19886 AI := First_Elmt (Ifaces);
19887 while Present (AI) loop
19888 if Has_Inheritable_Invariants (Node (AI)) then
19889 Error_Msg_N
19890 ("hidden inheritance of class-wide type invariants " &
19891 "not allowed", N);
19892 exit;
19893 end if;
19895 Next_Elmt (AI);
19896 end loop;
19897 end;
19898 end if;
19900 -- Propagate predicates to full type, and predicate function if already
19901 -- defined. It is not clear that this can actually happen? the partial
19902 -- view cannot be frozen yet, and the predicate function has not been
19903 -- built. Still it is a cheap check and seems safer to make it.
19905 if Has_Predicates (Priv_T) then
19906 if Present (Predicate_Function (Priv_T)) then
19907 Set_Predicate_Function (Full_T, Predicate_Function (Priv_T));
19908 end if;
19910 Set_Has_Predicates (Full_T);
19911 end if;
19912 end Process_Full_View;
19914 -----------------------------------
19915 -- Process_Incomplete_Dependents --
19916 -----------------------------------
19918 procedure Process_Incomplete_Dependents
19919 (N : Node_Id;
19920 Full_T : Entity_Id;
19921 Inc_T : Entity_Id)
19923 Inc_Elmt : Elmt_Id;
19924 Priv_Dep : Entity_Id;
19925 New_Subt : Entity_Id;
19927 Disc_Constraint : Elist_Id;
19929 begin
19930 if No (Private_Dependents (Inc_T)) then
19931 return;
19932 end if;
19934 -- Itypes that may be generated by the completion of an incomplete
19935 -- subtype are not used by the back-end and not attached to the tree.
19936 -- They are created only for constraint-checking purposes.
19938 Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
19939 while Present (Inc_Elmt) loop
19940 Priv_Dep := Node (Inc_Elmt);
19942 if Ekind (Priv_Dep) = E_Subprogram_Type then
19944 -- An Access_To_Subprogram type may have a return type or a
19945 -- parameter type that is incomplete. Replace with the full view.
19947 if Etype (Priv_Dep) = Inc_T then
19948 Set_Etype (Priv_Dep, Full_T);
19949 end if;
19951 declare
19952 Formal : Entity_Id;
19954 begin
19955 Formal := First_Formal (Priv_Dep);
19956 while Present (Formal) loop
19957 if Etype (Formal) = Inc_T then
19958 Set_Etype (Formal, Full_T);
19959 end if;
19961 Next_Formal (Formal);
19962 end loop;
19963 end;
19965 elsif Is_Overloadable (Priv_Dep) then
19967 -- If a subprogram in the incomplete dependents list is primitive
19968 -- for a tagged full type then mark it as a dispatching operation,
19969 -- check whether it overrides an inherited subprogram, and check
19970 -- restrictions on its controlling formals. Note that a protected
19971 -- operation is never dispatching: only its wrapper operation
19972 -- (which has convention Ada) is.
19974 if Is_Tagged_Type (Full_T)
19975 and then Is_Primitive (Priv_Dep)
19976 and then Convention (Priv_Dep) /= Convention_Protected
19977 then
19978 Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
19979 Set_Is_Dispatching_Operation (Priv_Dep);
19980 Check_Controlling_Formals (Full_T, Priv_Dep);
19981 end if;
19983 elsif Ekind (Priv_Dep) = E_Subprogram_Body then
19985 -- Can happen during processing of a body before the completion
19986 -- of a TA type. Ignore, because spec is also on dependent list.
19988 return;
19990 -- Ada 2005 (AI-412): Transform a regular incomplete subtype into a
19991 -- corresponding subtype of the full view.
19993 elsif Ekind (Priv_Dep) = E_Incomplete_Subtype then
19994 Set_Subtype_Indication
19995 (Parent (Priv_Dep), New_Occurrence_Of (Full_T, Sloc (Priv_Dep)));
19996 Set_Etype (Priv_Dep, Full_T);
19997 Set_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
19998 Set_Analyzed (Parent (Priv_Dep), False);
20000 -- Reanalyze the declaration, suppressing the call to
20001 -- Enter_Name to avoid duplicate names.
20003 Analyze_Subtype_Declaration
20004 (N => Parent (Priv_Dep),
20005 Skip => True);
20007 -- Dependent is a subtype
20009 else
20010 -- We build a new subtype indication using the full view of the
20011 -- incomplete parent. The discriminant constraints have been
20012 -- elaborated already at the point of the subtype declaration.
20014 New_Subt := Create_Itype (E_Void, N);
20016 if Has_Discriminants (Full_T) then
20017 Disc_Constraint := Discriminant_Constraint (Priv_Dep);
20018 else
20019 Disc_Constraint := No_Elist;
20020 end if;
20022 Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
20023 Set_Full_View (Priv_Dep, New_Subt);
20024 end if;
20026 Next_Elmt (Inc_Elmt);
20027 end loop;
20028 end Process_Incomplete_Dependents;
20030 --------------------------------
20031 -- Process_Range_Expr_In_Decl --
20032 --------------------------------
20034 procedure Process_Range_Expr_In_Decl
20035 (R : Node_Id;
20036 T : Entity_Id;
20037 Subtyp : Entity_Id := Empty;
20038 Check_List : List_Id := Empty_List;
20039 R_Check_Off : Boolean := False;
20040 In_Iter_Schm : Boolean := False)
20042 Lo, Hi : Node_Id;
20043 R_Checks : Check_Result;
20044 Insert_Node : Node_Id;
20045 Def_Id : Entity_Id;
20047 begin
20048 Analyze_And_Resolve (R, Base_Type (T));
20050 if Nkind (R) = N_Range then
20052 -- In SPARK, all ranges should be static, with the exception of the
20053 -- discrete type definition of a loop parameter specification.
20055 if not In_Iter_Schm
20056 and then not Is_OK_Static_Range (R)
20057 then
20058 Check_SPARK_05_Restriction ("range should be static", R);
20059 end if;
20061 Lo := Low_Bound (R);
20062 Hi := High_Bound (R);
20064 -- Validity checks on the range of a quantified expression are
20065 -- delayed until the construct is transformed into a loop.
20067 if Nkind (Parent (R)) = N_Loop_Parameter_Specification
20068 and then Nkind (Parent (Parent (R))) = N_Quantified_Expression
20069 then
20070 null;
20072 -- We need to ensure validity of the bounds here, because if we
20073 -- go ahead and do the expansion, then the expanded code will get
20074 -- analyzed with range checks suppressed and we miss the check.
20076 -- WARNING: The capture of the range bounds with xxx_FIRST/_LAST and
20077 -- the temporaries generated by routine Remove_Side_Effects by means
20078 -- of validity checks must use the same names. When a range appears
20079 -- in the parent of a generic, the range is processed with checks
20080 -- disabled as part of the generic context and with checks enabled
20081 -- for code generation purposes. This leads to link issues as the
20082 -- generic contains references to xxx_FIRST/_LAST, but the inlined
20083 -- template sees the temporaries generated by Remove_Side_Effects.
20085 else
20086 Validity_Check_Range (R, Subtyp);
20087 end if;
20089 -- If there were errors in the declaration, try and patch up some
20090 -- common mistakes in the bounds. The cases handled are literals
20091 -- which are Integer where the expected type is Real and vice versa.
20092 -- These corrections allow the compilation process to proceed further
20093 -- along since some basic assumptions of the format of the bounds
20094 -- are guaranteed.
20096 if Etype (R) = Any_Type then
20097 if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
20098 Rewrite (Lo,
20099 Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
20101 elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
20102 Rewrite (Hi,
20103 Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
20105 elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
20106 Rewrite (Lo,
20107 Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
20109 elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
20110 Rewrite (Hi,
20111 Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
20112 end if;
20114 Set_Etype (Lo, T);
20115 Set_Etype (Hi, T);
20116 end if;
20118 -- If the bounds of the range have been mistakenly given as string
20119 -- literals (perhaps in place of character literals), then an error
20120 -- has already been reported, but we rewrite the string literal as a
20121 -- bound of the range's type to avoid blowups in later processing
20122 -- that looks at static values.
20124 if Nkind (Lo) = N_String_Literal then
20125 Rewrite (Lo,
20126 Make_Attribute_Reference (Sloc (Lo),
20127 Prefix => New_Occurrence_Of (T, Sloc (Lo)),
20128 Attribute_Name => Name_First));
20129 Analyze_And_Resolve (Lo);
20130 end if;
20132 if Nkind (Hi) = N_String_Literal then
20133 Rewrite (Hi,
20134 Make_Attribute_Reference (Sloc (Hi),
20135 Prefix => New_Occurrence_Of (T, Sloc (Hi)),
20136 Attribute_Name => Name_First));
20137 Analyze_And_Resolve (Hi);
20138 end if;
20140 -- If bounds aren't scalar at this point then exit, avoiding
20141 -- problems with further processing of the range in this procedure.
20143 if not Is_Scalar_Type (Etype (Lo)) then
20144 return;
20145 end if;
20147 -- Resolve (actually Sem_Eval) has checked that the bounds are in
20148 -- then range of the base type. Here we check whether the bounds
20149 -- are in the range of the subtype itself. Note that if the bounds
20150 -- represent the null range the Constraint_Error exception should
20151 -- not be raised.
20153 -- ??? The following code should be cleaned up as follows
20155 -- 1. The Is_Null_Range (Lo, Hi) test should disappear since it
20156 -- is done in the call to Range_Check (R, T); below
20158 -- 2. The use of R_Check_Off should be investigated and possibly
20159 -- removed, this would clean up things a bit.
20161 if Is_Null_Range (Lo, Hi) then
20162 null;
20164 else
20165 -- Capture values of bounds and generate temporaries for them
20166 -- if needed, before applying checks, since checks may cause
20167 -- duplication of the expression without forcing evaluation.
20169 -- The forced evaluation removes side effects from expressions,
20170 -- which should occur also in GNATprove mode. Otherwise, we end up
20171 -- with unexpected insertions of actions at places where this is
20172 -- not supposed to occur, e.g. on default parameters of a call.
20174 if Expander_Active or GNATprove_Mode then
20176 -- Call Force_Evaluation to create declarations as needed to
20177 -- deal with side effects, and also create typ_FIRST/LAST
20178 -- entities for bounds if we have a subtype name.
20180 -- Note: we do this transformation even if expansion is not
20181 -- active if we are in GNATprove_Mode since the transformation
20182 -- is in general required to ensure that the resulting tree has
20183 -- proper Ada semantics.
20185 Force_Evaluation
20186 (Lo, Related_Id => Subtyp, Is_Low_Bound => True);
20187 Force_Evaluation
20188 (Hi, Related_Id => Subtyp, Is_High_Bound => True);
20189 end if;
20191 -- We use a flag here instead of suppressing checks on the
20192 -- type because the type we check against isn't necessarily
20193 -- the place where we put the check.
20195 if not R_Check_Off then
20196 R_Checks := Get_Range_Checks (R, T);
20198 -- Look up tree to find an appropriate insertion point. We
20199 -- can't just use insert_actions because later processing
20200 -- depends on the insertion node. Prior to Ada 2012 the
20201 -- insertion point could only be a declaration or a loop, but
20202 -- quantified expressions can appear within any context in an
20203 -- expression, and the insertion point can be any statement,
20204 -- pragma, or declaration.
20206 Insert_Node := Parent (R);
20207 while Present (Insert_Node) loop
20208 exit when
20209 Nkind (Insert_Node) in N_Declaration
20210 and then
20211 not Nkind_In
20212 (Insert_Node, N_Component_Declaration,
20213 N_Loop_Parameter_Specification,
20214 N_Function_Specification,
20215 N_Procedure_Specification);
20217 exit when Nkind (Insert_Node) in N_Later_Decl_Item
20218 or else Nkind (Insert_Node) in
20219 N_Statement_Other_Than_Procedure_Call
20220 or else Nkind_In (Insert_Node, N_Procedure_Call_Statement,
20221 N_Pragma);
20223 Insert_Node := Parent (Insert_Node);
20224 end loop;
20226 -- Why would Type_Decl not be present??? Without this test,
20227 -- short regression tests fail.
20229 if Present (Insert_Node) then
20231 -- Case of loop statement. Verify that the range is part
20232 -- of the subtype indication of the iteration scheme.
20234 if Nkind (Insert_Node) = N_Loop_Statement then
20235 declare
20236 Indic : Node_Id;
20238 begin
20239 Indic := Parent (R);
20240 while Present (Indic)
20241 and then Nkind (Indic) /= N_Subtype_Indication
20242 loop
20243 Indic := Parent (Indic);
20244 end loop;
20246 if Present (Indic) then
20247 Def_Id := Etype (Subtype_Mark (Indic));
20249 Insert_Range_Checks
20250 (R_Checks,
20251 Insert_Node,
20252 Def_Id,
20253 Sloc (Insert_Node),
20255 Do_Before => True);
20256 end if;
20257 end;
20259 -- Insertion before a declaration. If the declaration
20260 -- includes discriminants, the list of applicable checks
20261 -- is given by the caller.
20263 elsif Nkind (Insert_Node) in N_Declaration then
20264 Def_Id := Defining_Identifier (Insert_Node);
20266 if (Ekind (Def_Id) = E_Record_Type
20267 and then Depends_On_Discriminant (R))
20268 or else
20269 (Ekind (Def_Id) = E_Protected_Type
20270 and then Has_Discriminants (Def_Id))
20271 then
20272 Append_Range_Checks
20273 (R_Checks,
20274 Check_List, Def_Id, Sloc (Insert_Node), R);
20276 else
20277 Insert_Range_Checks
20278 (R_Checks,
20279 Insert_Node, Def_Id, Sloc (Insert_Node), R);
20281 end if;
20283 -- Insertion before a statement. Range appears in the
20284 -- context of a quantified expression. Insertion will
20285 -- take place when expression is expanded.
20287 else
20288 null;
20289 end if;
20290 end if;
20291 end if;
20292 end if;
20294 -- Case of other than an explicit N_Range node
20296 -- The forced evaluation removes side effects from expressions, which
20297 -- should occur also in GNATprove mode. Otherwise, we end up with
20298 -- unexpected insertions of actions at places where this is not
20299 -- supposed to occur, e.g. on default parameters of a call.
20301 elsif Expander_Active or GNATprove_Mode then
20302 Get_Index_Bounds (R, Lo, Hi);
20303 Force_Evaluation (Lo);
20304 Force_Evaluation (Hi);
20305 end if;
20306 end Process_Range_Expr_In_Decl;
20308 --------------------------------------
20309 -- Process_Real_Range_Specification --
20310 --------------------------------------
20312 procedure Process_Real_Range_Specification (Def : Node_Id) is
20313 Spec : constant Node_Id := Real_Range_Specification (Def);
20314 Lo : Node_Id;
20315 Hi : Node_Id;
20316 Err : Boolean := False;
20318 procedure Analyze_Bound (N : Node_Id);
20319 -- Analyze and check one bound
20321 -------------------
20322 -- Analyze_Bound --
20323 -------------------
20325 procedure Analyze_Bound (N : Node_Id) is
20326 begin
20327 Analyze_And_Resolve (N, Any_Real);
20329 if not Is_OK_Static_Expression (N) then
20330 Flag_Non_Static_Expr
20331 ("bound in real type definition is not static!", N);
20332 Err := True;
20333 end if;
20334 end Analyze_Bound;
20336 -- Start of processing for Process_Real_Range_Specification
20338 begin
20339 if Present (Spec) then
20340 Lo := Low_Bound (Spec);
20341 Hi := High_Bound (Spec);
20342 Analyze_Bound (Lo);
20343 Analyze_Bound (Hi);
20345 -- If error, clear away junk range specification
20347 if Err then
20348 Set_Real_Range_Specification (Def, Empty);
20349 end if;
20350 end if;
20351 end Process_Real_Range_Specification;
20353 ---------------------
20354 -- Process_Subtype --
20355 ---------------------
20357 function Process_Subtype
20358 (S : Node_Id;
20359 Related_Nod : Node_Id;
20360 Related_Id : Entity_Id := Empty;
20361 Suffix : Character := ' ') return Entity_Id
20363 P : Node_Id;
20364 Def_Id : Entity_Id;
20365 Error_Node : Node_Id;
20366 Full_View_Id : Entity_Id;
20367 Subtype_Mark_Id : Entity_Id;
20369 May_Have_Null_Exclusion : Boolean;
20371 procedure Check_Incomplete (T : Entity_Id);
20372 -- Called to verify that an incomplete type is not used prematurely
20374 ----------------------
20375 -- Check_Incomplete --
20376 ----------------------
20378 procedure Check_Incomplete (T : Entity_Id) is
20379 begin
20380 -- Ada 2005 (AI-412): Incomplete subtypes are legal
20382 if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
20383 and then
20384 not (Ada_Version >= Ada_2005
20385 and then
20386 (Nkind (Parent (T)) = N_Subtype_Declaration
20387 or else (Nkind (Parent (T)) = N_Subtype_Indication
20388 and then Nkind (Parent (Parent (T))) =
20389 N_Subtype_Declaration)))
20390 then
20391 Error_Msg_N ("invalid use of type before its full declaration", T);
20392 end if;
20393 end Check_Incomplete;
20395 -- Start of processing for Process_Subtype
20397 begin
20398 -- Case of no constraints present
20400 if Nkind (S) /= N_Subtype_Indication then
20401 Find_Type (S);
20402 Check_Incomplete (S);
20403 P := Parent (S);
20405 -- Ada 2005 (AI-231): Static check
20407 if Ada_Version >= Ada_2005
20408 and then Present (P)
20409 and then Null_Exclusion_Present (P)
20410 and then Nkind (P) /= N_Access_To_Object_Definition
20411 and then not Is_Access_Type (Entity (S))
20412 then
20413 Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
20414 end if;
20416 -- The following is ugly, can't we have a range or even a flag???
20418 May_Have_Null_Exclusion :=
20419 Nkind_In (P, N_Access_Definition,
20420 N_Access_Function_Definition,
20421 N_Access_Procedure_Definition,
20422 N_Access_To_Object_Definition,
20423 N_Allocator,
20424 N_Component_Definition)
20425 or else
20426 Nkind_In (P, N_Derived_Type_Definition,
20427 N_Discriminant_Specification,
20428 N_Formal_Object_Declaration,
20429 N_Object_Declaration,
20430 N_Object_Renaming_Declaration,
20431 N_Parameter_Specification,
20432 N_Subtype_Declaration);
20434 -- Create an Itype that is a duplicate of Entity (S) but with the
20435 -- null-exclusion attribute.
20437 if May_Have_Null_Exclusion
20438 and then Is_Access_Type (Entity (S))
20439 and then Null_Exclusion_Present (P)
20441 -- No need to check the case of an access to object definition.
20442 -- It is correct to define double not-null pointers.
20444 -- Example:
20445 -- type Not_Null_Int_Ptr is not null access Integer;
20446 -- type Acc is not null access Not_Null_Int_Ptr;
20448 and then Nkind (P) /= N_Access_To_Object_Definition
20449 then
20450 if Can_Never_Be_Null (Entity (S)) then
20451 case Nkind (Related_Nod) is
20452 when N_Full_Type_Declaration =>
20453 if Nkind (Type_Definition (Related_Nod))
20454 in N_Array_Type_Definition
20455 then
20456 Error_Node :=
20457 Subtype_Indication
20458 (Component_Definition
20459 (Type_Definition (Related_Nod)));
20460 else
20461 Error_Node :=
20462 Subtype_Indication (Type_Definition (Related_Nod));
20463 end if;
20465 when N_Subtype_Declaration =>
20466 Error_Node := Subtype_Indication (Related_Nod);
20468 when N_Object_Declaration =>
20469 Error_Node := Object_Definition (Related_Nod);
20471 when N_Component_Declaration =>
20472 Error_Node :=
20473 Subtype_Indication (Component_Definition (Related_Nod));
20475 when N_Allocator =>
20476 Error_Node := Expression (Related_Nod);
20478 when others =>
20479 pragma Assert (False);
20480 Error_Node := Related_Nod;
20481 end case;
20483 Error_Msg_NE
20484 ("`NOT NULL` not allowed (& already excludes null)",
20485 Error_Node,
20486 Entity (S));
20487 end if;
20489 Set_Etype (S,
20490 Create_Null_Excluding_Itype
20491 (T => Entity (S),
20492 Related_Nod => P));
20493 Set_Entity (S, Etype (S));
20494 end if;
20496 return Entity (S);
20498 -- Case of constraint present, so that we have an N_Subtype_Indication
20499 -- node (this node is created only if constraints are present).
20501 else
20502 Find_Type (Subtype_Mark (S));
20504 if Nkind (Parent (S)) /= N_Access_To_Object_Definition
20505 and then not
20506 (Nkind (Parent (S)) = N_Subtype_Declaration
20507 and then Is_Itype (Defining_Identifier (Parent (S))))
20508 then
20509 Check_Incomplete (Subtype_Mark (S));
20510 end if;
20512 P := Parent (S);
20513 Subtype_Mark_Id := Entity (Subtype_Mark (S));
20515 -- Explicit subtype declaration case
20517 if Nkind (P) = N_Subtype_Declaration then
20518 Def_Id := Defining_Identifier (P);
20520 -- Explicit derived type definition case
20522 elsif Nkind (P) = N_Derived_Type_Definition then
20523 Def_Id := Defining_Identifier (Parent (P));
20525 -- Implicit case, the Def_Id must be created as an implicit type.
20526 -- The one exception arises in the case of concurrent types, array
20527 -- and access types, where other subsidiary implicit types may be
20528 -- created and must appear before the main implicit type. In these
20529 -- cases we leave Def_Id set to Empty as a signal that Create_Itype
20530 -- has not yet been called to create Def_Id.
20532 else
20533 if Is_Array_Type (Subtype_Mark_Id)
20534 or else Is_Concurrent_Type (Subtype_Mark_Id)
20535 or else Is_Access_Type (Subtype_Mark_Id)
20536 then
20537 Def_Id := Empty;
20539 -- For the other cases, we create a new unattached Itype,
20540 -- and set the indication to ensure it gets attached later.
20542 else
20543 Def_Id :=
20544 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
20545 end if;
20546 end if;
20548 -- If the kind of constraint is invalid for this kind of type,
20549 -- then give an error, and then pretend no constraint was given.
20551 if not Is_Valid_Constraint_Kind
20552 (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
20553 then
20554 Error_Msg_N
20555 ("incorrect constraint for this kind of type", Constraint (S));
20557 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
20559 -- Set Ekind of orphan itype, to prevent cascaded errors
20561 if Present (Def_Id) then
20562 Set_Ekind (Def_Id, Ekind (Any_Type));
20563 end if;
20565 -- Make recursive call, having got rid of the bogus constraint
20567 return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
20568 end if;
20570 -- Remaining processing depends on type. Select on Base_Type kind to
20571 -- ensure getting to the concrete type kind in the case of a private
20572 -- subtype (needed when only doing semantic analysis).
20574 case Ekind (Base_Type (Subtype_Mark_Id)) is
20575 when Access_Kind =>
20577 -- If this is a constraint on a class-wide type, discard it.
20578 -- There is currently no way to express a partial discriminant
20579 -- constraint on a type with unknown discriminants. This is
20580 -- a pathology that the ACATS wisely decides not to test.
20582 if Is_Class_Wide_Type (Designated_Type (Subtype_Mark_Id)) then
20583 if Comes_From_Source (S) then
20584 Error_Msg_N
20585 ("constraint on class-wide type ignored??",
20586 Constraint (S));
20587 end if;
20589 if Nkind (P) = N_Subtype_Declaration then
20590 Set_Subtype_Indication (P,
20591 New_Occurrence_Of (Subtype_Mark_Id, Sloc (S)));
20592 end if;
20594 return Subtype_Mark_Id;
20595 end if;
20597 Constrain_Access (Def_Id, S, Related_Nod);
20599 if Expander_Active
20600 and then Is_Itype (Designated_Type (Def_Id))
20601 and then Nkind (Related_Nod) = N_Subtype_Declaration
20602 and then not Is_Incomplete_Type (Designated_Type (Def_Id))
20603 then
20604 Build_Itype_Reference
20605 (Designated_Type (Def_Id), Related_Nod);
20606 end if;
20608 when Array_Kind =>
20609 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
20611 when Decimal_Fixed_Point_Kind =>
20612 Constrain_Decimal (Def_Id, S);
20614 when Enumeration_Kind =>
20615 Constrain_Enumeration (Def_Id, S);
20616 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
20618 when Ordinary_Fixed_Point_Kind =>
20619 Constrain_Ordinary_Fixed (Def_Id, S);
20621 when Float_Kind =>
20622 Constrain_Float (Def_Id, S);
20624 when Integer_Kind =>
20625 Constrain_Integer (Def_Id, S);
20626 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
20628 when E_Record_Type |
20629 E_Record_Subtype |
20630 Class_Wide_Kind |
20631 E_Incomplete_Type =>
20632 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
20634 if Ekind (Def_Id) = E_Incomplete_Type then
20635 Set_Private_Dependents (Def_Id, New_Elmt_List);
20636 end if;
20638 when Private_Kind =>
20639 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
20640 Set_Private_Dependents (Def_Id, New_Elmt_List);
20642 -- In case of an invalid constraint prevent further processing
20643 -- since the type constructed is missing expected fields.
20645 if Etype (Def_Id) = Any_Type then
20646 return Def_Id;
20647 end if;
20649 -- If the full view is that of a task with discriminants,
20650 -- we must constrain both the concurrent type and its
20651 -- corresponding record type. Otherwise we will just propagate
20652 -- the constraint to the full view, if available.
20654 if Present (Full_View (Subtype_Mark_Id))
20655 and then Has_Discriminants (Subtype_Mark_Id)
20656 and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
20657 then
20658 Full_View_Id :=
20659 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
20661 Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
20662 Constrain_Concurrent (Full_View_Id, S,
20663 Related_Nod, Related_Id, Suffix);
20664 Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
20665 Set_Full_View (Def_Id, Full_View_Id);
20667 -- Introduce an explicit reference to the private subtype,
20668 -- to prevent scope anomalies in gigi if first use appears
20669 -- in a nested context, e.g. a later function body.
20670 -- Should this be generated in other contexts than a full
20671 -- type declaration?
20673 if Is_Itype (Def_Id)
20674 and then
20675 Nkind (Parent (P)) = N_Full_Type_Declaration
20676 then
20677 Build_Itype_Reference (Def_Id, Parent (P));
20678 end if;
20680 else
20681 Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
20682 end if;
20684 when Concurrent_Kind =>
20685 Constrain_Concurrent (Def_Id, S,
20686 Related_Nod, Related_Id, Suffix);
20688 when others =>
20689 Error_Msg_N ("invalid subtype mark in subtype indication", S);
20690 end case;
20692 -- Size and Convention are always inherited from the base type
20694 Set_Size_Info (Def_Id, (Subtype_Mark_Id));
20695 Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
20697 return Def_Id;
20698 end if;
20699 end Process_Subtype;
20701 --------------------------------------------
20702 -- Propagate_Default_Init_Cond_Attributes --
20703 --------------------------------------------
20705 procedure Propagate_Default_Init_Cond_Attributes
20706 (From_Typ : Entity_Id;
20707 To_Typ : Entity_Id;
20708 Parent_To_Derivation : Boolean := False;
20709 Private_To_Full_View : Boolean := False)
20711 procedure Remove_Default_Init_Cond_Procedure (Typ : Entity_Id);
20712 -- Remove the default initial procedure (if any) from the rep chain of
20713 -- type Typ.
20715 ----------------------------------------
20716 -- Remove_Default_Init_Cond_Procedure --
20717 ----------------------------------------
20719 procedure Remove_Default_Init_Cond_Procedure (Typ : Entity_Id) is
20720 Found : Boolean := False;
20721 Prev : Entity_Id;
20722 Subp : Entity_Id;
20724 begin
20725 Prev := Typ;
20726 Subp := Subprograms_For_Type (Typ);
20727 while Present (Subp) loop
20728 if Is_Default_Init_Cond_Procedure (Subp) then
20729 Found := True;
20730 exit;
20731 end if;
20733 Prev := Subp;
20734 Subp := Subprograms_For_Type (Subp);
20735 end loop;
20737 if Found then
20738 Set_Subprograms_For_Type (Prev, Subprograms_For_Type (Subp));
20739 Set_Subprograms_For_Type (Subp, Empty);
20740 end if;
20741 end Remove_Default_Init_Cond_Procedure;
20743 -- Local variables
20745 Inherit_Procedure : Boolean := False;
20747 -- Start of processing for Propagate_Default_Init_Cond_Attributes
20749 begin
20750 if Has_Default_Init_Cond (From_Typ) then
20752 -- A derived type inherits the attributes from its parent type
20754 if Parent_To_Derivation then
20755 Set_Has_Inherited_Default_Init_Cond (To_Typ);
20757 -- A full view shares the attributes with its private view
20759 else
20760 Set_Has_Default_Init_Cond (To_Typ);
20761 end if;
20763 Inherit_Procedure := True;
20765 -- Due to the order of expansion, a derived private type is processed
20766 -- by two routines which both attempt to set the attributes related
20767 -- to pragma Default_Initial_Condition - Build_Derived_Type and then
20768 -- Process_Full_View.
20770 -- package Pack is
20771 -- type Parent_Typ is private
20772 -- with Default_Initial_Condition ...;
20773 -- private
20774 -- type Parent_Typ is ...;
20775 -- end Pack;
20777 -- with Pack; use Pack;
20778 -- package Pack_2 is
20779 -- type Deriv_Typ is private
20780 -- with Default_Initial_Condition ...;
20781 -- private
20782 -- type Deriv_Typ is new Parent_Typ;
20783 -- end Pack_2;
20785 -- When Build_Derived_Type operates, it sets the attributes on the
20786 -- full view without taking into account that the private view may
20787 -- define its own default initial condition procedure. This becomes
20788 -- apparent in Process_Full_View which must undo some of the work by
20789 -- Build_Derived_Type and propagate the attributes from the private
20790 -- to the full view.
20792 if Private_To_Full_View then
20793 Set_Has_Inherited_Default_Init_Cond (To_Typ, False);
20794 Remove_Default_Init_Cond_Procedure (To_Typ);
20795 end if;
20797 -- A type must inherit the default initial condition procedure from a
20798 -- parent type when the parent itself is inheriting the procedure or
20799 -- when it is defining one. This circuitry is also used when dealing
20800 -- with the private / full view of a type.
20802 elsif Has_Inherited_Default_Init_Cond (From_Typ)
20803 or (Parent_To_Derivation
20804 and Present (Get_Pragma
20805 (From_Typ, Pragma_Default_Initial_Condition)))
20806 then
20807 Set_Has_Inherited_Default_Init_Cond (To_Typ);
20808 Inherit_Procedure := True;
20809 end if;
20811 if Inherit_Procedure
20812 and then No (Default_Init_Cond_Procedure (To_Typ))
20813 then
20814 Set_Default_Init_Cond_Procedure
20815 (To_Typ, Default_Init_Cond_Procedure (From_Typ));
20816 end if;
20817 end Propagate_Default_Init_Cond_Attributes;
20819 -----------------------------
20820 -- Record_Type_Declaration --
20821 -----------------------------
20823 procedure Record_Type_Declaration
20824 (T : Entity_Id;
20825 N : Node_Id;
20826 Prev : Entity_Id)
20828 Def : constant Node_Id := Type_Definition (N);
20829 Is_Tagged : Boolean;
20830 Tag_Comp : Entity_Id;
20832 begin
20833 -- These flags must be initialized before calling Process_Discriminants
20834 -- because this routine makes use of them.
20836 Set_Ekind (T, E_Record_Type);
20837 Set_Etype (T, T);
20838 Init_Size_Align (T);
20839 Set_Interfaces (T, No_Elist);
20840 Set_Stored_Constraint (T, No_Elist);
20841 Set_Default_SSO (T);
20843 -- Normal case
20845 if Ada_Version < Ada_2005 or else not Interface_Present (Def) then
20846 if Limited_Present (Def) then
20847 Check_SPARK_05_Restriction ("limited is not allowed", N);
20848 end if;
20850 if Abstract_Present (Def) then
20851 Check_SPARK_05_Restriction ("abstract is not allowed", N);
20852 end if;
20854 -- The flag Is_Tagged_Type might have already been set by
20855 -- Find_Type_Name if it detected an error for declaration T. This
20856 -- arises in the case of private tagged types where the full view
20857 -- omits the word tagged.
20859 Is_Tagged :=
20860 Tagged_Present (Def)
20861 or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
20863 Set_Is_Limited_Record (T, Limited_Present (Def));
20865 if Is_Tagged then
20866 Set_Is_Tagged_Type (T, True);
20867 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
20868 end if;
20870 -- Type is abstract if full declaration carries keyword, or if
20871 -- previous partial view did.
20873 Set_Is_Abstract_Type (T, Is_Abstract_Type (T)
20874 or else Abstract_Present (Def));
20876 else
20877 Check_SPARK_05_Restriction ("interface is not allowed", N);
20879 Is_Tagged := True;
20880 Analyze_Interface_Declaration (T, Def);
20882 if Present (Discriminant_Specifications (N)) then
20883 Error_Msg_N
20884 ("interface types cannot have discriminants",
20885 Defining_Identifier
20886 (First (Discriminant_Specifications (N))));
20887 end if;
20888 end if;
20890 -- First pass: if there are self-referential access components,
20891 -- create the required anonymous access type declarations, and if
20892 -- need be an incomplete type declaration for T itself.
20894 Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
20896 if Ada_Version >= Ada_2005
20897 and then Present (Interface_List (Def))
20898 then
20899 Check_Interfaces (N, Def);
20901 declare
20902 Ifaces_List : Elist_Id;
20904 begin
20905 -- Ada 2005 (AI-251): Collect the list of progenitors that are not
20906 -- already in the parents.
20908 Collect_Interfaces
20909 (T => T,
20910 Ifaces_List => Ifaces_List,
20911 Exclude_Parents => True);
20913 Set_Interfaces (T, Ifaces_List);
20914 end;
20915 end if;
20917 -- Records constitute a scope for the component declarations within.
20918 -- The scope is created prior to the processing of these declarations.
20919 -- Discriminants are processed first, so that they are visible when
20920 -- processing the other components. The Ekind of the record type itself
20921 -- is set to E_Record_Type (subtypes appear as E_Record_Subtype).
20923 -- Enter record scope
20925 Push_Scope (T);
20927 -- If an incomplete or private type declaration was already given for
20928 -- the type, then this scope already exists, and the discriminants have
20929 -- been declared within. We must verify that the full declaration
20930 -- matches the incomplete one.
20932 Check_Or_Process_Discriminants (N, T, Prev);
20934 Set_Is_Constrained (T, not Has_Discriminants (T));
20935 Set_Has_Delayed_Freeze (T, True);
20937 -- For tagged types add a manually analyzed component corresponding
20938 -- to the component _tag, the corresponding piece of tree will be
20939 -- expanded as part of the freezing actions if it is not a CPP_Class.
20941 if Is_Tagged then
20943 -- Do not add the tag unless we are in expansion mode
20945 if Expander_Active then
20946 Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
20947 Enter_Name (Tag_Comp);
20949 Set_Ekind (Tag_Comp, E_Component);
20950 Set_Is_Tag (Tag_Comp);
20951 Set_Is_Aliased (Tag_Comp);
20952 Set_Etype (Tag_Comp, RTE (RE_Tag));
20953 Set_DT_Entry_Count (Tag_Comp, No_Uint);
20954 Set_Original_Record_Component (Tag_Comp, Tag_Comp);
20955 Init_Component_Location (Tag_Comp);
20957 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
20958 -- implemented interfaces.
20960 if Has_Interfaces (T) then
20961 Add_Interface_Tag_Components (N, T);
20962 end if;
20963 end if;
20965 Make_Class_Wide_Type (T);
20966 Set_Direct_Primitive_Operations (T, New_Elmt_List);
20967 end if;
20969 -- We must suppress range checks when processing record components in
20970 -- the presence of discriminants, since we don't want spurious checks to
20971 -- be generated during their analysis, but Suppress_Range_Checks flags
20972 -- must be reset the after processing the record definition.
20974 -- Note: this is the only use of Kill_Range_Checks, and is a bit odd,
20975 -- couldn't we just use the normal range check suppression method here.
20976 -- That would seem cleaner ???
20978 if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
20979 Set_Kill_Range_Checks (T, True);
20980 Record_Type_Definition (Def, Prev);
20981 Set_Kill_Range_Checks (T, False);
20982 else
20983 Record_Type_Definition (Def, Prev);
20984 end if;
20986 -- Exit from record scope
20988 End_Scope;
20990 -- Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
20991 -- the implemented interfaces and associate them an aliased entity.
20993 if Is_Tagged
20994 and then not Is_Empty_List (Interface_List (Def))
20995 then
20996 Derive_Progenitor_Subprograms (T, T);
20997 end if;
20999 Check_Function_Writable_Actuals (N);
21000 end Record_Type_Declaration;
21002 ----------------------------
21003 -- Record_Type_Definition --
21004 ----------------------------
21006 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
21007 Component : Entity_Id;
21008 Ctrl_Components : Boolean := False;
21009 Final_Storage_Only : Boolean;
21010 T : Entity_Id;
21012 begin
21013 if Ekind (Prev_T) = E_Incomplete_Type then
21014 T := Full_View (Prev_T);
21015 else
21016 T := Prev_T;
21017 end if;
21019 -- In SPARK, tagged types and type extensions may only be declared in
21020 -- the specification of library unit packages.
21022 if Present (Def) and then Is_Tagged_Type (T) then
21023 declare
21024 Typ : Node_Id;
21025 Ctxt : Node_Id;
21027 begin
21028 if Nkind (Parent (Def)) = N_Full_Type_Declaration then
21029 Typ := Parent (Def);
21030 else
21031 pragma Assert
21032 (Nkind (Parent (Def)) = N_Derived_Type_Definition);
21033 Typ := Parent (Parent (Def));
21034 end if;
21036 Ctxt := Parent (Typ);
21038 if Nkind (Ctxt) = N_Package_Body
21039 and then Nkind (Parent (Ctxt)) = N_Compilation_Unit
21040 then
21041 Check_SPARK_05_Restriction
21042 ("type should be defined in package specification", Typ);
21044 elsif Nkind (Ctxt) /= N_Package_Specification
21045 or else Nkind (Parent (Parent (Ctxt))) /= N_Compilation_Unit
21046 then
21047 Check_SPARK_05_Restriction
21048 ("type should be defined in library unit package", Typ);
21049 end if;
21050 end;
21051 end if;
21053 Final_Storage_Only := not Is_Controlled (T);
21055 -- Ada 2005: Check whether an explicit Limited is present in a derived
21056 -- type declaration.
21058 if Nkind (Parent (Def)) = N_Derived_Type_Definition
21059 and then Limited_Present (Parent (Def))
21060 then
21061 Set_Is_Limited_Record (T);
21062 end if;
21064 -- If the component list of a record type is defined by the reserved
21065 -- word null and there is no discriminant part, then the record type has
21066 -- no components and all records of the type are null records (RM 3.7)
21067 -- This procedure is also called to process the extension part of a
21068 -- record extension, in which case the current scope may have inherited
21069 -- components.
21071 if No (Def)
21072 or else No (Component_List (Def))
21073 or else Null_Present (Component_List (Def))
21074 then
21075 if not Is_Tagged_Type (T) then
21076 Check_SPARK_05_Restriction ("untagged record cannot be null", Def);
21077 end if;
21079 else
21080 Analyze_Declarations (Component_Items (Component_List (Def)));
21082 if Present (Variant_Part (Component_List (Def))) then
21083 Check_SPARK_05_Restriction ("variant part is not allowed", Def);
21084 Analyze (Variant_Part (Component_List (Def)));
21085 end if;
21086 end if;
21088 -- After completing the semantic analysis of the record definition,
21089 -- record components, both new and inherited, are accessible. Set their
21090 -- kind accordingly. Exclude malformed itypes from illegal declarations,
21091 -- whose Ekind may be void.
21093 Component := First_Entity (Current_Scope);
21094 while Present (Component) loop
21095 if Ekind (Component) = E_Void
21096 and then not Is_Itype (Component)
21097 then
21098 Set_Ekind (Component, E_Component);
21099 Init_Component_Location (Component);
21100 end if;
21102 if Has_Task (Etype (Component)) then
21103 Set_Has_Task (T);
21104 end if;
21106 if Has_Protected (Etype (Component)) then
21107 Set_Has_Protected (T);
21108 end if;
21110 if Ekind (Component) /= E_Component then
21111 null;
21113 -- Do not set Has_Controlled_Component on a class-wide equivalent
21114 -- type. See Make_CW_Equivalent_Type.
21116 elsif not Is_Class_Wide_Equivalent_Type (T)
21117 and then (Has_Controlled_Component (Etype (Component))
21118 or else (Chars (Component) /= Name_uParent
21119 and then Is_Controlled (Etype (Component))))
21120 then
21121 Set_Has_Controlled_Component (T, True);
21122 Final_Storage_Only :=
21123 Final_Storage_Only
21124 and then Finalize_Storage_Only (Etype (Component));
21125 Ctrl_Components := True;
21126 end if;
21128 Next_Entity (Component);
21129 end loop;
21131 -- A Type is Finalize_Storage_Only only if all its controlled components
21132 -- are also.
21134 if Ctrl_Components then
21135 Set_Finalize_Storage_Only (T, Final_Storage_Only);
21136 end if;
21138 -- Place reference to end record on the proper entity, which may
21139 -- be a partial view.
21141 if Present (Def) then
21142 Process_End_Label (Def, 'e', Prev_T);
21143 end if;
21144 end Record_Type_Definition;
21146 ------------------------
21147 -- Replace_Components --
21148 ------------------------
21150 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id) is
21151 function Process (N : Node_Id) return Traverse_Result;
21153 -------------
21154 -- Process --
21155 -------------
21157 function Process (N : Node_Id) return Traverse_Result is
21158 Comp : Entity_Id;
21160 begin
21161 if Nkind (N) = N_Discriminant_Specification then
21162 Comp := First_Discriminant (Typ);
21163 while Present (Comp) loop
21164 if Chars (Comp) = Chars (Defining_Identifier (N)) then
21165 Set_Defining_Identifier (N, Comp);
21166 exit;
21167 end if;
21169 Next_Discriminant (Comp);
21170 end loop;
21172 elsif Nkind (N) = N_Component_Declaration then
21173 Comp := First_Component (Typ);
21174 while Present (Comp) loop
21175 if Chars (Comp) = Chars (Defining_Identifier (N)) then
21176 Set_Defining_Identifier (N, Comp);
21177 exit;
21178 end if;
21180 Next_Component (Comp);
21181 end loop;
21182 end if;
21184 return OK;
21185 end Process;
21187 procedure Replace is new Traverse_Proc (Process);
21189 -- Start of processing for Replace_Components
21191 begin
21192 Replace (Decl);
21193 end Replace_Components;
21195 -------------------------------
21196 -- Set_Completion_Referenced --
21197 -------------------------------
21199 procedure Set_Completion_Referenced (E : Entity_Id) is
21200 begin
21201 -- If in main unit, mark entity that is a completion as referenced,
21202 -- warnings go on the partial view when needed.
21204 if In_Extended_Main_Source_Unit (E) then
21205 Set_Referenced (E);
21206 end if;
21207 end Set_Completion_Referenced;
21209 ---------------------
21210 -- Set_Default_SSO --
21211 ---------------------
21213 procedure Set_Default_SSO (T : Entity_Id) is
21214 begin
21215 case Opt.Default_SSO is
21216 when ' ' =>
21217 null;
21218 when 'L' =>
21219 Set_SSO_Set_Low_By_Default (T, True);
21220 when 'H' =>
21221 Set_SSO_Set_High_By_Default (T, True);
21222 when others =>
21223 raise Program_Error;
21224 end case;
21225 end Set_Default_SSO;
21227 ---------------------
21228 -- Set_Fixed_Range --
21229 ---------------------
21231 -- The range for fixed-point types is complicated by the fact that we
21232 -- do not know the exact end points at the time of the declaration. This
21233 -- is true for three reasons:
21235 -- A size clause may affect the fudging of the end-points.
21236 -- A small clause may affect the values of the end-points.
21237 -- We try to include the end-points if it does not affect the size.
21239 -- This means that the actual end-points must be established at the
21240 -- point when the type is frozen. Meanwhile, we first narrow the range
21241 -- as permitted (so that it will fit if necessary in a small specified
21242 -- size), and then build a range subtree with these narrowed bounds.
21243 -- Set_Fixed_Range constructs the range from real literal values, and
21244 -- sets the range as the Scalar_Range of the given fixed-point type entity.
21246 -- The parent of this range is set to point to the entity so that it is
21247 -- properly hooked into the tree (unlike normal Scalar_Range entries for
21248 -- other scalar types, which are just pointers to the range in the
21249 -- original tree, this would otherwise be an orphan).
21251 -- The tree is left unanalyzed. When the type is frozen, the processing
21252 -- in Freeze.Freeze_Fixed_Point_Type notices that the range is not
21253 -- analyzed, and uses this as an indication that it should complete
21254 -- work on the range (it will know the final small and size values).
21256 procedure Set_Fixed_Range
21257 (E : Entity_Id;
21258 Loc : Source_Ptr;
21259 Lo : Ureal;
21260 Hi : Ureal)
21262 S : constant Node_Id :=
21263 Make_Range (Loc,
21264 Low_Bound => Make_Real_Literal (Loc, Lo),
21265 High_Bound => Make_Real_Literal (Loc, Hi));
21266 begin
21267 Set_Scalar_Range (E, S);
21268 Set_Parent (S, E);
21270 -- Before the freeze point, the bounds of a fixed point are universal
21271 -- and carry the corresponding type.
21273 Set_Etype (Low_Bound (S), Universal_Real);
21274 Set_Etype (High_Bound (S), Universal_Real);
21275 end Set_Fixed_Range;
21277 ----------------------------------
21278 -- Set_Scalar_Range_For_Subtype --
21279 ----------------------------------
21281 procedure Set_Scalar_Range_For_Subtype
21282 (Def_Id : Entity_Id;
21283 R : Node_Id;
21284 Subt : Entity_Id)
21286 Kind : constant Entity_Kind := Ekind (Def_Id);
21288 begin
21289 -- Defend against previous error
21291 if Nkind (R) = N_Error then
21292 return;
21293 end if;
21295 Set_Scalar_Range (Def_Id, R);
21297 -- We need to link the range into the tree before resolving it so
21298 -- that types that are referenced, including importantly the subtype
21299 -- itself, are properly frozen (Freeze_Expression requires that the
21300 -- expression be properly linked into the tree). Of course if it is
21301 -- already linked in, then we do not disturb the current link.
21303 if No (Parent (R)) then
21304 Set_Parent (R, Def_Id);
21305 end if;
21307 -- Reset the kind of the subtype during analysis of the range, to
21308 -- catch possible premature use in the bounds themselves.
21310 Set_Ekind (Def_Id, E_Void);
21311 Process_Range_Expr_In_Decl (R, Subt, Subtyp => Def_Id);
21312 Set_Ekind (Def_Id, Kind);
21313 end Set_Scalar_Range_For_Subtype;
21315 --------------------------------------------------------
21316 -- Set_Stored_Constraint_From_Discriminant_Constraint --
21317 --------------------------------------------------------
21319 procedure Set_Stored_Constraint_From_Discriminant_Constraint
21320 (E : Entity_Id)
21322 begin
21323 -- Make sure set if encountered during Expand_To_Stored_Constraint
21325 Set_Stored_Constraint (E, No_Elist);
21327 -- Give it the right value
21329 if Is_Constrained (E) and then Has_Discriminants (E) then
21330 Set_Stored_Constraint (E,
21331 Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
21332 end if;
21333 end Set_Stored_Constraint_From_Discriminant_Constraint;
21335 -------------------------------------
21336 -- Signed_Integer_Type_Declaration --
21337 -------------------------------------
21339 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
21340 Implicit_Base : Entity_Id;
21341 Base_Typ : Entity_Id;
21342 Lo_Val : Uint;
21343 Hi_Val : Uint;
21344 Errs : Boolean := False;
21345 Lo : Node_Id;
21346 Hi : Node_Id;
21348 function Can_Derive_From (E : Entity_Id) return Boolean;
21349 -- Determine whether given bounds allow derivation from specified type
21351 procedure Check_Bound (Expr : Node_Id);
21352 -- Check bound to make sure it is integral and static. If not, post
21353 -- appropriate error message and set Errs flag
21355 ---------------------
21356 -- Can_Derive_From --
21357 ---------------------
21359 -- Note we check both bounds against both end values, to deal with
21360 -- strange types like ones with a range of 0 .. -12341234.
21362 function Can_Derive_From (E : Entity_Id) return Boolean is
21363 Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
21364 Hi : constant Uint := Expr_Value (Type_High_Bound (E));
21365 begin
21366 return Lo <= Lo_Val and then Lo_Val <= Hi
21367 and then
21368 Lo <= Hi_Val and then Hi_Val <= Hi;
21369 end Can_Derive_From;
21371 -----------------
21372 -- Check_Bound --
21373 -----------------
21375 procedure Check_Bound (Expr : Node_Id) is
21376 begin
21377 -- If a range constraint is used as an integer type definition, each
21378 -- bound of the range must be defined by a static expression of some
21379 -- integer type, but the two bounds need not have the same integer
21380 -- type (Negative bounds are allowed.) (RM 3.5.4)
21382 if not Is_Integer_Type (Etype (Expr)) then
21383 Error_Msg_N
21384 ("integer type definition bounds must be of integer type", Expr);
21385 Errs := True;
21387 elsif not Is_OK_Static_Expression (Expr) then
21388 Flag_Non_Static_Expr
21389 ("non-static expression used for integer type bound!", Expr);
21390 Errs := True;
21392 -- The bounds are folded into literals, and we set their type to be
21393 -- universal, to avoid typing difficulties: we cannot set the type
21394 -- of the literal to the new type, because this would be a forward
21395 -- reference for the back end, and if the original type is user-
21396 -- defined this can lead to spurious semantic errors (e.g. 2928-003).
21398 else
21399 if Is_Entity_Name (Expr) then
21400 Fold_Uint (Expr, Expr_Value (Expr), True);
21401 end if;
21403 Set_Etype (Expr, Universal_Integer);
21404 end if;
21405 end Check_Bound;
21407 -- Start of processing for Signed_Integer_Type_Declaration
21409 begin
21410 -- Create an anonymous base type
21412 Implicit_Base :=
21413 Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
21415 -- Analyze and check the bounds, they can be of any integer type
21417 Lo := Low_Bound (Def);
21418 Hi := High_Bound (Def);
21420 -- Arbitrarily use Integer as the type if either bound had an error
21422 if Hi = Error or else Lo = Error then
21423 Base_Typ := Any_Integer;
21424 Set_Error_Posted (T, True);
21426 -- Here both bounds are OK expressions
21428 else
21429 Analyze_And_Resolve (Lo, Any_Integer);
21430 Analyze_And_Resolve (Hi, Any_Integer);
21432 Check_Bound (Lo);
21433 Check_Bound (Hi);
21435 if Errs then
21436 Hi := Type_High_Bound (Standard_Long_Long_Integer);
21437 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
21438 end if;
21440 -- Find type to derive from
21442 Lo_Val := Expr_Value (Lo);
21443 Hi_Val := Expr_Value (Hi);
21445 if Can_Derive_From (Standard_Short_Short_Integer) then
21446 Base_Typ := Base_Type (Standard_Short_Short_Integer);
21448 elsif Can_Derive_From (Standard_Short_Integer) then
21449 Base_Typ := Base_Type (Standard_Short_Integer);
21451 elsif Can_Derive_From (Standard_Integer) then
21452 Base_Typ := Base_Type (Standard_Integer);
21454 elsif Can_Derive_From (Standard_Long_Integer) then
21455 Base_Typ := Base_Type (Standard_Long_Integer);
21457 elsif Can_Derive_From (Standard_Long_Long_Integer) then
21458 Check_Restriction (No_Long_Long_Integers, Def);
21459 Base_Typ := Base_Type (Standard_Long_Long_Integer);
21461 else
21462 Base_Typ := Base_Type (Standard_Long_Long_Integer);
21463 Error_Msg_N ("integer type definition bounds out of range", Def);
21464 Hi := Type_High_Bound (Standard_Long_Long_Integer);
21465 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
21466 end if;
21467 end if;
21469 -- Complete both implicit base and declared first subtype entities. The
21470 -- inheritance of the rep item chain ensures that SPARK-related pragmas
21471 -- are not clobbered when the signed integer type acts as a full view of
21472 -- a private type.
21474 Set_Etype (Implicit_Base, Base_Typ);
21475 Set_Size_Info (Implicit_Base, Base_Typ);
21476 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
21477 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
21478 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
21480 Set_Ekind (T, E_Signed_Integer_Subtype);
21481 Set_Etype (T, Implicit_Base);
21482 Set_Size_Info (T, Implicit_Base);
21483 Inherit_Rep_Item_Chain (T, Implicit_Base);
21484 Set_Scalar_Range (T, Def);
21485 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
21486 Set_Is_Constrained (T);
21487 end Signed_Integer_Type_Declaration;
21489 end Sem_Ch3;