PR target/82524
[official-gcc.git] / gcc / ada / sem_ch3.adb
blobc163aab8e7863c429c918c1ed2392545e751cbcd
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ C H 3 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2017, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Contracts; use Contracts;
30 with Debug; use Debug;
31 with Elists; use Elists;
32 with Einfo; use Einfo;
33 with Errout; use Errout;
34 with Eval_Fat; use Eval_Fat;
35 with Exp_Ch3; use Exp_Ch3;
36 with Exp_Ch9; use Exp_Ch9;
37 with Exp_Disp; use Exp_Disp;
38 with Exp_Dist; use Exp_Dist;
39 with Exp_Tss; use Exp_Tss;
40 with Exp_Util; use Exp_Util;
41 with Freeze; use Freeze;
42 with Ghost; use Ghost;
43 with Itypes; use Itypes;
44 with Layout; use Layout;
45 with Lib; use Lib;
46 with Lib.Xref; use Lib.Xref;
47 with Namet; use Namet;
48 with Nmake; use Nmake;
49 with Opt; use Opt;
50 with Restrict; use Restrict;
51 with Rident; use Rident;
52 with Rtsfind; use Rtsfind;
53 with Sem; use Sem;
54 with Sem_Aux; use Sem_Aux;
55 with Sem_Case; use Sem_Case;
56 with Sem_Cat; use Sem_Cat;
57 with Sem_Ch6; use Sem_Ch6;
58 with Sem_Ch7; use Sem_Ch7;
59 with Sem_Ch8; use Sem_Ch8;
60 with Sem_Ch13; use Sem_Ch13;
61 with Sem_Dim; use Sem_Dim;
62 with Sem_Disp; use Sem_Disp;
63 with Sem_Dist; use Sem_Dist;
64 with Sem_Elim; use Sem_Elim;
65 with Sem_Eval; use Sem_Eval;
66 with Sem_Mech; use Sem_Mech;
67 with Sem_Res; use Sem_Res;
68 with Sem_Smem; use Sem_Smem;
69 with Sem_Type; use Sem_Type;
70 with Sem_Util; use Sem_Util;
71 with Sem_Warn; use Sem_Warn;
72 with Stand; use Stand;
73 with Sinfo; use Sinfo;
74 with Sinput; use Sinput;
75 with Snames; use Snames;
76 with Targparm; use Targparm;
77 with Tbuild; use Tbuild;
78 with Ttypes; use Ttypes;
79 with Uintp; use Uintp;
80 with Urealp; use Urealp;
82 package body Sem_Ch3 is
84 -----------------------
85 -- Local Subprograms --
86 -----------------------
88 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id);
89 -- Ada 2005 (AI-251): Add the tag components corresponding to all the
90 -- abstract interface types implemented by a record type or a derived
91 -- record type.
93 procedure Build_Derived_Type
94 (N : Node_Id;
95 Parent_Type : Entity_Id;
96 Derived_Type : Entity_Id;
97 Is_Completion : Boolean;
98 Derive_Subps : Boolean := True);
99 -- Create and decorate a Derived_Type given the Parent_Type entity. N is
100 -- the N_Full_Type_Declaration node containing the derived type definition.
101 -- Parent_Type is the entity for the parent type in the derived type
102 -- definition and Derived_Type the actual derived type. Is_Completion must
103 -- be set to False if Derived_Type is the N_Defining_Identifier node in N
104 -- (i.e. Derived_Type = Defining_Identifier (N)). In this case N is not the
105 -- completion of a private type declaration. If Is_Completion is set to
106 -- True, N is the completion of a private type declaration and Derived_Type
107 -- is different from the defining identifier inside N (i.e. Derived_Type /=
108 -- Defining_Identifier (N)). Derive_Subps indicates whether the parent
109 -- subprograms should be derived. The only case where this parameter is
110 -- False is when Build_Derived_Type is recursively called to process an
111 -- implicit derived full type for a type derived from a private type (in
112 -- that case the subprograms must only be derived for the private view of
113 -- the type).
115 -- ??? These flags need a bit of re-examination and re-documentation:
116 -- ??? are they both necessary (both seem related to the recursion)?
118 procedure Build_Derived_Access_Type
119 (N : Node_Id;
120 Parent_Type : Entity_Id;
121 Derived_Type : Entity_Id);
122 -- Subsidiary procedure to Build_Derived_Type. For a derived access type,
123 -- create an implicit base if the parent type is constrained or if the
124 -- subtype indication has a constraint.
126 procedure Build_Derived_Array_Type
127 (N : Node_Id;
128 Parent_Type : Entity_Id;
129 Derived_Type : Entity_Id);
130 -- Subsidiary procedure to Build_Derived_Type. For a derived array type,
131 -- create an implicit base if the parent type is constrained or if the
132 -- subtype indication has a constraint.
134 procedure Build_Derived_Concurrent_Type
135 (N : Node_Id;
136 Parent_Type : Entity_Id;
137 Derived_Type : Entity_Id);
138 -- Subsidiary procedure to Build_Derived_Type. For a derived task or
139 -- protected type, inherit entries and protected subprograms, check
140 -- legality of discriminant constraints if any.
142 procedure Build_Derived_Enumeration_Type
143 (N : Node_Id;
144 Parent_Type : Entity_Id;
145 Derived_Type : Entity_Id);
146 -- Subsidiary procedure to Build_Derived_Type. For a derived enumeration
147 -- type, we must create a new list of literals. Types derived from
148 -- Character and [Wide_]Wide_Character are special-cased.
150 procedure Build_Derived_Numeric_Type
151 (N : Node_Id;
152 Parent_Type : Entity_Id;
153 Derived_Type : Entity_Id);
154 -- Subsidiary procedure to Build_Derived_Type. For numeric types, create
155 -- an anonymous base type, and propagate constraint to subtype if needed.
157 procedure Build_Derived_Private_Type
158 (N : Node_Id;
159 Parent_Type : Entity_Id;
160 Derived_Type : Entity_Id;
161 Is_Completion : Boolean;
162 Derive_Subps : Boolean := True);
163 -- Subsidiary procedure to Build_Derived_Type. This procedure is complex
164 -- because the parent may or may not have a completion, and the derivation
165 -- may itself be a completion.
167 procedure Build_Derived_Record_Type
168 (N : Node_Id;
169 Parent_Type : Entity_Id;
170 Derived_Type : Entity_Id;
171 Derive_Subps : Boolean := True);
172 -- Subsidiary procedure used for tagged and untagged record types
173 -- by Build_Derived_Type and Analyze_Private_Extension_Declaration.
174 -- All parameters are as in Build_Derived_Type except that N, in
175 -- addition to being an N_Full_Type_Declaration node, can also be an
176 -- N_Private_Extension_Declaration node. See the definition of this routine
177 -- for much more info. Derive_Subps indicates whether subprograms should be
178 -- derived from the parent type. The only case where Derive_Subps is False
179 -- is for an implicit derived full type for a type derived from a private
180 -- type (see Build_Derived_Type).
182 procedure Build_Discriminal (Discrim : Entity_Id);
183 -- Create the discriminal corresponding to discriminant Discrim, that is
184 -- the parameter corresponding to Discrim to be used in initialization
185 -- procedures for the type where Discrim is a discriminant. Discriminals
186 -- are not used during semantic analysis, and are not fully defined
187 -- entities until expansion. Thus they are not given a scope until
188 -- initialization procedures are built.
190 function Build_Discriminant_Constraints
191 (T : Entity_Id;
192 Def : Node_Id;
193 Derived_Def : Boolean := False) return Elist_Id;
194 -- Validate discriminant constraints and return the list of the constraints
195 -- in order of discriminant declarations, where T is the discriminated
196 -- unconstrained type. Def is the N_Subtype_Indication node where the
197 -- discriminants constraints for T are specified. Derived_Def is True
198 -- when building the discriminant constraints in a derived type definition
199 -- of the form "type D (...) is new T (xxx)". In this case T is the parent
200 -- type and Def is the constraint "(xxx)" on T and this routine sets the
201 -- Corresponding_Discriminant field of the discriminants in the derived
202 -- type D to point to the corresponding discriminants in the parent type T.
204 procedure Build_Discriminated_Subtype
205 (T : Entity_Id;
206 Def_Id : Entity_Id;
207 Elist : Elist_Id;
208 Related_Nod : Node_Id;
209 For_Access : Boolean := False);
210 -- Subsidiary procedure to Constrain_Discriminated_Type and to
211 -- Process_Incomplete_Dependents. Given
213 -- T (a possibly discriminated base type)
214 -- Def_Id (a very partially built subtype for T),
216 -- the call completes Def_Id to be the appropriate E_*_Subtype.
218 -- The Elist is the list of discriminant constraints if any (it is set
219 -- to No_Elist if T is not a discriminated type, and to an empty list if
220 -- T has discriminants but there are no discriminant constraints). The
221 -- Related_Nod is the same as Decl_Node in Create_Constrained_Components.
222 -- The For_Access says whether or not this subtype is really constraining
223 -- an access type. That is its sole purpose is the designated type of an
224 -- access type -- in which case a Private_Subtype Is_For_Access_Subtype
225 -- is built to avoid freezing T when the access subtype is frozen.
227 function Build_Scalar_Bound
228 (Bound : Node_Id;
229 Par_T : Entity_Id;
230 Der_T : Entity_Id) return Node_Id;
231 -- The bounds of a derived scalar type are conversions of the bounds of
232 -- the parent type. Optimize the representation if the bounds are literals.
233 -- Needs a more complete spec--what are the parameters exactly, and what
234 -- exactly is the returned value, and how is Bound affected???
236 procedure Build_Underlying_Full_View
237 (N : Node_Id;
238 Typ : Entity_Id;
239 Par : Entity_Id);
240 -- If the completion of a private type is itself derived from a private
241 -- type, or if the full view of a private subtype is itself private, the
242 -- back-end has no way to compute the actual size of this type. We build
243 -- an internal subtype declaration of the proper parent type to convey
244 -- this information. This extra mechanism is needed because a full
245 -- view cannot itself have a full view (it would get clobbered during
246 -- view exchanges).
248 procedure Check_Access_Discriminant_Requires_Limited
249 (D : Node_Id;
250 Loc : Node_Id);
251 -- Check the restriction that the type to which an access discriminant
252 -- belongs must be a concurrent type or a descendant of a type with
253 -- the reserved word 'limited' in its declaration.
255 procedure Check_Anonymous_Access_Components
256 (Typ_Decl : Node_Id;
257 Typ : Entity_Id;
258 Prev : Entity_Id;
259 Comp_List : Node_Id);
260 -- Ada 2005 AI-382: an access component in a record definition can refer to
261 -- the enclosing record, in which case it denotes the type itself, and not
262 -- the current instance of the type. We create an anonymous access type for
263 -- the component, and flag it as an access to a component, so accessibility
264 -- checks are properly performed on it. The declaration of the access type
265 -- is placed ahead of that of the record to prevent order-of-elaboration
266 -- circularity issues in Gigi. We create an incomplete type for the record
267 -- declaration, which is the designated type of the anonymous access.
269 procedure Check_Delta_Expression (E : Node_Id);
270 -- Check that the expression represented by E is suitable for use as a
271 -- delta expression, i.e. it is of real type and is static.
273 procedure Check_Digits_Expression (E : Node_Id);
274 -- Check that the expression represented by E is suitable for use as a
275 -- digits expression, i.e. it is of integer type, positive and static.
277 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id);
278 -- Validate the initialization of an object declaration. T is the required
279 -- type, and Exp is the initialization expression.
281 procedure Check_Interfaces (N : Node_Id; Def : Node_Id);
282 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
284 procedure Check_Or_Process_Discriminants
285 (N : Node_Id;
286 T : Entity_Id;
287 Prev : Entity_Id := Empty);
288 -- If N is the full declaration of the completion T of an incomplete or
289 -- private type, check its discriminants (which are already known to be
290 -- conformant with those of the partial view, see Find_Type_Name),
291 -- otherwise process them. Prev is the entity of the partial declaration,
292 -- if any.
294 procedure Check_Real_Bound (Bound : Node_Id);
295 -- Check given bound for being of real type and static. If not, post an
296 -- appropriate message, and rewrite the bound with the real literal zero.
298 procedure Constant_Redeclaration
299 (Id : Entity_Id;
300 N : Node_Id;
301 T : out Entity_Id);
302 -- Various checks on legality of full declaration of deferred constant.
303 -- Id is the entity for the redeclaration, N is the N_Object_Declaration,
304 -- node. The caller has not yet set any attributes of this entity.
306 function Contain_Interface
307 (Iface : Entity_Id;
308 Ifaces : Elist_Id) return Boolean;
309 -- Ada 2005: Determine whether Iface is present in the list Ifaces
311 procedure Convert_Scalar_Bounds
312 (N : Node_Id;
313 Parent_Type : Entity_Id;
314 Derived_Type : Entity_Id;
315 Loc : Source_Ptr);
316 -- For derived scalar types, convert the bounds in the type definition to
317 -- the derived type, and complete their analysis. Given a constraint of the
318 -- form ".. new T range Lo .. Hi", Lo and Hi are analyzed and resolved with
319 -- T'Base, the parent_type. The bounds of the derived type (the anonymous
320 -- base) are copies of Lo and Hi. Finally, the bounds of the derived
321 -- subtype are conversions of those bounds to the derived_type, so that
322 -- their typing is consistent.
324 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id);
325 -- Copies attributes from array base type T2 to array base type T1. Copies
326 -- only attributes that apply to base types, but not subtypes.
328 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id);
329 -- Copies attributes from array subtype T2 to array subtype T1. Copies
330 -- attributes that apply to both subtypes and base types.
332 procedure Create_Constrained_Components
333 (Subt : Entity_Id;
334 Decl_Node : Node_Id;
335 Typ : Entity_Id;
336 Constraints : Elist_Id);
337 -- Build the list of entities for a constrained discriminated record
338 -- subtype. If a component depends on a discriminant, replace its subtype
339 -- using the discriminant values in the discriminant constraint. Subt
340 -- is the defining identifier for the subtype whose list of constrained
341 -- entities we will create. Decl_Node is the type declaration node where
342 -- we will attach all the itypes created. Typ is the base discriminated
343 -- type for the subtype Subt. Constraints is the list of discriminant
344 -- constraints for Typ.
346 function Constrain_Component_Type
347 (Comp : Entity_Id;
348 Constrained_Typ : Entity_Id;
349 Related_Node : Node_Id;
350 Typ : Entity_Id;
351 Constraints : Elist_Id) return Entity_Id;
352 -- Given a discriminated base type Typ, a list of discriminant constraints,
353 -- Constraints, for Typ and a component Comp of Typ, create and return the
354 -- type corresponding to Etype (Comp) where all discriminant references
355 -- are replaced with the corresponding constraint. If Etype (Comp) contains
356 -- no discriminant references then it is returned as-is. Constrained_Typ
357 -- is the final constrained subtype to which the constrained component
358 -- belongs. Related_Node is the node where we attach all created itypes.
360 procedure Constrain_Access
361 (Def_Id : in out Entity_Id;
362 S : Node_Id;
363 Related_Nod : Node_Id);
364 -- Apply a list of constraints to an access type. If Def_Id is empty, it is
365 -- an anonymous type created for a subtype indication. In that case it is
366 -- created in the procedure and attached to Related_Nod.
368 procedure Constrain_Array
369 (Def_Id : in out Entity_Id;
370 SI : Node_Id;
371 Related_Nod : Node_Id;
372 Related_Id : Entity_Id;
373 Suffix : Character);
374 -- Apply a list of index constraints to an unconstrained array type. The
375 -- first parameter is the entity for the resulting subtype. A value of
376 -- Empty for Def_Id indicates that an implicit type must be created, but
377 -- creation is delayed (and must be done by this procedure) because other
378 -- subsidiary implicit types must be created first (which is why Def_Id
379 -- is an in/out parameter). The second parameter is a subtype indication
380 -- node for the constrained array to be created (e.g. something of the
381 -- form string (1 .. 10)). Related_Nod gives the place where this type
382 -- has to be inserted in the tree. The Related_Id and Suffix parameters
383 -- are used to build the associated Implicit type name.
385 procedure Constrain_Concurrent
386 (Def_Id : in out Entity_Id;
387 SI : Node_Id;
388 Related_Nod : Node_Id;
389 Related_Id : Entity_Id;
390 Suffix : Character);
391 -- Apply list of discriminant constraints to an unconstrained concurrent
392 -- type.
394 -- SI is the N_Subtype_Indication node containing the constraint and
395 -- the unconstrained type to constrain.
397 -- Def_Id is the entity for the resulting constrained subtype. A value
398 -- of Empty for Def_Id indicates that an implicit type must be created,
399 -- but creation is delayed (and must be done by this procedure) because
400 -- other subsidiary implicit types must be created first (which is why
401 -- Def_Id is an in/out parameter).
403 -- Related_Nod gives the place where this type has to be inserted
404 -- in the tree.
406 -- The last two arguments are used to create its external name if needed.
408 function Constrain_Corresponding_Record
409 (Prot_Subt : Entity_Id;
410 Corr_Rec : Entity_Id;
411 Related_Nod : Node_Id) return Entity_Id;
412 -- When constraining a protected type or task type with discriminants,
413 -- constrain the corresponding record with the same discriminant values.
415 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id);
416 -- Constrain a decimal fixed point type with a digits constraint and/or a
417 -- range constraint, and build E_Decimal_Fixed_Point_Subtype entity.
419 procedure Constrain_Discriminated_Type
420 (Def_Id : Entity_Id;
421 S : Node_Id;
422 Related_Nod : Node_Id;
423 For_Access : Boolean := False);
424 -- Process discriminant constraints of composite type. Verify that values
425 -- have been provided for all discriminants, that the original type is
426 -- unconstrained, and that the types of the supplied expressions match
427 -- the discriminant types. The first three parameters are like in routine
428 -- Constrain_Concurrent. See Build_Discriminated_Subtype for an explanation
429 -- of For_Access.
431 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id);
432 -- Constrain an enumeration type with a range constraint. This is identical
433 -- to Constrain_Integer, but for the Ekind of the resulting subtype.
435 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id);
436 -- Constrain a floating point type with either a digits constraint
437 -- and/or a range constraint, building a E_Floating_Point_Subtype.
439 procedure Constrain_Index
440 (Index : Node_Id;
441 S : Node_Id;
442 Related_Nod : Node_Id;
443 Related_Id : Entity_Id;
444 Suffix : Character;
445 Suffix_Index : Nat);
446 -- Process an index constraint S in a constrained array declaration. The
447 -- constraint can be a subtype name, or a range with or without an explicit
448 -- subtype mark. The index is the corresponding index of the unconstrained
449 -- array. The Related_Id and Suffix parameters are used to build the
450 -- associated Implicit type name.
452 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id);
453 -- Build subtype of a signed or modular integer type
455 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id);
456 -- Constrain an ordinary fixed point type with a range constraint, and
457 -- build an E_Ordinary_Fixed_Point_Subtype entity.
459 procedure Copy_And_Swap (Priv, Full : Entity_Id);
460 -- Copy the Priv entity into the entity of its full declaration then swap
461 -- the two entities in such a manner that the former private type is now
462 -- seen as a full type.
464 procedure Decimal_Fixed_Point_Type_Declaration
465 (T : Entity_Id;
466 Def : Node_Id);
467 -- Create a new decimal fixed point type, and apply the constraint to
468 -- obtain a subtype of this new type.
470 procedure Complete_Private_Subtype
471 (Priv : Entity_Id;
472 Full : Entity_Id;
473 Full_Base : Entity_Id;
474 Related_Nod : Node_Id);
475 -- Complete the implicit full view of a private subtype by setting the
476 -- appropriate semantic fields. If the full view of the parent is a record
477 -- type, build constrained components of subtype.
479 procedure Derive_Progenitor_Subprograms
480 (Parent_Type : Entity_Id;
481 Tagged_Type : Entity_Id);
482 -- Ada 2005 (AI-251): To complete type derivation, collect the primitive
483 -- operations of progenitors of Tagged_Type, and replace the subsidiary
484 -- subtypes with Tagged_Type, to build the specs of the inherited interface
485 -- primitives. The derived primitives are aliased to those of the
486 -- interface. This routine takes care also of transferring to the full view
487 -- subprograms associated with the partial view of Tagged_Type that cover
488 -- interface primitives.
490 procedure Derived_Standard_Character
491 (N : Node_Id;
492 Parent_Type : Entity_Id;
493 Derived_Type : Entity_Id);
494 -- Subsidiary procedure to Build_Derived_Enumeration_Type which handles
495 -- derivations from types Standard.Character and Standard.Wide_Character.
497 procedure Derived_Type_Declaration
498 (T : Entity_Id;
499 N : Node_Id;
500 Is_Completion : Boolean);
501 -- Process a derived type declaration. Build_Derived_Type is invoked
502 -- to process the actual derived type definition. Parameters N and
503 -- Is_Completion have the same meaning as in Build_Derived_Type.
504 -- T is the N_Defining_Identifier for the entity defined in the
505 -- N_Full_Type_Declaration node N, that is T is the derived type.
507 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id);
508 -- Insert each literal in symbol table, as an overloadable identifier. Each
509 -- enumeration type is mapped into a sequence of integers, and each literal
510 -- is defined as a constant with integer value. If any of the literals are
511 -- character literals, the type is a character type, which means that
512 -- strings are legal aggregates for arrays of components of the type.
514 function Expand_To_Stored_Constraint
515 (Typ : Entity_Id;
516 Constraint : Elist_Id) return Elist_Id;
517 -- Given a constraint (i.e. a list of expressions) on the discriminants of
518 -- Typ, expand it into a constraint on the stored discriminants and return
519 -- the new list of expressions constraining the stored discriminants.
521 function Find_Type_Of_Object
522 (Obj_Def : Node_Id;
523 Related_Nod : Node_Id) return Entity_Id;
524 -- Get type entity for object referenced by Obj_Def, attaching the implicit
525 -- types generated to Related_Nod.
527 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id);
528 -- Create a new float and apply the constraint to obtain subtype of it
530 function Has_Range_Constraint (N : Node_Id) return Boolean;
531 -- Given an N_Subtype_Indication node N, return True if a range constraint
532 -- is present, either directly, or as part of a digits or delta constraint.
533 -- In addition, a digits constraint in the decimal case returns True, since
534 -- it establishes a default range if no explicit range is present.
536 function Inherit_Components
537 (N : Node_Id;
538 Parent_Base : Entity_Id;
539 Derived_Base : Entity_Id;
540 Is_Tagged : Boolean;
541 Inherit_Discr : Boolean;
542 Discs : Elist_Id) return Elist_Id;
543 -- Called from Build_Derived_Record_Type to inherit the components of
544 -- Parent_Base (a base type) into the Derived_Base (the derived base type).
545 -- For more information on derived types and component inheritance please
546 -- consult the comment above the body of Build_Derived_Record_Type.
548 -- N is the original derived type declaration
550 -- Is_Tagged is set if we are dealing with tagged types
552 -- If Inherit_Discr is set, Derived_Base inherits its discriminants from
553 -- Parent_Base, otherwise no discriminants are inherited.
555 -- Discs gives the list of constraints that apply to Parent_Base in the
556 -- derived type declaration. If Discs is set to No_Elist, then we have
557 -- the following situation:
559 -- type Parent (D1..Dn : ..) is [tagged] record ...;
560 -- type Derived is new Parent [with ...];
562 -- which gets treated as
564 -- type Derived (D1..Dn : ..) is new Parent (D1,..,Dn) [with ...];
566 -- For untagged types the returned value is an association list. The list
567 -- starts from the association (Parent_Base => Derived_Base), and then it
568 -- contains a sequence of the associations of the form
570 -- (Old_Component => New_Component),
572 -- where Old_Component is the Entity_Id of a component in Parent_Base and
573 -- New_Component is the Entity_Id of the corresponding component in
574 -- Derived_Base. For untagged records, this association list is needed when
575 -- copying the record declaration for the derived base. In the tagged case
576 -- the value returned is irrelevant.
578 procedure Inherit_Predicate_Flags (Subt, Par : Entity_Id);
579 -- Propagate static and dynamic predicate flags from a parent to the
580 -- subtype in a subtype declaration with and without constraints.
582 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean;
583 -- Subsidiary to Check_Abstract_Overriding and Derive_Subprogram.
584 -- Determine whether subprogram Subp is a procedure subject to pragma
585 -- Extensions_Visible with value False and has at least one controlling
586 -- parameter of mode OUT.
588 function Is_Valid_Constraint_Kind
589 (T_Kind : Type_Kind;
590 Constraint_Kind : Node_Kind) return Boolean;
591 -- Returns True if it is legal to apply the given kind of constraint to the
592 -- given kind of type (index constraint to an array type, for example).
594 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id);
595 -- Create new modular type. Verify that modulus is in bounds
597 procedure New_Concatenation_Op (Typ : Entity_Id);
598 -- Create an abbreviated declaration for an operator in order to
599 -- materialize concatenation on array types.
601 procedure Ordinary_Fixed_Point_Type_Declaration
602 (T : Entity_Id;
603 Def : Node_Id);
604 -- Create a new ordinary fixed point type, and apply the constraint to
605 -- obtain subtype of it.
607 procedure Prepare_Private_Subtype_Completion
608 (Id : Entity_Id;
609 Related_Nod : Node_Id);
610 -- Id is a subtype of some private type. Creates the full declaration
611 -- associated with Id whenever possible, i.e. when the full declaration
612 -- of the base type is already known. Records each subtype into
613 -- Private_Dependents of the base type.
615 procedure Process_Incomplete_Dependents
616 (N : Node_Id;
617 Full_T : Entity_Id;
618 Inc_T : Entity_Id);
619 -- Process all entities that depend on an incomplete type. There include
620 -- subtypes, subprogram types that mention the incomplete type in their
621 -- profiles, and subprogram with access parameters that designate the
622 -- incomplete type.
624 -- Inc_T is the defining identifier of an incomplete type declaration, its
625 -- Ekind is E_Incomplete_Type.
627 -- N is the corresponding N_Full_Type_Declaration for Inc_T.
629 -- Full_T is N's defining identifier.
631 -- Subtypes of incomplete types with discriminants are completed when the
632 -- parent type is. This is simpler than private subtypes, because they can
633 -- only appear in the same scope, and there is no need to exchange views.
634 -- Similarly, access_to_subprogram types may have a parameter or a return
635 -- type that is an incomplete type, and that must be replaced with the
636 -- full type.
638 -- If the full type is tagged, subprogram with access parameters that
639 -- designated the incomplete may be primitive operations of the full type,
640 -- and have to be processed accordingly.
642 procedure Process_Real_Range_Specification (Def : Node_Id);
643 -- Given the type definition for a real type, this procedure processes and
644 -- checks the real range specification of this type definition if one is
645 -- present. If errors are found, error messages are posted, and the
646 -- Real_Range_Specification of Def is reset to Empty.
648 procedure Record_Type_Declaration
649 (T : Entity_Id;
650 N : Node_Id;
651 Prev : Entity_Id);
652 -- Process a record type declaration (for both untagged and tagged
653 -- records). Parameters T and N are exactly like in procedure
654 -- Derived_Type_Declaration, except that no flag Is_Completion is needed
655 -- for this routine. If this is the completion of an incomplete type
656 -- declaration, Prev is the entity of the incomplete declaration, used for
657 -- cross-referencing. Otherwise Prev = T.
659 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id);
660 -- This routine is used to process the actual record type definition (both
661 -- for untagged and tagged records). Def is a record type definition node.
662 -- This procedure analyzes the components in this record type definition.
663 -- Prev_T is the entity for the enclosing record type. It is provided so
664 -- that its Has_Task flag can be set if any of the component have Has_Task
665 -- set. If the declaration is the completion of an incomplete type
666 -- declaration, Prev_T is the original incomplete type, whose full view is
667 -- the record type.
669 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id);
670 -- Subsidiary to Build_Derived_Record_Type. For untagged records, we
671 -- build a copy of the declaration tree of the parent, and we create
672 -- independently the list of components for the derived type. Semantic
673 -- information uses the component entities, but record representation
674 -- clauses are validated on the declaration tree. This procedure replaces
675 -- discriminants and components in the declaration with those that have
676 -- been created by Inherit_Components.
678 procedure Set_Fixed_Range
679 (E : Entity_Id;
680 Loc : Source_Ptr;
681 Lo : Ureal;
682 Hi : Ureal);
683 -- Build a range node with the given bounds and set it as the Scalar_Range
684 -- of the given fixed-point type entity. Loc is the source location used
685 -- for the constructed range. See body for further details.
687 procedure Set_Scalar_Range_For_Subtype
688 (Def_Id : Entity_Id;
689 R : Node_Id;
690 Subt : Entity_Id);
691 -- This routine is used to set the scalar range field for a subtype given
692 -- Def_Id, the entity for the subtype, and R, the range expression for the
693 -- scalar range. Subt provides the parent subtype to be used to analyze,
694 -- resolve, and check the given range.
696 procedure Set_Default_SSO (T : Entity_Id);
697 -- T is the entity for an array or record being declared. This procedure
698 -- sets the flags SSO_Set_Low_By_Default/SSO_Set_High_By_Default according
699 -- to the setting of Opt.Default_SSO.
701 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id);
702 -- Create a new signed integer entity, and apply the constraint to obtain
703 -- the required first named subtype of this type.
705 procedure Set_Stored_Constraint_From_Discriminant_Constraint
706 (E : Entity_Id);
707 -- E is some record type. This routine computes E's Stored_Constraint
708 -- from its Discriminant_Constraint.
710 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id);
711 -- Check that an entity in a list of progenitors is an interface,
712 -- emit error otherwise.
714 -----------------------
715 -- Access_Definition --
716 -----------------------
718 function Access_Definition
719 (Related_Nod : Node_Id;
720 N : Node_Id) return Entity_Id
722 Anon_Type : Entity_Id;
723 Anon_Scope : Entity_Id;
724 Desig_Type : Entity_Id;
725 Enclosing_Prot_Type : Entity_Id := Empty;
727 begin
728 Check_SPARK_05_Restriction ("access type is not allowed", N);
730 if Is_Entry (Current_Scope)
731 and then Is_Task_Type (Etype (Scope (Current_Scope)))
732 then
733 Error_Msg_N ("task entries cannot have access parameters", N);
734 return Empty;
735 end if;
737 -- Ada 2005: For an object declaration the corresponding anonymous
738 -- type is declared in the current scope.
740 -- If the access definition is the return type of another access to
741 -- function, scope is the current one, because it is the one of the
742 -- current type declaration, except for the pathological case below.
744 if Nkind_In (Related_Nod, N_Object_Declaration,
745 N_Access_Function_Definition)
746 then
747 Anon_Scope := Current_Scope;
749 -- A pathological case: function returning access functions that
750 -- return access functions, etc. Each anonymous access type created
751 -- is in the enclosing scope of the outermost function.
753 declare
754 Par : Node_Id;
756 begin
757 Par := Related_Nod;
758 while Nkind_In (Par, N_Access_Function_Definition,
759 N_Access_Definition)
760 loop
761 Par := Parent (Par);
762 end loop;
764 if Nkind (Par) = N_Function_Specification then
765 Anon_Scope := Scope (Defining_Entity (Par));
766 end if;
767 end;
769 -- For the anonymous function result case, retrieve the scope of the
770 -- function specification's associated entity rather than using the
771 -- current scope. The current scope will be the function itself if the
772 -- formal part is currently being analyzed, but will be the parent scope
773 -- in the case of a parameterless function, and we always want to use
774 -- the function's parent scope. Finally, if the function is a child
775 -- unit, we must traverse the tree to retrieve the proper entity.
777 elsif Nkind (Related_Nod) = N_Function_Specification
778 and then Nkind (Parent (N)) /= N_Parameter_Specification
779 then
780 -- If the current scope is a protected type, the anonymous access
781 -- is associated with one of the protected operations, and must
782 -- be available in the scope that encloses the protected declaration.
783 -- Otherwise the type is in the scope enclosing the subprogram.
785 -- If the function has formals, The return type of a subprogram
786 -- declaration is analyzed in the scope of the subprogram (see
787 -- Process_Formals) and thus the protected type, if present, is
788 -- the scope of the current function scope.
790 if Ekind (Current_Scope) = E_Protected_Type then
791 Enclosing_Prot_Type := Current_Scope;
793 elsif Ekind (Current_Scope) = E_Function
794 and then Ekind (Scope (Current_Scope)) = E_Protected_Type
795 then
796 Enclosing_Prot_Type := Scope (Current_Scope);
797 end if;
799 if Present (Enclosing_Prot_Type) then
800 Anon_Scope := Scope (Enclosing_Prot_Type);
802 else
803 Anon_Scope := Scope (Defining_Entity (Related_Nod));
804 end if;
806 -- For an access type definition, if the current scope is a child
807 -- unit it is the scope of the type.
809 elsif Is_Compilation_Unit (Current_Scope) then
810 Anon_Scope := Current_Scope;
812 -- For access formals, access components, and access discriminants, the
813 -- scope is that of the enclosing declaration,
815 else
816 Anon_Scope := Scope (Current_Scope);
817 end if;
819 Anon_Type :=
820 Create_Itype
821 (E_Anonymous_Access_Type, Related_Nod, Scope_Id => Anon_Scope);
823 if All_Present (N)
824 and then Ada_Version >= Ada_2005
825 then
826 Error_Msg_N ("ALL is not permitted for anonymous access types", N);
827 end if;
829 -- Ada 2005 (AI-254): In case of anonymous access to subprograms call
830 -- the corresponding semantic routine
832 if Present (Access_To_Subprogram_Definition (N)) then
834 -- Compiler runtime units are compiled in Ada 2005 mode when building
835 -- the runtime library but must also be compilable in Ada 95 mode
836 -- (when bootstrapping the compiler).
838 Check_Compiler_Unit ("anonymous access to subprogram", N);
840 Access_Subprogram_Declaration
841 (T_Name => Anon_Type,
842 T_Def => Access_To_Subprogram_Definition (N));
844 if Ekind (Anon_Type) = E_Access_Protected_Subprogram_Type then
845 Set_Ekind
846 (Anon_Type, E_Anonymous_Access_Protected_Subprogram_Type);
847 else
848 Set_Ekind (Anon_Type, E_Anonymous_Access_Subprogram_Type);
849 end if;
851 Set_Can_Use_Internal_Rep
852 (Anon_Type, not Always_Compatible_Rep_On_Target);
854 -- If the anonymous access is associated with a protected operation,
855 -- create a reference to it after the enclosing protected definition
856 -- because the itype will be used in the subsequent bodies.
858 -- If the anonymous access itself is protected, a full type
859 -- declaratiton will be created for it, so that the equivalent
860 -- record type can be constructed. For further details, see
861 -- Replace_Anonymous_Access_To_Protected-Subprogram.
863 if Ekind (Current_Scope) = E_Protected_Type
864 and then not Protected_Present (Access_To_Subprogram_Definition (N))
865 then
866 Build_Itype_Reference (Anon_Type, Parent (Current_Scope));
867 end if;
869 return Anon_Type;
870 end if;
872 Find_Type (Subtype_Mark (N));
873 Desig_Type := Entity (Subtype_Mark (N));
875 Set_Directly_Designated_Type (Anon_Type, Desig_Type);
876 Set_Etype (Anon_Type, Anon_Type);
878 -- Make sure the anonymous access type has size and alignment fields
879 -- set, as required by gigi. This is necessary in the case of the
880 -- Task_Body_Procedure.
882 if not Has_Private_Component (Desig_Type) then
883 Layout_Type (Anon_Type);
884 end if;
886 -- Ada 2005 (AI-231): Ada 2005 semantics for anonymous access differs
887 -- from Ada 95 semantics. In Ada 2005, anonymous access must specify if
888 -- the null value is allowed. In Ada 95 the null value is never allowed.
890 if Ada_Version >= Ada_2005 then
891 Set_Can_Never_Be_Null (Anon_Type, Null_Exclusion_Present (N));
892 else
893 Set_Can_Never_Be_Null (Anon_Type, True);
894 end if;
896 -- The anonymous access type is as public as the discriminated type or
897 -- subprogram that defines it. It is imported (for back-end purposes)
898 -- if the designated type is.
900 Set_Is_Public (Anon_Type, Is_Public (Scope (Anon_Type)));
902 -- Ada 2005 (AI-231): Propagate the access-constant attribute
904 Set_Is_Access_Constant (Anon_Type, Constant_Present (N));
906 -- The context is either a subprogram declaration, object declaration,
907 -- or an access discriminant, in a private or a full type declaration.
908 -- In the case of a subprogram, if the designated type is incomplete,
909 -- the operation will be a primitive operation of the full type, to be
910 -- updated subsequently. If the type is imported through a limited_with
911 -- clause, the subprogram is not a primitive operation of the type
912 -- (which is declared elsewhere in some other scope).
914 if Ekind (Desig_Type) = E_Incomplete_Type
915 and then not From_Limited_With (Desig_Type)
916 and then Is_Overloadable (Current_Scope)
917 then
918 Append_Elmt (Current_Scope, Private_Dependents (Desig_Type));
919 Set_Has_Delayed_Freeze (Current_Scope);
920 end if;
922 -- Ada 2005: If the designated type is an interface that may contain
923 -- tasks, create a Master entity for the declaration. This must be done
924 -- before expansion of the full declaration, because the declaration may
925 -- include an expression that is an allocator, whose expansion needs the
926 -- proper Master for the created tasks.
928 if Nkind (Related_Nod) = N_Object_Declaration and then Expander_Active
929 then
930 if Is_Interface (Desig_Type) and then Is_Limited_Record (Desig_Type)
931 then
932 Build_Class_Wide_Master (Anon_Type);
934 -- Similarly, if the type is an anonymous access that designates
935 -- tasks, create a master entity for it in the current context.
937 elsif Has_Task (Desig_Type) and then Comes_From_Source (Related_Nod)
938 then
939 Build_Master_Entity (Defining_Identifier (Related_Nod));
940 Build_Master_Renaming (Anon_Type);
941 end if;
942 end if;
944 -- For a private component of a protected type, it is imperative that
945 -- the back-end elaborate the type immediately after the protected
946 -- declaration, because this type will be used in the declarations
947 -- created for the component within each protected body, so we must
948 -- create an itype reference for it now.
950 if Nkind (Parent (Related_Nod)) = N_Protected_Definition then
951 Build_Itype_Reference (Anon_Type, Parent (Parent (Related_Nod)));
953 -- Similarly, if the access definition is the return result of a
954 -- function, create an itype reference for it because it will be used
955 -- within the function body. For a regular function that is not a
956 -- compilation unit, insert reference after the declaration. For a
957 -- protected operation, insert it after the enclosing protected type
958 -- declaration. In either case, do not create a reference for a type
959 -- obtained through a limited_with clause, because this would introduce
960 -- semantic dependencies.
962 -- Similarly, do not create a reference if the designated type is a
963 -- generic formal, because no use of it will reach the backend.
965 elsif Nkind (Related_Nod) = N_Function_Specification
966 and then not From_Limited_With (Desig_Type)
967 and then not Is_Generic_Type (Desig_Type)
968 then
969 if Present (Enclosing_Prot_Type) then
970 Build_Itype_Reference (Anon_Type, Parent (Enclosing_Prot_Type));
972 elsif Is_List_Member (Parent (Related_Nod))
973 and then Nkind (Parent (N)) /= N_Parameter_Specification
974 then
975 Build_Itype_Reference (Anon_Type, Parent (Related_Nod));
976 end if;
978 -- Finally, create an itype reference for an object declaration of an
979 -- anonymous access type. This is strictly necessary only for deferred
980 -- constants, but in any case will avoid out-of-scope problems in the
981 -- back-end.
983 elsif Nkind (Related_Nod) = N_Object_Declaration then
984 Build_Itype_Reference (Anon_Type, Related_Nod);
985 end if;
987 return Anon_Type;
988 end Access_Definition;
990 -----------------------------------
991 -- Access_Subprogram_Declaration --
992 -----------------------------------
994 procedure Access_Subprogram_Declaration
995 (T_Name : Entity_Id;
996 T_Def : Node_Id)
998 procedure Check_For_Premature_Usage (Def : Node_Id);
999 -- Check that type T_Name is not used, directly or recursively, as a
1000 -- parameter or a return type in Def. Def is either a subtype, an
1001 -- access_definition, or an access_to_subprogram_definition.
1003 -------------------------------
1004 -- Check_For_Premature_Usage --
1005 -------------------------------
1007 procedure Check_For_Premature_Usage (Def : Node_Id) is
1008 Param : Node_Id;
1010 begin
1011 -- Check for a subtype mark
1013 if Nkind (Def) in N_Has_Etype then
1014 if Etype (Def) = T_Name then
1015 Error_Msg_N
1016 ("type& cannot be used before end of its declaration", Def);
1017 end if;
1019 -- If this is not a subtype, then this is an access_definition
1021 elsif Nkind (Def) = N_Access_Definition then
1022 if Present (Access_To_Subprogram_Definition (Def)) then
1023 Check_For_Premature_Usage
1024 (Access_To_Subprogram_Definition (Def));
1025 else
1026 Check_For_Premature_Usage (Subtype_Mark (Def));
1027 end if;
1029 -- The only cases left are N_Access_Function_Definition and
1030 -- N_Access_Procedure_Definition.
1032 else
1033 if Present (Parameter_Specifications (Def)) then
1034 Param := First (Parameter_Specifications (Def));
1035 while Present (Param) loop
1036 Check_For_Premature_Usage (Parameter_Type (Param));
1037 Param := Next (Param);
1038 end loop;
1039 end if;
1041 if Nkind (Def) = N_Access_Function_Definition then
1042 Check_For_Premature_Usage (Result_Definition (Def));
1043 end if;
1044 end if;
1045 end Check_For_Premature_Usage;
1047 -- Local variables
1049 Formals : constant List_Id := Parameter_Specifications (T_Def);
1050 Formal : Entity_Id;
1051 D_Ityp : Node_Id;
1052 Desig_Type : constant Entity_Id :=
1053 Create_Itype (E_Subprogram_Type, Parent (T_Def));
1055 -- Start of processing for Access_Subprogram_Declaration
1057 begin
1058 Check_SPARK_05_Restriction ("access type is not allowed", T_Def);
1060 -- Associate the Itype node with the inner full-type declaration or
1061 -- subprogram spec or entry body. This is required to handle nested
1062 -- anonymous declarations. For example:
1064 -- procedure P
1065 -- (X : access procedure
1066 -- (Y : access procedure
1067 -- (Z : access T)))
1069 D_Ityp := Associated_Node_For_Itype (Desig_Type);
1070 while not (Nkind_In (D_Ityp, N_Full_Type_Declaration,
1071 N_Private_Type_Declaration,
1072 N_Private_Extension_Declaration,
1073 N_Procedure_Specification,
1074 N_Function_Specification,
1075 N_Entry_Body)
1077 or else
1078 Nkind_In (D_Ityp, N_Object_Declaration,
1079 N_Object_Renaming_Declaration,
1080 N_Formal_Object_Declaration,
1081 N_Formal_Type_Declaration,
1082 N_Task_Type_Declaration,
1083 N_Protected_Type_Declaration))
1084 loop
1085 D_Ityp := Parent (D_Ityp);
1086 pragma Assert (D_Ityp /= Empty);
1087 end loop;
1089 Set_Associated_Node_For_Itype (Desig_Type, D_Ityp);
1091 if Nkind_In (D_Ityp, N_Procedure_Specification,
1092 N_Function_Specification)
1093 then
1094 Set_Scope (Desig_Type, Scope (Defining_Entity (D_Ityp)));
1096 elsif Nkind_In (D_Ityp, N_Full_Type_Declaration,
1097 N_Object_Declaration,
1098 N_Object_Renaming_Declaration,
1099 N_Formal_Type_Declaration)
1100 then
1101 Set_Scope (Desig_Type, Scope (Defining_Identifier (D_Ityp)));
1102 end if;
1104 if Nkind (T_Def) = N_Access_Function_Definition then
1105 if Nkind (Result_Definition (T_Def)) = N_Access_Definition then
1106 declare
1107 Acc : constant Node_Id := Result_Definition (T_Def);
1109 begin
1110 if Present (Access_To_Subprogram_Definition (Acc))
1111 and then
1112 Protected_Present (Access_To_Subprogram_Definition (Acc))
1113 then
1114 Set_Etype
1115 (Desig_Type,
1116 Replace_Anonymous_Access_To_Protected_Subprogram
1117 (T_Def));
1119 else
1120 Set_Etype
1121 (Desig_Type,
1122 Access_Definition (T_Def, Result_Definition (T_Def)));
1123 end if;
1124 end;
1126 else
1127 Analyze (Result_Definition (T_Def));
1129 declare
1130 Typ : constant Entity_Id := Entity (Result_Definition (T_Def));
1132 begin
1133 -- If a null exclusion is imposed on the result type, then
1134 -- create a null-excluding itype (an access subtype) and use
1135 -- it as the function's Etype.
1137 if Is_Access_Type (Typ)
1138 and then Null_Exclusion_In_Return_Present (T_Def)
1139 then
1140 Set_Etype (Desig_Type,
1141 Create_Null_Excluding_Itype
1142 (T => Typ,
1143 Related_Nod => T_Def,
1144 Scope_Id => Current_Scope));
1146 else
1147 if From_Limited_With (Typ) then
1149 -- AI05-151: Incomplete types are allowed in all basic
1150 -- declarations, including access to subprograms.
1152 if Ada_Version >= Ada_2012 then
1153 null;
1155 else
1156 Error_Msg_NE
1157 ("illegal use of incomplete type&",
1158 Result_Definition (T_Def), Typ);
1159 end if;
1161 elsif Ekind (Current_Scope) = E_Package
1162 and then In_Private_Part (Current_Scope)
1163 then
1164 if Ekind (Typ) = E_Incomplete_Type then
1165 Append_Elmt (Desig_Type, Private_Dependents (Typ));
1167 elsif Is_Class_Wide_Type (Typ)
1168 and then Ekind (Etype (Typ)) = E_Incomplete_Type
1169 then
1170 Append_Elmt
1171 (Desig_Type, Private_Dependents (Etype (Typ)));
1172 end if;
1173 end if;
1175 Set_Etype (Desig_Type, Typ);
1176 end if;
1177 end;
1178 end if;
1180 if not (Is_Type (Etype (Desig_Type))) then
1181 Error_Msg_N
1182 ("expect type in function specification",
1183 Result_Definition (T_Def));
1184 end if;
1186 else
1187 Set_Etype (Desig_Type, Standard_Void_Type);
1188 end if;
1190 if Present (Formals) then
1191 Push_Scope (Desig_Type);
1193 -- Some special tests here. These special tests can be removed
1194 -- if and when Itypes always have proper parent pointers to their
1195 -- declarations???
1197 -- Special test 1) Link defining_identifier of formals. Required by
1198 -- First_Formal to provide its functionality.
1200 declare
1201 F : Node_Id;
1203 begin
1204 F := First (Formals);
1206 -- In ASIS mode, the access_to_subprogram may be analyzed twice,
1207 -- when it is part of an unconstrained type and subtype expansion
1208 -- is disabled. To avoid back-end problems with shared profiles,
1209 -- use previous subprogram type as the designated type, and then
1210 -- remove scope added above.
1212 if ASIS_Mode and then Present (Scope (Defining_Identifier (F)))
1213 then
1214 Set_Etype (T_Name, T_Name);
1215 Init_Size_Align (T_Name);
1216 Set_Directly_Designated_Type (T_Name,
1217 Scope (Defining_Identifier (F)));
1218 End_Scope;
1219 return;
1220 end if;
1222 while Present (F) loop
1223 if No (Parent (Defining_Identifier (F))) then
1224 Set_Parent (Defining_Identifier (F), F);
1225 end if;
1227 Next (F);
1228 end loop;
1229 end;
1231 Process_Formals (Formals, Parent (T_Def));
1233 -- Special test 2) End_Scope requires that the parent pointer be set
1234 -- to something reasonable, but Itypes don't have parent pointers. So
1235 -- we set it and then unset it ???
1237 Set_Parent (Desig_Type, T_Name);
1238 End_Scope;
1239 Set_Parent (Desig_Type, Empty);
1240 end if;
1242 -- Check for premature usage of the type being defined
1244 Check_For_Premature_Usage (T_Def);
1246 -- The return type and/or any parameter type may be incomplete. Mark the
1247 -- subprogram_type as depending on the incomplete type, so that it can
1248 -- be updated when the full type declaration is seen. This only applies
1249 -- to incomplete types declared in some enclosing scope, not to limited
1250 -- views from other packages.
1252 -- Prior to Ada 2012, access to functions can only have in_parameters.
1254 if Present (Formals) then
1255 Formal := First_Formal (Desig_Type);
1256 while Present (Formal) loop
1257 if Ekind (Formal) /= E_In_Parameter
1258 and then Nkind (T_Def) = N_Access_Function_Definition
1259 and then Ada_Version < Ada_2012
1260 then
1261 Error_Msg_N ("functions can only have IN parameters", Formal);
1262 end if;
1264 if Ekind (Etype (Formal)) = E_Incomplete_Type
1265 and then In_Open_Scopes (Scope (Etype (Formal)))
1266 then
1267 Append_Elmt (Desig_Type, Private_Dependents (Etype (Formal)));
1268 Set_Has_Delayed_Freeze (Desig_Type);
1269 end if;
1271 Next_Formal (Formal);
1272 end loop;
1273 end if;
1275 -- Check whether an indirect call without actuals may be possible. This
1276 -- is used when resolving calls whose result is then indexed.
1278 May_Need_Actuals (Desig_Type);
1280 -- If the return type is incomplete, this is legal as long as the type
1281 -- is declared in the current scope and will be completed in it (rather
1282 -- than being part of limited view).
1284 if Ekind (Etype (Desig_Type)) = E_Incomplete_Type
1285 and then not Has_Delayed_Freeze (Desig_Type)
1286 and then In_Open_Scopes (Scope (Etype (Desig_Type)))
1287 then
1288 Append_Elmt (Desig_Type, Private_Dependents (Etype (Desig_Type)));
1289 Set_Has_Delayed_Freeze (Desig_Type);
1290 end if;
1292 Check_Delayed_Subprogram (Desig_Type);
1294 if Protected_Present (T_Def) then
1295 Set_Ekind (T_Name, E_Access_Protected_Subprogram_Type);
1296 Set_Convention (Desig_Type, Convention_Protected);
1297 else
1298 Set_Ekind (T_Name, E_Access_Subprogram_Type);
1299 end if;
1301 Set_Can_Use_Internal_Rep (T_Name, not Always_Compatible_Rep_On_Target);
1303 Set_Etype (T_Name, T_Name);
1304 Init_Size_Align (T_Name);
1305 Set_Directly_Designated_Type (T_Name, Desig_Type);
1307 Generate_Reference_To_Formals (T_Name);
1309 -- Ada 2005 (AI-231): Propagate the null-excluding attribute
1311 Set_Can_Never_Be_Null (T_Name, Null_Exclusion_Present (T_Def));
1313 Check_Restriction (No_Access_Subprograms, T_Def);
1314 end Access_Subprogram_Declaration;
1316 ----------------------------
1317 -- Access_Type_Declaration --
1318 ----------------------------
1320 procedure Access_Type_Declaration (T : Entity_Id; Def : Node_Id) is
1321 P : constant Node_Id := Parent (Def);
1322 S : constant Node_Id := Subtype_Indication (Def);
1324 Full_Desig : Entity_Id;
1326 begin
1327 Check_SPARK_05_Restriction ("access type is not allowed", Def);
1329 -- Check for permissible use of incomplete type
1331 if Nkind (S) /= N_Subtype_Indication then
1332 Analyze (S);
1334 if Present (Entity (S))
1335 and then Ekind (Root_Type (Entity (S))) = E_Incomplete_Type
1336 then
1337 Set_Directly_Designated_Type (T, Entity (S));
1339 -- If the designated type is a limited view, we cannot tell if
1340 -- the full view contains tasks, and there is no way to handle
1341 -- that full view in a client. We create a master entity for the
1342 -- scope, which will be used when a client determines that one
1343 -- is needed.
1345 if From_Limited_With (Entity (S))
1346 and then not Is_Class_Wide_Type (Entity (S))
1347 then
1348 Set_Ekind (T, E_Access_Type);
1349 Build_Master_Entity (T);
1350 Build_Master_Renaming (T);
1351 end if;
1353 else
1354 Set_Directly_Designated_Type (T, Process_Subtype (S, P, T, 'P'));
1355 end if;
1357 -- If the access definition is of the form: ACCESS NOT NULL ..
1358 -- the subtype indication must be of an access type. Create
1359 -- a null-excluding subtype of it.
1361 if Null_Excluding_Subtype (Def) then
1362 if not Is_Access_Type (Entity (S)) then
1363 Error_Msg_N ("null exclusion must apply to access type", Def);
1365 else
1366 declare
1367 Loc : constant Source_Ptr := Sloc (S);
1368 Decl : Node_Id;
1369 Nam : constant Entity_Id := Make_Temporary (Loc, 'S');
1371 begin
1372 Decl :=
1373 Make_Subtype_Declaration (Loc,
1374 Defining_Identifier => Nam,
1375 Subtype_Indication =>
1376 New_Occurrence_Of (Entity (S), Loc));
1377 Set_Null_Exclusion_Present (Decl);
1378 Insert_Before (Parent (Def), Decl);
1379 Analyze (Decl);
1380 Set_Entity (S, Nam);
1381 end;
1382 end if;
1383 end if;
1385 else
1386 Set_Directly_Designated_Type (T,
1387 Process_Subtype (S, P, T, 'P'));
1388 end if;
1390 if All_Present (Def) or Constant_Present (Def) then
1391 Set_Ekind (T, E_General_Access_Type);
1392 else
1393 Set_Ekind (T, E_Access_Type);
1394 end if;
1396 Full_Desig := Designated_Type (T);
1398 if Base_Type (Full_Desig) = T then
1399 Error_Msg_N ("access type cannot designate itself", S);
1401 -- In Ada 2005, the type may have a limited view through some unit in
1402 -- its own context, allowing the following circularity that cannot be
1403 -- detected earlier.
1405 elsif Is_Class_Wide_Type (Full_Desig) and then Etype (Full_Desig) = T
1406 then
1407 Error_Msg_N
1408 ("access type cannot designate its own class-wide type", S);
1410 -- Clean up indication of tagged status to prevent cascaded errors
1412 Set_Is_Tagged_Type (T, False);
1413 end if;
1415 Set_Etype (T, T);
1417 -- If the type has appeared already in a with_type clause, it is frozen
1418 -- and the pointer size is already set. Else, initialize.
1420 if not From_Limited_With (T) then
1421 Init_Size_Align (T);
1422 end if;
1424 -- Note that Has_Task is always false, since the access type itself
1425 -- is not a task type. See Einfo for more description on this point.
1426 -- Exactly the same consideration applies to Has_Controlled_Component
1427 -- and to Has_Protected.
1429 Set_Has_Task (T, False);
1430 Set_Has_Protected (T, False);
1431 Set_Has_Timing_Event (T, False);
1432 Set_Has_Controlled_Component (T, False);
1434 -- Initialize field Finalization_Master explicitly to Empty, to avoid
1435 -- problems where an incomplete view of this entity has been previously
1436 -- established by a limited with and an overlaid version of this field
1437 -- (Stored_Constraint) was initialized for the incomplete view.
1439 -- This reset is performed in most cases except where the access type
1440 -- has been created for the purposes of allocating or deallocating a
1441 -- build-in-place object. Such access types have explicitly set pools
1442 -- and finalization masters.
1444 if No (Associated_Storage_Pool (T)) then
1445 Set_Finalization_Master (T, Empty);
1446 end if;
1448 -- Ada 2005 (AI-231): Propagate the null-excluding and access-constant
1449 -- attributes
1451 Set_Can_Never_Be_Null (T, Null_Exclusion_Present (Def));
1452 Set_Is_Access_Constant (T, Constant_Present (Def));
1453 end Access_Type_Declaration;
1455 ----------------------------------
1456 -- Add_Interface_Tag_Components --
1457 ----------------------------------
1459 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id) is
1460 Loc : constant Source_Ptr := Sloc (N);
1461 L : List_Id;
1462 Last_Tag : Node_Id;
1464 procedure Add_Tag (Iface : Entity_Id);
1465 -- Add tag for one of the progenitor interfaces
1467 -------------
1468 -- Add_Tag --
1469 -------------
1471 procedure Add_Tag (Iface : Entity_Id) is
1472 Decl : Node_Id;
1473 Def : Node_Id;
1474 Tag : Entity_Id;
1475 Offset : Entity_Id;
1477 begin
1478 pragma Assert (Is_Tagged_Type (Iface) and then Is_Interface (Iface));
1480 -- This is a reasonable place to propagate predicates
1482 if Has_Predicates (Iface) then
1483 Set_Has_Predicates (Typ);
1484 end if;
1486 Def :=
1487 Make_Component_Definition (Loc,
1488 Aliased_Present => True,
1489 Subtype_Indication =>
1490 New_Occurrence_Of (RTE (RE_Interface_Tag), Loc));
1492 Tag := Make_Temporary (Loc, 'V');
1494 Decl :=
1495 Make_Component_Declaration (Loc,
1496 Defining_Identifier => Tag,
1497 Component_Definition => Def);
1499 Analyze_Component_Declaration (Decl);
1501 Set_Analyzed (Decl);
1502 Set_Ekind (Tag, E_Component);
1503 Set_Is_Tag (Tag);
1504 Set_Is_Aliased (Tag);
1505 Set_Related_Type (Tag, Iface);
1506 Init_Component_Location (Tag);
1508 pragma Assert (Is_Frozen (Iface));
1510 Set_DT_Entry_Count (Tag,
1511 DT_Entry_Count (First_Entity (Iface)));
1513 if No (Last_Tag) then
1514 Prepend (Decl, L);
1515 else
1516 Insert_After (Last_Tag, Decl);
1517 end if;
1519 Last_Tag := Decl;
1521 -- If the ancestor has discriminants we need to give special support
1522 -- to store the offset_to_top value of the secondary dispatch tables.
1523 -- For this purpose we add a supplementary component just after the
1524 -- field that contains the tag associated with each secondary DT.
1526 if Typ /= Etype (Typ) and then Has_Discriminants (Etype (Typ)) then
1527 Def :=
1528 Make_Component_Definition (Loc,
1529 Subtype_Indication =>
1530 New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
1532 Offset := Make_Temporary (Loc, 'V');
1534 Decl :=
1535 Make_Component_Declaration (Loc,
1536 Defining_Identifier => Offset,
1537 Component_Definition => Def);
1539 Analyze_Component_Declaration (Decl);
1541 Set_Analyzed (Decl);
1542 Set_Ekind (Offset, E_Component);
1543 Set_Is_Aliased (Offset);
1544 Set_Related_Type (Offset, Iface);
1545 Init_Component_Location (Offset);
1546 Insert_After (Last_Tag, Decl);
1547 Last_Tag := Decl;
1548 end if;
1549 end Add_Tag;
1551 -- Local variables
1553 Elmt : Elmt_Id;
1554 Ext : Node_Id;
1555 Comp : Node_Id;
1557 -- Start of processing for Add_Interface_Tag_Components
1559 begin
1560 if not RTE_Available (RE_Interface_Tag) then
1561 Error_Msg
1562 ("(Ada 2005) interface types not supported by this run-time!",
1563 Sloc (N));
1564 return;
1565 end if;
1567 if Ekind (Typ) /= E_Record_Type
1568 or else (Is_Concurrent_Record_Type (Typ)
1569 and then Is_Empty_List (Abstract_Interface_List (Typ)))
1570 or else (not Is_Concurrent_Record_Type (Typ)
1571 and then No (Interfaces (Typ))
1572 and then Is_Empty_Elmt_List (Interfaces (Typ)))
1573 then
1574 return;
1575 end if;
1577 -- Find the current last tag
1579 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1580 Ext := Record_Extension_Part (Type_Definition (N));
1581 else
1582 pragma Assert (Nkind (Type_Definition (N)) = N_Record_Definition);
1583 Ext := Type_Definition (N);
1584 end if;
1586 Last_Tag := Empty;
1588 if not (Present (Component_List (Ext))) then
1589 Set_Null_Present (Ext, False);
1590 L := New_List;
1591 Set_Component_List (Ext,
1592 Make_Component_List (Loc,
1593 Component_Items => L,
1594 Null_Present => False));
1595 else
1596 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1597 L := Component_Items
1598 (Component_List
1599 (Record_Extension_Part
1600 (Type_Definition (N))));
1601 else
1602 L := Component_Items
1603 (Component_List
1604 (Type_Definition (N)));
1605 end if;
1607 -- Find the last tag component
1609 Comp := First (L);
1610 while Present (Comp) loop
1611 if Nkind (Comp) = N_Component_Declaration
1612 and then Is_Tag (Defining_Identifier (Comp))
1613 then
1614 Last_Tag := Comp;
1615 end if;
1617 Next (Comp);
1618 end loop;
1619 end if;
1621 -- At this point L references the list of components and Last_Tag
1622 -- references the current last tag (if any). Now we add the tag
1623 -- corresponding with all the interfaces that are not implemented
1624 -- by the parent.
1626 if Present (Interfaces (Typ)) then
1627 Elmt := First_Elmt (Interfaces (Typ));
1628 while Present (Elmt) loop
1629 Add_Tag (Node (Elmt));
1630 Next_Elmt (Elmt);
1631 end loop;
1632 end if;
1633 end Add_Interface_Tag_Components;
1635 -------------------------------------
1636 -- Add_Internal_Interface_Entities --
1637 -------------------------------------
1639 procedure Add_Internal_Interface_Entities (Tagged_Type : Entity_Id) is
1640 Elmt : Elmt_Id;
1641 Iface : Entity_Id;
1642 Iface_Elmt : Elmt_Id;
1643 Iface_Prim : Entity_Id;
1644 Ifaces_List : Elist_Id;
1645 New_Subp : Entity_Id := Empty;
1646 Prim : Entity_Id;
1647 Restore_Scope : Boolean := False;
1649 begin
1650 pragma Assert (Ada_Version >= Ada_2005
1651 and then Is_Record_Type (Tagged_Type)
1652 and then Is_Tagged_Type (Tagged_Type)
1653 and then Has_Interfaces (Tagged_Type)
1654 and then not Is_Interface (Tagged_Type));
1656 -- Ensure that the internal entities are added to the scope of the type
1658 if Scope (Tagged_Type) /= Current_Scope then
1659 Push_Scope (Scope (Tagged_Type));
1660 Restore_Scope := True;
1661 end if;
1663 Collect_Interfaces (Tagged_Type, Ifaces_List);
1665 Iface_Elmt := First_Elmt (Ifaces_List);
1666 while Present (Iface_Elmt) loop
1667 Iface := Node (Iface_Elmt);
1669 -- Originally we excluded here from this processing interfaces that
1670 -- are parents of Tagged_Type because their primitives are located
1671 -- in the primary dispatch table (and hence no auxiliary internal
1672 -- entities are required to handle secondary dispatch tables in such
1673 -- case). However, these auxiliary entities are also required to
1674 -- handle derivations of interfaces in formals of generics (see
1675 -- Derive_Subprograms).
1677 Elmt := First_Elmt (Primitive_Operations (Iface));
1678 while Present (Elmt) loop
1679 Iface_Prim := Node (Elmt);
1681 if not Is_Predefined_Dispatching_Operation (Iface_Prim) then
1682 Prim :=
1683 Find_Primitive_Covering_Interface
1684 (Tagged_Type => Tagged_Type,
1685 Iface_Prim => Iface_Prim);
1687 if No (Prim) and then Serious_Errors_Detected > 0 then
1688 goto Continue;
1689 end if;
1691 pragma Assert (Present (Prim));
1693 -- Ada 2012 (AI05-0197): If the name of the covering primitive
1694 -- differs from the name of the interface primitive then it is
1695 -- a private primitive inherited from a parent type. In such
1696 -- case, given that Tagged_Type covers the interface, the
1697 -- inherited private primitive becomes visible. For such
1698 -- purpose we add a new entity that renames the inherited
1699 -- private primitive.
1701 if Chars (Prim) /= Chars (Iface_Prim) then
1702 pragma Assert (Has_Suffix (Prim, 'P'));
1703 Derive_Subprogram
1704 (New_Subp => New_Subp,
1705 Parent_Subp => Iface_Prim,
1706 Derived_Type => Tagged_Type,
1707 Parent_Type => Iface);
1708 Set_Alias (New_Subp, Prim);
1709 Set_Is_Abstract_Subprogram
1710 (New_Subp, Is_Abstract_Subprogram (Prim));
1711 end if;
1713 Derive_Subprogram
1714 (New_Subp => New_Subp,
1715 Parent_Subp => Iface_Prim,
1716 Derived_Type => Tagged_Type,
1717 Parent_Type => Iface);
1719 declare
1720 Anc : Entity_Id;
1721 begin
1722 if Is_Inherited_Operation (Prim)
1723 and then Present (Alias (Prim))
1724 then
1725 Anc := Alias (Prim);
1726 else
1727 Anc := Overridden_Operation (Prim);
1728 end if;
1730 -- Apply legality checks in RM 6.1.1 (10-13) concerning
1731 -- nonconforming preconditions in both an ancestor and
1732 -- a progenitor operation.
1734 if Present (Anc)
1735 and then Has_Non_Trivial_Precondition (Anc)
1736 and then Has_Non_Trivial_Precondition (Iface_Prim)
1737 then
1738 if Is_Abstract_Subprogram (Prim)
1739 or else
1740 (Ekind (Prim) = E_Procedure
1741 and then Nkind (Parent (Prim)) =
1742 N_Procedure_Specification
1743 and then Null_Present (Parent (Prim)))
1744 then
1745 null;
1747 -- The inherited operation must be overridden
1749 elsif not Comes_From_Source (Prim) then
1750 Error_Msg_NE
1751 ("&inherits non-conforming preconditions and must "
1752 & "be overridden (RM 6.1.1 (10-16)",
1753 Parent (Tagged_Type), Prim);
1754 end if;
1755 end if;
1756 end;
1758 -- Ada 2005 (AI-251): Decorate internal entity Iface_Subp
1759 -- associated with interface types. These entities are
1760 -- only registered in the list of primitives of its
1761 -- corresponding tagged type because they are only used
1762 -- to fill the contents of the secondary dispatch tables.
1763 -- Therefore they are removed from the homonym chains.
1765 Set_Is_Hidden (New_Subp);
1766 Set_Is_Internal (New_Subp);
1767 Set_Alias (New_Subp, Prim);
1768 Set_Is_Abstract_Subprogram
1769 (New_Subp, Is_Abstract_Subprogram (Prim));
1770 Set_Interface_Alias (New_Subp, Iface_Prim);
1772 -- If the returned type is an interface then propagate it to
1773 -- the returned type. Needed by the thunk to generate the code
1774 -- which displaces "this" to reference the corresponding
1775 -- secondary dispatch table in the returned object.
1777 if Is_Interface (Etype (Iface_Prim)) then
1778 Set_Etype (New_Subp, Etype (Iface_Prim));
1779 end if;
1781 -- Internal entities associated with interface types are only
1782 -- registered in the list of primitives of the tagged type.
1783 -- They are only used to fill the contents of the secondary
1784 -- dispatch tables. Therefore they are not needed in the
1785 -- homonym chains.
1787 Remove_Homonym (New_Subp);
1789 -- Hidden entities associated with interfaces must have set
1790 -- the Has_Delay_Freeze attribute to ensure that, in case
1791 -- of locally defined tagged types (or compiling with static
1792 -- dispatch tables generation disabled) the corresponding
1793 -- entry of the secondary dispatch table is filled when such
1794 -- an entity is frozen. This is an expansion activity that must
1795 -- be suppressed for ASIS because it leads to gigi elaboration
1796 -- issues in annotate mode.
1798 if not ASIS_Mode then
1799 Set_Has_Delayed_Freeze (New_Subp);
1800 end if;
1801 end if;
1803 <<Continue>>
1804 Next_Elmt (Elmt);
1805 end loop;
1807 Next_Elmt (Iface_Elmt);
1808 end loop;
1810 if Restore_Scope then
1811 Pop_Scope;
1812 end if;
1813 end Add_Internal_Interface_Entities;
1815 -----------------------------------
1816 -- Analyze_Component_Declaration --
1817 -----------------------------------
1819 procedure Analyze_Component_Declaration (N : Node_Id) is
1820 Loc : constant Source_Ptr := Sloc (Component_Definition (N));
1821 Id : constant Entity_Id := Defining_Identifier (N);
1822 E : constant Node_Id := Expression (N);
1823 Typ : constant Node_Id :=
1824 Subtype_Indication (Component_Definition (N));
1825 T : Entity_Id;
1826 P : Entity_Id;
1828 function Contains_POC (Constr : Node_Id) return Boolean;
1829 -- Determines whether a constraint uses the discriminant of a record
1830 -- type thus becoming a per-object constraint (POC).
1832 function Is_Known_Limited (Typ : Entity_Id) return Boolean;
1833 -- Typ is the type of the current component, check whether this type is
1834 -- a limited type. Used to validate declaration against that of
1835 -- enclosing record.
1837 ------------------
1838 -- Contains_POC --
1839 ------------------
1841 function Contains_POC (Constr : Node_Id) return Boolean is
1842 begin
1843 -- Prevent cascaded errors
1845 if Error_Posted (Constr) then
1846 return False;
1847 end if;
1849 case Nkind (Constr) is
1850 when N_Attribute_Reference =>
1851 return Attribute_Name (Constr) = Name_Access
1852 and then Prefix (Constr) = Scope (Entity (Prefix (Constr)));
1854 when N_Discriminant_Association =>
1855 return Denotes_Discriminant (Expression (Constr));
1857 when N_Identifier =>
1858 return Denotes_Discriminant (Constr);
1860 when N_Index_Or_Discriminant_Constraint =>
1861 declare
1862 IDC : Node_Id;
1864 begin
1865 IDC := First (Constraints (Constr));
1866 while Present (IDC) loop
1868 -- One per-object constraint is sufficient
1870 if Contains_POC (IDC) then
1871 return True;
1872 end if;
1874 Next (IDC);
1875 end loop;
1877 return False;
1878 end;
1880 when N_Range =>
1881 return Denotes_Discriminant (Low_Bound (Constr))
1882 or else
1883 Denotes_Discriminant (High_Bound (Constr));
1885 when N_Range_Constraint =>
1886 return Denotes_Discriminant (Range_Expression (Constr));
1888 when others =>
1889 return False;
1890 end case;
1891 end Contains_POC;
1893 ----------------------
1894 -- Is_Known_Limited --
1895 ----------------------
1897 function Is_Known_Limited (Typ : Entity_Id) return Boolean is
1898 P : constant Entity_Id := Etype (Typ);
1899 R : constant Entity_Id := Root_Type (Typ);
1901 begin
1902 if Is_Limited_Record (Typ) then
1903 return True;
1905 -- If the root type is limited (and not a limited interface)
1906 -- so is the current type
1908 elsif Is_Limited_Record (R)
1909 and then (not Is_Interface (R) or else not Is_Limited_Interface (R))
1910 then
1911 return True;
1913 -- Else the type may have a limited interface progenitor, but a
1914 -- limited record parent.
1916 elsif R /= P and then Is_Limited_Record (P) then
1917 return True;
1919 else
1920 return False;
1921 end if;
1922 end Is_Known_Limited;
1924 -- Start of processing for Analyze_Component_Declaration
1926 begin
1927 Generate_Definition (Id);
1928 Enter_Name (Id);
1930 if Present (Typ) then
1931 T := Find_Type_Of_Object
1932 (Subtype_Indication (Component_Definition (N)), N);
1934 if not Nkind_In (Typ, N_Identifier, N_Expanded_Name) then
1935 Check_SPARK_05_Restriction ("subtype mark required", Typ);
1936 end if;
1938 -- Ada 2005 (AI-230): Access Definition case
1940 else
1941 pragma Assert (Present
1942 (Access_Definition (Component_Definition (N))));
1944 T := Access_Definition
1945 (Related_Nod => N,
1946 N => Access_Definition (Component_Definition (N)));
1947 Set_Is_Local_Anonymous_Access (T);
1949 -- Ada 2005 (AI-254)
1951 if Present (Access_To_Subprogram_Definition
1952 (Access_Definition (Component_Definition (N))))
1953 and then Protected_Present (Access_To_Subprogram_Definition
1954 (Access_Definition
1955 (Component_Definition (N))))
1956 then
1957 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1958 end if;
1959 end if;
1961 -- If the subtype is a constrained subtype of the enclosing record,
1962 -- (which must have a partial view) the back-end does not properly
1963 -- handle the recursion. Rewrite the component declaration with an
1964 -- explicit subtype indication, which is acceptable to Gigi. We can copy
1965 -- the tree directly because side effects have already been removed from
1966 -- discriminant constraints.
1968 if Ekind (T) = E_Access_Subtype
1969 and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
1970 and then Comes_From_Source (T)
1971 and then Nkind (Parent (T)) = N_Subtype_Declaration
1972 and then Etype (Directly_Designated_Type (T)) = Current_Scope
1973 then
1974 Rewrite
1975 (Subtype_Indication (Component_Definition (N)),
1976 New_Copy_Tree (Subtype_Indication (Parent (T))));
1977 T := Find_Type_Of_Object
1978 (Subtype_Indication (Component_Definition (N)), N);
1979 end if;
1981 -- If the component declaration includes a default expression, then we
1982 -- check that the component is not of a limited type (RM 3.7(5)),
1983 -- and do the special preanalysis of the expression (see section on
1984 -- "Handling of Default and Per-Object Expressions" in the spec of
1985 -- package Sem).
1987 if Present (E) then
1988 Check_SPARK_05_Restriction ("default expression is not allowed", E);
1989 Preanalyze_Default_Expression (E, T);
1990 Check_Initialization (T, E);
1992 if Ada_Version >= Ada_2005
1993 and then Ekind (T) = E_Anonymous_Access_Type
1994 and then Etype (E) /= Any_Type
1995 then
1996 -- Check RM 3.9.2(9): "if the expected type for an expression is
1997 -- an anonymous access-to-specific tagged type, then the object
1998 -- designated by the expression shall not be dynamically tagged
1999 -- unless it is a controlling operand in a call on a dispatching
2000 -- operation"
2002 if Is_Tagged_Type (Directly_Designated_Type (T))
2003 and then
2004 Ekind (Directly_Designated_Type (T)) /= E_Class_Wide_Type
2005 and then
2006 Ekind (Directly_Designated_Type (Etype (E))) =
2007 E_Class_Wide_Type
2008 then
2009 Error_Msg_N
2010 ("access to specific tagged type required (RM 3.9.2(9))", E);
2011 end if;
2013 -- (Ada 2005: AI-230): Accessibility check for anonymous
2014 -- components
2016 if Type_Access_Level (Etype (E)) >
2017 Deepest_Type_Access_Level (T)
2018 then
2019 Error_Msg_N
2020 ("expression has deeper access level than component " &
2021 "(RM 3.10.2 (12.2))", E);
2022 end if;
2024 -- The initialization expression is a reference to an access
2025 -- discriminant. The type of the discriminant is always deeper
2026 -- than any access type.
2028 if Ekind (Etype (E)) = E_Anonymous_Access_Type
2029 and then Is_Entity_Name (E)
2030 and then Ekind (Entity (E)) = E_In_Parameter
2031 and then Present (Discriminal_Link (Entity (E)))
2032 then
2033 Error_Msg_N
2034 ("discriminant has deeper accessibility level than target",
2036 end if;
2037 end if;
2038 end if;
2040 -- The parent type may be a private view with unknown discriminants,
2041 -- and thus unconstrained. Regular components must be constrained.
2043 if not Is_Definite_Subtype (T) and then Chars (Id) /= Name_uParent then
2044 if Is_Class_Wide_Type (T) then
2045 Error_Msg_N
2046 ("class-wide subtype with unknown discriminants" &
2047 " in component declaration",
2048 Subtype_Indication (Component_Definition (N)));
2049 else
2050 Error_Msg_N
2051 ("unconstrained subtype in component declaration",
2052 Subtype_Indication (Component_Definition (N)));
2053 end if;
2055 -- Components cannot be abstract, except for the special case of
2056 -- the _Parent field (case of extending an abstract tagged type)
2058 elsif Is_Abstract_Type (T) and then Chars (Id) /= Name_uParent then
2059 Error_Msg_N ("type of a component cannot be abstract", N);
2060 end if;
2062 Set_Etype (Id, T);
2063 Set_Is_Aliased (Id, Aliased_Present (Component_Definition (N)));
2065 -- The component declaration may have a per-object constraint, set
2066 -- the appropriate flag in the defining identifier of the subtype.
2068 if Present (Subtype_Indication (Component_Definition (N))) then
2069 declare
2070 Sindic : constant Node_Id :=
2071 Subtype_Indication (Component_Definition (N));
2072 begin
2073 if Nkind (Sindic) = N_Subtype_Indication
2074 and then Present (Constraint (Sindic))
2075 and then Contains_POC (Constraint (Sindic))
2076 then
2077 Set_Has_Per_Object_Constraint (Id);
2078 end if;
2079 end;
2080 end if;
2082 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
2083 -- out some static checks.
2085 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (T) then
2086 Null_Exclusion_Static_Checks (N);
2087 end if;
2089 -- If this component is private (or depends on a private type), flag the
2090 -- record type to indicate that some operations are not available.
2092 P := Private_Component (T);
2094 if Present (P) then
2096 -- Check for circular definitions
2098 if P = Any_Type then
2099 Set_Etype (Id, Any_Type);
2101 -- There is a gap in the visibility of operations only if the
2102 -- component type is not defined in the scope of the record type.
2104 elsif Scope (P) = Scope (Current_Scope) then
2105 null;
2107 elsif Is_Limited_Type (P) then
2108 Set_Is_Limited_Composite (Current_Scope);
2110 else
2111 Set_Is_Private_Composite (Current_Scope);
2112 end if;
2113 end if;
2115 if P /= Any_Type
2116 and then Is_Limited_Type (T)
2117 and then Chars (Id) /= Name_uParent
2118 and then Is_Tagged_Type (Current_Scope)
2119 then
2120 if Is_Derived_Type (Current_Scope)
2121 and then not Is_Known_Limited (Current_Scope)
2122 then
2123 Error_Msg_N
2124 ("extension of nonlimited type cannot have limited components",
2127 if Is_Interface (Root_Type (Current_Scope)) then
2128 Error_Msg_N
2129 ("\limitedness is not inherited from limited interface", N);
2130 Error_Msg_N ("\add LIMITED to type indication", N);
2131 end if;
2133 Explain_Limited_Type (T, N);
2134 Set_Etype (Id, Any_Type);
2135 Set_Is_Limited_Composite (Current_Scope, False);
2137 elsif not Is_Derived_Type (Current_Scope)
2138 and then not Is_Limited_Record (Current_Scope)
2139 and then not Is_Concurrent_Type (Current_Scope)
2140 then
2141 Error_Msg_N
2142 ("nonlimited tagged type cannot have limited components", N);
2143 Explain_Limited_Type (T, N);
2144 Set_Etype (Id, Any_Type);
2145 Set_Is_Limited_Composite (Current_Scope, False);
2146 end if;
2147 end if;
2149 -- If the component is an unconstrained task or protected type with
2150 -- discriminants, the component and the enclosing record are limited
2151 -- and the component is constrained by its default values. Compute
2152 -- its actual subtype, else it may be allocated the maximum size by
2153 -- the backend, and possibly overflow.
2155 if Is_Concurrent_Type (T)
2156 and then not Is_Constrained (T)
2157 and then Has_Discriminants (T)
2158 and then not Has_Discriminants (Current_Scope)
2159 then
2160 declare
2161 Act_T : constant Entity_Id := Build_Default_Subtype (T, N);
2163 begin
2164 Set_Etype (Id, Act_T);
2166 -- Rewrite component definition to use the constrained subtype
2168 Rewrite (Component_Definition (N),
2169 Make_Component_Definition (Loc,
2170 Subtype_Indication => New_Occurrence_Of (Act_T, Loc)));
2171 end;
2172 end if;
2174 Set_Original_Record_Component (Id, Id);
2176 if Has_Aspects (N) then
2177 Analyze_Aspect_Specifications (N, Id);
2178 end if;
2180 Analyze_Dimension (N);
2181 end Analyze_Component_Declaration;
2183 --------------------------
2184 -- Analyze_Declarations --
2185 --------------------------
2187 procedure Analyze_Declarations (L : List_Id) is
2188 Decl : Node_Id;
2190 procedure Adjust_Decl;
2191 -- Adjust Decl not to include implicit label declarations, since these
2192 -- have strange Sloc values that result in elaboration check problems.
2193 -- (They have the sloc of the label as found in the source, and that
2194 -- is ahead of the current declarative part).
2196 procedure Build_Assertion_Bodies (Decls : List_Id; Context : Node_Id);
2197 -- Create the subprogram bodies which verify the run-time semantics of
2198 -- the pragmas listed below for each elibigle type found in declarative
2199 -- list Decls. The pragmas are:
2201 -- Default_Initial_Condition
2202 -- Invariant
2203 -- Type_Invariant
2205 -- Context denotes the owner of the declarative list.
2207 procedure Check_Entry_Contracts;
2208 -- Perform a pre-analysis of the pre- and postconditions of an entry
2209 -- declaration. This must be done before full resolution and creation
2210 -- of the parameter block, etc. to catch illegal uses within the
2211 -- contract expression. Full analysis of the expression is done when
2212 -- the contract is processed.
2214 function Contains_Lib_Incomplete_Type (Pkg : Entity_Id) return Boolean;
2215 -- Check if a nested package has entities within it that rely on library
2216 -- level private types where the full view has not been completed for
2217 -- the purposes of checking if it is acceptable to freeze an expression
2218 -- function at the point of declaration.
2220 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id);
2221 -- Determine whether Body_Decl denotes the body of a late controlled
2222 -- primitive (either Initialize, Adjust or Finalize). If this is the
2223 -- case, add a proper spec if the body lacks one. The spec is inserted
2224 -- before Body_Decl and immediately analyzed.
2226 procedure Remove_Partial_Visible_Refinements (Spec_Id : Entity_Id);
2227 -- Spec_Id is the entity of a package that may define abstract states,
2228 -- and in the case of a child unit, whose ancestors may define abstract
2229 -- states. If the states have partial visible refinement, remove the
2230 -- partial visibility of each constituent at the end of the package
2231 -- spec and body declarations.
2233 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id);
2234 -- Spec_Id is the entity of a package that may define abstract states.
2235 -- If the states have visible refinement, remove the visibility of each
2236 -- constituent at the end of the package body declaration.
2238 procedure Resolve_Aspects;
2239 -- Utility to resolve the expressions of aspects at the end of a list of
2240 -- declarations, or before a declaration that freezes previous entities,
2241 -- such as in a subprogram body.
2243 -----------------
2244 -- Adjust_Decl --
2245 -----------------
2247 procedure Adjust_Decl is
2248 begin
2249 while Present (Prev (Decl))
2250 and then Nkind (Decl) = N_Implicit_Label_Declaration
2251 loop
2252 Prev (Decl);
2253 end loop;
2254 end Adjust_Decl;
2256 ----------------------------
2257 -- Build_Assertion_Bodies --
2258 ----------------------------
2260 procedure Build_Assertion_Bodies (Decls : List_Id; Context : Node_Id) is
2261 procedure Build_Assertion_Bodies_For_Type (Typ : Entity_Id);
2262 -- Create the subprogram bodies which verify the run-time semantics
2263 -- of the pragmas listed below for type Typ. The pragmas are:
2265 -- Default_Initial_Condition
2266 -- Invariant
2267 -- Type_Invariant
2269 -------------------------------------
2270 -- Build_Assertion_Bodies_For_Type --
2271 -------------------------------------
2273 procedure Build_Assertion_Bodies_For_Type (Typ : Entity_Id) is
2274 begin
2275 -- Preanalyze and resolve the Default_Initial_Condition assertion
2276 -- expression at the end of the declarations to catch any errors.
2278 if Has_DIC (Typ) then
2279 Build_DIC_Procedure_Body (Typ);
2280 end if;
2282 if Nkind (Context) = N_Package_Specification then
2284 -- Preanalyze and resolve the class-wide invariants of an
2285 -- interface at the end of whichever declarative part has the
2286 -- interface type. Note that an interface may be declared in
2287 -- any non-package declarative part, but reaching the end of
2288 -- such a declarative part will always freeze the type and
2289 -- generate the invariant procedure (see Freeze_Type).
2291 if Is_Interface (Typ) then
2293 -- Interfaces are treated as the partial view of a private
2294 -- type, in order to achieve uniformity with the general
2295 -- case. As a result, an interface receives only a "partial"
2296 -- invariant procedure, which is never called.
2298 if Has_Own_Invariants (Typ) then
2299 Build_Invariant_Procedure_Body
2300 (Typ => Typ,
2301 Partial_Invariant => True);
2302 end if;
2304 -- Preanalyze and resolve the invariants of a private type
2305 -- at the end of the visible declarations to catch potential
2306 -- errors. Inherited class-wide invariants are not included
2307 -- because they have already been resolved.
2309 elsif Decls = Visible_Declarations (Context)
2310 and then Ekind_In (Typ, E_Limited_Private_Type,
2311 E_Private_Type,
2312 E_Record_Type_With_Private)
2313 and then Has_Own_Invariants (Typ)
2314 then
2315 Build_Invariant_Procedure_Body
2316 (Typ => Typ,
2317 Partial_Invariant => True);
2319 -- Preanalyze and resolve the invariants of a private type's
2320 -- full view at the end of the private declarations to catch
2321 -- potential errors.
2323 elsif Decls = Private_Declarations (Context)
2324 and then not Is_Private_Type (Typ)
2325 and then Has_Private_Declaration (Typ)
2326 and then Has_Invariants (Typ)
2327 then
2328 Build_Invariant_Procedure_Body (Typ);
2329 end if;
2330 end if;
2331 end Build_Assertion_Bodies_For_Type;
2333 -- Local variables
2335 Decl : Node_Id;
2336 Decl_Id : Entity_Id;
2338 -- Start of processing for Build_Assertion_Bodies
2340 begin
2341 Decl := First (Decls);
2342 while Present (Decl) loop
2343 if Is_Declaration (Decl) then
2344 Decl_Id := Defining_Entity (Decl);
2346 if Is_Type (Decl_Id) then
2347 Build_Assertion_Bodies_For_Type (Decl_Id);
2348 end if;
2349 end if;
2351 Next (Decl);
2352 end loop;
2353 end Build_Assertion_Bodies;
2355 ---------------------------
2356 -- Check_Entry_Contracts --
2357 ---------------------------
2359 procedure Check_Entry_Contracts is
2360 ASN : Node_Id;
2361 Ent : Entity_Id;
2362 Exp : Node_Id;
2364 begin
2365 Ent := First_Entity (Current_Scope);
2366 while Present (Ent) loop
2368 -- This only concerns entries with pre/postconditions
2370 if Ekind (Ent) = E_Entry
2371 and then Present (Contract (Ent))
2372 and then Present (Pre_Post_Conditions (Contract (Ent)))
2373 then
2374 ASN := Pre_Post_Conditions (Contract (Ent));
2375 Push_Scope (Ent);
2376 Install_Formals (Ent);
2378 -- Pre/postconditions are rewritten as Check pragmas. Analysis
2379 -- is performed on a copy of the pragma expression, to prevent
2380 -- modifying the original expression.
2382 while Present (ASN) loop
2383 if Nkind (ASN) = N_Pragma then
2384 Exp :=
2385 New_Copy_Tree
2386 (Expression
2387 (First (Pragma_Argument_Associations (ASN))));
2388 Set_Parent (Exp, ASN);
2390 Preanalyze_Assert_Expression (Exp, Standard_Boolean);
2391 end if;
2393 ASN := Next_Pragma (ASN);
2394 end loop;
2396 End_Scope;
2397 end if;
2399 Next_Entity (Ent);
2400 end loop;
2401 end Check_Entry_Contracts;
2403 ----------------------------------
2404 -- Contains_Lib_Incomplete_Type --
2405 ----------------------------------
2407 function Contains_Lib_Incomplete_Type (Pkg : Entity_Id) return Boolean is
2408 Curr : Entity_Id;
2410 begin
2411 -- Avoid looking through scopes that do not meet the precondition of
2412 -- Pkg not being within a library unit spec.
2414 if not Is_Compilation_Unit (Pkg)
2415 and then not Is_Generic_Instance (Pkg)
2416 and then not In_Package_Body (Enclosing_Lib_Unit_Entity (Pkg))
2417 then
2418 -- Loop through all entities in the current scope to identify
2419 -- an entity that depends on a private type.
2421 Curr := First_Entity (Pkg);
2422 loop
2423 if Nkind (Curr) in N_Entity
2424 and then Depends_On_Private (Curr)
2425 then
2426 return True;
2427 end if;
2429 exit when Last_Entity (Current_Scope) = Curr;
2430 Curr := Next_Entity (Curr);
2431 end loop;
2432 end if;
2434 return False;
2435 end Contains_Lib_Incomplete_Type;
2437 --------------------------------------
2438 -- Handle_Late_Controlled_Primitive --
2439 --------------------------------------
2441 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id) is
2442 Body_Spec : constant Node_Id := Specification (Body_Decl);
2443 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2444 Loc : constant Source_Ptr := Sloc (Body_Id);
2445 Params : constant List_Id :=
2446 Parameter_Specifications (Body_Spec);
2447 Spec : Node_Id;
2448 Spec_Id : Entity_Id;
2449 Typ : Node_Id;
2451 begin
2452 -- Consider only procedure bodies whose name matches one of the three
2453 -- controlled primitives.
2455 if Nkind (Body_Spec) /= N_Procedure_Specification
2456 or else not Nam_In (Chars (Body_Id), Name_Adjust,
2457 Name_Finalize,
2458 Name_Initialize)
2459 then
2460 return;
2462 -- A controlled primitive must have exactly one formal which is not
2463 -- an anonymous access type.
2465 elsif List_Length (Params) /= 1 then
2466 return;
2467 end if;
2469 Typ := Parameter_Type (First (Params));
2471 if Nkind (Typ) = N_Access_Definition then
2472 return;
2473 end if;
2475 Find_Type (Typ);
2477 -- The type of the formal must be derived from [Limited_]Controlled
2479 if not Is_Controlled (Entity (Typ)) then
2480 return;
2481 end if;
2483 -- Check whether a specification exists for this body. We do not
2484 -- analyze the spec of the body in full, because it will be analyzed
2485 -- again when the body is properly analyzed, and we cannot create
2486 -- duplicate entries in the formals chain. We look for an explicit
2487 -- specification because the body may be an overriding operation and
2488 -- an inherited spec may be present.
2490 Spec_Id := Current_Entity (Body_Id);
2492 while Present (Spec_Id) loop
2493 if Ekind_In (Spec_Id, E_Procedure, E_Generic_Procedure)
2494 and then Scope (Spec_Id) = Current_Scope
2495 and then Present (First_Formal (Spec_Id))
2496 and then No (Next_Formal (First_Formal (Spec_Id)))
2497 and then Etype (First_Formal (Spec_Id)) = Entity (Typ)
2498 and then Comes_From_Source (Spec_Id)
2499 then
2500 return;
2501 end if;
2503 Spec_Id := Homonym (Spec_Id);
2504 end loop;
2506 -- At this point the body is known to be a late controlled primitive.
2507 -- Generate a matching spec and insert it before the body. Note the
2508 -- use of Copy_Separate_Tree - we want an entirely separate semantic
2509 -- tree in this case.
2511 Spec := Copy_Separate_Tree (Body_Spec);
2513 -- Ensure that the subprogram declaration does not inherit the null
2514 -- indicator from the body as we now have a proper spec/body pair.
2516 Set_Null_Present (Spec, False);
2518 -- Ensure that the freeze node is inserted after the declaration of
2519 -- the primitive since its expansion will freeze the primitive.
2521 Decl := Make_Subprogram_Declaration (Loc, Specification => Spec);
2523 Insert_Before_And_Analyze (Body_Decl, Decl);
2524 end Handle_Late_Controlled_Primitive;
2526 ----------------------------------------
2527 -- Remove_Partial_Visible_Refinements --
2528 ----------------------------------------
2530 procedure Remove_Partial_Visible_Refinements (Spec_Id : Entity_Id) is
2531 State_Elmt : Elmt_Id;
2532 begin
2533 if Present (Abstract_States (Spec_Id)) then
2534 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2535 while Present (State_Elmt) loop
2536 Set_Has_Partial_Visible_Refinement (Node (State_Elmt), False);
2537 Next_Elmt (State_Elmt);
2538 end loop;
2539 end if;
2541 -- For a child unit, also hide the partial state refinement from
2542 -- ancestor packages.
2544 if Is_Child_Unit (Spec_Id) then
2545 Remove_Partial_Visible_Refinements (Scope (Spec_Id));
2546 end if;
2547 end Remove_Partial_Visible_Refinements;
2549 --------------------------------
2550 -- Remove_Visible_Refinements --
2551 --------------------------------
2553 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id) is
2554 State_Elmt : Elmt_Id;
2555 begin
2556 if Present (Abstract_States (Spec_Id)) then
2557 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2558 while Present (State_Elmt) loop
2559 Set_Has_Visible_Refinement (Node (State_Elmt), False);
2560 Next_Elmt (State_Elmt);
2561 end loop;
2562 end if;
2563 end Remove_Visible_Refinements;
2565 ---------------------
2566 -- Resolve_Aspects --
2567 ---------------------
2569 procedure Resolve_Aspects is
2570 E : Entity_Id;
2572 begin
2573 E := First_Entity (Current_Scope);
2574 while Present (E) loop
2575 Resolve_Aspect_Expressions (E);
2576 Next_Entity (E);
2577 end loop;
2578 end Resolve_Aspects;
2580 -- Local variables
2582 Context : Node_Id := Empty;
2583 Freeze_From : Entity_Id := Empty;
2584 Next_Decl : Node_Id;
2586 Body_Seen : Boolean := False;
2587 -- Flag set when the first body [stub] is encountered
2589 -- Start of processing for Analyze_Declarations
2591 begin
2592 if Restriction_Check_Required (SPARK_05) then
2593 Check_Later_Vs_Basic_Declarations (L, During_Parsing => False);
2594 end if;
2596 Decl := First (L);
2597 while Present (Decl) loop
2599 -- Package spec cannot contain a package declaration in SPARK
2601 if Nkind (Decl) = N_Package_Declaration
2602 and then Nkind (Parent (L)) = N_Package_Specification
2603 then
2604 Check_SPARK_05_Restriction
2605 ("package specification cannot contain a package declaration",
2606 Decl);
2607 end if;
2609 -- Complete analysis of declaration
2611 Analyze (Decl);
2612 Next_Decl := Next (Decl);
2614 if No (Freeze_From) then
2615 Freeze_From := First_Entity (Current_Scope);
2616 end if;
2618 -- At the end of a declarative part, freeze remaining entities
2619 -- declared in it. The end of the visible declarations of package
2620 -- specification is not the end of a declarative part if private
2621 -- declarations are present. The end of a package declaration is a
2622 -- freezing point only if it a library package. A task definition or
2623 -- protected type definition is not a freeze point either. Finally,
2624 -- we do not freeze entities in generic scopes, because there is no
2625 -- code generated for them and freeze nodes will be generated for
2626 -- the instance.
2628 -- The end of a package instantiation is not a freeze point, but
2629 -- for now we make it one, because the generic body is inserted
2630 -- (currently) immediately after. Generic instantiations will not
2631 -- be a freeze point once delayed freezing of bodies is implemented.
2632 -- (This is needed in any case for early instantiations ???).
2634 if No (Next_Decl) then
2635 if Nkind (Parent (L)) = N_Component_List then
2636 null;
2638 elsif Nkind_In (Parent (L), N_Protected_Definition,
2639 N_Task_Definition)
2640 then
2641 Check_Entry_Contracts;
2643 elsif Nkind (Parent (L)) /= N_Package_Specification then
2644 if Nkind (Parent (L)) = N_Package_Body then
2645 Freeze_From := First_Entity (Current_Scope);
2646 end if;
2648 -- There may have been several freezing points previously,
2649 -- for example object declarations or subprogram bodies, but
2650 -- at the end of a declarative part we check freezing from
2651 -- the beginning, even though entities may already be frozen,
2652 -- in order to perform visibility checks on delayed aspects.
2654 Adjust_Decl;
2656 -- If the current scope is a generic subprogram body. Skip the
2657 -- generic formal parameters that are not frozen here.
2659 if Is_Subprogram (Current_Scope)
2660 and then Nkind (Unit_Declaration_Node (Current_Scope)) =
2661 N_Generic_Subprogram_Declaration
2662 and then Present (First_Entity (Current_Scope))
2663 then
2664 while Is_Generic_Formal (Freeze_From) loop
2665 Freeze_From := Next_Entity (Freeze_From);
2666 end loop;
2668 Freeze_All (Freeze_From, Decl);
2669 Freeze_From := Last_Entity (Current_Scope);
2671 else
2672 -- For declarations in a subprogram body there is no issue
2673 -- with name resolution in aspect specifications, but in
2674 -- ASIS mode we need to preanalyze aspect specifications
2675 -- that may otherwise only be analyzed during expansion
2676 -- (e.g. during generation of a related subprogram).
2678 if ASIS_Mode then
2679 Resolve_Aspects;
2680 end if;
2682 Freeze_All (First_Entity (Current_Scope), Decl);
2683 Freeze_From := Last_Entity (Current_Scope);
2684 end if;
2686 -- Current scope is a package specification
2688 elsif Scope (Current_Scope) /= Standard_Standard
2689 and then not Is_Child_Unit (Current_Scope)
2690 and then No (Generic_Parent (Parent (L)))
2691 then
2692 -- ARM rule 13.1.1(11/3): usage names in aspect definitions are
2693 -- resolved at the end of the immediately enclosing declaration
2694 -- list (AI05-0183-1).
2696 Resolve_Aspects;
2698 elsif L /= Visible_Declarations (Parent (L))
2699 or else No (Private_Declarations (Parent (L)))
2700 or else Is_Empty_List (Private_Declarations (Parent (L)))
2701 then
2702 Adjust_Decl;
2704 -- End of a package declaration
2706 -- In compilation mode the expansion of freeze node takes care
2707 -- of resolving expressions of all aspects in the list. In ASIS
2708 -- mode this must be done explicitly.
2710 if ASIS_Mode
2711 and then Scope (Current_Scope) = Standard_Standard
2712 then
2713 Resolve_Aspects;
2714 end if;
2716 -- This is a freeze point because it is the end of a
2717 -- compilation unit.
2719 Freeze_All (First_Entity (Current_Scope), Decl);
2720 Freeze_From := Last_Entity (Current_Scope);
2722 -- At the end of the visible declarations the expressions in
2723 -- aspects of all entities declared so far must be resolved.
2724 -- The entities themselves might be frozen later, and the
2725 -- generated pragmas and attribute definition clauses analyzed
2726 -- in full at that point, but name resolution must take place
2727 -- now.
2728 -- In addition to being the proper semantics, this is mandatory
2729 -- within generic units, because global name capture requires
2730 -- those expressions to be analyzed, given that the generated
2731 -- pragmas do not appear in the original generic tree.
2733 elsif Serious_Errors_Detected = 0 then
2734 Resolve_Aspects;
2735 end if;
2737 -- If next node is a body then freeze all types before the body.
2738 -- An exception occurs for some expander-generated bodies. If these
2739 -- are generated at places where in general language rules would not
2740 -- allow a freeze point, then we assume that the expander has
2741 -- explicitly checked that all required types are properly frozen,
2742 -- and we do not cause general freezing here. This special circuit
2743 -- is used when the encountered body is marked as having already
2744 -- been analyzed.
2746 -- In all other cases (bodies that come from source, and expander
2747 -- generated bodies that have not been analyzed yet), freeze all
2748 -- types now. Note that in the latter case, the expander must take
2749 -- care to attach the bodies at a proper place in the tree so as to
2750 -- not cause unwanted freezing at that point.
2752 -- It is also necessary to check for a case where both an expression
2753 -- function is used and the current scope depends on an incomplete
2754 -- private type from a library unit, otherwise premature freezing of
2755 -- the private type will occur.
2757 elsif not Analyzed (Next_Decl) and then Is_Body (Next_Decl)
2758 and then ((Nkind (Next_Decl) /= N_Subprogram_Body
2759 or else not Was_Expression_Function (Next_Decl))
2760 or else (not Is_Ignored_Ghost_Entity (Current_Scope)
2761 and then not Contains_Lib_Incomplete_Type
2762 (Current_Scope)))
2763 then
2764 -- When a controlled type is frozen, the expander generates stream
2765 -- and controlled-type support routines. If the freeze is caused
2766 -- by the stand-alone body of Initialize, Adjust, or Finalize, the
2767 -- expander will end up using the wrong version of these routines,
2768 -- as the body has not been processed yet. To remedy this, detect
2769 -- a late controlled primitive and create a proper spec for it.
2770 -- This ensures that the primitive will override its inherited
2771 -- counterpart before the freeze takes place.
2773 -- If the declaration we just processed is a body, do not attempt
2774 -- to examine Next_Decl as the late primitive idiom can only apply
2775 -- to the first encountered body.
2777 -- The spec of the late primitive is not generated in ASIS mode to
2778 -- ensure a consistent list of primitives that indicates the true
2779 -- semantic structure of the program (which is not relevant when
2780 -- generating executable code).
2782 -- ??? A cleaner approach may be possible and/or this solution
2783 -- could be extended to general-purpose late primitives, TBD.
2785 if not ASIS_Mode
2786 and then not Body_Seen
2787 and then not Is_Body (Decl)
2788 then
2789 Body_Seen := True;
2791 if Nkind (Next_Decl) = N_Subprogram_Body then
2792 Handle_Late_Controlled_Primitive (Next_Decl);
2793 end if;
2795 else
2796 -- In ASIS mode, if the next declaration is a body, complete
2797 -- the analysis of declarations so far.
2799 Resolve_Aspects;
2800 end if;
2802 Adjust_Decl;
2804 -- The generated body of an expression function does not freeze,
2805 -- unless it is a completion, in which case only the expression
2806 -- itself freezes. This is handled when the body itself is
2807 -- analyzed (see Freeze_Expr_Types, sem_ch6.adb).
2809 Freeze_All (Freeze_From, Decl);
2810 Freeze_From := Last_Entity (Current_Scope);
2811 end if;
2813 Decl := Next_Decl;
2814 end loop;
2816 -- Post-freezing actions
2818 if Present (L) then
2819 Context := Parent (L);
2821 -- Analyze the contracts of packages and their bodies
2823 if Nkind (Context) = N_Package_Specification then
2825 -- When a package has private declarations, its contract must be
2826 -- analyzed at the end of the said declarations. This way both the
2827 -- analysis and freeze actions are properly synchronized in case
2828 -- of private type use within the contract.
2830 if L = Private_Declarations (Context) then
2831 Analyze_Package_Contract (Defining_Entity (Context));
2833 -- Otherwise the contract is analyzed at the end of the visible
2834 -- declarations.
2836 elsif L = Visible_Declarations (Context)
2837 and then No (Private_Declarations (Context))
2838 then
2839 Analyze_Package_Contract (Defining_Entity (Context));
2840 end if;
2842 elsif Nkind (Context) = N_Package_Body then
2843 Analyze_Package_Body_Contract (Defining_Entity (Context));
2844 end if;
2846 -- Analyze the contracts of various constructs now due to the delayed
2847 -- visibility needs of their aspects and pragmas.
2849 Analyze_Contracts (L);
2851 if Nkind (Context) = N_Package_Body then
2853 -- Ensure that all abstract states and objects declared in the
2854 -- state space of a package body are utilized as constituents.
2856 Check_Unused_Body_States (Defining_Entity (Context));
2858 -- State refinements are visible up to the end of the package body
2859 -- declarations. Hide the state refinements from visibility to
2860 -- restore the original state conditions.
2862 Remove_Visible_Refinements (Corresponding_Spec (Context));
2863 Remove_Partial_Visible_Refinements (Corresponding_Spec (Context));
2865 elsif Nkind (Context) = N_Package_Declaration then
2867 -- Partial state refinements are visible up to the end of the
2868 -- package spec declarations. Hide the partial state refinements
2869 -- from visibility to restore the original state conditions.
2871 Remove_Partial_Visible_Refinements (Corresponding_Spec (Context));
2872 end if;
2874 -- Verify that all abstract states found in any package declared in
2875 -- the input declarative list have proper refinements. The check is
2876 -- performed only when the context denotes a block, entry, package,
2877 -- protected, subprogram, or task body (SPARK RM 7.2.2(3)).
2879 Check_State_Refinements (Context);
2881 -- Create the subprogram bodies which verify the run-time semantics
2882 -- of pragmas Default_Initial_Condition and [Type_]Invariant for all
2883 -- types within the current declarative list. This ensures that all
2884 -- assertion expressions are preanalyzed and resolved at the end of
2885 -- the declarative part. Note that the resolution happens even when
2886 -- freezing does not take place.
2888 Build_Assertion_Bodies (L, Context);
2889 end if;
2890 end Analyze_Declarations;
2892 -----------------------------------
2893 -- Analyze_Full_Type_Declaration --
2894 -----------------------------------
2896 procedure Analyze_Full_Type_Declaration (N : Node_Id) is
2897 Def : constant Node_Id := Type_Definition (N);
2898 Def_Id : constant Entity_Id := Defining_Identifier (N);
2899 T : Entity_Id;
2900 Prev : Entity_Id;
2902 Is_Remote : constant Boolean :=
2903 (Is_Remote_Types (Current_Scope)
2904 or else Is_Remote_Call_Interface (Current_Scope))
2905 and then not (In_Private_Part (Current_Scope)
2906 or else In_Package_Body (Current_Scope));
2908 procedure Check_Nonoverridable_Aspects;
2909 -- Apply the rule in RM 13.1.1(18.4/4) on iterator aspects that cannot
2910 -- be overridden, and can only be confirmed on derivation.
2912 procedure Check_Ops_From_Incomplete_Type;
2913 -- If there is a tagged incomplete partial view of the type, traverse
2914 -- the primitives of the incomplete view and change the type of any
2915 -- controlling formals and result to indicate the full view. The
2916 -- primitives will be added to the full type's primitive operations
2917 -- list later in Sem_Disp.Check_Operation_From_Incomplete_Type (which
2918 -- is called from Process_Incomplete_Dependents).
2920 ----------------------------------
2921 -- Check_Nonoverridable_Aspects --
2922 ----------------------------------
2924 procedure Check_Nonoverridable_Aspects is
2925 function Get_Aspect_Spec
2926 (Specs : List_Id;
2927 Aspect_Name : Name_Id) return Node_Id;
2928 -- Check whether a list of aspect specifications includes an entry
2929 -- for a specific aspect. The list is either that of a partial or
2930 -- a full view.
2932 ---------------------
2933 -- Get_Aspect_Spec --
2934 ---------------------
2936 function Get_Aspect_Spec
2937 (Specs : List_Id;
2938 Aspect_Name : Name_Id) return Node_Id
2940 Spec : Node_Id;
2942 begin
2943 Spec := First (Specs);
2944 while Present (Spec) loop
2945 if Chars (Identifier (Spec)) = Aspect_Name then
2946 return Spec;
2947 end if;
2948 Next (Spec);
2949 end loop;
2951 return Empty;
2952 end Get_Aspect_Spec;
2954 -- Local variables
2956 Prev_Aspects : constant List_Id :=
2957 Aspect_Specifications (Parent (Def_Id));
2958 Par_Type : Entity_Id;
2959 Prev_Aspect : Node_Id;
2961 -- Start of processing for Check_Nonoverridable_Aspects
2963 begin
2964 -- Get parent type of derived type. Note that Prev is the entity in
2965 -- the partial declaration, but its contents are now those of full
2966 -- view, while Def_Id reflects the partial view.
2968 if Is_Private_Type (Def_Id) then
2969 Par_Type := Etype (Full_View (Def_Id));
2970 else
2971 Par_Type := Etype (Def_Id);
2972 end if;
2974 -- If there is an inherited Implicit_Dereference, verify that it is
2975 -- made explicit in the partial view.
2977 if Has_Discriminants (Base_Type (Par_Type))
2978 and then Nkind (Parent (Prev)) = N_Full_Type_Declaration
2979 and then Present (Discriminant_Specifications (Parent (Prev)))
2980 and then Present (Get_Reference_Discriminant (Par_Type))
2981 then
2982 Prev_Aspect :=
2983 Get_Aspect_Spec (Prev_Aspects, Name_Implicit_Dereference);
2985 if No (Prev_Aspect)
2986 and then Present
2987 (Discriminant_Specifications
2988 (Original_Node (Parent (Prev))))
2989 then
2990 Error_Msg_N
2991 ("type does not inherit implicit dereference", Prev);
2993 else
2994 -- If one of the views has the aspect specified, verify that it
2995 -- is consistent with that of the parent.
2997 declare
2998 Par_Discr : constant Entity_Id :=
2999 Get_Reference_Discriminant (Par_Type);
3000 Cur_Discr : constant Entity_Id :=
3001 Get_Reference_Discriminant (Prev);
3003 begin
3004 if Corresponding_Discriminant (Cur_Discr) /= Par_Discr then
3005 Error_Msg_N ("aspect incosistent with that of parent", N);
3006 end if;
3008 -- Check that specification in partial view matches the
3009 -- inherited aspect. Compare names directly because aspect
3010 -- expression may not be analyzed.
3012 if Present (Prev_Aspect)
3013 and then Nkind (Expression (Prev_Aspect)) = N_Identifier
3014 and then Chars (Expression (Prev_Aspect)) /=
3015 Chars (Cur_Discr)
3016 then
3017 Error_Msg_N
3018 ("aspect incosistent with that of parent", N);
3019 end if;
3020 end;
3021 end if;
3022 end if;
3024 -- TBD : other nonoverridable aspects.
3025 end Check_Nonoverridable_Aspects;
3027 ------------------------------------
3028 -- Check_Ops_From_Incomplete_Type --
3029 ------------------------------------
3031 procedure Check_Ops_From_Incomplete_Type is
3032 Elmt : Elmt_Id;
3033 Formal : Entity_Id;
3034 Op : Entity_Id;
3036 begin
3037 if Prev /= T
3038 and then Ekind (Prev) = E_Incomplete_Type
3039 and then Is_Tagged_Type (Prev)
3040 and then Is_Tagged_Type (T)
3041 then
3042 Elmt := First_Elmt (Primitive_Operations (Prev));
3043 while Present (Elmt) loop
3044 Op := Node (Elmt);
3046 Formal := First_Formal (Op);
3047 while Present (Formal) loop
3048 if Etype (Formal) = Prev then
3049 Set_Etype (Formal, T);
3050 end if;
3052 Next_Formal (Formal);
3053 end loop;
3055 if Etype (Op) = Prev then
3056 Set_Etype (Op, T);
3057 end if;
3059 Next_Elmt (Elmt);
3060 end loop;
3061 end if;
3062 end Check_Ops_From_Incomplete_Type;
3064 -- Start of processing for Analyze_Full_Type_Declaration
3066 begin
3067 Prev := Find_Type_Name (N);
3069 -- The full view, if present, now points to the current type. If there
3070 -- is an incomplete partial view, set a link to it, to simplify the
3071 -- retrieval of primitive operations of the type.
3073 -- Ada 2005 (AI-50217): If the type was previously decorated when
3074 -- imported through a LIMITED WITH clause, it appears as incomplete
3075 -- but has no full view.
3077 if Ekind (Prev) = E_Incomplete_Type
3078 and then Present (Full_View (Prev))
3079 then
3080 T := Full_View (Prev);
3081 Set_Incomplete_View (N, Parent (Prev));
3082 else
3083 T := Prev;
3084 end if;
3086 Set_Is_Pure (T, Is_Pure (Current_Scope));
3088 -- We set the flag Is_First_Subtype here. It is needed to set the
3089 -- corresponding flag for the Implicit class-wide-type created
3090 -- during tagged types processing.
3092 Set_Is_First_Subtype (T, True);
3094 -- Only composite types other than array types are allowed to have
3095 -- discriminants.
3097 case Nkind (Def) is
3099 -- For derived types, the rule will be checked once we've figured
3100 -- out the parent type.
3102 when N_Derived_Type_Definition =>
3103 null;
3105 -- For record types, discriminants are allowed, unless we are in
3106 -- SPARK.
3108 when N_Record_Definition =>
3109 if Present (Discriminant_Specifications (N)) then
3110 Check_SPARK_05_Restriction
3111 ("discriminant type is not allowed",
3112 Defining_Identifier
3113 (First (Discriminant_Specifications (N))));
3114 end if;
3116 when others =>
3117 if Present (Discriminant_Specifications (N)) then
3118 Error_Msg_N
3119 ("elementary or array type cannot have discriminants",
3120 Defining_Identifier
3121 (First (Discriminant_Specifications (N))));
3122 end if;
3123 end case;
3125 -- Elaborate the type definition according to kind, and generate
3126 -- subsidiary (implicit) subtypes where needed. We skip this if it was
3127 -- already done (this happens during the reanalysis that follows a call
3128 -- to the high level optimizer).
3130 if not Analyzed (T) then
3131 Set_Analyzed (T);
3133 case Nkind (Def) is
3134 when N_Access_To_Subprogram_Definition =>
3135 Access_Subprogram_Declaration (T, Def);
3137 -- If this is a remote access to subprogram, we must create the
3138 -- equivalent fat pointer type, and related subprograms.
3140 if Is_Remote then
3141 Process_Remote_AST_Declaration (N);
3142 end if;
3144 -- Validate categorization rule against access type declaration
3145 -- usually a violation in Pure unit, Shared_Passive unit.
3147 Validate_Access_Type_Declaration (T, N);
3149 when N_Access_To_Object_Definition =>
3150 Access_Type_Declaration (T, Def);
3152 -- Validate categorization rule against access type declaration
3153 -- usually a violation in Pure unit, Shared_Passive unit.
3155 Validate_Access_Type_Declaration (T, N);
3157 -- If we are in a Remote_Call_Interface package and define a
3158 -- RACW, then calling stubs and specific stream attributes
3159 -- must be added.
3161 if Is_Remote
3162 and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
3163 then
3164 Add_RACW_Features (Def_Id);
3165 end if;
3167 when N_Array_Type_Definition =>
3168 Array_Type_Declaration (T, Def);
3170 when N_Derived_Type_Definition =>
3171 Derived_Type_Declaration (T, N, T /= Def_Id);
3173 -- Inherit predicates from parent, and protect against illegal
3174 -- derivations.
3176 if Is_Type (T) and then Has_Predicates (T) then
3177 Set_Has_Predicates (Def_Id);
3178 end if;
3180 when N_Enumeration_Type_Definition =>
3181 Enumeration_Type_Declaration (T, Def);
3183 when N_Floating_Point_Definition =>
3184 Floating_Point_Type_Declaration (T, Def);
3186 when N_Decimal_Fixed_Point_Definition =>
3187 Decimal_Fixed_Point_Type_Declaration (T, Def);
3189 when N_Ordinary_Fixed_Point_Definition =>
3190 Ordinary_Fixed_Point_Type_Declaration (T, Def);
3192 when N_Signed_Integer_Type_Definition =>
3193 Signed_Integer_Type_Declaration (T, Def);
3195 when N_Modular_Type_Definition =>
3196 Modular_Type_Declaration (T, Def);
3198 when N_Record_Definition =>
3199 Record_Type_Declaration (T, N, Prev);
3201 -- If declaration has a parse error, nothing to elaborate.
3203 when N_Error =>
3204 null;
3206 when others =>
3207 raise Program_Error;
3208 end case;
3209 end if;
3211 if Etype (T) = Any_Type then
3212 return;
3213 end if;
3215 -- Controlled type is not allowed in SPARK
3217 if Is_Visibly_Controlled (T) then
3218 Check_SPARK_05_Restriction ("controlled type is not allowed", N);
3219 end if;
3221 -- Some common processing for all types
3223 Set_Depends_On_Private (T, Has_Private_Component (T));
3224 Check_Ops_From_Incomplete_Type;
3226 -- Both the declared entity, and its anonymous base type if one was
3227 -- created, need freeze nodes allocated.
3229 declare
3230 B : constant Entity_Id := Base_Type (T);
3232 begin
3233 -- In the case where the base type differs from the first subtype, we
3234 -- pre-allocate a freeze node, and set the proper link to the first
3235 -- subtype. Freeze_Entity will use this preallocated freeze node when
3236 -- it freezes the entity.
3238 -- This does not apply if the base type is a generic type, whose
3239 -- declaration is independent of the current derived definition.
3241 if B /= T and then not Is_Generic_Type (B) then
3242 Ensure_Freeze_Node (B);
3243 Set_First_Subtype_Link (Freeze_Node (B), T);
3244 end if;
3246 -- A type that is imported through a limited_with clause cannot
3247 -- generate any code, and thus need not be frozen. However, an access
3248 -- type with an imported designated type needs a finalization list,
3249 -- which may be referenced in some other package that has non-limited
3250 -- visibility on the designated type. Thus we must create the
3251 -- finalization list at the point the access type is frozen, to
3252 -- prevent unsatisfied references at link time.
3254 if not From_Limited_With (T) or else Is_Access_Type (T) then
3255 Set_Has_Delayed_Freeze (T);
3256 end if;
3257 end;
3259 -- Case where T is the full declaration of some private type which has
3260 -- been swapped in Defining_Identifier (N).
3262 if T /= Def_Id and then Is_Private_Type (Def_Id) then
3263 Process_Full_View (N, T, Def_Id);
3265 -- Record the reference. The form of this is a little strange, since
3266 -- the full declaration has been swapped in. So the first parameter
3267 -- here represents the entity to which a reference is made which is
3268 -- the "real" entity, i.e. the one swapped in, and the second
3269 -- parameter provides the reference location.
3271 -- Also, we want to kill Has_Pragma_Unreferenced temporarily here
3272 -- since we don't want a complaint about the full type being an
3273 -- unwanted reference to the private type
3275 declare
3276 B : constant Boolean := Has_Pragma_Unreferenced (T);
3277 begin
3278 Set_Has_Pragma_Unreferenced (T, False);
3279 Generate_Reference (T, T, 'c');
3280 Set_Has_Pragma_Unreferenced (T, B);
3281 end;
3283 Set_Completion_Referenced (Def_Id);
3285 -- For completion of incomplete type, process incomplete dependents
3286 -- and always mark the full type as referenced (it is the incomplete
3287 -- type that we get for any real reference).
3289 elsif Ekind (Prev) = E_Incomplete_Type then
3290 Process_Incomplete_Dependents (N, T, Prev);
3291 Generate_Reference (Prev, Def_Id, 'c');
3292 Set_Completion_Referenced (Def_Id);
3294 -- If not private type or incomplete type completion, this is a real
3295 -- definition of a new entity, so record it.
3297 else
3298 Generate_Definition (Def_Id);
3299 end if;
3301 -- Propagate any pending access types whose finalization masters need to
3302 -- be fully initialized from the partial to the full view. Guard against
3303 -- an illegal full view that remains unanalyzed.
3305 if Is_Type (Def_Id) and then Is_Incomplete_Or_Private_Type (Prev) then
3306 Set_Pending_Access_Types (Def_Id, Pending_Access_Types (Prev));
3307 end if;
3309 if Chars (Scope (Def_Id)) = Name_System
3310 and then Chars (Def_Id) = Name_Address
3311 and then In_Predefined_Unit (N)
3312 then
3313 Set_Is_Descendant_Of_Address (Def_Id);
3314 Set_Is_Descendant_Of_Address (Base_Type (Def_Id));
3315 Set_Is_Descendant_Of_Address (Prev);
3316 end if;
3318 Set_Optimize_Alignment_Flags (Def_Id);
3319 Check_Eliminated (Def_Id);
3321 -- If the declaration is a completion and aspects are present, apply
3322 -- them to the entity for the type which is currently the partial
3323 -- view, but which is the one that will be frozen.
3325 if Has_Aspects (N) then
3327 -- In most cases the partial view is a private type, and both views
3328 -- appear in different declarative parts. In the unusual case where
3329 -- the partial view is incomplete, perform the analysis on the
3330 -- full view, to prevent freezing anomalies with the corresponding
3331 -- class-wide type, which otherwise might be frozen before the
3332 -- dispatch table is built.
3334 if Prev /= Def_Id
3335 and then Ekind (Prev) /= E_Incomplete_Type
3336 then
3337 Analyze_Aspect_Specifications (N, Prev);
3339 -- Normal case
3341 else
3342 Analyze_Aspect_Specifications (N, Def_Id);
3343 end if;
3344 end if;
3346 if Is_Derived_Type (Prev)
3347 and then Def_Id /= Prev
3348 then
3349 Check_Nonoverridable_Aspects;
3350 end if;
3351 end Analyze_Full_Type_Declaration;
3353 ----------------------------------
3354 -- Analyze_Incomplete_Type_Decl --
3355 ----------------------------------
3357 procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
3358 F : constant Boolean := Is_Pure (Current_Scope);
3359 T : Entity_Id;
3361 begin
3362 Check_SPARK_05_Restriction ("incomplete type is not allowed", N);
3364 Generate_Definition (Defining_Identifier (N));
3366 -- Process an incomplete declaration. The identifier must not have been
3367 -- declared already in the scope. However, an incomplete declaration may
3368 -- appear in the private part of a package, for a private type that has
3369 -- already been declared.
3371 -- In this case, the discriminants (if any) must match
3373 T := Find_Type_Name (N);
3375 Set_Ekind (T, E_Incomplete_Type);
3376 Init_Size_Align (T);
3377 Set_Is_First_Subtype (T, True);
3378 Set_Etype (T, T);
3380 -- Ada 2005 (AI-326): Minimum decoration to give support to tagged
3381 -- incomplete types.
3383 if Tagged_Present (N) then
3384 Set_Is_Tagged_Type (T, True);
3385 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
3386 Make_Class_Wide_Type (T);
3387 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3388 end if;
3390 Set_Stored_Constraint (T, No_Elist);
3392 if Present (Discriminant_Specifications (N)) then
3393 Push_Scope (T);
3394 Process_Discriminants (N);
3395 End_Scope;
3396 end if;
3398 -- If the type has discriminants, nontrivial subtypes may be declared
3399 -- before the full view of the type. The full views of those subtypes
3400 -- will be built after the full view of the type.
3402 Set_Private_Dependents (T, New_Elmt_List);
3403 Set_Is_Pure (T, F);
3404 end Analyze_Incomplete_Type_Decl;
3406 -----------------------------------
3407 -- Analyze_Interface_Declaration --
3408 -----------------------------------
3410 procedure Analyze_Interface_Declaration (T : Entity_Id; Def : Node_Id) is
3411 CW : constant Entity_Id := Class_Wide_Type (T);
3413 begin
3414 Set_Is_Tagged_Type (T);
3415 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
3417 Set_Is_Limited_Record (T, Limited_Present (Def)
3418 or else Task_Present (Def)
3419 or else Protected_Present (Def)
3420 or else Synchronized_Present (Def));
3422 -- Type is abstract if full declaration carries keyword, or if previous
3423 -- partial view did.
3425 Set_Is_Abstract_Type (T);
3426 Set_Is_Interface (T);
3428 -- Type is a limited interface if it includes the keyword limited, task,
3429 -- protected, or synchronized.
3431 Set_Is_Limited_Interface
3432 (T, Limited_Present (Def)
3433 or else Protected_Present (Def)
3434 or else Synchronized_Present (Def)
3435 or else Task_Present (Def));
3437 Set_Interfaces (T, New_Elmt_List);
3438 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3440 -- Complete the decoration of the class-wide entity if it was already
3441 -- built (i.e. during the creation of the limited view)
3443 if Present (CW) then
3444 Set_Is_Interface (CW);
3445 Set_Is_Limited_Interface (CW, Is_Limited_Interface (T));
3446 end if;
3448 -- Check runtime support for synchronized interfaces
3450 if (Is_Task_Interface (T)
3451 or else Is_Protected_Interface (T)
3452 or else Is_Synchronized_Interface (T))
3453 and then not RTE_Available (RE_Select_Specific_Data)
3454 then
3455 Error_Msg_CRT ("synchronized interfaces", T);
3456 end if;
3457 end Analyze_Interface_Declaration;
3459 -----------------------------
3460 -- Analyze_Itype_Reference --
3461 -----------------------------
3463 -- Nothing to do. This node is placed in the tree only for the benefit of
3464 -- back end processing, and has no effect on the semantic processing.
3466 procedure Analyze_Itype_Reference (N : Node_Id) is
3467 begin
3468 pragma Assert (Is_Itype (Itype (N)));
3469 null;
3470 end Analyze_Itype_Reference;
3472 --------------------------------
3473 -- Analyze_Number_Declaration --
3474 --------------------------------
3476 procedure Analyze_Number_Declaration (N : Node_Id) is
3477 E : constant Node_Id := Expression (N);
3478 Id : constant Entity_Id := Defining_Identifier (N);
3479 Index : Interp_Index;
3480 It : Interp;
3481 T : Entity_Id;
3483 begin
3484 Generate_Definition (Id);
3485 Enter_Name (Id);
3487 -- This is an optimization of a common case of an integer literal
3489 if Nkind (E) = N_Integer_Literal then
3490 Set_Is_Static_Expression (E, True);
3491 Set_Etype (E, Universal_Integer);
3493 Set_Etype (Id, Universal_Integer);
3494 Set_Ekind (Id, E_Named_Integer);
3495 Set_Is_Frozen (Id, True);
3496 return;
3497 end if;
3499 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3501 -- Process expression, replacing error by integer zero, to avoid
3502 -- cascaded errors or aborts further along in the processing
3504 -- Replace Error by integer zero, which seems least likely to cause
3505 -- cascaded errors.
3507 if E = Error then
3508 Rewrite (E, Make_Integer_Literal (Sloc (E), Uint_0));
3509 Set_Error_Posted (E);
3510 end if;
3512 Analyze (E);
3514 -- Verify that the expression is static and numeric. If
3515 -- the expression is overloaded, we apply the preference
3516 -- rule that favors root numeric types.
3518 if not Is_Overloaded (E) then
3519 T := Etype (E);
3520 if Has_Dynamic_Predicate_Aspect (T) then
3521 Error_Msg_N
3522 ("subtype has dynamic predicate, "
3523 & "not allowed in number declaration", N);
3524 end if;
3526 else
3527 T := Any_Type;
3529 Get_First_Interp (E, Index, It);
3530 while Present (It.Typ) loop
3531 if (Is_Integer_Type (It.Typ) or else Is_Real_Type (It.Typ))
3532 and then (Scope (Base_Type (It.Typ))) = Standard_Standard
3533 then
3534 if T = Any_Type then
3535 T := It.Typ;
3537 elsif It.Typ = Universal_Real
3538 or else
3539 It.Typ = Universal_Integer
3540 then
3541 -- Choose universal interpretation over any other
3543 T := It.Typ;
3544 exit;
3545 end if;
3546 end if;
3548 Get_Next_Interp (Index, It);
3549 end loop;
3550 end if;
3552 if Is_Integer_Type (T) then
3553 Resolve (E, T);
3554 Set_Etype (Id, Universal_Integer);
3555 Set_Ekind (Id, E_Named_Integer);
3557 elsif Is_Real_Type (T) then
3559 -- Because the real value is converted to universal_real, this is a
3560 -- legal context for a universal fixed expression.
3562 if T = Universal_Fixed then
3563 declare
3564 Loc : constant Source_Ptr := Sloc (N);
3565 Conv : constant Node_Id := Make_Type_Conversion (Loc,
3566 Subtype_Mark =>
3567 New_Occurrence_Of (Universal_Real, Loc),
3568 Expression => Relocate_Node (E));
3570 begin
3571 Rewrite (E, Conv);
3572 Analyze (E);
3573 end;
3575 elsif T = Any_Fixed then
3576 Error_Msg_N ("illegal context for mixed mode operation", E);
3578 -- Expression is of the form : universal_fixed * integer. Try to
3579 -- resolve as universal_real.
3581 T := Universal_Real;
3582 Set_Etype (E, T);
3583 end if;
3585 Resolve (E, T);
3586 Set_Etype (Id, Universal_Real);
3587 Set_Ekind (Id, E_Named_Real);
3589 else
3590 Wrong_Type (E, Any_Numeric);
3591 Resolve (E, T);
3593 Set_Etype (Id, T);
3594 Set_Ekind (Id, E_Constant);
3595 Set_Never_Set_In_Source (Id, True);
3596 Set_Is_True_Constant (Id, True);
3597 return;
3598 end if;
3600 if Nkind_In (E, N_Integer_Literal, N_Real_Literal) then
3601 Set_Etype (E, Etype (Id));
3602 end if;
3604 if not Is_OK_Static_Expression (E) then
3605 Flag_Non_Static_Expr
3606 ("non-static expression used in number declaration!", E);
3607 Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
3608 Set_Etype (E, Any_Type);
3609 end if;
3611 Analyze_Dimension (N);
3612 end Analyze_Number_Declaration;
3614 --------------------------------
3615 -- Analyze_Object_Declaration --
3616 --------------------------------
3618 -- WARNING: This routine manages Ghost regions. Return statements must be
3619 -- replaced by gotos which jump to the end of the routine and restore the
3620 -- Ghost mode.
3622 procedure Analyze_Object_Declaration (N : Node_Id) is
3623 Loc : constant Source_Ptr := Sloc (N);
3624 Id : constant Entity_Id := Defining_Identifier (N);
3625 Act_T : Entity_Id;
3626 T : Entity_Id;
3628 E : Node_Id := Expression (N);
3629 -- E is set to Expression (N) throughout this routine. When Expression
3630 -- (N) is modified, E is changed accordingly.
3632 Prev_Entity : Entity_Id := Empty;
3634 procedure Check_Dynamic_Object (Typ : Entity_Id);
3635 -- A library-level object with non-static discriminant constraints may
3636 -- require dynamic allocation. The declaration is illegal if the
3637 -- profile includes the restriction No_Implicit_Heap_Allocations.
3639 procedure Check_For_Null_Excluding_Components
3640 (Obj_Typ : Entity_Id;
3641 Obj_Decl : Node_Id);
3642 -- Verify that each null-excluding component of object declaration
3643 -- Obj_Decl carrying type Obj_Typ has explicit initialization. Emit
3644 -- a compile-time warning if this is not the case.
3646 function Count_Tasks (T : Entity_Id) return Uint;
3647 -- This function is called when a non-generic library level object of a
3648 -- task type is declared. Its function is to count the static number of
3649 -- tasks declared within the type (it is only called if Has_Task is set
3650 -- for T). As a side effect, if an array of tasks with non-static bounds
3651 -- or a variant record type is encountered, Check_Restriction is called
3652 -- indicating the count is unknown.
3654 function Delayed_Aspect_Present return Boolean;
3655 -- If the declaration has an expression that is an aggregate, and it
3656 -- has aspects that require delayed analysis, the resolution of the
3657 -- aggregate must be deferred to the freeze point of the objet. This
3658 -- special processing was created for address clauses, but it must
3659 -- also apply to Alignment. This must be done before the aspect
3660 -- specifications are analyzed because we must handle the aggregate
3661 -- before the analysis of the object declaration is complete.
3663 -- Any other relevant delayed aspects on object declarations ???
3665 --------------------------
3666 -- Check_Dynamic_Object --
3667 --------------------------
3669 procedure Check_Dynamic_Object (Typ : Entity_Id) is
3670 Comp : Entity_Id;
3671 Obj_Type : Entity_Id;
3673 begin
3674 Obj_Type := Typ;
3676 if Is_Private_Type (Obj_Type)
3677 and then Present (Full_View (Obj_Type))
3678 then
3679 Obj_Type := Full_View (Obj_Type);
3680 end if;
3682 if Known_Static_Esize (Obj_Type) then
3683 return;
3684 end if;
3686 if Restriction_Active (No_Implicit_Heap_Allocations)
3687 and then Expander_Active
3688 and then Has_Discriminants (Obj_Type)
3689 then
3690 Comp := First_Component (Obj_Type);
3691 while Present (Comp) loop
3692 if Known_Static_Esize (Etype (Comp))
3693 or else Size_Known_At_Compile_Time (Etype (Comp))
3694 then
3695 null;
3697 elsif not Discriminated_Size (Comp)
3698 and then Comes_From_Source (Comp)
3699 then
3700 Error_Msg_NE
3701 ("component& of non-static size will violate restriction "
3702 & "No_Implicit_Heap_Allocation?", N, Comp);
3704 elsif Is_Record_Type (Etype (Comp)) then
3705 Check_Dynamic_Object (Etype (Comp));
3706 end if;
3708 Next_Component (Comp);
3709 end loop;
3710 end if;
3711 end Check_Dynamic_Object;
3713 -----------------------------------------
3714 -- Check_For_Null_Excluding_Components --
3715 -----------------------------------------
3717 procedure Check_For_Null_Excluding_Components
3718 (Obj_Typ : Entity_Id;
3719 Obj_Decl : Node_Id)
3721 procedure Check_Component
3722 (Comp_Typ : Entity_Id;
3723 Comp_Decl : Node_Id := Empty;
3724 Array_Comp : Boolean := False);
3725 -- Apply a compile-time null-exclusion check on a component denoted
3726 -- by its declaration Comp_Decl and type Comp_Typ, and all of its
3727 -- subcomponents (if any).
3729 ---------------------
3730 -- Check_Component --
3731 ---------------------
3733 procedure Check_Component
3734 (Comp_Typ : Entity_Id;
3735 Comp_Decl : Node_Id := Empty;
3736 Array_Comp : Boolean := False)
3738 Comp : Entity_Id;
3739 T : Entity_Id;
3741 begin
3742 -- Do not consider internally-generated components or those that
3743 -- are already initialized.
3745 if Present (Comp_Decl)
3746 and then (not Comes_From_Source (Comp_Decl)
3747 or else Present (Expression (Comp_Decl)))
3748 then
3749 return;
3750 end if;
3752 if Is_Incomplete_Or_Private_Type (Comp_Typ)
3753 and then Present (Full_View (Comp_Typ))
3754 then
3755 T := Full_View (Comp_Typ);
3756 else
3757 T := Comp_Typ;
3758 end if;
3760 -- Verify a component of a null-excluding access type
3762 if Is_Access_Type (T)
3763 and then Can_Never_Be_Null (T)
3764 then
3765 if Comp_Decl = Obj_Decl then
3766 Null_Exclusion_Static_Checks
3767 (N => Obj_Decl,
3768 Comp => Empty,
3769 Array_Comp => Array_Comp);
3771 else
3772 Null_Exclusion_Static_Checks
3773 (N => Obj_Decl,
3774 Comp => Comp_Decl,
3775 Array_Comp => Array_Comp);
3776 end if;
3778 -- Check array components
3780 elsif Is_Array_Type (T) then
3782 -- There is no suitable component when the object is of an
3783 -- array type. However, a namable component may appear at some
3784 -- point during the recursive inspection, but not at the top
3785 -- level. At the top level just indicate array component case.
3787 if Comp_Decl = Obj_Decl then
3788 Check_Component (Component_Type (T), Array_Comp => True);
3789 else
3790 Check_Component (Component_Type (T), Comp_Decl);
3791 end if;
3793 -- Verify all components of type T
3795 -- Note: No checks are performed on types with discriminants due
3796 -- to complexities involving variants. ???
3798 elsif (Is_Concurrent_Type (T)
3799 or else Is_Incomplete_Or_Private_Type (T)
3800 or else Is_Record_Type (T))
3801 and then not Has_Discriminants (T)
3802 then
3803 Comp := First_Component (T);
3804 while Present (Comp) loop
3805 Check_Component (Etype (Comp), Parent (Comp));
3807 Comp := Next_Component (Comp);
3808 end loop;
3809 end if;
3810 end Check_Component;
3812 -- Start processing for Check_For_Null_Excluding_Components
3814 begin
3815 Check_Component (Obj_Typ, Obj_Decl);
3816 end Check_For_Null_Excluding_Components;
3818 -----------------
3819 -- Count_Tasks --
3820 -----------------
3822 function Count_Tasks (T : Entity_Id) return Uint is
3823 C : Entity_Id;
3824 X : Node_Id;
3825 V : Uint;
3827 begin
3828 if Is_Task_Type (T) then
3829 return Uint_1;
3831 elsif Is_Record_Type (T) then
3832 if Has_Discriminants (T) then
3833 Check_Restriction (Max_Tasks, N);
3834 return Uint_0;
3836 else
3837 V := Uint_0;
3838 C := First_Component (T);
3839 while Present (C) loop
3840 V := V + Count_Tasks (Etype (C));
3841 Next_Component (C);
3842 end loop;
3844 return V;
3845 end if;
3847 elsif Is_Array_Type (T) then
3848 X := First_Index (T);
3849 V := Count_Tasks (Component_Type (T));
3850 while Present (X) loop
3851 C := Etype (X);
3853 if not Is_OK_Static_Subtype (C) then
3854 Check_Restriction (Max_Tasks, N);
3855 return Uint_0;
3856 else
3857 V := V * (UI_Max (Uint_0,
3858 Expr_Value (Type_High_Bound (C)) -
3859 Expr_Value (Type_Low_Bound (C)) + Uint_1));
3860 end if;
3862 Next_Index (X);
3863 end loop;
3865 return V;
3867 else
3868 return Uint_0;
3869 end if;
3870 end Count_Tasks;
3872 ----------------------------
3873 -- Delayed_Aspect_Present --
3874 ----------------------------
3876 function Delayed_Aspect_Present return Boolean is
3877 A : Node_Id;
3878 A_Id : Aspect_Id;
3880 begin
3881 if Present (Aspect_Specifications (N)) then
3882 A := First (Aspect_Specifications (N));
3883 A_Id := Get_Aspect_Id (Chars (Identifier (A)));
3884 while Present (A) loop
3885 if A_Id = Aspect_Alignment or else A_Id = Aspect_Address then
3886 return True;
3887 end if;
3889 Next (A);
3890 end loop;
3891 end if;
3893 return False;
3894 end Delayed_Aspect_Present;
3896 -- Local variables
3898 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
3899 -- Save the Ghost mode to restore on exit
3901 Related_Id : Entity_Id;
3903 -- Start of processing for Analyze_Object_Declaration
3905 begin
3906 -- There are three kinds of implicit types generated by an
3907 -- object declaration:
3909 -- 1. Those generated by the original Object Definition
3911 -- 2. Those generated by the Expression
3913 -- 3. Those used to constrain the Object Definition with the
3914 -- expression constraints when the definition is unconstrained.
3916 -- They must be generated in this order to avoid order of elaboration
3917 -- issues. Thus the first step (after entering the name) is to analyze
3918 -- the object definition.
3920 if Constant_Present (N) then
3921 Prev_Entity := Current_Entity_In_Scope (Id);
3923 if Present (Prev_Entity)
3924 and then
3925 -- If the homograph is an implicit subprogram, it is overridden
3926 -- by the current declaration.
3928 ((Is_Overloadable (Prev_Entity)
3929 and then Is_Inherited_Operation (Prev_Entity))
3931 -- The current object is a discriminal generated for an entry
3932 -- family index. Even though the index is a constant, in this
3933 -- particular context there is no true constant redeclaration.
3934 -- Enter_Name will handle the visibility.
3936 or else
3937 (Is_Discriminal (Id)
3938 and then Ekind (Discriminal_Link (Id)) =
3939 E_Entry_Index_Parameter)
3941 -- The current object is the renaming for a generic declared
3942 -- within the instance.
3944 or else
3945 (Ekind (Prev_Entity) = E_Package
3946 and then Nkind (Parent (Prev_Entity)) =
3947 N_Package_Renaming_Declaration
3948 and then not Comes_From_Source (Prev_Entity)
3949 and then
3950 Is_Generic_Instance (Renamed_Entity (Prev_Entity)))
3952 -- The entity may be a homonym of a private component of the
3953 -- enclosing protected object, for which we create a local
3954 -- renaming declaration. The declaration is legal, even if
3955 -- useless when it just captures that component.
3957 or else
3958 (Ekind (Scope (Current_Scope)) = E_Protected_Type
3959 and then Nkind (Parent (Prev_Entity)) =
3960 N_Object_Renaming_Declaration))
3961 then
3962 Prev_Entity := Empty;
3963 end if;
3964 end if;
3966 if Present (Prev_Entity) then
3968 -- The object declaration is Ghost when it completes a deferred Ghost
3969 -- constant.
3971 Mark_And_Set_Ghost_Completion (N, Prev_Entity);
3973 Constant_Redeclaration (Id, N, T);
3975 Generate_Reference (Prev_Entity, Id, 'c');
3976 Set_Completion_Referenced (Id);
3978 if Error_Posted (N) then
3980 -- Type mismatch or illegal redeclaration; do not analyze
3981 -- expression to avoid cascaded errors.
3983 T := Find_Type_Of_Object (Object_Definition (N), N);
3984 Set_Etype (Id, T);
3985 Set_Ekind (Id, E_Variable);
3986 goto Leave;
3987 end if;
3989 -- In the normal case, enter identifier at the start to catch premature
3990 -- usage in the initialization expression.
3992 else
3993 Generate_Definition (Id);
3994 Enter_Name (Id);
3996 Mark_Coextensions (N, Object_Definition (N));
3998 T := Find_Type_Of_Object (Object_Definition (N), N);
4000 if Nkind (Object_Definition (N)) = N_Access_Definition
4001 and then Present
4002 (Access_To_Subprogram_Definition (Object_Definition (N)))
4003 and then Protected_Present
4004 (Access_To_Subprogram_Definition (Object_Definition (N)))
4005 then
4006 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
4007 end if;
4009 if Error_Posted (Id) then
4010 Set_Etype (Id, T);
4011 Set_Ekind (Id, E_Variable);
4012 goto Leave;
4013 end if;
4014 end if;
4016 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
4017 -- out some static checks.
4019 if Ada_Version >= Ada_2005 then
4021 -- In case of aggregates we must also take care of the correct
4022 -- initialization of nested aggregates bug this is done at the
4023 -- point of the analysis of the aggregate (see sem_aggr.adb) ???
4025 if Can_Never_Be_Null (T) then
4026 if Present (Expression (N))
4027 and then Nkind (Expression (N)) = N_Aggregate
4028 then
4029 null;
4031 else
4032 declare
4033 Save_Typ : constant Entity_Id := Etype (Id);
4034 begin
4035 Set_Etype (Id, T); -- Temp. decoration for static checks
4036 Null_Exclusion_Static_Checks (N);
4037 Set_Etype (Id, Save_Typ);
4038 end;
4039 end if;
4041 -- We might be dealing with an object of a composite type containing
4042 -- null-excluding components without an aggregate, so we must verify
4043 -- that such components have default initialization.
4045 else
4046 Check_For_Null_Excluding_Components (T, N);
4047 end if;
4048 end if;
4050 -- Object is marked pure if it is in a pure scope
4052 Set_Is_Pure (Id, Is_Pure (Current_Scope));
4054 -- If deferred constant, make sure context is appropriate. We detect
4055 -- a deferred constant as a constant declaration with no expression.
4056 -- A deferred constant can appear in a package body if its completion
4057 -- is by means of an interface pragma.
4059 if Constant_Present (N) and then No (E) then
4061 -- A deferred constant may appear in the declarative part of the
4062 -- following constructs:
4064 -- blocks
4065 -- entry bodies
4066 -- extended return statements
4067 -- package specs
4068 -- package bodies
4069 -- subprogram bodies
4070 -- task bodies
4072 -- When declared inside a package spec, a deferred constant must be
4073 -- completed by a full constant declaration or pragma Import. In all
4074 -- other cases, the only proper completion is pragma Import. Extended
4075 -- return statements are flagged as invalid contexts because they do
4076 -- not have a declarative part and so cannot accommodate the pragma.
4078 if Ekind (Current_Scope) = E_Return_Statement then
4079 Error_Msg_N
4080 ("invalid context for deferred constant declaration (RM 7.4)",
4082 Error_Msg_N
4083 ("\declaration requires an initialization expression",
4085 Set_Constant_Present (N, False);
4087 -- In Ada 83, deferred constant must be of private type
4089 elsif not Is_Private_Type (T) then
4090 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
4091 Error_Msg_N
4092 ("(Ada 83) deferred constant must be private type", N);
4093 end if;
4094 end if;
4096 -- If not a deferred constant, then the object declaration freezes
4097 -- its type, unless the object is of an anonymous type and has delayed
4098 -- aspects. In that case the type is frozen when the object itself is.
4100 else
4101 Check_Fully_Declared (T, N);
4103 if Has_Delayed_Aspects (Id)
4104 and then Is_Array_Type (T)
4105 and then Is_Itype (T)
4106 then
4107 Set_Has_Delayed_Freeze (T);
4108 else
4109 Freeze_Before (N, T);
4110 end if;
4111 end if;
4113 -- If the object was created by a constrained array definition, then
4114 -- set the link in both the anonymous base type and anonymous subtype
4115 -- that are built to represent the array type to point to the object.
4117 if Nkind (Object_Definition (Declaration_Node (Id))) =
4118 N_Constrained_Array_Definition
4119 then
4120 Set_Related_Array_Object (T, Id);
4121 Set_Related_Array_Object (Base_Type (T), Id);
4122 end if;
4124 -- Special checks for protected objects not at library level
4126 if Has_Protected (T) and then not Is_Library_Level_Entity (Id) then
4127 Check_Restriction (No_Local_Protected_Objects, Id);
4129 -- Protected objects with interrupt handlers must be at library level
4131 -- Ada 2005: This test is not needed (and the corresponding clause
4132 -- in the RM is removed) because accessibility checks are sufficient
4133 -- to make handlers not at the library level illegal.
4135 -- AI05-0303: The AI is in fact a binding interpretation, and thus
4136 -- applies to the '95 version of the language as well.
4138 if Is_Protected_Type (T)
4139 and then Has_Interrupt_Handler (T)
4140 and then Ada_Version < Ada_95
4141 then
4142 Error_Msg_N
4143 ("interrupt object can only be declared at library level", Id);
4144 end if;
4145 end if;
4147 -- Check for violation of No_Local_Timing_Events
4149 if Has_Timing_Event (T) and then not Is_Library_Level_Entity (Id) then
4150 Check_Restriction (No_Local_Timing_Events, Id);
4151 end if;
4153 -- The actual subtype of the object is the nominal subtype, unless
4154 -- the nominal one is unconstrained and obtained from the expression.
4156 Act_T := T;
4158 -- These checks should be performed before the initialization expression
4159 -- is considered, so that the Object_Definition node is still the same
4160 -- as in source code.
4162 -- In SPARK, the nominal subtype is always given by a subtype mark
4163 -- and must not be unconstrained. (The only exception to this is the
4164 -- acceptance of declarations of constants of type String.)
4166 if not Nkind_In (Object_Definition (N), N_Expanded_Name, N_Identifier)
4167 then
4168 Check_SPARK_05_Restriction
4169 ("subtype mark required", Object_Definition (N));
4171 elsif Is_Array_Type (T)
4172 and then not Is_Constrained (T)
4173 and then T /= Standard_String
4174 then
4175 Check_SPARK_05_Restriction
4176 ("subtype mark of constrained type expected",
4177 Object_Definition (N));
4178 end if;
4180 if Is_Library_Level_Entity (Id) then
4181 Check_Dynamic_Object (T);
4182 end if;
4184 -- There are no aliased objects in SPARK
4186 if Aliased_Present (N) then
4187 Check_SPARK_05_Restriction ("aliased object is not allowed", N);
4188 end if;
4190 -- Process initialization expression if present and not in error
4192 if Present (E) and then E /= Error then
4194 -- Generate an error in case of CPP class-wide object initialization.
4195 -- Required because otherwise the expansion of the class-wide
4196 -- assignment would try to use 'size to initialize the object
4197 -- (primitive that is not available in CPP tagged types).
4199 if Is_Class_Wide_Type (Act_T)
4200 and then
4201 (Is_CPP_Class (Root_Type (Etype (Act_T)))
4202 or else
4203 (Present (Full_View (Root_Type (Etype (Act_T))))
4204 and then
4205 Is_CPP_Class (Full_View (Root_Type (Etype (Act_T))))))
4206 then
4207 Error_Msg_N
4208 ("predefined assignment not available for 'C'P'P tagged types",
4210 end if;
4212 Mark_Coextensions (N, E);
4213 Analyze (E);
4215 -- In case of errors detected in the analysis of the expression,
4216 -- decorate it with the expected type to avoid cascaded errors
4218 if No (Etype (E)) then
4219 Set_Etype (E, T);
4220 end if;
4222 -- If an initialization expression is present, then we set the
4223 -- Is_True_Constant flag. It will be reset if this is a variable
4224 -- and it is indeed modified.
4226 Set_Is_True_Constant (Id, True);
4228 -- If we are analyzing a constant declaration, set its completion
4229 -- flag after analyzing and resolving the expression.
4231 if Constant_Present (N) then
4232 Set_Has_Completion (Id);
4233 end if;
4235 -- Set type and resolve (type may be overridden later on). Note:
4236 -- Ekind (Id) must still be E_Void at this point so that incorrect
4237 -- early usage within E is properly diagnosed.
4239 Set_Etype (Id, T);
4241 -- If the expression is an aggregate we must look ahead to detect
4242 -- the possible presence of an address clause, and defer resolution
4243 -- and expansion of the aggregate to the freeze point of the entity.
4245 -- This is not always legal because the aggregate may contain other
4246 -- references that need freezing, e.g. references to other entities
4247 -- with address clauses. In any case, when compiling with -gnatI the
4248 -- presence of the address clause must be ignored.
4250 if Comes_From_Source (N)
4251 and then Expander_Active
4252 and then Nkind (E) = N_Aggregate
4253 and then
4254 ((Present (Following_Address_Clause (N))
4255 and then not Ignore_Rep_Clauses)
4256 or else Delayed_Aspect_Present)
4257 then
4258 Set_Etype (E, T);
4260 else
4261 Resolve (E, T);
4262 end if;
4264 -- No further action needed if E is a call to an inlined function
4265 -- which returns an unconstrained type and it has been expanded into
4266 -- a procedure call. In that case N has been replaced by an object
4267 -- declaration without initializing expression and it has been
4268 -- analyzed (see Expand_Inlined_Call).
4270 if Back_End_Inlining
4271 and then Expander_Active
4272 and then Nkind (E) = N_Function_Call
4273 and then Nkind (Name (E)) in N_Has_Entity
4274 and then Is_Inlined (Entity (Name (E)))
4275 and then not Is_Constrained (Etype (E))
4276 and then Analyzed (N)
4277 and then No (Expression (N))
4278 then
4279 goto Leave;
4280 end if;
4282 -- If E is null and has been replaced by an N_Raise_Constraint_Error
4283 -- node (which was marked already-analyzed), we need to set the type
4284 -- to something other than Any_Access in order to keep gigi happy.
4286 if Etype (E) = Any_Access then
4287 Set_Etype (E, T);
4288 end if;
4290 -- If the object is an access to variable, the initialization
4291 -- expression cannot be an access to constant.
4293 if Is_Access_Type (T)
4294 and then not Is_Access_Constant (T)
4295 and then Is_Access_Type (Etype (E))
4296 and then Is_Access_Constant (Etype (E))
4297 then
4298 Error_Msg_N
4299 ("access to variable cannot be initialized with an "
4300 & "access-to-constant expression", E);
4301 end if;
4303 if not Assignment_OK (N) then
4304 Check_Initialization (T, E);
4305 end if;
4307 Check_Unset_Reference (E);
4309 -- If this is a variable, then set current value. If this is a
4310 -- declared constant of a scalar type with a static expression,
4311 -- indicate that it is always valid.
4313 if not Constant_Present (N) then
4314 if Compile_Time_Known_Value (E) then
4315 Set_Current_Value (Id, E);
4316 end if;
4318 elsif Is_Scalar_Type (T) and then Is_OK_Static_Expression (E) then
4319 Set_Is_Known_Valid (Id);
4320 end if;
4322 -- Deal with setting of null flags
4324 if Is_Access_Type (T) then
4325 if Known_Non_Null (E) then
4326 Set_Is_Known_Non_Null (Id, True);
4327 elsif Known_Null (E) and then not Can_Never_Be_Null (Id) then
4328 Set_Is_Known_Null (Id, True);
4329 end if;
4330 end if;
4332 -- Check incorrect use of dynamically tagged expressions
4334 if Is_Tagged_Type (T) then
4335 Check_Dynamically_Tagged_Expression
4336 (Expr => E,
4337 Typ => T,
4338 Related_Nod => N);
4339 end if;
4341 Apply_Scalar_Range_Check (E, T);
4342 Apply_Static_Length_Check (E, T);
4344 if Nkind (Original_Node (N)) = N_Object_Declaration
4345 and then Comes_From_Source (Original_Node (N))
4347 -- Only call test if needed
4349 and then Restriction_Check_Required (SPARK_05)
4350 and then not Is_SPARK_05_Initialization_Expr (Original_Node (E))
4351 then
4352 Check_SPARK_05_Restriction
4353 ("initialization expression is not appropriate", E);
4354 end if;
4356 -- A formal parameter of a specific tagged type whose related
4357 -- subprogram is subject to pragma Extensions_Visible with value
4358 -- "False" cannot be implicitly converted to a class-wide type by
4359 -- means of an initialization expression (SPARK RM 6.1.7(3)). Do
4360 -- not consider internally generated expressions.
4362 if Is_Class_Wide_Type (T)
4363 and then Comes_From_Source (E)
4364 and then Is_EVF_Expression (E)
4365 then
4366 Error_Msg_N
4367 ("formal parameter cannot be implicitly converted to "
4368 & "class-wide type when Extensions_Visible is False", E);
4369 end if;
4370 end if;
4372 -- If the No_Streams restriction is set, check that the type of the
4373 -- object is not, and does not contain, any subtype derived from
4374 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
4375 -- Has_Stream just for efficiency reasons. There is no point in
4376 -- spending time on a Has_Stream check if the restriction is not set.
4378 if Restriction_Check_Required (No_Streams) then
4379 if Has_Stream (T) then
4380 Check_Restriction (No_Streams, N);
4381 end if;
4382 end if;
4384 -- Deal with predicate check before we start to do major rewriting. It
4385 -- is OK to initialize and then check the initialized value, since the
4386 -- object goes out of scope if we get a predicate failure. Note that we
4387 -- do this in the analyzer and not the expander because the analyzer
4388 -- does some substantial rewriting in some cases.
4390 -- We need a predicate check if the type has predicates that are not
4391 -- ignored, and if either there is an initializing expression, or for
4392 -- default initialization when we have at least one case of an explicit
4393 -- default initial value and then this is not an internal declaration
4394 -- whose initialization comes later (as for an aggregate expansion).
4396 if not Suppress_Assignment_Checks (N)
4397 and then Present (Predicate_Function (T))
4398 and then not Predicates_Ignored (T)
4399 and then not No_Initialization (N)
4400 and then
4401 (Present (E)
4402 or else
4403 Is_Partially_Initialized_Type (T, Include_Implicit => False))
4404 then
4405 -- If the type has a static predicate and the expression is known at
4406 -- compile time, see if the expression satisfies the predicate.
4408 if Present (E) then
4409 Check_Expression_Against_Static_Predicate (E, T);
4410 end if;
4412 -- If the type is a null record and there is no explicit initial
4413 -- expression, no predicate check applies.
4415 if No (E) and then Is_Null_Record_Type (T) then
4416 null;
4418 -- Do not generate a predicate check if the initialization expression
4419 -- is a type conversion because the conversion has been subjected to
4420 -- the same check. This is a small optimization which avoid redundant
4421 -- checks.
4423 elsif Present (E) and then Nkind (E) = N_Type_Conversion then
4424 null;
4426 else
4427 Insert_After (N,
4428 Make_Predicate_Check (T, New_Occurrence_Of (Id, Loc)));
4429 end if;
4430 end if;
4432 -- Case of unconstrained type
4434 if not Is_Definite_Subtype (T) then
4436 -- In SPARK, a declaration of unconstrained type is allowed
4437 -- only for constants of type string.
4439 if Is_String_Type (T) and then not Constant_Present (N) then
4440 Check_SPARK_05_Restriction
4441 ("declaration of object of unconstrained type not allowed", N);
4442 end if;
4444 -- Nothing to do in deferred constant case
4446 if Constant_Present (N) and then No (E) then
4447 null;
4449 -- Case of no initialization present
4451 elsif No (E) then
4452 if No_Initialization (N) then
4453 null;
4455 elsif Is_Class_Wide_Type (T) then
4456 Error_Msg_N
4457 ("initialization required in class-wide declaration ", N);
4459 else
4460 Error_Msg_N
4461 ("unconstrained subtype not allowed (need initialization)",
4462 Object_Definition (N));
4464 if Is_Record_Type (T) and then Has_Discriminants (T) then
4465 Error_Msg_N
4466 ("\provide initial value or explicit discriminant values",
4467 Object_Definition (N));
4469 Error_Msg_NE
4470 ("\or give default discriminant values for type&",
4471 Object_Definition (N), T);
4473 elsif Is_Array_Type (T) then
4474 Error_Msg_N
4475 ("\provide initial value or explicit array bounds",
4476 Object_Definition (N));
4477 end if;
4478 end if;
4480 -- Case of initialization present but in error. Set initial
4481 -- expression as absent (but do not make above complaints)
4483 elsif E = Error then
4484 Set_Expression (N, Empty);
4485 E := Empty;
4487 -- Case of initialization present
4489 else
4490 -- Check restrictions in Ada 83
4492 if not Constant_Present (N) then
4494 -- Unconstrained variables not allowed in Ada 83 mode
4496 if Ada_Version = Ada_83
4497 and then Comes_From_Source (Object_Definition (N))
4498 then
4499 Error_Msg_N
4500 ("(Ada 83) unconstrained variable not allowed",
4501 Object_Definition (N));
4502 end if;
4503 end if;
4505 -- Now we constrain the variable from the initializing expression
4507 -- If the expression is an aggregate, it has been expanded into
4508 -- individual assignments. Retrieve the actual type from the
4509 -- expanded construct.
4511 if Is_Array_Type (T)
4512 and then No_Initialization (N)
4513 and then Nkind (Original_Node (E)) = N_Aggregate
4514 then
4515 Act_T := Etype (E);
4517 -- In case of class-wide interface object declarations we delay
4518 -- the generation of the equivalent record type declarations until
4519 -- its expansion because there are cases in they are not required.
4521 elsif Is_Interface (T) then
4522 null;
4524 -- In GNATprove mode, Expand_Subtype_From_Expr does nothing. Thus,
4525 -- we should prevent the generation of another Itype with the
4526 -- same name as the one already generated, or we end up with
4527 -- two identical types in GNATprove.
4529 elsif GNATprove_Mode then
4530 null;
4532 -- If the type is an unchecked union, no subtype can be built from
4533 -- the expression. Rewrite declaration as a renaming, which the
4534 -- back-end can handle properly. This is a rather unusual case,
4535 -- because most unchecked_union declarations have default values
4536 -- for discriminants and are thus not indefinite.
4538 elsif Is_Unchecked_Union (T) then
4539 if Constant_Present (N) or else Nkind (E) = N_Function_Call then
4540 Set_Ekind (Id, E_Constant);
4541 else
4542 Set_Ekind (Id, E_Variable);
4543 end if;
4545 Rewrite (N,
4546 Make_Object_Renaming_Declaration (Loc,
4547 Defining_Identifier => Id,
4548 Subtype_Mark => New_Occurrence_Of (T, Loc),
4549 Name => E));
4551 Set_Renamed_Object (Id, E);
4552 Freeze_Before (N, T);
4553 Set_Is_Frozen (Id);
4554 goto Leave;
4556 else
4557 -- Ensure that the generated subtype has a unique external name
4558 -- when the related object is public. This guarantees that the
4559 -- subtype and its bounds will not be affected by switches or
4560 -- pragmas that may offset the internal counter due to extra
4561 -- generated code.
4563 if Is_Public (Id) then
4564 Related_Id := Id;
4565 else
4566 Related_Id := Empty;
4567 end if;
4569 Expand_Subtype_From_Expr
4570 (N => N,
4571 Unc_Type => T,
4572 Subtype_Indic => Object_Definition (N),
4573 Exp => E,
4574 Related_Id => Related_Id);
4576 Act_T := Find_Type_Of_Object (Object_Definition (N), N);
4577 end if;
4579 Set_Is_Constr_Subt_For_U_Nominal (Act_T);
4581 if Aliased_Present (N) then
4582 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4583 end if;
4585 Freeze_Before (N, Act_T);
4586 Freeze_Before (N, T);
4587 end if;
4589 elsif Is_Array_Type (T)
4590 and then No_Initialization (N)
4591 and then (Nkind (Original_Node (E)) = N_Aggregate
4592 or else (Nkind (Original_Node (E)) = N_Qualified_Expression
4593 and then Nkind (Original_Node (Expression
4594 (Original_Node (E)))) = N_Aggregate))
4595 then
4596 if not Is_Entity_Name (Object_Definition (N)) then
4597 Act_T := Etype (E);
4598 Check_Compile_Time_Size (Act_T);
4600 if Aliased_Present (N) then
4601 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4602 end if;
4603 end if;
4605 -- When the given object definition and the aggregate are specified
4606 -- independently, and their lengths might differ do a length check.
4607 -- This cannot happen if the aggregate is of the form (others =>...)
4609 if not Is_Constrained (T) then
4610 null;
4612 elsif Nkind (E) = N_Raise_Constraint_Error then
4614 -- Aggregate is statically illegal. Place back in declaration
4616 Set_Expression (N, E);
4617 Set_No_Initialization (N, False);
4619 elsif T = Etype (E) then
4620 null;
4622 elsif Nkind (E) = N_Aggregate
4623 and then Present (Component_Associations (E))
4624 and then Present (Choice_List (First (Component_Associations (E))))
4625 and then
4626 Nkind (First (Choice_List (First (Component_Associations (E))))) =
4627 N_Others_Choice
4628 then
4629 null;
4631 else
4632 Apply_Length_Check (E, T);
4633 end if;
4635 -- If the type is limited unconstrained with defaulted discriminants and
4636 -- there is no expression, then the object is constrained by the
4637 -- defaults, so it is worthwhile building the corresponding subtype.
4639 elsif (Is_Limited_Record (T) or else Is_Concurrent_Type (T))
4640 and then not Is_Constrained (T)
4641 and then Has_Discriminants (T)
4642 then
4643 if No (E) then
4644 Act_T := Build_Default_Subtype (T, N);
4645 else
4646 -- Ada 2005: A limited object may be initialized by means of an
4647 -- aggregate. If the type has default discriminants it has an
4648 -- unconstrained nominal type, Its actual subtype will be obtained
4649 -- from the aggregate, and not from the default discriminants.
4651 Act_T := Etype (E);
4652 end if;
4654 Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
4656 elsif Nkind (E) = N_Function_Call
4657 and then Constant_Present (N)
4658 and then Has_Unconstrained_Elements (Etype (E))
4659 then
4660 -- The back-end has problems with constants of a discriminated type
4661 -- with defaults, if the initial value is a function call. We
4662 -- generate an intermediate temporary that will receive a reference
4663 -- to the result of the call. The initialization expression then
4664 -- becomes a dereference of that temporary.
4666 Remove_Side_Effects (E);
4668 -- If this is a constant declaration of an unconstrained type and
4669 -- the initialization is an aggregate, we can use the subtype of the
4670 -- aggregate for the declared entity because it is immutable.
4672 elsif not Is_Constrained (T)
4673 and then Has_Discriminants (T)
4674 and then Constant_Present (N)
4675 and then not Has_Unchecked_Union (T)
4676 and then Nkind (E) = N_Aggregate
4677 then
4678 Act_T := Etype (E);
4679 end if;
4681 -- Check No_Wide_Characters restriction
4683 Check_Wide_Character_Restriction (T, Object_Definition (N));
4685 -- Indicate this is not set in source. Certainly true for constants, and
4686 -- true for variables so far (will be reset for a variable if and when
4687 -- we encounter a modification in the source).
4689 Set_Never_Set_In_Source (Id);
4691 -- Now establish the proper kind and type of the object
4693 if Constant_Present (N) then
4694 Set_Ekind (Id, E_Constant);
4695 Set_Is_True_Constant (Id);
4697 else
4698 Set_Ekind (Id, E_Variable);
4700 -- A variable is set as shared passive if it appears in a shared
4701 -- passive package, and is at the outer level. This is not done for
4702 -- entities generated during expansion, because those are always
4703 -- manipulated locally.
4705 if Is_Shared_Passive (Current_Scope)
4706 and then Is_Library_Level_Entity (Id)
4707 and then Comes_From_Source (Id)
4708 then
4709 Set_Is_Shared_Passive (Id);
4710 Check_Shared_Var (Id, T, N);
4711 end if;
4713 -- Set Has_Initial_Value if initializing expression present. Note
4714 -- that if there is no initializing expression, we leave the state
4715 -- of this flag unchanged (usually it will be False, but notably in
4716 -- the case of exception choice variables, it will already be true).
4718 if Present (E) then
4719 Set_Has_Initial_Value (Id);
4720 end if;
4721 end if;
4723 -- Set the SPARK mode from the current context (may be overwritten later
4724 -- with explicit pragma).
4726 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
4727 Set_SPARK_Pragma_Inherited (Id);
4729 -- Preserve relevant elaboration-related attributes of the context which
4730 -- are no longer available or very expensive to recompute once analysis,
4731 -- resolution, and expansion are over.
4733 Mark_Elaboration_Attributes
4734 (N_Id => Id,
4735 Checks => True);
4737 -- Initialize alignment and size and capture alignment setting
4739 Init_Alignment (Id);
4740 Init_Esize (Id);
4741 Set_Optimize_Alignment_Flags (Id);
4743 -- Deal with aliased case
4745 if Aliased_Present (N) then
4746 Set_Is_Aliased (Id);
4748 -- If the object is aliased and the type is unconstrained with
4749 -- defaulted discriminants and there is no expression, then the
4750 -- object is constrained by the defaults, so it is worthwhile
4751 -- building the corresponding subtype.
4753 -- Ada 2005 (AI-363): If the aliased object is discriminated and
4754 -- unconstrained, then only establish an actual subtype if the
4755 -- nominal subtype is indefinite. In definite cases the object is
4756 -- unconstrained in Ada 2005.
4758 if No (E)
4759 and then Is_Record_Type (T)
4760 and then not Is_Constrained (T)
4761 and then Has_Discriminants (T)
4762 and then (Ada_Version < Ada_2005
4763 or else not Is_Definite_Subtype (T))
4764 then
4765 Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
4766 end if;
4767 end if;
4769 -- Now we can set the type of the object
4771 Set_Etype (Id, Act_T);
4773 -- Non-constant object is marked to be treated as volatile if type is
4774 -- volatile and we clear the Current_Value setting that may have been
4775 -- set above. Doing so for constants isn't required and might interfere
4776 -- with possible uses of the object as a static expression in contexts
4777 -- incompatible with volatility (e.g. as a case-statement alternative).
4779 if Ekind (Id) /= E_Constant and then Treat_As_Volatile (Etype (Id)) then
4780 Set_Treat_As_Volatile (Id);
4781 Set_Current_Value (Id, Empty);
4782 end if;
4784 -- Deal with controlled types
4786 if Has_Controlled_Component (Etype (Id))
4787 or else Is_Controlled (Etype (Id))
4788 then
4789 if not Is_Library_Level_Entity (Id) then
4790 Check_Restriction (No_Nested_Finalization, N);
4791 else
4792 Validate_Controlled_Object (Id);
4793 end if;
4794 end if;
4796 if Has_Task (Etype (Id)) then
4797 Check_Restriction (No_Tasking, N);
4799 -- Deal with counting max tasks
4801 -- Nothing to do if inside a generic
4803 if Inside_A_Generic then
4804 null;
4806 -- If library level entity, then count tasks
4808 elsif Is_Library_Level_Entity (Id) then
4809 Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
4811 -- If not library level entity, then indicate we don't know max
4812 -- tasks and also check task hierarchy restriction and blocking
4813 -- operation (since starting a task is definitely blocking).
4815 else
4816 Check_Restriction (Max_Tasks, N);
4817 Check_Restriction (No_Task_Hierarchy, N);
4818 Check_Potentially_Blocking_Operation (N);
4819 end if;
4821 -- A rather specialized test. If we see two tasks being declared
4822 -- of the same type in the same object declaration, and the task
4823 -- has an entry with an address clause, we know that program error
4824 -- will be raised at run time since we can't have two tasks with
4825 -- entries at the same address.
4827 if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
4828 declare
4829 E : Entity_Id;
4831 begin
4832 E := First_Entity (Etype (Id));
4833 while Present (E) loop
4834 if Ekind (E) = E_Entry
4835 and then Present (Get_Attribute_Definition_Clause
4836 (E, Attribute_Address))
4837 then
4838 Error_Msg_Warn := SPARK_Mode /= On;
4839 Error_Msg_N
4840 ("more than one task with same entry address<<", N);
4841 Error_Msg_N ("\Program_Error [<<", N);
4842 Insert_Action (N,
4843 Make_Raise_Program_Error (Loc,
4844 Reason => PE_Duplicated_Entry_Address));
4845 exit;
4846 end if;
4848 Next_Entity (E);
4849 end loop;
4850 end;
4851 end if;
4852 end if;
4854 -- Some simple constant-propagation: if the expression is a constant
4855 -- string initialized with a literal, share the literal. This avoids
4856 -- a run-time copy.
4858 if Present (E)
4859 and then Is_Entity_Name (E)
4860 and then Ekind (Entity (E)) = E_Constant
4861 and then Base_Type (Etype (E)) = Standard_String
4862 then
4863 declare
4864 Val : constant Node_Id := Constant_Value (Entity (E));
4865 begin
4866 if Present (Val) and then Nkind (Val) = N_String_Literal then
4867 Rewrite (E, New_Copy (Val));
4868 end if;
4869 end;
4870 end if;
4872 -- Another optimization: if the nominal subtype is unconstrained and
4873 -- the expression is a function call that returns an unconstrained
4874 -- type, rewrite the declaration as a renaming of the result of the
4875 -- call. The exceptions below are cases where the copy is expected,
4876 -- either by the back end (Aliased case) or by the semantics, as for
4877 -- initializing controlled types or copying tags for class-wide types.
4879 if Present (E)
4880 and then Nkind (E) = N_Explicit_Dereference
4881 and then Nkind (Original_Node (E)) = N_Function_Call
4882 and then not Is_Library_Level_Entity (Id)
4883 and then not Is_Constrained (Underlying_Type (T))
4884 and then not Is_Aliased (Id)
4885 and then not Is_Class_Wide_Type (T)
4886 and then not Is_Controlled (T)
4887 and then not Has_Controlled_Component (Base_Type (T))
4888 and then Expander_Active
4889 then
4890 Rewrite (N,
4891 Make_Object_Renaming_Declaration (Loc,
4892 Defining_Identifier => Id,
4893 Access_Definition => Empty,
4894 Subtype_Mark => New_Occurrence_Of
4895 (Base_Type (Etype (Id)), Loc),
4896 Name => E));
4898 Set_Renamed_Object (Id, E);
4900 -- Force generation of debugging information for the constant and for
4901 -- the renamed function call.
4903 Set_Debug_Info_Needed (Id);
4904 Set_Debug_Info_Needed (Entity (Prefix (E)));
4905 end if;
4907 if Present (Prev_Entity)
4908 and then Is_Frozen (Prev_Entity)
4909 and then not Error_Posted (Id)
4910 then
4911 Error_Msg_N ("full constant declaration appears too late", N);
4912 end if;
4914 Check_Eliminated (Id);
4916 -- Deal with setting In_Private_Part flag if in private part
4918 if Ekind (Scope (Id)) = E_Package
4919 and then In_Private_Part (Scope (Id))
4920 then
4921 Set_In_Private_Part (Id);
4922 end if;
4924 <<Leave>>
4925 -- Initialize the refined state of a variable here because this is a
4926 -- common destination for legal and illegal object declarations.
4928 if Ekind (Id) = E_Variable then
4929 Set_Encapsulating_State (Id, Empty);
4930 end if;
4932 if Has_Aspects (N) then
4933 Analyze_Aspect_Specifications (N, Id);
4934 end if;
4936 Analyze_Dimension (N);
4938 -- Verify whether the object declaration introduces an illegal hidden
4939 -- state within a package subject to a null abstract state.
4941 if Ekind (Id) = E_Variable then
4942 Check_No_Hidden_State (Id);
4943 end if;
4945 Restore_Ghost_Mode (Saved_GM);
4946 end Analyze_Object_Declaration;
4948 ---------------------------
4949 -- Analyze_Others_Choice --
4950 ---------------------------
4952 -- Nothing to do for the others choice node itself, the semantic analysis
4953 -- of the others choice will occur as part of the processing of the parent
4955 procedure Analyze_Others_Choice (N : Node_Id) is
4956 pragma Warnings (Off, N);
4957 begin
4958 null;
4959 end Analyze_Others_Choice;
4961 -------------------------------------------
4962 -- Analyze_Private_Extension_Declaration --
4963 -------------------------------------------
4965 procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
4966 Indic : constant Node_Id := Subtype_Indication (N);
4967 T : constant Entity_Id := Defining_Identifier (N);
4968 Iface : Entity_Id;
4969 Iface_Elmt : Elmt_Id;
4970 Parent_Base : Entity_Id;
4971 Parent_Type : Entity_Id;
4973 begin
4974 -- Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
4976 if Is_Non_Empty_List (Interface_List (N)) then
4977 declare
4978 Intf : Node_Id;
4979 T : Entity_Id;
4981 begin
4982 Intf := First (Interface_List (N));
4983 while Present (Intf) loop
4984 T := Find_Type_Of_Subtype_Indic (Intf);
4986 Diagnose_Interface (Intf, T);
4987 Next (Intf);
4988 end loop;
4989 end;
4990 end if;
4992 Generate_Definition (T);
4994 -- For other than Ada 2012, just enter the name in the current scope
4996 if Ada_Version < Ada_2012 then
4997 Enter_Name (T);
4999 -- Ada 2012 (AI05-0162): Enter the name in the current scope handling
5000 -- case of private type that completes an incomplete type.
5002 else
5003 declare
5004 Prev : Entity_Id;
5006 begin
5007 Prev := Find_Type_Name (N);
5009 pragma Assert (Prev = T
5010 or else (Ekind (Prev) = E_Incomplete_Type
5011 and then Present (Full_View (Prev))
5012 and then Full_View (Prev) = T));
5013 end;
5014 end if;
5016 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
5017 Parent_Base := Base_Type (Parent_Type);
5019 if Parent_Type = Any_Type or else Etype (Parent_Type) = Any_Type then
5020 Set_Ekind (T, Ekind (Parent_Type));
5021 Set_Etype (T, Any_Type);
5022 goto Leave;
5024 elsif not Is_Tagged_Type (Parent_Type) then
5025 Error_Msg_N
5026 ("parent of type extension must be a tagged type ", Indic);
5027 goto Leave;
5029 elsif Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
5030 Error_Msg_N ("premature derivation of incomplete type", Indic);
5031 goto Leave;
5033 elsif Is_Concurrent_Type (Parent_Type) then
5034 Error_Msg_N
5035 ("parent type of a private extension cannot be a synchronized "
5036 & "tagged type (RM 3.9.1 (3/1))", N);
5038 Set_Etype (T, Any_Type);
5039 Set_Ekind (T, E_Limited_Private_Type);
5040 Set_Private_Dependents (T, New_Elmt_List);
5041 Set_Error_Posted (T);
5042 goto Leave;
5043 end if;
5045 -- Perhaps the parent type should be changed to the class-wide type's
5046 -- specific type in this case to prevent cascading errors ???
5048 if Is_Class_Wide_Type (Parent_Type) then
5049 Error_Msg_N
5050 ("parent of type extension must not be a class-wide type", Indic);
5051 goto Leave;
5052 end if;
5054 if (not Is_Package_Or_Generic_Package (Current_Scope)
5055 and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
5056 or else In_Private_Part (Current_Scope)
5057 then
5058 Error_Msg_N ("invalid context for private extension", N);
5059 end if;
5061 -- Set common attributes
5063 Set_Is_Pure (T, Is_Pure (Current_Scope));
5064 Set_Scope (T, Current_Scope);
5065 Set_Ekind (T, E_Record_Type_With_Private);
5066 Init_Size_Align (T);
5067 Set_Default_SSO (T);
5068 Set_No_Reordering (T, No_Component_Reordering);
5070 Set_Etype (T, Parent_Base);
5071 Propagate_Concurrent_Flags (T, Parent_Base);
5073 Set_Convention (T, Convention (Parent_Type));
5074 Set_First_Rep_Item (T, First_Rep_Item (Parent_Type));
5075 Set_Is_First_Subtype (T);
5076 Make_Class_Wide_Type (T);
5078 if Unknown_Discriminants_Present (N) then
5079 Set_Discriminant_Constraint (T, No_Elist);
5080 end if;
5082 Build_Derived_Record_Type (N, Parent_Type, T);
5084 -- A private extension inherits the Default_Initial_Condition pragma
5085 -- coming from any parent type within the derivation chain.
5087 if Has_DIC (Parent_Type) then
5088 Set_Has_Inherited_DIC (T);
5089 end if;
5091 -- A private extension inherits any class-wide invariants coming from a
5092 -- parent type or an interface. Note that the invariant procedure of the
5093 -- parent type should not be inherited because the private extension may
5094 -- define invariants of its own.
5096 if Has_Inherited_Invariants (Parent_Type)
5097 or else Has_Inheritable_Invariants (Parent_Type)
5098 then
5099 Set_Has_Inherited_Invariants (T);
5101 elsif Present (Interfaces (T)) then
5102 Iface_Elmt := First_Elmt (Interfaces (T));
5103 while Present (Iface_Elmt) loop
5104 Iface := Node (Iface_Elmt);
5106 if Has_Inheritable_Invariants (Iface) then
5107 Set_Has_Inherited_Invariants (T);
5108 exit;
5109 end if;
5111 Next_Elmt (Iface_Elmt);
5112 end loop;
5113 end if;
5115 -- Ada 2005 (AI-443): Synchronized private extension or a rewritten
5116 -- synchronized formal derived type.
5118 if Ada_Version >= Ada_2005 and then Synchronized_Present (N) then
5119 Set_Is_Limited_Record (T);
5121 -- Formal derived type case
5123 if Is_Generic_Type (T) then
5125 -- The parent must be a tagged limited type or a synchronized
5126 -- interface.
5128 if (not Is_Tagged_Type (Parent_Type)
5129 or else not Is_Limited_Type (Parent_Type))
5130 and then
5131 (not Is_Interface (Parent_Type)
5132 or else not Is_Synchronized_Interface (Parent_Type))
5133 then
5134 Error_Msg_NE
5135 ("parent type of & must be tagged limited or synchronized",
5136 N, T);
5137 end if;
5139 -- The progenitors (if any) must be limited or synchronized
5140 -- interfaces.
5142 if Present (Interfaces (T)) then
5143 Iface_Elmt := First_Elmt (Interfaces (T));
5144 while Present (Iface_Elmt) loop
5145 Iface := Node (Iface_Elmt);
5147 if not Is_Limited_Interface (Iface)
5148 and then not Is_Synchronized_Interface (Iface)
5149 then
5150 Error_Msg_NE
5151 ("progenitor & must be limited or synchronized",
5152 N, Iface);
5153 end if;
5155 Next_Elmt (Iface_Elmt);
5156 end loop;
5157 end if;
5159 -- Regular derived extension, the parent must be a limited or
5160 -- synchronized interface.
5162 else
5163 if not Is_Interface (Parent_Type)
5164 or else (not Is_Limited_Interface (Parent_Type)
5165 and then not Is_Synchronized_Interface (Parent_Type))
5166 then
5167 Error_Msg_NE
5168 ("parent type of & must be limited interface", N, T);
5169 end if;
5170 end if;
5172 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
5173 -- extension with a synchronized parent must be explicitly declared
5174 -- synchronized, because the full view will be a synchronized type.
5175 -- This must be checked before the check for limited types below,
5176 -- to ensure that types declared limited are not allowed to extend
5177 -- synchronized interfaces.
5179 elsif Is_Interface (Parent_Type)
5180 and then Is_Synchronized_Interface (Parent_Type)
5181 and then not Synchronized_Present (N)
5182 then
5183 Error_Msg_NE
5184 ("private extension of& must be explicitly synchronized",
5185 N, Parent_Type);
5187 elsif Limited_Present (N) then
5188 Set_Is_Limited_Record (T);
5190 if not Is_Limited_Type (Parent_Type)
5191 and then
5192 (not Is_Interface (Parent_Type)
5193 or else not Is_Limited_Interface (Parent_Type))
5194 then
5195 Error_Msg_NE ("parent type& of limited extension must be limited",
5196 N, Parent_Type);
5197 end if;
5198 end if;
5200 -- Remember that its parent type has a private extension. Used to warn
5201 -- on public primitives of the parent type defined after its private
5202 -- extensions (see Check_Dispatching_Operation).
5204 Set_Has_Private_Extension (Parent_Type);
5206 <<Leave>>
5207 if Has_Aspects (N) then
5208 Analyze_Aspect_Specifications (N, T);
5209 end if;
5210 end Analyze_Private_Extension_Declaration;
5212 ---------------------------------
5213 -- Analyze_Subtype_Declaration --
5214 ---------------------------------
5216 procedure Analyze_Subtype_Declaration
5217 (N : Node_Id;
5218 Skip : Boolean := False)
5220 Id : constant Entity_Id := Defining_Identifier (N);
5221 R_Checks : Check_Result;
5222 T : Entity_Id;
5224 begin
5225 Generate_Definition (Id);
5226 Set_Is_Pure (Id, Is_Pure (Current_Scope));
5227 Init_Size_Align (Id);
5229 -- The following guard condition on Enter_Name is to handle cases where
5230 -- the defining identifier has already been entered into the scope but
5231 -- the declaration as a whole needs to be analyzed.
5233 -- This case in particular happens for derived enumeration types. The
5234 -- derived enumeration type is processed as an inserted enumeration type
5235 -- declaration followed by a rewritten subtype declaration. The defining
5236 -- identifier, however, is entered into the name scope very early in the
5237 -- processing of the original type declaration and therefore needs to be
5238 -- avoided here, when the created subtype declaration is analyzed. (See
5239 -- Build_Derived_Types)
5241 -- This also happens when the full view of a private type is derived
5242 -- type with constraints. In this case the entity has been introduced
5243 -- in the private declaration.
5245 -- Finally this happens in some complex cases when validity checks are
5246 -- enabled, where the same subtype declaration may be analyzed twice.
5247 -- This can happen if the subtype is created by the pre-analysis of
5248 -- an attribute tht gives the range of a loop statement, and the loop
5249 -- itself appears within an if_statement that will be rewritten during
5250 -- expansion.
5252 if Skip
5253 or else (Present (Etype (Id))
5254 and then (Is_Private_Type (Etype (Id))
5255 or else Is_Task_Type (Etype (Id))
5256 or else Is_Rewrite_Substitution (N)))
5257 then
5258 null;
5260 elsif Current_Entity (Id) = Id then
5261 null;
5263 else
5264 Enter_Name (Id);
5265 end if;
5267 T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
5269 -- Class-wide equivalent types of records with unknown discriminants
5270 -- involve the generation of an itype which serves as the private view
5271 -- of a constrained record subtype. In such cases the base type of the
5272 -- current subtype we are processing is the private itype. Use the full
5273 -- of the private itype when decorating various attributes.
5275 if Is_Itype (T)
5276 and then Is_Private_Type (T)
5277 and then Present (Full_View (T))
5278 then
5279 T := Full_View (T);
5280 end if;
5282 -- Inherit common attributes
5284 Set_Is_Volatile (Id, Is_Volatile (T));
5285 Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
5286 Set_Is_Generic_Type (Id, Is_Generic_Type (Base_Type (T)));
5287 Set_Convention (Id, Convention (T));
5289 -- If ancestor has predicates then so does the subtype, and in addition
5290 -- we must delay the freeze to properly arrange predicate inheritance.
5292 -- The Ancestor_Type test is really unpleasant, there seem to be cases
5293 -- in which T = ID, so the above tests and assignments do nothing???
5295 if Has_Predicates (T)
5296 or else (Present (Ancestor_Subtype (T))
5297 and then Has_Predicates (Ancestor_Subtype (T)))
5298 then
5299 Set_Has_Predicates (Id);
5300 Set_Has_Delayed_Freeze (Id);
5302 -- Generated subtypes inherit the predicate function from the parent
5303 -- (no aspects to examine on the generated declaration).
5305 if not Comes_From_Source (N) then
5306 Set_Ekind (Id, Ekind (T));
5308 if Present (Predicate_Function (T)) then
5309 Set_Predicate_Function (Id, Predicate_Function (T));
5311 elsif Present (Ancestor_Subtype (T))
5312 and then Has_Predicates (Ancestor_Subtype (T))
5313 and then Present (Predicate_Function (Ancestor_Subtype (T)))
5314 then
5315 Set_Predicate_Function (Id,
5316 Predicate_Function (Ancestor_Subtype (T)));
5317 end if;
5318 end if;
5319 end if;
5321 -- Subtype of Boolean cannot have a constraint in SPARK
5323 if Is_Boolean_Type (T)
5324 and then Nkind (Subtype_Indication (N)) = N_Subtype_Indication
5325 then
5326 Check_SPARK_05_Restriction
5327 ("subtype of Boolean cannot have constraint", N);
5328 end if;
5330 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5331 declare
5332 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
5333 One_Cstr : Node_Id;
5334 Low : Node_Id;
5335 High : Node_Id;
5337 begin
5338 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint then
5339 One_Cstr := First (Constraints (Cstr));
5340 while Present (One_Cstr) loop
5342 -- Index or discriminant constraint in SPARK must be a
5343 -- subtype mark.
5345 if not
5346 Nkind_In (One_Cstr, N_Identifier, N_Expanded_Name)
5347 then
5348 Check_SPARK_05_Restriction
5349 ("subtype mark required", One_Cstr);
5351 -- String subtype must have a lower bound of 1 in SPARK.
5352 -- Note that we do not need to test for the non-static case
5353 -- here, since that was already taken care of in
5354 -- Process_Range_Expr_In_Decl.
5356 elsif Base_Type (T) = Standard_String then
5357 Get_Index_Bounds (One_Cstr, Low, High);
5359 if Is_OK_Static_Expression (Low)
5360 and then Expr_Value (Low) /= 1
5361 then
5362 Check_SPARK_05_Restriction
5363 ("String subtype must have lower bound of 1", N);
5364 end if;
5365 end if;
5367 Next (One_Cstr);
5368 end loop;
5369 end if;
5370 end;
5371 end if;
5373 -- In the case where there is no constraint given in the subtype
5374 -- indication, Process_Subtype just returns the Subtype_Mark, so its
5375 -- semantic attributes must be established here.
5377 if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
5378 Set_Etype (Id, Base_Type (T));
5380 -- Subtype of unconstrained array without constraint is not allowed
5381 -- in SPARK.
5383 if Is_Array_Type (T) and then not Is_Constrained (T) then
5384 Check_SPARK_05_Restriction
5385 ("subtype of unconstrained array must have constraint", N);
5386 end if;
5388 case Ekind (T) is
5389 when Array_Kind =>
5390 Set_Ekind (Id, E_Array_Subtype);
5391 Copy_Array_Subtype_Attributes (Id, T);
5393 when Decimal_Fixed_Point_Kind =>
5394 Set_Ekind (Id, E_Decimal_Fixed_Point_Subtype);
5395 Set_Digits_Value (Id, Digits_Value (T));
5396 Set_Delta_Value (Id, Delta_Value (T));
5397 Set_Scale_Value (Id, Scale_Value (T));
5398 Set_Small_Value (Id, Small_Value (T));
5399 Set_Scalar_Range (Id, Scalar_Range (T));
5400 Set_Machine_Radix_10 (Id, Machine_Radix_10 (T));
5401 Set_Is_Constrained (Id, Is_Constrained (T));
5402 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5403 Set_RM_Size (Id, RM_Size (T));
5405 when Enumeration_Kind =>
5406 Set_Ekind (Id, E_Enumeration_Subtype);
5407 Set_First_Literal (Id, First_Literal (Base_Type (T)));
5408 Set_Scalar_Range (Id, Scalar_Range (T));
5409 Set_Is_Character_Type (Id, Is_Character_Type (T));
5410 Set_Is_Constrained (Id, Is_Constrained (T));
5411 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5412 Set_RM_Size (Id, RM_Size (T));
5413 Inherit_Predicate_Flags (Id, T);
5415 when Ordinary_Fixed_Point_Kind =>
5416 Set_Ekind (Id, E_Ordinary_Fixed_Point_Subtype);
5417 Set_Scalar_Range (Id, Scalar_Range (T));
5418 Set_Small_Value (Id, Small_Value (T));
5419 Set_Delta_Value (Id, Delta_Value (T));
5420 Set_Is_Constrained (Id, Is_Constrained (T));
5421 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5422 Set_RM_Size (Id, RM_Size (T));
5424 when Float_Kind =>
5425 Set_Ekind (Id, E_Floating_Point_Subtype);
5426 Set_Scalar_Range (Id, Scalar_Range (T));
5427 Set_Digits_Value (Id, Digits_Value (T));
5428 Set_Is_Constrained (Id, Is_Constrained (T));
5430 -- If the floating point type has dimensions, these will be
5431 -- inherited subsequently when Analyze_Dimensions is called.
5433 when Signed_Integer_Kind =>
5434 Set_Ekind (Id, E_Signed_Integer_Subtype);
5435 Set_Scalar_Range (Id, Scalar_Range (T));
5436 Set_Is_Constrained (Id, Is_Constrained (T));
5437 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5438 Set_RM_Size (Id, RM_Size (T));
5439 Inherit_Predicate_Flags (Id, T);
5441 when Modular_Integer_Kind =>
5442 Set_Ekind (Id, E_Modular_Integer_Subtype);
5443 Set_Scalar_Range (Id, Scalar_Range (T));
5444 Set_Is_Constrained (Id, Is_Constrained (T));
5445 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5446 Set_RM_Size (Id, RM_Size (T));
5447 Inherit_Predicate_Flags (Id, T);
5449 when Class_Wide_Kind =>
5450 Set_Ekind (Id, E_Class_Wide_Subtype);
5451 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5452 Set_Cloned_Subtype (Id, T);
5453 Set_Is_Tagged_Type (Id, True);
5454 Set_Has_Unknown_Discriminants
5455 (Id, True);
5456 Set_No_Tagged_Streams_Pragma
5457 (Id, No_Tagged_Streams_Pragma (T));
5459 if Ekind (T) = E_Class_Wide_Subtype then
5460 Set_Equivalent_Type (Id, Equivalent_Type (T));
5461 end if;
5463 when E_Record_Subtype
5464 | E_Record_Type
5466 Set_Ekind (Id, E_Record_Subtype);
5468 if Ekind (T) = E_Record_Subtype
5469 and then Present (Cloned_Subtype (T))
5470 then
5471 Set_Cloned_Subtype (Id, Cloned_Subtype (T));
5472 else
5473 Set_Cloned_Subtype (Id, T);
5474 end if;
5476 Set_First_Entity (Id, First_Entity (T));
5477 Set_Last_Entity (Id, Last_Entity (T));
5478 Set_Has_Discriminants (Id, Has_Discriminants (T));
5479 Set_Is_Constrained (Id, Is_Constrained (T));
5480 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5481 Set_Has_Implicit_Dereference
5482 (Id, Has_Implicit_Dereference (T));
5483 Set_Has_Unknown_Discriminants
5484 (Id, Has_Unknown_Discriminants (T));
5486 if Has_Discriminants (T) then
5487 Set_Discriminant_Constraint
5488 (Id, Discriminant_Constraint (T));
5489 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5491 elsif Has_Unknown_Discriminants (Id) then
5492 Set_Discriminant_Constraint (Id, No_Elist);
5493 end if;
5495 if Is_Tagged_Type (T) then
5496 Set_Is_Tagged_Type (Id, True);
5497 Set_No_Tagged_Streams_Pragma
5498 (Id, No_Tagged_Streams_Pragma (T));
5499 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5500 Set_Direct_Primitive_Operations
5501 (Id, Direct_Primitive_Operations (T));
5502 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5504 if Is_Interface (T) then
5505 Set_Is_Interface (Id);
5506 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
5507 end if;
5508 end if;
5510 when Private_Kind =>
5511 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
5512 Set_Has_Discriminants (Id, Has_Discriminants (T));
5513 Set_Is_Constrained (Id, Is_Constrained (T));
5514 Set_First_Entity (Id, First_Entity (T));
5515 Set_Last_Entity (Id, Last_Entity (T));
5516 Set_Private_Dependents (Id, New_Elmt_List);
5517 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5518 Set_Has_Implicit_Dereference
5519 (Id, Has_Implicit_Dereference (T));
5520 Set_Has_Unknown_Discriminants
5521 (Id, Has_Unknown_Discriminants (T));
5522 Set_Known_To_Have_Preelab_Init
5523 (Id, Known_To_Have_Preelab_Init (T));
5525 if Is_Tagged_Type (T) then
5526 Set_Is_Tagged_Type (Id);
5527 Set_No_Tagged_Streams_Pragma (Id,
5528 No_Tagged_Streams_Pragma (T));
5529 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5530 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5531 Set_Direct_Primitive_Operations (Id,
5532 Direct_Primitive_Operations (T));
5533 end if;
5535 -- In general the attributes of the subtype of a private type
5536 -- are the attributes of the partial view of parent. However,
5537 -- the full view may be a discriminated type, and the subtype
5538 -- must share the discriminant constraint to generate correct
5539 -- calls to initialization procedures.
5541 if Has_Discriminants (T) then
5542 Set_Discriminant_Constraint
5543 (Id, Discriminant_Constraint (T));
5544 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5546 elsif Present (Full_View (T))
5547 and then Has_Discriminants (Full_View (T))
5548 then
5549 Set_Discriminant_Constraint
5550 (Id, Discriminant_Constraint (Full_View (T)));
5551 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5553 -- This would seem semantically correct, but apparently
5554 -- generates spurious errors about missing components ???
5556 -- Set_Has_Discriminants (Id);
5557 end if;
5559 Prepare_Private_Subtype_Completion (Id, N);
5561 -- If this is the subtype of a constrained private type with
5562 -- discriminants that has got a full view and we also have
5563 -- built a completion just above, show that the completion
5564 -- is a clone of the full view to the back-end.
5566 if Has_Discriminants (T)
5567 and then not Has_Unknown_Discriminants (T)
5568 and then not Is_Empty_Elmt_List (Discriminant_Constraint (T))
5569 and then Present (Full_View (T))
5570 and then Present (Full_View (Id))
5571 then
5572 Set_Cloned_Subtype (Full_View (Id), Full_View (T));
5573 end if;
5575 when Access_Kind =>
5576 Set_Ekind (Id, E_Access_Subtype);
5577 Set_Is_Constrained (Id, Is_Constrained (T));
5578 Set_Is_Access_Constant
5579 (Id, Is_Access_Constant (T));
5580 Set_Directly_Designated_Type
5581 (Id, Designated_Type (T));
5582 Set_Can_Never_Be_Null (Id, Can_Never_Be_Null (T));
5584 -- A Pure library_item must not contain the declaration of a
5585 -- named access type, except within a subprogram, generic
5586 -- subprogram, task unit, or protected unit, or if it has
5587 -- a specified Storage_Size of zero (RM05-10.2.1(15.4-15.5)).
5589 if Comes_From_Source (Id)
5590 and then In_Pure_Unit
5591 and then not In_Subprogram_Task_Protected_Unit
5592 and then not No_Pool_Assigned (Id)
5593 then
5594 Error_Msg_N
5595 ("named access types not allowed in pure unit", N);
5596 end if;
5598 when Concurrent_Kind =>
5599 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
5600 Set_Corresponding_Record_Type (Id,
5601 Corresponding_Record_Type (T));
5602 Set_First_Entity (Id, First_Entity (T));
5603 Set_First_Private_Entity (Id, First_Private_Entity (T));
5604 Set_Has_Discriminants (Id, Has_Discriminants (T));
5605 Set_Is_Constrained (Id, Is_Constrained (T));
5606 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5607 Set_Last_Entity (Id, Last_Entity (T));
5609 if Is_Tagged_Type (T) then
5610 Set_No_Tagged_Streams_Pragma
5611 (Id, No_Tagged_Streams_Pragma (T));
5612 end if;
5614 if Has_Discriminants (T) then
5615 Set_Discriminant_Constraint
5616 (Id, Discriminant_Constraint (T));
5617 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5618 end if;
5620 when Incomplete_Kind =>
5621 if Ada_Version >= Ada_2005 then
5623 -- In Ada 2005 an incomplete type can be explicitly tagged:
5624 -- propagate indication. Note that we also have to include
5625 -- subtypes for Ada 2012 extended use of incomplete types.
5627 Set_Ekind (Id, E_Incomplete_Subtype);
5628 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5629 Set_Private_Dependents (Id, New_Elmt_List);
5631 if Is_Tagged_Type (Id) then
5632 Set_No_Tagged_Streams_Pragma
5633 (Id, No_Tagged_Streams_Pragma (T));
5634 Set_Direct_Primitive_Operations (Id, New_Elmt_List);
5635 end if;
5637 -- Ada 2005 (AI-412): Decorate an incomplete subtype of an
5638 -- incomplete type visible through a limited with clause.
5640 if From_Limited_With (T)
5641 and then Present (Non_Limited_View (T))
5642 then
5643 Set_From_Limited_With (Id);
5644 Set_Non_Limited_View (Id, Non_Limited_View (T));
5646 -- Ada 2005 (AI-412): Add the regular incomplete subtype
5647 -- to the private dependents of the original incomplete
5648 -- type for future transformation.
5650 else
5651 Append_Elmt (Id, Private_Dependents (T));
5652 end if;
5654 -- If the subtype name denotes an incomplete type an error
5655 -- was already reported by Process_Subtype.
5657 else
5658 Set_Etype (Id, Any_Type);
5659 end if;
5661 when others =>
5662 raise Program_Error;
5663 end case;
5664 end if;
5666 if Etype (Id) = Any_Type then
5667 goto Leave;
5668 end if;
5670 -- Some common processing on all types
5672 Set_Size_Info (Id, T);
5673 Set_First_Rep_Item (Id, First_Rep_Item (T));
5675 -- If the parent type is a generic actual, so is the subtype. This may
5676 -- happen in a nested instance. Why Comes_From_Source test???
5678 if not Comes_From_Source (N) then
5679 Set_Is_Generic_Actual_Type (Id, Is_Generic_Actual_Type (T));
5680 end if;
5682 -- If this is a subtype declaration for an actual in an instance,
5683 -- inherit static and dynamic predicates if any.
5685 -- If declaration has no aspect specifications, inherit predicate
5686 -- info as well. Unclear how to handle the case of both specified
5687 -- and inherited predicates ??? Other inherited aspects, such as
5688 -- invariants, should be OK, but the combination with later pragmas
5689 -- may also require special merging.
5691 if Has_Predicates (T)
5692 and then Present (Predicate_Function (T))
5693 and then
5694 ((In_Instance and then not Comes_From_Source (N))
5695 or else No (Aspect_Specifications (N)))
5696 then
5697 Set_Subprograms_For_Type (Id, Subprograms_For_Type (T));
5699 if Has_Static_Predicate (T) then
5700 Set_Has_Static_Predicate (Id);
5701 Set_Static_Discrete_Predicate (Id, Static_Discrete_Predicate (T));
5702 end if;
5703 end if;
5705 -- Remaining processing depends on characteristics of base type
5707 T := Etype (Id);
5709 Set_Is_Immediately_Visible (Id, True);
5710 Set_Depends_On_Private (Id, Has_Private_Component (T));
5711 Set_Is_Descendant_Of_Address (Id, Is_Descendant_Of_Address (T));
5713 if Is_Interface (T) then
5714 Set_Is_Interface (Id);
5715 end if;
5717 if Present (Generic_Parent_Type (N))
5718 and then
5719 (Nkind (Parent (Generic_Parent_Type (N))) /=
5720 N_Formal_Type_Declaration
5721 or else Nkind (Formal_Type_Definition
5722 (Parent (Generic_Parent_Type (N)))) /=
5723 N_Formal_Private_Type_Definition)
5724 then
5725 if Is_Tagged_Type (Id) then
5727 -- If this is a generic actual subtype for a synchronized type,
5728 -- the primitive operations are those of the corresponding record
5729 -- for which there is a separate subtype declaration.
5731 if Is_Concurrent_Type (Id) then
5732 null;
5733 elsif Is_Class_Wide_Type (Id) then
5734 Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
5735 else
5736 Derive_Subprograms (Generic_Parent_Type (N), Id, T);
5737 end if;
5739 elsif Scope (Etype (Id)) /= Standard_Standard then
5740 Derive_Subprograms (Generic_Parent_Type (N), Id);
5741 end if;
5742 end if;
5744 if Is_Private_Type (T) and then Present (Full_View (T)) then
5745 Conditional_Delay (Id, Full_View (T));
5747 -- The subtypes of components or subcomponents of protected types
5748 -- do not need freeze nodes, which would otherwise appear in the
5749 -- wrong scope (before the freeze node for the protected type). The
5750 -- proper subtypes are those of the subcomponents of the corresponding
5751 -- record.
5753 elsif Ekind (Scope (Id)) /= E_Protected_Type
5754 and then Present (Scope (Scope (Id))) -- error defense
5755 and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
5756 then
5757 Conditional_Delay (Id, T);
5758 end if;
5760 -- If we have a subtype of an incomplete type whose full type is a
5761 -- derived numeric type, we need to have a freeze node for the subtype.
5762 -- Otherwise gigi will complain while computing the (static) bounds of
5763 -- the subtype.
5765 if Is_Itype (T)
5766 and then Is_Elementary_Type (Id)
5767 and then Etype (Id) /= Id
5768 then
5769 declare
5770 Partial : constant Entity_Id :=
5771 Incomplete_Or_Partial_View (First_Subtype (Id));
5772 begin
5773 if Present (Partial)
5774 and then Ekind (Partial) = E_Incomplete_Type
5775 then
5776 Set_Has_Delayed_Freeze (Id);
5777 end if;
5778 end;
5779 end if;
5781 -- Check that Constraint_Error is raised for a scalar subtype indication
5782 -- when the lower or upper bound of a non-null range lies outside the
5783 -- range of the type mark.
5785 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5786 if Is_Scalar_Type (Etype (Id))
5787 and then Scalar_Range (Id) /=
5788 Scalar_Range
5789 (Etype (Subtype_Mark (Subtype_Indication (N))))
5790 then
5791 Apply_Range_Check
5792 (Scalar_Range (Id),
5793 Etype (Subtype_Mark (Subtype_Indication (N))));
5795 -- In the array case, check compatibility for each index
5797 elsif Is_Array_Type (Etype (Id)) and then Present (First_Index (Id))
5798 then
5799 -- This really should be a subprogram that finds the indications
5800 -- to check???
5802 declare
5803 Subt_Index : Node_Id := First_Index (Id);
5804 Target_Index : Node_Id :=
5805 First_Index (Etype
5806 (Subtype_Mark (Subtype_Indication (N))));
5807 Has_Dyn_Chk : Boolean := Has_Dynamic_Range_Check (N);
5809 begin
5810 while Present (Subt_Index) loop
5811 if ((Nkind (Subt_Index) = N_Identifier
5812 and then Ekind (Entity (Subt_Index)) in Scalar_Kind)
5813 or else Nkind (Subt_Index) = N_Subtype_Indication)
5814 and then
5815 Nkind (Scalar_Range (Etype (Subt_Index))) = N_Range
5816 then
5817 declare
5818 Target_Typ : constant Entity_Id :=
5819 Etype (Target_Index);
5820 begin
5821 R_Checks :=
5822 Get_Range_Checks
5823 (Scalar_Range (Etype (Subt_Index)),
5824 Target_Typ,
5825 Etype (Subt_Index),
5826 Defining_Identifier (N));
5828 -- Reset Has_Dynamic_Range_Check on the subtype to
5829 -- prevent elision of the index check due to a dynamic
5830 -- check generated for a preceding index (needed since
5831 -- Insert_Range_Checks tries to avoid generating
5832 -- redundant checks on a given declaration).
5834 Set_Has_Dynamic_Range_Check (N, False);
5836 Insert_Range_Checks
5837 (R_Checks,
5839 Target_Typ,
5840 Sloc (Defining_Identifier (N)));
5842 -- Record whether this index involved a dynamic check
5844 Has_Dyn_Chk :=
5845 Has_Dyn_Chk or else Has_Dynamic_Range_Check (N);
5846 end;
5847 end if;
5849 Next_Index (Subt_Index);
5850 Next_Index (Target_Index);
5851 end loop;
5853 -- Finally, mark whether the subtype involves dynamic checks
5855 Set_Has_Dynamic_Range_Check (N, Has_Dyn_Chk);
5856 end;
5857 end if;
5858 end if;
5860 Set_Optimize_Alignment_Flags (Id);
5861 Check_Eliminated (Id);
5863 <<Leave>>
5864 if Has_Aspects (N) then
5865 Analyze_Aspect_Specifications (N, Id);
5866 end if;
5868 Analyze_Dimension (N);
5870 -- Check No_Dynamic_Sized_Objects restriction, which disallows subtype
5871 -- indications on composite types where the constraints are dynamic.
5872 -- Note that object declarations and aggregates generate implicit
5873 -- subtype declarations, which this covers. One special case is that the
5874 -- implicitly generated "=" for discriminated types includes an
5875 -- offending subtype declaration, which is harmless, so we ignore it
5876 -- here.
5878 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5879 declare
5880 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
5881 begin
5882 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint
5883 and then not (Is_Internal (Id)
5884 and then Is_TSS (Scope (Id),
5885 TSS_Composite_Equality))
5886 and then not Within_Init_Proc
5887 and then not All_Composite_Constraints_Static (Cstr)
5888 then
5889 Check_Restriction (No_Dynamic_Sized_Objects, Cstr);
5890 end if;
5891 end;
5892 end if;
5893 end Analyze_Subtype_Declaration;
5895 --------------------------------
5896 -- Analyze_Subtype_Indication --
5897 --------------------------------
5899 procedure Analyze_Subtype_Indication (N : Node_Id) is
5900 T : constant Entity_Id := Subtype_Mark (N);
5901 R : constant Node_Id := Range_Expression (Constraint (N));
5903 begin
5904 Analyze (T);
5906 if R /= Error then
5907 Analyze (R);
5908 Set_Etype (N, Etype (R));
5909 Resolve (R, Entity (T));
5910 else
5911 Set_Error_Posted (R);
5912 Set_Error_Posted (T);
5913 end if;
5914 end Analyze_Subtype_Indication;
5916 --------------------------
5917 -- Analyze_Variant_Part --
5918 --------------------------
5920 procedure Analyze_Variant_Part (N : Node_Id) is
5921 Discr_Name : Node_Id;
5922 Discr_Type : Entity_Id;
5924 procedure Process_Variant (A : Node_Id);
5925 -- Analyze declarations for a single variant
5927 package Analyze_Variant_Choices is
5928 new Generic_Analyze_Choices (Process_Variant);
5929 use Analyze_Variant_Choices;
5931 ---------------------
5932 -- Process_Variant --
5933 ---------------------
5935 procedure Process_Variant (A : Node_Id) is
5936 CL : constant Node_Id := Component_List (A);
5937 begin
5938 if not Null_Present (CL) then
5939 Analyze_Declarations (Component_Items (CL));
5941 if Present (Variant_Part (CL)) then
5942 Analyze (Variant_Part (CL));
5943 end if;
5944 end if;
5945 end Process_Variant;
5947 -- Start of processing for Analyze_Variant_Part
5949 begin
5950 Discr_Name := Name (N);
5951 Analyze (Discr_Name);
5953 -- If Discr_Name bad, get out (prevent cascaded errors)
5955 if Etype (Discr_Name) = Any_Type then
5956 return;
5957 end if;
5959 -- Check invalid discriminant in variant part
5961 if Ekind (Entity (Discr_Name)) /= E_Discriminant then
5962 Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
5963 end if;
5965 Discr_Type := Etype (Entity (Discr_Name));
5967 if not Is_Discrete_Type (Discr_Type) then
5968 Error_Msg_N
5969 ("discriminant in a variant part must be of a discrete type",
5970 Name (N));
5971 return;
5972 end if;
5974 -- Now analyze the choices, which also analyzes the declarations that
5975 -- are associated with each choice.
5977 Analyze_Choices (Variants (N), Discr_Type);
5979 -- Note: we used to instantiate and call Check_Choices here to check
5980 -- that the choices covered the discriminant, but it's too early to do
5981 -- that because of statically predicated subtypes, whose analysis may
5982 -- be deferred to their freeze point which may be as late as the freeze
5983 -- point of the containing record. So this call is now to be found in
5984 -- Freeze_Record_Declaration.
5986 end Analyze_Variant_Part;
5988 ----------------------------
5989 -- Array_Type_Declaration --
5990 ----------------------------
5992 procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
5993 Component_Def : constant Node_Id := Component_Definition (Def);
5994 Component_Typ : constant Node_Id := Subtype_Indication (Component_Def);
5995 P : constant Node_Id := Parent (Def);
5996 Element_Type : Entity_Id;
5997 Implicit_Base : Entity_Id;
5998 Index : Node_Id;
5999 Nb_Index : Nat;
6000 Priv : Entity_Id;
6001 Related_Id : Entity_Id := Empty;
6003 begin
6004 if Nkind (Def) = N_Constrained_Array_Definition then
6005 Index := First (Discrete_Subtype_Definitions (Def));
6006 else
6007 Index := First (Subtype_Marks (Def));
6008 end if;
6010 -- Find proper names for the implicit types which may be public. In case
6011 -- of anonymous arrays we use the name of the first object of that type
6012 -- as prefix.
6014 if No (T) then
6015 Related_Id := Defining_Identifier (P);
6016 else
6017 Related_Id := T;
6018 end if;
6020 Nb_Index := 1;
6021 while Present (Index) loop
6022 Analyze (Index);
6024 -- Test for odd case of trying to index a type by the type itself
6026 if Is_Entity_Name (Index) and then Entity (Index) = T then
6027 Error_Msg_N ("type& cannot be indexed by itself", Index);
6028 Set_Entity (Index, Standard_Boolean);
6029 Set_Etype (Index, Standard_Boolean);
6030 end if;
6032 -- Check SPARK restriction requiring a subtype mark
6034 if not Nkind_In (Index, N_Identifier, N_Expanded_Name) then
6035 Check_SPARK_05_Restriction ("subtype mark required", Index);
6036 end if;
6038 -- Add a subtype declaration for each index of private array type
6039 -- declaration whose etype is also private. For example:
6041 -- package Pkg is
6042 -- type Index is private;
6043 -- private
6044 -- type Table is array (Index) of ...
6045 -- end;
6047 -- This is currently required by the expander for the internally
6048 -- generated equality subprogram of records with variant parts in
6049 -- which the etype of some component is such private type.
6051 if Ekind (Current_Scope) = E_Package
6052 and then In_Private_Part (Current_Scope)
6053 and then Has_Private_Declaration (Etype (Index))
6054 then
6055 declare
6056 Loc : constant Source_Ptr := Sloc (Def);
6057 Decl : Entity_Id;
6058 New_E : Entity_Id;
6060 begin
6061 New_E := Make_Temporary (Loc, 'T');
6062 Set_Is_Internal (New_E);
6064 Decl :=
6065 Make_Subtype_Declaration (Loc,
6066 Defining_Identifier => New_E,
6067 Subtype_Indication =>
6068 New_Occurrence_Of (Etype (Index), Loc));
6070 Insert_Before (Parent (Def), Decl);
6071 Analyze (Decl);
6072 Set_Etype (Index, New_E);
6074 -- If the index is a range or a subtype indication it carries
6075 -- no entity. Example:
6077 -- package Pkg is
6078 -- type T is private;
6079 -- private
6080 -- type T is new Natural;
6081 -- Table : array (T(1) .. T(10)) of Boolean;
6082 -- end Pkg;
6084 -- Otherwise the type of the reference is its entity.
6086 if Is_Entity_Name (Index) then
6087 Set_Entity (Index, New_E);
6088 end if;
6089 end;
6090 end if;
6092 Make_Index (Index, P, Related_Id, Nb_Index);
6094 -- Check error of subtype with predicate for index type
6096 Bad_Predicated_Subtype_Use
6097 ("subtype& has predicate, not allowed as index subtype",
6098 Index, Etype (Index));
6100 -- Move to next index
6102 Next_Index (Index);
6103 Nb_Index := Nb_Index + 1;
6104 end loop;
6106 -- Process subtype indication if one is present
6108 if Present (Component_Typ) then
6109 Element_Type := Process_Subtype (Component_Typ, P, Related_Id, 'C');
6111 Set_Etype (Component_Typ, Element_Type);
6113 if not Nkind_In (Component_Typ, N_Identifier, N_Expanded_Name) then
6114 Check_SPARK_05_Restriction
6115 ("subtype mark required", Component_Typ);
6116 end if;
6118 -- Ada 2005 (AI-230): Access Definition case
6120 else pragma Assert (Present (Access_Definition (Component_Def)));
6122 -- Indicate that the anonymous access type is created by the
6123 -- array type declaration.
6125 Element_Type := Access_Definition
6126 (Related_Nod => P,
6127 N => Access_Definition (Component_Def));
6128 Set_Is_Local_Anonymous_Access (Element_Type);
6130 -- Propagate the parent. This field is needed if we have to generate
6131 -- the master_id associated with an anonymous access to task type
6132 -- component (see Expand_N_Full_Type_Declaration.Build_Master)
6134 Set_Parent (Element_Type, Parent (T));
6136 -- Ada 2005 (AI-230): In case of components that are anonymous access
6137 -- types the level of accessibility depends on the enclosing type
6138 -- declaration
6140 Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
6142 -- Ada 2005 (AI-254)
6144 declare
6145 CD : constant Node_Id :=
6146 Access_To_Subprogram_Definition
6147 (Access_Definition (Component_Def));
6148 begin
6149 if Present (CD) and then Protected_Present (CD) then
6150 Element_Type :=
6151 Replace_Anonymous_Access_To_Protected_Subprogram (Def);
6152 end if;
6153 end;
6154 end if;
6156 -- Constrained array case
6158 if No (T) then
6159 T := Create_Itype (E_Void, P, Related_Id, 'T');
6160 end if;
6162 if Nkind (Def) = N_Constrained_Array_Definition then
6164 -- Establish Implicit_Base as unconstrained base type
6166 Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
6168 Set_Etype (Implicit_Base, Implicit_Base);
6169 Set_Scope (Implicit_Base, Current_Scope);
6170 Set_Has_Delayed_Freeze (Implicit_Base);
6171 Set_Default_SSO (Implicit_Base);
6173 -- The constrained array type is a subtype of the unconstrained one
6175 Set_Ekind (T, E_Array_Subtype);
6176 Init_Size_Align (T);
6177 Set_Etype (T, Implicit_Base);
6178 Set_Scope (T, Current_Scope);
6179 Set_Is_Constrained (T);
6180 Set_First_Index (T,
6181 First (Discrete_Subtype_Definitions (Def)));
6182 Set_Has_Delayed_Freeze (T);
6184 -- Complete setup of implicit base type
6186 Set_Component_Size (Implicit_Base, Uint_0);
6187 Set_Component_Type (Implicit_Base, Element_Type);
6188 Set_Finalize_Storage_Only
6189 (Implicit_Base,
6190 Finalize_Storage_Only (Element_Type));
6191 Set_First_Index (Implicit_Base, First_Index (T));
6192 Set_Has_Controlled_Component
6193 (Implicit_Base,
6194 Has_Controlled_Component (Element_Type)
6195 or else Is_Controlled (Element_Type));
6196 Set_Packed_Array_Impl_Type
6197 (Implicit_Base, Empty);
6199 Propagate_Concurrent_Flags (Implicit_Base, Element_Type);
6201 -- Unconstrained array case
6203 else
6204 Set_Ekind (T, E_Array_Type);
6205 Init_Size_Align (T);
6206 Set_Etype (T, T);
6207 Set_Scope (T, Current_Scope);
6208 Set_Component_Size (T, Uint_0);
6209 Set_Is_Constrained (T, False);
6210 Set_First_Index (T, First (Subtype_Marks (Def)));
6211 Set_Has_Delayed_Freeze (T, True);
6212 Propagate_Concurrent_Flags (T, Element_Type);
6213 Set_Has_Controlled_Component (T, Has_Controlled_Component
6214 (Element_Type)
6215 or else
6216 Is_Controlled (Element_Type));
6217 Set_Finalize_Storage_Only (T, Finalize_Storage_Only
6218 (Element_Type));
6219 Set_Default_SSO (T);
6220 end if;
6222 -- Common attributes for both cases
6224 Set_Component_Type (Base_Type (T), Element_Type);
6225 Set_Packed_Array_Impl_Type (T, Empty);
6227 if Aliased_Present (Component_Definition (Def)) then
6228 Check_SPARK_05_Restriction
6229 ("aliased is not allowed", Component_Definition (Def));
6230 Set_Has_Aliased_Components (Etype (T));
6231 end if;
6233 -- Ada 2005 (AI-231): Propagate the null-excluding attribute to the
6234 -- array type to ensure that objects of this type are initialized.
6236 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (Element_Type) then
6237 Set_Can_Never_Be_Null (T);
6239 if Null_Exclusion_Present (Component_Definition (Def))
6241 -- No need to check itypes because in their case this check was
6242 -- done at their point of creation
6244 and then not Is_Itype (Element_Type)
6245 then
6246 Error_Msg_N
6247 ("`NOT NULL` not allowed (null already excluded)",
6248 Subtype_Indication (Component_Definition (Def)));
6249 end if;
6250 end if;
6252 Priv := Private_Component (Element_Type);
6254 if Present (Priv) then
6256 -- Check for circular definitions
6258 if Priv = Any_Type then
6259 Set_Component_Type (Etype (T), Any_Type);
6261 -- There is a gap in the visibility of operations on the composite
6262 -- type only if the component type is defined in a different scope.
6264 elsif Scope (Priv) = Current_Scope then
6265 null;
6267 elsif Is_Limited_Type (Priv) then
6268 Set_Is_Limited_Composite (Etype (T));
6269 Set_Is_Limited_Composite (T);
6270 else
6271 Set_Is_Private_Composite (Etype (T));
6272 Set_Is_Private_Composite (T);
6273 end if;
6274 end if;
6276 -- A syntax error in the declaration itself may lead to an empty index
6277 -- list, in which case do a minimal patch.
6279 if No (First_Index (T)) then
6280 Error_Msg_N ("missing index definition in array type declaration", T);
6282 declare
6283 Indexes : constant List_Id :=
6284 New_List (New_Occurrence_Of (Any_Id, Sloc (T)));
6285 begin
6286 Set_Discrete_Subtype_Definitions (Def, Indexes);
6287 Set_First_Index (T, First (Indexes));
6288 return;
6289 end;
6290 end if;
6292 -- Create a concatenation operator for the new type. Internal array
6293 -- types created for packed entities do not need such, they are
6294 -- compatible with the user-defined type.
6296 if Number_Dimensions (T) = 1
6297 and then not Is_Packed_Array_Impl_Type (T)
6298 then
6299 New_Concatenation_Op (T);
6300 end if;
6302 -- In the case of an unconstrained array the parser has already verified
6303 -- that all the indexes are unconstrained but we still need to make sure
6304 -- that the element type is constrained.
6306 if not Is_Definite_Subtype (Element_Type) then
6307 Error_Msg_N
6308 ("unconstrained element type in array declaration",
6309 Subtype_Indication (Component_Def));
6311 elsif Is_Abstract_Type (Element_Type) then
6312 Error_Msg_N
6313 ("the type of a component cannot be abstract",
6314 Subtype_Indication (Component_Def));
6315 end if;
6317 -- There may be an invariant declared for the component type, but
6318 -- the construction of the component invariant checking procedure
6319 -- takes place during expansion.
6320 end Array_Type_Declaration;
6322 ------------------------------------------------------
6323 -- Replace_Anonymous_Access_To_Protected_Subprogram --
6324 ------------------------------------------------------
6326 function Replace_Anonymous_Access_To_Protected_Subprogram
6327 (N : Node_Id) return Entity_Id
6329 Loc : constant Source_Ptr := Sloc (N);
6331 Curr_Scope : constant Scope_Stack_Entry :=
6332 Scope_Stack.Table (Scope_Stack.Last);
6334 Anon : constant Entity_Id := Make_Temporary (Loc, 'S');
6336 Acc : Node_Id;
6337 -- Access definition in declaration
6339 Comp : Node_Id;
6340 -- Object definition or formal definition with an access definition
6342 Decl : Node_Id;
6343 -- Declaration of anonymous access to subprogram type
6345 Spec : Node_Id;
6346 -- Original specification in access to subprogram
6348 P : Node_Id;
6350 begin
6351 Set_Is_Internal (Anon);
6353 case Nkind (N) is
6354 when N_Constrained_Array_Definition
6355 | N_Component_Declaration
6356 | N_Unconstrained_Array_Definition
6358 Comp := Component_Definition (N);
6359 Acc := Access_Definition (Comp);
6361 when N_Discriminant_Specification =>
6362 Comp := Discriminant_Type (N);
6363 Acc := Comp;
6365 when N_Parameter_Specification =>
6366 Comp := Parameter_Type (N);
6367 Acc := Comp;
6369 when N_Access_Function_Definition =>
6370 Comp := Result_Definition (N);
6371 Acc := Comp;
6373 when N_Object_Declaration =>
6374 Comp := Object_Definition (N);
6375 Acc := Comp;
6377 when N_Function_Specification =>
6378 Comp := Result_Definition (N);
6379 Acc := Comp;
6381 when others =>
6382 raise Program_Error;
6383 end case;
6385 Spec := Access_To_Subprogram_Definition (Acc);
6387 Decl :=
6388 Make_Full_Type_Declaration (Loc,
6389 Defining_Identifier => Anon,
6390 Type_Definition => Copy_Separate_Tree (Spec));
6392 Mark_Rewrite_Insertion (Decl);
6394 -- In ASIS mode, analyze the profile on the original node, because
6395 -- the separate copy does not provide enough links to recover the
6396 -- original tree. Analysis is limited to type annotations, within
6397 -- a temporary scope that serves as an anonymous subprogram to collect
6398 -- otherwise useless temporaries and itypes.
6400 if ASIS_Mode then
6401 declare
6402 Typ : constant Entity_Id := Make_Temporary (Loc, 'S');
6404 begin
6405 if Nkind (Spec) = N_Access_Function_Definition then
6406 Set_Ekind (Typ, E_Function);
6407 else
6408 Set_Ekind (Typ, E_Procedure);
6409 end if;
6411 Set_Parent (Typ, N);
6412 Set_Scope (Typ, Current_Scope);
6413 Push_Scope (Typ);
6415 -- Nothing to do if procedure is parameterless
6417 if Present (Parameter_Specifications (Spec)) then
6418 Process_Formals (Parameter_Specifications (Spec), Spec);
6419 end if;
6421 if Nkind (Spec) = N_Access_Function_Definition then
6422 declare
6423 Def : constant Node_Id := Result_Definition (Spec);
6425 begin
6426 -- The result might itself be an anonymous access type, so
6427 -- have to recurse.
6429 if Nkind (Def) = N_Access_Definition then
6430 if Present (Access_To_Subprogram_Definition (Def)) then
6431 Set_Etype
6432 (Def,
6433 Replace_Anonymous_Access_To_Protected_Subprogram
6434 (Spec));
6435 else
6436 Find_Type (Subtype_Mark (Def));
6437 end if;
6439 else
6440 Find_Type (Def);
6441 end if;
6442 end;
6443 end if;
6445 End_Scope;
6446 end;
6447 end if;
6449 -- Insert the new declaration in the nearest enclosing scope. If the
6450 -- parent is a body and N is its return type, the declaration belongs
6451 -- in the enclosing scope. Likewise if N is the type of a parameter.
6453 P := Parent (N);
6455 if Nkind (N) = N_Function_Specification
6456 and then Nkind (P) = N_Subprogram_Body
6457 then
6458 P := Parent (P);
6459 elsif Nkind (N) = N_Parameter_Specification
6460 and then Nkind (P) in N_Subprogram_Specification
6461 and then Nkind (Parent (P)) = N_Subprogram_Body
6462 then
6463 P := Parent (Parent (P));
6464 end if;
6466 while Present (P) and then not Has_Declarations (P) loop
6467 P := Parent (P);
6468 end loop;
6470 pragma Assert (Present (P));
6472 if Nkind (P) = N_Package_Specification then
6473 Prepend (Decl, Visible_Declarations (P));
6474 else
6475 Prepend (Decl, Declarations (P));
6476 end if;
6478 -- Replace the anonymous type with an occurrence of the new declaration.
6479 -- In all cases the rewritten node does not have the null-exclusion
6480 -- attribute because (if present) it was already inherited by the
6481 -- anonymous entity (Anon). Thus, in case of components we do not
6482 -- inherit this attribute.
6484 if Nkind (N) = N_Parameter_Specification then
6485 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6486 Set_Etype (Defining_Identifier (N), Anon);
6487 Set_Null_Exclusion_Present (N, False);
6489 elsif Nkind (N) = N_Object_Declaration then
6490 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6491 Set_Etype (Defining_Identifier (N), Anon);
6493 elsif Nkind (N) = N_Access_Function_Definition then
6494 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6496 elsif Nkind (N) = N_Function_Specification then
6497 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6498 Set_Etype (Defining_Unit_Name (N), Anon);
6500 else
6501 Rewrite (Comp,
6502 Make_Component_Definition (Loc,
6503 Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
6504 end if;
6506 Mark_Rewrite_Insertion (Comp);
6508 if Nkind_In (N, N_Object_Declaration, N_Access_Function_Definition)
6509 or else (Nkind (Parent (N)) = N_Full_Type_Declaration
6510 and then not Is_Type (Current_Scope))
6511 then
6513 -- Declaration can be analyzed in the current scope.
6515 Analyze (Decl);
6517 else
6518 -- Temporarily remove the current scope (record or subprogram) from
6519 -- the stack to add the new declarations to the enclosing scope.
6520 -- The anonymous entity is an Itype with the proper attributes.
6522 Scope_Stack.Decrement_Last;
6523 Analyze (Decl);
6524 Set_Is_Itype (Anon);
6525 Set_Associated_Node_For_Itype (Anon, N);
6526 Scope_Stack.Append (Curr_Scope);
6527 end if;
6529 Set_Ekind (Anon, E_Anonymous_Access_Protected_Subprogram_Type);
6530 Set_Can_Use_Internal_Rep (Anon, not Always_Compatible_Rep_On_Target);
6531 return Anon;
6532 end Replace_Anonymous_Access_To_Protected_Subprogram;
6534 -------------------------------
6535 -- Build_Derived_Access_Type --
6536 -------------------------------
6538 procedure Build_Derived_Access_Type
6539 (N : Node_Id;
6540 Parent_Type : Entity_Id;
6541 Derived_Type : Entity_Id)
6543 S : constant Node_Id := Subtype_Indication (Type_Definition (N));
6545 Desig_Type : Entity_Id;
6546 Discr : Entity_Id;
6547 Discr_Con_Elist : Elist_Id;
6548 Discr_Con_El : Elmt_Id;
6549 Subt : Entity_Id;
6551 begin
6552 -- Set the designated type so it is available in case this is an access
6553 -- to a self-referential type, e.g. a standard list type with a next
6554 -- pointer. Will be reset after subtype is built.
6556 Set_Directly_Designated_Type
6557 (Derived_Type, Designated_Type (Parent_Type));
6559 Subt := Process_Subtype (S, N);
6561 if Nkind (S) /= N_Subtype_Indication
6562 and then Subt /= Base_Type (Subt)
6563 then
6564 Set_Ekind (Derived_Type, E_Access_Subtype);
6565 end if;
6567 if Ekind (Derived_Type) = E_Access_Subtype then
6568 declare
6569 Pbase : constant Entity_Id := Base_Type (Parent_Type);
6570 Ibase : constant Entity_Id :=
6571 Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
6572 Svg_Chars : constant Name_Id := Chars (Ibase);
6573 Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
6575 begin
6576 Copy_Node (Pbase, Ibase);
6578 -- Restore Itype status after Copy_Node
6580 Set_Is_Itype (Ibase);
6581 Set_Associated_Node_For_Itype (Ibase, N);
6583 Set_Chars (Ibase, Svg_Chars);
6584 Set_Next_Entity (Ibase, Svg_Next_E);
6585 Set_Sloc (Ibase, Sloc (Derived_Type));
6586 Set_Scope (Ibase, Scope (Derived_Type));
6587 Set_Freeze_Node (Ibase, Empty);
6588 Set_Is_Frozen (Ibase, False);
6589 Set_Comes_From_Source (Ibase, False);
6590 Set_Is_First_Subtype (Ibase, False);
6592 Set_Etype (Ibase, Pbase);
6593 Set_Etype (Derived_Type, Ibase);
6594 end;
6595 end if;
6597 Set_Directly_Designated_Type
6598 (Derived_Type, Designated_Type (Subt));
6600 Set_Is_Constrained (Derived_Type, Is_Constrained (Subt));
6601 Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
6602 Set_Size_Info (Derived_Type, Parent_Type);
6603 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
6604 Set_Depends_On_Private (Derived_Type,
6605 Has_Private_Component (Derived_Type));
6606 Conditional_Delay (Derived_Type, Subt);
6608 -- Ada 2005 (AI-231): Set the null-exclusion attribute, and verify
6609 -- that it is not redundant.
6611 if Null_Exclusion_Present (Type_Definition (N)) then
6612 Set_Can_Never_Be_Null (Derived_Type);
6614 elsif Can_Never_Be_Null (Parent_Type) then
6615 Set_Can_Never_Be_Null (Derived_Type);
6616 end if;
6618 -- Note: we do not copy the Storage_Size_Variable, since we always go to
6619 -- the root type for this information.
6621 -- Apply range checks to discriminants for derived record case
6622 -- ??? THIS CODE SHOULD NOT BE HERE REALLY.
6624 Desig_Type := Designated_Type (Derived_Type);
6626 if Is_Composite_Type (Desig_Type)
6627 and then (not Is_Array_Type (Desig_Type))
6628 and then Has_Discriminants (Desig_Type)
6629 and then Base_Type (Desig_Type) /= Desig_Type
6630 then
6631 Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
6632 Discr_Con_El := First_Elmt (Discr_Con_Elist);
6634 Discr := First_Discriminant (Base_Type (Desig_Type));
6635 while Present (Discr_Con_El) loop
6636 Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
6637 Next_Elmt (Discr_Con_El);
6638 Next_Discriminant (Discr);
6639 end loop;
6640 end if;
6641 end Build_Derived_Access_Type;
6643 ------------------------------
6644 -- Build_Derived_Array_Type --
6645 ------------------------------
6647 procedure Build_Derived_Array_Type
6648 (N : Node_Id;
6649 Parent_Type : Entity_Id;
6650 Derived_Type : Entity_Id)
6652 Loc : constant Source_Ptr := Sloc (N);
6653 Tdef : constant Node_Id := Type_Definition (N);
6654 Indic : constant Node_Id := Subtype_Indication (Tdef);
6655 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
6656 Implicit_Base : Entity_Id;
6657 New_Indic : Node_Id;
6659 procedure Make_Implicit_Base;
6660 -- If the parent subtype is constrained, the derived type is a subtype
6661 -- of an implicit base type derived from the parent base.
6663 ------------------------
6664 -- Make_Implicit_Base --
6665 ------------------------
6667 procedure Make_Implicit_Base is
6668 begin
6669 Implicit_Base :=
6670 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
6672 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
6673 Set_Etype (Implicit_Base, Parent_Base);
6675 Copy_Array_Subtype_Attributes (Implicit_Base, Parent_Base);
6676 Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
6678 Set_Has_Delayed_Freeze (Implicit_Base, True);
6679 end Make_Implicit_Base;
6681 -- Start of processing for Build_Derived_Array_Type
6683 begin
6684 if not Is_Constrained (Parent_Type) then
6685 if Nkind (Indic) /= N_Subtype_Indication then
6686 Set_Ekind (Derived_Type, E_Array_Type);
6688 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6689 Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
6691 Set_Has_Delayed_Freeze (Derived_Type, True);
6693 else
6694 Make_Implicit_Base;
6695 Set_Etype (Derived_Type, Implicit_Base);
6697 New_Indic :=
6698 Make_Subtype_Declaration (Loc,
6699 Defining_Identifier => Derived_Type,
6700 Subtype_Indication =>
6701 Make_Subtype_Indication (Loc,
6702 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
6703 Constraint => Constraint (Indic)));
6705 Rewrite (N, New_Indic);
6706 Analyze (N);
6707 end if;
6709 else
6710 if Nkind (Indic) /= N_Subtype_Indication then
6711 Make_Implicit_Base;
6713 Set_Ekind (Derived_Type, Ekind (Parent_Type));
6714 Set_Etype (Derived_Type, Implicit_Base);
6715 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6717 else
6718 Error_Msg_N ("illegal constraint on constrained type", Indic);
6719 end if;
6720 end if;
6722 -- If parent type is not a derived type itself, and is declared in
6723 -- closed scope (e.g. a subprogram), then we must explicitly introduce
6724 -- the new type's concatenation operator since Derive_Subprograms
6725 -- will not inherit the parent's operator. If the parent type is
6726 -- unconstrained, the operator is of the unconstrained base type.
6728 if Number_Dimensions (Parent_Type) = 1
6729 and then not Is_Limited_Type (Parent_Type)
6730 and then not Is_Derived_Type (Parent_Type)
6731 and then not Is_Package_Or_Generic_Package
6732 (Scope (Base_Type (Parent_Type)))
6733 then
6734 if not Is_Constrained (Parent_Type)
6735 and then Is_Constrained (Derived_Type)
6736 then
6737 New_Concatenation_Op (Implicit_Base);
6738 else
6739 New_Concatenation_Op (Derived_Type);
6740 end if;
6741 end if;
6742 end Build_Derived_Array_Type;
6744 -----------------------------------
6745 -- Build_Derived_Concurrent_Type --
6746 -----------------------------------
6748 procedure Build_Derived_Concurrent_Type
6749 (N : Node_Id;
6750 Parent_Type : Entity_Id;
6751 Derived_Type : Entity_Id)
6753 Loc : constant Source_Ptr := Sloc (N);
6755 Corr_Record : constant Entity_Id := Make_Temporary (Loc, 'C');
6756 Corr_Decl : Node_Id;
6757 Corr_Decl_Needed : Boolean;
6758 -- If the derived type has fewer discriminants than its parent, the
6759 -- corresponding record is also a derived type, in order to account for
6760 -- the bound discriminants. We create a full type declaration for it in
6761 -- this case.
6763 Constraint_Present : constant Boolean :=
6764 Nkind (Subtype_Indication (Type_Definition (N))) =
6765 N_Subtype_Indication;
6767 D_Constraint : Node_Id;
6768 New_Constraint : Elist_Id;
6769 Old_Disc : Entity_Id;
6770 New_Disc : Entity_Id;
6771 New_N : Node_Id;
6773 begin
6774 Set_Stored_Constraint (Derived_Type, No_Elist);
6775 Corr_Decl_Needed := False;
6776 Old_Disc := Empty;
6778 if Present (Discriminant_Specifications (N))
6779 and then Constraint_Present
6780 then
6781 Old_Disc := First_Discriminant (Parent_Type);
6782 New_Disc := First (Discriminant_Specifications (N));
6783 while Present (New_Disc) and then Present (Old_Disc) loop
6784 Next_Discriminant (Old_Disc);
6785 Next (New_Disc);
6786 end loop;
6787 end if;
6789 if Present (Old_Disc) and then Expander_Active then
6791 -- The new type has fewer discriminants, so we need to create a new
6792 -- corresponding record, which is derived from the corresponding
6793 -- record of the parent, and has a stored constraint that captures
6794 -- the values of the discriminant constraints. The corresponding
6795 -- record is needed only if expander is active and code generation is
6796 -- enabled.
6798 -- The type declaration for the derived corresponding record has the
6799 -- same discriminant part and constraints as the current declaration.
6800 -- Copy the unanalyzed tree to build declaration.
6802 Corr_Decl_Needed := True;
6803 New_N := Copy_Separate_Tree (N);
6805 Corr_Decl :=
6806 Make_Full_Type_Declaration (Loc,
6807 Defining_Identifier => Corr_Record,
6808 Discriminant_Specifications =>
6809 Discriminant_Specifications (New_N),
6810 Type_Definition =>
6811 Make_Derived_Type_Definition (Loc,
6812 Subtype_Indication =>
6813 Make_Subtype_Indication (Loc,
6814 Subtype_Mark =>
6815 New_Occurrence_Of
6816 (Corresponding_Record_Type (Parent_Type), Loc),
6817 Constraint =>
6818 Constraint
6819 (Subtype_Indication (Type_Definition (New_N))))));
6820 end if;
6822 -- Copy Storage_Size and Relative_Deadline variables if task case
6824 if Is_Task_Type (Parent_Type) then
6825 Set_Storage_Size_Variable (Derived_Type,
6826 Storage_Size_Variable (Parent_Type));
6827 Set_Relative_Deadline_Variable (Derived_Type,
6828 Relative_Deadline_Variable (Parent_Type));
6829 end if;
6831 if Present (Discriminant_Specifications (N)) then
6832 Push_Scope (Derived_Type);
6833 Check_Or_Process_Discriminants (N, Derived_Type);
6835 if Constraint_Present then
6836 New_Constraint :=
6837 Expand_To_Stored_Constraint
6838 (Parent_Type,
6839 Build_Discriminant_Constraints
6840 (Parent_Type,
6841 Subtype_Indication (Type_Definition (N)), True));
6842 end if;
6844 End_Scope;
6846 elsif Constraint_Present then
6848 -- Build constrained subtype, copying the constraint, and derive
6849 -- from it to create a derived constrained type.
6851 declare
6852 Loc : constant Source_Ptr := Sloc (N);
6853 Anon : constant Entity_Id :=
6854 Make_Defining_Identifier (Loc,
6855 Chars => New_External_Name (Chars (Derived_Type), 'T'));
6856 Decl : Node_Id;
6858 begin
6859 Decl :=
6860 Make_Subtype_Declaration (Loc,
6861 Defining_Identifier => Anon,
6862 Subtype_Indication =>
6863 New_Copy_Tree (Subtype_Indication (Type_Definition (N))));
6864 Insert_Before (N, Decl);
6865 Analyze (Decl);
6867 Rewrite (Subtype_Indication (Type_Definition (N)),
6868 New_Occurrence_Of (Anon, Loc));
6869 Set_Analyzed (Derived_Type, False);
6870 Analyze (N);
6871 return;
6872 end;
6873 end if;
6875 -- By default, operations and private data are inherited from parent.
6876 -- However, in the presence of bound discriminants, a new corresponding
6877 -- record will be created, see below.
6879 Set_Has_Discriminants
6880 (Derived_Type, Has_Discriminants (Parent_Type));
6881 Set_Corresponding_Record_Type
6882 (Derived_Type, Corresponding_Record_Type (Parent_Type));
6884 -- Is_Constrained is set according the parent subtype, but is set to
6885 -- False if the derived type is declared with new discriminants.
6887 Set_Is_Constrained
6888 (Derived_Type,
6889 (Is_Constrained (Parent_Type) or else Constraint_Present)
6890 and then not Present (Discriminant_Specifications (N)));
6892 if Constraint_Present then
6893 if not Has_Discriminants (Parent_Type) then
6894 Error_Msg_N ("untagged parent must have discriminants", N);
6896 elsif Present (Discriminant_Specifications (N)) then
6898 -- Verify that new discriminants are used to constrain old ones
6900 D_Constraint :=
6901 First
6902 (Constraints
6903 (Constraint (Subtype_Indication (Type_Definition (N)))));
6905 Old_Disc := First_Discriminant (Parent_Type);
6907 while Present (D_Constraint) loop
6908 if Nkind (D_Constraint) /= N_Discriminant_Association then
6910 -- Positional constraint. If it is a reference to a new
6911 -- discriminant, it constrains the corresponding old one.
6913 if Nkind (D_Constraint) = N_Identifier then
6914 New_Disc := First_Discriminant (Derived_Type);
6915 while Present (New_Disc) loop
6916 exit when Chars (New_Disc) = Chars (D_Constraint);
6917 Next_Discriminant (New_Disc);
6918 end loop;
6920 if Present (New_Disc) then
6921 Set_Corresponding_Discriminant (New_Disc, Old_Disc);
6922 end if;
6923 end if;
6925 Next_Discriminant (Old_Disc);
6927 -- if this is a named constraint, search by name for the old
6928 -- discriminants constrained by the new one.
6930 elsif Nkind (Expression (D_Constraint)) = N_Identifier then
6932 -- Find new discriminant with that name
6934 New_Disc := First_Discriminant (Derived_Type);
6935 while Present (New_Disc) loop
6936 exit when
6937 Chars (New_Disc) = Chars (Expression (D_Constraint));
6938 Next_Discriminant (New_Disc);
6939 end loop;
6941 if Present (New_Disc) then
6943 -- Verify that new discriminant renames some discriminant
6944 -- of the parent type, and associate the new discriminant
6945 -- with one or more old ones that it renames.
6947 declare
6948 Selector : Node_Id;
6950 begin
6951 Selector := First (Selector_Names (D_Constraint));
6952 while Present (Selector) loop
6953 Old_Disc := First_Discriminant (Parent_Type);
6954 while Present (Old_Disc) loop
6955 exit when Chars (Old_Disc) = Chars (Selector);
6956 Next_Discriminant (Old_Disc);
6957 end loop;
6959 if Present (Old_Disc) then
6960 Set_Corresponding_Discriminant
6961 (New_Disc, Old_Disc);
6962 end if;
6964 Next (Selector);
6965 end loop;
6966 end;
6967 end if;
6968 end if;
6970 Next (D_Constraint);
6971 end loop;
6973 New_Disc := First_Discriminant (Derived_Type);
6974 while Present (New_Disc) loop
6975 if No (Corresponding_Discriminant (New_Disc)) then
6976 Error_Msg_NE
6977 ("new discriminant& must constrain old one", N, New_Disc);
6979 elsif not
6980 Subtypes_Statically_Compatible
6981 (Etype (New_Disc),
6982 Etype (Corresponding_Discriminant (New_Disc)))
6983 then
6984 Error_Msg_NE
6985 ("& not statically compatible with parent discriminant",
6986 N, New_Disc);
6987 end if;
6989 Next_Discriminant (New_Disc);
6990 end loop;
6991 end if;
6993 elsif Present (Discriminant_Specifications (N)) then
6994 Error_Msg_N
6995 ("missing discriminant constraint in untagged derivation", N);
6996 end if;
6998 -- The entity chain of the derived type includes the new discriminants
6999 -- but shares operations with the parent.
7001 if Present (Discriminant_Specifications (N)) then
7002 Old_Disc := First_Discriminant (Parent_Type);
7003 while Present (Old_Disc) loop
7004 if No (Next_Entity (Old_Disc))
7005 or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
7006 then
7007 Set_Next_Entity
7008 (Last_Entity (Derived_Type), Next_Entity (Old_Disc));
7009 exit;
7010 end if;
7012 Next_Discriminant (Old_Disc);
7013 end loop;
7015 else
7016 Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
7017 if Has_Discriminants (Parent_Type) then
7018 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7019 Set_Discriminant_Constraint (
7020 Derived_Type, Discriminant_Constraint (Parent_Type));
7021 end if;
7022 end if;
7024 Set_Last_Entity (Derived_Type, Last_Entity (Parent_Type));
7026 Set_Has_Completion (Derived_Type);
7028 if Corr_Decl_Needed then
7029 Set_Stored_Constraint (Derived_Type, New_Constraint);
7030 Insert_After (N, Corr_Decl);
7031 Analyze (Corr_Decl);
7032 Set_Corresponding_Record_Type (Derived_Type, Corr_Record);
7033 end if;
7034 end Build_Derived_Concurrent_Type;
7036 ------------------------------------
7037 -- Build_Derived_Enumeration_Type --
7038 ------------------------------------
7040 procedure Build_Derived_Enumeration_Type
7041 (N : Node_Id;
7042 Parent_Type : Entity_Id;
7043 Derived_Type : Entity_Id)
7045 Loc : constant Source_Ptr := Sloc (N);
7046 Def : constant Node_Id := Type_Definition (N);
7047 Indic : constant Node_Id := Subtype_Indication (Def);
7048 Implicit_Base : Entity_Id;
7049 Literal : Entity_Id;
7050 New_Lit : Entity_Id;
7051 Literals_List : List_Id;
7052 Type_Decl : Node_Id;
7053 Hi, Lo : Node_Id;
7054 Rang_Expr : Node_Id;
7056 begin
7057 -- Since types Standard.Character and Standard.[Wide_]Wide_Character do
7058 -- not have explicit literals lists we need to process types derived
7059 -- from them specially. This is handled by Derived_Standard_Character.
7060 -- If the parent type is a generic type, there are no literals either,
7061 -- and we construct the same skeletal representation as for the generic
7062 -- parent type.
7064 if Is_Standard_Character_Type (Parent_Type) then
7065 Derived_Standard_Character (N, Parent_Type, Derived_Type);
7067 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
7068 declare
7069 Lo : Node_Id;
7070 Hi : Node_Id;
7072 begin
7073 if Nkind (Indic) /= N_Subtype_Indication then
7074 Lo :=
7075 Make_Attribute_Reference (Loc,
7076 Attribute_Name => Name_First,
7077 Prefix => New_Occurrence_Of (Derived_Type, Loc));
7078 Set_Etype (Lo, Derived_Type);
7080 Hi :=
7081 Make_Attribute_Reference (Loc,
7082 Attribute_Name => Name_Last,
7083 Prefix => New_Occurrence_Of (Derived_Type, Loc));
7084 Set_Etype (Hi, Derived_Type);
7086 Set_Scalar_Range (Derived_Type,
7087 Make_Range (Loc,
7088 Low_Bound => Lo,
7089 High_Bound => Hi));
7090 else
7092 -- Analyze subtype indication and verify compatibility
7093 -- with parent type.
7095 if Base_Type (Process_Subtype (Indic, N)) /=
7096 Base_Type (Parent_Type)
7097 then
7098 Error_Msg_N
7099 ("illegal constraint for formal discrete type", N);
7100 end if;
7101 end if;
7102 end;
7104 else
7105 -- If a constraint is present, analyze the bounds to catch
7106 -- premature usage of the derived literals.
7108 if Nkind (Indic) = N_Subtype_Indication
7109 and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
7110 then
7111 Analyze (Low_Bound (Range_Expression (Constraint (Indic))));
7112 Analyze (High_Bound (Range_Expression (Constraint (Indic))));
7113 end if;
7115 -- Introduce an implicit base type for the derived type even if there
7116 -- is no constraint attached to it, since this seems closer to the
7117 -- Ada semantics. Build a full type declaration tree for the derived
7118 -- type using the implicit base type as the defining identifier. The
7119 -- build a subtype declaration tree which applies the constraint (if
7120 -- any) have it replace the derived type declaration.
7122 Literal := First_Literal (Parent_Type);
7123 Literals_List := New_List;
7124 while Present (Literal)
7125 and then Ekind (Literal) = E_Enumeration_Literal
7126 loop
7127 -- Literals of the derived type have the same representation as
7128 -- those of the parent type, but this representation can be
7129 -- overridden by an explicit representation clause. Indicate
7130 -- that there is no explicit representation given yet. These
7131 -- derived literals are implicit operations of the new type,
7132 -- and can be overridden by explicit ones.
7134 if Nkind (Literal) = N_Defining_Character_Literal then
7135 New_Lit :=
7136 Make_Defining_Character_Literal (Loc, Chars (Literal));
7137 else
7138 New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
7139 end if;
7141 Set_Ekind (New_Lit, E_Enumeration_Literal);
7142 Set_Enumeration_Pos (New_Lit, Enumeration_Pos (Literal));
7143 Set_Enumeration_Rep (New_Lit, Enumeration_Rep (Literal));
7144 Set_Enumeration_Rep_Expr (New_Lit, Empty);
7145 Set_Alias (New_Lit, Literal);
7146 Set_Is_Known_Valid (New_Lit, True);
7148 Append (New_Lit, Literals_List);
7149 Next_Literal (Literal);
7150 end loop;
7152 Implicit_Base :=
7153 Make_Defining_Identifier (Sloc (Derived_Type),
7154 Chars => New_External_Name (Chars (Derived_Type), 'B'));
7156 -- Indicate the proper nature of the derived type. This must be done
7157 -- before analysis of the literals, to recognize cases when a literal
7158 -- may be hidden by a previous explicit function definition (cf.
7159 -- c83031a).
7161 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
7162 Set_Etype (Derived_Type, Implicit_Base);
7164 Type_Decl :=
7165 Make_Full_Type_Declaration (Loc,
7166 Defining_Identifier => Implicit_Base,
7167 Discriminant_Specifications => No_List,
7168 Type_Definition =>
7169 Make_Enumeration_Type_Definition (Loc, Literals_List));
7171 Mark_Rewrite_Insertion (Type_Decl);
7172 Insert_Before (N, Type_Decl);
7173 Analyze (Type_Decl);
7175 -- The anonymous base now has a full declaration, but this base
7176 -- is not a first subtype.
7178 Set_Is_First_Subtype (Implicit_Base, False);
7180 -- After the implicit base is analyzed its Etype needs to be changed
7181 -- to reflect the fact that it is derived from the parent type which
7182 -- was ignored during analysis. We also set the size at this point.
7184 Set_Etype (Implicit_Base, Parent_Type);
7186 Set_Size_Info (Implicit_Base, Parent_Type);
7187 Set_RM_Size (Implicit_Base, RM_Size (Parent_Type));
7188 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
7190 -- Copy other flags from parent type
7192 Set_Has_Non_Standard_Rep
7193 (Implicit_Base, Has_Non_Standard_Rep
7194 (Parent_Type));
7195 Set_Has_Pragma_Ordered
7196 (Implicit_Base, Has_Pragma_Ordered
7197 (Parent_Type));
7198 Set_Has_Delayed_Freeze (Implicit_Base);
7200 -- Process the subtype indication including a validation check on the
7201 -- constraint, if any. If a constraint is given, its bounds must be
7202 -- implicitly converted to the new type.
7204 if Nkind (Indic) = N_Subtype_Indication then
7205 declare
7206 R : constant Node_Id :=
7207 Range_Expression (Constraint (Indic));
7209 begin
7210 if Nkind (R) = N_Range then
7211 Hi := Build_Scalar_Bound
7212 (High_Bound (R), Parent_Type, Implicit_Base);
7213 Lo := Build_Scalar_Bound
7214 (Low_Bound (R), Parent_Type, Implicit_Base);
7216 else
7217 -- Constraint is a Range attribute. Replace with explicit
7218 -- mention of the bounds of the prefix, which must be a
7219 -- subtype.
7221 Analyze (Prefix (R));
7222 Hi :=
7223 Convert_To (Implicit_Base,
7224 Make_Attribute_Reference (Loc,
7225 Attribute_Name => Name_Last,
7226 Prefix =>
7227 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
7229 Lo :=
7230 Convert_To (Implicit_Base,
7231 Make_Attribute_Reference (Loc,
7232 Attribute_Name => Name_First,
7233 Prefix =>
7234 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
7235 end if;
7236 end;
7238 else
7239 Hi :=
7240 Build_Scalar_Bound
7241 (Type_High_Bound (Parent_Type),
7242 Parent_Type, Implicit_Base);
7243 Lo :=
7244 Build_Scalar_Bound
7245 (Type_Low_Bound (Parent_Type),
7246 Parent_Type, Implicit_Base);
7247 end if;
7249 Rang_Expr :=
7250 Make_Range (Loc,
7251 Low_Bound => Lo,
7252 High_Bound => Hi);
7254 -- If we constructed a default range for the case where no range
7255 -- was given, then the expressions in the range must not freeze
7256 -- since they do not correspond to expressions in the source.
7257 -- However, if the type inherits predicates the expressions will
7258 -- be elaborated earlier and must freeze.
7260 if Nkind (Indic) /= N_Subtype_Indication
7261 and then not Has_Predicates (Derived_Type)
7262 then
7263 Set_Must_Not_Freeze (Lo);
7264 Set_Must_Not_Freeze (Hi);
7265 Set_Must_Not_Freeze (Rang_Expr);
7266 end if;
7268 Rewrite (N,
7269 Make_Subtype_Declaration (Loc,
7270 Defining_Identifier => Derived_Type,
7271 Subtype_Indication =>
7272 Make_Subtype_Indication (Loc,
7273 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
7274 Constraint =>
7275 Make_Range_Constraint (Loc,
7276 Range_Expression => Rang_Expr))));
7278 Analyze (N);
7280 -- Propagate the aspects from the original type declaration to the
7281 -- declaration of the implicit base.
7283 Move_Aspects (From => Original_Node (N), To => Type_Decl);
7285 -- Apply a range check. Since this range expression doesn't have an
7286 -- Etype, we have to specifically pass the Source_Typ parameter. Is
7287 -- this right???
7289 if Nkind (Indic) = N_Subtype_Indication then
7290 Apply_Range_Check
7291 (Range_Expression (Constraint (Indic)), Parent_Type,
7292 Source_Typ => Entity (Subtype_Mark (Indic)));
7293 end if;
7294 end if;
7295 end Build_Derived_Enumeration_Type;
7297 --------------------------------
7298 -- Build_Derived_Numeric_Type --
7299 --------------------------------
7301 procedure Build_Derived_Numeric_Type
7302 (N : Node_Id;
7303 Parent_Type : Entity_Id;
7304 Derived_Type : Entity_Id)
7306 Loc : constant Source_Ptr := Sloc (N);
7307 Tdef : constant Node_Id := Type_Definition (N);
7308 Indic : constant Node_Id := Subtype_Indication (Tdef);
7309 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
7310 No_Constraint : constant Boolean := Nkind (Indic) /=
7311 N_Subtype_Indication;
7312 Implicit_Base : Entity_Id;
7314 Lo : Node_Id;
7315 Hi : Node_Id;
7317 begin
7318 -- Process the subtype indication including a validation check on
7319 -- the constraint if any.
7321 Discard_Node (Process_Subtype (Indic, N));
7323 -- Introduce an implicit base type for the derived type even if there
7324 -- is no constraint attached to it, since this seems closer to the Ada
7325 -- semantics.
7327 Implicit_Base :=
7328 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
7330 Set_Etype (Implicit_Base, Parent_Base);
7331 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
7332 Set_Size_Info (Implicit_Base, Parent_Base);
7333 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
7334 Set_Parent (Implicit_Base, Parent (Derived_Type));
7335 Set_Is_Known_Valid (Implicit_Base, Is_Known_Valid (Parent_Base));
7337 -- Set RM Size for discrete type or decimal fixed-point type
7338 -- Ordinary fixed-point is excluded, why???
7340 if Is_Discrete_Type (Parent_Base)
7341 or else Is_Decimal_Fixed_Point_Type (Parent_Base)
7342 then
7343 Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
7344 end if;
7346 Set_Has_Delayed_Freeze (Implicit_Base);
7348 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
7349 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
7351 Set_Scalar_Range (Implicit_Base,
7352 Make_Range (Loc,
7353 Low_Bound => Lo,
7354 High_Bound => Hi));
7356 if Has_Infinities (Parent_Base) then
7357 Set_Includes_Infinities (Scalar_Range (Implicit_Base));
7358 end if;
7360 -- The Derived_Type, which is the entity of the declaration, is a
7361 -- subtype of the implicit base. Its Ekind is a subtype, even in the
7362 -- absence of an explicit constraint.
7364 Set_Etype (Derived_Type, Implicit_Base);
7366 -- If we did not have a constraint, then the Ekind is set from the
7367 -- parent type (otherwise Process_Subtype has set the bounds)
7369 if No_Constraint then
7370 Set_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
7371 end if;
7373 -- If we did not have a range constraint, then set the range from the
7374 -- parent type. Otherwise, the Process_Subtype call has set the bounds.
7376 if No_Constraint or else not Has_Range_Constraint (Indic) then
7377 Set_Scalar_Range (Derived_Type,
7378 Make_Range (Loc,
7379 Low_Bound => New_Copy_Tree (Type_Low_Bound (Parent_Type)),
7380 High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
7381 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7383 if Has_Infinities (Parent_Type) then
7384 Set_Includes_Infinities (Scalar_Range (Derived_Type));
7385 end if;
7387 Set_Is_Known_Valid (Derived_Type, Is_Known_Valid (Parent_Type));
7388 end if;
7390 Set_Is_Descendant_Of_Address (Derived_Type,
7391 Is_Descendant_Of_Address (Parent_Type));
7392 Set_Is_Descendant_Of_Address (Implicit_Base,
7393 Is_Descendant_Of_Address (Parent_Type));
7395 -- Set remaining type-specific fields, depending on numeric type
7397 if Is_Modular_Integer_Type (Parent_Type) then
7398 Set_Modulus (Implicit_Base, Modulus (Parent_Base));
7400 Set_Non_Binary_Modulus
7401 (Implicit_Base, Non_Binary_Modulus (Parent_Base));
7403 Set_Is_Known_Valid
7404 (Implicit_Base, Is_Known_Valid (Parent_Base));
7406 elsif Is_Floating_Point_Type (Parent_Type) then
7408 -- Digits of base type is always copied from the digits value of
7409 -- the parent base type, but the digits of the derived type will
7410 -- already have been set if there was a constraint present.
7412 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
7413 Set_Float_Rep (Implicit_Base, Float_Rep (Parent_Base));
7415 if No_Constraint then
7416 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
7417 end if;
7419 elsif Is_Fixed_Point_Type (Parent_Type) then
7421 -- Small of base type and derived type are always copied from the
7422 -- parent base type, since smalls never change. The delta of the
7423 -- base type is also copied from the parent base type. However the
7424 -- delta of the derived type will have been set already if a
7425 -- constraint was present.
7427 Set_Small_Value (Derived_Type, Small_Value (Parent_Base));
7428 Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
7429 Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
7431 if No_Constraint then
7432 Set_Delta_Value (Derived_Type, Delta_Value (Parent_Type));
7433 end if;
7435 -- The scale and machine radix in the decimal case are always
7436 -- copied from the parent base type.
7438 if Is_Decimal_Fixed_Point_Type (Parent_Type) then
7439 Set_Scale_Value (Derived_Type, Scale_Value (Parent_Base));
7440 Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
7442 Set_Machine_Radix_10
7443 (Derived_Type, Machine_Radix_10 (Parent_Base));
7444 Set_Machine_Radix_10
7445 (Implicit_Base, Machine_Radix_10 (Parent_Base));
7447 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
7449 if No_Constraint then
7450 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
7452 else
7453 -- the analysis of the subtype_indication sets the
7454 -- digits value of the derived type.
7456 null;
7457 end if;
7458 end if;
7459 end if;
7461 if Is_Integer_Type (Parent_Type) then
7462 Set_Has_Shift_Operator
7463 (Implicit_Base, Has_Shift_Operator (Parent_Type));
7464 end if;
7466 -- The type of the bounds is that of the parent type, and they
7467 -- must be converted to the derived type.
7469 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
7471 -- The implicit_base should be frozen when the derived type is frozen,
7472 -- but note that it is used in the conversions of the bounds. For fixed
7473 -- types we delay the determination of the bounds until the proper
7474 -- freezing point. For other numeric types this is rejected by GCC, for
7475 -- reasons that are currently unclear (???), so we choose to freeze the
7476 -- implicit base now. In the case of integers and floating point types
7477 -- this is harmless because subsequent representation clauses cannot
7478 -- affect anything, but it is still baffling that we cannot use the
7479 -- same mechanism for all derived numeric types.
7481 -- There is a further complication: actually some representation
7482 -- clauses can affect the implicit base type. For example, attribute
7483 -- definition clauses for stream-oriented attributes need to set the
7484 -- corresponding TSS entries on the base type, and this normally
7485 -- cannot be done after the base type is frozen, so the circuitry in
7486 -- Sem_Ch13.New_Stream_Subprogram must account for this possibility
7487 -- and not use Set_TSS in this case.
7489 -- There are also consequences for the case of delayed representation
7490 -- aspects for some cases. For example, a Size aspect is delayed and
7491 -- should not be evaluated to the freeze point. This early freezing
7492 -- means that the size attribute evaluation happens too early???
7494 if Is_Fixed_Point_Type (Parent_Type) then
7495 Conditional_Delay (Implicit_Base, Parent_Type);
7496 else
7497 Freeze_Before (N, Implicit_Base);
7498 end if;
7499 end Build_Derived_Numeric_Type;
7501 --------------------------------
7502 -- Build_Derived_Private_Type --
7503 --------------------------------
7505 procedure Build_Derived_Private_Type
7506 (N : Node_Id;
7507 Parent_Type : Entity_Id;
7508 Derived_Type : Entity_Id;
7509 Is_Completion : Boolean;
7510 Derive_Subps : Boolean := True)
7512 Loc : constant Source_Ptr := Sloc (N);
7513 Par_Base : constant Entity_Id := Base_Type (Parent_Type);
7514 Par_Scope : constant Entity_Id := Scope (Par_Base);
7515 Full_N : constant Node_Id := New_Copy_Tree (N);
7516 Full_Der : Entity_Id := New_Copy (Derived_Type);
7517 Full_P : Entity_Id;
7519 procedure Build_Full_Derivation;
7520 -- Build full derivation, i.e. derive from the full view
7522 procedure Copy_And_Build;
7523 -- Copy derived type declaration, replace parent with its full view,
7524 -- and build derivation
7526 ---------------------------
7527 -- Build_Full_Derivation --
7528 ---------------------------
7530 procedure Build_Full_Derivation is
7531 begin
7532 -- If parent scope is not open, install the declarations
7534 if not In_Open_Scopes (Par_Scope) then
7535 Install_Private_Declarations (Par_Scope);
7536 Install_Visible_Declarations (Par_Scope);
7537 Copy_And_Build;
7538 Uninstall_Declarations (Par_Scope);
7540 -- If parent scope is open and in another unit, and parent has a
7541 -- completion, then the derivation is taking place in the visible
7542 -- part of a child unit. In that case retrieve the full view of
7543 -- the parent momentarily.
7545 elsif not In_Same_Source_Unit (N, Parent_Type) then
7546 Full_P := Full_View (Parent_Type);
7547 Exchange_Declarations (Parent_Type);
7548 Copy_And_Build;
7549 Exchange_Declarations (Full_P);
7551 -- Otherwise it is a local derivation
7553 else
7554 Copy_And_Build;
7555 end if;
7556 end Build_Full_Derivation;
7558 --------------------
7559 -- Copy_And_Build --
7560 --------------------
7562 procedure Copy_And_Build is
7563 Full_Parent : Entity_Id := Parent_Type;
7565 begin
7566 -- If the parent is itself derived from another private type,
7567 -- installing the private declarations has not affected its
7568 -- privacy status, so use its own full view explicitly.
7570 if Is_Private_Type (Full_Parent)
7571 and then Present (Full_View (Full_Parent))
7572 then
7573 Full_Parent := Full_View (Full_Parent);
7574 end if;
7576 -- And its underlying full view if necessary
7578 if Is_Private_Type (Full_Parent)
7579 and then Present (Underlying_Full_View (Full_Parent))
7580 then
7581 Full_Parent := Underlying_Full_View (Full_Parent);
7582 end if;
7584 -- For record, access and most enumeration types, derivation from
7585 -- the full view requires a fully-fledged declaration. In the other
7586 -- cases, just use an itype.
7588 if Ekind (Full_Parent) in Record_Kind
7589 or else Ekind (Full_Parent) in Access_Kind
7590 or else
7591 (Ekind (Full_Parent) in Enumeration_Kind
7592 and then not Is_Standard_Character_Type (Full_Parent)
7593 and then not Is_Generic_Type (Root_Type (Full_Parent)))
7594 then
7595 -- Copy and adjust declaration to provide a completion for what
7596 -- is originally a private declaration. Indicate that full view
7597 -- is internally generated.
7599 Set_Comes_From_Source (Full_N, False);
7600 Set_Comes_From_Source (Full_Der, False);
7601 Set_Parent (Full_Der, Full_N);
7602 Set_Defining_Identifier (Full_N, Full_Der);
7604 -- If there are no constraints, adjust the subtype mark
7606 if Nkind (Subtype_Indication (Type_Definition (Full_N))) /=
7607 N_Subtype_Indication
7608 then
7609 Set_Subtype_Indication
7610 (Type_Definition (Full_N),
7611 New_Occurrence_Of (Full_Parent, Sloc (Full_N)));
7612 end if;
7614 Insert_After (N, Full_N);
7616 -- Build full view of derived type from full view of parent which
7617 -- is now installed. Subprograms have been derived on the partial
7618 -- view, the completion does not derive them anew.
7620 if Ekind (Full_Parent) in Record_Kind then
7622 -- If parent type is tagged, the completion inherits the proper
7623 -- primitive operations.
7625 if Is_Tagged_Type (Parent_Type) then
7626 Build_Derived_Record_Type
7627 (Full_N, Full_Parent, Full_Der, Derive_Subps);
7628 else
7629 Build_Derived_Record_Type
7630 (Full_N, Full_Parent, Full_Der, Derive_Subps => False);
7631 end if;
7633 else
7634 Build_Derived_Type
7635 (Full_N, Full_Parent, Full_Der,
7636 Is_Completion => False, Derive_Subps => False);
7637 end if;
7639 -- The full declaration has been introduced into the tree and
7640 -- processed in the step above. It should not be analyzed again
7641 -- (when encountered later in the current list of declarations)
7642 -- to prevent spurious name conflicts. The full entity remains
7643 -- invisible.
7645 Set_Analyzed (Full_N);
7647 else
7648 Full_Der :=
7649 Make_Defining_Identifier (Sloc (Derived_Type),
7650 Chars => Chars (Derived_Type));
7651 Set_Is_Itype (Full_Der);
7652 Set_Associated_Node_For_Itype (Full_Der, N);
7653 Set_Parent (Full_Der, N);
7654 Build_Derived_Type
7655 (N, Full_Parent, Full_Der,
7656 Is_Completion => False, Derive_Subps => False);
7657 end if;
7659 Set_Has_Private_Declaration (Full_Der);
7660 Set_Has_Private_Declaration (Derived_Type);
7662 Set_Scope (Full_Der, Scope (Derived_Type));
7663 Set_Is_First_Subtype (Full_Der, Is_First_Subtype (Derived_Type));
7664 Set_Has_Size_Clause (Full_Der, False);
7665 Set_Has_Alignment_Clause (Full_Der, False);
7666 Set_Has_Delayed_Freeze (Full_Der);
7667 Set_Is_Frozen (Full_Der, False);
7668 Set_Freeze_Node (Full_Der, Empty);
7669 Set_Depends_On_Private (Full_Der, Has_Private_Component (Full_Der));
7670 Set_Is_Public (Full_Der, Is_Public (Derived_Type));
7672 -- The convention on the base type may be set in the private part
7673 -- and not propagated to the subtype until later, so we obtain the
7674 -- convention from the base type of the parent.
7676 Set_Convention (Full_Der, Convention (Base_Type (Full_Parent)));
7677 end Copy_And_Build;
7679 -- Start of processing for Build_Derived_Private_Type
7681 begin
7682 if Is_Tagged_Type (Parent_Type) then
7683 Full_P := Full_View (Parent_Type);
7685 -- A type extension of a type with unknown discriminants is an
7686 -- indefinite type that the back-end cannot handle directly.
7687 -- We treat it as a private type, and build a completion that is
7688 -- derived from the full view of the parent, and hopefully has
7689 -- known discriminants.
7691 -- If the full view of the parent type has an underlying record view,
7692 -- use it to generate the underlying record view of this derived type
7693 -- (required for chains of derivations with unknown discriminants).
7695 -- Minor optimization: we avoid the generation of useless underlying
7696 -- record view entities if the private type declaration has unknown
7697 -- discriminants but its corresponding full view has no
7698 -- discriminants.
7700 if Has_Unknown_Discriminants (Parent_Type)
7701 and then Present (Full_P)
7702 and then (Has_Discriminants (Full_P)
7703 or else Present (Underlying_Record_View (Full_P)))
7704 and then not In_Open_Scopes (Par_Scope)
7705 and then Expander_Active
7706 then
7707 declare
7708 Full_Der : constant Entity_Id := Make_Temporary (Loc, 'T');
7709 New_Ext : constant Node_Id :=
7710 Copy_Separate_Tree
7711 (Record_Extension_Part (Type_Definition (N)));
7712 Decl : Node_Id;
7714 begin
7715 Build_Derived_Record_Type
7716 (N, Parent_Type, Derived_Type, Derive_Subps);
7718 -- Build anonymous completion, as a derivation from the full
7719 -- view of the parent. This is not a completion in the usual
7720 -- sense, because the current type is not private.
7722 Decl :=
7723 Make_Full_Type_Declaration (Loc,
7724 Defining_Identifier => Full_Der,
7725 Type_Definition =>
7726 Make_Derived_Type_Definition (Loc,
7727 Subtype_Indication =>
7728 New_Copy_Tree
7729 (Subtype_Indication (Type_Definition (N))),
7730 Record_Extension_Part => New_Ext));
7732 -- If the parent type has an underlying record view, use it
7733 -- here to build the new underlying record view.
7735 if Present (Underlying_Record_View (Full_P)) then
7736 pragma Assert
7737 (Nkind (Subtype_Indication (Type_Definition (Decl)))
7738 = N_Identifier);
7739 Set_Entity (Subtype_Indication (Type_Definition (Decl)),
7740 Underlying_Record_View (Full_P));
7741 end if;
7743 Install_Private_Declarations (Par_Scope);
7744 Install_Visible_Declarations (Par_Scope);
7745 Insert_Before (N, Decl);
7747 -- Mark entity as an underlying record view before analysis,
7748 -- to avoid generating the list of its primitive operations
7749 -- (which is not really required for this entity) and thus
7750 -- prevent spurious errors associated with missing overriding
7751 -- of abstract primitives (overridden only for Derived_Type).
7753 Set_Ekind (Full_Der, E_Record_Type);
7754 Set_Is_Underlying_Record_View (Full_Der);
7755 Set_Default_SSO (Full_Der);
7756 Set_No_Reordering (Full_Der, No_Component_Reordering);
7758 Analyze (Decl);
7760 pragma Assert (Has_Discriminants (Full_Der)
7761 and then not Has_Unknown_Discriminants (Full_Der));
7763 Uninstall_Declarations (Par_Scope);
7765 -- Freeze the underlying record view, to prevent generation of
7766 -- useless dispatching information, which is simply shared with
7767 -- the real derived type.
7769 Set_Is_Frozen (Full_Der);
7771 -- If the derived type has access discriminants, create
7772 -- references to their anonymous types now, to prevent
7773 -- back-end problems when their first use is in generated
7774 -- bodies of primitives.
7776 declare
7777 E : Entity_Id;
7779 begin
7780 E := First_Entity (Full_Der);
7782 while Present (E) loop
7783 if Ekind (E) = E_Discriminant
7784 and then Ekind (Etype (E)) = E_Anonymous_Access_Type
7785 then
7786 Build_Itype_Reference (Etype (E), Decl);
7787 end if;
7789 Next_Entity (E);
7790 end loop;
7791 end;
7793 -- Set up links between real entity and underlying record view
7795 Set_Underlying_Record_View (Derived_Type, Base_Type (Full_Der));
7796 Set_Underlying_Record_View (Base_Type (Full_Der), Derived_Type);
7797 end;
7799 -- If discriminants are known, build derived record
7801 else
7802 Build_Derived_Record_Type
7803 (N, Parent_Type, Derived_Type, Derive_Subps);
7804 end if;
7806 return;
7808 elsif Has_Discriminants (Parent_Type) then
7810 -- Build partial view of derived type from partial view of parent.
7811 -- This must be done before building the full derivation because the
7812 -- second derivation will modify the discriminants of the first and
7813 -- the discriminants are chained with the rest of the components in
7814 -- the full derivation.
7816 Build_Derived_Record_Type
7817 (N, Parent_Type, Derived_Type, Derive_Subps);
7819 -- Build the full derivation if this is not the anonymous derived
7820 -- base type created by Build_Derived_Record_Type in the constrained
7821 -- case (see point 5. of its head comment) since we build it for the
7822 -- derived subtype. And skip it for protected types altogether, as
7823 -- gigi does not use these types directly.
7825 if Present (Full_View (Parent_Type))
7826 and then not Is_Itype (Derived_Type)
7827 and then not (Ekind (Full_View (Parent_Type)) in Protected_Kind)
7828 then
7829 declare
7830 Der_Base : constant Entity_Id := Base_Type (Derived_Type);
7831 Discr : Entity_Id;
7832 Last_Discr : Entity_Id;
7834 begin
7835 -- If this is not a completion, construct the implicit full
7836 -- view by deriving from the full view of the parent type.
7837 -- But if this is a completion, the derived private type
7838 -- being built is a full view and the full derivation can
7839 -- only be its underlying full view.
7841 Build_Full_Derivation;
7843 if not Is_Completion then
7844 Set_Full_View (Derived_Type, Full_Der);
7845 else
7846 Set_Underlying_Full_View (Derived_Type, Full_Der);
7847 Set_Is_Underlying_Full_View (Full_Der);
7848 end if;
7850 if not Is_Base_Type (Derived_Type) then
7851 Set_Full_View (Der_Base, Base_Type (Full_Der));
7852 end if;
7854 -- Copy the discriminant list from full view to the partial
7855 -- view (base type and its subtype). Gigi requires that the
7856 -- partial and full views have the same discriminants.
7858 -- Note that since the partial view points to discriminants
7859 -- in the full view, their scope will be that of the full
7860 -- view. This might cause some front end problems and need
7861 -- adjustment???
7863 Discr := First_Discriminant (Base_Type (Full_Der));
7864 Set_First_Entity (Der_Base, Discr);
7866 loop
7867 Last_Discr := Discr;
7868 Next_Discriminant (Discr);
7869 exit when No (Discr);
7870 end loop;
7872 Set_Last_Entity (Der_Base, Last_Discr);
7873 Set_First_Entity (Derived_Type, First_Entity (Der_Base));
7874 Set_Last_Entity (Derived_Type, Last_Entity (Der_Base));
7875 end;
7876 end if;
7878 elsif Present (Full_View (Parent_Type))
7879 and then Has_Discriminants (Full_View (Parent_Type))
7880 then
7881 if Has_Unknown_Discriminants (Parent_Type)
7882 and then Nkind (Subtype_Indication (Type_Definition (N))) =
7883 N_Subtype_Indication
7884 then
7885 Error_Msg_N
7886 ("cannot constrain type with unknown discriminants",
7887 Subtype_Indication (Type_Definition (N)));
7888 return;
7889 end if;
7891 -- If this is not a completion, construct the implicit full view by
7892 -- deriving from the full view of the parent type. But if this is a
7893 -- completion, the derived private type being built is a full view
7894 -- and the full derivation can only be its underlying full view.
7896 Build_Full_Derivation;
7898 if not Is_Completion then
7899 Set_Full_View (Derived_Type, Full_Der);
7900 else
7901 Set_Underlying_Full_View (Derived_Type, Full_Der);
7902 Set_Is_Underlying_Full_View (Full_Der);
7903 end if;
7905 -- In any case, the primitive operations are inherited from the
7906 -- parent type, not from the internal full view.
7908 Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
7910 if Derive_Subps then
7911 Derive_Subprograms (Parent_Type, Derived_Type);
7912 end if;
7914 Set_Stored_Constraint (Derived_Type, No_Elist);
7915 Set_Is_Constrained
7916 (Derived_Type, Is_Constrained (Full_View (Parent_Type)));
7918 else
7919 -- Untagged type, No discriminants on either view
7921 if Nkind (Subtype_Indication (Type_Definition (N))) =
7922 N_Subtype_Indication
7923 then
7924 Error_Msg_N
7925 ("illegal constraint on type without discriminants", N);
7926 end if;
7928 if Present (Discriminant_Specifications (N))
7929 and then Present (Full_View (Parent_Type))
7930 and then not Is_Tagged_Type (Full_View (Parent_Type))
7931 then
7932 Error_Msg_N ("cannot add discriminants to untagged type", N);
7933 end if;
7935 Set_Stored_Constraint (Derived_Type, No_Elist);
7936 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7938 Set_Is_Controlled_Active
7939 (Derived_Type, Is_Controlled_Active (Parent_Type));
7941 Set_Disable_Controlled
7942 (Derived_Type, Disable_Controlled (Parent_Type));
7944 Set_Has_Controlled_Component
7945 (Derived_Type, Has_Controlled_Component (Parent_Type));
7947 -- Direct controlled types do not inherit Finalize_Storage_Only flag
7949 if not Is_Controlled (Parent_Type) then
7950 Set_Finalize_Storage_Only
7951 (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
7952 end if;
7954 -- If this is not a completion, construct the implicit full view by
7955 -- deriving from the full view of the parent type.
7957 -- ??? If the parent is untagged private and its completion is
7958 -- tagged, this mechanism will not work because we cannot derive from
7959 -- the tagged full view unless we have an extension.
7961 if Present (Full_View (Parent_Type))
7962 and then not Is_Tagged_Type (Full_View (Parent_Type))
7963 and then not Is_Completion
7964 then
7965 Build_Full_Derivation;
7966 Set_Full_View (Derived_Type, Full_Der);
7967 end if;
7968 end if;
7970 Set_Has_Unknown_Discriminants (Derived_Type,
7971 Has_Unknown_Discriminants (Parent_Type));
7973 if Is_Private_Type (Derived_Type) then
7974 Set_Private_Dependents (Derived_Type, New_Elmt_List);
7975 end if;
7977 -- If the parent base type is in scope, add the derived type to its
7978 -- list of private dependents, because its full view may become
7979 -- visible subsequently (in a nested private part, a body, or in a
7980 -- further child unit).
7982 if Is_Private_Type (Par_Base) and then In_Open_Scopes (Par_Scope) then
7983 Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
7985 -- Check for unusual case where a type completed by a private
7986 -- derivation occurs within a package nested in a child unit, and
7987 -- the parent is declared in an ancestor.
7989 if Is_Child_Unit (Scope (Current_Scope))
7990 and then Is_Completion
7991 and then In_Private_Part (Current_Scope)
7992 and then Scope (Parent_Type) /= Current_Scope
7994 -- Note that if the parent has a completion in the private part,
7995 -- (which is itself a derivation from some other private type)
7996 -- it is that completion that is visible, there is no full view
7997 -- available, and no special processing is needed.
7999 and then Present (Full_View (Parent_Type))
8000 then
8001 -- In this case, the full view of the parent type will become
8002 -- visible in the body of the enclosing child, and only then will
8003 -- the current type be possibly non-private. Build an underlying
8004 -- full view that will be installed when the enclosing child body
8005 -- is compiled.
8007 if Present (Underlying_Full_View (Derived_Type)) then
8008 Full_Der := Underlying_Full_View (Derived_Type);
8009 else
8010 Build_Full_Derivation;
8011 Set_Underlying_Full_View (Derived_Type, Full_Der);
8012 Set_Is_Underlying_Full_View (Full_Der);
8013 end if;
8015 -- The full view will be used to swap entities on entry/exit to
8016 -- the body, and must appear in the entity list for the package.
8018 Append_Entity (Full_Der, Scope (Derived_Type));
8019 end if;
8020 end if;
8021 end Build_Derived_Private_Type;
8023 -------------------------------
8024 -- Build_Derived_Record_Type --
8025 -------------------------------
8027 -- 1. INTRODUCTION
8029 -- Ideally we would like to use the same model of type derivation for
8030 -- tagged and untagged record types. Unfortunately this is not quite
8031 -- possible because the semantics of representation clauses is different
8032 -- for tagged and untagged records under inheritance. Consider the
8033 -- following:
8035 -- type R (...) is [tagged] record ... end record;
8036 -- type T (...) is new R (...) [with ...];
8038 -- The representation clauses for T can specify a completely different
8039 -- record layout from R's. Hence the same component can be placed in two
8040 -- very different positions in objects of type T and R. If R and T are
8041 -- tagged types, representation clauses for T can only specify the layout
8042 -- of non inherited components, thus components that are common in R and T
8043 -- have the same position in objects of type R and T.
8045 -- This has two implications. The first is that the entire tree for R's
8046 -- declaration needs to be copied for T in the untagged case, so that T
8047 -- can be viewed as a record type of its own with its own representation
8048 -- clauses. The second implication is the way we handle discriminants.
8049 -- Specifically, in the untagged case we need a way to communicate to Gigi
8050 -- what are the real discriminants in the record, while for the semantics
8051 -- we need to consider those introduced by the user to rename the
8052 -- discriminants in the parent type. This is handled by introducing the
8053 -- notion of stored discriminants. See below for more.
8055 -- Fortunately the way regular components are inherited can be handled in
8056 -- the same way in tagged and untagged types.
8058 -- To complicate things a bit more the private view of a private extension
8059 -- cannot be handled in the same way as the full view (for one thing the
8060 -- semantic rules are somewhat different). We will explain what differs
8061 -- below.
8063 -- 2. DISCRIMINANTS UNDER INHERITANCE
8065 -- The semantic rules governing the discriminants of derived types are
8066 -- quite subtle.
8068 -- type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
8069 -- [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
8071 -- If parent type has discriminants, then the discriminants that are
8072 -- declared in the derived type are [3.4 (11)]:
8074 -- o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
8075 -- there is one;
8077 -- o Otherwise, each discriminant of the parent type (implicitly declared
8078 -- in the same order with the same specifications). In this case, the
8079 -- discriminants are said to be "inherited", or if unknown in the parent
8080 -- are also unknown in the derived type.
8082 -- Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
8084 -- o The parent subtype must be constrained;
8086 -- o If the parent type is not a tagged type, then each discriminant of
8087 -- the derived type must be used in the constraint defining a parent
8088 -- subtype. [Implementation note: This ensures that the new discriminant
8089 -- can share storage with an existing discriminant.]
8091 -- For the derived type each discriminant of the parent type is either
8092 -- inherited, constrained to equal some new discriminant of the derived
8093 -- type, or constrained to the value of an expression.
8095 -- When inherited or constrained to equal some new discriminant, the
8096 -- parent discriminant and the discriminant of the derived type are said
8097 -- to "correspond".
8099 -- If a discriminant of the parent type is constrained to a specific value
8100 -- in the derived type definition, then the discriminant is said to be
8101 -- "specified" by that derived type definition.
8103 -- 3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
8105 -- We have spoken about stored discriminants in point 1 (introduction)
8106 -- above. There are two sorts of stored discriminants: implicit and
8107 -- explicit. As long as the derived type inherits the same discriminants as
8108 -- the root record type, stored discriminants are the same as regular
8109 -- discriminants, and are said to be implicit. However, if any discriminant
8110 -- in the root type was renamed in the derived type, then the derived
8111 -- type will contain explicit stored discriminants. Explicit stored
8112 -- discriminants are discriminants in addition to the semantically visible
8113 -- discriminants defined for the derived type. Stored discriminants are
8114 -- used by Gigi to figure out what are the physical discriminants in
8115 -- objects of the derived type (see precise definition in einfo.ads).
8116 -- As an example, consider the following:
8118 -- type R (D1, D2, D3 : Int) is record ... end record;
8119 -- type T1 is new R;
8120 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
8121 -- type T3 is new T2;
8122 -- type T4 (Y : Int) is new T3 (Y, 99);
8124 -- The following table summarizes the discriminants and stored
8125 -- discriminants in R and T1 through T4:
8127 -- Type Discrim Stored Discrim Comment
8128 -- R (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in R
8129 -- T1 (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in T1
8130 -- T2 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T2
8131 -- T3 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T3
8132 -- T4 (Y) (D1, D2, D3) Girder discrims EXPLICIT in T4
8134 -- Field Corresponding_Discriminant (abbreviated CD below) allows us to
8135 -- find the corresponding discriminant in the parent type, while
8136 -- Original_Record_Component (abbreviated ORC below) the actual physical
8137 -- component that is renamed. Finally the field Is_Completely_Hidden
8138 -- (abbreviated ICH below) is set for all explicit stored discriminants
8139 -- (see einfo.ads for more info). For the above example this gives:
8141 -- Discrim CD ORC ICH
8142 -- ^^^^^^^ ^^ ^^^ ^^^
8143 -- D1 in R empty itself no
8144 -- D2 in R empty itself no
8145 -- D3 in R empty itself no
8147 -- D1 in T1 D1 in R itself no
8148 -- D2 in T1 D2 in R itself no
8149 -- D3 in T1 D3 in R itself no
8151 -- X1 in T2 D3 in T1 D3 in T2 no
8152 -- X2 in T2 D1 in T1 D1 in T2 no
8153 -- D1 in T2 empty itself yes
8154 -- D2 in T2 empty itself yes
8155 -- D3 in T2 empty itself yes
8157 -- X1 in T3 X1 in T2 D3 in T3 no
8158 -- X2 in T3 X2 in T2 D1 in T3 no
8159 -- D1 in T3 empty itself yes
8160 -- D2 in T3 empty itself yes
8161 -- D3 in T3 empty itself yes
8163 -- Y in T4 X1 in T3 D3 in T4 no
8164 -- D1 in T4 empty itself yes
8165 -- D2 in T4 empty itself yes
8166 -- D3 in T4 empty itself yes
8168 -- 4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
8170 -- Type derivation for tagged types is fairly straightforward. If no
8171 -- discriminants are specified by the derived type, these are inherited
8172 -- from the parent. No explicit stored discriminants are ever necessary.
8173 -- The only manipulation that is done to the tree is that of adding a
8174 -- _parent field with parent type and constrained to the same constraint
8175 -- specified for the parent in the derived type definition. For instance:
8177 -- type R (D1, D2, D3 : Int) is tagged record ... end record;
8178 -- type T1 is new R with null record;
8179 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
8181 -- are changed into:
8183 -- type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
8184 -- _parent : R (D1, D2, D3);
8185 -- end record;
8187 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
8188 -- _parent : T1 (X2, 88, X1);
8189 -- end record;
8191 -- The discriminants actually present in R, T1 and T2 as well as their CD,
8192 -- ORC and ICH fields are:
8194 -- Discrim CD ORC ICH
8195 -- ^^^^^^^ ^^ ^^^ ^^^
8196 -- D1 in R empty itself no
8197 -- D2 in R empty itself no
8198 -- D3 in R empty itself no
8200 -- D1 in T1 D1 in R D1 in R no
8201 -- D2 in T1 D2 in R D2 in R no
8202 -- D3 in T1 D3 in R D3 in R no
8204 -- X1 in T2 D3 in T1 D3 in R no
8205 -- X2 in T2 D1 in T1 D1 in R no
8207 -- 5. FIRST TRANSFORMATION FOR DERIVED RECORDS
8209 -- Regardless of whether we dealing with a tagged or untagged type
8210 -- we will transform all derived type declarations of the form
8212 -- type T is new R (...) [with ...];
8213 -- or
8214 -- subtype S is R (...);
8215 -- type T is new S [with ...];
8216 -- into
8217 -- type BT is new R [with ...];
8218 -- subtype T is BT (...);
8220 -- That is, the base derived type is constrained only if it has no
8221 -- discriminants. The reason for doing this is that GNAT's semantic model
8222 -- assumes that a base type with discriminants is unconstrained.
8224 -- Note that, strictly speaking, the above transformation is not always
8225 -- correct. Consider for instance the following excerpt from ACVC b34011a:
8227 -- procedure B34011A is
8228 -- type REC (D : integer := 0) is record
8229 -- I : Integer;
8230 -- end record;
8232 -- package P is
8233 -- type T6 is new Rec;
8234 -- function F return T6;
8235 -- end P;
8237 -- use P;
8238 -- package Q6 is
8239 -- type U is new T6 (Q6.F.I); -- ERROR: Q6.F.
8240 -- end Q6;
8242 -- The definition of Q6.U is illegal. However transforming Q6.U into
8244 -- type BaseU is new T6;
8245 -- subtype U is BaseU (Q6.F.I)
8247 -- turns U into a legal subtype, which is incorrect. To avoid this problem
8248 -- we always analyze the constraint (in this case (Q6.F.I)) before applying
8249 -- the transformation described above.
8251 -- There is another instance where the above transformation is incorrect.
8252 -- Consider:
8254 -- package Pack is
8255 -- type Base (D : Integer) is tagged null record;
8256 -- procedure P (X : Base);
8258 -- type Der is new Base (2) with null record;
8259 -- procedure P (X : Der);
8260 -- end Pack;
8262 -- Then the above transformation turns this into
8264 -- type Der_Base is new Base with null record;
8265 -- -- procedure P (X : Base) is implicitly inherited here
8266 -- -- as procedure P (X : Der_Base).
8268 -- subtype Der is Der_Base (2);
8269 -- procedure P (X : Der);
8270 -- -- The overriding of P (X : Der_Base) is illegal since we
8271 -- -- have a parameter conformance problem.
8273 -- To get around this problem, after having semantically processed Der_Base
8274 -- and the rewritten subtype declaration for Der, we copy Der_Base field
8275 -- Discriminant_Constraint from Der so that when parameter conformance is
8276 -- checked when P is overridden, no semantic errors are flagged.
8278 -- 6. SECOND TRANSFORMATION FOR DERIVED RECORDS
8280 -- Regardless of whether we are dealing with a tagged or untagged type
8281 -- we will transform all derived type declarations of the form
8283 -- type R (D1, .., Dn : ...) is [tagged] record ...;
8284 -- type T is new R [with ...];
8285 -- into
8286 -- type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
8288 -- The reason for such transformation is that it allows us to implement a
8289 -- very clean form of component inheritance as explained below.
8291 -- Note that this transformation is not achieved by direct tree rewriting
8292 -- and manipulation, but rather by redoing the semantic actions that the
8293 -- above transformation will entail. This is done directly in routine
8294 -- Inherit_Components.
8296 -- 7. TYPE DERIVATION AND COMPONENT INHERITANCE
8298 -- In both tagged and untagged derived types, regular non discriminant
8299 -- components are inherited in the derived type from the parent type. In
8300 -- the absence of discriminants component, inheritance is straightforward
8301 -- as components can simply be copied from the parent.
8303 -- If the parent has discriminants, inheriting components constrained with
8304 -- these discriminants requires caution. Consider the following example:
8306 -- type R (D1, D2 : Positive) is [tagged] record
8307 -- S : String (D1 .. D2);
8308 -- end record;
8310 -- type T1 is new R [with null record];
8311 -- type T2 (X : positive) is new R (1, X) [with null record];
8313 -- As explained in 6. above, T1 is rewritten as
8314 -- type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
8315 -- which makes the treatment for T1 and T2 identical.
8317 -- What we want when inheriting S, is that references to D1 and D2 in R are
8318 -- replaced with references to their correct constraints, i.e. D1 and D2 in
8319 -- T1 and 1 and X in T2. So all R's discriminant references are replaced
8320 -- with either discriminant references in the derived type or expressions.
8321 -- This replacement is achieved as follows: before inheriting R's
8322 -- components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
8323 -- created in the scope of T1 (resp. scope of T2) so that discriminants D1
8324 -- and D2 of T1 are visible (resp. discriminant X of T2 is visible).
8325 -- For T2, for instance, this has the effect of replacing String (D1 .. D2)
8326 -- by String (1 .. X).
8328 -- 8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
8330 -- We explain here the rules governing private type extensions relevant to
8331 -- type derivation. These rules are explained on the following example:
8333 -- type D [(...)] is new A [(...)] with private; <-- partial view
8334 -- type D [(...)] is new P [(...)] with null record; <-- full view
8336 -- Type A is called the ancestor subtype of the private extension.
8337 -- Type P is the parent type of the full view of the private extension. It
8338 -- must be A or a type derived from A.
8340 -- The rules concerning the discriminants of private type extensions are
8341 -- [7.3(10-13)]:
8343 -- o If a private extension inherits known discriminants from the ancestor
8344 -- subtype, then the full view must also inherit its discriminants from
8345 -- the ancestor subtype and the parent subtype of the full view must be
8346 -- constrained if and only if the ancestor subtype is constrained.
8348 -- o If a partial view has unknown discriminants, then the full view may
8349 -- define a definite or an indefinite subtype, with or without
8350 -- discriminants.
8352 -- o If a partial view has neither known nor unknown discriminants, then
8353 -- the full view must define a definite subtype.
8355 -- o If the ancestor subtype of a private extension has constrained
8356 -- discriminants, then the parent subtype of the full view must impose a
8357 -- statically matching constraint on those discriminants.
8359 -- This means that only the following forms of private extensions are
8360 -- allowed:
8362 -- type D is new A with private; <-- partial view
8363 -- type D is new P with null record; <-- full view
8365 -- If A has no discriminants than P has no discriminants, otherwise P must
8366 -- inherit A's discriminants.
8368 -- type D is new A (...) with private; <-- partial view
8369 -- type D is new P (:::) with null record; <-- full view
8371 -- P must inherit A's discriminants and (...) and (:::) must statically
8372 -- match.
8374 -- subtype A is R (...);
8375 -- type D is new A with private; <-- partial view
8376 -- type D is new P with null record; <-- full view
8378 -- P must have inherited R's discriminants and must be derived from A or
8379 -- any of its subtypes.
8381 -- type D (..) is new A with private; <-- partial view
8382 -- type D (..) is new P [(:::)] with null record; <-- full view
8384 -- No specific constraints on P's discriminants or constraint (:::).
8385 -- Note that A can be unconstrained, but the parent subtype P must either
8386 -- be constrained or (:::) must be present.
8388 -- type D (..) is new A [(...)] with private; <-- partial view
8389 -- type D (..) is new P [(:::)] with null record; <-- full view
8391 -- P's constraints on A's discriminants must statically match those
8392 -- imposed by (...).
8394 -- 9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
8396 -- The full view of a private extension is handled exactly as described
8397 -- above. The model chose for the private view of a private extension is
8398 -- the same for what concerns discriminants (i.e. they receive the same
8399 -- treatment as in the tagged case). However, the private view of the
8400 -- private extension always inherits the components of the parent base,
8401 -- without replacing any discriminant reference. Strictly speaking this is
8402 -- incorrect. However, Gigi never uses this view to generate code so this
8403 -- is a purely semantic issue. In theory, a set of transformations similar
8404 -- to those given in 5. and 6. above could be applied to private views of
8405 -- private extensions to have the same model of component inheritance as
8406 -- for non private extensions. However, this is not done because it would
8407 -- further complicate private type processing. Semantically speaking, this
8408 -- leaves us in an uncomfortable situation. As an example consider:
8410 -- package Pack is
8411 -- type R (D : integer) is tagged record
8412 -- S : String (1 .. D);
8413 -- end record;
8414 -- procedure P (X : R);
8415 -- type T is new R (1) with private;
8416 -- private
8417 -- type T is new R (1) with null record;
8418 -- end;
8420 -- This is transformed into:
8422 -- package Pack is
8423 -- type R (D : integer) is tagged record
8424 -- S : String (1 .. D);
8425 -- end record;
8426 -- procedure P (X : R);
8427 -- type T is new R (1) with private;
8428 -- private
8429 -- type BaseT is new R with null record;
8430 -- subtype T is BaseT (1);
8431 -- end;
8433 -- (strictly speaking the above is incorrect Ada)
8435 -- From the semantic standpoint the private view of private extension T
8436 -- should be flagged as constrained since one can clearly have
8438 -- Obj : T;
8440 -- in a unit withing Pack. However, when deriving subprograms for the
8441 -- private view of private extension T, T must be seen as unconstrained
8442 -- since T has discriminants (this is a constraint of the current
8443 -- subprogram derivation model). Thus, when processing the private view of
8444 -- a private extension such as T, we first mark T as unconstrained, we
8445 -- process it, we perform program derivation and just before returning from
8446 -- Build_Derived_Record_Type we mark T as constrained.
8448 -- ??? Are there are other uncomfortable cases that we will have to
8449 -- deal with.
8451 -- 10. RECORD_TYPE_WITH_PRIVATE complications
8453 -- Types that are derived from a visible record type and have a private
8454 -- extension present other peculiarities. They behave mostly like private
8455 -- types, but if they have primitive operations defined, these will not
8456 -- have the proper signatures for further inheritance, because other
8457 -- primitive operations will use the implicit base that we define for
8458 -- private derivations below. This affect subprogram inheritance (see
8459 -- Derive_Subprograms for details). We also derive the implicit base from
8460 -- the base type of the full view, so that the implicit base is a record
8461 -- type and not another private type, This avoids infinite loops.
8463 procedure Build_Derived_Record_Type
8464 (N : Node_Id;
8465 Parent_Type : Entity_Id;
8466 Derived_Type : Entity_Id;
8467 Derive_Subps : Boolean := True)
8469 Discriminant_Specs : constant Boolean :=
8470 Present (Discriminant_Specifications (N));
8471 Is_Tagged : constant Boolean := Is_Tagged_Type (Parent_Type);
8472 Loc : constant Source_Ptr := Sloc (N);
8473 Private_Extension : constant Boolean :=
8474 Nkind (N) = N_Private_Extension_Declaration;
8475 Assoc_List : Elist_Id;
8476 Constraint_Present : Boolean;
8477 Constrs : Elist_Id;
8478 Discrim : Entity_Id;
8479 Indic : Node_Id;
8480 Inherit_Discrims : Boolean := False;
8481 Last_Discrim : Entity_Id;
8482 New_Base : Entity_Id;
8483 New_Decl : Node_Id;
8484 New_Discrs : Elist_Id;
8485 New_Indic : Node_Id;
8486 Parent_Base : Entity_Id;
8487 Save_Etype : Entity_Id;
8488 Save_Discr_Constr : Elist_Id;
8489 Save_Next_Entity : Entity_Id;
8490 Type_Def : Node_Id;
8492 Discs : Elist_Id := New_Elmt_List;
8493 -- An empty Discs list means that there were no constraints in the
8494 -- subtype indication or that there was an error processing it.
8496 begin
8497 if Ekind (Parent_Type) = E_Record_Type_With_Private
8498 and then Present (Full_View (Parent_Type))
8499 and then Has_Discriminants (Parent_Type)
8500 then
8501 Parent_Base := Base_Type (Full_View (Parent_Type));
8502 else
8503 Parent_Base := Base_Type (Parent_Type);
8504 end if;
8506 -- AI05-0115 : if this is a derivation from a private type in some
8507 -- other scope that may lead to invisible components for the derived
8508 -- type, mark it accordingly.
8510 if Is_Private_Type (Parent_Type) then
8511 if Scope (Parent_Type) = Scope (Derived_Type) then
8512 null;
8514 elsif In_Open_Scopes (Scope (Parent_Type))
8515 and then In_Private_Part (Scope (Parent_Type))
8516 then
8517 null;
8519 else
8520 Set_Has_Private_Ancestor (Derived_Type);
8521 end if;
8523 else
8524 Set_Has_Private_Ancestor
8525 (Derived_Type, Has_Private_Ancestor (Parent_Type));
8526 end if;
8528 -- Before we start the previously documented transformations, here is
8529 -- little fix for size and alignment of tagged types. Normally when we
8530 -- derive type D from type P, we copy the size and alignment of P as the
8531 -- default for D, and in the absence of explicit representation clauses
8532 -- for D, the size and alignment are indeed the same as the parent.
8534 -- But this is wrong for tagged types, since fields may be added, and
8535 -- the default size may need to be larger, and the default alignment may
8536 -- need to be larger.
8538 -- We therefore reset the size and alignment fields in the tagged case.
8539 -- Note that the size and alignment will in any case be at least as
8540 -- large as the parent type (since the derived type has a copy of the
8541 -- parent type in the _parent field)
8543 -- The type is also marked as being tagged here, which is needed when
8544 -- processing components with a self-referential anonymous access type
8545 -- in the call to Check_Anonymous_Access_Components below. Note that
8546 -- this flag is also set later on for completeness.
8548 if Is_Tagged then
8549 Set_Is_Tagged_Type (Derived_Type);
8550 Init_Size_Align (Derived_Type);
8551 end if;
8553 -- STEP 0a: figure out what kind of derived type declaration we have
8555 if Private_Extension then
8556 Type_Def := N;
8557 Set_Ekind (Derived_Type, E_Record_Type_With_Private);
8558 Set_Default_SSO (Derived_Type);
8559 Set_No_Reordering (Derived_Type, No_Component_Reordering);
8561 else
8562 Type_Def := Type_Definition (N);
8564 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
8565 -- Parent_Base can be a private type or private extension. However,
8566 -- for tagged types with an extension the newly added fields are
8567 -- visible and hence the Derived_Type is always an E_Record_Type.
8568 -- (except that the parent may have its own private fields).
8569 -- For untagged types we preserve the Ekind of the Parent_Base.
8571 if Present (Record_Extension_Part (Type_Def)) then
8572 Set_Ekind (Derived_Type, E_Record_Type);
8573 Set_Default_SSO (Derived_Type);
8574 Set_No_Reordering (Derived_Type, No_Component_Reordering);
8576 -- Create internal access types for components with anonymous
8577 -- access types.
8579 if Ada_Version >= Ada_2005 then
8580 Check_Anonymous_Access_Components
8581 (N, Derived_Type, Derived_Type,
8582 Component_List (Record_Extension_Part (Type_Def)));
8583 end if;
8585 else
8586 Set_Ekind (Derived_Type, Ekind (Parent_Base));
8587 end if;
8588 end if;
8590 -- Indic can either be an N_Identifier if the subtype indication
8591 -- contains no constraint or an N_Subtype_Indication if the subtype
8592 -- indication has a constraint.
8594 Indic := Subtype_Indication (Type_Def);
8595 Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
8597 -- Check that the type has visible discriminants. The type may be
8598 -- a private type with unknown discriminants whose full view has
8599 -- discriminants which are invisible.
8601 if Constraint_Present then
8602 if not Has_Discriminants (Parent_Base)
8603 or else
8604 (Has_Unknown_Discriminants (Parent_Base)
8605 and then Is_Private_Type (Parent_Base))
8606 then
8607 Error_Msg_N
8608 ("invalid constraint: type has no discriminant",
8609 Constraint (Indic));
8611 Constraint_Present := False;
8612 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
8614 elsif Is_Constrained (Parent_Type) then
8615 Error_Msg_N
8616 ("invalid constraint: parent type is already constrained",
8617 Constraint (Indic));
8619 Constraint_Present := False;
8620 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
8621 end if;
8622 end if;
8624 -- STEP 0b: If needed, apply transformation given in point 5. above
8626 if not Private_Extension
8627 and then Has_Discriminants (Parent_Type)
8628 and then not Discriminant_Specs
8629 and then (Is_Constrained (Parent_Type) or else Constraint_Present)
8630 then
8631 -- First, we must analyze the constraint (see comment in point 5.)
8632 -- The constraint may come from the subtype indication of the full
8633 -- declaration.
8635 if Constraint_Present then
8636 New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
8638 -- If there is no explicit constraint, there might be one that is
8639 -- inherited from a constrained parent type. In that case verify that
8640 -- it conforms to the constraint in the partial view. In perverse
8641 -- cases the parent subtypes of the partial and full view can have
8642 -- different constraints.
8644 elsif Present (Stored_Constraint (Parent_Type)) then
8645 New_Discrs := Stored_Constraint (Parent_Type);
8647 else
8648 New_Discrs := No_Elist;
8649 end if;
8651 if Has_Discriminants (Derived_Type)
8652 and then Has_Private_Declaration (Derived_Type)
8653 and then Present (Discriminant_Constraint (Derived_Type))
8654 and then Present (New_Discrs)
8655 then
8656 -- Verify that constraints of the full view statically match
8657 -- those given in the partial view.
8659 declare
8660 C1, C2 : Elmt_Id;
8662 begin
8663 C1 := First_Elmt (New_Discrs);
8664 C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
8665 while Present (C1) and then Present (C2) loop
8666 if Fully_Conformant_Expressions (Node (C1), Node (C2))
8667 or else
8668 (Is_OK_Static_Expression (Node (C1))
8669 and then Is_OK_Static_Expression (Node (C2))
8670 and then
8671 Expr_Value (Node (C1)) = Expr_Value (Node (C2)))
8672 then
8673 null;
8675 else
8676 if Constraint_Present then
8677 Error_Msg_N
8678 ("constraint not conformant to previous declaration",
8679 Node (C1));
8680 else
8681 Error_Msg_N
8682 ("constraint of full view is incompatible "
8683 & "with partial view", N);
8684 end if;
8685 end if;
8687 Next_Elmt (C1);
8688 Next_Elmt (C2);
8689 end loop;
8690 end;
8691 end if;
8693 -- Insert and analyze the declaration for the unconstrained base type
8695 New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
8697 New_Decl :=
8698 Make_Full_Type_Declaration (Loc,
8699 Defining_Identifier => New_Base,
8700 Type_Definition =>
8701 Make_Derived_Type_Definition (Loc,
8702 Abstract_Present => Abstract_Present (Type_Def),
8703 Limited_Present => Limited_Present (Type_Def),
8704 Subtype_Indication =>
8705 New_Occurrence_Of (Parent_Base, Loc),
8706 Record_Extension_Part =>
8707 Relocate_Node (Record_Extension_Part (Type_Def)),
8708 Interface_List => Interface_List (Type_Def)));
8710 Set_Parent (New_Decl, Parent (N));
8711 Mark_Rewrite_Insertion (New_Decl);
8712 Insert_Before (N, New_Decl);
8714 -- In the extension case, make sure ancestor is frozen appropriately
8715 -- (see also non-discriminated case below).
8717 if Present (Record_Extension_Part (Type_Def))
8718 or else Is_Interface (Parent_Base)
8719 then
8720 Freeze_Before (New_Decl, Parent_Type);
8721 end if;
8723 -- Note that this call passes False for the Derive_Subps parameter
8724 -- because subprogram derivation is deferred until after creating
8725 -- the subtype (see below).
8727 Build_Derived_Type
8728 (New_Decl, Parent_Base, New_Base,
8729 Is_Completion => False, Derive_Subps => False);
8731 -- ??? This needs re-examination to determine whether the
8732 -- above call can simply be replaced by a call to Analyze.
8734 Set_Analyzed (New_Decl);
8736 -- Insert and analyze the declaration for the constrained subtype
8738 if Constraint_Present then
8739 New_Indic :=
8740 Make_Subtype_Indication (Loc,
8741 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
8742 Constraint => Relocate_Node (Constraint (Indic)));
8744 else
8745 declare
8746 Constr_List : constant List_Id := New_List;
8747 C : Elmt_Id;
8748 Expr : Node_Id;
8750 begin
8751 C := First_Elmt (Discriminant_Constraint (Parent_Type));
8752 while Present (C) loop
8753 Expr := Node (C);
8755 -- It is safe here to call New_Copy_Tree since we called
8756 -- Force_Evaluation on each constraint previously
8757 -- in Build_Discriminant_Constraints.
8759 Append (New_Copy_Tree (Expr), To => Constr_List);
8761 Next_Elmt (C);
8762 end loop;
8764 New_Indic :=
8765 Make_Subtype_Indication (Loc,
8766 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
8767 Constraint =>
8768 Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
8769 end;
8770 end if;
8772 Rewrite (N,
8773 Make_Subtype_Declaration (Loc,
8774 Defining_Identifier => Derived_Type,
8775 Subtype_Indication => New_Indic));
8777 Analyze (N);
8779 -- Derivation of subprograms must be delayed until the full subtype
8780 -- has been established, to ensure proper overriding of subprograms
8781 -- inherited by full types. If the derivations occurred as part of
8782 -- the call to Build_Derived_Type above, then the check for type
8783 -- conformance would fail because earlier primitive subprograms
8784 -- could still refer to the full type prior the change to the new
8785 -- subtype and hence would not match the new base type created here.
8786 -- Subprograms are not derived, however, when Derive_Subps is False
8787 -- (since otherwise there could be redundant derivations).
8789 if Derive_Subps then
8790 Derive_Subprograms (Parent_Type, Derived_Type);
8791 end if;
8793 -- For tagged types the Discriminant_Constraint of the new base itype
8794 -- is inherited from the first subtype so that no subtype conformance
8795 -- problem arise when the first subtype overrides primitive
8796 -- operations inherited by the implicit base type.
8798 if Is_Tagged then
8799 Set_Discriminant_Constraint
8800 (New_Base, Discriminant_Constraint (Derived_Type));
8801 end if;
8803 return;
8804 end if;
8806 -- If we get here Derived_Type will have no discriminants or it will be
8807 -- a discriminated unconstrained base type.
8809 -- STEP 1a: perform preliminary actions/checks for derived tagged types
8811 if Is_Tagged then
8813 -- The parent type is frozen for non-private extensions (RM 13.14(7))
8814 -- The declaration of a specific descendant of an interface type
8815 -- freezes the interface type (RM 13.14).
8817 if not Private_Extension or else Is_Interface (Parent_Base) then
8818 Freeze_Before (N, Parent_Type);
8819 end if;
8821 -- In Ada 2005 (AI-344), the restriction that a derived tagged type
8822 -- cannot be declared at a deeper level than its parent type is
8823 -- removed. The check on derivation within a generic body is also
8824 -- relaxed, but there's a restriction that a derived tagged type
8825 -- cannot be declared in a generic body if it's derived directly
8826 -- or indirectly from a formal type of that generic.
8828 if Ada_Version >= Ada_2005 then
8829 if Present (Enclosing_Generic_Body (Derived_Type)) then
8830 declare
8831 Ancestor_Type : Entity_Id;
8833 begin
8834 -- Check to see if any ancestor of the derived type is a
8835 -- formal type.
8837 Ancestor_Type := Parent_Type;
8838 while not Is_Generic_Type (Ancestor_Type)
8839 and then Etype (Ancestor_Type) /= Ancestor_Type
8840 loop
8841 Ancestor_Type := Etype (Ancestor_Type);
8842 end loop;
8844 -- If the derived type does have a formal type as an
8845 -- ancestor, then it's an error if the derived type is
8846 -- declared within the body of the generic unit that
8847 -- declares the formal type in its generic formal part. It's
8848 -- sufficient to check whether the ancestor type is declared
8849 -- inside the same generic body as the derived type (such as
8850 -- within a nested generic spec), in which case the
8851 -- derivation is legal. If the formal type is declared
8852 -- outside of that generic body, then it's guaranteed that
8853 -- the derived type is declared within the generic body of
8854 -- the generic unit declaring the formal type.
8856 if Is_Generic_Type (Ancestor_Type)
8857 and then Enclosing_Generic_Body (Ancestor_Type) /=
8858 Enclosing_Generic_Body (Derived_Type)
8859 then
8860 Error_Msg_NE
8861 ("parent type of& must not be descendant of formal type"
8862 & " of an enclosing generic body",
8863 Indic, Derived_Type);
8864 end if;
8865 end;
8866 end if;
8868 elsif Type_Access_Level (Derived_Type) /=
8869 Type_Access_Level (Parent_Type)
8870 and then not Is_Generic_Type (Derived_Type)
8871 then
8872 if Is_Controlled (Parent_Type) then
8873 Error_Msg_N
8874 ("controlled type must be declared at the library level",
8875 Indic);
8876 else
8877 Error_Msg_N
8878 ("type extension at deeper accessibility level than parent",
8879 Indic);
8880 end if;
8882 else
8883 declare
8884 GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
8885 begin
8886 if Present (GB)
8887 and then GB /= Enclosing_Generic_Body (Parent_Base)
8888 then
8889 Error_Msg_NE
8890 ("parent type of& must not be outside generic body"
8891 & " (RM 3.9.1(4))",
8892 Indic, Derived_Type);
8893 end if;
8894 end;
8895 end if;
8896 end if;
8898 -- Ada 2005 (AI-251)
8900 if Ada_Version >= Ada_2005 and then Is_Tagged then
8902 -- "The declaration of a specific descendant of an interface type
8903 -- freezes the interface type" (RM 13.14).
8905 declare
8906 Iface : Node_Id;
8907 begin
8908 if Is_Non_Empty_List (Interface_List (Type_Def)) then
8909 Iface := First (Interface_List (Type_Def));
8910 while Present (Iface) loop
8911 Freeze_Before (N, Etype (Iface));
8912 Next (Iface);
8913 end loop;
8914 end if;
8915 end;
8916 end if;
8918 -- STEP 1b : preliminary cleanup of the full view of private types
8920 -- If the type is already marked as having discriminants, then it's the
8921 -- completion of a private type or private extension and we need to
8922 -- retain the discriminants from the partial view if the current
8923 -- declaration has Discriminant_Specifications so that we can verify
8924 -- conformance. However, we must remove any existing components that
8925 -- were inherited from the parent (and attached in Copy_And_Swap)
8926 -- because the full type inherits all appropriate components anyway, and
8927 -- we do not want the partial view's components interfering.
8929 if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
8930 Discrim := First_Discriminant (Derived_Type);
8931 loop
8932 Last_Discrim := Discrim;
8933 Next_Discriminant (Discrim);
8934 exit when No (Discrim);
8935 end loop;
8937 Set_Last_Entity (Derived_Type, Last_Discrim);
8939 -- In all other cases wipe out the list of inherited components (even
8940 -- inherited discriminants), it will be properly rebuilt here.
8942 else
8943 Set_First_Entity (Derived_Type, Empty);
8944 Set_Last_Entity (Derived_Type, Empty);
8945 end if;
8947 -- STEP 1c: Initialize some flags for the Derived_Type
8949 -- The following flags must be initialized here so that
8950 -- Process_Discriminants can check that discriminants of tagged types do
8951 -- not have a default initial value and that access discriminants are
8952 -- only specified for limited records. For completeness, these flags are
8953 -- also initialized along with all the other flags below.
8955 -- AI-419: Limitedness is not inherited from an interface parent, so to
8956 -- be limited in that case the type must be explicitly declared as
8957 -- limited. However, task and protected interfaces are always limited.
8959 if Limited_Present (Type_Def) then
8960 Set_Is_Limited_Record (Derived_Type);
8962 elsif Is_Limited_Record (Parent_Type)
8963 or else (Present (Full_View (Parent_Type))
8964 and then Is_Limited_Record (Full_View (Parent_Type)))
8965 then
8966 if not Is_Interface (Parent_Type)
8967 or else Is_Synchronized_Interface (Parent_Type)
8968 or else Is_Protected_Interface (Parent_Type)
8969 or else Is_Task_Interface (Parent_Type)
8970 then
8971 Set_Is_Limited_Record (Derived_Type);
8972 end if;
8973 end if;
8975 -- STEP 2a: process discriminants of derived type if any
8977 Push_Scope (Derived_Type);
8979 if Discriminant_Specs then
8980 Set_Has_Unknown_Discriminants (Derived_Type, False);
8982 -- The following call initializes fields Has_Discriminants and
8983 -- Discriminant_Constraint, unless we are processing the completion
8984 -- of a private type declaration.
8986 Check_Or_Process_Discriminants (N, Derived_Type);
8988 -- For untagged types, the constraint on the Parent_Type must be
8989 -- present and is used to rename the discriminants.
8991 if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
8992 Error_Msg_N ("untagged parent must have discriminants", Indic);
8994 elsif not Is_Tagged and then not Constraint_Present then
8995 Error_Msg_N
8996 ("discriminant constraint needed for derived untagged records",
8997 Indic);
8999 -- Otherwise the parent subtype must be constrained unless we have a
9000 -- private extension.
9002 elsif not Constraint_Present
9003 and then not Private_Extension
9004 and then not Is_Constrained (Parent_Type)
9005 then
9006 Error_Msg_N
9007 ("unconstrained type not allowed in this context", Indic);
9009 elsif Constraint_Present then
9010 -- The following call sets the field Corresponding_Discriminant
9011 -- for the discriminants in the Derived_Type.
9013 Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
9015 -- For untagged types all new discriminants must rename
9016 -- discriminants in the parent. For private extensions new
9017 -- discriminants cannot rename old ones (implied by [7.3(13)]).
9019 Discrim := First_Discriminant (Derived_Type);
9020 while Present (Discrim) loop
9021 if not Is_Tagged
9022 and then No (Corresponding_Discriminant (Discrim))
9023 then
9024 Error_Msg_N
9025 ("new discriminants must constrain old ones", Discrim);
9027 elsif Private_Extension
9028 and then Present (Corresponding_Discriminant (Discrim))
9029 then
9030 Error_Msg_N
9031 ("only static constraints allowed for parent"
9032 & " discriminants in the partial view", Indic);
9033 exit;
9034 end if;
9036 -- If a new discriminant is used in the constraint, then its
9037 -- subtype must be statically compatible with the parent
9038 -- discriminant's subtype (3.7(15)).
9040 -- However, if the record contains an array constrained by
9041 -- the discriminant but with some different bound, the compiler
9042 -- tries to create a smaller range for the discriminant type.
9043 -- (See exp_ch3.Adjust_Discriminants). In this case, where
9044 -- the discriminant type is a scalar type, the check must use
9045 -- the original discriminant type in the parent declaration.
9047 declare
9048 Corr_Disc : constant Entity_Id :=
9049 Corresponding_Discriminant (Discrim);
9050 Disc_Type : constant Entity_Id := Etype (Discrim);
9051 Corr_Type : Entity_Id;
9053 begin
9054 if Present (Corr_Disc) then
9055 if Is_Scalar_Type (Disc_Type) then
9056 Corr_Type :=
9057 Entity (Discriminant_Type (Parent (Corr_Disc)));
9058 else
9059 Corr_Type := Etype (Corr_Disc);
9060 end if;
9062 if not
9063 Subtypes_Statically_Compatible (Disc_Type, Corr_Type)
9064 then
9065 Error_Msg_N
9066 ("subtype must be compatible "
9067 & "with parent discriminant",
9068 Discrim);
9069 end if;
9070 end if;
9071 end;
9073 Next_Discriminant (Discrim);
9074 end loop;
9076 -- Check whether the constraints of the full view statically
9077 -- match those imposed by the parent subtype [7.3(13)].
9079 if Present (Stored_Constraint (Derived_Type)) then
9080 declare
9081 C1, C2 : Elmt_Id;
9083 begin
9084 C1 := First_Elmt (Discs);
9085 C2 := First_Elmt (Stored_Constraint (Derived_Type));
9086 while Present (C1) and then Present (C2) loop
9087 if not
9088 Fully_Conformant_Expressions (Node (C1), Node (C2))
9089 then
9090 Error_Msg_N
9091 ("not conformant with previous declaration",
9092 Node (C1));
9093 end if;
9095 Next_Elmt (C1);
9096 Next_Elmt (C2);
9097 end loop;
9098 end;
9099 end if;
9100 end if;
9102 -- STEP 2b: No new discriminants, inherit discriminants if any
9104 else
9105 if Private_Extension then
9106 Set_Has_Unknown_Discriminants
9107 (Derived_Type,
9108 Has_Unknown_Discriminants (Parent_Type)
9109 or else Unknown_Discriminants_Present (N));
9111 -- The partial view of the parent may have unknown discriminants,
9112 -- but if the full view has discriminants and the parent type is
9113 -- in scope they must be inherited.
9115 elsif Has_Unknown_Discriminants (Parent_Type)
9116 and then
9117 (not Has_Discriminants (Parent_Type)
9118 or else not In_Open_Scopes (Scope (Parent_Type)))
9119 then
9120 Set_Has_Unknown_Discriminants (Derived_Type);
9121 end if;
9123 if not Has_Unknown_Discriminants (Derived_Type)
9124 and then not Has_Unknown_Discriminants (Parent_Base)
9125 and then Has_Discriminants (Parent_Type)
9126 then
9127 Inherit_Discrims := True;
9128 Set_Has_Discriminants
9129 (Derived_Type, True);
9130 Set_Discriminant_Constraint
9131 (Derived_Type, Discriminant_Constraint (Parent_Base));
9132 end if;
9134 -- The following test is true for private types (remember
9135 -- transformation 5. is not applied to those) and in an error
9136 -- situation.
9138 if Constraint_Present then
9139 Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
9140 end if;
9142 -- For now mark a new derived type as constrained only if it has no
9143 -- discriminants. At the end of Build_Derived_Record_Type we properly
9144 -- set this flag in the case of private extensions. See comments in
9145 -- point 9. just before body of Build_Derived_Record_Type.
9147 Set_Is_Constrained
9148 (Derived_Type,
9149 not (Inherit_Discrims
9150 or else Has_Unknown_Discriminants (Derived_Type)));
9151 end if;
9153 -- STEP 3: initialize fields of derived type
9155 Set_Is_Tagged_Type (Derived_Type, Is_Tagged);
9156 Set_Stored_Constraint (Derived_Type, No_Elist);
9158 -- Ada 2005 (AI-251): Private type-declarations can implement interfaces
9159 -- but cannot be interfaces
9161 if not Private_Extension
9162 and then Ekind (Derived_Type) /= E_Private_Type
9163 and then Ekind (Derived_Type) /= E_Limited_Private_Type
9164 then
9165 if Interface_Present (Type_Def) then
9166 Analyze_Interface_Declaration (Derived_Type, Type_Def);
9167 end if;
9169 Set_Interfaces (Derived_Type, No_Elist);
9170 end if;
9172 -- Fields inherited from the Parent_Type
9174 Set_Has_Specified_Layout
9175 (Derived_Type, Has_Specified_Layout (Parent_Type));
9176 Set_Is_Limited_Composite
9177 (Derived_Type, Is_Limited_Composite (Parent_Type));
9178 Set_Is_Private_Composite
9179 (Derived_Type, Is_Private_Composite (Parent_Type));
9181 if Is_Tagged_Type (Parent_Type) then
9182 Set_No_Tagged_Streams_Pragma
9183 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
9184 end if;
9186 -- Fields inherited from the Parent_Base
9188 Set_Has_Controlled_Component
9189 (Derived_Type, Has_Controlled_Component (Parent_Base));
9190 Set_Has_Non_Standard_Rep
9191 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
9192 Set_Has_Primitive_Operations
9193 (Derived_Type, Has_Primitive_Operations (Parent_Base));
9195 -- Set fields for private derived types
9197 if Is_Private_Type (Derived_Type) then
9198 Set_Depends_On_Private (Derived_Type, True);
9199 Set_Private_Dependents (Derived_Type, New_Elmt_List);
9200 end if;
9202 -- Inherit fields for non-private types. If this is the completion of a
9203 -- derivation from a private type, the parent itself is private and the
9204 -- attributes come from its full view, which must be present.
9206 if Is_Record_Type (Derived_Type) then
9207 declare
9208 Parent_Full : Entity_Id;
9210 begin
9211 if Is_Private_Type (Parent_Base)
9212 and then not Is_Record_Type (Parent_Base)
9213 then
9214 Parent_Full := Full_View (Parent_Base);
9215 else
9216 Parent_Full := Parent_Base;
9217 end if;
9219 Set_Component_Alignment
9220 (Derived_Type, Component_Alignment (Parent_Full));
9221 Set_C_Pass_By_Copy
9222 (Derived_Type, C_Pass_By_Copy (Parent_Full));
9223 Set_Has_Complex_Representation
9224 (Derived_Type, Has_Complex_Representation (Parent_Full));
9226 -- For untagged types, inherit the layout by default to avoid
9227 -- costly changes of representation for type conversions.
9229 if not Is_Tagged then
9230 Set_Is_Packed (Derived_Type, Is_Packed (Parent_Full));
9231 Set_No_Reordering (Derived_Type, No_Reordering (Parent_Full));
9232 end if;
9233 end;
9234 end if;
9236 -- Set fields for tagged types
9238 if Is_Tagged then
9239 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
9241 -- All tagged types defined in Ada.Finalization are controlled
9243 if Chars (Scope (Derived_Type)) = Name_Finalization
9244 and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
9245 and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
9246 then
9247 Set_Is_Controlled_Active (Derived_Type);
9248 else
9249 Set_Is_Controlled_Active
9250 (Derived_Type, Is_Controlled_Active (Parent_Base));
9251 end if;
9253 -- Minor optimization: there is no need to generate the class-wide
9254 -- entity associated with an underlying record view.
9256 if not Is_Underlying_Record_View (Derived_Type) then
9257 Make_Class_Wide_Type (Derived_Type);
9258 end if;
9260 Set_Is_Abstract_Type (Derived_Type, Abstract_Present (Type_Def));
9262 if Has_Discriminants (Derived_Type)
9263 and then Constraint_Present
9264 then
9265 Set_Stored_Constraint
9266 (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
9267 end if;
9269 if Ada_Version >= Ada_2005 then
9270 declare
9271 Ifaces_List : Elist_Id;
9273 begin
9274 -- Checks rules 3.9.4 (13/2 and 14/2)
9276 if Comes_From_Source (Derived_Type)
9277 and then not Is_Private_Type (Derived_Type)
9278 and then Is_Interface (Parent_Type)
9279 and then not Is_Interface (Derived_Type)
9280 then
9281 if Is_Task_Interface (Parent_Type) then
9282 Error_Msg_N
9283 ("(Ada 2005) task type required (RM 3.9.4 (13.2))",
9284 Derived_Type);
9286 elsif Is_Protected_Interface (Parent_Type) then
9287 Error_Msg_N
9288 ("(Ada 2005) protected type required (RM 3.9.4 (14.2))",
9289 Derived_Type);
9290 end if;
9291 end if;
9293 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
9295 Check_Interfaces (N, Type_Def);
9297 -- Ada 2005 (AI-251): Collect the list of progenitors that are
9298 -- not already in the parents.
9300 Collect_Interfaces
9301 (T => Derived_Type,
9302 Ifaces_List => Ifaces_List,
9303 Exclude_Parents => True);
9305 Set_Interfaces (Derived_Type, Ifaces_List);
9307 -- If the derived type is the anonymous type created for
9308 -- a declaration whose parent has a constraint, propagate
9309 -- the interface list to the source type. This must be done
9310 -- prior to the completion of the analysis of the source type
9311 -- because the components in the extension may contain current
9312 -- instances whose legality depends on some ancestor.
9314 if Is_Itype (Derived_Type) then
9315 declare
9316 Def : constant Node_Id :=
9317 Associated_Node_For_Itype (Derived_Type);
9318 begin
9319 if Present (Def)
9320 and then Nkind (Def) = N_Full_Type_Declaration
9321 then
9322 Set_Interfaces
9323 (Defining_Identifier (Def), Ifaces_List);
9324 end if;
9325 end;
9326 end if;
9328 -- A type extension is automatically Ghost when one of its
9329 -- progenitors is Ghost (SPARK RM 6.9(9)). This property is
9330 -- also inherited when the parent type is Ghost, but this is
9331 -- done in Build_Derived_Type as the mechanism also handles
9332 -- untagged derivations.
9334 if Implements_Ghost_Interface (Derived_Type) then
9335 Set_Is_Ghost_Entity (Derived_Type);
9336 end if;
9337 end;
9338 end if;
9339 end if;
9341 -- STEP 4: Inherit components from the parent base and constrain them.
9342 -- Apply the second transformation described in point 6. above.
9344 if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
9345 or else not Has_Discriminants (Parent_Type)
9346 or else not Is_Constrained (Parent_Type)
9347 then
9348 Constrs := Discs;
9349 else
9350 Constrs := Discriminant_Constraint (Parent_Type);
9351 end if;
9353 Assoc_List :=
9354 Inherit_Components
9355 (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
9357 -- STEP 5a: Copy the parent record declaration for untagged types
9359 Set_Has_Implicit_Dereference
9360 (Derived_Type, Has_Implicit_Dereference (Parent_Type));
9362 if not Is_Tagged then
9364 -- Discriminant_Constraint (Derived_Type) has been properly
9365 -- constructed. Save it and temporarily set it to Empty because we
9366 -- do not want the call to New_Copy_Tree below to mess this list.
9368 if Has_Discriminants (Derived_Type) then
9369 Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
9370 Set_Discriminant_Constraint (Derived_Type, No_Elist);
9371 else
9372 Save_Discr_Constr := No_Elist;
9373 end if;
9375 -- Save the Etype field of Derived_Type. It is correctly set now,
9376 -- but the call to New_Copy tree may remap it to point to itself,
9377 -- which is not what we want. Ditto for the Next_Entity field.
9379 Save_Etype := Etype (Derived_Type);
9380 Save_Next_Entity := Next_Entity (Derived_Type);
9382 -- Assoc_List maps all stored discriminants in the Parent_Base to
9383 -- stored discriminants in the Derived_Type. It is fundamental that
9384 -- no types or itypes with discriminants other than the stored
9385 -- discriminants appear in the entities declared inside
9386 -- Derived_Type, since the back end cannot deal with it.
9388 New_Decl :=
9389 New_Copy_Tree
9390 (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
9391 Copy_Dimensions_Of_Components (Derived_Type);
9393 -- Restore the fields saved prior to the New_Copy_Tree call
9394 -- and compute the stored constraint.
9396 Set_Etype (Derived_Type, Save_Etype);
9397 Set_Next_Entity (Derived_Type, Save_Next_Entity);
9399 if Has_Discriminants (Derived_Type) then
9400 Set_Discriminant_Constraint
9401 (Derived_Type, Save_Discr_Constr);
9402 Set_Stored_Constraint
9403 (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
9404 Replace_Components (Derived_Type, New_Decl);
9405 end if;
9407 -- Insert the new derived type declaration
9409 Rewrite (N, New_Decl);
9411 -- STEP 5b: Complete the processing for record extensions in generics
9413 -- There is no completion for record extensions declared in the
9414 -- parameter part of a generic, so we need to complete processing for
9415 -- these generic record extensions here. The Record_Type_Definition call
9416 -- will change the Ekind of the components from E_Void to E_Component.
9418 elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
9419 Record_Type_Definition (Empty, Derived_Type);
9421 -- STEP 5c: Process the record extension for non private tagged types
9423 elsif not Private_Extension then
9424 Expand_Record_Extension (Derived_Type, Type_Def);
9426 -- Note : previously in ASIS mode we set the Parent_Subtype of the
9427 -- derived type to propagate some semantic information. This led
9428 -- to other ASIS failures and has been removed.
9430 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
9431 -- implemented interfaces if we are in expansion mode
9433 if Expander_Active
9434 and then Has_Interfaces (Derived_Type)
9435 then
9436 Add_Interface_Tag_Components (N, Derived_Type);
9437 end if;
9439 -- Analyze the record extension
9441 Record_Type_Definition
9442 (Record_Extension_Part (Type_Def), Derived_Type);
9443 end if;
9445 End_Scope;
9447 -- Nothing else to do if there is an error in the derivation.
9448 -- An unusual case: the full view may be derived from a type in an
9449 -- instance, when the partial view was used illegally as an actual
9450 -- in that instance, leading to a circular definition.
9452 if Etype (Derived_Type) = Any_Type
9453 or else Etype (Parent_Type) = Derived_Type
9454 then
9455 return;
9456 end if;
9458 -- Set delayed freeze and then derive subprograms, we need to do
9459 -- this in this order so that derived subprograms inherit the
9460 -- derived freeze if necessary.
9462 Set_Has_Delayed_Freeze (Derived_Type);
9464 if Derive_Subps then
9465 Derive_Subprograms (Parent_Type, Derived_Type);
9466 end if;
9468 -- If we have a private extension which defines a constrained derived
9469 -- type mark as constrained here after we have derived subprograms. See
9470 -- comment on point 9. just above the body of Build_Derived_Record_Type.
9472 if Private_Extension and then Inherit_Discrims then
9473 if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
9474 Set_Is_Constrained (Derived_Type, True);
9475 Set_Discriminant_Constraint (Derived_Type, Discs);
9477 elsif Is_Constrained (Parent_Type) then
9478 Set_Is_Constrained
9479 (Derived_Type, True);
9480 Set_Discriminant_Constraint
9481 (Derived_Type, Discriminant_Constraint (Parent_Type));
9482 end if;
9483 end if;
9485 -- Update the class-wide type, which shares the now-completed entity
9486 -- list with its specific type. In case of underlying record views,
9487 -- we do not generate the corresponding class wide entity.
9489 if Is_Tagged
9490 and then not Is_Underlying_Record_View (Derived_Type)
9491 then
9492 Set_First_Entity
9493 (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
9494 Set_Last_Entity
9495 (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
9496 end if;
9498 Check_Function_Writable_Actuals (N);
9499 end Build_Derived_Record_Type;
9501 ------------------------
9502 -- Build_Derived_Type --
9503 ------------------------
9505 procedure Build_Derived_Type
9506 (N : Node_Id;
9507 Parent_Type : Entity_Id;
9508 Derived_Type : Entity_Id;
9509 Is_Completion : Boolean;
9510 Derive_Subps : Boolean := True)
9512 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
9514 begin
9515 -- Set common attributes
9517 Set_Scope (Derived_Type, Current_Scope);
9518 Set_Etype (Derived_Type, Parent_Base);
9519 Set_Ekind (Derived_Type, Ekind (Parent_Base));
9520 Propagate_Concurrent_Flags (Derived_Type, Parent_Base);
9522 Set_Size_Info (Derived_Type, Parent_Type);
9523 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
9525 Set_Is_Controlled_Active
9526 (Derived_Type, Is_Controlled_Active (Parent_Type));
9528 Set_Disable_Controlled (Derived_Type, Disable_Controlled (Parent_Type));
9529 Set_Is_Tagged_Type (Derived_Type, Is_Tagged_Type (Parent_Type));
9530 Set_Is_Volatile (Derived_Type, Is_Volatile (Parent_Type));
9532 if Is_Tagged_Type (Derived_Type) then
9533 Set_No_Tagged_Streams_Pragma
9534 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
9535 end if;
9537 -- If the parent has primitive routines, set the derived type link
9539 if Has_Primitive_Operations (Parent_Type) then
9540 Set_Derived_Type_Link (Parent_Base, Derived_Type);
9541 end if;
9543 -- If the parent type is a private subtype, the convention on the base
9544 -- type may be set in the private part, and not propagated to the
9545 -- subtype until later, so we obtain the convention from the base type.
9547 Set_Convention (Derived_Type, Convention (Parent_Base));
9549 -- Set SSO default for record or array type
9551 if (Is_Array_Type (Derived_Type) or else Is_Record_Type (Derived_Type))
9552 and then Is_Base_Type (Derived_Type)
9553 then
9554 Set_Default_SSO (Derived_Type);
9555 end if;
9557 -- A derived type inherits the Default_Initial_Condition pragma coming
9558 -- from any parent type within the derivation chain.
9560 if Has_DIC (Parent_Type) then
9561 Set_Has_Inherited_DIC (Derived_Type);
9562 end if;
9564 -- A derived type inherits any class-wide invariants coming from a
9565 -- parent type or an interface. Note that the invariant procedure of
9566 -- the parent type should not be inherited because the derived type may
9567 -- define invariants of its own.
9569 if not Is_Interface (Derived_Type) then
9570 if Has_Inherited_Invariants (Parent_Type)
9571 or else Has_Inheritable_Invariants (Parent_Type)
9572 then
9573 Set_Has_Inherited_Invariants (Derived_Type);
9575 elsif Is_Concurrent_Type (Derived_Type)
9576 or else Is_Tagged_Type (Derived_Type)
9577 then
9578 declare
9579 Iface : Entity_Id;
9580 Ifaces : Elist_Id;
9581 Iface_Elmt : Elmt_Id;
9583 begin
9584 Collect_Interfaces
9585 (T => Derived_Type,
9586 Ifaces_List => Ifaces,
9587 Exclude_Parents => True);
9589 if Present (Ifaces) then
9590 Iface_Elmt := First_Elmt (Ifaces);
9591 while Present (Iface_Elmt) loop
9592 Iface := Node (Iface_Elmt);
9594 if Has_Inheritable_Invariants (Iface) then
9595 Set_Has_Inherited_Invariants (Derived_Type);
9596 exit;
9597 end if;
9599 Next_Elmt (Iface_Elmt);
9600 end loop;
9601 end if;
9602 end;
9603 end if;
9604 end if;
9606 -- We similarly inherit predicates. Note that for scalar derived types
9607 -- the predicate is inherited from the first subtype, and not from its
9608 -- (anonymous) base type.
9610 if Has_Predicates (Parent_Type)
9611 or else Has_Predicates (First_Subtype (Parent_Type))
9612 then
9613 Set_Has_Predicates (Derived_Type);
9614 end if;
9616 -- The derived type inherits representation clauses from the parent
9617 -- type, and from any interfaces.
9619 Inherit_Rep_Item_Chain (Derived_Type, Parent_Type);
9621 declare
9622 Iface : Node_Id := First (Abstract_Interface_List (Derived_Type));
9623 begin
9624 while Present (Iface) loop
9625 Inherit_Rep_Item_Chain (Derived_Type, Entity (Iface));
9626 Next (Iface);
9627 end loop;
9628 end;
9630 -- If the parent type has delayed rep aspects, then mark the derived
9631 -- type as possibly inheriting a delayed rep aspect.
9633 if Has_Delayed_Rep_Aspects (Parent_Type) then
9634 Set_May_Inherit_Delayed_Rep_Aspects (Derived_Type);
9635 end if;
9637 -- A derived type becomes Ghost when its parent type is also Ghost
9638 -- (SPARK RM 6.9(9)). Note that the Ghost-related attributes are not
9639 -- directly inherited because the Ghost policy in effect may differ.
9641 if Is_Ghost_Entity (Parent_Type) then
9642 Set_Is_Ghost_Entity (Derived_Type);
9643 end if;
9645 -- Type dependent processing
9647 case Ekind (Parent_Type) is
9648 when Numeric_Kind =>
9649 Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
9651 when Array_Kind =>
9652 Build_Derived_Array_Type (N, Parent_Type, Derived_Type);
9654 when Class_Wide_Kind
9655 | E_Record_Subtype
9656 | E_Record_Type
9658 Build_Derived_Record_Type
9659 (N, Parent_Type, Derived_Type, Derive_Subps);
9660 return;
9662 when Enumeration_Kind =>
9663 Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
9665 when Access_Kind =>
9666 Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
9668 when Incomplete_Or_Private_Kind =>
9669 Build_Derived_Private_Type
9670 (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
9672 -- For discriminated types, the derivation includes deriving
9673 -- primitive operations. For others it is done below.
9675 if Is_Tagged_Type (Parent_Type)
9676 or else Has_Discriminants (Parent_Type)
9677 or else (Present (Full_View (Parent_Type))
9678 and then Has_Discriminants (Full_View (Parent_Type)))
9679 then
9680 return;
9681 end if;
9683 when Concurrent_Kind =>
9684 Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
9686 when others =>
9687 raise Program_Error;
9688 end case;
9690 -- Nothing more to do if some error occurred
9692 if Etype (Derived_Type) = Any_Type then
9693 return;
9694 end if;
9696 -- Set delayed freeze and then derive subprograms, we need to do this
9697 -- in this order so that derived subprograms inherit the derived freeze
9698 -- if necessary.
9700 Set_Has_Delayed_Freeze (Derived_Type);
9702 if Derive_Subps then
9703 Derive_Subprograms (Parent_Type, Derived_Type);
9704 end if;
9706 Set_Has_Primitive_Operations
9707 (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
9708 end Build_Derived_Type;
9710 -----------------------
9711 -- Build_Discriminal --
9712 -----------------------
9714 procedure Build_Discriminal (Discrim : Entity_Id) is
9715 D_Minal : Entity_Id;
9716 CR_Disc : Entity_Id;
9718 begin
9719 -- A discriminal has the same name as the discriminant
9721 D_Minal := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9723 Set_Ekind (D_Minal, E_In_Parameter);
9724 Set_Mechanism (D_Minal, Default_Mechanism);
9725 Set_Etype (D_Minal, Etype (Discrim));
9726 Set_Scope (D_Minal, Current_Scope);
9727 Set_Parent (D_Minal, Parent (Discrim));
9729 Set_Discriminal (Discrim, D_Minal);
9730 Set_Discriminal_Link (D_Minal, Discrim);
9732 -- For task types, build at once the discriminants of the corresponding
9733 -- record, which are needed if discriminants are used in entry defaults
9734 -- and in family bounds.
9736 if Is_Concurrent_Type (Current_Scope)
9737 or else
9738 Is_Limited_Type (Current_Scope)
9739 then
9740 CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9742 Set_Ekind (CR_Disc, E_In_Parameter);
9743 Set_Mechanism (CR_Disc, Default_Mechanism);
9744 Set_Etype (CR_Disc, Etype (Discrim));
9745 Set_Scope (CR_Disc, Current_Scope);
9746 Set_Discriminal_Link (CR_Disc, Discrim);
9747 Set_CR_Discriminant (Discrim, CR_Disc);
9748 end if;
9749 end Build_Discriminal;
9751 ------------------------------------
9752 -- Build_Discriminant_Constraints --
9753 ------------------------------------
9755 function Build_Discriminant_Constraints
9756 (T : Entity_Id;
9757 Def : Node_Id;
9758 Derived_Def : Boolean := False) return Elist_Id
9760 C : constant Node_Id := Constraint (Def);
9761 Nb_Discr : constant Nat := Number_Discriminants (T);
9763 Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
9764 -- Saves the expression corresponding to a given discriminant in T
9766 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
9767 -- Return the Position number within array Discr_Expr of a discriminant
9768 -- D within the discriminant list of the discriminated type T.
9770 procedure Process_Discriminant_Expression
9771 (Expr : Node_Id;
9772 D : Entity_Id);
9773 -- If this is a discriminant constraint on a partial view, do not
9774 -- generate an overflow check on the discriminant expression. The check
9775 -- will be generated when constraining the full view. Otherwise the
9776 -- backend creates duplicate symbols for the temporaries corresponding
9777 -- to the expressions to be checked, causing spurious assembler errors.
9779 ------------------
9780 -- Pos_Of_Discr --
9781 ------------------
9783 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
9784 Disc : Entity_Id;
9786 begin
9787 Disc := First_Discriminant (T);
9788 for J in Discr_Expr'Range loop
9789 if Disc = D then
9790 return J;
9791 end if;
9793 Next_Discriminant (Disc);
9794 end loop;
9796 -- Note: Since this function is called on discriminants that are
9797 -- known to belong to the discriminated type, falling through the
9798 -- loop with no match signals an internal compiler error.
9800 raise Program_Error;
9801 end Pos_Of_Discr;
9803 -------------------------------------
9804 -- Process_Discriminant_Expression --
9805 -------------------------------------
9807 procedure Process_Discriminant_Expression
9808 (Expr : Node_Id;
9809 D : Entity_Id)
9811 BDT : constant Entity_Id := Base_Type (Etype (D));
9813 begin
9814 -- If this is a discriminant constraint on a partial view, do
9815 -- not generate an overflow on the discriminant expression. The
9816 -- check will be generated when constraining the full view.
9818 if Is_Private_Type (T)
9819 and then Present (Full_View (T))
9820 then
9821 Analyze_And_Resolve (Expr, BDT, Suppress => Overflow_Check);
9822 else
9823 Analyze_And_Resolve (Expr, BDT);
9824 end if;
9825 end Process_Discriminant_Expression;
9827 -- Declarations local to Build_Discriminant_Constraints
9829 Discr : Entity_Id;
9830 E : Entity_Id;
9831 Elist : constant Elist_Id := New_Elmt_List;
9833 Constr : Node_Id;
9834 Expr : Node_Id;
9835 Id : Node_Id;
9836 Position : Nat;
9837 Found : Boolean;
9839 Discrim_Present : Boolean := False;
9841 -- Start of processing for Build_Discriminant_Constraints
9843 begin
9844 -- The following loop will process positional associations only.
9845 -- For a positional association, the (single) discriminant is
9846 -- implicitly specified by position, in textual order (RM 3.7.2).
9848 Discr := First_Discriminant (T);
9849 Constr := First (Constraints (C));
9850 for D in Discr_Expr'Range loop
9851 exit when Nkind (Constr) = N_Discriminant_Association;
9853 if No (Constr) then
9854 Error_Msg_N ("too few discriminants given in constraint", C);
9855 return New_Elmt_List;
9857 elsif Nkind (Constr) = N_Range
9858 or else (Nkind (Constr) = N_Attribute_Reference
9859 and then Attribute_Name (Constr) = Name_Range)
9860 then
9861 Error_Msg_N
9862 ("a range is not a valid discriminant constraint", Constr);
9863 Discr_Expr (D) := Error;
9865 else
9866 Process_Discriminant_Expression (Constr, Discr);
9867 Discr_Expr (D) := Constr;
9868 end if;
9870 Next_Discriminant (Discr);
9871 Next (Constr);
9872 end loop;
9874 if No (Discr) and then Present (Constr) then
9875 Error_Msg_N ("too many discriminants given in constraint", Constr);
9876 return New_Elmt_List;
9877 end if;
9879 -- Named associations can be given in any order, but if both positional
9880 -- and named associations are used in the same discriminant constraint,
9881 -- then positional associations must occur first, at their normal
9882 -- position. Hence once a named association is used, the rest of the
9883 -- discriminant constraint must use only named associations.
9885 while Present (Constr) loop
9887 -- Positional association forbidden after a named association
9889 if Nkind (Constr) /= N_Discriminant_Association then
9890 Error_Msg_N ("positional association follows named one", Constr);
9891 return New_Elmt_List;
9893 -- Otherwise it is a named association
9895 else
9896 -- E records the type of the discriminants in the named
9897 -- association. All the discriminants specified in the same name
9898 -- association must have the same type.
9900 E := Empty;
9902 -- Search the list of discriminants in T to see if the simple name
9903 -- given in the constraint matches any of them.
9905 Id := First (Selector_Names (Constr));
9906 while Present (Id) loop
9907 Found := False;
9909 -- If Original_Discriminant is present, we are processing a
9910 -- generic instantiation and this is an instance node. We need
9911 -- to find the name of the corresponding discriminant in the
9912 -- actual record type T and not the name of the discriminant in
9913 -- the generic formal. Example:
9915 -- generic
9916 -- type G (D : int) is private;
9917 -- package P is
9918 -- subtype W is G (D => 1);
9919 -- end package;
9920 -- type Rec (X : int) is record ... end record;
9921 -- package Q is new P (G => Rec);
9923 -- At the point of the instantiation, formal type G is Rec
9924 -- and therefore when reanalyzing "subtype W is G (D => 1);"
9925 -- which really looks like "subtype W is Rec (D => 1);" at
9926 -- the point of instantiation, we want to find the discriminant
9927 -- that corresponds to D in Rec, i.e. X.
9929 if Present (Original_Discriminant (Id))
9930 and then In_Instance
9931 then
9932 Discr := Find_Corresponding_Discriminant (Id, T);
9933 Found := True;
9935 else
9936 Discr := First_Discriminant (T);
9937 while Present (Discr) loop
9938 if Chars (Discr) = Chars (Id) then
9939 Found := True;
9940 exit;
9941 end if;
9943 Next_Discriminant (Discr);
9944 end loop;
9946 if not Found then
9947 Error_Msg_N ("& does not match any discriminant", Id);
9948 return New_Elmt_List;
9950 -- If the parent type is a generic formal, preserve the
9951 -- name of the discriminant for subsequent instances.
9952 -- see comment at the beginning of this if statement.
9954 elsif Is_Generic_Type (Root_Type (T)) then
9955 Set_Original_Discriminant (Id, Discr);
9956 end if;
9957 end if;
9959 Position := Pos_Of_Discr (T, Discr);
9961 if Present (Discr_Expr (Position)) then
9962 Error_Msg_N ("duplicate constraint for discriminant&", Id);
9964 else
9965 -- Each discriminant specified in the same named association
9966 -- must be associated with a separate copy of the
9967 -- corresponding expression.
9969 if Present (Next (Id)) then
9970 Expr := New_Copy_Tree (Expression (Constr));
9971 Set_Parent (Expr, Parent (Expression (Constr)));
9972 else
9973 Expr := Expression (Constr);
9974 end if;
9976 Discr_Expr (Position) := Expr;
9977 Process_Discriminant_Expression (Expr, Discr);
9978 end if;
9980 -- A discriminant association with more than one discriminant
9981 -- name is only allowed if the named discriminants are all of
9982 -- the same type (RM 3.7.1(8)).
9984 if E = Empty then
9985 E := Base_Type (Etype (Discr));
9987 elsif Base_Type (Etype (Discr)) /= E then
9988 Error_Msg_N
9989 ("all discriminants in an association " &
9990 "must have the same type", Id);
9991 end if;
9993 Next (Id);
9994 end loop;
9995 end if;
9997 Next (Constr);
9998 end loop;
10000 -- A discriminant constraint must provide exactly one value for each
10001 -- discriminant of the type (RM 3.7.1(8)).
10003 for J in Discr_Expr'Range loop
10004 if No (Discr_Expr (J)) then
10005 Error_Msg_N ("too few discriminants given in constraint", C);
10006 return New_Elmt_List;
10007 end if;
10008 end loop;
10010 -- Determine if there are discriminant expressions in the constraint
10012 for J in Discr_Expr'Range loop
10013 if Denotes_Discriminant
10014 (Discr_Expr (J), Check_Concurrent => True)
10015 then
10016 Discrim_Present := True;
10017 end if;
10018 end loop;
10020 -- Build an element list consisting of the expressions given in the
10021 -- discriminant constraint and apply the appropriate checks. The list
10022 -- is constructed after resolving any named discriminant associations
10023 -- and therefore the expressions appear in the textual order of the
10024 -- discriminants.
10026 Discr := First_Discriminant (T);
10027 for J in Discr_Expr'Range loop
10028 if Discr_Expr (J) /= Error then
10029 Append_Elmt (Discr_Expr (J), Elist);
10031 -- If any of the discriminant constraints is given by a
10032 -- discriminant and we are in a derived type declaration we
10033 -- have a discriminant renaming. Establish link between new
10034 -- and old discriminant. The new discriminant has an implicit
10035 -- dereference if the old one does.
10037 if Denotes_Discriminant (Discr_Expr (J)) then
10038 if Derived_Def then
10039 declare
10040 New_Discr : constant Entity_Id := Entity (Discr_Expr (J));
10042 begin
10043 Set_Corresponding_Discriminant (New_Discr, Discr);
10044 Set_Has_Implicit_Dereference (New_Discr,
10045 Has_Implicit_Dereference (Discr));
10046 end;
10047 end if;
10049 -- Force the evaluation of non-discriminant expressions.
10050 -- If we have found a discriminant in the constraint 3.4(26)
10051 -- and 3.8(18) demand that no range checks are performed are
10052 -- after evaluation. If the constraint is for a component
10053 -- definition that has a per-object constraint, expressions are
10054 -- evaluated but not checked either. In all other cases perform
10055 -- a range check.
10057 else
10058 if Discrim_Present then
10059 null;
10061 elsif Nkind (Parent (Parent (Def))) = N_Component_Declaration
10062 and then Has_Per_Object_Constraint
10063 (Defining_Identifier (Parent (Parent (Def))))
10064 then
10065 null;
10067 elsif Is_Access_Type (Etype (Discr)) then
10068 Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
10070 else
10071 Apply_Range_Check (Discr_Expr (J), Etype (Discr));
10072 end if;
10074 Force_Evaluation (Discr_Expr (J));
10075 end if;
10077 -- Check that the designated type of an access discriminant's
10078 -- expression is not a class-wide type unless the discriminant's
10079 -- designated type is also class-wide.
10081 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
10082 and then not Is_Class_Wide_Type
10083 (Designated_Type (Etype (Discr)))
10084 and then Etype (Discr_Expr (J)) /= Any_Type
10085 and then Is_Class_Wide_Type
10086 (Designated_Type (Etype (Discr_Expr (J))))
10087 then
10088 Wrong_Type (Discr_Expr (J), Etype (Discr));
10090 elsif Is_Access_Type (Etype (Discr))
10091 and then not Is_Access_Constant (Etype (Discr))
10092 and then Is_Access_Type (Etype (Discr_Expr (J)))
10093 and then Is_Access_Constant (Etype (Discr_Expr (J)))
10094 then
10095 Error_Msg_NE
10096 ("constraint for discriminant& must be access to variable",
10097 Def, Discr);
10098 end if;
10099 end if;
10101 Next_Discriminant (Discr);
10102 end loop;
10104 return Elist;
10105 end Build_Discriminant_Constraints;
10107 ---------------------------------
10108 -- Build_Discriminated_Subtype --
10109 ---------------------------------
10111 procedure Build_Discriminated_Subtype
10112 (T : Entity_Id;
10113 Def_Id : Entity_Id;
10114 Elist : Elist_Id;
10115 Related_Nod : Node_Id;
10116 For_Access : Boolean := False)
10118 Has_Discrs : constant Boolean := Has_Discriminants (T);
10119 Constrained : constant Boolean :=
10120 (Has_Discrs
10121 and then not Is_Empty_Elmt_List (Elist)
10122 and then not Is_Class_Wide_Type (T))
10123 or else Is_Constrained (T);
10125 begin
10126 if Ekind (T) = E_Record_Type then
10127 if For_Access then
10128 Set_Ekind (Def_Id, E_Private_Subtype);
10129 Set_Is_For_Access_Subtype (Def_Id, True);
10130 else
10131 Set_Ekind (Def_Id, E_Record_Subtype);
10132 end if;
10134 -- Inherit preelaboration flag from base, for types for which it
10135 -- may have been set: records, private types, protected types.
10137 Set_Known_To_Have_Preelab_Init
10138 (Def_Id, Known_To_Have_Preelab_Init (T));
10140 elsif Ekind (T) = E_Task_Type then
10141 Set_Ekind (Def_Id, E_Task_Subtype);
10143 elsif Ekind (T) = E_Protected_Type then
10144 Set_Ekind (Def_Id, E_Protected_Subtype);
10145 Set_Known_To_Have_Preelab_Init
10146 (Def_Id, Known_To_Have_Preelab_Init (T));
10148 elsif Is_Private_Type (T) then
10149 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
10150 Set_Known_To_Have_Preelab_Init
10151 (Def_Id, Known_To_Have_Preelab_Init (T));
10153 -- Private subtypes may have private dependents
10155 Set_Private_Dependents (Def_Id, New_Elmt_List);
10157 elsif Is_Class_Wide_Type (T) then
10158 Set_Ekind (Def_Id, E_Class_Wide_Subtype);
10160 else
10161 -- Incomplete type. Attach subtype to list of dependents, to be
10162 -- completed with full view of parent type, unless is it the
10163 -- designated subtype of a record component within an init_proc.
10164 -- This last case arises for a component of an access type whose
10165 -- designated type is incomplete (e.g. a Taft Amendment type).
10166 -- The designated subtype is within an inner scope, and needs no
10167 -- elaboration, because only the access type is needed in the
10168 -- initialization procedure.
10170 if Ekind (T) = E_Incomplete_Type then
10171 Set_Ekind (Def_Id, E_Incomplete_Subtype);
10172 else
10173 Set_Ekind (Def_Id, Ekind (T));
10174 end if;
10176 if For_Access and then Within_Init_Proc then
10177 null;
10178 else
10179 Append_Elmt (Def_Id, Private_Dependents (T));
10180 end if;
10181 end if;
10183 Set_Etype (Def_Id, T);
10184 Init_Size_Align (Def_Id);
10185 Set_Has_Discriminants (Def_Id, Has_Discrs);
10186 Set_Is_Constrained (Def_Id, Constrained);
10188 Set_First_Entity (Def_Id, First_Entity (T));
10189 Set_Last_Entity (Def_Id, Last_Entity (T));
10190 Set_Has_Implicit_Dereference
10191 (Def_Id, Has_Implicit_Dereference (T));
10192 Set_Has_Pragma_Unreferenced_Objects
10193 (Def_Id, Has_Pragma_Unreferenced_Objects (T));
10195 -- If the subtype is the completion of a private declaration, there may
10196 -- have been representation clauses for the partial view, and they must
10197 -- be preserved. Build_Derived_Type chains the inherited clauses with
10198 -- the ones appearing on the extension. If this comes from a subtype
10199 -- declaration, all clauses are inherited.
10201 if No (First_Rep_Item (Def_Id)) then
10202 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10203 end if;
10205 if Is_Tagged_Type (T) then
10206 Set_Is_Tagged_Type (Def_Id);
10207 Set_No_Tagged_Streams_Pragma (Def_Id, No_Tagged_Streams_Pragma (T));
10208 Make_Class_Wide_Type (Def_Id);
10209 end if;
10211 Set_Stored_Constraint (Def_Id, No_Elist);
10213 if Has_Discrs then
10214 Set_Discriminant_Constraint (Def_Id, Elist);
10215 Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
10216 end if;
10218 if Is_Tagged_Type (T) then
10220 -- Ada 2005 (AI-251): In case of concurrent types we inherit the
10221 -- concurrent record type (which has the list of primitive
10222 -- operations).
10224 if Ada_Version >= Ada_2005
10225 and then Is_Concurrent_Type (T)
10226 then
10227 Set_Corresponding_Record_Type (Def_Id,
10228 Corresponding_Record_Type (T));
10229 else
10230 Set_Direct_Primitive_Operations (Def_Id,
10231 Direct_Primitive_Operations (T));
10232 end if;
10234 Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
10235 end if;
10237 -- Subtypes introduced by component declarations do not need to be
10238 -- marked as delayed, and do not get freeze nodes, because the semantics
10239 -- verifies that the parents of the subtypes are frozen before the
10240 -- enclosing record is frozen.
10242 if not Is_Type (Scope (Def_Id)) then
10243 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
10245 if Is_Private_Type (T)
10246 and then Present (Full_View (T))
10247 then
10248 Conditional_Delay (Def_Id, Full_View (T));
10249 else
10250 Conditional_Delay (Def_Id, T);
10251 end if;
10252 end if;
10254 if Is_Record_Type (T) then
10255 Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
10257 if Has_Discrs
10258 and then not Is_Empty_Elmt_List (Elist)
10259 and then not For_Access
10260 then
10261 Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
10263 elsif not For_Access then
10264 Set_Cloned_Subtype (Def_Id, T);
10265 end if;
10266 end if;
10267 end Build_Discriminated_Subtype;
10269 ---------------------------
10270 -- Build_Itype_Reference --
10271 ---------------------------
10273 procedure Build_Itype_Reference
10274 (Ityp : Entity_Id;
10275 Nod : Node_Id)
10277 IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
10278 begin
10280 -- Itype references are only created for use by the back-end
10282 if Inside_A_Generic then
10283 return;
10284 else
10285 Set_Itype (IR, Ityp);
10287 -- If Nod is a library unit entity, then Insert_After won't work,
10288 -- because Nod is not a member of any list. Therefore, we use
10289 -- Add_Global_Declaration in this case. This can happen if we have a
10290 -- build-in-place library function.
10292 if (Nkind (Nod) in N_Entity and then Is_Compilation_Unit (Nod))
10293 or else
10294 (Nkind (Nod) = N_Defining_Program_Unit_Name
10295 and then Is_Compilation_Unit (Defining_Identifier (Nod)))
10296 then
10297 Add_Global_Declaration (IR);
10298 else
10299 Insert_After (Nod, IR);
10300 end if;
10301 end if;
10302 end Build_Itype_Reference;
10304 ------------------------
10305 -- Build_Scalar_Bound --
10306 ------------------------
10308 function Build_Scalar_Bound
10309 (Bound : Node_Id;
10310 Par_T : Entity_Id;
10311 Der_T : Entity_Id) return Node_Id
10313 New_Bound : Entity_Id;
10315 begin
10316 -- Note: not clear why this is needed, how can the original bound
10317 -- be unanalyzed at this point? and if it is, what business do we
10318 -- have messing around with it? and why is the base type of the
10319 -- parent type the right type for the resolution. It probably is
10320 -- not. It is OK for the new bound we are creating, but not for
10321 -- the old one??? Still if it never happens, no problem.
10323 Analyze_And_Resolve (Bound, Base_Type (Par_T));
10325 if Nkind_In (Bound, N_Integer_Literal, N_Real_Literal) then
10326 New_Bound := New_Copy (Bound);
10327 Set_Etype (New_Bound, Der_T);
10328 Set_Analyzed (New_Bound);
10330 elsif Is_Entity_Name (Bound) then
10331 New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
10333 -- The following is almost certainly wrong. What business do we have
10334 -- relocating a node (Bound) that is presumably still attached to
10335 -- the tree elsewhere???
10337 else
10338 New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
10339 end if;
10341 Set_Etype (New_Bound, Der_T);
10342 return New_Bound;
10343 end Build_Scalar_Bound;
10345 --------------------------------
10346 -- Build_Underlying_Full_View --
10347 --------------------------------
10349 procedure Build_Underlying_Full_View
10350 (N : Node_Id;
10351 Typ : Entity_Id;
10352 Par : Entity_Id)
10354 Loc : constant Source_Ptr := Sloc (N);
10355 Subt : constant Entity_Id :=
10356 Make_Defining_Identifier
10357 (Loc, New_External_Name (Chars (Typ), 'S'));
10359 Constr : Node_Id;
10360 Indic : Node_Id;
10361 C : Node_Id;
10362 Id : Node_Id;
10364 procedure Set_Discriminant_Name (Id : Node_Id);
10365 -- If the derived type has discriminants, they may rename discriminants
10366 -- of the parent. When building the full view of the parent, we need to
10367 -- recover the names of the original discriminants if the constraint is
10368 -- given by named associations.
10370 ---------------------------
10371 -- Set_Discriminant_Name --
10372 ---------------------------
10374 procedure Set_Discriminant_Name (Id : Node_Id) is
10375 Disc : Entity_Id;
10377 begin
10378 Set_Original_Discriminant (Id, Empty);
10380 if Has_Discriminants (Typ) then
10381 Disc := First_Discriminant (Typ);
10382 while Present (Disc) loop
10383 if Chars (Disc) = Chars (Id)
10384 and then Present (Corresponding_Discriminant (Disc))
10385 then
10386 Set_Chars (Id, Chars (Corresponding_Discriminant (Disc)));
10387 end if;
10388 Next_Discriminant (Disc);
10389 end loop;
10390 end if;
10391 end Set_Discriminant_Name;
10393 -- Start of processing for Build_Underlying_Full_View
10395 begin
10396 if Nkind (N) = N_Full_Type_Declaration then
10397 Constr := Constraint (Subtype_Indication (Type_Definition (N)));
10399 elsif Nkind (N) = N_Subtype_Declaration then
10400 Constr := New_Copy_Tree (Constraint (Subtype_Indication (N)));
10402 elsif Nkind (N) = N_Component_Declaration then
10403 Constr :=
10404 New_Copy_Tree
10405 (Constraint (Subtype_Indication (Component_Definition (N))));
10407 else
10408 raise Program_Error;
10409 end if;
10411 C := First (Constraints (Constr));
10412 while Present (C) loop
10413 if Nkind (C) = N_Discriminant_Association then
10414 Id := First (Selector_Names (C));
10415 while Present (Id) loop
10416 Set_Discriminant_Name (Id);
10417 Next (Id);
10418 end loop;
10419 end if;
10421 Next (C);
10422 end loop;
10424 Indic :=
10425 Make_Subtype_Declaration (Loc,
10426 Defining_Identifier => Subt,
10427 Subtype_Indication =>
10428 Make_Subtype_Indication (Loc,
10429 Subtype_Mark => New_Occurrence_Of (Par, Loc),
10430 Constraint => New_Copy_Tree (Constr)));
10432 -- If this is a component subtype for an outer itype, it is not
10433 -- a list member, so simply set the parent link for analysis: if
10434 -- the enclosing type does not need to be in a declarative list,
10435 -- neither do the components.
10437 if Is_List_Member (N)
10438 and then Nkind (N) /= N_Component_Declaration
10439 then
10440 Insert_Before (N, Indic);
10441 else
10442 Set_Parent (Indic, Parent (N));
10443 end if;
10445 Analyze (Indic);
10446 Set_Underlying_Full_View (Typ, Full_View (Subt));
10447 Set_Is_Underlying_Full_View (Full_View (Subt));
10448 end Build_Underlying_Full_View;
10450 -------------------------------
10451 -- Check_Abstract_Overriding --
10452 -------------------------------
10454 procedure Check_Abstract_Overriding (T : Entity_Id) is
10455 Alias_Subp : Entity_Id;
10456 Elmt : Elmt_Id;
10457 Op_List : Elist_Id;
10458 Subp : Entity_Id;
10459 Type_Def : Node_Id;
10461 procedure Check_Pragma_Implemented (Subp : Entity_Id);
10462 -- Ada 2012 (AI05-0030): Subprogram Subp overrides an interface routine
10463 -- which has pragma Implemented already set. Check whether Subp's entity
10464 -- kind conforms to the implementation kind of the overridden routine.
10466 procedure Check_Pragma_Implemented
10467 (Subp : Entity_Id;
10468 Iface_Subp : Entity_Id);
10469 -- Ada 2012 (AI05-0030): Subprogram Subp overrides interface routine
10470 -- Iface_Subp and both entities have pragma Implemented already set on
10471 -- them. Check whether the two implementation kinds are conforming.
10473 procedure Inherit_Pragma_Implemented
10474 (Subp : Entity_Id;
10475 Iface_Subp : Entity_Id);
10476 -- Ada 2012 (AI05-0030): Interface primitive Subp overrides interface
10477 -- subprogram Iface_Subp which has been marked by pragma Implemented.
10478 -- Propagate the implementation kind of Iface_Subp to Subp.
10480 ------------------------------
10481 -- Check_Pragma_Implemented --
10482 ------------------------------
10484 procedure Check_Pragma_Implemented (Subp : Entity_Id) is
10485 Iface_Alias : constant Entity_Id := Interface_Alias (Subp);
10486 Impl_Kind : constant Name_Id := Implementation_Kind (Iface_Alias);
10487 Subp_Alias : constant Entity_Id := Alias (Subp);
10488 Contr_Typ : Entity_Id;
10489 Impl_Subp : Entity_Id;
10491 begin
10492 -- Subp must have an alias since it is a hidden entity used to link
10493 -- an interface subprogram to its overriding counterpart.
10495 pragma Assert (Present (Subp_Alias));
10497 -- Handle aliases to synchronized wrappers
10499 Impl_Subp := Subp_Alias;
10501 if Is_Primitive_Wrapper (Impl_Subp) then
10502 Impl_Subp := Wrapped_Entity (Impl_Subp);
10503 end if;
10505 -- Extract the type of the controlling formal
10507 Contr_Typ := Etype (First_Formal (Subp_Alias));
10509 if Is_Concurrent_Record_Type (Contr_Typ) then
10510 Contr_Typ := Corresponding_Concurrent_Type (Contr_Typ);
10511 end if;
10513 -- An interface subprogram whose implementation kind is By_Entry must
10514 -- be implemented by an entry.
10516 if Impl_Kind = Name_By_Entry
10517 and then Ekind (Impl_Subp) /= E_Entry
10518 then
10519 Error_Msg_Node_2 := Iface_Alias;
10520 Error_Msg_NE
10521 ("type & must implement abstract subprogram & with an entry",
10522 Subp_Alias, Contr_Typ);
10524 elsif Impl_Kind = Name_By_Protected_Procedure then
10526 -- An interface subprogram whose implementation kind is By_
10527 -- Protected_Procedure cannot be implemented by a primitive
10528 -- procedure of a task type.
10530 if Ekind (Contr_Typ) /= E_Protected_Type then
10531 Error_Msg_Node_2 := Contr_Typ;
10532 Error_Msg_NE
10533 ("interface subprogram & cannot be implemented by a " &
10534 "primitive procedure of task type &", Subp_Alias,
10535 Iface_Alias);
10537 -- An interface subprogram whose implementation kind is By_
10538 -- Protected_Procedure must be implemented by a procedure.
10540 elsif Ekind (Impl_Subp) /= E_Procedure then
10541 Error_Msg_Node_2 := Iface_Alias;
10542 Error_Msg_NE
10543 ("type & must implement abstract subprogram & with a " &
10544 "procedure", Subp_Alias, Contr_Typ);
10546 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
10547 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
10548 then
10549 Error_Msg_Name_1 := Impl_Kind;
10550 Error_Msg_N
10551 ("overriding operation& must have synchronization%",
10552 Subp_Alias);
10553 end if;
10555 -- If primitive has Optional synchronization, overriding operation
10556 -- must match if it has an explicit synchronization..
10558 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
10559 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
10560 then
10561 Error_Msg_Name_1 := Impl_Kind;
10562 Error_Msg_N
10563 ("overriding operation& must have syncrhonization%",
10564 Subp_Alias);
10565 end if;
10566 end Check_Pragma_Implemented;
10568 ------------------------------
10569 -- Check_Pragma_Implemented --
10570 ------------------------------
10572 procedure Check_Pragma_Implemented
10573 (Subp : Entity_Id;
10574 Iface_Subp : Entity_Id)
10576 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
10577 Subp_Kind : constant Name_Id := Implementation_Kind (Subp);
10579 begin
10580 -- Ada 2012 (AI05-0030): The implementation kinds of an overridden
10581 -- and overriding subprogram are different. In general this is an
10582 -- error except when the implementation kind of the overridden
10583 -- subprograms is By_Any or Optional.
10585 if Iface_Kind /= Subp_Kind
10586 and then Iface_Kind /= Name_By_Any
10587 and then Iface_Kind /= Name_Optional
10588 then
10589 if Iface_Kind = Name_By_Entry then
10590 Error_Msg_N
10591 ("incompatible implementation kind, overridden subprogram " &
10592 "is marked By_Entry", Subp);
10593 else
10594 Error_Msg_N
10595 ("incompatible implementation kind, overridden subprogram " &
10596 "is marked By_Protected_Procedure", Subp);
10597 end if;
10598 end if;
10599 end Check_Pragma_Implemented;
10601 --------------------------------
10602 -- Inherit_Pragma_Implemented --
10603 --------------------------------
10605 procedure Inherit_Pragma_Implemented
10606 (Subp : Entity_Id;
10607 Iface_Subp : Entity_Id)
10609 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
10610 Loc : constant Source_Ptr := Sloc (Subp);
10611 Impl_Prag : Node_Id;
10613 begin
10614 -- Since the implementation kind is stored as a representation item
10615 -- rather than a flag, create a pragma node.
10617 Impl_Prag :=
10618 Make_Pragma (Loc,
10619 Chars => Name_Implemented,
10620 Pragma_Argument_Associations => New_List (
10621 Make_Pragma_Argument_Association (Loc,
10622 Expression => New_Occurrence_Of (Subp, Loc)),
10624 Make_Pragma_Argument_Association (Loc,
10625 Expression => Make_Identifier (Loc, Iface_Kind))));
10627 -- The pragma doesn't need to be analyzed because it is internally
10628 -- built. It is safe to directly register it as a rep item since we
10629 -- are only interested in the characters of the implementation kind.
10631 Record_Rep_Item (Subp, Impl_Prag);
10632 end Inherit_Pragma_Implemented;
10634 -- Start of processing for Check_Abstract_Overriding
10636 begin
10637 Op_List := Primitive_Operations (T);
10639 -- Loop to check primitive operations
10641 Elmt := First_Elmt (Op_List);
10642 while Present (Elmt) loop
10643 Subp := Node (Elmt);
10644 Alias_Subp := Alias (Subp);
10646 -- Inherited subprograms are identified by the fact that they do not
10647 -- come from source, and the associated source location is the
10648 -- location of the first subtype of the derived type.
10650 -- Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
10651 -- subprograms that "require overriding".
10653 -- Special exception, do not complain about failure to override the
10654 -- stream routines _Input and _Output, as well as the primitive
10655 -- operations used in dispatching selects since we always provide
10656 -- automatic overridings for these subprograms.
10658 -- The partial view of T may have been a private extension, for
10659 -- which inherited functions dispatching on result are abstract.
10660 -- If the full view is a null extension, there is no need for
10661 -- overriding in Ada 2005, but wrappers need to be built for them
10662 -- (see exp_ch3, Build_Controlling_Function_Wrappers).
10664 if Is_Null_Extension (T)
10665 and then Has_Controlling_Result (Subp)
10666 and then Ada_Version >= Ada_2005
10667 and then Present (Alias_Subp)
10668 and then not Comes_From_Source (Subp)
10669 and then not Is_Abstract_Subprogram (Alias_Subp)
10670 and then not Is_Access_Type (Etype (Subp))
10671 then
10672 null;
10674 -- Ada 2005 (AI-251): Internal entities of interfaces need no
10675 -- processing because this check is done with the aliased
10676 -- entity
10678 elsif Present (Interface_Alias (Subp)) then
10679 null;
10681 elsif (Is_Abstract_Subprogram (Subp)
10682 or else Requires_Overriding (Subp)
10683 or else
10684 (Has_Controlling_Result (Subp)
10685 and then Present (Alias_Subp)
10686 and then not Comes_From_Source (Subp)
10687 and then Sloc (Subp) = Sloc (First_Subtype (T))))
10688 and then not Is_TSS (Subp, TSS_Stream_Input)
10689 and then not Is_TSS (Subp, TSS_Stream_Output)
10690 and then not Is_Abstract_Type (T)
10691 and then not Is_Predefined_Interface_Primitive (Subp)
10693 -- Ada 2005 (AI-251): Do not consider hidden entities associated
10694 -- with abstract interface types because the check will be done
10695 -- with the aliased entity (otherwise we generate a duplicated
10696 -- error message).
10698 and then not Present (Interface_Alias (Subp))
10699 then
10700 if Present (Alias_Subp) then
10702 -- Only perform the check for a derived subprogram when the
10703 -- type has an explicit record extension. This avoids incorrect
10704 -- flagging of abstract subprograms for the case of a type
10705 -- without an extension that is derived from a formal type
10706 -- with a tagged actual (can occur within a private part).
10708 -- Ada 2005 (AI-391): In the case of an inherited function with
10709 -- a controlling result of the type, the rule does not apply if
10710 -- the type is a null extension (unless the parent function
10711 -- itself is abstract, in which case the function must still be
10712 -- be overridden). The expander will generate an overriding
10713 -- wrapper function calling the parent subprogram (see
10714 -- Exp_Ch3.Make_Controlling_Wrapper_Functions).
10716 Type_Def := Type_Definition (Parent (T));
10718 if Nkind (Type_Def) = N_Derived_Type_Definition
10719 and then Present (Record_Extension_Part (Type_Def))
10720 and then
10721 (Ada_Version < Ada_2005
10722 or else not Is_Null_Extension (T)
10723 or else Ekind (Subp) = E_Procedure
10724 or else not Has_Controlling_Result (Subp)
10725 or else Is_Abstract_Subprogram (Alias_Subp)
10726 or else Requires_Overriding (Subp)
10727 or else Is_Access_Type (Etype (Subp)))
10728 then
10729 -- Avoid reporting error in case of abstract predefined
10730 -- primitive inherited from interface type because the
10731 -- body of internally generated predefined primitives
10732 -- of tagged types are generated later by Freeze_Type
10734 if Is_Interface (Root_Type (T))
10735 and then Is_Abstract_Subprogram (Subp)
10736 and then Is_Predefined_Dispatching_Operation (Subp)
10737 and then not Comes_From_Source (Ultimate_Alias (Subp))
10738 then
10739 null;
10741 -- A null extension is not obliged to override an inherited
10742 -- procedure subject to pragma Extensions_Visible with value
10743 -- False and at least one controlling OUT parameter
10744 -- (SPARK RM 6.1.7(6)).
10746 elsif Is_Null_Extension (T)
10747 and then Is_EVF_Procedure (Subp)
10748 then
10749 null;
10751 else
10752 Error_Msg_NE
10753 ("type must be declared abstract or & overridden",
10754 T, Subp);
10756 -- Traverse the whole chain of aliased subprograms to
10757 -- complete the error notification. This is especially
10758 -- useful for traceability of the chain of entities when
10759 -- the subprogram corresponds with an interface
10760 -- subprogram (which may be defined in another package).
10762 if Present (Alias_Subp) then
10763 declare
10764 E : Entity_Id;
10766 begin
10767 E := Subp;
10768 while Present (Alias (E)) loop
10770 -- Avoid reporting redundant errors on entities
10771 -- inherited from interfaces
10773 if Sloc (E) /= Sloc (T) then
10774 Error_Msg_Sloc := Sloc (E);
10775 Error_Msg_NE
10776 ("\& has been inherited #", T, Subp);
10777 end if;
10779 E := Alias (E);
10780 end loop;
10782 Error_Msg_Sloc := Sloc (E);
10784 -- AI05-0068: report if there is an overriding
10785 -- non-abstract subprogram that is invisible.
10787 if Is_Hidden (E)
10788 and then not Is_Abstract_Subprogram (E)
10789 then
10790 Error_Msg_NE
10791 ("\& subprogram# is not visible",
10792 T, Subp);
10794 -- Clarify the case where a non-null extension must
10795 -- override inherited procedure subject to pragma
10796 -- Extensions_Visible with value False and at least
10797 -- one controlling OUT param.
10799 elsif Is_EVF_Procedure (E) then
10800 Error_Msg_NE
10801 ("\& # is subject to Extensions_Visible False",
10802 T, Subp);
10804 else
10805 Error_Msg_NE
10806 ("\& has been inherited from subprogram #",
10807 T, Subp);
10808 end if;
10809 end;
10810 end if;
10811 end if;
10813 -- Ada 2005 (AI-345): Protected or task type implementing
10814 -- abstract interfaces.
10816 elsif Is_Concurrent_Record_Type (T)
10817 and then Present (Interfaces (T))
10818 then
10819 -- There is no need to check here RM 9.4(11.9/3) since we
10820 -- are processing the corresponding record type and the
10821 -- mode of the overriding subprograms was verified by
10822 -- Check_Conformance when the corresponding concurrent
10823 -- type declaration was analyzed.
10825 Error_Msg_NE
10826 ("interface subprogram & must be overridden", T, Subp);
10828 -- Examine primitive operations of synchronized type to find
10829 -- homonyms that have the wrong profile.
10831 declare
10832 Prim : Entity_Id;
10834 begin
10835 Prim := First_Entity (Corresponding_Concurrent_Type (T));
10836 while Present (Prim) loop
10837 if Chars (Prim) = Chars (Subp) then
10838 Error_Msg_NE
10839 ("profile is not type conformant with prefixed "
10840 & "view profile of inherited operation&",
10841 Prim, Subp);
10842 end if;
10844 Next_Entity (Prim);
10845 end loop;
10846 end;
10847 end if;
10849 else
10850 Error_Msg_Node_2 := T;
10851 Error_Msg_N
10852 ("abstract subprogram& not allowed for type&", Subp);
10854 -- Also post unconditional warning on the type (unconditional
10855 -- so that if there are more than one of these cases, we get
10856 -- them all, and not just the first one).
10858 Error_Msg_Node_2 := Subp;
10859 Error_Msg_N ("nonabstract type& has abstract subprogram&!", T);
10860 end if;
10862 -- A subprogram subject to pragma Extensions_Visible with value
10863 -- "True" cannot override a subprogram subject to the same pragma
10864 -- with value "False" (SPARK RM 6.1.7(5)).
10866 elsif Extensions_Visible_Status (Subp) = Extensions_Visible_True
10867 and then Present (Overridden_Operation (Subp))
10868 and then Extensions_Visible_Status (Overridden_Operation (Subp)) =
10869 Extensions_Visible_False
10870 then
10871 Error_Msg_Sloc := Sloc (Overridden_Operation (Subp));
10872 Error_Msg_N
10873 ("subprogram & with Extensions_Visible True cannot override "
10874 & "subprogram # with Extensions_Visible False", Subp);
10875 end if;
10877 -- Ada 2012 (AI05-0030): Perform checks related to pragma Implemented
10879 -- Subp is an expander-generated procedure which maps an interface
10880 -- alias to a protected wrapper. The interface alias is flagged by
10881 -- pragma Implemented. Ensure that Subp is a procedure when the
10882 -- implementation kind is By_Protected_Procedure or an entry when
10883 -- By_Entry.
10885 if Ada_Version >= Ada_2012
10886 and then Is_Hidden (Subp)
10887 and then Present (Interface_Alias (Subp))
10888 and then Has_Rep_Pragma (Interface_Alias (Subp), Name_Implemented)
10889 then
10890 Check_Pragma_Implemented (Subp);
10891 end if;
10893 -- Subp is an interface primitive which overrides another interface
10894 -- primitive marked with pragma Implemented.
10896 if Ada_Version >= Ada_2012
10897 and then Present (Overridden_Operation (Subp))
10898 and then Has_Rep_Pragma
10899 (Overridden_Operation (Subp), Name_Implemented)
10900 then
10901 -- If the overriding routine is also marked by Implemented, check
10902 -- that the two implementation kinds are conforming.
10904 if Has_Rep_Pragma (Subp, Name_Implemented) then
10905 Check_Pragma_Implemented
10906 (Subp => Subp,
10907 Iface_Subp => Overridden_Operation (Subp));
10909 -- Otherwise the overriding routine inherits the implementation
10910 -- kind from the overridden subprogram.
10912 else
10913 Inherit_Pragma_Implemented
10914 (Subp => Subp,
10915 Iface_Subp => Overridden_Operation (Subp));
10916 end if;
10917 end if;
10919 -- If the operation is a wrapper for a synchronized primitive, it
10920 -- may be called indirectly through a dispatching select. We assume
10921 -- that it will be referenced elsewhere indirectly, and suppress
10922 -- warnings about an unused entity.
10924 if Is_Primitive_Wrapper (Subp)
10925 and then Present (Wrapped_Entity (Subp))
10926 then
10927 Set_Referenced (Wrapped_Entity (Subp));
10928 end if;
10930 Next_Elmt (Elmt);
10931 end loop;
10932 end Check_Abstract_Overriding;
10934 ------------------------------------------------
10935 -- Check_Access_Discriminant_Requires_Limited --
10936 ------------------------------------------------
10938 procedure Check_Access_Discriminant_Requires_Limited
10939 (D : Node_Id;
10940 Loc : Node_Id)
10942 begin
10943 -- A discriminant_specification for an access discriminant shall appear
10944 -- only in the declaration for a task or protected type, or for a type
10945 -- with the reserved word 'limited' in its definition or in one of its
10946 -- ancestors (RM 3.7(10)).
10948 -- AI-0063: The proper condition is that type must be immutably limited,
10949 -- or else be a partial view.
10951 if Nkind (Discriminant_Type (D)) = N_Access_Definition then
10952 if Is_Limited_View (Current_Scope)
10953 or else
10954 (Nkind (Parent (Current_Scope)) = N_Private_Type_Declaration
10955 and then Limited_Present (Parent (Current_Scope)))
10956 then
10957 null;
10959 else
10960 Error_Msg_N
10961 ("access discriminants allowed only for limited types", Loc);
10962 end if;
10963 end if;
10964 end Check_Access_Discriminant_Requires_Limited;
10966 -----------------------------------
10967 -- Check_Aliased_Component_Types --
10968 -----------------------------------
10970 procedure Check_Aliased_Component_Types (T : Entity_Id) is
10971 C : Entity_Id;
10973 begin
10974 -- ??? Also need to check components of record extensions, but not
10975 -- components of protected types (which are always limited).
10977 -- Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
10978 -- types to be unconstrained. This is safe because it is illegal to
10979 -- create access subtypes to such types with explicit discriminant
10980 -- constraints.
10982 if not Is_Limited_Type (T) then
10983 if Ekind (T) = E_Record_Type then
10984 C := First_Component (T);
10985 while Present (C) loop
10986 if Is_Aliased (C)
10987 and then Has_Discriminants (Etype (C))
10988 and then not Is_Constrained (Etype (C))
10989 and then not In_Instance_Body
10990 and then Ada_Version < Ada_2005
10991 then
10992 Error_Msg_N
10993 ("aliased component must be constrained (RM 3.6(11))",
10995 end if;
10997 Next_Component (C);
10998 end loop;
11000 elsif Ekind (T) = E_Array_Type then
11001 if Has_Aliased_Components (T)
11002 and then Has_Discriminants (Component_Type (T))
11003 and then not Is_Constrained (Component_Type (T))
11004 and then not In_Instance_Body
11005 and then Ada_Version < Ada_2005
11006 then
11007 Error_Msg_N
11008 ("aliased component type must be constrained (RM 3.6(11))",
11010 end if;
11011 end if;
11012 end if;
11013 end Check_Aliased_Component_Types;
11015 ---------------------------------------
11016 -- Check_Anonymous_Access_Components --
11017 ---------------------------------------
11019 procedure Check_Anonymous_Access_Components
11020 (Typ_Decl : Node_Id;
11021 Typ : Entity_Id;
11022 Prev : Entity_Id;
11023 Comp_List : Node_Id)
11025 Loc : constant Source_Ptr := Sloc (Typ_Decl);
11026 Anon_Access : Entity_Id;
11027 Acc_Def : Node_Id;
11028 Comp : Node_Id;
11029 Comp_Def : Node_Id;
11030 Decl : Node_Id;
11031 Type_Def : Node_Id;
11033 procedure Build_Incomplete_Type_Declaration;
11034 -- If the record type contains components that include an access to the
11035 -- current record, then create an incomplete type declaration for the
11036 -- record, to be used as the designated type of the anonymous access.
11037 -- This is done only once, and only if there is no previous partial
11038 -- view of the type.
11040 function Designates_T (Subt : Node_Id) return Boolean;
11041 -- Check whether a node designates the enclosing record type, or 'Class
11042 -- of that type
11044 function Mentions_T (Acc_Def : Node_Id) return Boolean;
11045 -- Check whether an access definition includes a reference to
11046 -- the enclosing record type. The reference can be a subtype mark
11047 -- in the access definition itself, a 'Class attribute reference, or
11048 -- recursively a reference appearing in a parameter specification
11049 -- or result definition of an access_to_subprogram definition.
11051 --------------------------------------
11052 -- Build_Incomplete_Type_Declaration --
11053 --------------------------------------
11055 procedure Build_Incomplete_Type_Declaration is
11056 Decl : Node_Id;
11057 Inc_T : Entity_Id;
11058 H : Entity_Id;
11060 -- Is_Tagged indicates whether the type is tagged. It is tagged if
11061 -- it's "is new ... with record" or else "is tagged record ...".
11063 Is_Tagged : constant Boolean :=
11064 (Nkind (Type_Definition (Typ_Decl)) = N_Derived_Type_Definition
11065 and then
11066 Present (Record_Extension_Part (Type_Definition (Typ_Decl))))
11067 or else
11068 (Nkind (Type_Definition (Typ_Decl)) = N_Record_Definition
11069 and then Tagged_Present (Type_Definition (Typ_Decl)));
11071 begin
11072 -- If there is a previous partial view, no need to create a new one
11073 -- If the partial view, given by Prev, is incomplete, If Prev is
11074 -- a private declaration, full declaration is flagged accordingly.
11076 if Prev /= Typ then
11077 if Is_Tagged then
11078 Make_Class_Wide_Type (Prev);
11079 Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
11080 Set_Etype (Class_Wide_Type (Typ), Typ);
11081 end if;
11083 return;
11085 elsif Has_Private_Declaration (Typ) then
11087 -- If we refer to T'Class inside T, and T is the completion of a
11088 -- private type, then make sure the class-wide type exists.
11090 if Is_Tagged then
11091 Make_Class_Wide_Type (Typ);
11092 end if;
11094 return;
11096 -- If there was a previous anonymous access type, the incomplete
11097 -- type declaration will have been created already.
11099 elsif Present (Current_Entity (Typ))
11100 and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
11101 and then Full_View (Current_Entity (Typ)) = Typ
11102 then
11103 if Is_Tagged
11104 and then Comes_From_Source (Current_Entity (Typ))
11105 and then not Is_Tagged_Type (Current_Entity (Typ))
11106 then
11107 Make_Class_Wide_Type (Typ);
11108 Error_Msg_N
11109 ("incomplete view of tagged type should be declared tagged??",
11110 Parent (Current_Entity (Typ)));
11111 end if;
11112 return;
11114 else
11115 Inc_T := Make_Defining_Identifier (Loc, Chars (Typ));
11116 Decl := Make_Incomplete_Type_Declaration (Loc, Inc_T);
11118 -- Type has already been inserted into the current scope. Remove
11119 -- it, and add incomplete declaration for type, so that subsequent
11120 -- anonymous access types can use it. The entity is unchained from
11121 -- the homonym list and from immediate visibility. After analysis,
11122 -- the entity in the incomplete declaration becomes immediately
11123 -- visible in the record declaration that follows.
11125 H := Current_Entity (Typ);
11127 if H = Typ then
11128 Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
11129 else
11130 while Present (H)
11131 and then Homonym (H) /= Typ
11132 loop
11133 H := Homonym (Typ);
11134 end loop;
11136 Set_Homonym (H, Homonym (Typ));
11137 end if;
11139 Insert_Before (Typ_Decl, Decl);
11140 Analyze (Decl);
11141 Set_Full_View (Inc_T, Typ);
11143 if Is_Tagged then
11145 -- Create a common class-wide type for both views, and set the
11146 -- Etype of the class-wide type to the full view.
11148 Make_Class_Wide_Type (Inc_T);
11149 Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
11150 Set_Etype (Class_Wide_Type (Typ), Typ);
11151 end if;
11152 end if;
11153 end Build_Incomplete_Type_Declaration;
11155 ------------------
11156 -- Designates_T --
11157 ------------------
11159 function Designates_T (Subt : Node_Id) return Boolean is
11160 Type_Id : constant Name_Id := Chars (Typ);
11162 function Names_T (Nam : Node_Id) return Boolean;
11163 -- The record type has not been introduced in the current scope
11164 -- yet, so we must examine the name of the type itself, either
11165 -- an identifier T, or an expanded name of the form P.T, where
11166 -- P denotes the current scope.
11168 -------------
11169 -- Names_T --
11170 -------------
11172 function Names_T (Nam : Node_Id) return Boolean is
11173 begin
11174 if Nkind (Nam) = N_Identifier then
11175 return Chars (Nam) = Type_Id;
11177 elsif Nkind (Nam) = N_Selected_Component then
11178 if Chars (Selector_Name (Nam)) = Type_Id then
11179 if Nkind (Prefix (Nam)) = N_Identifier then
11180 return Chars (Prefix (Nam)) = Chars (Current_Scope);
11182 elsif Nkind (Prefix (Nam)) = N_Selected_Component then
11183 return Chars (Selector_Name (Prefix (Nam))) =
11184 Chars (Current_Scope);
11185 else
11186 return False;
11187 end if;
11189 else
11190 return False;
11191 end if;
11193 else
11194 return False;
11195 end if;
11196 end Names_T;
11198 -- Start of processing for Designates_T
11200 begin
11201 if Nkind (Subt) = N_Identifier then
11202 return Chars (Subt) = Type_Id;
11204 -- Reference can be through an expanded name which has not been
11205 -- analyzed yet, and which designates enclosing scopes.
11207 elsif Nkind (Subt) = N_Selected_Component then
11208 if Names_T (Subt) then
11209 return True;
11211 -- Otherwise it must denote an entity that is already visible.
11212 -- The access definition may name a subtype of the enclosing
11213 -- type, if there is a previous incomplete declaration for it.
11215 else
11216 Find_Selected_Component (Subt);
11217 return
11218 Is_Entity_Name (Subt)
11219 and then Scope (Entity (Subt)) = Current_Scope
11220 and then
11221 (Chars (Base_Type (Entity (Subt))) = Type_Id
11222 or else
11223 (Is_Class_Wide_Type (Entity (Subt))
11224 and then
11225 Chars (Etype (Base_Type (Entity (Subt)))) =
11226 Type_Id));
11227 end if;
11229 -- A reference to the current type may appear as the prefix of
11230 -- a 'Class attribute.
11232 elsif Nkind (Subt) = N_Attribute_Reference
11233 and then Attribute_Name (Subt) = Name_Class
11234 then
11235 return Names_T (Prefix (Subt));
11237 else
11238 return False;
11239 end if;
11240 end Designates_T;
11242 ----------------
11243 -- Mentions_T --
11244 ----------------
11246 function Mentions_T (Acc_Def : Node_Id) return Boolean is
11247 Param_Spec : Node_Id;
11249 Acc_Subprg : constant Node_Id :=
11250 Access_To_Subprogram_Definition (Acc_Def);
11252 begin
11253 if No (Acc_Subprg) then
11254 return Designates_T (Subtype_Mark (Acc_Def));
11255 end if;
11257 -- Component is an access_to_subprogram: examine its formals,
11258 -- and result definition in the case of an access_to_function.
11260 Param_Spec := First (Parameter_Specifications (Acc_Subprg));
11261 while Present (Param_Spec) loop
11262 if Nkind (Parameter_Type (Param_Spec)) = N_Access_Definition
11263 and then Mentions_T (Parameter_Type (Param_Spec))
11264 then
11265 return True;
11267 elsif Designates_T (Parameter_Type (Param_Spec)) then
11268 return True;
11269 end if;
11271 Next (Param_Spec);
11272 end loop;
11274 if Nkind (Acc_Subprg) = N_Access_Function_Definition then
11275 if Nkind (Result_Definition (Acc_Subprg)) =
11276 N_Access_Definition
11277 then
11278 return Mentions_T (Result_Definition (Acc_Subprg));
11279 else
11280 return Designates_T (Result_Definition (Acc_Subprg));
11281 end if;
11282 end if;
11284 return False;
11285 end Mentions_T;
11287 -- Start of processing for Check_Anonymous_Access_Components
11289 begin
11290 if No (Comp_List) then
11291 return;
11292 end if;
11294 Comp := First (Component_Items (Comp_List));
11295 while Present (Comp) loop
11296 if Nkind (Comp) = N_Component_Declaration
11297 and then Present
11298 (Access_Definition (Component_Definition (Comp)))
11299 and then
11300 Mentions_T (Access_Definition (Component_Definition (Comp)))
11301 then
11302 Comp_Def := Component_Definition (Comp);
11303 Acc_Def :=
11304 Access_To_Subprogram_Definition (Access_Definition (Comp_Def));
11306 Build_Incomplete_Type_Declaration;
11307 Anon_Access := Make_Temporary (Loc, 'S');
11309 -- Create a declaration for the anonymous access type: either
11310 -- an access_to_object or an access_to_subprogram.
11312 if Present (Acc_Def) then
11313 if Nkind (Acc_Def) = N_Access_Function_Definition then
11314 Type_Def :=
11315 Make_Access_Function_Definition (Loc,
11316 Parameter_Specifications =>
11317 Parameter_Specifications (Acc_Def),
11318 Result_Definition => Result_Definition (Acc_Def));
11319 else
11320 Type_Def :=
11321 Make_Access_Procedure_Definition (Loc,
11322 Parameter_Specifications =>
11323 Parameter_Specifications (Acc_Def));
11324 end if;
11326 else
11327 Type_Def :=
11328 Make_Access_To_Object_Definition (Loc,
11329 Subtype_Indication =>
11330 Relocate_Node
11331 (Subtype_Mark (Access_Definition (Comp_Def))));
11333 Set_Constant_Present
11334 (Type_Def, Constant_Present (Access_Definition (Comp_Def)));
11335 Set_All_Present
11336 (Type_Def, All_Present (Access_Definition (Comp_Def)));
11337 end if;
11339 Set_Null_Exclusion_Present
11340 (Type_Def,
11341 Null_Exclusion_Present (Access_Definition (Comp_Def)));
11343 Decl :=
11344 Make_Full_Type_Declaration (Loc,
11345 Defining_Identifier => Anon_Access,
11346 Type_Definition => Type_Def);
11348 Insert_Before (Typ_Decl, Decl);
11349 Analyze (Decl);
11351 -- If an access to subprogram, create the extra formals
11353 if Present (Acc_Def) then
11354 Create_Extra_Formals (Designated_Type (Anon_Access));
11356 -- If an access to object, preserve entity of designated type,
11357 -- for ASIS use, before rewriting the component definition.
11359 else
11360 declare
11361 Desig : Entity_Id;
11363 begin
11364 Desig := Entity (Subtype_Indication (Type_Def));
11366 -- If the access definition is to the current record,
11367 -- the visible entity at this point is an incomplete
11368 -- type. Retrieve the full view to simplify ASIS queries
11370 if Ekind (Desig) = E_Incomplete_Type then
11371 Desig := Full_View (Desig);
11372 end if;
11374 Set_Entity
11375 (Subtype_Mark (Access_Definition (Comp_Def)), Desig);
11376 end;
11377 end if;
11379 Rewrite (Comp_Def,
11380 Make_Component_Definition (Loc,
11381 Subtype_Indication =>
11382 New_Occurrence_Of (Anon_Access, Loc)));
11384 if Ekind (Designated_Type (Anon_Access)) = E_Subprogram_Type then
11385 Set_Ekind (Anon_Access, E_Anonymous_Access_Subprogram_Type);
11386 else
11387 Set_Ekind (Anon_Access, E_Anonymous_Access_Type);
11388 end if;
11390 Set_Is_Local_Anonymous_Access (Anon_Access);
11391 end if;
11393 Next (Comp);
11394 end loop;
11396 if Present (Variant_Part (Comp_List)) then
11397 declare
11398 V : Node_Id;
11399 begin
11400 V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
11401 while Present (V) loop
11402 Check_Anonymous_Access_Components
11403 (Typ_Decl, Typ, Prev, Component_List (V));
11404 Next_Non_Pragma (V);
11405 end loop;
11406 end;
11407 end if;
11408 end Check_Anonymous_Access_Components;
11410 ----------------------
11411 -- Check_Completion --
11412 ----------------------
11414 procedure Check_Completion (Body_Id : Node_Id := Empty) is
11415 E : Entity_Id;
11417 procedure Post_Error;
11418 -- Post error message for lack of completion for entity E
11420 ----------------
11421 -- Post_Error --
11422 ----------------
11424 procedure Post_Error is
11425 procedure Missing_Body;
11426 -- Output missing body message
11428 ------------------
11429 -- Missing_Body --
11430 ------------------
11432 procedure Missing_Body is
11433 begin
11434 -- Spec is in same unit, so we can post on spec
11436 if In_Same_Source_Unit (Body_Id, E) then
11437 Error_Msg_N ("missing body for &", E);
11439 -- Spec is in a separate unit, so we have to post on the body
11441 else
11442 Error_Msg_NE ("missing body for & declared#!", Body_Id, E);
11443 end if;
11444 end Missing_Body;
11446 -- Start of processing for Post_Error
11448 begin
11449 if not Comes_From_Source (E) then
11450 if Ekind_In (E, E_Task_Type, E_Protected_Type) then
11452 -- It may be an anonymous protected type created for a
11453 -- single variable. Post error on variable, if present.
11455 declare
11456 Var : Entity_Id;
11458 begin
11459 Var := First_Entity (Current_Scope);
11460 while Present (Var) loop
11461 exit when Etype (Var) = E
11462 and then Comes_From_Source (Var);
11464 Next_Entity (Var);
11465 end loop;
11467 if Present (Var) then
11468 E := Var;
11469 end if;
11470 end;
11471 end if;
11472 end if;
11474 -- If a generated entity has no completion, then either previous
11475 -- semantic errors have disabled the expansion phase, or else we had
11476 -- missing subunits, or else we are compiling without expansion,
11477 -- or else something is very wrong.
11479 if not Comes_From_Source (E) then
11480 pragma Assert
11481 (Serious_Errors_Detected > 0
11482 or else Configurable_Run_Time_Violations > 0
11483 or else Subunits_Missing
11484 or else not Expander_Active);
11485 return;
11487 -- Here for source entity
11489 else
11490 -- Here if no body to post the error message, so we post the error
11491 -- on the declaration that has no completion. This is not really
11492 -- the right place to post it, think about this later ???
11494 if No (Body_Id) then
11495 if Is_Type (E) then
11496 Error_Msg_NE
11497 ("missing full declaration for }", Parent (E), E);
11498 else
11499 Error_Msg_NE ("missing body for &", Parent (E), E);
11500 end if;
11502 -- Package body has no completion for a declaration that appears
11503 -- in the corresponding spec. Post error on the body, with a
11504 -- reference to the non-completed declaration.
11506 else
11507 Error_Msg_Sloc := Sloc (E);
11509 if Is_Type (E) then
11510 Error_Msg_NE ("missing full declaration for }!", Body_Id, E);
11512 elsif Is_Overloadable (E)
11513 and then Current_Entity_In_Scope (E) /= E
11514 then
11515 -- It may be that the completion is mistyped and appears as
11516 -- a distinct overloading of the entity.
11518 declare
11519 Candidate : constant Entity_Id :=
11520 Current_Entity_In_Scope (E);
11521 Decl : constant Node_Id :=
11522 Unit_Declaration_Node (Candidate);
11524 begin
11525 if Is_Overloadable (Candidate)
11526 and then Ekind (Candidate) = Ekind (E)
11527 and then Nkind (Decl) = N_Subprogram_Body
11528 and then Acts_As_Spec (Decl)
11529 then
11530 Check_Type_Conformant (Candidate, E);
11532 else
11533 Missing_Body;
11534 end if;
11535 end;
11537 else
11538 Missing_Body;
11539 end if;
11540 end if;
11541 end if;
11542 end Post_Error;
11544 -- Local variables
11546 Pack_Id : constant Entity_Id := Current_Scope;
11548 -- Start of processing for Check_Completion
11550 begin
11551 E := First_Entity (Pack_Id);
11552 while Present (E) loop
11553 if Is_Intrinsic_Subprogram (E) then
11554 null;
11556 -- The following situation requires special handling: a child unit
11557 -- that appears in the context clause of the body of its parent:
11559 -- procedure Parent.Child (...);
11561 -- with Parent.Child;
11562 -- package body Parent is
11564 -- Here Parent.Child appears as a local entity, but should not be
11565 -- flagged as requiring completion, because it is a compilation
11566 -- unit.
11568 -- Ignore missing completion for a subprogram that does not come from
11569 -- source (including the _Call primitive operation of RAS types,
11570 -- which has to have the flag Comes_From_Source for other purposes):
11571 -- we assume that the expander will provide the missing completion.
11572 -- In case of previous errors, other expansion actions that provide
11573 -- bodies for null procedures with not be invoked, so inhibit message
11574 -- in those cases.
11576 -- Note that E_Operator is not in the list that follows, because
11577 -- this kind is reserved for predefined operators, that are
11578 -- intrinsic and do not need completion.
11580 elsif Ekind_In (E, E_Function,
11581 E_Procedure,
11582 E_Generic_Function,
11583 E_Generic_Procedure)
11584 then
11585 if Has_Completion (E) then
11586 null;
11588 elsif Is_Subprogram (E) and then Is_Abstract_Subprogram (E) then
11589 null;
11591 elsif Is_Subprogram (E)
11592 and then (not Comes_From_Source (E)
11593 or else Chars (E) = Name_uCall)
11594 then
11595 null;
11597 elsif
11598 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
11599 then
11600 null;
11602 elsif Nkind (Parent (E)) = N_Procedure_Specification
11603 and then Null_Present (Parent (E))
11604 and then Serious_Errors_Detected > 0
11605 then
11606 null;
11608 else
11609 Post_Error;
11610 end if;
11612 elsif Is_Entry (E) then
11613 if not Has_Completion (E) and then
11614 (Ekind (Scope (E)) = E_Protected_Object
11615 or else Ekind (Scope (E)) = E_Protected_Type)
11616 then
11617 Post_Error;
11618 end if;
11620 elsif Is_Package_Or_Generic_Package (E) then
11621 if Unit_Requires_Body (E) then
11622 if not Has_Completion (E)
11623 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
11624 N_Compilation_Unit
11625 then
11626 Post_Error;
11627 end if;
11629 elsif not Is_Child_Unit (E) then
11630 May_Need_Implicit_Body (E);
11631 end if;
11633 -- A formal incomplete type (Ada 2012) does not require a completion;
11634 -- other incomplete type declarations do.
11636 elsif Ekind (E) = E_Incomplete_Type
11637 and then No (Underlying_Type (E))
11638 and then not Is_Generic_Type (E)
11639 then
11640 Post_Error;
11642 elsif Ekind_In (E, E_Task_Type, E_Protected_Type)
11643 and then not Has_Completion (E)
11644 then
11645 Post_Error;
11647 -- A single task declared in the current scope is a constant, verify
11648 -- that the body of its anonymous type is in the same scope. If the
11649 -- task is defined elsewhere, this may be a renaming declaration for
11650 -- which no completion is needed.
11652 elsif Ekind (E) = E_Constant
11653 and then Ekind (Etype (E)) = E_Task_Type
11654 and then not Has_Completion (Etype (E))
11655 and then Scope (Etype (E)) = Current_Scope
11656 then
11657 Post_Error;
11659 elsif Ekind (E) = E_Protected_Object
11660 and then not Has_Completion (Etype (E))
11661 then
11662 Post_Error;
11664 elsif Ekind (E) = E_Record_Type then
11665 if Is_Tagged_Type (E) then
11666 Check_Abstract_Overriding (E);
11667 Check_Conventions (E);
11668 end if;
11670 Check_Aliased_Component_Types (E);
11672 elsif Ekind (E) = E_Array_Type then
11673 Check_Aliased_Component_Types (E);
11675 end if;
11677 Next_Entity (E);
11678 end loop;
11679 end Check_Completion;
11681 ------------------------------------
11682 -- Check_CPP_Type_Has_No_Defaults --
11683 ------------------------------------
11685 procedure Check_CPP_Type_Has_No_Defaults (T : Entity_Id) is
11686 Tdef : constant Node_Id := Type_Definition (Declaration_Node (T));
11687 Clist : Node_Id;
11688 Comp : Node_Id;
11690 begin
11691 -- Obtain the component list
11693 if Nkind (Tdef) = N_Record_Definition then
11694 Clist := Component_List (Tdef);
11695 else pragma Assert (Nkind (Tdef) = N_Derived_Type_Definition);
11696 Clist := Component_List (Record_Extension_Part (Tdef));
11697 end if;
11699 -- Check all components to ensure no default expressions
11701 if Present (Clist) then
11702 Comp := First (Component_Items (Clist));
11703 while Present (Comp) loop
11704 if Present (Expression (Comp)) then
11705 Error_Msg_N
11706 ("component of imported 'C'P'P type cannot have "
11707 & "default expression", Expression (Comp));
11708 end if;
11710 Next (Comp);
11711 end loop;
11712 end if;
11713 end Check_CPP_Type_Has_No_Defaults;
11715 ----------------------------
11716 -- Check_Delta_Expression --
11717 ----------------------------
11719 procedure Check_Delta_Expression (E : Node_Id) is
11720 begin
11721 if not (Is_Real_Type (Etype (E))) then
11722 Wrong_Type (E, Any_Real);
11724 elsif not Is_OK_Static_Expression (E) then
11725 Flag_Non_Static_Expr
11726 ("non-static expression used for delta value!", E);
11728 elsif not UR_Is_Positive (Expr_Value_R (E)) then
11729 Error_Msg_N ("delta expression must be positive", E);
11731 else
11732 return;
11733 end if;
11735 -- If any of above errors occurred, then replace the incorrect
11736 -- expression by the real 0.1, which should prevent further errors.
11738 Rewrite (E,
11739 Make_Real_Literal (Sloc (E), Ureal_Tenth));
11740 Analyze_And_Resolve (E, Standard_Float);
11741 end Check_Delta_Expression;
11743 -----------------------------
11744 -- Check_Digits_Expression --
11745 -----------------------------
11747 procedure Check_Digits_Expression (E : Node_Id) is
11748 begin
11749 if not (Is_Integer_Type (Etype (E))) then
11750 Wrong_Type (E, Any_Integer);
11752 elsif not Is_OK_Static_Expression (E) then
11753 Flag_Non_Static_Expr
11754 ("non-static expression used for digits value!", E);
11756 elsif Expr_Value (E) <= 0 then
11757 Error_Msg_N ("digits value must be greater than zero", E);
11759 else
11760 return;
11761 end if;
11763 -- If any of above errors occurred, then replace the incorrect
11764 -- expression by the integer 1, which should prevent further errors.
11766 Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
11767 Analyze_And_Resolve (E, Standard_Integer);
11769 end Check_Digits_Expression;
11771 --------------------------
11772 -- Check_Initialization --
11773 --------------------------
11775 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
11776 begin
11777 -- Special processing for limited types
11779 if Is_Limited_Type (T)
11780 and then not In_Instance
11781 and then not In_Inlined_Body
11782 then
11783 if not OK_For_Limited_Init (T, Exp) then
11785 -- In GNAT mode, this is just a warning, to allow it to be evilly
11786 -- turned off. Otherwise it is a real error.
11788 if GNAT_Mode then
11789 Error_Msg_N
11790 ("??cannot initialize entities of limited type!", Exp);
11792 elsif Ada_Version < Ada_2005 then
11794 -- The side effect removal machinery may generate illegal Ada
11795 -- code to avoid the usage of access types and 'reference in
11796 -- SPARK mode. Since this is legal code with respect to theorem
11797 -- proving, do not emit the error.
11799 if GNATprove_Mode
11800 and then Nkind (Exp) = N_Function_Call
11801 and then Nkind (Parent (Exp)) = N_Object_Declaration
11802 and then not Comes_From_Source
11803 (Defining_Identifier (Parent (Exp)))
11804 then
11805 null;
11807 else
11808 Error_Msg_N
11809 ("cannot initialize entities of limited type", Exp);
11810 Explain_Limited_Type (T, Exp);
11811 end if;
11813 else
11814 -- Specialize error message according to kind of illegal
11815 -- initial expression.
11817 if Nkind (Exp) = N_Type_Conversion
11818 and then Nkind (Expression (Exp)) = N_Function_Call
11819 then
11820 -- No error for internally-generated object declarations,
11821 -- which can come from build-in-place assignment statements.
11823 if Nkind (Parent (Exp)) = N_Object_Declaration
11824 and then not Comes_From_Source
11825 (Defining_Identifier (Parent (Exp)))
11826 then
11827 null;
11829 else
11830 Error_Msg_N
11831 ("illegal context for call to function with limited "
11832 & "result", Exp);
11833 end if;
11835 else
11836 Error_Msg_N
11837 ("initialization of limited object requires aggregate or "
11838 & "function call", Exp);
11839 end if;
11840 end if;
11841 end if;
11842 end if;
11844 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag gets
11845 -- set unless we can be sure that no range check is required.
11847 if (GNATprove_Mode or not Expander_Active)
11848 and then Is_Scalar_Type (T)
11849 and then not Is_In_Range (Exp, T, Assume_Valid => True)
11850 then
11851 Set_Do_Range_Check (Exp);
11852 end if;
11853 end Check_Initialization;
11855 ----------------------
11856 -- Check_Interfaces --
11857 ----------------------
11859 procedure Check_Interfaces (N : Node_Id; Def : Node_Id) is
11860 Parent_Type : constant Entity_Id := Etype (Defining_Identifier (N));
11862 Iface : Node_Id;
11863 Iface_Def : Node_Id;
11864 Iface_Typ : Entity_Id;
11865 Parent_Node : Node_Id;
11867 Is_Task : Boolean := False;
11868 -- Set True if parent type or any progenitor is a task interface
11870 Is_Protected : Boolean := False;
11871 -- Set True if parent type or any progenitor is a protected interface
11873 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
11874 -- Check that a progenitor is compatible with declaration. If an error
11875 -- message is output, it is posted on Error_Node.
11877 ------------------
11878 -- Check_Ifaces --
11879 ------------------
11881 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
11882 Iface_Id : constant Entity_Id :=
11883 Defining_Identifier (Parent (Iface_Def));
11884 Type_Def : Node_Id;
11886 begin
11887 if Nkind (N) = N_Private_Extension_Declaration then
11888 Type_Def := N;
11889 else
11890 Type_Def := Type_Definition (N);
11891 end if;
11893 if Is_Task_Interface (Iface_Id) then
11894 Is_Task := True;
11896 elsif Is_Protected_Interface (Iface_Id) then
11897 Is_Protected := True;
11898 end if;
11900 if Is_Synchronized_Interface (Iface_Id) then
11902 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
11903 -- extension derived from a synchronized interface must explicitly
11904 -- be declared synchronized, because the full view will be a
11905 -- synchronized type.
11907 if Nkind (N) = N_Private_Extension_Declaration then
11908 if not Synchronized_Present (N) then
11909 Error_Msg_NE
11910 ("private extension of& must be explicitly synchronized",
11911 N, Iface_Id);
11912 end if;
11914 -- However, by 3.9.4(16/2), a full type that is a record extension
11915 -- is never allowed to derive from a synchronized interface (note
11916 -- that interfaces must be excluded from this check, because those
11917 -- are represented by derived type definitions in some cases).
11919 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition
11920 and then not Interface_Present (Type_Definition (N))
11921 then
11922 Error_Msg_N ("record extension cannot derive from synchronized "
11923 & "interface", Error_Node);
11924 end if;
11925 end if;
11927 -- Check that the characteristics of the progenitor are compatible
11928 -- with the explicit qualifier in the declaration.
11929 -- The check only applies to qualifiers that come from source.
11930 -- Limited_Present also appears in the declaration of corresponding
11931 -- records, and the check does not apply to them.
11933 if Limited_Present (Type_Def)
11934 and then not
11935 Is_Concurrent_Record_Type (Defining_Identifier (N))
11936 then
11937 if Is_Limited_Interface (Parent_Type)
11938 and then not Is_Limited_Interface (Iface_Id)
11939 then
11940 Error_Msg_NE
11941 ("progenitor & must be limited interface",
11942 Error_Node, Iface_Id);
11944 elsif
11945 (Task_Present (Iface_Def)
11946 or else Protected_Present (Iface_Def)
11947 or else Synchronized_Present (Iface_Def))
11948 and then Nkind (N) /= N_Private_Extension_Declaration
11949 and then not Error_Posted (N)
11950 then
11951 Error_Msg_NE
11952 ("progenitor & must be limited interface",
11953 Error_Node, Iface_Id);
11954 end if;
11956 -- Protected interfaces can only inherit from limited, synchronized
11957 -- or protected interfaces.
11959 elsif Nkind (N) = N_Full_Type_Declaration
11960 and then Protected_Present (Type_Def)
11961 then
11962 if Limited_Present (Iface_Def)
11963 or else Synchronized_Present (Iface_Def)
11964 or else Protected_Present (Iface_Def)
11965 then
11966 null;
11968 elsif Task_Present (Iface_Def) then
11969 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
11970 & "from task interface", Error_Node);
11972 else
11973 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
11974 & "from non-limited interface", Error_Node);
11975 end if;
11977 -- Ada 2005 (AI-345): Synchronized interfaces can only inherit from
11978 -- limited and synchronized.
11980 elsif Synchronized_Present (Type_Def) then
11981 if Limited_Present (Iface_Def)
11982 or else Synchronized_Present (Iface_Def)
11983 then
11984 null;
11986 elsif Protected_Present (Iface_Def)
11987 and then Nkind (N) /= N_Private_Extension_Declaration
11988 then
11989 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11990 & "from protected interface", Error_Node);
11992 elsif Task_Present (Iface_Def)
11993 and then Nkind (N) /= N_Private_Extension_Declaration
11994 then
11995 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11996 & "from task interface", Error_Node);
11998 elsif not Is_Limited_Interface (Iface_Id) then
11999 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
12000 & "from non-limited interface", Error_Node);
12001 end if;
12003 -- Ada 2005 (AI-345): Task interfaces can only inherit from limited,
12004 -- synchronized or task interfaces.
12006 elsif Nkind (N) = N_Full_Type_Declaration
12007 and then Task_Present (Type_Def)
12008 then
12009 if Limited_Present (Iface_Def)
12010 or else Synchronized_Present (Iface_Def)
12011 or else Task_Present (Iface_Def)
12012 then
12013 null;
12015 elsif Protected_Present (Iface_Def) then
12016 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
12017 & "protected interface", Error_Node);
12019 else
12020 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
12021 & "non-limited interface", Error_Node);
12022 end if;
12023 end if;
12024 end Check_Ifaces;
12026 -- Start of processing for Check_Interfaces
12028 begin
12029 if Is_Interface (Parent_Type) then
12030 if Is_Task_Interface (Parent_Type) then
12031 Is_Task := True;
12033 elsif Is_Protected_Interface (Parent_Type) then
12034 Is_Protected := True;
12035 end if;
12036 end if;
12038 if Nkind (N) = N_Private_Extension_Declaration then
12040 -- Check that progenitors are compatible with declaration
12042 Iface := First (Interface_List (Def));
12043 while Present (Iface) loop
12044 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
12046 Parent_Node := Parent (Base_Type (Iface_Typ));
12047 Iface_Def := Type_Definition (Parent_Node);
12049 if not Is_Interface (Iface_Typ) then
12050 Diagnose_Interface (Iface, Iface_Typ);
12051 else
12052 Check_Ifaces (Iface_Def, Iface);
12053 end if;
12055 Next (Iface);
12056 end loop;
12058 if Is_Task and Is_Protected then
12059 Error_Msg_N
12060 ("type cannot derive from task and protected interface", N);
12061 end if;
12063 return;
12064 end if;
12066 -- Full type declaration of derived type.
12067 -- Check compatibility with parent if it is interface type
12069 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
12070 and then Is_Interface (Parent_Type)
12071 then
12072 Parent_Node := Parent (Parent_Type);
12074 -- More detailed checks for interface varieties
12076 Check_Ifaces
12077 (Iface_Def => Type_Definition (Parent_Node),
12078 Error_Node => Subtype_Indication (Type_Definition (N)));
12079 end if;
12081 Iface := First (Interface_List (Def));
12082 while Present (Iface) loop
12083 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
12085 Parent_Node := Parent (Base_Type (Iface_Typ));
12086 Iface_Def := Type_Definition (Parent_Node);
12088 if not Is_Interface (Iface_Typ) then
12089 Diagnose_Interface (Iface, Iface_Typ);
12091 else
12092 -- "The declaration of a specific descendant of an interface
12093 -- type freezes the interface type" RM 13.14
12095 Freeze_Before (N, Iface_Typ);
12096 Check_Ifaces (Iface_Def, Error_Node => Iface);
12097 end if;
12099 Next (Iface);
12100 end loop;
12102 if Is_Task and Is_Protected then
12103 Error_Msg_N
12104 ("type cannot derive from task and protected interface", N);
12105 end if;
12106 end Check_Interfaces;
12108 ------------------------------------
12109 -- Check_Or_Process_Discriminants --
12110 ------------------------------------
12112 -- If an incomplete or private type declaration was already given for the
12113 -- type, the discriminants may have already been processed if they were
12114 -- present on the incomplete declaration. In this case a full conformance
12115 -- check has been performed in Find_Type_Name, and we then recheck here
12116 -- some properties that can't be checked on the partial view alone.
12117 -- Otherwise we call Process_Discriminants.
12119 procedure Check_Or_Process_Discriminants
12120 (N : Node_Id;
12121 T : Entity_Id;
12122 Prev : Entity_Id := Empty)
12124 begin
12125 if Has_Discriminants (T) then
12127 -- Discriminants are already set on T if they were already present
12128 -- on the partial view. Make them visible to component declarations.
12130 declare
12131 D : Entity_Id;
12132 -- Discriminant on T (full view) referencing expr on partial view
12134 Prev_D : Entity_Id;
12135 -- Entity of corresponding discriminant on partial view
12137 New_D : Node_Id;
12138 -- Discriminant specification for full view, expression is
12139 -- the syntactic copy on full view (which has been checked for
12140 -- conformance with partial view), only used here to post error
12141 -- message.
12143 begin
12144 D := First_Discriminant (T);
12145 New_D := First (Discriminant_Specifications (N));
12146 while Present (D) loop
12147 Prev_D := Current_Entity (D);
12148 Set_Current_Entity (D);
12149 Set_Is_Immediately_Visible (D);
12150 Set_Homonym (D, Prev_D);
12152 -- Handle the case where there is an untagged partial view and
12153 -- the full view is tagged: must disallow discriminants with
12154 -- defaults, unless compiling for Ada 2012, which allows a
12155 -- limited tagged type to have defaulted discriminants (see
12156 -- AI05-0214). However, suppress error here if it was already
12157 -- reported on the default expression of the partial view.
12159 if Is_Tagged_Type (T)
12160 and then Present (Expression (Parent (D)))
12161 and then (not Is_Limited_Type (Current_Scope)
12162 or else Ada_Version < Ada_2012)
12163 and then not Error_Posted (Expression (Parent (D)))
12164 then
12165 if Ada_Version >= Ada_2012 then
12166 Error_Msg_N
12167 ("discriminants of nonlimited tagged type cannot have "
12168 & "defaults",
12169 Expression (New_D));
12170 else
12171 Error_Msg_N
12172 ("discriminants of tagged type cannot have defaults",
12173 Expression (New_D));
12174 end if;
12175 end if;
12177 -- Ada 2005 (AI-230): Access discriminant allowed in
12178 -- non-limited record types.
12180 if Ada_Version < Ada_2005 then
12182 -- This restriction gets applied to the full type here. It
12183 -- has already been applied earlier to the partial view.
12185 Check_Access_Discriminant_Requires_Limited (Parent (D), N);
12186 end if;
12188 Next_Discriminant (D);
12189 Next (New_D);
12190 end loop;
12191 end;
12193 elsif Present (Discriminant_Specifications (N)) then
12194 Process_Discriminants (N, Prev);
12195 end if;
12196 end Check_Or_Process_Discriminants;
12198 ----------------------
12199 -- Check_Real_Bound --
12200 ----------------------
12202 procedure Check_Real_Bound (Bound : Node_Id) is
12203 begin
12204 if not Is_Real_Type (Etype (Bound)) then
12205 Error_Msg_N
12206 ("bound in real type definition must be of real type", Bound);
12208 elsif not Is_OK_Static_Expression (Bound) then
12209 Flag_Non_Static_Expr
12210 ("non-static expression used for real type bound!", Bound);
12212 else
12213 return;
12214 end if;
12216 Rewrite
12217 (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
12218 Analyze (Bound);
12219 Resolve (Bound, Standard_Float);
12220 end Check_Real_Bound;
12222 ------------------------------
12223 -- Complete_Private_Subtype --
12224 ------------------------------
12226 procedure Complete_Private_Subtype
12227 (Priv : Entity_Id;
12228 Full : Entity_Id;
12229 Full_Base : Entity_Id;
12230 Related_Nod : Node_Id)
12232 Save_Next_Entity : Entity_Id;
12233 Save_Homonym : Entity_Id;
12235 begin
12236 -- Set semantic attributes for (implicit) private subtype completion.
12237 -- If the full type has no discriminants, then it is a copy of the
12238 -- full view of the base. Otherwise, it is a subtype of the base with
12239 -- a possible discriminant constraint. Save and restore the original
12240 -- Next_Entity field of full to ensure that the calls to Copy_Node do
12241 -- not corrupt the entity chain.
12243 -- Note that the type of the full view is the same entity as the type
12244 -- of the partial view. In this fashion, the subtype has access to the
12245 -- correct view of the parent.
12247 Save_Next_Entity := Next_Entity (Full);
12248 Save_Homonym := Homonym (Priv);
12250 case Ekind (Full_Base) is
12251 when Class_Wide_Kind
12252 | Private_Kind
12253 | Protected_Kind
12254 | Task_Kind
12255 | E_Record_Subtype
12256 | E_Record_Type
12258 Copy_Node (Priv, Full);
12260 Set_Has_Discriminants
12261 (Full, Has_Discriminants (Full_Base));
12262 Set_Has_Unknown_Discriminants
12263 (Full, Has_Unknown_Discriminants (Full_Base));
12264 Set_First_Entity (Full, First_Entity (Full_Base));
12265 Set_Last_Entity (Full, Last_Entity (Full_Base));
12267 -- If the underlying base type is constrained, we know that the
12268 -- full view of the subtype is constrained as well (the converse
12269 -- is not necessarily true).
12271 if Is_Constrained (Full_Base) then
12272 Set_Is_Constrained (Full);
12273 end if;
12275 when others =>
12276 Copy_Node (Full_Base, Full);
12278 Set_Chars (Full, Chars (Priv));
12279 Conditional_Delay (Full, Priv);
12280 Set_Sloc (Full, Sloc (Priv));
12281 end case;
12283 Set_Next_Entity (Full, Save_Next_Entity);
12284 Set_Homonym (Full, Save_Homonym);
12285 Set_Associated_Node_For_Itype (Full, Related_Nod);
12287 -- Set common attributes for all subtypes: kind, convention, etc.
12289 Set_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
12290 Set_Convention (Full, Convention (Full_Base));
12292 -- The Etype of the full view is inconsistent. Gigi needs to see the
12293 -- structural full view, which is what the current scheme gives: the
12294 -- Etype of the full view is the etype of the full base. However, if the
12295 -- full base is a derived type, the full view then looks like a subtype
12296 -- of the parent, not a subtype of the full base. If instead we write:
12298 -- Set_Etype (Full, Full_Base);
12300 -- then we get inconsistencies in the front-end (confusion between
12301 -- views). Several outstanding bugs are related to this ???
12303 Set_Is_First_Subtype (Full, False);
12304 Set_Scope (Full, Scope (Priv));
12305 Set_Size_Info (Full, Full_Base);
12306 Set_RM_Size (Full, RM_Size (Full_Base));
12307 Set_Is_Itype (Full);
12309 -- A subtype of a private-type-without-discriminants, whose full-view
12310 -- has discriminants with default expressions, is not constrained.
12312 if not Has_Discriminants (Priv) then
12313 Set_Is_Constrained (Full, Is_Constrained (Full_Base));
12315 if Has_Discriminants (Full_Base) then
12316 Set_Discriminant_Constraint
12317 (Full, Discriminant_Constraint (Full_Base));
12319 -- The partial view may have been indefinite, the full view
12320 -- might not be.
12322 Set_Has_Unknown_Discriminants
12323 (Full, Has_Unknown_Discriminants (Full_Base));
12324 end if;
12325 end if;
12327 Set_First_Rep_Item (Full, First_Rep_Item (Full_Base));
12328 Set_Depends_On_Private (Full, Has_Private_Component (Full));
12330 -- Freeze the private subtype entity if its parent is delayed, and not
12331 -- already frozen. We skip this processing if the type is an anonymous
12332 -- subtype of a record component, or is the corresponding record of a
12333 -- protected type, since these are processed when the enclosing type
12334 -- is frozen. If the parent type is declared in a nested package then
12335 -- the freezing of the private and full views also happens later.
12337 if not Is_Type (Scope (Full)) then
12338 if Is_Itype (Priv)
12339 and then In_Same_Source_Unit (Full, Full_Base)
12340 and then Scope (Full_Base) /= Scope (Full)
12341 then
12342 Set_Has_Delayed_Freeze (Full);
12343 Set_Has_Delayed_Freeze (Priv);
12345 else
12346 Set_Has_Delayed_Freeze (Full,
12347 Has_Delayed_Freeze (Full_Base)
12348 and then not Is_Frozen (Full_Base));
12349 end if;
12350 end if;
12352 Set_Freeze_Node (Full, Empty);
12353 Set_Is_Frozen (Full, False);
12354 Set_Full_View (Priv, Full);
12356 if Has_Discriminants (Full) then
12357 Set_Stored_Constraint_From_Discriminant_Constraint (Full);
12358 Set_Stored_Constraint (Priv, Stored_Constraint (Full));
12360 if Has_Unknown_Discriminants (Full) then
12361 Set_Discriminant_Constraint (Full, No_Elist);
12362 end if;
12363 end if;
12365 if Ekind (Full_Base) = E_Record_Type
12366 and then Has_Discriminants (Full_Base)
12367 and then Has_Discriminants (Priv) -- might not, if errors
12368 and then not Has_Unknown_Discriminants (Priv)
12369 and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
12370 then
12371 Create_Constrained_Components
12372 (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
12374 -- If the full base is itself derived from private, build a congruent
12375 -- subtype of its underlying type, for use by the back end. For a
12376 -- constrained record component, the declaration cannot be placed on
12377 -- the component list, but it must nevertheless be built an analyzed, to
12378 -- supply enough information for Gigi to compute the size of component.
12380 elsif Ekind (Full_Base) in Private_Kind
12381 and then Is_Derived_Type (Full_Base)
12382 and then Has_Discriminants (Full_Base)
12383 and then (Ekind (Current_Scope) /= E_Record_Subtype)
12384 then
12385 if not Is_Itype (Priv)
12386 and then
12387 Nkind (Subtype_Indication (Parent (Priv))) = N_Subtype_Indication
12388 then
12389 Build_Underlying_Full_View
12390 (Parent (Priv), Full, Etype (Full_Base));
12392 elsif Nkind (Related_Nod) = N_Component_Declaration then
12393 Build_Underlying_Full_View (Related_Nod, Full, Etype (Full_Base));
12394 end if;
12396 elsif Is_Record_Type (Full_Base) then
12398 -- Show Full is simply a renaming of Full_Base
12400 Set_Cloned_Subtype (Full, Full_Base);
12401 end if;
12403 -- It is unsafe to share the bounds of a scalar type, because the Itype
12404 -- is elaborated on demand, and if a bound is non-static then different
12405 -- orders of elaboration in different units will lead to different
12406 -- external symbols.
12408 if Is_Scalar_Type (Full_Base) then
12409 Set_Scalar_Range (Full,
12410 Make_Range (Sloc (Related_Nod),
12411 Low_Bound =>
12412 Duplicate_Subexpr_No_Checks (Type_Low_Bound (Full_Base)),
12413 High_Bound =>
12414 Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
12416 -- This completion inherits the bounds of the full parent, but if
12417 -- the parent is an unconstrained floating point type, so is the
12418 -- completion.
12420 if Is_Floating_Point_Type (Full_Base) then
12421 Set_Includes_Infinities
12422 (Scalar_Range (Full), Has_Infinities (Full_Base));
12423 end if;
12424 end if;
12426 -- ??? It seems that a lot of fields are missing that should be copied
12427 -- from Full_Base to Full. Here are some that are introduced in a
12428 -- non-disruptive way but a cleanup is necessary.
12430 if Is_Tagged_Type (Full_Base) then
12431 Set_Is_Tagged_Type (Full);
12432 Set_Direct_Primitive_Operations
12433 (Full, Direct_Primitive_Operations (Full_Base));
12434 Set_No_Tagged_Streams_Pragma
12435 (Full, No_Tagged_Streams_Pragma (Full_Base));
12437 -- Inherit class_wide type of full_base in case the partial view was
12438 -- not tagged. Otherwise it has already been created when the private
12439 -- subtype was analyzed.
12441 if No (Class_Wide_Type (Full)) then
12442 Set_Class_Wide_Type (Full, Class_Wide_Type (Full_Base));
12443 end if;
12445 -- If this is a subtype of a protected or task type, constrain its
12446 -- corresponding record, unless this is a subtype without constraints,
12447 -- i.e. a simple renaming as with an actual subtype in an instance.
12449 elsif Is_Concurrent_Type (Full_Base) then
12450 if Has_Discriminants (Full)
12451 and then Present (Corresponding_Record_Type (Full_Base))
12452 and then
12453 not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
12454 then
12455 Set_Corresponding_Record_Type (Full,
12456 Constrain_Corresponding_Record
12457 (Full, Corresponding_Record_Type (Full_Base), Related_Nod));
12459 else
12460 Set_Corresponding_Record_Type (Full,
12461 Corresponding_Record_Type (Full_Base));
12462 end if;
12463 end if;
12465 -- Link rep item chain, and also setting of Has_Predicates from private
12466 -- subtype to full subtype, since we will need these on the full subtype
12467 -- to create the predicate function. Note that the full subtype may
12468 -- already have rep items, inherited from the full view of the base
12469 -- type, so we must be sure not to overwrite these entries.
12471 declare
12472 Append : Boolean;
12473 Item : Node_Id;
12474 Next_Item : Node_Id;
12475 Priv_Item : Node_Id;
12477 begin
12478 Item := First_Rep_Item (Full);
12479 Priv_Item := First_Rep_Item (Priv);
12481 -- If no existing rep items on full type, we can just link directly
12482 -- to the list of items on the private type, if any exist.. Same if
12483 -- the rep items are only those inherited from the base
12485 if (No (Item)
12486 or else Nkind (Item) /= N_Aspect_Specification
12487 or else Entity (Item) = Full_Base)
12488 and then Present (First_Rep_Item (Priv))
12489 then
12490 Set_First_Rep_Item (Full, Priv_Item);
12492 -- Otherwise, search to the end of items currently linked to the full
12493 -- subtype and append the private items to the end. However, if Priv
12494 -- and Full already have the same list of rep items, then the append
12495 -- is not done, as that would create a circularity.
12497 -- The partial view may have a predicate and the rep item lists of
12498 -- both views agree when inherited from the same ancestor. In that
12499 -- case, simply propagate the list from one view to the other.
12500 -- A more complex analysis needed here ???
12502 elsif Present (Priv_Item)
12503 and then Item = Next_Rep_Item (Priv_Item)
12504 then
12505 Set_First_Rep_Item (Full, Priv_Item);
12507 elsif Item /= Priv_Item then
12508 Append := True;
12509 loop
12510 Next_Item := Next_Rep_Item (Item);
12511 exit when No (Next_Item);
12512 Item := Next_Item;
12514 -- If the private view has aspect specifications, the full view
12515 -- inherits them. Since these aspects may already have been
12516 -- attached to the full view during derivation, do not append
12517 -- them if already present.
12519 if Item = First_Rep_Item (Priv) then
12520 Append := False;
12521 exit;
12522 end if;
12523 end loop;
12525 -- And link the private type items at the end of the chain
12527 if Append then
12528 Set_Next_Rep_Item (Item, First_Rep_Item (Priv));
12529 end if;
12530 end if;
12531 end;
12533 -- Make sure Has_Predicates is set on full type if it is set on the
12534 -- private type. Note that it may already be set on the full type and
12535 -- if so, we don't want to unset it. Similarly, propagate information
12536 -- about delayed aspects, because the corresponding pragmas must be
12537 -- analyzed when one of the views is frozen. This last step is needed
12538 -- in particular when the full type is a scalar type for which an
12539 -- anonymous base type is constructed.
12541 -- The predicate functions are generated either at the freeze point
12542 -- of the type or at the end of the visible part, and we must avoid
12543 -- generating them twice.
12545 if Has_Predicates (Priv) then
12546 Set_Has_Predicates (Full);
12548 if Present (Predicate_Function (Priv))
12549 and then No (Predicate_Function (Full))
12550 then
12551 Set_Predicate_Function (Full, Predicate_Function (Priv));
12552 end if;
12553 end if;
12555 if Has_Delayed_Aspects (Priv) then
12556 Set_Has_Delayed_Aspects (Full);
12557 end if;
12558 end Complete_Private_Subtype;
12560 ----------------------------
12561 -- Constant_Redeclaration --
12562 ----------------------------
12564 procedure Constant_Redeclaration
12565 (Id : Entity_Id;
12566 N : Node_Id;
12567 T : out Entity_Id)
12569 Prev : constant Entity_Id := Current_Entity_In_Scope (Id);
12570 Obj_Def : constant Node_Id := Object_Definition (N);
12571 New_T : Entity_Id;
12573 procedure Check_Possible_Deferred_Completion
12574 (Prev_Id : Entity_Id;
12575 Prev_Obj_Def : Node_Id;
12576 Curr_Obj_Def : Node_Id);
12577 -- Determine whether the two object definitions describe the partial
12578 -- and the full view of a constrained deferred constant. Generate
12579 -- a subtype for the full view and verify that it statically matches
12580 -- the subtype of the partial view.
12582 procedure Check_Recursive_Declaration (Typ : Entity_Id);
12583 -- If deferred constant is an access type initialized with an allocator,
12584 -- check whether there is an illegal recursion in the definition,
12585 -- through a default value of some record subcomponent. This is normally
12586 -- detected when generating init procs, but requires this additional
12587 -- mechanism when expansion is disabled.
12589 ----------------------------------------
12590 -- Check_Possible_Deferred_Completion --
12591 ----------------------------------------
12593 procedure Check_Possible_Deferred_Completion
12594 (Prev_Id : Entity_Id;
12595 Prev_Obj_Def : Node_Id;
12596 Curr_Obj_Def : Node_Id)
12598 begin
12599 if Nkind (Prev_Obj_Def) = N_Subtype_Indication
12600 and then Present (Constraint (Prev_Obj_Def))
12601 and then Nkind (Curr_Obj_Def) = N_Subtype_Indication
12602 and then Present (Constraint (Curr_Obj_Def))
12603 then
12604 declare
12605 Loc : constant Source_Ptr := Sloc (N);
12606 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
12607 Decl : constant Node_Id :=
12608 Make_Subtype_Declaration (Loc,
12609 Defining_Identifier => Def_Id,
12610 Subtype_Indication =>
12611 Relocate_Node (Curr_Obj_Def));
12613 begin
12614 Insert_Before_And_Analyze (N, Decl);
12615 Set_Etype (Id, Def_Id);
12617 if not Subtypes_Statically_Match (Etype (Prev_Id), Def_Id) then
12618 Error_Msg_Sloc := Sloc (Prev_Id);
12619 Error_Msg_N ("subtype does not statically match deferred "
12620 & "declaration #", N);
12621 end if;
12622 end;
12623 end if;
12624 end Check_Possible_Deferred_Completion;
12626 ---------------------------------
12627 -- Check_Recursive_Declaration --
12628 ---------------------------------
12630 procedure Check_Recursive_Declaration (Typ : Entity_Id) is
12631 Comp : Entity_Id;
12633 begin
12634 if Is_Record_Type (Typ) then
12635 Comp := First_Component (Typ);
12636 while Present (Comp) loop
12637 if Comes_From_Source (Comp) then
12638 if Present (Expression (Parent (Comp)))
12639 and then Is_Entity_Name (Expression (Parent (Comp)))
12640 and then Entity (Expression (Parent (Comp))) = Prev
12641 then
12642 Error_Msg_Sloc := Sloc (Parent (Comp));
12643 Error_Msg_NE
12644 ("illegal circularity with declaration for & #",
12645 N, Comp);
12646 return;
12648 elsif Is_Record_Type (Etype (Comp)) then
12649 Check_Recursive_Declaration (Etype (Comp));
12650 end if;
12651 end if;
12653 Next_Component (Comp);
12654 end loop;
12655 end if;
12656 end Check_Recursive_Declaration;
12658 -- Start of processing for Constant_Redeclaration
12660 begin
12661 if Nkind (Parent (Prev)) = N_Object_Declaration then
12662 if Nkind (Object_Definition
12663 (Parent (Prev))) = N_Subtype_Indication
12664 then
12665 -- Find type of new declaration. The constraints of the two
12666 -- views must match statically, but there is no point in
12667 -- creating an itype for the full view.
12669 if Nkind (Obj_Def) = N_Subtype_Indication then
12670 Find_Type (Subtype_Mark (Obj_Def));
12671 New_T := Entity (Subtype_Mark (Obj_Def));
12673 else
12674 Find_Type (Obj_Def);
12675 New_T := Entity (Obj_Def);
12676 end if;
12678 T := Etype (Prev);
12680 else
12681 -- The full view may impose a constraint, even if the partial
12682 -- view does not, so construct the subtype.
12684 New_T := Find_Type_Of_Object (Obj_Def, N);
12685 T := New_T;
12686 end if;
12688 else
12689 -- Current declaration is illegal, diagnosed below in Enter_Name
12691 T := Empty;
12692 New_T := Any_Type;
12693 end if;
12695 -- If previous full declaration or a renaming declaration exists, or if
12696 -- a homograph is present, let Enter_Name handle it, either with an
12697 -- error or with the removal of an overridden implicit subprogram.
12698 -- The previous one is a full declaration if it has an expression
12699 -- (which in the case of an aggregate is indicated by the Init flag).
12701 if Ekind (Prev) /= E_Constant
12702 or else Nkind (Parent (Prev)) = N_Object_Renaming_Declaration
12703 or else Present (Expression (Parent (Prev)))
12704 or else Has_Init_Expression (Parent (Prev))
12705 or else Present (Full_View (Prev))
12706 then
12707 Enter_Name (Id);
12709 -- Verify that types of both declarations match, or else that both types
12710 -- are anonymous access types whose designated subtypes statically match
12711 -- (as allowed in Ada 2005 by AI-385).
12713 elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
12714 and then
12715 (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
12716 or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
12717 or else Is_Access_Constant (Etype (New_T)) /=
12718 Is_Access_Constant (Etype (Prev))
12719 or else Can_Never_Be_Null (Etype (New_T)) /=
12720 Can_Never_Be_Null (Etype (Prev))
12721 or else Null_Exclusion_Present (Parent (Prev)) /=
12722 Null_Exclusion_Present (Parent (Id))
12723 or else not Subtypes_Statically_Match
12724 (Designated_Type (Etype (Prev)),
12725 Designated_Type (Etype (New_T))))
12726 then
12727 Error_Msg_Sloc := Sloc (Prev);
12728 Error_Msg_N ("type does not match declaration#", N);
12729 Set_Full_View (Prev, Id);
12730 Set_Etype (Id, Any_Type);
12732 -- A deferred constant whose type is an anonymous array is always
12733 -- illegal (unless imported). A detailed error message might be
12734 -- helpful for Ada beginners.
12736 if Nkind (Object_Definition (Parent (Prev)))
12737 = N_Constrained_Array_Definition
12738 and then Nkind (Object_Definition (N))
12739 = N_Constrained_Array_Definition
12740 then
12741 Error_Msg_N ("\each anonymous array is a distinct type", N);
12742 Error_Msg_N ("a deferred constant must have a named type",
12743 Object_Definition (Parent (Prev)));
12744 end if;
12746 elsif
12747 Null_Exclusion_Present (Parent (Prev))
12748 and then not Null_Exclusion_Present (N)
12749 then
12750 Error_Msg_Sloc := Sloc (Prev);
12751 Error_Msg_N ("null-exclusion does not match declaration#", N);
12752 Set_Full_View (Prev, Id);
12753 Set_Etype (Id, Any_Type);
12755 -- If so, process the full constant declaration
12757 else
12758 -- RM 7.4 (6): If the subtype defined by the subtype_indication in
12759 -- the deferred declaration is constrained, then the subtype defined
12760 -- by the subtype_indication in the full declaration shall match it
12761 -- statically.
12763 Check_Possible_Deferred_Completion
12764 (Prev_Id => Prev,
12765 Prev_Obj_Def => Object_Definition (Parent (Prev)),
12766 Curr_Obj_Def => Obj_Def);
12768 Set_Full_View (Prev, Id);
12769 Set_Is_Public (Id, Is_Public (Prev));
12770 Set_Is_Internal (Id);
12771 Append_Entity (Id, Current_Scope);
12773 -- Check ALIASED present if present before (RM 7.4(7))
12775 if Is_Aliased (Prev)
12776 and then not Aliased_Present (N)
12777 then
12778 Error_Msg_Sloc := Sloc (Prev);
12779 Error_Msg_N ("ALIASED required (see declaration #)", N);
12780 end if;
12782 -- Check that placement is in private part and that the incomplete
12783 -- declaration appeared in the visible part.
12785 if Ekind (Current_Scope) = E_Package
12786 and then not In_Private_Part (Current_Scope)
12787 then
12788 Error_Msg_Sloc := Sloc (Prev);
12789 Error_Msg_N
12790 ("full constant for declaration # must be in private part", N);
12792 elsif Ekind (Current_Scope) = E_Package
12793 and then
12794 List_Containing (Parent (Prev)) /=
12795 Visible_Declarations (Package_Specification (Current_Scope))
12796 then
12797 Error_Msg_N
12798 ("deferred constant must be declared in visible part",
12799 Parent (Prev));
12800 end if;
12802 if Is_Access_Type (T)
12803 and then Nkind (Expression (N)) = N_Allocator
12804 then
12805 Check_Recursive_Declaration (Designated_Type (T));
12806 end if;
12808 -- A deferred constant is a visible entity. If type has invariants,
12809 -- verify that the initial value satisfies them. This is not done in
12810 -- GNATprove mode, as GNATprove handles invariant checks itself.
12812 if Has_Invariants (T)
12813 and then Present (Invariant_Procedure (T))
12814 and then not GNATprove_Mode
12815 then
12816 Insert_After (N,
12817 Make_Invariant_Call (New_Occurrence_Of (Prev, Sloc (N))));
12818 end if;
12819 end if;
12820 end Constant_Redeclaration;
12822 ----------------------
12823 -- Constrain_Access --
12824 ----------------------
12826 procedure Constrain_Access
12827 (Def_Id : in out Entity_Id;
12828 S : Node_Id;
12829 Related_Nod : Node_Id)
12831 T : constant Entity_Id := Entity (Subtype_Mark (S));
12832 Desig_Type : constant Entity_Id := Designated_Type (T);
12833 Desig_Subtype : Entity_Id := Create_Itype (E_Void, Related_Nod);
12834 Constraint_OK : Boolean := True;
12836 begin
12837 if Is_Array_Type (Desig_Type) then
12838 Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
12840 elsif (Is_Record_Type (Desig_Type)
12841 or else Is_Incomplete_Or_Private_Type (Desig_Type))
12842 and then not Is_Constrained (Desig_Type)
12843 then
12844 -- ??? The following code is a temporary bypass to ignore a
12845 -- discriminant constraint on access type if it is constraining
12846 -- the current record. Avoid creating the implicit subtype of the
12847 -- record we are currently compiling since right now, we cannot
12848 -- handle these. For now, just return the access type itself.
12850 if Desig_Type = Current_Scope
12851 and then No (Def_Id)
12852 then
12853 Set_Ekind (Desig_Subtype, E_Record_Subtype);
12854 Def_Id := Entity (Subtype_Mark (S));
12856 -- This call added to ensure that the constraint is analyzed
12857 -- (needed for a B test). Note that we still return early from
12858 -- this procedure to avoid recursive processing. ???
12860 Constrain_Discriminated_Type
12861 (Desig_Subtype, S, Related_Nod, For_Access => True);
12862 return;
12863 end if;
12865 -- Enforce rule that the constraint is illegal if there is an
12866 -- unconstrained view of the designated type. This means that the
12867 -- partial view (either a private type declaration or a derivation
12868 -- from a private type) has no discriminants. (Defect Report
12869 -- 8652/0008, Technical Corrigendum 1, checked by ACATS B371001).
12871 -- Rule updated for Ada 2005: The private type is said to have
12872 -- a constrained partial view, given that objects of the type
12873 -- can be declared. Furthermore, the rule applies to all access
12874 -- types, unlike the rule concerning default discriminants (see
12875 -- RM 3.7.1(7/3))
12877 if (Ekind (T) = E_General_Access_Type or else Ada_Version >= Ada_2005)
12878 and then Has_Private_Declaration (Desig_Type)
12879 and then In_Open_Scopes (Scope (Desig_Type))
12880 and then Has_Discriminants (Desig_Type)
12881 then
12882 declare
12883 Pack : constant Node_Id :=
12884 Unit_Declaration_Node (Scope (Desig_Type));
12885 Decls : List_Id;
12886 Decl : Node_Id;
12888 begin
12889 if Nkind (Pack) = N_Package_Declaration then
12890 Decls := Visible_Declarations (Specification (Pack));
12891 Decl := First (Decls);
12892 while Present (Decl) loop
12893 if (Nkind (Decl) = N_Private_Type_Declaration
12894 and then Chars (Defining_Identifier (Decl)) =
12895 Chars (Desig_Type))
12897 or else
12898 (Nkind (Decl) = N_Full_Type_Declaration
12899 and then
12900 Chars (Defining_Identifier (Decl)) =
12901 Chars (Desig_Type)
12902 and then Is_Derived_Type (Desig_Type)
12903 and then
12904 Has_Private_Declaration (Etype (Desig_Type)))
12905 then
12906 if No (Discriminant_Specifications (Decl)) then
12907 Error_Msg_N
12908 ("cannot constrain access type if designated "
12909 & "type has constrained partial view", S);
12910 end if;
12912 exit;
12913 end if;
12915 Next (Decl);
12916 end loop;
12917 end if;
12918 end;
12919 end if;
12921 Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
12922 For_Access => True);
12924 elsif Is_Concurrent_Type (Desig_Type)
12925 and then not Is_Constrained (Desig_Type)
12926 then
12927 Constrain_Concurrent (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
12929 else
12930 Error_Msg_N ("invalid constraint on access type", S);
12932 -- We simply ignore an invalid constraint
12934 Desig_Subtype := Desig_Type;
12935 Constraint_OK := False;
12936 end if;
12938 if No (Def_Id) then
12939 Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
12940 else
12941 Set_Ekind (Def_Id, E_Access_Subtype);
12942 end if;
12944 if Constraint_OK then
12945 Set_Etype (Def_Id, Base_Type (T));
12947 if Is_Private_Type (Desig_Type) then
12948 Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
12949 end if;
12950 else
12951 Set_Etype (Def_Id, Any_Type);
12952 end if;
12954 Set_Size_Info (Def_Id, T);
12955 Set_Is_Constrained (Def_Id, Constraint_OK);
12956 Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
12957 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
12958 Set_Is_Access_Constant (Def_Id, Is_Access_Constant (T));
12960 Conditional_Delay (Def_Id, T);
12962 -- AI-363 : Subtypes of general access types whose designated types have
12963 -- default discriminants are disallowed. In instances, the rule has to
12964 -- be checked against the actual, of which T is the subtype. In a
12965 -- generic body, the rule is checked assuming that the actual type has
12966 -- defaulted discriminants.
12968 if Ada_Version >= Ada_2005 or else Warn_On_Ada_2005_Compatibility then
12969 if Ekind (Base_Type (T)) = E_General_Access_Type
12970 and then Has_Defaulted_Discriminants (Desig_Type)
12971 then
12972 if Ada_Version < Ada_2005 then
12973 Error_Msg_N
12974 ("access subtype of general access type would not " &
12975 "be allowed in Ada 2005?y?", S);
12976 else
12977 Error_Msg_N
12978 ("access subtype of general access type not allowed", S);
12979 end if;
12981 Error_Msg_N ("\discriminants have defaults", S);
12983 elsif Is_Access_Type (T)
12984 and then Is_Generic_Type (Desig_Type)
12985 and then Has_Discriminants (Desig_Type)
12986 and then In_Package_Body (Current_Scope)
12987 then
12988 if Ada_Version < Ada_2005 then
12989 Error_Msg_N
12990 ("access subtype would not be allowed in generic body "
12991 & "in Ada 2005?y?", S);
12992 else
12993 Error_Msg_N
12994 ("access subtype not allowed in generic body", S);
12995 end if;
12997 Error_Msg_N
12998 ("\designated type is a discriminated formal", S);
12999 end if;
13000 end if;
13001 end Constrain_Access;
13003 ---------------------
13004 -- Constrain_Array --
13005 ---------------------
13007 procedure Constrain_Array
13008 (Def_Id : in out Entity_Id;
13009 SI : Node_Id;
13010 Related_Nod : Node_Id;
13011 Related_Id : Entity_Id;
13012 Suffix : Character)
13014 C : constant Node_Id := Constraint (SI);
13015 Number_Of_Constraints : Nat := 0;
13016 Index : Node_Id;
13017 S, T : Entity_Id;
13018 Constraint_OK : Boolean := True;
13020 begin
13021 T := Entity (Subtype_Mark (SI));
13023 if Is_Access_Type (T) then
13024 T := Designated_Type (T);
13025 end if;
13027 -- If an index constraint follows a subtype mark in a subtype indication
13028 -- then the type or subtype denoted by the subtype mark must not already
13029 -- impose an index constraint. The subtype mark must denote either an
13030 -- unconstrained array type or an access type whose designated type
13031 -- is such an array type... (RM 3.6.1)
13033 if Is_Constrained (T) then
13034 Error_Msg_N ("array type is already constrained", Subtype_Mark (SI));
13035 Constraint_OK := False;
13037 else
13038 S := First (Constraints (C));
13039 while Present (S) loop
13040 Number_Of_Constraints := Number_Of_Constraints + 1;
13041 Next (S);
13042 end loop;
13044 -- In either case, the index constraint must provide a discrete
13045 -- range for each index of the array type and the type of each
13046 -- discrete range must be the same as that of the corresponding
13047 -- index. (RM 3.6.1)
13049 if Number_Of_Constraints /= Number_Dimensions (T) then
13050 Error_Msg_NE ("incorrect number of index constraints for }", C, T);
13051 Constraint_OK := False;
13053 else
13054 S := First (Constraints (C));
13055 Index := First_Index (T);
13056 Analyze (Index);
13058 -- Apply constraints to each index type
13060 for J in 1 .. Number_Of_Constraints loop
13061 Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
13062 Next (Index);
13063 Next (S);
13064 end loop;
13066 end if;
13067 end if;
13069 if No (Def_Id) then
13070 Def_Id :=
13071 Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
13072 Set_Parent (Def_Id, Related_Nod);
13074 else
13075 Set_Ekind (Def_Id, E_Array_Subtype);
13076 end if;
13078 Set_Size_Info (Def_Id, (T));
13079 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13080 Set_Etype (Def_Id, Base_Type (T));
13082 if Constraint_OK then
13083 Set_First_Index (Def_Id, First (Constraints (C)));
13084 else
13085 Set_First_Index (Def_Id, First_Index (T));
13086 end if;
13088 Set_Is_Constrained (Def_Id, True);
13089 Set_Is_Aliased (Def_Id, Is_Aliased (T));
13090 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
13092 Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
13093 Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
13095 -- A subtype does not inherit the Packed_Array_Impl_Type of is parent.
13096 -- We need to initialize the attribute because if Def_Id is previously
13097 -- analyzed through a limited_with clause, it will have the attributes
13098 -- of an incomplete type, one of which is an Elist that overlaps the
13099 -- Packed_Array_Impl_Type field.
13101 Set_Packed_Array_Impl_Type (Def_Id, Empty);
13103 -- Build a freeze node if parent still needs one. Also make sure that
13104 -- the Depends_On_Private status is set because the subtype will need
13105 -- reprocessing at the time the base type does, and also we must set a
13106 -- conditional delay.
13108 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
13109 Conditional_Delay (Def_Id, T);
13110 end Constrain_Array;
13112 ------------------------------
13113 -- Constrain_Component_Type --
13114 ------------------------------
13116 function Constrain_Component_Type
13117 (Comp : Entity_Id;
13118 Constrained_Typ : Entity_Id;
13119 Related_Node : Node_Id;
13120 Typ : Entity_Id;
13121 Constraints : Elist_Id) return Entity_Id
13123 Loc : constant Source_Ptr := Sloc (Constrained_Typ);
13124 Compon_Type : constant Entity_Id := Etype (Comp);
13126 function Build_Constrained_Array_Type
13127 (Old_Type : Entity_Id) return Entity_Id;
13128 -- If Old_Type is an array type, one of whose indexes is constrained
13129 -- by a discriminant, build an Itype whose constraint replaces the
13130 -- discriminant with its value in the constraint.
13132 function Build_Constrained_Discriminated_Type
13133 (Old_Type : Entity_Id) return Entity_Id;
13134 -- Ditto for record components
13136 function Build_Constrained_Access_Type
13137 (Old_Type : Entity_Id) return Entity_Id;
13138 -- Ditto for access types. Makes use of previous two functions, to
13139 -- constrain designated type.
13141 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id;
13142 -- T is an array or discriminated type, C is a list of constraints
13143 -- that apply to T. This routine builds the constrained subtype.
13145 function Is_Discriminant (Expr : Node_Id) return Boolean;
13146 -- Returns True if Expr is a discriminant
13148 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id;
13149 -- Find the value of discriminant Discrim in Constraint
13151 -----------------------------------
13152 -- Build_Constrained_Access_Type --
13153 -----------------------------------
13155 function Build_Constrained_Access_Type
13156 (Old_Type : Entity_Id) return Entity_Id
13158 Desig_Type : constant Entity_Id := Designated_Type (Old_Type);
13159 Itype : Entity_Id;
13160 Desig_Subtype : Entity_Id;
13161 Scop : Entity_Id;
13163 begin
13164 -- if the original access type was not embedded in the enclosing
13165 -- type definition, there is no need to produce a new access
13166 -- subtype. In fact every access type with an explicit constraint
13167 -- generates an itype whose scope is the enclosing record.
13169 if not Is_Type (Scope (Old_Type)) then
13170 return Old_Type;
13172 elsif Is_Array_Type (Desig_Type) then
13173 Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
13175 elsif Has_Discriminants (Desig_Type) then
13177 -- This may be an access type to an enclosing record type for
13178 -- which we are constructing the constrained components. Return
13179 -- the enclosing record subtype. This is not always correct,
13180 -- but avoids infinite recursion. ???
13182 Desig_Subtype := Any_Type;
13184 for J in reverse 0 .. Scope_Stack.Last loop
13185 Scop := Scope_Stack.Table (J).Entity;
13187 if Is_Type (Scop)
13188 and then Base_Type (Scop) = Base_Type (Desig_Type)
13189 then
13190 Desig_Subtype := Scop;
13191 end if;
13193 exit when not Is_Type (Scop);
13194 end loop;
13196 if Desig_Subtype = Any_Type then
13197 Desig_Subtype :=
13198 Build_Constrained_Discriminated_Type (Desig_Type);
13199 end if;
13201 else
13202 return Old_Type;
13203 end if;
13205 if Desig_Subtype /= Desig_Type then
13207 -- The Related_Node better be here or else we won't be able
13208 -- to attach new itypes to a node in the tree.
13210 pragma Assert (Present (Related_Node));
13212 Itype := Create_Itype (E_Access_Subtype, Related_Node);
13214 Set_Etype (Itype, Base_Type (Old_Type));
13215 Set_Size_Info (Itype, (Old_Type));
13216 Set_Directly_Designated_Type (Itype, Desig_Subtype);
13217 Set_Depends_On_Private (Itype, Has_Private_Component
13218 (Old_Type));
13219 Set_Is_Access_Constant (Itype, Is_Access_Constant
13220 (Old_Type));
13222 -- The new itype needs freezing when it depends on a not frozen
13223 -- type and the enclosing subtype needs freezing.
13225 if Has_Delayed_Freeze (Constrained_Typ)
13226 and then not Is_Frozen (Constrained_Typ)
13227 then
13228 Conditional_Delay (Itype, Base_Type (Old_Type));
13229 end if;
13231 return Itype;
13233 else
13234 return Old_Type;
13235 end if;
13236 end Build_Constrained_Access_Type;
13238 ----------------------------------
13239 -- Build_Constrained_Array_Type --
13240 ----------------------------------
13242 function Build_Constrained_Array_Type
13243 (Old_Type : Entity_Id) return Entity_Id
13245 Lo_Expr : Node_Id;
13246 Hi_Expr : Node_Id;
13247 Old_Index : Node_Id;
13248 Range_Node : Node_Id;
13249 Constr_List : List_Id;
13251 Need_To_Create_Itype : Boolean := False;
13253 begin
13254 Old_Index := First_Index (Old_Type);
13255 while Present (Old_Index) loop
13256 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
13258 if Is_Discriminant (Lo_Expr)
13259 or else
13260 Is_Discriminant (Hi_Expr)
13261 then
13262 Need_To_Create_Itype := True;
13263 end if;
13265 Next_Index (Old_Index);
13266 end loop;
13268 if Need_To_Create_Itype then
13269 Constr_List := New_List;
13271 Old_Index := First_Index (Old_Type);
13272 while Present (Old_Index) loop
13273 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
13275 if Is_Discriminant (Lo_Expr) then
13276 Lo_Expr := Get_Discr_Value (Lo_Expr);
13277 end if;
13279 if Is_Discriminant (Hi_Expr) then
13280 Hi_Expr := Get_Discr_Value (Hi_Expr);
13281 end if;
13283 Range_Node :=
13284 Make_Range
13285 (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
13287 Append (Range_Node, To => Constr_List);
13289 Next_Index (Old_Index);
13290 end loop;
13292 return Build_Subtype (Old_Type, Constr_List);
13294 else
13295 return Old_Type;
13296 end if;
13297 end Build_Constrained_Array_Type;
13299 ------------------------------------------
13300 -- Build_Constrained_Discriminated_Type --
13301 ------------------------------------------
13303 function Build_Constrained_Discriminated_Type
13304 (Old_Type : Entity_Id) return Entity_Id
13306 Expr : Node_Id;
13307 Constr_List : List_Id;
13308 Old_Constraint : Elmt_Id;
13310 Need_To_Create_Itype : Boolean := False;
13312 begin
13313 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
13314 while Present (Old_Constraint) loop
13315 Expr := Node (Old_Constraint);
13317 if Is_Discriminant (Expr) then
13318 Need_To_Create_Itype := True;
13319 end if;
13321 Next_Elmt (Old_Constraint);
13322 end loop;
13324 if Need_To_Create_Itype then
13325 Constr_List := New_List;
13327 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
13328 while Present (Old_Constraint) loop
13329 Expr := Node (Old_Constraint);
13331 if Is_Discriminant (Expr) then
13332 Expr := Get_Discr_Value (Expr);
13333 end if;
13335 Append (New_Copy_Tree (Expr), To => Constr_List);
13337 Next_Elmt (Old_Constraint);
13338 end loop;
13340 return Build_Subtype (Old_Type, Constr_List);
13342 else
13343 return Old_Type;
13344 end if;
13345 end Build_Constrained_Discriminated_Type;
13347 -------------------
13348 -- Build_Subtype --
13349 -------------------
13351 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id is
13352 Indic : Node_Id;
13353 Subtyp_Decl : Node_Id;
13354 Def_Id : Entity_Id;
13355 Btyp : Entity_Id := Base_Type (T);
13357 begin
13358 -- The Related_Node better be here or else we won't be able to
13359 -- attach new itypes to a node in the tree.
13361 pragma Assert (Present (Related_Node));
13363 -- If the view of the component's type is incomplete or private
13364 -- with unknown discriminants, then the constraint must be applied
13365 -- to the full type.
13367 if Has_Unknown_Discriminants (Btyp)
13368 and then Present (Underlying_Type (Btyp))
13369 then
13370 Btyp := Underlying_Type (Btyp);
13371 end if;
13373 Indic :=
13374 Make_Subtype_Indication (Loc,
13375 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
13376 Constraint => Make_Index_Or_Discriminant_Constraint (Loc, C));
13378 Def_Id := Create_Itype (Ekind (T), Related_Node);
13380 Subtyp_Decl :=
13381 Make_Subtype_Declaration (Loc,
13382 Defining_Identifier => Def_Id,
13383 Subtype_Indication => Indic);
13385 Set_Parent (Subtyp_Decl, Parent (Related_Node));
13387 -- Itypes must be analyzed with checks off (see package Itypes)
13389 Analyze (Subtyp_Decl, Suppress => All_Checks);
13391 return Def_Id;
13392 end Build_Subtype;
13394 ---------------------
13395 -- Get_Discr_Value --
13396 ---------------------
13398 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id is
13399 D : Entity_Id;
13400 E : Elmt_Id;
13402 begin
13403 -- The discriminant may be declared for the type, in which case we
13404 -- find it by iterating over the list of discriminants. If the
13405 -- discriminant is inherited from a parent type, it appears as the
13406 -- corresponding discriminant of the current type. This will be the
13407 -- case when constraining an inherited component whose constraint is
13408 -- given by a discriminant of the parent.
13410 D := First_Discriminant (Typ);
13411 E := First_Elmt (Constraints);
13413 while Present (D) loop
13414 if D = Entity (Discrim)
13415 or else D = CR_Discriminant (Entity (Discrim))
13416 or else Corresponding_Discriminant (D) = Entity (Discrim)
13417 then
13418 return Node (E);
13419 end if;
13421 Next_Discriminant (D);
13422 Next_Elmt (E);
13423 end loop;
13425 -- The Corresponding_Discriminant mechanism is incomplete, because
13426 -- the correspondence between new and old discriminants is not one
13427 -- to one: one new discriminant can constrain several old ones. In
13428 -- that case, scan sequentially the stored_constraint, the list of
13429 -- discriminants of the parents, and the constraints.
13431 -- Previous code checked for the present of the Stored_Constraint
13432 -- list for the derived type, but did not use it at all. Should it
13433 -- be present when the component is a discriminated task type?
13435 if Is_Derived_Type (Typ)
13436 and then Scope (Entity (Discrim)) = Etype (Typ)
13437 then
13438 D := First_Discriminant (Etype (Typ));
13439 E := First_Elmt (Constraints);
13440 while Present (D) loop
13441 if D = Entity (Discrim) then
13442 return Node (E);
13443 end if;
13445 Next_Discriminant (D);
13446 Next_Elmt (E);
13447 end loop;
13448 end if;
13450 -- Something is wrong if we did not find the value
13452 raise Program_Error;
13453 end Get_Discr_Value;
13455 ---------------------
13456 -- Is_Discriminant --
13457 ---------------------
13459 function Is_Discriminant (Expr : Node_Id) return Boolean is
13460 Discrim_Scope : Entity_Id;
13462 begin
13463 if Denotes_Discriminant (Expr) then
13464 Discrim_Scope := Scope (Entity (Expr));
13466 -- Either we have a reference to one of Typ's discriminants,
13468 pragma Assert (Discrim_Scope = Typ
13470 -- or to the discriminants of the parent type, in the case
13471 -- of a derivation of a tagged type with variants.
13473 or else Discrim_Scope = Etype (Typ)
13474 or else Full_View (Discrim_Scope) = Etype (Typ)
13476 -- or same as above for the case where the discriminants
13477 -- were declared in Typ's private view.
13479 or else (Is_Private_Type (Discrim_Scope)
13480 and then Chars (Discrim_Scope) = Chars (Typ))
13482 -- or else we are deriving from the full view and the
13483 -- discriminant is declared in the private entity.
13485 or else (Is_Private_Type (Typ)
13486 and then Chars (Discrim_Scope) = Chars (Typ))
13488 -- Or we are constrained the corresponding record of a
13489 -- synchronized type that completes a private declaration.
13491 or else (Is_Concurrent_Record_Type (Typ)
13492 and then
13493 Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
13495 -- or we have a class-wide type, in which case make sure the
13496 -- discriminant found belongs to the root type.
13498 or else (Is_Class_Wide_Type (Typ)
13499 and then Etype (Typ) = Discrim_Scope));
13501 return True;
13502 end if;
13504 -- In all other cases we have something wrong
13506 return False;
13507 end Is_Discriminant;
13509 -- Start of processing for Constrain_Component_Type
13511 begin
13512 if Nkind (Parent (Comp)) = N_Component_Declaration
13513 and then Comes_From_Source (Parent (Comp))
13514 and then Comes_From_Source
13515 (Subtype_Indication (Component_Definition (Parent (Comp))))
13516 and then
13517 Is_Entity_Name
13518 (Subtype_Indication (Component_Definition (Parent (Comp))))
13519 then
13520 return Compon_Type;
13522 elsif Is_Array_Type (Compon_Type) then
13523 return Build_Constrained_Array_Type (Compon_Type);
13525 elsif Has_Discriminants (Compon_Type) then
13526 return Build_Constrained_Discriminated_Type (Compon_Type);
13528 elsif Is_Access_Type (Compon_Type) then
13529 return Build_Constrained_Access_Type (Compon_Type);
13531 else
13532 return Compon_Type;
13533 end if;
13534 end Constrain_Component_Type;
13536 --------------------------
13537 -- Constrain_Concurrent --
13538 --------------------------
13540 -- For concurrent types, the associated record value type carries the same
13541 -- discriminants, so when we constrain a concurrent type, we must constrain
13542 -- the corresponding record type as well.
13544 procedure Constrain_Concurrent
13545 (Def_Id : in out Entity_Id;
13546 SI : Node_Id;
13547 Related_Nod : Node_Id;
13548 Related_Id : Entity_Id;
13549 Suffix : Character)
13551 -- Retrieve Base_Type to ensure getting to the concurrent type in the
13552 -- case of a private subtype (needed when only doing semantic analysis).
13554 T_Ent : Entity_Id := Base_Type (Entity (Subtype_Mark (SI)));
13555 T_Val : Entity_Id;
13557 begin
13558 if Is_Access_Type (T_Ent) then
13559 T_Ent := Designated_Type (T_Ent);
13560 end if;
13562 T_Val := Corresponding_Record_Type (T_Ent);
13564 if Present (T_Val) then
13566 if No (Def_Id) then
13567 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
13569 -- Elaborate itype now, as it may be used in a subsequent
13570 -- synchronized operation in another scope.
13572 if Nkind (Related_Nod) = N_Full_Type_Declaration then
13573 Build_Itype_Reference (Def_Id, Related_Nod);
13574 end if;
13575 end if;
13577 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
13578 Set_First_Private_Entity (Def_Id, First_Private_Entity (T_Ent));
13580 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
13581 Set_Corresponding_Record_Type (Def_Id,
13582 Constrain_Corresponding_Record (Def_Id, T_Val, Related_Nod));
13584 else
13585 -- If there is no associated record, expansion is disabled and this
13586 -- is a generic context. Create a subtype in any case, so that
13587 -- semantic analysis can proceed.
13589 if No (Def_Id) then
13590 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
13591 end if;
13593 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
13594 end if;
13595 end Constrain_Concurrent;
13597 ------------------------------------
13598 -- Constrain_Corresponding_Record --
13599 ------------------------------------
13601 function Constrain_Corresponding_Record
13602 (Prot_Subt : Entity_Id;
13603 Corr_Rec : Entity_Id;
13604 Related_Nod : Node_Id) return Entity_Id
13606 T_Sub : constant Entity_Id :=
13607 Create_Itype (E_Record_Subtype, Related_Nod, Corr_Rec, 'C');
13609 begin
13610 Set_Etype (T_Sub, Corr_Rec);
13611 Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
13612 Set_Is_Constrained (T_Sub, True);
13613 Set_First_Entity (T_Sub, First_Entity (Corr_Rec));
13614 Set_Last_Entity (T_Sub, Last_Entity (Corr_Rec));
13616 if Has_Discriminants (Prot_Subt) then -- False only if errors.
13617 Set_Discriminant_Constraint
13618 (T_Sub, Discriminant_Constraint (Prot_Subt));
13619 Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
13620 Create_Constrained_Components
13621 (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
13622 end if;
13624 Set_Depends_On_Private (T_Sub, Has_Private_Component (T_Sub));
13626 if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
13627 Conditional_Delay (T_Sub, Corr_Rec);
13629 else
13630 -- This is a component subtype: it will be frozen in the context of
13631 -- the enclosing record's init_proc, so that discriminant references
13632 -- are resolved to discriminals. (Note: we used to skip freezing
13633 -- altogether in that case, which caused errors downstream for
13634 -- components of a bit packed array type).
13636 Set_Has_Delayed_Freeze (T_Sub);
13637 end if;
13639 return T_Sub;
13640 end Constrain_Corresponding_Record;
13642 -----------------------
13643 -- Constrain_Decimal --
13644 -----------------------
13646 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id) is
13647 T : constant Entity_Id := Entity (Subtype_Mark (S));
13648 C : constant Node_Id := Constraint (S);
13649 Loc : constant Source_Ptr := Sloc (C);
13650 Range_Expr : Node_Id;
13651 Digits_Expr : Node_Id;
13652 Digits_Val : Uint;
13653 Bound_Val : Ureal;
13655 begin
13656 Set_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
13658 if Nkind (C) = N_Range_Constraint then
13659 Range_Expr := Range_Expression (C);
13660 Digits_Val := Digits_Value (T);
13662 else
13663 pragma Assert (Nkind (C) = N_Digits_Constraint);
13665 Check_SPARK_05_Restriction ("digits constraint is not allowed", S);
13667 Digits_Expr := Digits_Expression (C);
13668 Analyze_And_Resolve (Digits_Expr, Any_Integer);
13670 Check_Digits_Expression (Digits_Expr);
13671 Digits_Val := Expr_Value (Digits_Expr);
13673 if Digits_Val > Digits_Value (T) then
13674 Error_Msg_N
13675 ("digits expression is incompatible with subtype", C);
13676 Digits_Val := Digits_Value (T);
13677 end if;
13679 if Present (Range_Constraint (C)) then
13680 Range_Expr := Range_Expression (Range_Constraint (C));
13681 else
13682 Range_Expr := Empty;
13683 end if;
13684 end if;
13686 Set_Etype (Def_Id, Base_Type (T));
13687 Set_Size_Info (Def_Id, (T));
13688 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13689 Set_Delta_Value (Def_Id, Delta_Value (T));
13690 Set_Scale_Value (Def_Id, Scale_Value (T));
13691 Set_Small_Value (Def_Id, Small_Value (T));
13692 Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
13693 Set_Digits_Value (Def_Id, Digits_Val);
13695 -- Manufacture range from given digits value if no range present
13697 if No (Range_Expr) then
13698 Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
13699 Range_Expr :=
13700 Make_Range (Loc,
13701 Low_Bound =>
13702 Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
13703 High_Bound =>
13704 Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
13705 end if;
13707 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
13708 Set_Discrete_RM_Size (Def_Id);
13710 -- Unconditionally delay the freeze, since we cannot set size
13711 -- information in all cases correctly until the freeze point.
13713 Set_Has_Delayed_Freeze (Def_Id);
13714 end Constrain_Decimal;
13716 ----------------------------------
13717 -- Constrain_Discriminated_Type --
13718 ----------------------------------
13720 procedure Constrain_Discriminated_Type
13721 (Def_Id : Entity_Id;
13722 S : Node_Id;
13723 Related_Nod : Node_Id;
13724 For_Access : Boolean := False)
13726 E : Entity_Id := Entity (Subtype_Mark (S));
13727 T : Entity_Id;
13729 procedure Fixup_Bad_Constraint;
13730 -- Called after finding a bad constraint, and after having posted an
13731 -- appropriate error message. The goal is to leave type Def_Id in as
13732 -- reasonable state as possible.
13734 --------------------------
13735 -- Fixup_Bad_Constraint --
13736 --------------------------
13738 procedure Fixup_Bad_Constraint is
13739 begin
13740 -- Set a reasonable Ekind for the entity, including incomplete types.
13742 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
13744 -- Set Etype to the known type, to reduce chances of cascaded errors
13746 Set_Etype (Def_Id, E);
13747 Set_Error_Posted (Def_Id);
13748 end Fixup_Bad_Constraint;
13750 -- Local variables
13752 C : Node_Id;
13753 Constr : Elist_Id := New_Elmt_List;
13755 -- Start of processing for Constrain_Discriminated_Type
13757 begin
13758 C := Constraint (S);
13760 -- A discriminant constraint is only allowed in a subtype indication,
13761 -- after a subtype mark. This subtype mark must denote either a type
13762 -- with discriminants, or an access type whose designated type is a
13763 -- type with discriminants. A discriminant constraint specifies the
13764 -- values of these discriminants (RM 3.7.2(5)).
13766 T := Base_Type (Entity (Subtype_Mark (S)));
13768 if Is_Access_Type (T) then
13769 T := Designated_Type (T);
13770 end if;
13772 -- In an instance it may be necessary to retrieve the full view of a
13773 -- type with unknown discriminants, or a full view with defaulted
13774 -- discriminants. In other contexts the constraint is illegal.
13776 if In_Instance
13777 and then Is_Private_Type (T)
13778 and then Present (Full_View (T))
13779 and then
13780 (Has_Unknown_Discriminants (T)
13781 or else
13782 (not Has_Discriminants (T)
13783 and then Has_Discriminants (Full_View (T))
13784 and then Present (Discriminant_Default_Value
13785 (First_Discriminant (Full_View (T))))))
13786 then
13787 T := Full_View (T);
13788 E := Full_View (E);
13789 end if;
13791 -- Ada 2005 (AI-412): Constrained incomplete subtypes are illegal. Avoid
13792 -- generating an error for access-to-incomplete subtypes.
13794 if Ada_Version >= Ada_2005
13795 and then Ekind (T) = E_Incomplete_Type
13796 and then Nkind (Parent (S)) = N_Subtype_Declaration
13797 and then not Is_Itype (Def_Id)
13798 then
13799 -- A little sanity check: emit an error message if the type has
13800 -- discriminants to begin with. Type T may be a regular incomplete
13801 -- type or imported via a limited with clause.
13803 if Has_Discriminants (T)
13804 or else (From_Limited_With (T)
13805 and then Present (Non_Limited_View (T))
13806 and then Nkind (Parent (Non_Limited_View (T))) =
13807 N_Full_Type_Declaration
13808 and then Present (Discriminant_Specifications
13809 (Parent (Non_Limited_View (T)))))
13810 then
13811 Error_Msg_N
13812 ("(Ada 2005) incomplete subtype may not be constrained", C);
13813 else
13814 Error_Msg_N ("invalid constraint: type has no discriminant", C);
13815 end if;
13817 Fixup_Bad_Constraint;
13818 return;
13820 -- Check that the type has visible discriminants. The type may be
13821 -- a private type with unknown discriminants whose full view has
13822 -- discriminants which are invisible.
13824 elsif not Has_Discriminants (T)
13825 or else
13826 (Has_Unknown_Discriminants (T)
13827 and then Is_Private_Type (T))
13828 then
13829 Error_Msg_N ("invalid constraint: type has no discriminant", C);
13830 Fixup_Bad_Constraint;
13831 return;
13833 elsif Is_Constrained (E)
13834 or else (Ekind (E) = E_Class_Wide_Subtype
13835 and then Present (Discriminant_Constraint (E)))
13836 then
13837 Error_Msg_N ("type is already constrained", Subtype_Mark (S));
13838 Fixup_Bad_Constraint;
13839 return;
13840 end if;
13842 -- T may be an unconstrained subtype (e.g. a generic actual). Constraint
13843 -- applies to the base type.
13845 T := Base_Type (T);
13847 Constr := Build_Discriminant_Constraints (T, S);
13849 -- If the list returned was empty we had an error in building the
13850 -- discriminant constraint. We have also already signalled an error
13851 -- in the incomplete type case
13853 if Is_Empty_Elmt_List (Constr) then
13854 Fixup_Bad_Constraint;
13855 return;
13856 end if;
13858 Build_Discriminated_Subtype (T, Def_Id, Constr, Related_Nod, For_Access);
13859 end Constrain_Discriminated_Type;
13861 ---------------------------
13862 -- Constrain_Enumeration --
13863 ---------------------------
13865 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id) is
13866 T : constant Entity_Id := Entity (Subtype_Mark (S));
13867 C : constant Node_Id := Constraint (S);
13869 begin
13870 Set_Ekind (Def_Id, E_Enumeration_Subtype);
13872 Set_First_Literal (Def_Id, First_Literal (Base_Type (T)));
13874 Set_Etype (Def_Id, Base_Type (T));
13875 Set_Size_Info (Def_Id, (T));
13876 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13877 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
13879 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13881 Set_Discrete_RM_Size (Def_Id);
13882 end Constrain_Enumeration;
13884 ----------------------
13885 -- Constrain_Float --
13886 ----------------------
13888 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id) is
13889 T : constant Entity_Id := Entity (Subtype_Mark (S));
13890 C : Node_Id;
13891 D : Node_Id;
13892 Rais : Node_Id;
13894 begin
13895 Set_Ekind (Def_Id, E_Floating_Point_Subtype);
13897 Set_Etype (Def_Id, Base_Type (T));
13898 Set_Size_Info (Def_Id, (T));
13899 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13901 -- Process the constraint
13903 C := Constraint (S);
13905 -- Digits constraint present
13907 if Nkind (C) = N_Digits_Constraint then
13909 Check_SPARK_05_Restriction ("digits constraint is not allowed", S);
13910 Check_Restriction (No_Obsolescent_Features, C);
13912 if Warn_On_Obsolescent_Feature then
13913 Error_Msg_N
13914 ("subtype digits constraint is an " &
13915 "obsolescent feature (RM J.3(8))?j?", C);
13916 end if;
13918 D := Digits_Expression (C);
13919 Analyze_And_Resolve (D, Any_Integer);
13920 Check_Digits_Expression (D);
13921 Set_Digits_Value (Def_Id, Expr_Value (D));
13923 -- Check that digits value is in range. Obviously we can do this
13924 -- at compile time, but it is strictly a runtime check, and of
13925 -- course there is an ACVC test that checks this.
13927 if Digits_Value (Def_Id) > Digits_Value (T) then
13928 Error_Msg_Uint_1 := Digits_Value (T);
13929 Error_Msg_N ("??digits value is too large, maximum is ^", D);
13930 Rais :=
13931 Make_Raise_Constraint_Error (Sloc (D),
13932 Reason => CE_Range_Check_Failed);
13933 Insert_Action (Declaration_Node (Def_Id), Rais);
13934 end if;
13936 C := Range_Constraint (C);
13938 -- No digits constraint present
13940 else
13941 Set_Digits_Value (Def_Id, Digits_Value (T));
13942 end if;
13944 -- Range constraint present
13946 if Nkind (C) = N_Range_Constraint then
13947 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13949 -- No range constraint present
13951 else
13952 pragma Assert (No (C));
13953 Set_Scalar_Range (Def_Id, Scalar_Range (T));
13954 end if;
13956 Set_Is_Constrained (Def_Id);
13957 end Constrain_Float;
13959 ---------------------
13960 -- Constrain_Index --
13961 ---------------------
13963 procedure Constrain_Index
13964 (Index : Node_Id;
13965 S : Node_Id;
13966 Related_Nod : Node_Id;
13967 Related_Id : Entity_Id;
13968 Suffix : Character;
13969 Suffix_Index : Nat)
13971 Def_Id : Entity_Id;
13972 R : Node_Id := Empty;
13973 T : constant Entity_Id := Etype (Index);
13975 begin
13976 Def_Id :=
13977 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
13978 Set_Etype (Def_Id, Base_Type (T));
13980 if Nkind (S) = N_Range
13981 or else
13982 (Nkind (S) = N_Attribute_Reference
13983 and then Attribute_Name (S) = Name_Range)
13984 then
13985 -- A Range attribute will be transformed into N_Range by Resolve
13987 Analyze (S);
13988 Set_Etype (S, T);
13989 R := S;
13991 Process_Range_Expr_In_Decl (R, T);
13993 if not Error_Posted (S)
13994 and then
13995 (Nkind (S) /= N_Range
13996 or else not Covers (T, (Etype (Low_Bound (S))))
13997 or else not Covers (T, (Etype (High_Bound (S)))))
13998 then
13999 if Base_Type (T) /= Any_Type
14000 and then Etype (Low_Bound (S)) /= Any_Type
14001 and then Etype (High_Bound (S)) /= Any_Type
14002 then
14003 Error_Msg_N ("range expected", S);
14004 end if;
14005 end if;
14007 elsif Nkind (S) = N_Subtype_Indication then
14009 -- The parser has verified that this is a discrete indication
14011 Resolve_Discrete_Subtype_Indication (S, T);
14012 Bad_Predicated_Subtype_Use
14013 ("subtype& has predicate, not allowed in index constraint",
14014 S, Entity (Subtype_Mark (S)));
14016 R := Range_Expression (Constraint (S));
14018 -- Capture values of bounds and generate temporaries for them if
14019 -- needed, since checks may cause duplication of the expressions
14020 -- which must not be reevaluated.
14022 -- The forced evaluation removes side effects from expressions, which
14023 -- should occur also in GNATprove mode. Otherwise, we end up with
14024 -- unexpected insertions of actions at places where this is not
14025 -- supposed to occur, e.g. on default parameters of a call.
14027 if Expander_Active or GNATprove_Mode then
14028 Force_Evaluation
14029 (Low_Bound (R), Related_Id => Def_Id, Is_Low_Bound => True);
14030 Force_Evaluation
14031 (High_Bound (R), Related_Id => Def_Id, Is_High_Bound => True);
14032 end if;
14034 elsif Nkind (S) = N_Discriminant_Association then
14036 -- Syntactically valid in subtype indication
14038 Error_Msg_N ("invalid index constraint", S);
14039 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
14040 return;
14042 -- Subtype_Mark case, no anonymous subtypes to construct
14044 else
14045 Analyze (S);
14047 if Is_Entity_Name (S) then
14048 if not Is_Type (Entity (S)) then
14049 Error_Msg_N ("expect subtype mark for index constraint", S);
14051 elsif Base_Type (Entity (S)) /= Base_Type (T) then
14052 Wrong_Type (S, Base_Type (T));
14054 -- Check error of subtype with predicate in index constraint
14056 else
14057 Bad_Predicated_Subtype_Use
14058 ("subtype& has predicate, not allowed in index constraint",
14059 S, Entity (S));
14060 end if;
14062 return;
14064 else
14065 Error_Msg_N ("invalid index constraint", S);
14066 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
14067 return;
14068 end if;
14069 end if;
14071 -- Complete construction of the Itype
14073 if Is_Modular_Integer_Type (T) then
14074 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
14076 elsif Is_Integer_Type (T) then
14077 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
14079 else
14080 Set_Ekind (Def_Id, E_Enumeration_Subtype);
14081 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
14082 Set_First_Literal (Def_Id, First_Literal (T));
14083 end if;
14085 Set_Size_Info (Def_Id, (T));
14086 Set_RM_Size (Def_Id, RM_Size (T));
14087 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14089 Set_Scalar_Range (Def_Id, R);
14091 Set_Etype (S, Def_Id);
14092 Set_Discrete_RM_Size (Def_Id);
14093 end Constrain_Index;
14095 -----------------------
14096 -- Constrain_Integer --
14097 -----------------------
14099 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id) is
14100 T : constant Entity_Id := Entity (Subtype_Mark (S));
14101 C : constant Node_Id := Constraint (S);
14103 begin
14104 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14106 if Is_Modular_Integer_Type (T) then
14107 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
14108 else
14109 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
14110 end if;
14112 Set_Etype (Def_Id, Base_Type (T));
14113 Set_Size_Info (Def_Id, (T));
14114 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14115 Set_Discrete_RM_Size (Def_Id);
14116 end Constrain_Integer;
14118 ------------------------------
14119 -- Constrain_Ordinary_Fixed --
14120 ------------------------------
14122 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id) is
14123 T : constant Entity_Id := Entity (Subtype_Mark (S));
14124 C : Node_Id;
14125 D : Node_Id;
14126 Rais : Node_Id;
14128 begin
14129 Set_Ekind (Def_Id, E_Ordinary_Fixed_Point_Subtype);
14130 Set_Etype (Def_Id, Base_Type (T));
14131 Set_Size_Info (Def_Id, (T));
14132 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14133 Set_Small_Value (Def_Id, Small_Value (T));
14135 -- Process the constraint
14137 C := Constraint (S);
14139 -- Delta constraint present
14141 if Nkind (C) = N_Delta_Constraint then
14143 Check_SPARK_05_Restriction ("delta constraint is not allowed", S);
14144 Check_Restriction (No_Obsolescent_Features, C);
14146 if Warn_On_Obsolescent_Feature then
14147 Error_Msg_S
14148 ("subtype delta constraint is an " &
14149 "obsolescent feature (RM J.3(7))?j?");
14150 end if;
14152 D := Delta_Expression (C);
14153 Analyze_And_Resolve (D, Any_Real);
14154 Check_Delta_Expression (D);
14155 Set_Delta_Value (Def_Id, Expr_Value_R (D));
14157 -- Check that delta value is in range. Obviously we can do this
14158 -- at compile time, but it is strictly a runtime check, and of
14159 -- course there is an ACVC test that checks this.
14161 if Delta_Value (Def_Id) < Delta_Value (T) then
14162 Error_Msg_N ("??delta value is too small", D);
14163 Rais :=
14164 Make_Raise_Constraint_Error (Sloc (D),
14165 Reason => CE_Range_Check_Failed);
14166 Insert_Action (Declaration_Node (Def_Id), Rais);
14167 end if;
14169 C := Range_Constraint (C);
14171 -- No delta constraint present
14173 else
14174 Set_Delta_Value (Def_Id, Delta_Value (T));
14175 end if;
14177 -- Range constraint present
14179 if Nkind (C) = N_Range_Constraint then
14180 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14182 -- No range constraint present
14184 else
14185 pragma Assert (No (C));
14186 Set_Scalar_Range (Def_Id, Scalar_Range (T));
14187 end if;
14189 Set_Discrete_RM_Size (Def_Id);
14191 -- Unconditionally delay the freeze, since we cannot set size
14192 -- information in all cases correctly until the freeze point.
14194 Set_Has_Delayed_Freeze (Def_Id);
14195 end Constrain_Ordinary_Fixed;
14197 -----------------------
14198 -- Contain_Interface --
14199 -----------------------
14201 function Contain_Interface
14202 (Iface : Entity_Id;
14203 Ifaces : Elist_Id) return Boolean
14205 Iface_Elmt : Elmt_Id;
14207 begin
14208 if Present (Ifaces) then
14209 Iface_Elmt := First_Elmt (Ifaces);
14210 while Present (Iface_Elmt) loop
14211 if Node (Iface_Elmt) = Iface then
14212 return True;
14213 end if;
14215 Next_Elmt (Iface_Elmt);
14216 end loop;
14217 end if;
14219 return False;
14220 end Contain_Interface;
14222 ---------------------------
14223 -- Convert_Scalar_Bounds --
14224 ---------------------------
14226 procedure Convert_Scalar_Bounds
14227 (N : Node_Id;
14228 Parent_Type : Entity_Id;
14229 Derived_Type : Entity_Id;
14230 Loc : Source_Ptr)
14232 Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
14234 Lo : Node_Id;
14235 Hi : Node_Id;
14236 Rng : Node_Id;
14238 begin
14239 -- Defend against previous errors
14241 if No (Scalar_Range (Derived_Type)) then
14242 Check_Error_Detected;
14243 return;
14244 end if;
14246 Lo := Build_Scalar_Bound
14247 (Type_Low_Bound (Derived_Type),
14248 Parent_Type, Implicit_Base);
14250 Hi := Build_Scalar_Bound
14251 (Type_High_Bound (Derived_Type),
14252 Parent_Type, Implicit_Base);
14254 Rng :=
14255 Make_Range (Loc,
14256 Low_Bound => Lo,
14257 High_Bound => Hi);
14259 Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
14261 Set_Parent (Rng, N);
14262 Set_Scalar_Range (Derived_Type, Rng);
14264 -- Analyze the bounds
14266 Analyze_And_Resolve (Lo, Implicit_Base);
14267 Analyze_And_Resolve (Hi, Implicit_Base);
14269 -- Analyze the range itself, except that we do not analyze it if
14270 -- the bounds are real literals, and we have a fixed-point type.
14271 -- The reason for this is that we delay setting the bounds in this
14272 -- case till we know the final Small and Size values (see circuit
14273 -- in Freeze.Freeze_Fixed_Point_Type for further details).
14275 if Is_Fixed_Point_Type (Parent_Type)
14276 and then Nkind (Lo) = N_Real_Literal
14277 and then Nkind (Hi) = N_Real_Literal
14278 then
14279 return;
14281 -- Here we do the analysis of the range
14283 -- Note: we do this manually, since if we do a normal Analyze and
14284 -- Resolve call, there are problems with the conversions used for
14285 -- the derived type range.
14287 else
14288 Set_Etype (Rng, Implicit_Base);
14289 Set_Analyzed (Rng, True);
14290 end if;
14291 end Convert_Scalar_Bounds;
14293 -------------------
14294 -- Copy_And_Swap --
14295 -------------------
14297 procedure Copy_And_Swap (Priv, Full : Entity_Id) is
14298 begin
14299 -- Initialize new full declaration entity by copying the pertinent
14300 -- fields of the corresponding private declaration entity.
14302 -- We temporarily set Ekind to a value appropriate for a type to
14303 -- avoid assert failures in Einfo from checking for setting type
14304 -- attributes on something that is not a type. Ekind (Priv) is an
14305 -- appropriate choice, since it allowed the attributes to be set
14306 -- in the first place. This Ekind value will be modified later.
14308 Set_Ekind (Full, Ekind (Priv));
14310 -- Also set Etype temporarily to Any_Type, again, in the absence
14311 -- of errors, it will be properly reset, and if there are errors,
14312 -- then we want a value of Any_Type to remain.
14314 Set_Etype (Full, Any_Type);
14316 -- Now start copying attributes
14318 Set_Has_Discriminants (Full, Has_Discriminants (Priv));
14320 if Has_Discriminants (Full) then
14321 Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
14322 Set_Stored_Constraint (Full, Stored_Constraint (Priv));
14323 end if;
14325 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
14326 Set_Homonym (Full, Homonym (Priv));
14327 Set_Is_Immediately_Visible (Full, Is_Immediately_Visible (Priv));
14328 Set_Is_Public (Full, Is_Public (Priv));
14329 Set_Is_Pure (Full, Is_Pure (Priv));
14330 Set_Is_Tagged_Type (Full, Is_Tagged_Type (Priv));
14331 Set_Has_Pragma_Unmodified (Full, Has_Pragma_Unmodified (Priv));
14332 Set_Has_Pragma_Unreferenced (Full, Has_Pragma_Unreferenced (Priv));
14333 Set_Has_Pragma_Unreferenced_Objects
14334 (Full, Has_Pragma_Unreferenced_Objects
14335 (Priv));
14337 Conditional_Delay (Full, Priv);
14339 if Is_Tagged_Type (Full) then
14340 Set_Direct_Primitive_Operations
14341 (Full, Direct_Primitive_Operations (Priv));
14342 Set_No_Tagged_Streams_Pragma
14343 (Full, No_Tagged_Streams_Pragma (Priv));
14345 if Is_Base_Type (Priv) then
14346 Set_Class_Wide_Type (Full, Class_Wide_Type (Priv));
14347 end if;
14348 end if;
14350 Set_Is_Volatile (Full, Is_Volatile (Priv));
14351 Set_Treat_As_Volatile (Full, Treat_As_Volatile (Priv));
14352 Set_Scope (Full, Scope (Priv));
14353 Set_Next_Entity (Full, Next_Entity (Priv));
14354 Set_First_Entity (Full, First_Entity (Priv));
14355 Set_Last_Entity (Full, Last_Entity (Priv));
14357 -- If access types have been recorded for later handling, keep them in
14358 -- the full view so that they get handled when the full view freeze
14359 -- node is expanded.
14361 if Present (Freeze_Node (Priv))
14362 and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
14363 then
14364 Ensure_Freeze_Node (Full);
14365 Set_Access_Types_To_Process
14366 (Freeze_Node (Full),
14367 Access_Types_To_Process (Freeze_Node (Priv)));
14368 end if;
14370 -- Swap the two entities. Now Private is the full type entity and Full
14371 -- is the private one. They will be swapped back at the end of the
14372 -- private part. This swapping ensures that the entity that is visible
14373 -- in the private part is the full declaration.
14375 Exchange_Entities (Priv, Full);
14376 Append_Entity (Full, Scope (Full));
14377 end Copy_And_Swap;
14379 -------------------------------------
14380 -- Copy_Array_Base_Type_Attributes --
14381 -------------------------------------
14383 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
14384 begin
14385 Set_Component_Alignment (T1, Component_Alignment (T2));
14386 Set_Component_Type (T1, Component_Type (T2));
14387 Set_Component_Size (T1, Component_Size (T2));
14388 Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
14389 Set_Has_Non_Standard_Rep (T1, Has_Non_Standard_Rep (T2));
14390 Propagate_Concurrent_Flags (T1, T2);
14391 Set_Is_Packed (T1, Is_Packed (T2));
14392 Set_Has_Aliased_Components (T1, Has_Aliased_Components (T2));
14393 Set_Has_Atomic_Components (T1, Has_Atomic_Components (T2));
14394 Set_Has_Volatile_Components (T1, Has_Volatile_Components (T2));
14395 end Copy_Array_Base_Type_Attributes;
14397 -----------------------------------
14398 -- Copy_Array_Subtype_Attributes --
14399 -----------------------------------
14401 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
14402 begin
14403 Set_Size_Info (T1, T2);
14405 Set_First_Index (T1, First_Index (T2));
14406 Set_Is_Aliased (T1, Is_Aliased (T2));
14407 Set_Is_Volatile (T1, Is_Volatile (T2));
14408 Set_Treat_As_Volatile (T1, Treat_As_Volatile (T2));
14409 Set_Is_Constrained (T1, Is_Constrained (T2));
14410 Set_Depends_On_Private (T1, Has_Private_Component (T2));
14411 Inherit_Rep_Item_Chain (T1, T2);
14412 Set_Convention (T1, Convention (T2));
14413 Set_Is_Limited_Composite (T1, Is_Limited_Composite (T2));
14414 Set_Is_Private_Composite (T1, Is_Private_Composite (T2));
14415 Set_Packed_Array_Impl_Type (T1, Packed_Array_Impl_Type (T2));
14416 end Copy_Array_Subtype_Attributes;
14418 -----------------------------------
14419 -- Create_Constrained_Components --
14420 -----------------------------------
14422 procedure Create_Constrained_Components
14423 (Subt : Entity_Id;
14424 Decl_Node : Node_Id;
14425 Typ : Entity_Id;
14426 Constraints : Elist_Id)
14428 Loc : constant Source_Ptr := Sloc (Subt);
14429 Comp_List : constant Elist_Id := New_Elmt_List;
14430 Parent_Type : constant Entity_Id := Etype (Typ);
14431 Assoc_List : constant List_Id := New_List;
14432 Discr_Val : Elmt_Id;
14433 Errors : Boolean;
14434 New_C : Entity_Id;
14435 Old_C : Entity_Id;
14436 Is_Static : Boolean := True;
14438 procedure Collect_Fixed_Components (Typ : Entity_Id);
14439 -- Collect parent type components that do not appear in a variant part
14441 procedure Create_All_Components;
14442 -- Iterate over Comp_List to create the components of the subtype
14444 function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
14445 -- Creates a new component from Old_Compon, copying all the fields from
14446 -- it, including its Etype, inserts the new component in the Subt entity
14447 -- chain and returns the new component.
14449 function Is_Variant_Record (T : Entity_Id) return Boolean;
14450 -- If true, and discriminants are static, collect only components from
14451 -- variants selected by discriminant values.
14453 ------------------------------
14454 -- Collect_Fixed_Components --
14455 ------------------------------
14457 procedure Collect_Fixed_Components (Typ : Entity_Id) is
14458 begin
14459 -- Build association list for discriminants, and find components of the
14460 -- variant part selected by the values of the discriminants.
14462 Old_C := First_Discriminant (Typ);
14463 Discr_Val := First_Elmt (Constraints);
14464 while Present (Old_C) loop
14465 Append_To (Assoc_List,
14466 Make_Component_Association (Loc,
14467 Choices => New_List (New_Occurrence_Of (Old_C, Loc)),
14468 Expression => New_Copy (Node (Discr_Val))));
14470 Next_Elmt (Discr_Val);
14471 Next_Discriminant (Old_C);
14472 end loop;
14474 -- The tag and the possible parent component are unconditionally in
14475 -- the subtype.
14477 if Is_Tagged_Type (Typ) or else Has_Controlled_Component (Typ) then
14478 Old_C := First_Component (Typ);
14479 while Present (Old_C) loop
14480 if Nam_In (Chars (Old_C), Name_uTag, Name_uParent) then
14481 Append_Elmt (Old_C, Comp_List);
14482 end if;
14484 Next_Component (Old_C);
14485 end loop;
14486 end if;
14487 end Collect_Fixed_Components;
14489 ---------------------------
14490 -- Create_All_Components --
14491 ---------------------------
14493 procedure Create_All_Components is
14494 Comp : Elmt_Id;
14496 begin
14497 Comp := First_Elmt (Comp_List);
14498 while Present (Comp) loop
14499 Old_C := Node (Comp);
14500 New_C := Create_Component (Old_C);
14502 Set_Etype
14503 (New_C,
14504 Constrain_Component_Type
14505 (Old_C, Subt, Decl_Node, Typ, Constraints));
14506 Set_Is_Public (New_C, Is_Public (Subt));
14508 Next_Elmt (Comp);
14509 end loop;
14510 end Create_All_Components;
14512 ----------------------
14513 -- Create_Component --
14514 ----------------------
14516 function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
14517 New_Compon : constant Entity_Id := New_Copy (Old_Compon);
14519 begin
14520 if Ekind (Old_Compon) = E_Discriminant
14521 and then Is_Completely_Hidden (Old_Compon)
14522 then
14523 -- This is a shadow discriminant created for a discriminant of
14524 -- the parent type, which needs to be present in the subtype.
14525 -- Give the shadow discriminant an internal name that cannot
14526 -- conflict with that of visible components.
14528 Set_Chars (New_Compon, New_Internal_Name ('C'));
14529 end if;
14531 -- Set the parent so we have a proper link for freezing etc. This is
14532 -- not a real parent pointer, since of course our parent does not own
14533 -- up to us and reference us, we are an illegitimate child of the
14534 -- original parent.
14536 Set_Parent (New_Compon, Parent (Old_Compon));
14538 -- We do not want this node marked as Comes_From_Source, since
14539 -- otherwise it would get first class status and a separate cross-
14540 -- reference line would be generated. Illegitimate children do not
14541 -- rate such recognition.
14543 Set_Comes_From_Source (New_Compon, False);
14545 -- But it is a real entity, and a birth certificate must be properly
14546 -- registered by entering it into the entity list.
14548 Enter_Name (New_Compon);
14550 return New_Compon;
14551 end Create_Component;
14553 -----------------------
14554 -- Is_Variant_Record --
14555 -----------------------
14557 function Is_Variant_Record (T : Entity_Id) return Boolean is
14558 begin
14559 return Nkind (Parent (T)) = N_Full_Type_Declaration
14560 and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
14561 and then Present (Component_List (Type_Definition (Parent (T))))
14562 and then
14563 Present
14564 (Variant_Part (Component_List (Type_Definition (Parent (T)))));
14565 end Is_Variant_Record;
14567 -- Start of processing for Create_Constrained_Components
14569 begin
14570 pragma Assert (Subt /= Base_Type (Subt));
14571 pragma Assert (Typ = Base_Type (Typ));
14573 Set_First_Entity (Subt, Empty);
14574 Set_Last_Entity (Subt, Empty);
14576 -- Check whether constraint is fully static, in which case we can
14577 -- optimize the list of components.
14579 Discr_Val := First_Elmt (Constraints);
14580 while Present (Discr_Val) loop
14581 if not Is_OK_Static_Expression (Node (Discr_Val)) then
14582 Is_Static := False;
14583 exit;
14584 end if;
14586 Next_Elmt (Discr_Val);
14587 end loop;
14589 Set_Has_Static_Discriminants (Subt, Is_Static);
14591 Push_Scope (Subt);
14593 -- Inherit the discriminants of the parent type
14595 Add_Discriminants : declare
14596 Num_Disc : Nat;
14597 Num_Gird : Nat;
14599 begin
14600 Num_Disc := 0;
14601 Old_C := First_Discriminant (Typ);
14603 while Present (Old_C) loop
14604 Num_Disc := Num_Disc + 1;
14605 New_C := Create_Component (Old_C);
14606 Set_Is_Public (New_C, Is_Public (Subt));
14607 Next_Discriminant (Old_C);
14608 end loop;
14610 -- For an untagged derived subtype, the number of discriminants may
14611 -- be smaller than the number of inherited discriminants, because
14612 -- several of them may be renamed by a single new discriminant or
14613 -- constrained. In this case, add the hidden discriminants back into
14614 -- the subtype, because they need to be present if the optimizer of
14615 -- the GCC 4.x back-end decides to break apart assignments between
14616 -- objects using the parent view into member-wise assignments.
14618 Num_Gird := 0;
14620 if Is_Derived_Type (Typ)
14621 and then not Is_Tagged_Type (Typ)
14622 then
14623 Old_C := First_Stored_Discriminant (Typ);
14625 while Present (Old_C) loop
14626 Num_Gird := Num_Gird + 1;
14627 Next_Stored_Discriminant (Old_C);
14628 end loop;
14629 end if;
14631 if Num_Gird > Num_Disc then
14633 -- Find out multiple uses of new discriminants, and add hidden
14634 -- components for the extra renamed discriminants. We recognize
14635 -- multiple uses through the Corresponding_Discriminant of a
14636 -- new discriminant: if it constrains several old discriminants,
14637 -- this field points to the last one in the parent type. The
14638 -- stored discriminants of the derived type have the same name
14639 -- as those of the parent.
14641 declare
14642 Constr : Elmt_Id;
14643 New_Discr : Entity_Id;
14644 Old_Discr : Entity_Id;
14646 begin
14647 Constr := First_Elmt (Stored_Constraint (Typ));
14648 Old_Discr := First_Stored_Discriminant (Typ);
14649 while Present (Constr) loop
14650 if Is_Entity_Name (Node (Constr))
14651 and then Ekind (Entity (Node (Constr))) = E_Discriminant
14652 then
14653 New_Discr := Entity (Node (Constr));
14655 if Chars (Corresponding_Discriminant (New_Discr)) /=
14656 Chars (Old_Discr)
14657 then
14658 -- The new discriminant has been used to rename a
14659 -- subsequent old discriminant. Introduce a shadow
14660 -- component for the current old discriminant.
14662 New_C := Create_Component (Old_Discr);
14663 Set_Original_Record_Component (New_C, Old_Discr);
14664 end if;
14666 else
14667 -- The constraint has eliminated the old discriminant.
14668 -- Introduce a shadow component.
14670 New_C := Create_Component (Old_Discr);
14671 Set_Original_Record_Component (New_C, Old_Discr);
14672 end if;
14674 Next_Elmt (Constr);
14675 Next_Stored_Discriminant (Old_Discr);
14676 end loop;
14677 end;
14678 end if;
14679 end Add_Discriminants;
14681 if Is_Static
14682 and then Is_Variant_Record (Typ)
14683 then
14684 Collect_Fixed_Components (Typ);
14686 Gather_Components (
14687 Typ,
14688 Component_List (Type_Definition (Parent (Typ))),
14689 Governed_By => Assoc_List,
14690 Into => Comp_List,
14691 Report_Errors => Errors);
14692 pragma Assert (not Errors
14693 or else Serious_Errors_Detected > 0);
14695 Create_All_Components;
14697 -- If the subtype declaration is created for a tagged type derivation
14698 -- with constraints, we retrieve the record definition of the parent
14699 -- type to select the components of the proper variant.
14701 elsif Is_Static
14702 and then Is_Tagged_Type (Typ)
14703 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
14704 and then
14705 Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
14706 and then Is_Variant_Record (Parent_Type)
14707 then
14708 Collect_Fixed_Components (Typ);
14710 Gather_Components
14711 (Typ,
14712 Component_List (Type_Definition (Parent (Parent_Type))),
14713 Governed_By => Assoc_List,
14714 Into => Comp_List,
14715 Report_Errors => Errors);
14717 -- Note: previously there was a check at this point that no errors
14718 -- were detected. As a consequence of AI05-220 there may be an error
14719 -- if an inherited discriminant that controls a variant has a non-
14720 -- static constraint.
14722 -- If the tagged derivation has a type extension, collect all the
14723 -- new components therein.
14725 if Present (Record_Extension_Part (Type_Definition (Parent (Typ))))
14726 then
14727 Old_C := First_Component (Typ);
14728 while Present (Old_C) loop
14729 if Original_Record_Component (Old_C) = Old_C
14730 and then Chars (Old_C) /= Name_uTag
14731 and then Chars (Old_C) /= Name_uParent
14732 then
14733 Append_Elmt (Old_C, Comp_List);
14734 end if;
14736 Next_Component (Old_C);
14737 end loop;
14738 end if;
14740 Create_All_Components;
14742 else
14743 -- If discriminants are not static, or if this is a multi-level type
14744 -- extension, we have to include all components of the parent type.
14746 Old_C := First_Component (Typ);
14747 while Present (Old_C) loop
14748 New_C := Create_Component (Old_C);
14750 Set_Etype
14751 (New_C,
14752 Constrain_Component_Type
14753 (Old_C, Subt, Decl_Node, Typ, Constraints));
14754 Set_Is_Public (New_C, Is_Public (Subt));
14756 Next_Component (Old_C);
14757 end loop;
14758 end if;
14760 End_Scope;
14761 end Create_Constrained_Components;
14763 ------------------------------------------
14764 -- Decimal_Fixed_Point_Type_Declaration --
14765 ------------------------------------------
14767 procedure Decimal_Fixed_Point_Type_Declaration
14768 (T : Entity_Id;
14769 Def : Node_Id)
14771 Loc : constant Source_Ptr := Sloc (Def);
14772 Digs_Expr : constant Node_Id := Digits_Expression (Def);
14773 Delta_Expr : constant Node_Id := Delta_Expression (Def);
14774 Implicit_Base : Entity_Id;
14775 Digs_Val : Uint;
14776 Delta_Val : Ureal;
14777 Scale_Val : Uint;
14778 Bound_Val : Ureal;
14780 begin
14781 Check_SPARK_05_Restriction
14782 ("decimal fixed point type is not allowed", Def);
14783 Check_Restriction (No_Fixed_Point, Def);
14785 -- Create implicit base type
14787 Implicit_Base :=
14788 Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
14789 Set_Etype (Implicit_Base, Implicit_Base);
14791 -- Analyze and process delta expression
14793 Analyze_And_Resolve (Delta_Expr, Universal_Real);
14795 Check_Delta_Expression (Delta_Expr);
14796 Delta_Val := Expr_Value_R (Delta_Expr);
14798 -- Check delta is power of 10, and determine scale value from it
14800 declare
14801 Val : Ureal;
14803 begin
14804 Scale_Val := Uint_0;
14805 Val := Delta_Val;
14807 if Val < Ureal_1 then
14808 while Val < Ureal_1 loop
14809 Val := Val * Ureal_10;
14810 Scale_Val := Scale_Val + 1;
14811 end loop;
14813 if Scale_Val > 18 then
14814 Error_Msg_N ("scale exceeds maximum value of 18", Def);
14815 Scale_Val := UI_From_Int (+18);
14816 end if;
14818 else
14819 while Val > Ureal_1 loop
14820 Val := Val / Ureal_10;
14821 Scale_Val := Scale_Val - 1;
14822 end loop;
14824 if Scale_Val < -18 then
14825 Error_Msg_N ("scale is less than minimum value of -18", Def);
14826 Scale_Val := UI_From_Int (-18);
14827 end if;
14828 end if;
14830 if Val /= Ureal_1 then
14831 Error_Msg_N ("delta expression must be a power of 10", Def);
14832 Delta_Val := Ureal_10 ** (-Scale_Val);
14833 end if;
14834 end;
14836 -- Set delta, scale and small (small = delta for decimal type)
14838 Set_Delta_Value (Implicit_Base, Delta_Val);
14839 Set_Scale_Value (Implicit_Base, Scale_Val);
14840 Set_Small_Value (Implicit_Base, Delta_Val);
14842 -- Analyze and process digits expression
14844 Analyze_And_Resolve (Digs_Expr, Any_Integer);
14845 Check_Digits_Expression (Digs_Expr);
14846 Digs_Val := Expr_Value (Digs_Expr);
14848 if Digs_Val > 18 then
14849 Digs_Val := UI_From_Int (+18);
14850 Error_Msg_N ("digits value out of range, maximum is 18", Digs_Expr);
14851 end if;
14853 Set_Digits_Value (Implicit_Base, Digs_Val);
14854 Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
14856 -- Set range of base type from digits value for now. This will be
14857 -- expanded to represent the true underlying base range by Freeze.
14859 Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
14861 -- Note: We leave size as zero for now, size will be set at freeze
14862 -- time. We have to do this for ordinary fixed-point, because the size
14863 -- depends on the specified small, and we might as well do the same for
14864 -- decimal fixed-point.
14866 pragma Assert (Esize (Implicit_Base) = Uint_0);
14868 -- If there are bounds given in the declaration use them as the
14869 -- bounds of the first named subtype.
14871 if Present (Real_Range_Specification (Def)) then
14872 declare
14873 RRS : constant Node_Id := Real_Range_Specification (Def);
14874 Low : constant Node_Id := Low_Bound (RRS);
14875 High : constant Node_Id := High_Bound (RRS);
14876 Low_Val : Ureal;
14877 High_Val : Ureal;
14879 begin
14880 Analyze_And_Resolve (Low, Any_Real);
14881 Analyze_And_Resolve (High, Any_Real);
14882 Check_Real_Bound (Low);
14883 Check_Real_Bound (High);
14884 Low_Val := Expr_Value_R (Low);
14885 High_Val := Expr_Value_R (High);
14887 if Low_Val < (-Bound_Val) then
14888 Error_Msg_N
14889 ("range low bound too small for digits value", Low);
14890 Low_Val := -Bound_Val;
14891 end if;
14893 if High_Val > Bound_Val then
14894 Error_Msg_N
14895 ("range high bound too large for digits value", High);
14896 High_Val := Bound_Val;
14897 end if;
14899 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
14900 end;
14902 -- If no explicit range, use range that corresponds to given
14903 -- digits value. This will end up as the final range for the
14904 -- first subtype.
14906 else
14907 Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
14908 end if;
14910 -- Complete entity for first subtype. The inheritance of the rep item
14911 -- chain ensures that SPARK-related pragmas are not clobbered when the
14912 -- decimal fixed point type acts as a full view of a private type.
14914 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
14915 Set_Etype (T, Implicit_Base);
14916 Set_Size_Info (T, Implicit_Base);
14917 Inherit_Rep_Item_Chain (T, Implicit_Base);
14918 Set_Digits_Value (T, Digs_Val);
14919 Set_Delta_Value (T, Delta_Val);
14920 Set_Small_Value (T, Delta_Val);
14921 Set_Scale_Value (T, Scale_Val);
14922 Set_Is_Constrained (T);
14923 end Decimal_Fixed_Point_Type_Declaration;
14925 -----------------------------------
14926 -- Derive_Progenitor_Subprograms --
14927 -----------------------------------
14929 procedure Derive_Progenitor_Subprograms
14930 (Parent_Type : Entity_Id;
14931 Tagged_Type : Entity_Id)
14933 E : Entity_Id;
14934 Elmt : Elmt_Id;
14935 Iface : Entity_Id;
14936 Iface_Elmt : Elmt_Id;
14937 Iface_Subp : Entity_Id;
14938 New_Subp : Entity_Id := Empty;
14939 Prim_Elmt : Elmt_Id;
14940 Subp : Entity_Id;
14941 Typ : Entity_Id;
14943 begin
14944 pragma Assert (Ada_Version >= Ada_2005
14945 and then Is_Record_Type (Tagged_Type)
14946 and then Is_Tagged_Type (Tagged_Type)
14947 and then Has_Interfaces (Tagged_Type));
14949 -- Step 1: Transfer to the full-view primitives associated with the
14950 -- partial-view that cover interface primitives. Conceptually this
14951 -- work should be done later by Process_Full_View; done here to
14952 -- simplify its implementation at later stages. It can be safely
14953 -- done here because interfaces must be visible in the partial and
14954 -- private view (RM 7.3(7.3/2)).
14956 -- Small optimization: This work is only required if the parent may
14957 -- have entities whose Alias attribute reference an interface primitive.
14958 -- Such a situation may occur if the parent is an abstract type and the
14959 -- primitive has not been yet overridden or if the parent is a generic
14960 -- formal type covering interfaces.
14962 -- If the tagged type is not abstract, it cannot have abstract
14963 -- primitives (the only entities in the list of primitives of
14964 -- non-abstract tagged types that can reference abstract primitives
14965 -- through its Alias attribute are the internal entities that have
14966 -- attribute Interface_Alias, and these entities are generated later
14967 -- by Add_Internal_Interface_Entities).
14969 if In_Private_Part (Current_Scope)
14970 and then (Is_Abstract_Type (Parent_Type)
14971 or else
14972 Is_Generic_Type (Parent_Type))
14973 then
14974 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
14975 while Present (Elmt) loop
14976 Subp := Node (Elmt);
14978 -- At this stage it is not possible to have entities in the list
14979 -- of primitives that have attribute Interface_Alias.
14981 pragma Assert (No (Interface_Alias (Subp)));
14983 Typ := Find_Dispatching_Type (Ultimate_Alias (Subp));
14985 if Is_Interface (Typ) then
14986 E := Find_Primitive_Covering_Interface
14987 (Tagged_Type => Tagged_Type,
14988 Iface_Prim => Subp);
14990 if Present (E)
14991 and then Find_Dispatching_Type (Ultimate_Alias (E)) /= Typ
14992 then
14993 Replace_Elmt (Elmt, E);
14994 Remove_Homonym (Subp);
14995 end if;
14996 end if;
14998 Next_Elmt (Elmt);
14999 end loop;
15000 end if;
15002 -- Step 2: Add primitives of progenitors that are not implemented by
15003 -- parents of Tagged_Type.
15005 if Present (Interfaces (Base_Type (Tagged_Type))) then
15006 Iface_Elmt := First_Elmt (Interfaces (Base_Type (Tagged_Type)));
15007 while Present (Iface_Elmt) loop
15008 Iface := Node (Iface_Elmt);
15010 Prim_Elmt := First_Elmt (Primitive_Operations (Iface));
15011 while Present (Prim_Elmt) loop
15012 Iface_Subp := Node (Prim_Elmt);
15014 -- Exclude derivation of predefined primitives except those
15015 -- that come from source, or are inherited from one that comes
15016 -- from source. Required to catch declarations of equality
15017 -- operators of interfaces. For example:
15019 -- type Iface is interface;
15020 -- function "=" (Left, Right : Iface) return Boolean;
15022 if not Is_Predefined_Dispatching_Operation (Iface_Subp)
15023 or else Comes_From_Source (Ultimate_Alias (Iface_Subp))
15024 then
15025 E := Find_Primitive_Covering_Interface
15026 (Tagged_Type => Tagged_Type,
15027 Iface_Prim => Iface_Subp);
15029 -- If not found we derive a new primitive leaving its alias
15030 -- attribute referencing the interface primitive.
15032 if No (E) then
15033 Derive_Subprogram
15034 (New_Subp, Iface_Subp, Tagged_Type, Iface);
15036 -- Ada 2012 (AI05-0197): If the covering primitive's name
15037 -- differs from the name of the interface primitive then it
15038 -- is a private primitive inherited from a parent type. In
15039 -- such case, given that Tagged_Type covers the interface,
15040 -- the inherited private primitive becomes visible. For such
15041 -- purpose we add a new entity that renames the inherited
15042 -- private primitive.
15044 elsif Chars (E) /= Chars (Iface_Subp) then
15045 pragma Assert (Has_Suffix (E, 'P'));
15046 Derive_Subprogram
15047 (New_Subp, Iface_Subp, Tagged_Type, Iface);
15048 Set_Alias (New_Subp, E);
15049 Set_Is_Abstract_Subprogram (New_Subp,
15050 Is_Abstract_Subprogram (E));
15052 -- Propagate to the full view interface entities associated
15053 -- with the partial view.
15055 elsif In_Private_Part (Current_Scope)
15056 and then Present (Alias (E))
15057 and then Alias (E) = Iface_Subp
15058 and then
15059 List_Containing (Parent (E)) /=
15060 Private_Declarations
15061 (Specification
15062 (Unit_Declaration_Node (Current_Scope)))
15063 then
15064 Append_Elmt (E, Primitive_Operations (Tagged_Type));
15065 end if;
15066 end if;
15068 Next_Elmt (Prim_Elmt);
15069 end loop;
15071 Next_Elmt (Iface_Elmt);
15072 end loop;
15073 end if;
15074 end Derive_Progenitor_Subprograms;
15076 -----------------------
15077 -- Derive_Subprogram --
15078 -----------------------
15080 procedure Derive_Subprogram
15081 (New_Subp : out Entity_Id;
15082 Parent_Subp : Entity_Id;
15083 Derived_Type : Entity_Id;
15084 Parent_Type : Entity_Id;
15085 Actual_Subp : Entity_Id := Empty)
15087 Formal : Entity_Id;
15088 -- Formal parameter of parent primitive operation
15090 Formal_Of_Actual : Entity_Id;
15091 -- Formal parameter of actual operation, when the derivation is to
15092 -- create a renaming for a primitive operation of an actual in an
15093 -- instantiation.
15095 New_Formal : Entity_Id;
15096 -- Formal of inherited operation
15098 Visible_Subp : Entity_Id := Parent_Subp;
15100 function Is_Private_Overriding return Boolean;
15101 -- If Subp is a private overriding of a visible operation, the inherited
15102 -- operation derives from the overridden op (even though its body is the
15103 -- overriding one) and the inherited operation is visible now. See
15104 -- sem_disp to see the full details of the handling of the overridden
15105 -- subprogram, which is removed from the list of primitive operations of
15106 -- the type. The overridden subprogram is saved locally in Visible_Subp,
15107 -- and used to diagnose abstract operations that need overriding in the
15108 -- derived type.
15110 procedure Replace_Type (Id, New_Id : Entity_Id);
15111 -- When the type is an anonymous access type, create a new access type
15112 -- designating the derived type.
15114 procedure Set_Derived_Name;
15115 -- This procedure sets the appropriate Chars name for New_Subp. This
15116 -- is normally just a copy of the parent name. An exception arises for
15117 -- type support subprograms, where the name is changed to reflect the
15118 -- name of the derived type, e.g. if type foo is derived from type bar,
15119 -- then a procedure barDA is derived with a name fooDA.
15121 ---------------------------
15122 -- Is_Private_Overriding --
15123 ---------------------------
15125 function Is_Private_Overriding return Boolean is
15126 Prev : Entity_Id;
15128 begin
15129 -- If the parent is not a dispatching operation there is no
15130 -- need to investigate overridings
15132 if not Is_Dispatching_Operation (Parent_Subp) then
15133 return False;
15134 end if;
15136 -- The visible operation that is overridden is a homonym of the
15137 -- parent subprogram. We scan the homonym chain to find the one
15138 -- whose alias is the subprogram we are deriving.
15140 Prev := Current_Entity (Parent_Subp);
15141 while Present (Prev) loop
15142 if Ekind (Prev) = Ekind (Parent_Subp)
15143 and then Alias (Prev) = Parent_Subp
15144 and then Scope (Parent_Subp) = Scope (Prev)
15145 and then not Is_Hidden (Prev)
15146 then
15147 Visible_Subp := Prev;
15148 return True;
15149 end if;
15151 Prev := Homonym (Prev);
15152 end loop;
15154 return False;
15155 end Is_Private_Overriding;
15157 ------------------
15158 -- Replace_Type --
15159 ------------------
15161 procedure Replace_Type (Id, New_Id : Entity_Id) is
15162 Id_Type : constant Entity_Id := Etype (Id);
15163 Acc_Type : Entity_Id;
15164 Par : constant Node_Id := Parent (Derived_Type);
15166 begin
15167 -- When the type is an anonymous access type, create a new access
15168 -- type designating the derived type. This itype must be elaborated
15169 -- at the point of the derivation, not on subsequent calls that may
15170 -- be out of the proper scope for Gigi, so we insert a reference to
15171 -- it after the derivation.
15173 if Ekind (Id_Type) = E_Anonymous_Access_Type then
15174 declare
15175 Desig_Typ : Entity_Id := Designated_Type (Id_Type);
15177 begin
15178 if Ekind (Desig_Typ) = E_Record_Type_With_Private
15179 and then Present (Full_View (Desig_Typ))
15180 and then not Is_Private_Type (Parent_Type)
15181 then
15182 Desig_Typ := Full_View (Desig_Typ);
15183 end if;
15185 if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
15187 -- Ada 2005 (AI-251): Handle also derivations of abstract
15188 -- interface primitives.
15190 or else (Is_Interface (Desig_Typ)
15191 and then not Is_Class_Wide_Type (Desig_Typ))
15192 then
15193 Acc_Type := New_Copy (Id_Type);
15194 Set_Etype (Acc_Type, Acc_Type);
15195 Set_Scope (Acc_Type, New_Subp);
15197 -- Set size of anonymous access type. If we have an access
15198 -- to an unconstrained array, this is a fat pointer, so it
15199 -- is sizes at twice addtress size.
15201 if Is_Array_Type (Desig_Typ)
15202 and then not Is_Constrained (Desig_Typ)
15203 then
15204 Init_Size (Acc_Type, 2 * System_Address_Size);
15206 -- Other cases use a thin pointer
15208 else
15209 Init_Size (Acc_Type, System_Address_Size);
15210 end if;
15212 -- Set remaining characterstics of anonymous access type
15214 Init_Alignment (Acc_Type);
15215 Set_Directly_Designated_Type (Acc_Type, Derived_Type);
15217 Set_Etype (New_Id, Acc_Type);
15218 Set_Scope (New_Id, New_Subp);
15220 -- Create a reference to it
15222 Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
15224 else
15225 Set_Etype (New_Id, Id_Type);
15226 end if;
15227 end;
15229 -- In Ada2012, a formal may have an incomplete type but the type
15230 -- derivation that inherits the primitive follows the full view.
15232 elsif Base_Type (Id_Type) = Base_Type (Parent_Type)
15233 or else
15234 (Ekind (Id_Type) = E_Record_Type_With_Private
15235 and then Present (Full_View (Id_Type))
15236 and then
15237 Base_Type (Full_View (Id_Type)) = Base_Type (Parent_Type))
15238 or else
15239 (Ada_Version >= Ada_2012
15240 and then Ekind (Id_Type) = E_Incomplete_Type
15241 and then Full_View (Id_Type) = Parent_Type)
15242 then
15243 -- Constraint checks on formals are generated during expansion,
15244 -- based on the signature of the original subprogram. The bounds
15245 -- of the derived type are not relevant, and thus we can use
15246 -- the base type for the formals. However, the return type may be
15247 -- used in a context that requires that the proper static bounds
15248 -- be used (a case statement, for example) and for those cases
15249 -- we must use the derived type (first subtype), not its base.
15251 -- If the derived_type_definition has no constraints, we know that
15252 -- the derived type has the same constraints as the first subtype
15253 -- of the parent, and we can also use it rather than its base,
15254 -- which can lead to more efficient code.
15256 if Etype (Id) = Parent_Type then
15257 if Is_Scalar_Type (Parent_Type)
15258 and then
15259 Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
15260 then
15261 Set_Etype (New_Id, Derived_Type);
15263 elsif Nkind (Par) = N_Full_Type_Declaration
15264 and then
15265 Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
15266 and then
15267 Is_Entity_Name
15268 (Subtype_Indication (Type_Definition (Par)))
15269 then
15270 Set_Etype (New_Id, Derived_Type);
15272 else
15273 Set_Etype (New_Id, Base_Type (Derived_Type));
15274 end if;
15276 else
15277 Set_Etype (New_Id, Base_Type (Derived_Type));
15278 end if;
15280 else
15281 Set_Etype (New_Id, Etype (Id));
15282 end if;
15283 end Replace_Type;
15285 ----------------------
15286 -- Set_Derived_Name --
15287 ----------------------
15289 procedure Set_Derived_Name is
15290 Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
15291 begin
15292 if Nm = TSS_Null then
15293 Set_Chars (New_Subp, Chars (Parent_Subp));
15294 else
15295 Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
15296 end if;
15297 end Set_Derived_Name;
15299 -- Start of processing for Derive_Subprogram
15301 begin
15302 New_Subp := New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
15303 Set_Ekind (New_Subp, Ekind (Parent_Subp));
15305 -- Check whether the inherited subprogram is a private operation that
15306 -- should be inherited but not yet made visible. Such subprograms can
15307 -- become visible at a later point (e.g., the private part of a public
15308 -- child unit) via Declare_Inherited_Private_Subprograms. If the
15309 -- following predicate is true, then this is not such a private
15310 -- operation and the subprogram simply inherits the name of the parent
15311 -- subprogram. Note the special check for the names of controlled
15312 -- operations, which are currently exempted from being inherited with
15313 -- a hidden name because they must be findable for generation of
15314 -- implicit run-time calls.
15316 if not Is_Hidden (Parent_Subp)
15317 or else Is_Internal (Parent_Subp)
15318 or else Is_Private_Overriding
15319 or else Is_Internal_Name (Chars (Parent_Subp))
15320 or else (Is_Controlled (Parent_Type)
15321 and then Nam_In (Chars (Parent_Subp), Name_Adjust,
15322 Name_Finalize,
15323 Name_Initialize))
15324 then
15325 Set_Derived_Name;
15327 -- An inherited dispatching equality will be overridden by an internally
15328 -- generated one, or by an explicit one, so preserve its name and thus
15329 -- its entry in the dispatch table. Otherwise, if Parent_Subp is a
15330 -- private operation it may become invisible if the full view has
15331 -- progenitors, and the dispatch table will be malformed.
15332 -- We check that the type is limited to handle the anomalous declaration
15333 -- of Limited_Controlled, which is derived from a non-limited type, and
15334 -- which is handled specially elsewhere as well.
15336 elsif Chars (Parent_Subp) = Name_Op_Eq
15337 and then Is_Dispatching_Operation (Parent_Subp)
15338 and then Etype (Parent_Subp) = Standard_Boolean
15339 and then not Is_Limited_Type (Etype (First_Formal (Parent_Subp)))
15340 and then
15341 Etype (First_Formal (Parent_Subp)) =
15342 Etype (Next_Formal (First_Formal (Parent_Subp)))
15343 then
15344 Set_Derived_Name;
15346 -- If parent is hidden, this can be a regular derivation if the
15347 -- parent is immediately visible in a non-instantiating context,
15348 -- or if we are in the private part of an instance. This test
15349 -- should still be refined ???
15351 -- The test for In_Instance_Not_Visible avoids inheriting the derived
15352 -- operation as a non-visible operation in cases where the parent
15353 -- subprogram might not be visible now, but was visible within the
15354 -- original generic, so it would be wrong to make the inherited
15355 -- subprogram non-visible now. (Not clear if this test is fully
15356 -- correct; are there any cases where we should declare the inherited
15357 -- operation as not visible to avoid it being overridden, e.g., when
15358 -- the parent type is a generic actual with private primitives ???)
15360 -- (they should be treated the same as other private inherited
15361 -- subprograms, but it's not clear how to do this cleanly). ???
15363 elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
15364 and then Is_Immediately_Visible (Parent_Subp)
15365 and then not In_Instance)
15366 or else In_Instance_Not_Visible
15367 then
15368 Set_Derived_Name;
15370 -- Ada 2005 (AI-251): Regular derivation if the parent subprogram
15371 -- overrides an interface primitive because interface primitives
15372 -- must be visible in the partial view of the parent (RM 7.3 (7.3/2))
15374 elsif Ada_Version >= Ada_2005
15375 and then Is_Dispatching_Operation (Parent_Subp)
15376 and then Present (Covered_Interface_Op (Parent_Subp))
15377 then
15378 Set_Derived_Name;
15380 -- Otherwise, the type is inheriting a private operation, so enter it
15381 -- with a special name so it can't be overridden.
15383 else
15384 Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
15385 end if;
15387 Set_Parent (New_Subp, Parent (Derived_Type));
15389 if Present (Actual_Subp) then
15390 Replace_Type (Actual_Subp, New_Subp);
15391 else
15392 Replace_Type (Parent_Subp, New_Subp);
15393 end if;
15395 Conditional_Delay (New_Subp, Parent_Subp);
15397 -- If we are creating a renaming for a primitive operation of an
15398 -- actual of a generic derived type, we must examine the signature
15399 -- of the actual primitive, not that of the generic formal, which for
15400 -- example may be an interface. However the name and initial value
15401 -- of the inherited operation are those of the formal primitive.
15403 Formal := First_Formal (Parent_Subp);
15405 if Present (Actual_Subp) then
15406 Formal_Of_Actual := First_Formal (Actual_Subp);
15407 else
15408 Formal_Of_Actual := Empty;
15409 end if;
15411 while Present (Formal) loop
15412 New_Formal := New_Copy (Formal);
15414 -- Normally we do not go copying parents, but in the case of
15415 -- formals, we need to link up to the declaration (which is the
15416 -- parameter specification), and it is fine to link up to the
15417 -- original formal's parameter specification in this case.
15419 Set_Parent (New_Formal, Parent (Formal));
15420 Append_Entity (New_Formal, New_Subp);
15422 if Present (Formal_Of_Actual) then
15423 Replace_Type (Formal_Of_Actual, New_Formal);
15424 Next_Formal (Formal_Of_Actual);
15425 else
15426 Replace_Type (Formal, New_Formal);
15427 end if;
15429 Next_Formal (Formal);
15430 end loop;
15432 -- If this derivation corresponds to a tagged generic actual, then
15433 -- primitive operations rename those of the actual. Otherwise the
15434 -- primitive operations rename those of the parent type, If the parent
15435 -- renames an intrinsic operator, so does the new subprogram. We except
15436 -- concatenation, which is always properly typed, and does not get
15437 -- expanded as other intrinsic operations.
15439 if No (Actual_Subp) then
15440 if Is_Intrinsic_Subprogram (Parent_Subp) then
15441 Set_Is_Intrinsic_Subprogram (New_Subp);
15443 if Present (Alias (Parent_Subp))
15444 and then Chars (Parent_Subp) /= Name_Op_Concat
15445 then
15446 Set_Alias (New_Subp, Alias (Parent_Subp));
15447 else
15448 Set_Alias (New_Subp, Parent_Subp);
15449 end if;
15451 else
15452 Set_Alias (New_Subp, Parent_Subp);
15453 end if;
15455 else
15456 Set_Alias (New_Subp, Actual_Subp);
15457 end if;
15459 -- Derived subprograms of a tagged type must inherit the convention
15460 -- of the parent subprogram (a requirement of AI-117). Derived
15461 -- subprograms of untagged types simply get convention Ada by default.
15463 -- If the derived type is a tagged generic formal type with unknown
15464 -- discriminants, its convention is intrinsic (RM 6.3.1 (8)).
15466 -- However, if the type is derived from a generic formal, the further
15467 -- inherited subprogram has the convention of the non-generic ancestor.
15468 -- Otherwise there would be no way to override the operation.
15469 -- (This is subject to forthcoming ARG discussions).
15471 if Is_Tagged_Type (Derived_Type) then
15472 if Is_Generic_Type (Derived_Type)
15473 and then Has_Unknown_Discriminants (Derived_Type)
15474 then
15475 Set_Convention (New_Subp, Convention_Intrinsic);
15477 else
15478 if Is_Generic_Type (Parent_Type)
15479 and then Has_Unknown_Discriminants (Parent_Type)
15480 then
15481 Set_Convention (New_Subp, Convention (Alias (Parent_Subp)));
15482 else
15483 Set_Convention (New_Subp, Convention (Parent_Subp));
15484 end if;
15485 end if;
15486 end if;
15488 -- Predefined controlled operations retain their name even if the parent
15489 -- is hidden (see above), but they are not primitive operations if the
15490 -- ancestor is not visible, for example if the parent is a private
15491 -- extension completed with a controlled extension. Note that a full
15492 -- type that is controlled can break privacy: the flag Is_Controlled is
15493 -- set on both views of the type.
15495 if Is_Controlled (Parent_Type)
15496 and then Nam_In (Chars (Parent_Subp), Name_Initialize,
15497 Name_Adjust,
15498 Name_Finalize)
15499 and then Is_Hidden (Parent_Subp)
15500 and then not Is_Visibly_Controlled (Parent_Type)
15501 then
15502 Set_Is_Hidden (New_Subp);
15503 end if;
15505 Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
15506 Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
15508 if Ekind (Parent_Subp) = E_Procedure then
15509 Set_Is_Valued_Procedure
15510 (New_Subp, Is_Valued_Procedure (Parent_Subp));
15511 else
15512 Set_Has_Controlling_Result
15513 (New_Subp, Has_Controlling_Result (Parent_Subp));
15514 end if;
15516 -- No_Return must be inherited properly. If this is overridden in the
15517 -- case of a dispatching operation, then a check is made in Sem_Disp
15518 -- that the overriding operation is also No_Return (no such check is
15519 -- required for the case of non-dispatching operation.
15521 Set_No_Return (New_Subp, No_Return (Parent_Subp));
15523 -- A derived function with a controlling result is abstract. If the
15524 -- Derived_Type is a nonabstract formal generic derived type, then
15525 -- inherited operations are not abstract: the required check is done at
15526 -- instantiation time. If the derivation is for a generic actual, the
15527 -- function is not abstract unless the actual is.
15529 if Is_Generic_Type (Derived_Type)
15530 and then not Is_Abstract_Type (Derived_Type)
15531 then
15532 null;
15534 -- Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
15535 -- properties of the subprogram, as defined in RM-3.9.3(4/2-6/2).
15537 -- A subprogram subject to pragma Extensions_Visible with value False
15538 -- requires overriding if the subprogram has at least one controlling
15539 -- OUT parameter (SPARK RM 6.1.7(6)).
15541 elsif Ada_Version >= Ada_2005
15542 and then (Is_Abstract_Subprogram (Alias (New_Subp))
15543 or else (Is_Tagged_Type (Derived_Type)
15544 and then Etype (New_Subp) = Derived_Type
15545 and then not Is_Null_Extension (Derived_Type))
15546 or else (Is_Tagged_Type (Derived_Type)
15547 and then Ekind (Etype (New_Subp)) =
15548 E_Anonymous_Access_Type
15549 and then Designated_Type (Etype (New_Subp)) =
15550 Derived_Type
15551 and then not Is_Null_Extension (Derived_Type))
15552 or else (Comes_From_Source (Alias (New_Subp))
15553 and then Is_EVF_Procedure (Alias (New_Subp))))
15554 and then No (Actual_Subp)
15555 then
15556 if not Is_Tagged_Type (Derived_Type)
15557 or else Is_Abstract_Type (Derived_Type)
15558 or else Is_Abstract_Subprogram (Alias (New_Subp))
15559 then
15560 Set_Is_Abstract_Subprogram (New_Subp);
15561 else
15562 Set_Requires_Overriding (New_Subp);
15563 end if;
15565 elsif Ada_Version < Ada_2005
15566 and then (Is_Abstract_Subprogram (Alias (New_Subp))
15567 or else (Is_Tagged_Type (Derived_Type)
15568 and then Etype (New_Subp) = Derived_Type
15569 and then No (Actual_Subp)))
15570 then
15571 Set_Is_Abstract_Subprogram (New_Subp);
15573 -- AI05-0097 : an inherited operation that dispatches on result is
15574 -- abstract if the derived type is abstract, even if the parent type
15575 -- is concrete and the derived type is a null extension.
15577 elsif Has_Controlling_Result (Alias (New_Subp))
15578 and then Is_Abstract_Type (Etype (New_Subp))
15579 then
15580 Set_Is_Abstract_Subprogram (New_Subp);
15582 -- Finally, if the parent type is abstract we must verify that all
15583 -- inherited operations are either non-abstract or overridden, or that
15584 -- the derived type itself is abstract (this check is performed at the
15585 -- end of a package declaration, in Check_Abstract_Overriding). A
15586 -- private overriding in the parent type will not be visible in the
15587 -- derivation if we are not in an inner package or in a child unit of
15588 -- the parent type, in which case the abstractness of the inherited
15589 -- operation is carried to the new subprogram.
15591 elsif Is_Abstract_Type (Parent_Type)
15592 and then not In_Open_Scopes (Scope (Parent_Type))
15593 and then Is_Private_Overriding
15594 and then Is_Abstract_Subprogram (Visible_Subp)
15595 then
15596 if No (Actual_Subp) then
15597 Set_Alias (New_Subp, Visible_Subp);
15598 Set_Is_Abstract_Subprogram (New_Subp, True);
15600 else
15601 -- If this is a derivation for an instance of a formal derived
15602 -- type, abstractness comes from the primitive operation of the
15603 -- actual, not from the operation inherited from the ancestor.
15605 Set_Is_Abstract_Subprogram
15606 (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
15607 end if;
15608 end if;
15610 New_Overloaded_Entity (New_Subp, Derived_Type);
15612 -- Ada RM 6.1.1 (15): If a subprogram inherits nonconforming class-wide
15613 -- preconditions and the derived type is abstract, the derived operation
15614 -- is abstract as well if parent subprogram is not abstract or null.
15616 if Is_Abstract_Type (Derived_Type)
15617 and then Has_Non_Trivial_Precondition (Parent_Subp)
15618 and then Present (Interfaces (Derived_Type))
15619 then
15621 -- Add useful attributes of subprogram before the freeze point,
15622 -- in case freezing is delayed or there are previous errors.
15624 Set_Is_Dispatching_Operation (New_Subp);
15626 declare
15627 Iface_Prim : constant Entity_Id := Covered_Interface_Op (New_Subp);
15629 begin
15630 if Present (Iface_Prim)
15631 and then Has_Non_Trivial_Precondition (Iface_Prim)
15632 then
15633 Set_Is_Abstract_Subprogram (New_Subp);
15634 end if;
15635 end;
15636 end if;
15638 -- Check for case of a derived subprogram for the instantiation of a
15639 -- formal derived tagged type, if so mark the subprogram as dispatching
15640 -- and inherit the dispatching attributes of the actual subprogram. The
15641 -- derived subprogram is effectively renaming of the actual subprogram,
15642 -- so it needs to have the same attributes as the actual.
15644 if Present (Actual_Subp)
15645 and then Is_Dispatching_Operation (Actual_Subp)
15646 then
15647 Set_Is_Dispatching_Operation (New_Subp);
15649 if Present (DTC_Entity (Actual_Subp)) then
15650 Set_DTC_Entity (New_Subp, DTC_Entity (Actual_Subp));
15651 Set_DT_Position_Value (New_Subp, DT_Position (Actual_Subp));
15652 end if;
15653 end if;
15655 -- Indicate that a derived subprogram does not require a body and that
15656 -- it does not require processing of default expressions.
15658 Set_Has_Completion (New_Subp);
15659 Set_Default_Expressions_Processed (New_Subp);
15661 if Ekind (New_Subp) = E_Function then
15662 Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
15663 end if;
15664 end Derive_Subprogram;
15666 ------------------------
15667 -- Derive_Subprograms --
15668 ------------------------
15670 procedure Derive_Subprograms
15671 (Parent_Type : Entity_Id;
15672 Derived_Type : Entity_Id;
15673 Generic_Actual : Entity_Id := Empty)
15675 Op_List : constant Elist_Id :=
15676 Collect_Primitive_Operations (Parent_Type);
15678 function Check_Derived_Type return Boolean;
15679 -- Check that all the entities derived from Parent_Type are found in
15680 -- the list of primitives of Derived_Type exactly in the same order.
15682 procedure Derive_Interface_Subprogram
15683 (New_Subp : out Entity_Id;
15684 Subp : Entity_Id;
15685 Actual_Subp : Entity_Id);
15686 -- Derive New_Subp from the ultimate alias of the parent subprogram Subp
15687 -- (which is an interface primitive). If Generic_Actual is present then
15688 -- Actual_Subp is the actual subprogram corresponding with the generic
15689 -- subprogram Subp.
15691 ------------------------
15692 -- Check_Derived_Type --
15693 ------------------------
15695 function Check_Derived_Type return Boolean is
15696 E : Entity_Id;
15697 Elmt : Elmt_Id;
15698 List : Elist_Id;
15699 New_Subp : Entity_Id;
15700 Op_Elmt : Elmt_Id;
15701 Subp : Entity_Id;
15703 begin
15704 -- Traverse list of entities in the current scope searching for
15705 -- an incomplete type whose full-view is derived type.
15707 E := First_Entity (Scope (Derived_Type));
15708 while Present (E) and then E /= Derived_Type loop
15709 if Ekind (E) = E_Incomplete_Type
15710 and then Present (Full_View (E))
15711 and then Full_View (E) = Derived_Type
15712 then
15713 -- Disable this test if Derived_Type completes an incomplete
15714 -- type because in such case more primitives can be added
15715 -- later to the list of primitives of Derived_Type by routine
15716 -- Process_Incomplete_Dependents
15718 return True;
15719 end if;
15721 E := Next_Entity (E);
15722 end loop;
15724 List := Collect_Primitive_Operations (Derived_Type);
15725 Elmt := First_Elmt (List);
15727 Op_Elmt := First_Elmt (Op_List);
15728 while Present (Op_Elmt) loop
15729 Subp := Node (Op_Elmt);
15730 New_Subp := Node (Elmt);
15732 -- At this early stage Derived_Type has no entities with attribute
15733 -- Interface_Alias. In addition, such primitives are always
15734 -- located at the end of the list of primitives of Parent_Type.
15735 -- Therefore, if found we can safely stop processing pending
15736 -- entities.
15738 exit when Present (Interface_Alias (Subp));
15740 -- Handle hidden entities
15742 if not Is_Predefined_Dispatching_Operation (Subp)
15743 and then Is_Hidden (Subp)
15744 then
15745 if Present (New_Subp)
15746 and then Primitive_Names_Match (Subp, New_Subp)
15747 then
15748 Next_Elmt (Elmt);
15749 end if;
15751 else
15752 if not Present (New_Subp)
15753 or else Ekind (Subp) /= Ekind (New_Subp)
15754 or else not Primitive_Names_Match (Subp, New_Subp)
15755 then
15756 return False;
15757 end if;
15759 Next_Elmt (Elmt);
15760 end if;
15762 Next_Elmt (Op_Elmt);
15763 end loop;
15765 return True;
15766 end Check_Derived_Type;
15768 ---------------------------------
15769 -- Derive_Interface_Subprogram --
15770 ---------------------------------
15772 procedure Derive_Interface_Subprogram
15773 (New_Subp : out Entity_Id;
15774 Subp : Entity_Id;
15775 Actual_Subp : Entity_Id)
15777 Iface_Subp : constant Entity_Id := Ultimate_Alias (Subp);
15778 Iface_Type : constant Entity_Id := Find_Dispatching_Type (Iface_Subp);
15780 begin
15781 pragma Assert (Is_Interface (Iface_Type));
15783 Derive_Subprogram
15784 (New_Subp => New_Subp,
15785 Parent_Subp => Iface_Subp,
15786 Derived_Type => Derived_Type,
15787 Parent_Type => Iface_Type,
15788 Actual_Subp => Actual_Subp);
15790 -- Given that this new interface entity corresponds with a primitive
15791 -- of the parent that was not overridden we must leave it associated
15792 -- with its parent primitive to ensure that it will share the same
15793 -- dispatch table slot when overridden. We must set the Alias to Subp
15794 -- (instead of Iface_Subp), and we must fix Is_Abstract_Subprogram
15795 -- (in case we inherited Subp from Iface_Type via a nonabstract
15796 -- generic formal type).
15798 if No (Actual_Subp) then
15799 Set_Alias (New_Subp, Subp);
15801 declare
15802 T : Entity_Id := Find_Dispatching_Type (Subp);
15803 begin
15804 while Etype (T) /= T loop
15805 if Is_Generic_Type (T) and then not Is_Abstract_Type (T) then
15806 Set_Is_Abstract_Subprogram (New_Subp, False);
15807 exit;
15808 end if;
15810 T := Etype (T);
15811 end loop;
15812 end;
15814 -- For instantiations this is not needed since the previous call to
15815 -- Derive_Subprogram leaves the entity well decorated.
15817 else
15818 pragma Assert (Alias (New_Subp) = Actual_Subp);
15819 null;
15820 end if;
15821 end Derive_Interface_Subprogram;
15823 -- Local variables
15825 Alias_Subp : Entity_Id;
15826 Act_List : Elist_Id;
15827 Act_Elmt : Elmt_Id;
15828 Act_Subp : Entity_Id := Empty;
15829 Elmt : Elmt_Id;
15830 Need_Search : Boolean := False;
15831 New_Subp : Entity_Id := Empty;
15832 Parent_Base : Entity_Id;
15833 Subp : Entity_Id;
15835 -- Start of processing for Derive_Subprograms
15837 begin
15838 if Ekind (Parent_Type) = E_Record_Type_With_Private
15839 and then Has_Discriminants (Parent_Type)
15840 and then Present (Full_View (Parent_Type))
15841 then
15842 Parent_Base := Full_View (Parent_Type);
15843 else
15844 Parent_Base := Parent_Type;
15845 end if;
15847 if Present (Generic_Actual) then
15848 Act_List := Collect_Primitive_Operations (Generic_Actual);
15849 Act_Elmt := First_Elmt (Act_List);
15850 else
15851 Act_List := No_Elist;
15852 Act_Elmt := No_Elmt;
15853 end if;
15855 -- Derive primitives inherited from the parent. Note that if the generic
15856 -- actual is present, this is not really a type derivation, it is a
15857 -- completion within an instance.
15859 -- Case 1: Derived_Type does not implement interfaces
15861 if not Is_Tagged_Type (Derived_Type)
15862 or else (not Has_Interfaces (Derived_Type)
15863 and then not (Present (Generic_Actual)
15864 and then Has_Interfaces (Generic_Actual)))
15865 then
15866 Elmt := First_Elmt (Op_List);
15867 while Present (Elmt) loop
15868 Subp := Node (Elmt);
15870 -- Literals are derived earlier in the process of building the
15871 -- derived type, and are skipped here.
15873 if Ekind (Subp) = E_Enumeration_Literal then
15874 null;
15876 -- The actual is a direct descendant and the common primitive
15877 -- operations appear in the same order.
15879 -- If the generic parent type is present, the derived type is an
15880 -- instance of a formal derived type, and within the instance its
15881 -- operations are those of the actual. We derive from the formal
15882 -- type but make the inherited operations aliases of the
15883 -- corresponding operations of the actual.
15885 else
15886 pragma Assert (No (Node (Act_Elmt))
15887 or else (Primitive_Names_Match (Subp, Node (Act_Elmt))
15888 and then
15889 Type_Conformant
15890 (Subp, Node (Act_Elmt),
15891 Skip_Controlling_Formals => True)));
15893 Derive_Subprogram
15894 (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
15896 if Present (Act_Elmt) then
15897 Next_Elmt (Act_Elmt);
15898 end if;
15899 end if;
15901 Next_Elmt (Elmt);
15902 end loop;
15904 -- Case 2: Derived_Type implements interfaces
15906 else
15907 -- If the parent type has no predefined primitives we remove
15908 -- predefined primitives from the list of primitives of generic
15909 -- actual to simplify the complexity of this algorithm.
15911 if Present (Generic_Actual) then
15912 declare
15913 Has_Predefined_Primitives : Boolean := False;
15915 begin
15916 -- Check if the parent type has predefined primitives
15918 Elmt := First_Elmt (Op_List);
15919 while Present (Elmt) loop
15920 Subp := Node (Elmt);
15922 if Is_Predefined_Dispatching_Operation (Subp)
15923 and then not Comes_From_Source (Ultimate_Alias (Subp))
15924 then
15925 Has_Predefined_Primitives := True;
15926 exit;
15927 end if;
15929 Next_Elmt (Elmt);
15930 end loop;
15932 -- Remove predefined primitives of Generic_Actual. We must use
15933 -- an auxiliary list because in case of tagged types the value
15934 -- returned by Collect_Primitive_Operations is the value stored
15935 -- in its Primitive_Operations attribute (and we don't want to
15936 -- modify its current contents).
15938 if not Has_Predefined_Primitives then
15939 declare
15940 Aux_List : constant Elist_Id := New_Elmt_List;
15942 begin
15943 Elmt := First_Elmt (Act_List);
15944 while Present (Elmt) loop
15945 Subp := Node (Elmt);
15947 if not Is_Predefined_Dispatching_Operation (Subp)
15948 or else Comes_From_Source (Subp)
15949 then
15950 Append_Elmt (Subp, Aux_List);
15951 end if;
15953 Next_Elmt (Elmt);
15954 end loop;
15956 Act_List := Aux_List;
15957 end;
15958 end if;
15960 Act_Elmt := First_Elmt (Act_List);
15961 Act_Subp := Node (Act_Elmt);
15962 end;
15963 end if;
15965 -- Stage 1: If the generic actual is not present we derive the
15966 -- primitives inherited from the parent type. If the generic parent
15967 -- type is present, the derived type is an instance of a formal
15968 -- derived type, and within the instance its operations are those of
15969 -- the actual. We derive from the formal type but make the inherited
15970 -- operations aliases of the corresponding operations of the actual.
15972 Elmt := First_Elmt (Op_List);
15973 while Present (Elmt) loop
15974 Subp := Node (Elmt);
15975 Alias_Subp := Ultimate_Alias (Subp);
15977 -- Do not derive internal entities of the parent that link
15978 -- interface primitives with their covering primitive. These
15979 -- entities will be added to this type when frozen.
15981 if Present (Interface_Alias (Subp)) then
15982 goto Continue;
15983 end if;
15985 -- If the generic actual is present find the corresponding
15986 -- operation in the generic actual. If the parent type is a
15987 -- direct ancestor of the derived type then, even if it is an
15988 -- interface, the operations are inherited from the primary
15989 -- dispatch table and are in the proper order. If we detect here
15990 -- that primitives are not in the same order we traverse the list
15991 -- of primitive operations of the actual to find the one that
15992 -- implements the interface primitive.
15994 if Need_Search
15995 or else
15996 (Present (Generic_Actual)
15997 and then Present (Act_Subp)
15998 and then not
15999 (Primitive_Names_Match (Subp, Act_Subp)
16000 and then
16001 Type_Conformant (Subp, Act_Subp,
16002 Skip_Controlling_Formals => True)))
16003 then
16004 pragma Assert (not Is_Ancestor (Parent_Base, Generic_Actual,
16005 Use_Full_View => True));
16007 -- Remember that we need searching for all pending primitives
16009 Need_Search := True;
16011 -- Handle entities associated with interface primitives
16013 if Present (Alias_Subp)
16014 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
16015 and then not Is_Predefined_Dispatching_Operation (Subp)
16016 then
16017 -- Search for the primitive in the homonym chain
16019 Act_Subp :=
16020 Find_Primitive_Covering_Interface
16021 (Tagged_Type => Generic_Actual,
16022 Iface_Prim => Alias_Subp);
16024 -- Previous search may not locate primitives covering
16025 -- interfaces defined in generics units or instantiations.
16026 -- (it fails if the covering primitive has formals whose
16027 -- type is also defined in generics or instantiations).
16028 -- In such case we search in the list of primitives of the
16029 -- generic actual for the internal entity that links the
16030 -- interface primitive and the covering primitive.
16032 if No (Act_Subp)
16033 and then Is_Generic_Type (Parent_Type)
16034 then
16035 -- This code has been designed to handle only generic
16036 -- formals that implement interfaces that are defined
16037 -- in a generic unit or instantiation. If this code is
16038 -- needed for other cases we must review it because
16039 -- (given that it relies on Original_Location to locate
16040 -- the primitive of Generic_Actual that covers the
16041 -- interface) it could leave linked through attribute
16042 -- Alias entities of unrelated instantiations).
16044 pragma Assert
16045 (Is_Generic_Unit
16046 (Scope (Find_Dispatching_Type (Alias_Subp)))
16047 or else
16048 Instantiation_Depth
16049 (Sloc (Find_Dispatching_Type (Alias_Subp))) > 0);
16051 declare
16052 Iface_Prim_Loc : constant Source_Ptr :=
16053 Original_Location (Sloc (Alias_Subp));
16055 Elmt : Elmt_Id;
16056 Prim : Entity_Id;
16058 begin
16059 Elmt :=
16060 First_Elmt (Primitive_Operations (Generic_Actual));
16062 Search : while Present (Elmt) loop
16063 Prim := Node (Elmt);
16065 if Present (Interface_Alias (Prim))
16066 and then Original_Location
16067 (Sloc (Interface_Alias (Prim))) =
16068 Iface_Prim_Loc
16069 then
16070 Act_Subp := Alias (Prim);
16071 exit Search;
16072 end if;
16074 Next_Elmt (Elmt);
16075 end loop Search;
16076 end;
16077 end if;
16079 pragma Assert (Present (Act_Subp)
16080 or else Is_Abstract_Type (Generic_Actual)
16081 or else Serious_Errors_Detected > 0);
16083 -- Handle predefined primitives plus the rest of user-defined
16084 -- primitives
16086 else
16087 Act_Elmt := First_Elmt (Act_List);
16088 while Present (Act_Elmt) loop
16089 Act_Subp := Node (Act_Elmt);
16091 exit when Primitive_Names_Match (Subp, Act_Subp)
16092 and then Type_Conformant
16093 (Subp, Act_Subp,
16094 Skip_Controlling_Formals => True)
16095 and then No (Interface_Alias (Act_Subp));
16097 Next_Elmt (Act_Elmt);
16098 end loop;
16100 if No (Act_Elmt) then
16101 Act_Subp := Empty;
16102 end if;
16103 end if;
16104 end if;
16106 -- Case 1: If the parent is a limited interface then it has the
16107 -- predefined primitives of synchronized interfaces. However, the
16108 -- actual type may be a non-limited type and hence it does not
16109 -- have such primitives.
16111 if Present (Generic_Actual)
16112 and then not Present (Act_Subp)
16113 and then Is_Limited_Interface (Parent_Base)
16114 and then Is_Predefined_Interface_Primitive (Subp)
16115 then
16116 null;
16118 -- Case 2: Inherit entities associated with interfaces that were
16119 -- not covered by the parent type. We exclude here null interface
16120 -- primitives because they do not need special management.
16122 -- We also exclude interface operations that are renamings. If the
16123 -- subprogram is an explicit renaming of an interface primitive,
16124 -- it is a regular primitive operation, and the presence of its
16125 -- alias is not relevant: it has to be derived like any other
16126 -- primitive.
16128 elsif Present (Alias (Subp))
16129 and then Nkind (Unit_Declaration_Node (Subp)) /=
16130 N_Subprogram_Renaming_Declaration
16131 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
16132 and then not
16133 (Nkind (Parent (Alias_Subp)) = N_Procedure_Specification
16134 and then Null_Present (Parent (Alias_Subp)))
16135 then
16136 -- If this is an abstract private type then we transfer the
16137 -- derivation of the interface primitive from the partial view
16138 -- to the full view. This is safe because all the interfaces
16139 -- must be visible in the partial view. Done to avoid adding
16140 -- a new interface derivation to the private part of the
16141 -- enclosing package; otherwise this new derivation would be
16142 -- decorated as hidden when the analysis of the enclosing
16143 -- package completes.
16145 if Is_Abstract_Type (Derived_Type)
16146 and then In_Private_Part (Current_Scope)
16147 and then Has_Private_Declaration (Derived_Type)
16148 then
16149 declare
16150 Partial_View : Entity_Id;
16151 Elmt : Elmt_Id;
16152 Ent : Entity_Id;
16154 begin
16155 Partial_View := First_Entity (Current_Scope);
16156 loop
16157 exit when No (Partial_View)
16158 or else (Has_Private_Declaration (Partial_View)
16159 and then
16160 Full_View (Partial_View) = Derived_Type);
16162 Next_Entity (Partial_View);
16163 end loop;
16165 -- If the partial view was not found then the source code
16166 -- has errors and the derivation is not needed.
16168 if Present (Partial_View) then
16169 Elmt :=
16170 First_Elmt (Primitive_Operations (Partial_View));
16171 while Present (Elmt) loop
16172 Ent := Node (Elmt);
16174 if Present (Alias (Ent))
16175 and then Ultimate_Alias (Ent) = Alias (Subp)
16176 then
16177 Append_Elmt
16178 (Ent, Primitive_Operations (Derived_Type));
16179 exit;
16180 end if;
16182 Next_Elmt (Elmt);
16183 end loop;
16185 -- If the interface primitive was not found in the
16186 -- partial view then this interface primitive was
16187 -- overridden. We add a derivation to activate in
16188 -- Derive_Progenitor_Subprograms the machinery to
16189 -- search for it.
16191 if No (Elmt) then
16192 Derive_Interface_Subprogram
16193 (New_Subp => New_Subp,
16194 Subp => Subp,
16195 Actual_Subp => Act_Subp);
16196 end if;
16197 end if;
16198 end;
16199 else
16200 Derive_Interface_Subprogram
16201 (New_Subp => New_Subp,
16202 Subp => Subp,
16203 Actual_Subp => Act_Subp);
16204 end if;
16206 -- Case 3: Common derivation
16208 else
16209 Derive_Subprogram
16210 (New_Subp => New_Subp,
16211 Parent_Subp => Subp,
16212 Derived_Type => Derived_Type,
16213 Parent_Type => Parent_Base,
16214 Actual_Subp => Act_Subp);
16215 end if;
16217 -- No need to update Act_Elm if we must search for the
16218 -- corresponding operation in the generic actual
16220 if not Need_Search
16221 and then Present (Act_Elmt)
16222 then
16223 Next_Elmt (Act_Elmt);
16224 Act_Subp := Node (Act_Elmt);
16225 end if;
16227 <<Continue>>
16228 Next_Elmt (Elmt);
16229 end loop;
16231 -- Inherit additional operations from progenitors. If the derived
16232 -- type is a generic actual, there are not new primitive operations
16233 -- for the type because it has those of the actual, and therefore
16234 -- nothing needs to be done. The renamings generated above are not
16235 -- primitive operations, and their purpose is simply to make the
16236 -- proper operations visible within an instantiation.
16238 if No (Generic_Actual) then
16239 Derive_Progenitor_Subprograms (Parent_Base, Derived_Type);
16240 end if;
16241 end if;
16243 -- Final check: Direct descendants must have their primitives in the
16244 -- same order. We exclude from this test untagged types and instances
16245 -- of formal derived types. We skip this test if we have already
16246 -- reported serious errors in the sources.
16248 pragma Assert (not Is_Tagged_Type (Derived_Type)
16249 or else Present (Generic_Actual)
16250 or else Serious_Errors_Detected > 0
16251 or else Check_Derived_Type);
16252 end Derive_Subprograms;
16254 --------------------------------
16255 -- Derived_Standard_Character --
16256 --------------------------------
16258 procedure Derived_Standard_Character
16259 (N : Node_Id;
16260 Parent_Type : Entity_Id;
16261 Derived_Type : Entity_Id)
16263 Loc : constant Source_Ptr := Sloc (N);
16264 Def : constant Node_Id := Type_Definition (N);
16265 Indic : constant Node_Id := Subtype_Indication (Def);
16266 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
16267 Implicit_Base : constant Entity_Id :=
16268 Create_Itype
16269 (E_Enumeration_Type, N, Derived_Type, 'B');
16271 Lo : Node_Id;
16272 Hi : Node_Id;
16274 begin
16275 Discard_Node (Process_Subtype (Indic, N));
16277 Set_Etype (Implicit_Base, Parent_Base);
16278 Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
16279 Set_RM_Size (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
16281 Set_Is_Character_Type (Implicit_Base, True);
16282 Set_Has_Delayed_Freeze (Implicit_Base);
16284 -- The bounds of the implicit base are the bounds of the parent base.
16285 -- Note that their type is the parent base.
16287 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
16288 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
16290 Set_Scalar_Range (Implicit_Base,
16291 Make_Range (Loc,
16292 Low_Bound => Lo,
16293 High_Bound => Hi));
16295 Conditional_Delay (Derived_Type, Parent_Type);
16297 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
16298 Set_Etype (Derived_Type, Implicit_Base);
16299 Set_Size_Info (Derived_Type, Parent_Type);
16301 if Unknown_RM_Size (Derived_Type) then
16302 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
16303 end if;
16305 Set_Is_Character_Type (Derived_Type, True);
16307 if Nkind (Indic) /= N_Subtype_Indication then
16309 -- If no explicit constraint, the bounds are those
16310 -- of the parent type.
16312 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Type));
16313 Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
16314 Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
16315 end if;
16317 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
16319 -- Because the implicit base is used in the conversion of the bounds, we
16320 -- have to freeze it now. This is similar to what is done for numeric
16321 -- types, and it equally suspicious, but otherwise a non-static bound
16322 -- will have a reference to an unfrozen type, which is rejected by Gigi
16323 -- (???). This requires specific care for definition of stream
16324 -- attributes. For details, see comments at the end of
16325 -- Build_Derived_Numeric_Type.
16327 Freeze_Before (N, Implicit_Base);
16328 end Derived_Standard_Character;
16330 ------------------------------
16331 -- Derived_Type_Declaration --
16332 ------------------------------
16334 procedure Derived_Type_Declaration
16335 (T : Entity_Id;
16336 N : Node_Id;
16337 Is_Completion : Boolean)
16339 Parent_Type : Entity_Id;
16341 function Comes_From_Generic (Typ : Entity_Id) return Boolean;
16342 -- Check whether the parent type is a generic formal, or derives
16343 -- directly or indirectly from one.
16345 ------------------------
16346 -- Comes_From_Generic --
16347 ------------------------
16349 function Comes_From_Generic (Typ : Entity_Id) return Boolean is
16350 begin
16351 if Is_Generic_Type (Typ) then
16352 return True;
16354 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
16355 return True;
16357 elsif Is_Private_Type (Typ)
16358 and then Present (Full_View (Typ))
16359 and then Is_Generic_Type (Root_Type (Full_View (Typ)))
16360 then
16361 return True;
16363 elsif Is_Generic_Actual_Type (Typ) then
16364 return True;
16366 else
16367 return False;
16368 end if;
16369 end Comes_From_Generic;
16371 -- Local variables
16373 Def : constant Node_Id := Type_Definition (N);
16374 Iface_Def : Node_Id;
16375 Indic : constant Node_Id := Subtype_Indication (Def);
16376 Extension : constant Node_Id := Record_Extension_Part (Def);
16377 Parent_Node : Node_Id;
16378 Taggd : Boolean;
16380 -- Start of processing for Derived_Type_Declaration
16382 begin
16383 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
16385 if SPARK_Mode = On
16386 and then Is_Tagged_Type (Parent_Type)
16387 then
16388 declare
16389 Partial_View : constant Entity_Id :=
16390 Incomplete_Or_Partial_View (Parent_Type);
16392 begin
16393 -- If the partial view was not found then the parent type is not
16394 -- a private type. Otherwise check if the partial view is a tagged
16395 -- private type.
16397 if Present (Partial_View)
16398 and then Is_Private_Type (Partial_View)
16399 and then not Is_Tagged_Type (Partial_View)
16400 then
16401 Error_Msg_NE
16402 ("cannot derive from & declared as untagged private "
16403 & "(SPARK RM 3.4(1))", N, Partial_View);
16404 end if;
16405 end;
16406 end if;
16408 -- Ada 2005 (AI-251): In case of interface derivation check that the
16409 -- parent is also an interface.
16411 if Interface_Present (Def) then
16412 Check_SPARK_05_Restriction ("interface is not allowed", Def);
16414 if not Is_Interface (Parent_Type) then
16415 Diagnose_Interface (Indic, Parent_Type);
16417 else
16418 Parent_Node := Parent (Base_Type (Parent_Type));
16419 Iface_Def := Type_Definition (Parent_Node);
16421 -- Ada 2005 (AI-251): Limited interfaces can only inherit from
16422 -- other limited interfaces.
16424 if Limited_Present (Def) then
16425 if Limited_Present (Iface_Def) then
16426 null;
16428 elsif Protected_Present (Iface_Def) then
16429 Error_Msg_NE
16430 ("descendant of & must be declared as a protected "
16431 & "interface", N, Parent_Type);
16433 elsif Synchronized_Present (Iface_Def) then
16434 Error_Msg_NE
16435 ("descendant of & must be declared as a synchronized "
16436 & "interface", N, Parent_Type);
16438 elsif Task_Present (Iface_Def) then
16439 Error_Msg_NE
16440 ("descendant of & must be declared as a task interface",
16441 N, Parent_Type);
16443 else
16444 Error_Msg_N
16445 ("(Ada 2005) limited interface cannot inherit from "
16446 & "non-limited interface", Indic);
16447 end if;
16449 -- Ada 2005 (AI-345): Non-limited interfaces can only inherit
16450 -- from non-limited or limited interfaces.
16452 elsif not Protected_Present (Def)
16453 and then not Synchronized_Present (Def)
16454 and then not Task_Present (Def)
16455 then
16456 if Limited_Present (Iface_Def) then
16457 null;
16459 elsif Protected_Present (Iface_Def) then
16460 Error_Msg_NE
16461 ("descendant of & must be declared as a protected "
16462 & "interface", N, Parent_Type);
16464 elsif Synchronized_Present (Iface_Def) then
16465 Error_Msg_NE
16466 ("descendant of & must be declared as a synchronized "
16467 & "interface", N, Parent_Type);
16469 elsif Task_Present (Iface_Def) then
16470 Error_Msg_NE
16471 ("descendant of & must be declared as a task interface",
16472 N, Parent_Type);
16473 else
16474 null;
16475 end if;
16476 end if;
16477 end if;
16478 end if;
16480 if Is_Tagged_Type (Parent_Type)
16481 and then Is_Concurrent_Type (Parent_Type)
16482 and then not Is_Interface (Parent_Type)
16483 then
16484 Error_Msg_N
16485 ("parent type of a record extension cannot be a synchronized "
16486 & "tagged type (RM 3.9.1 (3/1))", N);
16487 Set_Etype (T, Any_Type);
16488 return;
16489 end if;
16491 -- Ada 2005 (AI-251): Decorate all the names in the list of ancestor
16492 -- interfaces
16494 if Is_Tagged_Type (Parent_Type)
16495 and then Is_Non_Empty_List (Interface_List (Def))
16496 then
16497 declare
16498 Intf : Node_Id;
16499 T : Entity_Id;
16501 begin
16502 Intf := First (Interface_List (Def));
16503 while Present (Intf) loop
16504 T := Find_Type_Of_Subtype_Indic (Intf);
16506 if not Is_Interface (T) then
16507 Diagnose_Interface (Intf, T);
16509 -- Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
16510 -- a limited type from having a nonlimited progenitor.
16512 elsif (Limited_Present (Def)
16513 or else (not Is_Interface (Parent_Type)
16514 and then Is_Limited_Type (Parent_Type)))
16515 and then not Is_Limited_Interface (T)
16516 then
16517 Error_Msg_NE
16518 ("progenitor interface& of limited type must be limited",
16519 N, T);
16520 end if;
16522 Next (Intf);
16523 end loop;
16524 end;
16525 end if;
16527 if Parent_Type = Any_Type
16528 or else Etype (Parent_Type) = Any_Type
16529 or else (Is_Class_Wide_Type (Parent_Type)
16530 and then Etype (Parent_Type) = T)
16531 then
16532 -- If Parent_Type is undefined or illegal, make new type into a
16533 -- subtype of Any_Type, and set a few attributes to prevent cascaded
16534 -- errors. If this is a self-definition, emit error now.
16536 if T = Parent_Type or else T = Etype (Parent_Type) then
16537 Error_Msg_N ("type cannot be used in its own definition", Indic);
16538 end if;
16540 Set_Ekind (T, Ekind (Parent_Type));
16541 Set_Etype (T, Any_Type);
16542 Set_Scalar_Range (T, Scalar_Range (Any_Type));
16544 if Is_Tagged_Type (T)
16545 and then Is_Record_Type (T)
16546 then
16547 Set_Direct_Primitive_Operations (T, New_Elmt_List);
16548 end if;
16550 return;
16551 end if;
16553 -- Ada 2005 (AI-251): The case in which the parent of the full-view is
16554 -- an interface is special because the list of interfaces in the full
16555 -- view can be given in any order. For example:
16557 -- type A is interface;
16558 -- type B is interface and A;
16559 -- type D is new B with private;
16560 -- private
16561 -- type D is new A and B with null record; -- 1 --
16563 -- In this case we perform the following transformation of -1-:
16565 -- type D is new B and A with null record;
16567 -- If the parent of the full-view covers the parent of the partial-view
16568 -- we have two possible cases:
16570 -- 1) They have the same parent
16571 -- 2) The parent of the full-view implements some further interfaces
16573 -- In both cases we do not need to perform the transformation. In the
16574 -- first case the source program is correct and the transformation is
16575 -- not needed; in the second case the source program does not fulfill
16576 -- the no-hidden interfaces rule (AI-396) and the error will be reported
16577 -- later.
16579 -- This transformation not only simplifies the rest of the analysis of
16580 -- this type declaration but also simplifies the correct generation of
16581 -- the object layout to the expander.
16583 if In_Private_Part (Current_Scope)
16584 and then Is_Interface (Parent_Type)
16585 then
16586 declare
16587 Iface : Node_Id;
16588 Partial_View : Entity_Id;
16589 Partial_View_Parent : Entity_Id;
16590 New_Iface : Node_Id;
16592 begin
16593 -- Look for the associated private type declaration
16595 Partial_View := Incomplete_Or_Partial_View (T);
16597 -- If the partial view was not found then the source code has
16598 -- errors and the transformation is not needed.
16600 if Present (Partial_View) then
16601 Partial_View_Parent := Etype (Partial_View);
16603 -- If the parent of the full-view covers the parent of the
16604 -- partial-view we have nothing else to do.
16606 if Interface_Present_In_Ancestor
16607 (Parent_Type, Partial_View_Parent)
16608 then
16609 null;
16611 -- Traverse the list of interfaces of the full-view to look
16612 -- for the parent of the partial-view and perform the tree
16613 -- transformation.
16615 else
16616 Iface := First (Interface_List (Def));
16617 while Present (Iface) loop
16618 if Etype (Iface) = Etype (Partial_View) then
16619 Rewrite (Subtype_Indication (Def),
16620 New_Copy (Subtype_Indication
16621 (Parent (Partial_View))));
16623 New_Iface :=
16624 Make_Identifier (Sloc (N), Chars (Parent_Type));
16625 Append (New_Iface, Interface_List (Def));
16627 -- Analyze the transformed code
16629 Derived_Type_Declaration (T, N, Is_Completion);
16630 return;
16631 end if;
16633 Next (Iface);
16634 end loop;
16635 end if;
16636 end if;
16637 end;
16638 end if;
16640 -- Only composite types other than array types are allowed to have
16641 -- discriminants.
16643 if Present (Discriminant_Specifications (N)) then
16644 if (Is_Elementary_Type (Parent_Type)
16645 or else
16646 Is_Array_Type (Parent_Type))
16647 and then not Error_Posted (N)
16648 then
16649 Error_Msg_N
16650 ("elementary or array type cannot have discriminants",
16651 Defining_Identifier (First (Discriminant_Specifications (N))));
16652 Set_Has_Discriminants (T, False);
16654 -- The type is allowed to have discriminants
16656 else
16657 Check_SPARK_05_Restriction ("discriminant type is not allowed", N);
16658 end if;
16659 end if;
16661 -- In Ada 83, a derived type defined in a package specification cannot
16662 -- be used for further derivation until the end of its visible part.
16663 -- Note that derivation in the private part of the package is allowed.
16665 if Ada_Version = Ada_83
16666 and then Is_Derived_Type (Parent_Type)
16667 and then In_Visible_Part (Scope (Parent_Type))
16668 then
16669 if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
16670 Error_Msg_N
16671 ("(Ada 83): premature use of type for derivation", Indic);
16672 end if;
16673 end if;
16675 -- Check for early use of incomplete or private type
16677 if Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
16678 Error_Msg_N ("premature derivation of incomplete type", Indic);
16679 return;
16681 elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
16682 and then not Comes_From_Generic (Parent_Type))
16683 or else Has_Private_Component (Parent_Type)
16684 then
16685 -- The ancestor type of a formal type can be incomplete, in which
16686 -- case only the operations of the partial view are available in the
16687 -- generic. Subsequent checks may be required when the full view is
16688 -- analyzed to verify that a derivation from a tagged type has an
16689 -- extension.
16691 if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
16692 null;
16694 elsif No (Underlying_Type (Parent_Type))
16695 or else Has_Private_Component (Parent_Type)
16696 then
16697 Error_Msg_N
16698 ("premature derivation of derived or private type", Indic);
16700 -- Flag the type itself as being in error, this prevents some
16701 -- nasty problems with subsequent uses of the malformed type.
16703 Set_Error_Posted (T);
16705 -- Check that within the immediate scope of an untagged partial
16706 -- view it's illegal to derive from the partial view if the
16707 -- full view is tagged. (7.3(7))
16709 -- We verify that the Parent_Type is a partial view by checking
16710 -- that it is not a Full_Type_Declaration (i.e. a private type or
16711 -- private extension declaration), to distinguish a partial view
16712 -- from a derivation from a private type which also appears as
16713 -- E_Private_Type. If the parent base type is not declared in an
16714 -- enclosing scope there is no need to check.
16716 elsif Present (Full_View (Parent_Type))
16717 and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
16718 and then not Is_Tagged_Type (Parent_Type)
16719 and then Is_Tagged_Type (Full_View (Parent_Type))
16720 and then In_Open_Scopes (Scope (Base_Type (Parent_Type)))
16721 then
16722 Error_Msg_N
16723 ("premature derivation from type with tagged full view",
16724 Indic);
16725 end if;
16726 end if;
16728 -- Check that form of derivation is appropriate
16730 Taggd := Is_Tagged_Type (Parent_Type);
16732 -- Set the parent type to the class-wide type's specific type in this
16733 -- case to prevent cascading errors
16735 if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
16736 Error_Msg_N ("parent type must not be a class-wide type", Indic);
16737 Set_Etype (T, Etype (Parent_Type));
16738 return;
16739 end if;
16741 if Present (Extension) and then not Taggd then
16742 Error_Msg_N
16743 ("type derived from untagged type cannot have extension", Indic);
16745 elsif No (Extension) and then Taggd then
16747 -- If this declaration is within a private part (or body) of a
16748 -- generic instantiation then the derivation is allowed (the parent
16749 -- type can only appear tagged in this case if it's a generic actual
16750 -- type, since it would otherwise have been rejected in the analysis
16751 -- of the generic template).
16753 if not Is_Generic_Actual_Type (Parent_Type)
16754 or else In_Visible_Part (Scope (Parent_Type))
16755 then
16756 if Is_Class_Wide_Type (Parent_Type) then
16757 Error_Msg_N
16758 ("parent type must not be a class-wide type", Indic);
16760 -- Use specific type to prevent cascaded errors.
16762 Parent_Type := Etype (Parent_Type);
16764 else
16765 Error_Msg_N
16766 ("type derived from tagged type must have extension", Indic);
16767 end if;
16768 end if;
16769 end if;
16771 -- AI-443: Synchronized formal derived types require a private
16772 -- extension. There is no point in checking the ancestor type or
16773 -- the progenitors since the construct is wrong to begin with.
16775 if Ada_Version >= Ada_2005
16776 and then Is_Generic_Type (T)
16777 and then Present (Original_Node (N))
16778 then
16779 declare
16780 Decl : constant Node_Id := Original_Node (N);
16782 begin
16783 if Nkind (Decl) = N_Formal_Type_Declaration
16784 and then Nkind (Formal_Type_Definition (Decl)) =
16785 N_Formal_Derived_Type_Definition
16786 and then Synchronized_Present (Formal_Type_Definition (Decl))
16787 and then No (Extension)
16789 -- Avoid emitting a duplicate error message
16791 and then not Error_Posted (Indic)
16792 then
16793 Error_Msg_N
16794 ("synchronized derived type must have extension", N);
16795 end if;
16796 end;
16797 end if;
16799 if Null_Exclusion_Present (Def)
16800 and then not Is_Access_Type (Parent_Type)
16801 then
16802 Error_Msg_N ("null exclusion can only apply to an access type", N);
16803 end if;
16805 -- Avoid deriving parent primitives of underlying record views
16807 Build_Derived_Type (N, Parent_Type, T, Is_Completion,
16808 Derive_Subps => not Is_Underlying_Record_View (T));
16810 -- AI-419: The parent type of an explicitly limited derived type must
16811 -- be a limited type or a limited interface.
16813 if Limited_Present (Def) then
16814 Set_Is_Limited_Record (T);
16816 if Is_Interface (T) then
16817 Set_Is_Limited_Interface (T);
16818 end if;
16820 if not Is_Limited_Type (Parent_Type)
16821 and then
16822 (not Is_Interface (Parent_Type)
16823 or else not Is_Limited_Interface (Parent_Type))
16824 then
16825 -- AI05-0096: a derivation in the private part of an instance is
16826 -- legal if the generic formal is untagged limited, and the actual
16827 -- is non-limited.
16829 if Is_Generic_Actual_Type (Parent_Type)
16830 and then In_Private_Part (Current_Scope)
16831 and then
16832 not Is_Tagged_Type
16833 (Generic_Parent_Type (Parent (Parent_Type)))
16834 then
16835 null;
16837 else
16838 Error_Msg_NE
16839 ("parent type& of limited type must be limited",
16840 N, Parent_Type);
16841 end if;
16842 end if;
16843 end if;
16845 -- In SPARK, there are no derived type definitions other than type
16846 -- extensions of tagged record types.
16848 if No (Extension) then
16849 Check_SPARK_05_Restriction
16850 ("derived type is not allowed", Original_Node (N));
16851 end if;
16852 end Derived_Type_Declaration;
16854 ------------------------
16855 -- Diagnose_Interface --
16856 ------------------------
16858 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id) is
16859 begin
16860 if not Is_Interface (E) and then E /= Any_Type then
16861 Error_Msg_NE ("(Ada 2005) & must be an interface", N, E);
16862 end if;
16863 end Diagnose_Interface;
16865 ----------------------------------
16866 -- Enumeration_Type_Declaration --
16867 ----------------------------------
16869 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
16870 Ev : Uint;
16871 L : Node_Id;
16872 R_Node : Node_Id;
16873 B_Node : Node_Id;
16875 begin
16876 -- Create identifier node representing lower bound
16878 B_Node := New_Node (N_Identifier, Sloc (Def));
16879 L := First (Literals (Def));
16880 Set_Chars (B_Node, Chars (L));
16881 Set_Entity (B_Node, L);
16882 Set_Etype (B_Node, T);
16883 Set_Is_Static_Expression (B_Node, True);
16885 R_Node := New_Node (N_Range, Sloc (Def));
16886 Set_Low_Bound (R_Node, B_Node);
16888 Set_Ekind (T, E_Enumeration_Type);
16889 Set_First_Literal (T, L);
16890 Set_Etype (T, T);
16891 Set_Is_Constrained (T);
16893 Ev := Uint_0;
16895 -- Loop through literals of enumeration type setting pos and rep values
16896 -- except that if the Ekind is already set, then it means the literal
16897 -- was already constructed (case of a derived type declaration and we
16898 -- should not disturb the Pos and Rep values.
16900 while Present (L) loop
16901 if Ekind (L) /= E_Enumeration_Literal then
16902 Set_Ekind (L, E_Enumeration_Literal);
16903 Set_Enumeration_Pos (L, Ev);
16904 Set_Enumeration_Rep (L, Ev);
16905 Set_Is_Known_Valid (L, True);
16906 end if;
16908 Set_Etype (L, T);
16909 New_Overloaded_Entity (L);
16910 Generate_Definition (L);
16911 Set_Convention (L, Convention_Intrinsic);
16913 -- Case of character literal
16915 if Nkind (L) = N_Defining_Character_Literal then
16916 Set_Is_Character_Type (T, True);
16918 -- Check violation of No_Wide_Characters
16920 if Restriction_Check_Required (No_Wide_Characters) then
16921 Get_Name_String (Chars (L));
16923 if Name_Len >= 3 and then Name_Buffer (1 .. 2) = "QW" then
16924 Check_Restriction (No_Wide_Characters, L);
16925 end if;
16926 end if;
16927 end if;
16929 Ev := Ev + 1;
16930 Next (L);
16931 end loop;
16933 -- Now create a node representing upper bound
16935 B_Node := New_Node (N_Identifier, Sloc (Def));
16936 Set_Chars (B_Node, Chars (Last (Literals (Def))));
16937 Set_Entity (B_Node, Last (Literals (Def)));
16938 Set_Etype (B_Node, T);
16939 Set_Is_Static_Expression (B_Node, True);
16941 Set_High_Bound (R_Node, B_Node);
16943 -- Initialize various fields of the type. Some of this information
16944 -- may be overwritten later through rep.clauses.
16946 Set_Scalar_Range (T, R_Node);
16947 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
16948 Set_Enum_Esize (T);
16949 Set_Enum_Pos_To_Rep (T, Empty);
16951 -- Set Discard_Names if configuration pragma set, or if there is
16952 -- a parameterless pragma in the current declarative region
16954 if Global_Discard_Names or else Discard_Names (Scope (T)) then
16955 Set_Discard_Names (T);
16956 end if;
16958 -- Process end label if there is one
16960 if Present (Def) then
16961 Process_End_Label (Def, 'e', T);
16962 end if;
16963 end Enumeration_Type_Declaration;
16965 ---------------------------------
16966 -- Expand_To_Stored_Constraint --
16967 ---------------------------------
16969 function Expand_To_Stored_Constraint
16970 (Typ : Entity_Id;
16971 Constraint : Elist_Id) return Elist_Id
16973 Explicitly_Discriminated_Type : Entity_Id;
16974 Expansion : Elist_Id;
16975 Discriminant : Entity_Id;
16977 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
16978 -- Find the nearest type that actually specifies discriminants
16980 ---------------------------------
16981 -- Type_With_Explicit_Discrims --
16982 ---------------------------------
16984 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
16985 Typ : constant E := Base_Type (Id);
16987 begin
16988 if Ekind (Typ) in Incomplete_Or_Private_Kind then
16989 if Present (Full_View (Typ)) then
16990 return Type_With_Explicit_Discrims (Full_View (Typ));
16991 end if;
16993 else
16994 if Has_Discriminants (Typ) then
16995 return Typ;
16996 end if;
16997 end if;
16999 if Etype (Typ) = Typ then
17000 return Empty;
17001 elsif Has_Discriminants (Typ) then
17002 return Typ;
17003 else
17004 return Type_With_Explicit_Discrims (Etype (Typ));
17005 end if;
17007 end Type_With_Explicit_Discrims;
17009 -- Start of processing for Expand_To_Stored_Constraint
17011 begin
17012 if No (Constraint) or else Is_Empty_Elmt_List (Constraint) then
17013 return No_Elist;
17014 end if;
17016 Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
17018 if No (Explicitly_Discriminated_Type) then
17019 return No_Elist;
17020 end if;
17022 Expansion := New_Elmt_List;
17024 Discriminant :=
17025 First_Stored_Discriminant (Explicitly_Discriminated_Type);
17026 while Present (Discriminant) loop
17027 Append_Elmt
17028 (Get_Discriminant_Value
17029 (Discriminant, Explicitly_Discriminated_Type, Constraint),
17030 To => Expansion);
17031 Next_Stored_Discriminant (Discriminant);
17032 end loop;
17034 return Expansion;
17035 end Expand_To_Stored_Constraint;
17037 ---------------------------
17038 -- Find_Hidden_Interface --
17039 ---------------------------
17041 function Find_Hidden_Interface
17042 (Src : Elist_Id;
17043 Dest : Elist_Id) return Entity_Id
17045 Iface : Entity_Id;
17046 Iface_Elmt : Elmt_Id;
17048 begin
17049 if Present (Src) and then Present (Dest) then
17050 Iface_Elmt := First_Elmt (Src);
17051 while Present (Iface_Elmt) loop
17052 Iface := Node (Iface_Elmt);
17054 if Is_Interface (Iface)
17055 and then not Contain_Interface (Iface, Dest)
17056 then
17057 return Iface;
17058 end if;
17060 Next_Elmt (Iface_Elmt);
17061 end loop;
17062 end if;
17064 return Empty;
17065 end Find_Hidden_Interface;
17067 --------------------
17068 -- Find_Type_Name --
17069 --------------------
17071 function Find_Type_Name (N : Node_Id) return Entity_Id is
17072 Id : constant Entity_Id := Defining_Identifier (N);
17073 New_Id : Entity_Id;
17074 Prev : Entity_Id;
17075 Prev_Par : Node_Id;
17077 procedure Check_Duplicate_Aspects;
17078 -- Check that aspects specified in a completion have not been specified
17079 -- already in the partial view.
17081 procedure Tag_Mismatch;
17082 -- Diagnose a tagged partial view whose full view is untagged. We post
17083 -- the message on the full view, with a reference to the previous
17084 -- partial view. The partial view can be private or incomplete, and
17085 -- these are handled in a different manner, so we determine the position
17086 -- of the error message from the respective slocs of both.
17088 -----------------------------
17089 -- Check_Duplicate_Aspects --
17090 -----------------------------
17092 procedure Check_Duplicate_Aspects is
17093 function Get_Partial_View_Aspect (Asp : Node_Id) return Node_Id;
17094 -- Return the corresponding aspect of the partial view which matches
17095 -- the aspect id of Asp. Return Empty is no such aspect exists.
17097 -----------------------------
17098 -- Get_Partial_View_Aspect --
17099 -----------------------------
17101 function Get_Partial_View_Aspect (Asp : Node_Id) return Node_Id is
17102 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp);
17103 Prev_Asps : constant List_Id := Aspect_Specifications (Prev_Par);
17104 Prev_Asp : Node_Id;
17106 begin
17107 if Present (Prev_Asps) then
17108 Prev_Asp := First (Prev_Asps);
17109 while Present (Prev_Asp) loop
17110 if Get_Aspect_Id (Prev_Asp) = Asp_Id then
17111 return Prev_Asp;
17112 end if;
17114 Next (Prev_Asp);
17115 end loop;
17116 end if;
17118 return Empty;
17119 end Get_Partial_View_Aspect;
17121 -- Local variables
17123 Full_Asps : constant List_Id := Aspect_Specifications (N);
17124 Full_Asp : Node_Id;
17125 Part_Asp : Node_Id;
17127 -- Start of processing for Check_Duplicate_Aspects
17129 begin
17130 if Present (Full_Asps) then
17131 Full_Asp := First (Full_Asps);
17132 while Present (Full_Asp) loop
17133 Part_Asp := Get_Partial_View_Aspect (Full_Asp);
17135 -- An aspect and its class-wide counterpart are two distinct
17136 -- aspects and may apply to both views of an entity.
17138 if Present (Part_Asp)
17139 and then Class_Present (Part_Asp) = Class_Present (Full_Asp)
17140 then
17141 Error_Msg_N
17142 ("aspect already specified in private declaration",
17143 Full_Asp);
17145 Remove (Full_Asp);
17146 return;
17147 end if;
17149 if Has_Discriminants (Prev)
17150 and then not Has_Unknown_Discriminants (Prev)
17151 and then Get_Aspect_Id (Full_Asp) =
17152 Aspect_Implicit_Dereference
17153 then
17154 Error_Msg_N
17155 ("cannot specify aspect if partial view has known "
17156 & "discriminants", Full_Asp);
17157 end if;
17159 Next (Full_Asp);
17160 end loop;
17161 end if;
17162 end Check_Duplicate_Aspects;
17164 ------------------
17165 -- Tag_Mismatch --
17166 ------------------
17168 procedure Tag_Mismatch is
17169 begin
17170 if Sloc (Prev) < Sloc (Id) then
17171 if Ada_Version >= Ada_2012
17172 and then Nkind (N) = N_Private_Type_Declaration
17173 then
17174 Error_Msg_NE
17175 ("declaration of private } must be a tagged type ", Id, Prev);
17176 else
17177 Error_Msg_NE
17178 ("full declaration of } must be a tagged type ", Id, Prev);
17179 end if;
17181 else
17182 if Ada_Version >= Ada_2012
17183 and then Nkind (N) = N_Private_Type_Declaration
17184 then
17185 Error_Msg_NE
17186 ("declaration of private } must be a tagged type ", Prev, Id);
17187 else
17188 Error_Msg_NE
17189 ("full declaration of } must be a tagged type ", Prev, Id);
17190 end if;
17191 end if;
17192 end Tag_Mismatch;
17194 -- Start of processing for Find_Type_Name
17196 begin
17197 -- Find incomplete declaration, if one was given
17199 Prev := Current_Entity_In_Scope (Id);
17201 -- New type declaration
17203 if No (Prev) then
17204 Enter_Name (Id);
17205 return Id;
17207 -- Previous declaration exists
17209 else
17210 Prev_Par := Parent (Prev);
17212 -- Error if not incomplete/private case except if previous
17213 -- declaration is implicit, etc. Enter_Name will emit error if
17214 -- appropriate.
17216 if not Is_Incomplete_Or_Private_Type (Prev) then
17217 Enter_Name (Id);
17218 New_Id := Id;
17220 -- Check invalid completion of private or incomplete type
17222 elsif not Nkind_In (N, N_Full_Type_Declaration,
17223 N_Task_Type_Declaration,
17224 N_Protected_Type_Declaration)
17225 and then
17226 (Ada_Version < Ada_2012
17227 or else not Is_Incomplete_Type (Prev)
17228 or else not Nkind_In (N, N_Private_Type_Declaration,
17229 N_Private_Extension_Declaration))
17230 then
17231 -- Completion must be a full type declarations (RM 7.3(4))
17233 Error_Msg_Sloc := Sloc (Prev);
17234 Error_Msg_NE ("invalid completion of }", Id, Prev);
17236 -- Set scope of Id to avoid cascaded errors. Entity is never
17237 -- examined again, except when saving globals in generics.
17239 Set_Scope (Id, Current_Scope);
17240 New_Id := Id;
17242 -- If this is a repeated incomplete declaration, no further
17243 -- checks are possible.
17245 if Nkind (N) = N_Incomplete_Type_Declaration then
17246 return Prev;
17247 end if;
17249 -- Case of full declaration of incomplete type
17251 elsif Ekind (Prev) = E_Incomplete_Type
17252 and then (Ada_Version < Ada_2012
17253 or else No (Full_View (Prev))
17254 or else not Is_Private_Type (Full_View (Prev)))
17255 then
17256 -- Indicate that the incomplete declaration has a matching full
17257 -- declaration. The defining occurrence of the incomplete
17258 -- declaration remains the visible one, and the procedure
17259 -- Get_Full_View dereferences it whenever the type is used.
17261 if Present (Full_View (Prev)) then
17262 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
17263 end if;
17265 Set_Full_View (Prev, Id);
17266 Append_Entity (Id, Current_Scope);
17267 Set_Is_Public (Id, Is_Public (Prev));
17268 Set_Is_Internal (Id);
17269 New_Id := Prev;
17271 -- If the incomplete view is tagged, a class_wide type has been
17272 -- created already. Use it for the private type as well, in order
17273 -- to prevent multiple incompatible class-wide types that may be
17274 -- created for self-referential anonymous access components.
17276 if Is_Tagged_Type (Prev)
17277 and then Present (Class_Wide_Type (Prev))
17278 then
17279 Set_Ekind (Id, Ekind (Prev)); -- will be reset later
17280 Set_Class_Wide_Type (Id, Class_Wide_Type (Prev));
17282 -- Type of the class-wide type is the current Id. Previously
17283 -- this was not done for private declarations because of order-
17284 -- of-elaboration issues in the back end, but gigi now handles
17285 -- this properly.
17287 Set_Etype (Class_Wide_Type (Id), Id);
17288 end if;
17290 -- Case of full declaration of private type
17292 else
17293 -- If the private type was a completion of an incomplete type then
17294 -- update Prev to reference the private type
17296 if Ada_Version >= Ada_2012
17297 and then Ekind (Prev) = E_Incomplete_Type
17298 and then Present (Full_View (Prev))
17299 and then Is_Private_Type (Full_View (Prev))
17300 then
17301 Prev := Full_View (Prev);
17302 Prev_Par := Parent (Prev);
17303 end if;
17305 if Nkind (N) = N_Full_Type_Declaration
17306 and then Nkind_In
17307 (Type_Definition (N), N_Record_Definition,
17308 N_Derived_Type_Definition)
17309 and then Interface_Present (Type_Definition (N))
17310 then
17311 Error_Msg_N
17312 ("completion of private type cannot be an interface", N);
17313 end if;
17315 if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
17316 if Etype (Prev) /= Prev then
17318 -- Prev is a private subtype or a derived type, and needs
17319 -- no completion.
17321 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
17322 New_Id := Id;
17324 elsif Ekind (Prev) = E_Private_Type
17325 and then Nkind_In (N, N_Task_Type_Declaration,
17326 N_Protected_Type_Declaration)
17327 then
17328 Error_Msg_N
17329 ("completion of nonlimited type cannot be limited", N);
17331 elsif Ekind (Prev) = E_Record_Type_With_Private
17332 and then Nkind_In (N, N_Task_Type_Declaration,
17333 N_Protected_Type_Declaration)
17334 then
17335 if not Is_Limited_Record (Prev) then
17336 Error_Msg_N
17337 ("completion of nonlimited type cannot be limited", N);
17339 elsif No (Interface_List (N)) then
17340 Error_Msg_N
17341 ("completion of tagged private type must be tagged",
17343 end if;
17344 end if;
17346 -- Ada 2005 (AI-251): Private extension declaration of a task
17347 -- type or a protected type. This case arises when covering
17348 -- interface types.
17350 elsif Nkind_In (N, N_Task_Type_Declaration,
17351 N_Protected_Type_Declaration)
17352 then
17353 null;
17355 elsif Nkind (N) /= N_Full_Type_Declaration
17356 or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
17357 then
17358 Error_Msg_N
17359 ("full view of private extension must be an extension", N);
17361 elsif not (Abstract_Present (Parent (Prev)))
17362 and then Abstract_Present (Type_Definition (N))
17363 then
17364 Error_Msg_N
17365 ("full view of non-abstract extension cannot be abstract", N);
17366 end if;
17368 if not In_Private_Part (Current_Scope) then
17369 Error_Msg_N
17370 ("declaration of full view must appear in private part", N);
17371 end if;
17373 if Ada_Version >= Ada_2012 then
17374 Check_Duplicate_Aspects;
17375 end if;
17377 Copy_And_Swap (Prev, Id);
17378 Set_Has_Private_Declaration (Prev);
17379 Set_Has_Private_Declaration (Id);
17381 -- AI12-0133: Indicate whether we have a partial view with
17382 -- unknown discriminants, in which case initialization of objects
17383 -- of the type do not receive an invariant check.
17385 Set_Partial_View_Has_Unknown_Discr
17386 (Prev, Has_Unknown_Discriminants (Id));
17388 -- Preserve aspect and iterator flags that may have been set on
17389 -- the partial view.
17391 Set_Has_Delayed_Aspects (Prev, Has_Delayed_Aspects (Id));
17392 Set_Has_Implicit_Dereference (Prev, Has_Implicit_Dereference (Id));
17394 -- If no error, propagate freeze_node from private to full view.
17395 -- It may have been generated for an early operational item.
17397 if Present (Freeze_Node (Id))
17398 and then Serious_Errors_Detected = 0
17399 and then No (Full_View (Id))
17400 then
17401 Set_Freeze_Node (Prev, Freeze_Node (Id));
17402 Set_Freeze_Node (Id, Empty);
17403 Set_First_Rep_Item (Prev, First_Rep_Item (Id));
17404 end if;
17406 Set_Full_View (Id, Prev);
17407 New_Id := Prev;
17408 end if;
17410 -- Verify that full declaration conforms to partial one
17412 if Is_Incomplete_Or_Private_Type (Prev)
17413 and then Present (Discriminant_Specifications (Prev_Par))
17414 then
17415 if Present (Discriminant_Specifications (N)) then
17416 if Ekind (Prev) = E_Incomplete_Type then
17417 Check_Discriminant_Conformance (N, Prev, Prev);
17418 else
17419 Check_Discriminant_Conformance (N, Prev, Id);
17420 end if;
17422 else
17423 Error_Msg_N
17424 ("missing discriminants in full type declaration", N);
17426 -- To avoid cascaded errors on subsequent use, share the
17427 -- discriminants of the partial view.
17429 Set_Discriminant_Specifications (N,
17430 Discriminant_Specifications (Prev_Par));
17431 end if;
17432 end if;
17434 -- A prior untagged partial view can have an associated class-wide
17435 -- type due to use of the class attribute, and in this case the full
17436 -- type must also be tagged. This Ada 95 usage is deprecated in favor
17437 -- of incomplete tagged declarations, but we check for it.
17439 if Is_Type (Prev)
17440 and then (Is_Tagged_Type (Prev)
17441 or else Present (Class_Wide_Type (Prev)))
17442 then
17443 -- Ada 2012 (AI05-0162): A private type may be the completion of
17444 -- an incomplete type.
17446 if Ada_Version >= Ada_2012
17447 and then Is_Incomplete_Type (Prev)
17448 and then Nkind_In (N, N_Private_Type_Declaration,
17449 N_Private_Extension_Declaration)
17450 then
17451 -- No need to check private extensions since they are tagged
17453 if Nkind (N) = N_Private_Type_Declaration
17454 and then not Tagged_Present (N)
17455 then
17456 Tag_Mismatch;
17457 end if;
17459 -- The full declaration is either a tagged type (including
17460 -- a synchronized type that implements interfaces) or a
17461 -- type extension, otherwise this is an error.
17463 elsif Nkind_In (N, N_Task_Type_Declaration,
17464 N_Protected_Type_Declaration)
17465 then
17466 if No (Interface_List (N)) and then not Error_Posted (N) then
17467 Tag_Mismatch;
17468 end if;
17470 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
17472 -- Indicate that the previous declaration (tagged incomplete
17473 -- or private declaration) requires the same on the full one.
17475 if not Tagged_Present (Type_Definition (N)) then
17476 Tag_Mismatch;
17477 Set_Is_Tagged_Type (Id);
17478 end if;
17480 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
17481 if No (Record_Extension_Part (Type_Definition (N))) then
17482 Error_Msg_NE
17483 ("full declaration of } must be a record extension",
17484 Prev, Id);
17486 -- Set some attributes to produce a usable full view
17488 Set_Is_Tagged_Type (Id);
17489 end if;
17491 else
17492 Tag_Mismatch;
17493 end if;
17494 end if;
17496 if Present (Prev)
17497 and then Nkind (Parent (Prev)) = N_Incomplete_Type_Declaration
17498 and then Present (Premature_Use (Parent (Prev)))
17499 then
17500 Error_Msg_Sloc := Sloc (N);
17501 Error_Msg_N
17502 ("\full declaration #", Premature_Use (Parent (Prev)));
17503 end if;
17505 return New_Id;
17506 end if;
17507 end Find_Type_Name;
17509 -------------------------
17510 -- Find_Type_Of_Object --
17511 -------------------------
17513 function Find_Type_Of_Object
17514 (Obj_Def : Node_Id;
17515 Related_Nod : Node_Id) return Entity_Id
17517 Def_Kind : constant Node_Kind := Nkind (Obj_Def);
17518 P : Node_Id := Parent (Obj_Def);
17519 T : Entity_Id;
17520 Nam : Name_Id;
17522 begin
17523 -- If the parent is a component_definition node we climb to the
17524 -- component_declaration node
17526 if Nkind (P) = N_Component_Definition then
17527 P := Parent (P);
17528 end if;
17530 -- Case of an anonymous array subtype
17532 if Nkind_In (Def_Kind, N_Constrained_Array_Definition,
17533 N_Unconstrained_Array_Definition)
17534 then
17535 T := Empty;
17536 Array_Type_Declaration (T, Obj_Def);
17538 -- Create an explicit subtype whenever possible
17540 elsif Nkind (P) /= N_Component_Declaration
17541 and then Def_Kind = N_Subtype_Indication
17542 then
17543 -- Base name of subtype on object name, which will be unique in
17544 -- the current scope.
17546 -- If this is a duplicate declaration, return base type, to avoid
17547 -- generating duplicate anonymous types.
17549 if Error_Posted (P) then
17550 Analyze (Subtype_Mark (Obj_Def));
17551 return Entity (Subtype_Mark (Obj_Def));
17552 end if;
17554 Nam :=
17555 New_External_Name
17556 (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
17558 T := Make_Defining_Identifier (Sloc (P), Nam);
17560 Insert_Action (Obj_Def,
17561 Make_Subtype_Declaration (Sloc (P),
17562 Defining_Identifier => T,
17563 Subtype_Indication => Relocate_Node (Obj_Def)));
17565 -- This subtype may need freezing, and this will not be done
17566 -- automatically if the object declaration is not in declarative
17567 -- part. Since this is an object declaration, the type cannot always
17568 -- be frozen here. Deferred constants do not freeze their type
17569 -- (which often enough will be private).
17571 if Nkind (P) = N_Object_Declaration
17572 and then Constant_Present (P)
17573 and then No (Expression (P))
17574 then
17575 null;
17577 -- Here we freeze the base type of object type to catch premature use
17578 -- of discriminated private type without a full view.
17580 else
17581 Insert_Actions (Obj_Def, Freeze_Entity (Base_Type (T), P));
17582 end if;
17584 -- Ada 2005 AI-406: the object definition in an object declaration
17585 -- can be an access definition.
17587 elsif Def_Kind = N_Access_Definition then
17588 T := Access_Definition (Related_Nod, Obj_Def);
17590 Set_Is_Local_Anonymous_Access
17592 V => (Ada_Version < Ada_2012)
17593 or else (Nkind (P) /= N_Object_Declaration)
17594 or else Is_Library_Level_Entity (Defining_Identifier (P)));
17596 -- Otherwise, the object definition is just a subtype_mark
17598 else
17599 T := Process_Subtype (Obj_Def, Related_Nod);
17601 -- If expansion is disabled an object definition that is an aggregate
17602 -- will not get expanded and may lead to scoping problems in the back
17603 -- end, if the object is referenced in an inner scope. In that case
17604 -- create an itype reference for the object definition now. This
17605 -- may be redundant in some cases, but harmless.
17607 if Is_Itype (T)
17608 and then Nkind (Related_Nod) = N_Object_Declaration
17609 and then ASIS_Mode
17610 then
17611 Build_Itype_Reference (T, Related_Nod);
17612 end if;
17613 end if;
17615 return T;
17616 end Find_Type_Of_Object;
17618 --------------------------------
17619 -- Find_Type_Of_Subtype_Indic --
17620 --------------------------------
17622 function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
17623 Typ : Entity_Id;
17625 begin
17626 -- Case of subtype mark with a constraint
17628 if Nkind (S) = N_Subtype_Indication then
17629 Find_Type (Subtype_Mark (S));
17630 Typ := Entity (Subtype_Mark (S));
17632 if not
17633 Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
17634 then
17635 Error_Msg_N
17636 ("incorrect constraint for this kind of type", Constraint (S));
17637 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
17638 end if;
17640 -- Otherwise we have a subtype mark without a constraint
17642 elsif Error_Posted (S) then
17643 Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
17644 return Any_Type;
17646 else
17647 Find_Type (S);
17648 Typ := Entity (S);
17649 end if;
17651 -- Check No_Wide_Characters restriction
17653 Check_Wide_Character_Restriction (Typ, S);
17655 return Typ;
17656 end Find_Type_Of_Subtype_Indic;
17658 -------------------------------------
17659 -- Floating_Point_Type_Declaration --
17660 -------------------------------------
17662 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
17663 Digs : constant Node_Id := Digits_Expression (Def);
17664 Max_Digs_Val : constant Uint := Digits_Value (Standard_Long_Long_Float);
17665 Digs_Val : Uint;
17666 Base_Typ : Entity_Id;
17667 Implicit_Base : Entity_Id;
17668 Bound : Node_Id;
17670 function Can_Derive_From (E : Entity_Id) return Boolean;
17671 -- Find if given digits value, and possibly a specified range, allows
17672 -- derivation from specified type
17674 function Find_Base_Type return Entity_Id;
17675 -- Find a predefined base type that Def can derive from, or generate
17676 -- an error and substitute Long_Long_Float if none exists.
17678 ---------------------
17679 -- Can_Derive_From --
17680 ---------------------
17682 function Can_Derive_From (E : Entity_Id) return Boolean is
17683 Spec : constant Entity_Id := Real_Range_Specification (Def);
17685 begin
17686 -- Check specified "digits" constraint
17688 if Digs_Val > Digits_Value (E) then
17689 return False;
17690 end if;
17692 -- Check for matching range, if specified
17694 if Present (Spec) then
17695 if Expr_Value_R (Type_Low_Bound (E)) >
17696 Expr_Value_R (Low_Bound (Spec))
17697 then
17698 return False;
17699 end if;
17701 if Expr_Value_R (Type_High_Bound (E)) <
17702 Expr_Value_R (High_Bound (Spec))
17703 then
17704 return False;
17705 end if;
17706 end if;
17708 return True;
17709 end Can_Derive_From;
17711 --------------------
17712 -- Find_Base_Type --
17713 --------------------
17715 function Find_Base_Type return Entity_Id is
17716 Choice : Elmt_Id := First_Elmt (Predefined_Float_Types);
17718 begin
17719 -- Iterate over the predefined types in order, returning the first
17720 -- one that Def can derive from.
17722 while Present (Choice) loop
17723 if Can_Derive_From (Node (Choice)) then
17724 return Node (Choice);
17725 end if;
17727 Next_Elmt (Choice);
17728 end loop;
17730 -- If we can't derive from any existing type, use Long_Long_Float
17731 -- and give appropriate message explaining the problem.
17733 if Digs_Val > Max_Digs_Val then
17734 -- It might be the case that there is a type with the requested
17735 -- range, just not the combination of digits and range.
17737 Error_Msg_N
17738 ("no predefined type has requested range and precision",
17739 Real_Range_Specification (Def));
17741 else
17742 Error_Msg_N
17743 ("range too large for any predefined type",
17744 Real_Range_Specification (Def));
17745 end if;
17747 return Standard_Long_Long_Float;
17748 end Find_Base_Type;
17750 -- Start of processing for Floating_Point_Type_Declaration
17752 begin
17753 Check_Restriction (No_Floating_Point, Def);
17755 -- Create an implicit base type
17757 Implicit_Base :=
17758 Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
17760 -- Analyze and verify digits value
17762 Analyze_And_Resolve (Digs, Any_Integer);
17763 Check_Digits_Expression (Digs);
17764 Digs_Val := Expr_Value (Digs);
17766 -- Process possible range spec and find correct type to derive from
17768 Process_Real_Range_Specification (Def);
17770 -- Check that requested number of digits is not too high.
17772 if Digs_Val > Max_Digs_Val then
17774 -- The check for Max_Base_Digits may be somewhat expensive, as it
17775 -- requires reading System, so only do it when necessary.
17777 declare
17778 Max_Base_Digits : constant Uint :=
17779 Expr_Value
17780 (Expression
17781 (Parent (RTE (RE_Max_Base_Digits))));
17783 begin
17784 if Digs_Val > Max_Base_Digits then
17785 Error_Msg_Uint_1 := Max_Base_Digits;
17786 Error_Msg_N ("digits value out of range, maximum is ^", Digs);
17788 elsif No (Real_Range_Specification (Def)) then
17789 Error_Msg_Uint_1 := Max_Digs_Val;
17790 Error_Msg_N ("types with more than ^ digits need range spec "
17791 & "(RM 3.5.7(6))", Digs);
17792 end if;
17793 end;
17794 end if;
17796 -- Find a suitable type to derive from or complain and use a substitute
17798 Base_Typ := Find_Base_Type;
17800 -- If there are bounds given in the declaration use them as the bounds
17801 -- of the type, otherwise use the bounds of the predefined base type
17802 -- that was chosen based on the Digits value.
17804 if Present (Real_Range_Specification (Def)) then
17805 Set_Scalar_Range (T, Real_Range_Specification (Def));
17806 Set_Is_Constrained (T);
17808 -- The bounds of this range must be converted to machine numbers
17809 -- in accordance with RM 4.9(38).
17811 Bound := Type_Low_Bound (T);
17813 if Nkind (Bound) = N_Real_Literal then
17814 Set_Realval
17815 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
17816 Set_Is_Machine_Number (Bound);
17817 end if;
17819 Bound := Type_High_Bound (T);
17821 if Nkind (Bound) = N_Real_Literal then
17822 Set_Realval
17823 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
17824 Set_Is_Machine_Number (Bound);
17825 end if;
17827 else
17828 Set_Scalar_Range (T, Scalar_Range (Base_Typ));
17829 end if;
17831 -- Complete definition of implicit base and declared first subtype. The
17832 -- inheritance of the rep item chain ensures that SPARK-related pragmas
17833 -- are not clobbered when the floating point type acts as a full view of
17834 -- a private type.
17836 Set_Etype (Implicit_Base, Base_Typ);
17837 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
17838 Set_Size_Info (Implicit_Base, Base_Typ);
17839 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
17840 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
17841 Set_Digits_Value (Implicit_Base, Digits_Value (Base_Typ));
17842 Set_Float_Rep (Implicit_Base, Float_Rep (Base_Typ));
17844 Set_Ekind (T, E_Floating_Point_Subtype);
17845 Set_Etype (T, Implicit_Base);
17846 Set_Size_Info (T, Implicit_Base);
17847 Set_RM_Size (T, RM_Size (Implicit_Base));
17848 Inherit_Rep_Item_Chain (T, Implicit_Base);
17849 Set_Digits_Value (T, Digs_Val);
17850 end Floating_Point_Type_Declaration;
17852 ----------------------------
17853 -- Get_Discriminant_Value --
17854 ----------------------------
17856 -- This is the situation:
17858 -- There is a non-derived type
17860 -- type T0 (Dx, Dy, Dz...)
17862 -- There are zero or more levels of derivation, with each derivation
17863 -- either purely inheriting the discriminants, or defining its own.
17865 -- type Ti is new Ti-1
17866 -- or
17867 -- type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
17868 -- or
17869 -- subtype Ti is ...
17871 -- The subtype issue is avoided by the use of Original_Record_Component,
17872 -- and the fact that derived subtypes also derive the constraints.
17874 -- This chain leads back from
17876 -- Typ_For_Constraint
17878 -- Typ_For_Constraint has discriminants, and the value for each
17879 -- discriminant is given by its corresponding Elmt of Constraints.
17881 -- Discriminant is some discriminant in this hierarchy
17883 -- We need to return its value
17885 -- We do this by recursively searching each level, and looking for
17886 -- Discriminant. Once we get to the bottom, we start backing up
17887 -- returning the value for it which may in turn be a discriminant
17888 -- further up, so on the backup we continue the substitution.
17890 function Get_Discriminant_Value
17891 (Discriminant : Entity_Id;
17892 Typ_For_Constraint : Entity_Id;
17893 Constraint : Elist_Id) return Node_Id
17895 function Root_Corresponding_Discriminant
17896 (Discr : Entity_Id) return Entity_Id;
17897 -- Given a discriminant, traverse the chain of inherited discriminants
17898 -- and return the topmost discriminant.
17900 function Search_Derivation_Levels
17901 (Ti : Entity_Id;
17902 Discrim_Values : Elist_Id;
17903 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
17904 -- This is the routine that performs the recursive search of levels
17905 -- as described above.
17907 -------------------------------------
17908 -- Root_Corresponding_Discriminant --
17909 -------------------------------------
17911 function Root_Corresponding_Discriminant
17912 (Discr : Entity_Id) return Entity_Id
17914 D : Entity_Id;
17916 begin
17917 D := Discr;
17918 while Present (Corresponding_Discriminant (D)) loop
17919 D := Corresponding_Discriminant (D);
17920 end loop;
17922 return D;
17923 end Root_Corresponding_Discriminant;
17925 ------------------------------
17926 -- Search_Derivation_Levels --
17927 ------------------------------
17929 function Search_Derivation_Levels
17930 (Ti : Entity_Id;
17931 Discrim_Values : Elist_Id;
17932 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
17934 Assoc : Elmt_Id;
17935 Disc : Entity_Id;
17936 Result : Node_Or_Entity_Id;
17937 Result_Entity : Node_Id;
17939 begin
17940 -- If inappropriate type, return Error, this happens only in
17941 -- cascaded error situations, and we want to avoid a blow up.
17943 if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
17944 return Error;
17945 end if;
17947 -- Look deeper if possible. Use Stored_Constraints only for
17948 -- untagged types. For tagged types use the given constraint.
17949 -- This asymmetry needs explanation???
17951 if not Stored_Discrim_Values
17952 and then Present (Stored_Constraint (Ti))
17953 and then not Is_Tagged_Type (Ti)
17954 then
17955 Result :=
17956 Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
17957 else
17958 declare
17959 Td : constant Entity_Id := Etype (Ti);
17961 begin
17962 if Td = Ti then
17963 Result := Discriminant;
17965 else
17966 if Present (Stored_Constraint (Ti)) then
17967 Result :=
17968 Search_Derivation_Levels
17969 (Td, Stored_Constraint (Ti), True);
17970 else
17971 Result :=
17972 Search_Derivation_Levels
17973 (Td, Discrim_Values, Stored_Discrim_Values);
17974 end if;
17975 end if;
17976 end;
17977 end if;
17979 -- Extra underlying places to search, if not found above. For
17980 -- concurrent types, the relevant discriminant appears in the
17981 -- corresponding record. For a type derived from a private type
17982 -- without discriminant, the full view inherits the discriminants
17983 -- of the full view of the parent.
17985 if Result = Discriminant then
17986 if Is_Concurrent_Type (Ti)
17987 and then Present (Corresponding_Record_Type (Ti))
17988 then
17989 Result :=
17990 Search_Derivation_Levels (
17991 Corresponding_Record_Type (Ti),
17992 Discrim_Values,
17993 Stored_Discrim_Values);
17995 elsif Is_Private_Type (Ti)
17996 and then not Has_Discriminants (Ti)
17997 and then Present (Full_View (Ti))
17998 and then Etype (Full_View (Ti)) /= Ti
17999 then
18000 Result :=
18001 Search_Derivation_Levels (
18002 Full_View (Ti),
18003 Discrim_Values,
18004 Stored_Discrim_Values);
18005 end if;
18006 end if;
18008 -- If Result is not a (reference to a) discriminant, return it,
18009 -- otherwise set Result_Entity to the discriminant.
18011 if Nkind (Result) = N_Defining_Identifier then
18012 pragma Assert (Result = Discriminant);
18013 Result_Entity := Result;
18015 else
18016 if not Denotes_Discriminant (Result) then
18017 return Result;
18018 end if;
18020 Result_Entity := Entity (Result);
18021 end if;
18023 -- See if this level of derivation actually has discriminants because
18024 -- tagged derivations can add them, hence the lower levels need not
18025 -- have any.
18027 if not Has_Discriminants (Ti) then
18028 return Result;
18029 end if;
18031 -- Scan Ti's discriminants for Result_Entity, and return its
18032 -- corresponding value, if any.
18034 Result_Entity := Original_Record_Component (Result_Entity);
18036 Assoc := First_Elmt (Discrim_Values);
18038 if Stored_Discrim_Values then
18039 Disc := First_Stored_Discriminant (Ti);
18040 else
18041 Disc := First_Discriminant (Ti);
18042 end if;
18044 while Present (Disc) loop
18046 -- If no further associations return the discriminant, value will
18047 -- be found on the second pass.
18049 if No (Assoc) then
18050 return Result;
18051 end if;
18053 if Original_Record_Component (Disc) = Result_Entity then
18054 return Node (Assoc);
18055 end if;
18057 Next_Elmt (Assoc);
18059 if Stored_Discrim_Values then
18060 Next_Stored_Discriminant (Disc);
18061 else
18062 Next_Discriminant (Disc);
18063 end if;
18064 end loop;
18066 -- Could not find it
18068 return Result;
18069 end Search_Derivation_Levels;
18071 -- Local Variables
18073 Result : Node_Or_Entity_Id;
18075 -- Start of processing for Get_Discriminant_Value
18077 begin
18078 -- ??? This routine is a gigantic mess and will be deleted. For the
18079 -- time being just test for the trivial case before calling recurse.
18081 -- We are now celebrating the 20th anniversary of this comment!
18083 if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
18084 declare
18085 D : Entity_Id;
18086 E : Elmt_Id;
18088 begin
18089 D := First_Discriminant (Typ_For_Constraint);
18090 E := First_Elmt (Constraint);
18091 while Present (D) loop
18092 if Chars (D) = Chars (Discriminant) then
18093 return Node (E);
18094 end if;
18096 Next_Discriminant (D);
18097 Next_Elmt (E);
18098 end loop;
18099 end;
18100 end if;
18102 Result := Search_Derivation_Levels
18103 (Typ_For_Constraint, Constraint, False);
18105 -- ??? hack to disappear when this routine is gone
18107 if Nkind (Result) = N_Defining_Identifier then
18108 declare
18109 D : Entity_Id;
18110 E : Elmt_Id;
18112 begin
18113 D := First_Discriminant (Typ_For_Constraint);
18114 E := First_Elmt (Constraint);
18115 while Present (D) loop
18116 if Root_Corresponding_Discriminant (D) = Discriminant then
18117 return Node (E);
18118 end if;
18120 Next_Discriminant (D);
18121 Next_Elmt (E);
18122 end loop;
18123 end;
18124 end if;
18126 pragma Assert (Nkind (Result) /= N_Defining_Identifier);
18127 return Result;
18128 end Get_Discriminant_Value;
18130 --------------------------
18131 -- Has_Range_Constraint --
18132 --------------------------
18134 function Has_Range_Constraint (N : Node_Id) return Boolean is
18135 C : constant Node_Id := Constraint (N);
18137 begin
18138 if Nkind (C) = N_Range_Constraint then
18139 return True;
18141 elsif Nkind (C) = N_Digits_Constraint then
18142 return
18143 Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
18144 or else Present (Range_Constraint (C));
18146 elsif Nkind (C) = N_Delta_Constraint then
18147 return Present (Range_Constraint (C));
18149 else
18150 return False;
18151 end if;
18152 end Has_Range_Constraint;
18154 ------------------------
18155 -- Inherit_Components --
18156 ------------------------
18158 function Inherit_Components
18159 (N : Node_Id;
18160 Parent_Base : Entity_Id;
18161 Derived_Base : Entity_Id;
18162 Is_Tagged : Boolean;
18163 Inherit_Discr : Boolean;
18164 Discs : Elist_Id) return Elist_Id
18166 Assoc_List : constant Elist_Id := New_Elmt_List;
18168 procedure Inherit_Component
18169 (Old_C : Entity_Id;
18170 Plain_Discrim : Boolean := False;
18171 Stored_Discrim : Boolean := False);
18172 -- Inherits component Old_C from Parent_Base to the Derived_Base. If
18173 -- Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
18174 -- True, Old_C is a stored discriminant. If they are both false then
18175 -- Old_C is a regular component.
18177 -----------------------
18178 -- Inherit_Component --
18179 -----------------------
18181 procedure Inherit_Component
18182 (Old_C : Entity_Id;
18183 Plain_Discrim : Boolean := False;
18184 Stored_Discrim : Boolean := False)
18186 procedure Set_Anonymous_Type (Id : Entity_Id);
18187 -- Id denotes the entity of an access discriminant or anonymous
18188 -- access component. Set the type of Id to either the same type of
18189 -- Old_C or create a new one depending on whether the parent and
18190 -- the child types are in the same scope.
18192 ------------------------
18193 -- Set_Anonymous_Type --
18194 ------------------------
18196 procedure Set_Anonymous_Type (Id : Entity_Id) is
18197 Old_Typ : constant Entity_Id := Etype (Old_C);
18199 begin
18200 if Scope (Parent_Base) = Scope (Derived_Base) then
18201 Set_Etype (Id, Old_Typ);
18203 -- The parent and the derived type are in two different scopes.
18204 -- Reuse the type of the original discriminant / component by
18205 -- copying it in order to preserve all attributes.
18207 else
18208 declare
18209 Typ : constant Entity_Id := New_Copy (Old_Typ);
18211 begin
18212 Set_Etype (Id, Typ);
18214 -- Since we do not generate component declarations for
18215 -- inherited components, associate the itype with the
18216 -- derived type.
18218 Set_Associated_Node_For_Itype (Typ, Parent (Derived_Base));
18219 Set_Scope (Typ, Derived_Base);
18220 end;
18221 end if;
18222 end Set_Anonymous_Type;
18224 -- Local variables and constants
18226 New_C : constant Entity_Id := New_Copy (Old_C);
18228 Corr_Discrim : Entity_Id;
18229 Discrim : Entity_Id;
18231 -- Start of processing for Inherit_Component
18233 begin
18234 pragma Assert (not Is_Tagged or not Stored_Discrim);
18236 Set_Parent (New_C, Parent (Old_C));
18238 -- Regular discriminants and components must be inserted in the scope
18239 -- of the Derived_Base. Do it here.
18241 if not Stored_Discrim then
18242 Enter_Name (New_C);
18243 end if;
18245 -- For tagged types the Original_Record_Component must point to
18246 -- whatever this field was pointing to in the parent type. This has
18247 -- already been achieved by the call to New_Copy above.
18249 if not Is_Tagged then
18250 Set_Original_Record_Component (New_C, New_C);
18251 Set_Corresponding_Record_Component (New_C, Old_C);
18252 end if;
18254 -- Set the proper type of an access discriminant
18256 if Ekind (New_C) = E_Discriminant
18257 and then Ekind (Etype (New_C)) = E_Anonymous_Access_Type
18258 then
18259 Set_Anonymous_Type (New_C);
18260 end if;
18262 -- If we have inherited a component then see if its Etype contains
18263 -- references to Parent_Base discriminants. In this case, replace
18264 -- these references with the constraints given in Discs. We do not
18265 -- do this for the partial view of private types because this is
18266 -- not needed (only the components of the full view will be used
18267 -- for code generation) and cause problem. We also avoid this
18268 -- transformation in some error situations.
18270 if Ekind (New_C) = E_Component then
18272 -- Set the proper type of an anonymous access component
18274 if Ekind (Etype (New_C)) = E_Anonymous_Access_Type then
18275 Set_Anonymous_Type (New_C);
18277 elsif (Is_Private_Type (Derived_Base)
18278 and then not Is_Generic_Type (Derived_Base))
18279 or else (Is_Empty_Elmt_List (Discs)
18280 and then not Expander_Active)
18281 then
18282 Set_Etype (New_C, Etype (Old_C));
18284 else
18285 -- The current component introduces a circularity of the
18286 -- following kind:
18288 -- limited with Pack_2;
18289 -- package Pack_1 is
18290 -- type T_1 is tagged record
18291 -- Comp : access Pack_2.T_2;
18292 -- ...
18293 -- end record;
18294 -- end Pack_1;
18296 -- with Pack_1;
18297 -- package Pack_2 is
18298 -- type T_2 is new Pack_1.T_1 with ...;
18299 -- end Pack_2;
18301 Set_Etype
18302 (New_C,
18303 Constrain_Component_Type
18304 (Old_C, Derived_Base, N, Parent_Base, Discs));
18305 end if;
18306 end if;
18308 -- In derived tagged types it is illegal to reference a non
18309 -- discriminant component in the parent type. To catch this, mark
18310 -- these components with an Ekind of E_Void. This will be reset in
18311 -- Record_Type_Definition after processing the record extension of
18312 -- the derived type.
18314 -- If the declaration is a private extension, there is no further
18315 -- record extension to process, and the components retain their
18316 -- current kind, because they are visible at this point.
18318 if Is_Tagged and then Ekind (New_C) = E_Component
18319 and then Nkind (N) /= N_Private_Extension_Declaration
18320 then
18321 Set_Ekind (New_C, E_Void);
18322 end if;
18324 if Plain_Discrim then
18325 Set_Corresponding_Discriminant (New_C, Old_C);
18326 Build_Discriminal (New_C);
18328 -- If we are explicitly inheriting a stored discriminant it will be
18329 -- completely hidden.
18331 elsif Stored_Discrim then
18332 Set_Corresponding_Discriminant (New_C, Empty);
18333 Set_Discriminal (New_C, Empty);
18334 Set_Is_Completely_Hidden (New_C);
18336 -- Set the Original_Record_Component of each discriminant in the
18337 -- derived base to point to the corresponding stored that we just
18338 -- created.
18340 Discrim := First_Discriminant (Derived_Base);
18341 while Present (Discrim) loop
18342 Corr_Discrim := Corresponding_Discriminant (Discrim);
18344 -- Corr_Discrim could be missing in an error situation
18346 if Present (Corr_Discrim)
18347 and then Original_Record_Component (Corr_Discrim) = Old_C
18348 then
18349 Set_Original_Record_Component (Discrim, New_C);
18350 Set_Corresponding_Record_Component (Discrim, Empty);
18351 end if;
18353 Next_Discriminant (Discrim);
18354 end loop;
18356 Append_Entity (New_C, Derived_Base);
18357 end if;
18359 if not Is_Tagged then
18360 Append_Elmt (Old_C, Assoc_List);
18361 Append_Elmt (New_C, Assoc_List);
18362 end if;
18363 end Inherit_Component;
18365 -- Variables local to Inherit_Component
18367 Loc : constant Source_Ptr := Sloc (N);
18369 Parent_Discrim : Entity_Id;
18370 Stored_Discrim : Entity_Id;
18371 D : Entity_Id;
18372 Component : Entity_Id;
18374 -- Start of processing for Inherit_Components
18376 begin
18377 if not Is_Tagged then
18378 Append_Elmt (Parent_Base, Assoc_List);
18379 Append_Elmt (Derived_Base, Assoc_List);
18380 end if;
18382 -- Inherit parent discriminants if needed
18384 if Inherit_Discr then
18385 Parent_Discrim := First_Discriminant (Parent_Base);
18386 while Present (Parent_Discrim) loop
18387 Inherit_Component (Parent_Discrim, Plain_Discrim => True);
18388 Next_Discriminant (Parent_Discrim);
18389 end loop;
18390 end if;
18392 -- Create explicit stored discrims for untagged types when necessary
18394 if not Has_Unknown_Discriminants (Derived_Base)
18395 and then Has_Discriminants (Parent_Base)
18396 and then not Is_Tagged
18397 and then
18398 (not Inherit_Discr
18399 or else First_Discriminant (Parent_Base) /=
18400 First_Stored_Discriminant (Parent_Base))
18401 then
18402 Stored_Discrim := First_Stored_Discriminant (Parent_Base);
18403 while Present (Stored_Discrim) loop
18404 Inherit_Component (Stored_Discrim, Stored_Discrim => True);
18405 Next_Stored_Discriminant (Stored_Discrim);
18406 end loop;
18407 end if;
18409 -- See if we can apply the second transformation for derived types, as
18410 -- explained in point 6. in the comments above Build_Derived_Record_Type
18411 -- This is achieved by appending Derived_Base discriminants into Discs,
18412 -- which has the side effect of returning a non empty Discs list to the
18413 -- caller of Inherit_Components, which is what we want. This must be
18414 -- done for private derived types if there are explicit stored
18415 -- discriminants, to ensure that we can retrieve the values of the
18416 -- constraints provided in the ancestors.
18418 if Inherit_Discr
18419 and then Is_Empty_Elmt_List (Discs)
18420 and then Present (First_Discriminant (Derived_Base))
18421 and then
18422 (not Is_Private_Type (Derived_Base)
18423 or else Is_Completely_Hidden
18424 (First_Stored_Discriminant (Derived_Base))
18425 or else Is_Generic_Type (Derived_Base))
18426 then
18427 D := First_Discriminant (Derived_Base);
18428 while Present (D) loop
18429 Append_Elmt (New_Occurrence_Of (D, Loc), Discs);
18430 Next_Discriminant (D);
18431 end loop;
18432 end if;
18434 -- Finally, inherit non-discriminant components unless they are not
18435 -- visible because defined or inherited from the full view of the
18436 -- parent. Don't inherit the _parent field of the parent type.
18438 Component := First_Entity (Parent_Base);
18439 while Present (Component) loop
18441 -- Ada 2005 (AI-251): Do not inherit components associated with
18442 -- secondary tags of the parent.
18444 if Ekind (Component) = E_Component
18445 and then Present (Related_Type (Component))
18446 then
18447 null;
18449 elsif Ekind (Component) /= E_Component
18450 or else Chars (Component) = Name_uParent
18451 then
18452 null;
18454 -- If the derived type is within the parent type's declarative
18455 -- region, then the components can still be inherited even though
18456 -- they aren't visible at this point. This can occur for cases
18457 -- such as within public child units where the components must
18458 -- become visible upon entering the child unit's private part.
18460 elsif not Is_Visible_Component (Component)
18461 and then not In_Open_Scopes (Scope (Parent_Base))
18462 then
18463 null;
18465 elsif Ekind_In (Derived_Base, E_Private_Type,
18466 E_Limited_Private_Type)
18467 then
18468 null;
18470 else
18471 Inherit_Component (Component);
18472 end if;
18474 Next_Entity (Component);
18475 end loop;
18477 -- For tagged derived types, inherited discriminants cannot be used in
18478 -- component declarations of the record extension part. To achieve this
18479 -- we mark the inherited discriminants as not visible.
18481 if Is_Tagged and then Inherit_Discr then
18482 D := First_Discriminant (Derived_Base);
18483 while Present (D) loop
18484 Set_Is_Immediately_Visible (D, False);
18485 Next_Discriminant (D);
18486 end loop;
18487 end if;
18489 return Assoc_List;
18490 end Inherit_Components;
18492 -----------------------------
18493 -- Inherit_Predicate_Flags --
18494 -----------------------------
18496 procedure Inherit_Predicate_Flags (Subt, Par : Entity_Id) is
18497 begin
18498 Set_Has_Predicates (Subt, Has_Predicates (Par));
18499 Set_Has_Static_Predicate_Aspect
18500 (Subt, Has_Static_Predicate_Aspect (Par));
18501 Set_Has_Dynamic_Predicate_Aspect
18502 (Subt, Has_Dynamic_Predicate_Aspect (Par));
18504 -- A named subtype does not inherit the predicate function of its
18505 -- parent but an itype declared for a loop index needs the discrete
18506 -- predicate information of its parent to execute the loop properly.
18508 if Is_Itype (Subt) and then Present (Predicate_Function (Par)) then
18509 Set_Subprograms_For_Type (Subt, Subprograms_For_Type (Par));
18511 if Has_Static_Predicate (Par) then
18512 Set_Static_Discrete_Predicate
18513 (Subt, Static_Discrete_Predicate (Par));
18514 end if;
18515 end if;
18516 end Inherit_Predicate_Flags;
18518 ----------------------
18519 -- Is_EVF_Procedure --
18520 ----------------------
18522 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean is
18523 Formal : Entity_Id;
18525 begin
18526 -- Examine the formals of an Extensions_Visible False procedure looking
18527 -- for a controlling OUT parameter.
18529 if Ekind (Subp) = E_Procedure
18530 and then Extensions_Visible_Status (Subp) = Extensions_Visible_False
18531 then
18532 Formal := First_Formal (Subp);
18533 while Present (Formal) loop
18534 if Ekind (Formal) = E_Out_Parameter
18535 and then Is_Controlling_Formal (Formal)
18536 then
18537 return True;
18538 end if;
18540 Next_Formal (Formal);
18541 end loop;
18542 end if;
18544 return False;
18545 end Is_EVF_Procedure;
18547 -----------------------
18548 -- Is_Null_Extension --
18549 -----------------------
18551 function Is_Null_Extension (T : Entity_Id) return Boolean is
18552 Type_Decl : constant Node_Id := Parent (Base_Type (T));
18553 Comp_List : Node_Id;
18554 Comp : Node_Id;
18556 begin
18557 if Nkind (Type_Decl) /= N_Full_Type_Declaration
18558 or else not Is_Tagged_Type (T)
18559 or else Nkind (Type_Definition (Type_Decl)) /=
18560 N_Derived_Type_Definition
18561 or else No (Record_Extension_Part (Type_Definition (Type_Decl)))
18562 then
18563 return False;
18564 end if;
18566 Comp_List :=
18567 Component_List (Record_Extension_Part (Type_Definition (Type_Decl)));
18569 if Present (Discriminant_Specifications (Type_Decl)) then
18570 return False;
18572 elsif Present (Comp_List)
18573 and then Is_Non_Empty_List (Component_Items (Comp_List))
18574 then
18575 Comp := First (Component_Items (Comp_List));
18577 -- Only user-defined components are relevant. The component list
18578 -- may also contain a parent component and internal components
18579 -- corresponding to secondary tags, but these do not determine
18580 -- whether this is a null extension.
18582 while Present (Comp) loop
18583 if Comes_From_Source (Comp) then
18584 return False;
18585 end if;
18587 Next (Comp);
18588 end loop;
18590 return True;
18592 else
18593 return True;
18594 end if;
18595 end Is_Null_Extension;
18597 ------------------------------
18598 -- Is_Valid_Constraint_Kind --
18599 ------------------------------
18601 function Is_Valid_Constraint_Kind
18602 (T_Kind : Type_Kind;
18603 Constraint_Kind : Node_Kind) return Boolean
18605 begin
18606 case T_Kind is
18607 when Enumeration_Kind
18608 | Integer_Kind
18610 return Constraint_Kind = N_Range_Constraint;
18612 when Decimal_Fixed_Point_Kind =>
18613 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
18614 N_Range_Constraint);
18616 when Ordinary_Fixed_Point_Kind =>
18617 return Nkind_In (Constraint_Kind, N_Delta_Constraint,
18618 N_Range_Constraint);
18620 when Float_Kind =>
18621 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
18622 N_Range_Constraint);
18624 when Access_Kind
18625 | Array_Kind
18626 | Class_Wide_Kind
18627 | Concurrent_Kind
18628 | Private_Kind
18629 | E_Incomplete_Type
18630 | E_Record_Subtype
18631 | E_Record_Type
18633 return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
18635 when others =>
18636 return True; -- Error will be detected later
18637 end case;
18638 end Is_Valid_Constraint_Kind;
18640 --------------------------
18641 -- Is_Visible_Component --
18642 --------------------------
18644 function Is_Visible_Component
18645 (C : Entity_Id;
18646 N : Node_Id := Empty) return Boolean
18648 Original_Comp : Entity_Id := Empty;
18649 Original_Type : Entity_Id;
18650 Type_Scope : Entity_Id;
18652 function Is_Local_Type (Typ : Entity_Id) return Boolean;
18653 -- Check whether parent type of inherited component is declared locally,
18654 -- possibly within a nested package or instance. The current scope is
18655 -- the derived record itself.
18657 -------------------
18658 -- Is_Local_Type --
18659 -------------------
18661 function Is_Local_Type (Typ : Entity_Id) return Boolean is
18662 Scop : Entity_Id;
18664 begin
18665 Scop := Scope (Typ);
18666 while Present (Scop)
18667 and then Scop /= Standard_Standard
18668 loop
18669 if Scop = Scope (Current_Scope) then
18670 return True;
18671 end if;
18673 Scop := Scope (Scop);
18674 end loop;
18676 return False;
18677 end Is_Local_Type;
18679 -- Start of processing for Is_Visible_Component
18681 begin
18682 if Ekind_In (C, E_Component, E_Discriminant) then
18683 Original_Comp := Original_Record_Component (C);
18684 end if;
18686 if No (Original_Comp) then
18688 -- Premature usage, or previous error
18690 return False;
18692 else
18693 Original_Type := Scope (Original_Comp);
18694 Type_Scope := Scope (Base_Type (Scope (C)));
18695 end if;
18697 -- This test only concerns tagged types
18699 if not Is_Tagged_Type (Original_Type) then
18700 return True;
18702 -- If it is _Parent or _Tag, there is no visibility issue
18704 elsif not Comes_From_Source (Original_Comp) then
18705 return True;
18707 -- Discriminants are visible unless the (private) type has unknown
18708 -- discriminants. If the discriminant reference is inserted for a
18709 -- discriminant check on a full view it is also visible.
18711 elsif Ekind (Original_Comp) = E_Discriminant
18712 and then
18713 (not Has_Unknown_Discriminants (Original_Type)
18714 or else (Present (N)
18715 and then Nkind (N) = N_Selected_Component
18716 and then Nkind (Prefix (N)) = N_Type_Conversion
18717 and then not Comes_From_Source (Prefix (N))))
18718 then
18719 return True;
18721 -- In the body of an instantiation, check the visibility of a component
18722 -- in case it has a homograph that is a primitive operation of a private
18723 -- type which was not visible in the generic unit.
18725 -- Should Is_Prefixed_Call be propagated from template to instance???
18727 elsif In_Instance_Body then
18728 if not Is_Tagged_Type (Original_Type)
18729 or else not Is_Private_Type (Original_Type)
18730 then
18731 return True;
18733 else
18734 declare
18735 Subp_Elmt : Elmt_Id;
18737 begin
18738 Subp_Elmt := First_Elmt (Primitive_Operations (Original_Type));
18739 while Present (Subp_Elmt) loop
18741 -- The component is hidden by a primitive operation
18743 if Chars (Node (Subp_Elmt)) = Chars (C) then
18744 return False;
18745 end if;
18747 Next_Elmt (Subp_Elmt);
18748 end loop;
18750 return True;
18751 end;
18752 end if;
18754 -- If the component has been declared in an ancestor which is currently
18755 -- a private type, then it is not visible. The same applies if the
18756 -- component's containing type is not in an open scope and the original
18757 -- component's enclosing type is a visible full view of a private type
18758 -- (which can occur in cases where an attempt is being made to reference
18759 -- a component in a sibling package that is inherited from a visible
18760 -- component of a type in an ancestor package; the component in the
18761 -- sibling package should not be visible even though the component it
18762 -- inherited from is visible). This does not apply however in the case
18763 -- where the scope of the type is a private child unit, or when the
18764 -- parent comes from a local package in which the ancestor is currently
18765 -- visible. The latter suppression of visibility is needed for cases
18766 -- that are tested in B730006.
18768 elsif Is_Private_Type (Original_Type)
18769 or else
18770 (not Is_Private_Descendant (Type_Scope)
18771 and then not In_Open_Scopes (Type_Scope)
18772 and then Has_Private_Declaration (Original_Type))
18773 then
18774 -- If the type derives from an entity in a formal package, there
18775 -- are no additional visible components.
18777 if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
18778 N_Formal_Package_Declaration
18779 then
18780 return False;
18782 -- if we are not in the private part of the current package, there
18783 -- are no additional visible components.
18785 elsif Ekind (Scope (Current_Scope)) = E_Package
18786 and then not In_Private_Part (Scope (Current_Scope))
18787 then
18788 return False;
18789 else
18790 return
18791 Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
18792 and then In_Open_Scopes (Scope (Original_Type))
18793 and then Is_Local_Type (Type_Scope);
18794 end if;
18796 -- There is another weird way in which a component may be invisible when
18797 -- the private and the full view are not derived from the same ancestor.
18798 -- Here is an example :
18800 -- type A1 is tagged record F1 : integer; end record;
18801 -- type A2 is new A1 with record F2 : integer; end record;
18802 -- type T is new A1 with private;
18803 -- private
18804 -- type T is new A2 with null record;
18806 -- In this case, the full view of T inherits F1 and F2 but the private
18807 -- view inherits only F1
18809 else
18810 declare
18811 Ancestor : Entity_Id := Scope (C);
18813 begin
18814 loop
18815 if Ancestor = Original_Type then
18816 return True;
18818 -- The ancestor may have a partial view of the original type,
18819 -- but if the full view is in scope, as in a child body, the
18820 -- component is visible.
18822 elsif In_Private_Part (Scope (Original_Type))
18823 and then Full_View (Ancestor) = Original_Type
18824 then
18825 return True;
18827 elsif Ancestor = Etype (Ancestor) then
18829 -- No further ancestors to examine
18831 return False;
18832 end if;
18834 Ancestor := Etype (Ancestor);
18835 end loop;
18836 end;
18837 end if;
18838 end Is_Visible_Component;
18840 --------------------------
18841 -- Make_Class_Wide_Type --
18842 --------------------------
18844 procedure Make_Class_Wide_Type (T : Entity_Id) is
18845 CW_Type : Entity_Id;
18846 CW_Name : Name_Id;
18847 Next_E : Entity_Id;
18849 begin
18850 if Present (Class_Wide_Type (T)) then
18852 -- The class-wide type is a partially decorated entity created for a
18853 -- unanalyzed tagged type referenced through a limited with clause.
18854 -- When the tagged type is analyzed, its class-wide type needs to be
18855 -- redecorated. Note that we reuse the entity created by Decorate_
18856 -- Tagged_Type in order to preserve all links.
18858 if Materialize_Entity (Class_Wide_Type (T)) then
18859 CW_Type := Class_Wide_Type (T);
18860 Set_Materialize_Entity (CW_Type, False);
18862 -- The class wide type can have been defined by the partial view, in
18863 -- which case everything is already done.
18865 else
18866 return;
18867 end if;
18869 -- Default case, we need to create a new class-wide type
18871 else
18872 CW_Type :=
18873 New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
18874 end if;
18876 -- Inherit root type characteristics
18878 CW_Name := Chars (CW_Type);
18879 Next_E := Next_Entity (CW_Type);
18880 Copy_Node (T, CW_Type);
18881 Set_Comes_From_Source (CW_Type, False);
18882 Set_Chars (CW_Type, CW_Name);
18883 Set_Parent (CW_Type, Parent (T));
18884 Set_Next_Entity (CW_Type, Next_E);
18886 -- Ensure we have a new freeze node for the class-wide type. The partial
18887 -- view may have freeze action of its own, requiring a proper freeze
18888 -- node, and the same freeze node cannot be shared between the two
18889 -- types.
18891 Set_Has_Delayed_Freeze (CW_Type);
18892 Set_Freeze_Node (CW_Type, Empty);
18894 -- Customize the class-wide type: It has no prim. op., it cannot be
18895 -- abstract, its Etype points back to the specific root type, and it
18896 -- cannot have any invariants.
18898 Set_Ekind (CW_Type, E_Class_Wide_Type);
18899 Set_Is_Tagged_Type (CW_Type, True);
18900 Set_Direct_Primitive_Operations (CW_Type, New_Elmt_List);
18901 Set_Is_Abstract_Type (CW_Type, False);
18902 Set_Is_Constrained (CW_Type, False);
18903 Set_Is_First_Subtype (CW_Type, Is_First_Subtype (T));
18904 Set_Default_SSO (CW_Type);
18905 Set_Has_Inheritable_Invariants (CW_Type, False);
18906 Set_Has_Inherited_Invariants (CW_Type, False);
18907 Set_Has_Own_Invariants (CW_Type, False);
18909 if Ekind (T) = E_Class_Wide_Subtype then
18910 Set_Etype (CW_Type, Etype (Base_Type (T)));
18911 else
18912 Set_Etype (CW_Type, T);
18913 end if;
18915 Set_No_Tagged_Streams_Pragma (CW_Type, No_Tagged_Streams);
18917 -- If this is the class_wide type of a constrained subtype, it does
18918 -- not have discriminants.
18920 Set_Has_Discriminants (CW_Type,
18921 Has_Discriminants (T) and then not Is_Constrained (T));
18923 Set_Has_Unknown_Discriminants (CW_Type, True);
18924 Set_Class_Wide_Type (T, CW_Type);
18925 Set_Equivalent_Type (CW_Type, Empty);
18927 -- The class-wide type of a class-wide type is itself (RM 3.9(14))
18929 Set_Class_Wide_Type (CW_Type, CW_Type);
18930 end Make_Class_Wide_Type;
18932 ----------------
18933 -- Make_Index --
18934 ----------------
18936 procedure Make_Index
18937 (N : Node_Id;
18938 Related_Nod : Node_Id;
18939 Related_Id : Entity_Id := Empty;
18940 Suffix_Index : Nat := 1;
18941 In_Iter_Schm : Boolean := False)
18943 R : Node_Id;
18944 T : Entity_Id;
18945 Def_Id : Entity_Id := Empty;
18946 Found : Boolean := False;
18948 begin
18949 -- For a discrete range used in a constrained array definition and
18950 -- defined by a range, an implicit conversion to the predefined type
18951 -- INTEGER is assumed if each bound is either a numeric literal, a named
18952 -- number, or an attribute, and the type of both bounds (prior to the
18953 -- implicit conversion) is the type universal_integer. Otherwise, both
18954 -- bounds must be of the same discrete type, other than universal
18955 -- integer; this type must be determinable independently of the
18956 -- context, but using the fact that the type must be discrete and that
18957 -- both bounds must have the same type.
18959 -- Character literals also have a universal type in the absence of
18960 -- of additional context, and are resolved to Standard_Character.
18962 if Nkind (N) = N_Range then
18964 -- The index is given by a range constraint. The bounds are known
18965 -- to be of a consistent type.
18967 if not Is_Overloaded (N) then
18968 T := Etype (N);
18970 -- For universal bounds, choose the specific predefined type
18972 if T = Universal_Integer then
18973 T := Standard_Integer;
18975 elsif T = Any_Character then
18976 Ambiguous_Character (Low_Bound (N));
18978 T := Standard_Character;
18979 end if;
18981 -- The node may be overloaded because some user-defined operators
18982 -- are available, but if a universal interpretation exists it is
18983 -- also the selected one.
18985 elsif Universal_Interpretation (N) = Universal_Integer then
18986 T := Standard_Integer;
18988 else
18989 T := Any_Type;
18991 declare
18992 Ind : Interp_Index;
18993 It : Interp;
18995 begin
18996 Get_First_Interp (N, Ind, It);
18997 while Present (It.Typ) loop
18998 if Is_Discrete_Type (It.Typ) then
19000 if Found
19001 and then not Covers (It.Typ, T)
19002 and then not Covers (T, It.Typ)
19003 then
19004 Error_Msg_N ("ambiguous bounds in discrete range", N);
19005 exit;
19006 else
19007 T := It.Typ;
19008 Found := True;
19009 end if;
19010 end if;
19012 Get_Next_Interp (Ind, It);
19013 end loop;
19015 if T = Any_Type then
19016 Error_Msg_N ("discrete type required for range", N);
19017 Set_Etype (N, Any_Type);
19018 return;
19020 elsif T = Universal_Integer then
19021 T := Standard_Integer;
19022 end if;
19023 end;
19024 end if;
19026 if not Is_Discrete_Type (T) then
19027 Error_Msg_N ("discrete type required for range", N);
19028 Set_Etype (N, Any_Type);
19029 return;
19030 end if;
19032 if Nkind (Low_Bound (N)) = N_Attribute_Reference
19033 and then Attribute_Name (Low_Bound (N)) = Name_First
19034 and then Is_Entity_Name (Prefix (Low_Bound (N)))
19035 and then Is_Type (Entity (Prefix (Low_Bound (N))))
19036 and then Is_Discrete_Type (Entity (Prefix (Low_Bound (N))))
19037 then
19038 -- The type of the index will be the type of the prefix, as long
19039 -- as the upper bound is 'Last of the same type.
19041 Def_Id := Entity (Prefix (Low_Bound (N)));
19043 if Nkind (High_Bound (N)) /= N_Attribute_Reference
19044 or else Attribute_Name (High_Bound (N)) /= Name_Last
19045 or else not Is_Entity_Name (Prefix (High_Bound (N)))
19046 or else Entity (Prefix (High_Bound (N))) /= Def_Id
19047 then
19048 Def_Id := Empty;
19049 end if;
19050 end if;
19052 R := N;
19053 Process_Range_Expr_In_Decl (R, T, In_Iter_Schm => In_Iter_Schm);
19055 elsif Nkind (N) = N_Subtype_Indication then
19057 -- The index is given by a subtype with a range constraint
19059 T := Base_Type (Entity (Subtype_Mark (N)));
19061 if not Is_Discrete_Type (T) then
19062 Error_Msg_N ("discrete type required for range", N);
19063 Set_Etype (N, Any_Type);
19064 return;
19065 end if;
19067 R := Range_Expression (Constraint (N));
19069 Resolve (R, T);
19070 Process_Range_Expr_In_Decl
19071 (R, Entity (Subtype_Mark (N)), In_Iter_Schm => In_Iter_Schm);
19073 elsif Nkind (N) = N_Attribute_Reference then
19075 -- Catch beginner's error (use of attribute other than 'Range)
19077 if Attribute_Name (N) /= Name_Range then
19078 Error_Msg_N ("expect attribute ''Range", N);
19079 Set_Etype (N, Any_Type);
19080 return;
19081 end if;
19083 -- If the node denotes the range of a type mark, that is also the
19084 -- resulting type, and we do not need to create an Itype for it.
19086 if Is_Entity_Name (Prefix (N))
19087 and then Comes_From_Source (N)
19088 and then Is_Type (Entity (Prefix (N)))
19089 and then Is_Discrete_Type (Entity (Prefix (N)))
19090 then
19091 Def_Id := Entity (Prefix (N));
19092 end if;
19094 Analyze_And_Resolve (N);
19095 T := Etype (N);
19096 R := N;
19098 -- If none of the above, must be a subtype. We convert this to a
19099 -- range attribute reference because in the case of declared first
19100 -- named subtypes, the types in the range reference can be different
19101 -- from the type of the entity. A range attribute normalizes the
19102 -- reference and obtains the correct types for the bounds.
19104 -- This transformation is in the nature of an expansion, is only
19105 -- done if expansion is active. In particular, it is not done on
19106 -- formal generic types, because we need to retain the name of the
19107 -- original index for instantiation purposes.
19109 else
19110 if not Is_Entity_Name (N) or else not Is_Type (Entity (N)) then
19111 Error_Msg_N ("invalid subtype mark in discrete range ", N);
19112 Set_Etype (N, Any_Integer);
19113 return;
19115 else
19116 -- The type mark may be that of an incomplete type. It is only
19117 -- now that we can get the full view, previous analysis does
19118 -- not look specifically for a type mark.
19120 Set_Entity (N, Get_Full_View (Entity (N)));
19121 Set_Etype (N, Entity (N));
19122 Def_Id := Entity (N);
19124 if not Is_Discrete_Type (Def_Id) then
19125 Error_Msg_N ("discrete type required for index", N);
19126 Set_Etype (N, Any_Type);
19127 return;
19128 end if;
19129 end if;
19131 if Expander_Active then
19132 Rewrite (N,
19133 Make_Attribute_Reference (Sloc (N),
19134 Attribute_Name => Name_Range,
19135 Prefix => Relocate_Node (N)));
19137 -- The original was a subtype mark that does not freeze. This
19138 -- means that the rewritten version must not freeze either.
19140 Set_Must_Not_Freeze (N);
19141 Set_Must_Not_Freeze (Prefix (N));
19142 Analyze_And_Resolve (N);
19143 T := Etype (N);
19144 R := N;
19146 -- If expander is inactive, type is legal, nothing else to construct
19148 else
19149 return;
19150 end if;
19151 end if;
19153 if not Is_Discrete_Type (T) then
19154 Error_Msg_N ("discrete type required for range", N);
19155 Set_Etype (N, Any_Type);
19156 return;
19158 elsif T = Any_Type then
19159 Set_Etype (N, Any_Type);
19160 return;
19161 end if;
19163 -- We will now create the appropriate Itype to describe the range, but
19164 -- first a check. If we originally had a subtype, then we just label
19165 -- the range with this subtype. Not only is there no need to construct
19166 -- a new subtype, but it is wrong to do so for two reasons:
19168 -- 1. A legality concern, if we have a subtype, it must not freeze,
19169 -- and the Itype would cause freezing incorrectly
19171 -- 2. An efficiency concern, if we created an Itype, it would not be
19172 -- recognized as the same type for the purposes of eliminating
19173 -- checks in some circumstances.
19175 -- We signal this case by setting the subtype entity in Def_Id
19177 if No (Def_Id) then
19178 Def_Id :=
19179 Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
19180 Set_Etype (Def_Id, Base_Type (T));
19182 if Is_Signed_Integer_Type (T) then
19183 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
19185 elsif Is_Modular_Integer_Type (T) then
19186 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
19188 else
19189 Set_Ekind (Def_Id, E_Enumeration_Subtype);
19190 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
19191 Set_First_Literal (Def_Id, First_Literal (T));
19192 end if;
19194 Set_Size_Info (Def_Id, (T));
19195 Set_RM_Size (Def_Id, RM_Size (T));
19196 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
19198 Set_Scalar_Range (Def_Id, R);
19199 Conditional_Delay (Def_Id, T);
19201 if Nkind (N) = N_Subtype_Indication then
19202 Inherit_Predicate_Flags (Def_Id, Entity (Subtype_Mark (N)));
19203 end if;
19205 -- In the subtype indication case, if the immediate parent of the
19206 -- new subtype is non-static, then the subtype we create is non-
19207 -- static, even if its bounds are static.
19209 if Nkind (N) = N_Subtype_Indication
19210 and then not Is_OK_Static_Subtype (Entity (Subtype_Mark (N)))
19211 then
19212 Set_Is_Non_Static_Subtype (Def_Id);
19213 end if;
19214 end if;
19216 -- Final step is to label the index with this constructed type
19218 Set_Etype (N, Def_Id);
19219 end Make_Index;
19221 ------------------------------
19222 -- Modular_Type_Declaration --
19223 ------------------------------
19225 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
19226 Mod_Expr : constant Node_Id := Expression (Def);
19227 M_Val : Uint;
19229 procedure Set_Modular_Size (Bits : Int);
19230 -- Sets RM_Size to Bits, and Esize to normal word size above this
19232 ----------------------
19233 -- Set_Modular_Size --
19234 ----------------------
19236 procedure Set_Modular_Size (Bits : Int) is
19237 begin
19238 Set_RM_Size (T, UI_From_Int (Bits));
19240 if Bits <= 8 then
19241 Init_Esize (T, 8);
19243 elsif Bits <= 16 then
19244 Init_Esize (T, 16);
19246 elsif Bits <= 32 then
19247 Init_Esize (T, 32);
19249 else
19250 Init_Esize (T, System_Max_Binary_Modulus_Power);
19251 end if;
19253 if not Non_Binary_Modulus (T) and then Esize (T) = RM_Size (T) then
19254 Set_Is_Known_Valid (T);
19255 end if;
19256 end Set_Modular_Size;
19258 -- Start of processing for Modular_Type_Declaration
19260 begin
19261 -- If the mod expression is (exactly) 2 * literal, where literal is
19262 -- 64 or less,then almost certainly the * was meant to be **. Warn.
19264 if Warn_On_Suspicious_Modulus_Value
19265 and then Nkind (Mod_Expr) = N_Op_Multiply
19266 and then Nkind (Left_Opnd (Mod_Expr)) = N_Integer_Literal
19267 and then Intval (Left_Opnd (Mod_Expr)) = Uint_2
19268 and then Nkind (Right_Opnd (Mod_Expr)) = N_Integer_Literal
19269 and then Intval (Right_Opnd (Mod_Expr)) <= Uint_64
19270 then
19271 Error_Msg_N
19272 ("suspicious MOD value, was '*'* intended'??M?", Mod_Expr);
19273 end if;
19275 -- Proceed with analysis of mod expression
19277 Analyze_And_Resolve (Mod_Expr, Any_Integer);
19278 Set_Etype (T, T);
19279 Set_Ekind (T, E_Modular_Integer_Type);
19280 Init_Alignment (T);
19281 Set_Is_Constrained (T);
19283 if not Is_OK_Static_Expression (Mod_Expr) then
19284 Flag_Non_Static_Expr
19285 ("non-static expression used for modular type bound!", Mod_Expr);
19286 M_Val := 2 ** System_Max_Binary_Modulus_Power;
19287 else
19288 M_Val := Expr_Value (Mod_Expr);
19289 end if;
19291 if M_Val < 1 then
19292 Error_Msg_N ("modulus value must be positive", Mod_Expr);
19293 M_Val := 2 ** System_Max_Binary_Modulus_Power;
19294 end if;
19296 if M_Val > 2 ** Standard_Long_Integer_Size then
19297 Check_Restriction (No_Long_Long_Integers, Mod_Expr);
19298 end if;
19300 Set_Modulus (T, M_Val);
19302 -- Create bounds for the modular type based on the modulus given in
19303 -- the type declaration and then analyze and resolve those bounds.
19305 Set_Scalar_Range (T,
19306 Make_Range (Sloc (Mod_Expr),
19307 Low_Bound => Make_Integer_Literal (Sloc (Mod_Expr), 0),
19308 High_Bound => Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
19310 -- Properly analyze the literals for the range. We do this manually
19311 -- because we can't go calling Resolve, since we are resolving these
19312 -- bounds with the type, and this type is certainly not complete yet.
19314 Set_Etype (Low_Bound (Scalar_Range (T)), T);
19315 Set_Etype (High_Bound (Scalar_Range (T)), T);
19316 Set_Is_Static_Expression (Low_Bound (Scalar_Range (T)));
19317 Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
19319 -- Loop through powers of two to find number of bits required
19321 for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
19323 -- Binary case
19325 if M_Val = 2 ** Bits then
19326 Set_Modular_Size (Bits);
19327 return;
19329 -- Nonbinary case
19331 elsif M_Val < 2 ** Bits then
19332 Check_SPARK_05_Restriction ("modulus should be a power of 2", T);
19333 Set_Non_Binary_Modulus (T);
19335 if Bits > System_Max_Nonbinary_Modulus_Power then
19336 Error_Msg_Uint_1 :=
19337 UI_From_Int (System_Max_Nonbinary_Modulus_Power);
19338 Error_Msg_F
19339 ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
19340 Set_Modular_Size (System_Max_Binary_Modulus_Power);
19341 return;
19343 else
19344 -- In the nonbinary case, set size as per RM 13.3(55)
19346 Set_Modular_Size (Bits);
19347 return;
19348 end if;
19349 end if;
19351 end loop;
19353 -- If we fall through, then the size exceed System.Max_Binary_Modulus
19354 -- so we just signal an error and set the maximum size.
19356 Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
19357 Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
19359 Set_Modular_Size (System_Max_Binary_Modulus_Power);
19360 Init_Alignment (T);
19362 end Modular_Type_Declaration;
19364 --------------------------
19365 -- New_Concatenation_Op --
19366 --------------------------
19368 procedure New_Concatenation_Op (Typ : Entity_Id) is
19369 Loc : constant Source_Ptr := Sloc (Typ);
19370 Op : Entity_Id;
19372 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
19373 -- Create abbreviated declaration for the formal of a predefined
19374 -- Operator 'Op' of type 'Typ'
19376 --------------------
19377 -- Make_Op_Formal --
19378 --------------------
19380 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
19381 Formal : Entity_Id;
19382 begin
19383 Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
19384 Set_Etype (Formal, Typ);
19385 Set_Mechanism (Formal, Default_Mechanism);
19386 return Formal;
19387 end Make_Op_Formal;
19389 -- Start of processing for New_Concatenation_Op
19391 begin
19392 Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
19394 Set_Ekind (Op, E_Operator);
19395 Set_Scope (Op, Current_Scope);
19396 Set_Etype (Op, Typ);
19397 Set_Homonym (Op, Get_Name_Entity_Id (Name_Op_Concat));
19398 Set_Is_Immediately_Visible (Op);
19399 Set_Is_Intrinsic_Subprogram (Op);
19400 Set_Has_Completion (Op);
19401 Append_Entity (Op, Current_Scope);
19403 Set_Name_Entity_Id (Name_Op_Concat, Op);
19405 Append_Entity (Make_Op_Formal (Typ, Op), Op);
19406 Append_Entity (Make_Op_Formal (Typ, Op), Op);
19407 end New_Concatenation_Op;
19409 -------------------------
19410 -- OK_For_Limited_Init --
19411 -------------------------
19413 -- ???Check all calls of this, and compare the conditions under which it's
19414 -- called.
19416 function OK_For_Limited_Init
19417 (Typ : Entity_Id;
19418 Exp : Node_Id) return Boolean
19420 begin
19421 return Is_CPP_Constructor_Call (Exp)
19422 or else (Ada_Version >= Ada_2005
19423 and then not Debug_Flag_Dot_L
19424 and then OK_For_Limited_Init_In_05 (Typ, Exp));
19425 end OK_For_Limited_Init;
19427 -------------------------------
19428 -- OK_For_Limited_Init_In_05 --
19429 -------------------------------
19431 function OK_For_Limited_Init_In_05
19432 (Typ : Entity_Id;
19433 Exp : Node_Id) return Boolean
19435 begin
19436 -- An object of a limited interface type can be initialized with any
19437 -- expression of a nonlimited descendant type. However this does not
19438 -- apply if this is a view conversion of some other expression. This
19439 -- is checked below.
19441 if Is_Class_Wide_Type (Typ)
19442 and then Is_Limited_Interface (Typ)
19443 and then not Is_Limited_Type (Etype (Exp))
19444 and then Nkind (Exp) /= N_Type_Conversion
19445 then
19446 return True;
19447 end if;
19449 -- Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
19450 -- case of limited aggregates (including extension aggregates), and
19451 -- function calls. The function call may have been given in prefixed
19452 -- notation, in which case the original node is an indexed component.
19453 -- If the function is parameterless, the original node was an explicit
19454 -- dereference. The function may also be parameterless, in which case
19455 -- the source node is just an identifier.
19457 -- A branch of a conditional expression may have been removed if the
19458 -- condition is statically known. This happens during expansion, and
19459 -- thus will not happen if previous errors were encountered. The check
19460 -- will have been performed on the chosen branch, which replaces the
19461 -- original conditional expression.
19463 if No (Exp) then
19464 return True;
19465 end if;
19467 case Nkind (Original_Node (Exp)) is
19468 when N_Aggregate
19469 | N_Extension_Aggregate
19470 | N_Function_Call
19471 | N_Op
19473 return True;
19475 when N_Identifier =>
19476 return Present (Entity (Original_Node (Exp)))
19477 and then Ekind (Entity (Original_Node (Exp))) = E_Function;
19479 when N_Qualified_Expression =>
19480 return
19481 OK_For_Limited_Init_In_05
19482 (Typ, Expression (Original_Node (Exp)));
19484 -- Ada 2005 (AI-251): If a class-wide interface object is initialized
19485 -- with a function call, the expander has rewritten the call into an
19486 -- N_Type_Conversion node to force displacement of the pointer to
19487 -- reference the component containing the secondary dispatch table.
19488 -- Otherwise a type conversion is not a legal context.
19489 -- A return statement for a build-in-place function returning a
19490 -- synchronized type also introduces an unchecked conversion.
19492 when N_Type_Conversion
19493 | N_Unchecked_Type_Conversion
19495 return not Comes_From_Source (Exp)
19496 and then
19497 OK_For_Limited_Init_In_05
19498 (Typ, Expression (Original_Node (Exp)));
19500 when N_Explicit_Dereference
19501 | N_Indexed_Component
19502 | N_Selected_Component
19504 return Nkind (Exp) = N_Function_Call;
19506 -- A use of 'Input is a function call, hence allowed. Normally the
19507 -- attribute will be changed to a call, but the attribute by itself
19508 -- can occur with -gnatc.
19510 when N_Attribute_Reference =>
19511 return Attribute_Name (Original_Node (Exp)) = Name_Input;
19513 -- "return raise ..." is OK
19515 when N_Raise_Expression =>
19516 return True;
19518 -- For a case expression, all dependent expressions must be legal
19520 when N_Case_Expression =>
19521 declare
19522 Alt : Node_Id;
19524 begin
19525 Alt := First (Alternatives (Original_Node (Exp)));
19526 while Present (Alt) loop
19527 if not OK_For_Limited_Init_In_05 (Typ, Expression (Alt)) then
19528 return False;
19529 end if;
19531 Next (Alt);
19532 end loop;
19534 return True;
19535 end;
19537 -- For an if expression, all dependent expressions must be legal
19539 when N_If_Expression =>
19540 declare
19541 Then_Expr : constant Node_Id :=
19542 Next (First (Expressions (Original_Node (Exp))));
19543 Else_Expr : constant Node_Id := Next (Then_Expr);
19544 begin
19545 return OK_For_Limited_Init_In_05 (Typ, Then_Expr)
19546 and then
19547 OK_For_Limited_Init_In_05 (Typ, Else_Expr);
19548 end;
19550 when others =>
19551 return False;
19552 end case;
19553 end OK_For_Limited_Init_In_05;
19555 -------------------------------------------
19556 -- Ordinary_Fixed_Point_Type_Declaration --
19557 -------------------------------------------
19559 procedure Ordinary_Fixed_Point_Type_Declaration
19560 (T : Entity_Id;
19561 Def : Node_Id)
19563 Loc : constant Source_Ptr := Sloc (Def);
19564 Delta_Expr : constant Node_Id := Delta_Expression (Def);
19565 RRS : constant Node_Id := Real_Range_Specification (Def);
19566 Implicit_Base : Entity_Id;
19567 Delta_Val : Ureal;
19568 Small_Val : Ureal;
19569 Low_Val : Ureal;
19570 High_Val : Ureal;
19572 begin
19573 Check_Restriction (No_Fixed_Point, Def);
19575 -- Create implicit base type
19577 Implicit_Base :=
19578 Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
19579 Set_Etype (Implicit_Base, Implicit_Base);
19581 -- Analyze and process delta expression
19583 Analyze_And_Resolve (Delta_Expr, Any_Real);
19585 Check_Delta_Expression (Delta_Expr);
19586 Delta_Val := Expr_Value_R (Delta_Expr);
19588 Set_Delta_Value (Implicit_Base, Delta_Val);
19590 -- Compute default small from given delta, which is the largest power
19591 -- of two that does not exceed the given delta value.
19593 declare
19594 Tmp : Ureal;
19595 Scale : Int;
19597 begin
19598 Tmp := Ureal_1;
19599 Scale := 0;
19601 if Delta_Val < Ureal_1 then
19602 while Delta_Val < Tmp loop
19603 Tmp := Tmp / Ureal_2;
19604 Scale := Scale + 1;
19605 end loop;
19607 else
19608 loop
19609 Tmp := Tmp * Ureal_2;
19610 exit when Tmp > Delta_Val;
19611 Scale := Scale - 1;
19612 end loop;
19613 end if;
19615 Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
19616 end;
19618 Set_Small_Value (Implicit_Base, Small_Val);
19620 -- If no range was given, set a dummy range
19622 if RRS <= Empty_Or_Error then
19623 Low_Val := -Small_Val;
19624 High_Val := Small_Val;
19626 -- Otherwise analyze and process given range
19628 else
19629 declare
19630 Low : constant Node_Id := Low_Bound (RRS);
19631 High : constant Node_Id := High_Bound (RRS);
19633 begin
19634 Analyze_And_Resolve (Low, Any_Real);
19635 Analyze_And_Resolve (High, Any_Real);
19636 Check_Real_Bound (Low);
19637 Check_Real_Bound (High);
19639 -- Obtain and set the range
19641 Low_Val := Expr_Value_R (Low);
19642 High_Val := Expr_Value_R (High);
19644 if Low_Val > High_Val then
19645 Error_Msg_NE ("??fixed point type& has null range", Def, T);
19646 end if;
19647 end;
19648 end if;
19650 -- The range for both the implicit base and the declared first subtype
19651 -- cannot be set yet, so we use the special routine Set_Fixed_Range to
19652 -- set a temporary range in place. Note that the bounds of the base
19653 -- type will be widened to be symmetrical and to fill the available
19654 -- bits when the type is frozen.
19656 -- We could do this with all discrete types, and probably should, but
19657 -- we absolutely have to do it for fixed-point, since the end-points
19658 -- of the range and the size are determined by the small value, which
19659 -- could be reset before the freeze point.
19661 Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
19662 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
19664 -- Complete definition of first subtype. The inheritance of the rep item
19665 -- chain ensures that SPARK-related pragmas are not clobbered when the
19666 -- ordinary fixed point type acts as a full view of a private type.
19668 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
19669 Set_Etype (T, Implicit_Base);
19670 Init_Size_Align (T);
19671 Inherit_Rep_Item_Chain (T, Implicit_Base);
19672 Set_Small_Value (T, Small_Val);
19673 Set_Delta_Value (T, Delta_Val);
19674 Set_Is_Constrained (T);
19675 end Ordinary_Fixed_Point_Type_Declaration;
19677 ----------------------------------
19678 -- Preanalyze_Assert_Expression --
19679 ----------------------------------
19681 procedure Preanalyze_Assert_Expression (N : Node_Id; T : Entity_Id) is
19682 begin
19683 In_Assertion_Expr := In_Assertion_Expr + 1;
19684 Preanalyze_Spec_Expression (N, T);
19685 In_Assertion_Expr := In_Assertion_Expr - 1;
19686 end Preanalyze_Assert_Expression;
19688 -----------------------------------
19689 -- Preanalyze_Default_Expression --
19690 -----------------------------------
19692 procedure Preanalyze_Default_Expression (N : Node_Id; T : Entity_Id) is
19693 Save_In_Default_Expr : constant Boolean := In_Default_Expr;
19694 begin
19695 In_Default_Expr := True;
19696 Preanalyze_Spec_Expression (N, T);
19697 In_Default_Expr := Save_In_Default_Expr;
19698 end Preanalyze_Default_Expression;
19700 --------------------------------
19701 -- Preanalyze_Spec_Expression --
19702 --------------------------------
19704 procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
19705 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
19706 begin
19707 In_Spec_Expression := True;
19708 Preanalyze_And_Resolve (N, T);
19709 In_Spec_Expression := Save_In_Spec_Expression;
19710 end Preanalyze_Spec_Expression;
19712 ----------------------------------------
19713 -- Prepare_Private_Subtype_Completion --
19714 ----------------------------------------
19716 procedure Prepare_Private_Subtype_Completion
19717 (Id : Entity_Id;
19718 Related_Nod : Node_Id)
19720 Id_B : constant Entity_Id := Base_Type (Id);
19721 Full_B : Entity_Id := Full_View (Id_B);
19722 Full : Entity_Id;
19724 begin
19725 if Present (Full_B) then
19727 -- Get to the underlying full view if necessary
19729 if Is_Private_Type (Full_B)
19730 and then Present (Underlying_Full_View (Full_B))
19731 then
19732 Full_B := Underlying_Full_View (Full_B);
19733 end if;
19735 -- The Base_Type is already completed, we can complete the subtype
19736 -- now. We have to create a new entity with the same name, Thus we
19737 -- can't use Create_Itype.
19739 Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
19740 Set_Is_Itype (Full);
19741 Set_Associated_Node_For_Itype (Full, Related_Nod);
19742 Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
19743 end if;
19745 -- The parent subtype may be private, but the base might not, in some
19746 -- nested instances. In that case, the subtype does not need to be
19747 -- exchanged. It would still be nice to make private subtypes and their
19748 -- bases consistent at all times ???
19750 if Is_Private_Type (Id_B) then
19751 Append_Elmt (Id, Private_Dependents (Id_B));
19752 end if;
19753 end Prepare_Private_Subtype_Completion;
19755 ---------------------------
19756 -- Process_Discriminants --
19757 ---------------------------
19759 procedure Process_Discriminants
19760 (N : Node_Id;
19761 Prev : Entity_Id := Empty)
19763 Elist : constant Elist_Id := New_Elmt_List;
19764 Id : Node_Id;
19765 Discr : Node_Id;
19766 Discr_Number : Uint;
19767 Discr_Type : Entity_Id;
19768 Default_Present : Boolean := False;
19769 Default_Not_Present : Boolean := False;
19771 begin
19772 -- A composite type other than an array type can have discriminants.
19773 -- On entry, the current scope is the composite type.
19775 -- The discriminants are initially entered into the scope of the type
19776 -- via Enter_Name with the default Ekind of E_Void to prevent premature
19777 -- use, as explained at the end of this procedure.
19779 Discr := First (Discriminant_Specifications (N));
19780 while Present (Discr) loop
19781 Enter_Name (Defining_Identifier (Discr));
19783 -- For navigation purposes we add a reference to the discriminant
19784 -- in the entity for the type. If the current declaration is a
19785 -- completion, place references on the partial view. Otherwise the
19786 -- type is the current scope.
19788 if Present (Prev) then
19790 -- The references go on the partial view, if present. If the
19791 -- partial view has discriminants, the references have been
19792 -- generated already.
19794 if not Has_Discriminants (Prev) then
19795 Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
19796 end if;
19797 else
19798 Generate_Reference
19799 (Current_Scope, Defining_Identifier (Discr), 'd');
19800 end if;
19802 if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
19803 Discr_Type := Access_Definition (Discr, Discriminant_Type (Discr));
19805 -- Ada 2005 (AI-254)
19807 if Present (Access_To_Subprogram_Definition
19808 (Discriminant_Type (Discr)))
19809 and then Protected_Present (Access_To_Subprogram_Definition
19810 (Discriminant_Type (Discr)))
19811 then
19812 Discr_Type :=
19813 Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
19814 end if;
19816 else
19817 Find_Type (Discriminant_Type (Discr));
19818 Discr_Type := Etype (Discriminant_Type (Discr));
19820 if Error_Posted (Discriminant_Type (Discr)) then
19821 Discr_Type := Any_Type;
19822 end if;
19823 end if;
19825 -- Handling of discriminants that are access types
19827 if Is_Access_Type (Discr_Type) then
19829 -- Ada 2005 (AI-230): Access discriminant allowed in non-
19830 -- limited record types
19832 if Ada_Version < Ada_2005 then
19833 Check_Access_Discriminant_Requires_Limited
19834 (Discr, Discriminant_Type (Discr));
19835 end if;
19837 if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
19838 Error_Msg_N
19839 ("(Ada 83) access discriminant not allowed", Discr);
19840 end if;
19842 -- If not access type, must be a discrete type
19844 elsif not Is_Discrete_Type (Discr_Type) then
19845 Error_Msg_N
19846 ("discriminants must have a discrete or access type",
19847 Discriminant_Type (Discr));
19848 end if;
19850 Set_Etype (Defining_Identifier (Discr), Discr_Type);
19852 -- If a discriminant specification includes the assignment compound
19853 -- delimiter followed by an expression, the expression is the default
19854 -- expression of the discriminant; the default expression must be of
19855 -- the type of the discriminant. (RM 3.7.1) Since this expression is
19856 -- a default expression, we do the special preanalysis, since this
19857 -- expression does not freeze (see section "Handling of Default and
19858 -- Per-Object Expressions" in spec of package Sem).
19860 if Present (Expression (Discr)) then
19861 Preanalyze_Spec_Expression (Expression (Discr), Discr_Type);
19863 -- Legaity checks
19865 if Nkind (N) = N_Formal_Type_Declaration then
19866 Error_Msg_N
19867 ("discriminant defaults not allowed for formal type",
19868 Expression (Discr));
19870 -- Flag an error for a tagged type with defaulted discriminants,
19871 -- excluding limited tagged types when compiling for Ada 2012
19872 -- (see AI05-0214).
19874 elsif Is_Tagged_Type (Current_Scope)
19875 and then (not Is_Limited_Type (Current_Scope)
19876 or else Ada_Version < Ada_2012)
19877 and then Comes_From_Source (N)
19878 then
19879 -- Note: see similar test in Check_Or_Process_Discriminants, to
19880 -- handle the (illegal) case of the completion of an untagged
19881 -- view with discriminants with defaults by a tagged full view.
19882 -- We skip the check if Discr does not come from source, to
19883 -- account for the case of an untagged derived type providing
19884 -- defaults for a renamed discriminant from a private untagged
19885 -- ancestor with a tagged full view (ACATS B460006).
19887 if Ada_Version >= Ada_2012 then
19888 Error_Msg_N
19889 ("discriminants of nonlimited tagged type cannot have"
19890 & " defaults",
19891 Expression (Discr));
19892 else
19893 Error_Msg_N
19894 ("discriminants of tagged type cannot have defaults",
19895 Expression (Discr));
19896 end if;
19898 else
19899 Default_Present := True;
19900 Append_Elmt (Expression (Discr), Elist);
19902 -- Tag the defining identifiers for the discriminants with
19903 -- their corresponding default expressions from the tree.
19905 Set_Discriminant_Default_Value
19906 (Defining_Identifier (Discr), Expression (Discr));
19907 end if;
19909 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag
19910 -- gets set unless we can be sure that no range check is required.
19912 if (GNATprove_Mode or not Expander_Active)
19913 and then not
19914 Is_In_Range
19915 (Expression (Discr), Discr_Type, Assume_Valid => True)
19916 then
19917 Set_Do_Range_Check (Expression (Discr));
19918 end if;
19920 -- No default discriminant value given
19922 else
19923 Default_Not_Present := True;
19924 end if;
19926 -- Ada 2005 (AI-231): Create an Itype that is a duplicate of
19927 -- Discr_Type but with the null-exclusion attribute
19929 if Ada_Version >= Ada_2005 then
19931 -- Ada 2005 (AI-231): Static checks
19933 if Can_Never_Be_Null (Discr_Type) then
19934 Null_Exclusion_Static_Checks (Discr);
19936 elsif Is_Access_Type (Discr_Type)
19937 and then Null_Exclusion_Present (Discr)
19939 -- No need to check itypes because in their case this check
19940 -- was done at their point of creation
19942 and then not Is_Itype (Discr_Type)
19943 then
19944 if Can_Never_Be_Null (Discr_Type) then
19945 Error_Msg_NE
19946 ("`NOT NULL` not allowed (& already excludes null)",
19947 Discr,
19948 Discr_Type);
19949 end if;
19951 Set_Etype (Defining_Identifier (Discr),
19952 Create_Null_Excluding_Itype
19953 (T => Discr_Type,
19954 Related_Nod => Discr));
19956 -- Check for improper null exclusion if the type is otherwise
19957 -- legal for a discriminant.
19959 elsif Null_Exclusion_Present (Discr)
19960 and then Is_Discrete_Type (Discr_Type)
19961 then
19962 Error_Msg_N
19963 ("null exclusion can only apply to an access type", Discr);
19964 end if;
19966 -- Ada 2005 (AI-402): access discriminants of nonlimited types
19967 -- can't have defaults. Synchronized types, or types that are
19968 -- explicitly limited are fine, but special tests apply to derived
19969 -- types in generics: in a generic body we have to assume the
19970 -- worst, and therefore defaults are not allowed if the parent is
19971 -- a generic formal private type (see ACATS B370001).
19973 if Is_Access_Type (Discr_Type) and then Default_Present then
19974 if Ekind (Discr_Type) /= E_Anonymous_Access_Type
19975 or else Is_Limited_Record (Current_Scope)
19976 or else Is_Concurrent_Type (Current_Scope)
19977 or else Is_Concurrent_Record_Type (Current_Scope)
19978 or else Ekind (Current_Scope) = E_Limited_Private_Type
19979 then
19980 if not Is_Derived_Type (Current_Scope)
19981 or else not Is_Generic_Type (Etype (Current_Scope))
19982 or else not In_Package_Body (Scope (Etype (Current_Scope)))
19983 or else Limited_Present
19984 (Type_Definition (Parent (Current_Scope)))
19985 then
19986 null;
19988 else
19989 Error_Msg_N
19990 ("access discriminants of nonlimited types cannot "
19991 & "have defaults", Expression (Discr));
19992 end if;
19994 elsif Present (Expression (Discr)) then
19995 Error_Msg_N
19996 ("(Ada 2005) access discriminants of nonlimited types "
19997 & "cannot have defaults", Expression (Discr));
19998 end if;
19999 end if;
20000 end if;
20002 -- A discriminant cannot be effectively volatile (SPARK RM 7.1.3(6)).
20003 -- This check is relevant only when SPARK_Mode is on as it is not a
20004 -- standard Ada legality rule.
20006 if SPARK_Mode = On
20007 and then Is_Effectively_Volatile (Defining_Identifier (Discr))
20008 then
20009 Error_Msg_N ("discriminant cannot be volatile", Discr);
20010 end if;
20012 Next (Discr);
20013 end loop;
20015 -- An element list consisting of the default expressions of the
20016 -- discriminants is constructed in the above loop and used to set
20017 -- the Discriminant_Constraint attribute for the type. If an object
20018 -- is declared of this (record or task) type without any explicit
20019 -- discriminant constraint given, this element list will form the
20020 -- actual parameters for the corresponding initialization procedure
20021 -- for the type.
20023 Set_Discriminant_Constraint (Current_Scope, Elist);
20024 Set_Stored_Constraint (Current_Scope, No_Elist);
20026 -- Default expressions must be provided either for all or for none
20027 -- of the discriminants of a discriminant part. (RM 3.7.1)
20029 if Default_Present and then Default_Not_Present then
20030 Error_Msg_N
20031 ("incomplete specification of defaults for discriminants", N);
20032 end if;
20034 -- The use of the name of a discriminant is not allowed in default
20035 -- expressions of a discriminant part if the specification of the
20036 -- discriminant is itself given in the discriminant part. (RM 3.7.1)
20038 -- To detect this, the discriminant names are entered initially with an
20039 -- Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
20040 -- attempt to use a void entity (for example in an expression that is
20041 -- type-checked) produces the error message: premature usage. Now after
20042 -- completing the semantic analysis of the discriminant part, we can set
20043 -- the Ekind of all the discriminants appropriately.
20045 Discr := First (Discriminant_Specifications (N));
20046 Discr_Number := Uint_1;
20047 while Present (Discr) loop
20048 Id := Defining_Identifier (Discr);
20049 Set_Ekind (Id, E_Discriminant);
20050 Init_Component_Location (Id);
20051 Init_Esize (Id);
20052 Set_Discriminant_Number (Id, Discr_Number);
20054 -- Make sure this is always set, even in illegal programs
20056 Set_Corresponding_Discriminant (Id, Empty);
20058 -- Initialize the Original_Record_Component to the entity itself.
20059 -- Inherit_Components will propagate the right value to
20060 -- discriminants in derived record types.
20062 Set_Original_Record_Component (Id, Id);
20064 -- Create the discriminal for the discriminant
20066 Build_Discriminal (Id);
20068 Next (Discr);
20069 Discr_Number := Discr_Number + 1;
20070 end loop;
20072 Set_Has_Discriminants (Current_Scope);
20073 end Process_Discriminants;
20075 -----------------------
20076 -- Process_Full_View --
20077 -----------------------
20079 -- WARNING: This routine manages Ghost regions. Return statements must be
20080 -- replaced by gotos which jump to the end of the routine and restore the
20081 -- Ghost mode.
20083 procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
20084 procedure Collect_Implemented_Interfaces
20085 (Typ : Entity_Id;
20086 Ifaces : Elist_Id);
20087 -- Ada 2005: Gather all the interfaces that Typ directly or
20088 -- inherently implements. Duplicate entries are not added to
20089 -- the list Ifaces.
20091 ------------------------------------
20092 -- Collect_Implemented_Interfaces --
20093 ------------------------------------
20095 procedure Collect_Implemented_Interfaces
20096 (Typ : Entity_Id;
20097 Ifaces : Elist_Id)
20099 Iface : Entity_Id;
20100 Iface_Elmt : Elmt_Id;
20102 begin
20103 -- Abstract interfaces are only associated with tagged record types
20105 if not Is_Tagged_Type (Typ) or else not Is_Record_Type (Typ) then
20106 return;
20107 end if;
20109 -- Recursively climb to the ancestors
20111 if Etype (Typ) /= Typ
20113 -- Protect the frontend against wrong cyclic declarations like:
20115 -- type B is new A with private;
20116 -- type C is new A with private;
20117 -- private
20118 -- type B is new C with null record;
20119 -- type C is new B with null record;
20121 and then Etype (Typ) /= Priv_T
20122 and then Etype (Typ) /= Full_T
20123 then
20124 -- Keep separate the management of private type declarations
20126 if Ekind (Typ) = E_Record_Type_With_Private then
20128 -- Handle the following illegal usage:
20129 -- type Private_Type is tagged private;
20130 -- private
20131 -- type Private_Type is new Type_Implementing_Iface;
20133 if Present (Full_View (Typ))
20134 and then Etype (Typ) /= Full_View (Typ)
20135 then
20136 if Is_Interface (Etype (Typ)) then
20137 Append_Unique_Elmt (Etype (Typ), Ifaces);
20138 end if;
20140 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
20141 end if;
20143 -- Non-private types
20145 else
20146 if Is_Interface (Etype (Typ)) then
20147 Append_Unique_Elmt (Etype (Typ), Ifaces);
20148 end if;
20150 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
20151 end if;
20152 end if;
20154 -- Handle entities in the list of abstract interfaces
20156 if Present (Interfaces (Typ)) then
20157 Iface_Elmt := First_Elmt (Interfaces (Typ));
20158 while Present (Iface_Elmt) loop
20159 Iface := Node (Iface_Elmt);
20161 pragma Assert (Is_Interface (Iface));
20163 if not Contain_Interface (Iface, Ifaces) then
20164 Append_Elmt (Iface, Ifaces);
20165 Collect_Implemented_Interfaces (Iface, Ifaces);
20166 end if;
20168 Next_Elmt (Iface_Elmt);
20169 end loop;
20170 end if;
20171 end Collect_Implemented_Interfaces;
20173 -- Local variables
20175 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
20177 Full_Indic : Node_Id;
20178 Full_Parent : Entity_Id;
20179 Priv_Parent : Entity_Id;
20181 -- Start of processing for Process_Full_View
20183 begin
20184 Mark_And_Set_Ghost_Completion (N, Priv_T);
20186 -- First some sanity checks that must be done after semantic
20187 -- decoration of the full view and thus cannot be placed with other
20188 -- similar checks in Find_Type_Name
20190 if not Is_Limited_Type (Priv_T)
20191 and then (Is_Limited_Type (Full_T)
20192 or else Is_Limited_Composite (Full_T))
20193 then
20194 if In_Instance then
20195 null;
20196 else
20197 Error_Msg_N
20198 ("completion of nonlimited type cannot be limited", Full_T);
20199 Explain_Limited_Type (Full_T, Full_T);
20200 end if;
20202 elsif Is_Abstract_Type (Full_T)
20203 and then not Is_Abstract_Type (Priv_T)
20204 then
20205 Error_Msg_N
20206 ("completion of nonabstract type cannot be abstract", Full_T);
20208 elsif Is_Tagged_Type (Priv_T)
20209 and then Is_Limited_Type (Priv_T)
20210 and then not Is_Limited_Type (Full_T)
20211 then
20212 -- If pragma CPP_Class was applied to the private declaration
20213 -- propagate the limitedness to the full-view
20215 if Is_CPP_Class (Priv_T) then
20216 Set_Is_Limited_Record (Full_T);
20218 -- GNAT allow its own definition of Limited_Controlled to disobey
20219 -- this rule in order in ease the implementation. This test is safe
20220 -- because Root_Controlled is defined in a child of System that
20221 -- normal programs are not supposed to use.
20223 elsif Is_RTE (Etype (Full_T), RE_Root_Controlled) then
20224 Set_Is_Limited_Composite (Full_T);
20225 else
20226 Error_Msg_N
20227 ("completion of limited tagged type must be limited", Full_T);
20228 end if;
20230 elsif Is_Generic_Type (Priv_T) then
20231 Error_Msg_N ("generic type cannot have a completion", Full_T);
20232 end if;
20234 -- Check that ancestor interfaces of private and full views are
20235 -- consistent. We omit this check for synchronized types because
20236 -- they are performed on the corresponding record type when frozen.
20238 if Ada_Version >= Ada_2005
20239 and then Is_Tagged_Type (Priv_T)
20240 and then Is_Tagged_Type (Full_T)
20241 and then not Is_Concurrent_Type (Full_T)
20242 then
20243 declare
20244 Iface : Entity_Id;
20245 Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
20246 Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
20248 begin
20249 Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
20250 Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
20252 -- Ada 2005 (AI-251): The partial view shall be a descendant of
20253 -- an interface type if and only if the full type is descendant
20254 -- of the interface type (AARM 7.3 (7.3/2)).
20256 Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
20258 if Present (Iface) then
20259 Error_Msg_NE
20260 ("interface in partial view& not implemented by full type "
20261 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
20262 end if;
20264 Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
20266 if Present (Iface) then
20267 Error_Msg_NE
20268 ("interface & not implemented by partial view "
20269 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
20270 end if;
20271 end;
20272 end if;
20274 if Is_Tagged_Type (Priv_T)
20275 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
20276 and then Is_Derived_Type (Full_T)
20277 then
20278 Priv_Parent := Etype (Priv_T);
20280 -- The full view of a private extension may have been transformed
20281 -- into an unconstrained derived type declaration and a subtype
20282 -- declaration (see build_derived_record_type for details).
20284 if Nkind (N) = N_Subtype_Declaration then
20285 Full_Indic := Subtype_Indication (N);
20286 Full_Parent := Etype (Base_Type (Full_T));
20287 else
20288 Full_Indic := Subtype_Indication (Type_Definition (N));
20289 Full_Parent := Etype (Full_T);
20290 end if;
20292 -- Check that the parent type of the full type is a descendant of
20293 -- the ancestor subtype given in the private extension. If either
20294 -- entity has an Etype equal to Any_Type then we had some previous
20295 -- error situation [7.3(8)].
20297 if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
20298 goto Leave;
20300 -- Ada 2005 (AI-251): Interfaces in the full type can be given in
20301 -- any order. Therefore we don't have to check that its parent must
20302 -- be a descendant of the parent of the private type declaration.
20304 elsif Is_Interface (Priv_Parent)
20305 and then Is_Interface (Full_Parent)
20306 then
20307 null;
20309 -- Ada 2005 (AI-251): If the parent of the private type declaration
20310 -- is an interface there is no need to check that it is an ancestor
20311 -- of the associated full type declaration. The required tests for
20312 -- this case are performed by Build_Derived_Record_Type.
20314 elsif not Is_Interface (Base_Type (Priv_Parent))
20315 and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
20316 then
20317 Error_Msg_N
20318 ("parent of full type must descend from parent of private "
20319 & "extension", Full_Indic);
20321 -- First check a formal restriction, and then proceed with checking
20322 -- Ada rules. Since the formal restriction is not a serious error, we
20323 -- don't prevent further error detection for this check, hence the
20324 -- ELSE.
20326 else
20327 -- In formal mode, when completing a private extension the type
20328 -- named in the private part must be exactly the same as that
20329 -- named in the visible part.
20331 if Priv_Parent /= Full_Parent then
20332 Error_Msg_Name_1 := Chars (Priv_Parent);
20333 Check_SPARK_05_Restriction ("% expected", Full_Indic);
20334 end if;
20336 -- Check the rules of 7.3(10): if the private extension inherits
20337 -- known discriminants, then the full type must also inherit those
20338 -- discriminants from the same (ancestor) type, and the parent
20339 -- subtype of the full type must be constrained if and only if
20340 -- the ancestor subtype of the private extension is constrained.
20342 if No (Discriminant_Specifications (Parent (Priv_T)))
20343 and then not Has_Unknown_Discriminants (Priv_T)
20344 and then Has_Discriminants (Base_Type (Priv_Parent))
20345 then
20346 declare
20347 Priv_Indic : constant Node_Id :=
20348 Subtype_Indication (Parent (Priv_T));
20350 Priv_Constr : constant Boolean :=
20351 Is_Constrained (Priv_Parent)
20352 or else
20353 Nkind (Priv_Indic) = N_Subtype_Indication
20354 or else
20355 Is_Constrained (Entity (Priv_Indic));
20357 Full_Constr : constant Boolean :=
20358 Is_Constrained (Full_Parent)
20359 or else
20360 Nkind (Full_Indic) = N_Subtype_Indication
20361 or else
20362 Is_Constrained (Entity (Full_Indic));
20364 Priv_Discr : Entity_Id;
20365 Full_Discr : Entity_Id;
20367 begin
20368 Priv_Discr := First_Discriminant (Priv_Parent);
20369 Full_Discr := First_Discriminant (Full_Parent);
20370 while Present (Priv_Discr) and then Present (Full_Discr) loop
20371 if Original_Record_Component (Priv_Discr) =
20372 Original_Record_Component (Full_Discr)
20373 or else
20374 Corresponding_Discriminant (Priv_Discr) =
20375 Corresponding_Discriminant (Full_Discr)
20376 then
20377 null;
20378 else
20379 exit;
20380 end if;
20382 Next_Discriminant (Priv_Discr);
20383 Next_Discriminant (Full_Discr);
20384 end loop;
20386 if Present (Priv_Discr) or else Present (Full_Discr) then
20387 Error_Msg_N
20388 ("full view must inherit discriminants of the parent "
20389 & "type used in the private extension", Full_Indic);
20391 elsif Priv_Constr and then not Full_Constr then
20392 Error_Msg_N
20393 ("parent subtype of full type must be constrained",
20394 Full_Indic);
20396 elsif Full_Constr and then not Priv_Constr then
20397 Error_Msg_N
20398 ("parent subtype of full type must be unconstrained",
20399 Full_Indic);
20400 end if;
20401 end;
20403 -- Check the rules of 7.3(12): if a partial view has neither
20404 -- known or unknown discriminants, then the full type
20405 -- declaration shall define a definite subtype.
20407 elsif not Has_Unknown_Discriminants (Priv_T)
20408 and then not Has_Discriminants (Priv_T)
20409 and then not Is_Constrained (Full_T)
20410 then
20411 Error_Msg_N
20412 ("full view must define a constrained type if partial view "
20413 & "has no discriminants", Full_T);
20414 end if;
20416 -- ??????? Do we implement the following properly ?????
20417 -- If the ancestor subtype of a private extension has constrained
20418 -- discriminants, then the parent subtype of the full view shall
20419 -- impose a statically matching constraint on those discriminants
20420 -- [7.3(13)].
20421 end if;
20423 else
20424 -- For untagged types, verify that a type without discriminants is
20425 -- not completed with an unconstrained type. A separate error message
20426 -- is produced if the full type has defaulted discriminants.
20428 if Is_Definite_Subtype (Priv_T)
20429 and then not Is_Definite_Subtype (Full_T)
20430 then
20431 Error_Msg_Sloc := Sloc (Parent (Priv_T));
20432 Error_Msg_NE
20433 ("full view of& not compatible with declaration#",
20434 Full_T, Priv_T);
20436 if not Is_Tagged_Type (Full_T) then
20437 Error_Msg_N
20438 ("\one is constrained, the other unconstrained", Full_T);
20439 end if;
20440 end if;
20441 end if;
20443 -- AI-419: verify that the use of "limited" is consistent
20445 declare
20446 Orig_Decl : constant Node_Id := Original_Node (N);
20448 begin
20449 if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
20450 and then Nkind (Orig_Decl) = N_Full_Type_Declaration
20451 and then Nkind
20452 (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
20453 then
20454 if not Limited_Present (Parent (Priv_T))
20455 and then not Synchronized_Present (Parent (Priv_T))
20456 and then Limited_Present (Type_Definition (Orig_Decl))
20457 then
20458 Error_Msg_N
20459 ("full view of non-limited extension cannot be limited", N);
20461 -- Conversely, if the partial view carries the limited keyword,
20462 -- the full view must as well, even if it may be redundant.
20464 elsif Limited_Present (Parent (Priv_T))
20465 and then not Limited_Present (Type_Definition (Orig_Decl))
20466 then
20467 Error_Msg_N
20468 ("full view of limited extension must be explicitly limited",
20470 end if;
20471 end if;
20472 end;
20474 -- Ada 2005 (AI-443): A synchronized private extension must be
20475 -- completed by a task or protected type.
20477 if Ada_Version >= Ada_2005
20478 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
20479 and then Synchronized_Present (Parent (Priv_T))
20480 and then not Is_Concurrent_Type (Full_T)
20481 then
20482 Error_Msg_N ("full view of synchronized extension must " &
20483 "be synchronized type", N);
20484 end if;
20486 -- Ada 2005 AI-363: if the full view has discriminants with
20487 -- defaults, it is illegal to declare constrained access subtypes
20488 -- whose designated type is the current type. This allows objects
20489 -- of the type that are declared in the heap to be unconstrained.
20491 if not Has_Unknown_Discriminants (Priv_T)
20492 and then not Has_Discriminants (Priv_T)
20493 and then Has_Discriminants (Full_T)
20494 and then
20495 Present (Discriminant_Default_Value (First_Discriminant (Full_T)))
20496 then
20497 Set_Has_Constrained_Partial_View (Full_T);
20498 Set_Has_Constrained_Partial_View (Priv_T);
20499 end if;
20501 -- Create a full declaration for all its subtypes recorded in
20502 -- Private_Dependents and swap them similarly to the base type. These
20503 -- are subtypes that have been define before the full declaration of
20504 -- the private type. We also swap the entry in Private_Dependents list
20505 -- so we can properly restore the private view on exit from the scope.
20507 declare
20508 Priv_Elmt : Elmt_Id;
20509 Priv_Scop : Entity_Id;
20510 Priv : Entity_Id;
20511 Full : Entity_Id;
20513 begin
20514 Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
20515 while Present (Priv_Elmt) loop
20516 Priv := Node (Priv_Elmt);
20517 Priv_Scop := Scope (Priv);
20519 if Ekind_In (Priv, E_Private_Subtype,
20520 E_Limited_Private_Subtype,
20521 E_Record_Subtype_With_Private)
20522 then
20523 Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
20524 Set_Is_Itype (Full);
20525 Set_Parent (Full, Parent (Priv));
20526 Set_Associated_Node_For_Itype (Full, N);
20528 -- Now we need to complete the private subtype, but since the
20529 -- base type has already been swapped, we must also swap the
20530 -- subtypes (and thus, reverse the arguments in the call to
20531 -- Complete_Private_Subtype). Also note that we may need to
20532 -- re-establish the scope of the private subtype.
20534 Copy_And_Swap (Priv, Full);
20536 if not In_Open_Scopes (Priv_Scop) then
20537 Push_Scope (Priv_Scop);
20539 else
20540 -- Reset Priv_Scop to Empty to indicate no scope was pushed
20542 Priv_Scop := Empty;
20543 end if;
20545 Complete_Private_Subtype (Full, Priv, Full_T, N);
20547 if Present (Priv_Scop) then
20548 Pop_Scope;
20549 end if;
20551 Replace_Elmt (Priv_Elmt, Full);
20552 end if;
20554 Next_Elmt (Priv_Elmt);
20555 end loop;
20556 end;
20558 -- If the private view was tagged, copy the new primitive operations
20559 -- from the private view to the full view.
20561 if Is_Tagged_Type (Full_T) then
20562 declare
20563 Disp_Typ : Entity_Id;
20564 Full_List : Elist_Id;
20565 Prim : Entity_Id;
20566 Prim_Elmt : Elmt_Id;
20567 Priv_List : Elist_Id;
20569 function Contains
20570 (E : Entity_Id;
20571 L : Elist_Id) return Boolean;
20572 -- Determine whether list L contains element E
20574 --------------
20575 -- Contains --
20576 --------------
20578 function Contains
20579 (E : Entity_Id;
20580 L : Elist_Id) return Boolean
20582 List_Elmt : Elmt_Id;
20584 begin
20585 List_Elmt := First_Elmt (L);
20586 while Present (List_Elmt) loop
20587 if Node (List_Elmt) = E then
20588 return True;
20589 end if;
20591 Next_Elmt (List_Elmt);
20592 end loop;
20594 return False;
20595 end Contains;
20597 -- Start of processing
20599 begin
20600 if Is_Tagged_Type (Priv_T) then
20601 Priv_List := Primitive_Operations (Priv_T);
20602 Prim_Elmt := First_Elmt (Priv_List);
20604 -- In the case of a concurrent type completing a private tagged
20605 -- type, primitives may have been declared in between the two
20606 -- views. These subprograms need to be wrapped the same way
20607 -- entries and protected procedures are handled because they
20608 -- cannot be directly shared by the two views.
20610 if Is_Concurrent_Type (Full_T) then
20611 declare
20612 Conc_Typ : constant Entity_Id :=
20613 Corresponding_Record_Type (Full_T);
20614 Curr_Nod : Node_Id := Parent (Conc_Typ);
20615 Wrap_Spec : Node_Id;
20617 begin
20618 while Present (Prim_Elmt) loop
20619 Prim := Node (Prim_Elmt);
20621 if Comes_From_Source (Prim)
20622 and then not Is_Abstract_Subprogram (Prim)
20623 then
20624 Wrap_Spec :=
20625 Make_Subprogram_Declaration (Sloc (Prim),
20626 Specification =>
20627 Build_Wrapper_Spec
20628 (Subp_Id => Prim,
20629 Obj_Typ => Conc_Typ,
20630 Formals =>
20631 Parameter_Specifications
20632 (Parent (Prim))));
20634 Insert_After (Curr_Nod, Wrap_Spec);
20635 Curr_Nod := Wrap_Spec;
20637 Analyze (Wrap_Spec);
20639 -- Remove the wrapper from visibility to avoid
20640 -- spurious conflict with the wrapped entity.
20642 Set_Is_Immediately_Visible
20643 (Defining_Entity (Specification (Wrap_Spec)),
20644 False);
20645 end if;
20647 Next_Elmt (Prim_Elmt);
20648 end loop;
20650 goto Leave;
20651 end;
20653 -- For non-concurrent types, transfer explicit primitives, but
20654 -- omit those inherited from the parent of the private view
20655 -- since they will be re-inherited later on.
20657 else
20658 Full_List := Primitive_Operations (Full_T);
20660 while Present (Prim_Elmt) loop
20661 Prim := Node (Prim_Elmt);
20663 if Comes_From_Source (Prim)
20664 and then not Contains (Prim, Full_List)
20665 then
20666 Append_Elmt (Prim, Full_List);
20667 end if;
20669 Next_Elmt (Prim_Elmt);
20670 end loop;
20671 end if;
20673 -- Untagged private view
20675 else
20676 Full_List := Primitive_Operations (Full_T);
20678 -- In this case the partial view is untagged, so here we locate
20679 -- all of the earlier primitives that need to be treated as
20680 -- dispatching (those that appear between the two views). Note
20681 -- that these additional operations must all be new operations
20682 -- (any earlier operations that override inherited operations
20683 -- of the full view will already have been inserted in the
20684 -- primitives list, marked by Check_Operation_From_Private_View
20685 -- as dispatching. Note that implicit "/=" operators are
20686 -- excluded from being added to the primitives list since they
20687 -- shouldn't be treated as dispatching (tagged "/=" is handled
20688 -- specially).
20690 Prim := Next_Entity (Full_T);
20691 while Present (Prim) and then Prim /= Priv_T loop
20692 if Ekind_In (Prim, E_Procedure, E_Function) then
20693 Disp_Typ := Find_Dispatching_Type (Prim);
20695 if Disp_Typ = Full_T
20696 and then (Chars (Prim) /= Name_Op_Ne
20697 or else Comes_From_Source (Prim))
20698 then
20699 Check_Controlling_Formals (Full_T, Prim);
20701 if not Is_Dispatching_Operation (Prim) then
20702 Append_Elmt (Prim, Full_List);
20703 Set_Is_Dispatching_Operation (Prim, True);
20704 Set_DT_Position_Value (Prim, No_Uint);
20705 end if;
20707 elsif Is_Dispatching_Operation (Prim)
20708 and then Disp_Typ /= Full_T
20709 then
20711 -- Verify that it is not otherwise controlled by a
20712 -- formal or a return value of type T.
20714 Check_Controlling_Formals (Disp_Typ, Prim);
20715 end if;
20716 end if;
20718 Next_Entity (Prim);
20719 end loop;
20720 end if;
20722 -- For the tagged case, the two views can share the same primitive
20723 -- operations list and the same class-wide type. Update attributes
20724 -- of the class-wide type which depend on the full declaration.
20726 if Is_Tagged_Type (Priv_T) then
20727 Set_Direct_Primitive_Operations (Priv_T, Full_List);
20728 Set_Class_Wide_Type
20729 (Base_Type (Full_T), Class_Wide_Type (Priv_T));
20731 Propagate_Concurrent_Flags (Class_Wide_Type (Priv_T), Full_T);
20732 end if;
20733 end;
20734 end if;
20736 -- Ada 2005 AI 161: Check preelaborable initialization consistency
20738 if Known_To_Have_Preelab_Init (Priv_T) then
20740 -- Case where there is a pragma Preelaborable_Initialization. We
20741 -- always allow this in predefined units, which is cheating a bit,
20742 -- but it means we don't have to struggle to meet the requirements in
20743 -- the RM for having Preelaborable Initialization. Otherwise we
20744 -- require that the type meets the RM rules. But we can't check that
20745 -- yet, because of the rule about overriding Initialize, so we simply
20746 -- set a flag that will be checked at freeze time.
20748 if not In_Predefined_Unit (Full_T) then
20749 Set_Must_Have_Preelab_Init (Full_T);
20750 end if;
20751 end if;
20753 -- If pragma CPP_Class was applied to the private type declaration,
20754 -- propagate it now to the full type declaration.
20756 if Is_CPP_Class (Priv_T) then
20757 Set_Is_CPP_Class (Full_T);
20758 Set_Convention (Full_T, Convention_CPP);
20760 -- Check that components of imported CPP types do not have default
20761 -- expressions.
20763 Check_CPP_Type_Has_No_Defaults (Full_T);
20764 end if;
20766 -- If the private view has user specified stream attributes, then so has
20767 -- the full view.
20769 -- Why the test, how could these flags be already set in Full_T ???
20771 if Has_Specified_Stream_Read (Priv_T) then
20772 Set_Has_Specified_Stream_Read (Full_T);
20773 end if;
20775 if Has_Specified_Stream_Write (Priv_T) then
20776 Set_Has_Specified_Stream_Write (Full_T);
20777 end if;
20779 if Has_Specified_Stream_Input (Priv_T) then
20780 Set_Has_Specified_Stream_Input (Full_T);
20781 end if;
20783 if Has_Specified_Stream_Output (Priv_T) then
20784 Set_Has_Specified_Stream_Output (Full_T);
20785 end if;
20787 -- Propagate Default_Initial_Condition-related attributes from the
20788 -- partial view to the full view and its base type.
20790 Propagate_DIC_Attributes (Full_T, From_Typ => Priv_T);
20791 Propagate_DIC_Attributes (Base_Type (Full_T), From_Typ => Priv_T);
20793 -- Propagate invariant-related attributes from the partial view to the
20794 -- full view and its base type.
20796 Propagate_Invariant_Attributes (Full_T, From_Typ => Priv_T);
20797 Propagate_Invariant_Attributes (Base_Type (Full_T), From_Typ => Priv_T);
20799 -- AI12-0041: Detect an attempt to inherit a class-wide type invariant
20800 -- in the full view without advertising the inheritance in the partial
20801 -- view. This can only occur when the partial view has no parent type
20802 -- and the full view has an interface as a parent. Any other scenarios
20803 -- are illegal because implemented interfaces must match between the
20804 -- two views.
20806 if Is_Tagged_Type (Priv_T) and then Is_Tagged_Type (Full_T) then
20807 declare
20808 Full_Par : constant Entity_Id := Etype (Full_T);
20809 Priv_Par : constant Entity_Id := Etype (Priv_T);
20811 begin
20812 if not Is_Interface (Priv_Par)
20813 and then Is_Interface (Full_Par)
20814 and then Has_Inheritable_Invariants (Full_Par)
20815 then
20816 Error_Msg_N
20817 ("hidden inheritance of class-wide type invariants not "
20818 & "allowed", N);
20819 end if;
20820 end;
20821 end if;
20823 -- Propagate predicates to full type, and predicate function if already
20824 -- defined. It is not clear that this can actually happen? the partial
20825 -- view cannot be frozen yet, and the predicate function has not been
20826 -- built. Still it is a cheap check and seems safer to make it.
20828 if Has_Predicates (Priv_T) then
20829 Set_Has_Predicates (Full_T);
20831 if Present (Predicate_Function (Priv_T)) then
20832 Set_Predicate_Function (Full_T, Predicate_Function (Priv_T));
20833 end if;
20834 end if;
20836 <<Leave>>
20837 Restore_Ghost_Mode (Saved_GM);
20838 end Process_Full_View;
20840 -----------------------------------
20841 -- Process_Incomplete_Dependents --
20842 -----------------------------------
20844 procedure Process_Incomplete_Dependents
20845 (N : Node_Id;
20846 Full_T : Entity_Id;
20847 Inc_T : Entity_Id)
20849 Inc_Elmt : Elmt_Id;
20850 Priv_Dep : Entity_Id;
20851 New_Subt : Entity_Id;
20853 Disc_Constraint : Elist_Id;
20855 begin
20856 if No (Private_Dependents (Inc_T)) then
20857 return;
20858 end if;
20860 -- Itypes that may be generated by the completion of an incomplete
20861 -- subtype are not used by the back-end and not attached to the tree.
20862 -- They are created only for constraint-checking purposes.
20864 Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
20865 while Present (Inc_Elmt) loop
20866 Priv_Dep := Node (Inc_Elmt);
20868 if Ekind (Priv_Dep) = E_Subprogram_Type then
20870 -- An Access_To_Subprogram type may have a return type or a
20871 -- parameter type that is incomplete. Replace with the full view.
20873 if Etype (Priv_Dep) = Inc_T then
20874 Set_Etype (Priv_Dep, Full_T);
20875 end if;
20877 declare
20878 Formal : Entity_Id;
20880 begin
20881 Formal := First_Formal (Priv_Dep);
20882 while Present (Formal) loop
20883 if Etype (Formal) = Inc_T then
20884 Set_Etype (Formal, Full_T);
20885 end if;
20887 Next_Formal (Formal);
20888 end loop;
20889 end;
20891 elsif Is_Overloadable (Priv_Dep) then
20893 -- If a subprogram in the incomplete dependents list is primitive
20894 -- for a tagged full type then mark it as a dispatching operation,
20895 -- check whether it overrides an inherited subprogram, and check
20896 -- restrictions on its controlling formals. Note that a protected
20897 -- operation is never dispatching: only its wrapper operation
20898 -- (which has convention Ada) is.
20900 if Is_Tagged_Type (Full_T)
20901 and then Is_Primitive (Priv_Dep)
20902 and then Convention (Priv_Dep) /= Convention_Protected
20903 then
20904 Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
20905 Set_Is_Dispatching_Operation (Priv_Dep);
20906 Check_Controlling_Formals (Full_T, Priv_Dep);
20907 end if;
20909 elsif Ekind (Priv_Dep) = E_Subprogram_Body then
20911 -- Can happen during processing of a body before the completion
20912 -- of a TA type. Ignore, because spec is also on dependent list.
20914 return;
20916 -- Ada 2005 (AI-412): Transform a regular incomplete subtype into a
20917 -- corresponding subtype of the full view.
20919 elsif Ekind (Priv_Dep) = E_Incomplete_Subtype
20920 and then Comes_From_Source (Priv_Dep)
20921 then
20922 Set_Subtype_Indication
20923 (Parent (Priv_Dep), New_Occurrence_Of (Full_T, Sloc (Priv_Dep)));
20924 Set_Etype (Priv_Dep, Full_T);
20925 Set_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
20926 Set_Analyzed (Parent (Priv_Dep), False);
20928 -- Reanalyze the declaration, suppressing the call to Enter_Name
20929 -- to avoid duplicate names.
20931 Analyze_Subtype_Declaration
20932 (N => Parent (Priv_Dep),
20933 Skip => True);
20935 -- Dependent is a subtype
20937 else
20938 -- We build a new subtype indication using the full view of the
20939 -- incomplete parent. The discriminant constraints have been
20940 -- elaborated already at the point of the subtype declaration.
20942 New_Subt := Create_Itype (E_Void, N);
20944 if Has_Discriminants (Full_T) then
20945 Disc_Constraint := Discriminant_Constraint (Priv_Dep);
20946 else
20947 Disc_Constraint := No_Elist;
20948 end if;
20950 Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
20951 Set_Full_View (Priv_Dep, New_Subt);
20952 end if;
20954 Next_Elmt (Inc_Elmt);
20955 end loop;
20956 end Process_Incomplete_Dependents;
20958 --------------------------------
20959 -- Process_Range_Expr_In_Decl --
20960 --------------------------------
20962 procedure Process_Range_Expr_In_Decl
20963 (R : Node_Id;
20964 T : Entity_Id;
20965 Subtyp : Entity_Id := Empty;
20966 Check_List : List_Id := Empty_List;
20967 R_Check_Off : Boolean := False;
20968 In_Iter_Schm : Boolean := False)
20970 Lo, Hi : Node_Id;
20971 R_Checks : Check_Result;
20972 Insert_Node : Node_Id;
20973 Def_Id : Entity_Id;
20975 begin
20976 Analyze_And_Resolve (R, Base_Type (T));
20978 if Nkind (R) = N_Range then
20980 -- In SPARK, all ranges should be static, with the exception of the
20981 -- discrete type definition of a loop parameter specification.
20983 if not In_Iter_Schm
20984 and then not Is_OK_Static_Range (R)
20985 then
20986 Check_SPARK_05_Restriction ("range should be static", R);
20987 end if;
20989 Lo := Low_Bound (R);
20990 Hi := High_Bound (R);
20992 -- Validity checks on the range of a quantified expression are
20993 -- delayed until the construct is transformed into a loop.
20995 if Nkind (Parent (R)) = N_Loop_Parameter_Specification
20996 and then Nkind (Parent (Parent (R))) = N_Quantified_Expression
20997 then
20998 null;
21000 -- We need to ensure validity of the bounds here, because if we
21001 -- go ahead and do the expansion, then the expanded code will get
21002 -- analyzed with range checks suppressed and we miss the check.
21004 -- WARNING: The capture of the range bounds with xxx_FIRST/_LAST and
21005 -- the temporaries generated by routine Remove_Side_Effects by means
21006 -- of validity checks must use the same names. When a range appears
21007 -- in the parent of a generic, the range is processed with checks
21008 -- disabled as part of the generic context and with checks enabled
21009 -- for code generation purposes. This leads to link issues as the
21010 -- generic contains references to xxx_FIRST/_LAST, but the inlined
21011 -- template sees the temporaries generated by Remove_Side_Effects.
21013 else
21014 Validity_Check_Range (R, Subtyp);
21015 end if;
21017 -- If there were errors in the declaration, try and patch up some
21018 -- common mistakes in the bounds. The cases handled are literals
21019 -- which are Integer where the expected type is Real and vice versa.
21020 -- These corrections allow the compilation process to proceed further
21021 -- along since some basic assumptions of the format of the bounds
21022 -- are guaranteed.
21024 if Etype (R) = Any_Type then
21025 if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
21026 Rewrite (Lo,
21027 Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
21029 elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
21030 Rewrite (Hi,
21031 Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
21033 elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
21034 Rewrite (Lo,
21035 Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
21037 elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
21038 Rewrite (Hi,
21039 Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
21040 end if;
21042 Set_Etype (Lo, T);
21043 Set_Etype (Hi, T);
21044 end if;
21046 -- If the bounds of the range have been mistakenly given as string
21047 -- literals (perhaps in place of character literals), then an error
21048 -- has already been reported, but we rewrite the string literal as a
21049 -- bound of the range's type to avoid blowups in later processing
21050 -- that looks at static values.
21052 if Nkind (Lo) = N_String_Literal then
21053 Rewrite (Lo,
21054 Make_Attribute_Reference (Sloc (Lo),
21055 Prefix => New_Occurrence_Of (T, Sloc (Lo)),
21056 Attribute_Name => Name_First));
21057 Analyze_And_Resolve (Lo);
21058 end if;
21060 if Nkind (Hi) = N_String_Literal then
21061 Rewrite (Hi,
21062 Make_Attribute_Reference (Sloc (Hi),
21063 Prefix => New_Occurrence_Of (T, Sloc (Hi)),
21064 Attribute_Name => Name_First));
21065 Analyze_And_Resolve (Hi);
21066 end if;
21068 -- If bounds aren't scalar at this point then exit, avoiding
21069 -- problems with further processing of the range in this procedure.
21071 if not Is_Scalar_Type (Etype (Lo)) then
21072 return;
21073 end if;
21075 -- Resolve (actually Sem_Eval) has checked that the bounds are in
21076 -- then range of the base type. Here we check whether the bounds
21077 -- are in the range of the subtype itself. Note that if the bounds
21078 -- represent the null range the Constraint_Error exception should
21079 -- not be raised.
21081 -- ??? The following code should be cleaned up as follows
21083 -- 1. The Is_Null_Range (Lo, Hi) test should disappear since it
21084 -- is done in the call to Range_Check (R, T); below
21086 -- 2. The use of R_Check_Off should be investigated and possibly
21087 -- removed, this would clean up things a bit.
21089 if Is_Null_Range (Lo, Hi) then
21090 null;
21092 else
21093 -- Capture values of bounds and generate temporaries for them
21094 -- if needed, before applying checks, since checks may cause
21095 -- duplication of the expression without forcing evaluation.
21097 -- The forced evaluation removes side effects from expressions,
21098 -- which should occur also in GNATprove mode. Otherwise, we end up
21099 -- with unexpected insertions of actions at places where this is
21100 -- not supposed to occur, e.g. on default parameters of a call.
21102 if Expander_Active or GNATprove_Mode then
21104 -- Call Force_Evaluation to create declarations as needed to
21105 -- deal with side effects, and also create typ_FIRST/LAST
21106 -- entities for bounds if we have a subtype name.
21108 -- Note: we do this transformation even if expansion is not
21109 -- active if we are in GNATprove_Mode since the transformation
21110 -- is in general required to ensure that the resulting tree has
21111 -- proper Ada semantics.
21113 Force_Evaluation
21114 (Lo, Related_Id => Subtyp, Is_Low_Bound => True);
21115 Force_Evaluation
21116 (Hi, Related_Id => Subtyp, Is_High_Bound => True);
21117 end if;
21119 -- We use a flag here instead of suppressing checks on the type
21120 -- because the type we check against isn't necessarily the place
21121 -- where we put the check.
21123 if not R_Check_Off then
21124 R_Checks := Get_Range_Checks (R, T);
21126 -- Look up tree to find an appropriate insertion point. We
21127 -- can't just use insert_actions because later processing
21128 -- depends on the insertion node. Prior to Ada 2012 the
21129 -- insertion point could only be a declaration or a loop, but
21130 -- quantified expressions can appear within any context in an
21131 -- expression, and the insertion point can be any statement,
21132 -- pragma, or declaration.
21134 Insert_Node := Parent (R);
21135 while Present (Insert_Node) loop
21136 exit when
21137 Nkind (Insert_Node) in N_Declaration
21138 and then
21139 not Nkind_In
21140 (Insert_Node, N_Component_Declaration,
21141 N_Loop_Parameter_Specification,
21142 N_Function_Specification,
21143 N_Procedure_Specification);
21145 exit when Nkind (Insert_Node) in N_Later_Decl_Item
21146 or else Nkind (Insert_Node) in
21147 N_Statement_Other_Than_Procedure_Call
21148 or else Nkind_In (Insert_Node, N_Procedure_Call_Statement,
21149 N_Pragma);
21151 Insert_Node := Parent (Insert_Node);
21152 end loop;
21154 -- Why would Type_Decl not be present??? Without this test,
21155 -- short regression tests fail.
21157 if Present (Insert_Node) then
21159 -- Case of loop statement. Verify that the range is part
21160 -- of the subtype indication of the iteration scheme.
21162 if Nkind (Insert_Node) = N_Loop_Statement then
21163 declare
21164 Indic : Node_Id;
21166 begin
21167 Indic := Parent (R);
21168 while Present (Indic)
21169 and then Nkind (Indic) /= N_Subtype_Indication
21170 loop
21171 Indic := Parent (Indic);
21172 end loop;
21174 if Present (Indic) then
21175 Def_Id := Etype (Subtype_Mark (Indic));
21177 Insert_Range_Checks
21178 (R_Checks,
21179 Insert_Node,
21180 Def_Id,
21181 Sloc (Insert_Node),
21183 Do_Before => True);
21184 end if;
21185 end;
21187 -- Insertion before a declaration. If the declaration
21188 -- includes discriminants, the list of applicable checks
21189 -- is given by the caller.
21191 elsif Nkind (Insert_Node) in N_Declaration then
21192 Def_Id := Defining_Identifier (Insert_Node);
21194 if (Ekind (Def_Id) = E_Record_Type
21195 and then Depends_On_Discriminant (R))
21196 or else
21197 (Ekind (Def_Id) = E_Protected_Type
21198 and then Has_Discriminants (Def_Id))
21199 then
21200 Append_Range_Checks
21201 (R_Checks,
21202 Check_List, Def_Id, Sloc (Insert_Node), R);
21204 else
21205 Insert_Range_Checks
21206 (R_Checks,
21207 Insert_Node, Def_Id, Sloc (Insert_Node), R);
21209 end if;
21211 -- Insertion before a statement. Range appears in the
21212 -- context of a quantified expression. Insertion will
21213 -- take place when expression is expanded.
21215 else
21216 null;
21217 end if;
21218 end if;
21219 end if;
21220 end if;
21222 -- Case of other than an explicit N_Range node
21224 -- The forced evaluation removes side effects from expressions, which
21225 -- should occur also in GNATprove mode. Otherwise, we end up with
21226 -- unexpected insertions of actions at places where this is not
21227 -- supposed to occur, e.g. on default parameters of a call.
21229 elsif Expander_Active or GNATprove_Mode then
21230 Get_Index_Bounds (R, Lo, Hi);
21231 Force_Evaluation (Lo);
21232 Force_Evaluation (Hi);
21233 end if;
21234 end Process_Range_Expr_In_Decl;
21236 --------------------------------------
21237 -- Process_Real_Range_Specification --
21238 --------------------------------------
21240 procedure Process_Real_Range_Specification (Def : Node_Id) is
21241 Spec : constant Node_Id := Real_Range_Specification (Def);
21242 Lo : Node_Id;
21243 Hi : Node_Id;
21244 Err : Boolean := False;
21246 procedure Analyze_Bound (N : Node_Id);
21247 -- Analyze and check one bound
21249 -------------------
21250 -- Analyze_Bound --
21251 -------------------
21253 procedure Analyze_Bound (N : Node_Id) is
21254 begin
21255 Analyze_And_Resolve (N, Any_Real);
21257 if not Is_OK_Static_Expression (N) then
21258 Flag_Non_Static_Expr
21259 ("bound in real type definition is not static!", N);
21260 Err := True;
21261 end if;
21262 end Analyze_Bound;
21264 -- Start of processing for Process_Real_Range_Specification
21266 begin
21267 if Present (Spec) then
21268 Lo := Low_Bound (Spec);
21269 Hi := High_Bound (Spec);
21270 Analyze_Bound (Lo);
21271 Analyze_Bound (Hi);
21273 -- If error, clear away junk range specification
21275 if Err then
21276 Set_Real_Range_Specification (Def, Empty);
21277 end if;
21278 end if;
21279 end Process_Real_Range_Specification;
21281 ---------------------
21282 -- Process_Subtype --
21283 ---------------------
21285 function Process_Subtype
21286 (S : Node_Id;
21287 Related_Nod : Node_Id;
21288 Related_Id : Entity_Id := Empty;
21289 Suffix : Character := ' ') return Entity_Id
21291 P : Node_Id;
21292 Def_Id : Entity_Id;
21293 Error_Node : Node_Id;
21294 Full_View_Id : Entity_Id;
21295 Subtype_Mark_Id : Entity_Id;
21297 May_Have_Null_Exclusion : Boolean;
21299 procedure Check_Incomplete (T : Node_Id);
21300 -- Called to verify that an incomplete type is not used prematurely
21302 ----------------------
21303 -- Check_Incomplete --
21304 ----------------------
21306 procedure Check_Incomplete (T : Node_Id) is
21307 begin
21308 -- Ada 2005 (AI-412): Incomplete subtypes are legal
21310 if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
21311 and then
21312 not (Ada_Version >= Ada_2005
21313 and then
21314 (Nkind (Parent (T)) = N_Subtype_Declaration
21315 or else (Nkind (Parent (T)) = N_Subtype_Indication
21316 and then Nkind (Parent (Parent (T))) =
21317 N_Subtype_Declaration)))
21318 then
21319 Error_Msg_N ("invalid use of type before its full declaration", T);
21320 end if;
21321 end Check_Incomplete;
21323 -- Start of processing for Process_Subtype
21325 begin
21326 -- Case of no constraints present
21328 if Nkind (S) /= N_Subtype_Indication then
21329 Find_Type (S);
21330 Check_Incomplete (S);
21331 P := Parent (S);
21333 -- Ada 2005 (AI-231): Static check
21335 if Ada_Version >= Ada_2005
21336 and then Present (P)
21337 and then Null_Exclusion_Present (P)
21338 and then Nkind (P) /= N_Access_To_Object_Definition
21339 and then not Is_Access_Type (Entity (S))
21340 then
21341 Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
21342 end if;
21344 -- The following is ugly, can't we have a range or even a flag???
21346 May_Have_Null_Exclusion :=
21347 Nkind_In (P, N_Access_Definition,
21348 N_Access_Function_Definition,
21349 N_Access_Procedure_Definition,
21350 N_Access_To_Object_Definition,
21351 N_Allocator,
21352 N_Component_Definition)
21353 or else
21354 Nkind_In (P, N_Derived_Type_Definition,
21355 N_Discriminant_Specification,
21356 N_Formal_Object_Declaration,
21357 N_Object_Declaration,
21358 N_Object_Renaming_Declaration,
21359 N_Parameter_Specification,
21360 N_Subtype_Declaration);
21362 -- Create an Itype that is a duplicate of Entity (S) but with the
21363 -- null-exclusion attribute.
21365 if May_Have_Null_Exclusion
21366 and then Is_Access_Type (Entity (S))
21367 and then Null_Exclusion_Present (P)
21369 -- No need to check the case of an access to object definition.
21370 -- It is correct to define double not-null pointers.
21372 -- Example:
21373 -- type Not_Null_Int_Ptr is not null access Integer;
21374 -- type Acc is not null access Not_Null_Int_Ptr;
21376 and then Nkind (P) /= N_Access_To_Object_Definition
21377 then
21378 if Can_Never_Be_Null (Entity (S)) then
21379 case Nkind (Related_Nod) is
21380 when N_Full_Type_Declaration =>
21381 if Nkind (Type_Definition (Related_Nod))
21382 in N_Array_Type_Definition
21383 then
21384 Error_Node :=
21385 Subtype_Indication
21386 (Component_Definition
21387 (Type_Definition (Related_Nod)));
21388 else
21389 Error_Node :=
21390 Subtype_Indication (Type_Definition (Related_Nod));
21391 end if;
21393 when N_Subtype_Declaration =>
21394 Error_Node := Subtype_Indication (Related_Nod);
21396 when N_Object_Declaration =>
21397 Error_Node := Object_Definition (Related_Nod);
21399 when N_Component_Declaration =>
21400 Error_Node :=
21401 Subtype_Indication (Component_Definition (Related_Nod));
21403 when N_Allocator =>
21404 Error_Node := Expression (Related_Nod);
21406 when others =>
21407 pragma Assert (False);
21408 Error_Node := Related_Nod;
21409 end case;
21411 Error_Msg_NE
21412 ("`NOT NULL` not allowed (& already excludes null)",
21413 Error_Node,
21414 Entity (S));
21415 end if;
21417 Set_Etype (S,
21418 Create_Null_Excluding_Itype
21419 (T => Entity (S),
21420 Related_Nod => P));
21421 Set_Entity (S, Etype (S));
21422 end if;
21424 return Entity (S);
21426 -- Case of constraint present, so that we have an N_Subtype_Indication
21427 -- node (this node is created only if constraints are present).
21429 else
21430 Find_Type (Subtype_Mark (S));
21432 if Nkind (Parent (S)) /= N_Access_To_Object_Definition
21433 and then not
21434 (Nkind (Parent (S)) = N_Subtype_Declaration
21435 and then Is_Itype (Defining_Identifier (Parent (S))))
21436 then
21437 Check_Incomplete (Subtype_Mark (S));
21438 end if;
21440 P := Parent (S);
21441 Subtype_Mark_Id := Entity (Subtype_Mark (S));
21443 -- Explicit subtype declaration case
21445 if Nkind (P) = N_Subtype_Declaration then
21446 Def_Id := Defining_Identifier (P);
21448 -- Explicit derived type definition case
21450 elsif Nkind (P) = N_Derived_Type_Definition then
21451 Def_Id := Defining_Identifier (Parent (P));
21453 -- Implicit case, the Def_Id must be created as an implicit type.
21454 -- The one exception arises in the case of concurrent types, array
21455 -- and access types, where other subsidiary implicit types may be
21456 -- created and must appear before the main implicit type. In these
21457 -- cases we leave Def_Id set to Empty as a signal that Create_Itype
21458 -- has not yet been called to create Def_Id.
21460 else
21461 if Is_Array_Type (Subtype_Mark_Id)
21462 or else Is_Concurrent_Type (Subtype_Mark_Id)
21463 or else Is_Access_Type (Subtype_Mark_Id)
21464 then
21465 Def_Id := Empty;
21467 -- For the other cases, we create a new unattached Itype,
21468 -- and set the indication to ensure it gets attached later.
21470 else
21471 Def_Id :=
21472 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
21473 end if;
21474 end if;
21476 -- If the kind of constraint is invalid for this kind of type,
21477 -- then give an error, and then pretend no constraint was given.
21479 if not Is_Valid_Constraint_Kind
21480 (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
21481 then
21482 Error_Msg_N
21483 ("incorrect constraint for this kind of type", Constraint (S));
21485 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
21487 -- Set Ekind of orphan itype, to prevent cascaded errors
21489 if Present (Def_Id) then
21490 Set_Ekind (Def_Id, Ekind (Any_Type));
21491 end if;
21493 -- Make recursive call, having got rid of the bogus constraint
21495 return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
21496 end if;
21498 -- Remaining processing depends on type. Select on Base_Type kind to
21499 -- ensure getting to the concrete type kind in the case of a private
21500 -- subtype (needed when only doing semantic analysis).
21502 case Ekind (Base_Type (Subtype_Mark_Id)) is
21503 when Access_Kind =>
21505 -- If this is a constraint on a class-wide type, discard it.
21506 -- There is currently no way to express a partial discriminant
21507 -- constraint on a type with unknown discriminants. This is
21508 -- a pathology that the ACATS wisely decides not to test.
21510 if Is_Class_Wide_Type (Designated_Type (Subtype_Mark_Id)) then
21511 if Comes_From_Source (S) then
21512 Error_Msg_N
21513 ("constraint on class-wide type ignored??",
21514 Constraint (S));
21515 end if;
21517 if Nkind (P) = N_Subtype_Declaration then
21518 Set_Subtype_Indication (P,
21519 New_Occurrence_Of (Subtype_Mark_Id, Sloc (S)));
21520 end if;
21522 return Subtype_Mark_Id;
21523 end if;
21525 Constrain_Access (Def_Id, S, Related_Nod);
21527 if Expander_Active
21528 and then Is_Itype (Designated_Type (Def_Id))
21529 and then Nkind (Related_Nod) = N_Subtype_Declaration
21530 and then not Is_Incomplete_Type (Designated_Type (Def_Id))
21531 then
21532 Build_Itype_Reference
21533 (Designated_Type (Def_Id), Related_Nod);
21534 end if;
21536 when Array_Kind =>
21537 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
21539 when Decimal_Fixed_Point_Kind =>
21540 Constrain_Decimal (Def_Id, S);
21542 when Enumeration_Kind =>
21543 Constrain_Enumeration (Def_Id, S);
21544 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
21546 when Ordinary_Fixed_Point_Kind =>
21547 Constrain_Ordinary_Fixed (Def_Id, S);
21549 when Float_Kind =>
21550 Constrain_Float (Def_Id, S);
21552 when Integer_Kind =>
21553 Constrain_Integer (Def_Id, S);
21554 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
21556 when Class_Wide_Kind
21557 | E_Incomplete_Type
21558 | E_Record_Subtype
21559 | E_Record_Type
21561 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
21563 if Ekind (Def_Id) = E_Incomplete_Type then
21564 Set_Private_Dependents (Def_Id, New_Elmt_List);
21565 end if;
21567 when Private_Kind =>
21568 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
21570 -- The base type may be private but Def_Id may be a full view
21571 -- in an instance.
21573 if Is_Private_Type (Def_Id) then
21574 Set_Private_Dependents (Def_Id, New_Elmt_List);
21575 end if;
21577 -- In case of an invalid constraint prevent further processing
21578 -- since the type constructed is missing expected fields.
21580 if Etype (Def_Id) = Any_Type then
21581 return Def_Id;
21582 end if;
21584 -- If the full view is that of a task with discriminants,
21585 -- we must constrain both the concurrent type and its
21586 -- corresponding record type. Otherwise we will just propagate
21587 -- the constraint to the full view, if available.
21589 if Present (Full_View (Subtype_Mark_Id))
21590 and then Has_Discriminants (Subtype_Mark_Id)
21591 and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
21592 then
21593 Full_View_Id :=
21594 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
21596 Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
21597 Constrain_Concurrent (Full_View_Id, S,
21598 Related_Nod, Related_Id, Suffix);
21599 Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
21600 Set_Full_View (Def_Id, Full_View_Id);
21602 -- Introduce an explicit reference to the private subtype,
21603 -- to prevent scope anomalies in gigi if first use appears
21604 -- in a nested context, e.g. a later function body.
21605 -- Should this be generated in other contexts than a full
21606 -- type declaration?
21608 if Is_Itype (Def_Id)
21609 and then
21610 Nkind (Parent (P)) = N_Full_Type_Declaration
21611 then
21612 Build_Itype_Reference (Def_Id, Parent (P));
21613 end if;
21615 else
21616 Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
21617 end if;
21619 when Concurrent_Kind =>
21620 Constrain_Concurrent (Def_Id, S,
21621 Related_Nod, Related_Id, Suffix);
21623 when others =>
21624 Error_Msg_N ("invalid subtype mark in subtype indication", S);
21625 end case;
21627 -- Size, Alignment, Representation aspects and Convention are always
21628 -- inherited from the base type.
21630 Set_Size_Info (Def_Id, (Subtype_Mark_Id));
21631 Set_Rep_Info (Def_Id, (Subtype_Mark_Id));
21632 Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
21634 return Def_Id;
21635 end if;
21636 end Process_Subtype;
21638 -----------------------------
21639 -- Record_Type_Declaration --
21640 -----------------------------
21642 procedure Record_Type_Declaration
21643 (T : Entity_Id;
21644 N : Node_Id;
21645 Prev : Entity_Id)
21647 Def : constant Node_Id := Type_Definition (N);
21648 Is_Tagged : Boolean;
21649 Tag_Comp : Entity_Id;
21651 begin
21652 -- These flags must be initialized before calling Process_Discriminants
21653 -- because this routine makes use of them.
21655 Set_Ekind (T, E_Record_Type);
21656 Set_Etype (T, T);
21657 Init_Size_Align (T);
21658 Set_Interfaces (T, No_Elist);
21659 Set_Stored_Constraint (T, No_Elist);
21660 Set_Default_SSO (T);
21661 Set_No_Reordering (T, No_Component_Reordering);
21663 -- Normal case
21665 if Ada_Version < Ada_2005 or else not Interface_Present (Def) then
21666 if Limited_Present (Def) then
21667 Check_SPARK_05_Restriction ("limited is not allowed", N);
21668 end if;
21670 if Abstract_Present (Def) then
21671 Check_SPARK_05_Restriction ("abstract is not allowed", N);
21672 end if;
21674 -- The flag Is_Tagged_Type might have already been set by
21675 -- Find_Type_Name if it detected an error for declaration T. This
21676 -- arises in the case of private tagged types where the full view
21677 -- omits the word tagged.
21679 Is_Tagged :=
21680 Tagged_Present (Def)
21681 or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
21683 Set_Is_Limited_Record (T, Limited_Present (Def));
21685 if Is_Tagged then
21686 Set_Is_Tagged_Type (T, True);
21687 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
21688 end if;
21690 -- Type is abstract if full declaration carries keyword, or if
21691 -- previous partial view did.
21693 Set_Is_Abstract_Type (T, Is_Abstract_Type (T)
21694 or else Abstract_Present (Def));
21696 else
21697 Check_SPARK_05_Restriction ("interface is not allowed", N);
21699 Is_Tagged := True;
21700 Analyze_Interface_Declaration (T, Def);
21702 if Present (Discriminant_Specifications (N)) then
21703 Error_Msg_N
21704 ("interface types cannot have discriminants",
21705 Defining_Identifier
21706 (First (Discriminant_Specifications (N))));
21707 end if;
21708 end if;
21710 -- First pass: if there are self-referential access components,
21711 -- create the required anonymous access type declarations, and if
21712 -- need be an incomplete type declaration for T itself.
21714 Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
21716 if Ada_Version >= Ada_2005
21717 and then Present (Interface_List (Def))
21718 then
21719 Check_Interfaces (N, Def);
21721 declare
21722 Ifaces_List : Elist_Id;
21724 begin
21725 -- Ada 2005 (AI-251): Collect the list of progenitors that are not
21726 -- already in the parents.
21728 Collect_Interfaces
21729 (T => T,
21730 Ifaces_List => Ifaces_List,
21731 Exclude_Parents => True);
21733 Set_Interfaces (T, Ifaces_List);
21734 end;
21735 end if;
21737 -- Records constitute a scope for the component declarations within.
21738 -- The scope is created prior to the processing of these declarations.
21739 -- Discriminants are processed first, so that they are visible when
21740 -- processing the other components. The Ekind of the record type itself
21741 -- is set to E_Record_Type (subtypes appear as E_Record_Subtype).
21743 -- Enter record scope
21745 Push_Scope (T);
21747 -- If an incomplete or private type declaration was already given for
21748 -- the type, then this scope already exists, and the discriminants have
21749 -- been declared within. We must verify that the full declaration
21750 -- matches the incomplete one.
21752 Check_Or_Process_Discriminants (N, T, Prev);
21754 Set_Is_Constrained (T, not Has_Discriminants (T));
21755 Set_Has_Delayed_Freeze (T, True);
21757 -- For tagged types add a manually analyzed component corresponding
21758 -- to the component _tag, the corresponding piece of tree will be
21759 -- expanded as part of the freezing actions if it is not a CPP_Class.
21761 if Is_Tagged then
21763 -- Do not add the tag unless we are in expansion mode
21765 if Expander_Active then
21766 Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
21767 Enter_Name (Tag_Comp);
21769 Set_Ekind (Tag_Comp, E_Component);
21770 Set_Is_Tag (Tag_Comp);
21771 Set_Is_Aliased (Tag_Comp);
21772 Set_Etype (Tag_Comp, RTE (RE_Tag));
21773 Set_DT_Entry_Count (Tag_Comp, No_Uint);
21774 Set_Original_Record_Component (Tag_Comp, Tag_Comp);
21775 Init_Component_Location (Tag_Comp);
21777 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
21778 -- implemented interfaces.
21780 if Has_Interfaces (T) then
21781 Add_Interface_Tag_Components (N, T);
21782 end if;
21783 end if;
21785 Make_Class_Wide_Type (T);
21786 Set_Direct_Primitive_Operations (T, New_Elmt_List);
21787 end if;
21789 -- We must suppress range checks when processing record components in
21790 -- the presence of discriminants, since we don't want spurious checks to
21791 -- be generated during their analysis, but Suppress_Range_Checks flags
21792 -- must be reset the after processing the record definition.
21794 -- Note: this is the only use of Kill_Range_Checks, and is a bit odd,
21795 -- couldn't we just use the normal range check suppression method here.
21796 -- That would seem cleaner ???
21798 if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
21799 Set_Kill_Range_Checks (T, True);
21800 Record_Type_Definition (Def, Prev);
21801 Set_Kill_Range_Checks (T, False);
21802 else
21803 Record_Type_Definition (Def, Prev);
21804 end if;
21806 -- Exit from record scope
21808 End_Scope;
21810 -- Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
21811 -- the implemented interfaces and associate them an aliased entity.
21813 if Is_Tagged
21814 and then not Is_Empty_List (Interface_List (Def))
21815 then
21816 Derive_Progenitor_Subprograms (T, T);
21817 end if;
21819 Check_Function_Writable_Actuals (N);
21820 end Record_Type_Declaration;
21822 ----------------------------
21823 -- Record_Type_Definition --
21824 ----------------------------
21826 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
21827 Component : Entity_Id;
21828 Ctrl_Components : Boolean := False;
21829 Final_Storage_Only : Boolean;
21830 T : Entity_Id;
21832 begin
21833 if Ekind (Prev_T) = E_Incomplete_Type then
21834 T := Full_View (Prev_T);
21835 else
21836 T := Prev_T;
21837 end if;
21839 -- In SPARK, tagged types and type extensions may only be declared in
21840 -- the specification of library unit packages.
21842 if Present (Def) and then Is_Tagged_Type (T) then
21843 declare
21844 Typ : Node_Id;
21845 Ctxt : Node_Id;
21847 begin
21848 if Nkind (Parent (Def)) = N_Full_Type_Declaration then
21849 Typ := Parent (Def);
21850 else
21851 pragma Assert
21852 (Nkind (Parent (Def)) = N_Derived_Type_Definition);
21853 Typ := Parent (Parent (Def));
21854 end if;
21856 Ctxt := Parent (Typ);
21858 if Nkind (Ctxt) = N_Package_Body
21859 and then Nkind (Parent (Ctxt)) = N_Compilation_Unit
21860 then
21861 Check_SPARK_05_Restriction
21862 ("type should be defined in package specification", Typ);
21864 elsif Nkind (Ctxt) /= N_Package_Specification
21865 or else Nkind (Parent (Parent (Ctxt))) /= N_Compilation_Unit
21866 then
21867 Check_SPARK_05_Restriction
21868 ("type should be defined in library unit package", Typ);
21869 end if;
21870 end;
21871 end if;
21873 Final_Storage_Only := not Is_Controlled (T);
21875 -- Ada 2005: Check whether an explicit Limited is present in a derived
21876 -- type declaration.
21878 if Nkind (Parent (Def)) = N_Derived_Type_Definition
21879 and then Limited_Present (Parent (Def))
21880 then
21881 Set_Is_Limited_Record (T);
21882 end if;
21884 -- If the component list of a record type is defined by the reserved
21885 -- word null and there is no discriminant part, then the record type has
21886 -- no components and all records of the type are null records (RM 3.7)
21887 -- This procedure is also called to process the extension part of a
21888 -- record extension, in which case the current scope may have inherited
21889 -- components.
21891 if No (Def)
21892 or else No (Component_List (Def))
21893 or else Null_Present (Component_List (Def))
21894 then
21895 if not Is_Tagged_Type (T) then
21896 Check_SPARK_05_Restriction ("untagged record cannot be null", Def);
21897 end if;
21899 else
21900 Analyze_Declarations (Component_Items (Component_List (Def)));
21902 if Present (Variant_Part (Component_List (Def))) then
21903 Check_SPARK_05_Restriction ("variant part is not allowed", Def);
21904 Analyze (Variant_Part (Component_List (Def)));
21905 end if;
21906 end if;
21908 -- After completing the semantic analysis of the record definition,
21909 -- record components, both new and inherited, are accessible. Set their
21910 -- kind accordingly. Exclude malformed itypes from illegal declarations,
21911 -- whose Ekind may be void.
21913 Component := First_Entity (Current_Scope);
21914 while Present (Component) loop
21915 if Ekind (Component) = E_Void
21916 and then not Is_Itype (Component)
21917 then
21918 Set_Ekind (Component, E_Component);
21919 Init_Component_Location (Component);
21920 end if;
21922 Propagate_Concurrent_Flags (T, Etype (Component));
21924 if Ekind (Component) /= E_Component then
21925 null;
21927 -- Do not set Has_Controlled_Component on a class-wide equivalent
21928 -- type. See Make_CW_Equivalent_Type.
21930 elsif not Is_Class_Wide_Equivalent_Type (T)
21931 and then (Has_Controlled_Component (Etype (Component))
21932 or else (Chars (Component) /= Name_uParent
21933 and then Is_Controlled (Etype (Component))))
21934 then
21935 Set_Has_Controlled_Component (T, True);
21936 Final_Storage_Only :=
21937 Final_Storage_Only
21938 and then Finalize_Storage_Only (Etype (Component));
21939 Ctrl_Components := True;
21940 end if;
21942 Next_Entity (Component);
21943 end loop;
21945 -- A Type is Finalize_Storage_Only only if all its controlled components
21946 -- are also.
21948 if Ctrl_Components then
21949 Set_Finalize_Storage_Only (T, Final_Storage_Only);
21950 end if;
21952 -- Place reference to end record on the proper entity, which may
21953 -- be a partial view.
21955 if Present (Def) then
21956 Process_End_Label (Def, 'e', Prev_T);
21957 end if;
21958 end Record_Type_Definition;
21960 ------------------------
21961 -- Replace_Components --
21962 ------------------------
21964 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id) is
21965 function Process (N : Node_Id) return Traverse_Result;
21967 -------------
21968 -- Process --
21969 -------------
21971 function Process (N : Node_Id) return Traverse_Result is
21972 Comp : Entity_Id;
21974 begin
21975 if Nkind (N) = N_Discriminant_Specification then
21976 Comp := First_Discriminant (Typ);
21977 while Present (Comp) loop
21978 if Chars (Comp) = Chars (Defining_Identifier (N)) then
21979 Set_Defining_Identifier (N, Comp);
21980 exit;
21981 end if;
21983 Next_Discriminant (Comp);
21984 end loop;
21986 elsif Nkind (N) = N_Variant_Part then
21987 Comp := First_Discriminant (Typ);
21988 while Present (Comp) loop
21989 if Chars (Comp) = Chars (Name (N)) then
21990 Set_Entity (Name (N), Comp);
21991 exit;
21992 end if;
21994 Next_Discriminant (Comp);
21995 end loop;
21997 elsif Nkind (N) = N_Component_Declaration then
21998 Comp := First_Component (Typ);
21999 while Present (Comp) loop
22000 if Chars (Comp) = Chars (Defining_Identifier (N)) then
22001 Set_Defining_Identifier (N, Comp);
22002 exit;
22003 end if;
22005 Next_Component (Comp);
22006 end loop;
22007 end if;
22009 return OK;
22010 end Process;
22012 procedure Replace is new Traverse_Proc (Process);
22014 -- Start of processing for Replace_Components
22016 begin
22017 Replace (Decl);
22018 end Replace_Components;
22020 -------------------------------
22021 -- Set_Completion_Referenced --
22022 -------------------------------
22024 procedure Set_Completion_Referenced (E : Entity_Id) is
22025 begin
22026 -- If in main unit, mark entity that is a completion as referenced,
22027 -- warnings go on the partial view when needed.
22029 if In_Extended_Main_Source_Unit (E) then
22030 Set_Referenced (E);
22031 end if;
22032 end Set_Completion_Referenced;
22034 ---------------------
22035 -- Set_Default_SSO --
22036 ---------------------
22038 procedure Set_Default_SSO (T : Entity_Id) is
22039 begin
22040 case Opt.Default_SSO is
22041 when ' ' =>
22042 null;
22043 when 'L' =>
22044 Set_SSO_Set_Low_By_Default (T, True);
22045 when 'H' =>
22046 Set_SSO_Set_High_By_Default (T, True);
22047 when others =>
22048 raise Program_Error;
22049 end case;
22050 end Set_Default_SSO;
22052 ---------------------
22053 -- Set_Fixed_Range --
22054 ---------------------
22056 -- The range for fixed-point types is complicated by the fact that we
22057 -- do not know the exact end points at the time of the declaration. This
22058 -- is true for three reasons:
22060 -- A size clause may affect the fudging of the end-points.
22061 -- A small clause may affect the values of the end-points.
22062 -- We try to include the end-points if it does not affect the size.
22064 -- This means that the actual end-points must be established at the
22065 -- point when the type is frozen. Meanwhile, we first narrow the range
22066 -- as permitted (so that it will fit if necessary in a small specified
22067 -- size), and then build a range subtree with these narrowed bounds.
22068 -- Set_Fixed_Range constructs the range from real literal values, and
22069 -- sets the range as the Scalar_Range of the given fixed-point type entity.
22071 -- The parent of this range is set to point to the entity so that it is
22072 -- properly hooked into the tree (unlike normal Scalar_Range entries for
22073 -- other scalar types, which are just pointers to the range in the
22074 -- original tree, this would otherwise be an orphan).
22076 -- The tree is left unanalyzed. When the type is frozen, the processing
22077 -- in Freeze.Freeze_Fixed_Point_Type notices that the range is not
22078 -- analyzed, and uses this as an indication that it should complete
22079 -- work on the range (it will know the final small and size values).
22081 procedure Set_Fixed_Range
22082 (E : Entity_Id;
22083 Loc : Source_Ptr;
22084 Lo : Ureal;
22085 Hi : Ureal)
22087 S : constant Node_Id :=
22088 Make_Range (Loc,
22089 Low_Bound => Make_Real_Literal (Loc, Lo),
22090 High_Bound => Make_Real_Literal (Loc, Hi));
22091 begin
22092 Set_Scalar_Range (E, S);
22093 Set_Parent (S, E);
22095 -- Before the freeze point, the bounds of a fixed point are universal
22096 -- and carry the corresponding type.
22098 Set_Etype (Low_Bound (S), Universal_Real);
22099 Set_Etype (High_Bound (S), Universal_Real);
22100 end Set_Fixed_Range;
22102 ----------------------------------
22103 -- Set_Scalar_Range_For_Subtype --
22104 ----------------------------------
22106 procedure Set_Scalar_Range_For_Subtype
22107 (Def_Id : Entity_Id;
22108 R : Node_Id;
22109 Subt : Entity_Id)
22111 Kind : constant Entity_Kind := Ekind (Def_Id);
22113 begin
22114 -- Defend against previous error
22116 if Nkind (R) = N_Error then
22117 return;
22118 end if;
22120 Set_Scalar_Range (Def_Id, R);
22122 -- We need to link the range into the tree before resolving it so
22123 -- that types that are referenced, including importantly the subtype
22124 -- itself, are properly frozen (Freeze_Expression requires that the
22125 -- expression be properly linked into the tree). Of course if it is
22126 -- already linked in, then we do not disturb the current link.
22128 if No (Parent (R)) then
22129 Set_Parent (R, Def_Id);
22130 end if;
22132 -- Reset the kind of the subtype during analysis of the range, to
22133 -- catch possible premature use in the bounds themselves.
22135 Set_Ekind (Def_Id, E_Void);
22136 Process_Range_Expr_In_Decl (R, Subt, Subtyp => Def_Id);
22137 Set_Ekind (Def_Id, Kind);
22138 end Set_Scalar_Range_For_Subtype;
22140 --------------------------------------------------------
22141 -- Set_Stored_Constraint_From_Discriminant_Constraint --
22142 --------------------------------------------------------
22144 procedure Set_Stored_Constraint_From_Discriminant_Constraint
22145 (E : Entity_Id)
22147 begin
22148 -- Make sure set if encountered during Expand_To_Stored_Constraint
22150 Set_Stored_Constraint (E, No_Elist);
22152 -- Give it the right value
22154 if Is_Constrained (E) and then Has_Discriminants (E) then
22155 Set_Stored_Constraint (E,
22156 Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
22157 end if;
22158 end Set_Stored_Constraint_From_Discriminant_Constraint;
22160 -------------------------------------
22161 -- Signed_Integer_Type_Declaration --
22162 -------------------------------------
22164 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
22165 Implicit_Base : Entity_Id;
22166 Base_Typ : Entity_Id;
22167 Lo_Val : Uint;
22168 Hi_Val : Uint;
22169 Errs : Boolean := False;
22170 Lo : Node_Id;
22171 Hi : Node_Id;
22173 function Can_Derive_From (E : Entity_Id) return Boolean;
22174 -- Determine whether given bounds allow derivation from specified type
22176 procedure Check_Bound (Expr : Node_Id);
22177 -- Check bound to make sure it is integral and static. If not, post
22178 -- appropriate error message and set Errs flag
22180 ---------------------
22181 -- Can_Derive_From --
22182 ---------------------
22184 -- Note we check both bounds against both end values, to deal with
22185 -- strange types like ones with a range of 0 .. -12341234.
22187 function Can_Derive_From (E : Entity_Id) return Boolean is
22188 Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
22189 Hi : constant Uint := Expr_Value (Type_High_Bound (E));
22190 begin
22191 return Lo <= Lo_Val and then Lo_Val <= Hi
22192 and then
22193 Lo <= Hi_Val and then Hi_Val <= Hi;
22194 end Can_Derive_From;
22196 -----------------
22197 -- Check_Bound --
22198 -----------------
22200 procedure Check_Bound (Expr : Node_Id) is
22201 begin
22202 -- If a range constraint is used as an integer type definition, each
22203 -- bound of the range must be defined by a static expression of some
22204 -- integer type, but the two bounds need not have the same integer
22205 -- type (Negative bounds are allowed.) (RM 3.5.4)
22207 if not Is_Integer_Type (Etype (Expr)) then
22208 Error_Msg_N
22209 ("integer type definition bounds must be of integer type", Expr);
22210 Errs := True;
22212 elsif not Is_OK_Static_Expression (Expr) then
22213 Flag_Non_Static_Expr
22214 ("non-static expression used for integer type bound!", Expr);
22215 Errs := True;
22217 -- The bounds are folded into literals, and we set their type to be
22218 -- universal, to avoid typing difficulties: we cannot set the type
22219 -- of the literal to the new type, because this would be a forward
22220 -- reference for the back end, and if the original type is user-
22221 -- defined this can lead to spurious semantic errors (e.g. 2928-003).
22223 else
22224 if Is_Entity_Name (Expr) then
22225 Fold_Uint (Expr, Expr_Value (Expr), True);
22226 end if;
22228 Set_Etype (Expr, Universal_Integer);
22229 end if;
22230 end Check_Bound;
22232 -- Start of processing for Signed_Integer_Type_Declaration
22234 begin
22235 -- Create an anonymous base type
22237 Implicit_Base :=
22238 Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
22240 -- Analyze and check the bounds, they can be of any integer type
22242 Lo := Low_Bound (Def);
22243 Hi := High_Bound (Def);
22245 -- Arbitrarily use Integer as the type if either bound had an error
22247 if Hi = Error or else Lo = Error then
22248 Base_Typ := Any_Integer;
22249 Set_Error_Posted (T, True);
22251 -- Here both bounds are OK expressions
22253 else
22254 Analyze_And_Resolve (Lo, Any_Integer);
22255 Analyze_And_Resolve (Hi, Any_Integer);
22257 Check_Bound (Lo);
22258 Check_Bound (Hi);
22260 if Errs then
22261 Hi := Type_High_Bound (Standard_Long_Long_Integer);
22262 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
22263 end if;
22265 -- Find type to derive from
22267 Lo_Val := Expr_Value (Lo);
22268 Hi_Val := Expr_Value (Hi);
22270 if Can_Derive_From (Standard_Short_Short_Integer) then
22271 Base_Typ := Base_Type (Standard_Short_Short_Integer);
22273 elsif Can_Derive_From (Standard_Short_Integer) then
22274 Base_Typ := Base_Type (Standard_Short_Integer);
22276 elsif Can_Derive_From (Standard_Integer) then
22277 Base_Typ := Base_Type (Standard_Integer);
22279 elsif Can_Derive_From (Standard_Long_Integer) then
22280 Base_Typ := Base_Type (Standard_Long_Integer);
22282 elsif Can_Derive_From (Standard_Long_Long_Integer) then
22283 Check_Restriction (No_Long_Long_Integers, Def);
22284 Base_Typ := Base_Type (Standard_Long_Long_Integer);
22286 else
22287 Base_Typ := Base_Type (Standard_Long_Long_Integer);
22288 Error_Msg_N ("integer type definition bounds out of range", Def);
22289 Hi := Type_High_Bound (Standard_Long_Long_Integer);
22290 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
22291 end if;
22292 end if;
22294 -- Complete both implicit base and declared first subtype entities. The
22295 -- inheritance of the rep item chain ensures that SPARK-related pragmas
22296 -- are not clobbered when the signed integer type acts as a full view of
22297 -- a private type.
22299 Set_Etype (Implicit_Base, Base_Typ);
22300 Set_Size_Info (Implicit_Base, Base_Typ);
22301 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
22302 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
22303 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
22305 Set_Ekind (T, E_Signed_Integer_Subtype);
22306 Set_Etype (T, Implicit_Base);
22307 Set_Size_Info (T, Implicit_Base);
22308 Inherit_Rep_Item_Chain (T, Implicit_Base);
22309 Set_Scalar_Range (T, Def);
22310 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
22311 Set_Is_Constrained (T);
22312 end Signed_Integer_Type_Declaration;
22314 end Sem_Ch3;