2015-03-02 Robert Dewar <dewar@adacore.com>
[official-gcc.git] / gcc / ada / sem_ch3.adb
blob537be5ea6f305394407b647ac323875ec28aae87
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
8057 -- Force_Evaluation was called on each constraint in
8058 -- 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 -- A type extension is automatically Ghost when one of its
8644 -- progenitors is Ghost (SPARK RM 6.9(9)). This property is
8645 -- also inherited when the parent type is Ghost, but this is
8646 -- done in Build_Derived_Type as the mechanism also handles
8647 -- untagged derivations.
8649 if Implements_Ghost_Interface (Derived_Type) then
8650 Set_Is_Ghost_Entity (Derived_Type);
8651 end if;
8652 end;
8653 end if;
8655 else
8656 Set_Is_Packed (Derived_Type, Is_Packed (Parent_Base));
8657 Set_Has_Non_Standard_Rep
8658 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
8659 end if;
8661 -- STEP 4: Inherit components from the parent base and constrain them.
8662 -- Apply the second transformation described in point 6. above.
8664 if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
8665 or else not Has_Discriminants (Parent_Type)
8666 or else not Is_Constrained (Parent_Type)
8667 then
8668 Constrs := Discs;
8669 else
8670 Constrs := Discriminant_Constraint (Parent_Type);
8671 end if;
8673 Assoc_List :=
8674 Inherit_Components
8675 (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
8677 -- STEP 5a: Copy the parent record declaration for untagged types
8679 if not Is_Tagged then
8681 -- Discriminant_Constraint (Derived_Type) has been properly
8682 -- constructed. Save it and temporarily set it to Empty because we
8683 -- do not want the call to New_Copy_Tree below to mess this list.
8685 if Has_Discriminants (Derived_Type) then
8686 Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
8687 Set_Discriminant_Constraint (Derived_Type, No_Elist);
8688 else
8689 Save_Discr_Constr := No_Elist;
8690 end if;
8692 -- Save the Etype field of Derived_Type. It is correctly set now,
8693 -- but the call to New_Copy tree may remap it to point to itself,
8694 -- which is not what we want. Ditto for the Next_Entity field.
8696 Save_Etype := Etype (Derived_Type);
8697 Save_Next_Entity := Next_Entity (Derived_Type);
8699 -- Assoc_List maps all stored discriminants in the Parent_Base to
8700 -- stored discriminants in the Derived_Type. It is fundamental that
8701 -- no types or itypes with discriminants other than the stored
8702 -- discriminants appear in the entities declared inside
8703 -- Derived_Type, since the back end cannot deal with it.
8705 New_Decl :=
8706 New_Copy_Tree
8707 (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
8709 -- Restore the fields saved prior to the New_Copy_Tree call
8710 -- and compute the stored constraint.
8712 Set_Etype (Derived_Type, Save_Etype);
8713 Set_Next_Entity (Derived_Type, Save_Next_Entity);
8715 if Has_Discriminants (Derived_Type) then
8716 Set_Discriminant_Constraint
8717 (Derived_Type, Save_Discr_Constr);
8718 Set_Stored_Constraint
8719 (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
8720 Replace_Components (Derived_Type, New_Decl);
8721 Set_Has_Implicit_Dereference
8722 (Derived_Type, Has_Implicit_Dereference (Parent_Type));
8723 end if;
8725 -- Insert the new derived type declaration
8727 Rewrite (N, New_Decl);
8729 -- STEP 5b: Complete the processing for record extensions in generics
8731 -- There is no completion for record extensions declared in the
8732 -- parameter part of a generic, so we need to complete processing for
8733 -- these generic record extensions here. The Record_Type_Definition call
8734 -- will change the Ekind of the components from E_Void to E_Component.
8736 elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
8737 Record_Type_Definition (Empty, Derived_Type);
8739 -- STEP 5c: Process the record extension for non private tagged types
8741 elsif not Private_Extension then
8742 Expand_Record_Extension (Derived_Type, Type_Def);
8744 -- Note : previously in ASIS mode we set the Parent_Subtype of the
8745 -- derived type to propagate some semantic information. This led
8746 -- to other ASIS failures and has been removed.
8748 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
8749 -- implemented interfaces if we are in expansion mode
8751 if Expander_Active
8752 and then Has_Interfaces (Derived_Type)
8753 then
8754 Add_Interface_Tag_Components (N, Derived_Type);
8755 end if;
8757 -- Analyze the record extension
8759 Record_Type_Definition
8760 (Record_Extension_Part (Type_Def), Derived_Type);
8761 end if;
8763 End_Scope;
8765 -- Nothing else to do if there is an error in the derivation.
8766 -- An unusual case: the full view may be derived from a type in an
8767 -- instance, when the partial view was used illegally as an actual
8768 -- in that instance, leading to a circular definition.
8770 if Etype (Derived_Type) = Any_Type
8771 or else Etype (Parent_Type) = Derived_Type
8772 then
8773 return;
8774 end if;
8776 -- Set delayed freeze and then derive subprograms, we need to do
8777 -- this in this order so that derived subprograms inherit the
8778 -- derived freeze if necessary.
8780 Set_Has_Delayed_Freeze (Derived_Type);
8782 if Derive_Subps then
8783 Derive_Subprograms (Parent_Type, Derived_Type);
8784 end if;
8786 -- If we have a private extension which defines a constrained derived
8787 -- type mark as constrained here after we have derived subprograms. See
8788 -- comment on point 9. just above the body of Build_Derived_Record_Type.
8790 if Private_Extension and then Inherit_Discrims then
8791 if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
8792 Set_Is_Constrained (Derived_Type, True);
8793 Set_Discriminant_Constraint (Derived_Type, Discs);
8795 elsif Is_Constrained (Parent_Type) then
8796 Set_Is_Constrained
8797 (Derived_Type, True);
8798 Set_Discriminant_Constraint
8799 (Derived_Type, Discriminant_Constraint (Parent_Type));
8800 end if;
8801 end if;
8803 -- Update the class-wide type, which shares the now-completed entity
8804 -- list with its specific type. In case of underlying record views,
8805 -- we do not generate the corresponding class wide entity.
8807 if Is_Tagged
8808 and then not Is_Underlying_Record_View (Derived_Type)
8809 then
8810 Set_First_Entity
8811 (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
8812 Set_Last_Entity
8813 (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
8814 end if;
8816 Check_Function_Writable_Actuals (N);
8817 end Build_Derived_Record_Type;
8819 ------------------------
8820 -- Build_Derived_Type --
8821 ------------------------
8823 procedure Build_Derived_Type
8824 (N : Node_Id;
8825 Parent_Type : Entity_Id;
8826 Derived_Type : Entity_Id;
8827 Is_Completion : Boolean;
8828 Derive_Subps : Boolean := True)
8830 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
8832 begin
8833 -- Set common attributes
8835 Set_Scope (Derived_Type, Current_Scope);
8837 Set_Etype (Derived_Type, Parent_Base);
8838 Set_Ekind (Derived_Type, Ekind (Parent_Base));
8839 Set_Has_Task (Derived_Type, Has_Task (Parent_Base));
8840 Set_Has_Protected (Derived_Type, Has_Protected (Parent_Base));
8842 Set_Size_Info (Derived_Type, Parent_Type);
8843 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
8844 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
8845 Set_Is_Tagged_Type (Derived_Type, Is_Tagged_Type (Parent_Type));
8846 Set_Is_Volatile (Derived_Type, Is_Volatile (Parent_Type));
8848 if Is_Tagged_Type (Derived_Type) then
8849 Set_No_Tagged_Streams_Pragma
8850 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
8851 end if;
8853 -- If the parent has primitive routines, set the derived type link
8855 if Has_Primitive_Operations (Parent_Type) then
8856 Set_Derived_Type_Link (Parent_Base, Derived_Type);
8857 end if;
8859 -- If the parent type is a private subtype, the convention on the base
8860 -- type may be set in the private part, and not propagated to the
8861 -- subtype until later, so we obtain the convention from the base type.
8863 Set_Convention (Derived_Type, Convention (Parent_Base));
8865 -- Set SSO default for record or array type
8867 if (Is_Array_Type (Derived_Type) or else Is_Record_Type (Derived_Type))
8868 and then Is_Base_Type (Derived_Type)
8869 then
8870 Set_Default_SSO (Derived_Type);
8871 end if;
8873 -- Propagate invariant information. The new type has invariants if
8874 -- they are inherited from the parent type, and these invariants can
8875 -- be further inherited, so both flags are set.
8877 -- We similarly inherit predicates
8879 if Has_Predicates (Parent_Type) then
8880 Set_Has_Predicates (Derived_Type);
8881 end if;
8883 -- The derived type inherits the representation clauses of the parent
8885 Inherit_Rep_Item_Chain (Derived_Type, Parent_Type);
8887 -- Propagate the attributes related to pragma Default_Initial_Condition
8888 -- from the parent type to the private extension. A derived type always
8889 -- inherits the default initial condition flag from the parent type. If
8890 -- the derived type carries its own Default_Initial_Condition pragma,
8891 -- the flag is later reset in Analyze_Pragma. Note that both flags are
8892 -- mutually exclusive.
8894 Propagate_Default_Init_Cond_Attributes
8895 (From_Typ => Parent_Type,
8896 To_Typ => Derived_Type,
8897 Parent_To_Derivation => True);
8899 -- If the parent type has delayed rep aspects, then mark the derived
8900 -- type as possibly inheriting a delayed rep aspect.
8902 if Has_Delayed_Rep_Aspects (Parent_Type) then
8903 Set_May_Inherit_Delayed_Rep_Aspects (Derived_Type);
8904 end if;
8906 -- Propagate the attributes related to pragma Ghost from the parent type
8907 -- to the derived type or type extension (SPARK RM 6.9(9)).
8909 if Is_Ghost_Entity (Parent_Type) then
8910 Set_Is_Ghost_Entity (Derived_Type);
8911 end if;
8913 -- Type dependent processing
8915 case Ekind (Parent_Type) is
8916 when Numeric_Kind =>
8917 Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
8919 when Array_Kind =>
8920 Build_Derived_Array_Type (N, Parent_Type, Derived_Type);
8922 when E_Record_Type
8923 | E_Record_Subtype
8924 | Class_Wide_Kind =>
8925 Build_Derived_Record_Type
8926 (N, Parent_Type, Derived_Type, Derive_Subps);
8927 return;
8929 when Enumeration_Kind =>
8930 Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
8932 when Access_Kind =>
8933 Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
8935 when Incomplete_Or_Private_Kind =>
8936 Build_Derived_Private_Type
8937 (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
8939 -- For discriminated types, the derivation includes deriving
8940 -- primitive operations. For others it is done below.
8942 if Is_Tagged_Type (Parent_Type)
8943 or else Has_Discriminants (Parent_Type)
8944 or else (Present (Full_View (Parent_Type))
8945 and then Has_Discriminants (Full_View (Parent_Type)))
8946 then
8947 return;
8948 end if;
8950 when Concurrent_Kind =>
8951 Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
8953 when others =>
8954 raise Program_Error;
8955 end case;
8957 -- Nothing more to do if some error occurred
8959 if Etype (Derived_Type) = Any_Type then
8960 return;
8961 end if;
8963 -- Set delayed freeze and then derive subprograms, we need to do this
8964 -- in this order so that derived subprograms inherit the derived freeze
8965 -- if necessary.
8967 Set_Has_Delayed_Freeze (Derived_Type);
8969 if Derive_Subps then
8970 Derive_Subprograms (Parent_Type, Derived_Type);
8971 end if;
8973 Set_Has_Primitive_Operations
8974 (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
8975 end Build_Derived_Type;
8977 -----------------------
8978 -- Build_Discriminal --
8979 -----------------------
8981 procedure Build_Discriminal (Discrim : Entity_Id) is
8982 D_Minal : Entity_Id;
8983 CR_Disc : Entity_Id;
8985 begin
8986 -- A discriminal has the same name as the discriminant
8988 D_Minal := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
8990 Set_Ekind (D_Minal, E_In_Parameter);
8991 Set_Mechanism (D_Minal, Default_Mechanism);
8992 Set_Etype (D_Minal, Etype (Discrim));
8993 Set_Scope (D_Minal, Current_Scope);
8995 Set_Discriminal (Discrim, D_Minal);
8996 Set_Discriminal_Link (D_Minal, Discrim);
8998 -- For task types, build at once the discriminants of the corresponding
8999 -- record, which are needed if discriminants are used in entry defaults
9000 -- and in family bounds.
9002 if Is_Concurrent_Type (Current_Scope)
9003 or else
9004 Is_Limited_Type (Current_Scope)
9005 then
9006 CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9008 Set_Ekind (CR_Disc, E_In_Parameter);
9009 Set_Mechanism (CR_Disc, Default_Mechanism);
9010 Set_Etype (CR_Disc, Etype (Discrim));
9011 Set_Scope (CR_Disc, Current_Scope);
9012 Set_Discriminal_Link (CR_Disc, Discrim);
9013 Set_CR_Discriminant (Discrim, CR_Disc);
9014 end if;
9015 end Build_Discriminal;
9017 ------------------------------------
9018 -- Build_Discriminant_Constraints --
9019 ------------------------------------
9021 function Build_Discriminant_Constraints
9022 (T : Entity_Id;
9023 Def : Node_Id;
9024 Derived_Def : Boolean := False) return Elist_Id
9026 C : constant Node_Id := Constraint (Def);
9027 Nb_Discr : constant Nat := Number_Discriminants (T);
9029 Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
9030 -- Saves the expression corresponding to a given discriminant in T
9032 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
9033 -- Return the Position number within array Discr_Expr of a discriminant
9034 -- D within the discriminant list of the discriminated type T.
9036 procedure Process_Discriminant_Expression
9037 (Expr : Node_Id;
9038 D : Entity_Id);
9039 -- If this is a discriminant constraint on a partial view, do not
9040 -- generate an overflow check on the discriminant expression. The check
9041 -- will be generated when constraining the full view. Otherwise the
9042 -- backend creates duplicate symbols for the temporaries corresponding
9043 -- to the expressions to be checked, causing spurious assembler errors.
9045 ------------------
9046 -- Pos_Of_Discr --
9047 ------------------
9049 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
9050 Disc : Entity_Id;
9052 begin
9053 Disc := First_Discriminant (T);
9054 for J in Discr_Expr'Range loop
9055 if Disc = D then
9056 return J;
9057 end if;
9059 Next_Discriminant (Disc);
9060 end loop;
9062 -- Note: Since this function is called on discriminants that are
9063 -- known to belong to the discriminated type, falling through the
9064 -- loop with no match signals an internal compiler error.
9066 raise Program_Error;
9067 end Pos_Of_Discr;
9069 -------------------------------------
9070 -- Process_Discriminant_Expression --
9071 -------------------------------------
9073 procedure Process_Discriminant_Expression
9074 (Expr : Node_Id;
9075 D : Entity_Id)
9077 BDT : constant Entity_Id := Base_Type (Etype (D));
9079 begin
9080 -- If this is a discriminant constraint on a partial view, do
9081 -- not generate an overflow on the discriminant expression. The
9082 -- check will be generated when constraining the full view.
9084 if Is_Private_Type (T)
9085 and then Present (Full_View (T))
9086 then
9087 Analyze_And_Resolve (Expr, BDT, Suppress => Overflow_Check);
9088 else
9089 Analyze_And_Resolve (Expr, BDT);
9090 end if;
9091 end Process_Discriminant_Expression;
9093 -- Declarations local to Build_Discriminant_Constraints
9095 Discr : Entity_Id;
9096 E : Entity_Id;
9097 Elist : constant Elist_Id := New_Elmt_List;
9099 Constr : Node_Id;
9100 Expr : Node_Id;
9101 Id : Node_Id;
9102 Position : Nat;
9103 Found : Boolean;
9105 Discrim_Present : Boolean := False;
9107 -- Start of processing for Build_Discriminant_Constraints
9109 begin
9110 -- The following loop will process positional associations only.
9111 -- For a positional association, the (single) discriminant is
9112 -- implicitly specified by position, in textual order (RM 3.7.2).
9114 Discr := First_Discriminant (T);
9115 Constr := First (Constraints (C));
9116 for D in Discr_Expr'Range loop
9117 exit when Nkind (Constr) = N_Discriminant_Association;
9119 if No (Constr) then
9120 Error_Msg_N ("too few discriminants given in constraint", C);
9121 return New_Elmt_List;
9123 elsif Nkind (Constr) = N_Range
9124 or else (Nkind (Constr) = N_Attribute_Reference
9125 and then Attribute_Name (Constr) = Name_Range)
9126 then
9127 Error_Msg_N
9128 ("a range is not a valid discriminant constraint", Constr);
9129 Discr_Expr (D) := Error;
9131 else
9132 Process_Discriminant_Expression (Constr, Discr);
9133 Discr_Expr (D) := Constr;
9134 end if;
9136 Next_Discriminant (Discr);
9137 Next (Constr);
9138 end loop;
9140 if No (Discr) and then Present (Constr) then
9141 Error_Msg_N ("too many discriminants given in constraint", Constr);
9142 return New_Elmt_List;
9143 end if;
9145 -- Named associations can be given in any order, but if both positional
9146 -- and named associations are used in the same discriminant constraint,
9147 -- then positional associations must occur first, at their normal
9148 -- position. Hence once a named association is used, the rest of the
9149 -- discriminant constraint must use only named associations.
9151 while Present (Constr) loop
9153 -- Positional association forbidden after a named association
9155 if Nkind (Constr) /= N_Discriminant_Association then
9156 Error_Msg_N ("positional association follows named one", Constr);
9157 return New_Elmt_List;
9159 -- Otherwise it is a named association
9161 else
9162 -- E records the type of the discriminants in the named
9163 -- association. All the discriminants specified in the same name
9164 -- association must have the same type.
9166 E := Empty;
9168 -- Search the list of discriminants in T to see if the simple name
9169 -- given in the constraint matches any of them.
9171 Id := First (Selector_Names (Constr));
9172 while Present (Id) loop
9173 Found := False;
9175 -- If Original_Discriminant is present, we are processing a
9176 -- generic instantiation and this is an instance node. We need
9177 -- to find the name of the corresponding discriminant in the
9178 -- actual record type T and not the name of the discriminant in
9179 -- the generic formal. Example:
9181 -- generic
9182 -- type G (D : int) is private;
9183 -- package P is
9184 -- subtype W is G (D => 1);
9185 -- end package;
9186 -- type Rec (X : int) is record ... end record;
9187 -- package Q is new P (G => Rec);
9189 -- At the point of the instantiation, formal type G is Rec
9190 -- and therefore when reanalyzing "subtype W is G (D => 1);"
9191 -- which really looks like "subtype W is Rec (D => 1);" at
9192 -- the point of instantiation, we want to find the discriminant
9193 -- that corresponds to D in Rec, i.e. X.
9195 if Present (Original_Discriminant (Id))
9196 and then In_Instance
9197 then
9198 Discr := Find_Corresponding_Discriminant (Id, T);
9199 Found := True;
9201 else
9202 Discr := First_Discriminant (T);
9203 while Present (Discr) loop
9204 if Chars (Discr) = Chars (Id) then
9205 Found := True;
9206 exit;
9207 end if;
9209 Next_Discriminant (Discr);
9210 end loop;
9212 if not Found then
9213 Error_Msg_N ("& does not match any discriminant", Id);
9214 return New_Elmt_List;
9216 -- If the parent type is a generic formal, preserve the
9217 -- name of the discriminant for subsequent instances.
9218 -- see comment at the beginning of this if statement.
9220 elsif Is_Generic_Type (Root_Type (T)) then
9221 Set_Original_Discriminant (Id, Discr);
9222 end if;
9223 end if;
9225 Position := Pos_Of_Discr (T, Discr);
9227 if Present (Discr_Expr (Position)) then
9228 Error_Msg_N ("duplicate constraint for discriminant&", Id);
9230 else
9231 -- Each discriminant specified in the same named association
9232 -- must be associated with a separate copy of the
9233 -- corresponding expression.
9235 if Present (Next (Id)) then
9236 Expr := New_Copy_Tree (Expression (Constr));
9237 Set_Parent (Expr, Parent (Expression (Constr)));
9238 else
9239 Expr := Expression (Constr);
9240 end if;
9242 Discr_Expr (Position) := Expr;
9243 Process_Discriminant_Expression (Expr, Discr);
9244 end if;
9246 -- A discriminant association with more than one discriminant
9247 -- name is only allowed if the named discriminants are all of
9248 -- the same type (RM 3.7.1(8)).
9250 if E = Empty then
9251 E := Base_Type (Etype (Discr));
9253 elsif Base_Type (Etype (Discr)) /= E then
9254 Error_Msg_N
9255 ("all discriminants in an association " &
9256 "must have the same type", Id);
9257 end if;
9259 Next (Id);
9260 end loop;
9261 end if;
9263 Next (Constr);
9264 end loop;
9266 -- A discriminant constraint must provide exactly one value for each
9267 -- discriminant of the type (RM 3.7.1(8)).
9269 for J in Discr_Expr'Range loop
9270 if No (Discr_Expr (J)) then
9271 Error_Msg_N ("too few discriminants given in constraint", C);
9272 return New_Elmt_List;
9273 end if;
9274 end loop;
9276 -- Determine if there are discriminant expressions in the constraint
9278 for J in Discr_Expr'Range loop
9279 if Denotes_Discriminant
9280 (Discr_Expr (J), Check_Concurrent => True)
9281 then
9282 Discrim_Present := True;
9283 end if;
9284 end loop;
9286 -- Build an element list consisting of the expressions given in the
9287 -- discriminant constraint and apply the appropriate checks. The list
9288 -- is constructed after resolving any named discriminant associations
9289 -- and therefore the expressions appear in the textual order of the
9290 -- discriminants.
9292 Discr := First_Discriminant (T);
9293 for J in Discr_Expr'Range loop
9294 if Discr_Expr (J) /= Error then
9295 Append_Elmt (Discr_Expr (J), Elist);
9297 -- If any of the discriminant constraints is given by a
9298 -- discriminant and we are in a derived type declaration we
9299 -- have a discriminant renaming. Establish link between new
9300 -- and old discriminant.
9302 if Denotes_Discriminant (Discr_Expr (J)) then
9303 if Derived_Def then
9304 Set_Corresponding_Discriminant
9305 (Entity (Discr_Expr (J)), Discr);
9306 end if;
9308 -- Force the evaluation of non-discriminant expressions.
9309 -- If we have found a discriminant in the constraint 3.4(26)
9310 -- and 3.8(18) demand that no range checks are performed are
9311 -- after evaluation. If the constraint is for a component
9312 -- definition that has a per-object constraint, expressions are
9313 -- evaluated but not checked either. In all other cases perform
9314 -- a range check.
9316 else
9317 if Discrim_Present then
9318 null;
9320 elsif Nkind (Parent (Parent (Def))) = N_Component_Declaration
9321 and then
9322 Has_Per_Object_Constraint
9323 (Defining_Identifier (Parent (Parent (Def))))
9324 then
9325 null;
9327 elsif Is_Access_Type (Etype (Discr)) then
9328 Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
9330 else
9331 Apply_Range_Check (Discr_Expr (J), Etype (Discr));
9332 end if;
9334 Force_Evaluation (Discr_Expr (J));
9335 end if;
9337 -- Check that the designated type of an access discriminant's
9338 -- expression is not a class-wide type unless the discriminant's
9339 -- designated type is also class-wide.
9341 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
9342 and then not Is_Class_Wide_Type
9343 (Designated_Type (Etype (Discr)))
9344 and then Etype (Discr_Expr (J)) /= Any_Type
9345 and then Is_Class_Wide_Type
9346 (Designated_Type (Etype (Discr_Expr (J))))
9347 then
9348 Wrong_Type (Discr_Expr (J), Etype (Discr));
9350 elsif Is_Access_Type (Etype (Discr))
9351 and then not Is_Access_Constant (Etype (Discr))
9352 and then Is_Access_Type (Etype (Discr_Expr (J)))
9353 and then Is_Access_Constant (Etype (Discr_Expr (J)))
9354 then
9355 Error_Msg_NE
9356 ("constraint for discriminant& must be access to variable",
9357 Def, Discr);
9358 end if;
9359 end if;
9361 Next_Discriminant (Discr);
9362 end loop;
9364 return Elist;
9365 end Build_Discriminant_Constraints;
9367 ---------------------------------
9368 -- Build_Discriminated_Subtype --
9369 ---------------------------------
9371 procedure Build_Discriminated_Subtype
9372 (T : Entity_Id;
9373 Def_Id : Entity_Id;
9374 Elist : Elist_Id;
9375 Related_Nod : Node_Id;
9376 For_Access : Boolean := False)
9378 Has_Discrs : constant Boolean := Has_Discriminants (T);
9379 Constrained : constant Boolean :=
9380 (Has_Discrs
9381 and then not Is_Empty_Elmt_List (Elist)
9382 and then not Is_Class_Wide_Type (T))
9383 or else Is_Constrained (T);
9385 begin
9386 if Ekind (T) = E_Record_Type then
9387 if For_Access then
9388 Set_Ekind (Def_Id, E_Private_Subtype);
9389 Set_Is_For_Access_Subtype (Def_Id, True);
9390 else
9391 Set_Ekind (Def_Id, E_Record_Subtype);
9392 end if;
9394 -- Inherit preelaboration flag from base, for types for which it
9395 -- may have been set: records, private types, protected types.
9397 Set_Known_To_Have_Preelab_Init
9398 (Def_Id, Known_To_Have_Preelab_Init (T));
9400 elsif Ekind (T) = E_Task_Type then
9401 Set_Ekind (Def_Id, E_Task_Subtype);
9403 elsif Ekind (T) = E_Protected_Type then
9404 Set_Ekind (Def_Id, E_Protected_Subtype);
9405 Set_Known_To_Have_Preelab_Init
9406 (Def_Id, Known_To_Have_Preelab_Init (T));
9408 elsif Is_Private_Type (T) then
9409 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
9410 Set_Known_To_Have_Preelab_Init
9411 (Def_Id, Known_To_Have_Preelab_Init (T));
9413 -- Private subtypes may have private dependents
9415 Set_Private_Dependents (Def_Id, New_Elmt_List);
9417 elsif Is_Class_Wide_Type (T) then
9418 Set_Ekind (Def_Id, E_Class_Wide_Subtype);
9420 else
9421 -- Incomplete type. Attach subtype to list of dependents, to be
9422 -- completed with full view of parent type, unless is it the
9423 -- designated subtype of a record component within an init_proc.
9424 -- This last case arises for a component of an access type whose
9425 -- designated type is incomplete (e.g. a Taft Amendment type).
9426 -- The designated subtype is within an inner scope, and needs no
9427 -- elaboration, because only the access type is needed in the
9428 -- initialization procedure.
9430 Set_Ekind (Def_Id, Ekind (T));
9432 if For_Access and then Within_Init_Proc then
9433 null;
9434 else
9435 Append_Elmt (Def_Id, Private_Dependents (T));
9436 end if;
9437 end if;
9439 Set_Etype (Def_Id, T);
9440 Init_Size_Align (Def_Id);
9441 Set_Has_Discriminants (Def_Id, Has_Discrs);
9442 Set_Is_Constrained (Def_Id, Constrained);
9444 Set_First_Entity (Def_Id, First_Entity (T));
9445 Set_Last_Entity (Def_Id, Last_Entity (T));
9446 Set_Has_Implicit_Dereference
9447 (Def_Id, Has_Implicit_Dereference (T));
9449 -- If the subtype is the completion of a private declaration, there may
9450 -- have been representation clauses for the partial view, and they must
9451 -- be preserved. Build_Derived_Type chains the inherited clauses with
9452 -- the ones appearing on the extension. If this comes from a subtype
9453 -- declaration, all clauses are inherited.
9455 if No (First_Rep_Item (Def_Id)) then
9456 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
9457 end if;
9459 if Is_Tagged_Type (T) then
9460 Set_Is_Tagged_Type (Def_Id);
9461 Set_No_Tagged_Streams_Pragma (Def_Id, No_Tagged_Streams_Pragma (T));
9462 Make_Class_Wide_Type (Def_Id);
9463 end if;
9465 Set_Stored_Constraint (Def_Id, No_Elist);
9467 if Has_Discrs then
9468 Set_Discriminant_Constraint (Def_Id, Elist);
9469 Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
9470 end if;
9472 if Is_Tagged_Type (T) then
9474 -- Ada 2005 (AI-251): In case of concurrent types we inherit the
9475 -- concurrent record type (which has the list of primitive
9476 -- operations).
9478 if Ada_Version >= Ada_2005
9479 and then Is_Concurrent_Type (T)
9480 then
9481 Set_Corresponding_Record_Type (Def_Id,
9482 Corresponding_Record_Type (T));
9483 else
9484 Set_Direct_Primitive_Operations (Def_Id,
9485 Direct_Primitive_Operations (T));
9486 end if;
9488 Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
9489 end if;
9491 -- Subtypes introduced by component declarations do not need to be
9492 -- marked as delayed, and do not get freeze nodes, because the semantics
9493 -- verifies that the parents of the subtypes are frozen before the
9494 -- enclosing record is frozen.
9496 if not Is_Type (Scope (Def_Id)) then
9497 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
9499 if Is_Private_Type (T)
9500 and then Present (Full_View (T))
9501 then
9502 Conditional_Delay (Def_Id, Full_View (T));
9503 else
9504 Conditional_Delay (Def_Id, T);
9505 end if;
9506 end if;
9508 if Is_Record_Type (T) then
9509 Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
9511 if Has_Discrs
9512 and then not Is_Empty_Elmt_List (Elist)
9513 and then not For_Access
9514 then
9515 Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
9516 elsif not For_Access then
9517 Set_Cloned_Subtype (Def_Id, T);
9518 end if;
9519 end if;
9520 end Build_Discriminated_Subtype;
9522 ---------------------------
9523 -- Build_Itype_Reference --
9524 ---------------------------
9526 procedure Build_Itype_Reference
9527 (Ityp : Entity_Id;
9528 Nod : Node_Id)
9530 IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
9531 begin
9533 -- Itype references are only created for use by the back-end
9535 if Inside_A_Generic then
9536 return;
9537 else
9538 Set_Itype (IR, Ityp);
9539 Insert_After (Nod, IR);
9540 end if;
9541 end Build_Itype_Reference;
9543 ------------------------
9544 -- Build_Scalar_Bound --
9545 ------------------------
9547 function Build_Scalar_Bound
9548 (Bound : Node_Id;
9549 Par_T : Entity_Id;
9550 Der_T : Entity_Id) return Node_Id
9552 New_Bound : Entity_Id;
9554 begin
9555 -- Note: not clear why this is needed, how can the original bound
9556 -- be unanalyzed at this point? and if it is, what business do we
9557 -- have messing around with it? and why is the base type of the
9558 -- parent type the right type for the resolution. It probably is
9559 -- not. It is OK for the new bound we are creating, but not for
9560 -- the old one??? Still if it never happens, no problem.
9562 Analyze_And_Resolve (Bound, Base_Type (Par_T));
9564 if Nkind_In (Bound, N_Integer_Literal, N_Real_Literal) then
9565 New_Bound := New_Copy (Bound);
9566 Set_Etype (New_Bound, Der_T);
9567 Set_Analyzed (New_Bound);
9569 elsif Is_Entity_Name (Bound) then
9570 New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
9572 -- The following is almost certainly wrong. What business do we have
9573 -- relocating a node (Bound) that is presumably still attached to
9574 -- the tree elsewhere???
9576 else
9577 New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
9578 end if;
9580 Set_Etype (New_Bound, Der_T);
9581 return New_Bound;
9582 end Build_Scalar_Bound;
9584 --------------------------------
9585 -- Build_Underlying_Full_View --
9586 --------------------------------
9588 procedure Build_Underlying_Full_View
9589 (N : Node_Id;
9590 Typ : Entity_Id;
9591 Par : Entity_Id)
9593 Loc : constant Source_Ptr := Sloc (N);
9594 Subt : constant Entity_Id :=
9595 Make_Defining_Identifier
9596 (Loc, New_External_Name (Chars (Typ), 'S'));
9598 Constr : Node_Id;
9599 Indic : Node_Id;
9600 C : Node_Id;
9601 Id : Node_Id;
9603 procedure Set_Discriminant_Name (Id : Node_Id);
9604 -- If the derived type has discriminants, they may rename discriminants
9605 -- of the parent. When building the full view of the parent, we need to
9606 -- recover the names of the original discriminants if the constraint is
9607 -- given by named associations.
9609 ---------------------------
9610 -- Set_Discriminant_Name --
9611 ---------------------------
9613 procedure Set_Discriminant_Name (Id : Node_Id) is
9614 Disc : Entity_Id;
9616 begin
9617 Set_Original_Discriminant (Id, Empty);
9619 if Has_Discriminants (Typ) then
9620 Disc := First_Discriminant (Typ);
9621 while Present (Disc) loop
9622 if Chars (Disc) = Chars (Id)
9623 and then Present (Corresponding_Discriminant (Disc))
9624 then
9625 Set_Chars (Id, Chars (Corresponding_Discriminant (Disc)));
9626 end if;
9627 Next_Discriminant (Disc);
9628 end loop;
9629 end if;
9630 end Set_Discriminant_Name;
9632 -- Start of processing for Build_Underlying_Full_View
9634 begin
9635 if Nkind (N) = N_Full_Type_Declaration then
9636 Constr := Constraint (Subtype_Indication (Type_Definition (N)));
9638 elsif Nkind (N) = N_Subtype_Declaration then
9639 Constr := New_Copy_Tree (Constraint (Subtype_Indication (N)));
9641 elsif Nkind (N) = N_Component_Declaration then
9642 Constr :=
9643 New_Copy_Tree
9644 (Constraint (Subtype_Indication (Component_Definition (N))));
9646 else
9647 raise Program_Error;
9648 end if;
9650 C := First (Constraints (Constr));
9651 while Present (C) loop
9652 if Nkind (C) = N_Discriminant_Association then
9653 Id := First (Selector_Names (C));
9654 while Present (Id) loop
9655 Set_Discriminant_Name (Id);
9656 Next (Id);
9657 end loop;
9658 end if;
9660 Next (C);
9661 end loop;
9663 Indic :=
9664 Make_Subtype_Declaration (Loc,
9665 Defining_Identifier => Subt,
9666 Subtype_Indication =>
9667 Make_Subtype_Indication (Loc,
9668 Subtype_Mark => New_Occurrence_Of (Par, Loc),
9669 Constraint => New_Copy_Tree (Constr)));
9671 -- If this is a component subtype for an outer itype, it is not
9672 -- a list member, so simply set the parent link for analysis: if
9673 -- the enclosing type does not need to be in a declarative list,
9674 -- neither do the components.
9676 if Is_List_Member (N)
9677 and then Nkind (N) /= N_Component_Declaration
9678 then
9679 Insert_Before (N, Indic);
9680 else
9681 Set_Parent (Indic, Parent (N));
9682 end if;
9684 Analyze (Indic);
9685 Set_Underlying_Full_View (Typ, Full_View (Subt));
9686 end Build_Underlying_Full_View;
9688 -------------------------------
9689 -- Check_Abstract_Overriding --
9690 -------------------------------
9692 procedure Check_Abstract_Overriding (T : Entity_Id) is
9693 Alias_Subp : Entity_Id;
9694 Elmt : Elmt_Id;
9695 Op_List : Elist_Id;
9696 Subp : Entity_Id;
9697 Type_Def : Node_Id;
9699 procedure Check_Pragma_Implemented (Subp : Entity_Id);
9700 -- Ada 2012 (AI05-0030): Subprogram Subp overrides an interface routine
9701 -- which has pragma Implemented already set. Check whether Subp's entity
9702 -- kind conforms to the implementation kind of the overridden routine.
9704 procedure Check_Pragma_Implemented
9705 (Subp : Entity_Id;
9706 Iface_Subp : Entity_Id);
9707 -- Ada 2012 (AI05-0030): Subprogram Subp overrides interface routine
9708 -- Iface_Subp and both entities have pragma Implemented already set on
9709 -- them. Check whether the two implementation kinds are conforming.
9711 procedure Inherit_Pragma_Implemented
9712 (Subp : Entity_Id;
9713 Iface_Subp : Entity_Id);
9714 -- Ada 2012 (AI05-0030): Interface primitive Subp overrides interface
9715 -- subprogram Iface_Subp which has been marked by pragma Implemented.
9716 -- Propagate the implementation kind of Iface_Subp to Subp.
9718 ------------------------------
9719 -- Check_Pragma_Implemented --
9720 ------------------------------
9722 procedure Check_Pragma_Implemented (Subp : Entity_Id) is
9723 Iface_Alias : constant Entity_Id := Interface_Alias (Subp);
9724 Impl_Kind : constant Name_Id := Implementation_Kind (Iface_Alias);
9725 Subp_Alias : constant Entity_Id := Alias (Subp);
9726 Contr_Typ : Entity_Id;
9727 Impl_Subp : Entity_Id;
9729 begin
9730 -- Subp must have an alias since it is a hidden entity used to link
9731 -- an interface subprogram to its overriding counterpart.
9733 pragma Assert (Present (Subp_Alias));
9735 -- Handle aliases to synchronized wrappers
9737 Impl_Subp := Subp_Alias;
9739 if Is_Primitive_Wrapper (Impl_Subp) then
9740 Impl_Subp := Wrapped_Entity (Impl_Subp);
9741 end if;
9743 -- Extract the type of the controlling formal
9745 Contr_Typ := Etype (First_Formal (Subp_Alias));
9747 if Is_Concurrent_Record_Type (Contr_Typ) then
9748 Contr_Typ := Corresponding_Concurrent_Type (Contr_Typ);
9749 end if;
9751 -- An interface subprogram whose implementation kind is By_Entry must
9752 -- be implemented by an entry.
9754 if Impl_Kind = Name_By_Entry
9755 and then Ekind (Impl_Subp) /= E_Entry
9756 then
9757 Error_Msg_Node_2 := Iface_Alias;
9758 Error_Msg_NE
9759 ("type & must implement abstract subprogram & with an entry",
9760 Subp_Alias, Contr_Typ);
9762 elsif Impl_Kind = Name_By_Protected_Procedure then
9764 -- An interface subprogram whose implementation kind is By_
9765 -- Protected_Procedure cannot be implemented by a primitive
9766 -- procedure of a task type.
9768 if Ekind (Contr_Typ) /= E_Protected_Type then
9769 Error_Msg_Node_2 := Contr_Typ;
9770 Error_Msg_NE
9771 ("interface subprogram & cannot be implemented by a " &
9772 "primitive procedure of task type &", Subp_Alias,
9773 Iface_Alias);
9775 -- An interface subprogram whose implementation kind is By_
9776 -- Protected_Procedure must be implemented by a procedure.
9778 elsif Ekind (Impl_Subp) /= E_Procedure then
9779 Error_Msg_Node_2 := Iface_Alias;
9780 Error_Msg_NE
9781 ("type & must implement abstract subprogram & with a " &
9782 "procedure", Subp_Alias, Contr_Typ);
9784 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
9785 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
9786 then
9787 Error_Msg_Name_1 := Impl_Kind;
9788 Error_Msg_N
9789 ("overriding operation& must have synchronization%",
9790 Subp_Alias);
9791 end if;
9793 -- If primitive has Optional synchronization, overriding operation
9794 -- must match if it has an explicit synchronization..
9796 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
9797 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
9798 then
9799 Error_Msg_Name_1 := Impl_Kind;
9800 Error_Msg_N
9801 ("overriding operation& must have syncrhonization%",
9802 Subp_Alias);
9803 end if;
9804 end Check_Pragma_Implemented;
9806 ------------------------------
9807 -- Check_Pragma_Implemented --
9808 ------------------------------
9810 procedure Check_Pragma_Implemented
9811 (Subp : Entity_Id;
9812 Iface_Subp : Entity_Id)
9814 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
9815 Subp_Kind : constant Name_Id := Implementation_Kind (Subp);
9817 begin
9818 -- Ada 2012 (AI05-0030): The implementation kinds of an overridden
9819 -- and overriding subprogram are different. In general this is an
9820 -- error except when the implementation kind of the overridden
9821 -- subprograms is By_Any or Optional.
9823 if Iface_Kind /= Subp_Kind
9824 and then Iface_Kind /= Name_By_Any
9825 and then Iface_Kind /= Name_Optional
9826 then
9827 if Iface_Kind = Name_By_Entry then
9828 Error_Msg_N
9829 ("incompatible implementation kind, overridden subprogram " &
9830 "is marked By_Entry", Subp);
9831 else
9832 Error_Msg_N
9833 ("incompatible implementation kind, overridden subprogram " &
9834 "is marked By_Protected_Procedure", Subp);
9835 end if;
9836 end if;
9837 end Check_Pragma_Implemented;
9839 --------------------------------
9840 -- Inherit_Pragma_Implemented --
9841 --------------------------------
9843 procedure Inherit_Pragma_Implemented
9844 (Subp : Entity_Id;
9845 Iface_Subp : Entity_Id)
9847 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
9848 Loc : constant Source_Ptr := Sloc (Subp);
9849 Impl_Prag : Node_Id;
9851 begin
9852 -- Since the implementation kind is stored as a representation item
9853 -- rather than a flag, create a pragma node.
9855 Impl_Prag :=
9856 Make_Pragma (Loc,
9857 Chars => Name_Implemented,
9858 Pragma_Argument_Associations => New_List (
9859 Make_Pragma_Argument_Association (Loc,
9860 Expression => New_Occurrence_Of (Subp, Loc)),
9862 Make_Pragma_Argument_Association (Loc,
9863 Expression => Make_Identifier (Loc, Iface_Kind))));
9865 -- The pragma doesn't need to be analyzed because it is internally
9866 -- built. It is safe to directly register it as a rep item since we
9867 -- are only interested in the characters of the implementation kind.
9869 Record_Rep_Item (Subp, Impl_Prag);
9870 end Inherit_Pragma_Implemented;
9872 -- Start of processing for Check_Abstract_Overriding
9874 begin
9875 Op_List := Primitive_Operations (T);
9877 -- Loop to check primitive operations
9879 Elmt := First_Elmt (Op_List);
9880 while Present (Elmt) loop
9881 Subp := Node (Elmt);
9882 Alias_Subp := Alias (Subp);
9884 -- Inherited subprograms are identified by the fact that they do not
9885 -- come from source, and the associated source location is the
9886 -- location of the first subtype of the derived type.
9888 -- Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
9889 -- subprograms that "require overriding".
9891 -- Special exception, do not complain about failure to override the
9892 -- stream routines _Input and _Output, as well as the primitive
9893 -- operations used in dispatching selects since we always provide
9894 -- automatic overridings for these subprograms.
9896 -- Also ignore this rule for convention CIL since .NET libraries
9897 -- do bizarre things with interfaces???
9899 -- The partial view of T may have been a private extension, for
9900 -- which inherited functions dispatching on result are abstract.
9901 -- If the full view is a null extension, there is no need for
9902 -- overriding in Ada 2005, but wrappers need to be built for them
9903 -- (see exp_ch3, Build_Controlling_Function_Wrappers).
9905 if Is_Null_Extension (T)
9906 and then Has_Controlling_Result (Subp)
9907 and then Ada_Version >= Ada_2005
9908 and then Present (Alias_Subp)
9909 and then not Comes_From_Source (Subp)
9910 and then not Is_Abstract_Subprogram (Alias_Subp)
9911 and then not Is_Access_Type (Etype (Subp))
9912 then
9913 null;
9915 -- Ada 2005 (AI-251): Internal entities of interfaces need no
9916 -- processing because this check is done with the aliased
9917 -- entity
9919 elsif Present (Interface_Alias (Subp)) then
9920 null;
9922 elsif (Is_Abstract_Subprogram (Subp)
9923 or else Requires_Overriding (Subp)
9924 or else
9925 (Has_Controlling_Result (Subp)
9926 and then Present (Alias_Subp)
9927 and then not Comes_From_Source (Subp)
9928 and then Sloc (Subp) = Sloc (First_Subtype (T))))
9929 and then not Is_TSS (Subp, TSS_Stream_Input)
9930 and then not Is_TSS (Subp, TSS_Stream_Output)
9931 and then not Is_Abstract_Type (T)
9932 and then Convention (T) /= Convention_CIL
9933 and then not Is_Predefined_Interface_Primitive (Subp)
9935 -- Ada 2005 (AI-251): Do not consider hidden entities associated
9936 -- with abstract interface types because the check will be done
9937 -- with the aliased entity (otherwise we generate a duplicated
9938 -- error message).
9940 and then not Present (Interface_Alias (Subp))
9941 then
9942 if Present (Alias_Subp) then
9944 -- Only perform the check for a derived subprogram when the
9945 -- type has an explicit record extension. This avoids incorrect
9946 -- flagging of abstract subprograms for the case of a type
9947 -- without an extension that is derived from a formal type
9948 -- with a tagged actual (can occur within a private part).
9950 -- Ada 2005 (AI-391): In the case of an inherited function with
9951 -- a controlling result of the type, the rule does not apply if
9952 -- the type is a null extension (unless the parent function
9953 -- itself is abstract, in which case the function must still be
9954 -- be overridden). The expander will generate an overriding
9955 -- wrapper function calling the parent subprogram (see
9956 -- Exp_Ch3.Make_Controlling_Wrapper_Functions).
9958 Type_Def := Type_Definition (Parent (T));
9960 if Nkind (Type_Def) = N_Derived_Type_Definition
9961 and then Present (Record_Extension_Part (Type_Def))
9962 and then
9963 (Ada_Version < Ada_2005
9964 or else not Is_Null_Extension (T)
9965 or else Ekind (Subp) = E_Procedure
9966 or else not Has_Controlling_Result (Subp)
9967 or else Is_Abstract_Subprogram (Alias_Subp)
9968 or else Requires_Overriding (Subp)
9969 or else Is_Access_Type (Etype (Subp)))
9970 then
9971 -- Avoid reporting error in case of abstract predefined
9972 -- primitive inherited from interface type because the
9973 -- body of internally generated predefined primitives
9974 -- of tagged types are generated later by Freeze_Type
9976 if Is_Interface (Root_Type (T))
9977 and then Is_Abstract_Subprogram (Subp)
9978 and then Is_Predefined_Dispatching_Operation (Subp)
9979 and then not Comes_From_Source (Ultimate_Alias (Subp))
9980 then
9981 null;
9983 -- A null extension is not obliged to override an inherited
9984 -- procedure subject to pragma Extensions_Visible with value
9985 -- False and at least one controlling OUT parameter
9986 -- (SPARK RM 6.1.7(6)).
9988 elsif Is_Null_Extension (T)
9989 and then Is_EVF_Procedure (Subp)
9990 then
9991 null;
9993 else
9994 Error_Msg_NE
9995 ("type must be declared abstract or & overridden",
9996 T, Subp);
9998 -- Traverse the whole chain of aliased subprograms to
9999 -- complete the error notification. This is especially
10000 -- useful for traceability of the chain of entities when
10001 -- the subprogram corresponds with an interface
10002 -- subprogram (which may be defined in another package).
10004 if Present (Alias_Subp) then
10005 declare
10006 E : Entity_Id;
10008 begin
10009 E := Subp;
10010 while Present (Alias (E)) loop
10012 -- Avoid reporting redundant errors on entities
10013 -- inherited from interfaces
10015 if Sloc (E) /= Sloc (T) then
10016 Error_Msg_Sloc := Sloc (E);
10017 Error_Msg_NE
10018 ("\& has been inherited #", T, Subp);
10019 end if;
10021 E := Alias (E);
10022 end loop;
10024 Error_Msg_Sloc := Sloc (E);
10026 -- AI05-0068: report if there is an overriding
10027 -- non-abstract subprogram that is invisible.
10029 if Is_Hidden (E)
10030 and then not Is_Abstract_Subprogram (E)
10031 then
10032 Error_Msg_NE
10033 ("\& subprogram# is not visible",
10034 T, Subp);
10036 -- Clarify the case where a non-null extension must
10037 -- override inherited procedure subject to pragma
10038 -- Extensions_Visible with value False and at least
10039 -- one controlling OUT param.
10041 elsif Is_EVF_Procedure (E) then
10042 Error_Msg_NE
10043 ("\& # is subject to Extensions_Visible False",
10044 T, Subp);
10046 else
10047 Error_Msg_NE
10048 ("\& has been inherited from subprogram #",
10049 T, Subp);
10050 end if;
10051 end;
10052 end if;
10053 end if;
10055 -- Ada 2005 (AI-345): Protected or task type implementing
10056 -- abstract interfaces.
10058 elsif Is_Concurrent_Record_Type (T)
10059 and then Present (Interfaces (T))
10060 then
10061 -- There is no need to check here RM 9.4(11.9/3) since we
10062 -- are processing the corresponding record type and the
10063 -- mode of the overriding subprograms was verified by
10064 -- Check_Conformance when the corresponding concurrent
10065 -- type declaration was analyzed.
10067 Error_Msg_NE
10068 ("interface subprogram & must be overridden", T, Subp);
10070 -- Examine primitive operations of synchronized type to find
10071 -- homonyms that have the wrong profile.
10073 declare
10074 Prim : Entity_Id;
10076 begin
10077 Prim := First_Entity (Corresponding_Concurrent_Type (T));
10078 while Present (Prim) loop
10079 if Chars (Prim) = Chars (Subp) then
10080 Error_Msg_NE
10081 ("profile is not type conformant with prefixed "
10082 & "view profile of inherited operation&",
10083 Prim, Subp);
10084 end if;
10086 Next_Entity (Prim);
10087 end loop;
10088 end;
10089 end if;
10091 else
10092 Error_Msg_Node_2 := T;
10093 Error_Msg_N
10094 ("abstract subprogram& not allowed for type&", Subp);
10096 -- Also post unconditional warning on the type (unconditional
10097 -- so that if there are more than one of these cases, we get
10098 -- them all, and not just the first one).
10100 Error_Msg_Node_2 := Subp;
10101 Error_Msg_N ("nonabstract type& has abstract subprogram&!", T);
10102 end if;
10104 -- A subprogram subject to pragma Extensions_Visible with value
10105 -- "True" cannot override a subprogram subject to the same pragma
10106 -- with value "False" (SPARK RM 6.1.7(5)).
10108 elsif Extensions_Visible_Status (Subp) = Extensions_Visible_True
10109 and then Present (Overridden_Operation (Subp))
10110 and then Extensions_Visible_Status (Overridden_Operation (Subp)) =
10111 Extensions_Visible_False
10112 then
10113 Error_Msg_Sloc := Sloc (Overridden_Operation (Subp));
10114 Error_Msg_N
10115 ("subprogram & with Extensions_Visible True cannot override "
10116 & "subprogram # with Extensions_Visible False", Subp);
10117 end if;
10119 -- Ada 2012 (AI05-0030): Perform checks related to pragma Implemented
10121 -- Subp is an expander-generated procedure which maps an interface
10122 -- alias to a protected wrapper. The interface alias is flagged by
10123 -- pragma Implemented. Ensure that Subp is a procedure when the
10124 -- implementation kind is By_Protected_Procedure or an entry when
10125 -- By_Entry.
10127 if Ada_Version >= Ada_2012
10128 and then Is_Hidden (Subp)
10129 and then Present (Interface_Alias (Subp))
10130 and then Has_Rep_Pragma (Interface_Alias (Subp), Name_Implemented)
10131 then
10132 Check_Pragma_Implemented (Subp);
10133 end if;
10135 -- Subp is an interface primitive which overrides another interface
10136 -- primitive marked with pragma Implemented.
10138 if Ada_Version >= Ada_2012
10139 and then Present (Overridden_Operation (Subp))
10140 and then Has_Rep_Pragma
10141 (Overridden_Operation (Subp), Name_Implemented)
10142 then
10143 -- If the overriding routine is also marked by Implemented, check
10144 -- that the two implementation kinds are conforming.
10146 if Has_Rep_Pragma (Subp, Name_Implemented) then
10147 Check_Pragma_Implemented
10148 (Subp => Subp,
10149 Iface_Subp => Overridden_Operation (Subp));
10151 -- Otherwise the overriding routine inherits the implementation
10152 -- kind from the overridden subprogram.
10154 else
10155 Inherit_Pragma_Implemented
10156 (Subp => Subp,
10157 Iface_Subp => Overridden_Operation (Subp));
10158 end if;
10159 end if;
10161 -- If the operation is a wrapper for a synchronized primitive, it
10162 -- may be called indirectly through a dispatching select. We assume
10163 -- that it will be referenced elsewhere indirectly, and suppress
10164 -- warnings about an unused entity.
10166 if Is_Primitive_Wrapper (Subp)
10167 and then Present (Wrapped_Entity (Subp))
10168 then
10169 Set_Referenced (Wrapped_Entity (Subp));
10170 end if;
10172 Next_Elmt (Elmt);
10173 end loop;
10174 end Check_Abstract_Overriding;
10176 ------------------------------------------------
10177 -- Check_Access_Discriminant_Requires_Limited --
10178 ------------------------------------------------
10180 procedure Check_Access_Discriminant_Requires_Limited
10181 (D : Node_Id;
10182 Loc : Node_Id)
10184 begin
10185 -- A discriminant_specification for an access discriminant shall appear
10186 -- only in the declaration for a task or protected type, or for a type
10187 -- with the reserved word 'limited' in its definition or in one of its
10188 -- ancestors (RM 3.7(10)).
10190 -- AI-0063: The proper condition is that type must be immutably limited,
10191 -- or else be a partial view.
10193 if Nkind (Discriminant_Type (D)) = N_Access_Definition then
10194 if Is_Limited_View (Current_Scope)
10195 or else
10196 (Nkind (Parent (Current_Scope)) = N_Private_Type_Declaration
10197 and then Limited_Present (Parent (Current_Scope)))
10198 then
10199 null;
10201 else
10202 Error_Msg_N
10203 ("access discriminants allowed only for limited types", Loc);
10204 end if;
10205 end if;
10206 end Check_Access_Discriminant_Requires_Limited;
10208 -----------------------------------
10209 -- Check_Aliased_Component_Types --
10210 -----------------------------------
10212 procedure Check_Aliased_Component_Types (T : Entity_Id) is
10213 C : Entity_Id;
10215 begin
10216 -- ??? Also need to check components of record extensions, but not
10217 -- components of protected types (which are always limited).
10219 -- Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
10220 -- types to be unconstrained. This is safe because it is illegal to
10221 -- create access subtypes to such types with explicit discriminant
10222 -- constraints.
10224 if not Is_Limited_Type (T) then
10225 if Ekind (T) = E_Record_Type then
10226 C := First_Component (T);
10227 while Present (C) loop
10228 if Is_Aliased (C)
10229 and then Has_Discriminants (Etype (C))
10230 and then not Is_Constrained (Etype (C))
10231 and then not In_Instance_Body
10232 and then Ada_Version < Ada_2005
10233 then
10234 Error_Msg_N
10235 ("aliased component must be constrained (RM 3.6(11))",
10237 end if;
10239 Next_Component (C);
10240 end loop;
10242 elsif Ekind (T) = E_Array_Type then
10243 if Has_Aliased_Components (T)
10244 and then Has_Discriminants (Component_Type (T))
10245 and then not Is_Constrained (Component_Type (T))
10246 and then not In_Instance_Body
10247 and then Ada_Version < Ada_2005
10248 then
10249 Error_Msg_N
10250 ("aliased component type must be constrained (RM 3.6(11))",
10252 end if;
10253 end if;
10254 end if;
10255 end Check_Aliased_Component_Types;
10257 ---------------------------------------
10258 -- Check_Anonymous_Access_Components --
10259 ---------------------------------------
10261 procedure Check_Anonymous_Access_Components
10262 (Typ_Decl : Node_Id;
10263 Typ : Entity_Id;
10264 Prev : Entity_Id;
10265 Comp_List : Node_Id)
10267 Loc : constant Source_Ptr := Sloc (Typ_Decl);
10268 Anon_Access : Entity_Id;
10269 Acc_Def : Node_Id;
10270 Comp : Node_Id;
10271 Comp_Def : Node_Id;
10272 Decl : Node_Id;
10273 Type_Def : Node_Id;
10275 procedure Build_Incomplete_Type_Declaration;
10276 -- If the record type contains components that include an access to the
10277 -- current record, then create an incomplete type declaration for the
10278 -- record, to be used as the designated type of the anonymous access.
10279 -- This is done only once, and only if there is no previous partial
10280 -- view of the type.
10282 function Designates_T (Subt : Node_Id) return Boolean;
10283 -- Check whether a node designates the enclosing record type, or 'Class
10284 -- of that type
10286 function Mentions_T (Acc_Def : Node_Id) return Boolean;
10287 -- Check whether an access definition includes a reference to
10288 -- the enclosing record type. The reference can be a subtype mark
10289 -- in the access definition itself, a 'Class attribute reference, or
10290 -- recursively a reference appearing in a parameter specification
10291 -- or result definition of an access_to_subprogram definition.
10293 --------------------------------------
10294 -- Build_Incomplete_Type_Declaration --
10295 --------------------------------------
10297 procedure Build_Incomplete_Type_Declaration is
10298 Decl : Node_Id;
10299 Inc_T : Entity_Id;
10300 H : Entity_Id;
10302 -- Is_Tagged indicates whether the type is tagged. It is tagged if
10303 -- it's "is new ... with record" or else "is tagged record ...".
10305 Is_Tagged : constant Boolean :=
10306 (Nkind (Type_Definition (Typ_Decl)) = N_Derived_Type_Definition
10307 and then
10308 Present (Record_Extension_Part (Type_Definition (Typ_Decl))))
10309 or else
10310 (Nkind (Type_Definition (Typ_Decl)) = N_Record_Definition
10311 and then Tagged_Present (Type_Definition (Typ_Decl)));
10313 begin
10314 -- If there is a previous partial view, no need to create a new one
10315 -- If the partial view, given by Prev, is incomplete, If Prev is
10316 -- a private declaration, full declaration is flagged accordingly.
10318 if Prev /= Typ then
10319 if Is_Tagged then
10320 Make_Class_Wide_Type (Prev);
10321 Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
10322 Set_Etype (Class_Wide_Type (Typ), Typ);
10323 end if;
10325 return;
10327 elsif Has_Private_Declaration (Typ) then
10329 -- If we refer to T'Class inside T, and T is the completion of a
10330 -- private type, then make sure the class-wide type exists.
10332 if Is_Tagged then
10333 Make_Class_Wide_Type (Typ);
10334 end if;
10336 return;
10338 -- If there was a previous anonymous access type, the incomplete
10339 -- type declaration will have been created already.
10341 elsif Present (Current_Entity (Typ))
10342 and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
10343 and then Full_View (Current_Entity (Typ)) = Typ
10344 then
10345 if Is_Tagged
10346 and then Comes_From_Source (Current_Entity (Typ))
10347 and then not Is_Tagged_Type (Current_Entity (Typ))
10348 then
10349 Make_Class_Wide_Type (Typ);
10350 Error_Msg_N
10351 ("incomplete view of tagged type should be declared tagged??",
10352 Parent (Current_Entity (Typ)));
10353 end if;
10354 return;
10356 else
10357 Inc_T := Make_Defining_Identifier (Loc, Chars (Typ));
10358 Decl := Make_Incomplete_Type_Declaration (Loc, Inc_T);
10360 -- Type has already been inserted into the current scope. Remove
10361 -- it, and add incomplete declaration for type, so that subsequent
10362 -- anonymous access types can use it. The entity is unchained from
10363 -- the homonym list and from immediate visibility. After analysis,
10364 -- the entity in the incomplete declaration becomes immediately
10365 -- visible in the record declaration that follows.
10367 H := Current_Entity (Typ);
10369 if H = Typ then
10370 Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
10371 else
10372 while Present (H)
10373 and then Homonym (H) /= Typ
10374 loop
10375 H := Homonym (Typ);
10376 end loop;
10378 Set_Homonym (H, Homonym (Typ));
10379 end if;
10381 Insert_Before (Typ_Decl, Decl);
10382 Analyze (Decl);
10383 Set_Full_View (Inc_T, Typ);
10385 if Is_Tagged then
10387 -- Create a common class-wide type for both views, and set the
10388 -- Etype of the class-wide type to the full view.
10390 Make_Class_Wide_Type (Inc_T);
10391 Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
10392 Set_Etype (Class_Wide_Type (Typ), Typ);
10393 end if;
10394 end if;
10395 end Build_Incomplete_Type_Declaration;
10397 ------------------
10398 -- Designates_T --
10399 ------------------
10401 function Designates_T (Subt : Node_Id) return Boolean is
10402 Type_Id : constant Name_Id := Chars (Typ);
10404 function Names_T (Nam : Node_Id) return Boolean;
10405 -- The record type has not been introduced in the current scope
10406 -- yet, so we must examine the name of the type itself, either
10407 -- an identifier T, or an expanded name of the form P.T, where
10408 -- P denotes the current scope.
10410 -------------
10411 -- Names_T --
10412 -------------
10414 function Names_T (Nam : Node_Id) return Boolean is
10415 begin
10416 if Nkind (Nam) = N_Identifier then
10417 return Chars (Nam) = Type_Id;
10419 elsif Nkind (Nam) = N_Selected_Component then
10420 if Chars (Selector_Name (Nam)) = Type_Id then
10421 if Nkind (Prefix (Nam)) = N_Identifier then
10422 return Chars (Prefix (Nam)) = Chars (Current_Scope);
10424 elsif Nkind (Prefix (Nam)) = N_Selected_Component then
10425 return Chars (Selector_Name (Prefix (Nam))) =
10426 Chars (Current_Scope);
10427 else
10428 return False;
10429 end if;
10431 else
10432 return False;
10433 end if;
10435 else
10436 return False;
10437 end if;
10438 end Names_T;
10440 -- Start of processing for Designates_T
10442 begin
10443 if Nkind (Subt) = N_Identifier then
10444 return Chars (Subt) = Type_Id;
10446 -- Reference can be through an expanded name which has not been
10447 -- analyzed yet, and which designates enclosing scopes.
10449 elsif Nkind (Subt) = N_Selected_Component then
10450 if Names_T (Subt) then
10451 return True;
10453 -- Otherwise it must denote an entity that is already visible.
10454 -- The access definition may name a subtype of the enclosing
10455 -- type, if there is a previous incomplete declaration for it.
10457 else
10458 Find_Selected_Component (Subt);
10459 return
10460 Is_Entity_Name (Subt)
10461 and then Scope (Entity (Subt)) = Current_Scope
10462 and then
10463 (Chars (Base_Type (Entity (Subt))) = Type_Id
10464 or else
10465 (Is_Class_Wide_Type (Entity (Subt))
10466 and then
10467 Chars (Etype (Base_Type (Entity (Subt)))) =
10468 Type_Id));
10469 end if;
10471 -- A reference to the current type may appear as the prefix of
10472 -- a 'Class attribute.
10474 elsif Nkind (Subt) = N_Attribute_Reference
10475 and then Attribute_Name (Subt) = Name_Class
10476 then
10477 return Names_T (Prefix (Subt));
10479 else
10480 return False;
10481 end if;
10482 end Designates_T;
10484 ----------------
10485 -- Mentions_T --
10486 ----------------
10488 function Mentions_T (Acc_Def : Node_Id) return Boolean is
10489 Param_Spec : Node_Id;
10491 Acc_Subprg : constant Node_Id :=
10492 Access_To_Subprogram_Definition (Acc_Def);
10494 begin
10495 if No (Acc_Subprg) then
10496 return Designates_T (Subtype_Mark (Acc_Def));
10497 end if;
10499 -- Component is an access_to_subprogram: examine its formals,
10500 -- and result definition in the case of an access_to_function.
10502 Param_Spec := First (Parameter_Specifications (Acc_Subprg));
10503 while Present (Param_Spec) loop
10504 if Nkind (Parameter_Type (Param_Spec)) = N_Access_Definition
10505 and then Mentions_T (Parameter_Type (Param_Spec))
10506 then
10507 return True;
10509 elsif Designates_T (Parameter_Type (Param_Spec)) then
10510 return True;
10511 end if;
10513 Next (Param_Spec);
10514 end loop;
10516 if Nkind (Acc_Subprg) = N_Access_Function_Definition then
10517 if Nkind (Result_Definition (Acc_Subprg)) =
10518 N_Access_Definition
10519 then
10520 return Mentions_T (Result_Definition (Acc_Subprg));
10521 else
10522 return Designates_T (Result_Definition (Acc_Subprg));
10523 end if;
10524 end if;
10526 return False;
10527 end Mentions_T;
10529 -- Start of processing for Check_Anonymous_Access_Components
10531 begin
10532 if No (Comp_List) then
10533 return;
10534 end if;
10536 Comp := First (Component_Items (Comp_List));
10537 while Present (Comp) loop
10538 if Nkind (Comp) = N_Component_Declaration
10539 and then Present
10540 (Access_Definition (Component_Definition (Comp)))
10541 and then
10542 Mentions_T (Access_Definition (Component_Definition (Comp)))
10543 then
10544 Comp_Def := Component_Definition (Comp);
10545 Acc_Def :=
10546 Access_To_Subprogram_Definition (Access_Definition (Comp_Def));
10548 Build_Incomplete_Type_Declaration;
10549 Anon_Access := Make_Temporary (Loc, 'S');
10551 -- Create a declaration for the anonymous access type: either
10552 -- an access_to_object or an access_to_subprogram.
10554 if Present (Acc_Def) then
10555 if Nkind (Acc_Def) = N_Access_Function_Definition then
10556 Type_Def :=
10557 Make_Access_Function_Definition (Loc,
10558 Parameter_Specifications =>
10559 Parameter_Specifications (Acc_Def),
10560 Result_Definition => Result_Definition (Acc_Def));
10561 else
10562 Type_Def :=
10563 Make_Access_Procedure_Definition (Loc,
10564 Parameter_Specifications =>
10565 Parameter_Specifications (Acc_Def));
10566 end if;
10568 else
10569 Type_Def :=
10570 Make_Access_To_Object_Definition (Loc,
10571 Subtype_Indication =>
10572 Relocate_Node
10573 (Subtype_Mark (Access_Definition (Comp_Def))));
10575 Set_Constant_Present
10576 (Type_Def, Constant_Present (Access_Definition (Comp_Def)));
10577 Set_All_Present
10578 (Type_Def, All_Present (Access_Definition (Comp_Def)));
10579 end if;
10581 Set_Null_Exclusion_Present
10582 (Type_Def,
10583 Null_Exclusion_Present (Access_Definition (Comp_Def)));
10585 Decl :=
10586 Make_Full_Type_Declaration (Loc,
10587 Defining_Identifier => Anon_Access,
10588 Type_Definition => Type_Def);
10590 Insert_Before (Typ_Decl, Decl);
10591 Analyze (Decl);
10593 -- If an access to subprogram, create the extra formals
10595 if Present (Acc_Def) then
10596 Create_Extra_Formals (Designated_Type (Anon_Access));
10598 -- If an access to object, preserve entity of designated type,
10599 -- for ASIS use, before rewriting the component definition.
10601 else
10602 declare
10603 Desig : Entity_Id;
10605 begin
10606 Desig := Entity (Subtype_Indication (Type_Def));
10608 -- If the access definition is to the current record,
10609 -- the visible entity at this point is an incomplete
10610 -- type. Retrieve the full view to simplify ASIS queries
10612 if Ekind (Desig) = E_Incomplete_Type then
10613 Desig := Full_View (Desig);
10614 end if;
10616 Set_Entity
10617 (Subtype_Mark (Access_Definition (Comp_Def)), Desig);
10618 end;
10619 end if;
10621 Rewrite (Comp_Def,
10622 Make_Component_Definition (Loc,
10623 Subtype_Indication =>
10624 New_Occurrence_Of (Anon_Access, Loc)));
10626 if Ekind (Designated_Type (Anon_Access)) = E_Subprogram_Type then
10627 Set_Ekind (Anon_Access, E_Anonymous_Access_Subprogram_Type);
10628 else
10629 Set_Ekind (Anon_Access, E_Anonymous_Access_Type);
10630 end if;
10632 Set_Is_Local_Anonymous_Access (Anon_Access);
10633 end if;
10635 Next (Comp);
10636 end loop;
10638 if Present (Variant_Part (Comp_List)) then
10639 declare
10640 V : Node_Id;
10641 begin
10642 V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
10643 while Present (V) loop
10644 Check_Anonymous_Access_Components
10645 (Typ_Decl, Typ, Prev, Component_List (V));
10646 Next_Non_Pragma (V);
10647 end loop;
10648 end;
10649 end if;
10650 end Check_Anonymous_Access_Components;
10652 ----------------------
10653 -- Check_Completion --
10654 ----------------------
10656 procedure Check_Completion (Body_Id : Node_Id := Empty) is
10657 E : Entity_Id;
10659 procedure Post_Error;
10660 -- Post error message for lack of completion for entity E
10662 ----------------
10663 -- Post_Error --
10664 ----------------
10666 procedure Post_Error is
10668 procedure Missing_Body;
10669 -- Output missing body message
10671 ------------------
10672 -- Missing_Body --
10673 ------------------
10675 procedure Missing_Body is
10676 begin
10677 -- Spec is in same unit, so we can post on spec
10679 if In_Same_Source_Unit (Body_Id, E) then
10680 Error_Msg_N ("missing body for &", E);
10682 -- Spec is in a separate unit, so we have to post on the body
10684 else
10685 Error_Msg_NE ("missing body for & declared#!", Body_Id, E);
10686 end if;
10687 end Missing_Body;
10689 -- Start of processing for Post_Error
10691 begin
10692 if not Comes_From_Source (E) then
10694 if Ekind_In (E, E_Task_Type, E_Protected_Type) then
10696 -- It may be an anonymous protected type created for a
10697 -- single variable. Post error on variable, if present.
10699 declare
10700 Var : Entity_Id;
10702 begin
10703 Var := First_Entity (Current_Scope);
10704 while Present (Var) loop
10705 exit when Etype (Var) = E
10706 and then Comes_From_Source (Var);
10708 Next_Entity (Var);
10709 end loop;
10711 if Present (Var) then
10712 E := Var;
10713 end if;
10714 end;
10715 end if;
10716 end if;
10718 -- If a generated entity has no completion, then either previous
10719 -- semantic errors have disabled the expansion phase, or else we had
10720 -- missing subunits, or else we are compiling without expansion,
10721 -- or else something is very wrong.
10723 if not Comes_From_Source (E) then
10724 pragma Assert
10725 (Serious_Errors_Detected > 0
10726 or else Configurable_Run_Time_Violations > 0
10727 or else Subunits_Missing
10728 or else not Expander_Active);
10729 return;
10731 -- Here for source entity
10733 else
10734 -- Here if no body to post the error message, so we post the error
10735 -- on the declaration that has no completion. This is not really
10736 -- the right place to post it, think about this later ???
10738 if No (Body_Id) then
10739 if Is_Type (E) then
10740 Error_Msg_NE
10741 ("missing full declaration for }", Parent (E), E);
10742 else
10743 Error_Msg_NE ("missing body for &", Parent (E), E);
10744 end if;
10746 -- Package body has no completion for a declaration that appears
10747 -- in the corresponding spec. Post error on the body, with a
10748 -- reference to the non-completed declaration.
10750 else
10751 Error_Msg_Sloc := Sloc (E);
10753 if Is_Type (E) then
10754 Error_Msg_NE ("missing full declaration for }!", Body_Id, E);
10756 elsif Is_Overloadable (E)
10757 and then Current_Entity_In_Scope (E) /= E
10758 then
10759 -- It may be that the completion is mistyped and appears as
10760 -- a distinct overloading of the entity.
10762 declare
10763 Candidate : constant Entity_Id :=
10764 Current_Entity_In_Scope (E);
10765 Decl : constant Node_Id :=
10766 Unit_Declaration_Node (Candidate);
10768 begin
10769 if Is_Overloadable (Candidate)
10770 and then Ekind (Candidate) = Ekind (E)
10771 and then Nkind (Decl) = N_Subprogram_Body
10772 and then Acts_As_Spec (Decl)
10773 then
10774 Check_Type_Conformant (Candidate, E);
10776 else
10777 Missing_Body;
10778 end if;
10779 end;
10781 else
10782 Missing_Body;
10783 end if;
10784 end if;
10785 end if;
10786 end Post_Error;
10788 -- Local variables
10790 Pack_Id : constant Entity_Id := Current_Scope;
10792 -- Start of processing for Check_Completion
10794 begin
10795 E := First_Entity (Pack_Id);
10796 while Present (E) loop
10797 if Is_Intrinsic_Subprogram (E) then
10798 null;
10800 -- A Ghost entity declared in a non-Ghost package does not force the
10801 -- need for a body (SPARK RM 6.9(11)).
10803 elsif not Is_Ghost_Entity (Pack_Id) and then Is_Ghost_Entity (E) then
10804 null;
10806 -- The following situation requires special handling: a child unit
10807 -- that appears in the context clause of the body of its parent:
10809 -- procedure Parent.Child (...);
10811 -- with Parent.Child;
10812 -- package body Parent is
10814 -- Here Parent.Child appears as a local entity, but should not be
10815 -- flagged as requiring completion, because it is a compilation
10816 -- unit.
10818 -- Ignore missing completion for a subprogram that does not come from
10819 -- source (including the _Call primitive operation of RAS types,
10820 -- which has to have the flag Comes_From_Source for other purposes):
10821 -- we assume that the expander will provide the missing completion.
10822 -- In case of previous errors, other expansion actions that provide
10823 -- bodies for null procedures with not be invoked, so inhibit message
10824 -- in those cases.
10826 -- Note that E_Operator is not in the list that follows, because
10827 -- this kind is reserved for predefined operators, that are
10828 -- intrinsic and do not need completion.
10830 elsif Ekind_In (E, E_Function,
10831 E_Procedure,
10832 E_Generic_Function,
10833 E_Generic_Procedure)
10834 then
10835 if Has_Completion (E) then
10836 null;
10838 elsif Is_Subprogram (E) and then Is_Abstract_Subprogram (E) then
10839 null;
10841 elsif Is_Subprogram (E)
10842 and then (not Comes_From_Source (E)
10843 or else Chars (E) = Name_uCall)
10844 then
10845 null;
10847 elsif
10848 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
10849 then
10850 null;
10852 elsif Nkind (Parent (E)) = N_Procedure_Specification
10853 and then Null_Present (Parent (E))
10854 and then Serious_Errors_Detected > 0
10855 then
10856 null;
10858 else
10859 Post_Error;
10860 end if;
10862 elsif Is_Entry (E) then
10863 if not Has_Completion (E) and then
10864 (Ekind (Scope (E)) = E_Protected_Object
10865 or else Ekind (Scope (E)) = E_Protected_Type)
10866 then
10867 Post_Error;
10868 end if;
10870 elsif Is_Package_Or_Generic_Package (E) then
10871 if Unit_Requires_Body (E) then
10872 if not Has_Completion (E)
10873 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
10874 N_Compilation_Unit
10875 then
10876 Post_Error;
10877 end if;
10879 elsif not Is_Child_Unit (E) then
10880 May_Need_Implicit_Body (E);
10881 end if;
10883 -- A formal incomplete type (Ada 2012) does not require a completion;
10884 -- other incomplete type declarations do.
10886 elsif Ekind (E) = E_Incomplete_Type
10887 and then No (Underlying_Type (E))
10888 and then not Is_Generic_Type (E)
10889 then
10890 Post_Error;
10892 elsif Ekind_In (E, E_Task_Type, E_Protected_Type)
10893 and then not Has_Completion (E)
10894 then
10895 Post_Error;
10897 -- A single task declared in the current scope is a constant, verify
10898 -- that the body of its anonymous type is in the same scope. If the
10899 -- task is defined elsewhere, this may be a renaming declaration for
10900 -- which no completion is needed.
10902 elsif Ekind (E) = E_Constant
10903 and then Ekind (Etype (E)) = E_Task_Type
10904 and then not Has_Completion (Etype (E))
10905 and then Scope (Etype (E)) = Current_Scope
10906 then
10907 Post_Error;
10909 elsif Ekind (E) = E_Protected_Object
10910 and then not Has_Completion (Etype (E))
10911 then
10912 Post_Error;
10914 elsif Ekind (E) = E_Record_Type then
10915 if Is_Tagged_Type (E) then
10916 Check_Abstract_Overriding (E);
10917 Check_Conventions (E);
10918 end if;
10920 Check_Aliased_Component_Types (E);
10922 elsif Ekind (E) = E_Array_Type then
10923 Check_Aliased_Component_Types (E);
10925 end if;
10927 Next_Entity (E);
10928 end loop;
10929 end Check_Completion;
10931 ------------------------------------
10932 -- Check_CPP_Type_Has_No_Defaults --
10933 ------------------------------------
10935 procedure Check_CPP_Type_Has_No_Defaults (T : Entity_Id) is
10936 Tdef : constant Node_Id := Type_Definition (Declaration_Node (T));
10937 Clist : Node_Id;
10938 Comp : Node_Id;
10940 begin
10941 -- Obtain the component list
10943 if Nkind (Tdef) = N_Record_Definition then
10944 Clist := Component_List (Tdef);
10945 else pragma Assert (Nkind (Tdef) = N_Derived_Type_Definition);
10946 Clist := Component_List (Record_Extension_Part (Tdef));
10947 end if;
10949 -- Check all components to ensure no default expressions
10951 if Present (Clist) then
10952 Comp := First (Component_Items (Clist));
10953 while Present (Comp) loop
10954 if Present (Expression (Comp)) then
10955 Error_Msg_N
10956 ("component of imported 'C'P'P type cannot have "
10957 & "default expression", Expression (Comp));
10958 end if;
10960 Next (Comp);
10961 end loop;
10962 end if;
10963 end Check_CPP_Type_Has_No_Defaults;
10965 ----------------------------
10966 -- Check_Delta_Expression --
10967 ----------------------------
10969 procedure Check_Delta_Expression (E : Node_Id) is
10970 begin
10971 if not (Is_Real_Type (Etype (E))) then
10972 Wrong_Type (E, Any_Real);
10974 elsif not Is_OK_Static_Expression (E) then
10975 Flag_Non_Static_Expr
10976 ("non-static expression used for delta value!", E);
10978 elsif not UR_Is_Positive (Expr_Value_R (E)) then
10979 Error_Msg_N ("delta expression must be positive", E);
10981 else
10982 return;
10983 end if;
10985 -- If any of above errors occurred, then replace the incorrect
10986 -- expression by the real 0.1, which should prevent further errors.
10988 Rewrite (E,
10989 Make_Real_Literal (Sloc (E), Ureal_Tenth));
10990 Analyze_And_Resolve (E, Standard_Float);
10991 end Check_Delta_Expression;
10993 -----------------------------
10994 -- Check_Digits_Expression --
10995 -----------------------------
10997 procedure Check_Digits_Expression (E : Node_Id) is
10998 begin
10999 if not (Is_Integer_Type (Etype (E))) then
11000 Wrong_Type (E, Any_Integer);
11002 elsif not Is_OK_Static_Expression (E) then
11003 Flag_Non_Static_Expr
11004 ("non-static expression used for digits value!", E);
11006 elsif Expr_Value (E) <= 0 then
11007 Error_Msg_N ("digits value must be greater than zero", E);
11009 else
11010 return;
11011 end if;
11013 -- If any of above errors occurred, then replace the incorrect
11014 -- expression by the integer 1, which should prevent further errors.
11016 Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
11017 Analyze_And_Resolve (E, Standard_Integer);
11019 end Check_Digits_Expression;
11021 --------------------------
11022 -- Check_Initialization --
11023 --------------------------
11025 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
11026 begin
11027 -- Special processing for limited types
11029 if Is_Limited_Type (T)
11030 and then not In_Instance
11031 and then not In_Inlined_Body
11032 then
11033 if not OK_For_Limited_Init (T, Exp) then
11035 -- In GNAT mode, this is just a warning, to allow it to be evilly
11036 -- turned off. Otherwise it is a real error.
11038 if GNAT_Mode then
11039 Error_Msg_N
11040 ("??cannot initialize entities of limited type!", Exp);
11042 elsif Ada_Version < Ada_2005 then
11044 -- The side effect removal machinery may generate illegal Ada
11045 -- code to avoid the usage of access types and 'reference in
11046 -- SPARK mode. Since this is legal code with respect to theorem
11047 -- proving, do not emit the error.
11049 if GNATprove_Mode
11050 and then Nkind (Exp) = N_Function_Call
11051 and then Nkind (Parent (Exp)) = N_Object_Declaration
11052 and then not Comes_From_Source
11053 (Defining_Identifier (Parent (Exp)))
11054 then
11055 null;
11057 else
11058 Error_Msg_N
11059 ("cannot initialize entities of limited type", Exp);
11060 Explain_Limited_Type (T, Exp);
11061 end if;
11063 else
11064 -- Specialize error message according to kind of illegal
11065 -- initial expression.
11067 if Nkind (Exp) = N_Type_Conversion
11068 and then Nkind (Expression (Exp)) = N_Function_Call
11069 then
11070 Error_Msg_N
11071 ("illegal context for call"
11072 & " to function with limited result", Exp);
11074 else
11075 Error_Msg_N
11076 ("initialization of limited object requires aggregate "
11077 & "or function call", Exp);
11078 end if;
11079 end if;
11080 end if;
11081 end if;
11083 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag gets
11084 -- set unless we can be sure that no range check is required.
11086 if (GNATprove_Mode or not Expander_Active)
11087 and then Is_Scalar_Type (T)
11088 and then not Is_In_Range (Exp, T, Assume_Valid => True)
11089 then
11090 Set_Do_Range_Check (Exp);
11091 end if;
11092 end Check_Initialization;
11094 ----------------------
11095 -- Check_Interfaces --
11096 ----------------------
11098 procedure Check_Interfaces (N : Node_Id; Def : Node_Id) is
11099 Parent_Type : constant Entity_Id := Etype (Defining_Identifier (N));
11101 Iface : Node_Id;
11102 Iface_Def : Node_Id;
11103 Iface_Typ : Entity_Id;
11104 Parent_Node : Node_Id;
11106 Is_Task : Boolean := False;
11107 -- Set True if parent type or any progenitor is a task interface
11109 Is_Protected : Boolean := False;
11110 -- Set True if parent type or any progenitor is a protected interface
11112 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
11113 -- Check that a progenitor is compatible with declaration. If an error
11114 -- message is output, it is posted on Error_Node.
11116 ------------------
11117 -- Check_Ifaces --
11118 ------------------
11120 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
11121 Iface_Id : constant Entity_Id :=
11122 Defining_Identifier (Parent (Iface_Def));
11123 Type_Def : Node_Id;
11125 begin
11126 if Nkind (N) = N_Private_Extension_Declaration then
11127 Type_Def := N;
11128 else
11129 Type_Def := Type_Definition (N);
11130 end if;
11132 if Is_Task_Interface (Iface_Id) then
11133 Is_Task := True;
11135 elsif Is_Protected_Interface (Iface_Id) then
11136 Is_Protected := True;
11137 end if;
11139 if Is_Synchronized_Interface (Iface_Id) then
11141 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
11142 -- extension derived from a synchronized interface must explicitly
11143 -- be declared synchronized, because the full view will be a
11144 -- synchronized type.
11146 if Nkind (N) = N_Private_Extension_Declaration then
11147 if not Synchronized_Present (N) then
11148 Error_Msg_NE
11149 ("private extension of& must be explicitly synchronized",
11150 N, Iface_Id);
11151 end if;
11153 -- However, by 3.9.4(16/2), a full type that is a record extension
11154 -- is never allowed to derive from a synchronized interface (note
11155 -- that interfaces must be excluded from this check, because those
11156 -- are represented by derived type definitions in some cases).
11158 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition
11159 and then not Interface_Present (Type_Definition (N))
11160 then
11161 Error_Msg_N ("record extension cannot derive from synchronized "
11162 & "interface", Error_Node);
11163 end if;
11164 end if;
11166 -- Check that the characteristics of the progenitor are compatible
11167 -- with the explicit qualifier in the declaration.
11168 -- The check only applies to qualifiers that come from source.
11169 -- Limited_Present also appears in the declaration of corresponding
11170 -- records, and the check does not apply to them.
11172 if Limited_Present (Type_Def)
11173 and then not
11174 Is_Concurrent_Record_Type (Defining_Identifier (N))
11175 then
11176 if Is_Limited_Interface (Parent_Type)
11177 and then not Is_Limited_Interface (Iface_Id)
11178 then
11179 Error_Msg_NE
11180 ("progenitor & must be limited interface",
11181 Error_Node, Iface_Id);
11183 elsif
11184 (Task_Present (Iface_Def)
11185 or else Protected_Present (Iface_Def)
11186 or else Synchronized_Present (Iface_Def))
11187 and then Nkind (N) /= N_Private_Extension_Declaration
11188 and then not Error_Posted (N)
11189 then
11190 Error_Msg_NE
11191 ("progenitor & must be limited interface",
11192 Error_Node, Iface_Id);
11193 end if;
11195 -- Protected interfaces can only inherit from limited, synchronized
11196 -- or protected interfaces.
11198 elsif Nkind (N) = N_Full_Type_Declaration
11199 and then Protected_Present (Type_Def)
11200 then
11201 if Limited_Present (Iface_Def)
11202 or else Synchronized_Present (Iface_Def)
11203 or else Protected_Present (Iface_Def)
11204 then
11205 null;
11207 elsif Task_Present (Iface_Def) then
11208 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
11209 & "from task interface", Error_Node);
11211 else
11212 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
11213 & "from non-limited interface", Error_Node);
11214 end if;
11216 -- Ada 2005 (AI-345): Synchronized interfaces can only inherit from
11217 -- limited and synchronized.
11219 elsif Synchronized_Present (Type_Def) then
11220 if Limited_Present (Iface_Def)
11221 or else Synchronized_Present (Iface_Def)
11222 then
11223 null;
11225 elsif Protected_Present (Iface_Def)
11226 and then Nkind (N) /= N_Private_Extension_Declaration
11227 then
11228 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11229 & "from protected interface", Error_Node);
11231 elsif Task_Present (Iface_Def)
11232 and then Nkind (N) /= N_Private_Extension_Declaration
11233 then
11234 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11235 & "from task interface", Error_Node);
11237 elsif not Is_Limited_Interface (Iface_Id) then
11238 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11239 & "from non-limited interface", Error_Node);
11240 end if;
11242 -- Ada 2005 (AI-345): Task interfaces can only inherit from limited,
11243 -- synchronized or task interfaces.
11245 elsif Nkind (N) = N_Full_Type_Declaration
11246 and then Task_Present (Type_Def)
11247 then
11248 if Limited_Present (Iface_Def)
11249 or else Synchronized_Present (Iface_Def)
11250 or else Task_Present (Iface_Def)
11251 then
11252 null;
11254 elsif Protected_Present (Iface_Def) then
11255 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
11256 & "protected interface", Error_Node);
11258 else
11259 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
11260 & "non-limited interface", Error_Node);
11261 end if;
11262 end if;
11263 end Check_Ifaces;
11265 -- Start of processing for Check_Interfaces
11267 begin
11268 if Is_Interface (Parent_Type) then
11269 if Is_Task_Interface (Parent_Type) then
11270 Is_Task := True;
11272 elsif Is_Protected_Interface (Parent_Type) then
11273 Is_Protected := True;
11274 end if;
11275 end if;
11277 if Nkind (N) = N_Private_Extension_Declaration then
11279 -- Check that progenitors are compatible with declaration
11281 Iface := First (Interface_List (Def));
11282 while Present (Iface) loop
11283 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
11285 Parent_Node := Parent (Base_Type (Iface_Typ));
11286 Iface_Def := Type_Definition (Parent_Node);
11288 if not Is_Interface (Iface_Typ) then
11289 Diagnose_Interface (Iface, Iface_Typ);
11290 else
11291 Check_Ifaces (Iface_Def, Iface);
11292 end if;
11294 Next (Iface);
11295 end loop;
11297 if Is_Task and Is_Protected then
11298 Error_Msg_N
11299 ("type cannot derive from task and protected interface", N);
11300 end if;
11302 return;
11303 end if;
11305 -- Full type declaration of derived type.
11306 -- Check compatibility with parent if it is interface type
11308 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
11309 and then Is_Interface (Parent_Type)
11310 then
11311 Parent_Node := Parent (Parent_Type);
11313 -- More detailed checks for interface varieties
11315 Check_Ifaces
11316 (Iface_Def => Type_Definition (Parent_Node),
11317 Error_Node => Subtype_Indication (Type_Definition (N)));
11318 end if;
11320 Iface := First (Interface_List (Def));
11321 while Present (Iface) loop
11322 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
11324 Parent_Node := Parent (Base_Type (Iface_Typ));
11325 Iface_Def := Type_Definition (Parent_Node);
11327 if not Is_Interface (Iface_Typ) then
11328 Diagnose_Interface (Iface, Iface_Typ);
11330 else
11331 -- "The declaration of a specific descendant of an interface
11332 -- type freezes the interface type" RM 13.14
11334 Freeze_Before (N, Iface_Typ);
11335 Check_Ifaces (Iface_Def, Error_Node => Iface);
11336 end if;
11338 Next (Iface);
11339 end loop;
11341 if Is_Task and Is_Protected then
11342 Error_Msg_N
11343 ("type cannot derive from task and protected interface", N);
11344 end if;
11345 end Check_Interfaces;
11347 ------------------------------------
11348 -- Check_Or_Process_Discriminants --
11349 ------------------------------------
11351 -- If an incomplete or private type declaration was already given for the
11352 -- type, the discriminants may have already been processed if they were
11353 -- present on the incomplete declaration. In this case a full conformance
11354 -- check has been performed in Find_Type_Name, and we then recheck here
11355 -- some properties that can't be checked on the partial view alone.
11356 -- Otherwise we call Process_Discriminants.
11358 procedure Check_Or_Process_Discriminants
11359 (N : Node_Id;
11360 T : Entity_Id;
11361 Prev : Entity_Id := Empty)
11363 begin
11364 if Has_Discriminants (T) then
11366 -- Discriminants are already set on T if they were already present
11367 -- on the partial view. Make them visible to component declarations.
11369 declare
11370 D : Entity_Id;
11371 -- Discriminant on T (full view) referencing expr on partial view
11373 Prev_D : Entity_Id;
11374 -- Entity of corresponding discriminant on partial view
11376 New_D : Node_Id;
11377 -- Discriminant specification for full view, expression is
11378 -- the syntactic copy on full view (which has been checked for
11379 -- conformance with partial view), only used here to post error
11380 -- message.
11382 begin
11383 D := First_Discriminant (T);
11384 New_D := First (Discriminant_Specifications (N));
11385 while Present (D) loop
11386 Prev_D := Current_Entity (D);
11387 Set_Current_Entity (D);
11388 Set_Is_Immediately_Visible (D);
11389 Set_Homonym (D, Prev_D);
11391 -- Handle the case where there is an untagged partial view and
11392 -- the full view is tagged: must disallow discriminants with
11393 -- defaults, unless compiling for Ada 2012, which allows a
11394 -- limited tagged type to have defaulted discriminants (see
11395 -- AI05-0214). However, suppress error here if it was already
11396 -- reported on the default expression of the partial view.
11398 if Is_Tagged_Type (T)
11399 and then Present (Expression (Parent (D)))
11400 and then (not Is_Limited_Type (Current_Scope)
11401 or else Ada_Version < Ada_2012)
11402 and then not Error_Posted (Expression (Parent (D)))
11403 then
11404 if Ada_Version >= Ada_2012 then
11405 Error_Msg_N
11406 ("discriminants of nonlimited tagged type cannot have "
11407 & "defaults",
11408 Expression (New_D));
11409 else
11410 Error_Msg_N
11411 ("discriminants of tagged type cannot have defaults",
11412 Expression (New_D));
11413 end if;
11414 end if;
11416 -- Ada 2005 (AI-230): Access discriminant allowed in
11417 -- non-limited record types.
11419 if Ada_Version < Ada_2005 then
11421 -- This restriction gets applied to the full type here. It
11422 -- has already been applied earlier to the partial view.
11424 Check_Access_Discriminant_Requires_Limited (Parent (D), N);
11425 end if;
11427 Next_Discriminant (D);
11428 Next (New_D);
11429 end loop;
11430 end;
11432 elsif Present (Discriminant_Specifications (N)) then
11433 Process_Discriminants (N, Prev);
11434 end if;
11435 end Check_Or_Process_Discriminants;
11437 ----------------------
11438 -- Check_Real_Bound --
11439 ----------------------
11441 procedure Check_Real_Bound (Bound : Node_Id) is
11442 begin
11443 if not Is_Real_Type (Etype (Bound)) then
11444 Error_Msg_N
11445 ("bound in real type definition must be of real type", Bound);
11447 elsif not Is_OK_Static_Expression (Bound) then
11448 Flag_Non_Static_Expr
11449 ("non-static expression used for real type bound!", Bound);
11451 else
11452 return;
11453 end if;
11455 Rewrite
11456 (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
11457 Analyze (Bound);
11458 Resolve (Bound, Standard_Float);
11459 end Check_Real_Bound;
11461 ------------------------------
11462 -- Complete_Private_Subtype --
11463 ------------------------------
11465 procedure Complete_Private_Subtype
11466 (Priv : Entity_Id;
11467 Full : Entity_Id;
11468 Full_Base : Entity_Id;
11469 Related_Nod : Node_Id)
11471 Save_Next_Entity : Entity_Id;
11472 Save_Homonym : Entity_Id;
11474 begin
11475 -- Set semantic attributes for (implicit) private subtype completion.
11476 -- If the full type has no discriminants, then it is a copy of the
11477 -- full view of the base. Otherwise, it is a subtype of the base with
11478 -- a possible discriminant constraint. Save and restore the original
11479 -- Next_Entity field of full to ensure that the calls to Copy_Node do
11480 -- not corrupt the entity chain.
11482 -- Note that the type of the full view is the same entity as the type
11483 -- of the partial view. In this fashion, the subtype has access to the
11484 -- correct view of the parent.
11486 Save_Next_Entity := Next_Entity (Full);
11487 Save_Homonym := Homonym (Priv);
11489 case Ekind (Full_Base) is
11490 when E_Record_Type |
11491 E_Record_Subtype |
11492 Class_Wide_Kind |
11493 Private_Kind |
11494 Task_Kind |
11495 Protected_Kind =>
11496 Copy_Node (Priv, Full);
11498 Set_Has_Discriminants
11499 (Full, Has_Discriminants (Full_Base));
11500 Set_Has_Unknown_Discriminants
11501 (Full, Has_Unknown_Discriminants (Full_Base));
11502 Set_First_Entity (Full, First_Entity (Full_Base));
11503 Set_Last_Entity (Full, Last_Entity (Full_Base));
11505 -- If the underlying base type is constrained, we know that the
11506 -- full view of the subtype is constrained as well (the converse
11507 -- is not necessarily true).
11509 if Is_Constrained (Full_Base) then
11510 Set_Is_Constrained (Full);
11511 end if;
11513 when others =>
11514 Copy_Node (Full_Base, Full);
11516 Set_Chars (Full, Chars (Priv));
11517 Conditional_Delay (Full, Priv);
11518 Set_Sloc (Full, Sloc (Priv));
11519 end case;
11521 Set_Next_Entity (Full, Save_Next_Entity);
11522 Set_Homonym (Full, Save_Homonym);
11523 Set_Associated_Node_For_Itype (Full, Related_Nod);
11525 -- Set common attributes for all subtypes: kind, convention, etc.
11527 Set_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
11528 Set_Convention (Full, Convention (Full_Base));
11530 -- The Etype of the full view is inconsistent. Gigi needs to see the
11531 -- structural full view, which is what the current scheme gives: the
11532 -- Etype of the full view is the etype of the full base. However, if the
11533 -- full base is a derived type, the full view then looks like a subtype
11534 -- of the parent, not a subtype of the full base. If instead we write:
11536 -- Set_Etype (Full, Full_Base);
11538 -- then we get inconsistencies in the front-end (confusion between
11539 -- views). Several outstanding bugs are related to this ???
11541 Set_Is_First_Subtype (Full, False);
11542 Set_Scope (Full, Scope (Priv));
11543 Set_Size_Info (Full, Full_Base);
11544 Set_RM_Size (Full, RM_Size (Full_Base));
11545 Set_Is_Itype (Full);
11547 -- A subtype of a private-type-without-discriminants, whose full-view
11548 -- has discriminants with default expressions, is not constrained.
11550 if not Has_Discriminants (Priv) then
11551 Set_Is_Constrained (Full, Is_Constrained (Full_Base));
11553 if Has_Discriminants (Full_Base) then
11554 Set_Discriminant_Constraint
11555 (Full, Discriminant_Constraint (Full_Base));
11557 -- The partial view may have been indefinite, the full view
11558 -- might not be.
11560 Set_Has_Unknown_Discriminants
11561 (Full, Has_Unknown_Discriminants (Full_Base));
11562 end if;
11563 end if;
11565 Set_First_Rep_Item (Full, First_Rep_Item (Full_Base));
11566 Set_Depends_On_Private (Full, Has_Private_Component (Full));
11568 -- Freeze the private subtype entity if its parent is delayed, and not
11569 -- already frozen. We skip this processing if the type is an anonymous
11570 -- subtype of a record component, or is the corresponding record of a
11571 -- protected type, since ???
11573 if not Is_Type (Scope (Full)) then
11574 Set_Has_Delayed_Freeze (Full,
11575 Has_Delayed_Freeze (Full_Base)
11576 and then (not Is_Frozen (Full_Base)));
11577 end if;
11579 Set_Freeze_Node (Full, Empty);
11580 Set_Is_Frozen (Full, False);
11581 Set_Full_View (Priv, Full);
11583 if Has_Discriminants (Full) then
11584 Set_Stored_Constraint_From_Discriminant_Constraint (Full);
11585 Set_Stored_Constraint (Priv, Stored_Constraint (Full));
11587 if Has_Unknown_Discriminants (Full) then
11588 Set_Discriminant_Constraint (Full, No_Elist);
11589 end if;
11590 end if;
11592 if Ekind (Full_Base) = E_Record_Type
11593 and then Has_Discriminants (Full_Base)
11594 and then Has_Discriminants (Priv) -- might not, if errors
11595 and then not Has_Unknown_Discriminants (Priv)
11596 and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
11597 then
11598 Create_Constrained_Components
11599 (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
11601 -- If the full base is itself derived from private, build a congruent
11602 -- subtype of its underlying type, for use by the back end. For a
11603 -- constrained record component, the declaration cannot be placed on
11604 -- the component list, but it must nevertheless be built an analyzed, to
11605 -- supply enough information for Gigi to compute the size of component.
11607 elsif Ekind (Full_Base) in Private_Kind
11608 and then Is_Derived_Type (Full_Base)
11609 and then Has_Discriminants (Full_Base)
11610 and then (Ekind (Current_Scope) /= E_Record_Subtype)
11611 then
11612 if not Is_Itype (Priv)
11613 and then
11614 Nkind (Subtype_Indication (Parent (Priv))) = N_Subtype_Indication
11615 then
11616 Build_Underlying_Full_View
11617 (Parent (Priv), Full, Etype (Full_Base));
11619 elsif Nkind (Related_Nod) = N_Component_Declaration then
11620 Build_Underlying_Full_View (Related_Nod, Full, Etype (Full_Base));
11621 end if;
11623 elsif Is_Record_Type (Full_Base) then
11625 -- Show Full is simply a renaming of Full_Base
11627 Set_Cloned_Subtype (Full, Full_Base);
11628 end if;
11630 -- It is unsafe to share the bounds of a scalar type, because the Itype
11631 -- is elaborated on demand, and if a bound is non-static then different
11632 -- orders of elaboration in different units will lead to different
11633 -- external symbols.
11635 if Is_Scalar_Type (Full_Base) then
11636 Set_Scalar_Range (Full,
11637 Make_Range (Sloc (Related_Nod),
11638 Low_Bound =>
11639 Duplicate_Subexpr_No_Checks (Type_Low_Bound (Full_Base)),
11640 High_Bound =>
11641 Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
11643 -- This completion inherits the bounds of the full parent, but if
11644 -- the parent is an unconstrained floating point type, so is the
11645 -- completion.
11647 if Is_Floating_Point_Type (Full_Base) then
11648 Set_Includes_Infinities
11649 (Scalar_Range (Full), Has_Infinities (Full_Base));
11650 end if;
11651 end if;
11653 -- ??? It seems that a lot of fields are missing that should be copied
11654 -- from Full_Base to Full. Here are some that are introduced in a
11655 -- non-disruptive way but a cleanup is necessary.
11657 if Is_Tagged_Type (Full_Base) then
11658 Set_Is_Tagged_Type (Full);
11659 Set_Direct_Primitive_Operations
11660 (Full, Direct_Primitive_Operations (Full_Base));
11661 Set_No_Tagged_Streams_Pragma
11662 (Full, No_Tagged_Streams_Pragma (Full_Base));
11664 -- Inherit class_wide type of full_base in case the partial view was
11665 -- not tagged. Otherwise it has already been created when the private
11666 -- subtype was analyzed.
11668 if No (Class_Wide_Type (Full)) then
11669 Set_Class_Wide_Type (Full, Class_Wide_Type (Full_Base));
11670 end if;
11672 -- If this is a subtype of a protected or task type, constrain its
11673 -- corresponding record, unless this is a subtype without constraints,
11674 -- i.e. a simple renaming as with an actual subtype in an instance.
11676 elsif Is_Concurrent_Type (Full_Base) then
11677 if Has_Discriminants (Full)
11678 and then Present (Corresponding_Record_Type (Full_Base))
11679 and then
11680 not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
11681 then
11682 Set_Corresponding_Record_Type (Full,
11683 Constrain_Corresponding_Record
11684 (Full, Corresponding_Record_Type (Full_Base), Related_Nod));
11686 else
11687 Set_Corresponding_Record_Type (Full,
11688 Corresponding_Record_Type (Full_Base));
11689 end if;
11690 end if;
11692 -- Link rep item chain, and also setting of Has_Predicates from private
11693 -- subtype to full subtype, since we will need these on the full subtype
11694 -- to create the predicate function. Note that the full subtype may
11695 -- already have rep items, inherited from the full view of the base
11696 -- type, so we must be sure not to overwrite these entries.
11698 declare
11699 Append : Boolean;
11700 Item : Node_Id;
11701 Next_Item : Node_Id;
11703 begin
11704 Item := First_Rep_Item (Full);
11706 -- If no existing rep items on full type, we can just link directly
11707 -- to the list of items on the private type, if any exist.. Same if
11708 -- the rep items are only those inherited from the base
11710 if (No (Item)
11711 or else Nkind (Item) /= N_Aspect_Specification
11712 or else Entity (Item) = Full_Base)
11713 and then Present (First_Rep_Item (Priv))
11714 then
11715 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
11717 -- Otherwise, search to the end of items currently linked to the full
11718 -- subtype and append the private items to the end. However, if Priv
11719 -- and Full already have the same list of rep items, then the append
11720 -- is not done, as that would create a circularity.
11722 elsif Item /= First_Rep_Item (Priv) then
11723 Append := True;
11724 loop
11725 Next_Item := Next_Rep_Item (Item);
11726 exit when No (Next_Item);
11727 Item := Next_Item;
11729 -- If the private view has aspect specifications, the full view
11730 -- inherits them. Since these aspects may already have been
11731 -- attached to the full view during derivation, do not append
11732 -- them if already present.
11734 if Item = First_Rep_Item (Priv) then
11735 Append := False;
11736 exit;
11737 end if;
11738 end loop;
11740 -- And link the private type items at the end of the chain
11742 if Append then
11743 Set_Next_Rep_Item (Item, First_Rep_Item (Priv));
11744 end if;
11745 end if;
11746 end;
11748 -- Make sure Has_Predicates is set on full type if it is set on the
11749 -- private type. Note that it may already be set on the full type and
11750 -- if so, we don't want to unset it.
11752 if Has_Predicates (Priv) then
11753 Set_Has_Predicates (Full);
11754 end if;
11755 end Complete_Private_Subtype;
11757 ----------------------------
11758 -- Constant_Redeclaration --
11759 ----------------------------
11761 procedure Constant_Redeclaration
11762 (Id : Entity_Id;
11763 N : Node_Id;
11764 T : out Entity_Id)
11766 Prev : constant Entity_Id := Current_Entity_In_Scope (Id);
11767 Obj_Def : constant Node_Id := Object_Definition (N);
11768 New_T : Entity_Id;
11770 procedure Check_Possible_Deferred_Completion
11771 (Prev_Id : Entity_Id;
11772 Prev_Obj_Def : Node_Id;
11773 Curr_Obj_Def : Node_Id);
11774 -- Determine whether the two object definitions describe the partial
11775 -- and the full view of a constrained deferred constant. Generate
11776 -- a subtype for the full view and verify that it statically matches
11777 -- the subtype of the partial view.
11779 procedure Check_Recursive_Declaration (Typ : Entity_Id);
11780 -- If deferred constant is an access type initialized with an allocator,
11781 -- check whether there is an illegal recursion in the definition,
11782 -- through a default value of some record subcomponent. This is normally
11783 -- detected when generating init procs, but requires this additional
11784 -- mechanism when expansion is disabled.
11786 ----------------------------------------
11787 -- Check_Possible_Deferred_Completion --
11788 ----------------------------------------
11790 procedure Check_Possible_Deferred_Completion
11791 (Prev_Id : Entity_Id;
11792 Prev_Obj_Def : Node_Id;
11793 Curr_Obj_Def : Node_Id)
11795 begin
11796 if Nkind (Prev_Obj_Def) = N_Subtype_Indication
11797 and then Present (Constraint (Prev_Obj_Def))
11798 and then Nkind (Curr_Obj_Def) = N_Subtype_Indication
11799 and then Present (Constraint (Curr_Obj_Def))
11800 then
11801 declare
11802 Loc : constant Source_Ptr := Sloc (N);
11803 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
11804 Decl : constant Node_Id :=
11805 Make_Subtype_Declaration (Loc,
11806 Defining_Identifier => Def_Id,
11807 Subtype_Indication =>
11808 Relocate_Node (Curr_Obj_Def));
11810 begin
11811 Insert_Before_And_Analyze (N, Decl);
11812 Set_Etype (Id, Def_Id);
11814 if not Subtypes_Statically_Match (Etype (Prev_Id), Def_Id) then
11815 Error_Msg_Sloc := Sloc (Prev_Id);
11816 Error_Msg_N ("subtype does not statically match deferred "
11817 & "declaration #", N);
11818 end if;
11819 end;
11820 end if;
11821 end Check_Possible_Deferred_Completion;
11823 ---------------------------------
11824 -- Check_Recursive_Declaration --
11825 ---------------------------------
11827 procedure Check_Recursive_Declaration (Typ : Entity_Id) is
11828 Comp : Entity_Id;
11830 begin
11831 if Is_Record_Type (Typ) then
11832 Comp := First_Component (Typ);
11833 while Present (Comp) loop
11834 if Comes_From_Source (Comp) then
11835 if Present (Expression (Parent (Comp)))
11836 and then Is_Entity_Name (Expression (Parent (Comp)))
11837 and then Entity (Expression (Parent (Comp))) = Prev
11838 then
11839 Error_Msg_Sloc := Sloc (Parent (Comp));
11840 Error_Msg_NE
11841 ("illegal circularity with declaration for & #",
11842 N, Comp);
11843 return;
11845 elsif Is_Record_Type (Etype (Comp)) then
11846 Check_Recursive_Declaration (Etype (Comp));
11847 end if;
11848 end if;
11850 Next_Component (Comp);
11851 end loop;
11852 end if;
11853 end Check_Recursive_Declaration;
11855 -- Start of processing for Constant_Redeclaration
11857 begin
11858 if Nkind (Parent (Prev)) = N_Object_Declaration then
11859 if Nkind (Object_Definition
11860 (Parent (Prev))) = N_Subtype_Indication
11861 then
11862 -- Find type of new declaration. The constraints of the two
11863 -- views must match statically, but there is no point in
11864 -- creating an itype for the full view.
11866 if Nkind (Obj_Def) = N_Subtype_Indication then
11867 Find_Type (Subtype_Mark (Obj_Def));
11868 New_T := Entity (Subtype_Mark (Obj_Def));
11870 else
11871 Find_Type (Obj_Def);
11872 New_T := Entity (Obj_Def);
11873 end if;
11875 T := Etype (Prev);
11877 else
11878 -- The full view may impose a constraint, even if the partial
11879 -- view does not, so construct the subtype.
11881 New_T := Find_Type_Of_Object (Obj_Def, N);
11882 T := New_T;
11883 end if;
11885 else
11886 -- Current declaration is illegal, diagnosed below in Enter_Name
11888 T := Empty;
11889 New_T := Any_Type;
11890 end if;
11892 -- If previous full declaration or a renaming declaration exists, or if
11893 -- a homograph is present, let Enter_Name handle it, either with an
11894 -- error or with the removal of an overridden implicit subprogram.
11895 -- The previous one is a full declaration if it has an expression
11896 -- (which in the case of an aggregate is indicated by the Init flag).
11898 if Ekind (Prev) /= E_Constant
11899 or else Nkind (Parent (Prev)) = N_Object_Renaming_Declaration
11900 or else Present (Expression (Parent (Prev)))
11901 or else Has_Init_Expression (Parent (Prev))
11902 or else Present (Full_View (Prev))
11903 then
11904 Enter_Name (Id);
11906 -- Verify that types of both declarations match, or else that both types
11907 -- are anonymous access types whose designated subtypes statically match
11908 -- (as allowed in Ada 2005 by AI-385).
11910 elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
11911 and then
11912 (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
11913 or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
11914 or else Is_Access_Constant (Etype (New_T)) /=
11915 Is_Access_Constant (Etype (Prev))
11916 or else Can_Never_Be_Null (Etype (New_T)) /=
11917 Can_Never_Be_Null (Etype (Prev))
11918 or else Null_Exclusion_Present (Parent (Prev)) /=
11919 Null_Exclusion_Present (Parent (Id))
11920 or else not Subtypes_Statically_Match
11921 (Designated_Type (Etype (Prev)),
11922 Designated_Type (Etype (New_T))))
11923 then
11924 Error_Msg_Sloc := Sloc (Prev);
11925 Error_Msg_N ("type does not match declaration#", N);
11926 Set_Full_View (Prev, Id);
11927 Set_Etype (Id, Any_Type);
11929 -- A deferred constant whose type is an anonymous array is always
11930 -- illegal (unless imported). A detailed error message might be
11931 -- helpful for Ada beginners.
11933 if Nkind (Object_Definition (Parent (Prev)))
11934 = N_Constrained_Array_Definition
11935 and then Nkind (Object_Definition (N))
11936 = N_Constrained_Array_Definition
11937 then
11938 Error_Msg_N ("\each anonymous array is a distinct type", N);
11939 Error_Msg_N ("a deferred constant must have a named type",
11940 Object_Definition (Parent (Prev)));
11941 end if;
11943 elsif
11944 Null_Exclusion_Present (Parent (Prev))
11945 and then not Null_Exclusion_Present (N)
11946 then
11947 Error_Msg_Sloc := Sloc (Prev);
11948 Error_Msg_N ("null-exclusion does not match declaration#", N);
11949 Set_Full_View (Prev, Id);
11950 Set_Etype (Id, Any_Type);
11952 -- If so, process the full constant declaration
11954 else
11955 -- RM 7.4 (6): If the subtype defined by the subtype_indication in
11956 -- the deferred declaration is constrained, then the subtype defined
11957 -- by the subtype_indication in the full declaration shall match it
11958 -- statically.
11960 Check_Possible_Deferred_Completion
11961 (Prev_Id => Prev,
11962 Prev_Obj_Def => Object_Definition (Parent (Prev)),
11963 Curr_Obj_Def => Obj_Def);
11965 Set_Full_View (Prev, Id);
11966 Set_Is_Public (Id, Is_Public (Prev));
11967 Set_Is_Internal (Id);
11968 Append_Entity (Id, Current_Scope);
11970 -- Check ALIASED present if present before (RM 7.4(7))
11972 if Is_Aliased (Prev)
11973 and then not Aliased_Present (N)
11974 then
11975 Error_Msg_Sloc := Sloc (Prev);
11976 Error_Msg_N ("ALIASED required (see declaration #)", N);
11977 end if;
11979 -- Check that placement is in private part and that the incomplete
11980 -- declaration appeared in the visible part.
11982 if Ekind (Current_Scope) = E_Package
11983 and then not In_Private_Part (Current_Scope)
11984 then
11985 Error_Msg_Sloc := Sloc (Prev);
11986 Error_Msg_N
11987 ("full constant for declaration # must be in private part", N);
11989 elsif Ekind (Current_Scope) = E_Package
11990 and then
11991 List_Containing (Parent (Prev)) /=
11992 Visible_Declarations (Package_Specification (Current_Scope))
11993 then
11994 Error_Msg_N
11995 ("deferred constant must be declared in visible part",
11996 Parent (Prev));
11997 end if;
11999 if Is_Access_Type (T)
12000 and then Nkind (Expression (N)) = N_Allocator
12001 then
12002 Check_Recursive_Declaration (Designated_Type (T));
12003 end if;
12005 -- A deferred constant is a visible entity. If type has invariants,
12006 -- verify that the initial value satisfies them.
12008 if Has_Invariants (T) and then Present (Invariant_Procedure (T)) then
12009 Insert_After (N,
12010 Make_Invariant_Call (New_Occurrence_Of (Prev, Sloc (N))));
12011 end if;
12012 end if;
12013 end Constant_Redeclaration;
12015 ----------------------
12016 -- Constrain_Access --
12017 ----------------------
12019 procedure Constrain_Access
12020 (Def_Id : in out Entity_Id;
12021 S : Node_Id;
12022 Related_Nod : Node_Id)
12024 T : constant Entity_Id := Entity (Subtype_Mark (S));
12025 Desig_Type : constant Entity_Id := Designated_Type (T);
12026 Desig_Subtype : Entity_Id := Create_Itype (E_Void, Related_Nod);
12027 Constraint_OK : Boolean := True;
12029 begin
12030 if Is_Array_Type (Desig_Type) then
12031 Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
12033 elsif (Is_Record_Type (Desig_Type)
12034 or else Is_Incomplete_Or_Private_Type (Desig_Type))
12035 and then not Is_Constrained (Desig_Type)
12036 then
12037 -- ??? The following code is a temporary bypass to ignore a
12038 -- discriminant constraint on access type if it is constraining
12039 -- the current record. Avoid creating the implicit subtype of the
12040 -- record we are currently compiling since right now, we cannot
12041 -- handle these. For now, just return the access type itself.
12043 if Desig_Type = Current_Scope
12044 and then No (Def_Id)
12045 then
12046 Set_Ekind (Desig_Subtype, E_Record_Subtype);
12047 Def_Id := Entity (Subtype_Mark (S));
12049 -- This call added to ensure that the constraint is analyzed
12050 -- (needed for a B test). Note that we still return early from
12051 -- this procedure to avoid recursive processing. ???
12053 Constrain_Discriminated_Type
12054 (Desig_Subtype, S, Related_Nod, For_Access => True);
12055 return;
12056 end if;
12058 -- Enforce rule that the constraint is illegal if there is an
12059 -- unconstrained view of the designated type. This means that the
12060 -- partial view (either a private type declaration or a derivation
12061 -- from a private type) has no discriminants. (Defect Report
12062 -- 8652/0008, Technical Corrigendum 1, checked by ACATS B371001).
12064 -- Rule updated for Ada 2005: The private type is said to have
12065 -- a constrained partial view, given that objects of the type
12066 -- can be declared. Furthermore, the rule applies to all access
12067 -- types, unlike the rule concerning default discriminants (see
12068 -- RM 3.7.1(7/3))
12070 if (Ekind (T) = E_General_Access_Type or else Ada_Version >= Ada_2005)
12071 and then Has_Private_Declaration (Desig_Type)
12072 and then In_Open_Scopes (Scope (Desig_Type))
12073 and then Has_Discriminants (Desig_Type)
12074 then
12075 declare
12076 Pack : constant Node_Id :=
12077 Unit_Declaration_Node (Scope (Desig_Type));
12078 Decls : List_Id;
12079 Decl : Node_Id;
12081 begin
12082 if Nkind (Pack) = N_Package_Declaration then
12083 Decls := Visible_Declarations (Specification (Pack));
12084 Decl := First (Decls);
12085 while Present (Decl) loop
12086 if (Nkind (Decl) = N_Private_Type_Declaration
12087 and then Chars (Defining_Identifier (Decl)) =
12088 Chars (Desig_Type))
12090 or else
12091 (Nkind (Decl) = N_Full_Type_Declaration
12092 and then
12093 Chars (Defining_Identifier (Decl)) =
12094 Chars (Desig_Type)
12095 and then Is_Derived_Type (Desig_Type)
12096 and then
12097 Has_Private_Declaration (Etype (Desig_Type)))
12098 then
12099 if No (Discriminant_Specifications (Decl)) then
12100 Error_Msg_N
12101 ("cannot constrain access type if designated "
12102 & "type has constrained partial view", S);
12103 end if;
12105 exit;
12106 end if;
12108 Next (Decl);
12109 end loop;
12110 end if;
12111 end;
12112 end if;
12114 Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
12115 For_Access => True);
12117 elsif Is_Concurrent_Type (Desig_Type)
12118 and then not Is_Constrained (Desig_Type)
12119 then
12120 Constrain_Concurrent (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
12122 else
12123 Error_Msg_N ("invalid constraint on access type", S);
12125 -- We simply ignore an invalid constraint
12127 Desig_Subtype := Desig_Type;
12128 Constraint_OK := False;
12129 end if;
12131 if No (Def_Id) then
12132 Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
12133 else
12134 Set_Ekind (Def_Id, E_Access_Subtype);
12135 end if;
12137 if Constraint_OK then
12138 Set_Etype (Def_Id, Base_Type (T));
12140 if Is_Private_Type (Desig_Type) then
12141 Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
12142 end if;
12143 else
12144 Set_Etype (Def_Id, Any_Type);
12145 end if;
12147 Set_Size_Info (Def_Id, T);
12148 Set_Is_Constrained (Def_Id, Constraint_OK);
12149 Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
12150 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
12151 Set_Is_Access_Constant (Def_Id, Is_Access_Constant (T));
12153 Conditional_Delay (Def_Id, T);
12155 -- AI-363 : Subtypes of general access types whose designated types have
12156 -- default discriminants are disallowed. In instances, the rule has to
12157 -- be checked against the actual, of which T is the subtype. In a
12158 -- generic body, the rule is checked assuming that the actual type has
12159 -- defaulted discriminants.
12161 if Ada_Version >= Ada_2005 or else Warn_On_Ada_2005_Compatibility then
12162 if Ekind (Base_Type (T)) = E_General_Access_Type
12163 and then Has_Defaulted_Discriminants (Desig_Type)
12164 then
12165 if Ada_Version < Ada_2005 then
12166 Error_Msg_N
12167 ("access subtype of general access type would not " &
12168 "be allowed in Ada 2005?y?", S);
12169 else
12170 Error_Msg_N
12171 ("access subtype of general access type not allowed", S);
12172 end if;
12174 Error_Msg_N ("\discriminants have defaults", S);
12176 elsif Is_Access_Type (T)
12177 and then Is_Generic_Type (Desig_Type)
12178 and then Has_Discriminants (Desig_Type)
12179 and then In_Package_Body (Current_Scope)
12180 then
12181 if Ada_Version < Ada_2005 then
12182 Error_Msg_N
12183 ("access subtype would not be allowed in generic body "
12184 & "in Ada 2005?y?", S);
12185 else
12186 Error_Msg_N
12187 ("access subtype not allowed in generic body", S);
12188 end if;
12190 Error_Msg_N
12191 ("\designated type is a discriminated formal", S);
12192 end if;
12193 end if;
12194 end Constrain_Access;
12196 ---------------------
12197 -- Constrain_Array --
12198 ---------------------
12200 procedure Constrain_Array
12201 (Def_Id : in out Entity_Id;
12202 SI : Node_Id;
12203 Related_Nod : Node_Id;
12204 Related_Id : Entity_Id;
12205 Suffix : Character)
12207 C : constant Node_Id := Constraint (SI);
12208 Number_Of_Constraints : Nat := 0;
12209 Index : Node_Id;
12210 S, T : Entity_Id;
12211 Constraint_OK : Boolean := True;
12213 begin
12214 T := Entity (Subtype_Mark (SI));
12216 if Is_Access_Type (T) then
12217 T := Designated_Type (T);
12218 end if;
12220 -- If an index constraint follows a subtype mark in a subtype indication
12221 -- then the type or subtype denoted by the subtype mark must not already
12222 -- impose an index constraint. The subtype mark must denote either an
12223 -- unconstrained array type or an access type whose designated type
12224 -- is such an array type... (RM 3.6.1)
12226 if Is_Constrained (T) then
12227 Error_Msg_N ("array type is already constrained", Subtype_Mark (SI));
12228 Constraint_OK := False;
12230 else
12231 S := First (Constraints (C));
12232 while Present (S) loop
12233 Number_Of_Constraints := Number_Of_Constraints + 1;
12234 Next (S);
12235 end loop;
12237 -- In either case, the index constraint must provide a discrete
12238 -- range for each index of the array type and the type of each
12239 -- discrete range must be the same as that of the corresponding
12240 -- index. (RM 3.6.1)
12242 if Number_Of_Constraints /= Number_Dimensions (T) then
12243 Error_Msg_NE ("incorrect number of index constraints for }", C, T);
12244 Constraint_OK := False;
12246 else
12247 S := First (Constraints (C));
12248 Index := First_Index (T);
12249 Analyze (Index);
12251 -- Apply constraints to each index type
12253 for J in 1 .. Number_Of_Constraints loop
12254 Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
12255 Next (Index);
12256 Next (S);
12257 end loop;
12259 end if;
12260 end if;
12262 if No (Def_Id) then
12263 Def_Id :=
12264 Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
12265 Set_Parent (Def_Id, Related_Nod);
12267 else
12268 Set_Ekind (Def_Id, E_Array_Subtype);
12269 end if;
12271 Set_Size_Info (Def_Id, (T));
12272 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
12273 Set_Etype (Def_Id, Base_Type (T));
12275 if Constraint_OK then
12276 Set_First_Index (Def_Id, First (Constraints (C)));
12277 else
12278 Set_First_Index (Def_Id, First_Index (T));
12279 end if;
12281 Set_Is_Constrained (Def_Id, True);
12282 Set_Is_Aliased (Def_Id, Is_Aliased (T));
12283 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
12285 Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
12286 Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
12288 -- A subtype does not inherit the Packed_Array_Impl_Type of is parent.
12289 -- We need to initialize the attribute because if Def_Id is previously
12290 -- analyzed through a limited_with clause, it will have the attributes
12291 -- of an incomplete type, one of which is an Elist that overlaps the
12292 -- Packed_Array_Impl_Type field.
12294 Set_Packed_Array_Impl_Type (Def_Id, Empty);
12296 -- Build a freeze node if parent still needs one. Also make sure that
12297 -- the Depends_On_Private status is set because the subtype will need
12298 -- reprocessing at the time the base type does, and also we must set a
12299 -- conditional delay.
12301 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
12302 Conditional_Delay (Def_Id, T);
12303 end Constrain_Array;
12305 ------------------------------
12306 -- Constrain_Component_Type --
12307 ------------------------------
12309 function Constrain_Component_Type
12310 (Comp : Entity_Id;
12311 Constrained_Typ : Entity_Id;
12312 Related_Node : Node_Id;
12313 Typ : Entity_Id;
12314 Constraints : Elist_Id) return Entity_Id
12316 Loc : constant Source_Ptr := Sloc (Constrained_Typ);
12317 Compon_Type : constant Entity_Id := Etype (Comp);
12319 function Build_Constrained_Array_Type
12320 (Old_Type : Entity_Id) return Entity_Id;
12321 -- If Old_Type is an array type, one of whose indexes is constrained
12322 -- by a discriminant, build an Itype whose constraint replaces the
12323 -- discriminant with its value in the constraint.
12325 function Build_Constrained_Discriminated_Type
12326 (Old_Type : Entity_Id) return Entity_Id;
12327 -- Ditto for record components
12329 function Build_Constrained_Access_Type
12330 (Old_Type : Entity_Id) return Entity_Id;
12331 -- Ditto for access types. Makes use of previous two functions, to
12332 -- constrain designated type.
12334 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id;
12335 -- T is an array or discriminated type, C is a list of constraints
12336 -- that apply to T. This routine builds the constrained subtype.
12338 function Is_Discriminant (Expr : Node_Id) return Boolean;
12339 -- Returns True if Expr is a discriminant
12341 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id;
12342 -- Find the value of discriminant Discrim in Constraint
12344 -----------------------------------
12345 -- Build_Constrained_Access_Type --
12346 -----------------------------------
12348 function Build_Constrained_Access_Type
12349 (Old_Type : Entity_Id) return Entity_Id
12351 Desig_Type : constant Entity_Id := Designated_Type (Old_Type);
12352 Itype : Entity_Id;
12353 Desig_Subtype : Entity_Id;
12354 Scop : Entity_Id;
12356 begin
12357 -- if the original access type was not embedded in the enclosing
12358 -- type definition, there is no need to produce a new access
12359 -- subtype. In fact every access type with an explicit constraint
12360 -- generates an itype whose scope is the enclosing record.
12362 if not Is_Type (Scope (Old_Type)) then
12363 return Old_Type;
12365 elsif Is_Array_Type (Desig_Type) then
12366 Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
12368 elsif Has_Discriminants (Desig_Type) then
12370 -- This may be an access type to an enclosing record type for
12371 -- which we are constructing the constrained components. Return
12372 -- the enclosing record subtype. This is not always correct,
12373 -- but avoids infinite recursion. ???
12375 Desig_Subtype := Any_Type;
12377 for J in reverse 0 .. Scope_Stack.Last loop
12378 Scop := Scope_Stack.Table (J).Entity;
12380 if Is_Type (Scop)
12381 and then Base_Type (Scop) = Base_Type (Desig_Type)
12382 then
12383 Desig_Subtype := Scop;
12384 end if;
12386 exit when not Is_Type (Scop);
12387 end loop;
12389 if Desig_Subtype = Any_Type then
12390 Desig_Subtype :=
12391 Build_Constrained_Discriminated_Type (Desig_Type);
12392 end if;
12394 else
12395 return Old_Type;
12396 end if;
12398 if Desig_Subtype /= Desig_Type then
12400 -- The Related_Node better be here or else we won't be able
12401 -- to attach new itypes to a node in the tree.
12403 pragma Assert (Present (Related_Node));
12405 Itype := Create_Itype (E_Access_Subtype, Related_Node);
12407 Set_Etype (Itype, Base_Type (Old_Type));
12408 Set_Size_Info (Itype, (Old_Type));
12409 Set_Directly_Designated_Type (Itype, Desig_Subtype);
12410 Set_Depends_On_Private (Itype, Has_Private_Component
12411 (Old_Type));
12412 Set_Is_Access_Constant (Itype, Is_Access_Constant
12413 (Old_Type));
12415 -- The new itype needs freezing when it depends on a not frozen
12416 -- type and the enclosing subtype needs freezing.
12418 if Has_Delayed_Freeze (Constrained_Typ)
12419 and then not Is_Frozen (Constrained_Typ)
12420 then
12421 Conditional_Delay (Itype, Base_Type (Old_Type));
12422 end if;
12424 return Itype;
12426 else
12427 return Old_Type;
12428 end if;
12429 end Build_Constrained_Access_Type;
12431 ----------------------------------
12432 -- Build_Constrained_Array_Type --
12433 ----------------------------------
12435 function Build_Constrained_Array_Type
12436 (Old_Type : Entity_Id) return Entity_Id
12438 Lo_Expr : Node_Id;
12439 Hi_Expr : Node_Id;
12440 Old_Index : Node_Id;
12441 Range_Node : Node_Id;
12442 Constr_List : List_Id;
12444 Need_To_Create_Itype : Boolean := False;
12446 begin
12447 Old_Index := First_Index (Old_Type);
12448 while Present (Old_Index) loop
12449 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
12451 if Is_Discriminant (Lo_Expr)
12452 or else
12453 Is_Discriminant (Hi_Expr)
12454 then
12455 Need_To_Create_Itype := True;
12456 end if;
12458 Next_Index (Old_Index);
12459 end loop;
12461 if Need_To_Create_Itype then
12462 Constr_List := New_List;
12464 Old_Index := First_Index (Old_Type);
12465 while Present (Old_Index) loop
12466 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
12468 if Is_Discriminant (Lo_Expr) then
12469 Lo_Expr := Get_Discr_Value (Lo_Expr);
12470 end if;
12472 if Is_Discriminant (Hi_Expr) then
12473 Hi_Expr := Get_Discr_Value (Hi_Expr);
12474 end if;
12476 Range_Node :=
12477 Make_Range
12478 (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
12480 Append (Range_Node, To => Constr_List);
12482 Next_Index (Old_Index);
12483 end loop;
12485 return Build_Subtype (Old_Type, Constr_List);
12487 else
12488 return Old_Type;
12489 end if;
12490 end Build_Constrained_Array_Type;
12492 ------------------------------------------
12493 -- Build_Constrained_Discriminated_Type --
12494 ------------------------------------------
12496 function Build_Constrained_Discriminated_Type
12497 (Old_Type : Entity_Id) return Entity_Id
12499 Expr : Node_Id;
12500 Constr_List : List_Id;
12501 Old_Constraint : Elmt_Id;
12503 Need_To_Create_Itype : Boolean := False;
12505 begin
12506 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
12507 while Present (Old_Constraint) loop
12508 Expr := Node (Old_Constraint);
12510 if Is_Discriminant (Expr) then
12511 Need_To_Create_Itype := True;
12512 end if;
12514 Next_Elmt (Old_Constraint);
12515 end loop;
12517 if Need_To_Create_Itype then
12518 Constr_List := New_List;
12520 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
12521 while Present (Old_Constraint) loop
12522 Expr := Node (Old_Constraint);
12524 if Is_Discriminant (Expr) then
12525 Expr := Get_Discr_Value (Expr);
12526 end if;
12528 Append (New_Copy_Tree (Expr), To => Constr_List);
12530 Next_Elmt (Old_Constraint);
12531 end loop;
12533 return Build_Subtype (Old_Type, Constr_List);
12535 else
12536 return Old_Type;
12537 end if;
12538 end Build_Constrained_Discriminated_Type;
12540 -------------------
12541 -- Build_Subtype --
12542 -------------------
12544 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id is
12545 Indic : Node_Id;
12546 Subtyp_Decl : Node_Id;
12547 Def_Id : Entity_Id;
12548 Btyp : Entity_Id := Base_Type (T);
12550 begin
12551 -- The Related_Node better be here or else we won't be able to
12552 -- attach new itypes to a node in the tree.
12554 pragma Assert (Present (Related_Node));
12556 -- If the view of the component's type is incomplete or private
12557 -- with unknown discriminants, then the constraint must be applied
12558 -- to the full type.
12560 if Has_Unknown_Discriminants (Btyp)
12561 and then Present (Underlying_Type (Btyp))
12562 then
12563 Btyp := Underlying_Type (Btyp);
12564 end if;
12566 Indic :=
12567 Make_Subtype_Indication (Loc,
12568 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
12569 Constraint => Make_Index_Or_Discriminant_Constraint (Loc, C));
12571 Def_Id := Create_Itype (Ekind (T), Related_Node);
12573 Subtyp_Decl :=
12574 Make_Subtype_Declaration (Loc,
12575 Defining_Identifier => Def_Id,
12576 Subtype_Indication => Indic);
12578 Set_Parent (Subtyp_Decl, Parent (Related_Node));
12580 -- Itypes must be analyzed with checks off (see package Itypes)
12582 Analyze (Subtyp_Decl, Suppress => All_Checks);
12584 return Def_Id;
12585 end Build_Subtype;
12587 ---------------------
12588 -- Get_Discr_Value --
12589 ---------------------
12591 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id is
12592 D : Entity_Id;
12593 E : Elmt_Id;
12595 begin
12596 -- The discriminant may be declared for the type, in which case we
12597 -- find it by iterating over the list of discriminants. If the
12598 -- discriminant is inherited from a parent type, it appears as the
12599 -- corresponding discriminant of the current type. This will be the
12600 -- case when constraining an inherited component whose constraint is
12601 -- given by a discriminant of the parent.
12603 D := First_Discriminant (Typ);
12604 E := First_Elmt (Constraints);
12606 while Present (D) loop
12607 if D = Entity (Discrim)
12608 or else D = CR_Discriminant (Entity (Discrim))
12609 or else Corresponding_Discriminant (D) = Entity (Discrim)
12610 then
12611 return Node (E);
12612 end if;
12614 Next_Discriminant (D);
12615 Next_Elmt (E);
12616 end loop;
12618 -- The Corresponding_Discriminant mechanism is incomplete, because
12619 -- the correspondence between new and old discriminants is not one
12620 -- to one: one new discriminant can constrain several old ones. In
12621 -- that case, scan sequentially the stored_constraint, the list of
12622 -- discriminants of the parents, and the constraints.
12624 -- Previous code checked for the present of the Stored_Constraint
12625 -- list for the derived type, but did not use it at all. Should it
12626 -- be present when the component is a discriminated task type?
12628 if Is_Derived_Type (Typ)
12629 and then Scope (Entity (Discrim)) = Etype (Typ)
12630 then
12631 D := First_Discriminant (Etype (Typ));
12632 E := First_Elmt (Constraints);
12633 while Present (D) loop
12634 if D = Entity (Discrim) then
12635 return Node (E);
12636 end if;
12638 Next_Discriminant (D);
12639 Next_Elmt (E);
12640 end loop;
12641 end if;
12643 -- Something is wrong if we did not find the value
12645 raise Program_Error;
12646 end Get_Discr_Value;
12648 ---------------------
12649 -- Is_Discriminant --
12650 ---------------------
12652 function Is_Discriminant (Expr : Node_Id) return Boolean is
12653 Discrim_Scope : Entity_Id;
12655 begin
12656 if Denotes_Discriminant (Expr) then
12657 Discrim_Scope := Scope (Entity (Expr));
12659 -- Either we have a reference to one of Typ's discriminants,
12661 pragma Assert (Discrim_Scope = Typ
12663 -- or to the discriminants of the parent type, in the case
12664 -- of a derivation of a tagged type with variants.
12666 or else Discrim_Scope = Etype (Typ)
12667 or else Full_View (Discrim_Scope) = Etype (Typ)
12669 -- or same as above for the case where the discriminants
12670 -- were declared in Typ's private view.
12672 or else (Is_Private_Type (Discrim_Scope)
12673 and then Chars (Discrim_Scope) = Chars (Typ))
12675 -- or else we are deriving from the full view and the
12676 -- discriminant is declared in the private entity.
12678 or else (Is_Private_Type (Typ)
12679 and then Chars (Discrim_Scope) = Chars (Typ))
12681 -- Or we are constrained the corresponding record of a
12682 -- synchronized type that completes a private declaration.
12684 or else (Is_Concurrent_Record_Type (Typ)
12685 and then
12686 Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
12688 -- or we have a class-wide type, in which case make sure the
12689 -- discriminant found belongs to the root type.
12691 or else (Is_Class_Wide_Type (Typ)
12692 and then Etype (Typ) = Discrim_Scope));
12694 return True;
12695 end if;
12697 -- In all other cases we have something wrong
12699 return False;
12700 end Is_Discriminant;
12702 -- Start of processing for Constrain_Component_Type
12704 begin
12705 if Nkind (Parent (Comp)) = N_Component_Declaration
12706 and then Comes_From_Source (Parent (Comp))
12707 and then Comes_From_Source
12708 (Subtype_Indication (Component_Definition (Parent (Comp))))
12709 and then
12710 Is_Entity_Name
12711 (Subtype_Indication (Component_Definition (Parent (Comp))))
12712 then
12713 return Compon_Type;
12715 elsif Is_Array_Type (Compon_Type) then
12716 return Build_Constrained_Array_Type (Compon_Type);
12718 elsif Has_Discriminants (Compon_Type) then
12719 return Build_Constrained_Discriminated_Type (Compon_Type);
12721 elsif Is_Access_Type (Compon_Type) then
12722 return Build_Constrained_Access_Type (Compon_Type);
12724 else
12725 return Compon_Type;
12726 end if;
12727 end Constrain_Component_Type;
12729 --------------------------
12730 -- Constrain_Concurrent --
12731 --------------------------
12733 -- For concurrent types, the associated record value type carries the same
12734 -- discriminants, so when we constrain a concurrent type, we must constrain
12735 -- the corresponding record type as well.
12737 procedure Constrain_Concurrent
12738 (Def_Id : in out Entity_Id;
12739 SI : Node_Id;
12740 Related_Nod : Node_Id;
12741 Related_Id : Entity_Id;
12742 Suffix : Character)
12744 -- Retrieve Base_Type to ensure getting to the concurrent type in the
12745 -- case of a private subtype (needed when only doing semantic analysis).
12747 T_Ent : Entity_Id := Base_Type (Entity (Subtype_Mark (SI)));
12748 T_Val : Entity_Id;
12750 begin
12751 if Is_Access_Type (T_Ent) then
12752 T_Ent := Designated_Type (T_Ent);
12753 end if;
12755 T_Val := Corresponding_Record_Type (T_Ent);
12757 if Present (T_Val) then
12759 if No (Def_Id) then
12760 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
12761 end if;
12763 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
12765 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
12766 Set_Corresponding_Record_Type (Def_Id,
12767 Constrain_Corresponding_Record (Def_Id, T_Val, Related_Nod));
12769 else
12770 -- If there is no associated record, expansion is disabled and this
12771 -- is a generic context. Create a subtype in any case, so that
12772 -- semantic analysis can proceed.
12774 if No (Def_Id) then
12775 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
12776 end if;
12778 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
12779 end if;
12780 end Constrain_Concurrent;
12782 ------------------------------------
12783 -- Constrain_Corresponding_Record --
12784 ------------------------------------
12786 function Constrain_Corresponding_Record
12787 (Prot_Subt : Entity_Id;
12788 Corr_Rec : Entity_Id;
12789 Related_Nod : Node_Id) return Entity_Id
12791 T_Sub : constant Entity_Id :=
12792 Create_Itype (E_Record_Subtype, Related_Nod, Corr_Rec, 'C');
12794 begin
12795 Set_Etype (T_Sub, Corr_Rec);
12796 Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
12797 Set_Is_Constrained (T_Sub, True);
12798 Set_First_Entity (T_Sub, First_Entity (Corr_Rec));
12799 Set_Last_Entity (T_Sub, Last_Entity (Corr_Rec));
12801 if Has_Discriminants (Prot_Subt) then -- False only if errors.
12802 Set_Discriminant_Constraint
12803 (T_Sub, Discriminant_Constraint (Prot_Subt));
12804 Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
12805 Create_Constrained_Components
12806 (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
12807 end if;
12809 Set_Depends_On_Private (T_Sub, Has_Private_Component (T_Sub));
12811 if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
12812 Conditional_Delay (T_Sub, Corr_Rec);
12814 else
12815 -- This is a component subtype: it will be frozen in the context of
12816 -- the enclosing record's init_proc, so that discriminant references
12817 -- are resolved to discriminals. (Note: we used to skip freezing
12818 -- altogether in that case, which caused errors downstream for
12819 -- components of a bit packed array type).
12821 Set_Has_Delayed_Freeze (T_Sub);
12822 end if;
12824 return T_Sub;
12825 end Constrain_Corresponding_Record;
12827 -----------------------
12828 -- Constrain_Decimal --
12829 -----------------------
12831 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id) is
12832 T : constant Entity_Id := Entity (Subtype_Mark (S));
12833 C : constant Node_Id := Constraint (S);
12834 Loc : constant Source_Ptr := Sloc (C);
12835 Range_Expr : Node_Id;
12836 Digits_Expr : Node_Id;
12837 Digits_Val : Uint;
12838 Bound_Val : Ureal;
12840 begin
12841 Set_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
12843 if Nkind (C) = N_Range_Constraint then
12844 Range_Expr := Range_Expression (C);
12845 Digits_Val := Digits_Value (T);
12847 else
12848 pragma Assert (Nkind (C) = N_Digits_Constraint);
12850 Check_SPARK_05_Restriction ("digits constraint is not allowed", S);
12852 Digits_Expr := Digits_Expression (C);
12853 Analyze_And_Resolve (Digits_Expr, Any_Integer);
12855 Check_Digits_Expression (Digits_Expr);
12856 Digits_Val := Expr_Value (Digits_Expr);
12858 if Digits_Val > Digits_Value (T) then
12859 Error_Msg_N
12860 ("digits expression is incompatible with subtype", C);
12861 Digits_Val := Digits_Value (T);
12862 end if;
12864 if Present (Range_Constraint (C)) then
12865 Range_Expr := Range_Expression (Range_Constraint (C));
12866 else
12867 Range_Expr := Empty;
12868 end if;
12869 end if;
12871 Set_Etype (Def_Id, Base_Type (T));
12872 Set_Size_Info (Def_Id, (T));
12873 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
12874 Set_Delta_Value (Def_Id, Delta_Value (T));
12875 Set_Scale_Value (Def_Id, Scale_Value (T));
12876 Set_Small_Value (Def_Id, Small_Value (T));
12877 Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
12878 Set_Digits_Value (Def_Id, Digits_Val);
12880 -- Manufacture range from given digits value if no range present
12882 if No (Range_Expr) then
12883 Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
12884 Range_Expr :=
12885 Make_Range (Loc,
12886 Low_Bound =>
12887 Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
12888 High_Bound =>
12889 Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
12890 end if;
12892 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
12893 Set_Discrete_RM_Size (Def_Id);
12895 -- Unconditionally delay the freeze, since we cannot set size
12896 -- information in all cases correctly until the freeze point.
12898 Set_Has_Delayed_Freeze (Def_Id);
12899 end Constrain_Decimal;
12901 ----------------------------------
12902 -- Constrain_Discriminated_Type --
12903 ----------------------------------
12905 procedure Constrain_Discriminated_Type
12906 (Def_Id : Entity_Id;
12907 S : Node_Id;
12908 Related_Nod : Node_Id;
12909 For_Access : Boolean := False)
12911 E : constant Entity_Id := Entity (Subtype_Mark (S));
12912 T : Entity_Id;
12913 C : Node_Id;
12914 Elist : Elist_Id := New_Elmt_List;
12916 procedure Fixup_Bad_Constraint;
12917 -- This is called after finding a bad constraint, and after having
12918 -- posted an appropriate error message. The mission is to leave the
12919 -- entity T in as reasonable state as possible.
12921 --------------------------
12922 -- Fixup_Bad_Constraint --
12923 --------------------------
12925 procedure Fixup_Bad_Constraint is
12926 begin
12927 -- Set a reasonable Ekind for the entity. For an incomplete type,
12928 -- we can't do much, but for other types, we can set the proper
12929 -- corresponding subtype kind.
12931 if Ekind (T) = E_Incomplete_Type then
12932 Set_Ekind (Def_Id, Ekind (T));
12933 else
12934 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
12935 end if;
12937 -- Set Etype to the known type, to reduce chances of cascaded errors
12939 Set_Etype (Def_Id, E);
12940 Set_Error_Posted (Def_Id);
12941 end Fixup_Bad_Constraint;
12943 -- Start of processing for Constrain_Discriminated_Type
12945 begin
12946 C := Constraint (S);
12948 -- A discriminant constraint is only allowed in a subtype indication,
12949 -- after a subtype mark. This subtype mark must denote either a type
12950 -- with discriminants, or an access type whose designated type is a
12951 -- type with discriminants. A discriminant constraint specifies the
12952 -- values of these discriminants (RM 3.7.2(5)).
12954 T := Base_Type (Entity (Subtype_Mark (S)));
12956 if Is_Access_Type (T) then
12957 T := Designated_Type (T);
12958 end if;
12960 -- Ada 2005 (AI-412): Constrained incomplete subtypes are illegal.
12961 -- Avoid generating an error for access-to-incomplete subtypes.
12963 if Ada_Version >= Ada_2005
12964 and then Ekind (T) = E_Incomplete_Type
12965 and then Nkind (Parent (S)) = N_Subtype_Declaration
12966 and then not Is_Itype (Def_Id)
12967 then
12968 -- A little sanity check, emit an error message if the type
12969 -- has discriminants to begin with. Type T may be a regular
12970 -- incomplete type or imported via a limited with clause.
12972 if Has_Discriminants (T)
12973 or else (From_Limited_With (T)
12974 and then Present (Non_Limited_View (T))
12975 and then Nkind (Parent (Non_Limited_View (T))) =
12976 N_Full_Type_Declaration
12977 and then Present (Discriminant_Specifications
12978 (Parent (Non_Limited_View (T)))))
12979 then
12980 Error_Msg_N
12981 ("(Ada 2005) incomplete subtype may not be constrained", C);
12982 else
12983 Error_Msg_N ("invalid constraint: type has no discriminant", C);
12984 end if;
12986 Fixup_Bad_Constraint;
12987 return;
12989 -- Check that the type has visible discriminants. The type may be
12990 -- a private type with unknown discriminants whose full view has
12991 -- discriminants which are invisible.
12993 elsif not Has_Discriminants (T)
12994 or else
12995 (Has_Unknown_Discriminants (T)
12996 and then Is_Private_Type (T))
12997 then
12998 Error_Msg_N ("invalid constraint: type has no discriminant", C);
12999 Fixup_Bad_Constraint;
13000 return;
13002 elsif Is_Constrained (E)
13003 or else (Ekind (E) = E_Class_Wide_Subtype
13004 and then Present (Discriminant_Constraint (E)))
13005 then
13006 Error_Msg_N ("type is already constrained", Subtype_Mark (S));
13007 Fixup_Bad_Constraint;
13008 return;
13009 end if;
13011 -- T may be an unconstrained subtype (e.g. a generic actual).
13012 -- Constraint applies to the base type.
13014 T := Base_Type (T);
13016 Elist := Build_Discriminant_Constraints (T, S);
13018 -- If the list returned was empty we had an error in building the
13019 -- discriminant constraint. We have also already signalled an error
13020 -- in the incomplete type case
13022 if Is_Empty_Elmt_List (Elist) then
13023 Fixup_Bad_Constraint;
13024 return;
13025 end if;
13027 Build_Discriminated_Subtype (T, Def_Id, Elist, Related_Nod, For_Access);
13028 end Constrain_Discriminated_Type;
13030 ---------------------------
13031 -- Constrain_Enumeration --
13032 ---------------------------
13034 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id) is
13035 T : constant Entity_Id := Entity (Subtype_Mark (S));
13036 C : constant Node_Id := Constraint (S);
13038 begin
13039 Set_Ekind (Def_Id, E_Enumeration_Subtype);
13041 Set_First_Literal (Def_Id, First_Literal (Base_Type (T)));
13043 Set_Etype (Def_Id, Base_Type (T));
13044 Set_Size_Info (Def_Id, (T));
13045 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13046 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
13048 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13050 Set_Discrete_RM_Size (Def_Id);
13051 end Constrain_Enumeration;
13053 ----------------------
13054 -- Constrain_Float --
13055 ----------------------
13057 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id) is
13058 T : constant Entity_Id := Entity (Subtype_Mark (S));
13059 C : Node_Id;
13060 D : Node_Id;
13061 Rais : Node_Id;
13063 begin
13064 Set_Ekind (Def_Id, E_Floating_Point_Subtype);
13066 Set_Etype (Def_Id, Base_Type (T));
13067 Set_Size_Info (Def_Id, (T));
13068 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13070 -- Process the constraint
13072 C := Constraint (S);
13074 -- Digits constraint present
13076 if Nkind (C) = N_Digits_Constraint then
13078 Check_SPARK_05_Restriction ("digits constraint is not allowed", S);
13079 Check_Restriction (No_Obsolescent_Features, C);
13081 if Warn_On_Obsolescent_Feature then
13082 Error_Msg_N
13083 ("subtype digits constraint is an " &
13084 "obsolescent feature (RM J.3(8))?j?", C);
13085 end if;
13087 D := Digits_Expression (C);
13088 Analyze_And_Resolve (D, Any_Integer);
13089 Check_Digits_Expression (D);
13090 Set_Digits_Value (Def_Id, Expr_Value (D));
13092 -- Check that digits value is in range. Obviously we can do this
13093 -- at compile time, but it is strictly a runtime check, and of
13094 -- course there is an ACVC test that checks this.
13096 if Digits_Value (Def_Id) > Digits_Value (T) then
13097 Error_Msg_Uint_1 := Digits_Value (T);
13098 Error_Msg_N ("??digits value is too large, maximum is ^", D);
13099 Rais :=
13100 Make_Raise_Constraint_Error (Sloc (D),
13101 Reason => CE_Range_Check_Failed);
13102 Insert_Action (Declaration_Node (Def_Id), Rais);
13103 end if;
13105 C := Range_Constraint (C);
13107 -- No digits constraint present
13109 else
13110 Set_Digits_Value (Def_Id, Digits_Value (T));
13111 end if;
13113 -- Range constraint present
13115 if Nkind (C) = N_Range_Constraint then
13116 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13118 -- No range constraint present
13120 else
13121 pragma Assert (No (C));
13122 Set_Scalar_Range (Def_Id, Scalar_Range (T));
13123 end if;
13125 Set_Is_Constrained (Def_Id);
13126 end Constrain_Float;
13128 ---------------------
13129 -- Constrain_Index --
13130 ---------------------
13132 procedure Constrain_Index
13133 (Index : Node_Id;
13134 S : Node_Id;
13135 Related_Nod : Node_Id;
13136 Related_Id : Entity_Id;
13137 Suffix : Character;
13138 Suffix_Index : Nat)
13140 Def_Id : Entity_Id;
13141 R : Node_Id := Empty;
13142 T : constant Entity_Id := Etype (Index);
13144 begin
13145 if Nkind (S) = N_Range
13146 or else
13147 (Nkind (S) = N_Attribute_Reference
13148 and then Attribute_Name (S) = Name_Range)
13149 then
13150 -- A Range attribute will be transformed into N_Range by Resolve
13152 Analyze (S);
13153 Set_Etype (S, T);
13154 R := S;
13156 Process_Range_Expr_In_Decl (R, T);
13158 if not Error_Posted (S)
13159 and then
13160 (Nkind (S) /= N_Range
13161 or else not Covers (T, (Etype (Low_Bound (S))))
13162 or else not Covers (T, (Etype (High_Bound (S)))))
13163 then
13164 if Base_Type (T) /= Any_Type
13165 and then Etype (Low_Bound (S)) /= Any_Type
13166 and then Etype (High_Bound (S)) /= Any_Type
13167 then
13168 Error_Msg_N ("range expected", S);
13169 end if;
13170 end if;
13172 elsif Nkind (S) = N_Subtype_Indication then
13174 -- The parser has verified that this is a discrete indication
13176 Resolve_Discrete_Subtype_Indication (S, T);
13177 Bad_Predicated_Subtype_Use
13178 ("subtype& has predicate, not allowed in index constraint",
13179 S, Entity (Subtype_Mark (S)));
13181 R := Range_Expression (Constraint (S));
13183 -- Capture values of bounds and generate temporaries for them if
13184 -- needed, since checks may cause duplication of the expressions
13185 -- which must not be reevaluated.
13187 -- The forced evaluation removes side effects from expressions, which
13188 -- should occur also in GNATprove mode. Otherwise, we end up with
13189 -- unexpected insertions of actions at places where this is not
13190 -- supposed to occur, e.g. on default parameters of a call.
13192 if Expander_Active or GNATprove_Mode then
13193 Force_Evaluation (Low_Bound (R));
13194 Force_Evaluation (High_Bound (R));
13195 end if;
13197 elsif Nkind (S) = N_Discriminant_Association then
13199 -- Syntactically valid in subtype indication
13201 Error_Msg_N ("invalid index constraint", S);
13202 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
13203 return;
13205 -- Subtype_Mark case, no anonymous subtypes to construct
13207 else
13208 Analyze (S);
13210 if Is_Entity_Name (S) then
13211 if not Is_Type (Entity (S)) then
13212 Error_Msg_N ("expect subtype mark for index constraint", S);
13214 elsif Base_Type (Entity (S)) /= Base_Type (T) then
13215 Wrong_Type (S, Base_Type (T));
13217 -- Check error of subtype with predicate in index constraint
13219 else
13220 Bad_Predicated_Subtype_Use
13221 ("subtype& has predicate, not allowed in index constraint",
13222 S, Entity (S));
13223 end if;
13225 return;
13227 else
13228 Error_Msg_N ("invalid index constraint", S);
13229 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
13230 return;
13231 end if;
13232 end if;
13234 Def_Id :=
13235 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
13237 Set_Etype (Def_Id, Base_Type (T));
13239 if Is_Modular_Integer_Type (T) then
13240 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
13242 elsif Is_Integer_Type (T) then
13243 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
13245 else
13246 Set_Ekind (Def_Id, E_Enumeration_Subtype);
13247 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
13248 Set_First_Literal (Def_Id, First_Literal (T));
13249 end if;
13251 Set_Size_Info (Def_Id, (T));
13252 Set_RM_Size (Def_Id, RM_Size (T));
13253 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13255 Set_Scalar_Range (Def_Id, R);
13257 Set_Etype (S, Def_Id);
13258 Set_Discrete_RM_Size (Def_Id);
13259 end Constrain_Index;
13261 -----------------------
13262 -- Constrain_Integer --
13263 -----------------------
13265 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id) is
13266 T : constant Entity_Id := Entity (Subtype_Mark (S));
13267 C : constant Node_Id := Constraint (S);
13269 begin
13270 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13272 if Is_Modular_Integer_Type (T) then
13273 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
13274 else
13275 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
13276 end if;
13278 Set_Etype (Def_Id, Base_Type (T));
13279 Set_Size_Info (Def_Id, (T));
13280 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13281 Set_Discrete_RM_Size (Def_Id);
13282 end Constrain_Integer;
13284 ------------------------------
13285 -- Constrain_Ordinary_Fixed --
13286 ------------------------------
13288 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id) is
13289 T : constant Entity_Id := Entity (Subtype_Mark (S));
13290 C : Node_Id;
13291 D : Node_Id;
13292 Rais : Node_Id;
13294 begin
13295 Set_Ekind (Def_Id, E_Ordinary_Fixed_Point_Subtype);
13296 Set_Etype (Def_Id, Base_Type (T));
13297 Set_Size_Info (Def_Id, (T));
13298 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13299 Set_Small_Value (Def_Id, Small_Value (T));
13301 -- Process the constraint
13303 C := Constraint (S);
13305 -- Delta constraint present
13307 if Nkind (C) = N_Delta_Constraint then
13309 Check_SPARK_05_Restriction ("delta constraint is not allowed", S);
13310 Check_Restriction (No_Obsolescent_Features, C);
13312 if Warn_On_Obsolescent_Feature then
13313 Error_Msg_S
13314 ("subtype delta constraint is an " &
13315 "obsolescent feature (RM J.3(7))?j?");
13316 end if;
13318 D := Delta_Expression (C);
13319 Analyze_And_Resolve (D, Any_Real);
13320 Check_Delta_Expression (D);
13321 Set_Delta_Value (Def_Id, Expr_Value_R (D));
13323 -- Check that delta value is in range. Obviously we can do this
13324 -- at compile time, but it is strictly a runtime check, and of
13325 -- course there is an ACVC test that checks this.
13327 if Delta_Value (Def_Id) < Delta_Value (T) then
13328 Error_Msg_N ("??delta value is too small", D);
13329 Rais :=
13330 Make_Raise_Constraint_Error (Sloc (D),
13331 Reason => CE_Range_Check_Failed);
13332 Insert_Action (Declaration_Node (Def_Id), Rais);
13333 end if;
13335 C := Range_Constraint (C);
13337 -- No delta constraint present
13339 else
13340 Set_Delta_Value (Def_Id, Delta_Value (T));
13341 end if;
13343 -- Range constraint present
13345 if Nkind (C) = N_Range_Constraint then
13346 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13348 -- No range constraint present
13350 else
13351 pragma Assert (No (C));
13352 Set_Scalar_Range (Def_Id, Scalar_Range (T));
13354 end if;
13356 Set_Discrete_RM_Size (Def_Id);
13358 -- Unconditionally delay the freeze, since we cannot set size
13359 -- information in all cases correctly until the freeze point.
13361 Set_Has_Delayed_Freeze (Def_Id);
13362 end Constrain_Ordinary_Fixed;
13364 -----------------------
13365 -- Contain_Interface --
13366 -----------------------
13368 function Contain_Interface
13369 (Iface : Entity_Id;
13370 Ifaces : Elist_Id) return Boolean
13372 Iface_Elmt : Elmt_Id;
13374 begin
13375 if Present (Ifaces) then
13376 Iface_Elmt := First_Elmt (Ifaces);
13377 while Present (Iface_Elmt) loop
13378 if Node (Iface_Elmt) = Iface then
13379 return True;
13380 end if;
13382 Next_Elmt (Iface_Elmt);
13383 end loop;
13384 end if;
13386 return False;
13387 end Contain_Interface;
13389 ---------------------------
13390 -- Convert_Scalar_Bounds --
13391 ---------------------------
13393 procedure Convert_Scalar_Bounds
13394 (N : Node_Id;
13395 Parent_Type : Entity_Id;
13396 Derived_Type : Entity_Id;
13397 Loc : Source_Ptr)
13399 Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
13401 Lo : Node_Id;
13402 Hi : Node_Id;
13403 Rng : Node_Id;
13405 begin
13406 -- Defend against previous errors
13408 if No (Scalar_Range (Derived_Type)) then
13409 Check_Error_Detected;
13410 return;
13411 end if;
13413 Lo := Build_Scalar_Bound
13414 (Type_Low_Bound (Derived_Type),
13415 Parent_Type, Implicit_Base);
13417 Hi := Build_Scalar_Bound
13418 (Type_High_Bound (Derived_Type),
13419 Parent_Type, Implicit_Base);
13421 Rng :=
13422 Make_Range (Loc,
13423 Low_Bound => Lo,
13424 High_Bound => Hi);
13426 Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
13428 Set_Parent (Rng, N);
13429 Set_Scalar_Range (Derived_Type, Rng);
13431 -- Analyze the bounds
13433 Analyze_And_Resolve (Lo, Implicit_Base);
13434 Analyze_And_Resolve (Hi, Implicit_Base);
13436 -- Analyze the range itself, except that we do not analyze it if
13437 -- the bounds are real literals, and we have a fixed-point type.
13438 -- The reason for this is that we delay setting the bounds in this
13439 -- case till we know the final Small and Size values (see circuit
13440 -- in Freeze.Freeze_Fixed_Point_Type for further details).
13442 if Is_Fixed_Point_Type (Parent_Type)
13443 and then Nkind (Lo) = N_Real_Literal
13444 and then Nkind (Hi) = N_Real_Literal
13445 then
13446 return;
13448 -- Here we do the analysis of the range
13450 -- Note: we do this manually, since if we do a normal Analyze and
13451 -- Resolve call, there are problems with the conversions used for
13452 -- the derived type range.
13454 else
13455 Set_Etype (Rng, Implicit_Base);
13456 Set_Analyzed (Rng, True);
13457 end if;
13458 end Convert_Scalar_Bounds;
13460 -------------------
13461 -- Copy_And_Swap --
13462 -------------------
13464 procedure Copy_And_Swap (Priv, Full : Entity_Id) is
13465 begin
13466 -- Initialize new full declaration entity by copying the pertinent
13467 -- fields of the corresponding private declaration entity.
13469 -- We temporarily set Ekind to a value appropriate for a type to
13470 -- avoid assert failures in Einfo from checking for setting type
13471 -- attributes on something that is not a type. Ekind (Priv) is an
13472 -- appropriate choice, since it allowed the attributes to be set
13473 -- in the first place. This Ekind value will be modified later.
13475 Set_Ekind (Full, Ekind (Priv));
13477 -- Also set Etype temporarily to Any_Type, again, in the absence
13478 -- of errors, it will be properly reset, and if there are errors,
13479 -- then we want a value of Any_Type to remain.
13481 Set_Etype (Full, Any_Type);
13483 -- Now start copying attributes
13485 Set_Has_Discriminants (Full, Has_Discriminants (Priv));
13487 if Has_Discriminants (Full) then
13488 Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
13489 Set_Stored_Constraint (Full, Stored_Constraint (Priv));
13490 end if;
13492 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
13493 Set_Homonym (Full, Homonym (Priv));
13494 Set_Is_Immediately_Visible (Full, Is_Immediately_Visible (Priv));
13495 Set_Is_Public (Full, Is_Public (Priv));
13496 Set_Is_Pure (Full, Is_Pure (Priv));
13497 Set_Is_Tagged_Type (Full, Is_Tagged_Type (Priv));
13498 Set_Has_Pragma_Unmodified (Full, Has_Pragma_Unmodified (Priv));
13499 Set_Has_Pragma_Unreferenced (Full, Has_Pragma_Unreferenced (Priv));
13500 Set_Has_Pragma_Unreferenced_Objects
13501 (Full, Has_Pragma_Unreferenced_Objects
13502 (Priv));
13504 Conditional_Delay (Full, Priv);
13506 if Is_Tagged_Type (Full) then
13507 Set_Direct_Primitive_Operations
13508 (Full, Direct_Primitive_Operations (Priv));
13509 Set_No_Tagged_Streams_Pragma
13510 (Full, No_Tagged_Streams_Pragma (Priv));
13512 if Is_Base_Type (Priv) then
13513 Set_Class_Wide_Type (Full, Class_Wide_Type (Priv));
13514 end if;
13515 end if;
13517 Set_Is_Volatile (Full, Is_Volatile (Priv));
13518 Set_Treat_As_Volatile (Full, Treat_As_Volatile (Priv));
13519 Set_Scope (Full, Scope (Priv));
13520 Set_Next_Entity (Full, Next_Entity (Priv));
13521 Set_First_Entity (Full, First_Entity (Priv));
13522 Set_Last_Entity (Full, Last_Entity (Priv));
13524 -- If access types have been recorded for later handling, keep them in
13525 -- the full view so that they get handled when the full view freeze
13526 -- node is expanded.
13528 if Present (Freeze_Node (Priv))
13529 and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
13530 then
13531 Ensure_Freeze_Node (Full);
13532 Set_Access_Types_To_Process
13533 (Freeze_Node (Full),
13534 Access_Types_To_Process (Freeze_Node (Priv)));
13535 end if;
13537 -- Swap the two entities. Now Private is the full type entity and Full
13538 -- is the private one. They will be swapped back at the end of the
13539 -- private part. This swapping ensures that the entity that is visible
13540 -- in the private part is the full declaration.
13542 Exchange_Entities (Priv, Full);
13543 Append_Entity (Full, Scope (Full));
13544 end Copy_And_Swap;
13546 -------------------------------------
13547 -- Copy_Array_Base_Type_Attributes --
13548 -------------------------------------
13550 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
13551 begin
13552 Set_Component_Alignment (T1, Component_Alignment (T2));
13553 Set_Component_Type (T1, Component_Type (T2));
13554 Set_Component_Size (T1, Component_Size (T2));
13555 Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
13556 Set_Has_Non_Standard_Rep (T1, Has_Non_Standard_Rep (T2));
13557 Set_Has_Protected (T1, Has_Protected (T2));
13558 Set_Has_Task (T1, Has_Task (T2));
13559 Set_Is_Packed (T1, Is_Packed (T2));
13560 Set_Has_Aliased_Components (T1, Has_Aliased_Components (T2));
13561 Set_Has_Atomic_Components (T1, Has_Atomic_Components (T2));
13562 Set_Has_Volatile_Components (T1, Has_Volatile_Components (T2));
13563 end Copy_Array_Base_Type_Attributes;
13565 -----------------------------------
13566 -- Copy_Array_Subtype_Attributes --
13567 -----------------------------------
13569 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
13570 begin
13571 Set_Size_Info (T1, T2);
13573 Set_First_Index (T1, First_Index (T2));
13574 Set_Is_Aliased (T1, Is_Aliased (T2));
13575 Set_Is_Volatile (T1, Is_Volatile (T2));
13576 Set_Treat_As_Volatile (T1, Treat_As_Volatile (T2));
13577 Set_Is_Constrained (T1, Is_Constrained (T2));
13578 Set_Depends_On_Private (T1, Has_Private_Component (T2));
13579 Inherit_Rep_Item_Chain (T1, T2);
13580 Set_Convention (T1, Convention (T2));
13581 Set_Is_Limited_Composite (T1, Is_Limited_Composite (T2));
13582 Set_Is_Private_Composite (T1, Is_Private_Composite (T2));
13583 Set_Packed_Array_Impl_Type (T1, Packed_Array_Impl_Type (T2));
13584 end Copy_Array_Subtype_Attributes;
13586 -----------------------------------
13587 -- Create_Constrained_Components --
13588 -----------------------------------
13590 procedure Create_Constrained_Components
13591 (Subt : Entity_Id;
13592 Decl_Node : Node_Id;
13593 Typ : Entity_Id;
13594 Constraints : Elist_Id)
13596 Loc : constant Source_Ptr := Sloc (Subt);
13597 Comp_List : constant Elist_Id := New_Elmt_List;
13598 Parent_Type : constant Entity_Id := Etype (Typ);
13599 Assoc_List : constant List_Id := New_List;
13600 Discr_Val : Elmt_Id;
13601 Errors : Boolean;
13602 New_C : Entity_Id;
13603 Old_C : Entity_Id;
13604 Is_Static : Boolean := True;
13606 procedure Collect_Fixed_Components (Typ : Entity_Id);
13607 -- Collect parent type components that do not appear in a variant part
13609 procedure Create_All_Components;
13610 -- Iterate over Comp_List to create the components of the subtype
13612 function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
13613 -- Creates a new component from Old_Compon, copying all the fields from
13614 -- it, including its Etype, inserts the new component in the Subt entity
13615 -- chain and returns the new component.
13617 function Is_Variant_Record (T : Entity_Id) return Boolean;
13618 -- If true, and discriminants are static, collect only components from
13619 -- variants selected by discriminant values.
13621 ------------------------------
13622 -- Collect_Fixed_Components --
13623 ------------------------------
13625 procedure Collect_Fixed_Components (Typ : Entity_Id) is
13626 begin
13627 -- Build association list for discriminants, and find components of the
13628 -- variant part selected by the values of the discriminants.
13630 Old_C := First_Discriminant (Typ);
13631 Discr_Val := First_Elmt (Constraints);
13632 while Present (Old_C) loop
13633 Append_To (Assoc_List,
13634 Make_Component_Association (Loc,
13635 Choices => New_List (New_Occurrence_Of (Old_C, Loc)),
13636 Expression => New_Copy (Node (Discr_Val))));
13638 Next_Elmt (Discr_Val);
13639 Next_Discriminant (Old_C);
13640 end loop;
13642 -- The tag and the possible parent component are unconditionally in
13643 -- the subtype.
13645 if Is_Tagged_Type (Typ) or else Has_Controlled_Component (Typ) then
13646 Old_C := First_Component (Typ);
13647 while Present (Old_C) loop
13648 if Nam_In (Chars (Old_C), Name_uTag, Name_uParent) then
13649 Append_Elmt (Old_C, Comp_List);
13650 end if;
13652 Next_Component (Old_C);
13653 end loop;
13654 end if;
13655 end Collect_Fixed_Components;
13657 ---------------------------
13658 -- Create_All_Components --
13659 ---------------------------
13661 procedure Create_All_Components is
13662 Comp : Elmt_Id;
13664 begin
13665 Comp := First_Elmt (Comp_List);
13666 while Present (Comp) loop
13667 Old_C := Node (Comp);
13668 New_C := Create_Component (Old_C);
13670 Set_Etype
13671 (New_C,
13672 Constrain_Component_Type
13673 (Old_C, Subt, Decl_Node, Typ, Constraints));
13674 Set_Is_Public (New_C, Is_Public (Subt));
13676 Next_Elmt (Comp);
13677 end loop;
13678 end Create_All_Components;
13680 ----------------------
13681 -- Create_Component --
13682 ----------------------
13684 function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
13685 New_Compon : constant Entity_Id := New_Copy (Old_Compon);
13687 begin
13688 if Ekind (Old_Compon) = E_Discriminant
13689 and then Is_Completely_Hidden (Old_Compon)
13690 then
13691 -- This is a shadow discriminant created for a discriminant of
13692 -- the parent type, which needs to be present in the subtype.
13693 -- Give the shadow discriminant an internal name that cannot
13694 -- conflict with that of visible components.
13696 Set_Chars (New_Compon, New_Internal_Name ('C'));
13697 end if;
13699 -- Set the parent so we have a proper link for freezing etc. This is
13700 -- not a real parent pointer, since of course our parent does not own
13701 -- up to us and reference us, we are an illegitimate child of the
13702 -- original parent.
13704 Set_Parent (New_Compon, Parent (Old_Compon));
13706 -- If the old component's Esize was already determined and is a
13707 -- static value, then the new component simply inherits it. Otherwise
13708 -- the old component's size may require run-time determination, but
13709 -- the new component's size still might be statically determinable
13710 -- (if, for example it has a static constraint). In that case we want
13711 -- Layout_Type to recompute the component's size, so we reset its
13712 -- size and positional fields.
13714 if Frontend_Layout_On_Target
13715 and then not Known_Static_Esize (Old_Compon)
13716 then
13717 Set_Esize (New_Compon, Uint_0);
13718 Init_Normalized_First_Bit (New_Compon);
13719 Init_Normalized_Position (New_Compon);
13720 Init_Normalized_Position_Max (New_Compon);
13721 end if;
13723 -- We do not want this node marked as Comes_From_Source, since
13724 -- otherwise it would get first class status and a separate cross-
13725 -- reference line would be generated. Illegitimate children do not
13726 -- rate such recognition.
13728 Set_Comes_From_Source (New_Compon, False);
13730 -- But it is a real entity, and a birth certificate must be properly
13731 -- registered by entering it into the entity list.
13733 Enter_Name (New_Compon);
13735 return New_Compon;
13736 end Create_Component;
13738 -----------------------
13739 -- Is_Variant_Record --
13740 -----------------------
13742 function Is_Variant_Record (T : Entity_Id) return Boolean is
13743 begin
13744 return Nkind (Parent (T)) = N_Full_Type_Declaration
13745 and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
13746 and then Present (Component_List (Type_Definition (Parent (T))))
13747 and then
13748 Present
13749 (Variant_Part (Component_List (Type_Definition (Parent (T)))));
13750 end Is_Variant_Record;
13752 -- Start of processing for Create_Constrained_Components
13754 begin
13755 pragma Assert (Subt /= Base_Type (Subt));
13756 pragma Assert (Typ = Base_Type (Typ));
13758 Set_First_Entity (Subt, Empty);
13759 Set_Last_Entity (Subt, Empty);
13761 -- Check whether constraint is fully static, in which case we can
13762 -- optimize the list of components.
13764 Discr_Val := First_Elmt (Constraints);
13765 while Present (Discr_Val) loop
13766 if not Is_OK_Static_Expression (Node (Discr_Val)) then
13767 Is_Static := False;
13768 exit;
13769 end if;
13771 Next_Elmt (Discr_Val);
13772 end loop;
13774 Set_Has_Static_Discriminants (Subt, Is_Static);
13776 Push_Scope (Subt);
13778 -- Inherit the discriminants of the parent type
13780 Add_Discriminants : declare
13781 Num_Disc : Int;
13782 Num_Gird : Int;
13784 begin
13785 Num_Disc := 0;
13786 Old_C := First_Discriminant (Typ);
13788 while Present (Old_C) loop
13789 Num_Disc := Num_Disc + 1;
13790 New_C := Create_Component (Old_C);
13791 Set_Is_Public (New_C, Is_Public (Subt));
13792 Next_Discriminant (Old_C);
13793 end loop;
13795 -- For an untagged derived subtype, the number of discriminants may
13796 -- be smaller than the number of inherited discriminants, because
13797 -- several of them may be renamed by a single new discriminant or
13798 -- constrained. In this case, add the hidden discriminants back into
13799 -- the subtype, because they need to be present if the optimizer of
13800 -- the GCC 4.x back-end decides to break apart assignments between
13801 -- objects using the parent view into member-wise assignments.
13803 Num_Gird := 0;
13805 if Is_Derived_Type (Typ)
13806 and then not Is_Tagged_Type (Typ)
13807 then
13808 Old_C := First_Stored_Discriminant (Typ);
13810 while Present (Old_C) loop
13811 Num_Gird := Num_Gird + 1;
13812 Next_Stored_Discriminant (Old_C);
13813 end loop;
13814 end if;
13816 if Num_Gird > Num_Disc then
13818 -- Find out multiple uses of new discriminants, and add hidden
13819 -- components for the extra renamed discriminants. We recognize
13820 -- multiple uses through the Corresponding_Discriminant of a
13821 -- new discriminant: if it constrains several old discriminants,
13822 -- this field points to the last one in the parent type. The
13823 -- stored discriminants of the derived type have the same name
13824 -- as those of the parent.
13826 declare
13827 Constr : Elmt_Id;
13828 New_Discr : Entity_Id;
13829 Old_Discr : Entity_Id;
13831 begin
13832 Constr := First_Elmt (Stored_Constraint (Typ));
13833 Old_Discr := First_Stored_Discriminant (Typ);
13834 while Present (Constr) loop
13835 if Is_Entity_Name (Node (Constr))
13836 and then Ekind (Entity (Node (Constr))) = E_Discriminant
13837 then
13838 New_Discr := Entity (Node (Constr));
13840 if Chars (Corresponding_Discriminant (New_Discr)) /=
13841 Chars (Old_Discr)
13842 then
13843 -- The new discriminant has been used to rename a
13844 -- subsequent old discriminant. Introduce a shadow
13845 -- component for the current old discriminant.
13847 New_C := Create_Component (Old_Discr);
13848 Set_Original_Record_Component (New_C, Old_Discr);
13849 end if;
13851 else
13852 -- The constraint has eliminated the old discriminant.
13853 -- Introduce a shadow component.
13855 New_C := Create_Component (Old_Discr);
13856 Set_Original_Record_Component (New_C, Old_Discr);
13857 end if;
13859 Next_Elmt (Constr);
13860 Next_Stored_Discriminant (Old_Discr);
13861 end loop;
13862 end;
13863 end if;
13864 end Add_Discriminants;
13866 if Is_Static
13867 and then Is_Variant_Record (Typ)
13868 then
13869 Collect_Fixed_Components (Typ);
13871 Gather_Components (
13872 Typ,
13873 Component_List (Type_Definition (Parent (Typ))),
13874 Governed_By => Assoc_List,
13875 Into => Comp_List,
13876 Report_Errors => Errors);
13877 pragma Assert (not Errors);
13879 Create_All_Components;
13881 -- If the subtype declaration is created for a tagged type derivation
13882 -- with constraints, we retrieve the record definition of the parent
13883 -- type to select the components of the proper variant.
13885 elsif Is_Static
13886 and then Is_Tagged_Type (Typ)
13887 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
13888 and then
13889 Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
13890 and then Is_Variant_Record (Parent_Type)
13891 then
13892 Collect_Fixed_Components (Typ);
13894 Gather_Components
13895 (Typ,
13896 Component_List (Type_Definition (Parent (Parent_Type))),
13897 Governed_By => Assoc_List,
13898 Into => Comp_List,
13899 Report_Errors => Errors);
13901 -- Note: previously there was a check at this point that no errors
13902 -- were detected. As a consequence of AI05-220 there may be an error
13903 -- if an inherited discriminant that controls a variant has a non-
13904 -- static constraint.
13906 -- If the tagged derivation has a type extension, collect all the
13907 -- new components therein.
13909 if Present (Record_Extension_Part (Type_Definition (Parent (Typ))))
13910 then
13911 Old_C := First_Component (Typ);
13912 while Present (Old_C) loop
13913 if Original_Record_Component (Old_C) = Old_C
13914 and then Chars (Old_C) /= Name_uTag
13915 and then Chars (Old_C) /= Name_uParent
13916 then
13917 Append_Elmt (Old_C, Comp_List);
13918 end if;
13920 Next_Component (Old_C);
13921 end loop;
13922 end if;
13924 Create_All_Components;
13926 else
13927 -- If discriminants are not static, or if this is a multi-level type
13928 -- extension, we have to include all components of the parent type.
13930 Old_C := First_Component (Typ);
13931 while Present (Old_C) loop
13932 New_C := Create_Component (Old_C);
13934 Set_Etype
13935 (New_C,
13936 Constrain_Component_Type
13937 (Old_C, Subt, Decl_Node, Typ, Constraints));
13938 Set_Is_Public (New_C, Is_Public (Subt));
13940 Next_Component (Old_C);
13941 end loop;
13942 end if;
13944 End_Scope;
13945 end Create_Constrained_Components;
13947 ------------------------------------------
13948 -- Decimal_Fixed_Point_Type_Declaration --
13949 ------------------------------------------
13951 procedure Decimal_Fixed_Point_Type_Declaration
13952 (T : Entity_Id;
13953 Def : Node_Id)
13955 Loc : constant Source_Ptr := Sloc (Def);
13956 Digs_Expr : constant Node_Id := Digits_Expression (Def);
13957 Delta_Expr : constant Node_Id := Delta_Expression (Def);
13958 Implicit_Base : Entity_Id;
13959 Digs_Val : Uint;
13960 Delta_Val : Ureal;
13961 Scale_Val : Uint;
13962 Bound_Val : Ureal;
13964 begin
13965 Check_SPARK_05_Restriction
13966 ("decimal fixed point type is not allowed", Def);
13967 Check_Restriction (No_Fixed_Point, Def);
13969 -- Create implicit base type
13971 Implicit_Base :=
13972 Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
13973 Set_Etype (Implicit_Base, Implicit_Base);
13975 -- Analyze and process delta expression
13977 Analyze_And_Resolve (Delta_Expr, Universal_Real);
13979 Check_Delta_Expression (Delta_Expr);
13980 Delta_Val := Expr_Value_R (Delta_Expr);
13982 -- Check delta is power of 10, and determine scale value from it
13984 declare
13985 Val : Ureal;
13987 begin
13988 Scale_Val := Uint_0;
13989 Val := Delta_Val;
13991 if Val < Ureal_1 then
13992 while Val < Ureal_1 loop
13993 Val := Val * Ureal_10;
13994 Scale_Val := Scale_Val + 1;
13995 end loop;
13997 if Scale_Val > 18 then
13998 Error_Msg_N ("scale exceeds maximum value of 18", Def);
13999 Scale_Val := UI_From_Int (+18);
14000 end if;
14002 else
14003 while Val > Ureal_1 loop
14004 Val := Val / Ureal_10;
14005 Scale_Val := Scale_Val - 1;
14006 end loop;
14008 if Scale_Val < -18 then
14009 Error_Msg_N ("scale is less than minimum value of -18", Def);
14010 Scale_Val := UI_From_Int (-18);
14011 end if;
14012 end if;
14014 if Val /= Ureal_1 then
14015 Error_Msg_N ("delta expression must be a power of 10", Def);
14016 Delta_Val := Ureal_10 ** (-Scale_Val);
14017 end if;
14018 end;
14020 -- Set delta, scale and small (small = delta for decimal type)
14022 Set_Delta_Value (Implicit_Base, Delta_Val);
14023 Set_Scale_Value (Implicit_Base, Scale_Val);
14024 Set_Small_Value (Implicit_Base, Delta_Val);
14026 -- Analyze and process digits expression
14028 Analyze_And_Resolve (Digs_Expr, Any_Integer);
14029 Check_Digits_Expression (Digs_Expr);
14030 Digs_Val := Expr_Value (Digs_Expr);
14032 if Digs_Val > 18 then
14033 Digs_Val := UI_From_Int (+18);
14034 Error_Msg_N ("digits value out of range, maximum is 18", Digs_Expr);
14035 end if;
14037 Set_Digits_Value (Implicit_Base, Digs_Val);
14038 Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
14040 -- Set range of base type from digits value for now. This will be
14041 -- expanded to represent the true underlying base range by Freeze.
14043 Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
14045 -- Note: We leave size as zero for now, size will be set at freeze
14046 -- time. We have to do this for ordinary fixed-point, because the size
14047 -- depends on the specified small, and we might as well do the same for
14048 -- decimal fixed-point.
14050 pragma Assert (Esize (Implicit_Base) = Uint_0);
14052 -- If there are bounds given in the declaration use them as the
14053 -- bounds of the first named subtype.
14055 if Present (Real_Range_Specification (Def)) then
14056 declare
14057 RRS : constant Node_Id := Real_Range_Specification (Def);
14058 Low : constant Node_Id := Low_Bound (RRS);
14059 High : constant Node_Id := High_Bound (RRS);
14060 Low_Val : Ureal;
14061 High_Val : Ureal;
14063 begin
14064 Analyze_And_Resolve (Low, Any_Real);
14065 Analyze_And_Resolve (High, Any_Real);
14066 Check_Real_Bound (Low);
14067 Check_Real_Bound (High);
14068 Low_Val := Expr_Value_R (Low);
14069 High_Val := Expr_Value_R (High);
14071 if Low_Val < (-Bound_Val) then
14072 Error_Msg_N
14073 ("range low bound too small for digits value", Low);
14074 Low_Val := -Bound_Val;
14075 end if;
14077 if High_Val > Bound_Val then
14078 Error_Msg_N
14079 ("range high bound too large for digits value", High);
14080 High_Val := Bound_Val;
14081 end if;
14083 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
14084 end;
14086 -- If no explicit range, use range that corresponds to given
14087 -- digits value. This will end up as the final range for the
14088 -- first subtype.
14090 else
14091 Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
14092 end if;
14094 -- Complete entity for first subtype. The inheritance of the rep item
14095 -- chain ensures that SPARK-related pragmas are not clobbered when the
14096 -- decimal fixed point type acts as a full view of a private type.
14098 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
14099 Set_Etype (T, Implicit_Base);
14100 Set_Size_Info (T, Implicit_Base);
14101 Inherit_Rep_Item_Chain (T, Implicit_Base);
14102 Set_Digits_Value (T, Digs_Val);
14103 Set_Delta_Value (T, Delta_Val);
14104 Set_Small_Value (T, Delta_Val);
14105 Set_Scale_Value (T, Scale_Val);
14106 Set_Is_Constrained (T);
14107 end Decimal_Fixed_Point_Type_Declaration;
14109 -----------------------------------
14110 -- Derive_Progenitor_Subprograms --
14111 -----------------------------------
14113 procedure Derive_Progenitor_Subprograms
14114 (Parent_Type : Entity_Id;
14115 Tagged_Type : Entity_Id)
14117 E : Entity_Id;
14118 Elmt : Elmt_Id;
14119 Iface : Entity_Id;
14120 Iface_Elmt : Elmt_Id;
14121 Iface_Subp : Entity_Id;
14122 New_Subp : Entity_Id := Empty;
14123 Prim_Elmt : Elmt_Id;
14124 Subp : Entity_Id;
14125 Typ : Entity_Id;
14127 begin
14128 pragma Assert (Ada_Version >= Ada_2005
14129 and then Is_Record_Type (Tagged_Type)
14130 and then Is_Tagged_Type (Tagged_Type)
14131 and then Has_Interfaces (Tagged_Type));
14133 -- Step 1: Transfer to the full-view primitives associated with the
14134 -- partial-view that cover interface primitives. Conceptually this
14135 -- work should be done later by Process_Full_View; done here to
14136 -- simplify its implementation at later stages. It can be safely
14137 -- done here because interfaces must be visible in the partial and
14138 -- private view (RM 7.3(7.3/2)).
14140 -- Small optimization: This work is only required if the parent may
14141 -- have entities whose Alias attribute reference an interface primitive.
14142 -- Such a situation may occur if the parent is an abstract type and the
14143 -- primitive has not been yet overridden or if the parent is a generic
14144 -- formal type covering interfaces.
14146 -- If the tagged type is not abstract, it cannot have abstract
14147 -- primitives (the only entities in the list of primitives of
14148 -- non-abstract tagged types that can reference abstract primitives
14149 -- through its Alias attribute are the internal entities that have
14150 -- attribute Interface_Alias, and these entities are generated later
14151 -- by Add_Internal_Interface_Entities).
14153 if In_Private_Part (Current_Scope)
14154 and then (Is_Abstract_Type (Parent_Type)
14155 or else
14156 Is_Generic_Type (Parent_Type))
14157 then
14158 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
14159 while Present (Elmt) loop
14160 Subp := Node (Elmt);
14162 -- At this stage it is not possible to have entities in the list
14163 -- of primitives that have attribute Interface_Alias.
14165 pragma Assert (No (Interface_Alias (Subp)));
14167 Typ := Find_Dispatching_Type (Ultimate_Alias (Subp));
14169 if Is_Interface (Typ) then
14170 E := Find_Primitive_Covering_Interface
14171 (Tagged_Type => Tagged_Type,
14172 Iface_Prim => Subp);
14174 if Present (E)
14175 and then Find_Dispatching_Type (Ultimate_Alias (E)) /= Typ
14176 then
14177 Replace_Elmt (Elmt, E);
14178 Remove_Homonym (Subp);
14179 end if;
14180 end if;
14182 Next_Elmt (Elmt);
14183 end loop;
14184 end if;
14186 -- Step 2: Add primitives of progenitors that are not implemented by
14187 -- parents of Tagged_Type.
14189 if Present (Interfaces (Base_Type (Tagged_Type))) then
14190 Iface_Elmt := First_Elmt (Interfaces (Base_Type (Tagged_Type)));
14191 while Present (Iface_Elmt) loop
14192 Iface := Node (Iface_Elmt);
14194 Prim_Elmt := First_Elmt (Primitive_Operations (Iface));
14195 while Present (Prim_Elmt) loop
14196 Iface_Subp := Node (Prim_Elmt);
14198 -- Exclude derivation of predefined primitives except those
14199 -- that come from source, or are inherited from one that comes
14200 -- from source. Required to catch declarations of equality
14201 -- operators of interfaces. For example:
14203 -- type Iface is interface;
14204 -- function "=" (Left, Right : Iface) return Boolean;
14206 if not Is_Predefined_Dispatching_Operation (Iface_Subp)
14207 or else Comes_From_Source (Ultimate_Alias (Iface_Subp))
14208 then
14209 E := Find_Primitive_Covering_Interface
14210 (Tagged_Type => Tagged_Type,
14211 Iface_Prim => Iface_Subp);
14213 -- If not found we derive a new primitive leaving its alias
14214 -- attribute referencing the interface primitive.
14216 if No (E) then
14217 Derive_Subprogram
14218 (New_Subp, Iface_Subp, Tagged_Type, Iface);
14220 -- Ada 2012 (AI05-0197): If the covering primitive's name
14221 -- differs from the name of the interface primitive then it
14222 -- is a private primitive inherited from a parent type. In
14223 -- such case, given that Tagged_Type covers the interface,
14224 -- the inherited private primitive becomes visible. For such
14225 -- purpose we add a new entity that renames the inherited
14226 -- private primitive.
14228 elsif Chars (E) /= Chars (Iface_Subp) then
14229 pragma Assert (Has_Suffix (E, 'P'));
14230 Derive_Subprogram
14231 (New_Subp, Iface_Subp, Tagged_Type, Iface);
14232 Set_Alias (New_Subp, E);
14233 Set_Is_Abstract_Subprogram (New_Subp,
14234 Is_Abstract_Subprogram (E));
14236 -- Propagate to the full view interface entities associated
14237 -- with the partial view.
14239 elsif In_Private_Part (Current_Scope)
14240 and then Present (Alias (E))
14241 and then Alias (E) = Iface_Subp
14242 and then
14243 List_Containing (Parent (E)) /=
14244 Private_Declarations
14245 (Specification
14246 (Unit_Declaration_Node (Current_Scope)))
14247 then
14248 Append_Elmt (E, Primitive_Operations (Tagged_Type));
14249 end if;
14250 end if;
14252 Next_Elmt (Prim_Elmt);
14253 end loop;
14255 Next_Elmt (Iface_Elmt);
14256 end loop;
14257 end if;
14258 end Derive_Progenitor_Subprograms;
14260 -----------------------
14261 -- Derive_Subprogram --
14262 -----------------------
14264 procedure Derive_Subprogram
14265 (New_Subp : in out Entity_Id;
14266 Parent_Subp : Entity_Id;
14267 Derived_Type : Entity_Id;
14268 Parent_Type : Entity_Id;
14269 Actual_Subp : Entity_Id := Empty)
14271 Formal : Entity_Id;
14272 -- Formal parameter of parent primitive operation
14274 Formal_Of_Actual : Entity_Id;
14275 -- Formal parameter of actual operation, when the derivation is to
14276 -- create a renaming for a primitive operation of an actual in an
14277 -- instantiation.
14279 New_Formal : Entity_Id;
14280 -- Formal of inherited operation
14282 Visible_Subp : Entity_Id := Parent_Subp;
14284 function Is_Private_Overriding return Boolean;
14285 -- If Subp is a private overriding of a visible operation, the inherited
14286 -- operation derives from the overridden op (even though its body is the
14287 -- overriding one) and the inherited operation is visible now. See
14288 -- sem_disp to see the full details of the handling of the overridden
14289 -- subprogram, which is removed from the list of primitive operations of
14290 -- the type. The overridden subprogram is saved locally in Visible_Subp,
14291 -- and used to diagnose abstract operations that need overriding in the
14292 -- derived type.
14294 procedure Replace_Type (Id, New_Id : Entity_Id);
14295 -- When the type is an anonymous access type, create a new access type
14296 -- designating the derived type.
14298 procedure Set_Derived_Name;
14299 -- This procedure sets the appropriate Chars name for New_Subp. This
14300 -- is normally just a copy of the parent name. An exception arises for
14301 -- type support subprograms, where the name is changed to reflect the
14302 -- name of the derived type, e.g. if type foo is derived from type bar,
14303 -- then a procedure barDA is derived with a name fooDA.
14305 ---------------------------
14306 -- Is_Private_Overriding --
14307 ---------------------------
14309 function Is_Private_Overriding return Boolean is
14310 Prev : Entity_Id;
14312 begin
14313 -- If the parent is not a dispatching operation there is no
14314 -- need to investigate overridings
14316 if not Is_Dispatching_Operation (Parent_Subp) then
14317 return False;
14318 end if;
14320 -- The visible operation that is overridden is a homonym of the
14321 -- parent subprogram. We scan the homonym chain to find the one
14322 -- whose alias is the subprogram we are deriving.
14324 Prev := Current_Entity (Parent_Subp);
14325 while Present (Prev) loop
14326 if Ekind (Prev) = Ekind (Parent_Subp)
14327 and then Alias (Prev) = Parent_Subp
14328 and then Scope (Parent_Subp) = Scope (Prev)
14329 and then not Is_Hidden (Prev)
14330 then
14331 Visible_Subp := Prev;
14332 return True;
14333 end if;
14335 Prev := Homonym (Prev);
14336 end loop;
14338 return False;
14339 end Is_Private_Overriding;
14341 ------------------
14342 -- Replace_Type --
14343 ------------------
14345 procedure Replace_Type (Id, New_Id : Entity_Id) is
14346 Id_Type : constant Entity_Id := Etype (Id);
14347 Acc_Type : Entity_Id;
14348 Par : constant Node_Id := Parent (Derived_Type);
14350 begin
14351 -- When the type is an anonymous access type, create a new access
14352 -- type designating the derived type. This itype must be elaborated
14353 -- at the point of the derivation, not on subsequent calls that may
14354 -- be out of the proper scope for Gigi, so we insert a reference to
14355 -- it after the derivation.
14357 if Ekind (Id_Type) = E_Anonymous_Access_Type then
14358 declare
14359 Desig_Typ : Entity_Id := Designated_Type (Id_Type);
14361 begin
14362 if Ekind (Desig_Typ) = E_Record_Type_With_Private
14363 and then Present (Full_View (Desig_Typ))
14364 and then not Is_Private_Type (Parent_Type)
14365 then
14366 Desig_Typ := Full_View (Desig_Typ);
14367 end if;
14369 if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
14371 -- Ada 2005 (AI-251): Handle also derivations of abstract
14372 -- interface primitives.
14374 or else (Is_Interface (Desig_Typ)
14375 and then not Is_Class_Wide_Type (Desig_Typ))
14376 then
14377 Acc_Type := New_Copy (Id_Type);
14378 Set_Etype (Acc_Type, Acc_Type);
14379 Set_Scope (Acc_Type, New_Subp);
14381 -- Set size of anonymous access type. If we have an access
14382 -- to an unconstrained array, this is a fat pointer, so it
14383 -- is sizes at twice addtress size.
14385 if Is_Array_Type (Desig_Typ)
14386 and then not Is_Constrained (Desig_Typ)
14387 then
14388 Init_Size (Acc_Type, 2 * System_Address_Size);
14390 -- Other cases use a thin pointer
14392 else
14393 Init_Size (Acc_Type, System_Address_Size);
14394 end if;
14396 -- Set remaining characterstics of anonymous access type
14398 Init_Alignment (Acc_Type);
14399 Set_Directly_Designated_Type (Acc_Type, Derived_Type);
14401 Set_Etype (New_Id, Acc_Type);
14402 Set_Scope (New_Id, New_Subp);
14404 -- Create a reference to it
14406 Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
14408 else
14409 Set_Etype (New_Id, Id_Type);
14410 end if;
14411 end;
14413 -- In Ada2012, a formal may have an incomplete type but the type
14414 -- derivation that inherits the primitive follows the full view.
14416 elsif Base_Type (Id_Type) = Base_Type (Parent_Type)
14417 or else
14418 (Ekind (Id_Type) = E_Record_Type_With_Private
14419 and then Present (Full_View (Id_Type))
14420 and then
14421 Base_Type (Full_View (Id_Type)) = Base_Type (Parent_Type))
14422 or else
14423 (Ada_Version >= Ada_2012
14424 and then Ekind (Id_Type) = E_Incomplete_Type
14425 and then Full_View (Id_Type) = Parent_Type)
14426 then
14427 -- Constraint checks on formals are generated during expansion,
14428 -- based on the signature of the original subprogram. The bounds
14429 -- of the derived type are not relevant, and thus we can use
14430 -- the base type for the formals. However, the return type may be
14431 -- used in a context that requires that the proper static bounds
14432 -- be used (a case statement, for example) and for those cases
14433 -- we must use the derived type (first subtype), not its base.
14435 -- If the derived_type_definition has no constraints, we know that
14436 -- the derived type has the same constraints as the first subtype
14437 -- of the parent, and we can also use it rather than its base,
14438 -- which can lead to more efficient code.
14440 if Etype (Id) = Parent_Type then
14441 if Is_Scalar_Type (Parent_Type)
14442 and then
14443 Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
14444 then
14445 Set_Etype (New_Id, Derived_Type);
14447 elsif Nkind (Par) = N_Full_Type_Declaration
14448 and then
14449 Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
14450 and then
14451 Is_Entity_Name
14452 (Subtype_Indication (Type_Definition (Par)))
14453 then
14454 Set_Etype (New_Id, Derived_Type);
14456 else
14457 Set_Etype (New_Id, Base_Type (Derived_Type));
14458 end if;
14460 else
14461 Set_Etype (New_Id, Base_Type (Derived_Type));
14462 end if;
14464 else
14465 Set_Etype (New_Id, Etype (Id));
14466 end if;
14467 end Replace_Type;
14469 ----------------------
14470 -- Set_Derived_Name --
14471 ----------------------
14473 procedure Set_Derived_Name is
14474 Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
14475 begin
14476 if Nm = TSS_Null then
14477 Set_Chars (New_Subp, Chars (Parent_Subp));
14478 else
14479 Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
14480 end if;
14481 end Set_Derived_Name;
14483 -- Start of processing for Derive_Subprogram
14485 begin
14486 New_Subp := New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
14487 Set_Ekind (New_Subp, Ekind (Parent_Subp));
14489 -- Check whether the inherited subprogram is a private operation that
14490 -- should be inherited but not yet made visible. Such subprograms can
14491 -- become visible at a later point (e.g., the private part of a public
14492 -- child unit) via Declare_Inherited_Private_Subprograms. If the
14493 -- following predicate is true, then this is not such a private
14494 -- operation and the subprogram simply inherits the name of the parent
14495 -- subprogram. Note the special check for the names of controlled
14496 -- operations, which are currently exempted from being inherited with
14497 -- a hidden name because they must be findable for generation of
14498 -- implicit run-time calls.
14500 if not Is_Hidden (Parent_Subp)
14501 or else Is_Internal (Parent_Subp)
14502 or else Is_Private_Overriding
14503 or else Is_Internal_Name (Chars (Parent_Subp))
14504 or else Nam_In (Chars (Parent_Subp), Name_Initialize,
14505 Name_Adjust,
14506 Name_Finalize)
14507 then
14508 Set_Derived_Name;
14510 -- An inherited dispatching equality will be overridden by an internally
14511 -- generated one, or by an explicit one, so preserve its name and thus
14512 -- its entry in the dispatch table. Otherwise, if Parent_Subp is a
14513 -- private operation it may become invisible if the full view has
14514 -- progenitors, and the dispatch table will be malformed.
14515 -- We check that the type is limited to handle the anomalous declaration
14516 -- of Limited_Controlled, which is derived from a non-limited type, and
14517 -- which is handled specially elsewhere as well.
14519 elsif Chars (Parent_Subp) = Name_Op_Eq
14520 and then Is_Dispatching_Operation (Parent_Subp)
14521 and then Etype (Parent_Subp) = Standard_Boolean
14522 and then not Is_Limited_Type (Etype (First_Formal (Parent_Subp)))
14523 and then
14524 Etype (First_Formal (Parent_Subp)) =
14525 Etype (Next_Formal (First_Formal (Parent_Subp)))
14526 then
14527 Set_Derived_Name;
14529 -- If parent is hidden, this can be a regular derivation if the
14530 -- parent is immediately visible in a non-instantiating context,
14531 -- or if we are in the private part of an instance. This test
14532 -- should still be refined ???
14534 -- The test for In_Instance_Not_Visible avoids inheriting the derived
14535 -- operation as a non-visible operation in cases where the parent
14536 -- subprogram might not be visible now, but was visible within the
14537 -- original generic, so it would be wrong to make the inherited
14538 -- subprogram non-visible now. (Not clear if this test is fully
14539 -- correct; are there any cases where we should declare the inherited
14540 -- operation as not visible to avoid it being overridden, e.g., when
14541 -- the parent type is a generic actual with private primitives ???)
14543 -- (they should be treated the same as other private inherited
14544 -- subprograms, but it's not clear how to do this cleanly). ???
14546 elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
14547 and then Is_Immediately_Visible (Parent_Subp)
14548 and then not In_Instance)
14549 or else In_Instance_Not_Visible
14550 then
14551 Set_Derived_Name;
14553 -- Ada 2005 (AI-251): Regular derivation if the parent subprogram
14554 -- overrides an interface primitive because interface primitives
14555 -- must be visible in the partial view of the parent (RM 7.3 (7.3/2))
14557 elsif Ada_Version >= Ada_2005
14558 and then Is_Dispatching_Operation (Parent_Subp)
14559 and then Covers_Some_Interface (Parent_Subp)
14560 then
14561 Set_Derived_Name;
14563 -- Otherwise, the type is inheriting a private operation, so enter
14564 -- it with a special name so it can't be overridden.
14566 else
14567 Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
14568 end if;
14570 Set_Parent (New_Subp, Parent (Derived_Type));
14572 if Present (Actual_Subp) then
14573 Replace_Type (Actual_Subp, New_Subp);
14574 else
14575 Replace_Type (Parent_Subp, New_Subp);
14576 end if;
14578 Conditional_Delay (New_Subp, Parent_Subp);
14580 -- If we are creating a renaming for a primitive operation of an
14581 -- actual of a generic derived type, we must examine the signature
14582 -- of the actual primitive, not that of the generic formal, which for
14583 -- example may be an interface. However the name and initial value
14584 -- of the inherited operation are those of the formal primitive.
14586 Formal := First_Formal (Parent_Subp);
14588 if Present (Actual_Subp) then
14589 Formal_Of_Actual := First_Formal (Actual_Subp);
14590 else
14591 Formal_Of_Actual := Empty;
14592 end if;
14594 while Present (Formal) loop
14595 New_Formal := New_Copy (Formal);
14597 -- Normally we do not go copying parents, but in the case of
14598 -- formals, we need to link up to the declaration (which is the
14599 -- parameter specification), and it is fine to link up to the
14600 -- original formal's parameter specification in this case.
14602 Set_Parent (New_Formal, Parent (Formal));
14603 Append_Entity (New_Formal, New_Subp);
14605 if Present (Formal_Of_Actual) then
14606 Replace_Type (Formal_Of_Actual, New_Formal);
14607 Next_Formal (Formal_Of_Actual);
14608 else
14609 Replace_Type (Formal, New_Formal);
14610 end if;
14612 Next_Formal (Formal);
14613 end loop;
14615 -- If this derivation corresponds to a tagged generic actual, then
14616 -- primitive operations rename those of the actual. Otherwise the
14617 -- primitive operations rename those of the parent type, If the parent
14618 -- renames an intrinsic operator, so does the new subprogram. We except
14619 -- concatenation, which is always properly typed, and does not get
14620 -- expanded as other intrinsic operations.
14622 if No (Actual_Subp) then
14623 if Is_Intrinsic_Subprogram (Parent_Subp) then
14624 Set_Is_Intrinsic_Subprogram (New_Subp);
14626 if Present (Alias (Parent_Subp))
14627 and then Chars (Parent_Subp) /= Name_Op_Concat
14628 then
14629 Set_Alias (New_Subp, Alias (Parent_Subp));
14630 else
14631 Set_Alias (New_Subp, Parent_Subp);
14632 end if;
14634 else
14635 Set_Alias (New_Subp, Parent_Subp);
14636 end if;
14638 else
14639 Set_Alias (New_Subp, Actual_Subp);
14640 end if;
14642 -- Inherit the "ghostness" from the parent subprogram
14644 if Is_Ghost_Entity (Alias (New_Subp)) then
14645 Set_Is_Ghost_Entity (New_Subp);
14646 end if;
14648 -- Derived subprograms of a tagged type must inherit the convention
14649 -- of the parent subprogram (a requirement of AI-117). Derived
14650 -- subprograms of untagged types simply get convention Ada by default.
14652 -- If the derived type is a tagged generic formal type with unknown
14653 -- discriminants, its convention is intrinsic (RM 6.3.1 (8)).
14655 -- However, if the type is derived from a generic formal, the further
14656 -- inherited subprogram has the convention of the non-generic ancestor.
14657 -- Otherwise there would be no way to override the operation.
14658 -- (This is subject to forthcoming ARG discussions).
14660 if Is_Tagged_Type (Derived_Type) then
14661 if Is_Generic_Type (Derived_Type)
14662 and then Has_Unknown_Discriminants (Derived_Type)
14663 then
14664 Set_Convention (New_Subp, Convention_Intrinsic);
14666 else
14667 if Is_Generic_Type (Parent_Type)
14668 and then Has_Unknown_Discriminants (Parent_Type)
14669 then
14670 Set_Convention (New_Subp, Convention (Alias (Parent_Subp)));
14671 else
14672 Set_Convention (New_Subp, Convention (Parent_Subp));
14673 end if;
14674 end if;
14675 end if;
14677 -- Predefined controlled operations retain their name even if the parent
14678 -- is hidden (see above), but they are not primitive operations if the
14679 -- ancestor is not visible, for example if the parent is a private
14680 -- extension completed with a controlled extension. Note that a full
14681 -- type that is controlled can break privacy: the flag Is_Controlled is
14682 -- set on both views of the type.
14684 if Is_Controlled (Parent_Type)
14685 and then Nam_In (Chars (Parent_Subp), Name_Initialize,
14686 Name_Adjust,
14687 Name_Finalize)
14688 and then Is_Hidden (Parent_Subp)
14689 and then not Is_Visibly_Controlled (Parent_Type)
14690 then
14691 Set_Is_Hidden (New_Subp);
14692 end if;
14694 Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
14695 Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
14697 if Ekind (Parent_Subp) = E_Procedure then
14698 Set_Is_Valued_Procedure
14699 (New_Subp, Is_Valued_Procedure (Parent_Subp));
14700 else
14701 Set_Has_Controlling_Result
14702 (New_Subp, Has_Controlling_Result (Parent_Subp));
14703 end if;
14705 -- No_Return must be inherited properly. If this is overridden in the
14706 -- case of a dispatching operation, then a check is made in Sem_Disp
14707 -- that the overriding operation is also No_Return (no such check is
14708 -- required for the case of non-dispatching operation.
14710 Set_No_Return (New_Subp, No_Return (Parent_Subp));
14712 -- A derived function with a controlling result is abstract. If the
14713 -- Derived_Type is a nonabstract formal generic derived type, then
14714 -- inherited operations are not abstract: the required check is done at
14715 -- instantiation time. If the derivation is for a generic actual, the
14716 -- function is not abstract unless the actual is.
14718 if Is_Generic_Type (Derived_Type)
14719 and then not Is_Abstract_Type (Derived_Type)
14720 then
14721 null;
14723 -- Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
14724 -- properties of the subprogram, as defined in RM-3.9.3(4/2-6/2).
14726 -- A subprogram subject to pragma Extensions_Visible with value False
14727 -- requires overriding if the subprogram has at least one controlling
14728 -- OUT parameter (SPARK RM 6.1.7(6)).
14730 elsif Ada_Version >= Ada_2005
14731 and then (Is_Abstract_Subprogram (Alias (New_Subp))
14732 or else (Is_Tagged_Type (Derived_Type)
14733 and then Etype (New_Subp) = Derived_Type
14734 and then not Is_Null_Extension (Derived_Type))
14735 or else (Is_Tagged_Type (Derived_Type)
14736 and then Ekind (Etype (New_Subp)) =
14737 E_Anonymous_Access_Type
14738 and then Designated_Type (Etype (New_Subp)) =
14739 Derived_Type
14740 and then not Is_Null_Extension (Derived_Type))
14741 or else (Comes_From_Source (Alias (New_Subp))
14742 and then Is_EVF_Procedure (Alias (New_Subp))))
14743 and then No (Actual_Subp)
14744 then
14745 if not Is_Tagged_Type (Derived_Type)
14746 or else Is_Abstract_Type (Derived_Type)
14747 or else Is_Abstract_Subprogram (Alias (New_Subp))
14748 then
14749 Set_Is_Abstract_Subprogram (New_Subp);
14750 else
14751 Set_Requires_Overriding (New_Subp);
14752 end if;
14754 elsif Ada_Version < Ada_2005
14755 and then (Is_Abstract_Subprogram (Alias (New_Subp))
14756 or else (Is_Tagged_Type (Derived_Type)
14757 and then Etype (New_Subp) = Derived_Type
14758 and then No (Actual_Subp)))
14759 then
14760 Set_Is_Abstract_Subprogram (New_Subp);
14762 -- AI05-0097 : an inherited operation that dispatches on result is
14763 -- abstract if the derived type is abstract, even if the parent type
14764 -- is concrete and the derived type is a null extension.
14766 elsif Has_Controlling_Result (Alias (New_Subp))
14767 and then Is_Abstract_Type (Etype (New_Subp))
14768 then
14769 Set_Is_Abstract_Subprogram (New_Subp);
14771 -- Finally, if the parent type is abstract we must verify that all
14772 -- inherited operations are either non-abstract or overridden, or that
14773 -- the derived type itself is abstract (this check is performed at the
14774 -- end of a package declaration, in Check_Abstract_Overriding). A
14775 -- private overriding in the parent type will not be visible in the
14776 -- derivation if we are not in an inner package or in a child unit of
14777 -- the parent type, in which case the abstractness of the inherited
14778 -- operation is carried to the new subprogram.
14780 elsif Is_Abstract_Type (Parent_Type)
14781 and then not In_Open_Scopes (Scope (Parent_Type))
14782 and then Is_Private_Overriding
14783 and then Is_Abstract_Subprogram (Visible_Subp)
14784 then
14785 if No (Actual_Subp) then
14786 Set_Alias (New_Subp, Visible_Subp);
14787 Set_Is_Abstract_Subprogram (New_Subp, True);
14789 else
14790 -- If this is a derivation for an instance of a formal derived
14791 -- type, abstractness comes from the primitive operation of the
14792 -- actual, not from the operation inherited from the ancestor.
14794 Set_Is_Abstract_Subprogram
14795 (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
14796 end if;
14797 end if;
14799 New_Overloaded_Entity (New_Subp, Derived_Type);
14801 -- Check for case of a derived subprogram for the instantiation of a
14802 -- formal derived tagged type, if so mark the subprogram as dispatching
14803 -- and inherit the dispatching attributes of the actual subprogram. The
14804 -- derived subprogram is effectively renaming of the actual subprogram,
14805 -- so it needs to have the same attributes as the actual.
14807 if Present (Actual_Subp)
14808 and then Is_Dispatching_Operation (Actual_Subp)
14809 then
14810 Set_Is_Dispatching_Operation (New_Subp);
14812 if Present (DTC_Entity (Actual_Subp)) then
14813 Set_DTC_Entity (New_Subp, DTC_Entity (Actual_Subp));
14814 Set_DT_Position (New_Subp, DT_Position (Actual_Subp));
14815 end if;
14816 end if;
14818 -- Indicate that a derived subprogram does not require a body and that
14819 -- it does not require processing of default expressions.
14821 Set_Has_Completion (New_Subp);
14822 Set_Default_Expressions_Processed (New_Subp);
14824 if Ekind (New_Subp) = E_Function then
14825 Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
14826 end if;
14827 end Derive_Subprogram;
14829 ------------------------
14830 -- Derive_Subprograms --
14831 ------------------------
14833 procedure Derive_Subprograms
14834 (Parent_Type : Entity_Id;
14835 Derived_Type : Entity_Id;
14836 Generic_Actual : Entity_Id := Empty)
14838 Op_List : constant Elist_Id :=
14839 Collect_Primitive_Operations (Parent_Type);
14841 function Check_Derived_Type return Boolean;
14842 -- Check that all the entities derived from Parent_Type are found in
14843 -- the list of primitives of Derived_Type exactly in the same order.
14845 procedure Derive_Interface_Subprogram
14846 (New_Subp : in out Entity_Id;
14847 Subp : Entity_Id;
14848 Actual_Subp : Entity_Id);
14849 -- Derive New_Subp from the ultimate alias of the parent subprogram Subp
14850 -- (which is an interface primitive). If Generic_Actual is present then
14851 -- Actual_Subp is the actual subprogram corresponding with the generic
14852 -- subprogram Subp.
14854 function Check_Derived_Type return Boolean is
14855 E : Entity_Id;
14856 Elmt : Elmt_Id;
14857 List : Elist_Id;
14858 New_Subp : Entity_Id;
14859 Op_Elmt : Elmt_Id;
14860 Subp : Entity_Id;
14862 begin
14863 -- Traverse list of entities in the current scope searching for
14864 -- an incomplete type whose full-view is derived type
14866 E := First_Entity (Scope (Derived_Type));
14867 while Present (E) and then E /= Derived_Type loop
14868 if Ekind (E) = E_Incomplete_Type
14869 and then Present (Full_View (E))
14870 and then Full_View (E) = Derived_Type
14871 then
14872 -- Disable this test if Derived_Type completes an incomplete
14873 -- type because in such case more primitives can be added
14874 -- later to the list of primitives of Derived_Type by routine
14875 -- Process_Incomplete_Dependents
14877 return True;
14878 end if;
14880 E := Next_Entity (E);
14881 end loop;
14883 List := Collect_Primitive_Operations (Derived_Type);
14884 Elmt := First_Elmt (List);
14886 Op_Elmt := First_Elmt (Op_List);
14887 while Present (Op_Elmt) loop
14888 Subp := Node (Op_Elmt);
14889 New_Subp := Node (Elmt);
14891 -- At this early stage Derived_Type has no entities with attribute
14892 -- Interface_Alias. In addition, such primitives are always
14893 -- located at the end of the list of primitives of Parent_Type.
14894 -- Therefore, if found we can safely stop processing pending
14895 -- entities.
14897 exit when Present (Interface_Alias (Subp));
14899 -- Handle hidden entities
14901 if not Is_Predefined_Dispatching_Operation (Subp)
14902 and then Is_Hidden (Subp)
14903 then
14904 if Present (New_Subp)
14905 and then Primitive_Names_Match (Subp, New_Subp)
14906 then
14907 Next_Elmt (Elmt);
14908 end if;
14910 else
14911 if not Present (New_Subp)
14912 or else Ekind (Subp) /= Ekind (New_Subp)
14913 or else not Primitive_Names_Match (Subp, New_Subp)
14914 then
14915 return False;
14916 end if;
14918 Next_Elmt (Elmt);
14919 end if;
14921 Next_Elmt (Op_Elmt);
14922 end loop;
14924 return True;
14925 end Check_Derived_Type;
14927 ---------------------------------
14928 -- Derive_Interface_Subprogram --
14929 ---------------------------------
14931 procedure Derive_Interface_Subprogram
14932 (New_Subp : in out Entity_Id;
14933 Subp : Entity_Id;
14934 Actual_Subp : Entity_Id)
14936 Iface_Subp : constant Entity_Id := Ultimate_Alias (Subp);
14937 Iface_Type : constant Entity_Id := Find_Dispatching_Type (Iface_Subp);
14939 begin
14940 pragma Assert (Is_Interface (Iface_Type));
14942 Derive_Subprogram
14943 (New_Subp => New_Subp,
14944 Parent_Subp => Iface_Subp,
14945 Derived_Type => Derived_Type,
14946 Parent_Type => Iface_Type,
14947 Actual_Subp => Actual_Subp);
14949 -- Given that this new interface entity corresponds with a primitive
14950 -- of the parent that was not overridden we must leave it associated
14951 -- with its parent primitive to ensure that it will share the same
14952 -- dispatch table slot when overridden.
14954 if No (Actual_Subp) then
14955 Set_Alias (New_Subp, Subp);
14957 -- For instantiations this is not needed since the previous call to
14958 -- Derive_Subprogram leaves the entity well decorated.
14960 else
14961 pragma Assert (Alias (New_Subp) = Actual_Subp);
14962 null;
14963 end if;
14964 end Derive_Interface_Subprogram;
14966 -- Local variables
14968 Alias_Subp : Entity_Id;
14969 Act_List : Elist_Id;
14970 Act_Elmt : Elmt_Id;
14971 Act_Subp : Entity_Id := Empty;
14972 Elmt : Elmt_Id;
14973 Need_Search : Boolean := False;
14974 New_Subp : Entity_Id := Empty;
14975 Parent_Base : Entity_Id;
14976 Subp : Entity_Id;
14978 -- Start of processing for Derive_Subprograms
14980 begin
14981 if Ekind (Parent_Type) = E_Record_Type_With_Private
14982 and then Has_Discriminants (Parent_Type)
14983 and then Present (Full_View (Parent_Type))
14984 then
14985 Parent_Base := Full_View (Parent_Type);
14986 else
14987 Parent_Base := Parent_Type;
14988 end if;
14990 if Present (Generic_Actual) then
14991 Act_List := Collect_Primitive_Operations (Generic_Actual);
14992 Act_Elmt := First_Elmt (Act_List);
14993 else
14994 Act_List := No_Elist;
14995 Act_Elmt := No_Elmt;
14996 end if;
14998 -- Derive primitives inherited from the parent. Note that if the generic
14999 -- actual is present, this is not really a type derivation, it is a
15000 -- completion within an instance.
15002 -- Case 1: Derived_Type does not implement interfaces
15004 if not Is_Tagged_Type (Derived_Type)
15005 or else (not Has_Interfaces (Derived_Type)
15006 and then not (Present (Generic_Actual)
15007 and then Has_Interfaces (Generic_Actual)))
15008 then
15009 Elmt := First_Elmt (Op_List);
15010 while Present (Elmt) loop
15011 Subp := Node (Elmt);
15013 -- Literals are derived earlier in the process of building the
15014 -- derived type, and are skipped here.
15016 if Ekind (Subp) = E_Enumeration_Literal then
15017 null;
15019 -- The actual is a direct descendant and the common primitive
15020 -- operations appear in the same order.
15022 -- If the generic parent type is present, the derived type is an
15023 -- instance of a formal derived type, and within the instance its
15024 -- operations are those of the actual. We derive from the formal
15025 -- type but make the inherited operations aliases of the
15026 -- corresponding operations of the actual.
15028 else
15029 pragma Assert (No (Node (Act_Elmt))
15030 or else (Primitive_Names_Match (Subp, Node (Act_Elmt))
15031 and then
15032 Type_Conformant
15033 (Subp, Node (Act_Elmt),
15034 Skip_Controlling_Formals => True)));
15036 Derive_Subprogram
15037 (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
15039 if Present (Act_Elmt) then
15040 Next_Elmt (Act_Elmt);
15041 end if;
15042 end if;
15044 Next_Elmt (Elmt);
15045 end loop;
15047 -- Case 2: Derived_Type implements interfaces
15049 else
15050 -- If the parent type has no predefined primitives we remove
15051 -- predefined primitives from the list of primitives of generic
15052 -- actual to simplify the complexity of this algorithm.
15054 if Present (Generic_Actual) then
15055 declare
15056 Has_Predefined_Primitives : Boolean := False;
15058 begin
15059 -- Check if the parent type has predefined primitives
15061 Elmt := First_Elmt (Op_List);
15062 while Present (Elmt) loop
15063 Subp := Node (Elmt);
15065 if Is_Predefined_Dispatching_Operation (Subp)
15066 and then not Comes_From_Source (Ultimate_Alias (Subp))
15067 then
15068 Has_Predefined_Primitives := True;
15069 exit;
15070 end if;
15072 Next_Elmt (Elmt);
15073 end loop;
15075 -- Remove predefined primitives of Generic_Actual. We must use
15076 -- an auxiliary list because in case of tagged types the value
15077 -- returned by Collect_Primitive_Operations is the value stored
15078 -- in its Primitive_Operations attribute (and we don't want to
15079 -- modify its current contents).
15081 if not Has_Predefined_Primitives then
15082 declare
15083 Aux_List : constant Elist_Id := New_Elmt_List;
15085 begin
15086 Elmt := First_Elmt (Act_List);
15087 while Present (Elmt) loop
15088 Subp := Node (Elmt);
15090 if not Is_Predefined_Dispatching_Operation (Subp)
15091 or else Comes_From_Source (Subp)
15092 then
15093 Append_Elmt (Subp, Aux_List);
15094 end if;
15096 Next_Elmt (Elmt);
15097 end loop;
15099 Act_List := Aux_List;
15100 end;
15101 end if;
15103 Act_Elmt := First_Elmt (Act_List);
15104 Act_Subp := Node (Act_Elmt);
15105 end;
15106 end if;
15108 -- Stage 1: If the generic actual is not present we derive the
15109 -- primitives inherited from the parent type. If the generic parent
15110 -- type is present, the derived type is an instance of a formal
15111 -- derived type, and within the instance its operations are those of
15112 -- the actual. We derive from the formal type but make the inherited
15113 -- operations aliases of the corresponding operations of the actual.
15115 Elmt := First_Elmt (Op_List);
15116 while Present (Elmt) loop
15117 Subp := Node (Elmt);
15118 Alias_Subp := Ultimate_Alias (Subp);
15120 -- Do not derive internal entities of the parent that link
15121 -- interface primitives with their covering primitive. These
15122 -- entities will be added to this type when frozen.
15124 if Present (Interface_Alias (Subp)) then
15125 goto Continue;
15126 end if;
15128 -- If the generic actual is present find the corresponding
15129 -- operation in the generic actual. If the parent type is a
15130 -- direct ancestor of the derived type then, even if it is an
15131 -- interface, the operations are inherited from the primary
15132 -- dispatch table and are in the proper order. If we detect here
15133 -- that primitives are not in the same order we traverse the list
15134 -- of primitive operations of the actual to find the one that
15135 -- implements the interface primitive.
15137 if Need_Search
15138 or else
15139 (Present (Generic_Actual)
15140 and then Present (Act_Subp)
15141 and then not
15142 (Primitive_Names_Match (Subp, Act_Subp)
15143 and then
15144 Type_Conformant (Subp, Act_Subp,
15145 Skip_Controlling_Formals => True)))
15146 then
15147 pragma Assert (not Is_Ancestor (Parent_Base, Generic_Actual,
15148 Use_Full_View => True));
15150 -- Remember that we need searching for all pending primitives
15152 Need_Search := True;
15154 -- Handle entities associated with interface primitives
15156 if Present (Alias_Subp)
15157 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
15158 and then not Is_Predefined_Dispatching_Operation (Subp)
15159 then
15160 -- Search for the primitive in the homonym chain
15162 Act_Subp :=
15163 Find_Primitive_Covering_Interface
15164 (Tagged_Type => Generic_Actual,
15165 Iface_Prim => Alias_Subp);
15167 -- Previous search may not locate primitives covering
15168 -- interfaces defined in generics units or instantiations.
15169 -- (it fails if the covering primitive has formals whose
15170 -- type is also defined in generics or instantiations).
15171 -- In such case we search in the list of primitives of the
15172 -- generic actual for the internal entity that links the
15173 -- interface primitive and the covering primitive.
15175 if No (Act_Subp)
15176 and then Is_Generic_Type (Parent_Type)
15177 then
15178 -- This code has been designed to handle only generic
15179 -- formals that implement interfaces that are defined
15180 -- in a generic unit or instantiation. If this code is
15181 -- needed for other cases we must review it because
15182 -- (given that it relies on Original_Location to locate
15183 -- the primitive of Generic_Actual that covers the
15184 -- interface) it could leave linked through attribute
15185 -- Alias entities of unrelated instantiations).
15187 pragma Assert
15188 (Is_Generic_Unit
15189 (Scope (Find_Dispatching_Type (Alias_Subp)))
15190 or else
15191 Instantiation_Depth
15192 (Sloc (Find_Dispatching_Type (Alias_Subp))) > 0);
15194 declare
15195 Iface_Prim_Loc : constant Source_Ptr :=
15196 Original_Location (Sloc (Alias_Subp));
15198 Elmt : Elmt_Id;
15199 Prim : Entity_Id;
15201 begin
15202 Elmt :=
15203 First_Elmt (Primitive_Operations (Generic_Actual));
15205 Search : while Present (Elmt) loop
15206 Prim := Node (Elmt);
15208 if Present (Interface_Alias (Prim))
15209 and then Original_Location
15210 (Sloc (Interface_Alias (Prim))) =
15211 Iface_Prim_Loc
15212 then
15213 Act_Subp := Alias (Prim);
15214 exit Search;
15215 end if;
15217 Next_Elmt (Elmt);
15218 end loop Search;
15219 end;
15220 end if;
15222 pragma Assert (Present (Act_Subp)
15223 or else Is_Abstract_Type (Generic_Actual)
15224 or else Serious_Errors_Detected > 0);
15226 -- Handle predefined primitives plus the rest of user-defined
15227 -- primitives
15229 else
15230 Act_Elmt := First_Elmt (Act_List);
15231 while Present (Act_Elmt) loop
15232 Act_Subp := Node (Act_Elmt);
15234 exit when Primitive_Names_Match (Subp, Act_Subp)
15235 and then Type_Conformant
15236 (Subp, Act_Subp,
15237 Skip_Controlling_Formals => True)
15238 and then No (Interface_Alias (Act_Subp));
15240 Next_Elmt (Act_Elmt);
15241 end loop;
15243 if No (Act_Elmt) then
15244 Act_Subp := Empty;
15245 end if;
15246 end if;
15247 end if;
15249 -- Case 1: If the parent is a limited interface then it has the
15250 -- predefined primitives of synchronized interfaces. However, the
15251 -- actual type may be a non-limited type and hence it does not
15252 -- have such primitives.
15254 if Present (Generic_Actual)
15255 and then not Present (Act_Subp)
15256 and then Is_Limited_Interface (Parent_Base)
15257 and then Is_Predefined_Interface_Primitive (Subp)
15258 then
15259 null;
15261 -- Case 2: Inherit entities associated with interfaces that were
15262 -- not covered by the parent type. We exclude here null interface
15263 -- primitives because they do not need special management.
15265 -- We also exclude interface operations that are renamings. If the
15266 -- subprogram is an explicit renaming of an interface primitive,
15267 -- it is a regular primitive operation, and the presence of its
15268 -- alias is not relevant: it has to be derived like any other
15269 -- primitive.
15271 elsif Present (Alias (Subp))
15272 and then Nkind (Unit_Declaration_Node (Subp)) /=
15273 N_Subprogram_Renaming_Declaration
15274 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
15275 and then not
15276 (Nkind (Parent (Alias_Subp)) = N_Procedure_Specification
15277 and then Null_Present (Parent (Alias_Subp)))
15278 then
15279 -- If this is an abstract private type then we transfer the
15280 -- derivation of the interface primitive from the partial view
15281 -- to the full view. This is safe because all the interfaces
15282 -- must be visible in the partial view. Done to avoid adding
15283 -- a new interface derivation to the private part of the
15284 -- enclosing package; otherwise this new derivation would be
15285 -- decorated as hidden when the analysis of the enclosing
15286 -- package completes.
15288 if Is_Abstract_Type (Derived_Type)
15289 and then In_Private_Part (Current_Scope)
15290 and then Has_Private_Declaration (Derived_Type)
15291 then
15292 declare
15293 Partial_View : Entity_Id;
15294 Elmt : Elmt_Id;
15295 Ent : Entity_Id;
15297 begin
15298 Partial_View := First_Entity (Current_Scope);
15299 loop
15300 exit when No (Partial_View)
15301 or else (Has_Private_Declaration (Partial_View)
15302 and then
15303 Full_View (Partial_View) = Derived_Type);
15305 Next_Entity (Partial_View);
15306 end loop;
15308 -- If the partial view was not found then the source code
15309 -- has errors and the derivation is not needed.
15311 if Present (Partial_View) then
15312 Elmt :=
15313 First_Elmt (Primitive_Operations (Partial_View));
15314 while Present (Elmt) loop
15315 Ent := Node (Elmt);
15317 if Present (Alias (Ent))
15318 and then Ultimate_Alias (Ent) = Alias (Subp)
15319 then
15320 Append_Elmt
15321 (Ent, Primitive_Operations (Derived_Type));
15322 exit;
15323 end if;
15325 Next_Elmt (Elmt);
15326 end loop;
15328 -- If the interface primitive was not found in the
15329 -- partial view then this interface primitive was
15330 -- overridden. We add a derivation to activate in
15331 -- Derive_Progenitor_Subprograms the machinery to
15332 -- search for it.
15334 if No (Elmt) then
15335 Derive_Interface_Subprogram
15336 (New_Subp => New_Subp,
15337 Subp => Subp,
15338 Actual_Subp => Act_Subp);
15339 end if;
15340 end if;
15341 end;
15342 else
15343 Derive_Interface_Subprogram
15344 (New_Subp => New_Subp,
15345 Subp => Subp,
15346 Actual_Subp => Act_Subp);
15347 end if;
15349 -- Case 3: Common derivation
15351 else
15352 Derive_Subprogram
15353 (New_Subp => New_Subp,
15354 Parent_Subp => Subp,
15355 Derived_Type => Derived_Type,
15356 Parent_Type => Parent_Base,
15357 Actual_Subp => Act_Subp);
15358 end if;
15360 -- No need to update Act_Elm if we must search for the
15361 -- corresponding operation in the generic actual
15363 if not Need_Search
15364 and then Present (Act_Elmt)
15365 then
15366 Next_Elmt (Act_Elmt);
15367 Act_Subp := Node (Act_Elmt);
15368 end if;
15370 <<Continue>>
15371 Next_Elmt (Elmt);
15372 end loop;
15374 -- Inherit additional operations from progenitors. If the derived
15375 -- type is a generic actual, there are not new primitive operations
15376 -- for the type because it has those of the actual, and therefore
15377 -- nothing needs to be done. The renamings generated above are not
15378 -- primitive operations, and their purpose is simply to make the
15379 -- proper operations visible within an instantiation.
15381 if No (Generic_Actual) then
15382 Derive_Progenitor_Subprograms (Parent_Base, Derived_Type);
15383 end if;
15384 end if;
15386 -- Final check: Direct descendants must have their primitives in the
15387 -- same order. We exclude from this test untagged types and instances
15388 -- of formal derived types. We skip this test if we have already
15389 -- reported serious errors in the sources.
15391 pragma Assert (not Is_Tagged_Type (Derived_Type)
15392 or else Present (Generic_Actual)
15393 or else Serious_Errors_Detected > 0
15394 or else Check_Derived_Type);
15395 end Derive_Subprograms;
15397 --------------------------------
15398 -- Derived_Standard_Character --
15399 --------------------------------
15401 procedure Derived_Standard_Character
15402 (N : Node_Id;
15403 Parent_Type : Entity_Id;
15404 Derived_Type : Entity_Id)
15406 Loc : constant Source_Ptr := Sloc (N);
15407 Def : constant Node_Id := Type_Definition (N);
15408 Indic : constant Node_Id := Subtype_Indication (Def);
15409 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
15410 Implicit_Base : constant Entity_Id :=
15411 Create_Itype
15412 (E_Enumeration_Type, N, Derived_Type, 'B');
15414 Lo : Node_Id;
15415 Hi : Node_Id;
15417 begin
15418 Discard_Node (Process_Subtype (Indic, N));
15420 Set_Etype (Implicit_Base, Parent_Base);
15421 Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
15422 Set_RM_Size (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
15424 Set_Is_Character_Type (Implicit_Base, True);
15425 Set_Has_Delayed_Freeze (Implicit_Base);
15427 -- The bounds of the implicit base are the bounds of the parent base.
15428 -- Note that their type is the parent base.
15430 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
15431 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
15433 Set_Scalar_Range (Implicit_Base,
15434 Make_Range (Loc,
15435 Low_Bound => Lo,
15436 High_Bound => Hi));
15438 Conditional_Delay (Derived_Type, Parent_Type);
15440 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
15441 Set_Etype (Derived_Type, Implicit_Base);
15442 Set_Size_Info (Derived_Type, Parent_Type);
15444 if Unknown_RM_Size (Derived_Type) then
15445 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
15446 end if;
15448 Set_Is_Character_Type (Derived_Type, True);
15450 if Nkind (Indic) /= N_Subtype_Indication then
15452 -- If no explicit constraint, the bounds are those
15453 -- of the parent type.
15455 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Type));
15456 Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
15457 Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
15458 end if;
15460 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
15462 -- Because the implicit base is used in the conversion of the bounds, we
15463 -- have to freeze it now. This is similar to what is done for numeric
15464 -- types, and it equally suspicious, but otherwise a non-static bound
15465 -- will have a reference to an unfrozen type, which is rejected by Gigi
15466 -- (???). This requires specific care for definition of stream
15467 -- attributes. For details, see comments at the end of
15468 -- Build_Derived_Numeric_Type.
15470 Freeze_Before (N, Implicit_Base);
15471 end Derived_Standard_Character;
15473 ------------------------------
15474 -- Derived_Type_Declaration --
15475 ------------------------------
15477 procedure Derived_Type_Declaration
15478 (T : Entity_Id;
15479 N : Node_Id;
15480 Is_Completion : Boolean)
15482 Parent_Type : Entity_Id;
15484 function Comes_From_Generic (Typ : Entity_Id) return Boolean;
15485 -- Check whether the parent type is a generic formal, or derives
15486 -- directly or indirectly from one.
15488 ------------------------
15489 -- Comes_From_Generic --
15490 ------------------------
15492 function Comes_From_Generic (Typ : Entity_Id) return Boolean is
15493 begin
15494 if Is_Generic_Type (Typ) then
15495 return True;
15497 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
15498 return True;
15500 elsif Is_Private_Type (Typ)
15501 and then Present (Full_View (Typ))
15502 and then Is_Generic_Type (Root_Type (Full_View (Typ)))
15503 then
15504 return True;
15506 elsif Is_Generic_Actual_Type (Typ) then
15507 return True;
15509 else
15510 return False;
15511 end if;
15512 end Comes_From_Generic;
15514 -- Local variables
15516 Def : constant Node_Id := Type_Definition (N);
15517 Iface_Def : Node_Id;
15518 Indic : constant Node_Id := Subtype_Indication (Def);
15519 Extension : constant Node_Id := Record_Extension_Part (Def);
15520 Parent_Node : Node_Id;
15521 Taggd : Boolean;
15523 -- Start of processing for Derived_Type_Declaration
15525 begin
15526 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
15528 -- Ada 2005 (AI-251): In case of interface derivation check that the
15529 -- parent is also an interface.
15531 if Interface_Present (Def) then
15532 Check_SPARK_05_Restriction ("interface is not allowed", Def);
15534 if not Is_Interface (Parent_Type) then
15535 Diagnose_Interface (Indic, Parent_Type);
15537 else
15538 Parent_Node := Parent (Base_Type (Parent_Type));
15539 Iface_Def := Type_Definition (Parent_Node);
15541 -- Ada 2005 (AI-251): Limited interfaces can only inherit from
15542 -- other limited interfaces.
15544 if Limited_Present (Def) then
15545 if Limited_Present (Iface_Def) then
15546 null;
15548 elsif Protected_Present (Iface_Def) then
15549 Error_Msg_NE
15550 ("descendant of& must be declared"
15551 & " as a protected interface",
15552 N, Parent_Type);
15554 elsif Synchronized_Present (Iface_Def) then
15555 Error_Msg_NE
15556 ("descendant of& must be declared"
15557 & " as a synchronized interface",
15558 N, Parent_Type);
15560 elsif Task_Present (Iface_Def) then
15561 Error_Msg_NE
15562 ("descendant of& must be declared as a task interface",
15563 N, Parent_Type);
15565 else
15566 Error_Msg_N
15567 ("(Ada 2005) limited interface cannot "
15568 & "inherit from non-limited interface", Indic);
15569 end if;
15571 -- Ada 2005 (AI-345): Non-limited interfaces can only inherit
15572 -- from non-limited or limited interfaces.
15574 elsif not Protected_Present (Def)
15575 and then not Synchronized_Present (Def)
15576 and then not Task_Present (Def)
15577 then
15578 if Limited_Present (Iface_Def) then
15579 null;
15581 elsif Protected_Present (Iface_Def) then
15582 Error_Msg_NE
15583 ("descendant of& must be declared"
15584 & " as a protected interface",
15585 N, Parent_Type);
15587 elsif Synchronized_Present (Iface_Def) then
15588 Error_Msg_NE
15589 ("descendant of& must be declared"
15590 & " as a synchronized interface",
15591 N, Parent_Type);
15593 elsif Task_Present (Iface_Def) then
15594 Error_Msg_NE
15595 ("descendant of& must be declared as a task interface",
15596 N, Parent_Type);
15597 else
15598 null;
15599 end if;
15600 end if;
15601 end if;
15602 end if;
15604 if Is_Tagged_Type (Parent_Type)
15605 and then Is_Concurrent_Type (Parent_Type)
15606 and then not Is_Interface (Parent_Type)
15607 then
15608 Error_Msg_N
15609 ("parent type of a record extension cannot be "
15610 & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
15611 Set_Etype (T, Any_Type);
15612 return;
15613 end if;
15615 -- Ada 2005 (AI-251): Decorate all the names in the list of ancestor
15616 -- interfaces
15618 if Is_Tagged_Type (Parent_Type)
15619 and then Is_Non_Empty_List (Interface_List (Def))
15620 then
15621 declare
15622 Intf : Node_Id;
15623 T : Entity_Id;
15625 begin
15626 Intf := First (Interface_List (Def));
15627 while Present (Intf) loop
15628 T := Find_Type_Of_Subtype_Indic (Intf);
15630 if not Is_Interface (T) then
15631 Diagnose_Interface (Intf, T);
15633 -- Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
15634 -- a limited type from having a nonlimited progenitor.
15636 elsif (Limited_Present (Def)
15637 or else (not Is_Interface (Parent_Type)
15638 and then Is_Limited_Type (Parent_Type)))
15639 and then not Is_Limited_Interface (T)
15640 then
15641 Error_Msg_NE
15642 ("progenitor interface& of limited type must be limited",
15643 N, T);
15644 end if;
15646 Next (Intf);
15647 end loop;
15648 end;
15649 end if;
15651 if Parent_Type = Any_Type
15652 or else Etype (Parent_Type) = Any_Type
15653 or else (Is_Class_Wide_Type (Parent_Type)
15654 and then Etype (Parent_Type) = T)
15655 then
15656 -- If Parent_Type is undefined or illegal, make new type into a
15657 -- subtype of Any_Type, and set a few attributes to prevent cascaded
15658 -- errors. If this is a self-definition, emit error now.
15660 if T = Parent_Type or else T = Etype (Parent_Type) then
15661 Error_Msg_N ("type cannot be used in its own definition", Indic);
15662 end if;
15664 Set_Ekind (T, Ekind (Parent_Type));
15665 Set_Etype (T, Any_Type);
15666 Set_Scalar_Range (T, Scalar_Range (Any_Type));
15668 if Is_Tagged_Type (T)
15669 and then Is_Record_Type (T)
15670 then
15671 Set_Direct_Primitive_Operations (T, New_Elmt_List);
15672 end if;
15674 return;
15675 end if;
15677 -- Ada 2005 (AI-251): The case in which the parent of the full-view is
15678 -- an interface is special because the list of interfaces in the full
15679 -- view can be given in any order. For example:
15681 -- type A is interface;
15682 -- type B is interface and A;
15683 -- type D is new B with private;
15684 -- private
15685 -- type D is new A and B with null record; -- 1 --
15687 -- In this case we perform the following transformation of -1-:
15689 -- type D is new B and A with null record;
15691 -- If the parent of the full-view covers the parent of the partial-view
15692 -- we have two possible cases:
15694 -- 1) They have the same parent
15695 -- 2) The parent of the full-view implements some further interfaces
15697 -- In both cases we do not need to perform the transformation. In the
15698 -- first case the source program is correct and the transformation is
15699 -- not needed; in the second case the source program does not fulfill
15700 -- the no-hidden interfaces rule (AI-396) and the error will be reported
15701 -- later.
15703 -- This transformation not only simplifies the rest of the analysis of
15704 -- this type declaration but also simplifies the correct generation of
15705 -- the object layout to the expander.
15707 if In_Private_Part (Current_Scope)
15708 and then Is_Interface (Parent_Type)
15709 then
15710 declare
15711 Iface : Node_Id;
15712 Partial_View : Entity_Id;
15713 Partial_View_Parent : Entity_Id;
15714 New_Iface : Node_Id;
15716 begin
15717 -- Look for the associated private type declaration
15719 Partial_View := First_Entity (Current_Scope);
15720 loop
15721 exit when No (Partial_View)
15722 or else (Has_Private_Declaration (Partial_View)
15723 and then Full_View (Partial_View) = T);
15725 Next_Entity (Partial_View);
15726 end loop;
15728 -- If the partial view was not found then the source code has
15729 -- errors and the transformation is not needed.
15731 if Present (Partial_View) then
15732 Partial_View_Parent := Etype (Partial_View);
15734 -- If the parent of the full-view covers the parent of the
15735 -- partial-view we have nothing else to do.
15737 if Interface_Present_In_Ancestor
15738 (Parent_Type, Partial_View_Parent)
15739 then
15740 null;
15742 -- Traverse the list of interfaces of the full-view to look
15743 -- for the parent of the partial-view and perform the tree
15744 -- transformation.
15746 else
15747 Iface := First (Interface_List (Def));
15748 while Present (Iface) loop
15749 if Etype (Iface) = Etype (Partial_View) then
15750 Rewrite (Subtype_Indication (Def),
15751 New_Copy (Subtype_Indication
15752 (Parent (Partial_View))));
15754 New_Iface :=
15755 Make_Identifier (Sloc (N), Chars (Parent_Type));
15756 Append (New_Iface, Interface_List (Def));
15758 -- Analyze the transformed code
15760 Derived_Type_Declaration (T, N, Is_Completion);
15761 return;
15762 end if;
15764 Next (Iface);
15765 end loop;
15766 end if;
15767 end if;
15768 end;
15769 end if;
15771 -- Only composite types other than array types are allowed to have
15772 -- discriminants.
15774 if Present (Discriminant_Specifications (N)) then
15775 if (Is_Elementary_Type (Parent_Type)
15776 or else
15777 Is_Array_Type (Parent_Type))
15778 and then not Error_Posted (N)
15779 then
15780 Error_Msg_N
15781 ("elementary or array type cannot have discriminants",
15782 Defining_Identifier (First (Discriminant_Specifications (N))));
15783 Set_Has_Discriminants (T, False);
15785 -- The type is allowed to have discriminants
15787 else
15788 Check_SPARK_05_Restriction ("discriminant type is not allowed", N);
15789 end if;
15790 end if;
15792 -- In Ada 83, a derived type defined in a package specification cannot
15793 -- be used for further derivation until the end of its visible part.
15794 -- Note that derivation in the private part of the package is allowed.
15796 if Ada_Version = Ada_83
15797 and then Is_Derived_Type (Parent_Type)
15798 and then In_Visible_Part (Scope (Parent_Type))
15799 then
15800 if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
15801 Error_Msg_N
15802 ("(Ada 83): premature use of type for derivation", Indic);
15803 end if;
15804 end if;
15806 -- Check for early use of incomplete or private type
15808 if Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
15809 Error_Msg_N ("premature derivation of incomplete type", Indic);
15810 return;
15812 elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
15813 and then not Comes_From_Generic (Parent_Type))
15814 or else Has_Private_Component (Parent_Type)
15815 then
15816 -- The ancestor type of a formal type can be incomplete, in which
15817 -- case only the operations of the partial view are available in the
15818 -- generic. Subsequent checks may be required when the full view is
15819 -- analyzed to verify that a derivation from a tagged type has an
15820 -- extension.
15822 if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
15823 null;
15825 elsif No (Underlying_Type (Parent_Type))
15826 or else Has_Private_Component (Parent_Type)
15827 then
15828 Error_Msg_N
15829 ("premature derivation of derived or private type", Indic);
15831 -- Flag the type itself as being in error, this prevents some
15832 -- nasty problems with subsequent uses of the malformed type.
15834 Set_Error_Posted (T);
15836 -- Check that within the immediate scope of an untagged partial
15837 -- view it's illegal to derive from the partial view if the
15838 -- full view is tagged. (7.3(7))
15840 -- We verify that the Parent_Type is a partial view by checking
15841 -- that it is not a Full_Type_Declaration (i.e. a private type or
15842 -- private extension declaration), to distinguish a partial view
15843 -- from a derivation from a private type which also appears as
15844 -- E_Private_Type. If the parent base type is not declared in an
15845 -- enclosing scope there is no need to check.
15847 elsif Present (Full_View (Parent_Type))
15848 and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
15849 and then not Is_Tagged_Type (Parent_Type)
15850 and then Is_Tagged_Type (Full_View (Parent_Type))
15851 and then In_Open_Scopes (Scope (Base_Type (Parent_Type)))
15852 then
15853 Error_Msg_N
15854 ("premature derivation from type with tagged full view",
15855 Indic);
15856 end if;
15857 end if;
15859 -- Check that form of derivation is appropriate
15861 Taggd := Is_Tagged_Type (Parent_Type);
15863 -- Set the parent type to the class-wide type's specific type in this
15864 -- case to prevent cascading errors
15866 if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
15867 Error_Msg_N ("parent type must not be a class-wide type", Indic);
15868 Set_Etype (T, Etype (Parent_Type));
15869 return;
15870 end if;
15872 if Present (Extension) and then not Taggd then
15873 Error_Msg_N
15874 ("type derived from untagged type cannot have extension", Indic);
15876 elsif No (Extension) and then Taggd then
15878 -- If this declaration is within a private part (or body) of a
15879 -- generic instantiation then the derivation is allowed (the parent
15880 -- type can only appear tagged in this case if it's a generic actual
15881 -- type, since it would otherwise have been rejected in the analysis
15882 -- of the generic template).
15884 if not Is_Generic_Actual_Type (Parent_Type)
15885 or else In_Visible_Part (Scope (Parent_Type))
15886 then
15887 if Is_Class_Wide_Type (Parent_Type) then
15888 Error_Msg_N
15889 ("parent type must not be a class-wide type", Indic);
15891 -- Use specific type to prevent cascaded errors.
15893 Parent_Type := Etype (Parent_Type);
15895 else
15896 Error_Msg_N
15897 ("type derived from tagged type must have extension", Indic);
15898 end if;
15899 end if;
15900 end if;
15902 -- AI-443: Synchronized formal derived types require a private
15903 -- extension. There is no point in checking the ancestor type or
15904 -- the progenitors since the construct is wrong to begin with.
15906 if Ada_Version >= Ada_2005
15907 and then Is_Generic_Type (T)
15908 and then Present (Original_Node (N))
15909 then
15910 declare
15911 Decl : constant Node_Id := Original_Node (N);
15913 begin
15914 if Nkind (Decl) = N_Formal_Type_Declaration
15915 and then Nkind (Formal_Type_Definition (Decl)) =
15916 N_Formal_Derived_Type_Definition
15917 and then Synchronized_Present (Formal_Type_Definition (Decl))
15918 and then No (Extension)
15920 -- Avoid emitting a duplicate error message
15922 and then not Error_Posted (Indic)
15923 then
15924 Error_Msg_N
15925 ("synchronized derived type must have extension", N);
15926 end if;
15927 end;
15928 end if;
15930 if Null_Exclusion_Present (Def)
15931 and then not Is_Access_Type (Parent_Type)
15932 then
15933 Error_Msg_N ("null exclusion can only apply to an access type", N);
15934 end if;
15936 -- Avoid deriving parent primitives of underlying record views
15938 Build_Derived_Type (N, Parent_Type, T, Is_Completion,
15939 Derive_Subps => not Is_Underlying_Record_View (T));
15941 -- AI-419: The parent type of an explicitly limited derived type must
15942 -- be a limited type or a limited interface.
15944 if Limited_Present (Def) then
15945 Set_Is_Limited_Record (T);
15947 if Is_Interface (T) then
15948 Set_Is_Limited_Interface (T);
15949 end if;
15951 if not Is_Limited_Type (Parent_Type)
15952 and then
15953 (not Is_Interface (Parent_Type)
15954 or else not Is_Limited_Interface (Parent_Type))
15955 then
15956 -- AI05-0096: a derivation in the private part of an instance is
15957 -- legal if the generic formal is untagged limited, and the actual
15958 -- is non-limited.
15960 if Is_Generic_Actual_Type (Parent_Type)
15961 and then In_Private_Part (Current_Scope)
15962 and then
15963 not Is_Tagged_Type
15964 (Generic_Parent_Type (Parent (Parent_Type)))
15965 then
15966 null;
15968 else
15969 Error_Msg_NE
15970 ("parent type& of limited type must be limited",
15971 N, Parent_Type);
15972 end if;
15973 end if;
15974 end if;
15976 -- In SPARK, there are no derived type definitions other than type
15977 -- extensions of tagged record types.
15979 if No (Extension) then
15980 Check_SPARK_05_Restriction
15981 ("derived type is not allowed", Original_Node (N));
15982 end if;
15983 end Derived_Type_Declaration;
15985 ------------------------
15986 -- Diagnose_Interface --
15987 ------------------------
15989 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id) is
15990 begin
15991 if not Is_Interface (E) and then E /= Any_Type then
15992 Error_Msg_NE ("(Ada 2005) & must be an interface", N, E);
15993 end if;
15994 end Diagnose_Interface;
15996 ----------------------------------
15997 -- Enumeration_Type_Declaration --
15998 ----------------------------------
16000 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
16001 Ev : Uint;
16002 L : Node_Id;
16003 R_Node : Node_Id;
16004 B_Node : Node_Id;
16006 begin
16007 -- Create identifier node representing lower bound
16009 B_Node := New_Node (N_Identifier, Sloc (Def));
16010 L := First (Literals (Def));
16011 Set_Chars (B_Node, Chars (L));
16012 Set_Entity (B_Node, L);
16013 Set_Etype (B_Node, T);
16014 Set_Is_Static_Expression (B_Node, True);
16016 R_Node := New_Node (N_Range, Sloc (Def));
16017 Set_Low_Bound (R_Node, B_Node);
16019 Set_Ekind (T, E_Enumeration_Type);
16020 Set_First_Literal (T, L);
16021 Set_Etype (T, T);
16022 Set_Is_Constrained (T);
16024 Ev := Uint_0;
16026 -- Loop through literals of enumeration type setting pos and rep values
16027 -- except that if the Ekind is already set, then it means the literal
16028 -- was already constructed (case of a derived type declaration and we
16029 -- should not disturb the Pos and Rep values.
16031 while Present (L) loop
16032 if Ekind (L) /= E_Enumeration_Literal then
16033 Set_Ekind (L, E_Enumeration_Literal);
16034 Set_Enumeration_Pos (L, Ev);
16035 Set_Enumeration_Rep (L, Ev);
16036 Set_Is_Known_Valid (L, True);
16037 end if;
16039 Set_Etype (L, T);
16040 New_Overloaded_Entity (L);
16041 Generate_Definition (L);
16042 Set_Convention (L, Convention_Intrinsic);
16044 -- Case of character literal
16046 if Nkind (L) = N_Defining_Character_Literal then
16047 Set_Is_Character_Type (T, True);
16049 -- Check violation of No_Wide_Characters
16051 if Restriction_Check_Required (No_Wide_Characters) then
16052 Get_Name_String (Chars (L));
16054 if Name_Len >= 3 and then Name_Buffer (1 .. 2) = "QW" then
16055 Check_Restriction (No_Wide_Characters, L);
16056 end if;
16057 end if;
16058 end if;
16060 Ev := Ev + 1;
16061 Next (L);
16062 end loop;
16064 -- Now create a node representing upper bound
16066 B_Node := New_Node (N_Identifier, Sloc (Def));
16067 Set_Chars (B_Node, Chars (Last (Literals (Def))));
16068 Set_Entity (B_Node, Last (Literals (Def)));
16069 Set_Etype (B_Node, T);
16070 Set_Is_Static_Expression (B_Node, True);
16072 Set_High_Bound (R_Node, B_Node);
16074 -- Initialize various fields of the type. Some of this information
16075 -- may be overwritten later through rep.clauses.
16077 Set_Scalar_Range (T, R_Node);
16078 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
16079 Set_Enum_Esize (T);
16080 Set_Enum_Pos_To_Rep (T, Empty);
16082 -- Set Discard_Names if configuration pragma set, or if there is
16083 -- a parameterless pragma in the current declarative region
16085 if Global_Discard_Names or else Discard_Names (Scope (T)) then
16086 Set_Discard_Names (T);
16087 end if;
16089 -- Process end label if there is one
16091 if Present (Def) then
16092 Process_End_Label (Def, 'e', T);
16093 end if;
16094 end Enumeration_Type_Declaration;
16096 ---------------------------------
16097 -- Expand_To_Stored_Constraint --
16098 ---------------------------------
16100 function Expand_To_Stored_Constraint
16101 (Typ : Entity_Id;
16102 Constraint : Elist_Id) return Elist_Id
16104 Explicitly_Discriminated_Type : Entity_Id;
16105 Expansion : Elist_Id;
16106 Discriminant : Entity_Id;
16108 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
16109 -- Find the nearest type that actually specifies discriminants
16111 ---------------------------------
16112 -- Type_With_Explicit_Discrims --
16113 ---------------------------------
16115 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
16116 Typ : constant E := Base_Type (Id);
16118 begin
16119 if Ekind (Typ) in Incomplete_Or_Private_Kind then
16120 if Present (Full_View (Typ)) then
16121 return Type_With_Explicit_Discrims (Full_View (Typ));
16122 end if;
16124 else
16125 if Has_Discriminants (Typ) then
16126 return Typ;
16127 end if;
16128 end if;
16130 if Etype (Typ) = Typ then
16131 return Empty;
16132 elsif Has_Discriminants (Typ) then
16133 return Typ;
16134 else
16135 return Type_With_Explicit_Discrims (Etype (Typ));
16136 end if;
16138 end Type_With_Explicit_Discrims;
16140 -- Start of processing for Expand_To_Stored_Constraint
16142 begin
16143 if No (Constraint) or else Is_Empty_Elmt_List (Constraint) then
16144 return No_Elist;
16145 end if;
16147 Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
16149 if No (Explicitly_Discriminated_Type) then
16150 return No_Elist;
16151 end if;
16153 Expansion := New_Elmt_List;
16155 Discriminant :=
16156 First_Stored_Discriminant (Explicitly_Discriminated_Type);
16157 while Present (Discriminant) loop
16158 Append_Elmt
16159 (Get_Discriminant_Value
16160 (Discriminant, Explicitly_Discriminated_Type, Constraint),
16161 To => Expansion);
16162 Next_Stored_Discriminant (Discriminant);
16163 end loop;
16165 return Expansion;
16166 end Expand_To_Stored_Constraint;
16168 ---------------------------
16169 -- Find_Hidden_Interface --
16170 ---------------------------
16172 function Find_Hidden_Interface
16173 (Src : Elist_Id;
16174 Dest : Elist_Id) return Entity_Id
16176 Iface : Entity_Id;
16177 Iface_Elmt : Elmt_Id;
16179 begin
16180 if Present (Src) and then Present (Dest) then
16181 Iface_Elmt := First_Elmt (Src);
16182 while Present (Iface_Elmt) loop
16183 Iface := Node (Iface_Elmt);
16185 if Is_Interface (Iface)
16186 and then not Contain_Interface (Iface, Dest)
16187 then
16188 return Iface;
16189 end if;
16191 Next_Elmt (Iface_Elmt);
16192 end loop;
16193 end if;
16195 return Empty;
16196 end Find_Hidden_Interface;
16198 --------------------
16199 -- Find_Type_Name --
16200 --------------------
16202 function Find_Type_Name (N : Node_Id) return Entity_Id is
16203 Id : constant Entity_Id := Defining_Identifier (N);
16204 Prev : Entity_Id;
16205 New_Id : Entity_Id;
16206 Prev_Par : Node_Id;
16208 procedure Check_Duplicate_Aspects;
16209 -- Check that aspects specified in a completion have not been specified
16210 -- already in the partial view. Type_Invariant and others can be
16211 -- specified on either view but never on both.
16213 procedure Tag_Mismatch;
16214 -- Diagnose a tagged partial view whose full view is untagged.
16215 -- We post the message on the full view, with a reference to
16216 -- the previous partial view. The partial view can be private
16217 -- or incomplete, and these are handled in a different manner,
16218 -- so we determine the position of the error message from the
16219 -- respective slocs of both.
16221 -----------------------------
16222 -- Check_Duplicate_Aspects --
16223 -----------------------------
16224 procedure Check_Duplicate_Aspects is
16225 Prev_Aspects : constant List_Id := Aspect_Specifications (Prev_Par);
16226 Full_Aspects : constant List_Id := Aspect_Specifications (N);
16227 F_Spec, P_Spec : Node_Id;
16229 begin
16230 if Present (Prev_Aspects) and then Present (Full_Aspects) then
16231 F_Spec := First (Full_Aspects);
16232 while Present (F_Spec) loop
16233 P_Spec := First (Prev_Aspects);
16234 while Present (P_Spec) loop
16235 if Chars (Identifier (P_Spec)) = Chars (Identifier (F_Spec))
16236 then
16237 Error_Msg_N
16238 ("aspect already specified in private declaration",
16239 F_Spec);
16240 Remove (F_Spec);
16241 return;
16242 end if;
16244 Next (P_Spec);
16245 end loop;
16247 Next (F_Spec);
16248 end loop;
16249 end if;
16250 end Check_Duplicate_Aspects;
16252 ------------------
16253 -- Tag_Mismatch --
16254 ------------------
16256 procedure Tag_Mismatch is
16257 begin
16258 if Sloc (Prev) < Sloc (Id) then
16259 if Ada_Version >= Ada_2012
16260 and then Nkind (N) = N_Private_Type_Declaration
16261 then
16262 Error_Msg_NE
16263 ("declaration of private } must be a tagged type ", Id, Prev);
16264 else
16265 Error_Msg_NE
16266 ("full declaration of } must be a tagged type ", Id, Prev);
16267 end if;
16269 else
16270 if Ada_Version >= Ada_2012
16271 and then Nkind (N) = N_Private_Type_Declaration
16272 then
16273 Error_Msg_NE
16274 ("declaration of private } must be a tagged type ", Prev, Id);
16275 else
16276 Error_Msg_NE
16277 ("full declaration of } must be a tagged type ", Prev, Id);
16278 end if;
16279 end if;
16280 end Tag_Mismatch;
16282 -- Start of processing for Find_Type_Name
16284 begin
16285 -- Find incomplete declaration, if one was given
16287 Prev := Current_Entity_In_Scope (Id);
16289 -- New type declaration
16291 if No (Prev) then
16292 Enter_Name (Id);
16293 return Id;
16295 -- Previous declaration exists
16297 else
16298 Prev_Par := Parent (Prev);
16300 -- Error if not incomplete/private case except if previous
16301 -- declaration is implicit, etc. Enter_Name will emit error if
16302 -- appropriate.
16304 if not Is_Incomplete_Or_Private_Type (Prev) then
16305 Enter_Name (Id);
16306 New_Id := Id;
16308 -- Check invalid completion of private or incomplete type
16310 elsif not Nkind_In (N, N_Full_Type_Declaration,
16311 N_Task_Type_Declaration,
16312 N_Protected_Type_Declaration)
16313 and then
16314 (Ada_Version < Ada_2012
16315 or else not Is_Incomplete_Type (Prev)
16316 or else not Nkind_In (N, N_Private_Type_Declaration,
16317 N_Private_Extension_Declaration))
16318 then
16319 -- Completion must be a full type declarations (RM 7.3(4))
16321 Error_Msg_Sloc := Sloc (Prev);
16322 Error_Msg_NE ("invalid completion of }", Id, Prev);
16324 -- Set scope of Id to avoid cascaded errors. Entity is never
16325 -- examined again, except when saving globals in generics.
16327 Set_Scope (Id, Current_Scope);
16328 New_Id := Id;
16330 -- If this is a repeated incomplete declaration, no further
16331 -- checks are possible.
16333 if Nkind (N) = N_Incomplete_Type_Declaration then
16334 return Prev;
16335 end if;
16337 -- Case of full declaration of incomplete type
16339 elsif Ekind (Prev) = E_Incomplete_Type
16340 and then (Ada_Version < Ada_2012
16341 or else No (Full_View (Prev))
16342 or else not Is_Private_Type (Full_View (Prev)))
16343 then
16344 -- Indicate that the incomplete declaration has a matching full
16345 -- declaration. The defining occurrence of the incomplete
16346 -- declaration remains the visible one, and the procedure
16347 -- Get_Full_View dereferences it whenever the type is used.
16349 if Present (Full_View (Prev)) then
16350 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
16351 end if;
16353 Set_Full_View (Prev, Id);
16354 Append_Entity (Id, Current_Scope);
16355 Set_Is_Public (Id, Is_Public (Prev));
16356 Set_Is_Internal (Id);
16357 New_Id := Prev;
16359 -- If the incomplete view is tagged, a class_wide type has been
16360 -- created already. Use it for the private type as well, in order
16361 -- to prevent multiple incompatible class-wide types that may be
16362 -- created for self-referential anonymous access components.
16364 if Is_Tagged_Type (Prev)
16365 and then Present (Class_Wide_Type (Prev))
16366 then
16367 Set_Ekind (Id, Ekind (Prev)); -- will be reset later
16368 Set_Class_Wide_Type (Id, Class_Wide_Type (Prev));
16370 -- The type of the classwide type is the current Id. Previously
16371 -- this was not done for private declarations because of order-
16372 -- of elaboration issues in the back-end, but gigi now handles
16373 -- this properly.
16375 Set_Etype (Class_Wide_Type (Id), Id);
16376 end if;
16378 -- Case of full declaration of private type
16380 else
16381 -- If the private type was a completion of an incomplete type then
16382 -- update Prev to reference the private type
16384 if Ada_Version >= Ada_2012
16385 and then Ekind (Prev) = E_Incomplete_Type
16386 and then Present (Full_View (Prev))
16387 and then Is_Private_Type (Full_View (Prev))
16388 then
16389 Prev := Full_View (Prev);
16390 Prev_Par := Parent (Prev);
16391 end if;
16393 if Nkind (N) = N_Full_Type_Declaration
16394 and then Nkind_In
16395 (Type_Definition (N), N_Record_Definition,
16396 N_Derived_Type_Definition)
16397 and then Interface_Present (Type_Definition (N))
16398 then
16399 Error_Msg_N
16400 ("completion of private type cannot be an interface", N);
16401 end if;
16403 if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
16404 if Etype (Prev) /= Prev then
16406 -- Prev is a private subtype or a derived type, and needs
16407 -- no completion.
16409 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
16410 New_Id := Id;
16412 elsif Ekind (Prev) = E_Private_Type
16413 and then Nkind_In (N, N_Task_Type_Declaration,
16414 N_Protected_Type_Declaration)
16415 then
16416 Error_Msg_N
16417 ("completion of nonlimited type cannot be limited", N);
16419 elsif Ekind (Prev) = E_Record_Type_With_Private
16420 and then Nkind_In (N, N_Task_Type_Declaration,
16421 N_Protected_Type_Declaration)
16422 then
16423 if not Is_Limited_Record (Prev) then
16424 Error_Msg_N
16425 ("completion of nonlimited type cannot be limited", N);
16427 elsif No (Interface_List (N)) then
16428 Error_Msg_N
16429 ("completion of tagged private type must be tagged",
16431 end if;
16432 end if;
16434 -- Ada 2005 (AI-251): Private extension declaration of a task
16435 -- type or a protected type. This case arises when covering
16436 -- interface types.
16438 elsif Nkind_In (N, N_Task_Type_Declaration,
16439 N_Protected_Type_Declaration)
16440 then
16441 null;
16443 elsif Nkind (N) /= N_Full_Type_Declaration
16444 or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
16445 then
16446 Error_Msg_N
16447 ("full view of private extension must be an extension", N);
16449 elsif not (Abstract_Present (Parent (Prev)))
16450 and then Abstract_Present (Type_Definition (N))
16451 then
16452 Error_Msg_N
16453 ("full view of non-abstract extension cannot be abstract", N);
16454 end if;
16456 if not In_Private_Part (Current_Scope) then
16457 Error_Msg_N
16458 ("declaration of full view must appear in private part", N);
16459 end if;
16461 if Ada_Version >= Ada_2012 then
16462 Check_Duplicate_Aspects;
16463 end if;
16465 Copy_And_Swap (Prev, Id);
16466 Set_Has_Private_Declaration (Prev);
16467 Set_Has_Private_Declaration (Id);
16469 -- AI12-0133: Indicate whether we have a partial view with
16470 -- unknown discriminants, in which case initialization of objects
16471 -- of the type do not receive an invariant check.
16473 Set_Partial_View_Has_Unknown_Discr
16474 (Prev, Has_Unknown_Discriminants (Id));
16476 -- Preserve aspect and iterator flags that may have been set on
16477 -- the partial view.
16479 Set_Has_Delayed_Aspects (Prev, Has_Delayed_Aspects (Id));
16480 Set_Has_Implicit_Dereference (Prev, Has_Implicit_Dereference (Id));
16482 -- If no error, propagate freeze_node from private to full view.
16483 -- It may have been generated for an early operational item.
16485 if Present (Freeze_Node (Id))
16486 and then Serious_Errors_Detected = 0
16487 and then No (Full_View (Id))
16488 then
16489 Set_Freeze_Node (Prev, Freeze_Node (Id));
16490 Set_Freeze_Node (Id, Empty);
16491 Set_First_Rep_Item (Prev, First_Rep_Item (Id));
16492 end if;
16494 Set_Full_View (Id, Prev);
16495 New_Id := Prev;
16496 end if;
16498 -- Verify that full declaration conforms to partial one
16500 if Is_Incomplete_Or_Private_Type (Prev)
16501 and then Present (Discriminant_Specifications (Prev_Par))
16502 then
16503 if Present (Discriminant_Specifications (N)) then
16504 if Ekind (Prev) = E_Incomplete_Type then
16505 Check_Discriminant_Conformance (N, Prev, Prev);
16506 else
16507 Check_Discriminant_Conformance (N, Prev, Id);
16508 end if;
16510 else
16511 Error_Msg_N
16512 ("missing discriminants in full type declaration", N);
16514 -- To avoid cascaded errors on subsequent use, share the
16515 -- discriminants of the partial view.
16517 Set_Discriminant_Specifications (N,
16518 Discriminant_Specifications (Prev_Par));
16519 end if;
16520 end if;
16522 -- A prior untagged partial view can have an associated class-wide
16523 -- type due to use of the class attribute, and in this case the full
16524 -- type must also be tagged. This Ada 95 usage is deprecated in favor
16525 -- of incomplete tagged declarations, but we check for it.
16527 if Is_Type (Prev)
16528 and then (Is_Tagged_Type (Prev)
16529 or else Present (Class_Wide_Type (Prev)))
16530 then
16531 -- Ada 2012 (AI05-0162): A private type may be the completion of
16532 -- an incomplete type.
16534 if Ada_Version >= Ada_2012
16535 and then Is_Incomplete_Type (Prev)
16536 and then Nkind_In (N, N_Private_Type_Declaration,
16537 N_Private_Extension_Declaration)
16538 then
16539 -- No need to check private extensions since they are tagged
16541 if Nkind (N) = N_Private_Type_Declaration
16542 and then not Tagged_Present (N)
16543 then
16544 Tag_Mismatch;
16545 end if;
16547 -- The full declaration is either a tagged type (including
16548 -- a synchronized type that implements interfaces) or a
16549 -- type extension, otherwise this is an error.
16551 elsif Nkind_In (N, N_Task_Type_Declaration,
16552 N_Protected_Type_Declaration)
16553 then
16554 if No (Interface_List (N)) and then not Error_Posted (N) then
16555 Tag_Mismatch;
16556 end if;
16558 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
16560 -- Indicate that the previous declaration (tagged incomplete
16561 -- or private declaration) requires the same on the full one.
16563 if not Tagged_Present (Type_Definition (N)) then
16564 Tag_Mismatch;
16565 Set_Is_Tagged_Type (Id);
16566 end if;
16568 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
16569 if No (Record_Extension_Part (Type_Definition (N))) then
16570 Error_Msg_NE
16571 ("full declaration of } must be a record extension",
16572 Prev, Id);
16574 -- Set some attributes to produce a usable full view
16576 Set_Is_Tagged_Type (Id);
16577 end if;
16579 else
16580 Tag_Mismatch;
16581 end if;
16582 end if;
16584 if Present (Prev)
16585 and then Nkind (Parent (Prev)) = N_Incomplete_Type_Declaration
16586 and then Present (Premature_Use (Parent (Prev)))
16587 then
16588 Error_Msg_Sloc := Sloc (N);
16589 Error_Msg_N
16590 ("\full declaration #", Premature_Use (Parent (Prev)));
16591 end if;
16593 return New_Id;
16594 end if;
16595 end Find_Type_Name;
16597 -------------------------
16598 -- Find_Type_Of_Object --
16599 -------------------------
16601 function Find_Type_Of_Object
16602 (Obj_Def : Node_Id;
16603 Related_Nod : Node_Id) return Entity_Id
16605 Def_Kind : constant Node_Kind := Nkind (Obj_Def);
16606 P : Node_Id := Parent (Obj_Def);
16607 T : Entity_Id;
16608 Nam : Name_Id;
16610 begin
16611 -- If the parent is a component_definition node we climb to the
16612 -- component_declaration node
16614 if Nkind (P) = N_Component_Definition then
16615 P := Parent (P);
16616 end if;
16618 -- Case of an anonymous array subtype
16620 if Nkind_In (Def_Kind, N_Constrained_Array_Definition,
16621 N_Unconstrained_Array_Definition)
16622 then
16623 T := Empty;
16624 Array_Type_Declaration (T, Obj_Def);
16626 -- Create an explicit subtype whenever possible
16628 elsif Nkind (P) /= N_Component_Declaration
16629 and then Def_Kind = N_Subtype_Indication
16630 then
16631 -- Base name of subtype on object name, which will be unique in
16632 -- the current scope.
16634 -- If this is a duplicate declaration, return base type, to avoid
16635 -- generating duplicate anonymous types.
16637 if Error_Posted (P) then
16638 Analyze (Subtype_Mark (Obj_Def));
16639 return Entity (Subtype_Mark (Obj_Def));
16640 end if;
16642 Nam :=
16643 New_External_Name
16644 (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
16646 T := Make_Defining_Identifier (Sloc (P), Nam);
16648 Insert_Action (Obj_Def,
16649 Make_Subtype_Declaration (Sloc (P),
16650 Defining_Identifier => T,
16651 Subtype_Indication => Relocate_Node (Obj_Def)));
16653 -- This subtype may need freezing, and this will not be done
16654 -- automatically if the object declaration is not in declarative
16655 -- part. Since this is an object declaration, the type cannot always
16656 -- be frozen here. Deferred constants do not freeze their type
16657 -- (which often enough will be private).
16659 if Nkind (P) = N_Object_Declaration
16660 and then Constant_Present (P)
16661 and then No (Expression (P))
16662 then
16663 null;
16665 -- Here we freeze the base type of object type to catch premature use
16666 -- of discriminated private type without a full view.
16668 else
16669 Insert_Actions (Obj_Def, Freeze_Entity (Base_Type (T), P));
16670 end if;
16672 -- Ada 2005 AI-406: the object definition in an object declaration
16673 -- can be an access definition.
16675 elsif Def_Kind = N_Access_Definition then
16676 T := Access_Definition (Related_Nod, Obj_Def);
16678 Set_Is_Local_Anonymous_Access
16680 V => (Ada_Version < Ada_2012)
16681 or else (Nkind (P) /= N_Object_Declaration)
16682 or else Is_Library_Level_Entity (Defining_Identifier (P)));
16684 -- Otherwise, the object definition is just a subtype_mark
16686 else
16687 T := Process_Subtype (Obj_Def, Related_Nod);
16689 -- If expansion is disabled an object definition that is an aggregate
16690 -- will not get expanded and may lead to scoping problems in the back
16691 -- end, if the object is referenced in an inner scope. In that case
16692 -- create an itype reference for the object definition now. This
16693 -- may be redundant in some cases, but harmless.
16695 if Is_Itype (T)
16696 and then Nkind (Related_Nod) = N_Object_Declaration
16697 and then ASIS_Mode
16698 then
16699 Build_Itype_Reference (T, Related_Nod);
16700 end if;
16701 end if;
16703 return T;
16704 end Find_Type_Of_Object;
16706 --------------------------------
16707 -- Find_Type_Of_Subtype_Indic --
16708 --------------------------------
16710 function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
16711 Typ : Entity_Id;
16713 begin
16714 -- Case of subtype mark with a constraint
16716 if Nkind (S) = N_Subtype_Indication then
16717 Find_Type (Subtype_Mark (S));
16718 Typ := Entity (Subtype_Mark (S));
16720 if not
16721 Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
16722 then
16723 Error_Msg_N
16724 ("incorrect constraint for this kind of type", Constraint (S));
16725 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
16726 end if;
16728 -- Otherwise we have a subtype mark without a constraint
16730 elsif Error_Posted (S) then
16731 Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
16732 return Any_Type;
16734 else
16735 Find_Type (S);
16736 Typ := Entity (S);
16737 end if;
16739 -- Check No_Wide_Characters restriction
16741 Check_Wide_Character_Restriction (Typ, S);
16743 return Typ;
16744 end Find_Type_Of_Subtype_Indic;
16746 -------------------------------------
16747 -- Floating_Point_Type_Declaration --
16748 -------------------------------------
16750 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
16751 Digs : constant Node_Id := Digits_Expression (Def);
16752 Max_Digs_Val : constant Uint := Digits_Value (Standard_Long_Long_Float);
16753 Digs_Val : Uint;
16754 Base_Typ : Entity_Id;
16755 Implicit_Base : Entity_Id;
16756 Bound : Node_Id;
16758 function Can_Derive_From (E : Entity_Id) return Boolean;
16759 -- Find if given digits value, and possibly a specified range, allows
16760 -- derivation from specified type
16762 function Find_Base_Type return Entity_Id;
16763 -- Find a predefined base type that Def can derive from, or generate
16764 -- an error and substitute Long_Long_Float if none exists.
16766 ---------------------
16767 -- Can_Derive_From --
16768 ---------------------
16770 function Can_Derive_From (E : Entity_Id) return Boolean is
16771 Spec : constant Entity_Id := Real_Range_Specification (Def);
16773 begin
16774 -- Check specified "digits" constraint
16776 if Digs_Val > Digits_Value (E) then
16777 return False;
16778 end if;
16780 -- Check for matching range, if specified
16782 if Present (Spec) then
16783 if Expr_Value_R (Type_Low_Bound (E)) >
16784 Expr_Value_R (Low_Bound (Spec))
16785 then
16786 return False;
16787 end if;
16789 if Expr_Value_R (Type_High_Bound (E)) <
16790 Expr_Value_R (High_Bound (Spec))
16791 then
16792 return False;
16793 end if;
16794 end if;
16796 return True;
16797 end Can_Derive_From;
16799 --------------------
16800 -- Find_Base_Type --
16801 --------------------
16803 function Find_Base_Type return Entity_Id is
16804 Choice : Elmt_Id := First_Elmt (Predefined_Float_Types);
16806 begin
16807 -- Iterate over the predefined types in order, returning the first
16808 -- one that Def can derive from.
16810 while Present (Choice) loop
16811 if Can_Derive_From (Node (Choice)) then
16812 return Node (Choice);
16813 end if;
16815 Next_Elmt (Choice);
16816 end loop;
16818 -- If we can't derive from any existing type, use Long_Long_Float
16819 -- and give appropriate message explaining the problem.
16821 if Digs_Val > Max_Digs_Val then
16822 -- It might be the case that there is a type with the requested
16823 -- range, just not the combination of digits and range.
16825 Error_Msg_N
16826 ("no predefined type has requested range and precision",
16827 Real_Range_Specification (Def));
16829 else
16830 Error_Msg_N
16831 ("range too large for any predefined type",
16832 Real_Range_Specification (Def));
16833 end if;
16835 return Standard_Long_Long_Float;
16836 end Find_Base_Type;
16838 -- Start of processing for Floating_Point_Type_Declaration
16840 begin
16841 Check_Restriction (No_Floating_Point, Def);
16843 -- Create an implicit base type
16845 Implicit_Base :=
16846 Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
16848 -- Analyze and verify digits value
16850 Analyze_And_Resolve (Digs, Any_Integer);
16851 Check_Digits_Expression (Digs);
16852 Digs_Val := Expr_Value (Digs);
16854 -- Process possible range spec and find correct type to derive from
16856 Process_Real_Range_Specification (Def);
16858 -- Check that requested number of digits is not too high.
16860 if Digs_Val > Max_Digs_Val then
16862 -- The check for Max_Base_Digits may be somewhat expensive, as it
16863 -- requires reading System, so only do it when necessary.
16865 declare
16866 Max_Base_Digits : constant Uint :=
16867 Expr_Value
16868 (Expression
16869 (Parent (RTE (RE_Max_Base_Digits))));
16871 begin
16872 if Digs_Val > Max_Base_Digits then
16873 Error_Msg_Uint_1 := Max_Base_Digits;
16874 Error_Msg_N ("digits value out of range, maximum is ^", Digs);
16876 elsif No (Real_Range_Specification (Def)) then
16877 Error_Msg_Uint_1 := Max_Digs_Val;
16878 Error_Msg_N ("types with more than ^ digits need range spec "
16879 & "(RM 3.5.7(6))", Digs);
16880 end if;
16881 end;
16882 end if;
16884 -- Find a suitable type to derive from or complain and use a substitute
16886 Base_Typ := Find_Base_Type;
16888 -- If there are bounds given in the declaration use them as the bounds
16889 -- of the type, otherwise use the bounds of the predefined base type
16890 -- that was chosen based on the Digits value.
16892 if Present (Real_Range_Specification (Def)) then
16893 Set_Scalar_Range (T, Real_Range_Specification (Def));
16894 Set_Is_Constrained (T);
16896 -- The bounds of this range must be converted to machine numbers
16897 -- in accordance with RM 4.9(38).
16899 Bound := Type_Low_Bound (T);
16901 if Nkind (Bound) = N_Real_Literal then
16902 Set_Realval
16903 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
16904 Set_Is_Machine_Number (Bound);
16905 end if;
16907 Bound := Type_High_Bound (T);
16909 if Nkind (Bound) = N_Real_Literal then
16910 Set_Realval
16911 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
16912 Set_Is_Machine_Number (Bound);
16913 end if;
16915 else
16916 Set_Scalar_Range (T, Scalar_Range (Base_Typ));
16917 end if;
16919 -- Complete definition of implicit base and declared first subtype. The
16920 -- inheritance of the rep item chain ensures that SPARK-related pragmas
16921 -- are not clobbered when the floating point type acts as a full view of
16922 -- a private type.
16924 Set_Etype (Implicit_Base, Base_Typ);
16925 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
16926 Set_Size_Info (Implicit_Base, Base_Typ);
16927 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
16928 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
16929 Set_Digits_Value (Implicit_Base, Digits_Value (Base_Typ));
16930 Set_Float_Rep (Implicit_Base, Float_Rep (Base_Typ));
16932 Set_Ekind (T, E_Floating_Point_Subtype);
16933 Set_Etype (T, Implicit_Base);
16934 Set_Size_Info (T, Implicit_Base);
16935 Set_RM_Size (T, RM_Size (Implicit_Base));
16936 Inherit_Rep_Item_Chain (T, Implicit_Base);
16937 Set_Digits_Value (T, Digs_Val);
16938 end Floating_Point_Type_Declaration;
16940 ----------------------------
16941 -- Get_Discriminant_Value --
16942 ----------------------------
16944 -- This is the situation:
16946 -- There is a non-derived type
16948 -- type T0 (Dx, Dy, Dz...)
16950 -- There are zero or more levels of derivation, with each derivation
16951 -- either purely inheriting the discriminants, or defining its own.
16953 -- type Ti is new Ti-1
16954 -- or
16955 -- type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
16956 -- or
16957 -- subtype Ti is ...
16959 -- The subtype issue is avoided by the use of Original_Record_Component,
16960 -- and the fact that derived subtypes also derive the constraints.
16962 -- This chain leads back from
16964 -- Typ_For_Constraint
16966 -- Typ_For_Constraint has discriminants, and the value for each
16967 -- discriminant is given by its corresponding Elmt of Constraints.
16969 -- Discriminant is some discriminant in this hierarchy
16971 -- We need to return its value
16973 -- We do this by recursively searching each level, and looking for
16974 -- Discriminant. Once we get to the bottom, we start backing up
16975 -- returning the value for it which may in turn be a discriminant
16976 -- further up, so on the backup we continue the substitution.
16978 function Get_Discriminant_Value
16979 (Discriminant : Entity_Id;
16980 Typ_For_Constraint : Entity_Id;
16981 Constraint : Elist_Id) return Node_Id
16983 function Root_Corresponding_Discriminant
16984 (Discr : Entity_Id) return Entity_Id;
16985 -- Given a discriminant, traverse the chain of inherited discriminants
16986 -- and return the topmost discriminant.
16988 function Search_Derivation_Levels
16989 (Ti : Entity_Id;
16990 Discrim_Values : Elist_Id;
16991 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
16992 -- This is the routine that performs the recursive search of levels
16993 -- as described above.
16995 -------------------------------------
16996 -- Root_Corresponding_Discriminant --
16997 -------------------------------------
16999 function Root_Corresponding_Discriminant
17000 (Discr : Entity_Id) return Entity_Id
17002 D : Entity_Id;
17004 begin
17005 D := Discr;
17006 while Present (Corresponding_Discriminant (D)) loop
17007 D := Corresponding_Discriminant (D);
17008 end loop;
17010 return D;
17011 end Root_Corresponding_Discriminant;
17013 ------------------------------
17014 -- Search_Derivation_Levels --
17015 ------------------------------
17017 function Search_Derivation_Levels
17018 (Ti : Entity_Id;
17019 Discrim_Values : Elist_Id;
17020 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
17022 Assoc : Elmt_Id;
17023 Disc : Entity_Id;
17024 Result : Node_Or_Entity_Id;
17025 Result_Entity : Node_Id;
17027 begin
17028 -- If inappropriate type, return Error, this happens only in
17029 -- cascaded error situations, and we want to avoid a blow up.
17031 if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
17032 return Error;
17033 end if;
17035 -- Look deeper if possible. Use Stored_Constraints only for
17036 -- untagged types. For tagged types use the given constraint.
17037 -- This asymmetry needs explanation???
17039 if not Stored_Discrim_Values
17040 and then Present (Stored_Constraint (Ti))
17041 and then not Is_Tagged_Type (Ti)
17042 then
17043 Result :=
17044 Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
17045 else
17046 declare
17047 Td : constant Entity_Id := Etype (Ti);
17049 begin
17050 if Td = Ti then
17051 Result := Discriminant;
17053 else
17054 if Present (Stored_Constraint (Ti)) then
17055 Result :=
17056 Search_Derivation_Levels
17057 (Td, Stored_Constraint (Ti), True);
17058 else
17059 Result :=
17060 Search_Derivation_Levels
17061 (Td, Discrim_Values, Stored_Discrim_Values);
17062 end if;
17063 end if;
17064 end;
17065 end if;
17067 -- Extra underlying places to search, if not found above. For
17068 -- concurrent types, the relevant discriminant appears in the
17069 -- corresponding record. For a type derived from a private type
17070 -- without discriminant, the full view inherits the discriminants
17071 -- of the full view of the parent.
17073 if Result = Discriminant then
17074 if Is_Concurrent_Type (Ti)
17075 and then Present (Corresponding_Record_Type (Ti))
17076 then
17077 Result :=
17078 Search_Derivation_Levels (
17079 Corresponding_Record_Type (Ti),
17080 Discrim_Values,
17081 Stored_Discrim_Values);
17083 elsif Is_Private_Type (Ti)
17084 and then not Has_Discriminants (Ti)
17085 and then Present (Full_View (Ti))
17086 and then Etype (Full_View (Ti)) /= Ti
17087 then
17088 Result :=
17089 Search_Derivation_Levels (
17090 Full_View (Ti),
17091 Discrim_Values,
17092 Stored_Discrim_Values);
17093 end if;
17094 end if;
17096 -- If Result is not a (reference to a) discriminant, return it,
17097 -- otherwise set Result_Entity to the discriminant.
17099 if Nkind (Result) = N_Defining_Identifier then
17100 pragma Assert (Result = Discriminant);
17101 Result_Entity := Result;
17103 else
17104 if not Denotes_Discriminant (Result) then
17105 return Result;
17106 end if;
17108 Result_Entity := Entity (Result);
17109 end if;
17111 -- See if this level of derivation actually has discriminants because
17112 -- tagged derivations can add them, hence the lower levels need not
17113 -- have any.
17115 if not Has_Discriminants (Ti) then
17116 return Result;
17117 end if;
17119 -- Scan Ti's discriminants for Result_Entity, and return its
17120 -- corresponding value, if any.
17122 Result_Entity := Original_Record_Component (Result_Entity);
17124 Assoc := First_Elmt (Discrim_Values);
17126 if Stored_Discrim_Values then
17127 Disc := First_Stored_Discriminant (Ti);
17128 else
17129 Disc := First_Discriminant (Ti);
17130 end if;
17132 while Present (Disc) loop
17133 pragma Assert (Present (Assoc));
17135 if Original_Record_Component (Disc) = Result_Entity then
17136 return Node (Assoc);
17137 end if;
17139 Next_Elmt (Assoc);
17141 if Stored_Discrim_Values then
17142 Next_Stored_Discriminant (Disc);
17143 else
17144 Next_Discriminant (Disc);
17145 end if;
17146 end loop;
17148 -- Could not find it
17150 return Result;
17151 end Search_Derivation_Levels;
17153 -- Local Variables
17155 Result : Node_Or_Entity_Id;
17157 -- Start of processing for Get_Discriminant_Value
17159 begin
17160 -- ??? This routine is a gigantic mess and will be deleted. For the
17161 -- time being just test for the trivial case before calling recurse.
17163 if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
17164 declare
17165 D : Entity_Id;
17166 E : Elmt_Id;
17168 begin
17169 D := First_Discriminant (Typ_For_Constraint);
17170 E := First_Elmt (Constraint);
17171 while Present (D) loop
17172 if Chars (D) = Chars (Discriminant) then
17173 return Node (E);
17174 end if;
17176 Next_Discriminant (D);
17177 Next_Elmt (E);
17178 end loop;
17179 end;
17180 end if;
17182 Result := Search_Derivation_Levels
17183 (Typ_For_Constraint, Constraint, False);
17185 -- ??? hack to disappear when this routine is gone
17187 if Nkind (Result) = N_Defining_Identifier then
17188 declare
17189 D : Entity_Id;
17190 E : Elmt_Id;
17192 begin
17193 D := First_Discriminant (Typ_For_Constraint);
17194 E := First_Elmt (Constraint);
17195 while Present (D) loop
17196 if Root_Corresponding_Discriminant (D) = Discriminant then
17197 return Node (E);
17198 end if;
17200 Next_Discriminant (D);
17201 Next_Elmt (E);
17202 end loop;
17203 end;
17204 end if;
17206 pragma Assert (Nkind (Result) /= N_Defining_Identifier);
17207 return Result;
17208 end Get_Discriminant_Value;
17210 --------------------------
17211 -- Has_Range_Constraint --
17212 --------------------------
17214 function Has_Range_Constraint (N : Node_Id) return Boolean is
17215 C : constant Node_Id := Constraint (N);
17217 begin
17218 if Nkind (C) = N_Range_Constraint then
17219 return True;
17221 elsif Nkind (C) = N_Digits_Constraint then
17222 return
17223 Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
17224 or else Present (Range_Constraint (C));
17226 elsif Nkind (C) = N_Delta_Constraint then
17227 return Present (Range_Constraint (C));
17229 else
17230 return False;
17231 end if;
17232 end Has_Range_Constraint;
17234 ------------------------
17235 -- Inherit_Components --
17236 ------------------------
17238 function Inherit_Components
17239 (N : Node_Id;
17240 Parent_Base : Entity_Id;
17241 Derived_Base : Entity_Id;
17242 Is_Tagged : Boolean;
17243 Inherit_Discr : Boolean;
17244 Discs : Elist_Id) return Elist_Id
17246 Assoc_List : constant Elist_Id := New_Elmt_List;
17248 procedure Inherit_Component
17249 (Old_C : Entity_Id;
17250 Plain_Discrim : Boolean := False;
17251 Stored_Discrim : Boolean := False);
17252 -- Inherits component Old_C from Parent_Base to the Derived_Base. If
17253 -- Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
17254 -- True, Old_C is a stored discriminant. If they are both false then
17255 -- Old_C is a regular component.
17257 -----------------------
17258 -- Inherit_Component --
17259 -----------------------
17261 procedure Inherit_Component
17262 (Old_C : Entity_Id;
17263 Plain_Discrim : Boolean := False;
17264 Stored_Discrim : Boolean := False)
17266 procedure Set_Anonymous_Type (Id : Entity_Id);
17267 -- Id denotes the entity of an access discriminant or anonymous
17268 -- access component. Set the type of Id to either the same type of
17269 -- Old_C or create a new one depending on whether the parent and
17270 -- the child types are in the same scope.
17272 ------------------------
17273 -- Set_Anonymous_Type --
17274 ------------------------
17276 procedure Set_Anonymous_Type (Id : Entity_Id) is
17277 Old_Typ : constant Entity_Id := Etype (Old_C);
17279 begin
17280 if Scope (Parent_Base) = Scope (Derived_Base) then
17281 Set_Etype (Id, Old_Typ);
17283 -- The parent and the derived type are in two different scopes.
17284 -- Reuse the type of the original discriminant / component by
17285 -- copying it in order to preserve all attributes.
17287 else
17288 declare
17289 Typ : constant Entity_Id := New_Copy (Old_Typ);
17291 begin
17292 Set_Etype (Id, Typ);
17294 -- Since we do not generate component declarations for
17295 -- inherited components, associate the itype with the
17296 -- derived type.
17298 Set_Associated_Node_For_Itype (Typ, Parent (Derived_Base));
17299 Set_Scope (Typ, Derived_Base);
17300 end;
17301 end if;
17302 end Set_Anonymous_Type;
17304 -- Local variables and constants
17306 New_C : constant Entity_Id := New_Copy (Old_C);
17308 Corr_Discrim : Entity_Id;
17309 Discrim : Entity_Id;
17311 -- Start of processing for Inherit_Component
17313 begin
17314 pragma Assert (not Is_Tagged or not Stored_Discrim);
17316 Set_Parent (New_C, Parent (Old_C));
17318 -- Regular discriminants and components must be inserted in the scope
17319 -- of the Derived_Base. Do it here.
17321 if not Stored_Discrim then
17322 Enter_Name (New_C);
17323 end if;
17325 -- For tagged types the Original_Record_Component must point to
17326 -- whatever this field was pointing to in the parent type. This has
17327 -- already been achieved by the call to New_Copy above.
17329 if not Is_Tagged then
17330 Set_Original_Record_Component (New_C, New_C);
17331 end if;
17333 -- Set the proper type of an access discriminant
17335 if Ekind (New_C) = E_Discriminant
17336 and then Ekind (Etype (New_C)) = E_Anonymous_Access_Type
17337 then
17338 Set_Anonymous_Type (New_C);
17339 end if;
17341 -- If we have inherited a component then see if its Etype contains
17342 -- references to Parent_Base discriminants. In this case, replace
17343 -- these references with the constraints given in Discs. We do not
17344 -- do this for the partial view of private types because this is
17345 -- not needed (only the components of the full view will be used
17346 -- for code generation) and cause problem. We also avoid this
17347 -- transformation in some error situations.
17349 if Ekind (New_C) = E_Component then
17351 -- Set the proper type of an anonymous access component
17353 if Ekind (Etype (New_C)) = E_Anonymous_Access_Type then
17354 Set_Anonymous_Type (New_C);
17356 elsif (Is_Private_Type (Derived_Base)
17357 and then not Is_Generic_Type (Derived_Base))
17358 or else (Is_Empty_Elmt_List (Discs)
17359 and then not Expander_Active)
17360 then
17361 Set_Etype (New_C, Etype (Old_C));
17363 else
17364 -- The current component introduces a circularity of the
17365 -- following kind:
17367 -- limited with Pack_2;
17368 -- package Pack_1 is
17369 -- type T_1 is tagged record
17370 -- Comp : access Pack_2.T_2;
17371 -- ...
17372 -- end record;
17373 -- end Pack_1;
17375 -- with Pack_1;
17376 -- package Pack_2 is
17377 -- type T_2 is new Pack_1.T_1 with ...;
17378 -- end Pack_2;
17380 Set_Etype
17381 (New_C,
17382 Constrain_Component_Type
17383 (Old_C, Derived_Base, N, Parent_Base, Discs));
17384 end if;
17385 end if;
17387 -- In derived tagged types it is illegal to reference a non
17388 -- discriminant component in the parent type. To catch this, mark
17389 -- these components with an Ekind of E_Void. This will be reset in
17390 -- Record_Type_Definition after processing the record extension of
17391 -- the derived type.
17393 -- If the declaration is a private extension, there is no further
17394 -- record extension to process, and the components retain their
17395 -- current kind, because they are visible at this point.
17397 if Is_Tagged and then Ekind (New_C) = E_Component
17398 and then Nkind (N) /= N_Private_Extension_Declaration
17399 then
17400 Set_Ekind (New_C, E_Void);
17401 end if;
17403 if Plain_Discrim then
17404 Set_Corresponding_Discriminant (New_C, Old_C);
17405 Build_Discriminal (New_C);
17407 -- If we are explicitly inheriting a stored discriminant it will be
17408 -- completely hidden.
17410 elsif Stored_Discrim then
17411 Set_Corresponding_Discriminant (New_C, Empty);
17412 Set_Discriminal (New_C, Empty);
17413 Set_Is_Completely_Hidden (New_C);
17415 -- Set the Original_Record_Component of each discriminant in the
17416 -- derived base to point to the corresponding stored that we just
17417 -- created.
17419 Discrim := First_Discriminant (Derived_Base);
17420 while Present (Discrim) loop
17421 Corr_Discrim := Corresponding_Discriminant (Discrim);
17423 -- Corr_Discrim could be missing in an error situation
17425 if Present (Corr_Discrim)
17426 and then Original_Record_Component (Corr_Discrim) = Old_C
17427 then
17428 Set_Original_Record_Component (Discrim, New_C);
17429 end if;
17431 Next_Discriminant (Discrim);
17432 end loop;
17434 Append_Entity (New_C, Derived_Base);
17435 end if;
17437 if not Is_Tagged then
17438 Append_Elmt (Old_C, Assoc_List);
17439 Append_Elmt (New_C, Assoc_List);
17440 end if;
17441 end Inherit_Component;
17443 -- Variables local to Inherit_Component
17445 Loc : constant Source_Ptr := Sloc (N);
17447 Parent_Discrim : Entity_Id;
17448 Stored_Discrim : Entity_Id;
17449 D : Entity_Id;
17450 Component : Entity_Id;
17452 -- Start of processing for Inherit_Components
17454 begin
17455 if not Is_Tagged then
17456 Append_Elmt (Parent_Base, Assoc_List);
17457 Append_Elmt (Derived_Base, Assoc_List);
17458 end if;
17460 -- Inherit parent discriminants if needed
17462 if Inherit_Discr then
17463 Parent_Discrim := First_Discriminant (Parent_Base);
17464 while Present (Parent_Discrim) loop
17465 Inherit_Component (Parent_Discrim, Plain_Discrim => True);
17466 Next_Discriminant (Parent_Discrim);
17467 end loop;
17468 end if;
17470 -- Create explicit stored discrims for untagged types when necessary
17472 if not Has_Unknown_Discriminants (Derived_Base)
17473 and then Has_Discriminants (Parent_Base)
17474 and then not Is_Tagged
17475 and then
17476 (not Inherit_Discr
17477 or else First_Discriminant (Parent_Base) /=
17478 First_Stored_Discriminant (Parent_Base))
17479 then
17480 Stored_Discrim := First_Stored_Discriminant (Parent_Base);
17481 while Present (Stored_Discrim) loop
17482 Inherit_Component (Stored_Discrim, Stored_Discrim => True);
17483 Next_Stored_Discriminant (Stored_Discrim);
17484 end loop;
17485 end if;
17487 -- See if we can apply the second transformation for derived types, as
17488 -- explained in point 6. in the comments above Build_Derived_Record_Type
17489 -- This is achieved by appending Derived_Base discriminants into Discs,
17490 -- which has the side effect of returning a non empty Discs list to the
17491 -- caller of Inherit_Components, which is what we want. This must be
17492 -- done for private derived types if there are explicit stored
17493 -- discriminants, to ensure that we can retrieve the values of the
17494 -- constraints provided in the ancestors.
17496 if Inherit_Discr
17497 and then Is_Empty_Elmt_List (Discs)
17498 and then Present (First_Discriminant (Derived_Base))
17499 and then
17500 (not Is_Private_Type (Derived_Base)
17501 or else Is_Completely_Hidden
17502 (First_Stored_Discriminant (Derived_Base))
17503 or else Is_Generic_Type (Derived_Base))
17504 then
17505 D := First_Discriminant (Derived_Base);
17506 while Present (D) loop
17507 Append_Elmt (New_Occurrence_Of (D, Loc), Discs);
17508 Next_Discriminant (D);
17509 end loop;
17510 end if;
17512 -- Finally, inherit non-discriminant components unless they are not
17513 -- visible because defined or inherited from the full view of the
17514 -- parent. Don't inherit the _parent field of the parent type.
17516 Component := First_Entity (Parent_Base);
17517 while Present (Component) loop
17519 -- Ada 2005 (AI-251): Do not inherit components associated with
17520 -- secondary tags of the parent.
17522 if Ekind (Component) = E_Component
17523 and then Present (Related_Type (Component))
17524 then
17525 null;
17527 elsif Ekind (Component) /= E_Component
17528 or else Chars (Component) = Name_uParent
17529 then
17530 null;
17532 -- If the derived type is within the parent type's declarative
17533 -- region, then the components can still be inherited even though
17534 -- they aren't visible at this point. This can occur for cases
17535 -- such as within public child units where the components must
17536 -- become visible upon entering the child unit's private part.
17538 elsif not Is_Visible_Component (Component)
17539 and then not In_Open_Scopes (Scope (Parent_Base))
17540 then
17541 null;
17543 elsif Ekind_In (Derived_Base, E_Private_Type,
17544 E_Limited_Private_Type)
17545 then
17546 null;
17548 else
17549 Inherit_Component (Component);
17550 end if;
17552 Next_Entity (Component);
17553 end loop;
17555 -- For tagged derived types, inherited discriminants cannot be used in
17556 -- component declarations of the record extension part. To achieve this
17557 -- we mark the inherited discriminants as not visible.
17559 if Is_Tagged and then Inherit_Discr then
17560 D := First_Discriminant (Derived_Base);
17561 while Present (D) loop
17562 Set_Is_Immediately_Visible (D, False);
17563 Next_Discriminant (D);
17564 end loop;
17565 end if;
17567 return Assoc_List;
17568 end Inherit_Components;
17570 -----------------------------
17571 -- Inherit_Predicate_Flags --
17572 -----------------------------
17574 procedure Inherit_Predicate_Flags (Subt, Par : Entity_Id) is
17575 begin
17576 Set_Has_Predicates (Subt, Has_Predicates (Par));
17577 Set_Has_Static_Predicate_Aspect
17578 (Subt, Has_Static_Predicate_Aspect (Par));
17579 Set_Has_Dynamic_Predicate_Aspect
17580 (Subt, Has_Dynamic_Predicate_Aspect (Par));
17581 end Inherit_Predicate_Flags;
17583 ----------------------
17584 -- Is_EVF_Procedure --
17585 ----------------------
17587 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean is
17588 Formal : Entity_Id;
17590 begin
17591 -- Examine the formals of an Extensions_Visible False procedure looking
17592 -- for a controlling OUT parameter.
17594 if Ekind (Subp) = E_Procedure
17595 and then Extensions_Visible_Status (Subp) = Extensions_Visible_False
17596 then
17597 Formal := First_Formal (Subp);
17598 while Present (Formal) loop
17599 if Ekind (Formal) = E_Out_Parameter
17600 and then Is_Controlling_Formal (Formal)
17601 then
17602 return True;
17603 end if;
17605 Next_Formal (Formal);
17606 end loop;
17607 end if;
17609 return False;
17610 end Is_EVF_Procedure;
17612 -----------------------
17613 -- Is_Null_Extension --
17614 -----------------------
17616 function Is_Null_Extension (T : Entity_Id) return Boolean is
17617 Type_Decl : constant Node_Id := Parent (Base_Type (T));
17618 Comp_List : Node_Id;
17619 Comp : Node_Id;
17621 begin
17622 if Nkind (Type_Decl) /= N_Full_Type_Declaration
17623 or else not Is_Tagged_Type (T)
17624 or else Nkind (Type_Definition (Type_Decl)) /=
17625 N_Derived_Type_Definition
17626 or else No (Record_Extension_Part (Type_Definition (Type_Decl)))
17627 then
17628 return False;
17629 end if;
17631 Comp_List :=
17632 Component_List (Record_Extension_Part (Type_Definition (Type_Decl)));
17634 if Present (Discriminant_Specifications (Type_Decl)) then
17635 return False;
17637 elsif Present (Comp_List)
17638 and then Is_Non_Empty_List (Component_Items (Comp_List))
17639 then
17640 Comp := First (Component_Items (Comp_List));
17642 -- Only user-defined components are relevant. The component list
17643 -- may also contain a parent component and internal components
17644 -- corresponding to secondary tags, but these do not determine
17645 -- whether this is a null extension.
17647 while Present (Comp) loop
17648 if Comes_From_Source (Comp) then
17649 return False;
17650 end if;
17652 Next (Comp);
17653 end loop;
17655 return True;
17657 else
17658 return True;
17659 end if;
17660 end Is_Null_Extension;
17662 ------------------------------
17663 -- Is_Valid_Constraint_Kind --
17664 ------------------------------
17666 function Is_Valid_Constraint_Kind
17667 (T_Kind : Type_Kind;
17668 Constraint_Kind : Node_Kind) return Boolean
17670 begin
17671 case T_Kind is
17672 when Enumeration_Kind |
17673 Integer_Kind =>
17674 return Constraint_Kind = N_Range_Constraint;
17676 when Decimal_Fixed_Point_Kind =>
17677 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
17678 N_Range_Constraint);
17680 when Ordinary_Fixed_Point_Kind =>
17681 return Nkind_In (Constraint_Kind, N_Delta_Constraint,
17682 N_Range_Constraint);
17684 when Float_Kind =>
17685 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
17686 N_Range_Constraint);
17688 when Access_Kind |
17689 Array_Kind |
17690 E_Record_Type |
17691 E_Record_Subtype |
17692 Class_Wide_Kind |
17693 E_Incomplete_Type |
17694 Private_Kind |
17695 Concurrent_Kind =>
17696 return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
17698 when others =>
17699 return True; -- Error will be detected later
17700 end case;
17701 end Is_Valid_Constraint_Kind;
17703 --------------------------
17704 -- Is_Visible_Component --
17705 --------------------------
17707 function Is_Visible_Component
17708 (C : Entity_Id;
17709 N : Node_Id := Empty) return Boolean
17711 Original_Comp : Entity_Id := Empty;
17712 Original_Scope : Entity_Id;
17713 Type_Scope : Entity_Id;
17715 function Is_Local_Type (Typ : Entity_Id) return Boolean;
17716 -- Check whether parent type of inherited component is declared locally,
17717 -- possibly within a nested package or instance. The current scope is
17718 -- the derived record itself.
17720 -------------------
17721 -- Is_Local_Type --
17722 -------------------
17724 function Is_Local_Type (Typ : Entity_Id) return Boolean is
17725 Scop : Entity_Id;
17727 begin
17728 Scop := Scope (Typ);
17729 while Present (Scop)
17730 and then Scop /= Standard_Standard
17731 loop
17732 if Scop = Scope (Current_Scope) then
17733 return True;
17734 end if;
17736 Scop := Scope (Scop);
17737 end loop;
17739 return False;
17740 end Is_Local_Type;
17742 -- Start of processing for Is_Visible_Component
17744 begin
17745 if Ekind_In (C, E_Component, E_Discriminant) then
17746 Original_Comp := Original_Record_Component (C);
17747 end if;
17749 if No (Original_Comp) then
17751 -- Premature usage, or previous error
17753 return False;
17755 else
17756 Original_Scope := Scope (Original_Comp);
17757 Type_Scope := Scope (Base_Type (Scope (C)));
17758 end if;
17760 -- This test only concerns tagged types
17762 if not Is_Tagged_Type (Original_Scope) then
17763 return True;
17765 -- If it is _Parent or _Tag, there is no visibility issue
17767 elsif not Comes_From_Source (Original_Comp) then
17768 return True;
17770 -- Discriminants are visible unless the (private) type has unknown
17771 -- discriminants. If the discriminant reference is inserted for a
17772 -- discriminant check on a full view it is also visible.
17774 elsif Ekind (Original_Comp) = E_Discriminant
17775 and then
17776 (not Has_Unknown_Discriminants (Original_Scope)
17777 or else (Present (N)
17778 and then Nkind (N) = N_Selected_Component
17779 and then Nkind (Prefix (N)) = N_Type_Conversion
17780 and then not Comes_From_Source (Prefix (N))))
17781 then
17782 return True;
17784 -- In the body of an instantiation, no need to check for the visibility
17785 -- of a component.
17787 elsif In_Instance_Body then
17788 return True;
17790 -- If the component has been declared in an ancestor which is currently
17791 -- a private type, then it is not visible. The same applies if the
17792 -- component's containing type is not in an open scope and the original
17793 -- component's enclosing type is a visible full view of a private type
17794 -- (which can occur in cases where an attempt is being made to reference
17795 -- a component in a sibling package that is inherited from a visible
17796 -- component of a type in an ancestor package; the component in the
17797 -- sibling package should not be visible even though the component it
17798 -- inherited from is visible). This does not apply however in the case
17799 -- where the scope of the type is a private child unit, or when the
17800 -- parent comes from a local package in which the ancestor is currently
17801 -- visible. The latter suppression of visibility is needed for cases
17802 -- that are tested in B730006.
17804 elsif Is_Private_Type (Original_Scope)
17805 or else
17806 (not Is_Private_Descendant (Type_Scope)
17807 and then not In_Open_Scopes (Type_Scope)
17808 and then Has_Private_Declaration (Original_Scope))
17809 then
17810 -- If the type derives from an entity in a formal package, there
17811 -- are no additional visible components.
17813 if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
17814 N_Formal_Package_Declaration
17815 then
17816 return False;
17818 -- if we are not in the private part of the current package, there
17819 -- are no additional visible components.
17821 elsif Ekind (Scope (Current_Scope)) = E_Package
17822 and then not In_Private_Part (Scope (Current_Scope))
17823 then
17824 return False;
17825 else
17826 return
17827 Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
17828 and then In_Open_Scopes (Scope (Original_Scope))
17829 and then Is_Local_Type (Type_Scope);
17830 end if;
17832 -- There is another weird way in which a component may be invisible when
17833 -- the private and the full view are not derived from the same ancestor.
17834 -- Here is an example :
17836 -- type A1 is tagged record F1 : integer; end record;
17837 -- type A2 is new A1 with record F2 : integer; end record;
17838 -- type T is new A1 with private;
17839 -- private
17840 -- type T is new A2 with null record;
17842 -- In this case, the full view of T inherits F1 and F2 but the private
17843 -- view inherits only F1
17845 else
17846 declare
17847 Ancestor : Entity_Id := Scope (C);
17849 begin
17850 loop
17851 if Ancestor = Original_Scope then
17852 return True;
17853 elsif Ancestor = Etype (Ancestor) then
17854 return False;
17855 end if;
17857 Ancestor := Etype (Ancestor);
17858 end loop;
17859 end;
17860 end if;
17861 end Is_Visible_Component;
17863 --------------------------
17864 -- Make_Class_Wide_Type --
17865 --------------------------
17867 procedure Make_Class_Wide_Type (T : Entity_Id) is
17868 CW_Type : Entity_Id;
17869 CW_Name : Name_Id;
17870 Next_E : Entity_Id;
17872 begin
17873 if Present (Class_Wide_Type (T)) then
17875 -- The class-wide type is a partially decorated entity created for a
17876 -- unanalyzed tagged type referenced through a limited with clause.
17877 -- When the tagged type is analyzed, its class-wide type needs to be
17878 -- redecorated. Note that we reuse the entity created by Decorate_
17879 -- Tagged_Type in order to preserve all links.
17881 if Materialize_Entity (Class_Wide_Type (T)) then
17882 CW_Type := Class_Wide_Type (T);
17883 Set_Materialize_Entity (CW_Type, False);
17885 -- The class wide type can have been defined by the partial view, in
17886 -- which case everything is already done.
17888 else
17889 return;
17890 end if;
17892 -- Default case, we need to create a new class-wide type
17894 else
17895 CW_Type :=
17896 New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
17897 end if;
17899 -- Inherit root type characteristics
17901 CW_Name := Chars (CW_Type);
17902 Next_E := Next_Entity (CW_Type);
17903 Copy_Node (T, CW_Type);
17904 Set_Comes_From_Source (CW_Type, False);
17905 Set_Chars (CW_Type, CW_Name);
17906 Set_Parent (CW_Type, Parent (T));
17907 Set_Next_Entity (CW_Type, Next_E);
17909 -- Ensure we have a new freeze node for the class-wide type. The partial
17910 -- view may have freeze action of its own, requiring a proper freeze
17911 -- node, and the same freeze node cannot be shared between the two
17912 -- types.
17914 Set_Has_Delayed_Freeze (CW_Type);
17915 Set_Freeze_Node (CW_Type, Empty);
17917 -- Customize the class-wide type: It has no prim. op., it cannot be
17918 -- abstract and its Etype points back to the specific root type.
17920 Set_Ekind (CW_Type, E_Class_Wide_Type);
17921 Set_Is_Tagged_Type (CW_Type, True);
17922 Set_Direct_Primitive_Operations (CW_Type, New_Elmt_List);
17923 Set_Is_Abstract_Type (CW_Type, False);
17924 Set_Is_Constrained (CW_Type, False);
17925 Set_Is_First_Subtype (CW_Type, Is_First_Subtype (T));
17926 Set_Default_SSO (CW_Type);
17928 if Ekind (T) = E_Class_Wide_Subtype then
17929 Set_Etype (CW_Type, Etype (Base_Type (T)));
17930 else
17931 Set_Etype (CW_Type, T);
17932 end if;
17934 Set_No_Tagged_Streams_Pragma (CW_Type, No_Tagged_Streams);
17936 -- If this is the class_wide type of a constrained subtype, it does
17937 -- not have discriminants.
17939 Set_Has_Discriminants (CW_Type,
17940 Has_Discriminants (T) and then not Is_Constrained (T));
17942 Set_Has_Unknown_Discriminants (CW_Type, True);
17943 Set_Class_Wide_Type (T, CW_Type);
17944 Set_Equivalent_Type (CW_Type, Empty);
17946 -- The class-wide type of a class-wide type is itself (RM 3.9(14))
17948 Set_Class_Wide_Type (CW_Type, CW_Type);
17949 end Make_Class_Wide_Type;
17951 ----------------
17952 -- Make_Index --
17953 ----------------
17955 procedure Make_Index
17956 (N : Node_Id;
17957 Related_Nod : Node_Id;
17958 Related_Id : Entity_Id := Empty;
17959 Suffix_Index : Nat := 1;
17960 In_Iter_Schm : Boolean := False)
17962 R : Node_Id;
17963 T : Entity_Id;
17964 Def_Id : Entity_Id := Empty;
17965 Found : Boolean := False;
17967 begin
17968 -- For a discrete range used in a constrained array definition and
17969 -- defined by a range, an implicit conversion to the predefined type
17970 -- INTEGER is assumed if each bound is either a numeric literal, a named
17971 -- number, or an attribute, and the type of both bounds (prior to the
17972 -- implicit conversion) is the type universal_integer. Otherwise, both
17973 -- bounds must be of the same discrete type, other than universal
17974 -- integer; this type must be determinable independently of the
17975 -- context, but using the fact that the type must be discrete and that
17976 -- both bounds must have the same type.
17978 -- Character literals also have a universal type in the absence of
17979 -- of additional context, and are resolved to Standard_Character.
17981 if Nkind (N) = N_Range then
17983 -- The index is given by a range constraint. The bounds are known
17984 -- to be of a consistent type.
17986 if not Is_Overloaded (N) then
17987 T := Etype (N);
17989 -- For universal bounds, choose the specific predefined type
17991 if T = Universal_Integer then
17992 T := Standard_Integer;
17994 elsif T = Any_Character then
17995 Ambiguous_Character (Low_Bound (N));
17997 T := Standard_Character;
17998 end if;
18000 -- The node may be overloaded because some user-defined operators
18001 -- are available, but if a universal interpretation exists it is
18002 -- also the selected one.
18004 elsif Universal_Interpretation (N) = Universal_Integer then
18005 T := Standard_Integer;
18007 else
18008 T := Any_Type;
18010 declare
18011 Ind : Interp_Index;
18012 It : Interp;
18014 begin
18015 Get_First_Interp (N, Ind, It);
18016 while Present (It.Typ) loop
18017 if Is_Discrete_Type (It.Typ) then
18019 if Found
18020 and then not Covers (It.Typ, T)
18021 and then not Covers (T, It.Typ)
18022 then
18023 Error_Msg_N ("ambiguous bounds in discrete range", N);
18024 exit;
18025 else
18026 T := It.Typ;
18027 Found := True;
18028 end if;
18029 end if;
18031 Get_Next_Interp (Ind, It);
18032 end loop;
18034 if T = Any_Type then
18035 Error_Msg_N ("discrete type required for range", N);
18036 Set_Etype (N, Any_Type);
18037 return;
18039 elsif T = Universal_Integer then
18040 T := Standard_Integer;
18041 end if;
18042 end;
18043 end if;
18045 if not Is_Discrete_Type (T) then
18046 Error_Msg_N ("discrete type required for range", N);
18047 Set_Etype (N, Any_Type);
18048 return;
18049 end if;
18051 if Nkind (Low_Bound (N)) = N_Attribute_Reference
18052 and then Attribute_Name (Low_Bound (N)) = Name_First
18053 and then Is_Entity_Name (Prefix (Low_Bound (N)))
18054 and then Is_Type (Entity (Prefix (Low_Bound (N))))
18055 and then Is_Discrete_Type (Entity (Prefix (Low_Bound (N))))
18056 then
18057 -- The type of the index will be the type of the prefix, as long
18058 -- as the upper bound is 'Last of the same type.
18060 Def_Id := Entity (Prefix (Low_Bound (N)));
18062 if Nkind (High_Bound (N)) /= N_Attribute_Reference
18063 or else Attribute_Name (High_Bound (N)) /= Name_Last
18064 or else not Is_Entity_Name (Prefix (High_Bound (N)))
18065 or else Entity (Prefix (High_Bound (N))) /= Def_Id
18066 then
18067 Def_Id := Empty;
18068 end if;
18069 end if;
18071 R := N;
18072 Process_Range_Expr_In_Decl (R, T, In_Iter_Schm => In_Iter_Schm);
18074 elsif Nkind (N) = N_Subtype_Indication then
18076 -- The index is given by a subtype with a range constraint
18078 T := Base_Type (Entity (Subtype_Mark (N)));
18080 if not Is_Discrete_Type (T) then
18081 Error_Msg_N ("discrete type required for range", N);
18082 Set_Etype (N, Any_Type);
18083 return;
18084 end if;
18086 R := Range_Expression (Constraint (N));
18088 Resolve (R, T);
18089 Process_Range_Expr_In_Decl
18090 (R, Entity (Subtype_Mark (N)), In_Iter_Schm => In_Iter_Schm);
18092 elsif Nkind (N) = N_Attribute_Reference then
18094 -- Catch beginner's error (use of attribute other than 'Range)
18096 if Attribute_Name (N) /= Name_Range then
18097 Error_Msg_N ("expect attribute ''Range", N);
18098 Set_Etype (N, Any_Type);
18099 return;
18100 end if;
18102 -- If the node denotes the range of a type mark, that is also the
18103 -- resulting type, and we do not need to create an Itype for it.
18105 if Is_Entity_Name (Prefix (N))
18106 and then Comes_From_Source (N)
18107 and then Is_Type (Entity (Prefix (N)))
18108 and then Is_Discrete_Type (Entity (Prefix (N)))
18109 then
18110 Def_Id := Entity (Prefix (N));
18111 end if;
18113 Analyze_And_Resolve (N);
18114 T := Etype (N);
18115 R := N;
18117 -- If none of the above, must be a subtype. We convert this to a
18118 -- range attribute reference because in the case of declared first
18119 -- named subtypes, the types in the range reference can be different
18120 -- from the type of the entity. A range attribute normalizes the
18121 -- reference and obtains the correct types for the bounds.
18123 -- This transformation is in the nature of an expansion, is only
18124 -- done if expansion is active. In particular, it is not done on
18125 -- formal generic types, because we need to retain the name of the
18126 -- original index for instantiation purposes.
18128 else
18129 if not Is_Entity_Name (N) or else not Is_Type (Entity (N)) then
18130 Error_Msg_N ("invalid subtype mark in discrete range ", N);
18131 Set_Etype (N, Any_Integer);
18132 return;
18134 else
18135 -- The type mark may be that of an incomplete type. It is only
18136 -- now that we can get the full view, previous analysis does
18137 -- not look specifically for a type mark.
18139 Set_Entity (N, Get_Full_View (Entity (N)));
18140 Set_Etype (N, Entity (N));
18141 Def_Id := Entity (N);
18143 if not Is_Discrete_Type (Def_Id) then
18144 Error_Msg_N ("discrete type required for index", N);
18145 Set_Etype (N, Any_Type);
18146 return;
18147 end if;
18148 end if;
18150 if Expander_Active then
18151 Rewrite (N,
18152 Make_Attribute_Reference (Sloc (N),
18153 Attribute_Name => Name_Range,
18154 Prefix => Relocate_Node (N)));
18156 -- The original was a subtype mark that does not freeze. This
18157 -- means that the rewritten version must not freeze either.
18159 Set_Must_Not_Freeze (N);
18160 Set_Must_Not_Freeze (Prefix (N));
18161 Analyze_And_Resolve (N);
18162 T := Etype (N);
18163 R := N;
18165 -- If expander is inactive, type is legal, nothing else to construct
18167 else
18168 return;
18169 end if;
18170 end if;
18172 if not Is_Discrete_Type (T) then
18173 Error_Msg_N ("discrete type required for range", N);
18174 Set_Etype (N, Any_Type);
18175 return;
18177 elsif T = Any_Type then
18178 Set_Etype (N, Any_Type);
18179 return;
18180 end if;
18182 -- We will now create the appropriate Itype to describe the range, but
18183 -- first a check. If we originally had a subtype, then we just label
18184 -- the range with this subtype. Not only is there no need to construct
18185 -- a new subtype, but it is wrong to do so for two reasons:
18187 -- 1. A legality concern, if we have a subtype, it must not freeze,
18188 -- and the Itype would cause freezing incorrectly
18190 -- 2. An efficiency concern, if we created an Itype, it would not be
18191 -- recognized as the same type for the purposes of eliminating
18192 -- checks in some circumstances.
18194 -- We signal this case by setting the subtype entity in Def_Id
18196 if No (Def_Id) then
18197 Def_Id :=
18198 Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
18199 Set_Etype (Def_Id, Base_Type (T));
18201 if Is_Signed_Integer_Type (T) then
18202 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
18204 elsif Is_Modular_Integer_Type (T) then
18205 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
18207 else
18208 Set_Ekind (Def_Id, E_Enumeration_Subtype);
18209 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
18210 Set_First_Literal (Def_Id, First_Literal (T));
18211 end if;
18213 Set_Size_Info (Def_Id, (T));
18214 Set_RM_Size (Def_Id, RM_Size (T));
18215 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
18217 Set_Scalar_Range (Def_Id, R);
18218 Conditional_Delay (Def_Id, T);
18220 if Nkind (N) = N_Subtype_Indication then
18221 Inherit_Predicate_Flags (Def_Id, Entity (Subtype_Mark (N)));
18222 end if;
18224 -- In the subtype indication case, if the immediate parent of the
18225 -- new subtype is non-static, then the subtype we create is non-
18226 -- static, even if its bounds are static.
18228 if Nkind (N) = N_Subtype_Indication
18229 and then not Is_OK_Static_Subtype (Entity (Subtype_Mark (N)))
18230 then
18231 Set_Is_Non_Static_Subtype (Def_Id);
18232 end if;
18233 end if;
18235 -- Final step is to label the index with this constructed type
18237 Set_Etype (N, Def_Id);
18238 end Make_Index;
18240 ------------------------------
18241 -- Modular_Type_Declaration --
18242 ------------------------------
18244 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
18245 Mod_Expr : constant Node_Id := Expression (Def);
18246 M_Val : Uint;
18248 procedure Set_Modular_Size (Bits : Int);
18249 -- Sets RM_Size to Bits, and Esize to normal word size above this
18251 ----------------------
18252 -- Set_Modular_Size --
18253 ----------------------
18255 procedure Set_Modular_Size (Bits : Int) is
18256 begin
18257 Set_RM_Size (T, UI_From_Int (Bits));
18259 if Bits <= 8 then
18260 Init_Esize (T, 8);
18262 elsif Bits <= 16 then
18263 Init_Esize (T, 16);
18265 elsif Bits <= 32 then
18266 Init_Esize (T, 32);
18268 else
18269 Init_Esize (T, System_Max_Binary_Modulus_Power);
18270 end if;
18272 if not Non_Binary_Modulus (T) and then Esize (T) = RM_Size (T) then
18273 Set_Is_Known_Valid (T);
18274 end if;
18275 end Set_Modular_Size;
18277 -- Start of processing for Modular_Type_Declaration
18279 begin
18280 -- If the mod expression is (exactly) 2 * literal, where literal is
18281 -- 64 or less,then almost certainly the * was meant to be **. Warn.
18283 if Warn_On_Suspicious_Modulus_Value
18284 and then Nkind (Mod_Expr) = N_Op_Multiply
18285 and then Nkind (Left_Opnd (Mod_Expr)) = N_Integer_Literal
18286 and then Intval (Left_Opnd (Mod_Expr)) = Uint_2
18287 and then Nkind (Right_Opnd (Mod_Expr)) = N_Integer_Literal
18288 and then Intval (Right_Opnd (Mod_Expr)) <= Uint_64
18289 then
18290 Error_Msg_N
18291 ("suspicious MOD value, was '*'* intended'??M?", Mod_Expr);
18292 end if;
18294 -- Proceed with analysis of mod expression
18296 Analyze_And_Resolve (Mod_Expr, Any_Integer);
18297 Set_Etype (T, T);
18298 Set_Ekind (T, E_Modular_Integer_Type);
18299 Init_Alignment (T);
18300 Set_Is_Constrained (T);
18302 if not Is_OK_Static_Expression (Mod_Expr) then
18303 Flag_Non_Static_Expr
18304 ("non-static expression used for modular type bound!", Mod_Expr);
18305 M_Val := 2 ** System_Max_Binary_Modulus_Power;
18306 else
18307 M_Val := Expr_Value (Mod_Expr);
18308 end if;
18310 if M_Val < 1 then
18311 Error_Msg_N ("modulus value must be positive", Mod_Expr);
18312 M_Val := 2 ** System_Max_Binary_Modulus_Power;
18313 end if;
18315 if M_Val > 2 ** Standard_Long_Integer_Size then
18316 Check_Restriction (No_Long_Long_Integers, Mod_Expr);
18317 end if;
18319 Set_Modulus (T, M_Val);
18321 -- Create bounds for the modular type based on the modulus given in
18322 -- the type declaration and then analyze and resolve those bounds.
18324 Set_Scalar_Range (T,
18325 Make_Range (Sloc (Mod_Expr),
18326 Low_Bound => Make_Integer_Literal (Sloc (Mod_Expr), 0),
18327 High_Bound => Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
18329 -- Properly analyze the literals for the range. We do this manually
18330 -- because we can't go calling Resolve, since we are resolving these
18331 -- bounds with the type, and this type is certainly not complete yet.
18333 Set_Etype (Low_Bound (Scalar_Range (T)), T);
18334 Set_Etype (High_Bound (Scalar_Range (T)), T);
18335 Set_Is_Static_Expression (Low_Bound (Scalar_Range (T)));
18336 Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
18338 -- Loop through powers of two to find number of bits required
18340 for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
18342 -- Binary case
18344 if M_Val = 2 ** Bits then
18345 Set_Modular_Size (Bits);
18346 return;
18348 -- Non-binary case
18350 elsif M_Val < 2 ** Bits then
18351 Check_SPARK_05_Restriction ("modulus should be a power of 2", T);
18352 Set_Non_Binary_Modulus (T);
18354 if Bits > System_Max_Nonbinary_Modulus_Power then
18355 Error_Msg_Uint_1 :=
18356 UI_From_Int (System_Max_Nonbinary_Modulus_Power);
18357 Error_Msg_F
18358 ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
18359 Set_Modular_Size (System_Max_Binary_Modulus_Power);
18360 return;
18362 else
18363 -- In the non-binary case, set size as per RM 13.3(55)
18365 Set_Modular_Size (Bits);
18366 return;
18367 end if;
18368 end if;
18370 end loop;
18372 -- If we fall through, then the size exceed System.Max_Binary_Modulus
18373 -- so we just signal an error and set the maximum size.
18375 Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
18376 Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
18378 Set_Modular_Size (System_Max_Binary_Modulus_Power);
18379 Init_Alignment (T);
18381 end Modular_Type_Declaration;
18383 --------------------------
18384 -- New_Concatenation_Op --
18385 --------------------------
18387 procedure New_Concatenation_Op (Typ : Entity_Id) is
18388 Loc : constant Source_Ptr := Sloc (Typ);
18389 Op : Entity_Id;
18391 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
18392 -- Create abbreviated declaration for the formal of a predefined
18393 -- Operator 'Op' of type 'Typ'
18395 --------------------
18396 -- Make_Op_Formal --
18397 --------------------
18399 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
18400 Formal : Entity_Id;
18401 begin
18402 Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
18403 Set_Etype (Formal, Typ);
18404 Set_Mechanism (Formal, Default_Mechanism);
18405 return Formal;
18406 end Make_Op_Formal;
18408 -- Start of processing for New_Concatenation_Op
18410 begin
18411 Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
18413 Set_Ekind (Op, E_Operator);
18414 Set_Scope (Op, Current_Scope);
18415 Set_Etype (Op, Typ);
18416 Set_Homonym (Op, Get_Name_Entity_Id (Name_Op_Concat));
18417 Set_Is_Immediately_Visible (Op);
18418 Set_Is_Intrinsic_Subprogram (Op);
18419 Set_Has_Completion (Op);
18420 Append_Entity (Op, Current_Scope);
18422 Set_Name_Entity_Id (Name_Op_Concat, Op);
18424 Append_Entity (Make_Op_Formal (Typ, Op), Op);
18425 Append_Entity (Make_Op_Formal (Typ, Op), Op);
18426 end New_Concatenation_Op;
18428 -------------------------
18429 -- OK_For_Limited_Init --
18430 -------------------------
18432 -- ???Check all calls of this, and compare the conditions under which it's
18433 -- called.
18435 function OK_For_Limited_Init
18436 (Typ : Entity_Id;
18437 Exp : Node_Id) return Boolean
18439 begin
18440 return Is_CPP_Constructor_Call (Exp)
18441 or else (Ada_Version >= Ada_2005
18442 and then not Debug_Flag_Dot_L
18443 and then OK_For_Limited_Init_In_05 (Typ, Exp));
18444 end OK_For_Limited_Init;
18446 -------------------------------
18447 -- OK_For_Limited_Init_In_05 --
18448 -------------------------------
18450 function OK_For_Limited_Init_In_05
18451 (Typ : Entity_Id;
18452 Exp : Node_Id) return Boolean
18454 begin
18455 -- An object of a limited interface type can be initialized with any
18456 -- expression of a nonlimited descendant type.
18458 if Is_Class_Wide_Type (Typ)
18459 and then Is_Limited_Interface (Typ)
18460 and then not Is_Limited_Type (Etype (Exp))
18461 then
18462 return True;
18463 end if;
18465 -- Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
18466 -- case of limited aggregates (including extension aggregates), and
18467 -- function calls. The function call may have been given in prefixed
18468 -- notation, in which case the original node is an indexed component.
18469 -- If the function is parameterless, the original node was an explicit
18470 -- dereference. The function may also be parameterless, in which case
18471 -- the source node is just an identifier.
18473 case Nkind (Original_Node (Exp)) is
18474 when N_Aggregate | N_Extension_Aggregate | N_Function_Call | N_Op =>
18475 return True;
18477 when N_Identifier =>
18478 return Present (Entity (Original_Node (Exp)))
18479 and then Ekind (Entity (Original_Node (Exp))) = E_Function;
18481 when N_Qualified_Expression =>
18482 return
18483 OK_For_Limited_Init_In_05
18484 (Typ, Expression (Original_Node (Exp)));
18486 -- Ada 2005 (AI-251): If a class-wide interface object is initialized
18487 -- with a function call, the expander has rewritten the call into an
18488 -- N_Type_Conversion node to force displacement of the pointer to
18489 -- reference the component containing the secondary dispatch table.
18490 -- Otherwise a type conversion is not a legal context.
18491 -- A return statement for a build-in-place function returning a
18492 -- synchronized type also introduces an unchecked conversion.
18494 when N_Type_Conversion |
18495 N_Unchecked_Type_Conversion =>
18496 return not Comes_From_Source (Exp)
18497 and then
18498 OK_For_Limited_Init_In_05
18499 (Typ, Expression (Original_Node (Exp)));
18501 when N_Indexed_Component |
18502 N_Selected_Component |
18503 N_Explicit_Dereference =>
18504 return Nkind (Exp) = N_Function_Call;
18506 -- A use of 'Input is a function call, hence allowed. Normally the
18507 -- attribute will be changed to a call, but the attribute by itself
18508 -- can occur with -gnatc.
18510 when N_Attribute_Reference =>
18511 return Attribute_Name (Original_Node (Exp)) = Name_Input;
18513 -- For a case expression, all dependent expressions must be legal
18515 when N_Case_Expression =>
18516 declare
18517 Alt : Node_Id;
18519 begin
18520 Alt := First (Alternatives (Original_Node (Exp)));
18521 while Present (Alt) loop
18522 if not OK_For_Limited_Init_In_05 (Typ, Expression (Alt)) then
18523 return False;
18524 end if;
18526 Next (Alt);
18527 end loop;
18529 return True;
18530 end;
18532 -- For an if expression, all dependent expressions must be legal
18534 when N_If_Expression =>
18535 declare
18536 Then_Expr : constant Node_Id :=
18537 Next (First (Expressions (Original_Node (Exp))));
18538 Else_Expr : constant Node_Id := Next (Then_Expr);
18539 begin
18540 return OK_For_Limited_Init_In_05 (Typ, Then_Expr)
18541 and then
18542 OK_For_Limited_Init_In_05 (Typ, Else_Expr);
18543 end;
18545 when others =>
18546 return False;
18547 end case;
18548 end OK_For_Limited_Init_In_05;
18550 -------------------------------------------
18551 -- Ordinary_Fixed_Point_Type_Declaration --
18552 -------------------------------------------
18554 procedure Ordinary_Fixed_Point_Type_Declaration
18555 (T : Entity_Id;
18556 Def : Node_Id)
18558 Loc : constant Source_Ptr := Sloc (Def);
18559 Delta_Expr : constant Node_Id := Delta_Expression (Def);
18560 RRS : constant Node_Id := Real_Range_Specification (Def);
18561 Implicit_Base : Entity_Id;
18562 Delta_Val : Ureal;
18563 Small_Val : Ureal;
18564 Low_Val : Ureal;
18565 High_Val : Ureal;
18567 begin
18568 Check_Restriction (No_Fixed_Point, Def);
18570 -- Create implicit base type
18572 Implicit_Base :=
18573 Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
18574 Set_Etype (Implicit_Base, Implicit_Base);
18576 -- Analyze and process delta expression
18578 Analyze_And_Resolve (Delta_Expr, Any_Real);
18580 Check_Delta_Expression (Delta_Expr);
18581 Delta_Val := Expr_Value_R (Delta_Expr);
18583 Set_Delta_Value (Implicit_Base, Delta_Val);
18585 -- Compute default small from given delta, which is the largest power
18586 -- of two that does not exceed the given delta value.
18588 declare
18589 Tmp : Ureal;
18590 Scale : Int;
18592 begin
18593 Tmp := Ureal_1;
18594 Scale := 0;
18596 if Delta_Val < Ureal_1 then
18597 while Delta_Val < Tmp loop
18598 Tmp := Tmp / Ureal_2;
18599 Scale := Scale + 1;
18600 end loop;
18602 else
18603 loop
18604 Tmp := Tmp * Ureal_2;
18605 exit when Tmp > Delta_Val;
18606 Scale := Scale - 1;
18607 end loop;
18608 end if;
18610 Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
18611 end;
18613 Set_Small_Value (Implicit_Base, Small_Val);
18615 -- If no range was given, set a dummy range
18617 if RRS <= Empty_Or_Error then
18618 Low_Val := -Small_Val;
18619 High_Val := Small_Val;
18621 -- Otherwise analyze and process given range
18623 else
18624 declare
18625 Low : constant Node_Id := Low_Bound (RRS);
18626 High : constant Node_Id := High_Bound (RRS);
18628 begin
18629 Analyze_And_Resolve (Low, Any_Real);
18630 Analyze_And_Resolve (High, Any_Real);
18631 Check_Real_Bound (Low);
18632 Check_Real_Bound (High);
18634 -- Obtain and set the range
18636 Low_Val := Expr_Value_R (Low);
18637 High_Val := Expr_Value_R (High);
18639 if Low_Val > High_Val then
18640 Error_Msg_NE ("??fixed point type& has null range", Def, T);
18641 end if;
18642 end;
18643 end if;
18645 -- The range for both the implicit base and the declared first subtype
18646 -- cannot be set yet, so we use the special routine Set_Fixed_Range to
18647 -- set a temporary range in place. Note that the bounds of the base
18648 -- type will be widened to be symmetrical and to fill the available
18649 -- bits when the type is frozen.
18651 -- We could do this with all discrete types, and probably should, but
18652 -- we absolutely have to do it for fixed-point, since the end-points
18653 -- of the range and the size are determined by the small value, which
18654 -- could be reset before the freeze point.
18656 Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
18657 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
18659 -- Complete definition of first subtype. The inheritance of the rep item
18660 -- chain ensures that SPARK-related pragmas are not clobbered when the
18661 -- ordinary fixed point type acts as a full view of a private type.
18663 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
18664 Set_Etype (T, Implicit_Base);
18665 Init_Size_Align (T);
18666 Inherit_Rep_Item_Chain (T, Implicit_Base);
18667 Set_Small_Value (T, Small_Val);
18668 Set_Delta_Value (T, Delta_Val);
18669 Set_Is_Constrained (T);
18670 end Ordinary_Fixed_Point_Type_Declaration;
18672 ----------------------------------
18673 -- Preanalyze_Assert_Expression --
18674 ----------------------------------
18676 procedure Preanalyze_Assert_Expression (N : Node_Id; T : Entity_Id) is
18677 begin
18678 In_Assertion_Expr := In_Assertion_Expr + 1;
18679 Preanalyze_Spec_Expression (N, T);
18680 In_Assertion_Expr := In_Assertion_Expr - 1;
18681 end Preanalyze_Assert_Expression;
18683 -----------------------------------
18684 -- Preanalyze_Default_Expression --
18685 -----------------------------------
18687 procedure Preanalyze_Default_Expression (N : Node_Id; T : Entity_Id) is
18688 Save_In_Default_Expr : constant Boolean := In_Default_Expr;
18689 begin
18690 In_Default_Expr := True;
18691 Preanalyze_Spec_Expression (N, T);
18692 In_Default_Expr := Save_In_Default_Expr;
18693 end Preanalyze_Default_Expression;
18695 --------------------------------
18696 -- Preanalyze_Spec_Expression --
18697 --------------------------------
18699 procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
18700 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
18701 begin
18702 In_Spec_Expression := True;
18703 Preanalyze_And_Resolve (N, T);
18704 In_Spec_Expression := Save_In_Spec_Expression;
18705 end Preanalyze_Spec_Expression;
18707 ----------------------------------------
18708 -- Prepare_Private_Subtype_Completion --
18709 ----------------------------------------
18711 procedure Prepare_Private_Subtype_Completion
18712 (Id : Entity_Id;
18713 Related_Nod : Node_Id)
18715 Id_B : constant Entity_Id := Base_Type (Id);
18716 Full_B : Entity_Id := Full_View (Id_B);
18717 Full : Entity_Id;
18719 begin
18720 if Present (Full_B) then
18722 -- Get to the underlying full view if necessary
18724 if Is_Private_Type (Full_B)
18725 and then Present (Underlying_Full_View (Full_B))
18726 then
18727 Full_B := Underlying_Full_View (Full_B);
18728 end if;
18730 -- The Base_Type is already completed, we can complete the subtype
18731 -- now. We have to create a new entity with the same name, Thus we
18732 -- can't use Create_Itype.
18734 Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
18735 Set_Is_Itype (Full);
18736 Set_Associated_Node_For_Itype (Full, Related_Nod);
18737 Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
18738 end if;
18740 -- The parent subtype may be private, but the base might not, in some
18741 -- nested instances. In that case, the subtype does not need to be
18742 -- exchanged. It would still be nice to make private subtypes and their
18743 -- bases consistent at all times ???
18745 if Is_Private_Type (Id_B) then
18746 Append_Elmt (Id, Private_Dependents (Id_B));
18747 end if;
18748 end Prepare_Private_Subtype_Completion;
18750 ---------------------------
18751 -- Process_Discriminants --
18752 ---------------------------
18754 procedure Process_Discriminants
18755 (N : Node_Id;
18756 Prev : Entity_Id := Empty)
18758 Elist : constant Elist_Id := New_Elmt_List;
18759 Id : Node_Id;
18760 Discr : Node_Id;
18761 Discr_Number : Uint;
18762 Discr_Type : Entity_Id;
18763 Default_Present : Boolean := False;
18764 Default_Not_Present : Boolean := False;
18766 begin
18767 -- A composite type other than an array type can have discriminants.
18768 -- On entry, the current scope is the composite type.
18770 -- The discriminants are initially entered into the scope of the type
18771 -- via Enter_Name with the default Ekind of E_Void to prevent premature
18772 -- use, as explained at the end of this procedure.
18774 Discr := First (Discriminant_Specifications (N));
18775 while Present (Discr) loop
18776 Enter_Name (Defining_Identifier (Discr));
18778 -- For navigation purposes we add a reference to the discriminant
18779 -- in the entity for the type. If the current declaration is a
18780 -- completion, place references on the partial view. Otherwise the
18781 -- type is the current scope.
18783 if Present (Prev) then
18785 -- The references go on the partial view, if present. If the
18786 -- partial view has discriminants, the references have been
18787 -- generated already.
18789 if not Has_Discriminants (Prev) then
18790 Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
18791 end if;
18792 else
18793 Generate_Reference
18794 (Current_Scope, Defining_Identifier (Discr), 'd');
18795 end if;
18797 if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
18798 Discr_Type := Access_Definition (Discr, Discriminant_Type (Discr));
18800 -- Ada 2005 (AI-254)
18802 if Present (Access_To_Subprogram_Definition
18803 (Discriminant_Type (Discr)))
18804 and then Protected_Present (Access_To_Subprogram_Definition
18805 (Discriminant_Type (Discr)))
18806 then
18807 Discr_Type :=
18808 Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
18809 end if;
18811 else
18812 Find_Type (Discriminant_Type (Discr));
18813 Discr_Type := Etype (Discriminant_Type (Discr));
18815 if Error_Posted (Discriminant_Type (Discr)) then
18816 Discr_Type := Any_Type;
18817 end if;
18818 end if;
18820 -- Handling of discriminants that are access types
18822 if Is_Access_Type (Discr_Type) then
18824 -- Ada 2005 (AI-230): Access discriminant allowed in non-
18825 -- limited record types
18827 if Ada_Version < Ada_2005 then
18828 Check_Access_Discriminant_Requires_Limited
18829 (Discr, Discriminant_Type (Discr));
18830 end if;
18832 if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
18833 Error_Msg_N
18834 ("(Ada 83) access discriminant not allowed", Discr);
18835 end if;
18837 -- If not access type, must be a discrete type
18839 elsif not Is_Discrete_Type (Discr_Type) then
18840 Error_Msg_N
18841 ("discriminants must have a discrete or access type",
18842 Discriminant_Type (Discr));
18843 end if;
18845 Set_Etype (Defining_Identifier (Discr), Discr_Type);
18847 -- If a discriminant specification includes the assignment compound
18848 -- delimiter followed by an expression, the expression is the default
18849 -- expression of the discriminant; the default expression must be of
18850 -- the type of the discriminant. (RM 3.7.1) Since this expression is
18851 -- a default expression, we do the special preanalysis, since this
18852 -- expression does not freeze (see section "Handling of Default and
18853 -- Per-Object Expressions" in spec of package Sem).
18855 if Present (Expression (Discr)) then
18856 Preanalyze_Spec_Expression (Expression (Discr), Discr_Type);
18858 -- Legaity checks
18860 if Nkind (N) = N_Formal_Type_Declaration then
18861 Error_Msg_N
18862 ("discriminant defaults not allowed for formal type",
18863 Expression (Discr));
18865 -- Flag an error for a tagged type with defaulted discriminants,
18866 -- excluding limited tagged types when compiling for Ada 2012
18867 -- (see AI05-0214).
18869 elsif Is_Tagged_Type (Current_Scope)
18870 and then (not Is_Limited_Type (Current_Scope)
18871 or else Ada_Version < Ada_2012)
18872 and then Comes_From_Source (N)
18873 then
18874 -- Note: see similar test in Check_Or_Process_Discriminants, to
18875 -- handle the (illegal) case of the completion of an untagged
18876 -- view with discriminants with defaults by a tagged full view.
18877 -- We skip the check if Discr does not come from source, to
18878 -- account for the case of an untagged derived type providing
18879 -- defaults for a renamed discriminant from a private untagged
18880 -- ancestor with a tagged full view (ACATS B460006).
18882 if Ada_Version >= Ada_2012 then
18883 Error_Msg_N
18884 ("discriminants of nonlimited tagged type cannot have"
18885 & " defaults",
18886 Expression (Discr));
18887 else
18888 Error_Msg_N
18889 ("discriminants of tagged type cannot have defaults",
18890 Expression (Discr));
18891 end if;
18893 else
18894 Default_Present := True;
18895 Append_Elmt (Expression (Discr), Elist);
18897 -- Tag the defining identifiers for the discriminants with
18898 -- their corresponding default expressions from the tree.
18900 Set_Discriminant_Default_Value
18901 (Defining_Identifier (Discr), Expression (Discr));
18902 end if;
18904 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag
18905 -- gets set unless we can be sure that no range check is required.
18907 if (GNATprove_Mode or not Expander_Active)
18908 and then not
18909 Is_In_Range
18910 (Expression (Discr), Discr_Type, Assume_Valid => True)
18911 then
18912 Set_Do_Range_Check (Expression (Discr));
18913 end if;
18915 -- No default discriminant value given
18917 else
18918 Default_Not_Present := True;
18919 end if;
18921 -- Ada 2005 (AI-231): Create an Itype that is a duplicate of
18922 -- Discr_Type but with the null-exclusion attribute
18924 if Ada_Version >= Ada_2005 then
18926 -- Ada 2005 (AI-231): Static checks
18928 if Can_Never_Be_Null (Discr_Type) then
18929 Null_Exclusion_Static_Checks (Discr);
18931 elsif Is_Access_Type (Discr_Type)
18932 and then Null_Exclusion_Present (Discr)
18934 -- No need to check itypes because in their case this check
18935 -- was done at their point of creation
18937 and then not Is_Itype (Discr_Type)
18938 then
18939 if Can_Never_Be_Null (Discr_Type) then
18940 Error_Msg_NE
18941 ("`NOT NULL` not allowed (& already excludes null)",
18942 Discr,
18943 Discr_Type);
18944 end if;
18946 Set_Etype (Defining_Identifier (Discr),
18947 Create_Null_Excluding_Itype
18948 (T => Discr_Type,
18949 Related_Nod => Discr));
18951 -- Check for improper null exclusion if the type is otherwise
18952 -- legal for a discriminant.
18954 elsif Null_Exclusion_Present (Discr)
18955 and then Is_Discrete_Type (Discr_Type)
18956 then
18957 Error_Msg_N
18958 ("null exclusion can only apply to an access type", Discr);
18959 end if;
18961 -- Ada 2005 (AI-402): access discriminants of nonlimited types
18962 -- can't have defaults. Synchronized types, or types that are
18963 -- explicitly limited are fine, but special tests apply to derived
18964 -- types in generics: in a generic body we have to assume the
18965 -- worst, and therefore defaults are not allowed if the parent is
18966 -- a generic formal private type (see ACATS B370001).
18968 if Is_Access_Type (Discr_Type) and then Default_Present then
18969 if Ekind (Discr_Type) /= E_Anonymous_Access_Type
18970 or else Is_Limited_Record (Current_Scope)
18971 or else Is_Concurrent_Type (Current_Scope)
18972 or else Is_Concurrent_Record_Type (Current_Scope)
18973 or else Ekind (Current_Scope) = E_Limited_Private_Type
18974 then
18975 if not Is_Derived_Type (Current_Scope)
18976 or else not Is_Generic_Type (Etype (Current_Scope))
18977 or else not In_Package_Body (Scope (Etype (Current_Scope)))
18978 or else Limited_Present
18979 (Type_Definition (Parent (Current_Scope)))
18980 then
18981 null;
18983 else
18984 Error_Msg_N
18985 ("access discriminants of nonlimited types cannot "
18986 & "have defaults", Expression (Discr));
18987 end if;
18989 elsif Present (Expression (Discr)) then
18990 Error_Msg_N
18991 ("(Ada 2005) access discriminants of nonlimited types "
18992 & "cannot have defaults", Expression (Discr));
18993 end if;
18994 end if;
18995 end if;
18997 -- A discriminant cannot be effectively volatile. This check is only
18998 -- relevant when SPARK_Mode is on as it is not standard Ada legality
18999 -- rule (SPARK RM 7.1.3(6)).
19001 if SPARK_Mode = On
19002 and then Is_Effectively_Volatile (Defining_Identifier (Discr))
19003 then
19004 Error_Msg_N ("discriminant cannot be volatile", Discr);
19005 end if;
19007 Next (Discr);
19008 end loop;
19010 -- An element list consisting of the default expressions of the
19011 -- discriminants is constructed in the above loop and used to set
19012 -- the Discriminant_Constraint attribute for the type. If an object
19013 -- is declared of this (record or task) type without any explicit
19014 -- discriminant constraint given, this element list will form the
19015 -- actual parameters for the corresponding initialization procedure
19016 -- for the type.
19018 Set_Discriminant_Constraint (Current_Scope, Elist);
19019 Set_Stored_Constraint (Current_Scope, No_Elist);
19021 -- Default expressions must be provided either for all or for none
19022 -- of the discriminants of a discriminant part. (RM 3.7.1)
19024 if Default_Present and then Default_Not_Present then
19025 Error_Msg_N
19026 ("incomplete specification of defaults for discriminants", N);
19027 end if;
19029 -- The use of the name of a discriminant is not allowed in default
19030 -- expressions of a discriminant part if the specification of the
19031 -- discriminant is itself given in the discriminant part. (RM 3.7.1)
19033 -- To detect this, the discriminant names are entered initially with an
19034 -- Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
19035 -- attempt to use a void entity (for example in an expression that is
19036 -- type-checked) produces the error message: premature usage. Now after
19037 -- completing the semantic analysis of the discriminant part, we can set
19038 -- the Ekind of all the discriminants appropriately.
19040 Discr := First (Discriminant_Specifications (N));
19041 Discr_Number := Uint_1;
19042 while Present (Discr) loop
19043 Id := Defining_Identifier (Discr);
19044 Set_Ekind (Id, E_Discriminant);
19045 Init_Component_Location (Id);
19046 Init_Esize (Id);
19047 Set_Discriminant_Number (Id, Discr_Number);
19049 -- Make sure this is always set, even in illegal programs
19051 Set_Corresponding_Discriminant (Id, Empty);
19053 -- Initialize the Original_Record_Component to the entity itself.
19054 -- Inherit_Components will propagate the right value to
19055 -- discriminants in derived record types.
19057 Set_Original_Record_Component (Id, Id);
19059 -- Create the discriminal for the discriminant
19061 Build_Discriminal (Id);
19063 Next (Discr);
19064 Discr_Number := Discr_Number + 1;
19065 end loop;
19067 Set_Has_Discriminants (Current_Scope);
19068 end Process_Discriminants;
19070 -----------------------
19071 -- Process_Full_View --
19072 -----------------------
19074 procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
19075 procedure Collect_Implemented_Interfaces
19076 (Typ : Entity_Id;
19077 Ifaces : Elist_Id);
19078 -- Ada 2005: Gather all the interfaces that Typ directly or
19079 -- inherently implements. Duplicate entries are not added to
19080 -- the list Ifaces.
19082 ------------------------------------
19083 -- Collect_Implemented_Interfaces --
19084 ------------------------------------
19086 procedure Collect_Implemented_Interfaces
19087 (Typ : Entity_Id;
19088 Ifaces : Elist_Id)
19090 Iface : Entity_Id;
19091 Iface_Elmt : Elmt_Id;
19093 begin
19094 -- Abstract interfaces are only associated with tagged record types
19096 if not Is_Tagged_Type (Typ) or else not Is_Record_Type (Typ) then
19097 return;
19098 end if;
19100 -- Recursively climb to the ancestors
19102 if Etype (Typ) /= Typ
19104 -- Protect the frontend against wrong cyclic declarations like:
19106 -- type B is new A with private;
19107 -- type C is new A with private;
19108 -- private
19109 -- type B is new C with null record;
19110 -- type C is new B with null record;
19112 and then Etype (Typ) /= Priv_T
19113 and then Etype (Typ) /= Full_T
19114 then
19115 -- Keep separate the management of private type declarations
19117 if Ekind (Typ) = E_Record_Type_With_Private then
19119 -- Handle the following illegal usage:
19120 -- type Private_Type is tagged private;
19121 -- private
19122 -- type Private_Type is new Type_Implementing_Iface;
19124 if Present (Full_View (Typ))
19125 and then Etype (Typ) /= Full_View (Typ)
19126 then
19127 if Is_Interface (Etype (Typ)) then
19128 Append_Unique_Elmt (Etype (Typ), Ifaces);
19129 end if;
19131 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
19132 end if;
19134 -- Non-private types
19136 else
19137 if Is_Interface (Etype (Typ)) then
19138 Append_Unique_Elmt (Etype (Typ), Ifaces);
19139 end if;
19141 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
19142 end if;
19143 end if;
19145 -- Handle entities in the list of abstract interfaces
19147 if Present (Interfaces (Typ)) then
19148 Iface_Elmt := First_Elmt (Interfaces (Typ));
19149 while Present (Iface_Elmt) loop
19150 Iface := Node (Iface_Elmt);
19152 pragma Assert (Is_Interface (Iface));
19154 if not Contain_Interface (Iface, Ifaces) then
19155 Append_Elmt (Iface, Ifaces);
19156 Collect_Implemented_Interfaces (Iface, Ifaces);
19157 end if;
19159 Next_Elmt (Iface_Elmt);
19160 end loop;
19161 end if;
19162 end Collect_Implemented_Interfaces;
19164 -- Local variables
19166 Full_Indic : Node_Id;
19167 Full_Parent : Entity_Id;
19168 Priv_Parent : Entity_Id;
19170 -- Start of processing for Process_Full_View
19172 begin
19173 -- First some sanity checks that must be done after semantic
19174 -- decoration of the full view and thus cannot be placed with other
19175 -- similar checks in Find_Type_Name
19177 if not Is_Limited_Type (Priv_T)
19178 and then (Is_Limited_Type (Full_T)
19179 or else Is_Limited_Composite (Full_T))
19180 then
19181 if In_Instance then
19182 null;
19183 else
19184 Error_Msg_N
19185 ("completion of nonlimited type cannot be limited", Full_T);
19186 Explain_Limited_Type (Full_T, Full_T);
19187 end if;
19189 elsif Is_Abstract_Type (Full_T)
19190 and then not Is_Abstract_Type (Priv_T)
19191 then
19192 Error_Msg_N
19193 ("completion of nonabstract type cannot be abstract", Full_T);
19195 elsif Is_Tagged_Type (Priv_T)
19196 and then Is_Limited_Type (Priv_T)
19197 and then not Is_Limited_Type (Full_T)
19198 then
19199 -- If pragma CPP_Class was applied to the private declaration
19200 -- propagate the limitedness to the full-view
19202 if Is_CPP_Class (Priv_T) then
19203 Set_Is_Limited_Record (Full_T);
19205 -- GNAT allow its own definition of Limited_Controlled to disobey
19206 -- this rule in order in ease the implementation. This test is safe
19207 -- because Root_Controlled is defined in a child of System that
19208 -- normal programs are not supposed to use.
19210 elsif Is_RTE (Etype (Full_T), RE_Root_Controlled) then
19211 Set_Is_Limited_Composite (Full_T);
19212 else
19213 Error_Msg_N
19214 ("completion of limited tagged type must be limited", Full_T);
19215 end if;
19217 elsif Is_Generic_Type (Priv_T) then
19218 Error_Msg_N ("generic type cannot have a completion", Full_T);
19219 end if;
19221 -- Check that ancestor interfaces of private and full views are
19222 -- consistent. We omit this check for synchronized types because
19223 -- they are performed on the corresponding record type when frozen.
19225 if Ada_Version >= Ada_2005
19226 and then Is_Tagged_Type (Priv_T)
19227 and then Is_Tagged_Type (Full_T)
19228 and then not Is_Concurrent_Type (Full_T)
19229 then
19230 declare
19231 Iface : Entity_Id;
19232 Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
19233 Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
19235 begin
19236 Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
19237 Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
19239 -- Ada 2005 (AI-251): The partial view shall be a descendant of
19240 -- an interface type if and only if the full type is descendant
19241 -- of the interface type (AARM 7.3 (7.3/2)).
19243 Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
19245 if Present (Iface) then
19246 Error_Msg_NE
19247 ("interface in partial view& not implemented by full type "
19248 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
19249 end if;
19251 Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
19253 if Present (Iface) then
19254 Error_Msg_NE
19255 ("interface & not implemented by partial view "
19256 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
19257 end if;
19258 end;
19259 end if;
19261 if Is_Tagged_Type (Priv_T)
19262 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
19263 and then Is_Derived_Type (Full_T)
19264 then
19265 Priv_Parent := Etype (Priv_T);
19267 -- The full view of a private extension may have been transformed
19268 -- into an unconstrained derived type declaration and a subtype
19269 -- declaration (see build_derived_record_type for details).
19271 if Nkind (N) = N_Subtype_Declaration then
19272 Full_Indic := Subtype_Indication (N);
19273 Full_Parent := Etype (Base_Type (Full_T));
19274 else
19275 Full_Indic := Subtype_Indication (Type_Definition (N));
19276 Full_Parent := Etype (Full_T);
19277 end if;
19279 -- Check that the parent type of the full type is a descendant of
19280 -- the ancestor subtype given in the private extension. If either
19281 -- entity has an Etype equal to Any_Type then we had some previous
19282 -- error situation [7.3(8)].
19284 if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
19285 return;
19287 -- Ada 2005 (AI-251): Interfaces in the full type can be given in
19288 -- any order. Therefore we don't have to check that its parent must
19289 -- be a descendant of the parent of the private type declaration.
19291 elsif Is_Interface (Priv_Parent)
19292 and then Is_Interface (Full_Parent)
19293 then
19294 null;
19296 -- Ada 2005 (AI-251): If the parent of the private type declaration
19297 -- is an interface there is no need to check that it is an ancestor
19298 -- of the associated full type declaration. The required tests for
19299 -- this case are performed by Build_Derived_Record_Type.
19301 elsif not Is_Interface (Base_Type (Priv_Parent))
19302 and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
19303 then
19304 Error_Msg_N
19305 ("parent of full type must descend from parent"
19306 & " of private extension", Full_Indic);
19308 -- First check a formal restriction, and then proceed with checking
19309 -- Ada rules. Since the formal restriction is not a serious error, we
19310 -- don't prevent further error detection for this check, hence the
19311 -- ELSE.
19313 else
19314 -- In formal mode, when completing a private extension the type
19315 -- named in the private part must be exactly the same as that
19316 -- named in the visible part.
19318 if Priv_Parent /= Full_Parent then
19319 Error_Msg_Name_1 := Chars (Priv_Parent);
19320 Check_SPARK_05_Restriction ("% expected", Full_Indic);
19321 end if;
19323 -- Check the rules of 7.3(10): if the private extension inherits
19324 -- known discriminants, then the full type must also inherit those
19325 -- discriminants from the same (ancestor) type, and the parent
19326 -- subtype of the full type must be constrained if and only if
19327 -- the ancestor subtype of the private extension is constrained.
19329 if No (Discriminant_Specifications (Parent (Priv_T)))
19330 and then not Has_Unknown_Discriminants (Priv_T)
19331 and then Has_Discriminants (Base_Type (Priv_Parent))
19332 then
19333 declare
19334 Priv_Indic : constant Node_Id :=
19335 Subtype_Indication (Parent (Priv_T));
19337 Priv_Constr : constant Boolean :=
19338 Is_Constrained (Priv_Parent)
19339 or else
19340 Nkind (Priv_Indic) = N_Subtype_Indication
19341 or else
19342 Is_Constrained (Entity (Priv_Indic));
19344 Full_Constr : constant Boolean :=
19345 Is_Constrained (Full_Parent)
19346 or else
19347 Nkind (Full_Indic) = N_Subtype_Indication
19348 or else
19349 Is_Constrained (Entity (Full_Indic));
19351 Priv_Discr : Entity_Id;
19352 Full_Discr : Entity_Id;
19354 begin
19355 Priv_Discr := First_Discriminant (Priv_Parent);
19356 Full_Discr := First_Discriminant (Full_Parent);
19357 while Present (Priv_Discr) and then Present (Full_Discr) loop
19358 if Original_Record_Component (Priv_Discr) =
19359 Original_Record_Component (Full_Discr)
19360 or else
19361 Corresponding_Discriminant (Priv_Discr) =
19362 Corresponding_Discriminant (Full_Discr)
19363 then
19364 null;
19365 else
19366 exit;
19367 end if;
19369 Next_Discriminant (Priv_Discr);
19370 Next_Discriminant (Full_Discr);
19371 end loop;
19373 if Present (Priv_Discr) or else Present (Full_Discr) then
19374 Error_Msg_N
19375 ("full view must inherit discriminants of the parent"
19376 & " type used in the private extension", Full_Indic);
19378 elsif Priv_Constr and then not Full_Constr then
19379 Error_Msg_N
19380 ("parent subtype of full type must be constrained",
19381 Full_Indic);
19383 elsif Full_Constr and then not Priv_Constr then
19384 Error_Msg_N
19385 ("parent subtype of full type must be unconstrained",
19386 Full_Indic);
19387 end if;
19388 end;
19390 -- Check the rules of 7.3(12): if a partial view has neither
19391 -- known or unknown discriminants, then the full type
19392 -- declaration shall define a definite subtype.
19394 elsif not Has_Unknown_Discriminants (Priv_T)
19395 and then not Has_Discriminants (Priv_T)
19396 and then not Is_Constrained (Full_T)
19397 then
19398 Error_Msg_N
19399 ("full view must define a constrained type if partial view"
19400 & " has no discriminants", Full_T);
19401 end if;
19403 -- ??????? Do we implement the following properly ?????
19404 -- If the ancestor subtype of a private extension has constrained
19405 -- discriminants, then the parent subtype of the full view shall
19406 -- impose a statically matching constraint on those discriminants
19407 -- [7.3(13)].
19408 end if;
19410 else
19411 -- For untagged types, verify that a type without discriminants is
19412 -- not completed with an unconstrained type. A separate error message
19413 -- is produced if the full type has defaulted discriminants.
19415 if not Is_Indefinite_Subtype (Priv_T)
19416 and then Is_Indefinite_Subtype (Full_T)
19417 then
19418 Error_Msg_Sloc := Sloc (Parent (Priv_T));
19419 Error_Msg_NE
19420 ("full view of& not compatible with declaration#",
19421 Full_T, Priv_T);
19423 if not Is_Tagged_Type (Full_T) then
19424 Error_Msg_N
19425 ("\one is constrained, the other unconstrained", Full_T);
19426 end if;
19427 end if;
19428 end if;
19430 -- AI-419: verify that the use of "limited" is consistent
19432 declare
19433 Orig_Decl : constant Node_Id := Original_Node (N);
19435 begin
19436 if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
19437 and then Nkind (Orig_Decl) = N_Full_Type_Declaration
19438 and then Nkind
19439 (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
19440 then
19441 if not Limited_Present (Parent (Priv_T))
19442 and then not Synchronized_Present (Parent (Priv_T))
19443 and then Limited_Present (Type_Definition (Orig_Decl))
19444 then
19445 Error_Msg_N
19446 ("full view of non-limited extension cannot be limited", N);
19448 -- Conversely, if the partial view carries the limited keyword,
19449 -- the full view must as well, even if it may be redundant.
19451 elsif Limited_Present (Parent (Priv_T))
19452 and then not Limited_Present (Type_Definition (Orig_Decl))
19453 then
19454 Error_Msg_N
19455 ("full view of limited extension must be explicitly limited",
19457 end if;
19458 end if;
19459 end;
19461 -- Ada 2005 (AI-443): A synchronized private extension must be
19462 -- completed by a task or protected type.
19464 if Ada_Version >= Ada_2005
19465 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
19466 and then Synchronized_Present (Parent (Priv_T))
19467 and then not Is_Concurrent_Type (Full_T)
19468 then
19469 Error_Msg_N ("full view of synchronized extension must " &
19470 "be synchronized type", N);
19471 end if;
19473 -- Ada 2005 AI-363: if the full view has discriminants with
19474 -- defaults, it is illegal to declare constrained access subtypes
19475 -- whose designated type is the current type. This allows objects
19476 -- of the type that are declared in the heap to be unconstrained.
19478 if not Has_Unknown_Discriminants (Priv_T)
19479 and then not Has_Discriminants (Priv_T)
19480 and then Has_Discriminants (Full_T)
19481 and then
19482 Present (Discriminant_Default_Value (First_Discriminant (Full_T)))
19483 then
19484 Set_Has_Constrained_Partial_View (Full_T);
19485 Set_Has_Constrained_Partial_View (Priv_T);
19486 end if;
19488 -- Create a full declaration for all its subtypes recorded in
19489 -- Private_Dependents and swap them similarly to the base type. These
19490 -- are subtypes that have been define before the full declaration of
19491 -- the private type. We also swap the entry in Private_Dependents list
19492 -- so we can properly restore the private view on exit from the scope.
19494 declare
19495 Priv_Elmt : Elmt_Id;
19496 Priv_Scop : Entity_Id;
19497 Priv : Entity_Id;
19498 Full : Entity_Id;
19500 begin
19501 Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
19502 while Present (Priv_Elmt) loop
19503 Priv := Node (Priv_Elmt);
19504 Priv_Scop := Scope (Priv);
19506 if Ekind_In (Priv, E_Private_Subtype,
19507 E_Limited_Private_Subtype,
19508 E_Record_Subtype_With_Private)
19509 then
19510 Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
19511 Set_Is_Itype (Full);
19512 Set_Parent (Full, Parent (Priv));
19513 Set_Associated_Node_For_Itype (Full, N);
19515 -- Now we need to complete the private subtype, but since the
19516 -- base type has already been swapped, we must also swap the
19517 -- subtypes (and thus, reverse the arguments in the call to
19518 -- Complete_Private_Subtype). Also note that we may need to
19519 -- re-establish the scope of the private subtype.
19521 Copy_And_Swap (Priv, Full);
19523 if not In_Open_Scopes (Priv_Scop) then
19524 Push_Scope (Priv_Scop);
19526 else
19527 -- Reset Priv_Scop to Empty to indicate no scope was pushed
19529 Priv_Scop := Empty;
19530 end if;
19532 Complete_Private_Subtype (Full, Priv, Full_T, N);
19534 if Present (Priv_Scop) then
19535 Pop_Scope;
19536 end if;
19538 Replace_Elmt (Priv_Elmt, Full);
19539 end if;
19541 Next_Elmt (Priv_Elmt);
19542 end loop;
19543 end;
19545 -- If the private view was tagged, copy the new primitive operations
19546 -- from the private view to the full view.
19548 if Is_Tagged_Type (Full_T) then
19549 declare
19550 Disp_Typ : Entity_Id;
19551 Full_List : Elist_Id;
19552 Prim : Entity_Id;
19553 Prim_Elmt : Elmt_Id;
19554 Priv_List : Elist_Id;
19556 function Contains
19557 (E : Entity_Id;
19558 L : Elist_Id) return Boolean;
19559 -- Determine whether list L contains element E
19561 --------------
19562 -- Contains --
19563 --------------
19565 function Contains
19566 (E : Entity_Id;
19567 L : Elist_Id) return Boolean
19569 List_Elmt : Elmt_Id;
19571 begin
19572 List_Elmt := First_Elmt (L);
19573 while Present (List_Elmt) loop
19574 if Node (List_Elmt) = E then
19575 return True;
19576 end if;
19578 Next_Elmt (List_Elmt);
19579 end loop;
19581 return False;
19582 end Contains;
19584 -- Start of processing
19586 begin
19587 if Is_Tagged_Type (Priv_T) then
19588 Priv_List := Primitive_Operations (Priv_T);
19589 Prim_Elmt := First_Elmt (Priv_List);
19591 -- In the case of a concurrent type completing a private tagged
19592 -- type, primitives may have been declared in between the two
19593 -- views. These subprograms need to be wrapped the same way
19594 -- entries and protected procedures are handled because they
19595 -- cannot be directly shared by the two views.
19597 if Is_Concurrent_Type (Full_T) then
19598 declare
19599 Conc_Typ : constant Entity_Id :=
19600 Corresponding_Record_Type (Full_T);
19601 Curr_Nod : Node_Id := Parent (Conc_Typ);
19602 Wrap_Spec : Node_Id;
19604 begin
19605 while Present (Prim_Elmt) loop
19606 Prim := Node (Prim_Elmt);
19608 if Comes_From_Source (Prim)
19609 and then not Is_Abstract_Subprogram (Prim)
19610 then
19611 Wrap_Spec :=
19612 Make_Subprogram_Declaration (Sloc (Prim),
19613 Specification =>
19614 Build_Wrapper_Spec
19615 (Subp_Id => Prim,
19616 Obj_Typ => Conc_Typ,
19617 Formals =>
19618 Parameter_Specifications (
19619 Parent (Prim))));
19621 Insert_After (Curr_Nod, Wrap_Spec);
19622 Curr_Nod := Wrap_Spec;
19624 Analyze (Wrap_Spec);
19625 end if;
19627 Next_Elmt (Prim_Elmt);
19628 end loop;
19630 return;
19631 end;
19633 -- For non-concurrent types, transfer explicit primitives, but
19634 -- omit those inherited from the parent of the private view
19635 -- since they will be re-inherited later on.
19637 else
19638 Full_List := Primitive_Operations (Full_T);
19640 while Present (Prim_Elmt) loop
19641 Prim := Node (Prim_Elmt);
19643 if Comes_From_Source (Prim)
19644 and then not Contains (Prim, Full_List)
19645 then
19646 Append_Elmt (Prim, Full_List);
19647 end if;
19649 Next_Elmt (Prim_Elmt);
19650 end loop;
19651 end if;
19653 -- Untagged private view
19655 else
19656 Full_List := Primitive_Operations (Full_T);
19658 -- In this case the partial view is untagged, so here we locate
19659 -- all of the earlier primitives that need to be treated as
19660 -- dispatching (those that appear between the two views). Note
19661 -- that these additional operations must all be new operations
19662 -- (any earlier operations that override inherited operations
19663 -- of the full view will already have been inserted in the
19664 -- primitives list, marked by Check_Operation_From_Private_View
19665 -- as dispatching. Note that implicit "/=" operators are
19666 -- excluded from being added to the primitives list since they
19667 -- shouldn't be treated as dispatching (tagged "/=" is handled
19668 -- specially).
19670 Prim := Next_Entity (Full_T);
19671 while Present (Prim) and then Prim /= Priv_T loop
19672 if Ekind_In (Prim, E_Procedure, E_Function) then
19673 Disp_Typ := Find_Dispatching_Type (Prim);
19675 if Disp_Typ = Full_T
19676 and then (Chars (Prim) /= Name_Op_Ne
19677 or else Comes_From_Source (Prim))
19678 then
19679 Check_Controlling_Formals (Full_T, Prim);
19681 if not Is_Dispatching_Operation (Prim) then
19682 Append_Elmt (Prim, Full_List);
19683 Set_Is_Dispatching_Operation (Prim, True);
19684 Set_DT_Position (Prim, No_Uint);
19685 end if;
19687 elsif Is_Dispatching_Operation (Prim)
19688 and then Disp_Typ /= Full_T
19689 then
19691 -- Verify that it is not otherwise controlled by a
19692 -- formal or a return value of type T.
19694 Check_Controlling_Formals (Disp_Typ, Prim);
19695 end if;
19696 end if;
19698 Next_Entity (Prim);
19699 end loop;
19700 end if;
19702 -- For the tagged case, the two views can share the same primitive
19703 -- operations list and the same class-wide type. Update attributes
19704 -- of the class-wide type which depend on the full declaration.
19706 if Is_Tagged_Type (Priv_T) then
19707 Set_Direct_Primitive_Operations (Priv_T, Full_List);
19708 Set_Class_Wide_Type
19709 (Base_Type (Full_T), Class_Wide_Type (Priv_T));
19711 Set_Has_Task (Class_Wide_Type (Priv_T), Has_Task (Full_T));
19712 Set_Has_Protected
19713 (Class_Wide_Type (Priv_T), Has_Protected (Full_T));
19714 end if;
19715 end;
19716 end if;
19718 -- Ada 2005 AI 161: Check preelaborable initialization consistency
19720 if Known_To_Have_Preelab_Init (Priv_T) then
19722 -- Case where there is a pragma Preelaborable_Initialization. We
19723 -- always allow this in predefined units, which is cheating a bit,
19724 -- but it means we don't have to struggle to meet the requirements in
19725 -- the RM for having Preelaborable Initialization. Otherwise we
19726 -- require that the type meets the RM rules. But we can't check that
19727 -- yet, because of the rule about overriding Initialize, so we simply
19728 -- set a flag that will be checked at freeze time.
19730 if not In_Predefined_Unit (Full_T) then
19731 Set_Must_Have_Preelab_Init (Full_T);
19732 end if;
19733 end if;
19735 -- If pragma CPP_Class was applied to the private type declaration,
19736 -- propagate it now to the full type declaration.
19738 if Is_CPP_Class (Priv_T) then
19739 Set_Is_CPP_Class (Full_T);
19740 Set_Convention (Full_T, Convention_CPP);
19742 -- Check that components of imported CPP types do not have default
19743 -- expressions.
19745 Check_CPP_Type_Has_No_Defaults (Full_T);
19746 end if;
19748 -- If the private view has user specified stream attributes, then so has
19749 -- the full view.
19751 -- Why the test, how could these flags be already set in Full_T ???
19753 if Has_Specified_Stream_Read (Priv_T) then
19754 Set_Has_Specified_Stream_Read (Full_T);
19755 end if;
19757 if Has_Specified_Stream_Write (Priv_T) then
19758 Set_Has_Specified_Stream_Write (Full_T);
19759 end if;
19761 if Has_Specified_Stream_Input (Priv_T) then
19762 Set_Has_Specified_Stream_Input (Full_T);
19763 end if;
19765 if Has_Specified_Stream_Output (Priv_T) then
19766 Set_Has_Specified_Stream_Output (Full_T);
19767 end if;
19769 -- Propagate the attributes related to pragma Default_Initial_Condition
19770 -- from the private to the full view. Note that both flags are mutually
19771 -- exclusive.
19773 if Has_Default_Init_Cond (Priv_T)
19774 or else Has_Inherited_Default_Init_Cond (Priv_T)
19775 then
19776 Propagate_Default_Init_Cond_Attributes
19777 (From_Typ => Priv_T,
19778 To_Typ => Full_T,
19779 Private_To_Full_View => True);
19781 -- In the case where the full view is derived from another private type,
19782 -- the attributes related to pragma Default_Initial_Condition must be
19783 -- propagated from the full to the private view to maintain consistency
19784 -- of views.
19786 -- package Pack is
19787 -- type Parent_Typ is private
19788 -- with Default_Initial_Condition ...;
19789 -- private
19790 -- type Parent_Typ is ...;
19791 -- end Pack;
19793 -- with Pack; use Pack;
19794 -- package Pack_2 is
19795 -- type Deriv_Typ is private; -- must inherit
19796 -- private
19797 -- type Deriv_Typ is new Parent_Typ; -- must inherit
19798 -- end Pack_2;
19800 elsif Has_Default_Init_Cond (Full_T)
19801 or else Has_Inherited_Default_Init_Cond (Full_T)
19802 then
19803 Propagate_Default_Init_Cond_Attributes
19804 (From_Typ => Full_T,
19805 To_Typ => Priv_T,
19806 Private_To_Full_View => True);
19807 end if;
19809 -- Propagate the attributes related to pragma Ghost from the private to
19810 -- the full view.
19812 if Is_Ghost_Entity (Priv_T) then
19813 Set_Is_Ghost_Entity (Full_T);
19815 -- The Ghost policy in effect at the point of declaration and at the
19816 -- point of completion must match (SPARK RM 6.9(15)).
19818 Check_Ghost_Completion (Priv_T, Full_T);
19820 -- In the case where the private view of a tagged type lacks a parent
19821 -- type and is subject to pragma Ghost, ensure that the parent type
19822 -- specified by the full view is also Ghost (SPARK RM 6.9(9)).
19824 if Is_Derived_Type (Full_T) then
19825 Check_Ghost_Derivation (Full_T);
19826 end if;
19827 end if;
19829 -- Propagate invariants to full type
19831 if Has_Invariants (Priv_T) then
19832 Set_Has_Invariants (Full_T);
19833 Set_Invariant_Procedure (Full_T, Invariant_Procedure (Priv_T));
19834 end if;
19836 if Has_Inheritable_Invariants (Priv_T) then
19837 Set_Has_Inheritable_Invariants (Full_T);
19838 end if;
19840 -- Propagate predicates to full type, and predicate function if already
19841 -- defined. It is not clear that this can actually happen? the partial
19842 -- view cannot be frozen yet, and the predicate function has not been
19843 -- built. Still it is a cheap check and seems safer to make it.
19845 if Has_Predicates (Priv_T) then
19846 if Present (Predicate_Function (Priv_T)) then
19847 Set_Predicate_Function (Full_T, Predicate_Function (Priv_T));
19848 end if;
19850 Set_Has_Predicates (Full_T);
19851 end if;
19852 end Process_Full_View;
19854 -----------------------------------
19855 -- Process_Incomplete_Dependents --
19856 -----------------------------------
19858 procedure Process_Incomplete_Dependents
19859 (N : Node_Id;
19860 Full_T : Entity_Id;
19861 Inc_T : Entity_Id)
19863 Inc_Elmt : Elmt_Id;
19864 Priv_Dep : Entity_Id;
19865 New_Subt : Entity_Id;
19867 Disc_Constraint : Elist_Id;
19869 begin
19870 if No (Private_Dependents (Inc_T)) then
19871 return;
19872 end if;
19874 -- Itypes that may be generated by the completion of an incomplete
19875 -- subtype are not used by the back-end and not attached to the tree.
19876 -- They are created only for constraint-checking purposes.
19878 Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
19879 while Present (Inc_Elmt) loop
19880 Priv_Dep := Node (Inc_Elmt);
19882 if Ekind (Priv_Dep) = E_Subprogram_Type then
19884 -- An Access_To_Subprogram type may have a return type or a
19885 -- parameter type that is incomplete. Replace with the full view.
19887 if Etype (Priv_Dep) = Inc_T then
19888 Set_Etype (Priv_Dep, Full_T);
19889 end if;
19891 declare
19892 Formal : Entity_Id;
19894 begin
19895 Formal := First_Formal (Priv_Dep);
19896 while Present (Formal) loop
19897 if Etype (Formal) = Inc_T then
19898 Set_Etype (Formal, Full_T);
19899 end if;
19901 Next_Formal (Formal);
19902 end loop;
19903 end;
19905 elsif Is_Overloadable (Priv_Dep) then
19907 -- If a subprogram in the incomplete dependents list is primitive
19908 -- for a tagged full type then mark it as a dispatching operation,
19909 -- check whether it overrides an inherited subprogram, and check
19910 -- restrictions on its controlling formals. Note that a protected
19911 -- operation is never dispatching: only its wrapper operation
19912 -- (which has convention Ada) is.
19914 if Is_Tagged_Type (Full_T)
19915 and then Is_Primitive (Priv_Dep)
19916 and then Convention (Priv_Dep) /= Convention_Protected
19917 then
19918 Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
19919 Set_Is_Dispatching_Operation (Priv_Dep);
19920 Check_Controlling_Formals (Full_T, Priv_Dep);
19921 end if;
19923 elsif Ekind (Priv_Dep) = E_Subprogram_Body then
19925 -- Can happen during processing of a body before the completion
19926 -- of a TA type. Ignore, because spec is also on dependent list.
19928 return;
19930 -- Ada 2005 (AI-412): Transform a regular incomplete subtype into a
19931 -- corresponding subtype of the full view.
19933 elsif Ekind (Priv_Dep) = E_Incomplete_Subtype then
19934 Set_Subtype_Indication
19935 (Parent (Priv_Dep), New_Occurrence_Of (Full_T, Sloc (Priv_Dep)));
19936 Set_Etype (Priv_Dep, Full_T);
19937 Set_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
19938 Set_Analyzed (Parent (Priv_Dep), False);
19940 -- Reanalyze the declaration, suppressing the call to
19941 -- Enter_Name to avoid duplicate names.
19943 Analyze_Subtype_Declaration
19944 (N => Parent (Priv_Dep),
19945 Skip => True);
19947 -- Dependent is a subtype
19949 else
19950 -- We build a new subtype indication using the full view of the
19951 -- incomplete parent. The discriminant constraints have been
19952 -- elaborated already at the point of the subtype declaration.
19954 New_Subt := Create_Itype (E_Void, N);
19956 if Has_Discriminants (Full_T) then
19957 Disc_Constraint := Discriminant_Constraint (Priv_Dep);
19958 else
19959 Disc_Constraint := No_Elist;
19960 end if;
19962 Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
19963 Set_Full_View (Priv_Dep, New_Subt);
19964 end if;
19966 Next_Elmt (Inc_Elmt);
19967 end loop;
19968 end Process_Incomplete_Dependents;
19970 --------------------------------
19971 -- Process_Range_Expr_In_Decl --
19972 --------------------------------
19974 procedure Process_Range_Expr_In_Decl
19975 (R : Node_Id;
19976 T : Entity_Id;
19977 Subtyp : Entity_Id := Empty;
19978 Check_List : List_Id := Empty_List;
19979 R_Check_Off : Boolean := False;
19980 In_Iter_Schm : Boolean := False)
19982 Lo, Hi : Node_Id;
19983 R_Checks : Check_Result;
19984 Insert_Node : Node_Id;
19985 Def_Id : Entity_Id;
19987 begin
19988 Analyze_And_Resolve (R, Base_Type (T));
19990 if Nkind (R) = N_Range then
19992 -- In SPARK, all ranges should be static, with the exception of the
19993 -- discrete type definition of a loop parameter specification.
19995 if not In_Iter_Schm
19996 and then not Is_OK_Static_Range (R)
19997 then
19998 Check_SPARK_05_Restriction ("range should be static", R);
19999 end if;
20001 Lo := Low_Bound (R);
20002 Hi := High_Bound (R);
20004 -- Validity checks on the range of a quantified expression are
20005 -- delayed until the construct is transformed into a loop.
20007 if Nkind (Parent (R)) = N_Loop_Parameter_Specification
20008 and then Nkind (Parent (Parent (R))) = N_Quantified_Expression
20009 then
20010 null;
20012 -- We need to ensure validity of the bounds here, because if we
20013 -- go ahead and do the expansion, then the expanded code will get
20014 -- analyzed with range checks suppressed and we miss the check.
20016 -- WARNING: The capture of the range bounds with xxx_FIRST/_LAST and
20017 -- the temporaries generated by routine Remove_Side_Effects by means
20018 -- of validity checks must use the same names. When a range appears
20019 -- in the parent of a generic, the range is processed with checks
20020 -- disabled as part of the generic context and with checks enabled
20021 -- for code generation purposes. This leads to link issues as the
20022 -- generic contains references to xxx_FIRST/_LAST, but the inlined
20023 -- template sees the temporaries generated by Remove_Side_Effects.
20025 else
20026 Validity_Check_Range (R, Subtyp);
20027 end if;
20029 -- If there were errors in the declaration, try and patch up some
20030 -- common mistakes in the bounds. The cases handled are literals
20031 -- which are Integer where the expected type is Real and vice versa.
20032 -- These corrections allow the compilation process to proceed further
20033 -- along since some basic assumptions of the format of the bounds
20034 -- are guaranteed.
20036 if Etype (R) = Any_Type then
20037 if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
20038 Rewrite (Lo,
20039 Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
20041 elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
20042 Rewrite (Hi,
20043 Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
20045 elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
20046 Rewrite (Lo,
20047 Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
20049 elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
20050 Rewrite (Hi,
20051 Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
20052 end if;
20054 Set_Etype (Lo, T);
20055 Set_Etype (Hi, T);
20056 end if;
20058 -- If the bounds of the range have been mistakenly given as string
20059 -- literals (perhaps in place of character literals), then an error
20060 -- has already been reported, but we rewrite the string literal as a
20061 -- bound of the range's type to avoid blowups in later processing
20062 -- that looks at static values.
20064 if Nkind (Lo) = N_String_Literal then
20065 Rewrite (Lo,
20066 Make_Attribute_Reference (Sloc (Lo),
20067 Prefix => New_Occurrence_Of (T, Sloc (Lo)),
20068 Attribute_Name => Name_First));
20069 Analyze_And_Resolve (Lo);
20070 end if;
20072 if Nkind (Hi) = N_String_Literal then
20073 Rewrite (Hi,
20074 Make_Attribute_Reference (Sloc (Hi),
20075 Prefix => New_Occurrence_Of (T, Sloc (Hi)),
20076 Attribute_Name => Name_First));
20077 Analyze_And_Resolve (Hi);
20078 end if;
20080 -- If bounds aren't scalar at this point then exit, avoiding
20081 -- problems with further processing of the range in this procedure.
20083 if not Is_Scalar_Type (Etype (Lo)) then
20084 return;
20085 end if;
20087 -- Resolve (actually Sem_Eval) has checked that the bounds are in
20088 -- then range of the base type. Here we check whether the bounds
20089 -- are in the range of the subtype itself. Note that if the bounds
20090 -- represent the null range the Constraint_Error exception should
20091 -- not be raised.
20093 -- ??? The following code should be cleaned up as follows
20095 -- 1. The Is_Null_Range (Lo, Hi) test should disappear since it
20096 -- is done in the call to Range_Check (R, T); below
20098 -- 2. The use of R_Check_Off should be investigated and possibly
20099 -- removed, this would clean up things a bit.
20101 if Is_Null_Range (Lo, Hi) then
20102 null;
20104 else
20105 -- Capture values of bounds and generate temporaries for them
20106 -- if needed, before applying checks, since checks may cause
20107 -- duplication of the expression without forcing evaluation.
20109 -- The forced evaluation removes side effects from expressions,
20110 -- which should occur also in GNATprove mode. Otherwise, we end up
20111 -- with unexpected insertions of actions at places where this is
20112 -- not supposed to occur, e.g. on default parameters of a call.
20114 if Expander_Active or GNATprove_Mode then
20116 -- If no subtype name, then just call Force_Evaluation to
20117 -- create declarations as needed to deal with side effects.
20118 -- Also ignore calls from within a record type, where we
20119 -- have possible scoping issues.
20121 if No (Subtyp) or else Is_Record_Type (Current_Scope) then
20122 Force_Evaluation (Lo);
20123 Force_Evaluation (Hi);
20125 -- If a subtype is given, then we capture the bounds if they
20126 -- are not known at compile time, using constant identifiers
20127 -- xxx_FIRST and xxx_LAST where xxx is the name of the subtype.
20129 -- Note: we do this transformation even if expansion is not
20130 -- active, and in particular we do it in GNATprove_Mode since
20131 -- the transformation is in general required to ensure that the
20132 -- resulting tree has proper Ada semantics.
20134 -- Historical note: We used to just do Force_Evaluation calls
20135 -- in all cases, but it is better to capture the bounds with
20136 -- proper non-serialized names, since these will be accessed
20137 -- from other units, and hence may be public, and also we can
20138 -- then expand 'First and 'Last references to be references to
20139 -- these special names.
20141 else
20142 if not Compile_Time_Known_Value (Lo)
20144 -- No need to capture bounds if they already are
20145 -- references to constants.
20147 and then not (Is_Entity_Name (Lo)
20148 and then Is_Constant_Object (Entity (Lo)))
20149 then
20150 declare
20151 Loc : constant Source_Ptr := Sloc (Lo);
20152 Lov : constant Entity_Id :=
20153 Make_Defining_Identifier (Loc,
20154 Chars =>
20155 New_External_Name (Chars (Subtyp), "_FIRST"));
20156 begin
20157 Insert_Action (R,
20158 Make_Object_Declaration (Loc,
20159 Defining_Identifier => Lov,
20160 Object_Definition =>
20161 New_Occurrence_Of (Base_Type (T), Loc),
20162 Constant_Present => True,
20163 Expression => Relocate_Node (Lo)));
20164 Rewrite (Lo, New_Occurrence_Of (Lov, Loc));
20165 end;
20166 end if;
20168 if not Compile_Time_Known_Value (Hi)
20169 and then not (Is_Entity_Name (Hi)
20170 and then Is_Constant_Object (Entity (Hi)))
20171 then
20172 declare
20173 Loc : constant Source_Ptr := Sloc (Hi);
20174 Hiv : constant Entity_Id :=
20175 Make_Defining_Identifier (Loc,
20176 Chars =>
20177 New_External_Name (Chars (Subtyp), "_LAST"));
20178 begin
20179 Insert_Action (R,
20180 Make_Object_Declaration (Loc,
20181 Defining_Identifier => Hiv,
20182 Object_Definition =>
20183 New_Occurrence_Of (Base_Type (T), Loc),
20184 Constant_Present => True,
20185 Expression => Relocate_Node (Hi)));
20186 Rewrite (Hi, New_Occurrence_Of (Hiv, Loc));
20187 end;
20188 end if;
20189 end if;
20190 end if;
20192 -- We use a flag here instead of suppressing checks on the
20193 -- type because the type we check against isn't necessarily
20194 -- the place where we put the check.
20196 if not R_Check_Off then
20197 R_Checks := Get_Range_Checks (R, T);
20199 -- Look up tree to find an appropriate insertion point. We
20200 -- can't just use insert_actions because later processing
20201 -- depends on the insertion node. Prior to Ada 2012 the
20202 -- insertion point could only be a declaration or a loop, but
20203 -- quantified expressions can appear within any context in an
20204 -- expression, and the insertion point can be any statement,
20205 -- pragma, or declaration.
20207 Insert_Node := Parent (R);
20208 while Present (Insert_Node) loop
20209 exit when
20210 Nkind (Insert_Node) in N_Declaration
20211 and then
20212 not Nkind_In
20213 (Insert_Node, N_Component_Declaration,
20214 N_Loop_Parameter_Specification,
20215 N_Function_Specification,
20216 N_Procedure_Specification);
20218 exit when Nkind (Insert_Node) in N_Later_Decl_Item
20219 or else Nkind (Insert_Node) in
20220 N_Statement_Other_Than_Procedure_Call
20221 or else Nkind_In (Insert_Node, N_Procedure_Call_Statement,
20222 N_Pragma);
20224 Insert_Node := Parent (Insert_Node);
20225 end loop;
20227 -- Why would Type_Decl not be present??? Without this test,
20228 -- short regression tests fail.
20230 if Present (Insert_Node) then
20232 -- Case of loop statement. Verify that the range is part
20233 -- of the subtype indication of the iteration scheme.
20235 if Nkind (Insert_Node) = N_Loop_Statement then
20236 declare
20237 Indic : Node_Id;
20239 begin
20240 Indic := Parent (R);
20241 while Present (Indic)
20242 and then Nkind (Indic) /= N_Subtype_Indication
20243 loop
20244 Indic := Parent (Indic);
20245 end loop;
20247 if Present (Indic) then
20248 Def_Id := Etype (Subtype_Mark (Indic));
20250 Insert_Range_Checks
20251 (R_Checks,
20252 Insert_Node,
20253 Def_Id,
20254 Sloc (Insert_Node),
20256 Do_Before => True);
20257 end if;
20258 end;
20260 -- Insertion before a declaration. If the declaration
20261 -- includes discriminants, the list of applicable checks
20262 -- is given by the caller.
20264 elsif Nkind (Insert_Node) in N_Declaration then
20265 Def_Id := Defining_Identifier (Insert_Node);
20267 if (Ekind (Def_Id) = E_Record_Type
20268 and then Depends_On_Discriminant (R))
20269 or else
20270 (Ekind (Def_Id) = E_Protected_Type
20271 and then Has_Discriminants (Def_Id))
20272 then
20273 Append_Range_Checks
20274 (R_Checks,
20275 Check_List, Def_Id, Sloc (Insert_Node), R);
20277 else
20278 Insert_Range_Checks
20279 (R_Checks,
20280 Insert_Node, Def_Id, Sloc (Insert_Node), R);
20282 end if;
20284 -- Insertion before a statement. Range appears in the
20285 -- context of a quantified expression. Insertion will
20286 -- take place when expression is expanded.
20288 else
20289 null;
20290 end if;
20291 end if;
20292 end if;
20293 end if;
20295 -- Case of other than an explicit N_Range node
20297 -- The forced evaluation removes side effects from expressions, which
20298 -- should occur also in GNATprove mode. Otherwise, we end up with
20299 -- unexpected insertions of actions at places where this is not
20300 -- supposed to occur, e.g. on default parameters of a call.
20302 elsif Expander_Active or GNATprove_Mode then
20303 Get_Index_Bounds (R, Lo, Hi);
20304 Force_Evaluation (Lo);
20305 Force_Evaluation (Hi);
20306 end if;
20307 end Process_Range_Expr_In_Decl;
20309 --------------------------------------
20310 -- Process_Real_Range_Specification --
20311 --------------------------------------
20313 procedure Process_Real_Range_Specification (Def : Node_Id) is
20314 Spec : constant Node_Id := Real_Range_Specification (Def);
20315 Lo : Node_Id;
20316 Hi : Node_Id;
20317 Err : Boolean := False;
20319 procedure Analyze_Bound (N : Node_Id);
20320 -- Analyze and check one bound
20322 -------------------
20323 -- Analyze_Bound --
20324 -------------------
20326 procedure Analyze_Bound (N : Node_Id) is
20327 begin
20328 Analyze_And_Resolve (N, Any_Real);
20330 if not Is_OK_Static_Expression (N) then
20331 Flag_Non_Static_Expr
20332 ("bound in real type definition is not static!", N);
20333 Err := True;
20334 end if;
20335 end Analyze_Bound;
20337 -- Start of processing for Process_Real_Range_Specification
20339 begin
20340 if Present (Spec) then
20341 Lo := Low_Bound (Spec);
20342 Hi := High_Bound (Spec);
20343 Analyze_Bound (Lo);
20344 Analyze_Bound (Hi);
20346 -- If error, clear away junk range specification
20348 if Err then
20349 Set_Real_Range_Specification (Def, Empty);
20350 end if;
20351 end if;
20352 end Process_Real_Range_Specification;
20354 ---------------------
20355 -- Process_Subtype --
20356 ---------------------
20358 function Process_Subtype
20359 (S : Node_Id;
20360 Related_Nod : Node_Id;
20361 Related_Id : Entity_Id := Empty;
20362 Suffix : Character := ' ') return Entity_Id
20364 P : Node_Id;
20365 Def_Id : Entity_Id;
20366 Error_Node : Node_Id;
20367 Full_View_Id : Entity_Id;
20368 Subtype_Mark_Id : Entity_Id;
20370 May_Have_Null_Exclusion : Boolean;
20372 procedure Check_Incomplete (T : Entity_Id);
20373 -- Called to verify that an incomplete type is not used prematurely
20375 ----------------------
20376 -- Check_Incomplete --
20377 ----------------------
20379 procedure Check_Incomplete (T : Entity_Id) is
20380 begin
20381 -- Ada 2005 (AI-412): Incomplete subtypes are legal
20383 if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
20384 and then
20385 not (Ada_Version >= Ada_2005
20386 and then
20387 (Nkind (Parent (T)) = N_Subtype_Declaration
20388 or else (Nkind (Parent (T)) = N_Subtype_Indication
20389 and then Nkind (Parent (Parent (T))) =
20390 N_Subtype_Declaration)))
20391 then
20392 Error_Msg_N ("invalid use of type before its full declaration", T);
20393 end if;
20394 end Check_Incomplete;
20396 -- Start of processing for Process_Subtype
20398 begin
20399 -- Case of no constraints present
20401 if Nkind (S) /= N_Subtype_Indication then
20402 Find_Type (S);
20403 Check_Incomplete (S);
20404 P := Parent (S);
20406 -- Ada 2005 (AI-231): Static check
20408 if Ada_Version >= Ada_2005
20409 and then Present (P)
20410 and then Null_Exclusion_Present (P)
20411 and then Nkind (P) /= N_Access_To_Object_Definition
20412 and then not Is_Access_Type (Entity (S))
20413 then
20414 Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
20415 end if;
20417 -- The following is ugly, can't we have a range or even a flag???
20419 May_Have_Null_Exclusion :=
20420 Nkind_In (P, N_Access_Definition,
20421 N_Access_Function_Definition,
20422 N_Access_Procedure_Definition,
20423 N_Access_To_Object_Definition,
20424 N_Allocator,
20425 N_Component_Definition)
20426 or else
20427 Nkind_In (P, N_Derived_Type_Definition,
20428 N_Discriminant_Specification,
20429 N_Formal_Object_Declaration,
20430 N_Object_Declaration,
20431 N_Object_Renaming_Declaration,
20432 N_Parameter_Specification,
20433 N_Subtype_Declaration);
20435 -- Create an Itype that is a duplicate of Entity (S) but with the
20436 -- null-exclusion attribute.
20438 if May_Have_Null_Exclusion
20439 and then Is_Access_Type (Entity (S))
20440 and then Null_Exclusion_Present (P)
20442 -- No need to check the case of an access to object definition.
20443 -- It is correct to define double not-null pointers.
20445 -- Example:
20446 -- type Not_Null_Int_Ptr is not null access Integer;
20447 -- type Acc is not null access Not_Null_Int_Ptr;
20449 and then Nkind (P) /= N_Access_To_Object_Definition
20450 then
20451 if Can_Never_Be_Null (Entity (S)) then
20452 case Nkind (Related_Nod) is
20453 when N_Full_Type_Declaration =>
20454 if Nkind (Type_Definition (Related_Nod))
20455 in N_Array_Type_Definition
20456 then
20457 Error_Node :=
20458 Subtype_Indication
20459 (Component_Definition
20460 (Type_Definition (Related_Nod)));
20461 else
20462 Error_Node :=
20463 Subtype_Indication (Type_Definition (Related_Nod));
20464 end if;
20466 when N_Subtype_Declaration =>
20467 Error_Node := Subtype_Indication (Related_Nod);
20469 when N_Object_Declaration =>
20470 Error_Node := Object_Definition (Related_Nod);
20472 when N_Component_Declaration =>
20473 Error_Node :=
20474 Subtype_Indication (Component_Definition (Related_Nod));
20476 when N_Allocator =>
20477 Error_Node := Expression (Related_Nod);
20479 when others =>
20480 pragma Assert (False);
20481 Error_Node := Related_Nod;
20482 end case;
20484 Error_Msg_NE
20485 ("`NOT NULL` not allowed (& already excludes null)",
20486 Error_Node,
20487 Entity (S));
20488 end if;
20490 Set_Etype (S,
20491 Create_Null_Excluding_Itype
20492 (T => Entity (S),
20493 Related_Nod => P));
20494 Set_Entity (S, Etype (S));
20495 end if;
20497 return Entity (S);
20499 -- Case of constraint present, so that we have an N_Subtype_Indication
20500 -- node (this node is created only if constraints are present).
20502 else
20503 Find_Type (Subtype_Mark (S));
20505 if Nkind (Parent (S)) /= N_Access_To_Object_Definition
20506 and then not
20507 (Nkind (Parent (S)) = N_Subtype_Declaration
20508 and then Is_Itype (Defining_Identifier (Parent (S))))
20509 then
20510 Check_Incomplete (Subtype_Mark (S));
20511 end if;
20513 P := Parent (S);
20514 Subtype_Mark_Id := Entity (Subtype_Mark (S));
20516 -- Explicit subtype declaration case
20518 if Nkind (P) = N_Subtype_Declaration then
20519 Def_Id := Defining_Identifier (P);
20521 -- Explicit derived type definition case
20523 elsif Nkind (P) = N_Derived_Type_Definition then
20524 Def_Id := Defining_Identifier (Parent (P));
20526 -- Implicit case, the Def_Id must be created as an implicit type.
20527 -- The one exception arises in the case of concurrent types, array
20528 -- and access types, where other subsidiary implicit types may be
20529 -- created and must appear before the main implicit type. In these
20530 -- cases we leave Def_Id set to Empty as a signal that Create_Itype
20531 -- has not yet been called to create Def_Id.
20533 else
20534 if Is_Array_Type (Subtype_Mark_Id)
20535 or else Is_Concurrent_Type (Subtype_Mark_Id)
20536 or else Is_Access_Type (Subtype_Mark_Id)
20537 then
20538 Def_Id := Empty;
20540 -- For the other cases, we create a new unattached Itype,
20541 -- and set the indication to ensure it gets attached later.
20543 else
20544 Def_Id :=
20545 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
20546 end if;
20547 end if;
20549 -- If the kind of constraint is invalid for this kind of type,
20550 -- then give an error, and then pretend no constraint was given.
20552 if not Is_Valid_Constraint_Kind
20553 (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
20554 then
20555 Error_Msg_N
20556 ("incorrect constraint for this kind of type", Constraint (S));
20558 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
20560 -- Set Ekind of orphan itype, to prevent cascaded errors
20562 if Present (Def_Id) then
20563 Set_Ekind (Def_Id, Ekind (Any_Type));
20564 end if;
20566 -- Make recursive call, having got rid of the bogus constraint
20568 return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
20569 end if;
20571 -- Remaining processing depends on type. Select on Base_Type kind to
20572 -- ensure getting to the concrete type kind in the case of a private
20573 -- subtype (needed when only doing semantic analysis).
20575 case Ekind (Base_Type (Subtype_Mark_Id)) is
20576 when Access_Kind =>
20578 -- If this is a constraint on a class-wide type, discard it.
20579 -- There is currently no way to express a partial discriminant
20580 -- constraint on a type with unknown discriminants. This is
20581 -- a pathology that the ACATS wisely decides not to test.
20583 if Is_Class_Wide_Type (Designated_Type (Subtype_Mark_Id)) then
20584 if Comes_From_Source (S) then
20585 Error_Msg_N
20586 ("constraint on class-wide type ignored??",
20587 Constraint (S));
20588 end if;
20590 if Nkind (P) = N_Subtype_Declaration then
20591 Set_Subtype_Indication (P,
20592 New_Occurrence_Of (Subtype_Mark_Id, Sloc (S)));
20593 end if;
20595 return Subtype_Mark_Id;
20596 end if;
20598 Constrain_Access (Def_Id, S, Related_Nod);
20600 if Expander_Active
20601 and then Is_Itype (Designated_Type (Def_Id))
20602 and then Nkind (Related_Nod) = N_Subtype_Declaration
20603 and then not Is_Incomplete_Type (Designated_Type (Def_Id))
20604 then
20605 Build_Itype_Reference
20606 (Designated_Type (Def_Id), Related_Nod);
20607 end if;
20609 when Array_Kind =>
20610 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
20612 when Decimal_Fixed_Point_Kind =>
20613 Constrain_Decimal (Def_Id, S);
20615 when Enumeration_Kind =>
20616 Constrain_Enumeration (Def_Id, S);
20617 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
20619 when Ordinary_Fixed_Point_Kind =>
20620 Constrain_Ordinary_Fixed (Def_Id, S);
20622 when Float_Kind =>
20623 Constrain_Float (Def_Id, S);
20625 when Integer_Kind =>
20626 Constrain_Integer (Def_Id, S);
20627 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
20629 when E_Record_Type |
20630 E_Record_Subtype |
20631 Class_Wide_Kind |
20632 E_Incomplete_Type =>
20633 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
20635 if Ekind (Def_Id) = E_Incomplete_Type then
20636 Set_Private_Dependents (Def_Id, New_Elmt_List);
20637 end if;
20639 when Private_Kind =>
20640 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
20641 Set_Private_Dependents (Def_Id, New_Elmt_List);
20643 -- In case of an invalid constraint prevent further processing
20644 -- since the type constructed is missing expected fields.
20646 if Etype (Def_Id) = Any_Type then
20647 return Def_Id;
20648 end if;
20650 -- If the full view is that of a task with discriminants,
20651 -- we must constrain both the concurrent type and its
20652 -- corresponding record type. Otherwise we will just propagate
20653 -- the constraint to the full view, if available.
20655 if Present (Full_View (Subtype_Mark_Id))
20656 and then Has_Discriminants (Subtype_Mark_Id)
20657 and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
20658 then
20659 Full_View_Id :=
20660 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
20662 Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
20663 Constrain_Concurrent (Full_View_Id, S,
20664 Related_Nod, Related_Id, Suffix);
20665 Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
20666 Set_Full_View (Def_Id, Full_View_Id);
20668 -- Introduce an explicit reference to the private subtype,
20669 -- to prevent scope anomalies in gigi if first use appears
20670 -- in a nested context, e.g. a later function body.
20671 -- Should this be generated in other contexts than a full
20672 -- type declaration?
20674 if Is_Itype (Def_Id)
20675 and then
20676 Nkind (Parent (P)) = N_Full_Type_Declaration
20677 then
20678 Build_Itype_Reference (Def_Id, Parent (P));
20679 end if;
20681 else
20682 Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
20683 end if;
20685 when Concurrent_Kind =>
20686 Constrain_Concurrent (Def_Id, S,
20687 Related_Nod, Related_Id, Suffix);
20689 when others =>
20690 Error_Msg_N ("invalid subtype mark in subtype indication", S);
20691 end case;
20693 -- Size and Convention are always inherited from the base type
20695 Set_Size_Info (Def_Id, (Subtype_Mark_Id));
20696 Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
20698 return Def_Id;
20699 end if;
20700 end Process_Subtype;
20702 --------------------------------------------
20703 -- Propagate_Default_Init_Cond_Attributes --
20704 --------------------------------------------
20706 procedure Propagate_Default_Init_Cond_Attributes
20707 (From_Typ : Entity_Id;
20708 To_Typ : Entity_Id;
20709 Parent_To_Derivation : Boolean := False;
20710 Private_To_Full_View : Boolean := False)
20712 procedure Remove_Default_Init_Cond_Procedure (Typ : Entity_Id);
20713 -- Remove the default initial procedure (if any) from the rep chain of
20714 -- type Typ.
20716 ----------------------------------------
20717 -- Remove_Default_Init_Cond_Procedure --
20718 ----------------------------------------
20720 procedure Remove_Default_Init_Cond_Procedure (Typ : Entity_Id) is
20721 Found : Boolean := False;
20722 Prev : Entity_Id;
20723 Subp : Entity_Id;
20725 begin
20726 Prev := Typ;
20727 Subp := Subprograms_For_Type (Typ);
20728 while Present (Subp) loop
20729 if Is_Default_Init_Cond_Procedure (Subp) then
20730 Found := True;
20731 exit;
20732 end if;
20734 Prev := Subp;
20735 Subp := Subprograms_For_Type (Subp);
20736 end loop;
20738 if Found then
20739 Set_Subprograms_For_Type (Prev, Subprograms_For_Type (Subp));
20740 Set_Subprograms_For_Type (Subp, Empty);
20741 end if;
20742 end Remove_Default_Init_Cond_Procedure;
20744 -- Local variables
20746 Inherit_Procedure : Boolean := False;
20748 -- Start of processing for Propagate_Default_Init_Cond_Attributes
20750 begin
20751 if Has_Default_Init_Cond (From_Typ) then
20753 -- A derived type inherits the attributes from its parent type
20755 if Parent_To_Derivation then
20756 Set_Has_Inherited_Default_Init_Cond (To_Typ);
20758 -- A full view shares the attributes with its private view
20760 else
20761 Set_Has_Default_Init_Cond (To_Typ);
20762 end if;
20764 Inherit_Procedure := True;
20766 -- Due to the order of expansion, a derived private type is processed
20767 -- by two routines which both attempt to set the attributes related
20768 -- to pragma Default_Initial_Condition - Build_Derived_Type and then
20769 -- Process_Full_View.
20771 -- package Pack is
20772 -- type Parent_Typ is private
20773 -- with Default_Initial_Condition ...;
20774 -- private
20775 -- type Parent_Typ is ...;
20776 -- end Pack;
20778 -- with Pack; use Pack;
20779 -- package Pack_2 is
20780 -- type Deriv_Typ is private
20781 -- with Default_Initial_Condition ...;
20782 -- private
20783 -- type Deriv_Typ is new Parent_Typ;
20784 -- end Pack_2;
20786 -- When Build_Derived_Type operates, it sets the attributes on the
20787 -- full view without taking into account that the private view may
20788 -- define its own default initial condition procedure. This becomes
20789 -- apparent in Process_Full_View which must undo some of the work by
20790 -- Build_Derived_Type and propagate the attributes from the private
20791 -- to the full view.
20793 if Private_To_Full_View then
20794 Set_Has_Inherited_Default_Init_Cond (To_Typ, False);
20795 Remove_Default_Init_Cond_Procedure (To_Typ);
20796 end if;
20798 -- A type must inherit the default initial condition procedure from a
20799 -- parent type when the parent itself is inheriting the procedure or
20800 -- when it is defining one. This circuitry is also used when dealing
20801 -- with the private / full view of a type.
20803 elsif Has_Inherited_Default_Init_Cond (From_Typ)
20804 or (Parent_To_Derivation
20805 and Present (Get_Pragma
20806 (From_Typ, Pragma_Default_Initial_Condition)))
20807 then
20808 Set_Has_Inherited_Default_Init_Cond (To_Typ);
20809 Inherit_Procedure := True;
20810 end if;
20812 if Inherit_Procedure
20813 and then No (Default_Init_Cond_Procedure (To_Typ))
20814 then
20815 Set_Default_Init_Cond_Procedure
20816 (To_Typ, Default_Init_Cond_Procedure (From_Typ));
20817 end if;
20818 end Propagate_Default_Init_Cond_Attributes;
20820 -----------------------------
20821 -- Record_Type_Declaration --
20822 -----------------------------
20824 procedure Record_Type_Declaration
20825 (T : Entity_Id;
20826 N : Node_Id;
20827 Prev : Entity_Id)
20829 Def : constant Node_Id := Type_Definition (N);
20830 Is_Tagged : Boolean;
20831 Tag_Comp : Entity_Id;
20833 begin
20834 -- These flags must be initialized before calling Process_Discriminants
20835 -- because this routine makes use of them.
20837 Set_Ekind (T, E_Record_Type);
20838 Set_Etype (T, T);
20839 Init_Size_Align (T);
20840 Set_Interfaces (T, No_Elist);
20841 Set_Stored_Constraint (T, No_Elist);
20842 Set_Default_SSO (T);
20844 -- Normal case
20846 if Ada_Version < Ada_2005 or else not Interface_Present (Def) then
20847 if Limited_Present (Def) then
20848 Check_SPARK_05_Restriction ("limited is not allowed", N);
20849 end if;
20851 if Abstract_Present (Def) then
20852 Check_SPARK_05_Restriction ("abstract is not allowed", N);
20853 end if;
20855 -- The flag Is_Tagged_Type might have already been set by
20856 -- Find_Type_Name if it detected an error for declaration T. This
20857 -- arises in the case of private tagged types where the full view
20858 -- omits the word tagged.
20860 Is_Tagged :=
20861 Tagged_Present (Def)
20862 or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
20864 Set_Is_Limited_Record (T, Limited_Present (Def));
20866 if Is_Tagged then
20867 Set_Is_Tagged_Type (T, True);
20868 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
20869 end if;
20871 -- Type is abstract if full declaration carries keyword, or if
20872 -- previous partial view did.
20874 Set_Is_Abstract_Type (T, Is_Abstract_Type (T)
20875 or else Abstract_Present (Def));
20877 else
20878 Check_SPARK_05_Restriction ("interface is not allowed", N);
20880 Is_Tagged := True;
20881 Analyze_Interface_Declaration (T, Def);
20883 if Present (Discriminant_Specifications (N)) then
20884 Error_Msg_N
20885 ("interface types cannot have discriminants",
20886 Defining_Identifier
20887 (First (Discriminant_Specifications (N))));
20888 end if;
20889 end if;
20891 -- First pass: if there are self-referential access components,
20892 -- create the required anonymous access type declarations, and if
20893 -- need be an incomplete type declaration for T itself.
20895 Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
20897 if Ada_Version >= Ada_2005
20898 and then Present (Interface_List (Def))
20899 then
20900 Check_Interfaces (N, Def);
20902 declare
20903 Ifaces_List : Elist_Id;
20905 begin
20906 -- Ada 2005 (AI-251): Collect the list of progenitors that are not
20907 -- already in the parents.
20909 Collect_Interfaces
20910 (T => T,
20911 Ifaces_List => Ifaces_List,
20912 Exclude_Parents => True);
20914 Set_Interfaces (T, Ifaces_List);
20915 end;
20916 end if;
20918 -- Records constitute a scope for the component declarations within.
20919 -- The scope is created prior to the processing of these declarations.
20920 -- Discriminants are processed first, so that they are visible when
20921 -- processing the other components. The Ekind of the record type itself
20922 -- is set to E_Record_Type (subtypes appear as E_Record_Subtype).
20924 -- Enter record scope
20926 Push_Scope (T);
20928 -- If an incomplete or private type declaration was already given for
20929 -- the type, then this scope already exists, and the discriminants have
20930 -- been declared within. We must verify that the full declaration
20931 -- matches the incomplete one.
20933 Check_Or_Process_Discriminants (N, T, Prev);
20935 Set_Is_Constrained (T, not Has_Discriminants (T));
20936 Set_Has_Delayed_Freeze (T, True);
20938 -- For tagged types add a manually analyzed component corresponding
20939 -- to the component _tag, the corresponding piece of tree will be
20940 -- expanded as part of the freezing actions if it is not a CPP_Class.
20942 if Is_Tagged then
20944 -- Do not add the tag unless we are in expansion mode
20946 if Expander_Active then
20947 Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
20948 Enter_Name (Tag_Comp);
20950 Set_Ekind (Tag_Comp, E_Component);
20951 Set_Is_Tag (Tag_Comp);
20952 Set_Is_Aliased (Tag_Comp);
20953 Set_Etype (Tag_Comp, RTE (RE_Tag));
20954 Set_DT_Entry_Count (Tag_Comp, No_Uint);
20955 Set_Original_Record_Component (Tag_Comp, Tag_Comp);
20956 Init_Component_Location (Tag_Comp);
20958 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
20959 -- implemented interfaces.
20961 if Has_Interfaces (T) then
20962 Add_Interface_Tag_Components (N, T);
20963 end if;
20964 end if;
20966 Make_Class_Wide_Type (T);
20967 Set_Direct_Primitive_Operations (T, New_Elmt_List);
20968 end if;
20970 -- We must suppress range checks when processing record components in
20971 -- the presence of discriminants, since we don't want spurious checks to
20972 -- be generated during their analysis, but Suppress_Range_Checks flags
20973 -- must be reset the after processing the record definition.
20975 -- Note: this is the only use of Kill_Range_Checks, and is a bit odd,
20976 -- couldn't we just use the normal range check suppression method here.
20977 -- That would seem cleaner ???
20979 if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
20980 Set_Kill_Range_Checks (T, True);
20981 Record_Type_Definition (Def, Prev);
20982 Set_Kill_Range_Checks (T, False);
20983 else
20984 Record_Type_Definition (Def, Prev);
20985 end if;
20987 -- Exit from record scope
20989 End_Scope;
20991 -- Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
20992 -- the implemented interfaces and associate them an aliased entity.
20994 if Is_Tagged
20995 and then not Is_Empty_List (Interface_List (Def))
20996 then
20997 Derive_Progenitor_Subprograms (T, T);
20998 end if;
21000 Check_Function_Writable_Actuals (N);
21001 end Record_Type_Declaration;
21003 ----------------------------
21004 -- Record_Type_Definition --
21005 ----------------------------
21007 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
21008 Component : Entity_Id;
21009 Ctrl_Components : Boolean := False;
21010 Final_Storage_Only : Boolean;
21011 T : Entity_Id;
21013 begin
21014 if Ekind (Prev_T) = E_Incomplete_Type then
21015 T := Full_View (Prev_T);
21016 else
21017 T := Prev_T;
21018 end if;
21020 -- In SPARK, tagged types and type extensions may only be declared in
21021 -- the specification of library unit packages.
21023 if Present (Def) and then Is_Tagged_Type (T) then
21024 declare
21025 Typ : Node_Id;
21026 Ctxt : Node_Id;
21028 begin
21029 if Nkind (Parent (Def)) = N_Full_Type_Declaration then
21030 Typ := Parent (Def);
21031 else
21032 pragma Assert
21033 (Nkind (Parent (Def)) = N_Derived_Type_Definition);
21034 Typ := Parent (Parent (Def));
21035 end if;
21037 Ctxt := Parent (Typ);
21039 if Nkind (Ctxt) = N_Package_Body
21040 and then Nkind (Parent (Ctxt)) = N_Compilation_Unit
21041 then
21042 Check_SPARK_05_Restriction
21043 ("type should be defined in package specification", Typ);
21045 elsif Nkind (Ctxt) /= N_Package_Specification
21046 or else Nkind (Parent (Parent (Ctxt))) /= N_Compilation_Unit
21047 then
21048 Check_SPARK_05_Restriction
21049 ("type should be defined in library unit package", Typ);
21050 end if;
21051 end;
21052 end if;
21054 Final_Storage_Only := not Is_Controlled (T);
21056 -- Ada 2005: Check whether an explicit Limited is present in a derived
21057 -- type declaration.
21059 if Nkind (Parent (Def)) = N_Derived_Type_Definition
21060 and then Limited_Present (Parent (Def))
21061 then
21062 Set_Is_Limited_Record (T);
21063 end if;
21065 -- If the component list of a record type is defined by the reserved
21066 -- word null and there is no discriminant part, then the record type has
21067 -- no components and all records of the type are null records (RM 3.7)
21068 -- This procedure is also called to process the extension part of a
21069 -- record extension, in which case the current scope may have inherited
21070 -- components.
21072 if No (Def)
21073 or else No (Component_List (Def))
21074 or else Null_Present (Component_List (Def))
21075 then
21076 if not Is_Tagged_Type (T) then
21077 Check_SPARK_05_Restriction ("untagged record cannot be null", Def);
21078 end if;
21080 else
21081 Analyze_Declarations (Component_Items (Component_List (Def)));
21083 if Present (Variant_Part (Component_List (Def))) then
21084 Check_SPARK_05_Restriction ("variant part is not allowed", Def);
21085 Analyze (Variant_Part (Component_List (Def)));
21086 end if;
21087 end if;
21089 -- After completing the semantic analysis of the record definition,
21090 -- record components, both new and inherited, are accessible. Set their
21091 -- kind accordingly. Exclude malformed itypes from illegal declarations,
21092 -- whose Ekind may be void.
21094 Component := First_Entity (Current_Scope);
21095 while Present (Component) loop
21096 if Ekind (Component) = E_Void
21097 and then not Is_Itype (Component)
21098 then
21099 Set_Ekind (Component, E_Component);
21100 Init_Component_Location (Component);
21101 end if;
21103 if Has_Task (Etype (Component)) then
21104 Set_Has_Task (T);
21105 end if;
21107 if Has_Protected (Etype (Component)) then
21108 Set_Has_Protected (T);
21109 end if;
21111 if Ekind (Component) /= E_Component then
21112 null;
21114 -- Do not set Has_Controlled_Component on a class-wide equivalent
21115 -- type. See Make_CW_Equivalent_Type.
21117 elsif not Is_Class_Wide_Equivalent_Type (T)
21118 and then (Has_Controlled_Component (Etype (Component))
21119 or else (Chars (Component) /= Name_uParent
21120 and then Is_Controlled (Etype (Component))))
21121 then
21122 Set_Has_Controlled_Component (T, True);
21123 Final_Storage_Only :=
21124 Final_Storage_Only
21125 and then Finalize_Storage_Only (Etype (Component));
21126 Ctrl_Components := True;
21127 end if;
21129 Next_Entity (Component);
21130 end loop;
21132 -- A Type is Finalize_Storage_Only only if all its controlled components
21133 -- are also.
21135 if Ctrl_Components then
21136 Set_Finalize_Storage_Only (T, Final_Storage_Only);
21137 end if;
21139 -- Place reference to end record on the proper entity, which may
21140 -- be a partial view.
21142 if Present (Def) then
21143 Process_End_Label (Def, 'e', Prev_T);
21144 end if;
21145 end Record_Type_Definition;
21147 ------------------------
21148 -- Replace_Components --
21149 ------------------------
21151 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id) is
21152 function Process (N : Node_Id) return Traverse_Result;
21154 -------------
21155 -- Process --
21156 -------------
21158 function Process (N : Node_Id) return Traverse_Result is
21159 Comp : Entity_Id;
21161 begin
21162 if Nkind (N) = N_Discriminant_Specification then
21163 Comp := First_Discriminant (Typ);
21164 while Present (Comp) loop
21165 if Chars (Comp) = Chars (Defining_Identifier (N)) then
21166 Set_Defining_Identifier (N, Comp);
21167 exit;
21168 end if;
21170 Next_Discriminant (Comp);
21171 end loop;
21173 elsif Nkind (N) = N_Component_Declaration then
21174 Comp := First_Component (Typ);
21175 while Present (Comp) loop
21176 if Chars (Comp) = Chars (Defining_Identifier (N)) then
21177 Set_Defining_Identifier (N, Comp);
21178 exit;
21179 end if;
21181 Next_Component (Comp);
21182 end loop;
21183 end if;
21185 return OK;
21186 end Process;
21188 procedure Replace is new Traverse_Proc (Process);
21190 -- Start of processing for Replace_Components
21192 begin
21193 Replace (Decl);
21194 end Replace_Components;
21196 -------------------------------
21197 -- Set_Completion_Referenced --
21198 -------------------------------
21200 procedure Set_Completion_Referenced (E : Entity_Id) is
21201 begin
21202 -- If in main unit, mark entity that is a completion as referenced,
21203 -- warnings go on the partial view when needed.
21205 if In_Extended_Main_Source_Unit (E) then
21206 Set_Referenced (E);
21207 end if;
21208 end Set_Completion_Referenced;
21210 ---------------------
21211 -- Set_Default_SSO --
21212 ---------------------
21214 procedure Set_Default_SSO (T : Entity_Id) is
21215 begin
21216 case Opt.Default_SSO is
21217 when ' ' =>
21218 null;
21219 when 'L' =>
21220 Set_SSO_Set_Low_By_Default (T, True);
21221 when 'H' =>
21222 Set_SSO_Set_High_By_Default (T, True);
21223 when others =>
21224 raise Program_Error;
21225 end case;
21226 end Set_Default_SSO;
21228 ---------------------
21229 -- Set_Fixed_Range --
21230 ---------------------
21232 -- The range for fixed-point types is complicated by the fact that we
21233 -- do not know the exact end points at the time of the declaration. This
21234 -- is true for three reasons:
21236 -- A size clause may affect the fudging of the end-points.
21237 -- A small clause may affect the values of the end-points.
21238 -- We try to include the end-points if it does not affect the size.
21240 -- This means that the actual end-points must be established at the
21241 -- point when the type is frozen. Meanwhile, we first narrow the range
21242 -- as permitted (so that it will fit if necessary in a small specified
21243 -- size), and then build a range subtree with these narrowed bounds.
21244 -- Set_Fixed_Range constructs the range from real literal values, and
21245 -- sets the range as the Scalar_Range of the given fixed-point type entity.
21247 -- The parent of this range is set to point to the entity so that it is
21248 -- properly hooked into the tree (unlike normal Scalar_Range entries for
21249 -- other scalar types, which are just pointers to the range in the
21250 -- original tree, this would otherwise be an orphan).
21252 -- The tree is left unanalyzed. When the type is frozen, the processing
21253 -- in Freeze.Freeze_Fixed_Point_Type notices that the range is not
21254 -- analyzed, and uses this as an indication that it should complete
21255 -- work on the range (it will know the final small and size values).
21257 procedure Set_Fixed_Range
21258 (E : Entity_Id;
21259 Loc : Source_Ptr;
21260 Lo : Ureal;
21261 Hi : Ureal)
21263 S : constant Node_Id :=
21264 Make_Range (Loc,
21265 Low_Bound => Make_Real_Literal (Loc, Lo),
21266 High_Bound => Make_Real_Literal (Loc, Hi));
21267 begin
21268 Set_Scalar_Range (E, S);
21269 Set_Parent (S, E);
21271 -- Before the freeze point, the bounds of a fixed point are universal
21272 -- and carry the corresponding type.
21274 Set_Etype (Low_Bound (S), Universal_Real);
21275 Set_Etype (High_Bound (S), Universal_Real);
21276 end Set_Fixed_Range;
21278 ----------------------------------
21279 -- Set_Scalar_Range_For_Subtype --
21280 ----------------------------------
21282 procedure Set_Scalar_Range_For_Subtype
21283 (Def_Id : Entity_Id;
21284 R : Node_Id;
21285 Subt : Entity_Id)
21287 Kind : constant Entity_Kind := Ekind (Def_Id);
21289 begin
21290 -- Defend against previous error
21292 if Nkind (R) = N_Error then
21293 return;
21294 end if;
21296 Set_Scalar_Range (Def_Id, R);
21298 -- We need to link the range into the tree before resolving it so
21299 -- that types that are referenced, including importantly the subtype
21300 -- itself, are properly frozen (Freeze_Expression requires that the
21301 -- expression be properly linked into the tree). Of course if it is
21302 -- already linked in, then we do not disturb the current link.
21304 if No (Parent (R)) then
21305 Set_Parent (R, Def_Id);
21306 end if;
21308 -- Reset the kind of the subtype during analysis of the range, to
21309 -- catch possible premature use in the bounds themselves.
21311 Set_Ekind (Def_Id, E_Void);
21312 Process_Range_Expr_In_Decl (R, Subt, Subtyp => Def_Id);
21313 Set_Ekind (Def_Id, Kind);
21314 end Set_Scalar_Range_For_Subtype;
21316 --------------------------------------------------------
21317 -- Set_Stored_Constraint_From_Discriminant_Constraint --
21318 --------------------------------------------------------
21320 procedure Set_Stored_Constraint_From_Discriminant_Constraint
21321 (E : Entity_Id)
21323 begin
21324 -- Make sure set if encountered during Expand_To_Stored_Constraint
21326 Set_Stored_Constraint (E, No_Elist);
21328 -- Give it the right value
21330 if Is_Constrained (E) and then Has_Discriminants (E) then
21331 Set_Stored_Constraint (E,
21332 Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
21333 end if;
21334 end Set_Stored_Constraint_From_Discriminant_Constraint;
21336 -------------------------------------
21337 -- Signed_Integer_Type_Declaration --
21338 -------------------------------------
21340 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
21341 Implicit_Base : Entity_Id;
21342 Base_Typ : Entity_Id;
21343 Lo_Val : Uint;
21344 Hi_Val : Uint;
21345 Errs : Boolean := False;
21346 Lo : Node_Id;
21347 Hi : Node_Id;
21349 function Can_Derive_From (E : Entity_Id) return Boolean;
21350 -- Determine whether given bounds allow derivation from specified type
21352 procedure Check_Bound (Expr : Node_Id);
21353 -- Check bound to make sure it is integral and static. If not, post
21354 -- appropriate error message and set Errs flag
21356 ---------------------
21357 -- Can_Derive_From --
21358 ---------------------
21360 -- Note we check both bounds against both end values, to deal with
21361 -- strange types like ones with a range of 0 .. -12341234.
21363 function Can_Derive_From (E : Entity_Id) return Boolean is
21364 Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
21365 Hi : constant Uint := Expr_Value (Type_High_Bound (E));
21366 begin
21367 return Lo <= Lo_Val and then Lo_Val <= Hi
21368 and then
21369 Lo <= Hi_Val and then Hi_Val <= Hi;
21370 end Can_Derive_From;
21372 -----------------
21373 -- Check_Bound --
21374 -----------------
21376 procedure Check_Bound (Expr : Node_Id) is
21377 begin
21378 -- If a range constraint is used as an integer type definition, each
21379 -- bound of the range must be defined by a static expression of some
21380 -- integer type, but the two bounds need not have the same integer
21381 -- type (Negative bounds are allowed.) (RM 3.5.4)
21383 if not Is_Integer_Type (Etype (Expr)) then
21384 Error_Msg_N
21385 ("integer type definition bounds must be of integer type", Expr);
21386 Errs := True;
21388 elsif not Is_OK_Static_Expression (Expr) then
21389 Flag_Non_Static_Expr
21390 ("non-static expression used for integer type bound!", Expr);
21391 Errs := True;
21393 -- The bounds are folded into literals, and we set their type to be
21394 -- universal, to avoid typing difficulties: we cannot set the type
21395 -- of the literal to the new type, because this would be a forward
21396 -- reference for the back end, and if the original type is user-
21397 -- defined this can lead to spurious semantic errors (e.g. 2928-003).
21399 else
21400 if Is_Entity_Name (Expr) then
21401 Fold_Uint (Expr, Expr_Value (Expr), True);
21402 end if;
21404 Set_Etype (Expr, Universal_Integer);
21405 end if;
21406 end Check_Bound;
21408 -- Start of processing for Signed_Integer_Type_Declaration
21410 begin
21411 -- Create an anonymous base type
21413 Implicit_Base :=
21414 Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
21416 -- Analyze and check the bounds, they can be of any integer type
21418 Lo := Low_Bound (Def);
21419 Hi := High_Bound (Def);
21421 -- Arbitrarily use Integer as the type if either bound had an error
21423 if Hi = Error or else Lo = Error then
21424 Base_Typ := Any_Integer;
21425 Set_Error_Posted (T, True);
21427 -- Here both bounds are OK expressions
21429 else
21430 Analyze_And_Resolve (Lo, Any_Integer);
21431 Analyze_And_Resolve (Hi, Any_Integer);
21433 Check_Bound (Lo);
21434 Check_Bound (Hi);
21436 if Errs then
21437 Hi := Type_High_Bound (Standard_Long_Long_Integer);
21438 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
21439 end if;
21441 -- Find type to derive from
21443 Lo_Val := Expr_Value (Lo);
21444 Hi_Val := Expr_Value (Hi);
21446 if Can_Derive_From (Standard_Short_Short_Integer) then
21447 Base_Typ := Base_Type (Standard_Short_Short_Integer);
21449 elsif Can_Derive_From (Standard_Short_Integer) then
21450 Base_Typ := Base_Type (Standard_Short_Integer);
21452 elsif Can_Derive_From (Standard_Integer) then
21453 Base_Typ := Base_Type (Standard_Integer);
21455 elsif Can_Derive_From (Standard_Long_Integer) then
21456 Base_Typ := Base_Type (Standard_Long_Integer);
21458 elsif Can_Derive_From (Standard_Long_Long_Integer) then
21459 Check_Restriction (No_Long_Long_Integers, Def);
21460 Base_Typ := Base_Type (Standard_Long_Long_Integer);
21462 else
21463 Base_Typ := Base_Type (Standard_Long_Long_Integer);
21464 Error_Msg_N ("integer type definition bounds out of range", Def);
21465 Hi := Type_High_Bound (Standard_Long_Long_Integer);
21466 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
21467 end if;
21468 end if;
21470 -- Complete both implicit base and declared first subtype entities. The
21471 -- inheritance of the rep item chain ensures that SPARK-related pragmas
21472 -- are not clobbered when the signed integer type acts as a full view of
21473 -- a private type.
21475 Set_Etype (Implicit_Base, Base_Typ);
21476 Set_Size_Info (Implicit_Base, Base_Typ);
21477 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
21478 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
21479 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
21481 Set_Ekind (T, E_Signed_Integer_Subtype);
21482 Set_Etype (T, Implicit_Base);
21483 Set_Size_Info (T, Implicit_Base);
21484 Inherit_Rep_Item_Chain (T, Implicit_Base);
21485 Set_Scalar_Range (T, Def);
21486 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
21487 Set_Is_Constrained (T);
21488 end Signed_Integer_Type_Declaration;
21490 end Sem_Ch3;