Daily bump.
[official-gcc.git] / gcc / ada / sem_ch3.adb
blobf163b1581b24b8f622bbb13c9c0e555e01e9d6bd
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_Ch12; use Sem_Ch12;
62 with Sem_Ch13; use Sem_Ch13;
63 with Sem_Dim; use Sem_Dim;
64 with Sem_Disp; use Sem_Disp;
65 with Sem_Dist; use Sem_Dist;
66 with Sem_Elim; use Sem_Elim;
67 with Sem_Eval; use Sem_Eval;
68 with Sem_Mech; use Sem_Mech;
69 with Sem_Prag; use Sem_Prag;
70 with Sem_Res; use Sem_Res;
71 with Sem_Smem; use Sem_Smem;
72 with Sem_Type; use Sem_Type;
73 with Sem_Util; use Sem_Util;
74 with Sem_Warn; use Sem_Warn;
75 with Stand; use Stand;
76 with Sinfo; use Sinfo;
77 with Sinput; use Sinput;
78 with Snames; use Snames;
79 with Targparm; use Targparm;
80 with Tbuild; use Tbuild;
81 with Ttypes; use Ttypes;
82 with Uintp; use Uintp;
83 with Urealp; use Urealp;
85 package body Sem_Ch3 is
87 -----------------------
88 -- Local Subprograms --
89 -----------------------
91 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id);
92 -- Ada 2005 (AI-251): Add the tag components corresponding to all the
93 -- abstract interface types implemented by a record type or a derived
94 -- record type.
96 procedure Analyze_Object_Contract (Obj_Id : Entity_Id);
97 -- Analyze all delayed pragmas chained on the contract of object Obj_Id as
98 -- if they appeared at the end of the declarative region. The pragmas to be
99 -- considered are:
100 -- Async_Readers
101 -- Async_Writers
102 -- Effective_Reads
103 -- Effective_Writes
104 -- Part_Of
106 procedure Build_Derived_Type
107 (N : Node_Id;
108 Parent_Type : Entity_Id;
109 Derived_Type : Entity_Id;
110 Is_Completion : Boolean;
111 Derive_Subps : Boolean := True);
112 -- Create and decorate a Derived_Type given the Parent_Type entity. N is
113 -- the N_Full_Type_Declaration node containing the derived type definition.
114 -- Parent_Type is the entity for the parent type in the derived type
115 -- definition and Derived_Type the actual derived type. Is_Completion must
116 -- be set to False if Derived_Type is the N_Defining_Identifier node in N
117 -- (i.e. Derived_Type = Defining_Identifier (N)). In this case N is not the
118 -- completion of a private type declaration. If Is_Completion is set to
119 -- True, N is the completion of a private type declaration and Derived_Type
120 -- is different from the defining identifier inside N (i.e. Derived_Type /=
121 -- Defining_Identifier (N)). Derive_Subps indicates whether the parent
122 -- subprograms should be derived. The only case where this parameter is
123 -- False is when Build_Derived_Type is recursively called to process an
124 -- implicit derived full type for a type derived from a private type (in
125 -- that case the subprograms must only be derived for the private view of
126 -- the type).
128 -- ??? These flags need a bit of re-examination and re-documentation:
129 -- ??? are they both necessary (both seem related to the recursion)?
131 procedure Build_Derived_Access_Type
132 (N : Node_Id;
133 Parent_Type : Entity_Id;
134 Derived_Type : Entity_Id);
135 -- Subsidiary procedure to Build_Derived_Type. For a derived access type,
136 -- create an implicit base if the parent type is constrained or if the
137 -- subtype indication has a constraint.
139 procedure Build_Derived_Array_Type
140 (N : Node_Id;
141 Parent_Type : Entity_Id;
142 Derived_Type : Entity_Id);
143 -- Subsidiary procedure to Build_Derived_Type. For a derived array type,
144 -- create an implicit base if the parent type is constrained or if the
145 -- subtype indication has a constraint.
147 procedure Build_Derived_Concurrent_Type
148 (N : Node_Id;
149 Parent_Type : Entity_Id;
150 Derived_Type : Entity_Id);
151 -- Subsidiary procedure to Build_Derived_Type. For a derived task or
152 -- protected type, inherit entries and protected subprograms, check
153 -- legality of discriminant constraints if any.
155 procedure Build_Derived_Enumeration_Type
156 (N : Node_Id;
157 Parent_Type : Entity_Id;
158 Derived_Type : Entity_Id);
159 -- Subsidiary procedure to Build_Derived_Type. For a derived enumeration
160 -- type, we must create a new list of literals. Types derived from
161 -- Character and [Wide_]Wide_Character are special-cased.
163 procedure Build_Derived_Numeric_Type
164 (N : Node_Id;
165 Parent_Type : Entity_Id;
166 Derived_Type : Entity_Id);
167 -- Subsidiary procedure to Build_Derived_Type. For numeric types, create
168 -- an anonymous base type, and propagate constraint to subtype if needed.
170 procedure Build_Derived_Private_Type
171 (N : Node_Id;
172 Parent_Type : Entity_Id;
173 Derived_Type : Entity_Id;
174 Is_Completion : Boolean;
175 Derive_Subps : Boolean := True);
176 -- Subsidiary procedure to Build_Derived_Type. This procedure is complex
177 -- because the parent may or may not have a completion, and the derivation
178 -- may itself be a completion.
180 procedure Build_Derived_Record_Type
181 (N : Node_Id;
182 Parent_Type : Entity_Id;
183 Derived_Type : Entity_Id;
184 Derive_Subps : Boolean := True);
185 -- Subsidiary procedure used for tagged and untagged record types
186 -- by Build_Derived_Type and Analyze_Private_Extension_Declaration.
187 -- All parameters are as in Build_Derived_Type except that N, in
188 -- addition to being an N_Full_Type_Declaration node, can also be an
189 -- N_Private_Extension_Declaration node. See the definition of this routine
190 -- for much more info. Derive_Subps indicates whether subprograms should be
191 -- derived from the parent type. The only case where Derive_Subps is False
192 -- is for an implicit derived full type for a type derived from a private
193 -- type (see Build_Derived_Type).
195 procedure Build_Discriminal (Discrim : Entity_Id);
196 -- Create the discriminal corresponding to discriminant Discrim, that is
197 -- the parameter corresponding to Discrim to be used in initialization
198 -- procedures for the type where Discrim is a discriminant. Discriminals
199 -- are not used during semantic analysis, and are not fully defined
200 -- entities until expansion. Thus they are not given a scope until
201 -- initialization procedures are built.
203 function Build_Discriminant_Constraints
204 (T : Entity_Id;
205 Def : Node_Id;
206 Derived_Def : Boolean := False) return Elist_Id;
207 -- Validate discriminant constraints and return the list of the constraints
208 -- in order of discriminant declarations, where T is the discriminated
209 -- unconstrained type. Def is the N_Subtype_Indication node where the
210 -- discriminants constraints for T are specified. Derived_Def is True
211 -- when building the discriminant constraints in a derived type definition
212 -- of the form "type D (...) is new T (xxx)". In this case T is the parent
213 -- type and Def is the constraint "(xxx)" on T and this routine sets the
214 -- Corresponding_Discriminant field of the discriminants in the derived
215 -- type D to point to the corresponding discriminants in the parent type T.
217 procedure Build_Discriminated_Subtype
218 (T : Entity_Id;
219 Def_Id : Entity_Id;
220 Elist : Elist_Id;
221 Related_Nod : Node_Id;
222 For_Access : Boolean := False);
223 -- Subsidiary procedure to Constrain_Discriminated_Type and to
224 -- Process_Incomplete_Dependents. Given
226 -- T (a possibly discriminated base type)
227 -- Def_Id (a very partially built subtype for T),
229 -- the call completes Def_Id to be the appropriate E_*_Subtype.
231 -- The Elist is the list of discriminant constraints if any (it is set
232 -- to No_Elist if T is not a discriminated type, and to an empty list if
233 -- T has discriminants but there are no discriminant constraints). The
234 -- Related_Nod is the same as Decl_Node in Create_Constrained_Components.
235 -- The For_Access says whether or not this subtype is really constraining
236 -- an access type. That is its sole purpose is the designated type of an
237 -- access type -- in which case a Private_Subtype Is_For_Access_Subtype
238 -- is built to avoid freezing T when the access subtype is frozen.
240 function Build_Scalar_Bound
241 (Bound : Node_Id;
242 Par_T : Entity_Id;
243 Der_T : Entity_Id) return Node_Id;
244 -- The bounds of a derived scalar type are conversions of the bounds of
245 -- the parent type. Optimize the representation if the bounds are literals.
246 -- Needs a more complete spec--what are the parameters exactly, and what
247 -- exactly is the returned value, and how is Bound affected???
249 procedure Build_Underlying_Full_View
250 (N : Node_Id;
251 Typ : Entity_Id;
252 Par : Entity_Id);
253 -- If the completion of a private type is itself derived from a private
254 -- type, or if the full view of a private subtype is itself private, the
255 -- back-end has no way to compute the actual size of this type. We build
256 -- an internal subtype declaration of the proper parent type to convey
257 -- this information. This extra mechanism is needed because a full
258 -- view cannot itself have a full view (it would get clobbered during
259 -- view exchanges).
261 procedure Check_Access_Discriminant_Requires_Limited
262 (D : Node_Id;
263 Loc : Node_Id);
264 -- Check the restriction that the type to which an access discriminant
265 -- belongs must be a concurrent type or a descendant of a type with
266 -- the reserved word 'limited' in its declaration.
268 procedure Check_Anonymous_Access_Components
269 (Typ_Decl : Node_Id;
270 Typ : Entity_Id;
271 Prev : Entity_Id;
272 Comp_List : Node_Id);
273 -- Ada 2005 AI-382: an access component in a record definition can refer to
274 -- the enclosing record, in which case it denotes the type itself, and not
275 -- the current instance of the type. We create an anonymous access type for
276 -- the component, and flag it as an access to a component, so accessibility
277 -- checks are properly performed on it. The declaration of the access type
278 -- is placed ahead of that of the record to prevent order-of-elaboration
279 -- circularity issues in Gigi. We create an incomplete type for the record
280 -- declaration, which is the designated type of the anonymous access.
282 procedure Check_Delta_Expression (E : Node_Id);
283 -- Check that the expression represented by E is suitable for use as a
284 -- delta expression, i.e. it is of real type and is static.
286 procedure Check_Digits_Expression (E : Node_Id);
287 -- Check that the expression represented by E is suitable for use as a
288 -- digits expression, i.e. it is of integer type, positive and static.
290 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id);
291 -- Validate the initialization of an object declaration. T is the required
292 -- type, and Exp is the initialization expression.
294 procedure Check_Interfaces (N : Node_Id; Def : Node_Id);
295 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
297 procedure Check_Or_Process_Discriminants
298 (N : Node_Id;
299 T : Entity_Id;
300 Prev : Entity_Id := Empty);
301 -- If N is the full declaration of the completion T of an incomplete or
302 -- private type, check its discriminants (which are already known to be
303 -- conformant with those of the partial view, see Find_Type_Name),
304 -- otherwise process them. Prev is the entity of the partial declaration,
305 -- if any.
307 procedure Check_Real_Bound (Bound : Node_Id);
308 -- Check given bound for being of real type and static. If not, post an
309 -- appropriate message, and rewrite the bound with the real literal zero.
311 procedure Constant_Redeclaration
312 (Id : Entity_Id;
313 N : Node_Id;
314 T : out Entity_Id);
315 -- Various checks on legality of full declaration of deferred constant.
316 -- Id is the entity for the redeclaration, N is the N_Object_Declaration,
317 -- node. The caller has not yet set any attributes of this entity.
319 function Contain_Interface
320 (Iface : Entity_Id;
321 Ifaces : Elist_Id) return Boolean;
322 -- Ada 2005: Determine whether Iface is present in the list Ifaces
324 procedure Convert_Scalar_Bounds
325 (N : Node_Id;
326 Parent_Type : Entity_Id;
327 Derived_Type : Entity_Id;
328 Loc : Source_Ptr);
329 -- For derived scalar types, convert the bounds in the type definition to
330 -- the derived type, and complete their analysis. Given a constraint of the
331 -- form ".. new T range Lo .. Hi", Lo and Hi are analyzed and resolved with
332 -- T'Base, the parent_type. The bounds of the derived type (the anonymous
333 -- base) are copies of Lo and Hi. Finally, the bounds of the derived
334 -- subtype are conversions of those bounds to the derived_type, so that
335 -- their typing is consistent.
337 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id);
338 -- Copies attributes from array base type T2 to array base type T1. Copies
339 -- only attributes that apply to base types, but not subtypes.
341 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id);
342 -- Copies attributes from array subtype T2 to array subtype T1. Copies
343 -- attributes that apply to both subtypes and base types.
345 procedure Create_Constrained_Components
346 (Subt : Entity_Id;
347 Decl_Node : Node_Id;
348 Typ : Entity_Id;
349 Constraints : Elist_Id);
350 -- Build the list of entities for a constrained discriminated record
351 -- subtype. If a component depends on a discriminant, replace its subtype
352 -- using the discriminant values in the discriminant constraint. Subt
353 -- is the defining identifier for the subtype whose list of constrained
354 -- entities we will create. Decl_Node is the type declaration node where
355 -- we will attach all the itypes created. Typ is the base discriminated
356 -- type for the subtype Subt. Constraints is the list of discriminant
357 -- constraints for Typ.
359 function Constrain_Component_Type
360 (Comp : Entity_Id;
361 Constrained_Typ : Entity_Id;
362 Related_Node : Node_Id;
363 Typ : Entity_Id;
364 Constraints : Elist_Id) return Entity_Id;
365 -- Given a discriminated base type Typ, a list of discriminant constraints,
366 -- Constraints, for Typ and a component Comp of Typ, create and return the
367 -- type corresponding to Etype (Comp) where all discriminant references
368 -- are replaced with the corresponding constraint. If Etype (Comp) contains
369 -- no discriminant references then it is returned as-is. Constrained_Typ
370 -- is the final constrained subtype to which the constrained component
371 -- belongs. Related_Node is the node where we attach all created itypes.
373 procedure Constrain_Access
374 (Def_Id : in out Entity_Id;
375 S : Node_Id;
376 Related_Nod : Node_Id);
377 -- Apply a list of constraints to an access type. If Def_Id is empty, it is
378 -- an anonymous type created for a subtype indication. In that case it is
379 -- created in the procedure and attached to Related_Nod.
381 procedure Constrain_Array
382 (Def_Id : in out Entity_Id;
383 SI : Node_Id;
384 Related_Nod : Node_Id;
385 Related_Id : Entity_Id;
386 Suffix : Character);
387 -- Apply a list of index constraints to an unconstrained array type. The
388 -- first parameter is the entity for the resulting subtype. A value of
389 -- Empty for Def_Id indicates that an implicit type must be created, but
390 -- creation is delayed (and must be done by this procedure) because other
391 -- subsidiary implicit types must be created first (which is why Def_Id
392 -- is an in/out parameter). The second parameter is a subtype indication
393 -- node for the constrained array to be created (e.g. something of the
394 -- form string (1 .. 10)). Related_Nod gives the place where this type
395 -- has to be inserted in the tree. The Related_Id and Suffix parameters
396 -- are used to build the associated Implicit type name.
398 procedure Constrain_Concurrent
399 (Def_Id : in out Entity_Id;
400 SI : Node_Id;
401 Related_Nod : Node_Id;
402 Related_Id : Entity_Id;
403 Suffix : Character);
404 -- Apply list of discriminant constraints to an unconstrained concurrent
405 -- type.
407 -- SI is the N_Subtype_Indication node containing the constraint and
408 -- the unconstrained type to constrain.
410 -- Def_Id is the entity for the resulting constrained subtype. A value
411 -- of Empty for Def_Id indicates that an implicit type must be created,
412 -- but creation is delayed (and must be done by this procedure) because
413 -- other subsidiary implicit types must be created first (which is why
414 -- Def_Id is an in/out parameter).
416 -- Related_Nod gives the place where this type has to be inserted
417 -- in the tree.
419 -- The last two arguments are used to create its external name if needed.
421 function Constrain_Corresponding_Record
422 (Prot_Subt : Entity_Id;
423 Corr_Rec : Entity_Id;
424 Related_Nod : Node_Id) return Entity_Id;
425 -- When constraining a protected type or task type with discriminants,
426 -- constrain the corresponding record with the same discriminant values.
428 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id);
429 -- Constrain a decimal fixed point type with a digits constraint and/or a
430 -- range constraint, and build E_Decimal_Fixed_Point_Subtype entity.
432 procedure Constrain_Discriminated_Type
433 (Def_Id : Entity_Id;
434 S : Node_Id;
435 Related_Nod : Node_Id;
436 For_Access : Boolean := False);
437 -- Process discriminant constraints of composite type. Verify that values
438 -- have been provided for all discriminants, that the original type is
439 -- unconstrained, and that the types of the supplied expressions match
440 -- the discriminant types. The first three parameters are like in routine
441 -- Constrain_Concurrent. See Build_Discriminated_Subtype for an explanation
442 -- of For_Access.
444 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id);
445 -- Constrain an enumeration type with a range constraint. This is identical
446 -- to Constrain_Integer, but for the Ekind of the resulting subtype.
448 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id);
449 -- Constrain a floating point type with either a digits constraint
450 -- and/or a range constraint, building a E_Floating_Point_Subtype.
452 procedure Constrain_Index
453 (Index : Node_Id;
454 S : Node_Id;
455 Related_Nod : Node_Id;
456 Related_Id : Entity_Id;
457 Suffix : Character;
458 Suffix_Index : Nat);
459 -- Process an index constraint S in a constrained array declaration. The
460 -- constraint can be a subtype name, or a range with or without an explicit
461 -- subtype mark. The index is the corresponding index of the unconstrained
462 -- array. The Related_Id and Suffix parameters are used to build the
463 -- associated Implicit type name.
465 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id);
466 -- Build subtype of a signed or modular integer type
468 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id);
469 -- Constrain an ordinary fixed point type with a range constraint, and
470 -- build an E_Ordinary_Fixed_Point_Subtype entity.
472 procedure Copy_And_Swap (Priv, Full : Entity_Id);
473 -- Copy the Priv entity into the entity of its full declaration then swap
474 -- the two entities in such a manner that the former private type is now
475 -- seen as a full type.
477 procedure Decimal_Fixed_Point_Type_Declaration
478 (T : Entity_Id;
479 Def : Node_Id);
480 -- Create a new decimal fixed point type, and apply the constraint to
481 -- obtain a subtype of this new type.
483 procedure Complete_Private_Subtype
484 (Priv : Entity_Id;
485 Full : Entity_Id;
486 Full_Base : Entity_Id;
487 Related_Nod : Node_Id);
488 -- Complete the implicit full view of a private subtype by setting the
489 -- appropriate semantic fields. If the full view of the parent is a record
490 -- type, build constrained components of subtype.
492 procedure Derive_Progenitor_Subprograms
493 (Parent_Type : Entity_Id;
494 Tagged_Type : Entity_Id);
495 -- Ada 2005 (AI-251): To complete type derivation, collect the primitive
496 -- operations of progenitors of Tagged_Type, and replace the subsidiary
497 -- subtypes with Tagged_Type, to build the specs of the inherited interface
498 -- primitives. The derived primitives are aliased to those of the
499 -- interface. This routine takes care also of transferring to the full view
500 -- subprograms associated with the partial view of Tagged_Type that cover
501 -- interface primitives.
503 procedure Derived_Standard_Character
504 (N : Node_Id;
505 Parent_Type : Entity_Id;
506 Derived_Type : Entity_Id);
507 -- Subsidiary procedure to Build_Derived_Enumeration_Type which handles
508 -- derivations from types Standard.Character and Standard.Wide_Character.
510 procedure Derived_Type_Declaration
511 (T : Entity_Id;
512 N : Node_Id;
513 Is_Completion : Boolean);
514 -- Process a derived type declaration. Build_Derived_Type is invoked
515 -- to process the actual derived type definition. Parameters N and
516 -- Is_Completion have the same meaning as in Build_Derived_Type.
517 -- T is the N_Defining_Identifier for the entity defined in the
518 -- N_Full_Type_Declaration node N, that is T is the derived type.
520 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id);
521 -- Insert each literal in symbol table, as an overloadable identifier. Each
522 -- enumeration type is mapped into a sequence of integers, and each literal
523 -- is defined as a constant with integer value. If any of the literals are
524 -- character literals, the type is a character type, which means that
525 -- strings are legal aggregates for arrays of components of the type.
527 function Expand_To_Stored_Constraint
528 (Typ : Entity_Id;
529 Constraint : Elist_Id) return Elist_Id;
530 -- Given a constraint (i.e. a list of expressions) on the discriminants of
531 -- Typ, expand it into a constraint on the stored discriminants and return
532 -- the new list of expressions constraining the stored discriminants.
534 function Find_Type_Of_Object
535 (Obj_Def : Node_Id;
536 Related_Nod : Node_Id) return Entity_Id;
537 -- Get type entity for object referenced by Obj_Def, attaching the implicit
538 -- types generated to Related_Nod.
540 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id);
541 -- Create a new float and apply the constraint to obtain subtype of it
543 function Has_Range_Constraint (N : Node_Id) return Boolean;
544 -- Given an N_Subtype_Indication node N, return True if a range constraint
545 -- is present, either directly, or as part of a digits or delta constraint.
546 -- In addition, a digits constraint in the decimal case returns True, since
547 -- it establishes a default range if no explicit range is present.
549 function Inherit_Components
550 (N : Node_Id;
551 Parent_Base : Entity_Id;
552 Derived_Base : Entity_Id;
553 Is_Tagged : Boolean;
554 Inherit_Discr : Boolean;
555 Discs : Elist_Id) return Elist_Id;
556 -- Called from Build_Derived_Record_Type to inherit the components of
557 -- Parent_Base (a base type) into the Derived_Base (the derived base type).
558 -- For more information on derived types and component inheritance please
559 -- consult the comment above the body of Build_Derived_Record_Type.
561 -- N is the original derived type declaration
563 -- Is_Tagged is set if we are dealing with tagged types
565 -- If Inherit_Discr is set, Derived_Base inherits its discriminants from
566 -- Parent_Base, otherwise no discriminants are inherited.
568 -- Discs gives the list of constraints that apply to Parent_Base in the
569 -- derived type declaration. If Discs is set to No_Elist, then we have
570 -- the following situation:
572 -- type Parent (D1..Dn : ..) is [tagged] record ...;
573 -- type Derived is new Parent [with ...];
575 -- which gets treated as
577 -- type Derived (D1..Dn : ..) is new Parent (D1,..,Dn) [with ...];
579 -- For untagged types the returned value is an association list. The list
580 -- starts from the association (Parent_Base => Derived_Base), and then it
581 -- contains a sequence of the associations of the form
583 -- (Old_Component => New_Component),
585 -- where Old_Component is the Entity_Id of a component in Parent_Base and
586 -- New_Component is the Entity_Id of the corresponding component in
587 -- Derived_Base. For untagged records, this association list is needed when
588 -- copying the record declaration for the derived base. In the tagged case
589 -- the value returned is irrelevant.
591 procedure Inherit_Predicate_Flags (Subt, Par : Entity_Id);
592 -- Propagate static and dynamic predicate flags from a parent to the
593 -- subtype in a subtype declaration with and without constraints.
595 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean;
596 -- Subsidiary to Check_Abstract_Overriding and Derive_Subprogram.
597 -- Determine whether subprogram Subp is a procedure subject to pragma
598 -- Extensions_Visible with value False and has at least one controlling
599 -- parameter of mode OUT.
601 function Is_Valid_Constraint_Kind
602 (T_Kind : Type_Kind;
603 Constraint_Kind : Node_Kind) return Boolean;
604 -- Returns True if it is legal to apply the given kind of constraint to the
605 -- given kind of type (index constraint to an array type, for example).
607 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id);
608 -- Create new modular type. Verify that modulus is in bounds
610 procedure New_Concatenation_Op (Typ : Entity_Id);
611 -- Create an abbreviated declaration for an operator in order to
612 -- materialize concatenation on array types.
614 procedure Ordinary_Fixed_Point_Type_Declaration
615 (T : Entity_Id;
616 Def : Node_Id);
617 -- Create a new ordinary fixed point type, and apply the constraint to
618 -- obtain subtype of it.
620 procedure Prepare_Private_Subtype_Completion
621 (Id : Entity_Id;
622 Related_Nod : Node_Id);
623 -- Id is a subtype of some private type. Creates the full declaration
624 -- associated with Id whenever possible, i.e. when the full declaration
625 -- of the base type is already known. Records each subtype into
626 -- Private_Dependents of the base type.
628 procedure Process_Incomplete_Dependents
629 (N : Node_Id;
630 Full_T : Entity_Id;
631 Inc_T : Entity_Id);
632 -- Process all entities that depend on an incomplete type. There include
633 -- subtypes, subprogram types that mention the incomplete type in their
634 -- profiles, and subprogram with access parameters that designate the
635 -- incomplete type.
637 -- Inc_T is the defining identifier of an incomplete type declaration, its
638 -- Ekind is E_Incomplete_Type.
640 -- N is the corresponding N_Full_Type_Declaration for Inc_T.
642 -- Full_T is N's defining identifier.
644 -- Subtypes of incomplete types with discriminants are completed when the
645 -- parent type is. This is simpler than private subtypes, because they can
646 -- only appear in the same scope, and there is no need to exchange views.
647 -- Similarly, access_to_subprogram types may have a parameter or a return
648 -- type that is an incomplete type, and that must be replaced with the
649 -- full type.
651 -- If the full type is tagged, subprogram with access parameters that
652 -- designated the incomplete may be primitive operations of the full type,
653 -- and have to be processed accordingly.
655 procedure Process_Real_Range_Specification (Def : Node_Id);
656 -- Given the type definition for a real type, this procedure processes and
657 -- checks the real range specification of this type definition if one is
658 -- present. If errors are found, error messages are posted, and the
659 -- Real_Range_Specification of Def is reset to Empty.
661 procedure Propagate_Default_Init_Cond_Attributes
662 (From_Typ : Entity_Id;
663 To_Typ : Entity_Id;
664 Parent_To_Derivation : Boolean := False;
665 Private_To_Full_View : Boolean := False);
666 -- Subsidiary to routines Build_Derived_Type and Process_Full_View. Inherit
667 -- all attributes related to pragma Default_Initial_Condition from From_Typ
668 -- to To_Typ. Flag Parent_To_Derivation should be set when the context is
669 -- the creation of a derived type. Flag Private_To_Full_View should be set
670 -- when processing both views of a private type.
672 procedure Record_Type_Declaration
673 (T : Entity_Id;
674 N : Node_Id;
675 Prev : Entity_Id);
676 -- Process a record type declaration (for both untagged and tagged
677 -- records). Parameters T and N are exactly like in procedure
678 -- Derived_Type_Declaration, except that no flag Is_Completion is needed
679 -- for this routine. If this is the completion of an incomplete type
680 -- declaration, Prev is the entity of the incomplete declaration, used for
681 -- cross-referencing. Otherwise Prev = T.
683 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id);
684 -- This routine is used to process the actual record type definition (both
685 -- for untagged and tagged records). Def is a record type definition node.
686 -- This procedure analyzes the components in this record type definition.
687 -- Prev_T is the entity for the enclosing record type. It is provided so
688 -- that its Has_Task flag can be set if any of the component have Has_Task
689 -- set. If the declaration is the completion of an incomplete type
690 -- declaration, Prev_T is the original incomplete type, whose full view is
691 -- the record type.
693 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id);
694 -- Subsidiary to Build_Derived_Record_Type. For untagged records, we
695 -- build a copy of the declaration tree of the parent, and we create
696 -- independently the list of components for the derived type. Semantic
697 -- information uses the component entities, but record representation
698 -- clauses are validated on the declaration tree. This procedure replaces
699 -- discriminants and components in the declaration with those that have
700 -- been created by Inherit_Components.
702 procedure Set_Fixed_Range
703 (E : Entity_Id;
704 Loc : Source_Ptr;
705 Lo : Ureal;
706 Hi : Ureal);
707 -- Build a range node with the given bounds and set it as the Scalar_Range
708 -- of the given fixed-point type entity. Loc is the source location used
709 -- for the constructed range. See body for further details.
711 procedure Set_Scalar_Range_For_Subtype
712 (Def_Id : Entity_Id;
713 R : Node_Id;
714 Subt : Entity_Id);
715 -- This routine is used to set the scalar range field for a subtype given
716 -- Def_Id, the entity for the subtype, and R, the range expression for the
717 -- scalar range. Subt provides the parent subtype to be used to analyze,
718 -- resolve, and check the given range.
720 procedure Set_Default_SSO (T : Entity_Id);
721 -- T is the entity for an array or record being declared. This procedure
722 -- sets the flags SSO_Set_Low_By_Default/SSO_Set_High_By_Default according
723 -- to the setting of Opt.Default_SSO.
725 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id);
726 -- Create a new signed integer entity, and apply the constraint to obtain
727 -- the required first named subtype of this type.
729 procedure Set_Stored_Constraint_From_Discriminant_Constraint
730 (E : Entity_Id);
731 -- E is some record type. This routine computes E's Stored_Constraint
732 -- from its Discriminant_Constraint.
734 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id);
735 -- Check that an entity in a list of progenitors is an interface,
736 -- emit error otherwise.
738 -----------------------
739 -- Access_Definition --
740 -----------------------
742 function Access_Definition
743 (Related_Nod : Node_Id;
744 N : Node_Id) return Entity_Id
746 Anon_Type : Entity_Id;
747 Anon_Scope : Entity_Id;
748 Desig_Type : Entity_Id;
749 Enclosing_Prot_Type : Entity_Id := Empty;
751 begin
752 Check_SPARK_05_Restriction ("access type is not allowed", N);
754 if Is_Entry (Current_Scope)
755 and then Is_Task_Type (Etype (Scope (Current_Scope)))
756 then
757 Error_Msg_N ("task entries cannot have access parameters", N);
758 return Empty;
759 end if;
761 -- Ada 2005: For an object declaration the corresponding anonymous
762 -- type is declared in the current scope.
764 -- If the access definition is the return type of another access to
765 -- function, scope is the current one, because it is the one of the
766 -- current type declaration, except for the pathological case below.
768 if Nkind_In (Related_Nod, N_Object_Declaration,
769 N_Access_Function_Definition)
770 then
771 Anon_Scope := Current_Scope;
773 -- A pathological case: function returning access functions that
774 -- return access functions, etc. Each anonymous access type created
775 -- is in the enclosing scope of the outermost function.
777 declare
778 Par : Node_Id;
780 begin
781 Par := Related_Nod;
782 while Nkind_In (Par, N_Access_Function_Definition,
783 N_Access_Definition)
784 loop
785 Par := Parent (Par);
786 end loop;
788 if Nkind (Par) = N_Function_Specification then
789 Anon_Scope := Scope (Defining_Entity (Par));
790 end if;
791 end;
793 -- For the anonymous function result case, retrieve the scope of the
794 -- function specification's associated entity rather than using the
795 -- current scope. The current scope will be the function itself if the
796 -- formal part is currently being analyzed, but will be the parent scope
797 -- in the case of a parameterless function, and we always want to use
798 -- the function's parent scope. Finally, if the function is a child
799 -- unit, we must traverse the tree to retrieve the proper entity.
801 elsif Nkind (Related_Nod) = N_Function_Specification
802 and then Nkind (Parent (N)) /= N_Parameter_Specification
803 then
804 -- If the current scope is a protected type, the anonymous access
805 -- is associated with one of the protected operations, and must
806 -- be available in the scope that encloses the protected declaration.
807 -- Otherwise the type is in the scope enclosing the subprogram.
809 -- If the function has formals, The return type of a subprogram
810 -- declaration is analyzed in the scope of the subprogram (see
811 -- Process_Formals) and thus the protected type, if present, is
812 -- the scope of the current function scope.
814 if Ekind (Current_Scope) = E_Protected_Type then
815 Enclosing_Prot_Type := Current_Scope;
817 elsif Ekind (Current_Scope) = E_Function
818 and then Ekind (Scope (Current_Scope)) = E_Protected_Type
819 then
820 Enclosing_Prot_Type := Scope (Current_Scope);
821 end if;
823 if Present (Enclosing_Prot_Type) then
824 Anon_Scope := Scope (Enclosing_Prot_Type);
826 else
827 Anon_Scope := Scope (Defining_Entity (Related_Nod));
828 end if;
830 -- For an access type definition, if the current scope is a child
831 -- unit it is the scope of the type.
833 elsif Is_Compilation_Unit (Current_Scope) then
834 Anon_Scope := Current_Scope;
836 -- For access formals, access components, and access discriminants, the
837 -- scope is that of the enclosing declaration,
839 else
840 Anon_Scope := Scope (Current_Scope);
841 end if;
843 Anon_Type :=
844 Create_Itype
845 (E_Anonymous_Access_Type, Related_Nod, Scope_Id => Anon_Scope);
847 if All_Present (N)
848 and then Ada_Version >= Ada_2005
849 then
850 Error_Msg_N ("ALL is not permitted for anonymous access types", N);
851 end if;
853 -- Ada 2005 (AI-254): In case of anonymous access to subprograms call
854 -- the corresponding semantic routine
856 if Present (Access_To_Subprogram_Definition (N)) then
858 -- Compiler runtime units are compiled in Ada 2005 mode when building
859 -- the runtime library but must also be compilable in Ada 95 mode
860 -- (when bootstrapping the compiler).
862 Check_Compiler_Unit ("anonymous access to subprogram", N);
864 Access_Subprogram_Declaration
865 (T_Name => Anon_Type,
866 T_Def => Access_To_Subprogram_Definition (N));
868 if Ekind (Anon_Type) = E_Access_Protected_Subprogram_Type then
869 Set_Ekind
870 (Anon_Type, E_Anonymous_Access_Protected_Subprogram_Type);
871 else
872 Set_Ekind (Anon_Type, E_Anonymous_Access_Subprogram_Type);
873 end if;
875 Set_Can_Use_Internal_Rep
876 (Anon_Type, not Always_Compatible_Rep_On_Target);
878 -- If the anonymous access is associated with a protected operation,
879 -- create a reference to it after the enclosing protected definition
880 -- because the itype will be used in the subsequent bodies.
882 -- If the anonymous access itself is protected, a full type
883 -- declaratiton will be created for it, so that the equivalent
884 -- record type can be constructed. For further details, see
885 -- Replace_Anonymous_Access_To_Protected-Subprogram.
887 if Ekind (Current_Scope) = E_Protected_Type
888 and then not Protected_Present (Access_To_Subprogram_Definition (N))
889 then
890 Build_Itype_Reference (Anon_Type, Parent (Current_Scope));
891 end if;
893 return Anon_Type;
894 end if;
896 Find_Type (Subtype_Mark (N));
897 Desig_Type := Entity (Subtype_Mark (N));
899 Set_Directly_Designated_Type (Anon_Type, Desig_Type);
900 Set_Etype (Anon_Type, Anon_Type);
902 -- Make sure the anonymous access type has size and alignment fields
903 -- set, as required by gigi. This is necessary in the case of the
904 -- Task_Body_Procedure.
906 if not Has_Private_Component (Desig_Type) then
907 Layout_Type (Anon_Type);
908 end if;
910 -- Ada 2005 (AI-231): Ada 2005 semantics for anonymous access differs
911 -- from Ada 95 semantics. In Ada 2005, anonymous access must specify if
912 -- the null value is allowed. In Ada 95 the null value is never allowed.
914 if Ada_Version >= Ada_2005 then
915 Set_Can_Never_Be_Null (Anon_Type, Null_Exclusion_Present (N));
916 else
917 Set_Can_Never_Be_Null (Anon_Type, True);
918 end if;
920 -- The anonymous access type is as public as the discriminated type or
921 -- subprogram that defines it. It is imported (for back-end purposes)
922 -- if the designated type is.
924 Set_Is_Public (Anon_Type, Is_Public (Scope (Anon_Type)));
926 -- Ada 2005 (AI-231): Propagate the access-constant attribute
928 Set_Is_Access_Constant (Anon_Type, Constant_Present (N));
930 -- The context is either a subprogram declaration, object declaration,
931 -- or an access discriminant, in a private or a full type declaration.
932 -- In the case of a subprogram, if the designated type is incomplete,
933 -- the operation will be a primitive operation of the full type, to be
934 -- updated subsequently. If the type is imported through a limited_with
935 -- clause, the subprogram is not a primitive operation of the type
936 -- (which is declared elsewhere in some other scope).
938 if Ekind (Desig_Type) = E_Incomplete_Type
939 and then not From_Limited_With (Desig_Type)
940 and then Is_Overloadable (Current_Scope)
941 then
942 Append_Elmt (Current_Scope, Private_Dependents (Desig_Type));
943 Set_Has_Delayed_Freeze (Current_Scope);
944 end if;
946 -- Ada 2005: If the designated type is an interface that may contain
947 -- tasks, create a Master entity for the declaration. This must be done
948 -- before expansion of the full declaration, because the declaration may
949 -- include an expression that is an allocator, whose expansion needs the
950 -- proper Master for the created tasks.
952 if Nkind (Related_Nod) = N_Object_Declaration and then Expander_Active
953 then
954 if Is_Interface (Desig_Type) and then Is_Limited_Record (Desig_Type)
955 then
956 Build_Class_Wide_Master (Anon_Type);
958 -- Similarly, if the type is an anonymous access that designates
959 -- tasks, create a master entity for it in the current context.
961 elsif Has_Task (Desig_Type) and then Comes_From_Source (Related_Nod)
962 then
963 Build_Master_Entity (Defining_Identifier (Related_Nod));
964 Build_Master_Renaming (Anon_Type);
965 end if;
966 end if;
968 -- For a private component of a protected type, it is imperative that
969 -- the back-end elaborate the type immediately after the protected
970 -- declaration, because this type will be used in the declarations
971 -- created for the component within each protected body, so we must
972 -- create an itype reference for it now.
974 if Nkind (Parent (Related_Nod)) = N_Protected_Definition then
975 Build_Itype_Reference (Anon_Type, Parent (Parent (Related_Nod)));
977 -- Similarly, if the access definition is the return result of a
978 -- function, create an itype reference for it because it will be used
979 -- within the function body. For a regular function that is not a
980 -- compilation unit, insert reference after the declaration. For a
981 -- protected operation, insert it after the enclosing protected type
982 -- declaration. In either case, do not create a reference for a type
983 -- obtained through a limited_with clause, because this would introduce
984 -- semantic dependencies.
986 -- Similarly, do not create a reference if the designated type is a
987 -- generic formal, because no use of it will reach the backend.
989 elsif Nkind (Related_Nod) = N_Function_Specification
990 and then not From_Limited_With (Desig_Type)
991 and then not Is_Generic_Type (Desig_Type)
992 then
993 if Present (Enclosing_Prot_Type) then
994 Build_Itype_Reference (Anon_Type, Parent (Enclosing_Prot_Type));
996 elsif Is_List_Member (Parent (Related_Nod))
997 and then Nkind (Parent (N)) /= N_Parameter_Specification
998 then
999 Build_Itype_Reference (Anon_Type, Parent (Related_Nod));
1000 end if;
1002 -- Finally, create an itype reference for an object declaration of an
1003 -- anonymous access type. This is strictly necessary only for deferred
1004 -- constants, but in any case will avoid out-of-scope problems in the
1005 -- back-end.
1007 elsif Nkind (Related_Nod) = N_Object_Declaration then
1008 Build_Itype_Reference (Anon_Type, Related_Nod);
1009 end if;
1011 return Anon_Type;
1012 end Access_Definition;
1014 -----------------------------------
1015 -- Access_Subprogram_Declaration --
1016 -----------------------------------
1018 procedure Access_Subprogram_Declaration
1019 (T_Name : Entity_Id;
1020 T_Def : Node_Id)
1022 procedure Check_For_Premature_Usage (Def : Node_Id);
1023 -- Check that type T_Name is not used, directly or recursively, as a
1024 -- parameter or a return type in Def. Def is either a subtype, an
1025 -- access_definition, or an access_to_subprogram_definition.
1027 -------------------------------
1028 -- Check_For_Premature_Usage --
1029 -------------------------------
1031 procedure Check_For_Premature_Usage (Def : Node_Id) is
1032 Param : Node_Id;
1034 begin
1035 -- Check for a subtype mark
1037 if Nkind (Def) in N_Has_Etype then
1038 if Etype (Def) = T_Name then
1039 Error_Msg_N
1040 ("type& cannot be used before end of its declaration", Def);
1041 end if;
1043 -- If this is not a subtype, then this is an access_definition
1045 elsif Nkind (Def) = N_Access_Definition then
1046 if Present (Access_To_Subprogram_Definition (Def)) then
1047 Check_For_Premature_Usage
1048 (Access_To_Subprogram_Definition (Def));
1049 else
1050 Check_For_Premature_Usage (Subtype_Mark (Def));
1051 end if;
1053 -- The only cases left are N_Access_Function_Definition and
1054 -- N_Access_Procedure_Definition.
1056 else
1057 if Present (Parameter_Specifications (Def)) then
1058 Param := First (Parameter_Specifications (Def));
1059 while Present (Param) loop
1060 Check_For_Premature_Usage (Parameter_Type (Param));
1061 Param := Next (Param);
1062 end loop;
1063 end if;
1065 if Nkind (Def) = N_Access_Function_Definition then
1066 Check_For_Premature_Usage (Result_Definition (Def));
1067 end if;
1068 end if;
1069 end Check_For_Premature_Usage;
1071 -- Local variables
1073 Formals : constant List_Id := Parameter_Specifications (T_Def);
1074 Formal : Entity_Id;
1075 D_Ityp : Node_Id;
1076 Desig_Type : constant Entity_Id :=
1077 Create_Itype (E_Subprogram_Type, Parent (T_Def));
1079 -- Start of processing for Access_Subprogram_Declaration
1081 begin
1082 Check_SPARK_05_Restriction ("access type is not allowed", T_Def);
1084 -- Associate the Itype node with the inner full-type declaration or
1085 -- subprogram spec or entry body. This is required to handle nested
1086 -- anonymous declarations. For example:
1088 -- procedure P
1089 -- (X : access procedure
1090 -- (Y : access procedure
1091 -- (Z : access T)))
1093 D_Ityp := Associated_Node_For_Itype (Desig_Type);
1094 while not (Nkind_In (D_Ityp, N_Full_Type_Declaration,
1095 N_Private_Type_Declaration,
1096 N_Private_Extension_Declaration,
1097 N_Procedure_Specification,
1098 N_Function_Specification,
1099 N_Entry_Body)
1101 or else
1102 Nkind_In (D_Ityp, N_Object_Declaration,
1103 N_Object_Renaming_Declaration,
1104 N_Formal_Object_Declaration,
1105 N_Formal_Type_Declaration,
1106 N_Task_Type_Declaration,
1107 N_Protected_Type_Declaration))
1108 loop
1109 D_Ityp := Parent (D_Ityp);
1110 pragma Assert (D_Ityp /= Empty);
1111 end loop;
1113 Set_Associated_Node_For_Itype (Desig_Type, D_Ityp);
1115 if Nkind_In (D_Ityp, N_Procedure_Specification,
1116 N_Function_Specification)
1117 then
1118 Set_Scope (Desig_Type, Scope (Defining_Entity (D_Ityp)));
1120 elsif Nkind_In (D_Ityp, N_Full_Type_Declaration,
1121 N_Object_Declaration,
1122 N_Object_Renaming_Declaration,
1123 N_Formal_Type_Declaration)
1124 then
1125 Set_Scope (Desig_Type, Scope (Defining_Identifier (D_Ityp)));
1126 end if;
1128 if Nkind (T_Def) = N_Access_Function_Definition then
1129 if Nkind (Result_Definition (T_Def)) = N_Access_Definition then
1130 declare
1131 Acc : constant Node_Id := Result_Definition (T_Def);
1133 begin
1134 if Present (Access_To_Subprogram_Definition (Acc))
1135 and then
1136 Protected_Present (Access_To_Subprogram_Definition (Acc))
1137 then
1138 Set_Etype
1139 (Desig_Type,
1140 Replace_Anonymous_Access_To_Protected_Subprogram
1141 (T_Def));
1143 else
1144 Set_Etype
1145 (Desig_Type,
1146 Access_Definition (T_Def, Result_Definition (T_Def)));
1147 end if;
1148 end;
1150 else
1151 Analyze (Result_Definition (T_Def));
1153 declare
1154 Typ : constant Entity_Id := Entity (Result_Definition (T_Def));
1156 begin
1157 -- If a null exclusion is imposed on the result type, then
1158 -- create a null-excluding itype (an access subtype) and use
1159 -- it as the function's Etype.
1161 if Is_Access_Type (Typ)
1162 and then Null_Exclusion_In_Return_Present (T_Def)
1163 then
1164 Set_Etype (Desig_Type,
1165 Create_Null_Excluding_Itype
1166 (T => Typ,
1167 Related_Nod => T_Def,
1168 Scope_Id => Current_Scope));
1170 else
1171 if From_Limited_With (Typ) then
1173 -- AI05-151: Incomplete types are allowed in all basic
1174 -- declarations, including access to subprograms.
1176 if Ada_Version >= Ada_2012 then
1177 null;
1179 else
1180 Error_Msg_NE
1181 ("illegal use of incomplete type&",
1182 Result_Definition (T_Def), Typ);
1183 end if;
1185 elsif Ekind (Current_Scope) = E_Package
1186 and then In_Private_Part (Current_Scope)
1187 then
1188 if Ekind (Typ) = E_Incomplete_Type then
1189 Append_Elmt (Desig_Type, Private_Dependents (Typ));
1191 elsif Is_Class_Wide_Type (Typ)
1192 and then Ekind (Etype (Typ)) = E_Incomplete_Type
1193 then
1194 Append_Elmt
1195 (Desig_Type, Private_Dependents (Etype (Typ)));
1196 end if;
1197 end if;
1199 Set_Etype (Desig_Type, Typ);
1200 end if;
1201 end;
1202 end if;
1204 if not (Is_Type (Etype (Desig_Type))) then
1205 Error_Msg_N
1206 ("expect type in function specification",
1207 Result_Definition (T_Def));
1208 end if;
1210 else
1211 Set_Etype (Desig_Type, Standard_Void_Type);
1212 end if;
1214 if Present (Formals) then
1215 Push_Scope (Desig_Type);
1217 -- Some special tests here. These special tests can be removed
1218 -- if and when Itypes always have proper parent pointers to their
1219 -- declarations???
1221 -- Special test 1) Link defining_identifier of formals. Required by
1222 -- First_Formal to provide its functionality.
1224 declare
1225 F : Node_Id;
1227 begin
1228 F := First (Formals);
1230 -- In ASIS mode, the access_to_subprogram may be analyzed twice,
1231 -- when it is part of an unconstrained type and subtype expansion
1232 -- is disabled. To avoid back-end problems with shared profiles,
1233 -- use previous subprogram type as the designated type, and then
1234 -- remove scope added above.
1236 if ASIS_Mode and then Present (Scope (Defining_Identifier (F)))
1237 then
1238 Set_Etype (T_Name, T_Name);
1239 Init_Size_Align (T_Name);
1240 Set_Directly_Designated_Type (T_Name,
1241 Scope (Defining_Identifier (F)));
1242 End_Scope;
1243 return;
1244 end if;
1246 while Present (F) loop
1247 if No (Parent (Defining_Identifier (F))) then
1248 Set_Parent (Defining_Identifier (F), F);
1249 end if;
1251 Next (F);
1252 end loop;
1253 end;
1255 Process_Formals (Formals, Parent (T_Def));
1257 -- Special test 2) End_Scope requires that the parent pointer be set
1258 -- to something reasonable, but Itypes don't have parent pointers. So
1259 -- we set it and then unset it ???
1261 Set_Parent (Desig_Type, T_Name);
1262 End_Scope;
1263 Set_Parent (Desig_Type, Empty);
1264 end if;
1266 -- Check for premature usage of the type being defined
1268 Check_For_Premature_Usage (T_Def);
1270 -- The return type and/or any parameter type may be incomplete. Mark the
1271 -- subprogram_type as depending on the incomplete type, so that it can
1272 -- be updated when the full type declaration is seen. This only applies
1273 -- to incomplete types declared in some enclosing scope, not to limited
1274 -- views from other packages.
1276 -- Prior to Ada 2012, access to functions can only have in_parameters.
1278 if Present (Formals) then
1279 Formal := First_Formal (Desig_Type);
1280 while Present (Formal) loop
1281 if Ekind (Formal) /= E_In_Parameter
1282 and then Nkind (T_Def) = N_Access_Function_Definition
1283 and then Ada_Version < Ada_2012
1284 then
1285 Error_Msg_N ("functions can only have IN parameters", Formal);
1286 end if;
1288 if Ekind (Etype (Formal)) = E_Incomplete_Type
1289 and then In_Open_Scopes (Scope (Etype (Formal)))
1290 then
1291 Append_Elmt (Desig_Type, Private_Dependents (Etype (Formal)));
1292 Set_Has_Delayed_Freeze (Desig_Type);
1293 end if;
1295 Next_Formal (Formal);
1296 end loop;
1297 end if;
1299 -- Check whether an indirect call without actuals may be possible. This
1300 -- is used when resolving calls whose result is then indexed.
1302 May_Need_Actuals (Desig_Type);
1304 -- If the return type is incomplete, this is legal as long as the type
1305 -- is declared in the current scope and will be completed in it (rather
1306 -- than being part of limited view).
1308 if Ekind (Etype (Desig_Type)) = E_Incomplete_Type
1309 and then not Has_Delayed_Freeze (Desig_Type)
1310 and then In_Open_Scopes (Scope (Etype (Desig_Type)))
1311 then
1312 Append_Elmt (Desig_Type, Private_Dependents (Etype (Desig_Type)));
1313 Set_Has_Delayed_Freeze (Desig_Type);
1314 end if;
1316 Check_Delayed_Subprogram (Desig_Type);
1318 if Protected_Present (T_Def) then
1319 Set_Ekind (T_Name, E_Access_Protected_Subprogram_Type);
1320 Set_Convention (Desig_Type, Convention_Protected);
1321 else
1322 Set_Ekind (T_Name, E_Access_Subprogram_Type);
1323 end if;
1325 Set_Can_Use_Internal_Rep (T_Name, not Always_Compatible_Rep_On_Target);
1327 Set_Etype (T_Name, T_Name);
1328 Init_Size_Align (T_Name);
1329 Set_Directly_Designated_Type (T_Name, Desig_Type);
1331 Generate_Reference_To_Formals (T_Name);
1333 -- Ada 2005 (AI-231): Propagate the null-excluding attribute
1335 Set_Can_Never_Be_Null (T_Name, Null_Exclusion_Present (T_Def));
1337 Check_Restriction (No_Access_Subprograms, T_Def);
1338 end Access_Subprogram_Declaration;
1340 ----------------------------
1341 -- Access_Type_Declaration --
1342 ----------------------------
1344 procedure Access_Type_Declaration (T : Entity_Id; Def : Node_Id) is
1345 P : constant Node_Id := Parent (Def);
1346 S : constant Node_Id := Subtype_Indication (Def);
1348 Full_Desig : Entity_Id;
1350 begin
1351 Check_SPARK_05_Restriction ("access type is not allowed", Def);
1353 -- Check for permissible use of incomplete type
1355 if Nkind (S) /= N_Subtype_Indication then
1356 Analyze (S);
1358 if Ekind (Root_Type (Entity (S))) = E_Incomplete_Type then
1359 Set_Directly_Designated_Type (T, Entity (S));
1361 -- If the designated type is a limited view, we cannot tell if
1362 -- the full view contains tasks, and there is no way to handle
1363 -- that full view in a client. We create a master entity for the
1364 -- scope, which will be used when a client determines that one
1365 -- is needed.
1367 if From_Limited_With (Entity (S))
1368 and then not Is_Class_Wide_Type (Entity (S))
1369 then
1370 Set_Ekind (T, E_Access_Type);
1371 Build_Master_Entity (T);
1372 Build_Master_Renaming (T);
1373 end if;
1375 else
1376 Set_Directly_Designated_Type (T, Process_Subtype (S, P, T, 'P'));
1377 end if;
1379 -- If the access definition is of the form: ACCESS NOT NULL ..
1380 -- the subtype indication must be of an access type. Create
1381 -- a null-excluding subtype of it.
1383 if Null_Excluding_Subtype (Def) then
1384 if not Is_Access_Type (Entity (S)) then
1385 Error_Msg_N ("null exclusion must apply to access type", Def);
1387 else
1388 declare
1389 Loc : constant Source_Ptr := Sloc (S);
1390 Decl : Node_Id;
1391 Nam : constant Entity_Id := Make_Temporary (Loc, 'S');
1393 begin
1394 Decl :=
1395 Make_Subtype_Declaration (Loc,
1396 Defining_Identifier => Nam,
1397 Subtype_Indication =>
1398 New_Occurrence_Of (Entity (S), Loc));
1399 Set_Null_Exclusion_Present (Decl);
1400 Insert_Before (Parent (Def), Decl);
1401 Analyze (Decl);
1402 Set_Entity (S, Nam);
1403 end;
1404 end if;
1405 end if;
1407 else
1408 Set_Directly_Designated_Type (T,
1409 Process_Subtype (S, P, T, 'P'));
1410 end if;
1412 if All_Present (Def) or Constant_Present (Def) then
1413 Set_Ekind (T, E_General_Access_Type);
1414 else
1415 Set_Ekind (T, E_Access_Type);
1416 end if;
1418 Full_Desig := Designated_Type (T);
1420 if Base_Type (Full_Desig) = T then
1421 Error_Msg_N ("access type cannot designate itself", S);
1423 -- In Ada 2005, the type may have a limited view through some unit in
1424 -- its own context, allowing the following circularity that cannot be
1425 -- detected earlier.
1427 elsif Is_Class_Wide_Type (Full_Desig) and then Etype (Full_Desig) = T
1428 then
1429 Error_Msg_N
1430 ("access type cannot designate its own classwide type", S);
1432 -- Clean up indication of tagged status to prevent cascaded errors
1434 Set_Is_Tagged_Type (T, False);
1435 end if;
1437 Set_Etype (T, T);
1439 -- If the type has appeared already in a with_type clause, it is frozen
1440 -- and the pointer size is already set. Else, initialize.
1442 if not From_Limited_With (T) then
1443 Init_Size_Align (T);
1444 end if;
1446 -- Note that Has_Task is always false, since the access type itself
1447 -- is not a task type. See Einfo for more description on this point.
1448 -- Exactly the same consideration applies to Has_Controlled_Component
1449 -- and to Has_Protected.
1451 Set_Has_Task (T, False);
1452 Set_Has_Controlled_Component (T, False);
1453 Set_Has_Protected (T, False);
1455 -- Initialize field Finalization_Master explicitly to Empty, to avoid
1456 -- problems where an incomplete view of this entity has been previously
1457 -- established by a limited with and an overlaid version of this field
1458 -- (Stored_Constraint) was initialized for the incomplete view.
1460 -- This reset is performed in most cases except where the access type
1461 -- has been created for the purposes of allocating or deallocating a
1462 -- build-in-place object. Such access types have explicitly set pools
1463 -- and finalization masters.
1465 if No (Associated_Storage_Pool (T)) then
1466 Set_Finalization_Master (T, Empty);
1467 end if;
1469 -- Ada 2005 (AI-231): Propagate the null-excluding and access-constant
1470 -- attributes
1472 Set_Can_Never_Be_Null (T, Null_Exclusion_Present (Def));
1473 Set_Is_Access_Constant (T, Constant_Present (Def));
1474 end Access_Type_Declaration;
1476 ----------------------------------
1477 -- Add_Interface_Tag_Components --
1478 ----------------------------------
1480 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id) is
1481 Loc : constant Source_Ptr := Sloc (N);
1482 L : List_Id;
1483 Last_Tag : Node_Id;
1485 procedure Add_Tag (Iface : Entity_Id);
1486 -- Add tag for one of the progenitor interfaces
1488 -------------
1489 -- Add_Tag --
1490 -------------
1492 procedure Add_Tag (Iface : Entity_Id) is
1493 Decl : Node_Id;
1494 Def : Node_Id;
1495 Tag : Entity_Id;
1496 Offset : Entity_Id;
1498 begin
1499 pragma Assert (Is_Tagged_Type (Iface) and then Is_Interface (Iface));
1501 -- This is a reasonable place to propagate predicates
1503 if Has_Predicates (Iface) then
1504 Set_Has_Predicates (Typ);
1505 end if;
1507 Def :=
1508 Make_Component_Definition (Loc,
1509 Aliased_Present => True,
1510 Subtype_Indication =>
1511 New_Occurrence_Of (RTE (RE_Interface_Tag), Loc));
1513 Tag := Make_Temporary (Loc, 'V');
1515 Decl :=
1516 Make_Component_Declaration (Loc,
1517 Defining_Identifier => Tag,
1518 Component_Definition => Def);
1520 Analyze_Component_Declaration (Decl);
1522 Set_Analyzed (Decl);
1523 Set_Ekind (Tag, E_Component);
1524 Set_Is_Tag (Tag);
1525 Set_Is_Aliased (Tag);
1526 Set_Related_Type (Tag, Iface);
1527 Init_Component_Location (Tag);
1529 pragma Assert (Is_Frozen (Iface));
1531 Set_DT_Entry_Count (Tag,
1532 DT_Entry_Count (First_Entity (Iface)));
1534 if No (Last_Tag) then
1535 Prepend (Decl, L);
1536 else
1537 Insert_After (Last_Tag, Decl);
1538 end if;
1540 Last_Tag := Decl;
1542 -- If the ancestor has discriminants we need to give special support
1543 -- to store the offset_to_top value of the secondary dispatch tables.
1544 -- For this purpose we add a supplementary component just after the
1545 -- field that contains the tag associated with each secondary DT.
1547 if Typ /= Etype (Typ) and then Has_Discriminants (Etype (Typ)) then
1548 Def :=
1549 Make_Component_Definition (Loc,
1550 Subtype_Indication =>
1551 New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
1553 Offset := Make_Temporary (Loc, 'V');
1555 Decl :=
1556 Make_Component_Declaration (Loc,
1557 Defining_Identifier => Offset,
1558 Component_Definition => Def);
1560 Analyze_Component_Declaration (Decl);
1562 Set_Analyzed (Decl);
1563 Set_Ekind (Offset, E_Component);
1564 Set_Is_Aliased (Offset);
1565 Set_Related_Type (Offset, Iface);
1566 Init_Component_Location (Offset);
1567 Insert_After (Last_Tag, Decl);
1568 Last_Tag := Decl;
1569 end if;
1570 end Add_Tag;
1572 -- Local variables
1574 Elmt : Elmt_Id;
1575 Ext : Node_Id;
1576 Comp : Node_Id;
1578 -- Start of processing for Add_Interface_Tag_Components
1580 begin
1581 if not RTE_Available (RE_Interface_Tag) then
1582 Error_Msg
1583 ("(Ada 2005) interface types not supported by this run-time!",
1584 Sloc (N));
1585 return;
1586 end if;
1588 if Ekind (Typ) /= E_Record_Type
1589 or else (Is_Concurrent_Record_Type (Typ)
1590 and then Is_Empty_List (Abstract_Interface_List (Typ)))
1591 or else (not Is_Concurrent_Record_Type (Typ)
1592 and then No (Interfaces (Typ))
1593 and then Is_Empty_Elmt_List (Interfaces (Typ)))
1594 then
1595 return;
1596 end if;
1598 -- Find the current last tag
1600 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1601 Ext := Record_Extension_Part (Type_Definition (N));
1602 else
1603 pragma Assert (Nkind (Type_Definition (N)) = N_Record_Definition);
1604 Ext := Type_Definition (N);
1605 end if;
1607 Last_Tag := Empty;
1609 if not (Present (Component_List (Ext))) then
1610 Set_Null_Present (Ext, False);
1611 L := New_List;
1612 Set_Component_List (Ext,
1613 Make_Component_List (Loc,
1614 Component_Items => L,
1615 Null_Present => False));
1616 else
1617 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1618 L := Component_Items
1619 (Component_List
1620 (Record_Extension_Part
1621 (Type_Definition (N))));
1622 else
1623 L := Component_Items
1624 (Component_List
1625 (Type_Definition (N)));
1626 end if;
1628 -- Find the last tag component
1630 Comp := First (L);
1631 while Present (Comp) loop
1632 if Nkind (Comp) = N_Component_Declaration
1633 and then Is_Tag (Defining_Identifier (Comp))
1634 then
1635 Last_Tag := Comp;
1636 end if;
1638 Next (Comp);
1639 end loop;
1640 end if;
1642 -- At this point L references the list of components and Last_Tag
1643 -- references the current last tag (if any). Now we add the tag
1644 -- corresponding with all the interfaces that are not implemented
1645 -- by the parent.
1647 if Present (Interfaces (Typ)) then
1648 Elmt := First_Elmt (Interfaces (Typ));
1649 while Present (Elmt) loop
1650 Add_Tag (Node (Elmt));
1651 Next_Elmt (Elmt);
1652 end loop;
1653 end if;
1654 end Add_Interface_Tag_Components;
1656 -------------------------------------
1657 -- Add_Internal_Interface_Entities --
1658 -------------------------------------
1660 procedure Add_Internal_Interface_Entities (Tagged_Type : Entity_Id) is
1661 Elmt : Elmt_Id;
1662 Iface : Entity_Id;
1663 Iface_Elmt : Elmt_Id;
1664 Iface_Prim : Entity_Id;
1665 Ifaces_List : Elist_Id;
1666 New_Subp : Entity_Id := Empty;
1667 Prim : Entity_Id;
1668 Restore_Scope : Boolean := False;
1670 begin
1671 pragma Assert (Ada_Version >= Ada_2005
1672 and then Is_Record_Type (Tagged_Type)
1673 and then Is_Tagged_Type (Tagged_Type)
1674 and then Has_Interfaces (Tagged_Type)
1675 and then not Is_Interface (Tagged_Type));
1677 -- Ensure that the internal entities are added to the scope of the type
1679 if Scope (Tagged_Type) /= Current_Scope then
1680 Push_Scope (Scope (Tagged_Type));
1681 Restore_Scope := True;
1682 end if;
1684 Collect_Interfaces (Tagged_Type, Ifaces_List);
1686 Iface_Elmt := First_Elmt (Ifaces_List);
1687 while Present (Iface_Elmt) loop
1688 Iface := Node (Iface_Elmt);
1690 -- Originally we excluded here from this processing interfaces that
1691 -- are parents of Tagged_Type because their primitives are located
1692 -- in the primary dispatch table (and hence no auxiliary internal
1693 -- entities are required to handle secondary dispatch tables in such
1694 -- case). However, these auxiliary entities are also required to
1695 -- handle derivations of interfaces in formals of generics (see
1696 -- Derive_Subprograms).
1698 Elmt := First_Elmt (Primitive_Operations (Iface));
1699 while Present (Elmt) loop
1700 Iface_Prim := Node (Elmt);
1702 if not Is_Predefined_Dispatching_Operation (Iface_Prim) then
1703 Prim :=
1704 Find_Primitive_Covering_Interface
1705 (Tagged_Type => Tagged_Type,
1706 Iface_Prim => Iface_Prim);
1708 if No (Prim) and then Serious_Errors_Detected > 0 then
1709 goto Continue;
1710 end if;
1712 pragma Assert (Present (Prim));
1714 -- Ada 2012 (AI05-0197): If the name of the covering primitive
1715 -- differs from the name of the interface primitive then it is
1716 -- a private primitive inherited from a parent type. In such
1717 -- case, given that Tagged_Type covers the interface, the
1718 -- inherited private primitive becomes visible. For such
1719 -- purpose we add a new entity that renames the inherited
1720 -- private primitive.
1722 if Chars (Prim) /= Chars (Iface_Prim) then
1723 pragma Assert (Has_Suffix (Prim, 'P'));
1724 Derive_Subprogram
1725 (New_Subp => New_Subp,
1726 Parent_Subp => Iface_Prim,
1727 Derived_Type => Tagged_Type,
1728 Parent_Type => Iface);
1729 Set_Alias (New_Subp, Prim);
1730 Set_Is_Abstract_Subprogram
1731 (New_Subp, Is_Abstract_Subprogram (Prim));
1732 end if;
1734 Derive_Subprogram
1735 (New_Subp => New_Subp,
1736 Parent_Subp => Iface_Prim,
1737 Derived_Type => Tagged_Type,
1738 Parent_Type => Iface);
1740 -- Ada 2005 (AI-251): Decorate internal entity Iface_Subp
1741 -- associated with interface types. These entities are
1742 -- only registered in the list of primitives of its
1743 -- corresponding tagged type because they are only used
1744 -- to fill the contents of the secondary dispatch tables.
1745 -- Therefore they are removed from the homonym chains.
1747 Set_Is_Hidden (New_Subp);
1748 Set_Is_Internal (New_Subp);
1749 Set_Alias (New_Subp, Prim);
1750 Set_Is_Abstract_Subprogram
1751 (New_Subp, Is_Abstract_Subprogram (Prim));
1752 Set_Interface_Alias (New_Subp, Iface_Prim);
1754 -- If the returned type is an interface then propagate it to
1755 -- the returned type. Needed by the thunk to generate the code
1756 -- which displaces "this" to reference the corresponding
1757 -- secondary dispatch table in the returned object.
1759 if Is_Interface (Etype (Iface_Prim)) then
1760 Set_Etype (New_Subp, Etype (Iface_Prim));
1761 end if;
1763 -- Internal entities associated with interface types are only
1764 -- registered in the list of primitives of the tagged type.
1765 -- They are only used to fill the contents of the secondary
1766 -- dispatch tables. Therefore they are not needed in the
1767 -- homonym chains.
1769 Remove_Homonym (New_Subp);
1771 -- Hidden entities associated with interfaces must have set
1772 -- the Has_Delay_Freeze attribute to ensure that, in case
1773 -- of locally defined tagged types (or compiling with static
1774 -- dispatch tables generation disabled) the corresponding
1775 -- entry of the secondary dispatch table is filled when such
1776 -- an entity is frozen. This is an expansion activity that must
1777 -- be suppressed for ASIS because it leads to gigi elaboration
1778 -- issues in annotate mode.
1780 if not ASIS_Mode then
1781 Set_Has_Delayed_Freeze (New_Subp);
1782 end if;
1783 end if;
1785 <<Continue>>
1786 Next_Elmt (Elmt);
1787 end loop;
1789 Next_Elmt (Iface_Elmt);
1790 end loop;
1792 if Restore_Scope then
1793 Pop_Scope;
1794 end if;
1795 end Add_Internal_Interface_Entities;
1797 -----------------------------------
1798 -- Analyze_Component_Declaration --
1799 -----------------------------------
1801 procedure Analyze_Component_Declaration (N : Node_Id) is
1802 Loc : constant Source_Ptr := Sloc (Component_Definition (N));
1803 Id : constant Entity_Id := Defining_Identifier (N);
1804 E : constant Node_Id := Expression (N);
1805 Typ : constant Node_Id :=
1806 Subtype_Indication (Component_Definition (N));
1807 T : Entity_Id;
1808 P : Entity_Id;
1810 function Contains_POC (Constr : Node_Id) return Boolean;
1811 -- Determines whether a constraint uses the discriminant of a record
1812 -- type thus becoming a per-object constraint (POC).
1814 function Is_Known_Limited (Typ : Entity_Id) return Boolean;
1815 -- Typ is the type of the current component, check whether this type is
1816 -- a limited type. Used to validate declaration against that of
1817 -- enclosing record.
1819 ------------------
1820 -- Contains_POC --
1821 ------------------
1823 function Contains_POC (Constr : Node_Id) return Boolean is
1824 begin
1825 -- Prevent cascaded errors
1827 if Error_Posted (Constr) then
1828 return False;
1829 end if;
1831 case Nkind (Constr) is
1832 when N_Attribute_Reference =>
1833 return Attribute_Name (Constr) = Name_Access
1834 and then Prefix (Constr) = Scope (Entity (Prefix (Constr)));
1836 when N_Discriminant_Association =>
1837 return Denotes_Discriminant (Expression (Constr));
1839 when N_Identifier =>
1840 return Denotes_Discriminant (Constr);
1842 when N_Index_Or_Discriminant_Constraint =>
1843 declare
1844 IDC : Node_Id;
1846 begin
1847 IDC := First (Constraints (Constr));
1848 while Present (IDC) loop
1850 -- One per-object constraint is sufficient
1852 if Contains_POC (IDC) then
1853 return True;
1854 end if;
1856 Next (IDC);
1857 end loop;
1859 return False;
1860 end;
1862 when N_Range =>
1863 return Denotes_Discriminant (Low_Bound (Constr))
1864 or else
1865 Denotes_Discriminant (High_Bound (Constr));
1867 when N_Range_Constraint =>
1868 return Denotes_Discriminant (Range_Expression (Constr));
1870 when others =>
1871 return False;
1873 end case;
1874 end Contains_POC;
1876 ----------------------
1877 -- Is_Known_Limited --
1878 ----------------------
1880 function Is_Known_Limited (Typ : Entity_Id) return Boolean is
1881 P : constant Entity_Id := Etype (Typ);
1882 R : constant Entity_Id := Root_Type (Typ);
1884 begin
1885 if Is_Limited_Record (Typ) then
1886 return True;
1888 -- If the root type is limited (and not a limited interface)
1889 -- so is the current type
1891 elsif Is_Limited_Record (R)
1892 and then (not Is_Interface (R) or else not Is_Limited_Interface (R))
1893 then
1894 return True;
1896 -- Else the type may have a limited interface progenitor, but a
1897 -- limited record parent.
1899 elsif R /= P and then Is_Limited_Record (P) then
1900 return True;
1902 else
1903 return False;
1904 end if;
1905 end Is_Known_Limited;
1907 -- Start of processing for Analyze_Component_Declaration
1909 begin
1910 Generate_Definition (Id);
1911 Enter_Name (Id);
1913 if Present (Typ) then
1914 T := Find_Type_Of_Object
1915 (Subtype_Indication (Component_Definition (N)), N);
1917 if not Nkind_In (Typ, N_Identifier, N_Expanded_Name) then
1918 Check_SPARK_05_Restriction ("subtype mark required", Typ);
1919 end if;
1921 -- Ada 2005 (AI-230): Access Definition case
1923 else
1924 pragma Assert (Present
1925 (Access_Definition (Component_Definition (N))));
1927 T := Access_Definition
1928 (Related_Nod => N,
1929 N => Access_Definition (Component_Definition (N)));
1930 Set_Is_Local_Anonymous_Access (T);
1932 -- Ada 2005 (AI-254)
1934 if Present (Access_To_Subprogram_Definition
1935 (Access_Definition (Component_Definition (N))))
1936 and then Protected_Present (Access_To_Subprogram_Definition
1937 (Access_Definition
1938 (Component_Definition (N))))
1939 then
1940 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1941 end if;
1942 end if;
1944 -- If the subtype is a constrained subtype of the enclosing record,
1945 -- (which must have a partial view) the back-end does not properly
1946 -- handle the recursion. Rewrite the component declaration with an
1947 -- explicit subtype indication, which is acceptable to Gigi. We can copy
1948 -- the tree directly because side effects have already been removed from
1949 -- discriminant constraints.
1951 if Ekind (T) = E_Access_Subtype
1952 and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
1953 and then Comes_From_Source (T)
1954 and then Nkind (Parent (T)) = N_Subtype_Declaration
1955 and then Etype (Directly_Designated_Type (T)) = Current_Scope
1956 then
1957 Rewrite
1958 (Subtype_Indication (Component_Definition (N)),
1959 New_Copy_Tree (Subtype_Indication (Parent (T))));
1960 T := Find_Type_Of_Object
1961 (Subtype_Indication (Component_Definition (N)), N);
1962 end if;
1964 -- If the component declaration includes a default expression, then we
1965 -- check that the component is not of a limited type (RM 3.7(5)),
1966 -- and do the special preanalysis of the expression (see section on
1967 -- "Handling of Default and Per-Object Expressions" in the spec of
1968 -- package Sem).
1970 if Present (E) then
1971 Check_SPARK_05_Restriction ("default expression is not allowed", E);
1972 Preanalyze_Default_Expression (E, T);
1973 Check_Initialization (T, E);
1975 if Ada_Version >= Ada_2005
1976 and then Ekind (T) = E_Anonymous_Access_Type
1977 and then Etype (E) /= Any_Type
1978 then
1979 -- Check RM 3.9.2(9): "if the expected type for an expression is
1980 -- an anonymous access-to-specific tagged type, then the object
1981 -- designated by the expression shall not be dynamically tagged
1982 -- unless it is a controlling operand in a call on a dispatching
1983 -- operation"
1985 if Is_Tagged_Type (Directly_Designated_Type (T))
1986 and then
1987 Ekind (Directly_Designated_Type (T)) /= E_Class_Wide_Type
1988 and then
1989 Ekind (Directly_Designated_Type (Etype (E))) =
1990 E_Class_Wide_Type
1991 then
1992 Error_Msg_N
1993 ("access to specific tagged type required (RM 3.9.2(9))", E);
1994 end if;
1996 -- (Ada 2005: AI-230): Accessibility check for anonymous
1997 -- components
1999 if Type_Access_Level (Etype (E)) >
2000 Deepest_Type_Access_Level (T)
2001 then
2002 Error_Msg_N
2003 ("expression has deeper access level than component " &
2004 "(RM 3.10.2 (12.2))", E);
2005 end if;
2007 -- The initialization expression is a reference to an access
2008 -- discriminant. The type of the discriminant is always deeper
2009 -- than any access type.
2011 if Ekind (Etype (E)) = E_Anonymous_Access_Type
2012 and then Is_Entity_Name (E)
2013 and then Ekind (Entity (E)) = E_In_Parameter
2014 and then Present (Discriminal_Link (Entity (E)))
2015 then
2016 Error_Msg_N
2017 ("discriminant has deeper accessibility level than target",
2019 end if;
2020 end if;
2021 end if;
2023 -- The parent type may be a private view with unknown discriminants,
2024 -- and thus unconstrained. Regular components must be constrained.
2026 if not Is_Definite_Subtype (T) and then Chars (Id) /= Name_uParent then
2027 if Is_Class_Wide_Type (T) then
2028 Error_Msg_N
2029 ("class-wide subtype with unknown discriminants" &
2030 " in component declaration",
2031 Subtype_Indication (Component_Definition (N)));
2032 else
2033 Error_Msg_N
2034 ("unconstrained subtype in component declaration",
2035 Subtype_Indication (Component_Definition (N)));
2036 end if;
2038 -- Components cannot be abstract, except for the special case of
2039 -- the _Parent field (case of extending an abstract tagged type)
2041 elsif Is_Abstract_Type (T) and then Chars (Id) /= Name_uParent then
2042 Error_Msg_N ("type of a component cannot be abstract", N);
2043 end if;
2045 Set_Etype (Id, T);
2046 Set_Is_Aliased (Id, Aliased_Present (Component_Definition (N)));
2048 -- The component declaration may have a per-object constraint, set
2049 -- the appropriate flag in the defining identifier of the subtype.
2051 if Present (Subtype_Indication (Component_Definition (N))) then
2052 declare
2053 Sindic : constant Node_Id :=
2054 Subtype_Indication (Component_Definition (N));
2055 begin
2056 if Nkind (Sindic) = N_Subtype_Indication
2057 and then Present (Constraint (Sindic))
2058 and then Contains_POC (Constraint (Sindic))
2059 then
2060 Set_Has_Per_Object_Constraint (Id);
2061 end if;
2062 end;
2063 end if;
2065 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
2066 -- out some static checks.
2068 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (T) then
2069 Null_Exclusion_Static_Checks (N);
2070 end if;
2072 -- If this component is private (or depends on a private type), flag the
2073 -- record type to indicate that some operations are not available.
2075 P := Private_Component (T);
2077 if Present (P) then
2079 -- Check for circular definitions
2081 if P = Any_Type then
2082 Set_Etype (Id, Any_Type);
2084 -- There is a gap in the visibility of operations only if the
2085 -- component type is not defined in the scope of the record type.
2087 elsif Scope (P) = Scope (Current_Scope) then
2088 null;
2090 elsif Is_Limited_Type (P) then
2091 Set_Is_Limited_Composite (Current_Scope);
2093 else
2094 Set_Is_Private_Composite (Current_Scope);
2095 end if;
2096 end if;
2098 if P /= Any_Type
2099 and then Is_Limited_Type (T)
2100 and then Chars (Id) /= Name_uParent
2101 and then Is_Tagged_Type (Current_Scope)
2102 then
2103 if Is_Derived_Type (Current_Scope)
2104 and then not Is_Known_Limited (Current_Scope)
2105 then
2106 Error_Msg_N
2107 ("extension of nonlimited type cannot have limited components",
2110 if Is_Interface (Root_Type (Current_Scope)) then
2111 Error_Msg_N
2112 ("\limitedness is not inherited from limited interface", N);
2113 Error_Msg_N ("\add LIMITED to type indication", N);
2114 end if;
2116 Explain_Limited_Type (T, N);
2117 Set_Etype (Id, Any_Type);
2118 Set_Is_Limited_Composite (Current_Scope, False);
2120 elsif not Is_Derived_Type (Current_Scope)
2121 and then not Is_Limited_Record (Current_Scope)
2122 and then not Is_Concurrent_Type (Current_Scope)
2123 then
2124 Error_Msg_N
2125 ("nonlimited tagged type cannot have limited components", N);
2126 Explain_Limited_Type (T, N);
2127 Set_Etype (Id, Any_Type);
2128 Set_Is_Limited_Composite (Current_Scope, False);
2129 end if;
2130 end if;
2132 -- If the component is an unconstrained task or protected type with
2133 -- discriminants, the component and the enclosing record are limited
2134 -- and the component is constrained by its default values. Compute
2135 -- its actual subtype, else it may be allocated the maximum size by
2136 -- the backend, and possibly overflow.
2138 if Is_Concurrent_Type (T)
2139 and then not Is_Constrained (T)
2140 and then Has_Discriminants (T)
2141 and then not Has_Discriminants (Current_Scope)
2142 then
2143 declare
2144 Act_T : constant Entity_Id := Build_Default_Subtype (T, N);
2146 begin
2147 Set_Etype (Id, Act_T);
2149 -- Rewrite component definition to use the constrained subtype
2151 Rewrite (Component_Definition (N),
2152 Make_Component_Definition (Loc,
2153 Subtype_Indication => New_Occurrence_Of (Act_T, Loc)));
2154 end;
2155 end if;
2157 Set_Original_Record_Component (Id, Id);
2159 if Has_Aspects (N) then
2160 Analyze_Aspect_Specifications (N, Id);
2161 end if;
2163 Analyze_Dimension (N);
2164 end Analyze_Component_Declaration;
2166 --------------------------
2167 -- Analyze_Declarations --
2168 --------------------------
2170 procedure Analyze_Declarations (L : List_Id) is
2171 Decl : Node_Id;
2173 procedure Adjust_Decl;
2174 -- Adjust Decl not to include implicit label declarations, since these
2175 -- have strange Sloc values that result in elaboration check problems.
2176 -- (They have the sloc of the label as found in the source, and that
2177 -- is ahead of the current declarative part).
2179 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id);
2180 -- Determine whether Body_Decl denotes the body of a late controlled
2181 -- primitive (either Initialize, Adjust or Finalize). If this is the
2182 -- case, add a proper spec if the body lacks one. The spec is inserted
2183 -- before Body_Decl and immedately analyzed.
2185 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id);
2186 -- Spec_Id is the entity of a package that may define abstract states.
2187 -- If the states have visible refinement, remove the visibility of each
2188 -- constituent at the end of the package body declarations.
2190 -----------------
2191 -- Adjust_Decl --
2192 -----------------
2194 procedure Adjust_Decl is
2195 begin
2196 while Present (Prev (Decl))
2197 and then Nkind (Decl) = N_Implicit_Label_Declaration
2198 loop
2199 Prev (Decl);
2200 end loop;
2201 end Adjust_Decl;
2203 --------------------------------------
2204 -- Handle_Late_Controlled_Primitive --
2205 --------------------------------------
2207 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id) is
2208 Body_Spec : constant Node_Id := Specification (Body_Decl);
2209 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2210 Loc : constant Source_Ptr := Sloc (Body_Id);
2211 Params : constant List_Id :=
2212 Parameter_Specifications (Body_Spec);
2213 Spec : Node_Id;
2214 Spec_Id : Entity_Id;
2215 Typ : Node_Id;
2217 begin
2218 -- Consider only procedure bodies whose name matches one of the three
2219 -- controlled primitives.
2221 if Nkind (Body_Spec) /= N_Procedure_Specification
2222 or else not Nam_In (Chars (Body_Id), Name_Adjust,
2223 Name_Finalize,
2224 Name_Initialize)
2225 then
2226 return;
2228 -- A controlled primitive must have exactly one formal which is not
2229 -- an anonymous access type.
2231 elsif List_Length (Params) /= 1 then
2232 return;
2233 end if;
2235 Typ := Parameter_Type (First (Params));
2237 if Nkind (Typ) = N_Access_Definition then
2238 return;
2239 end if;
2241 Find_Type (Typ);
2243 -- The type of the formal must be derived from [Limited_]Controlled
2245 if not Is_Controlled (Entity (Typ)) then
2246 return;
2247 end if;
2249 -- Check whether a specification exists for this body. We do not
2250 -- analyze the spec of the body in full, because it will be analyzed
2251 -- again when the body is properly analyzed, and we cannot create
2252 -- duplicate entries in the formals chain. We look for an explicit
2253 -- specification because the body may be an overriding operation and
2254 -- an inherited spec may be present.
2256 Spec_Id := Current_Entity (Body_Id);
2258 while Present (Spec_Id) loop
2259 if Ekind_In (Spec_Id, E_Procedure, E_Generic_Procedure)
2260 and then Scope (Spec_Id) = Current_Scope
2261 and then Present (First_Formal (Spec_Id))
2262 and then No (Next_Formal (First_Formal (Spec_Id)))
2263 and then Etype (First_Formal (Spec_Id)) = Entity (Typ)
2264 and then Comes_From_Source (Spec_Id)
2265 then
2266 return;
2267 end if;
2269 Spec_Id := Homonym (Spec_Id);
2270 end loop;
2272 -- At this point the body is known to be a late controlled primitive.
2273 -- Generate a matching spec and insert it before the body. Note the
2274 -- use of Copy_Separate_Tree - we want an entirely separate semantic
2275 -- tree in this case.
2277 Spec := Copy_Separate_Tree (Body_Spec);
2279 -- Ensure that the subprogram declaration does not inherit the null
2280 -- indicator from the body as we now have a proper spec/body pair.
2282 Set_Null_Present (Spec, False);
2284 Insert_Before_And_Analyze (Body_Decl,
2285 Make_Subprogram_Declaration (Loc, Specification => Spec));
2286 end Handle_Late_Controlled_Primitive;
2288 --------------------------------
2289 -- Remove_Visible_Refinements --
2290 --------------------------------
2292 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id) is
2293 State_Elmt : Elmt_Id;
2294 begin
2295 if Present (Abstract_States (Spec_Id)) then
2296 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2297 while Present (State_Elmt) loop
2298 Set_Has_Visible_Refinement (Node (State_Elmt), False);
2299 Next_Elmt (State_Elmt);
2300 end loop;
2301 end if;
2302 end Remove_Visible_Refinements;
2304 -- Local variables
2306 Context : Node_Id := Empty;
2307 Freeze_From : Entity_Id := Empty;
2308 Next_Decl : Node_Id;
2309 Pack_Decl : Node_Id := Empty;
2311 Body_Seen : Boolean := False;
2312 -- Flag set when the first body [stub] is encountered
2314 -- Start of processing for Analyze_Declarations
2316 begin
2317 if Restriction_Check_Required (SPARK_05) then
2318 Check_Later_Vs_Basic_Declarations (L, During_Parsing => False);
2319 end if;
2321 Decl := First (L);
2322 while Present (Decl) loop
2324 -- Package spec cannot contain a package declaration in SPARK
2326 if Nkind (Decl) = N_Package_Declaration
2327 and then Nkind (Parent (L)) = N_Package_Specification
2328 then
2329 Check_SPARK_05_Restriction
2330 ("package specification cannot contain a package declaration",
2331 Decl);
2332 end if;
2334 -- Complete analysis of declaration
2336 Analyze (Decl);
2337 Next_Decl := Next (Decl);
2339 if No (Freeze_From) then
2340 Freeze_From := First_Entity (Current_Scope);
2341 end if;
2343 -- At the end of a declarative part, freeze remaining entities
2344 -- declared in it. The end of the visible declarations of package
2345 -- specification is not the end of a declarative part if private
2346 -- declarations are present. The end of a package declaration is a
2347 -- freezing point only if it a library package. A task definition or
2348 -- protected type definition is not a freeze point either. Finally,
2349 -- we do not freeze entities in generic scopes, because there is no
2350 -- code generated for them and freeze nodes will be generated for
2351 -- the instance.
2353 -- The end of a package instantiation is not a freeze point, but
2354 -- for now we make it one, because the generic body is inserted
2355 -- (currently) immediately after. Generic instantiations will not
2356 -- be a freeze point once delayed freezing of bodies is implemented.
2357 -- (This is needed in any case for early instantiations ???).
2359 if No (Next_Decl) then
2360 if Nkind_In (Parent (L), N_Component_List,
2361 N_Task_Definition,
2362 N_Protected_Definition)
2363 then
2364 null;
2366 elsif Nkind (Parent (L)) /= N_Package_Specification then
2367 if Nkind (Parent (L)) = N_Package_Body then
2368 Freeze_From := First_Entity (Current_Scope);
2369 end if;
2371 -- There may have been several freezing points previously,
2372 -- for example object declarations or subprogram bodies, but
2373 -- at the end of a declarative part we check freezing from
2374 -- the beginning, even though entities may already be frozen,
2375 -- in order to perform visibility checks on delayed aspects.
2377 Adjust_Decl;
2378 Freeze_All (First_Entity (Current_Scope), Decl);
2379 Freeze_From := Last_Entity (Current_Scope);
2381 elsif Scope (Current_Scope) /= Standard_Standard
2382 and then not Is_Child_Unit (Current_Scope)
2383 and then No (Generic_Parent (Parent (L)))
2384 then
2385 null;
2387 elsif L /= Visible_Declarations (Parent (L))
2388 or else No (Private_Declarations (Parent (L)))
2389 or else Is_Empty_List (Private_Declarations (Parent (L)))
2390 then
2391 Adjust_Decl;
2392 Freeze_All (First_Entity (Current_Scope), Decl);
2393 Freeze_From := Last_Entity (Current_Scope);
2394 end if;
2396 -- If next node is a body then freeze all types before the body.
2397 -- An exception occurs for some expander-generated bodies. If these
2398 -- are generated at places where in general language rules would not
2399 -- allow a freeze point, then we assume that the expander has
2400 -- explicitly checked that all required types are properly frozen,
2401 -- and we do not cause general freezing here. This special circuit
2402 -- is used when the encountered body is marked as having already
2403 -- been analyzed.
2405 -- In all other cases (bodies that come from source, and expander
2406 -- generated bodies that have not been analyzed yet), freeze all
2407 -- types now. Note that in the latter case, the expander must take
2408 -- care to attach the bodies at a proper place in the tree so as to
2409 -- not cause unwanted freezing at that point.
2411 elsif not Analyzed (Next_Decl) and then Is_Body (Next_Decl) then
2413 -- When a controlled type is frozen, the expander generates stream
2414 -- and controlled type support routines. If the freeze is caused
2415 -- by the stand alone body of Initialize, Adjust and Finalize, the
2416 -- expander will end up using the wrong version of these routines
2417 -- as the body has not been processed yet. To remedy this, detect
2418 -- a late controlled primitive and create a proper spec for it.
2419 -- This ensures that the primitive will override its inherited
2420 -- counterpart before the freeze takes place.
2422 -- If the declaration we just processed is a body, do not attempt
2423 -- to examine Next_Decl as the late primitive idiom can only apply
2424 -- to the first encountered body.
2426 -- The spec of the late primitive is not generated in ASIS mode to
2427 -- ensure a consistent list of primitives that indicates the true
2428 -- semantic structure of the program (which is not relevant when
2429 -- generating executable code.
2431 -- ??? a cleaner approach may be possible and/or this solution
2432 -- could be extended to general-purpose late primitives, TBD.
2434 if not ASIS_Mode and then not Body_Seen and then not Is_Body (Decl)
2435 then
2436 Body_Seen := True;
2438 if Nkind (Next_Decl) = N_Subprogram_Body then
2439 Handle_Late_Controlled_Primitive (Next_Decl);
2440 end if;
2441 end if;
2443 Adjust_Decl;
2444 Freeze_All (Freeze_From, Decl);
2445 Freeze_From := Last_Entity (Current_Scope);
2446 end if;
2448 Decl := Next_Decl;
2449 end loop;
2451 -- Analyze the contracts of packages and their bodies
2453 if Present (L) then
2454 Context := Parent (L);
2456 if Nkind (Context) = N_Package_Specification then
2457 Pack_Decl := Parent (Context);
2459 -- When a package has private declarations, its contract must be
2460 -- analyzed at the end of the said declarations. This way both the
2461 -- analysis and freeze actions are properly synchronized in case
2462 -- of private type use within the contract.
2464 if L = Private_Declarations (Context) then
2465 Analyze_Package_Contract (Defining_Entity (Context));
2467 -- Build the bodies of the default initial condition procedures
2468 -- for all types subject to pragma Default_Initial_Condition.
2469 -- From a purely Ada stand point, this is a freezing activity,
2470 -- however freezing is not available under GNATprove_Mode. To
2471 -- accomodate both scenarios, the bodies are build at the end
2472 -- of private declaration analysis.
2474 Build_Default_Init_Cond_Procedure_Bodies (L);
2476 -- Otherwise the contract is analyzed at the end of the visible
2477 -- declarations.
2479 elsif L = Visible_Declarations (Context)
2480 and then No (Private_Declarations (Context))
2481 then
2482 Analyze_Package_Contract (Defining_Entity (Context));
2483 end if;
2485 elsif Nkind (Context) = N_Package_Body then
2486 Pack_Decl := Context;
2487 Analyze_Package_Body_Contract (Defining_Entity (Context));
2488 end if;
2490 -- Analyze the contracts of all subprogram declarations, subprogram
2491 -- bodies and variables now due to the delayed visibility needs of
2492 -- of their aspects and pragmas. Capture global references in generic
2493 -- subprograms or bodies.
2495 Decl := First (L);
2496 while Present (Decl) loop
2497 if Nkind (Decl) = N_Object_Declaration then
2498 Analyze_Object_Contract (Defining_Entity (Decl));
2500 elsif Nkind_In (Decl, N_Abstract_Subprogram_Declaration,
2501 N_Generic_Subprogram_Declaration,
2502 N_Subprogram_Declaration)
2503 then
2504 Analyze_Subprogram_Contract (Defining_Entity (Decl));
2506 elsif Nkind (Decl) = N_Subprogram_Body then
2507 Analyze_Subprogram_Body_Contract (Defining_Entity (Decl));
2509 elsif Nkind (Decl) = N_Subprogram_Body_Stub then
2510 Analyze_Subprogram_Body_Stub_Contract (Defining_Entity (Decl));
2511 end if;
2513 -- Capture all global references in a generic subprogram or a body
2514 -- [stub] now that the contract has been analyzed.
2516 if Nkind_In (Decl, N_Generic_Subprogram_Declaration,
2517 N_Subprogram_Body,
2518 N_Subprogram_Body_Stub)
2519 and then Is_Generic_Declaration_Or_Body (Decl)
2520 then
2521 Save_Global_References_In_Contract
2522 (Templ => Original_Node (Decl),
2523 Gen_Id => Corresponding_Spec_Of (Decl));
2524 end if;
2526 Next (Decl);
2527 end loop;
2529 -- The owner of the declarations is a package [body]
2531 if Present (Pack_Decl) then
2533 -- Capture all global references in a generic package or a body
2534 -- after all nested generic subprograms and bodies were subjected
2535 -- to the same processing.
2537 if Is_Generic_Declaration_Or_Body (Pack_Decl) then
2538 Save_Global_References_In_Contract
2539 (Templ => Original_Node (Pack_Decl),
2540 Gen_Id => Corresponding_Spec_Of (Pack_Decl));
2541 end if;
2543 -- State refinements are visible upto the end the of the package
2544 -- body declarations. Hide the state refinements from visibility
2545 -- to restore the original state conditions.
2547 if Nkind (Pack_Decl) = N_Package_Body then
2548 Remove_Visible_Refinements (Corresponding_Spec (Pack_Decl));
2549 end if;
2550 end if;
2551 end if;
2552 end Analyze_Declarations;
2554 -----------------------------------
2555 -- Analyze_Full_Type_Declaration --
2556 -----------------------------------
2558 procedure Analyze_Full_Type_Declaration (N : Node_Id) is
2559 Def : constant Node_Id := Type_Definition (N);
2560 Def_Id : constant Entity_Id := Defining_Identifier (N);
2561 GM : constant Ghost_Mode_Type := Ghost_Mode;
2562 T : Entity_Id;
2563 Prev : Entity_Id;
2565 Is_Remote : constant Boolean :=
2566 (Is_Remote_Types (Current_Scope)
2567 or else Is_Remote_Call_Interface (Current_Scope))
2568 and then not (In_Private_Part (Current_Scope)
2569 or else In_Package_Body (Current_Scope));
2571 procedure Check_Ops_From_Incomplete_Type;
2572 -- If there is a tagged incomplete partial view of the type, traverse
2573 -- the primitives of the incomplete view and change the type of any
2574 -- controlling formals and result to indicate the full view. The
2575 -- primitives will be added to the full type's primitive operations
2576 -- list later in Sem_Disp.Check_Operation_From_Incomplete_Type (which
2577 -- is called from Process_Incomplete_Dependents).
2579 procedure Restore_Globals;
2580 -- Restore the values of all saved global variables
2582 ------------------------------------
2583 -- Check_Ops_From_Incomplete_Type --
2584 ------------------------------------
2586 procedure Check_Ops_From_Incomplete_Type is
2587 Elmt : Elmt_Id;
2588 Formal : Entity_Id;
2589 Op : Entity_Id;
2591 begin
2592 if Prev /= T
2593 and then Ekind (Prev) = E_Incomplete_Type
2594 and then Is_Tagged_Type (Prev)
2595 and then Is_Tagged_Type (T)
2596 then
2597 Elmt := First_Elmt (Primitive_Operations (Prev));
2598 while Present (Elmt) loop
2599 Op := Node (Elmt);
2601 Formal := First_Formal (Op);
2602 while Present (Formal) loop
2603 if Etype (Formal) = Prev then
2604 Set_Etype (Formal, T);
2605 end if;
2607 Next_Formal (Formal);
2608 end loop;
2610 if Etype (Op) = Prev then
2611 Set_Etype (Op, T);
2612 end if;
2614 Next_Elmt (Elmt);
2615 end loop;
2616 end if;
2617 end Check_Ops_From_Incomplete_Type;
2619 ---------------------
2620 -- Restore_Globals --
2621 ---------------------
2623 procedure Restore_Globals is
2624 begin
2625 Ghost_Mode := GM;
2626 end Restore_Globals;
2628 -- Start of processing for Analyze_Full_Type_Declaration
2630 begin
2631 Prev := Find_Type_Name (N);
2633 -- The type declaration may be subject to pragma Ghost with policy
2634 -- Ignore. Set the mode now to ensure that any nodes generated during
2635 -- analysis and expansion are properly flagged as ignored Ghost.
2637 Set_Ghost_Mode (N, Prev);
2639 -- The full view, if present, now points to the current type. If there
2640 -- is an incomplete partial view, set a link to it, to simplify the
2641 -- retrieval of primitive operations of the type.
2643 -- Ada 2005 (AI-50217): If the type was previously decorated when
2644 -- imported through a LIMITED WITH clause, it appears as incomplete
2645 -- but has no full view.
2647 if Ekind (Prev) = E_Incomplete_Type
2648 and then Present (Full_View (Prev))
2649 then
2650 T := Full_View (Prev);
2651 Set_Incomplete_View (N, Parent (Prev));
2652 else
2653 T := Prev;
2654 end if;
2656 Set_Is_Pure (T, Is_Pure (Current_Scope));
2658 -- We set the flag Is_First_Subtype here. It is needed to set the
2659 -- corresponding flag for the Implicit class-wide-type created
2660 -- during tagged types processing.
2662 Set_Is_First_Subtype (T, True);
2664 -- Only composite types other than array types are allowed to have
2665 -- discriminants.
2667 case Nkind (Def) is
2669 -- For derived types, the rule will be checked once we've figured
2670 -- out the parent type.
2672 when N_Derived_Type_Definition =>
2673 null;
2675 -- For record types, discriminants are allowed, unless we are in
2676 -- SPARK.
2678 when N_Record_Definition =>
2679 if Present (Discriminant_Specifications (N)) then
2680 Check_SPARK_05_Restriction
2681 ("discriminant type is not allowed",
2682 Defining_Identifier
2683 (First (Discriminant_Specifications (N))));
2684 end if;
2686 when others =>
2687 if Present (Discriminant_Specifications (N)) then
2688 Error_Msg_N
2689 ("elementary or array type cannot have discriminants",
2690 Defining_Identifier
2691 (First (Discriminant_Specifications (N))));
2692 end if;
2693 end case;
2695 -- Elaborate the type definition according to kind, and generate
2696 -- subsidiary (implicit) subtypes where needed. We skip this if it was
2697 -- already done (this happens during the reanalysis that follows a call
2698 -- to the high level optimizer).
2700 if not Analyzed (T) then
2701 Set_Analyzed (T);
2703 case Nkind (Def) is
2704 when N_Access_To_Subprogram_Definition =>
2705 Access_Subprogram_Declaration (T, Def);
2707 -- If this is a remote access to subprogram, we must create the
2708 -- equivalent fat pointer type, and related subprograms.
2710 if Is_Remote then
2711 Process_Remote_AST_Declaration (N);
2712 end if;
2714 -- Validate categorization rule against access type declaration
2715 -- usually a violation in Pure unit, Shared_Passive unit.
2717 Validate_Access_Type_Declaration (T, N);
2719 when N_Access_To_Object_Definition =>
2720 Access_Type_Declaration (T, Def);
2722 -- Validate categorization rule against access type declaration
2723 -- usually a violation in Pure unit, Shared_Passive unit.
2725 Validate_Access_Type_Declaration (T, N);
2727 -- If we are in a Remote_Call_Interface package and define a
2728 -- RACW, then calling stubs and specific stream attributes
2729 -- must be added.
2731 if Is_Remote
2732 and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
2733 then
2734 Add_RACW_Features (Def_Id);
2735 end if;
2737 when N_Array_Type_Definition =>
2738 Array_Type_Declaration (T, Def);
2740 when N_Derived_Type_Definition =>
2741 Derived_Type_Declaration (T, N, T /= Def_Id);
2743 when N_Enumeration_Type_Definition =>
2744 Enumeration_Type_Declaration (T, Def);
2746 when N_Floating_Point_Definition =>
2747 Floating_Point_Type_Declaration (T, Def);
2749 when N_Decimal_Fixed_Point_Definition =>
2750 Decimal_Fixed_Point_Type_Declaration (T, Def);
2752 when N_Ordinary_Fixed_Point_Definition =>
2753 Ordinary_Fixed_Point_Type_Declaration (T, Def);
2755 when N_Signed_Integer_Type_Definition =>
2756 Signed_Integer_Type_Declaration (T, Def);
2758 when N_Modular_Type_Definition =>
2759 Modular_Type_Declaration (T, Def);
2761 when N_Record_Definition =>
2762 Record_Type_Declaration (T, N, Prev);
2764 -- If declaration has a parse error, nothing to elaborate.
2766 when N_Error =>
2767 null;
2769 when others =>
2770 raise Program_Error;
2772 end case;
2773 end if;
2775 if Etype (T) = Any_Type then
2776 Restore_Globals;
2777 return;
2778 end if;
2780 -- Controlled type is not allowed in SPARK
2782 if Is_Visibly_Controlled (T) then
2783 Check_SPARK_05_Restriction ("controlled type is not allowed", N);
2784 end if;
2786 -- A type declared within a Ghost region is automatically Ghost
2787 -- (SPARK RM 6.9(2)).
2789 if Ghost_Mode > None then
2790 Set_Is_Ghost_Entity (T);
2791 end if;
2793 -- Some common processing for all types
2795 Set_Depends_On_Private (T, Has_Private_Component (T));
2796 Check_Ops_From_Incomplete_Type;
2798 -- Both the declared entity, and its anonymous base type if one was
2799 -- created, need freeze nodes allocated.
2801 declare
2802 B : constant Entity_Id := Base_Type (T);
2804 begin
2805 -- In the case where the base type differs from the first subtype, we
2806 -- pre-allocate a freeze node, and set the proper link to the first
2807 -- subtype. Freeze_Entity will use this preallocated freeze node when
2808 -- it freezes the entity.
2810 -- This does not apply if the base type is a generic type, whose
2811 -- declaration is independent of the current derived definition.
2813 if B /= T and then not Is_Generic_Type (B) then
2814 Ensure_Freeze_Node (B);
2815 Set_First_Subtype_Link (Freeze_Node (B), T);
2816 end if;
2818 -- A type that is imported through a limited_with clause cannot
2819 -- generate any code, and thus need not be frozen. However, an access
2820 -- type with an imported designated type needs a finalization list,
2821 -- which may be referenced in some other package that has non-limited
2822 -- visibility on the designated type. Thus we must create the
2823 -- finalization list at the point the access type is frozen, to
2824 -- prevent unsatisfied references at link time.
2826 if not From_Limited_With (T) or else Is_Access_Type (T) then
2827 Set_Has_Delayed_Freeze (T);
2828 end if;
2829 end;
2831 -- Case where T is the full declaration of some private type which has
2832 -- been swapped in Defining_Identifier (N).
2834 if T /= Def_Id and then Is_Private_Type (Def_Id) then
2835 Process_Full_View (N, T, Def_Id);
2837 -- Record the reference. The form of this is a little strange, since
2838 -- the full declaration has been swapped in. So the first parameter
2839 -- here represents the entity to which a reference is made which is
2840 -- the "real" entity, i.e. the one swapped in, and the second
2841 -- parameter provides the reference location.
2843 -- Also, we want to kill Has_Pragma_Unreferenced temporarily here
2844 -- since we don't want a complaint about the full type being an
2845 -- unwanted reference to the private type
2847 declare
2848 B : constant Boolean := Has_Pragma_Unreferenced (T);
2849 begin
2850 Set_Has_Pragma_Unreferenced (T, False);
2851 Generate_Reference (T, T, 'c');
2852 Set_Has_Pragma_Unreferenced (T, B);
2853 end;
2855 Set_Completion_Referenced (Def_Id);
2857 -- For completion of incomplete type, process incomplete dependents
2858 -- and always mark the full type as referenced (it is the incomplete
2859 -- type that we get for any real reference).
2861 elsif Ekind (Prev) = E_Incomplete_Type then
2862 Process_Incomplete_Dependents (N, T, Prev);
2863 Generate_Reference (Prev, Def_Id, 'c');
2864 Set_Completion_Referenced (Def_Id);
2866 -- If not private type or incomplete type completion, this is a real
2867 -- definition of a new entity, so record it.
2869 else
2870 Generate_Definition (Def_Id);
2871 end if;
2873 -- Propagate any pending access types whose finalization masters need to
2874 -- be fully initialized from the partial to the full view. Guard against
2875 -- an illegal full view that remains unanalyzed.
2877 if Is_Type (Def_Id) and then Is_Incomplete_Or_Private_Type (Prev) then
2878 Set_Pending_Access_Types (Def_Id, Pending_Access_Types (Prev));
2879 end if;
2881 if Chars (Scope (Def_Id)) = Name_System
2882 and then Chars (Def_Id) = Name_Address
2883 and then Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (N)))
2884 then
2885 Set_Is_Descendent_Of_Address (Def_Id);
2886 Set_Is_Descendent_Of_Address (Base_Type (Def_Id));
2887 Set_Is_Descendent_Of_Address (Prev);
2888 end if;
2890 Set_Optimize_Alignment_Flags (Def_Id);
2891 Check_Eliminated (Def_Id);
2893 -- If the declaration is a completion and aspects are present, apply
2894 -- them to the entity for the type which is currently the partial
2895 -- view, but which is the one that will be frozen.
2897 if Has_Aspects (N) then
2899 -- In most cases the partial view is a private type, and both views
2900 -- appear in different declarative parts. In the unusual case where
2901 -- the partial view is incomplete, perform the analysis on the
2902 -- full view, to prevent freezing anomalies with the corresponding
2903 -- class-wide type, which otherwise might be frozen before the
2904 -- dispatch table is built.
2906 if Prev /= Def_Id
2907 and then Ekind (Prev) /= E_Incomplete_Type
2908 then
2909 Analyze_Aspect_Specifications (N, Prev);
2911 -- Normal case
2913 else
2914 Analyze_Aspect_Specifications (N, Def_Id);
2915 end if;
2916 end if;
2918 Restore_Globals;
2919 end Analyze_Full_Type_Declaration;
2921 ----------------------------------
2922 -- Analyze_Incomplete_Type_Decl --
2923 ----------------------------------
2925 procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
2926 F : constant Boolean := Is_Pure (Current_Scope);
2927 GM : constant Ghost_Mode_Type := Ghost_Mode;
2928 T : Entity_Id;
2930 begin
2931 Check_SPARK_05_Restriction ("incomplete type is not allowed", N);
2933 -- The incomplete type declaration may be subject to pragma Ghost with
2934 -- policy Ignore. Set the mode now to ensure that any nodes generated
2935 -- during analysis and expansion are properly flagged as ignored Ghost.
2937 Set_Ghost_Mode (N);
2938 Generate_Definition (Defining_Identifier (N));
2940 -- Process an incomplete declaration. The identifier must not have been
2941 -- declared already in the scope. However, an incomplete declaration may
2942 -- appear in the private part of a package, for a private type that has
2943 -- already been declared.
2945 -- In this case, the discriminants (if any) must match
2947 T := Find_Type_Name (N);
2949 Set_Ekind (T, E_Incomplete_Type);
2950 Init_Size_Align (T);
2951 Set_Is_First_Subtype (T, True);
2952 Set_Etype (T, T);
2954 -- An incomplete type declared within a Ghost region is automatically
2955 -- Ghost (SPARK RM 6.9(2)).
2957 if Ghost_Mode > None then
2958 Set_Is_Ghost_Entity (T);
2959 end if;
2961 -- Ada 2005 (AI-326): Minimum decoration to give support to tagged
2962 -- incomplete types.
2964 if Tagged_Present (N) then
2965 Set_Is_Tagged_Type (T, True);
2966 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
2967 Make_Class_Wide_Type (T);
2968 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2969 end if;
2971 Push_Scope (T);
2973 Set_Stored_Constraint (T, No_Elist);
2975 if Present (Discriminant_Specifications (N)) then
2976 Process_Discriminants (N);
2977 end if;
2979 End_Scope;
2981 -- If the type has discriminants, non-trivial subtypes may be
2982 -- declared before the full view of the type. The full views of those
2983 -- subtypes will be built after the full view of the type.
2985 Set_Private_Dependents (T, New_Elmt_List);
2986 Set_Is_Pure (T, F);
2988 -- Restore the original Ghost mode once analysis and expansion have
2989 -- taken place.
2991 Ghost_Mode := GM;
2992 end Analyze_Incomplete_Type_Decl;
2994 -----------------------------------
2995 -- Analyze_Interface_Declaration --
2996 -----------------------------------
2998 procedure Analyze_Interface_Declaration (T : Entity_Id; Def : Node_Id) is
2999 CW : constant Entity_Id := Class_Wide_Type (T);
3001 begin
3002 Set_Is_Tagged_Type (T);
3003 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
3005 Set_Is_Limited_Record (T, Limited_Present (Def)
3006 or else Task_Present (Def)
3007 or else Protected_Present (Def)
3008 or else Synchronized_Present (Def));
3010 -- Type is abstract if full declaration carries keyword, or if previous
3011 -- partial view did.
3013 Set_Is_Abstract_Type (T);
3014 Set_Is_Interface (T);
3016 -- Type is a limited interface if it includes the keyword limited, task,
3017 -- protected, or synchronized.
3019 Set_Is_Limited_Interface
3020 (T, Limited_Present (Def)
3021 or else Protected_Present (Def)
3022 or else Synchronized_Present (Def)
3023 or else Task_Present (Def));
3025 Set_Interfaces (T, New_Elmt_List);
3026 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3028 -- Complete the decoration of the class-wide entity if it was already
3029 -- built (i.e. during the creation of the limited view)
3031 if Present (CW) then
3032 Set_Is_Interface (CW);
3033 Set_Is_Limited_Interface (CW, Is_Limited_Interface (T));
3034 end if;
3036 -- Check runtime support for synchronized interfaces
3038 if VM_Target = No_VM
3039 and then (Is_Task_Interface (T)
3040 or else Is_Protected_Interface (T)
3041 or else Is_Synchronized_Interface (T))
3042 and then not RTE_Available (RE_Select_Specific_Data)
3043 then
3044 Error_Msg_CRT ("synchronized interfaces", T);
3045 end if;
3046 end Analyze_Interface_Declaration;
3048 -----------------------------
3049 -- Analyze_Itype_Reference --
3050 -----------------------------
3052 -- Nothing to do. This node is placed in the tree only for the benefit of
3053 -- back end processing, and has no effect on the semantic processing.
3055 procedure Analyze_Itype_Reference (N : Node_Id) is
3056 begin
3057 pragma Assert (Is_Itype (Itype (N)));
3058 null;
3059 end Analyze_Itype_Reference;
3061 --------------------------------
3062 -- Analyze_Number_Declaration --
3063 --------------------------------
3065 procedure Analyze_Number_Declaration (N : Node_Id) is
3066 GM : constant Ghost_Mode_Type := Ghost_Mode;
3068 procedure Restore_Globals;
3069 -- Restore the values of all saved global variables
3071 ---------------------
3072 -- Restore_Globals --
3073 ---------------------
3075 procedure Restore_Globals is
3076 begin
3077 Ghost_Mode := GM;
3078 end Restore_Globals;
3080 -- Local variables
3082 E : constant Node_Id := Expression (N);
3083 Id : constant Entity_Id := Defining_Identifier (N);
3084 Index : Interp_Index;
3085 It : Interp;
3086 T : Entity_Id;
3088 -- Start of processing for Analyze_Number_Declaration
3090 begin
3091 -- The number declaration may be subject to pragma Ghost with policy
3092 -- Ignore. Set the mode now to ensure that any nodes generated during
3093 -- analysis and expansion are properly flagged as ignored Ghost.
3095 Set_Ghost_Mode (N);
3097 Generate_Definition (Id);
3098 Enter_Name (Id);
3100 -- A number declared within a Ghost region is automatically Ghost
3101 -- (SPARK RM 6.9(2)).
3103 if Ghost_Mode > None then
3104 Set_Is_Ghost_Entity (Id);
3105 end if;
3107 -- This is an optimization of a common case of an integer literal
3109 if Nkind (E) = N_Integer_Literal then
3110 Set_Is_Static_Expression (E, True);
3111 Set_Etype (E, Universal_Integer);
3113 Set_Etype (Id, Universal_Integer);
3114 Set_Ekind (Id, E_Named_Integer);
3115 Set_Is_Frozen (Id, True);
3117 Restore_Globals;
3118 return;
3119 end if;
3121 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3123 -- Process expression, replacing error by integer zero, to avoid
3124 -- cascaded errors or aborts further along in the processing
3126 -- Replace Error by integer zero, which seems least likely to cause
3127 -- cascaded errors.
3129 if E = Error then
3130 Rewrite (E, Make_Integer_Literal (Sloc (E), Uint_0));
3131 Set_Error_Posted (E);
3132 end if;
3134 Analyze (E);
3136 -- Verify that the expression is static and numeric. If
3137 -- the expression is overloaded, we apply the preference
3138 -- rule that favors root numeric types.
3140 if not Is_Overloaded (E) then
3141 T := Etype (E);
3142 if Has_Dynamic_Predicate_Aspect (T) then
3143 Error_Msg_N
3144 ("subtype has dynamic predicate, "
3145 & "not allowed in number declaration", N);
3146 end if;
3148 else
3149 T := Any_Type;
3151 Get_First_Interp (E, Index, It);
3152 while Present (It.Typ) loop
3153 if (Is_Integer_Type (It.Typ) or else Is_Real_Type (It.Typ))
3154 and then (Scope (Base_Type (It.Typ))) = Standard_Standard
3155 then
3156 if T = Any_Type then
3157 T := It.Typ;
3159 elsif It.Typ = Universal_Real
3160 or else
3161 It.Typ = Universal_Integer
3162 then
3163 -- Choose universal interpretation over any other
3165 T := It.Typ;
3166 exit;
3167 end if;
3168 end if;
3170 Get_Next_Interp (Index, It);
3171 end loop;
3172 end if;
3174 if Is_Integer_Type (T) then
3175 Resolve (E, T);
3176 Set_Etype (Id, Universal_Integer);
3177 Set_Ekind (Id, E_Named_Integer);
3179 elsif Is_Real_Type (T) then
3181 -- Because the real value is converted to universal_real, this is a
3182 -- legal context for a universal fixed expression.
3184 if T = Universal_Fixed then
3185 declare
3186 Loc : constant Source_Ptr := Sloc (N);
3187 Conv : constant Node_Id := Make_Type_Conversion (Loc,
3188 Subtype_Mark =>
3189 New_Occurrence_Of (Universal_Real, Loc),
3190 Expression => Relocate_Node (E));
3192 begin
3193 Rewrite (E, Conv);
3194 Analyze (E);
3195 end;
3197 elsif T = Any_Fixed then
3198 Error_Msg_N ("illegal context for mixed mode operation", E);
3200 -- Expression is of the form : universal_fixed * integer. Try to
3201 -- resolve as universal_real.
3203 T := Universal_Real;
3204 Set_Etype (E, T);
3205 end if;
3207 Resolve (E, T);
3208 Set_Etype (Id, Universal_Real);
3209 Set_Ekind (Id, E_Named_Real);
3211 else
3212 Wrong_Type (E, Any_Numeric);
3213 Resolve (E, T);
3215 Set_Etype (Id, T);
3216 Set_Ekind (Id, E_Constant);
3217 Set_Never_Set_In_Source (Id, True);
3218 Set_Is_True_Constant (Id, True);
3220 Restore_Globals;
3221 return;
3222 end if;
3224 if Nkind_In (E, N_Integer_Literal, N_Real_Literal) then
3225 Set_Etype (E, Etype (Id));
3226 end if;
3228 if not Is_OK_Static_Expression (E) then
3229 Flag_Non_Static_Expr
3230 ("non-static expression used in number declaration!", E);
3231 Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
3232 Set_Etype (E, Any_Type);
3233 end if;
3235 Restore_Globals;
3236 end Analyze_Number_Declaration;
3238 -----------------------------
3239 -- Analyze_Object_Contract --
3240 -----------------------------
3242 procedure Analyze_Object_Contract (Obj_Id : Entity_Id) is
3243 Obj_Typ : constant Entity_Id := Etype (Obj_Id);
3244 AR_Val : Boolean := False;
3245 AW_Val : Boolean := False;
3246 ER_Val : Boolean := False;
3247 EW_Val : Boolean := False;
3248 Prag : Node_Id;
3249 Seen : Boolean := False;
3251 begin
3252 -- The loop parameter in an element iterator over a formal container
3253 -- is declared with an object declaration but no contracts apply.
3255 if Ekind (Obj_Id) = E_Loop_Parameter then
3256 return;
3257 end if;
3259 -- Constant related checks
3261 if Ekind (Obj_Id) = E_Constant then
3263 -- A constant cannot be effectively volatile. This check is only
3264 -- relevant with SPARK_Mode on as it is not a standard Ada legality
3265 -- rule. Do not flag internally-generated constants that map generic
3266 -- formals to actuals in instantiations (SPARK RM 7.1.3(6)).
3268 if SPARK_Mode = On
3269 and then Is_Effectively_Volatile (Obj_Id)
3270 and then No (Corresponding_Generic_Association (Parent (Obj_Id)))
3272 -- Don't give this for internally generated entities (such as the
3273 -- FIRST and LAST temporaries generated for bounds).
3275 and then Comes_From_Source (Obj_Id)
3276 then
3277 Error_Msg_N ("constant cannot be volatile", Obj_Id);
3278 end if;
3280 -- Variable related checks
3282 else pragma Assert (Ekind (Obj_Id) = E_Variable);
3284 -- The following checks are only relevant when SPARK_Mode is on as
3285 -- they are not standard Ada legality rules. Internally generated
3286 -- temporaries are ignored.
3288 if SPARK_Mode = On and then Comes_From_Source (Obj_Id) then
3289 if Is_Effectively_Volatile (Obj_Id) then
3291 -- The declaration of an effectively volatile object must
3292 -- appear at the library level (SPARK RM 7.1.3(7), C.6(6)).
3294 if not Is_Library_Level_Entity (Obj_Id) then
3295 Error_Msg_N
3296 ("volatile variable & must be declared at library level",
3297 Obj_Id);
3299 -- An object of a discriminated type cannot be effectively
3300 -- volatile (SPARK RM C.6(4)).
3302 elsif Has_Discriminants (Obj_Typ) then
3303 Error_Msg_N
3304 ("discriminated object & cannot be volatile", Obj_Id);
3306 -- An object of a tagged type cannot be effectively volatile
3307 -- (SPARK RM C.6(5)).
3309 elsif Is_Tagged_Type (Obj_Typ) then
3310 Error_Msg_N ("tagged object & cannot be volatile", Obj_Id);
3311 end if;
3313 -- The object is not effectively volatile
3315 else
3316 -- A non-effectively volatile object cannot have effectively
3317 -- volatile components (SPARK RM 7.1.3(7)).
3319 if not Is_Effectively_Volatile (Obj_Id)
3320 and then Has_Volatile_Component (Obj_Typ)
3321 then
3322 Error_Msg_N
3323 ("non-volatile object & cannot have volatile components",
3324 Obj_Id);
3325 end if;
3326 end if;
3327 end if;
3329 if Is_Ghost_Entity (Obj_Id) then
3331 -- A Ghost object cannot be effectively volatile (SPARK RM 6.9(8))
3333 if Is_Effectively_Volatile (Obj_Id) then
3334 Error_Msg_N ("ghost variable & cannot be volatile", Obj_Id);
3336 -- A Ghost object cannot be imported or exported (SPARK RM 6.9(8))
3338 elsif Is_Imported (Obj_Id) then
3339 Error_Msg_N ("ghost object & cannot be imported", Obj_Id);
3341 elsif Is_Exported (Obj_Id) then
3342 Error_Msg_N ("ghost object & cannot be exported", Obj_Id);
3343 end if;
3344 end if;
3346 -- Analyze all external properties
3348 Prag := Get_Pragma (Obj_Id, Pragma_Async_Readers);
3350 if Present (Prag) then
3351 Analyze_External_Property_In_Decl_Part (Prag, AR_Val);
3352 Seen := True;
3353 end if;
3355 Prag := Get_Pragma (Obj_Id, Pragma_Async_Writers);
3357 if Present (Prag) then
3358 Analyze_External_Property_In_Decl_Part (Prag, AW_Val);
3359 Seen := True;
3360 end if;
3362 Prag := Get_Pragma (Obj_Id, Pragma_Effective_Reads);
3364 if Present (Prag) then
3365 Analyze_External_Property_In_Decl_Part (Prag, ER_Val);
3366 Seen := True;
3367 end if;
3369 Prag := Get_Pragma (Obj_Id, Pragma_Effective_Writes);
3371 if Present (Prag) then
3372 Analyze_External_Property_In_Decl_Part (Prag, EW_Val);
3373 Seen := True;
3374 end if;
3376 -- Verify the mutual interaction of the various external properties
3378 if Seen then
3379 Check_External_Properties (Obj_Id, AR_Val, AW_Val, ER_Val, EW_Val);
3380 end if;
3381 end if;
3383 -- Check whether the lack of indicator Part_Of agrees with the placement
3384 -- of the object with respect to the state space.
3386 Prag := Get_Pragma (Obj_Id, Pragma_Part_Of);
3388 if No (Prag) then
3389 Check_Missing_Part_Of (Obj_Id);
3390 end if;
3392 -- A ghost object cannot be imported or exported (SPARK RM 6.9(8))
3394 if Is_Ghost_Entity (Obj_Id) then
3395 if Is_Exported (Obj_Id) then
3396 Error_Msg_N ("ghost object & cannot be exported", Obj_Id);
3398 elsif Is_Imported (Obj_Id) then
3399 Error_Msg_N ("ghost object & cannot be imported", Obj_Id);
3400 end if;
3401 end if;
3402 end Analyze_Object_Contract;
3404 --------------------------------
3405 -- Analyze_Object_Declaration --
3406 --------------------------------
3408 procedure Analyze_Object_Declaration (N : Node_Id) is
3409 Loc : constant Source_Ptr := Sloc (N);
3410 GM : constant Ghost_Mode_Type := Ghost_Mode;
3411 Id : constant Entity_Id := Defining_Identifier (N);
3412 Act_T : Entity_Id;
3413 T : Entity_Id;
3415 E : Node_Id := Expression (N);
3416 -- E is set to Expression (N) throughout this routine. When
3417 -- Expression (N) is modified, E is changed accordingly.
3419 Prev_Entity : Entity_Id := Empty;
3421 function Count_Tasks (T : Entity_Id) return Uint;
3422 -- This function is called when a non-generic library level object of a
3423 -- task type is declared. Its function is to count the static number of
3424 -- tasks declared within the type (it is only called if Has_Tasks is set
3425 -- for T). As a side effect, if an array of tasks with non-static bounds
3426 -- or a variant record type is encountered, Check_Restrictions is called
3427 -- indicating the count is unknown.
3429 function Delayed_Aspect_Present return Boolean;
3430 -- If the declaration has an expression that is an aggregate, and it
3431 -- has aspects that require delayed analysis, the resolution of the
3432 -- aggregate must be deferred to the freeze point of the objet. This
3433 -- special processing was created for address clauses, but it must
3434 -- also apply to Alignment. This must be done before the aspect
3435 -- specifications are analyzed because we must handle the aggregate
3436 -- before the analysis of the object declaration is complete.
3438 -- Any other relevant delayed aspects on object declarations ???
3440 procedure Restore_Globals;
3441 -- Restore the values of all saved global variables
3443 -----------------
3444 -- Count_Tasks --
3445 -----------------
3447 function Count_Tasks (T : Entity_Id) return Uint is
3448 C : Entity_Id;
3449 X : Node_Id;
3450 V : Uint;
3452 begin
3453 if Is_Task_Type (T) then
3454 return Uint_1;
3456 elsif Is_Record_Type (T) then
3457 if Has_Discriminants (T) then
3458 Check_Restriction (Max_Tasks, N);
3459 return Uint_0;
3461 else
3462 V := Uint_0;
3463 C := First_Component (T);
3464 while Present (C) loop
3465 V := V + Count_Tasks (Etype (C));
3466 Next_Component (C);
3467 end loop;
3469 return V;
3470 end if;
3472 elsif Is_Array_Type (T) then
3473 X := First_Index (T);
3474 V := Count_Tasks (Component_Type (T));
3475 while Present (X) loop
3476 C := Etype (X);
3478 if not Is_OK_Static_Subtype (C) then
3479 Check_Restriction (Max_Tasks, N);
3480 return Uint_0;
3481 else
3482 V := V * (UI_Max (Uint_0,
3483 Expr_Value (Type_High_Bound (C)) -
3484 Expr_Value (Type_Low_Bound (C)) + Uint_1));
3485 end if;
3487 Next_Index (X);
3488 end loop;
3490 return V;
3492 else
3493 return Uint_0;
3494 end if;
3495 end Count_Tasks;
3497 ----------------------------
3498 -- Delayed_Aspect_Present --
3499 ----------------------------
3501 function Delayed_Aspect_Present return Boolean is
3502 A : Node_Id;
3503 A_Id : Aspect_Id;
3505 begin
3506 if Present (Aspect_Specifications (N)) then
3507 A := First (Aspect_Specifications (N));
3508 A_Id := Get_Aspect_Id (Chars (Identifier (A)));
3509 while Present (A) loop
3510 if A_Id = Aspect_Alignment or else A_Id = Aspect_Address then
3511 return True;
3512 end if;
3514 Next (A);
3515 end loop;
3516 end if;
3518 return False;
3519 end Delayed_Aspect_Present;
3521 ---------------------
3522 -- Restore_Globals --
3523 ---------------------
3525 procedure Restore_Globals is
3526 begin
3527 Ghost_Mode := GM;
3528 end Restore_Globals;
3530 -- Start of processing for Analyze_Object_Declaration
3532 begin
3533 -- There are three kinds of implicit types generated by an
3534 -- object declaration:
3536 -- 1. Those generated by the original Object Definition
3538 -- 2. Those generated by the Expression
3540 -- 3. Those used to constrain the Object Definition with the
3541 -- expression constraints when the definition is unconstrained.
3543 -- They must be generated in this order to avoid order of elaboration
3544 -- issues. Thus the first step (after entering the name) is to analyze
3545 -- the object definition.
3547 if Constant_Present (N) then
3548 Prev_Entity := Current_Entity_In_Scope (Id);
3550 if Present (Prev_Entity)
3551 and then
3552 -- If the homograph is an implicit subprogram, it is overridden
3553 -- by the current declaration.
3555 ((Is_Overloadable (Prev_Entity)
3556 and then Is_Inherited_Operation (Prev_Entity))
3558 -- The current object is a discriminal generated for an entry
3559 -- family index. Even though the index is a constant, in this
3560 -- particular context there is no true constant redeclaration.
3561 -- Enter_Name will handle the visibility.
3563 or else
3564 (Is_Discriminal (Id)
3565 and then Ekind (Discriminal_Link (Id)) =
3566 E_Entry_Index_Parameter)
3568 -- The current object is the renaming for a generic declared
3569 -- within the instance.
3571 or else
3572 (Ekind (Prev_Entity) = E_Package
3573 and then Nkind (Parent (Prev_Entity)) =
3574 N_Package_Renaming_Declaration
3575 and then not Comes_From_Source (Prev_Entity)
3576 and then
3577 Is_Generic_Instance (Renamed_Entity (Prev_Entity))))
3578 then
3579 Prev_Entity := Empty;
3580 end if;
3581 end if;
3583 -- The object declaration may be subject to pragma Ghost with policy
3584 -- Ignore. Set the mode now to ensure that any nodes generated during
3585 -- analysis and expansion are properly flagged as ignored Ghost.
3587 Set_Ghost_Mode (N, Prev_Entity);
3589 if Present (Prev_Entity) then
3590 Constant_Redeclaration (Id, N, T);
3592 Generate_Reference (Prev_Entity, Id, 'c');
3593 Set_Completion_Referenced (Id);
3595 if Error_Posted (N) then
3597 -- Type mismatch or illegal redeclaration, Do not analyze
3598 -- expression to avoid cascaded errors.
3600 T := Find_Type_Of_Object (Object_Definition (N), N);
3601 Set_Etype (Id, T);
3602 Set_Ekind (Id, E_Variable);
3603 goto Leave;
3604 end if;
3606 -- In the normal case, enter identifier at the start to catch premature
3607 -- usage in the initialization expression.
3609 else
3610 Generate_Definition (Id);
3611 Enter_Name (Id);
3613 Mark_Coextensions (N, Object_Definition (N));
3615 T := Find_Type_Of_Object (Object_Definition (N), N);
3617 if Nkind (Object_Definition (N)) = N_Access_Definition
3618 and then Present
3619 (Access_To_Subprogram_Definition (Object_Definition (N)))
3620 and then Protected_Present
3621 (Access_To_Subprogram_Definition (Object_Definition (N)))
3622 then
3623 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
3624 end if;
3626 if Error_Posted (Id) then
3627 Set_Etype (Id, T);
3628 Set_Ekind (Id, E_Variable);
3629 goto Leave;
3630 end if;
3631 end if;
3633 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
3634 -- out some static checks
3636 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (T) then
3638 -- In case of aggregates we must also take care of the correct
3639 -- initialization of nested aggregates bug this is done at the
3640 -- point of the analysis of the aggregate (see sem_aggr.adb).
3642 if Present (Expression (N))
3643 and then Nkind (Expression (N)) = N_Aggregate
3644 then
3645 null;
3647 else
3648 declare
3649 Save_Typ : constant Entity_Id := Etype (Id);
3650 begin
3651 Set_Etype (Id, T); -- Temp. decoration for static checks
3652 Null_Exclusion_Static_Checks (N);
3653 Set_Etype (Id, Save_Typ);
3654 end;
3655 end if;
3656 end if;
3658 -- Object is marked pure if it is in a pure scope
3660 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3662 -- If deferred constant, make sure context is appropriate. We detect
3663 -- a deferred constant as a constant declaration with no expression.
3664 -- A deferred constant can appear in a package body if its completion
3665 -- is by means of an interface pragma.
3667 if Constant_Present (N) and then No (E) then
3669 -- A deferred constant may appear in the declarative part of the
3670 -- following constructs:
3672 -- blocks
3673 -- entry bodies
3674 -- extended return statements
3675 -- package specs
3676 -- package bodies
3677 -- subprogram bodies
3678 -- task bodies
3680 -- When declared inside a package spec, a deferred constant must be
3681 -- completed by a full constant declaration or pragma Import. In all
3682 -- other cases, the only proper completion is pragma Import. Extended
3683 -- return statements are flagged as invalid contexts because they do
3684 -- not have a declarative part and so cannot accommodate the pragma.
3686 if Ekind (Current_Scope) = E_Return_Statement then
3687 Error_Msg_N
3688 ("invalid context for deferred constant declaration (RM 7.4)",
3690 Error_Msg_N
3691 ("\declaration requires an initialization expression",
3693 Set_Constant_Present (N, False);
3695 -- In Ada 83, deferred constant must be of private type
3697 elsif not Is_Private_Type (T) then
3698 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
3699 Error_Msg_N
3700 ("(Ada 83) deferred constant must be private type", N);
3701 end if;
3702 end if;
3704 -- If not a deferred constant, then the object declaration freezes
3705 -- its type, unless the object is of an anonymous type and has delayed
3706 -- aspects. In that case the type is frozen when the object itself is.
3708 else
3709 Check_Fully_Declared (T, N);
3711 if Has_Delayed_Aspects (Id)
3712 and then Is_Array_Type (T)
3713 and then Is_Itype (T)
3714 then
3715 Set_Has_Delayed_Freeze (T);
3716 else
3717 Freeze_Before (N, T);
3718 end if;
3719 end if;
3721 -- If the object was created by a constrained array definition, then
3722 -- set the link in both the anonymous base type and anonymous subtype
3723 -- that are built to represent the array type to point to the object.
3725 if Nkind (Object_Definition (Declaration_Node (Id))) =
3726 N_Constrained_Array_Definition
3727 then
3728 Set_Related_Array_Object (T, Id);
3729 Set_Related_Array_Object (Base_Type (T), Id);
3730 end if;
3732 -- Special checks for protected objects not at library level
3734 if Is_Protected_Type (T)
3735 and then not Is_Library_Level_Entity (Id)
3736 then
3737 Check_Restriction (No_Local_Protected_Objects, Id);
3739 -- Protected objects with interrupt handlers must be at library level
3741 -- Ada 2005: This test is not needed (and the corresponding clause
3742 -- in the RM is removed) because accessibility checks are sufficient
3743 -- to make handlers not at the library level illegal.
3745 -- AI05-0303: The AI is in fact a binding interpretation, and thus
3746 -- applies to the '95 version of the language as well.
3748 if Has_Interrupt_Handler (T) and then Ada_Version < Ada_95 then
3749 Error_Msg_N
3750 ("interrupt object can only be declared at library level", Id);
3751 end if;
3752 end if;
3754 -- The actual subtype of the object is the nominal subtype, unless
3755 -- the nominal one is unconstrained and obtained from the expression.
3757 Act_T := T;
3759 -- These checks should be performed before the initialization expression
3760 -- is considered, so that the Object_Definition node is still the same
3761 -- as in source code.
3763 -- In SPARK, the nominal subtype is always given by a subtype mark
3764 -- and must not be unconstrained. (The only exception to this is the
3765 -- acceptance of declarations of constants of type String.)
3767 if not Nkind_In (Object_Definition (N), N_Expanded_Name, N_Identifier)
3768 then
3769 Check_SPARK_05_Restriction
3770 ("subtype mark required", Object_Definition (N));
3772 elsif Is_Array_Type (T)
3773 and then not Is_Constrained (T)
3774 and then T /= Standard_String
3775 then
3776 Check_SPARK_05_Restriction
3777 ("subtype mark of constrained type expected",
3778 Object_Definition (N));
3779 end if;
3781 -- There are no aliased objects in SPARK
3783 if Aliased_Present (N) then
3784 Check_SPARK_05_Restriction ("aliased object is not allowed", N);
3785 end if;
3787 -- Process initialization expression if present and not in error
3789 if Present (E) and then E /= Error then
3791 -- Generate an error in case of CPP class-wide object initialization.
3792 -- Required because otherwise the expansion of the class-wide
3793 -- assignment would try to use 'size to initialize the object
3794 -- (primitive that is not available in CPP tagged types).
3796 if Is_Class_Wide_Type (Act_T)
3797 and then
3798 (Is_CPP_Class (Root_Type (Etype (Act_T)))
3799 or else
3800 (Present (Full_View (Root_Type (Etype (Act_T))))
3801 and then
3802 Is_CPP_Class (Full_View (Root_Type (Etype (Act_T))))))
3803 then
3804 Error_Msg_N
3805 ("predefined assignment not available for 'C'P'P tagged types",
3807 end if;
3809 Mark_Coextensions (N, E);
3810 Analyze (E);
3812 -- In case of errors detected in the analysis of the expression,
3813 -- decorate it with the expected type to avoid cascaded errors
3815 if No (Etype (E)) then
3816 Set_Etype (E, T);
3817 end if;
3819 -- If an initialization expression is present, then we set the
3820 -- Is_True_Constant flag. It will be reset if this is a variable
3821 -- and it is indeed modified.
3823 Set_Is_True_Constant (Id, True);
3825 -- If we are analyzing a constant declaration, set its completion
3826 -- flag after analyzing and resolving the expression.
3828 if Constant_Present (N) then
3829 Set_Has_Completion (Id);
3830 end if;
3832 -- Set type and resolve (type may be overridden later on). Note:
3833 -- Ekind (Id) must still be E_Void at this point so that incorrect
3834 -- early usage within E is properly diagnosed.
3836 Set_Etype (Id, T);
3838 -- If the expression is an aggregate we must look ahead to detect
3839 -- the possible presence of an address clause, and defer resolution
3840 -- and expansion of the aggregate to the freeze point of the entity.
3842 if Comes_From_Source (N)
3843 and then Expander_Active
3844 and then Nkind (E) = N_Aggregate
3845 and then (Present (Following_Address_Clause (N))
3846 or else Delayed_Aspect_Present)
3847 then
3848 Set_Etype (E, T);
3850 else
3851 Resolve (E, T);
3852 end if;
3854 -- No further action needed if E is a call to an inlined function
3855 -- which returns an unconstrained type and it has been expanded into
3856 -- a procedure call. In that case N has been replaced by an object
3857 -- declaration without initializing expression and it has been
3858 -- analyzed (see Expand_Inlined_Call).
3860 if Back_End_Inlining
3861 and then Expander_Active
3862 and then Nkind (E) = N_Function_Call
3863 and then Nkind (Name (E)) in N_Has_Entity
3864 and then Is_Inlined (Entity (Name (E)))
3865 and then not Is_Constrained (Etype (E))
3866 and then Analyzed (N)
3867 and then No (Expression (N))
3868 then
3869 Restore_Globals;
3870 return;
3871 end if;
3873 -- If E is null and has been replaced by an N_Raise_Constraint_Error
3874 -- node (which was marked already-analyzed), we need to set the type
3875 -- to something other than Any_Access in order to keep gigi happy.
3877 if Etype (E) = Any_Access then
3878 Set_Etype (E, T);
3879 end if;
3881 -- If the object is an access to variable, the initialization
3882 -- expression cannot be an access to constant.
3884 if Is_Access_Type (T)
3885 and then not Is_Access_Constant (T)
3886 and then Is_Access_Type (Etype (E))
3887 and then Is_Access_Constant (Etype (E))
3888 then
3889 Error_Msg_N
3890 ("access to variable cannot be initialized with an "
3891 & "access-to-constant expression", E);
3892 end if;
3894 if not Assignment_OK (N) then
3895 Check_Initialization (T, E);
3896 end if;
3898 Check_Unset_Reference (E);
3900 -- If this is a variable, then set current value. If this is a
3901 -- declared constant of a scalar type with a static expression,
3902 -- indicate that it is always valid.
3904 if not Constant_Present (N) then
3905 if Compile_Time_Known_Value (E) then
3906 Set_Current_Value (Id, E);
3907 end if;
3909 elsif Is_Scalar_Type (T) and then Is_OK_Static_Expression (E) then
3910 Set_Is_Known_Valid (Id);
3911 end if;
3913 -- Deal with setting of null flags
3915 if Is_Access_Type (T) then
3916 if Known_Non_Null (E) then
3917 Set_Is_Known_Non_Null (Id, True);
3918 elsif Known_Null (E) and then not Can_Never_Be_Null (Id) then
3919 Set_Is_Known_Null (Id, True);
3920 end if;
3921 end if;
3923 -- Check incorrect use of dynamically tagged expressions
3925 if Is_Tagged_Type (T) then
3926 Check_Dynamically_Tagged_Expression
3927 (Expr => E,
3928 Typ => T,
3929 Related_Nod => N);
3930 end if;
3932 Apply_Scalar_Range_Check (E, T);
3933 Apply_Static_Length_Check (E, T);
3935 if Nkind (Original_Node (N)) = N_Object_Declaration
3936 and then Comes_From_Source (Original_Node (N))
3938 -- Only call test if needed
3940 and then Restriction_Check_Required (SPARK_05)
3941 and then not Is_SPARK_05_Initialization_Expr (Original_Node (E))
3942 then
3943 Check_SPARK_05_Restriction
3944 ("initialization expression is not appropriate", E);
3945 end if;
3947 -- A formal parameter of a specific tagged type whose related
3948 -- subprogram is subject to pragma Extensions_Visible with value
3949 -- "False" cannot be implicitly converted to a class-wide type by
3950 -- means of an initialization expression (SPARK RM 6.1.7(3)).
3952 if Is_Class_Wide_Type (T) and then Is_EVF_Expression (E) then
3953 Error_Msg_N
3954 ("formal parameter with Extensions_Visible False cannot be "
3955 & "implicitly converted to class-wide type", E);
3956 end if;
3957 end if;
3959 -- If the No_Streams restriction is set, check that the type of the
3960 -- object is not, and does not contain, any subtype derived from
3961 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
3962 -- Has_Stream just for efficiency reasons. There is no point in
3963 -- spending time on a Has_Stream check if the restriction is not set.
3965 if Restriction_Check_Required (No_Streams) then
3966 if Has_Stream (T) then
3967 Check_Restriction (No_Streams, N);
3968 end if;
3969 end if;
3971 -- Deal with predicate check before we start to do major rewriting. It
3972 -- is OK to initialize and then check the initialized value, since the
3973 -- object goes out of scope if we get a predicate failure. Note that we
3974 -- do this in the analyzer and not the expander because the analyzer
3975 -- does some substantial rewriting in some cases.
3977 -- We need a predicate check if the type has predicates, and if either
3978 -- there is an initializing expression, or for default initialization
3979 -- when we have at least one case of an explicit default initial value
3980 -- and then this is not an internal declaration whose initialization
3981 -- comes later (as for an aggregate expansion).
3983 if not Suppress_Assignment_Checks (N)
3984 and then Present (Predicate_Function (T))
3985 and then not No_Initialization (N)
3986 and then
3987 (Present (E)
3988 or else
3989 Is_Partially_Initialized_Type (T, Include_Implicit => False))
3990 then
3991 -- If the type has a static predicate and the expression is known at
3992 -- compile time, see if the expression satisfies the predicate.
3994 if Present (E) then
3995 Check_Expression_Against_Static_Predicate (E, T);
3996 end if;
3998 Insert_After (N,
3999 Make_Predicate_Check (T, New_Occurrence_Of (Id, Loc)));
4000 end if;
4002 -- Case of unconstrained type
4004 if not Is_Definite_Subtype (T) then
4006 -- In SPARK, a declaration of unconstrained type is allowed
4007 -- only for constants of type string.
4009 if Is_String_Type (T) and then not Constant_Present (N) then
4010 Check_SPARK_05_Restriction
4011 ("declaration of object of unconstrained type not allowed", N);
4012 end if;
4014 -- Nothing to do in deferred constant case
4016 if Constant_Present (N) and then No (E) then
4017 null;
4019 -- Case of no initialization present
4021 elsif No (E) then
4022 if No_Initialization (N) then
4023 null;
4025 elsif Is_Class_Wide_Type (T) then
4026 Error_Msg_N
4027 ("initialization required in class-wide declaration ", N);
4029 else
4030 Error_Msg_N
4031 ("unconstrained subtype not allowed (need initialization)",
4032 Object_Definition (N));
4034 if Is_Record_Type (T) and then Has_Discriminants (T) then
4035 Error_Msg_N
4036 ("\provide initial value or explicit discriminant values",
4037 Object_Definition (N));
4039 Error_Msg_NE
4040 ("\or give default discriminant values for type&",
4041 Object_Definition (N), T);
4043 elsif Is_Array_Type (T) then
4044 Error_Msg_N
4045 ("\provide initial value or explicit array bounds",
4046 Object_Definition (N));
4047 end if;
4048 end if;
4050 -- Case of initialization present but in error. Set initial
4051 -- expression as absent (but do not make above complaints)
4053 elsif E = Error then
4054 Set_Expression (N, Empty);
4055 E := Empty;
4057 -- Case of initialization present
4059 else
4060 -- Check restrictions in Ada 83
4062 if not Constant_Present (N) then
4064 -- Unconstrained variables not allowed in Ada 83 mode
4066 if Ada_Version = Ada_83
4067 and then Comes_From_Source (Object_Definition (N))
4068 then
4069 Error_Msg_N
4070 ("(Ada 83) unconstrained variable not allowed",
4071 Object_Definition (N));
4072 end if;
4073 end if;
4075 -- Now we constrain the variable from the initializing expression
4077 -- If the expression is an aggregate, it has been expanded into
4078 -- individual assignments. Retrieve the actual type from the
4079 -- expanded construct.
4081 if Is_Array_Type (T)
4082 and then No_Initialization (N)
4083 and then Nkind (Original_Node (E)) = N_Aggregate
4084 then
4085 Act_T := Etype (E);
4087 -- In case of class-wide interface object declarations we delay
4088 -- the generation of the equivalent record type declarations until
4089 -- its expansion because there are cases in they are not required.
4091 elsif Is_Interface (T) then
4092 null;
4094 -- In GNATprove mode, Expand_Subtype_From_Expr does nothing. Thus,
4095 -- we should prevent the generation of another Itype with the
4096 -- same name as the one already generated, or we end up with
4097 -- two identical types in GNATprove.
4099 elsif GNATprove_Mode then
4100 null;
4102 -- If the type is an unchecked union, no subtype can be built from
4103 -- the expression. Rewrite declaration as a renaming, which the
4104 -- back-end can handle properly. This is a rather unusual case,
4105 -- because most unchecked_union declarations have default values
4106 -- for discriminants and are thus not indefinite.
4108 elsif Is_Unchecked_Union (T) then
4109 if Constant_Present (N) or else Nkind (E) = N_Function_Call then
4110 Set_Ekind (Id, E_Constant);
4111 else
4112 Set_Ekind (Id, E_Variable);
4113 end if;
4115 -- An object declared within a Ghost region is automatically
4116 -- Ghost (SPARK RM 6.9(2)).
4118 if Comes_From_Source (Id) and then Ghost_Mode > None then
4119 Set_Is_Ghost_Entity (Id);
4121 -- The Ghost policy in effect at the point of declaration
4122 -- and at the point of completion must match
4123 -- (SPARK RM 6.9(14)).
4125 if Present (Prev_Entity)
4126 and then Is_Ghost_Entity (Prev_Entity)
4127 then
4128 Check_Ghost_Completion (Prev_Entity, Id);
4129 end if;
4130 end if;
4132 Rewrite (N,
4133 Make_Object_Renaming_Declaration (Loc,
4134 Defining_Identifier => Id,
4135 Subtype_Mark => New_Occurrence_Of (T, Loc),
4136 Name => E));
4138 Set_Renamed_Object (Id, E);
4139 Freeze_Before (N, T);
4140 Set_Is_Frozen (Id);
4142 Restore_Globals;
4143 return;
4145 else
4146 Expand_Subtype_From_Expr (N, T, Object_Definition (N), E);
4147 Act_T := Find_Type_Of_Object (Object_Definition (N), N);
4148 end if;
4150 Set_Is_Constr_Subt_For_U_Nominal (Act_T);
4152 if Aliased_Present (N) then
4153 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4154 end if;
4156 Freeze_Before (N, Act_T);
4157 Freeze_Before (N, T);
4158 end if;
4160 elsif Is_Array_Type (T)
4161 and then No_Initialization (N)
4162 and then Nkind (Original_Node (E)) = N_Aggregate
4163 then
4164 if not Is_Entity_Name (Object_Definition (N)) then
4165 Act_T := Etype (E);
4166 Check_Compile_Time_Size (Act_T);
4168 if Aliased_Present (N) then
4169 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4170 end if;
4171 end if;
4173 -- When the given object definition and the aggregate are specified
4174 -- independently, and their lengths might differ do a length check.
4175 -- This cannot happen if the aggregate is of the form (others =>...)
4177 if not Is_Constrained (T) then
4178 null;
4180 elsif Nkind (E) = N_Raise_Constraint_Error then
4182 -- Aggregate is statically illegal. Place back in declaration
4184 Set_Expression (N, E);
4185 Set_No_Initialization (N, False);
4187 elsif T = Etype (E) then
4188 null;
4190 elsif Nkind (E) = N_Aggregate
4191 and then Present (Component_Associations (E))
4192 and then Present (Choices (First (Component_Associations (E))))
4193 and then Nkind (First
4194 (Choices (First (Component_Associations (E))))) = N_Others_Choice
4195 then
4196 null;
4198 else
4199 Apply_Length_Check (E, T);
4200 end if;
4202 -- If the type is limited unconstrained with defaulted discriminants and
4203 -- there is no expression, then the object is constrained by the
4204 -- defaults, so it is worthwhile building the corresponding subtype.
4206 elsif (Is_Limited_Record (T) or else Is_Concurrent_Type (T))
4207 and then not Is_Constrained (T)
4208 and then Has_Discriminants (T)
4209 then
4210 if No (E) then
4211 Act_T := Build_Default_Subtype (T, N);
4212 else
4213 -- Ada 2005: A limited object may be initialized by means of an
4214 -- aggregate. If the type has default discriminants it has an
4215 -- unconstrained nominal type, Its actual subtype will be obtained
4216 -- from the aggregate, and not from the default discriminants.
4218 Act_T := Etype (E);
4219 end if;
4221 Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
4223 elsif Nkind (E) = N_Function_Call
4224 and then Constant_Present (N)
4225 and then Has_Unconstrained_Elements (Etype (E))
4226 then
4227 -- The back-end has problems with constants of a discriminated type
4228 -- with defaults, if the initial value is a function call. We
4229 -- generate an intermediate temporary that will receive a reference
4230 -- to the result of the call. The initialization expression then
4231 -- becomes a dereference of that temporary.
4233 Remove_Side_Effects (E);
4235 -- If this is a constant declaration of an unconstrained type and
4236 -- the initialization is an aggregate, we can use the subtype of the
4237 -- aggregate for the declared entity because it is immutable.
4239 elsif not Is_Constrained (T)
4240 and then Has_Discriminants (T)
4241 and then Constant_Present (N)
4242 and then not Has_Unchecked_Union (T)
4243 and then Nkind (E) = N_Aggregate
4244 then
4245 Act_T := Etype (E);
4246 end if;
4248 -- Check No_Wide_Characters restriction
4250 Check_Wide_Character_Restriction (T, Object_Definition (N));
4252 -- Indicate this is not set in source. Certainly true for constants, and
4253 -- true for variables so far (will be reset for a variable if and when
4254 -- we encounter a modification in the source).
4256 Set_Never_Set_In_Source (Id);
4258 -- Now establish the proper kind and type of the object
4260 if Constant_Present (N) then
4261 Set_Ekind (Id, E_Constant);
4262 Set_Is_True_Constant (Id);
4264 else
4265 Set_Ekind (Id, E_Variable);
4267 -- A variable is set as shared passive if it appears in a shared
4268 -- passive package, and is at the outer level. This is not done for
4269 -- entities generated during expansion, because those are always
4270 -- manipulated locally.
4272 if Is_Shared_Passive (Current_Scope)
4273 and then Is_Library_Level_Entity (Id)
4274 and then Comes_From_Source (Id)
4275 then
4276 Set_Is_Shared_Passive (Id);
4277 Check_Shared_Var (Id, T, N);
4278 end if;
4280 -- Set Has_Initial_Value if initializing expression present. Note
4281 -- that if there is no initializing expression, we leave the state
4282 -- of this flag unchanged (usually it will be False, but notably in
4283 -- the case of exception choice variables, it will already be true).
4285 if Present (E) then
4286 Set_Has_Initial_Value (Id);
4287 end if;
4288 end if;
4290 -- Initialize alignment and size and capture alignment setting
4292 Init_Alignment (Id);
4293 Init_Esize (Id);
4294 Set_Optimize_Alignment_Flags (Id);
4296 -- An object declared within a Ghost region is automatically Ghost
4297 -- (SPARK RM 6.9(2)).
4299 if Comes_From_Source (Id)
4300 and then (Ghost_Mode > None
4301 or else (Present (Prev_Entity)
4302 and then Is_Ghost_Entity (Prev_Entity)))
4303 then
4304 Set_Is_Ghost_Entity (Id);
4306 -- The Ghost policy in effect at the point of declaration and at the
4307 -- point of completion must match (SPARK RM 6.9(14)).
4309 if Present (Prev_Entity) and then Is_Ghost_Entity (Prev_Entity) then
4310 Check_Ghost_Completion (Prev_Entity, Id);
4311 end if;
4312 end if;
4314 -- Deal with aliased case
4316 if Aliased_Present (N) then
4317 Set_Is_Aliased (Id);
4319 -- If the object is aliased and the type is unconstrained with
4320 -- defaulted discriminants and there is no expression, then the
4321 -- object is constrained by the defaults, so it is worthwhile
4322 -- building the corresponding subtype.
4324 -- Ada 2005 (AI-363): If the aliased object is discriminated and
4325 -- unconstrained, then only establish an actual subtype if the
4326 -- nominal subtype is indefinite. In definite cases the object is
4327 -- unconstrained in Ada 2005.
4329 if No (E)
4330 and then Is_Record_Type (T)
4331 and then not Is_Constrained (T)
4332 and then Has_Discriminants (T)
4333 and then (Ada_Version < Ada_2005
4334 or else not Is_Definite_Subtype (T))
4335 then
4336 Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
4337 end if;
4338 end if;
4340 -- Now we can set the type of the object
4342 Set_Etype (Id, Act_T);
4344 -- Non-constant object is marked to be treated as volatile if type is
4345 -- volatile and we clear the Current_Value setting that may have been
4346 -- set above. Doing so for constants isn't required and might interfere
4347 -- with possible uses of the object as a static expression in contexts
4348 -- incompatible with volatility (e.g. as a case-statement alternative).
4350 if Ekind (Id) /= E_Constant and then Treat_As_Volatile (Etype (Id)) then
4351 Set_Treat_As_Volatile (Id);
4352 Set_Current_Value (Id, Empty);
4353 end if;
4355 -- Deal with controlled types
4357 if Has_Controlled_Component (Etype (Id))
4358 or else Is_Controlled (Etype (Id))
4359 then
4360 if not Is_Library_Level_Entity (Id) then
4361 Check_Restriction (No_Nested_Finalization, N);
4362 else
4363 Validate_Controlled_Object (Id);
4364 end if;
4365 end if;
4367 if Has_Task (Etype (Id)) then
4368 Check_Restriction (No_Tasking, N);
4370 -- Deal with counting max tasks
4372 -- Nothing to do if inside a generic
4374 if Inside_A_Generic then
4375 null;
4377 -- If library level entity, then count tasks
4379 elsif Is_Library_Level_Entity (Id) then
4380 Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
4382 -- If not library level entity, then indicate we don't know max
4383 -- tasks and also check task hierarchy restriction and blocking
4384 -- operation (since starting a task is definitely blocking).
4386 else
4387 Check_Restriction (Max_Tasks, N);
4388 Check_Restriction (No_Task_Hierarchy, N);
4389 Check_Potentially_Blocking_Operation (N);
4390 end if;
4392 -- A rather specialized test. If we see two tasks being declared
4393 -- of the same type in the same object declaration, and the task
4394 -- has an entry with an address clause, we know that program error
4395 -- will be raised at run time since we can't have two tasks with
4396 -- entries at the same address.
4398 if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
4399 declare
4400 E : Entity_Id;
4402 begin
4403 E := First_Entity (Etype (Id));
4404 while Present (E) loop
4405 if Ekind (E) = E_Entry
4406 and then Present (Get_Attribute_Definition_Clause
4407 (E, Attribute_Address))
4408 then
4409 Error_Msg_Warn := SPARK_Mode /= On;
4410 Error_Msg_N
4411 ("more than one task with same entry address<<", N);
4412 Error_Msg_N ("\Program_Error [<<", N);
4413 Insert_Action (N,
4414 Make_Raise_Program_Error (Loc,
4415 Reason => PE_Duplicated_Entry_Address));
4416 exit;
4417 end if;
4419 Next_Entity (E);
4420 end loop;
4421 end;
4422 end if;
4423 end if;
4425 -- Some simple constant-propagation: if the expression is a constant
4426 -- string initialized with a literal, share the literal. This avoids
4427 -- a run-time copy.
4429 if Present (E)
4430 and then Is_Entity_Name (E)
4431 and then Ekind (Entity (E)) = E_Constant
4432 and then Base_Type (Etype (E)) = Standard_String
4433 then
4434 declare
4435 Val : constant Node_Id := Constant_Value (Entity (E));
4436 begin
4437 if Present (Val) and then Nkind (Val) = N_String_Literal then
4438 Rewrite (E, New_Copy (Val));
4439 end if;
4440 end;
4441 end if;
4443 -- Another optimization: if the nominal subtype is unconstrained and
4444 -- the expression is a function call that returns an unconstrained
4445 -- type, rewrite the declaration as a renaming of the result of the
4446 -- call. The exceptions below are cases where the copy is expected,
4447 -- either by the back end (Aliased case) or by the semantics, as for
4448 -- initializing controlled types or copying tags for classwide types.
4450 if Present (E)
4451 and then Nkind (E) = N_Explicit_Dereference
4452 and then Nkind (Original_Node (E)) = N_Function_Call
4453 and then not Is_Library_Level_Entity (Id)
4454 and then not Is_Constrained (Underlying_Type (T))
4455 and then not Is_Aliased (Id)
4456 and then not Is_Class_Wide_Type (T)
4457 and then not Is_Controlled_Active (T)
4458 and then not Has_Controlled_Component (Base_Type (T))
4459 and then Expander_Active
4460 then
4461 Rewrite (N,
4462 Make_Object_Renaming_Declaration (Loc,
4463 Defining_Identifier => Id,
4464 Access_Definition => Empty,
4465 Subtype_Mark => New_Occurrence_Of
4466 (Base_Type (Etype (Id)), Loc),
4467 Name => E));
4469 Set_Renamed_Object (Id, E);
4471 -- Force generation of debugging information for the constant and for
4472 -- the renamed function call.
4474 Set_Debug_Info_Needed (Id);
4475 Set_Debug_Info_Needed (Entity (Prefix (E)));
4476 end if;
4478 if Present (Prev_Entity)
4479 and then Is_Frozen (Prev_Entity)
4480 and then not Error_Posted (Id)
4481 then
4482 Error_Msg_N ("full constant declaration appears too late", N);
4483 end if;
4485 Check_Eliminated (Id);
4487 -- Deal with setting In_Private_Part flag if in private part
4489 if Ekind (Scope (Id)) = E_Package
4490 and then In_Private_Part (Scope (Id))
4491 then
4492 Set_In_Private_Part (Id);
4493 end if;
4495 -- Check for violation of No_Local_Timing_Events
4497 if Restriction_Check_Required (No_Local_Timing_Events)
4498 and then not Is_Library_Level_Entity (Id)
4499 and then Is_RTE (Etype (Id), RE_Timing_Event)
4500 then
4501 Check_Restriction (No_Local_Timing_Events, N);
4502 end if;
4504 <<Leave>>
4505 -- Initialize the refined state of a variable here because this is a
4506 -- common destination for legal and illegal object declarations.
4508 if Ekind (Id) = E_Variable then
4509 Set_Encapsulating_State (Id, Empty);
4510 end if;
4512 if Has_Aspects (N) then
4513 Analyze_Aspect_Specifications (N, Id);
4514 end if;
4516 Analyze_Dimension (N);
4518 -- Verify whether the object declaration introduces an illegal hidden
4519 -- state within a package subject to a null abstract state.
4521 if Ekind (Id) = E_Variable then
4522 Check_No_Hidden_State (Id);
4523 end if;
4525 Restore_Globals;
4526 end Analyze_Object_Declaration;
4528 ---------------------------
4529 -- Analyze_Others_Choice --
4530 ---------------------------
4532 -- Nothing to do for the others choice node itself, the semantic analysis
4533 -- of the others choice will occur as part of the processing of the parent
4535 procedure Analyze_Others_Choice (N : Node_Id) is
4536 pragma Warnings (Off, N);
4537 begin
4538 null;
4539 end Analyze_Others_Choice;
4541 -------------------------------------------
4542 -- Analyze_Private_Extension_Declaration --
4543 -------------------------------------------
4545 procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
4546 GM : constant Ghost_Mode_Type := Ghost_Mode;
4547 Indic : constant Node_Id := Subtype_Indication (N);
4548 T : constant Entity_Id := Defining_Identifier (N);
4549 Parent_Base : Entity_Id;
4550 Parent_Type : Entity_Id;
4552 begin
4553 -- The private extension declaration may be subject to pragma Ghost with
4554 -- policy Ignore. Set the mode now to ensure that any nodes generated
4555 -- during analysis and expansion are properly flagged as ignored Ghost.
4557 Set_Ghost_Mode (N);
4559 -- Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
4561 if Is_Non_Empty_List (Interface_List (N)) then
4562 declare
4563 Intf : Node_Id;
4564 T : Entity_Id;
4566 begin
4567 Intf := First (Interface_List (N));
4568 while Present (Intf) loop
4569 T := Find_Type_Of_Subtype_Indic (Intf);
4571 Diagnose_Interface (Intf, T);
4572 Next (Intf);
4573 end loop;
4574 end;
4575 end if;
4577 Generate_Definition (T);
4579 -- For other than Ada 2012, just enter the name in the current scope
4581 if Ada_Version < Ada_2012 then
4582 Enter_Name (T);
4584 -- Ada 2012 (AI05-0162): Enter the name in the current scope handling
4585 -- case of private type that completes an incomplete type.
4587 else
4588 declare
4589 Prev : Entity_Id;
4591 begin
4592 Prev := Find_Type_Name (N);
4594 pragma Assert (Prev = T
4595 or else (Ekind (Prev) = E_Incomplete_Type
4596 and then Present (Full_View (Prev))
4597 and then Full_View (Prev) = T));
4598 end;
4599 end if;
4601 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
4602 Parent_Base := Base_Type (Parent_Type);
4604 if Parent_Type = Any_Type or else Etype (Parent_Type) = Any_Type then
4605 Set_Ekind (T, Ekind (Parent_Type));
4606 Set_Etype (T, Any_Type);
4607 goto Leave;
4609 elsif not Is_Tagged_Type (Parent_Type) then
4610 Error_Msg_N
4611 ("parent of type extension must be a tagged type ", Indic);
4612 goto Leave;
4614 elsif Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
4615 Error_Msg_N ("premature derivation of incomplete type", Indic);
4616 goto Leave;
4618 elsif Is_Concurrent_Type (Parent_Type) then
4619 Error_Msg_N
4620 ("parent type of a private extension cannot be "
4621 & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
4623 Set_Etype (T, Any_Type);
4624 Set_Ekind (T, E_Limited_Private_Type);
4625 Set_Private_Dependents (T, New_Elmt_List);
4626 Set_Error_Posted (T);
4627 goto Leave;
4628 end if;
4630 -- Perhaps the parent type should be changed to the class-wide type's
4631 -- specific type in this case to prevent cascading errors ???
4633 if Is_Class_Wide_Type (Parent_Type) then
4634 Error_Msg_N
4635 ("parent of type extension must not be a class-wide type", Indic);
4636 goto Leave;
4637 end if;
4639 if (not Is_Package_Or_Generic_Package (Current_Scope)
4640 and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
4641 or else In_Private_Part (Current_Scope)
4643 then
4644 Error_Msg_N ("invalid context for private extension", N);
4645 end if;
4647 -- Set common attributes
4649 Set_Is_Pure (T, Is_Pure (Current_Scope));
4650 Set_Scope (T, Current_Scope);
4651 Set_Ekind (T, E_Record_Type_With_Private);
4652 Init_Size_Align (T);
4653 Set_Default_SSO (T);
4655 Set_Etype (T, Parent_Base);
4656 Set_Has_Task (T, Has_Task (Parent_Base));
4657 Set_Has_Protected (T, Has_Task (Parent_Base));
4659 Set_Convention (T, Convention (Parent_Type));
4660 Set_First_Rep_Item (T, First_Rep_Item (Parent_Type));
4661 Set_Is_First_Subtype (T);
4662 Make_Class_Wide_Type (T);
4664 if Unknown_Discriminants_Present (N) then
4665 Set_Discriminant_Constraint (T, No_Elist);
4666 end if;
4668 Build_Derived_Record_Type (N, Parent_Type, T);
4670 -- Propagate inherited invariant information. The new type has
4671 -- invariants, if the parent type has inheritable invariants,
4672 -- and these invariants can in turn be inherited.
4674 if Has_Inheritable_Invariants (Parent_Type) then
4675 Set_Has_Inheritable_Invariants (T);
4676 Set_Has_Invariants (T);
4677 end if;
4679 -- Ada 2005 (AI-443): Synchronized private extension or a rewritten
4680 -- synchronized formal derived type.
4682 if Ada_Version >= Ada_2005 and then Synchronized_Present (N) then
4683 Set_Is_Limited_Record (T);
4685 -- Formal derived type case
4687 if Is_Generic_Type (T) then
4689 -- The parent must be a tagged limited type or a synchronized
4690 -- interface.
4692 if (not Is_Tagged_Type (Parent_Type)
4693 or else not Is_Limited_Type (Parent_Type))
4694 and then
4695 (not Is_Interface (Parent_Type)
4696 or else not Is_Synchronized_Interface (Parent_Type))
4697 then
4698 Error_Msg_NE ("parent type of & must be tagged limited " &
4699 "or synchronized", N, T);
4700 end if;
4702 -- The progenitors (if any) must be limited or synchronized
4703 -- interfaces.
4705 if Present (Interfaces (T)) then
4706 declare
4707 Iface : Entity_Id;
4708 Iface_Elmt : Elmt_Id;
4710 begin
4711 Iface_Elmt := First_Elmt (Interfaces (T));
4712 while Present (Iface_Elmt) loop
4713 Iface := Node (Iface_Elmt);
4715 if not Is_Limited_Interface (Iface)
4716 and then not Is_Synchronized_Interface (Iface)
4717 then
4718 Error_Msg_NE ("progenitor & must be limited " &
4719 "or synchronized", N, Iface);
4720 end if;
4722 Next_Elmt (Iface_Elmt);
4723 end loop;
4724 end;
4725 end if;
4727 -- Regular derived extension, the parent must be a limited or
4728 -- synchronized interface.
4730 else
4731 if not Is_Interface (Parent_Type)
4732 or else (not Is_Limited_Interface (Parent_Type)
4733 and then not Is_Synchronized_Interface (Parent_Type))
4734 then
4735 Error_Msg_NE
4736 ("parent type of & must be limited interface", N, T);
4737 end if;
4738 end if;
4740 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
4741 -- extension with a synchronized parent must be explicitly declared
4742 -- synchronized, because the full view will be a synchronized type.
4743 -- This must be checked before the check for limited types below,
4744 -- to ensure that types declared limited are not allowed to extend
4745 -- synchronized interfaces.
4747 elsif Is_Interface (Parent_Type)
4748 and then Is_Synchronized_Interface (Parent_Type)
4749 and then not Synchronized_Present (N)
4750 then
4751 Error_Msg_NE
4752 ("private extension of& must be explicitly synchronized",
4753 N, Parent_Type);
4755 elsif Limited_Present (N) then
4756 Set_Is_Limited_Record (T);
4758 if not Is_Limited_Type (Parent_Type)
4759 and then
4760 (not Is_Interface (Parent_Type)
4761 or else not Is_Limited_Interface (Parent_Type))
4762 then
4763 Error_Msg_NE ("parent type& of limited extension must be limited",
4764 N, Parent_Type);
4765 end if;
4766 end if;
4768 <<Leave>>
4769 if Has_Aspects (N) then
4770 Analyze_Aspect_Specifications (N, T);
4771 end if;
4773 -- Restore the original Ghost mode once analysis and expansion have
4774 -- taken place.
4776 Ghost_Mode := GM;
4777 end Analyze_Private_Extension_Declaration;
4779 ---------------------------------
4780 -- Analyze_Subtype_Declaration --
4781 ---------------------------------
4783 procedure Analyze_Subtype_Declaration
4784 (N : Node_Id;
4785 Skip : Boolean := False)
4787 GM : constant Ghost_Mode_Type := Ghost_Mode;
4788 Id : constant Entity_Id := Defining_Identifier (N);
4789 R_Checks : Check_Result;
4790 T : Entity_Id;
4792 begin
4793 -- The subtype declaration may be subject to pragma Ghost with policy
4794 -- Ignore. Set the mode now to ensure that any nodes generated during
4795 -- analysis and expansion are properly flagged as ignored Ghost.
4797 Set_Ghost_Mode (N);
4799 Generate_Definition (Id);
4800 Set_Is_Pure (Id, Is_Pure (Current_Scope));
4801 Init_Size_Align (Id);
4803 -- The following guard condition on Enter_Name is to handle cases where
4804 -- the defining identifier has already been entered into the scope but
4805 -- the declaration as a whole needs to be analyzed.
4807 -- This case in particular happens for derived enumeration types. The
4808 -- derived enumeration type is processed as an inserted enumeration type
4809 -- declaration followed by a rewritten subtype declaration. The defining
4810 -- identifier, however, is entered into the name scope very early in the
4811 -- processing of the original type declaration and therefore needs to be
4812 -- avoided here, when the created subtype declaration is analyzed. (See
4813 -- Build_Derived_Types)
4815 -- This also happens when the full view of a private type is derived
4816 -- type with constraints. In this case the entity has been introduced
4817 -- in the private declaration.
4819 -- Finally this happens in some complex cases when validity checks are
4820 -- enabled, where the same subtype declaration may be analyzed twice.
4821 -- This can happen if the subtype is created by the pre-analysis of
4822 -- an attribute tht gives the range of a loop statement, and the loop
4823 -- itself appears within an if_statement that will be rewritten during
4824 -- expansion.
4826 if Skip
4827 or else (Present (Etype (Id))
4828 and then (Is_Private_Type (Etype (Id))
4829 or else Is_Task_Type (Etype (Id))
4830 or else Is_Rewrite_Substitution (N)))
4831 then
4832 null;
4834 elsif Current_Entity (Id) = Id then
4835 null;
4837 else
4838 Enter_Name (Id);
4839 end if;
4841 T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
4843 -- Class-wide equivalent types of records with unknown discriminants
4844 -- involve the generation of an itype which serves as the private view
4845 -- of a constrained record subtype. In such cases the base type of the
4846 -- current subtype we are processing is the private itype. Use the full
4847 -- of the private itype when decorating various attributes.
4849 if Is_Itype (T)
4850 and then Is_Private_Type (T)
4851 and then Present (Full_View (T))
4852 then
4853 T := Full_View (T);
4854 end if;
4856 -- Inherit common attributes
4858 Set_Is_Volatile (Id, Is_Volatile (T));
4859 Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
4860 Set_Is_Generic_Type (Id, Is_Generic_Type (Base_Type (T)));
4861 Set_Convention (Id, Convention (T));
4863 -- If ancestor has predicates then so does the subtype, and in addition
4864 -- we must delay the freeze to properly arrange predicate inheritance.
4866 -- The Ancestor_Type test is really unpleasant, there seem to be cases
4867 -- in which T = ID, so the above tests and assignments do nothing???
4869 if Has_Predicates (T)
4870 or else (Present (Ancestor_Subtype (T))
4871 and then Has_Predicates (Ancestor_Subtype (T)))
4872 then
4873 Set_Has_Predicates (Id);
4874 Set_Has_Delayed_Freeze (Id);
4875 end if;
4877 -- Subtype of Boolean cannot have a constraint in SPARK
4879 if Is_Boolean_Type (T)
4880 and then Nkind (Subtype_Indication (N)) = N_Subtype_Indication
4881 then
4882 Check_SPARK_05_Restriction
4883 ("subtype of Boolean cannot have constraint", N);
4884 end if;
4886 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
4887 declare
4888 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
4889 One_Cstr : Node_Id;
4890 Low : Node_Id;
4891 High : Node_Id;
4893 begin
4894 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint then
4895 One_Cstr := First (Constraints (Cstr));
4896 while Present (One_Cstr) loop
4898 -- Index or discriminant constraint in SPARK must be a
4899 -- subtype mark.
4901 if not
4902 Nkind_In (One_Cstr, N_Identifier, N_Expanded_Name)
4903 then
4904 Check_SPARK_05_Restriction
4905 ("subtype mark required", One_Cstr);
4907 -- String subtype must have a lower bound of 1 in SPARK.
4908 -- Note that we do not need to test for the non-static case
4909 -- here, since that was already taken care of in
4910 -- Process_Range_Expr_In_Decl.
4912 elsif Base_Type (T) = Standard_String then
4913 Get_Index_Bounds (One_Cstr, Low, High);
4915 if Is_OK_Static_Expression (Low)
4916 and then Expr_Value (Low) /= 1
4917 then
4918 Check_SPARK_05_Restriction
4919 ("String subtype must have lower bound of 1", N);
4920 end if;
4921 end if;
4923 Next (One_Cstr);
4924 end loop;
4925 end if;
4926 end;
4927 end if;
4929 -- In the case where there is no constraint given in the subtype
4930 -- indication, Process_Subtype just returns the Subtype_Mark, so its
4931 -- semantic attributes must be established here.
4933 if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
4934 Set_Etype (Id, Base_Type (T));
4936 -- Subtype of unconstrained array without constraint is not allowed
4937 -- in SPARK.
4939 if Is_Array_Type (T) and then not Is_Constrained (T) then
4940 Check_SPARK_05_Restriction
4941 ("subtype of unconstrained array must have constraint", N);
4942 end if;
4944 case Ekind (T) is
4945 when Array_Kind =>
4946 Set_Ekind (Id, E_Array_Subtype);
4947 Copy_Array_Subtype_Attributes (Id, T);
4949 when Decimal_Fixed_Point_Kind =>
4950 Set_Ekind (Id, E_Decimal_Fixed_Point_Subtype);
4951 Set_Digits_Value (Id, Digits_Value (T));
4952 Set_Delta_Value (Id, Delta_Value (T));
4953 Set_Scale_Value (Id, Scale_Value (T));
4954 Set_Small_Value (Id, Small_Value (T));
4955 Set_Scalar_Range (Id, Scalar_Range (T));
4956 Set_Machine_Radix_10 (Id, Machine_Radix_10 (T));
4957 Set_Is_Constrained (Id, Is_Constrained (T));
4958 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4959 Set_RM_Size (Id, RM_Size (T));
4961 when Enumeration_Kind =>
4962 Set_Ekind (Id, E_Enumeration_Subtype);
4963 Set_First_Literal (Id, First_Literal (Base_Type (T)));
4964 Set_Scalar_Range (Id, Scalar_Range (T));
4965 Set_Is_Character_Type (Id, Is_Character_Type (T));
4966 Set_Is_Constrained (Id, Is_Constrained (T));
4967 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4968 Set_RM_Size (Id, RM_Size (T));
4969 Inherit_Predicate_Flags (Id, T);
4971 when Ordinary_Fixed_Point_Kind =>
4972 Set_Ekind (Id, E_Ordinary_Fixed_Point_Subtype);
4973 Set_Scalar_Range (Id, Scalar_Range (T));
4974 Set_Small_Value (Id, Small_Value (T));
4975 Set_Delta_Value (Id, Delta_Value (T));
4976 Set_Is_Constrained (Id, Is_Constrained (T));
4977 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4978 Set_RM_Size (Id, RM_Size (T));
4980 when Float_Kind =>
4981 Set_Ekind (Id, E_Floating_Point_Subtype);
4982 Set_Scalar_Range (Id, Scalar_Range (T));
4983 Set_Digits_Value (Id, Digits_Value (T));
4984 Set_Is_Constrained (Id, Is_Constrained (T));
4986 when Signed_Integer_Kind =>
4987 Set_Ekind (Id, E_Signed_Integer_Subtype);
4988 Set_Scalar_Range (Id, Scalar_Range (T));
4989 Set_Is_Constrained (Id, Is_Constrained (T));
4990 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4991 Set_RM_Size (Id, RM_Size (T));
4992 Inherit_Predicate_Flags (Id, T);
4994 when Modular_Integer_Kind =>
4995 Set_Ekind (Id, E_Modular_Integer_Subtype);
4996 Set_Scalar_Range (Id, Scalar_Range (T));
4997 Set_Is_Constrained (Id, Is_Constrained (T));
4998 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4999 Set_RM_Size (Id, RM_Size (T));
5000 Inherit_Predicate_Flags (Id, T);
5002 when Class_Wide_Kind =>
5003 Set_Ekind (Id, E_Class_Wide_Subtype);
5004 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5005 Set_Cloned_Subtype (Id, T);
5006 Set_Is_Tagged_Type (Id, True);
5007 Set_Has_Unknown_Discriminants
5008 (Id, True);
5009 Set_No_Tagged_Streams_Pragma
5010 (Id, No_Tagged_Streams_Pragma (T));
5012 if Ekind (T) = E_Class_Wide_Subtype then
5013 Set_Equivalent_Type (Id, Equivalent_Type (T));
5014 end if;
5016 when E_Record_Type | E_Record_Subtype =>
5017 Set_Ekind (Id, E_Record_Subtype);
5019 if Ekind (T) = E_Record_Subtype
5020 and then Present (Cloned_Subtype (T))
5021 then
5022 Set_Cloned_Subtype (Id, Cloned_Subtype (T));
5023 else
5024 Set_Cloned_Subtype (Id, T);
5025 end if;
5027 Set_First_Entity (Id, First_Entity (T));
5028 Set_Last_Entity (Id, Last_Entity (T));
5029 Set_Has_Discriminants (Id, Has_Discriminants (T));
5030 Set_Is_Constrained (Id, Is_Constrained (T));
5031 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5032 Set_Has_Implicit_Dereference
5033 (Id, Has_Implicit_Dereference (T));
5034 Set_Has_Unknown_Discriminants
5035 (Id, Has_Unknown_Discriminants (T));
5037 if Has_Discriminants (T) then
5038 Set_Discriminant_Constraint
5039 (Id, Discriminant_Constraint (T));
5040 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5042 elsif Has_Unknown_Discriminants (Id) then
5043 Set_Discriminant_Constraint (Id, No_Elist);
5044 end if;
5046 if Is_Tagged_Type (T) then
5047 Set_Is_Tagged_Type (Id, True);
5048 Set_No_Tagged_Streams_Pragma
5049 (Id, No_Tagged_Streams_Pragma (T));
5050 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5051 Set_Direct_Primitive_Operations
5052 (Id, Direct_Primitive_Operations (T));
5053 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5055 if Is_Interface (T) then
5056 Set_Is_Interface (Id);
5057 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
5058 end if;
5059 end if;
5061 when Private_Kind =>
5062 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
5063 Set_Has_Discriminants (Id, Has_Discriminants (T));
5064 Set_Is_Constrained (Id, Is_Constrained (T));
5065 Set_First_Entity (Id, First_Entity (T));
5066 Set_Last_Entity (Id, Last_Entity (T));
5067 Set_Private_Dependents (Id, New_Elmt_List);
5068 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5069 Set_Has_Implicit_Dereference
5070 (Id, Has_Implicit_Dereference (T));
5071 Set_Has_Unknown_Discriminants
5072 (Id, Has_Unknown_Discriminants (T));
5073 Set_Known_To_Have_Preelab_Init
5074 (Id, Known_To_Have_Preelab_Init (T));
5076 if Is_Tagged_Type (T) then
5077 Set_Is_Tagged_Type (Id);
5078 Set_No_Tagged_Streams_Pragma (Id,
5079 No_Tagged_Streams_Pragma (T));
5080 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5081 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5082 Set_Direct_Primitive_Operations (Id,
5083 Direct_Primitive_Operations (T));
5084 end if;
5086 -- In general the attributes of the subtype of a private type
5087 -- are the attributes of the partial view of parent. However,
5088 -- the full view may be a discriminated type, and the subtype
5089 -- must share the discriminant constraint to generate correct
5090 -- calls to initialization procedures.
5092 if Has_Discriminants (T) then
5093 Set_Discriminant_Constraint
5094 (Id, Discriminant_Constraint (T));
5095 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5097 elsif Present (Full_View (T))
5098 and then Has_Discriminants (Full_View (T))
5099 then
5100 Set_Discriminant_Constraint
5101 (Id, Discriminant_Constraint (Full_View (T)));
5102 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5104 -- This would seem semantically correct, but apparently
5105 -- generates spurious errors about missing components ???
5107 -- Set_Has_Discriminants (Id);
5108 end if;
5110 Prepare_Private_Subtype_Completion (Id, N);
5112 -- If this is the subtype of a constrained private type with
5113 -- discriminants that has got a full view and we also have
5114 -- built a completion just above, show that the completion
5115 -- is a clone of the full view to the back-end.
5117 if Has_Discriminants (T)
5118 and then not Has_Unknown_Discriminants (T)
5119 and then not Is_Empty_Elmt_List (Discriminant_Constraint (T))
5120 and then Present (Full_View (T))
5121 and then Present (Full_View (Id))
5122 then
5123 Set_Cloned_Subtype (Full_View (Id), Full_View (T));
5124 end if;
5126 when Access_Kind =>
5127 Set_Ekind (Id, E_Access_Subtype);
5128 Set_Is_Constrained (Id, Is_Constrained (T));
5129 Set_Is_Access_Constant
5130 (Id, Is_Access_Constant (T));
5131 Set_Directly_Designated_Type
5132 (Id, Designated_Type (T));
5133 Set_Can_Never_Be_Null (Id, Can_Never_Be_Null (T));
5135 -- A Pure library_item must not contain the declaration of a
5136 -- named access type, except within a subprogram, generic
5137 -- subprogram, task unit, or protected unit, or if it has
5138 -- a specified Storage_Size of zero (RM05-10.2.1(15.4-15.5)).
5140 if Comes_From_Source (Id)
5141 and then In_Pure_Unit
5142 and then not In_Subprogram_Task_Protected_Unit
5143 and then not No_Pool_Assigned (Id)
5144 then
5145 Error_Msg_N
5146 ("named access types not allowed in pure unit", N);
5147 end if;
5149 when Concurrent_Kind =>
5150 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
5151 Set_Corresponding_Record_Type (Id,
5152 Corresponding_Record_Type (T));
5153 Set_First_Entity (Id, First_Entity (T));
5154 Set_First_Private_Entity (Id, First_Private_Entity (T));
5155 Set_Has_Discriminants (Id, Has_Discriminants (T));
5156 Set_Is_Constrained (Id, Is_Constrained (T));
5157 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5158 Set_Last_Entity (Id, Last_Entity (T));
5160 if Is_Tagged_Type (T) then
5161 Set_No_Tagged_Streams_Pragma
5162 (Id, No_Tagged_Streams_Pragma (T));
5163 end if;
5165 if Has_Discriminants (T) then
5166 Set_Discriminant_Constraint
5167 (Id, Discriminant_Constraint (T));
5168 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5169 end if;
5171 when Incomplete_Kind =>
5172 if Ada_Version >= Ada_2005 then
5174 -- In Ada 2005 an incomplete type can be explicitly tagged:
5175 -- propagate indication. Note that we also have to include
5176 -- subtypes for Ada 2012 extended use of incomplete types.
5178 Set_Ekind (Id, E_Incomplete_Subtype);
5179 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5180 Set_Private_Dependents (Id, New_Elmt_List);
5182 if Is_Tagged_Type (Id) then
5183 Set_No_Tagged_Streams_Pragma
5184 (Id, No_Tagged_Streams_Pragma (T));
5185 Set_Direct_Primitive_Operations (Id, New_Elmt_List);
5186 end if;
5188 -- Ada 2005 (AI-412): Decorate an incomplete subtype of an
5189 -- incomplete type visible through a limited with clause.
5191 if From_Limited_With (T)
5192 and then Present (Non_Limited_View (T))
5193 then
5194 Set_From_Limited_With (Id);
5195 Set_Non_Limited_View (Id, Non_Limited_View (T));
5197 -- Ada 2005 (AI-412): Add the regular incomplete subtype
5198 -- to the private dependents of the original incomplete
5199 -- type for future transformation.
5201 else
5202 Append_Elmt (Id, Private_Dependents (T));
5203 end if;
5205 -- If the subtype name denotes an incomplete type an error
5206 -- was already reported by Process_Subtype.
5208 else
5209 Set_Etype (Id, Any_Type);
5210 end if;
5212 when others =>
5213 raise Program_Error;
5214 end case;
5215 end if;
5217 if Etype (Id) = Any_Type then
5218 goto Leave;
5219 end if;
5221 -- Some common processing on all types
5223 Set_Size_Info (Id, T);
5224 Set_First_Rep_Item (Id, First_Rep_Item (T));
5226 -- If the parent type is a generic actual, so is the subtype. This may
5227 -- happen in a nested instance. Why Comes_From_Source test???
5229 if not Comes_From_Source (N) then
5230 Set_Is_Generic_Actual_Type (Id, Is_Generic_Actual_Type (T));
5231 end if;
5233 T := Etype (Id);
5235 Set_Is_Immediately_Visible (Id, True);
5236 Set_Depends_On_Private (Id, Has_Private_Component (T));
5237 Set_Is_Descendent_Of_Address (Id, Is_Descendent_Of_Address (T));
5239 if Is_Interface (T) then
5240 Set_Is_Interface (Id);
5241 end if;
5243 if Present (Generic_Parent_Type (N))
5244 and then
5245 (Nkind (Parent (Generic_Parent_Type (N))) /=
5246 N_Formal_Type_Declaration
5247 or else Nkind (Formal_Type_Definition
5248 (Parent (Generic_Parent_Type (N)))) /=
5249 N_Formal_Private_Type_Definition)
5250 then
5251 if Is_Tagged_Type (Id) then
5253 -- If this is a generic actual subtype for a synchronized type,
5254 -- the primitive operations are those of the corresponding record
5255 -- for which there is a separate subtype declaration.
5257 if Is_Concurrent_Type (Id) then
5258 null;
5259 elsif Is_Class_Wide_Type (Id) then
5260 Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
5261 else
5262 Derive_Subprograms (Generic_Parent_Type (N), Id, T);
5263 end if;
5265 elsif Scope (Etype (Id)) /= Standard_Standard then
5266 Derive_Subprograms (Generic_Parent_Type (N), Id);
5267 end if;
5268 end if;
5270 if Is_Private_Type (T) and then Present (Full_View (T)) then
5271 Conditional_Delay (Id, Full_View (T));
5273 -- The subtypes of components or subcomponents of protected types
5274 -- do not need freeze nodes, which would otherwise appear in the
5275 -- wrong scope (before the freeze node for the protected type). The
5276 -- proper subtypes are those of the subcomponents of the corresponding
5277 -- record.
5279 elsif Ekind (Scope (Id)) /= E_Protected_Type
5280 and then Present (Scope (Scope (Id))) -- error defense
5281 and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
5282 then
5283 Conditional_Delay (Id, T);
5284 end if;
5286 -- Check that Constraint_Error is raised for a scalar subtype indication
5287 -- when the lower or upper bound of a non-null range lies outside the
5288 -- range of the type mark.
5290 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5291 if Is_Scalar_Type (Etype (Id))
5292 and then Scalar_Range (Id) /=
5293 Scalar_Range (Etype (Subtype_Mark
5294 (Subtype_Indication (N))))
5295 then
5296 Apply_Range_Check
5297 (Scalar_Range (Id),
5298 Etype (Subtype_Mark (Subtype_Indication (N))));
5300 -- In the array case, check compatibility for each index
5302 elsif Is_Array_Type (Etype (Id)) and then Present (First_Index (Id))
5303 then
5304 -- This really should be a subprogram that finds the indications
5305 -- to check???
5307 declare
5308 Subt_Index : Node_Id := First_Index (Id);
5309 Target_Index : Node_Id :=
5310 First_Index (Etype
5311 (Subtype_Mark (Subtype_Indication (N))));
5312 Has_Dyn_Chk : Boolean := Has_Dynamic_Range_Check (N);
5314 begin
5315 while Present (Subt_Index) loop
5316 if ((Nkind (Subt_Index) = N_Identifier
5317 and then Ekind (Entity (Subt_Index)) in Scalar_Kind)
5318 or else Nkind (Subt_Index) = N_Subtype_Indication)
5319 and then
5320 Nkind (Scalar_Range (Etype (Subt_Index))) = N_Range
5321 then
5322 declare
5323 Target_Typ : constant Entity_Id :=
5324 Etype (Target_Index);
5325 begin
5326 R_Checks :=
5327 Get_Range_Checks
5328 (Scalar_Range (Etype (Subt_Index)),
5329 Target_Typ,
5330 Etype (Subt_Index),
5331 Defining_Identifier (N));
5333 -- Reset Has_Dynamic_Range_Check on the subtype to
5334 -- prevent elision of the index check due to a dynamic
5335 -- check generated for a preceding index (needed since
5336 -- Insert_Range_Checks tries to avoid generating
5337 -- redundant checks on a given declaration).
5339 Set_Has_Dynamic_Range_Check (N, False);
5341 Insert_Range_Checks
5342 (R_Checks,
5344 Target_Typ,
5345 Sloc (Defining_Identifier (N)));
5347 -- Record whether this index involved a dynamic check
5349 Has_Dyn_Chk :=
5350 Has_Dyn_Chk or else Has_Dynamic_Range_Check (N);
5351 end;
5352 end if;
5354 Next_Index (Subt_Index);
5355 Next_Index (Target_Index);
5356 end loop;
5358 -- Finally, mark whether the subtype involves dynamic checks
5360 Set_Has_Dynamic_Range_Check (N, Has_Dyn_Chk);
5361 end;
5362 end if;
5363 end if;
5365 -- A type invariant applies to any subtype in its scope, in particular
5366 -- to a generic actual.
5368 if Has_Invariants (T) and then In_Open_Scopes (Scope (T)) then
5369 Set_Has_Invariants (Id);
5370 Set_Invariant_Procedure (Id, Invariant_Procedure (T));
5371 end if;
5373 -- Make sure that generic actual types are properly frozen. The subtype
5374 -- is marked as a generic actual type when the enclosing instance is
5375 -- analyzed, so here we identify the subtype from the tree structure.
5377 if Expander_Active
5378 and then Is_Generic_Actual_Type (Id)
5379 and then In_Instance
5380 and then not Comes_From_Source (N)
5381 and then Nkind (Subtype_Indication (N)) /= N_Subtype_Indication
5382 and then Is_Frozen (T)
5383 then
5384 Freeze_Before (N, Id);
5385 end if;
5387 Set_Optimize_Alignment_Flags (Id);
5388 Check_Eliminated (Id);
5390 <<Leave>>
5391 if Has_Aspects (N) then
5392 Analyze_Aspect_Specifications (N, Id);
5393 end if;
5395 Analyze_Dimension (N);
5397 -- Restore the original Ghost mode once analysis and expansion have
5398 -- taken place.
5400 Ghost_Mode := GM;
5401 end Analyze_Subtype_Declaration;
5403 --------------------------------
5404 -- Analyze_Subtype_Indication --
5405 --------------------------------
5407 procedure Analyze_Subtype_Indication (N : Node_Id) is
5408 T : constant Entity_Id := Subtype_Mark (N);
5409 R : constant Node_Id := Range_Expression (Constraint (N));
5411 begin
5412 Analyze (T);
5414 if R /= Error then
5415 Analyze (R);
5416 Set_Etype (N, Etype (R));
5417 Resolve (R, Entity (T));
5418 else
5419 Set_Error_Posted (R);
5420 Set_Error_Posted (T);
5421 end if;
5422 end Analyze_Subtype_Indication;
5424 --------------------------
5425 -- Analyze_Variant_Part --
5426 --------------------------
5428 procedure Analyze_Variant_Part (N : Node_Id) is
5429 Discr_Name : Node_Id;
5430 Discr_Type : Entity_Id;
5432 procedure Process_Variant (A : Node_Id);
5433 -- Analyze declarations for a single variant
5435 package Analyze_Variant_Choices is
5436 new Generic_Analyze_Choices (Process_Variant);
5437 use Analyze_Variant_Choices;
5439 ---------------------
5440 -- Process_Variant --
5441 ---------------------
5443 procedure Process_Variant (A : Node_Id) is
5444 CL : constant Node_Id := Component_List (A);
5445 begin
5446 if not Null_Present (CL) then
5447 Analyze_Declarations (Component_Items (CL));
5449 if Present (Variant_Part (CL)) then
5450 Analyze (Variant_Part (CL));
5451 end if;
5452 end if;
5453 end Process_Variant;
5455 -- Start of processing for Analyze_Variant_Part
5457 begin
5458 Discr_Name := Name (N);
5459 Analyze (Discr_Name);
5461 -- If Discr_Name bad, get out (prevent cascaded errors)
5463 if Etype (Discr_Name) = Any_Type then
5464 return;
5465 end if;
5467 -- Check invalid discriminant in variant part
5469 if Ekind (Entity (Discr_Name)) /= E_Discriminant then
5470 Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
5471 end if;
5473 Discr_Type := Etype (Entity (Discr_Name));
5475 if not Is_Discrete_Type (Discr_Type) then
5476 Error_Msg_N
5477 ("discriminant in a variant part must be of a discrete type",
5478 Name (N));
5479 return;
5480 end if;
5482 -- Now analyze the choices, which also analyzes the declarations that
5483 -- are associated with each choice.
5485 Analyze_Choices (Variants (N), Discr_Type);
5487 -- Note: we used to instantiate and call Check_Choices here to check
5488 -- that the choices covered the discriminant, but it's too early to do
5489 -- that because of statically predicated subtypes, whose analysis may
5490 -- be deferred to their freeze point which may be as late as the freeze
5491 -- point of the containing record. So this call is now to be found in
5492 -- Freeze_Record_Declaration.
5494 end Analyze_Variant_Part;
5496 ----------------------------
5497 -- Array_Type_Declaration --
5498 ----------------------------
5500 procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
5501 Component_Def : constant Node_Id := Component_Definition (Def);
5502 Component_Typ : constant Node_Id := Subtype_Indication (Component_Def);
5503 Element_Type : Entity_Id;
5504 Implicit_Base : Entity_Id;
5505 Index : Node_Id;
5506 Related_Id : Entity_Id := Empty;
5507 Nb_Index : Nat;
5508 P : constant Node_Id := Parent (Def);
5509 Priv : Entity_Id;
5511 begin
5512 if Nkind (Def) = N_Constrained_Array_Definition then
5513 Index := First (Discrete_Subtype_Definitions (Def));
5514 else
5515 Index := First (Subtype_Marks (Def));
5516 end if;
5518 -- Find proper names for the implicit types which may be public. In case
5519 -- of anonymous arrays we use the name of the first object of that type
5520 -- as prefix.
5522 if No (T) then
5523 Related_Id := Defining_Identifier (P);
5524 else
5525 Related_Id := T;
5526 end if;
5528 Nb_Index := 1;
5529 while Present (Index) loop
5530 Analyze (Index);
5532 -- Test for odd case of trying to index a type by the type itself
5534 if Is_Entity_Name (Index) and then Entity (Index) = T then
5535 Error_Msg_N ("type& cannot be indexed by itself", Index);
5536 Set_Entity (Index, Standard_Boolean);
5537 Set_Etype (Index, Standard_Boolean);
5538 end if;
5540 -- Check SPARK restriction requiring a subtype mark
5542 if not Nkind_In (Index, N_Identifier, N_Expanded_Name) then
5543 Check_SPARK_05_Restriction ("subtype mark required", Index);
5544 end if;
5546 -- Add a subtype declaration for each index of private array type
5547 -- declaration whose etype is also private. For example:
5549 -- package Pkg is
5550 -- type Index is private;
5551 -- private
5552 -- type Table is array (Index) of ...
5553 -- end;
5555 -- This is currently required by the expander for the internally
5556 -- generated equality subprogram of records with variant parts in
5557 -- which the etype of some component is such private type.
5559 if Ekind (Current_Scope) = E_Package
5560 and then In_Private_Part (Current_Scope)
5561 and then Has_Private_Declaration (Etype (Index))
5562 then
5563 declare
5564 Loc : constant Source_Ptr := Sloc (Def);
5565 New_E : Entity_Id;
5566 Decl : Entity_Id;
5568 begin
5569 New_E := Make_Temporary (Loc, 'T');
5570 Set_Is_Internal (New_E);
5572 Decl :=
5573 Make_Subtype_Declaration (Loc,
5574 Defining_Identifier => New_E,
5575 Subtype_Indication =>
5576 New_Occurrence_Of (Etype (Index), Loc));
5578 Insert_Before (Parent (Def), Decl);
5579 Analyze (Decl);
5580 Set_Etype (Index, New_E);
5582 -- If the index is a range the Entity attribute is not
5583 -- available. Example:
5585 -- package Pkg is
5586 -- type T is private;
5587 -- private
5588 -- type T is new Natural;
5589 -- Table : array (T(1) .. T(10)) of Boolean;
5590 -- end Pkg;
5592 if Nkind (Index) /= N_Range then
5593 Set_Entity (Index, New_E);
5594 end if;
5595 end;
5596 end if;
5598 Make_Index (Index, P, Related_Id, Nb_Index);
5600 -- Check error of subtype with predicate for index type
5602 Bad_Predicated_Subtype_Use
5603 ("subtype& has predicate, not allowed as index subtype",
5604 Index, Etype (Index));
5606 -- Move to next index
5608 Next_Index (Index);
5609 Nb_Index := Nb_Index + 1;
5610 end loop;
5612 -- Process subtype indication if one is present
5614 if Present (Component_Typ) then
5615 Element_Type := Process_Subtype (Component_Typ, P, Related_Id, 'C');
5617 Set_Etype (Component_Typ, Element_Type);
5619 if not Nkind_In (Component_Typ, N_Identifier, N_Expanded_Name) then
5620 Check_SPARK_05_Restriction
5621 ("subtype mark required", Component_Typ);
5622 end if;
5624 -- Ada 2005 (AI-230): Access Definition case
5626 else pragma Assert (Present (Access_Definition (Component_Def)));
5628 -- Indicate that the anonymous access type is created by the
5629 -- array type declaration.
5631 Element_Type := Access_Definition
5632 (Related_Nod => P,
5633 N => Access_Definition (Component_Def));
5634 Set_Is_Local_Anonymous_Access (Element_Type);
5636 -- Propagate the parent. This field is needed if we have to generate
5637 -- the master_id associated with an anonymous access to task type
5638 -- component (see Expand_N_Full_Type_Declaration.Build_Master)
5640 Set_Parent (Element_Type, Parent (T));
5642 -- Ada 2005 (AI-230): In case of components that are anonymous access
5643 -- types the level of accessibility depends on the enclosing type
5644 -- declaration
5646 Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
5648 -- Ada 2005 (AI-254)
5650 declare
5651 CD : constant Node_Id :=
5652 Access_To_Subprogram_Definition
5653 (Access_Definition (Component_Def));
5654 begin
5655 if Present (CD) and then Protected_Present (CD) then
5656 Element_Type :=
5657 Replace_Anonymous_Access_To_Protected_Subprogram (Def);
5658 end if;
5659 end;
5660 end if;
5662 -- Constrained array case
5664 if No (T) then
5665 T := Create_Itype (E_Void, P, Related_Id, 'T');
5666 end if;
5668 if Nkind (Def) = N_Constrained_Array_Definition then
5670 -- Establish Implicit_Base as unconstrained base type
5672 Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
5674 Set_Etype (Implicit_Base, Implicit_Base);
5675 Set_Scope (Implicit_Base, Current_Scope);
5676 Set_Has_Delayed_Freeze (Implicit_Base);
5677 Set_Default_SSO (Implicit_Base);
5679 -- The constrained array type is a subtype of the unconstrained one
5681 Set_Ekind (T, E_Array_Subtype);
5682 Init_Size_Align (T);
5683 Set_Etype (T, Implicit_Base);
5684 Set_Scope (T, Current_Scope);
5685 Set_Is_Constrained (T);
5686 Set_First_Index (T,
5687 First (Discrete_Subtype_Definitions (Def)));
5688 Set_Has_Delayed_Freeze (T);
5690 -- Complete setup of implicit base type
5692 Set_First_Index (Implicit_Base, First_Index (T));
5693 Set_Component_Type (Implicit_Base, Element_Type);
5694 Set_Has_Task (Implicit_Base, Has_Task (Element_Type));
5695 Set_Has_Protected (Implicit_Base, Has_Protected (Element_Type));
5696 Set_Component_Size (Implicit_Base, Uint_0);
5697 Set_Packed_Array_Impl_Type (Implicit_Base, Empty);
5698 Set_Has_Controlled_Component (Implicit_Base,
5699 Has_Controlled_Component (Element_Type)
5700 or else Is_Controlled_Active (Element_Type));
5701 Set_Finalize_Storage_Only (Implicit_Base,
5702 Finalize_Storage_Only (Element_Type));
5704 -- Inherit the "ghostness" from the constrained array type
5706 if Is_Ghost_Entity (T) or else Ghost_Mode > None then
5707 Set_Is_Ghost_Entity (Implicit_Base);
5708 end if;
5710 -- Unconstrained array case
5712 else
5713 Set_Ekind (T, E_Array_Type);
5714 Init_Size_Align (T);
5715 Set_Etype (T, T);
5716 Set_Scope (T, Current_Scope);
5717 Set_Component_Size (T, Uint_0);
5718 Set_Is_Constrained (T, False);
5719 Set_First_Index (T, First (Subtype_Marks (Def)));
5720 Set_Has_Delayed_Freeze (T, True);
5721 Set_Has_Task (T, Has_Task (Element_Type));
5722 Set_Has_Protected (T, Has_Protected (Element_Type));
5723 Set_Has_Controlled_Component (T, Has_Controlled_Component
5724 (Element_Type)
5725 or else
5726 Is_Controlled_Active (Element_Type));
5727 Set_Finalize_Storage_Only (T, Finalize_Storage_Only
5728 (Element_Type));
5729 Set_Default_SSO (T);
5730 end if;
5732 -- Common attributes for both cases
5734 Set_Component_Type (Base_Type (T), Element_Type);
5735 Set_Packed_Array_Impl_Type (T, Empty);
5737 if Aliased_Present (Component_Definition (Def)) then
5738 Check_SPARK_05_Restriction
5739 ("aliased is not allowed", Component_Definition (Def));
5740 Set_Has_Aliased_Components (Etype (T));
5741 end if;
5743 -- Ada 2005 (AI-231): Propagate the null-excluding attribute to the
5744 -- array type to ensure that objects of this type are initialized.
5746 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (Element_Type) then
5747 Set_Can_Never_Be_Null (T);
5749 if Null_Exclusion_Present (Component_Definition (Def))
5751 -- No need to check itypes because in their case this check was
5752 -- done at their point of creation
5754 and then not Is_Itype (Element_Type)
5755 then
5756 Error_Msg_N
5757 ("`NOT NULL` not allowed (null already excluded)",
5758 Subtype_Indication (Component_Definition (Def)));
5759 end if;
5760 end if;
5762 Priv := Private_Component (Element_Type);
5764 if Present (Priv) then
5766 -- Check for circular definitions
5768 if Priv = Any_Type then
5769 Set_Component_Type (Etype (T), Any_Type);
5771 -- There is a gap in the visibility of operations on the composite
5772 -- type only if the component type is defined in a different scope.
5774 elsif Scope (Priv) = Current_Scope then
5775 null;
5777 elsif Is_Limited_Type (Priv) then
5778 Set_Is_Limited_Composite (Etype (T));
5779 Set_Is_Limited_Composite (T);
5780 else
5781 Set_Is_Private_Composite (Etype (T));
5782 Set_Is_Private_Composite (T);
5783 end if;
5784 end if;
5786 -- A syntax error in the declaration itself may lead to an empty index
5787 -- list, in which case do a minimal patch.
5789 if No (First_Index (T)) then
5790 Error_Msg_N ("missing index definition in array type declaration", T);
5792 declare
5793 Indexes : constant List_Id :=
5794 New_List (New_Occurrence_Of (Any_Id, Sloc (T)));
5795 begin
5796 Set_Discrete_Subtype_Definitions (Def, Indexes);
5797 Set_First_Index (T, First (Indexes));
5798 return;
5799 end;
5800 end if;
5802 -- Create a concatenation operator for the new type. Internal array
5803 -- types created for packed entities do not need such, they are
5804 -- compatible with the user-defined type.
5806 if Number_Dimensions (T) = 1
5807 and then not Is_Packed_Array_Impl_Type (T)
5808 then
5809 New_Concatenation_Op (T);
5810 end if;
5812 -- In the case of an unconstrained array the parser has already verified
5813 -- that all the indexes are unconstrained but we still need to make sure
5814 -- that the element type is constrained.
5816 if not Is_Definite_Subtype (Element_Type) then
5817 Error_Msg_N
5818 ("unconstrained element type in array declaration",
5819 Subtype_Indication (Component_Def));
5821 elsif Is_Abstract_Type (Element_Type) then
5822 Error_Msg_N
5823 ("the type of a component cannot be abstract",
5824 Subtype_Indication (Component_Def));
5825 end if;
5827 -- There may be an invariant declared for the component type, but
5828 -- the construction of the component invariant checking procedure
5829 -- takes place during expansion.
5830 end Array_Type_Declaration;
5832 ------------------------------------------------------
5833 -- Replace_Anonymous_Access_To_Protected_Subprogram --
5834 ------------------------------------------------------
5836 function Replace_Anonymous_Access_To_Protected_Subprogram
5837 (N : Node_Id) return Entity_Id
5839 Loc : constant Source_Ptr := Sloc (N);
5841 Curr_Scope : constant Scope_Stack_Entry :=
5842 Scope_Stack.Table (Scope_Stack.Last);
5844 Anon : constant Entity_Id := Make_Temporary (Loc, 'S');
5846 Acc : Node_Id;
5847 -- Access definition in declaration
5849 Comp : Node_Id;
5850 -- Object definition or formal definition with an access definition
5852 Decl : Node_Id;
5853 -- Declaration of anonymous access to subprogram type
5855 Spec : Node_Id;
5856 -- Original specification in access to subprogram
5858 P : Node_Id;
5860 begin
5861 Set_Is_Internal (Anon);
5863 case Nkind (N) is
5864 when N_Component_Declaration |
5865 N_Unconstrained_Array_Definition |
5866 N_Constrained_Array_Definition =>
5867 Comp := Component_Definition (N);
5868 Acc := Access_Definition (Comp);
5870 when N_Discriminant_Specification =>
5871 Comp := Discriminant_Type (N);
5872 Acc := Comp;
5874 when N_Parameter_Specification =>
5875 Comp := Parameter_Type (N);
5876 Acc := Comp;
5878 when N_Access_Function_Definition =>
5879 Comp := Result_Definition (N);
5880 Acc := Comp;
5882 when N_Object_Declaration =>
5883 Comp := Object_Definition (N);
5884 Acc := Comp;
5886 when N_Function_Specification =>
5887 Comp := Result_Definition (N);
5888 Acc := Comp;
5890 when others =>
5891 raise Program_Error;
5892 end case;
5894 Spec := Access_To_Subprogram_Definition (Acc);
5896 Decl :=
5897 Make_Full_Type_Declaration (Loc,
5898 Defining_Identifier => Anon,
5899 Type_Definition => Copy_Separate_Tree (Spec));
5901 Mark_Rewrite_Insertion (Decl);
5903 -- In ASIS mode, analyze the profile on the original node, because
5904 -- the separate copy does not provide enough links to recover the
5905 -- original tree. Analysis is limited to type annotations, within
5906 -- a temporary scope that serves as an anonymous subprogram to collect
5907 -- otherwise useless temporaries and itypes.
5909 if ASIS_Mode then
5910 declare
5911 Typ : constant Entity_Id := Make_Temporary (Loc, 'S');
5913 begin
5914 if Nkind (Spec) = N_Access_Function_Definition then
5915 Set_Ekind (Typ, E_Function);
5916 else
5917 Set_Ekind (Typ, E_Procedure);
5918 end if;
5920 Set_Parent (Typ, N);
5921 Set_Scope (Typ, Current_Scope);
5922 Push_Scope (Typ);
5924 -- Nothing to do if procedure is parameterless
5926 if Present (Parameter_Specifications (Spec)) then
5927 Process_Formals (Parameter_Specifications (Spec), Spec);
5928 end if;
5930 if Nkind (Spec) = N_Access_Function_Definition then
5931 declare
5932 Def : constant Node_Id := Result_Definition (Spec);
5934 begin
5935 -- The result might itself be an anonymous access type, so
5936 -- have to recurse.
5938 if Nkind (Def) = N_Access_Definition then
5939 if Present (Access_To_Subprogram_Definition (Def)) then
5940 Set_Etype
5941 (Def,
5942 Replace_Anonymous_Access_To_Protected_Subprogram
5943 (Spec));
5944 else
5945 Find_Type (Subtype_Mark (Def));
5946 end if;
5948 else
5949 Find_Type (Def);
5950 end if;
5951 end;
5952 end if;
5954 End_Scope;
5955 end;
5956 end if;
5958 -- Insert the new declaration in the nearest enclosing scope. If the
5959 -- node is a body and N is its return type, the declaration belongs in
5960 -- the enclosing scope.
5962 P := Parent (N);
5964 if Nkind (P) = N_Subprogram_Body
5965 and then Nkind (N) = N_Function_Specification
5966 then
5967 P := Parent (P);
5968 end if;
5970 while Present (P) and then not Has_Declarations (P) loop
5971 P := Parent (P);
5972 end loop;
5974 pragma Assert (Present (P));
5976 if Nkind (P) = N_Package_Specification then
5977 Prepend (Decl, Visible_Declarations (P));
5978 else
5979 Prepend (Decl, Declarations (P));
5980 end if;
5982 -- Replace the anonymous type with an occurrence of the new declaration.
5983 -- In all cases the rewritten node does not have the null-exclusion
5984 -- attribute because (if present) it was already inherited by the
5985 -- anonymous entity (Anon). Thus, in case of components we do not
5986 -- inherit this attribute.
5988 if Nkind (N) = N_Parameter_Specification then
5989 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5990 Set_Etype (Defining_Identifier (N), Anon);
5991 Set_Null_Exclusion_Present (N, False);
5993 elsif Nkind (N) = N_Object_Declaration then
5994 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5995 Set_Etype (Defining_Identifier (N), Anon);
5997 elsif Nkind (N) = N_Access_Function_Definition then
5998 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6000 elsif Nkind (N) = N_Function_Specification then
6001 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6002 Set_Etype (Defining_Unit_Name (N), Anon);
6004 else
6005 Rewrite (Comp,
6006 Make_Component_Definition (Loc,
6007 Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
6008 end if;
6010 Mark_Rewrite_Insertion (Comp);
6012 if Nkind_In (N, N_Object_Declaration, N_Access_Function_Definition) then
6013 Analyze (Decl);
6015 else
6016 -- Temporarily remove the current scope (record or subprogram) from
6017 -- the stack to add the new declarations to the enclosing scope.
6019 Scope_Stack.Decrement_Last;
6020 Analyze (Decl);
6021 Set_Is_Itype (Anon);
6022 Scope_Stack.Append (Curr_Scope);
6023 end if;
6025 Set_Ekind (Anon, E_Anonymous_Access_Protected_Subprogram_Type);
6026 Set_Can_Use_Internal_Rep (Anon, not Always_Compatible_Rep_On_Target);
6027 return Anon;
6028 end Replace_Anonymous_Access_To_Protected_Subprogram;
6030 -------------------------------
6031 -- Build_Derived_Access_Type --
6032 -------------------------------
6034 procedure Build_Derived_Access_Type
6035 (N : Node_Id;
6036 Parent_Type : Entity_Id;
6037 Derived_Type : Entity_Id)
6039 S : constant Node_Id := Subtype_Indication (Type_Definition (N));
6041 Desig_Type : Entity_Id;
6042 Discr : Entity_Id;
6043 Discr_Con_Elist : Elist_Id;
6044 Discr_Con_El : Elmt_Id;
6045 Subt : Entity_Id;
6047 begin
6048 -- Set the designated type so it is available in case this is an access
6049 -- to a self-referential type, e.g. a standard list type with a next
6050 -- pointer. Will be reset after subtype is built.
6052 Set_Directly_Designated_Type
6053 (Derived_Type, Designated_Type (Parent_Type));
6055 Subt := Process_Subtype (S, N);
6057 if Nkind (S) /= N_Subtype_Indication
6058 and then Subt /= Base_Type (Subt)
6059 then
6060 Set_Ekind (Derived_Type, E_Access_Subtype);
6061 end if;
6063 if Ekind (Derived_Type) = E_Access_Subtype then
6064 declare
6065 Pbase : constant Entity_Id := Base_Type (Parent_Type);
6066 Ibase : constant Entity_Id :=
6067 Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
6068 Svg_Chars : constant Name_Id := Chars (Ibase);
6069 Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
6071 begin
6072 Copy_Node (Pbase, Ibase);
6074 Set_Chars (Ibase, Svg_Chars);
6075 Set_Next_Entity (Ibase, Svg_Next_E);
6076 Set_Sloc (Ibase, Sloc (Derived_Type));
6077 Set_Scope (Ibase, Scope (Derived_Type));
6078 Set_Freeze_Node (Ibase, Empty);
6079 Set_Is_Frozen (Ibase, False);
6080 Set_Comes_From_Source (Ibase, False);
6081 Set_Is_First_Subtype (Ibase, False);
6083 Set_Etype (Ibase, Pbase);
6084 Set_Etype (Derived_Type, Ibase);
6085 end;
6086 end if;
6088 Set_Directly_Designated_Type
6089 (Derived_Type, Designated_Type (Subt));
6091 Set_Is_Constrained (Derived_Type, Is_Constrained (Subt));
6092 Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
6093 Set_Size_Info (Derived_Type, Parent_Type);
6094 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
6095 Set_Depends_On_Private (Derived_Type,
6096 Has_Private_Component (Derived_Type));
6097 Conditional_Delay (Derived_Type, Subt);
6099 -- Ada 2005 (AI-231): Set the null-exclusion attribute, and verify
6100 -- that it is not redundant.
6102 if Null_Exclusion_Present (Type_Definition (N)) then
6103 Set_Can_Never_Be_Null (Derived_Type);
6105 -- What is with the "AND THEN FALSE" here ???
6107 if Can_Never_Be_Null (Parent_Type)
6108 and then False
6109 then
6110 Error_Msg_NE
6111 ("`NOT NULL` not allowed (& already excludes null)",
6112 N, Parent_Type);
6113 end if;
6115 elsif Can_Never_Be_Null (Parent_Type) then
6116 Set_Can_Never_Be_Null (Derived_Type);
6117 end if;
6119 -- Note: we do not copy the Storage_Size_Variable, since we always go to
6120 -- the root type for this information.
6122 -- Apply range checks to discriminants for derived record case
6123 -- ??? THIS CODE SHOULD NOT BE HERE REALLY.
6125 Desig_Type := Designated_Type (Derived_Type);
6126 if Is_Composite_Type (Desig_Type)
6127 and then (not Is_Array_Type (Desig_Type))
6128 and then Has_Discriminants (Desig_Type)
6129 and then Base_Type (Desig_Type) /= Desig_Type
6130 then
6131 Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
6132 Discr_Con_El := First_Elmt (Discr_Con_Elist);
6134 Discr := First_Discriminant (Base_Type (Desig_Type));
6135 while Present (Discr_Con_El) loop
6136 Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
6137 Next_Elmt (Discr_Con_El);
6138 Next_Discriminant (Discr);
6139 end loop;
6140 end if;
6141 end Build_Derived_Access_Type;
6143 ------------------------------
6144 -- Build_Derived_Array_Type --
6145 ------------------------------
6147 procedure Build_Derived_Array_Type
6148 (N : Node_Id;
6149 Parent_Type : Entity_Id;
6150 Derived_Type : Entity_Id)
6152 Loc : constant Source_Ptr := Sloc (N);
6153 Tdef : constant Node_Id := Type_Definition (N);
6154 Indic : constant Node_Id := Subtype_Indication (Tdef);
6155 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
6156 Implicit_Base : Entity_Id;
6157 New_Indic : Node_Id;
6159 procedure Make_Implicit_Base;
6160 -- If the parent subtype is constrained, the derived type is a subtype
6161 -- of an implicit base type derived from the parent base.
6163 ------------------------
6164 -- Make_Implicit_Base --
6165 ------------------------
6167 procedure Make_Implicit_Base is
6168 begin
6169 Implicit_Base :=
6170 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
6172 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
6173 Set_Etype (Implicit_Base, Parent_Base);
6175 Copy_Array_Subtype_Attributes (Implicit_Base, Parent_Base);
6176 Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
6178 Set_Has_Delayed_Freeze (Implicit_Base, True);
6180 -- Inherit the "ghostness" from the parent base type
6182 if Is_Ghost_Entity (Parent_Base) or else Ghost_Mode > None then
6183 Set_Is_Ghost_Entity (Implicit_Base);
6184 end if;
6185 end Make_Implicit_Base;
6187 -- Start of processing for Build_Derived_Array_Type
6189 begin
6190 if not Is_Constrained (Parent_Type) then
6191 if Nkind (Indic) /= N_Subtype_Indication then
6192 Set_Ekind (Derived_Type, E_Array_Type);
6194 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6195 Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
6197 Set_Has_Delayed_Freeze (Derived_Type, True);
6199 else
6200 Make_Implicit_Base;
6201 Set_Etype (Derived_Type, Implicit_Base);
6203 New_Indic :=
6204 Make_Subtype_Declaration (Loc,
6205 Defining_Identifier => Derived_Type,
6206 Subtype_Indication =>
6207 Make_Subtype_Indication (Loc,
6208 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
6209 Constraint => Constraint (Indic)));
6211 Rewrite (N, New_Indic);
6212 Analyze (N);
6213 end if;
6215 else
6216 if Nkind (Indic) /= N_Subtype_Indication then
6217 Make_Implicit_Base;
6219 Set_Ekind (Derived_Type, Ekind (Parent_Type));
6220 Set_Etype (Derived_Type, Implicit_Base);
6221 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6223 else
6224 Error_Msg_N ("illegal constraint on constrained type", Indic);
6225 end if;
6226 end if;
6228 -- If parent type is not a derived type itself, and is declared in
6229 -- closed scope (e.g. a subprogram), then we must explicitly introduce
6230 -- the new type's concatenation operator since Derive_Subprograms
6231 -- will not inherit the parent's operator. If the parent type is
6232 -- unconstrained, the operator is of the unconstrained base type.
6234 if Number_Dimensions (Parent_Type) = 1
6235 and then not Is_Limited_Type (Parent_Type)
6236 and then not Is_Derived_Type (Parent_Type)
6237 and then not Is_Package_Or_Generic_Package
6238 (Scope (Base_Type (Parent_Type)))
6239 then
6240 if not Is_Constrained (Parent_Type)
6241 and then Is_Constrained (Derived_Type)
6242 then
6243 New_Concatenation_Op (Implicit_Base);
6244 else
6245 New_Concatenation_Op (Derived_Type);
6246 end if;
6247 end if;
6248 end Build_Derived_Array_Type;
6250 -----------------------------------
6251 -- Build_Derived_Concurrent_Type --
6252 -----------------------------------
6254 procedure Build_Derived_Concurrent_Type
6255 (N : Node_Id;
6256 Parent_Type : Entity_Id;
6257 Derived_Type : Entity_Id)
6259 Loc : constant Source_Ptr := Sloc (N);
6261 Corr_Record : constant Entity_Id := Make_Temporary (Loc, 'C');
6262 Corr_Decl : Node_Id;
6263 Corr_Decl_Needed : Boolean;
6264 -- If the derived type has fewer discriminants than its parent, the
6265 -- corresponding record is also a derived type, in order to account for
6266 -- the bound discriminants. We create a full type declaration for it in
6267 -- this case.
6269 Constraint_Present : constant Boolean :=
6270 Nkind (Subtype_Indication (Type_Definition (N))) =
6271 N_Subtype_Indication;
6273 D_Constraint : Node_Id;
6274 New_Constraint : Elist_Id;
6275 Old_Disc : Entity_Id;
6276 New_Disc : Entity_Id;
6277 New_N : Node_Id;
6279 begin
6280 Set_Stored_Constraint (Derived_Type, No_Elist);
6281 Corr_Decl_Needed := False;
6282 Old_Disc := Empty;
6284 if Present (Discriminant_Specifications (N))
6285 and then Constraint_Present
6286 then
6287 Old_Disc := First_Discriminant (Parent_Type);
6288 New_Disc := First (Discriminant_Specifications (N));
6289 while Present (New_Disc) and then Present (Old_Disc) loop
6290 Next_Discriminant (Old_Disc);
6291 Next (New_Disc);
6292 end loop;
6293 end if;
6295 if Present (Old_Disc) and then Expander_Active then
6297 -- The new type has fewer discriminants, so we need to create a new
6298 -- corresponding record, which is derived from the corresponding
6299 -- record of the parent, and has a stored constraint that captures
6300 -- the values of the discriminant constraints. The corresponding
6301 -- record is needed only if expander is active and code generation is
6302 -- enabled.
6304 -- The type declaration for the derived corresponding record has the
6305 -- same discriminant part and constraints as the current declaration.
6306 -- Copy the unanalyzed tree to build declaration.
6308 Corr_Decl_Needed := True;
6309 New_N := Copy_Separate_Tree (N);
6311 Corr_Decl :=
6312 Make_Full_Type_Declaration (Loc,
6313 Defining_Identifier => Corr_Record,
6314 Discriminant_Specifications =>
6315 Discriminant_Specifications (New_N),
6316 Type_Definition =>
6317 Make_Derived_Type_Definition (Loc,
6318 Subtype_Indication =>
6319 Make_Subtype_Indication (Loc,
6320 Subtype_Mark =>
6321 New_Occurrence_Of
6322 (Corresponding_Record_Type (Parent_Type), Loc),
6323 Constraint =>
6324 Constraint
6325 (Subtype_Indication (Type_Definition (New_N))))));
6326 end if;
6328 -- Copy Storage_Size and Relative_Deadline variables if task case
6330 if Is_Task_Type (Parent_Type) then
6331 Set_Storage_Size_Variable (Derived_Type,
6332 Storage_Size_Variable (Parent_Type));
6333 Set_Relative_Deadline_Variable (Derived_Type,
6334 Relative_Deadline_Variable (Parent_Type));
6335 end if;
6337 if Present (Discriminant_Specifications (N)) then
6338 Push_Scope (Derived_Type);
6339 Check_Or_Process_Discriminants (N, Derived_Type);
6341 if Constraint_Present then
6342 New_Constraint :=
6343 Expand_To_Stored_Constraint
6344 (Parent_Type,
6345 Build_Discriminant_Constraints
6346 (Parent_Type,
6347 Subtype_Indication (Type_Definition (N)), True));
6348 end if;
6350 End_Scope;
6352 elsif Constraint_Present then
6354 -- Build constrained subtype, copying the constraint, and derive
6355 -- from it to create a derived constrained type.
6357 declare
6358 Loc : constant Source_Ptr := Sloc (N);
6359 Anon : constant Entity_Id :=
6360 Make_Defining_Identifier (Loc,
6361 Chars => New_External_Name (Chars (Derived_Type), 'T'));
6362 Decl : Node_Id;
6364 begin
6365 Decl :=
6366 Make_Subtype_Declaration (Loc,
6367 Defining_Identifier => Anon,
6368 Subtype_Indication =>
6369 New_Copy_Tree (Subtype_Indication (Type_Definition (N))));
6370 Insert_Before (N, Decl);
6371 Analyze (Decl);
6373 Rewrite (Subtype_Indication (Type_Definition (N)),
6374 New_Occurrence_Of (Anon, Loc));
6375 Set_Analyzed (Derived_Type, False);
6376 Analyze (N);
6377 return;
6378 end;
6379 end if;
6381 -- By default, operations and private data are inherited from parent.
6382 -- However, in the presence of bound discriminants, a new corresponding
6383 -- record will be created, see below.
6385 Set_Has_Discriminants
6386 (Derived_Type, Has_Discriminants (Parent_Type));
6387 Set_Corresponding_Record_Type
6388 (Derived_Type, Corresponding_Record_Type (Parent_Type));
6390 -- Is_Constrained is set according the parent subtype, but is set to
6391 -- False if the derived type is declared with new discriminants.
6393 Set_Is_Constrained
6394 (Derived_Type,
6395 (Is_Constrained (Parent_Type) or else Constraint_Present)
6396 and then not Present (Discriminant_Specifications (N)));
6398 if Constraint_Present then
6399 if not Has_Discriminants (Parent_Type) then
6400 Error_Msg_N ("untagged parent must have discriminants", N);
6402 elsif Present (Discriminant_Specifications (N)) then
6404 -- Verify that new discriminants are used to constrain old ones
6406 D_Constraint :=
6407 First
6408 (Constraints
6409 (Constraint (Subtype_Indication (Type_Definition (N)))));
6411 Old_Disc := First_Discriminant (Parent_Type);
6413 while Present (D_Constraint) loop
6414 if Nkind (D_Constraint) /= N_Discriminant_Association then
6416 -- Positional constraint. If it is a reference to a new
6417 -- discriminant, it constrains the corresponding old one.
6419 if Nkind (D_Constraint) = N_Identifier then
6420 New_Disc := First_Discriminant (Derived_Type);
6421 while Present (New_Disc) loop
6422 exit when Chars (New_Disc) = Chars (D_Constraint);
6423 Next_Discriminant (New_Disc);
6424 end loop;
6426 if Present (New_Disc) then
6427 Set_Corresponding_Discriminant (New_Disc, Old_Disc);
6428 end if;
6429 end if;
6431 Next_Discriminant (Old_Disc);
6433 -- if this is a named constraint, search by name for the old
6434 -- discriminants constrained by the new one.
6436 elsif Nkind (Expression (D_Constraint)) = N_Identifier then
6438 -- Find new discriminant with that name
6440 New_Disc := First_Discriminant (Derived_Type);
6441 while Present (New_Disc) loop
6442 exit when
6443 Chars (New_Disc) = Chars (Expression (D_Constraint));
6444 Next_Discriminant (New_Disc);
6445 end loop;
6447 if Present (New_Disc) then
6449 -- Verify that new discriminant renames some discriminant
6450 -- of the parent type, and associate the new discriminant
6451 -- with one or more old ones that it renames.
6453 declare
6454 Selector : Node_Id;
6456 begin
6457 Selector := First (Selector_Names (D_Constraint));
6458 while Present (Selector) loop
6459 Old_Disc := First_Discriminant (Parent_Type);
6460 while Present (Old_Disc) loop
6461 exit when Chars (Old_Disc) = Chars (Selector);
6462 Next_Discriminant (Old_Disc);
6463 end loop;
6465 if Present (Old_Disc) then
6466 Set_Corresponding_Discriminant
6467 (New_Disc, Old_Disc);
6468 end if;
6470 Next (Selector);
6471 end loop;
6472 end;
6473 end if;
6474 end if;
6476 Next (D_Constraint);
6477 end loop;
6479 New_Disc := First_Discriminant (Derived_Type);
6480 while Present (New_Disc) loop
6481 if No (Corresponding_Discriminant (New_Disc)) then
6482 Error_Msg_NE
6483 ("new discriminant& must constrain old one", N, New_Disc);
6485 elsif not
6486 Subtypes_Statically_Compatible
6487 (Etype (New_Disc),
6488 Etype (Corresponding_Discriminant (New_Disc)))
6489 then
6490 Error_Msg_NE
6491 ("& not statically compatible with parent discriminant",
6492 N, New_Disc);
6493 end if;
6495 Next_Discriminant (New_Disc);
6496 end loop;
6497 end if;
6499 elsif Present (Discriminant_Specifications (N)) then
6500 Error_Msg_N
6501 ("missing discriminant constraint in untagged derivation", N);
6502 end if;
6504 -- The entity chain of the derived type includes the new discriminants
6505 -- but shares operations with the parent.
6507 if Present (Discriminant_Specifications (N)) then
6508 Old_Disc := First_Discriminant (Parent_Type);
6509 while Present (Old_Disc) loop
6510 if No (Next_Entity (Old_Disc))
6511 or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
6512 then
6513 Set_Next_Entity
6514 (Last_Entity (Derived_Type), Next_Entity (Old_Disc));
6515 exit;
6516 end if;
6518 Next_Discriminant (Old_Disc);
6519 end loop;
6521 else
6522 Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
6523 if Has_Discriminants (Parent_Type) then
6524 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
6525 Set_Discriminant_Constraint (
6526 Derived_Type, Discriminant_Constraint (Parent_Type));
6527 end if;
6528 end if;
6530 Set_Last_Entity (Derived_Type, Last_Entity (Parent_Type));
6532 Set_Has_Completion (Derived_Type);
6534 if Corr_Decl_Needed then
6535 Set_Stored_Constraint (Derived_Type, New_Constraint);
6536 Insert_After (N, Corr_Decl);
6537 Analyze (Corr_Decl);
6538 Set_Corresponding_Record_Type (Derived_Type, Corr_Record);
6539 end if;
6540 end Build_Derived_Concurrent_Type;
6542 ------------------------------------
6543 -- Build_Derived_Enumeration_Type --
6544 ------------------------------------
6546 procedure Build_Derived_Enumeration_Type
6547 (N : Node_Id;
6548 Parent_Type : Entity_Id;
6549 Derived_Type : Entity_Id)
6551 Loc : constant Source_Ptr := Sloc (N);
6552 Def : constant Node_Id := Type_Definition (N);
6553 Indic : constant Node_Id := Subtype_Indication (Def);
6554 Implicit_Base : Entity_Id;
6555 Literal : Entity_Id;
6556 New_Lit : Entity_Id;
6557 Literals_List : List_Id;
6558 Type_Decl : Node_Id;
6559 Hi, Lo : Node_Id;
6560 Rang_Expr : Node_Id;
6562 begin
6563 -- Since types Standard.Character and Standard.[Wide_]Wide_Character do
6564 -- not have explicit literals lists we need to process types derived
6565 -- from them specially. This is handled by Derived_Standard_Character.
6566 -- If the parent type is a generic type, there are no literals either,
6567 -- and we construct the same skeletal representation as for the generic
6568 -- parent type.
6570 if Is_Standard_Character_Type (Parent_Type) then
6571 Derived_Standard_Character (N, Parent_Type, Derived_Type);
6573 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
6574 declare
6575 Lo : Node_Id;
6576 Hi : Node_Id;
6578 begin
6579 if Nkind (Indic) /= N_Subtype_Indication then
6580 Lo :=
6581 Make_Attribute_Reference (Loc,
6582 Attribute_Name => Name_First,
6583 Prefix => New_Occurrence_Of (Derived_Type, Loc));
6584 Set_Etype (Lo, Derived_Type);
6586 Hi :=
6587 Make_Attribute_Reference (Loc,
6588 Attribute_Name => Name_Last,
6589 Prefix => New_Occurrence_Of (Derived_Type, Loc));
6590 Set_Etype (Hi, Derived_Type);
6592 Set_Scalar_Range (Derived_Type,
6593 Make_Range (Loc,
6594 Low_Bound => Lo,
6595 High_Bound => Hi));
6596 else
6598 -- Analyze subtype indication and verify compatibility
6599 -- with parent type.
6601 if Base_Type (Process_Subtype (Indic, N)) /=
6602 Base_Type (Parent_Type)
6603 then
6604 Error_Msg_N
6605 ("illegal constraint for formal discrete type", N);
6606 end if;
6607 end if;
6608 end;
6610 else
6611 -- If a constraint is present, analyze the bounds to catch
6612 -- premature usage of the derived literals.
6614 if Nkind (Indic) = N_Subtype_Indication
6615 and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
6616 then
6617 Analyze (Low_Bound (Range_Expression (Constraint (Indic))));
6618 Analyze (High_Bound (Range_Expression (Constraint (Indic))));
6619 end if;
6621 -- Introduce an implicit base type for the derived type even if there
6622 -- is no constraint attached to it, since this seems closer to the
6623 -- Ada semantics. Build a full type declaration tree for the derived
6624 -- type using the implicit base type as the defining identifier. The
6625 -- build a subtype declaration tree which applies the constraint (if
6626 -- any) have it replace the derived type declaration.
6628 Literal := First_Literal (Parent_Type);
6629 Literals_List := New_List;
6630 while Present (Literal)
6631 and then Ekind (Literal) = E_Enumeration_Literal
6632 loop
6633 -- Literals of the derived type have the same representation as
6634 -- those of the parent type, but this representation can be
6635 -- overridden by an explicit representation clause. Indicate
6636 -- that there is no explicit representation given yet. These
6637 -- derived literals are implicit operations of the new type,
6638 -- and can be overridden by explicit ones.
6640 if Nkind (Literal) = N_Defining_Character_Literal then
6641 New_Lit :=
6642 Make_Defining_Character_Literal (Loc, Chars (Literal));
6643 else
6644 New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
6645 end if;
6647 Set_Ekind (New_Lit, E_Enumeration_Literal);
6648 Set_Enumeration_Pos (New_Lit, Enumeration_Pos (Literal));
6649 Set_Enumeration_Rep (New_Lit, Enumeration_Rep (Literal));
6650 Set_Enumeration_Rep_Expr (New_Lit, Empty);
6651 Set_Alias (New_Lit, Literal);
6652 Set_Is_Known_Valid (New_Lit, True);
6654 Append (New_Lit, Literals_List);
6655 Next_Literal (Literal);
6656 end loop;
6658 Implicit_Base :=
6659 Make_Defining_Identifier (Sloc (Derived_Type),
6660 Chars => New_External_Name (Chars (Derived_Type), 'B'));
6662 -- Indicate the proper nature of the derived type. This must be done
6663 -- before analysis of the literals, to recognize cases when a literal
6664 -- may be hidden by a previous explicit function definition (cf.
6665 -- c83031a).
6667 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
6668 Set_Etype (Derived_Type, Implicit_Base);
6670 Type_Decl :=
6671 Make_Full_Type_Declaration (Loc,
6672 Defining_Identifier => Implicit_Base,
6673 Discriminant_Specifications => No_List,
6674 Type_Definition =>
6675 Make_Enumeration_Type_Definition (Loc, Literals_List));
6677 Mark_Rewrite_Insertion (Type_Decl);
6678 Insert_Before (N, Type_Decl);
6679 Analyze (Type_Decl);
6681 -- The anonymous base now has a full declaration, but this base
6682 -- is not a first subtype.
6684 Set_Is_First_Subtype (Implicit_Base, False);
6686 -- After the implicit base is analyzed its Etype needs to be changed
6687 -- to reflect the fact that it is derived from the parent type which
6688 -- was ignored during analysis. We also set the size at this point.
6690 Set_Etype (Implicit_Base, Parent_Type);
6692 Set_Size_Info (Implicit_Base, Parent_Type);
6693 Set_RM_Size (Implicit_Base, RM_Size (Parent_Type));
6694 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
6696 -- Copy other flags from parent type
6698 Set_Has_Non_Standard_Rep
6699 (Implicit_Base, Has_Non_Standard_Rep
6700 (Parent_Type));
6701 Set_Has_Pragma_Ordered
6702 (Implicit_Base, Has_Pragma_Ordered
6703 (Parent_Type));
6704 Set_Has_Delayed_Freeze (Implicit_Base);
6706 -- Process the subtype indication including a validation check on the
6707 -- constraint, if any. If a constraint is given, its bounds must be
6708 -- implicitly converted to the new type.
6710 if Nkind (Indic) = N_Subtype_Indication then
6711 declare
6712 R : constant Node_Id :=
6713 Range_Expression (Constraint (Indic));
6715 begin
6716 if Nkind (R) = N_Range then
6717 Hi := Build_Scalar_Bound
6718 (High_Bound (R), Parent_Type, Implicit_Base);
6719 Lo := Build_Scalar_Bound
6720 (Low_Bound (R), Parent_Type, Implicit_Base);
6722 else
6723 -- Constraint is a Range attribute. Replace with explicit
6724 -- mention of the bounds of the prefix, which must be a
6725 -- subtype.
6727 Analyze (Prefix (R));
6728 Hi :=
6729 Convert_To (Implicit_Base,
6730 Make_Attribute_Reference (Loc,
6731 Attribute_Name => Name_Last,
6732 Prefix =>
6733 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
6735 Lo :=
6736 Convert_To (Implicit_Base,
6737 Make_Attribute_Reference (Loc,
6738 Attribute_Name => Name_First,
6739 Prefix =>
6740 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
6741 end if;
6742 end;
6744 else
6745 Hi :=
6746 Build_Scalar_Bound
6747 (Type_High_Bound (Parent_Type),
6748 Parent_Type, Implicit_Base);
6749 Lo :=
6750 Build_Scalar_Bound
6751 (Type_Low_Bound (Parent_Type),
6752 Parent_Type, Implicit_Base);
6753 end if;
6755 Rang_Expr :=
6756 Make_Range (Loc,
6757 Low_Bound => Lo,
6758 High_Bound => Hi);
6760 -- If we constructed a default range for the case where no range
6761 -- was given, then the expressions in the range must not freeze
6762 -- since they do not correspond to expressions in the source.
6764 if Nkind (Indic) /= N_Subtype_Indication then
6765 Set_Must_Not_Freeze (Lo);
6766 Set_Must_Not_Freeze (Hi);
6767 Set_Must_Not_Freeze (Rang_Expr);
6768 end if;
6770 Rewrite (N,
6771 Make_Subtype_Declaration (Loc,
6772 Defining_Identifier => Derived_Type,
6773 Subtype_Indication =>
6774 Make_Subtype_Indication (Loc,
6775 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
6776 Constraint =>
6777 Make_Range_Constraint (Loc,
6778 Range_Expression => Rang_Expr))));
6780 Analyze (N);
6782 -- Propagate the aspects from the original type declaration to the
6783 -- declaration of the implicit base.
6785 Move_Aspects (From => Original_Node (N), To => Type_Decl);
6787 -- Apply a range check. Since this range expression doesn't have an
6788 -- Etype, we have to specifically pass the Source_Typ parameter. Is
6789 -- this right???
6791 if Nkind (Indic) = N_Subtype_Indication then
6792 Apply_Range_Check
6793 (Range_Expression (Constraint (Indic)), Parent_Type,
6794 Source_Typ => Entity (Subtype_Mark (Indic)));
6795 end if;
6796 end if;
6797 end Build_Derived_Enumeration_Type;
6799 --------------------------------
6800 -- Build_Derived_Numeric_Type --
6801 --------------------------------
6803 procedure Build_Derived_Numeric_Type
6804 (N : Node_Id;
6805 Parent_Type : Entity_Id;
6806 Derived_Type : Entity_Id)
6808 Loc : constant Source_Ptr := Sloc (N);
6809 Tdef : constant Node_Id := Type_Definition (N);
6810 Indic : constant Node_Id := Subtype_Indication (Tdef);
6811 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
6812 No_Constraint : constant Boolean := Nkind (Indic) /=
6813 N_Subtype_Indication;
6814 Implicit_Base : Entity_Id;
6816 Lo : Node_Id;
6817 Hi : Node_Id;
6819 begin
6820 -- Process the subtype indication including a validation check on
6821 -- the constraint if any.
6823 Discard_Node (Process_Subtype (Indic, N));
6825 -- Introduce an implicit base type for the derived type even if there
6826 -- is no constraint attached to it, since this seems closer to the Ada
6827 -- semantics.
6829 Implicit_Base :=
6830 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
6832 Set_Etype (Implicit_Base, Parent_Base);
6833 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
6834 Set_Size_Info (Implicit_Base, Parent_Base);
6835 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
6836 Set_Parent (Implicit_Base, Parent (Derived_Type));
6837 Set_Is_Known_Valid (Implicit_Base, Is_Known_Valid (Parent_Base));
6839 -- Set RM Size for discrete type or decimal fixed-point type
6840 -- Ordinary fixed-point is excluded, why???
6842 if Is_Discrete_Type (Parent_Base)
6843 or else Is_Decimal_Fixed_Point_Type (Parent_Base)
6844 then
6845 Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
6846 end if;
6848 Set_Has_Delayed_Freeze (Implicit_Base);
6850 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
6851 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
6853 Set_Scalar_Range (Implicit_Base,
6854 Make_Range (Loc,
6855 Low_Bound => Lo,
6856 High_Bound => Hi));
6858 if Has_Infinities (Parent_Base) then
6859 Set_Includes_Infinities (Scalar_Range (Implicit_Base));
6860 end if;
6862 -- The Derived_Type, which is the entity of the declaration, is a
6863 -- subtype of the implicit base. Its Ekind is a subtype, even in the
6864 -- absence of an explicit constraint.
6866 Set_Etype (Derived_Type, Implicit_Base);
6868 -- If we did not have a constraint, then the Ekind is set from the
6869 -- parent type (otherwise Process_Subtype has set the bounds)
6871 if No_Constraint then
6872 Set_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
6873 end if;
6875 -- If we did not have a range constraint, then set the range from the
6876 -- parent type. Otherwise, the Process_Subtype call has set the bounds.
6878 if No_Constraint or else not Has_Range_Constraint (Indic) then
6879 Set_Scalar_Range (Derived_Type,
6880 Make_Range (Loc,
6881 Low_Bound => New_Copy_Tree (Type_Low_Bound (Parent_Type)),
6882 High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
6883 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
6885 if Has_Infinities (Parent_Type) then
6886 Set_Includes_Infinities (Scalar_Range (Derived_Type));
6887 end if;
6889 Set_Is_Known_Valid (Derived_Type, Is_Known_Valid (Parent_Type));
6890 end if;
6892 Set_Is_Descendent_Of_Address (Derived_Type,
6893 Is_Descendent_Of_Address (Parent_Type));
6894 Set_Is_Descendent_Of_Address (Implicit_Base,
6895 Is_Descendent_Of_Address (Parent_Type));
6897 -- Set remaining type-specific fields, depending on numeric type
6899 if Is_Modular_Integer_Type (Parent_Type) then
6900 Set_Modulus (Implicit_Base, Modulus (Parent_Base));
6902 Set_Non_Binary_Modulus
6903 (Implicit_Base, Non_Binary_Modulus (Parent_Base));
6905 Set_Is_Known_Valid
6906 (Implicit_Base, Is_Known_Valid (Parent_Base));
6908 elsif Is_Floating_Point_Type (Parent_Type) then
6910 -- Digits of base type is always copied from the digits value of
6911 -- the parent base type, but the digits of the derived type will
6912 -- already have been set if there was a constraint present.
6914 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
6915 Set_Float_Rep (Implicit_Base, Float_Rep (Parent_Base));
6917 if No_Constraint then
6918 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
6919 end if;
6921 elsif Is_Fixed_Point_Type (Parent_Type) then
6923 -- Small of base type and derived type are always copied from the
6924 -- parent base type, since smalls never change. The delta of the
6925 -- base type is also copied from the parent base type. However the
6926 -- delta of the derived type will have been set already if a
6927 -- constraint was present.
6929 Set_Small_Value (Derived_Type, Small_Value (Parent_Base));
6930 Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
6931 Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
6933 if No_Constraint then
6934 Set_Delta_Value (Derived_Type, Delta_Value (Parent_Type));
6935 end if;
6937 -- The scale and machine radix in the decimal case are always
6938 -- copied from the parent base type.
6940 if Is_Decimal_Fixed_Point_Type (Parent_Type) then
6941 Set_Scale_Value (Derived_Type, Scale_Value (Parent_Base));
6942 Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
6944 Set_Machine_Radix_10
6945 (Derived_Type, Machine_Radix_10 (Parent_Base));
6946 Set_Machine_Radix_10
6947 (Implicit_Base, Machine_Radix_10 (Parent_Base));
6949 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
6951 if No_Constraint then
6952 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
6954 else
6955 -- the analysis of the subtype_indication sets the
6956 -- digits value of the derived type.
6958 null;
6959 end if;
6960 end if;
6961 end if;
6963 if Is_Integer_Type (Parent_Type) then
6964 Set_Has_Shift_Operator
6965 (Implicit_Base, Has_Shift_Operator (Parent_Type));
6966 end if;
6968 -- The type of the bounds is that of the parent type, and they
6969 -- must be converted to the derived type.
6971 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
6973 -- The implicit_base should be frozen when the derived type is frozen,
6974 -- but note that it is used in the conversions of the bounds. For fixed
6975 -- types we delay the determination of the bounds until the proper
6976 -- freezing point. For other numeric types this is rejected by GCC, for
6977 -- reasons that are currently unclear (???), so we choose to freeze the
6978 -- implicit base now. In the case of integers and floating point types
6979 -- this is harmless because subsequent representation clauses cannot
6980 -- affect anything, but it is still baffling that we cannot use the
6981 -- same mechanism for all derived numeric types.
6983 -- There is a further complication: actually some representation
6984 -- clauses can affect the implicit base type. For example, attribute
6985 -- definition clauses for stream-oriented attributes need to set the
6986 -- corresponding TSS entries on the base type, and this normally
6987 -- cannot be done after the base type is frozen, so the circuitry in
6988 -- Sem_Ch13.New_Stream_Subprogram must account for this possibility
6989 -- and not use Set_TSS in this case.
6991 -- There are also consequences for the case of delayed representation
6992 -- aspects for some cases. For example, a Size aspect is delayed and
6993 -- should not be evaluated to the freeze point. This early freezing
6994 -- means that the size attribute evaluation happens too early???
6996 if Is_Fixed_Point_Type (Parent_Type) then
6997 Conditional_Delay (Implicit_Base, Parent_Type);
6998 else
6999 Freeze_Before (N, Implicit_Base);
7000 end if;
7001 end Build_Derived_Numeric_Type;
7003 --------------------------------
7004 -- Build_Derived_Private_Type --
7005 --------------------------------
7007 procedure Build_Derived_Private_Type
7008 (N : Node_Id;
7009 Parent_Type : Entity_Id;
7010 Derived_Type : Entity_Id;
7011 Is_Completion : Boolean;
7012 Derive_Subps : Boolean := True)
7014 Loc : constant Source_Ptr := Sloc (N);
7015 Par_Base : constant Entity_Id := Base_Type (Parent_Type);
7016 Par_Scope : constant Entity_Id := Scope (Par_Base);
7017 Full_N : constant Node_Id := New_Copy_Tree (N);
7018 Full_Der : Entity_Id := New_Copy (Derived_Type);
7019 Full_P : Entity_Id;
7021 procedure Build_Full_Derivation;
7022 -- Build full derivation, i.e. derive from the full view
7024 procedure Copy_And_Build;
7025 -- Copy derived type declaration, replace parent with its full view,
7026 -- and build derivation
7028 ---------------------------
7029 -- Build_Full_Derivation --
7030 ---------------------------
7032 procedure Build_Full_Derivation is
7033 begin
7034 -- If parent scope is not open, install the declarations
7036 if not In_Open_Scopes (Par_Scope) then
7037 Install_Private_Declarations (Par_Scope);
7038 Install_Visible_Declarations (Par_Scope);
7039 Copy_And_Build;
7040 Uninstall_Declarations (Par_Scope);
7042 -- If parent scope is open and in another unit, and parent has a
7043 -- completion, then the derivation is taking place in the visible
7044 -- part of a child unit. In that case retrieve the full view of
7045 -- the parent momentarily.
7047 elsif not In_Same_Source_Unit (N, Parent_Type) then
7048 Full_P := Full_View (Parent_Type);
7049 Exchange_Declarations (Parent_Type);
7050 Copy_And_Build;
7051 Exchange_Declarations (Full_P);
7053 -- Otherwise it is a local derivation
7055 else
7056 Copy_And_Build;
7057 end if;
7058 end Build_Full_Derivation;
7060 --------------------
7061 -- Copy_And_Build --
7062 --------------------
7064 procedure Copy_And_Build is
7065 Full_Parent : Entity_Id := Parent_Type;
7067 begin
7068 -- If the parent is itself derived from another private type,
7069 -- installing the private declarations has not affected its
7070 -- privacy status, so use its own full view explicitly.
7072 if Is_Private_Type (Full_Parent)
7073 and then Present (Full_View (Full_Parent))
7074 then
7075 Full_Parent := Full_View (Full_Parent);
7076 end if;
7078 -- And its underlying full view if necessary
7080 if Is_Private_Type (Full_Parent)
7081 and then Present (Underlying_Full_View (Full_Parent))
7082 then
7083 Full_Parent := Underlying_Full_View (Full_Parent);
7084 end if;
7086 -- For record, access and most enumeration types, derivation from
7087 -- the full view requires a fully-fledged declaration. In the other
7088 -- cases, just use an itype.
7090 if Ekind (Full_Parent) in Record_Kind
7091 or else Ekind (Full_Parent) in Access_Kind
7092 or else
7093 (Ekind (Full_Parent) in Enumeration_Kind
7094 and then not Is_Standard_Character_Type (Full_Parent)
7095 and then not Is_Generic_Type (Root_Type (Full_Parent)))
7096 then
7097 -- Copy and adjust declaration to provide a completion for what
7098 -- is originally a private declaration. Indicate that full view
7099 -- is internally generated.
7101 Set_Comes_From_Source (Full_N, False);
7102 Set_Comes_From_Source (Full_Der, False);
7103 Set_Parent (Full_Der, Full_N);
7104 Set_Defining_Identifier (Full_N, Full_Der);
7106 -- If there are no constraints, adjust the subtype mark
7108 if Nkind (Subtype_Indication (Type_Definition (Full_N))) /=
7109 N_Subtype_Indication
7110 then
7111 Set_Subtype_Indication
7112 (Type_Definition (Full_N),
7113 New_Occurrence_Of (Full_Parent, Sloc (Full_N)));
7114 end if;
7116 Insert_After (N, Full_N);
7118 -- Build full view of derived type from full view of parent which
7119 -- is now installed. Subprograms have been derived on the partial
7120 -- view, the completion does not derive them anew.
7122 if Ekind (Full_Parent) in Record_Kind then
7124 -- If parent type is tagged, the completion inherits the proper
7125 -- primitive operations.
7127 if Is_Tagged_Type (Parent_Type) then
7128 Build_Derived_Record_Type
7129 (Full_N, Full_Parent, Full_Der, Derive_Subps);
7130 else
7131 Build_Derived_Record_Type
7132 (Full_N, Full_Parent, Full_Der, Derive_Subps => False);
7133 end if;
7135 else
7136 Build_Derived_Type
7137 (Full_N, Full_Parent, Full_Der,
7138 Is_Completion => False, Derive_Subps => False);
7139 end if;
7141 -- The full declaration has been introduced into the tree and
7142 -- processed in the step above. It should not be analyzed again
7143 -- (when encountered later in the current list of declarations)
7144 -- to prevent spurious name conflicts. The full entity remains
7145 -- invisible.
7147 Set_Analyzed (Full_N);
7149 else
7150 Full_Der :=
7151 Make_Defining_Identifier (Sloc (Derived_Type),
7152 Chars => Chars (Derived_Type));
7153 Set_Is_Itype (Full_Der);
7154 Set_Associated_Node_For_Itype (Full_Der, N);
7155 Set_Parent (Full_Der, N);
7156 Build_Derived_Type
7157 (N, Full_Parent, Full_Der,
7158 Is_Completion => False, Derive_Subps => False);
7159 end if;
7161 Set_Has_Private_Declaration (Full_Der);
7162 Set_Has_Private_Declaration (Derived_Type);
7164 Set_Scope (Full_Der, Scope (Derived_Type));
7165 Set_Is_First_Subtype (Full_Der, Is_First_Subtype (Derived_Type));
7166 Set_Has_Size_Clause (Full_Der, False);
7167 Set_Has_Alignment_Clause (Full_Der, False);
7168 Set_Has_Delayed_Freeze (Full_Der);
7169 Set_Is_Frozen (Full_Der, False);
7170 Set_Freeze_Node (Full_Der, Empty);
7171 Set_Depends_On_Private (Full_Der, Has_Private_Component (Full_Der));
7172 Set_Is_Public (Full_Der, Is_Public (Derived_Type));
7174 -- The convention on the base type may be set in the private part
7175 -- and not propagated to the subtype until later, so we obtain the
7176 -- convention from the base type of the parent.
7178 Set_Convention (Full_Der, Convention (Base_Type (Full_Parent)));
7179 end Copy_And_Build;
7181 -- Start of processing for Build_Derived_Private_Type
7183 begin
7184 if Is_Tagged_Type (Parent_Type) then
7185 Full_P := Full_View (Parent_Type);
7187 -- A type extension of a type with unknown discriminants is an
7188 -- indefinite type that the back-end cannot handle directly.
7189 -- We treat it as a private type, and build a completion that is
7190 -- derived from the full view of the parent, and hopefully has
7191 -- known discriminants.
7193 -- If the full view of the parent type has an underlying record view,
7194 -- use it to generate the underlying record view of this derived type
7195 -- (required for chains of derivations with unknown discriminants).
7197 -- Minor optimization: we avoid the generation of useless underlying
7198 -- record view entities if the private type declaration has unknown
7199 -- discriminants but its corresponding full view has no
7200 -- discriminants.
7202 if Has_Unknown_Discriminants (Parent_Type)
7203 and then Present (Full_P)
7204 and then (Has_Discriminants (Full_P)
7205 or else Present (Underlying_Record_View (Full_P)))
7206 and then not In_Open_Scopes (Par_Scope)
7207 and then Expander_Active
7208 then
7209 declare
7210 Full_Der : constant Entity_Id := Make_Temporary (Loc, 'T');
7211 New_Ext : constant Node_Id :=
7212 Copy_Separate_Tree
7213 (Record_Extension_Part (Type_Definition (N)));
7214 Decl : Node_Id;
7216 begin
7217 Build_Derived_Record_Type
7218 (N, Parent_Type, Derived_Type, Derive_Subps);
7220 -- Build anonymous completion, as a derivation from the full
7221 -- view of the parent. This is not a completion in the usual
7222 -- sense, because the current type is not private.
7224 Decl :=
7225 Make_Full_Type_Declaration (Loc,
7226 Defining_Identifier => Full_Der,
7227 Type_Definition =>
7228 Make_Derived_Type_Definition (Loc,
7229 Subtype_Indication =>
7230 New_Copy_Tree
7231 (Subtype_Indication (Type_Definition (N))),
7232 Record_Extension_Part => New_Ext));
7234 -- If the parent type has an underlying record view, use it
7235 -- here to build the new underlying record view.
7237 if Present (Underlying_Record_View (Full_P)) then
7238 pragma Assert
7239 (Nkind (Subtype_Indication (Type_Definition (Decl)))
7240 = N_Identifier);
7241 Set_Entity (Subtype_Indication (Type_Definition (Decl)),
7242 Underlying_Record_View (Full_P));
7243 end if;
7245 Install_Private_Declarations (Par_Scope);
7246 Install_Visible_Declarations (Par_Scope);
7247 Insert_Before (N, Decl);
7249 -- Mark entity as an underlying record view before analysis,
7250 -- to avoid generating the list of its primitive operations
7251 -- (which is not really required for this entity) and thus
7252 -- prevent spurious errors associated with missing overriding
7253 -- of abstract primitives (overridden only for Derived_Type).
7255 Set_Ekind (Full_Der, E_Record_Type);
7256 Set_Is_Underlying_Record_View (Full_Der);
7257 Set_Default_SSO (Full_Der);
7259 Analyze (Decl);
7261 pragma Assert (Has_Discriminants (Full_Der)
7262 and then not Has_Unknown_Discriminants (Full_Der));
7264 Uninstall_Declarations (Par_Scope);
7266 -- Freeze the underlying record view, to prevent generation of
7267 -- useless dispatching information, which is simply shared with
7268 -- the real derived type.
7270 Set_Is_Frozen (Full_Der);
7272 -- If the derived type has access discriminants, create
7273 -- references to their anonymous types now, to prevent
7274 -- back-end problems when their first use is in generated
7275 -- bodies of primitives.
7277 declare
7278 E : Entity_Id;
7280 begin
7281 E := First_Entity (Full_Der);
7283 while Present (E) loop
7284 if Ekind (E) = E_Discriminant
7285 and then Ekind (Etype (E)) = E_Anonymous_Access_Type
7286 then
7287 Build_Itype_Reference (Etype (E), Decl);
7288 end if;
7290 Next_Entity (E);
7291 end loop;
7292 end;
7294 -- Set up links between real entity and underlying record view
7296 Set_Underlying_Record_View (Derived_Type, Base_Type (Full_Der));
7297 Set_Underlying_Record_View (Base_Type (Full_Der), Derived_Type);
7298 end;
7300 -- If discriminants are known, build derived record
7302 else
7303 Build_Derived_Record_Type
7304 (N, Parent_Type, Derived_Type, Derive_Subps);
7305 end if;
7307 return;
7309 elsif Has_Discriminants (Parent_Type) then
7311 -- Build partial view of derived type from partial view of parent.
7312 -- This must be done before building the full derivation because the
7313 -- second derivation will modify the discriminants of the first and
7314 -- the discriminants are chained with the rest of the components in
7315 -- the full derivation.
7317 Build_Derived_Record_Type
7318 (N, Parent_Type, Derived_Type, Derive_Subps);
7320 -- Build the full derivation if this is not the anonymous derived
7321 -- base type created by Build_Derived_Record_Type in the constrained
7322 -- case (see point 5. of its head comment) since we build it for the
7323 -- derived subtype. And skip it for protected types altogether, as
7324 -- gigi does not use these types directly.
7326 if Present (Full_View (Parent_Type))
7327 and then not Is_Itype (Derived_Type)
7328 and then not (Ekind (Full_View (Parent_Type)) in Protected_Kind)
7329 then
7330 declare
7331 Der_Base : constant Entity_Id := Base_Type (Derived_Type);
7332 Discr : Entity_Id;
7333 Last_Discr : Entity_Id;
7335 begin
7336 -- If this is not a completion, construct the implicit full
7337 -- view by deriving from the full view of the parent type.
7338 -- But if this is a completion, the derived private type
7339 -- being built is a full view and the full derivation can
7340 -- only be its underlying full view.
7342 Build_Full_Derivation;
7344 if not Is_Completion then
7345 Set_Full_View (Derived_Type, Full_Der);
7346 else
7347 Set_Underlying_Full_View (Derived_Type, Full_Der);
7348 end if;
7350 if not Is_Base_Type (Derived_Type) then
7351 Set_Full_View (Der_Base, Base_Type (Full_Der));
7352 end if;
7354 -- Copy the discriminant list from full view to the partial
7355 -- view (base type and its subtype). Gigi requires that the
7356 -- partial and full views have the same discriminants.
7358 -- Note that since the partial view points to discriminants
7359 -- in the full view, their scope will be that of the full
7360 -- view. This might cause some front end problems and need
7361 -- adjustment???
7363 Discr := First_Discriminant (Base_Type (Full_Der));
7364 Set_First_Entity (Der_Base, Discr);
7366 loop
7367 Last_Discr := Discr;
7368 Next_Discriminant (Discr);
7369 exit when No (Discr);
7370 end loop;
7372 Set_Last_Entity (Der_Base, Last_Discr);
7373 Set_First_Entity (Derived_Type, First_Entity (Der_Base));
7374 Set_Last_Entity (Derived_Type, Last_Entity (Der_Base));
7376 Set_Stored_Constraint
7377 (Full_Der, Stored_Constraint (Derived_Type));
7378 end;
7379 end if;
7381 elsif Present (Full_View (Parent_Type))
7382 and then Has_Discriminants (Full_View (Parent_Type))
7383 then
7384 if Has_Unknown_Discriminants (Parent_Type)
7385 and then Nkind (Subtype_Indication (Type_Definition (N))) =
7386 N_Subtype_Indication
7387 then
7388 Error_Msg_N
7389 ("cannot constrain type with unknown discriminants",
7390 Subtype_Indication (Type_Definition (N)));
7391 return;
7392 end if;
7394 -- If this is not a completion, construct the implicit full view by
7395 -- deriving from the full view of the parent type. But if this is a
7396 -- completion, the derived private type being built is a full view
7397 -- and the full derivation can only be its underlying full view.
7399 Build_Full_Derivation;
7401 if not Is_Completion then
7402 Set_Full_View (Derived_Type, Full_Der);
7403 else
7404 Set_Underlying_Full_View (Derived_Type, Full_Der);
7405 end if;
7407 -- In any case, the primitive operations are inherited from the
7408 -- parent type, not from the internal full view.
7410 Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
7412 if Derive_Subps then
7413 Derive_Subprograms (Parent_Type, Derived_Type);
7414 end if;
7416 Set_Stored_Constraint (Derived_Type, No_Elist);
7417 Set_Is_Constrained
7418 (Derived_Type, Is_Constrained (Full_View (Parent_Type)));
7420 else
7421 -- Untagged type, No discriminants on either view
7423 if Nkind (Subtype_Indication (Type_Definition (N))) =
7424 N_Subtype_Indication
7425 then
7426 Error_Msg_N
7427 ("illegal constraint on type without discriminants", N);
7428 end if;
7430 if Present (Discriminant_Specifications (N))
7431 and then Present (Full_View (Parent_Type))
7432 and then not Is_Tagged_Type (Full_View (Parent_Type))
7433 then
7434 Error_Msg_N ("cannot add discriminants to untagged type", N);
7435 end if;
7437 Set_Stored_Constraint (Derived_Type, No_Elist);
7438 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7439 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
7440 Set_Disable_Controlled (Derived_Type, Disable_Controlled
7441 (Parent_Type));
7442 Set_Has_Controlled_Component
7443 (Derived_Type, Has_Controlled_Component
7444 (Parent_Type));
7446 -- Direct controlled types do not inherit Finalize_Storage_Only flag
7448 if not Is_Controlled_Active (Parent_Type) then
7449 Set_Finalize_Storage_Only
7450 (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
7451 end if;
7453 -- If this is not a completion, construct the implicit full view by
7454 -- deriving from the full view of the parent type.
7456 -- ??? If the parent is untagged private and its completion is
7457 -- tagged, this mechanism will not work because we cannot derive from
7458 -- the tagged full view unless we have an extension.
7460 if Present (Full_View (Parent_Type))
7461 and then not Is_Tagged_Type (Full_View (Parent_Type))
7462 and then not Is_Completion
7463 then
7464 Build_Full_Derivation;
7465 Set_Full_View (Derived_Type, Full_Der);
7466 end if;
7467 end if;
7469 Set_Has_Unknown_Discriminants (Derived_Type,
7470 Has_Unknown_Discriminants (Parent_Type));
7472 if Is_Private_Type (Derived_Type) then
7473 Set_Private_Dependents (Derived_Type, New_Elmt_List);
7474 end if;
7476 -- If the parent base type is in scope, add the derived type to its
7477 -- list of private dependents, because its full view may become
7478 -- visible subsequently (in a nested private part, a body, or in a
7479 -- further child unit).
7481 if Is_Private_Type (Par_Base) and then In_Open_Scopes (Par_Scope) then
7482 Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
7484 -- Check for unusual case where a type completed by a private
7485 -- derivation occurs within a package nested in a child unit, and
7486 -- the parent is declared in an ancestor.
7488 if Is_Child_Unit (Scope (Current_Scope))
7489 and then Is_Completion
7490 and then In_Private_Part (Current_Scope)
7491 and then Scope (Parent_Type) /= Current_Scope
7493 -- Note that if the parent has a completion in the private part,
7494 -- (which is itself a derivation from some other private type)
7495 -- it is that completion that is visible, there is no full view
7496 -- available, and no special processing is needed.
7498 and then Present (Full_View (Parent_Type))
7499 then
7500 -- In this case, the full view of the parent type will become
7501 -- visible in the body of the enclosing child, and only then will
7502 -- the current type be possibly non-private. Build an underlying
7503 -- full view that will be installed when the enclosing child body
7504 -- is compiled.
7506 if Present (Underlying_Full_View (Derived_Type)) then
7507 Full_Der := Underlying_Full_View (Derived_Type);
7508 else
7509 Build_Full_Derivation;
7510 Set_Underlying_Full_View (Derived_Type, Full_Der);
7511 end if;
7513 -- The full view will be used to swap entities on entry/exit to
7514 -- the body, and must appear in the entity list for the package.
7516 Append_Entity (Full_Der, Scope (Derived_Type));
7517 end if;
7518 end if;
7519 end Build_Derived_Private_Type;
7521 -------------------------------
7522 -- Build_Derived_Record_Type --
7523 -------------------------------
7525 -- 1. INTRODUCTION
7527 -- Ideally we would like to use the same model of type derivation for
7528 -- tagged and untagged record types. Unfortunately this is not quite
7529 -- possible because the semantics of representation clauses is different
7530 -- for tagged and untagged records under inheritance. Consider the
7531 -- following:
7533 -- type R (...) is [tagged] record ... end record;
7534 -- type T (...) is new R (...) [with ...];
7536 -- The representation clauses for T can specify a completely different
7537 -- record layout from R's. Hence the same component can be placed in two
7538 -- very different positions in objects of type T and R. If R and T are
7539 -- tagged types, representation clauses for T can only specify the layout
7540 -- of non inherited components, thus components that are common in R and T
7541 -- have the same position in objects of type R and T.
7543 -- This has two implications. The first is that the entire tree for R's
7544 -- declaration needs to be copied for T in the untagged case, so that T
7545 -- can be viewed as a record type of its own with its own representation
7546 -- clauses. The second implication is the way we handle discriminants.
7547 -- Specifically, in the untagged case we need a way to communicate to Gigi
7548 -- what are the real discriminants in the record, while for the semantics
7549 -- we need to consider those introduced by the user to rename the
7550 -- discriminants in the parent type. This is handled by introducing the
7551 -- notion of stored discriminants. See below for more.
7553 -- Fortunately the way regular components are inherited can be handled in
7554 -- the same way in tagged and untagged types.
7556 -- To complicate things a bit more the private view of a private extension
7557 -- cannot be handled in the same way as the full view (for one thing the
7558 -- semantic rules are somewhat different). We will explain what differs
7559 -- below.
7561 -- 2. DISCRIMINANTS UNDER INHERITANCE
7563 -- The semantic rules governing the discriminants of derived types are
7564 -- quite subtle.
7566 -- type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
7567 -- [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
7569 -- If parent type has discriminants, then the discriminants that are
7570 -- declared in the derived type are [3.4 (11)]:
7572 -- o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
7573 -- there is one;
7575 -- o Otherwise, each discriminant of the parent type (implicitly declared
7576 -- in the same order with the same specifications). In this case, the
7577 -- discriminants are said to be "inherited", or if unknown in the parent
7578 -- are also unknown in the derived type.
7580 -- Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
7582 -- o The parent subtype must be constrained;
7584 -- o If the parent type is not a tagged type, then each discriminant of
7585 -- the derived type must be used in the constraint defining a parent
7586 -- subtype. [Implementation note: This ensures that the new discriminant
7587 -- can share storage with an existing discriminant.]
7589 -- For the derived type each discriminant of the parent type is either
7590 -- inherited, constrained to equal some new discriminant of the derived
7591 -- type, or constrained to the value of an expression.
7593 -- When inherited or constrained to equal some new discriminant, the
7594 -- parent discriminant and the discriminant of the derived type are said
7595 -- to "correspond".
7597 -- If a discriminant of the parent type is constrained to a specific value
7598 -- in the derived type definition, then the discriminant is said to be
7599 -- "specified" by that derived type definition.
7601 -- 3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
7603 -- We have spoken about stored discriminants in point 1 (introduction)
7604 -- above. There are two sort of stored discriminants: implicit and
7605 -- explicit. As long as the derived type inherits the same discriminants as
7606 -- the root record type, stored discriminants are the same as regular
7607 -- discriminants, and are said to be implicit. However, if any discriminant
7608 -- in the root type was renamed in the derived type, then the derived
7609 -- type will contain explicit stored discriminants. Explicit stored
7610 -- discriminants are discriminants in addition to the semantically visible
7611 -- discriminants defined for the derived type. Stored discriminants are
7612 -- used by Gigi to figure out what are the physical discriminants in
7613 -- objects of the derived type (see precise definition in einfo.ads).
7614 -- As an example, consider the following:
7616 -- type R (D1, D2, D3 : Int) is record ... end record;
7617 -- type T1 is new R;
7618 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
7619 -- type T3 is new T2;
7620 -- type T4 (Y : Int) is new T3 (Y, 99);
7622 -- The following table summarizes the discriminants and stored
7623 -- discriminants in R and T1 through T4.
7625 -- Type Discrim Stored Discrim Comment
7626 -- R (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in R
7627 -- T1 (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in T1
7628 -- T2 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T2
7629 -- T3 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T3
7630 -- T4 (Y) (D1, D2, D3) Girder discrims EXPLICIT in T4
7632 -- Field Corresponding_Discriminant (abbreviated CD below) allows us to
7633 -- find the corresponding discriminant in the parent type, while
7634 -- Original_Record_Component (abbreviated ORC below), the actual physical
7635 -- component that is renamed. Finally the field Is_Completely_Hidden
7636 -- (abbreviated ICH below) is set for all explicit stored discriminants
7637 -- (see einfo.ads for more info). For the above example this gives:
7639 -- Discrim CD ORC ICH
7640 -- ^^^^^^^ ^^ ^^^ ^^^
7641 -- D1 in R empty itself no
7642 -- D2 in R empty itself no
7643 -- D3 in R empty itself no
7645 -- D1 in T1 D1 in R itself no
7646 -- D2 in T1 D2 in R itself no
7647 -- D3 in T1 D3 in R itself no
7649 -- X1 in T2 D3 in T1 D3 in T2 no
7650 -- X2 in T2 D1 in T1 D1 in T2 no
7651 -- D1 in T2 empty itself yes
7652 -- D2 in T2 empty itself yes
7653 -- D3 in T2 empty itself yes
7655 -- X1 in T3 X1 in T2 D3 in T3 no
7656 -- X2 in T3 X2 in T2 D1 in T3 no
7657 -- D1 in T3 empty itself yes
7658 -- D2 in T3 empty itself yes
7659 -- D3 in T3 empty itself yes
7661 -- Y in T4 X1 in T3 D3 in T3 no
7662 -- D1 in T3 empty itself yes
7663 -- D2 in T3 empty itself yes
7664 -- D3 in T3 empty itself yes
7666 -- 4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
7668 -- Type derivation for tagged types is fairly straightforward. If no
7669 -- discriminants are specified by the derived type, these are inherited
7670 -- from the parent. No explicit stored discriminants are ever necessary.
7671 -- The only manipulation that is done to the tree is that of adding a
7672 -- _parent field with parent type and constrained to the same constraint
7673 -- specified for the parent in the derived type definition. For instance:
7675 -- type R (D1, D2, D3 : Int) is tagged record ... end record;
7676 -- type T1 is new R with null record;
7677 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
7679 -- are changed into:
7681 -- type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
7682 -- _parent : R (D1, D2, D3);
7683 -- end record;
7685 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
7686 -- _parent : T1 (X2, 88, X1);
7687 -- end record;
7689 -- The discriminants actually present in R, T1 and T2 as well as their CD,
7690 -- ORC and ICH fields are:
7692 -- Discrim CD ORC ICH
7693 -- ^^^^^^^ ^^ ^^^ ^^^
7694 -- D1 in R empty itself no
7695 -- D2 in R empty itself no
7696 -- D3 in R empty itself no
7698 -- D1 in T1 D1 in R D1 in R no
7699 -- D2 in T1 D2 in R D2 in R no
7700 -- D3 in T1 D3 in R D3 in R no
7702 -- X1 in T2 D3 in T1 D3 in R no
7703 -- X2 in T2 D1 in T1 D1 in R no
7705 -- 5. FIRST TRANSFORMATION FOR DERIVED RECORDS
7707 -- Regardless of whether we dealing with a tagged or untagged type
7708 -- we will transform all derived type declarations of the form
7710 -- type T is new R (...) [with ...];
7711 -- or
7712 -- subtype S is R (...);
7713 -- type T is new S [with ...];
7714 -- into
7715 -- type BT is new R [with ...];
7716 -- subtype T is BT (...);
7718 -- That is, the base derived type is constrained only if it has no
7719 -- discriminants. The reason for doing this is that GNAT's semantic model
7720 -- assumes that a base type with discriminants is unconstrained.
7722 -- Note that, strictly speaking, the above transformation is not always
7723 -- correct. Consider for instance the following excerpt from ACVC b34011a:
7725 -- procedure B34011A is
7726 -- type REC (D : integer := 0) is record
7727 -- I : Integer;
7728 -- end record;
7730 -- package P is
7731 -- type T6 is new Rec;
7732 -- function F return T6;
7733 -- end P;
7735 -- use P;
7736 -- package Q6 is
7737 -- type U is new T6 (Q6.F.I); -- ERROR: Q6.F.
7738 -- end Q6;
7740 -- The definition of Q6.U is illegal. However transforming Q6.U into
7742 -- type BaseU is new T6;
7743 -- subtype U is BaseU (Q6.F.I)
7745 -- turns U into a legal subtype, which is incorrect. To avoid this problem
7746 -- we always analyze the constraint (in this case (Q6.F.I)) before applying
7747 -- the transformation described above.
7749 -- There is another instance where the above transformation is incorrect.
7750 -- Consider:
7752 -- package Pack is
7753 -- type Base (D : Integer) is tagged null record;
7754 -- procedure P (X : Base);
7756 -- type Der is new Base (2) with null record;
7757 -- procedure P (X : Der);
7758 -- end Pack;
7760 -- Then the above transformation turns this into
7762 -- type Der_Base is new Base with null record;
7763 -- -- procedure P (X : Base) is implicitly inherited here
7764 -- -- as procedure P (X : Der_Base).
7766 -- subtype Der is Der_Base (2);
7767 -- procedure P (X : Der);
7768 -- -- The overriding of P (X : Der_Base) is illegal since we
7769 -- -- have a parameter conformance problem.
7771 -- To get around this problem, after having semantically processed Der_Base
7772 -- and the rewritten subtype declaration for Der, we copy Der_Base field
7773 -- Discriminant_Constraint from Der so that when parameter conformance is
7774 -- checked when P is overridden, no semantic errors are flagged.
7776 -- 6. SECOND TRANSFORMATION FOR DERIVED RECORDS
7778 -- Regardless of whether we are dealing with a tagged or untagged type
7779 -- we will transform all derived type declarations of the form
7781 -- type R (D1, .., Dn : ...) is [tagged] record ...;
7782 -- type T is new R [with ...];
7783 -- into
7784 -- type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
7786 -- The reason for such transformation is that it allows us to implement a
7787 -- very clean form of component inheritance as explained below.
7789 -- Note that this transformation is not achieved by direct tree rewriting
7790 -- and manipulation, but rather by redoing the semantic actions that the
7791 -- above transformation will entail. This is done directly in routine
7792 -- Inherit_Components.
7794 -- 7. TYPE DERIVATION AND COMPONENT INHERITANCE
7796 -- In both tagged and untagged derived types, regular non discriminant
7797 -- components are inherited in the derived type from the parent type. In
7798 -- the absence of discriminants component, inheritance is straightforward
7799 -- as components can simply be copied from the parent.
7801 -- If the parent has discriminants, inheriting components constrained with
7802 -- these discriminants requires caution. Consider the following example:
7804 -- type R (D1, D2 : Positive) is [tagged] record
7805 -- S : String (D1 .. D2);
7806 -- end record;
7808 -- type T1 is new R [with null record];
7809 -- type T2 (X : positive) is new R (1, X) [with null record];
7811 -- As explained in 6. above, T1 is rewritten as
7812 -- type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
7813 -- which makes the treatment for T1 and T2 identical.
7815 -- What we want when inheriting S, is that references to D1 and D2 in R are
7816 -- replaced with references to their correct constraints, i.e. D1 and D2 in
7817 -- T1 and 1 and X in T2. So all R's discriminant references are replaced
7818 -- with either discriminant references in the derived type or expressions.
7819 -- This replacement is achieved as follows: before inheriting R's
7820 -- components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
7821 -- created in the scope of T1 (resp. scope of T2) so that discriminants D1
7822 -- and D2 of T1 are visible (resp. discriminant X of T2 is visible).
7823 -- For T2, for instance, this has the effect of replacing String (D1 .. D2)
7824 -- by String (1 .. X).
7826 -- 8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
7828 -- We explain here the rules governing private type extensions relevant to
7829 -- type derivation. These rules are explained on the following example:
7831 -- type D [(...)] is new A [(...)] with private; <-- partial view
7832 -- type D [(...)] is new P [(...)] with null record; <-- full view
7834 -- Type A is called the ancestor subtype of the private extension.
7835 -- Type P is the parent type of the full view of the private extension. It
7836 -- must be A or a type derived from A.
7838 -- The rules concerning the discriminants of private type extensions are
7839 -- [7.3(10-13)]:
7841 -- o If a private extension inherits known discriminants from the ancestor
7842 -- subtype, then the full view must also inherit its discriminants from
7843 -- the ancestor subtype and the parent subtype of the full view must be
7844 -- constrained if and only if the ancestor subtype is constrained.
7846 -- o If a partial view has unknown discriminants, then the full view may
7847 -- define a definite or an indefinite subtype, with or without
7848 -- discriminants.
7850 -- o If a partial view has neither known nor unknown discriminants, then
7851 -- the full view must define a definite subtype.
7853 -- o If the ancestor subtype of a private extension has constrained
7854 -- discriminants, then the parent subtype of the full view must impose a
7855 -- statically matching constraint on those discriminants.
7857 -- This means that only the following forms of private extensions are
7858 -- allowed:
7860 -- type D is new A with private; <-- partial view
7861 -- type D is new P with null record; <-- full view
7863 -- If A has no discriminants than P has no discriminants, otherwise P must
7864 -- inherit A's discriminants.
7866 -- type D is new A (...) with private; <-- partial view
7867 -- type D is new P (:::) with null record; <-- full view
7869 -- P must inherit A's discriminants and (...) and (:::) must statically
7870 -- match.
7872 -- subtype A is R (...);
7873 -- type D is new A with private; <-- partial view
7874 -- type D is new P with null record; <-- full view
7876 -- P must have inherited R's discriminants and must be derived from A or
7877 -- any of its subtypes.
7879 -- type D (..) is new A with private; <-- partial view
7880 -- type D (..) is new P [(:::)] with null record; <-- full view
7882 -- No specific constraints on P's discriminants or constraint (:::).
7883 -- Note that A can be unconstrained, but the parent subtype P must either
7884 -- be constrained or (:::) must be present.
7886 -- type D (..) is new A [(...)] with private; <-- partial view
7887 -- type D (..) is new P [(:::)] with null record; <-- full view
7889 -- P's constraints on A's discriminants must statically match those
7890 -- imposed by (...).
7892 -- 9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
7894 -- The full view of a private extension is handled exactly as described
7895 -- above. The model chose for the private view of a private extension is
7896 -- the same for what concerns discriminants (i.e. they receive the same
7897 -- treatment as in the tagged case). However, the private view of the
7898 -- private extension always inherits the components of the parent base,
7899 -- without replacing any discriminant reference. Strictly speaking this is
7900 -- incorrect. However, Gigi never uses this view to generate code so this
7901 -- is a purely semantic issue. In theory, a set of transformations similar
7902 -- to those given in 5. and 6. above could be applied to private views of
7903 -- private extensions to have the same model of component inheritance as
7904 -- for non private extensions. However, this is not done because it would
7905 -- further complicate private type processing. Semantically speaking, this
7906 -- leaves us in an uncomfortable situation. As an example consider:
7908 -- package Pack is
7909 -- type R (D : integer) is tagged record
7910 -- S : String (1 .. D);
7911 -- end record;
7912 -- procedure P (X : R);
7913 -- type T is new R (1) with private;
7914 -- private
7915 -- type T is new R (1) with null record;
7916 -- end;
7918 -- This is transformed into:
7920 -- package Pack is
7921 -- type R (D : integer) is tagged record
7922 -- S : String (1 .. D);
7923 -- end record;
7924 -- procedure P (X : R);
7925 -- type T is new R (1) with private;
7926 -- private
7927 -- type BaseT is new R with null record;
7928 -- subtype T is BaseT (1);
7929 -- end;
7931 -- (strictly speaking the above is incorrect Ada)
7933 -- From the semantic standpoint the private view of private extension T
7934 -- should be flagged as constrained since one can clearly have
7936 -- Obj : T;
7938 -- in a unit withing Pack. However, when deriving subprograms for the
7939 -- private view of private extension T, T must be seen as unconstrained
7940 -- since T has discriminants (this is a constraint of the current
7941 -- subprogram derivation model). Thus, when processing the private view of
7942 -- a private extension such as T, we first mark T as unconstrained, we
7943 -- process it, we perform program derivation and just before returning from
7944 -- Build_Derived_Record_Type we mark T as constrained.
7946 -- ??? Are there are other uncomfortable cases that we will have to
7947 -- deal with.
7949 -- 10. RECORD_TYPE_WITH_PRIVATE complications
7951 -- Types that are derived from a visible record type and have a private
7952 -- extension present other peculiarities. They behave mostly like private
7953 -- types, but if they have primitive operations defined, these will not
7954 -- have the proper signatures for further inheritance, because other
7955 -- primitive operations will use the implicit base that we define for
7956 -- private derivations below. This affect subprogram inheritance (see
7957 -- Derive_Subprograms for details). We also derive the implicit base from
7958 -- the base type of the full view, so that the implicit base is a record
7959 -- type and not another private type, This avoids infinite loops.
7961 procedure Build_Derived_Record_Type
7962 (N : Node_Id;
7963 Parent_Type : Entity_Id;
7964 Derived_Type : Entity_Id;
7965 Derive_Subps : Boolean := True)
7967 Discriminant_Specs : constant Boolean :=
7968 Present (Discriminant_Specifications (N));
7969 Is_Tagged : constant Boolean := Is_Tagged_Type (Parent_Type);
7970 Loc : constant Source_Ptr := Sloc (N);
7971 Private_Extension : constant Boolean :=
7972 Nkind (N) = N_Private_Extension_Declaration;
7973 Assoc_List : Elist_Id;
7974 Constraint_Present : Boolean;
7975 Constrs : Elist_Id;
7976 Discrim : Entity_Id;
7977 Indic : Node_Id;
7978 Inherit_Discrims : Boolean := False;
7979 Last_Discrim : Entity_Id;
7980 New_Base : Entity_Id;
7981 New_Decl : Node_Id;
7982 New_Discrs : Elist_Id;
7983 New_Indic : Node_Id;
7984 Parent_Base : Entity_Id;
7985 Save_Etype : Entity_Id;
7986 Save_Discr_Constr : Elist_Id;
7987 Save_Next_Entity : Entity_Id;
7988 Type_Def : Node_Id;
7990 Discs : Elist_Id := New_Elmt_List;
7991 -- An empty Discs list means that there were no constraints in the
7992 -- subtype indication or that there was an error processing it.
7994 begin
7995 if Ekind (Parent_Type) = E_Record_Type_With_Private
7996 and then Present (Full_View (Parent_Type))
7997 and then Has_Discriminants (Parent_Type)
7998 then
7999 Parent_Base := Base_Type (Full_View (Parent_Type));
8000 else
8001 Parent_Base := Base_Type (Parent_Type);
8002 end if;
8004 -- AI05-0115 : if this is a derivation from a private type in some
8005 -- other scope that may lead to invisible components for the derived
8006 -- type, mark it accordingly.
8008 if Is_Private_Type (Parent_Type) then
8009 if Scope (Parent_Type) = Scope (Derived_Type) then
8010 null;
8012 elsif In_Open_Scopes (Scope (Parent_Type))
8013 and then In_Private_Part (Scope (Parent_Type))
8014 then
8015 null;
8017 else
8018 Set_Has_Private_Ancestor (Derived_Type);
8019 end if;
8021 else
8022 Set_Has_Private_Ancestor
8023 (Derived_Type, Has_Private_Ancestor (Parent_Type));
8024 end if;
8026 -- Before we start the previously documented transformations, here is
8027 -- little fix for size and alignment of tagged types. Normally when we
8028 -- derive type D from type P, we copy the size and alignment of P as the
8029 -- default for D, and in the absence of explicit representation clauses
8030 -- for D, the size and alignment are indeed the same as the parent.
8032 -- But this is wrong for tagged types, since fields may be added, and
8033 -- the default size may need to be larger, and the default alignment may
8034 -- need to be larger.
8036 -- We therefore reset the size and alignment fields in the tagged case.
8037 -- Note that the size and alignment will in any case be at least as
8038 -- large as the parent type (since the derived type has a copy of the
8039 -- parent type in the _parent field)
8041 -- The type is also marked as being tagged here, which is needed when
8042 -- processing components with a self-referential anonymous access type
8043 -- in the call to Check_Anonymous_Access_Components below. Note that
8044 -- this flag is also set later on for completeness.
8046 if Is_Tagged then
8047 Set_Is_Tagged_Type (Derived_Type);
8048 Init_Size_Align (Derived_Type);
8049 end if;
8051 -- STEP 0a: figure out what kind of derived type declaration we have
8053 if Private_Extension then
8054 Type_Def := N;
8055 Set_Ekind (Derived_Type, E_Record_Type_With_Private);
8056 Set_Default_SSO (Derived_Type);
8058 else
8059 Type_Def := Type_Definition (N);
8061 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
8062 -- Parent_Base can be a private type or private extension. However,
8063 -- for tagged types with an extension the newly added fields are
8064 -- visible and hence the Derived_Type is always an E_Record_Type.
8065 -- (except that the parent may have its own private fields).
8066 -- For untagged types we preserve the Ekind of the Parent_Base.
8068 if Present (Record_Extension_Part (Type_Def)) then
8069 Set_Ekind (Derived_Type, E_Record_Type);
8070 Set_Default_SSO (Derived_Type);
8072 -- Create internal access types for components with anonymous
8073 -- access types.
8075 if Ada_Version >= Ada_2005 then
8076 Check_Anonymous_Access_Components
8077 (N, Derived_Type, Derived_Type,
8078 Component_List (Record_Extension_Part (Type_Def)));
8079 end if;
8081 else
8082 Set_Ekind (Derived_Type, Ekind (Parent_Base));
8083 end if;
8084 end if;
8086 -- Indic can either be an N_Identifier if the subtype indication
8087 -- contains no constraint or an N_Subtype_Indication if the subtype
8088 -- indication has a constraint.
8090 Indic := Subtype_Indication (Type_Def);
8091 Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
8093 -- Check that the type has visible discriminants. The type may be
8094 -- a private type with unknown discriminants whose full view has
8095 -- discriminants which are invisible.
8097 if Constraint_Present then
8098 if not Has_Discriminants (Parent_Base)
8099 or else
8100 (Has_Unknown_Discriminants (Parent_Base)
8101 and then Is_Private_Type (Parent_Base))
8102 then
8103 Error_Msg_N
8104 ("invalid constraint: type has no discriminant",
8105 Constraint (Indic));
8107 Constraint_Present := False;
8108 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
8110 elsif Is_Constrained (Parent_Type) then
8111 Error_Msg_N
8112 ("invalid constraint: parent type is already constrained",
8113 Constraint (Indic));
8115 Constraint_Present := False;
8116 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
8117 end if;
8118 end if;
8120 -- STEP 0b: If needed, apply transformation given in point 5. above
8122 if not Private_Extension
8123 and then Has_Discriminants (Parent_Type)
8124 and then not Discriminant_Specs
8125 and then (Is_Constrained (Parent_Type) or else Constraint_Present)
8126 then
8127 -- First, we must analyze the constraint (see comment in point 5.)
8128 -- The constraint may come from the subtype indication of the full
8129 -- declaration.
8131 if Constraint_Present then
8132 New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
8134 -- If there is no explicit constraint, there might be one that is
8135 -- inherited from a constrained parent type. In that case verify that
8136 -- it conforms to the constraint in the partial view. In perverse
8137 -- cases the parent subtypes of the partial and full view can have
8138 -- different constraints.
8140 elsif Present (Stored_Constraint (Parent_Type)) then
8141 New_Discrs := Stored_Constraint (Parent_Type);
8143 else
8144 New_Discrs := No_Elist;
8145 end if;
8147 if Has_Discriminants (Derived_Type)
8148 and then Has_Private_Declaration (Derived_Type)
8149 and then Present (Discriminant_Constraint (Derived_Type))
8150 and then Present (New_Discrs)
8151 then
8152 -- Verify that constraints of the full view statically match
8153 -- those given in the partial view.
8155 declare
8156 C1, C2 : Elmt_Id;
8158 begin
8159 C1 := First_Elmt (New_Discrs);
8160 C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
8161 while Present (C1) and then Present (C2) loop
8162 if Fully_Conformant_Expressions (Node (C1), Node (C2))
8163 or else
8164 (Is_OK_Static_Expression (Node (C1))
8165 and then Is_OK_Static_Expression (Node (C2))
8166 and then
8167 Expr_Value (Node (C1)) = Expr_Value (Node (C2)))
8168 then
8169 null;
8171 else
8172 if Constraint_Present then
8173 Error_Msg_N
8174 ("constraint not conformant to previous declaration",
8175 Node (C1));
8176 else
8177 Error_Msg_N
8178 ("constraint of full view is incompatible "
8179 & "with partial view", N);
8180 end if;
8181 end if;
8183 Next_Elmt (C1);
8184 Next_Elmt (C2);
8185 end loop;
8186 end;
8187 end if;
8189 -- Insert and analyze the declaration for the unconstrained base type
8191 New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
8193 New_Decl :=
8194 Make_Full_Type_Declaration (Loc,
8195 Defining_Identifier => New_Base,
8196 Type_Definition =>
8197 Make_Derived_Type_Definition (Loc,
8198 Abstract_Present => Abstract_Present (Type_Def),
8199 Limited_Present => Limited_Present (Type_Def),
8200 Subtype_Indication =>
8201 New_Occurrence_Of (Parent_Base, Loc),
8202 Record_Extension_Part =>
8203 Relocate_Node (Record_Extension_Part (Type_Def)),
8204 Interface_List => Interface_List (Type_Def)));
8206 Set_Parent (New_Decl, Parent (N));
8207 Mark_Rewrite_Insertion (New_Decl);
8208 Insert_Before (N, New_Decl);
8210 -- In the extension case, make sure ancestor is frozen appropriately
8211 -- (see also non-discriminated case below).
8213 if Present (Record_Extension_Part (Type_Def))
8214 or else Is_Interface (Parent_Base)
8215 then
8216 Freeze_Before (New_Decl, Parent_Type);
8217 end if;
8219 -- Note that this call passes False for the Derive_Subps parameter
8220 -- because subprogram derivation is deferred until after creating
8221 -- the subtype (see below).
8223 Build_Derived_Type
8224 (New_Decl, Parent_Base, New_Base,
8225 Is_Completion => False, Derive_Subps => False);
8227 -- ??? This needs re-examination to determine whether the
8228 -- above call can simply be replaced by a call to Analyze.
8230 Set_Analyzed (New_Decl);
8232 -- Insert and analyze the declaration for the constrained subtype
8234 if Constraint_Present then
8235 New_Indic :=
8236 Make_Subtype_Indication (Loc,
8237 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
8238 Constraint => Relocate_Node (Constraint (Indic)));
8240 else
8241 declare
8242 Constr_List : constant List_Id := New_List;
8243 C : Elmt_Id;
8244 Expr : Node_Id;
8246 begin
8247 C := First_Elmt (Discriminant_Constraint (Parent_Type));
8248 while Present (C) loop
8249 Expr := Node (C);
8251 -- It is safe here to call New_Copy_Tree since we called
8252 -- Force_Evaluation on each constraint previously
8253 -- in Build_Discriminant_Constraints.
8255 Append (New_Copy_Tree (Expr), To => Constr_List);
8257 Next_Elmt (C);
8258 end loop;
8260 New_Indic :=
8261 Make_Subtype_Indication (Loc,
8262 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
8263 Constraint =>
8264 Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
8265 end;
8266 end if;
8268 Rewrite (N,
8269 Make_Subtype_Declaration (Loc,
8270 Defining_Identifier => Derived_Type,
8271 Subtype_Indication => New_Indic));
8273 Analyze (N);
8275 -- Derivation of subprograms must be delayed until the full subtype
8276 -- has been established, to ensure proper overriding of subprograms
8277 -- inherited by full types. If the derivations occurred as part of
8278 -- the call to Build_Derived_Type above, then the check for type
8279 -- conformance would fail because earlier primitive subprograms
8280 -- could still refer to the full type prior the change to the new
8281 -- subtype and hence would not match the new base type created here.
8282 -- Subprograms are not derived, however, when Derive_Subps is False
8283 -- (since otherwise there could be redundant derivations).
8285 if Derive_Subps then
8286 Derive_Subprograms (Parent_Type, Derived_Type);
8287 end if;
8289 -- For tagged types the Discriminant_Constraint of the new base itype
8290 -- is inherited from the first subtype so that no subtype conformance
8291 -- problem arise when the first subtype overrides primitive
8292 -- operations inherited by the implicit base type.
8294 if Is_Tagged then
8295 Set_Discriminant_Constraint
8296 (New_Base, Discriminant_Constraint (Derived_Type));
8297 end if;
8299 return;
8300 end if;
8302 -- If we get here Derived_Type will have no discriminants or it will be
8303 -- a discriminated unconstrained base type.
8305 -- STEP 1a: perform preliminary actions/checks for derived tagged types
8307 if Is_Tagged then
8309 -- The parent type is frozen for non-private extensions (RM 13.14(7))
8310 -- The declaration of a specific descendant of an interface type
8311 -- freezes the interface type (RM 13.14).
8313 if not Private_Extension or else Is_Interface (Parent_Base) then
8314 Freeze_Before (N, Parent_Type);
8315 end if;
8317 -- In Ada 2005 (AI-344), the restriction that a derived tagged type
8318 -- cannot be declared at a deeper level than its parent type is
8319 -- removed. The check on derivation within a generic body is also
8320 -- relaxed, but there's a restriction that a derived tagged type
8321 -- cannot be declared in a generic body if it's derived directly
8322 -- or indirectly from a formal type of that generic.
8324 if Ada_Version >= Ada_2005 then
8325 if Present (Enclosing_Generic_Body (Derived_Type)) then
8326 declare
8327 Ancestor_Type : Entity_Id;
8329 begin
8330 -- Check to see if any ancestor of the derived type is a
8331 -- formal type.
8333 Ancestor_Type := Parent_Type;
8334 while not Is_Generic_Type (Ancestor_Type)
8335 and then Etype (Ancestor_Type) /= Ancestor_Type
8336 loop
8337 Ancestor_Type := Etype (Ancestor_Type);
8338 end loop;
8340 -- If the derived type does have a formal type as an
8341 -- ancestor, then it's an error if the derived type is
8342 -- declared within the body of the generic unit that
8343 -- declares the formal type in its generic formal part. It's
8344 -- sufficient to check whether the ancestor type is declared
8345 -- inside the same generic body as the derived type (such as
8346 -- within a nested generic spec), in which case the
8347 -- derivation is legal. If the formal type is declared
8348 -- outside of that generic body, then it's guaranteed that
8349 -- the derived type is declared within the generic body of
8350 -- the generic unit declaring the formal type.
8352 if Is_Generic_Type (Ancestor_Type)
8353 and then Enclosing_Generic_Body (Ancestor_Type) /=
8354 Enclosing_Generic_Body (Derived_Type)
8355 then
8356 Error_Msg_NE
8357 ("parent type of& must not be descendant of formal type"
8358 & " of an enclosing generic body",
8359 Indic, Derived_Type);
8360 end if;
8361 end;
8362 end if;
8364 elsif Type_Access_Level (Derived_Type) /=
8365 Type_Access_Level (Parent_Type)
8366 and then not Is_Generic_Type (Derived_Type)
8367 then
8368 if Is_Controlled (Parent_Type) then
8369 Error_Msg_N
8370 ("controlled type must be declared at the library level",
8371 Indic);
8372 else
8373 Error_Msg_N
8374 ("type extension at deeper accessibility level than parent",
8375 Indic);
8376 end if;
8378 else
8379 declare
8380 GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
8381 begin
8382 if Present (GB)
8383 and then GB /= Enclosing_Generic_Body (Parent_Base)
8384 then
8385 Error_Msg_NE
8386 ("parent type of& must not be outside generic body"
8387 & " (RM 3.9.1(4))",
8388 Indic, Derived_Type);
8389 end if;
8390 end;
8391 end if;
8392 end if;
8394 -- Ada 2005 (AI-251)
8396 if Ada_Version >= Ada_2005 and then Is_Tagged then
8398 -- "The declaration of a specific descendant of an interface type
8399 -- freezes the interface type" (RM 13.14).
8401 declare
8402 Iface : Node_Id;
8403 begin
8404 if Is_Non_Empty_List (Interface_List (Type_Def)) then
8405 Iface := First (Interface_List (Type_Def));
8406 while Present (Iface) loop
8407 Freeze_Before (N, Etype (Iface));
8408 Next (Iface);
8409 end loop;
8410 end if;
8411 end;
8412 end if;
8414 -- STEP 1b : preliminary cleanup of the full view of private types
8416 -- If the type is already marked as having discriminants, then it's the
8417 -- completion of a private type or private extension and we need to
8418 -- retain the discriminants from the partial view if the current
8419 -- declaration has Discriminant_Specifications so that we can verify
8420 -- conformance. However, we must remove any existing components that
8421 -- were inherited from the parent (and attached in Copy_And_Swap)
8422 -- because the full type inherits all appropriate components anyway, and
8423 -- we do not want the partial view's components interfering.
8425 if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
8426 Discrim := First_Discriminant (Derived_Type);
8427 loop
8428 Last_Discrim := Discrim;
8429 Next_Discriminant (Discrim);
8430 exit when No (Discrim);
8431 end loop;
8433 Set_Last_Entity (Derived_Type, Last_Discrim);
8435 -- In all other cases wipe out the list of inherited components (even
8436 -- inherited discriminants), it will be properly rebuilt here.
8438 else
8439 Set_First_Entity (Derived_Type, Empty);
8440 Set_Last_Entity (Derived_Type, Empty);
8441 end if;
8443 -- STEP 1c: Initialize some flags for the Derived_Type
8445 -- The following flags must be initialized here so that
8446 -- Process_Discriminants can check that discriminants of tagged types do
8447 -- not have a default initial value and that access discriminants are
8448 -- only specified for limited records. For completeness, these flags are
8449 -- also initialized along with all the other flags below.
8451 -- AI-419: Limitedness is not inherited from an interface parent, so to
8452 -- be limited in that case the type must be explicitly declared as
8453 -- limited. However, task and protected interfaces are always limited.
8455 if Limited_Present (Type_Def) then
8456 Set_Is_Limited_Record (Derived_Type);
8458 elsif Is_Limited_Record (Parent_Type)
8459 or else (Present (Full_View (Parent_Type))
8460 and then Is_Limited_Record (Full_View (Parent_Type)))
8461 then
8462 if not Is_Interface (Parent_Type)
8463 or else Is_Synchronized_Interface (Parent_Type)
8464 or else Is_Protected_Interface (Parent_Type)
8465 or else Is_Task_Interface (Parent_Type)
8466 then
8467 Set_Is_Limited_Record (Derived_Type);
8468 end if;
8469 end if;
8471 -- STEP 2a: process discriminants of derived type if any
8473 Push_Scope (Derived_Type);
8475 if Discriminant_Specs then
8476 Set_Has_Unknown_Discriminants (Derived_Type, False);
8478 -- The following call initializes fields Has_Discriminants and
8479 -- Discriminant_Constraint, unless we are processing the completion
8480 -- of a private type declaration.
8482 Check_Or_Process_Discriminants (N, Derived_Type);
8484 -- For untagged types, the constraint on the Parent_Type must be
8485 -- present and is used to rename the discriminants.
8487 if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
8488 Error_Msg_N ("untagged parent must have discriminants", Indic);
8490 elsif not Is_Tagged and then not Constraint_Present then
8491 Error_Msg_N
8492 ("discriminant constraint needed for derived untagged records",
8493 Indic);
8495 -- Otherwise the parent subtype must be constrained unless we have a
8496 -- private extension.
8498 elsif not Constraint_Present
8499 and then not Private_Extension
8500 and then not Is_Constrained (Parent_Type)
8501 then
8502 Error_Msg_N
8503 ("unconstrained type not allowed in this context", Indic);
8505 elsif Constraint_Present then
8506 -- The following call sets the field Corresponding_Discriminant
8507 -- for the discriminants in the Derived_Type.
8509 Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
8511 -- For untagged types all new discriminants must rename
8512 -- discriminants in the parent. For private extensions new
8513 -- discriminants cannot rename old ones (implied by [7.3(13)]).
8515 Discrim := First_Discriminant (Derived_Type);
8516 while Present (Discrim) loop
8517 if not Is_Tagged
8518 and then No (Corresponding_Discriminant (Discrim))
8519 then
8520 Error_Msg_N
8521 ("new discriminants must constrain old ones", Discrim);
8523 elsif Private_Extension
8524 and then Present (Corresponding_Discriminant (Discrim))
8525 then
8526 Error_Msg_N
8527 ("only static constraints allowed for parent"
8528 & " discriminants in the partial view", Indic);
8529 exit;
8530 end if;
8532 -- If a new discriminant is used in the constraint, then its
8533 -- subtype must be statically compatible with the parent
8534 -- discriminant's subtype (3.7(15)).
8536 -- However, if the record contains an array constrained by
8537 -- the discriminant but with some different bound, the compiler
8538 -- attemps to create a smaller range for the discriminant type.
8539 -- (See exp_ch3.Adjust_Discriminants). In this case, where
8540 -- the discriminant type is a scalar type, the check must use
8541 -- the original discriminant type in the parent declaration.
8543 declare
8544 Corr_Disc : constant Entity_Id :=
8545 Corresponding_Discriminant (Discrim);
8546 Disc_Type : constant Entity_Id := Etype (Discrim);
8547 Corr_Type : Entity_Id;
8549 begin
8550 if Present (Corr_Disc) then
8551 if Is_Scalar_Type (Disc_Type) then
8552 Corr_Type :=
8553 Entity (Discriminant_Type (Parent (Corr_Disc)));
8554 else
8555 Corr_Type := Etype (Corr_Disc);
8556 end if;
8558 if not
8559 Subtypes_Statically_Compatible (Disc_Type, Corr_Type)
8560 then
8561 Error_Msg_N
8562 ("subtype must be compatible "
8563 & "with parent discriminant",
8564 Discrim);
8565 end if;
8566 end if;
8567 end;
8569 Next_Discriminant (Discrim);
8570 end loop;
8572 -- Check whether the constraints of the full view statically
8573 -- match those imposed by the parent subtype [7.3(13)].
8575 if Present (Stored_Constraint (Derived_Type)) then
8576 declare
8577 C1, C2 : Elmt_Id;
8579 begin
8580 C1 := First_Elmt (Discs);
8581 C2 := First_Elmt (Stored_Constraint (Derived_Type));
8582 while Present (C1) and then Present (C2) loop
8583 if not
8584 Fully_Conformant_Expressions (Node (C1), Node (C2))
8585 then
8586 Error_Msg_N
8587 ("not conformant with previous declaration",
8588 Node (C1));
8589 end if;
8591 Next_Elmt (C1);
8592 Next_Elmt (C2);
8593 end loop;
8594 end;
8595 end if;
8596 end if;
8598 -- STEP 2b: No new discriminants, inherit discriminants if any
8600 else
8601 if Private_Extension then
8602 Set_Has_Unknown_Discriminants
8603 (Derived_Type,
8604 Has_Unknown_Discriminants (Parent_Type)
8605 or else Unknown_Discriminants_Present (N));
8607 -- The partial view of the parent may have unknown discriminants,
8608 -- but if the full view has discriminants and the parent type is
8609 -- in scope they must be inherited.
8611 elsif Has_Unknown_Discriminants (Parent_Type)
8612 and then
8613 (not Has_Discriminants (Parent_Type)
8614 or else not In_Open_Scopes (Scope (Parent_Type)))
8615 then
8616 Set_Has_Unknown_Discriminants (Derived_Type);
8617 end if;
8619 if not Has_Unknown_Discriminants (Derived_Type)
8620 and then not Has_Unknown_Discriminants (Parent_Base)
8621 and then Has_Discriminants (Parent_Type)
8622 then
8623 Inherit_Discrims := True;
8624 Set_Has_Discriminants
8625 (Derived_Type, True);
8626 Set_Discriminant_Constraint
8627 (Derived_Type, Discriminant_Constraint (Parent_Base));
8628 end if;
8630 -- The following test is true for private types (remember
8631 -- transformation 5. is not applied to those) and in an error
8632 -- situation.
8634 if Constraint_Present then
8635 Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
8636 end if;
8638 -- For now mark a new derived type as constrained only if it has no
8639 -- discriminants. At the end of Build_Derived_Record_Type we properly
8640 -- set this flag in the case of private extensions. See comments in
8641 -- point 9. just before body of Build_Derived_Record_Type.
8643 Set_Is_Constrained
8644 (Derived_Type,
8645 not (Inherit_Discrims
8646 or else Has_Unknown_Discriminants (Derived_Type)));
8647 end if;
8649 -- STEP 3: initialize fields of derived type
8651 Set_Is_Tagged_Type (Derived_Type, Is_Tagged);
8652 Set_Stored_Constraint (Derived_Type, No_Elist);
8654 -- Ada 2005 (AI-251): Private type-declarations can implement interfaces
8655 -- but cannot be interfaces
8657 if not Private_Extension
8658 and then Ekind (Derived_Type) /= E_Private_Type
8659 and then Ekind (Derived_Type) /= E_Limited_Private_Type
8660 then
8661 if Interface_Present (Type_Def) then
8662 Analyze_Interface_Declaration (Derived_Type, Type_Def);
8663 end if;
8665 Set_Interfaces (Derived_Type, No_Elist);
8666 end if;
8668 -- Fields inherited from the Parent_Type
8670 Set_Has_Specified_Layout
8671 (Derived_Type, Has_Specified_Layout (Parent_Type));
8672 Set_Is_Limited_Composite
8673 (Derived_Type, Is_Limited_Composite (Parent_Type));
8674 Set_Is_Private_Composite
8675 (Derived_Type, Is_Private_Composite (Parent_Type));
8677 if Is_Tagged_Type (Parent_Type) then
8678 Set_No_Tagged_Streams_Pragma
8679 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
8680 end if;
8682 -- Fields inherited from the Parent_Base
8684 Set_Has_Controlled_Component
8685 (Derived_Type, Has_Controlled_Component (Parent_Base));
8686 Set_Has_Non_Standard_Rep
8687 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
8688 Set_Has_Primitive_Operations
8689 (Derived_Type, Has_Primitive_Operations (Parent_Base));
8691 -- Fields inherited from the Parent_Base in the non-private case
8693 if Ekind (Derived_Type) = E_Record_Type then
8694 Set_Has_Complex_Representation
8695 (Derived_Type, Has_Complex_Representation (Parent_Base));
8696 end if;
8698 -- Fields inherited from the Parent_Base for record types
8700 if Is_Record_Type (Derived_Type) then
8701 declare
8702 Parent_Full : Entity_Id;
8704 begin
8705 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
8706 -- Parent_Base can be a private type or private extension. Go
8707 -- to the full view here to get the E_Record_Type specific flags.
8709 if Present (Full_View (Parent_Base)) then
8710 Parent_Full := Full_View (Parent_Base);
8711 else
8712 Parent_Full := Parent_Base;
8713 end if;
8715 Set_OK_To_Reorder_Components
8716 (Derived_Type, OK_To_Reorder_Components (Parent_Full));
8717 end;
8718 end if;
8720 -- Set fields for private derived types
8722 if Is_Private_Type (Derived_Type) then
8723 Set_Depends_On_Private (Derived_Type, True);
8724 Set_Private_Dependents (Derived_Type, New_Elmt_List);
8726 -- Inherit fields from non private record types. If this is the
8727 -- completion of a derivation from a private type, the parent itself
8728 -- is private, and the attributes come from its full view, which must
8729 -- be present.
8731 else
8732 if Is_Private_Type (Parent_Base)
8733 and then not Is_Record_Type (Parent_Base)
8734 then
8735 Set_Component_Alignment
8736 (Derived_Type, Component_Alignment (Full_View (Parent_Base)));
8737 Set_C_Pass_By_Copy
8738 (Derived_Type, C_Pass_By_Copy (Full_View (Parent_Base)));
8739 else
8740 Set_Component_Alignment
8741 (Derived_Type, Component_Alignment (Parent_Base));
8742 Set_C_Pass_By_Copy
8743 (Derived_Type, C_Pass_By_Copy (Parent_Base));
8744 end if;
8745 end if;
8747 -- Set fields for tagged types
8749 if Is_Tagged then
8750 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
8752 -- All tagged types defined in Ada.Finalization are controlled
8754 if Chars (Scope (Derived_Type)) = Name_Finalization
8755 and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
8756 and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
8757 then
8758 Set_Is_Controlled (Derived_Type);
8759 else
8760 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Base));
8761 end if;
8763 -- Minor optimization: there is no need to generate the class-wide
8764 -- entity associated with an underlying record view.
8766 if not Is_Underlying_Record_View (Derived_Type) then
8767 Make_Class_Wide_Type (Derived_Type);
8768 end if;
8770 Set_Is_Abstract_Type (Derived_Type, Abstract_Present (Type_Def));
8772 if Has_Discriminants (Derived_Type)
8773 and then Constraint_Present
8774 then
8775 Set_Stored_Constraint
8776 (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
8777 end if;
8779 if Ada_Version >= Ada_2005 then
8780 declare
8781 Ifaces_List : Elist_Id;
8783 begin
8784 -- Checks rules 3.9.4 (13/2 and 14/2)
8786 if Comes_From_Source (Derived_Type)
8787 and then not Is_Private_Type (Derived_Type)
8788 and then Is_Interface (Parent_Type)
8789 and then not Is_Interface (Derived_Type)
8790 then
8791 if Is_Task_Interface (Parent_Type) then
8792 Error_Msg_N
8793 ("(Ada 2005) task type required (RM 3.9.4 (13.2))",
8794 Derived_Type);
8796 elsif Is_Protected_Interface (Parent_Type) then
8797 Error_Msg_N
8798 ("(Ada 2005) protected type required (RM 3.9.4 (14.2))",
8799 Derived_Type);
8800 end if;
8801 end if;
8803 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
8805 Check_Interfaces (N, Type_Def);
8807 -- Ada 2005 (AI-251): Collect the list of progenitors that are
8808 -- not already in the parents.
8810 Collect_Interfaces
8811 (T => Derived_Type,
8812 Ifaces_List => Ifaces_List,
8813 Exclude_Parents => True);
8815 Set_Interfaces (Derived_Type, Ifaces_List);
8817 -- If the derived type is the anonymous type created for
8818 -- a declaration whose parent has a constraint, propagate
8819 -- the interface list to the source type. This must be done
8820 -- prior to the completion of the analysis of the source type
8821 -- because the components in the extension may contain current
8822 -- instances whose legality depends on some ancestor.
8824 if Is_Itype (Derived_Type) then
8825 declare
8826 Def : constant Node_Id :=
8827 Associated_Node_For_Itype (Derived_Type);
8828 begin
8829 if Present (Def)
8830 and then Nkind (Def) = N_Full_Type_Declaration
8831 then
8832 Set_Interfaces
8833 (Defining_Identifier (Def), Ifaces_List);
8834 end if;
8835 end;
8836 end if;
8838 -- Propagate inherited invariant information of parents
8839 -- and progenitors
8841 if Ada_Version >= Ada_2012
8842 and then not Is_Interface (Derived_Type)
8843 then
8844 if Has_Inheritable_Invariants (Parent_Type) then
8845 Set_Has_Invariants (Derived_Type);
8846 Set_Has_Inheritable_Invariants (Derived_Type);
8848 elsif not Is_Empty_Elmt_List (Ifaces_List) then
8849 declare
8850 AI : Elmt_Id;
8852 begin
8853 AI := First_Elmt (Ifaces_List);
8854 while Present (AI) loop
8855 if Has_Inheritable_Invariants (Node (AI)) then
8856 Set_Has_Invariants (Derived_Type);
8857 Set_Has_Inheritable_Invariants (Derived_Type);
8859 exit;
8860 end if;
8862 Next_Elmt (AI);
8863 end loop;
8864 end;
8865 end if;
8866 end if;
8868 -- A type extension is automatically Ghost when one of its
8869 -- progenitors is Ghost (SPARK RM 6.9(9)). This property is
8870 -- also inherited when the parent type is Ghost, but this is
8871 -- done in Build_Derived_Type as the mechanism also handles
8872 -- untagged derivations.
8874 if Implements_Ghost_Interface (Derived_Type) then
8875 Set_Is_Ghost_Entity (Derived_Type);
8876 end if;
8877 end;
8878 end if;
8880 else
8881 Set_Is_Packed (Derived_Type, Is_Packed (Parent_Base));
8882 Set_Has_Non_Standard_Rep
8883 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
8884 end if;
8886 -- STEP 4: Inherit components from the parent base and constrain them.
8887 -- Apply the second transformation described in point 6. above.
8889 if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
8890 or else not Has_Discriminants (Parent_Type)
8891 or else not Is_Constrained (Parent_Type)
8892 then
8893 Constrs := Discs;
8894 else
8895 Constrs := Discriminant_Constraint (Parent_Type);
8896 end if;
8898 Assoc_List :=
8899 Inherit_Components
8900 (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
8902 -- STEP 5a: Copy the parent record declaration for untagged types
8904 if not Is_Tagged then
8906 -- Discriminant_Constraint (Derived_Type) has been properly
8907 -- constructed. Save it and temporarily set it to Empty because we
8908 -- do not want the call to New_Copy_Tree below to mess this list.
8910 if Has_Discriminants (Derived_Type) then
8911 Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
8912 Set_Discriminant_Constraint (Derived_Type, No_Elist);
8913 else
8914 Save_Discr_Constr := No_Elist;
8915 end if;
8917 -- Save the Etype field of Derived_Type. It is correctly set now,
8918 -- but the call to New_Copy tree may remap it to point to itself,
8919 -- which is not what we want. Ditto for the Next_Entity field.
8921 Save_Etype := Etype (Derived_Type);
8922 Save_Next_Entity := Next_Entity (Derived_Type);
8924 -- Assoc_List maps all stored discriminants in the Parent_Base to
8925 -- stored discriminants in the Derived_Type. It is fundamental that
8926 -- no types or itypes with discriminants other than the stored
8927 -- discriminants appear in the entities declared inside
8928 -- Derived_Type, since the back end cannot deal with it.
8930 New_Decl :=
8931 New_Copy_Tree
8932 (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
8934 -- Restore the fields saved prior to the New_Copy_Tree call
8935 -- and compute the stored constraint.
8937 Set_Etype (Derived_Type, Save_Etype);
8938 Set_Next_Entity (Derived_Type, Save_Next_Entity);
8940 if Has_Discriminants (Derived_Type) then
8941 Set_Discriminant_Constraint
8942 (Derived_Type, Save_Discr_Constr);
8943 Set_Stored_Constraint
8944 (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
8945 Replace_Components (Derived_Type, New_Decl);
8946 Set_Has_Implicit_Dereference
8947 (Derived_Type, Has_Implicit_Dereference (Parent_Type));
8948 end if;
8950 -- Insert the new derived type declaration
8952 Rewrite (N, New_Decl);
8954 -- STEP 5b: Complete the processing for record extensions in generics
8956 -- There is no completion for record extensions declared in the
8957 -- parameter part of a generic, so we need to complete processing for
8958 -- these generic record extensions here. The Record_Type_Definition call
8959 -- will change the Ekind of the components from E_Void to E_Component.
8961 elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
8962 Record_Type_Definition (Empty, Derived_Type);
8964 -- STEP 5c: Process the record extension for non private tagged types
8966 elsif not Private_Extension then
8967 Expand_Record_Extension (Derived_Type, Type_Def);
8969 -- Note : previously in ASIS mode we set the Parent_Subtype of the
8970 -- derived type to propagate some semantic information. This led
8971 -- to other ASIS failures and has been removed.
8973 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
8974 -- implemented interfaces if we are in expansion mode
8976 if Expander_Active
8977 and then Has_Interfaces (Derived_Type)
8978 then
8979 Add_Interface_Tag_Components (N, Derived_Type);
8980 end if;
8982 -- Analyze the record extension
8984 Record_Type_Definition
8985 (Record_Extension_Part (Type_Def), Derived_Type);
8986 end if;
8988 End_Scope;
8990 -- Nothing else to do if there is an error in the derivation.
8991 -- An unusual case: the full view may be derived from a type in an
8992 -- instance, when the partial view was used illegally as an actual
8993 -- in that instance, leading to a circular definition.
8995 if Etype (Derived_Type) = Any_Type
8996 or else Etype (Parent_Type) = Derived_Type
8997 then
8998 return;
8999 end if;
9001 -- Set delayed freeze and then derive subprograms, we need to do
9002 -- this in this order so that derived subprograms inherit the
9003 -- derived freeze if necessary.
9005 Set_Has_Delayed_Freeze (Derived_Type);
9007 if Derive_Subps then
9008 Derive_Subprograms (Parent_Type, Derived_Type);
9009 end if;
9011 -- If we have a private extension which defines a constrained derived
9012 -- type mark as constrained here after we have derived subprograms. See
9013 -- comment on point 9. just above the body of Build_Derived_Record_Type.
9015 if Private_Extension and then Inherit_Discrims then
9016 if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
9017 Set_Is_Constrained (Derived_Type, True);
9018 Set_Discriminant_Constraint (Derived_Type, Discs);
9020 elsif Is_Constrained (Parent_Type) then
9021 Set_Is_Constrained
9022 (Derived_Type, True);
9023 Set_Discriminant_Constraint
9024 (Derived_Type, Discriminant_Constraint (Parent_Type));
9025 end if;
9026 end if;
9028 -- Update the class-wide type, which shares the now-completed entity
9029 -- list with its specific type. In case of underlying record views,
9030 -- we do not generate the corresponding class wide entity.
9032 if Is_Tagged
9033 and then not Is_Underlying_Record_View (Derived_Type)
9034 then
9035 Set_First_Entity
9036 (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
9037 Set_Last_Entity
9038 (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
9039 end if;
9041 Check_Function_Writable_Actuals (N);
9042 end Build_Derived_Record_Type;
9044 ------------------------
9045 -- Build_Derived_Type --
9046 ------------------------
9048 procedure Build_Derived_Type
9049 (N : Node_Id;
9050 Parent_Type : Entity_Id;
9051 Derived_Type : Entity_Id;
9052 Is_Completion : Boolean;
9053 Derive_Subps : Boolean := True)
9055 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
9057 begin
9058 -- Set common attributes
9060 Set_Scope (Derived_Type, Current_Scope);
9062 Set_Etype (Derived_Type, Parent_Base);
9063 Set_Ekind (Derived_Type, Ekind (Parent_Base));
9064 Set_Has_Task (Derived_Type, Has_Task (Parent_Base));
9065 Set_Has_Protected (Derived_Type, Has_Protected (Parent_Base));
9067 Set_Size_Info (Derived_Type, Parent_Type);
9068 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
9069 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
9070 Set_Disable_Controlled (Derived_Type, Disable_Controlled (Parent_Type));
9072 Set_Is_Tagged_Type (Derived_Type, Is_Tagged_Type (Parent_Type));
9073 Set_Is_Volatile (Derived_Type, Is_Volatile (Parent_Type));
9075 if Is_Tagged_Type (Derived_Type) then
9076 Set_No_Tagged_Streams_Pragma
9077 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
9078 end if;
9080 -- If the parent has primitive routines, set the derived type link
9082 if Has_Primitive_Operations (Parent_Type) then
9083 Set_Derived_Type_Link (Parent_Base, Derived_Type);
9084 end if;
9086 -- If the parent type is a private subtype, the convention on the base
9087 -- type may be set in the private part, and not propagated to the
9088 -- subtype until later, so we obtain the convention from the base type.
9090 Set_Convention (Derived_Type, Convention (Parent_Base));
9092 -- Set SSO default for record or array type
9094 if (Is_Array_Type (Derived_Type) or else Is_Record_Type (Derived_Type))
9095 and then Is_Base_Type (Derived_Type)
9096 then
9097 Set_Default_SSO (Derived_Type);
9098 end if;
9100 -- Propagate invariant information. The new type has invariants if
9101 -- they are inherited from the parent type, and these invariants can
9102 -- be further inherited, so both flags are set.
9104 -- We similarly inherit predicates
9106 if Has_Predicates (Parent_Type) then
9107 Set_Has_Predicates (Derived_Type);
9108 end if;
9110 -- The derived type inherits the representation clauses of the parent
9112 Inherit_Rep_Item_Chain (Derived_Type, Parent_Type);
9114 -- Propagate the attributes related to pragma Default_Initial_Condition
9115 -- from the parent type to the private extension. A derived type always
9116 -- inherits the default initial condition flag from the parent type. If
9117 -- the derived type carries its own Default_Initial_Condition pragma,
9118 -- the flag is later reset in Analyze_Pragma. Note that both flags are
9119 -- mutually exclusive.
9121 Propagate_Default_Init_Cond_Attributes
9122 (From_Typ => Parent_Type,
9123 To_Typ => Derived_Type,
9124 Parent_To_Derivation => True);
9126 -- If the parent type has delayed rep aspects, then mark the derived
9127 -- type as possibly inheriting a delayed rep aspect.
9129 if Has_Delayed_Rep_Aspects (Parent_Type) then
9130 Set_May_Inherit_Delayed_Rep_Aspects (Derived_Type);
9131 end if;
9133 -- Propagate the attributes related to pragma Ghost from the parent type
9134 -- to the derived type or type extension (SPARK RM 6.9(9)).
9136 if Is_Ghost_Entity (Parent_Type) then
9137 Set_Is_Ghost_Entity (Derived_Type);
9138 end if;
9140 -- Type dependent processing
9142 case Ekind (Parent_Type) is
9143 when Numeric_Kind =>
9144 Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
9146 when Array_Kind =>
9147 Build_Derived_Array_Type (N, Parent_Type, Derived_Type);
9149 when E_Record_Type
9150 | E_Record_Subtype
9151 | Class_Wide_Kind =>
9152 Build_Derived_Record_Type
9153 (N, Parent_Type, Derived_Type, Derive_Subps);
9154 return;
9156 when Enumeration_Kind =>
9157 Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
9159 when Access_Kind =>
9160 Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
9162 when Incomplete_Or_Private_Kind =>
9163 Build_Derived_Private_Type
9164 (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
9166 -- For discriminated types, the derivation includes deriving
9167 -- primitive operations. For others it is done below.
9169 if Is_Tagged_Type (Parent_Type)
9170 or else Has_Discriminants (Parent_Type)
9171 or else (Present (Full_View (Parent_Type))
9172 and then Has_Discriminants (Full_View (Parent_Type)))
9173 then
9174 return;
9175 end if;
9177 when Concurrent_Kind =>
9178 Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
9180 when others =>
9181 raise Program_Error;
9182 end case;
9184 -- Nothing more to do if some error occurred
9186 if Etype (Derived_Type) = Any_Type then
9187 return;
9188 end if;
9190 -- Set delayed freeze and then derive subprograms, we need to do this
9191 -- in this order so that derived subprograms inherit the derived freeze
9192 -- if necessary.
9194 Set_Has_Delayed_Freeze (Derived_Type);
9196 if Derive_Subps then
9197 Derive_Subprograms (Parent_Type, Derived_Type);
9198 end if;
9200 Set_Has_Primitive_Operations
9201 (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
9202 end Build_Derived_Type;
9204 -----------------------
9205 -- Build_Discriminal --
9206 -----------------------
9208 procedure Build_Discriminal (Discrim : Entity_Id) is
9209 D_Minal : Entity_Id;
9210 CR_Disc : Entity_Id;
9212 begin
9213 -- A discriminal has the same name as the discriminant
9215 D_Minal := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9217 Set_Ekind (D_Minal, E_In_Parameter);
9218 Set_Mechanism (D_Minal, Default_Mechanism);
9219 Set_Etype (D_Minal, Etype (Discrim));
9220 Set_Scope (D_Minal, Current_Scope);
9222 Set_Discriminal (Discrim, D_Minal);
9223 Set_Discriminal_Link (D_Minal, Discrim);
9225 -- For task types, build at once the discriminants of the corresponding
9226 -- record, which are needed if discriminants are used in entry defaults
9227 -- and in family bounds.
9229 if Is_Concurrent_Type (Current_Scope)
9230 or else
9231 Is_Limited_Type (Current_Scope)
9232 then
9233 CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9235 Set_Ekind (CR_Disc, E_In_Parameter);
9236 Set_Mechanism (CR_Disc, Default_Mechanism);
9237 Set_Etype (CR_Disc, Etype (Discrim));
9238 Set_Scope (CR_Disc, Current_Scope);
9239 Set_Discriminal_Link (CR_Disc, Discrim);
9240 Set_CR_Discriminant (Discrim, CR_Disc);
9241 end if;
9242 end Build_Discriminal;
9244 ------------------------------------
9245 -- Build_Discriminant_Constraints --
9246 ------------------------------------
9248 function Build_Discriminant_Constraints
9249 (T : Entity_Id;
9250 Def : Node_Id;
9251 Derived_Def : Boolean := False) return Elist_Id
9253 C : constant Node_Id := Constraint (Def);
9254 Nb_Discr : constant Nat := Number_Discriminants (T);
9256 Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
9257 -- Saves the expression corresponding to a given discriminant in T
9259 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
9260 -- Return the Position number within array Discr_Expr of a discriminant
9261 -- D within the discriminant list of the discriminated type T.
9263 procedure Process_Discriminant_Expression
9264 (Expr : Node_Id;
9265 D : Entity_Id);
9266 -- If this is a discriminant constraint on a partial view, do not
9267 -- generate an overflow check on the discriminant expression. The check
9268 -- will be generated when constraining the full view. Otherwise the
9269 -- backend creates duplicate symbols for the temporaries corresponding
9270 -- to the expressions to be checked, causing spurious assembler errors.
9272 ------------------
9273 -- Pos_Of_Discr --
9274 ------------------
9276 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
9277 Disc : Entity_Id;
9279 begin
9280 Disc := First_Discriminant (T);
9281 for J in Discr_Expr'Range loop
9282 if Disc = D then
9283 return J;
9284 end if;
9286 Next_Discriminant (Disc);
9287 end loop;
9289 -- Note: Since this function is called on discriminants that are
9290 -- known to belong to the discriminated type, falling through the
9291 -- loop with no match signals an internal compiler error.
9293 raise Program_Error;
9294 end Pos_Of_Discr;
9296 -------------------------------------
9297 -- Process_Discriminant_Expression --
9298 -------------------------------------
9300 procedure Process_Discriminant_Expression
9301 (Expr : Node_Id;
9302 D : Entity_Id)
9304 BDT : constant Entity_Id := Base_Type (Etype (D));
9306 begin
9307 -- If this is a discriminant constraint on a partial view, do
9308 -- not generate an overflow on the discriminant expression. The
9309 -- check will be generated when constraining the full view.
9311 if Is_Private_Type (T)
9312 and then Present (Full_View (T))
9313 then
9314 Analyze_And_Resolve (Expr, BDT, Suppress => Overflow_Check);
9315 else
9316 Analyze_And_Resolve (Expr, BDT);
9317 end if;
9318 end Process_Discriminant_Expression;
9320 -- Declarations local to Build_Discriminant_Constraints
9322 Discr : Entity_Id;
9323 E : Entity_Id;
9324 Elist : constant Elist_Id := New_Elmt_List;
9326 Constr : Node_Id;
9327 Expr : Node_Id;
9328 Id : Node_Id;
9329 Position : Nat;
9330 Found : Boolean;
9332 Discrim_Present : Boolean := False;
9334 -- Start of processing for Build_Discriminant_Constraints
9336 begin
9337 -- The following loop will process positional associations only.
9338 -- For a positional association, the (single) discriminant is
9339 -- implicitly specified by position, in textual order (RM 3.7.2).
9341 Discr := First_Discriminant (T);
9342 Constr := First (Constraints (C));
9343 for D in Discr_Expr'Range loop
9344 exit when Nkind (Constr) = N_Discriminant_Association;
9346 if No (Constr) then
9347 Error_Msg_N ("too few discriminants given in constraint", C);
9348 return New_Elmt_List;
9350 elsif Nkind (Constr) = N_Range
9351 or else (Nkind (Constr) = N_Attribute_Reference
9352 and then Attribute_Name (Constr) = Name_Range)
9353 then
9354 Error_Msg_N
9355 ("a range is not a valid discriminant constraint", Constr);
9356 Discr_Expr (D) := Error;
9358 else
9359 Process_Discriminant_Expression (Constr, Discr);
9360 Discr_Expr (D) := Constr;
9361 end if;
9363 Next_Discriminant (Discr);
9364 Next (Constr);
9365 end loop;
9367 if No (Discr) and then Present (Constr) then
9368 Error_Msg_N ("too many discriminants given in constraint", Constr);
9369 return New_Elmt_List;
9370 end if;
9372 -- Named associations can be given in any order, but if both positional
9373 -- and named associations are used in the same discriminant constraint,
9374 -- then positional associations must occur first, at their normal
9375 -- position. Hence once a named association is used, the rest of the
9376 -- discriminant constraint must use only named associations.
9378 while Present (Constr) loop
9380 -- Positional association forbidden after a named association
9382 if Nkind (Constr) /= N_Discriminant_Association then
9383 Error_Msg_N ("positional association follows named one", Constr);
9384 return New_Elmt_List;
9386 -- Otherwise it is a named association
9388 else
9389 -- E records the type of the discriminants in the named
9390 -- association. All the discriminants specified in the same name
9391 -- association must have the same type.
9393 E := Empty;
9395 -- Search the list of discriminants in T to see if the simple name
9396 -- given in the constraint matches any of them.
9398 Id := First (Selector_Names (Constr));
9399 while Present (Id) loop
9400 Found := False;
9402 -- If Original_Discriminant is present, we are processing a
9403 -- generic instantiation and this is an instance node. We need
9404 -- to find the name of the corresponding discriminant in the
9405 -- actual record type T and not the name of the discriminant in
9406 -- the generic formal. Example:
9408 -- generic
9409 -- type G (D : int) is private;
9410 -- package P is
9411 -- subtype W is G (D => 1);
9412 -- end package;
9413 -- type Rec (X : int) is record ... end record;
9414 -- package Q is new P (G => Rec);
9416 -- At the point of the instantiation, formal type G is Rec
9417 -- and therefore when reanalyzing "subtype W is G (D => 1);"
9418 -- which really looks like "subtype W is Rec (D => 1);" at
9419 -- the point of instantiation, we want to find the discriminant
9420 -- that corresponds to D in Rec, i.e. X.
9422 if Present (Original_Discriminant (Id))
9423 and then In_Instance
9424 then
9425 Discr := Find_Corresponding_Discriminant (Id, T);
9426 Found := True;
9428 else
9429 Discr := First_Discriminant (T);
9430 while Present (Discr) loop
9431 if Chars (Discr) = Chars (Id) then
9432 Found := True;
9433 exit;
9434 end if;
9436 Next_Discriminant (Discr);
9437 end loop;
9439 if not Found then
9440 Error_Msg_N ("& does not match any discriminant", Id);
9441 return New_Elmt_List;
9443 -- If the parent type is a generic formal, preserve the
9444 -- name of the discriminant for subsequent instances.
9445 -- see comment at the beginning of this if statement.
9447 elsif Is_Generic_Type (Root_Type (T)) then
9448 Set_Original_Discriminant (Id, Discr);
9449 end if;
9450 end if;
9452 Position := Pos_Of_Discr (T, Discr);
9454 if Present (Discr_Expr (Position)) then
9455 Error_Msg_N ("duplicate constraint for discriminant&", Id);
9457 else
9458 -- Each discriminant specified in the same named association
9459 -- must be associated with a separate copy of the
9460 -- corresponding expression.
9462 if Present (Next (Id)) then
9463 Expr := New_Copy_Tree (Expression (Constr));
9464 Set_Parent (Expr, Parent (Expression (Constr)));
9465 else
9466 Expr := Expression (Constr);
9467 end if;
9469 Discr_Expr (Position) := Expr;
9470 Process_Discriminant_Expression (Expr, Discr);
9471 end if;
9473 -- A discriminant association with more than one discriminant
9474 -- name is only allowed if the named discriminants are all of
9475 -- the same type (RM 3.7.1(8)).
9477 if E = Empty then
9478 E := Base_Type (Etype (Discr));
9480 elsif Base_Type (Etype (Discr)) /= E then
9481 Error_Msg_N
9482 ("all discriminants in an association " &
9483 "must have the same type", Id);
9484 end if;
9486 Next (Id);
9487 end loop;
9488 end if;
9490 Next (Constr);
9491 end loop;
9493 -- A discriminant constraint must provide exactly one value for each
9494 -- discriminant of the type (RM 3.7.1(8)).
9496 for J in Discr_Expr'Range loop
9497 if No (Discr_Expr (J)) then
9498 Error_Msg_N ("too few discriminants given in constraint", C);
9499 return New_Elmt_List;
9500 end if;
9501 end loop;
9503 -- Determine if there are discriminant expressions in the constraint
9505 for J in Discr_Expr'Range loop
9506 if Denotes_Discriminant
9507 (Discr_Expr (J), Check_Concurrent => True)
9508 then
9509 Discrim_Present := True;
9510 end if;
9511 end loop;
9513 -- Build an element list consisting of the expressions given in the
9514 -- discriminant constraint and apply the appropriate checks. The list
9515 -- is constructed after resolving any named discriminant associations
9516 -- and therefore the expressions appear in the textual order of the
9517 -- discriminants.
9519 Discr := First_Discriminant (T);
9520 for J in Discr_Expr'Range loop
9521 if Discr_Expr (J) /= Error then
9522 Append_Elmt (Discr_Expr (J), Elist);
9524 -- If any of the discriminant constraints is given by a
9525 -- discriminant and we are in a derived type declaration we
9526 -- have a discriminant renaming. Establish link between new
9527 -- and old discriminant.
9529 if Denotes_Discriminant (Discr_Expr (J)) then
9530 if Derived_Def then
9531 Set_Corresponding_Discriminant
9532 (Entity (Discr_Expr (J)), Discr);
9533 end if;
9535 -- Force the evaluation of non-discriminant expressions.
9536 -- If we have found a discriminant in the constraint 3.4(26)
9537 -- and 3.8(18) demand that no range checks are performed are
9538 -- after evaluation. If the constraint is for a component
9539 -- definition that has a per-object constraint, expressions are
9540 -- evaluated but not checked either. In all other cases perform
9541 -- a range check.
9543 else
9544 if Discrim_Present then
9545 null;
9547 elsif Nkind (Parent (Parent (Def))) = N_Component_Declaration
9548 and then
9549 Has_Per_Object_Constraint
9550 (Defining_Identifier (Parent (Parent (Def))))
9551 then
9552 null;
9554 elsif Is_Access_Type (Etype (Discr)) then
9555 Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
9557 else
9558 Apply_Range_Check (Discr_Expr (J), Etype (Discr));
9559 end if;
9561 Force_Evaluation (Discr_Expr (J));
9562 end if;
9564 -- Check that the designated type of an access discriminant's
9565 -- expression is not a class-wide type unless the discriminant's
9566 -- designated type is also class-wide.
9568 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
9569 and then not Is_Class_Wide_Type
9570 (Designated_Type (Etype (Discr)))
9571 and then Etype (Discr_Expr (J)) /= Any_Type
9572 and then Is_Class_Wide_Type
9573 (Designated_Type (Etype (Discr_Expr (J))))
9574 then
9575 Wrong_Type (Discr_Expr (J), Etype (Discr));
9577 elsif Is_Access_Type (Etype (Discr))
9578 and then not Is_Access_Constant (Etype (Discr))
9579 and then Is_Access_Type (Etype (Discr_Expr (J)))
9580 and then Is_Access_Constant (Etype (Discr_Expr (J)))
9581 then
9582 Error_Msg_NE
9583 ("constraint for discriminant& must be access to variable",
9584 Def, Discr);
9585 end if;
9586 end if;
9588 Next_Discriminant (Discr);
9589 end loop;
9591 return Elist;
9592 end Build_Discriminant_Constraints;
9594 ---------------------------------
9595 -- Build_Discriminated_Subtype --
9596 ---------------------------------
9598 procedure Build_Discriminated_Subtype
9599 (T : Entity_Id;
9600 Def_Id : Entity_Id;
9601 Elist : Elist_Id;
9602 Related_Nod : Node_Id;
9603 For_Access : Boolean := False)
9605 Has_Discrs : constant Boolean := Has_Discriminants (T);
9606 Constrained : constant Boolean :=
9607 (Has_Discrs
9608 and then not Is_Empty_Elmt_List (Elist)
9609 and then not Is_Class_Wide_Type (T))
9610 or else Is_Constrained (T);
9612 begin
9613 if Ekind (T) = E_Record_Type then
9614 if For_Access then
9615 Set_Ekind (Def_Id, E_Private_Subtype);
9616 Set_Is_For_Access_Subtype (Def_Id, True);
9617 else
9618 Set_Ekind (Def_Id, E_Record_Subtype);
9619 end if;
9621 -- Inherit preelaboration flag from base, for types for which it
9622 -- may have been set: records, private types, protected types.
9624 Set_Known_To_Have_Preelab_Init
9625 (Def_Id, Known_To_Have_Preelab_Init (T));
9627 elsif Ekind (T) = E_Task_Type then
9628 Set_Ekind (Def_Id, E_Task_Subtype);
9630 elsif Ekind (T) = E_Protected_Type then
9631 Set_Ekind (Def_Id, E_Protected_Subtype);
9632 Set_Known_To_Have_Preelab_Init
9633 (Def_Id, Known_To_Have_Preelab_Init (T));
9635 elsif Is_Private_Type (T) then
9636 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
9637 Set_Known_To_Have_Preelab_Init
9638 (Def_Id, Known_To_Have_Preelab_Init (T));
9640 -- Private subtypes may have private dependents
9642 Set_Private_Dependents (Def_Id, New_Elmt_List);
9644 elsif Is_Class_Wide_Type (T) then
9645 Set_Ekind (Def_Id, E_Class_Wide_Subtype);
9647 else
9648 -- Incomplete type. Attach subtype to list of dependents, to be
9649 -- completed with full view of parent type, unless is it the
9650 -- designated subtype of a record component within an init_proc.
9651 -- This last case arises for a component of an access type whose
9652 -- designated type is incomplete (e.g. a Taft Amendment type).
9653 -- The designated subtype is within an inner scope, and needs no
9654 -- elaboration, because only the access type is needed in the
9655 -- initialization procedure.
9657 Set_Ekind (Def_Id, Ekind (T));
9659 if For_Access and then Within_Init_Proc then
9660 null;
9661 else
9662 Append_Elmt (Def_Id, Private_Dependents (T));
9663 end if;
9664 end if;
9666 Set_Etype (Def_Id, T);
9667 Init_Size_Align (Def_Id);
9668 Set_Has_Discriminants (Def_Id, Has_Discrs);
9669 Set_Is_Constrained (Def_Id, Constrained);
9671 Set_First_Entity (Def_Id, First_Entity (T));
9672 Set_Last_Entity (Def_Id, Last_Entity (T));
9673 Set_Has_Implicit_Dereference
9674 (Def_Id, Has_Implicit_Dereference (T));
9676 -- If the subtype is the completion of a private declaration, there may
9677 -- have been representation clauses for the partial view, and they must
9678 -- be preserved. Build_Derived_Type chains the inherited clauses with
9679 -- the ones appearing on the extension. If this comes from a subtype
9680 -- declaration, all clauses are inherited.
9682 if No (First_Rep_Item (Def_Id)) then
9683 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
9684 end if;
9686 if Is_Tagged_Type (T) then
9687 Set_Is_Tagged_Type (Def_Id);
9688 Set_No_Tagged_Streams_Pragma (Def_Id, No_Tagged_Streams_Pragma (T));
9689 Make_Class_Wide_Type (Def_Id);
9690 end if;
9692 Set_Stored_Constraint (Def_Id, No_Elist);
9694 if Has_Discrs then
9695 Set_Discriminant_Constraint (Def_Id, Elist);
9696 Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
9697 end if;
9699 if Is_Tagged_Type (T) then
9701 -- Ada 2005 (AI-251): In case of concurrent types we inherit the
9702 -- concurrent record type (which has the list of primitive
9703 -- operations).
9705 if Ada_Version >= Ada_2005
9706 and then Is_Concurrent_Type (T)
9707 then
9708 Set_Corresponding_Record_Type (Def_Id,
9709 Corresponding_Record_Type (T));
9710 else
9711 Set_Direct_Primitive_Operations (Def_Id,
9712 Direct_Primitive_Operations (T));
9713 end if;
9715 Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
9716 end if;
9718 -- Subtypes introduced by component declarations do not need to be
9719 -- marked as delayed, and do not get freeze nodes, because the semantics
9720 -- verifies that the parents of the subtypes are frozen before the
9721 -- enclosing record is frozen.
9723 if not Is_Type (Scope (Def_Id)) then
9724 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
9726 if Is_Private_Type (T)
9727 and then Present (Full_View (T))
9728 then
9729 Conditional_Delay (Def_Id, Full_View (T));
9730 else
9731 Conditional_Delay (Def_Id, T);
9732 end if;
9733 end if;
9735 if Is_Record_Type (T) then
9736 Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
9738 if Has_Discrs
9739 and then not Is_Empty_Elmt_List (Elist)
9740 and then not For_Access
9741 then
9742 Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
9743 elsif not For_Access then
9744 Set_Cloned_Subtype (Def_Id, T);
9745 end if;
9746 end if;
9747 end Build_Discriminated_Subtype;
9749 ---------------------------
9750 -- Build_Itype_Reference --
9751 ---------------------------
9753 procedure Build_Itype_Reference
9754 (Ityp : Entity_Id;
9755 Nod : Node_Id)
9757 IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
9758 begin
9760 -- Itype references are only created for use by the back-end
9762 if Inside_A_Generic then
9763 return;
9764 else
9765 Set_Itype (IR, Ityp);
9766 Insert_After (Nod, IR);
9767 end if;
9768 end Build_Itype_Reference;
9770 ------------------------
9771 -- Build_Scalar_Bound --
9772 ------------------------
9774 function Build_Scalar_Bound
9775 (Bound : Node_Id;
9776 Par_T : Entity_Id;
9777 Der_T : Entity_Id) return Node_Id
9779 New_Bound : Entity_Id;
9781 begin
9782 -- Note: not clear why this is needed, how can the original bound
9783 -- be unanalyzed at this point? and if it is, what business do we
9784 -- have messing around with it? and why is the base type of the
9785 -- parent type the right type for the resolution. It probably is
9786 -- not. It is OK for the new bound we are creating, but not for
9787 -- the old one??? Still if it never happens, no problem.
9789 Analyze_And_Resolve (Bound, Base_Type (Par_T));
9791 if Nkind_In (Bound, N_Integer_Literal, N_Real_Literal) then
9792 New_Bound := New_Copy (Bound);
9793 Set_Etype (New_Bound, Der_T);
9794 Set_Analyzed (New_Bound);
9796 elsif Is_Entity_Name (Bound) then
9797 New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
9799 -- The following is almost certainly wrong. What business do we have
9800 -- relocating a node (Bound) that is presumably still attached to
9801 -- the tree elsewhere???
9803 else
9804 New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
9805 end if;
9807 Set_Etype (New_Bound, Der_T);
9808 return New_Bound;
9809 end Build_Scalar_Bound;
9811 --------------------------------
9812 -- Build_Underlying_Full_View --
9813 --------------------------------
9815 procedure Build_Underlying_Full_View
9816 (N : Node_Id;
9817 Typ : Entity_Id;
9818 Par : Entity_Id)
9820 Loc : constant Source_Ptr := Sloc (N);
9821 Subt : constant Entity_Id :=
9822 Make_Defining_Identifier
9823 (Loc, New_External_Name (Chars (Typ), 'S'));
9825 Constr : Node_Id;
9826 Indic : Node_Id;
9827 C : Node_Id;
9828 Id : Node_Id;
9830 procedure Set_Discriminant_Name (Id : Node_Id);
9831 -- If the derived type has discriminants, they may rename discriminants
9832 -- of the parent. When building the full view of the parent, we need to
9833 -- recover the names of the original discriminants if the constraint is
9834 -- given by named associations.
9836 ---------------------------
9837 -- Set_Discriminant_Name --
9838 ---------------------------
9840 procedure Set_Discriminant_Name (Id : Node_Id) is
9841 Disc : Entity_Id;
9843 begin
9844 Set_Original_Discriminant (Id, Empty);
9846 if Has_Discriminants (Typ) then
9847 Disc := First_Discriminant (Typ);
9848 while Present (Disc) loop
9849 if Chars (Disc) = Chars (Id)
9850 and then Present (Corresponding_Discriminant (Disc))
9851 then
9852 Set_Chars (Id, Chars (Corresponding_Discriminant (Disc)));
9853 end if;
9854 Next_Discriminant (Disc);
9855 end loop;
9856 end if;
9857 end Set_Discriminant_Name;
9859 -- Start of processing for Build_Underlying_Full_View
9861 begin
9862 if Nkind (N) = N_Full_Type_Declaration then
9863 Constr := Constraint (Subtype_Indication (Type_Definition (N)));
9865 elsif Nkind (N) = N_Subtype_Declaration then
9866 Constr := New_Copy_Tree (Constraint (Subtype_Indication (N)));
9868 elsif Nkind (N) = N_Component_Declaration then
9869 Constr :=
9870 New_Copy_Tree
9871 (Constraint (Subtype_Indication (Component_Definition (N))));
9873 else
9874 raise Program_Error;
9875 end if;
9877 C := First (Constraints (Constr));
9878 while Present (C) loop
9879 if Nkind (C) = N_Discriminant_Association then
9880 Id := First (Selector_Names (C));
9881 while Present (Id) loop
9882 Set_Discriminant_Name (Id);
9883 Next (Id);
9884 end loop;
9885 end if;
9887 Next (C);
9888 end loop;
9890 Indic :=
9891 Make_Subtype_Declaration (Loc,
9892 Defining_Identifier => Subt,
9893 Subtype_Indication =>
9894 Make_Subtype_Indication (Loc,
9895 Subtype_Mark => New_Occurrence_Of (Par, Loc),
9896 Constraint => New_Copy_Tree (Constr)));
9898 -- If this is a component subtype for an outer itype, it is not
9899 -- a list member, so simply set the parent link for analysis: if
9900 -- the enclosing type does not need to be in a declarative list,
9901 -- neither do the components.
9903 if Is_List_Member (N)
9904 and then Nkind (N) /= N_Component_Declaration
9905 then
9906 Insert_Before (N, Indic);
9907 else
9908 Set_Parent (Indic, Parent (N));
9909 end if;
9911 Analyze (Indic);
9912 Set_Underlying_Full_View (Typ, Full_View (Subt));
9913 end Build_Underlying_Full_View;
9915 -------------------------------
9916 -- Check_Abstract_Overriding --
9917 -------------------------------
9919 procedure Check_Abstract_Overriding (T : Entity_Id) is
9920 Alias_Subp : Entity_Id;
9921 Elmt : Elmt_Id;
9922 Op_List : Elist_Id;
9923 Subp : Entity_Id;
9924 Type_Def : Node_Id;
9926 procedure Check_Pragma_Implemented (Subp : Entity_Id);
9927 -- Ada 2012 (AI05-0030): Subprogram Subp overrides an interface routine
9928 -- which has pragma Implemented already set. Check whether Subp's entity
9929 -- kind conforms to the implementation kind of the overridden routine.
9931 procedure Check_Pragma_Implemented
9932 (Subp : Entity_Id;
9933 Iface_Subp : Entity_Id);
9934 -- Ada 2012 (AI05-0030): Subprogram Subp overrides interface routine
9935 -- Iface_Subp and both entities have pragma Implemented already set on
9936 -- them. Check whether the two implementation kinds are conforming.
9938 procedure Inherit_Pragma_Implemented
9939 (Subp : Entity_Id;
9940 Iface_Subp : Entity_Id);
9941 -- Ada 2012 (AI05-0030): Interface primitive Subp overrides interface
9942 -- subprogram Iface_Subp which has been marked by pragma Implemented.
9943 -- Propagate the implementation kind of Iface_Subp to Subp.
9945 ------------------------------
9946 -- Check_Pragma_Implemented --
9947 ------------------------------
9949 procedure Check_Pragma_Implemented (Subp : Entity_Id) is
9950 Iface_Alias : constant Entity_Id := Interface_Alias (Subp);
9951 Impl_Kind : constant Name_Id := Implementation_Kind (Iface_Alias);
9952 Subp_Alias : constant Entity_Id := Alias (Subp);
9953 Contr_Typ : Entity_Id;
9954 Impl_Subp : Entity_Id;
9956 begin
9957 -- Subp must have an alias since it is a hidden entity used to link
9958 -- an interface subprogram to its overriding counterpart.
9960 pragma Assert (Present (Subp_Alias));
9962 -- Handle aliases to synchronized wrappers
9964 Impl_Subp := Subp_Alias;
9966 if Is_Primitive_Wrapper (Impl_Subp) then
9967 Impl_Subp := Wrapped_Entity (Impl_Subp);
9968 end if;
9970 -- Extract the type of the controlling formal
9972 Contr_Typ := Etype (First_Formal (Subp_Alias));
9974 if Is_Concurrent_Record_Type (Contr_Typ) then
9975 Contr_Typ := Corresponding_Concurrent_Type (Contr_Typ);
9976 end if;
9978 -- An interface subprogram whose implementation kind is By_Entry must
9979 -- be implemented by an entry.
9981 if Impl_Kind = Name_By_Entry
9982 and then Ekind (Impl_Subp) /= E_Entry
9983 then
9984 Error_Msg_Node_2 := Iface_Alias;
9985 Error_Msg_NE
9986 ("type & must implement abstract subprogram & with an entry",
9987 Subp_Alias, Contr_Typ);
9989 elsif Impl_Kind = Name_By_Protected_Procedure then
9991 -- An interface subprogram whose implementation kind is By_
9992 -- Protected_Procedure cannot be implemented by a primitive
9993 -- procedure of a task type.
9995 if Ekind (Contr_Typ) /= E_Protected_Type then
9996 Error_Msg_Node_2 := Contr_Typ;
9997 Error_Msg_NE
9998 ("interface subprogram & cannot be implemented by a " &
9999 "primitive procedure of task type &", Subp_Alias,
10000 Iface_Alias);
10002 -- An interface subprogram whose implementation kind is By_
10003 -- Protected_Procedure must be implemented by a procedure.
10005 elsif Ekind (Impl_Subp) /= E_Procedure then
10006 Error_Msg_Node_2 := Iface_Alias;
10007 Error_Msg_NE
10008 ("type & must implement abstract subprogram & with a " &
10009 "procedure", Subp_Alias, Contr_Typ);
10011 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
10012 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
10013 then
10014 Error_Msg_Name_1 := Impl_Kind;
10015 Error_Msg_N
10016 ("overriding operation& must have synchronization%",
10017 Subp_Alias);
10018 end if;
10020 -- If primitive has Optional synchronization, overriding operation
10021 -- must match if it has an explicit synchronization..
10023 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
10024 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
10025 then
10026 Error_Msg_Name_1 := Impl_Kind;
10027 Error_Msg_N
10028 ("overriding operation& must have syncrhonization%",
10029 Subp_Alias);
10030 end if;
10031 end Check_Pragma_Implemented;
10033 ------------------------------
10034 -- Check_Pragma_Implemented --
10035 ------------------------------
10037 procedure Check_Pragma_Implemented
10038 (Subp : Entity_Id;
10039 Iface_Subp : Entity_Id)
10041 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
10042 Subp_Kind : constant Name_Id := Implementation_Kind (Subp);
10044 begin
10045 -- Ada 2012 (AI05-0030): The implementation kinds of an overridden
10046 -- and overriding subprogram are different. In general this is an
10047 -- error except when the implementation kind of the overridden
10048 -- subprograms is By_Any or Optional.
10050 if Iface_Kind /= Subp_Kind
10051 and then Iface_Kind /= Name_By_Any
10052 and then Iface_Kind /= Name_Optional
10053 then
10054 if Iface_Kind = Name_By_Entry then
10055 Error_Msg_N
10056 ("incompatible implementation kind, overridden subprogram " &
10057 "is marked By_Entry", Subp);
10058 else
10059 Error_Msg_N
10060 ("incompatible implementation kind, overridden subprogram " &
10061 "is marked By_Protected_Procedure", Subp);
10062 end if;
10063 end if;
10064 end Check_Pragma_Implemented;
10066 --------------------------------
10067 -- Inherit_Pragma_Implemented --
10068 --------------------------------
10070 procedure Inherit_Pragma_Implemented
10071 (Subp : Entity_Id;
10072 Iface_Subp : Entity_Id)
10074 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
10075 Loc : constant Source_Ptr := Sloc (Subp);
10076 Impl_Prag : Node_Id;
10078 begin
10079 -- Since the implementation kind is stored as a representation item
10080 -- rather than a flag, create a pragma node.
10082 Impl_Prag :=
10083 Make_Pragma (Loc,
10084 Chars => Name_Implemented,
10085 Pragma_Argument_Associations => New_List (
10086 Make_Pragma_Argument_Association (Loc,
10087 Expression => New_Occurrence_Of (Subp, Loc)),
10089 Make_Pragma_Argument_Association (Loc,
10090 Expression => Make_Identifier (Loc, Iface_Kind))));
10092 -- The pragma doesn't need to be analyzed because it is internally
10093 -- built. It is safe to directly register it as a rep item since we
10094 -- are only interested in the characters of the implementation kind.
10096 Record_Rep_Item (Subp, Impl_Prag);
10097 end Inherit_Pragma_Implemented;
10099 -- Start of processing for Check_Abstract_Overriding
10101 begin
10102 Op_List := Primitive_Operations (T);
10104 -- Loop to check primitive operations
10106 Elmt := First_Elmt (Op_List);
10107 while Present (Elmt) loop
10108 Subp := Node (Elmt);
10109 Alias_Subp := Alias (Subp);
10111 -- Inherited subprograms are identified by the fact that they do not
10112 -- come from source, and the associated source location is the
10113 -- location of the first subtype of the derived type.
10115 -- Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
10116 -- subprograms that "require overriding".
10118 -- Special exception, do not complain about failure to override the
10119 -- stream routines _Input and _Output, as well as the primitive
10120 -- operations used in dispatching selects since we always provide
10121 -- automatic overridings for these subprograms.
10123 -- Also ignore this rule for convention CIL since .NET libraries
10124 -- do bizarre things with interfaces???
10126 -- The partial view of T may have been a private extension, for
10127 -- which inherited functions dispatching on result are abstract.
10128 -- If the full view is a null extension, there is no need for
10129 -- overriding in Ada 2005, but wrappers need to be built for them
10130 -- (see exp_ch3, Build_Controlling_Function_Wrappers).
10132 if Is_Null_Extension (T)
10133 and then Has_Controlling_Result (Subp)
10134 and then Ada_Version >= Ada_2005
10135 and then Present (Alias_Subp)
10136 and then not Comes_From_Source (Subp)
10137 and then not Is_Abstract_Subprogram (Alias_Subp)
10138 and then not Is_Access_Type (Etype (Subp))
10139 then
10140 null;
10142 -- Ada 2005 (AI-251): Internal entities of interfaces need no
10143 -- processing because this check is done with the aliased
10144 -- entity
10146 elsif Present (Interface_Alias (Subp)) then
10147 null;
10149 elsif (Is_Abstract_Subprogram (Subp)
10150 or else Requires_Overriding (Subp)
10151 or else
10152 (Has_Controlling_Result (Subp)
10153 and then Present (Alias_Subp)
10154 and then not Comes_From_Source (Subp)
10155 and then Sloc (Subp) = Sloc (First_Subtype (T))))
10156 and then not Is_TSS (Subp, TSS_Stream_Input)
10157 and then not Is_TSS (Subp, TSS_Stream_Output)
10158 and then not Is_Abstract_Type (T)
10159 and then Convention (T) /= Convention_CIL
10160 and then not Is_Predefined_Interface_Primitive (Subp)
10162 -- Ada 2005 (AI-251): Do not consider hidden entities associated
10163 -- with abstract interface types because the check will be done
10164 -- with the aliased entity (otherwise we generate a duplicated
10165 -- error message).
10167 and then not Present (Interface_Alias (Subp))
10168 then
10169 if Present (Alias_Subp) then
10171 -- Only perform the check for a derived subprogram when the
10172 -- type has an explicit record extension. This avoids incorrect
10173 -- flagging of abstract subprograms for the case of a type
10174 -- without an extension that is derived from a formal type
10175 -- with a tagged actual (can occur within a private part).
10177 -- Ada 2005 (AI-391): In the case of an inherited function with
10178 -- a controlling result of the type, the rule does not apply if
10179 -- the type is a null extension (unless the parent function
10180 -- itself is abstract, in which case the function must still be
10181 -- be overridden). The expander will generate an overriding
10182 -- wrapper function calling the parent subprogram (see
10183 -- Exp_Ch3.Make_Controlling_Wrapper_Functions).
10185 Type_Def := Type_Definition (Parent (T));
10187 if Nkind (Type_Def) = N_Derived_Type_Definition
10188 and then Present (Record_Extension_Part (Type_Def))
10189 and then
10190 (Ada_Version < Ada_2005
10191 or else not Is_Null_Extension (T)
10192 or else Ekind (Subp) = E_Procedure
10193 or else not Has_Controlling_Result (Subp)
10194 or else Is_Abstract_Subprogram (Alias_Subp)
10195 or else Requires_Overriding (Subp)
10196 or else Is_Access_Type (Etype (Subp)))
10197 then
10198 -- Avoid reporting error in case of abstract predefined
10199 -- primitive inherited from interface type because the
10200 -- body of internally generated predefined primitives
10201 -- of tagged types are generated later by Freeze_Type
10203 if Is_Interface (Root_Type (T))
10204 and then Is_Abstract_Subprogram (Subp)
10205 and then Is_Predefined_Dispatching_Operation (Subp)
10206 and then not Comes_From_Source (Ultimate_Alias (Subp))
10207 then
10208 null;
10210 -- A null extension is not obliged to override an inherited
10211 -- procedure subject to pragma Extensions_Visible with value
10212 -- False and at least one controlling OUT parameter
10213 -- (SPARK RM 6.1.7(6)).
10215 elsif Is_Null_Extension (T)
10216 and then Is_EVF_Procedure (Subp)
10217 then
10218 null;
10220 else
10221 Error_Msg_NE
10222 ("type must be declared abstract or & overridden",
10223 T, Subp);
10225 -- Traverse the whole chain of aliased subprograms to
10226 -- complete the error notification. This is especially
10227 -- useful for traceability of the chain of entities when
10228 -- the subprogram corresponds with an interface
10229 -- subprogram (which may be defined in another package).
10231 if Present (Alias_Subp) then
10232 declare
10233 E : Entity_Id;
10235 begin
10236 E := Subp;
10237 while Present (Alias (E)) loop
10239 -- Avoid reporting redundant errors on entities
10240 -- inherited from interfaces
10242 if Sloc (E) /= Sloc (T) then
10243 Error_Msg_Sloc := Sloc (E);
10244 Error_Msg_NE
10245 ("\& has been inherited #", T, Subp);
10246 end if;
10248 E := Alias (E);
10249 end loop;
10251 Error_Msg_Sloc := Sloc (E);
10253 -- AI05-0068: report if there is an overriding
10254 -- non-abstract subprogram that is invisible.
10256 if Is_Hidden (E)
10257 and then not Is_Abstract_Subprogram (E)
10258 then
10259 Error_Msg_NE
10260 ("\& subprogram# is not visible",
10261 T, Subp);
10263 -- Clarify the case where a non-null extension must
10264 -- override inherited procedure subject to pragma
10265 -- Extensions_Visible with value False and at least
10266 -- one controlling OUT param.
10268 elsif Is_EVF_Procedure (E) then
10269 Error_Msg_NE
10270 ("\& # is subject to Extensions_Visible False",
10271 T, Subp);
10273 else
10274 Error_Msg_NE
10275 ("\& has been inherited from subprogram #",
10276 T, Subp);
10277 end if;
10278 end;
10279 end if;
10280 end if;
10282 -- Ada 2005 (AI-345): Protected or task type implementing
10283 -- abstract interfaces.
10285 elsif Is_Concurrent_Record_Type (T)
10286 and then Present (Interfaces (T))
10287 then
10288 -- There is no need to check here RM 9.4(11.9/3) since we
10289 -- are processing the corresponding record type and the
10290 -- mode of the overriding subprograms was verified by
10291 -- Check_Conformance when the corresponding concurrent
10292 -- type declaration was analyzed.
10294 Error_Msg_NE
10295 ("interface subprogram & must be overridden", T, Subp);
10297 -- Examine primitive operations of synchronized type to find
10298 -- homonyms that have the wrong profile.
10300 declare
10301 Prim : Entity_Id;
10303 begin
10304 Prim := First_Entity (Corresponding_Concurrent_Type (T));
10305 while Present (Prim) loop
10306 if Chars (Prim) = Chars (Subp) then
10307 Error_Msg_NE
10308 ("profile is not type conformant with prefixed "
10309 & "view profile of inherited operation&",
10310 Prim, Subp);
10311 end if;
10313 Next_Entity (Prim);
10314 end loop;
10315 end;
10316 end if;
10318 else
10319 Error_Msg_Node_2 := T;
10320 Error_Msg_N
10321 ("abstract subprogram& not allowed for type&", Subp);
10323 -- Also post unconditional warning on the type (unconditional
10324 -- so that if there are more than one of these cases, we get
10325 -- them all, and not just the first one).
10327 Error_Msg_Node_2 := Subp;
10328 Error_Msg_N ("nonabstract type& has abstract subprogram&!", T);
10329 end if;
10331 -- A subprogram subject to pragma Extensions_Visible with value
10332 -- "True" cannot override a subprogram subject to the same pragma
10333 -- with value "False" (SPARK RM 6.1.7(5)).
10335 elsif Extensions_Visible_Status (Subp) = Extensions_Visible_True
10336 and then Present (Overridden_Operation (Subp))
10337 and then Extensions_Visible_Status (Overridden_Operation (Subp)) =
10338 Extensions_Visible_False
10339 then
10340 Error_Msg_Sloc := Sloc (Overridden_Operation (Subp));
10341 Error_Msg_N
10342 ("subprogram & with Extensions_Visible True cannot override "
10343 & "subprogram # with Extensions_Visible False", Subp);
10344 end if;
10346 -- Ada 2012 (AI05-0030): Perform checks related to pragma Implemented
10348 -- Subp is an expander-generated procedure which maps an interface
10349 -- alias to a protected wrapper. The interface alias is flagged by
10350 -- pragma Implemented. Ensure that Subp is a procedure when the
10351 -- implementation kind is By_Protected_Procedure or an entry when
10352 -- By_Entry.
10354 if Ada_Version >= Ada_2012
10355 and then Is_Hidden (Subp)
10356 and then Present (Interface_Alias (Subp))
10357 and then Has_Rep_Pragma (Interface_Alias (Subp), Name_Implemented)
10358 then
10359 Check_Pragma_Implemented (Subp);
10360 end if;
10362 -- Subp is an interface primitive which overrides another interface
10363 -- primitive marked with pragma Implemented.
10365 if Ada_Version >= Ada_2012
10366 and then Present (Overridden_Operation (Subp))
10367 and then Has_Rep_Pragma
10368 (Overridden_Operation (Subp), Name_Implemented)
10369 then
10370 -- If the overriding routine is also marked by Implemented, check
10371 -- that the two implementation kinds are conforming.
10373 if Has_Rep_Pragma (Subp, Name_Implemented) then
10374 Check_Pragma_Implemented
10375 (Subp => Subp,
10376 Iface_Subp => Overridden_Operation (Subp));
10378 -- Otherwise the overriding routine inherits the implementation
10379 -- kind from the overridden subprogram.
10381 else
10382 Inherit_Pragma_Implemented
10383 (Subp => Subp,
10384 Iface_Subp => Overridden_Operation (Subp));
10385 end if;
10386 end if;
10388 -- If the operation is a wrapper for a synchronized primitive, it
10389 -- may be called indirectly through a dispatching select. We assume
10390 -- that it will be referenced elsewhere indirectly, and suppress
10391 -- warnings about an unused entity.
10393 if Is_Primitive_Wrapper (Subp)
10394 and then Present (Wrapped_Entity (Subp))
10395 then
10396 Set_Referenced (Wrapped_Entity (Subp));
10397 end if;
10399 Next_Elmt (Elmt);
10400 end loop;
10401 end Check_Abstract_Overriding;
10403 ------------------------------------------------
10404 -- Check_Access_Discriminant_Requires_Limited --
10405 ------------------------------------------------
10407 procedure Check_Access_Discriminant_Requires_Limited
10408 (D : Node_Id;
10409 Loc : Node_Id)
10411 begin
10412 -- A discriminant_specification for an access discriminant shall appear
10413 -- only in the declaration for a task or protected type, or for a type
10414 -- with the reserved word 'limited' in its definition or in one of its
10415 -- ancestors (RM 3.7(10)).
10417 -- AI-0063: The proper condition is that type must be immutably limited,
10418 -- or else be a partial view.
10420 if Nkind (Discriminant_Type (D)) = N_Access_Definition then
10421 if Is_Limited_View (Current_Scope)
10422 or else
10423 (Nkind (Parent (Current_Scope)) = N_Private_Type_Declaration
10424 and then Limited_Present (Parent (Current_Scope)))
10425 then
10426 null;
10428 else
10429 Error_Msg_N
10430 ("access discriminants allowed only for limited types", Loc);
10431 end if;
10432 end if;
10433 end Check_Access_Discriminant_Requires_Limited;
10435 -----------------------------------
10436 -- Check_Aliased_Component_Types --
10437 -----------------------------------
10439 procedure Check_Aliased_Component_Types (T : Entity_Id) is
10440 C : Entity_Id;
10442 begin
10443 -- ??? Also need to check components of record extensions, but not
10444 -- components of protected types (which are always limited).
10446 -- Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
10447 -- types to be unconstrained. This is safe because it is illegal to
10448 -- create access subtypes to such types with explicit discriminant
10449 -- constraints.
10451 if not Is_Limited_Type (T) then
10452 if Ekind (T) = E_Record_Type then
10453 C := First_Component (T);
10454 while Present (C) loop
10455 if Is_Aliased (C)
10456 and then Has_Discriminants (Etype (C))
10457 and then not Is_Constrained (Etype (C))
10458 and then not In_Instance_Body
10459 and then Ada_Version < Ada_2005
10460 then
10461 Error_Msg_N
10462 ("aliased component must be constrained (RM 3.6(11))",
10464 end if;
10466 Next_Component (C);
10467 end loop;
10469 elsif Ekind (T) = E_Array_Type then
10470 if Has_Aliased_Components (T)
10471 and then Has_Discriminants (Component_Type (T))
10472 and then not Is_Constrained (Component_Type (T))
10473 and then not In_Instance_Body
10474 and then Ada_Version < Ada_2005
10475 then
10476 Error_Msg_N
10477 ("aliased component type must be constrained (RM 3.6(11))",
10479 end if;
10480 end if;
10481 end if;
10482 end Check_Aliased_Component_Types;
10484 ---------------------------------------
10485 -- Check_Anonymous_Access_Components --
10486 ---------------------------------------
10488 procedure Check_Anonymous_Access_Components
10489 (Typ_Decl : Node_Id;
10490 Typ : Entity_Id;
10491 Prev : Entity_Id;
10492 Comp_List : Node_Id)
10494 Loc : constant Source_Ptr := Sloc (Typ_Decl);
10495 Anon_Access : Entity_Id;
10496 Acc_Def : Node_Id;
10497 Comp : Node_Id;
10498 Comp_Def : Node_Id;
10499 Decl : Node_Id;
10500 Type_Def : Node_Id;
10502 procedure Build_Incomplete_Type_Declaration;
10503 -- If the record type contains components that include an access to the
10504 -- current record, then create an incomplete type declaration for the
10505 -- record, to be used as the designated type of the anonymous access.
10506 -- This is done only once, and only if there is no previous partial
10507 -- view of the type.
10509 function Designates_T (Subt : Node_Id) return Boolean;
10510 -- Check whether a node designates the enclosing record type, or 'Class
10511 -- of that type
10513 function Mentions_T (Acc_Def : Node_Id) return Boolean;
10514 -- Check whether an access definition includes a reference to
10515 -- the enclosing record type. The reference can be a subtype mark
10516 -- in the access definition itself, a 'Class attribute reference, or
10517 -- recursively a reference appearing in a parameter specification
10518 -- or result definition of an access_to_subprogram definition.
10520 --------------------------------------
10521 -- Build_Incomplete_Type_Declaration --
10522 --------------------------------------
10524 procedure Build_Incomplete_Type_Declaration is
10525 Decl : Node_Id;
10526 Inc_T : Entity_Id;
10527 H : Entity_Id;
10529 -- Is_Tagged indicates whether the type is tagged. It is tagged if
10530 -- it's "is new ... with record" or else "is tagged record ...".
10532 Is_Tagged : constant Boolean :=
10533 (Nkind (Type_Definition (Typ_Decl)) = N_Derived_Type_Definition
10534 and then
10535 Present (Record_Extension_Part (Type_Definition (Typ_Decl))))
10536 or else
10537 (Nkind (Type_Definition (Typ_Decl)) = N_Record_Definition
10538 and then Tagged_Present (Type_Definition (Typ_Decl)));
10540 begin
10541 -- If there is a previous partial view, no need to create a new one
10542 -- If the partial view, given by Prev, is incomplete, If Prev is
10543 -- a private declaration, full declaration is flagged accordingly.
10545 if Prev /= Typ then
10546 if Is_Tagged then
10547 Make_Class_Wide_Type (Prev);
10548 Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
10549 Set_Etype (Class_Wide_Type (Typ), Typ);
10550 end if;
10552 return;
10554 elsif Has_Private_Declaration (Typ) then
10556 -- If we refer to T'Class inside T, and T is the completion of a
10557 -- private type, then make sure the class-wide type exists.
10559 if Is_Tagged then
10560 Make_Class_Wide_Type (Typ);
10561 end if;
10563 return;
10565 -- If there was a previous anonymous access type, the incomplete
10566 -- type declaration will have been created already.
10568 elsif Present (Current_Entity (Typ))
10569 and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
10570 and then Full_View (Current_Entity (Typ)) = Typ
10571 then
10572 if Is_Tagged
10573 and then Comes_From_Source (Current_Entity (Typ))
10574 and then not Is_Tagged_Type (Current_Entity (Typ))
10575 then
10576 Make_Class_Wide_Type (Typ);
10577 Error_Msg_N
10578 ("incomplete view of tagged type should be declared tagged??",
10579 Parent (Current_Entity (Typ)));
10580 end if;
10581 return;
10583 else
10584 Inc_T := Make_Defining_Identifier (Loc, Chars (Typ));
10585 Decl := Make_Incomplete_Type_Declaration (Loc, Inc_T);
10587 -- Type has already been inserted into the current scope. Remove
10588 -- it, and add incomplete declaration for type, so that subsequent
10589 -- anonymous access types can use it. The entity is unchained from
10590 -- the homonym list and from immediate visibility. After analysis,
10591 -- the entity in the incomplete declaration becomes immediately
10592 -- visible in the record declaration that follows.
10594 H := Current_Entity (Typ);
10596 if H = Typ then
10597 Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
10598 else
10599 while Present (H)
10600 and then Homonym (H) /= Typ
10601 loop
10602 H := Homonym (Typ);
10603 end loop;
10605 Set_Homonym (H, Homonym (Typ));
10606 end if;
10608 Insert_Before (Typ_Decl, Decl);
10609 Analyze (Decl);
10610 Set_Full_View (Inc_T, Typ);
10612 if Is_Tagged then
10614 -- Create a common class-wide type for both views, and set the
10615 -- Etype of the class-wide type to the full view.
10617 Make_Class_Wide_Type (Inc_T);
10618 Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
10619 Set_Etype (Class_Wide_Type (Typ), Typ);
10620 end if;
10621 end if;
10622 end Build_Incomplete_Type_Declaration;
10624 ------------------
10625 -- Designates_T --
10626 ------------------
10628 function Designates_T (Subt : Node_Id) return Boolean is
10629 Type_Id : constant Name_Id := Chars (Typ);
10631 function Names_T (Nam : Node_Id) return Boolean;
10632 -- The record type has not been introduced in the current scope
10633 -- yet, so we must examine the name of the type itself, either
10634 -- an identifier T, or an expanded name of the form P.T, where
10635 -- P denotes the current scope.
10637 -------------
10638 -- Names_T --
10639 -------------
10641 function Names_T (Nam : Node_Id) return Boolean is
10642 begin
10643 if Nkind (Nam) = N_Identifier then
10644 return Chars (Nam) = Type_Id;
10646 elsif Nkind (Nam) = N_Selected_Component then
10647 if Chars (Selector_Name (Nam)) = Type_Id then
10648 if Nkind (Prefix (Nam)) = N_Identifier then
10649 return Chars (Prefix (Nam)) = Chars (Current_Scope);
10651 elsif Nkind (Prefix (Nam)) = N_Selected_Component then
10652 return Chars (Selector_Name (Prefix (Nam))) =
10653 Chars (Current_Scope);
10654 else
10655 return False;
10656 end if;
10658 else
10659 return False;
10660 end if;
10662 else
10663 return False;
10664 end if;
10665 end Names_T;
10667 -- Start of processing for Designates_T
10669 begin
10670 if Nkind (Subt) = N_Identifier then
10671 return Chars (Subt) = Type_Id;
10673 -- Reference can be through an expanded name which has not been
10674 -- analyzed yet, and which designates enclosing scopes.
10676 elsif Nkind (Subt) = N_Selected_Component then
10677 if Names_T (Subt) then
10678 return True;
10680 -- Otherwise it must denote an entity that is already visible.
10681 -- The access definition may name a subtype of the enclosing
10682 -- type, if there is a previous incomplete declaration for it.
10684 else
10685 Find_Selected_Component (Subt);
10686 return
10687 Is_Entity_Name (Subt)
10688 and then Scope (Entity (Subt)) = Current_Scope
10689 and then
10690 (Chars (Base_Type (Entity (Subt))) = Type_Id
10691 or else
10692 (Is_Class_Wide_Type (Entity (Subt))
10693 and then
10694 Chars (Etype (Base_Type (Entity (Subt)))) =
10695 Type_Id));
10696 end if;
10698 -- A reference to the current type may appear as the prefix of
10699 -- a 'Class attribute.
10701 elsif Nkind (Subt) = N_Attribute_Reference
10702 and then Attribute_Name (Subt) = Name_Class
10703 then
10704 return Names_T (Prefix (Subt));
10706 else
10707 return False;
10708 end if;
10709 end Designates_T;
10711 ----------------
10712 -- Mentions_T --
10713 ----------------
10715 function Mentions_T (Acc_Def : Node_Id) return Boolean is
10716 Param_Spec : Node_Id;
10718 Acc_Subprg : constant Node_Id :=
10719 Access_To_Subprogram_Definition (Acc_Def);
10721 begin
10722 if No (Acc_Subprg) then
10723 return Designates_T (Subtype_Mark (Acc_Def));
10724 end if;
10726 -- Component is an access_to_subprogram: examine its formals,
10727 -- and result definition in the case of an access_to_function.
10729 Param_Spec := First (Parameter_Specifications (Acc_Subprg));
10730 while Present (Param_Spec) loop
10731 if Nkind (Parameter_Type (Param_Spec)) = N_Access_Definition
10732 and then Mentions_T (Parameter_Type (Param_Spec))
10733 then
10734 return True;
10736 elsif Designates_T (Parameter_Type (Param_Spec)) then
10737 return True;
10738 end if;
10740 Next (Param_Spec);
10741 end loop;
10743 if Nkind (Acc_Subprg) = N_Access_Function_Definition then
10744 if Nkind (Result_Definition (Acc_Subprg)) =
10745 N_Access_Definition
10746 then
10747 return Mentions_T (Result_Definition (Acc_Subprg));
10748 else
10749 return Designates_T (Result_Definition (Acc_Subprg));
10750 end if;
10751 end if;
10753 return False;
10754 end Mentions_T;
10756 -- Start of processing for Check_Anonymous_Access_Components
10758 begin
10759 if No (Comp_List) then
10760 return;
10761 end if;
10763 Comp := First (Component_Items (Comp_List));
10764 while Present (Comp) loop
10765 if Nkind (Comp) = N_Component_Declaration
10766 and then Present
10767 (Access_Definition (Component_Definition (Comp)))
10768 and then
10769 Mentions_T (Access_Definition (Component_Definition (Comp)))
10770 then
10771 Comp_Def := Component_Definition (Comp);
10772 Acc_Def :=
10773 Access_To_Subprogram_Definition (Access_Definition (Comp_Def));
10775 Build_Incomplete_Type_Declaration;
10776 Anon_Access := Make_Temporary (Loc, 'S');
10778 -- Create a declaration for the anonymous access type: either
10779 -- an access_to_object or an access_to_subprogram.
10781 if Present (Acc_Def) then
10782 if Nkind (Acc_Def) = N_Access_Function_Definition then
10783 Type_Def :=
10784 Make_Access_Function_Definition (Loc,
10785 Parameter_Specifications =>
10786 Parameter_Specifications (Acc_Def),
10787 Result_Definition => Result_Definition (Acc_Def));
10788 else
10789 Type_Def :=
10790 Make_Access_Procedure_Definition (Loc,
10791 Parameter_Specifications =>
10792 Parameter_Specifications (Acc_Def));
10793 end if;
10795 else
10796 Type_Def :=
10797 Make_Access_To_Object_Definition (Loc,
10798 Subtype_Indication =>
10799 Relocate_Node
10800 (Subtype_Mark (Access_Definition (Comp_Def))));
10802 Set_Constant_Present
10803 (Type_Def, Constant_Present (Access_Definition (Comp_Def)));
10804 Set_All_Present
10805 (Type_Def, All_Present (Access_Definition (Comp_Def)));
10806 end if;
10808 Set_Null_Exclusion_Present
10809 (Type_Def,
10810 Null_Exclusion_Present (Access_Definition (Comp_Def)));
10812 Decl :=
10813 Make_Full_Type_Declaration (Loc,
10814 Defining_Identifier => Anon_Access,
10815 Type_Definition => Type_Def);
10817 Insert_Before (Typ_Decl, Decl);
10818 Analyze (Decl);
10820 -- If an access to subprogram, create the extra formals
10822 if Present (Acc_Def) then
10823 Create_Extra_Formals (Designated_Type (Anon_Access));
10825 -- If an access to object, preserve entity of designated type,
10826 -- for ASIS use, before rewriting the component definition.
10828 else
10829 declare
10830 Desig : Entity_Id;
10832 begin
10833 Desig := Entity (Subtype_Indication (Type_Def));
10835 -- If the access definition is to the current record,
10836 -- the visible entity at this point is an incomplete
10837 -- type. Retrieve the full view to simplify ASIS queries
10839 if Ekind (Desig) = E_Incomplete_Type then
10840 Desig := Full_View (Desig);
10841 end if;
10843 Set_Entity
10844 (Subtype_Mark (Access_Definition (Comp_Def)), Desig);
10845 end;
10846 end if;
10848 Rewrite (Comp_Def,
10849 Make_Component_Definition (Loc,
10850 Subtype_Indication =>
10851 New_Occurrence_Of (Anon_Access, Loc)));
10853 if Ekind (Designated_Type (Anon_Access)) = E_Subprogram_Type then
10854 Set_Ekind (Anon_Access, E_Anonymous_Access_Subprogram_Type);
10855 else
10856 Set_Ekind (Anon_Access, E_Anonymous_Access_Type);
10857 end if;
10859 Set_Is_Local_Anonymous_Access (Anon_Access);
10860 end if;
10862 Next (Comp);
10863 end loop;
10865 if Present (Variant_Part (Comp_List)) then
10866 declare
10867 V : Node_Id;
10868 begin
10869 V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
10870 while Present (V) loop
10871 Check_Anonymous_Access_Components
10872 (Typ_Decl, Typ, Prev, Component_List (V));
10873 Next_Non_Pragma (V);
10874 end loop;
10875 end;
10876 end if;
10877 end Check_Anonymous_Access_Components;
10879 ----------------------
10880 -- Check_Completion --
10881 ----------------------
10883 procedure Check_Completion (Body_Id : Node_Id := Empty) is
10884 E : Entity_Id;
10886 procedure Post_Error;
10887 -- Post error message for lack of completion for entity E
10889 ----------------
10890 -- Post_Error --
10891 ----------------
10893 procedure Post_Error is
10894 procedure Missing_Body;
10895 -- Output missing body message
10897 ------------------
10898 -- Missing_Body --
10899 ------------------
10901 procedure Missing_Body is
10902 begin
10903 -- Spec is in same unit, so we can post on spec
10905 if In_Same_Source_Unit (Body_Id, E) then
10906 Error_Msg_N ("missing body for &", E);
10908 -- Spec is in a separate unit, so we have to post on the body
10910 else
10911 Error_Msg_NE ("missing body for & declared#!", Body_Id, E);
10912 end if;
10913 end Missing_Body;
10915 -- Start of processing for Post_Error
10917 begin
10918 if not Comes_From_Source (E) then
10919 if Ekind_In (E, E_Task_Type, E_Protected_Type) then
10921 -- It may be an anonymous protected type created for a
10922 -- single variable. Post error on variable, if present.
10924 declare
10925 Var : Entity_Id;
10927 begin
10928 Var := First_Entity (Current_Scope);
10929 while Present (Var) loop
10930 exit when Etype (Var) = E
10931 and then Comes_From_Source (Var);
10933 Next_Entity (Var);
10934 end loop;
10936 if Present (Var) then
10937 E := Var;
10938 end if;
10939 end;
10940 end if;
10941 end if;
10943 -- If a generated entity has no completion, then either previous
10944 -- semantic errors have disabled the expansion phase, or else we had
10945 -- missing subunits, or else we are compiling without expansion,
10946 -- or else something is very wrong.
10948 if not Comes_From_Source (E) then
10949 pragma Assert
10950 (Serious_Errors_Detected > 0
10951 or else Configurable_Run_Time_Violations > 0
10952 or else Subunits_Missing
10953 or else not Expander_Active);
10954 return;
10956 -- Here for source entity
10958 else
10959 -- Here if no body to post the error message, so we post the error
10960 -- on the declaration that has no completion. This is not really
10961 -- the right place to post it, think about this later ???
10963 if No (Body_Id) then
10964 if Is_Type (E) then
10965 Error_Msg_NE
10966 ("missing full declaration for }", Parent (E), E);
10967 else
10968 Error_Msg_NE ("missing body for &", Parent (E), E);
10969 end if;
10971 -- Package body has no completion for a declaration that appears
10972 -- in the corresponding spec. Post error on the body, with a
10973 -- reference to the non-completed declaration.
10975 else
10976 Error_Msg_Sloc := Sloc (E);
10978 if Is_Type (E) then
10979 Error_Msg_NE ("missing full declaration for }!", Body_Id, E);
10981 elsif Is_Overloadable (E)
10982 and then Current_Entity_In_Scope (E) /= E
10983 then
10984 -- It may be that the completion is mistyped and appears as
10985 -- a distinct overloading of the entity.
10987 declare
10988 Candidate : constant Entity_Id :=
10989 Current_Entity_In_Scope (E);
10990 Decl : constant Node_Id :=
10991 Unit_Declaration_Node (Candidate);
10993 begin
10994 if Is_Overloadable (Candidate)
10995 and then Ekind (Candidate) = Ekind (E)
10996 and then Nkind (Decl) = N_Subprogram_Body
10997 and then Acts_As_Spec (Decl)
10998 then
10999 Check_Type_Conformant (Candidate, E);
11001 else
11002 Missing_Body;
11003 end if;
11004 end;
11006 else
11007 Missing_Body;
11008 end if;
11009 end if;
11010 end if;
11011 end Post_Error;
11013 -- Local variables
11015 Pack_Id : constant Entity_Id := Current_Scope;
11017 -- Start of processing for Check_Completion
11019 begin
11020 E := First_Entity (Pack_Id);
11021 while Present (E) loop
11022 if Is_Intrinsic_Subprogram (E) then
11023 null;
11025 -- The following situation requires special handling: a child unit
11026 -- that appears in the context clause of the body of its parent:
11028 -- procedure Parent.Child (...);
11030 -- with Parent.Child;
11031 -- package body Parent is
11033 -- Here Parent.Child appears as a local entity, but should not be
11034 -- flagged as requiring completion, because it is a compilation
11035 -- unit.
11037 -- Ignore missing completion for a subprogram that does not come from
11038 -- source (including the _Call primitive operation of RAS types,
11039 -- which has to have the flag Comes_From_Source for other purposes):
11040 -- we assume that the expander will provide the missing completion.
11041 -- In case of previous errors, other expansion actions that provide
11042 -- bodies for null procedures with not be invoked, so inhibit message
11043 -- in those cases.
11045 -- Note that E_Operator is not in the list that follows, because
11046 -- this kind is reserved for predefined operators, that are
11047 -- intrinsic and do not need completion.
11049 elsif Ekind_In (E, E_Function,
11050 E_Procedure,
11051 E_Generic_Function,
11052 E_Generic_Procedure)
11053 then
11054 if Has_Completion (E) then
11055 null;
11057 elsif Is_Subprogram (E) and then Is_Abstract_Subprogram (E) then
11058 null;
11060 elsif Is_Subprogram (E)
11061 and then (not Comes_From_Source (E)
11062 or else Chars (E) = Name_uCall)
11063 then
11064 null;
11066 elsif
11067 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
11068 then
11069 null;
11071 elsif Nkind (Parent (E)) = N_Procedure_Specification
11072 and then Null_Present (Parent (E))
11073 and then Serious_Errors_Detected > 0
11074 then
11075 null;
11077 else
11078 Post_Error;
11079 end if;
11081 elsif Is_Entry (E) then
11082 if not Has_Completion (E) and then
11083 (Ekind (Scope (E)) = E_Protected_Object
11084 or else Ekind (Scope (E)) = E_Protected_Type)
11085 then
11086 Post_Error;
11087 end if;
11089 elsif Is_Package_Or_Generic_Package (E) then
11090 if Unit_Requires_Body (E) then
11091 if not Has_Completion (E)
11092 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
11093 N_Compilation_Unit
11094 then
11095 Post_Error;
11096 end if;
11098 elsif not Is_Child_Unit (E) then
11099 May_Need_Implicit_Body (E);
11100 end if;
11102 -- A formal incomplete type (Ada 2012) does not require a completion;
11103 -- other incomplete type declarations do.
11105 elsif Ekind (E) = E_Incomplete_Type
11106 and then No (Underlying_Type (E))
11107 and then not Is_Generic_Type (E)
11108 then
11109 Post_Error;
11111 elsif Ekind_In (E, E_Task_Type, E_Protected_Type)
11112 and then not Has_Completion (E)
11113 then
11114 Post_Error;
11116 -- A single task declared in the current scope is a constant, verify
11117 -- that the body of its anonymous type is in the same scope. If the
11118 -- task is defined elsewhere, this may be a renaming declaration for
11119 -- which no completion is needed.
11121 elsif Ekind (E) = E_Constant
11122 and then Ekind (Etype (E)) = E_Task_Type
11123 and then not Has_Completion (Etype (E))
11124 and then Scope (Etype (E)) = Current_Scope
11125 then
11126 Post_Error;
11128 elsif Ekind (E) = E_Protected_Object
11129 and then not Has_Completion (Etype (E))
11130 then
11131 Post_Error;
11133 elsif Ekind (E) = E_Record_Type then
11134 if Is_Tagged_Type (E) then
11135 Check_Abstract_Overriding (E);
11136 Check_Conventions (E);
11137 end if;
11139 Check_Aliased_Component_Types (E);
11141 elsif Ekind (E) = E_Array_Type then
11142 Check_Aliased_Component_Types (E);
11144 end if;
11146 Next_Entity (E);
11147 end loop;
11148 end Check_Completion;
11150 ------------------------------------
11151 -- Check_CPP_Type_Has_No_Defaults --
11152 ------------------------------------
11154 procedure Check_CPP_Type_Has_No_Defaults (T : Entity_Id) is
11155 Tdef : constant Node_Id := Type_Definition (Declaration_Node (T));
11156 Clist : Node_Id;
11157 Comp : Node_Id;
11159 begin
11160 -- Obtain the component list
11162 if Nkind (Tdef) = N_Record_Definition then
11163 Clist := Component_List (Tdef);
11164 else pragma Assert (Nkind (Tdef) = N_Derived_Type_Definition);
11165 Clist := Component_List (Record_Extension_Part (Tdef));
11166 end if;
11168 -- Check all components to ensure no default expressions
11170 if Present (Clist) then
11171 Comp := First (Component_Items (Clist));
11172 while Present (Comp) loop
11173 if Present (Expression (Comp)) then
11174 Error_Msg_N
11175 ("component of imported 'C'P'P type cannot have "
11176 & "default expression", Expression (Comp));
11177 end if;
11179 Next (Comp);
11180 end loop;
11181 end if;
11182 end Check_CPP_Type_Has_No_Defaults;
11184 ----------------------------
11185 -- Check_Delta_Expression --
11186 ----------------------------
11188 procedure Check_Delta_Expression (E : Node_Id) is
11189 begin
11190 if not (Is_Real_Type (Etype (E))) then
11191 Wrong_Type (E, Any_Real);
11193 elsif not Is_OK_Static_Expression (E) then
11194 Flag_Non_Static_Expr
11195 ("non-static expression used for delta value!", E);
11197 elsif not UR_Is_Positive (Expr_Value_R (E)) then
11198 Error_Msg_N ("delta expression must be positive", E);
11200 else
11201 return;
11202 end if;
11204 -- If any of above errors occurred, then replace the incorrect
11205 -- expression by the real 0.1, which should prevent further errors.
11207 Rewrite (E,
11208 Make_Real_Literal (Sloc (E), Ureal_Tenth));
11209 Analyze_And_Resolve (E, Standard_Float);
11210 end Check_Delta_Expression;
11212 -----------------------------
11213 -- Check_Digits_Expression --
11214 -----------------------------
11216 procedure Check_Digits_Expression (E : Node_Id) is
11217 begin
11218 if not (Is_Integer_Type (Etype (E))) then
11219 Wrong_Type (E, Any_Integer);
11221 elsif not Is_OK_Static_Expression (E) then
11222 Flag_Non_Static_Expr
11223 ("non-static expression used for digits value!", E);
11225 elsif Expr_Value (E) <= 0 then
11226 Error_Msg_N ("digits value must be greater than zero", E);
11228 else
11229 return;
11230 end if;
11232 -- If any of above errors occurred, then replace the incorrect
11233 -- expression by the integer 1, which should prevent further errors.
11235 Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
11236 Analyze_And_Resolve (E, Standard_Integer);
11238 end Check_Digits_Expression;
11240 --------------------------
11241 -- Check_Initialization --
11242 --------------------------
11244 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
11245 begin
11246 -- Special processing for limited types
11248 if Is_Limited_Type (T)
11249 and then not In_Instance
11250 and then not In_Inlined_Body
11251 then
11252 if not OK_For_Limited_Init (T, Exp) then
11254 -- In GNAT mode, this is just a warning, to allow it to be evilly
11255 -- turned off. Otherwise it is a real error.
11257 if GNAT_Mode then
11258 Error_Msg_N
11259 ("??cannot initialize entities of limited type!", Exp);
11261 elsif Ada_Version < Ada_2005 then
11263 -- The side effect removal machinery may generate illegal Ada
11264 -- code to avoid the usage of access types and 'reference in
11265 -- SPARK mode. Since this is legal code with respect to theorem
11266 -- proving, do not emit the error.
11268 if GNATprove_Mode
11269 and then Nkind (Exp) = N_Function_Call
11270 and then Nkind (Parent (Exp)) = N_Object_Declaration
11271 and then not Comes_From_Source
11272 (Defining_Identifier (Parent (Exp)))
11273 then
11274 null;
11276 else
11277 Error_Msg_N
11278 ("cannot initialize entities of limited type", Exp);
11279 Explain_Limited_Type (T, Exp);
11280 end if;
11282 else
11283 -- Specialize error message according to kind of illegal
11284 -- initial expression.
11286 if Nkind (Exp) = N_Type_Conversion
11287 and then Nkind (Expression (Exp)) = N_Function_Call
11288 then
11289 Error_Msg_N
11290 ("illegal context for call"
11291 & " to function with limited result", Exp);
11293 else
11294 Error_Msg_N
11295 ("initialization of limited object requires aggregate "
11296 & "or function call", Exp);
11297 end if;
11298 end if;
11299 end if;
11300 end if;
11302 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag gets
11303 -- set unless we can be sure that no range check is required.
11305 if (GNATprove_Mode or not Expander_Active)
11306 and then Is_Scalar_Type (T)
11307 and then not Is_In_Range (Exp, T, Assume_Valid => True)
11308 then
11309 Set_Do_Range_Check (Exp);
11310 end if;
11311 end Check_Initialization;
11313 ----------------------
11314 -- Check_Interfaces --
11315 ----------------------
11317 procedure Check_Interfaces (N : Node_Id; Def : Node_Id) is
11318 Parent_Type : constant Entity_Id := Etype (Defining_Identifier (N));
11320 Iface : Node_Id;
11321 Iface_Def : Node_Id;
11322 Iface_Typ : Entity_Id;
11323 Parent_Node : Node_Id;
11325 Is_Task : Boolean := False;
11326 -- Set True if parent type or any progenitor is a task interface
11328 Is_Protected : Boolean := False;
11329 -- Set True if parent type or any progenitor is a protected interface
11331 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
11332 -- Check that a progenitor is compatible with declaration. If an error
11333 -- message is output, it is posted on Error_Node.
11335 ------------------
11336 -- Check_Ifaces --
11337 ------------------
11339 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
11340 Iface_Id : constant Entity_Id :=
11341 Defining_Identifier (Parent (Iface_Def));
11342 Type_Def : Node_Id;
11344 begin
11345 if Nkind (N) = N_Private_Extension_Declaration then
11346 Type_Def := N;
11347 else
11348 Type_Def := Type_Definition (N);
11349 end if;
11351 if Is_Task_Interface (Iface_Id) then
11352 Is_Task := True;
11354 elsif Is_Protected_Interface (Iface_Id) then
11355 Is_Protected := True;
11356 end if;
11358 if Is_Synchronized_Interface (Iface_Id) then
11360 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
11361 -- extension derived from a synchronized interface must explicitly
11362 -- be declared synchronized, because the full view will be a
11363 -- synchronized type.
11365 if Nkind (N) = N_Private_Extension_Declaration then
11366 if not Synchronized_Present (N) then
11367 Error_Msg_NE
11368 ("private extension of& must be explicitly synchronized",
11369 N, Iface_Id);
11370 end if;
11372 -- However, by 3.9.4(16/2), a full type that is a record extension
11373 -- is never allowed to derive from a synchronized interface (note
11374 -- that interfaces must be excluded from this check, because those
11375 -- are represented by derived type definitions in some cases).
11377 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition
11378 and then not Interface_Present (Type_Definition (N))
11379 then
11380 Error_Msg_N ("record extension cannot derive from synchronized "
11381 & "interface", Error_Node);
11382 end if;
11383 end if;
11385 -- Check that the characteristics of the progenitor are compatible
11386 -- with the explicit qualifier in the declaration.
11387 -- The check only applies to qualifiers that come from source.
11388 -- Limited_Present also appears in the declaration of corresponding
11389 -- records, and the check does not apply to them.
11391 if Limited_Present (Type_Def)
11392 and then not
11393 Is_Concurrent_Record_Type (Defining_Identifier (N))
11394 then
11395 if Is_Limited_Interface (Parent_Type)
11396 and then not Is_Limited_Interface (Iface_Id)
11397 then
11398 Error_Msg_NE
11399 ("progenitor & must be limited interface",
11400 Error_Node, Iface_Id);
11402 elsif
11403 (Task_Present (Iface_Def)
11404 or else Protected_Present (Iface_Def)
11405 or else Synchronized_Present (Iface_Def))
11406 and then Nkind (N) /= N_Private_Extension_Declaration
11407 and then not Error_Posted (N)
11408 then
11409 Error_Msg_NE
11410 ("progenitor & must be limited interface",
11411 Error_Node, Iface_Id);
11412 end if;
11414 -- Protected interfaces can only inherit from limited, synchronized
11415 -- or protected interfaces.
11417 elsif Nkind (N) = N_Full_Type_Declaration
11418 and then Protected_Present (Type_Def)
11419 then
11420 if Limited_Present (Iface_Def)
11421 or else Synchronized_Present (Iface_Def)
11422 or else Protected_Present (Iface_Def)
11423 then
11424 null;
11426 elsif Task_Present (Iface_Def) then
11427 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
11428 & "from task interface", Error_Node);
11430 else
11431 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
11432 & "from non-limited interface", Error_Node);
11433 end if;
11435 -- Ada 2005 (AI-345): Synchronized interfaces can only inherit from
11436 -- limited and synchronized.
11438 elsif Synchronized_Present (Type_Def) then
11439 if Limited_Present (Iface_Def)
11440 or else Synchronized_Present (Iface_Def)
11441 then
11442 null;
11444 elsif Protected_Present (Iface_Def)
11445 and then Nkind (N) /= N_Private_Extension_Declaration
11446 then
11447 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11448 & "from protected interface", Error_Node);
11450 elsif Task_Present (Iface_Def)
11451 and then Nkind (N) /= N_Private_Extension_Declaration
11452 then
11453 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11454 & "from task interface", Error_Node);
11456 elsif not Is_Limited_Interface (Iface_Id) then
11457 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11458 & "from non-limited interface", Error_Node);
11459 end if;
11461 -- Ada 2005 (AI-345): Task interfaces can only inherit from limited,
11462 -- synchronized or task interfaces.
11464 elsif Nkind (N) = N_Full_Type_Declaration
11465 and then Task_Present (Type_Def)
11466 then
11467 if Limited_Present (Iface_Def)
11468 or else Synchronized_Present (Iface_Def)
11469 or else Task_Present (Iface_Def)
11470 then
11471 null;
11473 elsif Protected_Present (Iface_Def) then
11474 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
11475 & "protected interface", Error_Node);
11477 else
11478 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
11479 & "non-limited interface", Error_Node);
11480 end if;
11481 end if;
11482 end Check_Ifaces;
11484 -- Start of processing for Check_Interfaces
11486 begin
11487 if Is_Interface (Parent_Type) then
11488 if Is_Task_Interface (Parent_Type) then
11489 Is_Task := True;
11491 elsif Is_Protected_Interface (Parent_Type) then
11492 Is_Protected := True;
11493 end if;
11494 end if;
11496 if Nkind (N) = N_Private_Extension_Declaration then
11498 -- Check that progenitors are compatible with declaration
11500 Iface := First (Interface_List (Def));
11501 while Present (Iface) loop
11502 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
11504 Parent_Node := Parent (Base_Type (Iface_Typ));
11505 Iface_Def := Type_Definition (Parent_Node);
11507 if not Is_Interface (Iface_Typ) then
11508 Diagnose_Interface (Iface, Iface_Typ);
11509 else
11510 Check_Ifaces (Iface_Def, Iface);
11511 end if;
11513 Next (Iface);
11514 end loop;
11516 if Is_Task and Is_Protected then
11517 Error_Msg_N
11518 ("type cannot derive from task and protected interface", N);
11519 end if;
11521 return;
11522 end if;
11524 -- Full type declaration of derived type.
11525 -- Check compatibility with parent if it is interface type
11527 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
11528 and then Is_Interface (Parent_Type)
11529 then
11530 Parent_Node := Parent (Parent_Type);
11532 -- More detailed checks for interface varieties
11534 Check_Ifaces
11535 (Iface_Def => Type_Definition (Parent_Node),
11536 Error_Node => Subtype_Indication (Type_Definition (N)));
11537 end if;
11539 Iface := First (Interface_List (Def));
11540 while Present (Iface) loop
11541 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
11543 Parent_Node := Parent (Base_Type (Iface_Typ));
11544 Iface_Def := Type_Definition (Parent_Node);
11546 if not Is_Interface (Iface_Typ) then
11547 Diagnose_Interface (Iface, Iface_Typ);
11549 else
11550 -- "The declaration of a specific descendant of an interface
11551 -- type freezes the interface type" RM 13.14
11553 Freeze_Before (N, Iface_Typ);
11554 Check_Ifaces (Iface_Def, Error_Node => Iface);
11555 end if;
11557 Next (Iface);
11558 end loop;
11560 if Is_Task and Is_Protected then
11561 Error_Msg_N
11562 ("type cannot derive from task and protected interface", N);
11563 end if;
11564 end Check_Interfaces;
11566 ------------------------------------
11567 -- Check_Or_Process_Discriminants --
11568 ------------------------------------
11570 -- If an incomplete or private type declaration was already given for the
11571 -- type, the discriminants may have already been processed if they were
11572 -- present on the incomplete declaration. In this case a full conformance
11573 -- check has been performed in Find_Type_Name, and we then recheck here
11574 -- some properties that can't be checked on the partial view alone.
11575 -- Otherwise we call Process_Discriminants.
11577 procedure Check_Or_Process_Discriminants
11578 (N : Node_Id;
11579 T : Entity_Id;
11580 Prev : Entity_Id := Empty)
11582 begin
11583 if Has_Discriminants (T) then
11585 -- Discriminants are already set on T if they were already present
11586 -- on the partial view. Make them visible to component declarations.
11588 declare
11589 D : Entity_Id;
11590 -- Discriminant on T (full view) referencing expr on partial view
11592 Prev_D : Entity_Id;
11593 -- Entity of corresponding discriminant on partial view
11595 New_D : Node_Id;
11596 -- Discriminant specification for full view, expression is
11597 -- the syntactic copy on full view (which has been checked for
11598 -- conformance with partial view), only used here to post error
11599 -- message.
11601 begin
11602 D := First_Discriminant (T);
11603 New_D := First (Discriminant_Specifications (N));
11604 while Present (D) loop
11605 Prev_D := Current_Entity (D);
11606 Set_Current_Entity (D);
11607 Set_Is_Immediately_Visible (D);
11608 Set_Homonym (D, Prev_D);
11610 -- Handle the case where there is an untagged partial view and
11611 -- the full view is tagged: must disallow discriminants with
11612 -- defaults, unless compiling for Ada 2012, which allows a
11613 -- limited tagged type to have defaulted discriminants (see
11614 -- AI05-0214). However, suppress error here if it was already
11615 -- reported on the default expression of the partial view.
11617 if Is_Tagged_Type (T)
11618 and then Present (Expression (Parent (D)))
11619 and then (not Is_Limited_Type (Current_Scope)
11620 or else Ada_Version < Ada_2012)
11621 and then not Error_Posted (Expression (Parent (D)))
11622 then
11623 if Ada_Version >= Ada_2012 then
11624 Error_Msg_N
11625 ("discriminants of nonlimited tagged type cannot have "
11626 & "defaults",
11627 Expression (New_D));
11628 else
11629 Error_Msg_N
11630 ("discriminants of tagged type cannot have defaults",
11631 Expression (New_D));
11632 end if;
11633 end if;
11635 -- Ada 2005 (AI-230): Access discriminant allowed in
11636 -- non-limited record types.
11638 if Ada_Version < Ada_2005 then
11640 -- This restriction gets applied to the full type here. It
11641 -- has already been applied earlier to the partial view.
11643 Check_Access_Discriminant_Requires_Limited (Parent (D), N);
11644 end if;
11646 Next_Discriminant (D);
11647 Next (New_D);
11648 end loop;
11649 end;
11651 elsif Present (Discriminant_Specifications (N)) then
11652 Process_Discriminants (N, Prev);
11653 end if;
11654 end Check_Or_Process_Discriminants;
11656 ----------------------
11657 -- Check_Real_Bound --
11658 ----------------------
11660 procedure Check_Real_Bound (Bound : Node_Id) is
11661 begin
11662 if not Is_Real_Type (Etype (Bound)) then
11663 Error_Msg_N
11664 ("bound in real type definition must be of real type", Bound);
11666 elsif not Is_OK_Static_Expression (Bound) then
11667 Flag_Non_Static_Expr
11668 ("non-static expression used for real type bound!", Bound);
11670 else
11671 return;
11672 end if;
11674 Rewrite
11675 (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
11676 Analyze (Bound);
11677 Resolve (Bound, Standard_Float);
11678 end Check_Real_Bound;
11680 ------------------------------
11681 -- Complete_Private_Subtype --
11682 ------------------------------
11684 procedure Complete_Private_Subtype
11685 (Priv : Entity_Id;
11686 Full : Entity_Id;
11687 Full_Base : Entity_Id;
11688 Related_Nod : Node_Id)
11690 Save_Next_Entity : Entity_Id;
11691 Save_Homonym : Entity_Id;
11693 begin
11694 -- Set semantic attributes for (implicit) private subtype completion.
11695 -- If the full type has no discriminants, then it is a copy of the
11696 -- full view of the base. Otherwise, it is a subtype of the base with
11697 -- a possible discriminant constraint. Save and restore the original
11698 -- Next_Entity field of full to ensure that the calls to Copy_Node do
11699 -- not corrupt the entity chain.
11701 -- Note that the type of the full view is the same entity as the type
11702 -- of the partial view. In this fashion, the subtype has access to the
11703 -- correct view of the parent.
11705 Save_Next_Entity := Next_Entity (Full);
11706 Save_Homonym := Homonym (Priv);
11708 case Ekind (Full_Base) is
11709 when E_Record_Type |
11710 E_Record_Subtype |
11711 Class_Wide_Kind |
11712 Private_Kind |
11713 Task_Kind |
11714 Protected_Kind =>
11715 Copy_Node (Priv, Full);
11717 Set_Has_Discriminants
11718 (Full, Has_Discriminants (Full_Base));
11719 Set_Has_Unknown_Discriminants
11720 (Full, Has_Unknown_Discriminants (Full_Base));
11721 Set_First_Entity (Full, First_Entity (Full_Base));
11722 Set_Last_Entity (Full, Last_Entity (Full_Base));
11724 -- If the underlying base type is constrained, we know that the
11725 -- full view of the subtype is constrained as well (the converse
11726 -- is not necessarily true).
11728 if Is_Constrained (Full_Base) then
11729 Set_Is_Constrained (Full);
11730 end if;
11732 when others =>
11733 Copy_Node (Full_Base, Full);
11735 Set_Chars (Full, Chars (Priv));
11736 Conditional_Delay (Full, Priv);
11737 Set_Sloc (Full, Sloc (Priv));
11738 end case;
11740 Set_Next_Entity (Full, Save_Next_Entity);
11741 Set_Homonym (Full, Save_Homonym);
11742 Set_Associated_Node_For_Itype (Full, Related_Nod);
11744 -- Set common attributes for all subtypes: kind, convention, etc.
11746 Set_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
11747 Set_Convention (Full, Convention (Full_Base));
11749 -- The Etype of the full view is inconsistent. Gigi needs to see the
11750 -- structural full view, which is what the current scheme gives: the
11751 -- Etype of the full view is the etype of the full base. However, if the
11752 -- full base is a derived type, the full view then looks like a subtype
11753 -- of the parent, not a subtype of the full base. If instead we write:
11755 -- Set_Etype (Full, Full_Base);
11757 -- then we get inconsistencies in the front-end (confusion between
11758 -- views). Several outstanding bugs are related to this ???
11760 Set_Is_First_Subtype (Full, False);
11761 Set_Scope (Full, Scope (Priv));
11762 Set_Size_Info (Full, Full_Base);
11763 Set_RM_Size (Full, RM_Size (Full_Base));
11764 Set_Is_Itype (Full);
11766 -- A subtype of a private-type-without-discriminants, whose full-view
11767 -- has discriminants with default expressions, is not constrained.
11769 if not Has_Discriminants (Priv) then
11770 Set_Is_Constrained (Full, Is_Constrained (Full_Base));
11772 if Has_Discriminants (Full_Base) then
11773 Set_Discriminant_Constraint
11774 (Full, Discriminant_Constraint (Full_Base));
11776 -- The partial view may have been indefinite, the full view
11777 -- might not be.
11779 Set_Has_Unknown_Discriminants
11780 (Full, Has_Unknown_Discriminants (Full_Base));
11781 end if;
11782 end if;
11784 Set_First_Rep_Item (Full, First_Rep_Item (Full_Base));
11785 Set_Depends_On_Private (Full, Has_Private_Component (Full));
11787 -- Freeze the private subtype entity if its parent is delayed, and not
11788 -- already frozen. We skip this processing if the type is an anonymous
11789 -- subtype of a record component, or is the corresponding record of a
11790 -- protected type, since these are processed when the enclosing type
11791 -- is frozen.
11793 if not Is_Type (Scope (Full)) then
11794 Set_Has_Delayed_Freeze (Full,
11795 Has_Delayed_Freeze (Full_Base)
11796 and then (not Is_Frozen (Full_Base)));
11797 end if;
11799 Set_Freeze_Node (Full, Empty);
11800 Set_Is_Frozen (Full, False);
11801 Set_Full_View (Priv, Full);
11803 if Has_Discriminants (Full) then
11804 Set_Stored_Constraint_From_Discriminant_Constraint (Full);
11805 Set_Stored_Constraint (Priv, Stored_Constraint (Full));
11807 if Has_Unknown_Discriminants (Full) then
11808 Set_Discriminant_Constraint (Full, No_Elist);
11809 end if;
11810 end if;
11812 if Ekind (Full_Base) = E_Record_Type
11813 and then Has_Discriminants (Full_Base)
11814 and then Has_Discriminants (Priv) -- might not, if errors
11815 and then not Has_Unknown_Discriminants (Priv)
11816 and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
11817 then
11818 Create_Constrained_Components
11819 (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
11821 -- If the full base is itself derived from private, build a congruent
11822 -- subtype of its underlying type, for use by the back end. For a
11823 -- constrained record component, the declaration cannot be placed on
11824 -- the component list, but it must nevertheless be built an analyzed, to
11825 -- supply enough information for Gigi to compute the size of component.
11827 elsif Ekind (Full_Base) in Private_Kind
11828 and then Is_Derived_Type (Full_Base)
11829 and then Has_Discriminants (Full_Base)
11830 and then (Ekind (Current_Scope) /= E_Record_Subtype)
11831 then
11832 if not Is_Itype (Priv)
11833 and then
11834 Nkind (Subtype_Indication (Parent (Priv))) = N_Subtype_Indication
11835 then
11836 Build_Underlying_Full_View
11837 (Parent (Priv), Full, Etype (Full_Base));
11839 elsif Nkind (Related_Nod) = N_Component_Declaration then
11840 Build_Underlying_Full_View (Related_Nod, Full, Etype (Full_Base));
11841 end if;
11843 elsif Is_Record_Type (Full_Base) then
11845 -- Show Full is simply a renaming of Full_Base
11847 Set_Cloned_Subtype (Full, Full_Base);
11848 end if;
11850 -- It is unsafe to share the bounds of a scalar type, because the Itype
11851 -- is elaborated on demand, and if a bound is non-static then different
11852 -- orders of elaboration in different units will lead to different
11853 -- external symbols.
11855 if Is_Scalar_Type (Full_Base) then
11856 Set_Scalar_Range (Full,
11857 Make_Range (Sloc (Related_Nod),
11858 Low_Bound =>
11859 Duplicate_Subexpr_No_Checks (Type_Low_Bound (Full_Base)),
11860 High_Bound =>
11861 Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
11863 -- This completion inherits the bounds of the full parent, but if
11864 -- the parent is an unconstrained floating point type, so is the
11865 -- completion.
11867 if Is_Floating_Point_Type (Full_Base) then
11868 Set_Includes_Infinities
11869 (Scalar_Range (Full), Has_Infinities (Full_Base));
11870 end if;
11871 end if;
11873 -- ??? It seems that a lot of fields are missing that should be copied
11874 -- from Full_Base to Full. Here are some that are introduced in a
11875 -- non-disruptive way but a cleanup is necessary.
11877 if Is_Tagged_Type (Full_Base) then
11878 Set_Is_Tagged_Type (Full);
11879 Set_Direct_Primitive_Operations
11880 (Full, Direct_Primitive_Operations (Full_Base));
11881 Set_No_Tagged_Streams_Pragma
11882 (Full, No_Tagged_Streams_Pragma (Full_Base));
11884 -- Inherit class_wide type of full_base in case the partial view was
11885 -- not tagged. Otherwise it has already been created when the private
11886 -- subtype was analyzed.
11888 if No (Class_Wide_Type (Full)) then
11889 Set_Class_Wide_Type (Full, Class_Wide_Type (Full_Base));
11890 end if;
11892 -- If this is a subtype of a protected or task type, constrain its
11893 -- corresponding record, unless this is a subtype without constraints,
11894 -- i.e. a simple renaming as with an actual subtype in an instance.
11896 elsif Is_Concurrent_Type (Full_Base) then
11897 if Has_Discriminants (Full)
11898 and then Present (Corresponding_Record_Type (Full_Base))
11899 and then
11900 not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
11901 then
11902 Set_Corresponding_Record_Type (Full,
11903 Constrain_Corresponding_Record
11904 (Full, Corresponding_Record_Type (Full_Base), Related_Nod));
11906 else
11907 Set_Corresponding_Record_Type (Full,
11908 Corresponding_Record_Type (Full_Base));
11909 end if;
11910 end if;
11912 -- Link rep item chain, and also setting of Has_Predicates from private
11913 -- subtype to full subtype, since we will need these on the full subtype
11914 -- to create the predicate function. Note that the full subtype may
11915 -- already have rep items, inherited from the full view of the base
11916 -- type, so we must be sure not to overwrite these entries.
11918 declare
11919 Append : Boolean;
11920 Item : Node_Id;
11921 Next_Item : Node_Id;
11923 begin
11924 Item := First_Rep_Item (Full);
11926 -- If no existing rep items on full type, we can just link directly
11927 -- to the list of items on the private type, if any exist.. Same if
11928 -- the rep items are only those inherited from the base
11930 if (No (Item)
11931 or else Nkind (Item) /= N_Aspect_Specification
11932 or else Entity (Item) = Full_Base)
11933 and then Present (First_Rep_Item (Priv))
11934 then
11935 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
11937 -- Otherwise, search to the end of items currently linked to the full
11938 -- subtype and append the private items to the end. However, if Priv
11939 -- and Full already have the same list of rep items, then the append
11940 -- is not done, as that would create a circularity.
11942 elsif Item /= First_Rep_Item (Priv) then
11943 Append := True;
11944 loop
11945 Next_Item := Next_Rep_Item (Item);
11946 exit when No (Next_Item);
11947 Item := Next_Item;
11949 -- If the private view has aspect specifications, the full view
11950 -- inherits them. Since these aspects may already have been
11951 -- attached to the full view during derivation, do not append
11952 -- them if already present.
11954 if Item = First_Rep_Item (Priv) then
11955 Append := False;
11956 exit;
11957 end if;
11958 end loop;
11960 -- And link the private type items at the end of the chain
11962 if Append then
11963 Set_Next_Rep_Item (Item, First_Rep_Item (Priv));
11964 end if;
11965 end if;
11966 end;
11968 -- Make sure Has_Predicates is set on full type if it is set on the
11969 -- private type. Note that it may already be set on the full type and
11970 -- if so, we don't want to unset it. Similarly, propagate information
11971 -- about delayed aspects, because the corresponding pragmas must be
11972 -- analyzed when one of the views is frozen. This last step is needed
11973 -- in particular when the full type is a scalar type for which an
11974 -- anonymous base type is constructed.
11976 if Has_Predicates (Priv) then
11977 Set_Has_Predicates (Full);
11978 end if;
11980 if Has_Delayed_Aspects (Priv) then
11981 Set_Has_Delayed_Aspects (Full);
11982 end if;
11983 end Complete_Private_Subtype;
11985 ----------------------------
11986 -- Constant_Redeclaration --
11987 ----------------------------
11989 procedure Constant_Redeclaration
11990 (Id : Entity_Id;
11991 N : Node_Id;
11992 T : out Entity_Id)
11994 Prev : constant Entity_Id := Current_Entity_In_Scope (Id);
11995 Obj_Def : constant Node_Id := Object_Definition (N);
11996 New_T : Entity_Id;
11998 procedure Check_Possible_Deferred_Completion
11999 (Prev_Id : Entity_Id;
12000 Prev_Obj_Def : Node_Id;
12001 Curr_Obj_Def : Node_Id);
12002 -- Determine whether the two object definitions describe the partial
12003 -- and the full view of a constrained deferred constant. Generate
12004 -- a subtype for the full view and verify that it statically matches
12005 -- the subtype of the partial view.
12007 procedure Check_Recursive_Declaration (Typ : Entity_Id);
12008 -- If deferred constant is an access type initialized with an allocator,
12009 -- check whether there is an illegal recursion in the definition,
12010 -- through a default value of some record subcomponent. This is normally
12011 -- detected when generating init procs, but requires this additional
12012 -- mechanism when expansion is disabled.
12014 ----------------------------------------
12015 -- Check_Possible_Deferred_Completion --
12016 ----------------------------------------
12018 procedure Check_Possible_Deferred_Completion
12019 (Prev_Id : Entity_Id;
12020 Prev_Obj_Def : Node_Id;
12021 Curr_Obj_Def : Node_Id)
12023 begin
12024 if Nkind (Prev_Obj_Def) = N_Subtype_Indication
12025 and then Present (Constraint (Prev_Obj_Def))
12026 and then Nkind (Curr_Obj_Def) = N_Subtype_Indication
12027 and then Present (Constraint (Curr_Obj_Def))
12028 then
12029 declare
12030 Loc : constant Source_Ptr := Sloc (N);
12031 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
12032 Decl : constant Node_Id :=
12033 Make_Subtype_Declaration (Loc,
12034 Defining_Identifier => Def_Id,
12035 Subtype_Indication =>
12036 Relocate_Node (Curr_Obj_Def));
12038 begin
12039 Insert_Before_And_Analyze (N, Decl);
12040 Set_Etype (Id, Def_Id);
12042 if not Subtypes_Statically_Match (Etype (Prev_Id), Def_Id) then
12043 Error_Msg_Sloc := Sloc (Prev_Id);
12044 Error_Msg_N ("subtype does not statically match deferred "
12045 & "declaration #", N);
12046 end if;
12047 end;
12048 end if;
12049 end Check_Possible_Deferred_Completion;
12051 ---------------------------------
12052 -- Check_Recursive_Declaration --
12053 ---------------------------------
12055 procedure Check_Recursive_Declaration (Typ : Entity_Id) is
12056 Comp : Entity_Id;
12058 begin
12059 if Is_Record_Type (Typ) then
12060 Comp := First_Component (Typ);
12061 while Present (Comp) loop
12062 if Comes_From_Source (Comp) then
12063 if Present (Expression (Parent (Comp)))
12064 and then Is_Entity_Name (Expression (Parent (Comp)))
12065 and then Entity (Expression (Parent (Comp))) = Prev
12066 then
12067 Error_Msg_Sloc := Sloc (Parent (Comp));
12068 Error_Msg_NE
12069 ("illegal circularity with declaration for & #",
12070 N, Comp);
12071 return;
12073 elsif Is_Record_Type (Etype (Comp)) then
12074 Check_Recursive_Declaration (Etype (Comp));
12075 end if;
12076 end if;
12078 Next_Component (Comp);
12079 end loop;
12080 end if;
12081 end Check_Recursive_Declaration;
12083 -- Start of processing for Constant_Redeclaration
12085 begin
12086 if Nkind (Parent (Prev)) = N_Object_Declaration then
12087 if Nkind (Object_Definition
12088 (Parent (Prev))) = N_Subtype_Indication
12089 then
12090 -- Find type of new declaration. The constraints of the two
12091 -- views must match statically, but there is no point in
12092 -- creating an itype for the full view.
12094 if Nkind (Obj_Def) = N_Subtype_Indication then
12095 Find_Type (Subtype_Mark (Obj_Def));
12096 New_T := Entity (Subtype_Mark (Obj_Def));
12098 else
12099 Find_Type (Obj_Def);
12100 New_T := Entity (Obj_Def);
12101 end if;
12103 T := Etype (Prev);
12105 else
12106 -- The full view may impose a constraint, even if the partial
12107 -- view does not, so construct the subtype.
12109 New_T := Find_Type_Of_Object (Obj_Def, N);
12110 T := New_T;
12111 end if;
12113 else
12114 -- Current declaration is illegal, diagnosed below in Enter_Name
12116 T := Empty;
12117 New_T := Any_Type;
12118 end if;
12120 -- If previous full declaration or a renaming declaration exists, or if
12121 -- a homograph is present, let Enter_Name handle it, either with an
12122 -- error or with the removal of an overridden implicit subprogram.
12123 -- The previous one is a full declaration if it has an expression
12124 -- (which in the case of an aggregate is indicated by the Init flag).
12126 if Ekind (Prev) /= E_Constant
12127 or else Nkind (Parent (Prev)) = N_Object_Renaming_Declaration
12128 or else Present (Expression (Parent (Prev)))
12129 or else Has_Init_Expression (Parent (Prev))
12130 or else Present (Full_View (Prev))
12131 then
12132 Enter_Name (Id);
12134 -- Verify that types of both declarations match, or else that both types
12135 -- are anonymous access types whose designated subtypes statically match
12136 -- (as allowed in Ada 2005 by AI-385).
12138 elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
12139 and then
12140 (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
12141 or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
12142 or else Is_Access_Constant (Etype (New_T)) /=
12143 Is_Access_Constant (Etype (Prev))
12144 or else Can_Never_Be_Null (Etype (New_T)) /=
12145 Can_Never_Be_Null (Etype (Prev))
12146 or else Null_Exclusion_Present (Parent (Prev)) /=
12147 Null_Exclusion_Present (Parent (Id))
12148 or else not Subtypes_Statically_Match
12149 (Designated_Type (Etype (Prev)),
12150 Designated_Type (Etype (New_T))))
12151 then
12152 Error_Msg_Sloc := Sloc (Prev);
12153 Error_Msg_N ("type does not match declaration#", N);
12154 Set_Full_View (Prev, Id);
12155 Set_Etype (Id, Any_Type);
12157 -- A deferred constant whose type is an anonymous array is always
12158 -- illegal (unless imported). A detailed error message might be
12159 -- helpful for Ada beginners.
12161 if Nkind (Object_Definition (Parent (Prev)))
12162 = N_Constrained_Array_Definition
12163 and then Nkind (Object_Definition (N))
12164 = N_Constrained_Array_Definition
12165 then
12166 Error_Msg_N ("\each anonymous array is a distinct type", N);
12167 Error_Msg_N ("a deferred constant must have a named type",
12168 Object_Definition (Parent (Prev)));
12169 end if;
12171 elsif
12172 Null_Exclusion_Present (Parent (Prev))
12173 and then not Null_Exclusion_Present (N)
12174 then
12175 Error_Msg_Sloc := Sloc (Prev);
12176 Error_Msg_N ("null-exclusion does not match declaration#", N);
12177 Set_Full_View (Prev, Id);
12178 Set_Etype (Id, Any_Type);
12180 -- If so, process the full constant declaration
12182 else
12183 -- RM 7.4 (6): If the subtype defined by the subtype_indication in
12184 -- the deferred declaration is constrained, then the subtype defined
12185 -- by the subtype_indication in the full declaration shall match it
12186 -- statically.
12188 Check_Possible_Deferred_Completion
12189 (Prev_Id => Prev,
12190 Prev_Obj_Def => Object_Definition (Parent (Prev)),
12191 Curr_Obj_Def => Obj_Def);
12193 Set_Full_View (Prev, Id);
12194 Set_Is_Public (Id, Is_Public (Prev));
12195 Set_Is_Internal (Id);
12196 Append_Entity (Id, Current_Scope);
12198 -- Check ALIASED present if present before (RM 7.4(7))
12200 if Is_Aliased (Prev)
12201 and then not Aliased_Present (N)
12202 then
12203 Error_Msg_Sloc := Sloc (Prev);
12204 Error_Msg_N ("ALIASED required (see declaration #)", N);
12205 end if;
12207 -- Check that placement is in private part and that the incomplete
12208 -- declaration appeared in the visible part.
12210 if Ekind (Current_Scope) = E_Package
12211 and then not In_Private_Part (Current_Scope)
12212 then
12213 Error_Msg_Sloc := Sloc (Prev);
12214 Error_Msg_N
12215 ("full constant for declaration # must be in private part", N);
12217 elsif Ekind (Current_Scope) = E_Package
12218 and then
12219 List_Containing (Parent (Prev)) /=
12220 Visible_Declarations (Package_Specification (Current_Scope))
12221 then
12222 Error_Msg_N
12223 ("deferred constant must be declared in visible part",
12224 Parent (Prev));
12225 end if;
12227 if Is_Access_Type (T)
12228 and then Nkind (Expression (N)) = N_Allocator
12229 then
12230 Check_Recursive_Declaration (Designated_Type (T));
12231 end if;
12233 -- A deferred constant is a visible entity. If type has invariants,
12234 -- verify that the initial value satisfies them.
12236 if Has_Invariants (T) and then Present (Invariant_Procedure (T)) then
12237 Insert_After (N,
12238 Make_Invariant_Call (New_Occurrence_Of (Prev, Sloc (N))));
12239 end if;
12240 end if;
12241 end Constant_Redeclaration;
12243 ----------------------
12244 -- Constrain_Access --
12245 ----------------------
12247 procedure Constrain_Access
12248 (Def_Id : in out Entity_Id;
12249 S : Node_Id;
12250 Related_Nod : Node_Id)
12252 T : constant Entity_Id := Entity (Subtype_Mark (S));
12253 Desig_Type : constant Entity_Id := Designated_Type (T);
12254 Desig_Subtype : Entity_Id := Create_Itype (E_Void, Related_Nod);
12255 Constraint_OK : Boolean := True;
12257 begin
12258 if Is_Array_Type (Desig_Type) then
12259 Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
12261 elsif (Is_Record_Type (Desig_Type)
12262 or else Is_Incomplete_Or_Private_Type (Desig_Type))
12263 and then not Is_Constrained (Desig_Type)
12264 then
12265 -- ??? The following code is a temporary bypass to ignore a
12266 -- discriminant constraint on access type if it is constraining
12267 -- the current record. Avoid creating the implicit subtype of the
12268 -- record we are currently compiling since right now, we cannot
12269 -- handle these. For now, just return the access type itself.
12271 if Desig_Type = Current_Scope
12272 and then No (Def_Id)
12273 then
12274 Set_Ekind (Desig_Subtype, E_Record_Subtype);
12275 Def_Id := Entity (Subtype_Mark (S));
12277 -- This call added to ensure that the constraint is analyzed
12278 -- (needed for a B test). Note that we still return early from
12279 -- this procedure to avoid recursive processing. ???
12281 Constrain_Discriminated_Type
12282 (Desig_Subtype, S, Related_Nod, For_Access => True);
12283 return;
12284 end if;
12286 -- Enforce rule that the constraint is illegal if there is an
12287 -- unconstrained view of the designated type. This means that the
12288 -- partial view (either a private type declaration or a derivation
12289 -- from a private type) has no discriminants. (Defect Report
12290 -- 8652/0008, Technical Corrigendum 1, checked by ACATS B371001).
12292 -- Rule updated for Ada 2005: The private type is said to have
12293 -- a constrained partial view, given that objects of the type
12294 -- can be declared. Furthermore, the rule applies to all access
12295 -- types, unlike the rule concerning default discriminants (see
12296 -- RM 3.7.1(7/3))
12298 if (Ekind (T) = E_General_Access_Type or else Ada_Version >= Ada_2005)
12299 and then Has_Private_Declaration (Desig_Type)
12300 and then In_Open_Scopes (Scope (Desig_Type))
12301 and then Has_Discriminants (Desig_Type)
12302 then
12303 declare
12304 Pack : constant Node_Id :=
12305 Unit_Declaration_Node (Scope (Desig_Type));
12306 Decls : List_Id;
12307 Decl : Node_Id;
12309 begin
12310 if Nkind (Pack) = N_Package_Declaration then
12311 Decls := Visible_Declarations (Specification (Pack));
12312 Decl := First (Decls);
12313 while Present (Decl) loop
12314 if (Nkind (Decl) = N_Private_Type_Declaration
12315 and then Chars (Defining_Identifier (Decl)) =
12316 Chars (Desig_Type))
12318 or else
12319 (Nkind (Decl) = N_Full_Type_Declaration
12320 and then
12321 Chars (Defining_Identifier (Decl)) =
12322 Chars (Desig_Type)
12323 and then Is_Derived_Type (Desig_Type)
12324 and then
12325 Has_Private_Declaration (Etype (Desig_Type)))
12326 then
12327 if No (Discriminant_Specifications (Decl)) then
12328 Error_Msg_N
12329 ("cannot constrain access type if designated "
12330 & "type has constrained partial view", S);
12331 end if;
12333 exit;
12334 end if;
12336 Next (Decl);
12337 end loop;
12338 end if;
12339 end;
12340 end if;
12342 Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
12343 For_Access => True);
12345 elsif Is_Concurrent_Type (Desig_Type)
12346 and then not Is_Constrained (Desig_Type)
12347 then
12348 Constrain_Concurrent (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
12350 else
12351 Error_Msg_N ("invalid constraint on access type", S);
12353 -- We simply ignore an invalid constraint
12355 Desig_Subtype := Desig_Type;
12356 Constraint_OK := False;
12357 end if;
12359 if No (Def_Id) then
12360 Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
12361 else
12362 Set_Ekind (Def_Id, E_Access_Subtype);
12363 end if;
12365 if Constraint_OK then
12366 Set_Etype (Def_Id, Base_Type (T));
12368 if Is_Private_Type (Desig_Type) then
12369 Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
12370 end if;
12371 else
12372 Set_Etype (Def_Id, Any_Type);
12373 end if;
12375 Set_Size_Info (Def_Id, T);
12376 Set_Is_Constrained (Def_Id, Constraint_OK);
12377 Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
12378 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
12379 Set_Is_Access_Constant (Def_Id, Is_Access_Constant (T));
12381 Conditional_Delay (Def_Id, T);
12383 -- AI-363 : Subtypes of general access types whose designated types have
12384 -- default discriminants are disallowed. In instances, the rule has to
12385 -- be checked against the actual, of which T is the subtype. In a
12386 -- generic body, the rule is checked assuming that the actual type has
12387 -- defaulted discriminants.
12389 if Ada_Version >= Ada_2005 or else Warn_On_Ada_2005_Compatibility then
12390 if Ekind (Base_Type (T)) = E_General_Access_Type
12391 and then Has_Defaulted_Discriminants (Desig_Type)
12392 then
12393 if Ada_Version < Ada_2005 then
12394 Error_Msg_N
12395 ("access subtype of general access type would not " &
12396 "be allowed in Ada 2005?y?", S);
12397 else
12398 Error_Msg_N
12399 ("access subtype of general access type not allowed", S);
12400 end if;
12402 Error_Msg_N ("\discriminants have defaults", S);
12404 elsif Is_Access_Type (T)
12405 and then Is_Generic_Type (Desig_Type)
12406 and then Has_Discriminants (Desig_Type)
12407 and then In_Package_Body (Current_Scope)
12408 then
12409 if Ada_Version < Ada_2005 then
12410 Error_Msg_N
12411 ("access subtype would not be allowed in generic body "
12412 & "in Ada 2005?y?", S);
12413 else
12414 Error_Msg_N
12415 ("access subtype not allowed in generic body", S);
12416 end if;
12418 Error_Msg_N
12419 ("\designated type is a discriminated formal", S);
12420 end if;
12421 end if;
12422 end Constrain_Access;
12424 ---------------------
12425 -- Constrain_Array --
12426 ---------------------
12428 procedure Constrain_Array
12429 (Def_Id : in out Entity_Id;
12430 SI : Node_Id;
12431 Related_Nod : Node_Id;
12432 Related_Id : Entity_Id;
12433 Suffix : Character)
12435 C : constant Node_Id := Constraint (SI);
12436 Number_Of_Constraints : Nat := 0;
12437 Index : Node_Id;
12438 S, T : Entity_Id;
12439 Constraint_OK : Boolean := True;
12441 begin
12442 T := Entity (Subtype_Mark (SI));
12444 if Is_Access_Type (T) then
12445 T := Designated_Type (T);
12446 end if;
12448 -- If an index constraint follows a subtype mark in a subtype indication
12449 -- then the type or subtype denoted by the subtype mark must not already
12450 -- impose an index constraint. The subtype mark must denote either an
12451 -- unconstrained array type or an access type whose designated type
12452 -- is such an array type... (RM 3.6.1)
12454 if Is_Constrained (T) then
12455 Error_Msg_N ("array type is already constrained", Subtype_Mark (SI));
12456 Constraint_OK := False;
12458 else
12459 S := First (Constraints (C));
12460 while Present (S) loop
12461 Number_Of_Constraints := Number_Of_Constraints + 1;
12462 Next (S);
12463 end loop;
12465 -- In either case, the index constraint must provide a discrete
12466 -- range for each index of the array type and the type of each
12467 -- discrete range must be the same as that of the corresponding
12468 -- index. (RM 3.6.1)
12470 if Number_Of_Constraints /= Number_Dimensions (T) then
12471 Error_Msg_NE ("incorrect number of index constraints for }", C, T);
12472 Constraint_OK := False;
12474 else
12475 S := First (Constraints (C));
12476 Index := First_Index (T);
12477 Analyze (Index);
12479 -- Apply constraints to each index type
12481 for J in 1 .. Number_Of_Constraints loop
12482 Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
12483 Next (Index);
12484 Next (S);
12485 end loop;
12487 end if;
12488 end if;
12490 if No (Def_Id) then
12491 Def_Id :=
12492 Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
12493 Set_Parent (Def_Id, Related_Nod);
12495 else
12496 Set_Ekind (Def_Id, E_Array_Subtype);
12497 end if;
12499 Set_Size_Info (Def_Id, (T));
12500 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
12501 Set_Etype (Def_Id, Base_Type (T));
12503 if Constraint_OK then
12504 Set_First_Index (Def_Id, First (Constraints (C)));
12505 else
12506 Set_First_Index (Def_Id, First_Index (T));
12507 end if;
12509 Set_Is_Constrained (Def_Id, True);
12510 Set_Is_Aliased (Def_Id, Is_Aliased (T));
12511 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
12513 Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
12514 Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
12516 -- A subtype does not inherit the Packed_Array_Impl_Type of is parent.
12517 -- We need to initialize the attribute because if Def_Id is previously
12518 -- analyzed through a limited_with clause, it will have the attributes
12519 -- of an incomplete type, one of which is an Elist that overlaps the
12520 -- Packed_Array_Impl_Type field.
12522 Set_Packed_Array_Impl_Type (Def_Id, Empty);
12524 -- Build a freeze node if parent still needs one. Also make sure that
12525 -- the Depends_On_Private status is set because the subtype will need
12526 -- reprocessing at the time the base type does, and also we must set a
12527 -- conditional delay.
12529 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
12530 Conditional_Delay (Def_Id, T);
12531 end Constrain_Array;
12533 ------------------------------
12534 -- Constrain_Component_Type --
12535 ------------------------------
12537 function Constrain_Component_Type
12538 (Comp : Entity_Id;
12539 Constrained_Typ : Entity_Id;
12540 Related_Node : Node_Id;
12541 Typ : Entity_Id;
12542 Constraints : Elist_Id) return Entity_Id
12544 Loc : constant Source_Ptr := Sloc (Constrained_Typ);
12545 Compon_Type : constant Entity_Id := Etype (Comp);
12547 function Build_Constrained_Array_Type
12548 (Old_Type : Entity_Id) return Entity_Id;
12549 -- If Old_Type is an array type, one of whose indexes is constrained
12550 -- by a discriminant, build an Itype whose constraint replaces the
12551 -- discriminant with its value in the constraint.
12553 function Build_Constrained_Discriminated_Type
12554 (Old_Type : Entity_Id) return Entity_Id;
12555 -- Ditto for record components
12557 function Build_Constrained_Access_Type
12558 (Old_Type : Entity_Id) return Entity_Id;
12559 -- Ditto for access types. Makes use of previous two functions, to
12560 -- constrain designated type.
12562 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id;
12563 -- T is an array or discriminated type, C is a list of constraints
12564 -- that apply to T. This routine builds the constrained subtype.
12566 function Is_Discriminant (Expr : Node_Id) return Boolean;
12567 -- Returns True if Expr is a discriminant
12569 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id;
12570 -- Find the value of discriminant Discrim in Constraint
12572 -----------------------------------
12573 -- Build_Constrained_Access_Type --
12574 -----------------------------------
12576 function Build_Constrained_Access_Type
12577 (Old_Type : Entity_Id) return Entity_Id
12579 Desig_Type : constant Entity_Id := Designated_Type (Old_Type);
12580 Itype : Entity_Id;
12581 Desig_Subtype : Entity_Id;
12582 Scop : Entity_Id;
12584 begin
12585 -- if the original access type was not embedded in the enclosing
12586 -- type definition, there is no need to produce a new access
12587 -- subtype. In fact every access type with an explicit constraint
12588 -- generates an itype whose scope is the enclosing record.
12590 if not Is_Type (Scope (Old_Type)) then
12591 return Old_Type;
12593 elsif Is_Array_Type (Desig_Type) then
12594 Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
12596 elsif Has_Discriminants (Desig_Type) then
12598 -- This may be an access type to an enclosing record type for
12599 -- which we are constructing the constrained components. Return
12600 -- the enclosing record subtype. This is not always correct,
12601 -- but avoids infinite recursion. ???
12603 Desig_Subtype := Any_Type;
12605 for J in reverse 0 .. Scope_Stack.Last loop
12606 Scop := Scope_Stack.Table (J).Entity;
12608 if Is_Type (Scop)
12609 and then Base_Type (Scop) = Base_Type (Desig_Type)
12610 then
12611 Desig_Subtype := Scop;
12612 end if;
12614 exit when not Is_Type (Scop);
12615 end loop;
12617 if Desig_Subtype = Any_Type then
12618 Desig_Subtype :=
12619 Build_Constrained_Discriminated_Type (Desig_Type);
12620 end if;
12622 else
12623 return Old_Type;
12624 end if;
12626 if Desig_Subtype /= Desig_Type then
12628 -- The Related_Node better be here or else we won't be able
12629 -- to attach new itypes to a node in the tree.
12631 pragma Assert (Present (Related_Node));
12633 Itype := Create_Itype (E_Access_Subtype, Related_Node);
12635 Set_Etype (Itype, Base_Type (Old_Type));
12636 Set_Size_Info (Itype, (Old_Type));
12637 Set_Directly_Designated_Type (Itype, Desig_Subtype);
12638 Set_Depends_On_Private (Itype, Has_Private_Component
12639 (Old_Type));
12640 Set_Is_Access_Constant (Itype, Is_Access_Constant
12641 (Old_Type));
12643 -- The new itype needs freezing when it depends on a not frozen
12644 -- type and the enclosing subtype needs freezing.
12646 if Has_Delayed_Freeze (Constrained_Typ)
12647 and then not Is_Frozen (Constrained_Typ)
12648 then
12649 Conditional_Delay (Itype, Base_Type (Old_Type));
12650 end if;
12652 return Itype;
12654 else
12655 return Old_Type;
12656 end if;
12657 end Build_Constrained_Access_Type;
12659 ----------------------------------
12660 -- Build_Constrained_Array_Type --
12661 ----------------------------------
12663 function Build_Constrained_Array_Type
12664 (Old_Type : Entity_Id) return Entity_Id
12666 Lo_Expr : Node_Id;
12667 Hi_Expr : Node_Id;
12668 Old_Index : Node_Id;
12669 Range_Node : Node_Id;
12670 Constr_List : List_Id;
12672 Need_To_Create_Itype : Boolean := False;
12674 begin
12675 Old_Index := First_Index (Old_Type);
12676 while Present (Old_Index) loop
12677 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
12679 if Is_Discriminant (Lo_Expr)
12680 or else
12681 Is_Discriminant (Hi_Expr)
12682 then
12683 Need_To_Create_Itype := True;
12684 end if;
12686 Next_Index (Old_Index);
12687 end loop;
12689 if Need_To_Create_Itype then
12690 Constr_List := New_List;
12692 Old_Index := First_Index (Old_Type);
12693 while Present (Old_Index) loop
12694 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
12696 if Is_Discriminant (Lo_Expr) then
12697 Lo_Expr := Get_Discr_Value (Lo_Expr);
12698 end if;
12700 if Is_Discriminant (Hi_Expr) then
12701 Hi_Expr := Get_Discr_Value (Hi_Expr);
12702 end if;
12704 Range_Node :=
12705 Make_Range
12706 (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
12708 Append (Range_Node, To => Constr_List);
12710 Next_Index (Old_Index);
12711 end loop;
12713 return Build_Subtype (Old_Type, Constr_List);
12715 else
12716 return Old_Type;
12717 end if;
12718 end Build_Constrained_Array_Type;
12720 ------------------------------------------
12721 -- Build_Constrained_Discriminated_Type --
12722 ------------------------------------------
12724 function Build_Constrained_Discriminated_Type
12725 (Old_Type : Entity_Id) return Entity_Id
12727 Expr : Node_Id;
12728 Constr_List : List_Id;
12729 Old_Constraint : Elmt_Id;
12731 Need_To_Create_Itype : Boolean := False;
12733 begin
12734 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
12735 while Present (Old_Constraint) loop
12736 Expr := Node (Old_Constraint);
12738 if Is_Discriminant (Expr) then
12739 Need_To_Create_Itype := True;
12740 end if;
12742 Next_Elmt (Old_Constraint);
12743 end loop;
12745 if Need_To_Create_Itype then
12746 Constr_List := New_List;
12748 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
12749 while Present (Old_Constraint) loop
12750 Expr := Node (Old_Constraint);
12752 if Is_Discriminant (Expr) then
12753 Expr := Get_Discr_Value (Expr);
12754 end if;
12756 Append (New_Copy_Tree (Expr), To => Constr_List);
12758 Next_Elmt (Old_Constraint);
12759 end loop;
12761 return Build_Subtype (Old_Type, Constr_List);
12763 else
12764 return Old_Type;
12765 end if;
12766 end Build_Constrained_Discriminated_Type;
12768 -------------------
12769 -- Build_Subtype --
12770 -------------------
12772 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id is
12773 Indic : Node_Id;
12774 Subtyp_Decl : Node_Id;
12775 Def_Id : Entity_Id;
12776 Btyp : Entity_Id := Base_Type (T);
12778 begin
12779 -- The Related_Node better be here or else we won't be able to
12780 -- attach new itypes to a node in the tree.
12782 pragma Assert (Present (Related_Node));
12784 -- If the view of the component's type is incomplete or private
12785 -- with unknown discriminants, then the constraint must be applied
12786 -- to the full type.
12788 if Has_Unknown_Discriminants (Btyp)
12789 and then Present (Underlying_Type (Btyp))
12790 then
12791 Btyp := Underlying_Type (Btyp);
12792 end if;
12794 Indic :=
12795 Make_Subtype_Indication (Loc,
12796 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
12797 Constraint => Make_Index_Or_Discriminant_Constraint (Loc, C));
12799 Def_Id := Create_Itype (Ekind (T), Related_Node);
12801 Subtyp_Decl :=
12802 Make_Subtype_Declaration (Loc,
12803 Defining_Identifier => Def_Id,
12804 Subtype_Indication => Indic);
12806 Set_Parent (Subtyp_Decl, Parent (Related_Node));
12808 -- Itypes must be analyzed with checks off (see package Itypes)
12810 Analyze (Subtyp_Decl, Suppress => All_Checks);
12812 return Def_Id;
12813 end Build_Subtype;
12815 ---------------------
12816 -- Get_Discr_Value --
12817 ---------------------
12819 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id is
12820 D : Entity_Id;
12821 E : Elmt_Id;
12823 begin
12824 -- The discriminant may be declared for the type, in which case we
12825 -- find it by iterating over the list of discriminants. If the
12826 -- discriminant is inherited from a parent type, it appears as the
12827 -- corresponding discriminant of the current type. This will be the
12828 -- case when constraining an inherited component whose constraint is
12829 -- given by a discriminant of the parent.
12831 D := First_Discriminant (Typ);
12832 E := First_Elmt (Constraints);
12834 while Present (D) loop
12835 if D = Entity (Discrim)
12836 or else D = CR_Discriminant (Entity (Discrim))
12837 or else Corresponding_Discriminant (D) = Entity (Discrim)
12838 then
12839 return Node (E);
12840 end if;
12842 Next_Discriminant (D);
12843 Next_Elmt (E);
12844 end loop;
12846 -- The Corresponding_Discriminant mechanism is incomplete, because
12847 -- the correspondence between new and old discriminants is not one
12848 -- to one: one new discriminant can constrain several old ones. In
12849 -- that case, scan sequentially the stored_constraint, the list of
12850 -- discriminants of the parents, and the constraints.
12852 -- Previous code checked for the present of the Stored_Constraint
12853 -- list for the derived type, but did not use it at all. Should it
12854 -- be present when the component is a discriminated task type?
12856 if Is_Derived_Type (Typ)
12857 and then Scope (Entity (Discrim)) = Etype (Typ)
12858 then
12859 D := First_Discriminant (Etype (Typ));
12860 E := First_Elmt (Constraints);
12861 while Present (D) loop
12862 if D = Entity (Discrim) then
12863 return Node (E);
12864 end if;
12866 Next_Discriminant (D);
12867 Next_Elmt (E);
12868 end loop;
12869 end if;
12871 -- Something is wrong if we did not find the value
12873 raise Program_Error;
12874 end Get_Discr_Value;
12876 ---------------------
12877 -- Is_Discriminant --
12878 ---------------------
12880 function Is_Discriminant (Expr : Node_Id) return Boolean is
12881 Discrim_Scope : Entity_Id;
12883 begin
12884 if Denotes_Discriminant (Expr) then
12885 Discrim_Scope := Scope (Entity (Expr));
12887 -- Either we have a reference to one of Typ's discriminants,
12889 pragma Assert (Discrim_Scope = Typ
12891 -- or to the discriminants of the parent type, in the case
12892 -- of a derivation of a tagged type with variants.
12894 or else Discrim_Scope = Etype (Typ)
12895 or else Full_View (Discrim_Scope) = Etype (Typ)
12897 -- or same as above for the case where the discriminants
12898 -- were declared in Typ's private view.
12900 or else (Is_Private_Type (Discrim_Scope)
12901 and then Chars (Discrim_Scope) = Chars (Typ))
12903 -- or else we are deriving from the full view and the
12904 -- discriminant is declared in the private entity.
12906 or else (Is_Private_Type (Typ)
12907 and then Chars (Discrim_Scope) = Chars (Typ))
12909 -- Or we are constrained the corresponding record of a
12910 -- synchronized type that completes a private declaration.
12912 or else (Is_Concurrent_Record_Type (Typ)
12913 and then
12914 Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
12916 -- or we have a class-wide type, in which case make sure the
12917 -- discriminant found belongs to the root type.
12919 or else (Is_Class_Wide_Type (Typ)
12920 and then Etype (Typ) = Discrim_Scope));
12922 return True;
12923 end if;
12925 -- In all other cases we have something wrong
12927 return False;
12928 end Is_Discriminant;
12930 -- Start of processing for Constrain_Component_Type
12932 begin
12933 if Nkind (Parent (Comp)) = N_Component_Declaration
12934 and then Comes_From_Source (Parent (Comp))
12935 and then Comes_From_Source
12936 (Subtype_Indication (Component_Definition (Parent (Comp))))
12937 and then
12938 Is_Entity_Name
12939 (Subtype_Indication (Component_Definition (Parent (Comp))))
12940 then
12941 return Compon_Type;
12943 elsif Is_Array_Type (Compon_Type) then
12944 return Build_Constrained_Array_Type (Compon_Type);
12946 elsif Has_Discriminants (Compon_Type) then
12947 return Build_Constrained_Discriminated_Type (Compon_Type);
12949 elsif Is_Access_Type (Compon_Type) then
12950 return Build_Constrained_Access_Type (Compon_Type);
12952 else
12953 return Compon_Type;
12954 end if;
12955 end Constrain_Component_Type;
12957 --------------------------
12958 -- Constrain_Concurrent --
12959 --------------------------
12961 -- For concurrent types, the associated record value type carries the same
12962 -- discriminants, so when we constrain a concurrent type, we must constrain
12963 -- the corresponding record type as well.
12965 procedure Constrain_Concurrent
12966 (Def_Id : in out Entity_Id;
12967 SI : Node_Id;
12968 Related_Nod : Node_Id;
12969 Related_Id : Entity_Id;
12970 Suffix : Character)
12972 -- Retrieve Base_Type to ensure getting to the concurrent type in the
12973 -- case of a private subtype (needed when only doing semantic analysis).
12975 T_Ent : Entity_Id := Base_Type (Entity (Subtype_Mark (SI)));
12976 T_Val : Entity_Id;
12978 begin
12979 if Is_Access_Type (T_Ent) then
12980 T_Ent := Designated_Type (T_Ent);
12981 end if;
12983 T_Val := Corresponding_Record_Type (T_Ent);
12985 if Present (T_Val) then
12987 if No (Def_Id) then
12988 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
12990 -- Elaborate itype now, as it may be used in a subsequent
12991 -- synchronized operation in another scope.
12993 if Nkind (Related_Nod) = N_Full_Type_Declaration then
12994 Build_Itype_Reference (Def_Id, Related_Nod);
12995 end if;
12996 end if;
12998 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
13000 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
13001 Set_Corresponding_Record_Type (Def_Id,
13002 Constrain_Corresponding_Record (Def_Id, T_Val, Related_Nod));
13004 else
13005 -- If there is no associated record, expansion is disabled and this
13006 -- is a generic context. Create a subtype in any case, so that
13007 -- semantic analysis can proceed.
13009 if No (Def_Id) then
13010 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
13011 end if;
13013 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
13014 end if;
13015 end Constrain_Concurrent;
13017 ------------------------------------
13018 -- Constrain_Corresponding_Record --
13019 ------------------------------------
13021 function Constrain_Corresponding_Record
13022 (Prot_Subt : Entity_Id;
13023 Corr_Rec : Entity_Id;
13024 Related_Nod : Node_Id) return Entity_Id
13026 T_Sub : constant Entity_Id :=
13027 Create_Itype (E_Record_Subtype, Related_Nod, Corr_Rec, 'C');
13029 begin
13030 Set_Etype (T_Sub, Corr_Rec);
13031 Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
13032 Set_Is_Constrained (T_Sub, True);
13033 Set_First_Entity (T_Sub, First_Entity (Corr_Rec));
13034 Set_Last_Entity (T_Sub, Last_Entity (Corr_Rec));
13036 if Has_Discriminants (Prot_Subt) then -- False only if errors.
13037 Set_Discriminant_Constraint
13038 (T_Sub, Discriminant_Constraint (Prot_Subt));
13039 Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
13040 Create_Constrained_Components
13041 (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
13042 end if;
13044 Set_Depends_On_Private (T_Sub, Has_Private_Component (T_Sub));
13046 if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
13047 Conditional_Delay (T_Sub, Corr_Rec);
13049 else
13050 -- This is a component subtype: it will be frozen in the context of
13051 -- the enclosing record's init_proc, so that discriminant references
13052 -- are resolved to discriminals. (Note: we used to skip freezing
13053 -- altogether in that case, which caused errors downstream for
13054 -- components of a bit packed array type).
13056 Set_Has_Delayed_Freeze (T_Sub);
13057 end if;
13059 return T_Sub;
13060 end Constrain_Corresponding_Record;
13062 -----------------------
13063 -- Constrain_Decimal --
13064 -----------------------
13066 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id) is
13067 T : constant Entity_Id := Entity (Subtype_Mark (S));
13068 C : constant Node_Id := Constraint (S);
13069 Loc : constant Source_Ptr := Sloc (C);
13070 Range_Expr : Node_Id;
13071 Digits_Expr : Node_Id;
13072 Digits_Val : Uint;
13073 Bound_Val : Ureal;
13075 begin
13076 Set_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
13078 if Nkind (C) = N_Range_Constraint then
13079 Range_Expr := Range_Expression (C);
13080 Digits_Val := Digits_Value (T);
13082 else
13083 pragma Assert (Nkind (C) = N_Digits_Constraint);
13085 Check_SPARK_05_Restriction ("digits constraint is not allowed", S);
13087 Digits_Expr := Digits_Expression (C);
13088 Analyze_And_Resolve (Digits_Expr, Any_Integer);
13090 Check_Digits_Expression (Digits_Expr);
13091 Digits_Val := Expr_Value (Digits_Expr);
13093 if Digits_Val > Digits_Value (T) then
13094 Error_Msg_N
13095 ("digits expression is incompatible with subtype", C);
13096 Digits_Val := Digits_Value (T);
13097 end if;
13099 if Present (Range_Constraint (C)) then
13100 Range_Expr := Range_Expression (Range_Constraint (C));
13101 else
13102 Range_Expr := Empty;
13103 end if;
13104 end if;
13106 Set_Etype (Def_Id, Base_Type (T));
13107 Set_Size_Info (Def_Id, (T));
13108 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13109 Set_Delta_Value (Def_Id, Delta_Value (T));
13110 Set_Scale_Value (Def_Id, Scale_Value (T));
13111 Set_Small_Value (Def_Id, Small_Value (T));
13112 Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
13113 Set_Digits_Value (Def_Id, Digits_Val);
13115 -- Manufacture range from given digits value if no range present
13117 if No (Range_Expr) then
13118 Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
13119 Range_Expr :=
13120 Make_Range (Loc,
13121 Low_Bound =>
13122 Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
13123 High_Bound =>
13124 Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
13125 end if;
13127 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
13128 Set_Discrete_RM_Size (Def_Id);
13130 -- Unconditionally delay the freeze, since we cannot set size
13131 -- information in all cases correctly until the freeze point.
13133 Set_Has_Delayed_Freeze (Def_Id);
13134 end Constrain_Decimal;
13136 ----------------------------------
13137 -- Constrain_Discriminated_Type --
13138 ----------------------------------
13140 procedure Constrain_Discriminated_Type
13141 (Def_Id : Entity_Id;
13142 S : Node_Id;
13143 Related_Nod : Node_Id;
13144 For_Access : Boolean := False)
13146 E : constant Entity_Id := Entity (Subtype_Mark (S));
13147 T : Entity_Id;
13148 C : Node_Id;
13149 Elist : Elist_Id := New_Elmt_List;
13151 procedure Fixup_Bad_Constraint;
13152 -- This is called after finding a bad constraint, and after having
13153 -- posted an appropriate error message. The mission is to leave the
13154 -- entity T in as reasonable state as possible.
13156 --------------------------
13157 -- Fixup_Bad_Constraint --
13158 --------------------------
13160 procedure Fixup_Bad_Constraint is
13161 begin
13162 -- Set a reasonable Ekind for the entity. For an incomplete type,
13163 -- we can't do much, but for other types, we can set the proper
13164 -- corresponding subtype kind.
13166 if Ekind (T) = E_Incomplete_Type then
13167 Set_Ekind (Def_Id, Ekind (T));
13168 else
13169 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
13170 end if;
13172 -- Set Etype to the known type, to reduce chances of cascaded errors
13174 Set_Etype (Def_Id, E);
13175 Set_Error_Posted (Def_Id);
13176 end Fixup_Bad_Constraint;
13178 -- Start of processing for Constrain_Discriminated_Type
13180 begin
13181 C := Constraint (S);
13183 -- A discriminant constraint is only allowed in a subtype indication,
13184 -- after a subtype mark. This subtype mark must denote either a type
13185 -- with discriminants, or an access type whose designated type is a
13186 -- type with discriminants. A discriminant constraint specifies the
13187 -- values of these discriminants (RM 3.7.2(5)).
13189 T := Base_Type (Entity (Subtype_Mark (S)));
13191 if Is_Access_Type (T) then
13192 T := Designated_Type (T);
13193 end if;
13195 -- Ada 2005 (AI-412): Constrained incomplete subtypes are illegal.
13196 -- Avoid generating an error for access-to-incomplete subtypes.
13198 if Ada_Version >= Ada_2005
13199 and then Ekind (T) = E_Incomplete_Type
13200 and then Nkind (Parent (S)) = N_Subtype_Declaration
13201 and then not Is_Itype (Def_Id)
13202 then
13203 -- A little sanity check, emit an error message if the type
13204 -- has discriminants to begin with. Type T may be a regular
13205 -- incomplete type or imported via a limited with clause.
13207 if Has_Discriminants (T)
13208 or else (From_Limited_With (T)
13209 and then Present (Non_Limited_View (T))
13210 and then Nkind (Parent (Non_Limited_View (T))) =
13211 N_Full_Type_Declaration
13212 and then Present (Discriminant_Specifications
13213 (Parent (Non_Limited_View (T)))))
13214 then
13215 Error_Msg_N
13216 ("(Ada 2005) incomplete subtype may not be constrained", C);
13217 else
13218 Error_Msg_N ("invalid constraint: type has no discriminant", C);
13219 end if;
13221 Fixup_Bad_Constraint;
13222 return;
13224 -- Check that the type has visible discriminants. The type may be
13225 -- a private type with unknown discriminants whose full view has
13226 -- discriminants which are invisible.
13228 elsif not Has_Discriminants (T)
13229 or else
13230 (Has_Unknown_Discriminants (T)
13231 and then Is_Private_Type (T))
13232 then
13233 Error_Msg_N ("invalid constraint: type has no discriminant", C);
13234 Fixup_Bad_Constraint;
13235 return;
13237 elsif Is_Constrained (E)
13238 or else (Ekind (E) = E_Class_Wide_Subtype
13239 and then Present (Discriminant_Constraint (E)))
13240 then
13241 Error_Msg_N ("type is already constrained", Subtype_Mark (S));
13242 Fixup_Bad_Constraint;
13243 return;
13244 end if;
13246 -- T may be an unconstrained subtype (e.g. a generic actual).
13247 -- Constraint applies to the base type.
13249 T := Base_Type (T);
13251 Elist := Build_Discriminant_Constraints (T, S);
13253 -- If the list returned was empty we had an error in building the
13254 -- discriminant constraint. We have also already signalled an error
13255 -- in the incomplete type case
13257 if Is_Empty_Elmt_List (Elist) then
13258 Fixup_Bad_Constraint;
13259 return;
13260 end if;
13262 Build_Discriminated_Subtype (T, Def_Id, Elist, Related_Nod, For_Access);
13263 end Constrain_Discriminated_Type;
13265 ---------------------------
13266 -- Constrain_Enumeration --
13267 ---------------------------
13269 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id) is
13270 T : constant Entity_Id := Entity (Subtype_Mark (S));
13271 C : constant Node_Id := Constraint (S);
13273 begin
13274 Set_Ekind (Def_Id, E_Enumeration_Subtype);
13276 Set_First_Literal (Def_Id, First_Literal (Base_Type (T)));
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_Is_Character_Type (Def_Id, Is_Character_Type (T));
13283 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13285 Set_Discrete_RM_Size (Def_Id);
13286 end Constrain_Enumeration;
13288 ----------------------
13289 -- Constrain_Float --
13290 ----------------------
13292 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id) is
13293 T : constant Entity_Id := Entity (Subtype_Mark (S));
13294 C : Node_Id;
13295 D : Node_Id;
13296 Rais : Node_Id;
13298 begin
13299 Set_Ekind (Def_Id, E_Floating_Point_Subtype);
13301 Set_Etype (Def_Id, Base_Type (T));
13302 Set_Size_Info (Def_Id, (T));
13303 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13305 -- Process the constraint
13307 C := Constraint (S);
13309 -- Digits constraint present
13311 if Nkind (C) = N_Digits_Constraint then
13313 Check_SPARK_05_Restriction ("digits constraint is not allowed", S);
13314 Check_Restriction (No_Obsolescent_Features, C);
13316 if Warn_On_Obsolescent_Feature then
13317 Error_Msg_N
13318 ("subtype digits constraint is an " &
13319 "obsolescent feature (RM J.3(8))?j?", C);
13320 end if;
13322 D := Digits_Expression (C);
13323 Analyze_And_Resolve (D, Any_Integer);
13324 Check_Digits_Expression (D);
13325 Set_Digits_Value (Def_Id, Expr_Value (D));
13327 -- Check that digits value is in range. Obviously we can do this
13328 -- at compile time, but it is strictly a runtime check, and of
13329 -- course there is an ACVC test that checks this.
13331 if Digits_Value (Def_Id) > Digits_Value (T) then
13332 Error_Msg_Uint_1 := Digits_Value (T);
13333 Error_Msg_N ("??digits value is too large, maximum is ^", D);
13334 Rais :=
13335 Make_Raise_Constraint_Error (Sloc (D),
13336 Reason => CE_Range_Check_Failed);
13337 Insert_Action (Declaration_Node (Def_Id), Rais);
13338 end if;
13340 C := Range_Constraint (C);
13342 -- No digits constraint present
13344 else
13345 Set_Digits_Value (Def_Id, Digits_Value (T));
13346 end if;
13348 -- Range constraint present
13350 if Nkind (C) = N_Range_Constraint then
13351 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13353 -- No range constraint present
13355 else
13356 pragma Assert (No (C));
13357 Set_Scalar_Range (Def_Id, Scalar_Range (T));
13358 end if;
13360 Set_Is_Constrained (Def_Id);
13361 end Constrain_Float;
13363 ---------------------
13364 -- Constrain_Index --
13365 ---------------------
13367 procedure Constrain_Index
13368 (Index : Node_Id;
13369 S : Node_Id;
13370 Related_Nod : Node_Id;
13371 Related_Id : Entity_Id;
13372 Suffix : Character;
13373 Suffix_Index : Nat)
13375 Def_Id : Entity_Id;
13376 R : Node_Id := Empty;
13377 T : constant Entity_Id := Etype (Index);
13379 begin
13380 Def_Id :=
13381 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
13382 Set_Etype (Def_Id, Base_Type (T));
13384 if Nkind (S) = N_Range
13385 or else
13386 (Nkind (S) = N_Attribute_Reference
13387 and then Attribute_Name (S) = Name_Range)
13388 then
13389 -- A Range attribute will be transformed into N_Range by Resolve
13391 Analyze (S);
13392 Set_Etype (S, T);
13393 R := S;
13395 Process_Range_Expr_In_Decl (R, T);
13397 if not Error_Posted (S)
13398 and then
13399 (Nkind (S) /= N_Range
13400 or else not Covers (T, (Etype (Low_Bound (S))))
13401 or else not Covers (T, (Etype (High_Bound (S)))))
13402 then
13403 if Base_Type (T) /= Any_Type
13404 and then Etype (Low_Bound (S)) /= Any_Type
13405 and then Etype (High_Bound (S)) /= Any_Type
13406 then
13407 Error_Msg_N ("range expected", S);
13408 end if;
13409 end if;
13411 elsif Nkind (S) = N_Subtype_Indication then
13413 -- The parser has verified that this is a discrete indication
13415 Resolve_Discrete_Subtype_Indication (S, T);
13416 Bad_Predicated_Subtype_Use
13417 ("subtype& has predicate, not allowed in index constraint",
13418 S, Entity (Subtype_Mark (S)));
13420 R := Range_Expression (Constraint (S));
13422 -- Capture values of bounds and generate temporaries for them if
13423 -- needed, since checks may cause duplication of the expressions
13424 -- which must not be reevaluated.
13426 -- The forced evaluation removes side effects from expressions, which
13427 -- should occur also in GNATprove mode. Otherwise, we end up with
13428 -- unexpected insertions of actions at places where this is not
13429 -- supposed to occur, e.g. on default parameters of a call.
13431 if Expander_Active or GNATprove_Mode then
13432 Force_Evaluation
13433 (Low_Bound (R), Related_Id => Def_Id, Is_Low_Bound => True);
13434 Force_Evaluation
13435 (High_Bound (R), Related_Id => Def_Id, Is_High_Bound => True);
13436 end if;
13438 elsif Nkind (S) = N_Discriminant_Association then
13440 -- Syntactically valid in subtype indication
13442 Error_Msg_N ("invalid index constraint", S);
13443 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
13444 return;
13446 -- Subtype_Mark case, no anonymous subtypes to construct
13448 else
13449 Analyze (S);
13451 if Is_Entity_Name (S) then
13452 if not Is_Type (Entity (S)) then
13453 Error_Msg_N ("expect subtype mark for index constraint", S);
13455 elsif Base_Type (Entity (S)) /= Base_Type (T) then
13456 Wrong_Type (S, Base_Type (T));
13458 -- Check error of subtype with predicate in index constraint
13460 else
13461 Bad_Predicated_Subtype_Use
13462 ("subtype& has predicate, not allowed in index constraint",
13463 S, Entity (S));
13464 end if;
13466 return;
13468 else
13469 Error_Msg_N ("invalid index constraint", S);
13470 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
13471 return;
13472 end if;
13473 end if;
13475 -- Complete construction of the Itype
13477 if Is_Modular_Integer_Type (T) then
13478 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
13480 elsif Is_Integer_Type (T) then
13481 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
13483 else
13484 Set_Ekind (Def_Id, E_Enumeration_Subtype);
13485 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
13486 Set_First_Literal (Def_Id, First_Literal (T));
13487 end if;
13489 Set_Size_Info (Def_Id, (T));
13490 Set_RM_Size (Def_Id, RM_Size (T));
13491 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13493 Set_Scalar_Range (Def_Id, R);
13495 Set_Etype (S, Def_Id);
13496 Set_Discrete_RM_Size (Def_Id);
13497 end Constrain_Index;
13499 -----------------------
13500 -- Constrain_Integer --
13501 -----------------------
13503 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id) is
13504 T : constant Entity_Id := Entity (Subtype_Mark (S));
13505 C : constant Node_Id := Constraint (S);
13507 begin
13508 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13510 if Is_Modular_Integer_Type (T) then
13511 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
13512 else
13513 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
13514 end if;
13516 Set_Etype (Def_Id, Base_Type (T));
13517 Set_Size_Info (Def_Id, (T));
13518 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13519 Set_Discrete_RM_Size (Def_Id);
13520 end Constrain_Integer;
13522 ------------------------------
13523 -- Constrain_Ordinary_Fixed --
13524 ------------------------------
13526 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id) is
13527 T : constant Entity_Id := Entity (Subtype_Mark (S));
13528 C : Node_Id;
13529 D : Node_Id;
13530 Rais : Node_Id;
13532 begin
13533 Set_Ekind (Def_Id, E_Ordinary_Fixed_Point_Subtype);
13534 Set_Etype (Def_Id, Base_Type (T));
13535 Set_Size_Info (Def_Id, (T));
13536 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13537 Set_Small_Value (Def_Id, Small_Value (T));
13539 -- Process the constraint
13541 C := Constraint (S);
13543 -- Delta constraint present
13545 if Nkind (C) = N_Delta_Constraint then
13547 Check_SPARK_05_Restriction ("delta constraint is not allowed", S);
13548 Check_Restriction (No_Obsolescent_Features, C);
13550 if Warn_On_Obsolescent_Feature then
13551 Error_Msg_S
13552 ("subtype delta constraint is an " &
13553 "obsolescent feature (RM J.3(7))?j?");
13554 end if;
13556 D := Delta_Expression (C);
13557 Analyze_And_Resolve (D, Any_Real);
13558 Check_Delta_Expression (D);
13559 Set_Delta_Value (Def_Id, Expr_Value_R (D));
13561 -- Check that delta value is in range. Obviously we can do this
13562 -- at compile time, but it is strictly a runtime check, and of
13563 -- course there is an ACVC test that checks this.
13565 if Delta_Value (Def_Id) < Delta_Value (T) then
13566 Error_Msg_N ("??delta value is too small", D);
13567 Rais :=
13568 Make_Raise_Constraint_Error (Sloc (D),
13569 Reason => CE_Range_Check_Failed);
13570 Insert_Action (Declaration_Node (Def_Id), Rais);
13571 end if;
13573 C := Range_Constraint (C);
13575 -- No delta constraint present
13577 else
13578 Set_Delta_Value (Def_Id, Delta_Value (T));
13579 end if;
13581 -- Range constraint present
13583 if Nkind (C) = N_Range_Constraint then
13584 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13586 -- No range constraint present
13588 else
13589 pragma Assert (No (C));
13590 Set_Scalar_Range (Def_Id, Scalar_Range (T));
13591 end if;
13593 Set_Discrete_RM_Size (Def_Id);
13595 -- Unconditionally delay the freeze, since we cannot set size
13596 -- information in all cases correctly until the freeze point.
13598 Set_Has_Delayed_Freeze (Def_Id);
13599 end Constrain_Ordinary_Fixed;
13601 -----------------------
13602 -- Contain_Interface --
13603 -----------------------
13605 function Contain_Interface
13606 (Iface : Entity_Id;
13607 Ifaces : Elist_Id) return Boolean
13609 Iface_Elmt : Elmt_Id;
13611 begin
13612 if Present (Ifaces) then
13613 Iface_Elmt := First_Elmt (Ifaces);
13614 while Present (Iface_Elmt) loop
13615 if Node (Iface_Elmt) = Iface then
13616 return True;
13617 end if;
13619 Next_Elmt (Iface_Elmt);
13620 end loop;
13621 end if;
13623 return False;
13624 end Contain_Interface;
13626 ---------------------------
13627 -- Convert_Scalar_Bounds --
13628 ---------------------------
13630 procedure Convert_Scalar_Bounds
13631 (N : Node_Id;
13632 Parent_Type : Entity_Id;
13633 Derived_Type : Entity_Id;
13634 Loc : Source_Ptr)
13636 Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
13638 Lo : Node_Id;
13639 Hi : Node_Id;
13640 Rng : Node_Id;
13642 begin
13643 -- Defend against previous errors
13645 if No (Scalar_Range (Derived_Type)) then
13646 Check_Error_Detected;
13647 return;
13648 end if;
13650 Lo := Build_Scalar_Bound
13651 (Type_Low_Bound (Derived_Type),
13652 Parent_Type, Implicit_Base);
13654 Hi := Build_Scalar_Bound
13655 (Type_High_Bound (Derived_Type),
13656 Parent_Type, Implicit_Base);
13658 Rng :=
13659 Make_Range (Loc,
13660 Low_Bound => Lo,
13661 High_Bound => Hi);
13663 Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
13665 Set_Parent (Rng, N);
13666 Set_Scalar_Range (Derived_Type, Rng);
13668 -- Analyze the bounds
13670 Analyze_And_Resolve (Lo, Implicit_Base);
13671 Analyze_And_Resolve (Hi, Implicit_Base);
13673 -- Analyze the range itself, except that we do not analyze it if
13674 -- the bounds are real literals, and we have a fixed-point type.
13675 -- The reason for this is that we delay setting the bounds in this
13676 -- case till we know the final Small and Size values (see circuit
13677 -- in Freeze.Freeze_Fixed_Point_Type for further details).
13679 if Is_Fixed_Point_Type (Parent_Type)
13680 and then Nkind (Lo) = N_Real_Literal
13681 and then Nkind (Hi) = N_Real_Literal
13682 then
13683 return;
13685 -- Here we do the analysis of the range
13687 -- Note: we do this manually, since if we do a normal Analyze and
13688 -- Resolve call, there are problems with the conversions used for
13689 -- the derived type range.
13691 else
13692 Set_Etype (Rng, Implicit_Base);
13693 Set_Analyzed (Rng, True);
13694 end if;
13695 end Convert_Scalar_Bounds;
13697 -------------------
13698 -- Copy_And_Swap --
13699 -------------------
13701 procedure Copy_And_Swap (Priv, Full : Entity_Id) is
13702 begin
13703 -- Initialize new full declaration entity by copying the pertinent
13704 -- fields of the corresponding private declaration entity.
13706 -- We temporarily set Ekind to a value appropriate for a type to
13707 -- avoid assert failures in Einfo from checking for setting type
13708 -- attributes on something that is not a type. Ekind (Priv) is an
13709 -- appropriate choice, since it allowed the attributes to be set
13710 -- in the first place. This Ekind value will be modified later.
13712 Set_Ekind (Full, Ekind (Priv));
13714 -- Also set Etype temporarily to Any_Type, again, in the absence
13715 -- of errors, it will be properly reset, and if there are errors,
13716 -- then we want a value of Any_Type to remain.
13718 Set_Etype (Full, Any_Type);
13720 -- Now start copying attributes
13722 Set_Has_Discriminants (Full, Has_Discriminants (Priv));
13724 if Has_Discriminants (Full) then
13725 Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
13726 Set_Stored_Constraint (Full, Stored_Constraint (Priv));
13727 end if;
13729 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
13730 Set_Homonym (Full, Homonym (Priv));
13731 Set_Is_Immediately_Visible (Full, Is_Immediately_Visible (Priv));
13732 Set_Is_Public (Full, Is_Public (Priv));
13733 Set_Is_Pure (Full, Is_Pure (Priv));
13734 Set_Is_Tagged_Type (Full, Is_Tagged_Type (Priv));
13735 Set_Has_Pragma_Unmodified (Full, Has_Pragma_Unmodified (Priv));
13736 Set_Has_Pragma_Unreferenced (Full, Has_Pragma_Unreferenced (Priv));
13737 Set_Has_Pragma_Unreferenced_Objects
13738 (Full, Has_Pragma_Unreferenced_Objects
13739 (Priv));
13741 Conditional_Delay (Full, Priv);
13743 if Is_Tagged_Type (Full) then
13744 Set_Direct_Primitive_Operations
13745 (Full, Direct_Primitive_Operations (Priv));
13746 Set_No_Tagged_Streams_Pragma
13747 (Full, No_Tagged_Streams_Pragma (Priv));
13749 if Is_Base_Type (Priv) then
13750 Set_Class_Wide_Type (Full, Class_Wide_Type (Priv));
13751 end if;
13752 end if;
13754 Set_Is_Volatile (Full, Is_Volatile (Priv));
13755 Set_Treat_As_Volatile (Full, Treat_As_Volatile (Priv));
13756 Set_Scope (Full, Scope (Priv));
13757 Set_Next_Entity (Full, Next_Entity (Priv));
13758 Set_First_Entity (Full, First_Entity (Priv));
13759 Set_Last_Entity (Full, Last_Entity (Priv));
13761 -- If access types have been recorded for later handling, keep them in
13762 -- the full view so that they get handled when the full view freeze
13763 -- node is expanded.
13765 if Present (Freeze_Node (Priv))
13766 and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
13767 then
13768 Ensure_Freeze_Node (Full);
13769 Set_Access_Types_To_Process
13770 (Freeze_Node (Full),
13771 Access_Types_To_Process (Freeze_Node (Priv)));
13772 end if;
13774 -- Swap the two entities. Now Private is the full type entity and Full
13775 -- is the private one. They will be swapped back at the end of the
13776 -- private part. This swapping ensures that the entity that is visible
13777 -- in the private part is the full declaration.
13779 Exchange_Entities (Priv, Full);
13780 Append_Entity (Full, Scope (Full));
13781 end Copy_And_Swap;
13783 -------------------------------------
13784 -- Copy_Array_Base_Type_Attributes --
13785 -------------------------------------
13787 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
13788 begin
13789 Set_Component_Alignment (T1, Component_Alignment (T2));
13790 Set_Component_Type (T1, Component_Type (T2));
13791 Set_Component_Size (T1, Component_Size (T2));
13792 Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
13793 Set_Has_Non_Standard_Rep (T1, Has_Non_Standard_Rep (T2));
13794 Set_Has_Protected (T1, Has_Protected (T2));
13795 Set_Has_Task (T1, Has_Task (T2));
13796 Set_Is_Packed (T1, Is_Packed (T2));
13797 Set_Has_Aliased_Components (T1, Has_Aliased_Components (T2));
13798 Set_Has_Atomic_Components (T1, Has_Atomic_Components (T2));
13799 Set_Has_Volatile_Components (T1, Has_Volatile_Components (T2));
13800 end Copy_Array_Base_Type_Attributes;
13802 -----------------------------------
13803 -- Copy_Array_Subtype_Attributes --
13804 -----------------------------------
13806 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
13807 begin
13808 Set_Size_Info (T1, T2);
13810 Set_First_Index (T1, First_Index (T2));
13811 Set_Is_Aliased (T1, Is_Aliased (T2));
13812 Set_Is_Volatile (T1, Is_Volatile (T2));
13813 Set_Treat_As_Volatile (T1, Treat_As_Volatile (T2));
13814 Set_Is_Constrained (T1, Is_Constrained (T2));
13815 Set_Depends_On_Private (T1, Has_Private_Component (T2));
13816 Inherit_Rep_Item_Chain (T1, T2);
13817 Set_Convention (T1, Convention (T2));
13818 Set_Is_Limited_Composite (T1, Is_Limited_Composite (T2));
13819 Set_Is_Private_Composite (T1, Is_Private_Composite (T2));
13820 Set_Packed_Array_Impl_Type (T1, Packed_Array_Impl_Type (T2));
13821 end Copy_Array_Subtype_Attributes;
13823 -----------------------------------
13824 -- Create_Constrained_Components --
13825 -----------------------------------
13827 procedure Create_Constrained_Components
13828 (Subt : Entity_Id;
13829 Decl_Node : Node_Id;
13830 Typ : Entity_Id;
13831 Constraints : Elist_Id)
13833 Loc : constant Source_Ptr := Sloc (Subt);
13834 Comp_List : constant Elist_Id := New_Elmt_List;
13835 Parent_Type : constant Entity_Id := Etype (Typ);
13836 Assoc_List : constant List_Id := New_List;
13837 Discr_Val : Elmt_Id;
13838 Errors : Boolean;
13839 New_C : Entity_Id;
13840 Old_C : Entity_Id;
13841 Is_Static : Boolean := True;
13843 procedure Collect_Fixed_Components (Typ : Entity_Id);
13844 -- Collect parent type components that do not appear in a variant part
13846 procedure Create_All_Components;
13847 -- Iterate over Comp_List to create the components of the subtype
13849 function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
13850 -- Creates a new component from Old_Compon, copying all the fields from
13851 -- it, including its Etype, inserts the new component in the Subt entity
13852 -- chain and returns the new component.
13854 function Is_Variant_Record (T : Entity_Id) return Boolean;
13855 -- If true, and discriminants are static, collect only components from
13856 -- variants selected by discriminant values.
13858 ------------------------------
13859 -- Collect_Fixed_Components --
13860 ------------------------------
13862 procedure Collect_Fixed_Components (Typ : Entity_Id) is
13863 begin
13864 -- Build association list for discriminants, and find components of the
13865 -- variant part selected by the values of the discriminants.
13867 Old_C := First_Discriminant (Typ);
13868 Discr_Val := First_Elmt (Constraints);
13869 while Present (Old_C) loop
13870 Append_To (Assoc_List,
13871 Make_Component_Association (Loc,
13872 Choices => New_List (New_Occurrence_Of (Old_C, Loc)),
13873 Expression => New_Copy (Node (Discr_Val))));
13875 Next_Elmt (Discr_Val);
13876 Next_Discriminant (Old_C);
13877 end loop;
13879 -- The tag and the possible parent component are unconditionally in
13880 -- the subtype.
13882 if Is_Tagged_Type (Typ) or else Has_Controlled_Component (Typ) then
13883 Old_C := First_Component (Typ);
13884 while Present (Old_C) loop
13885 if Nam_In (Chars (Old_C), Name_uTag, Name_uParent) then
13886 Append_Elmt (Old_C, Comp_List);
13887 end if;
13889 Next_Component (Old_C);
13890 end loop;
13891 end if;
13892 end Collect_Fixed_Components;
13894 ---------------------------
13895 -- Create_All_Components --
13896 ---------------------------
13898 procedure Create_All_Components is
13899 Comp : Elmt_Id;
13901 begin
13902 Comp := First_Elmt (Comp_List);
13903 while Present (Comp) loop
13904 Old_C := Node (Comp);
13905 New_C := Create_Component (Old_C);
13907 Set_Etype
13908 (New_C,
13909 Constrain_Component_Type
13910 (Old_C, Subt, Decl_Node, Typ, Constraints));
13911 Set_Is_Public (New_C, Is_Public (Subt));
13913 Next_Elmt (Comp);
13914 end loop;
13915 end Create_All_Components;
13917 ----------------------
13918 -- Create_Component --
13919 ----------------------
13921 function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
13922 New_Compon : constant Entity_Id := New_Copy (Old_Compon);
13924 begin
13925 if Ekind (Old_Compon) = E_Discriminant
13926 and then Is_Completely_Hidden (Old_Compon)
13927 then
13928 -- This is a shadow discriminant created for a discriminant of
13929 -- the parent type, which needs to be present in the subtype.
13930 -- Give the shadow discriminant an internal name that cannot
13931 -- conflict with that of visible components.
13933 Set_Chars (New_Compon, New_Internal_Name ('C'));
13934 end if;
13936 -- Set the parent so we have a proper link for freezing etc. This is
13937 -- not a real parent pointer, since of course our parent does not own
13938 -- up to us and reference us, we are an illegitimate child of the
13939 -- original parent.
13941 Set_Parent (New_Compon, Parent (Old_Compon));
13943 -- If the old component's Esize was already determined and is a
13944 -- static value, then the new component simply inherits it. Otherwise
13945 -- the old component's size may require run-time determination, but
13946 -- the new component's size still might be statically determinable
13947 -- (if, for example it has a static constraint). In that case we want
13948 -- Layout_Type to recompute the component's size, so we reset its
13949 -- size and positional fields.
13951 if Frontend_Layout_On_Target
13952 and then not Known_Static_Esize (Old_Compon)
13953 then
13954 Set_Esize (New_Compon, Uint_0);
13955 Init_Normalized_First_Bit (New_Compon);
13956 Init_Normalized_Position (New_Compon);
13957 Init_Normalized_Position_Max (New_Compon);
13958 end if;
13960 -- We do not want this node marked as Comes_From_Source, since
13961 -- otherwise it would get first class status and a separate cross-
13962 -- reference line would be generated. Illegitimate children do not
13963 -- rate such recognition.
13965 Set_Comes_From_Source (New_Compon, False);
13967 -- But it is a real entity, and a birth certificate must be properly
13968 -- registered by entering it into the entity list.
13970 Enter_Name (New_Compon);
13972 return New_Compon;
13973 end Create_Component;
13975 -----------------------
13976 -- Is_Variant_Record --
13977 -----------------------
13979 function Is_Variant_Record (T : Entity_Id) return Boolean is
13980 begin
13981 return Nkind (Parent (T)) = N_Full_Type_Declaration
13982 and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
13983 and then Present (Component_List (Type_Definition (Parent (T))))
13984 and then
13985 Present
13986 (Variant_Part (Component_List (Type_Definition (Parent (T)))));
13987 end Is_Variant_Record;
13989 -- Start of processing for Create_Constrained_Components
13991 begin
13992 pragma Assert (Subt /= Base_Type (Subt));
13993 pragma Assert (Typ = Base_Type (Typ));
13995 Set_First_Entity (Subt, Empty);
13996 Set_Last_Entity (Subt, Empty);
13998 -- Check whether constraint is fully static, in which case we can
13999 -- optimize the list of components.
14001 Discr_Val := First_Elmt (Constraints);
14002 while Present (Discr_Val) loop
14003 if not Is_OK_Static_Expression (Node (Discr_Val)) then
14004 Is_Static := False;
14005 exit;
14006 end if;
14008 Next_Elmt (Discr_Val);
14009 end loop;
14011 Set_Has_Static_Discriminants (Subt, Is_Static);
14013 Push_Scope (Subt);
14015 -- Inherit the discriminants of the parent type
14017 Add_Discriminants : declare
14018 Num_Disc : Int;
14019 Num_Gird : Int;
14021 begin
14022 Num_Disc := 0;
14023 Old_C := First_Discriminant (Typ);
14025 while Present (Old_C) loop
14026 Num_Disc := Num_Disc + 1;
14027 New_C := Create_Component (Old_C);
14028 Set_Is_Public (New_C, Is_Public (Subt));
14029 Next_Discriminant (Old_C);
14030 end loop;
14032 -- For an untagged derived subtype, the number of discriminants may
14033 -- be smaller than the number of inherited discriminants, because
14034 -- several of them may be renamed by a single new discriminant or
14035 -- constrained. In this case, add the hidden discriminants back into
14036 -- the subtype, because they need to be present if the optimizer of
14037 -- the GCC 4.x back-end decides to break apart assignments between
14038 -- objects using the parent view into member-wise assignments.
14040 Num_Gird := 0;
14042 if Is_Derived_Type (Typ)
14043 and then not Is_Tagged_Type (Typ)
14044 then
14045 Old_C := First_Stored_Discriminant (Typ);
14047 while Present (Old_C) loop
14048 Num_Gird := Num_Gird + 1;
14049 Next_Stored_Discriminant (Old_C);
14050 end loop;
14051 end if;
14053 if Num_Gird > Num_Disc then
14055 -- Find out multiple uses of new discriminants, and add hidden
14056 -- components for the extra renamed discriminants. We recognize
14057 -- multiple uses through the Corresponding_Discriminant of a
14058 -- new discriminant: if it constrains several old discriminants,
14059 -- this field points to the last one in the parent type. The
14060 -- stored discriminants of the derived type have the same name
14061 -- as those of the parent.
14063 declare
14064 Constr : Elmt_Id;
14065 New_Discr : Entity_Id;
14066 Old_Discr : Entity_Id;
14068 begin
14069 Constr := First_Elmt (Stored_Constraint (Typ));
14070 Old_Discr := First_Stored_Discriminant (Typ);
14071 while Present (Constr) loop
14072 if Is_Entity_Name (Node (Constr))
14073 and then Ekind (Entity (Node (Constr))) = E_Discriminant
14074 then
14075 New_Discr := Entity (Node (Constr));
14077 if Chars (Corresponding_Discriminant (New_Discr)) /=
14078 Chars (Old_Discr)
14079 then
14080 -- The new discriminant has been used to rename a
14081 -- subsequent old discriminant. Introduce a shadow
14082 -- component for the current old discriminant.
14084 New_C := Create_Component (Old_Discr);
14085 Set_Original_Record_Component (New_C, Old_Discr);
14086 end if;
14088 else
14089 -- The constraint has eliminated the old discriminant.
14090 -- Introduce a shadow component.
14092 New_C := Create_Component (Old_Discr);
14093 Set_Original_Record_Component (New_C, Old_Discr);
14094 end if;
14096 Next_Elmt (Constr);
14097 Next_Stored_Discriminant (Old_Discr);
14098 end loop;
14099 end;
14100 end if;
14101 end Add_Discriminants;
14103 if Is_Static
14104 and then Is_Variant_Record (Typ)
14105 then
14106 Collect_Fixed_Components (Typ);
14108 Gather_Components (
14109 Typ,
14110 Component_List (Type_Definition (Parent (Typ))),
14111 Governed_By => Assoc_List,
14112 Into => Comp_List,
14113 Report_Errors => Errors);
14114 pragma Assert (not Errors);
14116 Create_All_Components;
14118 -- If the subtype declaration is created for a tagged type derivation
14119 -- with constraints, we retrieve the record definition of the parent
14120 -- type to select the components of the proper variant.
14122 elsif Is_Static
14123 and then Is_Tagged_Type (Typ)
14124 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
14125 and then
14126 Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
14127 and then Is_Variant_Record (Parent_Type)
14128 then
14129 Collect_Fixed_Components (Typ);
14131 Gather_Components
14132 (Typ,
14133 Component_List (Type_Definition (Parent (Parent_Type))),
14134 Governed_By => Assoc_List,
14135 Into => Comp_List,
14136 Report_Errors => Errors);
14138 -- Note: previously there was a check at this point that no errors
14139 -- were detected. As a consequence of AI05-220 there may be an error
14140 -- if an inherited discriminant that controls a variant has a non-
14141 -- static constraint.
14143 -- If the tagged derivation has a type extension, collect all the
14144 -- new components therein.
14146 if Present (Record_Extension_Part (Type_Definition (Parent (Typ))))
14147 then
14148 Old_C := First_Component (Typ);
14149 while Present (Old_C) loop
14150 if Original_Record_Component (Old_C) = Old_C
14151 and then Chars (Old_C) /= Name_uTag
14152 and then Chars (Old_C) /= Name_uParent
14153 then
14154 Append_Elmt (Old_C, Comp_List);
14155 end if;
14157 Next_Component (Old_C);
14158 end loop;
14159 end if;
14161 Create_All_Components;
14163 else
14164 -- If discriminants are not static, or if this is a multi-level type
14165 -- extension, we have to include all components of the parent type.
14167 Old_C := First_Component (Typ);
14168 while Present (Old_C) loop
14169 New_C := Create_Component (Old_C);
14171 Set_Etype
14172 (New_C,
14173 Constrain_Component_Type
14174 (Old_C, Subt, Decl_Node, Typ, Constraints));
14175 Set_Is_Public (New_C, Is_Public (Subt));
14177 Next_Component (Old_C);
14178 end loop;
14179 end if;
14181 End_Scope;
14182 end Create_Constrained_Components;
14184 ------------------------------------------
14185 -- Decimal_Fixed_Point_Type_Declaration --
14186 ------------------------------------------
14188 procedure Decimal_Fixed_Point_Type_Declaration
14189 (T : Entity_Id;
14190 Def : Node_Id)
14192 Loc : constant Source_Ptr := Sloc (Def);
14193 Digs_Expr : constant Node_Id := Digits_Expression (Def);
14194 Delta_Expr : constant Node_Id := Delta_Expression (Def);
14195 Implicit_Base : Entity_Id;
14196 Digs_Val : Uint;
14197 Delta_Val : Ureal;
14198 Scale_Val : Uint;
14199 Bound_Val : Ureal;
14201 begin
14202 Check_SPARK_05_Restriction
14203 ("decimal fixed point type is not allowed", Def);
14204 Check_Restriction (No_Fixed_Point, Def);
14206 -- Create implicit base type
14208 Implicit_Base :=
14209 Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
14210 Set_Etype (Implicit_Base, Implicit_Base);
14212 -- Analyze and process delta expression
14214 Analyze_And_Resolve (Delta_Expr, Universal_Real);
14216 Check_Delta_Expression (Delta_Expr);
14217 Delta_Val := Expr_Value_R (Delta_Expr);
14219 -- Check delta is power of 10, and determine scale value from it
14221 declare
14222 Val : Ureal;
14224 begin
14225 Scale_Val := Uint_0;
14226 Val := Delta_Val;
14228 if Val < Ureal_1 then
14229 while Val < Ureal_1 loop
14230 Val := Val * Ureal_10;
14231 Scale_Val := Scale_Val + 1;
14232 end loop;
14234 if Scale_Val > 18 then
14235 Error_Msg_N ("scale exceeds maximum value of 18", Def);
14236 Scale_Val := UI_From_Int (+18);
14237 end if;
14239 else
14240 while Val > Ureal_1 loop
14241 Val := Val / Ureal_10;
14242 Scale_Val := Scale_Val - 1;
14243 end loop;
14245 if Scale_Val < -18 then
14246 Error_Msg_N ("scale is less than minimum value of -18", Def);
14247 Scale_Val := UI_From_Int (-18);
14248 end if;
14249 end if;
14251 if Val /= Ureal_1 then
14252 Error_Msg_N ("delta expression must be a power of 10", Def);
14253 Delta_Val := Ureal_10 ** (-Scale_Val);
14254 end if;
14255 end;
14257 -- Set delta, scale and small (small = delta for decimal type)
14259 Set_Delta_Value (Implicit_Base, Delta_Val);
14260 Set_Scale_Value (Implicit_Base, Scale_Val);
14261 Set_Small_Value (Implicit_Base, Delta_Val);
14263 -- Analyze and process digits expression
14265 Analyze_And_Resolve (Digs_Expr, Any_Integer);
14266 Check_Digits_Expression (Digs_Expr);
14267 Digs_Val := Expr_Value (Digs_Expr);
14269 if Digs_Val > 18 then
14270 Digs_Val := UI_From_Int (+18);
14271 Error_Msg_N ("digits value out of range, maximum is 18", Digs_Expr);
14272 end if;
14274 Set_Digits_Value (Implicit_Base, Digs_Val);
14275 Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
14277 -- Set range of base type from digits value for now. This will be
14278 -- expanded to represent the true underlying base range by Freeze.
14280 Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
14282 -- Note: We leave size as zero for now, size will be set at freeze
14283 -- time. We have to do this for ordinary fixed-point, because the size
14284 -- depends on the specified small, and we might as well do the same for
14285 -- decimal fixed-point.
14287 pragma Assert (Esize (Implicit_Base) = Uint_0);
14289 -- If there are bounds given in the declaration use them as the
14290 -- bounds of the first named subtype.
14292 if Present (Real_Range_Specification (Def)) then
14293 declare
14294 RRS : constant Node_Id := Real_Range_Specification (Def);
14295 Low : constant Node_Id := Low_Bound (RRS);
14296 High : constant Node_Id := High_Bound (RRS);
14297 Low_Val : Ureal;
14298 High_Val : Ureal;
14300 begin
14301 Analyze_And_Resolve (Low, Any_Real);
14302 Analyze_And_Resolve (High, Any_Real);
14303 Check_Real_Bound (Low);
14304 Check_Real_Bound (High);
14305 Low_Val := Expr_Value_R (Low);
14306 High_Val := Expr_Value_R (High);
14308 if Low_Val < (-Bound_Val) then
14309 Error_Msg_N
14310 ("range low bound too small for digits value", Low);
14311 Low_Val := -Bound_Val;
14312 end if;
14314 if High_Val > Bound_Val then
14315 Error_Msg_N
14316 ("range high bound too large for digits value", High);
14317 High_Val := Bound_Val;
14318 end if;
14320 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
14321 end;
14323 -- If no explicit range, use range that corresponds to given
14324 -- digits value. This will end up as the final range for the
14325 -- first subtype.
14327 else
14328 Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
14329 end if;
14331 -- Complete entity for first subtype. The inheritance of the rep item
14332 -- chain ensures that SPARK-related pragmas are not clobbered when the
14333 -- decimal fixed point type acts as a full view of a private type.
14335 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
14336 Set_Etype (T, Implicit_Base);
14337 Set_Size_Info (T, Implicit_Base);
14338 Inherit_Rep_Item_Chain (T, Implicit_Base);
14339 Set_Digits_Value (T, Digs_Val);
14340 Set_Delta_Value (T, Delta_Val);
14341 Set_Small_Value (T, Delta_Val);
14342 Set_Scale_Value (T, Scale_Val);
14343 Set_Is_Constrained (T);
14344 end Decimal_Fixed_Point_Type_Declaration;
14346 -----------------------------------
14347 -- Derive_Progenitor_Subprograms --
14348 -----------------------------------
14350 procedure Derive_Progenitor_Subprograms
14351 (Parent_Type : Entity_Id;
14352 Tagged_Type : Entity_Id)
14354 E : Entity_Id;
14355 Elmt : Elmt_Id;
14356 Iface : Entity_Id;
14357 Iface_Elmt : Elmt_Id;
14358 Iface_Subp : Entity_Id;
14359 New_Subp : Entity_Id := Empty;
14360 Prim_Elmt : Elmt_Id;
14361 Subp : Entity_Id;
14362 Typ : Entity_Id;
14364 begin
14365 pragma Assert (Ada_Version >= Ada_2005
14366 and then Is_Record_Type (Tagged_Type)
14367 and then Is_Tagged_Type (Tagged_Type)
14368 and then Has_Interfaces (Tagged_Type));
14370 -- Step 1: Transfer to the full-view primitives associated with the
14371 -- partial-view that cover interface primitives. Conceptually this
14372 -- work should be done later by Process_Full_View; done here to
14373 -- simplify its implementation at later stages. It can be safely
14374 -- done here because interfaces must be visible in the partial and
14375 -- private view (RM 7.3(7.3/2)).
14377 -- Small optimization: This work is only required if the parent may
14378 -- have entities whose Alias attribute reference an interface primitive.
14379 -- Such a situation may occur if the parent is an abstract type and the
14380 -- primitive has not been yet overridden or if the parent is a generic
14381 -- formal type covering interfaces.
14383 -- If the tagged type is not abstract, it cannot have abstract
14384 -- primitives (the only entities in the list of primitives of
14385 -- non-abstract tagged types that can reference abstract primitives
14386 -- through its Alias attribute are the internal entities that have
14387 -- attribute Interface_Alias, and these entities are generated later
14388 -- by Add_Internal_Interface_Entities).
14390 if In_Private_Part (Current_Scope)
14391 and then (Is_Abstract_Type (Parent_Type)
14392 or else
14393 Is_Generic_Type (Parent_Type))
14394 then
14395 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
14396 while Present (Elmt) loop
14397 Subp := Node (Elmt);
14399 -- At this stage it is not possible to have entities in the list
14400 -- of primitives that have attribute Interface_Alias.
14402 pragma Assert (No (Interface_Alias (Subp)));
14404 Typ := Find_Dispatching_Type (Ultimate_Alias (Subp));
14406 if Is_Interface (Typ) then
14407 E := Find_Primitive_Covering_Interface
14408 (Tagged_Type => Tagged_Type,
14409 Iface_Prim => Subp);
14411 if Present (E)
14412 and then Find_Dispatching_Type (Ultimate_Alias (E)) /= Typ
14413 then
14414 Replace_Elmt (Elmt, E);
14415 Remove_Homonym (Subp);
14416 end if;
14417 end if;
14419 Next_Elmt (Elmt);
14420 end loop;
14421 end if;
14423 -- Step 2: Add primitives of progenitors that are not implemented by
14424 -- parents of Tagged_Type.
14426 if Present (Interfaces (Base_Type (Tagged_Type))) then
14427 Iface_Elmt := First_Elmt (Interfaces (Base_Type (Tagged_Type)));
14428 while Present (Iface_Elmt) loop
14429 Iface := Node (Iface_Elmt);
14431 Prim_Elmt := First_Elmt (Primitive_Operations (Iface));
14432 while Present (Prim_Elmt) loop
14433 Iface_Subp := Node (Prim_Elmt);
14435 -- Exclude derivation of predefined primitives except those
14436 -- that come from source, or are inherited from one that comes
14437 -- from source. Required to catch declarations of equality
14438 -- operators of interfaces. For example:
14440 -- type Iface is interface;
14441 -- function "=" (Left, Right : Iface) return Boolean;
14443 if not Is_Predefined_Dispatching_Operation (Iface_Subp)
14444 or else Comes_From_Source (Ultimate_Alias (Iface_Subp))
14445 then
14446 E := Find_Primitive_Covering_Interface
14447 (Tagged_Type => Tagged_Type,
14448 Iface_Prim => Iface_Subp);
14450 -- If not found we derive a new primitive leaving its alias
14451 -- attribute referencing the interface primitive.
14453 if No (E) then
14454 Derive_Subprogram
14455 (New_Subp, Iface_Subp, Tagged_Type, Iface);
14457 -- Ada 2012 (AI05-0197): If the covering primitive's name
14458 -- differs from the name of the interface primitive then it
14459 -- is a private primitive inherited from a parent type. In
14460 -- such case, given that Tagged_Type covers the interface,
14461 -- the inherited private primitive becomes visible. For such
14462 -- purpose we add a new entity that renames the inherited
14463 -- private primitive.
14465 elsif Chars (E) /= Chars (Iface_Subp) then
14466 pragma Assert (Has_Suffix (E, 'P'));
14467 Derive_Subprogram
14468 (New_Subp, Iface_Subp, Tagged_Type, Iface);
14469 Set_Alias (New_Subp, E);
14470 Set_Is_Abstract_Subprogram (New_Subp,
14471 Is_Abstract_Subprogram (E));
14473 -- Propagate to the full view interface entities associated
14474 -- with the partial view.
14476 elsif In_Private_Part (Current_Scope)
14477 and then Present (Alias (E))
14478 and then Alias (E) = Iface_Subp
14479 and then
14480 List_Containing (Parent (E)) /=
14481 Private_Declarations
14482 (Specification
14483 (Unit_Declaration_Node (Current_Scope)))
14484 then
14485 Append_Elmt (E, Primitive_Operations (Tagged_Type));
14486 end if;
14487 end if;
14489 Next_Elmt (Prim_Elmt);
14490 end loop;
14492 Next_Elmt (Iface_Elmt);
14493 end loop;
14494 end if;
14495 end Derive_Progenitor_Subprograms;
14497 -----------------------
14498 -- Derive_Subprogram --
14499 -----------------------
14501 procedure Derive_Subprogram
14502 (New_Subp : in out Entity_Id;
14503 Parent_Subp : Entity_Id;
14504 Derived_Type : Entity_Id;
14505 Parent_Type : Entity_Id;
14506 Actual_Subp : Entity_Id := Empty)
14508 Formal : Entity_Id;
14509 -- Formal parameter of parent primitive operation
14511 Formal_Of_Actual : Entity_Id;
14512 -- Formal parameter of actual operation, when the derivation is to
14513 -- create a renaming for a primitive operation of an actual in an
14514 -- instantiation.
14516 New_Formal : Entity_Id;
14517 -- Formal of inherited operation
14519 Visible_Subp : Entity_Id := Parent_Subp;
14521 function Is_Private_Overriding return Boolean;
14522 -- If Subp is a private overriding of a visible operation, the inherited
14523 -- operation derives from the overridden op (even though its body is the
14524 -- overriding one) and the inherited operation is visible now. See
14525 -- sem_disp to see the full details of the handling of the overridden
14526 -- subprogram, which is removed from the list of primitive operations of
14527 -- the type. The overridden subprogram is saved locally in Visible_Subp,
14528 -- and used to diagnose abstract operations that need overriding in the
14529 -- derived type.
14531 procedure Replace_Type (Id, New_Id : Entity_Id);
14532 -- When the type is an anonymous access type, create a new access type
14533 -- designating the derived type.
14535 procedure Set_Derived_Name;
14536 -- This procedure sets the appropriate Chars name for New_Subp. This
14537 -- is normally just a copy of the parent name. An exception arises for
14538 -- type support subprograms, where the name is changed to reflect the
14539 -- name of the derived type, e.g. if type foo is derived from type bar,
14540 -- then a procedure barDA is derived with a name fooDA.
14542 ---------------------------
14543 -- Is_Private_Overriding --
14544 ---------------------------
14546 function Is_Private_Overriding return Boolean is
14547 Prev : Entity_Id;
14549 begin
14550 -- If the parent is not a dispatching operation there is no
14551 -- need to investigate overridings
14553 if not Is_Dispatching_Operation (Parent_Subp) then
14554 return False;
14555 end if;
14557 -- The visible operation that is overridden is a homonym of the
14558 -- parent subprogram. We scan the homonym chain to find the one
14559 -- whose alias is the subprogram we are deriving.
14561 Prev := Current_Entity (Parent_Subp);
14562 while Present (Prev) loop
14563 if Ekind (Prev) = Ekind (Parent_Subp)
14564 and then Alias (Prev) = Parent_Subp
14565 and then Scope (Parent_Subp) = Scope (Prev)
14566 and then not Is_Hidden (Prev)
14567 then
14568 Visible_Subp := Prev;
14569 return True;
14570 end if;
14572 Prev := Homonym (Prev);
14573 end loop;
14575 return False;
14576 end Is_Private_Overriding;
14578 ------------------
14579 -- Replace_Type --
14580 ------------------
14582 procedure Replace_Type (Id, New_Id : Entity_Id) is
14583 Id_Type : constant Entity_Id := Etype (Id);
14584 Acc_Type : Entity_Id;
14585 Par : constant Node_Id := Parent (Derived_Type);
14587 begin
14588 -- When the type is an anonymous access type, create a new access
14589 -- type designating the derived type. This itype must be elaborated
14590 -- at the point of the derivation, not on subsequent calls that may
14591 -- be out of the proper scope for Gigi, so we insert a reference to
14592 -- it after the derivation.
14594 if Ekind (Id_Type) = E_Anonymous_Access_Type then
14595 declare
14596 Desig_Typ : Entity_Id := Designated_Type (Id_Type);
14598 begin
14599 if Ekind (Desig_Typ) = E_Record_Type_With_Private
14600 and then Present (Full_View (Desig_Typ))
14601 and then not Is_Private_Type (Parent_Type)
14602 then
14603 Desig_Typ := Full_View (Desig_Typ);
14604 end if;
14606 if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
14608 -- Ada 2005 (AI-251): Handle also derivations of abstract
14609 -- interface primitives.
14611 or else (Is_Interface (Desig_Typ)
14612 and then not Is_Class_Wide_Type (Desig_Typ))
14613 then
14614 Acc_Type := New_Copy (Id_Type);
14615 Set_Etype (Acc_Type, Acc_Type);
14616 Set_Scope (Acc_Type, New_Subp);
14618 -- Set size of anonymous access type. If we have an access
14619 -- to an unconstrained array, this is a fat pointer, so it
14620 -- is sizes at twice addtress size.
14622 if Is_Array_Type (Desig_Typ)
14623 and then not Is_Constrained (Desig_Typ)
14624 then
14625 Init_Size (Acc_Type, 2 * System_Address_Size);
14627 -- Other cases use a thin pointer
14629 else
14630 Init_Size (Acc_Type, System_Address_Size);
14631 end if;
14633 -- Set remaining characterstics of anonymous access type
14635 Init_Alignment (Acc_Type);
14636 Set_Directly_Designated_Type (Acc_Type, Derived_Type);
14638 Set_Etype (New_Id, Acc_Type);
14639 Set_Scope (New_Id, New_Subp);
14641 -- Create a reference to it
14643 Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
14645 else
14646 Set_Etype (New_Id, Id_Type);
14647 end if;
14648 end;
14650 -- In Ada2012, a formal may have an incomplete type but the type
14651 -- derivation that inherits the primitive follows the full view.
14653 elsif Base_Type (Id_Type) = Base_Type (Parent_Type)
14654 or else
14655 (Ekind (Id_Type) = E_Record_Type_With_Private
14656 and then Present (Full_View (Id_Type))
14657 and then
14658 Base_Type (Full_View (Id_Type)) = Base_Type (Parent_Type))
14659 or else
14660 (Ada_Version >= Ada_2012
14661 and then Ekind (Id_Type) = E_Incomplete_Type
14662 and then Full_View (Id_Type) = Parent_Type)
14663 then
14664 -- Constraint checks on formals are generated during expansion,
14665 -- based on the signature of the original subprogram. The bounds
14666 -- of the derived type are not relevant, and thus we can use
14667 -- the base type for the formals. However, the return type may be
14668 -- used in a context that requires that the proper static bounds
14669 -- be used (a case statement, for example) and for those cases
14670 -- we must use the derived type (first subtype), not its base.
14672 -- If the derived_type_definition has no constraints, we know that
14673 -- the derived type has the same constraints as the first subtype
14674 -- of the parent, and we can also use it rather than its base,
14675 -- which can lead to more efficient code.
14677 if Etype (Id) = Parent_Type then
14678 if Is_Scalar_Type (Parent_Type)
14679 and then
14680 Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
14681 then
14682 Set_Etype (New_Id, Derived_Type);
14684 elsif Nkind (Par) = N_Full_Type_Declaration
14685 and then
14686 Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
14687 and then
14688 Is_Entity_Name
14689 (Subtype_Indication (Type_Definition (Par)))
14690 then
14691 Set_Etype (New_Id, Derived_Type);
14693 else
14694 Set_Etype (New_Id, Base_Type (Derived_Type));
14695 end if;
14697 else
14698 Set_Etype (New_Id, Base_Type (Derived_Type));
14699 end if;
14701 else
14702 Set_Etype (New_Id, Etype (Id));
14703 end if;
14704 end Replace_Type;
14706 ----------------------
14707 -- Set_Derived_Name --
14708 ----------------------
14710 procedure Set_Derived_Name is
14711 Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
14712 begin
14713 if Nm = TSS_Null then
14714 Set_Chars (New_Subp, Chars (Parent_Subp));
14715 else
14716 Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
14717 end if;
14718 end Set_Derived_Name;
14720 -- Start of processing for Derive_Subprogram
14722 begin
14723 New_Subp := New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
14724 Set_Ekind (New_Subp, Ekind (Parent_Subp));
14726 -- Check whether the inherited subprogram is a private operation that
14727 -- should be inherited but not yet made visible. Such subprograms can
14728 -- become visible at a later point (e.g., the private part of a public
14729 -- child unit) via Declare_Inherited_Private_Subprograms. If the
14730 -- following predicate is true, then this is not such a private
14731 -- operation and the subprogram simply inherits the name of the parent
14732 -- subprogram. Note the special check for the names of controlled
14733 -- operations, which are currently exempted from being inherited with
14734 -- a hidden name because they must be findable for generation of
14735 -- implicit run-time calls.
14737 if not Is_Hidden (Parent_Subp)
14738 or else Is_Internal (Parent_Subp)
14739 or else Is_Private_Overriding
14740 or else Is_Internal_Name (Chars (Parent_Subp))
14741 or else Nam_In (Chars (Parent_Subp), Name_Initialize,
14742 Name_Adjust,
14743 Name_Finalize)
14744 then
14745 Set_Derived_Name;
14747 -- An inherited dispatching equality will be overridden by an internally
14748 -- generated one, or by an explicit one, so preserve its name and thus
14749 -- its entry in the dispatch table. Otherwise, if Parent_Subp is a
14750 -- private operation it may become invisible if the full view has
14751 -- progenitors, and the dispatch table will be malformed.
14752 -- We check that the type is limited to handle the anomalous declaration
14753 -- of Limited_Controlled, which is derived from a non-limited type, and
14754 -- which is handled specially elsewhere as well.
14756 elsif Chars (Parent_Subp) = Name_Op_Eq
14757 and then Is_Dispatching_Operation (Parent_Subp)
14758 and then Etype (Parent_Subp) = Standard_Boolean
14759 and then not Is_Limited_Type (Etype (First_Formal (Parent_Subp)))
14760 and then
14761 Etype (First_Formal (Parent_Subp)) =
14762 Etype (Next_Formal (First_Formal (Parent_Subp)))
14763 then
14764 Set_Derived_Name;
14766 -- If parent is hidden, this can be a regular derivation if the
14767 -- parent is immediately visible in a non-instantiating context,
14768 -- or if we are in the private part of an instance. This test
14769 -- should still be refined ???
14771 -- The test for In_Instance_Not_Visible avoids inheriting the derived
14772 -- operation as a non-visible operation in cases where the parent
14773 -- subprogram might not be visible now, but was visible within the
14774 -- original generic, so it would be wrong to make the inherited
14775 -- subprogram non-visible now. (Not clear if this test is fully
14776 -- correct; are there any cases where we should declare the inherited
14777 -- operation as not visible to avoid it being overridden, e.g., when
14778 -- the parent type is a generic actual with private primitives ???)
14780 -- (they should be treated the same as other private inherited
14781 -- subprograms, but it's not clear how to do this cleanly). ???
14783 elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
14784 and then Is_Immediately_Visible (Parent_Subp)
14785 and then not In_Instance)
14786 or else In_Instance_Not_Visible
14787 then
14788 Set_Derived_Name;
14790 -- Ada 2005 (AI-251): Regular derivation if the parent subprogram
14791 -- overrides an interface primitive because interface primitives
14792 -- must be visible in the partial view of the parent (RM 7.3 (7.3/2))
14794 elsif Ada_Version >= Ada_2005
14795 and then Is_Dispatching_Operation (Parent_Subp)
14796 and then Covers_Some_Interface (Parent_Subp)
14797 then
14798 Set_Derived_Name;
14800 -- Otherwise, the type is inheriting a private operation, so enter
14801 -- it with a special name so it can't be overridden.
14803 else
14804 Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
14805 end if;
14807 Set_Parent (New_Subp, Parent (Derived_Type));
14809 if Present (Actual_Subp) then
14810 Replace_Type (Actual_Subp, New_Subp);
14811 else
14812 Replace_Type (Parent_Subp, New_Subp);
14813 end if;
14815 Conditional_Delay (New_Subp, Parent_Subp);
14817 -- If we are creating a renaming for a primitive operation of an
14818 -- actual of a generic derived type, we must examine the signature
14819 -- of the actual primitive, not that of the generic formal, which for
14820 -- example may be an interface. However the name and initial value
14821 -- of the inherited operation are those of the formal primitive.
14823 Formal := First_Formal (Parent_Subp);
14825 if Present (Actual_Subp) then
14826 Formal_Of_Actual := First_Formal (Actual_Subp);
14827 else
14828 Formal_Of_Actual := Empty;
14829 end if;
14831 while Present (Formal) loop
14832 New_Formal := New_Copy (Formal);
14834 -- Normally we do not go copying parents, but in the case of
14835 -- formals, we need to link up to the declaration (which is the
14836 -- parameter specification), and it is fine to link up to the
14837 -- original formal's parameter specification in this case.
14839 Set_Parent (New_Formal, Parent (Formal));
14840 Append_Entity (New_Formal, New_Subp);
14842 if Present (Formal_Of_Actual) then
14843 Replace_Type (Formal_Of_Actual, New_Formal);
14844 Next_Formal (Formal_Of_Actual);
14845 else
14846 Replace_Type (Formal, New_Formal);
14847 end if;
14849 Next_Formal (Formal);
14850 end loop;
14852 -- If this derivation corresponds to a tagged generic actual, then
14853 -- primitive operations rename those of the actual. Otherwise the
14854 -- primitive operations rename those of the parent type, If the parent
14855 -- renames an intrinsic operator, so does the new subprogram. We except
14856 -- concatenation, which is always properly typed, and does not get
14857 -- expanded as other intrinsic operations.
14859 if No (Actual_Subp) then
14860 if Is_Intrinsic_Subprogram (Parent_Subp) then
14861 Set_Is_Intrinsic_Subprogram (New_Subp);
14863 if Present (Alias (Parent_Subp))
14864 and then Chars (Parent_Subp) /= Name_Op_Concat
14865 then
14866 Set_Alias (New_Subp, Alias (Parent_Subp));
14867 else
14868 Set_Alias (New_Subp, Parent_Subp);
14869 end if;
14871 else
14872 Set_Alias (New_Subp, Parent_Subp);
14873 end if;
14875 else
14876 Set_Alias (New_Subp, Actual_Subp);
14877 end if;
14879 -- Inherit the "ghostness" from the parent subprogram
14881 if Is_Ghost_Entity (Alias (New_Subp)) then
14882 Set_Is_Ghost_Entity (New_Subp);
14883 end if;
14885 -- Derived subprograms of a tagged type must inherit the convention
14886 -- of the parent subprogram (a requirement of AI-117). Derived
14887 -- subprograms of untagged types simply get convention Ada by default.
14889 -- If the derived type is a tagged generic formal type with unknown
14890 -- discriminants, its convention is intrinsic (RM 6.3.1 (8)).
14892 -- However, if the type is derived from a generic formal, the further
14893 -- inherited subprogram has the convention of the non-generic ancestor.
14894 -- Otherwise there would be no way to override the operation.
14895 -- (This is subject to forthcoming ARG discussions).
14897 if Is_Tagged_Type (Derived_Type) then
14898 if Is_Generic_Type (Derived_Type)
14899 and then Has_Unknown_Discriminants (Derived_Type)
14900 then
14901 Set_Convention (New_Subp, Convention_Intrinsic);
14903 else
14904 if Is_Generic_Type (Parent_Type)
14905 and then Has_Unknown_Discriminants (Parent_Type)
14906 then
14907 Set_Convention (New_Subp, Convention (Alias (Parent_Subp)));
14908 else
14909 Set_Convention (New_Subp, Convention (Parent_Subp));
14910 end if;
14911 end if;
14912 end if;
14914 -- Predefined controlled operations retain their name even if the parent
14915 -- is hidden (see above), but they are not primitive operations if the
14916 -- ancestor is not visible, for example if the parent is a private
14917 -- extension completed with a controlled extension. Note that a full
14918 -- type that is controlled can break privacy: the flag Is_Controlled is
14919 -- set on both views of the type.
14921 if Is_Controlled (Parent_Type)
14922 and then Nam_In (Chars (Parent_Subp), Name_Initialize,
14923 Name_Adjust,
14924 Name_Finalize)
14925 and then Is_Hidden (Parent_Subp)
14926 and then not Is_Visibly_Controlled (Parent_Type)
14927 then
14928 Set_Is_Hidden (New_Subp);
14929 end if;
14931 Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
14932 Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
14934 if Ekind (Parent_Subp) = E_Procedure then
14935 Set_Is_Valued_Procedure
14936 (New_Subp, Is_Valued_Procedure (Parent_Subp));
14937 else
14938 Set_Has_Controlling_Result
14939 (New_Subp, Has_Controlling_Result (Parent_Subp));
14940 end if;
14942 -- No_Return must be inherited properly. If this is overridden in the
14943 -- case of a dispatching operation, then a check is made in Sem_Disp
14944 -- that the overriding operation is also No_Return (no such check is
14945 -- required for the case of non-dispatching operation.
14947 Set_No_Return (New_Subp, No_Return (Parent_Subp));
14949 -- A derived function with a controlling result is abstract. If the
14950 -- Derived_Type is a nonabstract formal generic derived type, then
14951 -- inherited operations are not abstract: the required check is done at
14952 -- instantiation time. If the derivation is for a generic actual, the
14953 -- function is not abstract unless the actual is.
14955 if Is_Generic_Type (Derived_Type)
14956 and then not Is_Abstract_Type (Derived_Type)
14957 then
14958 null;
14960 -- Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
14961 -- properties of the subprogram, as defined in RM-3.9.3(4/2-6/2).
14963 -- A subprogram subject to pragma Extensions_Visible with value False
14964 -- requires overriding if the subprogram has at least one controlling
14965 -- OUT parameter (SPARK RM 6.1.7(6)).
14967 elsif Ada_Version >= Ada_2005
14968 and then (Is_Abstract_Subprogram (Alias (New_Subp))
14969 or else (Is_Tagged_Type (Derived_Type)
14970 and then Etype (New_Subp) = Derived_Type
14971 and then not Is_Null_Extension (Derived_Type))
14972 or else (Is_Tagged_Type (Derived_Type)
14973 and then Ekind (Etype (New_Subp)) =
14974 E_Anonymous_Access_Type
14975 and then Designated_Type (Etype (New_Subp)) =
14976 Derived_Type
14977 and then not Is_Null_Extension (Derived_Type))
14978 or else (Comes_From_Source (Alias (New_Subp))
14979 and then Is_EVF_Procedure (Alias (New_Subp))))
14980 and then No (Actual_Subp)
14981 then
14982 if not Is_Tagged_Type (Derived_Type)
14983 or else Is_Abstract_Type (Derived_Type)
14984 or else Is_Abstract_Subprogram (Alias (New_Subp))
14985 then
14986 Set_Is_Abstract_Subprogram (New_Subp);
14987 else
14988 Set_Requires_Overriding (New_Subp);
14989 end if;
14991 elsif Ada_Version < Ada_2005
14992 and then (Is_Abstract_Subprogram (Alias (New_Subp))
14993 or else (Is_Tagged_Type (Derived_Type)
14994 and then Etype (New_Subp) = Derived_Type
14995 and then No (Actual_Subp)))
14996 then
14997 Set_Is_Abstract_Subprogram (New_Subp);
14999 -- AI05-0097 : an inherited operation that dispatches on result is
15000 -- abstract if the derived type is abstract, even if the parent type
15001 -- is concrete and the derived type is a null extension.
15003 elsif Has_Controlling_Result (Alias (New_Subp))
15004 and then Is_Abstract_Type (Etype (New_Subp))
15005 then
15006 Set_Is_Abstract_Subprogram (New_Subp);
15008 -- Finally, if the parent type is abstract we must verify that all
15009 -- inherited operations are either non-abstract or overridden, or that
15010 -- the derived type itself is abstract (this check is performed at the
15011 -- end of a package declaration, in Check_Abstract_Overriding). A
15012 -- private overriding in the parent type will not be visible in the
15013 -- derivation if we are not in an inner package or in a child unit of
15014 -- the parent type, in which case the abstractness of the inherited
15015 -- operation is carried to the new subprogram.
15017 elsif Is_Abstract_Type (Parent_Type)
15018 and then not In_Open_Scopes (Scope (Parent_Type))
15019 and then Is_Private_Overriding
15020 and then Is_Abstract_Subprogram (Visible_Subp)
15021 then
15022 if No (Actual_Subp) then
15023 Set_Alias (New_Subp, Visible_Subp);
15024 Set_Is_Abstract_Subprogram (New_Subp, True);
15026 else
15027 -- If this is a derivation for an instance of a formal derived
15028 -- type, abstractness comes from the primitive operation of the
15029 -- actual, not from the operation inherited from the ancestor.
15031 Set_Is_Abstract_Subprogram
15032 (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
15033 end if;
15034 end if;
15036 New_Overloaded_Entity (New_Subp, Derived_Type);
15038 -- Check for case of a derived subprogram for the instantiation of a
15039 -- formal derived tagged type, if so mark the subprogram as dispatching
15040 -- and inherit the dispatching attributes of the actual subprogram. The
15041 -- derived subprogram is effectively renaming of the actual subprogram,
15042 -- so it needs to have the same attributes as the actual.
15044 if Present (Actual_Subp)
15045 and then Is_Dispatching_Operation (Actual_Subp)
15046 then
15047 Set_Is_Dispatching_Operation (New_Subp);
15049 if Present (DTC_Entity (Actual_Subp)) then
15050 Set_DTC_Entity (New_Subp, DTC_Entity (Actual_Subp));
15051 Set_DT_Position_Value (New_Subp, DT_Position (Actual_Subp));
15052 end if;
15053 end if;
15055 -- Indicate that a derived subprogram does not require a body and that
15056 -- it does not require processing of default expressions.
15058 Set_Has_Completion (New_Subp);
15059 Set_Default_Expressions_Processed (New_Subp);
15061 if Ekind (New_Subp) = E_Function then
15062 Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
15063 end if;
15064 end Derive_Subprogram;
15066 ------------------------
15067 -- Derive_Subprograms --
15068 ------------------------
15070 procedure Derive_Subprograms
15071 (Parent_Type : Entity_Id;
15072 Derived_Type : Entity_Id;
15073 Generic_Actual : Entity_Id := Empty)
15075 Op_List : constant Elist_Id :=
15076 Collect_Primitive_Operations (Parent_Type);
15078 function Check_Derived_Type return Boolean;
15079 -- Check that all the entities derived from Parent_Type are found in
15080 -- the list of primitives of Derived_Type exactly in the same order.
15082 procedure Derive_Interface_Subprogram
15083 (New_Subp : in out Entity_Id;
15084 Subp : Entity_Id;
15085 Actual_Subp : Entity_Id);
15086 -- Derive New_Subp from the ultimate alias of the parent subprogram Subp
15087 -- (which is an interface primitive). If Generic_Actual is present then
15088 -- Actual_Subp is the actual subprogram corresponding with the generic
15089 -- subprogram Subp.
15091 function Check_Derived_Type return Boolean is
15092 E : Entity_Id;
15093 Elmt : Elmt_Id;
15094 List : Elist_Id;
15095 New_Subp : Entity_Id;
15096 Op_Elmt : Elmt_Id;
15097 Subp : Entity_Id;
15099 begin
15100 -- Traverse list of entities in the current scope searching for
15101 -- an incomplete type whose full-view is derived type
15103 E := First_Entity (Scope (Derived_Type));
15104 while Present (E) and then E /= Derived_Type loop
15105 if Ekind (E) = E_Incomplete_Type
15106 and then Present (Full_View (E))
15107 and then Full_View (E) = Derived_Type
15108 then
15109 -- Disable this test if Derived_Type completes an incomplete
15110 -- type because in such case more primitives can be added
15111 -- later to the list of primitives of Derived_Type by routine
15112 -- Process_Incomplete_Dependents
15114 return True;
15115 end if;
15117 E := Next_Entity (E);
15118 end loop;
15120 List := Collect_Primitive_Operations (Derived_Type);
15121 Elmt := First_Elmt (List);
15123 Op_Elmt := First_Elmt (Op_List);
15124 while Present (Op_Elmt) loop
15125 Subp := Node (Op_Elmt);
15126 New_Subp := Node (Elmt);
15128 -- At this early stage Derived_Type has no entities with attribute
15129 -- Interface_Alias. In addition, such primitives are always
15130 -- located at the end of the list of primitives of Parent_Type.
15131 -- Therefore, if found we can safely stop processing pending
15132 -- entities.
15134 exit when Present (Interface_Alias (Subp));
15136 -- Handle hidden entities
15138 if not Is_Predefined_Dispatching_Operation (Subp)
15139 and then Is_Hidden (Subp)
15140 then
15141 if Present (New_Subp)
15142 and then Primitive_Names_Match (Subp, New_Subp)
15143 then
15144 Next_Elmt (Elmt);
15145 end if;
15147 else
15148 if not Present (New_Subp)
15149 or else Ekind (Subp) /= Ekind (New_Subp)
15150 or else not Primitive_Names_Match (Subp, New_Subp)
15151 then
15152 return False;
15153 end if;
15155 Next_Elmt (Elmt);
15156 end if;
15158 Next_Elmt (Op_Elmt);
15159 end loop;
15161 return True;
15162 end Check_Derived_Type;
15164 ---------------------------------
15165 -- Derive_Interface_Subprogram --
15166 ---------------------------------
15168 procedure Derive_Interface_Subprogram
15169 (New_Subp : in out Entity_Id;
15170 Subp : Entity_Id;
15171 Actual_Subp : Entity_Id)
15173 Iface_Subp : constant Entity_Id := Ultimate_Alias (Subp);
15174 Iface_Type : constant Entity_Id := Find_Dispatching_Type (Iface_Subp);
15176 begin
15177 pragma Assert (Is_Interface (Iface_Type));
15179 Derive_Subprogram
15180 (New_Subp => New_Subp,
15181 Parent_Subp => Iface_Subp,
15182 Derived_Type => Derived_Type,
15183 Parent_Type => Iface_Type,
15184 Actual_Subp => Actual_Subp);
15186 -- Given that this new interface entity corresponds with a primitive
15187 -- of the parent that was not overridden we must leave it associated
15188 -- with its parent primitive to ensure that it will share the same
15189 -- dispatch table slot when overridden.
15191 if No (Actual_Subp) then
15192 Set_Alias (New_Subp, Subp);
15194 -- For instantiations this is not needed since the previous call to
15195 -- Derive_Subprogram leaves the entity well decorated.
15197 else
15198 pragma Assert (Alias (New_Subp) = Actual_Subp);
15199 null;
15200 end if;
15201 end Derive_Interface_Subprogram;
15203 -- Local variables
15205 Alias_Subp : Entity_Id;
15206 Act_List : Elist_Id;
15207 Act_Elmt : Elmt_Id;
15208 Act_Subp : Entity_Id := Empty;
15209 Elmt : Elmt_Id;
15210 Need_Search : Boolean := False;
15211 New_Subp : Entity_Id := Empty;
15212 Parent_Base : Entity_Id;
15213 Subp : Entity_Id;
15215 -- Start of processing for Derive_Subprograms
15217 begin
15218 if Ekind (Parent_Type) = E_Record_Type_With_Private
15219 and then Has_Discriminants (Parent_Type)
15220 and then Present (Full_View (Parent_Type))
15221 then
15222 Parent_Base := Full_View (Parent_Type);
15223 else
15224 Parent_Base := Parent_Type;
15225 end if;
15227 if Present (Generic_Actual) then
15228 Act_List := Collect_Primitive_Operations (Generic_Actual);
15229 Act_Elmt := First_Elmt (Act_List);
15230 else
15231 Act_List := No_Elist;
15232 Act_Elmt := No_Elmt;
15233 end if;
15235 -- Derive primitives inherited from the parent. Note that if the generic
15236 -- actual is present, this is not really a type derivation, it is a
15237 -- completion within an instance.
15239 -- Case 1: Derived_Type does not implement interfaces
15241 if not Is_Tagged_Type (Derived_Type)
15242 or else (not Has_Interfaces (Derived_Type)
15243 and then not (Present (Generic_Actual)
15244 and then Has_Interfaces (Generic_Actual)))
15245 then
15246 Elmt := First_Elmt (Op_List);
15247 while Present (Elmt) loop
15248 Subp := Node (Elmt);
15250 -- Literals are derived earlier in the process of building the
15251 -- derived type, and are skipped here.
15253 if Ekind (Subp) = E_Enumeration_Literal then
15254 null;
15256 -- The actual is a direct descendant and the common primitive
15257 -- operations appear in the same order.
15259 -- If the generic parent type is present, the derived type is an
15260 -- instance of a formal derived type, and within the instance its
15261 -- operations are those of the actual. We derive from the formal
15262 -- type but make the inherited operations aliases of the
15263 -- corresponding operations of the actual.
15265 else
15266 pragma Assert (No (Node (Act_Elmt))
15267 or else (Primitive_Names_Match (Subp, Node (Act_Elmt))
15268 and then
15269 Type_Conformant
15270 (Subp, Node (Act_Elmt),
15271 Skip_Controlling_Formals => True)));
15273 Derive_Subprogram
15274 (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
15276 if Present (Act_Elmt) then
15277 Next_Elmt (Act_Elmt);
15278 end if;
15279 end if;
15281 Next_Elmt (Elmt);
15282 end loop;
15284 -- Case 2: Derived_Type implements interfaces
15286 else
15287 -- If the parent type has no predefined primitives we remove
15288 -- predefined primitives from the list of primitives of generic
15289 -- actual to simplify the complexity of this algorithm.
15291 if Present (Generic_Actual) then
15292 declare
15293 Has_Predefined_Primitives : Boolean := False;
15295 begin
15296 -- Check if the parent type has predefined primitives
15298 Elmt := First_Elmt (Op_List);
15299 while Present (Elmt) loop
15300 Subp := Node (Elmt);
15302 if Is_Predefined_Dispatching_Operation (Subp)
15303 and then not Comes_From_Source (Ultimate_Alias (Subp))
15304 then
15305 Has_Predefined_Primitives := True;
15306 exit;
15307 end if;
15309 Next_Elmt (Elmt);
15310 end loop;
15312 -- Remove predefined primitives of Generic_Actual. We must use
15313 -- an auxiliary list because in case of tagged types the value
15314 -- returned by Collect_Primitive_Operations is the value stored
15315 -- in its Primitive_Operations attribute (and we don't want to
15316 -- modify its current contents).
15318 if not Has_Predefined_Primitives then
15319 declare
15320 Aux_List : constant Elist_Id := New_Elmt_List;
15322 begin
15323 Elmt := First_Elmt (Act_List);
15324 while Present (Elmt) loop
15325 Subp := Node (Elmt);
15327 if not Is_Predefined_Dispatching_Operation (Subp)
15328 or else Comes_From_Source (Subp)
15329 then
15330 Append_Elmt (Subp, Aux_List);
15331 end if;
15333 Next_Elmt (Elmt);
15334 end loop;
15336 Act_List := Aux_List;
15337 end;
15338 end if;
15340 Act_Elmt := First_Elmt (Act_List);
15341 Act_Subp := Node (Act_Elmt);
15342 end;
15343 end if;
15345 -- Stage 1: If the generic actual is not present we derive the
15346 -- primitives inherited from the parent type. If the generic parent
15347 -- type is present, the derived type is an instance of a formal
15348 -- derived type, and within the instance its operations are those of
15349 -- the actual. We derive from the formal type but make the inherited
15350 -- operations aliases of the corresponding operations of the actual.
15352 Elmt := First_Elmt (Op_List);
15353 while Present (Elmt) loop
15354 Subp := Node (Elmt);
15355 Alias_Subp := Ultimate_Alias (Subp);
15357 -- Do not derive internal entities of the parent that link
15358 -- interface primitives with their covering primitive. These
15359 -- entities will be added to this type when frozen.
15361 if Present (Interface_Alias (Subp)) then
15362 goto Continue;
15363 end if;
15365 -- If the generic actual is present find the corresponding
15366 -- operation in the generic actual. If the parent type is a
15367 -- direct ancestor of the derived type then, even if it is an
15368 -- interface, the operations are inherited from the primary
15369 -- dispatch table and are in the proper order. If we detect here
15370 -- that primitives are not in the same order we traverse the list
15371 -- of primitive operations of the actual to find the one that
15372 -- implements the interface primitive.
15374 if Need_Search
15375 or else
15376 (Present (Generic_Actual)
15377 and then Present (Act_Subp)
15378 and then not
15379 (Primitive_Names_Match (Subp, Act_Subp)
15380 and then
15381 Type_Conformant (Subp, Act_Subp,
15382 Skip_Controlling_Formals => True)))
15383 then
15384 pragma Assert (not Is_Ancestor (Parent_Base, Generic_Actual,
15385 Use_Full_View => True));
15387 -- Remember that we need searching for all pending primitives
15389 Need_Search := True;
15391 -- Handle entities associated with interface primitives
15393 if Present (Alias_Subp)
15394 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
15395 and then not Is_Predefined_Dispatching_Operation (Subp)
15396 then
15397 -- Search for the primitive in the homonym chain
15399 Act_Subp :=
15400 Find_Primitive_Covering_Interface
15401 (Tagged_Type => Generic_Actual,
15402 Iface_Prim => Alias_Subp);
15404 -- Previous search may not locate primitives covering
15405 -- interfaces defined in generics units or instantiations.
15406 -- (it fails if the covering primitive has formals whose
15407 -- type is also defined in generics or instantiations).
15408 -- In such case we search in the list of primitives of the
15409 -- generic actual for the internal entity that links the
15410 -- interface primitive and the covering primitive.
15412 if No (Act_Subp)
15413 and then Is_Generic_Type (Parent_Type)
15414 then
15415 -- This code has been designed to handle only generic
15416 -- formals that implement interfaces that are defined
15417 -- in a generic unit or instantiation. If this code is
15418 -- needed for other cases we must review it because
15419 -- (given that it relies on Original_Location to locate
15420 -- the primitive of Generic_Actual that covers the
15421 -- interface) it could leave linked through attribute
15422 -- Alias entities of unrelated instantiations).
15424 pragma Assert
15425 (Is_Generic_Unit
15426 (Scope (Find_Dispatching_Type (Alias_Subp)))
15427 or else
15428 Instantiation_Depth
15429 (Sloc (Find_Dispatching_Type (Alias_Subp))) > 0);
15431 declare
15432 Iface_Prim_Loc : constant Source_Ptr :=
15433 Original_Location (Sloc (Alias_Subp));
15435 Elmt : Elmt_Id;
15436 Prim : Entity_Id;
15438 begin
15439 Elmt :=
15440 First_Elmt (Primitive_Operations (Generic_Actual));
15442 Search : while Present (Elmt) loop
15443 Prim := Node (Elmt);
15445 if Present (Interface_Alias (Prim))
15446 and then Original_Location
15447 (Sloc (Interface_Alias (Prim))) =
15448 Iface_Prim_Loc
15449 then
15450 Act_Subp := Alias (Prim);
15451 exit Search;
15452 end if;
15454 Next_Elmt (Elmt);
15455 end loop Search;
15456 end;
15457 end if;
15459 pragma Assert (Present (Act_Subp)
15460 or else Is_Abstract_Type (Generic_Actual)
15461 or else Serious_Errors_Detected > 0);
15463 -- Handle predefined primitives plus the rest of user-defined
15464 -- primitives
15466 else
15467 Act_Elmt := First_Elmt (Act_List);
15468 while Present (Act_Elmt) loop
15469 Act_Subp := Node (Act_Elmt);
15471 exit when Primitive_Names_Match (Subp, Act_Subp)
15472 and then Type_Conformant
15473 (Subp, Act_Subp,
15474 Skip_Controlling_Formals => True)
15475 and then No (Interface_Alias (Act_Subp));
15477 Next_Elmt (Act_Elmt);
15478 end loop;
15480 if No (Act_Elmt) then
15481 Act_Subp := Empty;
15482 end if;
15483 end if;
15484 end if;
15486 -- Case 1: If the parent is a limited interface then it has the
15487 -- predefined primitives of synchronized interfaces. However, the
15488 -- actual type may be a non-limited type and hence it does not
15489 -- have such primitives.
15491 if Present (Generic_Actual)
15492 and then not Present (Act_Subp)
15493 and then Is_Limited_Interface (Parent_Base)
15494 and then Is_Predefined_Interface_Primitive (Subp)
15495 then
15496 null;
15498 -- Case 2: Inherit entities associated with interfaces that were
15499 -- not covered by the parent type. We exclude here null interface
15500 -- primitives because they do not need special management.
15502 -- We also exclude interface operations that are renamings. If the
15503 -- subprogram is an explicit renaming of an interface primitive,
15504 -- it is a regular primitive operation, and the presence of its
15505 -- alias is not relevant: it has to be derived like any other
15506 -- primitive.
15508 elsif Present (Alias (Subp))
15509 and then Nkind (Unit_Declaration_Node (Subp)) /=
15510 N_Subprogram_Renaming_Declaration
15511 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
15512 and then not
15513 (Nkind (Parent (Alias_Subp)) = N_Procedure_Specification
15514 and then Null_Present (Parent (Alias_Subp)))
15515 then
15516 -- If this is an abstract private type then we transfer the
15517 -- derivation of the interface primitive from the partial view
15518 -- to the full view. This is safe because all the interfaces
15519 -- must be visible in the partial view. Done to avoid adding
15520 -- a new interface derivation to the private part of the
15521 -- enclosing package; otherwise this new derivation would be
15522 -- decorated as hidden when the analysis of the enclosing
15523 -- package completes.
15525 if Is_Abstract_Type (Derived_Type)
15526 and then In_Private_Part (Current_Scope)
15527 and then Has_Private_Declaration (Derived_Type)
15528 then
15529 declare
15530 Partial_View : Entity_Id;
15531 Elmt : Elmt_Id;
15532 Ent : Entity_Id;
15534 begin
15535 Partial_View := First_Entity (Current_Scope);
15536 loop
15537 exit when No (Partial_View)
15538 or else (Has_Private_Declaration (Partial_View)
15539 and then
15540 Full_View (Partial_View) = Derived_Type);
15542 Next_Entity (Partial_View);
15543 end loop;
15545 -- If the partial view was not found then the source code
15546 -- has errors and the derivation is not needed.
15548 if Present (Partial_View) then
15549 Elmt :=
15550 First_Elmt (Primitive_Operations (Partial_View));
15551 while Present (Elmt) loop
15552 Ent := Node (Elmt);
15554 if Present (Alias (Ent))
15555 and then Ultimate_Alias (Ent) = Alias (Subp)
15556 then
15557 Append_Elmt
15558 (Ent, Primitive_Operations (Derived_Type));
15559 exit;
15560 end if;
15562 Next_Elmt (Elmt);
15563 end loop;
15565 -- If the interface primitive was not found in the
15566 -- partial view then this interface primitive was
15567 -- overridden. We add a derivation to activate in
15568 -- Derive_Progenitor_Subprograms the machinery to
15569 -- search for it.
15571 if No (Elmt) then
15572 Derive_Interface_Subprogram
15573 (New_Subp => New_Subp,
15574 Subp => Subp,
15575 Actual_Subp => Act_Subp);
15576 end if;
15577 end if;
15578 end;
15579 else
15580 Derive_Interface_Subprogram
15581 (New_Subp => New_Subp,
15582 Subp => Subp,
15583 Actual_Subp => Act_Subp);
15584 end if;
15586 -- Case 3: Common derivation
15588 else
15589 Derive_Subprogram
15590 (New_Subp => New_Subp,
15591 Parent_Subp => Subp,
15592 Derived_Type => Derived_Type,
15593 Parent_Type => Parent_Base,
15594 Actual_Subp => Act_Subp);
15595 end if;
15597 -- No need to update Act_Elm if we must search for the
15598 -- corresponding operation in the generic actual
15600 if not Need_Search
15601 and then Present (Act_Elmt)
15602 then
15603 Next_Elmt (Act_Elmt);
15604 Act_Subp := Node (Act_Elmt);
15605 end if;
15607 <<Continue>>
15608 Next_Elmt (Elmt);
15609 end loop;
15611 -- Inherit additional operations from progenitors. If the derived
15612 -- type is a generic actual, there are not new primitive operations
15613 -- for the type because it has those of the actual, and therefore
15614 -- nothing needs to be done. The renamings generated above are not
15615 -- primitive operations, and their purpose is simply to make the
15616 -- proper operations visible within an instantiation.
15618 if No (Generic_Actual) then
15619 Derive_Progenitor_Subprograms (Parent_Base, Derived_Type);
15620 end if;
15621 end if;
15623 -- Final check: Direct descendants must have their primitives in the
15624 -- same order. We exclude from this test untagged types and instances
15625 -- of formal derived types. We skip this test if we have already
15626 -- reported serious errors in the sources.
15628 pragma Assert (not Is_Tagged_Type (Derived_Type)
15629 or else Present (Generic_Actual)
15630 or else Serious_Errors_Detected > 0
15631 or else Check_Derived_Type);
15632 end Derive_Subprograms;
15634 --------------------------------
15635 -- Derived_Standard_Character --
15636 --------------------------------
15638 procedure Derived_Standard_Character
15639 (N : Node_Id;
15640 Parent_Type : Entity_Id;
15641 Derived_Type : Entity_Id)
15643 Loc : constant Source_Ptr := Sloc (N);
15644 Def : constant Node_Id := Type_Definition (N);
15645 Indic : constant Node_Id := Subtype_Indication (Def);
15646 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
15647 Implicit_Base : constant Entity_Id :=
15648 Create_Itype
15649 (E_Enumeration_Type, N, Derived_Type, 'B');
15651 Lo : Node_Id;
15652 Hi : Node_Id;
15654 begin
15655 Discard_Node (Process_Subtype (Indic, N));
15657 Set_Etype (Implicit_Base, Parent_Base);
15658 Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
15659 Set_RM_Size (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
15661 Set_Is_Character_Type (Implicit_Base, True);
15662 Set_Has_Delayed_Freeze (Implicit_Base);
15664 -- The bounds of the implicit base are the bounds of the parent base.
15665 -- Note that their type is the parent base.
15667 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
15668 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
15670 Set_Scalar_Range (Implicit_Base,
15671 Make_Range (Loc,
15672 Low_Bound => Lo,
15673 High_Bound => Hi));
15675 Conditional_Delay (Derived_Type, Parent_Type);
15677 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
15678 Set_Etype (Derived_Type, Implicit_Base);
15679 Set_Size_Info (Derived_Type, Parent_Type);
15681 if Unknown_RM_Size (Derived_Type) then
15682 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
15683 end if;
15685 Set_Is_Character_Type (Derived_Type, True);
15687 if Nkind (Indic) /= N_Subtype_Indication then
15689 -- If no explicit constraint, the bounds are those
15690 -- of the parent type.
15692 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Type));
15693 Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
15694 Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
15695 end if;
15697 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
15699 -- Because the implicit base is used in the conversion of the bounds, we
15700 -- have to freeze it now. This is similar to what is done for numeric
15701 -- types, and it equally suspicious, but otherwise a non-static bound
15702 -- will have a reference to an unfrozen type, which is rejected by Gigi
15703 -- (???). This requires specific care for definition of stream
15704 -- attributes. For details, see comments at the end of
15705 -- Build_Derived_Numeric_Type.
15707 Freeze_Before (N, Implicit_Base);
15708 end Derived_Standard_Character;
15710 ------------------------------
15711 -- Derived_Type_Declaration --
15712 ------------------------------
15714 procedure Derived_Type_Declaration
15715 (T : Entity_Id;
15716 N : Node_Id;
15717 Is_Completion : Boolean)
15719 Parent_Type : Entity_Id;
15721 function Comes_From_Generic (Typ : Entity_Id) return Boolean;
15722 -- Check whether the parent type is a generic formal, or derives
15723 -- directly or indirectly from one.
15725 ------------------------
15726 -- Comes_From_Generic --
15727 ------------------------
15729 function Comes_From_Generic (Typ : Entity_Id) return Boolean is
15730 begin
15731 if Is_Generic_Type (Typ) then
15732 return True;
15734 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
15735 return True;
15737 elsif Is_Private_Type (Typ)
15738 and then Present (Full_View (Typ))
15739 and then Is_Generic_Type (Root_Type (Full_View (Typ)))
15740 then
15741 return True;
15743 elsif Is_Generic_Actual_Type (Typ) then
15744 return True;
15746 else
15747 return False;
15748 end if;
15749 end Comes_From_Generic;
15751 -- Local variables
15753 Def : constant Node_Id := Type_Definition (N);
15754 Iface_Def : Node_Id;
15755 Indic : constant Node_Id := Subtype_Indication (Def);
15756 Extension : constant Node_Id := Record_Extension_Part (Def);
15757 Parent_Node : Node_Id;
15758 Taggd : Boolean;
15760 -- Start of processing for Derived_Type_Declaration
15762 begin
15763 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
15765 -- Ada 2005 (AI-251): In case of interface derivation check that the
15766 -- parent is also an interface.
15768 if Interface_Present (Def) then
15769 Check_SPARK_05_Restriction ("interface is not allowed", Def);
15771 if not Is_Interface (Parent_Type) then
15772 Diagnose_Interface (Indic, Parent_Type);
15774 else
15775 Parent_Node := Parent (Base_Type (Parent_Type));
15776 Iface_Def := Type_Definition (Parent_Node);
15778 -- Ada 2005 (AI-251): Limited interfaces can only inherit from
15779 -- other limited interfaces.
15781 if Limited_Present (Def) then
15782 if Limited_Present (Iface_Def) then
15783 null;
15785 elsif Protected_Present (Iface_Def) then
15786 Error_Msg_NE
15787 ("descendant of& must be declared"
15788 & " as a protected interface",
15789 N, Parent_Type);
15791 elsif Synchronized_Present (Iface_Def) then
15792 Error_Msg_NE
15793 ("descendant of& must be declared"
15794 & " as a synchronized interface",
15795 N, Parent_Type);
15797 elsif Task_Present (Iface_Def) then
15798 Error_Msg_NE
15799 ("descendant of& must be declared as a task interface",
15800 N, Parent_Type);
15802 else
15803 Error_Msg_N
15804 ("(Ada 2005) limited interface cannot "
15805 & "inherit from non-limited interface", Indic);
15806 end if;
15808 -- Ada 2005 (AI-345): Non-limited interfaces can only inherit
15809 -- from non-limited or limited interfaces.
15811 elsif not Protected_Present (Def)
15812 and then not Synchronized_Present (Def)
15813 and then not Task_Present (Def)
15814 then
15815 if Limited_Present (Iface_Def) then
15816 null;
15818 elsif Protected_Present (Iface_Def) then
15819 Error_Msg_NE
15820 ("descendant of& must be declared"
15821 & " as a protected interface",
15822 N, Parent_Type);
15824 elsif Synchronized_Present (Iface_Def) then
15825 Error_Msg_NE
15826 ("descendant of& must be declared"
15827 & " as a synchronized interface",
15828 N, Parent_Type);
15830 elsif Task_Present (Iface_Def) then
15831 Error_Msg_NE
15832 ("descendant of& must be declared as a task interface",
15833 N, Parent_Type);
15834 else
15835 null;
15836 end if;
15837 end if;
15838 end if;
15839 end if;
15841 if Is_Tagged_Type (Parent_Type)
15842 and then Is_Concurrent_Type (Parent_Type)
15843 and then not Is_Interface (Parent_Type)
15844 then
15845 Error_Msg_N
15846 ("parent type of a record extension cannot be "
15847 & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
15848 Set_Etype (T, Any_Type);
15849 return;
15850 end if;
15852 -- Ada 2005 (AI-251): Decorate all the names in the list of ancestor
15853 -- interfaces
15855 if Is_Tagged_Type (Parent_Type)
15856 and then Is_Non_Empty_List (Interface_List (Def))
15857 then
15858 declare
15859 Intf : Node_Id;
15860 T : Entity_Id;
15862 begin
15863 Intf := First (Interface_List (Def));
15864 while Present (Intf) loop
15865 T := Find_Type_Of_Subtype_Indic (Intf);
15867 if not Is_Interface (T) then
15868 Diagnose_Interface (Intf, T);
15870 -- Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
15871 -- a limited type from having a nonlimited progenitor.
15873 elsif (Limited_Present (Def)
15874 or else (not Is_Interface (Parent_Type)
15875 and then Is_Limited_Type (Parent_Type)))
15876 and then not Is_Limited_Interface (T)
15877 then
15878 Error_Msg_NE
15879 ("progenitor interface& of limited type must be limited",
15880 N, T);
15881 end if;
15883 Next (Intf);
15884 end loop;
15885 end;
15886 end if;
15888 if Parent_Type = Any_Type
15889 or else Etype (Parent_Type) = Any_Type
15890 or else (Is_Class_Wide_Type (Parent_Type)
15891 and then Etype (Parent_Type) = T)
15892 then
15893 -- If Parent_Type is undefined or illegal, make new type into a
15894 -- subtype of Any_Type, and set a few attributes to prevent cascaded
15895 -- errors. If this is a self-definition, emit error now.
15897 if T = Parent_Type or else T = Etype (Parent_Type) then
15898 Error_Msg_N ("type cannot be used in its own definition", Indic);
15899 end if;
15901 Set_Ekind (T, Ekind (Parent_Type));
15902 Set_Etype (T, Any_Type);
15903 Set_Scalar_Range (T, Scalar_Range (Any_Type));
15905 if Is_Tagged_Type (T)
15906 and then Is_Record_Type (T)
15907 then
15908 Set_Direct_Primitive_Operations (T, New_Elmt_List);
15909 end if;
15911 return;
15912 end if;
15914 -- Ada 2005 (AI-251): The case in which the parent of the full-view is
15915 -- an interface is special because the list of interfaces in the full
15916 -- view can be given in any order. For example:
15918 -- type A is interface;
15919 -- type B is interface and A;
15920 -- type D is new B with private;
15921 -- private
15922 -- type D is new A and B with null record; -- 1 --
15924 -- In this case we perform the following transformation of -1-:
15926 -- type D is new B and A with null record;
15928 -- If the parent of the full-view covers the parent of the partial-view
15929 -- we have two possible cases:
15931 -- 1) They have the same parent
15932 -- 2) The parent of the full-view implements some further interfaces
15934 -- In both cases we do not need to perform the transformation. In the
15935 -- first case the source program is correct and the transformation is
15936 -- not needed; in the second case the source program does not fulfill
15937 -- the no-hidden interfaces rule (AI-396) and the error will be reported
15938 -- later.
15940 -- This transformation not only simplifies the rest of the analysis of
15941 -- this type declaration but also simplifies the correct generation of
15942 -- the object layout to the expander.
15944 if In_Private_Part (Current_Scope)
15945 and then Is_Interface (Parent_Type)
15946 then
15947 declare
15948 Iface : Node_Id;
15949 Partial_View : Entity_Id;
15950 Partial_View_Parent : Entity_Id;
15951 New_Iface : Node_Id;
15953 begin
15954 -- Look for the associated private type declaration
15956 Partial_View := First_Entity (Current_Scope);
15957 loop
15958 exit when No (Partial_View)
15959 or else (Has_Private_Declaration (Partial_View)
15960 and then Full_View (Partial_View) = T);
15962 Next_Entity (Partial_View);
15963 end loop;
15965 -- If the partial view was not found then the source code has
15966 -- errors and the transformation is not needed.
15968 if Present (Partial_View) then
15969 Partial_View_Parent := Etype (Partial_View);
15971 -- If the parent of the full-view covers the parent of the
15972 -- partial-view we have nothing else to do.
15974 if Interface_Present_In_Ancestor
15975 (Parent_Type, Partial_View_Parent)
15976 then
15977 null;
15979 -- Traverse the list of interfaces of the full-view to look
15980 -- for the parent of the partial-view and perform the tree
15981 -- transformation.
15983 else
15984 Iface := First (Interface_List (Def));
15985 while Present (Iface) loop
15986 if Etype (Iface) = Etype (Partial_View) then
15987 Rewrite (Subtype_Indication (Def),
15988 New_Copy (Subtype_Indication
15989 (Parent (Partial_View))));
15991 New_Iface :=
15992 Make_Identifier (Sloc (N), Chars (Parent_Type));
15993 Append (New_Iface, Interface_List (Def));
15995 -- Analyze the transformed code
15997 Derived_Type_Declaration (T, N, Is_Completion);
15998 return;
15999 end if;
16001 Next (Iface);
16002 end loop;
16003 end if;
16004 end if;
16005 end;
16006 end if;
16008 -- Only composite types other than array types are allowed to have
16009 -- discriminants.
16011 if Present (Discriminant_Specifications (N)) then
16012 if (Is_Elementary_Type (Parent_Type)
16013 or else
16014 Is_Array_Type (Parent_Type))
16015 and then not Error_Posted (N)
16016 then
16017 Error_Msg_N
16018 ("elementary or array type cannot have discriminants",
16019 Defining_Identifier (First (Discriminant_Specifications (N))));
16020 Set_Has_Discriminants (T, False);
16022 -- The type is allowed to have discriminants
16024 else
16025 Check_SPARK_05_Restriction ("discriminant type is not allowed", N);
16026 end if;
16027 end if;
16029 -- In Ada 83, a derived type defined in a package specification cannot
16030 -- be used for further derivation until the end of its visible part.
16031 -- Note that derivation in the private part of the package is allowed.
16033 if Ada_Version = Ada_83
16034 and then Is_Derived_Type (Parent_Type)
16035 and then In_Visible_Part (Scope (Parent_Type))
16036 then
16037 if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
16038 Error_Msg_N
16039 ("(Ada 83): premature use of type for derivation", Indic);
16040 end if;
16041 end if;
16043 -- Check for early use of incomplete or private type
16045 if Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
16046 Error_Msg_N ("premature derivation of incomplete type", Indic);
16047 return;
16049 elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
16050 and then not Comes_From_Generic (Parent_Type))
16051 or else Has_Private_Component (Parent_Type)
16052 then
16053 -- The ancestor type of a formal type can be incomplete, in which
16054 -- case only the operations of the partial view are available in the
16055 -- generic. Subsequent checks may be required when the full view is
16056 -- analyzed to verify that a derivation from a tagged type has an
16057 -- extension.
16059 if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
16060 null;
16062 elsif No (Underlying_Type (Parent_Type))
16063 or else Has_Private_Component (Parent_Type)
16064 then
16065 Error_Msg_N
16066 ("premature derivation of derived or private type", Indic);
16068 -- Flag the type itself as being in error, this prevents some
16069 -- nasty problems with subsequent uses of the malformed type.
16071 Set_Error_Posted (T);
16073 -- Check that within the immediate scope of an untagged partial
16074 -- view it's illegal to derive from the partial view if the
16075 -- full view is tagged. (7.3(7))
16077 -- We verify that the Parent_Type is a partial view by checking
16078 -- that it is not a Full_Type_Declaration (i.e. a private type or
16079 -- private extension declaration), to distinguish a partial view
16080 -- from a derivation from a private type which also appears as
16081 -- E_Private_Type. If the parent base type is not declared in an
16082 -- enclosing scope there is no need to check.
16084 elsif Present (Full_View (Parent_Type))
16085 and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
16086 and then not Is_Tagged_Type (Parent_Type)
16087 and then Is_Tagged_Type (Full_View (Parent_Type))
16088 and then In_Open_Scopes (Scope (Base_Type (Parent_Type)))
16089 then
16090 Error_Msg_N
16091 ("premature derivation from type with tagged full view",
16092 Indic);
16093 end if;
16094 end if;
16096 -- Check that form of derivation is appropriate
16098 Taggd := Is_Tagged_Type (Parent_Type);
16100 -- Set the parent type to the class-wide type's specific type in this
16101 -- case to prevent cascading errors
16103 if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
16104 Error_Msg_N ("parent type must not be a class-wide type", Indic);
16105 Set_Etype (T, Etype (Parent_Type));
16106 return;
16107 end if;
16109 if Present (Extension) and then not Taggd then
16110 Error_Msg_N
16111 ("type derived from untagged type cannot have extension", Indic);
16113 elsif No (Extension) and then Taggd then
16115 -- If this declaration is within a private part (or body) of a
16116 -- generic instantiation then the derivation is allowed (the parent
16117 -- type can only appear tagged in this case if it's a generic actual
16118 -- type, since it would otherwise have been rejected in the analysis
16119 -- of the generic template).
16121 if not Is_Generic_Actual_Type (Parent_Type)
16122 or else In_Visible_Part (Scope (Parent_Type))
16123 then
16124 if Is_Class_Wide_Type (Parent_Type) then
16125 Error_Msg_N
16126 ("parent type must not be a class-wide type", Indic);
16128 -- Use specific type to prevent cascaded errors.
16130 Parent_Type := Etype (Parent_Type);
16132 else
16133 Error_Msg_N
16134 ("type derived from tagged type must have extension", Indic);
16135 end if;
16136 end if;
16137 end if;
16139 -- AI-443: Synchronized formal derived types require a private
16140 -- extension. There is no point in checking the ancestor type or
16141 -- the progenitors since the construct is wrong to begin with.
16143 if Ada_Version >= Ada_2005
16144 and then Is_Generic_Type (T)
16145 and then Present (Original_Node (N))
16146 then
16147 declare
16148 Decl : constant Node_Id := Original_Node (N);
16150 begin
16151 if Nkind (Decl) = N_Formal_Type_Declaration
16152 and then Nkind (Formal_Type_Definition (Decl)) =
16153 N_Formal_Derived_Type_Definition
16154 and then Synchronized_Present (Formal_Type_Definition (Decl))
16155 and then No (Extension)
16157 -- Avoid emitting a duplicate error message
16159 and then not Error_Posted (Indic)
16160 then
16161 Error_Msg_N
16162 ("synchronized derived type must have extension", N);
16163 end if;
16164 end;
16165 end if;
16167 if Null_Exclusion_Present (Def)
16168 and then not Is_Access_Type (Parent_Type)
16169 then
16170 Error_Msg_N ("null exclusion can only apply to an access type", N);
16171 end if;
16173 -- Avoid deriving parent primitives of underlying record views
16175 Build_Derived_Type (N, Parent_Type, T, Is_Completion,
16176 Derive_Subps => not Is_Underlying_Record_View (T));
16178 -- AI-419: The parent type of an explicitly limited derived type must
16179 -- be a limited type or a limited interface.
16181 if Limited_Present (Def) then
16182 Set_Is_Limited_Record (T);
16184 if Is_Interface (T) then
16185 Set_Is_Limited_Interface (T);
16186 end if;
16188 if not Is_Limited_Type (Parent_Type)
16189 and then
16190 (not Is_Interface (Parent_Type)
16191 or else not Is_Limited_Interface (Parent_Type))
16192 then
16193 -- AI05-0096: a derivation in the private part of an instance is
16194 -- legal if the generic formal is untagged limited, and the actual
16195 -- is non-limited.
16197 if Is_Generic_Actual_Type (Parent_Type)
16198 and then In_Private_Part (Current_Scope)
16199 and then
16200 not Is_Tagged_Type
16201 (Generic_Parent_Type (Parent (Parent_Type)))
16202 then
16203 null;
16205 else
16206 Error_Msg_NE
16207 ("parent type& of limited type must be limited",
16208 N, Parent_Type);
16209 end if;
16210 end if;
16211 end if;
16213 -- In SPARK, there are no derived type definitions other than type
16214 -- extensions of tagged record types.
16216 if No (Extension) then
16217 Check_SPARK_05_Restriction
16218 ("derived type is not allowed", Original_Node (N));
16219 end if;
16220 end Derived_Type_Declaration;
16222 ------------------------
16223 -- Diagnose_Interface --
16224 ------------------------
16226 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id) is
16227 begin
16228 if not Is_Interface (E) and then E /= Any_Type then
16229 Error_Msg_NE ("(Ada 2005) & must be an interface", N, E);
16230 end if;
16231 end Diagnose_Interface;
16233 ----------------------------------
16234 -- Enumeration_Type_Declaration --
16235 ----------------------------------
16237 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
16238 Ev : Uint;
16239 L : Node_Id;
16240 R_Node : Node_Id;
16241 B_Node : Node_Id;
16243 begin
16244 -- Create identifier node representing lower bound
16246 B_Node := New_Node (N_Identifier, Sloc (Def));
16247 L := First (Literals (Def));
16248 Set_Chars (B_Node, Chars (L));
16249 Set_Entity (B_Node, L);
16250 Set_Etype (B_Node, T);
16251 Set_Is_Static_Expression (B_Node, True);
16253 R_Node := New_Node (N_Range, Sloc (Def));
16254 Set_Low_Bound (R_Node, B_Node);
16256 Set_Ekind (T, E_Enumeration_Type);
16257 Set_First_Literal (T, L);
16258 Set_Etype (T, T);
16259 Set_Is_Constrained (T);
16261 Ev := Uint_0;
16263 -- Loop through literals of enumeration type setting pos and rep values
16264 -- except that if the Ekind is already set, then it means the literal
16265 -- was already constructed (case of a derived type declaration and we
16266 -- should not disturb the Pos and Rep values.
16268 while Present (L) loop
16269 if Ekind (L) /= E_Enumeration_Literal then
16270 Set_Ekind (L, E_Enumeration_Literal);
16271 Set_Enumeration_Pos (L, Ev);
16272 Set_Enumeration_Rep (L, Ev);
16273 Set_Is_Known_Valid (L, True);
16274 end if;
16276 Set_Etype (L, T);
16277 New_Overloaded_Entity (L);
16278 Generate_Definition (L);
16279 Set_Convention (L, Convention_Intrinsic);
16281 -- Case of character literal
16283 if Nkind (L) = N_Defining_Character_Literal then
16284 Set_Is_Character_Type (T, True);
16286 -- Check violation of No_Wide_Characters
16288 if Restriction_Check_Required (No_Wide_Characters) then
16289 Get_Name_String (Chars (L));
16291 if Name_Len >= 3 and then Name_Buffer (1 .. 2) = "QW" then
16292 Check_Restriction (No_Wide_Characters, L);
16293 end if;
16294 end if;
16295 end if;
16297 Ev := Ev + 1;
16298 Next (L);
16299 end loop;
16301 -- Now create a node representing upper bound
16303 B_Node := New_Node (N_Identifier, Sloc (Def));
16304 Set_Chars (B_Node, Chars (Last (Literals (Def))));
16305 Set_Entity (B_Node, Last (Literals (Def)));
16306 Set_Etype (B_Node, T);
16307 Set_Is_Static_Expression (B_Node, True);
16309 Set_High_Bound (R_Node, B_Node);
16311 -- Initialize various fields of the type. Some of this information
16312 -- may be overwritten later through rep.clauses.
16314 Set_Scalar_Range (T, R_Node);
16315 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
16316 Set_Enum_Esize (T);
16317 Set_Enum_Pos_To_Rep (T, Empty);
16319 -- Set Discard_Names if configuration pragma set, or if there is
16320 -- a parameterless pragma in the current declarative region
16322 if Global_Discard_Names or else Discard_Names (Scope (T)) then
16323 Set_Discard_Names (T);
16324 end if;
16326 -- Process end label if there is one
16328 if Present (Def) then
16329 Process_End_Label (Def, 'e', T);
16330 end if;
16331 end Enumeration_Type_Declaration;
16333 ---------------------------------
16334 -- Expand_To_Stored_Constraint --
16335 ---------------------------------
16337 function Expand_To_Stored_Constraint
16338 (Typ : Entity_Id;
16339 Constraint : Elist_Id) return Elist_Id
16341 Explicitly_Discriminated_Type : Entity_Id;
16342 Expansion : Elist_Id;
16343 Discriminant : Entity_Id;
16345 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
16346 -- Find the nearest type that actually specifies discriminants
16348 ---------------------------------
16349 -- Type_With_Explicit_Discrims --
16350 ---------------------------------
16352 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
16353 Typ : constant E := Base_Type (Id);
16355 begin
16356 if Ekind (Typ) in Incomplete_Or_Private_Kind then
16357 if Present (Full_View (Typ)) then
16358 return Type_With_Explicit_Discrims (Full_View (Typ));
16359 end if;
16361 else
16362 if Has_Discriminants (Typ) then
16363 return Typ;
16364 end if;
16365 end if;
16367 if Etype (Typ) = Typ then
16368 return Empty;
16369 elsif Has_Discriminants (Typ) then
16370 return Typ;
16371 else
16372 return Type_With_Explicit_Discrims (Etype (Typ));
16373 end if;
16375 end Type_With_Explicit_Discrims;
16377 -- Start of processing for Expand_To_Stored_Constraint
16379 begin
16380 if No (Constraint) or else Is_Empty_Elmt_List (Constraint) then
16381 return No_Elist;
16382 end if;
16384 Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
16386 if No (Explicitly_Discriminated_Type) then
16387 return No_Elist;
16388 end if;
16390 Expansion := New_Elmt_List;
16392 Discriminant :=
16393 First_Stored_Discriminant (Explicitly_Discriminated_Type);
16394 while Present (Discriminant) loop
16395 Append_Elmt
16396 (Get_Discriminant_Value
16397 (Discriminant, Explicitly_Discriminated_Type, Constraint),
16398 To => Expansion);
16399 Next_Stored_Discriminant (Discriminant);
16400 end loop;
16402 return Expansion;
16403 end Expand_To_Stored_Constraint;
16405 ---------------------------
16406 -- Find_Hidden_Interface --
16407 ---------------------------
16409 function Find_Hidden_Interface
16410 (Src : Elist_Id;
16411 Dest : Elist_Id) return Entity_Id
16413 Iface : Entity_Id;
16414 Iface_Elmt : Elmt_Id;
16416 begin
16417 if Present (Src) and then Present (Dest) then
16418 Iface_Elmt := First_Elmt (Src);
16419 while Present (Iface_Elmt) loop
16420 Iface := Node (Iface_Elmt);
16422 if Is_Interface (Iface)
16423 and then not Contain_Interface (Iface, Dest)
16424 then
16425 return Iface;
16426 end if;
16428 Next_Elmt (Iface_Elmt);
16429 end loop;
16430 end if;
16432 return Empty;
16433 end Find_Hidden_Interface;
16435 --------------------
16436 -- Find_Type_Name --
16437 --------------------
16439 function Find_Type_Name (N : Node_Id) return Entity_Id is
16440 Id : constant Entity_Id := Defining_Identifier (N);
16441 Prev : Entity_Id;
16442 New_Id : Entity_Id;
16443 Prev_Par : Node_Id;
16445 procedure Check_Duplicate_Aspects;
16446 -- Check that aspects specified in a completion have not been specified
16447 -- already in the partial view. Type_Invariant and others can be
16448 -- specified on either view but never on both.
16450 procedure Tag_Mismatch;
16451 -- Diagnose a tagged partial view whose full view is untagged.
16452 -- We post the message on the full view, with a reference to
16453 -- the previous partial view. The partial view can be private
16454 -- or incomplete, and these are handled in a different manner,
16455 -- so we determine the position of the error message from the
16456 -- respective slocs of both.
16458 -----------------------------
16459 -- Check_Duplicate_Aspects --
16460 -----------------------------
16461 procedure Check_Duplicate_Aspects is
16462 Prev_Aspects : constant List_Id := Aspect_Specifications (Prev_Par);
16463 Full_Aspects : constant List_Id := Aspect_Specifications (N);
16464 F_Spec, P_Spec : Node_Id;
16466 begin
16467 if Present (Prev_Aspects) and then Present (Full_Aspects) then
16468 F_Spec := First (Full_Aspects);
16469 while Present (F_Spec) loop
16470 P_Spec := First (Prev_Aspects);
16471 while Present (P_Spec) loop
16472 if Chars (Identifier (P_Spec)) = Chars (Identifier (F_Spec))
16473 then
16474 Error_Msg_N
16475 ("aspect already specified in private declaration",
16476 F_Spec);
16477 Remove (F_Spec);
16478 return;
16479 end if;
16481 Next (P_Spec);
16482 end loop;
16484 Next (F_Spec);
16485 end loop;
16486 end if;
16487 end Check_Duplicate_Aspects;
16489 ------------------
16490 -- Tag_Mismatch --
16491 ------------------
16493 procedure Tag_Mismatch is
16494 begin
16495 if Sloc (Prev) < Sloc (Id) then
16496 if Ada_Version >= Ada_2012
16497 and then Nkind (N) = N_Private_Type_Declaration
16498 then
16499 Error_Msg_NE
16500 ("declaration of private } must be a tagged type ", Id, Prev);
16501 else
16502 Error_Msg_NE
16503 ("full declaration of } must be a tagged type ", Id, Prev);
16504 end if;
16506 else
16507 if Ada_Version >= Ada_2012
16508 and then Nkind (N) = N_Private_Type_Declaration
16509 then
16510 Error_Msg_NE
16511 ("declaration of private } must be a tagged type ", Prev, Id);
16512 else
16513 Error_Msg_NE
16514 ("full declaration of } must be a tagged type ", Prev, Id);
16515 end if;
16516 end if;
16517 end Tag_Mismatch;
16519 -- Start of processing for Find_Type_Name
16521 begin
16522 -- Find incomplete declaration, if one was given
16524 Prev := Current_Entity_In_Scope (Id);
16526 -- New type declaration
16528 if No (Prev) then
16529 Enter_Name (Id);
16530 return Id;
16532 -- Previous declaration exists
16534 else
16535 Prev_Par := Parent (Prev);
16537 -- Error if not incomplete/private case except if previous
16538 -- declaration is implicit, etc. Enter_Name will emit error if
16539 -- appropriate.
16541 if not Is_Incomplete_Or_Private_Type (Prev) then
16542 Enter_Name (Id);
16543 New_Id := Id;
16545 -- Check invalid completion of private or incomplete type
16547 elsif not Nkind_In (N, N_Full_Type_Declaration,
16548 N_Task_Type_Declaration,
16549 N_Protected_Type_Declaration)
16550 and then
16551 (Ada_Version < Ada_2012
16552 or else not Is_Incomplete_Type (Prev)
16553 or else not Nkind_In (N, N_Private_Type_Declaration,
16554 N_Private_Extension_Declaration))
16555 then
16556 -- Completion must be a full type declarations (RM 7.3(4))
16558 Error_Msg_Sloc := Sloc (Prev);
16559 Error_Msg_NE ("invalid completion of }", Id, Prev);
16561 -- Set scope of Id to avoid cascaded errors. Entity is never
16562 -- examined again, except when saving globals in generics.
16564 Set_Scope (Id, Current_Scope);
16565 New_Id := Id;
16567 -- If this is a repeated incomplete declaration, no further
16568 -- checks are possible.
16570 if Nkind (N) = N_Incomplete_Type_Declaration then
16571 return Prev;
16572 end if;
16574 -- Case of full declaration of incomplete type
16576 elsif Ekind (Prev) = E_Incomplete_Type
16577 and then (Ada_Version < Ada_2012
16578 or else No (Full_View (Prev))
16579 or else not Is_Private_Type (Full_View (Prev)))
16580 then
16581 -- Indicate that the incomplete declaration has a matching full
16582 -- declaration. The defining occurrence of the incomplete
16583 -- declaration remains the visible one, and the procedure
16584 -- Get_Full_View dereferences it whenever the type is used.
16586 if Present (Full_View (Prev)) then
16587 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
16588 end if;
16590 Set_Full_View (Prev, Id);
16591 Append_Entity (Id, Current_Scope);
16592 Set_Is_Public (Id, Is_Public (Prev));
16593 Set_Is_Internal (Id);
16594 New_Id := Prev;
16596 -- If the incomplete view is tagged, a class_wide type has been
16597 -- created already. Use it for the private type as well, in order
16598 -- to prevent multiple incompatible class-wide types that may be
16599 -- created for self-referential anonymous access components.
16601 if Is_Tagged_Type (Prev)
16602 and then Present (Class_Wide_Type (Prev))
16603 then
16604 Set_Ekind (Id, Ekind (Prev)); -- will be reset later
16605 Set_Class_Wide_Type (Id, Class_Wide_Type (Prev));
16607 -- The type of the classwide type is the current Id. Previously
16608 -- this was not done for private declarations because of order-
16609 -- of elaboration issues in the back-end, but gigi now handles
16610 -- this properly.
16612 Set_Etype (Class_Wide_Type (Id), Id);
16613 end if;
16615 -- Case of full declaration of private type
16617 else
16618 -- If the private type was a completion of an incomplete type then
16619 -- update Prev to reference the private type
16621 if Ada_Version >= Ada_2012
16622 and then Ekind (Prev) = E_Incomplete_Type
16623 and then Present (Full_View (Prev))
16624 and then Is_Private_Type (Full_View (Prev))
16625 then
16626 Prev := Full_View (Prev);
16627 Prev_Par := Parent (Prev);
16628 end if;
16630 if Nkind (N) = N_Full_Type_Declaration
16631 and then Nkind_In
16632 (Type_Definition (N), N_Record_Definition,
16633 N_Derived_Type_Definition)
16634 and then Interface_Present (Type_Definition (N))
16635 then
16636 Error_Msg_N
16637 ("completion of private type cannot be an interface", N);
16638 end if;
16640 if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
16641 if Etype (Prev) /= Prev then
16643 -- Prev is a private subtype or a derived type, and needs
16644 -- no completion.
16646 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
16647 New_Id := Id;
16649 elsif Ekind (Prev) = E_Private_Type
16650 and then Nkind_In (N, N_Task_Type_Declaration,
16651 N_Protected_Type_Declaration)
16652 then
16653 Error_Msg_N
16654 ("completion of nonlimited type cannot be limited", N);
16656 elsif Ekind (Prev) = E_Record_Type_With_Private
16657 and then Nkind_In (N, N_Task_Type_Declaration,
16658 N_Protected_Type_Declaration)
16659 then
16660 if not Is_Limited_Record (Prev) then
16661 Error_Msg_N
16662 ("completion of nonlimited type cannot be limited", N);
16664 elsif No (Interface_List (N)) then
16665 Error_Msg_N
16666 ("completion of tagged private type must be tagged",
16668 end if;
16669 end if;
16671 -- Ada 2005 (AI-251): Private extension declaration of a task
16672 -- type or a protected type. This case arises when covering
16673 -- interface types.
16675 elsif Nkind_In (N, N_Task_Type_Declaration,
16676 N_Protected_Type_Declaration)
16677 then
16678 null;
16680 elsif Nkind (N) /= N_Full_Type_Declaration
16681 or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
16682 then
16683 Error_Msg_N
16684 ("full view of private extension must be an extension", N);
16686 elsif not (Abstract_Present (Parent (Prev)))
16687 and then Abstract_Present (Type_Definition (N))
16688 then
16689 Error_Msg_N
16690 ("full view of non-abstract extension cannot be abstract", N);
16691 end if;
16693 if not In_Private_Part (Current_Scope) then
16694 Error_Msg_N
16695 ("declaration of full view must appear in private part", N);
16696 end if;
16698 if Ada_Version >= Ada_2012 then
16699 Check_Duplicate_Aspects;
16700 end if;
16702 Copy_And_Swap (Prev, Id);
16703 Set_Has_Private_Declaration (Prev);
16704 Set_Has_Private_Declaration (Id);
16706 -- AI12-0133: Indicate whether we have a partial view with
16707 -- unknown discriminants, in which case initialization of objects
16708 -- of the type do not receive an invariant check.
16710 Set_Partial_View_Has_Unknown_Discr
16711 (Prev, Has_Unknown_Discriminants (Id));
16713 -- Preserve aspect and iterator flags that may have been set on
16714 -- the partial view.
16716 Set_Has_Delayed_Aspects (Prev, Has_Delayed_Aspects (Id));
16717 Set_Has_Implicit_Dereference (Prev, Has_Implicit_Dereference (Id));
16719 -- If no error, propagate freeze_node from private to full view.
16720 -- It may have been generated for an early operational item.
16722 if Present (Freeze_Node (Id))
16723 and then Serious_Errors_Detected = 0
16724 and then No (Full_View (Id))
16725 then
16726 Set_Freeze_Node (Prev, Freeze_Node (Id));
16727 Set_Freeze_Node (Id, Empty);
16728 Set_First_Rep_Item (Prev, First_Rep_Item (Id));
16729 end if;
16731 Set_Full_View (Id, Prev);
16732 New_Id := Prev;
16733 end if;
16735 -- Verify that full declaration conforms to partial one
16737 if Is_Incomplete_Or_Private_Type (Prev)
16738 and then Present (Discriminant_Specifications (Prev_Par))
16739 then
16740 if Present (Discriminant_Specifications (N)) then
16741 if Ekind (Prev) = E_Incomplete_Type then
16742 Check_Discriminant_Conformance (N, Prev, Prev);
16743 else
16744 Check_Discriminant_Conformance (N, Prev, Id);
16745 end if;
16747 else
16748 Error_Msg_N
16749 ("missing discriminants in full type declaration", N);
16751 -- To avoid cascaded errors on subsequent use, share the
16752 -- discriminants of the partial view.
16754 Set_Discriminant_Specifications (N,
16755 Discriminant_Specifications (Prev_Par));
16756 end if;
16757 end if;
16759 -- A prior untagged partial view can have an associated class-wide
16760 -- type due to use of the class attribute, and in this case the full
16761 -- type must also be tagged. This Ada 95 usage is deprecated in favor
16762 -- of incomplete tagged declarations, but we check for it.
16764 if Is_Type (Prev)
16765 and then (Is_Tagged_Type (Prev)
16766 or else Present (Class_Wide_Type (Prev)))
16767 then
16768 -- Ada 2012 (AI05-0162): A private type may be the completion of
16769 -- an incomplete type.
16771 if Ada_Version >= Ada_2012
16772 and then Is_Incomplete_Type (Prev)
16773 and then Nkind_In (N, N_Private_Type_Declaration,
16774 N_Private_Extension_Declaration)
16775 then
16776 -- No need to check private extensions since they are tagged
16778 if Nkind (N) = N_Private_Type_Declaration
16779 and then not Tagged_Present (N)
16780 then
16781 Tag_Mismatch;
16782 end if;
16784 -- The full declaration is either a tagged type (including
16785 -- a synchronized type that implements interfaces) or a
16786 -- type extension, otherwise this is an error.
16788 elsif Nkind_In (N, N_Task_Type_Declaration,
16789 N_Protected_Type_Declaration)
16790 then
16791 if No (Interface_List (N)) and then not Error_Posted (N) then
16792 Tag_Mismatch;
16793 end if;
16795 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
16797 -- Indicate that the previous declaration (tagged incomplete
16798 -- or private declaration) requires the same on the full one.
16800 if not Tagged_Present (Type_Definition (N)) then
16801 Tag_Mismatch;
16802 Set_Is_Tagged_Type (Id);
16803 end if;
16805 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
16806 if No (Record_Extension_Part (Type_Definition (N))) then
16807 Error_Msg_NE
16808 ("full declaration of } must be a record extension",
16809 Prev, Id);
16811 -- Set some attributes to produce a usable full view
16813 Set_Is_Tagged_Type (Id);
16814 end if;
16816 else
16817 Tag_Mismatch;
16818 end if;
16819 end if;
16821 if Present (Prev)
16822 and then Nkind (Parent (Prev)) = N_Incomplete_Type_Declaration
16823 and then Present (Premature_Use (Parent (Prev)))
16824 then
16825 Error_Msg_Sloc := Sloc (N);
16826 Error_Msg_N
16827 ("\full declaration #", Premature_Use (Parent (Prev)));
16828 end if;
16830 return New_Id;
16831 end if;
16832 end Find_Type_Name;
16834 -------------------------
16835 -- Find_Type_Of_Object --
16836 -------------------------
16838 function Find_Type_Of_Object
16839 (Obj_Def : Node_Id;
16840 Related_Nod : Node_Id) return Entity_Id
16842 Def_Kind : constant Node_Kind := Nkind (Obj_Def);
16843 P : Node_Id := Parent (Obj_Def);
16844 T : Entity_Id;
16845 Nam : Name_Id;
16847 begin
16848 -- If the parent is a component_definition node we climb to the
16849 -- component_declaration node
16851 if Nkind (P) = N_Component_Definition then
16852 P := Parent (P);
16853 end if;
16855 -- Case of an anonymous array subtype
16857 if Nkind_In (Def_Kind, N_Constrained_Array_Definition,
16858 N_Unconstrained_Array_Definition)
16859 then
16860 T := Empty;
16861 Array_Type_Declaration (T, Obj_Def);
16863 -- Create an explicit subtype whenever possible
16865 elsif Nkind (P) /= N_Component_Declaration
16866 and then Def_Kind = N_Subtype_Indication
16867 then
16868 -- Base name of subtype on object name, which will be unique in
16869 -- the current scope.
16871 -- If this is a duplicate declaration, return base type, to avoid
16872 -- generating duplicate anonymous types.
16874 if Error_Posted (P) then
16875 Analyze (Subtype_Mark (Obj_Def));
16876 return Entity (Subtype_Mark (Obj_Def));
16877 end if;
16879 Nam :=
16880 New_External_Name
16881 (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
16883 T := Make_Defining_Identifier (Sloc (P), Nam);
16885 Insert_Action (Obj_Def,
16886 Make_Subtype_Declaration (Sloc (P),
16887 Defining_Identifier => T,
16888 Subtype_Indication => Relocate_Node (Obj_Def)));
16890 -- This subtype may need freezing, and this will not be done
16891 -- automatically if the object declaration is not in declarative
16892 -- part. Since this is an object declaration, the type cannot always
16893 -- be frozen here. Deferred constants do not freeze their type
16894 -- (which often enough will be private).
16896 if Nkind (P) = N_Object_Declaration
16897 and then Constant_Present (P)
16898 and then No (Expression (P))
16899 then
16900 null;
16902 -- Here we freeze the base type of object type to catch premature use
16903 -- of discriminated private type without a full view.
16905 else
16906 Insert_Actions (Obj_Def, Freeze_Entity (Base_Type (T), P));
16907 end if;
16909 -- Ada 2005 AI-406: the object definition in an object declaration
16910 -- can be an access definition.
16912 elsif Def_Kind = N_Access_Definition then
16913 T := Access_Definition (Related_Nod, Obj_Def);
16915 Set_Is_Local_Anonymous_Access
16917 V => (Ada_Version < Ada_2012)
16918 or else (Nkind (P) /= N_Object_Declaration)
16919 or else Is_Library_Level_Entity (Defining_Identifier (P)));
16921 -- Otherwise, the object definition is just a subtype_mark
16923 else
16924 T := Process_Subtype (Obj_Def, Related_Nod);
16926 -- If expansion is disabled an object definition that is an aggregate
16927 -- will not get expanded and may lead to scoping problems in the back
16928 -- end, if the object is referenced in an inner scope. In that case
16929 -- create an itype reference for the object definition now. This
16930 -- may be redundant in some cases, but harmless.
16932 if Is_Itype (T)
16933 and then Nkind (Related_Nod) = N_Object_Declaration
16934 and then ASIS_Mode
16935 then
16936 Build_Itype_Reference (T, Related_Nod);
16937 end if;
16938 end if;
16940 return T;
16941 end Find_Type_Of_Object;
16943 --------------------------------
16944 -- Find_Type_Of_Subtype_Indic --
16945 --------------------------------
16947 function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
16948 Typ : Entity_Id;
16950 begin
16951 -- Case of subtype mark with a constraint
16953 if Nkind (S) = N_Subtype_Indication then
16954 Find_Type (Subtype_Mark (S));
16955 Typ := Entity (Subtype_Mark (S));
16957 if not
16958 Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
16959 then
16960 Error_Msg_N
16961 ("incorrect constraint for this kind of type", Constraint (S));
16962 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
16963 end if;
16965 -- Otherwise we have a subtype mark without a constraint
16967 elsif Error_Posted (S) then
16968 Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
16969 return Any_Type;
16971 else
16972 Find_Type (S);
16973 Typ := Entity (S);
16974 end if;
16976 -- Check No_Wide_Characters restriction
16978 Check_Wide_Character_Restriction (Typ, S);
16980 return Typ;
16981 end Find_Type_Of_Subtype_Indic;
16983 -------------------------------------
16984 -- Floating_Point_Type_Declaration --
16985 -------------------------------------
16987 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
16988 Digs : constant Node_Id := Digits_Expression (Def);
16989 Max_Digs_Val : constant Uint := Digits_Value (Standard_Long_Long_Float);
16990 Digs_Val : Uint;
16991 Base_Typ : Entity_Id;
16992 Implicit_Base : Entity_Id;
16993 Bound : Node_Id;
16995 function Can_Derive_From (E : Entity_Id) return Boolean;
16996 -- Find if given digits value, and possibly a specified range, allows
16997 -- derivation from specified type
16999 function Find_Base_Type return Entity_Id;
17000 -- Find a predefined base type that Def can derive from, or generate
17001 -- an error and substitute Long_Long_Float if none exists.
17003 ---------------------
17004 -- Can_Derive_From --
17005 ---------------------
17007 function Can_Derive_From (E : Entity_Id) return Boolean is
17008 Spec : constant Entity_Id := Real_Range_Specification (Def);
17010 begin
17011 -- Check specified "digits" constraint
17013 if Digs_Val > Digits_Value (E) then
17014 return False;
17015 end if;
17017 -- Check for matching range, if specified
17019 if Present (Spec) then
17020 if Expr_Value_R (Type_Low_Bound (E)) >
17021 Expr_Value_R (Low_Bound (Spec))
17022 then
17023 return False;
17024 end if;
17026 if Expr_Value_R (Type_High_Bound (E)) <
17027 Expr_Value_R (High_Bound (Spec))
17028 then
17029 return False;
17030 end if;
17031 end if;
17033 return True;
17034 end Can_Derive_From;
17036 --------------------
17037 -- Find_Base_Type --
17038 --------------------
17040 function Find_Base_Type return Entity_Id is
17041 Choice : Elmt_Id := First_Elmt (Predefined_Float_Types);
17043 begin
17044 -- Iterate over the predefined types in order, returning the first
17045 -- one that Def can derive from.
17047 while Present (Choice) loop
17048 if Can_Derive_From (Node (Choice)) then
17049 return Node (Choice);
17050 end if;
17052 Next_Elmt (Choice);
17053 end loop;
17055 -- If we can't derive from any existing type, use Long_Long_Float
17056 -- and give appropriate message explaining the problem.
17058 if Digs_Val > Max_Digs_Val then
17059 -- It might be the case that there is a type with the requested
17060 -- range, just not the combination of digits and range.
17062 Error_Msg_N
17063 ("no predefined type has requested range and precision",
17064 Real_Range_Specification (Def));
17066 else
17067 Error_Msg_N
17068 ("range too large for any predefined type",
17069 Real_Range_Specification (Def));
17070 end if;
17072 return Standard_Long_Long_Float;
17073 end Find_Base_Type;
17075 -- Start of processing for Floating_Point_Type_Declaration
17077 begin
17078 Check_Restriction (No_Floating_Point, Def);
17080 -- Create an implicit base type
17082 Implicit_Base :=
17083 Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
17085 -- Analyze and verify digits value
17087 Analyze_And_Resolve (Digs, Any_Integer);
17088 Check_Digits_Expression (Digs);
17089 Digs_Val := Expr_Value (Digs);
17091 -- Process possible range spec and find correct type to derive from
17093 Process_Real_Range_Specification (Def);
17095 -- Check that requested number of digits is not too high.
17097 if Digs_Val > Max_Digs_Val then
17099 -- The check for Max_Base_Digits may be somewhat expensive, as it
17100 -- requires reading System, so only do it when necessary.
17102 declare
17103 Max_Base_Digits : constant Uint :=
17104 Expr_Value
17105 (Expression
17106 (Parent (RTE (RE_Max_Base_Digits))));
17108 begin
17109 if Digs_Val > Max_Base_Digits then
17110 Error_Msg_Uint_1 := Max_Base_Digits;
17111 Error_Msg_N ("digits value out of range, maximum is ^", Digs);
17113 elsif No (Real_Range_Specification (Def)) then
17114 Error_Msg_Uint_1 := Max_Digs_Val;
17115 Error_Msg_N ("types with more than ^ digits need range spec "
17116 & "(RM 3.5.7(6))", Digs);
17117 end if;
17118 end;
17119 end if;
17121 -- Find a suitable type to derive from or complain and use a substitute
17123 Base_Typ := Find_Base_Type;
17125 -- If there are bounds given in the declaration use them as the bounds
17126 -- of the type, otherwise use the bounds of the predefined base type
17127 -- that was chosen based on the Digits value.
17129 if Present (Real_Range_Specification (Def)) then
17130 Set_Scalar_Range (T, Real_Range_Specification (Def));
17131 Set_Is_Constrained (T);
17133 -- The bounds of this range must be converted to machine numbers
17134 -- in accordance with RM 4.9(38).
17136 Bound := Type_Low_Bound (T);
17138 if Nkind (Bound) = N_Real_Literal then
17139 Set_Realval
17140 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
17141 Set_Is_Machine_Number (Bound);
17142 end if;
17144 Bound := Type_High_Bound (T);
17146 if Nkind (Bound) = N_Real_Literal then
17147 Set_Realval
17148 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
17149 Set_Is_Machine_Number (Bound);
17150 end if;
17152 else
17153 Set_Scalar_Range (T, Scalar_Range (Base_Typ));
17154 end if;
17156 -- Complete definition of implicit base and declared first subtype. The
17157 -- inheritance of the rep item chain ensures that SPARK-related pragmas
17158 -- are not clobbered when the floating point type acts as a full view of
17159 -- a private type.
17161 Set_Etype (Implicit_Base, Base_Typ);
17162 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
17163 Set_Size_Info (Implicit_Base, Base_Typ);
17164 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
17165 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
17166 Set_Digits_Value (Implicit_Base, Digits_Value (Base_Typ));
17167 Set_Float_Rep (Implicit_Base, Float_Rep (Base_Typ));
17169 Set_Ekind (T, E_Floating_Point_Subtype);
17170 Set_Etype (T, Implicit_Base);
17171 Set_Size_Info (T, Implicit_Base);
17172 Set_RM_Size (T, RM_Size (Implicit_Base));
17173 Inherit_Rep_Item_Chain (T, Implicit_Base);
17174 Set_Digits_Value (T, Digs_Val);
17175 end Floating_Point_Type_Declaration;
17177 ----------------------------
17178 -- Get_Discriminant_Value --
17179 ----------------------------
17181 -- This is the situation:
17183 -- There is a non-derived type
17185 -- type T0 (Dx, Dy, Dz...)
17187 -- There are zero or more levels of derivation, with each derivation
17188 -- either purely inheriting the discriminants, or defining its own.
17190 -- type Ti is new Ti-1
17191 -- or
17192 -- type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
17193 -- or
17194 -- subtype Ti is ...
17196 -- The subtype issue is avoided by the use of Original_Record_Component,
17197 -- and the fact that derived subtypes also derive the constraints.
17199 -- This chain leads back from
17201 -- Typ_For_Constraint
17203 -- Typ_For_Constraint has discriminants, and the value for each
17204 -- discriminant is given by its corresponding Elmt of Constraints.
17206 -- Discriminant is some discriminant in this hierarchy
17208 -- We need to return its value
17210 -- We do this by recursively searching each level, and looking for
17211 -- Discriminant. Once we get to the bottom, we start backing up
17212 -- returning the value for it which may in turn be a discriminant
17213 -- further up, so on the backup we continue the substitution.
17215 function Get_Discriminant_Value
17216 (Discriminant : Entity_Id;
17217 Typ_For_Constraint : Entity_Id;
17218 Constraint : Elist_Id) return Node_Id
17220 function Root_Corresponding_Discriminant
17221 (Discr : Entity_Id) return Entity_Id;
17222 -- Given a discriminant, traverse the chain of inherited discriminants
17223 -- and return the topmost discriminant.
17225 function Search_Derivation_Levels
17226 (Ti : Entity_Id;
17227 Discrim_Values : Elist_Id;
17228 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
17229 -- This is the routine that performs the recursive search of levels
17230 -- as described above.
17232 -------------------------------------
17233 -- Root_Corresponding_Discriminant --
17234 -------------------------------------
17236 function Root_Corresponding_Discriminant
17237 (Discr : Entity_Id) return Entity_Id
17239 D : Entity_Id;
17241 begin
17242 D := Discr;
17243 while Present (Corresponding_Discriminant (D)) loop
17244 D := Corresponding_Discriminant (D);
17245 end loop;
17247 return D;
17248 end Root_Corresponding_Discriminant;
17250 ------------------------------
17251 -- Search_Derivation_Levels --
17252 ------------------------------
17254 function Search_Derivation_Levels
17255 (Ti : Entity_Id;
17256 Discrim_Values : Elist_Id;
17257 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
17259 Assoc : Elmt_Id;
17260 Disc : Entity_Id;
17261 Result : Node_Or_Entity_Id;
17262 Result_Entity : Node_Id;
17264 begin
17265 -- If inappropriate type, return Error, this happens only in
17266 -- cascaded error situations, and we want to avoid a blow up.
17268 if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
17269 return Error;
17270 end if;
17272 -- Look deeper if possible. Use Stored_Constraints only for
17273 -- untagged types. For tagged types use the given constraint.
17274 -- This asymmetry needs explanation???
17276 if not Stored_Discrim_Values
17277 and then Present (Stored_Constraint (Ti))
17278 and then not Is_Tagged_Type (Ti)
17279 then
17280 Result :=
17281 Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
17282 else
17283 declare
17284 Td : constant Entity_Id := Etype (Ti);
17286 begin
17287 if Td = Ti then
17288 Result := Discriminant;
17290 else
17291 if Present (Stored_Constraint (Ti)) then
17292 Result :=
17293 Search_Derivation_Levels
17294 (Td, Stored_Constraint (Ti), True);
17295 else
17296 Result :=
17297 Search_Derivation_Levels
17298 (Td, Discrim_Values, Stored_Discrim_Values);
17299 end if;
17300 end if;
17301 end;
17302 end if;
17304 -- Extra underlying places to search, if not found above. For
17305 -- concurrent types, the relevant discriminant appears in the
17306 -- corresponding record. For a type derived from a private type
17307 -- without discriminant, the full view inherits the discriminants
17308 -- of the full view of the parent.
17310 if Result = Discriminant then
17311 if Is_Concurrent_Type (Ti)
17312 and then Present (Corresponding_Record_Type (Ti))
17313 then
17314 Result :=
17315 Search_Derivation_Levels (
17316 Corresponding_Record_Type (Ti),
17317 Discrim_Values,
17318 Stored_Discrim_Values);
17320 elsif Is_Private_Type (Ti)
17321 and then not Has_Discriminants (Ti)
17322 and then Present (Full_View (Ti))
17323 and then Etype (Full_View (Ti)) /= Ti
17324 then
17325 Result :=
17326 Search_Derivation_Levels (
17327 Full_View (Ti),
17328 Discrim_Values,
17329 Stored_Discrim_Values);
17330 end if;
17331 end if;
17333 -- If Result is not a (reference to a) discriminant, return it,
17334 -- otherwise set Result_Entity to the discriminant.
17336 if Nkind (Result) = N_Defining_Identifier then
17337 pragma Assert (Result = Discriminant);
17338 Result_Entity := Result;
17340 else
17341 if not Denotes_Discriminant (Result) then
17342 return Result;
17343 end if;
17345 Result_Entity := Entity (Result);
17346 end if;
17348 -- See if this level of derivation actually has discriminants because
17349 -- tagged derivations can add them, hence the lower levels need not
17350 -- have any.
17352 if not Has_Discriminants (Ti) then
17353 return Result;
17354 end if;
17356 -- Scan Ti's discriminants for Result_Entity, and return its
17357 -- corresponding value, if any.
17359 Result_Entity := Original_Record_Component (Result_Entity);
17361 Assoc := First_Elmt (Discrim_Values);
17363 if Stored_Discrim_Values then
17364 Disc := First_Stored_Discriminant (Ti);
17365 else
17366 Disc := First_Discriminant (Ti);
17367 end if;
17369 while Present (Disc) loop
17370 pragma Assert (Present (Assoc));
17372 if Original_Record_Component (Disc) = Result_Entity then
17373 return Node (Assoc);
17374 end if;
17376 Next_Elmt (Assoc);
17378 if Stored_Discrim_Values then
17379 Next_Stored_Discriminant (Disc);
17380 else
17381 Next_Discriminant (Disc);
17382 end if;
17383 end loop;
17385 -- Could not find it
17387 return Result;
17388 end Search_Derivation_Levels;
17390 -- Local Variables
17392 Result : Node_Or_Entity_Id;
17394 -- Start of processing for Get_Discriminant_Value
17396 begin
17397 -- ??? This routine is a gigantic mess and will be deleted. For the
17398 -- time being just test for the trivial case before calling recurse.
17400 if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
17401 declare
17402 D : Entity_Id;
17403 E : Elmt_Id;
17405 begin
17406 D := First_Discriminant (Typ_For_Constraint);
17407 E := First_Elmt (Constraint);
17408 while Present (D) loop
17409 if Chars (D) = Chars (Discriminant) then
17410 return Node (E);
17411 end if;
17413 Next_Discriminant (D);
17414 Next_Elmt (E);
17415 end loop;
17416 end;
17417 end if;
17419 Result := Search_Derivation_Levels
17420 (Typ_For_Constraint, Constraint, False);
17422 -- ??? hack to disappear when this routine is gone
17424 if Nkind (Result) = N_Defining_Identifier then
17425 declare
17426 D : Entity_Id;
17427 E : Elmt_Id;
17429 begin
17430 D := First_Discriminant (Typ_For_Constraint);
17431 E := First_Elmt (Constraint);
17432 while Present (D) loop
17433 if Root_Corresponding_Discriminant (D) = Discriminant then
17434 return Node (E);
17435 end if;
17437 Next_Discriminant (D);
17438 Next_Elmt (E);
17439 end loop;
17440 end;
17441 end if;
17443 pragma Assert (Nkind (Result) /= N_Defining_Identifier);
17444 return Result;
17445 end Get_Discriminant_Value;
17447 --------------------------
17448 -- Has_Range_Constraint --
17449 --------------------------
17451 function Has_Range_Constraint (N : Node_Id) return Boolean is
17452 C : constant Node_Id := Constraint (N);
17454 begin
17455 if Nkind (C) = N_Range_Constraint then
17456 return True;
17458 elsif Nkind (C) = N_Digits_Constraint then
17459 return
17460 Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
17461 or else Present (Range_Constraint (C));
17463 elsif Nkind (C) = N_Delta_Constraint then
17464 return Present (Range_Constraint (C));
17466 else
17467 return False;
17468 end if;
17469 end Has_Range_Constraint;
17471 ------------------------
17472 -- Inherit_Components --
17473 ------------------------
17475 function Inherit_Components
17476 (N : Node_Id;
17477 Parent_Base : Entity_Id;
17478 Derived_Base : Entity_Id;
17479 Is_Tagged : Boolean;
17480 Inherit_Discr : Boolean;
17481 Discs : Elist_Id) return Elist_Id
17483 Assoc_List : constant Elist_Id := New_Elmt_List;
17485 procedure Inherit_Component
17486 (Old_C : Entity_Id;
17487 Plain_Discrim : Boolean := False;
17488 Stored_Discrim : Boolean := False);
17489 -- Inherits component Old_C from Parent_Base to the Derived_Base. If
17490 -- Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
17491 -- True, Old_C is a stored discriminant. If they are both false then
17492 -- Old_C is a regular component.
17494 -----------------------
17495 -- Inherit_Component --
17496 -----------------------
17498 procedure Inherit_Component
17499 (Old_C : Entity_Id;
17500 Plain_Discrim : Boolean := False;
17501 Stored_Discrim : Boolean := False)
17503 procedure Set_Anonymous_Type (Id : Entity_Id);
17504 -- Id denotes the entity of an access discriminant or anonymous
17505 -- access component. Set the type of Id to either the same type of
17506 -- Old_C or create a new one depending on whether the parent and
17507 -- the child types are in the same scope.
17509 ------------------------
17510 -- Set_Anonymous_Type --
17511 ------------------------
17513 procedure Set_Anonymous_Type (Id : Entity_Id) is
17514 Old_Typ : constant Entity_Id := Etype (Old_C);
17516 begin
17517 if Scope (Parent_Base) = Scope (Derived_Base) then
17518 Set_Etype (Id, Old_Typ);
17520 -- The parent and the derived type are in two different scopes.
17521 -- Reuse the type of the original discriminant / component by
17522 -- copying it in order to preserve all attributes.
17524 else
17525 declare
17526 Typ : constant Entity_Id := New_Copy (Old_Typ);
17528 begin
17529 Set_Etype (Id, Typ);
17531 -- Since we do not generate component declarations for
17532 -- inherited components, associate the itype with the
17533 -- derived type.
17535 Set_Associated_Node_For_Itype (Typ, Parent (Derived_Base));
17536 Set_Scope (Typ, Derived_Base);
17537 end;
17538 end if;
17539 end Set_Anonymous_Type;
17541 -- Local variables and constants
17543 New_C : constant Entity_Id := New_Copy (Old_C);
17545 Corr_Discrim : Entity_Id;
17546 Discrim : Entity_Id;
17548 -- Start of processing for Inherit_Component
17550 begin
17551 pragma Assert (not Is_Tagged or not Stored_Discrim);
17553 Set_Parent (New_C, Parent (Old_C));
17555 -- Regular discriminants and components must be inserted in the scope
17556 -- of the Derived_Base. Do it here.
17558 if not Stored_Discrim then
17559 Enter_Name (New_C);
17560 end if;
17562 -- For tagged types the Original_Record_Component must point to
17563 -- whatever this field was pointing to in the parent type. This has
17564 -- already been achieved by the call to New_Copy above.
17566 if not Is_Tagged then
17567 Set_Original_Record_Component (New_C, New_C);
17568 end if;
17570 -- Set the proper type of an access discriminant
17572 if Ekind (New_C) = E_Discriminant
17573 and then Ekind (Etype (New_C)) = E_Anonymous_Access_Type
17574 then
17575 Set_Anonymous_Type (New_C);
17576 end if;
17578 -- If we have inherited a component then see if its Etype contains
17579 -- references to Parent_Base discriminants. In this case, replace
17580 -- these references with the constraints given in Discs. We do not
17581 -- do this for the partial view of private types because this is
17582 -- not needed (only the components of the full view will be used
17583 -- for code generation) and cause problem. We also avoid this
17584 -- transformation in some error situations.
17586 if Ekind (New_C) = E_Component then
17588 -- Set the proper type of an anonymous access component
17590 if Ekind (Etype (New_C)) = E_Anonymous_Access_Type then
17591 Set_Anonymous_Type (New_C);
17593 elsif (Is_Private_Type (Derived_Base)
17594 and then not Is_Generic_Type (Derived_Base))
17595 or else (Is_Empty_Elmt_List (Discs)
17596 and then not Expander_Active)
17597 then
17598 Set_Etype (New_C, Etype (Old_C));
17600 else
17601 -- The current component introduces a circularity of the
17602 -- following kind:
17604 -- limited with Pack_2;
17605 -- package Pack_1 is
17606 -- type T_1 is tagged record
17607 -- Comp : access Pack_2.T_2;
17608 -- ...
17609 -- end record;
17610 -- end Pack_1;
17612 -- with Pack_1;
17613 -- package Pack_2 is
17614 -- type T_2 is new Pack_1.T_1 with ...;
17615 -- end Pack_2;
17617 Set_Etype
17618 (New_C,
17619 Constrain_Component_Type
17620 (Old_C, Derived_Base, N, Parent_Base, Discs));
17621 end if;
17622 end if;
17624 -- In derived tagged types it is illegal to reference a non
17625 -- discriminant component in the parent type. To catch this, mark
17626 -- these components with an Ekind of E_Void. This will be reset in
17627 -- Record_Type_Definition after processing the record extension of
17628 -- the derived type.
17630 -- If the declaration is a private extension, there is no further
17631 -- record extension to process, and the components retain their
17632 -- current kind, because they are visible at this point.
17634 if Is_Tagged and then Ekind (New_C) = E_Component
17635 and then Nkind (N) /= N_Private_Extension_Declaration
17636 then
17637 Set_Ekind (New_C, E_Void);
17638 end if;
17640 if Plain_Discrim then
17641 Set_Corresponding_Discriminant (New_C, Old_C);
17642 Build_Discriminal (New_C);
17644 -- If we are explicitly inheriting a stored discriminant it will be
17645 -- completely hidden.
17647 elsif Stored_Discrim then
17648 Set_Corresponding_Discriminant (New_C, Empty);
17649 Set_Discriminal (New_C, Empty);
17650 Set_Is_Completely_Hidden (New_C);
17652 -- Set the Original_Record_Component of each discriminant in the
17653 -- derived base to point to the corresponding stored that we just
17654 -- created.
17656 Discrim := First_Discriminant (Derived_Base);
17657 while Present (Discrim) loop
17658 Corr_Discrim := Corresponding_Discriminant (Discrim);
17660 -- Corr_Discrim could be missing in an error situation
17662 if Present (Corr_Discrim)
17663 and then Original_Record_Component (Corr_Discrim) = Old_C
17664 then
17665 Set_Original_Record_Component (Discrim, New_C);
17666 end if;
17668 Next_Discriminant (Discrim);
17669 end loop;
17671 Append_Entity (New_C, Derived_Base);
17672 end if;
17674 if not Is_Tagged then
17675 Append_Elmt (Old_C, Assoc_List);
17676 Append_Elmt (New_C, Assoc_List);
17677 end if;
17678 end Inherit_Component;
17680 -- Variables local to Inherit_Component
17682 Loc : constant Source_Ptr := Sloc (N);
17684 Parent_Discrim : Entity_Id;
17685 Stored_Discrim : Entity_Id;
17686 D : Entity_Id;
17687 Component : Entity_Id;
17689 -- Start of processing for Inherit_Components
17691 begin
17692 if not Is_Tagged then
17693 Append_Elmt (Parent_Base, Assoc_List);
17694 Append_Elmt (Derived_Base, Assoc_List);
17695 end if;
17697 -- Inherit parent discriminants if needed
17699 if Inherit_Discr then
17700 Parent_Discrim := First_Discriminant (Parent_Base);
17701 while Present (Parent_Discrim) loop
17702 Inherit_Component (Parent_Discrim, Plain_Discrim => True);
17703 Next_Discriminant (Parent_Discrim);
17704 end loop;
17705 end if;
17707 -- Create explicit stored discrims for untagged types when necessary
17709 if not Has_Unknown_Discriminants (Derived_Base)
17710 and then Has_Discriminants (Parent_Base)
17711 and then not Is_Tagged
17712 and then
17713 (not Inherit_Discr
17714 or else First_Discriminant (Parent_Base) /=
17715 First_Stored_Discriminant (Parent_Base))
17716 then
17717 Stored_Discrim := First_Stored_Discriminant (Parent_Base);
17718 while Present (Stored_Discrim) loop
17719 Inherit_Component (Stored_Discrim, Stored_Discrim => True);
17720 Next_Stored_Discriminant (Stored_Discrim);
17721 end loop;
17722 end if;
17724 -- See if we can apply the second transformation for derived types, as
17725 -- explained in point 6. in the comments above Build_Derived_Record_Type
17726 -- This is achieved by appending Derived_Base discriminants into Discs,
17727 -- which has the side effect of returning a non empty Discs list to the
17728 -- caller of Inherit_Components, which is what we want. This must be
17729 -- done for private derived types if there are explicit stored
17730 -- discriminants, to ensure that we can retrieve the values of the
17731 -- constraints provided in the ancestors.
17733 if Inherit_Discr
17734 and then Is_Empty_Elmt_List (Discs)
17735 and then Present (First_Discriminant (Derived_Base))
17736 and then
17737 (not Is_Private_Type (Derived_Base)
17738 or else Is_Completely_Hidden
17739 (First_Stored_Discriminant (Derived_Base))
17740 or else Is_Generic_Type (Derived_Base))
17741 then
17742 D := First_Discriminant (Derived_Base);
17743 while Present (D) loop
17744 Append_Elmt (New_Occurrence_Of (D, Loc), Discs);
17745 Next_Discriminant (D);
17746 end loop;
17747 end if;
17749 -- Finally, inherit non-discriminant components unless they are not
17750 -- visible because defined or inherited from the full view of the
17751 -- parent. Don't inherit the _parent field of the parent type.
17753 Component := First_Entity (Parent_Base);
17754 while Present (Component) loop
17756 -- Ada 2005 (AI-251): Do not inherit components associated with
17757 -- secondary tags of the parent.
17759 if Ekind (Component) = E_Component
17760 and then Present (Related_Type (Component))
17761 then
17762 null;
17764 elsif Ekind (Component) /= E_Component
17765 or else Chars (Component) = Name_uParent
17766 then
17767 null;
17769 -- If the derived type is within the parent type's declarative
17770 -- region, then the components can still be inherited even though
17771 -- they aren't visible at this point. This can occur for cases
17772 -- such as within public child units where the components must
17773 -- become visible upon entering the child unit's private part.
17775 elsif not Is_Visible_Component (Component)
17776 and then not In_Open_Scopes (Scope (Parent_Base))
17777 then
17778 null;
17780 elsif Ekind_In (Derived_Base, E_Private_Type,
17781 E_Limited_Private_Type)
17782 then
17783 null;
17785 else
17786 Inherit_Component (Component);
17787 end if;
17789 Next_Entity (Component);
17790 end loop;
17792 -- For tagged derived types, inherited discriminants cannot be used in
17793 -- component declarations of the record extension part. To achieve this
17794 -- we mark the inherited discriminants as not visible.
17796 if Is_Tagged and then Inherit_Discr then
17797 D := First_Discriminant (Derived_Base);
17798 while Present (D) loop
17799 Set_Is_Immediately_Visible (D, False);
17800 Next_Discriminant (D);
17801 end loop;
17802 end if;
17804 return Assoc_List;
17805 end Inherit_Components;
17807 -----------------------------
17808 -- Inherit_Predicate_Flags --
17809 -----------------------------
17811 procedure Inherit_Predicate_Flags (Subt, Par : Entity_Id) is
17812 begin
17813 Set_Has_Predicates (Subt, Has_Predicates (Par));
17814 Set_Has_Static_Predicate_Aspect
17815 (Subt, Has_Static_Predicate_Aspect (Par));
17816 Set_Has_Dynamic_Predicate_Aspect
17817 (Subt, Has_Dynamic_Predicate_Aspect (Par));
17818 end Inherit_Predicate_Flags;
17820 ----------------------
17821 -- Is_EVF_Procedure --
17822 ----------------------
17824 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean is
17825 Formal : Entity_Id;
17827 begin
17828 -- Examine the formals of an Extensions_Visible False procedure looking
17829 -- for a controlling OUT parameter.
17831 if Ekind (Subp) = E_Procedure
17832 and then Extensions_Visible_Status (Subp) = Extensions_Visible_False
17833 then
17834 Formal := First_Formal (Subp);
17835 while Present (Formal) loop
17836 if Ekind (Formal) = E_Out_Parameter
17837 and then Is_Controlling_Formal (Formal)
17838 then
17839 return True;
17840 end if;
17842 Next_Formal (Formal);
17843 end loop;
17844 end if;
17846 return False;
17847 end Is_EVF_Procedure;
17849 -----------------------
17850 -- Is_Null_Extension --
17851 -----------------------
17853 function Is_Null_Extension (T : Entity_Id) return Boolean is
17854 Type_Decl : constant Node_Id := Parent (Base_Type (T));
17855 Comp_List : Node_Id;
17856 Comp : Node_Id;
17858 begin
17859 if Nkind (Type_Decl) /= N_Full_Type_Declaration
17860 or else not Is_Tagged_Type (T)
17861 or else Nkind (Type_Definition (Type_Decl)) /=
17862 N_Derived_Type_Definition
17863 or else No (Record_Extension_Part (Type_Definition (Type_Decl)))
17864 then
17865 return False;
17866 end if;
17868 Comp_List :=
17869 Component_List (Record_Extension_Part (Type_Definition (Type_Decl)));
17871 if Present (Discriminant_Specifications (Type_Decl)) then
17872 return False;
17874 elsif Present (Comp_List)
17875 and then Is_Non_Empty_List (Component_Items (Comp_List))
17876 then
17877 Comp := First (Component_Items (Comp_List));
17879 -- Only user-defined components are relevant. The component list
17880 -- may also contain a parent component and internal components
17881 -- corresponding to secondary tags, but these do not determine
17882 -- whether this is a null extension.
17884 while Present (Comp) loop
17885 if Comes_From_Source (Comp) then
17886 return False;
17887 end if;
17889 Next (Comp);
17890 end loop;
17892 return True;
17894 else
17895 return True;
17896 end if;
17897 end Is_Null_Extension;
17899 ------------------------------
17900 -- Is_Valid_Constraint_Kind --
17901 ------------------------------
17903 function Is_Valid_Constraint_Kind
17904 (T_Kind : Type_Kind;
17905 Constraint_Kind : Node_Kind) return Boolean
17907 begin
17908 case T_Kind is
17909 when Enumeration_Kind |
17910 Integer_Kind =>
17911 return Constraint_Kind = N_Range_Constraint;
17913 when Decimal_Fixed_Point_Kind =>
17914 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
17915 N_Range_Constraint);
17917 when Ordinary_Fixed_Point_Kind =>
17918 return Nkind_In (Constraint_Kind, N_Delta_Constraint,
17919 N_Range_Constraint);
17921 when Float_Kind =>
17922 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
17923 N_Range_Constraint);
17925 when Access_Kind |
17926 Array_Kind |
17927 E_Record_Type |
17928 E_Record_Subtype |
17929 Class_Wide_Kind |
17930 E_Incomplete_Type |
17931 Private_Kind |
17932 Concurrent_Kind =>
17933 return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
17935 when others =>
17936 return True; -- Error will be detected later
17937 end case;
17938 end Is_Valid_Constraint_Kind;
17940 --------------------------
17941 -- Is_Visible_Component --
17942 --------------------------
17944 function Is_Visible_Component
17945 (C : Entity_Id;
17946 N : Node_Id := Empty) return Boolean
17948 Original_Comp : Entity_Id := Empty;
17949 Original_Type : Entity_Id;
17950 Type_Scope : Entity_Id;
17952 function Is_Local_Type (Typ : Entity_Id) return Boolean;
17953 -- Check whether parent type of inherited component is declared locally,
17954 -- possibly within a nested package or instance. The current scope is
17955 -- the derived record itself.
17957 -------------------
17958 -- Is_Local_Type --
17959 -------------------
17961 function Is_Local_Type (Typ : Entity_Id) return Boolean is
17962 Scop : Entity_Id;
17964 begin
17965 Scop := Scope (Typ);
17966 while Present (Scop)
17967 and then Scop /= Standard_Standard
17968 loop
17969 if Scop = Scope (Current_Scope) then
17970 return True;
17971 end if;
17973 Scop := Scope (Scop);
17974 end loop;
17976 return False;
17977 end Is_Local_Type;
17979 -- Start of processing for Is_Visible_Component
17981 begin
17982 if Ekind_In (C, E_Component, E_Discriminant) then
17983 Original_Comp := Original_Record_Component (C);
17984 end if;
17986 if No (Original_Comp) then
17988 -- Premature usage, or previous error
17990 return False;
17992 else
17993 Original_Type := Scope (Original_Comp);
17994 Type_Scope := Scope (Base_Type (Scope (C)));
17995 end if;
17997 -- This test only concerns tagged types
17999 if not Is_Tagged_Type (Original_Type) then
18000 return True;
18002 -- If it is _Parent or _Tag, there is no visibility issue
18004 elsif not Comes_From_Source (Original_Comp) then
18005 return True;
18007 -- Discriminants are visible unless the (private) type has unknown
18008 -- discriminants. If the discriminant reference is inserted for a
18009 -- discriminant check on a full view it is also visible.
18011 elsif Ekind (Original_Comp) = E_Discriminant
18012 and then
18013 (not Has_Unknown_Discriminants (Original_Type)
18014 or else (Present (N)
18015 and then Nkind (N) = N_Selected_Component
18016 and then Nkind (Prefix (N)) = N_Type_Conversion
18017 and then not Comes_From_Source (Prefix (N))))
18018 then
18019 return True;
18021 -- In the body of an instantiation, no need to check for the visibility
18022 -- of a component.
18024 elsif In_Instance_Body then
18025 return True;
18027 -- If the component has been declared in an ancestor which is currently
18028 -- a private type, then it is not visible. The same applies if the
18029 -- component's containing type is not in an open scope and the original
18030 -- component's enclosing type is a visible full view of a private type
18031 -- (which can occur in cases where an attempt is being made to reference
18032 -- a component in a sibling package that is inherited from a visible
18033 -- component of a type in an ancestor package; the component in the
18034 -- sibling package should not be visible even though the component it
18035 -- inherited from is visible). This does not apply however in the case
18036 -- where the scope of the type is a private child unit, or when the
18037 -- parent comes from a local package in which the ancestor is currently
18038 -- visible. The latter suppression of visibility is needed for cases
18039 -- that are tested in B730006.
18041 elsif Is_Private_Type (Original_Type)
18042 or else
18043 (not Is_Private_Descendant (Type_Scope)
18044 and then not In_Open_Scopes (Type_Scope)
18045 and then Has_Private_Declaration (Original_Type))
18046 then
18047 -- If the type derives from an entity in a formal package, there
18048 -- are no additional visible components.
18050 if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
18051 N_Formal_Package_Declaration
18052 then
18053 return False;
18055 -- if we are not in the private part of the current package, there
18056 -- are no additional visible components.
18058 elsif Ekind (Scope (Current_Scope)) = E_Package
18059 and then not In_Private_Part (Scope (Current_Scope))
18060 then
18061 return False;
18062 else
18063 return
18064 Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
18065 and then In_Open_Scopes (Scope (Original_Type))
18066 and then Is_Local_Type (Type_Scope);
18067 end if;
18069 -- There is another weird way in which a component may be invisible when
18070 -- the private and the full view are not derived from the same ancestor.
18071 -- Here is an example :
18073 -- type A1 is tagged record F1 : integer; end record;
18074 -- type A2 is new A1 with record F2 : integer; end record;
18075 -- type T is new A1 with private;
18076 -- private
18077 -- type T is new A2 with null record;
18079 -- In this case, the full view of T inherits F1 and F2 but the private
18080 -- view inherits only F1
18082 else
18083 declare
18084 Ancestor : Entity_Id := Scope (C);
18086 begin
18087 loop
18088 if Ancestor = Original_Type then
18089 return True;
18091 -- The ancestor may have a partial view of the original
18092 -- type, but if the full view is in scope, as in a child
18093 -- body, the component is visible.
18095 elsif In_Private_Part (Scope (Original_Type))
18096 and then Full_View (Ancestor) = Original_Type
18097 then
18098 return True;
18100 elsif Ancestor = Etype (Ancestor) then
18102 -- No further ancestors to examine.
18104 return False;
18105 end if;
18107 Ancestor := Etype (Ancestor);
18108 end loop;
18109 end;
18110 end if;
18111 end Is_Visible_Component;
18113 --------------------------
18114 -- Make_Class_Wide_Type --
18115 --------------------------
18117 procedure Make_Class_Wide_Type (T : Entity_Id) is
18118 CW_Type : Entity_Id;
18119 CW_Name : Name_Id;
18120 Next_E : Entity_Id;
18122 begin
18123 if Present (Class_Wide_Type (T)) then
18125 -- The class-wide type is a partially decorated entity created for a
18126 -- unanalyzed tagged type referenced through a limited with clause.
18127 -- When the tagged type is analyzed, its class-wide type needs to be
18128 -- redecorated. Note that we reuse the entity created by Decorate_
18129 -- Tagged_Type in order to preserve all links.
18131 if Materialize_Entity (Class_Wide_Type (T)) then
18132 CW_Type := Class_Wide_Type (T);
18133 Set_Materialize_Entity (CW_Type, False);
18135 -- The class wide type can have been defined by the partial view, in
18136 -- which case everything is already done.
18138 else
18139 return;
18140 end if;
18142 -- Default case, we need to create a new class-wide type
18144 else
18145 CW_Type :=
18146 New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
18147 end if;
18149 -- Inherit root type characteristics
18151 CW_Name := Chars (CW_Type);
18152 Next_E := Next_Entity (CW_Type);
18153 Copy_Node (T, CW_Type);
18154 Set_Comes_From_Source (CW_Type, False);
18155 Set_Chars (CW_Type, CW_Name);
18156 Set_Parent (CW_Type, Parent (T));
18157 Set_Next_Entity (CW_Type, Next_E);
18159 -- Ensure we have a new freeze node for the class-wide type. The partial
18160 -- view may have freeze action of its own, requiring a proper freeze
18161 -- node, and the same freeze node cannot be shared between the two
18162 -- types.
18164 Set_Has_Delayed_Freeze (CW_Type);
18165 Set_Freeze_Node (CW_Type, Empty);
18167 -- Customize the class-wide type: It has no prim. op., it cannot be
18168 -- abstract and its Etype points back to the specific root type.
18170 Set_Ekind (CW_Type, E_Class_Wide_Type);
18171 Set_Is_Tagged_Type (CW_Type, True);
18172 Set_Direct_Primitive_Operations (CW_Type, New_Elmt_List);
18173 Set_Is_Abstract_Type (CW_Type, False);
18174 Set_Is_Constrained (CW_Type, False);
18175 Set_Is_First_Subtype (CW_Type, Is_First_Subtype (T));
18176 Set_Default_SSO (CW_Type);
18178 if Ekind (T) = E_Class_Wide_Subtype then
18179 Set_Etype (CW_Type, Etype (Base_Type (T)));
18180 else
18181 Set_Etype (CW_Type, T);
18182 end if;
18184 Set_No_Tagged_Streams_Pragma (CW_Type, No_Tagged_Streams);
18186 -- If this is the class_wide type of a constrained subtype, it does
18187 -- not have discriminants.
18189 Set_Has_Discriminants (CW_Type,
18190 Has_Discriminants (T) and then not Is_Constrained (T));
18192 Set_Has_Unknown_Discriminants (CW_Type, True);
18193 Set_Class_Wide_Type (T, CW_Type);
18194 Set_Equivalent_Type (CW_Type, Empty);
18196 -- The class-wide type of a class-wide type is itself (RM 3.9(14))
18198 Set_Class_Wide_Type (CW_Type, CW_Type);
18199 end Make_Class_Wide_Type;
18201 ----------------
18202 -- Make_Index --
18203 ----------------
18205 procedure Make_Index
18206 (N : Node_Id;
18207 Related_Nod : Node_Id;
18208 Related_Id : Entity_Id := Empty;
18209 Suffix_Index : Nat := 1;
18210 In_Iter_Schm : Boolean := False)
18212 R : Node_Id;
18213 T : Entity_Id;
18214 Def_Id : Entity_Id := Empty;
18215 Found : Boolean := False;
18217 begin
18218 -- For a discrete range used in a constrained array definition and
18219 -- defined by a range, an implicit conversion to the predefined type
18220 -- INTEGER is assumed if each bound is either a numeric literal, a named
18221 -- number, or an attribute, and the type of both bounds (prior to the
18222 -- implicit conversion) is the type universal_integer. Otherwise, both
18223 -- bounds must be of the same discrete type, other than universal
18224 -- integer; this type must be determinable independently of the
18225 -- context, but using the fact that the type must be discrete and that
18226 -- both bounds must have the same type.
18228 -- Character literals also have a universal type in the absence of
18229 -- of additional context, and are resolved to Standard_Character.
18231 if Nkind (N) = N_Range then
18233 -- The index is given by a range constraint. The bounds are known
18234 -- to be of a consistent type.
18236 if not Is_Overloaded (N) then
18237 T := Etype (N);
18239 -- For universal bounds, choose the specific predefined type
18241 if T = Universal_Integer then
18242 T := Standard_Integer;
18244 elsif T = Any_Character then
18245 Ambiguous_Character (Low_Bound (N));
18247 T := Standard_Character;
18248 end if;
18250 -- The node may be overloaded because some user-defined operators
18251 -- are available, but if a universal interpretation exists it is
18252 -- also the selected one.
18254 elsif Universal_Interpretation (N) = Universal_Integer then
18255 T := Standard_Integer;
18257 else
18258 T := Any_Type;
18260 declare
18261 Ind : Interp_Index;
18262 It : Interp;
18264 begin
18265 Get_First_Interp (N, Ind, It);
18266 while Present (It.Typ) loop
18267 if Is_Discrete_Type (It.Typ) then
18269 if Found
18270 and then not Covers (It.Typ, T)
18271 and then not Covers (T, It.Typ)
18272 then
18273 Error_Msg_N ("ambiguous bounds in discrete range", N);
18274 exit;
18275 else
18276 T := It.Typ;
18277 Found := True;
18278 end if;
18279 end if;
18281 Get_Next_Interp (Ind, It);
18282 end loop;
18284 if T = Any_Type then
18285 Error_Msg_N ("discrete type required for range", N);
18286 Set_Etype (N, Any_Type);
18287 return;
18289 elsif T = Universal_Integer then
18290 T := Standard_Integer;
18291 end if;
18292 end;
18293 end if;
18295 if not Is_Discrete_Type (T) then
18296 Error_Msg_N ("discrete type required for range", N);
18297 Set_Etype (N, Any_Type);
18298 return;
18299 end if;
18301 if Nkind (Low_Bound (N)) = N_Attribute_Reference
18302 and then Attribute_Name (Low_Bound (N)) = Name_First
18303 and then Is_Entity_Name (Prefix (Low_Bound (N)))
18304 and then Is_Type (Entity (Prefix (Low_Bound (N))))
18305 and then Is_Discrete_Type (Entity (Prefix (Low_Bound (N))))
18306 then
18307 -- The type of the index will be the type of the prefix, as long
18308 -- as the upper bound is 'Last of the same type.
18310 Def_Id := Entity (Prefix (Low_Bound (N)));
18312 if Nkind (High_Bound (N)) /= N_Attribute_Reference
18313 or else Attribute_Name (High_Bound (N)) /= Name_Last
18314 or else not Is_Entity_Name (Prefix (High_Bound (N)))
18315 or else Entity (Prefix (High_Bound (N))) /= Def_Id
18316 then
18317 Def_Id := Empty;
18318 end if;
18319 end if;
18321 R := N;
18322 Process_Range_Expr_In_Decl (R, T, In_Iter_Schm => In_Iter_Schm);
18324 elsif Nkind (N) = N_Subtype_Indication then
18326 -- The index is given by a subtype with a range constraint
18328 T := Base_Type (Entity (Subtype_Mark (N)));
18330 if not Is_Discrete_Type (T) then
18331 Error_Msg_N ("discrete type required for range", N);
18332 Set_Etype (N, Any_Type);
18333 return;
18334 end if;
18336 R := Range_Expression (Constraint (N));
18338 Resolve (R, T);
18339 Process_Range_Expr_In_Decl
18340 (R, Entity (Subtype_Mark (N)), In_Iter_Schm => In_Iter_Schm);
18342 elsif Nkind (N) = N_Attribute_Reference then
18344 -- Catch beginner's error (use of attribute other than 'Range)
18346 if Attribute_Name (N) /= Name_Range then
18347 Error_Msg_N ("expect attribute ''Range", N);
18348 Set_Etype (N, Any_Type);
18349 return;
18350 end if;
18352 -- If the node denotes the range of a type mark, that is also the
18353 -- resulting type, and we do not need to create an Itype for it.
18355 if Is_Entity_Name (Prefix (N))
18356 and then Comes_From_Source (N)
18357 and then Is_Type (Entity (Prefix (N)))
18358 and then Is_Discrete_Type (Entity (Prefix (N)))
18359 then
18360 Def_Id := Entity (Prefix (N));
18361 end if;
18363 Analyze_And_Resolve (N);
18364 T := Etype (N);
18365 R := N;
18367 -- If none of the above, must be a subtype. We convert this to a
18368 -- range attribute reference because in the case of declared first
18369 -- named subtypes, the types in the range reference can be different
18370 -- from the type of the entity. A range attribute normalizes the
18371 -- reference and obtains the correct types for the bounds.
18373 -- This transformation is in the nature of an expansion, is only
18374 -- done if expansion is active. In particular, it is not done on
18375 -- formal generic types, because we need to retain the name of the
18376 -- original index for instantiation purposes.
18378 else
18379 if not Is_Entity_Name (N) or else not Is_Type (Entity (N)) then
18380 Error_Msg_N ("invalid subtype mark in discrete range ", N);
18381 Set_Etype (N, Any_Integer);
18382 return;
18384 else
18385 -- The type mark may be that of an incomplete type. It is only
18386 -- now that we can get the full view, previous analysis does
18387 -- not look specifically for a type mark.
18389 Set_Entity (N, Get_Full_View (Entity (N)));
18390 Set_Etype (N, Entity (N));
18391 Def_Id := Entity (N);
18393 if not Is_Discrete_Type (Def_Id) then
18394 Error_Msg_N ("discrete type required for index", N);
18395 Set_Etype (N, Any_Type);
18396 return;
18397 end if;
18398 end if;
18400 if Expander_Active then
18401 Rewrite (N,
18402 Make_Attribute_Reference (Sloc (N),
18403 Attribute_Name => Name_Range,
18404 Prefix => Relocate_Node (N)));
18406 -- The original was a subtype mark that does not freeze. This
18407 -- means that the rewritten version must not freeze either.
18409 Set_Must_Not_Freeze (N);
18410 Set_Must_Not_Freeze (Prefix (N));
18411 Analyze_And_Resolve (N);
18412 T := Etype (N);
18413 R := N;
18415 -- If expander is inactive, type is legal, nothing else to construct
18417 else
18418 return;
18419 end if;
18420 end if;
18422 if not Is_Discrete_Type (T) then
18423 Error_Msg_N ("discrete type required for range", N);
18424 Set_Etype (N, Any_Type);
18425 return;
18427 elsif T = Any_Type then
18428 Set_Etype (N, Any_Type);
18429 return;
18430 end if;
18432 -- We will now create the appropriate Itype to describe the range, but
18433 -- first a check. If we originally had a subtype, then we just label
18434 -- the range with this subtype. Not only is there no need to construct
18435 -- a new subtype, but it is wrong to do so for two reasons:
18437 -- 1. A legality concern, if we have a subtype, it must not freeze,
18438 -- and the Itype would cause freezing incorrectly
18440 -- 2. An efficiency concern, if we created an Itype, it would not be
18441 -- recognized as the same type for the purposes of eliminating
18442 -- checks in some circumstances.
18444 -- We signal this case by setting the subtype entity in Def_Id
18446 if No (Def_Id) then
18447 Def_Id :=
18448 Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
18449 Set_Etype (Def_Id, Base_Type (T));
18451 if Is_Signed_Integer_Type (T) then
18452 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
18454 elsif Is_Modular_Integer_Type (T) then
18455 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
18457 else
18458 Set_Ekind (Def_Id, E_Enumeration_Subtype);
18459 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
18460 Set_First_Literal (Def_Id, First_Literal (T));
18461 end if;
18463 Set_Size_Info (Def_Id, (T));
18464 Set_RM_Size (Def_Id, RM_Size (T));
18465 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
18467 Set_Scalar_Range (Def_Id, R);
18468 Conditional_Delay (Def_Id, T);
18470 if Nkind (N) = N_Subtype_Indication then
18471 Inherit_Predicate_Flags (Def_Id, Entity (Subtype_Mark (N)));
18472 end if;
18474 -- In the subtype indication case, if the immediate parent of the
18475 -- new subtype is non-static, then the subtype we create is non-
18476 -- static, even if its bounds are static.
18478 if Nkind (N) = N_Subtype_Indication
18479 and then not Is_OK_Static_Subtype (Entity (Subtype_Mark (N)))
18480 then
18481 Set_Is_Non_Static_Subtype (Def_Id);
18482 end if;
18483 end if;
18485 -- Final step is to label the index with this constructed type
18487 Set_Etype (N, Def_Id);
18488 end Make_Index;
18490 ------------------------------
18491 -- Modular_Type_Declaration --
18492 ------------------------------
18494 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
18495 Mod_Expr : constant Node_Id := Expression (Def);
18496 M_Val : Uint;
18498 procedure Set_Modular_Size (Bits : Int);
18499 -- Sets RM_Size to Bits, and Esize to normal word size above this
18501 ----------------------
18502 -- Set_Modular_Size --
18503 ----------------------
18505 procedure Set_Modular_Size (Bits : Int) is
18506 begin
18507 Set_RM_Size (T, UI_From_Int (Bits));
18509 if Bits <= 8 then
18510 Init_Esize (T, 8);
18512 elsif Bits <= 16 then
18513 Init_Esize (T, 16);
18515 elsif Bits <= 32 then
18516 Init_Esize (T, 32);
18518 else
18519 Init_Esize (T, System_Max_Binary_Modulus_Power);
18520 end if;
18522 if not Non_Binary_Modulus (T) and then Esize (T) = RM_Size (T) then
18523 Set_Is_Known_Valid (T);
18524 end if;
18525 end Set_Modular_Size;
18527 -- Start of processing for Modular_Type_Declaration
18529 begin
18530 -- If the mod expression is (exactly) 2 * literal, where literal is
18531 -- 64 or less,then almost certainly the * was meant to be **. Warn.
18533 if Warn_On_Suspicious_Modulus_Value
18534 and then Nkind (Mod_Expr) = N_Op_Multiply
18535 and then Nkind (Left_Opnd (Mod_Expr)) = N_Integer_Literal
18536 and then Intval (Left_Opnd (Mod_Expr)) = Uint_2
18537 and then Nkind (Right_Opnd (Mod_Expr)) = N_Integer_Literal
18538 and then Intval (Right_Opnd (Mod_Expr)) <= Uint_64
18539 then
18540 Error_Msg_N
18541 ("suspicious MOD value, was '*'* intended'??M?", Mod_Expr);
18542 end if;
18544 -- Proceed with analysis of mod expression
18546 Analyze_And_Resolve (Mod_Expr, Any_Integer);
18547 Set_Etype (T, T);
18548 Set_Ekind (T, E_Modular_Integer_Type);
18549 Init_Alignment (T);
18550 Set_Is_Constrained (T);
18552 if not Is_OK_Static_Expression (Mod_Expr) then
18553 Flag_Non_Static_Expr
18554 ("non-static expression used for modular type bound!", Mod_Expr);
18555 M_Val := 2 ** System_Max_Binary_Modulus_Power;
18556 else
18557 M_Val := Expr_Value (Mod_Expr);
18558 end if;
18560 if M_Val < 1 then
18561 Error_Msg_N ("modulus value must be positive", Mod_Expr);
18562 M_Val := 2 ** System_Max_Binary_Modulus_Power;
18563 end if;
18565 if M_Val > 2 ** Standard_Long_Integer_Size then
18566 Check_Restriction (No_Long_Long_Integers, Mod_Expr);
18567 end if;
18569 Set_Modulus (T, M_Val);
18571 -- Create bounds for the modular type based on the modulus given in
18572 -- the type declaration and then analyze and resolve those bounds.
18574 Set_Scalar_Range (T,
18575 Make_Range (Sloc (Mod_Expr),
18576 Low_Bound => Make_Integer_Literal (Sloc (Mod_Expr), 0),
18577 High_Bound => Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
18579 -- Properly analyze the literals for the range. We do this manually
18580 -- because we can't go calling Resolve, since we are resolving these
18581 -- bounds with the type, and this type is certainly not complete yet.
18583 Set_Etype (Low_Bound (Scalar_Range (T)), T);
18584 Set_Etype (High_Bound (Scalar_Range (T)), T);
18585 Set_Is_Static_Expression (Low_Bound (Scalar_Range (T)));
18586 Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
18588 -- Loop through powers of two to find number of bits required
18590 for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
18592 -- Binary case
18594 if M_Val = 2 ** Bits then
18595 Set_Modular_Size (Bits);
18596 return;
18598 -- Nonbinary case
18600 elsif M_Val < 2 ** Bits then
18601 Check_SPARK_05_Restriction ("modulus should be a power of 2", T);
18602 Set_Non_Binary_Modulus (T);
18604 if Bits > System_Max_Nonbinary_Modulus_Power then
18605 Error_Msg_Uint_1 :=
18606 UI_From_Int (System_Max_Nonbinary_Modulus_Power);
18607 Error_Msg_F
18608 ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
18609 Set_Modular_Size (System_Max_Binary_Modulus_Power);
18610 return;
18612 else
18613 -- In the nonbinary case, set size as per RM 13.3(55)
18615 Set_Modular_Size (Bits);
18616 return;
18617 end if;
18618 end if;
18620 end loop;
18622 -- If we fall through, then the size exceed System.Max_Binary_Modulus
18623 -- so we just signal an error and set the maximum size.
18625 Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
18626 Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
18628 Set_Modular_Size (System_Max_Binary_Modulus_Power);
18629 Init_Alignment (T);
18631 end Modular_Type_Declaration;
18633 --------------------------
18634 -- New_Concatenation_Op --
18635 --------------------------
18637 procedure New_Concatenation_Op (Typ : Entity_Id) is
18638 Loc : constant Source_Ptr := Sloc (Typ);
18639 Op : Entity_Id;
18641 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
18642 -- Create abbreviated declaration for the formal of a predefined
18643 -- Operator 'Op' of type 'Typ'
18645 --------------------
18646 -- Make_Op_Formal --
18647 --------------------
18649 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
18650 Formal : Entity_Id;
18651 begin
18652 Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
18653 Set_Etype (Formal, Typ);
18654 Set_Mechanism (Formal, Default_Mechanism);
18655 return Formal;
18656 end Make_Op_Formal;
18658 -- Start of processing for New_Concatenation_Op
18660 begin
18661 Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
18663 Set_Ekind (Op, E_Operator);
18664 Set_Scope (Op, Current_Scope);
18665 Set_Etype (Op, Typ);
18666 Set_Homonym (Op, Get_Name_Entity_Id (Name_Op_Concat));
18667 Set_Is_Immediately_Visible (Op);
18668 Set_Is_Intrinsic_Subprogram (Op);
18669 Set_Has_Completion (Op);
18670 Append_Entity (Op, Current_Scope);
18672 Set_Name_Entity_Id (Name_Op_Concat, Op);
18674 Append_Entity (Make_Op_Formal (Typ, Op), Op);
18675 Append_Entity (Make_Op_Formal (Typ, Op), Op);
18676 end New_Concatenation_Op;
18678 -------------------------
18679 -- OK_For_Limited_Init --
18680 -------------------------
18682 -- ???Check all calls of this, and compare the conditions under which it's
18683 -- called.
18685 function OK_For_Limited_Init
18686 (Typ : Entity_Id;
18687 Exp : Node_Id) return Boolean
18689 begin
18690 return Is_CPP_Constructor_Call (Exp)
18691 or else (Ada_Version >= Ada_2005
18692 and then not Debug_Flag_Dot_L
18693 and then OK_For_Limited_Init_In_05 (Typ, Exp));
18694 end OK_For_Limited_Init;
18696 -------------------------------
18697 -- OK_For_Limited_Init_In_05 --
18698 -------------------------------
18700 function OK_For_Limited_Init_In_05
18701 (Typ : Entity_Id;
18702 Exp : Node_Id) return Boolean
18704 begin
18705 -- An object of a limited interface type can be initialized with any
18706 -- expression of a nonlimited descendant type.
18708 if Is_Class_Wide_Type (Typ)
18709 and then Is_Limited_Interface (Typ)
18710 and then not Is_Limited_Type (Etype (Exp))
18711 then
18712 return True;
18713 end if;
18715 -- Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
18716 -- case of limited aggregates (including extension aggregates), and
18717 -- function calls. The function call may have been given in prefixed
18718 -- notation, in which case the original node is an indexed component.
18719 -- If the function is parameterless, the original node was an explicit
18720 -- dereference. The function may also be parameterless, in which case
18721 -- the source node is just an identifier.
18723 case Nkind (Original_Node (Exp)) is
18724 when N_Aggregate | N_Extension_Aggregate | N_Function_Call | N_Op =>
18725 return True;
18727 when N_Identifier =>
18728 return Present (Entity (Original_Node (Exp)))
18729 and then Ekind (Entity (Original_Node (Exp))) = E_Function;
18731 when N_Qualified_Expression =>
18732 return
18733 OK_For_Limited_Init_In_05
18734 (Typ, Expression (Original_Node (Exp)));
18736 -- Ada 2005 (AI-251): If a class-wide interface object is initialized
18737 -- with a function call, the expander has rewritten the call into an
18738 -- N_Type_Conversion node to force displacement of the pointer to
18739 -- reference the component containing the secondary dispatch table.
18740 -- Otherwise a type conversion is not a legal context.
18741 -- A return statement for a build-in-place function returning a
18742 -- synchronized type also introduces an unchecked conversion.
18744 when N_Type_Conversion |
18745 N_Unchecked_Type_Conversion =>
18746 return not Comes_From_Source (Exp)
18747 and then
18748 OK_For_Limited_Init_In_05
18749 (Typ, Expression (Original_Node (Exp)));
18751 when N_Indexed_Component |
18752 N_Selected_Component |
18753 N_Explicit_Dereference =>
18754 return Nkind (Exp) = N_Function_Call;
18756 -- A use of 'Input is a function call, hence allowed. Normally the
18757 -- attribute will be changed to a call, but the attribute by itself
18758 -- can occur with -gnatc.
18760 when N_Attribute_Reference =>
18761 return Attribute_Name (Original_Node (Exp)) = Name_Input;
18763 -- For a case expression, all dependent expressions must be legal
18765 when N_Case_Expression =>
18766 declare
18767 Alt : Node_Id;
18769 begin
18770 Alt := First (Alternatives (Original_Node (Exp)));
18771 while Present (Alt) loop
18772 if not OK_For_Limited_Init_In_05 (Typ, Expression (Alt)) then
18773 return False;
18774 end if;
18776 Next (Alt);
18777 end loop;
18779 return True;
18780 end;
18782 -- For an if expression, all dependent expressions must be legal
18784 when N_If_Expression =>
18785 declare
18786 Then_Expr : constant Node_Id :=
18787 Next (First (Expressions (Original_Node (Exp))));
18788 Else_Expr : constant Node_Id := Next (Then_Expr);
18789 begin
18790 return OK_For_Limited_Init_In_05 (Typ, Then_Expr)
18791 and then
18792 OK_For_Limited_Init_In_05 (Typ, Else_Expr);
18793 end;
18795 when others =>
18796 return False;
18797 end case;
18798 end OK_For_Limited_Init_In_05;
18800 -------------------------------------------
18801 -- Ordinary_Fixed_Point_Type_Declaration --
18802 -------------------------------------------
18804 procedure Ordinary_Fixed_Point_Type_Declaration
18805 (T : Entity_Id;
18806 Def : Node_Id)
18808 Loc : constant Source_Ptr := Sloc (Def);
18809 Delta_Expr : constant Node_Id := Delta_Expression (Def);
18810 RRS : constant Node_Id := Real_Range_Specification (Def);
18811 Implicit_Base : Entity_Id;
18812 Delta_Val : Ureal;
18813 Small_Val : Ureal;
18814 Low_Val : Ureal;
18815 High_Val : Ureal;
18817 begin
18818 Check_Restriction (No_Fixed_Point, Def);
18820 -- Create implicit base type
18822 Implicit_Base :=
18823 Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
18824 Set_Etype (Implicit_Base, Implicit_Base);
18826 -- Analyze and process delta expression
18828 Analyze_And_Resolve (Delta_Expr, Any_Real);
18830 Check_Delta_Expression (Delta_Expr);
18831 Delta_Val := Expr_Value_R (Delta_Expr);
18833 Set_Delta_Value (Implicit_Base, Delta_Val);
18835 -- Compute default small from given delta, which is the largest power
18836 -- of two that does not exceed the given delta value.
18838 declare
18839 Tmp : Ureal;
18840 Scale : Int;
18842 begin
18843 Tmp := Ureal_1;
18844 Scale := 0;
18846 if Delta_Val < Ureal_1 then
18847 while Delta_Val < Tmp loop
18848 Tmp := Tmp / Ureal_2;
18849 Scale := Scale + 1;
18850 end loop;
18852 else
18853 loop
18854 Tmp := Tmp * Ureal_2;
18855 exit when Tmp > Delta_Val;
18856 Scale := Scale - 1;
18857 end loop;
18858 end if;
18860 Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
18861 end;
18863 Set_Small_Value (Implicit_Base, Small_Val);
18865 -- If no range was given, set a dummy range
18867 if RRS <= Empty_Or_Error then
18868 Low_Val := -Small_Val;
18869 High_Val := Small_Val;
18871 -- Otherwise analyze and process given range
18873 else
18874 declare
18875 Low : constant Node_Id := Low_Bound (RRS);
18876 High : constant Node_Id := High_Bound (RRS);
18878 begin
18879 Analyze_And_Resolve (Low, Any_Real);
18880 Analyze_And_Resolve (High, Any_Real);
18881 Check_Real_Bound (Low);
18882 Check_Real_Bound (High);
18884 -- Obtain and set the range
18886 Low_Val := Expr_Value_R (Low);
18887 High_Val := Expr_Value_R (High);
18889 if Low_Val > High_Val then
18890 Error_Msg_NE ("??fixed point type& has null range", Def, T);
18891 end if;
18892 end;
18893 end if;
18895 -- The range for both the implicit base and the declared first subtype
18896 -- cannot be set yet, so we use the special routine Set_Fixed_Range to
18897 -- set a temporary range in place. Note that the bounds of the base
18898 -- type will be widened to be symmetrical and to fill the available
18899 -- bits when the type is frozen.
18901 -- We could do this with all discrete types, and probably should, but
18902 -- we absolutely have to do it for fixed-point, since the end-points
18903 -- of the range and the size are determined by the small value, which
18904 -- could be reset before the freeze point.
18906 Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
18907 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
18909 -- Complete definition of first subtype. The inheritance of the rep item
18910 -- chain ensures that SPARK-related pragmas are not clobbered when the
18911 -- ordinary fixed point type acts as a full view of a private type.
18913 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
18914 Set_Etype (T, Implicit_Base);
18915 Init_Size_Align (T);
18916 Inherit_Rep_Item_Chain (T, Implicit_Base);
18917 Set_Small_Value (T, Small_Val);
18918 Set_Delta_Value (T, Delta_Val);
18919 Set_Is_Constrained (T);
18920 end Ordinary_Fixed_Point_Type_Declaration;
18922 ----------------------------------
18923 -- Preanalyze_Assert_Expression --
18924 ----------------------------------
18926 procedure Preanalyze_Assert_Expression (N : Node_Id; T : Entity_Id) is
18927 begin
18928 In_Assertion_Expr := In_Assertion_Expr + 1;
18929 Preanalyze_Spec_Expression (N, T);
18930 In_Assertion_Expr := In_Assertion_Expr - 1;
18931 end Preanalyze_Assert_Expression;
18933 -----------------------------------
18934 -- Preanalyze_Default_Expression --
18935 -----------------------------------
18937 procedure Preanalyze_Default_Expression (N : Node_Id; T : Entity_Id) is
18938 Save_In_Default_Expr : constant Boolean := In_Default_Expr;
18939 begin
18940 In_Default_Expr := True;
18941 Preanalyze_Spec_Expression (N, T);
18942 In_Default_Expr := Save_In_Default_Expr;
18943 end Preanalyze_Default_Expression;
18945 --------------------------------
18946 -- Preanalyze_Spec_Expression --
18947 --------------------------------
18949 procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
18950 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
18951 begin
18952 In_Spec_Expression := True;
18953 Preanalyze_And_Resolve (N, T);
18954 In_Spec_Expression := Save_In_Spec_Expression;
18955 end Preanalyze_Spec_Expression;
18957 ----------------------------------------
18958 -- Prepare_Private_Subtype_Completion --
18959 ----------------------------------------
18961 procedure Prepare_Private_Subtype_Completion
18962 (Id : Entity_Id;
18963 Related_Nod : Node_Id)
18965 Id_B : constant Entity_Id := Base_Type (Id);
18966 Full_B : Entity_Id := Full_View (Id_B);
18967 Full : Entity_Id;
18969 begin
18970 if Present (Full_B) then
18972 -- Get to the underlying full view if necessary
18974 if Is_Private_Type (Full_B)
18975 and then Present (Underlying_Full_View (Full_B))
18976 then
18977 Full_B := Underlying_Full_View (Full_B);
18978 end if;
18980 -- The Base_Type is already completed, we can complete the subtype
18981 -- now. We have to create a new entity with the same name, Thus we
18982 -- can't use Create_Itype.
18984 Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
18985 Set_Is_Itype (Full);
18986 Set_Associated_Node_For_Itype (Full, Related_Nod);
18987 Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
18988 end if;
18990 -- The parent subtype may be private, but the base might not, in some
18991 -- nested instances. In that case, the subtype does not need to be
18992 -- exchanged. It would still be nice to make private subtypes and their
18993 -- bases consistent at all times ???
18995 if Is_Private_Type (Id_B) then
18996 Append_Elmt (Id, Private_Dependents (Id_B));
18997 end if;
18998 end Prepare_Private_Subtype_Completion;
19000 ---------------------------
19001 -- Process_Discriminants --
19002 ---------------------------
19004 procedure Process_Discriminants
19005 (N : Node_Id;
19006 Prev : Entity_Id := Empty)
19008 Elist : constant Elist_Id := New_Elmt_List;
19009 Id : Node_Id;
19010 Discr : Node_Id;
19011 Discr_Number : Uint;
19012 Discr_Type : Entity_Id;
19013 Default_Present : Boolean := False;
19014 Default_Not_Present : Boolean := False;
19016 begin
19017 -- A composite type other than an array type can have discriminants.
19018 -- On entry, the current scope is the composite type.
19020 -- The discriminants are initially entered into the scope of the type
19021 -- via Enter_Name with the default Ekind of E_Void to prevent premature
19022 -- use, as explained at the end of this procedure.
19024 Discr := First (Discriminant_Specifications (N));
19025 while Present (Discr) loop
19026 Enter_Name (Defining_Identifier (Discr));
19028 -- For navigation purposes we add a reference to the discriminant
19029 -- in the entity for the type. If the current declaration is a
19030 -- completion, place references on the partial view. Otherwise the
19031 -- type is the current scope.
19033 if Present (Prev) then
19035 -- The references go on the partial view, if present. If the
19036 -- partial view has discriminants, the references have been
19037 -- generated already.
19039 if not Has_Discriminants (Prev) then
19040 Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
19041 end if;
19042 else
19043 Generate_Reference
19044 (Current_Scope, Defining_Identifier (Discr), 'd');
19045 end if;
19047 if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
19048 Discr_Type := Access_Definition (Discr, Discriminant_Type (Discr));
19050 -- Ada 2005 (AI-254)
19052 if Present (Access_To_Subprogram_Definition
19053 (Discriminant_Type (Discr)))
19054 and then Protected_Present (Access_To_Subprogram_Definition
19055 (Discriminant_Type (Discr)))
19056 then
19057 Discr_Type :=
19058 Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
19059 end if;
19061 else
19062 Find_Type (Discriminant_Type (Discr));
19063 Discr_Type := Etype (Discriminant_Type (Discr));
19065 if Error_Posted (Discriminant_Type (Discr)) then
19066 Discr_Type := Any_Type;
19067 end if;
19068 end if;
19070 -- Handling of discriminants that are access types
19072 if Is_Access_Type (Discr_Type) then
19074 -- Ada 2005 (AI-230): Access discriminant allowed in non-
19075 -- limited record types
19077 if Ada_Version < Ada_2005 then
19078 Check_Access_Discriminant_Requires_Limited
19079 (Discr, Discriminant_Type (Discr));
19080 end if;
19082 if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
19083 Error_Msg_N
19084 ("(Ada 83) access discriminant not allowed", Discr);
19085 end if;
19087 -- If not access type, must be a discrete type
19089 elsif not Is_Discrete_Type (Discr_Type) then
19090 Error_Msg_N
19091 ("discriminants must have a discrete or access type",
19092 Discriminant_Type (Discr));
19093 end if;
19095 Set_Etype (Defining_Identifier (Discr), Discr_Type);
19097 -- If a discriminant specification includes the assignment compound
19098 -- delimiter followed by an expression, the expression is the default
19099 -- expression of the discriminant; the default expression must be of
19100 -- the type of the discriminant. (RM 3.7.1) Since this expression is
19101 -- a default expression, we do the special preanalysis, since this
19102 -- expression does not freeze (see section "Handling of Default and
19103 -- Per-Object Expressions" in spec of package Sem).
19105 if Present (Expression (Discr)) then
19106 Preanalyze_Spec_Expression (Expression (Discr), Discr_Type);
19108 -- Legaity checks
19110 if Nkind (N) = N_Formal_Type_Declaration then
19111 Error_Msg_N
19112 ("discriminant defaults not allowed for formal type",
19113 Expression (Discr));
19115 -- Flag an error for a tagged type with defaulted discriminants,
19116 -- excluding limited tagged types when compiling for Ada 2012
19117 -- (see AI05-0214).
19119 elsif Is_Tagged_Type (Current_Scope)
19120 and then (not Is_Limited_Type (Current_Scope)
19121 or else Ada_Version < Ada_2012)
19122 and then Comes_From_Source (N)
19123 then
19124 -- Note: see similar test in Check_Or_Process_Discriminants, to
19125 -- handle the (illegal) case of the completion of an untagged
19126 -- view with discriminants with defaults by a tagged full view.
19127 -- We skip the check if Discr does not come from source, to
19128 -- account for the case of an untagged derived type providing
19129 -- defaults for a renamed discriminant from a private untagged
19130 -- ancestor with a tagged full view (ACATS B460006).
19132 if Ada_Version >= Ada_2012 then
19133 Error_Msg_N
19134 ("discriminants of nonlimited tagged type cannot have"
19135 & " defaults",
19136 Expression (Discr));
19137 else
19138 Error_Msg_N
19139 ("discriminants of tagged type cannot have defaults",
19140 Expression (Discr));
19141 end if;
19143 else
19144 Default_Present := True;
19145 Append_Elmt (Expression (Discr), Elist);
19147 -- Tag the defining identifiers for the discriminants with
19148 -- their corresponding default expressions from the tree.
19150 Set_Discriminant_Default_Value
19151 (Defining_Identifier (Discr), Expression (Discr));
19152 end if;
19154 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag
19155 -- gets set unless we can be sure that no range check is required.
19157 if (GNATprove_Mode or not Expander_Active)
19158 and then not
19159 Is_In_Range
19160 (Expression (Discr), Discr_Type, Assume_Valid => True)
19161 then
19162 Set_Do_Range_Check (Expression (Discr));
19163 end if;
19165 -- No default discriminant value given
19167 else
19168 Default_Not_Present := True;
19169 end if;
19171 -- Ada 2005 (AI-231): Create an Itype that is a duplicate of
19172 -- Discr_Type but with the null-exclusion attribute
19174 if Ada_Version >= Ada_2005 then
19176 -- Ada 2005 (AI-231): Static checks
19178 if Can_Never_Be_Null (Discr_Type) then
19179 Null_Exclusion_Static_Checks (Discr);
19181 elsif Is_Access_Type (Discr_Type)
19182 and then Null_Exclusion_Present (Discr)
19184 -- No need to check itypes because in their case this check
19185 -- was done at their point of creation
19187 and then not Is_Itype (Discr_Type)
19188 then
19189 if Can_Never_Be_Null (Discr_Type) then
19190 Error_Msg_NE
19191 ("`NOT NULL` not allowed (& already excludes null)",
19192 Discr,
19193 Discr_Type);
19194 end if;
19196 Set_Etype (Defining_Identifier (Discr),
19197 Create_Null_Excluding_Itype
19198 (T => Discr_Type,
19199 Related_Nod => Discr));
19201 -- Check for improper null exclusion if the type is otherwise
19202 -- legal for a discriminant.
19204 elsif Null_Exclusion_Present (Discr)
19205 and then Is_Discrete_Type (Discr_Type)
19206 then
19207 Error_Msg_N
19208 ("null exclusion can only apply to an access type", Discr);
19209 end if;
19211 -- Ada 2005 (AI-402): access discriminants of nonlimited types
19212 -- can't have defaults. Synchronized types, or types that are
19213 -- explicitly limited are fine, but special tests apply to derived
19214 -- types in generics: in a generic body we have to assume the
19215 -- worst, and therefore defaults are not allowed if the parent is
19216 -- a generic formal private type (see ACATS B370001).
19218 if Is_Access_Type (Discr_Type) and then Default_Present then
19219 if Ekind (Discr_Type) /= E_Anonymous_Access_Type
19220 or else Is_Limited_Record (Current_Scope)
19221 or else Is_Concurrent_Type (Current_Scope)
19222 or else Is_Concurrent_Record_Type (Current_Scope)
19223 or else Ekind (Current_Scope) = E_Limited_Private_Type
19224 then
19225 if not Is_Derived_Type (Current_Scope)
19226 or else not Is_Generic_Type (Etype (Current_Scope))
19227 or else not In_Package_Body (Scope (Etype (Current_Scope)))
19228 or else Limited_Present
19229 (Type_Definition (Parent (Current_Scope)))
19230 then
19231 null;
19233 else
19234 Error_Msg_N
19235 ("access discriminants of nonlimited types cannot "
19236 & "have defaults", Expression (Discr));
19237 end if;
19239 elsif Present (Expression (Discr)) then
19240 Error_Msg_N
19241 ("(Ada 2005) access discriminants of nonlimited types "
19242 & "cannot have defaults", Expression (Discr));
19243 end if;
19244 end if;
19245 end if;
19247 -- A discriminant cannot be effectively volatile. This check is only
19248 -- relevant when SPARK_Mode is on as it is not standard Ada legality
19249 -- rule (SPARK RM 7.1.3(6)).
19251 if SPARK_Mode = On
19252 and then Is_Effectively_Volatile (Defining_Identifier (Discr))
19253 then
19254 Error_Msg_N ("discriminant cannot be volatile", Discr);
19255 end if;
19257 Next (Discr);
19258 end loop;
19260 -- An element list consisting of the default expressions of the
19261 -- discriminants is constructed in the above loop and used to set
19262 -- the Discriminant_Constraint attribute for the type. If an object
19263 -- is declared of this (record or task) type without any explicit
19264 -- discriminant constraint given, this element list will form the
19265 -- actual parameters for the corresponding initialization procedure
19266 -- for the type.
19268 Set_Discriminant_Constraint (Current_Scope, Elist);
19269 Set_Stored_Constraint (Current_Scope, No_Elist);
19271 -- Default expressions must be provided either for all or for none
19272 -- of the discriminants of a discriminant part. (RM 3.7.1)
19274 if Default_Present and then Default_Not_Present then
19275 Error_Msg_N
19276 ("incomplete specification of defaults for discriminants", N);
19277 end if;
19279 -- The use of the name of a discriminant is not allowed in default
19280 -- expressions of a discriminant part if the specification of the
19281 -- discriminant is itself given in the discriminant part. (RM 3.7.1)
19283 -- To detect this, the discriminant names are entered initially with an
19284 -- Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
19285 -- attempt to use a void entity (for example in an expression that is
19286 -- type-checked) produces the error message: premature usage. Now after
19287 -- completing the semantic analysis of the discriminant part, we can set
19288 -- the Ekind of all the discriminants appropriately.
19290 Discr := First (Discriminant_Specifications (N));
19291 Discr_Number := Uint_1;
19292 while Present (Discr) loop
19293 Id := Defining_Identifier (Discr);
19294 Set_Ekind (Id, E_Discriminant);
19295 Init_Component_Location (Id);
19296 Init_Esize (Id);
19297 Set_Discriminant_Number (Id, Discr_Number);
19299 -- Make sure this is always set, even in illegal programs
19301 Set_Corresponding_Discriminant (Id, Empty);
19303 -- Initialize the Original_Record_Component to the entity itself.
19304 -- Inherit_Components will propagate the right value to
19305 -- discriminants in derived record types.
19307 Set_Original_Record_Component (Id, Id);
19309 -- Create the discriminal for the discriminant
19311 Build_Discriminal (Id);
19313 Next (Discr);
19314 Discr_Number := Discr_Number + 1;
19315 end loop;
19317 Set_Has_Discriminants (Current_Scope);
19318 end Process_Discriminants;
19320 -----------------------
19321 -- Process_Full_View --
19322 -----------------------
19324 procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
19325 procedure Collect_Implemented_Interfaces
19326 (Typ : Entity_Id;
19327 Ifaces : Elist_Id);
19328 -- Ada 2005: Gather all the interfaces that Typ directly or
19329 -- inherently implements. Duplicate entries are not added to
19330 -- the list Ifaces.
19332 ------------------------------------
19333 -- Collect_Implemented_Interfaces --
19334 ------------------------------------
19336 procedure Collect_Implemented_Interfaces
19337 (Typ : Entity_Id;
19338 Ifaces : Elist_Id)
19340 Iface : Entity_Id;
19341 Iface_Elmt : Elmt_Id;
19343 begin
19344 -- Abstract interfaces are only associated with tagged record types
19346 if not Is_Tagged_Type (Typ) or else not Is_Record_Type (Typ) then
19347 return;
19348 end if;
19350 -- Recursively climb to the ancestors
19352 if Etype (Typ) /= Typ
19354 -- Protect the frontend against wrong cyclic declarations like:
19356 -- type B is new A with private;
19357 -- type C is new A with private;
19358 -- private
19359 -- type B is new C with null record;
19360 -- type C is new B with null record;
19362 and then Etype (Typ) /= Priv_T
19363 and then Etype (Typ) /= Full_T
19364 then
19365 -- Keep separate the management of private type declarations
19367 if Ekind (Typ) = E_Record_Type_With_Private then
19369 -- Handle the following illegal usage:
19370 -- type Private_Type is tagged private;
19371 -- private
19372 -- type Private_Type is new Type_Implementing_Iface;
19374 if Present (Full_View (Typ))
19375 and then Etype (Typ) /= Full_View (Typ)
19376 then
19377 if Is_Interface (Etype (Typ)) then
19378 Append_Unique_Elmt (Etype (Typ), Ifaces);
19379 end if;
19381 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
19382 end if;
19384 -- Non-private types
19386 else
19387 if Is_Interface (Etype (Typ)) then
19388 Append_Unique_Elmt (Etype (Typ), Ifaces);
19389 end if;
19391 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
19392 end if;
19393 end if;
19395 -- Handle entities in the list of abstract interfaces
19397 if Present (Interfaces (Typ)) then
19398 Iface_Elmt := First_Elmt (Interfaces (Typ));
19399 while Present (Iface_Elmt) loop
19400 Iface := Node (Iface_Elmt);
19402 pragma Assert (Is_Interface (Iface));
19404 if not Contain_Interface (Iface, Ifaces) then
19405 Append_Elmt (Iface, Ifaces);
19406 Collect_Implemented_Interfaces (Iface, Ifaces);
19407 end if;
19409 Next_Elmt (Iface_Elmt);
19410 end loop;
19411 end if;
19412 end Collect_Implemented_Interfaces;
19414 -- Local variables
19416 Full_Indic : Node_Id;
19417 Full_Parent : Entity_Id;
19418 Priv_Parent : Entity_Id;
19420 -- Start of processing for Process_Full_View
19422 begin
19423 -- First some sanity checks that must be done after semantic
19424 -- decoration of the full view and thus cannot be placed with other
19425 -- similar checks in Find_Type_Name
19427 if not Is_Limited_Type (Priv_T)
19428 and then (Is_Limited_Type (Full_T)
19429 or else Is_Limited_Composite (Full_T))
19430 then
19431 if In_Instance then
19432 null;
19433 else
19434 Error_Msg_N
19435 ("completion of nonlimited type cannot be limited", Full_T);
19436 Explain_Limited_Type (Full_T, Full_T);
19437 end if;
19439 elsif Is_Abstract_Type (Full_T)
19440 and then not Is_Abstract_Type (Priv_T)
19441 then
19442 Error_Msg_N
19443 ("completion of nonabstract type cannot be abstract", Full_T);
19445 elsif Is_Tagged_Type (Priv_T)
19446 and then Is_Limited_Type (Priv_T)
19447 and then not Is_Limited_Type (Full_T)
19448 then
19449 -- If pragma CPP_Class was applied to the private declaration
19450 -- propagate the limitedness to the full-view
19452 if Is_CPP_Class (Priv_T) then
19453 Set_Is_Limited_Record (Full_T);
19455 -- GNAT allow its own definition of Limited_Controlled to disobey
19456 -- this rule in order in ease the implementation. This test is safe
19457 -- because Root_Controlled is defined in a child of System that
19458 -- normal programs are not supposed to use.
19460 elsif Is_RTE (Etype (Full_T), RE_Root_Controlled) then
19461 Set_Is_Limited_Composite (Full_T);
19462 else
19463 Error_Msg_N
19464 ("completion of limited tagged type must be limited", Full_T);
19465 end if;
19467 elsif Is_Generic_Type (Priv_T) then
19468 Error_Msg_N ("generic type cannot have a completion", Full_T);
19469 end if;
19471 -- Check that ancestor interfaces of private and full views are
19472 -- consistent. We omit this check for synchronized types because
19473 -- they are performed on the corresponding record type when frozen.
19475 if Ada_Version >= Ada_2005
19476 and then Is_Tagged_Type (Priv_T)
19477 and then Is_Tagged_Type (Full_T)
19478 and then not Is_Concurrent_Type (Full_T)
19479 then
19480 declare
19481 Iface : Entity_Id;
19482 Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
19483 Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
19485 begin
19486 Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
19487 Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
19489 -- Ada 2005 (AI-251): The partial view shall be a descendant of
19490 -- an interface type if and only if the full type is descendant
19491 -- of the interface type (AARM 7.3 (7.3/2)).
19493 Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
19495 if Present (Iface) then
19496 Error_Msg_NE
19497 ("interface in partial view& not implemented by full type "
19498 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
19499 end if;
19501 Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
19503 if Present (Iface) then
19504 Error_Msg_NE
19505 ("interface & not implemented by partial view "
19506 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
19507 end if;
19508 end;
19509 end if;
19511 if Is_Tagged_Type (Priv_T)
19512 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
19513 and then Is_Derived_Type (Full_T)
19514 then
19515 Priv_Parent := Etype (Priv_T);
19517 -- The full view of a private extension may have been transformed
19518 -- into an unconstrained derived type declaration and a subtype
19519 -- declaration (see build_derived_record_type for details).
19521 if Nkind (N) = N_Subtype_Declaration then
19522 Full_Indic := Subtype_Indication (N);
19523 Full_Parent := Etype (Base_Type (Full_T));
19524 else
19525 Full_Indic := Subtype_Indication (Type_Definition (N));
19526 Full_Parent := Etype (Full_T);
19527 end if;
19529 -- Check that the parent type of the full type is a descendant of
19530 -- the ancestor subtype given in the private extension. If either
19531 -- entity has an Etype equal to Any_Type then we had some previous
19532 -- error situation [7.3(8)].
19534 if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
19535 return;
19537 -- Ada 2005 (AI-251): Interfaces in the full type can be given in
19538 -- any order. Therefore we don't have to check that its parent must
19539 -- be a descendant of the parent of the private type declaration.
19541 elsif Is_Interface (Priv_Parent)
19542 and then Is_Interface (Full_Parent)
19543 then
19544 null;
19546 -- Ada 2005 (AI-251): If the parent of the private type declaration
19547 -- is an interface there is no need to check that it is an ancestor
19548 -- of the associated full type declaration. The required tests for
19549 -- this case are performed by Build_Derived_Record_Type.
19551 elsif not Is_Interface (Base_Type (Priv_Parent))
19552 and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
19553 then
19554 Error_Msg_N
19555 ("parent of full type must descend from parent"
19556 & " of private extension", Full_Indic);
19558 -- First check a formal restriction, and then proceed with checking
19559 -- Ada rules. Since the formal restriction is not a serious error, we
19560 -- don't prevent further error detection for this check, hence the
19561 -- ELSE.
19563 else
19564 -- In formal mode, when completing a private extension the type
19565 -- named in the private part must be exactly the same as that
19566 -- named in the visible part.
19568 if Priv_Parent /= Full_Parent then
19569 Error_Msg_Name_1 := Chars (Priv_Parent);
19570 Check_SPARK_05_Restriction ("% expected", Full_Indic);
19571 end if;
19573 -- Check the rules of 7.3(10): if the private extension inherits
19574 -- known discriminants, then the full type must also inherit those
19575 -- discriminants from the same (ancestor) type, and the parent
19576 -- subtype of the full type must be constrained if and only if
19577 -- the ancestor subtype of the private extension is constrained.
19579 if No (Discriminant_Specifications (Parent (Priv_T)))
19580 and then not Has_Unknown_Discriminants (Priv_T)
19581 and then Has_Discriminants (Base_Type (Priv_Parent))
19582 then
19583 declare
19584 Priv_Indic : constant Node_Id :=
19585 Subtype_Indication (Parent (Priv_T));
19587 Priv_Constr : constant Boolean :=
19588 Is_Constrained (Priv_Parent)
19589 or else
19590 Nkind (Priv_Indic) = N_Subtype_Indication
19591 or else
19592 Is_Constrained (Entity (Priv_Indic));
19594 Full_Constr : constant Boolean :=
19595 Is_Constrained (Full_Parent)
19596 or else
19597 Nkind (Full_Indic) = N_Subtype_Indication
19598 or else
19599 Is_Constrained (Entity (Full_Indic));
19601 Priv_Discr : Entity_Id;
19602 Full_Discr : Entity_Id;
19604 begin
19605 Priv_Discr := First_Discriminant (Priv_Parent);
19606 Full_Discr := First_Discriminant (Full_Parent);
19607 while Present (Priv_Discr) and then Present (Full_Discr) loop
19608 if Original_Record_Component (Priv_Discr) =
19609 Original_Record_Component (Full_Discr)
19610 or else
19611 Corresponding_Discriminant (Priv_Discr) =
19612 Corresponding_Discriminant (Full_Discr)
19613 then
19614 null;
19615 else
19616 exit;
19617 end if;
19619 Next_Discriminant (Priv_Discr);
19620 Next_Discriminant (Full_Discr);
19621 end loop;
19623 if Present (Priv_Discr) or else Present (Full_Discr) then
19624 Error_Msg_N
19625 ("full view must inherit discriminants of the parent"
19626 & " type used in the private extension", Full_Indic);
19628 elsif Priv_Constr and then not Full_Constr then
19629 Error_Msg_N
19630 ("parent subtype of full type must be constrained",
19631 Full_Indic);
19633 elsif Full_Constr and then not Priv_Constr then
19634 Error_Msg_N
19635 ("parent subtype of full type must be unconstrained",
19636 Full_Indic);
19637 end if;
19638 end;
19640 -- Check the rules of 7.3(12): if a partial view has neither
19641 -- known or unknown discriminants, then the full type
19642 -- declaration shall define a definite subtype.
19644 elsif not Has_Unknown_Discriminants (Priv_T)
19645 and then not Has_Discriminants (Priv_T)
19646 and then not Is_Constrained (Full_T)
19647 then
19648 Error_Msg_N
19649 ("full view must define a constrained type if partial view"
19650 & " has no discriminants", Full_T);
19651 end if;
19653 -- ??????? Do we implement the following properly ?????
19654 -- If the ancestor subtype of a private extension has constrained
19655 -- discriminants, then the parent subtype of the full view shall
19656 -- impose a statically matching constraint on those discriminants
19657 -- [7.3(13)].
19658 end if;
19660 else
19661 -- For untagged types, verify that a type without discriminants is
19662 -- not completed with an unconstrained type. A separate error message
19663 -- is produced if the full type has defaulted discriminants.
19665 if Is_Definite_Subtype (Priv_T)
19666 and then not Is_Definite_Subtype (Full_T)
19667 then
19668 Error_Msg_Sloc := Sloc (Parent (Priv_T));
19669 Error_Msg_NE
19670 ("full view of& not compatible with declaration#",
19671 Full_T, Priv_T);
19673 if not Is_Tagged_Type (Full_T) then
19674 Error_Msg_N
19675 ("\one is constrained, the other unconstrained", Full_T);
19676 end if;
19677 end if;
19678 end if;
19680 -- AI-419: verify that the use of "limited" is consistent
19682 declare
19683 Orig_Decl : constant Node_Id := Original_Node (N);
19685 begin
19686 if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
19687 and then Nkind (Orig_Decl) = N_Full_Type_Declaration
19688 and then Nkind
19689 (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
19690 then
19691 if not Limited_Present (Parent (Priv_T))
19692 and then not Synchronized_Present (Parent (Priv_T))
19693 and then Limited_Present (Type_Definition (Orig_Decl))
19694 then
19695 Error_Msg_N
19696 ("full view of non-limited extension cannot be limited", N);
19698 -- Conversely, if the partial view carries the limited keyword,
19699 -- the full view must as well, even if it may be redundant.
19701 elsif Limited_Present (Parent (Priv_T))
19702 and then not Limited_Present (Type_Definition (Orig_Decl))
19703 then
19704 Error_Msg_N
19705 ("full view of limited extension must be explicitly limited",
19707 end if;
19708 end if;
19709 end;
19711 -- Ada 2005 (AI-443): A synchronized private extension must be
19712 -- completed by a task or protected type.
19714 if Ada_Version >= Ada_2005
19715 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
19716 and then Synchronized_Present (Parent (Priv_T))
19717 and then not Is_Concurrent_Type (Full_T)
19718 then
19719 Error_Msg_N ("full view of synchronized extension must " &
19720 "be synchronized type", N);
19721 end if;
19723 -- Ada 2005 AI-363: if the full view has discriminants with
19724 -- defaults, it is illegal to declare constrained access subtypes
19725 -- whose designated type is the current type. This allows objects
19726 -- of the type that are declared in the heap to be unconstrained.
19728 if not Has_Unknown_Discriminants (Priv_T)
19729 and then not Has_Discriminants (Priv_T)
19730 and then Has_Discriminants (Full_T)
19731 and then
19732 Present (Discriminant_Default_Value (First_Discriminant (Full_T)))
19733 then
19734 Set_Has_Constrained_Partial_View (Full_T);
19735 Set_Has_Constrained_Partial_View (Priv_T);
19736 end if;
19738 -- Create a full declaration for all its subtypes recorded in
19739 -- Private_Dependents and swap them similarly to the base type. These
19740 -- are subtypes that have been define before the full declaration of
19741 -- the private type. We also swap the entry in Private_Dependents list
19742 -- so we can properly restore the private view on exit from the scope.
19744 declare
19745 Priv_Elmt : Elmt_Id;
19746 Priv_Scop : Entity_Id;
19747 Priv : Entity_Id;
19748 Full : Entity_Id;
19750 begin
19751 Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
19752 while Present (Priv_Elmt) loop
19753 Priv := Node (Priv_Elmt);
19754 Priv_Scop := Scope (Priv);
19756 if Ekind_In (Priv, E_Private_Subtype,
19757 E_Limited_Private_Subtype,
19758 E_Record_Subtype_With_Private)
19759 then
19760 Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
19761 Set_Is_Itype (Full);
19762 Set_Parent (Full, Parent (Priv));
19763 Set_Associated_Node_For_Itype (Full, N);
19765 -- Now we need to complete the private subtype, but since the
19766 -- base type has already been swapped, we must also swap the
19767 -- subtypes (and thus, reverse the arguments in the call to
19768 -- Complete_Private_Subtype). Also note that we may need to
19769 -- re-establish the scope of the private subtype.
19771 Copy_And_Swap (Priv, Full);
19773 if not In_Open_Scopes (Priv_Scop) then
19774 Push_Scope (Priv_Scop);
19776 else
19777 -- Reset Priv_Scop to Empty to indicate no scope was pushed
19779 Priv_Scop := Empty;
19780 end if;
19782 Complete_Private_Subtype (Full, Priv, Full_T, N);
19784 if Present (Priv_Scop) then
19785 Pop_Scope;
19786 end if;
19788 Replace_Elmt (Priv_Elmt, Full);
19789 end if;
19791 Next_Elmt (Priv_Elmt);
19792 end loop;
19793 end;
19795 -- If the private view was tagged, copy the new primitive operations
19796 -- from the private view to the full view.
19798 if Is_Tagged_Type (Full_T) then
19799 declare
19800 Disp_Typ : Entity_Id;
19801 Full_List : Elist_Id;
19802 Prim : Entity_Id;
19803 Prim_Elmt : Elmt_Id;
19804 Priv_List : Elist_Id;
19806 function Contains
19807 (E : Entity_Id;
19808 L : Elist_Id) return Boolean;
19809 -- Determine whether list L contains element E
19811 --------------
19812 -- Contains --
19813 --------------
19815 function Contains
19816 (E : Entity_Id;
19817 L : Elist_Id) return Boolean
19819 List_Elmt : Elmt_Id;
19821 begin
19822 List_Elmt := First_Elmt (L);
19823 while Present (List_Elmt) loop
19824 if Node (List_Elmt) = E then
19825 return True;
19826 end if;
19828 Next_Elmt (List_Elmt);
19829 end loop;
19831 return False;
19832 end Contains;
19834 -- Start of processing
19836 begin
19837 if Is_Tagged_Type (Priv_T) then
19838 Priv_List := Primitive_Operations (Priv_T);
19839 Prim_Elmt := First_Elmt (Priv_List);
19841 -- In the case of a concurrent type completing a private tagged
19842 -- type, primitives may have been declared in between the two
19843 -- views. These subprograms need to be wrapped the same way
19844 -- entries and protected procedures are handled because they
19845 -- cannot be directly shared by the two views.
19847 if Is_Concurrent_Type (Full_T) then
19848 declare
19849 Conc_Typ : constant Entity_Id :=
19850 Corresponding_Record_Type (Full_T);
19851 Curr_Nod : Node_Id := Parent (Conc_Typ);
19852 Wrap_Spec : Node_Id;
19854 begin
19855 while Present (Prim_Elmt) loop
19856 Prim := Node (Prim_Elmt);
19858 if Comes_From_Source (Prim)
19859 and then not Is_Abstract_Subprogram (Prim)
19860 then
19861 Wrap_Spec :=
19862 Make_Subprogram_Declaration (Sloc (Prim),
19863 Specification =>
19864 Build_Wrapper_Spec
19865 (Subp_Id => Prim,
19866 Obj_Typ => Conc_Typ,
19867 Formals =>
19868 Parameter_Specifications (
19869 Parent (Prim))));
19871 Insert_After (Curr_Nod, Wrap_Spec);
19872 Curr_Nod := Wrap_Spec;
19874 Analyze (Wrap_Spec);
19875 end if;
19877 Next_Elmt (Prim_Elmt);
19878 end loop;
19880 return;
19881 end;
19883 -- For non-concurrent types, transfer explicit primitives, but
19884 -- omit those inherited from the parent of the private view
19885 -- since they will be re-inherited later on.
19887 else
19888 Full_List := Primitive_Operations (Full_T);
19890 while Present (Prim_Elmt) loop
19891 Prim := Node (Prim_Elmt);
19893 if Comes_From_Source (Prim)
19894 and then not Contains (Prim, Full_List)
19895 then
19896 Append_Elmt (Prim, Full_List);
19897 end if;
19899 Next_Elmt (Prim_Elmt);
19900 end loop;
19901 end if;
19903 -- Untagged private view
19905 else
19906 Full_List := Primitive_Operations (Full_T);
19908 -- In this case the partial view is untagged, so here we locate
19909 -- all of the earlier primitives that need to be treated as
19910 -- dispatching (those that appear between the two views). Note
19911 -- that these additional operations must all be new operations
19912 -- (any earlier operations that override inherited operations
19913 -- of the full view will already have been inserted in the
19914 -- primitives list, marked by Check_Operation_From_Private_View
19915 -- as dispatching. Note that implicit "/=" operators are
19916 -- excluded from being added to the primitives list since they
19917 -- shouldn't be treated as dispatching (tagged "/=" is handled
19918 -- specially).
19920 Prim := Next_Entity (Full_T);
19921 while Present (Prim) and then Prim /= Priv_T loop
19922 if Ekind_In (Prim, E_Procedure, E_Function) then
19923 Disp_Typ := Find_Dispatching_Type (Prim);
19925 if Disp_Typ = Full_T
19926 and then (Chars (Prim) /= Name_Op_Ne
19927 or else Comes_From_Source (Prim))
19928 then
19929 Check_Controlling_Formals (Full_T, Prim);
19931 if not Is_Dispatching_Operation (Prim) then
19932 Append_Elmt (Prim, Full_List);
19933 Set_Is_Dispatching_Operation (Prim, True);
19934 Set_DT_Position_Value (Prim, No_Uint);
19935 end if;
19937 elsif Is_Dispatching_Operation (Prim)
19938 and then Disp_Typ /= Full_T
19939 then
19941 -- Verify that it is not otherwise controlled by a
19942 -- formal or a return value of type T.
19944 Check_Controlling_Formals (Disp_Typ, Prim);
19945 end if;
19946 end if;
19948 Next_Entity (Prim);
19949 end loop;
19950 end if;
19952 -- For the tagged case, the two views can share the same primitive
19953 -- operations list and the same class-wide type. Update attributes
19954 -- of the class-wide type which depend on the full declaration.
19956 if Is_Tagged_Type (Priv_T) then
19957 Set_Direct_Primitive_Operations (Priv_T, Full_List);
19958 Set_Class_Wide_Type
19959 (Base_Type (Full_T), Class_Wide_Type (Priv_T));
19961 Set_Has_Task (Class_Wide_Type (Priv_T), Has_Task (Full_T));
19962 Set_Has_Protected
19963 (Class_Wide_Type (Priv_T), Has_Protected (Full_T));
19964 end if;
19965 end;
19966 end if;
19968 -- Ada 2005 AI 161: Check preelaborable initialization consistency
19970 if Known_To_Have_Preelab_Init (Priv_T) then
19972 -- Case where there is a pragma Preelaborable_Initialization. We
19973 -- always allow this in predefined units, which is cheating a bit,
19974 -- but it means we don't have to struggle to meet the requirements in
19975 -- the RM for having Preelaborable Initialization. Otherwise we
19976 -- require that the type meets the RM rules. But we can't check that
19977 -- yet, because of the rule about overriding Initialize, so we simply
19978 -- set a flag that will be checked at freeze time.
19980 if not In_Predefined_Unit (Full_T) then
19981 Set_Must_Have_Preelab_Init (Full_T);
19982 end if;
19983 end if;
19985 -- If pragma CPP_Class was applied to the private type declaration,
19986 -- propagate it now to the full type declaration.
19988 if Is_CPP_Class (Priv_T) then
19989 Set_Is_CPP_Class (Full_T);
19990 Set_Convention (Full_T, Convention_CPP);
19992 -- Check that components of imported CPP types do not have default
19993 -- expressions.
19995 Check_CPP_Type_Has_No_Defaults (Full_T);
19996 end if;
19998 -- If the private view has user specified stream attributes, then so has
19999 -- the full view.
20001 -- Why the test, how could these flags be already set in Full_T ???
20003 if Has_Specified_Stream_Read (Priv_T) then
20004 Set_Has_Specified_Stream_Read (Full_T);
20005 end if;
20007 if Has_Specified_Stream_Write (Priv_T) then
20008 Set_Has_Specified_Stream_Write (Full_T);
20009 end if;
20011 if Has_Specified_Stream_Input (Priv_T) then
20012 Set_Has_Specified_Stream_Input (Full_T);
20013 end if;
20015 if Has_Specified_Stream_Output (Priv_T) then
20016 Set_Has_Specified_Stream_Output (Full_T);
20017 end if;
20019 -- Propagate the attributes related to pragma Default_Initial_Condition
20020 -- from the private to the full view. Note that both flags are mutually
20021 -- exclusive.
20023 if Has_Default_Init_Cond (Priv_T)
20024 or else Has_Inherited_Default_Init_Cond (Priv_T)
20025 then
20026 Propagate_Default_Init_Cond_Attributes
20027 (From_Typ => Priv_T,
20028 To_Typ => Full_T,
20029 Private_To_Full_View => True);
20031 -- In the case where the full view is derived from another private type,
20032 -- the attributes related to pragma Default_Initial_Condition must be
20033 -- propagated from the full to the private view to maintain consistency
20034 -- of views.
20036 -- package Pack is
20037 -- type Parent_Typ is private
20038 -- with Default_Initial_Condition ...;
20039 -- private
20040 -- type Parent_Typ is ...;
20041 -- end Pack;
20043 -- with Pack; use Pack;
20044 -- package Pack_2 is
20045 -- type Deriv_Typ is private; -- must inherit
20046 -- private
20047 -- type Deriv_Typ is new Parent_Typ; -- must inherit
20048 -- end Pack_2;
20050 elsif Has_Default_Init_Cond (Full_T)
20051 or else Has_Inherited_Default_Init_Cond (Full_T)
20052 then
20053 Propagate_Default_Init_Cond_Attributes
20054 (From_Typ => Full_T,
20055 To_Typ => Priv_T,
20056 Private_To_Full_View => True);
20057 end if;
20059 if Is_Ghost_Entity (Priv_T) then
20061 -- The Ghost policy in effect at the point of declaration and at the
20062 -- point of completion must match (SPARK RM 6.9(14)).
20064 Check_Ghost_Completion (Priv_T, Full_T);
20066 -- In the case where the private view of a tagged type lacks a parent
20067 -- type and is subject to pragma Ghost, ensure that the parent type
20068 -- specified by the full view is also Ghost (SPARK RM 6.9(9)).
20070 if Is_Derived_Type (Full_T) then
20071 Check_Ghost_Derivation (Full_T);
20072 end if;
20074 -- Propagate the attributes related to pragma Ghost from the private
20075 -- to the full view.
20077 Mark_Full_View_As_Ghost (Priv_T, Full_T);
20078 end if;
20080 -- Propagate invariants to full type
20082 if Has_Invariants (Priv_T) then
20083 Set_Has_Invariants (Full_T);
20084 Set_Invariant_Procedure (Full_T, Invariant_Procedure (Priv_T));
20085 end if;
20087 if Has_Inheritable_Invariants (Priv_T) then
20088 Set_Has_Inheritable_Invariants (Full_T);
20089 end if;
20091 -- Check hidden inheritance of class-wide type invariants
20093 if Ada_Version >= Ada_2012
20094 and then not Has_Inheritable_Invariants (Full_T)
20095 and then In_Private_Part (Current_Scope)
20096 and then Has_Interfaces (Full_T)
20097 then
20098 declare
20099 Ifaces : Elist_Id;
20100 AI : Elmt_Id;
20102 begin
20103 Collect_Interfaces (Full_T, Ifaces, Exclude_Parents => True);
20105 AI := First_Elmt (Ifaces);
20106 while Present (AI) loop
20107 if Has_Inheritable_Invariants (Node (AI)) then
20108 Error_Msg_N
20109 ("hidden inheritance of class-wide type invariants " &
20110 "not allowed", N);
20111 exit;
20112 end if;
20114 Next_Elmt (AI);
20115 end loop;
20116 end;
20117 end if;
20119 -- Propagate predicates to full type, and predicate function if already
20120 -- defined. It is not clear that this can actually happen? the partial
20121 -- view cannot be frozen yet, and the predicate function has not been
20122 -- built. Still it is a cheap check and seems safer to make it.
20124 if Has_Predicates (Priv_T) then
20125 if Present (Predicate_Function (Priv_T)) then
20126 Set_Predicate_Function (Full_T, Predicate_Function (Priv_T));
20127 end if;
20129 Set_Has_Predicates (Full_T);
20130 end if;
20131 end Process_Full_View;
20133 -----------------------------------
20134 -- Process_Incomplete_Dependents --
20135 -----------------------------------
20137 procedure Process_Incomplete_Dependents
20138 (N : Node_Id;
20139 Full_T : Entity_Id;
20140 Inc_T : Entity_Id)
20142 Inc_Elmt : Elmt_Id;
20143 Priv_Dep : Entity_Id;
20144 New_Subt : Entity_Id;
20146 Disc_Constraint : Elist_Id;
20148 begin
20149 if No (Private_Dependents (Inc_T)) then
20150 return;
20151 end if;
20153 -- Itypes that may be generated by the completion of an incomplete
20154 -- subtype are not used by the back-end and not attached to the tree.
20155 -- They are created only for constraint-checking purposes.
20157 Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
20158 while Present (Inc_Elmt) loop
20159 Priv_Dep := Node (Inc_Elmt);
20161 if Ekind (Priv_Dep) = E_Subprogram_Type then
20163 -- An Access_To_Subprogram type may have a return type or a
20164 -- parameter type that is incomplete. Replace with the full view.
20166 if Etype (Priv_Dep) = Inc_T then
20167 Set_Etype (Priv_Dep, Full_T);
20168 end if;
20170 declare
20171 Formal : Entity_Id;
20173 begin
20174 Formal := First_Formal (Priv_Dep);
20175 while Present (Formal) loop
20176 if Etype (Formal) = Inc_T then
20177 Set_Etype (Formal, Full_T);
20178 end if;
20180 Next_Formal (Formal);
20181 end loop;
20182 end;
20184 elsif Is_Overloadable (Priv_Dep) then
20186 -- If a subprogram in the incomplete dependents list is primitive
20187 -- for a tagged full type then mark it as a dispatching operation,
20188 -- check whether it overrides an inherited subprogram, and check
20189 -- restrictions on its controlling formals. Note that a protected
20190 -- operation is never dispatching: only its wrapper operation
20191 -- (which has convention Ada) is.
20193 if Is_Tagged_Type (Full_T)
20194 and then Is_Primitive (Priv_Dep)
20195 and then Convention (Priv_Dep) /= Convention_Protected
20196 then
20197 Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
20198 Set_Is_Dispatching_Operation (Priv_Dep);
20199 Check_Controlling_Formals (Full_T, Priv_Dep);
20200 end if;
20202 elsif Ekind (Priv_Dep) = E_Subprogram_Body then
20204 -- Can happen during processing of a body before the completion
20205 -- of a TA type. Ignore, because spec is also on dependent list.
20207 return;
20209 -- Ada 2005 (AI-412): Transform a regular incomplete subtype into a
20210 -- corresponding subtype of the full view.
20212 elsif Ekind (Priv_Dep) = E_Incomplete_Subtype then
20213 Set_Subtype_Indication
20214 (Parent (Priv_Dep), New_Occurrence_Of (Full_T, Sloc (Priv_Dep)));
20215 Set_Etype (Priv_Dep, Full_T);
20216 Set_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
20217 Set_Analyzed (Parent (Priv_Dep), False);
20219 -- Reanalyze the declaration, suppressing the call to
20220 -- Enter_Name to avoid duplicate names.
20222 Analyze_Subtype_Declaration
20223 (N => Parent (Priv_Dep),
20224 Skip => True);
20226 -- Dependent is a subtype
20228 else
20229 -- We build a new subtype indication using the full view of the
20230 -- incomplete parent. The discriminant constraints have been
20231 -- elaborated already at the point of the subtype declaration.
20233 New_Subt := Create_Itype (E_Void, N);
20235 if Has_Discriminants (Full_T) then
20236 Disc_Constraint := Discriminant_Constraint (Priv_Dep);
20237 else
20238 Disc_Constraint := No_Elist;
20239 end if;
20241 Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
20242 Set_Full_View (Priv_Dep, New_Subt);
20243 end if;
20245 Next_Elmt (Inc_Elmt);
20246 end loop;
20247 end Process_Incomplete_Dependents;
20249 --------------------------------
20250 -- Process_Range_Expr_In_Decl --
20251 --------------------------------
20253 procedure Process_Range_Expr_In_Decl
20254 (R : Node_Id;
20255 T : Entity_Id;
20256 Subtyp : Entity_Id := Empty;
20257 Check_List : List_Id := Empty_List;
20258 R_Check_Off : Boolean := False;
20259 In_Iter_Schm : Boolean := False)
20261 Lo, Hi : Node_Id;
20262 R_Checks : Check_Result;
20263 Insert_Node : Node_Id;
20264 Def_Id : Entity_Id;
20266 begin
20267 Analyze_And_Resolve (R, Base_Type (T));
20269 if Nkind (R) = N_Range then
20271 -- In SPARK, all ranges should be static, with the exception of the
20272 -- discrete type definition of a loop parameter specification.
20274 if not In_Iter_Schm
20275 and then not Is_OK_Static_Range (R)
20276 then
20277 Check_SPARK_05_Restriction ("range should be static", R);
20278 end if;
20280 Lo := Low_Bound (R);
20281 Hi := High_Bound (R);
20283 -- Validity checks on the range of a quantified expression are
20284 -- delayed until the construct is transformed into a loop.
20286 if Nkind (Parent (R)) = N_Loop_Parameter_Specification
20287 and then Nkind (Parent (Parent (R))) = N_Quantified_Expression
20288 then
20289 null;
20291 -- We need to ensure validity of the bounds here, because if we
20292 -- go ahead and do the expansion, then the expanded code will get
20293 -- analyzed with range checks suppressed and we miss the check.
20295 -- WARNING: The capture of the range bounds with xxx_FIRST/_LAST and
20296 -- the temporaries generated by routine Remove_Side_Effects by means
20297 -- of validity checks must use the same names. When a range appears
20298 -- in the parent of a generic, the range is processed with checks
20299 -- disabled as part of the generic context and with checks enabled
20300 -- for code generation purposes. This leads to link issues as the
20301 -- generic contains references to xxx_FIRST/_LAST, but the inlined
20302 -- template sees the temporaries generated by Remove_Side_Effects.
20304 else
20305 Validity_Check_Range (R, Subtyp);
20306 end if;
20308 -- If there were errors in the declaration, try and patch up some
20309 -- common mistakes in the bounds. The cases handled are literals
20310 -- which are Integer where the expected type is Real and vice versa.
20311 -- These corrections allow the compilation process to proceed further
20312 -- along since some basic assumptions of the format of the bounds
20313 -- are guaranteed.
20315 if Etype (R) = Any_Type then
20316 if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
20317 Rewrite (Lo,
20318 Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
20320 elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
20321 Rewrite (Hi,
20322 Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
20324 elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
20325 Rewrite (Lo,
20326 Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
20328 elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
20329 Rewrite (Hi,
20330 Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
20331 end if;
20333 Set_Etype (Lo, T);
20334 Set_Etype (Hi, T);
20335 end if;
20337 -- If the bounds of the range have been mistakenly given as string
20338 -- literals (perhaps in place of character literals), then an error
20339 -- has already been reported, but we rewrite the string literal as a
20340 -- bound of the range's type to avoid blowups in later processing
20341 -- that looks at static values.
20343 if Nkind (Lo) = N_String_Literal then
20344 Rewrite (Lo,
20345 Make_Attribute_Reference (Sloc (Lo),
20346 Prefix => New_Occurrence_Of (T, Sloc (Lo)),
20347 Attribute_Name => Name_First));
20348 Analyze_And_Resolve (Lo);
20349 end if;
20351 if Nkind (Hi) = N_String_Literal then
20352 Rewrite (Hi,
20353 Make_Attribute_Reference (Sloc (Hi),
20354 Prefix => New_Occurrence_Of (T, Sloc (Hi)),
20355 Attribute_Name => Name_First));
20356 Analyze_And_Resolve (Hi);
20357 end if;
20359 -- If bounds aren't scalar at this point then exit, avoiding
20360 -- problems with further processing of the range in this procedure.
20362 if not Is_Scalar_Type (Etype (Lo)) then
20363 return;
20364 end if;
20366 -- Resolve (actually Sem_Eval) has checked that the bounds are in
20367 -- then range of the base type. Here we check whether the bounds
20368 -- are in the range of the subtype itself. Note that if the bounds
20369 -- represent the null range the Constraint_Error exception should
20370 -- not be raised.
20372 -- ??? The following code should be cleaned up as follows
20374 -- 1. The Is_Null_Range (Lo, Hi) test should disappear since it
20375 -- is done in the call to Range_Check (R, T); below
20377 -- 2. The use of R_Check_Off should be investigated and possibly
20378 -- removed, this would clean up things a bit.
20380 if Is_Null_Range (Lo, Hi) then
20381 null;
20383 else
20384 -- Capture values of bounds and generate temporaries for them
20385 -- if needed, before applying checks, since checks may cause
20386 -- duplication of the expression without forcing evaluation.
20388 -- The forced evaluation removes side effects from expressions,
20389 -- which should occur also in GNATprove mode. Otherwise, we end up
20390 -- with unexpected insertions of actions at places where this is
20391 -- not supposed to occur, e.g. on default parameters of a call.
20393 if Expander_Active or GNATprove_Mode then
20395 -- Call Force_Evaluation to create declarations as needed to
20396 -- deal with side effects, and also create typ_FIRST/LAST
20397 -- entities for bounds if we have a subtype name.
20399 -- Note: we do this transformation even if expansion is not
20400 -- active if we are in GNATprove_Mode since the transformation
20401 -- is in general required to ensure that the resulting tree has
20402 -- proper Ada semantics.
20404 Force_Evaluation
20405 (Lo, Related_Id => Subtyp, Is_Low_Bound => True);
20406 Force_Evaluation
20407 (Hi, Related_Id => Subtyp, Is_High_Bound => True);
20408 end if;
20410 -- We use a flag here instead of suppressing checks on the type
20411 -- because the type we check against isn't necessarily the place
20412 -- where we put the check.
20414 if not R_Check_Off then
20415 R_Checks := Get_Range_Checks (R, T);
20417 -- Look up tree to find an appropriate insertion point. We
20418 -- can't just use insert_actions because later processing
20419 -- depends on the insertion node. Prior to Ada 2012 the
20420 -- insertion point could only be a declaration or a loop, but
20421 -- quantified expressions can appear within any context in an
20422 -- expression, and the insertion point can be any statement,
20423 -- pragma, or declaration.
20425 Insert_Node := Parent (R);
20426 while Present (Insert_Node) loop
20427 exit when
20428 Nkind (Insert_Node) in N_Declaration
20429 and then
20430 not Nkind_In
20431 (Insert_Node, N_Component_Declaration,
20432 N_Loop_Parameter_Specification,
20433 N_Function_Specification,
20434 N_Procedure_Specification);
20436 exit when Nkind (Insert_Node) in N_Later_Decl_Item
20437 or else Nkind (Insert_Node) in
20438 N_Statement_Other_Than_Procedure_Call
20439 or else Nkind_In (Insert_Node, N_Procedure_Call_Statement,
20440 N_Pragma);
20442 Insert_Node := Parent (Insert_Node);
20443 end loop;
20445 -- Why would Type_Decl not be present??? Without this test,
20446 -- short regression tests fail.
20448 if Present (Insert_Node) then
20450 -- Case of loop statement. Verify that the range is part
20451 -- of the subtype indication of the iteration scheme.
20453 if Nkind (Insert_Node) = N_Loop_Statement then
20454 declare
20455 Indic : Node_Id;
20457 begin
20458 Indic := Parent (R);
20459 while Present (Indic)
20460 and then Nkind (Indic) /= N_Subtype_Indication
20461 loop
20462 Indic := Parent (Indic);
20463 end loop;
20465 if Present (Indic) then
20466 Def_Id := Etype (Subtype_Mark (Indic));
20468 Insert_Range_Checks
20469 (R_Checks,
20470 Insert_Node,
20471 Def_Id,
20472 Sloc (Insert_Node),
20474 Do_Before => True);
20475 end if;
20476 end;
20478 -- Insertion before a declaration. If the declaration
20479 -- includes discriminants, the list of applicable checks
20480 -- is given by the caller.
20482 elsif Nkind (Insert_Node) in N_Declaration then
20483 Def_Id := Defining_Identifier (Insert_Node);
20485 if (Ekind (Def_Id) = E_Record_Type
20486 and then Depends_On_Discriminant (R))
20487 or else
20488 (Ekind (Def_Id) = E_Protected_Type
20489 and then Has_Discriminants (Def_Id))
20490 then
20491 Append_Range_Checks
20492 (R_Checks,
20493 Check_List, Def_Id, Sloc (Insert_Node), R);
20495 else
20496 Insert_Range_Checks
20497 (R_Checks,
20498 Insert_Node, Def_Id, Sloc (Insert_Node), R);
20500 end if;
20502 -- Insertion before a statement. Range appears in the
20503 -- context of a quantified expression. Insertion will
20504 -- take place when expression is expanded.
20506 else
20507 null;
20508 end if;
20509 end if;
20510 end if;
20511 end if;
20513 -- Case of other than an explicit N_Range node
20515 -- The forced evaluation removes side effects from expressions, which
20516 -- should occur also in GNATprove mode. Otherwise, we end up with
20517 -- unexpected insertions of actions at places where this is not
20518 -- supposed to occur, e.g. on default parameters of a call.
20520 elsif Expander_Active or GNATprove_Mode then
20521 Get_Index_Bounds (R, Lo, Hi);
20522 Force_Evaluation (Lo);
20523 Force_Evaluation (Hi);
20524 end if;
20525 end Process_Range_Expr_In_Decl;
20527 --------------------------------------
20528 -- Process_Real_Range_Specification --
20529 --------------------------------------
20531 procedure Process_Real_Range_Specification (Def : Node_Id) is
20532 Spec : constant Node_Id := Real_Range_Specification (Def);
20533 Lo : Node_Id;
20534 Hi : Node_Id;
20535 Err : Boolean := False;
20537 procedure Analyze_Bound (N : Node_Id);
20538 -- Analyze and check one bound
20540 -------------------
20541 -- Analyze_Bound --
20542 -------------------
20544 procedure Analyze_Bound (N : Node_Id) is
20545 begin
20546 Analyze_And_Resolve (N, Any_Real);
20548 if not Is_OK_Static_Expression (N) then
20549 Flag_Non_Static_Expr
20550 ("bound in real type definition is not static!", N);
20551 Err := True;
20552 end if;
20553 end Analyze_Bound;
20555 -- Start of processing for Process_Real_Range_Specification
20557 begin
20558 if Present (Spec) then
20559 Lo := Low_Bound (Spec);
20560 Hi := High_Bound (Spec);
20561 Analyze_Bound (Lo);
20562 Analyze_Bound (Hi);
20564 -- If error, clear away junk range specification
20566 if Err then
20567 Set_Real_Range_Specification (Def, Empty);
20568 end if;
20569 end if;
20570 end Process_Real_Range_Specification;
20572 ---------------------
20573 -- Process_Subtype --
20574 ---------------------
20576 function Process_Subtype
20577 (S : Node_Id;
20578 Related_Nod : Node_Id;
20579 Related_Id : Entity_Id := Empty;
20580 Suffix : Character := ' ') return Entity_Id
20582 P : Node_Id;
20583 Def_Id : Entity_Id;
20584 Error_Node : Node_Id;
20585 Full_View_Id : Entity_Id;
20586 Subtype_Mark_Id : Entity_Id;
20588 May_Have_Null_Exclusion : Boolean;
20590 procedure Check_Incomplete (T : Entity_Id);
20591 -- Called to verify that an incomplete type is not used prematurely
20593 ----------------------
20594 -- Check_Incomplete --
20595 ----------------------
20597 procedure Check_Incomplete (T : Entity_Id) is
20598 begin
20599 -- Ada 2005 (AI-412): Incomplete subtypes are legal
20601 if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
20602 and then
20603 not (Ada_Version >= Ada_2005
20604 and then
20605 (Nkind (Parent (T)) = N_Subtype_Declaration
20606 or else (Nkind (Parent (T)) = N_Subtype_Indication
20607 and then Nkind (Parent (Parent (T))) =
20608 N_Subtype_Declaration)))
20609 then
20610 Error_Msg_N ("invalid use of type before its full declaration", T);
20611 end if;
20612 end Check_Incomplete;
20614 -- Start of processing for Process_Subtype
20616 begin
20617 -- Case of no constraints present
20619 if Nkind (S) /= N_Subtype_Indication then
20620 Find_Type (S);
20621 Check_Incomplete (S);
20622 P := Parent (S);
20624 -- Ada 2005 (AI-231): Static check
20626 if Ada_Version >= Ada_2005
20627 and then Present (P)
20628 and then Null_Exclusion_Present (P)
20629 and then Nkind (P) /= N_Access_To_Object_Definition
20630 and then not Is_Access_Type (Entity (S))
20631 then
20632 Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
20633 end if;
20635 -- The following is ugly, can't we have a range or even a flag???
20637 May_Have_Null_Exclusion :=
20638 Nkind_In (P, N_Access_Definition,
20639 N_Access_Function_Definition,
20640 N_Access_Procedure_Definition,
20641 N_Access_To_Object_Definition,
20642 N_Allocator,
20643 N_Component_Definition)
20644 or else
20645 Nkind_In (P, N_Derived_Type_Definition,
20646 N_Discriminant_Specification,
20647 N_Formal_Object_Declaration,
20648 N_Object_Declaration,
20649 N_Object_Renaming_Declaration,
20650 N_Parameter_Specification,
20651 N_Subtype_Declaration);
20653 -- Create an Itype that is a duplicate of Entity (S) but with the
20654 -- null-exclusion attribute.
20656 if May_Have_Null_Exclusion
20657 and then Is_Access_Type (Entity (S))
20658 and then Null_Exclusion_Present (P)
20660 -- No need to check the case of an access to object definition.
20661 -- It is correct to define double not-null pointers.
20663 -- Example:
20664 -- type Not_Null_Int_Ptr is not null access Integer;
20665 -- type Acc is not null access Not_Null_Int_Ptr;
20667 and then Nkind (P) /= N_Access_To_Object_Definition
20668 then
20669 if Can_Never_Be_Null (Entity (S)) then
20670 case Nkind (Related_Nod) is
20671 when N_Full_Type_Declaration =>
20672 if Nkind (Type_Definition (Related_Nod))
20673 in N_Array_Type_Definition
20674 then
20675 Error_Node :=
20676 Subtype_Indication
20677 (Component_Definition
20678 (Type_Definition (Related_Nod)));
20679 else
20680 Error_Node :=
20681 Subtype_Indication (Type_Definition (Related_Nod));
20682 end if;
20684 when N_Subtype_Declaration =>
20685 Error_Node := Subtype_Indication (Related_Nod);
20687 when N_Object_Declaration =>
20688 Error_Node := Object_Definition (Related_Nod);
20690 when N_Component_Declaration =>
20691 Error_Node :=
20692 Subtype_Indication (Component_Definition (Related_Nod));
20694 when N_Allocator =>
20695 Error_Node := Expression (Related_Nod);
20697 when others =>
20698 pragma Assert (False);
20699 Error_Node := Related_Nod;
20700 end case;
20702 Error_Msg_NE
20703 ("`NOT NULL` not allowed (& already excludes null)",
20704 Error_Node,
20705 Entity (S));
20706 end if;
20708 Set_Etype (S,
20709 Create_Null_Excluding_Itype
20710 (T => Entity (S),
20711 Related_Nod => P));
20712 Set_Entity (S, Etype (S));
20713 end if;
20715 return Entity (S);
20717 -- Case of constraint present, so that we have an N_Subtype_Indication
20718 -- node (this node is created only if constraints are present).
20720 else
20721 Find_Type (Subtype_Mark (S));
20723 if Nkind (Parent (S)) /= N_Access_To_Object_Definition
20724 and then not
20725 (Nkind (Parent (S)) = N_Subtype_Declaration
20726 and then Is_Itype (Defining_Identifier (Parent (S))))
20727 then
20728 Check_Incomplete (Subtype_Mark (S));
20729 end if;
20731 P := Parent (S);
20732 Subtype_Mark_Id := Entity (Subtype_Mark (S));
20734 -- Explicit subtype declaration case
20736 if Nkind (P) = N_Subtype_Declaration then
20737 Def_Id := Defining_Identifier (P);
20739 -- Explicit derived type definition case
20741 elsif Nkind (P) = N_Derived_Type_Definition then
20742 Def_Id := Defining_Identifier (Parent (P));
20744 -- Implicit case, the Def_Id must be created as an implicit type.
20745 -- The one exception arises in the case of concurrent types, array
20746 -- and access types, where other subsidiary implicit types may be
20747 -- created and must appear before the main implicit type. In these
20748 -- cases we leave Def_Id set to Empty as a signal that Create_Itype
20749 -- has not yet been called to create Def_Id.
20751 else
20752 if Is_Array_Type (Subtype_Mark_Id)
20753 or else Is_Concurrent_Type (Subtype_Mark_Id)
20754 or else Is_Access_Type (Subtype_Mark_Id)
20755 then
20756 Def_Id := Empty;
20758 -- For the other cases, we create a new unattached Itype,
20759 -- and set the indication to ensure it gets attached later.
20761 else
20762 Def_Id :=
20763 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
20764 end if;
20765 end if;
20767 -- If the kind of constraint is invalid for this kind of type,
20768 -- then give an error, and then pretend no constraint was given.
20770 if not Is_Valid_Constraint_Kind
20771 (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
20772 then
20773 Error_Msg_N
20774 ("incorrect constraint for this kind of type", Constraint (S));
20776 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
20778 -- Set Ekind of orphan itype, to prevent cascaded errors
20780 if Present (Def_Id) then
20781 Set_Ekind (Def_Id, Ekind (Any_Type));
20782 end if;
20784 -- Make recursive call, having got rid of the bogus constraint
20786 return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
20787 end if;
20789 -- Remaining processing depends on type. Select on Base_Type kind to
20790 -- ensure getting to the concrete type kind in the case of a private
20791 -- subtype (needed when only doing semantic analysis).
20793 case Ekind (Base_Type (Subtype_Mark_Id)) is
20794 when Access_Kind =>
20796 -- If this is a constraint on a class-wide type, discard it.
20797 -- There is currently no way to express a partial discriminant
20798 -- constraint on a type with unknown discriminants. This is
20799 -- a pathology that the ACATS wisely decides not to test.
20801 if Is_Class_Wide_Type (Designated_Type (Subtype_Mark_Id)) then
20802 if Comes_From_Source (S) then
20803 Error_Msg_N
20804 ("constraint on class-wide type ignored??",
20805 Constraint (S));
20806 end if;
20808 if Nkind (P) = N_Subtype_Declaration then
20809 Set_Subtype_Indication (P,
20810 New_Occurrence_Of (Subtype_Mark_Id, Sloc (S)));
20811 end if;
20813 return Subtype_Mark_Id;
20814 end if;
20816 Constrain_Access (Def_Id, S, Related_Nod);
20818 if Expander_Active
20819 and then Is_Itype (Designated_Type (Def_Id))
20820 and then Nkind (Related_Nod) = N_Subtype_Declaration
20821 and then not Is_Incomplete_Type (Designated_Type (Def_Id))
20822 then
20823 Build_Itype_Reference
20824 (Designated_Type (Def_Id), Related_Nod);
20825 end if;
20827 when Array_Kind =>
20828 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
20830 when Decimal_Fixed_Point_Kind =>
20831 Constrain_Decimal (Def_Id, S);
20833 when Enumeration_Kind =>
20834 Constrain_Enumeration (Def_Id, S);
20835 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
20837 when Ordinary_Fixed_Point_Kind =>
20838 Constrain_Ordinary_Fixed (Def_Id, S);
20840 when Float_Kind =>
20841 Constrain_Float (Def_Id, S);
20843 when Integer_Kind =>
20844 Constrain_Integer (Def_Id, S);
20845 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
20847 when E_Record_Type |
20848 E_Record_Subtype |
20849 Class_Wide_Kind |
20850 E_Incomplete_Type =>
20851 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
20853 if Ekind (Def_Id) = E_Incomplete_Type then
20854 Set_Private_Dependents (Def_Id, New_Elmt_List);
20855 end if;
20857 when Private_Kind =>
20858 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
20859 Set_Private_Dependents (Def_Id, New_Elmt_List);
20861 -- In case of an invalid constraint prevent further processing
20862 -- since the type constructed is missing expected fields.
20864 if Etype (Def_Id) = Any_Type then
20865 return Def_Id;
20866 end if;
20868 -- If the full view is that of a task with discriminants,
20869 -- we must constrain both the concurrent type and its
20870 -- corresponding record type. Otherwise we will just propagate
20871 -- the constraint to the full view, if available.
20873 if Present (Full_View (Subtype_Mark_Id))
20874 and then Has_Discriminants (Subtype_Mark_Id)
20875 and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
20876 then
20877 Full_View_Id :=
20878 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
20880 Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
20881 Constrain_Concurrent (Full_View_Id, S,
20882 Related_Nod, Related_Id, Suffix);
20883 Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
20884 Set_Full_View (Def_Id, Full_View_Id);
20886 -- Introduce an explicit reference to the private subtype,
20887 -- to prevent scope anomalies in gigi if first use appears
20888 -- in a nested context, e.g. a later function body.
20889 -- Should this be generated in other contexts than a full
20890 -- type declaration?
20892 if Is_Itype (Def_Id)
20893 and then
20894 Nkind (Parent (P)) = N_Full_Type_Declaration
20895 then
20896 Build_Itype_Reference (Def_Id, Parent (P));
20897 end if;
20899 else
20900 Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
20901 end if;
20903 when Concurrent_Kind =>
20904 Constrain_Concurrent (Def_Id, S,
20905 Related_Nod, Related_Id, Suffix);
20907 when others =>
20908 Error_Msg_N ("invalid subtype mark in subtype indication", S);
20909 end case;
20911 -- Size and Convention are always inherited from the base type
20913 Set_Size_Info (Def_Id, (Subtype_Mark_Id));
20914 Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
20916 return Def_Id;
20917 end if;
20918 end Process_Subtype;
20920 --------------------------------------------
20921 -- Propagate_Default_Init_Cond_Attributes --
20922 --------------------------------------------
20924 procedure Propagate_Default_Init_Cond_Attributes
20925 (From_Typ : Entity_Id;
20926 To_Typ : Entity_Id;
20927 Parent_To_Derivation : Boolean := False;
20928 Private_To_Full_View : Boolean := False)
20930 procedure Remove_Default_Init_Cond_Procedure (Typ : Entity_Id);
20931 -- Remove the default initial procedure (if any) from the rep chain of
20932 -- type Typ.
20934 ----------------------------------------
20935 -- Remove_Default_Init_Cond_Procedure --
20936 ----------------------------------------
20938 procedure Remove_Default_Init_Cond_Procedure (Typ : Entity_Id) is
20939 Found : Boolean := False;
20940 Prev : Entity_Id;
20941 Subp : Entity_Id;
20943 begin
20944 Prev := Typ;
20945 Subp := Subprograms_For_Type (Typ);
20946 while Present (Subp) loop
20947 if Is_Default_Init_Cond_Procedure (Subp) then
20948 Found := True;
20949 exit;
20950 end if;
20952 Prev := Subp;
20953 Subp := Subprograms_For_Type (Subp);
20954 end loop;
20956 if Found then
20957 Set_Subprograms_For_Type (Prev, Subprograms_For_Type (Subp));
20958 Set_Subprograms_For_Type (Subp, Empty);
20959 end if;
20960 end Remove_Default_Init_Cond_Procedure;
20962 -- Local variables
20964 Inherit_Procedure : Boolean := False;
20966 -- Start of processing for Propagate_Default_Init_Cond_Attributes
20968 begin
20969 if Has_Default_Init_Cond (From_Typ) then
20971 -- A derived type inherits the attributes from its parent type
20973 if Parent_To_Derivation then
20974 Set_Has_Inherited_Default_Init_Cond (To_Typ);
20976 -- A full view shares the attributes with its private view
20978 else
20979 Set_Has_Default_Init_Cond (To_Typ);
20980 end if;
20982 Inherit_Procedure := True;
20984 -- Due to the order of expansion, a derived private type is processed
20985 -- by two routines which both attempt to set the attributes related
20986 -- to pragma Default_Initial_Condition - Build_Derived_Type and then
20987 -- Process_Full_View.
20989 -- package Pack is
20990 -- type Parent_Typ is private
20991 -- with Default_Initial_Condition ...;
20992 -- private
20993 -- type Parent_Typ is ...;
20994 -- end Pack;
20996 -- with Pack; use Pack;
20997 -- package Pack_2 is
20998 -- type Deriv_Typ is private
20999 -- with Default_Initial_Condition ...;
21000 -- private
21001 -- type Deriv_Typ is new Parent_Typ;
21002 -- end Pack_2;
21004 -- When Build_Derived_Type operates, it sets the attributes on the
21005 -- full view without taking into account that the private view may
21006 -- define its own default initial condition procedure. This becomes
21007 -- apparent in Process_Full_View which must undo some of the work by
21008 -- Build_Derived_Type and propagate the attributes from the private
21009 -- to the full view.
21011 if Private_To_Full_View then
21012 Set_Has_Inherited_Default_Init_Cond (To_Typ, False);
21013 Remove_Default_Init_Cond_Procedure (To_Typ);
21014 end if;
21016 -- A type must inherit the default initial condition procedure from a
21017 -- parent type when the parent itself is inheriting the procedure or
21018 -- when it is defining one. This circuitry is also used when dealing
21019 -- with the private / full view of a type.
21021 elsif Has_Inherited_Default_Init_Cond (From_Typ)
21022 or (Parent_To_Derivation
21023 and Present (Get_Pragma
21024 (From_Typ, Pragma_Default_Initial_Condition)))
21025 then
21026 Set_Has_Inherited_Default_Init_Cond (To_Typ);
21027 Inherit_Procedure := True;
21028 end if;
21030 if Inherit_Procedure
21031 and then No (Default_Init_Cond_Procedure (To_Typ))
21032 then
21033 Set_Default_Init_Cond_Procedure
21034 (To_Typ, Default_Init_Cond_Procedure (From_Typ));
21035 end if;
21036 end Propagate_Default_Init_Cond_Attributes;
21038 -----------------------------
21039 -- Record_Type_Declaration --
21040 -----------------------------
21042 procedure Record_Type_Declaration
21043 (T : Entity_Id;
21044 N : Node_Id;
21045 Prev : Entity_Id)
21047 Def : constant Node_Id := Type_Definition (N);
21048 Is_Tagged : Boolean;
21049 Tag_Comp : Entity_Id;
21051 begin
21052 -- These flags must be initialized before calling Process_Discriminants
21053 -- because this routine makes use of them.
21055 Set_Ekind (T, E_Record_Type);
21056 Set_Etype (T, T);
21057 Init_Size_Align (T);
21058 Set_Interfaces (T, No_Elist);
21059 Set_Stored_Constraint (T, No_Elist);
21060 Set_Default_SSO (T);
21062 -- Normal case
21064 if Ada_Version < Ada_2005 or else not Interface_Present (Def) then
21065 if Limited_Present (Def) then
21066 Check_SPARK_05_Restriction ("limited is not allowed", N);
21067 end if;
21069 if Abstract_Present (Def) then
21070 Check_SPARK_05_Restriction ("abstract is not allowed", N);
21071 end if;
21073 -- The flag Is_Tagged_Type might have already been set by
21074 -- Find_Type_Name if it detected an error for declaration T. This
21075 -- arises in the case of private tagged types where the full view
21076 -- omits the word tagged.
21078 Is_Tagged :=
21079 Tagged_Present (Def)
21080 or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
21082 Set_Is_Limited_Record (T, Limited_Present (Def));
21084 if Is_Tagged then
21085 Set_Is_Tagged_Type (T, True);
21086 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
21087 end if;
21089 -- Type is abstract if full declaration carries keyword, or if
21090 -- previous partial view did.
21092 Set_Is_Abstract_Type (T, Is_Abstract_Type (T)
21093 or else Abstract_Present (Def));
21095 else
21096 Check_SPARK_05_Restriction ("interface is not allowed", N);
21098 Is_Tagged := True;
21099 Analyze_Interface_Declaration (T, Def);
21101 if Present (Discriminant_Specifications (N)) then
21102 Error_Msg_N
21103 ("interface types cannot have discriminants",
21104 Defining_Identifier
21105 (First (Discriminant_Specifications (N))));
21106 end if;
21107 end if;
21109 -- First pass: if there are self-referential access components,
21110 -- create the required anonymous access type declarations, and if
21111 -- need be an incomplete type declaration for T itself.
21113 Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
21115 if Ada_Version >= Ada_2005
21116 and then Present (Interface_List (Def))
21117 then
21118 Check_Interfaces (N, Def);
21120 declare
21121 Ifaces_List : Elist_Id;
21123 begin
21124 -- Ada 2005 (AI-251): Collect the list of progenitors that are not
21125 -- already in the parents.
21127 Collect_Interfaces
21128 (T => T,
21129 Ifaces_List => Ifaces_List,
21130 Exclude_Parents => True);
21132 Set_Interfaces (T, Ifaces_List);
21133 end;
21134 end if;
21136 -- Records constitute a scope for the component declarations within.
21137 -- The scope is created prior to the processing of these declarations.
21138 -- Discriminants are processed first, so that they are visible when
21139 -- processing the other components. The Ekind of the record type itself
21140 -- is set to E_Record_Type (subtypes appear as E_Record_Subtype).
21142 -- Enter record scope
21144 Push_Scope (T);
21146 -- If an incomplete or private type declaration was already given for
21147 -- the type, then this scope already exists, and the discriminants have
21148 -- been declared within. We must verify that the full declaration
21149 -- matches the incomplete one.
21151 Check_Or_Process_Discriminants (N, T, Prev);
21153 Set_Is_Constrained (T, not Has_Discriminants (T));
21154 Set_Has_Delayed_Freeze (T, True);
21156 -- For tagged types add a manually analyzed component corresponding
21157 -- to the component _tag, the corresponding piece of tree will be
21158 -- expanded as part of the freezing actions if it is not a CPP_Class.
21160 if Is_Tagged then
21162 -- Do not add the tag unless we are in expansion mode
21164 if Expander_Active then
21165 Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
21166 Enter_Name (Tag_Comp);
21168 Set_Ekind (Tag_Comp, E_Component);
21169 Set_Is_Tag (Tag_Comp);
21170 Set_Is_Aliased (Tag_Comp);
21171 Set_Etype (Tag_Comp, RTE (RE_Tag));
21172 Set_DT_Entry_Count (Tag_Comp, No_Uint);
21173 Set_Original_Record_Component (Tag_Comp, Tag_Comp);
21174 Init_Component_Location (Tag_Comp);
21176 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
21177 -- implemented interfaces.
21179 if Has_Interfaces (T) then
21180 Add_Interface_Tag_Components (N, T);
21181 end if;
21182 end if;
21184 Make_Class_Wide_Type (T);
21185 Set_Direct_Primitive_Operations (T, New_Elmt_List);
21186 end if;
21188 -- We must suppress range checks when processing record components in
21189 -- the presence of discriminants, since we don't want spurious checks to
21190 -- be generated during their analysis, but Suppress_Range_Checks flags
21191 -- must be reset the after processing the record definition.
21193 -- Note: this is the only use of Kill_Range_Checks, and is a bit odd,
21194 -- couldn't we just use the normal range check suppression method here.
21195 -- That would seem cleaner ???
21197 if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
21198 Set_Kill_Range_Checks (T, True);
21199 Record_Type_Definition (Def, Prev);
21200 Set_Kill_Range_Checks (T, False);
21201 else
21202 Record_Type_Definition (Def, Prev);
21203 end if;
21205 -- Exit from record scope
21207 End_Scope;
21209 -- Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
21210 -- the implemented interfaces and associate them an aliased entity.
21212 if Is_Tagged
21213 and then not Is_Empty_List (Interface_List (Def))
21214 then
21215 Derive_Progenitor_Subprograms (T, T);
21216 end if;
21218 Check_Function_Writable_Actuals (N);
21219 end Record_Type_Declaration;
21221 ----------------------------
21222 -- Record_Type_Definition --
21223 ----------------------------
21225 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
21226 Component : Entity_Id;
21227 Ctrl_Components : Boolean := False;
21228 Final_Storage_Only : Boolean;
21229 T : Entity_Id;
21231 begin
21232 if Ekind (Prev_T) = E_Incomplete_Type then
21233 T := Full_View (Prev_T);
21234 else
21235 T := Prev_T;
21236 end if;
21238 -- In SPARK, tagged types and type extensions may only be declared in
21239 -- the specification of library unit packages.
21241 if Present (Def) and then Is_Tagged_Type (T) then
21242 declare
21243 Typ : Node_Id;
21244 Ctxt : Node_Id;
21246 begin
21247 if Nkind (Parent (Def)) = N_Full_Type_Declaration then
21248 Typ := Parent (Def);
21249 else
21250 pragma Assert
21251 (Nkind (Parent (Def)) = N_Derived_Type_Definition);
21252 Typ := Parent (Parent (Def));
21253 end if;
21255 Ctxt := Parent (Typ);
21257 if Nkind (Ctxt) = N_Package_Body
21258 and then Nkind (Parent (Ctxt)) = N_Compilation_Unit
21259 then
21260 Check_SPARK_05_Restriction
21261 ("type should be defined in package specification", Typ);
21263 elsif Nkind (Ctxt) /= N_Package_Specification
21264 or else Nkind (Parent (Parent (Ctxt))) /= N_Compilation_Unit
21265 then
21266 Check_SPARK_05_Restriction
21267 ("type should be defined in library unit package", Typ);
21268 end if;
21269 end;
21270 end if;
21272 Final_Storage_Only := not Is_Controlled_Active (T);
21274 -- Ada 2005: Check whether an explicit Limited is present in a derived
21275 -- type declaration.
21277 if Nkind (Parent (Def)) = N_Derived_Type_Definition
21278 and then Limited_Present (Parent (Def))
21279 then
21280 Set_Is_Limited_Record (T);
21281 end if;
21283 -- If the component list of a record type is defined by the reserved
21284 -- word null and there is no discriminant part, then the record type has
21285 -- no components and all records of the type are null records (RM 3.7)
21286 -- This procedure is also called to process the extension part of a
21287 -- record extension, in which case the current scope may have inherited
21288 -- components.
21290 if No (Def)
21291 or else No (Component_List (Def))
21292 or else Null_Present (Component_List (Def))
21293 then
21294 if not Is_Tagged_Type (T) then
21295 Check_SPARK_05_Restriction ("untagged record cannot be null", Def);
21296 end if;
21298 else
21299 Analyze_Declarations (Component_Items (Component_List (Def)));
21301 if Present (Variant_Part (Component_List (Def))) then
21302 Check_SPARK_05_Restriction ("variant part is not allowed", Def);
21303 Analyze (Variant_Part (Component_List (Def)));
21304 end if;
21305 end if;
21307 -- After completing the semantic analysis of the record definition,
21308 -- record components, both new and inherited, are accessible. Set their
21309 -- kind accordingly. Exclude malformed itypes from illegal declarations,
21310 -- whose Ekind may be void.
21312 Component := First_Entity (Current_Scope);
21313 while Present (Component) loop
21314 if Ekind (Component) = E_Void
21315 and then not Is_Itype (Component)
21316 then
21317 Set_Ekind (Component, E_Component);
21318 Init_Component_Location (Component);
21319 end if;
21321 if Has_Task (Etype (Component)) then
21322 Set_Has_Task (T);
21323 end if;
21325 if Has_Protected (Etype (Component)) then
21326 Set_Has_Protected (T);
21327 end if;
21329 if Ekind (Component) /= E_Component then
21330 null;
21332 -- Do not set Has_Controlled_Component on a class-wide equivalent
21333 -- type. See Make_CW_Equivalent_Type.
21335 elsif not Is_Class_Wide_Equivalent_Type (T)
21336 and then (Has_Controlled_Component (Etype (Component))
21337 or else (Chars (Component) /= Name_uParent
21338 and then Is_Controlled_Active
21339 (Etype (Component))))
21340 then
21341 Set_Has_Controlled_Component (T, True);
21342 Final_Storage_Only :=
21343 Final_Storage_Only
21344 and then Finalize_Storage_Only (Etype (Component));
21345 Ctrl_Components := True;
21346 end if;
21348 Next_Entity (Component);
21349 end loop;
21351 -- A Type is Finalize_Storage_Only only if all its controlled components
21352 -- are also.
21354 if Ctrl_Components then
21355 Set_Finalize_Storage_Only (T, Final_Storage_Only);
21356 end if;
21358 -- Place reference to end record on the proper entity, which may
21359 -- be a partial view.
21361 if Present (Def) then
21362 Process_End_Label (Def, 'e', Prev_T);
21363 end if;
21364 end Record_Type_Definition;
21366 ------------------------
21367 -- Replace_Components --
21368 ------------------------
21370 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id) is
21371 function Process (N : Node_Id) return Traverse_Result;
21373 -------------
21374 -- Process --
21375 -------------
21377 function Process (N : Node_Id) return Traverse_Result is
21378 Comp : Entity_Id;
21380 begin
21381 if Nkind (N) = N_Discriminant_Specification then
21382 Comp := First_Discriminant (Typ);
21383 while Present (Comp) loop
21384 if Chars (Comp) = Chars (Defining_Identifier (N)) then
21385 Set_Defining_Identifier (N, Comp);
21386 exit;
21387 end if;
21389 Next_Discriminant (Comp);
21390 end loop;
21392 elsif Nkind (N) = N_Component_Declaration then
21393 Comp := First_Component (Typ);
21394 while Present (Comp) loop
21395 if Chars (Comp) = Chars (Defining_Identifier (N)) then
21396 Set_Defining_Identifier (N, Comp);
21397 exit;
21398 end if;
21400 Next_Component (Comp);
21401 end loop;
21402 end if;
21404 return OK;
21405 end Process;
21407 procedure Replace is new Traverse_Proc (Process);
21409 -- Start of processing for Replace_Components
21411 begin
21412 Replace (Decl);
21413 end Replace_Components;
21415 -------------------------------
21416 -- Set_Completion_Referenced --
21417 -------------------------------
21419 procedure Set_Completion_Referenced (E : Entity_Id) is
21420 begin
21421 -- If in main unit, mark entity that is a completion as referenced,
21422 -- warnings go on the partial view when needed.
21424 if In_Extended_Main_Source_Unit (E) then
21425 Set_Referenced (E);
21426 end if;
21427 end Set_Completion_Referenced;
21429 ---------------------
21430 -- Set_Default_SSO --
21431 ---------------------
21433 procedure Set_Default_SSO (T : Entity_Id) is
21434 begin
21435 case Opt.Default_SSO is
21436 when ' ' =>
21437 null;
21438 when 'L' =>
21439 Set_SSO_Set_Low_By_Default (T, True);
21440 when 'H' =>
21441 Set_SSO_Set_High_By_Default (T, True);
21442 when others =>
21443 raise Program_Error;
21444 end case;
21445 end Set_Default_SSO;
21447 ---------------------
21448 -- Set_Fixed_Range --
21449 ---------------------
21451 -- The range for fixed-point types is complicated by the fact that we
21452 -- do not know the exact end points at the time of the declaration. This
21453 -- is true for three reasons:
21455 -- A size clause may affect the fudging of the end-points.
21456 -- A small clause may affect the values of the end-points.
21457 -- We try to include the end-points if it does not affect the size.
21459 -- This means that the actual end-points must be established at the
21460 -- point when the type is frozen. Meanwhile, we first narrow the range
21461 -- as permitted (so that it will fit if necessary in a small specified
21462 -- size), and then build a range subtree with these narrowed bounds.
21463 -- Set_Fixed_Range constructs the range from real literal values, and
21464 -- sets the range as the Scalar_Range of the given fixed-point type entity.
21466 -- The parent of this range is set to point to the entity so that it is
21467 -- properly hooked into the tree (unlike normal Scalar_Range entries for
21468 -- other scalar types, which are just pointers to the range in the
21469 -- original tree, this would otherwise be an orphan).
21471 -- The tree is left unanalyzed. When the type is frozen, the processing
21472 -- in Freeze.Freeze_Fixed_Point_Type notices that the range is not
21473 -- analyzed, and uses this as an indication that it should complete
21474 -- work on the range (it will know the final small and size values).
21476 procedure Set_Fixed_Range
21477 (E : Entity_Id;
21478 Loc : Source_Ptr;
21479 Lo : Ureal;
21480 Hi : Ureal)
21482 S : constant Node_Id :=
21483 Make_Range (Loc,
21484 Low_Bound => Make_Real_Literal (Loc, Lo),
21485 High_Bound => Make_Real_Literal (Loc, Hi));
21486 begin
21487 Set_Scalar_Range (E, S);
21488 Set_Parent (S, E);
21490 -- Before the freeze point, the bounds of a fixed point are universal
21491 -- and carry the corresponding type.
21493 Set_Etype (Low_Bound (S), Universal_Real);
21494 Set_Etype (High_Bound (S), Universal_Real);
21495 end Set_Fixed_Range;
21497 ----------------------------------
21498 -- Set_Scalar_Range_For_Subtype --
21499 ----------------------------------
21501 procedure Set_Scalar_Range_For_Subtype
21502 (Def_Id : Entity_Id;
21503 R : Node_Id;
21504 Subt : Entity_Id)
21506 Kind : constant Entity_Kind := Ekind (Def_Id);
21508 begin
21509 -- Defend against previous error
21511 if Nkind (R) = N_Error then
21512 return;
21513 end if;
21515 Set_Scalar_Range (Def_Id, R);
21517 -- We need to link the range into the tree before resolving it so
21518 -- that types that are referenced, including importantly the subtype
21519 -- itself, are properly frozen (Freeze_Expression requires that the
21520 -- expression be properly linked into the tree). Of course if it is
21521 -- already linked in, then we do not disturb the current link.
21523 if No (Parent (R)) then
21524 Set_Parent (R, Def_Id);
21525 end if;
21527 -- Reset the kind of the subtype during analysis of the range, to
21528 -- catch possible premature use in the bounds themselves.
21530 Set_Ekind (Def_Id, E_Void);
21531 Process_Range_Expr_In_Decl (R, Subt, Subtyp => Def_Id);
21532 Set_Ekind (Def_Id, Kind);
21533 end Set_Scalar_Range_For_Subtype;
21535 --------------------------------------------------------
21536 -- Set_Stored_Constraint_From_Discriminant_Constraint --
21537 --------------------------------------------------------
21539 procedure Set_Stored_Constraint_From_Discriminant_Constraint
21540 (E : Entity_Id)
21542 begin
21543 -- Make sure set if encountered during Expand_To_Stored_Constraint
21545 Set_Stored_Constraint (E, No_Elist);
21547 -- Give it the right value
21549 if Is_Constrained (E) and then Has_Discriminants (E) then
21550 Set_Stored_Constraint (E,
21551 Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
21552 end if;
21553 end Set_Stored_Constraint_From_Discriminant_Constraint;
21555 -------------------------------------
21556 -- Signed_Integer_Type_Declaration --
21557 -------------------------------------
21559 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
21560 Implicit_Base : Entity_Id;
21561 Base_Typ : Entity_Id;
21562 Lo_Val : Uint;
21563 Hi_Val : Uint;
21564 Errs : Boolean := False;
21565 Lo : Node_Id;
21566 Hi : Node_Id;
21568 function Can_Derive_From (E : Entity_Id) return Boolean;
21569 -- Determine whether given bounds allow derivation from specified type
21571 procedure Check_Bound (Expr : Node_Id);
21572 -- Check bound to make sure it is integral and static. If not, post
21573 -- appropriate error message and set Errs flag
21575 ---------------------
21576 -- Can_Derive_From --
21577 ---------------------
21579 -- Note we check both bounds against both end values, to deal with
21580 -- strange types like ones with a range of 0 .. -12341234.
21582 function Can_Derive_From (E : Entity_Id) return Boolean is
21583 Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
21584 Hi : constant Uint := Expr_Value (Type_High_Bound (E));
21585 begin
21586 return Lo <= Lo_Val and then Lo_Val <= Hi
21587 and then
21588 Lo <= Hi_Val and then Hi_Val <= Hi;
21589 end Can_Derive_From;
21591 -----------------
21592 -- Check_Bound --
21593 -----------------
21595 procedure Check_Bound (Expr : Node_Id) is
21596 begin
21597 -- If a range constraint is used as an integer type definition, each
21598 -- bound of the range must be defined by a static expression of some
21599 -- integer type, but the two bounds need not have the same integer
21600 -- type (Negative bounds are allowed.) (RM 3.5.4)
21602 if not Is_Integer_Type (Etype (Expr)) then
21603 Error_Msg_N
21604 ("integer type definition bounds must be of integer type", Expr);
21605 Errs := True;
21607 elsif not Is_OK_Static_Expression (Expr) then
21608 Flag_Non_Static_Expr
21609 ("non-static expression used for integer type bound!", Expr);
21610 Errs := True;
21612 -- The bounds are folded into literals, and we set their type to be
21613 -- universal, to avoid typing difficulties: we cannot set the type
21614 -- of the literal to the new type, because this would be a forward
21615 -- reference for the back end, and if the original type is user-
21616 -- defined this can lead to spurious semantic errors (e.g. 2928-003).
21618 else
21619 if Is_Entity_Name (Expr) then
21620 Fold_Uint (Expr, Expr_Value (Expr), True);
21621 end if;
21623 Set_Etype (Expr, Universal_Integer);
21624 end if;
21625 end Check_Bound;
21627 -- Start of processing for Signed_Integer_Type_Declaration
21629 begin
21630 -- Create an anonymous base type
21632 Implicit_Base :=
21633 Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
21635 -- Analyze and check the bounds, they can be of any integer type
21637 Lo := Low_Bound (Def);
21638 Hi := High_Bound (Def);
21640 -- Arbitrarily use Integer as the type if either bound had an error
21642 if Hi = Error or else Lo = Error then
21643 Base_Typ := Any_Integer;
21644 Set_Error_Posted (T, True);
21646 -- Here both bounds are OK expressions
21648 else
21649 Analyze_And_Resolve (Lo, Any_Integer);
21650 Analyze_And_Resolve (Hi, Any_Integer);
21652 Check_Bound (Lo);
21653 Check_Bound (Hi);
21655 if Errs then
21656 Hi := Type_High_Bound (Standard_Long_Long_Integer);
21657 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
21658 end if;
21660 -- Find type to derive from
21662 Lo_Val := Expr_Value (Lo);
21663 Hi_Val := Expr_Value (Hi);
21665 if Can_Derive_From (Standard_Short_Short_Integer) then
21666 Base_Typ := Base_Type (Standard_Short_Short_Integer);
21668 elsif Can_Derive_From (Standard_Short_Integer) then
21669 Base_Typ := Base_Type (Standard_Short_Integer);
21671 elsif Can_Derive_From (Standard_Integer) then
21672 Base_Typ := Base_Type (Standard_Integer);
21674 elsif Can_Derive_From (Standard_Long_Integer) then
21675 Base_Typ := Base_Type (Standard_Long_Integer);
21677 elsif Can_Derive_From (Standard_Long_Long_Integer) then
21678 Check_Restriction (No_Long_Long_Integers, Def);
21679 Base_Typ := Base_Type (Standard_Long_Long_Integer);
21681 else
21682 Base_Typ := Base_Type (Standard_Long_Long_Integer);
21683 Error_Msg_N ("integer type definition bounds out of range", Def);
21684 Hi := Type_High_Bound (Standard_Long_Long_Integer);
21685 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
21686 end if;
21687 end if;
21689 -- Complete both implicit base and declared first subtype entities. The
21690 -- inheritance of the rep item chain ensures that SPARK-related pragmas
21691 -- are not clobbered when the signed integer type acts as a full view of
21692 -- a private type.
21694 Set_Etype (Implicit_Base, Base_Typ);
21695 Set_Size_Info (Implicit_Base, Base_Typ);
21696 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
21697 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
21698 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
21700 Set_Ekind (T, E_Signed_Integer_Subtype);
21701 Set_Etype (T, Implicit_Base);
21702 Set_Size_Info (T, Implicit_Base);
21703 Inherit_Rep_Item_Chain (T, Implicit_Base);
21704 Set_Scalar_Range (T, Def);
21705 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
21706 Set_Is_Constrained (T);
21707 end Signed_Integer_Type_Declaration;
21709 end Sem_Ch3;