2015-01-30 Gary Dismukes <dismukes@adacore.com>
[official-gcc.git] / gcc / ada / sem_ch3.adb
blob7699a6fc80f98ea1b5720fdcd6feac8cfd0c339d
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ C H 3 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2015, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Debug; use Debug;
30 with Elists; use Elists;
31 with Einfo; use Einfo;
32 with Errout; use Errout;
33 with Eval_Fat; use Eval_Fat;
34 with Exp_Ch3; use Exp_Ch3;
35 with Exp_Ch9; use Exp_Ch9;
36 with Exp_Disp; use Exp_Disp;
37 with Exp_Dist; use Exp_Dist;
38 with Exp_Tss; use Exp_Tss;
39 with Exp_Util; use Exp_Util;
40 with Fname; use Fname;
41 with Freeze; use Freeze;
42 with Ghost; use Ghost;
43 with Itypes; use Itypes;
44 with Layout; use Layout;
45 with Lib; use Lib;
46 with Lib.Xref; use Lib.Xref;
47 with Namet; use Namet;
48 with Nmake; use Nmake;
49 with Opt; use Opt;
50 with Restrict; use Restrict;
51 with Rident; use Rident;
52 with Rtsfind; use Rtsfind;
53 with Sem; use Sem;
54 with Sem_Aux; use Sem_Aux;
55 with Sem_Case; use Sem_Case;
56 with Sem_Cat; use Sem_Cat;
57 with Sem_Ch6; use Sem_Ch6;
58 with Sem_Ch7; use Sem_Ch7;
59 with Sem_Ch8; use Sem_Ch8;
60 with Sem_Ch10; use Sem_Ch10;
61 with Sem_Ch13; use Sem_Ch13;
62 with Sem_Dim; use Sem_Dim;
63 with Sem_Disp; use Sem_Disp;
64 with Sem_Dist; use Sem_Dist;
65 with Sem_Elim; use Sem_Elim;
66 with Sem_Eval; use Sem_Eval;
67 with Sem_Mech; use Sem_Mech;
68 with Sem_Prag; use Sem_Prag;
69 with Sem_Res; use Sem_Res;
70 with Sem_Smem; use Sem_Smem;
71 with Sem_Type; use Sem_Type;
72 with Sem_Util; use Sem_Util;
73 with Sem_Warn; use Sem_Warn;
74 with Stand; use Stand;
75 with Sinfo; use Sinfo;
76 with Sinput; use Sinput;
77 with Snames; use Snames;
78 with Targparm; use Targparm;
79 with Tbuild; use Tbuild;
80 with Ttypes; use Ttypes;
81 with Uintp; use Uintp;
82 with Urealp; use Urealp;
84 package body Sem_Ch3 is
86 -----------------------
87 -- Local Subprograms --
88 -----------------------
90 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id);
91 -- Ada 2005 (AI-251): Add the tag components corresponding to all the
92 -- abstract interface types implemented by a record type or a derived
93 -- record type.
95 procedure Analyze_Object_Contract (Obj_Id : Entity_Id);
96 -- Analyze all delayed pragmas chained on the contract of object Obj_Id as
97 -- if they appeared at the end of the declarative region. The pragmas to be
98 -- considered are:
99 -- Async_Readers
100 -- Async_Writers
101 -- Effective_Reads
102 -- Effective_Writes
103 -- Part_Of
105 procedure Build_Derived_Type
106 (N : Node_Id;
107 Parent_Type : Entity_Id;
108 Derived_Type : Entity_Id;
109 Is_Completion : Boolean;
110 Derive_Subps : Boolean := True);
111 -- Create and decorate a Derived_Type given the Parent_Type entity. N is
112 -- the N_Full_Type_Declaration node containing the derived type definition.
113 -- Parent_Type is the entity for the parent type in the derived type
114 -- definition and Derived_Type the actual derived type. Is_Completion must
115 -- be set to False if Derived_Type is the N_Defining_Identifier node in N
116 -- (i.e. Derived_Type = Defining_Identifier (N)). In this case N is not the
117 -- completion of a private type declaration. If Is_Completion is set to
118 -- True, N is the completion of a private type declaration and Derived_Type
119 -- is different from the defining identifier inside N (i.e. Derived_Type /=
120 -- Defining_Identifier (N)). Derive_Subps indicates whether the parent
121 -- subprograms should be derived. The only case where this parameter is
122 -- False is when Build_Derived_Type is recursively called to process an
123 -- implicit derived full type for a type derived from a private type (in
124 -- that case the subprograms must only be derived for the private view of
125 -- the type).
127 -- ??? These flags need a bit of re-examination and re-documentation:
128 -- ??? are they both necessary (both seem related to the recursion)?
130 procedure Build_Derived_Access_Type
131 (N : Node_Id;
132 Parent_Type : Entity_Id;
133 Derived_Type : Entity_Id);
134 -- Subsidiary procedure to Build_Derived_Type. For a derived access type,
135 -- create an implicit base if the parent type is constrained or if the
136 -- subtype indication has a constraint.
138 procedure Build_Derived_Array_Type
139 (N : Node_Id;
140 Parent_Type : Entity_Id;
141 Derived_Type : Entity_Id);
142 -- Subsidiary procedure to Build_Derived_Type. For a derived array type,
143 -- create an implicit base if the parent type is constrained or if the
144 -- subtype indication has a constraint.
146 procedure Build_Derived_Concurrent_Type
147 (N : Node_Id;
148 Parent_Type : Entity_Id;
149 Derived_Type : Entity_Id);
150 -- Subsidiary procedure to Build_Derived_Type. For a derived task or
151 -- protected type, inherit entries and protected subprograms, check
152 -- legality of discriminant constraints if any.
154 procedure Build_Derived_Enumeration_Type
155 (N : Node_Id;
156 Parent_Type : Entity_Id;
157 Derived_Type : Entity_Id);
158 -- Subsidiary procedure to Build_Derived_Type. For a derived enumeration
159 -- type, we must create a new list of literals. Types derived from
160 -- Character and [Wide_]Wide_Character are special-cased.
162 procedure Build_Derived_Numeric_Type
163 (N : Node_Id;
164 Parent_Type : Entity_Id;
165 Derived_Type : Entity_Id);
166 -- Subsidiary procedure to Build_Derived_Type. For numeric types, create
167 -- an anonymous base type, and propagate constraint to subtype if needed.
169 procedure Build_Derived_Private_Type
170 (N : Node_Id;
171 Parent_Type : Entity_Id;
172 Derived_Type : Entity_Id;
173 Is_Completion : Boolean;
174 Derive_Subps : Boolean := True);
175 -- Subsidiary procedure to Build_Derived_Type. This procedure is complex
176 -- because the parent may or may not have a completion, and the derivation
177 -- may itself be a completion.
179 procedure Build_Derived_Record_Type
180 (N : Node_Id;
181 Parent_Type : Entity_Id;
182 Derived_Type : Entity_Id;
183 Derive_Subps : Boolean := True);
184 -- Subsidiary procedure used for tagged and untagged record types
185 -- by Build_Derived_Type and Analyze_Private_Extension_Declaration.
186 -- All parameters are as in Build_Derived_Type except that N, in
187 -- addition to being an N_Full_Type_Declaration node, can also be an
188 -- N_Private_Extension_Declaration node. See the definition of this routine
189 -- for much more info. Derive_Subps indicates whether subprograms should be
190 -- derived from the parent type. The only case where Derive_Subps is False
191 -- is for an implicit derived full type for a type derived from a private
192 -- type (see Build_Derived_Type).
194 procedure Build_Discriminal (Discrim : Entity_Id);
195 -- Create the discriminal corresponding to discriminant Discrim, that is
196 -- the parameter corresponding to Discrim to be used in initialization
197 -- procedures for the type where Discrim is a discriminant. Discriminals
198 -- are not used during semantic analysis, and are not fully defined
199 -- entities until expansion. Thus they are not given a scope until
200 -- initialization procedures are built.
202 function Build_Discriminant_Constraints
203 (T : Entity_Id;
204 Def : Node_Id;
205 Derived_Def : Boolean := False) return Elist_Id;
206 -- Validate discriminant constraints and return the list of the constraints
207 -- in order of discriminant declarations, where T is the discriminated
208 -- unconstrained type. Def is the N_Subtype_Indication node where the
209 -- discriminants constraints for T are specified. Derived_Def is True
210 -- when building the discriminant constraints in a derived type definition
211 -- of the form "type D (...) is new T (xxx)". In this case T is the parent
212 -- type and Def is the constraint "(xxx)" on T and this routine sets the
213 -- Corresponding_Discriminant field of the discriminants in the derived
214 -- type D to point to the corresponding discriminants in the parent type T.
216 procedure Build_Discriminated_Subtype
217 (T : Entity_Id;
218 Def_Id : Entity_Id;
219 Elist : Elist_Id;
220 Related_Nod : Node_Id;
221 For_Access : Boolean := False);
222 -- Subsidiary procedure to Constrain_Discriminated_Type and to
223 -- Process_Incomplete_Dependents. Given
225 -- T (a possibly discriminated base type)
226 -- Def_Id (a very partially built subtype for T),
228 -- the call completes Def_Id to be the appropriate E_*_Subtype.
230 -- The Elist is the list of discriminant constraints if any (it is set
231 -- to No_Elist if T is not a discriminated type, and to an empty list if
232 -- T has discriminants but there are no discriminant constraints). The
233 -- Related_Nod is the same as Decl_Node in Create_Constrained_Components.
234 -- The For_Access says whether or not this subtype is really constraining
235 -- an access type. That is its sole purpose is the designated type of an
236 -- access type -- in which case a Private_Subtype Is_For_Access_Subtype
237 -- is built to avoid freezing T when the access subtype is frozen.
239 function Build_Scalar_Bound
240 (Bound : Node_Id;
241 Par_T : Entity_Id;
242 Der_T : Entity_Id) return Node_Id;
243 -- The bounds of a derived scalar type are conversions of the bounds of
244 -- the parent type. Optimize the representation if the bounds are literals.
245 -- Needs a more complete spec--what are the parameters exactly, and what
246 -- exactly is the returned value, and how is Bound affected???
248 procedure Build_Underlying_Full_View
249 (N : Node_Id;
250 Typ : Entity_Id;
251 Par : Entity_Id);
252 -- If the completion of a private type is itself derived from a private
253 -- type, or if the full view of a private subtype is itself private, the
254 -- back-end has no way to compute the actual size of this type. We build
255 -- an internal subtype declaration of the proper parent type to convey
256 -- this information. This extra mechanism is needed because a full
257 -- view cannot itself have a full view (it would get clobbered during
258 -- view exchanges).
260 procedure Check_Access_Discriminant_Requires_Limited
261 (D : Node_Id;
262 Loc : Node_Id);
263 -- Check the restriction that the type to which an access discriminant
264 -- belongs must be a concurrent type or a descendant of a type with
265 -- the reserved word 'limited' in its declaration.
267 procedure Check_Anonymous_Access_Components
268 (Typ_Decl : Node_Id;
269 Typ : Entity_Id;
270 Prev : Entity_Id;
271 Comp_List : Node_Id);
272 -- Ada 2005 AI-382: an access component in a record definition can refer to
273 -- the enclosing record, in which case it denotes the type itself, and not
274 -- the current instance of the type. We create an anonymous access type for
275 -- the component, and flag it as an access to a component, so accessibility
276 -- checks are properly performed on it. The declaration of the access type
277 -- is placed ahead of that of the record to prevent order-of-elaboration
278 -- circularity issues in Gigi. We create an incomplete type for the record
279 -- declaration, which is the designated type of the anonymous access.
281 procedure Check_Delta_Expression (E : Node_Id);
282 -- Check that the expression represented by E is suitable for use as a
283 -- delta expression, i.e. it is of real type and is static.
285 procedure Check_Digits_Expression (E : Node_Id);
286 -- Check that the expression represented by E is suitable for use as a
287 -- digits expression, i.e. it is of integer type, positive and static.
289 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id);
290 -- Validate the initialization of an object declaration. T is the required
291 -- type, and Exp is the initialization expression.
293 procedure Check_Interfaces (N : Node_Id; Def : Node_Id);
294 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
296 procedure Check_Or_Process_Discriminants
297 (N : Node_Id;
298 T : Entity_Id;
299 Prev : Entity_Id := Empty);
300 -- If N is the full declaration of the completion T of an incomplete or
301 -- private type, check its discriminants (which are already known to be
302 -- conformant with those of the partial view, see Find_Type_Name),
303 -- otherwise process them. Prev is the entity of the partial declaration,
304 -- if any.
306 procedure Check_Real_Bound (Bound : Node_Id);
307 -- Check given bound for being of real type and static. If not, post an
308 -- appropriate message, and rewrite the bound with the real literal zero.
310 procedure Constant_Redeclaration
311 (Id : Entity_Id;
312 N : Node_Id;
313 T : out Entity_Id);
314 -- Various checks on legality of full declaration of deferred constant.
315 -- Id is the entity for the redeclaration, N is the N_Object_Declaration,
316 -- node. The caller has not yet set any attributes of this entity.
318 function Contain_Interface
319 (Iface : Entity_Id;
320 Ifaces : Elist_Id) return Boolean;
321 -- Ada 2005: Determine whether Iface is present in the list Ifaces
323 procedure Convert_Scalar_Bounds
324 (N : Node_Id;
325 Parent_Type : Entity_Id;
326 Derived_Type : Entity_Id;
327 Loc : Source_Ptr);
328 -- For derived scalar types, convert the bounds in the type definition to
329 -- the derived type, and complete their analysis. Given a constraint of the
330 -- form ".. new T range Lo .. Hi", Lo and Hi are analyzed and resolved with
331 -- T'Base, the parent_type. The bounds of the derived type (the anonymous
332 -- base) are copies of Lo and Hi. Finally, the bounds of the derived
333 -- subtype are conversions of those bounds to the derived_type, so that
334 -- their typing is consistent.
336 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id);
337 -- Copies attributes from array base type T2 to array base type T1. Copies
338 -- only attributes that apply to base types, but not subtypes.
340 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id);
341 -- Copies attributes from array subtype T2 to array subtype T1. Copies
342 -- attributes that apply to both subtypes and base types.
344 procedure Create_Constrained_Components
345 (Subt : Entity_Id;
346 Decl_Node : Node_Id;
347 Typ : Entity_Id;
348 Constraints : Elist_Id);
349 -- Build the list of entities for a constrained discriminated record
350 -- subtype. If a component depends on a discriminant, replace its subtype
351 -- using the discriminant values in the discriminant constraint. Subt
352 -- is the defining identifier for the subtype whose list of constrained
353 -- entities we will create. Decl_Node is the type declaration node where
354 -- we will attach all the itypes created. Typ is the base discriminated
355 -- type for the subtype Subt. Constraints is the list of discriminant
356 -- constraints for Typ.
358 function Constrain_Component_Type
359 (Comp : Entity_Id;
360 Constrained_Typ : Entity_Id;
361 Related_Node : Node_Id;
362 Typ : Entity_Id;
363 Constraints : Elist_Id) return Entity_Id;
364 -- Given a discriminated base type Typ, a list of discriminant constraints,
365 -- Constraints, for Typ and a component Comp of Typ, create and return the
366 -- type corresponding to Etype (Comp) where all discriminant references
367 -- are replaced with the corresponding constraint. If Etype (Comp) contains
368 -- no discriminant references then it is returned as-is. Constrained_Typ
369 -- is the final constrained subtype to which the constrained component
370 -- belongs. Related_Node is the node where we attach all created itypes.
372 procedure Constrain_Access
373 (Def_Id : in out Entity_Id;
374 S : Node_Id;
375 Related_Nod : Node_Id);
376 -- Apply a list of constraints to an access type. If Def_Id is empty, it is
377 -- an anonymous type created for a subtype indication. In that case it is
378 -- created in the procedure and attached to Related_Nod.
380 procedure Constrain_Array
381 (Def_Id : in out Entity_Id;
382 SI : Node_Id;
383 Related_Nod : Node_Id;
384 Related_Id : Entity_Id;
385 Suffix : Character);
386 -- Apply a list of index constraints to an unconstrained array type. The
387 -- first parameter is the entity for the resulting subtype. A value of
388 -- Empty for Def_Id indicates that an implicit type must be created, but
389 -- creation is delayed (and must be done by this procedure) because other
390 -- subsidiary implicit types must be created first (which is why Def_Id
391 -- is an in/out parameter). The second parameter is a subtype indication
392 -- node for the constrained array to be created (e.g. something of the
393 -- form string (1 .. 10)). Related_Nod gives the place where this type
394 -- has to be inserted in the tree. The Related_Id and Suffix parameters
395 -- are used to build the associated Implicit type name.
397 procedure Constrain_Concurrent
398 (Def_Id : in out Entity_Id;
399 SI : Node_Id;
400 Related_Nod : Node_Id;
401 Related_Id : Entity_Id;
402 Suffix : Character);
403 -- Apply list of discriminant constraints to an unconstrained concurrent
404 -- type.
406 -- SI is the N_Subtype_Indication node containing the constraint and
407 -- the unconstrained type to constrain.
409 -- Def_Id is the entity for the resulting constrained subtype. A value
410 -- of Empty for Def_Id indicates that an implicit type must be created,
411 -- but creation is delayed (and must be done by this procedure) because
412 -- other subsidiary implicit types must be created first (which is why
413 -- Def_Id is an in/out parameter).
415 -- Related_Nod gives the place where this type has to be inserted
416 -- in the tree.
418 -- The last two arguments are used to create its external name if needed.
420 function Constrain_Corresponding_Record
421 (Prot_Subt : Entity_Id;
422 Corr_Rec : Entity_Id;
423 Related_Nod : Node_Id) return Entity_Id;
424 -- When constraining a protected type or task type with discriminants,
425 -- constrain the corresponding record with the same discriminant values.
427 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id);
428 -- Constrain a decimal fixed point type with a digits constraint and/or a
429 -- range constraint, and build E_Decimal_Fixed_Point_Subtype entity.
431 procedure Constrain_Discriminated_Type
432 (Def_Id : Entity_Id;
433 S : Node_Id;
434 Related_Nod : Node_Id;
435 For_Access : Boolean := False);
436 -- Process discriminant constraints of composite type. Verify that values
437 -- have been provided for all discriminants, that the original type is
438 -- unconstrained, and that the types of the supplied expressions match
439 -- the discriminant types. The first three parameters are like in routine
440 -- Constrain_Concurrent. See Build_Discriminated_Subtype for an explanation
441 -- of For_Access.
443 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id);
444 -- Constrain an enumeration type with a range constraint. This is identical
445 -- to Constrain_Integer, but for the Ekind of the resulting subtype.
447 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id);
448 -- Constrain a floating point type with either a digits constraint
449 -- and/or a range constraint, building a E_Floating_Point_Subtype.
451 procedure Constrain_Index
452 (Index : Node_Id;
453 S : Node_Id;
454 Related_Nod : Node_Id;
455 Related_Id : Entity_Id;
456 Suffix : Character;
457 Suffix_Index : Nat);
458 -- Process an index constraint S in a constrained array declaration. The
459 -- constraint can be a subtype name, or a range with or without an explicit
460 -- subtype mark. The index is the corresponding index of the unconstrained
461 -- array. The Related_Id and Suffix parameters are used to build the
462 -- associated Implicit type name.
464 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id);
465 -- Build subtype of a signed or modular integer type
467 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id);
468 -- Constrain an ordinary fixed point type with a range constraint, and
469 -- build an E_Ordinary_Fixed_Point_Subtype entity.
471 procedure Copy_And_Swap (Priv, Full : Entity_Id);
472 -- Copy the Priv entity into the entity of its full declaration then swap
473 -- the two entities in such a manner that the former private type is now
474 -- seen as a full type.
476 procedure Decimal_Fixed_Point_Type_Declaration
477 (T : Entity_Id;
478 Def : Node_Id);
479 -- Create a new decimal fixed point type, and apply the constraint to
480 -- obtain a subtype of this new type.
482 procedure Complete_Private_Subtype
483 (Priv : Entity_Id;
484 Full : Entity_Id;
485 Full_Base : Entity_Id;
486 Related_Nod : Node_Id);
487 -- Complete the implicit full view of a private subtype by setting the
488 -- appropriate semantic fields. If the full view of the parent is a record
489 -- type, build constrained components of subtype.
491 procedure Derive_Progenitor_Subprograms
492 (Parent_Type : Entity_Id;
493 Tagged_Type : Entity_Id);
494 -- Ada 2005 (AI-251): To complete type derivation, collect the primitive
495 -- operations of progenitors of Tagged_Type, and replace the subsidiary
496 -- subtypes with Tagged_Type, to build the specs of the inherited interface
497 -- primitives. The derived primitives are aliased to those of the
498 -- interface. This routine takes care also of transferring to the full view
499 -- subprograms associated with the partial view of Tagged_Type that cover
500 -- interface primitives.
502 procedure Derived_Standard_Character
503 (N : Node_Id;
504 Parent_Type : Entity_Id;
505 Derived_Type : Entity_Id);
506 -- Subsidiary procedure to Build_Derived_Enumeration_Type which handles
507 -- derivations from types Standard.Character and Standard.Wide_Character.
509 procedure Derived_Type_Declaration
510 (T : Entity_Id;
511 N : Node_Id;
512 Is_Completion : Boolean);
513 -- Process a derived type declaration. Build_Derived_Type is invoked
514 -- to process the actual derived type definition. Parameters N and
515 -- Is_Completion have the same meaning as in Build_Derived_Type.
516 -- T is the N_Defining_Identifier for the entity defined in the
517 -- N_Full_Type_Declaration node N, that is T is the derived type.
519 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id);
520 -- Insert each literal in symbol table, as an overloadable identifier. Each
521 -- enumeration type is mapped into a sequence of integers, and each literal
522 -- is defined as a constant with integer value. If any of the literals are
523 -- character literals, the type is a character type, which means that
524 -- strings are legal aggregates for arrays of components of the type.
526 function Expand_To_Stored_Constraint
527 (Typ : Entity_Id;
528 Constraint : Elist_Id) return Elist_Id;
529 -- Given a constraint (i.e. a list of expressions) on the discriminants of
530 -- Typ, expand it into a constraint on the stored discriminants and return
531 -- the new list of expressions constraining the stored discriminants.
533 function Find_Type_Of_Object
534 (Obj_Def : Node_Id;
535 Related_Nod : Node_Id) return Entity_Id;
536 -- Get type entity for object referenced by Obj_Def, attaching the implicit
537 -- types generated to Related_Nod.
539 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id);
540 -- Create a new float and apply the constraint to obtain subtype of it
542 function Has_Range_Constraint (N : Node_Id) return Boolean;
543 -- Given an N_Subtype_Indication node N, return True if a range constraint
544 -- is present, either directly, or as part of a digits or delta constraint.
545 -- In addition, a digits constraint in the decimal case returns True, since
546 -- it establishes a default range if no explicit range is present.
548 function Inherit_Components
549 (N : Node_Id;
550 Parent_Base : Entity_Id;
551 Derived_Base : Entity_Id;
552 Is_Tagged : Boolean;
553 Inherit_Discr : Boolean;
554 Discs : Elist_Id) return Elist_Id;
555 -- Called from Build_Derived_Record_Type to inherit the components of
556 -- Parent_Base (a base type) into the Derived_Base (the derived base type).
557 -- For more information on derived types and component inheritance please
558 -- consult the comment above the body of Build_Derived_Record_Type.
560 -- N is the original derived type declaration
562 -- Is_Tagged is set if we are dealing with tagged types
564 -- If Inherit_Discr is set, Derived_Base inherits its discriminants from
565 -- Parent_Base, otherwise no discriminants are inherited.
567 -- Discs gives the list of constraints that apply to Parent_Base in the
568 -- derived type declaration. If Discs is set to No_Elist, then we have
569 -- the following situation:
571 -- type Parent (D1..Dn : ..) is [tagged] record ...;
572 -- type Derived is new Parent [with ...];
574 -- which gets treated as
576 -- type Derived (D1..Dn : ..) is new Parent (D1,..,Dn) [with ...];
578 -- For untagged types the returned value is an association list. The list
579 -- starts from the association (Parent_Base => Derived_Base), and then it
580 -- contains a sequence of the associations of the form
582 -- (Old_Component => New_Component),
584 -- where Old_Component is the Entity_Id of a component in Parent_Base and
585 -- New_Component is the Entity_Id of the corresponding component in
586 -- Derived_Base. For untagged records, this association list is needed when
587 -- copying the record declaration for the derived base. In the tagged case
588 -- the value returned is irrelevant.
590 procedure Inherit_Predicate_Flags (Subt, Par : Entity_Id);
591 -- Propagate static and dynamic predicate flags from a parent to the
592 -- subtype in a subtype declaration with and without constraints.
594 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean;
595 -- Subsidiary to Check_Abstract_Overriding and Derive_Subprogram.
596 -- Determine whether subprogram Subp is a procedure subject to pragma
597 -- Extensions_Visible with value False and has at least one controlling
598 -- parameter of mode OUT.
600 function Is_Valid_Constraint_Kind
601 (T_Kind : Type_Kind;
602 Constraint_Kind : Node_Kind) return Boolean;
603 -- Returns True if it is legal to apply the given kind of constraint to the
604 -- given kind of type (index constraint to an array type, for example).
606 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id);
607 -- Create new modular type. Verify that modulus is in bounds
609 procedure New_Concatenation_Op (Typ : Entity_Id);
610 -- Create an abbreviated declaration for an operator in order to
611 -- materialize concatenation on array types.
613 procedure Ordinary_Fixed_Point_Type_Declaration
614 (T : Entity_Id;
615 Def : Node_Id);
616 -- Create a new ordinary fixed point type, and apply the constraint to
617 -- obtain subtype of it.
619 procedure Prepare_Private_Subtype_Completion
620 (Id : Entity_Id;
621 Related_Nod : Node_Id);
622 -- Id is a subtype of some private type. Creates the full declaration
623 -- associated with Id whenever possible, i.e. when the full declaration
624 -- of the base type is already known. Records each subtype into
625 -- Private_Dependents of the base type.
627 procedure Process_Incomplete_Dependents
628 (N : Node_Id;
629 Full_T : Entity_Id;
630 Inc_T : Entity_Id);
631 -- Process all entities that depend on an incomplete type. There include
632 -- subtypes, subprogram types that mention the incomplete type in their
633 -- profiles, and subprogram with access parameters that designate the
634 -- incomplete type.
636 -- Inc_T is the defining identifier of an incomplete type declaration, its
637 -- Ekind is E_Incomplete_Type.
639 -- N is the corresponding N_Full_Type_Declaration for Inc_T.
641 -- Full_T is N's defining identifier.
643 -- Subtypes of incomplete types with discriminants are completed when the
644 -- parent type is. This is simpler than private subtypes, because they can
645 -- only appear in the same scope, and there is no need to exchange views.
646 -- Similarly, access_to_subprogram types may have a parameter or a return
647 -- type that is an incomplete type, and that must be replaced with the
648 -- full type.
650 -- If the full type is tagged, subprogram with access parameters that
651 -- designated the incomplete may be primitive operations of the full type,
652 -- and have to be processed accordingly.
654 procedure Process_Real_Range_Specification (Def : Node_Id);
655 -- Given the type definition for a real type, this procedure processes and
656 -- checks the real range specification of this type definition if one is
657 -- present. If errors are found, error messages are posted, and the
658 -- Real_Range_Specification of Def is reset to Empty.
660 procedure Propagate_Default_Init_Cond_Attributes
661 (From_Typ : Entity_Id;
662 To_Typ : Entity_Id;
663 Parent_To_Derivation : Boolean := False;
664 Private_To_Full_View : Boolean := False);
665 -- Subsidiary to routines Build_Derived_Type and Process_Full_View. Inherit
666 -- all attributes related to pragma Default_Initial_Condition from From_Typ
667 -- to To_Typ. Flag Parent_To_Derivation should be set when the context is
668 -- the creation of a derived type. Flag Private_To_Full_View should be set
669 -- when processing both views of a private type.
671 procedure Record_Type_Declaration
672 (T : Entity_Id;
673 N : Node_Id;
674 Prev : Entity_Id);
675 -- Process a record type declaration (for both untagged and tagged
676 -- records). Parameters T and N are exactly like in procedure
677 -- Derived_Type_Declaration, except that no flag Is_Completion is needed
678 -- for this routine. If this is the completion of an incomplete type
679 -- declaration, Prev is the entity of the incomplete declaration, used for
680 -- cross-referencing. Otherwise Prev = T.
682 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id);
683 -- This routine is used to process the actual record type definition (both
684 -- for untagged and tagged records). Def is a record type definition node.
685 -- This procedure analyzes the components in this record type definition.
686 -- Prev_T is the entity for the enclosing record type. It is provided so
687 -- that its Has_Task flag can be set if any of the component have Has_Task
688 -- set. If the declaration is the completion of an incomplete type
689 -- declaration, Prev_T is the original incomplete type, whose full view is
690 -- the record type.
692 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id);
693 -- Subsidiary to Build_Derived_Record_Type. For untagged records, we
694 -- build a copy of the declaration tree of the parent, and we create
695 -- independently the list of components for the derived type. Semantic
696 -- information uses the component entities, but record representation
697 -- clauses are validated on the declaration tree. This procedure replaces
698 -- discriminants and components in the declaration with those that have
699 -- been created by Inherit_Components.
701 procedure Set_Fixed_Range
702 (E : Entity_Id;
703 Loc : Source_Ptr;
704 Lo : Ureal;
705 Hi : Ureal);
706 -- Build a range node with the given bounds and set it as the Scalar_Range
707 -- of the given fixed-point type entity. Loc is the source location used
708 -- for the constructed range. See body for further details.
710 procedure Set_Scalar_Range_For_Subtype
711 (Def_Id : Entity_Id;
712 R : Node_Id;
713 Subt : Entity_Id);
714 -- This routine is used to set the scalar range field for a subtype given
715 -- Def_Id, the entity for the subtype, and R, the range expression for the
716 -- scalar range. Subt provides the parent subtype to be used to analyze,
717 -- resolve, and check the given range.
719 procedure Set_Default_SSO (T : Entity_Id);
720 -- T is the entity for an array or record being declared. This procedure
721 -- sets the flags SSO_Set_Low_By_Default/SSO_Set_High_By_Default according
722 -- to the setting of Opt.Default_SSO.
724 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id);
725 -- Create a new signed integer entity, and apply the constraint to obtain
726 -- the required first named subtype of this type.
728 procedure Set_Stored_Constraint_From_Discriminant_Constraint
729 (E : Entity_Id);
730 -- E is some record type. This routine computes E's Stored_Constraint
731 -- from its Discriminant_Constraint.
733 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id);
734 -- Check that an entity in a list of progenitors is an interface,
735 -- emit error otherwise.
737 -----------------------
738 -- Access_Definition --
739 -----------------------
741 function Access_Definition
742 (Related_Nod : Node_Id;
743 N : Node_Id) return Entity_Id
745 Anon_Type : Entity_Id;
746 Anon_Scope : Entity_Id;
747 Desig_Type : Entity_Id;
748 Enclosing_Prot_Type : Entity_Id := Empty;
750 begin
751 Check_SPARK_05_Restriction ("access type is not allowed", N);
753 if Is_Entry (Current_Scope)
754 and then Is_Task_Type (Etype (Scope (Current_Scope)))
755 then
756 Error_Msg_N ("task entries cannot have access parameters", N);
757 return Empty;
758 end if;
760 -- Ada 2005: For an object declaration the corresponding anonymous
761 -- type is declared in the current scope.
763 -- If the access definition is the return type of another access to
764 -- function, scope is the current one, because it is the one of the
765 -- current type declaration, except for the pathological case below.
767 if Nkind_In (Related_Nod, N_Object_Declaration,
768 N_Access_Function_Definition)
769 then
770 Anon_Scope := Current_Scope;
772 -- A pathological case: function returning access functions that
773 -- return access functions, etc. Each anonymous access type created
774 -- is in the enclosing scope of the outermost function.
776 declare
777 Par : Node_Id;
779 begin
780 Par := Related_Nod;
781 while Nkind_In (Par, N_Access_Function_Definition,
782 N_Access_Definition)
783 loop
784 Par := Parent (Par);
785 end loop;
787 if Nkind (Par) = N_Function_Specification then
788 Anon_Scope := Scope (Defining_Entity (Par));
789 end if;
790 end;
792 -- For the anonymous function result case, retrieve the scope of the
793 -- function specification's associated entity rather than using the
794 -- current scope. The current scope will be the function itself if the
795 -- formal part is currently being analyzed, but will be the parent scope
796 -- in the case of a parameterless function, and we always want to use
797 -- the function's parent scope. Finally, if the function is a child
798 -- unit, we must traverse the tree to retrieve the proper entity.
800 elsif Nkind (Related_Nod) = N_Function_Specification
801 and then Nkind (Parent (N)) /= N_Parameter_Specification
802 then
803 -- If the current scope is a protected type, the anonymous access
804 -- is associated with one of the protected operations, and must
805 -- be available in the scope that encloses the protected declaration.
806 -- Otherwise the type is in the scope enclosing the subprogram.
808 -- If the function has formals, The return type of a subprogram
809 -- declaration is analyzed in the scope of the subprogram (see
810 -- Process_Formals) and thus the protected type, if present, is
811 -- the scope of the current function scope.
813 if Ekind (Current_Scope) = E_Protected_Type then
814 Enclosing_Prot_Type := Current_Scope;
816 elsif Ekind (Current_Scope) = E_Function
817 and then Ekind (Scope (Current_Scope)) = E_Protected_Type
818 then
819 Enclosing_Prot_Type := Scope (Current_Scope);
820 end if;
822 if Present (Enclosing_Prot_Type) then
823 Anon_Scope := Scope (Enclosing_Prot_Type);
825 else
826 Anon_Scope := Scope (Defining_Entity (Related_Nod));
827 end if;
829 -- For an access type definition, if the current scope is a child
830 -- unit it is the scope of the type.
832 elsif Is_Compilation_Unit (Current_Scope) then
833 Anon_Scope := Current_Scope;
835 -- For access formals, access components, and access discriminants, the
836 -- scope is that of the enclosing declaration,
838 else
839 Anon_Scope := Scope (Current_Scope);
840 end if;
842 Anon_Type :=
843 Create_Itype
844 (E_Anonymous_Access_Type, Related_Nod, Scope_Id => Anon_Scope);
846 if All_Present (N)
847 and then Ada_Version >= Ada_2005
848 then
849 Error_Msg_N ("ALL is not permitted for anonymous access types", N);
850 end if;
852 -- Ada 2005 (AI-254): In case of anonymous access to subprograms call
853 -- the corresponding semantic routine
855 if Present (Access_To_Subprogram_Definition (N)) then
857 -- Compiler runtime units are compiled in Ada 2005 mode when building
858 -- the runtime library but must also be compilable in Ada 95 mode
859 -- (when bootstrapping the compiler).
861 Check_Compiler_Unit ("anonymous access to subprogram", N);
863 Access_Subprogram_Declaration
864 (T_Name => Anon_Type,
865 T_Def => Access_To_Subprogram_Definition (N));
867 if Ekind (Anon_Type) = E_Access_Protected_Subprogram_Type then
868 Set_Ekind
869 (Anon_Type, E_Anonymous_Access_Protected_Subprogram_Type);
870 else
871 Set_Ekind (Anon_Type, E_Anonymous_Access_Subprogram_Type);
872 end if;
874 Set_Can_Use_Internal_Rep
875 (Anon_Type, not Always_Compatible_Rep_On_Target);
877 -- If the anonymous access is associated with a protected operation,
878 -- create a reference to it after the enclosing protected definition
879 -- because the itype will be used in the subsequent bodies.
881 if Ekind (Current_Scope) = E_Protected_Type then
882 Build_Itype_Reference (Anon_Type, Parent (Current_Scope));
883 end if;
885 return Anon_Type;
886 end if;
888 Find_Type (Subtype_Mark (N));
889 Desig_Type := Entity (Subtype_Mark (N));
891 Set_Directly_Designated_Type (Anon_Type, Desig_Type);
892 Set_Etype (Anon_Type, Anon_Type);
894 -- Make sure the anonymous access type has size and alignment fields
895 -- set, as required by gigi. This is necessary in the case of the
896 -- Task_Body_Procedure.
898 if not Has_Private_Component (Desig_Type) then
899 Layout_Type (Anon_Type);
900 end if;
902 -- Ada 2005 (AI-231): Ada 2005 semantics for anonymous access differs
903 -- from Ada 95 semantics. In Ada 2005, anonymous access must specify if
904 -- the null value is allowed. In Ada 95 the null value is never allowed.
906 if Ada_Version >= Ada_2005 then
907 Set_Can_Never_Be_Null (Anon_Type, Null_Exclusion_Present (N));
908 else
909 Set_Can_Never_Be_Null (Anon_Type, True);
910 end if;
912 -- The anonymous access type is as public as the discriminated type or
913 -- subprogram that defines it. It is imported (for back-end purposes)
914 -- if the designated type is.
916 Set_Is_Public (Anon_Type, Is_Public (Scope (Anon_Type)));
918 -- Ada 2005 (AI-231): Propagate the access-constant attribute
920 Set_Is_Access_Constant (Anon_Type, Constant_Present (N));
922 -- The context is either a subprogram declaration, object declaration,
923 -- or an access discriminant, in a private or a full type declaration.
924 -- In the case of a subprogram, if the designated type is incomplete,
925 -- the operation will be a primitive operation of the full type, to be
926 -- updated subsequently. If the type is imported through a limited_with
927 -- clause, the subprogram is not a primitive operation of the type
928 -- (which is declared elsewhere in some other scope).
930 if Ekind (Desig_Type) = E_Incomplete_Type
931 and then not From_Limited_With (Desig_Type)
932 and then Is_Overloadable (Current_Scope)
933 then
934 Append_Elmt (Current_Scope, Private_Dependents (Desig_Type));
935 Set_Has_Delayed_Freeze (Current_Scope);
936 end if;
938 -- Ada 2005: If the designated type is an interface that may contain
939 -- tasks, create a Master entity for the declaration. This must be done
940 -- before expansion of the full declaration, because the declaration may
941 -- include an expression that is an allocator, whose expansion needs the
942 -- proper Master for the created tasks.
944 if Nkind (Related_Nod) = N_Object_Declaration and then Expander_Active
945 then
946 if Is_Interface (Desig_Type) and then Is_Limited_Record (Desig_Type)
947 then
948 Build_Class_Wide_Master (Anon_Type);
950 -- Similarly, if the type is an anonymous access that designates
951 -- tasks, create a master entity for it in the current context.
953 elsif Has_Task (Desig_Type) and then Comes_From_Source (Related_Nod)
954 then
955 Build_Master_Entity (Defining_Identifier (Related_Nod));
956 Build_Master_Renaming (Anon_Type);
957 end if;
958 end if;
960 -- For a private component of a protected type, it is imperative that
961 -- the back-end elaborate the type immediately after the protected
962 -- declaration, because this type will be used in the declarations
963 -- created for the component within each protected body, so we must
964 -- create an itype reference for it now.
966 if Nkind (Parent (Related_Nod)) = N_Protected_Definition then
967 Build_Itype_Reference (Anon_Type, Parent (Parent (Related_Nod)));
969 -- Similarly, if the access definition is the return result of a
970 -- function, create an itype reference for it because it will be used
971 -- within the function body. For a regular function that is not a
972 -- compilation unit, insert reference after the declaration. For a
973 -- protected operation, insert it after the enclosing protected type
974 -- declaration. In either case, do not create a reference for a type
975 -- obtained through a limited_with clause, because this would introduce
976 -- semantic dependencies.
978 -- Similarly, do not create a reference if the designated type is a
979 -- generic formal, because no use of it will reach the backend.
981 elsif Nkind (Related_Nod) = N_Function_Specification
982 and then not From_Limited_With (Desig_Type)
983 and then not Is_Generic_Type (Desig_Type)
984 then
985 if Present (Enclosing_Prot_Type) then
986 Build_Itype_Reference (Anon_Type, Parent (Enclosing_Prot_Type));
988 elsif Is_List_Member (Parent (Related_Nod))
989 and then Nkind (Parent (N)) /= N_Parameter_Specification
990 then
991 Build_Itype_Reference (Anon_Type, Parent (Related_Nod));
992 end if;
994 -- Finally, create an itype reference for an object declaration of an
995 -- anonymous access type. This is strictly necessary only for deferred
996 -- constants, but in any case will avoid out-of-scope problems in the
997 -- back-end.
999 elsif Nkind (Related_Nod) = N_Object_Declaration then
1000 Build_Itype_Reference (Anon_Type, Related_Nod);
1001 end if;
1003 return Anon_Type;
1004 end Access_Definition;
1006 -----------------------------------
1007 -- Access_Subprogram_Declaration --
1008 -----------------------------------
1010 procedure Access_Subprogram_Declaration
1011 (T_Name : Entity_Id;
1012 T_Def : Node_Id)
1014 procedure Check_For_Premature_Usage (Def : Node_Id);
1015 -- Check that type T_Name is not used, directly or recursively, as a
1016 -- parameter or a return type in Def. Def is either a subtype, an
1017 -- access_definition, or an access_to_subprogram_definition.
1019 -------------------------------
1020 -- Check_For_Premature_Usage --
1021 -------------------------------
1023 procedure Check_For_Premature_Usage (Def : Node_Id) is
1024 Param : Node_Id;
1026 begin
1027 -- Check for a subtype mark
1029 if Nkind (Def) in N_Has_Etype then
1030 if Etype (Def) = T_Name then
1031 Error_Msg_N
1032 ("type& cannot be used before end of its declaration", Def);
1033 end if;
1035 -- If this is not a subtype, then this is an access_definition
1037 elsif Nkind (Def) = N_Access_Definition then
1038 if Present (Access_To_Subprogram_Definition (Def)) then
1039 Check_For_Premature_Usage
1040 (Access_To_Subprogram_Definition (Def));
1041 else
1042 Check_For_Premature_Usage (Subtype_Mark (Def));
1043 end if;
1045 -- The only cases left are N_Access_Function_Definition and
1046 -- N_Access_Procedure_Definition.
1048 else
1049 if Present (Parameter_Specifications (Def)) then
1050 Param := First (Parameter_Specifications (Def));
1051 while Present (Param) loop
1052 Check_For_Premature_Usage (Parameter_Type (Param));
1053 Param := Next (Param);
1054 end loop;
1055 end if;
1057 if Nkind (Def) = N_Access_Function_Definition then
1058 Check_For_Premature_Usage (Result_Definition (Def));
1059 end if;
1060 end if;
1061 end Check_For_Premature_Usage;
1063 -- Local variables
1065 Formals : constant List_Id := Parameter_Specifications (T_Def);
1066 Formal : Entity_Id;
1067 D_Ityp : Node_Id;
1068 Desig_Type : constant Entity_Id :=
1069 Create_Itype (E_Subprogram_Type, Parent (T_Def));
1071 -- Start of processing for Access_Subprogram_Declaration
1073 begin
1074 Check_SPARK_05_Restriction ("access type is not allowed", T_Def);
1076 -- Associate the Itype node with the inner full-type declaration or
1077 -- subprogram spec or entry body. This is required to handle nested
1078 -- anonymous declarations. For example:
1080 -- procedure P
1081 -- (X : access procedure
1082 -- (Y : access procedure
1083 -- (Z : access T)))
1085 D_Ityp := Associated_Node_For_Itype (Desig_Type);
1086 while not (Nkind_In (D_Ityp, N_Full_Type_Declaration,
1087 N_Private_Type_Declaration,
1088 N_Private_Extension_Declaration,
1089 N_Procedure_Specification,
1090 N_Function_Specification,
1091 N_Entry_Body)
1093 or else
1094 Nkind_In (D_Ityp, N_Object_Declaration,
1095 N_Object_Renaming_Declaration,
1096 N_Formal_Object_Declaration,
1097 N_Formal_Type_Declaration,
1098 N_Task_Type_Declaration,
1099 N_Protected_Type_Declaration))
1100 loop
1101 D_Ityp := Parent (D_Ityp);
1102 pragma Assert (D_Ityp /= Empty);
1103 end loop;
1105 Set_Associated_Node_For_Itype (Desig_Type, D_Ityp);
1107 if Nkind_In (D_Ityp, N_Procedure_Specification,
1108 N_Function_Specification)
1109 then
1110 Set_Scope (Desig_Type, Scope (Defining_Entity (D_Ityp)));
1112 elsif Nkind_In (D_Ityp, N_Full_Type_Declaration,
1113 N_Object_Declaration,
1114 N_Object_Renaming_Declaration,
1115 N_Formal_Type_Declaration)
1116 then
1117 Set_Scope (Desig_Type, Scope (Defining_Identifier (D_Ityp)));
1118 end if;
1120 if Nkind (T_Def) = N_Access_Function_Definition then
1121 if Nkind (Result_Definition (T_Def)) = N_Access_Definition then
1122 declare
1123 Acc : constant Node_Id := Result_Definition (T_Def);
1125 begin
1126 if Present (Access_To_Subprogram_Definition (Acc))
1127 and then
1128 Protected_Present (Access_To_Subprogram_Definition (Acc))
1129 then
1130 Set_Etype
1131 (Desig_Type,
1132 Replace_Anonymous_Access_To_Protected_Subprogram
1133 (T_Def));
1135 else
1136 Set_Etype
1137 (Desig_Type,
1138 Access_Definition (T_Def, Result_Definition (T_Def)));
1139 end if;
1140 end;
1142 else
1143 Analyze (Result_Definition (T_Def));
1145 declare
1146 Typ : constant Entity_Id := Entity (Result_Definition (T_Def));
1148 begin
1149 -- If a null exclusion is imposed on the result type, then
1150 -- create a null-excluding itype (an access subtype) and use
1151 -- it as the function's Etype.
1153 if Is_Access_Type (Typ)
1154 and then Null_Exclusion_In_Return_Present (T_Def)
1155 then
1156 Set_Etype (Desig_Type,
1157 Create_Null_Excluding_Itype
1158 (T => Typ,
1159 Related_Nod => T_Def,
1160 Scope_Id => Current_Scope));
1162 else
1163 if From_Limited_With (Typ) then
1165 -- AI05-151: Incomplete types are allowed in all basic
1166 -- declarations, including access to subprograms.
1168 if Ada_Version >= Ada_2012 then
1169 null;
1171 else
1172 Error_Msg_NE
1173 ("illegal use of incomplete type&",
1174 Result_Definition (T_Def), Typ);
1175 end if;
1177 elsif Ekind (Current_Scope) = E_Package
1178 and then In_Private_Part (Current_Scope)
1179 then
1180 if Ekind (Typ) = E_Incomplete_Type then
1181 Append_Elmt (Desig_Type, Private_Dependents (Typ));
1183 elsif Is_Class_Wide_Type (Typ)
1184 and then Ekind (Etype (Typ)) = E_Incomplete_Type
1185 then
1186 Append_Elmt
1187 (Desig_Type, Private_Dependents (Etype (Typ)));
1188 end if;
1189 end if;
1191 Set_Etype (Desig_Type, Typ);
1192 end if;
1193 end;
1194 end if;
1196 if not (Is_Type (Etype (Desig_Type))) then
1197 Error_Msg_N
1198 ("expect type in function specification",
1199 Result_Definition (T_Def));
1200 end if;
1202 else
1203 Set_Etype (Desig_Type, Standard_Void_Type);
1204 end if;
1206 if Present (Formals) then
1207 Push_Scope (Desig_Type);
1209 -- Some special tests here. These special tests can be removed
1210 -- if and when Itypes always have proper parent pointers to their
1211 -- declarations???
1213 -- Special test 1) Link defining_identifier of formals. Required by
1214 -- First_Formal to provide its functionality.
1216 declare
1217 F : Node_Id;
1219 begin
1220 F := First (Formals);
1222 -- In ASIS mode, the access_to_subprogram may be analyzed twice,
1223 -- when it is part of an unconstrained type and subtype expansion
1224 -- is disabled. To avoid back-end problems with shared profiles,
1225 -- use previous subprogram type as the designated type, and then
1226 -- remove scope added above.
1228 if ASIS_Mode and then Present (Scope (Defining_Identifier (F)))
1229 then
1230 Set_Etype (T_Name, T_Name);
1231 Init_Size_Align (T_Name);
1232 Set_Directly_Designated_Type (T_Name,
1233 Scope (Defining_Identifier (F)));
1234 End_Scope;
1235 return;
1236 end if;
1238 while Present (F) loop
1239 if No (Parent (Defining_Identifier (F))) then
1240 Set_Parent (Defining_Identifier (F), F);
1241 end if;
1243 Next (F);
1244 end loop;
1245 end;
1247 Process_Formals (Formals, Parent (T_Def));
1249 -- Special test 2) End_Scope requires that the parent pointer be set
1250 -- to something reasonable, but Itypes don't have parent pointers. So
1251 -- we set it and then unset it ???
1253 Set_Parent (Desig_Type, T_Name);
1254 End_Scope;
1255 Set_Parent (Desig_Type, Empty);
1256 end if;
1258 -- Check for premature usage of the type being defined
1260 Check_For_Premature_Usage (T_Def);
1262 -- The return type and/or any parameter type may be incomplete. Mark the
1263 -- subprogram_type as depending on the incomplete type, so that it can
1264 -- be updated when the full type declaration is seen. This only applies
1265 -- to incomplete types declared in some enclosing scope, not to limited
1266 -- views from other packages.
1268 -- Prior to Ada 2012, access to functions can only have in_parameters.
1270 if Present (Formals) then
1271 Formal := First_Formal (Desig_Type);
1272 while Present (Formal) loop
1273 if Ekind (Formal) /= E_In_Parameter
1274 and then Nkind (T_Def) = N_Access_Function_Definition
1275 and then Ada_Version < Ada_2012
1276 then
1277 Error_Msg_N ("functions can only have IN parameters", Formal);
1278 end if;
1280 if Ekind (Etype (Formal)) = E_Incomplete_Type
1281 and then In_Open_Scopes (Scope (Etype (Formal)))
1282 then
1283 Append_Elmt (Desig_Type, Private_Dependents (Etype (Formal)));
1284 Set_Has_Delayed_Freeze (Desig_Type);
1285 end if;
1287 Next_Formal (Formal);
1288 end loop;
1289 end if;
1291 -- Check whether an indirect call without actuals may be possible. This
1292 -- is used when resolving calls whose result is then indexed.
1294 May_Need_Actuals (Desig_Type);
1296 -- If the return type is incomplete, this is legal as long as the type
1297 -- is declared in the current scope and will be completed in it (rather
1298 -- than being part of limited view).
1300 if Ekind (Etype (Desig_Type)) = E_Incomplete_Type
1301 and then not Has_Delayed_Freeze (Desig_Type)
1302 and then In_Open_Scopes (Scope (Etype (Desig_Type)))
1303 then
1304 Append_Elmt (Desig_Type, Private_Dependents (Etype (Desig_Type)));
1305 Set_Has_Delayed_Freeze (Desig_Type);
1306 end if;
1308 Check_Delayed_Subprogram (Desig_Type);
1310 if Protected_Present (T_Def) then
1311 Set_Ekind (T_Name, E_Access_Protected_Subprogram_Type);
1312 Set_Convention (Desig_Type, Convention_Protected);
1313 else
1314 Set_Ekind (T_Name, E_Access_Subprogram_Type);
1315 end if;
1317 Set_Can_Use_Internal_Rep (T_Name, not Always_Compatible_Rep_On_Target);
1319 Set_Etype (T_Name, T_Name);
1320 Init_Size_Align (T_Name);
1321 Set_Directly_Designated_Type (T_Name, Desig_Type);
1323 Generate_Reference_To_Formals (T_Name);
1325 -- Ada 2005 (AI-231): Propagate the null-excluding attribute
1327 Set_Can_Never_Be_Null (T_Name, Null_Exclusion_Present (T_Def));
1329 Check_Restriction (No_Access_Subprograms, T_Def);
1330 end Access_Subprogram_Declaration;
1332 ----------------------------
1333 -- Access_Type_Declaration --
1334 ----------------------------
1336 procedure Access_Type_Declaration (T : Entity_Id; Def : Node_Id) is
1337 P : constant Node_Id := Parent (Def);
1338 S : constant Node_Id := Subtype_Indication (Def);
1340 Full_Desig : Entity_Id;
1342 begin
1343 Check_SPARK_05_Restriction ("access type is not allowed", Def);
1345 -- Check for permissible use of incomplete type
1347 if Nkind (S) /= N_Subtype_Indication then
1348 Analyze (S);
1350 if Ekind (Root_Type (Entity (S))) = E_Incomplete_Type then
1351 Set_Directly_Designated_Type (T, Entity (S));
1353 -- If the designated type is a limited view, we cannot tell if
1354 -- the full view contains tasks, and there is no way to handle
1355 -- that full view in a client. We create a master entity for the
1356 -- scope, which will be used when a client determines that one
1357 -- is needed.
1359 if From_Limited_With (Entity (S))
1360 and then not Is_Class_Wide_Type (Entity (S))
1361 then
1362 Set_Ekind (T, E_Access_Type);
1363 Build_Master_Entity (T);
1364 Build_Master_Renaming (T);
1365 end if;
1367 else
1368 Set_Directly_Designated_Type (T, Process_Subtype (S, P, T, 'P'));
1369 end if;
1371 -- If the access definition is of the form: ACCESS NOT NULL ..
1372 -- the subtype indication must be of an access type. Create
1373 -- a null-excluding subtype of it.
1375 if Null_Excluding_Subtype (Def) then
1376 if not Is_Access_Type (Entity (S)) then
1377 Error_Msg_N ("null exclusion must apply to access type", Def);
1379 else
1380 declare
1381 Loc : constant Source_Ptr := Sloc (S);
1382 Decl : Node_Id;
1383 Nam : constant Entity_Id := Make_Temporary (Loc, 'S');
1385 begin
1386 Decl :=
1387 Make_Subtype_Declaration (Loc,
1388 Defining_Identifier => Nam,
1389 Subtype_Indication =>
1390 New_Occurrence_Of (Entity (S), Loc));
1391 Set_Null_Exclusion_Present (Decl);
1392 Insert_Before (Parent (Def), Decl);
1393 Analyze (Decl);
1394 Set_Entity (S, Nam);
1395 end;
1396 end if;
1397 end if;
1399 else
1400 Set_Directly_Designated_Type (T,
1401 Process_Subtype (S, P, T, 'P'));
1402 end if;
1404 if All_Present (Def) or Constant_Present (Def) then
1405 Set_Ekind (T, E_General_Access_Type);
1406 else
1407 Set_Ekind (T, E_Access_Type);
1408 end if;
1410 Full_Desig := Designated_Type (T);
1412 if Base_Type (Full_Desig) = T then
1413 Error_Msg_N ("access type cannot designate itself", S);
1415 -- In Ada 2005, the type may have a limited view through some unit in
1416 -- its own context, allowing the following circularity that cannot be
1417 -- detected earlier.
1419 elsif Is_Class_Wide_Type (Full_Desig) and then Etype (Full_Desig) = T
1420 then
1421 Error_Msg_N
1422 ("access type cannot designate its own classwide type", S);
1424 -- Clean up indication of tagged status to prevent cascaded errors
1426 Set_Is_Tagged_Type (T, False);
1427 end if;
1429 Set_Etype (T, T);
1431 -- If the type has appeared already in a with_type clause, it is frozen
1432 -- and the pointer size is already set. Else, initialize.
1434 if not From_Limited_With (T) then
1435 Init_Size_Align (T);
1436 end if;
1438 -- Note that Has_Task is always false, since the access type itself
1439 -- is not a task type. See Einfo for more description on this point.
1440 -- Exactly the same consideration applies to Has_Controlled_Component
1441 -- and to Has_Protected.
1443 Set_Has_Task (T, False);
1444 Set_Has_Controlled_Component (T, False);
1445 Set_Has_Protected (T, False);
1447 -- Initialize field Finalization_Master explicitly to Empty, to avoid
1448 -- problems where an incomplete view of this entity has been previously
1449 -- established by a limited with and an overlaid version of this field
1450 -- (Stored_Constraint) was initialized for the incomplete view.
1452 -- This reset is performed in most cases except where the access type
1453 -- has been created for the purposes of allocating or deallocating a
1454 -- build-in-place object. Such access types have explicitly set pools
1455 -- and finalization masters.
1457 if No (Associated_Storage_Pool (T)) then
1458 Set_Finalization_Master (T, Empty);
1459 end if;
1461 -- Ada 2005 (AI-231): Propagate the null-excluding and access-constant
1462 -- attributes
1464 Set_Can_Never_Be_Null (T, Null_Exclusion_Present (Def));
1465 Set_Is_Access_Constant (T, Constant_Present (Def));
1466 end Access_Type_Declaration;
1468 ----------------------------------
1469 -- Add_Interface_Tag_Components --
1470 ----------------------------------
1472 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id) is
1473 Loc : constant Source_Ptr := Sloc (N);
1474 L : List_Id;
1475 Last_Tag : Node_Id;
1477 procedure Add_Tag (Iface : Entity_Id);
1478 -- Add tag for one of the progenitor interfaces
1480 -------------
1481 -- Add_Tag --
1482 -------------
1484 procedure Add_Tag (Iface : Entity_Id) is
1485 Decl : Node_Id;
1486 Def : Node_Id;
1487 Tag : Entity_Id;
1488 Offset : Entity_Id;
1490 begin
1491 pragma Assert (Is_Tagged_Type (Iface) and then Is_Interface (Iface));
1493 -- This is a reasonable place to propagate predicates
1495 if Has_Predicates (Iface) then
1496 Set_Has_Predicates (Typ);
1497 end if;
1499 Def :=
1500 Make_Component_Definition (Loc,
1501 Aliased_Present => True,
1502 Subtype_Indication =>
1503 New_Occurrence_Of (RTE (RE_Interface_Tag), Loc));
1505 Tag := Make_Temporary (Loc, 'V');
1507 Decl :=
1508 Make_Component_Declaration (Loc,
1509 Defining_Identifier => Tag,
1510 Component_Definition => Def);
1512 Analyze_Component_Declaration (Decl);
1514 Set_Analyzed (Decl);
1515 Set_Ekind (Tag, E_Component);
1516 Set_Is_Tag (Tag);
1517 Set_Is_Aliased (Tag);
1518 Set_Related_Type (Tag, Iface);
1519 Init_Component_Location (Tag);
1521 pragma Assert (Is_Frozen (Iface));
1523 Set_DT_Entry_Count (Tag,
1524 DT_Entry_Count (First_Entity (Iface)));
1526 if No (Last_Tag) then
1527 Prepend (Decl, L);
1528 else
1529 Insert_After (Last_Tag, Decl);
1530 end if;
1532 Last_Tag := Decl;
1534 -- If the ancestor has discriminants we need to give special support
1535 -- to store the offset_to_top value of the secondary dispatch tables.
1536 -- For this purpose we add a supplementary component just after the
1537 -- field that contains the tag associated with each secondary DT.
1539 if Typ /= Etype (Typ) and then Has_Discriminants (Etype (Typ)) then
1540 Def :=
1541 Make_Component_Definition (Loc,
1542 Subtype_Indication =>
1543 New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
1545 Offset := Make_Temporary (Loc, 'V');
1547 Decl :=
1548 Make_Component_Declaration (Loc,
1549 Defining_Identifier => Offset,
1550 Component_Definition => Def);
1552 Analyze_Component_Declaration (Decl);
1554 Set_Analyzed (Decl);
1555 Set_Ekind (Offset, E_Component);
1556 Set_Is_Aliased (Offset);
1557 Set_Related_Type (Offset, Iface);
1558 Init_Component_Location (Offset);
1559 Insert_After (Last_Tag, Decl);
1560 Last_Tag := Decl;
1561 end if;
1562 end Add_Tag;
1564 -- Local variables
1566 Elmt : Elmt_Id;
1567 Ext : Node_Id;
1568 Comp : Node_Id;
1570 -- Start of processing for Add_Interface_Tag_Components
1572 begin
1573 if not RTE_Available (RE_Interface_Tag) then
1574 Error_Msg
1575 ("(Ada 2005) interface types not supported by this run-time!",
1576 Sloc (N));
1577 return;
1578 end if;
1580 if Ekind (Typ) /= E_Record_Type
1581 or else (Is_Concurrent_Record_Type (Typ)
1582 and then Is_Empty_List (Abstract_Interface_List (Typ)))
1583 or else (not Is_Concurrent_Record_Type (Typ)
1584 and then No (Interfaces (Typ))
1585 and then Is_Empty_Elmt_List (Interfaces (Typ)))
1586 then
1587 return;
1588 end if;
1590 -- Find the current last tag
1592 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1593 Ext := Record_Extension_Part (Type_Definition (N));
1594 else
1595 pragma Assert (Nkind (Type_Definition (N)) = N_Record_Definition);
1596 Ext := Type_Definition (N);
1597 end if;
1599 Last_Tag := Empty;
1601 if not (Present (Component_List (Ext))) then
1602 Set_Null_Present (Ext, False);
1603 L := New_List;
1604 Set_Component_List (Ext,
1605 Make_Component_List (Loc,
1606 Component_Items => L,
1607 Null_Present => False));
1608 else
1609 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1610 L := Component_Items
1611 (Component_List
1612 (Record_Extension_Part
1613 (Type_Definition (N))));
1614 else
1615 L := Component_Items
1616 (Component_List
1617 (Type_Definition (N)));
1618 end if;
1620 -- Find the last tag component
1622 Comp := First (L);
1623 while Present (Comp) loop
1624 if Nkind (Comp) = N_Component_Declaration
1625 and then Is_Tag (Defining_Identifier (Comp))
1626 then
1627 Last_Tag := Comp;
1628 end if;
1630 Next (Comp);
1631 end loop;
1632 end if;
1634 -- At this point L references the list of components and Last_Tag
1635 -- references the current last tag (if any). Now we add the tag
1636 -- corresponding with all the interfaces that are not implemented
1637 -- by the parent.
1639 if Present (Interfaces (Typ)) then
1640 Elmt := First_Elmt (Interfaces (Typ));
1641 while Present (Elmt) loop
1642 Add_Tag (Node (Elmt));
1643 Next_Elmt (Elmt);
1644 end loop;
1645 end if;
1646 end Add_Interface_Tag_Components;
1648 -------------------------------------
1649 -- Add_Internal_Interface_Entities --
1650 -------------------------------------
1652 procedure Add_Internal_Interface_Entities (Tagged_Type : Entity_Id) is
1653 Elmt : Elmt_Id;
1654 Iface : Entity_Id;
1655 Iface_Elmt : Elmt_Id;
1656 Iface_Prim : Entity_Id;
1657 Ifaces_List : Elist_Id;
1658 New_Subp : Entity_Id := Empty;
1659 Prim : Entity_Id;
1660 Restore_Scope : Boolean := False;
1662 begin
1663 pragma Assert (Ada_Version >= Ada_2005
1664 and then Is_Record_Type (Tagged_Type)
1665 and then Is_Tagged_Type (Tagged_Type)
1666 and then Has_Interfaces (Tagged_Type)
1667 and then not Is_Interface (Tagged_Type));
1669 -- Ensure that the internal entities are added to the scope of the type
1671 if Scope (Tagged_Type) /= Current_Scope then
1672 Push_Scope (Scope (Tagged_Type));
1673 Restore_Scope := True;
1674 end if;
1676 Collect_Interfaces (Tagged_Type, Ifaces_List);
1678 Iface_Elmt := First_Elmt (Ifaces_List);
1679 while Present (Iface_Elmt) loop
1680 Iface := Node (Iface_Elmt);
1682 -- Originally we excluded here from this processing interfaces that
1683 -- are parents of Tagged_Type because their primitives are located
1684 -- in the primary dispatch table (and hence no auxiliary internal
1685 -- entities are required to handle secondary dispatch tables in such
1686 -- case). However, these auxiliary entities are also required to
1687 -- handle derivations of interfaces in formals of generics (see
1688 -- Derive_Subprograms).
1690 Elmt := First_Elmt (Primitive_Operations (Iface));
1691 while Present (Elmt) loop
1692 Iface_Prim := Node (Elmt);
1694 if not Is_Predefined_Dispatching_Operation (Iface_Prim) then
1695 Prim :=
1696 Find_Primitive_Covering_Interface
1697 (Tagged_Type => Tagged_Type,
1698 Iface_Prim => Iface_Prim);
1700 if No (Prim) and then Serious_Errors_Detected > 0 then
1701 goto Continue;
1702 end if;
1704 pragma Assert (Present (Prim));
1706 -- Ada 2012 (AI05-0197): If the name of the covering primitive
1707 -- differs from the name of the interface primitive then it is
1708 -- a private primitive inherited from a parent type. In such
1709 -- case, given that Tagged_Type covers the interface, the
1710 -- inherited private primitive becomes visible. For such
1711 -- purpose we add a new entity that renames the inherited
1712 -- private primitive.
1714 if Chars (Prim) /= Chars (Iface_Prim) then
1715 pragma Assert (Has_Suffix (Prim, 'P'));
1716 Derive_Subprogram
1717 (New_Subp => New_Subp,
1718 Parent_Subp => Iface_Prim,
1719 Derived_Type => Tagged_Type,
1720 Parent_Type => Iface);
1721 Set_Alias (New_Subp, Prim);
1722 Set_Is_Abstract_Subprogram
1723 (New_Subp, Is_Abstract_Subprogram (Prim));
1724 end if;
1726 Derive_Subprogram
1727 (New_Subp => New_Subp,
1728 Parent_Subp => Iface_Prim,
1729 Derived_Type => Tagged_Type,
1730 Parent_Type => Iface);
1732 -- Ada 2005 (AI-251): Decorate internal entity Iface_Subp
1733 -- associated with interface types. These entities are
1734 -- only registered in the list of primitives of its
1735 -- corresponding tagged type because they are only used
1736 -- to fill the contents of the secondary dispatch tables.
1737 -- Therefore they are removed from the homonym chains.
1739 Set_Is_Hidden (New_Subp);
1740 Set_Is_Internal (New_Subp);
1741 Set_Alias (New_Subp, Prim);
1742 Set_Is_Abstract_Subprogram
1743 (New_Subp, Is_Abstract_Subprogram (Prim));
1744 Set_Interface_Alias (New_Subp, Iface_Prim);
1746 -- If the returned type is an interface then propagate it to
1747 -- the returned type. Needed by the thunk to generate the code
1748 -- which displaces "this" to reference the corresponding
1749 -- secondary dispatch table in the returned object.
1751 if Is_Interface (Etype (Iface_Prim)) then
1752 Set_Etype (New_Subp, Etype (Iface_Prim));
1753 end if;
1755 -- Internal entities associated with interface types are
1756 -- only registered in the list of primitives of the tagged
1757 -- type. They are only used to fill the contents of the
1758 -- secondary dispatch tables. Therefore they are not needed
1759 -- in the homonym chains.
1761 Remove_Homonym (New_Subp);
1763 -- Hidden entities associated with interfaces must have set
1764 -- the Has_Delay_Freeze attribute to ensure that, in case of
1765 -- locally defined tagged types (or compiling with static
1766 -- dispatch tables generation disabled) the corresponding
1767 -- entry of the secondary dispatch table is filled when
1768 -- such an entity is frozen.
1770 Set_Has_Delayed_Freeze (New_Subp);
1771 end if;
1773 <<Continue>>
1774 Next_Elmt (Elmt);
1775 end loop;
1777 Next_Elmt (Iface_Elmt);
1778 end loop;
1780 if Restore_Scope then
1781 Pop_Scope;
1782 end if;
1783 end Add_Internal_Interface_Entities;
1785 -----------------------------------
1786 -- Analyze_Component_Declaration --
1787 -----------------------------------
1789 procedure Analyze_Component_Declaration (N : Node_Id) is
1790 Id : constant Entity_Id := Defining_Identifier (N);
1791 E : constant Node_Id := Expression (N);
1792 Typ : constant Node_Id :=
1793 Subtype_Indication (Component_Definition (N));
1794 T : Entity_Id;
1795 P : Entity_Id;
1797 function Contains_POC (Constr : Node_Id) return Boolean;
1798 -- Determines whether a constraint uses the discriminant of a record
1799 -- type thus becoming a per-object constraint (POC).
1801 function Is_Known_Limited (Typ : Entity_Id) return Boolean;
1802 -- Typ is the type of the current component, check whether this type is
1803 -- a limited type. Used to validate declaration against that of
1804 -- enclosing record.
1806 ------------------
1807 -- Contains_POC --
1808 ------------------
1810 function Contains_POC (Constr : Node_Id) return Boolean is
1811 begin
1812 -- Prevent cascaded errors
1814 if Error_Posted (Constr) then
1815 return False;
1816 end if;
1818 case Nkind (Constr) is
1819 when N_Attribute_Reference =>
1820 return Attribute_Name (Constr) = Name_Access
1821 and then Prefix (Constr) = Scope (Entity (Prefix (Constr)));
1823 when N_Discriminant_Association =>
1824 return Denotes_Discriminant (Expression (Constr));
1826 when N_Identifier =>
1827 return Denotes_Discriminant (Constr);
1829 when N_Index_Or_Discriminant_Constraint =>
1830 declare
1831 IDC : Node_Id;
1833 begin
1834 IDC := First (Constraints (Constr));
1835 while Present (IDC) loop
1837 -- One per-object constraint is sufficient
1839 if Contains_POC (IDC) then
1840 return True;
1841 end if;
1843 Next (IDC);
1844 end loop;
1846 return False;
1847 end;
1849 when N_Range =>
1850 return Denotes_Discriminant (Low_Bound (Constr))
1851 or else
1852 Denotes_Discriminant (High_Bound (Constr));
1854 when N_Range_Constraint =>
1855 return Denotes_Discriminant (Range_Expression (Constr));
1857 when others =>
1858 return False;
1860 end case;
1861 end Contains_POC;
1863 ----------------------
1864 -- Is_Known_Limited --
1865 ----------------------
1867 function Is_Known_Limited (Typ : Entity_Id) return Boolean is
1868 P : constant Entity_Id := Etype (Typ);
1869 R : constant Entity_Id := Root_Type (Typ);
1871 begin
1872 if Is_Limited_Record (Typ) then
1873 return True;
1875 -- If the root type is limited (and not a limited interface)
1876 -- so is the current type
1878 elsif Is_Limited_Record (R)
1879 and then (not Is_Interface (R) or else not Is_Limited_Interface (R))
1880 then
1881 return True;
1883 -- Else the type may have a limited interface progenitor, but a
1884 -- limited record parent.
1886 elsif R /= P and then Is_Limited_Record (P) then
1887 return True;
1889 else
1890 return False;
1891 end if;
1892 end Is_Known_Limited;
1894 -- Start of processing for Analyze_Component_Declaration
1896 begin
1897 Generate_Definition (Id);
1898 Enter_Name (Id);
1900 if Present (Typ) then
1901 T := Find_Type_Of_Object
1902 (Subtype_Indication (Component_Definition (N)), N);
1904 if not Nkind_In (Typ, N_Identifier, N_Expanded_Name) then
1905 Check_SPARK_05_Restriction ("subtype mark required", Typ);
1906 end if;
1908 -- Ada 2005 (AI-230): Access Definition case
1910 else
1911 pragma Assert (Present
1912 (Access_Definition (Component_Definition (N))));
1914 T := Access_Definition
1915 (Related_Nod => N,
1916 N => Access_Definition (Component_Definition (N)));
1917 Set_Is_Local_Anonymous_Access (T);
1919 -- Ada 2005 (AI-254)
1921 if Present (Access_To_Subprogram_Definition
1922 (Access_Definition (Component_Definition (N))))
1923 and then Protected_Present (Access_To_Subprogram_Definition
1924 (Access_Definition
1925 (Component_Definition (N))))
1926 then
1927 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1928 end if;
1929 end if;
1931 -- If the subtype is a constrained subtype of the enclosing record,
1932 -- (which must have a partial view) the back-end does not properly
1933 -- handle the recursion. Rewrite the component declaration with an
1934 -- explicit subtype indication, which is acceptable to Gigi. We can copy
1935 -- the tree directly because side effects have already been removed from
1936 -- discriminant constraints.
1938 if Ekind (T) = E_Access_Subtype
1939 and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
1940 and then Comes_From_Source (T)
1941 and then Nkind (Parent (T)) = N_Subtype_Declaration
1942 and then Etype (Directly_Designated_Type (T)) = Current_Scope
1943 then
1944 Rewrite
1945 (Subtype_Indication (Component_Definition (N)),
1946 New_Copy_Tree (Subtype_Indication (Parent (T))));
1947 T := Find_Type_Of_Object
1948 (Subtype_Indication (Component_Definition (N)), N);
1949 end if;
1951 -- If the component declaration includes a default expression, then we
1952 -- check that the component is not of a limited type (RM 3.7(5)),
1953 -- and do the special preanalysis of the expression (see section on
1954 -- "Handling of Default and Per-Object Expressions" in the spec of
1955 -- package Sem).
1957 if Present (E) then
1958 Check_SPARK_05_Restriction ("default expression is not allowed", E);
1959 Preanalyze_Default_Expression (E, T);
1960 Check_Initialization (T, E);
1962 if Ada_Version >= Ada_2005
1963 and then Ekind (T) = E_Anonymous_Access_Type
1964 and then Etype (E) /= Any_Type
1965 then
1966 -- Check RM 3.9.2(9): "if the expected type for an expression is
1967 -- an anonymous access-to-specific tagged type, then the object
1968 -- designated by the expression shall not be dynamically tagged
1969 -- unless it is a controlling operand in a call on a dispatching
1970 -- operation"
1972 if Is_Tagged_Type (Directly_Designated_Type (T))
1973 and then
1974 Ekind (Directly_Designated_Type (T)) /= E_Class_Wide_Type
1975 and then
1976 Ekind (Directly_Designated_Type (Etype (E))) =
1977 E_Class_Wide_Type
1978 then
1979 Error_Msg_N
1980 ("access to specific tagged type required (RM 3.9.2(9))", E);
1981 end if;
1983 -- (Ada 2005: AI-230): Accessibility check for anonymous
1984 -- components
1986 if Type_Access_Level (Etype (E)) >
1987 Deepest_Type_Access_Level (T)
1988 then
1989 Error_Msg_N
1990 ("expression has deeper access level than component " &
1991 "(RM 3.10.2 (12.2))", E);
1992 end if;
1994 -- The initialization expression is a reference to an access
1995 -- discriminant. The type of the discriminant is always deeper
1996 -- than any access type.
1998 if Ekind (Etype (E)) = E_Anonymous_Access_Type
1999 and then Is_Entity_Name (E)
2000 and then Ekind (Entity (E)) = E_In_Parameter
2001 and then Present (Discriminal_Link (Entity (E)))
2002 then
2003 Error_Msg_N
2004 ("discriminant has deeper accessibility level than target",
2006 end if;
2007 end if;
2008 end if;
2010 -- The parent type may be a private view with unknown discriminants,
2011 -- and thus unconstrained. Regular components must be constrained.
2013 if Is_Indefinite_Subtype (T) and then Chars (Id) /= Name_uParent then
2014 if Is_Class_Wide_Type (T) then
2015 Error_Msg_N
2016 ("class-wide subtype with unknown discriminants" &
2017 " in component declaration",
2018 Subtype_Indication (Component_Definition (N)));
2019 else
2020 Error_Msg_N
2021 ("unconstrained subtype in component declaration",
2022 Subtype_Indication (Component_Definition (N)));
2023 end if;
2025 -- Components cannot be abstract, except for the special case of
2026 -- the _Parent field (case of extending an abstract tagged type)
2028 elsif Is_Abstract_Type (T) and then Chars (Id) /= Name_uParent then
2029 Error_Msg_N ("type of a component cannot be abstract", N);
2030 end if;
2032 Set_Etype (Id, T);
2033 Set_Is_Aliased (Id, Aliased_Present (Component_Definition (N)));
2035 -- The component declaration may have a per-object constraint, set
2036 -- the appropriate flag in the defining identifier of the subtype.
2038 if Present (Subtype_Indication (Component_Definition (N))) then
2039 declare
2040 Sindic : constant Node_Id :=
2041 Subtype_Indication (Component_Definition (N));
2042 begin
2043 if Nkind (Sindic) = N_Subtype_Indication
2044 and then Present (Constraint (Sindic))
2045 and then Contains_POC (Constraint (Sindic))
2046 then
2047 Set_Has_Per_Object_Constraint (Id);
2048 end if;
2049 end;
2050 end if;
2052 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
2053 -- out some static checks.
2055 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (T) then
2056 Null_Exclusion_Static_Checks (N);
2057 end if;
2059 -- If this component is private (or depends on a private type), flag the
2060 -- record type to indicate that some operations are not available.
2062 P := Private_Component (T);
2064 if Present (P) then
2066 -- Check for circular definitions
2068 if P = Any_Type then
2069 Set_Etype (Id, Any_Type);
2071 -- There is a gap in the visibility of operations only if the
2072 -- component type is not defined in the scope of the record type.
2074 elsif Scope (P) = Scope (Current_Scope) then
2075 null;
2077 elsif Is_Limited_Type (P) then
2078 Set_Is_Limited_Composite (Current_Scope);
2080 else
2081 Set_Is_Private_Composite (Current_Scope);
2082 end if;
2083 end if;
2085 if P /= Any_Type
2086 and then Is_Limited_Type (T)
2087 and then Chars (Id) /= Name_uParent
2088 and then Is_Tagged_Type (Current_Scope)
2089 then
2090 if Is_Derived_Type (Current_Scope)
2091 and then not Is_Known_Limited (Current_Scope)
2092 then
2093 Error_Msg_N
2094 ("extension of nonlimited type cannot have limited components",
2097 if Is_Interface (Root_Type (Current_Scope)) then
2098 Error_Msg_N
2099 ("\limitedness is not inherited from limited interface", N);
2100 Error_Msg_N ("\add LIMITED to type indication", N);
2101 end if;
2103 Explain_Limited_Type (T, N);
2104 Set_Etype (Id, Any_Type);
2105 Set_Is_Limited_Composite (Current_Scope, False);
2107 elsif not Is_Derived_Type (Current_Scope)
2108 and then not Is_Limited_Record (Current_Scope)
2109 and then not Is_Concurrent_Type (Current_Scope)
2110 then
2111 Error_Msg_N
2112 ("nonlimited tagged type cannot have limited components", N);
2113 Explain_Limited_Type (T, N);
2114 Set_Etype (Id, Any_Type);
2115 Set_Is_Limited_Composite (Current_Scope, False);
2116 end if;
2117 end if;
2119 Set_Original_Record_Component (Id, Id);
2121 if Has_Aspects (N) then
2122 Analyze_Aspect_Specifications (N, Id);
2123 end if;
2125 Analyze_Dimension (N);
2126 end Analyze_Component_Declaration;
2128 --------------------------
2129 -- Analyze_Declarations --
2130 --------------------------
2132 procedure Analyze_Declarations (L : List_Id) is
2133 Decl : Node_Id;
2135 procedure Adjust_Decl;
2136 -- Adjust Decl not to include implicit label declarations, since these
2137 -- have strange Sloc values that result in elaboration check problems.
2138 -- (They have the sloc of the label as found in the source, and that
2139 -- is ahead of the current declarative part).
2141 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id);
2142 -- Determine whether Body_Decl denotes the body of a late controlled
2143 -- primitive (either Initialize, Adjust or Finalize). If this is the
2144 -- case, add a proper spec if the body lacks one. The spec is inserted
2145 -- before Body_Decl and immedately analyzed.
2147 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id);
2148 -- Spec_Id is the entity of a package that may define abstract states.
2149 -- If the states have visible refinement, remove the visibility of each
2150 -- constituent at the end of the package body declarations.
2152 -----------------
2153 -- Adjust_Decl --
2154 -----------------
2156 procedure Adjust_Decl is
2157 begin
2158 while Present (Prev (Decl))
2159 and then Nkind (Decl) = N_Implicit_Label_Declaration
2160 loop
2161 Prev (Decl);
2162 end loop;
2163 end Adjust_Decl;
2165 --------------------------------------
2166 -- Handle_Late_Controlled_Primitive --
2167 --------------------------------------
2169 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id) is
2170 Body_Spec : constant Node_Id := Specification (Body_Decl);
2171 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2172 Loc : constant Source_Ptr := Sloc (Body_Id);
2173 Params : constant List_Id :=
2174 Parameter_Specifications (Body_Spec);
2175 Spec : Node_Id;
2176 Spec_Id : Entity_Id;
2177 Typ : Node_Id;
2179 begin
2180 -- Consider only procedure bodies whose name matches one of the three
2181 -- controlled primitives.
2183 if Nkind (Body_Spec) /= N_Procedure_Specification
2184 or else not Nam_In (Chars (Body_Id), Name_Adjust,
2185 Name_Finalize,
2186 Name_Initialize)
2187 then
2188 return;
2190 -- A controlled primitive must have exactly one formal which is not
2191 -- an anonymous access type.
2193 elsif List_Length (Params) /= 1 then
2194 return;
2195 end if;
2197 Typ := Parameter_Type (First (Params));
2199 if Nkind (Typ) = N_Access_Definition then
2200 return;
2201 end if;
2203 Find_Type (Typ);
2205 -- The type of the formal must be derived from [Limited_]Controlled
2207 if not Is_Controlled (Entity (Typ)) then
2208 return;
2209 end if;
2211 -- Check whether a specification exists for this body. We do not
2212 -- analyze the spec of the body in full, because it will be analyzed
2213 -- again when the body is properly analyzed, and we cannot create
2214 -- duplicate entries in the formals chain. We look for an explicit
2215 -- specification because the body may be an overriding operation and
2216 -- an inherited spec may be present.
2218 Spec_Id := Current_Entity (Body_Id);
2220 while Present (Spec_Id) loop
2221 if Ekind_In (Spec_Id, E_Procedure, E_Generic_Procedure)
2222 and then Scope (Spec_Id) = Current_Scope
2223 and then Present (First_Formal (Spec_Id))
2224 and then No (Next_Formal (First_Formal (Spec_Id)))
2225 and then Etype (First_Formal (Spec_Id)) = Entity (Typ)
2226 and then Comes_From_Source (Spec_Id)
2227 then
2228 return;
2229 end if;
2231 Spec_Id := Homonym (Spec_Id);
2232 end loop;
2234 -- At this point the body is known to be a late controlled primitive.
2235 -- Generate a matching spec and insert it before the body. Note the
2236 -- use of Copy_Separate_Tree - we want an entirely separate semantic
2237 -- tree in this case.
2239 Spec := Copy_Separate_Tree (Body_Spec);
2241 -- Ensure that the subprogram declaration does not inherit the null
2242 -- indicator from the body as we now have a proper spec/body pair.
2244 Set_Null_Present (Spec, False);
2246 Insert_Before_And_Analyze (Body_Decl,
2247 Make_Subprogram_Declaration (Loc, Specification => Spec));
2248 end Handle_Late_Controlled_Primitive;
2250 --------------------------------
2251 -- Remove_Visible_Refinements --
2252 --------------------------------
2254 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id) is
2255 State_Elmt : Elmt_Id;
2256 begin
2257 if Present (Abstract_States (Spec_Id)) then
2258 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2259 while Present (State_Elmt) loop
2260 Set_Has_Visible_Refinement (Node (State_Elmt), False);
2261 Next_Elmt (State_Elmt);
2262 end loop;
2263 end if;
2264 end Remove_Visible_Refinements;
2266 -- Local variables
2268 Context : Node_Id;
2269 Freeze_From : Entity_Id := Empty;
2270 Next_Decl : Node_Id;
2271 Spec_Id : Entity_Id;
2273 Body_Seen : Boolean := False;
2274 -- Flag set when the first body [stub] is encountered
2276 In_Package_Body : Boolean := False;
2277 -- Flag set when the current declaration list belongs to a package body
2279 -- Start of processing for Analyze_Declarations
2281 begin
2282 if Restriction_Check_Required (SPARK_05) then
2283 Check_Later_Vs_Basic_Declarations (L, During_Parsing => False);
2284 end if;
2286 Decl := First (L);
2287 while Present (Decl) loop
2289 -- Package spec cannot contain a package declaration in SPARK
2291 if Nkind (Decl) = N_Package_Declaration
2292 and then Nkind (Parent (L)) = N_Package_Specification
2293 then
2294 Check_SPARK_05_Restriction
2295 ("package specification cannot contain a package declaration",
2296 Decl);
2297 end if;
2299 -- Complete analysis of declaration
2301 Analyze (Decl);
2302 Next_Decl := Next (Decl);
2304 if No (Freeze_From) then
2305 Freeze_From := First_Entity (Current_Scope);
2306 end if;
2308 -- At the end of a declarative part, freeze remaining entities
2309 -- declared in it. The end of the visible declarations of package
2310 -- specification is not the end of a declarative part if private
2311 -- declarations are present. The end of a package declaration is a
2312 -- freezing point only if it a library package. A task definition or
2313 -- protected type definition is not a freeze point either. Finally,
2314 -- we do not freeze entities in generic scopes, because there is no
2315 -- code generated for them and freeze nodes will be generated for
2316 -- the instance.
2318 -- The end of a package instantiation is not a freeze point, but
2319 -- for now we make it one, because the generic body is inserted
2320 -- (currently) immediately after. Generic instantiations will not
2321 -- be a freeze point once delayed freezing of bodies is implemented.
2322 -- (This is needed in any case for early instantiations ???).
2324 if No (Next_Decl) then
2325 if Nkind_In (Parent (L), N_Component_List,
2326 N_Task_Definition,
2327 N_Protected_Definition)
2328 then
2329 null;
2331 elsif Nkind (Parent (L)) /= N_Package_Specification then
2332 if Nkind (Parent (L)) = N_Package_Body then
2333 Freeze_From := First_Entity (Current_Scope);
2334 end if;
2336 -- There may have been several freezing points previously,
2337 -- for example object declarations or subprogram bodies, but
2338 -- at the end of a declarative part we check freezing from
2339 -- the beginning, even though entities may already be frozen,
2340 -- in order to perform visibility checks on delayed aspects.
2342 Adjust_Decl;
2343 Freeze_All (First_Entity (Current_Scope), Decl);
2344 Freeze_From := Last_Entity (Current_Scope);
2346 elsif Scope (Current_Scope) /= Standard_Standard
2347 and then not Is_Child_Unit (Current_Scope)
2348 and then No (Generic_Parent (Parent (L)))
2349 then
2350 null;
2352 elsif L /= Visible_Declarations (Parent (L))
2353 or else No (Private_Declarations (Parent (L)))
2354 or else Is_Empty_List (Private_Declarations (Parent (L)))
2355 then
2356 Adjust_Decl;
2357 Freeze_All (First_Entity (Current_Scope), Decl);
2358 Freeze_From := Last_Entity (Current_Scope);
2359 end if;
2361 -- If next node is a body then freeze all types before the body.
2362 -- An exception occurs for some expander-generated bodies. If these
2363 -- are generated at places where in general language rules would not
2364 -- allow a freeze point, then we assume that the expander has
2365 -- explicitly checked that all required types are properly frozen,
2366 -- and we do not cause general freezing here. This special circuit
2367 -- is used when the encountered body is marked as having already
2368 -- been analyzed.
2370 -- In all other cases (bodies that come from source, and expander
2371 -- generated bodies that have not been analyzed yet), freeze all
2372 -- types now. Note that in the latter case, the expander must take
2373 -- care to attach the bodies at a proper place in the tree so as to
2374 -- not cause unwanted freezing at that point.
2376 elsif not Analyzed (Next_Decl) and then Is_Body (Next_Decl) then
2378 -- When a controlled type is frozen, the expander generates stream
2379 -- and controlled type support routines. If the freeze is caused
2380 -- by the stand alone body of Initialize, Adjust and Finalize, the
2381 -- expander will end up using the wrong version of these routines
2382 -- as the body has not been processed yet. To remedy this, detect
2383 -- a late controlled primitive and create a proper spec for it.
2384 -- This ensures that the primitive will override its inherited
2385 -- counterpart before the freeze takes place.
2387 -- If the declaration we just processed is a body, do not attempt
2388 -- to examine Next_Decl as the late primitive idiom can only apply
2389 -- to the first encountered body.
2391 -- The spec of the late primitive is not generated in ASIS mode to
2392 -- ensure a consistent list of primitives that indicates the true
2393 -- semantic structure of the program (which is not relevant when
2394 -- generating executable code.
2396 -- ??? a cleaner approach may be possible and/or this solution
2397 -- could be extended to general-purpose late primitives, TBD.
2399 if not ASIS_Mode and then not Body_Seen and then not Is_Body (Decl)
2400 then
2401 Body_Seen := True;
2403 if Nkind (Next_Decl) = N_Subprogram_Body then
2404 Handle_Late_Controlled_Primitive (Next_Decl);
2405 end if;
2406 end if;
2408 Adjust_Decl;
2409 Freeze_All (Freeze_From, Decl);
2410 Freeze_From := Last_Entity (Current_Scope);
2411 end if;
2413 Decl := Next_Decl;
2414 end loop;
2416 -- Analyze the contracts of packages and their bodies
2418 if Present (L) then
2419 Context := Parent (L);
2421 if Nkind (Context) = N_Package_Specification then
2423 -- When a package has private declarations, its contract must be
2424 -- analyzed at the end of the said declarations. This way both the
2425 -- analysis and freeze actions are properly synchronized in case
2426 -- of private type use within the contract.
2428 if L = Private_Declarations (Context) then
2429 Analyze_Package_Contract (Defining_Entity (Context));
2431 -- Build the bodies of the default initial condition procedures
2432 -- for all types subject to pragma Default_Initial_Condition.
2433 -- From a purely Ada stand point, this is a freezing activity,
2434 -- however freezing is not available under GNATprove_Mode. To
2435 -- accomodate both scenarios, the bodies are build at the end
2436 -- of private declaration analysis.
2438 Build_Default_Init_Cond_Procedure_Bodies (L);
2440 -- Otherwise the contract is analyzed at the end of the visible
2441 -- declarations.
2443 elsif L = Visible_Declarations (Context)
2444 and then No (Private_Declarations (Context))
2445 then
2446 Analyze_Package_Contract (Defining_Entity (Context));
2447 end if;
2449 elsif Nkind (Context) = N_Package_Body then
2450 In_Package_Body := True;
2451 Spec_Id := Corresponding_Spec (Context);
2453 Analyze_Package_Body_Contract (Defining_Entity (Context));
2454 end if;
2455 end if;
2457 -- Analyze the contracts of subprogram declarations, subprogram bodies
2458 -- and variables now due to the delayed visibility requirements of their
2459 -- aspects.
2461 Decl := First (L);
2462 while Present (Decl) loop
2463 if Nkind (Decl) = N_Object_Declaration then
2464 Analyze_Object_Contract (Defining_Entity (Decl));
2466 elsif Nkind_In (Decl, N_Abstract_Subprogram_Declaration,
2467 N_Subprogram_Declaration)
2468 then
2469 Analyze_Subprogram_Contract (Defining_Entity (Decl));
2471 elsif Nkind (Decl) = N_Subprogram_Body then
2472 Analyze_Subprogram_Body_Contract (Defining_Entity (Decl));
2474 elsif Nkind (Decl) = N_Subprogram_Body_Stub then
2475 Analyze_Subprogram_Body_Stub_Contract (Defining_Entity (Decl));
2476 end if;
2478 Next (Decl);
2479 end loop;
2481 -- State refinements are visible upto the end the of the package body
2482 -- declarations. Hide the refinements from visibility to restore the
2483 -- original state conditions.
2485 if In_Package_Body then
2486 Remove_Visible_Refinements (Spec_Id);
2487 end if;
2488 end Analyze_Declarations;
2490 -----------------------------------
2491 -- Analyze_Full_Type_Declaration --
2492 -----------------------------------
2494 procedure Analyze_Full_Type_Declaration (N : Node_Id) is
2495 Def : constant Node_Id := Type_Definition (N);
2496 Def_Id : constant Entity_Id := Defining_Identifier (N);
2497 T : Entity_Id;
2498 Prev : Entity_Id;
2500 Is_Remote : constant Boolean :=
2501 (Is_Remote_Types (Current_Scope)
2502 or else Is_Remote_Call_Interface (Current_Scope))
2503 and then not (In_Private_Part (Current_Scope)
2504 or else In_Package_Body (Current_Scope));
2506 procedure Check_Ops_From_Incomplete_Type;
2507 -- If there is a tagged incomplete partial view of the type, traverse
2508 -- the primitives of the incomplete view and change the type of any
2509 -- controlling formals and result to indicate the full view. The
2510 -- primitives will be added to the full type's primitive operations
2511 -- list later in Sem_Disp.Check_Operation_From_Incomplete_Type (which
2512 -- is called from Process_Incomplete_Dependents).
2514 ------------------------------------
2515 -- Check_Ops_From_Incomplete_Type --
2516 ------------------------------------
2518 procedure Check_Ops_From_Incomplete_Type is
2519 Elmt : Elmt_Id;
2520 Formal : Entity_Id;
2521 Op : Entity_Id;
2523 begin
2524 if Prev /= T
2525 and then Ekind (Prev) = E_Incomplete_Type
2526 and then Is_Tagged_Type (Prev)
2527 and then Is_Tagged_Type (T)
2528 then
2529 Elmt := First_Elmt (Primitive_Operations (Prev));
2530 while Present (Elmt) loop
2531 Op := Node (Elmt);
2533 Formal := First_Formal (Op);
2534 while Present (Formal) loop
2535 if Etype (Formal) = Prev then
2536 Set_Etype (Formal, T);
2537 end if;
2539 Next_Formal (Formal);
2540 end loop;
2542 if Etype (Op) = Prev then
2543 Set_Etype (Op, T);
2544 end if;
2546 Next_Elmt (Elmt);
2547 end loop;
2548 end if;
2549 end Check_Ops_From_Incomplete_Type;
2551 -- Start of processing for Analyze_Full_Type_Declaration
2553 begin
2554 Prev := Find_Type_Name (N);
2556 -- The type declaration may be subject to pragma Ghost with policy
2557 -- Ignore. Set the mode now to ensure that any nodes generated during
2558 -- analysis and expansion are properly flagged as ignored Ghost.
2560 Set_Ghost_Mode (N, Prev);
2562 -- The full view, if present, now points to the current type. If there
2563 -- is an incomplete partial view, set a link to it, to simplify the
2564 -- retrieval of primitive operations of the type.
2566 -- Ada 2005 (AI-50217): If the type was previously decorated when
2567 -- imported through a LIMITED WITH clause, it appears as incomplete
2568 -- but has no full view.
2570 if Ekind (Prev) = E_Incomplete_Type
2571 and then Present (Full_View (Prev))
2572 then
2573 T := Full_View (Prev);
2574 Set_Incomplete_View (N, Parent (Prev));
2575 else
2576 T := Prev;
2577 end if;
2579 Set_Is_Pure (T, Is_Pure (Current_Scope));
2581 -- We set the flag Is_First_Subtype here. It is needed to set the
2582 -- corresponding flag for the Implicit class-wide-type created
2583 -- during tagged types processing.
2585 Set_Is_First_Subtype (T, True);
2587 -- Only composite types other than array types are allowed to have
2588 -- discriminants.
2590 case Nkind (Def) is
2592 -- For derived types, the rule will be checked once we've figured
2593 -- out the parent type.
2595 when N_Derived_Type_Definition =>
2596 null;
2598 -- For record types, discriminants are allowed, unless we are in
2599 -- SPARK.
2601 when N_Record_Definition =>
2602 if Present (Discriminant_Specifications (N)) then
2603 Check_SPARK_05_Restriction
2604 ("discriminant type is not allowed",
2605 Defining_Identifier
2606 (First (Discriminant_Specifications (N))));
2607 end if;
2609 when others =>
2610 if Present (Discriminant_Specifications (N)) then
2611 Error_Msg_N
2612 ("elementary or array type cannot have discriminants",
2613 Defining_Identifier
2614 (First (Discriminant_Specifications (N))));
2615 end if;
2616 end case;
2618 -- Elaborate the type definition according to kind, and generate
2619 -- subsidiary (implicit) subtypes where needed. We skip this if it was
2620 -- already done (this happens during the reanalysis that follows a call
2621 -- to the high level optimizer).
2623 if not Analyzed (T) then
2624 Set_Analyzed (T);
2626 case Nkind (Def) is
2627 when N_Access_To_Subprogram_Definition =>
2628 Access_Subprogram_Declaration (T, Def);
2630 -- If this is a remote access to subprogram, we must create the
2631 -- equivalent fat pointer type, and related subprograms.
2633 if Is_Remote then
2634 Process_Remote_AST_Declaration (N);
2635 end if;
2637 -- Validate categorization rule against access type declaration
2638 -- usually a violation in Pure unit, Shared_Passive unit.
2640 Validate_Access_Type_Declaration (T, N);
2642 when N_Access_To_Object_Definition =>
2643 Access_Type_Declaration (T, Def);
2645 -- Validate categorization rule against access type declaration
2646 -- usually a violation in Pure unit, Shared_Passive unit.
2648 Validate_Access_Type_Declaration (T, N);
2650 -- If we are in a Remote_Call_Interface package and define a
2651 -- RACW, then calling stubs and specific stream attributes
2652 -- must be added.
2654 if Is_Remote
2655 and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
2656 then
2657 Add_RACW_Features (Def_Id);
2658 end if;
2660 when N_Array_Type_Definition =>
2661 Array_Type_Declaration (T, Def);
2663 when N_Derived_Type_Definition =>
2664 Derived_Type_Declaration (T, N, T /= Def_Id);
2666 when N_Enumeration_Type_Definition =>
2667 Enumeration_Type_Declaration (T, Def);
2669 when N_Floating_Point_Definition =>
2670 Floating_Point_Type_Declaration (T, Def);
2672 when N_Decimal_Fixed_Point_Definition =>
2673 Decimal_Fixed_Point_Type_Declaration (T, Def);
2675 when N_Ordinary_Fixed_Point_Definition =>
2676 Ordinary_Fixed_Point_Type_Declaration (T, Def);
2678 when N_Signed_Integer_Type_Definition =>
2679 Signed_Integer_Type_Declaration (T, Def);
2681 when N_Modular_Type_Definition =>
2682 Modular_Type_Declaration (T, Def);
2684 when N_Record_Definition =>
2685 Record_Type_Declaration (T, N, Prev);
2687 -- If declaration has a parse error, nothing to elaborate.
2689 when N_Error =>
2690 null;
2692 when others =>
2693 raise Program_Error;
2695 end case;
2696 end if;
2698 if Etype (T) = Any_Type then
2699 return;
2700 end if;
2702 -- Controlled type is not allowed in SPARK
2704 if Is_Visibly_Controlled (T) then
2705 Check_SPARK_05_Restriction ("controlled type is not allowed", N);
2706 end if;
2708 -- A type declared within a Ghost region is automatically Ghost
2709 -- (SPARK RM 6.9(2)).
2711 if Comes_From_Source (T) and then Ghost_Mode > None then
2712 Set_Is_Ghost_Entity (T);
2713 end if;
2715 -- Some common processing for all types
2717 Set_Depends_On_Private (T, Has_Private_Component (T));
2718 Check_Ops_From_Incomplete_Type;
2720 -- Both the declared entity, and its anonymous base type if one was
2721 -- created, need freeze nodes allocated.
2723 declare
2724 B : constant Entity_Id := Base_Type (T);
2726 begin
2727 -- In the case where the base type differs from the first subtype, we
2728 -- pre-allocate a freeze node, and set the proper link to the first
2729 -- subtype. Freeze_Entity will use this preallocated freeze node when
2730 -- it freezes the entity.
2732 -- This does not apply if the base type is a generic type, whose
2733 -- declaration is independent of the current derived definition.
2735 if B /= T and then not Is_Generic_Type (B) then
2736 Ensure_Freeze_Node (B);
2737 Set_First_Subtype_Link (Freeze_Node (B), T);
2738 end if;
2740 -- A type that is imported through a limited_with clause cannot
2741 -- generate any code, and thus need not be frozen. However, an access
2742 -- type with an imported designated type needs a finalization list,
2743 -- which may be referenced in some other package that has non-limited
2744 -- visibility on the designated type. Thus we must create the
2745 -- finalization list at the point the access type is frozen, to
2746 -- prevent unsatisfied references at link time.
2748 if not From_Limited_With (T) or else Is_Access_Type (T) then
2749 Set_Has_Delayed_Freeze (T);
2750 end if;
2751 end;
2753 -- Case where T is the full declaration of some private type which has
2754 -- been swapped in Defining_Identifier (N).
2756 if T /= Def_Id and then Is_Private_Type (Def_Id) then
2757 Process_Full_View (N, T, Def_Id);
2759 -- Record the reference. The form of this is a little strange, since
2760 -- the full declaration has been swapped in. So the first parameter
2761 -- here represents the entity to which a reference is made which is
2762 -- the "real" entity, i.e. the one swapped in, and the second
2763 -- parameter provides the reference location.
2765 -- Also, we want to kill Has_Pragma_Unreferenced temporarily here
2766 -- since we don't want a complaint about the full type being an
2767 -- unwanted reference to the private type
2769 declare
2770 B : constant Boolean := Has_Pragma_Unreferenced (T);
2771 begin
2772 Set_Has_Pragma_Unreferenced (T, False);
2773 Generate_Reference (T, T, 'c');
2774 Set_Has_Pragma_Unreferenced (T, B);
2775 end;
2777 Set_Completion_Referenced (Def_Id);
2779 -- For completion of incomplete type, process incomplete dependents
2780 -- and always mark the full type as referenced (it is the incomplete
2781 -- type that we get for any real reference).
2783 elsif Ekind (Prev) = E_Incomplete_Type then
2784 Process_Incomplete_Dependents (N, T, Prev);
2785 Generate_Reference (Prev, Def_Id, 'c');
2786 Set_Completion_Referenced (Def_Id);
2788 -- If not private type or incomplete type completion, this is a real
2789 -- definition of a new entity, so record it.
2791 else
2792 Generate_Definition (Def_Id);
2793 end if;
2795 -- Propagate any pending access types whose finalization masters need to
2796 -- be fully initialized from the partial to the full view. Guard against
2797 -- an illegal full view that remains unanalyzed.
2799 if Is_Type (Def_Id) and then Is_Incomplete_Or_Private_Type (Prev) then
2800 Set_Pending_Access_Types (Def_Id, Pending_Access_Types (Prev));
2801 end if;
2803 if Chars (Scope (Def_Id)) = Name_System
2804 and then Chars (Def_Id) = Name_Address
2805 and then Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (N)))
2806 then
2807 Set_Is_Descendent_Of_Address (Def_Id);
2808 Set_Is_Descendent_Of_Address (Base_Type (Def_Id));
2809 Set_Is_Descendent_Of_Address (Prev);
2810 end if;
2812 Set_Optimize_Alignment_Flags (Def_Id);
2813 Check_Eliminated (Def_Id);
2815 -- If the declaration is a completion and aspects are present, apply
2816 -- them to the entity for the type which is currently the partial
2817 -- view, but which is the one that will be frozen.
2819 if Has_Aspects (N) then
2821 -- In most cases the partial view is a private type, and both views
2822 -- appear in different declarative parts. In the unusual case where
2823 -- the partial view is incomplete, perform the analysis on the
2824 -- full view, to prevent freezing anomalies with the corresponding
2825 -- class-wide type, which otherwise might be frozen before the
2826 -- dispatch table is built.
2828 if Prev /= Def_Id
2829 and then Ekind (Prev) /= E_Incomplete_Type
2830 then
2831 Analyze_Aspect_Specifications (N, Prev);
2833 -- Normal case
2835 else
2836 Analyze_Aspect_Specifications (N, Def_Id);
2837 end if;
2838 end if;
2839 end Analyze_Full_Type_Declaration;
2841 ----------------------------------
2842 -- Analyze_Incomplete_Type_Decl --
2843 ----------------------------------
2845 procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
2846 F : constant Boolean := Is_Pure (Current_Scope);
2847 T : Entity_Id;
2849 begin
2850 Check_SPARK_05_Restriction ("incomplete type is not allowed", N);
2852 Generate_Definition (Defining_Identifier (N));
2854 -- Process an incomplete declaration. The identifier must not have been
2855 -- declared already in the scope. However, an incomplete declaration may
2856 -- appear in the private part of a package, for a private type that has
2857 -- already been declared.
2859 -- In this case, the discriminants (if any) must match
2861 T := Find_Type_Name (N);
2863 Set_Ekind (T, E_Incomplete_Type);
2864 Init_Size_Align (T);
2865 Set_Is_First_Subtype (T, True);
2866 Set_Etype (T, T);
2868 -- An incomplete type declared within a Ghost region is automatically
2869 -- Ghost (SPARK RM 6.9(2)).
2871 if Ghost_Mode > None then
2872 Set_Is_Ghost_Entity (T);
2873 end if;
2875 -- Ada 2005 (AI-326): Minimum decoration to give support to tagged
2876 -- incomplete types.
2878 if Tagged_Present (N) then
2879 Set_Is_Tagged_Type (T, True);
2880 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
2881 Make_Class_Wide_Type (T);
2882 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2883 end if;
2885 Push_Scope (T);
2887 Set_Stored_Constraint (T, No_Elist);
2889 if Present (Discriminant_Specifications (N)) then
2890 Process_Discriminants (N);
2891 end if;
2893 End_Scope;
2895 -- If the type has discriminants, non-trivial subtypes may be
2896 -- declared before the full view of the type. The full views of those
2897 -- subtypes will be built after the full view of the type.
2899 Set_Private_Dependents (T, New_Elmt_List);
2900 Set_Is_Pure (T, F);
2901 end Analyze_Incomplete_Type_Decl;
2903 -----------------------------------
2904 -- Analyze_Interface_Declaration --
2905 -----------------------------------
2907 procedure Analyze_Interface_Declaration (T : Entity_Id; Def : Node_Id) is
2908 CW : constant Entity_Id := Class_Wide_Type (T);
2910 begin
2911 Set_Is_Tagged_Type (T);
2912 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
2914 Set_Is_Limited_Record (T, Limited_Present (Def)
2915 or else Task_Present (Def)
2916 or else Protected_Present (Def)
2917 or else Synchronized_Present (Def));
2919 -- Type is abstract if full declaration carries keyword, or if previous
2920 -- partial view did.
2922 Set_Is_Abstract_Type (T);
2923 Set_Is_Interface (T);
2925 -- Type is a limited interface if it includes the keyword limited, task,
2926 -- protected, or synchronized.
2928 Set_Is_Limited_Interface
2929 (T, Limited_Present (Def)
2930 or else Protected_Present (Def)
2931 or else Synchronized_Present (Def)
2932 or else Task_Present (Def));
2934 Set_Interfaces (T, New_Elmt_List);
2935 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2937 -- Complete the decoration of the class-wide entity if it was already
2938 -- built (i.e. during the creation of the limited view)
2940 if Present (CW) then
2941 Set_Is_Interface (CW);
2942 Set_Is_Limited_Interface (CW, Is_Limited_Interface (T));
2943 end if;
2945 -- Check runtime support for synchronized interfaces
2947 if VM_Target = No_VM
2948 and then (Is_Task_Interface (T)
2949 or else Is_Protected_Interface (T)
2950 or else Is_Synchronized_Interface (T))
2951 and then not RTE_Available (RE_Select_Specific_Data)
2952 then
2953 Error_Msg_CRT ("synchronized interfaces", T);
2954 end if;
2955 end Analyze_Interface_Declaration;
2957 -----------------------------
2958 -- Analyze_Itype_Reference --
2959 -----------------------------
2961 -- Nothing to do. This node is placed in the tree only for the benefit of
2962 -- back end processing, and has no effect on the semantic processing.
2964 procedure Analyze_Itype_Reference (N : Node_Id) is
2965 begin
2966 pragma Assert (Is_Itype (Itype (N)));
2967 null;
2968 end Analyze_Itype_Reference;
2970 --------------------------------
2971 -- Analyze_Number_Declaration --
2972 --------------------------------
2974 procedure Analyze_Number_Declaration (N : Node_Id) is
2975 Id : constant Entity_Id := Defining_Identifier (N);
2976 E : constant Node_Id := Expression (N);
2977 T : Entity_Id;
2978 Index : Interp_Index;
2979 It : Interp;
2981 begin
2982 -- The number declaration may be subject to pragma Ghost with policy
2983 -- Ignore. Set the mode now to ensure that any nodes generated during
2984 -- analysis and expansion are properly flagged as ignored Ghost.
2986 Set_Ghost_Mode (N);
2988 Generate_Definition (Id);
2989 Enter_Name (Id);
2991 -- A number declared within a Ghost region is automatically Ghost
2992 -- (SPARK RM 6.9(2)).
2994 if Ghost_Mode > None then
2995 Set_Is_Ghost_Entity (Id);
2996 end if;
2998 -- This is an optimization of a common case of an integer literal
3000 if Nkind (E) = N_Integer_Literal then
3001 Set_Is_Static_Expression (E, True);
3002 Set_Etype (E, Universal_Integer);
3004 Set_Etype (Id, Universal_Integer);
3005 Set_Ekind (Id, E_Named_Integer);
3006 Set_Is_Frozen (Id, True);
3007 return;
3008 end if;
3010 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3012 -- Process expression, replacing error by integer zero, to avoid
3013 -- cascaded errors or aborts further along in the processing
3015 -- Replace Error by integer zero, which seems least likely to cause
3016 -- cascaded errors.
3018 if E = Error then
3019 Rewrite (E, Make_Integer_Literal (Sloc (E), Uint_0));
3020 Set_Error_Posted (E);
3021 end if;
3023 Analyze (E);
3025 -- Verify that the expression is static and numeric. If
3026 -- the expression is overloaded, we apply the preference
3027 -- rule that favors root numeric types.
3029 if not Is_Overloaded (E) then
3030 T := Etype (E);
3031 if Has_Dynamic_Predicate_Aspect (T) then
3032 Error_Msg_N
3033 ("subtype has dynamic predicate, "
3034 & "not allowed in number declaration", N);
3035 end if;
3037 else
3038 T := Any_Type;
3040 Get_First_Interp (E, Index, It);
3041 while Present (It.Typ) loop
3042 if (Is_Integer_Type (It.Typ) or else Is_Real_Type (It.Typ))
3043 and then (Scope (Base_Type (It.Typ))) = Standard_Standard
3044 then
3045 if T = Any_Type then
3046 T := It.Typ;
3048 elsif It.Typ = Universal_Real
3049 or else
3050 It.Typ = Universal_Integer
3051 then
3052 -- Choose universal interpretation over any other
3054 T := It.Typ;
3055 exit;
3056 end if;
3057 end if;
3059 Get_Next_Interp (Index, It);
3060 end loop;
3061 end if;
3063 if Is_Integer_Type (T) then
3064 Resolve (E, T);
3065 Set_Etype (Id, Universal_Integer);
3066 Set_Ekind (Id, E_Named_Integer);
3068 elsif Is_Real_Type (T) then
3070 -- Because the real value is converted to universal_real, this is a
3071 -- legal context for a universal fixed expression.
3073 if T = Universal_Fixed then
3074 declare
3075 Loc : constant Source_Ptr := Sloc (N);
3076 Conv : constant Node_Id := Make_Type_Conversion (Loc,
3077 Subtype_Mark =>
3078 New_Occurrence_Of (Universal_Real, Loc),
3079 Expression => Relocate_Node (E));
3081 begin
3082 Rewrite (E, Conv);
3083 Analyze (E);
3084 end;
3086 elsif T = Any_Fixed then
3087 Error_Msg_N ("illegal context for mixed mode operation", E);
3089 -- Expression is of the form : universal_fixed * integer. Try to
3090 -- resolve as universal_real.
3092 T := Universal_Real;
3093 Set_Etype (E, T);
3094 end if;
3096 Resolve (E, T);
3097 Set_Etype (Id, Universal_Real);
3098 Set_Ekind (Id, E_Named_Real);
3100 else
3101 Wrong_Type (E, Any_Numeric);
3102 Resolve (E, T);
3104 Set_Etype (Id, T);
3105 Set_Ekind (Id, E_Constant);
3106 Set_Never_Set_In_Source (Id, True);
3107 Set_Is_True_Constant (Id, True);
3108 return;
3109 end if;
3111 if Nkind_In (E, N_Integer_Literal, N_Real_Literal) then
3112 Set_Etype (E, Etype (Id));
3113 end if;
3115 if not Is_OK_Static_Expression (E) then
3116 Flag_Non_Static_Expr
3117 ("non-static expression used in number declaration!", E);
3118 Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
3119 Set_Etype (E, Any_Type);
3120 end if;
3121 end Analyze_Number_Declaration;
3123 -----------------------------
3124 -- Analyze_Object_Contract --
3125 -----------------------------
3127 procedure Analyze_Object_Contract (Obj_Id : Entity_Id) is
3128 Obj_Typ : constant Entity_Id := Etype (Obj_Id);
3129 AR_Val : Boolean := False;
3130 AW_Val : Boolean := False;
3131 ER_Val : Boolean := False;
3132 EW_Val : Boolean := False;
3133 Prag : Node_Id;
3134 Seen : Boolean := False;
3136 begin
3137 -- The loop parameter in an element iterator over a formal container
3138 -- is declared with an object declaration but no contracts apply.
3140 if Ekind (Obj_Id) = E_Loop_Parameter then
3141 return;
3142 end if;
3144 if Ekind (Obj_Id) = E_Constant then
3146 -- A constant cannot be effectively volatile. This check is only
3147 -- relevant with SPARK_Mode on as it is not a standard Ada legality
3148 -- rule. Do not flag internally-generated constants that map generic
3149 -- formals to actuals in instantiations (SPARK RM 7.1.3(6)).
3151 if SPARK_Mode = On
3152 and then Is_Effectively_Volatile (Obj_Id)
3153 and then No (Corresponding_Generic_Association (Parent (Obj_Id)))
3154 then
3155 Error_Msg_N ("constant cannot be volatile", Obj_Id);
3156 end if;
3158 else pragma Assert (Ekind (Obj_Id) = E_Variable);
3160 -- The following checks are only relevant when SPARK_Mode is on as
3161 -- they are not standard Ada legality rules. Internally generated
3162 -- temporaries are ignored.
3164 if SPARK_Mode = On and then Comes_From_Source (Obj_Id) then
3165 if Is_Effectively_Volatile (Obj_Id) then
3167 -- The declaration of an effectively volatile object must
3168 -- appear at the library level (SPARK RM 7.1.3(7), C.6(6)).
3170 if not Is_Library_Level_Entity (Obj_Id) then
3171 Error_Msg_N
3172 ("volatile variable & must be declared at library level",
3173 Obj_Id);
3175 -- An object of a discriminated type cannot be effectively
3176 -- volatile (SPARK RM C.6(4)).
3178 elsif Has_Discriminants (Obj_Typ) then
3179 Error_Msg_N
3180 ("discriminated object & cannot be volatile", Obj_Id);
3182 -- An object of a tagged type cannot be effectively volatile
3183 -- (SPARK RM C.6(5)).
3185 elsif Is_Tagged_Type (Obj_Typ) then
3186 Error_Msg_N ("tagged object & cannot be volatile", Obj_Id);
3187 end if;
3189 -- The object is not effectively volatile
3191 else
3192 -- A non-effectively volatile object cannot have effectively
3193 -- volatile components (SPARK RM 7.1.3(7)).
3195 if not Is_Effectively_Volatile (Obj_Id)
3196 and then Has_Volatile_Component (Obj_Typ)
3197 then
3198 Error_Msg_N
3199 ("non-volatile object & cannot have volatile components",
3200 Obj_Id);
3201 end if;
3202 end if;
3203 end if;
3205 if Is_Ghost_Entity (Obj_Id) then
3207 -- A Ghost object cannot be effectively volatile (SPARK RM 6.9(8))
3209 if Is_Effectively_Volatile (Obj_Id) then
3210 Error_Msg_N ("ghost variable & cannot be volatile", Obj_Id);
3212 -- A Ghost object cannot be imported or exported (SPARK RM 6.9(8))
3214 elsif Is_Imported (Obj_Id) then
3215 Error_Msg_N ("ghost object & cannot be imported", Obj_Id);
3217 elsif Is_Exported (Obj_Id) then
3218 Error_Msg_N ("ghost object & cannot be exported", Obj_Id);
3219 end if;
3220 end if;
3222 -- Analyze all external properties
3224 Prag := Get_Pragma (Obj_Id, Pragma_Async_Readers);
3226 if Present (Prag) then
3227 Analyze_External_Property_In_Decl_Part (Prag, AR_Val);
3228 Seen := True;
3229 end if;
3231 Prag := Get_Pragma (Obj_Id, Pragma_Async_Writers);
3233 if Present (Prag) then
3234 Analyze_External_Property_In_Decl_Part (Prag, AW_Val);
3235 Seen := True;
3236 end if;
3238 Prag := Get_Pragma (Obj_Id, Pragma_Effective_Reads);
3240 if Present (Prag) then
3241 Analyze_External_Property_In_Decl_Part (Prag, ER_Val);
3242 Seen := True;
3243 end if;
3245 Prag := Get_Pragma (Obj_Id, Pragma_Effective_Writes);
3247 if Present (Prag) then
3248 Analyze_External_Property_In_Decl_Part (Prag, EW_Val);
3249 Seen := True;
3250 end if;
3252 -- Verify the mutual interaction of the various external properties
3254 if Seen then
3255 Check_External_Properties (Obj_Id, AR_Val, AW_Val, ER_Val, EW_Val);
3256 end if;
3258 -- Check whether the lack of indicator Part_Of agrees with the
3259 -- placement of the variable with respect to the state space.
3261 Prag := Get_Pragma (Obj_Id, Pragma_Part_Of);
3263 if No (Prag) then
3264 Check_Missing_Part_Of (Obj_Id);
3265 end if;
3266 end if;
3268 -- A ghost object cannot be imported or exported (SPARK RM 6.9(8))
3270 if Is_Ghost_Entity (Obj_Id) then
3271 if Is_Exported (Obj_Id) then
3272 Error_Msg_N ("ghost object & cannot be exported", Obj_Id);
3274 elsif Is_Imported (Obj_Id) then
3275 Error_Msg_N ("ghost object & cannot be imported", Obj_Id);
3276 end if;
3277 end if;
3278 end Analyze_Object_Contract;
3280 --------------------------------
3281 -- Analyze_Object_Declaration --
3282 --------------------------------
3284 procedure Analyze_Object_Declaration (N : Node_Id) is
3285 Loc : constant Source_Ptr := Sloc (N);
3286 Id : constant Entity_Id := Defining_Identifier (N);
3287 T : Entity_Id;
3288 Act_T : Entity_Id;
3290 E : Node_Id := Expression (N);
3291 -- E is set to Expression (N) throughout this routine. When
3292 -- Expression (N) is modified, E is changed accordingly.
3294 Prev_Entity : Entity_Id := Empty;
3296 function Count_Tasks (T : Entity_Id) return Uint;
3297 -- This function is called when a non-generic library level object of a
3298 -- task type is declared. Its function is to count the static number of
3299 -- tasks declared within the type (it is only called if Has_Tasks is set
3300 -- for T). As a side effect, if an array of tasks with non-static bounds
3301 -- or a variant record type is encountered, Check_Restrictions is called
3302 -- indicating the count is unknown.
3304 -----------------
3305 -- Count_Tasks --
3306 -----------------
3308 function Count_Tasks (T : Entity_Id) return Uint is
3309 C : Entity_Id;
3310 X : Node_Id;
3311 V : Uint;
3313 begin
3314 if Is_Task_Type (T) then
3315 return Uint_1;
3317 elsif Is_Record_Type (T) then
3318 if Has_Discriminants (T) then
3319 Check_Restriction (Max_Tasks, N);
3320 return Uint_0;
3322 else
3323 V := Uint_0;
3324 C := First_Component (T);
3325 while Present (C) loop
3326 V := V + Count_Tasks (Etype (C));
3327 Next_Component (C);
3328 end loop;
3330 return V;
3331 end if;
3333 elsif Is_Array_Type (T) then
3334 X := First_Index (T);
3335 V := Count_Tasks (Component_Type (T));
3336 while Present (X) loop
3337 C := Etype (X);
3339 if not Is_OK_Static_Subtype (C) then
3340 Check_Restriction (Max_Tasks, N);
3341 return Uint_0;
3342 else
3343 V := V * (UI_Max (Uint_0,
3344 Expr_Value (Type_High_Bound (C)) -
3345 Expr_Value (Type_Low_Bound (C)) + Uint_1));
3346 end if;
3348 Next_Index (X);
3349 end loop;
3351 return V;
3353 else
3354 return Uint_0;
3355 end if;
3356 end Count_Tasks;
3358 -- Start of processing for Analyze_Object_Declaration
3360 begin
3361 -- There are three kinds of implicit types generated by an
3362 -- object declaration:
3364 -- 1. Those generated by the original Object Definition
3366 -- 2. Those generated by the Expression
3368 -- 3. Those used to constrain the Object Definition with the
3369 -- expression constraints when the definition is unconstrained.
3371 -- They must be generated in this order to avoid order of elaboration
3372 -- issues. Thus the first step (after entering the name) is to analyze
3373 -- the object definition.
3375 if Constant_Present (N) then
3376 Prev_Entity := Current_Entity_In_Scope (Id);
3378 if Present (Prev_Entity)
3379 and then
3380 -- If the homograph is an implicit subprogram, it is overridden
3381 -- by the current declaration.
3383 ((Is_Overloadable (Prev_Entity)
3384 and then Is_Inherited_Operation (Prev_Entity))
3386 -- The current object is a discriminal generated for an entry
3387 -- family index. Even though the index is a constant, in this
3388 -- particular context there is no true constant redeclaration.
3389 -- Enter_Name will handle the visibility.
3391 or else
3392 (Is_Discriminal (Id)
3393 and then Ekind (Discriminal_Link (Id)) =
3394 E_Entry_Index_Parameter)
3396 -- The current object is the renaming for a generic declared
3397 -- within the instance.
3399 or else
3400 (Ekind (Prev_Entity) = E_Package
3401 and then Nkind (Parent (Prev_Entity)) =
3402 N_Package_Renaming_Declaration
3403 and then not Comes_From_Source (Prev_Entity)
3404 and then
3405 Is_Generic_Instance (Renamed_Entity (Prev_Entity))))
3406 then
3407 Prev_Entity := Empty;
3408 end if;
3409 end if;
3411 -- The object declaration may be subject to pragma Ghost with policy
3412 -- Ignore. Set the mode now to ensure that any nodes generated during
3413 -- analysis and expansion are properly flagged as ignored Ghost.
3415 Set_Ghost_Mode (N, Prev_Entity);
3417 if Present (Prev_Entity) then
3418 Constant_Redeclaration (Id, N, T);
3420 Generate_Reference (Prev_Entity, Id, 'c');
3421 Set_Completion_Referenced (Id);
3423 if Error_Posted (N) then
3425 -- Type mismatch or illegal redeclaration, Do not analyze
3426 -- expression to avoid cascaded errors.
3428 T := Find_Type_Of_Object (Object_Definition (N), N);
3429 Set_Etype (Id, T);
3430 Set_Ekind (Id, E_Variable);
3431 goto Leave;
3432 end if;
3434 -- In the normal case, enter identifier at the start to catch premature
3435 -- usage in the initialization expression.
3437 else
3438 Generate_Definition (Id);
3439 Enter_Name (Id);
3441 Mark_Coextensions (N, Object_Definition (N));
3443 T := Find_Type_Of_Object (Object_Definition (N), N);
3445 if Nkind (Object_Definition (N)) = N_Access_Definition
3446 and then Present
3447 (Access_To_Subprogram_Definition (Object_Definition (N)))
3448 and then Protected_Present
3449 (Access_To_Subprogram_Definition (Object_Definition (N)))
3450 then
3451 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
3452 end if;
3454 if Error_Posted (Id) then
3455 Set_Etype (Id, T);
3456 Set_Ekind (Id, E_Variable);
3457 goto Leave;
3458 end if;
3459 end if;
3461 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
3462 -- out some static checks
3464 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (T) then
3466 -- In case of aggregates we must also take care of the correct
3467 -- initialization of nested aggregates bug this is done at the
3468 -- point of the analysis of the aggregate (see sem_aggr.adb).
3470 if Present (Expression (N))
3471 and then Nkind (Expression (N)) = N_Aggregate
3472 then
3473 null;
3475 else
3476 declare
3477 Save_Typ : constant Entity_Id := Etype (Id);
3478 begin
3479 Set_Etype (Id, T); -- Temp. decoration for static checks
3480 Null_Exclusion_Static_Checks (N);
3481 Set_Etype (Id, Save_Typ);
3482 end;
3483 end if;
3484 end if;
3486 -- Object is marked pure if it is in a pure scope
3488 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3490 -- If deferred constant, make sure context is appropriate. We detect
3491 -- a deferred constant as a constant declaration with no expression.
3492 -- A deferred constant can appear in a package body if its completion
3493 -- is by means of an interface pragma.
3495 if Constant_Present (N) and then No (E) then
3497 -- A deferred constant may appear in the declarative part of the
3498 -- following constructs:
3500 -- blocks
3501 -- entry bodies
3502 -- extended return statements
3503 -- package specs
3504 -- package bodies
3505 -- subprogram bodies
3506 -- task bodies
3508 -- When declared inside a package spec, a deferred constant must be
3509 -- completed by a full constant declaration or pragma Import. In all
3510 -- other cases, the only proper completion is pragma Import. Extended
3511 -- return statements are flagged as invalid contexts because they do
3512 -- not have a declarative part and so cannot accommodate the pragma.
3514 if Ekind (Current_Scope) = E_Return_Statement then
3515 Error_Msg_N
3516 ("invalid context for deferred constant declaration (RM 7.4)",
3518 Error_Msg_N
3519 ("\declaration requires an initialization expression",
3521 Set_Constant_Present (N, False);
3523 -- In Ada 83, deferred constant must be of private type
3525 elsif not Is_Private_Type (T) then
3526 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
3527 Error_Msg_N
3528 ("(Ada 83) deferred constant must be private type", N);
3529 end if;
3530 end if;
3532 -- If not a deferred constant, then the object declaration freezes
3533 -- its type, unless the object is of an anonymous type and has delayed
3534 -- aspects. In that case the type is frozen when the object itself is.
3536 else
3537 Check_Fully_Declared (T, N);
3539 if Has_Delayed_Aspects (Id)
3540 and then Is_Array_Type (T)
3541 and then Is_Itype (T)
3542 then
3543 Set_Has_Delayed_Freeze (T);
3544 else
3545 Freeze_Before (N, T);
3546 end if;
3547 end if;
3549 -- If the object was created by a constrained array definition, then
3550 -- set the link in both the anonymous base type and anonymous subtype
3551 -- that are built to represent the array type to point to the object.
3553 if Nkind (Object_Definition (Declaration_Node (Id))) =
3554 N_Constrained_Array_Definition
3555 then
3556 Set_Related_Array_Object (T, Id);
3557 Set_Related_Array_Object (Base_Type (T), Id);
3558 end if;
3560 -- Special checks for protected objects not at library level
3562 if Is_Protected_Type (T)
3563 and then not Is_Library_Level_Entity (Id)
3564 then
3565 Check_Restriction (No_Local_Protected_Objects, Id);
3567 -- Protected objects with interrupt handlers must be at library level
3569 -- Ada 2005: This test is not needed (and the corresponding clause
3570 -- in the RM is removed) because accessibility checks are sufficient
3571 -- to make handlers not at the library level illegal.
3573 -- AI05-0303: The AI is in fact a binding interpretation, and thus
3574 -- applies to the '95 version of the language as well.
3576 if Has_Interrupt_Handler (T) and then Ada_Version < Ada_95 then
3577 Error_Msg_N
3578 ("interrupt object can only be declared at library level", Id);
3579 end if;
3580 end if;
3582 -- The actual subtype of the object is the nominal subtype, unless
3583 -- the nominal one is unconstrained and obtained from the expression.
3585 Act_T := T;
3587 -- These checks should be performed before the initialization expression
3588 -- is considered, so that the Object_Definition node is still the same
3589 -- as in source code.
3591 -- In SPARK, the nominal subtype is always given by a subtype mark
3592 -- and must not be unconstrained. (The only exception to this is the
3593 -- acceptance of declarations of constants of type String.)
3595 if not Nkind_In (Object_Definition (N), N_Expanded_Name, N_Identifier)
3596 then
3597 Check_SPARK_05_Restriction
3598 ("subtype mark required", Object_Definition (N));
3600 elsif Is_Array_Type (T)
3601 and then not Is_Constrained (T)
3602 and then T /= Standard_String
3603 then
3604 Check_SPARK_05_Restriction
3605 ("subtype mark of constrained type expected",
3606 Object_Definition (N));
3607 end if;
3609 -- There are no aliased objects in SPARK
3611 if Aliased_Present (N) then
3612 Check_SPARK_05_Restriction ("aliased object is not allowed", N);
3613 end if;
3615 -- Process initialization expression if present and not in error
3617 if Present (E) and then E /= Error then
3619 -- Generate an error in case of CPP class-wide object initialization.
3620 -- Required because otherwise the expansion of the class-wide
3621 -- assignment would try to use 'size to initialize the object
3622 -- (primitive that is not available in CPP tagged types).
3624 if Is_Class_Wide_Type (Act_T)
3625 and then
3626 (Is_CPP_Class (Root_Type (Etype (Act_T)))
3627 or else
3628 (Present (Full_View (Root_Type (Etype (Act_T))))
3629 and then
3630 Is_CPP_Class (Full_View (Root_Type (Etype (Act_T))))))
3631 then
3632 Error_Msg_N
3633 ("predefined assignment not available for 'C'P'P tagged types",
3635 end if;
3637 Mark_Coextensions (N, E);
3638 Analyze (E);
3640 -- In case of errors detected in the analysis of the expression,
3641 -- decorate it with the expected type to avoid cascaded errors
3643 if No (Etype (E)) then
3644 Set_Etype (E, T);
3645 end if;
3647 -- If an initialization expression is present, then we set the
3648 -- Is_True_Constant flag. It will be reset if this is a variable
3649 -- and it is indeed modified.
3651 Set_Is_True_Constant (Id, True);
3653 -- If we are analyzing a constant declaration, set its completion
3654 -- flag after analyzing and resolving the expression.
3656 if Constant_Present (N) then
3657 Set_Has_Completion (Id);
3658 end if;
3660 -- Set type and resolve (type may be overridden later on). Note:
3661 -- Ekind (Id) must still be E_Void at this point so that incorrect
3662 -- early usage within E is properly diagnosed.
3664 Set_Etype (Id, T);
3666 -- If the expression is an aggregate we must look ahead to detect
3667 -- the possible presence of an address clause, and defer resolution
3668 -- and expansion of the aggregate to the freeze point of the entity.
3670 if Comes_From_Source (N)
3671 and then Expander_Active
3672 and then Nkind (E) = N_Aggregate
3673 and then Present (Following_Address_Clause (N))
3674 then
3675 Set_Etype (E, T);
3677 else
3678 Resolve (E, T);
3679 end if;
3681 -- No further action needed if E is a call to an inlined function
3682 -- which returns an unconstrained type and it has been expanded into
3683 -- a procedure call. In that case N has been replaced by an object
3684 -- declaration without initializing expression and it has been
3685 -- analyzed (see Expand_Inlined_Call).
3687 if Back_End_Inlining
3688 and then Expander_Active
3689 and then Nkind (E) = N_Function_Call
3690 and then Nkind (Name (E)) in N_Has_Entity
3691 and then Is_Inlined (Entity (Name (E)))
3692 and then not Is_Constrained (Etype (E))
3693 and then Analyzed (N)
3694 and then No (Expression (N))
3695 then
3696 return;
3697 end if;
3699 -- If E is null and has been replaced by an N_Raise_Constraint_Error
3700 -- node (which was marked already-analyzed), we need to set the type
3701 -- to something other than Any_Access in order to keep gigi happy.
3703 if Etype (E) = Any_Access then
3704 Set_Etype (E, T);
3705 end if;
3707 -- If the object is an access to variable, the initialization
3708 -- expression cannot be an access to constant.
3710 if Is_Access_Type (T)
3711 and then not Is_Access_Constant (T)
3712 and then Is_Access_Type (Etype (E))
3713 and then Is_Access_Constant (Etype (E))
3714 then
3715 Error_Msg_N
3716 ("access to variable cannot be initialized with an "
3717 & "access-to-constant expression", E);
3718 end if;
3720 if not Assignment_OK (N) then
3721 Check_Initialization (T, E);
3722 end if;
3724 Check_Unset_Reference (E);
3726 -- If this is a variable, then set current value. If this is a
3727 -- declared constant of a scalar type with a static expression,
3728 -- indicate that it is always valid.
3730 if not Constant_Present (N) then
3731 if Compile_Time_Known_Value (E) then
3732 Set_Current_Value (Id, E);
3733 end if;
3735 elsif Is_Scalar_Type (T) and then Is_OK_Static_Expression (E) then
3736 Set_Is_Known_Valid (Id);
3737 end if;
3739 -- Deal with setting of null flags
3741 if Is_Access_Type (T) then
3742 if Known_Non_Null (E) then
3743 Set_Is_Known_Non_Null (Id, True);
3744 elsif Known_Null (E) and then not Can_Never_Be_Null (Id) then
3745 Set_Is_Known_Null (Id, True);
3746 end if;
3747 end if;
3749 -- Check incorrect use of dynamically tagged expressions
3751 if Is_Tagged_Type (T) then
3752 Check_Dynamically_Tagged_Expression
3753 (Expr => E,
3754 Typ => T,
3755 Related_Nod => N);
3756 end if;
3758 Apply_Scalar_Range_Check (E, T);
3759 Apply_Static_Length_Check (E, T);
3761 if Nkind (Original_Node (N)) = N_Object_Declaration
3762 and then Comes_From_Source (Original_Node (N))
3764 -- Only call test if needed
3766 and then Restriction_Check_Required (SPARK_05)
3767 and then not Is_SPARK_05_Initialization_Expr (Original_Node (E))
3768 then
3769 Check_SPARK_05_Restriction
3770 ("initialization expression is not appropriate", E);
3771 end if;
3773 -- A formal parameter of a specific tagged type whose related
3774 -- subprogram is subject to pragma Extensions_Visible with value
3775 -- "False" cannot be implicitly converted to a class-wide type by
3776 -- means of an initialization expression (SPARK RM 6.1.7(3)).
3778 if Is_Class_Wide_Type (T) and then Is_EVF_Expression (E) then
3779 Error_Msg_N
3780 ("formal parameter with Extensions_Visible False cannot be "
3781 & "implicitly converted to class-wide type", E);
3782 end if;
3783 end if;
3785 -- If the No_Streams restriction is set, check that the type of the
3786 -- object is not, and does not contain, any subtype derived from
3787 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
3788 -- Has_Stream just for efficiency reasons. There is no point in
3789 -- spending time on a Has_Stream check if the restriction is not set.
3791 if Restriction_Check_Required (No_Streams) then
3792 if Has_Stream (T) then
3793 Check_Restriction (No_Streams, N);
3794 end if;
3795 end if;
3797 -- Deal with predicate check before we start to do major rewriting. It
3798 -- is OK to initialize and then check the initialized value, since the
3799 -- object goes out of scope if we get a predicate failure. Note that we
3800 -- do this in the analyzer and not the expander because the analyzer
3801 -- does some substantial rewriting in some cases.
3803 -- We need a predicate check if the type has predicates, and if either
3804 -- there is an initializing expression, or for default initialization
3805 -- when we have at least one case of an explicit default initial value
3806 -- and then this is not an internal declaration whose initialization
3807 -- comes later (as for an aggregate expansion).
3809 if not Suppress_Assignment_Checks (N)
3810 and then Present (Predicate_Function (T))
3811 and then not No_Initialization (N)
3812 and then
3813 (Present (E)
3814 or else
3815 Is_Partially_Initialized_Type (T, Include_Implicit => False))
3816 then
3817 -- If the type has a static predicate and the expression is known at
3818 -- compile time, see if the expression satisfies the predicate.
3820 if Present (E) then
3821 Check_Expression_Against_Static_Predicate (E, T);
3822 end if;
3824 Insert_After (N,
3825 Make_Predicate_Check (T, New_Occurrence_Of (Id, Loc)));
3826 end if;
3828 -- Case of unconstrained type
3830 if Is_Indefinite_Subtype (T) then
3832 -- In SPARK, a declaration of unconstrained type is allowed
3833 -- only for constants of type string.
3835 if Is_String_Type (T) and then not Constant_Present (N) then
3836 Check_SPARK_05_Restriction
3837 ("declaration of object of unconstrained type not allowed", N);
3838 end if;
3840 -- Nothing to do in deferred constant case
3842 if Constant_Present (N) and then No (E) then
3843 null;
3845 -- Case of no initialization present
3847 elsif No (E) then
3848 if No_Initialization (N) then
3849 null;
3851 elsif Is_Class_Wide_Type (T) then
3852 Error_Msg_N
3853 ("initialization required in class-wide declaration ", N);
3855 else
3856 Error_Msg_N
3857 ("unconstrained subtype not allowed (need initialization)",
3858 Object_Definition (N));
3860 if Is_Record_Type (T) and then Has_Discriminants (T) then
3861 Error_Msg_N
3862 ("\provide initial value or explicit discriminant values",
3863 Object_Definition (N));
3865 Error_Msg_NE
3866 ("\or give default discriminant values for type&",
3867 Object_Definition (N), T);
3869 elsif Is_Array_Type (T) then
3870 Error_Msg_N
3871 ("\provide initial value or explicit array bounds",
3872 Object_Definition (N));
3873 end if;
3874 end if;
3876 -- Case of initialization present but in error. Set initial
3877 -- expression as absent (but do not make above complaints)
3879 elsif E = Error then
3880 Set_Expression (N, Empty);
3881 E := Empty;
3883 -- Case of initialization present
3885 else
3886 -- Check restrictions in Ada 83
3888 if not Constant_Present (N) then
3890 -- Unconstrained variables not allowed in Ada 83 mode
3892 if Ada_Version = Ada_83
3893 and then Comes_From_Source (Object_Definition (N))
3894 then
3895 Error_Msg_N
3896 ("(Ada 83) unconstrained variable not allowed",
3897 Object_Definition (N));
3898 end if;
3899 end if;
3901 -- Now we constrain the variable from the initializing expression
3903 -- If the expression is an aggregate, it has been expanded into
3904 -- individual assignments. Retrieve the actual type from the
3905 -- expanded construct.
3907 if Is_Array_Type (T)
3908 and then No_Initialization (N)
3909 and then Nkind (Original_Node (E)) = N_Aggregate
3910 then
3911 Act_T := Etype (E);
3913 -- In case of class-wide interface object declarations we delay
3914 -- the generation of the equivalent record type declarations until
3915 -- its expansion because there are cases in they are not required.
3917 elsif Is_Interface (T) then
3918 null;
3920 -- In GNATprove mode, Expand_Subtype_From_Expr does nothing. Thus,
3921 -- we should prevent the generation of another Itype with the
3922 -- same name as the one already generated, or we end up with
3923 -- two identical types in GNATprove.
3925 elsif GNATprove_Mode then
3926 null;
3928 -- If the type is an unchecked union, no subtype can be built from
3929 -- the expression. Rewrite declaration as a renaming, which the
3930 -- back-end can handle properly. This is a rather unusual case,
3931 -- because most unchecked_union declarations have default values
3932 -- for discriminants and are thus not indefinite.
3934 elsif Is_Unchecked_Union (T) then
3935 if Constant_Present (N) or else Nkind (E) = N_Function_Call then
3936 Set_Ekind (Id, E_Constant);
3937 else
3938 Set_Ekind (Id, E_Variable);
3939 end if;
3941 -- An object declared within a Ghost region is automatically
3942 -- Ghost (SPARK RM 6.9(2)).
3944 if Comes_From_Source (Id) and then Ghost_Mode > None then
3945 Set_Is_Ghost_Entity (Id);
3947 -- The Ghost policy in effect at the point of declaration
3948 -- and at the point of completion must match
3949 -- (SPARK RM 6.9(15)).
3951 if Present (Prev_Entity)
3952 and then Is_Ghost_Entity (Prev_Entity)
3953 then
3954 Check_Ghost_Completion (Prev_Entity, Id);
3955 end if;
3956 end if;
3958 Rewrite (N,
3959 Make_Object_Renaming_Declaration (Loc,
3960 Defining_Identifier => Id,
3961 Subtype_Mark => New_Occurrence_Of (T, Loc),
3962 Name => E));
3964 Set_Renamed_Object (Id, E);
3965 Freeze_Before (N, T);
3966 Set_Is_Frozen (Id);
3967 return;
3969 else
3970 Expand_Subtype_From_Expr (N, T, Object_Definition (N), E);
3971 Act_T := Find_Type_Of_Object (Object_Definition (N), N);
3972 end if;
3974 Set_Is_Constr_Subt_For_U_Nominal (Act_T);
3976 if Aliased_Present (N) then
3977 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
3978 end if;
3980 Freeze_Before (N, Act_T);
3981 Freeze_Before (N, T);
3982 end if;
3984 elsif Is_Array_Type (T)
3985 and then No_Initialization (N)
3986 and then Nkind (Original_Node (E)) = N_Aggregate
3987 then
3988 if not Is_Entity_Name (Object_Definition (N)) then
3989 Act_T := Etype (E);
3990 Check_Compile_Time_Size (Act_T);
3992 if Aliased_Present (N) then
3993 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
3994 end if;
3995 end if;
3997 -- When the given object definition and the aggregate are specified
3998 -- independently, and their lengths might differ do a length check.
3999 -- This cannot happen if the aggregate is of the form (others =>...)
4001 if not Is_Constrained (T) then
4002 null;
4004 elsif Nkind (E) = N_Raise_Constraint_Error then
4006 -- Aggregate is statically illegal. Place back in declaration
4008 Set_Expression (N, E);
4009 Set_No_Initialization (N, False);
4011 elsif T = Etype (E) then
4012 null;
4014 elsif Nkind (E) = N_Aggregate
4015 and then Present (Component_Associations (E))
4016 and then Present (Choices (First (Component_Associations (E))))
4017 and then Nkind (First
4018 (Choices (First (Component_Associations (E))))) = N_Others_Choice
4019 then
4020 null;
4022 else
4023 Apply_Length_Check (E, T);
4024 end if;
4026 -- If the type is limited unconstrained with defaulted discriminants and
4027 -- there is no expression, then the object is constrained by the
4028 -- defaults, so it is worthwhile building the corresponding subtype.
4030 elsif (Is_Limited_Record (T) or else Is_Concurrent_Type (T))
4031 and then not Is_Constrained (T)
4032 and then Has_Discriminants (T)
4033 then
4034 if No (E) then
4035 Act_T := Build_Default_Subtype (T, N);
4036 else
4037 -- Ada 2005: A limited object may be initialized by means of an
4038 -- aggregate. If the type has default discriminants it has an
4039 -- unconstrained nominal type, Its actual subtype will be obtained
4040 -- from the aggregate, and not from the default discriminants.
4042 Act_T := Etype (E);
4043 end if;
4045 Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
4047 elsif Nkind (E) = N_Function_Call
4048 and then Constant_Present (N)
4049 and then Has_Unconstrained_Elements (Etype (E))
4050 then
4051 -- The back-end has problems with constants of a discriminated type
4052 -- with defaults, if the initial value is a function call. We
4053 -- generate an intermediate temporary that will receive a reference
4054 -- to the result of the call. The initialization expression then
4055 -- becomes a dereference of that temporary.
4057 Remove_Side_Effects (E);
4059 -- If this is a constant declaration of an unconstrained type and
4060 -- the initialization is an aggregate, we can use the subtype of the
4061 -- aggregate for the declared entity because it is immutable.
4063 elsif not Is_Constrained (T)
4064 and then Has_Discriminants (T)
4065 and then Constant_Present (N)
4066 and then not Has_Unchecked_Union (T)
4067 and then Nkind (E) = N_Aggregate
4068 then
4069 Act_T := Etype (E);
4070 end if;
4072 -- Check No_Wide_Characters restriction
4074 Check_Wide_Character_Restriction (T, Object_Definition (N));
4076 -- Indicate this is not set in source. Certainly true for constants, and
4077 -- true for variables so far (will be reset for a variable if and when
4078 -- we encounter a modification in the source).
4080 Set_Never_Set_In_Source (Id);
4082 -- Now establish the proper kind and type of the object
4084 if Constant_Present (N) then
4085 Set_Ekind (Id, E_Constant);
4086 Set_Is_True_Constant (Id);
4088 else
4089 Set_Ekind (Id, E_Variable);
4091 -- A variable is set as shared passive if it appears in a shared
4092 -- passive package, and is at the outer level. This is not done for
4093 -- entities generated during expansion, because those are always
4094 -- manipulated locally.
4096 if Is_Shared_Passive (Current_Scope)
4097 and then Is_Library_Level_Entity (Id)
4098 and then Comes_From_Source (Id)
4099 then
4100 Set_Is_Shared_Passive (Id);
4101 Check_Shared_Var (Id, T, N);
4102 end if;
4104 -- Set Has_Initial_Value if initializing expression present. Note
4105 -- that if there is no initializing expression, we leave the state
4106 -- of this flag unchanged (usually it will be False, but notably in
4107 -- the case of exception choice variables, it will already be true).
4109 if Present (E) then
4110 Set_Has_Initial_Value (Id);
4111 end if;
4113 Set_Contract (Id, Make_Contract (Sloc (Id)));
4114 end if;
4116 -- Initialize alignment and size and capture alignment setting
4118 Init_Alignment (Id);
4119 Init_Esize (Id);
4120 Set_Optimize_Alignment_Flags (Id);
4122 -- An object declared within a Ghost region is automatically Ghost
4123 -- (SPARK RM 6.9(2)).
4125 if Comes_From_Source (Id)
4126 and then (Ghost_Mode > None
4127 or else (Present (Prev_Entity)
4128 and then Is_Ghost_Entity (Prev_Entity)))
4129 then
4130 Set_Is_Ghost_Entity (Id);
4132 -- The Ghost policy in effect at the point of declaration and at the
4133 -- point of completion must match (SPARK RM 6.9(16)).
4135 if Present (Prev_Entity) and then Is_Ghost_Entity (Prev_Entity) then
4136 Check_Ghost_Completion (Prev_Entity, Id);
4137 end if;
4138 end if;
4140 -- Deal with aliased case
4142 if Aliased_Present (N) then
4143 Set_Is_Aliased (Id);
4145 -- If the object is aliased and the type is unconstrained with
4146 -- defaulted discriminants and there is no expression, then the
4147 -- object is constrained by the defaults, so it is worthwhile
4148 -- building the corresponding subtype.
4150 -- Ada 2005 (AI-363): If the aliased object is discriminated and
4151 -- unconstrained, then only establish an actual subtype if the
4152 -- nominal subtype is indefinite. In definite cases the object is
4153 -- unconstrained in Ada 2005.
4155 if No (E)
4156 and then Is_Record_Type (T)
4157 and then not Is_Constrained (T)
4158 and then Has_Discriminants (T)
4159 and then (Ada_Version < Ada_2005 or else Is_Indefinite_Subtype (T))
4160 then
4161 Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
4162 end if;
4163 end if;
4165 -- Now we can set the type of the object
4167 Set_Etype (Id, Act_T);
4169 -- Non-constant object is marked to be treated as volatile if type is
4170 -- volatile and we clear the Current_Value setting that may have been
4171 -- set above. Doing so for constants isn't required and might interfere
4172 -- with possible uses of the object as a static expression in contexts
4173 -- incompatible with volatility (e.g. as a case-statement alternative).
4175 if Ekind (Id) /= E_Constant and then Treat_As_Volatile (Etype (Id)) then
4176 Set_Treat_As_Volatile (Id);
4177 Set_Current_Value (Id, Empty);
4178 end if;
4180 -- Deal with controlled types
4182 if Has_Controlled_Component (Etype (Id))
4183 or else Is_Controlled (Etype (Id))
4184 then
4185 if not Is_Library_Level_Entity (Id) then
4186 Check_Restriction (No_Nested_Finalization, N);
4187 else
4188 Validate_Controlled_Object (Id);
4189 end if;
4190 end if;
4192 if Has_Task (Etype (Id)) then
4193 Check_Restriction (No_Tasking, N);
4195 -- Deal with counting max tasks
4197 -- Nothing to do if inside a generic
4199 if Inside_A_Generic then
4200 null;
4202 -- If library level entity, then count tasks
4204 elsif Is_Library_Level_Entity (Id) then
4205 Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
4207 -- If not library level entity, then indicate we don't know max
4208 -- tasks and also check task hierarchy restriction and blocking
4209 -- operation (since starting a task is definitely blocking).
4211 else
4212 Check_Restriction (Max_Tasks, N);
4213 Check_Restriction (No_Task_Hierarchy, N);
4214 Check_Potentially_Blocking_Operation (N);
4215 end if;
4217 -- A rather specialized test. If we see two tasks being declared
4218 -- of the same type in the same object declaration, and the task
4219 -- has an entry with an address clause, we know that program error
4220 -- will be raised at run time since we can't have two tasks with
4221 -- entries at the same address.
4223 if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
4224 declare
4225 E : Entity_Id;
4227 begin
4228 E := First_Entity (Etype (Id));
4229 while Present (E) loop
4230 if Ekind (E) = E_Entry
4231 and then Present (Get_Attribute_Definition_Clause
4232 (E, Attribute_Address))
4233 then
4234 Error_Msg_Warn := SPARK_Mode /= On;
4235 Error_Msg_N
4236 ("more than one task with same entry address<<", N);
4237 Error_Msg_N ("\Program_Error [<<", N);
4238 Insert_Action (N,
4239 Make_Raise_Program_Error (Loc,
4240 Reason => PE_Duplicated_Entry_Address));
4241 exit;
4242 end if;
4244 Next_Entity (E);
4245 end loop;
4246 end;
4247 end if;
4248 end if;
4250 -- Some simple constant-propagation: if the expression is a constant
4251 -- string initialized with a literal, share the literal. This avoids
4252 -- a run-time copy.
4254 if Present (E)
4255 and then Is_Entity_Name (E)
4256 and then Ekind (Entity (E)) = E_Constant
4257 and then Base_Type (Etype (E)) = Standard_String
4258 then
4259 declare
4260 Val : constant Node_Id := Constant_Value (Entity (E));
4261 begin
4262 if Present (Val) and then Nkind (Val) = N_String_Literal then
4263 Rewrite (E, New_Copy (Val));
4264 end if;
4265 end;
4266 end if;
4268 -- Another optimization: if the nominal subtype is unconstrained and
4269 -- the expression is a function call that returns an unconstrained
4270 -- type, rewrite the declaration as a renaming of the result of the
4271 -- call. The exceptions below are cases where the copy is expected,
4272 -- either by the back end (Aliased case) or by the semantics, as for
4273 -- initializing controlled types or copying tags for classwide types.
4275 if Present (E)
4276 and then Nkind (E) = N_Explicit_Dereference
4277 and then Nkind (Original_Node (E)) = N_Function_Call
4278 and then not Is_Library_Level_Entity (Id)
4279 and then not Is_Constrained (Underlying_Type (T))
4280 and then not Is_Aliased (Id)
4281 and then not Is_Class_Wide_Type (T)
4282 and then not Is_Controlled (T)
4283 and then not Has_Controlled_Component (Base_Type (T))
4284 and then Expander_Active
4285 then
4286 Rewrite (N,
4287 Make_Object_Renaming_Declaration (Loc,
4288 Defining_Identifier => Id,
4289 Access_Definition => Empty,
4290 Subtype_Mark => New_Occurrence_Of
4291 (Base_Type (Etype (Id)), Loc),
4292 Name => E));
4294 Set_Renamed_Object (Id, E);
4296 -- Force generation of debugging information for the constant and for
4297 -- the renamed function call.
4299 Set_Debug_Info_Needed (Id);
4300 Set_Debug_Info_Needed (Entity (Prefix (E)));
4301 end if;
4303 if Present (Prev_Entity)
4304 and then Is_Frozen (Prev_Entity)
4305 and then not Error_Posted (Id)
4306 then
4307 Error_Msg_N ("full constant declaration appears too late", N);
4308 end if;
4310 Check_Eliminated (Id);
4312 -- Deal with setting In_Private_Part flag if in private part
4314 if Ekind (Scope (Id)) = E_Package and then In_Private_Part (Scope (Id))
4315 then
4316 Set_In_Private_Part (Id);
4317 end if;
4319 -- Check for violation of No_Local_Timing_Events
4321 if Restriction_Check_Required (No_Local_Timing_Events)
4322 and then not Is_Library_Level_Entity (Id)
4323 and then Is_RTE (Etype (Id), RE_Timing_Event)
4324 then
4325 Check_Restriction (No_Local_Timing_Events, N);
4326 end if;
4328 <<Leave>>
4329 -- Initialize the refined state of a variable here because this is a
4330 -- common destination for legal and illegal object declarations.
4332 if Ekind (Id) = E_Variable then
4333 Set_Encapsulating_State (Id, Empty);
4334 end if;
4336 if Has_Aspects (N) then
4337 Analyze_Aspect_Specifications (N, Id);
4338 end if;
4340 Analyze_Dimension (N);
4342 -- Verify whether the object declaration introduces an illegal hidden
4343 -- state within a package subject to a null abstract state.
4345 if Ekind (Id) = E_Variable then
4346 Check_No_Hidden_State (Id);
4347 end if;
4348 end Analyze_Object_Declaration;
4350 ---------------------------
4351 -- Analyze_Others_Choice --
4352 ---------------------------
4354 -- Nothing to do for the others choice node itself, the semantic analysis
4355 -- of the others choice will occur as part of the processing of the parent
4357 procedure Analyze_Others_Choice (N : Node_Id) is
4358 pragma Warnings (Off, N);
4359 begin
4360 null;
4361 end Analyze_Others_Choice;
4363 -------------------------------------------
4364 -- Analyze_Private_Extension_Declaration --
4365 -------------------------------------------
4367 procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
4368 T : constant Entity_Id := Defining_Identifier (N);
4369 Indic : constant Node_Id := Subtype_Indication (N);
4370 Parent_Type : Entity_Id;
4371 Parent_Base : Entity_Id;
4373 begin
4374 -- The private extension declaration may be subject to pragma Ghost with
4375 -- policy Ignore. Set the mode now to ensure that any nodes generated
4376 -- during analysis and expansion are properly flagged as ignored Ghost.
4378 Set_Ghost_Mode (N);
4380 -- Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
4382 if Is_Non_Empty_List (Interface_List (N)) then
4383 declare
4384 Intf : Node_Id;
4385 T : Entity_Id;
4387 begin
4388 Intf := First (Interface_List (N));
4389 while Present (Intf) loop
4390 T := Find_Type_Of_Subtype_Indic (Intf);
4392 Diagnose_Interface (Intf, T);
4393 Next (Intf);
4394 end loop;
4395 end;
4396 end if;
4398 Generate_Definition (T);
4400 -- For other than Ada 2012, just enter the name in the current scope
4402 if Ada_Version < Ada_2012 then
4403 Enter_Name (T);
4405 -- Ada 2012 (AI05-0162): Enter the name in the current scope handling
4406 -- case of private type that completes an incomplete type.
4408 else
4409 declare
4410 Prev : Entity_Id;
4412 begin
4413 Prev := Find_Type_Name (N);
4415 pragma Assert (Prev = T
4416 or else (Ekind (Prev) = E_Incomplete_Type
4417 and then Present (Full_View (Prev))
4418 and then Full_View (Prev) = T));
4419 end;
4420 end if;
4422 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
4423 Parent_Base := Base_Type (Parent_Type);
4425 if Parent_Type = Any_Type or else Etype (Parent_Type) = Any_Type then
4426 Set_Ekind (T, Ekind (Parent_Type));
4427 Set_Etype (T, Any_Type);
4428 goto Leave;
4430 elsif not Is_Tagged_Type (Parent_Type) then
4431 Error_Msg_N
4432 ("parent of type extension must be a tagged type ", Indic);
4433 goto Leave;
4435 elsif Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
4436 Error_Msg_N ("premature derivation of incomplete type", Indic);
4437 goto Leave;
4439 elsif Is_Concurrent_Type (Parent_Type) then
4440 Error_Msg_N
4441 ("parent type of a private extension cannot be "
4442 & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
4444 Set_Etype (T, Any_Type);
4445 Set_Ekind (T, E_Limited_Private_Type);
4446 Set_Private_Dependents (T, New_Elmt_List);
4447 Set_Error_Posted (T);
4448 goto Leave;
4449 end if;
4451 -- Perhaps the parent type should be changed to the class-wide type's
4452 -- specific type in this case to prevent cascading errors ???
4454 if Is_Class_Wide_Type (Parent_Type) then
4455 Error_Msg_N
4456 ("parent of type extension must not be a class-wide type", Indic);
4457 goto Leave;
4458 end if;
4460 if (not Is_Package_Or_Generic_Package (Current_Scope)
4461 and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
4462 or else In_Private_Part (Current_Scope)
4464 then
4465 Error_Msg_N ("invalid context for private extension", N);
4466 end if;
4468 -- Set common attributes
4470 Set_Is_Pure (T, Is_Pure (Current_Scope));
4471 Set_Scope (T, Current_Scope);
4472 Set_Ekind (T, E_Record_Type_With_Private);
4473 Init_Size_Align (T);
4474 Set_Default_SSO (T);
4476 Set_Etype (T, Parent_Base);
4477 Set_Has_Task (T, Has_Task (Parent_Base));
4478 Set_Has_Protected (T, Has_Task (Parent_Base));
4480 Set_Convention (T, Convention (Parent_Type));
4481 Set_First_Rep_Item (T, First_Rep_Item (Parent_Type));
4482 Set_Is_First_Subtype (T);
4483 Make_Class_Wide_Type (T);
4485 if Unknown_Discriminants_Present (N) then
4486 Set_Discriminant_Constraint (T, No_Elist);
4487 end if;
4489 Build_Derived_Record_Type (N, Parent_Type, T);
4491 -- Propagate inherited invariant information. The new type has
4492 -- invariants, if the parent type has inheritable invariants,
4493 -- and these invariants can in turn be inherited.
4495 if Has_Inheritable_Invariants (Parent_Type) then
4496 Set_Has_Inheritable_Invariants (T);
4497 Set_Has_Invariants (T);
4498 end if;
4500 -- Ada 2005 (AI-443): Synchronized private extension or a rewritten
4501 -- synchronized formal derived type.
4503 if Ada_Version >= Ada_2005 and then Synchronized_Present (N) then
4504 Set_Is_Limited_Record (T);
4506 -- Formal derived type case
4508 if Is_Generic_Type (T) then
4510 -- The parent must be a tagged limited type or a synchronized
4511 -- interface.
4513 if (not Is_Tagged_Type (Parent_Type)
4514 or else not Is_Limited_Type (Parent_Type))
4515 and then
4516 (not Is_Interface (Parent_Type)
4517 or else not Is_Synchronized_Interface (Parent_Type))
4518 then
4519 Error_Msg_NE ("parent type of & must be tagged limited " &
4520 "or synchronized", N, T);
4521 end if;
4523 -- The progenitors (if any) must be limited or synchronized
4524 -- interfaces.
4526 if Present (Interfaces (T)) then
4527 declare
4528 Iface : Entity_Id;
4529 Iface_Elmt : Elmt_Id;
4531 begin
4532 Iface_Elmt := First_Elmt (Interfaces (T));
4533 while Present (Iface_Elmt) loop
4534 Iface := Node (Iface_Elmt);
4536 if not Is_Limited_Interface (Iface)
4537 and then not Is_Synchronized_Interface (Iface)
4538 then
4539 Error_Msg_NE ("progenitor & must be limited " &
4540 "or synchronized", N, Iface);
4541 end if;
4543 Next_Elmt (Iface_Elmt);
4544 end loop;
4545 end;
4546 end if;
4548 -- Regular derived extension, the parent must be a limited or
4549 -- synchronized interface.
4551 else
4552 if not Is_Interface (Parent_Type)
4553 or else (not Is_Limited_Interface (Parent_Type)
4554 and then not Is_Synchronized_Interface (Parent_Type))
4555 then
4556 Error_Msg_NE
4557 ("parent type of & must be limited interface", N, T);
4558 end if;
4559 end if;
4561 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
4562 -- extension with a synchronized parent must be explicitly declared
4563 -- synchronized, because the full view will be a synchronized type.
4564 -- This must be checked before the check for limited types below,
4565 -- to ensure that types declared limited are not allowed to extend
4566 -- synchronized interfaces.
4568 elsif Is_Interface (Parent_Type)
4569 and then Is_Synchronized_Interface (Parent_Type)
4570 and then not Synchronized_Present (N)
4571 then
4572 Error_Msg_NE
4573 ("private extension of& must be explicitly synchronized",
4574 N, Parent_Type);
4576 elsif Limited_Present (N) then
4577 Set_Is_Limited_Record (T);
4579 if not Is_Limited_Type (Parent_Type)
4580 and then
4581 (not Is_Interface (Parent_Type)
4582 or else not Is_Limited_Interface (Parent_Type))
4583 then
4584 Error_Msg_NE ("parent type& of limited extension must be limited",
4585 N, Parent_Type);
4586 end if;
4587 end if;
4589 <<Leave>>
4590 if Has_Aspects (N) then
4591 Analyze_Aspect_Specifications (N, T);
4592 end if;
4593 end Analyze_Private_Extension_Declaration;
4595 ---------------------------------
4596 -- Analyze_Subtype_Declaration --
4597 ---------------------------------
4599 procedure Analyze_Subtype_Declaration
4600 (N : Node_Id;
4601 Skip : Boolean := False)
4603 Id : constant Entity_Id := Defining_Identifier (N);
4604 T : Entity_Id;
4605 R_Checks : Check_Result;
4607 begin
4608 -- The subtype declaration may be subject to pragma Ghost with policy
4609 -- Ignore. Set the mode now to ensure that any nodes generated during
4610 -- analysis and expansion are properly flagged as ignored Ghost.
4612 Set_Ghost_Mode (N);
4614 Generate_Definition (Id);
4615 Set_Is_Pure (Id, Is_Pure (Current_Scope));
4616 Init_Size_Align (Id);
4618 -- The following guard condition on Enter_Name is to handle cases where
4619 -- the defining identifier has already been entered into the scope but
4620 -- the declaration as a whole needs to be analyzed.
4622 -- This case in particular happens for derived enumeration types. The
4623 -- derived enumeration type is processed as an inserted enumeration type
4624 -- declaration followed by a rewritten subtype declaration. The defining
4625 -- identifier, however, is entered into the name scope very early in the
4626 -- processing of the original type declaration and therefore needs to be
4627 -- avoided here, when the created subtype declaration is analyzed. (See
4628 -- Build_Derived_Types)
4630 -- This also happens when the full view of a private type is derived
4631 -- type with constraints. In this case the entity has been introduced
4632 -- in the private declaration.
4634 -- Finally this happens in some complex cases when validity checks are
4635 -- enabled, where the same subtype declaration may be analyzed twice.
4636 -- This can happen if the subtype is created by the pre-analysis of
4637 -- an attribute tht gives the range of a loop statement, and the loop
4638 -- itself appears within an if_statement that will be rewritten during
4639 -- expansion.
4641 if Skip
4642 or else (Present (Etype (Id))
4643 and then (Is_Private_Type (Etype (Id))
4644 or else Is_Task_Type (Etype (Id))
4645 or else Is_Rewrite_Substitution (N)))
4646 then
4647 null;
4649 elsif Current_Entity (Id) = Id then
4650 null;
4652 else
4653 Enter_Name (Id);
4654 end if;
4656 T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
4658 -- Class-wide equivalent types of records with unknown discriminants
4659 -- involve the generation of an itype which serves as the private view
4660 -- of a constrained record subtype. In such cases the base type of the
4661 -- current subtype we are processing is the private itype. Use the full
4662 -- of the private itype when decorating various attributes.
4664 if Is_Itype (T)
4665 and then Is_Private_Type (T)
4666 and then Present (Full_View (T))
4667 then
4668 T := Full_View (T);
4669 end if;
4671 -- Inherit common attributes
4673 Set_Is_Volatile (Id, Is_Volatile (T));
4674 Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
4675 Set_Is_Generic_Type (Id, Is_Generic_Type (Base_Type (T)));
4676 Set_Convention (Id, Convention (T));
4678 -- If ancestor has predicates then so does the subtype, and in addition
4679 -- we must delay the freeze to properly arrange predicate inheritance.
4681 -- The Ancestor_Type test is really unpleasant, there seem to be cases
4682 -- in which T = ID, so the above tests and assignments do nothing???
4684 if Has_Predicates (T)
4685 or else (Present (Ancestor_Subtype (T))
4686 and then Has_Predicates (Ancestor_Subtype (T)))
4687 then
4688 Set_Has_Predicates (Id);
4689 Set_Has_Delayed_Freeze (Id);
4690 end if;
4692 -- Subtype of Boolean cannot have a constraint in SPARK
4694 if Is_Boolean_Type (T)
4695 and then Nkind (Subtype_Indication (N)) = N_Subtype_Indication
4696 then
4697 Check_SPARK_05_Restriction
4698 ("subtype of Boolean cannot have constraint", N);
4699 end if;
4701 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
4702 declare
4703 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
4704 One_Cstr : Node_Id;
4705 Low : Node_Id;
4706 High : Node_Id;
4708 begin
4709 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint then
4710 One_Cstr := First (Constraints (Cstr));
4711 while Present (One_Cstr) loop
4713 -- Index or discriminant constraint in SPARK must be a
4714 -- subtype mark.
4716 if not
4717 Nkind_In (One_Cstr, N_Identifier, N_Expanded_Name)
4718 then
4719 Check_SPARK_05_Restriction
4720 ("subtype mark required", One_Cstr);
4722 -- String subtype must have a lower bound of 1 in SPARK.
4723 -- Note that we do not need to test for the non-static case
4724 -- here, since that was already taken care of in
4725 -- Process_Range_Expr_In_Decl.
4727 elsif Base_Type (T) = Standard_String then
4728 Get_Index_Bounds (One_Cstr, Low, High);
4730 if Is_OK_Static_Expression (Low)
4731 and then Expr_Value (Low) /= 1
4732 then
4733 Check_SPARK_05_Restriction
4734 ("String subtype must have lower bound of 1", N);
4735 end if;
4736 end if;
4738 Next (One_Cstr);
4739 end loop;
4740 end if;
4741 end;
4742 end if;
4744 -- In the case where there is no constraint given in the subtype
4745 -- indication, Process_Subtype just returns the Subtype_Mark, so its
4746 -- semantic attributes must be established here.
4748 if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
4749 Set_Etype (Id, Base_Type (T));
4751 -- Subtype of unconstrained array without constraint is not allowed
4752 -- in SPARK.
4754 if Is_Array_Type (T) and then not Is_Constrained (T) then
4755 Check_SPARK_05_Restriction
4756 ("subtype of unconstrained array must have constraint", N);
4757 end if;
4759 case Ekind (T) is
4760 when Array_Kind =>
4761 Set_Ekind (Id, E_Array_Subtype);
4762 Copy_Array_Subtype_Attributes (Id, T);
4764 when Decimal_Fixed_Point_Kind =>
4765 Set_Ekind (Id, E_Decimal_Fixed_Point_Subtype);
4766 Set_Digits_Value (Id, Digits_Value (T));
4767 Set_Delta_Value (Id, Delta_Value (T));
4768 Set_Scale_Value (Id, Scale_Value (T));
4769 Set_Small_Value (Id, Small_Value (T));
4770 Set_Scalar_Range (Id, Scalar_Range (T));
4771 Set_Machine_Radix_10 (Id, Machine_Radix_10 (T));
4772 Set_Is_Constrained (Id, Is_Constrained (T));
4773 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4774 Set_RM_Size (Id, RM_Size (T));
4776 when Enumeration_Kind =>
4777 Set_Ekind (Id, E_Enumeration_Subtype);
4778 Set_First_Literal (Id, First_Literal (Base_Type (T)));
4779 Set_Scalar_Range (Id, Scalar_Range (T));
4780 Set_Is_Character_Type (Id, Is_Character_Type (T));
4781 Set_Is_Constrained (Id, Is_Constrained (T));
4782 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4783 Set_RM_Size (Id, RM_Size (T));
4784 Inherit_Predicate_Flags (Id, T);
4786 when Ordinary_Fixed_Point_Kind =>
4787 Set_Ekind (Id, E_Ordinary_Fixed_Point_Subtype);
4788 Set_Scalar_Range (Id, Scalar_Range (T));
4789 Set_Small_Value (Id, Small_Value (T));
4790 Set_Delta_Value (Id, Delta_Value (T));
4791 Set_Is_Constrained (Id, Is_Constrained (T));
4792 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4793 Set_RM_Size (Id, RM_Size (T));
4795 when Float_Kind =>
4796 Set_Ekind (Id, E_Floating_Point_Subtype);
4797 Set_Scalar_Range (Id, Scalar_Range (T));
4798 Set_Digits_Value (Id, Digits_Value (T));
4799 Set_Is_Constrained (Id, Is_Constrained (T));
4801 when Signed_Integer_Kind =>
4802 Set_Ekind (Id, E_Signed_Integer_Subtype);
4803 Set_Scalar_Range (Id, Scalar_Range (T));
4804 Set_Is_Constrained (Id, Is_Constrained (T));
4805 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4806 Set_RM_Size (Id, RM_Size (T));
4807 Inherit_Predicate_Flags (Id, T);
4809 when Modular_Integer_Kind =>
4810 Set_Ekind (Id, E_Modular_Integer_Subtype);
4811 Set_Scalar_Range (Id, Scalar_Range (T));
4812 Set_Is_Constrained (Id, Is_Constrained (T));
4813 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4814 Set_RM_Size (Id, RM_Size (T));
4815 Inherit_Predicate_Flags (Id, T);
4817 when Class_Wide_Kind =>
4818 Set_Ekind (Id, E_Class_Wide_Subtype);
4819 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
4820 Set_Cloned_Subtype (Id, T);
4821 Set_Is_Tagged_Type (Id, True);
4822 Set_Has_Unknown_Discriminants
4823 (Id, True);
4824 Set_No_Tagged_Streams_Pragma
4825 (Id, No_Tagged_Streams_Pragma (T));
4827 if Ekind (T) = E_Class_Wide_Subtype then
4828 Set_Equivalent_Type (Id, Equivalent_Type (T));
4829 end if;
4831 when E_Record_Type | E_Record_Subtype =>
4832 Set_Ekind (Id, E_Record_Subtype);
4834 if Ekind (T) = E_Record_Subtype
4835 and then Present (Cloned_Subtype (T))
4836 then
4837 Set_Cloned_Subtype (Id, Cloned_Subtype (T));
4838 else
4839 Set_Cloned_Subtype (Id, T);
4840 end if;
4842 Set_First_Entity (Id, First_Entity (T));
4843 Set_Last_Entity (Id, Last_Entity (T));
4844 Set_Has_Discriminants (Id, Has_Discriminants (T));
4845 Set_Is_Constrained (Id, Is_Constrained (T));
4846 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
4847 Set_Has_Implicit_Dereference
4848 (Id, Has_Implicit_Dereference (T));
4849 Set_Has_Unknown_Discriminants
4850 (Id, Has_Unknown_Discriminants (T));
4852 if Has_Discriminants (T) then
4853 Set_Discriminant_Constraint
4854 (Id, Discriminant_Constraint (T));
4855 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
4857 elsif Has_Unknown_Discriminants (Id) then
4858 Set_Discriminant_Constraint (Id, No_Elist);
4859 end if;
4861 if Is_Tagged_Type (T) then
4862 Set_Is_Tagged_Type (Id, True);
4863 Set_No_Tagged_Streams_Pragma
4864 (Id, No_Tagged_Streams_Pragma (T));
4865 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
4866 Set_Direct_Primitive_Operations
4867 (Id, Direct_Primitive_Operations (T));
4868 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
4870 if Is_Interface (T) then
4871 Set_Is_Interface (Id);
4872 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
4873 end if;
4874 end if;
4876 when Private_Kind =>
4877 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
4878 Set_Has_Discriminants (Id, Has_Discriminants (T));
4879 Set_Is_Constrained (Id, Is_Constrained (T));
4880 Set_First_Entity (Id, First_Entity (T));
4881 Set_Last_Entity (Id, Last_Entity (T));
4882 Set_Private_Dependents (Id, New_Elmt_List);
4883 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
4884 Set_Has_Implicit_Dereference
4885 (Id, Has_Implicit_Dereference (T));
4886 Set_Has_Unknown_Discriminants
4887 (Id, Has_Unknown_Discriminants (T));
4888 Set_Known_To_Have_Preelab_Init
4889 (Id, Known_To_Have_Preelab_Init (T));
4891 if Is_Tagged_Type (T) then
4892 Set_Is_Tagged_Type (Id);
4893 Set_No_Tagged_Streams_Pragma (Id,
4894 No_Tagged_Streams_Pragma (T));
4895 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
4896 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
4897 Set_Direct_Primitive_Operations (Id,
4898 Direct_Primitive_Operations (T));
4899 end if;
4901 -- In general the attributes of the subtype of a private type
4902 -- are the attributes of the partial view of parent. However,
4903 -- the full view may be a discriminated type, and the subtype
4904 -- must share the discriminant constraint to generate correct
4905 -- calls to initialization procedures.
4907 if Has_Discriminants (T) then
4908 Set_Discriminant_Constraint
4909 (Id, Discriminant_Constraint (T));
4910 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
4912 elsif Present (Full_View (T))
4913 and then Has_Discriminants (Full_View (T))
4914 then
4915 Set_Discriminant_Constraint
4916 (Id, Discriminant_Constraint (Full_View (T)));
4917 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
4919 -- This would seem semantically correct, but apparently
4920 -- generates spurious errors about missing components ???
4922 -- Set_Has_Discriminants (Id);
4923 end if;
4925 Prepare_Private_Subtype_Completion (Id, N);
4927 -- If this is the subtype of a constrained private type with
4928 -- discriminants that has got a full view and we also have
4929 -- built a completion just above, show that the completion
4930 -- is a clone of the full view to the back-end.
4932 if Has_Discriminants (T)
4933 and then not Has_Unknown_Discriminants (T)
4934 and then not Is_Empty_Elmt_List (Discriminant_Constraint (T))
4935 and then Present (Full_View (T))
4936 and then Present (Full_View (Id))
4937 then
4938 Set_Cloned_Subtype (Full_View (Id), Full_View (T));
4939 end if;
4941 when Access_Kind =>
4942 Set_Ekind (Id, E_Access_Subtype);
4943 Set_Is_Constrained (Id, Is_Constrained (T));
4944 Set_Is_Access_Constant
4945 (Id, Is_Access_Constant (T));
4946 Set_Directly_Designated_Type
4947 (Id, Designated_Type (T));
4948 Set_Can_Never_Be_Null (Id, Can_Never_Be_Null (T));
4950 -- A Pure library_item must not contain the declaration of a
4951 -- named access type, except within a subprogram, generic
4952 -- subprogram, task unit, or protected unit, or if it has
4953 -- a specified Storage_Size of zero (RM05-10.2.1(15.4-15.5)).
4955 if Comes_From_Source (Id)
4956 and then In_Pure_Unit
4957 and then not In_Subprogram_Task_Protected_Unit
4958 and then not No_Pool_Assigned (Id)
4959 then
4960 Error_Msg_N
4961 ("named access types not allowed in pure unit", N);
4962 end if;
4964 when Concurrent_Kind =>
4965 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
4966 Set_Corresponding_Record_Type (Id,
4967 Corresponding_Record_Type (T));
4968 Set_First_Entity (Id, First_Entity (T));
4969 Set_First_Private_Entity (Id, First_Private_Entity (T));
4970 Set_Has_Discriminants (Id, Has_Discriminants (T));
4971 Set_Is_Constrained (Id, Is_Constrained (T));
4972 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
4973 Set_Last_Entity (Id, Last_Entity (T));
4975 if Is_Tagged_Type (T) then
4976 Set_No_Tagged_Streams_Pragma
4977 (Id, No_Tagged_Streams_Pragma (T));
4978 end if;
4980 if Has_Discriminants (T) then
4981 Set_Discriminant_Constraint (Id,
4982 Discriminant_Constraint (T));
4983 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
4984 end if;
4986 when E_Incomplete_Type =>
4987 if Ada_Version >= Ada_2005 then
4989 -- In Ada 2005 an incomplete type can be explicitly tagged:
4990 -- propagate indication.
4992 Set_Ekind (Id, E_Incomplete_Subtype);
4993 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
4994 Set_Private_Dependents (Id, New_Elmt_List);
4996 if Is_Tagged_Type (Id) then
4997 Set_No_Tagged_Streams_Pragma
4998 (Id, No_Tagged_Streams_Pragma (T));
4999 end if;
5001 -- Ada 2005 (AI-412): Decorate an incomplete subtype of an
5002 -- incomplete type visible through a limited with clause.
5004 if From_Limited_With (T)
5005 and then Present (Non_Limited_View (T))
5006 then
5007 Set_From_Limited_With (Id);
5008 Set_Non_Limited_View (Id, Non_Limited_View (T));
5010 -- Ada 2005 (AI-412): Add the regular incomplete subtype
5011 -- to the private dependents of the original incomplete
5012 -- type for future transformation.
5014 else
5015 Append_Elmt (Id, Private_Dependents (T));
5016 end if;
5018 -- If the subtype name denotes an incomplete type an error
5019 -- was already reported by Process_Subtype.
5021 else
5022 Set_Etype (Id, Any_Type);
5023 end if;
5025 when others =>
5026 raise Program_Error;
5027 end case;
5028 end if;
5030 if Etype (Id) = Any_Type then
5031 goto Leave;
5032 end if;
5034 -- Some common processing on all types
5036 Set_Size_Info (Id, T);
5037 Set_First_Rep_Item (Id, First_Rep_Item (T));
5039 -- If the parent type is a generic actual, so is the subtype. This may
5040 -- happen in a nested instance. Why Comes_From_Source test???
5042 if not Comes_From_Source (N) then
5043 Set_Is_Generic_Actual_Type (Id, Is_Generic_Actual_Type (T));
5044 end if;
5046 T := Etype (Id);
5048 Set_Is_Immediately_Visible (Id, True);
5049 Set_Depends_On_Private (Id, Has_Private_Component (T));
5050 Set_Is_Descendent_Of_Address (Id, Is_Descendent_Of_Address (T));
5052 if Is_Interface (T) then
5053 Set_Is_Interface (Id);
5054 end if;
5056 if Present (Generic_Parent_Type (N))
5057 and then
5058 (Nkind (Parent (Generic_Parent_Type (N))) /=
5059 N_Formal_Type_Declaration
5060 or else Nkind (Formal_Type_Definition
5061 (Parent (Generic_Parent_Type (N)))) /=
5062 N_Formal_Private_Type_Definition)
5063 then
5064 if Is_Tagged_Type (Id) then
5066 -- If this is a generic actual subtype for a synchronized type,
5067 -- the primitive operations are those of the corresponding record
5068 -- for which there is a separate subtype declaration.
5070 if Is_Concurrent_Type (Id) then
5071 null;
5072 elsif Is_Class_Wide_Type (Id) then
5073 Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
5074 else
5075 Derive_Subprograms (Generic_Parent_Type (N), Id, T);
5076 end if;
5078 elsif Scope (Etype (Id)) /= Standard_Standard then
5079 Derive_Subprograms (Generic_Parent_Type (N), Id);
5080 end if;
5081 end if;
5083 if Is_Private_Type (T) and then Present (Full_View (T)) then
5084 Conditional_Delay (Id, Full_View (T));
5086 -- The subtypes of components or subcomponents of protected types
5087 -- do not need freeze nodes, which would otherwise appear in the
5088 -- wrong scope (before the freeze node for the protected type). The
5089 -- proper subtypes are those of the subcomponents of the corresponding
5090 -- record.
5092 elsif Ekind (Scope (Id)) /= E_Protected_Type
5093 and then Present (Scope (Scope (Id))) -- error defense
5094 and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
5095 then
5096 Conditional_Delay (Id, T);
5097 end if;
5099 -- Check that Constraint_Error is raised for a scalar subtype indication
5100 -- when the lower or upper bound of a non-null range lies outside the
5101 -- range of the type mark.
5103 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5104 if Is_Scalar_Type (Etype (Id))
5105 and then Scalar_Range (Id) /=
5106 Scalar_Range (Etype (Subtype_Mark
5107 (Subtype_Indication (N))))
5108 then
5109 Apply_Range_Check
5110 (Scalar_Range (Id),
5111 Etype (Subtype_Mark (Subtype_Indication (N))));
5113 -- In the array case, check compatibility for each index
5115 elsif Is_Array_Type (Etype (Id)) and then Present (First_Index (Id))
5116 then
5117 -- This really should be a subprogram that finds the indications
5118 -- to check???
5120 declare
5121 Subt_Index : Node_Id := First_Index (Id);
5122 Target_Index : Node_Id :=
5123 First_Index (Etype
5124 (Subtype_Mark (Subtype_Indication (N))));
5125 Has_Dyn_Chk : Boolean := Has_Dynamic_Range_Check (N);
5127 begin
5128 while Present (Subt_Index) loop
5129 if ((Nkind (Subt_Index) = N_Identifier
5130 and then Ekind (Entity (Subt_Index)) in Scalar_Kind)
5131 or else Nkind (Subt_Index) = N_Subtype_Indication)
5132 and then
5133 Nkind (Scalar_Range (Etype (Subt_Index))) = N_Range
5134 then
5135 declare
5136 Target_Typ : constant Entity_Id :=
5137 Etype (Target_Index);
5138 begin
5139 R_Checks :=
5140 Get_Range_Checks
5141 (Scalar_Range (Etype (Subt_Index)),
5142 Target_Typ,
5143 Etype (Subt_Index),
5144 Defining_Identifier (N));
5146 -- Reset Has_Dynamic_Range_Check on the subtype to
5147 -- prevent elision of the index check due to a dynamic
5148 -- check generated for a preceding index (needed since
5149 -- Insert_Range_Checks tries to avoid generating
5150 -- redundant checks on a given declaration).
5152 Set_Has_Dynamic_Range_Check (N, False);
5154 Insert_Range_Checks
5155 (R_Checks,
5157 Target_Typ,
5158 Sloc (Defining_Identifier (N)));
5160 -- Record whether this index involved a dynamic check
5162 Has_Dyn_Chk :=
5163 Has_Dyn_Chk or else Has_Dynamic_Range_Check (N);
5164 end;
5165 end if;
5167 Next_Index (Subt_Index);
5168 Next_Index (Target_Index);
5169 end loop;
5171 -- Finally, mark whether the subtype involves dynamic checks
5173 Set_Has_Dynamic_Range_Check (N, Has_Dyn_Chk);
5174 end;
5175 end if;
5176 end if;
5178 -- A type invariant applies to any subtype in its scope, in particular
5179 -- to a generic actual.
5181 if Has_Invariants (T) and then In_Open_Scopes (Scope (T)) then
5182 Set_Has_Invariants (Id);
5183 Set_Invariant_Procedure (Id, Invariant_Procedure (T));
5184 end if;
5186 -- Make sure that generic actual types are properly frozen. The subtype
5187 -- is marked as a generic actual type when the enclosing instance is
5188 -- analyzed, so here we identify the subtype from the tree structure.
5190 if Expander_Active
5191 and then Is_Generic_Actual_Type (Id)
5192 and then In_Instance
5193 and then not Comes_From_Source (N)
5194 and then Nkind (Subtype_Indication (N)) /= N_Subtype_Indication
5195 and then Is_Frozen (T)
5196 then
5197 Freeze_Before (N, Id);
5198 end if;
5200 Set_Optimize_Alignment_Flags (Id);
5201 Check_Eliminated (Id);
5203 <<Leave>>
5204 if Has_Aspects (N) then
5205 Analyze_Aspect_Specifications (N, Id);
5206 end if;
5208 Analyze_Dimension (N);
5209 end Analyze_Subtype_Declaration;
5211 --------------------------------
5212 -- Analyze_Subtype_Indication --
5213 --------------------------------
5215 procedure Analyze_Subtype_Indication (N : Node_Id) is
5216 T : constant Entity_Id := Subtype_Mark (N);
5217 R : constant Node_Id := Range_Expression (Constraint (N));
5219 begin
5220 Analyze (T);
5222 if R /= Error then
5223 Analyze (R);
5224 Set_Etype (N, Etype (R));
5225 Resolve (R, Entity (T));
5226 else
5227 Set_Error_Posted (R);
5228 Set_Error_Posted (T);
5229 end if;
5230 end Analyze_Subtype_Indication;
5232 --------------------------
5233 -- Analyze_Variant_Part --
5234 --------------------------
5236 procedure Analyze_Variant_Part (N : Node_Id) is
5237 Discr_Name : Node_Id;
5238 Discr_Type : Entity_Id;
5240 procedure Process_Variant (A : Node_Id);
5241 -- Analyze declarations for a single variant
5243 package Analyze_Variant_Choices is
5244 new Generic_Analyze_Choices (Process_Variant);
5245 use Analyze_Variant_Choices;
5247 ---------------------
5248 -- Process_Variant --
5249 ---------------------
5251 procedure Process_Variant (A : Node_Id) is
5252 CL : constant Node_Id := Component_List (A);
5253 begin
5254 if not Null_Present (CL) then
5255 Analyze_Declarations (Component_Items (CL));
5257 if Present (Variant_Part (CL)) then
5258 Analyze (Variant_Part (CL));
5259 end if;
5260 end if;
5261 end Process_Variant;
5263 -- Start of processing for Analyze_Variant_Part
5265 begin
5266 Discr_Name := Name (N);
5267 Analyze (Discr_Name);
5269 -- If Discr_Name bad, get out (prevent cascaded errors)
5271 if Etype (Discr_Name) = Any_Type then
5272 return;
5273 end if;
5275 -- Check invalid discriminant in variant part
5277 if Ekind (Entity (Discr_Name)) /= E_Discriminant then
5278 Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
5279 end if;
5281 Discr_Type := Etype (Entity (Discr_Name));
5283 if not Is_Discrete_Type (Discr_Type) then
5284 Error_Msg_N
5285 ("discriminant in a variant part must be of a discrete type",
5286 Name (N));
5287 return;
5288 end if;
5290 -- Now analyze the choices, which also analyzes the declarations that
5291 -- are associated with each choice.
5293 Analyze_Choices (Variants (N), Discr_Type);
5295 -- Note: we used to instantiate and call Check_Choices here to check
5296 -- that the choices covered the discriminant, but it's too early to do
5297 -- that because of statically predicated subtypes, whose analysis may
5298 -- be deferred to their freeze point which may be as late as the freeze
5299 -- point of the containing record. So this call is now to be found in
5300 -- Freeze_Record_Declaration.
5302 end Analyze_Variant_Part;
5304 ----------------------------
5305 -- Array_Type_Declaration --
5306 ----------------------------
5308 procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
5309 Component_Def : constant Node_Id := Component_Definition (Def);
5310 Component_Typ : constant Node_Id := Subtype_Indication (Component_Def);
5311 Element_Type : Entity_Id;
5312 Implicit_Base : Entity_Id;
5313 Index : Node_Id;
5314 Related_Id : Entity_Id := Empty;
5315 Nb_Index : Nat;
5316 P : constant Node_Id := Parent (Def);
5317 Priv : Entity_Id;
5319 begin
5320 if Nkind (Def) = N_Constrained_Array_Definition then
5321 Index := First (Discrete_Subtype_Definitions (Def));
5322 else
5323 Index := First (Subtype_Marks (Def));
5324 end if;
5326 -- Find proper names for the implicit types which may be public. In case
5327 -- of anonymous arrays we use the name of the first object of that type
5328 -- as prefix.
5330 if No (T) then
5331 Related_Id := Defining_Identifier (P);
5332 else
5333 Related_Id := T;
5334 end if;
5336 Nb_Index := 1;
5337 while Present (Index) loop
5338 Analyze (Index);
5340 -- Test for odd case of trying to index a type by the type itself
5342 if Is_Entity_Name (Index) and then Entity (Index) = T then
5343 Error_Msg_N ("type& cannot be indexed by itself", Index);
5344 Set_Entity (Index, Standard_Boolean);
5345 Set_Etype (Index, Standard_Boolean);
5346 end if;
5348 -- Check SPARK restriction requiring a subtype mark
5350 if not Nkind_In (Index, N_Identifier, N_Expanded_Name) then
5351 Check_SPARK_05_Restriction ("subtype mark required", Index);
5352 end if;
5354 -- Add a subtype declaration for each index of private array type
5355 -- declaration whose etype is also private. For example:
5357 -- package Pkg is
5358 -- type Index is private;
5359 -- private
5360 -- type Table is array (Index) of ...
5361 -- end;
5363 -- This is currently required by the expander for the internally
5364 -- generated equality subprogram of records with variant parts in
5365 -- which the etype of some component is such private type.
5367 if Ekind (Current_Scope) = E_Package
5368 and then In_Private_Part (Current_Scope)
5369 and then Has_Private_Declaration (Etype (Index))
5370 then
5371 declare
5372 Loc : constant Source_Ptr := Sloc (Def);
5373 New_E : Entity_Id;
5374 Decl : Entity_Id;
5376 begin
5377 New_E := Make_Temporary (Loc, 'T');
5378 Set_Is_Internal (New_E);
5380 Decl :=
5381 Make_Subtype_Declaration (Loc,
5382 Defining_Identifier => New_E,
5383 Subtype_Indication =>
5384 New_Occurrence_Of (Etype (Index), Loc));
5386 Insert_Before (Parent (Def), Decl);
5387 Analyze (Decl);
5388 Set_Etype (Index, New_E);
5390 -- If the index is a range the Entity attribute is not
5391 -- available. Example:
5393 -- package Pkg is
5394 -- type T is private;
5395 -- private
5396 -- type T is new Natural;
5397 -- Table : array (T(1) .. T(10)) of Boolean;
5398 -- end Pkg;
5400 if Nkind (Index) /= N_Range then
5401 Set_Entity (Index, New_E);
5402 end if;
5403 end;
5404 end if;
5406 Make_Index (Index, P, Related_Id, Nb_Index);
5408 -- Check error of subtype with predicate for index type
5410 Bad_Predicated_Subtype_Use
5411 ("subtype& has predicate, not allowed as index subtype",
5412 Index, Etype (Index));
5414 -- Move to next index
5416 Next_Index (Index);
5417 Nb_Index := Nb_Index + 1;
5418 end loop;
5420 -- Process subtype indication if one is present
5422 if Present (Component_Typ) then
5423 Element_Type := Process_Subtype (Component_Typ, P, Related_Id, 'C');
5425 Set_Etype (Component_Typ, Element_Type);
5427 if not Nkind_In (Component_Typ, N_Identifier, N_Expanded_Name) then
5428 Check_SPARK_05_Restriction
5429 ("subtype mark required", Component_Typ);
5430 end if;
5432 -- Ada 2005 (AI-230): Access Definition case
5434 else pragma Assert (Present (Access_Definition (Component_Def)));
5436 -- Indicate that the anonymous access type is created by the
5437 -- array type declaration.
5439 Element_Type := Access_Definition
5440 (Related_Nod => P,
5441 N => Access_Definition (Component_Def));
5442 Set_Is_Local_Anonymous_Access (Element_Type);
5444 -- Propagate the parent. This field is needed if we have to generate
5445 -- the master_id associated with an anonymous access to task type
5446 -- component (see Expand_N_Full_Type_Declaration.Build_Master)
5448 Set_Parent (Element_Type, Parent (T));
5450 -- Ada 2005 (AI-230): In case of components that are anonymous access
5451 -- types the level of accessibility depends on the enclosing type
5452 -- declaration
5454 Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
5456 -- Ada 2005 (AI-254)
5458 declare
5459 CD : constant Node_Id :=
5460 Access_To_Subprogram_Definition
5461 (Access_Definition (Component_Def));
5462 begin
5463 if Present (CD) and then Protected_Present (CD) then
5464 Element_Type :=
5465 Replace_Anonymous_Access_To_Protected_Subprogram (Def);
5466 end if;
5467 end;
5468 end if;
5470 -- Constrained array case
5472 if No (T) then
5473 T := Create_Itype (E_Void, P, Related_Id, 'T');
5474 end if;
5476 if Nkind (Def) = N_Constrained_Array_Definition then
5478 -- Establish Implicit_Base as unconstrained base type
5480 Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
5482 Set_Etype (Implicit_Base, Implicit_Base);
5483 Set_Scope (Implicit_Base, Current_Scope);
5484 Set_Has_Delayed_Freeze (Implicit_Base);
5485 Set_Default_SSO (Implicit_Base);
5487 -- The constrained array type is a subtype of the unconstrained one
5489 Set_Ekind (T, E_Array_Subtype);
5490 Init_Size_Align (T);
5491 Set_Etype (T, Implicit_Base);
5492 Set_Scope (T, Current_Scope);
5493 Set_Is_Constrained (T);
5494 Set_First_Index (T,
5495 First (Discrete_Subtype_Definitions (Def)));
5496 Set_Has_Delayed_Freeze (T);
5498 -- Complete setup of implicit base type
5500 Set_First_Index (Implicit_Base, First_Index (T));
5501 Set_Component_Type (Implicit_Base, Element_Type);
5502 Set_Has_Task (Implicit_Base, Has_Task (Element_Type));
5503 Set_Has_Protected (Implicit_Base, Has_Protected (Element_Type));
5504 Set_Component_Size (Implicit_Base, Uint_0);
5505 Set_Packed_Array_Impl_Type (Implicit_Base, Empty);
5506 Set_Has_Controlled_Component (Implicit_Base,
5507 Has_Controlled_Component (Element_Type)
5508 or else Is_Controlled (Element_Type));
5509 Set_Finalize_Storage_Only (Implicit_Base,
5510 Finalize_Storage_Only (Element_Type));
5512 -- Inherit the "ghostness" from the constrained array type
5514 if Is_Ghost_Entity (T) or else Ghost_Mode > None then
5515 Set_Is_Ghost_Entity (Implicit_Base);
5516 end if;
5518 -- Unconstrained array case
5520 else
5521 Set_Ekind (T, E_Array_Type);
5522 Init_Size_Align (T);
5523 Set_Etype (T, T);
5524 Set_Scope (T, Current_Scope);
5525 Set_Component_Size (T, Uint_0);
5526 Set_Is_Constrained (T, False);
5527 Set_First_Index (T, First (Subtype_Marks (Def)));
5528 Set_Has_Delayed_Freeze (T, True);
5529 Set_Has_Task (T, Has_Task (Element_Type));
5530 Set_Has_Protected (T, Has_Protected (Element_Type));
5531 Set_Has_Controlled_Component (T, Has_Controlled_Component
5532 (Element_Type)
5533 or else
5534 Is_Controlled (Element_Type));
5535 Set_Finalize_Storage_Only (T, Finalize_Storage_Only
5536 (Element_Type));
5537 Set_Default_SSO (T);
5538 end if;
5540 -- Common attributes for both cases
5542 Set_Component_Type (Base_Type (T), Element_Type);
5543 Set_Packed_Array_Impl_Type (T, Empty);
5545 if Aliased_Present (Component_Definition (Def)) then
5546 Check_SPARK_05_Restriction
5547 ("aliased is not allowed", Component_Definition (Def));
5548 Set_Has_Aliased_Components (Etype (T));
5549 end if;
5551 -- Ada 2005 (AI-231): Propagate the null-excluding attribute to the
5552 -- array type to ensure that objects of this type are initialized.
5554 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (Element_Type) then
5555 Set_Can_Never_Be_Null (T);
5557 if Null_Exclusion_Present (Component_Definition (Def))
5559 -- No need to check itypes because in their case this check was
5560 -- done at their point of creation
5562 and then not Is_Itype (Element_Type)
5563 then
5564 Error_Msg_N
5565 ("`NOT NULL` not allowed (null already excluded)",
5566 Subtype_Indication (Component_Definition (Def)));
5567 end if;
5568 end if;
5570 Priv := Private_Component (Element_Type);
5572 if Present (Priv) then
5574 -- Check for circular definitions
5576 if Priv = Any_Type then
5577 Set_Component_Type (Etype (T), Any_Type);
5579 -- There is a gap in the visibility of operations on the composite
5580 -- type only if the component type is defined in a different scope.
5582 elsif Scope (Priv) = Current_Scope then
5583 null;
5585 elsif Is_Limited_Type (Priv) then
5586 Set_Is_Limited_Composite (Etype (T));
5587 Set_Is_Limited_Composite (T);
5588 else
5589 Set_Is_Private_Composite (Etype (T));
5590 Set_Is_Private_Composite (T);
5591 end if;
5592 end if;
5594 -- A syntax error in the declaration itself may lead to an empty index
5595 -- list, in which case do a minimal patch.
5597 if No (First_Index (T)) then
5598 Error_Msg_N ("missing index definition in array type declaration", T);
5600 declare
5601 Indexes : constant List_Id :=
5602 New_List (New_Occurrence_Of (Any_Id, Sloc (T)));
5603 begin
5604 Set_Discrete_Subtype_Definitions (Def, Indexes);
5605 Set_First_Index (T, First (Indexes));
5606 return;
5607 end;
5608 end if;
5610 -- Create a concatenation operator for the new type. Internal array
5611 -- types created for packed entities do not need such, they are
5612 -- compatible with the user-defined type.
5614 if Number_Dimensions (T) = 1
5615 and then not Is_Packed_Array_Impl_Type (T)
5616 then
5617 New_Concatenation_Op (T);
5618 end if;
5620 -- In the case of an unconstrained array the parser has already verified
5621 -- that all the indexes are unconstrained but we still need to make sure
5622 -- that the element type is constrained.
5624 if Is_Indefinite_Subtype (Element_Type) then
5625 Error_Msg_N
5626 ("unconstrained element type in array declaration",
5627 Subtype_Indication (Component_Def));
5629 elsif Is_Abstract_Type (Element_Type) then
5630 Error_Msg_N
5631 ("the type of a component cannot be abstract",
5632 Subtype_Indication (Component_Def));
5633 end if;
5635 -- There may be an invariant declared for the component type, but
5636 -- the construction of the component invariant checking procedure
5637 -- takes place during expansion.
5638 end Array_Type_Declaration;
5640 ------------------------------------------------------
5641 -- Replace_Anonymous_Access_To_Protected_Subprogram --
5642 ------------------------------------------------------
5644 function Replace_Anonymous_Access_To_Protected_Subprogram
5645 (N : Node_Id) return Entity_Id
5647 Loc : constant Source_Ptr := Sloc (N);
5649 Curr_Scope : constant Scope_Stack_Entry :=
5650 Scope_Stack.Table (Scope_Stack.Last);
5652 Anon : constant Entity_Id := Make_Temporary (Loc, 'S');
5654 Acc : Node_Id;
5655 -- Access definition in declaration
5657 Comp : Node_Id;
5658 -- Object definition or formal definition with an access definition
5660 Decl : Node_Id;
5661 -- Declaration of anonymous access to subprogram type
5663 Spec : Node_Id;
5664 -- Original specification in access to subprogram
5666 P : Node_Id;
5668 begin
5669 Set_Is_Internal (Anon);
5671 case Nkind (N) is
5672 when N_Component_Declaration |
5673 N_Unconstrained_Array_Definition |
5674 N_Constrained_Array_Definition =>
5675 Comp := Component_Definition (N);
5676 Acc := Access_Definition (Comp);
5678 when N_Discriminant_Specification =>
5679 Comp := Discriminant_Type (N);
5680 Acc := Comp;
5682 when N_Parameter_Specification =>
5683 Comp := Parameter_Type (N);
5684 Acc := Comp;
5686 when N_Access_Function_Definition =>
5687 Comp := Result_Definition (N);
5688 Acc := Comp;
5690 when N_Object_Declaration =>
5691 Comp := Object_Definition (N);
5692 Acc := Comp;
5694 when N_Function_Specification =>
5695 Comp := Result_Definition (N);
5696 Acc := Comp;
5698 when others =>
5699 raise Program_Error;
5700 end case;
5702 Spec := Access_To_Subprogram_Definition (Acc);
5704 Decl :=
5705 Make_Full_Type_Declaration (Loc,
5706 Defining_Identifier => Anon,
5707 Type_Definition => Copy_Separate_Tree (Spec));
5709 Mark_Rewrite_Insertion (Decl);
5711 -- In ASIS mode, analyze the profile on the original node, because
5712 -- the separate copy does not provide enough links to recover the
5713 -- original tree. Analysis is limited to type annotations, within
5714 -- a temporary scope that serves as an anonymous subprogram to collect
5715 -- otherwise useless temporaries and itypes.
5717 if ASIS_Mode then
5718 declare
5719 Typ : constant Entity_Id := Make_Temporary (Loc, 'S');
5721 begin
5722 if Nkind (Spec) = N_Access_Function_Definition then
5723 Set_Ekind (Typ, E_Function);
5724 else
5725 Set_Ekind (Typ, E_Procedure);
5726 end if;
5728 Set_Parent (Typ, N);
5729 Set_Scope (Typ, Current_Scope);
5730 Push_Scope (Typ);
5732 Process_Formals (Parameter_Specifications (Spec), Spec);
5734 if Nkind (Spec) = N_Access_Function_Definition then
5735 declare
5736 Def : constant Node_Id := Result_Definition (Spec);
5738 begin
5739 -- The result might itself be an anonymous access type, so
5740 -- have to recurse.
5742 if Nkind (Def) = N_Access_Definition then
5743 if Present (Access_To_Subprogram_Definition (Def)) then
5744 Set_Etype
5745 (Def,
5746 Replace_Anonymous_Access_To_Protected_Subprogram
5747 (Spec));
5748 else
5749 Find_Type (Subtype_Mark (Def));
5750 end if;
5752 else
5753 Find_Type (Def);
5754 end if;
5755 end;
5756 end if;
5758 End_Scope;
5759 end;
5760 end if;
5762 -- Insert the new declaration in the nearest enclosing scope. If the
5763 -- node is a body and N is its return type, the declaration belongs in
5764 -- the enclosing scope.
5766 P := Parent (N);
5768 if Nkind (P) = N_Subprogram_Body
5769 and then Nkind (N) = N_Function_Specification
5770 then
5771 P := Parent (P);
5772 end if;
5774 while Present (P) and then not Has_Declarations (P) loop
5775 P := Parent (P);
5776 end loop;
5778 pragma Assert (Present (P));
5780 if Nkind (P) = N_Package_Specification then
5781 Prepend (Decl, Visible_Declarations (P));
5782 else
5783 Prepend (Decl, Declarations (P));
5784 end if;
5786 -- Replace the anonymous type with an occurrence of the new declaration.
5787 -- In all cases the rewritten node does not have the null-exclusion
5788 -- attribute because (if present) it was already inherited by the
5789 -- anonymous entity (Anon). Thus, in case of components we do not
5790 -- inherit this attribute.
5792 if Nkind (N) = N_Parameter_Specification then
5793 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5794 Set_Etype (Defining_Identifier (N), Anon);
5795 Set_Null_Exclusion_Present (N, False);
5797 elsif Nkind (N) = N_Object_Declaration then
5798 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5799 Set_Etype (Defining_Identifier (N), Anon);
5801 elsif Nkind (N) = N_Access_Function_Definition then
5802 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5804 elsif Nkind (N) = N_Function_Specification then
5805 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5806 Set_Etype (Defining_Unit_Name (N), Anon);
5808 else
5809 Rewrite (Comp,
5810 Make_Component_Definition (Loc,
5811 Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
5812 end if;
5814 Mark_Rewrite_Insertion (Comp);
5816 if Nkind_In (N, N_Object_Declaration, N_Access_Function_Definition) then
5817 Analyze (Decl);
5819 else
5820 -- Temporarily remove the current scope (record or subprogram) from
5821 -- the stack to add the new declarations to the enclosing scope.
5823 Scope_Stack.Decrement_Last;
5824 Analyze (Decl);
5825 Set_Is_Itype (Anon);
5826 Scope_Stack.Append (Curr_Scope);
5827 end if;
5829 Set_Ekind (Anon, E_Anonymous_Access_Protected_Subprogram_Type);
5830 Set_Can_Use_Internal_Rep (Anon, not Always_Compatible_Rep_On_Target);
5831 return Anon;
5832 end Replace_Anonymous_Access_To_Protected_Subprogram;
5834 -------------------------------
5835 -- Build_Derived_Access_Type --
5836 -------------------------------
5838 procedure Build_Derived_Access_Type
5839 (N : Node_Id;
5840 Parent_Type : Entity_Id;
5841 Derived_Type : Entity_Id)
5843 S : constant Node_Id := Subtype_Indication (Type_Definition (N));
5845 Desig_Type : Entity_Id;
5846 Discr : Entity_Id;
5847 Discr_Con_Elist : Elist_Id;
5848 Discr_Con_El : Elmt_Id;
5849 Subt : Entity_Id;
5851 begin
5852 -- Set the designated type so it is available in case this is an access
5853 -- to a self-referential type, e.g. a standard list type with a next
5854 -- pointer. Will be reset after subtype is built.
5856 Set_Directly_Designated_Type
5857 (Derived_Type, Designated_Type (Parent_Type));
5859 Subt := Process_Subtype (S, N);
5861 if Nkind (S) /= N_Subtype_Indication
5862 and then Subt /= Base_Type (Subt)
5863 then
5864 Set_Ekind (Derived_Type, E_Access_Subtype);
5865 end if;
5867 if Ekind (Derived_Type) = E_Access_Subtype then
5868 declare
5869 Pbase : constant Entity_Id := Base_Type (Parent_Type);
5870 Ibase : constant Entity_Id :=
5871 Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
5872 Svg_Chars : constant Name_Id := Chars (Ibase);
5873 Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
5875 begin
5876 Copy_Node (Pbase, Ibase);
5878 Set_Chars (Ibase, Svg_Chars);
5879 Set_Next_Entity (Ibase, Svg_Next_E);
5880 Set_Sloc (Ibase, Sloc (Derived_Type));
5881 Set_Scope (Ibase, Scope (Derived_Type));
5882 Set_Freeze_Node (Ibase, Empty);
5883 Set_Is_Frozen (Ibase, False);
5884 Set_Comes_From_Source (Ibase, False);
5885 Set_Is_First_Subtype (Ibase, False);
5887 Set_Etype (Ibase, Pbase);
5888 Set_Etype (Derived_Type, Ibase);
5889 end;
5890 end if;
5892 Set_Directly_Designated_Type
5893 (Derived_Type, Designated_Type (Subt));
5895 Set_Is_Constrained (Derived_Type, Is_Constrained (Subt));
5896 Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
5897 Set_Size_Info (Derived_Type, Parent_Type);
5898 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
5899 Set_Depends_On_Private (Derived_Type,
5900 Has_Private_Component (Derived_Type));
5901 Conditional_Delay (Derived_Type, Subt);
5903 -- Ada 2005 (AI-231): Set the null-exclusion attribute, and verify
5904 -- that it is not redundant.
5906 if Null_Exclusion_Present (Type_Definition (N)) then
5907 Set_Can_Never_Be_Null (Derived_Type);
5909 -- What is with the "AND THEN FALSE" here ???
5911 if Can_Never_Be_Null (Parent_Type)
5912 and then False
5913 then
5914 Error_Msg_NE
5915 ("`NOT NULL` not allowed (& already excludes null)",
5916 N, Parent_Type);
5917 end if;
5919 elsif Can_Never_Be_Null (Parent_Type) then
5920 Set_Can_Never_Be_Null (Derived_Type);
5921 end if;
5923 -- Note: we do not copy the Storage_Size_Variable, since we always go to
5924 -- the root type for this information.
5926 -- Apply range checks to discriminants for derived record case
5927 -- ??? THIS CODE SHOULD NOT BE HERE REALLY.
5929 Desig_Type := Designated_Type (Derived_Type);
5930 if Is_Composite_Type (Desig_Type)
5931 and then (not Is_Array_Type (Desig_Type))
5932 and then Has_Discriminants (Desig_Type)
5933 and then Base_Type (Desig_Type) /= Desig_Type
5934 then
5935 Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
5936 Discr_Con_El := First_Elmt (Discr_Con_Elist);
5938 Discr := First_Discriminant (Base_Type (Desig_Type));
5939 while Present (Discr_Con_El) loop
5940 Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
5941 Next_Elmt (Discr_Con_El);
5942 Next_Discriminant (Discr);
5943 end loop;
5944 end if;
5945 end Build_Derived_Access_Type;
5947 ------------------------------
5948 -- Build_Derived_Array_Type --
5949 ------------------------------
5951 procedure Build_Derived_Array_Type
5952 (N : Node_Id;
5953 Parent_Type : Entity_Id;
5954 Derived_Type : Entity_Id)
5956 Loc : constant Source_Ptr := Sloc (N);
5957 Tdef : constant Node_Id := Type_Definition (N);
5958 Indic : constant Node_Id := Subtype_Indication (Tdef);
5959 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
5960 Implicit_Base : Entity_Id;
5961 New_Indic : Node_Id;
5963 procedure Make_Implicit_Base;
5964 -- If the parent subtype is constrained, the derived type is a subtype
5965 -- of an implicit base type derived from the parent base.
5967 ------------------------
5968 -- Make_Implicit_Base --
5969 ------------------------
5971 procedure Make_Implicit_Base is
5972 begin
5973 Implicit_Base :=
5974 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
5976 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
5977 Set_Etype (Implicit_Base, Parent_Base);
5979 Copy_Array_Subtype_Attributes (Implicit_Base, Parent_Base);
5980 Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
5982 Set_Has_Delayed_Freeze (Implicit_Base, True);
5984 -- Inherit the "ghostness" from the parent base type
5986 if Is_Ghost_Entity (Parent_Base) or else Ghost_Mode > None then
5987 Set_Is_Ghost_Entity (Implicit_Base);
5988 end if;
5989 end Make_Implicit_Base;
5991 -- Start of processing for Build_Derived_Array_Type
5993 begin
5994 if not Is_Constrained (Parent_Type) then
5995 if Nkind (Indic) /= N_Subtype_Indication then
5996 Set_Ekind (Derived_Type, E_Array_Type);
5998 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
5999 Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
6001 Set_Has_Delayed_Freeze (Derived_Type, True);
6003 else
6004 Make_Implicit_Base;
6005 Set_Etype (Derived_Type, Implicit_Base);
6007 New_Indic :=
6008 Make_Subtype_Declaration (Loc,
6009 Defining_Identifier => Derived_Type,
6010 Subtype_Indication =>
6011 Make_Subtype_Indication (Loc,
6012 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
6013 Constraint => Constraint (Indic)));
6015 Rewrite (N, New_Indic);
6016 Analyze (N);
6017 end if;
6019 else
6020 if Nkind (Indic) /= N_Subtype_Indication then
6021 Make_Implicit_Base;
6023 Set_Ekind (Derived_Type, Ekind (Parent_Type));
6024 Set_Etype (Derived_Type, Implicit_Base);
6025 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6027 else
6028 Error_Msg_N ("illegal constraint on constrained type", Indic);
6029 end if;
6030 end if;
6032 -- If parent type is not a derived type itself, and is declared in
6033 -- closed scope (e.g. a subprogram), then we must explicitly introduce
6034 -- the new type's concatenation operator since Derive_Subprograms
6035 -- will not inherit the parent's operator. If the parent type is
6036 -- unconstrained, the operator is of the unconstrained base type.
6038 if Number_Dimensions (Parent_Type) = 1
6039 and then not Is_Limited_Type (Parent_Type)
6040 and then not Is_Derived_Type (Parent_Type)
6041 and then not Is_Package_Or_Generic_Package
6042 (Scope (Base_Type (Parent_Type)))
6043 then
6044 if not Is_Constrained (Parent_Type)
6045 and then Is_Constrained (Derived_Type)
6046 then
6047 New_Concatenation_Op (Implicit_Base);
6048 else
6049 New_Concatenation_Op (Derived_Type);
6050 end if;
6051 end if;
6052 end Build_Derived_Array_Type;
6054 -----------------------------------
6055 -- Build_Derived_Concurrent_Type --
6056 -----------------------------------
6058 procedure Build_Derived_Concurrent_Type
6059 (N : Node_Id;
6060 Parent_Type : Entity_Id;
6061 Derived_Type : Entity_Id)
6063 Loc : constant Source_Ptr := Sloc (N);
6065 Corr_Record : constant Entity_Id := Make_Temporary (Loc, 'C');
6066 Corr_Decl : Node_Id;
6067 Corr_Decl_Needed : Boolean;
6068 -- If the derived type has fewer discriminants than its parent, the
6069 -- corresponding record is also a derived type, in order to account for
6070 -- the bound discriminants. We create a full type declaration for it in
6071 -- this case.
6073 Constraint_Present : constant Boolean :=
6074 Nkind (Subtype_Indication (Type_Definition (N))) =
6075 N_Subtype_Indication;
6077 D_Constraint : Node_Id;
6078 New_Constraint : Elist_Id;
6079 Old_Disc : Entity_Id;
6080 New_Disc : Entity_Id;
6081 New_N : Node_Id;
6083 begin
6084 Set_Stored_Constraint (Derived_Type, No_Elist);
6085 Corr_Decl_Needed := False;
6086 Old_Disc := Empty;
6088 if Present (Discriminant_Specifications (N))
6089 and then Constraint_Present
6090 then
6091 Old_Disc := First_Discriminant (Parent_Type);
6092 New_Disc := First (Discriminant_Specifications (N));
6093 while Present (New_Disc) and then Present (Old_Disc) loop
6094 Next_Discriminant (Old_Disc);
6095 Next (New_Disc);
6096 end loop;
6097 end if;
6099 if Present (Old_Disc) and then Expander_Active then
6101 -- The new type has fewer discriminants, so we need to create a new
6102 -- corresponding record, which is derived from the corresponding
6103 -- record of the parent, and has a stored constraint that captures
6104 -- the values of the discriminant constraints. The corresponding
6105 -- record is needed only if expander is active and code generation is
6106 -- enabled.
6108 -- The type declaration for the derived corresponding record has the
6109 -- same discriminant part and constraints as the current declaration.
6110 -- Copy the unanalyzed tree to build declaration.
6112 Corr_Decl_Needed := True;
6113 New_N := Copy_Separate_Tree (N);
6115 Corr_Decl :=
6116 Make_Full_Type_Declaration (Loc,
6117 Defining_Identifier => Corr_Record,
6118 Discriminant_Specifications =>
6119 Discriminant_Specifications (New_N),
6120 Type_Definition =>
6121 Make_Derived_Type_Definition (Loc,
6122 Subtype_Indication =>
6123 Make_Subtype_Indication (Loc,
6124 Subtype_Mark =>
6125 New_Occurrence_Of
6126 (Corresponding_Record_Type (Parent_Type), Loc),
6127 Constraint =>
6128 Constraint
6129 (Subtype_Indication (Type_Definition (New_N))))));
6130 end if;
6132 -- Copy Storage_Size and Relative_Deadline variables if task case
6134 if Is_Task_Type (Parent_Type) then
6135 Set_Storage_Size_Variable (Derived_Type,
6136 Storage_Size_Variable (Parent_Type));
6137 Set_Relative_Deadline_Variable (Derived_Type,
6138 Relative_Deadline_Variable (Parent_Type));
6139 end if;
6141 if Present (Discriminant_Specifications (N)) then
6142 Push_Scope (Derived_Type);
6143 Check_Or_Process_Discriminants (N, Derived_Type);
6145 if Constraint_Present then
6146 New_Constraint :=
6147 Expand_To_Stored_Constraint
6148 (Parent_Type,
6149 Build_Discriminant_Constraints
6150 (Parent_Type,
6151 Subtype_Indication (Type_Definition (N)), True));
6152 end if;
6154 End_Scope;
6156 elsif Constraint_Present then
6158 -- Build constrained subtype, copying the constraint, and derive
6159 -- from it to create a derived constrained type.
6161 declare
6162 Loc : constant Source_Ptr := Sloc (N);
6163 Anon : constant Entity_Id :=
6164 Make_Defining_Identifier (Loc,
6165 Chars => New_External_Name (Chars (Derived_Type), 'T'));
6166 Decl : Node_Id;
6168 begin
6169 Decl :=
6170 Make_Subtype_Declaration (Loc,
6171 Defining_Identifier => Anon,
6172 Subtype_Indication =>
6173 New_Copy_Tree (Subtype_Indication (Type_Definition (N))));
6174 Insert_Before (N, Decl);
6175 Analyze (Decl);
6177 Rewrite (Subtype_Indication (Type_Definition (N)),
6178 New_Occurrence_Of (Anon, Loc));
6179 Set_Analyzed (Derived_Type, False);
6180 Analyze (N);
6181 return;
6182 end;
6183 end if;
6185 -- By default, operations and private data are inherited from parent.
6186 -- However, in the presence of bound discriminants, a new corresponding
6187 -- record will be created, see below.
6189 Set_Has_Discriminants
6190 (Derived_Type, Has_Discriminants (Parent_Type));
6191 Set_Corresponding_Record_Type
6192 (Derived_Type, Corresponding_Record_Type (Parent_Type));
6194 -- Is_Constrained is set according the parent subtype, but is set to
6195 -- False if the derived type is declared with new discriminants.
6197 Set_Is_Constrained
6198 (Derived_Type,
6199 (Is_Constrained (Parent_Type) or else Constraint_Present)
6200 and then not Present (Discriminant_Specifications (N)));
6202 if Constraint_Present then
6203 if not Has_Discriminants (Parent_Type) then
6204 Error_Msg_N ("untagged parent must have discriminants", N);
6206 elsif Present (Discriminant_Specifications (N)) then
6208 -- Verify that new discriminants are used to constrain old ones
6210 D_Constraint :=
6211 First
6212 (Constraints
6213 (Constraint (Subtype_Indication (Type_Definition (N)))));
6215 Old_Disc := First_Discriminant (Parent_Type);
6217 while Present (D_Constraint) loop
6218 if Nkind (D_Constraint) /= N_Discriminant_Association then
6220 -- Positional constraint. If it is a reference to a new
6221 -- discriminant, it constrains the corresponding old one.
6223 if Nkind (D_Constraint) = N_Identifier then
6224 New_Disc := First_Discriminant (Derived_Type);
6225 while Present (New_Disc) loop
6226 exit when Chars (New_Disc) = Chars (D_Constraint);
6227 Next_Discriminant (New_Disc);
6228 end loop;
6230 if Present (New_Disc) then
6231 Set_Corresponding_Discriminant (New_Disc, Old_Disc);
6232 end if;
6233 end if;
6235 Next_Discriminant (Old_Disc);
6237 -- if this is a named constraint, search by name for the old
6238 -- discriminants constrained by the new one.
6240 elsif Nkind (Expression (D_Constraint)) = N_Identifier then
6242 -- Find new discriminant with that name
6244 New_Disc := First_Discriminant (Derived_Type);
6245 while Present (New_Disc) loop
6246 exit when
6247 Chars (New_Disc) = Chars (Expression (D_Constraint));
6248 Next_Discriminant (New_Disc);
6249 end loop;
6251 if Present (New_Disc) then
6253 -- Verify that new discriminant renames some discriminant
6254 -- of the parent type, and associate the new discriminant
6255 -- with one or more old ones that it renames.
6257 declare
6258 Selector : Node_Id;
6260 begin
6261 Selector := First (Selector_Names (D_Constraint));
6262 while Present (Selector) loop
6263 Old_Disc := First_Discriminant (Parent_Type);
6264 while Present (Old_Disc) loop
6265 exit when Chars (Old_Disc) = Chars (Selector);
6266 Next_Discriminant (Old_Disc);
6267 end loop;
6269 if Present (Old_Disc) then
6270 Set_Corresponding_Discriminant
6271 (New_Disc, Old_Disc);
6272 end if;
6274 Next (Selector);
6275 end loop;
6276 end;
6277 end if;
6278 end if;
6280 Next (D_Constraint);
6281 end loop;
6283 New_Disc := First_Discriminant (Derived_Type);
6284 while Present (New_Disc) loop
6285 if No (Corresponding_Discriminant (New_Disc)) then
6286 Error_Msg_NE
6287 ("new discriminant& must constrain old one", N, New_Disc);
6289 elsif not
6290 Subtypes_Statically_Compatible
6291 (Etype (New_Disc),
6292 Etype (Corresponding_Discriminant (New_Disc)))
6293 then
6294 Error_Msg_NE
6295 ("& not statically compatible with parent discriminant",
6296 N, New_Disc);
6297 end if;
6299 Next_Discriminant (New_Disc);
6300 end loop;
6301 end if;
6303 elsif Present (Discriminant_Specifications (N)) then
6304 Error_Msg_N
6305 ("missing discriminant constraint in untagged derivation", N);
6306 end if;
6308 -- The entity chain of the derived type includes the new discriminants
6309 -- but shares operations with the parent.
6311 if Present (Discriminant_Specifications (N)) then
6312 Old_Disc := First_Discriminant (Parent_Type);
6313 while Present (Old_Disc) loop
6314 if No (Next_Entity (Old_Disc))
6315 or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
6316 then
6317 Set_Next_Entity
6318 (Last_Entity (Derived_Type), Next_Entity (Old_Disc));
6319 exit;
6320 end if;
6322 Next_Discriminant (Old_Disc);
6323 end loop;
6325 else
6326 Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
6327 if Has_Discriminants (Parent_Type) then
6328 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
6329 Set_Discriminant_Constraint (
6330 Derived_Type, Discriminant_Constraint (Parent_Type));
6331 end if;
6332 end if;
6334 Set_Last_Entity (Derived_Type, Last_Entity (Parent_Type));
6336 Set_Has_Completion (Derived_Type);
6338 if Corr_Decl_Needed then
6339 Set_Stored_Constraint (Derived_Type, New_Constraint);
6340 Insert_After (N, Corr_Decl);
6341 Analyze (Corr_Decl);
6342 Set_Corresponding_Record_Type (Derived_Type, Corr_Record);
6343 end if;
6344 end Build_Derived_Concurrent_Type;
6346 ------------------------------------
6347 -- Build_Derived_Enumeration_Type --
6348 ------------------------------------
6350 procedure Build_Derived_Enumeration_Type
6351 (N : Node_Id;
6352 Parent_Type : Entity_Id;
6353 Derived_Type : Entity_Id)
6355 Loc : constant Source_Ptr := Sloc (N);
6356 Def : constant Node_Id := Type_Definition (N);
6357 Indic : constant Node_Id := Subtype_Indication (Def);
6358 Implicit_Base : Entity_Id;
6359 Literal : Entity_Id;
6360 New_Lit : Entity_Id;
6361 Literals_List : List_Id;
6362 Type_Decl : Node_Id;
6363 Hi, Lo : Node_Id;
6364 Rang_Expr : Node_Id;
6366 begin
6367 -- Since types Standard.Character and Standard.[Wide_]Wide_Character do
6368 -- not have explicit literals lists we need to process types derived
6369 -- from them specially. This is handled by Derived_Standard_Character.
6370 -- If the parent type is a generic type, there are no literals either,
6371 -- and we construct the same skeletal representation as for the generic
6372 -- parent type.
6374 if Is_Standard_Character_Type (Parent_Type) then
6375 Derived_Standard_Character (N, Parent_Type, Derived_Type);
6377 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
6378 declare
6379 Lo : Node_Id;
6380 Hi : Node_Id;
6382 begin
6383 if Nkind (Indic) /= N_Subtype_Indication then
6384 Lo :=
6385 Make_Attribute_Reference (Loc,
6386 Attribute_Name => Name_First,
6387 Prefix => New_Occurrence_Of (Derived_Type, Loc));
6388 Set_Etype (Lo, Derived_Type);
6390 Hi :=
6391 Make_Attribute_Reference (Loc,
6392 Attribute_Name => Name_Last,
6393 Prefix => New_Occurrence_Of (Derived_Type, Loc));
6394 Set_Etype (Hi, Derived_Type);
6396 Set_Scalar_Range (Derived_Type,
6397 Make_Range (Loc,
6398 Low_Bound => Lo,
6399 High_Bound => Hi));
6400 else
6402 -- Analyze subtype indication and verify compatibility
6403 -- with parent type.
6405 if Base_Type (Process_Subtype (Indic, N)) /=
6406 Base_Type (Parent_Type)
6407 then
6408 Error_Msg_N
6409 ("illegal constraint for formal discrete type", N);
6410 end if;
6411 end if;
6412 end;
6414 else
6415 -- If a constraint is present, analyze the bounds to catch
6416 -- premature usage of the derived literals.
6418 if Nkind (Indic) = N_Subtype_Indication
6419 and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
6420 then
6421 Analyze (Low_Bound (Range_Expression (Constraint (Indic))));
6422 Analyze (High_Bound (Range_Expression (Constraint (Indic))));
6423 end if;
6425 -- Introduce an implicit base type for the derived type even if there
6426 -- is no constraint attached to it, since this seems closer to the
6427 -- Ada semantics. Build a full type declaration tree for the derived
6428 -- type using the implicit base type as the defining identifier. The
6429 -- build a subtype declaration tree which applies the constraint (if
6430 -- any) have it replace the derived type declaration.
6432 Literal := First_Literal (Parent_Type);
6433 Literals_List := New_List;
6434 while Present (Literal)
6435 and then Ekind (Literal) = E_Enumeration_Literal
6436 loop
6437 -- Literals of the derived type have the same representation as
6438 -- those of the parent type, but this representation can be
6439 -- overridden by an explicit representation clause. Indicate
6440 -- that there is no explicit representation given yet. These
6441 -- derived literals are implicit operations of the new type,
6442 -- and can be overridden by explicit ones.
6444 if Nkind (Literal) = N_Defining_Character_Literal then
6445 New_Lit :=
6446 Make_Defining_Character_Literal (Loc, Chars (Literal));
6447 else
6448 New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
6449 end if;
6451 Set_Ekind (New_Lit, E_Enumeration_Literal);
6452 Set_Enumeration_Pos (New_Lit, Enumeration_Pos (Literal));
6453 Set_Enumeration_Rep (New_Lit, Enumeration_Rep (Literal));
6454 Set_Enumeration_Rep_Expr (New_Lit, Empty);
6455 Set_Alias (New_Lit, Literal);
6456 Set_Is_Known_Valid (New_Lit, True);
6458 Append (New_Lit, Literals_List);
6459 Next_Literal (Literal);
6460 end loop;
6462 Implicit_Base :=
6463 Make_Defining_Identifier (Sloc (Derived_Type),
6464 Chars => New_External_Name (Chars (Derived_Type), 'B'));
6466 -- Indicate the proper nature of the derived type. This must be done
6467 -- before analysis of the literals, to recognize cases when a literal
6468 -- may be hidden by a previous explicit function definition (cf.
6469 -- c83031a).
6471 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
6472 Set_Etype (Derived_Type, Implicit_Base);
6474 Type_Decl :=
6475 Make_Full_Type_Declaration (Loc,
6476 Defining_Identifier => Implicit_Base,
6477 Discriminant_Specifications => No_List,
6478 Type_Definition =>
6479 Make_Enumeration_Type_Definition (Loc, Literals_List));
6481 Mark_Rewrite_Insertion (Type_Decl);
6482 Insert_Before (N, Type_Decl);
6483 Analyze (Type_Decl);
6485 -- After the implicit base is analyzed its Etype needs to be changed
6486 -- to reflect the fact that it is derived from the parent type which
6487 -- was ignored during analysis. We also set the size at this point.
6489 Set_Etype (Implicit_Base, Parent_Type);
6491 Set_Size_Info (Implicit_Base, Parent_Type);
6492 Set_RM_Size (Implicit_Base, RM_Size (Parent_Type));
6493 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
6495 -- Copy other flags from parent type
6497 Set_Has_Non_Standard_Rep
6498 (Implicit_Base, Has_Non_Standard_Rep
6499 (Parent_Type));
6500 Set_Has_Pragma_Ordered
6501 (Implicit_Base, Has_Pragma_Ordered
6502 (Parent_Type));
6503 Set_Has_Delayed_Freeze (Implicit_Base);
6505 -- Process the subtype indication including a validation check on the
6506 -- constraint, if any. If a constraint is given, its bounds must be
6507 -- implicitly converted to the new type.
6509 if Nkind (Indic) = N_Subtype_Indication then
6510 declare
6511 R : constant Node_Id :=
6512 Range_Expression (Constraint (Indic));
6514 begin
6515 if Nkind (R) = N_Range then
6516 Hi := Build_Scalar_Bound
6517 (High_Bound (R), Parent_Type, Implicit_Base);
6518 Lo := Build_Scalar_Bound
6519 (Low_Bound (R), Parent_Type, Implicit_Base);
6521 else
6522 -- Constraint is a Range attribute. Replace with explicit
6523 -- mention of the bounds of the prefix, which must be a
6524 -- subtype.
6526 Analyze (Prefix (R));
6527 Hi :=
6528 Convert_To (Implicit_Base,
6529 Make_Attribute_Reference (Loc,
6530 Attribute_Name => Name_Last,
6531 Prefix =>
6532 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
6534 Lo :=
6535 Convert_To (Implicit_Base,
6536 Make_Attribute_Reference (Loc,
6537 Attribute_Name => Name_First,
6538 Prefix =>
6539 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
6540 end if;
6541 end;
6543 else
6544 Hi :=
6545 Build_Scalar_Bound
6546 (Type_High_Bound (Parent_Type),
6547 Parent_Type, Implicit_Base);
6548 Lo :=
6549 Build_Scalar_Bound
6550 (Type_Low_Bound (Parent_Type),
6551 Parent_Type, Implicit_Base);
6552 end if;
6554 Rang_Expr :=
6555 Make_Range (Loc,
6556 Low_Bound => Lo,
6557 High_Bound => Hi);
6559 -- If we constructed a default range for the case where no range
6560 -- was given, then the expressions in the range must not freeze
6561 -- since they do not correspond to expressions in the source.
6563 if Nkind (Indic) /= N_Subtype_Indication then
6564 Set_Must_Not_Freeze (Lo);
6565 Set_Must_Not_Freeze (Hi);
6566 Set_Must_Not_Freeze (Rang_Expr);
6567 end if;
6569 Rewrite (N,
6570 Make_Subtype_Declaration (Loc,
6571 Defining_Identifier => Derived_Type,
6572 Subtype_Indication =>
6573 Make_Subtype_Indication (Loc,
6574 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
6575 Constraint =>
6576 Make_Range_Constraint (Loc,
6577 Range_Expression => Rang_Expr))));
6579 Analyze (N);
6581 -- Propagate the aspects from the original type declaration to the
6582 -- declaration of the implicit base.
6584 Move_Aspects (From => Original_Node (N), To => Type_Decl);
6586 -- Apply a range check. Since this range expression doesn't have an
6587 -- Etype, we have to specifically pass the Source_Typ parameter. Is
6588 -- this right???
6590 if Nkind (Indic) = N_Subtype_Indication then
6591 Apply_Range_Check
6592 (Range_Expression (Constraint (Indic)), Parent_Type,
6593 Source_Typ => Entity (Subtype_Mark (Indic)));
6594 end if;
6595 end if;
6596 end Build_Derived_Enumeration_Type;
6598 --------------------------------
6599 -- Build_Derived_Numeric_Type --
6600 --------------------------------
6602 procedure Build_Derived_Numeric_Type
6603 (N : Node_Id;
6604 Parent_Type : Entity_Id;
6605 Derived_Type : Entity_Id)
6607 Loc : constant Source_Ptr := Sloc (N);
6608 Tdef : constant Node_Id := Type_Definition (N);
6609 Indic : constant Node_Id := Subtype_Indication (Tdef);
6610 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
6611 No_Constraint : constant Boolean := Nkind (Indic) /=
6612 N_Subtype_Indication;
6613 Implicit_Base : Entity_Id;
6615 Lo : Node_Id;
6616 Hi : Node_Id;
6618 begin
6619 -- Process the subtype indication including a validation check on
6620 -- the constraint if any.
6622 Discard_Node (Process_Subtype (Indic, N));
6624 -- Introduce an implicit base type for the derived type even if there
6625 -- is no constraint attached to it, since this seems closer to the Ada
6626 -- semantics.
6628 Implicit_Base :=
6629 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
6631 Set_Etype (Implicit_Base, Parent_Base);
6632 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
6633 Set_Size_Info (Implicit_Base, Parent_Base);
6634 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
6635 Set_Parent (Implicit_Base, Parent (Derived_Type));
6636 Set_Is_Known_Valid (Implicit_Base, Is_Known_Valid (Parent_Base));
6638 -- Set RM Size for discrete type or decimal fixed-point type
6639 -- Ordinary fixed-point is excluded, why???
6641 if Is_Discrete_Type (Parent_Base)
6642 or else Is_Decimal_Fixed_Point_Type (Parent_Base)
6643 then
6644 Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
6645 end if;
6647 Set_Has_Delayed_Freeze (Implicit_Base);
6649 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
6650 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
6652 Set_Scalar_Range (Implicit_Base,
6653 Make_Range (Loc,
6654 Low_Bound => Lo,
6655 High_Bound => Hi));
6657 if Has_Infinities (Parent_Base) then
6658 Set_Includes_Infinities (Scalar_Range (Implicit_Base));
6659 end if;
6661 -- The Derived_Type, which is the entity of the declaration, is a
6662 -- subtype of the implicit base. Its Ekind is a subtype, even in the
6663 -- absence of an explicit constraint.
6665 Set_Etype (Derived_Type, Implicit_Base);
6667 -- If we did not have a constraint, then the Ekind is set from the
6668 -- parent type (otherwise Process_Subtype has set the bounds)
6670 if No_Constraint then
6671 Set_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
6672 end if;
6674 -- If we did not have a range constraint, then set the range from the
6675 -- parent type. Otherwise, the Process_Subtype call has set the bounds.
6677 if No_Constraint or else not Has_Range_Constraint (Indic) then
6678 Set_Scalar_Range (Derived_Type,
6679 Make_Range (Loc,
6680 Low_Bound => New_Copy_Tree (Type_Low_Bound (Parent_Type)),
6681 High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
6682 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
6684 if Has_Infinities (Parent_Type) then
6685 Set_Includes_Infinities (Scalar_Range (Derived_Type));
6686 end if;
6688 Set_Is_Known_Valid (Derived_Type, Is_Known_Valid (Parent_Type));
6689 end if;
6691 Set_Is_Descendent_Of_Address (Derived_Type,
6692 Is_Descendent_Of_Address (Parent_Type));
6693 Set_Is_Descendent_Of_Address (Implicit_Base,
6694 Is_Descendent_Of_Address (Parent_Type));
6696 -- Set remaining type-specific fields, depending on numeric type
6698 if Is_Modular_Integer_Type (Parent_Type) then
6699 Set_Modulus (Implicit_Base, Modulus (Parent_Base));
6701 Set_Non_Binary_Modulus
6702 (Implicit_Base, Non_Binary_Modulus (Parent_Base));
6704 Set_Is_Known_Valid
6705 (Implicit_Base, Is_Known_Valid (Parent_Base));
6707 elsif Is_Floating_Point_Type (Parent_Type) then
6709 -- Digits of base type is always copied from the digits value of
6710 -- the parent base type, but the digits of the derived type will
6711 -- already have been set if there was a constraint present.
6713 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
6714 Set_Float_Rep (Implicit_Base, Float_Rep (Parent_Base));
6716 if No_Constraint then
6717 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
6718 end if;
6720 elsif Is_Fixed_Point_Type (Parent_Type) then
6722 -- Small of base type and derived type are always copied from the
6723 -- parent base type, since smalls never change. The delta of the
6724 -- base type is also copied from the parent base type. However the
6725 -- delta of the derived type will have been set already if a
6726 -- constraint was present.
6728 Set_Small_Value (Derived_Type, Small_Value (Parent_Base));
6729 Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
6730 Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
6732 if No_Constraint then
6733 Set_Delta_Value (Derived_Type, Delta_Value (Parent_Type));
6734 end if;
6736 -- The scale and machine radix in the decimal case are always
6737 -- copied from the parent base type.
6739 if Is_Decimal_Fixed_Point_Type (Parent_Type) then
6740 Set_Scale_Value (Derived_Type, Scale_Value (Parent_Base));
6741 Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
6743 Set_Machine_Radix_10
6744 (Derived_Type, Machine_Radix_10 (Parent_Base));
6745 Set_Machine_Radix_10
6746 (Implicit_Base, Machine_Radix_10 (Parent_Base));
6748 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
6750 if No_Constraint then
6751 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
6753 else
6754 -- the analysis of the subtype_indication sets the
6755 -- digits value of the derived type.
6757 null;
6758 end if;
6759 end if;
6760 end if;
6762 if Is_Integer_Type (Parent_Type) then
6763 Set_Has_Shift_Operator
6764 (Implicit_Base, Has_Shift_Operator (Parent_Type));
6765 end if;
6767 -- The type of the bounds is that of the parent type, and they
6768 -- must be converted to the derived type.
6770 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
6772 -- The implicit_base should be frozen when the derived type is frozen,
6773 -- but note that it is used in the conversions of the bounds. For fixed
6774 -- types we delay the determination of the bounds until the proper
6775 -- freezing point. For other numeric types this is rejected by GCC, for
6776 -- reasons that are currently unclear (???), so we choose to freeze the
6777 -- implicit base now. In the case of integers and floating point types
6778 -- this is harmless because subsequent representation clauses cannot
6779 -- affect anything, but it is still baffling that we cannot use the
6780 -- same mechanism for all derived numeric types.
6782 -- There is a further complication: actually some representation
6783 -- clauses can affect the implicit base type. For example, attribute
6784 -- definition clauses for stream-oriented attributes need to set the
6785 -- corresponding TSS entries on the base type, and this normally
6786 -- cannot be done after the base type is frozen, so the circuitry in
6787 -- Sem_Ch13.New_Stream_Subprogram must account for this possibility
6788 -- and not use Set_TSS in this case.
6790 -- There are also consequences for the case of delayed representation
6791 -- aspects for some cases. For example, a Size aspect is delayed and
6792 -- should not be evaluated to the freeze point. This early freezing
6793 -- means that the size attribute evaluation happens too early???
6795 if Is_Fixed_Point_Type (Parent_Type) then
6796 Conditional_Delay (Implicit_Base, Parent_Type);
6797 else
6798 Freeze_Before (N, Implicit_Base);
6799 end if;
6800 end Build_Derived_Numeric_Type;
6802 --------------------------------
6803 -- Build_Derived_Private_Type --
6804 --------------------------------
6806 procedure Build_Derived_Private_Type
6807 (N : Node_Id;
6808 Parent_Type : Entity_Id;
6809 Derived_Type : Entity_Id;
6810 Is_Completion : Boolean;
6811 Derive_Subps : Boolean := True)
6813 Loc : constant Source_Ptr := Sloc (N);
6814 Par_Base : constant Entity_Id := Base_Type (Parent_Type);
6815 Par_Scope : constant Entity_Id := Scope (Par_Base);
6816 Full_N : constant Node_Id := New_Copy_Tree (N);
6817 Full_Der : Entity_Id := New_Copy (Derived_Type);
6818 Full_P : Entity_Id;
6820 procedure Build_Full_Derivation;
6821 -- Build full derivation, i.e. derive from the full view
6823 procedure Copy_And_Build;
6824 -- Copy derived type declaration, replace parent with its full view,
6825 -- and build derivation
6827 ---------------------------
6828 -- Build_Full_Derivation --
6829 ---------------------------
6831 procedure Build_Full_Derivation is
6832 begin
6833 -- If parent scope is not open, install the declarations
6835 if not In_Open_Scopes (Par_Scope) then
6836 Install_Private_Declarations (Par_Scope);
6837 Install_Visible_Declarations (Par_Scope);
6838 Copy_And_Build;
6839 Uninstall_Declarations (Par_Scope);
6841 -- If parent scope is open and in another unit, and parent has a
6842 -- completion, then the derivation is taking place in the visible
6843 -- part of a child unit. In that case retrieve the full view of
6844 -- the parent momentarily.
6846 elsif not In_Same_Source_Unit (N, Parent_Type) then
6847 Full_P := Full_View (Parent_Type);
6848 Exchange_Declarations (Parent_Type);
6849 Copy_And_Build;
6850 Exchange_Declarations (Full_P);
6852 -- Otherwise it is a local derivation
6854 else
6855 Copy_And_Build;
6856 end if;
6857 end Build_Full_Derivation;
6859 --------------------
6860 -- Copy_And_Build --
6861 --------------------
6863 procedure Copy_And_Build is
6864 Full_Parent : Entity_Id := Parent_Type;
6866 begin
6867 -- If the parent is itself derived from another private type,
6868 -- installing the private declarations has not affected its
6869 -- privacy status, so use its own full view explicitly.
6871 if Is_Private_Type (Full_Parent)
6872 and then Present (Full_View (Full_Parent))
6873 then
6874 Full_Parent := Full_View (Full_Parent);
6875 end if;
6877 -- And its underlying full view if necessary
6879 if Is_Private_Type (Full_Parent)
6880 and then Present (Underlying_Full_View (Full_Parent))
6881 then
6882 Full_Parent := Underlying_Full_View (Full_Parent);
6883 end if;
6885 -- For record, access and most enumeration types, derivation from
6886 -- the full view requires a fully-fledged declaration. In the other
6887 -- cases, just use an itype.
6889 if Ekind (Full_Parent) in Record_Kind
6890 or else Ekind (Full_Parent) in Access_Kind
6891 or else
6892 (Ekind (Full_Parent) in Enumeration_Kind
6893 and then not Is_Standard_Character_Type (Full_Parent)
6894 and then not Is_Generic_Type (Root_Type (Full_Parent)))
6895 then
6896 -- Copy and adjust declaration to provide a completion for what
6897 -- is originally a private declaration. Indicate that full view
6898 -- is internally generated.
6900 Set_Comes_From_Source (Full_N, False);
6901 Set_Comes_From_Source (Full_Der, False);
6902 Set_Parent (Full_Der, Full_N);
6903 Set_Defining_Identifier (Full_N, Full_Der);
6905 -- If there are no constraints, adjust the subtype mark
6907 if Nkind (Subtype_Indication (Type_Definition (Full_N))) /=
6908 N_Subtype_Indication
6909 then
6910 Set_Subtype_Indication
6911 (Type_Definition (Full_N),
6912 New_Occurrence_Of (Full_Parent, Sloc (Full_N)));
6913 end if;
6915 Insert_After (N, Full_N);
6917 -- Build full view of derived type from full view of parent which
6918 -- is now installed. Subprograms have been derived on the partial
6919 -- view, the completion does not derive them anew.
6921 if Ekind (Full_Parent) in Record_Kind then
6923 -- If parent type is tagged, the completion inherits the proper
6924 -- primitive operations.
6926 if Is_Tagged_Type (Parent_Type) then
6927 Build_Derived_Record_Type
6928 (Full_N, Full_Parent, Full_Der, Derive_Subps);
6929 else
6930 Build_Derived_Record_Type
6931 (Full_N, Full_Parent, Full_Der, Derive_Subps => False);
6932 end if;
6934 else
6935 Build_Derived_Type
6936 (Full_N, Full_Parent, Full_Der,
6937 Is_Completion => False, Derive_Subps => False);
6938 end if;
6940 -- The full declaration has been introduced into the tree and
6941 -- processed in the step above. It should not be analyzed again
6942 -- (when encountered later in the current list of declarations)
6943 -- to prevent spurious name conflicts. The full entity remains
6944 -- invisible.
6946 Set_Analyzed (Full_N);
6948 else
6949 Full_Der :=
6950 Make_Defining_Identifier (Sloc (Derived_Type),
6951 Chars => Chars (Derived_Type));
6952 Set_Is_Itype (Full_Der);
6953 Set_Associated_Node_For_Itype (Full_Der, N);
6954 Set_Parent (Full_Der, N);
6955 Build_Derived_Type
6956 (N, Full_Parent, Full_Der,
6957 Is_Completion => False, Derive_Subps => False);
6958 end if;
6960 Set_Has_Private_Declaration (Full_Der);
6961 Set_Has_Private_Declaration (Derived_Type);
6963 Set_Scope (Full_Der, Scope (Derived_Type));
6964 Set_Is_First_Subtype (Full_Der, Is_First_Subtype (Derived_Type));
6965 Set_Has_Size_Clause (Full_Der, False);
6966 Set_Has_Alignment_Clause (Full_Der, False);
6967 Set_Has_Delayed_Freeze (Full_Der);
6968 Set_Is_Frozen (Full_Der, False);
6969 Set_Freeze_Node (Full_Der, Empty);
6970 Set_Depends_On_Private (Full_Der, Has_Private_Component (Full_Der));
6971 Set_Is_Public (Full_Der, Is_Public (Derived_Type));
6973 -- The convention on the base type may be set in the private part
6974 -- and not propagated to the subtype until later, so we obtain the
6975 -- convention from the base type of the parent.
6977 Set_Convention (Full_Der, Convention (Base_Type (Full_Parent)));
6978 end Copy_And_Build;
6980 -- Start of processing for Build_Derived_Private_Type
6982 begin
6983 if Is_Tagged_Type (Parent_Type) then
6984 Full_P := Full_View (Parent_Type);
6986 -- A type extension of a type with unknown discriminants is an
6987 -- indefinite type that the back-end cannot handle directly.
6988 -- We treat it as a private type, and build a completion that is
6989 -- derived from the full view of the parent, and hopefully has
6990 -- known discriminants.
6992 -- If the full view of the parent type has an underlying record view,
6993 -- use it to generate the underlying record view of this derived type
6994 -- (required for chains of derivations with unknown discriminants).
6996 -- Minor optimization: we avoid the generation of useless underlying
6997 -- record view entities if the private type declaration has unknown
6998 -- discriminants but its corresponding full view has no
6999 -- discriminants.
7001 if Has_Unknown_Discriminants (Parent_Type)
7002 and then Present (Full_P)
7003 and then (Has_Discriminants (Full_P)
7004 or else Present (Underlying_Record_View (Full_P)))
7005 and then not In_Open_Scopes (Par_Scope)
7006 and then Expander_Active
7007 then
7008 declare
7009 Full_Der : constant Entity_Id := Make_Temporary (Loc, 'T');
7010 New_Ext : constant Node_Id :=
7011 Copy_Separate_Tree
7012 (Record_Extension_Part (Type_Definition (N)));
7013 Decl : Node_Id;
7015 begin
7016 Build_Derived_Record_Type
7017 (N, Parent_Type, Derived_Type, Derive_Subps);
7019 -- Build anonymous completion, as a derivation from the full
7020 -- view of the parent. This is not a completion in the usual
7021 -- sense, because the current type is not private.
7023 Decl :=
7024 Make_Full_Type_Declaration (Loc,
7025 Defining_Identifier => Full_Der,
7026 Type_Definition =>
7027 Make_Derived_Type_Definition (Loc,
7028 Subtype_Indication =>
7029 New_Copy_Tree
7030 (Subtype_Indication (Type_Definition (N))),
7031 Record_Extension_Part => New_Ext));
7033 -- If the parent type has an underlying record view, use it
7034 -- here to build the new underlying record view.
7036 if Present (Underlying_Record_View (Full_P)) then
7037 pragma Assert
7038 (Nkind (Subtype_Indication (Type_Definition (Decl)))
7039 = N_Identifier);
7040 Set_Entity (Subtype_Indication (Type_Definition (Decl)),
7041 Underlying_Record_View (Full_P));
7042 end if;
7044 Install_Private_Declarations (Par_Scope);
7045 Install_Visible_Declarations (Par_Scope);
7046 Insert_Before (N, Decl);
7048 -- Mark entity as an underlying record view before analysis,
7049 -- to avoid generating the list of its primitive operations
7050 -- (which is not really required for this entity) and thus
7051 -- prevent spurious errors associated with missing overriding
7052 -- of abstract primitives (overridden only for Derived_Type).
7054 Set_Ekind (Full_Der, E_Record_Type);
7055 Set_Is_Underlying_Record_View (Full_Der);
7056 Set_Default_SSO (Full_Der);
7058 Analyze (Decl);
7060 pragma Assert (Has_Discriminants (Full_Der)
7061 and then not Has_Unknown_Discriminants (Full_Der));
7063 Uninstall_Declarations (Par_Scope);
7065 -- Freeze the underlying record view, to prevent generation of
7066 -- useless dispatching information, which is simply shared with
7067 -- the real derived type.
7069 Set_Is_Frozen (Full_Der);
7071 -- If the derived type has access discriminants, create
7072 -- references to their anonymous types now, to prevent
7073 -- back-end problems when their first use is in generated
7074 -- bodies of primitives.
7076 declare
7077 E : Entity_Id;
7079 begin
7080 E := First_Entity (Full_Der);
7082 while Present (E) loop
7083 if Ekind (E) = E_Discriminant
7084 and then Ekind (Etype (E)) = E_Anonymous_Access_Type
7085 then
7086 Build_Itype_Reference (Etype (E), Decl);
7087 end if;
7089 Next_Entity (E);
7090 end loop;
7091 end;
7093 -- Set up links between real entity and underlying record view
7095 Set_Underlying_Record_View (Derived_Type, Base_Type (Full_Der));
7096 Set_Underlying_Record_View (Base_Type (Full_Der), Derived_Type);
7097 end;
7099 -- If discriminants are known, build derived record
7101 else
7102 Build_Derived_Record_Type
7103 (N, Parent_Type, Derived_Type, Derive_Subps);
7104 end if;
7106 return;
7108 elsif Has_Discriminants (Parent_Type) then
7110 -- Build partial view of derived type from partial view of parent.
7111 -- This must be done before building the full derivation because the
7112 -- second derivation will modify the discriminants of the first and
7113 -- the discriminants are chained with the rest of the components in
7114 -- the full derivation.
7116 Build_Derived_Record_Type
7117 (N, Parent_Type, Derived_Type, Derive_Subps);
7119 -- Build the full derivation if this is not the anonymous derived
7120 -- base type created by Build_Derived_Record_Type in the constrained
7121 -- case (see point 5. of its head comment) since we build it for the
7122 -- derived subtype. And skip it for protected types altogether, as
7123 -- gigi does not use these types directly.
7125 if Present (Full_View (Parent_Type))
7126 and then not Is_Itype (Derived_Type)
7127 and then not (Ekind (Full_View (Parent_Type)) in Protected_Kind)
7128 then
7129 declare
7130 Der_Base : constant Entity_Id := Base_Type (Derived_Type);
7131 Discr : Entity_Id;
7132 Last_Discr : Entity_Id;
7134 begin
7135 -- If this is not a completion, construct the implicit full
7136 -- view by deriving from the full view of the parent type.
7137 -- But if this is a completion, the derived private type
7138 -- being built is a full view and the full derivation can
7139 -- only be its underlying full view.
7141 Build_Full_Derivation;
7143 if not Is_Completion then
7144 Set_Full_View (Derived_Type, Full_Der);
7145 else
7146 Set_Underlying_Full_View (Derived_Type, Full_Der);
7147 end if;
7149 if not Is_Base_Type (Derived_Type) then
7150 Set_Full_View (Der_Base, Base_Type (Full_Der));
7151 end if;
7153 -- Copy the discriminant list from full view to the partial
7154 -- view (base type and its subtype). Gigi requires that the
7155 -- partial and full views have the same discriminants.
7157 -- Note that since the partial view points to discriminants
7158 -- in the full view, their scope will be that of the full
7159 -- view. This might cause some front end problems and need
7160 -- adjustment???
7162 Discr := First_Discriminant (Base_Type (Full_Der));
7163 Set_First_Entity (Der_Base, Discr);
7165 loop
7166 Last_Discr := Discr;
7167 Next_Discriminant (Discr);
7168 exit when No (Discr);
7169 end loop;
7171 Set_Last_Entity (Der_Base, Last_Discr);
7172 Set_First_Entity (Derived_Type, First_Entity (Der_Base));
7173 Set_Last_Entity (Derived_Type, Last_Entity (Der_Base));
7175 Set_Stored_Constraint
7176 (Full_Der, Stored_Constraint (Derived_Type));
7177 end;
7178 end if;
7180 elsif Present (Full_View (Parent_Type))
7181 and then Has_Discriminants (Full_View (Parent_Type))
7182 then
7183 if Has_Unknown_Discriminants (Parent_Type)
7184 and then Nkind (Subtype_Indication (Type_Definition (N))) =
7185 N_Subtype_Indication
7186 then
7187 Error_Msg_N
7188 ("cannot constrain type with unknown discriminants",
7189 Subtype_Indication (Type_Definition (N)));
7190 return;
7191 end if;
7193 -- If this is not a completion, construct the implicit full view by
7194 -- deriving from the full view of the parent type. But if this is a
7195 -- completion, the derived private type being built is a full view
7196 -- and the full derivation can only be its underlying full view.
7198 Build_Full_Derivation;
7200 if not Is_Completion then
7201 Set_Full_View (Derived_Type, Full_Der);
7202 else
7203 Set_Underlying_Full_View (Derived_Type, Full_Der);
7204 end if;
7206 -- In any case, the primitive operations are inherited from the
7207 -- parent type, not from the internal full view.
7209 Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
7211 if Derive_Subps then
7212 Derive_Subprograms (Parent_Type, Derived_Type);
7213 end if;
7215 Set_Stored_Constraint (Derived_Type, No_Elist);
7216 Set_Is_Constrained
7217 (Derived_Type, Is_Constrained (Full_View (Parent_Type)));
7219 else
7220 -- Untagged type, No discriminants on either view
7222 if Nkind (Subtype_Indication (Type_Definition (N))) =
7223 N_Subtype_Indication
7224 then
7225 Error_Msg_N
7226 ("illegal constraint on type without discriminants", N);
7227 end if;
7229 if Present (Discriminant_Specifications (N))
7230 and then Present (Full_View (Parent_Type))
7231 and then not Is_Tagged_Type (Full_View (Parent_Type))
7232 then
7233 Error_Msg_N ("cannot add discriminants to untagged type", N);
7234 end if;
7236 Set_Stored_Constraint (Derived_Type, No_Elist);
7237 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7238 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
7239 Set_Has_Controlled_Component
7240 (Derived_Type, Has_Controlled_Component
7241 (Parent_Type));
7243 -- Direct controlled types do not inherit Finalize_Storage_Only flag
7245 if not Is_Controlled (Parent_Type) then
7246 Set_Finalize_Storage_Only
7247 (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
7248 end if;
7250 -- If this is not a completion, construct the implicit full view by
7251 -- deriving from the full view of the parent type.
7253 -- ??? If the parent is untagged private and its completion is
7254 -- tagged, this mechanism will not work because we cannot derive from
7255 -- the tagged full view unless we have an extension.
7257 if Present (Full_View (Parent_Type))
7258 and then not Is_Tagged_Type (Full_View (Parent_Type))
7259 and then not Is_Completion
7260 then
7261 Build_Full_Derivation;
7262 Set_Full_View (Derived_Type, Full_Der);
7263 end if;
7264 end if;
7266 Set_Has_Unknown_Discriminants (Derived_Type,
7267 Has_Unknown_Discriminants (Parent_Type));
7269 if Is_Private_Type (Derived_Type) then
7270 Set_Private_Dependents (Derived_Type, New_Elmt_List);
7271 end if;
7273 -- If the parent base type is in scope, add the derived type to its
7274 -- list of private dependents, because its full view may become
7275 -- visible subsequently (in a nested private part, a body, or in a
7276 -- further child unit).
7278 if Is_Private_Type (Par_Base) and then In_Open_Scopes (Par_Scope) then
7279 Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
7281 -- Check for unusual case where a type completed by a private
7282 -- derivation occurs within a package nested in a child unit, and
7283 -- the parent is declared in an ancestor.
7285 if Is_Child_Unit (Scope (Current_Scope))
7286 and then Is_Completion
7287 and then In_Private_Part (Current_Scope)
7288 and then Scope (Parent_Type) /= Current_Scope
7290 -- Note that if the parent has a completion in the private part,
7291 -- (which is itself a derivation from some other private type)
7292 -- it is that completion that is visible, there is no full view
7293 -- available, and no special processing is needed.
7295 and then Present (Full_View (Parent_Type))
7296 then
7297 -- In this case, the full view of the parent type will become
7298 -- visible in the body of the enclosing child, and only then will
7299 -- the current type be possibly non-private. Build an underlying
7300 -- full view that will be installed when the enclosing child body
7301 -- is compiled.
7303 if Present (Underlying_Full_View (Derived_Type)) then
7304 Full_Der := Underlying_Full_View (Derived_Type);
7305 else
7306 Build_Full_Derivation;
7307 Set_Underlying_Full_View (Derived_Type, Full_Der);
7308 end if;
7310 -- The full view will be used to swap entities on entry/exit to
7311 -- the body, and must appear in the entity list for the package.
7313 Append_Entity (Full_Der, Scope (Derived_Type));
7314 end if;
7315 end if;
7316 end Build_Derived_Private_Type;
7318 -------------------------------
7319 -- Build_Derived_Record_Type --
7320 -------------------------------
7322 -- 1. INTRODUCTION
7324 -- Ideally we would like to use the same model of type derivation for
7325 -- tagged and untagged record types. Unfortunately this is not quite
7326 -- possible because the semantics of representation clauses is different
7327 -- for tagged and untagged records under inheritance. Consider the
7328 -- following:
7330 -- type R (...) is [tagged] record ... end record;
7331 -- type T (...) is new R (...) [with ...];
7333 -- The representation clauses for T can specify a completely different
7334 -- record layout from R's. Hence the same component can be placed in two
7335 -- very different positions in objects of type T and R. If R and T are
7336 -- tagged types, representation clauses for T can only specify the layout
7337 -- of non inherited components, thus components that are common in R and T
7338 -- have the same position in objects of type R and T.
7340 -- This has two implications. The first is that the entire tree for R's
7341 -- declaration needs to be copied for T in the untagged case, so that T
7342 -- can be viewed as a record type of its own with its own representation
7343 -- clauses. The second implication is the way we handle discriminants.
7344 -- Specifically, in the untagged case we need a way to communicate to Gigi
7345 -- what are the real discriminants in the record, while for the semantics
7346 -- we need to consider those introduced by the user to rename the
7347 -- discriminants in the parent type. This is handled by introducing the
7348 -- notion of stored discriminants. See below for more.
7350 -- Fortunately the way regular components are inherited can be handled in
7351 -- the same way in tagged and untagged types.
7353 -- To complicate things a bit more the private view of a private extension
7354 -- cannot be handled in the same way as the full view (for one thing the
7355 -- semantic rules are somewhat different). We will explain what differs
7356 -- below.
7358 -- 2. DISCRIMINANTS UNDER INHERITANCE
7360 -- The semantic rules governing the discriminants of derived types are
7361 -- quite subtle.
7363 -- type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
7364 -- [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
7366 -- If parent type has discriminants, then the discriminants that are
7367 -- declared in the derived type are [3.4 (11)]:
7369 -- o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
7370 -- there is one;
7372 -- o Otherwise, each discriminant of the parent type (implicitly declared
7373 -- in the same order with the same specifications). In this case, the
7374 -- discriminants are said to be "inherited", or if unknown in the parent
7375 -- are also unknown in the derived type.
7377 -- Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
7379 -- o The parent subtype must be constrained;
7381 -- o If the parent type is not a tagged type, then each discriminant of
7382 -- the derived type must be used in the constraint defining a parent
7383 -- subtype. [Implementation note: This ensures that the new discriminant
7384 -- can share storage with an existing discriminant.]
7386 -- For the derived type each discriminant of the parent type is either
7387 -- inherited, constrained to equal some new discriminant of the derived
7388 -- type, or constrained to the value of an expression.
7390 -- When inherited or constrained to equal some new discriminant, the
7391 -- parent discriminant and the discriminant of the derived type are said
7392 -- to "correspond".
7394 -- If a discriminant of the parent type is constrained to a specific value
7395 -- in the derived type definition, then the discriminant is said to be
7396 -- "specified" by that derived type definition.
7398 -- 3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
7400 -- We have spoken about stored discriminants in point 1 (introduction)
7401 -- above. There are two sort of stored discriminants: implicit and
7402 -- explicit. As long as the derived type inherits the same discriminants as
7403 -- the root record type, stored discriminants are the same as regular
7404 -- discriminants, and are said to be implicit. However, if any discriminant
7405 -- in the root type was renamed in the derived type, then the derived
7406 -- type will contain explicit stored discriminants. Explicit stored
7407 -- discriminants are discriminants in addition to the semantically visible
7408 -- discriminants defined for the derived type. Stored discriminants are
7409 -- used by Gigi to figure out what are the physical discriminants in
7410 -- objects of the derived type (see precise definition in einfo.ads).
7411 -- As an example, consider the following:
7413 -- type R (D1, D2, D3 : Int) is record ... end record;
7414 -- type T1 is new R;
7415 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
7416 -- type T3 is new T2;
7417 -- type T4 (Y : Int) is new T3 (Y, 99);
7419 -- The following table summarizes the discriminants and stored
7420 -- discriminants in R and T1 through T4.
7422 -- Type Discrim Stored Discrim Comment
7423 -- R (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in R
7424 -- T1 (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in T1
7425 -- T2 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T2
7426 -- T3 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T3
7427 -- T4 (Y) (D1, D2, D3) Girder discrims EXPLICIT in T4
7429 -- Field Corresponding_Discriminant (abbreviated CD below) allows us to
7430 -- find the corresponding discriminant in the parent type, while
7431 -- Original_Record_Component (abbreviated ORC below), the actual physical
7432 -- component that is renamed. Finally the field Is_Completely_Hidden
7433 -- (abbreviated ICH below) is set for all explicit stored discriminants
7434 -- (see einfo.ads for more info). For the above example this gives:
7436 -- Discrim CD ORC ICH
7437 -- ^^^^^^^ ^^ ^^^ ^^^
7438 -- D1 in R empty itself no
7439 -- D2 in R empty itself no
7440 -- D3 in R empty itself no
7442 -- D1 in T1 D1 in R itself no
7443 -- D2 in T1 D2 in R itself no
7444 -- D3 in T1 D3 in R itself no
7446 -- X1 in T2 D3 in T1 D3 in T2 no
7447 -- X2 in T2 D1 in T1 D1 in T2 no
7448 -- D1 in T2 empty itself yes
7449 -- D2 in T2 empty itself yes
7450 -- D3 in T2 empty itself yes
7452 -- X1 in T3 X1 in T2 D3 in T3 no
7453 -- X2 in T3 X2 in T2 D1 in T3 no
7454 -- D1 in T3 empty itself yes
7455 -- D2 in T3 empty itself yes
7456 -- D3 in T3 empty itself yes
7458 -- Y in T4 X1 in T3 D3 in T3 no
7459 -- D1 in T3 empty itself yes
7460 -- D2 in T3 empty itself yes
7461 -- D3 in T3 empty itself yes
7463 -- 4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
7465 -- Type derivation for tagged types is fairly straightforward. If no
7466 -- discriminants are specified by the derived type, these are inherited
7467 -- from the parent. No explicit stored discriminants are ever necessary.
7468 -- The only manipulation that is done to the tree is that of adding a
7469 -- _parent field with parent type and constrained to the same constraint
7470 -- specified for the parent in the derived type definition. For instance:
7472 -- type R (D1, D2, D3 : Int) is tagged record ... end record;
7473 -- type T1 is new R with null record;
7474 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
7476 -- are changed into:
7478 -- type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
7479 -- _parent : R (D1, D2, D3);
7480 -- end record;
7482 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
7483 -- _parent : T1 (X2, 88, X1);
7484 -- end record;
7486 -- The discriminants actually present in R, T1 and T2 as well as their CD,
7487 -- ORC and ICH fields are:
7489 -- Discrim CD ORC ICH
7490 -- ^^^^^^^ ^^ ^^^ ^^^
7491 -- D1 in R empty itself no
7492 -- D2 in R empty itself no
7493 -- D3 in R empty itself no
7495 -- D1 in T1 D1 in R D1 in R no
7496 -- D2 in T1 D2 in R D2 in R no
7497 -- D3 in T1 D3 in R D3 in R no
7499 -- X1 in T2 D3 in T1 D3 in R no
7500 -- X2 in T2 D1 in T1 D1 in R no
7502 -- 5. FIRST TRANSFORMATION FOR DERIVED RECORDS
7504 -- Regardless of whether we dealing with a tagged or untagged type
7505 -- we will transform all derived type declarations of the form
7507 -- type T is new R (...) [with ...];
7508 -- or
7509 -- subtype S is R (...);
7510 -- type T is new S [with ...];
7511 -- into
7512 -- type BT is new R [with ...];
7513 -- subtype T is BT (...);
7515 -- That is, the base derived type is constrained only if it has no
7516 -- discriminants. The reason for doing this is that GNAT's semantic model
7517 -- assumes that a base type with discriminants is unconstrained.
7519 -- Note that, strictly speaking, the above transformation is not always
7520 -- correct. Consider for instance the following excerpt from ACVC b34011a:
7522 -- procedure B34011A is
7523 -- type REC (D : integer := 0) is record
7524 -- I : Integer;
7525 -- end record;
7527 -- package P is
7528 -- type T6 is new Rec;
7529 -- function F return T6;
7530 -- end P;
7532 -- use P;
7533 -- package Q6 is
7534 -- type U is new T6 (Q6.F.I); -- ERROR: Q6.F.
7535 -- end Q6;
7537 -- The definition of Q6.U is illegal. However transforming Q6.U into
7539 -- type BaseU is new T6;
7540 -- subtype U is BaseU (Q6.F.I)
7542 -- turns U into a legal subtype, which is incorrect. To avoid this problem
7543 -- we always analyze the constraint (in this case (Q6.F.I)) before applying
7544 -- the transformation described above.
7546 -- There is another instance where the above transformation is incorrect.
7547 -- Consider:
7549 -- package Pack is
7550 -- type Base (D : Integer) is tagged null record;
7551 -- procedure P (X : Base);
7553 -- type Der is new Base (2) with null record;
7554 -- procedure P (X : Der);
7555 -- end Pack;
7557 -- Then the above transformation turns this into
7559 -- type Der_Base is new Base with null record;
7560 -- -- procedure P (X : Base) is implicitly inherited here
7561 -- -- as procedure P (X : Der_Base).
7563 -- subtype Der is Der_Base (2);
7564 -- procedure P (X : Der);
7565 -- -- The overriding of P (X : Der_Base) is illegal since we
7566 -- -- have a parameter conformance problem.
7568 -- To get around this problem, after having semantically processed Der_Base
7569 -- and the rewritten subtype declaration for Der, we copy Der_Base field
7570 -- Discriminant_Constraint from Der so that when parameter conformance is
7571 -- checked when P is overridden, no semantic errors are flagged.
7573 -- 6. SECOND TRANSFORMATION FOR DERIVED RECORDS
7575 -- Regardless of whether we are dealing with a tagged or untagged type
7576 -- we will transform all derived type declarations of the form
7578 -- type R (D1, .., Dn : ...) is [tagged] record ...;
7579 -- type T is new R [with ...];
7580 -- into
7581 -- type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
7583 -- The reason for such transformation is that it allows us to implement a
7584 -- very clean form of component inheritance as explained below.
7586 -- Note that this transformation is not achieved by direct tree rewriting
7587 -- and manipulation, but rather by redoing the semantic actions that the
7588 -- above transformation will entail. This is done directly in routine
7589 -- Inherit_Components.
7591 -- 7. TYPE DERIVATION AND COMPONENT INHERITANCE
7593 -- In both tagged and untagged derived types, regular non discriminant
7594 -- components are inherited in the derived type from the parent type. In
7595 -- the absence of discriminants component, inheritance is straightforward
7596 -- as components can simply be copied from the parent.
7598 -- If the parent has discriminants, inheriting components constrained with
7599 -- these discriminants requires caution. Consider the following example:
7601 -- type R (D1, D2 : Positive) is [tagged] record
7602 -- S : String (D1 .. D2);
7603 -- end record;
7605 -- type T1 is new R [with null record];
7606 -- type T2 (X : positive) is new R (1, X) [with null record];
7608 -- As explained in 6. above, T1 is rewritten as
7609 -- type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
7610 -- which makes the treatment for T1 and T2 identical.
7612 -- What we want when inheriting S, is that references to D1 and D2 in R are
7613 -- replaced with references to their correct constraints, i.e. D1 and D2 in
7614 -- T1 and 1 and X in T2. So all R's discriminant references are replaced
7615 -- with either discriminant references in the derived type or expressions.
7616 -- This replacement is achieved as follows: before inheriting R's
7617 -- components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
7618 -- created in the scope of T1 (resp. scope of T2) so that discriminants D1
7619 -- and D2 of T1 are visible (resp. discriminant X of T2 is visible).
7620 -- For T2, for instance, this has the effect of replacing String (D1 .. D2)
7621 -- by String (1 .. X).
7623 -- 8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
7625 -- We explain here the rules governing private type extensions relevant to
7626 -- type derivation. These rules are explained on the following example:
7628 -- type D [(...)] is new A [(...)] with private; <-- partial view
7629 -- type D [(...)] is new P [(...)] with null record; <-- full view
7631 -- Type A is called the ancestor subtype of the private extension.
7632 -- Type P is the parent type of the full view of the private extension. It
7633 -- must be A or a type derived from A.
7635 -- The rules concerning the discriminants of private type extensions are
7636 -- [7.3(10-13)]:
7638 -- o If a private extension inherits known discriminants from the ancestor
7639 -- subtype, then the full view must also inherit its discriminants from
7640 -- the ancestor subtype and the parent subtype of the full view must be
7641 -- constrained if and only if the ancestor subtype is constrained.
7643 -- o If a partial view has unknown discriminants, then the full view may
7644 -- define a definite or an indefinite subtype, with or without
7645 -- discriminants.
7647 -- o If a partial view has neither known nor unknown discriminants, then
7648 -- the full view must define a definite subtype.
7650 -- o If the ancestor subtype of a private extension has constrained
7651 -- discriminants, then the parent subtype of the full view must impose a
7652 -- statically matching constraint on those discriminants.
7654 -- This means that only the following forms of private extensions are
7655 -- allowed:
7657 -- type D is new A with private; <-- partial view
7658 -- type D is new P with null record; <-- full view
7660 -- If A has no discriminants than P has no discriminants, otherwise P must
7661 -- inherit A's discriminants.
7663 -- type D is new A (...) with private; <-- partial view
7664 -- type D is new P (:::) with null record; <-- full view
7666 -- P must inherit A's discriminants and (...) and (:::) must statically
7667 -- match.
7669 -- subtype A is R (...);
7670 -- type D is new A with private; <-- partial view
7671 -- type D is new P with null record; <-- full view
7673 -- P must have inherited R's discriminants and must be derived from A or
7674 -- any of its subtypes.
7676 -- type D (..) is new A with private; <-- partial view
7677 -- type D (..) is new P [(:::)] with null record; <-- full view
7679 -- No specific constraints on P's discriminants or constraint (:::).
7680 -- Note that A can be unconstrained, but the parent subtype P must either
7681 -- be constrained or (:::) must be present.
7683 -- type D (..) is new A [(...)] with private; <-- partial view
7684 -- type D (..) is new P [(:::)] with null record; <-- full view
7686 -- P's constraints on A's discriminants must statically match those
7687 -- imposed by (...).
7689 -- 9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
7691 -- The full view of a private extension is handled exactly as described
7692 -- above. The model chose for the private view of a private extension is
7693 -- the same for what concerns discriminants (i.e. they receive the same
7694 -- treatment as in the tagged case). However, the private view of the
7695 -- private extension always inherits the components of the parent base,
7696 -- without replacing any discriminant reference. Strictly speaking this is
7697 -- incorrect. However, Gigi never uses this view to generate code so this
7698 -- is a purely semantic issue. In theory, a set of transformations similar
7699 -- to those given in 5. and 6. above could be applied to private views of
7700 -- private extensions to have the same model of component inheritance as
7701 -- for non private extensions. However, this is not done because it would
7702 -- further complicate private type processing. Semantically speaking, this
7703 -- leaves us in an uncomfortable situation. As an example consider:
7705 -- package Pack is
7706 -- type R (D : integer) is tagged record
7707 -- S : String (1 .. D);
7708 -- end record;
7709 -- procedure P (X : R);
7710 -- type T is new R (1) with private;
7711 -- private
7712 -- type T is new R (1) with null record;
7713 -- end;
7715 -- This is transformed into:
7717 -- package Pack is
7718 -- type R (D : integer) is tagged record
7719 -- S : String (1 .. D);
7720 -- end record;
7721 -- procedure P (X : R);
7722 -- type T is new R (1) with private;
7723 -- private
7724 -- type BaseT is new R with null record;
7725 -- subtype T is BaseT (1);
7726 -- end;
7728 -- (strictly speaking the above is incorrect Ada)
7730 -- From the semantic standpoint the private view of private extension T
7731 -- should be flagged as constrained since one can clearly have
7733 -- Obj : T;
7735 -- in a unit withing Pack. However, when deriving subprograms for the
7736 -- private view of private extension T, T must be seen as unconstrained
7737 -- since T has discriminants (this is a constraint of the current
7738 -- subprogram derivation model). Thus, when processing the private view of
7739 -- a private extension such as T, we first mark T as unconstrained, we
7740 -- process it, we perform program derivation and just before returning from
7741 -- Build_Derived_Record_Type we mark T as constrained.
7743 -- ??? Are there are other uncomfortable cases that we will have to
7744 -- deal with.
7746 -- 10. RECORD_TYPE_WITH_PRIVATE complications
7748 -- Types that are derived from a visible record type and have a private
7749 -- extension present other peculiarities. They behave mostly like private
7750 -- types, but if they have primitive operations defined, these will not
7751 -- have the proper signatures for further inheritance, because other
7752 -- primitive operations will use the implicit base that we define for
7753 -- private derivations below. This affect subprogram inheritance (see
7754 -- Derive_Subprograms for details). We also derive the implicit base from
7755 -- the base type of the full view, so that the implicit base is a record
7756 -- type and not another private type, This avoids infinite loops.
7758 procedure Build_Derived_Record_Type
7759 (N : Node_Id;
7760 Parent_Type : Entity_Id;
7761 Derived_Type : Entity_Id;
7762 Derive_Subps : Boolean := True)
7764 Discriminant_Specs : constant Boolean :=
7765 Present (Discriminant_Specifications (N));
7766 Is_Tagged : constant Boolean := Is_Tagged_Type (Parent_Type);
7767 Loc : constant Source_Ptr := Sloc (N);
7768 Private_Extension : constant Boolean :=
7769 Nkind (N) = N_Private_Extension_Declaration;
7770 Assoc_List : Elist_Id;
7771 Constraint_Present : Boolean;
7772 Constrs : Elist_Id;
7773 Discrim : Entity_Id;
7774 Indic : Node_Id;
7775 Inherit_Discrims : Boolean := False;
7776 Last_Discrim : Entity_Id;
7777 New_Base : Entity_Id;
7778 New_Decl : Node_Id;
7779 New_Discrs : Elist_Id;
7780 New_Indic : Node_Id;
7781 Parent_Base : Entity_Id;
7782 Save_Etype : Entity_Id;
7783 Save_Discr_Constr : Elist_Id;
7784 Save_Next_Entity : Entity_Id;
7785 Type_Def : Node_Id;
7787 Discs : Elist_Id := New_Elmt_List;
7788 -- An empty Discs list means that there were no constraints in the
7789 -- subtype indication or that there was an error processing it.
7791 begin
7792 if Ekind (Parent_Type) = E_Record_Type_With_Private
7793 and then Present (Full_View (Parent_Type))
7794 and then Has_Discriminants (Parent_Type)
7795 then
7796 Parent_Base := Base_Type (Full_View (Parent_Type));
7797 else
7798 Parent_Base := Base_Type (Parent_Type);
7799 end if;
7801 -- AI05-0115 : if this is a derivation from a private type in some
7802 -- other scope that may lead to invisible components for the derived
7803 -- type, mark it accordingly.
7805 if Is_Private_Type (Parent_Type) then
7806 if Scope (Parent_Type) = Scope (Derived_Type) then
7807 null;
7809 elsif In_Open_Scopes (Scope (Parent_Type))
7810 and then In_Private_Part (Scope (Parent_Type))
7811 then
7812 null;
7814 else
7815 Set_Has_Private_Ancestor (Derived_Type);
7816 end if;
7818 else
7819 Set_Has_Private_Ancestor
7820 (Derived_Type, Has_Private_Ancestor (Parent_Type));
7821 end if;
7823 -- Before we start the previously documented transformations, here is
7824 -- little fix for size and alignment of tagged types. Normally when we
7825 -- derive type D from type P, we copy the size and alignment of P as the
7826 -- default for D, and in the absence of explicit representation clauses
7827 -- for D, the size and alignment are indeed the same as the parent.
7829 -- But this is wrong for tagged types, since fields may be added, and
7830 -- the default size may need to be larger, and the default alignment may
7831 -- need to be larger.
7833 -- We therefore reset the size and alignment fields in the tagged case.
7834 -- Note that the size and alignment will in any case be at least as
7835 -- large as the parent type (since the derived type has a copy of the
7836 -- parent type in the _parent field)
7838 -- The type is also marked as being tagged here, which is needed when
7839 -- processing components with a self-referential anonymous access type
7840 -- in the call to Check_Anonymous_Access_Components below. Note that
7841 -- this flag is also set later on for completeness.
7843 if Is_Tagged then
7844 Set_Is_Tagged_Type (Derived_Type);
7845 Init_Size_Align (Derived_Type);
7846 end if;
7848 -- STEP 0a: figure out what kind of derived type declaration we have
7850 if Private_Extension then
7851 Type_Def := N;
7852 Set_Ekind (Derived_Type, E_Record_Type_With_Private);
7853 Set_Default_SSO (Derived_Type);
7855 else
7856 Type_Def := Type_Definition (N);
7858 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
7859 -- Parent_Base can be a private type or private extension. However,
7860 -- for tagged types with an extension the newly added fields are
7861 -- visible and hence the Derived_Type is always an E_Record_Type.
7862 -- (except that the parent may have its own private fields).
7863 -- For untagged types we preserve the Ekind of the Parent_Base.
7865 if Present (Record_Extension_Part (Type_Def)) then
7866 Set_Ekind (Derived_Type, E_Record_Type);
7867 Set_Default_SSO (Derived_Type);
7869 -- Create internal access types for components with anonymous
7870 -- access types.
7872 if Ada_Version >= Ada_2005 then
7873 Check_Anonymous_Access_Components
7874 (N, Derived_Type, Derived_Type,
7875 Component_List (Record_Extension_Part (Type_Def)));
7876 end if;
7878 else
7879 Set_Ekind (Derived_Type, Ekind (Parent_Base));
7880 end if;
7881 end if;
7883 -- Indic can either be an N_Identifier if the subtype indication
7884 -- contains no constraint or an N_Subtype_Indication if the subtype
7885 -- indication has a constraint.
7887 Indic := Subtype_Indication (Type_Def);
7888 Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
7890 -- Check that the type has visible discriminants. The type may be
7891 -- a private type with unknown discriminants whose full view has
7892 -- discriminants which are invisible.
7894 if Constraint_Present then
7895 if not Has_Discriminants (Parent_Base)
7896 or else
7897 (Has_Unknown_Discriminants (Parent_Base)
7898 and then Is_Private_Type (Parent_Base))
7899 then
7900 Error_Msg_N
7901 ("invalid constraint: type has no discriminant",
7902 Constraint (Indic));
7904 Constraint_Present := False;
7905 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
7907 elsif Is_Constrained (Parent_Type) then
7908 Error_Msg_N
7909 ("invalid constraint: parent type is already constrained",
7910 Constraint (Indic));
7912 Constraint_Present := False;
7913 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
7914 end if;
7915 end if;
7917 -- STEP 0b: If needed, apply transformation given in point 5. above
7919 if not Private_Extension
7920 and then Has_Discriminants (Parent_Type)
7921 and then not Discriminant_Specs
7922 and then (Is_Constrained (Parent_Type) or else Constraint_Present)
7923 then
7924 -- First, we must analyze the constraint (see comment in point 5.)
7925 -- The constraint may come from the subtype indication of the full
7926 -- declaration.
7928 if Constraint_Present then
7929 New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
7931 -- If there is no explicit constraint, there might be one that is
7932 -- inherited from a constrained parent type. In that case verify that
7933 -- it conforms to the constraint in the partial view. In perverse
7934 -- cases the parent subtypes of the partial and full view can have
7935 -- different constraints.
7937 elsif Present (Stored_Constraint (Parent_Type)) then
7938 New_Discrs := Stored_Constraint (Parent_Type);
7940 else
7941 New_Discrs := No_Elist;
7942 end if;
7944 if Has_Discriminants (Derived_Type)
7945 and then Has_Private_Declaration (Derived_Type)
7946 and then Present (Discriminant_Constraint (Derived_Type))
7947 and then Present (New_Discrs)
7948 then
7949 -- Verify that constraints of the full view statically match
7950 -- those given in the partial view.
7952 declare
7953 C1, C2 : Elmt_Id;
7955 begin
7956 C1 := First_Elmt (New_Discrs);
7957 C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
7958 while Present (C1) and then Present (C2) loop
7959 if Fully_Conformant_Expressions (Node (C1), Node (C2))
7960 or else
7961 (Is_OK_Static_Expression (Node (C1))
7962 and then Is_OK_Static_Expression (Node (C2))
7963 and then
7964 Expr_Value (Node (C1)) = Expr_Value (Node (C2)))
7965 then
7966 null;
7968 else
7969 if Constraint_Present then
7970 Error_Msg_N
7971 ("constraint not conformant to previous declaration",
7972 Node (C1));
7973 else
7974 Error_Msg_N
7975 ("constraint of full view is incompatible "
7976 & "with partial view", N);
7977 end if;
7978 end if;
7980 Next_Elmt (C1);
7981 Next_Elmt (C2);
7982 end loop;
7983 end;
7984 end if;
7986 -- Insert and analyze the declaration for the unconstrained base type
7988 New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
7990 New_Decl :=
7991 Make_Full_Type_Declaration (Loc,
7992 Defining_Identifier => New_Base,
7993 Type_Definition =>
7994 Make_Derived_Type_Definition (Loc,
7995 Abstract_Present => Abstract_Present (Type_Def),
7996 Limited_Present => Limited_Present (Type_Def),
7997 Subtype_Indication =>
7998 New_Occurrence_Of (Parent_Base, Loc),
7999 Record_Extension_Part =>
8000 Relocate_Node (Record_Extension_Part (Type_Def)),
8001 Interface_List => Interface_List (Type_Def)));
8003 Set_Parent (New_Decl, Parent (N));
8004 Mark_Rewrite_Insertion (New_Decl);
8005 Insert_Before (N, New_Decl);
8007 -- In the extension case, make sure ancestor is frozen appropriately
8008 -- (see also non-discriminated case below).
8010 if Present (Record_Extension_Part (Type_Def))
8011 or else Is_Interface (Parent_Base)
8012 then
8013 Freeze_Before (New_Decl, Parent_Type);
8014 end if;
8016 -- Note that this call passes False for the Derive_Subps parameter
8017 -- because subprogram derivation is deferred until after creating
8018 -- the subtype (see below).
8020 Build_Derived_Type
8021 (New_Decl, Parent_Base, New_Base,
8022 Is_Completion => False, Derive_Subps => False);
8024 -- ??? This needs re-examination to determine whether the
8025 -- above call can simply be replaced by a call to Analyze.
8027 Set_Analyzed (New_Decl);
8029 -- Insert and analyze the declaration for the constrained subtype
8031 if Constraint_Present then
8032 New_Indic :=
8033 Make_Subtype_Indication (Loc,
8034 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
8035 Constraint => Relocate_Node (Constraint (Indic)));
8037 else
8038 declare
8039 Constr_List : constant List_Id := New_List;
8040 C : Elmt_Id;
8041 Expr : Node_Id;
8043 begin
8044 C := First_Elmt (Discriminant_Constraint (Parent_Type));
8045 while Present (C) loop
8046 Expr := Node (C);
8048 -- It is safe here to call New_Copy_Tree since
8049 -- Force_Evaluation was called on each constraint in
8050 -- Build_Discriminant_Constraints.
8052 Append (New_Copy_Tree (Expr), To => Constr_List);
8054 Next_Elmt (C);
8055 end loop;
8057 New_Indic :=
8058 Make_Subtype_Indication (Loc,
8059 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
8060 Constraint =>
8061 Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
8062 end;
8063 end if;
8065 Rewrite (N,
8066 Make_Subtype_Declaration (Loc,
8067 Defining_Identifier => Derived_Type,
8068 Subtype_Indication => New_Indic));
8070 Analyze (N);
8072 -- Derivation of subprograms must be delayed until the full subtype
8073 -- has been established, to ensure proper overriding of subprograms
8074 -- inherited by full types. If the derivations occurred as part of
8075 -- the call to Build_Derived_Type above, then the check for type
8076 -- conformance would fail because earlier primitive subprograms
8077 -- could still refer to the full type prior the change to the new
8078 -- subtype and hence would not match the new base type created here.
8079 -- Subprograms are not derived, however, when Derive_Subps is False
8080 -- (since otherwise there could be redundant derivations).
8082 if Derive_Subps then
8083 Derive_Subprograms (Parent_Type, Derived_Type);
8084 end if;
8086 -- For tagged types the Discriminant_Constraint of the new base itype
8087 -- is inherited from the first subtype so that no subtype conformance
8088 -- problem arise when the first subtype overrides primitive
8089 -- operations inherited by the implicit base type.
8091 if Is_Tagged then
8092 Set_Discriminant_Constraint
8093 (New_Base, Discriminant_Constraint (Derived_Type));
8094 end if;
8096 return;
8097 end if;
8099 -- If we get here Derived_Type will have no discriminants or it will be
8100 -- a discriminated unconstrained base type.
8102 -- STEP 1a: perform preliminary actions/checks for derived tagged types
8104 if Is_Tagged then
8106 -- The parent type is frozen for non-private extensions (RM 13.14(7))
8107 -- The declaration of a specific descendant of an interface type
8108 -- freezes the interface type (RM 13.14).
8110 if not Private_Extension or else Is_Interface (Parent_Base) then
8111 Freeze_Before (N, Parent_Type);
8112 end if;
8114 -- In Ada 2005 (AI-344), the restriction that a derived tagged type
8115 -- cannot be declared at a deeper level than its parent type is
8116 -- removed. The check on derivation within a generic body is also
8117 -- relaxed, but there's a restriction that a derived tagged type
8118 -- cannot be declared in a generic body if it's derived directly
8119 -- or indirectly from a formal type of that generic.
8121 if Ada_Version >= Ada_2005 then
8122 if Present (Enclosing_Generic_Body (Derived_Type)) then
8123 declare
8124 Ancestor_Type : Entity_Id;
8126 begin
8127 -- Check to see if any ancestor of the derived type is a
8128 -- formal type.
8130 Ancestor_Type := Parent_Type;
8131 while not Is_Generic_Type (Ancestor_Type)
8132 and then Etype (Ancestor_Type) /= Ancestor_Type
8133 loop
8134 Ancestor_Type := Etype (Ancestor_Type);
8135 end loop;
8137 -- If the derived type does have a formal type as an
8138 -- ancestor, then it's an error if the derived type is
8139 -- declared within the body of the generic unit that
8140 -- declares the formal type in its generic formal part. It's
8141 -- sufficient to check whether the ancestor type is declared
8142 -- inside the same generic body as the derived type (such as
8143 -- within a nested generic spec), in which case the
8144 -- derivation is legal. If the formal type is declared
8145 -- outside of that generic body, then it's guaranteed that
8146 -- the derived type is declared within the generic body of
8147 -- the generic unit declaring the formal type.
8149 if Is_Generic_Type (Ancestor_Type)
8150 and then Enclosing_Generic_Body (Ancestor_Type) /=
8151 Enclosing_Generic_Body (Derived_Type)
8152 then
8153 Error_Msg_NE
8154 ("parent type of& must not be descendant of formal type"
8155 & " of an enclosing generic body",
8156 Indic, Derived_Type);
8157 end if;
8158 end;
8159 end if;
8161 elsif Type_Access_Level (Derived_Type) /=
8162 Type_Access_Level (Parent_Type)
8163 and then not Is_Generic_Type (Derived_Type)
8164 then
8165 if Is_Controlled (Parent_Type) then
8166 Error_Msg_N
8167 ("controlled type must be declared at the library level",
8168 Indic);
8169 else
8170 Error_Msg_N
8171 ("type extension at deeper accessibility level than parent",
8172 Indic);
8173 end if;
8175 else
8176 declare
8177 GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
8178 begin
8179 if Present (GB)
8180 and then GB /= Enclosing_Generic_Body (Parent_Base)
8181 then
8182 Error_Msg_NE
8183 ("parent type of& must not be outside generic body"
8184 & " (RM 3.9.1(4))",
8185 Indic, Derived_Type);
8186 end if;
8187 end;
8188 end if;
8189 end if;
8191 -- Ada 2005 (AI-251)
8193 if Ada_Version >= Ada_2005 and then Is_Tagged then
8195 -- "The declaration of a specific descendant of an interface type
8196 -- freezes the interface type" (RM 13.14).
8198 declare
8199 Iface : Node_Id;
8200 begin
8201 if Is_Non_Empty_List (Interface_List (Type_Def)) then
8202 Iface := First (Interface_List (Type_Def));
8203 while Present (Iface) loop
8204 Freeze_Before (N, Etype (Iface));
8205 Next (Iface);
8206 end loop;
8207 end if;
8208 end;
8209 end if;
8211 -- STEP 1b : preliminary cleanup of the full view of private types
8213 -- If the type is already marked as having discriminants, then it's the
8214 -- completion of a private type or private extension and we need to
8215 -- retain the discriminants from the partial view if the current
8216 -- declaration has Discriminant_Specifications so that we can verify
8217 -- conformance. However, we must remove any existing components that
8218 -- were inherited from the parent (and attached in Copy_And_Swap)
8219 -- because the full type inherits all appropriate components anyway, and
8220 -- we do not want the partial view's components interfering.
8222 if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
8223 Discrim := First_Discriminant (Derived_Type);
8224 loop
8225 Last_Discrim := Discrim;
8226 Next_Discriminant (Discrim);
8227 exit when No (Discrim);
8228 end loop;
8230 Set_Last_Entity (Derived_Type, Last_Discrim);
8232 -- In all other cases wipe out the list of inherited components (even
8233 -- inherited discriminants), it will be properly rebuilt here.
8235 else
8236 Set_First_Entity (Derived_Type, Empty);
8237 Set_Last_Entity (Derived_Type, Empty);
8238 end if;
8240 -- STEP 1c: Initialize some flags for the Derived_Type
8242 -- The following flags must be initialized here so that
8243 -- Process_Discriminants can check that discriminants of tagged types do
8244 -- not have a default initial value and that access discriminants are
8245 -- only specified for limited records. For completeness, these flags are
8246 -- also initialized along with all the other flags below.
8248 -- AI-419: Limitedness is not inherited from an interface parent, so to
8249 -- be limited in that case the type must be explicitly declared as
8250 -- limited. However, task and protected interfaces are always limited.
8252 if Limited_Present (Type_Def) then
8253 Set_Is_Limited_Record (Derived_Type);
8255 elsif Is_Limited_Record (Parent_Type)
8256 or else (Present (Full_View (Parent_Type))
8257 and then Is_Limited_Record (Full_View (Parent_Type)))
8258 then
8259 if not Is_Interface (Parent_Type)
8260 or else Is_Synchronized_Interface (Parent_Type)
8261 or else Is_Protected_Interface (Parent_Type)
8262 or else Is_Task_Interface (Parent_Type)
8263 then
8264 Set_Is_Limited_Record (Derived_Type);
8265 end if;
8266 end if;
8268 -- STEP 2a: process discriminants of derived type if any
8270 Push_Scope (Derived_Type);
8272 if Discriminant_Specs then
8273 Set_Has_Unknown_Discriminants (Derived_Type, False);
8275 -- The following call initializes fields Has_Discriminants and
8276 -- Discriminant_Constraint, unless we are processing the completion
8277 -- of a private type declaration.
8279 Check_Or_Process_Discriminants (N, Derived_Type);
8281 -- For untagged types, the constraint on the Parent_Type must be
8282 -- present and is used to rename the discriminants.
8284 if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
8285 Error_Msg_N ("untagged parent must have discriminants", Indic);
8287 elsif not Is_Tagged and then not Constraint_Present then
8288 Error_Msg_N
8289 ("discriminant constraint needed for derived untagged records",
8290 Indic);
8292 -- Otherwise the parent subtype must be constrained unless we have a
8293 -- private extension.
8295 elsif not Constraint_Present
8296 and then not Private_Extension
8297 and then not Is_Constrained (Parent_Type)
8298 then
8299 Error_Msg_N
8300 ("unconstrained type not allowed in this context", Indic);
8302 elsif Constraint_Present then
8303 -- The following call sets the field Corresponding_Discriminant
8304 -- for the discriminants in the Derived_Type.
8306 Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
8308 -- For untagged types all new discriminants must rename
8309 -- discriminants in the parent. For private extensions new
8310 -- discriminants cannot rename old ones (implied by [7.3(13)]).
8312 Discrim := First_Discriminant (Derived_Type);
8313 while Present (Discrim) loop
8314 if not Is_Tagged
8315 and then No (Corresponding_Discriminant (Discrim))
8316 then
8317 Error_Msg_N
8318 ("new discriminants must constrain old ones", Discrim);
8320 elsif Private_Extension
8321 and then Present (Corresponding_Discriminant (Discrim))
8322 then
8323 Error_Msg_N
8324 ("only static constraints allowed for parent"
8325 & " discriminants in the partial view", Indic);
8326 exit;
8327 end if;
8329 -- If a new discriminant is used in the constraint, then its
8330 -- subtype must be statically compatible with the parent
8331 -- discriminant's subtype (3.7(15)).
8333 -- However, if the record contains an array constrained by
8334 -- the discriminant but with some different bound, the compiler
8335 -- attemps to create a smaller range for the discriminant type.
8336 -- (See exp_ch3.Adjust_Discriminants). In this case, where
8337 -- the discriminant type is a scalar type, the check must use
8338 -- the original discriminant type in the parent declaration.
8340 declare
8341 Corr_Disc : constant Entity_Id :=
8342 Corresponding_Discriminant (Discrim);
8343 Disc_Type : constant Entity_Id := Etype (Discrim);
8344 Corr_Type : Entity_Id;
8346 begin
8347 if Present (Corr_Disc) then
8348 if Is_Scalar_Type (Disc_Type) then
8349 Corr_Type :=
8350 Entity (Discriminant_Type (Parent (Corr_Disc)));
8351 else
8352 Corr_Type := Etype (Corr_Disc);
8353 end if;
8355 if not
8356 Subtypes_Statically_Compatible (Disc_Type, Corr_Type)
8357 then
8358 Error_Msg_N
8359 ("subtype must be compatible "
8360 & "with parent discriminant",
8361 Discrim);
8362 end if;
8363 end if;
8364 end;
8366 Next_Discriminant (Discrim);
8367 end loop;
8369 -- Check whether the constraints of the full view statically
8370 -- match those imposed by the parent subtype [7.3(13)].
8372 if Present (Stored_Constraint (Derived_Type)) then
8373 declare
8374 C1, C2 : Elmt_Id;
8376 begin
8377 C1 := First_Elmt (Discs);
8378 C2 := First_Elmt (Stored_Constraint (Derived_Type));
8379 while Present (C1) and then Present (C2) loop
8380 if not
8381 Fully_Conformant_Expressions (Node (C1), Node (C2))
8382 then
8383 Error_Msg_N
8384 ("not conformant with previous declaration",
8385 Node (C1));
8386 end if;
8388 Next_Elmt (C1);
8389 Next_Elmt (C2);
8390 end loop;
8391 end;
8392 end if;
8393 end if;
8395 -- STEP 2b: No new discriminants, inherit discriminants if any
8397 else
8398 if Private_Extension then
8399 Set_Has_Unknown_Discriminants
8400 (Derived_Type,
8401 Has_Unknown_Discriminants (Parent_Type)
8402 or else Unknown_Discriminants_Present (N));
8404 -- The partial view of the parent may have unknown discriminants,
8405 -- but if the full view has discriminants and the parent type is
8406 -- in scope they must be inherited.
8408 elsif Has_Unknown_Discriminants (Parent_Type)
8409 and then
8410 (not Has_Discriminants (Parent_Type)
8411 or else not In_Open_Scopes (Scope (Parent_Type)))
8412 then
8413 Set_Has_Unknown_Discriminants (Derived_Type);
8414 end if;
8416 if not Has_Unknown_Discriminants (Derived_Type)
8417 and then not Has_Unknown_Discriminants (Parent_Base)
8418 and then Has_Discriminants (Parent_Type)
8419 then
8420 Inherit_Discrims := True;
8421 Set_Has_Discriminants
8422 (Derived_Type, True);
8423 Set_Discriminant_Constraint
8424 (Derived_Type, Discriminant_Constraint (Parent_Base));
8425 end if;
8427 -- The following test is true for private types (remember
8428 -- transformation 5. is not applied to those) and in an error
8429 -- situation.
8431 if Constraint_Present then
8432 Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
8433 end if;
8435 -- For now mark a new derived type as constrained only if it has no
8436 -- discriminants. At the end of Build_Derived_Record_Type we properly
8437 -- set this flag in the case of private extensions. See comments in
8438 -- point 9. just before body of Build_Derived_Record_Type.
8440 Set_Is_Constrained
8441 (Derived_Type,
8442 not (Inherit_Discrims
8443 or else Has_Unknown_Discriminants (Derived_Type)));
8444 end if;
8446 -- STEP 3: initialize fields of derived type
8448 Set_Is_Tagged_Type (Derived_Type, Is_Tagged);
8449 Set_Stored_Constraint (Derived_Type, No_Elist);
8451 -- Ada 2005 (AI-251): Private type-declarations can implement interfaces
8452 -- but cannot be interfaces
8454 if not Private_Extension
8455 and then Ekind (Derived_Type) /= E_Private_Type
8456 and then Ekind (Derived_Type) /= E_Limited_Private_Type
8457 then
8458 if Interface_Present (Type_Def) then
8459 Analyze_Interface_Declaration (Derived_Type, Type_Def);
8460 end if;
8462 Set_Interfaces (Derived_Type, No_Elist);
8463 end if;
8465 -- Fields inherited from the Parent_Type
8467 Set_Has_Specified_Layout
8468 (Derived_Type, Has_Specified_Layout (Parent_Type));
8469 Set_Is_Limited_Composite
8470 (Derived_Type, Is_Limited_Composite (Parent_Type));
8471 Set_Is_Private_Composite
8472 (Derived_Type, Is_Private_Composite (Parent_Type));
8474 if Is_Tagged_Type (Parent_Type) then
8475 Set_No_Tagged_Streams_Pragma
8476 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
8477 end if;
8479 -- Fields inherited from the Parent_Base
8481 Set_Has_Controlled_Component
8482 (Derived_Type, Has_Controlled_Component (Parent_Base));
8483 Set_Has_Non_Standard_Rep
8484 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
8485 Set_Has_Primitive_Operations
8486 (Derived_Type, Has_Primitive_Operations (Parent_Base));
8488 -- Fields inherited from the Parent_Base in the non-private case
8490 if Ekind (Derived_Type) = E_Record_Type then
8491 Set_Has_Complex_Representation
8492 (Derived_Type, Has_Complex_Representation (Parent_Base));
8493 end if;
8495 -- Fields inherited from the Parent_Base for record types
8497 if Is_Record_Type (Derived_Type) then
8498 declare
8499 Parent_Full : Entity_Id;
8501 begin
8502 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
8503 -- Parent_Base can be a private type or private extension. Go
8504 -- to the full view here to get the E_Record_Type specific flags.
8506 if Present (Full_View (Parent_Base)) then
8507 Parent_Full := Full_View (Parent_Base);
8508 else
8509 Parent_Full := Parent_Base;
8510 end if;
8512 Set_OK_To_Reorder_Components
8513 (Derived_Type, OK_To_Reorder_Components (Parent_Full));
8514 end;
8515 end if;
8517 -- Set fields for private derived types
8519 if Is_Private_Type (Derived_Type) then
8520 Set_Depends_On_Private (Derived_Type, True);
8521 Set_Private_Dependents (Derived_Type, New_Elmt_List);
8523 -- Inherit fields from non private record types. If this is the
8524 -- completion of a derivation from a private type, the parent itself
8525 -- is private, and the attributes come from its full view, which must
8526 -- be present.
8528 else
8529 if Is_Private_Type (Parent_Base)
8530 and then not Is_Record_Type (Parent_Base)
8531 then
8532 Set_Component_Alignment
8533 (Derived_Type, Component_Alignment (Full_View (Parent_Base)));
8534 Set_C_Pass_By_Copy
8535 (Derived_Type, C_Pass_By_Copy (Full_View (Parent_Base)));
8536 else
8537 Set_Component_Alignment
8538 (Derived_Type, Component_Alignment (Parent_Base));
8539 Set_C_Pass_By_Copy
8540 (Derived_Type, C_Pass_By_Copy (Parent_Base));
8541 end if;
8542 end if;
8544 -- Set fields for tagged types
8546 if Is_Tagged then
8547 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
8549 -- All tagged types defined in Ada.Finalization are controlled
8551 if Chars (Scope (Derived_Type)) = Name_Finalization
8552 and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
8553 and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
8554 then
8555 Set_Is_Controlled (Derived_Type);
8556 else
8557 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Base));
8558 end if;
8560 -- Minor optimization: there is no need to generate the class-wide
8561 -- entity associated with an underlying record view.
8563 if not Is_Underlying_Record_View (Derived_Type) then
8564 Make_Class_Wide_Type (Derived_Type);
8565 end if;
8567 Set_Is_Abstract_Type (Derived_Type, Abstract_Present (Type_Def));
8569 if Has_Discriminants (Derived_Type)
8570 and then Constraint_Present
8571 then
8572 Set_Stored_Constraint
8573 (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
8574 end if;
8576 if Ada_Version >= Ada_2005 then
8577 declare
8578 Ifaces_List : Elist_Id;
8580 begin
8581 -- Checks rules 3.9.4 (13/2 and 14/2)
8583 if Comes_From_Source (Derived_Type)
8584 and then not Is_Private_Type (Derived_Type)
8585 and then Is_Interface (Parent_Type)
8586 and then not Is_Interface (Derived_Type)
8587 then
8588 if Is_Task_Interface (Parent_Type) then
8589 Error_Msg_N
8590 ("(Ada 2005) task type required (RM 3.9.4 (13.2))",
8591 Derived_Type);
8593 elsif Is_Protected_Interface (Parent_Type) then
8594 Error_Msg_N
8595 ("(Ada 2005) protected type required (RM 3.9.4 (14.2))",
8596 Derived_Type);
8597 end if;
8598 end if;
8600 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
8602 Check_Interfaces (N, Type_Def);
8604 -- Ada 2005 (AI-251): Collect the list of progenitors that are
8605 -- not already in the parents.
8607 Collect_Interfaces
8608 (T => Derived_Type,
8609 Ifaces_List => Ifaces_List,
8610 Exclude_Parents => True);
8612 Set_Interfaces (Derived_Type, Ifaces_List);
8614 -- If the derived type is the anonymous type created for
8615 -- a declaration whose parent has a constraint, propagate
8616 -- the interface list to the source type. This must be done
8617 -- prior to the completion of the analysis of the source type
8618 -- because the components in the extension may contain current
8619 -- instances whose legality depends on some ancestor.
8621 if Is_Itype (Derived_Type) then
8622 declare
8623 Def : constant Node_Id :=
8624 Associated_Node_For_Itype (Derived_Type);
8625 begin
8626 if Present (Def)
8627 and then Nkind (Def) = N_Full_Type_Declaration
8628 then
8629 Set_Interfaces
8630 (Defining_Identifier (Def), Ifaces_List);
8631 end if;
8632 end;
8633 end if;
8635 -- A type extension is automatically Ghost when one of its
8636 -- progenitors is Ghost (SPARK RM 6.9(9)). This property is
8637 -- also inherited when the parent type is Ghost, but this is
8638 -- done in Build_Derived_Type as the mechanism also handles
8639 -- untagged derivations.
8641 if Implements_Ghost_Interface (Derived_Type) then
8642 Set_Is_Ghost_Entity (Derived_Type);
8643 end if;
8644 end;
8645 end if;
8647 else
8648 Set_Is_Packed (Derived_Type, Is_Packed (Parent_Base));
8649 Set_Has_Non_Standard_Rep
8650 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
8651 end if;
8653 -- STEP 4: Inherit components from the parent base and constrain them.
8654 -- Apply the second transformation described in point 6. above.
8656 if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
8657 or else not Has_Discriminants (Parent_Type)
8658 or else not Is_Constrained (Parent_Type)
8659 then
8660 Constrs := Discs;
8661 else
8662 Constrs := Discriminant_Constraint (Parent_Type);
8663 end if;
8665 Assoc_List :=
8666 Inherit_Components
8667 (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
8669 -- STEP 5a: Copy the parent record declaration for untagged types
8671 if not Is_Tagged then
8673 -- Discriminant_Constraint (Derived_Type) has been properly
8674 -- constructed. Save it and temporarily set it to Empty because we
8675 -- do not want the call to New_Copy_Tree below to mess this list.
8677 if Has_Discriminants (Derived_Type) then
8678 Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
8679 Set_Discriminant_Constraint (Derived_Type, No_Elist);
8680 else
8681 Save_Discr_Constr := No_Elist;
8682 end if;
8684 -- Save the Etype field of Derived_Type. It is correctly set now,
8685 -- but the call to New_Copy tree may remap it to point to itself,
8686 -- which is not what we want. Ditto for the Next_Entity field.
8688 Save_Etype := Etype (Derived_Type);
8689 Save_Next_Entity := Next_Entity (Derived_Type);
8691 -- Assoc_List maps all stored discriminants in the Parent_Base to
8692 -- stored discriminants in the Derived_Type. It is fundamental that
8693 -- no types or itypes with discriminants other than the stored
8694 -- discriminants appear in the entities declared inside
8695 -- Derived_Type, since the back end cannot deal with it.
8697 New_Decl :=
8698 New_Copy_Tree
8699 (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
8701 -- Restore the fields saved prior to the New_Copy_Tree call
8702 -- and compute the stored constraint.
8704 Set_Etype (Derived_Type, Save_Etype);
8705 Set_Next_Entity (Derived_Type, Save_Next_Entity);
8707 if Has_Discriminants (Derived_Type) then
8708 Set_Discriminant_Constraint
8709 (Derived_Type, Save_Discr_Constr);
8710 Set_Stored_Constraint
8711 (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
8712 Replace_Components (Derived_Type, New_Decl);
8713 Set_Has_Implicit_Dereference
8714 (Derived_Type, Has_Implicit_Dereference (Parent_Type));
8715 end if;
8717 -- Insert the new derived type declaration
8719 Rewrite (N, New_Decl);
8721 -- STEP 5b: Complete the processing for record extensions in generics
8723 -- There is no completion for record extensions declared in the
8724 -- parameter part of a generic, so we need to complete processing for
8725 -- these generic record extensions here. The Record_Type_Definition call
8726 -- will change the Ekind of the components from E_Void to E_Component.
8728 elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
8729 Record_Type_Definition (Empty, Derived_Type);
8731 -- STEP 5c: Process the record extension for non private tagged types
8733 elsif not Private_Extension then
8734 Expand_Record_Extension (Derived_Type, Type_Def);
8736 -- Note : previously in ASIS mode we set the Parent_Subtype of the
8737 -- derived type to propagate some semantic information. This led
8738 -- to other ASIS failures and has been removed.
8740 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
8741 -- implemented interfaces if we are in expansion mode
8743 if Expander_Active
8744 and then Has_Interfaces (Derived_Type)
8745 then
8746 Add_Interface_Tag_Components (N, Derived_Type);
8747 end if;
8749 -- Analyze the record extension
8751 Record_Type_Definition
8752 (Record_Extension_Part (Type_Def), Derived_Type);
8753 end if;
8755 End_Scope;
8757 -- Nothing else to do if there is an error in the derivation.
8758 -- An unusual case: the full view may be derived from a type in an
8759 -- instance, when the partial view was used illegally as an actual
8760 -- in that instance, leading to a circular definition.
8762 if Etype (Derived_Type) = Any_Type
8763 or else Etype (Parent_Type) = Derived_Type
8764 then
8765 return;
8766 end if;
8768 -- Set delayed freeze and then derive subprograms, we need to do
8769 -- this in this order so that derived subprograms inherit the
8770 -- derived freeze if necessary.
8772 Set_Has_Delayed_Freeze (Derived_Type);
8774 if Derive_Subps then
8775 Derive_Subprograms (Parent_Type, Derived_Type);
8776 end if;
8778 -- If we have a private extension which defines a constrained derived
8779 -- type mark as constrained here after we have derived subprograms. See
8780 -- comment on point 9. just above the body of Build_Derived_Record_Type.
8782 if Private_Extension and then Inherit_Discrims then
8783 if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
8784 Set_Is_Constrained (Derived_Type, True);
8785 Set_Discriminant_Constraint (Derived_Type, Discs);
8787 elsif Is_Constrained (Parent_Type) then
8788 Set_Is_Constrained
8789 (Derived_Type, True);
8790 Set_Discriminant_Constraint
8791 (Derived_Type, Discriminant_Constraint (Parent_Type));
8792 end if;
8793 end if;
8795 -- Update the class-wide type, which shares the now-completed entity
8796 -- list with its specific type. In case of underlying record views,
8797 -- we do not generate the corresponding class wide entity.
8799 if Is_Tagged
8800 and then not Is_Underlying_Record_View (Derived_Type)
8801 then
8802 Set_First_Entity
8803 (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
8804 Set_Last_Entity
8805 (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
8806 end if;
8808 Check_Function_Writable_Actuals (N);
8809 end Build_Derived_Record_Type;
8811 ------------------------
8812 -- Build_Derived_Type --
8813 ------------------------
8815 procedure Build_Derived_Type
8816 (N : Node_Id;
8817 Parent_Type : Entity_Id;
8818 Derived_Type : Entity_Id;
8819 Is_Completion : Boolean;
8820 Derive_Subps : Boolean := True)
8822 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
8824 begin
8825 -- Set common attributes
8827 Set_Scope (Derived_Type, Current_Scope);
8829 Set_Etype (Derived_Type, Parent_Base);
8830 Set_Ekind (Derived_Type, Ekind (Parent_Base));
8831 Set_Has_Task (Derived_Type, Has_Task (Parent_Base));
8832 Set_Has_Protected (Derived_Type, Has_Protected (Parent_Base));
8834 Set_Size_Info (Derived_Type, Parent_Type);
8835 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
8836 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
8837 Set_Is_Tagged_Type (Derived_Type, Is_Tagged_Type (Parent_Type));
8838 Set_Is_Volatile (Derived_Type, Is_Volatile (Parent_Type));
8840 if Is_Tagged_Type (Derived_Type) then
8841 Set_No_Tagged_Streams_Pragma
8842 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
8843 end if;
8845 -- If the parent has primitive routines, set the derived type link
8847 if Has_Primitive_Operations (Parent_Type) then
8848 Set_Derived_Type_Link (Parent_Base, Derived_Type);
8849 end if;
8851 -- If the parent type is a private subtype, the convention on the base
8852 -- type may be set in the private part, and not propagated to the
8853 -- subtype until later, so we obtain the convention from the base type.
8855 Set_Convention (Derived_Type, Convention (Parent_Base));
8857 -- Set SSO default for record or array type
8859 if (Is_Array_Type (Derived_Type) or else Is_Record_Type (Derived_Type))
8860 and then Is_Base_Type (Derived_Type)
8861 then
8862 Set_Default_SSO (Derived_Type);
8863 end if;
8865 -- Propagate invariant information. The new type has invariants if
8866 -- they are inherited from the parent type, and these invariants can
8867 -- be further inherited, so both flags are set.
8869 -- We similarly inherit predicates
8871 if Has_Predicates (Parent_Type) then
8872 Set_Has_Predicates (Derived_Type);
8873 end if;
8875 -- The derived type inherits the representation clauses of the parent
8877 Inherit_Rep_Item_Chain (Derived_Type, Parent_Type);
8879 -- Propagate the attributes related to pragma Default_Initial_Condition
8880 -- from the parent type to the private extension. A derived type always
8881 -- inherits the default initial condition flag from the parent type. If
8882 -- the derived type carries its own Default_Initial_Condition pragma,
8883 -- the flag is later reset in Analyze_Pragma. Note that both flags are
8884 -- mutually exclusive.
8886 Propagate_Default_Init_Cond_Attributes
8887 (From_Typ => Parent_Type,
8888 To_Typ => Derived_Type,
8889 Parent_To_Derivation => True);
8891 -- If the parent type has delayed rep aspects, then mark the derived
8892 -- type as possibly inheriting a delayed rep aspect.
8894 if Has_Delayed_Rep_Aspects (Parent_Type) then
8895 Set_May_Inherit_Delayed_Rep_Aspects (Derived_Type);
8896 end if;
8898 -- Propagate the attributes related to pragma Ghost from the parent type
8899 -- to the derived type or type extension (SPARK RM 6.9(9)).
8901 if Is_Ghost_Entity (Parent_Type) then
8902 Set_Is_Ghost_Entity (Derived_Type);
8903 end if;
8905 -- Type dependent processing
8907 case Ekind (Parent_Type) is
8908 when Numeric_Kind =>
8909 Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
8911 when Array_Kind =>
8912 Build_Derived_Array_Type (N, Parent_Type, Derived_Type);
8914 when E_Record_Type
8915 | E_Record_Subtype
8916 | Class_Wide_Kind =>
8917 Build_Derived_Record_Type
8918 (N, Parent_Type, Derived_Type, Derive_Subps);
8919 return;
8921 when Enumeration_Kind =>
8922 Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
8924 when Access_Kind =>
8925 Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
8927 when Incomplete_Or_Private_Kind =>
8928 Build_Derived_Private_Type
8929 (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
8931 -- For discriminated types, the derivation includes deriving
8932 -- primitive operations. For others it is done below.
8934 if Is_Tagged_Type (Parent_Type)
8935 or else Has_Discriminants (Parent_Type)
8936 or else (Present (Full_View (Parent_Type))
8937 and then Has_Discriminants (Full_View (Parent_Type)))
8938 then
8939 return;
8940 end if;
8942 when Concurrent_Kind =>
8943 Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
8945 when others =>
8946 raise Program_Error;
8947 end case;
8949 -- Nothing more to do if some error occurred
8951 if Etype (Derived_Type) = Any_Type then
8952 return;
8953 end if;
8955 -- Set delayed freeze and then derive subprograms, we need to do this
8956 -- in this order so that derived subprograms inherit the derived freeze
8957 -- if necessary.
8959 Set_Has_Delayed_Freeze (Derived_Type);
8961 if Derive_Subps then
8962 Derive_Subprograms (Parent_Type, Derived_Type);
8963 end if;
8965 Set_Has_Primitive_Operations
8966 (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
8967 end Build_Derived_Type;
8969 -----------------------
8970 -- Build_Discriminal --
8971 -----------------------
8973 procedure Build_Discriminal (Discrim : Entity_Id) is
8974 D_Minal : Entity_Id;
8975 CR_Disc : Entity_Id;
8977 begin
8978 -- A discriminal has the same name as the discriminant
8980 D_Minal := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
8982 Set_Ekind (D_Minal, E_In_Parameter);
8983 Set_Mechanism (D_Minal, Default_Mechanism);
8984 Set_Etype (D_Minal, Etype (Discrim));
8985 Set_Scope (D_Minal, Current_Scope);
8987 Set_Discriminal (Discrim, D_Minal);
8988 Set_Discriminal_Link (D_Minal, Discrim);
8990 -- For task types, build at once the discriminants of the corresponding
8991 -- record, which are needed if discriminants are used in entry defaults
8992 -- and in family bounds.
8994 if Is_Concurrent_Type (Current_Scope)
8995 or else
8996 Is_Limited_Type (Current_Scope)
8997 then
8998 CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9000 Set_Ekind (CR_Disc, E_In_Parameter);
9001 Set_Mechanism (CR_Disc, Default_Mechanism);
9002 Set_Etype (CR_Disc, Etype (Discrim));
9003 Set_Scope (CR_Disc, Current_Scope);
9004 Set_Discriminal_Link (CR_Disc, Discrim);
9005 Set_CR_Discriminant (Discrim, CR_Disc);
9006 end if;
9007 end Build_Discriminal;
9009 ------------------------------------
9010 -- Build_Discriminant_Constraints --
9011 ------------------------------------
9013 function Build_Discriminant_Constraints
9014 (T : Entity_Id;
9015 Def : Node_Id;
9016 Derived_Def : Boolean := False) return Elist_Id
9018 C : constant Node_Id := Constraint (Def);
9019 Nb_Discr : constant Nat := Number_Discriminants (T);
9021 Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
9022 -- Saves the expression corresponding to a given discriminant in T
9024 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
9025 -- Return the Position number within array Discr_Expr of a discriminant
9026 -- D within the discriminant list of the discriminated type T.
9028 procedure Process_Discriminant_Expression
9029 (Expr : Node_Id;
9030 D : Entity_Id);
9031 -- If this is a discriminant constraint on a partial view, do not
9032 -- generate an overflow check on the discriminant expression. The check
9033 -- will be generated when constraining the full view. Otherwise the
9034 -- backend creates duplicate symbols for the temporaries corresponding
9035 -- to the expressions to be checked, causing spurious assembler errors.
9037 ------------------
9038 -- Pos_Of_Discr --
9039 ------------------
9041 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
9042 Disc : Entity_Id;
9044 begin
9045 Disc := First_Discriminant (T);
9046 for J in Discr_Expr'Range loop
9047 if Disc = D then
9048 return J;
9049 end if;
9051 Next_Discriminant (Disc);
9052 end loop;
9054 -- Note: Since this function is called on discriminants that are
9055 -- known to belong to the discriminated type, falling through the
9056 -- loop with no match signals an internal compiler error.
9058 raise Program_Error;
9059 end Pos_Of_Discr;
9061 -------------------------------------
9062 -- Process_Discriminant_Expression --
9063 -------------------------------------
9065 procedure Process_Discriminant_Expression
9066 (Expr : Node_Id;
9067 D : Entity_Id)
9069 BDT : constant Entity_Id := Base_Type (Etype (D));
9071 begin
9072 -- If this is a discriminant constraint on a partial view, do
9073 -- not generate an overflow on the discriminant expression. The
9074 -- check will be generated when constraining the full view.
9076 if Is_Private_Type (T)
9077 and then Present (Full_View (T))
9078 then
9079 Analyze_And_Resolve (Expr, BDT, Suppress => Overflow_Check);
9080 else
9081 Analyze_And_Resolve (Expr, BDT);
9082 end if;
9083 end Process_Discriminant_Expression;
9085 -- Declarations local to Build_Discriminant_Constraints
9087 Discr : Entity_Id;
9088 E : Entity_Id;
9089 Elist : constant Elist_Id := New_Elmt_List;
9091 Constr : Node_Id;
9092 Expr : Node_Id;
9093 Id : Node_Id;
9094 Position : Nat;
9095 Found : Boolean;
9097 Discrim_Present : Boolean := False;
9099 -- Start of processing for Build_Discriminant_Constraints
9101 begin
9102 -- The following loop will process positional associations only.
9103 -- For a positional association, the (single) discriminant is
9104 -- implicitly specified by position, in textual order (RM 3.7.2).
9106 Discr := First_Discriminant (T);
9107 Constr := First (Constraints (C));
9108 for D in Discr_Expr'Range loop
9109 exit when Nkind (Constr) = N_Discriminant_Association;
9111 if No (Constr) then
9112 Error_Msg_N ("too few discriminants given in constraint", C);
9113 return New_Elmt_List;
9115 elsif Nkind (Constr) = N_Range
9116 or else (Nkind (Constr) = N_Attribute_Reference
9117 and then Attribute_Name (Constr) = Name_Range)
9118 then
9119 Error_Msg_N
9120 ("a range is not a valid discriminant constraint", Constr);
9121 Discr_Expr (D) := Error;
9123 else
9124 Process_Discriminant_Expression (Constr, Discr);
9125 Discr_Expr (D) := Constr;
9126 end if;
9128 Next_Discriminant (Discr);
9129 Next (Constr);
9130 end loop;
9132 if No (Discr) and then Present (Constr) then
9133 Error_Msg_N ("too many discriminants given in constraint", Constr);
9134 return New_Elmt_List;
9135 end if;
9137 -- Named associations can be given in any order, but if both positional
9138 -- and named associations are used in the same discriminant constraint,
9139 -- then positional associations must occur first, at their normal
9140 -- position. Hence once a named association is used, the rest of the
9141 -- discriminant constraint must use only named associations.
9143 while Present (Constr) loop
9145 -- Positional association forbidden after a named association
9147 if Nkind (Constr) /= N_Discriminant_Association then
9148 Error_Msg_N ("positional association follows named one", Constr);
9149 return New_Elmt_List;
9151 -- Otherwise it is a named association
9153 else
9154 -- E records the type of the discriminants in the named
9155 -- association. All the discriminants specified in the same name
9156 -- association must have the same type.
9158 E := Empty;
9160 -- Search the list of discriminants in T to see if the simple name
9161 -- given in the constraint matches any of them.
9163 Id := First (Selector_Names (Constr));
9164 while Present (Id) loop
9165 Found := False;
9167 -- If Original_Discriminant is present, we are processing a
9168 -- generic instantiation and this is an instance node. We need
9169 -- to find the name of the corresponding discriminant in the
9170 -- actual record type T and not the name of the discriminant in
9171 -- the generic formal. Example:
9173 -- generic
9174 -- type G (D : int) is private;
9175 -- package P is
9176 -- subtype W is G (D => 1);
9177 -- end package;
9178 -- type Rec (X : int) is record ... end record;
9179 -- package Q is new P (G => Rec);
9181 -- At the point of the instantiation, formal type G is Rec
9182 -- and therefore when reanalyzing "subtype W is G (D => 1);"
9183 -- which really looks like "subtype W is Rec (D => 1);" at
9184 -- the point of instantiation, we want to find the discriminant
9185 -- that corresponds to D in Rec, i.e. X.
9187 if Present (Original_Discriminant (Id))
9188 and then In_Instance
9189 then
9190 Discr := Find_Corresponding_Discriminant (Id, T);
9191 Found := True;
9193 else
9194 Discr := First_Discriminant (T);
9195 while Present (Discr) loop
9196 if Chars (Discr) = Chars (Id) then
9197 Found := True;
9198 exit;
9199 end if;
9201 Next_Discriminant (Discr);
9202 end loop;
9204 if not Found then
9205 Error_Msg_N ("& does not match any discriminant", Id);
9206 return New_Elmt_List;
9208 -- If the parent type is a generic formal, preserve the
9209 -- name of the discriminant for subsequent instances.
9210 -- see comment at the beginning of this if statement.
9212 elsif Is_Generic_Type (Root_Type (T)) then
9213 Set_Original_Discriminant (Id, Discr);
9214 end if;
9215 end if;
9217 Position := Pos_Of_Discr (T, Discr);
9219 if Present (Discr_Expr (Position)) then
9220 Error_Msg_N ("duplicate constraint for discriminant&", Id);
9222 else
9223 -- Each discriminant specified in the same named association
9224 -- must be associated with a separate copy of the
9225 -- corresponding expression.
9227 if Present (Next (Id)) then
9228 Expr := New_Copy_Tree (Expression (Constr));
9229 Set_Parent (Expr, Parent (Expression (Constr)));
9230 else
9231 Expr := Expression (Constr);
9232 end if;
9234 Discr_Expr (Position) := Expr;
9235 Process_Discriminant_Expression (Expr, Discr);
9236 end if;
9238 -- A discriminant association with more than one discriminant
9239 -- name is only allowed if the named discriminants are all of
9240 -- the same type (RM 3.7.1(8)).
9242 if E = Empty then
9243 E := Base_Type (Etype (Discr));
9245 elsif Base_Type (Etype (Discr)) /= E then
9246 Error_Msg_N
9247 ("all discriminants in an association " &
9248 "must have the same type", Id);
9249 end if;
9251 Next (Id);
9252 end loop;
9253 end if;
9255 Next (Constr);
9256 end loop;
9258 -- A discriminant constraint must provide exactly one value for each
9259 -- discriminant of the type (RM 3.7.1(8)).
9261 for J in Discr_Expr'Range loop
9262 if No (Discr_Expr (J)) then
9263 Error_Msg_N ("too few discriminants given in constraint", C);
9264 return New_Elmt_List;
9265 end if;
9266 end loop;
9268 -- Determine if there are discriminant expressions in the constraint
9270 for J in Discr_Expr'Range loop
9271 if Denotes_Discriminant
9272 (Discr_Expr (J), Check_Concurrent => True)
9273 then
9274 Discrim_Present := True;
9275 end if;
9276 end loop;
9278 -- Build an element list consisting of the expressions given in the
9279 -- discriminant constraint and apply the appropriate checks. The list
9280 -- is constructed after resolving any named discriminant associations
9281 -- and therefore the expressions appear in the textual order of the
9282 -- discriminants.
9284 Discr := First_Discriminant (T);
9285 for J in Discr_Expr'Range loop
9286 if Discr_Expr (J) /= Error then
9287 Append_Elmt (Discr_Expr (J), Elist);
9289 -- If any of the discriminant constraints is given by a
9290 -- discriminant and we are in a derived type declaration we
9291 -- have a discriminant renaming. Establish link between new
9292 -- and old discriminant.
9294 if Denotes_Discriminant (Discr_Expr (J)) then
9295 if Derived_Def then
9296 Set_Corresponding_Discriminant
9297 (Entity (Discr_Expr (J)), Discr);
9298 end if;
9300 -- Force the evaluation of non-discriminant expressions.
9301 -- If we have found a discriminant in the constraint 3.4(26)
9302 -- and 3.8(18) demand that no range checks are performed are
9303 -- after evaluation. If the constraint is for a component
9304 -- definition that has a per-object constraint, expressions are
9305 -- evaluated but not checked either. In all other cases perform
9306 -- a range check.
9308 else
9309 if Discrim_Present then
9310 null;
9312 elsif Nkind (Parent (Parent (Def))) = N_Component_Declaration
9313 and then
9314 Has_Per_Object_Constraint
9315 (Defining_Identifier (Parent (Parent (Def))))
9316 then
9317 null;
9319 elsif Is_Access_Type (Etype (Discr)) then
9320 Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
9322 else
9323 Apply_Range_Check (Discr_Expr (J), Etype (Discr));
9324 end if;
9326 Force_Evaluation (Discr_Expr (J));
9327 end if;
9329 -- Check that the designated type of an access discriminant's
9330 -- expression is not a class-wide type unless the discriminant's
9331 -- designated type is also class-wide.
9333 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
9334 and then not Is_Class_Wide_Type
9335 (Designated_Type (Etype (Discr)))
9336 and then Etype (Discr_Expr (J)) /= Any_Type
9337 and then Is_Class_Wide_Type
9338 (Designated_Type (Etype (Discr_Expr (J))))
9339 then
9340 Wrong_Type (Discr_Expr (J), Etype (Discr));
9342 elsif Is_Access_Type (Etype (Discr))
9343 and then not Is_Access_Constant (Etype (Discr))
9344 and then Is_Access_Type (Etype (Discr_Expr (J)))
9345 and then Is_Access_Constant (Etype (Discr_Expr (J)))
9346 then
9347 Error_Msg_NE
9348 ("constraint for discriminant& must be access to variable",
9349 Def, Discr);
9350 end if;
9351 end if;
9353 Next_Discriminant (Discr);
9354 end loop;
9356 return Elist;
9357 end Build_Discriminant_Constraints;
9359 ---------------------------------
9360 -- Build_Discriminated_Subtype --
9361 ---------------------------------
9363 procedure Build_Discriminated_Subtype
9364 (T : Entity_Id;
9365 Def_Id : Entity_Id;
9366 Elist : Elist_Id;
9367 Related_Nod : Node_Id;
9368 For_Access : Boolean := False)
9370 Has_Discrs : constant Boolean := Has_Discriminants (T);
9371 Constrained : constant Boolean :=
9372 (Has_Discrs
9373 and then not Is_Empty_Elmt_List (Elist)
9374 and then not Is_Class_Wide_Type (T))
9375 or else Is_Constrained (T);
9377 begin
9378 if Ekind (T) = E_Record_Type then
9379 if For_Access then
9380 Set_Ekind (Def_Id, E_Private_Subtype);
9381 Set_Is_For_Access_Subtype (Def_Id, True);
9382 else
9383 Set_Ekind (Def_Id, E_Record_Subtype);
9384 end if;
9386 -- Inherit preelaboration flag from base, for types for which it
9387 -- may have been set: records, private types, protected types.
9389 Set_Known_To_Have_Preelab_Init
9390 (Def_Id, Known_To_Have_Preelab_Init (T));
9392 elsif Ekind (T) = E_Task_Type then
9393 Set_Ekind (Def_Id, E_Task_Subtype);
9395 elsif Ekind (T) = E_Protected_Type then
9396 Set_Ekind (Def_Id, E_Protected_Subtype);
9397 Set_Known_To_Have_Preelab_Init
9398 (Def_Id, Known_To_Have_Preelab_Init (T));
9400 elsif Is_Private_Type (T) then
9401 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
9402 Set_Known_To_Have_Preelab_Init
9403 (Def_Id, Known_To_Have_Preelab_Init (T));
9405 -- Private subtypes may have private dependents
9407 Set_Private_Dependents (Def_Id, New_Elmt_List);
9409 elsif Is_Class_Wide_Type (T) then
9410 Set_Ekind (Def_Id, E_Class_Wide_Subtype);
9412 else
9413 -- Incomplete type. Attach subtype to list of dependents, to be
9414 -- completed with full view of parent type, unless is it the
9415 -- designated subtype of a record component within an init_proc.
9416 -- This last case arises for a component of an access type whose
9417 -- designated type is incomplete (e.g. a Taft Amendment type).
9418 -- The designated subtype is within an inner scope, and needs no
9419 -- elaboration, because only the access type is needed in the
9420 -- initialization procedure.
9422 Set_Ekind (Def_Id, Ekind (T));
9424 if For_Access and then Within_Init_Proc then
9425 null;
9426 else
9427 Append_Elmt (Def_Id, Private_Dependents (T));
9428 end if;
9429 end if;
9431 Set_Etype (Def_Id, T);
9432 Init_Size_Align (Def_Id);
9433 Set_Has_Discriminants (Def_Id, Has_Discrs);
9434 Set_Is_Constrained (Def_Id, Constrained);
9436 Set_First_Entity (Def_Id, First_Entity (T));
9437 Set_Last_Entity (Def_Id, Last_Entity (T));
9438 Set_Has_Implicit_Dereference
9439 (Def_Id, Has_Implicit_Dereference (T));
9441 -- If the subtype is the completion of a private declaration, there may
9442 -- have been representation clauses for the partial view, and they must
9443 -- be preserved. Build_Derived_Type chains the inherited clauses with
9444 -- the ones appearing on the extension. If this comes from a subtype
9445 -- declaration, all clauses are inherited.
9447 if No (First_Rep_Item (Def_Id)) then
9448 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
9449 end if;
9451 if Is_Tagged_Type (T) then
9452 Set_Is_Tagged_Type (Def_Id);
9453 Set_No_Tagged_Streams_Pragma (Def_Id, No_Tagged_Streams_Pragma (T));
9454 Make_Class_Wide_Type (Def_Id);
9455 end if;
9457 Set_Stored_Constraint (Def_Id, No_Elist);
9459 if Has_Discrs then
9460 Set_Discriminant_Constraint (Def_Id, Elist);
9461 Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
9462 end if;
9464 if Is_Tagged_Type (T) then
9466 -- Ada 2005 (AI-251): In case of concurrent types we inherit the
9467 -- concurrent record type (which has the list of primitive
9468 -- operations).
9470 if Ada_Version >= Ada_2005
9471 and then Is_Concurrent_Type (T)
9472 then
9473 Set_Corresponding_Record_Type (Def_Id,
9474 Corresponding_Record_Type (T));
9475 else
9476 Set_Direct_Primitive_Operations (Def_Id,
9477 Direct_Primitive_Operations (T));
9478 end if;
9480 Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
9481 end if;
9483 -- Subtypes introduced by component declarations do not need to be
9484 -- marked as delayed, and do not get freeze nodes, because the semantics
9485 -- verifies that the parents of the subtypes are frozen before the
9486 -- enclosing record is frozen.
9488 if not Is_Type (Scope (Def_Id)) then
9489 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
9491 if Is_Private_Type (T)
9492 and then Present (Full_View (T))
9493 then
9494 Conditional_Delay (Def_Id, Full_View (T));
9495 else
9496 Conditional_Delay (Def_Id, T);
9497 end if;
9498 end if;
9500 if Is_Record_Type (T) then
9501 Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
9503 if Has_Discrs
9504 and then not Is_Empty_Elmt_List (Elist)
9505 and then not For_Access
9506 then
9507 Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
9508 elsif not For_Access then
9509 Set_Cloned_Subtype (Def_Id, T);
9510 end if;
9511 end if;
9512 end Build_Discriminated_Subtype;
9514 ---------------------------
9515 -- Build_Itype_Reference --
9516 ---------------------------
9518 procedure Build_Itype_Reference
9519 (Ityp : Entity_Id;
9520 Nod : Node_Id)
9522 IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
9523 begin
9525 -- Itype references are only created for use by the back-end
9527 if Inside_A_Generic then
9528 return;
9529 else
9530 Set_Itype (IR, Ityp);
9531 Insert_After (Nod, IR);
9532 end if;
9533 end Build_Itype_Reference;
9535 ------------------------
9536 -- Build_Scalar_Bound --
9537 ------------------------
9539 function Build_Scalar_Bound
9540 (Bound : Node_Id;
9541 Par_T : Entity_Id;
9542 Der_T : Entity_Id) return Node_Id
9544 New_Bound : Entity_Id;
9546 begin
9547 -- Note: not clear why this is needed, how can the original bound
9548 -- be unanalyzed at this point? and if it is, what business do we
9549 -- have messing around with it? and why is the base type of the
9550 -- parent type the right type for the resolution. It probably is
9551 -- not. It is OK for the new bound we are creating, but not for
9552 -- the old one??? Still if it never happens, no problem.
9554 Analyze_And_Resolve (Bound, Base_Type (Par_T));
9556 if Nkind_In (Bound, N_Integer_Literal, N_Real_Literal) then
9557 New_Bound := New_Copy (Bound);
9558 Set_Etype (New_Bound, Der_T);
9559 Set_Analyzed (New_Bound);
9561 elsif Is_Entity_Name (Bound) then
9562 New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
9564 -- The following is almost certainly wrong. What business do we have
9565 -- relocating a node (Bound) that is presumably still attached to
9566 -- the tree elsewhere???
9568 else
9569 New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
9570 end if;
9572 Set_Etype (New_Bound, Der_T);
9573 return New_Bound;
9574 end Build_Scalar_Bound;
9576 --------------------------------
9577 -- Build_Underlying_Full_View --
9578 --------------------------------
9580 procedure Build_Underlying_Full_View
9581 (N : Node_Id;
9582 Typ : Entity_Id;
9583 Par : Entity_Id)
9585 Loc : constant Source_Ptr := Sloc (N);
9586 Subt : constant Entity_Id :=
9587 Make_Defining_Identifier
9588 (Loc, New_External_Name (Chars (Typ), 'S'));
9590 Constr : Node_Id;
9591 Indic : Node_Id;
9592 C : Node_Id;
9593 Id : Node_Id;
9595 procedure Set_Discriminant_Name (Id : Node_Id);
9596 -- If the derived type has discriminants, they may rename discriminants
9597 -- of the parent. When building the full view of the parent, we need to
9598 -- recover the names of the original discriminants if the constraint is
9599 -- given by named associations.
9601 ---------------------------
9602 -- Set_Discriminant_Name --
9603 ---------------------------
9605 procedure Set_Discriminant_Name (Id : Node_Id) is
9606 Disc : Entity_Id;
9608 begin
9609 Set_Original_Discriminant (Id, Empty);
9611 if Has_Discriminants (Typ) then
9612 Disc := First_Discriminant (Typ);
9613 while Present (Disc) loop
9614 if Chars (Disc) = Chars (Id)
9615 and then Present (Corresponding_Discriminant (Disc))
9616 then
9617 Set_Chars (Id, Chars (Corresponding_Discriminant (Disc)));
9618 end if;
9619 Next_Discriminant (Disc);
9620 end loop;
9621 end if;
9622 end Set_Discriminant_Name;
9624 -- Start of processing for Build_Underlying_Full_View
9626 begin
9627 if Nkind (N) = N_Full_Type_Declaration then
9628 Constr := Constraint (Subtype_Indication (Type_Definition (N)));
9630 elsif Nkind (N) = N_Subtype_Declaration then
9631 Constr := New_Copy_Tree (Constraint (Subtype_Indication (N)));
9633 elsif Nkind (N) = N_Component_Declaration then
9634 Constr :=
9635 New_Copy_Tree
9636 (Constraint (Subtype_Indication (Component_Definition (N))));
9638 else
9639 raise Program_Error;
9640 end if;
9642 C := First (Constraints (Constr));
9643 while Present (C) loop
9644 if Nkind (C) = N_Discriminant_Association then
9645 Id := First (Selector_Names (C));
9646 while Present (Id) loop
9647 Set_Discriminant_Name (Id);
9648 Next (Id);
9649 end loop;
9650 end if;
9652 Next (C);
9653 end loop;
9655 Indic :=
9656 Make_Subtype_Declaration (Loc,
9657 Defining_Identifier => Subt,
9658 Subtype_Indication =>
9659 Make_Subtype_Indication (Loc,
9660 Subtype_Mark => New_Occurrence_Of (Par, Loc),
9661 Constraint => New_Copy_Tree (Constr)));
9663 -- If this is a component subtype for an outer itype, it is not
9664 -- a list member, so simply set the parent link for analysis: if
9665 -- the enclosing type does not need to be in a declarative list,
9666 -- neither do the components.
9668 if Is_List_Member (N)
9669 and then Nkind (N) /= N_Component_Declaration
9670 then
9671 Insert_Before (N, Indic);
9672 else
9673 Set_Parent (Indic, Parent (N));
9674 end if;
9676 Analyze (Indic);
9677 Set_Underlying_Full_View (Typ, Full_View (Subt));
9678 end Build_Underlying_Full_View;
9680 -------------------------------
9681 -- Check_Abstract_Overriding --
9682 -------------------------------
9684 procedure Check_Abstract_Overriding (T : Entity_Id) is
9685 Alias_Subp : Entity_Id;
9686 Elmt : Elmt_Id;
9687 Op_List : Elist_Id;
9688 Subp : Entity_Id;
9689 Type_Def : Node_Id;
9691 procedure Check_Pragma_Implemented (Subp : Entity_Id);
9692 -- Ada 2012 (AI05-0030): Subprogram Subp overrides an interface routine
9693 -- which has pragma Implemented already set. Check whether Subp's entity
9694 -- kind conforms to the implementation kind of the overridden routine.
9696 procedure Check_Pragma_Implemented
9697 (Subp : Entity_Id;
9698 Iface_Subp : Entity_Id);
9699 -- Ada 2012 (AI05-0030): Subprogram Subp overrides interface routine
9700 -- Iface_Subp and both entities have pragma Implemented already set on
9701 -- them. Check whether the two implementation kinds are conforming.
9703 procedure Inherit_Pragma_Implemented
9704 (Subp : Entity_Id;
9705 Iface_Subp : Entity_Id);
9706 -- Ada 2012 (AI05-0030): Interface primitive Subp overrides interface
9707 -- subprogram Iface_Subp which has been marked by pragma Implemented.
9708 -- Propagate the implementation kind of Iface_Subp to Subp.
9710 ------------------------------
9711 -- Check_Pragma_Implemented --
9712 ------------------------------
9714 procedure Check_Pragma_Implemented (Subp : Entity_Id) is
9715 Iface_Alias : constant Entity_Id := Interface_Alias (Subp);
9716 Impl_Kind : constant Name_Id := Implementation_Kind (Iface_Alias);
9717 Subp_Alias : constant Entity_Id := Alias (Subp);
9718 Contr_Typ : Entity_Id;
9719 Impl_Subp : Entity_Id;
9721 begin
9722 -- Subp must have an alias since it is a hidden entity used to link
9723 -- an interface subprogram to its overriding counterpart.
9725 pragma Assert (Present (Subp_Alias));
9727 -- Handle aliases to synchronized wrappers
9729 Impl_Subp := Subp_Alias;
9731 if Is_Primitive_Wrapper (Impl_Subp) then
9732 Impl_Subp := Wrapped_Entity (Impl_Subp);
9733 end if;
9735 -- Extract the type of the controlling formal
9737 Contr_Typ := Etype (First_Formal (Subp_Alias));
9739 if Is_Concurrent_Record_Type (Contr_Typ) then
9740 Contr_Typ := Corresponding_Concurrent_Type (Contr_Typ);
9741 end if;
9743 -- An interface subprogram whose implementation kind is By_Entry must
9744 -- be implemented by an entry.
9746 if Impl_Kind = Name_By_Entry
9747 and then Ekind (Impl_Subp) /= E_Entry
9748 then
9749 Error_Msg_Node_2 := Iface_Alias;
9750 Error_Msg_NE
9751 ("type & must implement abstract subprogram & with an entry",
9752 Subp_Alias, Contr_Typ);
9754 elsif Impl_Kind = Name_By_Protected_Procedure then
9756 -- An interface subprogram whose implementation kind is By_
9757 -- Protected_Procedure cannot be implemented by a primitive
9758 -- procedure of a task type.
9760 if Ekind (Contr_Typ) /= E_Protected_Type then
9761 Error_Msg_Node_2 := Contr_Typ;
9762 Error_Msg_NE
9763 ("interface subprogram & cannot be implemented by a " &
9764 "primitive procedure of task type &", Subp_Alias,
9765 Iface_Alias);
9767 -- An interface subprogram whose implementation kind is By_
9768 -- Protected_Procedure must be implemented by a procedure.
9770 elsif Ekind (Impl_Subp) /= E_Procedure then
9771 Error_Msg_Node_2 := Iface_Alias;
9772 Error_Msg_NE
9773 ("type & must implement abstract subprogram & with a " &
9774 "procedure", Subp_Alias, Contr_Typ);
9776 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
9777 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
9778 then
9779 Error_Msg_Name_1 := Impl_Kind;
9780 Error_Msg_N
9781 ("overriding operation& must have synchronization%",
9782 Subp_Alias);
9783 end if;
9785 -- If primitive has Optional synchronization, overriding operation
9786 -- must match if it has an explicit synchronization..
9788 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
9789 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
9790 then
9791 Error_Msg_Name_1 := Impl_Kind;
9792 Error_Msg_N
9793 ("overriding operation& must have syncrhonization%",
9794 Subp_Alias);
9795 end if;
9796 end Check_Pragma_Implemented;
9798 ------------------------------
9799 -- Check_Pragma_Implemented --
9800 ------------------------------
9802 procedure Check_Pragma_Implemented
9803 (Subp : Entity_Id;
9804 Iface_Subp : Entity_Id)
9806 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
9807 Subp_Kind : constant Name_Id := Implementation_Kind (Subp);
9809 begin
9810 -- Ada 2012 (AI05-0030): The implementation kinds of an overridden
9811 -- and overriding subprogram are different. In general this is an
9812 -- error except when the implementation kind of the overridden
9813 -- subprograms is By_Any or Optional.
9815 if Iface_Kind /= Subp_Kind
9816 and then Iface_Kind /= Name_By_Any
9817 and then Iface_Kind /= Name_Optional
9818 then
9819 if Iface_Kind = Name_By_Entry then
9820 Error_Msg_N
9821 ("incompatible implementation kind, overridden subprogram " &
9822 "is marked By_Entry", Subp);
9823 else
9824 Error_Msg_N
9825 ("incompatible implementation kind, overridden subprogram " &
9826 "is marked By_Protected_Procedure", Subp);
9827 end if;
9828 end if;
9829 end Check_Pragma_Implemented;
9831 --------------------------------
9832 -- Inherit_Pragma_Implemented --
9833 --------------------------------
9835 procedure Inherit_Pragma_Implemented
9836 (Subp : Entity_Id;
9837 Iface_Subp : Entity_Id)
9839 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
9840 Loc : constant Source_Ptr := Sloc (Subp);
9841 Impl_Prag : Node_Id;
9843 begin
9844 -- Since the implementation kind is stored as a representation item
9845 -- rather than a flag, create a pragma node.
9847 Impl_Prag :=
9848 Make_Pragma (Loc,
9849 Chars => Name_Implemented,
9850 Pragma_Argument_Associations => New_List (
9851 Make_Pragma_Argument_Association (Loc,
9852 Expression => New_Occurrence_Of (Subp, Loc)),
9854 Make_Pragma_Argument_Association (Loc,
9855 Expression => Make_Identifier (Loc, Iface_Kind))));
9857 -- The pragma doesn't need to be analyzed because it is internally
9858 -- built. It is safe to directly register it as a rep item since we
9859 -- are only interested in the characters of the implementation kind.
9861 Record_Rep_Item (Subp, Impl_Prag);
9862 end Inherit_Pragma_Implemented;
9864 -- Start of processing for Check_Abstract_Overriding
9866 begin
9867 Op_List := Primitive_Operations (T);
9869 -- Loop to check primitive operations
9871 Elmt := First_Elmt (Op_List);
9872 while Present (Elmt) loop
9873 Subp := Node (Elmt);
9874 Alias_Subp := Alias (Subp);
9876 -- Inherited subprograms are identified by the fact that they do not
9877 -- come from source, and the associated source location is the
9878 -- location of the first subtype of the derived type.
9880 -- Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
9881 -- subprograms that "require overriding".
9883 -- Special exception, do not complain about failure to override the
9884 -- stream routines _Input and _Output, as well as the primitive
9885 -- operations used in dispatching selects since we always provide
9886 -- automatic overridings for these subprograms.
9888 -- Also ignore this rule for convention CIL since .NET libraries
9889 -- do bizarre things with interfaces???
9891 -- The partial view of T may have been a private extension, for
9892 -- which inherited functions dispatching on result are abstract.
9893 -- If the full view is a null extension, there is no need for
9894 -- overriding in Ada 2005, but wrappers need to be built for them
9895 -- (see exp_ch3, Build_Controlling_Function_Wrappers).
9897 if Is_Null_Extension (T)
9898 and then Has_Controlling_Result (Subp)
9899 and then Ada_Version >= Ada_2005
9900 and then Present (Alias_Subp)
9901 and then not Comes_From_Source (Subp)
9902 and then not Is_Abstract_Subprogram (Alias_Subp)
9903 and then not Is_Access_Type (Etype (Subp))
9904 then
9905 null;
9907 -- Ada 2005 (AI-251): Internal entities of interfaces need no
9908 -- processing because this check is done with the aliased
9909 -- entity
9911 elsif Present (Interface_Alias (Subp)) then
9912 null;
9914 elsif (Is_Abstract_Subprogram (Subp)
9915 or else Requires_Overriding (Subp)
9916 or else
9917 (Has_Controlling_Result (Subp)
9918 and then Present (Alias_Subp)
9919 and then not Comes_From_Source (Subp)
9920 and then Sloc (Subp) = Sloc (First_Subtype (T))))
9921 and then not Is_TSS (Subp, TSS_Stream_Input)
9922 and then not Is_TSS (Subp, TSS_Stream_Output)
9923 and then not Is_Abstract_Type (T)
9924 and then Convention (T) /= Convention_CIL
9925 and then not Is_Predefined_Interface_Primitive (Subp)
9927 -- Ada 2005 (AI-251): Do not consider hidden entities associated
9928 -- with abstract interface types because the check will be done
9929 -- with the aliased entity (otherwise we generate a duplicated
9930 -- error message).
9932 and then not Present (Interface_Alias (Subp))
9933 then
9934 if Present (Alias_Subp) then
9936 -- Only perform the check for a derived subprogram when the
9937 -- type has an explicit record extension. This avoids incorrect
9938 -- flagging of abstract subprograms for the case of a type
9939 -- without an extension that is derived from a formal type
9940 -- with a tagged actual (can occur within a private part).
9942 -- Ada 2005 (AI-391): In the case of an inherited function with
9943 -- a controlling result of the type, the rule does not apply if
9944 -- the type is a null extension (unless the parent function
9945 -- itself is abstract, in which case the function must still be
9946 -- be overridden). The expander will generate an overriding
9947 -- wrapper function calling the parent subprogram (see
9948 -- Exp_Ch3.Make_Controlling_Wrapper_Functions).
9950 Type_Def := Type_Definition (Parent (T));
9952 if Nkind (Type_Def) = N_Derived_Type_Definition
9953 and then Present (Record_Extension_Part (Type_Def))
9954 and then
9955 (Ada_Version < Ada_2005
9956 or else not Is_Null_Extension (T)
9957 or else Ekind (Subp) = E_Procedure
9958 or else not Has_Controlling_Result (Subp)
9959 or else Is_Abstract_Subprogram (Alias_Subp)
9960 or else Requires_Overriding (Subp)
9961 or else Is_Access_Type (Etype (Subp)))
9962 then
9963 -- Avoid reporting error in case of abstract predefined
9964 -- primitive inherited from interface type because the
9965 -- body of internally generated predefined primitives
9966 -- of tagged types are generated later by Freeze_Type
9968 if Is_Interface (Root_Type (T))
9969 and then Is_Abstract_Subprogram (Subp)
9970 and then Is_Predefined_Dispatching_Operation (Subp)
9971 and then not Comes_From_Source (Ultimate_Alias (Subp))
9972 then
9973 null;
9975 -- A null extension is not obliged to override an inherited
9976 -- procedure subject to pragma Extensions_Visible with value
9977 -- False and at least one controlling OUT parameter
9978 -- (SPARK RM 6.1.7(6)).
9980 elsif Is_Null_Extension (T)
9981 and then Is_EVF_Procedure (Subp)
9982 then
9983 null;
9985 else
9986 Error_Msg_NE
9987 ("type must be declared abstract or & overridden",
9988 T, Subp);
9990 -- Traverse the whole chain of aliased subprograms to
9991 -- complete the error notification. This is especially
9992 -- useful for traceability of the chain of entities when
9993 -- the subprogram corresponds with an interface
9994 -- subprogram (which may be defined in another package).
9996 if Present (Alias_Subp) then
9997 declare
9998 E : Entity_Id;
10000 begin
10001 E := Subp;
10002 while Present (Alias (E)) loop
10004 -- Avoid reporting redundant errors on entities
10005 -- inherited from interfaces
10007 if Sloc (E) /= Sloc (T) then
10008 Error_Msg_Sloc := Sloc (E);
10009 Error_Msg_NE
10010 ("\& has been inherited #", T, Subp);
10011 end if;
10013 E := Alias (E);
10014 end loop;
10016 Error_Msg_Sloc := Sloc (E);
10018 -- AI05-0068: report if there is an overriding
10019 -- non-abstract subprogram that is invisible.
10021 if Is_Hidden (E)
10022 and then not Is_Abstract_Subprogram (E)
10023 then
10024 Error_Msg_NE
10025 ("\& subprogram# is not visible",
10026 T, Subp);
10028 -- Clarify the case where a non-null extension must
10029 -- override inherited procedure subject to pragma
10030 -- Extensions_Visible with value False and at least
10031 -- one controlling OUT param.
10033 elsif Is_EVF_Procedure (E) then
10034 Error_Msg_NE
10035 ("\& # is subject to Extensions_Visible False",
10036 T, Subp);
10038 else
10039 Error_Msg_NE
10040 ("\& has been inherited from subprogram #",
10041 T, Subp);
10042 end if;
10043 end;
10044 end if;
10045 end if;
10047 -- Ada 2005 (AI-345): Protected or task type implementing
10048 -- abstract interfaces.
10050 elsif Is_Concurrent_Record_Type (T)
10051 and then Present (Interfaces (T))
10052 then
10053 -- There is no need to check here RM 9.4(11.9/3) since we
10054 -- are processing the corresponding record type and the
10055 -- mode of the overriding subprograms was verified by
10056 -- Check_Conformance when the corresponding concurrent
10057 -- type declaration was analyzed.
10059 Error_Msg_NE
10060 ("interface subprogram & must be overridden", T, Subp);
10062 -- Examine primitive operations of synchronized type to find
10063 -- homonyms that have the wrong profile.
10065 declare
10066 Prim : Entity_Id;
10068 begin
10069 Prim := First_Entity (Corresponding_Concurrent_Type (T));
10070 while Present (Prim) loop
10071 if Chars (Prim) = Chars (Subp) then
10072 Error_Msg_NE
10073 ("profile is not type conformant with prefixed "
10074 & "view profile of inherited operation&",
10075 Prim, Subp);
10076 end if;
10078 Next_Entity (Prim);
10079 end loop;
10080 end;
10081 end if;
10083 else
10084 Error_Msg_Node_2 := T;
10085 Error_Msg_N
10086 ("abstract subprogram& not allowed for type&", Subp);
10088 -- Also post unconditional warning on the type (unconditional
10089 -- so that if there are more than one of these cases, we get
10090 -- them all, and not just the first one).
10092 Error_Msg_Node_2 := Subp;
10093 Error_Msg_N ("nonabstract type& has abstract subprogram&!", T);
10094 end if;
10096 -- A subprogram subject to pragma Extensions_Visible with value
10097 -- "True" cannot override a subprogram subject to the same pragma
10098 -- with value "False" (SPARK RM 6.1.7(5)).
10100 elsif Extensions_Visible_Status (Subp) = Extensions_Visible_True
10101 and then Present (Overridden_Operation (Subp))
10102 and then Extensions_Visible_Status (Overridden_Operation (Subp)) =
10103 Extensions_Visible_False
10104 then
10105 Error_Msg_Sloc := Sloc (Overridden_Operation (Subp));
10106 Error_Msg_N
10107 ("subprogram & with Extensions_Visible True cannot override "
10108 & "subprogram # with Extensions_Visible False", Subp);
10109 end if;
10111 -- Ada 2012 (AI05-0030): Perform checks related to pragma Implemented
10113 -- Subp is an expander-generated procedure which maps an interface
10114 -- alias to a protected wrapper. The interface alias is flagged by
10115 -- pragma Implemented. Ensure that Subp is a procedure when the
10116 -- implementation kind is By_Protected_Procedure or an entry when
10117 -- By_Entry.
10119 if Ada_Version >= Ada_2012
10120 and then Is_Hidden (Subp)
10121 and then Present (Interface_Alias (Subp))
10122 and then Has_Rep_Pragma (Interface_Alias (Subp), Name_Implemented)
10123 then
10124 Check_Pragma_Implemented (Subp);
10125 end if;
10127 -- Subp is an interface primitive which overrides another interface
10128 -- primitive marked with pragma Implemented.
10130 if Ada_Version >= Ada_2012
10131 and then Present (Overridden_Operation (Subp))
10132 and then Has_Rep_Pragma
10133 (Overridden_Operation (Subp), Name_Implemented)
10134 then
10135 -- If the overriding routine is also marked by Implemented, check
10136 -- that the two implementation kinds are conforming.
10138 if Has_Rep_Pragma (Subp, Name_Implemented) then
10139 Check_Pragma_Implemented
10140 (Subp => Subp,
10141 Iface_Subp => Overridden_Operation (Subp));
10143 -- Otherwise the overriding routine inherits the implementation
10144 -- kind from the overridden subprogram.
10146 else
10147 Inherit_Pragma_Implemented
10148 (Subp => Subp,
10149 Iface_Subp => Overridden_Operation (Subp));
10150 end if;
10151 end if;
10153 -- If the operation is a wrapper for a synchronized primitive, it
10154 -- may be called indirectly through a dispatching select. We assume
10155 -- that it will be referenced elsewhere indirectly, and suppress
10156 -- warnings about an unused entity.
10158 if Is_Primitive_Wrapper (Subp)
10159 and then Present (Wrapped_Entity (Subp))
10160 then
10161 Set_Referenced (Wrapped_Entity (Subp));
10162 end if;
10164 Next_Elmt (Elmt);
10165 end loop;
10166 end Check_Abstract_Overriding;
10168 ------------------------------------------------
10169 -- Check_Access_Discriminant_Requires_Limited --
10170 ------------------------------------------------
10172 procedure Check_Access_Discriminant_Requires_Limited
10173 (D : Node_Id;
10174 Loc : Node_Id)
10176 begin
10177 -- A discriminant_specification for an access discriminant shall appear
10178 -- only in the declaration for a task or protected type, or for a type
10179 -- with the reserved word 'limited' in its definition or in one of its
10180 -- ancestors (RM 3.7(10)).
10182 -- AI-0063: The proper condition is that type must be immutably limited,
10183 -- or else be a partial view.
10185 if Nkind (Discriminant_Type (D)) = N_Access_Definition then
10186 if Is_Limited_View (Current_Scope)
10187 or else
10188 (Nkind (Parent (Current_Scope)) = N_Private_Type_Declaration
10189 and then Limited_Present (Parent (Current_Scope)))
10190 then
10191 null;
10193 else
10194 Error_Msg_N
10195 ("access discriminants allowed only for limited types", Loc);
10196 end if;
10197 end if;
10198 end Check_Access_Discriminant_Requires_Limited;
10200 -----------------------------------
10201 -- Check_Aliased_Component_Types --
10202 -----------------------------------
10204 procedure Check_Aliased_Component_Types (T : Entity_Id) is
10205 C : Entity_Id;
10207 begin
10208 -- ??? Also need to check components of record extensions, but not
10209 -- components of protected types (which are always limited).
10211 -- Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
10212 -- types to be unconstrained. This is safe because it is illegal to
10213 -- create access subtypes to such types with explicit discriminant
10214 -- constraints.
10216 if not Is_Limited_Type (T) then
10217 if Ekind (T) = E_Record_Type then
10218 C := First_Component (T);
10219 while Present (C) loop
10220 if Is_Aliased (C)
10221 and then Has_Discriminants (Etype (C))
10222 and then not Is_Constrained (Etype (C))
10223 and then not In_Instance_Body
10224 and then Ada_Version < Ada_2005
10225 then
10226 Error_Msg_N
10227 ("aliased component must be constrained (RM 3.6(11))",
10229 end if;
10231 Next_Component (C);
10232 end loop;
10234 elsif Ekind (T) = E_Array_Type then
10235 if Has_Aliased_Components (T)
10236 and then Has_Discriminants (Component_Type (T))
10237 and then not Is_Constrained (Component_Type (T))
10238 and then not In_Instance_Body
10239 and then Ada_Version < Ada_2005
10240 then
10241 Error_Msg_N
10242 ("aliased component type must be constrained (RM 3.6(11))",
10244 end if;
10245 end if;
10246 end if;
10247 end Check_Aliased_Component_Types;
10249 ---------------------------------------
10250 -- Check_Anonymous_Access_Components --
10251 ---------------------------------------
10253 procedure Check_Anonymous_Access_Components
10254 (Typ_Decl : Node_Id;
10255 Typ : Entity_Id;
10256 Prev : Entity_Id;
10257 Comp_List : Node_Id)
10259 Loc : constant Source_Ptr := Sloc (Typ_Decl);
10260 Anon_Access : Entity_Id;
10261 Acc_Def : Node_Id;
10262 Comp : Node_Id;
10263 Comp_Def : Node_Id;
10264 Decl : Node_Id;
10265 Type_Def : Node_Id;
10267 procedure Build_Incomplete_Type_Declaration;
10268 -- If the record type contains components that include an access to the
10269 -- current record, then create an incomplete type declaration for the
10270 -- record, to be used as the designated type of the anonymous access.
10271 -- This is done only once, and only if there is no previous partial
10272 -- view of the type.
10274 function Designates_T (Subt : Node_Id) return Boolean;
10275 -- Check whether a node designates the enclosing record type, or 'Class
10276 -- of that type
10278 function Mentions_T (Acc_Def : Node_Id) return Boolean;
10279 -- Check whether an access definition includes a reference to
10280 -- the enclosing record type. The reference can be a subtype mark
10281 -- in the access definition itself, a 'Class attribute reference, or
10282 -- recursively a reference appearing in a parameter specification
10283 -- or result definition of an access_to_subprogram definition.
10285 --------------------------------------
10286 -- Build_Incomplete_Type_Declaration --
10287 --------------------------------------
10289 procedure Build_Incomplete_Type_Declaration is
10290 Decl : Node_Id;
10291 Inc_T : Entity_Id;
10292 H : Entity_Id;
10294 -- Is_Tagged indicates whether the type is tagged. It is tagged if
10295 -- it's "is new ... with record" or else "is tagged record ...".
10297 Is_Tagged : constant Boolean :=
10298 (Nkind (Type_Definition (Typ_Decl)) = N_Derived_Type_Definition
10299 and then
10300 Present (Record_Extension_Part (Type_Definition (Typ_Decl))))
10301 or else
10302 (Nkind (Type_Definition (Typ_Decl)) = N_Record_Definition
10303 and then Tagged_Present (Type_Definition (Typ_Decl)));
10305 begin
10306 -- If there is a previous partial view, no need to create a new one
10307 -- If the partial view, given by Prev, is incomplete, If Prev is
10308 -- a private declaration, full declaration is flagged accordingly.
10310 if Prev /= Typ then
10311 if Is_Tagged then
10312 Make_Class_Wide_Type (Prev);
10313 Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
10314 Set_Etype (Class_Wide_Type (Typ), Typ);
10315 end if;
10317 return;
10319 elsif Has_Private_Declaration (Typ) then
10321 -- If we refer to T'Class inside T, and T is the completion of a
10322 -- private type, then make sure the class-wide type exists.
10324 if Is_Tagged then
10325 Make_Class_Wide_Type (Typ);
10326 end if;
10328 return;
10330 -- If there was a previous anonymous access type, the incomplete
10331 -- type declaration will have been created already.
10333 elsif Present (Current_Entity (Typ))
10334 and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
10335 and then Full_View (Current_Entity (Typ)) = Typ
10336 then
10337 if Is_Tagged
10338 and then Comes_From_Source (Current_Entity (Typ))
10339 and then not Is_Tagged_Type (Current_Entity (Typ))
10340 then
10341 Make_Class_Wide_Type (Typ);
10342 Error_Msg_N
10343 ("incomplete view of tagged type should be declared tagged??",
10344 Parent (Current_Entity (Typ)));
10345 end if;
10346 return;
10348 else
10349 Inc_T := Make_Defining_Identifier (Loc, Chars (Typ));
10350 Decl := Make_Incomplete_Type_Declaration (Loc, Inc_T);
10352 -- Type has already been inserted into the current scope. Remove
10353 -- it, and add incomplete declaration for type, so that subsequent
10354 -- anonymous access types can use it. The entity is unchained from
10355 -- the homonym list and from immediate visibility. After analysis,
10356 -- the entity in the incomplete declaration becomes immediately
10357 -- visible in the record declaration that follows.
10359 H := Current_Entity (Typ);
10361 if H = Typ then
10362 Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
10363 else
10364 while Present (H)
10365 and then Homonym (H) /= Typ
10366 loop
10367 H := Homonym (Typ);
10368 end loop;
10370 Set_Homonym (H, Homonym (Typ));
10371 end if;
10373 Insert_Before (Typ_Decl, Decl);
10374 Analyze (Decl);
10375 Set_Full_View (Inc_T, Typ);
10377 if Is_Tagged then
10379 -- Create a common class-wide type for both views, and set the
10380 -- Etype of the class-wide type to the full view.
10382 Make_Class_Wide_Type (Inc_T);
10383 Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
10384 Set_Etype (Class_Wide_Type (Typ), Typ);
10385 end if;
10386 end if;
10387 end Build_Incomplete_Type_Declaration;
10389 ------------------
10390 -- Designates_T --
10391 ------------------
10393 function Designates_T (Subt : Node_Id) return Boolean is
10394 Type_Id : constant Name_Id := Chars (Typ);
10396 function Names_T (Nam : Node_Id) return Boolean;
10397 -- The record type has not been introduced in the current scope
10398 -- yet, so we must examine the name of the type itself, either
10399 -- an identifier T, or an expanded name of the form P.T, where
10400 -- P denotes the current scope.
10402 -------------
10403 -- Names_T --
10404 -------------
10406 function Names_T (Nam : Node_Id) return Boolean is
10407 begin
10408 if Nkind (Nam) = N_Identifier then
10409 return Chars (Nam) = Type_Id;
10411 elsif Nkind (Nam) = N_Selected_Component then
10412 if Chars (Selector_Name (Nam)) = Type_Id then
10413 if Nkind (Prefix (Nam)) = N_Identifier then
10414 return Chars (Prefix (Nam)) = Chars (Current_Scope);
10416 elsif Nkind (Prefix (Nam)) = N_Selected_Component then
10417 return Chars (Selector_Name (Prefix (Nam))) =
10418 Chars (Current_Scope);
10419 else
10420 return False;
10421 end if;
10423 else
10424 return False;
10425 end if;
10427 else
10428 return False;
10429 end if;
10430 end Names_T;
10432 -- Start of processing for Designates_T
10434 begin
10435 if Nkind (Subt) = N_Identifier then
10436 return Chars (Subt) = Type_Id;
10438 -- Reference can be through an expanded name which has not been
10439 -- analyzed yet, and which designates enclosing scopes.
10441 elsif Nkind (Subt) = N_Selected_Component then
10442 if Names_T (Subt) then
10443 return True;
10445 -- Otherwise it must denote an entity that is already visible.
10446 -- The access definition may name a subtype of the enclosing
10447 -- type, if there is a previous incomplete declaration for it.
10449 else
10450 Find_Selected_Component (Subt);
10451 return
10452 Is_Entity_Name (Subt)
10453 and then Scope (Entity (Subt)) = Current_Scope
10454 and then
10455 (Chars (Base_Type (Entity (Subt))) = Type_Id
10456 or else
10457 (Is_Class_Wide_Type (Entity (Subt))
10458 and then
10459 Chars (Etype (Base_Type (Entity (Subt)))) =
10460 Type_Id));
10461 end if;
10463 -- A reference to the current type may appear as the prefix of
10464 -- a 'Class attribute.
10466 elsif Nkind (Subt) = N_Attribute_Reference
10467 and then Attribute_Name (Subt) = Name_Class
10468 then
10469 return Names_T (Prefix (Subt));
10471 else
10472 return False;
10473 end if;
10474 end Designates_T;
10476 ----------------
10477 -- Mentions_T --
10478 ----------------
10480 function Mentions_T (Acc_Def : Node_Id) return Boolean is
10481 Param_Spec : Node_Id;
10483 Acc_Subprg : constant Node_Id :=
10484 Access_To_Subprogram_Definition (Acc_Def);
10486 begin
10487 if No (Acc_Subprg) then
10488 return Designates_T (Subtype_Mark (Acc_Def));
10489 end if;
10491 -- Component is an access_to_subprogram: examine its formals,
10492 -- and result definition in the case of an access_to_function.
10494 Param_Spec := First (Parameter_Specifications (Acc_Subprg));
10495 while Present (Param_Spec) loop
10496 if Nkind (Parameter_Type (Param_Spec)) = N_Access_Definition
10497 and then Mentions_T (Parameter_Type (Param_Spec))
10498 then
10499 return True;
10501 elsif Designates_T (Parameter_Type (Param_Spec)) then
10502 return True;
10503 end if;
10505 Next (Param_Spec);
10506 end loop;
10508 if Nkind (Acc_Subprg) = N_Access_Function_Definition then
10509 if Nkind (Result_Definition (Acc_Subprg)) =
10510 N_Access_Definition
10511 then
10512 return Mentions_T (Result_Definition (Acc_Subprg));
10513 else
10514 return Designates_T (Result_Definition (Acc_Subprg));
10515 end if;
10516 end if;
10518 return False;
10519 end Mentions_T;
10521 -- Start of processing for Check_Anonymous_Access_Components
10523 begin
10524 if No (Comp_List) then
10525 return;
10526 end if;
10528 Comp := First (Component_Items (Comp_List));
10529 while Present (Comp) loop
10530 if Nkind (Comp) = N_Component_Declaration
10531 and then Present
10532 (Access_Definition (Component_Definition (Comp)))
10533 and then
10534 Mentions_T (Access_Definition (Component_Definition (Comp)))
10535 then
10536 Comp_Def := Component_Definition (Comp);
10537 Acc_Def :=
10538 Access_To_Subprogram_Definition (Access_Definition (Comp_Def));
10540 Build_Incomplete_Type_Declaration;
10541 Anon_Access := Make_Temporary (Loc, 'S');
10543 -- Create a declaration for the anonymous access type: either
10544 -- an access_to_object or an access_to_subprogram.
10546 if Present (Acc_Def) then
10547 if Nkind (Acc_Def) = N_Access_Function_Definition then
10548 Type_Def :=
10549 Make_Access_Function_Definition (Loc,
10550 Parameter_Specifications =>
10551 Parameter_Specifications (Acc_Def),
10552 Result_Definition => Result_Definition (Acc_Def));
10553 else
10554 Type_Def :=
10555 Make_Access_Procedure_Definition (Loc,
10556 Parameter_Specifications =>
10557 Parameter_Specifications (Acc_Def));
10558 end if;
10560 else
10561 Type_Def :=
10562 Make_Access_To_Object_Definition (Loc,
10563 Subtype_Indication =>
10564 Relocate_Node
10565 (Subtype_Mark (Access_Definition (Comp_Def))));
10567 Set_Constant_Present
10568 (Type_Def, Constant_Present (Access_Definition (Comp_Def)));
10569 Set_All_Present
10570 (Type_Def, All_Present (Access_Definition (Comp_Def)));
10571 end if;
10573 Set_Null_Exclusion_Present
10574 (Type_Def,
10575 Null_Exclusion_Present (Access_Definition (Comp_Def)));
10577 Decl :=
10578 Make_Full_Type_Declaration (Loc,
10579 Defining_Identifier => Anon_Access,
10580 Type_Definition => Type_Def);
10582 Insert_Before (Typ_Decl, Decl);
10583 Analyze (Decl);
10585 -- If an access to subprogram, create the extra formals
10587 if Present (Acc_Def) then
10588 Create_Extra_Formals (Designated_Type (Anon_Access));
10590 -- If an access to object, preserve entity of designated type,
10591 -- for ASIS use, before rewriting the component definition.
10593 else
10594 declare
10595 Desig : Entity_Id;
10597 begin
10598 Desig := Entity (Subtype_Indication (Type_Def));
10600 -- If the access definition is to the current record,
10601 -- the visible entity at this point is an incomplete
10602 -- type. Retrieve the full view to simplify ASIS queries
10604 if Ekind (Desig) = E_Incomplete_Type then
10605 Desig := Full_View (Desig);
10606 end if;
10608 Set_Entity
10609 (Subtype_Mark (Access_Definition (Comp_Def)), Desig);
10610 end;
10611 end if;
10613 Rewrite (Comp_Def,
10614 Make_Component_Definition (Loc,
10615 Subtype_Indication =>
10616 New_Occurrence_Of (Anon_Access, Loc)));
10618 if Ekind (Designated_Type (Anon_Access)) = E_Subprogram_Type then
10619 Set_Ekind (Anon_Access, E_Anonymous_Access_Subprogram_Type);
10620 else
10621 Set_Ekind (Anon_Access, E_Anonymous_Access_Type);
10622 end if;
10624 Set_Is_Local_Anonymous_Access (Anon_Access);
10625 end if;
10627 Next (Comp);
10628 end loop;
10630 if Present (Variant_Part (Comp_List)) then
10631 declare
10632 V : Node_Id;
10633 begin
10634 V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
10635 while Present (V) loop
10636 Check_Anonymous_Access_Components
10637 (Typ_Decl, Typ, Prev, Component_List (V));
10638 Next_Non_Pragma (V);
10639 end loop;
10640 end;
10641 end if;
10642 end Check_Anonymous_Access_Components;
10644 ----------------------
10645 -- Check_Completion --
10646 ----------------------
10648 procedure Check_Completion (Body_Id : Node_Id := Empty) is
10649 E : Entity_Id;
10651 procedure Post_Error;
10652 -- Post error message for lack of completion for entity E
10654 ----------------
10655 -- Post_Error --
10656 ----------------
10658 procedure Post_Error is
10660 procedure Missing_Body;
10661 -- Output missing body message
10663 ------------------
10664 -- Missing_Body --
10665 ------------------
10667 procedure Missing_Body is
10668 begin
10669 -- Spec is in same unit, so we can post on spec
10671 if In_Same_Source_Unit (Body_Id, E) then
10672 Error_Msg_N ("missing body for &", E);
10674 -- Spec is in a separate unit, so we have to post on the body
10676 else
10677 Error_Msg_NE ("missing body for & declared#!", Body_Id, E);
10678 end if;
10679 end Missing_Body;
10681 -- Start of processing for Post_Error
10683 begin
10684 if not Comes_From_Source (E) then
10686 if Ekind_In (E, E_Task_Type, E_Protected_Type) then
10688 -- It may be an anonymous protected type created for a
10689 -- single variable. Post error on variable, if present.
10691 declare
10692 Var : Entity_Id;
10694 begin
10695 Var := First_Entity (Current_Scope);
10696 while Present (Var) loop
10697 exit when Etype (Var) = E
10698 and then Comes_From_Source (Var);
10700 Next_Entity (Var);
10701 end loop;
10703 if Present (Var) then
10704 E := Var;
10705 end if;
10706 end;
10707 end if;
10708 end if;
10710 -- If a generated entity has no completion, then either previous
10711 -- semantic errors have disabled the expansion phase, or else we had
10712 -- missing subunits, or else we are compiling without expansion,
10713 -- or else something is very wrong.
10715 if not Comes_From_Source (E) then
10716 pragma Assert
10717 (Serious_Errors_Detected > 0
10718 or else Configurable_Run_Time_Violations > 0
10719 or else Subunits_Missing
10720 or else not Expander_Active);
10721 return;
10723 -- Here for source entity
10725 else
10726 -- Here if no body to post the error message, so we post the error
10727 -- on the declaration that has no completion. This is not really
10728 -- the right place to post it, think about this later ???
10730 if No (Body_Id) then
10731 if Is_Type (E) then
10732 Error_Msg_NE
10733 ("missing full declaration for }", Parent (E), E);
10734 else
10735 Error_Msg_NE ("missing body for &", Parent (E), E);
10736 end if;
10738 -- Package body has no completion for a declaration that appears
10739 -- in the corresponding spec. Post error on the body, with a
10740 -- reference to the non-completed declaration.
10742 else
10743 Error_Msg_Sloc := Sloc (E);
10745 if Is_Type (E) then
10746 Error_Msg_NE ("missing full declaration for }!", Body_Id, E);
10748 elsif Is_Overloadable (E)
10749 and then Current_Entity_In_Scope (E) /= E
10750 then
10751 -- It may be that the completion is mistyped and appears as
10752 -- a distinct overloading of the entity.
10754 declare
10755 Candidate : constant Entity_Id :=
10756 Current_Entity_In_Scope (E);
10757 Decl : constant Node_Id :=
10758 Unit_Declaration_Node (Candidate);
10760 begin
10761 if Is_Overloadable (Candidate)
10762 and then Ekind (Candidate) = Ekind (E)
10763 and then Nkind (Decl) = N_Subprogram_Body
10764 and then Acts_As_Spec (Decl)
10765 then
10766 Check_Type_Conformant (Candidate, E);
10768 else
10769 Missing_Body;
10770 end if;
10771 end;
10773 else
10774 Missing_Body;
10775 end if;
10776 end if;
10777 end if;
10778 end Post_Error;
10780 -- Local variables
10782 Pack_Id : constant Entity_Id := Current_Scope;
10784 -- Start of processing for Check_Completion
10786 begin
10787 E := First_Entity (Pack_Id);
10788 while Present (E) loop
10789 if Is_Intrinsic_Subprogram (E) then
10790 null;
10792 -- A Ghost entity declared in a non-Ghost package does not force the
10793 -- need for a body (SPARK RM 6.9(11)).
10795 elsif not Is_Ghost_Entity (Pack_Id) and then Is_Ghost_Entity (E) then
10796 null;
10798 -- The following situation requires special handling: a child unit
10799 -- that appears in the context clause of the body of its parent:
10801 -- procedure Parent.Child (...);
10803 -- with Parent.Child;
10804 -- package body Parent is
10806 -- Here Parent.Child appears as a local entity, but should not be
10807 -- flagged as requiring completion, because it is a compilation
10808 -- unit.
10810 -- Ignore missing completion for a subprogram that does not come from
10811 -- source (including the _Call primitive operation of RAS types,
10812 -- which has to have the flag Comes_From_Source for other purposes):
10813 -- we assume that the expander will provide the missing completion.
10814 -- In case of previous errors, other expansion actions that provide
10815 -- bodies for null procedures with not be invoked, so inhibit message
10816 -- in those cases.
10818 -- Note that E_Operator is not in the list that follows, because
10819 -- this kind is reserved for predefined operators, that are
10820 -- intrinsic and do not need completion.
10822 elsif Ekind_In (E, E_Function,
10823 E_Procedure,
10824 E_Generic_Function,
10825 E_Generic_Procedure)
10826 then
10827 if Has_Completion (E) then
10828 null;
10830 elsif Is_Subprogram (E) and then Is_Abstract_Subprogram (E) then
10831 null;
10833 elsif Is_Subprogram (E)
10834 and then (not Comes_From_Source (E)
10835 or else Chars (E) = Name_uCall)
10836 then
10837 null;
10839 elsif
10840 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
10841 then
10842 null;
10844 elsif Nkind (Parent (E)) = N_Procedure_Specification
10845 and then Null_Present (Parent (E))
10846 and then Serious_Errors_Detected > 0
10847 then
10848 null;
10850 else
10851 Post_Error;
10852 end if;
10854 elsif Is_Entry (E) then
10855 if not Has_Completion (E) and then
10856 (Ekind (Scope (E)) = E_Protected_Object
10857 or else Ekind (Scope (E)) = E_Protected_Type)
10858 then
10859 Post_Error;
10860 end if;
10862 elsif Is_Package_Or_Generic_Package (E) then
10863 if Unit_Requires_Body (E) then
10864 if not Has_Completion (E)
10865 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
10866 N_Compilation_Unit
10867 then
10868 Post_Error;
10869 end if;
10871 elsif not Is_Child_Unit (E) then
10872 May_Need_Implicit_Body (E);
10873 end if;
10875 -- A formal incomplete type (Ada 2012) does not require a completion;
10876 -- other incomplete type declarations do.
10878 elsif Ekind (E) = E_Incomplete_Type
10879 and then No (Underlying_Type (E))
10880 and then not Is_Generic_Type (E)
10881 then
10882 Post_Error;
10884 elsif Ekind_In (E, E_Task_Type, E_Protected_Type)
10885 and then not Has_Completion (E)
10886 then
10887 Post_Error;
10889 -- A single task declared in the current scope is a constant, verify
10890 -- that the body of its anonymous type is in the same scope. If the
10891 -- task is defined elsewhere, this may be a renaming declaration for
10892 -- which no completion is needed.
10894 elsif Ekind (E) = E_Constant
10895 and then Ekind (Etype (E)) = E_Task_Type
10896 and then not Has_Completion (Etype (E))
10897 and then Scope (Etype (E)) = Current_Scope
10898 then
10899 Post_Error;
10901 elsif Ekind (E) = E_Protected_Object
10902 and then not Has_Completion (Etype (E))
10903 then
10904 Post_Error;
10906 elsif Ekind (E) = E_Record_Type then
10907 if Is_Tagged_Type (E) then
10908 Check_Abstract_Overriding (E);
10909 Check_Conventions (E);
10910 end if;
10912 Check_Aliased_Component_Types (E);
10914 elsif Ekind (E) = E_Array_Type then
10915 Check_Aliased_Component_Types (E);
10917 end if;
10919 Next_Entity (E);
10920 end loop;
10921 end Check_Completion;
10923 ------------------------------------
10924 -- Check_CPP_Type_Has_No_Defaults --
10925 ------------------------------------
10927 procedure Check_CPP_Type_Has_No_Defaults (T : Entity_Id) is
10928 Tdef : constant Node_Id := Type_Definition (Declaration_Node (T));
10929 Clist : Node_Id;
10930 Comp : Node_Id;
10932 begin
10933 -- Obtain the component list
10935 if Nkind (Tdef) = N_Record_Definition then
10936 Clist := Component_List (Tdef);
10937 else pragma Assert (Nkind (Tdef) = N_Derived_Type_Definition);
10938 Clist := Component_List (Record_Extension_Part (Tdef));
10939 end if;
10941 -- Check all components to ensure no default expressions
10943 if Present (Clist) then
10944 Comp := First (Component_Items (Clist));
10945 while Present (Comp) loop
10946 if Present (Expression (Comp)) then
10947 Error_Msg_N
10948 ("component of imported 'C'P'P type cannot have "
10949 & "default expression", Expression (Comp));
10950 end if;
10952 Next (Comp);
10953 end loop;
10954 end if;
10955 end Check_CPP_Type_Has_No_Defaults;
10957 ----------------------------
10958 -- Check_Delta_Expression --
10959 ----------------------------
10961 procedure Check_Delta_Expression (E : Node_Id) is
10962 begin
10963 if not (Is_Real_Type (Etype (E))) then
10964 Wrong_Type (E, Any_Real);
10966 elsif not Is_OK_Static_Expression (E) then
10967 Flag_Non_Static_Expr
10968 ("non-static expression used for delta value!", E);
10970 elsif not UR_Is_Positive (Expr_Value_R (E)) then
10971 Error_Msg_N ("delta expression must be positive", E);
10973 else
10974 return;
10975 end if;
10977 -- If any of above errors occurred, then replace the incorrect
10978 -- expression by the real 0.1, which should prevent further errors.
10980 Rewrite (E,
10981 Make_Real_Literal (Sloc (E), Ureal_Tenth));
10982 Analyze_And_Resolve (E, Standard_Float);
10983 end Check_Delta_Expression;
10985 -----------------------------
10986 -- Check_Digits_Expression --
10987 -----------------------------
10989 procedure Check_Digits_Expression (E : Node_Id) is
10990 begin
10991 if not (Is_Integer_Type (Etype (E))) then
10992 Wrong_Type (E, Any_Integer);
10994 elsif not Is_OK_Static_Expression (E) then
10995 Flag_Non_Static_Expr
10996 ("non-static expression used for digits value!", E);
10998 elsif Expr_Value (E) <= 0 then
10999 Error_Msg_N ("digits value must be greater than zero", E);
11001 else
11002 return;
11003 end if;
11005 -- If any of above errors occurred, then replace the incorrect
11006 -- expression by the integer 1, which should prevent further errors.
11008 Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
11009 Analyze_And_Resolve (E, Standard_Integer);
11011 end Check_Digits_Expression;
11013 --------------------------
11014 -- Check_Initialization --
11015 --------------------------
11017 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
11018 begin
11019 -- Special processing for limited types
11021 if Is_Limited_Type (T)
11022 and then not In_Instance
11023 and then not In_Inlined_Body
11024 then
11025 if not OK_For_Limited_Init (T, Exp) then
11027 -- In GNAT mode, this is just a warning, to allow it to be evilly
11028 -- turned off. Otherwise it is a real error.
11030 if GNAT_Mode then
11031 Error_Msg_N
11032 ("??cannot initialize entities of limited type!", Exp);
11034 elsif Ada_Version < Ada_2005 then
11036 -- The side effect removal machinery may generate illegal Ada
11037 -- code to avoid the usage of access types and 'reference in
11038 -- SPARK mode. Since this is legal code with respect to theorem
11039 -- proving, do not emit the error.
11041 if GNATprove_Mode
11042 and then Nkind (Exp) = N_Function_Call
11043 and then Nkind (Parent (Exp)) = N_Object_Declaration
11044 and then not Comes_From_Source
11045 (Defining_Identifier (Parent (Exp)))
11046 then
11047 null;
11049 else
11050 Error_Msg_N
11051 ("cannot initialize entities of limited type", Exp);
11052 Explain_Limited_Type (T, Exp);
11053 end if;
11055 else
11056 -- Specialize error message according to kind of illegal
11057 -- initial expression.
11059 if Nkind (Exp) = N_Type_Conversion
11060 and then Nkind (Expression (Exp)) = N_Function_Call
11061 then
11062 Error_Msg_N
11063 ("illegal context for call"
11064 & " to function with limited result", Exp);
11066 else
11067 Error_Msg_N
11068 ("initialization of limited object requires aggregate "
11069 & "or function call", Exp);
11070 end if;
11071 end if;
11072 end if;
11073 end if;
11075 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag gets
11076 -- set unless we can be sure that no range check is required.
11078 if (GNATprove_Mode or not Expander_Active)
11079 and then Is_Scalar_Type (T)
11080 and then not Is_In_Range (Exp, T, Assume_Valid => True)
11081 then
11082 Set_Do_Range_Check (Exp);
11083 end if;
11084 end Check_Initialization;
11086 ----------------------
11087 -- Check_Interfaces --
11088 ----------------------
11090 procedure Check_Interfaces (N : Node_Id; Def : Node_Id) is
11091 Parent_Type : constant Entity_Id := Etype (Defining_Identifier (N));
11093 Iface : Node_Id;
11094 Iface_Def : Node_Id;
11095 Iface_Typ : Entity_Id;
11096 Parent_Node : Node_Id;
11098 Is_Task : Boolean := False;
11099 -- Set True if parent type or any progenitor is a task interface
11101 Is_Protected : Boolean := False;
11102 -- Set True if parent type or any progenitor is a protected interface
11104 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
11105 -- Check that a progenitor is compatible with declaration. If an error
11106 -- message is output, it is posted on Error_Node.
11108 ------------------
11109 -- Check_Ifaces --
11110 ------------------
11112 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
11113 Iface_Id : constant Entity_Id :=
11114 Defining_Identifier (Parent (Iface_Def));
11115 Type_Def : Node_Id;
11117 begin
11118 if Nkind (N) = N_Private_Extension_Declaration then
11119 Type_Def := N;
11120 else
11121 Type_Def := Type_Definition (N);
11122 end if;
11124 if Is_Task_Interface (Iface_Id) then
11125 Is_Task := True;
11127 elsif Is_Protected_Interface (Iface_Id) then
11128 Is_Protected := True;
11129 end if;
11131 if Is_Synchronized_Interface (Iface_Id) then
11133 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
11134 -- extension derived from a synchronized interface must explicitly
11135 -- be declared synchronized, because the full view will be a
11136 -- synchronized type.
11138 if Nkind (N) = N_Private_Extension_Declaration then
11139 if not Synchronized_Present (N) then
11140 Error_Msg_NE
11141 ("private extension of& must be explicitly synchronized",
11142 N, Iface_Id);
11143 end if;
11145 -- However, by 3.9.4(16/2), a full type that is a record extension
11146 -- is never allowed to derive from a synchronized interface (note
11147 -- that interfaces must be excluded from this check, because those
11148 -- are represented by derived type definitions in some cases).
11150 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition
11151 and then not Interface_Present (Type_Definition (N))
11152 then
11153 Error_Msg_N ("record extension cannot derive from synchronized "
11154 & "interface", Error_Node);
11155 end if;
11156 end if;
11158 -- Check that the characteristics of the progenitor are compatible
11159 -- with the explicit qualifier in the declaration.
11160 -- The check only applies to qualifiers that come from source.
11161 -- Limited_Present also appears in the declaration of corresponding
11162 -- records, and the check does not apply to them.
11164 if Limited_Present (Type_Def)
11165 and then not
11166 Is_Concurrent_Record_Type (Defining_Identifier (N))
11167 then
11168 if Is_Limited_Interface (Parent_Type)
11169 and then not Is_Limited_Interface (Iface_Id)
11170 then
11171 Error_Msg_NE
11172 ("progenitor & must be limited interface",
11173 Error_Node, Iface_Id);
11175 elsif
11176 (Task_Present (Iface_Def)
11177 or else Protected_Present (Iface_Def)
11178 or else Synchronized_Present (Iface_Def))
11179 and then Nkind (N) /= N_Private_Extension_Declaration
11180 and then not Error_Posted (N)
11181 then
11182 Error_Msg_NE
11183 ("progenitor & must be limited interface",
11184 Error_Node, Iface_Id);
11185 end if;
11187 -- Protected interfaces can only inherit from limited, synchronized
11188 -- or protected interfaces.
11190 elsif Nkind (N) = N_Full_Type_Declaration
11191 and then Protected_Present (Type_Def)
11192 then
11193 if Limited_Present (Iface_Def)
11194 or else Synchronized_Present (Iface_Def)
11195 or else Protected_Present (Iface_Def)
11196 then
11197 null;
11199 elsif Task_Present (Iface_Def) then
11200 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
11201 & "from task interface", Error_Node);
11203 else
11204 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
11205 & "from non-limited interface", Error_Node);
11206 end if;
11208 -- Ada 2005 (AI-345): Synchronized interfaces can only inherit from
11209 -- limited and synchronized.
11211 elsif Synchronized_Present (Type_Def) then
11212 if Limited_Present (Iface_Def)
11213 or else Synchronized_Present (Iface_Def)
11214 then
11215 null;
11217 elsif Protected_Present (Iface_Def)
11218 and then Nkind (N) /= N_Private_Extension_Declaration
11219 then
11220 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11221 & "from protected interface", Error_Node);
11223 elsif Task_Present (Iface_Def)
11224 and then Nkind (N) /= N_Private_Extension_Declaration
11225 then
11226 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11227 & "from task interface", Error_Node);
11229 elsif not Is_Limited_Interface (Iface_Id) then
11230 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11231 & "from non-limited interface", Error_Node);
11232 end if;
11234 -- Ada 2005 (AI-345): Task interfaces can only inherit from limited,
11235 -- synchronized or task interfaces.
11237 elsif Nkind (N) = N_Full_Type_Declaration
11238 and then Task_Present (Type_Def)
11239 then
11240 if Limited_Present (Iface_Def)
11241 or else Synchronized_Present (Iface_Def)
11242 or else Task_Present (Iface_Def)
11243 then
11244 null;
11246 elsif Protected_Present (Iface_Def) then
11247 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
11248 & "protected interface", Error_Node);
11250 else
11251 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
11252 & "non-limited interface", Error_Node);
11253 end if;
11254 end if;
11255 end Check_Ifaces;
11257 -- Start of processing for Check_Interfaces
11259 begin
11260 if Is_Interface (Parent_Type) then
11261 if Is_Task_Interface (Parent_Type) then
11262 Is_Task := True;
11264 elsif Is_Protected_Interface (Parent_Type) then
11265 Is_Protected := True;
11266 end if;
11267 end if;
11269 if Nkind (N) = N_Private_Extension_Declaration then
11271 -- Check that progenitors are compatible with declaration
11273 Iface := First (Interface_List (Def));
11274 while Present (Iface) loop
11275 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
11277 Parent_Node := Parent (Base_Type (Iface_Typ));
11278 Iface_Def := Type_Definition (Parent_Node);
11280 if not Is_Interface (Iface_Typ) then
11281 Diagnose_Interface (Iface, Iface_Typ);
11282 else
11283 Check_Ifaces (Iface_Def, Iface);
11284 end if;
11286 Next (Iface);
11287 end loop;
11289 if Is_Task and Is_Protected then
11290 Error_Msg_N
11291 ("type cannot derive from task and protected interface", N);
11292 end if;
11294 return;
11295 end if;
11297 -- Full type declaration of derived type.
11298 -- Check compatibility with parent if it is interface type
11300 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
11301 and then Is_Interface (Parent_Type)
11302 then
11303 Parent_Node := Parent (Parent_Type);
11305 -- More detailed checks for interface varieties
11307 Check_Ifaces
11308 (Iface_Def => Type_Definition (Parent_Node),
11309 Error_Node => Subtype_Indication (Type_Definition (N)));
11310 end if;
11312 Iface := First (Interface_List (Def));
11313 while Present (Iface) loop
11314 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
11316 Parent_Node := Parent (Base_Type (Iface_Typ));
11317 Iface_Def := Type_Definition (Parent_Node);
11319 if not Is_Interface (Iface_Typ) then
11320 Diagnose_Interface (Iface, Iface_Typ);
11322 else
11323 -- "The declaration of a specific descendant of an interface
11324 -- type freezes the interface type" RM 13.14
11326 Freeze_Before (N, Iface_Typ);
11327 Check_Ifaces (Iface_Def, Error_Node => Iface);
11328 end if;
11330 Next (Iface);
11331 end loop;
11333 if Is_Task and Is_Protected then
11334 Error_Msg_N
11335 ("type cannot derive from task and protected interface", N);
11336 end if;
11337 end Check_Interfaces;
11339 ------------------------------------
11340 -- Check_Or_Process_Discriminants --
11341 ------------------------------------
11343 -- If an incomplete or private type declaration was already given for the
11344 -- type, the discriminants may have already been processed if they were
11345 -- present on the incomplete declaration. In this case a full conformance
11346 -- check has been performed in Find_Type_Name, and we then recheck here
11347 -- some properties that can't be checked on the partial view alone.
11348 -- Otherwise we call Process_Discriminants.
11350 procedure Check_Or_Process_Discriminants
11351 (N : Node_Id;
11352 T : Entity_Id;
11353 Prev : Entity_Id := Empty)
11355 begin
11356 if Has_Discriminants (T) then
11358 -- Discriminants are already set on T if they were already present
11359 -- on the partial view. Make them visible to component declarations.
11361 declare
11362 D : Entity_Id;
11363 -- Discriminant on T (full view) referencing expr on partial view
11365 Prev_D : Entity_Id;
11366 -- Entity of corresponding discriminant on partial view
11368 New_D : Node_Id;
11369 -- Discriminant specification for full view, expression is
11370 -- the syntactic copy on full view (which has been checked for
11371 -- conformance with partial view), only used here to post error
11372 -- message.
11374 begin
11375 D := First_Discriminant (T);
11376 New_D := First (Discriminant_Specifications (N));
11377 while Present (D) loop
11378 Prev_D := Current_Entity (D);
11379 Set_Current_Entity (D);
11380 Set_Is_Immediately_Visible (D);
11381 Set_Homonym (D, Prev_D);
11383 -- Handle the case where there is an untagged partial view and
11384 -- the full view is tagged: must disallow discriminants with
11385 -- defaults, unless compiling for Ada 2012, which allows a
11386 -- limited tagged type to have defaulted discriminants (see
11387 -- AI05-0214). However, suppress error here if it was already
11388 -- reported on the default expression of the partial view.
11390 if Is_Tagged_Type (T)
11391 and then Present (Expression (Parent (D)))
11392 and then (not Is_Limited_Type (Current_Scope)
11393 or else Ada_Version < Ada_2012)
11394 and then not Error_Posted (Expression (Parent (D)))
11395 then
11396 if Ada_Version >= Ada_2012 then
11397 Error_Msg_N
11398 ("discriminants of nonlimited tagged type cannot have "
11399 & "defaults",
11400 Expression (New_D));
11401 else
11402 Error_Msg_N
11403 ("discriminants of tagged type cannot have defaults",
11404 Expression (New_D));
11405 end if;
11406 end if;
11408 -- Ada 2005 (AI-230): Access discriminant allowed in
11409 -- non-limited record types.
11411 if Ada_Version < Ada_2005 then
11413 -- This restriction gets applied to the full type here. It
11414 -- has already been applied earlier to the partial view.
11416 Check_Access_Discriminant_Requires_Limited (Parent (D), N);
11417 end if;
11419 Next_Discriminant (D);
11420 Next (New_D);
11421 end loop;
11422 end;
11424 elsif Present (Discriminant_Specifications (N)) then
11425 Process_Discriminants (N, Prev);
11426 end if;
11427 end Check_Or_Process_Discriminants;
11429 ----------------------
11430 -- Check_Real_Bound --
11431 ----------------------
11433 procedure Check_Real_Bound (Bound : Node_Id) is
11434 begin
11435 if not Is_Real_Type (Etype (Bound)) then
11436 Error_Msg_N
11437 ("bound in real type definition must be of real type", Bound);
11439 elsif not Is_OK_Static_Expression (Bound) then
11440 Flag_Non_Static_Expr
11441 ("non-static expression used for real type bound!", Bound);
11443 else
11444 return;
11445 end if;
11447 Rewrite
11448 (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
11449 Analyze (Bound);
11450 Resolve (Bound, Standard_Float);
11451 end Check_Real_Bound;
11453 ------------------------------
11454 -- Complete_Private_Subtype --
11455 ------------------------------
11457 procedure Complete_Private_Subtype
11458 (Priv : Entity_Id;
11459 Full : Entity_Id;
11460 Full_Base : Entity_Id;
11461 Related_Nod : Node_Id)
11463 Save_Next_Entity : Entity_Id;
11464 Save_Homonym : Entity_Id;
11466 begin
11467 -- Set semantic attributes for (implicit) private subtype completion.
11468 -- If the full type has no discriminants, then it is a copy of the
11469 -- full view of the base. Otherwise, it is a subtype of the base with
11470 -- a possible discriminant constraint. Save and restore the original
11471 -- Next_Entity field of full to ensure that the calls to Copy_Node do
11472 -- not corrupt the entity chain.
11474 -- Note that the type of the full view is the same entity as the type
11475 -- of the partial view. In this fashion, the subtype has access to the
11476 -- correct view of the parent.
11478 Save_Next_Entity := Next_Entity (Full);
11479 Save_Homonym := Homonym (Priv);
11481 case Ekind (Full_Base) is
11482 when E_Record_Type |
11483 E_Record_Subtype |
11484 Class_Wide_Kind |
11485 Private_Kind |
11486 Task_Kind |
11487 Protected_Kind =>
11488 Copy_Node (Priv, Full);
11490 Set_Has_Discriminants
11491 (Full, Has_Discriminants (Full_Base));
11492 Set_Has_Unknown_Discriminants
11493 (Full, Has_Unknown_Discriminants (Full_Base));
11494 Set_First_Entity (Full, First_Entity (Full_Base));
11495 Set_Last_Entity (Full, Last_Entity (Full_Base));
11497 -- If the underlying base type is constrained, we know that the
11498 -- full view of the subtype is constrained as well (the converse
11499 -- is not necessarily true).
11501 if Is_Constrained (Full_Base) then
11502 Set_Is_Constrained (Full);
11503 end if;
11505 when others =>
11506 Copy_Node (Full_Base, Full);
11508 Set_Chars (Full, Chars (Priv));
11509 Conditional_Delay (Full, Priv);
11510 Set_Sloc (Full, Sloc (Priv));
11511 end case;
11513 Set_Next_Entity (Full, Save_Next_Entity);
11514 Set_Homonym (Full, Save_Homonym);
11515 Set_Associated_Node_For_Itype (Full, Related_Nod);
11517 -- Set common attributes for all subtypes: kind, convention, etc.
11519 Set_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
11520 Set_Convention (Full, Convention (Full_Base));
11522 -- The Etype of the full view is inconsistent. Gigi needs to see the
11523 -- structural full view, which is what the current scheme gives: the
11524 -- Etype of the full view is the etype of the full base. However, if the
11525 -- full base is a derived type, the full view then looks like a subtype
11526 -- of the parent, not a subtype of the full base. If instead we write:
11528 -- Set_Etype (Full, Full_Base);
11530 -- then we get inconsistencies in the front-end (confusion between
11531 -- views). Several outstanding bugs are related to this ???
11533 Set_Is_First_Subtype (Full, False);
11534 Set_Scope (Full, Scope (Priv));
11535 Set_Size_Info (Full, Full_Base);
11536 Set_RM_Size (Full, RM_Size (Full_Base));
11537 Set_Is_Itype (Full);
11539 -- A subtype of a private-type-without-discriminants, whose full-view
11540 -- has discriminants with default expressions, is not constrained.
11542 if not Has_Discriminants (Priv) then
11543 Set_Is_Constrained (Full, Is_Constrained (Full_Base));
11545 if Has_Discriminants (Full_Base) then
11546 Set_Discriminant_Constraint
11547 (Full, Discriminant_Constraint (Full_Base));
11549 -- The partial view may have been indefinite, the full view
11550 -- might not be.
11552 Set_Has_Unknown_Discriminants
11553 (Full, Has_Unknown_Discriminants (Full_Base));
11554 end if;
11555 end if;
11557 Set_First_Rep_Item (Full, First_Rep_Item (Full_Base));
11558 Set_Depends_On_Private (Full, Has_Private_Component (Full));
11560 -- Freeze the private subtype entity if its parent is delayed, and not
11561 -- already frozen. We skip this processing if the type is an anonymous
11562 -- subtype of a record component, or is the corresponding record of a
11563 -- protected type, since ???
11565 if not Is_Type (Scope (Full)) then
11566 Set_Has_Delayed_Freeze (Full,
11567 Has_Delayed_Freeze (Full_Base)
11568 and then (not Is_Frozen (Full_Base)));
11569 end if;
11571 Set_Freeze_Node (Full, Empty);
11572 Set_Is_Frozen (Full, False);
11573 Set_Full_View (Priv, Full);
11575 if Has_Discriminants (Full) then
11576 Set_Stored_Constraint_From_Discriminant_Constraint (Full);
11577 Set_Stored_Constraint (Priv, Stored_Constraint (Full));
11579 if Has_Unknown_Discriminants (Full) then
11580 Set_Discriminant_Constraint (Full, No_Elist);
11581 end if;
11582 end if;
11584 if Ekind (Full_Base) = E_Record_Type
11585 and then Has_Discriminants (Full_Base)
11586 and then Has_Discriminants (Priv) -- might not, if errors
11587 and then not Has_Unknown_Discriminants (Priv)
11588 and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
11589 then
11590 Create_Constrained_Components
11591 (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
11593 -- If the full base is itself derived from private, build a congruent
11594 -- subtype of its underlying type, for use by the back end. For a
11595 -- constrained record component, the declaration cannot be placed on
11596 -- the component list, but it must nevertheless be built an analyzed, to
11597 -- supply enough information for Gigi to compute the size of component.
11599 elsif Ekind (Full_Base) in Private_Kind
11600 and then Is_Derived_Type (Full_Base)
11601 and then Has_Discriminants (Full_Base)
11602 and then (Ekind (Current_Scope) /= E_Record_Subtype)
11603 then
11604 if not Is_Itype (Priv)
11605 and then
11606 Nkind (Subtype_Indication (Parent (Priv))) = N_Subtype_Indication
11607 then
11608 Build_Underlying_Full_View
11609 (Parent (Priv), Full, Etype (Full_Base));
11611 elsif Nkind (Related_Nod) = N_Component_Declaration then
11612 Build_Underlying_Full_View (Related_Nod, Full, Etype (Full_Base));
11613 end if;
11615 elsif Is_Record_Type (Full_Base) then
11617 -- Show Full is simply a renaming of Full_Base
11619 Set_Cloned_Subtype (Full, Full_Base);
11620 end if;
11622 -- It is unsafe to share the bounds of a scalar type, because the Itype
11623 -- is elaborated on demand, and if a bound is non-static then different
11624 -- orders of elaboration in different units will lead to different
11625 -- external symbols.
11627 if Is_Scalar_Type (Full_Base) then
11628 Set_Scalar_Range (Full,
11629 Make_Range (Sloc (Related_Nod),
11630 Low_Bound =>
11631 Duplicate_Subexpr_No_Checks (Type_Low_Bound (Full_Base)),
11632 High_Bound =>
11633 Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
11635 -- This completion inherits the bounds of the full parent, but if
11636 -- the parent is an unconstrained floating point type, so is the
11637 -- completion.
11639 if Is_Floating_Point_Type (Full_Base) then
11640 Set_Includes_Infinities
11641 (Scalar_Range (Full), Has_Infinities (Full_Base));
11642 end if;
11643 end if;
11645 -- ??? It seems that a lot of fields are missing that should be copied
11646 -- from Full_Base to Full. Here are some that are introduced in a
11647 -- non-disruptive way but a cleanup is necessary.
11649 if Is_Tagged_Type (Full_Base) then
11650 Set_Is_Tagged_Type (Full);
11651 Set_Direct_Primitive_Operations
11652 (Full, Direct_Primitive_Operations (Full_Base));
11653 Set_No_Tagged_Streams_Pragma
11654 (Full, No_Tagged_Streams_Pragma (Full_Base));
11656 -- Inherit class_wide type of full_base in case the partial view was
11657 -- not tagged. Otherwise it has already been created when the private
11658 -- subtype was analyzed.
11660 if No (Class_Wide_Type (Full)) then
11661 Set_Class_Wide_Type (Full, Class_Wide_Type (Full_Base));
11662 end if;
11664 -- If this is a subtype of a protected or task type, constrain its
11665 -- corresponding record, unless this is a subtype without constraints,
11666 -- i.e. a simple renaming as with an actual subtype in an instance.
11668 elsif Is_Concurrent_Type (Full_Base) then
11669 if Has_Discriminants (Full)
11670 and then Present (Corresponding_Record_Type (Full_Base))
11671 and then
11672 not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
11673 then
11674 Set_Corresponding_Record_Type (Full,
11675 Constrain_Corresponding_Record
11676 (Full, Corresponding_Record_Type (Full_Base), Related_Nod));
11678 else
11679 Set_Corresponding_Record_Type (Full,
11680 Corresponding_Record_Type (Full_Base));
11681 end if;
11682 end if;
11684 -- Link rep item chain, and also setting of Has_Predicates from private
11685 -- subtype to full subtype, since we will need these on the full subtype
11686 -- to create the predicate function. Note that the full subtype may
11687 -- already have rep items, inherited from the full view of the base
11688 -- type, so we must be sure not to overwrite these entries.
11690 declare
11691 Append : Boolean;
11692 Item : Node_Id;
11693 Next_Item : Node_Id;
11695 begin
11696 Item := First_Rep_Item (Full);
11698 -- If no existing rep items on full type, we can just link directly
11699 -- to the list of items on the private type, if any exist.. Same if
11700 -- the rep items are only those inherited from the base
11702 if (No (Item)
11703 or else Nkind (Item) /= N_Aspect_Specification
11704 or else Entity (Item) = Full_Base)
11705 and then Present (First_Rep_Item (Priv))
11706 then
11707 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
11709 -- Otherwise, search to the end of items currently linked to the full
11710 -- subtype and append the private items to the end. However, if Priv
11711 -- and Full already have the same list of rep items, then the append
11712 -- is not done, as that would create a circularity.
11714 elsif Item /= First_Rep_Item (Priv) then
11715 Append := True;
11716 loop
11717 Next_Item := Next_Rep_Item (Item);
11718 exit when No (Next_Item);
11719 Item := Next_Item;
11721 -- If the private view has aspect specifications, the full view
11722 -- inherits them. Since these aspects may already have been
11723 -- attached to the full view during derivation, do not append
11724 -- them if already present.
11726 if Item = First_Rep_Item (Priv) then
11727 Append := False;
11728 exit;
11729 end if;
11730 end loop;
11732 -- And link the private type items at the end of the chain
11734 if Append then
11735 Set_Next_Rep_Item (Item, First_Rep_Item (Priv));
11736 end if;
11737 end if;
11738 end;
11740 -- Make sure Has_Predicates is set on full type if it is set on the
11741 -- private type. Note that it may already be set on the full type and
11742 -- if so, we don't want to unset it.
11744 if Has_Predicates (Priv) then
11745 Set_Has_Predicates (Full);
11746 end if;
11747 end Complete_Private_Subtype;
11749 ----------------------------
11750 -- Constant_Redeclaration --
11751 ----------------------------
11753 procedure Constant_Redeclaration
11754 (Id : Entity_Id;
11755 N : Node_Id;
11756 T : out Entity_Id)
11758 Prev : constant Entity_Id := Current_Entity_In_Scope (Id);
11759 Obj_Def : constant Node_Id := Object_Definition (N);
11760 New_T : Entity_Id;
11762 procedure Check_Possible_Deferred_Completion
11763 (Prev_Id : Entity_Id;
11764 Prev_Obj_Def : Node_Id;
11765 Curr_Obj_Def : Node_Id);
11766 -- Determine whether the two object definitions describe the partial
11767 -- and the full view of a constrained deferred constant. Generate
11768 -- a subtype for the full view and verify that it statically matches
11769 -- the subtype of the partial view.
11771 procedure Check_Recursive_Declaration (Typ : Entity_Id);
11772 -- If deferred constant is an access type initialized with an allocator,
11773 -- check whether there is an illegal recursion in the definition,
11774 -- through a default value of some record subcomponent. This is normally
11775 -- detected when generating init procs, but requires this additional
11776 -- mechanism when expansion is disabled.
11778 ----------------------------------------
11779 -- Check_Possible_Deferred_Completion --
11780 ----------------------------------------
11782 procedure Check_Possible_Deferred_Completion
11783 (Prev_Id : Entity_Id;
11784 Prev_Obj_Def : Node_Id;
11785 Curr_Obj_Def : Node_Id)
11787 begin
11788 if Nkind (Prev_Obj_Def) = N_Subtype_Indication
11789 and then Present (Constraint (Prev_Obj_Def))
11790 and then Nkind (Curr_Obj_Def) = N_Subtype_Indication
11791 and then Present (Constraint (Curr_Obj_Def))
11792 then
11793 declare
11794 Loc : constant Source_Ptr := Sloc (N);
11795 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
11796 Decl : constant Node_Id :=
11797 Make_Subtype_Declaration (Loc,
11798 Defining_Identifier => Def_Id,
11799 Subtype_Indication =>
11800 Relocate_Node (Curr_Obj_Def));
11802 begin
11803 Insert_Before_And_Analyze (N, Decl);
11804 Set_Etype (Id, Def_Id);
11806 if not Subtypes_Statically_Match (Etype (Prev_Id), Def_Id) then
11807 Error_Msg_Sloc := Sloc (Prev_Id);
11808 Error_Msg_N ("subtype does not statically match deferred "
11809 & "declaration #", N);
11810 end if;
11811 end;
11812 end if;
11813 end Check_Possible_Deferred_Completion;
11815 ---------------------------------
11816 -- Check_Recursive_Declaration --
11817 ---------------------------------
11819 procedure Check_Recursive_Declaration (Typ : Entity_Id) is
11820 Comp : Entity_Id;
11822 begin
11823 if Is_Record_Type (Typ) then
11824 Comp := First_Component (Typ);
11825 while Present (Comp) loop
11826 if Comes_From_Source (Comp) then
11827 if Present (Expression (Parent (Comp)))
11828 and then Is_Entity_Name (Expression (Parent (Comp)))
11829 and then Entity (Expression (Parent (Comp))) = Prev
11830 then
11831 Error_Msg_Sloc := Sloc (Parent (Comp));
11832 Error_Msg_NE
11833 ("illegal circularity with declaration for & #",
11834 N, Comp);
11835 return;
11837 elsif Is_Record_Type (Etype (Comp)) then
11838 Check_Recursive_Declaration (Etype (Comp));
11839 end if;
11840 end if;
11842 Next_Component (Comp);
11843 end loop;
11844 end if;
11845 end Check_Recursive_Declaration;
11847 -- Start of processing for Constant_Redeclaration
11849 begin
11850 if Nkind (Parent (Prev)) = N_Object_Declaration then
11851 if Nkind (Object_Definition
11852 (Parent (Prev))) = N_Subtype_Indication
11853 then
11854 -- Find type of new declaration. The constraints of the two
11855 -- views must match statically, but there is no point in
11856 -- creating an itype for the full view.
11858 if Nkind (Obj_Def) = N_Subtype_Indication then
11859 Find_Type (Subtype_Mark (Obj_Def));
11860 New_T := Entity (Subtype_Mark (Obj_Def));
11862 else
11863 Find_Type (Obj_Def);
11864 New_T := Entity (Obj_Def);
11865 end if;
11867 T := Etype (Prev);
11869 else
11870 -- The full view may impose a constraint, even if the partial
11871 -- view does not, so construct the subtype.
11873 New_T := Find_Type_Of_Object (Obj_Def, N);
11874 T := New_T;
11875 end if;
11877 else
11878 -- Current declaration is illegal, diagnosed below in Enter_Name
11880 T := Empty;
11881 New_T := Any_Type;
11882 end if;
11884 -- If previous full declaration or a renaming declaration exists, or if
11885 -- a homograph is present, let Enter_Name handle it, either with an
11886 -- error or with the removal of an overridden implicit subprogram.
11887 -- The previous one is a full declaration if it has an expression
11888 -- (which in the case of an aggregate is indicated by the Init flag).
11890 if Ekind (Prev) /= E_Constant
11891 or else Nkind (Parent (Prev)) = N_Object_Renaming_Declaration
11892 or else Present (Expression (Parent (Prev)))
11893 or else Has_Init_Expression (Parent (Prev))
11894 or else Present (Full_View (Prev))
11895 then
11896 Enter_Name (Id);
11898 -- Verify that types of both declarations match, or else that both types
11899 -- are anonymous access types whose designated subtypes statically match
11900 -- (as allowed in Ada 2005 by AI-385).
11902 elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
11903 and then
11904 (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
11905 or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
11906 or else Is_Access_Constant (Etype (New_T)) /=
11907 Is_Access_Constant (Etype (Prev))
11908 or else Can_Never_Be_Null (Etype (New_T)) /=
11909 Can_Never_Be_Null (Etype (Prev))
11910 or else Null_Exclusion_Present (Parent (Prev)) /=
11911 Null_Exclusion_Present (Parent (Id))
11912 or else not Subtypes_Statically_Match
11913 (Designated_Type (Etype (Prev)),
11914 Designated_Type (Etype (New_T))))
11915 then
11916 Error_Msg_Sloc := Sloc (Prev);
11917 Error_Msg_N ("type does not match declaration#", N);
11918 Set_Full_View (Prev, Id);
11919 Set_Etype (Id, Any_Type);
11921 -- A deferred constant whose type is an anonymous array is always
11922 -- illegal (unless imported). A detailed error message might be
11923 -- helpful for Ada beginners.
11925 if Nkind (Object_Definition (Parent (Prev)))
11926 = N_Constrained_Array_Definition
11927 and then Nkind (Object_Definition (N))
11928 = N_Constrained_Array_Definition
11929 then
11930 Error_Msg_N ("\each anonymous array is a distinct type", N);
11931 Error_Msg_N ("a deferred constant must have a named type",
11932 Object_Definition (Parent (Prev)));
11933 end if;
11935 elsif
11936 Null_Exclusion_Present (Parent (Prev))
11937 and then not Null_Exclusion_Present (N)
11938 then
11939 Error_Msg_Sloc := Sloc (Prev);
11940 Error_Msg_N ("null-exclusion does not match declaration#", N);
11941 Set_Full_View (Prev, Id);
11942 Set_Etype (Id, Any_Type);
11944 -- If so, process the full constant declaration
11946 else
11947 -- RM 7.4 (6): If the subtype defined by the subtype_indication in
11948 -- the deferred declaration is constrained, then the subtype defined
11949 -- by the subtype_indication in the full declaration shall match it
11950 -- statically.
11952 Check_Possible_Deferred_Completion
11953 (Prev_Id => Prev,
11954 Prev_Obj_Def => Object_Definition (Parent (Prev)),
11955 Curr_Obj_Def => Obj_Def);
11957 Set_Full_View (Prev, Id);
11958 Set_Is_Public (Id, Is_Public (Prev));
11959 Set_Is_Internal (Id);
11960 Append_Entity (Id, Current_Scope);
11962 -- Check ALIASED present if present before (RM 7.4(7))
11964 if Is_Aliased (Prev)
11965 and then not Aliased_Present (N)
11966 then
11967 Error_Msg_Sloc := Sloc (Prev);
11968 Error_Msg_N ("ALIASED required (see declaration #)", N);
11969 end if;
11971 -- Check that placement is in private part and that the incomplete
11972 -- declaration appeared in the visible part.
11974 if Ekind (Current_Scope) = E_Package
11975 and then not In_Private_Part (Current_Scope)
11976 then
11977 Error_Msg_Sloc := Sloc (Prev);
11978 Error_Msg_N
11979 ("full constant for declaration # must be in private part", N);
11981 elsif Ekind (Current_Scope) = E_Package
11982 and then
11983 List_Containing (Parent (Prev)) /=
11984 Visible_Declarations (Package_Specification (Current_Scope))
11985 then
11986 Error_Msg_N
11987 ("deferred constant must be declared in visible part",
11988 Parent (Prev));
11989 end if;
11991 if Is_Access_Type (T)
11992 and then Nkind (Expression (N)) = N_Allocator
11993 then
11994 Check_Recursive_Declaration (Designated_Type (T));
11995 end if;
11997 -- A deferred constant is a visible entity. If type has invariants,
11998 -- verify that the initial value satisfies them.
12000 if Has_Invariants (T) and then Present (Invariant_Procedure (T)) then
12001 Insert_After (N,
12002 Make_Invariant_Call (New_Occurrence_Of (Prev, Sloc (N))));
12003 end if;
12004 end if;
12005 end Constant_Redeclaration;
12007 ----------------------
12008 -- Constrain_Access --
12009 ----------------------
12011 procedure Constrain_Access
12012 (Def_Id : in out Entity_Id;
12013 S : Node_Id;
12014 Related_Nod : Node_Id)
12016 T : constant Entity_Id := Entity (Subtype_Mark (S));
12017 Desig_Type : constant Entity_Id := Designated_Type (T);
12018 Desig_Subtype : Entity_Id := Create_Itype (E_Void, Related_Nod);
12019 Constraint_OK : Boolean := True;
12021 begin
12022 if Is_Array_Type (Desig_Type) then
12023 Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
12025 elsif (Is_Record_Type (Desig_Type)
12026 or else Is_Incomplete_Or_Private_Type (Desig_Type))
12027 and then not Is_Constrained (Desig_Type)
12028 then
12029 -- ??? The following code is a temporary bypass to ignore a
12030 -- discriminant constraint on access type if it is constraining
12031 -- the current record. Avoid creating the implicit subtype of the
12032 -- record we are currently compiling since right now, we cannot
12033 -- handle these. For now, just return the access type itself.
12035 if Desig_Type = Current_Scope
12036 and then No (Def_Id)
12037 then
12038 Set_Ekind (Desig_Subtype, E_Record_Subtype);
12039 Def_Id := Entity (Subtype_Mark (S));
12041 -- This call added to ensure that the constraint is analyzed
12042 -- (needed for a B test). Note that we still return early from
12043 -- this procedure to avoid recursive processing. ???
12045 Constrain_Discriminated_Type
12046 (Desig_Subtype, S, Related_Nod, For_Access => True);
12047 return;
12048 end if;
12050 -- Enforce rule that the constraint is illegal if there is an
12051 -- unconstrained view of the designated type. This means that the
12052 -- partial view (either a private type declaration or a derivation
12053 -- from a private type) has no discriminants. (Defect Report
12054 -- 8652/0008, Technical Corrigendum 1, checked by ACATS B371001).
12056 -- Rule updated for Ada 2005: The private type is said to have
12057 -- a constrained partial view, given that objects of the type
12058 -- can be declared. Furthermore, the rule applies to all access
12059 -- types, unlike the rule concerning default discriminants (see
12060 -- RM 3.7.1(7/3))
12062 if (Ekind (T) = E_General_Access_Type or else Ada_Version >= Ada_2005)
12063 and then Has_Private_Declaration (Desig_Type)
12064 and then In_Open_Scopes (Scope (Desig_Type))
12065 and then Has_Discriminants (Desig_Type)
12066 then
12067 declare
12068 Pack : constant Node_Id :=
12069 Unit_Declaration_Node (Scope (Desig_Type));
12070 Decls : List_Id;
12071 Decl : Node_Id;
12073 begin
12074 if Nkind (Pack) = N_Package_Declaration then
12075 Decls := Visible_Declarations (Specification (Pack));
12076 Decl := First (Decls);
12077 while Present (Decl) loop
12078 if (Nkind (Decl) = N_Private_Type_Declaration
12079 and then Chars (Defining_Identifier (Decl)) =
12080 Chars (Desig_Type))
12082 or else
12083 (Nkind (Decl) = N_Full_Type_Declaration
12084 and then
12085 Chars (Defining_Identifier (Decl)) =
12086 Chars (Desig_Type)
12087 and then Is_Derived_Type (Desig_Type)
12088 and then
12089 Has_Private_Declaration (Etype (Desig_Type)))
12090 then
12091 if No (Discriminant_Specifications (Decl)) then
12092 Error_Msg_N
12093 ("cannot constrain access type if designated "
12094 & "type has constrained partial view", S);
12095 end if;
12097 exit;
12098 end if;
12100 Next (Decl);
12101 end loop;
12102 end if;
12103 end;
12104 end if;
12106 Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
12107 For_Access => True);
12109 elsif Is_Concurrent_Type (Desig_Type)
12110 and then not Is_Constrained (Desig_Type)
12111 then
12112 Constrain_Concurrent (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
12114 else
12115 Error_Msg_N ("invalid constraint on access type", S);
12117 -- We simply ignore an invalid constraint
12119 Desig_Subtype := Desig_Type;
12120 Constraint_OK := False;
12121 end if;
12123 if No (Def_Id) then
12124 Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
12125 else
12126 Set_Ekind (Def_Id, E_Access_Subtype);
12127 end if;
12129 if Constraint_OK then
12130 Set_Etype (Def_Id, Base_Type (T));
12132 if Is_Private_Type (Desig_Type) then
12133 Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
12134 end if;
12135 else
12136 Set_Etype (Def_Id, Any_Type);
12137 end if;
12139 Set_Size_Info (Def_Id, T);
12140 Set_Is_Constrained (Def_Id, Constraint_OK);
12141 Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
12142 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
12143 Set_Is_Access_Constant (Def_Id, Is_Access_Constant (T));
12145 Conditional_Delay (Def_Id, T);
12147 -- AI-363 : Subtypes of general access types whose designated types have
12148 -- default discriminants are disallowed. In instances, the rule has to
12149 -- be checked against the actual, of which T is the subtype. In a
12150 -- generic body, the rule is checked assuming that the actual type has
12151 -- defaulted discriminants.
12153 if Ada_Version >= Ada_2005 or else Warn_On_Ada_2005_Compatibility then
12154 if Ekind (Base_Type (T)) = E_General_Access_Type
12155 and then Has_Defaulted_Discriminants (Desig_Type)
12156 then
12157 if Ada_Version < Ada_2005 then
12158 Error_Msg_N
12159 ("access subtype of general access type would not " &
12160 "be allowed in Ada 2005?y?", S);
12161 else
12162 Error_Msg_N
12163 ("access subtype of general access type not allowed", S);
12164 end if;
12166 Error_Msg_N ("\discriminants have defaults", S);
12168 elsif Is_Access_Type (T)
12169 and then Is_Generic_Type (Desig_Type)
12170 and then Has_Discriminants (Desig_Type)
12171 and then In_Package_Body (Current_Scope)
12172 then
12173 if Ada_Version < Ada_2005 then
12174 Error_Msg_N
12175 ("access subtype would not be allowed in generic body "
12176 & "in Ada 2005?y?", S);
12177 else
12178 Error_Msg_N
12179 ("access subtype not allowed in generic body", S);
12180 end if;
12182 Error_Msg_N
12183 ("\designated type is a discriminated formal", S);
12184 end if;
12185 end if;
12186 end Constrain_Access;
12188 ---------------------
12189 -- Constrain_Array --
12190 ---------------------
12192 procedure Constrain_Array
12193 (Def_Id : in out Entity_Id;
12194 SI : Node_Id;
12195 Related_Nod : Node_Id;
12196 Related_Id : Entity_Id;
12197 Suffix : Character)
12199 C : constant Node_Id := Constraint (SI);
12200 Number_Of_Constraints : Nat := 0;
12201 Index : Node_Id;
12202 S, T : Entity_Id;
12203 Constraint_OK : Boolean := True;
12205 begin
12206 T := Entity (Subtype_Mark (SI));
12208 if Is_Access_Type (T) then
12209 T := Designated_Type (T);
12210 end if;
12212 -- If an index constraint follows a subtype mark in a subtype indication
12213 -- then the type or subtype denoted by the subtype mark must not already
12214 -- impose an index constraint. The subtype mark must denote either an
12215 -- unconstrained array type or an access type whose designated type
12216 -- is such an array type... (RM 3.6.1)
12218 if Is_Constrained (T) then
12219 Error_Msg_N ("array type is already constrained", Subtype_Mark (SI));
12220 Constraint_OK := False;
12222 else
12223 S := First (Constraints (C));
12224 while Present (S) loop
12225 Number_Of_Constraints := Number_Of_Constraints + 1;
12226 Next (S);
12227 end loop;
12229 -- In either case, the index constraint must provide a discrete
12230 -- range for each index of the array type and the type of each
12231 -- discrete range must be the same as that of the corresponding
12232 -- index. (RM 3.6.1)
12234 if Number_Of_Constraints /= Number_Dimensions (T) then
12235 Error_Msg_NE ("incorrect number of index constraints for }", C, T);
12236 Constraint_OK := False;
12238 else
12239 S := First (Constraints (C));
12240 Index := First_Index (T);
12241 Analyze (Index);
12243 -- Apply constraints to each index type
12245 for J in 1 .. Number_Of_Constraints loop
12246 Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
12247 Next (Index);
12248 Next (S);
12249 end loop;
12251 end if;
12252 end if;
12254 if No (Def_Id) then
12255 Def_Id :=
12256 Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
12257 Set_Parent (Def_Id, Related_Nod);
12259 else
12260 Set_Ekind (Def_Id, E_Array_Subtype);
12261 end if;
12263 Set_Size_Info (Def_Id, (T));
12264 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
12265 Set_Etype (Def_Id, Base_Type (T));
12267 if Constraint_OK then
12268 Set_First_Index (Def_Id, First (Constraints (C)));
12269 else
12270 Set_First_Index (Def_Id, First_Index (T));
12271 end if;
12273 Set_Is_Constrained (Def_Id, True);
12274 Set_Is_Aliased (Def_Id, Is_Aliased (T));
12275 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
12277 Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
12278 Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
12280 -- A subtype does not inherit the Packed_Array_Impl_Type of is parent.
12281 -- We need to initialize the attribute because if Def_Id is previously
12282 -- analyzed through a limited_with clause, it will have the attributes
12283 -- of an incomplete type, one of which is an Elist that overlaps the
12284 -- Packed_Array_Impl_Type field.
12286 Set_Packed_Array_Impl_Type (Def_Id, Empty);
12288 -- Build a freeze node if parent still needs one. Also make sure that
12289 -- the Depends_On_Private status is set because the subtype will need
12290 -- reprocessing at the time the base type does, and also we must set a
12291 -- conditional delay.
12293 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
12294 Conditional_Delay (Def_Id, T);
12295 end Constrain_Array;
12297 ------------------------------
12298 -- Constrain_Component_Type --
12299 ------------------------------
12301 function Constrain_Component_Type
12302 (Comp : Entity_Id;
12303 Constrained_Typ : Entity_Id;
12304 Related_Node : Node_Id;
12305 Typ : Entity_Id;
12306 Constraints : Elist_Id) return Entity_Id
12308 Loc : constant Source_Ptr := Sloc (Constrained_Typ);
12309 Compon_Type : constant Entity_Id := Etype (Comp);
12311 function Build_Constrained_Array_Type
12312 (Old_Type : Entity_Id) return Entity_Id;
12313 -- If Old_Type is an array type, one of whose indexes is constrained
12314 -- by a discriminant, build an Itype whose constraint replaces the
12315 -- discriminant with its value in the constraint.
12317 function Build_Constrained_Discriminated_Type
12318 (Old_Type : Entity_Id) return Entity_Id;
12319 -- Ditto for record components
12321 function Build_Constrained_Access_Type
12322 (Old_Type : Entity_Id) return Entity_Id;
12323 -- Ditto for access types. Makes use of previous two functions, to
12324 -- constrain designated type.
12326 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id;
12327 -- T is an array or discriminated type, C is a list of constraints
12328 -- that apply to T. This routine builds the constrained subtype.
12330 function Is_Discriminant (Expr : Node_Id) return Boolean;
12331 -- Returns True if Expr is a discriminant
12333 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id;
12334 -- Find the value of discriminant Discrim in Constraint
12336 -----------------------------------
12337 -- Build_Constrained_Access_Type --
12338 -----------------------------------
12340 function Build_Constrained_Access_Type
12341 (Old_Type : Entity_Id) return Entity_Id
12343 Desig_Type : constant Entity_Id := Designated_Type (Old_Type);
12344 Itype : Entity_Id;
12345 Desig_Subtype : Entity_Id;
12346 Scop : Entity_Id;
12348 begin
12349 -- if the original access type was not embedded in the enclosing
12350 -- type definition, there is no need to produce a new access
12351 -- subtype. In fact every access type with an explicit constraint
12352 -- generates an itype whose scope is the enclosing record.
12354 if not Is_Type (Scope (Old_Type)) then
12355 return Old_Type;
12357 elsif Is_Array_Type (Desig_Type) then
12358 Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
12360 elsif Has_Discriminants (Desig_Type) then
12362 -- This may be an access type to an enclosing record type for
12363 -- which we are constructing the constrained components. Return
12364 -- the enclosing record subtype. This is not always correct,
12365 -- but avoids infinite recursion. ???
12367 Desig_Subtype := Any_Type;
12369 for J in reverse 0 .. Scope_Stack.Last loop
12370 Scop := Scope_Stack.Table (J).Entity;
12372 if Is_Type (Scop)
12373 and then Base_Type (Scop) = Base_Type (Desig_Type)
12374 then
12375 Desig_Subtype := Scop;
12376 end if;
12378 exit when not Is_Type (Scop);
12379 end loop;
12381 if Desig_Subtype = Any_Type then
12382 Desig_Subtype :=
12383 Build_Constrained_Discriminated_Type (Desig_Type);
12384 end if;
12386 else
12387 return Old_Type;
12388 end if;
12390 if Desig_Subtype /= Desig_Type then
12392 -- The Related_Node better be here or else we won't be able
12393 -- to attach new itypes to a node in the tree.
12395 pragma Assert (Present (Related_Node));
12397 Itype := Create_Itype (E_Access_Subtype, Related_Node);
12399 Set_Etype (Itype, Base_Type (Old_Type));
12400 Set_Size_Info (Itype, (Old_Type));
12401 Set_Directly_Designated_Type (Itype, Desig_Subtype);
12402 Set_Depends_On_Private (Itype, Has_Private_Component
12403 (Old_Type));
12404 Set_Is_Access_Constant (Itype, Is_Access_Constant
12405 (Old_Type));
12407 -- The new itype needs freezing when it depends on a not frozen
12408 -- type and the enclosing subtype needs freezing.
12410 if Has_Delayed_Freeze (Constrained_Typ)
12411 and then not Is_Frozen (Constrained_Typ)
12412 then
12413 Conditional_Delay (Itype, Base_Type (Old_Type));
12414 end if;
12416 return Itype;
12418 else
12419 return Old_Type;
12420 end if;
12421 end Build_Constrained_Access_Type;
12423 ----------------------------------
12424 -- Build_Constrained_Array_Type --
12425 ----------------------------------
12427 function Build_Constrained_Array_Type
12428 (Old_Type : Entity_Id) return Entity_Id
12430 Lo_Expr : Node_Id;
12431 Hi_Expr : Node_Id;
12432 Old_Index : Node_Id;
12433 Range_Node : Node_Id;
12434 Constr_List : List_Id;
12436 Need_To_Create_Itype : Boolean := False;
12438 begin
12439 Old_Index := First_Index (Old_Type);
12440 while Present (Old_Index) loop
12441 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
12443 if Is_Discriminant (Lo_Expr)
12444 or else
12445 Is_Discriminant (Hi_Expr)
12446 then
12447 Need_To_Create_Itype := True;
12448 end if;
12450 Next_Index (Old_Index);
12451 end loop;
12453 if Need_To_Create_Itype then
12454 Constr_List := New_List;
12456 Old_Index := First_Index (Old_Type);
12457 while Present (Old_Index) loop
12458 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
12460 if Is_Discriminant (Lo_Expr) then
12461 Lo_Expr := Get_Discr_Value (Lo_Expr);
12462 end if;
12464 if Is_Discriminant (Hi_Expr) then
12465 Hi_Expr := Get_Discr_Value (Hi_Expr);
12466 end if;
12468 Range_Node :=
12469 Make_Range
12470 (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
12472 Append (Range_Node, To => Constr_List);
12474 Next_Index (Old_Index);
12475 end loop;
12477 return Build_Subtype (Old_Type, Constr_List);
12479 else
12480 return Old_Type;
12481 end if;
12482 end Build_Constrained_Array_Type;
12484 ------------------------------------------
12485 -- Build_Constrained_Discriminated_Type --
12486 ------------------------------------------
12488 function Build_Constrained_Discriminated_Type
12489 (Old_Type : Entity_Id) return Entity_Id
12491 Expr : Node_Id;
12492 Constr_List : List_Id;
12493 Old_Constraint : Elmt_Id;
12495 Need_To_Create_Itype : Boolean := False;
12497 begin
12498 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
12499 while Present (Old_Constraint) loop
12500 Expr := Node (Old_Constraint);
12502 if Is_Discriminant (Expr) then
12503 Need_To_Create_Itype := True;
12504 end if;
12506 Next_Elmt (Old_Constraint);
12507 end loop;
12509 if Need_To_Create_Itype then
12510 Constr_List := New_List;
12512 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
12513 while Present (Old_Constraint) loop
12514 Expr := Node (Old_Constraint);
12516 if Is_Discriminant (Expr) then
12517 Expr := Get_Discr_Value (Expr);
12518 end if;
12520 Append (New_Copy_Tree (Expr), To => Constr_List);
12522 Next_Elmt (Old_Constraint);
12523 end loop;
12525 return Build_Subtype (Old_Type, Constr_List);
12527 else
12528 return Old_Type;
12529 end if;
12530 end Build_Constrained_Discriminated_Type;
12532 -------------------
12533 -- Build_Subtype --
12534 -------------------
12536 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id is
12537 Indic : Node_Id;
12538 Subtyp_Decl : Node_Id;
12539 Def_Id : Entity_Id;
12540 Btyp : Entity_Id := Base_Type (T);
12542 begin
12543 -- The Related_Node better be here or else we won't be able to
12544 -- attach new itypes to a node in the tree.
12546 pragma Assert (Present (Related_Node));
12548 -- If the view of the component's type is incomplete or private
12549 -- with unknown discriminants, then the constraint must be applied
12550 -- to the full type.
12552 if Has_Unknown_Discriminants (Btyp)
12553 and then Present (Underlying_Type (Btyp))
12554 then
12555 Btyp := Underlying_Type (Btyp);
12556 end if;
12558 Indic :=
12559 Make_Subtype_Indication (Loc,
12560 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
12561 Constraint => Make_Index_Or_Discriminant_Constraint (Loc, C));
12563 Def_Id := Create_Itype (Ekind (T), Related_Node);
12565 Subtyp_Decl :=
12566 Make_Subtype_Declaration (Loc,
12567 Defining_Identifier => Def_Id,
12568 Subtype_Indication => Indic);
12570 Set_Parent (Subtyp_Decl, Parent (Related_Node));
12572 -- Itypes must be analyzed with checks off (see package Itypes)
12574 Analyze (Subtyp_Decl, Suppress => All_Checks);
12576 return Def_Id;
12577 end Build_Subtype;
12579 ---------------------
12580 -- Get_Discr_Value --
12581 ---------------------
12583 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id is
12584 D : Entity_Id;
12585 E : Elmt_Id;
12587 begin
12588 -- The discriminant may be declared for the type, in which case we
12589 -- find it by iterating over the list of discriminants. If the
12590 -- discriminant is inherited from a parent type, it appears as the
12591 -- corresponding discriminant of the current type. This will be the
12592 -- case when constraining an inherited component whose constraint is
12593 -- given by a discriminant of the parent.
12595 D := First_Discriminant (Typ);
12596 E := First_Elmt (Constraints);
12598 while Present (D) loop
12599 if D = Entity (Discrim)
12600 or else D = CR_Discriminant (Entity (Discrim))
12601 or else Corresponding_Discriminant (D) = Entity (Discrim)
12602 then
12603 return Node (E);
12604 end if;
12606 Next_Discriminant (D);
12607 Next_Elmt (E);
12608 end loop;
12610 -- The Corresponding_Discriminant mechanism is incomplete, because
12611 -- the correspondence between new and old discriminants is not one
12612 -- to one: one new discriminant can constrain several old ones. In
12613 -- that case, scan sequentially the stored_constraint, the list of
12614 -- discriminants of the parents, and the constraints.
12616 -- Previous code checked for the present of the Stored_Constraint
12617 -- list for the derived type, but did not use it at all. Should it
12618 -- be present when the component is a discriminated task type?
12620 if Is_Derived_Type (Typ)
12621 and then Scope (Entity (Discrim)) = Etype (Typ)
12622 then
12623 D := First_Discriminant (Etype (Typ));
12624 E := First_Elmt (Constraints);
12625 while Present (D) loop
12626 if D = Entity (Discrim) then
12627 return Node (E);
12628 end if;
12630 Next_Discriminant (D);
12631 Next_Elmt (E);
12632 end loop;
12633 end if;
12635 -- Something is wrong if we did not find the value
12637 raise Program_Error;
12638 end Get_Discr_Value;
12640 ---------------------
12641 -- Is_Discriminant --
12642 ---------------------
12644 function Is_Discriminant (Expr : Node_Id) return Boolean is
12645 Discrim_Scope : Entity_Id;
12647 begin
12648 if Denotes_Discriminant (Expr) then
12649 Discrim_Scope := Scope (Entity (Expr));
12651 -- Either we have a reference to one of Typ's discriminants,
12653 pragma Assert (Discrim_Scope = Typ
12655 -- or to the discriminants of the parent type, in the case
12656 -- of a derivation of a tagged type with variants.
12658 or else Discrim_Scope = Etype (Typ)
12659 or else Full_View (Discrim_Scope) = Etype (Typ)
12661 -- or same as above for the case where the discriminants
12662 -- were declared in Typ's private view.
12664 or else (Is_Private_Type (Discrim_Scope)
12665 and then Chars (Discrim_Scope) = Chars (Typ))
12667 -- or else we are deriving from the full view and the
12668 -- discriminant is declared in the private entity.
12670 or else (Is_Private_Type (Typ)
12671 and then Chars (Discrim_Scope) = Chars (Typ))
12673 -- Or we are constrained the corresponding record of a
12674 -- synchronized type that completes a private declaration.
12676 or else (Is_Concurrent_Record_Type (Typ)
12677 and then
12678 Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
12680 -- or we have a class-wide type, in which case make sure the
12681 -- discriminant found belongs to the root type.
12683 or else (Is_Class_Wide_Type (Typ)
12684 and then Etype (Typ) = Discrim_Scope));
12686 return True;
12687 end if;
12689 -- In all other cases we have something wrong
12691 return False;
12692 end Is_Discriminant;
12694 -- Start of processing for Constrain_Component_Type
12696 begin
12697 if Nkind (Parent (Comp)) = N_Component_Declaration
12698 and then Comes_From_Source (Parent (Comp))
12699 and then Comes_From_Source
12700 (Subtype_Indication (Component_Definition (Parent (Comp))))
12701 and then
12702 Is_Entity_Name
12703 (Subtype_Indication (Component_Definition (Parent (Comp))))
12704 then
12705 return Compon_Type;
12707 elsif Is_Array_Type (Compon_Type) then
12708 return Build_Constrained_Array_Type (Compon_Type);
12710 elsif Has_Discriminants (Compon_Type) then
12711 return Build_Constrained_Discriminated_Type (Compon_Type);
12713 elsif Is_Access_Type (Compon_Type) then
12714 return Build_Constrained_Access_Type (Compon_Type);
12716 else
12717 return Compon_Type;
12718 end if;
12719 end Constrain_Component_Type;
12721 --------------------------
12722 -- Constrain_Concurrent --
12723 --------------------------
12725 -- For concurrent types, the associated record value type carries the same
12726 -- discriminants, so when we constrain a concurrent type, we must constrain
12727 -- the corresponding record type as well.
12729 procedure Constrain_Concurrent
12730 (Def_Id : in out Entity_Id;
12731 SI : Node_Id;
12732 Related_Nod : Node_Id;
12733 Related_Id : Entity_Id;
12734 Suffix : Character)
12736 -- Retrieve Base_Type to ensure getting to the concurrent type in the
12737 -- case of a private subtype (needed when only doing semantic analysis).
12739 T_Ent : Entity_Id := Base_Type (Entity (Subtype_Mark (SI)));
12740 T_Val : Entity_Id;
12742 begin
12743 if Is_Access_Type (T_Ent) then
12744 T_Ent := Designated_Type (T_Ent);
12745 end if;
12747 T_Val := Corresponding_Record_Type (T_Ent);
12749 if Present (T_Val) then
12751 if No (Def_Id) then
12752 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
12753 end if;
12755 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
12757 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
12758 Set_Corresponding_Record_Type (Def_Id,
12759 Constrain_Corresponding_Record (Def_Id, T_Val, Related_Nod));
12761 else
12762 -- If there is no associated record, expansion is disabled and this
12763 -- is a generic context. Create a subtype in any case, so that
12764 -- semantic analysis can proceed.
12766 if No (Def_Id) then
12767 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
12768 end if;
12770 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
12771 end if;
12772 end Constrain_Concurrent;
12774 ------------------------------------
12775 -- Constrain_Corresponding_Record --
12776 ------------------------------------
12778 function Constrain_Corresponding_Record
12779 (Prot_Subt : Entity_Id;
12780 Corr_Rec : Entity_Id;
12781 Related_Nod : Node_Id) return Entity_Id
12783 T_Sub : constant Entity_Id :=
12784 Create_Itype (E_Record_Subtype, Related_Nod, Corr_Rec, 'C');
12786 begin
12787 Set_Etype (T_Sub, Corr_Rec);
12788 Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
12789 Set_Is_Constrained (T_Sub, True);
12790 Set_First_Entity (T_Sub, First_Entity (Corr_Rec));
12791 Set_Last_Entity (T_Sub, Last_Entity (Corr_Rec));
12793 if Has_Discriminants (Prot_Subt) then -- False only if errors.
12794 Set_Discriminant_Constraint
12795 (T_Sub, Discriminant_Constraint (Prot_Subt));
12796 Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
12797 Create_Constrained_Components
12798 (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
12799 end if;
12801 Set_Depends_On_Private (T_Sub, Has_Private_Component (T_Sub));
12803 if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
12804 Conditional_Delay (T_Sub, Corr_Rec);
12806 else
12807 -- This is a component subtype: it will be frozen in the context of
12808 -- the enclosing record's init_proc, so that discriminant references
12809 -- are resolved to discriminals. (Note: we used to skip freezing
12810 -- altogether in that case, which caused errors downstream for
12811 -- components of a bit packed array type).
12813 Set_Has_Delayed_Freeze (T_Sub);
12814 end if;
12816 return T_Sub;
12817 end Constrain_Corresponding_Record;
12819 -----------------------
12820 -- Constrain_Decimal --
12821 -----------------------
12823 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id) is
12824 T : constant Entity_Id := Entity (Subtype_Mark (S));
12825 C : constant Node_Id := Constraint (S);
12826 Loc : constant Source_Ptr := Sloc (C);
12827 Range_Expr : Node_Id;
12828 Digits_Expr : Node_Id;
12829 Digits_Val : Uint;
12830 Bound_Val : Ureal;
12832 begin
12833 Set_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
12835 if Nkind (C) = N_Range_Constraint then
12836 Range_Expr := Range_Expression (C);
12837 Digits_Val := Digits_Value (T);
12839 else
12840 pragma Assert (Nkind (C) = N_Digits_Constraint);
12842 Check_SPARK_05_Restriction ("digits constraint is not allowed", S);
12844 Digits_Expr := Digits_Expression (C);
12845 Analyze_And_Resolve (Digits_Expr, Any_Integer);
12847 Check_Digits_Expression (Digits_Expr);
12848 Digits_Val := Expr_Value (Digits_Expr);
12850 if Digits_Val > Digits_Value (T) then
12851 Error_Msg_N
12852 ("digits expression is incompatible with subtype", C);
12853 Digits_Val := Digits_Value (T);
12854 end if;
12856 if Present (Range_Constraint (C)) then
12857 Range_Expr := Range_Expression (Range_Constraint (C));
12858 else
12859 Range_Expr := Empty;
12860 end if;
12861 end if;
12863 Set_Etype (Def_Id, Base_Type (T));
12864 Set_Size_Info (Def_Id, (T));
12865 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
12866 Set_Delta_Value (Def_Id, Delta_Value (T));
12867 Set_Scale_Value (Def_Id, Scale_Value (T));
12868 Set_Small_Value (Def_Id, Small_Value (T));
12869 Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
12870 Set_Digits_Value (Def_Id, Digits_Val);
12872 -- Manufacture range from given digits value if no range present
12874 if No (Range_Expr) then
12875 Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
12876 Range_Expr :=
12877 Make_Range (Loc,
12878 Low_Bound =>
12879 Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
12880 High_Bound =>
12881 Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
12882 end if;
12884 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
12885 Set_Discrete_RM_Size (Def_Id);
12887 -- Unconditionally delay the freeze, since we cannot set size
12888 -- information in all cases correctly until the freeze point.
12890 Set_Has_Delayed_Freeze (Def_Id);
12891 end Constrain_Decimal;
12893 ----------------------------------
12894 -- Constrain_Discriminated_Type --
12895 ----------------------------------
12897 procedure Constrain_Discriminated_Type
12898 (Def_Id : Entity_Id;
12899 S : Node_Id;
12900 Related_Nod : Node_Id;
12901 For_Access : Boolean := False)
12903 E : constant Entity_Id := Entity (Subtype_Mark (S));
12904 T : Entity_Id;
12905 C : Node_Id;
12906 Elist : Elist_Id := New_Elmt_List;
12908 procedure Fixup_Bad_Constraint;
12909 -- This is called after finding a bad constraint, and after having
12910 -- posted an appropriate error message. The mission is to leave the
12911 -- entity T in as reasonable state as possible.
12913 --------------------------
12914 -- Fixup_Bad_Constraint --
12915 --------------------------
12917 procedure Fixup_Bad_Constraint is
12918 begin
12919 -- Set a reasonable Ekind for the entity. For an incomplete type,
12920 -- we can't do much, but for other types, we can set the proper
12921 -- corresponding subtype kind.
12923 if Ekind (T) = E_Incomplete_Type then
12924 Set_Ekind (Def_Id, Ekind (T));
12925 else
12926 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
12927 end if;
12929 -- Set Etype to the known type, to reduce chances of cascaded errors
12931 Set_Etype (Def_Id, E);
12932 Set_Error_Posted (Def_Id);
12933 end Fixup_Bad_Constraint;
12935 -- Start of processing for Constrain_Discriminated_Type
12937 begin
12938 C := Constraint (S);
12940 -- A discriminant constraint is only allowed in a subtype indication,
12941 -- after a subtype mark. This subtype mark must denote either a type
12942 -- with discriminants, or an access type whose designated type is a
12943 -- type with discriminants. A discriminant constraint specifies the
12944 -- values of these discriminants (RM 3.7.2(5)).
12946 T := Base_Type (Entity (Subtype_Mark (S)));
12948 if Is_Access_Type (T) then
12949 T := Designated_Type (T);
12950 end if;
12952 -- Ada 2005 (AI-412): Constrained incomplete subtypes are illegal.
12953 -- Avoid generating an error for access-to-incomplete subtypes.
12955 if Ada_Version >= Ada_2005
12956 and then Ekind (T) = E_Incomplete_Type
12957 and then Nkind (Parent (S)) = N_Subtype_Declaration
12958 and then not Is_Itype (Def_Id)
12959 then
12960 -- A little sanity check, emit an error message if the type
12961 -- has discriminants to begin with. Type T may be a regular
12962 -- incomplete type or imported via a limited with clause.
12964 if Has_Discriminants (T)
12965 or else (From_Limited_With (T)
12966 and then Present (Non_Limited_View (T))
12967 and then Nkind (Parent (Non_Limited_View (T))) =
12968 N_Full_Type_Declaration
12969 and then Present (Discriminant_Specifications
12970 (Parent (Non_Limited_View (T)))))
12971 then
12972 Error_Msg_N
12973 ("(Ada 2005) incomplete subtype may not be constrained", C);
12974 else
12975 Error_Msg_N ("invalid constraint: type has no discriminant", C);
12976 end if;
12978 Fixup_Bad_Constraint;
12979 return;
12981 -- Check that the type has visible discriminants. The type may be
12982 -- a private type with unknown discriminants whose full view has
12983 -- discriminants which are invisible.
12985 elsif not Has_Discriminants (T)
12986 or else
12987 (Has_Unknown_Discriminants (T)
12988 and then Is_Private_Type (T))
12989 then
12990 Error_Msg_N ("invalid constraint: type has no discriminant", C);
12991 Fixup_Bad_Constraint;
12992 return;
12994 elsif Is_Constrained (E)
12995 or else (Ekind (E) = E_Class_Wide_Subtype
12996 and then Present (Discriminant_Constraint (E)))
12997 then
12998 Error_Msg_N ("type is already constrained", Subtype_Mark (S));
12999 Fixup_Bad_Constraint;
13000 return;
13001 end if;
13003 -- T may be an unconstrained subtype (e.g. a generic actual).
13004 -- Constraint applies to the base type.
13006 T := Base_Type (T);
13008 Elist := Build_Discriminant_Constraints (T, S);
13010 -- If the list returned was empty we had an error in building the
13011 -- discriminant constraint. We have also already signalled an error
13012 -- in the incomplete type case
13014 if Is_Empty_Elmt_List (Elist) then
13015 Fixup_Bad_Constraint;
13016 return;
13017 end if;
13019 Build_Discriminated_Subtype (T, Def_Id, Elist, Related_Nod, For_Access);
13020 end Constrain_Discriminated_Type;
13022 ---------------------------
13023 -- Constrain_Enumeration --
13024 ---------------------------
13026 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id) is
13027 T : constant Entity_Id := Entity (Subtype_Mark (S));
13028 C : constant Node_Id := Constraint (S);
13030 begin
13031 Set_Ekind (Def_Id, E_Enumeration_Subtype);
13033 Set_First_Literal (Def_Id, First_Literal (Base_Type (T)));
13035 Set_Etype (Def_Id, Base_Type (T));
13036 Set_Size_Info (Def_Id, (T));
13037 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13038 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
13040 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13042 Set_Discrete_RM_Size (Def_Id);
13043 end Constrain_Enumeration;
13045 ----------------------
13046 -- Constrain_Float --
13047 ----------------------
13049 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id) is
13050 T : constant Entity_Id := Entity (Subtype_Mark (S));
13051 C : Node_Id;
13052 D : Node_Id;
13053 Rais : Node_Id;
13055 begin
13056 Set_Ekind (Def_Id, E_Floating_Point_Subtype);
13058 Set_Etype (Def_Id, Base_Type (T));
13059 Set_Size_Info (Def_Id, (T));
13060 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13062 -- Process the constraint
13064 C := Constraint (S);
13066 -- Digits constraint present
13068 if Nkind (C) = N_Digits_Constraint then
13070 Check_SPARK_05_Restriction ("digits constraint is not allowed", S);
13071 Check_Restriction (No_Obsolescent_Features, C);
13073 if Warn_On_Obsolescent_Feature then
13074 Error_Msg_N
13075 ("subtype digits constraint is an " &
13076 "obsolescent feature (RM J.3(8))?j?", C);
13077 end if;
13079 D := Digits_Expression (C);
13080 Analyze_And_Resolve (D, Any_Integer);
13081 Check_Digits_Expression (D);
13082 Set_Digits_Value (Def_Id, Expr_Value (D));
13084 -- Check that digits value is in range. Obviously we can do this
13085 -- at compile time, but it is strictly a runtime check, and of
13086 -- course there is an ACVC test that checks this.
13088 if Digits_Value (Def_Id) > Digits_Value (T) then
13089 Error_Msg_Uint_1 := Digits_Value (T);
13090 Error_Msg_N ("??digits value is too large, maximum is ^", D);
13091 Rais :=
13092 Make_Raise_Constraint_Error (Sloc (D),
13093 Reason => CE_Range_Check_Failed);
13094 Insert_Action (Declaration_Node (Def_Id), Rais);
13095 end if;
13097 C := Range_Constraint (C);
13099 -- No digits constraint present
13101 else
13102 Set_Digits_Value (Def_Id, Digits_Value (T));
13103 end if;
13105 -- Range constraint present
13107 if Nkind (C) = N_Range_Constraint then
13108 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13110 -- No range constraint present
13112 else
13113 pragma Assert (No (C));
13114 Set_Scalar_Range (Def_Id, Scalar_Range (T));
13115 end if;
13117 Set_Is_Constrained (Def_Id);
13118 end Constrain_Float;
13120 ---------------------
13121 -- Constrain_Index --
13122 ---------------------
13124 procedure Constrain_Index
13125 (Index : Node_Id;
13126 S : Node_Id;
13127 Related_Nod : Node_Id;
13128 Related_Id : Entity_Id;
13129 Suffix : Character;
13130 Suffix_Index : Nat)
13132 Def_Id : Entity_Id;
13133 R : Node_Id := Empty;
13134 T : constant Entity_Id := Etype (Index);
13136 begin
13137 if Nkind (S) = N_Range
13138 or else
13139 (Nkind (S) = N_Attribute_Reference
13140 and then Attribute_Name (S) = Name_Range)
13141 then
13142 -- A Range attribute will be transformed into N_Range by Resolve
13144 Analyze (S);
13145 Set_Etype (S, T);
13146 R := S;
13148 Process_Range_Expr_In_Decl (R, T);
13150 if not Error_Posted (S)
13151 and then
13152 (Nkind (S) /= N_Range
13153 or else not Covers (T, (Etype (Low_Bound (S))))
13154 or else not Covers (T, (Etype (High_Bound (S)))))
13155 then
13156 if Base_Type (T) /= Any_Type
13157 and then Etype (Low_Bound (S)) /= Any_Type
13158 and then Etype (High_Bound (S)) /= Any_Type
13159 then
13160 Error_Msg_N ("range expected", S);
13161 end if;
13162 end if;
13164 elsif Nkind (S) = N_Subtype_Indication then
13166 -- The parser has verified that this is a discrete indication
13168 Resolve_Discrete_Subtype_Indication (S, T);
13169 Bad_Predicated_Subtype_Use
13170 ("subtype& has predicate, not allowed in index constraint",
13171 S, Entity (Subtype_Mark (S)));
13173 R := Range_Expression (Constraint (S));
13175 -- Capture values of bounds and generate temporaries for them if
13176 -- needed, since checks may cause duplication of the expressions
13177 -- which must not be reevaluated.
13179 -- The forced evaluation removes side effects from expressions, which
13180 -- should occur also in GNATprove mode. Otherwise, we end up with
13181 -- unexpected insertions of actions at places where this is not
13182 -- supposed to occur, e.g. on default parameters of a call.
13184 if Expander_Active or GNATprove_Mode then
13185 Force_Evaluation (Low_Bound (R));
13186 Force_Evaluation (High_Bound (R));
13187 end if;
13189 elsif Nkind (S) = N_Discriminant_Association then
13191 -- Syntactically valid in subtype indication
13193 Error_Msg_N ("invalid index constraint", S);
13194 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
13195 return;
13197 -- Subtype_Mark case, no anonymous subtypes to construct
13199 else
13200 Analyze (S);
13202 if Is_Entity_Name (S) then
13203 if not Is_Type (Entity (S)) then
13204 Error_Msg_N ("expect subtype mark for index constraint", S);
13206 elsif Base_Type (Entity (S)) /= Base_Type (T) then
13207 Wrong_Type (S, Base_Type (T));
13209 -- Check error of subtype with predicate in index constraint
13211 else
13212 Bad_Predicated_Subtype_Use
13213 ("subtype& has predicate, not allowed in index constraint",
13214 S, Entity (S));
13215 end if;
13217 return;
13219 else
13220 Error_Msg_N ("invalid index constraint", S);
13221 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
13222 return;
13223 end if;
13224 end if;
13226 Def_Id :=
13227 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
13229 Set_Etype (Def_Id, Base_Type (T));
13231 if Is_Modular_Integer_Type (T) then
13232 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
13234 elsif Is_Integer_Type (T) then
13235 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
13237 else
13238 Set_Ekind (Def_Id, E_Enumeration_Subtype);
13239 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
13240 Set_First_Literal (Def_Id, First_Literal (T));
13241 end if;
13243 Set_Size_Info (Def_Id, (T));
13244 Set_RM_Size (Def_Id, RM_Size (T));
13245 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13247 Set_Scalar_Range (Def_Id, R);
13249 Set_Etype (S, Def_Id);
13250 Set_Discrete_RM_Size (Def_Id);
13251 end Constrain_Index;
13253 -----------------------
13254 -- Constrain_Integer --
13255 -----------------------
13257 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id) is
13258 T : constant Entity_Id := Entity (Subtype_Mark (S));
13259 C : constant Node_Id := Constraint (S);
13261 begin
13262 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13264 if Is_Modular_Integer_Type (T) then
13265 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
13266 else
13267 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
13268 end if;
13270 Set_Etype (Def_Id, Base_Type (T));
13271 Set_Size_Info (Def_Id, (T));
13272 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13273 Set_Discrete_RM_Size (Def_Id);
13274 end Constrain_Integer;
13276 ------------------------------
13277 -- Constrain_Ordinary_Fixed --
13278 ------------------------------
13280 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id) is
13281 T : constant Entity_Id := Entity (Subtype_Mark (S));
13282 C : Node_Id;
13283 D : Node_Id;
13284 Rais : Node_Id;
13286 begin
13287 Set_Ekind (Def_Id, E_Ordinary_Fixed_Point_Subtype);
13288 Set_Etype (Def_Id, Base_Type (T));
13289 Set_Size_Info (Def_Id, (T));
13290 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13291 Set_Small_Value (Def_Id, Small_Value (T));
13293 -- Process the constraint
13295 C := Constraint (S);
13297 -- Delta constraint present
13299 if Nkind (C) = N_Delta_Constraint then
13301 Check_SPARK_05_Restriction ("delta constraint is not allowed", S);
13302 Check_Restriction (No_Obsolescent_Features, C);
13304 if Warn_On_Obsolescent_Feature then
13305 Error_Msg_S
13306 ("subtype delta constraint is an " &
13307 "obsolescent feature (RM J.3(7))?j?");
13308 end if;
13310 D := Delta_Expression (C);
13311 Analyze_And_Resolve (D, Any_Real);
13312 Check_Delta_Expression (D);
13313 Set_Delta_Value (Def_Id, Expr_Value_R (D));
13315 -- Check that delta value is in range. Obviously we can do this
13316 -- at compile time, but it is strictly a runtime check, and of
13317 -- course there is an ACVC test that checks this.
13319 if Delta_Value (Def_Id) < Delta_Value (T) then
13320 Error_Msg_N ("??delta value is too small", D);
13321 Rais :=
13322 Make_Raise_Constraint_Error (Sloc (D),
13323 Reason => CE_Range_Check_Failed);
13324 Insert_Action (Declaration_Node (Def_Id), Rais);
13325 end if;
13327 C := Range_Constraint (C);
13329 -- No delta constraint present
13331 else
13332 Set_Delta_Value (Def_Id, Delta_Value (T));
13333 end if;
13335 -- Range constraint present
13337 if Nkind (C) = N_Range_Constraint then
13338 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13340 -- No range constraint present
13342 else
13343 pragma Assert (No (C));
13344 Set_Scalar_Range (Def_Id, Scalar_Range (T));
13346 end if;
13348 Set_Discrete_RM_Size (Def_Id);
13350 -- Unconditionally delay the freeze, since we cannot set size
13351 -- information in all cases correctly until the freeze point.
13353 Set_Has_Delayed_Freeze (Def_Id);
13354 end Constrain_Ordinary_Fixed;
13356 -----------------------
13357 -- Contain_Interface --
13358 -----------------------
13360 function Contain_Interface
13361 (Iface : Entity_Id;
13362 Ifaces : Elist_Id) return Boolean
13364 Iface_Elmt : Elmt_Id;
13366 begin
13367 if Present (Ifaces) then
13368 Iface_Elmt := First_Elmt (Ifaces);
13369 while Present (Iface_Elmt) loop
13370 if Node (Iface_Elmt) = Iface then
13371 return True;
13372 end if;
13374 Next_Elmt (Iface_Elmt);
13375 end loop;
13376 end if;
13378 return False;
13379 end Contain_Interface;
13381 ---------------------------
13382 -- Convert_Scalar_Bounds --
13383 ---------------------------
13385 procedure Convert_Scalar_Bounds
13386 (N : Node_Id;
13387 Parent_Type : Entity_Id;
13388 Derived_Type : Entity_Id;
13389 Loc : Source_Ptr)
13391 Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
13393 Lo : Node_Id;
13394 Hi : Node_Id;
13395 Rng : Node_Id;
13397 begin
13398 -- Defend against previous errors
13400 if No (Scalar_Range (Derived_Type)) then
13401 Check_Error_Detected;
13402 return;
13403 end if;
13405 Lo := Build_Scalar_Bound
13406 (Type_Low_Bound (Derived_Type),
13407 Parent_Type, Implicit_Base);
13409 Hi := Build_Scalar_Bound
13410 (Type_High_Bound (Derived_Type),
13411 Parent_Type, Implicit_Base);
13413 Rng :=
13414 Make_Range (Loc,
13415 Low_Bound => Lo,
13416 High_Bound => Hi);
13418 Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
13420 Set_Parent (Rng, N);
13421 Set_Scalar_Range (Derived_Type, Rng);
13423 -- Analyze the bounds
13425 Analyze_And_Resolve (Lo, Implicit_Base);
13426 Analyze_And_Resolve (Hi, Implicit_Base);
13428 -- Analyze the range itself, except that we do not analyze it if
13429 -- the bounds are real literals, and we have a fixed-point type.
13430 -- The reason for this is that we delay setting the bounds in this
13431 -- case till we know the final Small and Size values (see circuit
13432 -- in Freeze.Freeze_Fixed_Point_Type for further details).
13434 if Is_Fixed_Point_Type (Parent_Type)
13435 and then Nkind (Lo) = N_Real_Literal
13436 and then Nkind (Hi) = N_Real_Literal
13437 then
13438 return;
13440 -- Here we do the analysis of the range
13442 -- Note: we do this manually, since if we do a normal Analyze and
13443 -- Resolve call, there are problems with the conversions used for
13444 -- the derived type range.
13446 else
13447 Set_Etype (Rng, Implicit_Base);
13448 Set_Analyzed (Rng, True);
13449 end if;
13450 end Convert_Scalar_Bounds;
13452 -------------------
13453 -- Copy_And_Swap --
13454 -------------------
13456 procedure Copy_And_Swap (Priv, Full : Entity_Id) is
13457 begin
13458 -- Initialize new full declaration entity by copying the pertinent
13459 -- fields of the corresponding private declaration entity.
13461 -- We temporarily set Ekind to a value appropriate for a type to
13462 -- avoid assert failures in Einfo from checking for setting type
13463 -- attributes on something that is not a type. Ekind (Priv) is an
13464 -- appropriate choice, since it allowed the attributes to be set
13465 -- in the first place. This Ekind value will be modified later.
13467 Set_Ekind (Full, Ekind (Priv));
13469 -- Also set Etype temporarily to Any_Type, again, in the absence
13470 -- of errors, it will be properly reset, and if there are errors,
13471 -- then we want a value of Any_Type to remain.
13473 Set_Etype (Full, Any_Type);
13475 -- Now start copying attributes
13477 Set_Has_Discriminants (Full, Has_Discriminants (Priv));
13479 if Has_Discriminants (Full) then
13480 Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
13481 Set_Stored_Constraint (Full, Stored_Constraint (Priv));
13482 end if;
13484 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
13485 Set_Homonym (Full, Homonym (Priv));
13486 Set_Is_Immediately_Visible (Full, Is_Immediately_Visible (Priv));
13487 Set_Is_Public (Full, Is_Public (Priv));
13488 Set_Is_Pure (Full, Is_Pure (Priv));
13489 Set_Is_Tagged_Type (Full, Is_Tagged_Type (Priv));
13490 Set_Has_Pragma_Unmodified (Full, Has_Pragma_Unmodified (Priv));
13491 Set_Has_Pragma_Unreferenced (Full, Has_Pragma_Unreferenced (Priv));
13492 Set_Has_Pragma_Unreferenced_Objects
13493 (Full, Has_Pragma_Unreferenced_Objects
13494 (Priv));
13496 Conditional_Delay (Full, Priv);
13498 if Is_Tagged_Type (Full) then
13499 Set_Direct_Primitive_Operations
13500 (Full, Direct_Primitive_Operations (Priv));
13501 Set_No_Tagged_Streams_Pragma
13502 (Full, No_Tagged_Streams_Pragma (Priv));
13504 if Is_Base_Type (Priv) then
13505 Set_Class_Wide_Type (Full, Class_Wide_Type (Priv));
13506 end if;
13507 end if;
13509 Set_Is_Volatile (Full, Is_Volatile (Priv));
13510 Set_Treat_As_Volatile (Full, Treat_As_Volatile (Priv));
13511 Set_Scope (Full, Scope (Priv));
13512 Set_Next_Entity (Full, Next_Entity (Priv));
13513 Set_First_Entity (Full, First_Entity (Priv));
13514 Set_Last_Entity (Full, Last_Entity (Priv));
13516 -- If access types have been recorded for later handling, keep them in
13517 -- the full view so that they get handled when the full view freeze
13518 -- node is expanded.
13520 if Present (Freeze_Node (Priv))
13521 and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
13522 then
13523 Ensure_Freeze_Node (Full);
13524 Set_Access_Types_To_Process
13525 (Freeze_Node (Full),
13526 Access_Types_To_Process (Freeze_Node (Priv)));
13527 end if;
13529 -- Swap the two entities. Now Private is the full type entity and Full
13530 -- is the private one. They will be swapped back at the end of the
13531 -- private part. This swapping ensures that the entity that is visible
13532 -- in the private part is the full declaration.
13534 Exchange_Entities (Priv, Full);
13535 Append_Entity (Full, Scope (Full));
13536 end Copy_And_Swap;
13538 -------------------------------------
13539 -- Copy_Array_Base_Type_Attributes --
13540 -------------------------------------
13542 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
13543 begin
13544 Set_Component_Alignment (T1, Component_Alignment (T2));
13545 Set_Component_Type (T1, Component_Type (T2));
13546 Set_Component_Size (T1, Component_Size (T2));
13547 Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
13548 Set_Has_Non_Standard_Rep (T1, Has_Non_Standard_Rep (T2));
13549 Set_Has_Protected (T1, Has_Protected (T2));
13550 Set_Has_Task (T1, Has_Task (T2));
13551 Set_Is_Packed (T1, Is_Packed (T2));
13552 Set_Has_Aliased_Components (T1, Has_Aliased_Components (T2));
13553 Set_Has_Atomic_Components (T1, Has_Atomic_Components (T2));
13554 Set_Has_Volatile_Components (T1, Has_Volatile_Components (T2));
13555 end Copy_Array_Base_Type_Attributes;
13557 -----------------------------------
13558 -- Copy_Array_Subtype_Attributes --
13559 -----------------------------------
13561 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
13562 begin
13563 Set_Size_Info (T1, T2);
13565 Set_First_Index (T1, First_Index (T2));
13566 Set_Is_Aliased (T1, Is_Aliased (T2));
13567 Set_Is_Volatile (T1, Is_Volatile (T2));
13568 Set_Treat_As_Volatile (T1, Treat_As_Volatile (T2));
13569 Set_Is_Constrained (T1, Is_Constrained (T2));
13570 Set_Depends_On_Private (T1, Has_Private_Component (T2));
13571 Inherit_Rep_Item_Chain (T1, T2);
13572 Set_Convention (T1, Convention (T2));
13573 Set_Is_Limited_Composite (T1, Is_Limited_Composite (T2));
13574 Set_Is_Private_Composite (T1, Is_Private_Composite (T2));
13575 Set_Packed_Array_Impl_Type (T1, Packed_Array_Impl_Type (T2));
13576 end Copy_Array_Subtype_Attributes;
13578 -----------------------------------
13579 -- Create_Constrained_Components --
13580 -----------------------------------
13582 procedure Create_Constrained_Components
13583 (Subt : Entity_Id;
13584 Decl_Node : Node_Id;
13585 Typ : Entity_Id;
13586 Constraints : Elist_Id)
13588 Loc : constant Source_Ptr := Sloc (Subt);
13589 Comp_List : constant Elist_Id := New_Elmt_List;
13590 Parent_Type : constant Entity_Id := Etype (Typ);
13591 Assoc_List : constant List_Id := New_List;
13592 Discr_Val : Elmt_Id;
13593 Errors : Boolean;
13594 New_C : Entity_Id;
13595 Old_C : Entity_Id;
13596 Is_Static : Boolean := True;
13598 procedure Collect_Fixed_Components (Typ : Entity_Id);
13599 -- Collect parent type components that do not appear in a variant part
13601 procedure Create_All_Components;
13602 -- Iterate over Comp_List to create the components of the subtype
13604 function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
13605 -- Creates a new component from Old_Compon, copying all the fields from
13606 -- it, including its Etype, inserts the new component in the Subt entity
13607 -- chain and returns the new component.
13609 function Is_Variant_Record (T : Entity_Id) return Boolean;
13610 -- If true, and discriminants are static, collect only components from
13611 -- variants selected by discriminant values.
13613 ------------------------------
13614 -- Collect_Fixed_Components --
13615 ------------------------------
13617 procedure Collect_Fixed_Components (Typ : Entity_Id) is
13618 begin
13619 -- Build association list for discriminants, and find components of the
13620 -- variant part selected by the values of the discriminants.
13622 Old_C := First_Discriminant (Typ);
13623 Discr_Val := First_Elmt (Constraints);
13624 while Present (Old_C) loop
13625 Append_To (Assoc_List,
13626 Make_Component_Association (Loc,
13627 Choices => New_List (New_Occurrence_Of (Old_C, Loc)),
13628 Expression => New_Copy (Node (Discr_Val))));
13630 Next_Elmt (Discr_Val);
13631 Next_Discriminant (Old_C);
13632 end loop;
13634 -- The tag and the possible parent component are unconditionally in
13635 -- the subtype.
13637 if Is_Tagged_Type (Typ) or else Has_Controlled_Component (Typ) then
13638 Old_C := First_Component (Typ);
13639 while Present (Old_C) loop
13640 if Nam_In (Chars (Old_C), Name_uTag, Name_uParent) then
13641 Append_Elmt (Old_C, Comp_List);
13642 end if;
13644 Next_Component (Old_C);
13645 end loop;
13646 end if;
13647 end Collect_Fixed_Components;
13649 ---------------------------
13650 -- Create_All_Components --
13651 ---------------------------
13653 procedure Create_All_Components is
13654 Comp : Elmt_Id;
13656 begin
13657 Comp := First_Elmt (Comp_List);
13658 while Present (Comp) loop
13659 Old_C := Node (Comp);
13660 New_C := Create_Component (Old_C);
13662 Set_Etype
13663 (New_C,
13664 Constrain_Component_Type
13665 (Old_C, Subt, Decl_Node, Typ, Constraints));
13666 Set_Is_Public (New_C, Is_Public (Subt));
13668 Next_Elmt (Comp);
13669 end loop;
13670 end Create_All_Components;
13672 ----------------------
13673 -- Create_Component --
13674 ----------------------
13676 function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
13677 New_Compon : constant Entity_Id := New_Copy (Old_Compon);
13679 begin
13680 if Ekind (Old_Compon) = E_Discriminant
13681 and then Is_Completely_Hidden (Old_Compon)
13682 then
13683 -- This is a shadow discriminant created for a discriminant of
13684 -- the parent type, which needs to be present in the subtype.
13685 -- Give the shadow discriminant an internal name that cannot
13686 -- conflict with that of visible components.
13688 Set_Chars (New_Compon, New_Internal_Name ('C'));
13689 end if;
13691 -- Set the parent so we have a proper link for freezing etc. This is
13692 -- not a real parent pointer, since of course our parent does not own
13693 -- up to us and reference us, we are an illegitimate child of the
13694 -- original parent.
13696 Set_Parent (New_Compon, Parent (Old_Compon));
13698 -- If the old component's Esize was already determined and is a
13699 -- static value, then the new component simply inherits it. Otherwise
13700 -- the old component's size may require run-time determination, but
13701 -- the new component's size still might be statically determinable
13702 -- (if, for example it has a static constraint). In that case we want
13703 -- Layout_Type to recompute the component's size, so we reset its
13704 -- size and positional fields.
13706 if Frontend_Layout_On_Target
13707 and then not Known_Static_Esize (Old_Compon)
13708 then
13709 Set_Esize (New_Compon, Uint_0);
13710 Init_Normalized_First_Bit (New_Compon);
13711 Init_Normalized_Position (New_Compon);
13712 Init_Normalized_Position_Max (New_Compon);
13713 end if;
13715 -- We do not want this node marked as Comes_From_Source, since
13716 -- otherwise it would get first class status and a separate cross-
13717 -- reference line would be generated. Illegitimate children do not
13718 -- rate such recognition.
13720 Set_Comes_From_Source (New_Compon, False);
13722 -- But it is a real entity, and a birth certificate must be properly
13723 -- registered by entering it into the entity list.
13725 Enter_Name (New_Compon);
13727 return New_Compon;
13728 end Create_Component;
13730 -----------------------
13731 -- Is_Variant_Record --
13732 -----------------------
13734 function Is_Variant_Record (T : Entity_Id) return Boolean is
13735 begin
13736 return Nkind (Parent (T)) = N_Full_Type_Declaration
13737 and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
13738 and then Present (Component_List (Type_Definition (Parent (T))))
13739 and then
13740 Present
13741 (Variant_Part (Component_List (Type_Definition (Parent (T)))));
13742 end Is_Variant_Record;
13744 -- Start of processing for Create_Constrained_Components
13746 begin
13747 pragma Assert (Subt /= Base_Type (Subt));
13748 pragma Assert (Typ = Base_Type (Typ));
13750 Set_First_Entity (Subt, Empty);
13751 Set_Last_Entity (Subt, Empty);
13753 -- Check whether constraint is fully static, in which case we can
13754 -- optimize the list of components.
13756 Discr_Val := First_Elmt (Constraints);
13757 while Present (Discr_Val) loop
13758 if not Is_OK_Static_Expression (Node (Discr_Val)) then
13759 Is_Static := False;
13760 exit;
13761 end if;
13763 Next_Elmt (Discr_Val);
13764 end loop;
13766 Set_Has_Static_Discriminants (Subt, Is_Static);
13768 Push_Scope (Subt);
13770 -- Inherit the discriminants of the parent type
13772 Add_Discriminants : declare
13773 Num_Disc : Int;
13774 Num_Gird : Int;
13776 begin
13777 Num_Disc := 0;
13778 Old_C := First_Discriminant (Typ);
13780 while Present (Old_C) loop
13781 Num_Disc := Num_Disc + 1;
13782 New_C := Create_Component (Old_C);
13783 Set_Is_Public (New_C, Is_Public (Subt));
13784 Next_Discriminant (Old_C);
13785 end loop;
13787 -- For an untagged derived subtype, the number of discriminants may
13788 -- be smaller than the number of inherited discriminants, because
13789 -- several of them may be renamed by a single new discriminant or
13790 -- constrained. In this case, add the hidden discriminants back into
13791 -- the subtype, because they need to be present if the optimizer of
13792 -- the GCC 4.x back-end decides to break apart assignments between
13793 -- objects using the parent view into member-wise assignments.
13795 Num_Gird := 0;
13797 if Is_Derived_Type (Typ)
13798 and then not Is_Tagged_Type (Typ)
13799 then
13800 Old_C := First_Stored_Discriminant (Typ);
13802 while Present (Old_C) loop
13803 Num_Gird := Num_Gird + 1;
13804 Next_Stored_Discriminant (Old_C);
13805 end loop;
13806 end if;
13808 if Num_Gird > Num_Disc then
13810 -- Find out multiple uses of new discriminants, and add hidden
13811 -- components for the extra renamed discriminants. We recognize
13812 -- multiple uses through the Corresponding_Discriminant of a
13813 -- new discriminant: if it constrains several old discriminants,
13814 -- this field points to the last one in the parent type. The
13815 -- stored discriminants of the derived type have the same name
13816 -- as those of the parent.
13818 declare
13819 Constr : Elmt_Id;
13820 New_Discr : Entity_Id;
13821 Old_Discr : Entity_Id;
13823 begin
13824 Constr := First_Elmt (Stored_Constraint (Typ));
13825 Old_Discr := First_Stored_Discriminant (Typ);
13826 while Present (Constr) loop
13827 if Is_Entity_Name (Node (Constr))
13828 and then Ekind (Entity (Node (Constr))) = E_Discriminant
13829 then
13830 New_Discr := Entity (Node (Constr));
13832 if Chars (Corresponding_Discriminant (New_Discr)) /=
13833 Chars (Old_Discr)
13834 then
13835 -- The new discriminant has been used to rename a
13836 -- subsequent old discriminant. Introduce a shadow
13837 -- component for the current old discriminant.
13839 New_C := Create_Component (Old_Discr);
13840 Set_Original_Record_Component (New_C, Old_Discr);
13841 end if;
13843 else
13844 -- The constraint has eliminated the old discriminant.
13845 -- Introduce a shadow component.
13847 New_C := Create_Component (Old_Discr);
13848 Set_Original_Record_Component (New_C, Old_Discr);
13849 end if;
13851 Next_Elmt (Constr);
13852 Next_Stored_Discriminant (Old_Discr);
13853 end loop;
13854 end;
13855 end if;
13856 end Add_Discriminants;
13858 if Is_Static
13859 and then Is_Variant_Record (Typ)
13860 then
13861 Collect_Fixed_Components (Typ);
13863 Gather_Components (
13864 Typ,
13865 Component_List (Type_Definition (Parent (Typ))),
13866 Governed_By => Assoc_List,
13867 Into => Comp_List,
13868 Report_Errors => Errors);
13869 pragma Assert (not Errors);
13871 Create_All_Components;
13873 -- If the subtype declaration is created for a tagged type derivation
13874 -- with constraints, we retrieve the record definition of the parent
13875 -- type to select the components of the proper variant.
13877 elsif Is_Static
13878 and then Is_Tagged_Type (Typ)
13879 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
13880 and then
13881 Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
13882 and then Is_Variant_Record (Parent_Type)
13883 then
13884 Collect_Fixed_Components (Typ);
13886 Gather_Components
13887 (Typ,
13888 Component_List (Type_Definition (Parent (Parent_Type))),
13889 Governed_By => Assoc_List,
13890 Into => Comp_List,
13891 Report_Errors => Errors);
13893 -- Note: previously there was a check at this point that no errors
13894 -- were detected. As a consequence of AI05-220 there may be an error
13895 -- if an inherited discriminant that controls a variant has a non-
13896 -- static constraint.
13898 -- If the tagged derivation has a type extension, collect all the
13899 -- new components therein.
13901 if Present (Record_Extension_Part (Type_Definition (Parent (Typ))))
13902 then
13903 Old_C := First_Component (Typ);
13904 while Present (Old_C) loop
13905 if Original_Record_Component (Old_C) = Old_C
13906 and then Chars (Old_C) /= Name_uTag
13907 and then Chars (Old_C) /= Name_uParent
13908 then
13909 Append_Elmt (Old_C, Comp_List);
13910 end if;
13912 Next_Component (Old_C);
13913 end loop;
13914 end if;
13916 Create_All_Components;
13918 else
13919 -- If discriminants are not static, or if this is a multi-level type
13920 -- extension, we have to include all components of the parent type.
13922 Old_C := First_Component (Typ);
13923 while Present (Old_C) loop
13924 New_C := Create_Component (Old_C);
13926 Set_Etype
13927 (New_C,
13928 Constrain_Component_Type
13929 (Old_C, Subt, Decl_Node, Typ, Constraints));
13930 Set_Is_Public (New_C, Is_Public (Subt));
13932 Next_Component (Old_C);
13933 end loop;
13934 end if;
13936 End_Scope;
13937 end Create_Constrained_Components;
13939 ------------------------------------------
13940 -- Decimal_Fixed_Point_Type_Declaration --
13941 ------------------------------------------
13943 procedure Decimal_Fixed_Point_Type_Declaration
13944 (T : Entity_Id;
13945 Def : Node_Id)
13947 Loc : constant Source_Ptr := Sloc (Def);
13948 Digs_Expr : constant Node_Id := Digits_Expression (Def);
13949 Delta_Expr : constant Node_Id := Delta_Expression (Def);
13950 Implicit_Base : Entity_Id;
13951 Digs_Val : Uint;
13952 Delta_Val : Ureal;
13953 Scale_Val : Uint;
13954 Bound_Val : Ureal;
13956 begin
13957 Check_SPARK_05_Restriction
13958 ("decimal fixed point type is not allowed", Def);
13959 Check_Restriction (No_Fixed_Point, Def);
13961 -- Create implicit base type
13963 Implicit_Base :=
13964 Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
13965 Set_Etype (Implicit_Base, Implicit_Base);
13967 -- Analyze and process delta expression
13969 Analyze_And_Resolve (Delta_Expr, Universal_Real);
13971 Check_Delta_Expression (Delta_Expr);
13972 Delta_Val := Expr_Value_R (Delta_Expr);
13974 -- Check delta is power of 10, and determine scale value from it
13976 declare
13977 Val : Ureal;
13979 begin
13980 Scale_Val := Uint_0;
13981 Val := Delta_Val;
13983 if Val < Ureal_1 then
13984 while Val < Ureal_1 loop
13985 Val := Val * Ureal_10;
13986 Scale_Val := Scale_Val + 1;
13987 end loop;
13989 if Scale_Val > 18 then
13990 Error_Msg_N ("scale exceeds maximum value of 18", Def);
13991 Scale_Val := UI_From_Int (+18);
13992 end if;
13994 else
13995 while Val > Ureal_1 loop
13996 Val := Val / Ureal_10;
13997 Scale_Val := Scale_Val - 1;
13998 end loop;
14000 if Scale_Val < -18 then
14001 Error_Msg_N ("scale is less than minimum value of -18", Def);
14002 Scale_Val := UI_From_Int (-18);
14003 end if;
14004 end if;
14006 if Val /= Ureal_1 then
14007 Error_Msg_N ("delta expression must be a power of 10", Def);
14008 Delta_Val := Ureal_10 ** (-Scale_Val);
14009 end if;
14010 end;
14012 -- Set delta, scale and small (small = delta for decimal type)
14014 Set_Delta_Value (Implicit_Base, Delta_Val);
14015 Set_Scale_Value (Implicit_Base, Scale_Val);
14016 Set_Small_Value (Implicit_Base, Delta_Val);
14018 -- Analyze and process digits expression
14020 Analyze_And_Resolve (Digs_Expr, Any_Integer);
14021 Check_Digits_Expression (Digs_Expr);
14022 Digs_Val := Expr_Value (Digs_Expr);
14024 if Digs_Val > 18 then
14025 Digs_Val := UI_From_Int (+18);
14026 Error_Msg_N ("digits value out of range, maximum is 18", Digs_Expr);
14027 end if;
14029 Set_Digits_Value (Implicit_Base, Digs_Val);
14030 Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
14032 -- Set range of base type from digits value for now. This will be
14033 -- expanded to represent the true underlying base range by Freeze.
14035 Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
14037 -- Note: We leave size as zero for now, size will be set at freeze
14038 -- time. We have to do this for ordinary fixed-point, because the size
14039 -- depends on the specified small, and we might as well do the same for
14040 -- decimal fixed-point.
14042 pragma Assert (Esize (Implicit_Base) = Uint_0);
14044 -- If there are bounds given in the declaration use them as the
14045 -- bounds of the first named subtype.
14047 if Present (Real_Range_Specification (Def)) then
14048 declare
14049 RRS : constant Node_Id := Real_Range_Specification (Def);
14050 Low : constant Node_Id := Low_Bound (RRS);
14051 High : constant Node_Id := High_Bound (RRS);
14052 Low_Val : Ureal;
14053 High_Val : Ureal;
14055 begin
14056 Analyze_And_Resolve (Low, Any_Real);
14057 Analyze_And_Resolve (High, Any_Real);
14058 Check_Real_Bound (Low);
14059 Check_Real_Bound (High);
14060 Low_Val := Expr_Value_R (Low);
14061 High_Val := Expr_Value_R (High);
14063 if Low_Val < (-Bound_Val) then
14064 Error_Msg_N
14065 ("range low bound too small for digits value", Low);
14066 Low_Val := -Bound_Val;
14067 end if;
14069 if High_Val > Bound_Val then
14070 Error_Msg_N
14071 ("range high bound too large for digits value", High);
14072 High_Val := Bound_Val;
14073 end if;
14075 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
14076 end;
14078 -- If no explicit range, use range that corresponds to given
14079 -- digits value. This will end up as the final range for the
14080 -- first subtype.
14082 else
14083 Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
14084 end if;
14086 -- Complete entity for first subtype. The inheritance of the rep item
14087 -- chain ensures that SPARK-related pragmas are not clobbered when the
14088 -- decimal fixed point type acts as a full view of a private type.
14090 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
14091 Set_Etype (T, Implicit_Base);
14092 Set_Size_Info (T, Implicit_Base);
14093 Inherit_Rep_Item_Chain (T, Implicit_Base);
14094 Set_Digits_Value (T, Digs_Val);
14095 Set_Delta_Value (T, Delta_Val);
14096 Set_Small_Value (T, Delta_Val);
14097 Set_Scale_Value (T, Scale_Val);
14098 Set_Is_Constrained (T);
14099 end Decimal_Fixed_Point_Type_Declaration;
14101 -----------------------------------
14102 -- Derive_Progenitor_Subprograms --
14103 -----------------------------------
14105 procedure Derive_Progenitor_Subprograms
14106 (Parent_Type : Entity_Id;
14107 Tagged_Type : Entity_Id)
14109 E : Entity_Id;
14110 Elmt : Elmt_Id;
14111 Iface : Entity_Id;
14112 Iface_Elmt : Elmt_Id;
14113 Iface_Subp : Entity_Id;
14114 New_Subp : Entity_Id := Empty;
14115 Prim_Elmt : Elmt_Id;
14116 Subp : Entity_Id;
14117 Typ : Entity_Id;
14119 begin
14120 pragma Assert (Ada_Version >= Ada_2005
14121 and then Is_Record_Type (Tagged_Type)
14122 and then Is_Tagged_Type (Tagged_Type)
14123 and then Has_Interfaces (Tagged_Type));
14125 -- Step 1: Transfer to the full-view primitives associated with the
14126 -- partial-view that cover interface primitives. Conceptually this
14127 -- work should be done later by Process_Full_View; done here to
14128 -- simplify its implementation at later stages. It can be safely
14129 -- done here because interfaces must be visible in the partial and
14130 -- private view (RM 7.3(7.3/2)).
14132 -- Small optimization: This work is only required if the parent may
14133 -- have entities whose Alias attribute reference an interface primitive.
14134 -- Such a situation may occur if the parent is an abstract type and the
14135 -- primitive has not been yet overridden or if the parent is a generic
14136 -- formal type covering interfaces.
14138 -- If the tagged type is not abstract, it cannot have abstract
14139 -- primitives (the only entities in the list of primitives of
14140 -- non-abstract tagged types that can reference abstract primitives
14141 -- through its Alias attribute are the internal entities that have
14142 -- attribute Interface_Alias, and these entities are generated later
14143 -- by Add_Internal_Interface_Entities).
14145 if In_Private_Part (Current_Scope)
14146 and then (Is_Abstract_Type (Parent_Type)
14147 or else
14148 Is_Generic_Type (Parent_Type))
14149 then
14150 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
14151 while Present (Elmt) loop
14152 Subp := Node (Elmt);
14154 -- At this stage it is not possible to have entities in the list
14155 -- of primitives that have attribute Interface_Alias.
14157 pragma Assert (No (Interface_Alias (Subp)));
14159 Typ := Find_Dispatching_Type (Ultimate_Alias (Subp));
14161 if Is_Interface (Typ) then
14162 E := Find_Primitive_Covering_Interface
14163 (Tagged_Type => Tagged_Type,
14164 Iface_Prim => Subp);
14166 if Present (E)
14167 and then Find_Dispatching_Type (Ultimate_Alias (E)) /= Typ
14168 then
14169 Replace_Elmt (Elmt, E);
14170 Remove_Homonym (Subp);
14171 end if;
14172 end if;
14174 Next_Elmt (Elmt);
14175 end loop;
14176 end if;
14178 -- Step 2: Add primitives of progenitors that are not implemented by
14179 -- parents of Tagged_Type.
14181 if Present (Interfaces (Base_Type (Tagged_Type))) then
14182 Iface_Elmt := First_Elmt (Interfaces (Base_Type (Tagged_Type)));
14183 while Present (Iface_Elmt) loop
14184 Iface := Node (Iface_Elmt);
14186 Prim_Elmt := First_Elmt (Primitive_Operations (Iface));
14187 while Present (Prim_Elmt) loop
14188 Iface_Subp := Node (Prim_Elmt);
14190 -- Exclude derivation of predefined primitives except those
14191 -- that come from source, or are inherited from one that comes
14192 -- from source. Required to catch declarations of equality
14193 -- operators of interfaces. For example:
14195 -- type Iface is interface;
14196 -- function "=" (Left, Right : Iface) return Boolean;
14198 if not Is_Predefined_Dispatching_Operation (Iface_Subp)
14199 or else Comes_From_Source (Ultimate_Alias (Iface_Subp))
14200 then
14201 E := Find_Primitive_Covering_Interface
14202 (Tagged_Type => Tagged_Type,
14203 Iface_Prim => Iface_Subp);
14205 -- If not found we derive a new primitive leaving its alias
14206 -- attribute referencing the interface primitive.
14208 if No (E) then
14209 Derive_Subprogram
14210 (New_Subp, Iface_Subp, Tagged_Type, Iface);
14212 -- Ada 2012 (AI05-0197): If the covering primitive's name
14213 -- differs from the name of the interface primitive then it
14214 -- is a private primitive inherited from a parent type. In
14215 -- such case, given that Tagged_Type covers the interface,
14216 -- the inherited private primitive becomes visible. For such
14217 -- purpose we add a new entity that renames the inherited
14218 -- private primitive.
14220 elsif Chars (E) /= Chars (Iface_Subp) then
14221 pragma Assert (Has_Suffix (E, 'P'));
14222 Derive_Subprogram
14223 (New_Subp, Iface_Subp, Tagged_Type, Iface);
14224 Set_Alias (New_Subp, E);
14225 Set_Is_Abstract_Subprogram (New_Subp,
14226 Is_Abstract_Subprogram (E));
14228 -- Propagate to the full view interface entities associated
14229 -- with the partial view.
14231 elsif In_Private_Part (Current_Scope)
14232 and then Present (Alias (E))
14233 and then Alias (E) = Iface_Subp
14234 and then
14235 List_Containing (Parent (E)) /=
14236 Private_Declarations
14237 (Specification
14238 (Unit_Declaration_Node (Current_Scope)))
14239 then
14240 Append_Elmt (E, Primitive_Operations (Tagged_Type));
14241 end if;
14242 end if;
14244 Next_Elmt (Prim_Elmt);
14245 end loop;
14247 Next_Elmt (Iface_Elmt);
14248 end loop;
14249 end if;
14250 end Derive_Progenitor_Subprograms;
14252 -----------------------
14253 -- Derive_Subprogram --
14254 -----------------------
14256 procedure Derive_Subprogram
14257 (New_Subp : in out Entity_Id;
14258 Parent_Subp : Entity_Id;
14259 Derived_Type : Entity_Id;
14260 Parent_Type : Entity_Id;
14261 Actual_Subp : Entity_Id := Empty)
14263 Formal : Entity_Id;
14264 -- Formal parameter of parent primitive operation
14266 Formal_Of_Actual : Entity_Id;
14267 -- Formal parameter of actual operation, when the derivation is to
14268 -- create a renaming for a primitive operation of an actual in an
14269 -- instantiation.
14271 New_Formal : Entity_Id;
14272 -- Formal of inherited operation
14274 Visible_Subp : Entity_Id := Parent_Subp;
14276 function Is_Private_Overriding return Boolean;
14277 -- If Subp is a private overriding of a visible operation, the inherited
14278 -- operation derives from the overridden op (even though its body is the
14279 -- overriding one) and the inherited operation is visible now. See
14280 -- sem_disp to see the full details of the handling of the overridden
14281 -- subprogram, which is removed from the list of primitive operations of
14282 -- the type. The overridden subprogram is saved locally in Visible_Subp,
14283 -- and used to diagnose abstract operations that need overriding in the
14284 -- derived type.
14286 procedure Replace_Type (Id, New_Id : Entity_Id);
14287 -- When the type is an anonymous access type, create a new access type
14288 -- designating the derived type.
14290 procedure Set_Derived_Name;
14291 -- This procedure sets the appropriate Chars name for New_Subp. This
14292 -- is normally just a copy of the parent name. An exception arises for
14293 -- type support subprograms, where the name is changed to reflect the
14294 -- name of the derived type, e.g. if type foo is derived from type bar,
14295 -- then a procedure barDA is derived with a name fooDA.
14297 ---------------------------
14298 -- Is_Private_Overriding --
14299 ---------------------------
14301 function Is_Private_Overriding return Boolean is
14302 Prev : Entity_Id;
14304 begin
14305 -- If the parent is not a dispatching operation there is no
14306 -- need to investigate overridings
14308 if not Is_Dispatching_Operation (Parent_Subp) then
14309 return False;
14310 end if;
14312 -- The visible operation that is overridden is a homonym of the
14313 -- parent subprogram. We scan the homonym chain to find the one
14314 -- whose alias is the subprogram we are deriving.
14316 Prev := Current_Entity (Parent_Subp);
14317 while Present (Prev) loop
14318 if Ekind (Prev) = Ekind (Parent_Subp)
14319 and then Alias (Prev) = Parent_Subp
14320 and then Scope (Parent_Subp) = Scope (Prev)
14321 and then not Is_Hidden (Prev)
14322 then
14323 Visible_Subp := Prev;
14324 return True;
14325 end if;
14327 Prev := Homonym (Prev);
14328 end loop;
14330 return False;
14331 end Is_Private_Overriding;
14333 ------------------
14334 -- Replace_Type --
14335 ------------------
14337 procedure Replace_Type (Id, New_Id : Entity_Id) is
14338 Id_Type : constant Entity_Id := Etype (Id);
14339 Acc_Type : Entity_Id;
14340 Par : constant Node_Id := Parent (Derived_Type);
14342 begin
14343 -- When the type is an anonymous access type, create a new access
14344 -- type designating the derived type. This itype must be elaborated
14345 -- at the point of the derivation, not on subsequent calls that may
14346 -- be out of the proper scope for Gigi, so we insert a reference to
14347 -- it after the derivation.
14349 if Ekind (Id_Type) = E_Anonymous_Access_Type then
14350 declare
14351 Desig_Typ : Entity_Id := Designated_Type (Id_Type);
14353 begin
14354 if Ekind (Desig_Typ) = E_Record_Type_With_Private
14355 and then Present (Full_View (Desig_Typ))
14356 and then not Is_Private_Type (Parent_Type)
14357 then
14358 Desig_Typ := Full_View (Desig_Typ);
14359 end if;
14361 if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
14363 -- Ada 2005 (AI-251): Handle also derivations of abstract
14364 -- interface primitives.
14366 or else (Is_Interface (Desig_Typ)
14367 and then not Is_Class_Wide_Type (Desig_Typ))
14368 then
14369 Acc_Type := New_Copy (Id_Type);
14370 Set_Etype (Acc_Type, Acc_Type);
14371 Set_Scope (Acc_Type, New_Subp);
14373 -- Set size of anonymous access type. If we have an access
14374 -- to an unconstrained array, this is a fat pointer, so it
14375 -- is sizes at twice addtress size.
14377 if Is_Array_Type (Desig_Typ)
14378 and then not Is_Constrained (Desig_Typ)
14379 then
14380 Init_Size (Acc_Type, 2 * System_Address_Size);
14382 -- Other cases use a thin pointer
14384 else
14385 Init_Size (Acc_Type, System_Address_Size);
14386 end if;
14388 -- Set remaining characterstics of anonymous access type
14390 Init_Alignment (Acc_Type);
14391 Set_Directly_Designated_Type (Acc_Type, Derived_Type);
14393 Set_Etype (New_Id, Acc_Type);
14394 Set_Scope (New_Id, New_Subp);
14396 -- Create a reference to it
14398 Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
14400 else
14401 Set_Etype (New_Id, Id_Type);
14402 end if;
14403 end;
14405 -- In Ada2012, a formal may have an incomplete type but the type
14406 -- derivation that inherits the primitive follows the full view.
14408 elsif Base_Type (Id_Type) = Base_Type (Parent_Type)
14409 or else
14410 (Ekind (Id_Type) = E_Record_Type_With_Private
14411 and then Present (Full_View (Id_Type))
14412 and then
14413 Base_Type (Full_View (Id_Type)) = Base_Type (Parent_Type))
14414 or else
14415 (Ada_Version >= Ada_2012
14416 and then Ekind (Id_Type) = E_Incomplete_Type
14417 and then Full_View (Id_Type) = Parent_Type)
14418 then
14419 -- Constraint checks on formals are generated during expansion,
14420 -- based on the signature of the original subprogram. The bounds
14421 -- of the derived type are not relevant, and thus we can use
14422 -- the base type for the formals. However, the return type may be
14423 -- used in a context that requires that the proper static bounds
14424 -- be used (a case statement, for example) and for those cases
14425 -- we must use the derived type (first subtype), not its base.
14427 -- If the derived_type_definition has no constraints, we know that
14428 -- the derived type has the same constraints as the first subtype
14429 -- of the parent, and we can also use it rather than its base,
14430 -- which can lead to more efficient code.
14432 if Etype (Id) = Parent_Type then
14433 if Is_Scalar_Type (Parent_Type)
14434 and then
14435 Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
14436 then
14437 Set_Etype (New_Id, Derived_Type);
14439 elsif Nkind (Par) = N_Full_Type_Declaration
14440 and then
14441 Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
14442 and then
14443 Is_Entity_Name
14444 (Subtype_Indication (Type_Definition (Par)))
14445 then
14446 Set_Etype (New_Id, Derived_Type);
14448 else
14449 Set_Etype (New_Id, Base_Type (Derived_Type));
14450 end if;
14452 else
14453 Set_Etype (New_Id, Base_Type (Derived_Type));
14454 end if;
14456 else
14457 Set_Etype (New_Id, Etype (Id));
14458 end if;
14459 end Replace_Type;
14461 ----------------------
14462 -- Set_Derived_Name --
14463 ----------------------
14465 procedure Set_Derived_Name is
14466 Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
14467 begin
14468 if Nm = TSS_Null then
14469 Set_Chars (New_Subp, Chars (Parent_Subp));
14470 else
14471 Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
14472 end if;
14473 end Set_Derived_Name;
14475 -- Start of processing for Derive_Subprogram
14477 begin
14478 New_Subp := New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
14479 Set_Ekind (New_Subp, Ekind (Parent_Subp));
14480 Set_Contract (New_Subp, Make_Contract (Sloc (New_Subp)));
14482 -- Check whether the inherited subprogram is a private operation that
14483 -- should be inherited but not yet made visible. Such subprograms can
14484 -- become visible at a later point (e.g., the private part of a public
14485 -- child unit) via Declare_Inherited_Private_Subprograms. If the
14486 -- following predicate is true, then this is not such a private
14487 -- operation and the subprogram simply inherits the name of the parent
14488 -- subprogram. Note the special check for the names of controlled
14489 -- operations, which are currently exempted from being inherited with
14490 -- a hidden name because they must be findable for generation of
14491 -- implicit run-time calls.
14493 if not Is_Hidden (Parent_Subp)
14494 or else Is_Internal (Parent_Subp)
14495 or else Is_Private_Overriding
14496 or else Is_Internal_Name (Chars (Parent_Subp))
14497 or else Nam_In (Chars (Parent_Subp), Name_Initialize,
14498 Name_Adjust,
14499 Name_Finalize)
14500 then
14501 Set_Derived_Name;
14503 -- An inherited dispatching equality will be overridden by an internally
14504 -- generated one, or by an explicit one, so preserve its name and thus
14505 -- its entry in the dispatch table. Otherwise, if Parent_Subp is a
14506 -- private operation it may become invisible if the full view has
14507 -- progenitors, and the dispatch table will be malformed.
14508 -- We check that the type is limited to handle the anomalous declaration
14509 -- of Limited_Controlled, which is derived from a non-limited type, and
14510 -- which is handled specially elsewhere as well.
14512 elsif Chars (Parent_Subp) = Name_Op_Eq
14513 and then Is_Dispatching_Operation (Parent_Subp)
14514 and then Etype (Parent_Subp) = Standard_Boolean
14515 and then not Is_Limited_Type (Etype (First_Formal (Parent_Subp)))
14516 and then
14517 Etype (First_Formal (Parent_Subp)) =
14518 Etype (Next_Formal (First_Formal (Parent_Subp)))
14519 then
14520 Set_Derived_Name;
14522 -- If parent is hidden, this can be a regular derivation if the
14523 -- parent is immediately visible in a non-instantiating context,
14524 -- or if we are in the private part of an instance. This test
14525 -- should still be refined ???
14527 -- The test for In_Instance_Not_Visible avoids inheriting the derived
14528 -- operation as a non-visible operation in cases where the parent
14529 -- subprogram might not be visible now, but was visible within the
14530 -- original generic, so it would be wrong to make the inherited
14531 -- subprogram non-visible now. (Not clear if this test is fully
14532 -- correct; are there any cases where we should declare the inherited
14533 -- operation as not visible to avoid it being overridden, e.g., when
14534 -- the parent type is a generic actual with private primitives ???)
14536 -- (they should be treated the same as other private inherited
14537 -- subprograms, but it's not clear how to do this cleanly). ???
14539 elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
14540 and then Is_Immediately_Visible (Parent_Subp)
14541 and then not In_Instance)
14542 or else In_Instance_Not_Visible
14543 then
14544 Set_Derived_Name;
14546 -- Ada 2005 (AI-251): Regular derivation if the parent subprogram
14547 -- overrides an interface primitive because interface primitives
14548 -- must be visible in the partial view of the parent (RM 7.3 (7.3/2))
14550 elsif Ada_Version >= Ada_2005
14551 and then Is_Dispatching_Operation (Parent_Subp)
14552 and then Covers_Some_Interface (Parent_Subp)
14553 then
14554 Set_Derived_Name;
14556 -- Otherwise, the type is inheriting a private operation, so enter
14557 -- it with a special name so it can't be overridden.
14559 else
14560 Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
14561 end if;
14563 Set_Parent (New_Subp, Parent (Derived_Type));
14565 if Present (Actual_Subp) then
14566 Replace_Type (Actual_Subp, New_Subp);
14567 else
14568 Replace_Type (Parent_Subp, New_Subp);
14569 end if;
14571 Conditional_Delay (New_Subp, Parent_Subp);
14573 -- If we are creating a renaming for a primitive operation of an
14574 -- actual of a generic derived type, we must examine the signature
14575 -- of the actual primitive, not that of the generic formal, which for
14576 -- example may be an interface. However the name and initial value
14577 -- of the inherited operation are those of the formal primitive.
14579 Formal := First_Formal (Parent_Subp);
14581 if Present (Actual_Subp) then
14582 Formal_Of_Actual := First_Formal (Actual_Subp);
14583 else
14584 Formal_Of_Actual := Empty;
14585 end if;
14587 while Present (Formal) loop
14588 New_Formal := New_Copy (Formal);
14590 -- Normally we do not go copying parents, but in the case of
14591 -- formals, we need to link up to the declaration (which is the
14592 -- parameter specification), and it is fine to link up to the
14593 -- original formal's parameter specification in this case.
14595 Set_Parent (New_Formal, Parent (Formal));
14596 Append_Entity (New_Formal, New_Subp);
14598 if Present (Formal_Of_Actual) then
14599 Replace_Type (Formal_Of_Actual, New_Formal);
14600 Next_Formal (Formal_Of_Actual);
14601 else
14602 Replace_Type (Formal, New_Formal);
14603 end if;
14605 Next_Formal (Formal);
14606 end loop;
14608 -- If this derivation corresponds to a tagged generic actual, then
14609 -- primitive operations rename those of the actual. Otherwise the
14610 -- primitive operations rename those of the parent type, If the parent
14611 -- renames an intrinsic operator, so does the new subprogram. We except
14612 -- concatenation, which is always properly typed, and does not get
14613 -- expanded as other intrinsic operations.
14615 if No (Actual_Subp) then
14616 if Is_Intrinsic_Subprogram (Parent_Subp) then
14617 Set_Is_Intrinsic_Subprogram (New_Subp);
14619 if Present (Alias (Parent_Subp))
14620 and then Chars (Parent_Subp) /= Name_Op_Concat
14621 then
14622 Set_Alias (New_Subp, Alias (Parent_Subp));
14623 else
14624 Set_Alias (New_Subp, Parent_Subp);
14625 end if;
14627 else
14628 Set_Alias (New_Subp, Parent_Subp);
14629 end if;
14631 else
14632 Set_Alias (New_Subp, Actual_Subp);
14633 end if;
14635 -- Inherit the "ghostness" from the parent subprogram
14637 if Is_Ghost_Entity (Alias (New_Subp)) then
14638 Set_Is_Ghost_Entity (New_Subp);
14639 end if;
14641 -- Derived subprograms of a tagged type must inherit the convention
14642 -- of the parent subprogram (a requirement of AI-117). Derived
14643 -- subprograms of untagged types simply get convention Ada by default.
14645 -- If the derived type is a tagged generic formal type with unknown
14646 -- discriminants, its convention is intrinsic (RM 6.3.1 (8)).
14648 -- However, if the type is derived from a generic formal, the further
14649 -- inherited subprogram has the convention of the non-generic ancestor.
14650 -- Otherwise there would be no way to override the operation.
14651 -- (This is subject to forthcoming ARG discussions).
14653 if Is_Tagged_Type (Derived_Type) then
14654 if Is_Generic_Type (Derived_Type)
14655 and then Has_Unknown_Discriminants (Derived_Type)
14656 then
14657 Set_Convention (New_Subp, Convention_Intrinsic);
14659 else
14660 if Is_Generic_Type (Parent_Type)
14661 and then Has_Unknown_Discriminants (Parent_Type)
14662 then
14663 Set_Convention (New_Subp, Convention (Alias (Parent_Subp)));
14664 else
14665 Set_Convention (New_Subp, Convention (Parent_Subp));
14666 end if;
14667 end if;
14668 end if;
14670 -- Predefined controlled operations retain their name even if the parent
14671 -- is hidden (see above), but they are not primitive operations if the
14672 -- ancestor is not visible, for example if the parent is a private
14673 -- extension completed with a controlled extension. Note that a full
14674 -- type that is controlled can break privacy: the flag Is_Controlled is
14675 -- set on both views of the type.
14677 if Is_Controlled (Parent_Type)
14678 and then Nam_In (Chars (Parent_Subp), Name_Initialize,
14679 Name_Adjust,
14680 Name_Finalize)
14681 and then Is_Hidden (Parent_Subp)
14682 and then not Is_Visibly_Controlled (Parent_Type)
14683 then
14684 Set_Is_Hidden (New_Subp);
14685 end if;
14687 Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
14688 Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
14690 if Ekind (Parent_Subp) = E_Procedure then
14691 Set_Is_Valued_Procedure
14692 (New_Subp, Is_Valued_Procedure (Parent_Subp));
14693 else
14694 Set_Has_Controlling_Result
14695 (New_Subp, Has_Controlling_Result (Parent_Subp));
14696 end if;
14698 -- No_Return must be inherited properly. If this is overridden in the
14699 -- case of a dispatching operation, then a check is made in Sem_Disp
14700 -- that the overriding operation is also No_Return (no such check is
14701 -- required for the case of non-dispatching operation.
14703 Set_No_Return (New_Subp, No_Return (Parent_Subp));
14705 -- A derived function with a controlling result is abstract. If the
14706 -- Derived_Type is a nonabstract formal generic derived type, then
14707 -- inherited operations are not abstract: the required check is done at
14708 -- instantiation time. If the derivation is for a generic actual, the
14709 -- function is not abstract unless the actual is.
14711 if Is_Generic_Type (Derived_Type)
14712 and then not Is_Abstract_Type (Derived_Type)
14713 then
14714 null;
14716 -- Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
14717 -- properties of the subprogram, as defined in RM-3.9.3(4/2-6/2).
14719 -- A subprogram subject to pragma Extensions_Visible with value False
14720 -- requires overriding if the subprogram has at least one controlling
14721 -- OUT parameter (SPARK RM 6.1.7(6)).
14723 elsif Ada_Version >= Ada_2005
14724 and then (Is_Abstract_Subprogram (Alias (New_Subp))
14725 or else (Is_Tagged_Type (Derived_Type)
14726 and then Etype (New_Subp) = Derived_Type
14727 and then not Is_Null_Extension (Derived_Type))
14728 or else (Is_Tagged_Type (Derived_Type)
14729 and then Ekind (Etype (New_Subp)) =
14730 E_Anonymous_Access_Type
14731 and then Designated_Type (Etype (New_Subp)) =
14732 Derived_Type
14733 and then not Is_Null_Extension (Derived_Type))
14734 or else (Comes_From_Source (Alias (New_Subp))
14735 and then Is_EVF_Procedure (Alias (New_Subp))))
14736 and then No (Actual_Subp)
14737 then
14738 if not Is_Tagged_Type (Derived_Type)
14739 or else Is_Abstract_Type (Derived_Type)
14740 or else Is_Abstract_Subprogram (Alias (New_Subp))
14741 then
14742 Set_Is_Abstract_Subprogram (New_Subp);
14743 else
14744 Set_Requires_Overriding (New_Subp);
14745 end if;
14747 elsif Ada_Version < Ada_2005
14748 and then (Is_Abstract_Subprogram (Alias (New_Subp))
14749 or else (Is_Tagged_Type (Derived_Type)
14750 and then Etype (New_Subp) = Derived_Type
14751 and then No (Actual_Subp)))
14752 then
14753 Set_Is_Abstract_Subprogram (New_Subp);
14755 -- AI05-0097 : an inherited operation that dispatches on result is
14756 -- abstract if the derived type is abstract, even if the parent type
14757 -- is concrete and the derived type is a null extension.
14759 elsif Has_Controlling_Result (Alias (New_Subp))
14760 and then Is_Abstract_Type (Etype (New_Subp))
14761 then
14762 Set_Is_Abstract_Subprogram (New_Subp);
14764 -- Finally, if the parent type is abstract we must verify that all
14765 -- inherited operations are either non-abstract or overridden, or that
14766 -- the derived type itself is abstract (this check is performed at the
14767 -- end of a package declaration, in Check_Abstract_Overriding). A
14768 -- private overriding in the parent type will not be visible in the
14769 -- derivation if we are not in an inner package or in a child unit of
14770 -- the parent type, in which case the abstractness of the inherited
14771 -- operation is carried to the new subprogram.
14773 elsif Is_Abstract_Type (Parent_Type)
14774 and then not In_Open_Scopes (Scope (Parent_Type))
14775 and then Is_Private_Overriding
14776 and then Is_Abstract_Subprogram (Visible_Subp)
14777 then
14778 if No (Actual_Subp) then
14779 Set_Alias (New_Subp, Visible_Subp);
14780 Set_Is_Abstract_Subprogram (New_Subp, True);
14782 else
14783 -- If this is a derivation for an instance of a formal derived
14784 -- type, abstractness comes from the primitive operation of the
14785 -- actual, not from the operation inherited from the ancestor.
14787 Set_Is_Abstract_Subprogram
14788 (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
14789 end if;
14790 end if;
14792 New_Overloaded_Entity (New_Subp, Derived_Type);
14794 -- Check for case of a derived subprogram for the instantiation of a
14795 -- formal derived tagged type, if so mark the subprogram as dispatching
14796 -- and inherit the dispatching attributes of the actual subprogram. The
14797 -- derived subprogram is effectively renaming of the actual subprogram,
14798 -- so it needs to have the same attributes as the actual.
14800 if Present (Actual_Subp)
14801 and then Is_Dispatching_Operation (Actual_Subp)
14802 then
14803 Set_Is_Dispatching_Operation (New_Subp);
14805 if Present (DTC_Entity (Actual_Subp)) then
14806 Set_DTC_Entity (New_Subp, DTC_Entity (Actual_Subp));
14807 Set_DT_Position (New_Subp, DT_Position (Actual_Subp));
14808 end if;
14809 end if;
14811 -- Indicate that a derived subprogram does not require a body and that
14812 -- it does not require processing of default expressions.
14814 Set_Has_Completion (New_Subp);
14815 Set_Default_Expressions_Processed (New_Subp);
14817 if Ekind (New_Subp) = E_Function then
14818 Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
14819 end if;
14820 end Derive_Subprogram;
14822 ------------------------
14823 -- Derive_Subprograms --
14824 ------------------------
14826 procedure Derive_Subprograms
14827 (Parent_Type : Entity_Id;
14828 Derived_Type : Entity_Id;
14829 Generic_Actual : Entity_Id := Empty)
14831 Op_List : constant Elist_Id :=
14832 Collect_Primitive_Operations (Parent_Type);
14834 function Check_Derived_Type return Boolean;
14835 -- Check that all the entities derived from Parent_Type are found in
14836 -- the list of primitives of Derived_Type exactly in the same order.
14838 procedure Derive_Interface_Subprogram
14839 (New_Subp : in out Entity_Id;
14840 Subp : Entity_Id;
14841 Actual_Subp : Entity_Id);
14842 -- Derive New_Subp from the ultimate alias of the parent subprogram Subp
14843 -- (which is an interface primitive). If Generic_Actual is present then
14844 -- Actual_Subp is the actual subprogram corresponding with the generic
14845 -- subprogram Subp.
14847 function Check_Derived_Type return Boolean is
14848 E : Entity_Id;
14849 Elmt : Elmt_Id;
14850 List : Elist_Id;
14851 New_Subp : Entity_Id;
14852 Op_Elmt : Elmt_Id;
14853 Subp : Entity_Id;
14855 begin
14856 -- Traverse list of entities in the current scope searching for
14857 -- an incomplete type whose full-view is derived type
14859 E := First_Entity (Scope (Derived_Type));
14860 while Present (E) and then E /= Derived_Type loop
14861 if Ekind (E) = E_Incomplete_Type
14862 and then Present (Full_View (E))
14863 and then Full_View (E) = Derived_Type
14864 then
14865 -- Disable this test if Derived_Type completes an incomplete
14866 -- type because in such case more primitives can be added
14867 -- later to the list of primitives of Derived_Type by routine
14868 -- Process_Incomplete_Dependents
14870 return True;
14871 end if;
14873 E := Next_Entity (E);
14874 end loop;
14876 List := Collect_Primitive_Operations (Derived_Type);
14877 Elmt := First_Elmt (List);
14879 Op_Elmt := First_Elmt (Op_List);
14880 while Present (Op_Elmt) loop
14881 Subp := Node (Op_Elmt);
14882 New_Subp := Node (Elmt);
14884 -- At this early stage Derived_Type has no entities with attribute
14885 -- Interface_Alias. In addition, such primitives are always
14886 -- located at the end of the list of primitives of Parent_Type.
14887 -- Therefore, if found we can safely stop processing pending
14888 -- entities.
14890 exit when Present (Interface_Alias (Subp));
14892 -- Handle hidden entities
14894 if not Is_Predefined_Dispatching_Operation (Subp)
14895 and then Is_Hidden (Subp)
14896 then
14897 if Present (New_Subp)
14898 and then Primitive_Names_Match (Subp, New_Subp)
14899 then
14900 Next_Elmt (Elmt);
14901 end if;
14903 else
14904 if not Present (New_Subp)
14905 or else Ekind (Subp) /= Ekind (New_Subp)
14906 or else not Primitive_Names_Match (Subp, New_Subp)
14907 then
14908 return False;
14909 end if;
14911 Next_Elmt (Elmt);
14912 end if;
14914 Next_Elmt (Op_Elmt);
14915 end loop;
14917 return True;
14918 end Check_Derived_Type;
14920 ---------------------------------
14921 -- Derive_Interface_Subprogram --
14922 ---------------------------------
14924 procedure Derive_Interface_Subprogram
14925 (New_Subp : in out Entity_Id;
14926 Subp : Entity_Id;
14927 Actual_Subp : Entity_Id)
14929 Iface_Subp : constant Entity_Id := Ultimate_Alias (Subp);
14930 Iface_Type : constant Entity_Id := Find_Dispatching_Type (Iface_Subp);
14932 begin
14933 pragma Assert (Is_Interface (Iface_Type));
14935 Derive_Subprogram
14936 (New_Subp => New_Subp,
14937 Parent_Subp => Iface_Subp,
14938 Derived_Type => Derived_Type,
14939 Parent_Type => Iface_Type,
14940 Actual_Subp => Actual_Subp);
14942 -- Given that this new interface entity corresponds with a primitive
14943 -- of the parent that was not overridden we must leave it associated
14944 -- with its parent primitive to ensure that it will share the same
14945 -- dispatch table slot when overridden.
14947 if No (Actual_Subp) then
14948 Set_Alias (New_Subp, Subp);
14950 -- For instantiations this is not needed since the previous call to
14951 -- Derive_Subprogram leaves the entity well decorated.
14953 else
14954 pragma Assert (Alias (New_Subp) = Actual_Subp);
14955 null;
14956 end if;
14957 end Derive_Interface_Subprogram;
14959 -- Local variables
14961 Alias_Subp : Entity_Id;
14962 Act_List : Elist_Id;
14963 Act_Elmt : Elmt_Id;
14964 Act_Subp : Entity_Id := Empty;
14965 Elmt : Elmt_Id;
14966 Need_Search : Boolean := False;
14967 New_Subp : Entity_Id := Empty;
14968 Parent_Base : Entity_Id;
14969 Subp : Entity_Id;
14971 -- Start of processing for Derive_Subprograms
14973 begin
14974 if Ekind (Parent_Type) = E_Record_Type_With_Private
14975 and then Has_Discriminants (Parent_Type)
14976 and then Present (Full_View (Parent_Type))
14977 then
14978 Parent_Base := Full_View (Parent_Type);
14979 else
14980 Parent_Base := Parent_Type;
14981 end if;
14983 if Present (Generic_Actual) then
14984 Act_List := Collect_Primitive_Operations (Generic_Actual);
14985 Act_Elmt := First_Elmt (Act_List);
14986 else
14987 Act_List := No_Elist;
14988 Act_Elmt := No_Elmt;
14989 end if;
14991 -- Derive primitives inherited from the parent. Note that if the generic
14992 -- actual is present, this is not really a type derivation, it is a
14993 -- completion within an instance.
14995 -- Case 1: Derived_Type does not implement interfaces
14997 if not Is_Tagged_Type (Derived_Type)
14998 or else (not Has_Interfaces (Derived_Type)
14999 and then not (Present (Generic_Actual)
15000 and then Has_Interfaces (Generic_Actual)))
15001 then
15002 Elmt := First_Elmt (Op_List);
15003 while Present (Elmt) loop
15004 Subp := Node (Elmt);
15006 -- Literals are derived earlier in the process of building the
15007 -- derived type, and are skipped here.
15009 if Ekind (Subp) = E_Enumeration_Literal then
15010 null;
15012 -- The actual is a direct descendant and the common primitive
15013 -- operations appear in the same order.
15015 -- If the generic parent type is present, the derived type is an
15016 -- instance of a formal derived type, and within the instance its
15017 -- operations are those of the actual. We derive from the formal
15018 -- type but make the inherited operations aliases of the
15019 -- corresponding operations of the actual.
15021 else
15022 pragma Assert (No (Node (Act_Elmt))
15023 or else (Primitive_Names_Match (Subp, Node (Act_Elmt))
15024 and then
15025 Type_Conformant
15026 (Subp, Node (Act_Elmt),
15027 Skip_Controlling_Formals => True)));
15029 Derive_Subprogram
15030 (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
15032 if Present (Act_Elmt) then
15033 Next_Elmt (Act_Elmt);
15034 end if;
15035 end if;
15037 Next_Elmt (Elmt);
15038 end loop;
15040 -- Case 2: Derived_Type implements interfaces
15042 else
15043 -- If the parent type has no predefined primitives we remove
15044 -- predefined primitives from the list of primitives of generic
15045 -- actual to simplify the complexity of this algorithm.
15047 if Present (Generic_Actual) then
15048 declare
15049 Has_Predefined_Primitives : Boolean := False;
15051 begin
15052 -- Check if the parent type has predefined primitives
15054 Elmt := First_Elmt (Op_List);
15055 while Present (Elmt) loop
15056 Subp := Node (Elmt);
15058 if Is_Predefined_Dispatching_Operation (Subp)
15059 and then not Comes_From_Source (Ultimate_Alias (Subp))
15060 then
15061 Has_Predefined_Primitives := True;
15062 exit;
15063 end if;
15065 Next_Elmt (Elmt);
15066 end loop;
15068 -- Remove predefined primitives of Generic_Actual. We must use
15069 -- an auxiliary list because in case of tagged types the value
15070 -- returned by Collect_Primitive_Operations is the value stored
15071 -- in its Primitive_Operations attribute (and we don't want to
15072 -- modify its current contents).
15074 if not Has_Predefined_Primitives then
15075 declare
15076 Aux_List : constant Elist_Id := New_Elmt_List;
15078 begin
15079 Elmt := First_Elmt (Act_List);
15080 while Present (Elmt) loop
15081 Subp := Node (Elmt);
15083 if not Is_Predefined_Dispatching_Operation (Subp)
15084 or else Comes_From_Source (Subp)
15085 then
15086 Append_Elmt (Subp, Aux_List);
15087 end if;
15089 Next_Elmt (Elmt);
15090 end loop;
15092 Act_List := Aux_List;
15093 end;
15094 end if;
15096 Act_Elmt := First_Elmt (Act_List);
15097 Act_Subp := Node (Act_Elmt);
15098 end;
15099 end if;
15101 -- Stage 1: If the generic actual is not present we derive the
15102 -- primitives inherited from the parent type. If the generic parent
15103 -- type is present, the derived type is an instance of a formal
15104 -- derived type, and within the instance its operations are those of
15105 -- the actual. We derive from the formal type but make the inherited
15106 -- operations aliases of the corresponding operations of the actual.
15108 Elmt := First_Elmt (Op_List);
15109 while Present (Elmt) loop
15110 Subp := Node (Elmt);
15111 Alias_Subp := Ultimate_Alias (Subp);
15113 -- Do not derive internal entities of the parent that link
15114 -- interface primitives with their covering primitive. These
15115 -- entities will be added to this type when frozen.
15117 if Present (Interface_Alias (Subp)) then
15118 goto Continue;
15119 end if;
15121 -- If the generic actual is present find the corresponding
15122 -- operation in the generic actual. If the parent type is a
15123 -- direct ancestor of the derived type then, even if it is an
15124 -- interface, the operations are inherited from the primary
15125 -- dispatch table and are in the proper order. If we detect here
15126 -- that primitives are not in the same order we traverse the list
15127 -- of primitive operations of the actual to find the one that
15128 -- implements the interface primitive.
15130 if Need_Search
15131 or else
15132 (Present (Generic_Actual)
15133 and then Present (Act_Subp)
15134 and then not
15135 (Primitive_Names_Match (Subp, Act_Subp)
15136 and then
15137 Type_Conformant (Subp, Act_Subp,
15138 Skip_Controlling_Formals => True)))
15139 then
15140 pragma Assert (not Is_Ancestor (Parent_Base, Generic_Actual,
15141 Use_Full_View => True));
15143 -- Remember that we need searching for all pending primitives
15145 Need_Search := True;
15147 -- Handle entities associated with interface primitives
15149 if Present (Alias_Subp)
15150 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
15151 and then not Is_Predefined_Dispatching_Operation (Subp)
15152 then
15153 -- Search for the primitive in the homonym chain
15155 Act_Subp :=
15156 Find_Primitive_Covering_Interface
15157 (Tagged_Type => Generic_Actual,
15158 Iface_Prim => Alias_Subp);
15160 -- Previous search may not locate primitives covering
15161 -- interfaces defined in generics units or instantiations.
15162 -- (it fails if the covering primitive has formals whose
15163 -- type is also defined in generics or instantiations).
15164 -- In such case we search in the list of primitives of the
15165 -- generic actual for the internal entity that links the
15166 -- interface primitive and the covering primitive.
15168 if No (Act_Subp)
15169 and then Is_Generic_Type (Parent_Type)
15170 then
15171 -- This code has been designed to handle only generic
15172 -- formals that implement interfaces that are defined
15173 -- in a generic unit or instantiation. If this code is
15174 -- needed for other cases we must review it because
15175 -- (given that it relies on Original_Location to locate
15176 -- the primitive of Generic_Actual that covers the
15177 -- interface) it could leave linked through attribute
15178 -- Alias entities of unrelated instantiations).
15180 pragma Assert
15181 (Is_Generic_Unit
15182 (Scope (Find_Dispatching_Type (Alias_Subp)))
15183 or else
15184 Instantiation_Depth
15185 (Sloc (Find_Dispatching_Type (Alias_Subp))) > 0);
15187 declare
15188 Iface_Prim_Loc : constant Source_Ptr :=
15189 Original_Location (Sloc (Alias_Subp));
15191 Elmt : Elmt_Id;
15192 Prim : Entity_Id;
15194 begin
15195 Elmt :=
15196 First_Elmt (Primitive_Operations (Generic_Actual));
15198 Search : while Present (Elmt) loop
15199 Prim := Node (Elmt);
15201 if Present (Interface_Alias (Prim))
15202 and then Original_Location
15203 (Sloc (Interface_Alias (Prim))) =
15204 Iface_Prim_Loc
15205 then
15206 Act_Subp := Alias (Prim);
15207 exit Search;
15208 end if;
15210 Next_Elmt (Elmt);
15211 end loop Search;
15212 end;
15213 end if;
15215 pragma Assert (Present (Act_Subp)
15216 or else Is_Abstract_Type (Generic_Actual)
15217 or else Serious_Errors_Detected > 0);
15219 -- Handle predefined primitives plus the rest of user-defined
15220 -- primitives
15222 else
15223 Act_Elmt := First_Elmt (Act_List);
15224 while Present (Act_Elmt) loop
15225 Act_Subp := Node (Act_Elmt);
15227 exit when Primitive_Names_Match (Subp, Act_Subp)
15228 and then Type_Conformant
15229 (Subp, Act_Subp,
15230 Skip_Controlling_Formals => True)
15231 and then No (Interface_Alias (Act_Subp));
15233 Next_Elmt (Act_Elmt);
15234 end loop;
15236 if No (Act_Elmt) then
15237 Act_Subp := Empty;
15238 end if;
15239 end if;
15240 end if;
15242 -- Case 1: If the parent is a limited interface then it has the
15243 -- predefined primitives of synchronized interfaces. However, the
15244 -- actual type may be a non-limited type and hence it does not
15245 -- have such primitives.
15247 if Present (Generic_Actual)
15248 and then not Present (Act_Subp)
15249 and then Is_Limited_Interface (Parent_Base)
15250 and then Is_Predefined_Interface_Primitive (Subp)
15251 then
15252 null;
15254 -- Case 2: Inherit entities associated with interfaces that were
15255 -- not covered by the parent type. We exclude here null interface
15256 -- primitives because they do not need special management.
15258 -- We also exclude interface operations that are renamings. If the
15259 -- subprogram is an explicit renaming of an interface primitive,
15260 -- it is a regular primitive operation, and the presence of its
15261 -- alias is not relevant: it has to be derived like any other
15262 -- primitive.
15264 elsif Present (Alias (Subp))
15265 and then Nkind (Unit_Declaration_Node (Subp)) /=
15266 N_Subprogram_Renaming_Declaration
15267 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
15268 and then not
15269 (Nkind (Parent (Alias_Subp)) = N_Procedure_Specification
15270 and then Null_Present (Parent (Alias_Subp)))
15271 then
15272 -- If this is an abstract private type then we transfer the
15273 -- derivation of the interface primitive from the partial view
15274 -- to the full view. This is safe because all the interfaces
15275 -- must be visible in the partial view. Done to avoid adding
15276 -- a new interface derivation to the private part of the
15277 -- enclosing package; otherwise this new derivation would be
15278 -- decorated as hidden when the analysis of the enclosing
15279 -- package completes.
15281 if Is_Abstract_Type (Derived_Type)
15282 and then In_Private_Part (Current_Scope)
15283 and then Has_Private_Declaration (Derived_Type)
15284 then
15285 declare
15286 Partial_View : Entity_Id;
15287 Elmt : Elmt_Id;
15288 Ent : Entity_Id;
15290 begin
15291 Partial_View := First_Entity (Current_Scope);
15292 loop
15293 exit when No (Partial_View)
15294 or else (Has_Private_Declaration (Partial_View)
15295 and then
15296 Full_View (Partial_View) = Derived_Type);
15298 Next_Entity (Partial_View);
15299 end loop;
15301 -- If the partial view was not found then the source code
15302 -- has errors and the derivation is not needed.
15304 if Present (Partial_View) then
15305 Elmt :=
15306 First_Elmt (Primitive_Operations (Partial_View));
15307 while Present (Elmt) loop
15308 Ent := Node (Elmt);
15310 if Present (Alias (Ent))
15311 and then Ultimate_Alias (Ent) = Alias (Subp)
15312 then
15313 Append_Elmt
15314 (Ent, Primitive_Operations (Derived_Type));
15315 exit;
15316 end if;
15318 Next_Elmt (Elmt);
15319 end loop;
15321 -- If the interface primitive was not found in the
15322 -- partial view then this interface primitive was
15323 -- overridden. We add a derivation to activate in
15324 -- Derive_Progenitor_Subprograms the machinery to
15325 -- search for it.
15327 if No (Elmt) then
15328 Derive_Interface_Subprogram
15329 (New_Subp => New_Subp,
15330 Subp => Subp,
15331 Actual_Subp => Act_Subp);
15332 end if;
15333 end if;
15334 end;
15335 else
15336 Derive_Interface_Subprogram
15337 (New_Subp => New_Subp,
15338 Subp => Subp,
15339 Actual_Subp => Act_Subp);
15340 end if;
15342 -- Case 3: Common derivation
15344 else
15345 Derive_Subprogram
15346 (New_Subp => New_Subp,
15347 Parent_Subp => Subp,
15348 Derived_Type => Derived_Type,
15349 Parent_Type => Parent_Base,
15350 Actual_Subp => Act_Subp);
15351 end if;
15353 -- No need to update Act_Elm if we must search for the
15354 -- corresponding operation in the generic actual
15356 if not Need_Search
15357 and then Present (Act_Elmt)
15358 then
15359 Next_Elmt (Act_Elmt);
15360 Act_Subp := Node (Act_Elmt);
15361 end if;
15363 <<Continue>>
15364 Next_Elmt (Elmt);
15365 end loop;
15367 -- Inherit additional operations from progenitors. If the derived
15368 -- type is a generic actual, there are not new primitive operations
15369 -- for the type because it has those of the actual, and therefore
15370 -- nothing needs to be done. The renamings generated above are not
15371 -- primitive operations, and their purpose is simply to make the
15372 -- proper operations visible within an instantiation.
15374 if No (Generic_Actual) then
15375 Derive_Progenitor_Subprograms (Parent_Base, Derived_Type);
15376 end if;
15377 end if;
15379 -- Final check: Direct descendants must have their primitives in the
15380 -- same order. We exclude from this test untagged types and instances
15381 -- of formal derived types. We skip this test if we have already
15382 -- reported serious errors in the sources.
15384 pragma Assert (not Is_Tagged_Type (Derived_Type)
15385 or else Present (Generic_Actual)
15386 or else Serious_Errors_Detected > 0
15387 or else Check_Derived_Type);
15388 end Derive_Subprograms;
15390 --------------------------------
15391 -- Derived_Standard_Character --
15392 --------------------------------
15394 procedure Derived_Standard_Character
15395 (N : Node_Id;
15396 Parent_Type : Entity_Id;
15397 Derived_Type : Entity_Id)
15399 Loc : constant Source_Ptr := Sloc (N);
15400 Def : constant Node_Id := Type_Definition (N);
15401 Indic : constant Node_Id := Subtype_Indication (Def);
15402 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
15403 Implicit_Base : constant Entity_Id :=
15404 Create_Itype
15405 (E_Enumeration_Type, N, Derived_Type, 'B');
15407 Lo : Node_Id;
15408 Hi : Node_Id;
15410 begin
15411 Discard_Node (Process_Subtype (Indic, N));
15413 Set_Etype (Implicit_Base, Parent_Base);
15414 Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
15415 Set_RM_Size (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
15417 Set_Is_Character_Type (Implicit_Base, True);
15418 Set_Has_Delayed_Freeze (Implicit_Base);
15420 -- The bounds of the implicit base are the bounds of the parent base.
15421 -- Note that their type is the parent base.
15423 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
15424 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
15426 Set_Scalar_Range (Implicit_Base,
15427 Make_Range (Loc,
15428 Low_Bound => Lo,
15429 High_Bound => Hi));
15431 Conditional_Delay (Derived_Type, Parent_Type);
15433 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
15434 Set_Etype (Derived_Type, Implicit_Base);
15435 Set_Size_Info (Derived_Type, Parent_Type);
15437 if Unknown_RM_Size (Derived_Type) then
15438 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
15439 end if;
15441 Set_Is_Character_Type (Derived_Type, True);
15443 if Nkind (Indic) /= N_Subtype_Indication then
15445 -- If no explicit constraint, the bounds are those
15446 -- of the parent type.
15448 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Type));
15449 Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
15450 Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
15451 end if;
15453 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
15455 -- Because the implicit base is used in the conversion of the bounds, we
15456 -- have to freeze it now. This is similar to what is done for numeric
15457 -- types, and it equally suspicious, but otherwise a non-static bound
15458 -- will have a reference to an unfrozen type, which is rejected by Gigi
15459 -- (???). This requires specific care for definition of stream
15460 -- attributes. For details, see comments at the end of
15461 -- Build_Derived_Numeric_Type.
15463 Freeze_Before (N, Implicit_Base);
15464 end Derived_Standard_Character;
15466 ------------------------------
15467 -- Derived_Type_Declaration --
15468 ------------------------------
15470 procedure Derived_Type_Declaration
15471 (T : Entity_Id;
15472 N : Node_Id;
15473 Is_Completion : Boolean)
15475 Parent_Type : Entity_Id;
15477 function Comes_From_Generic (Typ : Entity_Id) return Boolean;
15478 -- Check whether the parent type is a generic formal, or derives
15479 -- directly or indirectly from one.
15481 ------------------------
15482 -- Comes_From_Generic --
15483 ------------------------
15485 function Comes_From_Generic (Typ : Entity_Id) return Boolean is
15486 begin
15487 if Is_Generic_Type (Typ) then
15488 return True;
15490 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
15491 return True;
15493 elsif Is_Private_Type (Typ)
15494 and then Present (Full_View (Typ))
15495 and then Is_Generic_Type (Root_Type (Full_View (Typ)))
15496 then
15497 return True;
15499 elsif Is_Generic_Actual_Type (Typ) then
15500 return True;
15502 else
15503 return False;
15504 end if;
15505 end Comes_From_Generic;
15507 -- Local variables
15509 Def : constant Node_Id := Type_Definition (N);
15510 Iface_Def : Node_Id;
15511 Indic : constant Node_Id := Subtype_Indication (Def);
15512 Extension : constant Node_Id := Record_Extension_Part (Def);
15513 Parent_Node : Node_Id;
15514 Taggd : Boolean;
15516 -- Start of processing for Derived_Type_Declaration
15518 begin
15519 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
15521 -- Ada 2005 (AI-251): In case of interface derivation check that the
15522 -- parent is also an interface.
15524 if Interface_Present (Def) then
15525 Check_SPARK_05_Restriction ("interface is not allowed", Def);
15527 if not Is_Interface (Parent_Type) then
15528 Diagnose_Interface (Indic, Parent_Type);
15530 else
15531 Parent_Node := Parent (Base_Type (Parent_Type));
15532 Iface_Def := Type_Definition (Parent_Node);
15534 -- Ada 2005 (AI-251): Limited interfaces can only inherit from
15535 -- other limited interfaces.
15537 if Limited_Present (Def) then
15538 if Limited_Present (Iface_Def) then
15539 null;
15541 elsif Protected_Present (Iface_Def) then
15542 Error_Msg_NE
15543 ("descendant of& must be declared"
15544 & " as a protected interface",
15545 N, Parent_Type);
15547 elsif Synchronized_Present (Iface_Def) then
15548 Error_Msg_NE
15549 ("descendant of& must be declared"
15550 & " as a synchronized interface",
15551 N, Parent_Type);
15553 elsif Task_Present (Iface_Def) then
15554 Error_Msg_NE
15555 ("descendant of& must be declared as a task interface",
15556 N, Parent_Type);
15558 else
15559 Error_Msg_N
15560 ("(Ada 2005) limited interface cannot "
15561 & "inherit from non-limited interface", Indic);
15562 end if;
15564 -- Ada 2005 (AI-345): Non-limited interfaces can only inherit
15565 -- from non-limited or limited interfaces.
15567 elsif not Protected_Present (Def)
15568 and then not Synchronized_Present (Def)
15569 and then not Task_Present (Def)
15570 then
15571 if Limited_Present (Iface_Def) then
15572 null;
15574 elsif Protected_Present (Iface_Def) then
15575 Error_Msg_NE
15576 ("descendant of& must be declared"
15577 & " as a protected interface",
15578 N, Parent_Type);
15580 elsif Synchronized_Present (Iface_Def) then
15581 Error_Msg_NE
15582 ("descendant of& must be declared"
15583 & " as a synchronized interface",
15584 N, Parent_Type);
15586 elsif Task_Present (Iface_Def) then
15587 Error_Msg_NE
15588 ("descendant of& must be declared as a task interface",
15589 N, Parent_Type);
15590 else
15591 null;
15592 end if;
15593 end if;
15594 end if;
15595 end if;
15597 if Is_Tagged_Type (Parent_Type)
15598 and then Is_Concurrent_Type (Parent_Type)
15599 and then not Is_Interface (Parent_Type)
15600 then
15601 Error_Msg_N
15602 ("parent type of a record extension cannot be "
15603 & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
15604 Set_Etype (T, Any_Type);
15605 return;
15606 end if;
15608 -- Ada 2005 (AI-251): Decorate all the names in the list of ancestor
15609 -- interfaces
15611 if Is_Tagged_Type (Parent_Type)
15612 and then Is_Non_Empty_List (Interface_List (Def))
15613 then
15614 declare
15615 Intf : Node_Id;
15616 T : Entity_Id;
15618 begin
15619 Intf := First (Interface_List (Def));
15620 while Present (Intf) loop
15621 T := Find_Type_Of_Subtype_Indic (Intf);
15623 if not Is_Interface (T) then
15624 Diagnose_Interface (Intf, T);
15626 -- Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
15627 -- a limited type from having a nonlimited progenitor.
15629 elsif (Limited_Present (Def)
15630 or else (not Is_Interface (Parent_Type)
15631 and then Is_Limited_Type (Parent_Type)))
15632 and then not Is_Limited_Interface (T)
15633 then
15634 Error_Msg_NE
15635 ("progenitor interface& of limited type must be limited",
15636 N, T);
15637 end if;
15639 Next (Intf);
15640 end loop;
15641 end;
15642 end if;
15644 if Parent_Type = Any_Type
15645 or else Etype (Parent_Type) = Any_Type
15646 or else (Is_Class_Wide_Type (Parent_Type)
15647 and then Etype (Parent_Type) = T)
15648 then
15649 -- If Parent_Type is undefined or illegal, make new type into a
15650 -- subtype of Any_Type, and set a few attributes to prevent cascaded
15651 -- errors. If this is a self-definition, emit error now.
15653 if T = Parent_Type or else T = Etype (Parent_Type) then
15654 Error_Msg_N ("type cannot be used in its own definition", Indic);
15655 end if;
15657 Set_Ekind (T, Ekind (Parent_Type));
15658 Set_Etype (T, Any_Type);
15659 Set_Scalar_Range (T, Scalar_Range (Any_Type));
15661 if Is_Tagged_Type (T)
15662 and then Is_Record_Type (T)
15663 then
15664 Set_Direct_Primitive_Operations (T, New_Elmt_List);
15665 end if;
15667 return;
15668 end if;
15670 -- Ada 2005 (AI-251): The case in which the parent of the full-view is
15671 -- an interface is special because the list of interfaces in the full
15672 -- view can be given in any order. For example:
15674 -- type A is interface;
15675 -- type B is interface and A;
15676 -- type D is new B with private;
15677 -- private
15678 -- type D is new A and B with null record; -- 1 --
15680 -- In this case we perform the following transformation of -1-:
15682 -- type D is new B and A with null record;
15684 -- If the parent of the full-view covers the parent of the partial-view
15685 -- we have two possible cases:
15687 -- 1) They have the same parent
15688 -- 2) The parent of the full-view implements some further interfaces
15690 -- In both cases we do not need to perform the transformation. In the
15691 -- first case the source program is correct and the transformation is
15692 -- not needed; in the second case the source program does not fulfill
15693 -- the no-hidden interfaces rule (AI-396) and the error will be reported
15694 -- later.
15696 -- This transformation not only simplifies the rest of the analysis of
15697 -- this type declaration but also simplifies the correct generation of
15698 -- the object layout to the expander.
15700 if In_Private_Part (Current_Scope)
15701 and then Is_Interface (Parent_Type)
15702 then
15703 declare
15704 Iface : Node_Id;
15705 Partial_View : Entity_Id;
15706 Partial_View_Parent : Entity_Id;
15707 New_Iface : Node_Id;
15709 begin
15710 -- Look for the associated private type declaration
15712 Partial_View := First_Entity (Current_Scope);
15713 loop
15714 exit when No (Partial_View)
15715 or else (Has_Private_Declaration (Partial_View)
15716 and then Full_View (Partial_View) = T);
15718 Next_Entity (Partial_View);
15719 end loop;
15721 -- If the partial view was not found then the source code has
15722 -- errors and the transformation is not needed.
15724 if Present (Partial_View) then
15725 Partial_View_Parent := Etype (Partial_View);
15727 -- If the parent of the full-view covers the parent of the
15728 -- partial-view we have nothing else to do.
15730 if Interface_Present_In_Ancestor
15731 (Parent_Type, Partial_View_Parent)
15732 then
15733 null;
15735 -- Traverse the list of interfaces of the full-view to look
15736 -- for the parent of the partial-view and perform the tree
15737 -- transformation.
15739 else
15740 Iface := First (Interface_List (Def));
15741 while Present (Iface) loop
15742 if Etype (Iface) = Etype (Partial_View) then
15743 Rewrite (Subtype_Indication (Def),
15744 New_Copy (Subtype_Indication
15745 (Parent (Partial_View))));
15747 New_Iface :=
15748 Make_Identifier (Sloc (N), Chars (Parent_Type));
15749 Append (New_Iface, Interface_List (Def));
15751 -- Analyze the transformed code
15753 Derived_Type_Declaration (T, N, Is_Completion);
15754 return;
15755 end if;
15757 Next (Iface);
15758 end loop;
15759 end if;
15760 end if;
15761 end;
15762 end if;
15764 -- Only composite types other than array types are allowed to have
15765 -- discriminants.
15767 if Present (Discriminant_Specifications (N)) then
15768 if (Is_Elementary_Type (Parent_Type)
15769 or else
15770 Is_Array_Type (Parent_Type))
15771 and then not Error_Posted (N)
15772 then
15773 Error_Msg_N
15774 ("elementary or array type cannot have discriminants",
15775 Defining_Identifier (First (Discriminant_Specifications (N))));
15776 Set_Has_Discriminants (T, False);
15778 -- The type is allowed to have discriminants
15780 else
15781 Check_SPARK_05_Restriction ("discriminant type is not allowed", N);
15782 end if;
15783 end if;
15785 -- In Ada 83, a derived type defined in a package specification cannot
15786 -- be used for further derivation until the end of its visible part.
15787 -- Note that derivation in the private part of the package is allowed.
15789 if Ada_Version = Ada_83
15790 and then Is_Derived_Type (Parent_Type)
15791 and then In_Visible_Part (Scope (Parent_Type))
15792 then
15793 if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
15794 Error_Msg_N
15795 ("(Ada 83): premature use of type for derivation", Indic);
15796 end if;
15797 end if;
15799 -- Check for early use of incomplete or private type
15801 if Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
15802 Error_Msg_N ("premature derivation of incomplete type", Indic);
15803 return;
15805 elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
15806 and then not Comes_From_Generic (Parent_Type))
15807 or else Has_Private_Component (Parent_Type)
15808 then
15809 -- The ancestor type of a formal type can be incomplete, in which
15810 -- case only the operations of the partial view are available in the
15811 -- generic. Subsequent checks may be required when the full view is
15812 -- analyzed to verify that a derivation from a tagged type has an
15813 -- extension.
15815 if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
15816 null;
15818 elsif No (Underlying_Type (Parent_Type))
15819 or else Has_Private_Component (Parent_Type)
15820 then
15821 Error_Msg_N
15822 ("premature derivation of derived or private type", Indic);
15824 -- Flag the type itself as being in error, this prevents some
15825 -- nasty problems with subsequent uses of the malformed type.
15827 Set_Error_Posted (T);
15829 -- Check that within the immediate scope of an untagged partial
15830 -- view it's illegal to derive from the partial view if the
15831 -- full view is tagged. (7.3(7))
15833 -- We verify that the Parent_Type is a partial view by checking
15834 -- that it is not a Full_Type_Declaration (i.e. a private type or
15835 -- private extension declaration), to distinguish a partial view
15836 -- from a derivation from a private type which also appears as
15837 -- E_Private_Type. If the parent base type is not declared in an
15838 -- enclosing scope there is no need to check.
15840 elsif Present (Full_View (Parent_Type))
15841 and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
15842 and then not Is_Tagged_Type (Parent_Type)
15843 and then Is_Tagged_Type (Full_View (Parent_Type))
15844 and then In_Open_Scopes (Scope (Base_Type (Parent_Type)))
15845 then
15846 Error_Msg_N
15847 ("premature derivation from type with tagged full view",
15848 Indic);
15849 end if;
15850 end if;
15852 -- Check that form of derivation is appropriate
15854 Taggd := Is_Tagged_Type (Parent_Type);
15856 -- Set the parent type to the class-wide type's specific type in this
15857 -- case to prevent cascading errors
15859 if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
15860 Error_Msg_N ("parent type must not be a class-wide type", Indic);
15861 Set_Etype (T, Etype (Parent_Type));
15862 return;
15863 end if;
15865 if Present (Extension) and then not Taggd then
15866 Error_Msg_N
15867 ("type derived from untagged type cannot have extension", Indic);
15869 elsif No (Extension) and then Taggd then
15871 -- If this declaration is within a private part (or body) of a
15872 -- generic instantiation then the derivation is allowed (the parent
15873 -- type can only appear tagged in this case if it's a generic actual
15874 -- type, since it would otherwise have been rejected in the analysis
15875 -- of the generic template).
15877 if not Is_Generic_Actual_Type (Parent_Type)
15878 or else In_Visible_Part (Scope (Parent_Type))
15879 then
15880 if Is_Class_Wide_Type (Parent_Type) then
15881 Error_Msg_N
15882 ("parent type must not be a class-wide type", Indic);
15884 -- Use specific type to prevent cascaded errors.
15886 Parent_Type := Etype (Parent_Type);
15888 else
15889 Error_Msg_N
15890 ("type derived from tagged type must have extension", Indic);
15891 end if;
15892 end if;
15893 end if;
15895 -- AI-443: Synchronized formal derived types require a private
15896 -- extension. There is no point in checking the ancestor type or
15897 -- the progenitors since the construct is wrong to begin with.
15899 if Ada_Version >= Ada_2005
15900 and then Is_Generic_Type (T)
15901 and then Present (Original_Node (N))
15902 then
15903 declare
15904 Decl : constant Node_Id := Original_Node (N);
15906 begin
15907 if Nkind (Decl) = N_Formal_Type_Declaration
15908 and then Nkind (Formal_Type_Definition (Decl)) =
15909 N_Formal_Derived_Type_Definition
15910 and then Synchronized_Present (Formal_Type_Definition (Decl))
15911 and then No (Extension)
15913 -- Avoid emitting a duplicate error message
15915 and then not Error_Posted (Indic)
15916 then
15917 Error_Msg_N
15918 ("synchronized derived type must have extension", N);
15919 end if;
15920 end;
15921 end if;
15923 if Null_Exclusion_Present (Def)
15924 and then not Is_Access_Type (Parent_Type)
15925 then
15926 Error_Msg_N ("null exclusion can only apply to an access type", N);
15927 end if;
15929 -- Avoid deriving parent primitives of underlying record views
15931 Build_Derived_Type (N, Parent_Type, T, Is_Completion,
15932 Derive_Subps => not Is_Underlying_Record_View (T));
15934 -- AI-419: The parent type of an explicitly limited derived type must
15935 -- be a limited type or a limited interface.
15937 if Limited_Present (Def) then
15938 Set_Is_Limited_Record (T);
15940 if Is_Interface (T) then
15941 Set_Is_Limited_Interface (T);
15942 end if;
15944 if not Is_Limited_Type (Parent_Type)
15945 and then
15946 (not Is_Interface (Parent_Type)
15947 or else not Is_Limited_Interface (Parent_Type))
15948 then
15949 -- AI05-0096: a derivation in the private part of an instance is
15950 -- legal if the generic formal is untagged limited, and the actual
15951 -- is non-limited.
15953 if Is_Generic_Actual_Type (Parent_Type)
15954 and then In_Private_Part (Current_Scope)
15955 and then
15956 not Is_Tagged_Type
15957 (Generic_Parent_Type (Parent (Parent_Type)))
15958 then
15959 null;
15961 else
15962 Error_Msg_NE
15963 ("parent type& of limited type must be limited",
15964 N, Parent_Type);
15965 end if;
15966 end if;
15967 end if;
15969 -- In SPARK, there are no derived type definitions other than type
15970 -- extensions of tagged record types.
15972 if No (Extension) then
15973 Check_SPARK_05_Restriction
15974 ("derived type is not allowed", Original_Node (N));
15975 end if;
15976 end Derived_Type_Declaration;
15978 ------------------------
15979 -- Diagnose_Interface --
15980 ------------------------
15982 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id) is
15983 begin
15984 if not Is_Interface (E) and then E /= Any_Type then
15985 Error_Msg_NE ("(Ada 2005) & must be an interface", N, E);
15986 end if;
15987 end Diagnose_Interface;
15989 ----------------------------------
15990 -- Enumeration_Type_Declaration --
15991 ----------------------------------
15993 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
15994 Ev : Uint;
15995 L : Node_Id;
15996 R_Node : Node_Id;
15997 B_Node : Node_Id;
15999 begin
16000 -- Create identifier node representing lower bound
16002 B_Node := New_Node (N_Identifier, Sloc (Def));
16003 L := First (Literals (Def));
16004 Set_Chars (B_Node, Chars (L));
16005 Set_Entity (B_Node, L);
16006 Set_Etype (B_Node, T);
16007 Set_Is_Static_Expression (B_Node, True);
16009 R_Node := New_Node (N_Range, Sloc (Def));
16010 Set_Low_Bound (R_Node, B_Node);
16012 Set_Ekind (T, E_Enumeration_Type);
16013 Set_First_Literal (T, L);
16014 Set_Etype (T, T);
16015 Set_Is_Constrained (T);
16017 Ev := Uint_0;
16019 -- Loop through literals of enumeration type setting pos and rep values
16020 -- except that if the Ekind is already set, then it means the literal
16021 -- was already constructed (case of a derived type declaration and we
16022 -- should not disturb the Pos and Rep values.
16024 while Present (L) loop
16025 if Ekind (L) /= E_Enumeration_Literal then
16026 Set_Ekind (L, E_Enumeration_Literal);
16027 Set_Enumeration_Pos (L, Ev);
16028 Set_Enumeration_Rep (L, Ev);
16029 Set_Is_Known_Valid (L, True);
16030 end if;
16032 Set_Etype (L, T);
16033 New_Overloaded_Entity (L);
16034 Generate_Definition (L);
16035 Set_Convention (L, Convention_Intrinsic);
16037 -- Case of character literal
16039 if Nkind (L) = N_Defining_Character_Literal then
16040 Set_Is_Character_Type (T, True);
16042 -- Check violation of No_Wide_Characters
16044 if Restriction_Check_Required (No_Wide_Characters) then
16045 Get_Name_String (Chars (L));
16047 if Name_Len >= 3 and then Name_Buffer (1 .. 2) = "QW" then
16048 Check_Restriction (No_Wide_Characters, L);
16049 end if;
16050 end if;
16051 end if;
16053 Ev := Ev + 1;
16054 Next (L);
16055 end loop;
16057 -- Now create a node representing upper bound
16059 B_Node := New_Node (N_Identifier, Sloc (Def));
16060 Set_Chars (B_Node, Chars (Last (Literals (Def))));
16061 Set_Entity (B_Node, Last (Literals (Def)));
16062 Set_Etype (B_Node, T);
16063 Set_Is_Static_Expression (B_Node, True);
16065 Set_High_Bound (R_Node, B_Node);
16067 -- Initialize various fields of the type. Some of this information
16068 -- may be overwritten later through rep.clauses.
16070 Set_Scalar_Range (T, R_Node);
16071 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
16072 Set_Enum_Esize (T);
16073 Set_Enum_Pos_To_Rep (T, Empty);
16075 -- Set Discard_Names if configuration pragma set, or if there is
16076 -- a parameterless pragma in the current declarative region
16078 if Global_Discard_Names or else Discard_Names (Scope (T)) then
16079 Set_Discard_Names (T);
16080 end if;
16082 -- Process end label if there is one
16084 if Present (Def) then
16085 Process_End_Label (Def, 'e', T);
16086 end if;
16087 end Enumeration_Type_Declaration;
16089 ---------------------------------
16090 -- Expand_To_Stored_Constraint --
16091 ---------------------------------
16093 function Expand_To_Stored_Constraint
16094 (Typ : Entity_Id;
16095 Constraint : Elist_Id) return Elist_Id
16097 Explicitly_Discriminated_Type : Entity_Id;
16098 Expansion : Elist_Id;
16099 Discriminant : Entity_Id;
16101 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
16102 -- Find the nearest type that actually specifies discriminants
16104 ---------------------------------
16105 -- Type_With_Explicit_Discrims --
16106 ---------------------------------
16108 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
16109 Typ : constant E := Base_Type (Id);
16111 begin
16112 if Ekind (Typ) in Incomplete_Or_Private_Kind then
16113 if Present (Full_View (Typ)) then
16114 return Type_With_Explicit_Discrims (Full_View (Typ));
16115 end if;
16117 else
16118 if Has_Discriminants (Typ) then
16119 return Typ;
16120 end if;
16121 end if;
16123 if Etype (Typ) = Typ then
16124 return Empty;
16125 elsif Has_Discriminants (Typ) then
16126 return Typ;
16127 else
16128 return Type_With_Explicit_Discrims (Etype (Typ));
16129 end if;
16131 end Type_With_Explicit_Discrims;
16133 -- Start of processing for Expand_To_Stored_Constraint
16135 begin
16136 if No (Constraint) or else Is_Empty_Elmt_List (Constraint) then
16137 return No_Elist;
16138 end if;
16140 Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
16142 if No (Explicitly_Discriminated_Type) then
16143 return No_Elist;
16144 end if;
16146 Expansion := New_Elmt_List;
16148 Discriminant :=
16149 First_Stored_Discriminant (Explicitly_Discriminated_Type);
16150 while Present (Discriminant) loop
16151 Append_Elmt
16152 (Get_Discriminant_Value
16153 (Discriminant, Explicitly_Discriminated_Type, Constraint),
16154 To => Expansion);
16155 Next_Stored_Discriminant (Discriminant);
16156 end loop;
16158 return Expansion;
16159 end Expand_To_Stored_Constraint;
16161 ---------------------------
16162 -- Find_Hidden_Interface --
16163 ---------------------------
16165 function Find_Hidden_Interface
16166 (Src : Elist_Id;
16167 Dest : Elist_Id) return Entity_Id
16169 Iface : Entity_Id;
16170 Iface_Elmt : Elmt_Id;
16172 begin
16173 if Present (Src) and then Present (Dest) then
16174 Iface_Elmt := First_Elmt (Src);
16175 while Present (Iface_Elmt) loop
16176 Iface := Node (Iface_Elmt);
16178 if Is_Interface (Iface)
16179 and then not Contain_Interface (Iface, Dest)
16180 then
16181 return Iface;
16182 end if;
16184 Next_Elmt (Iface_Elmt);
16185 end loop;
16186 end if;
16188 return Empty;
16189 end Find_Hidden_Interface;
16191 --------------------
16192 -- Find_Type_Name --
16193 --------------------
16195 function Find_Type_Name (N : Node_Id) return Entity_Id is
16196 Id : constant Entity_Id := Defining_Identifier (N);
16197 Prev : Entity_Id;
16198 New_Id : Entity_Id;
16199 Prev_Par : Node_Id;
16201 procedure Check_Duplicate_Aspects;
16202 -- Check that aspects specified in a completion have not been specified
16203 -- already in the partial view. Type_Invariant and others can be
16204 -- specified on either view but never on both.
16206 procedure Tag_Mismatch;
16207 -- Diagnose a tagged partial view whose full view is untagged.
16208 -- We post the message on the full view, with a reference to
16209 -- the previous partial view. The partial view can be private
16210 -- or incomplete, and these are handled in a different manner,
16211 -- so we determine the position of the error message from the
16212 -- respective slocs of both.
16214 -----------------------------
16215 -- Check_Duplicate_Aspects --
16216 -----------------------------
16217 procedure Check_Duplicate_Aspects is
16218 Prev_Aspects : constant List_Id := Aspect_Specifications (Prev_Par);
16219 Full_Aspects : constant List_Id := Aspect_Specifications (N);
16220 F_Spec, P_Spec : Node_Id;
16222 begin
16223 if Present (Prev_Aspects) and then Present (Full_Aspects) then
16224 F_Spec := First (Full_Aspects);
16225 while Present (F_Spec) loop
16226 P_Spec := First (Prev_Aspects);
16227 while Present (P_Spec) loop
16228 if Chars (Identifier (P_Spec)) = Chars (Identifier (F_Spec))
16229 then
16230 Error_Msg_N
16231 ("aspect already specified in private declaration",
16232 F_Spec);
16233 Remove (F_Spec);
16234 return;
16235 end if;
16237 Next (P_Spec);
16238 end loop;
16240 Next (F_Spec);
16241 end loop;
16242 end if;
16243 end Check_Duplicate_Aspects;
16245 ------------------
16246 -- Tag_Mismatch --
16247 ------------------
16249 procedure Tag_Mismatch is
16250 begin
16251 if Sloc (Prev) < Sloc (Id) then
16252 if Ada_Version >= Ada_2012
16253 and then Nkind (N) = N_Private_Type_Declaration
16254 then
16255 Error_Msg_NE
16256 ("declaration of private } must be a tagged type ", Id, Prev);
16257 else
16258 Error_Msg_NE
16259 ("full declaration of } must be a tagged type ", Id, Prev);
16260 end if;
16262 else
16263 if Ada_Version >= Ada_2012
16264 and then Nkind (N) = N_Private_Type_Declaration
16265 then
16266 Error_Msg_NE
16267 ("declaration of private } must be a tagged type ", Prev, Id);
16268 else
16269 Error_Msg_NE
16270 ("full declaration of } must be a tagged type ", Prev, Id);
16271 end if;
16272 end if;
16273 end Tag_Mismatch;
16275 -- Start of processing for Find_Type_Name
16277 begin
16278 -- Find incomplete declaration, if one was given
16280 Prev := Current_Entity_In_Scope (Id);
16282 -- New type declaration
16284 if No (Prev) then
16285 Enter_Name (Id);
16286 return Id;
16288 -- Previous declaration exists
16290 else
16291 Prev_Par := Parent (Prev);
16293 -- Error if not incomplete/private case except if previous
16294 -- declaration is implicit, etc. Enter_Name will emit error if
16295 -- appropriate.
16297 if not Is_Incomplete_Or_Private_Type (Prev) then
16298 Enter_Name (Id);
16299 New_Id := Id;
16301 -- Check invalid completion of private or incomplete type
16303 elsif not Nkind_In (N, N_Full_Type_Declaration,
16304 N_Task_Type_Declaration,
16305 N_Protected_Type_Declaration)
16306 and then
16307 (Ada_Version < Ada_2012
16308 or else not Is_Incomplete_Type (Prev)
16309 or else not Nkind_In (N, N_Private_Type_Declaration,
16310 N_Private_Extension_Declaration))
16311 then
16312 -- Completion must be a full type declarations (RM 7.3(4))
16314 Error_Msg_Sloc := Sloc (Prev);
16315 Error_Msg_NE ("invalid completion of }", Id, Prev);
16317 -- Set scope of Id to avoid cascaded errors. Entity is never
16318 -- examined again, except when saving globals in generics.
16320 Set_Scope (Id, Current_Scope);
16321 New_Id := Id;
16323 -- If this is a repeated incomplete declaration, no further
16324 -- checks are possible.
16326 if Nkind (N) = N_Incomplete_Type_Declaration then
16327 return Prev;
16328 end if;
16330 -- Case of full declaration of incomplete type
16332 elsif Ekind (Prev) = E_Incomplete_Type
16333 and then (Ada_Version < Ada_2012
16334 or else No (Full_View (Prev))
16335 or else not Is_Private_Type (Full_View (Prev)))
16336 then
16337 -- Indicate that the incomplete declaration has a matching full
16338 -- declaration. The defining occurrence of the incomplete
16339 -- declaration remains the visible one, and the procedure
16340 -- Get_Full_View dereferences it whenever the type is used.
16342 if Present (Full_View (Prev)) then
16343 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
16344 end if;
16346 Set_Full_View (Prev, Id);
16347 Append_Entity (Id, Current_Scope);
16348 Set_Is_Public (Id, Is_Public (Prev));
16349 Set_Is_Internal (Id);
16350 New_Id := Prev;
16352 -- If the incomplete view is tagged, a class_wide type has been
16353 -- created already. Use it for the private type as well, in order
16354 -- to prevent multiple incompatible class-wide types that may be
16355 -- created for self-referential anonymous access components.
16357 if Is_Tagged_Type (Prev)
16358 and then Present (Class_Wide_Type (Prev))
16359 then
16360 Set_Ekind (Id, Ekind (Prev)); -- will be reset later
16361 Set_Class_Wide_Type (Id, Class_Wide_Type (Prev));
16363 -- The type of the classwide type is the current Id. Previously
16364 -- this was not done for private declarations because of order-
16365 -- of elaboration issues in the back-end, but gigi now handles
16366 -- this properly.
16368 Set_Etype (Class_Wide_Type (Id), Id);
16369 end if;
16371 -- Case of full declaration of private type
16373 else
16374 -- If the private type was a completion of an incomplete type then
16375 -- update Prev to reference the private type
16377 if Ada_Version >= Ada_2012
16378 and then Ekind (Prev) = E_Incomplete_Type
16379 and then Present (Full_View (Prev))
16380 and then Is_Private_Type (Full_View (Prev))
16381 then
16382 Prev := Full_View (Prev);
16383 Prev_Par := Parent (Prev);
16384 end if;
16386 if Nkind (N) = N_Full_Type_Declaration
16387 and then Nkind_In
16388 (Type_Definition (N), N_Record_Definition,
16389 N_Derived_Type_Definition)
16390 and then Interface_Present (Type_Definition (N))
16391 then
16392 Error_Msg_N
16393 ("completion of private type cannot be an interface", N);
16394 end if;
16396 if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
16397 if Etype (Prev) /= Prev then
16399 -- Prev is a private subtype or a derived type, and needs
16400 -- no completion.
16402 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
16403 New_Id := Id;
16405 elsif Ekind (Prev) = E_Private_Type
16406 and then Nkind_In (N, N_Task_Type_Declaration,
16407 N_Protected_Type_Declaration)
16408 then
16409 Error_Msg_N
16410 ("completion of nonlimited type cannot be limited", N);
16412 elsif Ekind (Prev) = E_Record_Type_With_Private
16413 and then Nkind_In (N, N_Task_Type_Declaration,
16414 N_Protected_Type_Declaration)
16415 then
16416 if not Is_Limited_Record (Prev) then
16417 Error_Msg_N
16418 ("completion of nonlimited type cannot be limited", N);
16420 elsif No (Interface_List (N)) then
16421 Error_Msg_N
16422 ("completion of tagged private type must be tagged",
16424 end if;
16425 end if;
16427 -- Ada 2005 (AI-251): Private extension declaration of a task
16428 -- type or a protected type. This case arises when covering
16429 -- interface types.
16431 elsif Nkind_In (N, N_Task_Type_Declaration,
16432 N_Protected_Type_Declaration)
16433 then
16434 null;
16436 elsif Nkind (N) /= N_Full_Type_Declaration
16437 or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
16438 then
16439 Error_Msg_N
16440 ("full view of private extension must be an extension", N);
16442 elsif not (Abstract_Present (Parent (Prev)))
16443 and then Abstract_Present (Type_Definition (N))
16444 then
16445 Error_Msg_N
16446 ("full view of non-abstract extension cannot be abstract", N);
16447 end if;
16449 if not In_Private_Part (Current_Scope) then
16450 Error_Msg_N
16451 ("declaration of full view must appear in private part", N);
16452 end if;
16454 if Ada_Version >= Ada_2012 then
16455 Check_Duplicate_Aspects;
16456 end if;
16458 Copy_And_Swap (Prev, Id);
16459 Set_Has_Private_Declaration (Prev);
16460 Set_Has_Private_Declaration (Id);
16462 -- Preserve aspect and iterator flags that may have been set on
16463 -- the partial view.
16465 Set_Has_Delayed_Aspects (Prev, Has_Delayed_Aspects (Id));
16466 Set_Has_Implicit_Dereference (Prev, Has_Implicit_Dereference (Id));
16468 -- If no error, propagate freeze_node from private to full view.
16469 -- It may have been generated for an early operational item.
16471 if Present (Freeze_Node (Id))
16472 and then Serious_Errors_Detected = 0
16473 and then No (Full_View (Id))
16474 then
16475 Set_Freeze_Node (Prev, Freeze_Node (Id));
16476 Set_Freeze_Node (Id, Empty);
16477 Set_First_Rep_Item (Prev, First_Rep_Item (Id));
16478 end if;
16480 Set_Full_View (Id, Prev);
16481 New_Id := Prev;
16482 end if;
16484 -- Verify that full declaration conforms to partial one
16486 if Is_Incomplete_Or_Private_Type (Prev)
16487 and then Present (Discriminant_Specifications (Prev_Par))
16488 then
16489 if Present (Discriminant_Specifications (N)) then
16490 if Ekind (Prev) = E_Incomplete_Type then
16491 Check_Discriminant_Conformance (N, Prev, Prev);
16492 else
16493 Check_Discriminant_Conformance (N, Prev, Id);
16494 end if;
16496 else
16497 Error_Msg_N
16498 ("missing discriminants in full type declaration", N);
16500 -- To avoid cascaded errors on subsequent use, share the
16501 -- discriminants of the partial view.
16503 Set_Discriminant_Specifications (N,
16504 Discriminant_Specifications (Prev_Par));
16505 end if;
16506 end if;
16508 -- A prior untagged partial view can have an associated class-wide
16509 -- type due to use of the class attribute, and in this case the full
16510 -- type must also be tagged. This Ada 95 usage is deprecated in favor
16511 -- of incomplete tagged declarations, but we check for it.
16513 if Is_Type (Prev)
16514 and then (Is_Tagged_Type (Prev)
16515 or else Present (Class_Wide_Type (Prev)))
16516 then
16517 -- Ada 2012 (AI05-0162): A private type may be the completion of
16518 -- an incomplete type.
16520 if Ada_Version >= Ada_2012
16521 and then Is_Incomplete_Type (Prev)
16522 and then Nkind_In (N, N_Private_Type_Declaration,
16523 N_Private_Extension_Declaration)
16524 then
16525 -- No need to check private extensions since they are tagged
16527 if Nkind (N) = N_Private_Type_Declaration
16528 and then not Tagged_Present (N)
16529 then
16530 Tag_Mismatch;
16531 end if;
16533 -- The full declaration is either a tagged type (including
16534 -- a synchronized type that implements interfaces) or a
16535 -- type extension, otherwise this is an error.
16537 elsif Nkind_In (N, N_Task_Type_Declaration,
16538 N_Protected_Type_Declaration)
16539 then
16540 if No (Interface_List (N)) and then not Error_Posted (N) then
16541 Tag_Mismatch;
16542 end if;
16544 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
16546 -- Indicate that the previous declaration (tagged incomplete
16547 -- or private declaration) requires the same on the full one.
16549 if not Tagged_Present (Type_Definition (N)) then
16550 Tag_Mismatch;
16551 Set_Is_Tagged_Type (Id);
16552 end if;
16554 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
16555 if No (Record_Extension_Part (Type_Definition (N))) then
16556 Error_Msg_NE
16557 ("full declaration of } must be a record extension",
16558 Prev, Id);
16560 -- Set some attributes to produce a usable full view
16562 Set_Is_Tagged_Type (Id);
16563 end if;
16565 else
16566 Tag_Mismatch;
16567 end if;
16568 end if;
16570 if Present (Prev)
16571 and then Nkind (Parent (Prev)) = N_Incomplete_Type_Declaration
16572 and then Present (Premature_Use (Parent (Prev)))
16573 then
16574 Error_Msg_Sloc := Sloc (N);
16575 Error_Msg_N
16576 ("\full declaration #", Premature_Use (Parent (Prev)));
16577 end if;
16579 return New_Id;
16580 end if;
16581 end Find_Type_Name;
16583 -------------------------
16584 -- Find_Type_Of_Object --
16585 -------------------------
16587 function Find_Type_Of_Object
16588 (Obj_Def : Node_Id;
16589 Related_Nod : Node_Id) return Entity_Id
16591 Def_Kind : constant Node_Kind := Nkind (Obj_Def);
16592 P : Node_Id := Parent (Obj_Def);
16593 T : Entity_Id;
16594 Nam : Name_Id;
16596 begin
16597 -- If the parent is a component_definition node we climb to the
16598 -- component_declaration node
16600 if Nkind (P) = N_Component_Definition then
16601 P := Parent (P);
16602 end if;
16604 -- Case of an anonymous array subtype
16606 if Nkind_In (Def_Kind, N_Constrained_Array_Definition,
16607 N_Unconstrained_Array_Definition)
16608 then
16609 T := Empty;
16610 Array_Type_Declaration (T, Obj_Def);
16612 -- Create an explicit subtype whenever possible
16614 elsif Nkind (P) /= N_Component_Declaration
16615 and then Def_Kind = N_Subtype_Indication
16616 then
16617 -- Base name of subtype on object name, which will be unique in
16618 -- the current scope.
16620 -- If this is a duplicate declaration, return base type, to avoid
16621 -- generating duplicate anonymous types.
16623 if Error_Posted (P) then
16624 Analyze (Subtype_Mark (Obj_Def));
16625 return Entity (Subtype_Mark (Obj_Def));
16626 end if;
16628 Nam :=
16629 New_External_Name
16630 (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
16632 T := Make_Defining_Identifier (Sloc (P), Nam);
16634 Insert_Action (Obj_Def,
16635 Make_Subtype_Declaration (Sloc (P),
16636 Defining_Identifier => T,
16637 Subtype_Indication => Relocate_Node (Obj_Def)));
16639 -- This subtype may need freezing, and this will not be done
16640 -- automatically if the object declaration is not in declarative
16641 -- part. Since this is an object declaration, the type cannot always
16642 -- be frozen here. Deferred constants do not freeze their type
16643 -- (which often enough will be private).
16645 if Nkind (P) = N_Object_Declaration
16646 and then Constant_Present (P)
16647 and then No (Expression (P))
16648 then
16649 null;
16651 -- Here we freeze the base type of object type to catch premature use
16652 -- of discriminated private type without a full view.
16654 else
16655 Insert_Actions (Obj_Def, Freeze_Entity (Base_Type (T), P));
16656 end if;
16658 -- Ada 2005 AI-406: the object definition in an object declaration
16659 -- can be an access definition.
16661 elsif Def_Kind = N_Access_Definition then
16662 T := Access_Definition (Related_Nod, Obj_Def);
16664 Set_Is_Local_Anonymous_Access
16666 V => (Ada_Version < Ada_2012)
16667 or else (Nkind (P) /= N_Object_Declaration)
16668 or else Is_Library_Level_Entity (Defining_Identifier (P)));
16670 -- Otherwise, the object definition is just a subtype_mark
16672 else
16673 T := Process_Subtype (Obj_Def, Related_Nod);
16675 -- If expansion is disabled an object definition that is an aggregate
16676 -- will not get expanded and may lead to scoping problems in the back
16677 -- end, if the object is referenced in an inner scope. In that case
16678 -- create an itype reference for the object definition now. This
16679 -- may be redundant in some cases, but harmless.
16681 if Is_Itype (T)
16682 and then Nkind (Related_Nod) = N_Object_Declaration
16683 and then ASIS_Mode
16684 then
16685 Build_Itype_Reference (T, Related_Nod);
16686 end if;
16687 end if;
16689 return T;
16690 end Find_Type_Of_Object;
16692 --------------------------------
16693 -- Find_Type_Of_Subtype_Indic --
16694 --------------------------------
16696 function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
16697 Typ : Entity_Id;
16699 begin
16700 -- Case of subtype mark with a constraint
16702 if Nkind (S) = N_Subtype_Indication then
16703 Find_Type (Subtype_Mark (S));
16704 Typ := Entity (Subtype_Mark (S));
16706 if not
16707 Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
16708 then
16709 Error_Msg_N
16710 ("incorrect constraint for this kind of type", Constraint (S));
16711 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
16712 end if;
16714 -- Otherwise we have a subtype mark without a constraint
16716 elsif Error_Posted (S) then
16717 Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
16718 return Any_Type;
16720 else
16721 Find_Type (S);
16722 Typ := Entity (S);
16723 end if;
16725 -- Check No_Wide_Characters restriction
16727 Check_Wide_Character_Restriction (Typ, S);
16729 return Typ;
16730 end Find_Type_Of_Subtype_Indic;
16732 -------------------------------------
16733 -- Floating_Point_Type_Declaration --
16734 -------------------------------------
16736 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
16737 Digs : constant Node_Id := Digits_Expression (Def);
16738 Max_Digs_Val : constant Uint := Digits_Value (Standard_Long_Long_Float);
16739 Digs_Val : Uint;
16740 Base_Typ : Entity_Id;
16741 Implicit_Base : Entity_Id;
16742 Bound : Node_Id;
16744 function Can_Derive_From (E : Entity_Id) return Boolean;
16745 -- Find if given digits value, and possibly a specified range, allows
16746 -- derivation from specified type
16748 function Find_Base_Type return Entity_Id;
16749 -- Find a predefined base type that Def can derive from, or generate
16750 -- an error and substitute Long_Long_Float if none exists.
16752 ---------------------
16753 -- Can_Derive_From --
16754 ---------------------
16756 function Can_Derive_From (E : Entity_Id) return Boolean is
16757 Spec : constant Entity_Id := Real_Range_Specification (Def);
16759 begin
16760 -- Check specified "digits" constraint
16762 if Digs_Val > Digits_Value (E) then
16763 return False;
16764 end if;
16766 -- Check for matching range, if specified
16768 if Present (Spec) then
16769 if Expr_Value_R (Type_Low_Bound (E)) >
16770 Expr_Value_R (Low_Bound (Spec))
16771 then
16772 return False;
16773 end if;
16775 if Expr_Value_R (Type_High_Bound (E)) <
16776 Expr_Value_R (High_Bound (Spec))
16777 then
16778 return False;
16779 end if;
16780 end if;
16782 return True;
16783 end Can_Derive_From;
16785 --------------------
16786 -- Find_Base_Type --
16787 --------------------
16789 function Find_Base_Type return Entity_Id is
16790 Choice : Elmt_Id := First_Elmt (Predefined_Float_Types);
16792 begin
16793 -- Iterate over the predefined types in order, returning the first
16794 -- one that Def can derive from.
16796 while Present (Choice) loop
16797 if Can_Derive_From (Node (Choice)) then
16798 return Node (Choice);
16799 end if;
16801 Next_Elmt (Choice);
16802 end loop;
16804 -- If we can't derive from any existing type, use Long_Long_Float
16805 -- and give appropriate message explaining the problem.
16807 if Digs_Val > Max_Digs_Val then
16808 -- It might be the case that there is a type with the requested
16809 -- range, just not the combination of digits and range.
16811 Error_Msg_N
16812 ("no predefined type has requested range and precision",
16813 Real_Range_Specification (Def));
16815 else
16816 Error_Msg_N
16817 ("range too large for any predefined type",
16818 Real_Range_Specification (Def));
16819 end if;
16821 return Standard_Long_Long_Float;
16822 end Find_Base_Type;
16824 -- Start of processing for Floating_Point_Type_Declaration
16826 begin
16827 Check_Restriction (No_Floating_Point, Def);
16829 -- Create an implicit base type
16831 Implicit_Base :=
16832 Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
16834 -- Analyze and verify digits value
16836 Analyze_And_Resolve (Digs, Any_Integer);
16837 Check_Digits_Expression (Digs);
16838 Digs_Val := Expr_Value (Digs);
16840 -- Process possible range spec and find correct type to derive from
16842 Process_Real_Range_Specification (Def);
16844 -- Check that requested number of digits is not too high.
16846 if Digs_Val > Max_Digs_Val then
16848 -- The check for Max_Base_Digits may be somewhat expensive, as it
16849 -- requires reading System, so only do it when necessary.
16851 declare
16852 Max_Base_Digits : constant Uint :=
16853 Expr_Value
16854 (Expression
16855 (Parent (RTE (RE_Max_Base_Digits))));
16857 begin
16858 if Digs_Val > Max_Base_Digits then
16859 Error_Msg_Uint_1 := Max_Base_Digits;
16860 Error_Msg_N ("digits value out of range, maximum is ^", Digs);
16862 elsif No (Real_Range_Specification (Def)) then
16863 Error_Msg_Uint_1 := Max_Digs_Val;
16864 Error_Msg_N ("types with more than ^ digits need range spec "
16865 & "(RM 3.5.7(6))", Digs);
16866 end if;
16867 end;
16868 end if;
16870 -- Find a suitable type to derive from or complain and use a substitute
16872 Base_Typ := Find_Base_Type;
16874 -- If there are bounds given in the declaration use them as the bounds
16875 -- of the type, otherwise use the bounds of the predefined base type
16876 -- that was chosen based on the Digits value.
16878 if Present (Real_Range_Specification (Def)) then
16879 Set_Scalar_Range (T, Real_Range_Specification (Def));
16880 Set_Is_Constrained (T);
16882 -- The bounds of this range must be converted to machine numbers
16883 -- in accordance with RM 4.9(38).
16885 Bound := Type_Low_Bound (T);
16887 if Nkind (Bound) = N_Real_Literal then
16888 Set_Realval
16889 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
16890 Set_Is_Machine_Number (Bound);
16891 end if;
16893 Bound := Type_High_Bound (T);
16895 if Nkind (Bound) = N_Real_Literal then
16896 Set_Realval
16897 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
16898 Set_Is_Machine_Number (Bound);
16899 end if;
16901 else
16902 Set_Scalar_Range (T, Scalar_Range (Base_Typ));
16903 end if;
16905 -- Complete definition of implicit base and declared first subtype. The
16906 -- inheritance of the rep item chain ensures that SPARK-related pragmas
16907 -- are not clobbered when the floating point type acts as a full view of
16908 -- a private type.
16910 Set_Etype (Implicit_Base, Base_Typ);
16911 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
16912 Set_Size_Info (Implicit_Base, Base_Typ);
16913 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
16914 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
16915 Set_Digits_Value (Implicit_Base, Digits_Value (Base_Typ));
16916 Set_Float_Rep (Implicit_Base, Float_Rep (Base_Typ));
16918 Set_Ekind (T, E_Floating_Point_Subtype);
16919 Set_Etype (T, Implicit_Base);
16920 Set_Size_Info (T, Implicit_Base);
16921 Set_RM_Size (T, RM_Size (Implicit_Base));
16922 Inherit_Rep_Item_Chain (T, Implicit_Base);
16923 Set_Digits_Value (T, Digs_Val);
16924 end Floating_Point_Type_Declaration;
16926 ----------------------------
16927 -- Get_Discriminant_Value --
16928 ----------------------------
16930 -- This is the situation:
16932 -- There is a non-derived type
16934 -- type T0 (Dx, Dy, Dz...)
16936 -- There are zero or more levels of derivation, with each derivation
16937 -- either purely inheriting the discriminants, or defining its own.
16939 -- type Ti is new Ti-1
16940 -- or
16941 -- type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
16942 -- or
16943 -- subtype Ti is ...
16945 -- The subtype issue is avoided by the use of Original_Record_Component,
16946 -- and the fact that derived subtypes also derive the constraints.
16948 -- This chain leads back from
16950 -- Typ_For_Constraint
16952 -- Typ_For_Constraint has discriminants, and the value for each
16953 -- discriminant is given by its corresponding Elmt of Constraints.
16955 -- Discriminant is some discriminant in this hierarchy
16957 -- We need to return its value
16959 -- We do this by recursively searching each level, and looking for
16960 -- Discriminant. Once we get to the bottom, we start backing up
16961 -- returning the value for it which may in turn be a discriminant
16962 -- further up, so on the backup we continue the substitution.
16964 function Get_Discriminant_Value
16965 (Discriminant : Entity_Id;
16966 Typ_For_Constraint : Entity_Id;
16967 Constraint : Elist_Id) return Node_Id
16969 function Root_Corresponding_Discriminant
16970 (Discr : Entity_Id) return Entity_Id;
16971 -- Given a discriminant, traverse the chain of inherited discriminants
16972 -- and return the topmost discriminant.
16974 function Search_Derivation_Levels
16975 (Ti : Entity_Id;
16976 Discrim_Values : Elist_Id;
16977 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
16978 -- This is the routine that performs the recursive search of levels
16979 -- as described above.
16981 -------------------------------------
16982 -- Root_Corresponding_Discriminant --
16983 -------------------------------------
16985 function Root_Corresponding_Discriminant
16986 (Discr : Entity_Id) return Entity_Id
16988 D : Entity_Id;
16990 begin
16991 D := Discr;
16992 while Present (Corresponding_Discriminant (D)) loop
16993 D := Corresponding_Discriminant (D);
16994 end loop;
16996 return D;
16997 end Root_Corresponding_Discriminant;
16999 ------------------------------
17000 -- Search_Derivation_Levels --
17001 ------------------------------
17003 function Search_Derivation_Levels
17004 (Ti : Entity_Id;
17005 Discrim_Values : Elist_Id;
17006 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
17008 Assoc : Elmt_Id;
17009 Disc : Entity_Id;
17010 Result : Node_Or_Entity_Id;
17011 Result_Entity : Node_Id;
17013 begin
17014 -- If inappropriate type, return Error, this happens only in
17015 -- cascaded error situations, and we want to avoid a blow up.
17017 if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
17018 return Error;
17019 end if;
17021 -- Look deeper if possible. Use Stored_Constraints only for
17022 -- untagged types. For tagged types use the given constraint.
17023 -- This asymmetry needs explanation???
17025 if not Stored_Discrim_Values
17026 and then Present (Stored_Constraint (Ti))
17027 and then not Is_Tagged_Type (Ti)
17028 then
17029 Result :=
17030 Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
17031 else
17032 declare
17033 Td : constant Entity_Id := Etype (Ti);
17035 begin
17036 if Td = Ti then
17037 Result := Discriminant;
17039 else
17040 if Present (Stored_Constraint (Ti)) then
17041 Result :=
17042 Search_Derivation_Levels
17043 (Td, Stored_Constraint (Ti), True);
17044 else
17045 Result :=
17046 Search_Derivation_Levels
17047 (Td, Discrim_Values, Stored_Discrim_Values);
17048 end if;
17049 end if;
17050 end;
17051 end if;
17053 -- Extra underlying places to search, if not found above. For
17054 -- concurrent types, the relevant discriminant appears in the
17055 -- corresponding record. For a type derived from a private type
17056 -- without discriminant, the full view inherits the discriminants
17057 -- of the full view of the parent.
17059 if Result = Discriminant then
17060 if Is_Concurrent_Type (Ti)
17061 and then Present (Corresponding_Record_Type (Ti))
17062 then
17063 Result :=
17064 Search_Derivation_Levels (
17065 Corresponding_Record_Type (Ti),
17066 Discrim_Values,
17067 Stored_Discrim_Values);
17069 elsif Is_Private_Type (Ti)
17070 and then not Has_Discriminants (Ti)
17071 and then Present (Full_View (Ti))
17072 and then Etype (Full_View (Ti)) /= Ti
17073 then
17074 Result :=
17075 Search_Derivation_Levels (
17076 Full_View (Ti),
17077 Discrim_Values,
17078 Stored_Discrim_Values);
17079 end if;
17080 end if;
17082 -- If Result is not a (reference to a) discriminant, return it,
17083 -- otherwise set Result_Entity to the discriminant.
17085 if Nkind (Result) = N_Defining_Identifier then
17086 pragma Assert (Result = Discriminant);
17087 Result_Entity := Result;
17089 else
17090 if not Denotes_Discriminant (Result) then
17091 return Result;
17092 end if;
17094 Result_Entity := Entity (Result);
17095 end if;
17097 -- See if this level of derivation actually has discriminants because
17098 -- tagged derivations can add them, hence the lower levels need not
17099 -- have any.
17101 if not Has_Discriminants (Ti) then
17102 return Result;
17103 end if;
17105 -- Scan Ti's discriminants for Result_Entity, and return its
17106 -- corresponding value, if any.
17108 Result_Entity := Original_Record_Component (Result_Entity);
17110 Assoc := First_Elmt (Discrim_Values);
17112 if Stored_Discrim_Values then
17113 Disc := First_Stored_Discriminant (Ti);
17114 else
17115 Disc := First_Discriminant (Ti);
17116 end if;
17118 while Present (Disc) loop
17119 pragma Assert (Present (Assoc));
17121 if Original_Record_Component (Disc) = Result_Entity then
17122 return Node (Assoc);
17123 end if;
17125 Next_Elmt (Assoc);
17127 if Stored_Discrim_Values then
17128 Next_Stored_Discriminant (Disc);
17129 else
17130 Next_Discriminant (Disc);
17131 end if;
17132 end loop;
17134 -- Could not find it
17136 return Result;
17137 end Search_Derivation_Levels;
17139 -- Local Variables
17141 Result : Node_Or_Entity_Id;
17143 -- Start of processing for Get_Discriminant_Value
17145 begin
17146 -- ??? This routine is a gigantic mess and will be deleted. For the
17147 -- time being just test for the trivial case before calling recurse.
17149 if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
17150 declare
17151 D : Entity_Id;
17152 E : Elmt_Id;
17154 begin
17155 D := First_Discriminant (Typ_For_Constraint);
17156 E := First_Elmt (Constraint);
17157 while Present (D) loop
17158 if Chars (D) = Chars (Discriminant) then
17159 return Node (E);
17160 end if;
17162 Next_Discriminant (D);
17163 Next_Elmt (E);
17164 end loop;
17165 end;
17166 end if;
17168 Result := Search_Derivation_Levels
17169 (Typ_For_Constraint, Constraint, False);
17171 -- ??? hack to disappear when this routine is gone
17173 if Nkind (Result) = N_Defining_Identifier then
17174 declare
17175 D : Entity_Id;
17176 E : Elmt_Id;
17178 begin
17179 D := First_Discriminant (Typ_For_Constraint);
17180 E := First_Elmt (Constraint);
17181 while Present (D) loop
17182 if Root_Corresponding_Discriminant (D) = Discriminant then
17183 return Node (E);
17184 end if;
17186 Next_Discriminant (D);
17187 Next_Elmt (E);
17188 end loop;
17189 end;
17190 end if;
17192 pragma Assert (Nkind (Result) /= N_Defining_Identifier);
17193 return Result;
17194 end Get_Discriminant_Value;
17196 --------------------------
17197 -- Has_Range_Constraint --
17198 --------------------------
17200 function Has_Range_Constraint (N : Node_Id) return Boolean is
17201 C : constant Node_Id := Constraint (N);
17203 begin
17204 if Nkind (C) = N_Range_Constraint then
17205 return True;
17207 elsif Nkind (C) = N_Digits_Constraint then
17208 return
17209 Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
17210 or else Present (Range_Constraint (C));
17212 elsif Nkind (C) = N_Delta_Constraint then
17213 return Present (Range_Constraint (C));
17215 else
17216 return False;
17217 end if;
17218 end Has_Range_Constraint;
17220 ------------------------
17221 -- Inherit_Components --
17222 ------------------------
17224 function Inherit_Components
17225 (N : Node_Id;
17226 Parent_Base : Entity_Id;
17227 Derived_Base : Entity_Id;
17228 Is_Tagged : Boolean;
17229 Inherit_Discr : Boolean;
17230 Discs : Elist_Id) return Elist_Id
17232 Assoc_List : constant Elist_Id := New_Elmt_List;
17234 procedure Inherit_Component
17235 (Old_C : Entity_Id;
17236 Plain_Discrim : Boolean := False;
17237 Stored_Discrim : Boolean := False);
17238 -- Inherits component Old_C from Parent_Base to the Derived_Base. If
17239 -- Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
17240 -- True, Old_C is a stored discriminant. If they are both false then
17241 -- Old_C is a regular component.
17243 -----------------------
17244 -- Inherit_Component --
17245 -----------------------
17247 procedure Inherit_Component
17248 (Old_C : Entity_Id;
17249 Plain_Discrim : Boolean := False;
17250 Stored_Discrim : Boolean := False)
17252 procedure Set_Anonymous_Type (Id : Entity_Id);
17253 -- Id denotes the entity of an access discriminant or anonymous
17254 -- access component. Set the type of Id to either the same type of
17255 -- Old_C or create a new one depending on whether the parent and
17256 -- the child types are in the same scope.
17258 ------------------------
17259 -- Set_Anonymous_Type --
17260 ------------------------
17262 procedure Set_Anonymous_Type (Id : Entity_Id) is
17263 Old_Typ : constant Entity_Id := Etype (Old_C);
17265 begin
17266 if Scope (Parent_Base) = Scope (Derived_Base) then
17267 Set_Etype (Id, Old_Typ);
17269 -- The parent and the derived type are in two different scopes.
17270 -- Reuse the type of the original discriminant / component by
17271 -- copying it in order to preserve all attributes.
17273 else
17274 declare
17275 Typ : constant Entity_Id := New_Copy (Old_Typ);
17277 begin
17278 Set_Etype (Id, Typ);
17280 -- Since we do not generate component declarations for
17281 -- inherited components, associate the itype with the
17282 -- derived type.
17284 Set_Associated_Node_For_Itype (Typ, Parent (Derived_Base));
17285 Set_Scope (Typ, Derived_Base);
17286 end;
17287 end if;
17288 end Set_Anonymous_Type;
17290 -- Local variables and constants
17292 New_C : constant Entity_Id := New_Copy (Old_C);
17294 Corr_Discrim : Entity_Id;
17295 Discrim : Entity_Id;
17297 -- Start of processing for Inherit_Component
17299 begin
17300 pragma Assert (not Is_Tagged or not Stored_Discrim);
17302 Set_Parent (New_C, Parent (Old_C));
17304 -- Regular discriminants and components must be inserted in the scope
17305 -- of the Derived_Base. Do it here.
17307 if not Stored_Discrim then
17308 Enter_Name (New_C);
17309 end if;
17311 -- For tagged types the Original_Record_Component must point to
17312 -- whatever this field was pointing to in the parent type. This has
17313 -- already been achieved by the call to New_Copy above.
17315 if not Is_Tagged then
17316 Set_Original_Record_Component (New_C, New_C);
17317 end if;
17319 -- Set the proper type of an access discriminant
17321 if Ekind (New_C) = E_Discriminant
17322 and then Ekind (Etype (New_C)) = E_Anonymous_Access_Type
17323 then
17324 Set_Anonymous_Type (New_C);
17325 end if;
17327 -- If we have inherited a component then see if its Etype contains
17328 -- references to Parent_Base discriminants. In this case, replace
17329 -- these references with the constraints given in Discs. We do not
17330 -- do this for the partial view of private types because this is
17331 -- not needed (only the components of the full view will be used
17332 -- for code generation) and cause problem. We also avoid this
17333 -- transformation in some error situations.
17335 if Ekind (New_C) = E_Component then
17337 -- Set the proper type of an anonymous access component
17339 if Ekind (Etype (New_C)) = E_Anonymous_Access_Type then
17340 Set_Anonymous_Type (New_C);
17342 elsif (Is_Private_Type (Derived_Base)
17343 and then not Is_Generic_Type (Derived_Base))
17344 or else (Is_Empty_Elmt_List (Discs)
17345 and then not Expander_Active)
17346 then
17347 Set_Etype (New_C, Etype (Old_C));
17349 else
17350 -- The current component introduces a circularity of the
17351 -- following kind:
17353 -- limited with Pack_2;
17354 -- package Pack_1 is
17355 -- type T_1 is tagged record
17356 -- Comp : access Pack_2.T_2;
17357 -- ...
17358 -- end record;
17359 -- end Pack_1;
17361 -- with Pack_1;
17362 -- package Pack_2 is
17363 -- type T_2 is new Pack_1.T_1 with ...;
17364 -- end Pack_2;
17366 Set_Etype
17367 (New_C,
17368 Constrain_Component_Type
17369 (Old_C, Derived_Base, N, Parent_Base, Discs));
17370 end if;
17371 end if;
17373 -- In derived tagged types it is illegal to reference a non
17374 -- discriminant component in the parent type. To catch this, mark
17375 -- these components with an Ekind of E_Void. This will be reset in
17376 -- Record_Type_Definition after processing the record extension of
17377 -- the derived type.
17379 -- If the declaration is a private extension, there is no further
17380 -- record extension to process, and the components retain their
17381 -- current kind, because they are visible at this point.
17383 if Is_Tagged and then Ekind (New_C) = E_Component
17384 and then Nkind (N) /= N_Private_Extension_Declaration
17385 then
17386 Set_Ekind (New_C, E_Void);
17387 end if;
17389 if Plain_Discrim then
17390 Set_Corresponding_Discriminant (New_C, Old_C);
17391 Build_Discriminal (New_C);
17393 -- If we are explicitly inheriting a stored discriminant it will be
17394 -- completely hidden.
17396 elsif Stored_Discrim then
17397 Set_Corresponding_Discriminant (New_C, Empty);
17398 Set_Discriminal (New_C, Empty);
17399 Set_Is_Completely_Hidden (New_C);
17401 -- Set the Original_Record_Component of each discriminant in the
17402 -- derived base to point to the corresponding stored that we just
17403 -- created.
17405 Discrim := First_Discriminant (Derived_Base);
17406 while Present (Discrim) loop
17407 Corr_Discrim := Corresponding_Discriminant (Discrim);
17409 -- Corr_Discrim could be missing in an error situation
17411 if Present (Corr_Discrim)
17412 and then Original_Record_Component (Corr_Discrim) = Old_C
17413 then
17414 Set_Original_Record_Component (Discrim, New_C);
17415 end if;
17417 Next_Discriminant (Discrim);
17418 end loop;
17420 Append_Entity (New_C, Derived_Base);
17421 end if;
17423 if not Is_Tagged then
17424 Append_Elmt (Old_C, Assoc_List);
17425 Append_Elmt (New_C, Assoc_List);
17426 end if;
17427 end Inherit_Component;
17429 -- Variables local to Inherit_Component
17431 Loc : constant Source_Ptr := Sloc (N);
17433 Parent_Discrim : Entity_Id;
17434 Stored_Discrim : Entity_Id;
17435 D : Entity_Id;
17436 Component : Entity_Id;
17438 -- Start of processing for Inherit_Components
17440 begin
17441 if not Is_Tagged then
17442 Append_Elmt (Parent_Base, Assoc_List);
17443 Append_Elmt (Derived_Base, Assoc_List);
17444 end if;
17446 -- Inherit parent discriminants if needed
17448 if Inherit_Discr then
17449 Parent_Discrim := First_Discriminant (Parent_Base);
17450 while Present (Parent_Discrim) loop
17451 Inherit_Component (Parent_Discrim, Plain_Discrim => True);
17452 Next_Discriminant (Parent_Discrim);
17453 end loop;
17454 end if;
17456 -- Create explicit stored discrims for untagged types when necessary
17458 if not Has_Unknown_Discriminants (Derived_Base)
17459 and then Has_Discriminants (Parent_Base)
17460 and then not Is_Tagged
17461 and then
17462 (not Inherit_Discr
17463 or else First_Discriminant (Parent_Base) /=
17464 First_Stored_Discriminant (Parent_Base))
17465 then
17466 Stored_Discrim := First_Stored_Discriminant (Parent_Base);
17467 while Present (Stored_Discrim) loop
17468 Inherit_Component (Stored_Discrim, Stored_Discrim => True);
17469 Next_Stored_Discriminant (Stored_Discrim);
17470 end loop;
17471 end if;
17473 -- See if we can apply the second transformation for derived types, as
17474 -- explained in point 6. in the comments above Build_Derived_Record_Type
17475 -- This is achieved by appending Derived_Base discriminants into Discs,
17476 -- which has the side effect of returning a non empty Discs list to the
17477 -- caller of Inherit_Components, which is what we want. This must be
17478 -- done for private derived types if there are explicit stored
17479 -- discriminants, to ensure that we can retrieve the values of the
17480 -- constraints provided in the ancestors.
17482 if Inherit_Discr
17483 and then Is_Empty_Elmt_List (Discs)
17484 and then Present (First_Discriminant (Derived_Base))
17485 and then
17486 (not Is_Private_Type (Derived_Base)
17487 or else Is_Completely_Hidden
17488 (First_Stored_Discriminant (Derived_Base))
17489 or else Is_Generic_Type (Derived_Base))
17490 then
17491 D := First_Discriminant (Derived_Base);
17492 while Present (D) loop
17493 Append_Elmt (New_Occurrence_Of (D, Loc), Discs);
17494 Next_Discriminant (D);
17495 end loop;
17496 end if;
17498 -- Finally, inherit non-discriminant components unless they are not
17499 -- visible because defined or inherited from the full view of the
17500 -- parent. Don't inherit the _parent field of the parent type.
17502 Component := First_Entity (Parent_Base);
17503 while Present (Component) loop
17505 -- Ada 2005 (AI-251): Do not inherit components associated with
17506 -- secondary tags of the parent.
17508 if Ekind (Component) = E_Component
17509 and then Present (Related_Type (Component))
17510 then
17511 null;
17513 elsif Ekind (Component) /= E_Component
17514 or else Chars (Component) = Name_uParent
17515 then
17516 null;
17518 -- If the derived type is within the parent type's declarative
17519 -- region, then the components can still be inherited even though
17520 -- they aren't visible at this point. This can occur for cases
17521 -- such as within public child units where the components must
17522 -- become visible upon entering the child unit's private part.
17524 elsif not Is_Visible_Component (Component)
17525 and then not In_Open_Scopes (Scope (Parent_Base))
17526 then
17527 null;
17529 elsif Ekind_In (Derived_Base, E_Private_Type,
17530 E_Limited_Private_Type)
17531 then
17532 null;
17534 else
17535 Inherit_Component (Component);
17536 end if;
17538 Next_Entity (Component);
17539 end loop;
17541 -- For tagged derived types, inherited discriminants cannot be used in
17542 -- component declarations of the record extension part. To achieve this
17543 -- we mark the inherited discriminants as not visible.
17545 if Is_Tagged and then Inherit_Discr then
17546 D := First_Discriminant (Derived_Base);
17547 while Present (D) loop
17548 Set_Is_Immediately_Visible (D, False);
17549 Next_Discriminant (D);
17550 end loop;
17551 end if;
17553 return Assoc_List;
17554 end Inherit_Components;
17556 -----------------------------
17557 -- Inherit_Predicate_Flags --
17558 -----------------------------
17560 procedure Inherit_Predicate_Flags (Subt, Par : Entity_Id) is
17561 begin
17562 Set_Has_Predicates (Subt, Has_Predicates (Par));
17563 Set_Has_Static_Predicate_Aspect
17564 (Subt, Has_Static_Predicate_Aspect (Par));
17565 Set_Has_Dynamic_Predicate_Aspect
17566 (Subt, Has_Dynamic_Predicate_Aspect (Par));
17567 end Inherit_Predicate_Flags;
17569 ----------------------
17570 -- Is_EVF_Procedure --
17571 ----------------------
17573 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean is
17574 Formal : Entity_Id;
17576 begin
17577 -- Examine the formals of an Extensions_Visible False procedure looking
17578 -- for a controlling OUT parameter.
17580 if Ekind (Subp) = E_Procedure
17581 and then Extensions_Visible_Status (Subp) = Extensions_Visible_False
17582 then
17583 Formal := First_Formal (Subp);
17584 while Present (Formal) loop
17585 if Ekind (Formal) = E_Out_Parameter
17586 and then Is_Controlling_Formal (Formal)
17587 then
17588 return True;
17589 end if;
17591 Next_Formal (Formal);
17592 end loop;
17593 end if;
17595 return False;
17596 end Is_EVF_Procedure;
17598 -----------------------
17599 -- Is_Null_Extension --
17600 -----------------------
17602 function Is_Null_Extension (T : Entity_Id) return Boolean is
17603 Type_Decl : constant Node_Id := Parent (Base_Type (T));
17604 Comp_List : Node_Id;
17605 Comp : Node_Id;
17607 begin
17608 if Nkind (Type_Decl) /= N_Full_Type_Declaration
17609 or else not Is_Tagged_Type (T)
17610 or else Nkind (Type_Definition (Type_Decl)) /=
17611 N_Derived_Type_Definition
17612 or else No (Record_Extension_Part (Type_Definition (Type_Decl)))
17613 then
17614 return False;
17615 end if;
17617 Comp_List :=
17618 Component_List (Record_Extension_Part (Type_Definition (Type_Decl)));
17620 if Present (Discriminant_Specifications (Type_Decl)) then
17621 return False;
17623 elsif Present (Comp_List)
17624 and then Is_Non_Empty_List (Component_Items (Comp_List))
17625 then
17626 Comp := First (Component_Items (Comp_List));
17628 -- Only user-defined components are relevant. The component list
17629 -- may also contain a parent component and internal components
17630 -- corresponding to secondary tags, but these do not determine
17631 -- whether this is a null extension.
17633 while Present (Comp) loop
17634 if Comes_From_Source (Comp) then
17635 return False;
17636 end if;
17638 Next (Comp);
17639 end loop;
17641 return True;
17643 else
17644 return True;
17645 end if;
17646 end Is_Null_Extension;
17648 ------------------------------
17649 -- Is_Valid_Constraint_Kind --
17650 ------------------------------
17652 function Is_Valid_Constraint_Kind
17653 (T_Kind : Type_Kind;
17654 Constraint_Kind : Node_Kind) return Boolean
17656 begin
17657 case T_Kind is
17658 when Enumeration_Kind |
17659 Integer_Kind =>
17660 return Constraint_Kind = N_Range_Constraint;
17662 when Decimal_Fixed_Point_Kind =>
17663 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
17664 N_Range_Constraint);
17666 when Ordinary_Fixed_Point_Kind =>
17667 return Nkind_In (Constraint_Kind, N_Delta_Constraint,
17668 N_Range_Constraint);
17670 when Float_Kind =>
17671 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
17672 N_Range_Constraint);
17674 when Access_Kind |
17675 Array_Kind |
17676 E_Record_Type |
17677 E_Record_Subtype |
17678 Class_Wide_Kind |
17679 E_Incomplete_Type |
17680 Private_Kind |
17681 Concurrent_Kind =>
17682 return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
17684 when others =>
17685 return True; -- Error will be detected later
17686 end case;
17687 end Is_Valid_Constraint_Kind;
17689 --------------------------
17690 -- Is_Visible_Component --
17691 --------------------------
17693 function Is_Visible_Component
17694 (C : Entity_Id;
17695 N : Node_Id := Empty) return Boolean
17697 Original_Comp : Entity_Id := Empty;
17698 Original_Scope : Entity_Id;
17699 Type_Scope : Entity_Id;
17701 function Is_Local_Type (Typ : Entity_Id) return Boolean;
17702 -- Check whether parent type of inherited component is declared locally,
17703 -- possibly within a nested package or instance. The current scope is
17704 -- the derived record itself.
17706 -------------------
17707 -- Is_Local_Type --
17708 -------------------
17710 function Is_Local_Type (Typ : Entity_Id) return Boolean is
17711 Scop : Entity_Id;
17713 begin
17714 Scop := Scope (Typ);
17715 while Present (Scop)
17716 and then Scop /= Standard_Standard
17717 loop
17718 if Scop = Scope (Current_Scope) then
17719 return True;
17720 end if;
17722 Scop := Scope (Scop);
17723 end loop;
17725 return False;
17726 end Is_Local_Type;
17728 -- Start of processing for Is_Visible_Component
17730 begin
17731 if Ekind_In (C, E_Component, E_Discriminant) then
17732 Original_Comp := Original_Record_Component (C);
17733 end if;
17735 if No (Original_Comp) then
17737 -- Premature usage, or previous error
17739 return False;
17741 else
17742 Original_Scope := Scope (Original_Comp);
17743 Type_Scope := Scope (Base_Type (Scope (C)));
17744 end if;
17746 -- This test only concerns tagged types
17748 if not Is_Tagged_Type (Original_Scope) then
17749 return True;
17751 -- If it is _Parent or _Tag, there is no visibility issue
17753 elsif not Comes_From_Source (Original_Comp) then
17754 return True;
17756 -- Discriminants are visible unless the (private) type has unknown
17757 -- discriminants. If the discriminant reference is inserted for a
17758 -- discriminant check on a full view it is also visible.
17760 elsif Ekind (Original_Comp) = E_Discriminant
17761 and then
17762 (not Has_Unknown_Discriminants (Original_Scope)
17763 or else (Present (N)
17764 and then Nkind (N) = N_Selected_Component
17765 and then Nkind (Prefix (N)) = N_Type_Conversion
17766 and then not Comes_From_Source (Prefix (N))))
17767 then
17768 return True;
17770 -- In the body of an instantiation, no need to check for the visibility
17771 -- of a component.
17773 elsif In_Instance_Body then
17774 return True;
17776 -- If the component has been declared in an ancestor which is currently
17777 -- a private type, then it is not visible. The same applies if the
17778 -- component's containing type is not in an open scope and the original
17779 -- component's enclosing type is a visible full view of a private type
17780 -- (which can occur in cases where an attempt is being made to reference
17781 -- a component in a sibling package that is inherited from a visible
17782 -- component of a type in an ancestor package; the component in the
17783 -- sibling package should not be visible even though the component it
17784 -- inherited from is visible). This does not apply however in the case
17785 -- where the scope of the type is a private child unit, or when the
17786 -- parent comes from a local package in which the ancestor is currently
17787 -- visible. The latter suppression of visibility is needed for cases
17788 -- that are tested in B730006.
17790 elsif Is_Private_Type (Original_Scope)
17791 or else
17792 (not Is_Private_Descendant (Type_Scope)
17793 and then not In_Open_Scopes (Type_Scope)
17794 and then Has_Private_Declaration (Original_Scope))
17795 then
17796 -- If the type derives from an entity in a formal package, there
17797 -- are no additional visible components.
17799 if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
17800 N_Formal_Package_Declaration
17801 then
17802 return False;
17804 -- if we are not in the private part of the current package, there
17805 -- are no additional visible components.
17807 elsif Ekind (Scope (Current_Scope)) = E_Package
17808 and then not In_Private_Part (Scope (Current_Scope))
17809 then
17810 return False;
17811 else
17812 return
17813 Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
17814 and then In_Open_Scopes (Scope (Original_Scope))
17815 and then Is_Local_Type (Type_Scope);
17816 end if;
17818 -- There is another weird way in which a component may be invisible when
17819 -- the private and the full view are not derived from the same ancestor.
17820 -- Here is an example :
17822 -- type A1 is tagged record F1 : integer; end record;
17823 -- type A2 is new A1 with record F2 : integer; end record;
17824 -- type T is new A1 with private;
17825 -- private
17826 -- type T is new A2 with null record;
17828 -- In this case, the full view of T inherits F1 and F2 but the private
17829 -- view inherits only F1
17831 else
17832 declare
17833 Ancestor : Entity_Id := Scope (C);
17835 begin
17836 loop
17837 if Ancestor = Original_Scope then
17838 return True;
17839 elsif Ancestor = Etype (Ancestor) then
17840 return False;
17841 end if;
17843 Ancestor := Etype (Ancestor);
17844 end loop;
17845 end;
17846 end if;
17847 end Is_Visible_Component;
17849 --------------------------
17850 -- Make_Class_Wide_Type --
17851 --------------------------
17853 procedure Make_Class_Wide_Type (T : Entity_Id) is
17854 CW_Type : Entity_Id;
17855 CW_Name : Name_Id;
17856 Next_E : Entity_Id;
17858 begin
17859 if Present (Class_Wide_Type (T)) then
17861 -- The class-wide type is a partially decorated entity created for a
17862 -- unanalyzed tagged type referenced through a limited with clause.
17863 -- When the tagged type is analyzed, its class-wide type needs to be
17864 -- redecorated. Note that we reuse the entity created by Decorate_
17865 -- Tagged_Type in order to preserve all links.
17867 if Materialize_Entity (Class_Wide_Type (T)) then
17868 CW_Type := Class_Wide_Type (T);
17869 Set_Materialize_Entity (CW_Type, False);
17871 -- The class wide type can have been defined by the partial view, in
17872 -- which case everything is already done.
17874 else
17875 return;
17876 end if;
17878 -- Default case, we need to create a new class-wide type
17880 else
17881 CW_Type :=
17882 New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
17883 end if;
17885 -- Inherit root type characteristics
17887 CW_Name := Chars (CW_Type);
17888 Next_E := Next_Entity (CW_Type);
17889 Copy_Node (T, CW_Type);
17890 Set_Comes_From_Source (CW_Type, False);
17891 Set_Chars (CW_Type, CW_Name);
17892 Set_Parent (CW_Type, Parent (T));
17893 Set_Next_Entity (CW_Type, Next_E);
17895 -- Ensure we have a new freeze node for the class-wide type. The partial
17896 -- view may have freeze action of its own, requiring a proper freeze
17897 -- node, and the same freeze node cannot be shared between the two
17898 -- types.
17900 Set_Has_Delayed_Freeze (CW_Type);
17901 Set_Freeze_Node (CW_Type, Empty);
17903 -- Customize the class-wide type: It has no prim. op., it cannot be
17904 -- abstract and its Etype points back to the specific root type.
17906 Set_Ekind (CW_Type, E_Class_Wide_Type);
17907 Set_Is_Tagged_Type (CW_Type, True);
17908 Set_Direct_Primitive_Operations (CW_Type, New_Elmt_List);
17909 Set_Is_Abstract_Type (CW_Type, False);
17910 Set_Is_Constrained (CW_Type, False);
17911 Set_Is_First_Subtype (CW_Type, Is_First_Subtype (T));
17912 Set_Default_SSO (CW_Type);
17914 if Ekind (T) = E_Class_Wide_Subtype then
17915 Set_Etype (CW_Type, Etype (Base_Type (T)));
17916 else
17917 Set_Etype (CW_Type, T);
17918 end if;
17920 Set_No_Tagged_Streams_Pragma (CW_Type, No_Tagged_Streams);
17922 -- If this is the class_wide type of a constrained subtype, it does
17923 -- not have discriminants.
17925 Set_Has_Discriminants (CW_Type,
17926 Has_Discriminants (T) and then not Is_Constrained (T));
17928 Set_Has_Unknown_Discriminants (CW_Type, True);
17929 Set_Class_Wide_Type (T, CW_Type);
17930 Set_Equivalent_Type (CW_Type, Empty);
17932 -- The class-wide type of a class-wide type is itself (RM 3.9(14))
17934 Set_Class_Wide_Type (CW_Type, CW_Type);
17935 end Make_Class_Wide_Type;
17937 ----------------
17938 -- Make_Index --
17939 ----------------
17941 procedure Make_Index
17942 (N : Node_Id;
17943 Related_Nod : Node_Id;
17944 Related_Id : Entity_Id := Empty;
17945 Suffix_Index : Nat := 1;
17946 In_Iter_Schm : Boolean := False)
17948 R : Node_Id;
17949 T : Entity_Id;
17950 Def_Id : Entity_Id := Empty;
17951 Found : Boolean := False;
17953 begin
17954 -- For a discrete range used in a constrained array definition and
17955 -- defined by a range, an implicit conversion to the predefined type
17956 -- INTEGER is assumed if each bound is either a numeric literal, a named
17957 -- number, or an attribute, and the type of both bounds (prior to the
17958 -- implicit conversion) is the type universal_integer. Otherwise, both
17959 -- bounds must be of the same discrete type, other than universal
17960 -- integer; this type must be determinable independently of the
17961 -- context, but using the fact that the type must be discrete and that
17962 -- both bounds must have the same type.
17964 -- Character literals also have a universal type in the absence of
17965 -- of additional context, and are resolved to Standard_Character.
17967 if Nkind (N) = N_Range then
17969 -- The index is given by a range constraint. The bounds are known
17970 -- to be of a consistent type.
17972 if not Is_Overloaded (N) then
17973 T := Etype (N);
17975 -- For universal bounds, choose the specific predefined type
17977 if T = Universal_Integer then
17978 T := Standard_Integer;
17980 elsif T = Any_Character then
17981 Ambiguous_Character (Low_Bound (N));
17983 T := Standard_Character;
17984 end if;
17986 -- The node may be overloaded because some user-defined operators
17987 -- are available, but if a universal interpretation exists it is
17988 -- also the selected one.
17990 elsif Universal_Interpretation (N) = Universal_Integer then
17991 T := Standard_Integer;
17993 else
17994 T := Any_Type;
17996 declare
17997 Ind : Interp_Index;
17998 It : Interp;
18000 begin
18001 Get_First_Interp (N, Ind, It);
18002 while Present (It.Typ) loop
18003 if Is_Discrete_Type (It.Typ) then
18005 if Found
18006 and then not Covers (It.Typ, T)
18007 and then not Covers (T, It.Typ)
18008 then
18009 Error_Msg_N ("ambiguous bounds in discrete range", N);
18010 exit;
18011 else
18012 T := It.Typ;
18013 Found := True;
18014 end if;
18015 end if;
18017 Get_Next_Interp (Ind, It);
18018 end loop;
18020 if T = Any_Type then
18021 Error_Msg_N ("discrete type required for range", N);
18022 Set_Etype (N, Any_Type);
18023 return;
18025 elsif T = Universal_Integer then
18026 T := Standard_Integer;
18027 end if;
18028 end;
18029 end if;
18031 if not Is_Discrete_Type (T) then
18032 Error_Msg_N ("discrete type required for range", N);
18033 Set_Etype (N, Any_Type);
18034 return;
18035 end if;
18037 if Nkind (Low_Bound (N)) = N_Attribute_Reference
18038 and then Attribute_Name (Low_Bound (N)) = Name_First
18039 and then Is_Entity_Name (Prefix (Low_Bound (N)))
18040 and then Is_Type (Entity (Prefix (Low_Bound (N))))
18041 and then Is_Discrete_Type (Entity (Prefix (Low_Bound (N))))
18042 then
18043 -- The type of the index will be the type of the prefix, as long
18044 -- as the upper bound is 'Last of the same type.
18046 Def_Id := Entity (Prefix (Low_Bound (N)));
18048 if Nkind (High_Bound (N)) /= N_Attribute_Reference
18049 or else Attribute_Name (High_Bound (N)) /= Name_Last
18050 or else not Is_Entity_Name (Prefix (High_Bound (N)))
18051 or else Entity (Prefix (High_Bound (N))) /= Def_Id
18052 then
18053 Def_Id := Empty;
18054 end if;
18055 end if;
18057 R := N;
18058 Process_Range_Expr_In_Decl (R, T, In_Iter_Schm => In_Iter_Schm);
18060 elsif Nkind (N) = N_Subtype_Indication then
18062 -- The index is given by a subtype with a range constraint
18064 T := Base_Type (Entity (Subtype_Mark (N)));
18066 if not Is_Discrete_Type (T) then
18067 Error_Msg_N ("discrete type required for range", N);
18068 Set_Etype (N, Any_Type);
18069 return;
18070 end if;
18072 R := Range_Expression (Constraint (N));
18074 Resolve (R, T);
18075 Process_Range_Expr_In_Decl
18076 (R, Entity (Subtype_Mark (N)), In_Iter_Schm => In_Iter_Schm);
18078 elsif Nkind (N) = N_Attribute_Reference then
18080 -- Catch beginner's error (use of attribute other than 'Range)
18082 if Attribute_Name (N) /= Name_Range then
18083 Error_Msg_N ("expect attribute ''Range", N);
18084 Set_Etype (N, Any_Type);
18085 return;
18086 end if;
18088 -- If the node denotes the range of a type mark, that is also the
18089 -- resulting type, and we do not need to create an Itype for it.
18091 if Is_Entity_Name (Prefix (N))
18092 and then Comes_From_Source (N)
18093 and then Is_Type (Entity (Prefix (N)))
18094 and then Is_Discrete_Type (Entity (Prefix (N)))
18095 then
18096 Def_Id := Entity (Prefix (N));
18097 end if;
18099 Analyze_And_Resolve (N);
18100 T := Etype (N);
18101 R := N;
18103 -- If none of the above, must be a subtype. We convert this to a
18104 -- range attribute reference because in the case of declared first
18105 -- named subtypes, the types in the range reference can be different
18106 -- from the type of the entity. A range attribute normalizes the
18107 -- reference and obtains the correct types for the bounds.
18109 -- This transformation is in the nature of an expansion, is only
18110 -- done if expansion is active. In particular, it is not done on
18111 -- formal generic types, because we need to retain the name of the
18112 -- original index for instantiation purposes.
18114 else
18115 if not Is_Entity_Name (N) or else not Is_Type (Entity (N)) then
18116 Error_Msg_N ("invalid subtype mark in discrete range ", N);
18117 Set_Etype (N, Any_Integer);
18118 return;
18120 else
18121 -- The type mark may be that of an incomplete type. It is only
18122 -- now that we can get the full view, previous analysis does
18123 -- not look specifically for a type mark.
18125 Set_Entity (N, Get_Full_View (Entity (N)));
18126 Set_Etype (N, Entity (N));
18127 Def_Id := Entity (N);
18129 if not Is_Discrete_Type (Def_Id) then
18130 Error_Msg_N ("discrete type required for index", N);
18131 Set_Etype (N, Any_Type);
18132 return;
18133 end if;
18134 end if;
18136 if Expander_Active then
18137 Rewrite (N,
18138 Make_Attribute_Reference (Sloc (N),
18139 Attribute_Name => Name_Range,
18140 Prefix => Relocate_Node (N)));
18142 -- The original was a subtype mark that does not freeze. This
18143 -- means that the rewritten version must not freeze either.
18145 Set_Must_Not_Freeze (N);
18146 Set_Must_Not_Freeze (Prefix (N));
18147 Analyze_And_Resolve (N);
18148 T := Etype (N);
18149 R := N;
18151 -- If expander is inactive, type is legal, nothing else to construct
18153 else
18154 return;
18155 end if;
18156 end if;
18158 if not Is_Discrete_Type (T) then
18159 Error_Msg_N ("discrete type required for range", N);
18160 Set_Etype (N, Any_Type);
18161 return;
18163 elsif T = Any_Type then
18164 Set_Etype (N, Any_Type);
18165 return;
18166 end if;
18168 -- We will now create the appropriate Itype to describe the range, but
18169 -- first a check. If we originally had a subtype, then we just label
18170 -- the range with this subtype. Not only is there no need to construct
18171 -- a new subtype, but it is wrong to do so for two reasons:
18173 -- 1. A legality concern, if we have a subtype, it must not freeze,
18174 -- and the Itype would cause freezing incorrectly
18176 -- 2. An efficiency concern, if we created an Itype, it would not be
18177 -- recognized as the same type for the purposes of eliminating
18178 -- checks in some circumstances.
18180 -- We signal this case by setting the subtype entity in Def_Id
18182 if No (Def_Id) then
18183 Def_Id :=
18184 Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
18185 Set_Etype (Def_Id, Base_Type (T));
18187 if Is_Signed_Integer_Type (T) then
18188 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
18190 elsif Is_Modular_Integer_Type (T) then
18191 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
18193 else
18194 Set_Ekind (Def_Id, E_Enumeration_Subtype);
18195 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
18196 Set_First_Literal (Def_Id, First_Literal (T));
18197 end if;
18199 Set_Size_Info (Def_Id, (T));
18200 Set_RM_Size (Def_Id, RM_Size (T));
18201 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
18203 Set_Scalar_Range (Def_Id, R);
18204 Conditional_Delay (Def_Id, T);
18206 if Nkind (N) = N_Subtype_Indication then
18207 Inherit_Predicate_Flags (Def_Id, Entity (Subtype_Mark (N)));
18208 end if;
18210 -- In the subtype indication case, if the immediate parent of the
18211 -- new subtype is non-static, then the subtype we create is non-
18212 -- static, even if its bounds are static.
18214 if Nkind (N) = N_Subtype_Indication
18215 and then not Is_OK_Static_Subtype (Entity (Subtype_Mark (N)))
18216 then
18217 Set_Is_Non_Static_Subtype (Def_Id);
18218 end if;
18219 end if;
18221 -- Final step is to label the index with this constructed type
18223 Set_Etype (N, Def_Id);
18224 end Make_Index;
18226 ------------------------------
18227 -- Modular_Type_Declaration --
18228 ------------------------------
18230 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
18231 Mod_Expr : constant Node_Id := Expression (Def);
18232 M_Val : Uint;
18234 procedure Set_Modular_Size (Bits : Int);
18235 -- Sets RM_Size to Bits, and Esize to normal word size above this
18237 ----------------------
18238 -- Set_Modular_Size --
18239 ----------------------
18241 procedure Set_Modular_Size (Bits : Int) is
18242 begin
18243 Set_RM_Size (T, UI_From_Int (Bits));
18245 if Bits <= 8 then
18246 Init_Esize (T, 8);
18248 elsif Bits <= 16 then
18249 Init_Esize (T, 16);
18251 elsif Bits <= 32 then
18252 Init_Esize (T, 32);
18254 else
18255 Init_Esize (T, System_Max_Binary_Modulus_Power);
18256 end if;
18258 if not Non_Binary_Modulus (T) and then Esize (T) = RM_Size (T) then
18259 Set_Is_Known_Valid (T);
18260 end if;
18261 end Set_Modular_Size;
18263 -- Start of processing for Modular_Type_Declaration
18265 begin
18266 -- If the mod expression is (exactly) 2 * literal, where literal is
18267 -- 64 or less,then almost certainly the * was meant to be **. Warn.
18269 if Warn_On_Suspicious_Modulus_Value
18270 and then Nkind (Mod_Expr) = N_Op_Multiply
18271 and then Nkind (Left_Opnd (Mod_Expr)) = N_Integer_Literal
18272 and then Intval (Left_Opnd (Mod_Expr)) = Uint_2
18273 and then Nkind (Right_Opnd (Mod_Expr)) = N_Integer_Literal
18274 and then Intval (Right_Opnd (Mod_Expr)) <= Uint_64
18275 then
18276 Error_Msg_N
18277 ("suspicious MOD value, was '*'* intended'??M?", Mod_Expr);
18278 end if;
18280 -- Proceed with analysis of mod expression
18282 Analyze_And_Resolve (Mod_Expr, Any_Integer);
18283 Set_Etype (T, T);
18284 Set_Ekind (T, E_Modular_Integer_Type);
18285 Init_Alignment (T);
18286 Set_Is_Constrained (T);
18288 if not Is_OK_Static_Expression (Mod_Expr) then
18289 Flag_Non_Static_Expr
18290 ("non-static expression used for modular type bound!", Mod_Expr);
18291 M_Val := 2 ** System_Max_Binary_Modulus_Power;
18292 else
18293 M_Val := Expr_Value (Mod_Expr);
18294 end if;
18296 if M_Val < 1 then
18297 Error_Msg_N ("modulus value must be positive", Mod_Expr);
18298 M_Val := 2 ** System_Max_Binary_Modulus_Power;
18299 end if;
18301 if M_Val > 2 ** Standard_Long_Integer_Size then
18302 Check_Restriction (No_Long_Long_Integers, Mod_Expr);
18303 end if;
18305 Set_Modulus (T, M_Val);
18307 -- Create bounds for the modular type based on the modulus given in
18308 -- the type declaration and then analyze and resolve those bounds.
18310 Set_Scalar_Range (T,
18311 Make_Range (Sloc (Mod_Expr),
18312 Low_Bound => Make_Integer_Literal (Sloc (Mod_Expr), 0),
18313 High_Bound => Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
18315 -- Properly analyze the literals for the range. We do this manually
18316 -- because we can't go calling Resolve, since we are resolving these
18317 -- bounds with the type, and this type is certainly not complete yet.
18319 Set_Etype (Low_Bound (Scalar_Range (T)), T);
18320 Set_Etype (High_Bound (Scalar_Range (T)), T);
18321 Set_Is_Static_Expression (Low_Bound (Scalar_Range (T)));
18322 Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
18324 -- Loop through powers of two to find number of bits required
18326 for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
18328 -- Binary case
18330 if M_Val = 2 ** Bits then
18331 Set_Modular_Size (Bits);
18332 return;
18334 -- Non-binary case
18336 elsif M_Val < 2 ** Bits then
18337 Check_SPARK_05_Restriction ("modulus should be a power of 2", T);
18338 Set_Non_Binary_Modulus (T);
18340 if Bits > System_Max_Nonbinary_Modulus_Power then
18341 Error_Msg_Uint_1 :=
18342 UI_From_Int (System_Max_Nonbinary_Modulus_Power);
18343 Error_Msg_F
18344 ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
18345 Set_Modular_Size (System_Max_Binary_Modulus_Power);
18346 return;
18348 else
18349 -- In the non-binary case, set size as per RM 13.3(55)
18351 Set_Modular_Size (Bits);
18352 return;
18353 end if;
18354 end if;
18356 end loop;
18358 -- If we fall through, then the size exceed System.Max_Binary_Modulus
18359 -- so we just signal an error and set the maximum size.
18361 Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
18362 Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
18364 Set_Modular_Size (System_Max_Binary_Modulus_Power);
18365 Init_Alignment (T);
18367 end Modular_Type_Declaration;
18369 --------------------------
18370 -- New_Concatenation_Op --
18371 --------------------------
18373 procedure New_Concatenation_Op (Typ : Entity_Id) is
18374 Loc : constant Source_Ptr := Sloc (Typ);
18375 Op : Entity_Id;
18377 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
18378 -- Create abbreviated declaration for the formal of a predefined
18379 -- Operator 'Op' of type 'Typ'
18381 --------------------
18382 -- Make_Op_Formal --
18383 --------------------
18385 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
18386 Formal : Entity_Id;
18387 begin
18388 Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
18389 Set_Etype (Formal, Typ);
18390 Set_Mechanism (Formal, Default_Mechanism);
18391 return Formal;
18392 end Make_Op_Formal;
18394 -- Start of processing for New_Concatenation_Op
18396 begin
18397 Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
18399 Set_Ekind (Op, E_Operator);
18400 Set_Scope (Op, Current_Scope);
18401 Set_Etype (Op, Typ);
18402 Set_Homonym (Op, Get_Name_Entity_Id (Name_Op_Concat));
18403 Set_Is_Immediately_Visible (Op);
18404 Set_Is_Intrinsic_Subprogram (Op);
18405 Set_Has_Completion (Op);
18406 Append_Entity (Op, Current_Scope);
18408 Set_Name_Entity_Id (Name_Op_Concat, Op);
18410 Append_Entity (Make_Op_Formal (Typ, Op), Op);
18411 Append_Entity (Make_Op_Formal (Typ, Op), Op);
18412 end New_Concatenation_Op;
18414 -------------------------
18415 -- OK_For_Limited_Init --
18416 -------------------------
18418 -- ???Check all calls of this, and compare the conditions under which it's
18419 -- called.
18421 function OK_For_Limited_Init
18422 (Typ : Entity_Id;
18423 Exp : Node_Id) return Boolean
18425 begin
18426 return Is_CPP_Constructor_Call (Exp)
18427 or else (Ada_Version >= Ada_2005
18428 and then not Debug_Flag_Dot_L
18429 and then OK_For_Limited_Init_In_05 (Typ, Exp));
18430 end OK_For_Limited_Init;
18432 -------------------------------
18433 -- OK_For_Limited_Init_In_05 --
18434 -------------------------------
18436 function OK_For_Limited_Init_In_05
18437 (Typ : Entity_Id;
18438 Exp : Node_Id) return Boolean
18440 begin
18441 -- An object of a limited interface type can be initialized with any
18442 -- expression of a nonlimited descendant type.
18444 if Is_Class_Wide_Type (Typ)
18445 and then Is_Limited_Interface (Typ)
18446 and then not Is_Limited_Type (Etype (Exp))
18447 then
18448 return True;
18449 end if;
18451 -- Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
18452 -- case of limited aggregates (including extension aggregates), and
18453 -- function calls. The function call may have been given in prefixed
18454 -- notation, in which case the original node is an indexed component.
18455 -- If the function is parameterless, the original node was an explicit
18456 -- dereference. The function may also be parameterless, in which case
18457 -- the source node is just an identifier.
18459 case Nkind (Original_Node (Exp)) is
18460 when N_Aggregate | N_Extension_Aggregate | N_Function_Call | N_Op =>
18461 return True;
18463 when N_Identifier =>
18464 return Present (Entity (Original_Node (Exp)))
18465 and then Ekind (Entity (Original_Node (Exp))) = E_Function;
18467 when N_Qualified_Expression =>
18468 return
18469 OK_For_Limited_Init_In_05
18470 (Typ, Expression (Original_Node (Exp)));
18472 -- Ada 2005 (AI-251): If a class-wide interface object is initialized
18473 -- with a function call, the expander has rewritten the call into an
18474 -- N_Type_Conversion node to force displacement of the pointer to
18475 -- reference the component containing the secondary dispatch table.
18476 -- Otherwise a type conversion is not a legal context.
18477 -- A return statement for a build-in-place function returning a
18478 -- synchronized type also introduces an unchecked conversion.
18480 when N_Type_Conversion |
18481 N_Unchecked_Type_Conversion =>
18482 return not Comes_From_Source (Exp)
18483 and then
18484 OK_For_Limited_Init_In_05
18485 (Typ, Expression (Original_Node (Exp)));
18487 when N_Indexed_Component |
18488 N_Selected_Component |
18489 N_Explicit_Dereference =>
18490 return Nkind (Exp) = N_Function_Call;
18492 -- A use of 'Input is a function call, hence allowed. Normally the
18493 -- attribute will be changed to a call, but the attribute by itself
18494 -- can occur with -gnatc.
18496 when N_Attribute_Reference =>
18497 return Attribute_Name (Original_Node (Exp)) = Name_Input;
18499 -- For a case expression, all dependent expressions must be legal
18501 when N_Case_Expression =>
18502 declare
18503 Alt : Node_Id;
18505 begin
18506 Alt := First (Alternatives (Original_Node (Exp)));
18507 while Present (Alt) loop
18508 if not OK_For_Limited_Init_In_05 (Typ, Expression (Alt)) then
18509 return False;
18510 end if;
18512 Next (Alt);
18513 end loop;
18515 return True;
18516 end;
18518 -- For an if expression, all dependent expressions must be legal
18520 when N_If_Expression =>
18521 declare
18522 Then_Expr : constant Node_Id :=
18523 Next (First (Expressions (Original_Node (Exp))));
18524 Else_Expr : constant Node_Id := Next (Then_Expr);
18525 begin
18526 return OK_For_Limited_Init_In_05 (Typ, Then_Expr)
18527 and then
18528 OK_For_Limited_Init_In_05 (Typ, Else_Expr);
18529 end;
18531 when others =>
18532 return False;
18533 end case;
18534 end OK_For_Limited_Init_In_05;
18536 -------------------------------------------
18537 -- Ordinary_Fixed_Point_Type_Declaration --
18538 -------------------------------------------
18540 procedure Ordinary_Fixed_Point_Type_Declaration
18541 (T : Entity_Id;
18542 Def : Node_Id)
18544 Loc : constant Source_Ptr := Sloc (Def);
18545 Delta_Expr : constant Node_Id := Delta_Expression (Def);
18546 RRS : constant Node_Id := Real_Range_Specification (Def);
18547 Implicit_Base : Entity_Id;
18548 Delta_Val : Ureal;
18549 Small_Val : Ureal;
18550 Low_Val : Ureal;
18551 High_Val : Ureal;
18553 begin
18554 Check_Restriction (No_Fixed_Point, Def);
18556 -- Create implicit base type
18558 Implicit_Base :=
18559 Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
18560 Set_Etype (Implicit_Base, Implicit_Base);
18562 -- Analyze and process delta expression
18564 Analyze_And_Resolve (Delta_Expr, Any_Real);
18566 Check_Delta_Expression (Delta_Expr);
18567 Delta_Val := Expr_Value_R (Delta_Expr);
18569 Set_Delta_Value (Implicit_Base, Delta_Val);
18571 -- Compute default small from given delta, which is the largest power
18572 -- of two that does not exceed the given delta value.
18574 declare
18575 Tmp : Ureal;
18576 Scale : Int;
18578 begin
18579 Tmp := Ureal_1;
18580 Scale := 0;
18582 if Delta_Val < Ureal_1 then
18583 while Delta_Val < Tmp loop
18584 Tmp := Tmp / Ureal_2;
18585 Scale := Scale + 1;
18586 end loop;
18588 else
18589 loop
18590 Tmp := Tmp * Ureal_2;
18591 exit when Tmp > Delta_Val;
18592 Scale := Scale - 1;
18593 end loop;
18594 end if;
18596 Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
18597 end;
18599 Set_Small_Value (Implicit_Base, Small_Val);
18601 -- If no range was given, set a dummy range
18603 if RRS <= Empty_Or_Error then
18604 Low_Val := -Small_Val;
18605 High_Val := Small_Val;
18607 -- Otherwise analyze and process given range
18609 else
18610 declare
18611 Low : constant Node_Id := Low_Bound (RRS);
18612 High : constant Node_Id := High_Bound (RRS);
18614 begin
18615 Analyze_And_Resolve (Low, Any_Real);
18616 Analyze_And_Resolve (High, Any_Real);
18617 Check_Real_Bound (Low);
18618 Check_Real_Bound (High);
18620 -- Obtain and set the range
18622 Low_Val := Expr_Value_R (Low);
18623 High_Val := Expr_Value_R (High);
18625 if Low_Val > High_Val then
18626 Error_Msg_NE ("??fixed point type& has null range", Def, T);
18627 end if;
18628 end;
18629 end if;
18631 -- The range for both the implicit base and the declared first subtype
18632 -- cannot be set yet, so we use the special routine Set_Fixed_Range to
18633 -- set a temporary range in place. Note that the bounds of the base
18634 -- type will be widened to be symmetrical and to fill the available
18635 -- bits when the type is frozen.
18637 -- We could do this with all discrete types, and probably should, but
18638 -- we absolutely have to do it for fixed-point, since the end-points
18639 -- of the range and the size are determined by the small value, which
18640 -- could be reset before the freeze point.
18642 Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
18643 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
18645 -- Complete definition of first subtype. The inheritance of the rep item
18646 -- chain ensures that SPARK-related pragmas are not clobbered when the
18647 -- ordinary fixed point type acts as a full view of a private type.
18649 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
18650 Set_Etype (T, Implicit_Base);
18651 Init_Size_Align (T);
18652 Inherit_Rep_Item_Chain (T, Implicit_Base);
18653 Set_Small_Value (T, Small_Val);
18654 Set_Delta_Value (T, Delta_Val);
18655 Set_Is_Constrained (T);
18656 end Ordinary_Fixed_Point_Type_Declaration;
18658 ----------------------------------
18659 -- Preanalyze_Assert_Expression --
18660 ----------------------------------
18662 procedure Preanalyze_Assert_Expression (N : Node_Id; T : Entity_Id) is
18663 begin
18664 In_Assertion_Expr := In_Assertion_Expr + 1;
18665 Preanalyze_Spec_Expression (N, T);
18666 In_Assertion_Expr := In_Assertion_Expr - 1;
18667 end Preanalyze_Assert_Expression;
18669 -----------------------------------
18670 -- Preanalyze_Default_Expression --
18671 -----------------------------------
18673 procedure Preanalyze_Default_Expression (N : Node_Id; T : Entity_Id) is
18674 Save_In_Default_Expr : constant Boolean := In_Default_Expr;
18675 begin
18676 In_Default_Expr := True;
18677 Preanalyze_Spec_Expression (N, T);
18678 In_Default_Expr := Save_In_Default_Expr;
18679 end Preanalyze_Default_Expression;
18681 --------------------------------
18682 -- Preanalyze_Spec_Expression --
18683 --------------------------------
18685 procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
18686 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
18687 begin
18688 In_Spec_Expression := True;
18689 Preanalyze_And_Resolve (N, T);
18690 In_Spec_Expression := Save_In_Spec_Expression;
18691 end Preanalyze_Spec_Expression;
18693 ----------------------------------------
18694 -- Prepare_Private_Subtype_Completion --
18695 ----------------------------------------
18697 procedure Prepare_Private_Subtype_Completion
18698 (Id : Entity_Id;
18699 Related_Nod : Node_Id)
18701 Id_B : constant Entity_Id := Base_Type (Id);
18702 Full_B : Entity_Id := Full_View (Id_B);
18703 Full : Entity_Id;
18705 begin
18706 if Present (Full_B) then
18708 -- Get to the underlying full view if necessary
18710 if Is_Private_Type (Full_B)
18711 and then Present (Underlying_Full_View (Full_B))
18712 then
18713 Full_B := Underlying_Full_View (Full_B);
18714 end if;
18716 -- The Base_Type is already completed, we can complete the subtype
18717 -- now. We have to create a new entity with the same name, Thus we
18718 -- can't use Create_Itype.
18720 Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
18721 Set_Is_Itype (Full);
18722 Set_Associated_Node_For_Itype (Full, Related_Nod);
18723 Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
18724 end if;
18726 -- The parent subtype may be private, but the base might not, in some
18727 -- nested instances. In that case, the subtype does not need to be
18728 -- exchanged. It would still be nice to make private subtypes and their
18729 -- bases consistent at all times ???
18731 if Is_Private_Type (Id_B) then
18732 Append_Elmt (Id, Private_Dependents (Id_B));
18733 end if;
18734 end Prepare_Private_Subtype_Completion;
18736 ---------------------------
18737 -- Process_Discriminants --
18738 ---------------------------
18740 procedure Process_Discriminants
18741 (N : Node_Id;
18742 Prev : Entity_Id := Empty)
18744 Elist : constant Elist_Id := New_Elmt_List;
18745 Id : Node_Id;
18746 Discr : Node_Id;
18747 Discr_Number : Uint;
18748 Discr_Type : Entity_Id;
18749 Default_Present : Boolean := False;
18750 Default_Not_Present : Boolean := False;
18752 begin
18753 -- A composite type other than an array type can have discriminants.
18754 -- On entry, the current scope is the composite type.
18756 -- The discriminants are initially entered into the scope of the type
18757 -- via Enter_Name with the default Ekind of E_Void to prevent premature
18758 -- use, as explained at the end of this procedure.
18760 Discr := First (Discriminant_Specifications (N));
18761 while Present (Discr) loop
18762 Enter_Name (Defining_Identifier (Discr));
18764 -- For navigation purposes we add a reference to the discriminant
18765 -- in the entity for the type. If the current declaration is a
18766 -- completion, place references on the partial view. Otherwise the
18767 -- type is the current scope.
18769 if Present (Prev) then
18771 -- The references go on the partial view, if present. If the
18772 -- partial view has discriminants, the references have been
18773 -- generated already.
18775 if not Has_Discriminants (Prev) then
18776 Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
18777 end if;
18778 else
18779 Generate_Reference
18780 (Current_Scope, Defining_Identifier (Discr), 'd');
18781 end if;
18783 if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
18784 Discr_Type := Access_Definition (Discr, Discriminant_Type (Discr));
18786 -- Ada 2005 (AI-254)
18788 if Present (Access_To_Subprogram_Definition
18789 (Discriminant_Type (Discr)))
18790 and then Protected_Present (Access_To_Subprogram_Definition
18791 (Discriminant_Type (Discr)))
18792 then
18793 Discr_Type :=
18794 Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
18795 end if;
18797 else
18798 Find_Type (Discriminant_Type (Discr));
18799 Discr_Type := Etype (Discriminant_Type (Discr));
18801 if Error_Posted (Discriminant_Type (Discr)) then
18802 Discr_Type := Any_Type;
18803 end if;
18804 end if;
18806 -- Handling of discriminants that are access types
18808 if Is_Access_Type (Discr_Type) then
18810 -- Ada 2005 (AI-230): Access discriminant allowed in non-
18811 -- limited record types
18813 if Ada_Version < Ada_2005 then
18814 Check_Access_Discriminant_Requires_Limited
18815 (Discr, Discriminant_Type (Discr));
18816 end if;
18818 if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
18819 Error_Msg_N
18820 ("(Ada 83) access discriminant not allowed", Discr);
18821 end if;
18823 -- If not access type, must be a discrete type
18825 elsif not Is_Discrete_Type (Discr_Type) then
18826 Error_Msg_N
18827 ("discriminants must have a discrete or access type",
18828 Discriminant_Type (Discr));
18829 end if;
18831 Set_Etype (Defining_Identifier (Discr), Discr_Type);
18833 -- If a discriminant specification includes the assignment compound
18834 -- delimiter followed by an expression, the expression is the default
18835 -- expression of the discriminant; the default expression must be of
18836 -- the type of the discriminant. (RM 3.7.1) Since this expression is
18837 -- a default expression, we do the special preanalysis, since this
18838 -- expression does not freeze (see section "Handling of Default and
18839 -- Per-Object Expressions" in spec of package Sem).
18841 if Present (Expression (Discr)) then
18842 Preanalyze_Spec_Expression (Expression (Discr), Discr_Type);
18844 -- Legaity checks
18846 if Nkind (N) = N_Formal_Type_Declaration then
18847 Error_Msg_N
18848 ("discriminant defaults not allowed for formal type",
18849 Expression (Discr));
18851 -- Flag an error for a tagged type with defaulted discriminants,
18852 -- excluding limited tagged types when compiling for Ada 2012
18853 -- (see AI05-0214).
18855 elsif Is_Tagged_Type (Current_Scope)
18856 and then (not Is_Limited_Type (Current_Scope)
18857 or else Ada_Version < Ada_2012)
18858 and then Comes_From_Source (N)
18859 then
18860 -- Note: see similar test in Check_Or_Process_Discriminants, to
18861 -- handle the (illegal) case of the completion of an untagged
18862 -- view with discriminants with defaults by a tagged full view.
18863 -- We skip the check if Discr does not come from source, to
18864 -- account for the case of an untagged derived type providing
18865 -- defaults for a renamed discriminant from a private untagged
18866 -- ancestor with a tagged full view (ACATS B460006).
18868 if Ada_Version >= Ada_2012 then
18869 Error_Msg_N
18870 ("discriminants of nonlimited tagged type cannot have"
18871 & " defaults",
18872 Expression (Discr));
18873 else
18874 Error_Msg_N
18875 ("discriminants of tagged type cannot have defaults",
18876 Expression (Discr));
18877 end if;
18879 else
18880 Default_Present := True;
18881 Append_Elmt (Expression (Discr), Elist);
18883 -- Tag the defining identifiers for the discriminants with
18884 -- their corresponding default expressions from the tree.
18886 Set_Discriminant_Default_Value
18887 (Defining_Identifier (Discr), Expression (Discr));
18888 end if;
18890 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag
18891 -- gets set unless we can be sure that no range check is required.
18893 if (GNATprove_Mode or not Expander_Active)
18894 and then not
18895 Is_In_Range
18896 (Expression (Discr), Discr_Type, Assume_Valid => True)
18897 then
18898 Set_Do_Range_Check (Expression (Discr));
18899 end if;
18901 -- No default discriminant value given
18903 else
18904 Default_Not_Present := True;
18905 end if;
18907 -- Ada 2005 (AI-231): Create an Itype that is a duplicate of
18908 -- Discr_Type but with the null-exclusion attribute
18910 if Ada_Version >= Ada_2005 then
18912 -- Ada 2005 (AI-231): Static checks
18914 if Can_Never_Be_Null (Discr_Type) then
18915 Null_Exclusion_Static_Checks (Discr);
18917 elsif Is_Access_Type (Discr_Type)
18918 and then Null_Exclusion_Present (Discr)
18920 -- No need to check itypes because in their case this check
18921 -- was done at their point of creation
18923 and then not Is_Itype (Discr_Type)
18924 then
18925 if Can_Never_Be_Null (Discr_Type) then
18926 Error_Msg_NE
18927 ("`NOT NULL` not allowed (& already excludes null)",
18928 Discr,
18929 Discr_Type);
18930 end if;
18932 Set_Etype (Defining_Identifier (Discr),
18933 Create_Null_Excluding_Itype
18934 (T => Discr_Type,
18935 Related_Nod => Discr));
18937 -- Check for improper null exclusion if the type is otherwise
18938 -- legal for a discriminant.
18940 elsif Null_Exclusion_Present (Discr)
18941 and then Is_Discrete_Type (Discr_Type)
18942 then
18943 Error_Msg_N
18944 ("null exclusion can only apply to an access type", Discr);
18945 end if;
18947 -- Ada 2005 (AI-402): access discriminants of nonlimited types
18948 -- can't have defaults. Synchronized types, or types that are
18949 -- explicitly limited are fine, but special tests apply to derived
18950 -- types in generics: in a generic body we have to assume the
18951 -- worst, and therefore defaults are not allowed if the parent is
18952 -- a generic formal private type (see ACATS B370001).
18954 if Is_Access_Type (Discr_Type) and then Default_Present then
18955 if Ekind (Discr_Type) /= E_Anonymous_Access_Type
18956 or else Is_Limited_Record (Current_Scope)
18957 or else Is_Concurrent_Type (Current_Scope)
18958 or else Is_Concurrent_Record_Type (Current_Scope)
18959 or else Ekind (Current_Scope) = E_Limited_Private_Type
18960 then
18961 if not Is_Derived_Type (Current_Scope)
18962 or else not Is_Generic_Type (Etype (Current_Scope))
18963 or else not In_Package_Body (Scope (Etype (Current_Scope)))
18964 or else Limited_Present
18965 (Type_Definition (Parent (Current_Scope)))
18966 then
18967 null;
18969 else
18970 Error_Msg_N
18971 ("access discriminants of nonlimited types cannot "
18972 & "have defaults", Expression (Discr));
18973 end if;
18975 elsif Present (Expression (Discr)) then
18976 Error_Msg_N
18977 ("(Ada 2005) access discriminants of nonlimited types "
18978 & "cannot have defaults", Expression (Discr));
18979 end if;
18980 end if;
18981 end if;
18983 -- A discriminant cannot be effectively volatile. This check is only
18984 -- relevant when SPARK_Mode is on as it is not standard Ada legality
18985 -- rule (SPARK RM 7.1.3(6)).
18987 if SPARK_Mode = On
18988 and then Is_Effectively_Volatile (Defining_Identifier (Discr))
18989 then
18990 Error_Msg_N ("discriminant cannot be volatile", Discr);
18991 end if;
18993 Next (Discr);
18994 end loop;
18996 -- An element list consisting of the default expressions of the
18997 -- discriminants is constructed in the above loop and used to set
18998 -- the Discriminant_Constraint attribute for the type. If an object
18999 -- is declared of this (record or task) type without any explicit
19000 -- discriminant constraint given, this element list will form the
19001 -- actual parameters for the corresponding initialization procedure
19002 -- for the type.
19004 Set_Discriminant_Constraint (Current_Scope, Elist);
19005 Set_Stored_Constraint (Current_Scope, No_Elist);
19007 -- Default expressions must be provided either for all or for none
19008 -- of the discriminants of a discriminant part. (RM 3.7.1)
19010 if Default_Present and then Default_Not_Present then
19011 Error_Msg_N
19012 ("incomplete specification of defaults for discriminants", N);
19013 end if;
19015 -- The use of the name of a discriminant is not allowed in default
19016 -- expressions of a discriminant part if the specification of the
19017 -- discriminant is itself given in the discriminant part. (RM 3.7.1)
19019 -- To detect this, the discriminant names are entered initially with an
19020 -- Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
19021 -- attempt to use a void entity (for example in an expression that is
19022 -- type-checked) produces the error message: premature usage. Now after
19023 -- completing the semantic analysis of the discriminant part, we can set
19024 -- the Ekind of all the discriminants appropriately.
19026 Discr := First (Discriminant_Specifications (N));
19027 Discr_Number := Uint_1;
19028 while Present (Discr) loop
19029 Id := Defining_Identifier (Discr);
19030 Set_Ekind (Id, E_Discriminant);
19031 Init_Component_Location (Id);
19032 Init_Esize (Id);
19033 Set_Discriminant_Number (Id, Discr_Number);
19035 -- Make sure this is always set, even in illegal programs
19037 Set_Corresponding_Discriminant (Id, Empty);
19039 -- Initialize the Original_Record_Component to the entity itself.
19040 -- Inherit_Components will propagate the right value to
19041 -- discriminants in derived record types.
19043 Set_Original_Record_Component (Id, Id);
19045 -- Create the discriminal for the discriminant
19047 Build_Discriminal (Id);
19049 Next (Discr);
19050 Discr_Number := Discr_Number + 1;
19051 end loop;
19053 Set_Has_Discriminants (Current_Scope);
19054 end Process_Discriminants;
19056 -----------------------
19057 -- Process_Full_View --
19058 -----------------------
19060 procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
19061 procedure Collect_Implemented_Interfaces
19062 (Typ : Entity_Id;
19063 Ifaces : Elist_Id);
19064 -- Ada 2005: Gather all the interfaces that Typ directly or
19065 -- inherently implements. Duplicate entries are not added to
19066 -- the list Ifaces.
19068 ------------------------------------
19069 -- Collect_Implemented_Interfaces --
19070 ------------------------------------
19072 procedure Collect_Implemented_Interfaces
19073 (Typ : Entity_Id;
19074 Ifaces : Elist_Id)
19076 Iface : Entity_Id;
19077 Iface_Elmt : Elmt_Id;
19079 begin
19080 -- Abstract interfaces are only associated with tagged record types
19082 if not Is_Tagged_Type (Typ) or else not Is_Record_Type (Typ) then
19083 return;
19084 end if;
19086 -- Recursively climb to the ancestors
19088 if Etype (Typ) /= Typ
19090 -- Protect the frontend against wrong cyclic declarations like:
19092 -- type B is new A with private;
19093 -- type C is new A with private;
19094 -- private
19095 -- type B is new C with null record;
19096 -- type C is new B with null record;
19098 and then Etype (Typ) /= Priv_T
19099 and then Etype (Typ) /= Full_T
19100 then
19101 -- Keep separate the management of private type declarations
19103 if Ekind (Typ) = E_Record_Type_With_Private then
19105 -- Handle the following illegal usage:
19106 -- type Private_Type is tagged private;
19107 -- private
19108 -- type Private_Type is new Type_Implementing_Iface;
19110 if Present (Full_View (Typ))
19111 and then Etype (Typ) /= Full_View (Typ)
19112 then
19113 if Is_Interface (Etype (Typ)) then
19114 Append_Unique_Elmt (Etype (Typ), Ifaces);
19115 end if;
19117 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
19118 end if;
19120 -- Non-private types
19122 else
19123 if Is_Interface (Etype (Typ)) then
19124 Append_Unique_Elmt (Etype (Typ), Ifaces);
19125 end if;
19127 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
19128 end if;
19129 end if;
19131 -- Handle entities in the list of abstract interfaces
19133 if Present (Interfaces (Typ)) then
19134 Iface_Elmt := First_Elmt (Interfaces (Typ));
19135 while Present (Iface_Elmt) loop
19136 Iface := Node (Iface_Elmt);
19138 pragma Assert (Is_Interface (Iface));
19140 if not Contain_Interface (Iface, Ifaces) then
19141 Append_Elmt (Iface, Ifaces);
19142 Collect_Implemented_Interfaces (Iface, Ifaces);
19143 end if;
19145 Next_Elmt (Iface_Elmt);
19146 end loop;
19147 end if;
19148 end Collect_Implemented_Interfaces;
19150 -- Local variables
19152 Full_Indic : Node_Id;
19153 Full_Parent : Entity_Id;
19154 Priv_Parent : Entity_Id;
19156 -- Start of processing for Process_Full_View
19158 begin
19159 -- First some sanity checks that must be done after semantic
19160 -- decoration of the full view and thus cannot be placed with other
19161 -- similar checks in Find_Type_Name
19163 if not Is_Limited_Type (Priv_T)
19164 and then (Is_Limited_Type (Full_T)
19165 or else Is_Limited_Composite (Full_T))
19166 then
19167 if In_Instance then
19168 null;
19169 else
19170 Error_Msg_N
19171 ("completion of nonlimited type cannot be limited", Full_T);
19172 Explain_Limited_Type (Full_T, Full_T);
19173 end if;
19175 elsif Is_Abstract_Type (Full_T)
19176 and then not Is_Abstract_Type (Priv_T)
19177 then
19178 Error_Msg_N
19179 ("completion of nonabstract type cannot be abstract", Full_T);
19181 elsif Is_Tagged_Type (Priv_T)
19182 and then Is_Limited_Type (Priv_T)
19183 and then not Is_Limited_Type (Full_T)
19184 then
19185 -- If pragma CPP_Class was applied to the private declaration
19186 -- propagate the limitedness to the full-view
19188 if Is_CPP_Class (Priv_T) then
19189 Set_Is_Limited_Record (Full_T);
19191 -- GNAT allow its own definition of Limited_Controlled to disobey
19192 -- this rule in order in ease the implementation. This test is safe
19193 -- because Root_Controlled is defined in a child of System that
19194 -- normal programs are not supposed to use.
19196 elsif Is_RTE (Etype (Full_T), RE_Root_Controlled) then
19197 Set_Is_Limited_Composite (Full_T);
19198 else
19199 Error_Msg_N
19200 ("completion of limited tagged type must be limited", Full_T);
19201 end if;
19203 elsif Is_Generic_Type (Priv_T) then
19204 Error_Msg_N ("generic type cannot have a completion", Full_T);
19205 end if;
19207 -- Check that ancestor interfaces of private and full views are
19208 -- consistent. We omit this check for synchronized types because
19209 -- they are performed on the corresponding record type when frozen.
19211 if Ada_Version >= Ada_2005
19212 and then Is_Tagged_Type (Priv_T)
19213 and then Is_Tagged_Type (Full_T)
19214 and then not Is_Concurrent_Type (Full_T)
19215 then
19216 declare
19217 Iface : Entity_Id;
19218 Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
19219 Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
19221 begin
19222 Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
19223 Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
19225 -- Ada 2005 (AI-251): The partial view shall be a descendant of
19226 -- an interface type if and only if the full type is descendant
19227 -- of the interface type (AARM 7.3 (7.3/2)).
19229 Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
19231 if Present (Iface) then
19232 Error_Msg_NE
19233 ("interface in partial view& not implemented by full type "
19234 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
19235 end if;
19237 Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
19239 if Present (Iface) then
19240 Error_Msg_NE
19241 ("interface & not implemented by partial view "
19242 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
19243 end if;
19244 end;
19245 end if;
19247 if Is_Tagged_Type (Priv_T)
19248 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
19249 and then Is_Derived_Type (Full_T)
19250 then
19251 Priv_Parent := Etype (Priv_T);
19253 -- The full view of a private extension may have been transformed
19254 -- into an unconstrained derived type declaration and a subtype
19255 -- declaration (see build_derived_record_type for details).
19257 if Nkind (N) = N_Subtype_Declaration then
19258 Full_Indic := Subtype_Indication (N);
19259 Full_Parent := Etype (Base_Type (Full_T));
19260 else
19261 Full_Indic := Subtype_Indication (Type_Definition (N));
19262 Full_Parent := Etype (Full_T);
19263 end if;
19265 -- Check that the parent type of the full type is a descendant of
19266 -- the ancestor subtype given in the private extension. If either
19267 -- entity has an Etype equal to Any_Type then we had some previous
19268 -- error situation [7.3(8)].
19270 if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
19271 return;
19273 -- Ada 2005 (AI-251): Interfaces in the full type can be given in
19274 -- any order. Therefore we don't have to check that its parent must
19275 -- be a descendant of the parent of the private type declaration.
19277 elsif Is_Interface (Priv_Parent)
19278 and then Is_Interface (Full_Parent)
19279 then
19280 null;
19282 -- Ada 2005 (AI-251): If the parent of the private type declaration
19283 -- is an interface there is no need to check that it is an ancestor
19284 -- of the associated full type declaration. The required tests for
19285 -- this case are performed by Build_Derived_Record_Type.
19287 elsif not Is_Interface (Base_Type (Priv_Parent))
19288 and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
19289 then
19290 Error_Msg_N
19291 ("parent of full type must descend from parent"
19292 & " of private extension", Full_Indic);
19294 -- First check a formal restriction, and then proceed with checking
19295 -- Ada rules. Since the formal restriction is not a serious error, we
19296 -- don't prevent further error detection for this check, hence the
19297 -- ELSE.
19299 else
19300 -- In formal mode, when completing a private extension the type
19301 -- named in the private part must be exactly the same as that
19302 -- named in the visible part.
19304 if Priv_Parent /= Full_Parent then
19305 Error_Msg_Name_1 := Chars (Priv_Parent);
19306 Check_SPARK_05_Restriction ("% expected", Full_Indic);
19307 end if;
19309 -- Check the rules of 7.3(10): if the private extension inherits
19310 -- known discriminants, then the full type must also inherit those
19311 -- discriminants from the same (ancestor) type, and the parent
19312 -- subtype of the full type must be constrained if and only if
19313 -- the ancestor subtype of the private extension is constrained.
19315 if No (Discriminant_Specifications (Parent (Priv_T)))
19316 and then not Has_Unknown_Discriminants (Priv_T)
19317 and then Has_Discriminants (Base_Type (Priv_Parent))
19318 then
19319 declare
19320 Priv_Indic : constant Node_Id :=
19321 Subtype_Indication (Parent (Priv_T));
19323 Priv_Constr : constant Boolean :=
19324 Is_Constrained (Priv_Parent)
19325 or else
19326 Nkind (Priv_Indic) = N_Subtype_Indication
19327 or else
19328 Is_Constrained (Entity (Priv_Indic));
19330 Full_Constr : constant Boolean :=
19331 Is_Constrained (Full_Parent)
19332 or else
19333 Nkind (Full_Indic) = N_Subtype_Indication
19334 or else
19335 Is_Constrained (Entity (Full_Indic));
19337 Priv_Discr : Entity_Id;
19338 Full_Discr : Entity_Id;
19340 begin
19341 Priv_Discr := First_Discriminant (Priv_Parent);
19342 Full_Discr := First_Discriminant (Full_Parent);
19343 while Present (Priv_Discr) and then Present (Full_Discr) loop
19344 if Original_Record_Component (Priv_Discr) =
19345 Original_Record_Component (Full_Discr)
19346 or else
19347 Corresponding_Discriminant (Priv_Discr) =
19348 Corresponding_Discriminant (Full_Discr)
19349 then
19350 null;
19351 else
19352 exit;
19353 end if;
19355 Next_Discriminant (Priv_Discr);
19356 Next_Discriminant (Full_Discr);
19357 end loop;
19359 if Present (Priv_Discr) or else Present (Full_Discr) then
19360 Error_Msg_N
19361 ("full view must inherit discriminants of the parent"
19362 & " type used in the private extension", Full_Indic);
19364 elsif Priv_Constr and then not Full_Constr then
19365 Error_Msg_N
19366 ("parent subtype of full type must be constrained",
19367 Full_Indic);
19369 elsif Full_Constr and then not Priv_Constr then
19370 Error_Msg_N
19371 ("parent subtype of full type must be unconstrained",
19372 Full_Indic);
19373 end if;
19374 end;
19376 -- Check the rules of 7.3(12): if a partial view has neither
19377 -- known or unknown discriminants, then the full type
19378 -- declaration shall define a definite subtype.
19380 elsif not Has_Unknown_Discriminants (Priv_T)
19381 and then not Has_Discriminants (Priv_T)
19382 and then not Is_Constrained (Full_T)
19383 then
19384 Error_Msg_N
19385 ("full view must define a constrained type if partial view"
19386 & " has no discriminants", Full_T);
19387 end if;
19389 -- ??????? Do we implement the following properly ?????
19390 -- If the ancestor subtype of a private extension has constrained
19391 -- discriminants, then the parent subtype of the full view shall
19392 -- impose a statically matching constraint on those discriminants
19393 -- [7.3(13)].
19394 end if;
19396 else
19397 -- For untagged types, verify that a type without discriminants is
19398 -- not completed with an unconstrained type. A separate error message
19399 -- is produced if the full type has defaulted discriminants.
19401 if not Is_Indefinite_Subtype (Priv_T)
19402 and then Is_Indefinite_Subtype (Full_T)
19403 then
19404 Error_Msg_Sloc := Sloc (Parent (Priv_T));
19405 Error_Msg_NE
19406 ("full view of& not compatible with declaration#",
19407 Full_T, Priv_T);
19409 if not Is_Tagged_Type (Full_T) then
19410 Error_Msg_N
19411 ("\one is constrained, the other unconstrained", Full_T);
19412 end if;
19413 end if;
19414 end if;
19416 -- AI-419: verify that the use of "limited" is consistent
19418 declare
19419 Orig_Decl : constant Node_Id := Original_Node (N);
19421 begin
19422 if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
19423 and then not Limited_Present (Parent (Priv_T))
19424 and then not Synchronized_Present (Parent (Priv_T))
19425 and then Nkind (Orig_Decl) = N_Full_Type_Declaration
19426 and then Nkind
19427 (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
19428 and then Limited_Present (Type_Definition (Orig_Decl))
19429 then
19430 Error_Msg_N
19431 ("full view of non-limited extension cannot be limited", N);
19432 end if;
19433 end;
19435 -- Ada 2005 (AI-443): A synchronized private extension must be
19436 -- completed by a task or protected type.
19438 if Ada_Version >= Ada_2005
19439 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
19440 and then Synchronized_Present (Parent (Priv_T))
19441 and then not Is_Concurrent_Type (Full_T)
19442 then
19443 Error_Msg_N ("full view of synchronized extension must " &
19444 "be synchronized type", N);
19445 end if;
19447 -- Ada 2005 AI-363: if the full view has discriminants with
19448 -- defaults, it is illegal to declare constrained access subtypes
19449 -- whose designated type is the current type. This allows objects
19450 -- of the type that are declared in the heap to be unconstrained.
19452 if not Has_Unknown_Discriminants (Priv_T)
19453 and then not Has_Discriminants (Priv_T)
19454 and then Has_Discriminants (Full_T)
19455 and then
19456 Present (Discriminant_Default_Value (First_Discriminant (Full_T)))
19457 then
19458 Set_Has_Constrained_Partial_View (Full_T);
19459 Set_Has_Constrained_Partial_View (Priv_T);
19460 end if;
19462 -- Create a full declaration for all its subtypes recorded in
19463 -- Private_Dependents and swap them similarly to the base type. These
19464 -- are subtypes that have been define before the full declaration of
19465 -- the private type. We also swap the entry in Private_Dependents list
19466 -- so we can properly restore the private view on exit from the scope.
19468 declare
19469 Priv_Elmt : Elmt_Id;
19470 Priv_Scop : Entity_Id;
19471 Priv : Entity_Id;
19472 Full : Entity_Id;
19474 begin
19475 Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
19476 while Present (Priv_Elmt) loop
19477 Priv := Node (Priv_Elmt);
19478 Priv_Scop := Scope (Priv);
19480 if Ekind_In (Priv, E_Private_Subtype,
19481 E_Limited_Private_Subtype,
19482 E_Record_Subtype_With_Private)
19483 then
19484 Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
19485 Set_Is_Itype (Full);
19486 Set_Parent (Full, Parent (Priv));
19487 Set_Associated_Node_For_Itype (Full, N);
19489 -- Now we need to complete the private subtype, but since the
19490 -- base type has already been swapped, we must also swap the
19491 -- subtypes (and thus, reverse the arguments in the call to
19492 -- Complete_Private_Subtype). Also note that we may need to
19493 -- re-establish the scope of the private subtype.
19495 Copy_And_Swap (Priv, Full);
19497 if not In_Open_Scopes (Priv_Scop) then
19498 Push_Scope (Priv_Scop);
19500 else
19501 -- Reset Priv_Scop to Empty to indicate no scope was pushed
19503 Priv_Scop := Empty;
19504 end if;
19506 Complete_Private_Subtype (Full, Priv, Full_T, N);
19508 if Present (Priv_Scop) then
19509 Pop_Scope;
19510 end if;
19512 Replace_Elmt (Priv_Elmt, Full);
19513 end if;
19515 Next_Elmt (Priv_Elmt);
19516 end loop;
19517 end;
19519 -- If the private view was tagged, copy the new primitive operations
19520 -- from the private view to the full view.
19522 if Is_Tagged_Type (Full_T) then
19523 declare
19524 Disp_Typ : Entity_Id;
19525 Full_List : Elist_Id;
19526 Prim : Entity_Id;
19527 Prim_Elmt : Elmt_Id;
19528 Priv_List : Elist_Id;
19530 function Contains
19531 (E : Entity_Id;
19532 L : Elist_Id) return Boolean;
19533 -- Determine whether list L contains element E
19535 --------------
19536 -- Contains --
19537 --------------
19539 function Contains
19540 (E : Entity_Id;
19541 L : Elist_Id) return Boolean
19543 List_Elmt : Elmt_Id;
19545 begin
19546 List_Elmt := First_Elmt (L);
19547 while Present (List_Elmt) loop
19548 if Node (List_Elmt) = E then
19549 return True;
19550 end if;
19552 Next_Elmt (List_Elmt);
19553 end loop;
19555 return False;
19556 end Contains;
19558 -- Start of processing
19560 begin
19561 if Is_Tagged_Type (Priv_T) then
19562 Priv_List := Primitive_Operations (Priv_T);
19563 Prim_Elmt := First_Elmt (Priv_List);
19565 -- In the case of a concurrent type completing a private tagged
19566 -- type, primitives may have been declared in between the two
19567 -- views. These subprograms need to be wrapped the same way
19568 -- entries and protected procedures are handled because they
19569 -- cannot be directly shared by the two views.
19571 if Is_Concurrent_Type (Full_T) then
19572 declare
19573 Conc_Typ : constant Entity_Id :=
19574 Corresponding_Record_Type (Full_T);
19575 Curr_Nod : Node_Id := Parent (Conc_Typ);
19576 Wrap_Spec : Node_Id;
19578 begin
19579 while Present (Prim_Elmt) loop
19580 Prim := Node (Prim_Elmt);
19582 if Comes_From_Source (Prim)
19583 and then not Is_Abstract_Subprogram (Prim)
19584 then
19585 Wrap_Spec :=
19586 Make_Subprogram_Declaration (Sloc (Prim),
19587 Specification =>
19588 Build_Wrapper_Spec
19589 (Subp_Id => Prim,
19590 Obj_Typ => Conc_Typ,
19591 Formals =>
19592 Parameter_Specifications (
19593 Parent (Prim))));
19595 Insert_After (Curr_Nod, Wrap_Spec);
19596 Curr_Nod := Wrap_Spec;
19598 Analyze (Wrap_Spec);
19599 end if;
19601 Next_Elmt (Prim_Elmt);
19602 end loop;
19604 return;
19605 end;
19607 -- For non-concurrent types, transfer explicit primitives, but
19608 -- omit those inherited from the parent of the private view
19609 -- since they will be re-inherited later on.
19611 else
19612 Full_List := Primitive_Operations (Full_T);
19614 while Present (Prim_Elmt) loop
19615 Prim := Node (Prim_Elmt);
19617 if Comes_From_Source (Prim)
19618 and then not Contains (Prim, Full_List)
19619 then
19620 Append_Elmt (Prim, Full_List);
19621 end if;
19623 Next_Elmt (Prim_Elmt);
19624 end loop;
19625 end if;
19627 -- Untagged private view
19629 else
19630 Full_List := Primitive_Operations (Full_T);
19632 -- In this case the partial view is untagged, so here we locate
19633 -- all of the earlier primitives that need to be treated as
19634 -- dispatching (those that appear between the two views). Note
19635 -- that these additional operations must all be new operations
19636 -- (any earlier operations that override inherited operations
19637 -- of the full view will already have been inserted in the
19638 -- primitives list, marked by Check_Operation_From_Private_View
19639 -- as dispatching. Note that implicit "/=" operators are
19640 -- excluded from being added to the primitives list since they
19641 -- shouldn't be treated as dispatching (tagged "/=" is handled
19642 -- specially).
19644 Prim := Next_Entity (Full_T);
19645 while Present (Prim) and then Prim /= Priv_T loop
19646 if Ekind_In (Prim, E_Procedure, E_Function) then
19647 Disp_Typ := Find_Dispatching_Type (Prim);
19649 if Disp_Typ = Full_T
19650 and then (Chars (Prim) /= Name_Op_Ne
19651 or else Comes_From_Source (Prim))
19652 then
19653 Check_Controlling_Formals (Full_T, Prim);
19655 if not Is_Dispatching_Operation (Prim) then
19656 Append_Elmt (Prim, Full_List);
19657 Set_Is_Dispatching_Operation (Prim, True);
19658 Set_DT_Position (Prim, No_Uint);
19659 end if;
19661 elsif Is_Dispatching_Operation (Prim)
19662 and then Disp_Typ /= Full_T
19663 then
19665 -- Verify that it is not otherwise controlled by a
19666 -- formal or a return value of type T.
19668 Check_Controlling_Formals (Disp_Typ, Prim);
19669 end if;
19670 end if;
19672 Next_Entity (Prim);
19673 end loop;
19674 end if;
19676 -- For the tagged case, the two views can share the same primitive
19677 -- operations list and the same class-wide type. Update attributes
19678 -- of the class-wide type which depend on the full declaration.
19680 if Is_Tagged_Type (Priv_T) then
19681 Set_Direct_Primitive_Operations (Priv_T, Full_List);
19682 Set_Class_Wide_Type
19683 (Base_Type (Full_T), Class_Wide_Type (Priv_T));
19685 Set_Has_Task (Class_Wide_Type (Priv_T), Has_Task (Full_T));
19686 Set_Has_Protected
19687 (Class_Wide_Type (Priv_T), Has_Protected (Full_T));
19688 end if;
19689 end;
19690 end if;
19692 -- Ada 2005 AI 161: Check preelaborable initialization consistency
19694 if Known_To_Have_Preelab_Init (Priv_T) then
19696 -- Case where there is a pragma Preelaborable_Initialization. We
19697 -- always allow this in predefined units, which is cheating a bit,
19698 -- but it means we don't have to struggle to meet the requirements in
19699 -- the RM for having Preelaborable Initialization. Otherwise we
19700 -- require that the type meets the RM rules. But we can't check that
19701 -- yet, because of the rule about overriding Initialize, so we simply
19702 -- set a flag that will be checked at freeze time.
19704 if not In_Predefined_Unit (Full_T) then
19705 Set_Must_Have_Preelab_Init (Full_T);
19706 end if;
19707 end if;
19709 -- If pragma CPP_Class was applied to the private type declaration,
19710 -- propagate it now to the full type declaration.
19712 if Is_CPP_Class (Priv_T) then
19713 Set_Is_CPP_Class (Full_T);
19714 Set_Convention (Full_T, Convention_CPP);
19716 -- Check that components of imported CPP types do not have default
19717 -- expressions.
19719 Check_CPP_Type_Has_No_Defaults (Full_T);
19720 end if;
19722 -- If the private view has user specified stream attributes, then so has
19723 -- the full view.
19725 -- Why the test, how could these flags be already set in Full_T ???
19727 if Has_Specified_Stream_Read (Priv_T) then
19728 Set_Has_Specified_Stream_Read (Full_T);
19729 end if;
19731 if Has_Specified_Stream_Write (Priv_T) then
19732 Set_Has_Specified_Stream_Write (Full_T);
19733 end if;
19735 if Has_Specified_Stream_Input (Priv_T) then
19736 Set_Has_Specified_Stream_Input (Full_T);
19737 end if;
19739 if Has_Specified_Stream_Output (Priv_T) then
19740 Set_Has_Specified_Stream_Output (Full_T);
19741 end if;
19743 -- Propagate the attributes related to pragma Default_Initial_Condition
19744 -- from the private to the full view. Note that both flags are mutually
19745 -- exclusive.
19747 if Has_Default_Init_Cond (Priv_T)
19748 or else Has_Inherited_Default_Init_Cond (Priv_T)
19749 then
19750 Propagate_Default_Init_Cond_Attributes
19751 (From_Typ => Priv_T,
19752 To_Typ => Full_T,
19753 Private_To_Full_View => True);
19755 -- In the case where the full view is derived from another private type,
19756 -- the attributes related to pragma Default_Initial_Condition must be
19757 -- propagated from the full to the private view to maintain consistency
19758 -- of views.
19760 -- package Pack is
19761 -- type Parent_Typ is private
19762 -- with Default_Initial_Condition ...;
19763 -- private
19764 -- type Parent_Typ is ...;
19765 -- end Pack;
19767 -- with Pack; use Pack;
19768 -- package Pack_2 is
19769 -- type Deriv_Typ is private; -- must inherit
19770 -- private
19771 -- type Deriv_Typ is new Parent_Typ; -- must inherit
19772 -- end Pack_2;
19774 elsif Has_Default_Init_Cond (Full_T)
19775 or else Has_Inherited_Default_Init_Cond (Full_T)
19776 then
19777 Propagate_Default_Init_Cond_Attributes
19778 (From_Typ => Full_T,
19779 To_Typ => Priv_T,
19780 Private_To_Full_View => True);
19781 end if;
19783 -- Propagate the attributes related to pragma Ghost from the private to
19784 -- the full view.
19786 if Is_Ghost_Entity (Priv_T) then
19787 Set_Is_Ghost_Entity (Full_T);
19789 -- The Ghost policy in effect at the point of declaration and at the
19790 -- point of completion must match (SPARK RM 6.9(15)).
19792 Check_Ghost_Completion (Priv_T, Full_T);
19794 -- In the case where the private view of a tagged type lacks a parent
19795 -- type and is subject to pragma Ghost, ensure that the parent type
19796 -- specified by the full view is also Ghost (SPARK RM 6.9(9)).
19798 if Is_Derived_Type (Full_T) then
19799 Check_Ghost_Derivation (Full_T);
19800 end if;
19801 end if;
19803 -- Propagate invariants to full type
19805 if Has_Invariants (Priv_T) then
19806 Set_Has_Invariants (Full_T);
19807 Set_Invariant_Procedure (Full_T, Invariant_Procedure (Priv_T));
19808 end if;
19810 if Has_Inheritable_Invariants (Priv_T) then
19811 Set_Has_Inheritable_Invariants (Full_T);
19812 end if;
19814 -- Propagate predicates to full type, and predicate function if already
19815 -- defined. It is not clear that this can actually happen? the partial
19816 -- view cannot be frozen yet, and the predicate function has not been
19817 -- built. Still it is a cheap check and seems safer to make it.
19819 if Has_Predicates (Priv_T) then
19820 if Present (Predicate_Function (Priv_T)) then
19821 Set_Predicate_Function (Full_T, Predicate_Function (Priv_T));
19822 end if;
19824 Set_Has_Predicates (Full_T);
19825 end if;
19826 end Process_Full_View;
19828 -----------------------------------
19829 -- Process_Incomplete_Dependents --
19830 -----------------------------------
19832 procedure Process_Incomplete_Dependents
19833 (N : Node_Id;
19834 Full_T : Entity_Id;
19835 Inc_T : Entity_Id)
19837 Inc_Elmt : Elmt_Id;
19838 Priv_Dep : Entity_Id;
19839 New_Subt : Entity_Id;
19841 Disc_Constraint : Elist_Id;
19843 begin
19844 if No (Private_Dependents (Inc_T)) then
19845 return;
19846 end if;
19848 -- Itypes that may be generated by the completion of an incomplete
19849 -- subtype are not used by the back-end and not attached to the tree.
19850 -- They are created only for constraint-checking purposes.
19852 Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
19853 while Present (Inc_Elmt) loop
19854 Priv_Dep := Node (Inc_Elmt);
19856 if Ekind (Priv_Dep) = E_Subprogram_Type then
19858 -- An Access_To_Subprogram type may have a return type or a
19859 -- parameter type that is incomplete. Replace with the full view.
19861 if Etype (Priv_Dep) = Inc_T then
19862 Set_Etype (Priv_Dep, Full_T);
19863 end if;
19865 declare
19866 Formal : Entity_Id;
19868 begin
19869 Formal := First_Formal (Priv_Dep);
19870 while Present (Formal) loop
19871 if Etype (Formal) = Inc_T then
19872 Set_Etype (Formal, Full_T);
19873 end if;
19875 Next_Formal (Formal);
19876 end loop;
19877 end;
19879 elsif Is_Overloadable (Priv_Dep) then
19881 -- If a subprogram in the incomplete dependents list is primitive
19882 -- for a tagged full type then mark it as a dispatching operation,
19883 -- check whether it overrides an inherited subprogram, and check
19884 -- restrictions on its controlling formals. Note that a protected
19885 -- operation is never dispatching: only its wrapper operation
19886 -- (which has convention Ada) is.
19888 if Is_Tagged_Type (Full_T)
19889 and then Is_Primitive (Priv_Dep)
19890 and then Convention (Priv_Dep) /= Convention_Protected
19891 then
19892 Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
19893 Set_Is_Dispatching_Operation (Priv_Dep);
19894 Check_Controlling_Formals (Full_T, Priv_Dep);
19895 end if;
19897 elsif Ekind (Priv_Dep) = E_Subprogram_Body then
19899 -- Can happen during processing of a body before the completion
19900 -- of a TA type. Ignore, because spec is also on dependent list.
19902 return;
19904 -- Ada 2005 (AI-412): Transform a regular incomplete subtype into a
19905 -- corresponding subtype of the full view.
19907 elsif Ekind (Priv_Dep) = E_Incomplete_Subtype then
19908 Set_Subtype_Indication
19909 (Parent (Priv_Dep), New_Occurrence_Of (Full_T, Sloc (Priv_Dep)));
19910 Set_Etype (Priv_Dep, Full_T);
19911 Set_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
19912 Set_Analyzed (Parent (Priv_Dep), False);
19914 -- Reanalyze the declaration, suppressing the call to
19915 -- Enter_Name to avoid duplicate names.
19917 Analyze_Subtype_Declaration
19918 (N => Parent (Priv_Dep),
19919 Skip => True);
19921 -- Dependent is a subtype
19923 else
19924 -- We build a new subtype indication using the full view of the
19925 -- incomplete parent. The discriminant constraints have been
19926 -- elaborated already at the point of the subtype declaration.
19928 New_Subt := Create_Itype (E_Void, N);
19930 if Has_Discriminants (Full_T) then
19931 Disc_Constraint := Discriminant_Constraint (Priv_Dep);
19932 else
19933 Disc_Constraint := No_Elist;
19934 end if;
19936 Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
19937 Set_Full_View (Priv_Dep, New_Subt);
19938 end if;
19940 Next_Elmt (Inc_Elmt);
19941 end loop;
19942 end Process_Incomplete_Dependents;
19944 --------------------------------
19945 -- Process_Range_Expr_In_Decl --
19946 --------------------------------
19948 procedure Process_Range_Expr_In_Decl
19949 (R : Node_Id;
19950 T : Entity_Id;
19951 Subtyp : Entity_Id := Empty;
19952 Check_List : List_Id := Empty_List;
19953 R_Check_Off : Boolean := False;
19954 In_Iter_Schm : Boolean := False)
19956 Lo, Hi : Node_Id;
19957 R_Checks : Check_Result;
19958 Insert_Node : Node_Id;
19959 Def_Id : Entity_Id;
19961 begin
19962 Analyze_And_Resolve (R, Base_Type (T));
19964 if Nkind (R) = N_Range then
19966 -- In SPARK, all ranges should be static, with the exception of the
19967 -- discrete type definition of a loop parameter specification.
19969 if not In_Iter_Schm
19970 and then not Is_OK_Static_Range (R)
19971 then
19972 Check_SPARK_05_Restriction ("range should be static", R);
19973 end if;
19975 Lo := Low_Bound (R);
19976 Hi := High_Bound (R);
19978 -- Validity checks on the range of a quantified expression are
19979 -- delayed until the construct is transformed into a loop.
19981 if Nkind (Parent (R)) = N_Loop_Parameter_Specification
19982 and then Nkind (Parent (Parent (R))) = N_Quantified_Expression
19983 then
19984 null;
19986 -- We need to ensure validity of the bounds here, because if we
19987 -- go ahead and do the expansion, then the expanded code will get
19988 -- analyzed with range checks suppressed and we miss the check.
19990 -- WARNING: The capture of the range bounds with xxx_FIRST/_LAST and
19991 -- the temporaries generated by routine Remove_Side_Effects by means
19992 -- of validity checks must use the same names. When a range appears
19993 -- in the parent of a generic, the range is processed with checks
19994 -- disabled as part of the generic context and with checks enabled
19995 -- for code generation purposes. This leads to link issues as the
19996 -- generic contains references to xxx_FIRST/_LAST, but the inlined
19997 -- template sees the temporaries generated by Remove_Side_Effects.
19999 else
20000 Validity_Check_Range (R, Subtyp);
20001 end if;
20003 -- If there were errors in the declaration, try and patch up some
20004 -- common mistakes in the bounds. The cases handled are literals
20005 -- which are Integer where the expected type is Real and vice versa.
20006 -- These corrections allow the compilation process to proceed further
20007 -- along since some basic assumptions of the format of the bounds
20008 -- are guaranteed.
20010 if Etype (R) = Any_Type then
20011 if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
20012 Rewrite (Lo,
20013 Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
20015 elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
20016 Rewrite (Hi,
20017 Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
20019 elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
20020 Rewrite (Lo,
20021 Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
20023 elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
20024 Rewrite (Hi,
20025 Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
20026 end if;
20028 Set_Etype (Lo, T);
20029 Set_Etype (Hi, T);
20030 end if;
20032 -- If the bounds of the range have been mistakenly given as string
20033 -- literals (perhaps in place of character literals), then an error
20034 -- has already been reported, but we rewrite the string literal as a
20035 -- bound of the range's type to avoid blowups in later processing
20036 -- that looks at static values.
20038 if Nkind (Lo) = N_String_Literal then
20039 Rewrite (Lo,
20040 Make_Attribute_Reference (Sloc (Lo),
20041 Prefix => New_Occurrence_Of (T, Sloc (Lo)),
20042 Attribute_Name => Name_First));
20043 Analyze_And_Resolve (Lo);
20044 end if;
20046 if Nkind (Hi) = N_String_Literal then
20047 Rewrite (Hi,
20048 Make_Attribute_Reference (Sloc (Hi),
20049 Prefix => New_Occurrence_Of (T, Sloc (Hi)),
20050 Attribute_Name => Name_First));
20051 Analyze_And_Resolve (Hi);
20052 end if;
20054 -- If bounds aren't scalar at this point then exit, avoiding
20055 -- problems with further processing of the range in this procedure.
20057 if not Is_Scalar_Type (Etype (Lo)) then
20058 return;
20059 end if;
20061 -- Resolve (actually Sem_Eval) has checked that the bounds are in
20062 -- then range of the base type. Here we check whether the bounds
20063 -- are in the range of the subtype itself. Note that if the bounds
20064 -- represent the null range the Constraint_Error exception should
20065 -- not be raised.
20067 -- ??? The following code should be cleaned up as follows
20069 -- 1. The Is_Null_Range (Lo, Hi) test should disappear since it
20070 -- is done in the call to Range_Check (R, T); below
20072 -- 2. The use of R_Check_Off should be investigated and possibly
20073 -- removed, this would clean up things a bit.
20075 if Is_Null_Range (Lo, Hi) then
20076 null;
20078 else
20079 -- Capture values of bounds and generate temporaries for them
20080 -- if needed, before applying checks, since checks may cause
20081 -- duplication of the expression without forcing evaluation.
20083 -- The forced evaluation removes side effects from expressions,
20084 -- which should occur also in GNATprove mode. Otherwise, we end up
20085 -- with unexpected insertions of actions at places where this is
20086 -- not supposed to occur, e.g. on default parameters of a call.
20088 if Expander_Active or GNATprove_Mode then
20090 -- If no subtype name, then just call Force_Evaluation to
20091 -- create declarations as needed to deal with side effects.
20092 -- Also ignore calls from within a record type, where we
20093 -- have possible scoping issues.
20095 if No (Subtyp) or else Is_Record_Type (Current_Scope) then
20096 Force_Evaluation (Lo);
20097 Force_Evaluation (Hi);
20099 -- If a subtype is given, then we capture the bounds if they
20100 -- are not known at compile time, using constant identifiers
20101 -- xxx_FIRST and xxx_LAST where xxx is the name of the subtype.
20103 -- Note: we do this transformation even if expansion is not
20104 -- active, and in particular we do it in GNATprove_Mode since
20105 -- the transformation is in general required to ensure that the
20106 -- resulting tree has proper Ada semantics.
20108 -- Historical note: We used to just do Force_Evaluation calls
20109 -- in all cases, but it is better to capture the bounds with
20110 -- proper non-serialized names, since these will be accessed
20111 -- from other units, and hence may be public, and also we can
20112 -- then expand 'First and 'Last references to be references to
20113 -- these special names.
20115 else
20116 if not Compile_Time_Known_Value (Lo)
20118 -- No need to capture bounds if they already are
20119 -- references to constants.
20121 and then not (Is_Entity_Name (Lo)
20122 and then Is_Constant_Object (Entity (Lo)))
20123 then
20124 declare
20125 Loc : constant Source_Ptr := Sloc (Lo);
20126 Lov : constant Entity_Id :=
20127 Make_Defining_Identifier (Loc,
20128 Chars =>
20129 New_External_Name (Chars (Subtyp), "_FIRST"));
20130 begin
20131 Insert_Action (R,
20132 Make_Object_Declaration (Loc,
20133 Defining_Identifier => Lov,
20134 Object_Definition =>
20135 New_Occurrence_Of (Base_Type (T), Loc),
20136 Constant_Present => True,
20137 Expression => Relocate_Node (Lo)));
20138 Rewrite (Lo, New_Occurrence_Of (Lov, Loc));
20139 end;
20140 end if;
20142 if not Compile_Time_Known_Value (Hi)
20143 and then not (Is_Entity_Name (Hi)
20144 and then Is_Constant_Object (Entity (Hi)))
20145 then
20146 declare
20147 Loc : constant Source_Ptr := Sloc (Hi);
20148 Hiv : constant Entity_Id :=
20149 Make_Defining_Identifier (Loc,
20150 Chars =>
20151 New_External_Name (Chars (Subtyp), "_LAST"));
20152 begin
20153 Insert_Action (R,
20154 Make_Object_Declaration (Loc,
20155 Defining_Identifier => Hiv,
20156 Object_Definition =>
20157 New_Occurrence_Of (Base_Type (T), Loc),
20158 Constant_Present => True,
20159 Expression => Relocate_Node (Hi)));
20160 Rewrite (Hi, New_Occurrence_Of (Hiv, Loc));
20161 end;
20162 end if;
20163 end if;
20164 end if;
20166 -- We use a flag here instead of suppressing checks on the
20167 -- type because the type we check against isn't necessarily
20168 -- the place where we put the check.
20170 if not R_Check_Off then
20171 R_Checks := Get_Range_Checks (R, T);
20173 -- Look up tree to find an appropriate insertion point. We
20174 -- can't just use insert_actions because later processing
20175 -- depends on the insertion node. Prior to Ada 2012 the
20176 -- insertion point could only be a declaration or a loop, but
20177 -- quantified expressions can appear within any context in an
20178 -- expression, and the insertion point can be any statement,
20179 -- pragma, or declaration.
20181 Insert_Node := Parent (R);
20182 while Present (Insert_Node) loop
20183 exit when
20184 Nkind (Insert_Node) in N_Declaration
20185 and then
20186 not Nkind_In
20187 (Insert_Node, N_Component_Declaration,
20188 N_Loop_Parameter_Specification,
20189 N_Function_Specification,
20190 N_Procedure_Specification);
20192 exit when Nkind (Insert_Node) in N_Later_Decl_Item
20193 or else Nkind (Insert_Node) in
20194 N_Statement_Other_Than_Procedure_Call
20195 or else Nkind_In (Insert_Node, N_Procedure_Call_Statement,
20196 N_Pragma);
20198 Insert_Node := Parent (Insert_Node);
20199 end loop;
20201 -- Why would Type_Decl not be present??? Without this test,
20202 -- short regression tests fail.
20204 if Present (Insert_Node) then
20206 -- Case of loop statement. Verify that the range is part
20207 -- of the subtype indication of the iteration scheme.
20209 if Nkind (Insert_Node) = N_Loop_Statement then
20210 declare
20211 Indic : Node_Id;
20213 begin
20214 Indic := Parent (R);
20215 while Present (Indic)
20216 and then Nkind (Indic) /= N_Subtype_Indication
20217 loop
20218 Indic := Parent (Indic);
20219 end loop;
20221 if Present (Indic) then
20222 Def_Id := Etype (Subtype_Mark (Indic));
20224 Insert_Range_Checks
20225 (R_Checks,
20226 Insert_Node,
20227 Def_Id,
20228 Sloc (Insert_Node),
20230 Do_Before => True);
20231 end if;
20232 end;
20234 -- Insertion before a declaration. If the declaration
20235 -- includes discriminants, the list of applicable checks
20236 -- is given by the caller.
20238 elsif Nkind (Insert_Node) in N_Declaration then
20239 Def_Id := Defining_Identifier (Insert_Node);
20241 if (Ekind (Def_Id) = E_Record_Type
20242 and then Depends_On_Discriminant (R))
20243 or else
20244 (Ekind (Def_Id) = E_Protected_Type
20245 and then Has_Discriminants (Def_Id))
20246 then
20247 Append_Range_Checks
20248 (R_Checks,
20249 Check_List, Def_Id, Sloc (Insert_Node), R);
20251 else
20252 Insert_Range_Checks
20253 (R_Checks,
20254 Insert_Node, Def_Id, Sloc (Insert_Node), R);
20256 end if;
20258 -- Insertion before a statement. Range appears in the
20259 -- context of a quantified expression. Insertion will
20260 -- take place when expression is expanded.
20262 else
20263 null;
20264 end if;
20265 end if;
20266 end if;
20267 end if;
20269 -- Case of other than an explicit N_Range node
20271 -- The forced evaluation removes side effects from expressions, which
20272 -- should occur also in GNATprove mode. Otherwise, we end up with
20273 -- unexpected insertions of actions at places where this is not
20274 -- supposed to occur, e.g. on default parameters of a call.
20276 elsif Expander_Active or GNATprove_Mode then
20277 Get_Index_Bounds (R, Lo, Hi);
20278 Force_Evaluation (Lo);
20279 Force_Evaluation (Hi);
20280 end if;
20281 end Process_Range_Expr_In_Decl;
20283 --------------------------------------
20284 -- Process_Real_Range_Specification --
20285 --------------------------------------
20287 procedure Process_Real_Range_Specification (Def : Node_Id) is
20288 Spec : constant Node_Id := Real_Range_Specification (Def);
20289 Lo : Node_Id;
20290 Hi : Node_Id;
20291 Err : Boolean := False;
20293 procedure Analyze_Bound (N : Node_Id);
20294 -- Analyze and check one bound
20296 -------------------
20297 -- Analyze_Bound --
20298 -------------------
20300 procedure Analyze_Bound (N : Node_Id) is
20301 begin
20302 Analyze_And_Resolve (N, Any_Real);
20304 if not Is_OK_Static_Expression (N) then
20305 Flag_Non_Static_Expr
20306 ("bound in real type definition is not static!", N);
20307 Err := True;
20308 end if;
20309 end Analyze_Bound;
20311 -- Start of processing for Process_Real_Range_Specification
20313 begin
20314 if Present (Spec) then
20315 Lo := Low_Bound (Spec);
20316 Hi := High_Bound (Spec);
20317 Analyze_Bound (Lo);
20318 Analyze_Bound (Hi);
20320 -- If error, clear away junk range specification
20322 if Err then
20323 Set_Real_Range_Specification (Def, Empty);
20324 end if;
20325 end if;
20326 end Process_Real_Range_Specification;
20328 ---------------------
20329 -- Process_Subtype --
20330 ---------------------
20332 function Process_Subtype
20333 (S : Node_Id;
20334 Related_Nod : Node_Id;
20335 Related_Id : Entity_Id := Empty;
20336 Suffix : Character := ' ') return Entity_Id
20338 P : Node_Id;
20339 Def_Id : Entity_Id;
20340 Error_Node : Node_Id;
20341 Full_View_Id : Entity_Id;
20342 Subtype_Mark_Id : Entity_Id;
20344 May_Have_Null_Exclusion : Boolean;
20346 procedure Check_Incomplete (T : Entity_Id);
20347 -- Called to verify that an incomplete type is not used prematurely
20349 ----------------------
20350 -- Check_Incomplete --
20351 ----------------------
20353 procedure Check_Incomplete (T : Entity_Id) is
20354 begin
20355 -- Ada 2005 (AI-412): Incomplete subtypes are legal
20357 if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
20358 and then
20359 not (Ada_Version >= Ada_2005
20360 and then
20361 (Nkind (Parent (T)) = N_Subtype_Declaration
20362 or else (Nkind (Parent (T)) = N_Subtype_Indication
20363 and then Nkind (Parent (Parent (T))) =
20364 N_Subtype_Declaration)))
20365 then
20366 Error_Msg_N ("invalid use of type before its full declaration", T);
20367 end if;
20368 end Check_Incomplete;
20370 -- Start of processing for Process_Subtype
20372 begin
20373 -- Case of no constraints present
20375 if Nkind (S) /= N_Subtype_Indication then
20376 Find_Type (S);
20377 Check_Incomplete (S);
20378 P := Parent (S);
20380 -- Ada 2005 (AI-231): Static check
20382 if Ada_Version >= Ada_2005
20383 and then Present (P)
20384 and then Null_Exclusion_Present (P)
20385 and then Nkind (P) /= N_Access_To_Object_Definition
20386 and then not Is_Access_Type (Entity (S))
20387 then
20388 Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
20389 end if;
20391 -- The following is ugly, can't we have a range or even a flag???
20393 May_Have_Null_Exclusion :=
20394 Nkind_In (P, N_Access_Definition,
20395 N_Access_Function_Definition,
20396 N_Access_Procedure_Definition,
20397 N_Access_To_Object_Definition,
20398 N_Allocator,
20399 N_Component_Definition)
20400 or else
20401 Nkind_In (P, N_Derived_Type_Definition,
20402 N_Discriminant_Specification,
20403 N_Formal_Object_Declaration,
20404 N_Object_Declaration,
20405 N_Object_Renaming_Declaration,
20406 N_Parameter_Specification,
20407 N_Subtype_Declaration);
20409 -- Create an Itype that is a duplicate of Entity (S) but with the
20410 -- null-exclusion attribute.
20412 if May_Have_Null_Exclusion
20413 and then Is_Access_Type (Entity (S))
20414 and then Null_Exclusion_Present (P)
20416 -- No need to check the case of an access to object definition.
20417 -- It is correct to define double not-null pointers.
20419 -- Example:
20420 -- type Not_Null_Int_Ptr is not null access Integer;
20421 -- type Acc is not null access Not_Null_Int_Ptr;
20423 and then Nkind (P) /= N_Access_To_Object_Definition
20424 then
20425 if Can_Never_Be_Null (Entity (S)) then
20426 case Nkind (Related_Nod) is
20427 when N_Full_Type_Declaration =>
20428 if Nkind (Type_Definition (Related_Nod))
20429 in N_Array_Type_Definition
20430 then
20431 Error_Node :=
20432 Subtype_Indication
20433 (Component_Definition
20434 (Type_Definition (Related_Nod)));
20435 else
20436 Error_Node :=
20437 Subtype_Indication (Type_Definition (Related_Nod));
20438 end if;
20440 when N_Subtype_Declaration =>
20441 Error_Node := Subtype_Indication (Related_Nod);
20443 when N_Object_Declaration =>
20444 Error_Node := Object_Definition (Related_Nod);
20446 when N_Component_Declaration =>
20447 Error_Node :=
20448 Subtype_Indication (Component_Definition (Related_Nod));
20450 when N_Allocator =>
20451 Error_Node := Expression (Related_Nod);
20453 when others =>
20454 pragma Assert (False);
20455 Error_Node := Related_Nod;
20456 end case;
20458 Error_Msg_NE
20459 ("`NOT NULL` not allowed (& already excludes null)",
20460 Error_Node,
20461 Entity (S));
20462 end if;
20464 Set_Etype (S,
20465 Create_Null_Excluding_Itype
20466 (T => Entity (S),
20467 Related_Nod => P));
20468 Set_Entity (S, Etype (S));
20469 end if;
20471 return Entity (S);
20473 -- Case of constraint present, so that we have an N_Subtype_Indication
20474 -- node (this node is created only if constraints are present).
20476 else
20477 Find_Type (Subtype_Mark (S));
20479 if Nkind (Parent (S)) /= N_Access_To_Object_Definition
20480 and then not
20481 (Nkind (Parent (S)) = N_Subtype_Declaration
20482 and then Is_Itype (Defining_Identifier (Parent (S))))
20483 then
20484 Check_Incomplete (Subtype_Mark (S));
20485 end if;
20487 P := Parent (S);
20488 Subtype_Mark_Id := Entity (Subtype_Mark (S));
20490 -- Explicit subtype declaration case
20492 if Nkind (P) = N_Subtype_Declaration then
20493 Def_Id := Defining_Identifier (P);
20495 -- Explicit derived type definition case
20497 elsif Nkind (P) = N_Derived_Type_Definition then
20498 Def_Id := Defining_Identifier (Parent (P));
20500 -- Implicit case, the Def_Id must be created as an implicit type.
20501 -- The one exception arises in the case of concurrent types, array
20502 -- and access types, where other subsidiary implicit types may be
20503 -- created and must appear before the main implicit type. In these
20504 -- cases we leave Def_Id set to Empty as a signal that Create_Itype
20505 -- has not yet been called to create Def_Id.
20507 else
20508 if Is_Array_Type (Subtype_Mark_Id)
20509 or else Is_Concurrent_Type (Subtype_Mark_Id)
20510 or else Is_Access_Type (Subtype_Mark_Id)
20511 then
20512 Def_Id := Empty;
20514 -- For the other cases, we create a new unattached Itype,
20515 -- and set the indication to ensure it gets attached later.
20517 else
20518 Def_Id :=
20519 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
20520 end if;
20521 end if;
20523 -- If the kind of constraint is invalid for this kind of type,
20524 -- then give an error, and then pretend no constraint was given.
20526 if not Is_Valid_Constraint_Kind
20527 (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
20528 then
20529 Error_Msg_N
20530 ("incorrect constraint for this kind of type", Constraint (S));
20532 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
20534 -- Set Ekind of orphan itype, to prevent cascaded errors
20536 if Present (Def_Id) then
20537 Set_Ekind (Def_Id, Ekind (Any_Type));
20538 end if;
20540 -- Make recursive call, having got rid of the bogus constraint
20542 return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
20543 end if;
20545 -- Remaining processing depends on type. Select on Base_Type kind to
20546 -- ensure getting to the concrete type kind in the case of a private
20547 -- subtype (needed when only doing semantic analysis).
20549 case Ekind (Base_Type (Subtype_Mark_Id)) is
20550 when Access_Kind =>
20552 -- If this is a constraint on a class-wide type, discard it.
20553 -- There is currently no way to express a partial discriminant
20554 -- constraint on a type with unknown discriminants. This is
20555 -- a pathology that the ACATS wisely decides not to test.
20557 if Is_Class_Wide_Type (Designated_Type (Subtype_Mark_Id)) then
20558 if Comes_From_Source (S) then
20559 Error_Msg_N
20560 ("constraint on class-wide type ignored??",
20561 Constraint (S));
20562 end if;
20564 if Nkind (P) = N_Subtype_Declaration then
20565 Set_Subtype_Indication (P,
20566 New_Occurrence_Of (Subtype_Mark_Id, Sloc (S)));
20567 end if;
20569 return Subtype_Mark_Id;
20570 end if;
20572 Constrain_Access (Def_Id, S, Related_Nod);
20574 if Expander_Active
20575 and then Is_Itype (Designated_Type (Def_Id))
20576 and then Nkind (Related_Nod) = N_Subtype_Declaration
20577 and then not Is_Incomplete_Type (Designated_Type (Def_Id))
20578 then
20579 Build_Itype_Reference
20580 (Designated_Type (Def_Id), Related_Nod);
20581 end if;
20583 when Array_Kind =>
20584 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
20586 when Decimal_Fixed_Point_Kind =>
20587 Constrain_Decimal (Def_Id, S);
20589 when Enumeration_Kind =>
20590 Constrain_Enumeration (Def_Id, S);
20591 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
20593 when Ordinary_Fixed_Point_Kind =>
20594 Constrain_Ordinary_Fixed (Def_Id, S);
20596 when Float_Kind =>
20597 Constrain_Float (Def_Id, S);
20599 when Integer_Kind =>
20600 Constrain_Integer (Def_Id, S);
20601 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
20603 when E_Record_Type |
20604 E_Record_Subtype |
20605 Class_Wide_Kind |
20606 E_Incomplete_Type =>
20607 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
20609 if Ekind (Def_Id) = E_Incomplete_Type then
20610 Set_Private_Dependents (Def_Id, New_Elmt_List);
20611 end if;
20613 when Private_Kind =>
20614 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
20615 Set_Private_Dependents (Def_Id, New_Elmt_List);
20617 -- In case of an invalid constraint prevent further processing
20618 -- since the type constructed is missing expected fields.
20620 if Etype (Def_Id) = Any_Type then
20621 return Def_Id;
20622 end if;
20624 -- If the full view is that of a task with discriminants,
20625 -- we must constrain both the concurrent type and its
20626 -- corresponding record type. Otherwise we will just propagate
20627 -- the constraint to the full view, if available.
20629 if Present (Full_View (Subtype_Mark_Id))
20630 and then Has_Discriminants (Subtype_Mark_Id)
20631 and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
20632 then
20633 Full_View_Id :=
20634 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
20636 Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
20637 Constrain_Concurrent (Full_View_Id, S,
20638 Related_Nod, Related_Id, Suffix);
20639 Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
20640 Set_Full_View (Def_Id, Full_View_Id);
20642 -- Introduce an explicit reference to the private subtype,
20643 -- to prevent scope anomalies in gigi if first use appears
20644 -- in a nested context, e.g. a later function body.
20645 -- Should this be generated in other contexts than a full
20646 -- type declaration?
20648 if Is_Itype (Def_Id)
20649 and then
20650 Nkind (Parent (P)) = N_Full_Type_Declaration
20651 then
20652 Build_Itype_Reference (Def_Id, Parent (P));
20653 end if;
20655 else
20656 Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
20657 end if;
20659 when Concurrent_Kind =>
20660 Constrain_Concurrent (Def_Id, S,
20661 Related_Nod, Related_Id, Suffix);
20663 when others =>
20664 Error_Msg_N ("invalid subtype mark in subtype indication", S);
20665 end case;
20667 -- Size and Convention are always inherited from the base type
20669 Set_Size_Info (Def_Id, (Subtype_Mark_Id));
20670 Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
20672 return Def_Id;
20673 end if;
20674 end Process_Subtype;
20676 --------------------------------------------
20677 -- Propagate_Default_Init_Cond_Attributes --
20678 --------------------------------------------
20680 procedure Propagate_Default_Init_Cond_Attributes
20681 (From_Typ : Entity_Id;
20682 To_Typ : Entity_Id;
20683 Parent_To_Derivation : Boolean := False;
20684 Private_To_Full_View : Boolean := False)
20686 procedure Remove_Default_Init_Cond_Procedure (Typ : Entity_Id);
20687 -- Remove the default initial procedure (if any) from the rep chain of
20688 -- type Typ.
20690 ----------------------------------------
20691 -- Remove_Default_Init_Cond_Procedure --
20692 ----------------------------------------
20694 procedure Remove_Default_Init_Cond_Procedure (Typ : Entity_Id) is
20695 Found : Boolean := False;
20696 Prev : Entity_Id;
20697 Subp : Entity_Id;
20699 begin
20700 Prev := Typ;
20701 Subp := Subprograms_For_Type (Typ);
20702 while Present (Subp) loop
20703 if Is_Default_Init_Cond_Procedure (Subp) then
20704 Found := True;
20705 exit;
20706 end if;
20708 Prev := Subp;
20709 Subp := Subprograms_For_Type (Subp);
20710 end loop;
20712 if Found then
20713 Set_Subprograms_For_Type (Prev, Subprograms_For_Type (Subp));
20714 Set_Subprograms_For_Type (Subp, Empty);
20715 end if;
20716 end Remove_Default_Init_Cond_Procedure;
20718 -- Local variables
20720 Inherit_Procedure : Boolean := False;
20722 -- Start of processing for Propagate_Default_Init_Cond_Attributes
20724 begin
20725 if Has_Default_Init_Cond (From_Typ) then
20727 -- A derived type inherits the attributes from its parent type
20729 if Parent_To_Derivation then
20730 Set_Has_Inherited_Default_Init_Cond (To_Typ);
20732 -- A full view shares the attributes with its private view
20734 else
20735 Set_Has_Default_Init_Cond (To_Typ);
20736 end if;
20738 Inherit_Procedure := True;
20740 -- Due to the order of expansion, a derived private type is processed
20741 -- by two routines which both attempt to set the attributes related
20742 -- to pragma Default_Initial_Condition - Build_Derived_Type and then
20743 -- Process_Full_View.
20745 -- package Pack is
20746 -- type Parent_Typ is private
20747 -- with Default_Initial_Condition ...;
20748 -- private
20749 -- type Parent_Typ is ...;
20750 -- end Pack;
20752 -- with Pack; use Pack;
20753 -- package Pack_2 is
20754 -- type Deriv_Typ is private
20755 -- with Default_Initial_Condition ...;
20756 -- private
20757 -- type Deriv_Typ is new Parent_Typ;
20758 -- end Pack_2;
20760 -- When Build_Derived_Type operates, it sets the attributes on the
20761 -- full view without taking into account that the private view may
20762 -- define its own default initial condition procedure. This becomes
20763 -- apparent in Process_Full_View which must undo some of the work by
20764 -- Build_Derived_Type and propagate the attributes from the private
20765 -- to the full view.
20767 if Private_To_Full_View then
20768 Set_Has_Inherited_Default_Init_Cond (To_Typ, False);
20769 Remove_Default_Init_Cond_Procedure (To_Typ);
20770 end if;
20772 -- A type must inherit the default initial condition procedure from a
20773 -- parent type when the parent itself is inheriting the procedure or
20774 -- when it is defining one. This circuitry is also used when dealing
20775 -- with the private / full view of a type.
20777 elsif Has_Inherited_Default_Init_Cond (From_Typ)
20778 or (Parent_To_Derivation
20779 and Present (Get_Pragma
20780 (From_Typ, Pragma_Default_Initial_Condition)))
20781 then
20782 Set_Has_Inherited_Default_Init_Cond (To_Typ);
20783 Inherit_Procedure := True;
20784 end if;
20786 if Inherit_Procedure
20787 and then No (Default_Init_Cond_Procedure (To_Typ))
20788 then
20789 Set_Default_Init_Cond_Procedure
20790 (To_Typ, Default_Init_Cond_Procedure (From_Typ));
20791 end if;
20792 end Propagate_Default_Init_Cond_Attributes;
20794 -----------------------------
20795 -- Record_Type_Declaration --
20796 -----------------------------
20798 procedure Record_Type_Declaration
20799 (T : Entity_Id;
20800 N : Node_Id;
20801 Prev : Entity_Id)
20803 Def : constant Node_Id := Type_Definition (N);
20804 Is_Tagged : Boolean;
20805 Tag_Comp : Entity_Id;
20807 begin
20808 -- These flags must be initialized before calling Process_Discriminants
20809 -- because this routine makes use of them.
20811 Set_Ekind (T, E_Record_Type);
20812 Set_Etype (T, T);
20813 Init_Size_Align (T);
20814 Set_Interfaces (T, No_Elist);
20815 Set_Stored_Constraint (T, No_Elist);
20816 Set_Default_SSO (T);
20818 -- Normal case
20820 if Ada_Version < Ada_2005 or else not Interface_Present (Def) then
20821 if Limited_Present (Def) then
20822 Check_SPARK_05_Restriction ("limited is not allowed", N);
20823 end if;
20825 if Abstract_Present (Def) then
20826 Check_SPARK_05_Restriction ("abstract is not allowed", N);
20827 end if;
20829 -- The flag Is_Tagged_Type might have already been set by
20830 -- Find_Type_Name if it detected an error for declaration T. This
20831 -- arises in the case of private tagged types where the full view
20832 -- omits the word tagged.
20834 Is_Tagged :=
20835 Tagged_Present (Def)
20836 or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
20838 Set_Is_Limited_Record (T, Limited_Present (Def));
20840 if Is_Tagged then
20841 Set_Is_Tagged_Type (T, True);
20842 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
20843 end if;
20845 -- Type is abstract if full declaration carries keyword, or if
20846 -- previous partial view did.
20848 Set_Is_Abstract_Type (T, Is_Abstract_Type (T)
20849 or else Abstract_Present (Def));
20851 else
20852 Check_SPARK_05_Restriction ("interface is not allowed", N);
20854 Is_Tagged := True;
20855 Analyze_Interface_Declaration (T, Def);
20857 if Present (Discriminant_Specifications (N)) then
20858 Error_Msg_N
20859 ("interface types cannot have discriminants",
20860 Defining_Identifier
20861 (First (Discriminant_Specifications (N))));
20862 end if;
20863 end if;
20865 -- First pass: if there are self-referential access components,
20866 -- create the required anonymous access type declarations, and if
20867 -- need be an incomplete type declaration for T itself.
20869 Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
20871 if Ada_Version >= Ada_2005
20872 and then Present (Interface_List (Def))
20873 then
20874 Check_Interfaces (N, Def);
20876 declare
20877 Ifaces_List : Elist_Id;
20879 begin
20880 -- Ada 2005 (AI-251): Collect the list of progenitors that are not
20881 -- already in the parents.
20883 Collect_Interfaces
20884 (T => T,
20885 Ifaces_List => Ifaces_List,
20886 Exclude_Parents => True);
20888 Set_Interfaces (T, Ifaces_List);
20889 end;
20890 end if;
20892 -- Records constitute a scope for the component declarations within.
20893 -- The scope is created prior to the processing of these declarations.
20894 -- Discriminants are processed first, so that they are visible when
20895 -- processing the other components. The Ekind of the record type itself
20896 -- is set to E_Record_Type (subtypes appear as E_Record_Subtype).
20898 -- Enter record scope
20900 Push_Scope (T);
20902 -- If an incomplete or private type declaration was already given for
20903 -- the type, then this scope already exists, and the discriminants have
20904 -- been declared within. We must verify that the full declaration
20905 -- matches the incomplete one.
20907 Check_Or_Process_Discriminants (N, T, Prev);
20909 Set_Is_Constrained (T, not Has_Discriminants (T));
20910 Set_Has_Delayed_Freeze (T, True);
20912 -- For tagged types add a manually analyzed component corresponding
20913 -- to the component _tag, the corresponding piece of tree will be
20914 -- expanded as part of the freezing actions if it is not a CPP_Class.
20916 if Is_Tagged then
20918 -- Do not add the tag unless we are in expansion mode
20920 if Expander_Active then
20921 Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
20922 Enter_Name (Tag_Comp);
20924 Set_Ekind (Tag_Comp, E_Component);
20925 Set_Is_Tag (Tag_Comp);
20926 Set_Is_Aliased (Tag_Comp);
20927 Set_Etype (Tag_Comp, RTE (RE_Tag));
20928 Set_DT_Entry_Count (Tag_Comp, No_Uint);
20929 Set_Original_Record_Component (Tag_Comp, Tag_Comp);
20930 Init_Component_Location (Tag_Comp);
20932 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
20933 -- implemented interfaces.
20935 if Has_Interfaces (T) then
20936 Add_Interface_Tag_Components (N, T);
20937 end if;
20938 end if;
20940 Make_Class_Wide_Type (T);
20941 Set_Direct_Primitive_Operations (T, New_Elmt_List);
20942 end if;
20944 -- We must suppress range checks when processing record components in
20945 -- the presence of discriminants, since we don't want spurious checks to
20946 -- be generated during their analysis, but Suppress_Range_Checks flags
20947 -- must be reset the after processing the record definition.
20949 -- Note: this is the only use of Kill_Range_Checks, and is a bit odd,
20950 -- couldn't we just use the normal range check suppression method here.
20951 -- That would seem cleaner ???
20953 if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
20954 Set_Kill_Range_Checks (T, True);
20955 Record_Type_Definition (Def, Prev);
20956 Set_Kill_Range_Checks (T, False);
20957 else
20958 Record_Type_Definition (Def, Prev);
20959 end if;
20961 -- Exit from record scope
20963 End_Scope;
20965 -- Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
20966 -- the implemented interfaces and associate them an aliased entity.
20968 if Is_Tagged
20969 and then not Is_Empty_List (Interface_List (Def))
20970 then
20971 Derive_Progenitor_Subprograms (T, T);
20972 end if;
20974 Check_Function_Writable_Actuals (N);
20975 end Record_Type_Declaration;
20977 ----------------------------
20978 -- Record_Type_Definition --
20979 ----------------------------
20981 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
20982 Component : Entity_Id;
20983 Ctrl_Components : Boolean := False;
20984 Final_Storage_Only : Boolean;
20985 T : Entity_Id;
20987 begin
20988 if Ekind (Prev_T) = E_Incomplete_Type then
20989 T := Full_View (Prev_T);
20990 else
20991 T := Prev_T;
20992 end if;
20994 -- In SPARK, tagged types and type extensions may only be declared in
20995 -- the specification of library unit packages.
20997 if Present (Def) and then Is_Tagged_Type (T) then
20998 declare
20999 Typ : Node_Id;
21000 Ctxt : Node_Id;
21002 begin
21003 if Nkind (Parent (Def)) = N_Full_Type_Declaration then
21004 Typ := Parent (Def);
21005 else
21006 pragma Assert
21007 (Nkind (Parent (Def)) = N_Derived_Type_Definition);
21008 Typ := Parent (Parent (Def));
21009 end if;
21011 Ctxt := Parent (Typ);
21013 if Nkind (Ctxt) = N_Package_Body
21014 and then Nkind (Parent (Ctxt)) = N_Compilation_Unit
21015 then
21016 Check_SPARK_05_Restriction
21017 ("type should be defined in package specification", Typ);
21019 elsif Nkind (Ctxt) /= N_Package_Specification
21020 or else Nkind (Parent (Parent (Ctxt))) /= N_Compilation_Unit
21021 then
21022 Check_SPARK_05_Restriction
21023 ("type should be defined in library unit package", Typ);
21024 end if;
21025 end;
21026 end if;
21028 Final_Storage_Only := not Is_Controlled (T);
21030 -- Ada 2005: Check whether an explicit Limited is present in a derived
21031 -- type declaration.
21033 if Nkind (Parent (Def)) = N_Derived_Type_Definition
21034 and then Limited_Present (Parent (Def))
21035 then
21036 Set_Is_Limited_Record (T);
21037 end if;
21039 -- If the component list of a record type is defined by the reserved
21040 -- word null and there is no discriminant part, then the record type has
21041 -- no components and all records of the type are null records (RM 3.7)
21042 -- This procedure is also called to process the extension part of a
21043 -- record extension, in which case the current scope may have inherited
21044 -- components.
21046 if No (Def)
21047 or else No (Component_List (Def))
21048 or else Null_Present (Component_List (Def))
21049 then
21050 if not Is_Tagged_Type (T) then
21051 Check_SPARK_05_Restriction ("untagged record cannot be null", Def);
21052 end if;
21054 else
21055 Analyze_Declarations (Component_Items (Component_List (Def)));
21057 if Present (Variant_Part (Component_List (Def))) then
21058 Check_SPARK_05_Restriction ("variant part is not allowed", Def);
21059 Analyze (Variant_Part (Component_List (Def)));
21060 end if;
21061 end if;
21063 -- After completing the semantic analysis of the record definition,
21064 -- record components, both new and inherited, are accessible. Set their
21065 -- kind accordingly. Exclude malformed itypes from illegal declarations,
21066 -- whose Ekind may be void.
21068 Component := First_Entity (Current_Scope);
21069 while Present (Component) loop
21070 if Ekind (Component) = E_Void
21071 and then not Is_Itype (Component)
21072 then
21073 Set_Ekind (Component, E_Component);
21074 Init_Component_Location (Component);
21075 end if;
21077 if Has_Task (Etype (Component)) then
21078 Set_Has_Task (T);
21079 end if;
21081 if Has_Protected (Etype (Component)) then
21082 Set_Has_Protected (T);
21083 end if;
21085 if Ekind (Component) /= E_Component then
21086 null;
21088 -- Do not set Has_Controlled_Component on a class-wide equivalent
21089 -- type. See Make_CW_Equivalent_Type.
21091 elsif not Is_Class_Wide_Equivalent_Type (T)
21092 and then (Has_Controlled_Component (Etype (Component))
21093 or else (Chars (Component) /= Name_uParent
21094 and then Is_Controlled (Etype (Component))))
21095 then
21096 Set_Has_Controlled_Component (T, True);
21097 Final_Storage_Only :=
21098 Final_Storage_Only
21099 and then Finalize_Storage_Only (Etype (Component));
21100 Ctrl_Components := True;
21101 end if;
21103 Next_Entity (Component);
21104 end loop;
21106 -- A Type is Finalize_Storage_Only only if all its controlled components
21107 -- are also.
21109 if Ctrl_Components then
21110 Set_Finalize_Storage_Only (T, Final_Storage_Only);
21111 end if;
21113 -- Place reference to end record on the proper entity, which may
21114 -- be a partial view.
21116 if Present (Def) then
21117 Process_End_Label (Def, 'e', Prev_T);
21118 end if;
21119 end Record_Type_Definition;
21121 ------------------------
21122 -- Replace_Components --
21123 ------------------------
21125 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id) is
21126 function Process (N : Node_Id) return Traverse_Result;
21128 -------------
21129 -- Process --
21130 -------------
21132 function Process (N : Node_Id) return Traverse_Result is
21133 Comp : Entity_Id;
21135 begin
21136 if Nkind (N) = N_Discriminant_Specification then
21137 Comp := First_Discriminant (Typ);
21138 while Present (Comp) loop
21139 if Chars (Comp) = Chars (Defining_Identifier (N)) then
21140 Set_Defining_Identifier (N, Comp);
21141 exit;
21142 end if;
21144 Next_Discriminant (Comp);
21145 end loop;
21147 elsif Nkind (N) = N_Component_Declaration then
21148 Comp := First_Component (Typ);
21149 while Present (Comp) loop
21150 if Chars (Comp) = Chars (Defining_Identifier (N)) then
21151 Set_Defining_Identifier (N, Comp);
21152 exit;
21153 end if;
21155 Next_Component (Comp);
21156 end loop;
21157 end if;
21159 return OK;
21160 end Process;
21162 procedure Replace is new Traverse_Proc (Process);
21164 -- Start of processing for Replace_Components
21166 begin
21167 Replace (Decl);
21168 end Replace_Components;
21170 -------------------------------
21171 -- Set_Completion_Referenced --
21172 -------------------------------
21174 procedure Set_Completion_Referenced (E : Entity_Id) is
21175 begin
21176 -- If in main unit, mark entity that is a completion as referenced,
21177 -- warnings go on the partial view when needed.
21179 if In_Extended_Main_Source_Unit (E) then
21180 Set_Referenced (E);
21181 end if;
21182 end Set_Completion_Referenced;
21184 ---------------------
21185 -- Set_Default_SSO --
21186 ---------------------
21188 procedure Set_Default_SSO (T : Entity_Id) is
21189 begin
21190 case Opt.Default_SSO is
21191 when ' ' =>
21192 null;
21193 when 'L' =>
21194 Set_SSO_Set_Low_By_Default (T, True);
21195 when 'H' =>
21196 Set_SSO_Set_High_By_Default (T, True);
21197 when others =>
21198 raise Program_Error;
21199 end case;
21200 end Set_Default_SSO;
21202 ---------------------
21203 -- Set_Fixed_Range --
21204 ---------------------
21206 -- The range for fixed-point types is complicated by the fact that we
21207 -- do not know the exact end points at the time of the declaration. This
21208 -- is true for three reasons:
21210 -- A size clause may affect the fudging of the end-points.
21211 -- A small clause may affect the values of the end-points.
21212 -- We try to include the end-points if it does not affect the size.
21214 -- This means that the actual end-points must be established at the
21215 -- point when the type is frozen. Meanwhile, we first narrow the range
21216 -- as permitted (so that it will fit if necessary in a small specified
21217 -- size), and then build a range subtree with these narrowed bounds.
21218 -- Set_Fixed_Range constructs the range from real literal values, and
21219 -- sets the range as the Scalar_Range of the given fixed-point type entity.
21221 -- The parent of this range is set to point to the entity so that it is
21222 -- properly hooked into the tree (unlike normal Scalar_Range entries for
21223 -- other scalar types, which are just pointers to the range in the
21224 -- original tree, this would otherwise be an orphan).
21226 -- The tree is left unanalyzed. When the type is frozen, the processing
21227 -- in Freeze.Freeze_Fixed_Point_Type notices that the range is not
21228 -- analyzed, and uses this as an indication that it should complete
21229 -- work on the range (it will know the final small and size values).
21231 procedure Set_Fixed_Range
21232 (E : Entity_Id;
21233 Loc : Source_Ptr;
21234 Lo : Ureal;
21235 Hi : Ureal)
21237 S : constant Node_Id :=
21238 Make_Range (Loc,
21239 Low_Bound => Make_Real_Literal (Loc, Lo),
21240 High_Bound => Make_Real_Literal (Loc, Hi));
21241 begin
21242 Set_Scalar_Range (E, S);
21243 Set_Parent (S, E);
21245 -- Before the freeze point, the bounds of a fixed point are universal
21246 -- and carry the corresponding type.
21248 Set_Etype (Low_Bound (S), Universal_Real);
21249 Set_Etype (High_Bound (S), Universal_Real);
21250 end Set_Fixed_Range;
21252 ----------------------------------
21253 -- Set_Scalar_Range_For_Subtype --
21254 ----------------------------------
21256 procedure Set_Scalar_Range_For_Subtype
21257 (Def_Id : Entity_Id;
21258 R : Node_Id;
21259 Subt : Entity_Id)
21261 Kind : constant Entity_Kind := Ekind (Def_Id);
21263 begin
21264 -- Defend against previous error
21266 if Nkind (R) = N_Error then
21267 return;
21268 end if;
21270 Set_Scalar_Range (Def_Id, R);
21272 -- We need to link the range into the tree before resolving it so
21273 -- that types that are referenced, including importantly the subtype
21274 -- itself, are properly frozen (Freeze_Expression requires that the
21275 -- expression be properly linked into the tree). Of course if it is
21276 -- already linked in, then we do not disturb the current link.
21278 if No (Parent (R)) then
21279 Set_Parent (R, Def_Id);
21280 end if;
21282 -- Reset the kind of the subtype during analysis of the range, to
21283 -- catch possible premature use in the bounds themselves.
21285 Set_Ekind (Def_Id, E_Void);
21286 Process_Range_Expr_In_Decl (R, Subt, Subtyp => Def_Id);
21287 Set_Ekind (Def_Id, Kind);
21288 end Set_Scalar_Range_For_Subtype;
21290 --------------------------------------------------------
21291 -- Set_Stored_Constraint_From_Discriminant_Constraint --
21292 --------------------------------------------------------
21294 procedure Set_Stored_Constraint_From_Discriminant_Constraint
21295 (E : Entity_Id)
21297 begin
21298 -- Make sure set if encountered during Expand_To_Stored_Constraint
21300 Set_Stored_Constraint (E, No_Elist);
21302 -- Give it the right value
21304 if Is_Constrained (E) and then Has_Discriminants (E) then
21305 Set_Stored_Constraint (E,
21306 Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
21307 end if;
21308 end Set_Stored_Constraint_From_Discriminant_Constraint;
21310 -------------------------------------
21311 -- Signed_Integer_Type_Declaration --
21312 -------------------------------------
21314 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
21315 Implicit_Base : Entity_Id;
21316 Base_Typ : Entity_Id;
21317 Lo_Val : Uint;
21318 Hi_Val : Uint;
21319 Errs : Boolean := False;
21320 Lo : Node_Id;
21321 Hi : Node_Id;
21323 function Can_Derive_From (E : Entity_Id) return Boolean;
21324 -- Determine whether given bounds allow derivation from specified type
21326 procedure Check_Bound (Expr : Node_Id);
21327 -- Check bound to make sure it is integral and static. If not, post
21328 -- appropriate error message and set Errs flag
21330 ---------------------
21331 -- Can_Derive_From --
21332 ---------------------
21334 -- Note we check both bounds against both end values, to deal with
21335 -- strange types like ones with a range of 0 .. -12341234.
21337 function Can_Derive_From (E : Entity_Id) return Boolean is
21338 Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
21339 Hi : constant Uint := Expr_Value (Type_High_Bound (E));
21340 begin
21341 return Lo <= Lo_Val and then Lo_Val <= Hi
21342 and then
21343 Lo <= Hi_Val and then Hi_Val <= Hi;
21344 end Can_Derive_From;
21346 -----------------
21347 -- Check_Bound --
21348 -----------------
21350 procedure Check_Bound (Expr : Node_Id) is
21351 begin
21352 -- If a range constraint is used as an integer type definition, each
21353 -- bound of the range must be defined by a static expression of some
21354 -- integer type, but the two bounds need not have the same integer
21355 -- type (Negative bounds are allowed.) (RM 3.5.4)
21357 if not Is_Integer_Type (Etype (Expr)) then
21358 Error_Msg_N
21359 ("integer type definition bounds must be of integer type", Expr);
21360 Errs := True;
21362 elsif not Is_OK_Static_Expression (Expr) then
21363 Flag_Non_Static_Expr
21364 ("non-static expression used for integer type bound!", Expr);
21365 Errs := True;
21367 -- The bounds are folded into literals, and we set their type to be
21368 -- universal, to avoid typing difficulties: we cannot set the type
21369 -- of the literal to the new type, because this would be a forward
21370 -- reference for the back end, and if the original type is user-
21371 -- defined this can lead to spurious semantic errors (e.g. 2928-003).
21373 else
21374 if Is_Entity_Name (Expr) then
21375 Fold_Uint (Expr, Expr_Value (Expr), True);
21376 end if;
21378 Set_Etype (Expr, Universal_Integer);
21379 end if;
21380 end Check_Bound;
21382 -- Start of processing for Signed_Integer_Type_Declaration
21384 begin
21385 -- Create an anonymous base type
21387 Implicit_Base :=
21388 Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
21390 -- Analyze and check the bounds, they can be of any integer type
21392 Lo := Low_Bound (Def);
21393 Hi := High_Bound (Def);
21395 -- Arbitrarily use Integer as the type if either bound had an error
21397 if Hi = Error or else Lo = Error then
21398 Base_Typ := Any_Integer;
21399 Set_Error_Posted (T, True);
21401 -- Here both bounds are OK expressions
21403 else
21404 Analyze_And_Resolve (Lo, Any_Integer);
21405 Analyze_And_Resolve (Hi, Any_Integer);
21407 Check_Bound (Lo);
21408 Check_Bound (Hi);
21410 if Errs then
21411 Hi := Type_High_Bound (Standard_Long_Long_Integer);
21412 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
21413 end if;
21415 -- Find type to derive from
21417 Lo_Val := Expr_Value (Lo);
21418 Hi_Val := Expr_Value (Hi);
21420 if Can_Derive_From (Standard_Short_Short_Integer) then
21421 Base_Typ := Base_Type (Standard_Short_Short_Integer);
21423 elsif Can_Derive_From (Standard_Short_Integer) then
21424 Base_Typ := Base_Type (Standard_Short_Integer);
21426 elsif Can_Derive_From (Standard_Integer) then
21427 Base_Typ := Base_Type (Standard_Integer);
21429 elsif Can_Derive_From (Standard_Long_Integer) then
21430 Base_Typ := Base_Type (Standard_Long_Integer);
21432 elsif Can_Derive_From (Standard_Long_Long_Integer) then
21433 Check_Restriction (No_Long_Long_Integers, Def);
21434 Base_Typ := Base_Type (Standard_Long_Long_Integer);
21436 else
21437 Base_Typ := Base_Type (Standard_Long_Long_Integer);
21438 Error_Msg_N ("integer type definition bounds out of range", Def);
21439 Hi := Type_High_Bound (Standard_Long_Long_Integer);
21440 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
21441 end if;
21442 end if;
21444 -- Complete both implicit base and declared first subtype entities. The
21445 -- inheritance of the rep item chain ensures that SPARK-related pragmas
21446 -- are not clobbered when the signed integer type acts as a full view of
21447 -- a private type.
21449 Set_Etype (Implicit_Base, Base_Typ);
21450 Set_Size_Info (Implicit_Base, Base_Typ);
21451 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
21452 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
21453 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
21455 Set_Ekind (T, E_Signed_Integer_Subtype);
21456 Set_Etype (T, Implicit_Base);
21457 Set_Size_Info (T, Implicit_Base);
21458 Inherit_Rep_Item_Chain (T, Implicit_Base);
21459 Set_Scalar_Range (T, Def);
21460 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
21461 Set_Is_Constrained (T);
21462 end Signed_Integer_Type_Declaration;
21464 end Sem_Ch3;