2017-01-23 Hristian Kirtchev <kirtchev@adacore.com>
[official-gcc.git] / gcc / ada / sem_ch3.adb
blob79127a38ffde09d29b2d0e15feba02cc9c04047e
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-2016, 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_Ch7; use Exp_Ch7;
37 with Exp_Ch9; use Exp_Ch9;
38 with Exp_Disp; use Exp_Disp;
39 with Exp_Dist; use Exp_Dist;
40 with Exp_Tss; use Exp_Tss;
41 with Exp_Util; use Exp_Util;
42 with Fname; use Fname;
43 with Freeze; use Freeze;
44 with Ghost; use Ghost;
45 with Itypes; use Itypes;
46 with Layout; use Layout;
47 with Lib; use Lib;
48 with Lib.Xref; use Lib.Xref;
49 with Namet; use Namet;
50 with Nmake; use Nmake;
51 with Opt; use Opt;
52 with Restrict; use Restrict;
53 with Rident; use Rident;
54 with Rtsfind; use Rtsfind;
55 with Sem; use Sem;
56 with Sem_Aux; use Sem_Aux;
57 with Sem_Case; use Sem_Case;
58 with Sem_Cat; use Sem_Cat;
59 with Sem_Ch6; use Sem_Ch6;
60 with Sem_Ch7; use Sem_Ch7;
61 with Sem_Ch8; use Sem_Ch8;
62 with Sem_Ch13; use Sem_Ch13;
63 with Sem_Dim; use Sem_Dim;
64 with Sem_Disp; use Sem_Disp;
65 with Sem_Dist; use Sem_Dist;
66 with Sem_Elim; use Sem_Elim;
67 with Sem_Eval; use Sem_Eval;
68 with Sem_Mech; use Sem_Mech;
69 with Sem_Res; use Sem_Res;
70 with Sem_Smem; use Sem_Smem;
71 with Sem_Type; use Sem_Type;
72 with Sem_Util; use Sem_Util;
73 with Sem_Warn; use Sem_Warn;
74 with Stand; use Stand;
75 with Sinfo; use Sinfo;
76 with Sinput; use Sinput;
77 with Snames; use Snames;
78 with Targparm; use Targparm;
79 with Tbuild; use Tbuild;
80 with Ttypes; use Ttypes;
81 with Uintp; use Uintp;
82 with Urealp; use Urealp;
84 package body Sem_Ch3 is
86 -----------------------
87 -- Local Subprograms --
88 -----------------------
90 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id);
91 -- Ada 2005 (AI-251): Add the tag components corresponding to all the
92 -- abstract interface types implemented by a record type or a derived
93 -- record type.
95 procedure Build_Derived_Type
96 (N : Node_Id;
97 Parent_Type : Entity_Id;
98 Derived_Type : Entity_Id;
99 Is_Completion : Boolean;
100 Derive_Subps : Boolean := True);
101 -- Create and decorate a Derived_Type given the Parent_Type entity. N is
102 -- the N_Full_Type_Declaration node containing the derived type definition.
103 -- Parent_Type is the entity for the parent type in the derived type
104 -- definition and Derived_Type the actual derived type. Is_Completion must
105 -- be set to False if Derived_Type is the N_Defining_Identifier node in N
106 -- (i.e. Derived_Type = Defining_Identifier (N)). In this case N is not the
107 -- completion of a private type declaration. If Is_Completion is set to
108 -- True, N is the completion of a private type declaration and Derived_Type
109 -- is different from the defining identifier inside N (i.e. Derived_Type /=
110 -- Defining_Identifier (N)). Derive_Subps indicates whether the parent
111 -- subprograms should be derived. The only case where this parameter is
112 -- False is when Build_Derived_Type is recursively called to process an
113 -- implicit derived full type for a type derived from a private type (in
114 -- that case the subprograms must only be derived for the private view of
115 -- the type).
117 -- ??? These flags need a bit of re-examination and re-documentation:
118 -- ??? are they both necessary (both seem related to the recursion)?
120 procedure Build_Derived_Access_Type
121 (N : Node_Id;
122 Parent_Type : Entity_Id;
123 Derived_Type : Entity_Id);
124 -- Subsidiary procedure to Build_Derived_Type. For a derived access type,
125 -- create an implicit base if the parent type is constrained or if the
126 -- subtype indication has a constraint.
128 procedure Build_Derived_Array_Type
129 (N : Node_Id;
130 Parent_Type : Entity_Id;
131 Derived_Type : Entity_Id);
132 -- Subsidiary procedure to Build_Derived_Type. For a derived array type,
133 -- create an implicit base if the parent type is constrained or if the
134 -- subtype indication has a constraint.
136 procedure Build_Derived_Concurrent_Type
137 (N : Node_Id;
138 Parent_Type : Entity_Id;
139 Derived_Type : Entity_Id);
140 -- Subsidiary procedure to Build_Derived_Type. For a derived task or
141 -- protected type, inherit entries and protected subprograms, check
142 -- legality of discriminant constraints if any.
144 procedure Build_Derived_Enumeration_Type
145 (N : Node_Id;
146 Parent_Type : Entity_Id;
147 Derived_Type : Entity_Id);
148 -- Subsidiary procedure to Build_Derived_Type. For a derived enumeration
149 -- type, we must create a new list of literals. Types derived from
150 -- Character and [Wide_]Wide_Character are special-cased.
152 procedure Build_Derived_Numeric_Type
153 (N : Node_Id;
154 Parent_Type : Entity_Id;
155 Derived_Type : Entity_Id);
156 -- Subsidiary procedure to Build_Derived_Type. For numeric types, create
157 -- an anonymous base type, and propagate constraint to subtype if needed.
159 procedure Build_Derived_Private_Type
160 (N : Node_Id;
161 Parent_Type : Entity_Id;
162 Derived_Type : Entity_Id;
163 Is_Completion : Boolean;
164 Derive_Subps : Boolean := True);
165 -- Subsidiary procedure to Build_Derived_Type. This procedure is complex
166 -- because the parent may or may not have a completion, and the derivation
167 -- may itself be a completion.
169 procedure Build_Derived_Record_Type
170 (N : Node_Id;
171 Parent_Type : Entity_Id;
172 Derived_Type : Entity_Id;
173 Derive_Subps : Boolean := True);
174 -- Subsidiary procedure used for tagged and untagged record types
175 -- by Build_Derived_Type and Analyze_Private_Extension_Declaration.
176 -- All parameters are as in Build_Derived_Type except that N, in
177 -- addition to being an N_Full_Type_Declaration node, can also be an
178 -- N_Private_Extension_Declaration node. See the definition of this routine
179 -- for much more info. Derive_Subps indicates whether subprograms should be
180 -- derived from the parent type. The only case where Derive_Subps is False
181 -- is for an implicit derived full type for a type derived from a private
182 -- type (see Build_Derived_Type).
184 procedure Build_Discriminal (Discrim : Entity_Id);
185 -- Create the discriminal corresponding to discriminant Discrim, that is
186 -- the parameter corresponding to Discrim to be used in initialization
187 -- procedures for the type where Discrim is a discriminant. Discriminals
188 -- are not used during semantic analysis, and are not fully defined
189 -- entities until expansion. Thus they are not given a scope until
190 -- initialization procedures are built.
192 function Build_Discriminant_Constraints
193 (T : Entity_Id;
194 Def : Node_Id;
195 Derived_Def : Boolean := False) return Elist_Id;
196 -- Validate discriminant constraints and return the list of the constraints
197 -- in order of discriminant declarations, where T is the discriminated
198 -- unconstrained type. Def is the N_Subtype_Indication node where the
199 -- discriminants constraints for T are specified. Derived_Def is True
200 -- when building the discriminant constraints in a derived type definition
201 -- of the form "type D (...) is new T (xxx)". In this case T is the parent
202 -- type and Def is the constraint "(xxx)" on T and this routine sets the
203 -- Corresponding_Discriminant field of the discriminants in the derived
204 -- type D to point to the corresponding discriminants in the parent type T.
206 procedure Build_Discriminated_Subtype
207 (T : Entity_Id;
208 Def_Id : Entity_Id;
209 Elist : Elist_Id;
210 Related_Nod : Node_Id;
211 For_Access : Boolean := False);
212 -- Subsidiary procedure to Constrain_Discriminated_Type and to
213 -- Process_Incomplete_Dependents. Given
215 -- T (a possibly discriminated base type)
216 -- Def_Id (a very partially built subtype for T),
218 -- the call completes Def_Id to be the appropriate E_*_Subtype.
220 -- The Elist is the list of discriminant constraints if any (it is set
221 -- to No_Elist if T is not a discriminated type, and to an empty list if
222 -- T has discriminants but there are no discriminant constraints). The
223 -- Related_Nod is the same as Decl_Node in Create_Constrained_Components.
224 -- The For_Access says whether or not this subtype is really constraining
225 -- an access type. That is its sole purpose is the designated type of an
226 -- access type -- in which case a Private_Subtype Is_For_Access_Subtype
227 -- is built to avoid freezing T when the access subtype is frozen.
229 function Build_Scalar_Bound
230 (Bound : Node_Id;
231 Par_T : Entity_Id;
232 Der_T : Entity_Id) return Node_Id;
233 -- The bounds of a derived scalar type are conversions of the bounds of
234 -- the parent type. Optimize the representation if the bounds are literals.
235 -- Needs a more complete spec--what are the parameters exactly, and what
236 -- exactly is the returned value, and how is Bound affected???
238 procedure Build_Underlying_Full_View
239 (N : Node_Id;
240 Typ : Entity_Id;
241 Par : Entity_Id);
242 -- If the completion of a private type is itself derived from a private
243 -- type, or if the full view of a private subtype is itself private, the
244 -- back-end has no way to compute the actual size of this type. We build
245 -- an internal subtype declaration of the proper parent type to convey
246 -- this information. This extra mechanism is needed because a full
247 -- view cannot itself have a full view (it would get clobbered during
248 -- view exchanges).
250 procedure Check_Access_Discriminant_Requires_Limited
251 (D : Node_Id;
252 Loc : Node_Id);
253 -- Check the restriction that the type to which an access discriminant
254 -- belongs must be a concurrent type or a descendant of a type with
255 -- the reserved word 'limited' in its declaration.
257 procedure Check_Anonymous_Access_Components
258 (Typ_Decl : Node_Id;
259 Typ : Entity_Id;
260 Prev : Entity_Id;
261 Comp_List : Node_Id);
262 -- Ada 2005 AI-382: an access component in a record definition can refer to
263 -- the enclosing record, in which case it denotes the type itself, and not
264 -- the current instance of the type. We create an anonymous access type for
265 -- the component, and flag it as an access to a component, so accessibility
266 -- checks are properly performed on it. The declaration of the access type
267 -- is placed ahead of that of the record to prevent order-of-elaboration
268 -- circularity issues in Gigi. We create an incomplete type for the record
269 -- declaration, which is the designated type of the anonymous access.
271 procedure Check_Delta_Expression (E : Node_Id);
272 -- Check that the expression represented by E is suitable for use as a
273 -- delta expression, i.e. it is of real type and is static.
275 procedure Check_Digits_Expression (E : Node_Id);
276 -- Check that the expression represented by E is suitable for use as a
277 -- digits expression, i.e. it is of integer type, positive and static.
279 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id);
280 -- Validate the initialization of an object declaration. T is the required
281 -- type, and Exp is the initialization expression.
283 procedure Check_Interfaces (N : Node_Id; Def : Node_Id);
284 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
286 procedure Check_Or_Process_Discriminants
287 (N : Node_Id;
288 T : Entity_Id;
289 Prev : Entity_Id := Empty);
290 -- If N is the full declaration of the completion T of an incomplete or
291 -- private type, check its discriminants (which are already known to be
292 -- conformant with those of the partial view, see Find_Type_Name),
293 -- otherwise process them. Prev is the entity of the partial declaration,
294 -- if any.
296 procedure Check_Real_Bound (Bound : Node_Id);
297 -- Check given bound for being of real type and static. If not, post an
298 -- appropriate message, and rewrite the bound with the real literal zero.
300 procedure Constant_Redeclaration
301 (Id : Entity_Id;
302 N : Node_Id;
303 T : out Entity_Id);
304 -- Various checks on legality of full declaration of deferred constant.
305 -- Id is the entity for the redeclaration, N is the N_Object_Declaration,
306 -- node. The caller has not yet set any attributes of this entity.
308 function Contain_Interface
309 (Iface : Entity_Id;
310 Ifaces : Elist_Id) return Boolean;
311 -- Ada 2005: Determine whether Iface is present in the list Ifaces
313 procedure Convert_Scalar_Bounds
314 (N : Node_Id;
315 Parent_Type : Entity_Id;
316 Derived_Type : Entity_Id;
317 Loc : Source_Ptr);
318 -- For derived scalar types, convert the bounds in the type definition to
319 -- the derived type, and complete their analysis. Given a constraint of the
320 -- form ".. new T range Lo .. Hi", Lo and Hi are analyzed and resolved with
321 -- T'Base, the parent_type. The bounds of the derived type (the anonymous
322 -- base) are copies of Lo and Hi. Finally, the bounds of the derived
323 -- subtype are conversions of those bounds to the derived_type, so that
324 -- their typing is consistent.
326 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id);
327 -- Copies attributes from array base type T2 to array base type T1. Copies
328 -- only attributes that apply to base types, but not subtypes.
330 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id);
331 -- Copies attributes from array subtype T2 to array subtype T1. Copies
332 -- attributes that apply to both subtypes and base types.
334 procedure Create_Constrained_Components
335 (Subt : Entity_Id;
336 Decl_Node : Node_Id;
337 Typ : Entity_Id;
338 Constraints : Elist_Id);
339 -- Build the list of entities for a constrained discriminated record
340 -- subtype. If a component depends on a discriminant, replace its subtype
341 -- using the discriminant values in the discriminant constraint. Subt
342 -- is the defining identifier for the subtype whose list of constrained
343 -- entities we will create. Decl_Node is the type declaration node where
344 -- we will attach all the itypes created. Typ is the base discriminated
345 -- type for the subtype Subt. Constraints is the list of discriminant
346 -- constraints for Typ.
348 function Constrain_Component_Type
349 (Comp : Entity_Id;
350 Constrained_Typ : Entity_Id;
351 Related_Node : Node_Id;
352 Typ : Entity_Id;
353 Constraints : Elist_Id) return Entity_Id;
354 -- Given a discriminated base type Typ, a list of discriminant constraints,
355 -- Constraints, for Typ and a component Comp of Typ, create and return the
356 -- type corresponding to Etype (Comp) where all discriminant references
357 -- are replaced with the corresponding constraint. If Etype (Comp) contains
358 -- no discriminant references then it is returned as-is. Constrained_Typ
359 -- is the final constrained subtype to which the constrained component
360 -- belongs. Related_Node is the node where we attach all created itypes.
362 procedure Constrain_Access
363 (Def_Id : in out Entity_Id;
364 S : Node_Id;
365 Related_Nod : Node_Id);
366 -- Apply a list of constraints to an access type. If Def_Id is empty, it is
367 -- an anonymous type created for a subtype indication. In that case it is
368 -- created in the procedure and attached to Related_Nod.
370 procedure Constrain_Array
371 (Def_Id : in out Entity_Id;
372 SI : Node_Id;
373 Related_Nod : Node_Id;
374 Related_Id : Entity_Id;
375 Suffix : Character);
376 -- Apply a list of index constraints to an unconstrained array type. The
377 -- first parameter is the entity for the resulting subtype. A value of
378 -- Empty for Def_Id indicates that an implicit type must be created, but
379 -- creation is delayed (and must be done by this procedure) because other
380 -- subsidiary implicit types must be created first (which is why Def_Id
381 -- is an in/out parameter). The second parameter is a subtype indication
382 -- node for the constrained array to be created (e.g. something of the
383 -- form string (1 .. 10)). Related_Nod gives the place where this type
384 -- has to be inserted in the tree. The Related_Id and Suffix parameters
385 -- are used to build the associated Implicit type name.
387 procedure Constrain_Concurrent
388 (Def_Id : in out Entity_Id;
389 SI : Node_Id;
390 Related_Nod : Node_Id;
391 Related_Id : Entity_Id;
392 Suffix : Character);
393 -- Apply list of discriminant constraints to an unconstrained concurrent
394 -- type.
396 -- SI is the N_Subtype_Indication node containing the constraint and
397 -- the unconstrained type to constrain.
399 -- Def_Id is the entity for the resulting constrained subtype. A value
400 -- of Empty for Def_Id indicates that an implicit type must be created,
401 -- but creation is delayed (and must be done by this procedure) because
402 -- other subsidiary implicit types must be created first (which is why
403 -- Def_Id is an in/out parameter).
405 -- Related_Nod gives the place where this type has to be inserted
406 -- in the tree.
408 -- The last two arguments are used to create its external name if needed.
410 function Constrain_Corresponding_Record
411 (Prot_Subt : Entity_Id;
412 Corr_Rec : Entity_Id;
413 Related_Nod : Node_Id) return Entity_Id;
414 -- When constraining a protected type or task type with discriminants,
415 -- constrain the corresponding record with the same discriminant values.
417 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id);
418 -- Constrain a decimal fixed point type with a digits constraint and/or a
419 -- range constraint, and build E_Decimal_Fixed_Point_Subtype entity.
421 procedure Constrain_Discriminated_Type
422 (Def_Id : Entity_Id;
423 S : Node_Id;
424 Related_Nod : Node_Id;
425 For_Access : Boolean := False);
426 -- Process discriminant constraints of composite type. Verify that values
427 -- have been provided for all discriminants, that the original type is
428 -- unconstrained, and that the types of the supplied expressions match
429 -- the discriminant types. The first three parameters are like in routine
430 -- Constrain_Concurrent. See Build_Discriminated_Subtype for an explanation
431 -- of For_Access.
433 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id);
434 -- Constrain an enumeration type with a range constraint. This is identical
435 -- to Constrain_Integer, but for the Ekind of the resulting subtype.
437 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id);
438 -- Constrain a floating point type with either a digits constraint
439 -- and/or a range constraint, building a E_Floating_Point_Subtype.
441 procedure Constrain_Index
442 (Index : Node_Id;
443 S : Node_Id;
444 Related_Nod : Node_Id;
445 Related_Id : Entity_Id;
446 Suffix : Character;
447 Suffix_Index : Nat);
448 -- Process an index constraint S in a constrained array declaration. The
449 -- constraint can be a subtype name, or a range with or without an explicit
450 -- subtype mark. The index is the corresponding index of the unconstrained
451 -- array. The Related_Id and Suffix parameters are used to build the
452 -- associated Implicit type name.
454 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id);
455 -- Build subtype of a signed or modular integer type
457 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id);
458 -- Constrain an ordinary fixed point type with a range constraint, and
459 -- build an E_Ordinary_Fixed_Point_Subtype entity.
461 procedure Copy_And_Swap (Priv, Full : Entity_Id);
462 -- Copy the Priv entity into the entity of its full declaration then swap
463 -- the two entities in such a manner that the former private type is now
464 -- seen as a full type.
466 procedure Decimal_Fixed_Point_Type_Declaration
467 (T : Entity_Id;
468 Def : Node_Id);
469 -- Create a new decimal fixed point type, and apply the constraint to
470 -- obtain a subtype of this new type.
472 procedure Complete_Private_Subtype
473 (Priv : Entity_Id;
474 Full : Entity_Id;
475 Full_Base : Entity_Id;
476 Related_Nod : Node_Id);
477 -- Complete the implicit full view of a private subtype by setting the
478 -- appropriate semantic fields. If the full view of the parent is a record
479 -- type, build constrained components of subtype.
481 procedure Derive_Progenitor_Subprograms
482 (Parent_Type : Entity_Id;
483 Tagged_Type : Entity_Id);
484 -- Ada 2005 (AI-251): To complete type derivation, collect the primitive
485 -- operations of progenitors of Tagged_Type, and replace the subsidiary
486 -- subtypes with Tagged_Type, to build the specs of the inherited interface
487 -- primitives. The derived primitives are aliased to those of the
488 -- interface. This routine takes care also of transferring to the full view
489 -- subprograms associated with the partial view of Tagged_Type that cover
490 -- interface primitives.
492 procedure Derived_Standard_Character
493 (N : Node_Id;
494 Parent_Type : Entity_Id;
495 Derived_Type : Entity_Id);
496 -- Subsidiary procedure to Build_Derived_Enumeration_Type which handles
497 -- derivations from types Standard.Character and Standard.Wide_Character.
499 procedure Derived_Type_Declaration
500 (T : Entity_Id;
501 N : Node_Id;
502 Is_Completion : Boolean);
503 -- Process a derived type declaration. Build_Derived_Type is invoked
504 -- to process the actual derived type definition. Parameters N and
505 -- Is_Completion have the same meaning as in Build_Derived_Type.
506 -- T is the N_Defining_Identifier for the entity defined in the
507 -- N_Full_Type_Declaration node N, that is T is the derived type.
509 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id);
510 -- Insert each literal in symbol table, as an overloadable identifier. Each
511 -- enumeration type is mapped into a sequence of integers, and each literal
512 -- is defined as a constant with integer value. If any of the literals are
513 -- character literals, the type is a character type, which means that
514 -- strings are legal aggregates for arrays of components of the type.
516 function Expand_To_Stored_Constraint
517 (Typ : Entity_Id;
518 Constraint : Elist_Id) return Elist_Id;
519 -- Given a constraint (i.e. a list of expressions) on the discriminants of
520 -- Typ, expand it into a constraint on the stored discriminants and return
521 -- the new list of expressions constraining the stored discriminants.
523 function Find_Type_Of_Object
524 (Obj_Def : Node_Id;
525 Related_Nod : Node_Id) return Entity_Id;
526 -- Get type entity for object referenced by Obj_Def, attaching the implicit
527 -- types generated to Related_Nod.
529 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id);
530 -- Create a new float and apply the constraint to obtain subtype of it
532 function Has_Range_Constraint (N : Node_Id) return Boolean;
533 -- Given an N_Subtype_Indication node N, return True if a range constraint
534 -- is present, either directly, or as part of a digits or delta constraint.
535 -- In addition, a digits constraint in the decimal case returns True, since
536 -- it establishes a default range if no explicit range is present.
538 function Inherit_Components
539 (N : Node_Id;
540 Parent_Base : Entity_Id;
541 Derived_Base : Entity_Id;
542 Is_Tagged : Boolean;
543 Inherit_Discr : Boolean;
544 Discs : Elist_Id) return Elist_Id;
545 -- Called from Build_Derived_Record_Type to inherit the components of
546 -- Parent_Base (a base type) into the Derived_Base (the derived base type).
547 -- For more information on derived types and component inheritance please
548 -- consult the comment above the body of Build_Derived_Record_Type.
550 -- N is the original derived type declaration
552 -- Is_Tagged is set if we are dealing with tagged types
554 -- If Inherit_Discr is set, Derived_Base inherits its discriminants from
555 -- Parent_Base, otherwise no discriminants are inherited.
557 -- Discs gives the list of constraints that apply to Parent_Base in the
558 -- derived type declaration. If Discs is set to No_Elist, then we have
559 -- the following situation:
561 -- type Parent (D1..Dn : ..) is [tagged] record ...;
562 -- type Derived is new Parent [with ...];
564 -- which gets treated as
566 -- type Derived (D1..Dn : ..) is new Parent (D1,..,Dn) [with ...];
568 -- For untagged types the returned value is an association list. The list
569 -- starts from the association (Parent_Base => Derived_Base), and then it
570 -- contains a sequence of the associations of the form
572 -- (Old_Component => New_Component),
574 -- where Old_Component is the Entity_Id of a component in Parent_Base and
575 -- New_Component is the Entity_Id of the corresponding component in
576 -- Derived_Base. For untagged records, this association list is needed when
577 -- copying the record declaration for the derived base. In the tagged case
578 -- the value returned is irrelevant.
580 procedure Inherit_Predicate_Flags (Subt, Par : Entity_Id);
581 -- Propagate static and dynamic predicate flags from a parent to the
582 -- subtype in a subtype declaration with and without constraints.
584 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean;
585 -- Subsidiary to Check_Abstract_Overriding and Derive_Subprogram.
586 -- Determine whether subprogram Subp is a procedure subject to pragma
587 -- Extensions_Visible with value False and has at least one controlling
588 -- parameter of mode OUT.
590 function Is_Valid_Constraint_Kind
591 (T_Kind : Type_Kind;
592 Constraint_Kind : Node_Kind) return Boolean;
593 -- Returns True if it is legal to apply the given kind of constraint to the
594 -- given kind of type (index constraint to an array type, for example).
596 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id);
597 -- Create new modular type. Verify that modulus is in bounds
599 procedure New_Concatenation_Op (Typ : Entity_Id);
600 -- Create an abbreviated declaration for an operator in order to
601 -- materialize concatenation on array types.
603 procedure Ordinary_Fixed_Point_Type_Declaration
604 (T : Entity_Id;
605 Def : Node_Id);
606 -- Create a new ordinary fixed point type, and apply the constraint to
607 -- obtain subtype of it.
609 procedure Prepare_Private_Subtype_Completion
610 (Id : Entity_Id;
611 Related_Nod : Node_Id);
612 -- Id is a subtype of some private type. Creates the full declaration
613 -- associated with Id whenever possible, i.e. when the full declaration
614 -- of the base type is already known. Records each subtype into
615 -- Private_Dependents of the base type.
617 procedure Process_Incomplete_Dependents
618 (N : Node_Id;
619 Full_T : Entity_Id;
620 Inc_T : Entity_Id);
621 -- Process all entities that depend on an incomplete type. There include
622 -- subtypes, subprogram types that mention the incomplete type in their
623 -- profiles, and subprogram with access parameters that designate the
624 -- incomplete type.
626 -- Inc_T is the defining identifier of an incomplete type declaration, its
627 -- Ekind is E_Incomplete_Type.
629 -- N is the corresponding N_Full_Type_Declaration for Inc_T.
631 -- Full_T is N's defining identifier.
633 -- Subtypes of incomplete types with discriminants are completed when the
634 -- parent type is. This is simpler than private subtypes, because they can
635 -- only appear in the same scope, and there is no need to exchange views.
636 -- Similarly, access_to_subprogram types may have a parameter or a return
637 -- type that is an incomplete type, and that must be replaced with the
638 -- full type.
640 -- If the full type is tagged, subprogram with access parameters that
641 -- designated the incomplete may be primitive operations of the full type,
642 -- and have to be processed accordingly.
644 procedure Process_Real_Range_Specification (Def : Node_Id);
645 -- Given the type definition for a real type, this procedure processes and
646 -- checks the real range specification of this type definition if one is
647 -- present. If errors are found, error messages are posted, and the
648 -- Real_Range_Specification of Def is reset to Empty.
650 procedure Record_Type_Declaration
651 (T : Entity_Id;
652 N : Node_Id;
653 Prev : Entity_Id);
654 -- Process a record type declaration (for both untagged and tagged
655 -- records). Parameters T and N are exactly like in procedure
656 -- Derived_Type_Declaration, except that no flag Is_Completion is needed
657 -- for this routine. If this is the completion of an incomplete type
658 -- declaration, Prev is the entity of the incomplete declaration, used for
659 -- cross-referencing. Otherwise Prev = T.
661 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id);
662 -- This routine is used to process the actual record type definition (both
663 -- for untagged and tagged records). Def is a record type definition node.
664 -- This procedure analyzes the components in this record type definition.
665 -- Prev_T is the entity for the enclosing record type. It is provided so
666 -- that its Has_Task flag can be set if any of the component have Has_Task
667 -- set. If the declaration is the completion of an incomplete type
668 -- declaration, Prev_T is the original incomplete type, whose full view is
669 -- the record type.
671 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id);
672 -- Subsidiary to Build_Derived_Record_Type. For untagged records, we
673 -- build a copy of the declaration tree of the parent, and we create
674 -- independently the list of components for the derived type. Semantic
675 -- information uses the component entities, but record representation
676 -- clauses are validated on the declaration tree. This procedure replaces
677 -- discriminants and components in the declaration with those that have
678 -- been created by Inherit_Components.
680 procedure Set_Fixed_Range
681 (E : Entity_Id;
682 Loc : Source_Ptr;
683 Lo : Ureal;
684 Hi : Ureal);
685 -- Build a range node with the given bounds and set it as the Scalar_Range
686 -- of the given fixed-point type entity. Loc is the source location used
687 -- for the constructed range. See body for further details.
689 procedure Set_Scalar_Range_For_Subtype
690 (Def_Id : Entity_Id;
691 R : Node_Id;
692 Subt : Entity_Id);
693 -- This routine is used to set the scalar range field for a subtype given
694 -- Def_Id, the entity for the subtype, and R, the range expression for the
695 -- scalar range. Subt provides the parent subtype to be used to analyze,
696 -- resolve, and check the given range.
698 procedure Set_Default_SSO (T : Entity_Id);
699 -- T is the entity for an array or record being declared. This procedure
700 -- sets the flags SSO_Set_Low_By_Default/SSO_Set_High_By_Default according
701 -- to the setting of Opt.Default_SSO.
703 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id);
704 -- Create a new signed integer entity, and apply the constraint to obtain
705 -- the required first named subtype of this type.
707 procedure Set_Stored_Constraint_From_Discriminant_Constraint
708 (E : Entity_Id);
709 -- E is some record type. This routine computes E's Stored_Constraint
710 -- from its Discriminant_Constraint.
712 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id);
713 -- Check that an entity in a list of progenitors is an interface,
714 -- emit error otherwise.
716 -----------------------
717 -- Access_Definition --
718 -----------------------
720 function Access_Definition
721 (Related_Nod : Node_Id;
722 N : Node_Id) return Entity_Id
724 Anon_Type : Entity_Id;
725 Anon_Scope : Entity_Id;
726 Desig_Type : Entity_Id;
727 Enclosing_Prot_Type : Entity_Id := Empty;
729 begin
730 Check_SPARK_05_Restriction ("access type is not allowed", N);
732 if Is_Entry (Current_Scope)
733 and then Is_Task_Type (Etype (Scope (Current_Scope)))
734 then
735 Error_Msg_N ("task entries cannot have access parameters", N);
736 return Empty;
737 end if;
739 -- Ada 2005: For an object declaration the corresponding anonymous
740 -- type is declared in the current scope.
742 -- If the access definition is the return type of another access to
743 -- function, scope is the current one, because it is the one of the
744 -- current type declaration, except for the pathological case below.
746 if Nkind_In (Related_Nod, N_Object_Declaration,
747 N_Access_Function_Definition)
748 then
749 Anon_Scope := Current_Scope;
751 -- A pathological case: function returning access functions that
752 -- return access functions, etc. Each anonymous access type created
753 -- is in the enclosing scope of the outermost function.
755 declare
756 Par : Node_Id;
758 begin
759 Par := Related_Nod;
760 while Nkind_In (Par, N_Access_Function_Definition,
761 N_Access_Definition)
762 loop
763 Par := Parent (Par);
764 end loop;
766 if Nkind (Par) = N_Function_Specification then
767 Anon_Scope := Scope (Defining_Entity (Par));
768 end if;
769 end;
771 -- For the anonymous function result case, retrieve the scope of the
772 -- function specification's associated entity rather than using the
773 -- current scope. The current scope will be the function itself if the
774 -- formal part is currently being analyzed, but will be the parent scope
775 -- in the case of a parameterless function, and we always want to use
776 -- the function's parent scope. Finally, if the function is a child
777 -- unit, we must traverse the tree to retrieve the proper entity.
779 elsif Nkind (Related_Nod) = N_Function_Specification
780 and then Nkind (Parent (N)) /= N_Parameter_Specification
781 then
782 -- If the current scope is a protected type, the anonymous access
783 -- is associated with one of the protected operations, and must
784 -- be available in the scope that encloses the protected declaration.
785 -- Otherwise the type is in the scope enclosing the subprogram.
787 -- If the function has formals, The return type of a subprogram
788 -- declaration is analyzed in the scope of the subprogram (see
789 -- Process_Formals) and thus the protected type, if present, is
790 -- the scope of the current function scope.
792 if Ekind (Current_Scope) = E_Protected_Type then
793 Enclosing_Prot_Type := Current_Scope;
795 elsif Ekind (Current_Scope) = E_Function
796 and then Ekind (Scope (Current_Scope)) = E_Protected_Type
797 then
798 Enclosing_Prot_Type := Scope (Current_Scope);
799 end if;
801 if Present (Enclosing_Prot_Type) then
802 Anon_Scope := Scope (Enclosing_Prot_Type);
804 else
805 Anon_Scope := Scope (Defining_Entity (Related_Nod));
806 end if;
808 -- For an access type definition, if the current scope is a child
809 -- unit it is the scope of the type.
811 elsif Is_Compilation_Unit (Current_Scope) then
812 Anon_Scope := Current_Scope;
814 -- For access formals, access components, and access discriminants, the
815 -- scope is that of the enclosing declaration,
817 else
818 Anon_Scope := Scope (Current_Scope);
819 end if;
821 Anon_Type :=
822 Create_Itype
823 (E_Anonymous_Access_Type, Related_Nod, Scope_Id => Anon_Scope);
825 if All_Present (N)
826 and then Ada_Version >= Ada_2005
827 then
828 Error_Msg_N ("ALL is not permitted for anonymous access types", N);
829 end if;
831 -- Ada 2005 (AI-254): In case of anonymous access to subprograms call
832 -- the corresponding semantic routine
834 if Present (Access_To_Subprogram_Definition (N)) then
836 -- Compiler runtime units are compiled in Ada 2005 mode when building
837 -- the runtime library but must also be compilable in Ada 95 mode
838 -- (when bootstrapping the compiler).
840 Check_Compiler_Unit ("anonymous access to subprogram", N);
842 Access_Subprogram_Declaration
843 (T_Name => Anon_Type,
844 T_Def => Access_To_Subprogram_Definition (N));
846 if Ekind (Anon_Type) = E_Access_Protected_Subprogram_Type then
847 Set_Ekind
848 (Anon_Type, E_Anonymous_Access_Protected_Subprogram_Type);
849 else
850 Set_Ekind (Anon_Type, E_Anonymous_Access_Subprogram_Type);
851 end if;
853 Set_Can_Use_Internal_Rep
854 (Anon_Type, not Always_Compatible_Rep_On_Target);
856 -- If the anonymous access is associated with a protected operation,
857 -- create a reference to it after the enclosing protected definition
858 -- because the itype will be used in the subsequent bodies.
860 -- If the anonymous access itself is protected, a full type
861 -- declaratiton will be created for it, so that the equivalent
862 -- record type can be constructed. For further details, see
863 -- Replace_Anonymous_Access_To_Protected-Subprogram.
865 if Ekind (Current_Scope) = E_Protected_Type
866 and then not Protected_Present (Access_To_Subprogram_Definition (N))
867 then
868 Build_Itype_Reference (Anon_Type, Parent (Current_Scope));
869 end if;
871 return Anon_Type;
872 end if;
874 Find_Type (Subtype_Mark (N));
875 Desig_Type := Entity (Subtype_Mark (N));
877 Set_Directly_Designated_Type (Anon_Type, Desig_Type);
878 Set_Etype (Anon_Type, Anon_Type);
880 -- Make sure the anonymous access type has size and alignment fields
881 -- set, as required by gigi. This is necessary in the case of the
882 -- Task_Body_Procedure.
884 if not Has_Private_Component (Desig_Type) then
885 Layout_Type (Anon_Type);
886 end if;
888 -- Ada 2005 (AI-231): Ada 2005 semantics for anonymous access differs
889 -- from Ada 95 semantics. In Ada 2005, anonymous access must specify if
890 -- the null value is allowed. In Ada 95 the null value is never allowed.
892 if Ada_Version >= Ada_2005 then
893 Set_Can_Never_Be_Null (Anon_Type, Null_Exclusion_Present (N));
894 else
895 Set_Can_Never_Be_Null (Anon_Type, True);
896 end if;
898 -- The anonymous access type is as public as the discriminated type or
899 -- subprogram that defines it. It is imported (for back-end purposes)
900 -- if the designated type is.
902 Set_Is_Public (Anon_Type, Is_Public (Scope (Anon_Type)));
904 -- Ada 2005 (AI-231): Propagate the access-constant attribute
906 Set_Is_Access_Constant (Anon_Type, Constant_Present (N));
908 -- The context is either a subprogram declaration, object declaration,
909 -- or an access discriminant, in a private or a full type declaration.
910 -- In the case of a subprogram, if the designated type is incomplete,
911 -- the operation will be a primitive operation of the full type, to be
912 -- updated subsequently. If the type is imported through a limited_with
913 -- clause, the subprogram is not a primitive operation of the type
914 -- (which is declared elsewhere in some other scope).
916 if Ekind (Desig_Type) = E_Incomplete_Type
917 and then not From_Limited_With (Desig_Type)
918 and then Is_Overloadable (Current_Scope)
919 then
920 Append_Elmt (Current_Scope, Private_Dependents (Desig_Type));
921 Set_Has_Delayed_Freeze (Current_Scope);
922 end if;
924 -- Ada 2005: If the designated type is an interface that may contain
925 -- tasks, create a Master entity for the declaration. This must be done
926 -- before expansion of the full declaration, because the declaration may
927 -- include an expression that is an allocator, whose expansion needs the
928 -- proper Master for the created tasks.
930 if Nkind (Related_Nod) = N_Object_Declaration and then Expander_Active
931 then
932 if Is_Interface (Desig_Type) and then Is_Limited_Record (Desig_Type)
933 then
934 Build_Class_Wide_Master (Anon_Type);
936 -- Similarly, if the type is an anonymous access that designates
937 -- tasks, create a master entity for it in the current context.
939 elsif Has_Task (Desig_Type) and then Comes_From_Source (Related_Nod)
940 then
941 Build_Master_Entity (Defining_Identifier (Related_Nod));
942 Build_Master_Renaming (Anon_Type);
943 end if;
944 end if;
946 -- For a private component of a protected type, it is imperative that
947 -- the back-end elaborate the type immediately after the protected
948 -- declaration, because this type will be used in the declarations
949 -- created for the component within each protected body, so we must
950 -- create an itype reference for it now.
952 if Nkind (Parent (Related_Nod)) = N_Protected_Definition then
953 Build_Itype_Reference (Anon_Type, Parent (Parent (Related_Nod)));
955 -- Similarly, if the access definition is the return result of a
956 -- function, create an itype reference for it because it will be used
957 -- within the function body. For a regular function that is not a
958 -- compilation unit, insert reference after the declaration. For a
959 -- protected operation, insert it after the enclosing protected type
960 -- declaration. In either case, do not create a reference for a type
961 -- obtained through a limited_with clause, because this would introduce
962 -- semantic dependencies.
964 -- Similarly, do not create a reference if the designated type is a
965 -- generic formal, because no use of it will reach the backend.
967 elsif Nkind (Related_Nod) = N_Function_Specification
968 and then not From_Limited_With (Desig_Type)
969 and then not Is_Generic_Type (Desig_Type)
970 then
971 if Present (Enclosing_Prot_Type) then
972 Build_Itype_Reference (Anon_Type, Parent (Enclosing_Prot_Type));
974 elsif Is_List_Member (Parent (Related_Nod))
975 and then Nkind (Parent (N)) /= N_Parameter_Specification
976 then
977 Build_Itype_Reference (Anon_Type, Parent (Related_Nod));
978 end if;
980 -- Finally, create an itype reference for an object declaration of an
981 -- anonymous access type. This is strictly necessary only for deferred
982 -- constants, but in any case will avoid out-of-scope problems in the
983 -- back-end.
985 elsif Nkind (Related_Nod) = N_Object_Declaration then
986 Build_Itype_Reference (Anon_Type, Related_Nod);
987 end if;
989 return Anon_Type;
990 end Access_Definition;
992 -----------------------------------
993 -- Access_Subprogram_Declaration --
994 -----------------------------------
996 procedure Access_Subprogram_Declaration
997 (T_Name : Entity_Id;
998 T_Def : Node_Id)
1000 procedure Check_For_Premature_Usage (Def : Node_Id);
1001 -- Check that type T_Name is not used, directly or recursively, as a
1002 -- parameter or a return type in Def. Def is either a subtype, an
1003 -- access_definition, or an access_to_subprogram_definition.
1005 -------------------------------
1006 -- Check_For_Premature_Usage --
1007 -------------------------------
1009 procedure Check_For_Premature_Usage (Def : Node_Id) is
1010 Param : Node_Id;
1012 begin
1013 -- Check for a subtype mark
1015 if Nkind (Def) in N_Has_Etype then
1016 if Etype (Def) = T_Name then
1017 Error_Msg_N
1018 ("type& cannot be used before end of its declaration", Def);
1019 end if;
1021 -- If this is not a subtype, then this is an access_definition
1023 elsif Nkind (Def) = N_Access_Definition then
1024 if Present (Access_To_Subprogram_Definition (Def)) then
1025 Check_For_Premature_Usage
1026 (Access_To_Subprogram_Definition (Def));
1027 else
1028 Check_For_Premature_Usage (Subtype_Mark (Def));
1029 end if;
1031 -- The only cases left are N_Access_Function_Definition and
1032 -- N_Access_Procedure_Definition.
1034 else
1035 if Present (Parameter_Specifications (Def)) then
1036 Param := First (Parameter_Specifications (Def));
1037 while Present (Param) loop
1038 Check_For_Premature_Usage (Parameter_Type (Param));
1039 Param := Next (Param);
1040 end loop;
1041 end if;
1043 if Nkind (Def) = N_Access_Function_Definition then
1044 Check_For_Premature_Usage (Result_Definition (Def));
1045 end if;
1046 end if;
1047 end Check_For_Premature_Usage;
1049 -- Local variables
1051 Formals : constant List_Id := Parameter_Specifications (T_Def);
1052 Formal : Entity_Id;
1053 D_Ityp : Node_Id;
1054 Desig_Type : constant Entity_Id :=
1055 Create_Itype (E_Subprogram_Type, Parent (T_Def));
1057 -- Start of processing for Access_Subprogram_Declaration
1059 begin
1060 Check_SPARK_05_Restriction ("access type is not allowed", T_Def);
1062 -- Associate the Itype node with the inner full-type declaration or
1063 -- subprogram spec or entry body. This is required to handle nested
1064 -- anonymous declarations. For example:
1066 -- procedure P
1067 -- (X : access procedure
1068 -- (Y : access procedure
1069 -- (Z : access T)))
1071 D_Ityp := Associated_Node_For_Itype (Desig_Type);
1072 while not (Nkind_In (D_Ityp, N_Full_Type_Declaration,
1073 N_Private_Type_Declaration,
1074 N_Private_Extension_Declaration,
1075 N_Procedure_Specification,
1076 N_Function_Specification,
1077 N_Entry_Body)
1079 or else
1080 Nkind_In (D_Ityp, N_Object_Declaration,
1081 N_Object_Renaming_Declaration,
1082 N_Formal_Object_Declaration,
1083 N_Formal_Type_Declaration,
1084 N_Task_Type_Declaration,
1085 N_Protected_Type_Declaration))
1086 loop
1087 D_Ityp := Parent (D_Ityp);
1088 pragma Assert (D_Ityp /= Empty);
1089 end loop;
1091 Set_Associated_Node_For_Itype (Desig_Type, D_Ityp);
1093 if Nkind_In (D_Ityp, N_Procedure_Specification,
1094 N_Function_Specification)
1095 then
1096 Set_Scope (Desig_Type, Scope (Defining_Entity (D_Ityp)));
1098 elsif Nkind_In (D_Ityp, N_Full_Type_Declaration,
1099 N_Object_Declaration,
1100 N_Object_Renaming_Declaration,
1101 N_Formal_Type_Declaration)
1102 then
1103 Set_Scope (Desig_Type, Scope (Defining_Identifier (D_Ityp)));
1104 end if;
1106 if Nkind (T_Def) = N_Access_Function_Definition then
1107 if Nkind (Result_Definition (T_Def)) = N_Access_Definition then
1108 declare
1109 Acc : constant Node_Id := Result_Definition (T_Def);
1111 begin
1112 if Present (Access_To_Subprogram_Definition (Acc))
1113 and then
1114 Protected_Present (Access_To_Subprogram_Definition (Acc))
1115 then
1116 Set_Etype
1117 (Desig_Type,
1118 Replace_Anonymous_Access_To_Protected_Subprogram
1119 (T_Def));
1121 else
1122 Set_Etype
1123 (Desig_Type,
1124 Access_Definition (T_Def, Result_Definition (T_Def)));
1125 end if;
1126 end;
1128 else
1129 Analyze (Result_Definition (T_Def));
1131 declare
1132 Typ : constant Entity_Id := Entity (Result_Definition (T_Def));
1134 begin
1135 -- If a null exclusion is imposed on the result type, then
1136 -- create a null-excluding itype (an access subtype) and use
1137 -- it as the function's Etype.
1139 if Is_Access_Type (Typ)
1140 and then Null_Exclusion_In_Return_Present (T_Def)
1141 then
1142 Set_Etype (Desig_Type,
1143 Create_Null_Excluding_Itype
1144 (T => Typ,
1145 Related_Nod => T_Def,
1146 Scope_Id => Current_Scope));
1148 else
1149 if From_Limited_With (Typ) then
1151 -- AI05-151: Incomplete types are allowed in all basic
1152 -- declarations, including access to subprograms.
1154 if Ada_Version >= Ada_2012 then
1155 null;
1157 else
1158 Error_Msg_NE
1159 ("illegal use of incomplete type&",
1160 Result_Definition (T_Def), Typ);
1161 end if;
1163 elsif Ekind (Current_Scope) = E_Package
1164 and then In_Private_Part (Current_Scope)
1165 then
1166 if Ekind (Typ) = E_Incomplete_Type then
1167 Append_Elmt (Desig_Type, Private_Dependents (Typ));
1169 elsif Is_Class_Wide_Type (Typ)
1170 and then Ekind (Etype (Typ)) = E_Incomplete_Type
1171 then
1172 Append_Elmt
1173 (Desig_Type, Private_Dependents (Etype (Typ)));
1174 end if;
1175 end if;
1177 Set_Etype (Desig_Type, Typ);
1178 end if;
1179 end;
1180 end if;
1182 if not (Is_Type (Etype (Desig_Type))) then
1183 Error_Msg_N
1184 ("expect type in function specification",
1185 Result_Definition (T_Def));
1186 end if;
1188 else
1189 Set_Etype (Desig_Type, Standard_Void_Type);
1190 end if;
1192 if Present (Formals) then
1193 Push_Scope (Desig_Type);
1195 -- Some special tests here. These special tests can be removed
1196 -- if and when Itypes always have proper parent pointers to their
1197 -- declarations???
1199 -- Special test 1) Link defining_identifier of formals. Required by
1200 -- First_Formal to provide its functionality.
1202 declare
1203 F : Node_Id;
1205 begin
1206 F := First (Formals);
1208 -- In ASIS mode, the access_to_subprogram may be analyzed twice,
1209 -- when it is part of an unconstrained type and subtype expansion
1210 -- is disabled. To avoid back-end problems with shared profiles,
1211 -- use previous subprogram type as the designated type, and then
1212 -- remove scope added above.
1214 if ASIS_Mode and then Present (Scope (Defining_Identifier (F)))
1215 then
1216 Set_Etype (T_Name, T_Name);
1217 Init_Size_Align (T_Name);
1218 Set_Directly_Designated_Type (T_Name,
1219 Scope (Defining_Identifier (F)));
1220 End_Scope;
1221 return;
1222 end if;
1224 while Present (F) loop
1225 if No (Parent (Defining_Identifier (F))) then
1226 Set_Parent (Defining_Identifier (F), F);
1227 end if;
1229 Next (F);
1230 end loop;
1231 end;
1233 Process_Formals (Formals, Parent (T_Def));
1235 -- Special test 2) End_Scope requires that the parent pointer be set
1236 -- to something reasonable, but Itypes don't have parent pointers. So
1237 -- we set it and then unset it ???
1239 Set_Parent (Desig_Type, T_Name);
1240 End_Scope;
1241 Set_Parent (Desig_Type, Empty);
1242 end if;
1244 -- Check for premature usage of the type being defined
1246 Check_For_Premature_Usage (T_Def);
1248 -- The return type and/or any parameter type may be incomplete. Mark the
1249 -- subprogram_type as depending on the incomplete type, so that it can
1250 -- be updated when the full type declaration is seen. This only applies
1251 -- to incomplete types declared in some enclosing scope, not to limited
1252 -- views from other packages.
1254 -- Prior to Ada 2012, access to functions can only have in_parameters.
1256 if Present (Formals) then
1257 Formal := First_Formal (Desig_Type);
1258 while Present (Formal) loop
1259 if Ekind (Formal) /= E_In_Parameter
1260 and then Nkind (T_Def) = N_Access_Function_Definition
1261 and then Ada_Version < Ada_2012
1262 then
1263 Error_Msg_N ("functions can only have IN parameters", Formal);
1264 end if;
1266 if Ekind (Etype (Formal)) = E_Incomplete_Type
1267 and then In_Open_Scopes (Scope (Etype (Formal)))
1268 then
1269 Append_Elmt (Desig_Type, Private_Dependents (Etype (Formal)));
1270 Set_Has_Delayed_Freeze (Desig_Type);
1271 end if;
1273 Next_Formal (Formal);
1274 end loop;
1275 end if;
1277 -- Check whether an indirect call without actuals may be possible. This
1278 -- is used when resolving calls whose result is then indexed.
1280 May_Need_Actuals (Desig_Type);
1282 -- If the return type is incomplete, this is legal as long as the type
1283 -- is declared in the current scope and will be completed in it (rather
1284 -- than being part of limited view).
1286 if Ekind (Etype (Desig_Type)) = E_Incomplete_Type
1287 and then not Has_Delayed_Freeze (Desig_Type)
1288 and then In_Open_Scopes (Scope (Etype (Desig_Type)))
1289 then
1290 Append_Elmt (Desig_Type, Private_Dependents (Etype (Desig_Type)));
1291 Set_Has_Delayed_Freeze (Desig_Type);
1292 end if;
1294 Check_Delayed_Subprogram (Desig_Type);
1296 if Protected_Present (T_Def) then
1297 Set_Ekind (T_Name, E_Access_Protected_Subprogram_Type);
1298 Set_Convention (Desig_Type, Convention_Protected);
1299 else
1300 Set_Ekind (T_Name, E_Access_Subprogram_Type);
1301 end if;
1303 Set_Can_Use_Internal_Rep (T_Name, not Always_Compatible_Rep_On_Target);
1305 Set_Etype (T_Name, T_Name);
1306 Init_Size_Align (T_Name);
1307 Set_Directly_Designated_Type (T_Name, Desig_Type);
1309 Generate_Reference_To_Formals (T_Name);
1311 -- Ada 2005 (AI-231): Propagate the null-excluding attribute
1313 Set_Can_Never_Be_Null (T_Name, Null_Exclusion_Present (T_Def));
1315 Check_Restriction (No_Access_Subprograms, T_Def);
1316 end Access_Subprogram_Declaration;
1318 ----------------------------
1319 -- Access_Type_Declaration --
1320 ----------------------------
1322 procedure Access_Type_Declaration (T : Entity_Id; Def : Node_Id) is
1323 P : constant Node_Id := Parent (Def);
1324 S : constant Node_Id := Subtype_Indication (Def);
1326 Full_Desig : Entity_Id;
1328 begin
1329 Check_SPARK_05_Restriction ("access type is not allowed", Def);
1331 -- Check for permissible use of incomplete type
1333 if Nkind (S) /= N_Subtype_Indication then
1334 Analyze (S);
1336 if Present (Entity (S))
1337 and then Ekind (Root_Type (Entity (S))) = E_Incomplete_Type
1338 then
1339 Set_Directly_Designated_Type (T, Entity (S));
1341 -- If the designated type is a limited view, we cannot tell if
1342 -- the full view contains tasks, and there is no way to handle
1343 -- that full view in a client. We create a master entity for the
1344 -- scope, which will be used when a client determines that one
1345 -- is needed.
1347 if From_Limited_With (Entity (S))
1348 and then not Is_Class_Wide_Type (Entity (S))
1349 then
1350 Set_Ekind (T, E_Access_Type);
1351 Build_Master_Entity (T);
1352 Build_Master_Renaming (T);
1353 end if;
1355 else
1356 Set_Directly_Designated_Type (T, Process_Subtype (S, P, T, 'P'));
1357 end if;
1359 -- If the access definition is of the form: ACCESS NOT NULL ..
1360 -- the subtype indication must be of an access type. Create
1361 -- a null-excluding subtype of it.
1363 if Null_Excluding_Subtype (Def) then
1364 if not Is_Access_Type (Entity (S)) then
1365 Error_Msg_N ("null exclusion must apply to access type", Def);
1367 else
1368 declare
1369 Loc : constant Source_Ptr := Sloc (S);
1370 Decl : Node_Id;
1371 Nam : constant Entity_Id := Make_Temporary (Loc, 'S');
1373 begin
1374 Decl :=
1375 Make_Subtype_Declaration (Loc,
1376 Defining_Identifier => Nam,
1377 Subtype_Indication =>
1378 New_Occurrence_Of (Entity (S), Loc));
1379 Set_Null_Exclusion_Present (Decl);
1380 Insert_Before (Parent (Def), Decl);
1381 Analyze (Decl);
1382 Set_Entity (S, Nam);
1383 end;
1384 end if;
1385 end if;
1387 else
1388 Set_Directly_Designated_Type (T,
1389 Process_Subtype (S, P, T, 'P'));
1390 end if;
1392 if All_Present (Def) or Constant_Present (Def) then
1393 Set_Ekind (T, E_General_Access_Type);
1394 else
1395 Set_Ekind (T, E_Access_Type);
1396 end if;
1398 Full_Desig := Designated_Type (T);
1400 if Base_Type (Full_Desig) = T then
1401 Error_Msg_N ("access type cannot designate itself", S);
1403 -- In Ada 2005, the type may have a limited view through some unit in
1404 -- its own context, allowing the following circularity that cannot be
1405 -- detected earlier.
1407 elsif Is_Class_Wide_Type (Full_Desig) and then Etype (Full_Desig) = T
1408 then
1409 Error_Msg_N
1410 ("access type cannot designate its own class-wide type", S);
1412 -- Clean up indication of tagged status to prevent cascaded errors
1414 Set_Is_Tagged_Type (T, False);
1415 end if;
1417 Set_Etype (T, T);
1419 -- If the type has appeared already in a with_type clause, it is frozen
1420 -- and the pointer size is already set. Else, initialize.
1422 if not From_Limited_With (T) then
1423 Init_Size_Align (T);
1424 end if;
1426 -- Note that Has_Task is always false, since the access type itself
1427 -- is not a task type. See Einfo for more description on this point.
1428 -- Exactly the same consideration applies to Has_Controlled_Component
1429 -- and to Has_Protected.
1431 Set_Has_Task (T, False);
1432 Set_Has_Protected (T, False);
1433 Set_Has_Timing_Event (T, False);
1434 Set_Has_Controlled_Component (T, False);
1436 -- Initialize field Finalization_Master explicitly to Empty, to avoid
1437 -- problems where an incomplete view of this entity has been previously
1438 -- established by a limited with and an overlaid version of this field
1439 -- (Stored_Constraint) was initialized for the incomplete view.
1441 -- This reset is performed in most cases except where the access type
1442 -- has been created for the purposes of allocating or deallocating a
1443 -- build-in-place object. Such access types have explicitly set pools
1444 -- and finalization masters.
1446 if No (Associated_Storage_Pool (T)) then
1447 Set_Finalization_Master (T, Empty);
1448 end if;
1450 -- Ada 2005 (AI-231): Propagate the null-excluding and access-constant
1451 -- attributes
1453 Set_Can_Never_Be_Null (T, Null_Exclusion_Present (Def));
1454 Set_Is_Access_Constant (T, Constant_Present (Def));
1455 end Access_Type_Declaration;
1457 ----------------------------------
1458 -- Add_Interface_Tag_Components --
1459 ----------------------------------
1461 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id) is
1462 Loc : constant Source_Ptr := Sloc (N);
1463 L : List_Id;
1464 Last_Tag : Node_Id;
1466 procedure Add_Tag (Iface : Entity_Id);
1467 -- Add tag for one of the progenitor interfaces
1469 -------------
1470 -- Add_Tag --
1471 -------------
1473 procedure Add_Tag (Iface : Entity_Id) is
1474 Decl : Node_Id;
1475 Def : Node_Id;
1476 Tag : Entity_Id;
1477 Offset : Entity_Id;
1479 begin
1480 pragma Assert (Is_Tagged_Type (Iface) and then Is_Interface (Iface));
1482 -- This is a reasonable place to propagate predicates
1484 if Has_Predicates (Iface) then
1485 Set_Has_Predicates (Typ);
1486 end if;
1488 Def :=
1489 Make_Component_Definition (Loc,
1490 Aliased_Present => True,
1491 Subtype_Indication =>
1492 New_Occurrence_Of (RTE (RE_Interface_Tag), Loc));
1494 Tag := Make_Temporary (Loc, 'V');
1496 Decl :=
1497 Make_Component_Declaration (Loc,
1498 Defining_Identifier => Tag,
1499 Component_Definition => Def);
1501 Analyze_Component_Declaration (Decl);
1503 Set_Analyzed (Decl);
1504 Set_Ekind (Tag, E_Component);
1505 Set_Is_Tag (Tag);
1506 Set_Is_Aliased (Tag);
1507 Set_Related_Type (Tag, Iface);
1508 Init_Component_Location (Tag);
1510 pragma Assert (Is_Frozen (Iface));
1512 Set_DT_Entry_Count (Tag,
1513 DT_Entry_Count (First_Entity (Iface)));
1515 if No (Last_Tag) then
1516 Prepend (Decl, L);
1517 else
1518 Insert_After (Last_Tag, Decl);
1519 end if;
1521 Last_Tag := Decl;
1523 -- If the ancestor has discriminants we need to give special support
1524 -- to store the offset_to_top value of the secondary dispatch tables.
1525 -- For this purpose we add a supplementary component just after the
1526 -- field that contains the tag associated with each secondary DT.
1528 if Typ /= Etype (Typ) and then Has_Discriminants (Etype (Typ)) then
1529 Def :=
1530 Make_Component_Definition (Loc,
1531 Subtype_Indication =>
1532 New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
1534 Offset := Make_Temporary (Loc, 'V');
1536 Decl :=
1537 Make_Component_Declaration (Loc,
1538 Defining_Identifier => Offset,
1539 Component_Definition => Def);
1541 Analyze_Component_Declaration (Decl);
1543 Set_Analyzed (Decl);
1544 Set_Ekind (Offset, E_Component);
1545 Set_Is_Aliased (Offset);
1546 Set_Related_Type (Offset, Iface);
1547 Init_Component_Location (Offset);
1548 Insert_After (Last_Tag, Decl);
1549 Last_Tag := Decl;
1550 end if;
1551 end Add_Tag;
1553 -- Local variables
1555 Elmt : Elmt_Id;
1556 Ext : Node_Id;
1557 Comp : Node_Id;
1559 -- Start of processing for Add_Interface_Tag_Components
1561 begin
1562 if not RTE_Available (RE_Interface_Tag) then
1563 Error_Msg
1564 ("(Ada 2005) interface types not supported by this run-time!",
1565 Sloc (N));
1566 return;
1567 end if;
1569 if Ekind (Typ) /= E_Record_Type
1570 or else (Is_Concurrent_Record_Type (Typ)
1571 and then Is_Empty_List (Abstract_Interface_List (Typ)))
1572 or else (not Is_Concurrent_Record_Type (Typ)
1573 and then No (Interfaces (Typ))
1574 and then Is_Empty_Elmt_List (Interfaces (Typ)))
1575 then
1576 return;
1577 end if;
1579 -- Find the current last tag
1581 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1582 Ext := Record_Extension_Part (Type_Definition (N));
1583 else
1584 pragma Assert (Nkind (Type_Definition (N)) = N_Record_Definition);
1585 Ext := Type_Definition (N);
1586 end if;
1588 Last_Tag := Empty;
1590 if not (Present (Component_List (Ext))) then
1591 Set_Null_Present (Ext, False);
1592 L := New_List;
1593 Set_Component_List (Ext,
1594 Make_Component_List (Loc,
1595 Component_Items => L,
1596 Null_Present => False));
1597 else
1598 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1599 L := Component_Items
1600 (Component_List
1601 (Record_Extension_Part
1602 (Type_Definition (N))));
1603 else
1604 L := Component_Items
1605 (Component_List
1606 (Type_Definition (N)));
1607 end if;
1609 -- Find the last tag component
1611 Comp := First (L);
1612 while Present (Comp) loop
1613 if Nkind (Comp) = N_Component_Declaration
1614 and then Is_Tag (Defining_Identifier (Comp))
1615 then
1616 Last_Tag := Comp;
1617 end if;
1619 Next (Comp);
1620 end loop;
1621 end if;
1623 -- At this point L references the list of components and Last_Tag
1624 -- references the current last tag (if any). Now we add the tag
1625 -- corresponding with all the interfaces that are not implemented
1626 -- by the parent.
1628 if Present (Interfaces (Typ)) then
1629 Elmt := First_Elmt (Interfaces (Typ));
1630 while Present (Elmt) loop
1631 Add_Tag (Node (Elmt));
1632 Next_Elmt (Elmt);
1633 end loop;
1634 end if;
1635 end Add_Interface_Tag_Components;
1637 -------------------------------------
1638 -- Add_Internal_Interface_Entities --
1639 -------------------------------------
1641 procedure Add_Internal_Interface_Entities (Tagged_Type : Entity_Id) is
1642 Elmt : Elmt_Id;
1643 Iface : Entity_Id;
1644 Iface_Elmt : Elmt_Id;
1645 Iface_Prim : Entity_Id;
1646 Ifaces_List : Elist_Id;
1647 New_Subp : Entity_Id := Empty;
1648 Prim : Entity_Id;
1649 Restore_Scope : Boolean := False;
1651 begin
1652 pragma Assert (Ada_Version >= Ada_2005
1653 and then Is_Record_Type (Tagged_Type)
1654 and then Is_Tagged_Type (Tagged_Type)
1655 and then Has_Interfaces (Tagged_Type)
1656 and then not Is_Interface (Tagged_Type));
1658 -- Ensure that the internal entities are added to the scope of the type
1660 if Scope (Tagged_Type) /= Current_Scope then
1661 Push_Scope (Scope (Tagged_Type));
1662 Restore_Scope := True;
1663 end if;
1665 Collect_Interfaces (Tagged_Type, Ifaces_List);
1667 Iface_Elmt := First_Elmt (Ifaces_List);
1668 while Present (Iface_Elmt) loop
1669 Iface := Node (Iface_Elmt);
1671 -- Originally we excluded here from this processing interfaces that
1672 -- are parents of Tagged_Type because their primitives are located
1673 -- in the primary dispatch table (and hence no auxiliary internal
1674 -- entities are required to handle secondary dispatch tables in such
1675 -- case). However, these auxiliary entities are also required to
1676 -- handle derivations of interfaces in formals of generics (see
1677 -- Derive_Subprograms).
1679 Elmt := First_Elmt (Primitive_Operations (Iface));
1680 while Present (Elmt) loop
1681 Iface_Prim := Node (Elmt);
1683 if not Is_Predefined_Dispatching_Operation (Iface_Prim) then
1684 Prim :=
1685 Find_Primitive_Covering_Interface
1686 (Tagged_Type => Tagged_Type,
1687 Iface_Prim => Iface_Prim);
1689 if No (Prim) and then Serious_Errors_Detected > 0 then
1690 goto Continue;
1691 end if;
1693 pragma Assert (Present (Prim));
1695 -- Ada 2012 (AI05-0197): If the name of the covering primitive
1696 -- differs from the name of the interface primitive then it is
1697 -- a private primitive inherited from a parent type. In such
1698 -- case, given that Tagged_Type covers the interface, the
1699 -- inherited private primitive becomes visible. For such
1700 -- purpose we add a new entity that renames the inherited
1701 -- private primitive.
1703 if Chars (Prim) /= Chars (Iface_Prim) then
1704 pragma Assert (Has_Suffix (Prim, 'P'));
1705 Derive_Subprogram
1706 (New_Subp => New_Subp,
1707 Parent_Subp => Iface_Prim,
1708 Derived_Type => Tagged_Type,
1709 Parent_Type => Iface);
1710 Set_Alias (New_Subp, Prim);
1711 Set_Is_Abstract_Subprogram
1712 (New_Subp, Is_Abstract_Subprogram (Prim));
1713 end if;
1715 Derive_Subprogram
1716 (New_Subp => New_Subp,
1717 Parent_Subp => Iface_Prim,
1718 Derived_Type => Tagged_Type,
1719 Parent_Type => Iface);
1721 -- Ada 2005 (AI-251): Decorate internal entity Iface_Subp
1722 -- associated with interface types. These entities are
1723 -- only registered in the list of primitives of its
1724 -- corresponding tagged type because they are only used
1725 -- to fill the contents of the secondary dispatch tables.
1726 -- Therefore they are removed from the homonym chains.
1728 Set_Is_Hidden (New_Subp);
1729 Set_Is_Internal (New_Subp);
1730 Set_Alias (New_Subp, Prim);
1731 Set_Is_Abstract_Subprogram
1732 (New_Subp, Is_Abstract_Subprogram (Prim));
1733 Set_Interface_Alias (New_Subp, Iface_Prim);
1735 -- If the returned type is an interface then propagate it to
1736 -- the returned type. Needed by the thunk to generate the code
1737 -- which displaces "this" to reference the corresponding
1738 -- secondary dispatch table in the returned object.
1740 if Is_Interface (Etype (Iface_Prim)) then
1741 Set_Etype (New_Subp, Etype (Iface_Prim));
1742 end if;
1744 -- Internal entities associated with interface types are only
1745 -- registered in the list of primitives of the tagged type.
1746 -- They are only used to fill the contents of the secondary
1747 -- dispatch tables. Therefore they are not needed in the
1748 -- homonym chains.
1750 Remove_Homonym (New_Subp);
1752 -- Hidden entities associated with interfaces must have set
1753 -- the Has_Delay_Freeze attribute to ensure that, in case
1754 -- of locally defined tagged types (or compiling with static
1755 -- dispatch tables generation disabled) the corresponding
1756 -- entry of the secondary dispatch table is filled when such
1757 -- an entity is frozen. This is an expansion activity that must
1758 -- be suppressed for ASIS because it leads to gigi elaboration
1759 -- issues in annotate mode.
1761 if not ASIS_Mode then
1762 Set_Has_Delayed_Freeze (New_Subp);
1763 end if;
1764 end if;
1766 <<Continue>>
1767 Next_Elmt (Elmt);
1768 end loop;
1770 Next_Elmt (Iface_Elmt);
1771 end loop;
1773 if Restore_Scope then
1774 Pop_Scope;
1775 end if;
1776 end Add_Internal_Interface_Entities;
1778 -----------------------------------
1779 -- Analyze_Component_Declaration --
1780 -----------------------------------
1782 procedure Analyze_Component_Declaration (N : Node_Id) is
1783 Loc : constant Source_Ptr := Sloc (Component_Definition (N));
1784 Id : constant Entity_Id := Defining_Identifier (N);
1785 E : constant Node_Id := Expression (N);
1786 Typ : constant Node_Id :=
1787 Subtype_Indication (Component_Definition (N));
1788 T : Entity_Id;
1789 P : Entity_Id;
1791 function Contains_POC (Constr : Node_Id) return Boolean;
1792 -- Determines whether a constraint uses the discriminant of a record
1793 -- type thus becoming a per-object constraint (POC).
1795 function Is_Known_Limited (Typ : Entity_Id) return Boolean;
1796 -- Typ is the type of the current component, check whether this type is
1797 -- a limited type. Used to validate declaration against that of
1798 -- enclosing record.
1800 ------------------
1801 -- Contains_POC --
1802 ------------------
1804 function Contains_POC (Constr : Node_Id) return Boolean is
1805 begin
1806 -- Prevent cascaded errors
1808 if Error_Posted (Constr) then
1809 return False;
1810 end if;
1812 case Nkind (Constr) is
1813 when N_Attribute_Reference =>
1814 return Attribute_Name (Constr) = Name_Access
1815 and then Prefix (Constr) = Scope (Entity (Prefix (Constr)));
1817 when N_Discriminant_Association =>
1818 return Denotes_Discriminant (Expression (Constr));
1820 when N_Identifier =>
1821 return Denotes_Discriminant (Constr);
1823 when N_Index_Or_Discriminant_Constraint =>
1824 declare
1825 IDC : Node_Id;
1827 begin
1828 IDC := First (Constraints (Constr));
1829 while Present (IDC) loop
1831 -- One per-object constraint is sufficient
1833 if Contains_POC (IDC) then
1834 return True;
1835 end if;
1837 Next (IDC);
1838 end loop;
1840 return False;
1841 end;
1843 when N_Range =>
1844 return Denotes_Discriminant (Low_Bound (Constr))
1845 or else
1846 Denotes_Discriminant (High_Bound (Constr));
1848 when N_Range_Constraint =>
1849 return Denotes_Discriminant (Range_Expression (Constr));
1851 when others =>
1852 return False;
1853 end case;
1854 end Contains_POC;
1856 ----------------------
1857 -- Is_Known_Limited --
1858 ----------------------
1860 function Is_Known_Limited (Typ : Entity_Id) return Boolean is
1861 P : constant Entity_Id := Etype (Typ);
1862 R : constant Entity_Id := Root_Type (Typ);
1864 begin
1865 if Is_Limited_Record (Typ) then
1866 return True;
1868 -- If the root type is limited (and not a limited interface)
1869 -- so is the current type
1871 elsif Is_Limited_Record (R)
1872 and then (not Is_Interface (R) or else not Is_Limited_Interface (R))
1873 then
1874 return True;
1876 -- Else the type may have a limited interface progenitor, but a
1877 -- limited record parent.
1879 elsif R /= P and then Is_Limited_Record (P) then
1880 return True;
1882 else
1883 return False;
1884 end if;
1885 end Is_Known_Limited;
1887 -- Start of processing for Analyze_Component_Declaration
1889 begin
1890 Generate_Definition (Id);
1891 Enter_Name (Id);
1893 if Present (Typ) then
1894 T := Find_Type_Of_Object
1895 (Subtype_Indication (Component_Definition (N)), N);
1897 if not Nkind_In (Typ, N_Identifier, N_Expanded_Name) then
1898 Check_SPARK_05_Restriction ("subtype mark required", Typ);
1899 end if;
1901 -- Ada 2005 (AI-230): Access Definition case
1903 else
1904 pragma Assert (Present
1905 (Access_Definition (Component_Definition (N))));
1907 T := Access_Definition
1908 (Related_Nod => N,
1909 N => Access_Definition (Component_Definition (N)));
1910 Set_Is_Local_Anonymous_Access (T);
1912 -- Ada 2005 (AI-254)
1914 if Present (Access_To_Subprogram_Definition
1915 (Access_Definition (Component_Definition (N))))
1916 and then Protected_Present (Access_To_Subprogram_Definition
1917 (Access_Definition
1918 (Component_Definition (N))))
1919 then
1920 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1921 end if;
1922 end if;
1924 -- If the subtype is a constrained subtype of the enclosing record,
1925 -- (which must have a partial view) the back-end does not properly
1926 -- handle the recursion. Rewrite the component declaration with an
1927 -- explicit subtype indication, which is acceptable to Gigi. We can copy
1928 -- the tree directly because side effects have already been removed from
1929 -- discriminant constraints.
1931 if Ekind (T) = E_Access_Subtype
1932 and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
1933 and then Comes_From_Source (T)
1934 and then Nkind (Parent (T)) = N_Subtype_Declaration
1935 and then Etype (Directly_Designated_Type (T)) = Current_Scope
1936 then
1937 Rewrite
1938 (Subtype_Indication (Component_Definition (N)),
1939 New_Copy_Tree (Subtype_Indication (Parent (T))));
1940 T := Find_Type_Of_Object
1941 (Subtype_Indication (Component_Definition (N)), N);
1942 end if;
1944 -- If the component declaration includes a default expression, then we
1945 -- check that the component is not of a limited type (RM 3.7(5)),
1946 -- and do the special preanalysis of the expression (see section on
1947 -- "Handling of Default and Per-Object Expressions" in the spec of
1948 -- package Sem).
1950 if Present (E) then
1951 Check_SPARK_05_Restriction ("default expression is not allowed", E);
1952 Preanalyze_Default_Expression (E, T);
1953 Check_Initialization (T, E);
1955 if Ada_Version >= Ada_2005
1956 and then Ekind (T) = E_Anonymous_Access_Type
1957 and then Etype (E) /= Any_Type
1958 then
1959 -- Check RM 3.9.2(9): "if the expected type for an expression is
1960 -- an anonymous access-to-specific tagged type, then the object
1961 -- designated by the expression shall not be dynamically tagged
1962 -- unless it is a controlling operand in a call on a dispatching
1963 -- operation"
1965 if Is_Tagged_Type (Directly_Designated_Type (T))
1966 and then
1967 Ekind (Directly_Designated_Type (T)) /= E_Class_Wide_Type
1968 and then
1969 Ekind (Directly_Designated_Type (Etype (E))) =
1970 E_Class_Wide_Type
1971 then
1972 Error_Msg_N
1973 ("access to specific tagged type required (RM 3.9.2(9))", E);
1974 end if;
1976 -- (Ada 2005: AI-230): Accessibility check for anonymous
1977 -- components
1979 if Type_Access_Level (Etype (E)) >
1980 Deepest_Type_Access_Level (T)
1981 then
1982 Error_Msg_N
1983 ("expression has deeper access level than component " &
1984 "(RM 3.10.2 (12.2))", E);
1985 end if;
1987 -- The initialization expression is a reference to an access
1988 -- discriminant. The type of the discriminant is always deeper
1989 -- than any access type.
1991 if Ekind (Etype (E)) = E_Anonymous_Access_Type
1992 and then Is_Entity_Name (E)
1993 and then Ekind (Entity (E)) = E_In_Parameter
1994 and then Present (Discriminal_Link (Entity (E)))
1995 then
1996 Error_Msg_N
1997 ("discriminant has deeper accessibility level than target",
1999 end if;
2000 end if;
2001 end if;
2003 -- The parent type may be a private view with unknown discriminants,
2004 -- and thus unconstrained. Regular components must be constrained.
2006 if not Is_Definite_Subtype (T) and then Chars (Id) /= Name_uParent then
2007 if Is_Class_Wide_Type (T) then
2008 Error_Msg_N
2009 ("class-wide subtype with unknown discriminants" &
2010 " in component declaration",
2011 Subtype_Indication (Component_Definition (N)));
2012 else
2013 Error_Msg_N
2014 ("unconstrained subtype in component declaration",
2015 Subtype_Indication (Component_Definition (N)));
2016 end if;
2018 -- Components cannot be abstract, except for the special case of
2019 -- the _Parent field (case of extending an abstract tagged type)
2021 elsif Is_Abstract_Type (T) and then Chars (Id) /= Name_uParent then
2022 Error_Msg_N ("type of a component cannot be abstract", N);
2023 end if;
2025 Set_Etype (Id, T);
2026 Set_Is_Aliased (Id, Aliased_Present (Component_Definition (N)));
2028 -- The component declaration may have a per-object constraint, set
2029 -- the appropriate flag in the defining identifier of the subtype.
2031 if Present (Subtype_Indication (Component_Definition (N))) then
2032 declare
2033 Sindic : constant Node_Id :=
2034 Subtype_Indication (Component_Definition (N));
2035 begin
2036 if Nkind (Sindic) = N_Subtype_Indication
2037 and then Present (Constraint (Sindic))
2038 and then Contains_POC (Constraint (Sindic))
2039 then
2040 Set_Has_Per_Object_Constraint (Id);
2041 end if;
2042 end;
2043 end if;
2045 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
2046 -- out some static checks.
2048 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (T) then
2049 Null_Exclusion_Static_Checks (N);
2050 end if;
2052 -- If this component is private (or depends on a private type), flag the
2053 -- record type to indicate that some operations are not available.
2055 P := Private_Component (T);
2057 if Present (P) then
2059 -- Check for circular definitions
2061 if P = Any_Type then
2062 Set_Etype (Id, Any_Type);
2064 -- There is a gap in the visibility of operations only if the
2065 -- component type is not defined in the scope of the record type.
2067 elsif Scope (P) = Scope (Current_Scope) then
2068 null;
2070 elsif Is_Limited_Type (P) then
2071 Set_Is_Limited_Composite (Current_Scope);
2073 else
2074 Set_Is_Private_Composite (Current_Scope);
2075 end if;
2076 end if;
2078 if P /= Any_Type
2079 and then Is_Limited_Type (T)
2080 and then Chars (Id) /= Name_uParent
2081 and then Is_Tagged_Type (Current_Scope)
2082 then
2083 if Is_Derived_Type (Current_Scope)
2084 and then not Is_Known_Limited (Current_Scope)
2085 then
2086 Error_Msg_N
2087 ("extension of nonlimited type cannot have limited components",
2090 if Is_Interface (Root_Type (Current_Scope)) then
2091 Error_Msg_N
2092 ("\limitedness is not inherited from limited interface", N);
2093 Error_Msg_N ("\add LIMITED to type indication", N);
2094 end if;
2096 Explain_Limited_Type (T, N);
2097 Set_Etype (Id, Any_Type);
2098 Set_Is_Limited_Composite (Current_Scope, False);
2100 elsif not Is_Derived_Type (Current_Scope)
2101 and then not Is_Limited_Record (Current_Scope)
2102 and then not Is_Concurrent_Type (Current_Scope)
2103 then
2104 Error_Msg_N
2105 ("nonlimited tagged type cannot have limited components", N);
2106 Explain_Limited_Type (T, N);
2107 Set_Etype (Id, Any_Type);
2108 Set_Is_Limited_Composite (Current_Scope, False);
2109 end if;
2110 end if;
2112 -- If the component is an unconstrained task or protected type with
2113 -- discriminants, the component and the enclosing record are limited
2114 -- and the component is constrained by its default values. Compute
2115 -- its actual subtype, else it may be allocated the maximum size by
2116 -- the backend, and possibly overflow.
2118 if Is_Concurrent_Type (T)
2119 and then not Is_Constrained (T)
2120 and then Has_Discriminants (T)
2121 and then not Has_Discriminants (Current_Scope)
2122 then
2123 declare
2124 Act_T : constant Entity_Id := Build_Default_Subtype (T, N);
2126 begin
2127 Set_Etype (Id, Act_T);
2129 -- Rewrite component definition to use the constrained subtype
2131 Rewrite (Component_Definition (N),
2132 Make_Component_Definition (Loc,
2133 Subtype_Indication => New_Occurrence_Of (Act_T, Loc)));
2134 end;
2135 end if;
2137 Set_Original_Record_Component (Id, Id);
2139 if Has_Aspects (N) then
2140 Analyze_Aspect_Specifications (N, Id);
2141 end if;
2143 Analyze_Dimension (N);
2144 end Analyze_Component_Declaration;
2146 --------------------------
2147 -- Analyze_Declarations --
2148 --------------------------
2150 procedure Analyze_Declarations (L : List_Id) is
2151 Decl : Node_Id;
2153 procedure Adjust_Decl;
2154 -- Adjust Decl not to include implicit label declarations, since these
2155 -- have strange Sloc values that result in elaboration check problems.
2156 -- (They have the sloc of the label as found in the source, and that
2157 -- is ahead of the current declarative part).
2159 procedure Build_Assertion_Bodies (Decls : List_Id; Context : Node_Id);
2160 -- Create the subprogram bodies which verify the run-time semantics of
2161 -- the pragmas listed below for each elibigle type found in declarative
2162 -- list Decls. The pragmas are:
2164 -- Default_Initial_Condition
2165 -- Invariant
2166 -- Type_Invariant
2168 -- Context denotes the owner of the declarative list.
2170 procedure Check_Entry_Contracts;
2171 -- Perform a pre-analysis of the pre- and postconditions of an entry
2172 -- declaration. This must be done before full resolution and creation
2173 -- of the parameter block, etc. to catch illegal uses within the
2174 -- contract expression. Full analysis of the expression is done when
2175 -- the contract is processed.
2177 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id);
2178 -- Determine whether Body_Decl denotes the body of a late controlled
2179 -- primitive (either Initialize, Adjust or Finalize). If this is the
2180 -- case, add a proper spec if the body lacks one. The spec is inserted
2181 -- before Body_Decl and immediately analyzed.
2183 procedure Remove_Partial_Visible_Refinements (Spec_Id : Entity_Id);
2184 -- Spec_Id is the entity of a package that may define abstract states,
2185 -- and in the case of a child unit, whose ancestors may define abstract
2186 -- states. If the states have partial visible refinement, remove the
2187 -- partial visibility of each constituent at the end of the package
2188 -- spec and body declarations.
2190 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id);
2191 -- Spec_Id is the entity of a package that may define abstract states.
2192 -- If the states have visible refinement, remove the visibility of each
2193 -- constituent at the end of the package body declaration.
2195 procedure Resolve_Aspects;
2196 -- Utility to resolve the expressions of aspects at the end of a list of
2197 -- declarations.
2199 -----------------
2200 -- Adjust_Decl --
2201 -----------------
2203 procedure Adjust_Decl is
2204 begin
2205 while Present (Prev (Decl))
2206 and then Nkind (Decl) = N_Implicit_Label_Declaration
2207 loop
2208 Prev (Decl);
2209 end loop;
2210 end Adjust_Decl;
2212 ----------------------------
2213 -- Build_Assertion_Bodies --
2214 ----------------------------
2216 procedure Build_Assertion_Bodies (Decls : List_Id; Context : Node_Id) is
2217 procedure Build_Assertion_Bodies_For_Type (Typ : Entity_Id);
2218 -- Create the subprogram bodies which verify the run-time semantics
2219 -- of the pragmas listed below for type Typ. The pragmas are:
2221 -- Default_Initial_Condition
2222 -- Invariant
2223 -- Type_Invariant
2225 -------------------------------------
2226 -- Build_Assertion_Bodies_For_Type --
2227 -------------------------------------
2229 procedure Build_Assertion_Bodies_For_Type (Typ : Entity_Id) is
2230 begin
2231 -- Preanalyze and resolve the Default_Initial_Condition assertion
2232 -- expression at the end of the declarations to catch any errors.
2234 if Has_DIC (Typ) then
2235 Build_DIC_Procedure_Body (Typ);
2236 end if;
2238 if Nkind (Context) = N_Package_Specification then
2240 -- Preanalyze and resolve the invariants of a private type
2241 -- at the end of the visible declarations to catch potential
2242 -- errors. Inherited class-wide invariants are not included
2243 -- because they have already been resolved.
2245 if Decls = Visible_Declarations (Context)
2246 and then Ekind_In (Typ, E_Limited_Private_Type,
2247 E_Private_Type,
2248 E_Record_Type_With_Private)
2249 and then Has_Own_Invariants (Typ)
2250 then
2251 Build_Invariant_Procedure_Body
2252 (Typ => Typ,
2253 Partial_Invariant => True);
2255 -- Preanalyze and resolve the invariants of a private type's
2256 -- full view at the end of the private declarations to catch
2257 -- potential errors.
2259 elsif Decls = Private_Declarations (Context)
2260 and then not Is_Private_Type (Typ)
2261 and then Has_Private_Declaration (Typ)
2262 and then Has_Invariants (Typ)
2263 then
2264 Build_Invariant_Procedure_Body (Typ);
2265 end if;
2266 end if;
2267 end Build_Assertion_Bodies_For_Type;
2269 -- Local variables
2271 Decl : Node_Id;
2272 Decl_Id : Entity_Id;
2274 -- Start of processing for Build_Assertion_Bodies
2276 begin
2277 Decl := First (Decls);
2278 while Present (Decl) loop
2279 if Is_Declaration (Decl) then
2280 Decl_Id := Defining_Entity (Decl);
2282 if Is_Type (Decl_Id) then
2283 Build_Assertion_Bodies_For_Type (Decl_Id);
2284 end if;
2285 end if;
2287 Next (Decl);
2288 end loop;
2289 end Build_Assertion_Bodies;
2291 ---------------------------
2292 -- Check_Entry_Contracts --
2293 ---------------------------
2295 procedure Check_Entry_Contracts is
2296 ASN : Node_Id;
2297 Ent : Entity_Id;
2298 Exp : Node_Id;
2300 begin
2301 Ent := First_Entity (Current_Scope);
2302 while Present (Ent) loop
2304 -- This only concerns entries with pre/postconditions
2306 if Ekind (Ent) = E_Entry
2307 and then Present (Contract (Ent))
2308 and then Present (Pre_Post_Conditions (Contract (Ent)))
2309 then
2310 ASN := Pre_Post_Conditions (Contract (Ent));
2311 Push_Scope (Ent);
2312 Install_Formals (Ent);
2314 -- Pre/postconditions are rewritten as Check pragmas. Analysis
2315 -- is performed on a copy of the pragma expression, to prevent
2316 -- modifying the original expression.
2318 while Present (ASN) loop
2319 if Nkind (ASN) = N_Pragma then
2320 Exp :=
2321 New_Copy_Tree
2322 (Expression
2323 (First (Pragma_Argument_Associations (ASN))));
2324 Set_Parent (Exp, ASN);
2326 -- ??? why not Preanalyze_Assert_Expression
2328 Preanalyze (Exp);
2329 end if;
2331 ASN := Next_Pragma (ASN);
2332 end loop;
2334 End_Scope;
2335 end if;
2337 Next_Entity (Ent);
2338 end loop;
2339 end Check_Entry_Contracts;
2341 --------------------------------------
2342 -- Handle_Late_Controlled_Primitive --
2343 --------------------------------------
2345 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id) is
2346 Body_Spec : constant Node_Id := Specification (Body_Decl);
2347 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2348 Loc : constant Source_Ptr := Sloc (Body_Id);
2349 Params : constant List_Id :=
2350 Parameter_Specifications (Body_Spec);
2351 Spec : Node_Id;
2352 Spec_Id : Entity_Id;
2353 Typ : Node_Id;
2355 begin
2356 -- Consider only procedure bodies whose name matches one of the three
2357 -- controlled primitives.
2359 if Nkind (Body_Spec) /= N_Procedure_Specification
2360 or else not Nam_In (Chars (Body_Id), Name_Adjust,
2361 Name_Finalize,
2362 Name_Initialize)
2363 then
2364 return;
2366 -- A controlled primitive must have exactly one formal which is not
2367 -- an anonymous access type.
2369 elsif List_Length (Params) /= 1 then
2370 return;
2371 end if;
2373 Typ := Parameter_Type (First (Params));
2375 if Nkind (Typ) = N_Access_Definition then
2376 return;
2377 end if;
2379 Find_Type (Typ);
2381 -- The type of the formal must be derived from [Limited_]Controlled
2383 if not Is_Controlled (Entity (Typ)) then
2384 return;
2385 end if;
2387 -- Check whether a specification exists for this body. We do not
2388 -- analyze the spec of the body in full, because it will be analyzed
2389 -- again when the body is properly analyzed, and we cannot create
2390 -- duplicate entries in the formals chain. We look for an explicit
2391 -- specification because the body may be an overriding operation and
2392 -- an inherited spec may be present.
2394 Spec_Id := Current_Entity (Body_Id);
2396 while Present (Spec_Id) loop
2397 if Ekind_In (Spec_Id, E_Procedure, E_Generic_Procedure)
2398 and then Scope (Spec_Id) = Current_Scope
2399 and then Present (First_Formal (Spec_Id))
2400 and then No (Next_Formal (First_Formal (Spec_Id)))
2401 and then Etype (First_Formal (Spec_Id)) = Entity (Typ)
2402 and then Comes_From_Source (Spec_Id)
2403 then
2404 return;
2405 end if;
2407 Spec_Id := Homonym (Spec_Id);
2408 end loop;
2410 -- At this point the body is known to be a late controlled primitive.
2411 -- Generate a matching spec and insert it before the body. Note the
2412 -- use of Copy_Separate_Tree - we want an entirely separate semantic
2413 -- tree in this case.
2415 Spec := Copy_Separate_Tree (Body_Spec);
2417 -- Ensure that the subprogram declaration does not inherit the null
2418 -- indicator from the body as we now have a proper spec/body pair.
2420 Set_Null_Present (Spec, False);
2422 -- Ensure that the freeze node is inserted after the declaration of
2423 -- the primitive since its expansion will freeze the primitive.
2425 Decl := Make_Subprogram_Declaration (Loc, Specification => Spec);
2427 Insert_Before_And_Analyze (Body_Decl, Decl);
2428 end Handle_Late_Controlled_Primitive;
2430 ----------------------------------------
2431 -- Remove_Partial_Visible_Refinements --
2432 ----------------------------------------
2434 procedure Remove_Partial_Visible_Refinements (Spec_Id : Entity_Id) is
2435 State_Elmt : Elmt_Id;
2436 begin
2437 if Present (Abstract_States (Spec_Id)) then
2438 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2439 while Present (State_Elmt) loop
2440 Set_Has_Partial_Visible_Refinement (Node (State_Elmt), False);
2441 Next_Elmt (State_Elmt);
2442 end loop;
2443 end if;
2445 -- For a child unit, also hide the partial state refinement from
2446 -- ancestor packages.
2448 if Is_Child_Unit (Spec_Id) then
2449 Remove_Partial_Visible_Refinements (Scope (Spec_Id));
2450 end if;
2451 end Remove_Partial_Visible_Refinements;
2453 --------------------------------
2454 -- Remove_Visible_Refinements --
2455 --------------------------------
2457 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id) is
2458 State_Elmt : Elmt_Id;
2459 begin
2460 if Present (Abstract_States (Spec_Id)) then
2461 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2462 while Present (State_Elmt) loop
2463 Set_Has_Visible_Refinement (Node (State_Elmt), False);
2464 Next_Elmt (State_Elmt);
2465 end loop;
2466 end if;
2467 end Remove_Visible_Refinements;
2469 ---------------------
2470 -- Resolve_Aspects --
2471 ---------------------
2473 procedure Resolve_Aspects is
2474 E : Entity_Id;
2476 begin
2477 E := First_Entity (Current_Scope);
2478 while Present (E) loop
2479 Resolve_Aspect_Expressions (E);
2480 Next_Entity (E);
2481 end loop;
2482 end Resolve_Aspects;
2484 -- Local variables
2486 Context : Node_Id := Empty;
2487 Freeze_From : Entity_Id := Empty;
2488 Next_Decl : Node_Id;
2490 Body_Seen : Boolean := False;
2491 -- Flag set when the first body [stub] is encountered
2493 -- Start of processing for Analyze_Declarations
2495 begin
2496 if Restriction_Check_Required (SPARK_05) then
2497 Check_Later_Vs_Basic_Declarations (L, During_Parsing => False);
2498 end if;
2500 Decl := First (L);
2501 while Present (Decl) loop
2503 -- Package spec cannot contain a package declaration in SPARK
2505 if Nkind (Decl) = N_Package_Declaration
2506 and then Nkind (Parent (L)) = N_Package_Specification
2507 then
2508 Check_SPARK_05_Restriction
2509 ("package specification cannot contain a package declaration",
2510 Decl);
2511 end if;
2513 -- Complete analysis of declaration
2515 Analyze (Decl);
2516 Next_Decl := Next (Decl);
2518 if No (Freeze_From) then
2519 Freeze_From := First_Entity (Current_Scope);
2520 end if;
2522 -- At the end of a declarative part, freeze remaining entities
2523 -- declared in it. The end of the visible declarations of package
2524 -- specification is not the end of a declarative part if private
2525 -- declarations are present. The end of a package declaration is a
2526 -- freezing point only if it a library package. A task definition or
2527 -- protected type definition is not a freeze point either. Finally,
2528 -- we do not freeze entities in generic scopes, because there is no
2529 -- code generated for them and freeze nodes will be generated for
2530 -- the instance.
2532 -- The end of a package instantiation is not a freeze point, but
2533 -- for now we make it one, because the generic body is inserted
2534 -- (currently) immediately after. Generic instantiations will not
2535 -- be a freeze point once delayed freezing of bodies is implemented.
2536 -- (This is needed in any case for early instantiations ???).
2538 if No (Next_Decl) then
2539 if Nkind (Parent (L)) = N_Component_List then
2540 null;
2542 elsif Nkind_In (Parent (L), N_Protected_Definition,
2543 N_Task_Definition)
2544 then
2545 Check_Entry_Contracts;
2547 elsif Nkind (Parent (L)) /= N_Package_Specification then
2548 if Nkind (Parent (L)) = N_Package_Body then
2549 Freeze_From := First_Entity (Current_Scope);
2550 end if;
2552 -- There may have been several freezing points previously,
2553 -- for example object declarations or subprogram bodies, but
2554 -- at the end of a declarative part we check freezing from
2555 -- the beginning, even though entities may already be frozen,
2556 -- in order to perform visibility checks on delayed aspects.
2558 Adjust_Decl;
2559 Freeze_All (First_Entity (Current_Scope), Decl);
2560 Freeze_From := Last_Entity (Current_Scope);
2562 -- Current scope is a package specification
2564 elsif Scope (Current_Scope) /= Standard_Standard
2565 and then not Is_Child_Unit (Current_Scope)
2566 and then No (Generic_Parent (Parent (L)))
2567 then
2568 -- This is needed in all cases to catch visibility errors in
2569 -- aspect expressions, but several large user tests are now
2570 -- rejected. Pending notification we restrict this call to
2571 -- ASIS mode.
2573 if False and then ASIS_Mode then -- ????
2574 Resolve_Aspects;
2575 end if;
2577 elsif L /= Visible_Declarations (Parent (L))
2578 or else No (Private_Declarations (Parent (L)))
2579 or else Is_Empty_List (Private_Declarations (Parent (L)))
2580 then
2581 Adjust_Decl;
2583 -- End of a package declaration
2585 -- In compilation mode the expansion of freeze node takes care
2586 -- of resolving expressions of all aspects in the list. In ASIS
2587 -- mode this must be done explicitly.
2589 if ASIS_Mode
2590 and then Scope (Current_Scope) = Standard_Standard
2591 then
2592 Resolve_Aspects;
2593 end if;
2595 -- This is a freeze point because it is the end of a
2596 -- compilation unit.
2598 Freeze_All (First_Entity (Current_Scope), Decl);
2599 Freeze_From := Last_Entity (Current_Scope);
2601 -- At the end of the visible declarations the expressions in
2602 -- aspects of all entities declared so far must be resolved.
2603 -- The entities themselves might be frozen later, and the
2604 -- generated pragmas and attribute definition clauses analyzed
2605 -- in full at that point, but name resolution must take place
2606 -- now.
2607 -- In addition to being the proper semantics, this is mandatory
2608 -- within generic units, because global name capture requires
2609 -- those expressions to be analyzed, given that the generated
2610 -- pragmas do not appear in the original generic tree.
2612 elsif Serious_Errors_Detected = 0 then
2613 Resolve_Aspects;
2614 end if;
2616 -- If next node is a body then freeze all types before the body.
2617 -- An exception occurs for some expander-generated bodies. If these
2618 -- are generated at places where in general language rules would not
2619 -- allow a freeze point, then we assume that the expander has
2620 -- explicitly checked that all required types are properly frozen,
2621 -- and we do not cause general freezing here. This special circuit
2622 -- is used when the encountered body is marked as having already
2623 -- been analyzed.
2625 -- In all other cases (bodies that come from source, and expander
2626 -- generated bodies that have not been analyzed yet), freeze all
2627 -- types now. Note that in the latter case, the expander must take
2628 -- care to attach the bodies at a proper place in the tree so as to
2629 -- not cause unwanted freezing at that point.
2631 elsif not Analyzed (Next_Decl) and then Is_Body (Next_Decl) then
2633 -- When a controlled type is frozen, the expander generates stream
2634 -- and controlled type support routines. If the freeze is caused
2635 -- by the stand alone body of Initialize, Adjust and Finalize, the
2636 -- expander will end up using the wrong version of these routines
2637 -- as the body has not been processed yet. To remedy this, detect
2638 -- a late controlled primitive and create a proper spec for it.
2639 -- This ensures that the primitive will override its inherited
2640 -- counterpart before the freeze takes place.
2642 -- If the declaration we just processed is a body, do not attempt
2643 -- to examine Next_Decl as the late primitive idiom can only apply
2644 -- to the first encountered body.
2646 -- The spec of the late primitive is not generated in ASIS mode to
2647 -- ensure a consistent list of primitives that indicates the true
2648 -- semantic structure of the program (which is not relevant when
2649 -- generating executable code.
2651 -- ??? a cleaner approach may be possible and/or this solution
2652 -- could be extended to general-purpose late primitives, TBD.
2654 if not ASIS_Mode and then not Body_Seen and then not Is_Body (Decl)
2655 then
2656 Body_Seen := True;
2658 if Nkind (Next_Decl) = N_Subprogram_Body then
2659 Handle_Late_Controlled_Primitive (Next_Decl);
2660 end if;
2661 end if;
2663 Adjust_Decl;
2665 -- The generated body of an expression function does not freeze,
2666 -- unless it is a completion, in which case only the expression
2667 -- itself freezes. THis is handled when the body itself is
2668 -- analyzed (see Freeze_Expr_Types, sem_ch6.adb).
2670 Freeze_All (Freeze_From, Decl);
2671 Freeze_From := Last_Entity (Current_Scope);
2672 end if;
2674 Decl := Next_Decl;
2675 end loop;
2677 -- Post-freezing actions
2679 if Present (L) then
2680 Context := Parent (L);
2682 -- Analyze the contracts of packages and their bodies
2684 if Nkind (Context) = N_Package_Specification then
2686 -- When a package has private declarations, its contract must be
2687 -- analyzed at the end of the said declarations. This way both the
2688 -- analysis and freeze actions are properly synchronized in case
2689 -- of private type use within the contract.
2691 if L = Private_Declarations (Context) then
2692 Analyze_Package_Contract (Defining_Entity (Context));
2694 -- Otherwise the contract is analyzed at the end of the visible
2695 -- declarations.
2697 elsif L = Visible_Declarations (Context)
2698 and then No (Private_Declarations (Context))
2699 then
2700 Analyze_Package_Contract (Defining_Entity (Context));
2701 end if;
2703 elsif Nkind (Context) = N_Package_Body then
2704 Analyze_Package_Body_Contract (Defining_Entity (Context));
2705 end if;
2707 -- Analyze the contracts of various constructs now due to the delayed
2708 -- visibility needs of their aspects and pragmas.
2710 Analyze_Contracts (L);
2712 if Nkind (Context) = N_Package_Body then
2714 -- Ensure that all abstract states and objects declared in the
2715 -- state space of a package body are utilized as constituents.
2717 Check_Unused_Body_States (Defining_Entity (Context));
2719 -- State refinements are visible up to the end of the package body
2720 -- declarations. Hide the state refinements from visibility to
2721 -- restore the original state conditions.
2723 Remove_Visible_Refinements (Corresponding_Spec (Context));
2724 Remove_Partial_Visible_Refinements (Corresponding_Spec (Context));
2726 elsif Nkind (Context) = N_Package_Declaration then
2728 -- Partial state refinements are visible up to the end of the
2729 -- package spec declarations. Hide the partial state refinements
2730 -- from visibility to restore the original state conditions.
2732 Remove_Partial_Visible_Refinements (Corresponding_Spec (Context));
2733 end if;
2735 -- Verify that all abstract states found in any package declared in
2736 -- the input declarative list have proper refinements. The check is
2737 -- performed only when the context denotes a block, entry, package,
2738 -- protected, subprogram, or task body (SPARK RM 7.2.2(3)).
2740 Check_State_Refinements (Context);
2742 -- Create the subprogram bodies which verify the run-time semantics
2743 -- of pragmas Default_Initial_Condition and [Type_]Invariant for all
2744 -- types within the current declarative list. This ensures that all
2745 -- assertion expressions are preanalyzed and resolved at the end of
2746 -- the declarative part. Note that the resolution happens even when
2747 -- freezing does not take place.
2749 Build_Assertion_Bodies (L, Context);
2750 end if;
2751 end Analyze_Declarations;
2753 -----------------------------------
2754 -- Analyze_Full_Type_Declaration --
2755 -----------------------------------
2757 procedure Analyze_Full_Type_Declaration (N : Node_Id) is
2758 Def : constant Node_Id := Type_Definition (N);
2759 Def_Id : constant Entity_Id := Defining_Identifier (N);
2760 T : Entity_Id;
2761 Prev : Entity_Id;
2763 Is_Remote : constant Boolean :=
2764 (Is_Remote_Types (Current_Scope)
2765 or else Is_Remote_Call_Interface (Current_Scope))
2766 and then not (In_Private_Part (Current_Scope)
2767 or else In_Package_Body (Current_Scope));
2769 procedure Check_Nonoverridable_Aspects;
2770 -- Apply the rule in RM 13.1.1(18.4/4) on iterator aspects that cannot
2771 -- be overridden, and can only be confirmed on derivation.
2773 procedure Check_Ops_From_Incomplete_Type;
2774 -- If there is a tagged incomplete partial view of the type, traverse
2775 -- the primitives of the incomplete view and change the type of any
2776 -- controlling formals and result to indicate the full view. The
2777 -- primitives will be added to the full type's primitive operations
2778 -- list later in Sem_Disp.Check_Operation_From_Incomplete_Type (which
2779 -- is called from Process_Incomplete_Dependents).
2781 ----------------------------------
2782 -- Check_Nonoverridable_Aspects --
2783 ----------------------------------
2785 procedure Check_Nonoverridable_Aspects is
2786 function Get_Aspect_Spec
2787 (Specs : List_Id;
2788 Aspect_Name : Name_Id) return Node_Id;
2789 -- Check whether a list of aspect specifications includes an entry
2790 -- for a specific aspect. The list is either that of a partial or
2791 -- a full view.
2793 ---------------------
2794 -- Get_Aspect_Spec --
2795 ---------------------
2797 function Get_Aspect_Spec
2798 (Specs : List_Id;
2799 Aspect_Name : Name_Id) return Node_Id
2801 Spec : Node_Id;
2803 begin
2804 Spec := First (Specs);
2805 while Present (Spec) loop
2806 if Chars (Identifier (Spec)) = Aspect_Name then
2807 return Spec;
2808 end if;
2809 Next (Spec);
2810 end loop;
2812 return Empty;
2813 end Get_Aspect_Spec;
2815 -- Local variables
2817 Prev_Aspects : constant List_Id :=
2818 Aspect_Specifications (Parent (Def_Id));
2819 Par_Type : Entity_Id;
2820 Prev_Aspect : Node_Id;
2822 -- Start of processing for Check_Nonoverridable_Aspects
2824 begin
2825 -- Get parent type of derived type. Note that Prev is the entity in
2826 -- the partial declaration, but its contents are now those of full
2827 -- view, while Def_Id reflects the partial view.
2829 if Is_Private_Type (Def_Id) then
2830 Par_Type := Etype (Full_View (Def_Id));
2831 else
2832 Par_Type := Etype (Def_Id);
2833 end if;
2835 -- If there is an inherited Implicit_Dereference, verify that it is
2836 -- made explicit in the partial view.
2838 if Has_Discriminants (Base_Type (Par_Type))
2839 and then Nkind (Parent (Prev)) = N_Full_Type_Declaration
2840 and then Present (Discriminant_Specifications (Parent (Prev)))
2841 and then Present (Get_Reference_Discriminant (Par_Type))
2842 then
2843 Prev_Aspect :=
2844 Get_Aspect_Spec (Prev_Aspects, Name_Implicit_Dereference);
2846 if No (Prev_Aspect)
2847 and then Present
2848 (Discriminant_Specifications
2849 (Original_Node (Parent (Prev))))
2850 then
2851 Error_Msg_N
2852 ("type does not inherit implicit dereference", Prev);
2854 else
2855 -- If one of the views has the aspect specified, verify that it
2856 -- is consistent with that of the parent.
2858 declare
2859 Par_Discr : constant Entity_Id :=
2860 Get_Reference_Discriminant (Par_Type);
2861 Cur_Discr : constant Entity_Id :=
2862 Get_Reference_Discriminant (Prev);
2864 begin
2865 if Corresponding_Discriminant (Cur_Discr) /= Par_Discr then
2866 Error_Msg_N ("aspect incosistent with that of parent", N);
2867 end if;
2869 -- Check that specification in partial view matches the
2870 -- inherited aspect. Compare names directly because aspect
2871 -- expression may not be analyzed.
2873 if Present (Prev_Aspect)
2874 and then Nkind (Expression (Prev_Aspect)) = N_Identifier
2875 and then Chars (Expression (Prev_Aspect)) /=
2876 Chars (Cur_Discr)
2877 then
2878 Error_Msg_N
2879 ("aspect incosistent with that of parent", N);
2880 end if;
2881 end;
2882 end if;
2883 end if;
2885 -- TBD : other nonoverridable aspects.
2886 end Check_Nonoverridable_Aspects;
2888 ------------------------------------
2889 -- Check_Ops_From_Incomplete_Type --
2890 ------------------------------------
2892 procedure Check_Ops_From_Incomplete_Type is
2893 Elmt : Elmt_Id;
2894 Formal : Entity_Id;
2895 Op : Entity_Id;
2897 begin
2898 if Prev /= T
2899 and then Ekind (Prev) = E_Incomplete_Type
2900 and then Is_Tagged_Type (Prev)
2901 and then Is_Tagged_Type (T)
2902 then
2903 Elmt := First_Elmt (Primitive_Operations (Prev));
2904 while Present (Elmt) loop
2905 Op := Node (Elmt);
2907 Formal := First_Formal (Op);
2908 while Present (Formal) loop
2909 if Etype (Formal) = Prev then
2910 Set_Etype (Formal, T);
2911 end if;
2913 Next_Formal (Formal);
2914 end loop;
2916 if Etype (Op) = Prev then
2917 Set_Etype (Op, T);
2918 end if;
2920 Next_Elmt (Elmt);
2921 end loop;
2922 end if;
2923 end Check_Ops_From_Incomplete_Type;
2925 -- Start of processing for Analyze_Full_Type_Declaration
2927 begin
2928 Prev := Find_Type_Name (N);
2930 -- The full view, if present, now points to the current type. If there
2931 -- is an incomplete partial view, set a link to it, to simplify the
2932 -- retrieval of primitive operations of the type.
2934 -- Ada 2005 (AI-50217): If the type was previously decorated when
2935 -- imported through a LIMITED WITH clause, it appears as incomplete
2936 -- but has no full view.
2938 if Ekind (Prev) = E_Incomplete_Type
2939 and then Present (Full_View (Prev))
2940 then
2941 T := Full_View (Prev);
2942 Set_Incomplete_View (N, Parent (Prev));
2943 else
2944 T := Prev;
2945 end if;
2947 Set_Is_Pure (T, Is_Pure (Current_Scope));
2949 -- We set the flag Is_First_Subtype here. It is needed to set the
2950 -- corresponding flag for the Implicit class-wide-type created
2951 -- during tagged types processing.
2953 Set_Is_First_Subtype (T, True);
2955 -- Only composite types other than array types are allowed to have
2956 -- discriminants.
2958 case Nkind (Def) is
2960 -- For derived types, the rule will be checked once we've figured
2961 -- out the parent type.
2963 when N_Derived_Type_Definition =>
2964 null;
2966 -- For record types, discriminants are allowed, unless we are in
2967 -- SPARK.
2969 when N_Record_Definition =>
2970 if Present (Discriminant_Specifications (N)) then
2971 Check_SPARK_05_Restriction
2972 ("discriminant type is not allowed",
2973 Defining_Identifier
2974 (First (Discriminant_Specifications (N))));
2975 end if;
2977 when others =>
2978 if Present (Discriminant_Specifications (N)) then
2979 Error_Msg_N
2980 ("elementary or array type cannot have discriminants",
2981 Defining_Identifier
2982 (First (Discriminant_Specifications (N))));
2983 end if;
2984 end case;
2986 -- Elaborate the type definition according to kind, and generate
2987 -- subsidiary (implicit) subtypes where needed. We skip this if it was
2988 -- already done (this happens during the reanalysis that follows a call
2989 -- to the high level optimizer).
2991 if not Analyzed (T) then
2992 Set_Analyzed (T);
2994 case Nkind (Def) is
2995 when N_Access_To_Subprogram_Definition =>
2996 Access_Subprogram_Declaration (T, Def);
2998 -- If this is a remote access to subprogram, we must create the
2999 -- equivalent fat pointer type, and related subprograms.
3001 if Is_Remote then
3002 Process_Remote_AST_Declaration (N);
3003 end if;
3005 -- Validate categorization rule against access type declaration
3006 -- usually a violation in Pure unit, Shared_Passive unit.
3008 Validate_Access_Type_Declaration (T, N);
3010 when N_Access_To_Object_Definition =>
3011 Access_Type_Declaration (T, Def);
3013 -- Validate categorization rule against access type declaration
3014 -- usually a violation in Pure unit, Shared_Passive unit.
3016 Validate_Access_Type_Declaration (T, N);
3018 -- If we are in a Remote_Call_Interface package and define a
3019 -- RACW, then calling stubs and specific stream attributes
3020 -- must be added.
3022 if Is_Remote
3023 and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
3024 then
3025 Add_RACW_Features (Def_Id);
3026 end if;
3028 when N_Array_Type_Definition =>
3029 Array_Type_Declaration (T, Def);
3031 when N_Derived_Type_Definition =>
3032 Derived_Type_Declaration (T, N, T /= Def_Id);
3034 when N_Enumeration_Type_Definition =>
3035 Enumeration_Type_Declaration (T, Def);
3037 when N_Floating_Point_Definition =>
3038 Floating_Point_Type_Declaration (T, Def);
3040 when N_Decimal_Fixed_Point_Definition =>
3041 Decimal_Fixed_Point_Type_Declaration (T, Def);
3043 when N_Ordinary_Fixed_Point_Definition =>
3044 Ordinary_Fixed_Point_Type_Declaration (T, Def);
3046 when N_Signed_Integer_Type_Definition =>
3047 Signed_Integer_Type_Declaration (T, Def);
3049 when N_Modular_Type_Definition =>
3050 Modular_Type_Declaration (T, Def);
3052 when N_Record_Definition =>
3053 Record_Type_Declaration (T, N, Prev);
3055 -- If declaration has a parse error, nothing to elaborate.
3057 when N_Error =>
3058 null;
3060 when others =>
3061 raise Program_Error;
3062 end case;
3063 end if;
3065 if Etype (T) = Any_Type then
3066 return;
3067 end if;
3069 -- Controlled type is not allowed in SPARK
3071 if Is_Visibly_Controlled (T) then
3072 Check_SPARK_05_Restriction ("controlled type is not allowed", N);
3073 end if;
3075 -- Some common processing for all types
3077 Set_Depends_On_Private (T, Has_Private_Component (T));
3078 Check_Ops_From_Incomplete_Type;
3080 -- Both the declared entity, and its anonymous base type if one was
3081 -- created, need freeze nodes allocated.
3083 declare
3084 B : constant Entity_Id := Base_Type (T);
3086 begin
3087 -- In the case where the base type differs from the first subtype, we
3088 -- pre-allocate a freeze node, and set the proper link to the first
3089 -- subtype. Freeze_Entity will use this preallocated freeze node when
3090 -- it freezes the entity.
3092 -- This does not apply if the base type is a generic type, whose
3093 -- declaration is independent of the current derived definition.
3095 if B /= T and then not Is_Generic_Type (B) then
3096 Ensure_Freeze_Node (B);
3097 Set_First_Subtype_Link (Freeze_Node (B), T);
3098 end if;
3100 -- A type that is imported through a limited_with clause cannot
3101 -- generate any code, and thus need not be frozen. However, an access
3102 -- type with an imported designated type needs a finalization list,
3103 -- which may be referenced in some other package that has non-limited
3104 -- visibility on the designated type. Thus we must create the
3105 -- finalization list at the point the access type is frozen, to
3106 -- prevent unsatisfied references at link time.
3108 if not From_Limited_With (T) or else Is_Access_Type (T) then
3109 Set_Has_Delayed_Freeze (T);
3110 end if;
3111 end;
3113 -- Case where T is the full declaration of some private type which has
3114 -- been swapped in Defining_Identifier (N).
3116 if T /= Def_Id and then Is_Private_Type (Def_Id) then
3117 Process_Full_View (N, T, Def_Id);
3119 -- Record the reference. The form of this is a little strange, since
3120 -- the full declaration has been swapped in. So the first parameter
3121 -- here represents the entity to which a reference is made which is
3122 -- the "real" entity, i.e. the one swapped in, and the second
3123 -- parameter provides the reference location.
3125 -- Also, we want to kill Has_Pragma_Unreferenced temporarily here
3126 -- since we don't want a complaint about the full type being an
3127 -- unwanted reference to the private type
3129 declare
3130 B : constant Boolean := Has_Pragma_Unreferenced (T);
3131 begin
3132 Set_Has_Pragma_Unreferenced (T, False);
3133 Generate_Reference (T, T, 'c');
3134 Set_Has_Pragma_Unreferenced (T, B);
3135 end;
3137 Set_Completion_Referenced (Def_Id);
3139 -- For completion of incomplete type, process incomplete dependents
3140 -- and always mark the full type as referenced (it is the incomplete
3141 -- type that we get for any real reference).
3143 elsif Ekind (Prev) = E_Incomplete_Type then
3144 Process_Incomplete_Dependents (N, T, Prev);
3145 Generate_Reference (Prev, Def_Id, 'c');
3146 Set_Completion_Referenced (Def_Id);
3148 -- If not private type or incomplete type completion, this is a real
3149 -- definition of a new entity, so record it.
3151 else
3152 Generate_Definition (Def_Id);
3153 end if;
3155 -- Propagate any pending access types whose finalization masters need to
3156 -- be fully initialized from the partial to the full view. Guard against
3157 -- an illegal full view that remains unanalyzed.
3159 if Is_Type (Def_Id) and then Is_Incomplete_Or_Private_Type (Prev) then
3160 Set_Pending_Access_Types (Def_Id, Pending_Access_Types (Prev));
3161 end if;
3163 if Chars (Scope (Def_Id)) = Name_System
3164 and then Chars (Def_Id) = Name_Address
3165 and then Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (N)))
3166 then
3167 Set_Is_Descendant_Of_Address (Def_Id);
3168 Set_Is_Descendant_Of_Address (Base_Type (Def_Id));
3169 Set_Is_Descendant_Of_Address (Prev);
3170 end if;
3172 Set_Optimize_Alignment_Flags (Def_Id);
3173 Check_Eliminated (Def_Id);
3175 -- If the declaration is a completion and aspects are present, apply
3176 -- them to the entity for the type which is currently the partial
3177 -- view, but which is the one that will be frozen.
3179 if Has_Aspects (N) then
3181 -- In most cases the partial view is a private type, and both views
3182 -- appear in different declarative parts. In the unusual case where
3183 -- the partial view is incomplete, perform the analysis on the
3184 -- full view, to prevent freezing anomalies with the corresponding
3185 -- class-wide type, which otherwise might be frozen before the
3186 -- dispatch table is built.
3188 if Prev /= Def_Id
3189 and then Ekind (Prev) /= E_Incomplete_Type
3190 then
3191 Analyze_Aspect_Specifications (N, Prev);
3193 -- Normal case
3195 else
3196 Analyze_Aspect_Specifications (N, Def_Id);
3197 end if;
3198 end if;
3200 if Is_Derived_Type (Prev)
3201 and then Def_Id /= Prev
3202 then
3203 Check_Nonoverridable_Aspects;
3204 end if;
3205 end Analyze_Full_Type_Declaration;
3207 ----------------------------------
3208 -- Analyze_Incomplete_Type_Decl --
3209 ----------------------------------
3211 procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
3212 F : constant Boolean := Is_Pure (Current_Scope);
3213 T : Entity_Id;
3215 begin
3216 Check_SPARK_05_Restriction ("incomplete type is not allowed", N);
3218 Generate_Definition (Defining_Identifier (N));
3220 -- Process an incomplete declaration. The identifier must not have been
3221 -- declared already in the scope. However, an incomplete declaration may
3222 -- appear in the private part of a package, for a private type that has
3223 -- already been declared.
3225 -- In this case, the discriminants (if any) must match
3227 T := Find_Type_Name (N);
3229 Set_Ekind (T, E_Incomplete_Type);
3230 Init_Size_Align (T);
3231 Set_Is_First_Subtype (T, True);
3232 Set_Etype (T, T);
3234 -- Ada 2005 (AI-326): Minimum decoration to give support to tagged
3235 -- incomplete types.
3237 if Tagged_Present (N) then
3238 Set_Is_Tagged_Type (T, True);
3239 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
3240 Make_Class_Wide_Type (T);
3241 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3242 end if;
3244 Set_Stored_Constraint (T, No_Elist);
3246 if Present (Discriminant_Specifications (N)) then
3247 Push_Scope (T);
3248 Process_Discriminants (N);
3249 End_Scope;
3250 end if;
3252 -- If the type has discriminants, nontrivial subtypes may be declared
3253 -- before the full view of the type. The full views of those subtypes
3254 -- will be built after the full view of the type.
3256 Set_Private_Dependents (T, New_Elmt_List);
3257 Set_Is_Pure (T, F);
3258 end Analyze_Incomplete_Type_Decl;
3260 -----------------------------------
3261 -- Analyze_Interface_Declaration --
3262 -----------------------------------
3264 procedure Analyze_Interface_Declaration (T : Entity_Id; Def : Node_Id) is
3265 CW : constant Entity_Id := Class_Wide_Type (T);
3267 begin
3268 Set_Is_Tagged_Type (T);
3269 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
3271 Set_Is_Limited_Record (T, Limited_Present (Def)
3272 or else Task_Present (Def)
3273 or else Protected_Present (Def)
3274 or else Synchronized_Present (Def));
3276 -- Type is abstract if full declaration carries keyword, or if previous
3277 -- partial view did.
3279 Set_Is_Abstract_Type (T);
3280 Set_Is_Interface (T);
3282 -- Type is a limited interface if it includes the keyword limited, task,
3283 -- protected, or synchronized.
3285 Set_Is_Limited_Interface
3286 (T, Limited_Present (Def)
3287 or else Protected_Present (Def)
3288 or else Synchronized_Present (Def)
3289 or else Task_Present (Def));
3291 Set_Interfaces (T, New_Elmt_List);
3292 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3294 -- Complete the decoration of the class-wide entity if it was already
3295 -- built (i.e. during the creation of the limited view)
3297 if Present (CW) then
3298 Set_Is_Interface (CW);
3299 Set_Is_Limited_Interface (CW, Is_Limited_Interface (T));
3300 end if;
3302 -- Check runtime support for synchronized interfaces
3304 if (Is_Task_Interface (T)
3305 or else Is_Protected_Interface (T)
3306 or else Is_Synchronized_Interface (T))
3307 and then not RTE_Available (RE_Select_Specific_Data)
3308 then
3309 Error_Msg_CRT ("synchronized interfaces", T);
3310 end if;
3311 end Analyze_Interface_Declaration;
3313 -----------------------------
3314 -- Analyze_Itype_Reference --
3315 -----------------------------
3317 -- Nothing to do. This node is placed in the tree only for the benefit of
3318 -- back end processing, and has no effect on the semantic processing.
3320 procedure Analyze_Itype_Reference (N : Node_Id) is
3321 begin
3322 pragma Assert (Is_Itype (Itype (N)));
3323 null;
3324 end Analyze_Itype_Reference;
3326 --------------------------------
3327 -- Analyze_Number_Declaration --
3328 --------------------------------
3330 procedure Analyze_Number_Declaration (N : Node_Id) is
3331 E : constant Node_Id := Expression (N);
3332 Id : constant Entity_Id := Defining_Identifier (N);
3333 Index : Interp_Index;
3334 It : Interp;
3335 T : Entity_Id;
3337 begin
3338 Generate_Definition (Id);
3339 Enter_Name (Id);
3341 -- This is an optimization of a common case of an integer literal
3343 if Nkind (E) = N_Integer_Literal then
3344 Set_Is_Static_Expression (E, True);
3345 Set_Etype (E, Universal_Integer);
3347 Set_Etype (Id, Universal_Integer);
3348 Set_Ekind (Id, E_Named_Integer);
3349 Set_Is_Frozen (Id, True);
3350 return;
3351 end if;
3353 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3355 -- Process expression, replacing error by integer zero, to avoid
3356 -- cascaded errors or aborts further along in the processing
3358 -- Replace Error by integer zero, which seems least likely to cause
3359 -- cascaded errors.
3361 if E = Error then
3362 Rewrite (E, Make_Integer_Literal (Sloc (E), Uint_0));
3363 Set_Error_Posted (E);
3364 end if;
3366 Analyze (E);
3368 -- Verify that the expression is static and numeric. If
3369 -- the expression is overloaded, we apply the preference
3370 -- rule that favors root numeric types.
3372 if not Is_Overloaded (E) then
3373 T := Etype (E);
3374 if Has_Dynamic_Predicate_Aspect (T) then
3375 Error_Msg_N
3376 ("subtype has dynamic predicate, "
3377 & "not allowed in number declaration", N);
3378 end if;
3380 else
3381 T := Any_Type;
3383 Get_First_Interp (E, Index, It);
3384 while Present (It.Typ) loop
3385 if (Is_Integer_Type (It.Typ) or else Is_Real_Type (It.Typ))
3386 and then (Scope (Base_Type (It.Typ))) = Standard_Standard
3387 then
3388 if T = Any_Type then
3389 T := It.Typ;
3391 elsif It.Typ = Universal_Real
3392 or else
3393 It.Typ = Universal_Integer
3394 then
3395 -- Choose universal interpretation over any other
3397 T := It.Typ;
3398 exit;
3399 end if;
3400 end if;
3402 Get_Next_Interp (Index, It);
3403 end loop;
3404 end if;
3406 if Is_Integer_Type (T) then
3407 Resolve (E, T);
3408 Set_Etype (Id, Universal_Integer);
3409 Set_Ekind (Id, E_Named_Integer);
3411 elsif Is_Real_Type (T) then
3413 -- Because the real value is converted to universal_real, this is a
3414 -- legal context for a universal fixed expression.
3416 if T = Universal_Fixed then
3417 declare
3418 Loc : constant Source_Ptr := Sloc (N);
3419 Conv : constant Node_Id := Make_Type_Conversion (Loc,
3420 Subtype_Mark =>
3421 New_Occurrence_Of (Universal_Real, Loc),
3422 Expression => Relocate_Node (E));
3424 begin
3425 Rewrite (E, Conv);
3426 Analyze (E);
3427 end;
3429 elsif T = Any_Fixed then
3430 Error_Msg_N ("illegal context for mixed mode operation", E);
3432 -- Expression is of the form : universal_fixed * integer. Try to
3433 -- resolve as universal_real.
3435 T := Universal_Real;
3436 Set_Etype (E, T);
3437 end if;
3439 Resolve (E, T);
3440 Set_Etype (Id, Universal_Real);
3441 Set_Ekind (Id, E_Named_Real);
3443 else
3444 Wrong_Type (E, Any_Numeric);
3445 Resolve (E, T);
3447 Set_Etype (Id, T);
3448 Set_Ekind (Id, E_Constant);
3449 Set_Never_Set_In_Source (Id, True);
3450 Set_Is_True_Constant (Id, True);
3451 return;
3452 end if;
3454 if Nkind_In (E, N_Integer_Literal, N_Real_Literal) then
3455 Set_Etype (E, Etype (Id));
3456 end if;
3458 if not Is_OK_Static_Expression (E) then
3459 Flag_Non_Static_Expr
3460 ("non-static expression used in number declaration!", E);
3461 Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
3462 Set_Etype (E, Any_Type);
3463 end if;
3465 Analyze_Dimension (N);
3466 end Analyze_Number_Declaration;
3468 --------------------------------
3469 -- Analyze_Object_Declaration --
3470 --------------------------------
3472 -- WARNING: This routine manages Ghost regions. Return statements must be
3473 -- replaced by gotos which jump to the end of the routine and restore the
3474 -- Ghost mode.
3476 procedure Analyze_Object_Declaration (N : Node_Id) is
3477 Loc : constant Source_Ptr := Sloc (N);
3478 Id : constant Entity_Id := Defining_Identifier (N);
3479 Act_T : Entity_Id;
3480 T : Entity_Id;
3482 E : Node_Id := Expression (N);
3483 -- E is set to Expression (N) throughout this routine. When
3484 -- Expression (N) is modified, E is changed accordingly.
3486 Prev_Entity : Entity_Id := Empty;
3488 function Count_Tasks (T : Entity_Id) return Uint;
3489 -- This function is called when a non-generic library level object of a
3490 -- task type is declared. Its function is to count the static number of
3491 -- tasks declared within the type (it is only called if Has_Task is set
3492 -- for T). As a side effect, if an array of tasks with non-static bounds
3493 -- or a variant record type is encountered, Check_Restriction is called
3494 -- indicating the count is unknown.
3496 function Delayed_Aspect_Present return Boolean;
3497 -- If the declaration has an expression that is an aggregate, and it
3498 -- has aspects that require delayed analysis, the resolution of the
3499 -- aggregate must be deferred to the freeze point of the objet. This
3500 -- special processing was created for address clauses, but it must
3501 -- also apply to Alignment. This must be done before the aspect
3502 -- specifications are analyzed because we must handle the aggregate
3503 -- before the analysis of the object declaration is complete.
3505 -- Any other relevant delayed aspects on object declarations ???
3507 -----------------
3508 -- Count_Tasks --
3509 -----------------
3511 function Count_Tasks (T : Entity_Id) return Uint is
3512 C : Entity_Id;
3513 X : Node_Id;
3514 V : Uint;
3516 begin
3517 if Is_Task_Type (T) then
3518 return Uint_1;
3520 elsif Is_Record_Type (T) then
3521 if Has_Discriminants (T) then
3522 Check_Restriction (Max_Tasks, N);
3523 return Uint_0;
3525 else
3526 V := Uint_0;
3527 C := First_Component (T);
3528 while Present (C) loop
3529 V := V + Count_Tasks (Etype (C));
3530 Next_Component (C);
3531 end loop;
3533 return V;
3534 end if;
3536 elsif Is_Array_Type (T) then
3537 X := First_Index (T);
3538 V := Count_Tasks (Component_Type (T));
3539 while Present (X) loop
3540 C := Etype (X);
3542 if not Is_OK_Static_Subtype (C) then
3543 Check_Restriction (Max_Tasks, N);
3544 return Uint_0;
3545 else
3546 V := V * (UI_Max (Uint_0,
3547 Expr_Value (Type_High_Bound (C)) -
3548 Expr_Value (Type_Low_Bound (C)) + Uint_1));
3549 end if;
3551 Next_Index (X);
3552 end loop;
3554 return V;
3556 else
3557 return Uint_0;
3558 end if;
3559 end Count_Tasks;
3561 ----------------------------
3562 -- Delayed_Aspect_Present --
3563 ----------------------------
3565 function Delayed_Aspect_Present return Boolean is
3566 A : Node_Id;
3567 A_Id : Aspect_Id;
3569 begin
3570 if Present (Aspect_Specifications (N)) then
3571 A := First (Aspect_Specifications (N));
3572 A_Id := Get_Aspect_Id (Chars (Identifier (A)));
3573 while Present (A) loop
3574 if A_Id = Aspect_Alignment or else A_Id = Aspect_Address then
3575 return True;
3576 end if;
3578 Next (A);
3579 end loop;
3580 end if;
3582 return False;
3583 end Delayed_Aspect_Present;
3585 -- Local variables
3587 Mode : Ghost_Mode_Type;
3588 Mode_Set : Boolean := False;
3589 Related_Id : Entity_Id;
3591 -- Start of processing for Analyze_Object_Declaration
3593 begin
3594 -- There are three kinds of implicit types generated by an
3595 -- object declaration:
3597 -- 1. Those generated by the original Object Definition
3599 -- 2. Those generated by the Expression
3601 -- 3. Those used to constrain the Object Definition with the
3602 -- expression constraints when the definition is unconstrained.
3604 -- They must be generated in this order to avoid order of elaboration
3605 -- issues. Thus the first step (after entering the name) is to analyze
3606 -- the object definition.
3608 if Constant_Present (N) then
3609 Prev_Entity := Current_Entity_In_Scope (Id);
3611 if Present (Prev_Entity)
3612 and then
3613 -- If the homograph is an implicit subprogram, it is overridden
3614 -- by the current declaration.
3616 ((Is_Overloadable (Prev_Entity)
3617 and then Is_Inherited_Operation (Prev_Entity))
3619 -- The current object is a discriminal generated for an entry
3620 -- family index. Even though the index is a constant, in this
3621 -- particular context there is no true constant redeclaration.
3622 -- Enter_Name will handle the visibility.
3624 or else
3625 (Is_Discriminal (Id)
3626 and then Ekind (Discriminal_Link (Id)) =
3627 E_Entry_Index_Parameter)
3629 -- The current object is the renaming for a generic declared
3630 -- within the instance.
3632 or else
3633 (Ekind (Prev_Entity) = E_Package
3634 and then Nkind (Parent (Prev_Entity)) =
3635 N_Package_Renaming_Declaration
3636 and then not Comes_From_Source (Prev_Entity)
3637 and then
3638 Is_Generic_Instance (Renamed_Entity (Prev_Entity)))
3640 -- The entity may be a homonym of a private component of the
3641 -- enclosing protected object, for which we create a local
3642 -- renaming declaration. The declaration is legal, even if
3643 -- useless when it just captures that component.
3645 or else
3646 (Ekind (Scope (Current_Scope)) = E_Protected_Type
3647 and then Nkind (Parent (Prev_Entity)) =
3648 N_Object_Renaming_Declaration))
3649 then
3650 Prev_Entity := Empty;
3651 end if;
3652 end if;
3654 if Present (Prev_Entity) then
3656 -- The object declaration is Ghost when it completes a deferred Ghost
3657 -- constant.
3659 Mark_And_Set_Ghost_Completion (N, Prev_Entity, Mode);
3660 Mode_Set := True;
3662 Constant_Redeclaration (Id, N, T);
3664 Generate_Reference (Prev_Entity, Id, 'c');
3665 Set_Completion_Referenced (Id);
3667 if Error_Posted (N) then
3669 -- Type mismatch or illegal redeclaration; do not analyze
3670 -- expression to avoid cascaded errors.
3672 T := Find_Type_Of_Object (Object_Definition (N), N);
3673 Set_Etype (Id, T);
3674 Set_Ekind (Id, E_Variable);
3675 goto Leave;
3676 end if;
3678 -- In the normal case, enter identifier at the start to catch premature
3679 -- usage in the initialization expression.
3681 else
3682 Generate_Definition (Id);
3683 Enter_Name (Id);
3685 Mark_Coextensions (N, Object_Definition (N));
3687 T := Find_Type_Of_Object (Object_Definition (N), N);
3689 if Nkind (Object_Definition (N)) = N_Access_Definition
3690 and then Present
3691 (Access_To_Subprogram_Definition (Object_Definition (N)))
3692 and then Protected_Present
3693 (Access_To_Subprogram_Definition (Object_Definition (N)))
3694 then
3695 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
3696 end if;
3698 if Error_Posted (Id) then
3699 Set_Etype (Id, T);
3700 Set_Ekind (Id, E_Variable);
3701 goto Leave;
3702 end if;
3703 end if;
3705 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
3706 -- out some static checks.
3708 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (T) then
3710 -- In case of aggregates we must also take care of the correct
3711 -- initialization of nested aggregates bug this is done at the
3712 -- point of the analysis of the aggregate (see sem_aggr.adb) ???
3714 if Present (Expression (N))
3715 and then Nkind (Expression (N)) = N_Aggregate
3716 then
3717 null;
3719 else
3720 declare
3721 Save_Typ : constant Entity_Id := Etype (Id);
3722 begin
3723 Set_Etype (Id, T); -- Temp. decoration for static checks
3724 Null_Exclusion_Static_Checks (N);
3725 Set_Etype (Id, Save_Typ);
3726 end;
3727 end if;
3728 end if;
3730 -- Object is marked pure if it is in a pure scope
3732 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3734 -- If deferred constant, make sure context is appropriate. We detect
3735 -- a deferred constant as a constant declaration with no expression.
3736 -- A deferred constant can appear in a package body if its completion
3737 -- is by means of an interface pragma.
3739 if Constant_Present (N) and then No (E) then
3741 -- A deferred constant may appear in the declarative part of the
3742 -- following constructs:
3744 -- blocks
3745 -- entry bodies
3746 -- extended return statements
3747 -- package specs
3748 -- package bodies
3749 -- subprogram bodies
3750 -- task bodies
3752 -- When declared inside a package spec, a deferred constant must be
3753 -- completed by a full constant declaration or pragma Import. In all
3754 -- other cases, the only proper completion is pragma Import. Extended
3755 -- return statements are flagged as invalid contexts because they do
3756 -- not have a declarative part and so cannot accommodate the pragma.
3758 if Ekind (Current_Scope) = E_Return_Statement then
3759 Error_Msg_N
3760 ("invalid context for deferred constant declaration (RM 7.4)",
3762 Error_Msg_N
3763 ("\declaration requires an initialization expression",
3765 Set_Constant_Present (N, False);
3767 -- In Ada 83, deferred constant must be of private type
3769 elsif not Is_Private_Type (T) then
3770 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
3771 Error_Msg_N
3772 ("(Ada 83) deferred constant must be private type", N);
3773 end if;
3774 end if;
3776 -- If not a deferred constant, then the object declaration freezes
3777 -- its type, unless the object is of an anonymous type and has delayed
3778 -- aspects. In that case the type is frozen when the object itself is.
3780 else
3781 Check_Fully_Declared (T, N);
3783 if Has_Delayed_Aspects (Id)
3784 and then Is_Array_Type (T)
3785 and then Is_Itype (T)
3786 then
3787 Set_Has_Delayed_Freeze (T);
3788 else
3789 Freeze_Before (N, T);
3790 end if;
3791 end if;
3793 -- If the object was created by a constrained array definition, then
3794 -- set the link in both the anonymous base type and anonymous subtype
3795 -- that are built to represent the array type to point to the object.
3797 if Nkind (Object_Definition (Declaration_Node (Id))) =
3798 N_Constrained_Array_Definition
3799 then
3800 Set_Related_Array_Object (T, Id);
3801 Set_Related_Array_Object (Base_Type (T), Id);
3802 end if;
3804 -- Special checks for protected objects not at library level
3806 if Has_Protected (T) and then not Is_Library_Level_Entity (Id) then
3807 Check_Restriction (No_Local_Protected_Objects, Id);
3809 -- Protected objects with interrupt handlers must be at library level
3811 -- Ada 2005: This test is not needed (and the corresponding clause
3812 -- in the RM is removed) because accessibility checks are sufficient
3813 -- to make handlers not at the library level illegal.
3815 -- AI05-0303: The AI is in fact a binding interpretation, and thus
3816 -- applies to the '95 version of the language as well.
3818 if Is_Protected_Type (T)
3819 and then Has_Interrupt_Handler (T)
3820 and then Ada_Version < Ada_95
3821 then
3822 Error_Msg_N
3823 ("interrupt object can only be declared at library level", Id);
3824 end if;
3825 end if;
3827 -- Check for violation of No_Local_Timing_Events
3829 if Has_Timing_Event (T) and then not Is_Library_Level_Entity (Id) then
3830 Check_Restriction (No_Local_Timing_Events, Id);
3831 end if;
3833 -- The actual subtype of the object is the nominal subtype, unless
3834 -- the nominal one is unconstrained and obtained from the expression.
3836 Act_T := T;
3838 -- These checks should be performed before the initialization expression
3839 -- is considered, so that the Object_Definition node is still the same
3840 -- as in source code.
3842 -- In SPARK, the nominal subtype is always given by a subtype mark
3843 -- and must not be unconstrained. (The only exception to this is the
3844 -- acceptance of declarations of constants of type String.)
3846 if not Nkind_In (Object_Definition (N), N_Expanded_Name, N_Identifier)
3847 then
3848 Check_SPARK_05_Restriction
3849 ("subtype mark required", Object_Definition (N));
3851 elsif Is_Array_Type (T)
3852 and then not Is_Constrained (T)
3853 and then T /= Standard_String
3854 then
3855 Check_SPARK_05_Restriction
3856 ("subtype mark of constrained type expected",
3857 Object_Definition (N));
3858 end if;
3860 -- There are no aliased objects in SPARK
3862 if Aliased_Present (N) then
3863 Check_SPARK_05_Restriction ("aliased object is not allowed", N);
3864 end if;
3866 -- Process initialization expression if present and not in error
3868 if Present (E) and then E /= Error then
3870 -- Generate an error in case of CPP class-wide object initialization.
3871 -- Required because otherwise the expansion of the class-wide
3872 -- assignment would try to use 'size to initialize the object
3873 -- (primitive that is not available in CPP tagged types).
3875 if Is_Class_Wide_Type (Act_T)
3876 and then
3877 (Is_CPP_Class (Root_Type (Etype (Act_T)))
3878 or else
3879 (Present (Full_View (Root_Type (Etype (Act_T))))
3880 and then
3881 Is_CPP_Class (Full_View (Root_Type (Etype (Act_T))))))
3882 then
3883 Error_Msg_N
3884 ("predefined assignment not available for 'C'P'P tagged types",
3886 end if;
3888 Mark_Coextensions (N, E);
3889 Analyze (E);
3891 -- In case of errors detected in the analysis of the expression,
3892 -- decorate it with the expected type to avoid cascaded errors
3894 if No (Etype (E)) then
3895 Set_Etype (E, T);
3896 end if;
3898 -- If an initialization expression is present, then we set the
3899 -- Is_True_Constant flag. It will be reset if this is a variable
3900 -- and it is indeed modified.
3902 Set_Is_True_Constant (Id, True);
3904 -- If we are analyzing a constant declaration, set its completion
3905 -- flag after analyzing and resolving the expression.
3907 if Constant_Present (N) then
3908 Set_Has_Completion (Id);
3909 end if;
3911 -- Set type and resolve (type may be overridden later on). Note:
3912 -- Ekind (Id) must still be E_Void at this point so that incorrect
3913 -- early usage within E is properly diagnosed.
3915 Set_Etype (Id, T);
3917 -- If the expression is an aggregate we must look ahead to detect
3918 -- the possible presence of an address clause, and defer resolution
3919 -- and expansion of the aggregate to the freeze point of the entity.
3921 -- This is not always legal because the aggregate may contain other
3922 -- references that need freezing, e.g. references to other entities
3923 -- with address clauses. In any case, when compiling with -gnatI the
3924 -- presence of the address clause must be ignored.
3926 if Comes_From_Source (N)
3927 and then Expander_Active
3928 and then Nkind (E) = N_Aggregate
3929 and then
3930 ((Present (Following_Address_Clause (N))
3931 and then not Ignore_Rep_Clauses)
3932 or else Delayed_Aspect_Present)
3933 then
3934 Set_Etype (E, T);
3936 else
3937 Resolve (E, T);
3938 end if;
3940 -- No further action needed if E is a call to an inlined function
3941 -- which returns an unconstrained type and it has been expanded into
3942 -- a procedure call. In that case N has been replaced by an object
3943 -- declaration without initializing expression and it has been
3944 -- analyzed (see Expand_Inlined_Call).
3946 if Back_End_Inlining
3947 and then Expander_Active
3948 and then Nkind (E) = N_Function_Call
3949 and then Nkind (Name (E)) in N_Has_Entity
3950 and then Is_Inlined (Entity (Name (E)))
3951 and then not Is_Constrained (Etype (E))
3952 and then Analyzed (N)
3953 and then No (Expression (N))
3954 then
3955 goto Leave;
3956 end if;
3958 -- If E is null and has been replaced by an N_Raise_Constraint_Error
3959 -- node (which was marked already-analyzed), we need to set the type
3960 -- to something other than Any_Access in order to keep gigi happy.
3962 if Etype (E) = Any_Access then
3963 Set_Etype (E, T);
3964 end if;
3966 -- If the object is an access to variable, the initialization
3967 -- expression cannot be an access to constant.
3969 if Is_Access_Type (T)
3970 and then not Is_Access_Constant (T)
3971 and then Is_Access_Type (Etype (E))
3972 and then Is_Access_Constant (Etype (E))
3973 then
3974 Error_Msg_N
3975 ("access to variable cannot be initialized with an "
3976 & "access-to-constant expression", E);
3977 end if;
3979 if not Assignment_OK (N) then
3980 Check_Initialization (T, E);
3981 end if;
3983 Check_Unset_Reference (E);
3985 -- If this is a variable, then set current value. If this is a
3986 -- declared constant of a scalar type with a static expression,
3987 -- indicate that it is always valid.
3989 if not Constant_Present (N) then
3990 if Compile_Time_Known_Value (E) then
3991 Set_Current_Value (Id, E);
3992 end if;
3994 elsif Is_Scalar_Type (T) and then Is_OK_Static_Expression (E) then
3995 Set_Is_Known_Valid (Id);
3996 end if;
3998 -- Deal with setting of null flags
4000 if Is_Access_Type (T) then
4001 if Known_Non_Null (E) then
4002 Set_Is_Known_Non_Null (Id, True);
4003 elsif Known_Null (E) and then not Can_Never_Be_Null (Id) then
4004 Set_Is_Known_Null (Id, True);
4005 end if;
4006 end if;
4008 -- Check incorrect use of dynamically tagged expressions
4010 if Is_Tagged_Type (T) then
4011 Check_Dynamically_Tagged_Expression
4012 (Expr => E,
4013 Typ => T,
4014 Related_Nod => N);
4015 end if;
4017 Apply_Scalar_Range_Check (E, T);
4018 Apply_Static_Length_Check (E, T);
4020 if Nkind (Original_Node (N)) = N_Object_Declaration
4021 and then Comes_From_Source (Original_Node (N))
4023 -- Only call test if needed
4025 and then Restriction_Check_Required (SPARK_05)
4026 and then not Is_SPARK_05_Initialization_Expr (Original_Node (E))
4027 then
4028 Check_SPARK_05_Restriction
4029 ("initialization expression is not appropriate", E);
4030 end if;
4032 -- A formal parameter of a specific tagged type whose related
4033 -- subprogram is subject to pragma Extensions_Visible with value
4034 -- "False" cannot be implicitly converted to a class-wide type by
4035 -- means of an initialization expression (SPARK RM 6.1.7(3)). Do
4036 -- not consider internally generated expressions.
4038 if Is_Class_Wide_Type (T)
4039 and then Comes_From_Source (E)
4040 and then Is_EVF_Expression (E)
4041 then
4042 Error_Msg_N
4043 ("formal parameter cannot be implicitly converted to "
4044 & "class-wide type when Extensions_Visible is False", E);
4045 end if;
4046 end if;
4048 -- If the No_Streams restriction is set, check that the type of the
4049 -- object is not, and does not contain, any subtype derived from
4050 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
4051 -- Has_Stream just for efficiency reasons. There is no point in
4052 -- spending time on a Has_Stream check if the restriction is not set.
4054 if Restriction_Check_Required (No_Streams) then
4055 if Has_Stream (T) then
4056 Check_Restriction (No_Streams, N);
4057 end if;
4058 end if;
4060 -- Deal with predicate check before we start to do major rewriting. It
4061 -- is OK to initialize and then check the initialized value, since the
4062 -- object goes out of scope if we get a predicate failure. Note that we
4063 -- do this in the analyzer and not the expander because the analyzer
4064 -- does some substantial rewriting in some cases.
4066 -- We need a predicate check if the type has predicates that are not
4067 -- ignored, and if either there is an initializing expression, or for
4068 -- default initialization when we have at least one case of an explicit
4069 -- default initial value and then this is not an internal declaration
4070 -- whose initialization comes later (as for an aggregate expansion).
4072 if not Suppress_Assignment_Checks (N)
4073 and then Present (Predicate_Function (T))
4074 and then not Predicates_Ignored (T)
4075 and then not No_Initialization (N)
4076 and then
4077 (Present (E)
4078 or else
4079 Is_Partially_Initialized_Type (T, Include_Implicit => False))
4080 then
4081 -- If the type has a static predicate and the expression is known at
4082 -- compile time, see if the expression satisfies the predicate.
4084 if Present (E) then
4085 Check_Expression_Against_Static_Predicate (E, T);
4086 end if;
4088 -- If the type is a null record and there is no explicit initial
4089 -- expression, no predicate check applies.
4091 if No (E) and then Is_Null_Record_Type (T) then
4092 null;
4094 else
4095 Insert_After (N,
4096 Make_Predicate_Check (T, New_Occurrence_Of (Id, Loc)));
4097 end if;
4098 end if;
4100 -- Case of unconstrained type
4102 if not Is_Definite_Subtype (T) then
4104 -- In SPARK, a declaration of unconstrained type is allowed
4105 -- only for constants of type string.
4107 if Is_String_Type (T) and then not Constant_Present (N) then
4108 Check_SPARK_05_Restriction
4109 ("declaration of object of unconstrained type not allowed", N);
4110 end if;
4112 -- Nothing to do in deferred constant case
4114 if Constant_Present (N) and then No (E) then
4115 null;
4117 -- Case of no initialization present
4119 elsif No (E) then
4120 if No_Initialization (N) then
4121 null;
4123 elsif Is_Class_Wide_Type (T) then
4124 Error_Msg_N
4125 ("initialization required in class-wide declaration ", N);
4127 else
4128 Error_Msg_N
4129 ("unconstrained subtype not allowed (need initialization)",
4130 Object_Definition (N));
4132 if Is_Record_Type (T) and then Has_Discriminants (T) then
4133 Error_Msg_N
4134 ("\provide initial value or explicit discriminant values",
4135 Object_Definition (N));
4137 Error_Msg_NE
4138 ("\or give default discriminant values for type&",
4139 Object_Definition (N), T);
4141 elsif Is_Array_Type (T) then
4142 Error_Msg_N
4143 ("\provide initial value or explicit array bounds",
4144 Object_Definition (N));
4145 end if;
4146 end if;
4148 -- Case of initialization present but in error. Set initial
4149 -- expression as absent (but do not make above complaints)
4151 elsif E = Error then
4152 Set_Expression (N, Empty);
4153 E := Empty;
4155 -- Case of initialization present
4157 else
4158 -- Check restrictions in Ada 83
4160 if not Constant_Present (N) then
4162 -- Unconstrained variables not allowed in Ada 83 mode
4164 if Ada_Version = Ada_83
4165 and then Comes_From_Source (Object_Definition (N))
4166 then
4167 Error_Msg_N
4168 ("(Ada 83) unconstrained variable not allowed",
4169 Object_Definition (N));
4170 end if;
4171 end if;
4173 -- Now we constrain the variable from the initializing expression
4175 -- If the expression is an aggregate, it has been expanded into
4176 -- individual assignments. Retrieve the actual type from the
4177 -- expanded construct.
4179 if Is_Array_Type (T)
4180 and then No_Initialization (N)
4181 and then Nkind (Original_Node (E)) = N_Aggregate
4182 then
4183 Act_T := Etype (E);
4185 -- In case of class-wide interface object declarations we delay
4186 -- the generation of the equivalent record type declarations until
4187 -- its expansion because there are cases in they are not required.
4189 elsif Is_Interface (T) then
4190 null;
4192 -- In GNATprove mode, Expand_Subtype_From_Expr does nothing. Thus,
4193 -- we should prevent the generation of another Itype with the
4194 -- same name as the one already generated, or we end up with
4195 -- two identical types in GNATprove.
4197 elsif GNATprove_Mode then
4198 null;
4200 -- If the type is an unchecked union, no subtype can be built from
4201 -- the expression. Rewrite declaration as a renaming, which the
4202 -- back-end can handle properly. This is a rather unusual case,
4203 -- because most unchecked_union declarations have default values
4204 -- for discriminants and are thus not indefinite.
4206 elsif Is_Unchecked_Union (T) then
4207 if Constant_Present (N) or else Nkind (E) = N_Function_Call then
4208 Set_Ekind (Id, E_Constant);
4209 else
4210 Set_Ekind (Id, E_Variable);
4211 end if;
4213 Rewrite (N,
4214 Make_Object_Renaming_Declaration (Loc,
4215 Defining_Identifier => Id,
4216 Subtype_Mark => New_Occurrence_Of (T, Loc),
4217 Name => E));
4219 Set_Renamed_Object (Id, E);
4220 Freeze_Before (N, T);
4221 Set_Is_Frozen (Id);
4222 goto Leave;
4224 else
4225 -- Ensure that the generated subtype has a unique external name
4226 -- when the related object is public. This guarantees that the
4227 -- subtype and its bounds will not be affected by switches or
4228 -- pragmas that may offset the internal counter due to extra
4229 -- generated code.
4231 if Is_Public (Id) then
4232 Related_Id := Id;
4233 else
4234 Related_Id := Empty;
4235 end if;
4237 Expand_Subtype_From_Expr
4238 (N => N,
4239 Unc_Type => T,
4240 Subtype_Indic => Object_Definition (N),
4241 Exp => E,
4242 Related_Id => Related_Id);
4244 Act_T := Find_Type_Of_Object (Object_Definition (N), N);
4245 end if;
4247 Set_Is_Constr_Subt_For_U_Nominal (Act_T);
4249 if Aliased_Present (N) then
4250 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4251 end if;
4253 Freeze_Before (N, Act_T);
4254 Freeze_Before (N, T);
4255 end if;
4257 elsif Is_Array_Type (T)
4258 and then No_Initialization (N)
4259 and then (Nkind (Original_Node (E)) = N_Aggregate
4260 or else (Nkind (Original_Node (E)) = N_Qualified_Expression
4261 and then Nkind (Original_Node (Expression
4262 (Original_Node (E)))) = N_Aggregate))
4263 then
4264 if not Is_Entity_Name (Object_Definition (N)) then
4265 Act_T := Etype (E);
4266 Check_Compile_Time_Size (Act_T);
4268 if Aliased_Present (N) then
4269 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4270 end if;
4271 end if;
4273 -- When the given object definition and the aggregate are specified
4274 -- independently, and their lengths might differ do a length check.
4275 -- This cannot happen if the aggregate is of the form (others =>...)
4277 if not Is_Constrained (T) then
4278 null;
4280 elsif Nkind (E) = N_Raise_Constraint_Error then
4282 -- Aggregate is statically illegal. Place back in declaration
4284 Set_Expression (N, E);
4285 Set_No_Initialization (N, False);
4287 elsif T = Etype (E) then
4288 null;
4290 elsif Nkind (E) = N_Aggregate
4291 and then Present (Component_Associations (E))
4292 and then Present (Choice_List (First (Component_Associations (E))))
4293 and then
4294 Nkind (First (Choice_List (First (Component_Associations (E))))) =
4295 N_Others_Choice
4296 then
4297 null;
4299 else
4300 Apply_Length_Check (E, T);
4301 end if;
4303 -- If the type is limited unconstrained with defaulted discriminants and
4304 -- there is no expression, then the object is constrained by the
4305 -- defaults, so it is worthwhile building the corresponding subtype.
4307 elsif (Is_Limited_Record (T) or else Is_Concurrent_Type (T))
4308 and then not Is_Constrained (T)
4309 and then Has_Discriminants (T)
4310 then
4311 if No (E) then
4312 Act_T := Build_Default_Subtype (T, N);
4313 else
4314 -- Ada 2005: A limited object may be initialized by means of an
4315 -- aggregate. If the type has default discriminants it has an
4316 -- unconstrained nominal type, Its actual subtype will be obtained
4317 -- from the aggregate, and not from the default discriminants.
4319 Act_T := Etype (E);
4320 end if;
4322 Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
4324 elsif Nkind (E) = N_Function_Call
4325 and then Constant_Present (N)
4326 and then Has_Unconstrained_Elements (Etype (E))
4327 then
4328 -- The back-end has problems with constants of a discriminated type
4329 -- with defaults, if the initial value is a function call. We
4330 -- generate an intermediate temporary that will receive a reference
4331 -- to the result of the call. The initialization expression then
4332 -- becomes a dereference of that temporary.
4334 Remove_Side_Effects (E);
4336 -- If this is a constant declaration of an unconstrained type and
4337 -- the initialization is an aggregate, we can use the subtype of the
4338 -- aggregate for the declared entity because it is immutable.
4340 elsif not Is_Constrained (T)
4341 and then Has_Discriminants (T)
4342 and then Constant_Present (N)
4343 and then not Has_Unchecked_Union (T)
4344 and then Nkind (E) = N_Aggregate
4345 then
4346 Act_T := Etype (E);
4347 end if;
4349 -- Check No_Wide_Characters restriction
4351 Check_Wide_Character_Restriction (T, Object_Definition (N));
4353 -- Indicate this is not set in source. Certainly true for constants, and
4354 -- true for variables so far (will be reset for a variable if and when
4355 -- we encounter a modification in the source).
4357 Set_Never_Set_In_Source (Id);
4359 -- Now establish the proper kind and type of the object
4361 if Constant_Present (N) then
4362 Set_Ekind (Id, E_Constant);
4363 Set_Is_True_Constant (Id);
4365 else
4366 Set_Ekind (Id, E_Variable);
4368 -- A variable is set as shared passive if it appears in a shared
4369 -- passive package, and is at the outer level. This is not done for
4370 -- entities generated during expansion, because those are always
4371 -- manipulated locally.
4373 if Is_Shared_Passive (Current_Scope)
4374 and then Is_Library_Level_Entity (Id)
4375 and then Comes_From_Source (Id)
4376 then
4377 Set_Is_Shared_Passive (Id);
4378 Check_Shared_Var (Id, T, N);
4379 end if;
4381 -- Set Has_Initial_Value if initializing expression present. Note
4382 -- that if there is no initializing expression, we leave the state
4383 -- of this flag unchanged (usually it will be False, but notably in
4384 -- the case of exception choice variables, it will already be true).
4386 if Present (E) then
4387 Set_Has_Initial_Value (Id);
4388 end if;
4389 end if;
4391 -- Initialize alignment and size and capture alignment setting
4393 Init_Alignment (Id);
4394 Init_Esize (Id);
4395 Set_Optimize_Alignment_Flags (Id);
4397 -- Deal with aliased case
4399 if Aliased_Present (N) then
4400 Set_Is_Aliased (Id);
4402 -- If the object is aliased and the type is unconstrained with
4403 -- defaulted discriminants and there is no expression, then the
4404 -- object is constrained by the defaults, so it is worthwhile
4405 -- building the corresponding subtype.
4407 -- Ada 2005 (AI-363): If the aliased object is discriminated and
4408 -- unconstrained, then only establish an actual subtype if the
4409 -- nominal subtype is indefinite. In definite cases the object is
4410 -- unconstrained in Ada 2005.
4412 if No (E)
4413 and then Is_Record_Type (T)
4414 and then not Is_Constrained (T)
4415 and then Has_Discriminants (T)
4416 and then (Ada_Version < Ada_2005
4417 or else not Is_Definite_Subtype (T))
4418 then
4419 Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
4420 end if;
4421 end if;
4423 -- Now we can set the type of the object
4425 Set_Etype (Id, Act_T);
4427 -- Non-constant object is marked to be treated as volatile if type is
4428 -- volatile and we clear the Current_Value setting that may have been
4429 -- set above. Doing so for constants isn't required and might interfere
4430 -- with possible uses of the object as a static expression in contexts
4431 -- incompatible with volatility (e.g. as a case-statement alternative).
4433 if Ekind (Id) /= E_Constant and then Treat_As_Volatile (Etype (Id)) then
4434 Set_Treat_As_Volatile (Id);
4435 Set_Current_Value (Id, Empty);
4436 end if;
4438 -- Deal with controlled types
4440 if Has_Controlled_Component (Etype (Id))
4441 or else Is_Controlled (Etype (Id))
4442 then
4443 if not Is_Library_Level_Entity (Id) then
4444 Check_Restriction (No_Nested_Finalization, N);
4445 else
4446 Validate_Controlled_Object (Id);
4447 end if;
4448 end if;
4450 if Has_Task (Etype (Id)) then
4451 Check_Restriction (No_Tasking, N);
4453 -- Deal with counting max tasks
4455 -- Nothing to do if inside a generic
4457 if Inside_A_Generic then
4458 null;
4460 -- If library level entity, then count tasks
4462 elsif Is_Library_Level_Entity (Id) then
4463 Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
4465 -- If not library level entity, then indicate we don't know max
4466 -- tasks and also check task hierarchy restriction and blocking
4467 -- operation (since starting a task is definitely blocking).
4469 else
4470 Check_Restriction (Max_Tasks, N);
4471 Check_Restriction (No_Task_Hierarchy, N);
4472 Check_Potentially_Blocking_Operation (N);
4473 end if;
4475 -- A rather specialized test. If we see two tasks being declared
4476 -- of the same type in the same object declaration, and the task
4477 -- has an entry with an address clause, we know that program error
4478 -- will be raised at run time since we can't have two tasks with
4479 -- entries at the same address.
4481 if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
4482 declare
4483 E : Entity_Id;
4485 begin
4486 E := First_Entity (Etype (Id));
4487 while Present (E) loop
4488 if Ekind (E) = E_Entry
4489 and then Present (Get_Attribute_Definition_Clause
4490 (E, Attribute_Address))
4491 then
4492 Error_Msg_Warn := SPARK_Mode /= On;
4493 Error_Msg_N
4494 ("more than one task with same entry address<<", N);
4495 Error_Msg_N ("\Program_Error [<<", N);
4496 Insert_Action (N,
4497 Make_Raise_Program_Error (Loc,
4498 Reason => PE_Duplicated_Entry_Address));
4499 exit;
4500 end if;
4502 Next_Entity (E);
4503 end loop;
4504 end;
4505 end if;
4506 end if;
4508 -- Some simple constant-propagation: if the expression is a constant
4509 -- string initialized with a literal, share the literal. This avoids
4510 -- a run-time copy.
4512 if Present (E)
4513 and then Is_Entity_Name (E)
4514 and then Ekind (Entity (E)) = E_Constant
4515 and then Base_Type (Etype (E)) = Standard_String
4516 then
4517 declare
4518 Val : constant Node_Id := Constant_Value (Entity (E));
4519 begin
4520 if Present (Val) and then Nkind (Val) = N_String_Literal then
4521 Rewrite (E, New_Copy (Val));
4522 end if;
4523 end;
4524 end if;
4526 -- Another optimization: if the nominal subtype is unconstrained and
4527 -- the expression is a function call that returns an unconstrained
4528 -- type, rewrite the declaration as a renaming of the result of the
4529 -- call. The exceptions below are cases where the copy is expected,
4530 -- either by the back end (Aliased case) or by the semantics, as for
4531 -- initializing controlled types or copying tags for class-wide types.
4533 if Present (E)
4534 and then Nkind (E) = N_Explicit_Dereference
4535 and then Nkind (Original_Node (E)) = N_Function_Call
4536 and then not Is_Library_Level_Entity (Id)
4537 and then not Is_Constrained (Underlying_Type (T))
4538 and then not Is_Aliased (Id)
4539 and then not Is_Class_Wide_Type (T)
4540 and then not Is_Controlled_Active (T)
4541 and then not Has_Controlled_Component (Base_Type (T))
4542 and then Expander_Active
4543 then
4544 Rewrite (N,
4545 Make_Object_Renaming_Declaration (Loc,
4546 Defining_Identifier => Id,
4547 Access_Definition => Empty,
4548 Subtype_Mark => New_Occurrence_Of
4549 (Base_Type (Etype (Id)), Loc),
4550 Name => E));
4552 Set_Renamed_Object (Id, E);
4554 -- Force generation of debugging information for the constant and for
4555 -- the renamed function call.
4557 Set_Debug_Info_Needed (Id);
4558 Set_Debug_Info_Needed (Entity (Prefix (E)));
4559 end if;
4561 if Present (Prev_Entity)
4562 and then Is_Frozen (Prev_Entity)
4563 and then not Error_Posted (Id)
4564 then
4565 Error_Msg_N ("full constant declaration appears too late", N);
4566 end if;
4568 Check_Eliminated (Id);
4570 -- Deal with setting In_Private_Part flag if in private part
4572 if Ekind (Scope (Id)) = E_Package
4573 and then In_Private_Part (Scope (Id))
4574 then
4575 Set_In_Private_Part (Id);
4576 end if;
4578 <<Leave>>
4579 -- Initialize the refined state of a variable here because this is a
4580 -- common destination for legal and illegal object declarations.
4582 if Ekind (Id) = E_Variable then
4583 Set_Encapsulating_State (Id, Empty);
4584 end if;
4586 if Has_Aspects (N) then
4587 Analyze_Aspect_Specifications (N, Id);
4588 end if;
4590 Analyze_Dimension (N);
4592 -- Verify whether the object declaration introduces an illegal hidden
4593 -- state within a package subject to a null abstract state.
4595 if Ekind (Id) = E_Variable then
4596 Check_No_Hidden_State (Id);
4597 end if;
4599 if Mode_Set then
4600 Restore_Ghost_Mode (Mode);
4601 end if;
4602 end Analyze_Object_Declaration;
4604 ---------------------------
4605 -- Analyze_Others_Choice --
4606 ---------------------------
4608 -- Nothing to do for the others choice node itself, the semantic analysis
4609 -- of the others choice will occur as part of the processing of the parent
4611 procedure Analyze_Others_Choice (N : Node_Id) is
4612 pragma Warnings (Off, N);
4613 begin
4614 null;
4615 end Analyze_Others_Choice;
4617 -------------------------------------------
4618 -- Analyze_Private_Extension_Declaration --
4619 -------------------------------------------
4621 procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
4622 Indic : constant Node_Id := Subtype_Indication (N);
4623 T : constant Entity_Id := Defining_Identifier (N);
4624 Iface : Entity_Id;
4625 Iface_Elmt : Elmt_Id;
4626 Parent_Base : Entity_Id;
4627 Parent_Type : Entity_Id;
4629 begin
4630 -- Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
4632 if Is_Non_Empty_List (Interface_List (N)) then
4633 declare
4634 Intf : Node_Id;
4635 T : Entity_Id;
4637 begin
4638 Intf := First (Interface_List (N));
4639 while Present (Intf) loop
4640 T := Find_Type_Of_Subtype_Indic (Intf);
4642 Diagnose_Interface (Intf, T);
4643 Next (Intf);
4644 end loop;
4645 end;
4646 end if;
4648 Generate_Definition (T);
4650 -- For other than Ada 2012, just enter the name in the current scope
4652 if Ada_Version < Ada_2012 then
4653 Enter_Name (T);
4655 -- Ada 2012 (AI05-0162): Enter the name in the current scope handling
4656 -- case of private type that completes an incomplete type.
4658 else
4659 declare
4660 Prev : Entity_Id;
4662 begin
4663 Prev := Find_Type_Name (N);
4665 pragma Assert (Prev = T
4666 or else (Ekind (Prev) = E_Incomplete_Type
4667 and then Present (Full_View (Prev))
4668 and then Full_View (Prev) = T));
4669 end;
4670 end if;
4672 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
4673 Parent_Base := Base_Type (Parent_Type);
4675 if Parent_Type = Any_Type or else Etype (Parent_Type) = Any_Type then
4676 Set_Ekind (T, Ekind (Parent_Type));
4677 Set_Etype (T, Any_Type);
4678 goto Leave;
4680 elsif not Is_Tagged_Type (Parent_Type) then
4681 Error_Msg_N
4682 ("parent of type extension must be a tagged type ", Indic);
4683 goto Leave;
4685 elsif Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
4686 Error_Msg_N ("premature derivation of incomplete type", Indic);
4687 goto Leave;
4689 elsif Is_Concurrent_Type (Parent_Type) then
4690 Error_Msg_N
4691 ("parent type of a private extension cannot be a synchronized "
4692 & "tagged type (RM 3.9.1 (3/1))", N);
4694 Set_Etype (T, Any_Type);
4695 Set_Ekind (T, E_Limited_Private_Type);
4696 Set_Private_Dependents (T, New_Elmt_List);
4697 Set_Error_Posted (T);
4698 goto Leave;
4699 end if;
4701 -- Perhaps the parent type should be changed to the class-wide type's
4702 -- specific type in this case to prevent cascading errors ???
4704 if Is_Class_Wide_Type (Parent_Type) then
4705 Error_Msg_N
4706 ("parent of type extension must not be a class-wide type", Indic);
4707 goto Leave;
4708 end if;
4710 if (not Is_Package_Or_Generic_Package (Current_Scope)
4711 and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
4712 or else In_Private_Part (Current_Scope)
4713 then
4714 Error_Msg_N ("invalid context for private extension", N);
4715 end if;
4717 -- Set common attributes
4719 Set_Is_Pure (T, Is_Pure (Current_Scope));
4720 Set_Scope (T, Current_Scope);
4721 Set_Ekind (T, E_Record_Type_With_Private);
4722 Init_Size_Align (T);
4723 Set_Default_SSO (T);
4725 Set_Etype (T, Parent_Base);
4726 Propagate_Concurrent_Flags (T, Parent_Base);
4728 Set_Convention (T, Convention (Parent_Type));
4729 Set_First_Rep_Item (T, First_Rep_Item (Parent_Type));
4730 Set_Is_First_Subtype (T);
4731 Make_Class_Wide_Type (T);
4733 if Unknown_Discriminants_Present (N) then
4734 Set_Discriminant_Constraint (T, No_Elist);
4735 end if;
4737 Build_Derived_Record_Type (N, Parent_Type, T);
4739 -- A private extension inherits the Default_Initial_Condition pragma
4740 -- coming from any parent type within the derivation chain.
4742 if Has_DIC (Parent_Type) then
4743 Set_Has_Inherited_DIC (T);
4744 end if;
4746 -- A private extension inherits any class-wide invariants coming from a
4747 -- parent type or an interface. Note that the invariant procedure of the
4748 -- parent type should not be inherited because the private extension may
4749 -- define invariants of its own.
4751 if Has_Inherited_Invariants (Parent_Type)
4752 or else Has_Inheritable_Invariants (Parent_Type)
4753 then
4754 Set_Has_Inherited_Invariants (T);
4756 elsif Present (Interfaces (T)) then
4757 Iface_Elmt := First_Elmt (Interfaces (T));
4758 while Present (Iface_Elmt) loop
4759 Iface := Node (Iface_Elmt);
4761 if Has_Inheritable_Invariants (Iface) then
4762 Set_Has_Inherited_Invariants (T);
4763 exit;
4764 end if;
4766 Next_Elmt (Iface_Elmt);
4767 end loop;
4768 end if;
4770 -- Ada 2005 (AI-443): Synchronized private extension or a rewritten
4771 -- synchronized formal derived type.
4773 if Ada_Version >= Ada_2005 and then Synchronized_Present (N) then
4774 Set_Is_Limited_Record (T);
4776 -- Formal derived type case
4778 if Is_Generic_Type (T) then
4780 -- The parent must be a tagged limited type or a synchronized
4781 -- interface.
4783 if (not Is_Tagged_Type (Parent_Type)
4784 or else not Is_Limited_Type (Parent_Type))
4785 and then
4786 (not Is_Interface (Parent_Type)
4787 or else not Is_Synchronized_Interface (Parent_Type))
4788 then
4789 Error_Msg_NE
4790 ("parent type of & must be tagged limited or synchronized",
4791 N, T);
4792 end if;
4794 -- The progenitors (if any) must be limited or synchronized
4795 -- interfaces.
4797 if Present (Interfaces (T)) then
4798 Iface_Elmt := First_Elmt (Interfaces (T));
4799 while Present (Iface_Elmt) loop
4800 Iface := Node (Iface_Elmt);
4802 if not Is_Limited_Interface (Iface)
4803 and then not Is_Synchronized_Interface (Iface)
4804 then
4805 Error_Msg_NE
4806 ("progenitor & must be limited or synchronized",
4807 N, Iface);
4808 end if;
4810 Next_Elmt (Iface_Elmt);
4811 end loop;
4812 end if;
4814 -- Regular derived extension, the parent must be a limited or
4815 -- synchronized interface.
4817 else
4818 if not Is_Interface (Parent_Type)
4819 or else (not Is_Limited_Interface (Parent_Type)
4820 and then not Is_Synchronized_Interface (Parent_Type))
4821 then
4822 Error_Msg_NE
4823 ("parent type of & must be limited interface", N, T);
4824 end if;
4825 end if;
4827 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
4828 -- extension with a synchronized parent must be explicitly declared
4829 -- synchronized, because the full view will be a synchronized type.
4830 -- This must be checked before the check for limited types below,
4831 -- to ensure that types declared limited are not allowed to extend
4832 -- synchronized interfaces.
4834 elsif Is_Interface (Parent_Type)
4835 and then Is_Synchronized_Interface (Parent_Type)
4836 and then not Synchronized_Present (N)
4837 then
4838 Error_Msg_NE
4839 ("private extension of& must be explicitly synchronized",
4840 N, Parent_Type);
4842 elsif Limited_Present (N) then
4843 Set_Is_Limited_Record (T);
4845 if not Is_Limited_Type (Parent_Type)
4846 and then
4847 (not Is_Interface (Parent_Type)
4848 or else not Is_Limited_Interface (Parent_Type))
4849 then
4850 Error_Msg_NE ("parent type& of limited extension must be limited",
4851 N, Parent_Type);
4852 end if;
4853 end if;
4855 <<Leave>>
4856 if Has_Aspects (N) then
4857 Analyze_Aspect_Specifications (N, T);
4858 end if;
4859 end Analyze_Private_Extension_Declaration;
4861 ---------------------------------
4862 -- Analyze_Subtype_Declaration --
4863 ---------------------------------
4865 procedure Analyze_Subtype_Declaration
4866 (N : Node_Id;
4867 Skip : Boolean := False)
4869 Id : constant Entity_Id := Defining_Identifier (N);
4870 R_Checks : Check_Result;
4871 T : Entity_Id;
4873 begin
4874 Generate_Definition (Id);
4875 Set_Is_Pure (Id, Is_Pure (Current_Scope));
4876 Init_Size_Align (Id);
4878 -- The following guard condition on Enter_Name is to handle cases where
4879 -- the defining identifier has already been entered into the scope but
4880 -- the declaration as a whole needs to be analyzed.
4882 -- This case in particular happens for derived enumeration types. The
4883 -- derived enumeration type is processed as an inserted enumeration type
4884 -- declaration followed by a rewritten subtype declaration. The defining
4885 -- identifier, however, is entered into the name scope very early in the
4886 -- processing of the original type declaration and therefore needs to be
4887 -- avoided here, when the created subtype declaration is analyzed. (See
4888 -- Build_Derived_Types)
4890 -- This also happens when the full view of a private type is derived
4891 -- type with constraints. In this case the entity has been introduced
4892 -- in the private declaration.
4894 -- Finally this happens in some complex cases when validity checks are
4895 -- enabled, where the same subtype declaration may be analyzed twice.
4896 -- This can happen if the subtype is created by the pre-analysis of
4897 -- an attribute tht gives the range of a loop statement, and the loop
4898 -- itself appears within an if_statement that will be rewritten during
4899 -- expansion.
4901 if Skip
4902 or else (Present (Etype (Id))
4903 and then (Is_Private_Type (Etype (Id))
4904 or else Is_Task_Type (Etype (Id))
4905 or else Is_Rewrite_Substitution (N)))
4906 then
4907 null;
4909 elsif Current_Entity (Id) = Id then
4910 null;
4912 else
4913 Enter_Name (Id);
4914 end if;
4916 T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
4918 -- Class-wide equivalent types of records with unknown discriminants
4919 -- involve the generation of an itype which serves as the private view
4920 -- of a constrained record subtype. In such cases the base type of the
4921 -- current subtype we are processing is the private itype. Use the full
4922 -- of the private itype when decorating various attributes.
4924 if Is_Itype (T)
4925 and then Is_Private_Type (T)
4926 and then Present (Full_View (T))
4927 then
4928 T := Full_View (T);
4929 end if;
4931 -- Inherit common attributes
4933 Set_Is_Volatile (Id, Is_Volatile (T));
4934 Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
4935 Set_Is_Generic_Type (Id, Is_Generic_Type (Base_Type (T)));
4936 Set_Convention (Id, Convention (T));
4938 -- If ancestor has predicates then so does the subtype, and in addition
4939 -- we must delay the freeze to properly arrange predicate inheritance.
4941 -- The Ancestor_Type test is really unpleasant, there seem to be cases
4942 -- in which T = ID, so the above tests and assignments do nothing???
4944 if Has_Predicates (T)
4945 or else (Present (Ancestor_Subtype (T))
4946 and then Has_Predicates (Ancestor_Subtype (T)))
4947 then
4948 Set_Has_Predicates (Id);
4949 Set_Has_Delayed_Freeze (Id);
4951 -- Generated subtypes inherit the predicate function from the parent
4952 -- (no aspects to examine on the generated declaration).
4954 if not Comes_From_Source (N) then
4955 Set_Ekind (Id, Ekind (T));
4957 if Present (Predicate_Function (T)) then
4958 Set_Predicate_Function (Id, Predicate_Function (T));
4960 elsif Present (Ancestor_Subtype (T))
4961 and then Has_Predicates (Ancestor_Subtype (T))
4962 and then Present (Predicate_Function (Ancestor_Subtype (T)))
4963 then
4964 Set_Predicate_Function (Id,
4965 Predicate_Function (Ancestor_Subtype (T)));
4966 end if;
4967 end if;
4968 end if;
4970 -- Subtype of Boolean cannot have a constraint in SPARK
4972 if Is_Boolean_Type (T)
4973 and then Nkind (Subtype_Indication (N)) = N_Subtype_Indication
4974 then
4975 Check_SPARK_05_Restriction
4976 ("subtype of Boolean cannot have constraint", N);
4977 end if;
4979 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
4980 declare
4981 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
4982 One_Cstr : Node_Id;
4983 Low : Node_Id;
4984 High : Node_Id;
4986 begin
4987 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint then
4988 One_Cstr := First (Constraints (Cstr));
4989 while Present (One_Cstr) loop
4991 -- Index or discriminant constraint in SPARK must be a
4992 -- subtype mark.
4994 if not
4995 Nkind_In (One_Cstr, N_Identifier, N_Expanded_Name)
4996 then
4997 Check_SPARK_05_Restriction
4998 ("subtype mark required", One_Cstr);
5000 -- String subtype must have a lower bound of 1 in SPARK.
5001 -- Note that we do not need to test for the non-static case
5002 -- here, since that was already taken care of in
5003 -- Process_Range_Expr_In_Decl.
5005 elsif Base_Type (T) = Standard_String then
5006 Get_Index_Bounds (One_Cstr, Low, High);
5008 if Is_OK_Static_Expression (Low)
5009 and then Expr_Value (Low) /= 1
5010 then
5011 Check_SPARK_05_Restriction
5012 ("String subtype must have lower bound of 1", N);
5013 end if;
5014 end if;
5016 Next (One_Cstr);
5017 end loop;
5018 end if;
5019 end;
5020 end if;
5022 -- In the case where there is no constraint given in the subtype
5023 -- indication, Process_Subtype just returns the Subtype_Mark, so its
5024 -- semantic attributes must be established here.
5026 if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
5027 Set_Etype (Id, Base_Type (T));
5029 -- Subtype of unconstrained array without constraint is not allowed
5030 -- in SPARK.
5032 if Is_Array_Type (T) and then not Is_Constrained (T) then
5033 Check_SPARK_05_Restriction
5034 ("subtype of unconstrained array must have constraint", N);
5035 end if;
5037 case Ekind (T) is
5038 when Array_Kind =>
5039 Set_Ekind (Id, E_Array_Subtype);
5040 Copy_Array_Subtype_Attributes (Id, T);
5042 when Decimal_Fixed_Point_Kind =>
5043 Set_Ekind (Id, E_Decimal_Fixed_Point_Subtype);
5044 Set_Digits_Value (Id, Digits_Value (T));
5045 Set_Delta_Value (Id, Delta_Value (T));
5046 Set_Scale_Value (Id, Scale_Value (T));
5047 Set_Small_Value (Id, Small_Value (T));
5048 Set_Scalar_Range (Id, Scalar_Range (T));
5049 Set_Machine_Radix_10 (Id, Machine_Radix_10 (T));
5050 Set_Is_Constrained (Id, Is_Constrained (T));
5051 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5052 Set_RM_Size (Id, RM_Size (T));
5054 when Enumeration_Kind =>
5055 Set_Ekind (Id, E_Enumeration_Subtype);
5056 Set_First_Literal (Id, First_Literal (Base_Type (T)));
5057 Set_Scalar_Range (Id, Scalar_Range (T));
5058 Set_Is_Character_Type (Id, Is_Character_Type (T));
5059 Set_Is_Constrained (Id, Is_Constrained (T));
5060 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5061 Set_RM_Size (Id, RM_Size (T));
5062 Inherit_Predicate_Flags (Id, T);
5064 when Ordinary_Fixed_Point_Kind =>
5065 Set_Ekind (Id, E_Ordinary_Fixed_Point_Subtype);
5066 Set_Scalar_Range (Id, Scalar_Range (T));
5067 Set_Small_Value (Id, Small_Value (T));
5068 Set_Delta_Value (Id, Delta_Value (T));
5069 Set_Is_Constrained (Id, Is_Constrained (T));
5070 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5071 Set_RM_Size (Id, RM_Size (T));
5073 when Float_Kind =>
5074 Set_Ekind (Id, E_Floating_Point_Subtype);
5075 Set_Scalar_Range (Id, Scalar_Range (T));
5076 Set_Digits_Value (Id, Digits_Value (T));
5077 Set_Is_Constrained (Id, Is_Constrained (T));
5079 -- If the floating point type has dimensions, these will be
5080 -- inherited subsequently when Analyze_Dimensions is called.
5082 when Signed_Integer_Kind =>
5083 Set_Ekind (Id, E_Signed_Integer_Subtype);
5084 Set_Scalar_Range (Id, Scalar_Range (T));
5085 Set_Is_Constrained (Id, Is_Constrained (T));
5086 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5087 Set_RM_Size (Id, RM_Size (T));
5088 Inherit_Predicate_Flags (Id, T);
5090 when Modular_Integer_Kind =>
5091 Set_Ekind (Id, E_Modular_Integer_Subtype);
5092 Set_Scalar_Range (Id, Scalar_Range (T));
5093 Set_Is_Constrained (Id, Is_Constrained (T));
5094 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5095 Set_RM_Size (Id, RM_Size (T));
5096 Inherit_Predicate_Flags (Id, T);
5098 when Class_Wide_Kind =>
5099 Set_Ekind (Id, E_Class_Wide_Subtype);
5100 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5101 Set_Cloned_Subtype (Id, T);
5102 Set_Is_Tagged_Type (Id, True);
5103 Set_Has_Unknown_Discriminants
5104 (Id, True);
5105 Set_No_Tagged_Streams_Pragma
5106 (Id, No_Tagged_Streams_Pragma (T));
5108 if Ekind (T) = E_Class_Wide_Subtype then
5109 Set_Equivalent_Type (Id, Equivalent_Type (T));
5110 end if;
5112 when E_Record_Subtype
5113 | E_Record_Type
5115 Set_Ekind (Id, E_Record_Subtype);
5117 if Ekind (T) = E_Record_Subtype
5118 and then Present (Cloned_Subtype (T))
5119 then
5120 Set_Cloned_Subtype (Id, Cloned_Subtype (T));
5121 else
5122 Set_Cloned_Subtype (Id, T);
5123 end if;
5125 Set_First_Entity (Id, First_Entity (T));
5126 Set_Last_Entity (Id, Last_Entity (T));
5127 Set_Has_Discriminants (Id, Has_Discriminants (T));
5128 Set_Is_Constrained (Id, Is_Constrained (T));
5129 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5130 Set_Has_Implicit_Dereference
5131 (Id, Has_Implicit_Dereference (T));
5132 Set_Has_Unknown_Discriminants
5133 (Id, Has_Unknown_Discriminants (T));
5135 if Has_Discriminants (T) then
5136 Set_Discriminant_Constraint
5137 (Id, Discriminant_Constraint (T));
5138 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5140 elsif Has_Unknown_Discriminants (Id) then
5141 Set_Discriminant_Constraint (Id, No_Elist);
5142 end if;
5144 if Is_Tagged_Type (T) then
5145 Set_Is_Tagged_Type (Id, True);
5146 Set_No_Tagged_Streams_Pragma
5147 (Id, No_Tagged_Streams_Pragma (T));
5148 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5149 Set_Direct_Primitive_Operations
5150 (Id, Direct_Primitive_Operations (T));
5151 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5153 if Is_Interface (T) then
5154 Set_Is_Interface (Id);
5155 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
5156 end if;
5157 end if;
5159 when Private_Kind =>
5160 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
5161 Set_Has_Discriminants (Id, Has_Discriminants (T));
5162 Set_Is_Constrained (Id, Is_Constrained (T));
5163 Set_First_Entity (Id, First_Entity (T));
5164 Set_Last_Entity (Id, Last_Entity (T));
5165 Set_Private_Dependents (Id, New_Elmt_List);
5166 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5167 Set_Has_Implicit_Dereference
5168 (Id, Has_Implicit_Dereference (T));
5169 Set_Has_Unknown_Discriminants
5170 (Id, Has_Unknown_Discriminants (T));
5171 Set_Known_To_Have_Preelab_Init
5172 (Id, Known_To_Have_Preelab_Init (T));
5174 if Is_Tagged_Type (T) then
5175 Set_Is_Tagged_Type (Id);
5176 Set_No_Tagged_Streams_Pragma (Id,
5177 No_Tagged_Streams_Pragma (T));
5178 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5179 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5180 Set_Direct_Primitive_Operations (Id,
5181 Direct_Primitive_Operations (T));
5182 end if;
5184 -- In general the attributes of the subtype of a private type
5185 -- are the attributes of the partial view of parent. However,
5186 -- the full view may be a discriminated type, and the subtype
5187 -- must share the discriminant constraint to generate correct
5188 -- calls to initialization procedures.
5190 if Has_Discriminants (T) then
5191 Set_Discriminant_Constraint
5192 (Id, Discriminant_Constraint (T));
5193 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5195 elsif Present (Full_View (T))
5196 and then Has_Discriminants (Full_View (T))
5197 then
5198 Set_Discriminant_Constraint
5199 (Id, Discriminant_Constraint (Full_View (T)));
5200 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5202 -- This would seem semantically correct, but apparently
5203 -- generates spurious errors about missing components ???
5205 -- Set_Has_Discriminants (Id);
5206 end if;
5208 Prepare_Private_Subtype_Completion (Id, N);
5210 -- If this is the subtype of a constrained private type with
5211 -- discriminants that has got a full view and we also have
5212 -- built a completion just above, show that the completion
5213 -- is a clone of the full view to the back-end.
5215 if Has_Discriminants (T)
5216 and then not Has_Unknown_Discriminants (T)
5217 and then not Is_Empty_Elmt_List (Discriminant_Constraint (T))
5218 and then Present (Full_View (T))
5219 and then Present (Full_View (Id))
5220 then
5221 Set_Cloned_Subtype (Full_View (Id), Full_View (T));
5222 end if;
5224 when Access_Kind =>
5225 Set_Ekind (Id, E_Access_Subtype);
5226 Set_Is_Constrained (Id, Is_Constrained (T));
5227 Set_Is_Access_Constant
5228 (Id, Is_Access_Constant (T));
5229 Set_Directly_Designated_Type
5230 (Id, Designated_Type (T));
5231 Set_Can_Never_Be_Null (Id, Can_Never_Be_Null (T));
5233 -- A Pure library_item must not contain the declaration of a
5234 -- named access type, except within a subprogram, generic
5235 -- subprogram, task unit, or protected unit, or if it has
5236 -- a specified Storage_Size of zero (RM05-10.2.1(15.4-15.5)).
5238 if Comes_From_Source (Id)
5239 and then In_Pure_Unit
5240 and then not In_Subprogram_Task_Protected_Unit
5241 and then not No_Pool_Assigned (Id)
5242 then
5243 Error_Msg_N
5244 ("named access types not allowed in pure unit", N);
5245 end if;
5247 when Concurrent_Kind =>
5248 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
5249 Set_Corresponding_Record_Type (Id,
5250 Corresponding_Record_Type (T));
5251 Set_First_Entity (Id, First_Entity (T));
5252 Set_First_Private_Entity (Id, First_Private_Entity (T));
5253 Set_Has_Discriminants (Id, Has_Discriminants (T));
5254 Set_Is_Constrained (Id, Is_Constrained (T));
5255 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5256 Set_Last_Entity (Id, Last_Entity (T));
5258 if Is_Tagged_Type (T) then
5259 Set_No_Tagged_Streams_Pragma
5260 (Id, No_Tagged_Streams_Pragma (T));
5261 end if;
5263 if Has_Discriminants (T) then
5264 Set_Discriminant_Constraint
5265 (Id, Discriminant_Constraint (T));
5266 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5267 end if;
5269 when Incomplete_Kind =>
5270 if Ada_Version >= Ada_2005 then
5272 -- In Ada 2005 an incomplete type can be explicitly tagged:
5273 -- propagate indication. Note that we also have to include
5274 -- subtypes for Ada 2012 extended use of incomplete types.
5276 Set_Ekind (Id, E_Incomplete_Subtype);
5277 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5278 Set_Private_Dependents (Id, New_Elmt_List);
5280 if Is_Tagged_Type (Id) then
5281 Set_No_Tagged_Streams_Pragma
5282 (Id, No_Tagged_Streams_Pragma (T));
5283 Set_Direct_Primitive_Operations (Id, New_Elmt_List);
5284 end if;
5286 -- Ada 2005 (AI-412): Decorate an incomplete subtype of an
5287 -- incomplete type visible through a limited with clause.
5289 if From_Limited_With (T)
5290 and then Present (Non_Limited_View (T))
5291 then
5292 Set_From_Limited_With (Id);
5293 Set_Non_Limited_View (Id, Non_Limited_View (T));
5295 -- Ada 2005 (AI-412): Add the regular incomplete subtype
5296 -- to the private dependents of the original incomplete
5297 -- type for future transformation.
5299 else
5300 Append_Elmt (Id, Private_Dependents (T));
5301 end if;
5303 -- If the subtype name denotes an incomplete type an error
5304 -- was already reported by Process_Subtype.
5306 else
5307 Set_Etype (Id, Any_Type);
5308 end if;
5310 when others =>
5311 raise Program_Error;
5312 end case;
5313 end if;
5315 if Etype (Id) = Any_Type then
5316 goto Leave;
5317 end if;
5319 -- Some common processing on all types
5321 Set_Size_Info (Id, T);
5322 Set_First_Rep_Item (Id, First_Rep_Item (T));
5324 -- If the parent type is a generic actual, so is the subtype. This may
5325 -- happen in a nested instance. Why Comes_From_Source test???
5327 if not Comes_From_Source (N) then
5328 Set_Is_Generic_Actual_Type (Id, Is_Generic_Actual_Type (T));
5329 end if;
5331 -- If this is a subtype declaration for an actual in an instance,
5332 -- inherit static and dynamic predicates if any.
5334 -- If declaration has no aspect specifications, inherit predicate
5335 -- info as well. Unclear how to handle the case of both specified
5336 -- and inherited predicates ??? Other inherited aspects, such as
5337 -- invariants, should be OK, but the combination with later pragmas
5338 -- may also require special merging.
5340 if Has_Predicates (T)
5341 and then Present (Predicate_Function (T))
5342 and then
5343 ((In_Instance and then not Comes_From_Source (N))
5344 or else No (Aspect_Specifications (N)))
5345 then
5346 Set_Subprograms_For_Type (Id, Subprograms_For_Type (T));
5348 if Has_Static_Predicate (T) then
5349 Set_Has_Static_Predicate (Id);
5350 Set_Static_Discrete_Predicate (Id, Static_Discrete_Predicate (T));
5351 end if;
5352 end if;
5354 -- Remaining processing depends on characteristics of base type
5356 T := Etype (Id);
5358 Set_Is_Immediately_Visible (Id, True);
5359 Set_Depends_On_Private (Id, Has_Private_Component (T));
5360 Set_Is_Descendant_Of_Address (Id, Is_Descendant_Of_Address (T));
5362 if Is_Interface (T) then
5363 Set_Is_Interface (Id);
5364 end if;
5366 if Present (Generic_Parent_Type (N))
5367 and then
5368 (Nkind (Parent (Generic_Parent_Type (N))) /=
5369 N_Formal_Type_Declaration
5370 or else Nkind (Formal_Type_Definition
5371 (Parent (Generic_Parent_Type (N)))) /=
5372 N_Formal_Private_Type_Definition)
5373 then
5374 if Is_Tagged_Type (Id) then
5376 -- If this is a generic actual subtype for a synchronized type,
5377 -- the primitive operations are those of the corresponding record
5378 -- for which there is a separate subtype declaration.
5380 if Is_Concurrent_Type (Id) then
5381 null;
5382 elsif Is_Class_Wide_Type (Id) then
5383 Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
5384 else
5385 Derive_Subprograms (Generic_Parent_Type (N), Id, T);
5386 end if;
5388 elsif Scope (Etype (Id)) /= Standard_Standard then
5389 Derive_Subprograms (Generic_Parent_Type (N), Id);
5390 end if;
5391 end if;
5393 if Is_Private_Type (T) and then Present (Full_View (T)) then
5394 Conditional_Delay (Id, Full_View (T));
5396 -- The subtypes of components or subcomponents of protected types
5397 -- do not need freeze nodes, which would otherwise appear in the
5398 -- wrong scope (before the freeze node for the protected type). The
5399 -- proper subtypes are those of the subcomponents of the corresponding
5400 -- record.
5402 elsif Ekind (Scope (Id)) /= E_Protected_Type
5403 and then Present (Scope (Scope (Id))) -- error defense
5404 and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
5405 then
5406 Conditional_Delay (Id, T);
5407 end if;
5409 -- Check that Constraint_Error is raised for a scalar subtype indication
5410 -- when the lower or upper bound of a non-null range lies outside the
5411 -- range of the type mark.
5413 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5414 if Is_Scalar_Type (Etype (Id))
5415 and then Scalar_Range (Id) /=
5416 Scalar_Range
5417 (Etype (Subtype_Mark (Subtype_Indication (N))))
5418 then
5419 Apply_Range_Check
5420 (Scalar_Range (Id),
5421 Etype (Subtype_Mark (Subtype_Indication (N))));
5423 -- In the array case, check compatibility for each index
5425 elsif Is_Array_Type (Etype (Id)) and then Present (First_Index (Id))
5426 then
5427 -- This really should be a subprogram that finds the indications
5428 -- to check???
5430 declare
5431 Subt_Index : Node_Id := First_Index (Id);
5432 Target_Index : Node_Id :=
5433 First_Index (Etype
5434 (Subtype_Mark (Subtype_Indication (N))));
5435 Has_Dyn_Chk : Boolean := Has_Dynamic_Range_Check (N);
5437 begin
5438 while Present (Subt_Index) loop
5439 if ((Nkind (Subt_Index) = N_Identifier
5440 and then Ekind (Entity (Subt_Index)) in Scalar_Kind)
5441 or else Nkind (Subt_Index) = N_Subtype_Indication)
5442 and then
5443 Nkind (Scalar_Range (Etype (Subt_Index))) = N_Range
5444 then
5445 declare
5446 Target_Typ : constant Entity_Id :=
5447 Etype (Target_Index);
5448 begin
5449 R_Checks :=
5450 Get_Range_Checks
5451 (Scalar_Range (Etype (Subt_Index)),
5452 Target_Typ,
5453 Etype (Subt_Index),
5454 Defining_Identifier (N));
5456 -- Reset Has_Dynamic_Range_Check on the subtype to
5457 -- prevent elision of the index check due to a dynamic
5458 -- check generated for a preceding index (needed since
5459 -- Insert_Range_Checks tries to avoid generating
5460 -- redundant checks on a given declaration).
5462 Set_Has_Dynamic_Range_Check (N, False);
5464 Insert_Range_Checks
5465 (R_Checks,
5467 Target_Typ,
5468 Sloc (Defining_Identifier (N)));
5470 -- Record whether this index involved a dynamic check
5472 Has_Dyn_Chk :=
5473 Has_Dyn_Chk or else Has_Dynamic_Range_Check (N);
5474 end;
5475 end if;
5477 Next_Index (Subt_Index);
5478 Next_Index (Target_Index);
5479 end loop;
5481 -- Finally, mark whether the subtype involves dynamic checks
5483 Set_Has_Dynamic_Range_Check (N, Has_Dyn_Chk);
5484 end;
5485 end if;
5486 end if;
5488 Set_Optimize_Alignment_Flags (Id);
5489 Check_Eliminated (Id);
5491 <<Leave>>
5492 if Has_Aspects (N) then
5493 Analyze_Aspect_Specifications (N, Id);
5494 end if;
5496 Analyze_Dimension (N);
5498 -- Check No_Dynamic_Sized_Objects restriction, which disallows subtype
5499 -- indications on composite types where the constraints are dynamic.
5500 -- Note that object declarations and aggregates generate implicit
5501 -- subtype declarations, which this covers. One special case is that the
5502 -- implicitly generated "=" for discriminated types includes an
5503 -- offending subtype declaration, which is harmless, so we ignore it
5504 -- here.
5506 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5507 declare
5508 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
5509 begin
5510 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint
5511 and then not (Is_Internal (Id)
5512 and then Is_TSS (Scope (Id),
5513 TSS_Composite_Equality))
5514 and then not Within_Init_Proc
5515 and then not All_Composite_Constraints_Static (Cstr)
5516 then
5517 Check_Restriction (No_Dynamic_Sized_Objects, Cstr);
5518 end if;
5519 end;
5520 end if;
5521 end Analyze_Subtype_Declaration;
5523 --------------------------------
5524 -- Analyze_Subtype_Indication --
5525 --------------------------------
5527 procedure Analyze_Subtype_Indication (N : Node_Id) is
5528 T : constant Entity_Id := Subtype_Mark (N);
5529 R : constant Node_Id := Range_Expression (Constraint (N));
5531 begin
5532 Analyze (T);
5534 if R /= Error then
5535 Analyze (R);
5536 Set_Etype (N, Etype (R));
5537 Resolve (R, Entity (T));
5538 else
5539 Set_Error_Posted (R);
5540 Set_Error_Posted (T);
5541 end if;
5542 end Analyze_Subtype_Indication;
5544 --------------------------
5545 -- Analyze_Variant_Part --
5546 --------------------------
5548 procedure Analyze_Variant_Part (N : Node_Id) is
5549 Discr_Name : Node_Id;
5550 Discr_Type : Entity_Id;
5552 procedure Process_Variant (A : Node_Id);
5553 -- Analyze declarations for a single variant
5555 package Analyze_Variant_Choices is
5556 new Generic_Analyze_Choices (Process_Variant);
5557 use Analyze_Variant_Choices;
5559 ---------------------
5560 -- Process_Variant --
5561 ---------------------
5563 procedure Process_Variant (A : Node_Id) is
5564 CL : constant Node_Id := Component_List (A);
5565 begin
5566 if not Null_Present (CL) then
5567 Analyze_Declarations (Component_Items (CL));
5569 if Present (Variant_Part (CL)) then
5570 Analyze (Variant_Part (CL));
5571 end if;
5572 end if;
5573 end Process_Variant;
5575 -- Start of processing for Analyze_Variant_Part
5577 begin
5578 Discr_Name := Name (N);
5579 Analyze (Discr_Name);
5581 -- If Discr_Name bad, get out (prevent cascaded errors)
5583 if Etype (Discr_Name) = Any_Type then
5584 return;
5585 end if;
5587 -- Check invalid discriminant in variant part
5589 if Ekind (Entity (Discr_Name)) /= E_Discriminant then
5590 Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
5591 end if;
5593 Discr_Type := Etype (Entity (Discr_Name));
5595 if not Is_Discrete_Type (Discr_Type) then
5596 Error_Msg_N
5597 ("discriminant in a variant part must be of a discrete type",
5598 Name (N));
5599 return;
5600 end if;
5602 -- Now analyze the choices, which also analyzes the declarations that
5603 -- are associated with each choice.
5605 Analyze_Choices (Variants (N), Discr_Type);
5607 -- Note: we used to instantiate and call Check_Choices here to check
5608 -- that the choices covered the discriminant, but it's too early to do
5609 -- that because of statically predicated subtypes, whose analysis may
5610 -- be deferred to their freeze point which may be as late as the freeze
5611 -- point of the containing record. So this call is now to be found in
5612 -- Freeze_Record_Declaration.
5614 end Analyze_Variant_Part;
5616 ----------------------------
5617 -- Array_Type_Declaration --
5618 ----------------------------
5620 procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
5621 Component_Def : constant Node_Id := Component_Definition (Def);
5622 Component_Typ : constant Node_Id := Subtype_Indication (Component_Def);
5623 P : constant Node_Id := Parent (Def);
5624 Element_Type : Entity_Id;
5625 Implicit_Base : Entity_Id;
5626 Index : Node_Id;
5627 Nb_Index : Nat;
5628 Priv : Entity_Id;
5629 Related_Id : Entity_Id := Empty;
5631 begin
5632 if Nkind (Def) = N_Constrained_Array_Definition then
5633 Index := First (Discrete_Subtype_Definitions (Def));
5634 else
5635 Index := First (Subtype_Marks (Def));
5636 end if;
5638 -- Find proper names for the implicit types which may be public. In case
5639 -- of anonymous arrays we use the name of the first object of that type
5640 -- as prefix.
5642 if No (T) then
5643 Related_Id := Defining_Identifier (P);
5644 else
5645 Related_Id := T;
5646 end if;
5648 Nb_Index := 1;
5649 while Present (Index) loop
5650 Analyze (Index);
5652 -- Test for odd case of trying to index a type by the type itself
5654 if Is_Entity_Name (Index) and then Entity (Index) = T then
5655 Error_Msg_N ("type& cannot be indexed by itself", Index);
5656 Set_Entity (Index, Standard_Boolean);
5657 Set_Etype (Index, Standard_Boolean);
5658 end if;
5660 -- Check SPARK restriction requiring a subtype mark
5662 if not Nkind_In (Index, N_Identifier, N_Expanded_Name) then
5663 Check_SPARK_05_Restriction ("subtype mark required", Index);
5664 end if;
5666 -- Add a subtype declaration for each index of private array type
5667 -- declaration whose etype is also private. For example:
5669 -- package Pkg is
5670 -- type Index is private;
5671 -- private
5672 -- type Table is array (Index) of ...
5673 -- end;
5675 -- This is currently required by the expander for the internally
5676 -- generated equality subprogram of records with variant parts in
5677 -- which the etype of some component is such private type.
5679 if Ekind (Current_Scope) = E_Package
5680 and then In_Private_Part (Current_Scope)
5681 and then Has_Private_Declaration (Etype (Index))
5682 then
5683 declare
5684 Loc : constant Source_Ptr := Sloc (Def);
5685 Decl : Entity_Id;
5686 New_E : Entity_Id;
5688 begin
5689 New_E := Make_Temporary (Loc, 'T');
5690 Set_Is_Internal (New_E);
5692 Decl :=
5693 Make_Subtype_Declaration (Loc,
5694 Defining_Identifier => New_E,
5695 Subtype_Indication =>
5696 New_Occurrence_Of (Etype (Index), Loc));
5698 Insert_Before (Parent (Def), Decl);
5699 Analyze (Decl);
5700 Set_Etype (Index, New_E);
5702 -- If the index is a range the Entity attribute is not
5703 -- available. Example:
5705 -- package Pkg is
5706 -- type T is private;
5707 -- private
5708 -- type T is new Natural;
5709 -- Table : array (T(1) .. T(10)) of Boolean;
5710 -- end Pkg;
5712 if Nkind (Index) /= N_Range then
5713 Set_Entity (Index, New_E);
5714 end if;
5715 end;
5716 end if;
5718 Make_Index (Index, P, Related_Id, Nb_Index);
5720 -- Check error of subtype with predicate for index type
5722 Bad_Predicated_Subtype_Use
5723 ("subtype& has predicate, not allowed as index subtype",
5724 Index, Etype (Index));
5726 -- Move to next index
5728 Next_Index (Index);
5729 Nb_Index := Nb_Index + 1;
5730 end loop;
5732 -- Process subtype indication if one is present
5734 if Present (Component_Typ) then
5735 Element_Type := Process_Subtype (Component_Typ, P, Related_Id, 'C');
5737 Set_Etype (Component_Typ, Element_Type);
5739 if not Nkind_In (Component_Typ, N_Identifier, N_Expanded_Name) then
5740 Check_SPARK_05_Restriction
5741 ("subtype mark required", Component_Typ);
5742 end if;
5744 -- Ada 2005 (AI-230): Access Definition case
5746 else pragma Assert (Present (Access_Definition (Component_Def)));
5748 -- Indicate that the anonymous access type is created by the
5749 -- array type declaration.
5751 Element_Type := Access_Definition
5752 (Related_Nod => P,
5753 N => Access_Definition (Component_Def));
5754 Set_Is_Local_Anonymous_Access (Element_Type);
5756 -- Propagate the parent. This field is needed if we have to generate
5757 -- the master_id associated with an anonymous access to task type
5758 -- component (see Expand_N_Full_Type_Declaration.Build_Master)
5760 Set_Parent (Element_Type, Parent (T));
5762 -- Ada 2005 (AI-230): In case of components that are anonymous access
5763 -- types the level of accessibility depends on the enclosing type
5764 -- declaration
5766 Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
5768 -- Ada 2005 (AI-254)
5770 declare
5771 CD : constant Node_Id :=
5772 Access_To_Subprogram_Definition
5773 (Access_Definition (Component_Def));
5774 begin
5775 if Present (CD) and then Protected_Present (CD) then
5776 Element_Type :=
5777 Replace_Anonymous_Access_To_Protected_Subprogram (Def);
5778 end if;
5779 end;
5780 end if;
5782 -- Constrained array case
5784 if No (T) then
5785 T := Create_Itype (E_Void, P, Related_Id, 'T');
5786 end if;
5788 if Nkind (Def) = N_Constrained_Array_Definition then
5790 -- Establish Implicit_Base as unconstrained base type
5792 Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
5794 Set_Etype (Implicit_Base, Implicit_Base);
5795 Set_Scope (Implicit_Base, Current_Scope);
5796 Set_Has_Delayed_Freeze (Implicit_Base);
5797 Set_Default_SSO (Implicit_Base);
5799 -- The constrained array type is a subtype of the unconstrained one
5801 Set_Ekind (T, E_Array_Subtype);
5802 Init_Size_Align (T);
5803 Set_Etype (T, Implicit_Base);
5804 Set_Scope (T, Current_Scope);
5805 Set_Is_Constrained (T);
5806 Set_First_Index (T,
5807 First (Discrete_Subtype_Definitions (Def)));
5808 Set_Has_Delayed_Freeze (T);
5810 -- Complete setup of implicit base type
5812 Set_Component_Size (Implicit_Base, Uint_0);
5813 Set_Component_Type (Implicit_Base, Element_Type);
5814 Set_Finalize_Storage_Only
5815 (Implicit_Base,
5816 Finalize_Storage_Only (Element_Type));
5817 Set_First_Index (Implicit_Base, First_Index (T));
5818 Set_Has_Controlled_Component
5819 (Implicit_Base,
5820 Has_Controlled_Component (Element_Type)
5821 or else Is_Controlled_Active (Element_Type));
5822 Set_Packed_Array_Impl_Type
5823 (Implicit_Base, Empty);
5825 Propagate_Concurrent_Flags (Implicit_Base, Element_Type);
5827 -- Unconstrained array case
5829 else
5830 Set_Ekind (T, E_Array_Type);
5831 Init_Size_Align (T);
5832 Set_Etype (T, T);
5833 Set_Scope (T, Current_Scope);
5834 Set_Component_Size (T, Uint_0);
5835 Set_Is_Constrained (T, False);
5836 Set_First_Index (T, First (Subtype_Marks (Def)));
5837 Set_Has_Delayed_Freeze (T, True);
5838 Propagate_Concurrent_Flags (T, Element_Type);
5839 Set_Has_Controlled_Component (T, Has_Controlled_Component
5840 (Element_Type)
5841 or else
5842 Is_Controlled_Active (Element_Type));
5843 Set_Finalize_Storage_Only (T, Finalize_Storage_Only
5844 (Element_Type));
5845 Set_Default_SSO (T);
5846 end if;
5848 -- Common attributes for both cases
5850 Set_Component_Type (Base_Type (T), Element_Type);
5851 Set_Packed_Array_Impl_Type (T, Empty);
5853 if Aliased_Present (Component_Definition (Def)) then
5854 Check_SPARK_05_Restriction
5855 ("aliased is not allowed", Component_Definition (Def));
5856 Set_Has_Aliased_Components (Etype (T));
5857 end if;
5859 -- Ada 2005 (AI-231): Propagate the null-excluding attribute to the
5860 -- array type to ensure that objects of this type are initialized.
5862 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (Element_Type) then
5863 Set_Can_Never_Be_Null (T);
5865 if Null_Exclusion_Present (Component_Definition (Def))
5867 -- No need to check itypes because in their case this check was
5868 -- done at their point of creation
5870 and then not Is_Itype (Element_Type)
5871 then
5872 Error_Msg_N
5873 ("`NOT NULL` not allowed (null already excluded)",
5874 Subtype_Indication (Component_Definition (Def)));
5875 end if;
5876 end if;
5878 Priv := Private_Component (Element_Type);
5880 if Present (Priv) then
5882 -- Check for circular definitions
5884 if Priv = Any_Type then
5885 Set_Component_Type (Etype (T), Any_Type);
5887 -- There is a gap in the visibility of operations on the composite
5888 -- type only if the component type is defined in a different scope.
5890 elsif Scope (Priv) = Current_Scope then
5891 null;
5893 elsif Is_Limited_Type (Priv) then
5894 Set_Is_Limited_Composite (Etype (T));
5895 Set_Is_Limited_Composite (T);
5896 else
5897 Set_Is_Private_Composite (Etype (T));
5898 Set_Is_Private_Composite (T);
5899 end if;
5900 end if;
5902 -- A syntax error in the declaration itself may lead to an empty index
5903 -- list, in which case do a minimal patch.
5905 if No (First_Index (T)) then
5906 Error_Msg_N ("missing index definition in array type declaration", T);
5908 declare
5909 Indexes : constant List_Id :=
5910 New_List (New_Occurrence_Of (Any_Id, Sloc (T)));
5911 begin
5912 Set_Discrete_Subtype_Definitions (Def, Indexes);
5913 Set_First_Index (T, First (Indexes));
5914 return;
5915 end;
5916 end if;
5918 -- Create a concatenation operator for the new type. Internal array
5919 -- types created for packed entities do not need such, they are
5920 -- compatible with the user-defined type.
5922 if Number_Dimensions (T) = 1
5923 and then not Is_Packed_Array_Impl_Type (T)
5924 then
5925 New_Concatenation_Op (T);
5926 end if;
5928 -- In the case of an unconstrained array the parser has already verified
5929 -- that all the indexes are unconstrained but we still need to make sure
5930 -- that the element type is constrained.
5932 if not Is_Definite_Subtype (Element_Type) then
5933 Error_Msg_N
5934 ("unconstrained element type in array declaration",
5935 Subtype_Indication (Component_Def));
5937 elsif Is_Abstract_Type (Element_Type) then
5938 Error_Msg_N
5939 ("the type of a component cannot be abstract",
5940 Subtype_Indication (Component_Def));
5941 end if;
5943 -- There may be an invariant declared for the component type, but
5944 -- the construction of the component invariant checking procedure
5945 -- takes place during expansion.
5946 end Array_Type_Declaration;
5948 ------------------------------------------------------
5949 -- Replace_Anonymous_Access_To_Protected_Subprogram --
5950 ------------------------------------------------------
5952 function Replace_Anonymous_Access_To_Protected_Subprogram
5953 (N : Node_Id) return Entity_Id
5955 Loc : constant Source_Ptr := Sloc (N);
5957 Curr_Scope : constant Scope_Stack_Entry :=
5958 Scope_Stack.Table (Scope_Stack.Last);
5960 Anon : constant Entity_Id := Make_Temporary (Loc, 'S');
5962 Acc : Node_Id;
5963 -- Access definition in declaration
5965 Comp : Node_Id;
5966 -- Object definition or formal definition with an access definition
5968 Decl : Node_Id;
5969 -- Declaration of anonymous access to subprogram type
5971 Spec : Node_Id;
5972 -- Original specification in access to subprogram
5974 P : Node_Id;
5976 begin
5977 Set_Is_Internal (Anon);
5979 case Nkind (N) is
5980 when N_Constrained_Array_Definition
5981 | N_Component_Declaration
5982 | N_Unconstrained_Array_Definition
5984 Comp := Component_Definition (N);
5985 Acc := Access_Definition (Comp);
5987 when N_Discriminant_Specification =>
5988 Comp := Discriminant_Type (N);
5989 Acc := Comp;
5991 when N_Parameter_Specification =>
5992 Comp := Parameter_Type (N);
5993 Acc := Comp;
5995 when N_Access_Function_Definition =>
5996 Comp := Result_Definition (N);
5997 Acc := Comp;
5999 when N_Object_Declaration =>
6000 Comp := Object_Definition (N);
6001 Acc := Comp;
6003 when N_Function_Specification =>
6004 Comp := Result_Definition (N);
6005 Acc := Comp;
6007 when others =>
6008 raise Program_Error;
6009 end case;
6011 Spec := Access_To_Subprogram_Definition (Acc);
6013 Decl :=
6014 Make_Full_Type_Declaration (Loc,
6015 Defining_Identifier => Anon,
6016 Type_Definition => Copy_Separate_Tree (Spec));
6018 Mark_Rewrite_Insertion (Decl);
6020 -- In ASIS mode, analyze the profile on the original node, because
6021 -- the separate copy does not provide enough links to recover the
6022 -- original tree. Analysis is limited to type annotations, within
6023 -- a temporary scope that serves as an anonymous subprogram to collect
6024 -- otherwise useless temporaries and itypes.
6026 if ASIS_Mode then
6027 declare
6028 Typ : constant Entity_Id := Make_Temporary (Loc, 'S');
6030 begin
6031 if Nkind (Spec) = N_Access_Function_Definition then
6032 Set_Ekind (Typ, E_Function);
6033 else
6034 Set_Ekind (Typ, E_Procedure);
6035 end if;
6037 Set_Parent (Typ, N);
6038 Set_Scope (Typ, Current_Scope);
6039 Push_Scope (Typ);
6041 -- Nothing to do if procedure is parameterless
6043 if Present (Parameter_Specifications (Spec)) then
6044 Process_Formals (Parameter_Specifications (Spec), Spec);
6045 end if;
6047 if Nkind (Spec) = N_Access_Function_Definition then
6048 declare
6049 Def : constant Node_Id := Result_Definition (Spec);
6051 begin
6052 -- The result might itself be an anonymous access type, so
6053 -- have to recurse.
6055 if Nkind (Def) = N_Access_Definition then
6056 if Present (Access_To_Subprogram_Definition (Def)) then
6057 Set_Etype
6058 (Def,
6059 Replace_Anonymous_Access_To_Protected_Subprogram
6060 (Spec));
6061 else
6062 Find_Type (Subtype_Mark (Def));
6063 end if;
6065 else
6066 Find_Type (Def);
6067 end if;
6068 end;
6069 end if;
6071 End_Scope;
6072 end;
6073 end if;
6075 -- Insert the new declaration in the nearest enclosing scope. If the
6076 -- parent is a body and N is its return type, the declaration belongs
6077 -- in the enclosing scope. Likewise if N is the type of a parameter.
6079 P := Parent (N);
6081 if Nkind (N) = N_Function_Specification
6082 and then Nkind (P) = N_Subprogram_Body
6083 then
6084 P := Parent (P);
6085 elsif Nkind (N) = N_Parameter_Specification
6086 and then Nkind (P) in N_Subprogram_Specification
6087 and then Nkind (Parent (P)) = N_Subprogram_Body
6088 then
6089 P := Parent (Parent (P));
6090 end if;
6092 while Present (P) and then not Has_Declarations (P) loop
6093 P := Parent (P);
6094 end loop;
6096 pragma Assert (Present (P));
6098 if Nkind (P) = N_Package_Specification then
6099 Prepend (Decl, Visible_Declarations (P));
6100 else
6101 Prepend (Decl, Declarations (P));
6102 end if;
6104 -- Replace the anonymous type with an occurrence of the new declaration.
6105 -- In all cases the rewritten node does not have the null-exclusion
6106 -- attribute because (if present) it was already inherited by the
6107 -- anonymous entity (Anon). Thus, in case of components we do not
6108 -- inherit this attribute.
6110 if Nkind (N) = N_Parameter_Specification then
6111 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6112 Set_Etype (Defining_Identifier (N), Anon);
6113 Set_Null_Exclusion_Present (N, False);
6115 elsif Nkind (N) = N_Object_Declaration then
6116 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6117 Set_Etype (Defining_Identifier (N), Anon);
6119 elsif Nkind (N) = N_Access_Function_Definition then
6120 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6122 elsif Nkind (N) = N_Function_Specification then
6123 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6124 Set_Etype (Defining_Unit_Name (N), Anon);
6126 else
6127 Rewrite (Comp,
6128 Make_Component_Definition (Loc,
6129 Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
6130 end if;
6132 Mark_Rewrite_Insertion (Comp);
6134 if Nkind_In (N, N_Object_Declaration, N_Access_Function_Definition)
6135 or else (Nkind (Parent (N)) = N_Full_Type_Declaration
6136 and then not Is_Type (Current_Scope))
6137 then
6139 -- Declaration can be analyzed in the current scope.
6141 Analyze (Decl);
6143 else
6144 -- Temporarily remove the current scope (record or subprogram) from
6145 -- the stack to add the new declarations to the enclosing scope.
6146 -- The anonymous entity is an Itype with the proper attributes.
6148 Scope_Stack.Decrement_Last;
6149 Analyze (Decl);
6150 Set_Is_Itype (Anon);
6151 Set_Associated_Node_For_Itype (Anon, N);
6152 Scope_Stack.Append (Curr_Scope);
6153 end if;
6155 Set_Ekind (Anon, E_Anonymous_Access_Protected_Subprogram_Type);
6156 Set_Can_Use_Internal_Rep (Anon, not Always_Compatible_Rep_On_Target);
6157 return Anon;
6158 end Replace_Anonymous_Access_To_Protected_Subprogram;
6160 -------------------------------
6161 -- Build_Derived_Access_Type --
6162 -------------------------------
6164 procedure Build_Derived_Access_Type
6165 (N : Node_Id;
6166 Parent_Type : Entity_Id;
6167 Derived_Type : Entity_Id)
6169 S : constant Node_Id := Subtype_Indication (Type_Definition (N));
6171 Desig_Type : Entity_Id;
6172 Discr : Entity_Id;
6173 Discr_Con_Elist : Elist_Id;
6174 Discr_Con_El : Elmt_Id;
6175 Subt : Entity_Id;
6177 begin
6178 -- Set the designated type so it is available in case this is an access
6179 -- to a self-referential type, e.g. a standard list type with a next
6180 -- pointer. Will be reset after subtype is built.
6182 Set_Directly_Designated_Type
6183 (Derived_Type, Designated_Type (Parent_Type));
6185 Subt := Process_Subtype (S, N);
6187 if Nkind (S) /= N_Subtype_Indication
6188 and then Subt /= Base_Type (Subt)
6189 then
6190 Set_Ekind (Derived_Type, E_Access_Subtype);
6191 end if;
6193 if Ekind (Derived_Type) = E_Access_Subtype then
6194 declare
6195 Pbase : constant Entity_Id := Base_Type (Parent_Type);
6196 Ibase : constant Entity_Id :=
6197 Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
6198 Svg_Chars : constant Name_Id := Chars (Ibase);
6199 Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
6201 begin
6202 Copy_Node (Pbase, Ibase);
6204 -- Restore Itype status after Copy_Node
6206 Set_Is_Itype (Ibase);
6207 Set_Associated_Node_For_Itype (Ibase, N);
6209 Set_Chars (Ibase, Svg_Chars);
6210 Set_Next_Entity (Ibase, Svg_Next_E);
6211 Set_Sloc (Ibase, Sloc (Derived_Type));
6212 Set_Scope (Ibase, Scope (Derived_Type));
6213 Set_Freeze_Node (Ibase, Empty);
6214 Set_Is_Frozen (Ibase, False);
6215 Set_Comes_From_Source (Ibase, False);
6216 Set_Is_First_Subtype (Ibase, False);
6218 Set_Etype (Ibase, Pbase);
6219 Set_Etype (Derived_Type, Ibase);
6220 end;
6221 end if;
6223 Set_Directly_Designated_Type
6224 (Derived_Type, Designated_Type (Subt));
6226 Set_Is_Constrained (Derived_Type, Is_Constrained (Subt));
6227 Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
6228 Set_Size_Info (Derived_Type, Parent_Type);
6229 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
6230 Set_Depends_On_Private (Derived_Type,
6231 Has_Private_Component (Derived_Type));
6232 Conditional_Delay (Derived_Type, Subt);
6234 -- Ada 2005 (AI-231): Set the null-exclusion attribute, and verify
6235 -- that it is not redundant.
6237 if Null_Exclusion_Present (Type_Definition (N)) then
6238 Set_Can_Never_Be_Null (Derived_Type);
6240 elsif Can_Never_Be_Null (Parent_Type) then
6241 Set_Can_Never_Be_Null (Derived_Type);
6242 end if;
6244 -- Note: we do not copy the Storage_Size_Variable, since we always go to
6245 -- the root type for this information.
6247 -- Apply range checks to discriminants for derived record case
6248 -- ??? THIS CODE SHOULD NOT BE HERE REALLY.
6250 Desig_Type := Designated_Type (Derived_Type);
6252 if Is_Composite_Type (Desig_Type)
6253 and then (not Is_Array_Type (Desig_Type))
6254 and then Has_Discriminants (Desig_Type)
6255 and then Base_Type (Desig_Type) /= Desig_Type
6256 then
6257 Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
6258 Discr_Con_El := First_Elmt (Discr_Con_Elist);
6260 Discr := First_Discriminant (Base_Type (Desig_Type));
6261 while Present (Discr_Con_El) loop
6262 Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
6263 Next_Elmt (Discr_Con_El);
6264 Next_Discriminant (Discr);
6265 end loop;
6266 end if;
6267 end Build_Derived_Access_Type;
6269 ------------------------------
6270 -- Build_Derived_Array_Type --
6271 ------------------------------
6273 procedure Build_Derived_Array_Type
6274 (N : Node_Id;
6275 Parent_Type : Entity_Id;
6276 Derived_Type : Entity_Id)
6278 Loc : constant Source_Ptr := Sloc (N);
6279 Tdef : constant Node_Id := Type_Definition (N);
6280 Indic : constant Node_Id := Subtype_Indication (Tdef);
6281 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
6282 Implicit_Base : Entity_Id;
6283 New_Indic : Node_Id;
6285 procedure Make_Implicit_Base;
6286 -- If the parent subtype is constrained, the derived type is a subtype
6287 -- of an implicit base type derived from the parent base.
6289 ------------------------
6290 -- Make_Implicit_Base --
6291 ------------------------
6293 procedure Make_Implicit_Base is
6294 begin
6295 Implicit_Base :=
6296 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
6298 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
6299 Set_Etype (Implicit_Base, Parent_Base);
6301 Copy_Array_Subtype_Attributes (Implicit_Base, Parent_Base);
6302 Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
6304 Set_Has_Delayed_Freeze (Implicit_Base, True);
6305 end Make_Implicit_Base;
6307 -- Start of processing for Build_Derived_Array_Type
6309 begin
6310 if not Is_Constrained (Parent_Type) then
6311 if Nkind (Indic) /= N_Subtype_Indication then
6312 Set_Ekind (Derived_Type, E_Array_Type);
6314 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6315 Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
6317 Set_Has_Delayed_Freeze (Derived_Type, True);
6319 else
6320 Make_Implicit_Base;
6321 Set_Etype (Derived_Type, Implicit_Base);
6323 New_Indic :=
6324 Make_Subtype_Declaration (Loc,
6325 Defining_Identifier => Derived_Type,
6326 Subtype_Indication =>
6327 Make_Subtype_Indication (Loc,
6328 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
6329 Constraint => Constraint (Indic)));
6331 Rewrite (N, New_Indic);
6332 Analyze (N);
6333 end if;
6335 else
6336 if Nkind (Indic) /= N_Subtype_Indication then
6337 Make_Implicit_Base;
6339 Set_Ekind (Derived_Type, Ekind (Parent_Type));
6340 Set_Etype (Derived_Type, Implicit_Base);
6341 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6343 else
6344 Error_Msg_N ("illegal constraint on constrained type", Indic);
6345 end if;
6346 end if;
6348 -- If parent type is not a derived type itself, and is declared in
6349 -- closed scope (e.g. a subprogram), then we must explicitly introduce
6350 -- the new type's concatenation operator since Derive_Subprograms
6351 -- will not inherit the parent's operator. If the parent type is
6352 -- unconstrained, the operator is of the unconstrained base type.
6354 if Number_Dimensions (Parent_Type) = 1
6355 and then not Is_Limited_Type (Parent_Type)
6356 and then not Is_Derived_Type (Parent_Type)
6357 and then not Is_Package_Or_Generic_Package
6358 (Scope (Base_Type (Parent_Type)))
6359 then
6360 if not Is_Constrained (Parent_Type)
6361 and then Is_Constrained (Derived_Type)
6362 then
6363 New_Concatenation_Op (Implicit_Base);
6364 else
6365 New_Concatenation_Op (Derived_Type);
6366 end if;
6367 end if;
6368 end Build_Derived_Array_Type;
6370 -----------------------------------
6371 -- Build_Derived_Concurrent_Type --
6372 -----------------------------------
6374 procedure Build_Derived_Concurrent_Type
6375 (N : Node_Id;
6376 Parent_Type : Entity_Id;
6377 Derived_Type : Entity_Id)
6379 Loc : constant Source_Ptr := Sloc (N);
6381 Corr_Record : constant Entity_Id := Make_Temporary (Loc, 'C');
6382 Corr_Decl : Node_Id;
6383 Corr_Decl_Needed : Boolean;
6384 -- If the derived type has fewer discriminants than its parent, the
6385 -- corresponding record is also a derived type, in order to account for
6386 -- the bound discriminants. We create a full type declaration for it in
6387 -- this case.
6389 Constraint_Present : constant Boolean :=
6390 Nkind (Subtype_Indication (Type_Definition (N))) =
6391 N_Subtype_Indication;
6393 D_Constraint : Node_Id;
6394 New_Constraint : Elist_Id;
6395 Old_Disc : Entity_Id;
6396 New_Disc : Entity_Id;
6397 New_N : Node_Id;
6399 begin
6400 Set_Stored_Constraint (Derived_Type, No_Elist);
6401 Corr_Decl_Needed := False;
6402 Old_Disc := Empty;
6404 if Present (Discriminant_Specifications (N))
6405 and then Constraint_Present
6406 then
6407 Old_Disc := First_Discriminant (Parent_Type);
6408 New_Disc := First (Discriminant_Specifications (N));
6409 while Present (New_Disc) and then Present (Old_Disc) loop
6410 Next_Discriminant (Old_Disc);
6411 Next (New_Disc);
6412 end loop;
6413 end if;
6415 if Present (Old_Disc) and then Expander_Active then
6417 -- The new type has fewer discriminants, so we need to create a new
6418 -- corresponding record, which is derived from the corresponding
6419 -- record of the parent, and has a stored constraint that captures
6420 -- the values of the discriminant constraints. The corresponding
6421 -- record is needed only if expander is active and code generation is
6422 -- enabled.
6424 -- The type declaration for the derived corresponding record has the
6425 -- same discriminant part and constraints as the current declaration.
6426 -- Copy the unanalyzed tree to build declaration.
6428 Corr_Decl_Needed := True;
6429 New_N := Copy_Separate_Tree (N);
6431 Corr_Decl :=
6432 Make_Full_Type_Declaration (Loc,
6433 Defining_Identifier => Corr_Record,
6434 Discriminant_Specifications =>
6435 Discriminant_Specifications (New_N),
6436 Type_Definition =>
6437 Make_Derived_Type_Definition (Loc,
6438 Subtype_Indication =>
6439 Make_Subtype_Indication (Loc,
6440 Subtype_Mark =>
6441 New_Occurrence_Of
6442 (Corresponding_Record_Type (Parent_Type), Loc),
6443 Constraint =>
6444 Constraint
6445 (Subtype_Indication (Type_Definition (New_N))))));
6446 end if;
6448 -- Copy Storage_Size and Relative_Deadline variables if task case
6450 if Is_Task_Type (Parent_Type) then
6451 Set_Storage_Size_Variable (Derived_Type,
6452 Storage_Size_Variable (Parent_Type));
6453 Set_Relative_Deadline_Variable (Derived_Type,
6454 Relative_Deadline_Variable (Parent_Type));
6455 end if;
6457 if Present (Discriminant_Specifications (N)) then
6458 Push_Scope (Derived_Type);
6459 Check_Or_Process_Discriminants (N, Derived_Type);
6461 if Constraint_Present then
6462 New_Constraint :=
6463 Expand_To_Stored_Constraint
6464 (Parent_Type,
6465 Build_Discriminant_Constraints
6466 (Parent_Type,
6467 Subtype_Indication (Type_Definition (N)), True));
6468 end if;
6470 End_Scope;
6472 elsif Constraint_Present then
6474 -- Build constrained subtype, copying the constraint, and derive
6475 -- from it to create a derived constrained type.
6477 declare
6478 Loc : constant Source_Ptr := Sloc (N);
6479 Anon : constant Entity_Id :=
6480 Make_Defining_Identifier (Loc,
6481 Chars => New_External_Name (Chars (Derived_Type), 'T'));
6482 Decl : Node_Id;
6484 begin
6485 Decl :=
6486 Make_Subtype_Declaration (Loc,
6487 Defining_Identifier => Anon,
6488 Subtype_Indication =>
6489 New_Copy_Tree (Subtype_Indication (Type_Definition (N))));
6490 Insert_Before (N, Decl);
6491 Analyze (Decl);
6493 Rewrite (Subtype_Indication (Type_Definition (N)),
6494 New_Occurrence_Of (Anon, Loc));
6495 Set_Analyzed (Derived_Type, False);
6496 Analyze (N);
6497 return;
6498 end;
6499 end if;
6501 -- By default, operations and private data are inherited from parent.
6502 -- However, in the presence of bound discriminants, a new corresponding
6503 -- record will be created, see below.
6505 Set_Has_Discriminants
6506 (Derived_Type, Has_Discriminants (Parent_Type));
6507 Set_Corresponding_Record_Type
6508 (Derived_Type, Corresponding_Record_Type (Parent_Type));
6510 -- Is_Constrained is set according the parent subtype, but is set to
6511 -- False if the derived type is declared with new discriminants.
6513 Set_Is_Constrained
6514 (Derived_Type,
6515 (Is_Constrained (Parent_Type) or else Constraint_Present)
6516 and then not Present (Discriminant_Specifications (N)));
6518 if Constraint_Present then
6519 if not Has_Discriminants (Parent_Type) then
6520 Error_Msg_N ("untagged parent must have discriminants", N);
6522 elsif Present (Discriminant_Specifications (N)) then
6524 -- Verify that new discriminants are used to constrain old ones
6526 D_Constraint :=
6527 First
6528 (Constraints
6529 (Constraint (Subtype_Indication (Type_Definition (N)))));
6531 Old_Disc := First_Discriminant (Parent_Type);
6533 while Present (D_Constraint) loop
6534 if Nkind (D_Constraint) /= N_Discriminant_Association then
6536 -- Positional constraint. If it is a reference to a new
6537 -- discriminant, it constrains the corresponding old one.
6539 if Nkind (D_Constraint) = N_Identifier then
6540 New_Disc := First_Discriminant (Derived_Type);
6541 while Present (New_Disc) loop
6542 exit when Chars (New_Disc) = Chars (D_Constraint);
6543 Next_Discriminant (New_Disc);
6544 end loop;
6546 if Present (New_Disc) then
6547 Set_Corresponding_Discriminant (New_Disc, Old_Disc);
6548 end if;
6549 end if;
6551 Next_Discriminant (Old_Disc);
6553 -- if this is a named constraint, search by name for the old
6554 -- discriminants constrained by the new one.
6556 elsif Nkind (Expression (D_Constraint)) = N_Identifier then
6558 -- Find new discriminant with that name
6560 New_Disc := First_Discriminant (Derived_Type);
6561 while Present (New_Disc) loop
6562 exit when
6563 Chars (New_Disc) = Chars (Expression (D_Constraint));
6564 Next_Discriminant (New_Disc);
6565 end loop;
6567 if Present (New_Disc) then
6569 -- Verify that new discriminant renames some discriminant
6570 -- of the parent type, and associate the new discriminant
6571 -- with one or more old ones that it renames.
6573 declare
6574 Selector : Node_Id;
6576 begin
6577 Selector := First (Selector_Names (D_Constraint));
6578 while Present (Selector) loop
6579 Old_Disc := First_Discriminant (Parent_Type);
6580 while Present (Old_Disc) loop
6581 exit when Chars (Old_Disc) = Chars (Selector);
6582 Next_Discriminant (Old_Disc);
6583 end loop;
6585 if Present (Old_Disc) then
6586 Set_Corresponding_Discriminant
6587 (New_Disc, Old_Disc);
6588 end if;
6590 Next (Selector);
6591 end loop;
6592 end;
6593 end if;
6594 end if;
6596 Next (D_Constraint);
6597 end loop;
6599 New_Disc := First_Discriminant (Derived_Type);
6600 while Present (New_Disc) loop
6601 if No (Corresponding_Discriminant (New_Disc)) then
6602 Error_Msg_NE
6603 ("new discriminant& must constrain old one", N, New_Disc);
6605 elsif not
6606 Subtypes_Statically_Compatible
6607 (Etype (New_Disc),
6608 Etype (Corresponding_Discriminant (New_Disc)))
6609 then
6610 Error_Msg_NE
6611 ("& not statically compatible with parent discriminant",
6612 N, New_Disc);
6613 end if;
6615 Next_Discriminant (New_Disc);
6616 end loop;
6617 end if;
6619 elsif Present (Discriminant_Specifications (N)) then
6620 Error_Msg_N
6621 ("missing discriminant constraint in untagged derivation", N);
6622 end if;
6624 -- The entity chain of the derived type includes the new discriminants
6625 -- but shares operations with the parent.
6627 if Present (Discriminant_Specifications (N)) then
6628 Old_Disc := First_Discriminant (Parent_Type);
6629 while Present (Old_Disc) loop
6630 if No (Next_Entity (Old_Disc))
6631 or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
6632 then
6633 Set_Next_Entity
6634 (Last_Entity (Derived_Type), Next_Entity (Old_Disc));
6635 exit;
6636 end if;
6638 Next_Discriminant (Old_Disc);
6639 end loop;
6641 else
6642 Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
6643 if Has_Discriminants (Parent_Type) then
6644 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
6645 Set_Discriminant_Constraint (
6646 Derived_Type, Discriminant_Constraint (Parent_Type));
6647 end if;
6648 end if;
6650 Set_Last_Entity (Derived_Type, Last_Entity (Parent_Type));
6652 Set_Has_Completion (Derived_Type);
6654 if Corr_Decl_Needed then
6655 Set_Stored_Constraint (Derived_Type, New_Constraint);
6656 Insert_After (N, Corr_Decl);
6657 Analyze (Corr_Decl);
6658 Set_Corresponding_Record_Type (Derived_Type, Corr_Record);
6659 end if;
6660 end Build_Derived_Concurrent_Type;
6662 ------------------------------------
6663 -- Build_Derived_Enumeration_Type --
6664 ------------------------------------
6666 procedure Build_Derived_Enumeration_Type
6667 (N : Node_Id;
6668 Parent_Type : Entity_Id;
6669 Derived_Type : Entity_Id)
6671 Loc : constant Source_Ptr := Sloc (N);
6672 Def : constant Node_Id := Type_Definition (N);
6673 Indic : constant Node_Id := Subtype_Indication (Def);
6674 Implicit_Base : Entity_Id;
6675 Literal : Entity_Id;
6676 New_Lit : Entity_Id;
6677 Literals_List : List_Id;
6678 Type_Decl : Node_Id;
6679 Hi, Lo : Node_Id;
6680 Rang_Expr : Node_Id;
6682 begin
6683 -- Since types Standard.Character and Standard.[Wide_]Wide_Character do
6684 -- not have explicit literals lists we need to process types derived
6685 -- from them specially. This is handled by Derived_Standard_Character.
6686 -- If the parent type is a generic type, there are no literals either,
6687 -- and we construct the same skeletal representation as for the generic
6688 -- parent type.
6690 if Is_Standard_Character_Type (Parent_Type) then
6691 Derived_Standard_Character (N, Parent_Type, Derived_Type);
6693 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
6694 declare
6695 Lo : Node_Id;
6696 Hi : Node_Id;
6698 begin
6699 if Nkind (Indic) /= N_Subtype_Indication then
6700 Lo :=
6701 Make_Attribute_Reference (Loc,
6702 Attribute_Name => Name_First,
6703 Prefix => New_Occurrence_Of (Derived_Type, Loc));
6704 Set_Etype (Lo, Derived_Type);
6706 Hi :=
6707 Make_Attribute_Reference (Loc,
6708 Attribute_Name => Name_Last,
6709 Prefix => New_Occurrence_Of (Derived_Type, Loc));
6710 Set_Etype (Hi, Derived_Type);
6712 Set_Scalar_Range (Derived_Type,
6713 Make_Range (Loc,
6714 Low_Bound => Lo,
6715 High_Bound => Hi));
6716 else
6718 -- Analyze subtype indication and verify compatibility
6719 -- with parent type.
6721 if Base_Type (Process_Subtype (Indic, N)) /=
6722 Base_Type (Parent_Type)
6723 then
6724 Error_Msg_N
6725 ("illegal constraint for formal discrete type", N);
6726 end if;
6727 end if;
6728 end;
6730 else
6731 -- If a constraint is present, analyze the bounds to catch
6732 -- premature usage of the derived literals.
6734 if Nkind (Indic) = N_Subtype_Indication
6735 and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
6736 then
6737 Analyze (Low_Bound (Range_Expression (Constraint (Indic))));
6738 Analyze (High_Bound (Range_Expression (Constraint (Indic))));
6739 end if;
6741 -- Introduce an implicit base type for the derived type even if there
6742 -- is no constraint attached to it, since this seems closer to the
6743 -- Ada semantics. Build a full type declaration tree for the derived
6744 -- type using the implicit base type as the defining identifier. The
6745 -- build a subtype declaration tree which applies the constraint (if
6746 -- any) have it replace the derived type declaration.
6748 Literal := First_Literal (Parent_Type);
6749 Literals_List := New_List;
6750 while Present (Literal)
6751 and then Ekind (Literal) = E_Enumeration_Literal
6752 loop
6753 -- Literals of the derived type have the same representation as
6754 -- those of the parent type, but this representation can be
6755 -- overridden by an explicit representation clause. Indicate
6756 -- that there is no explicit representation given yet. These
6757 -- derived literals are implicit operations of the new type,
6758 -- and can be overridden by explicit ones.
6760 if Nkind (Literal) = N_Defining_Character_Literal then
6761 New_Lit :=
6762 Make_Defining_Character_Literal (Loc, Chars (Literal));
6763 else
6764 New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
6765 end if;
6767 Set_Ekind (New_Lit, E_Enumeration_Literal);
6768 Set_Enumeration_Pos (New_Lit, Enumeration_Pos (Literal));
6769 Set_Enumeration_Rep (New_Lit, Enumeration_Rep (Literal));
6770 Set_Enumeration_Rep_Expr (New_Lit, Empty);
6771 Set_Alias (New_Lit, Literal);
6772 Set_Is_Known_Valid (New_Lit, True);
6774 Append (New_Lit, Literals_List);
6775 Next_Literal (Literal);
6776 end loop;
6778 Implicit_Base :=
6779 Make_Defining_Identifier (Sloc (Derived_Type),
6780 Chars => New_External_Name (Chars (Derived_Type), 'B'));
6782 -- Indicate the proper nature of the derived type. This must be done
6783 -- before analysis of the literals, to recognize cases when a literal
6784 -- may be hidden by a previous explicit function definition (cf.
6785 -- c83031a).
6787 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
6788 Set_Etype (Derived_Type, Implicit_Base);
6790 Type_Decl :=
6791 Make_Full_Type_Declaration (Loc,
6792 Defining_Identifier => Implicit_Base,
6793 Discriminant_Specifications => No_List,
6794 Type_Definition =>
6795 Make_Enumeration_Type_Definition (Loc, Literals_List));
6797 Mark_Rewrite_Insertion (Type_Decl);
6798 Insert_Before (N, Type_Decl);
6799 Analyze (Type_Decl);
6801 -- The anonymous base now has a full declaration, but this base
6802 -- is not a first subtype.
6804 Set_Is_First_Subtype (Implicit_Base, False);
6806 -- After the implicit base is analyzed its Etype needs to be changed
6807 -- to reflect the fact that it is derived from the parent type which
6808 -- was ignored during analysis. We also set the size at this point.
6810 Set_Etype (Implicit_Base, Parent_Type);
6812 Set_Size_Info (Implicit_Base, Parent_Type);
6813 Set_RM_Size (Implicit_Base, RM_Size (Parent_Type));
6814 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
6816 -- Copy other flags from parent type
6818 Set_Has_Non_Standard_Rep
6819 (Implicit_Base, Has_Non_Standard_Rep
6820 (Parent_Type));
6821 Set_Has_Pragma_Ordered
6822 (Implicit_Base, Has_Pragma_Ordered
6823 (Parent_Type));
6824 Set_Has_Delayed_Freeze (Implicit_Base);
6826 -- Process the subtype indication including a validation check on the
6827 -- constraint, if any. If a constraint is given, its bounds must be
6828 -- implicitly converted to the new type.
6830 if Nkind (Indic) = N_Subtype_Indication then
6831 declare
6832 R : constant Node_Id :=
6833 Range_Expression (Constraint (Indic));
6835 begin
6836 if Nkind (R) = N_Range then
6837 Hi := Build_Scalar_Bound
6838 (High_Bound (R), Parent_Type, Implicit_Base);
6839 Lo := Build_Scalar_Bound
6840 (Low_Bound (R), Parent_Type, Implicit_Base);
6842 else
6843 -- Constraint is a Range attribute. Replace with explicit
6844 -- mention of the bounds of the prefix, which must be a
6845 -- subtype.
6847 Analyze (Prefix (R));
6848 Hi :=
6849 Convert_To (Implicit_Base,
6850 Make_Attribute_Reference (Loc,
6851 Attribute_Name => Name_Last,
6852 Prefix =>
6853 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
6855 Lo :=
6856 Convert_To (Implicit_Base,
6857 Make_Attribute_Reference (Loc,
6858 Attribute_Name => Name_First,
6859 Prefix =>
6860 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
6861 end if;
6862 end;
6864 else
6865 Hi :=
6866 Build_Scalar_Bound
6867 (Type_High_Bound (Parent_Type),
6868 Parent_Type, Implicit_Base);
6869 Lo :=
6870 Build_Scalar_Bound
6871 (Type_Low_Bound (Parent_Type),
6872 Parent_Type, Implicit_Base);
6873 end if;
6875 Rang_Expr :=
6876 Make_Range (Loc,
6877 Low_Bound => Lo,
6878 High_Bound => Hi);
6880 -- If we constructed a default range for the case where no range
6881 -- was given, then the expressions in the range must not freeze
6882 -- since they do not correspond to expressions in the source.
6883 -- However, if the type inherits predicates the expressions will
6884 -- be elaborated earlier and must freeze.
6886 if Nkind (Indic) /= N_Subtype_Indication
6887 and then not Has_Predicates (Derived_Type)
6888 then
6889 Set_Must_Not_Freeze (Lo);
6890 Set_Must_Not_Freeze (Hi);
6891 Set_Must_Not_Freeze (Rang_Expr);
6892 end if;
6894 Rewrite (N,
6895 Make_Subtype_Declaration (Loc,
6896 Defining_Identifier => Derived_Type,
6897 Subtype_Indication =>
6898 Make_Subtype_Indication (Loc,
6899 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
6900 Constraint =>
6901 Make_Range_Constraint (Loc,
6902 Range_Expression => Rang_Expr))));
6904 Analyze (N);
6906 -- Propagate the aspects from the original type declaration to the
6907 -- declaration of the implicit base.
6909 Move_Aspects (From => Original_Node (N), To => Type_Decl);
6911 -- Apply a range check. Since this range expression doesn't have an
6912 -- Etype, we have to specifically pass the Source_Typ parameter. Is
6913 -- this right???
6915 if Nkind (Indic) = N_Subtype_Indication then
6916 Apply_Range_Check
6917 (Range_Expression (Constraint (Indic)), Parent_Type,
6918 Source_Typ => Entity (Subtype_Mark (Indic)));
6919 end if;
6920 end if;
6921 end Build_Derived_Enumeration_Type;
6923 --------------------------------
6924 -- Build_Derived_Numeric_Type --
6925 --------------------------------
6927 procedure Build_Derived_Numeric_Type
6928 (N : Node_Id;
6929 Parent_Type : Entity_Id;
6930 Derived_Type : Entity_Id)
6932 Loc : constant Source_Ptr := Sloc (N);
6933 Tdef : constant Node_Id := Type_Definition (N);
6934 Indic : constant Node_Id := Subtype_Indication (Tdef);
6935 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
6936 No_Constraint : constant Boolean := Nkind (Indic) /=
6937 N_Subtype_Indication;
6938 Implicit_Base : Entity_Id;
6940 Lo : Node_Id;
6941 Hi : Node_Id;
6943 begin
6944 -- Process the subtype indication including a validation check on
6945 -- the constraint if any.
6947 Discard_Node (Process_Subtype (Indic, N));
6949 -- Introduce an implicit base type for the derived type even if there
6950 -- is no constraint attached to it, since this seems closer to the Ada
6951 -- semantics.
6953 Implicit_Base :=
6954 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
6956 Set_Etype (Implicit_Base, Parent_Base);
6957 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
6958 Set_Size_Info (Implicit_Base, Parent_Base);
6959 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
6960 Set_Parent (Implicit_Base, Parent (Derived_Type));
6961 Set_Is_Known_Valid (Implicit_Base, Is_Known_Valid (Parent_Base));
6963 -- Set RM Size for discrete type or decimal fixed-point type
6964 -- Ordinary fixed-point is excluded, why???
6966 if Is_Discrete_Type (Parent_Base)
6967 or else Is_Decimal_Fixed_Point_Type (Parent_Base)
6968 then
6969 Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
6970 end if;
6972 Set_Has_Delayed_Freeze (Implicit_Base);
6974 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
6975 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
6977 Set_Scalar_Range (Implicit_Base,
6978 Make_Range (Loc,
6979 Low_Bound => Lo,
6980 High_Bound => Hi));
6982 if Has_Infinities (Parent_Base) then
6983 Set_Includes_Infinities (Scalar_Range (Implicit_Base));
6984 end if;
6986 -- The Derived_Type, which is the entity of the declaration, is a
6987 -- subtype of the implicit base. Its Ekind is a subtype, even in the
6988 -- absence of an explicit constraint.
6990 Set_Etype (Derived_Type, Implicit_Base);
6992 -- If we did not have a constraint, then the Ekind is set from the
6993 -- parent type (otherwise Process_Subtype has set the bounds)
6995 if No_Constraint then
6996 Set_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
6997 end if;
6999 -- If we did not have a range constraint, then set the range from the
7000 -- parent type. Otherwise, the Process_Subtype call has set the bounds.
7002 if No_Constraint or else not Has_Range_Constraint (Indic) then
7003 Set_Scalar_Range (Derived_Type,
7004 Make_Range (Loc,
7005 Low_Bound => New_Copy_Tree (Type_Low_Bound (Parent_Type)),
7006 High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
7007 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7009 if Has_Infinities (Parent_Type) then
7010 Set_Includes_Infinities (Scalar_Range (Derived_Type));
7011 end if;
7013 Set_Is_Known_Valid (Derived_Type, Is_Known_Valid (Parent_Type));
7014 end if;
7016 Set_Is_Descendant_Of_Address (Derived_Type,
7017 Is_Descendant_Of_Address (Parent_Type));
7018 Set_Is_Descendant_Of_Address (Implicit_Base,
7019 Is_Descendant_Of_Address (Parent_Type));
7021 -- Set remaining type-specific fields, depending on numeric type
7023 if Is_Modular_Integer_Type (Parent_Type) then
7024 Set_Modulus (Implicit_Base, Modulus (Parent_Base));
7026 Set_Non_Binary_Modulus
7027 (Implicit_Base, Non_Binary_Modulus (Parent_Base));
7029 Set_Is_Known_Valid
7030 (Implicit_Base, Is_Known_Valid (Parent_Base));
7032 elsif Is_Floating_Point_Type (Parent_Type) then
7034 -- Digits of base type is always copied from the digits value of
7035 -- the parent base type, but the digits of the derived type will
7036 -- already have been set if there was a constraint present.
7038 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
7039 Set_Float_Rep (Implicit_Base, Float_Rep (Parent_Base));
7041 if No_Constraint then
7042 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
7043 end if;
7045 elsif Is_Fixed_Point_Type (Parent_Type) then
7047 -- Small of base type and derived type are always copied from the
7048 -- parent base type, since smalls never change. The delta of the
7049 -- base type is also copied from the parent base type. However the
7050 -- delta of the derived type will have been set already if a
7051 -- constraint was present.
7053 Set_Small_Value (Derived_Type, Small_Value (Parent_Base));
7054 Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
7055 Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
7057 if No_Constraint then
7058 Set_Delta_Value (Derived_Type, Delta_Value (Parent_Type));
7059 end if;
7061 -- The scale and machine radix in the decimal case are always
7062 -- copied from the parent base type.
7064 if Is_Decimal_Fixed_Point_Type (Parent_Type) then
7065 Set_Scale_Value (Derived_Type, Scale_Value (Parent_Base));
7066 Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
7068 Set_Machine_Radix_10
7069 (Derived_Type, Machine_Radix_10 (Parent_Base));
7070 Set_Machine_Radix_10
7071 (Implicit_Base, Machine_Radix_10 (Parent_Base));
7073 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
7075 if No_Constraint then
7076 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
7078 else
7079 -- the analysis of the subtype_indication sets the
7080 -- digits value of the derived type.
7082 null;
7083 end if;
7084 end if;
7085 end if;
7087 if Is_Integer_Type (Parent_Type) then
7088 Set_Has_Shift_Operator
7089 (Implicit_Base, Has_Shift_Operator (Parent_Type));
7090 end if;
7092 -- The type of the bounds is that of the parent type, and they
7093 -- must be converted to the derived type.
7095 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
7097 -- The implicit_base should be frozen when the derived type is frozen,
7098 -- but note that it is used in the conversions of the bounds. For fixed
7099 -- types we delay the determination of the bounds until the proper
7100 -- freezing point. For other numeric types this is rejected by GCC, for
7101 -- reasons that are currently unclear (???), so we choose to freeze the
7102 -- implicit base now. In the case of integers and floating point types
7103 -- this is harmless because subsequent representation clauses cannot
7104 -- affect anything, but it is still baffling that we cannot use the
7105 -- same mechanism for all derived numeric types.
7107 -- There is a further complication: actually some representation
7108 -- clauses can affect the implicit base type. For example, attribute
7109 -- definition clauses for stream-oriented attributes need to set the
7110 -- corresponding TSS entries on the base type, and this normally
7111 -- cannot be done after the base type is frozen, so the circuitry in
7112 -- Sem_Ch13.New_Stream_Subprogram must account for this possibility
7113 -- and not use Set_TSS in this case.
7115 -- There are also consequences for the case of delayed representation
7116 -- aspects for some cases. For example, a Size aspect is delayed and
7117 -- should not be evaluated to the freeze point. This early freezing
7118 -- means that the size attribute evaluation happens too early???
7120 if Is_Fixed_Point_Type (Parent_Type) then
7121 Conditional_Delay (Implicit_Base, Parent_Type);
7122 else
7123 Freeze_Before (N, Implicit_Base);
7124 end if;
7125 end Build_Derived_Numeric_Type;
7127 --------------------------------
7128 -- Build_Derived_Private_Type --
7129 --------------------------------
7131 procedure Build_Derived_Private_Type
7132 (N : Node_Id;
7133 Parent_Type : Entity_Id;
7134 Derived_Type : Entity_Id;
7135 Is_Completion : Boolean;
7136 Derive_Subps : Boolean := True)
7138 Loc : constant Source_Ptr := Sloc (N);
7139 Par_Base : constant Entity_Id := Base_Type (Parent_Type);
7140 Par_Scope : constant Entity_Id := Scope (Par_Base);
7141 Full_N : constant Node_Id := New_Copy_Tree (N);
7142 Full_Der : Entity_Id := New_Copy (Derived_Type);
7143 Full_P : Entity_Id;
7145 procedure Build_Full_Derivation;
7146 -- Build full derivation, i.e. derive from the full view
7148 procedure Copy_And_Build;
7149 -- Copy derived type declaration, replace parent with its full view,
7150 -- and build derivation
7152 ---------------------------
7153 -- Build_Full_Derivation --
7154 ---------------------------
7156 procedure Build_Full_Derivation is
7157 begin
7158 -- If parent scope is not open, install the declarations
7160 if not In_Open_Scopes (Par_Scope) then
7161 Install_Private_Declarations (Par_Scope);
7162 Install_Visible_Declarations (Par_Scope);
7163 Copy_And_Build;
7164 Uninstall_Declarations (Par_Scope);
7166 -- If parent scope is open and in another unit, and parent has a
7167 -- completion, then the derivation is taking place in the visible
7168 -- part of a child unit. In that case retrieve the full view of
7169 -- the parent momentarily.
7171 elsif not In_Same_Source_Unit (N, Parent_Type) then
7172 Full_P := Full_View (Parent_Type);
7173 Exchange_Declarations (Parent_Type);
7174 Copy_And_Build;
7175 Exchange_Declarations (Full_P);
7177 -- Otherwise it is a local derivation
7179 else
7180 Copy_And_Build;
7181 end if;
7182 end Build_Full_Derivation;
7184 --------------------
7185 -- Copy_And_Build --
7186 --------------------
7188 procedure Copy_And_Build is
7189 Full_Parent : Entity_Id := Parent_Type;
7191 begin
7192 -- If the parent is itself derived from another private type,
7193 -- installing the private declarations has not affected its
7194 -- privacy status, so use its own full view explicitly.
7196 if Is_Private_Type (Full_Parent)
7197 and then Present (Full_View (Full_Parent))
7198 then
7199 Full_Parent := Full_View (Full_Parent);
7200 end if;
7202 -- And its underlying full view if necessary
7204 if Is_Private_Type (Full_Parent)
7205 and then Present (Underlying_Full_View (Full_Parent))
7206 then
7207 Full_Parent := Underlying_Full_View (Full_Parent);
7208 end if;
7210 -- For record, access and most enumeration types, derivation from
7211 -- the full view requires a fully-fledged declaration. In the other
7212 -- cases, just use an itype.
7214 if Ekind (Full_Parent) in Record_Kind
7215 or else Ekind (Full_Parent) in Access_Kind
7216 or else
7217 (Ekind (Full_Parent) in Enumeration_Kind
7218 and then not Is_Standard_Character_Type (Full_Parent)
7219 and then not Is_Generic_Type (Root_Type (Full_Parent)))
7220 then
7221 -- Copy and adjust declaration to provide a completion for what
7222 -- is originally a private declaration. Indicate that full view
7223 -- is internally generated.
7225 Set_Comes_From_Source (Full_N, False);
7226 Set_Comes_From_Source (Full_Der, False);
7227 Set_Parent (Full_Der, Full_N);
7228 Set_Defining_Identifier (Full_N, Full_Der);
7230 -- If there are no constraints, adjust the subtype mark
7232 if Nkind (Subtype_Indication (Type_Definition (Full_N))) /=
7233 N_Subtype_Indication
7234 then
7235 Set_Subtype_Indication
7236 (Type_Definition (Full_N),
7237 New_Occurrence_Of (Full_Parent, Sloc (Full_N)));
7238 end if;
7240 Insert_After (N, Full_N);
7242 -- Build full view of derived type from full view of parent which
7243 -- is now installed. Subprograms have been derived on the partial
7244 -- view, the completion does not derive them anew.
7246 if Ekind (Full_Parent) in Record_Kind then
7248 -- If parent type is tagged, the completion inherits the proper
7249 -- primitive operations.
7251 if Is_Tagged_Type (Parent_Type) then
7252 Build_Derived_Record_Type
7253 (Full_N, Full_Parent, Full_Der, Derive_Subps);
7254 else
7255 Build_Derived_Record_Type
7256 (Full_N, Full_Parent, Full_Der, Derive_Subps => False);
7257 end if;
7259 else
7260 Build_Derived_Type
7261 (Full_N, Full_Parent, Full_Der,
7262 Is_Completion => False, Derive_Subps => False);
7263 end if;
7265 -- The full declaration has been introduced into the tree and
7266 -- processed in the step above. It should not be analyzed again
7267 -- (when encountered later in the current list of declarations)
7268 -- to prevent spurious name conflicts. The full entity remains
7269 -- invisible.
7271 Set_Analyzed (Full_N);
7273 else
7274 Full_Der :=
7275 Make_Defining_Identifier (Sloc (Derived_Type),
7276 Chars => Chars (Derived_Type));
7277 Set_Is_Itype (Full_Der);
7278 Set_Associated_Node_For_Itype (Full_Der, N);
7279 Set_Parent (Full_Der, N);
7280 Build_Derived_Type
7281 (N, Full_Parent, Full_Der,
7282 Is_Completion => False, Derive_Subps => False);
7283 end if;
7285 Set_Has_Private_Declaration (Full_Der);
7286 Set_Has_Private_Declaration (Derived_Type);
7288 Set_Scope (Full_Der, Scope (Derived_Type));
7289 Set_Is_First_Subtype (Full_Der, Is_First_Subtype (Derived_Type));
7290 Set_Has_Size_Clause (Full_Der, False);
7291 Set_Has_Alignment_Clause (Full_Der, False);
7292 Set_Has_Delayed_Freeze (Full_Der);
7293 Set_Is_Frozen (Full_Der, False);
7294 Set_Freeze_Node (Full_Der, Empty);
7295 Set_Depends_On_Private (Full_Der, Has_Private_Component (Full_Der));
7296 Set_Is_Public (Full_Der, Is_Public (Derived_Type));
7298 -- The convention on the base type may be set in the private part
7299 -- and not propagated to the subtype until later, so we obtain the
7300 -- convention from the base type of the parent.
7302 Set_Convention (Full_Der, Convention (Base_Type (Full_Parent)));
7303 end Copy_And_Build;
7305 -- Start of processing for Build_Derived_Private_Type
7307 begin
7308 if Is_Tagged_Type (Parent_Type) then
7309 Full_P := Full_View (Parent_Type);
7311 -- A type extension of a type with unknown discriminants is an
7312 -- indefinite type that the back-end cannot handle directly.
7313 -- We treat it as a private type, and build a completion that is
7314 -- derived from the full view of the parent, and hopefully has
7315 -- known discriminants.
7317 -- If the full view of the parent type has an underlying record view,
7318 -- use it to generate the underlying record view of this derived type
7319 -- (required for chains of derivations with unknown discriminants).
7321 -- Minor optimization: we avoid the generation of useless underlying
7322 -- record view entities if the private type declaration has unknown
7323 -- discriminants but its corresponding full view has no
7324 -- discriminants.
7326 if Has_Unknown_Discriminants (Parent_Type)
7327 and then Present (Full_P)
7328 and then (Has_Discriminants (Full_P)
7329 or else Present (Underlying_Record_View (Full_P)))
7330 and then not In_Open_Scopes (Par_Scope)
7331 and then Expander_Active
7332 then
7333 declare
7334 Full_Der : constant Entity_Id := Make_Temporary (Loc, 'T');
7335 New_Ext : constant Node_Id :=
7336 Copy_Separate_Tree
7337 (Record_Extension_Part (Type_Definition (N)));
7338 Decl : Node_Id;
7340 begin
7341 Build_Derived_Record_Type
7342 (N, Parent_Type, Derived_Type, Derive_Subps);
7344 -- Build anonymous completion, as a derivation from the full
7345 -- view of the parent. This is not a completion in the usual
7346 -- sense, because the current type is not private.
7348 Decl :=
7349 Make_Full_Type_Declaration (Loc,
7350 Defining_Identifier => Full_Der,
7351 Type_Definition =>
7352 Make_Derived_Type_Definition (Loc,
7353 Subtype_Indication =>
7354 New_Copy_Tree
7355 (Subtype_Indication (Type_Definition (N))),
7356 Record_Extension_Part => New_Ext));
7358 -- If the parent type has an underlying record view, use it
7359 -- here to build the new underlying record view.
7361 if Present (Underlying_Record_View (Full_P)) then
7362 pragma Assert
7363 (Nkind (Subtype_Indication (Type_Definition (Decl)))
7364 = N_Identifier);
7365 Set_Entity (Subtype_Indication (Type_Definition (Decl)),
7366 Underlying_Record_View (Full_P));
7367 end if;
7369 Install_Private_Declarations (Par_Scope);
7370 Install_Visible_Declarations (Par_Scope);
7371 Insert_Before (N, Decl);
7373 -- Mark entity as an underlying record view before analysis,
7374 -- to avoid generating the list of its primitive operations
7375 -- (which is not really required for this entity) and thus
7376 -- prevent spurious errors associated with missing overriding
7377 -- of abstract primitives (overridden only for Derived_Type).
7379 Set_Ekind (Full_Der, E_Record_Type);
7380 Set_Is_Underlying_Record_View (Full_Der);
7381 Set_Default_SSO (Full_Der);
7383 Analyze (Decl);
7385 pragma Assert (Has_Discriminants (Full_Der)
7386 and then not Has_Unknown_Discriminants (Full_Der));
7388 Uninstall_Declarations (Par_Scope);
7390 -- Freeze the underlying record view, to prevent generation of
7391 -- useless dispatching information, which is simply shared with
7392 -- the real derived type.
7394 Set_Is_Frozen (Full_Der);
7396 -- If the derived type has access discriminants, create
7397 -- references to their anonymous types now, to prevent
7398 -- back-end problems when their first use is in generated
7399 -- bodies of primitives.
7401 declare
7402 E : Entity_Id;
7404 begin
7405 E := First_Entity (Full_Der);
7407 while Present (E) loop
7408 if Ekind (E) = E_Discriminant
7409 and then Ekind (Etype (E)) = E_Anonymous_Access_Type
7410 then
7411 Build_Itype_Reference (Etype (E), Decl);
7412 end if;
7414 Next_Entity (E);
7415 end loop;
7416 end;
7418 -- Set up links between real entity and underlying record view
7420 Set_Underlying_Record_View (Derived_Type, Base_Type (Full_Der));
7421 Set_Underlying_Record_View (Base_Type (Full_Der), Derived_Type);
7422 end;
7424 -- If discriminants are known, build derived record
7426 else
7427 Build_Derived_Record_Type
7428 (N, Parent_Type, Derived_Type, Derive_Subps);
7429 end if;
7431 return;
7433 elsif Has_Discriminants (Parent_Type) then
7435 -- Build partial view of derived type from partial view of parent.
7436 -- This must be done before building the full derivation because the
7437 -- second derivation will modify the discriminants of the first and
7438 -- the discriminants are chained with the rest of the components in
7439 -- the full derivation.
7441 Build_Derived_Record_Type
7442 (N, Parent_Type, Derived_Type, Derive_Subps);
7444 -- Build the full derivation if this is not the anonymous derived
7445 -- base type created by Build_Derived_Record_Type in the constrained
7446 -- case (see point 5. of its head comment) since we build it for the
7447 -- derived subtype. And skip it for protected types altogether, as
7448 -- gigi does not use these types directly.
7450 if Present (Full_View (Parent_Type))
7451 and then not Is_Itype (Derived_Type)
7452 and then not (Ekind (Full_View (Parent_Type)) in Protected_Kind)
7453 then
7454 declare
7455 Der_Base : constant Entity_Id := Base_Type (Derived_Type);
7456 Discr : Entity_Id;
7457 Last_Discr : Entity_Id;
7459 begin
7460 -- If this is not a completion, construct the implicit full
7461 -- view by deriving from the full view of the parent type.
7462 -- But if this is a completion, the derived private type
7463 -- being built is a full view and the full derivation can
7464 -- only be its underlying full view.
7466 Build_Full_Derivation;
7468 if not Is_Completion then
7469 Set_Full_View (Derived_Type, Full_Der);
7470 else
7471 Set_Underlying_Full_View (Derived_Type, Full_Der);
7472 Set_Is_Underlying_Full_View (Full_Der);
7473 end if;
7475 if not Is_Base_Type (Derived_Type) then
7476 Set_Full_View (Der_Base, Base_Type (Full_Der));
7477 end if;
7479 -- Copy the discriminant list from full view to the partial
7480 -- view (base type and its subtype). Gigi requires that the
7481 -- partial and full views have the same discriminants.
7483 -- Note that since the partial view points to discriminants
7484 -- in the full view, their scope will be that of the full
7485 -- view. This might cause some front end problems and need
7486 -- adjustment???
7488 Discr := First_Discriminant (Base_Type (Full_Der));
7489 Set_First_Entity (Der_Base, Discr);
7491 loop
7492 Last_Discr := Discr;
7493 Next_Discriminant (Discr);
7494 exit when No (Discr);
7495 end loop;
7497 Set_Last_Entity (Der_Base, Last_Discr);
7498 Set_First_Entity (Derived_Type, First_Entity (Der_Base));
7499 Set_Last_Entity (Derived_Type, Last_Entity (Der_Base));
7501 Set_Stored_Constraint
7502 (Full_Der, Stored_Constraint (Derived_Type));
7503 end;
7504 end if;
7506 elsif Present (Full_View (Parent_Type))
7507 and then Has_Discriminants (Full_View (Parent_Type))
7508 then
7509 if Has_Unknown_Discriminants (Parent_Type)
7510 and then Nkind (Subtype_Indication (Type_Definition (N))) =
7511 N_Subtype_Indication
7512 then
7513 Error_Msg_N
7514 ("cannot constrain type with unknown discriminants",
7515 Subtype_Indication (Type_Definition (N)));
7516 return;
7517 end if;
7519 -- If this is not a completion, construct the implicit full view by
7520 -- deriving from the full view of the parent type. But if this is a
7521 -- completion, the derived private type being built is a full view
7522 -- and the full derivation can only be its underlying full view.
7524 Build_Full_Derivation;
7526 if not Is_Completion then
7527 Set_Full_View (Derived_Type, Full_Der);
7528 else
7529 Set_Underlying_Full_View (Derived_Type, Full_Der);
7530 Set_Is_Underlying_Full_View (Full_Der);
7531 end if;
7533 -- In any case, the primitive operations are inherited from the
7534 -- parent type, not from the internal full view.
7536 Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
7538 if Derive_Subps then
7539 Derive_Subprograms (Parent_Type, Derived_Type);
7540 end if;
7542 Set_Stored_Constraint (Derived_Type, No_Elist);
7543 Set_Is_Constrained
7544 (Derived_Type, Is_Constrained (Full_View (Parent_Type)));
7546 else
7547 -- Untagged type, No discriminants on either view
7549 if Nkind (Subtype_Indication (Type_Definition (N))) =
7550 N_Subtype_Indication
7551 then
7552 Error_Msg_N
7553 ("illegal constraint on type without discriminants", N);
7554 end if;
7556 if Present (Discriminant_Specifications (N))
7557 and then Present (Full_View (Parent_Type))
7558 and then not Is_Tagged_Type (Full_View (Parent_Type))
7559 then
7560 Error_Msg_N ("cannot add discriminants to untagged type", N);
7561 end if;
7563 Set_Stored_Constraint (Derived_Type, No_Elist);
7564 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7565 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
7566 Set_Disable_Controlled (Derived_Type, Disable_Controlled
7567 (Parent_Type));
7568 Set_Has_Controlled_Component
7569 (Derived_Type, Has_Controlled_Component
7570 (Parent_Type));
7572 -- Direct controlled types do not inherit Finalize_Storage_Only flag
7574 if not Is_Controlled_Active (Parent_Type) then
7575 Set_Finalize_Storage_Only
7576 (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
7577 end if;
7579 -- If this is not a completion, construct the implicit full view by
7580 -- deriving from the full view of the parent type.
7582 -- ??? If the parent is untagged private and its completion is
7583 -- tagged, this mechanism will not work because we cannot derive from
7584 -- the tagged full view unless we have an extension.
7586 if Present (Full_View (Parent_Type))
7587 and then not Is_Tagged_Type (Full_View (Parent_Type))
7588 and then not Is_Completion
7589 then
7590 Build_Full_Derivation;
7591 Set_Full_View (Derived_Type, Full_Der);
7592 end if;
7593 end if;
7595 Set_Has_Unknown_Discriminants (Derived_Type,
7596 Has_Unknown_Discriminants (Parent_Type));
7598 if Is_Private_Type (Derived_Type) then
7599 Set_Private_Dependents (Derived_Type, New_Elmt_List);
7600 end if;
7602 -- If the parent base type is in scope, add the derived type to its
7603 -- list of private dependents, because its full view may become
7604 -- visible subsequently (in a nested private part, a body, or in a
7605 -- further child unit).
7607 if Is_Private_Type (Par_Base) and then In_Open_Scopes (Par_Scope) then
7608 Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
7610 -- Check for unusual case where a type completed by a private
7611 -- derivation occurs within a package nested in a child unit, and
7612 -- the parent is declared in an ancestor.
7614 if Is_Child_Unit (Scope (Current_Scope))
7615 and then Is_Completion
7616 and then In_Private_Part (Current_Scope)
7617 and then Scope (Parent_Type) /= Current_Scope
7619 -- Note that if the parent has a completion in the private part,
7620 -- (which is itself a derivation from some other private type)
7621 -- it is that completion that is visible, there is no full view
7622 -- available, and no special processing is needed.
7624 and then Present (Full_View (Parent_Type))
7625 then
7626 -- In this case, the full view of the parent type will become
7627 -- visible in the body of the enclosing child, and only then will
7628 -- the current type be possibly non-private. Build an underlying
7629 -- full view that will be installed when the enclosing child body
7630 -- is compiled.
7632 if Present (Underlying_Full_View (Derived_Type)) then
7633 Full_Der := Underlying_Full_View (Derived_Type);
7634 else
7635 Build_Full_Derivation;
7636 Set_Underlying_Full_View (Derived_Type, Full_Der);
7637 Set_Is_Underlying_Full_View (Full_Der);
7638 end if;
7640 -- The full view will be used to swap entities on entry/exit to
7641 -- the body, and must appear in the entity list for the package.
7643 Append_Entity (Full_Der, Scope (Derived_Type));
7644 end if;
7645 end if;
7646 end Build_Derived_Private_Type;
7648 -------------------------------
7649 -- Build_Derived_Record_Type --
7650 -------------------------------
7652 -- 1. INTRODUCTION
7654 -- Ideally we would like to use the same model of type derivation for
7655 -- tagged and untagged record types. Unfortunately this is not quite
7656 -- possible because the semantics of representation clauses is different
7657 -- for tagged and untagged records under inheritance. Consider the
7658 -- following:
7660 -- type R (...) is [tagged] record ... end record;
7661 -- type T (...) is new R (...) [with ...];
7663 -- The representation clauses for T can specify a completely different
7664 -- record layout from R's. Hence the same component can be placed in two
7665 -- very different positions in objects of type T and R. If R and T are
7666 -- tagged types, representation clauses for T can only specify the layout
7667 -- of non inherited components, thus components that are common in R and T
7668 -- have the same position in objects of type R and T.
7670 -- This has two implications. The first is that the entire tree for R's
7671 -- declaration needs to be copied for T in the untagged case, so that T
7672 -- can be viewed as a record type of its own with its own representation
7673 -- clauses. The second implication is the way we handle discriminants.
7674 -- Specifically, in the untagged case we need a way to communicate to Gigi
7675 -- what are the real discriminants in the record, while for the semantics
7676 -- we need to consider those introduced by the user to rename the
7677 -- discriminants in the parent type. This is handled by introducing the
7678 -- notion of stored discriminants. See below for more.
7680 -- Fortunately the way regular components are inherited can be handled in
7681 -- the same way in tagged and untagged types.
7683 -- To complicate things a bit more the private view of a private extension
7684 -- cannot be handled in the same way as the full view (for one thing the
7685 -- semantic rules are somewhat different). We will explain what differs
7686 -- below.
7688 -- 2. DISCRIMINANTS UNDER INHERITANCE
7690 -- The semantic rules governing the discriminants of derived types are
7691 -- quite subtle.
7693 -- type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
7694 -- [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
7696 -- If parent type has discriminants, then the discriminants that are
7697 -- declared in the derived type are [3.4 (11)]:
7699 -- o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
7700 -- there is one;
7702 -- o Otherwise, each discriminant of the parent type (implicitly declared
7703 -- in the same order with the same specifications). In this case, the
7704 -- discriminants are said to be "inherited", or if unknown in the parent
7705 -- are also unknown in the derived type.
7707 -- Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
7709 -- o The parent subtype must be constrained;
7711 -- o If the parent type is not a tagged type, then each discriminant of
7712 -- the derived type must be used in the constraint defining a parent
7713 -- subtype. [Implementation note: This ensures that the new discriminant
7714 -- can share storage with an existing discriminant.]
7716 -- For the derived type each discriminant of the parent type is either
7717 -- inherited, constrained to equal some new discriminant of the derived
7718 -- type, or constrained to the value of an expression.
7720 -- When inherited or constrained to equal some new discriminant, the
7721 -- parent discriminant and the discriminant of the derived type are said
7722 -- to "correspond".
7724 -- If a discriminant of the parent type is constrained to a specific value
7725 -- in the derived type definition, then the discriminant is said to be
7726 -- "specified" by that derived type definition.
7728 -- 3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
7730 -- We have spoken about stored discriminants in point 1 (introduction)
7731 -- above. There are two sort of stored discriminants: implicit and
7732 -- explicit. As long as the derived type inherits the same discriminants as
7733 -- the root record type, stored discriminants are the same as regular
7734 -- discriminants, and are said to be implicit. However, if any discriminant
7735 -- in the root type was renamed in the derived type, then the derived
7736 -- type will contain explicit stored discriminants. Explicit stored
7737 -- discriminants are discriminants in addition to the semantically visible
7738 -- discriminants defined for the derived type. Stored discriminants are
7739 -- used by Gigi to figure out what are the physical discriminants in
7740 -- objects of the derived type (see precise definition in einfo.ads).
7741 -- As an example, consider the following:
7743 -- type R (D1, D2, D3 : Int) is record ... end record;
7744 -- type T1 is new R;
7745 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
7746 -- type T3 is new T2;
7747 -- type T4 (Y : Int) is new T3 (Y, 99);
7749 -- The following table summarizes the discriminants and stored
7750 -- discriminants in R and T1 through T4.
7752 -- Type Discrim Stored Discrim Comment
7753 -- R (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in R
7754 -- T1 (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in T1
7755 -- T2 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T2
7756 -- T3 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T3
7757 -- T4 (Y) (D1, D2, D3) Girder discrims EXPLICIT in T4
7759 -- Field Corresponding_Discriminant (abbreviated CD below) allows us to
7760 -- find the corresponding discriminant in the parent type, while
7761 -- Original_Record_Component (abbreviated ORC below), the actual physical
7762 -- component that is renamed. Finally the field Is_Completely_Hidden
7763 -- (abbreviated ICH below) is set for all explicit stored discriminants
7764 -- (see einfo.ads for more info). For the above example this gives:
7766 -- Discrim CD ORC ICH
7767 -- ^^^^^^^ ^^ ^^^ ^^^
7768 -- D1 in R empty itself no
7769 -- D2 in R empty itself no
7770 -- D3 in R empty itself no
7772 -- D1 in T1 D1 in R itself no
7773 -- D2 in T1 D2 in R itself no
7774 -- D3 in T1 D3 in R itself no
7776 -- X1 in T2 D3 in T1 D3 in T2 no
7777 -- X2 in T2 D1 in T1 D1 in T2 no
7778 -- D1 in T2 empty itself yes
7779 -- D2 in T2 empty itself yes
7780 -- D3 in T2 empty itself yes
7782 -- X1 in T3 X1 in T2 D3 in T3 no
7783 -- X2 in T3 X2 in T2 D1 in T3 no
7784 -- D1 in T3 empty itself yes
7785 -- D2 in T3 empty itself yes
7786 -- D3 in T3 empty itself yes
7788 -- Y in T4 X1 in T3 D3 in T3 no
7789 -- D1 in T3 empty itself yes
7790 -- D2 in T3 empty itself yes
7791 -- D3 in T3 empty itself yes
7793 -- 4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
7795 -- Type derivation for tagged types is fairly straightforward. If no
7796 -- discriminants are specified by the derived type, these are inherited
7797 -- from the parent. No explicit stored discriminants are ever necessary.
7798 -- The only manipulation that is done to the tree is that of adding a
7799 -- _parent field with parent type and constrained to the same constraint
7800 -- specified for the parent in the derived type definition. For instance:
7802 -- type R (D1, D2, D3 : Int) is tagged record ... end record;
7803 -- type T1 is new R with null record;
7804 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
7806 -- are changed into:
7808 -- type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
7809 -- _parent : R (D1, D2, D3);
7810 -- end record;
7812 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
7813 -- _parent : T1 (X2, 88, X1);
7814 -- end record;
7816 -- The discriminants actually present in R, T1 and T2 as well as their CD,
7817 -- ORC and ICH fields are:
7819 -- Discrim CD ORC ICH
7820 -- ^^^^^^^ ^^ ^^^ ^^^
7821 -- D1 in R empty itself no
7822 -- D2 in R empty itself no
7823 -- D3 in R empty itself no
7825 -- D1 in T1 D1 in R D1 in R no
7826 -- D2 in T1 D2 in R D2 in R no
7827 -- D3 in T1 D3 in R D3 in R no
7829 -- X1 in T2 D3 in T1 D3 in R no
7830 -- X2 in T2 D1 in T1 D1 in R no
7832 -- 5. FIRST TRANSFORMATION FOR DERIVED RECORDS
7834 -- Regardless of whether we dealing with a tagged or untagged type
7835 -- we will transform all derived type declarations of the form
7837 -- type T is new R (...) [with ...];
7838 -- or
7839 -- subtype S is R (...);
7840 -- type T is new S [with ...];
7841 -- into
7842 -- type BT is new R [with ...];
7843 -- subtype T is BT (...);
7845 -- That is, the base derived type is constrained only if it has no
7846 -- discriminants. The reason for doing this is that GNAT's semantic model
7847 -- assumes that a base type with discriminants is unconstrained.
7849 -- Note that, strictly speaking, the above transformation is not always
7850 -- correct. Consider for instance the following excerpt from ACVC b34011a:
7852 -- procedure B34011A is
7853 -- type REC (D : integer := 0) is record
7854 -- I : Integer;
7855 -- end record;
7857 -- package P is
7858 -- type T6 is new Rec;
7859 -- function F return T6;
7860 -- end P;
7862 -- use P;
7863 -- package Q6 is
7864 -- type U is new T6 (Q6.F.I); -- ERROR: Q6.F.
7865 -- end Q6;
7867 -- The definition of Q6.U is illegal. However transforming Q6.U into
7869 -- type BaseU is new T6;
7870 -- subtype U is BaseU (Q6.F.I)
7872 -- turns U into a legal subtype, which is incorrect. To avoid this problem
7873 -- we always analyze the constraint (in this case (Q6.F.I)) before applying
7874 -- the transformation described above.
7876 -- There is another instance where the above transformation is incorrect.
7877 -- Consider:
7879 -- package Pack is
7880 -- type Base (D : Integer) is tagged null record;
7881 -- procedure P (X : Base);
7883 -- type Der is new Base (2) with null record;
7884 -- procedure P (X : Der);
7885 -- end Pack;
7887 -- Then the above transformation turns this into
7889 -- type Der_Base is new Base with null record;
7890 -- -- procedure P (X : Base) is implicitly inherited here
7891 -- -- as procedure P (X : Der_Base).
7893 -- subtype Der is Der_Base (2);
7894 -- procedure P (X : Der);
7895 -- -- The overriding of P (X : Der_Base) is illegal since we
7896 -- -- have a parameter conformance problem.
7898 -- To get around this problem, after having semantically processed Der_Base
7899 -- and the rewritten subtype declaration for Der, we copy Der_Base field
7900 -- Discriminant_Constraint from Der so that when parameter conformance is
7901 -- checked when P is overridden, no semantic errors are flagged.
7903 -- 6. SECOND TRANSFORMATION FOR DERIVED RECORDS
7905 -- Regardless of whether we are dealing with a tagged or untagged type
7906 -- we will transform all derived type declarations of the form
7908 -- type R (D1, .., Dn : ...) is [tagged] record ...;
7909 -- type T is new R [with ...];
7910 -- into
7911 -- type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
7913 -- The reason for such transformation is that it allows us to implement a
7914 -- very clean form of component inheritance as explained below.
7916 -- Note that this transformation is not achieved by direct tree rewriting
7917 -- and manipulation, but rather by redoing the semantic actions that the
7918 -- above transformation will entail. This is done directly in routine
7919 -- Inherit_Components.
7921 -- 7. TYPE DERIVATION AND COMPONENT INHERITANCE
7923 -- In both tagged and untagged derived types, regular non discriminant
7924 -- components are inherited in the derived type from the parent type. In
7925 -- the absence of discriminants component, inheritance is straightforward
7926 -- as components can simply be copied from the parent.
7928 -- If the parent has discriminants, inheriting components constrained with
7929 -- these discriminants requires caution. Consider the following example:
7931 -- type R (D1, D2 : Positive) is [tagged] record
7932 -- S : String (D1 .. D2);
7933 -- end record;
7935 -- type T1 is new R [with null record];
7936 -- type T2 (X : positive) is new R (1, X) [with null record];
7938 -- As explained in 6. above, T1 is rewritten as
7939 -- type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
7940 -- which makes the treatment for T1 and T2 identical.
7942 -- What we want when inheriting S, is that references to D1 and D2 in R are
7943 -- replaced with references to their correct constraints, i.e. D1 and D2 in
7944 -- T1 and 1 and X in T2. So all R's discriminant references are replaced
7945 -- with either discriminant references in the derived type or expressions.
7946 -- This replacement is achieved as follows: before inheriting R's
7947 -- components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
7948 -- created in the scope of T1 (resp. scope of T2) so that discriminants D1
7949 -- and D2 of T1 are visible (resp. discriminant X of T2 is visible).
7950 -- For T2, for instance, this has the effect of replacing String (D1 .. D2)
7951 -- by String (1 .. X).
7953 -- 8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
7955 -- We explain here the rules governing private type extensions relevant to
7956 -- type derivation. These rules are explained on the following example:
7958 -- type D [(...)] is new A [(...)] with private; <-- partial view
7959 -- type D [(...)] is new P [(...)] with null record; <-- full view
7961 -- Type A is called the ancestor subtype of the private extension.
7962 -- Type P is the parent type of the full view of the private extension. It
7963 -- must be A or a type derived from A.
7965 -- The rules concerning the discriminants of private type extensions are
7966 -- [7.3(10-13)]:
7968 -- o If a private extension inherits known discriminants from the ancestor
7969 -- subtype, then the full view must also inherit its discriminants from
7970 -- the ancestor subtype and the parent subtype of the full view must be
7971 -- constrained if and only if the ancestor subtype is constrained.
7973 -- o If a partial view has unknown discriminants, then the full view may
7974 -- define a definite or an indefinite subtype, with or without
7975 -- discriminants.
7977 -- o If a partial view has neither known nor unknown discriminants, then
7978 -- the full view must define a definite subtype.
7980 -- o If the ancestor subtype of a private extension has constrained
7981 -- discriminants, then the parent subtype of the full view must impose a
7982 -- statically matching constraint on those discriminants.
7984 -- This means that only the following forms of private extensions are
7985 -- allowed:
7987 -- type D is new A with private; <-- partial view
7988 -- type D is new P with null record; <-- full view
7990 -- If A has no discriminants than P has no discriminants, otherwise P must
7991 -- inherit A's discriminants.
7993 -- type D is new A (...) with private; <-- partial view
7994 -- type D is new P (:::) with null record; <-- full view
7996 -- P must inherit A's discriminants and (...) and (:::) must statically
7997 -- match.
7999 -- subtype A is R (...);
8000 -- type D is new A with private; <-- partial view
8001 -- type D is new P with null record; <-- full view
8003 -- P must have inherited R's discriminants and must be derived from A or
8004 -- any of its subtypes.
8006 -- type D (..) is new A with private; <-- partial view
8007 -- type D (..) is new P [(:::)] with null record; <-- full view
8009 -- No specific constraints on P's discriminants or constraint (:::).
8010 -- Note that A can be unconstrained, but the parent subtype P must either
8011 -- be constrained or (:::) must be present.
8013 -- type D (..) is new A [(...)] with private; <-- partial view
8014 -- type D (..) is new P [(:::)] with null record; <-- full view
8016 -- P's constraints on A's discriminants must statically match those
8017 -- imposed by (...).
8019 -- 9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
8021 -- The full view of a private extension is handled exactly as described
8022 -- above. The model chose for the private view of a private extension is
8023 -- the same for what concerns discriminants (i.e. they receive the same
8024 -- treatment as in the tagged case). However, the private view of the
8025 -- private extension always inherits the components of the parent base,
8026 -- without replacing any discriminant reference. Strictly speaking this is
8027 -- incorrect. However, Gigi never uses this view to generate code so this
8028 -- is a purely semantic issue. In theory, a set of transformations similar
8029 -- to those given in 5. and 6. above could be applied to private views of
8030 -- private extensions to have the same model of component inheritance as
8031 -- for non private extensions. However, this is not done because it would
8032 -- further complicate private type processing. Semantically speaking, this
8033 -- leaves us in an uncomfortable situation. As an example consider:
8035 -- package Pack is
8036 -- type R (D : integer) is tagged record
8037 -- S : String (1 .. D);
8038 -- end record;
8039 -- procedure P (X : R);
8040 -- type T is new R (1) with private;
8041 -- private
8042 -- type T is new R (1) with null record;
8043 -- end;
8045 -- This is transformed into:
8047 -- package Pack is
8048 -- type R (D : integer) is tagged record
8049 -- S : String (1 .. D);
8050 -- end record;
8051 -- procedure P (X : R);
8052 -- type T is new R (1) with private;
8053 -- private
8054 -- type BaseT is new R with null record;
8055 -- subtype T is BaseT (1);
8056 -- end;
8058 -- (strictly speaking the above is incorrect Ada)
8060 -- From the semantic standpoint the private view of private extension T
8061 -- should be flagged as constrained since one can clearly have
8063 -- Obj : T;
8065 -- in a unit withing Pack. However, when deriving subprograms for the
8066 -- private view of private extension T, T must be seen as unconstrained
8067 -- since T has discriminants (this is a constraint of the current
8068 -- subprogram derivation model). Thus, when processing the private view of
8069 -- a private extension such as T, we first mark T as unconstrained, we
8070 -- process it, we perform program derivation and just before returning from
8071 -- Build_Derived_Record_Type we mark T as constrained.
8073 -- ??? Are there are other uncomfortable cases that we will have to
8074 -- deal with.
8076 -- 10. RECORD_TYPE_WITH_PRIVATE complications
8078 -- Types that are derived from a visible record type and have a private
8079 -- extension present other peculiarities. They behave mostly like private
8080 -- types, but if they have primitive operations defined, these will not
8081 -- have the proper signatures for further inheritance, because other
8082 -- primitive operations will use the implicit base that we define for
8083 -- private derivations below. This affect subprogram inheritance (see
8084 -- Derive_Subprograms for details). We also derive the implicit base from
8085 -- the base type of the full view, so that the implicit base is a record
8086 -- type and not another private type, This avoids infinite loops.
8088 procedure Build_Derived_Record_Type
8089 (N : Node_Id;
8090 Parent_Type : Entity_Id;
8091 Derived_Type : Entity_Id;
8092 Derive_Subps : Boolean := True)
8094 Discriminant_Specs : constant Boolean :=
8095 Present (Discriminant_Specifications (N));
8096 Is_Tagged : constant Boolean := Is_Tagged_Type (Parent_Type);
8097 Loc : constant Source_Ptr := Sloc (N);
8098 Private_Extension : constant Boolean :=
8099 Nkind (N) = N_Private_Extension_Declaration;
8100 Assoc_List : Elist_Id;
8101 Constraint_Present : Boolean;
8102 Constrs : Elist_Id;
8103 Discrim : Entity_Id;
8104 Indic : Node_Id;
8105 Inherit_Discrims : Boolean := False;
8106 Last_Discrim : Entity_Id;
8107 New_Base : Entity_Id;
8108 New_Decl : Node_Id;
8109 New_Discrs : Elist_Id;
8110 New_Indic : Node_Id;
8111 Parent_Base : Entity_Id;
8112 Save_Etype : Entity_Id;
8113 Save_Discr_Constr : Elist_Id;
8114 Save_Next_Entity : Entity_Id;
8115 Type_Def : Node_Id;
8117 Discs : Elist_Id := New_Elmt_List;
8118 -- An empty Discs list means that there were no constraints in the
8119 -- subtype indication or that there was an error processing it.
8121 begin
8122 if Ekind (Parent_Type) = E_Record_Type_With_Private
8123 and then Present (Full_View (Parent_Type))
8124 and then Has_Discriminants (Parent_Type)
8125 then
8126 Parent_Base := Base_Type (Full_View (Parent_Type));
8127 else
8128 Parent_Base := Base_Type (Parent_Type);
8129 end if;
8131 -- AI05-0115 : if this is a derivation from a private type in some
8132 -- other scope that may lead to invisible components for the derived
8133 -- type, mark it accordingly.
8135 if Is_Private_Type (Parent_Type) then
8136 if Scope (Parent_Type) = Scope (Derived_Type) then
8137 null;
8139 elsif In_Open_Scopes (Scope (Parent_Type))
8140 and then In_Private_Part (Scope (Parent_Type))
8141 then
8142 null;
8144 else
8145 Set_Has_Private_Ancestor (Derived_Type);
8146 end if;
8148 else
8149 Set_Has_Private_Ancestor
8150 (Derived_Type, Has_Private_Ancestor (Parent_Type));
8151 end if;
8153 -- Before we start the previously documented transformations, here is
8154 -- little fix for size and alignment of tagged types. Normally when we
8155 -- derive type D from type P, we copy the size and alignment of P as the
8156 -- default for D, and in the absence of explicit representation clauses
8157 -- for D, the size and alignment are indeed the same as the parent.
8159 -- But this is wrong for tagged types, since fields may be added, and
8160 -- the default size may need to be larger, and the default alignment may
8161 -- need to be larger.
8163 -- We therefore reset the size and alignment fields in the tagged case.
8164 -- Note that the size and alignment will in any case be at least as
8165 -- large as the parent type (since the derived type has a copy of the
8166 -- parent type in the _parent field)
8168 -- The type is also marked as being tagged here, which is needed when
8169 -- processing components with a self-referential anonymous access type
8170 -- in the call to Check_Anonymous_Access_Components below. Note that
8171 -- this flag is also set later on for completeness.
8173 if Is_Tagged then
8174 Set_Is_Tagged_Type (Derived_Type);
8175 Init_Size_Align (Derived_Type);
8176 end if;
8178 -- STEP 0a: figure out what kind of derived type declaration we have
8180 if Private_Extension then
8181 Type_Def := N;
8182 Set_Ekind (Derived_Type, E_Record_Type_With_Private);
8183 Set_Default_SSO (Derived_Type);
8185 else
8186 Type_Def := Type_Definition (N);
8188 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
8189 -- Parent_Base can be a private type or private extension. However,
8190 -- for tagged types with an extension the newly added fields are
8191 -- visible and hence the Derived_Type is always an E_Record_Type.
8192 -- (except that the parent may have its own private fields).
8193 -- For untagged types we preserve the Ekind of the Parent_Base.
8195 if Present (Record_Extension_Part (Type_Def)) then
8196 Set_Ekind (Derived_Type, E_Record_Type);
8197 Set_Default_SSO (Derived_Type);
8199 -- Create internal access types for components with anonymous
8200 -- access types.
8202 if Ada_Version >= Ada_2005 then
8203 Check_Anonymous_Access_Components
8204 (N, Derived_Type, Derived_Type,
8205 Component_List (Record_Extension_Part (Type_Def)));
8206 end if;
8208 else
8209 Set_Ekind (Derived_Type, Ekind (Parent_Base));
8210 end if;
8211 end if;
8213 -- Indic can either be an N_Identifier if the subtype indication
8214 -- contains no constraint or an N_Subtype_Indication if the subtype
8215 -- indication has a constraint.
8217 Indic := Subtype_Indication (Type_Def);
8218 Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
8220 -- Check that the type has visible discriminants. The type may be
8221 -- a private type with unknown discriminants whose full view has
8222 -- discriminants which are invisible.
8224 if Constraint_Present then
8225 if not Has_Discriminants (Parent_Base)
8226 or else
8227 (Has_Unknown_Discriminants (Parent_Base)
8228 and then Is_Private_Type (Parent_Base))
8229 then
8230 Error_Msg_N
8231 ("invalid constraint: type has no discriminant",
8232 Constraint (Indic));
8234 Constraint_Present := False;
8235 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
8237 elsif Is_Constrained (Parent_Type) then
8238 Error_Msg_N
8239 ("invalid constraint: parent type is already constrained",
8240 Constraint (Indic));
8242 Constraint_Present := False;
8243 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
8244 end if;
8245 end if;
8247 -- STEP 0b: If needed, apply transformation given in point 5. above
8249 if not Private_Extension
8250 and then Has_Discriminants (Parent_Type)
8251 and then not Discriminant_Specs
8252 and then (Is_Constrained (Parent_Type) or else Constraint_Present)
8253 then
8254 -- First, we must analyze the constraint (see comment in point 5.)
8255 -- The constraint may come from the subtype indication of the full
8256 -- declaration.
8258 if Constraint_Present then
8259 New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
8261 -- If there is no explicit constraint, there might be one that is
8262 -- inherited from a constrained parent type. In that case verify that
8263 -- it conforms to the constraint in the partial view. In perverse
8264 -- cases the parent subtypes of the partial and full view can have
8265 -- different constraints.
8267 elsif Present (Stored_Constraint (Parent_Type)) then
8268 New_Discrs := Stored_Constraint (Parent_Type);
8270 else
8271 New_Discrs := No_Elist;
8272 end if;
8274 if Has_Discriminants (Derived_Type)
8275 and then Has_Private_Declaration (Derived_Type)
8276 and then Present (Discriminant_Constraint (Derived_Type))
8277 and then Present (New_Discrs)
8278 then
8279 -- Verify that constraints of the full view statically match
8280 -- those given in the partial view.
8282 declare
8283 C1, C2 : Elmt_Id;
8285 begin
8286 C1 := First_Elmt (New_Discrs);
8287 C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
8288 while Present (C1) and then Present (C2) loop
8289 if Fully_Conformant_Expressions (Node (C1), Node (C2))
8290 or else
8291 (Is_OK_Static_Expression (Node (C1))
8292 and then Is_OK_Static_Expression (Node (C2))
8293 and then
8294 Expr_Value (Node (C1)) = Expr_Value (Node (C2)))
8295 then
8296 null;
8298 else
8299 if Constraint_Present then
8300 Error_Msg_N
8301 ("constraint not conformant to previous declaration",
8302 Node (C1));
8303 else
8304 Error_Msg_N
8305 ("constraint of full view is incompatible "
8306 & "with partial view", N);
8307 end if;
8308 end if;
8310 Next_Elmt (C1);
8311 Next_Elmt (C2);
8312 end loop;
8313 end;
8314 end if;
8316 -- Insert and analyze the declaration for the unconstrained base type
8318 New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
8320 New_Decl :=
8321 Make_Full_Type_Declaration (Loc,
8322 Defining_Identifier => New_Base,
8323 Type_Definition =>
8324 Make_Derived_Type_Definition (Loc,
8325 Abstract_Present => Abstract_Present (Type_Def),
8326 Limited_Present => Limited_Present (Type_Def),
8327 Subtype_Indication =>
8328 New_Occurrence_Of (Parent_Base, Loc),
8329 Record_Extension_Part =>
8330 Relocate_Node (Record_Extension_Part (Type_Def)),
8331 Interface_List => Interface_List (Type_Def)));
8333 Set_Parent (New_Decl, Parent (N));
8334 Mark_Rewrite_Insertion (New_Decl);
8335 Insert_Before (N, New_Decl);
8337 -- In the extension case, make sure ancestor is frozen appropriately
8338 -- (see also non-discriminated case below).
8340 if Present (Record_Extension_Part (Type_Def))
8341 or else Is_Interface (Parent_Base)
8342 then
8343 Freeze_Before (New_Decl, Parent_Type);
8344 end if;
8346 -- Note that this call passes False for the Derive_Subps parameter
8347 -- because subprogram derivation is deferred until after creating
8348 -- the subtype (see below).
8350 Build_Derived_Type
8351 (New_Decl, Parent_Base, New_Base,
8352 Is_Completion => False, Derive_Subps => False);
8354 -- ??? This needs re-examination to determine whether the
8355 -- above call can simply be replaced by a call to Analyze.
8357 Set_Analyzed (New_Decl);
8359 -- Insert and analyze the declaration for the constrained subtype
8361 if Constraint_Present then
8362 New_Indic :=
8363 Make_Subtype_Indication (Loc,
8364 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
8365 Constraint => Relocate_Node (Constraint (Indic)));
8367 else
8368 declare
8369 Constr_List : constant List_Id := New_List;
8370 C : Elmt_Id;
8371 Expr : Node_Id;
8373 begin
8374 C := First_Elmt (Discriminant_Constraint (Parent_Type));
8375 while Present (C) loop
8376 Expr := Node (C);
8378 -- It is safe here to call New_Copy_Tree since we called
8379 -- Force_Evaluation on each constraint previously
8380 -- in Build_Discriminant_Constraints.
8382 Append (New_Copy_Tree (Expr), To => Constr_List);
8384 Next_Elmt (C);
8385 end loop;
8387 New_Indic :=
8388 Make_Subtype_Indication (Loc,
8389 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
8390 Constraint =>
8391 Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
8392 end;
8393 end if;
8395 Rewrite (N,
8396 Make_Subtype_Declaration (Loc,
8397 Defining_Identifier => Derived_Type,
8398 Subtype_Indication => New_Indic));
8400 Analyze (N);
8402 -- Derivation of subprograms must be delayed until the full subtype
8403 -- has been established, to ensure proper overriding of subprograms
8404 -- inherited by full types. If the derivations occurred as part of
8405 -- the call to Build_Derived_Type above, then the check for type
8406 -- conformance would fail because earlier primitive subprograms
8407 -- could still refer to the full type prior the change to the new
8408 -- subtype and hence would not match the new base type created here.
8409 -- Subprograms are not derived, however, when Derive_Subps is False
8410 -- (since otherwise there could be redundant derivations).
8412 if Derive_Subps then
8413 Derive_Subprograms (Parent_Type, Derived_Type);
8414 end if;
8416 -- For tagged types the Discriminant_Constraint of the new base itype
8417 -- is inherited from the first subtype so that no subtype conformance
8418 -- problem arise when the first subtype overrides primitive
8419 -- operations inherited by the implicit base type.
8421 if Is_Tagged then
8422 Set_Discriminant_Constraint
8423 (New_Base, Discriminant_Constraint (Derived_Type));
8424 end if;
8426 return;
8427 end if;
8429 -- If we get here Derived_Type will have no discriminants or it will be
8430 -- a discriminated unconstrained base type.
8432 -- STEP 1a: perform preliminary actions/checks for derived tagged types
8434 if Is_Tagged then
8436 -- The parent type is frozen for non-private extensions (RM 13.14(7))
8437 -- The declaration of a specific descendant of an interface type
8438 -- freezes the interface type (RM 13.14).
8440 if not Private_Extension or else Is_Interface (Parent_Base) then
8441 Freeze_Before (N, Parent_Type);
8442 end if;
8444 -- In Ada 2005 (AI-344), the restriction that a derived tagged type
8445 -- cannot be declared at a deeper level than its parent type is
8446 -- removed. The check on derivation within a generic body is also
8447 -- relaxed, but there's a restriction that a derived tagged type
8448 -- cannot be declared in a generic body if it's derived directly
8449 -- or indirectly from a formal type of that generic.
8451 if Ada_Version >= Ada_2005 then
8452 if Present (Enclosing_Generic_Body (Derived_Type)) then
8453 declare
8454 Ancestor_Type : Entity_Id;
8456 begin
8457 -- Check to see if any ancestor of the derived type is a
8458 -- formal type.
8460 Ancestor_Type := Parent_Type;
8461 while not Is_Generic_Type (Ancestor_Type)
8462 and then Etype (Ancestor_Type) /= Ancestor_Type
8463 loop
8464 Ancestor_Type := Etype (Ancestor_Type);
8465 end loop;
8467 -- If the derived type does have a formal type as an
8468 -- ancestor, then it's an error if the derived type is
8469 -- declared within the body of the generic unit that
8470 -- declares the formal type in its generic formal part. It's
8471 -- sufficient to check whether the ancestor type is declared
8472 -- inside the same generic body as the derived type (such as
8473 -- within a nested generic spec), in which case the
8474 -- derivation is legal. If the formal type is declared
8475 -- outside of that generic body, then it's guaranteed that
8476 -- the derived type is declared within the generic body of
8477 -- the generic unit declaring the formal type.
8479 if Is_Generic_Type (Ancestor_Type)
8480 and then Enclosing_Generic_Body (Ancestor_Type) /=
8481 Enclosing_Generic_Body (Derived_Type)
8482 then
8483 Error_Msg_NE
8484 ("parent type of& must not be descendant of formal type"
8485 & " of an enclosing generic body",
8486 Indic, Derived_Type);
8487 end if;
8488 end;
8489 end if;
8491 elsif Type_Access_Level (Derived_Type) /=
8492 Type_Access_Level (Parent_Type)
8493 and then not Is_Generic_Type (Derived_Type)
8494 then
8495 if Is_Controlled (Parent_Type) then
8496 Error_Msg_N
8497 ("controlled type must be declared at the library level",
8498 Indic);
8499 else
8500 Error_Msg_N
8501 ("type extension at deeper accessibility level than parent",
8502 Indic);
8503 end if;
8505 else
8506 declare
8507 GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
8508 begin
8509 if Present (GB)
8510 and then GB /= Enclosing_Generic_Body (Parent_Base)
8511 then
8512 Error_Msg_NE
8513 ("parent type of& must not be outside generic body"
8514 & " (RM 3.9.1(4))",
8515 Indic, Derived_Type);
8516 end if;
8517 end;
8518 end if;
8519 end if;
8521 -- Ada 2005 (AI-251)
8523 if Ada_Version >= Ada_2005 and then Is_Tagged then
8525 -- "The declaration of a specific descendant of an interface type
8526 -- freezes the interface type" (RM 13.14).
8528 declare
8529 Iface : Node_Id;
8530 begin
8531 if Is_Non_Empty_List (Interface_List (Type_Def)) then
8532 Iface := First (Interface_List (Type_Def));
8533 while Present (Iface) loop
8534 Freeze_Before (N, Etype (Iface));
8535 Next (Iface);
8536 end loop;
8537 end if;
8538 end;
8539 end if;
8541 -- STEP 1b : preliminary cleanup of the full view of private types
8543 -- If the type is already marked as having discriminants, then it's the
8544 -- completion of a private type or private extension and we need to
8545 -- retain the discriminants from the partial view if the current
8546 -- declaration has Discriminant_Specifications so that we can verify
8547 -- conformance. However, we must remove any existing components that
8548 -- were inherited from the parent (and attached in Copy_And_Swap)
8549 -- because the full type inherits all appropriate components anyway, and
8550 -- we do not want the partial view's components interfering.
8552 if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
8553 Discrim := First_Discriminant (Derived_Type);
8554 loop
8555 Last_Discrim := Discrim;
8556 Next_Discriminant (Discrim);
8557 exit when No (Discrim);
8558 end loop;
8560 Set_Last_Entity (Derived_Type, Last_Discrim);
8562 -- In all other cases wipe out the list of inherited components (even
8563 -- inherited discriminants), it will be properly rebuilt here.
8565 else
8566 Set_First_Entity (Derived_Type, Empty);
8567 Set_Last_Entity (Derived_Type, Empty);
8568 end if;
8570 -- STEP 1c: Initialize some flags for the Derived_Type
8572 -- The following flags must be initialized here so that
8573 -- Process_Discriminants can check that discriminants of tagged types do
8574 -- not have a default initial value and that access discriminants are
8575 -- only specified for limited records. For completeness, these flags are
8576 -- also initialized along with all the other flags below.
8578 -- AI-419: Limitedness is not inherited from an interface parent, so to
8579 -- be limited in that case the type must be explicitly declared as
8580 -- limited. However, task and protected interfaces are always limited.
8582 if Limited_Present (Type_Def) then
8583 Set_Is_Limited_Record (Derived_Type);
8585 elsif Is_Limited_Record (Parent_Type)
8586 or else (Present (Full_View (Parent_Type))
8587 and then Is_Limited_Record (Full_View (Parent_Type)))
8588 then
8589 if not Is_Interface (Parent_Type)
8590 or else Is_Synchronized_Interface (Parent_Type)
8591 or else Is_Protected_Interface (Parent_Type)
8592 or else Is_Task_Interface (Parent_Type)
8593 then
8594 Set_Is_Limited_Record (Derived_Type);
8595 end if;
8596 end if;
8598 -- STEP 2a: process discriminants of derived type if any
8600 Push_Scope (Derived_Type);
8602 if Discriminant_Specs then
8603 Set_Has_Unknown_Discriminants (Derived_Type, False);
8605 -- The following call initializes fields Has_Discriminants and
8606 -- Discriminant_Constraint, unless we are processing the completion
8607 -- of a private type declaration.
8609 Check_Or_Process_Discriminants (N, Derived_Type);
8611 -- For untagged types, the constraint on the Parent_Type must be
8612 -- present and is used to rename the discriminants.
8614 if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
8615 Error_Msg_N ("untagged parent must have discriminants", Indic);
8617 elsif not Is_Tagged and then not Constraint_Present then
8618 Error_Msg_N
8619 ("discriminant constraint needed for derived untagged records",
8620 Indic);
8622 -- Otherwise the parent subtype must be constrained unless we have a
8623 -- private extension.
8625 elsif not Constraint_Present
8626 and then not Private_Extension
8627 and then not Is_Constrained (Parent_Type)
8628 then
8629 Error_Msg_N
8630 ("unconstrained type not allowed in this context", Indic);
8632 elsif Constraint_Present then
8633 -- The following call sets the field Corresponding_Discriminant
8634 -- for the discriminants in the Derived_Type.
8636 Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
8638 -- For untagged types all new discriminants must rename
8639 -- discriminants in the parent. For private extensions new
8640 -- discriminants cannot rename old ones (implied by [7.3(13)]).
8642 Discrim := First_Discriminant (Derived_Type);
8643 while Present (Discrim) loop
8644 if not Is_Tagged
8645 and then No (Corresponding_Discriminant (Discrim))
8646 then
8647 Error_Msg_N
8648 ("new discriminants must constrain old ones", Discrim);
8650 elsif Private_Extension
8651 and then Present (Corresponding_Discriminant (Discrim))
8652 then
8653 Error_Msg_N
8654 ("only static constraints allowed for parent"
8655 & " discriminants in the partial view", Indic);
8656 exit;
8657 end if;
8659 -- If a new discriminant is used in the constraint, then its
8660 -- subtype must be statically compatible with the parent
8661 -- discriminant's subtype (3.7(15)).
8663 -- However, if the record contains an array constrained by
8664 -- the discriminant but with some different bound, the compiler
8665 -- attemps to create a smaller range for the discriminant type.
8666 -- (See exp_ch3.Adjust_Discriminants). In this case, where
8667 -- the discriminant type is a scalar type, the check must use
8668 -- the original discriminant type in the parent declaration.
8670 declare
8671 Corr_Disc : constant Entity_Id :=
8672 Corresponding_Discriminant (Discrim);
8673 Disc_Type : constant Entity_Id := Etype (Discrim);
8674 Corr_Type : Entity_Id;
8676 begin
8677 if Present (Corr_Disc) then
8678 if Is_Scalar_Type (Disc_Type) then
8679 Corr_Type :=
8680 Entity (Discriminant_Type (Parent (Corr_Disc)));
8681 else
8682 Corr_Type := Etype (Corr_Disc);
8683 end if;
8685 if not
8686 Subtypes_Statically_Compatible (Disc_Type, Corr_Type)
8687 then
8688 Error_Msg_N
8689 ("subtype must be compatible "
8690 & "with parent discriminant",
8691 Discrim);
8692 end if;
8693 end if;
8694 end;
8696 Next_Discriminant (Discrim);
8697 end loop;
8699 -- Check whether the constraints of the full view statically
8700 -- match those imposed by the parent subtype [7.3(13)].
8702 if Present (Stored_Constraint (Derived_Type)) then
8703 declare
8704 C1, C2 : Elmt_Id;
8706 begin
8707 C1 := First_Elmt (Discs);
8708 C2 := First_Elmt (Stored_Constraint (Derived_Type));
8709 while Present (C1) and then Present (C2) loop
8710 if not
8711 Fully_Conformant_Expressions (Node (C1), Node (C2))
8712 then
8713 Error_Msg_N
8714 ("not conformant with previous declaration",
8715 Node (C1));
8716 end if;
8718 Next_Elmt (C1);
8719 Next_Elmt (C2);
8720 end loop;
8721 end;
8722 end if;
8723 end if;
8725 -- STEP 2b: No new discriminants, inherit discriminants if any
8727 else
8728 if Private_Extension then
8729 Set_Has_Unknown_Discriminants
8730 (Derived_Type,
8731 Has_Unknown_Discriminants (Parent_Type)
8732 or else Unknown_Discriminants_Present (N));
8734 -- The partial view of the parent may have unknown discriminants,
8735 -- but if the full view has discriminants and the parent type is
8736 -- in scope they must be inherited.
8738 elsif Has_Unknown_Discriminants (Parent_Type)
8739 and then
8740 (not Has_Discriminants (Parent_Type)
8741 or else not In_Open_Scopes (Scope (Parent_Type)))
8742 then
8743 Set_Has_Unknown_Discriminants (Derived_Type);
8744 end if;
8746 if not Has_Unknown_Discriminants (Derived_Type)
8747 and then not Has_Unknown_Discriminants (Parent_Base)
8748 and then Has_Discriminants (Parent_Type)
8749 then
8750 Inherit_Discrims := True;
8751 Set_Has_Discriminants
8752 (Derived_Type, True);
8753 Set_Discriminant_Constraint
8754 (Derived_Type, Discriminant_Constraint (Parent_Base));
8755 end if;
8757 -- The following test is true for private types (remember
8758 -- transformation 5. is not applied to those) and in an error
8759 -- situation.
8761 if Constraint_Present then
8762 Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
8763 end if;
8765 -- For now mark a new derived type as constrained only if it has no
8766 -- discriminants. At the end of Build_Derived_Record_Type we properly
8767 -- set this flag in the case of private extensions. See comments in
8768 -- point 9. just before body of Build_Derived_Record_Type.
8770 Set_Is_Constrained
8771 (Derived_Type,
8772 not (Inherit_Discrims
8773 or else Has_Unknown_Discriminants (Derived_Type)));
8774 end if;
8776 -- STEP 3: initialize fields of derived type
8778 Set_Is_Tagged_Type (Derived_Type, Is_Tagged);
8779 Set_Stored_Constraint (Derived_Type, No_Elist);
8781 -- Ada 2005 (AI-251): Private type-declarations can implement interfaces
8782 -- but cannot be interfaces
8784 if not Private_Extension
8785 and then Ekind (Derived_Type) /= E_Private_Type
8786 and then Ekind (Derived_Type) /= E_Limited_Private_Type
8787 then
8788 if Interface_Present (Type_Def) then
8789 Analyze_Interface_Declaration (Derived_Type, Type_Def);
8790 end if;
8792 Set_Interfaces (Derived_Type, No_Elist);
8793 end if;
8795 -- Fields inherited from the Parent_Type
8797 Set_Has_Specified_Layout
8798 (Derived_Type, Has_Specified_Layout (Parent_Type));
8799 Set_Is_Limited_Composite
8800 (Derived_Type, Is_Limited_Composite (Parent_Type));
8801 Set_Is_Private_Composite
8802 (Derived_Type, Is_Private_Composite (Parent_Type));
8804 if Is_Tagged_Type (Parent_Type) then
8805 Set_No_Tagged_Streams_Pragma
8806 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
8807 end if;
8809 -- Fields inherited from the Parent_Base
8811 Set_Has_Controlled_Component
8812 (Derived_Type, Has_Controlled_Component (Parent_Base));
8813 Set_Has_Non_Standard_Rep
8814 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
8815 Set_Has_Primitive_Operations
8816 (Derived_Type, Has_Primitive_Operations (Parent_Base));
8818 -- Fields inherited from the Parent_Base in the non-private case
8820 if Ekind (Derived_Type) = E_Record_Type then
8821 Set_Has_Complex_Representation
8822 (Derived_Type, Has_Complex_Representation (Parent_Base));
8823 end if;
8825 -- Fields inherited from the Parent_Base for record types
8827 if Is_Record_Type (Derived_Type) then
8828 declare
8829 Parent_Full : Entity_Id;
8831 begin
8832 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
8833 -- Parent_Base can be a private type or private extension. Go
8834 -- to the full view here to get the E_Record_Type specific flags.
8836 if Present (Full_View (Parent_Base)) then
8837 Parent_Full := Full_View (Parent_Base);
8838 else
8839 Parent_Full := Parent_Base;
8840 end if;
8842 Set_OK_To_Reorder_Components
8843 (Derived_Type, OK_To_Reorder_Components (Parent_Full));
8844 end;
8845 end if;
8847 -- Set fields for private derived types
8849 if Is_Private_Type (Derived_Type) then
8850 Set_Depends_On_Private (Derived_Type, True);
8851 Set_Private_Dependents (Derived_Type, New_Elmt_List);
8853 -- Inherit fields from non private record types. If this is the
8854 -- completion of a derivation from a private type, the parent itself
8855 -- is private, and the attributes come from its full view, which must
8856 -- be present.
8858 else
8859 if Is_Private_Type (Parent_Base)
8860 and then not Is_Record_Type (Parent_Base)
8861 then
8862 Set_Component_Alignment
8863 (Derived_Type, Component_Alignment (Full_View (Parent_Base)));
8864 Set_C_Pass_By_Copy
8865 (Derived_Type, C_Pass_By_Copy (Full_View (Parent_Base)));
8866 else
8867 Set_Component_Alignment
8868 (Derived_Type, Component_Alignment (Parent_Base));
8869 Set_C_Pass_By_Copy
8870 (Derived_Type, C_Pass_By_Copy (Parent_Base));
8871 end if;
8872 end if;
8874 -- Set fields for tagged types
8876 if Is_Tagged then
8877 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
8879 -- All tagged types defined in Ada.Finalization are controlled
8881 if Chars (Scope (Derived_Type)) = Name_Finalization
8882 and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
8883 and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
8884 then
8885 Set_Is_Controlled (Derived_Type);
8886 else
8887 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Base));
8888 end if;
8890 -- Minor optimization: there is no need to generate the class-wide
8891 -- entity associated with an underlying record view.
8893 if not Is_Underlying_Record_View (Derived_Type) then
8894 Make_Class_Wide_Type (Derived_Type);
8895 end if;
8897 Set_Is_Abstract_Type (Derived_Type, Abstract_Present (Type_Def));
8899 if Has_Discriminants (Derived_Type)
8900 and then Constraint_Present
8901 then
8902 Set_Stored_Constraint
8903 (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
8904 end if;
8906 if Ada_Version >= Ada_2005 then
8907 declare
8908 Ifaces_List : Elist_Id;
8910 begin
8911 -- Checks rules 3.9.4 (13/2 and 14/2)
8913 if Comes_From_Source (Derived_Type)
8914 and then not Is_Private_Type (Derived_Type)
8915 and then Is_Interface (Parent_Type)
8916 and then not Is_Interface (Derived_Type)
8917 then
8918 if Is_Task_Interface (Parent_Type) then
8919 Error_Msg_N
8920 ("(Ada 2005) task type required (RM 3.9.4 (13.2))",
8921 Derived_Type);
8923 elsif Is_Protected_Interface (Parent_Type) then
8924 Error_Msg_N
8925 ("(Ada 2005) protected type required (RM 3.9.4 (14.2))",
8926 Derived_Type);
8927 end if;
8928 end if;
8930 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
8932 Check_Interfaces (N, Type_Def);
8934 -- Ada 2005 (AI-251): Collect the list of progenitors that are
8935 -- not already in the parents.
8937 Collect_Interfaces
8938 (T => Derived_Type,
8939 Ifaces_List => Ifaces_List,
8940 Exclude_Parents => True);
8942 Set_Interfaces (Derived_Type, Ifaces_List);
8944 -- If the derived type is the anonymous type created for
8945 -- a declaration whose parent has a constraint, propagate
8946 -- the interface list to the source type. This must be done
8947 -- prior to the completion of the analysis of the source type
8948 -- because the components in the extension may contain current
8949 -- instances whose legality depends on some ancestor.
8951 if Is_Itype (Derived_Type) then
8952 declare
8953 Def : constant Node_Id :=
8954 Associated_Node_For_Itype (Derived_Type);
8955 begin
8956 if Present (Def)
8957 and then Nkind (Def) = N_Full_Type_Declaration
8958 then
8959 Set_Interfaces
8960 (Defining_Identifier (Def), Ifaces_List);
8961 end if;
8962 end;
8963 end if;
8965 -- A type extension is automatically Ghost when one of its
8966 -- progenitors is Ghost (SPARK RM 6.9(9)). This property is
8967 -- also inherited when the parent type is Ghost, but this is
8968 -- done in Build_Derived_Type as the mechanism also handles
8969 -- untagged derivations.
8971 if Implements_Ghost_Interface (Derived_Type) then
8972 Set_Is_Ghost_Entity (Derived_Type);
8973 end if;
8974 end;
8975 end if;
8977 else
8978 Set_Is_Packed (Derived_Type, Is_Packed (Parent_Base));
8979 Set_Has_Non_Standard_Rep
8980 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
8981 end if;
8983 -- STEP 4: Inherit components from the parent base and constrain them.
8984 -- Apply the second transformation described in point 6. above.
8986 if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
8987 or else not Has_Discriminants (Parent_Type)
8988 or else not Is_Constrained (Parent_Type)
8989 then
8990 Constrs := Discs;
8991 else
8992 Constrs := Discriminant_Constraint (Parent_Type);
8993 end if;
8995 Assoc_List :=
8996 Inherit_Components
8997 (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
8999 -- STEP 5a: Copy the parent record declaration for untagged types
9001 Set_Has_Implicit_Dereference
9002 (Derived_Type, Has_Implicit_Dereference (Parent_Type));
9004 if not Is_Tagged then
9006 -- Discriminant_Constraint (Derived_Type) has been properly
9007 -- constructed. Save it and temporarily set it to Empty because we
9008 -- do not want the call to New_Copy_Tree below to mess this list.
9010 if Has_Discriminants (Derived_Type) then
9011 Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
9012 Set_Discriminant_Constraint (Derived_Type, No_Elist);
9013 else
9014 Save_Discr_Constr := No_Elist;
9015 end if;
9017 -- Save the Etype field of Derived_Type. It is correctly set now,
9018 -- but the call to New_Copy tree may remap it to point to itself,
9019 -- which is not what we want. Ditto for the Next_Entity field.
9021 Save_Etype := Etype (Derived_Type);
9022 Save_Next_Entity := Next_Entity (Derived_Type);
9024 -- Assoc_List maps all stored discriminants in the Parent_Base to
9025 -- stored discriminants in the Derived_Type. It is fundamental that
9026 -- no types or itypes with discriminants other than the stored
9027 -- discriminants appear in the entities declared inside
9028 -- Derived_Type, since the back end cannot deal with it.
9030 New_Decl :=
9031 New_Copy_Tree
9032 (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
9034 -- Restore the fields saved prior to the New_Copy_Tree call
9035 -- and compute the stored constraint.
9037 Set_Etype (Derived_Type, Save_Etype);
9038 Set_Next_Entity (Derived_Type, Save_Next_Entity);
9040 if Has_Discriminants (Derived_Type) then
9041 Set_Discriminant_Constraint
9042 (Derived_Type, Save_Discr_Constr);
9043 Set_Stored_Constraint
9044 (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
9045 Replace_Components (Derived_Type, New_Decl);
9046 end if;
9048 -- Insert the new derived type declaration
9050 Rewrite (N, New_Decl);
9052 -- STEP 5b: Complete the processing for record extensions in generics
9054 -- There is no completion for record extensions declared in the
9055 -- parameter part of a generic, so we need to complete processing for
9056 -- these generic record extensions here. The Record_Type_Definition call
9057 -- will change the Ekind of the components from E_Void to E_Component.
9059 elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
9060 Record_Type_Definition (Empty, Derived_Type);
9062 -- STEP 5c: Process the record extension for non private tagged types
9064 elsif not Private_Extension then
9065 Expand_Record_Extension (Derived_Type, Type_Def);
9067 -- Note : previously in ASIS mode we set the Parent_Subtype of the
9068 -- derived type to propagate some semantic information. This led
9069 -- to other ASIS failures and has been removed.
9071 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
9072 -- implemented interfaces if we are in expansion mode
9074 if Expander_Active
9075 and then Has_Interfaces (Derived_Type)
9076 then
9077 Add_Interface_Tag_Components (N, Derived_Type);
9078 end if;
9080 -- Analyze the record extension
9082 Record_Type_Definition
9083 (Record_Extension_Part (Type_Def), Derived_Type);
9084 end if;
9086 End_Scope;
9088 -- Nothing else to do if there is an error in the derivation.
9089 -- An unusual case: the full view may be derived from a type in an
9090 -- instance, when the partial view was used illegally as an actual
9091 -- in that instance, leading to a circular definition.
9093 if Etype (Derived_Type) = Any_Type
9094 or else Etype (Parent_Type) = Derived_Type
9095 then
9096 return;
9097 end if;
9099 -- Set delayed freeze and then derive subprograms, we need to do
9100 -- this in this order so that derived subprograms inherit the
9101 -- derived freeze if necessary.
9103 Set_Has_Delayed_Freeze (Derived_Type);
9105 if Derive_Subps then
9106 Derive_Subprograms (Parent_Type, Derived_Type);
9107 end if;
9109 -- If we have a private extension which defines a constrained derived
9110 -- type mark as constrained here after we have derived subprograms. See
9111 -- comment on point 9. just above the body of Build_Derived_Record_Type.
9113 if Private_Extension and then Inherit_Discrims then
9114 if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
9115 Set_Is_Constrained (Derived_Type, True);
9116 Set_Discriminant_Constraint (Derived_Type, Discs);
9118 elsif Is_Constrained (Parent_Type) then
9119 Set_Is_Constrained
9120 (Derived_Type, True);
9121 Set_Discriminant_Constraint
9122 (Derived_Type, Discriminant_Constraint (Parent_Type));
9123 end if;
9124 end if;
9126 -- Update the class-wide type, which shares the now-completed entity
9127 -- list with its specific type. In case of underlying record views,
9128 -- we do not generate the corresponding class wide entity.
9130 if Is_Tagged
9131 and then not Is_Underlying_Record_View (Derived_Type)
9132 then
9133 Set_First_Entity
9134 (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
9135 Set_Last_Entity
9136 (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
9137 end if;
9139 Check_Function_Writable_Actuals (N);
9140 end Build_Derived_Record_Type;
9142 ------------------------
9143 -- Build_Derived_Type --
9144 ------------------------
9146 procedure Build_Derived_Type
9147 (N : Node_Id;
9148 Parent_Type : Entity_Id;
9149 Derived_Type : Entity_Id;
9150 Is_Completion : Boolean;
9151 Derive_Subps : Boolean := True)
9153 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
9155 begin
9156 -- Set common attributes
9158 Set_Scope (Derived_Type, Current_Scope);
9160 Set_Etype (Derived_Type, Parent_Base);
9161 Set_Ekind (Derived_Type, Ekind (Parent_Base));
9162 Propagate_Concurrent_Flags (Derived_Type, Parent_Base);
9164 Set_Size_Info (Derived_Type, Parent_Type);
9165 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
9166 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
9167 Set_Disable_Controlled (Derived_Type, Disable_Controlled (Parent_Type));
9169 Set_Is_Tagged_Type (Derived_Type, Is_Tagged_Type (Parent_Type));
9170 Set_Is_Volatile (Derived_Type, Is_Volatile (Parent_Type));
9172 if Is_Tagged_Type (Derived_Type) then
9173 Set_No_Tagged_Streams_Pragma
9174 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
9175 end if;
9177 -- If the parent has primitive routines, set the derived type link
9179 if Has_Primitive_Operations (Parent_Type) then
9180 Set_Derived_Type_Link (Parent_Base, Derived_Type);
9181 end if;
9183 -- If the parent type is a private subtype, the convention on the base
9184 -- type may be set in the private part, and not propagated to the
9185 -- subtype until later, so we obtain the convention from the base type.
9187 Set_Convention (Derived_Type, Convention (Parent_Base));
9189 -- Set SSO default for record or array type
9191 if (Is_Array_Type (Derived_Type) or else Is_Record_Type (Derived_Type))
9192 and then Is_Base_Type (Derived_Type)
9193 then
9194 Set_Default_SSO (Derived_Type);
9195 end if;
9197 -- A derived type inherits the Default_Initial_Condition pragma coming
9198 -- from any parent type within the derivation chain.
9200 if Has_DIC (Parent_Type) then
9201 Set_Has_Inherited_DIC (Derived_Type);
9202 end if;
9204 -- A derived type inherits any class-wide invariants coming from a
9205 -- parent type or an interface. Note that the invariant procedure of
9206 -- the parent type should not be inherited because the derived type may
9207 -- define invariants of its own.
9209 if not Is_Interface (Derived_Type) then
9210 if Has_Inherited_Invariants (Parent_Type)
9211 or else Has_Inheritable_Invariants (Parent_Type)
9212 then
9213 Set_Has_Inherited_Invariants (Derived_Type);
9215 elsif Is_Concurrent_Type (Derived_Type)
9216 or else Is_Tagged_Type (Derived_Type)
9217 then
9218 declare
9219 Iface : Entity_Id;
9220 Ifaces : Elist_Id;
9221 Iface_Elmt : Elmt_Id;
9223 begin
9224 Collect_Interfaces
9225 (T => Derived_Type,
9226 Ifaces_List => Ifaces,
9227 Exclude_Parents => True);
9229 if Present (Ifaces) then
9230 Iface_Elmt := First_Elmt (Ifaces);
9231 while Present (Iface_Elmt) loop
9232 Iface := Node (Iface_Elmt);
9234 if Has_Inheritable_Invariants (Iface) then
9235 Set_Has_Inherited_Invariants (Derived_Type);
9236 exit;
9237 end if;
9239 Next_Elmt (Iface_Elmt);
9240 end loop;
9241 end if;
9242 end;
9243 end if;
9244 end if;
9246 -- We similarly inherit predicates. Note that for scalar derived types
9247 -- the predicate is inherited from the first subtype, and not from its
9248 -- (anonymous) base type.
9250 if Has_Predicates (Parent_Type)
9251 or else Has_Predicates (First_Subtype (Parent_Type))
9252 then
9253 Set_Has_Predicates (Derived_Type);
9254 end if;
9256 -- The derived type inherits the representation clauses of the parent
9258 Inherit_Rep_Item_Chain (Derived_Type, Parent_Type);
9260 -- If the parent type has delayed rep aspects, then mark the derived
9261 -- type as possibly inheriting a delayed rep aspect.
9263 if Has_Delayed_Rep_Aspects (Parent_Type) then
9264 Set_May_Inherit_Delayed_Rep_Aspects (Derived_Type);
9265 end if;
9267 -- A derived type becomes Ghost when its parent type is also Ghost
9268 -- (SPARK RM 6.9(9)). Note that the Ghost-related attributes are not
9269 -- directly inherited because the Ghost policy in effect may differ.
9271 if Is_Ghost_Entity (Parent_Type) then
9272 Set_Is_Ghost_Entity (Derived_Type);
9273 end if;
9275 -- Type dependent processing
9277 case Ekind (Parent_Type) is
9278 when Numeric_Kind =>
9279 Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
9281 when Array_Kind =>
9282 Build_Derived_Array_Type (N, Parent_Type, Derived_Type);
9284 when Class_Wide_Kind
9285 | E_Record_Subtype
9286 | E_Record_Type
9288 Build_Derived_Record_Type
9289 (N, Parent_Type, Derived_Type, Derive_Subps);
9290 return;
9292 when Enumeration_Kind =>
9293 Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
9295 when Access_Kind =>
9296 Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
9298 when Incomplete_Or_Private_Kind =>
9299 Build_Derived_Private_Type
9300 (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
9302 -- For discriminated types, the derivation includes deriving
9303 -- primitive operations. For others it is done below.
9305 if Is_Tagged_Type (Parent_Type)
9306 or else Has_Discriminants (Parent_Type)
9307 or else (Present (Full_View (Parent_Type))
9308 and then Has_Discriminants (Full_View (Parent_Type)))
9309 then
9310 return;
9311 end if;
9313 when Concurrent_Kind =>
9314 Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
9316 when others =>
9317 raise Program_Error;
9318 end case;
9320 -- Nothing more to do if some error occurred
9322 if Etype (Derived_Type) = Any_Type then
9323 return;
9324 end if;
9326 -- Set delayed freeze and then derive subprograms, we need to do this
9327 -- in this order so that derived subprograms inherit the derived freeze
9328 -- if necessary.
9330 Set_Has_Delayed_Freeze (Derived_Type);
9332 if Derive_Subps then
9333 Derive_Subprograms (Parent_Type, Derived_Type);
9334 end if;
9336 Set_Has_Primitive_Operations
9337 (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
9338 end Build_Derived_Type;
9340 -----------------------
9341 -- Build_Discriminal --
9342 -----------------------
9344 procedure Build_Discriminal (Discrim : Entity_Id) is
9345 D_Minal : Entity_Id;
9346 CR_Disc : Entity_Id;
9348 begin
9349 -- A discriminal has the same name as the discriminant
9351 D_Minal := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9353 Set_Ekind (D_Minal, E_In_Parameter);
9354 Set_Mechanism (D_Minal, Default_Mechanism);
9355 Set_Etype (D_Minal, Etype (Discrim));
9356 Set_Scope (D_Minal, Current_Scope);
9357 Set_Parent (D_Minal, Parent (Discrim));
9359 Set_Discriminal (Discrim, D_Minal);
9360 Set_Discriminal_Link (D_Minal, Discrim);
9362 -- For task types, build at once the discriminants of the corresponding
9363 -- record, which are needed if discriminants are used in entry defaults
9364 -- and in family bounds.
9366 if Is_Concurrent_Type (Current_Scope)
9367 or else
9368 Is_Limited_Type (Current_Scope)
9369 then
9370 CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9372 Set_Ekind (CR_Disc, E_In_Parameter);
9373 Set_Mechanism (CR_Disc, Default_Mechanism);
9374 Set_Etype (CR_Disc, Etype (Discrim));
9375 Set_Scope (CR_Disc, Current_Scope);
9376 Set_Discriminal_Link (CR_Disc, Discrim);
9377 Set_CR_Discriminant (Discrim, CR_Disc);
9378 end if;
9379 end Build_Discriminal;
9381 ------------------------------------
9382 -- Build_Discriminant_Constraints --
9383 ------------------------------------
9385 function Build_Discriminant_Constraints
9386 (T : Entity_Id;
9387 Def : Node_Id;
9388 Derived_Def : Boolean := False) return Elist_Id
9390 C : constant Node_Id := Constraint (Def);
9391 Nb_Discr : constant Nat := Number_Discriminants (T);
9393 Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
9394 -- Saves the expression corresponding to a given discriminant in T
9396 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
9397 -- Return the Position number within array Discr_Expr of a discriminant
9398 -- D within the discriminant list of the discriminated type T.
9400 procedure Process_Discriminant_Expression
9401 (Expr : Node_Id;
9402 D : Entity_Id);
9403 -- If this is a discriminant constraint on a partial view, do not
9404 -- generate an overflow check on the discriminant expression. The check
9405 -- will be generated when constraining the full view. Otherwise the
9406 -- backend creates duplicate symbols for the temporaries corresponding
9407 -- to the expressions to be checked, causing spurious assembler errors.
9409 ------------------
9410 -- Pos_Of_Discr --
9411 ------------------
9413 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
9414 Disc : Entity_Id;
9416 begin
9417 Disc := First_Discriminant (T);
9418 for J in Discr_Expr'Range loop
9419 if Disc = D then
9420 return J;
9421 end if;
9423 Next_Discriminant (Disc);
9424 end loop;
9426 -- Note: Since this function is called on discriminants that are
9427 -- known to belong to the discriminated type, falling through the
9428 -- loop with no match signals an internal compiler error.
9430 raise Program_Error;
9431 end Pos_Of_Discr;
9433 -------------------------------------
9434 -- Process_Discriminant_Expression --
9435 -------------------------------------
9437 procedure Process_Discriminant_Expression
9438 (Expr : Node_Id;
9439 D : Entity_Id)
9441 BDT : constant Entity_Id := Base_Type (Etype (D));
9443 begin
9444 -- If this is a discriminant constraint on a partial view, do
9445 -- not generate an overflow on the discriminant expression. The
9446 -- check will be generated when constraining the full view.
9448 if Is_Private_Type (T)
9449 and then Present (Full_View (T))
9450 then
9451 Analyze_And_Resolve (Expr, BDT, Suppress => Overflow_Check);
9452 else
9453 Analyze_And_Resolve (Expr, BDT);
9454 end if;
9455 end Process_Discriminant_Expression;
9457 -- Declarations local to Build_Discriminant_Constraints
9459 Discr : Entity_Id;
9460 E : Entity_Id;
9461 Elist : constant Elist_Id := New_Elmt_List;
9463 Constr : Node_Id;
9464 Expr : Node_Id;
9465 Id : Node_Id;
9466 Position : Nat;
9467 Found : Boolean;
9469 Discrim_Present : Boolean := False;
9471 -- Start of processing for Build_Discriminant_Constraints
9473 begin
9474 -- The following loop will process positional associations only.
9475 -- For a positional association, the (single) discriminant is
9476 -- implicitly specified by position, in textual order (RM 3.7.2).
9478 Discr := First_Discriminant (T);
9479 Constr := First (Constraints (C));
9480 for D in Discr_Expr'Range loop
9481 exit when Nkind (Constr) = N_Discriminant_Association;
9483 if No (Constr) then
9484 Error_Msg_N ("too few discriminants given in constraint", C);
9485 return New_Elmt_List;
9487 elsif Nkind (Constr) = N_Range
9488 or else (Nkind (Constr) = N_Attribute_Reference
9489 and then Attribute_Name (Constr) = Name_Range)
9490 then
9491 Error_Msg_N
9492 ("a range is not a valid discriminant constraint", Constr);
9493 Discr_Expr (D) := Error;
9495 else
9496 Process_Discriminant_Expression (Constr, Discr);
9497 Discr_Expr (D) := Constr;
9498 end if;
9500 Next_Discriminant (Discr);
9501 Next (Constr);
9502 end loop;
9504 if No (Discr) and then Present (Constr) then
9505 Error_Msg_N ("too many discriminants given in constraint", Constr);
9506 return New_Elmt_List;
9507 end if;
9509 -- Named associations can be given in any order, but if both positional
9510 -- and named associations are used in the same discriminant constraint,
9511 -- then positional associations must occur first, at their normal
9512 -- position. Hence once a named association is used, the rest of the
9513 -- discriminant constraint must use only named associations.
9515 while Present (Constr) loop
9517 -- Positional association forbidden after a named association
9519 if Nkind (Constr) /= N_Discriminant_Association then
9520 Error_Msg_N ("positional association follows named one", Constr);
9521 return New_Elmt_List;
9523 -- Otherwise it is a named association
9525 else
9526 -- E records the type of the discriminants in the named
9527 -- association. All the discriminants specified in the same name
9528 -- association must have the same type.
9530 E := Empty;
9532 -- Search the list of discriminants in T to see if the simple name
9533 -- given in the constraint matches any of them.
9535 Id := First (Selector_Names (Constr));
9536 while Present (Id) loop
9537 Found := False;
9539 -- If Original_Discriminant is present, we are processing a
9540 -- generic instantiation and this is an instance node. We need
9541 -- to find the name of the corresponding discriminant in the
9542 -- actual record type T and not the name of the discriminant in
9543 -- the generic formal. Example:
9545 -- generic
9546 -- type G (D : int) is private;
9547 -- package P is
9548 -- subtype W is G (D => 1);
9549 -- end package;
9550 -- type Rec (X : int) is record ... end record;
9551 -- package Q is new P (G => Rec);
9553 -- At the point of the instantiation, formal type G is Rec
9554 -- and therefore when reanalyzing "subtype W is G (D => 1);"
9555 -- which really looks like "subtype W is Rec (D => 1);" at
9556 -- the point of instantiation, we want to find the discriminant
9557 -- that corresponds to D in Rec, i.e. X.
9559 if Present (Original_Discriminant (Id))
9560 and then In_Instance
9561 then
9562 Discr := Find_Corresponding_Discriminant (Id, T);
9563 Found := True;
9565 else
9566 Discr := First_Discriminant (T);
9567 while Present (Discr) loop
9568 if Chars (Discr) = Chars (Id) then
9569 Found := True;
9570 exit;
9571 end if;
9573 Next_Discriminant (Discr);
9574 end loop;
9576 if not Found then
9577 Error_Msg_N ("& does not match any discriminant", Id);
9578 return New_Elmt_List;
9580 -- If the parent type is a generic formal, preserve the
9581 -- name of the discriminant for subsequent instances.
9582 -- see comment at the beginning of this if statement.
9584 elsif Is_Generic_Type (Root_Type (T)) then
9585 Set_Original_Discriminant (Id, Discr);
9586 end if;
9587 end if;
9589 Position := Pos_Of_Discr (T, Discr);
9591 if Present (Discr_Expr (Position)) then
9592 Error_Msg_N ("duplicate constraint for discriminant&", Id);
9594 else
9595 -- Each discriminant specified in the same named association
9596 -- must be associated with a separate copy of the
9597 -- corresponding expression.
9599 if Present (Next (Id)) then
9600 Expr := New_Copy_Tree (Expression (Constr));
9601 Set_Parent (Expr, Parent (Expression (Constr)));
9602 else
9603 Expr := Expression (Constr);
9604 end if;
9606 Discr_Expr (Position) := Expr;
9607 Process_Discriminant_Expression (Expr, Discr);
9608 end if;
9610 -- A discriminant association with more than one discriminant
9611 -- name is only allowed if the named discriminants are all of
9612 -- the same type (RM 3.7.1(8)).
9614 if E = Empty then
9615 E := Base_Type (Etype (Discr));
9617 elsif Base_Type (Etype (Discr)) /= E then
9618 Error_Msg_N
9619 ("all discriminants in an association " &
9620 "must have the same type", Id);
9621 end if;
9623 Next (Id);
9624 end loop;
9625 end if;
9627 Next (Constr);
9628 end loop;
9630 -- A discriminant constraint must provide exactly one value for each
9631 -- discriminant of the type (RM 3.7.1(8)).
9633 for J in Discr_Expr'Range loop
9634 if No (Discr_Expr (J)) then
9635 Error_Msg_N ("too few discriminants given in constraint", C);
9636 return New_Elmt_List;
9637 end if;
9638 end loop;
9640 -- Determine if there are discriminant expressions in the constraint
9642 for J in Discr_Expr'Range loop
9643 if Denotes_Discriminant
9644 (Discr_Expr (J), Check_Concurrent => True)
9645 then
9646 Discrim_Present := True;
9647 end if;
9648 end loop;
9650 -- Build an element list consisting of the expressions given in the
9651 -- discriminant constraint and apply the appropriate checks. The list
9652 -- is constructed after resolving any named discriminant associations
9653 -- and therefore the expressions appear in the textual order of the
9654 -- discriminants.
9656 Discr := First_Discriminant (T);
9657 for J in Discr_Expr'Range loop
9658 if Discr_Expr (J) /= Error then
9659 Append_Elmt (Discr_Expr (J), Elist);
9661 -- If any of the discriminant constraints is given by a
9662 -- discriminant and we are in a derived type declaration we
9663 -- have a discriminant renaming. Establish link between new
9664 -- and old discriminant. The new discriminant has an implicit
9665 -- dereference if the old one does.
9667 if Denotes_Discriminant (Discr_Expr (J)) then
9668 if Derived_Def then
9669 declare
9670 New_Discr : constant Entity_Id := Entity (Discr_Expr (J));
9672 begin
9673 Set_Corresponding_Discriminant (New_Discr, Discr);
9674 Set_Has_Implicit_Dereference (New_Discr,
9675 Has_Implicit_Dereference (Discr));
9676 end;
9677 end if;
9679 -- Force the evaluation of non-discriminant expressions.
9680 -- If we have found a discriminant in the constraint 3.4(26)
9681 -- and 3.8(18) demand that no range checks are performed are
9682 -- after evaluation. If the constraint is for a component
9683 -- definition that has a per-object constraint, expressions are
9684 -- evaluated but not checked either. In all other cases perform
9685 -- a range check.
9687 else
9688 if Discrim_Present then
9689 null;
9691 elsif Nkind (Parent (Parent (Def))) = N_Component_Declaration
9692 and then Has_Per_Object_Constraint
9693 (Defining_Identifier (Parent (Parent (Def))))
9694 then
9695 null;
9697 elsif Is_Access_Type (Etype (Discr)) then
9698 Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
9700 else
9701 Apply_Range_Check (Discr_Expr (J), Etype (Discr));
9702 end if;
9704 Force_Evaluation (Discr_Expr (J));
9705 end if;
9707 -- Check that the designated type of an access discriminant's
9708 -- expression is not a class-wide type unless the discriminant's
9709 -- designated type is also class-wide.
9711 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
9712 and then not Is_Class_Wide_Type
9713 (Designated_Type (Etype (Discr)))
9714 and then Etype (Discr_Expr (J)) /= Any_Type
9715 and then Is_Class_Wide_Type
9716 (Designated_Type (Etype (Discr_Expr (J))))
9717 then
9718 Wrong_Type (Discr_Expr (J), Etype (Discr));
9720 elsif Is_Access_Type (Etype (Discr))
9721 and then not Is_Access_Constant (Etype (Discr))
9722 and then Is_Access_Type (Etype (Discr_Expr (J)))
9723 and then Is_Access_Constant (Etype (Discr_Expr (J)))
9724 then
9725 Error_Msg_NE
9726 ("constraint for discriminant& must be access to variable",
9727 Def, Discr);
9728 end if;
9729 end if;
9731 Next_Discriminant (Discr);
9732 end loop;
9734 return Elist;
9735 end Build_Discriminant_Constraints;
9737 ---------------------------------
9738 -- Build_Discriminated_Subtype --
9739 ---------------------------------
9741 procedure Build_Discriminated_Subtype
9742 (T : Entity_Id;
9743 Def_Id : Entity_Id;
9744 Elist : Elist_Id;
9745 Related_Nod : Node_Id;
9746 For_Access : Boolean := False)
9748 Has_Discrs : constant Boolean := Has_Discriminants (T);
9749 Constrained : constant Boolean :=
9750 (Has_Discrs
9751 and then not Is_Empty_Elmt_List (Elist)
9752 and then not Is_Class_Wide_Type (T))
9753 or else Is_Constrained (T);
9755 begin
9756 if Ekind (T) = E_Record_Type then
9757 if For_Access then
9758 Set_Ekind (Def_Id, E_Private_Subtype);
9759 Set_Is_For_Access_Subtype (Def_Id, True);
9760 else
9761 Set_Ekind (Def_Id, E_Record_Subtype);
9762 end if;
9764 -- Inherit preelaboration flag from base, for types for which it
9765 -- may have been set: records, private types, protected types.
9767 Set_Known_To_Have_Preelab_Init
9768 (Def_Id, Known_To_Have_Preelab_Init (T));
9770 elsif Ekind (T) = E_Task_Type then
9771 Set_Ekind (Def_Id, E_Task_Subtype);
9773 elsif Ekind (T) = E_Protected_Type then
9774 Set_Ekind (Def_Id, E_Protected_Subtype);
9775 Set_Known_To_Have_Preelab_Init
9776 (Def_Id, Known_To_Have_Preelab_Init (T));
9778 elsif Is_Private_Type (T) then
9779 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
9780 Set_Known_To_Have_Preelab_Init
9781 (Def_Id, Known_To_Have_Preelab_Init (T));
9783 -- Private subtypes may have private dependents
9785 Set_Private_Dependents (Def_Id, New_Elmt_List);
9787 elsif Is_Class_Wide_Type (T) then
9788 Set_Ekind (Def_Id, E_Class_Wide_Subtype);
9790 else
9791 -- Incomplete type. Attach subtype to list of dependents, to be
9792 -- completed with full view of parent type, unless is it the
9793 -- designated subtype of a record component within an init_proc.
9794 -- This last case arises for a component of an access type whose
9795 -- designated type is incomplete (e.g. a Taft Amendment type).
9796 -- The designated subtype is within an inner scope, and needs no
9797 -- elaboration, because only the access type is needed in the
9798 -- initialization procedure.
9800 Set_Ekind (Def_Id, Ekind (T));
9802 if For_Access and then Within_Init_Proc then
9803 null;
9804 else
9805 Append_Elmt (Def_Id, Private_Dependents (T));
9806 end if;
9807 end if;
9809 Set_Etype (Def_Id, T);
9810 Init_Size_Align (Def_Id);
9811 Set_Has_Discriminants (Def_Id, Has_Discrs);
9812 Set_Is_Constrained (Def_Id, Constrained);
9814 Set_First_Entity (Def_Id, First_Entity (T));
9815 Set_Last_Entity (Def_Id, Last_Entity (T));
9816 Set_Has_Implicit_Dereference
9817 (Def_Id, Has_Implicit_Dereference (T));
9819 -- If the subtype is the completion of a private declaration, there may
9820 -- have been representation clauses for the partial view, and they must
9821 -- be preserved. Build_Derived_Type chains the inherited clauses with
9822 -- the ones appearing on the extension. If this comes from a subtype
9823 -- declaration, all clauses are inherited.
9825 if No (First_Rep_Item (Def_Id)) then
9826 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
9827 end if;
9829 if Is_Tagged_Type (T) then
9830 Set_Is_Tagged_Type (Def_Id);
9831 Set_No_Tagged_Streams_Pragma (Def_Id, No_Tagged_Streams_Pragma (T));
9832 Make_Class_Wide_Type (Def_Id);
9833 end if;
9835 Set_Stored_Constraint (Def_Id, No_Elist);
9837 if Has_Discrs then
9838 Set_Discriminant_Constraint (Def_Id, Elist);
9839 Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
9840 end if;
9842 if Is_Tagged_Type (T) then
9844 -- Ada 2005 (AI-251): In case of concurrent types we inherit the
9845 -- concurrent record type (which has the list of primitive
9846 -- operations).
9848 if Ada_Version >= Ada_2005
9849 and then Is_Concurrent_Type (T)
9850 then
9851 Set_Corresponding_Record_Type (Def_Id,
9852 Corresponding_Record_Type (T));
9853 else
9854 Set_Direct_Primitive_Operations (Def_Id,
9855 Direct_Primitive_Operations (T));
9856 end if;
9858 Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
9859 end if;
9861 -- Subtypes introduced by component declarations do not need to be
9862 -- marked as delayed, and do not get freeze nodes, because the semantics
9863 -- verifies that the parents of the subtypes are frozen before the
9864 -- enclosing record is frozen.
9866 if not Is_Type (Scope (Def_Id)) then
9867 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
9869 if Is_Private_Type (T)
9870 and then Present (Full_View (T))
9871 then
9872 Conditional_Delay (Def_Id, Full_View (T));
9873 else
9874 Conditional_Delay (Def_Id, T);
9875 end if;
9876 end if;
9878 if Is_Record_Type (T) then
9879 Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
9881 if Has_Discrs
9882 and then not Is_Empty_Elmt_List (Elist)
9883 and then not For_Access
9884 then
9885 Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
9886 elsif not For_Access then
9887 Set_Cloned_Subtype (Def_Id, T);
9888 end if;
9889 end if;
9890 end Build_Discriminated_Subtype;
9892 ---------------------------
9893 -- Build_Itype_Reference --
9894 ---------------------------
9896 procedure Build_Itype_Reference
9897 (Ityp : Entity_Id;
9898 Nod : Node_Id)
9900 IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
9901 begin
9903 -- Itype references are only created for use by the back-end
9905 if Inside_A_Generic then
9906 return;
9907 else
9908 Set_Itype (IR, Ityp);
9909 Insert_After (Nod, IR);
9910 end if;
9911 end Build_Itype_Reference;
9913 ------------------------
9914 -- Build_Scalar_Bound --
9915 ------------------------
9917 function Build_Scalar_Bound
9918 (Bound : Node_Id;
9919 Par_T : Entity_Id;
9920 Der_T : Entity_Id) return Node_Id
9922 New_Bound : Entity_Id;
9924 begin
9925 -- Note: not clear why this is needed, how can the original bound
9926 -- be unanalyzed at this point? and if it is, what business do we
9927 -- have messing around with it? and why is the base type of the
9928 -- parent type the right type for the resolution. It probably is
9929 -- not. It is OK for the new bound we are creating, but not for
9930 -- the old one??? Still if it never happens, no problem.
9932 Analyze_And_Resolve (Bound, Base_Type (Par_T));
9934 if Nkind_In (Bound, N_Integer_Literal, N_Real_Literal) then
9935 New_Bound := New_Copy (Bound);
9936 Set_Etype (New_Bound, Der_T);
9937 Set_Analyzed (New_Bound);
9939 elsif Is_Entity_Name (Bound) then
9940 New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
9942 -- The following is almost certainly wrong. What business do we have
9943 -- relocating a node (Bound) that is presumably still attached to
9944 -- the tree elsewhere???
9946 else
9947 New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
9948 end if;
9950 Set_Etype (New_Bound, Der_T);
9951 return New_Bound;
9952 end Build_Scalar_Bound;
9954 --------------------------------
9955 -- Build_Underlying_Full_View --
9956 --------------------------------
9958 procedure Build_Underlying_Full_View
9959 (N : Node_Id;
9960 Typ : Entity_Id;
9961 Par : Entity_Id)
9963 Loc : constant Source_Ptr := Sloc (N);
9964 Subt : constant Entity_Id :=
9965 Make_Defining_Identifier
9966 (Loc, New_External_Name (Chars (Typ), 'S'));
9968 Constr : Node_Id;
9969 Indic : Node_Id;
9970 C : Node_Id;
9971 Id : Node_Id;
9973 procedure Set_Discriminant_Name (Id : Node_Id);
9974 -- If the derived type has discriminants, they may rename discriminants
9975 -- of the parent. When building the full view of the parent, we need to
9976 -- recover the names of the original discriminants if the constraint is
9977 -- given by named associations.
9979 ---------------------------
9980 -- Set_Discriminant_Name --
9981 ---------------------------
9983 procedure Set_Discriminant_Name (Id : Node_Id) is
9984 Disc : Entity_Id;
9986 begin
9987 Set_Original_Discriminant (Id, Empty);
9989 if Has_Discriminants (Typ) then
9990 Disc := First_Discriminant (Typ);
9991 while Present (Disc) loop
9992 if Chars (Disc) = Chars (Id)
9993 and then Present (Corresponding_Discriminant (Disc))
9994 then
9995 Set_Chars (Id, Chars (Corresponding_Discriminant (Disc)));
9996 end if;
9997 Next_Discriminant (Disc);
9998 end loop;
9999 end if;
10000 end Set_Discriminant_Name;
10002 -- Start of processing for Build_Underlying_Full_View
10004 begin
10005 if Nkind (N) = N_Full_Type_Declaration then
10006 Constr := Constraint (Subtype_Indication (Type_Definition (N)));
10008 elsif Nkind (N) = N_Subtype_Declaration then
10009 Constr := New_Copy_Tree (Constraint (Subtype_Indication (N)));
10011 elsif Nkind (N) = N_Component_Declaration then
10012 Constr :=
10013 New_Copy_Tree
10014 (Constraint (Subtype_Indication (Component_Definition (N))));
10016 else
10017 raise Program_Error;
10018 end if;
10020 C := First (Constraints (Constr));
10021 while Present (C) loop
10022 if Nkind (C) = N_Discriminant_Association then
10023 Id := First (Selector_Names (C));
10024 while Present (Id) loop
10025 Set_Discriminant_Name (Id);
10026 Next (Id);
10027 end loop;
10028 end if;
10030 Next (C);
10031 end loop;
10033 Indic :=
10034 Make_Subtype_Declaration (Loc,
10035 Defining_Identifier => Subt,
10036 Subtype_Indication =>
10037 Make_Subtype_Indication (Loc,
10038 Subtype_Mark => New_Occurrence_Of (Par, Loc),
10039 Constraint => New_Copy_Tree (Constr)));
10041 -- If this is a component subtype for an outer itype, it is not
10042 -- a list member, so simply set the parent link for analysis: if
10043 -- the enclosing type does not need to be in a declarative list,
10044 -- neither do the components.
10046 if Is_List_Member (N)
10047 and then Nkind (N) /= N_Component_Declaration
10048 then
10049 Insert_Before (N, Indic);
10050 else
10051 Set_Parent (Indic, Parent (N));
10052 end if;
10054 Analyze (Indic);
10055 Set_Underlying_Full_View (Typ, Full_View (Subt));
10056 Set_Is_Underlying_Full_View (Full_View (Subt));
10057 end Build_Underlying_Full_View;
10059 -------------------------------
10060 -- Check_Abstract_Overriding --
10061 -------------------------------
10063 procedure Check_Abstract_Overriding (T : Entity_Id) is
10064 Alias_Subp : Entity_Id;
10065 Elmt : Elmt_Id;
10066 Op_List : Elist_Id;
10067 Subp : Entity_Id;
10068 Type_Def : Node_Id;
10070 procedure Check_Pragma_Implemented (Subp : Entity_Id);
10071 -- Ada 2012 (AI05-0030): Subprogram Subp overrides an interface routine
10072 -- which has pragma Implemented already set. Check whether Subp's entity
10073 -- kind conforms to the implementation kind of the overridden routine.
10075 procedure Check_Pragma_Implemented
10076 (Subp : Entity_Id;
10077 Iface_Subp : Entity_Id);
10078 -- Ada 2012 (AI05-0030): Subprogram Subp overrides interface routine
10079 -- Iface_Subp and both entities have pragma Implemented already set on
10080 -- them. Check whether the two implementation kinds are conforming.
10082 procedure Inherit_Pragma_Implemented
10083 (Subp : Entity_Id;
10084 Iface_Subp : Entity_Id);
10085 -- Ada 2012 (AI05-0030): Interface primitive Subp overrides interface
10086 -- subprogram Iface_Subp which has been marked by pragma Implemented.
10087 -- Propagate the implementation kind of Iface_Subp to Subp.
10089 ------------------------------
10090 -- Check_Pragma_Implemented --
10091 ------------------------------
10093 procedure Check_Pragma_Implemented (Subp : Entity_Id) is
10094 Iface_Alias : constant Entity_Id := Interface_Alias (Subp);
10095 Impl_Kind : constant Name_Id := Implementation_Kind (Iface_Alias);
10096 Subp_Alias : constant Entity_Id := Alias (Subp);
10097 Contr_Typ : Entity_Id;
10098 Impl_Subp : Entity_Id;
10100 begin
10101 -- Subp must have an alias since it is a hidden entity used to link
10102 -- an interface subprogram to its overriding counterpart.
10104 pragma Assert (Present (Subp_Alias));
10106 -- Handle aliases to synchronized wrappers
10108 Impl_Subp := Subp_Alias;
10110 if Is_Primitive_Wrapper (Impl_Subp) then
10111 Impl_Subp := Wrapped_Entity (Impl_Subp);
10112 end if;
10114 -- Extract the type of the controlling formal
10116 Contr_Typ := Etype (First_Formal (Subp_Alias));
10118 if Is_Concurrent_Record_Type (Contr_Typ) then
10119 Contr_Typ := Corresponding_Concurrent_Type (Contr_Typ);
10120 end if;
10122 -- An interface subprogram whose implementation kind is By_Entry must
10123 -- be implemented by an entry.
10125 if Impl_Kind = Name_By_Entry
10126 and then Ekind (Impl_Subp) /= E_Entry
10127 then
10128 Error_Msg_Node_2 := Iface_Alias;
10129 Error_Msg_NE
10130 ("type & must implement abstract subprogram & with an entry",
10131 Subp_Alias, Contr_Typ);
10133 elsif Impl_Kind = Name_By_Protected_Procedure then
10135 -- An interface subprogram whose implementation kind is By_
10136 -- Protected_Procedure cannot be implemented by a primitive
10137 -- procedure of a task type.
10139 if Ekind (Contr_Typ) /= E_Protected_Type then
10140 Error_Msg_Node_2 := Contr_Typ;
10141 Error_Msg_NE
10142 ("interface subprogram & cannot be implemented by a " &
10143 "primitive procedure of task type &", Subp_Alias,
10144 Iface_Alias);
10146 -- An interface subprogram whose implementation kind is By_
10147 -- Protected_Procedure must be implemented by a procedure.
10149 elsif Ekind (Impl_Subp) /= E_Procedure then
10150 Error_Msg_Node_2 := Iface_Alias;
10151 Error_Msg_NE
10152 ("type & must implement abstract subprogram & with a " &
10153 "procedure", Subp_Alias, Contr_Typ);
10155 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
10156 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
10157 then
10158 Error_Msg_Name_1 := Impl_Kind;
10159 Error_Msg_N
10160 ("overriding operation& must have synchronization%",
10161 Subp_Alias);
10162 end if;
10164 -- If primitive has Optional synchronization, overriding operation
10165 -- must match if it has an explicit synchronization..
10167 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
10168 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
10169 then
10170 Error_Msg_Name_1 := Impl_Kind;
10171 Error_Msg_N
10172 ("overriding operation& must have syncrhonization%",
10173 Subp_Alias);
10174 end if;
10175 end Check_Pragma_Implemented;
10177 ------------------------------
10178 -- Check_Pragma_Implemented --
10179 ------------------------------
10181 procedure Check_Pragma_Implemented
10182 (Subp : Entity_Id;
10183 Iface_Subp : Entity_Id)
10185 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
10186 Subp_Kind : constant Name_Id := Implementation_Kind (Subp);
10188 begin
10189 -- Ada 2012 (AI05-0030): The implementation kinds of an overridden
10190 -- and overriding subprogram are different. In general this is an
10191 -- error except when the implementation kind of the overridden
10192 -- subprograms is By_Any or Optional.
10194 if Iface_Kind /= Subp_Kind
10195 and then Iface_Kind /= Name_By_Any
10196 and then Iface_Kind /= Name_Optional
10197 then
10198 if Iface_Kind = Name_By_Entry then
10199 Error_Msg_N
10200 ("incompatible implementation kind, overridden subprogram " &
10201 "is marked By_Entry", Subp);
10202 else
10203 Error_Msg_N
10204 ("incompatible implementation kind, overridden subprogram " &
10205 "is marked By_Protected_Procedure", Subp);
10206 end if;
10207 end if;
10208 end Check_Pragma_Implemented;
10210 --------------------------------
10211 -- Inherit_Pragma_Implemented --
10212 --------------------------------
10214 procedure Inherit_Pragma_Implemented
10215 (Subp : Entity_Id;
10216 Iface_Subp : Entity_Id)
10218 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
10219 Loc : constant Source_Ptr := Sloc (Subp);
10220 Impl_Prag : Node_Id;
10222 begin
10223 -- Since the implementation kind is stored as a representation item
10224 -- rather than a flag, create a pragma node.
10226 Impl_Prag :=
10227 Make_Pragma (Loc,
10228 Chars => Name_Implemented,
10229 Pragma_Argument_Associations => New_List (
10230 Make_Pragma_Argument_Association (Loc,
10231 Expression => New_Occurrence_Of (Subp, Loc)),
10233 Make_Pragma_Argument_Association (Loc,
10234 Expression => Make_Identifier (Loc, Iface_Kind))));
10236 -- The pragma doesn't need to be analyzed because it is internally
10237 -- built. It is safe to directly register it as a rep item since we
10238 -- are only interested in the characters of the implementation kind.
10240 Record_Rep_Item (Subp, Impl_Prag);
10241 end Inherit_Pragma_Implemented;
10243 -- Start of processing for Check_Abstract_Overriding
10245 begin
10246 Op_List := Primitive_Operations (T);
10248 -- Loop to check primitive operations
10250 Elmt := First_Elmt (Op_List);
10251 while Present (Elmt) loop
10252 Subp := Node (Elmt);
10253 Alias_Subp := Alias (Subp);
10255 -- Inherited subprograms are identified by the fact that they do not
10256 -- come from source, and the associated source location is the
10257 -- location of the first subtype of the derived type.
10259 -- Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
10260 -- subprograms that "require overriding".
10262 -- Special exception, do not complain about failure to override the
10263 -- stream routines _Input and _Output, as well as the primitive
10264 -- operations used in dispatching selects since we always provide
10265 -- automatic overridings for these subprograms.
10267 -- The partial view of T may have been a private extension, for
10268 -- which inherited functions dispatching on result are abstract.
10269 -- If the full view is a null extension, there is no need for
10270 -- overriding in Ada 2005, but wrappers need to be built for them
10271 -- (see exp_ch3, Build_Controlling_Function_Wrappers).
10273 if Is_Null_Extension (T)
10274 and then Has_Controlling_Result (Subp)
10275 and then Ada_Version >= Ada_2005
10276 and then Present (Alias_Subp)
10277 and then not Comes_From_Source (Subp)
10278 and then not Is_Abstract_Subprogram (Alias_Subp)
10279 and then not Is_Access_Type (Etype (Subp))
10280 then
10281 null;
10283 -- Ada 2005 (AI-251): Internal entities of interfaces need no
10284 -- processing because this check is done with the aliased
10285 -- entity
10287 elsif Present (Interface_Alias (Subp)) then
10288 null;
10290 elsif (Is_Abstract_Subprogram (Subp)
10291 or else Requires_Overriding (Subp)
10292 or else
10293 (Has_Controlling_Result (Subp)
10294 and then Present (Alias_Subp)
10295 and then not Comes_From_Source (Subp)
10296 and then Sloc (Subp) = Sloc (First_Subtype (T))))
10297 and then not Is_TSS (Subp, TSS_Stream_Input)
10298 and then not Is_TSS (Subp, TSS_Stream_Output)
10299 and then not Is_Abstract_Type (T)
10300 and then not Is_Predefined_Interface_Primitive (Subp)
10302 -- Ada 2005 (AI-251): Do not consider hidden entities associated
10303 -- with abstract interface types because the check will be done
10304 -- with the aliased entity (otherwise we generate a duplicated
10305 -- error message).
10307 and then not Present (Interface_Alias (Subp))
10308 then
10309 if Present (Alias_Subp) then
10311 -- Only perform the check for a derived subprogram when the
10312 -- type has an explicit record extension. This avoids incorrect
10313 -- flagging of abstract subprograms for the case of a type
10314 -- without an extension that is derived from a formal type
10315 -- with a tagged actual (can occur within a private part).
10317 -- Ada 2005 (AI-391): In the case of an inherited function with
10318 -- a controlling result of the type, the rule does not apply if
10319 -- the type is a null extension (unless the parent function
10320 -- itself is abstract, in which case the function must still be
10321 -- be overridden). The expander will generate an overriding
10322 -- wrapper function calling the parent subprogram (see
10323 -- Exp_Ch3.Make_Controlling_Wrapper_Functions).
10325 Type_Def := Type_Definition (Parent (T));
10327 if Nkind (Type_Def) = N_Derived_Type_Definition
10328 and then Present (Record_Extension_Part (Type_Def))
10329 and then
10330 (Ada_Version < Ada_2005
10331 or else not Is_Null_Extension (T)
10332 or else Ekind (Subp) = E_Procedure
10333 or else not Has_Controlling_Result (Subp)
10334 or else Is_Abstract_Subprogram (Alias_Subp)
10335 or else Requires_Overriding (Subp)
10336 or else Is_Access_Type (Etype (Subp)))
10337 then
10338 -- Avoid reporting error in case of abstract predefined
10339 -- primitive inherited from interface type because the
10340 -- body of internally generated predefined primitives
10341 -- of tagged types are generated later by Freeze_Type
10343 if Is_Interface (Root_Type (T))
10344 and then Is_Abstract_Subprogram (Subp)
10345 and then Is_Predefined_Dispatching_Operation (Subp)
10346 and then not Comes_From_Source (Ultimate_Alias (Subp))
10347 then
10348 null;
10350 -- A null extension is not obliged to override an inherited
10351 -- procedure subject to pragma Extensions_Visible with value
10352 -- False and at least one controlling OUT parameter
10353 -- (SPARK RM 6.1.7(6)).
10355 elsif Is_Null_Extension (T)
10356 and then Is_EVF_Procedure (Subp)
10357 then
10358 null;
10360 else
10361 Error_Msg_NE
10362 ("type must be declared abstract or & overridden",
10363 T, Subp);
10365 -- Traverse the whole chain of aliased subprograms to
10366 -- complete the error notification. This is especially
10367 -- useful for traceability of the chain of entities when
10368 -- the subprogram corresponds with an interface
10369 -- subprogram (which may be defined in another package).
10371 if Present (Alias_Subp) then
10372 declare
10373 E : Entity_Id;
10375 begin
10376 E := Subp;
10377 while Present (Alias (E)) loop
10379 -- Avoid reporting redundant errors on entities
10380 -- inherited from interfaces
10382 if Sloc (E) /= Sloc (T) then
10383 Error_Msg_Sloc := Sloc (E);
10384 Error_Msg_NE
10385 ("\& has been inherited #", T, Subp);
10386 end if;
10388 E := Alias (E);
10389 end loop;
10391 Error_Msg_Sloc := Sloc (E);
10393 -- AI05-0068: report if there is an overriding
10394 -- non-abstract subprogram that is invisible.
10396 if Is_Hidden (E)
10397 and then not Is_Abstract_Subprogram (E)
10398 then
10399 Error_Msg_NE
10400 ("\& subprogram# is not visible",
10401 T, Subp);
10403 -- Clarify the case where a non-null extension must
10404 -- override inherited procedure subject to pragma
10405 -- Extensions_Visible with value False and at least
10406 -- one controlling OUT param.
10408 elsif Is_EVF_Procedure (E) then
10409 Error_Msg_NE
10410 ("\& # is subject to Extensions_Visible False",
10411 T, Subp);
10413 else
10414 Error_Msg_NE
10415 ("\& has been inherited from subprogram #",
10416 T, Subp);
10417 end if;
10418 end;
10419 end if;
10420 end if;
10422 -- Ada 2005 (AI-345): Protected or task type implementing
10423 -- abstract interfaces.
10425 elsif Is_Concurrent_Record_Type (T)
10426 and then Present (Interfaces (T))
10427 then
10428 -- There is no need to check here RM 9.4(11.9/3) since we
10429 -- are processing the corresponding record type and the
10430 -- mode of the overriding subprograms was verified by
10431 -- Check_Conformance when the corresponding concurrent
10432 -- type declaration was analyzed.
10434 Error_Msg_NE
10435 ("interface subprogram & must be overridden", T, Subp);
10437 -- Examine primitive operations of synchronized type to find
10438 -- homonyms that have the wrong profile.
10440 declare
10441 Prim : Entity_Id;
10443 begin
10444 Prim := First_Entity (Corresponding_Concurrent_Type (T));
10445 while Present (Prim) loop
10446 if Chars (Prim) = Chars (Subp) then
10447 Error_Msg_NE
10448 ("profile is not type conformant with prefixed "
10449 & "view profile of inherited operation&",
10450 Prim, Subp);
10451 end if;
10453 Next_Entity (Prim);
10454 end loop;
10455 end;
10456 end if;
10458 else
10459 Error_Msg_Node_2 := T;
10460 Error_Msg_N
10461 ("abstract subprogram& not allowed for type&", Subp);
10463 -- Also post unconditional warning on the type (unconditional
10464 -- so that if there are more than one of these cases, we get
10465 -- them all, and not just the first one).
10467 Error_Msg_Node_2 := Subp;
10468 Error_Msg_N ("nonabstract type& has abstract subprogram&!", T);
10469 end if;
10471 -- A subprogram subject to pragma Extensions_Visible with value
10472 -- "True" cannot override a subprogram subject to the same pragma
10473 -- with value "False" (SPARK RM 6.1.7(5)).
10475 elsif Extensions_Visible_Status (Subp) = Extensions_Visible_True
10476 and then Present (Overridden_Operation (Subp))
10477 and then Extensions_Visible_Status (Overridden_Operation (Subp)) =
10478 Extensions_Visible_False
10479 then
10480 Error_Msg_Sloc := Sloc (Overridden_Operation (Subp));
10481 Error_Msg_N
10482 ("subprogram & with Extensions_Visible True cannot override "
10483 & "subprogram # with Extensions_Visible False", Subp);
10484 end if;
10486 -- Ada 2012 (AI05-0030): Perform checks related to pragma Implemented
10488 -- Subp is an expander-generated procedure which maps an interface
10489 -- alias to a protected wrapper. The interface alias is flagged by
10490 -- pragma Implemented. Ensure that Subp is a procedure when the
10491 -- implementation kind is By_Protected_Procedure or an entry when
10492 -- By_Entry.
10494 if Ada_Version >= Ada_2012
10495 and then Is_Hidden (Subp)
10496 and then Present (Interface_Alias (Subp))
10497 and then Has_Rep_Pragma (Interface_Alias (Subp), Name_Implemented)
10498 then
10499 Check_Pragma_Implemented (Subp);
10500 end if;
10502 -- Subp is an interface primitive which overrides another interface
10503 -- primitive marked with pragma Implemented.
10505 if Ada_Version >= Ada_2012
10506 and then Present (Overridden_Operation (Subp))
10507 and then Has_Rep_Pragma
10508 (Overridden_Operation (Subp), Name_Implemented)
10509 then
10510 -- If the overriding routine is also marked by Implemented, check
10511 -- that the two implementation kinds are conforming.
10513 if Has_Rep_Pragma (Subp, Name_Implemented) then
10514 Check_Pragma_Implemented
10515 (Subp => Subp,
10516 Iface_Subp => Overridden_Operation (Subp));
10518 -- Otherwise the overriding routine inherits the implementation
10519 -- kind from the overridden subprogram.
10521 else
10522 Inherit_Pragma_Implemented
10523 (Subp => Subp,
10524 Iface_Subp => Overridden_Operation (Subp));
10525 end if;
10526 end if;
10528 -- If the operation is a wrapper for a synchronized primitive, it
10529 -- may be called indirectly through a dispatching select. We assume
10530 -- that it will be referenced elsewhere indirectly, and suppress
10531 -- warnings about an unused entity.
10533 if Is_Primitive_Wrapper (Subp)
10534 and then Present (Wrapped_Entity (Subp))
10535 then
10536 Set_Referenced (Wrapped_Entity (Subp));
10537 end if;
10539 Next_Elmt (Elmt);
10540 end loop;
10541 end Check_Abstract_Overriding;
10543 ------------------------------------------------
10544 -- Check_Access_Discriminant_Requires_Limited --
10545 ------------------------------------------------
10547 procedure Check_Access_Discriminant_Requires_Limited
10548 (D : Node_Id;
10549 Loc : Node_Id)
10551 begin
10552 -- A discriminant_specification for an access discriminant shall appear
10553 -- only in the declaration for a task or protected type, or for a type
10554 -- with the reserved word 'limited' in its definition or in one of its
10555 -- ancestors (RM 3.7(10)).
10557 -- AI-0063: The proper condition is that type must be immutably limited,
10558 -- or else be a partial view.
10560 if Nkind (Discriminant_Type (D)) = N_Access_Definition then
10561 if Is_Limited_View (Current_Scope)
10562 or else
10563 (Nkind (Parent (Current_Scope)) = N_Private_Type_Declaration
10564 and then Limited_Present (Parent (Current_Scope)))
10565 then
10566 null;
10568 else
10569 Error_Msg_N
10570 ("access discriminants allowed only for limited types", Loc);
10571 end if;
10572 end if;
10573 end Check_Access_Discriminant_Requires_Limited;
10575 -----------------------------------
10576 -- Check_Aliased_Component_Types --
10577 -----------------------------------
10579 procedure Check_Aliased_Component_Types (T : Entity_Id) is
10580 C : Entity_Id;
10582 begin
10583 -- ??? Also need to check components of record extensions, but not
10584 -- components of protected types (which are always limited).
10586 -- Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
10587 -- types to be unconstrained. This is safe because it is illegal to
10588 -- create access subtypes to such types with explicit discriminant
10589 -- constraints.
10591 if not Is_Limited_Type (T) then
10592 if Ekind (T) = E_Record_Type then
10593 C := First_Component (T);
10594 while Present (C) loop
10595 if Is_Aliased (C)
10596 and then Has_Discriminants (Etype (C))
10597 and then not Is_Constrained (Etype (C))
10598 and then not In_Instance_Body
10599 and then Ada_Version < Ada_2005
10600 then
10601 Error_Msg_N
10602 ("aliased component must be constrained (RM 3.6(11))",
10604 end if;
10606 Next_Component (C);
10607 end loop;
10609 elsif Ekind (T) = E_Array_Type then
10610 if Has_Aliased_Components (T)
10611 and then Has_Discriminants (Component_Type (T))
10612 and then not Is_Constrained (Component_Type (T))
10613 and then not In_Instance_Body
10614 and then Ada_Version < Ada_2005
10615 then
10616 Error_Msg_N
10617 ("aliased component type must be constrained (RM 3.6(11))",
10619 end if;
10620 end if;
10621 end if;
10622 end Check_Aliased_Component_Types;
10624 ---------------------------------------
10625 -- Check_Anonymous_Access_Components --
10626 ---------------------------------------
10628 procedure Check_Anonymous_Access_Components
10629 (Typ_Decl : Node_Id;
10630 Typ : Entity_Id;
10631 Prev : Entity_Id;
10632 Comp_List : Node_Id)
10634 Loc : constant Source_Ptr := Sloc (Typ_Decl);
10635 Anon_Access : Entity_Id;
10636 Acc_Def : Node_Id;
10637 Comp : Node_Id;
10638 Comp_Def : Node_Id;
10639 Decl : Node_Id;
10640 Type_Def : Node_Id;
10642 procedure Build_Incomplete_Type_Declaration;
10643 -- If the record type contains components that include an access to the
10644 -- current record, then create an incomplete type declaration for the
10645 -- record, to be used as the designated type of the anonymous access.
10646 -- This is done only once, and only if there is no previous partial
10647 -- view of the type.
10649 function Designates_T (Subt : Node_Id) return Boolean;
10650 -- Check whether a node designates the enclosing record type, or 'Class
10651 -- of that type
10653 function Mentions_T (Acc_Def : Node_Id) return Boolean;
10654 -- Check whether an access definition includes a reference to
10655 -- the enclosing record type. The reference can be a subtype mark
10656 -- in the access definition itself, a 'Class attribute reference, or
10657 -- recursively a reference appearing in a parameter specification
10658 -- or result definition of an access_to_subprogram definition.
10660 --------------------------------------
10661 -- Build_Incomplete_Type_Declaration --
10662 --------------------------------------
10664 procedure Build_Incomplete_Type_Declaration is
10665 Decl : Node_Id;
10666 Inc_T : Entity_Id;
10667 H : Entity_Id;
10669 -- Is_Tagged indicates whether the type is tagged. It is tagged if
10670 -- it's "is new ... with record" or else "is tagged record ...".
10672 Is_Tagged : constant Boolean :=
10673 (Nkind (Type_Definition (Typ_Decl)) = N_Derived_Type_Definition
10674 and then
10675 Present (Record_Extension_Part (Type_Definition (Typ_Decl))))
10676 or else
10677 (Nkind (Type_Definition (Typ_Decl)) = N_Record_Definition
10678 and then Tagged_Present (Type_Definition (Typ_Decl)));
10680 begin
10681 -- If there is a previous partial view, no need to create a new one
10682 -- If the partial view, given by Prev, is incomplete, If Prev is
10683 -- a private declaration, full declaration is flagged accordingly.
10685 if Prev /= Typ then
10686 if Is_Tagged then
10687 Make_Class_Wide_Type (Prev);
10688 Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
10689 Set_Etype (Class_Wide_Type (Typ), Typ);
10690 end if;
10692 return;
10694 elsif Has_Private_Declaration (Typ) then
10696 -- If we refer to T'Class inside T, and T is the completion of a
10697 -- private type, then make sure the class-wide type exists.
10699 if Is_Tagged then
10700 Make_Class_Wide_Type (Typ);
10701 end if;
10703 return;
10705 -- If there was a previous anonymous access type, the incomplete
10706 -- type declaration will have been created already.
10708 elsif Present (Current_Entity (Typ))
10709 and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
10710 and then Full_View (Current_Entity (Typ)) = Typ
10711 then
10712 if Is_Tagged
10713 and then Comes_From_Source (Current_Entity (Typ))
10714 and then not Is_Tagged_Type (Current_Entity (Typ))
10715 then
10716 Make_Class_Wide_Type (Typ);
10717 Error_Msg_N
10718 ("incomplete view of tagged type should be declared tagged??",
10719 Parent (Current_Entity (Typ)));
10720 end if;
10721 return;
10723 else
10724 Inc_T := Make_Defining_Identifier (Loc, Chars (Typ));
10725 Decl := Make_Incomplete_Type_Declaration (Loc, Inc_T);
10727 -- Type has already been inserted into the current scope. Remove
10728 -- it, and add incomplete declaration for type, so that subsequent
10729 -- anonymous access types can use it. The entity is unchained from
10730 -- the homonym list and from immediate visibility. After analysis,
10731 -- the entity in the incomplete declaration becomes immediately
10732 -- visible in the record declaration that follows.
10734 H := Current_Entity (Typ);
10736 if H = Typ then
10737 Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
10738 else
10739 while Present (H)
10740 and then Homonym (H) /= Typ
10741 loop
10742 H := Homonym (Typ);
10743 end loop;
10745 Set_Homonym (H, Homonym (Typ));
10746 end if;
10748 Insert_Before (Typ_Decl, Decl);
10749 Analyze (Decl);
10750 Set_Full_View (Inc_T, Typ);
10752 if Is_Tagged then
10754 -- Create a common class-wide type for both views, and set the
10755 -- Etype of the class-wide type to the full view.
10757 Make_Class_Wide_Type (Inc_T);
10758 Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
10759 Set_Etype (Class_Wide_Type (Typ), Typ);
10760 end if;
10761 end if;
10762 end Build_Incomplete_Type_Declaration;
10764 ------------------
10765 -- Designates_T --
10766 ------------------
10768 function Designates_T (Subt : Node_Id) return Boolean is
10769 Type_Id : constant Name_Id := Chars (Typ);
10771 function Names_T (Nam : Node_Id) return Boolean;
10772 -- The record type has not been introduced in the current scope
10773 -- yet, so we must examine the name of the type itself, either
10774 -- an identifier T, or an expanded name of the form P.T, where
10775 -- P denotes the current scope.
10777 -------------
10778 -- Names_T --
10779 -------------
10781 function Names_T (Nam : Node_Id) return Boolean is
10782 begin
10783 if Nkind (Nam) = N_Identifier then
10784 return Chars (Nam) = Type_Id;
10786 elsif Nkind (Nam) = N_Selected_Component then
10787 if Chars (Selector_Name (Nam)) = Type_Id then
10788 if Nkind (Prefix (Nam)) = N_Identifier then
10789 return Chars (Prefix (Nam)) = Chars (Current_Scope);
10791 elsif Nkind (Prefix (Nam)) = N_Selected_Component then
10792 return Chars (Selector_Name (Prefix (Nam))) =
10793 Chars (Current_Scope);
10794 else
10795 return False;
10796 end if;
10798 else
10799 return False;
10800 end if;
10802 else
10803 return False;
10804 end if;
10805 end Names_T;
10807 -- Start of processing for Designates_T
10809 begin
10810 if Nkind (Subt) = N_Identifier then
10811 return Chars (Subt) = Type_Id;
10813 -- Reference can be through an expanded name which has not been
10814 -- analyzed yet, and which designates enclosing scopes.
10816 elsif Nkind (Subt) = N_Selected_Component then
10817 if Names_T (Subt) then
10818 return True;
10820 -- Otherwise it must denote an entity that is already visible.
10821 -- The access definition may name a subtype of the enclosing
10822 -- type, if there is a previous incomplete declaration for it.
10824 else
10825 Find_Selected_Component (Subt);
10826 return
10827 Is_Entity_Name (Subt)
10828 and then Scope (Entity (Subt)) = Current_Scope
10829 and then
10830 (Chars (Base_Type (Entity (Subt))) = Type_Id
10831 or else
10832 (Is_Class_Wide_Type (Entity (Subt))
10833 and then
10834 Chars (Etype (Base_Type (Entity (Subt)))) =
10835 Type_Id));
10836 end if;
10838 -- A reference to the current type may appear as the prefix of
10839 -- a 'Class attribute.
10841 elsif Nkind (Subt) = N_Attribute_Reference
10842 and then Attribute_Name (Subt) = Name_Class
10843 then
10844 return Names_T (Prefix (Subt));
10846 else
10847 return False;
10848 end if;
10849 end Designates_T;
10851 ----------------
10852 -- Mentions_T --
10853 ----------------
10855 function Mentions_T (Acc_Def : Node_Id) return Boolean is
10856 Param_Spec : Node_Id;
10858 Acc_Subprg : constant Node_Id :=
10859 Access_To_Subprogram_Definition (Acc_Def);
10861 begin
10862 if No (Acc_Subprg) then
10863 return Designates_T (Subtype_Mark (Acc_Def));
10864 end if;
10866 -- Component is an access_to_subprogram: examine its formals,
10867 -- and result definition in the case of an access_to_function.
10869 Param_Spec := First (Parameter_Specifications (Acc_Subprg));
10870 while Present (Param_Spec) loop
10871 if Nkind (Parameter_Type (Param_Spec)) = N_Access_Definition
10872 and then Mentions_T (Parameter_Type (Param_Spec))
10873 then
10874 return True;
10876 elsif Designates_T (Parameter_Type (Param_Spec)) then
10877 return True;
10878 end if;
10880 Next (Param_Spec);
10881 end loop;
10883 if Nkind (Acc_Subprg) = N_Access_Function_Definition then
10884 if Nkind (Result_Definition (Acc_Subprg)) =
10885 N_Access_Definition
10886 then
10887 return Mentions_T (Result_Definition (Acc_Subprg));
10888 else
10889 return Designates_T (Result_Definition (Acc_Subprg));
10890 end if;
10891 end if;
10893 return False;
10894 end Mentions_T;
10896 -- Start of processing for Check_Anonymous_Access_Components
10898 begin
10899 if No (Comp_List) then
10900 return;
10901 end if;
10903 Comp := First (Component_Items (Comp_List));
10904 while Present (Comp) loop
10905 if Nkind (Comp) = N_Component_Declaration
10906 and then Present
10907 (Access_Definition (Component_Definition (Comp)))
10908 and then
10909 Mentions_T (Access_Definition (Component_Definition (Comp)))
10910 then
10911 Comp_Def := Component_Definition (Comp);
10912 Acc_Def :=
10913 Access_To_Subprogram_Definition (Access_Definition (Comp_Def));
10915 Build_Incomplete_Type_Declaration;
10916 Anon_Access := Make_Temporary (Loc, 'S');
10918 -- Create a declaration for the anonymous access type: either
10919 -- an access_to_object or an access_to_subprogram.
10921 if Present (Acc_Def) then
10922 if Nkind (Acc_Def) = N_Access_Function_Definition then
10923 Type_Def :=
10924 Make_Access_Function_Definition (Loc,
10925 Parameter_Specifications =>
10926 Parameter_Specifications (Acc_Def),
10927 Result_Definition => Result_Definition (Acc_Def));
10928 else
10929 Type_Def :=
10930 Make_Access_Procedure_Definition (Loc,
10931 Parameter_Specifications =>
10932 Parameter_Specifications (Acc_Def));
10933 end if;
10935 else
10936 Type_Def :=
10937 Make_Access_To_Object_Definition (Loc,
10938 Subtype_Indication =>
10939 Relocate_Node
10940 (Subtype_Mark (Access_Definition (Comp_Def))));
10942 Set_Constant_Present
10943 (Type_Def, Constant_Present (Access_Definition (Comp_Def)));
10944 Set_All_Present
10945 (Type_Def, All_Present (Access_Definition (Comp_Def)));
10946 end if;
10948 Set_Null_Exclusion_Present
10949 (Type_Def,
10950 Null_Exclusion_Present (Access_Definition (Comp_Def)));
10952 Decl :=
10953 Make_Full_Type_Declaration (Loc,
10954 Defining_Identifier => Anon_Access,
10955 Type_Definition => Type_Def);
10957 Insert_Before (Typ_Decl, Decl);
10958 Analyze (Decl);
10960 -- If an access to subprogram, create the extra formals
10962 if Present (Acc_Def) then
10963 Create_Extra_Formals (Designated_Type (Anon_Access));
10965 -- If an access to object, preserve entity of designated type,
10966 -- for ASIS use, before rewriting the component definition.
10968 else
10969 declare
10970 Desig : Entity_Id;
10972 begin
10973 Desig := Entity (Subtype_Indication (Type_Def));
10975 -- If the access definition is to the current record,
10976 -- the visible entity at this point is an incomplete
10977 -- type. Retrieve the full view to simplify ASIS queries
10979 if Ekind (Desig) = E_Incomplete_Type then
10980 Desig := Full_View (Desig);
10981 end if;
10983 Set_Entity
10984 (Subtype_Mark (Access_Definition (Comp_Def)), Desig);
10985 end;
10986 end if;
10988 Rewrite (Comp_Def,
10989 Make_Component_Definition (Loc,
10990 Subtype_Indication =>
10991 New_Occurrence_Of (Anon_Access, Loc)));
10993 if Ekind (Designated_Type (Anon_Access)) = E_Subprogram_Type then
10994 Set_Ekind (Anon_Access, E_Anonymous_Access_Subprogram_Type);
10995 else
10996 Set_Ekind (Anon_Access, E_Anonymous_Access_Type);
10997 end if;
10999 Set_Is_Local_Anonymous_Access (Anon_Access);
11000 end if;
11002 Next (Comp);
11003 end loop;
11005 if Present (Variant_Part (Comp_List)) then
11006 declare
11007 V : Node_Id;
11008 begin
11009 V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
11010 while Present (V) loop
11011 Check_Anonymous_Access_Components
11012 (Typ_Decl, Typ, Prev, Component_List (V));
11013 Next_Non_Pragma (V);
11014 end loop;
11015 end;
11016 end if;
11017 end Check_Anonymous_Access_Components;
11019 ----------------------
11020 -- Check_Completion --
11021 ----------------------
11023 procedure Check_Completion (Body_Id : Node_Id := Empty) is
11024 E : Entity_Id;
11026 procedure Post_Error;
11027 -- Post error message for lack of completion for entity E
11029 ----------------
11030 -- Post_Error --
11031 ----------------
11033 procedure Post_Error is
11034 procedure Missing_Body;
11035 -- Output missing body message
11037 ------------------
11038 -- Missing_Body --
11039 ------------------
11041 procedure Missing_Body is
11042 begin
11043 -- Spec is in same unit, so we can post on spec
11045 if In_Same_Source_Unit (Body_Id, E) then
11046 Error_Msg_N ("missing body for &", E);
11048 -- Spec is in a separate unit, so we have to post on the body
11050 else
11051 Error_Msg_NE ("missing body for & declared#!", Body_Id, E);
11052 end if;
11053 end Missing_Body;
11055 -- Start of processing for Post_Error
11057 begin
11058 if not Comes_From_Source (E) then
11059 if Ekind_In (E, E_Task_Type, E_Protected_Type) then
11061 -- It may be an anonymous protected type created for a
11062 -- single variable. Post error on variable, if present.
11064 declare
11065 Var : Entity_Id;
11067 begin
11068 Var := First_Entity (Current_Scope);
11069 while Present (Var) loop
11070 exit when Etype (Var) = E
11071 and then Comes_From_Source (Var);
11073 Next_Entity (Var);
11074 end loop;
11076 if Present (Var) then
11077 E := Var;
11078 end if;
11079 end;
11080 end if;
11081 end if;
11083 -- If a generated entity has no completion, then either previous
11084 -- semantic errors have disabled the expansion phase, or else we had
11085 -- missing subunits, or else we are compiling without expansion,
11086 -- or else something is very wrong.
11088 if not Comes_From_Source (E) then
11089 pragma Assert
11090 (Serious_Errors_Detected > 0
11091 or else Configurable_Run_Time_Violations > 0
11092 or else Subunits_Missing
11093 or else not Expander_Active);
11094 return;
11096 -- Here for source entity
11098 else
11099 -- Here if no body to post the error message, so we post the error
11100 -- on the declaration that has no completion. This is not really
11101 -- the right place to post it, think about this later ???
11103 if No (Body_Id) then
11104 if Is_Type (E) then
11105 Error_Msg_NE
11106 ("missing full declaration for }", Parent (E), E);
11107 else
11108 Error_Msg_NE ("missing body for &", Parent (E), E);
11109 end if;
11111 -- Package body has no completion for a declaration that appears
11112 -- in the corresponding spec. Post error on the body, with a
11113 -- reference to the non-completed declaration.
11115 else
11116 Error_Msg_Sloc := Sloc (E);
11118 if Is_Type (E) then
11119 Error_Msg_NE ("missing full declaration for }!", Body_Id, E);
11121 elsif Is_Overloadable (E)
11122 and then Current_Entity_In_Scope (E) /= E
11123 then
11124 -- It may be that the completion is mistyped and appears as
11125 -- a distinct overloading of the entity.
11127 declare
11128 Candidate : constant Entity_Id :=
11129 Current_Entity_In_Scope (E);
11130 Decl : constant Node_Id :=
11131 Unit_Declaration_Node (Candidate);
11133 begin
11134 if Is_Overloadable (Candidate)
11135 and then Ekind (Candidate) = Ekind (E)
11136 and then Nkind (Decl) = N_Subprogram_Body
11137 and then Acts_As_Spec (Decl)
11138 then
11139 Check_Type_Conformant (Candidate, E);
11141 else
11142 Missing_Body;
11143 end if;
11144 end;
11146 else
11147 Missing_Body;
11148 end if;
11149 end if;
11150 end if;
11151 end Post_Error;
11153 -- Local variables
11155 Pack_Id : constant Entity_Id := Current_Scope;
11157 -- Start of processing for Check_Completion
11159 begin
11160 E := First_Entity (Pack_Id);
11161 while Present (E) loop
11162 if Is_Intrinsic_Subprogram (E) then
11163 null;
11165 -- The following situation requires special handling: a child unit
11166 -- that appears in the context clause of the body of its parent:
11168 -- procedure Parent.Child (...);
11170 -- with Parent.Child;
11171 -- package body Parent is
11173 -- Here Parent.Child appears as a local entity, but should not be
11174 -- flagged as requiring completion, because it is a compilation
11175 -- unit.
11177 -- Ignore missing completion for a subprogram that does not come from
11178 -- source (including the _Call primitive operation of RAS types,
11179 -- which has to have the flag Comes_From_Source for other purposes):
11180 -- we assume that the expander will provide the missing completion.
11181 -- In case of previous errors, other expansion actions that provide
11182 -- bodies for null procedures with not be invoked, so inhibit message
11183 -- in those cases.
11185 -- Note that E_Operator is not in the list that follows, because
11186 -- this kind is reserved for predefined operators, that are
11187 -- intrinsic and do not need completion.
11189 elsif Ekind_In (E, E_Function,
11190 E_Procedure,
11191 E_Generic_Function,
11192 E_Generic_Procedure)
11193 then
11194 if Has_Completion (E) then
11195 null;
11197 elsif Is_Subprogram (E) and then Is_Abstract_Subprogram (E) then
11198 null;
11200 elsif Is_Subprogram (E)
11201 and then (not Comes_From_Source (E)
11202 or else Chars (E) = Name_uCall)
11203 then
11204 null;
11206 elsif
11207 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
11208 then
11209 null;
11211 elsif Nkind (Parent (E)) = N_Procedure_Specification
11212 and then Null_Present (Parent (E))
11213 and then Serious_Errors_Detected > 0
11214 then
11215 null;
11217 else
11218 Post_Error;
11219 end if;
11221 elsif Is_Entry (E) then
11222 if not Has_Completion (E) and then
11223 (Ekind (Scope (E)) = E_Protected_Object
11224 or else Ekind (Scope (E)) = E_Protected_Type)
11225 then
11226 Post_Error;
11227 end if;
11229 elsif Is_Package_Or_Generic_Package (E) then
11230 if Unit_Requires_Body (E) then
11231 if not Has_Completion (E)
11232 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
11233 N_Compilation_Unit
11234 then
11235 Post_Error;
11236 end if;
11238 elsif not Is_Child_Unit (E) then
11239 May_Need_Implicit_Body (E);
11240 end if;
11242 -- A formal incomplete type (Ada 2012) does not require a completion;
11243 -- other incomplete type declarations do.
11245 elsif Ekind (E) = E_Incomplete_Type
11246 and then No (Underlying_Type (E))
11247 and then not Is_Generic_Type (E)
11248 then
11249 Post_Error;
11251 elsif Ekind_In (E, E_Task_Type, E_Protected_Type)
11252 and then not Has_Completion (E)
11253 then
11254 Post_Error;
11256 -- A single task declared in the current scope is a constant, verify
11257 -- that the body of its anonymous type is in the same scope. If the
11258 -- task is defined elsewhere, this may be a renaming declaration for
11259 -- which no completion is needed.
11261 elsif Ekind (E) = E_Constant
11262 and then Ekind (Etype (E)) = E_Task_Type
11263 and then not Has_Completion (Etype (E))
11264 and then Scope (Etype (E)) = Current_Scope
11265 then
11266 Post_Error;
11268 elsif Ekind (E) = E_Protected_Object
11269 and then not Has_Completion (Etype (E))
11270 then
11271 Post_Error;
11273 elsif Ekind (E) = E_Record_Type then
11274 if Is_Tagged_Type (E) then
11275 Check_Abstract_Overriding (E);
11276 Check_Conventions (E);
11277 end if;
11279 Check_Aliased_Component_Types (E);
11281 elsif Ekind (E) = E_Array_Type then
11282 Check_Aliased_Component_Types (E);
11284 end if;
11286 Next_Entity (E);
11287 end loop;
11288 end Check_Completion;
11290 ------------------------------------
11291 -- Check_CPP_Type_Has_No_Defaults --
11292 ------------------------------------
11294 procedure Check_CPP_Type_Has_No_Defaults (T : Entity_Id) is
11295 Tdef : constant Node_Id := Type_Definition (Declaration_Node (T));
11296 Clist : Node_Id;
11297 Comp : Node_Id;
11299 begin
11300 -- Obtain the component list
11302 if Nkind (Tdef) = N_Record_Definition then
11303 Clist := Component_List (Tdef);
11304 else pragma Assert (Nkind (Tdef) = N_Derived_Type_Definition);
11305 Clist := Component_List (Record_Extension_Part (Tdef));
11306 end if;
11308 -- Check all components to ensure no default expressions
11310 if Present (Clist) then
11311 Comp := First (Component_Items (Clist));
11312 while Present (Comp) loop
11313 if Present (Expression (Comp)) then
11314 Error_Msg_N
11315 ("component of imported 'C'P'P type cannot have "
11316 & "default expression", Expression (Comp));
11317 end if;
11319 Next (Comp);
11320 end loop;
11321 end if;
11322 end Check_CPP_Type_Has_No_Defaults;
11324 ----------------------------
11325 -- Check_Delta_Expression --
11326 ----------------------------
11328 procedure Check_Delta_Expression (E : Node_Id) is
11329 begin
11330 if not (Is_Real_Type (Etype (E))) then
11331 Wrong_Type (E, Any_Real);
11333 elsif not Is_OK_Static_Expression (E) then
11334 Flag_Non_Static_Expr
11335 ("non-static expression used for delta value!", E);
11337 elsif not UR_Is_Positive (Expr_Value_R (E)) then
11338 Error_Msg_N ("delta expression must be positive", E);
11340 else
11341 return;
11342 end if;
11344 -- If any of above errors occurred, then replace the incorrect
11345 -- expression by the real 0.1, which should prevent further errors.
11347 Rewrite (E,
11348 Make_Real_Literal (Sloc (E), Ureal_Tenth));
11349 Analyze_And_Resolve (E, Standard_Float);
11350 end Check_Delta_Expression;
11352 -----------------------------
11353 -- Check_Digits_Expression --
11354 -----------------------------
11356 procedure Check_Digits_Expression (E : Node_Id) is
11357 begin
11358 if not (Is_Integer_Type (Etype (E))) then
11359 Wrong_Type (E, Any_Integer);
11361 elsif not Is_OK_Static_Expression (E) then
11362 Flag_Non_Static_Expr
11363 ("non-static expression used for digits value!", E);
11365 elsif Expr_Value (E) <= 0 then
11366 Error_Msg_N ("digits value must be greater than zero", E);
11368 else
11369 return;
11370 end if;
11372 -- If any of above errors occurred, then replace the incorrect
11373 -- expression by the integer 1, which should prevent further errors.
11375 Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
11376 Analyze_And_Resolve (E, Standard_Integer);
11378 end Check_Digits_Expression;
11380 --------------------------
11381 -- Check_Initialization --
11382 --------------------------
11384 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
11385 begin
11386 -- Special processing for limited types
11388 if Is_Limited_Type (T)
11389 and then not In_Instance
11390 and then not In_Inlined_Body
11391 then
11392 if not OK_For_Limited_Init (T, Exp) then
11394 -- In GNAT mode, this is just a warning, to allow it to be evilly
11395 -- turned off. Otherwise it is a real error.
11397 if GNAT_Mode then
11398 Error_Msg_N
11399 ("??cannot initialize entities of limited type!", Exp);
11401 elsif Ada_Version < Ada_2005 then
11403 -- The side effect removal machinery may generate illegal Ada
11404 -- code to avoid the usage of access types and 'reference in
11405 -- SPARK mode. Since this is legal code with respect to theorem
11406 -- proving, do not emit the error.
11408 if GNATprove_Mode
11409 and then Nkind (Exp) = N_Function_Call
11410 and then Nkind (Parent (Exp)) = N_Object_Declaration
11411 and then not Comes_From_Source
11412 (Defining_Identifier (Parent (Exp)))
11413 then
11414 null;
11416 else
11417 Error_Msg_N
11418 ("cannot initialize entities of limited type", Exp);
11419 Explain_Limited_Type (T, Exp);
11420 end if;
11422 else
11423 -- Specialize error message according to kind of illegal
11424 -- initial expression.
11426 if Nkind (Exp) = N_Type_Conversion
11427 and then Nkind (Expression (Exp)) = N_Function_Call
11428 then
11429 Error_Msg_N
11430 ("illegal context for call"
11431 & " to function with limited result", Exp);
11433 else
11434 Error_Msg_N
11435 ("initialization of limited object requires aggregate "
11436 & "or function call", Exp);
11437 end if;
11438 end if;
11439 end if;
11440 end if;
11442 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag gets
11443 -- set unless we can be sure that no range check is required.
11445 if (GNATprove_Mode or not Expander_Active)
11446 and then Is_Scalar_Type (T)
11447 and then not Is_In_Range (Exp, T, Assume_Valid => True)
11448 then
11449 Set_Do_Range_Check (Exp);
11450 end if;
11451 end Check_Initialization;
11453 ----------------------
11454 -- Check_Interfaces --
11455 ----------------------
11457 procedure Check_Interfaces (N : Node_Id; Def : Node_Id) is
11458 Parent_Type : constant Entity_Id := Etype (Defining_Identifier (N));
11460 Iface : Node_Id;
11461 Iface_Def : Node_Id;
11462 Iface_Typ : Entity_Id;
11463 Parent_Node : Node_Id;
11465 Is_Task : Boolean := False;
11466 -- Set True if parent type or any progenitor is a task interface
11468 Is_Protected : Boolean := False;
11469 -- Set True if parent type or any progenitor is a protected interface
11471 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
11472 -- Check that a progenitor is compatible with declaration. If an error
11473 -- message is output, it is posted on Error_Node.
11475 ------------------
11476 -- Check_Ifaces --
11477 ------------------
11479 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
11480 Iface_Id : constant Entity_Id :=
11481 Defining_Identifier (Parent (Iface_Def));
11482 Type_Def : Node_Id;
11484 begin
11485 if Nkind (N) = N_Private_Extension_Declaration then
11486 Type_Def := N;
11487 else
11488 Type_Def := Type_Definition (N);
11489 end if;
11491 if Is_Task_Interface (Iface_Id) then
11492 Is_Task := True;
11494 elsif Is_Protected_Interface (Iface_Id) then
11495 Is_Protected := True;
11496 end if;
11498 if Is_Synchronized_Interface (Iface_Id) then
11500 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
11501 -- extension derived from a synchronized interface must explicitly
11502 -- be declared synchronized, because the full view will be a
11503 -- synchronized type.
11505 if Nkind (N) = N_Private_Extension_Declaration then
11506 if not Synchronized_Present (N) then
11507 Error_Msg_NE
11508 ("private extension of& must be explicitly synchronized",
11509 N, Iface_Id);
11510 end if;
11512 -- However, by 3.9.4(16/2), a full type that is a record extension
11513 -- is never allowed to derive from a synchronized interface (note
11514 -- that interfaces must be excluded from this check, because those
11515 -- are represented by derived type definitions in some cases).
11517 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition
11518 and then not Interface_Present (Type_Definition (N))
11519 then
11520 Error_Msg_N ("record extension cannot derive from synchronized "
11521 & "interface", Error_Node);
11522 end if;
11523 end if;
11525 -- Check that the characteristics of the progenitor are compatible
11526 -- with the explicit qualifier in the declaration.
11527 -- The check only applies to qualifiers that come from source.
11528 -- Limited_Present also appears in the declaration of corresponding
11529 -- records, and the check does not apply to them.
11531 if Limited_Present (Type_Def)
11532 and then not
11533 Is_Concurrent_Record_Type (Defining_Identifier (N))
11534 then
11535 if Is_Limited_Interface (Parent_Type)
11536 and then not Is_Limited_Interface (Iface_Id)
11537 then
11538 Error_Msg_NE
11539 ("progenitor & must be limited interface",
11540 Error_Node, Iface_Id);
11542 elsif
11543 (Task_Present (Iface_Def)
11544 or else Protected_Present (Iface_Def)
11545 or else Synchronized_Present (Iface_Def))
11546 and then Nkind (N) /= N_Private_Extension_Declaration
11547 and then not Error_Posted (N)
11548 then
11549 Error_Msg_NE
11550 ("progenitor & must be limited interface",
11551 Error_Node, Iface_Id);
11552 end if;
11554 -- Protected interfaces can only inherit from limited, synchronized
11555 -- or protected interfaces.
11557 elsif Nkind (N) = N_Full_Type_Declaration
11558 and then Protected_Present (Type_Def)
11559 then
11560 if Limited_Present (Iface_Def)
11561 or else Synchronized_Present (Iface_Def)
11562 or else Protected_Present (Iface_Def)
11563 then
11564 null;
11566 elsif Task_Present (Iface_Def) then
11567 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
11568 & "from task interface", Error_Node);
11570 else
11571 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
11572 & "from non-limited interface", Error_Node);
11573 end if;
11575 -- Ada 2005 (AI-345): Synchronized interfaces can only inherit from
11576 -- limited and synchronized.
11578 elsif Synchronized_Present (Type_Def) then
11579 if Limited_Present (Iface_Def)
11580 or else Synchronized_Present (Iface_Def)
11581 then
11582 null;
11584 elsif Protected_Present (Iface_Def)
11585 and then Nkind (N) /= N_Private_Extension_Declaration
11586 then
11587 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11588 & "from protected interface", Error_Node);
11590 elsif Task_Present (Iface_Def)
11591 and then Nkind (N) /= N_Private_Extension_Declaration
11592 then
11593 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11594 & "from task interface", Error_Node);
11596 elsif not Is_Limited_Interface (Iface_Id) then
11597 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11598 & "from non-limited interface", Error_Node);
11599 end if;
11601 -- Ada 2005 (AI-345): Task interfaces can only inherit from limited,
11602 -- synchronized or task interfaces.
11604 elsif Nkind (N) = N_Full_Type_Declaration
11605 and then Task_Present (Type_Def)
11606 then
11607 if Limited_Present (Iface_Def)
11608 or else Synchronized_Present (Iface_Def)
11609 or else Task_Present (Iface_Def)
11610 then
11611 null;
11613 elsif Protected_Present (Iface_Def) then
11614 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
11615 & "protected interface", Error_Node);
11617 else
11618 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
11619 & "non-limited interface", Error_Node);
11620 end if;
11621 end if;
11622 end Check_Ifaces;
11624 -- Start of processing for Check_Interfaces
11626 begin
11627 if Is_Interface (Parent_Type) then
11628 if Is_Task_Interface (Parent_Type) then
11629 Is_Task := True;
11631 elsif Is_Protected_Interface (Parent_Type) then
11632 Is_Protected := True;
11633 end if;
11634 end if;
11636 if Nkind (N) = N_Private_Extension_Declaration then
11638 -- Check that progenitors are compatible with declaration
11640 Iface := First (Interface_List (Def));
11641 while Present (Iface) loop
11642 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
11644 Parent_Node := Parent (Base_Type (Iface_Typ));
11645 Iface_Def := Type_Definition (Parent_Node);
11647 if not Is_Interface (Iface_Typ) then
11648 Diagnose_Interface (Iface, Iface_Typ);
11649 else
11650 Check_Ifaces (Iface_Def, Iface);
11651 end if;
11653 Next (Iface);
11654 end loop;
11656 if Is_Task and Is_Protected then
11657 Error_Msg_N
11658 ("type cannot derive from task and protected interface", N);
11659 end if;
11661 return;
11662 end if;
11664 -- Full type declaration of derived type.
11665 -- Check compatibility with parent if it is interface type
11667 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
11668 and then Is_Interface (Parent_Type)
11669 then
11670 Parent_Node := Parent (Parent_Type);
11672 -- More detailed checks for interface varieties
11674 Check_Ifaces
11675 (Iface_Def => Type_Definition (Parent_Node),
11676 Error_Node => Subtype_Indication (Type_Definition (N)));
11677 end if;
11679 Iface := First (Interface_List (Def));
11680 while Present (Iface) loop
11681 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
11683 Parent_Node := Parent (Base_Type (Iface_Typ));
11684 Iface_Def := Type_Definition (Parent_Node);
11686 if not Is_Interface (Iface_Typ) then
11687 Diagnose_Interface (Iface, Iface_Typ);
11689 else
11690 -- "The declaration of a specific descendant of an interface
11691 -- type freezes the interface type" RM 13.14
11693 Freeze_Before (N, Iface_Typ);
11694 Check_Ifaces (Iface_Def, Error_Node => Iface);
11695 end if;
11697 Next (Iface);
11698 end loop;
11700 if Is_Task and Is_Protected then
11701 Error_Msg_N
11702 ("type cannot derive from task and protected interface", N);
11703 end if;
11704 end Check_Interfaces;
11706 ------------------------------------
11707 -- Check_Or_Process_Discriminants --
11708 ------------------------------------
11710 -- If an incomplete or private type declaration was already given for the
11711 -- type, the discriminants may have already been processed if they were
11712 -- present on the incomplete declaration. In this case a full conformance
11713 -- check has been performed in Find_Type_Name, and we then recheck here
11714 -- some properties that can't be checked on the partial view alone.
11715 -- Otherwise we call Process_Discriminants.
11717 procedure Check_Or_Process_Discriminants
11718 (N : Node_Id;
11719 T : Entity_Id;
11720 Prev : Entity_Id := Empty)
11722 begin
11723 if Has_Discriminants (T) then
11725 -- Discriminants are already set on T if they were already present
11726 -- on the partial view. Make them visible to component declarations.
11728 declare
11729 D : Entity_Id;
11730 -- Discriminant on T (full view) referencing expr on partial view
11732 Prev_D : Entity_Id;
11733 -- Entity of corresponding discriminant on partial view
11735 New_D : Node_Id;
11736 -- Discriminant specification for full view, expression is
11737 -- the syntactic copy on full view (which has been checked for
11738 -- conformance with partial view), only used here to post error
11739 -- message.
11741 begin
11742 D := First_Discriminant (T);
11743 New_D := First (Discriminant_Specifications (N));
11744 while Present (D) loop
11745 Prev_D := Current_Entity (D);
11746 Set_Current_Entity (D);
11747 Set_Is_Immediately_Visible (D);
11748 Set_Homonym (D, Prev_D);
11750 -- Handle the case where there is an untagged partial view and
11751 -- the full view is tagged: must disallow discriminants with
11752 -- defaults, unless compiling for Ada 2012, which allows a
11753 -- limited tagged type to have defaulted discriminants (see
11754 -- AI05-0214). However, suppress error here if it was already
11755 -- reported on the default expression of the partial view.
11757 if Is_Tagged_Type (T)
11758 and then Present (Expression (Parent (D)))
11759 and then (not Is_Limited_Type (Current_Scope)
11760 or else Ada_Version < Ada_2012)
11761 and then not Error_Posted (Expression (Parent (D)))
11762 then
11763 if Ada_Version >= Ada_2012 then
11764 Error_Msg_N
11765 ("discriminants of nonlimited tagged type cannot have "
11766 & "defaults",
11767 Expression (New_D));
11768 else
11769 Error_Msg_N
11770 ("discriminants of tagged type cannot have defaults",
11771 Expression (New_D));
11772 end if;
11773 end if;
11775 -- Ada 2005 (AI-230): Access discriminant allowed in
11776 -- non-limited record types.
11778 if Ada_Version < Ada_2005 then
11780 -- This restriction gets applied to the full type here. It
11781 -- has already been applied earlier to the partial view.
11783 Check_Access_Discriminant_Requires_Limited (Parent (D), N);
11784 end if;
11786 Next_Discriminant (D);
11787 Next (New_D);
11788 end loop;
11789 end;
11791 elsif Present (Discriminant_Specifications (N)) then
11792 Process_Discriminants (N, Prev);
11793 end if;
11794 end Check_Or_Process_Discriminants;
11796 ----------------------
11797 -- Check_Real_Bound --
11798 ----------------------
11800 procedure Check_Real_Bound (Bound : Node_Id) is
11801 begin
11802 if not Is_Real_Type (Etype (Bound)) then
11803 Error_Msg_N
11804 ("bound in real type definition must be of real type", Bound);
11806 elsif not Is_OK_Static_Expression (Bound) then
11807 Flag_Non_Static_Expr
11808 ("non-static expression used for real type bound!", Bound);
11810 else
11811 return;
11812 end if;
11814 Rewrite
11815 (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
11816 Analyze (Bound);
11817 Resolve (Bound, Standard_Float);
11818 end Check_Real_Bound;
11820 ------------------------------
11821 -- Complete_Private_Subtype --
11822 ------------------------------
11824 procedure Complete_Private_Subtype
11825 (Priv : Entity_Id;
11826 Full : Entity_Id;
11827 Full_Base : Entity_Id;
11828 Related_Nod : Node_Id)
11830 Save_Next_Entity : Entity_Id;
11831 Save_Homonym : Entity_Id;
11833 begin
11834 -- Set semantic attributes for (implicit) private subtype completion.
11835 -- If the full type has no discriminants, then it is a copy of the
11836 -- full view of the base. Otherwise, it is a subtype of the base with
11837 -- a possible discriminant constraint. Save and restore the original
11838 -- Next_Entity field of full to ensure that the calls to Copy_Node do
11839 -- not corrupt the entity chain.
11841 -- Note that the type of the full view is the same entity as the type
11842 -- of the partial view. In this fashion, the subtype has access to the
11843 -- correct view of the parent.
11845 Save_Next_Entity := Next_Entity (Full);
11846 Save_Homonym := Homonym (Priv);
11848 case Ekind (Full_Base) is
11849 when Class_Wide_Kind
11850 | Private_Kind
11851 | Protected_Kind
11852 | Task_Kind
11853 | E_Record_Subtype
11854 | E_Record_Type
11856 Copy_Node (Priv, Full);
11858 Set_Has_Discriminants
11859 (Full, Has_Discriminants (Full_Base));
11860 Set_Has_Unknown_Discriminants
11861 (Full, Has_Unknown_Discriminants (Full_Base));
11862 Set_First_Entity (Full, First_Entity (Full_Base));
11863 Set_Last_Entity (Full, Last_Entity (Full_Base));
11865 -- If the underlying base type is constrained, we know that the
11866 -- full view of the subtype is constrained as well (the converse
11867 -- is not necessarily true).
11869 if Is_Constrained (Full_Base) then
11870 Set_Is_Constrained (Full);
11871 end if;
11873 when others =>
11874 Copy_Node (Full_Base, Full);
11876 Set_Chars (Full, Chars (Priv));
11877 Conditional_Delay (Full, Priv);
11878 Set_Sloc (Full, Sloc (Priv));
11879 end case;
11881 Set_Next_Entity (Full, Save_Next_Entity);
11882 Set_Homonym (Full, Save_Homonym);
11883 Set_Associated_Node_For_Itype (Full, Related_Nod);
11885 -- Set common attributes for all subtypes: kind, convention, etc.
11887 Set_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
11888 Set_Convention (Full, Convention (Full_Base));
11890 -- The Etype of the full view is inconsistent. Gigi needs to see the
11891 -- structural full view, which is what the current scheme gives: the
11892 -- Etype of the full view is the etype of the full base. However, if the
11893 -- full base is a derived type, the full view then looks like a subtype
11894 -- of the parent, not a subtype of the full base. If instead we write:
11896 -- Set_Etype (Full, Full_Base);
11898 -- then we get inconsistencies in the front-end (confusion between
11899 -- views). Several outstanding bugs are related to this ???
11901 Set_Is_First_Subtype (Full, False);
11902 Set_Scope (Full, Scope (Priv));
11903 Set_Size_Info (Full, Full_Base);
11904 Set_RM_Size (Full, RM_Size (Full_Base));
11905 Set_Is_Itype (Full);
11907 -- A subtype of a private-type-without-discriminants, whose full-view
11908 -- has discriminants with default expressions, is not constrained.
11910 if not Has_Discriminants (Priv) then
11911 Set_Is_Constrained (Full, Is_Constrained (Full_Base));
11913 if Has_Discriminants (Full_Base) then
11914 Set_Discriminant_Constraint
11915 (Full, Discriminant_Constraint (Full_Base));
11917 -- The partial view may have been indefinite, the full view
11918 -- might not be.
11920 Set_Has_Unknown_Discriminants
11921 (Full, Has_Unknown_Discriminants (Full_Base));
11922 end if;
11923 end if;
11925 Set_First_Rep_Item (Full, First_Rep_Item (Full_Base));
11926 Set_Depends_On_Private (Full, Has_Private_Component (Full));
11928 -- Freeze the private subtype entity if its parent is delayed, and not
11929 -- already frozen. We skip this processing if the type is an anonymous
11930 -- subtype of a record component, or is the corresponding record of a
11931 -- protected type, since these are processed when the enclosing type
11932 -- is frozen. If the parent type is declared in a nested package then
11933 -- the freezing of the private and full views also happens later.
11935 if not Is_Type (Scope (Full)) then
11936 if Is_Itype (Priv)
11937 and then In_Same_Source_Unit (Full, Full_Base)
11938 and then Scope (Full_Base) /= Scope (Full)
11939 then
11940 Set_Has_Delayed_Freeze (Full);
11941 Set_Has_Delayed_Freeze (Priv);
11943 else
11944 Set_Has_Delayed_Freeze (Full,
11945 Has_Delayed_Freeze (Full_Base)
11946 and then not Is_Frozen (Full_Base));
11947 end if;
11948 end if;
11950 Set_Freeze_Node (Full, Empty);
11951 Set_Is_Frozen (Full, False);
11952 Set_Full_View (Priv, Full);
11954 if Has_Discriminants (Full) then
11955 Set_Stored_Constraint_From_Discriminant_Constraint (Full);
11956 Set_Stored_Constraint (Priv, Stored_Constraint (Full));
11958 if Has_Unknown_Discriminants (Full) then
11959 Set_Discriminant_Constraint (Full, No_Elist);
11960 end if;
11961 end if;
11963 if Ekind (Full_Base) = E_Record_Type
11964 and then Has_Discriminants (Full_Base)
11965 and then Has_Discriminants (Priv) -- might not, if errors
11966 and then not Has_Unknown_Discriminants (Priv)
11967 and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
11968 then
11969 Create_Constrained_Components
11970 (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
11972 -- If the full base is itself derived from private, build a congruent
11973 -- subtype of its underlying type, for use by the back end. For a
11974 -- constrained record component, the declaration cannot be placed on
11975 -- the component list, but it must nevertheless be built an analyzed, to
11976 -- supply enough information for Gigi to compute the size of component.
11978 elsif Ekind (Full_Base) in Private_Kind
11979 and then Is_Derived_Type (Full_Base)
11980 and then Has_Discriminants (Full_Base)
11981 and then (Ekind (Current_Scope) /= E_Record_Subtype)
11982 then
11983 if not Is_Itype (Priv)
11984 and then
11985 Nkind (Subtype_Indication (Parent (Priv))) = N_Subtype_Indication
11986 then
11987 Build_Underlying_Full_View
11988 (Parent (Priv), Full, Etype (Full_Base));
11990 elsif Nkind (Related_Nod) = N_Component_Declaration then
11991 Build_Underlying_Full_View (Related_Nod, Full, Etype (Full_Base));
11992 end if;
11994 elsif Is_Record_Type (Full_Base) then
11996 -- Show Full is simply a renaming of Full_Base
11998 Set_Cloned_Subtype (Full, Full_Base);
11999 end if;
12001 -- It is unsafe to share the bounds of a scalar type, because the Itype
12002 -- is elaborated on demand, and if a bound is non-static then different
12003 -- orders of elaboration in different units will lead to different
12004 -- external symbols.
12006 if Is_Scalar_Type (Full_Base) then
12007 Set_Scalar_Range (Full,
12008 Make_Range (Sloc (Related_Nod),
12009 Low_Bound =>
12010 Duplicate_Subexpr_No_Checks (Type_Low_Bound (Full_Base)),
12011 High_Bound =>
12012 Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
12014 -- This completion inherits the bounds of the full parent, but if
12015 -- the parent is an unconstrained floating point type, so is the
12016 -- completion.
12018 if Is_Floating_Point_Type (Full_Base) then
12019 Set_Includes_Infinities
12020 (Scalar_Range (Full), Has_Infinities (Full_Base));
12021 end if;
12022 end if;
12024 -- ??? It seems that a lot of fields are missing that should be copied
12025 -- from Full_Base to Full. Here are some that are introduced in a
12026 -- non-disruptive way but a cleanup is necessary.
12028 if Is_Tagged_Type (Full_Base) then
12029 Set_Is_Tagged_Type (Full);
12030 Set_Direct_Primitive_Operations
12031 (Full, Direct_Primitive_Operations (Full_Base));
12032 Set_No_Tagged_Streams_Pragma
12033 (Full, No_Tagged_Streams_Pragma (Full_Base));
12035 -- Inherit class_wide type of full_base in case the partial view was
12036 -- not tagged. Otherwise it has already been created when the private
12037 -- subtype was analyzed.
12039 if No (Class_Wide_Type (Full)) then
12040 Set_Class_Wide_Type (Full, Class_Wide_Type (Full_Base));
12041 end if;
12043 -- If this is a subtype of a protected or task type, constrain its
12044 -- corresponding record, unless this is a subtype without constraints,
12045 -- i.e. a simple renaming as with an actual subtype in an instance.
12047 elsif Is_Concurrent_Type (Full_Base) then
12048 if Has_Discriminants (Full)
12049 and then Present (Corresponding_Record_Type (Full_Base))
12050 and then
12051 not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
12052 then
12053 Set_Corresponding_Record_Type (Full,
12054 Constrain_Corresponding_Record
12055 (Full, Corresponding_Record_Type (Full_Base), Related_Nod));
12057 else
12058 Set_Corresponding_Record_Type (Full,
12059 Corresponding_Record_Type (Full_Base));
12060 end if;
12061 end if;
12063 -- Link rep item chain, and also setting of Has_Predicates from private
12064 -- subtype to full subtype, since we will need these on the full subtype
12065 -- to create the predicate function. Note that the full subtype may
12066 -- already have rep items, inherited from the full view of the base
12067 -- type, so we must be sure not to overwrite these entries.
12069 declare
12070 Append : Boolean;
12071 Item : Node_Id;
12072 Next_Item : Node_Id;
12073 Priv_Item : Node_Id;
12075 begin
12076 Item := First_Rep_Item (Full);
12077 Priv_Item := First_Rep_Item (Priv);
12079 -- If no existing rep items on full type, we can just link directly
12080 -- to the list of items on the private type, if any exist.. Same if
12081 -- the rep items are only those inherited from the base
12083 if (No (Item)
12084 or else Nkind (Item) /= N_Aspect_Specification
12085 or else Entity (Item) = Full_Base)
12086 and then Present (First_Rep_Item (Priv))
12087 then
12088 Set_First_Rep_Item (Full, Priv_Item);
12090 -- Otherwise, search to the end of items currently linked to the full
12091 -- subtype and append the private items to the end. However, if Priv
12092 -- and Full already have the same list of rep items, then the append
12093 -- is not done, as that would create a circularity.
12095 -- The partial view may have a predicate and the rep item lists of
12096 -- both views agree when inherited from the same ancestor. In that
12097 -- case, simply propagate the list from one view to the other.
12098 -- A more complex analysis needed here ???
12100 elsif Present (Priv_Item)
12101 and then Item = Next_Rep_Item (Priv_Item)
12102 then
12103 Set_First_Rep_Item (Full, Priv_Item);
12105 elsif Item /= Priv_Item then
12106 Append := True;
12107 loop
12108 Next_Item := Next_Rep_Item (Item);
12109 exit when No (Next_Item);
12110 Item := Next_Item;
12112 -- If the private view has aspect specifications, the full view
12113 -- inherits them. Since these aspects may already have been
12114 -- attached to the full view during derivation, do not append
12115 -- them if already present.
12117 if Item = First_Rep_Item (Priv) then
12118 Append := False;
12119 exit;
12120 end if;
12121 end loop;
12123 -- And link the private type items at the end of the chain
12125 if Append then
12126 Set_Next_Rep_Item (Item, First_Rep_Item (Priv));
12127 end if;
12128 end if;
12129 end;
12131 -- Make sure Has_Predicates is set on full type if it is set on the
12132 -- private type. Note that it may already be set on the full type and
12133 -- if so, we don't want to unset it. Similarly, propagate information
12134 -- about delayed aspects, because the corresponding pragmas must be
12135 -- analyzed when one of the views is frozen. This last step is needed
12136 -- in particular when the full type is a scalar type for which an
12137 -- anonymous base type is constructed.
12139 -- The predicate functions are generated either at the freeze point
12140 -- of the type or at the end of the visible part, and we must avoid
12141 -- generating them twice.
12143 if Has_Predicates (Priv) then
12144 Set_Has_Predicates (Full);
12146 if Present (Predicate_Function (Priv))
12147 and then No (Predicate_Function (Full))
12148 then
12149 Set_Predicate_Function (Full, Predicate_Function (Priv));
12150 end if;
12151 end if;
12153 if Has_Delayed_Aspects (Priv) then
12154 Set_Has_Delayed_Aspects (Full);
12155 end if;
12156 end Complete_Private_Subtype;
12158 ----------------------------
12159 -- Constant_Redeclaration --
12160 ----------------------------
12162 procedure Constant_Redeclaration
12163 (Id : Entity_Id;
12164 N : Node_Id;
12165 T : out Entity_Id)
12167 Prev : constant Entity_Id := Current_Entity_In_Scope (Id);
12168 Obj_Def : constant Node_Id := Object_Definition (N);
12169 New_T : Entity_Id;
12171 procedure Check_Possible_Deferred_Completion
12172 (Prev_Id : Entity_Id;
12173 Prev_Obj_Def : Node_Id;
12174 Curr_Obj_Def : Node_Id);
12175 -- Determine whether the two object definitions describe the partial
12176 -- and the full view of a constrained deferred constant. Generate
12177 -- a subtype for the full view and verify that it statically matches
12178 -- the subtype of the partial view.
12180 procedure Check_Recursive_Declaration (Typ : Entity_Id);
12181 -- If deferred constant is an access type initialized with an allocator,
12182 -- check whether there is an illegal recursion in the definition,
12183 -- through a default value of some record subcomponent. This is normally
12184 -- detected when generating init procs, but requires this additional
12185 -- mechanism when expansion is disabled.
12187 ----------------------------------------
12188 -- Check_Possible_Deferred_Completion --
12189 ----------------------------------------
12191 procedure Check_Possible_Deferred_Completion
12192 (Prev_Id : Entity_Id;
12193 Prev_Obj_Def : Node_Id;
12194 Curr_Obj_Def : Node_Id)
12196 begin
12197 if Nkind (Prev_Obj_Def) = N_Subtype_Indication
12198 and then Present (Constraint (Prev_Obj_Def))
12199 and then Nkind (Curr_Obj_Def) = N_Subtype_Indication
12200 and then Present (Constraint (Curr_Obj_Def))
12201 then
12202 declare
12203 Loc : constant Source_Ptr := Sloc (N);
12204 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
12205 Decl : constant Node_Id :=
12206 Make_Subtype_Declaration (Loc,
12207 Defining_Identifier => Def_Id,
12208 Subtype_Indication =>
12209 Relocate_Node (Curr_Obj_Def));
12211 begin
12212 Insert_Before_And_Analyze (N, Decl);
12213 Set_Etype (Id, Def_Id);
12215 if not Subtypes_Statically_Match (Etype (Prev_Id), Def_Id) then
12216 Error_Msg_Sloc := Sloc (Prev_Id);
12217 Error_Msg_N ("subtype does not statically match deferred "
12218 & "declaration #", N);
12219 end if;
12220 end;
12221 end if;
12222 end Check_Possible_Deferred_Completion;
12224 ---------------------------------
12225 -- Check_Recursive_Declaration --
12226 ---------------------------------
12228 procedure Check_Recursive_Declaration (Typ : Entity_Id) is
12229 Comp : Entity_Id;
12231 begin
12232 if Is_Record_Type (Typ) then
12233 Comp := First_Component (Typ);
12234 while Present (Comp) loop
12235 if Comes_From_Source (Comp) then
12236 if Present (Expression (Parent (Comp)))
12237 and then Is_Entity_Name (Expression (Parent (Comp)))
12238 and then Entity (Expression (Parent (Comp))) = Prev
12239 then
12240 Error_Msg_Sloc := Sloc (Parent (Comp));
12241 Error_Msg_NE
12242 ("illegal circularity with declaration for & #",
12243 N, Comp);
12244 return;
12246 elsif Is_Record_Type (Etype (Comp)) then
12247 Check_Recursive_Declaration (Etype (Comp));
12248 end if;
12249 end if;
12251 Next_Component (Comp);
12252 end loop;
12253 end if;
12254 end Check_Recursive_Declaration;
12256 -- Start of processing for Constant_Redeclaration
12258 begin
12259 if Nkind (Parent (Prev)) = N_Object_Declaration then
12260 if Nkind (Object_Definition
12261 (Parent (Prev))) = N_Subtype_Indication
12262 then
12263 -- Find type of new declaration. The constraints of the two
12264 -- views must match statically, but there is no point in
12265 -- creating an itype for the full view.
12267 if Nkind (Obj_Def) = N_Subtype_Indication then
12268 Find_Type (Subtype_Mark (Obj_Def));
12269 New_T := Entity (Subtype_Mark (Obj_Def));
12271 else
12272 Find_Type (Obj_Def);
12273 New_T := Entity (Obj_Def);
12274 end if;
12276 T := Etype (Prev);
12278 else
12279 -- The full view may impose a constraint, even if the partial
12280 -- view does not, so construct the subtype.
12282 New_T := Find_Type_Of_Object (Obj_Def, N);
12283 T := New_T;
12284 end if;
12286 else
12287 -- Current declaration is illegal, diagnosed below in Enter_Name
12289 T := Empty;
12290 New_T := Any_Type;
12291 end if;
12293 -- If previous full declaration or a renaming declaration exists, or if
12294 -- a homograph is present, let Enter_Name handle it, either with an
12295 -- error or with the removal of an overridden implicit subprogram.
12296 -- The previous one is a full declaration if it has an expression
12297 -- (which in the case of an aggregate is indicated by the Init flag).
12299 if Ekind (Prev) /= E_Constant
12300 or else Nkind (Parent (Prev)) = N_Object_Renaming_Declaration
12301 or else Present (Expression (Parent (Prev)))
12302 or else Has_Init_Expression (Parent (Prev))
12303 or else Present (Full_View (Prev))
12304 then
12305 Enter_Name (Id);
12307 -- Verify that types of both declarations match, or else that both types
12308 -- are anonymous access types whose designated subtypes statically match
12309 -- (as allowed in Ada 2005 by AI-385).
12311 elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
12312 and then
12313 (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
12314 or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
12315 or else Is_Access_Constant (Etype (New_T)) /=
12316 Is_Access_Constant (Etype (Prev))
12317 or else Can_Never_Be_Null (Etype (New_T)) /=
12318 Can_Never_Be_Null (Etype (Prev))
12319 or else Null_Exclusion_Present (Parent (Prev)) /=
12320 Null_Exclusion_Present (Parent (Id))
12321 or else not Subtypes_Statically_Match
12322 (Designated_Type (Etype (Prev)),
12323 Designated_Type (Etype (New_T))))
12324 then
12325 Error_Msg_Sloc := Sloc (Prev);
12326 Error_Msg_N ("type does not match declaration#", N);
12327 Set_Full_View (Prev, Id);
12328 Set_Etype (Id, Any_Type);
12330 -- A deferred constant whose type is an anonymous array is always
12331 -- illegal (unless imported). A detailed error message might be
12332 -- helpful for Ada beginners.
12334 if Nkind (Object_Definition (Parent (Prev)))
12335 = N_Constrained_Array_Definition
12336 and then Nkind (Object_Definition (N))
12337 = N_Constrained_Array_Definition
12338 then
12339 Error_Msg_N ("\each anonymous array is a distinct type", N);
12340 Error_Msg_N ("a deferred constant must have a named type",
12341 Object_Definition (Parent (Prev)));
12342 end if;
12344 elsif
12345 Null_Exclusion_Present (Parent (Prev))
12346 and then not Null_Exclusion_Present (N)
12347 then
12348 Error_Msg_Sloc := Sloc (Prev);
12349 Error_Msg_N ("null-exclusion does not match declaration#", N);
12350 Set_Full_View (Prev, Id);
12351 Set_Etype (Id, Any_Type);
12353 -- If so, process the full constant declaration
12355 else
12356 -- RM 7.4 (6): If the subtype defined by the subtype_indication in
12357 -- the deferred declaration is constrained, then the subtype defined
12358 -- by the subtype_indication in the full declaration shall match it
12359 -- statically.
12361 Check_Possible_Deferred_Completion
12362 (Prev_Id => Prev,
12363 Prev_Obj_Def => Object_Definition (Parent (Prev)),
12364 Curr_Obj_Def => Obj_Def);
12366 Set_Full_View (Prev, Id);
12367 Set_Is_Public (Id, Is_Public (Prev));
12368 Set_Is_Internal (Id);
12369 Append_Entity (Id, Current_Scope);
12371 -- Check ALIASED present if present before (RM 7.4(7))
12373 if Is_Aliased (Prev)
12374 and then not Aliased_Present (N)
12375 then
12376 Error_Msg_Sloc := Sloc (Prev);
12377 Error_Msg_N ("ALIASED required (see declaration #)", N);
12378 end if;
12380 -- Check that placement is in private part and that the incomplete
12381 -- declaration appeared in the visible part.
12383 if Ekind (Current_Scope) = E_Package
12384 and then not In_Private_Part (Current_Scope)
12385 then
12386 Error_Msg_Sloc := Sloc (Prev);
12387 Error_Msg_N
12388 ("full constant for declaration # must be in private part", N);
12390 elsif Ekind (Current_Scope) = E_Package
12391 and then
12392 List_Containing (Parent (Prev)) /=
12393 Visible_Declarations (Package_Specification (Current_Scope))
12394 then
12395 Error_Msg_N
12396 ("deferred constant must be declared in visible part",
12397 Parent (Prev));
12398 end if;
12400 if Is_Access_Type (T)
12401 and then Nkind (Expression (N)) = N_Allocator
12402 then
12403 Check_Recursive_Declaration (Designated_Type (T));
12404 end if;
12406 -- A deferred constant is a visible entity. If type has invariants,
12407 -- verify that the initial value satisfies them.
12409 if Has_Invariants (T) and then Present (Invariant_Procedure (T)) then
12410 Insert_After (N,
12411 Make_Invariant_Call (New_Occurrence_Of (Prev, Sloc (N))));
12412 end if;
12413 end if;
12414 end Constant_Redeclaration;
12416 ----------------------
12417 -- Constrain_Access --
12418 ----------------------
12420 procedure Constrain_Access
12421 (Def_Id : in out Entity_Id;
12422 S : Node_Id;
12423 Related_Nod : Node_Id)
12425 T : constant Entity_Id := Entity (Subtype_Mark (S));
12426 Desig_Type : constant Entity_Id := Designated_Type (T);
12427 Desig_Subtype : Entity_Id := Create_Itype (E_Void, Related_Nod);
12428 Constraint_OK : Boolean := True;
12430 begin
12431 if Is_Array_Type (Desig_Type) then
12432 Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
12434 elsif (Is_Record_Type (Desig_Type)
12435 or else Is_Incomplete_Or_Private_Type (Desig_Type))
12436 and then not Is_Constrained (Desig_Type)
12437 then
12438 -- ??? The following code is a temporary bypass to ignore a
12439 -- discriminant constraint on access type if it is constraining
12440 -- the current record. Avoid creating the implicit subtype of the
12441 -- record we are currently compiling since right now, we cannot
12442 -- handle these. For now, just return the access type itself.
12444 if Desig_Type = Current_Scope
12445 and then No (Def_Id)
12446 then
12447 Set_Ekind (Desig_Subtype, E_Record_Subtype);
12448 Def_Id := Entity (Subtype_Mark (S));
12450 -- This call added to ensure that the constraint is analyzed
12451 -- (needed for a B test). Note that we still return early from
12452 -- this procedure to avoid recursive processing. ???
12454 Constrain_Discriminated_Type
12455 (Desig_Subtype, S, Related_Nod, For_Access => True);
12456 return;
12457 end if;
12459 -- Enforce rule that the constraint is illegal if there is an
12460 -- unconstrained view of the designated type. This means that the
12461 -- partial view (either a private type declaration or a derivation
12462 -- from a private type) has no discriminants. (Defect Report
12463 -- 8652/0008, Technical Corrigendum 1, checked by ACATS B371001).
12465 -- Rule updated for Ada 2005: The private type is said to have
12466 -- a constrained partial view, given that objects of the type
12467 -- can be declared. Furthermore, the rule applies to all access
12468 -- types, unlike the rule concerning default discriminants (see
12469 -- RM 3.7.1(7/3))
12471 if (Ekind (T) = E_General_Access_Type or else Ada_Version >= Ada_2005)
12472 and then Has_Private_Declaration (Desig_Type)
12473 and then In_Open_Scopes (Scope (Desig_Type))
12474 and then Has_Discriminants (Desig_Type)
12475 then
12476 declare
12477 Pack : constant Node_Id :=
12478 Unit_Declaration_Node (Scope (Desig_Type));
12479 Decls : List_Id;
12480 Decl : Node_Id;
12482 begin
12483 if Nkind (Pack) = N_Package_Declaration then
12484 Decls := Visible_Declarations (Specification (Pack));
12485 Decl := First (Decls);
12486 while Present (Decl) loop
12487 if (Nkind (Decl) = N_Private_Type_Declaration
12488 and then Chars (Defining_Identifier (Decl)) =
12489 Chars (Desig_Type))
12491 or else
12492 (Nkind (Decl) = N_Full_Type_Declaration
12493 and then
12494 Chars (Defining_Identifier (Decl)) =
12495 Chars (Desig_Type)
12496 and then Is_Derived_Type (Desig_Type)
12497 and then
12498 Has_Private_Declaration (Etype (Desig_Type)))
12499 then
12500 if No (Discriminant_Specifications (Decl)) then
12501 Error_Msg_N
12502 ("cannot constrain access type if designated "
12503 & "type has constrained partial view", S);
12504 end if;
12506 exit;
12507 end if;
12509 Next (Decl);
12510 end loop;
12511 end if;
12512 end;
12513 end if;
12515 Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
12516 For_Access => True);
12518 elsif Is_Concurrent_Type (Desig_Type)
12519 and then not Is_Constrained (Desig_Type)
12520 then
12521 Constrain_Concurrent (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
12523 else
12524 Error_Msg_N ("invalid constraint on access type", S);
12526 -- We simply ignore an invalid constraint
12528 Desig_Subtype := Desig_Type;
12529 Constraint_OK := False;
12530 end if;
12532 if No (Def_Id) then
12533 Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
12534 else
12535 Set_Ekind (Def_Id, E_Access_Subtype);
12536 end if;
12538 if Constraint_OK then
12539 Set_Etype (Def_Id, Base_Type (T));
12541 if Is_Private_Type (Desig_Type) then
12542 Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
12543 end if;
12544 else
12545 Set_Etype (Def_Id, Any_Type);
12546 end if;
12548 Set_Size_Info (Def_Id, T);
12549 Set_Is_Constrained (Def_Id, Constraint_OK);
12550 Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
12551 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
12552 Set_Is_Access_Constant (Def_Id, Is_Access_Constant (T));
12554 Conditional_Delay (Def_Id, T);
12556 -- AI-363 : Subtypes of general access types whose designated types have
12557 -- default discriminants are disallowed. In instances, the rule has to
12558 -- be checked against the actual, of which T is the subtype. In a
12559 -- generic body, the rule is checked assuming that the actual type has
12560 -- defaulted discriminants.
12562 if Ada_Version >= Ada_2005 or else Warn_On_Ada_2005_Compatibility then
12563 if Ekind (Base_Type (T)) = E_General_Access_Type
12564 and then Has_Defaulted_Discriminants (Desig_Type)
12565 then
12566 if Ada_Version < Ada_2005 then
12567 Error_Msg_N
12568 ("access subtype of general access type would not " &
12569 "be allowed in Ada 2005?y?", S);
12570 else
12571 Error_Msg_N
12572 ("access subtype of general access type not allowed", S);
12573 end if;
12575 Error_Msg_N ("\discriminants have defaults", S);
12577 elsif Is_Access_Type (T)
12578 and then Is_Generic_Type (Desig_Type)
12579 and then Has_Discriminants (Desig_Type)
12580 and then In_Package_Body (Current_Scope)
12581 then
12582 if Ada_Version < Ada_2005 then
12583 Error_Msg_N
12584 ("access subtype would not be allowed in generic body "
12585 & "in Ada 2005?y?", S);
12586 else
12587 Error_Msg_N
12588 ("access subtype not allowed in generic body", S);
12589 end if;
12591 Error_Msg_N
12592 ("\designated type is a discriminated formal", S);
12593 end if;
12594 end if;
12595 end Constrain_Access;
12597 ---------------------
12598 -- Constrain_Array --
12599 ---------------------
12601 procedure Constrain_Array
12602 (Def_Id : in out Entity_Id;
12603 SI : Node_Id;
12604 Related_Nod : Node_Id;
12605 Related_Id : Entity_Id;
12606 Suffix : Character)
12608 C : constant Node_Id := Constraint (SI);
12609 Number_Of_Constraints : Nat := 0;
12610 Index : Node_Id;
12611 S, T : Entity_Id;
12612 Constraint_OK : Boolean := True;
12614 begin
12615 T := Entity (Subtype_Mark (SI));
12617 if Is_Access_Type (T) then
12618 T := Designated_Type (T);
12619 end if;
12621 -- If an index constraint follows a subtype mark in a subtype indication
12622 -- then the type or subtype denoted by the subtype mark must not already
12623 -- impose an index constraint. The subtype mark must denote either an
12624 -- unconstrained array type or an access type whose designated type
12625 -- is such an array type... (RM 3.6.1)
12627 if Is_Constrained (T) then
12628 Error_Msg_N ("array type is already constrained", Subtype_Mark (SI));
12629 Constraint_OK := False;
12631 else
12632 S := First (Constraints (C));
12633 while Present (S) loop
12634 Number_Of_Constraints := Number_Of_Constraints + 1;
12635 Next (S);
12636 end loop;
12638 -- In either case, the index constraint must provide a discrete
12639 -- range for each index of the array type and the type of each
12640 -- discrete range must be the same as that of the corresponding
12641 -- index. (RM 3.6.1)
12643 if Number_Of_Constraints /= Number_Dimensions (T) then
12644 Error_Msg_NE ("incorrect number of index constraints for }", C, T);
12645 Constraint_OK := False;
12647 else
12648 S := First (Constraints (C));
12649 Index := First_Index (T);
12650 Analyze (Index);
12652 -- Apply constraints to each index type
12654 for J in 1 .. Number_Of_Constraints loop
12655 Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
12656 Next (Index);
12657 Next (S);
12658 end loop;
12660 end if;
12661 end if;
12663 if No (Def_Id) then
12664 Def_Id :=
12665 Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
12666 Set_Parent (Def_Id, Related_Nod);
12668 else
12669 Set_Ekind (Def_Id, E_Array_Subtype);
12670 end if;
12672 Set_Size_Info (Def_Id, (T));
12673 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
12674 Set_Etype (Def_Id, Base_Type (T));
12676 if Constraint_OK then
12677 Set_First_Index (Def_Id, First (Constraints (C)));
12678 else
12679 Set_First_Index (Def_Id, First_Index (T));
12680 end if;
12682 Set_Is_Constrained (Def_Id, True);
12683 Set_Is_Aliased (Def_Id, Is_Aliased (T));
12684 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
12686 Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
12687 Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
12689 -- A subtype does not inherit the Packed_Array_Impl_Type of is parent.
12690 -- We need to initialize the attribute because if Def_Id is previously
12691 -- analyzed through a limited_with clause, it will have the attributes
12692 -- of an incomplete type, one of which is an Elist that overlaps the
12693 -- Packed_Array_Impl_Type field.
12695 Set_Packed_Array_Impl_Type (Def_Id, Empty);
12697 -- Build a freeze node if parent still needs one. Also make sure that
12698 -- the Depends_On_Private status is set because the subtype will need
12699 -- reprocessing at the time the base type does, and also we must set a
12700 -- conditional delay.
12702 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
12703 Conditional_Delay (Def_Id, T);
12704 end Constrain_Array;
12706 ------------------------------
12707 -- Constrain_Component_Type --
12708 ------------------------------
12710 function Constrain_Component_Type
12711 (Comp : Entity_Id;
12712 Constrained_Typ : Entity_Id;
12713 Related_Node : Node_Id;
12714 Typ : Entity_Id;
12715 Constraints : Elist_Id) return Entity_Id
12717 Loc : constant Source_Ptr := Sloc (Constrained_Typ);
12718 Compon_Type : constant Entity_Id := Etype (Comp);
12720 function Build_Constrained_Array_Type
12721 (Old_Type : Entity_Id) return Entity_Id;
12722 -- If Old_Type is an array type, one of whose indexes is constrained
12723 -- by a discriminant, build an Itype whose constraint replaces the
12724 -- discriminant with its value in the constraint.
12726 function Build_Constrained_Discriminated_Type
12727 (Old_Type : Entity_Id) return Entity_Id;
12728 -- Ditto for record components
12730 function Build_Constrained_Access_Type
12731 (Old_Type : Entity_Id) return Entity_Id;
12732 -- Ditto for access types. Makes use of previous two functions, to
12733 -- constrain designated type.
12735 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id;
12736 -- T is an array or discriminated type, C is a list of constraints
12737 -- that apply to T. This routine builds the constrained subtype.
12739 function Is_Discriminant (Expr : Node_Id) return Boolean;
12740 -- Returns True if Expr is a discriminant
12742 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id;
12743 -- Find the value of discriminant Discrim in Constraint
12745 -----------------------------------
12746 -- Build_Constrained_Access_Type --
12747 -----------------------------------
12749 function Build_Constrained_Access_Type
12750 (Old_Type : Entity_Id) return Entity_Id
12752 Desig_Type : constant Entity_Id := Designated_Type (Old_Type);
12753 Itype : Entity_Id;
12754 Desig_Subtype : Entity_Id;
12755 Scop : Entity_Id;
12757 begin
12758 -- if the original access type was not embedded in the enclosing
12759 -- type definition, there is no need to produce a new access
12760 -- subtype. In fact every access type with an explicit constraint
12761 -- generates an itype whose scope is the enclosing record.
12763 if not Is_Type (Scope (Old_Type)) then
12764 return Old_Type;
12766 elsif Is_Array_Type (Desig_Type) then
12767 Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
12769 elsif Has_Discriminants (Desig_Type) then
12771 -- This may be an access type to an enclosing record type for
12772 -- which we are constructing the constrained components. Return
12773 -- the enclosing record subtype. This is not always correct,
12774 -- but avoids infinite recursion. ???
12776 Desig_Subtype := Any_Type;
12778 for J in reverse 0 .. Scope_Stack.Last loop
12779 Scop := Scope_Stack.Table (J).Entity;
12781 if Is_Type (Scop)
12782 and then Base_Type (Scop) = Base_Type (Desig_Type)
12783 then
12784 Desig_Subtype := Scop;
12785 end if;
12787 exit when not Is_Type (Scop);
12788 end loop;
12790 if Desig_Subtype = Any_Type then
12791 Desig_Subtype :=
12792 Build_Constrained_Discriminated_Type (Desig_Type);
12793 end if;
12795 else
12796 return Old_Type;
12797 end if;
12799 if Desig_Subtype /= Desig_Type then
12801 -- The Related_Node better be here or else we won't be able
12802 -- to attach new itypes to a node in the tree.
12804 pragma Assert (Present (Related_Node));
12806 Itype := Create_Itype (E_Access_Subtype, Related_Node);
12808 Set_Etype (Itype, Base_Type (Old_Type));
12809 Set_Size_Info (Itype, (Old_Type));
12810 Set_Directly_Designated_Type (Itype, Desig_Subtype);
12811 Set_Depends_On_Private (Itype, Has_Private_Component
12812 (Old_Type));
12813 Set_Is_Access_Constant (Itype, Is_Access_Constant
12814 (Old_Type));
12816 -- The new itype needs freezing when it depends on a not frozen
12817 -- type and the enclosing subtype needs freezing.
12819 if Has_Delayed_Freeze (Constrained_Typ)
12820 and then not Is_Frozen (Constrained_Typ)
12821 then
12822 Conditional_Delay (Itype, Base_Type (Old_Type));
12823 end if;
12825 return Itype;
12827 else
12828 return Old_Type;
12829 end if;
12830 end Build_Constrained_Access_Type;
12832 ----------------------------------
12833 -- Build_Constrained_Array_Type --
12834 ----------------------------------
12836 function Build_Constrained_Array_Type
12837 (Old_Type : Entity_Id) return Entity_Id
12839 Lo_Expr : Node_Id;
12840 Hi_Expr : Node_Id;
12841 Old_Index : Node_Id;
12842 Range_Node : Node_Id;
12843 Constr_List : List_Id;
12845 Need_To_Create_Itype : Boolean := False;
12847 begin
12848 Old_Index := First_Index (Old_Type);
12849 while Present (Old_Index) loop
12850 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
12852 if Is_Discriminant (Lo_Expr)
12853 or else
12854 Is_Discriminant (Hi_Expr)
12855 then
12856 Need_To_Create_Itype := True;
12857 end if;
12859 Next_Index (Old_Index);
12860 end loop;
12862 if Need_To_Create_Itype then
12863 Constr_List := New_List;
12865 Old_Index := First_Index (Old_Type);
12866 while Present (Old_Index) loop
12867 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
12869 if Is_Discriminant (Lo_Expr) then
12870 Lo_Expr := Get_Discr_Value (Lo_Expr);
12871 end if;
12873 if Is_Discriminant (Hi_Expr) then
12874 Hi_Expr := Get_Discr_Value (Hi_Expr);
12875 end if;
12877 Range_Node :=
12878 Make_Range
12879 (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
12881 Append (Range_Node, To => Constr_List);
12883 Next_Index (Old_Index);
12884 end loop;
12886 return Build_Subtype (Old_Type, Constr_List);
12888 else
12889 return Old_Type;
12890 end if;
12891 end Build_Constrained_Array_Type;
12893 ------------------------------------------
12894 -- Build_Constrained_Discriminated_Type --
12895 ------------------------------------------
12897 function Build_Constrained_Discriminated_Type
12898 (Old_Type : Entity_Id) return Entity_Id
12900 Expr : Node_Id;
12901 Constr_List : List_Id;
12902 Old_Constraint : Elmt_Id;
12904 Need_To_Create_Itype : Boolean := False;
12906 begin
12907 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
12908 while Present (Old_Constraint) loop
12909 Expr := Node (Old_Constraint);
12911 if Is_Discriminant (Expr) then
12912 Need_To_Create_Itype := True;
12913 end if;
12915 Next_Elmt (Old_Constraint);
12916 end loop;
12918 if Need_To_Create_Itype then
12919 Constr_List := New_List;
12921 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
12922 while Present (Old_Constraint) loop
12923 Expr := Node (Old_Constraint);
12925 if Is_Discriminant (Expr) then
12926 Expr := Get_Discr_Value (Expr);
12927 end if;
12929 Append (New_Copy_Tree (Expr), To => Constr_List);
12931 Next_Elmt (Old_Constraint);
12932 end loop;
12934 return Build_Subtype (Old_Type, Constr_List);
12936 else
12937 return Old_Type;
12938 end if;
12939 end Build_Constrained_Discriminated_Type;
12941 -------------------
12942 -- Build_Subtype --
12943 -------------------
12945 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id is
12946 Indic : Node_Id;
12947 Subtyp_Decl : Node_Id;
12948 Def_Id : Entity_Id;
12949 Btyp : Entity_Id := Base_Type (T);
12951 begin
12952 -- The Related_Node better be here or else we won't be able to
12953 -- attach new itypes to a node in the tree.
12955 pragma Assert (Present (Related_Node));
12957 -- If the view of the component's type is incomplete or private
12958 -- with unknown discriminants, then the constraint must be applied
12959 -- to the full type.
12961 if Has_Unknown_Discriminants (Btyp)
12962 and then Present (Underlying_Type (Btyp))
12963 then
12964 Btyp := Underlying_Type (Btyp);
12965 end if;
12967 Indic :=
12968 Make_Subtype_Indication (Loc,
12969 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
12970 Constraint => Make_Index_Or_Discriminant_Constraint (Loc, C));
12972 Def_Id := Create_Itype (Ekind (T), Related_Node);
12974 Subtyp_Decl :=
12975 Make_Subtype_Declaration (Loc,
12976 Defining_Identifier => Def_Id,
12977 Subtype_Indication => Indic);
12979 Set_Parent (Subtyp_Decl, Parent (Related_Node));
12981 -- Itypes must be analyzed with checks off (see package Itypes)
12983 Analyze (Subtyp_Decl, Suppress => All_Checks);
12985 return Def_Id;
12986 end Build_Subtype;
12988 ---------------------
12989 -- Get_Discr_Value --
12990 ---------------------
12992 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id is
12993 D : Entity_Id;
12994 E : Elmt_Id;
12996 begin
12997 -- The discriminant may be declared for the type, in which case we
12998 -- find it by iterating over the list of discriminants. If the
12999 -- discriminant is inherited from a parent type, it appears as the
13000 -- corresponding discriminant of the current type. This will be the
13001 -- case when constraining an inherited component whose constraint is
13002 -- given by a discriminant of the parent.
13004 D := First_Discriminant (Typ);
13005 E := First_Elmt (Constraints);
13007 while Present (D) loop
13008 if D = Entity (Discrim)
13009 or else D = CR_Discriminant (Entity (Discrim))
13010 or else Corresponding_Discriminant (D) = Entity (Discrim)
13011 then
13012 return Node (E);
13013 end if;
13015 Next_Discriminant (D);
13016 Next_Elmt (E);
13017 end loop;
13019 -- The Corresponding_Discriminant mechanism is incomplete, because
13020 -- the correspondence between new and old discriminants is not one
13021 -- to one: one new discriminant can constrain several old ones. In
13022 -- that case, scan sequentially the stored_constraint, the list of
13023 -- discriminants of the parents, and the constraints.
13025 -- Previous code checked for the present of the Stored_Constraint
13026 -- list for the derived type, but did not use it at all. Should it
13027 -- be present when the component is a discriminated task type?
13029 if Is_Derived_Type (Typ)
13030 and then Scope (Entity (Discrim)) = Etype (Typ)
13031 then
13032 D := First_Discriminant (Etype (Typ));
13033 E := First_Elmt (Constraints);
13034 while Present (D) loop
13035 if D = Entity (Discrim) then
13036 return Node (E);
13037 end if;
13039 Next_Discriminant (D);
13040 Next_Elmt (E);
13041 end loop;
13042 end if;
13044 -- Something is wrong if we did not find the value
13046 raise Program_Error;
13047 end Get_Discr_Value;
13049 ---------------------
13050 -- Is_Discriminant --
13051 ---------------------
13053 function Is_Discriminant (Expr : Node_Id) return Boolean is
13054 Discrim_Scope : Entity_Id;
13056 begin
13057 if Denotes_Discriminant (Expr) then
13058 Discrim_Scope := Scope (Entity (Expr));
13060 -- Either we have a reference to one of Typ's discriminants,
13062 pragma Assert (Discrim_Scope = Typ
13064 -- or to the discriminants of the parent type, in the case
13065 -- of a derivation of a tagged type with variants.
13067 or else Discrim_Scope = Etype (Typ)
13068 or else Full_View (Discrim_Scope) = Etype (Typ)
13070 -- or same as above for the case where the discriminants
13071 -- were declared in Typ's private view.
13073 or else (Is_Private_Type (Discrim_Scope)
13074 and then Chars (Discrim_Scope) = Chars (Typ))
13076 -- or else we are deriving from the full view and the
13077 -- discriminant is declared in the private entity.
13079 or else (Is_Private_Type (Typ)
13080 and then Chars (Discrim_Scope) = Chars (Typ))
13082 -- Or we are constrained the corresponding record of a
13083 -- synchronized type that completes a private declaration.
13085 or else (Is_Concurrent_Record_Type (Typ)
13086 and then
13087 Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
13089 -- or we have a class-wide type, in which case make sure the
13090 -- discriminant found belongs to the root type.
13092 or else (Is_Class_Wide_Type (Typ)
13093 and then Etype (Typ) = Discrim_Scope));
13095 return True;
13096 end if;
13098 -- In all other cases we have something wrong
13100 return False;
13101 end Is_Discriminant;
13103 -- Start of processing for Constrain_Component_Type
13105 begin
13106 if Nkind (Parent (Comp)) = N_Component_Declaration
13107 and then Comes_From_Source (Parent (Comp))
13108 and then Comes_From_Source
13109 (Subtype_Indication (Component_Definition (Parent (Comp))))
13110 and then
13111 Is_Entity_Name
13112 (Subtype_Indication (Component_Definition (Parent (Comp))))
13113 then
13114 return Compon_Type;
13116 elsif Is_Array_Type (Compon_Type) then
13117 return Build_Constrained_Array_Type (Compon_Type);
13119 elsif Has_Discriminants (Compon_Type) then
13120 return Build_Constrained_Discriminated_Type (Compon_Type);
13122 elsif Is_Access_Type (Compon_Type) then
13123 return Build_Constrained_Access_Type (Compon_Type);
13125 else
13126 return Compon_Type;
13127 end if;
13128 end Constrain_Component_Type;
13130 --------------------------
13131 -- Constrain_Concurrent --
13132 --------------------------
13134 -- For concurrent types, the associated record value type carries the same
13135 -- discriminants, so when we constrain a concurrent type, we must constrain
13136 -- the corresponding record type as well.
13138 procedure Constrain_Concurrent
13139 (Def_Id : in out Entity_Id;
13140 SI : Node_Id;
13141 Related_Nod : Node_Id;
13142 Related_Id : Entity_Id;
13143 Suffix : Character)
13145 -- Retrieve Base_Type to ensure getting to the concurrent type in the
13146 -- case of a private subtype (needed when only doing semantic analysis).
13148 T_Ent : Entity_Id := Base_Type (Entity (Subtype_Mark (SI)));
13149 T_Val : Entity_Id;
13151 begin
13152 if Is_Access_Type (T_Ent) then
13153 T_Ent := Designated_Type (T_Ent);
13154 end if;
13156 T_Val := Corresponding_Record_Type (T_Ent);
13158 if Present (T_Val) then
13160 if No (Def_Id) then
13161 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
13163 -- Elaborate itype now, as it may be used in a subsequent
13164 -- synchronized operation in another scope.
13166 if Nkind (Related_Nod) = N_Full_Type_Declaration then
13167 Build_Itype_Reference (Def_Id, Related_Nod);
13168 end if;
13169 end if;
13171 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
13173 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
13174 Set_Corresponding_Record_Type (Def_Id,
13175 Constrain_Corresponding_Record (Def_Id, T_Val, Related_Nod));
13177 else
13178 -- If there is no associated record, expansion is disabled and this
13179 -- is a generic context. Create a subtype in any case, so that
13180 -- semantic analysis can proceed.
13182 if No (Def_Id) then
13183 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
13184 end if;
13186 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
13187 end if;
13188 end Constrain_Concurrent;
13190 ------------------------------------
13191 -- Constrain_Corresponding_Record --
13192 ------------------------------------
13194 function Constrain_Corresponding_Record
13195 (Prot_Subt : Entity_Id;
13196 Corr_Rec : Entity_Id;
13197 Related_Nod : Node_Id) return Entity_Id
13199 T_Sub : constant Entity_Id :=
13200 Create_Itype (E_Record_Subtype, Related_Nod, Corr_Rec, 'C');
13202 begin
13203 Set_Etype (T_Sub, Corr_Rec);
13204 Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
13205 Set_Is_Constrained (T_Sub, True);
13206 Set_First_Entity (T_Sub, First_Entity (Corr_Rec));
13207 Set_Last_Entity (T_Sub, Last_Entity (Corr_Rec));
13209 if Has_Discriminants (Prot_Subt) then -- False only if errors.
13210 Set_Discriminant_Constraint
13211 (T_Sub, Discriminant_Constraint (Prot_Subt));
13212 Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
13213 Create_Constrained_Components
13214 (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
13215 end if;
13217 Set_Depends_On_Private (T_Sub, Has_Private_Component (T_Sub));
13219 if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
13220 Conditional_Delay (T_Sub, Corr_Rec);
13222 else
13223 -- This is a component subtype: it will be frozen in the context of
13224 -- the enclosing record's init_proc, so that discriminant references
13225 -- are resolved to discriminals. (Note: we used to skip freezing
13226 -- altogether in that case, which caused errors downstream for
13227 -- components of a bit packed array type).
13229 Set_Has_Delayed_Freeze (T_Sub);
13230 end if;
13232 return T_Sub;
13233 end Constrain_Corresponding_Record;
13235 -----------------------
13236 -- Constrain_Decimal --
13237 -----------------------
13239 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id) is
13240 T : constant Entity_Id := Entity (Subtype_Mark (S));
13241 C : constant Node_Id := Constraint (S);
13242 Loc : constant Source_Ptr := Sloc (C);
13243 Range_Expr : Node_Id;
13244 Digits_Expr : Node_Id;
13245 Digits_Val : Uint;
13246 Bound_Val : Ureal;
13248 begin
13249 Set_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
13251 if Nkind (C) = N_Range_Constraint then
13252 Range_Expr := Range_Expression (C);
13253 Digits_Val := Digits_Value (T);
13255 else
13256 pragma Assert (Nkind (C) = N_Digits_Constraint);
13258 Check_SPARK_05_Restriction ("digits constraint is not allowed", S);
13260 Digits_Expr := Digits_Expression (C);
13261 Analyze_And_Resolve (Digits_Expr, Any_Integer);
13263 Check_Digits_Expression (Digits_Expr);
13264 Digits_Val := Expr_Value (Digits_Expr);
13266 if Digits_Val > Digits_Value (T) then
13267 Error_Msg_N
13268 ("digits expression is incompatible with subtype", C);
13269 Digits_Val := Digits_Value (T);
13270 end if;
13272 if Present (Range_Constraint (C)) then
13273 Range_Expr := Range_Expression (Range_Constraint (C));
13274 else
13275 Range_Expr := Empty;
13276 end if;
13277 end if;
13279 Set_Etype (Def_Id, Base_Type (T));
13280 Set_Size_Info (Def_Id, (T));
13281 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13282 Set_Delta_Value (Def_Id, Delta_Value (T));
13283 Set_Scale_Value (Def_Id, Scale_Value (T));
13284 Set_Small_Value (Def_Id, Small_Value (T));
13285 Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
13286 Set_Digits_Value (Def_Id, Digits_Val);
13288 -- Manufacture range from given digits value if no range present
13290 if No (Range_Expr) then
13291 Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
13292 Range_Expr :=
13293 Make_Range (Loc,
13294 Low_Bound =>
13295 Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
13296 High_Bound =>
13297 Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
13298 end if;
13300 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
13301 Set_Discrete_RM_Size (Def_Id);
13303 -- Unconditionally delay the freeze, since we cannot set size
13304 -- information in all cases correctly until the freeze point.
13306 Set_Has_Delayed_Freeze (Def_Id);
13307 end Constrain_Decimal;
13309 ----------------------------------
13310 -- Constrain_Discriminated_Type --
13311 ----------------------------------
13313 procedure Constrain_Discriminated_Type
13314 (Def_Id : Entity_Id;
13315 S : Node_Id;
13316 Related_Nod : Node_Id;
13317 For_Access : Boolean := False)
13319 E : Entity_Id := Entity (Subtype_Mark (S));
13320 T : Entity_Id;
13322 procedure Fixup_Bad_Constraint;
13323 -- Called after finding a bad constraint, and after having posted an
13324 -- appropriate error message. The goal is to leave type Def_Id in as
13325 -- reasonable state as possible.
13327 --------------------------
13328 -- Fixup_Bad_Constraint --
13329 --------------------------
13331 procedure Fixup_Bad_Constraint is
13332 begin
13333 -- Set a reasonable Ekind for the entity. For an incomplete type,
13334 -- we can't do much, but for other types, we can set the proper
13335 -- corresponding subtype kind.
13337 if Ekind (T) = E_Incomplete_Type then
13338 Set_Ekind (Def_Id, Ekind (T));
13339 else
13340 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
13341 end if;
13343 -- Set Etype to the known type, to reduce chances of cascaded errors
13345 Set_Etype (Def_Id, E);
13346 Set_Error_Posted (Def_Id);
13347 end Fixup_Bad_Constraint;
13349 -- Local variables
13351 C : Node_Id;
13352 Constr : Elist_Id := New_Elmt_List;
13354 -- Start of processing for Constrain_Discriminated_Type
13356 begin
13357 C := Constraint (S);
13359 -- A discriminant constraint is only allowed in a subtype indication,
13360 -- after a subtype mark. This subtype mark must denote either a type
13361 -- with discriminants, or an access type whose designated type is a
13362 -- type with discriminants. A discriminant constraint specifies the
13363 -- values of these discriminants (RM 3.7.2(5)).
13365 T := Base_Type (Entity (Subtype_Mark (S)));
13367 if Is_Access_Type (T) then
13368 T := Designated_Type (T);
13369 end if;
13371 -- In an instance it may be necessary to retrieve the full view of a
13372 -- type with unknown discriminants, or a full view with defaulted
13373 -- discriminants. In other contexts the constraint is illegal.
13375 if In_Instance
13376 and then Is_Private_Type (T)
13377 and then Present (Full_View (T))
13378 and then
13379 (Has_Unknown_Discriminants (T)
13380 or else
13381 (not Has_Discriminants (T)
13382 and then Has_Discriminants (Full_View (T))
13383 and then Present (Discriminant_Default_Value
13384 (First_Discriminant (Full_View (T))))))
13385 then
13386 T := Full_View (T);
13387 E := Full_View (E);
13388 end if;
13390 -- Ada 2005 (AI-412): Constrained incomplete subtypes are illegal. Avoid
13391 -- generating an error for access-to-incomplete subtypes.
13393 if Ada_Version >= Ada_2005
13394 and then Ekind (T) = E_Incomplete_Type
13395 and then Nkind (Parent (S)) = N_Subtype_Declaration
13396 and then not Is_Itype (Def_Id)
13397 then
13398 -- A little sanity check: emit an error message if the type has
13399 -- discriminants to begin with. Type T may be a regular incomplete
13400 -- type or imported via a limited with clause.
13402 if Has_Discriminants (T)
13403 or else (From_Limited_With (T)
13404 and then Present (Non_Limited_View (T))
13405 and then Nkind (Parent (Non_Limited_View (T))) =
13406 N_Full_Type_Declaration
13407 and then Present (Discriminant_Specifications
13408 (Parent (Non_Limited_View (T)))))
13409 then
13410 Error_Msg_N
13411 ("(Ada 2005) incomplete subtype may not be constrained", C);
13412 else
13413 Error_Msg_N ("invalid constraint: type has no discriminant", C);
13414 end if;
13416 Fixup_Bad_Constraint;
13417 return;
13419 -- Check that the type has visible discriminants. The type may be
13420 -- a private type with unknown discriminants whose full view has
13421 -- discriminants which are invisible.
13423 elsif not Has_Discriminants (T)
13424 or else
13425 (Has_Unknown_Discriminants (T)
13426 and then Is_Private_Type (T))
13427 then
13428 Error_Msg_N ("invalid constraint: type has no discriminant", C);
13429 Fixup_Bad_Constraint;
13430 return;
13432 elsif Is_Constrained (E)
13433 or else (Ekind (E) = E_Class_Wide_Subtype
13434 and then Present (Discriminant_Constraint (E)))
13435 then
13436 Error_Msg_N ("type is already constrained", Subtype_Mark (S));
13437 Fixup_Bad_Constraint;
13438 return;
13439 end if;
13441 -- T may be an unconstrained subtype (e.g. a generic actual). Constraint
13442 -- applies to the base type.
13444 T := Base_Type (T);
13446 Constr := Build_Discriminant_Constraints (T, S);
13448 -- If the list returned was empty we had an error in building the
13449 -- discriminant constraint. We have also already signalled an error
13450 -- in the incomplete type case
13452 if Is_Empty_Elmt_List (Constr) then
13453 Fixup_Bad_Constraint;
13454 return;
13455 end if;
13457 Build_Discriminated_Subtype (T, Def_Id, Constr, Related_Nod, For_Access);
13458 end Constrain_Discriminated_Type;
13460 ---------------------------
13461 -- Constrain_Enumeration --
13462 ---------------------------
13464 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id) is
13465 T : constant Entity_Id := Entity (Subtype_Mark (S));
13466 C : constant Node_Id := Constraint (S);
13468 begin
13469 Set_Ekind (Def_Id, E_Enumeration_Subtype);
13471 Set_First_Literal (Def_Id, First_Literal (Base_Type (T)));
13473 Set_Etype (Def_Id, Base_Type (T));
13474 Set_Size_Info (Def_Id, (T));
13475 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13476 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
13478 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13480 Set_Discrete_RM_Size (Def_Id);
13481 end Constrain_Enumeration;
13483 ----------------------
13484 -- Constrain_Float --
13485 ----------------------
13487 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id) is
13488 T : constant Entity_Id := Entity (Subtype_Mark (S));
13489 C : Node_Id;
13490 D : Node_Id;
13491 Rais : Node_Id;
13493 begin
13494 Set_Ekind (Def_Id, E_Floating_Point_Subtype);
13496 Set_Etype (Def_Id, Base_Type (T));
13497 Set_Size_Info (Def_Id, (T));
13498 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13500 -- Process the constraint
13502 C := Constraint (S);
13504 -- Digits constraint present
13506 if Nkind (C) = N_Digits_Constraint then
13508 Check_SPARK_05_Restriction ("digits constraint is not allowed", S);
13509 Check_Restriction (No_Obsolescent_Features, C);
13511 if Warn_On_Obsolescent_Feature then
13512 Error_Msg_N
13513 ("subtype digits constraint is an " &
13514 "obsolescent feature (RM J.3(8))?j?", C);
13515 end if;
13517 D := Digits_Expression (C);
13518 Analyze_And_Resolve (D, Any_Integer);
13519 Check_Digits_Expression (D);
13520 Set_Digits_Value (Def_Id, Expr_Value (D));
13522 -- Check that digits value is in range. Obviously we can do this
13523 -- at compile time, but it is strictly a runtime check, and of
13524 -- course there is an ACVC test that checks this.
13526 if Digits_Value (Def_Id) > Digits_Value (T) then
13527 Error_Msg_Uint_1 := Digits_Value (T);
13528 Error_Msg_N ("??digits value is too large, maximum is ^", D);
13529 Rais :=
13530 Make_Raise_Constraint_Error (Sloc (D),
13531 Reason => CE_Range_Check_Failed);
13532 Insert_Action (Declaration_Node (Def_Id), Rais);
13533 end if;
13535 C := Range_Constraint (C);
13537 -- No digits constraint present
13539 else
13540 Set_Digits_Value (Def_Id, Digits_Value (T));
13541 end if;
13543 -- Range constraint present
13545 if Nkind (C) = N_Range_Constraint then
13546 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13548 -- No range constraint present
13550 else
13551 pragma Assert (No (C));
13552 Set_Scalar_Range (Def_Id, Scalar_Range (T));
13553 end if;
13555 Set_Is_Constrained (Def_Id);
13556 end Constrain_Float;
13558 ---------------------
13559 -- Constrain_Index --
13560 ---------------------
13562 procedure Constrain_Index
13563 (Index : Node_Id;
13564 S : Node_Id;
13565 Related_Nod : Node_Id;
13566 Related_Id : Entity_Id;
13567 Suffix : Character;
13568 Suffix_Index : Nat)
13570 Def_Id : Entity_Id;
13571 R : Node_Id := Empty;
13572 T : constant Entity_Id := Etype (Index);
13574 begin
13575 Def_Id :=
13576 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
13577 Set_Etype (Def_Id, Base_Type (T));
13579 if Nkind (S) = N_Range
13580 or else
13581 (Nkind (S) = N_Attribute_Reference
13582 and then Attribute_Name (S) = Name_Range)
13583 then
13584 -- A Range attribute will be transformed into N_Range by Resolve
13586 Analyze (S);
13587 Set_Etype (S, T);
13588 R := S;
13590 Process_Range_Expr_In_Decl (R, T);
13592 if not Error_Posted (S)
13593 and then
13594 (Nkind (S) /= N_Range
13595 or else not Covers (T, (Etype (Low_Bound (S))))
13596 or else not Covers (T, (Etype (High_Bound (S)))))
13597 then
13598 if Base_Type (T) /= Any_Type
13599 and then Etype (Low_Bound (S)) /= Any_Type
13600 and then Etype (High_Bound (S)) /= Any_Type
13601 then
13602 Error_Msg_N ("range expected", S);
13603 end if;
13604 end if;
13606 elsif Nkind (S) = N_Subtype_Indication then
13608 -- The parser has verified that this is a discrete indication
13610 Resolve_Discrete_Subtype_Indication (S, T);
13611 Bad_Predicated_Subtype_Use
13612 ("subtype& has predicate, not allowed in index constraint",
13613 S, Entity (Subtype_Mark (S)));
13615 R := Range_Expression (Constraint (S));
13617 -- Capture values of bounds and generate temporaries for them if
13618 -- needed, since checks may cause duplication of the expressions
13619 -- which must not be reevaluated.
13621 -- The forced evaluation removes side effects from expressions, which
13622 -- should occur also in GNATprove mode. Otherwise, we end up with
13623 -- unexpected insertions of actions at places where this is not
13624 -- supposed to occur, e.g. on default parameters of a call.
13626 if Expander_Active or GNATprove_Mode then
13627 Force_Evaluation
13628 (Low_Bound (R), Related_Id => Def_Id, Is_Low_Bound => True);
13629 Force_Evaluation
13630 (High_Bound (R), Related_Id => Def_Id, Is_High_Bound => True);
13631 end if;
13633 elsif Nkind (S) = N_Discriminant_Association then
13635 -- Syntactically valid in subtype indication
13637 Error_Msg_N ("invalid index constraint", S);
13638 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
13639 return;
13641 -- Subtype_Mark case, no anonymous subtypes to construct
13643 else
13644 Analyze (S);
13646 if Is_Entity_Name (S) then
13647 if not Is_Type (Entity (S)) then
13648 Error_Msg_N ("expect subtype mark for index constraint", S);
13650 elsif Base_Type (Entity (S)) /= Base_Type (T) then
13651 Wrong_Type (S, Base_Type (T));
13653 -- Check error of subtype with predicate in index constraint
13655 else
13656 Bad_Predicated_Subtype_Use
13657 ("subtype& has predicate, not allowed in index constraint",
13658 S, Entity (S));
13659 end if;
13661 return;
13663 else
13664 Error_Msg_N ("invalid index constraint", S);
13665 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
13666 return;
13667 end if;
13668 end if;
13670 -- Complete construction of the Itype
13672 if Is_Modular_Integer_Type (T) then
13673 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
13675 elsif Is_Integer_Type (T) then
13676 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
13678 else
13679 Set_Ekind (Def_Id, E_Enumeration_Subtype);
13680 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
13681 Set_First_Literal (Def_Id, First_Literal (T));
13682 end if;
13684 Set_Size_Info (Def_Id, (T));
13685 Set_RM_Size (Def_Id, RM_Size (T));
13686 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13688 Set_Scalar_Range (Def_Id, R);
13690 Set_Etype (S, Def_Id);
13691 Set_Discrete_RM_Size (Def_Id);
13692 end Constrain_Index;
13694 -----------------------
13695 -- Constrain_Integer --
13696 -----------------------
13698 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id) is
13699 T : constant Entity_Id := Entity (Subtype_Mark (S));
13700 C : constant Node_Id := Constraint (S);
13702 begin
13703 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13705 if Is_Modular_Integer_Type (T) then
13706 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
13707 else
13708 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
13709 end if;
13711 Set_Etype (Def_Id, Base_Type (T));
13712 Set_Size_Info (Def_Id, (T));
13713 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13714 Set_Discrete_RM_Size (Def_Id);
13715 end Constrain_Integer;
13717 ------------------------------
13718 -- Constrain_Ordinary_Fixed --
13719 ------------------------------
13721 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id) is
13722 T : constant Entity_Id := Entity (Subtype_Mark (S));
13723 C : Node_Id;
13724 D : Node_Id;
13725 Rais : Node_Id;
13727 begin
13728 Set_Ekind (Def_Id, E_Ordinary_Fixed_Point_Subtype);
13729 Set_Etype (Def_Id, Base_Type (T));
13730 Set_Size_Info (Def_Id, (T));
13731 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13732 Set_Small_Value (Def_Id, Small_Value (T));
13734 -- Process the constraint
13736 C := Constraint (S);
13738 -- Delta constraint present
13740 if Nkind (C) = N_Delta_Constraint then
13742 Check_SPARK_05_Restriction ("delta constraint is not allowed", S);
13743 Check_Restriction (No_Obsolescent_Features, C);
13745 if Warn_On_Obsolescent_Feature then
13746 Error_Msg_S
13747 ("subtype delta constraint is an " &
13748 "obsolescent feature (RM J.3(7))?j?");
13749 end if;
13751 D := Delta_Expression (C);
13752 Analyze_And_Resolve (D, Any_Real);
13753 Check_Delta_Expression (D);
13754 Set_Delta_Value (Def_Id, Expr_Value_R (D));
13756 -- Check that delta value is in range. Obviously we can do this
13757 -- at compile time, but it is strictly a runtime check, and of
13758 -- course there is an ACVC test that checks this.
13760 if Delta_Value (Def_Id) < Delta_Value (T) then
13761 Error_Msg_N ("??delta value is too small", D);
13762 Rais :=
13763 Make_Raise_Constraint_Error (Sloc (D),
13764 Reason => CE_Range_Check_Failed);
13765 Insert_Action (Declaration_Node (Def_Id), Rais);
13766 end if;
13768 C := Range_Constraint (C);
13770 -- No delta constraint present
13772 else
13773 Set_Delta_Value (Def_Id, Delta_Value (T));
13774 end if;
13776 -- Range constraint present
13778 if Nkind (C) = N_Range_Constraint then
13779 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13781 -- No range constraint present
13783 else
13784 pragma Assert (No (C));
13785 Set_Scalar_Range (Def_Id, Scalar_Range (T));
13786 end if;
13788 Set_Discrete_RM_Size (Def_Id);
13790 -- Unconditionally delay the freeze, since we cannot set size
13791 -- information in all cases correctly until the freeze point.
13793 Set_Has_Delayed_Freeze (Def_Id);
13794 end Constrain_Ordinary_Fixed;
13796 -----------------------
13797 -- Contain_Interface --
13798 -----------------------
13800 function Contain_Interface
13801 (Iface : Entity_Id;
13802 Ifaces : Elist_Id) return Boolean
13804 Iface_Elmt : Elmt_Id;
13806 begin
13807 if Present (Ifaces) then
13808 Iface_Elmt := First_Elmt (Ifaces);
13809 while Present (Iface_Elmt) loop
13810 if Node (Iface_Elmt) = Iface then
13811 return True;
13812 end if;
13814 Next_Elmt (Iface_Elmt);
13815 end loop;
13816 end if;
13818 return False;
13819 end Contain_Interface;
13821 ---------------------------
13822 -- Convert_Scalar_Bounds --
13823 ---------------------------
13825 procedure Convert_Scalar_Bounds
13826 (N : Node_Id;
13827 Parent_Type : Entity_Id;
13828 Derived_Type : Entity_Id;
13829 Loc : Source_Ptr)
13831 Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
13833 Lo : Node_Id;
13834 Hi : Node_Id;
13835 Rng : Node_Id;
13837 begin
13838 -- Defend against previous errors
13840 if No (Scalar_Range (Derived_Type)) then
13841 Check_Error_Detected;
13842 return;
13843 end if;
13845 Lo := Build_Scalar_Bound
13846 (Type_Low_Bound (Derived_Type),
13847 Parent_Type, Implicit_Base);
13849 Hi := Build_Scalar_Bound
13850 (Type_High_Bound (Derived_Type),
13851 Parent_Type, Implicit_Base);
13853 Rng :=
13854 Make_Range (Loc,
13855 Low_Bound => Lo,
13856 High_Bound => Hi);
13858 Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
13860 Set_Parent (Rng, N);
13861 Set_Scalar_Range (Derived_Type, Rng);
13863 -- Analyze the bounds
13865 Analyze_And_Resolve (Lo, Implicit_Base);
13866 Analyze_And_Resolve (Hi, Implicit_Base);
13868 -- Analyze the range itself, except that we do not analyze it if
13869 -- the bounds are real literals, and we have a fixed-point type.
13870 -- The reason for this is that we delay setting the bounds in this
13871 -- case till we know the final Small and Size values (see circuit
13872 -- in Freeze.Freeze_Fixed_Point_Type for further details).
13874 if Is_Fixed_Point_Type (Parent_Type)
13875 and then Nkind (Lo) = N_Real_Literal
13876 and then Nkind (Hi) = N_Real_Literal
13877 then
13878 return;
13880 -- Here we do the analysis of the range
13882 -- Note: we do this manually, since if we do a normal Analyze and
13883 -- Resolve call, there are problems with the conversions used for
13884 -- the derived type range.
13886 else
13887 Set_Etype (Rng, Implicit_Base);
13888 Set_Analyzed (Rng, True);
13889 end if;
13890 end Convert_Scalar_Bounds;
13892 -------------------
13893 -- Copy_And_Swap --
13894 -------------------
13896 procedure Copy_And_Swap (Priv, Full : Entity_Id) is
13897 begin
13898 -- Initialize new full declaration entity by copying the pertinent
13899 -- fields of the corresponding private declaration entity.
13901 -- We temporarily set Ekind to a value appropriate for a type to
13902 -- avoid assert failures in Einfo from checking for setting type
13903 -- attributes on something that is not a type. Ekind (Priv) is an
13904 -- appropriate choice, since it allowed the attributes to be set
13905 -- in the first place. This Ekind value will be modified later.
13907 Set_Ekind (Full, Ekind (Priv));
13909 -- Also set Etype temporarily to Any_Type, again, in the absence
13910 -- of errors, it will be properly reset, and if there are errors,
13911 -- then we want a value of Any_Type to remain.
13913 Set_Etype (Full, Any_Type);
13915 -- Now start copying attributes
13917 Set_Has_Discriminants (Full, Has_Discriminants (Priv));
13919 if Has_Discriminants (Full) then
13920 Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
13921 Set_Stored_Constraint (Full, Stored_Constraint (Priv));
13922 end if;
13924 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
13925 Set_Homonym (Full, Homonym (Priv));
13926 Set_Is_Immediately_Visible (Full, Is_Immediately_Visible (Priv));
13927 Set_Is_Public (Full, Is_Public (Priv));
13928 Set_Is_Pure (Full, Is_Pure (Priv));
13929 Set_Is_Tagged_Type (Full, Is_Tagged_Type (Priv));
13930 Set_Has_Pragma_Unmodified (Full, Has_Pragma_Unmodified (Priv));
13931 Set_Has_Pragma_Unreferenced (Full, Has_Pragma_Unreferenced (Priv));
13932 Set_Has_Pragma_Unreferenced_Objects
13933 (Full, Has_Pragma_Unreferenced_Objects
13934 (Priv));
13936 Conditional_Delay (Full, Priv);
13938 if Is_Tagged_Type (Full) then
13939 Set_Direct_Primitive_Operations
13940 (Full, Direct_Primitive_Operations (Priv));
13941 Set_No_Tagged_Streams_Pragma
13942 (Full, No_Tagged_Streams_Pragma (Priv));
13944 if Is_Base_Type (Priv) then
13945 Set_Class_Wide_Type (Full, Class_Wide_Type (Priv));
13946 end if;
13947 end if;
13949 Set_Is_Volatile (Full, Is_Volatile (Priv));
13950 Set_Treat_As_Volatile (Full, Treat_As_Volatile (Priv));
13951 Set_Scope (Full, Scope (Priv));
13952 Set_Next_Entity (Full, Next_Entity (Priv));
13953 Set_First_Entity (Full, First_Entity (Priv));
13954 Set_Last_Entity (Full, Last_Entity (Priv));
13956 -- If access types have been recorded for later handling, keep them in
13957 -- the full view so that they get handled when the full view freeze
13958 -- node is expanded.
13960 if Present (Freeze_Node (Priv))
13961 and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
13962 then
13963 Ensure_Freeze_Node (Full);
13964 Set_Access_Types_To_Process
13965 (Freeze_Node (Full),
13966 Access_Types_To_Process (Freeze_Node (Priv)));
13967 end if;
13969 -- Swap the two entities. Now Private is the full type entity and Full
13970 -- is the private one. They will be swapped back at the end of the
13971 -- private part. This swapping ensures that the entity that is visible
13972 -- in the private part is the full declaration.
13974 Exchange_Entities (Priv, Full);
13975 Append_Entity (Full, Scope (Full));
13976 end Copy_And_Swap;
13978 -------------------------------------
13979 -- Copy_Array_Base_Type_Attributes --
13980 -------------------------------------
13982 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
13983 begin
13984 Set_Component_Alignment (T1, Component_Alignment (T2));
13985 Set_Component_Type (T1, Component_Type (T2));
13986 Set_Component_Size (T1, Component_Size (T2));
13987 Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
13988 Set_Has_Non_Standard_Rep (T1, Has_Non_Standard_Rep (T2));
13989 Propagate_Concurrent_Flags (T1, T2);
13990 Set_Is_Packed (T1, Is_Packed (T2));
13991 Set_Has_Aliased_Components (T1, Has_Aliased_Components (T2));
13992 Set_Has_Atomic_Components (T1, Has_Atomic_Components (T2));
13993 Set_Has_Volatile_Components (T1, Has_Volatile_Components (T2));
13994 end Copy_Array_Base_Type_Attributes;
13996 -----------------------------------
13997 -- Copy_Array_Subtype_Attributes --
13998 -----------------------------------
14000 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
14001 begin
14002 Set_Size_Info (T1, T2);
14004 Set_First_Index (T1, First_Index (T2));
14005 Set_Is_Aliased (T1, Is_Aliased (T2));
14006 Set_Is_Volatile (T1, Is_Volatile (T2));
14007 Set_Treat_As_Volatile (T1, Treat_As_Volatile (T2));
14008 Set_Is_Constrained (T1, Is_Constrained (T2));
14009 Set_Depends_On_Private (T1, Has_Private_Component (T2));
14010 Inherit_Rep_Item_Chain (T1, T2);
14011 Set_Convention (T1, Convention (T2));
14012 Set_Is_Limited_Composite (T1, Is_Limited_Composite (T2));
14013 Set_Is_Private_Composite (T1, Is_Private_Composite (T2));
14014 Set_Packed_Array_Impl_Type (T1, Packed_Array_Impl_Type (T2));
14015 end Copy_Array_Subtype_Attributes;
14017 -----------------------------------
14018 -- Create_Constrained_Components --
14019 -----------------------------------
14021 procedure Create_Constrained_Components
14022 (Subt : Entity_Id;
14023 Decl_Node : Node_Id;
14024 Typ : Entity_Id;
14025 Constraints : Elist_Id)
14027 Loc : constant Source_Ptr := Sloc (Subt);
14028 Comp_List : constant Elist_Id := New_Elmt_List;
14029 Parent_Type : constant Entity_Id := Etype (Typ);
14030 Assoc_List : constant List_Id := New_List;
14031 Discr_Val : Elmt_Id;
14032 Errors : Boolean;
14033 New_C : Entity_Id;
14034 Old_C : Entity_Id;
14035 Is_Static : Boolean := True;
14037 procedure Collect_Fixed_Components (Typ : Entity_Id);
14038 -- Collect parent type components that do not appear in a variant part
14040 procedure Create_All_Components;
14041 -- Iterate over Comp_List to create the components of the subtype
14043 function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
14044 -- Creates a new component from Old_Compon, copying all the fields from
14045 -- it, including its Etype, inserts the new component in the Subt entity
14046 -- chain and returns the new component.
14048 function Is_Variant_Record (T : Entity_Id) return Boolean;
14049 -- If true, and discriminants are static, collect only components from
14050 -- variants selected by discriminant values.
14052 ------------------------------
14053 -- Collect_Fixed_Components --
14054 ------------------------------
14056 procedure Collect_Fixed_Components (Typ : Entity_Id) is
14057 begin
14058 -- Build association list for discriminants, and find components of the
14059 -- variant part selected by the values of the discriminants.
14061 Old_C := First_Discriminant (Typ);
14062 Discr_Val := First_Elmt (Constraints);
14063 while Present (Old_C) loop
14064 Append_To (Assoc_List,
14065 Make_Component_Association (Loc,
14066 Choices => New_List (New_Occurrence_Of (Old_C, Loc)),
14067 Expression => New_Copy (Node (Discr_Val))));
14069 Next_Elmt (Discr_Val);
14070 Next_Discriminant (Old_C);
14071 end loop;
14073 -- The tag and the possible parent component are unconditionally in
14074 -- the subtype.
14076 if Is_Tagged_Type (Typ) or else Has_Controlled_Component (Typ) then
14077 Old_C := First_Component (Typ);
14078 while Present (Old_C) loop
14079 if Nam_In (Chars (Old_C), Name_uTag, Name_uParent) then
14080 Append_Elmt (Old_C, Comp_List);
14081 end if;
14083 Next_Component (Old_C);
14084 end loop;
14085 end if;
14086 end Collect_Fixed_Components;
14088 ---------------------------
14089 -- Create_All_Components --
14090 ---------------------------
14092 procedure Create_All_Components is
14093 Comp : Elmt_Id;
14095 begin
14096 Comp := First_Elmt (Comp_List);
14097 while Present (Comp) loop
14098 Old_C := Node (Comp);
14099 New_C := Create_Component (Old_C);
14101 Set_Etype
14102 (New_C,
14103 Constrain_Component_Type
14104 (Old_C, Subt, Decl_Node, Typ, Constraints));
14105 Set_Is_Public (New_C, Is_Public (Subt));
14107 Next_Elmt (Comp);
14108 end loop;
14109 end Create_All_Components;
14111 ----------------------
14112 -- Create_Component --
14113 ----------------------
14115 function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
14116 New_Compon : constant Entity_Id := New_Copy (Old_Compon);
14118 begin
14119 if Ekind (Old_Compon) = E_Discriminant
14120 and then Is_Completely_Hidden (Old_Compon)
14121 then
14122 -- This is a shadow discriminant created for a discriminant of
14123 -- the parent type, which needs to be present in the subtype.
14124 -- Give the shadow discriminant an internal name that cannot
14125 -- conflict with that of visible components.
14127 Set_Chars (New_Compon, New_Internal_Name ('C'));
14128 end if;
14130 -- Set the parent so we have a proper link for freezing etc. This is
14131 -- not a real parent pointer, since of course our parent does not own
14132 -- up to us and reference us, we are an illegitimate child of the
14133 -- original parent.
14135 Set_Parent (New_Compon, Parent (Old_Compon));
14137 -- If the old component's Esize was already determined and is a
14138 -- static value, then the new component simply inherits it. Otherwise
14139 -- the old component's size may require run-time determination, but
14140 -- the new component's size still might be statically determinable
14141 -- (if, for example it has a static constraint). In that case we want
14142 -- Layout_Type to recompute the component's size, so we reset its
14143 -- size and positional fields.
14145 if Frontend_Layout_On_Target
14146 and then not Known_Static_Esize (Old_Compon)
14147 then
14148 Set_Esize (New_Compon, Uint_0);
14149 Init_Normalized_First_Bit (New_Compon);
14150 Init_Normalized_Position (New_Compon);
14151 Init_Normalized_Position_Max (New_Compon);
14152 end if;
14154 -- We do not want this node marked as Comes_From_Source, since
14155 -- otherwise it would get first class status and a separate cross-
14156 -- reference line would be generated. Illegitimate children do not
14157 -- rate such recognition.
14159 Set_Comes_From_Source (New_Compon, False);
14161 -- But it is a real entity, and a birth certificate must be properly
14162 -- registered by entering it into the entity list.
14164 Enter_Name (New_Compon);
14166 return New_Compon;
14167 end Create_Component;
14169 -----------------------
14170 -- Is_Variant_Record --
14171 -----------------------
14173 function Is_Variant_Record (T : Entity_Id) return Boolean is
14174 begin
14175 return Nkind (Parent (T)) = N_Full_Type_Declaration
14176 and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
14177 and then Present (Component_List (Type_Definition (Parent (T))))
14178 and then
14179 Present
14180 (Variant_Part (Component_List (Type_Definition (Parent (T)))));
14181 end Is_Variant_Record;
14183 -- Start of processing for Create_Constrained_Components
14185 begin
14186 pragma Assert (Subt /= Base_Type (Subt));
14187 pragma Assert (Typ = Base_Type (Typ));
14189 Set_First_Entity (Subt, Empty);
14190 Set_Last_Entity (Subt, Empty);
14192 -- Check whether constraint is fully static, in which case we can
14193 -- optimize the list of components.
14195 Discr_Val := First_Elmt (Constraints);
14196 while Present (Discr_Val) loop
14197 if not Is_OK_Static_Expression (Node (Discr_Val)) then
14198 Is_Static := False;
14199 exit;
14200 end if;
14202 Next_Elmt (Discr_Val);
14203 end loop;
14205 Set_Has_Static_Discriminants (Subt, Is_Static);
14207 Push_Scope (Subt);
14209 -- Inherit the discriminants of the parent type
14211 Add_Discriminants : declare
14212 Num_Disc : Nat;
14213 Num_Gird : Nat;
14215 begin
14216 Num_Disc := 0;
14217 Old_C := First_Discriminant (Typ);
14219 while Present (Old_C) loop
14220 Num_Disc := Num_Disc + 1;
14221 New_C := Create_Component (Old_C);
14222 Set_Is_Public (New_C, Is_Public (Subt));
14223 Next_Discriminant (Old_C);
14224 end loop;
14226 -- For an untagged derived subtype, the number of discriminants may
14227 -- be smaller than the number of inherited discriminants, because
14228 -- several of them may be renamed by a single new discriminant or
14229 -- constrained. In this case, add the hidden discriminants back into
14230 -- the subtype, because they need to be present if the optimizer of
14231 -- the GCC 4.x back-end decides to break apart assignments between
14232 -- objects using the parent view into member-wise assignments.
14234 Num_Gird := 0;
14236 if Is_Derived_Type (Typ)
14237 and then not Is_Tagged_Type (Typ)
14238 then
14239 Old_C := First_Stored_Discriminant (Typ);
14241 while Present (Old_C) loop
14242 Num_Gird := Num_Gird + 1;
14243 Next_Stored_Discriminant (Old_C);
14244 end loop;
14245 end if;
14247 if Num_Gird > Num_Disc then
14249 -- Find out multiple uses of new discriminants, and add hidden
14250 -- components for the extra renamed discriminants. We recognize
14251 -- multiple uses through the Corresponding_Discriminant of a
14252 -- new discriminant: if it constrains several old discriminants,
14253 -- this field points to the last one in the parent type. The
14254 -- stored discriminants of the derived type have the same name
14255 -- as those of the parent.
14257 declare
14258 Constr : Elmt_Id;
14259 New_Discr : Entity_Id;
14260 Old_Discr : Entity_Id;
14262 begin
14263 Constr := First_Elmt (Stored_Constraint (Typ));
14264 Old_Discr := First_Stored_Discriminant (Typ);
14265 while Present (Constr) loop
14266 if Is_Entity_Name (Node (Constr))
14267 and then Ekind (Entity (Node (Constr))) = E_Discriminant
14268 then
14269 New_Discr := Entity (Node (Constr));
14271 if Chars (Corresponding_Discriminant (New_Discr)) /=
14272 Chars (Old_Discr)
14273 then
14274 -- The new discriminant has been used to rename a
14275 -- subsequent old discriminant. Introduce a shadow
14276 -- component for the current old discriminant.
14278 New_C := Create_Component (Old_Discr);
14279 Set_Original_Record_Component (New_C, Old_Discr);
14280 end if;
14282 else
14283 -- The constraint has eliminated the old discriminant.
14284 -- Introduce a shadow component.
14286 New_C := Create_Component (Old_Discr);
14287 Set_Original_Record_Component (New_C, Old_Discr);
14288 end if;
14290 Next_Elmt (Constr);
14291 Next_Stored_Discriminant (Old_Discr);
14292 end loop;
14293 end;
14294 end if;
14295 end Add_Discriminants;
14297 if Is_Static
14298 and then Is_Variant_Record (Typ)
14299 then
14300 Collect_Fixed_Components (Typ);
14302 Gather_Components (
14303 Typ,
14304 Component_List (Type_Definition (Parent (Typ))),
14305 Governed_By => Assoc_List,
14306 Into => Comp_List,
14307 Report_Errors => Errors);
14308 pragma Assert (not Errors
14309 or else Serious_Errors_Detected > 0);
14311 Create_All_Components;
14313 -- If the subtype declaration is created for a tagged type derivation
14314 -- with constraints, we retrieve the record definition of the parent
14315 -- type to select the components of the proper variant.
14317 elsif Is_Static
14318 and then Is_Tagged_Type (Typ)
14319 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
14320 and then
14321 Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
14322 and then Is_Variant_Record (Parent_Type)
14323 then
14324 Collect_Fixed_Components (Typ);
14326 Gather_Components
14327 (Typ,
14328 Component_List (Type_Definition (Parent (Parent_Type))),
14329 Governed_By => Assoc_List,
14330 Into => Comp_List,
14331 Report_Errors => Errors);
14333 -- Note: previously there was a check at this point that no errors
14334 -- were detected. As a consequence of AI05-220 there may be an error
14335 -- if an inherited discriminant that controls a variant has a non-
14336 -- static constraint.
14338 -- If the tagged derivation has a type extension, collect all the
14339 -- new components therein.
14341 if Present (Record_Extension_Part (Type_Definition (Parent (Typ))))
14342 then
14343 Old_C := First_Component (Typ);
14344 while Present (Old_C) loop
14345 if Original_Record_Component (Old_C) = Old_C
14346 and then Chars (Old_C) /= Name_uTag
14347 and then Chars (Old_C) /= Name_uParent
14348 then
14349 Append_Elmt (Old_C, Comp_List);
14350 end if;
14352 Next_Component (Old_C);
14353 end loop;
14354 end if;
14356 Create_All_Components;
14358 else
14359 -- If discriminants are not static, or if this is a multi-level type
14360 -- extension, we have to include all components of the parent type.
14362 Old_C := First_Component (Typ);
14363 while Present (Old_C) loop
14364 New_C := Create_Component (Old_C);
14366 Set_Etype
14367 (New_C,
14368 Constrain_Component_Type
14369 (Old_C, Subt, Decl_Node, Typ, Constraints));
14370 Set_Is_Public (New_C, Is_Public (Subt));
14372 Next_Component (Old_C);
14373 end loop;
14374 end if;
14376 End_Scope;
14377 end Create_Constrained_Components;
14379 ------------------------------------------
14380 -- Decimal_Fixed_Point_Type_Declaration --
14381 ------------------------------------------
14383 procedure Decimal_Fixed_Point_Type_Declaration
14384 (T : Entity_Id;
14385 Def : Node_Id)
14387 Loc : constant Source_Ptr := Sloc (Def);
14388 Digs_Expr : constant Node_Id := Digits_Expression (Def);
14389 Delta_Expr : constant Node_Id := Delta_Expression (Def);
14390 Implicit_Base : Entity_Id;
14391 Digs_Val : Uint;
14392 Delta_Val : Ureal;
14393 Scale_Val : Uint;
14394 Bound_Val : Ureal;
14396 begin
14397 Check_SPARK_05_Restriction
14398 ("decimal fixed point type is not allowed", Def);
14399 Check_Restriction (No_Fixed_Point, Def);
14401 -- Create implicit base type
14403 Implicit_Base :=
14404 Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
14405 Set_Etype (Implicit_Base, Implicit_Base);
14407 -- Analyze and process delta expression
14409 Analyze_And_Resolve (Delta_Expr, Universal_Real);
14411 Check_Delta_Expression (Delta_Expr);
14412 Delta_Val := Expr_Value_R (Delta_Expr);
14414 -- Check delta is power of 10, and determine scale value from it
14416 declare
14417 Val : Ureal;
14419 begin
14420 Scale_Val := Uint_0;
14421 Val := Delta_Val;
14423 if Val < Ureal_1 then
14424 while Val < Ureal_1 loop
14425 Val := Val * Ureal_10;
14426 Scale_Val := Scale_Val + 1;
14427 end loop;
14429 if Scale_Val > 18 then
14430 Error_Msg_N ("scale exceeds maximum value of 18", Def);
14431 Scale_Val := UI_From_Int (+18);
14432 end if;
14434 else
14435 while Val > Ureal_1 loop
14436 Val := Val / Ureal_10;
14437 Scale_Val := Scale_Val - 1;
14438 end loop;
14440 if Scale_Val < -18 then
14441 Error_Msg_N ("scale is less than minimum value of -18", Def);
14442 Scale_Val := UI_From_Int (-18);
14443 end if;
14444 end if;
14446 if Val /= Ureal_1 then
14447 Error_Msg_N ("delta expression must be a power of 10", Def);
14448 Delta_Val := Ureal_10 ** (-Scale_Val);
14449 end if;
14450 end;
14452 -- Set delta, scale and small (small = delta for decimal type)
14454 Set_Delta_Value (Implicit_Base, Delta_Val);
14455 Set_Scale_Value (Implicit_Base, Scale_Val);
14456 Set_Small_Value (Implicit_Base, Delta_Val);
14458 -- Analyze and process digits expression
14460 Analyze_And_Resolve (Digs_Expr, Any_Integer);
14461 Check_Digits_Expression (Digs_Expr);
14462 Digs_Val := Expr_Value (Digs_Expr);
14464 if Digs_Val > 18 then
14465 Digs_Val := UI_From_Int (+18);
14466 Error_Msg_N ("digits value out of range, maximum is 18", Digs_Expr);
14467 end if;
14469 Set_Digits_Value (Implicit_Base, Digs_Val);
14470 Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
14472 -- Set range of base type from digits value for now. This will be
14473 -- expanded to represent the true underlying base range by Freeze.
14475 Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
14477 -- Note: We leave size as zero for now, size will be set at freeze
14478 -- time. We have to do this for ordinary fixed-point, because the size
14479 -- depends on the specified small, and we might as well do the same for
14480 -- decimal fixed-point.
14482 pragma Assert (Esize (Implicit_Base) = Uint_0);
14484 -- If there are bounds given in the declaration use them as the
14485 -- bounds of the first named subtype.
14487 if Present (Real_Range_Specification (Def)) then
14488 declare
14489 RRS : constant Node_Id := Real_Range_Specification (Def);
14490 Low : constant Node_Id := Low_Bound (RRS);
14491 High : constant Node_Id := High_Bound (RRS);
14492 Low_Val : Ureal;
14493 High_Val : Ureal;
14495 begin
14496 Analyze_And_Resolve (Low, Any_Real);
14497 Analyze_And_Resolve (High, Any_Real);
14498 Check_Real_Bound (Low);
14499 Check_Real_Bound (High);
14500 Low_Val := Expr_Value_R (Low);
14501 High_Val := Expr_Value_R (High);
14503 if Low_Val < (-Bound_Val) then
14504 Error_Msg_N
14505 ("range low bound too small for digits value", Low);
14506 Low_Val := -Bound_Val;
14507 end if;
14509 if High_Val > Bound_Val then
14510 Error_Msg_N
14511 ("range high bound too large for digits value", High);
14512 High_Val := Bound_Val;
14513 end if;
14515 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
14516 end;
14518 -- If no explicit range, use range that corresponds to given
14519 -- digits value. This will end up as the final range for the
14520 -- first subtype.
14522 else
14523 Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
14524 end if;
14526 -- Complete entity for first subtype. The inheritance of the rep item
14527 -- chain ensures that SPARK-related pragmas are not clobbered when the
14528 -- decimal fixed point type acts as a full view of a private type.
14530 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
14531 Set_Etype (T, Implicit_Base);
14532 Set_Size_Info (T, Implicit_Base);
14533 Inherit_Rep_Item_Chain (T, Implicit_Base);
14534 Set_Digits_Value (T, Digs_Val);
14535 Set_Delta_Value (T, Delta_Val);
14536 Set_Small_Value (T, Delta_Val);
14537 Set_Scale_Value (T, Scale_Val);
14538 Set_Is_Constrained (T);
14539 end Decimal_Fixed_Point_Type_Declaration;
14541 -----------------------------------
14542 -- Derive_Progenitor_Subprograms --
14543 -----------------------------------
14545 procedure Derive_Progenitor_Subprograms
14546 (Parent_Type : Entity_Id;
14547 Tagged_Type : Entity_Id)
14549 E : Entity_Id;
14550 Elmt : Elmt_Id;
14551 Iface : Entity_Id;
14552 Iface_Elmt : Elmt_Id;
14553 Iface_Subp : Entity_Id;
14554 New_Subp : Entity_Id := Empty;
14555 Prim_Elmt : Elmt_Id;
14556 Subp : Entity_Id;
14557 Typ : Entity_Id;
14559 begin
14560 pragma Assert (Ada_Version >= Ada_2005
14561 and then Is_Record_Type (Tagged_Type)
14562 and then Is_Tagged_Type (Tagged_Type)
14563 and then Has_Interfaces (Tagged_Type));
14565 -- Step 1: Transfer to the full-view primitives associated with the
14566 -- partial-view that cover interface primitives. Conceptually this
14567 -- work should be done later by Process_Full_View; done here to
14568 -- simplify its implementation at later stages. It can be safely
14569 -- done here because interfaces must be visible in the partial and
14570 -- private view (RM 7.3(7.3/2)).
14572 -- Small optimization: This work is only required if the parent may
14573 -- have entities whose Alias attribute reference an interface primitive.
14574 -- Such a situation may occur if the parent is an abstract type and the
14575 -- primitive has not been yet overridden or if the parent is a generic
14576 -- formal type covering interfaces.
14578 -- If the tagged type is not abstract, it cannot have abstract
14579 -- primitives (the only entities in the list of primitives of
14580 -- non-abstract tagged types that can reference abstract primitives
14581 -- through its Alias attribute are the internal entities that have
14582 -- attribute Interface_Alias, and these entities are generated later
14583 -- by Add_Internal_Interface_Entities).
14585 if In_Private_Part (Current_Scope)
14586 and then (Is_Abstract_Type (Parent_Type)
14587 or else
14588 Is_Generic_Type (Parent_Type))
14589 then
14590 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
14591 while Present (Elmt) loop
14592 Subp := Node (Elmt);
14594 -- At this stage it is not possible to have entities in the list
14595 -- of primitives that have attribute Interface_Alias.
14597 pragma Assert (No (Interface_Alias (Subp)));
14599 Typ := Find_Dispatching_Type (Ultimate_Alias (Subp));
14601 if Is_Interface (Typ) then
14602 E := Find_Primitive_Covering_Interface
14603 (Tagged_Type => Tagged_Type,
14604 Iface_Prim => Subp);
14606 if Present (E)
14607 and then Find_Dispatching_Type (Ultimate_Alias (E)) /= Typ
14608 then
14609 Replace_Elmt (Elmt, E);
14610 Remove_Homonym (Subp);
14611 end if;
14612 end if;
14614 Next_Elmt (Elmt);
14615 end loop;
14616 end if;
14618 -- Step 2: Add primitives of progenitors that are not implemented by
14619 -- parents of Tagged_Type.
14621 if Present (Interfaces (Base_Type (Tagged_Type))) then
14622 Iface_Elmt := First_Elmt (Interfaces (Base_Type (Tagged_Type)));
14623 while Present (Iface_Elmt) loop
14624 Iface := Node (Iface_Elmt);
14626 Prim_Elmt := First_Elmt (Primitive_Operations (Iface));
14627 while Present (Prim_Elmt) loop
14628 Iface_Subp := Node (Prim_Elmt);
14630 -- Exclude derivation of predefined primitives except those
14631 -- that come from source, or are inherited from one that comes
14632 -- from source. Required to catch declarations of equality
14633 -- operators of interfaces. For example:
14635 -- type Iface is interface;
14636 -- function "=" (Left, Right : Iface) return Boolean;
14638 if not Is_Predefined_Dispatching_Operation (Iface_Subp)
14639 or else Comes_From_Source (Ultimate_Alias (Iface_Subp))
14640 then
14641 E := Find_Primitive_Covering_Interface
14642 (Tagged_Type => Tagged_Type,
14643 Iface_Prim => Iface_Subp);
14645 -- If not found we derive a new primitive leaving its alias
14646 -- attribute referencing the interface primitive.
14648 if No (E) then
14649 Derive_Subprogram
14650 (New_Subp, Iface_Subp, Tagged_Type, Iface);
14652 -- Ada 2012 (AI05-0197): If the covering primitive's name
14653 -- differs from the name of the interface primitive then it
14654 -- is a private primitive inherited from a parent type. In
14655 -- such case, given that Tagged_Type covers the interface,
14656 -- the inherited private primitive becomes visible. For such
14657 -- purpose we add a new entity that renames the inherited
14658 -- private primitive.
14660 elsif Chars (E) /= Chars (Iface_Subp) then
14661 pragma Assert (Has_Suffix (E, 'P'));
14662 Derive_Subprogram
14663 (New_Subp, Iface_Subp, Tagged_Type, Iface);
14664 Set_Alias (New_Subp, E);
14665 Set_Is_Abstract_Subprogram (New_Subp,
14666 Is_Abstract_Subprogram (E));
14668 -- Propagate to the full view interface entities associated
14669 -- with the partial view.
14671 elsif In_Private_Part (Current_Scope)
14672 and then Present (Alias (E))
14673 and then Alias (E) = Iface_Subp
14674 and then
14675 List_Containing (Parent (E)) /=
14676 Private_Declarations
14677 (Specification
14678 (Unit_Declaration_Node (Current_Scope)))
14679 then
14680 Append_Elmt (E, Primitive_Operations (Tagged_Type));
14681 end if;
14682 end if;
14684 Next_Elmt (Prim_Elmt);
14685 end loop;
14687 Next_Elmt (Iface_Elmt);
14688 end loop;
14689 end if;
14690 end Derive_Progenitor_Subprograms;
14692 -----------------------
14693 -- Derive_Subprogram --
14694 -----------------------
14696 procedure Derive_Subprogram
14697 (New_Subp : out Entity_Id;
14698 Parent_Subp : Entity_Id;
14699 Derived_Type : Entity_Id;
14700 Parent_Type : Entity_Id;
14701 Actual_Subp : Entity_Id := Empty)
14703 Formal : Entity_Id;
14704 -- Formal parameter of parent primitive operation
14706 Formal_Of_Actual : Entity_Id;
14707 -- Formal parameter of actual operation, when the derivation is to
14708 -- create a renaming for a primitive operation of an actual in an
14709 -- instantiation.
14711 New_Formal : Entity_Id;
14712 -- Formal of inherited operation
14714 Visible_Subp : Entity_Id := Parent_Subp;
14716 function Is_Private_Overriding return Boolean;
14717 -- If Subp is a private overriding of a visible operation, the inherited
14718 -- operation derives from the overridden op (even though its body is the
14719 -- overriding one) and the inherited operation is visible now. See
14720 -- sem_disp to see the full details of the handling of the overridden
14721 -- subprogram, which is removed from the list of primitive operations of
14722 -- the type. The overridden subprogram is saved locally in Visible_Subp,
14723 -- and used to diagnose abstract operations that need overriding in the
14724 -- derived type.
14726 procedure Replace_Type (Id, New_Id : Entity_Id);
14727 -- When the type is an anonymous access type, create a new access type
14728 -- designating the derived type.
14730 procedure Set_Derived_Name;
14731 -- This procedure sets the appropriate Chars name for New_Subp. This
14732 -- is normally just a copy of the parent name. An exception arises for
14733 -- type support subprograms, where the name is changed to reflect the
14734 -- name of the derived type, e.g. if type foo is derived from type bar,
14735 -- then a procedure barDA is derived with a name fooDA.
14737 ---------------------------
14738 -- Is_Private_Overriding --
14739 ---------------------------
14741 function Is_Private_Overriding return Boolean is
14742 Prev : Entity_Id;
14744 begin
14745 -- If the parent is not a dispatching operation there is no
14746 -- need to investigate overridings
14748 if not Is_Dispatching_Operation (Parent_Subp) then
14749 return False;
14750 end if;
14752 -- The visible operation that is overridden is a homonym of the
14753 -- parent subprogram. We scan the homonym chain to find the one
14754 -- whose alias is the subprogram we are deriving.
14756 Prev := Current_Entity (Parent_Subp);
14757 while Present (Prev) loop
14758 if Ekind (Prev) = Ekind (Parent_Subp)
14759 and then Alias (Prev) = Parent_Subp
14760 and then Scope (Parent_Subp) = Scope (Prev)
14761 and then not Is_Hidden (Prev)
14762 then
14763 Visible_Subp := Prev;
14764 return True;
14765 end if;
14767 Prev := Homonym (Prev);
14768 end loop;
14770 return False;
14771 end Is_Private_Overriding;
14773 ------------------
14774 -- Replace_Type --
14775 ------------------
14777 procedure Replace_Type (Id, New_Id : Entity_Id) is
14778 Id_Type : constant Entity_Id := Etype (Id);
14779 Acc_Type : Entity_Id;
14780 Par : constant Node_Id := Parent (Derived_Type);
14782 begin
14783 -- When the type is an anonymous access type, create a new access
14784 -- type designating the derived type. This itype must be elaborated
14785 -- at the point of the derivation, not on subsequent calls that may
14786 -- be out of the proper scope for Gigi, so we insert a reference to
14787 -- it after the derivation.
14789 if Ekind (Id_Type) = E_Anonymous_Access_Type then
14790 declare
14791 Desig_Typ : Entity_Id := Designated_Type (Id_Type);
14793 begin
14794 if Ekind (Desig_Typ) = E_Record_Type_With_Private
14795 and then Present (Full_View (Desig_Typ))
14796 and then not Is_Private_Type (Parent_Type)
14797 then
14798 Desig_Typ := Full_View (Desig_Typ);
14799 end if;
14801 if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
14803 -- Ada 2005 (AI-251): Handle also derivations of abstract
14804 -- interface primitives.
14806 or else (Is_Interface (Desig_Typ)
14807 and then not Is_Class_Wide_Type (Desig_Typ))
14808 then
14809 Acc_Type := New_Copy (Id_Type);
14810 Set_Etype (Acc_Type, Acc_Type);
14811 Set_Scope (Acc_Type, New_Subp);
14813 -- Set size of anonymous access type. If we have an access
14814 -- to an unconstrained array, this is a fat pointer, so it
14815 -- is sizes at twice addtress size.
14817 if Is_Array_Type (Desig_Typ)
14818 and then not Is_Constrained (Desig_Typ)
14819 then
14820 Init_Size (Acc_Type, 2 * System_Address_Size);
14822 -- Other cases use a thin pointer
14824 else
14825 Init_Size (Acc_Type, System_Address_Size);
14826 end if;
14828 -- Set remaining characterstics of anonymous access type
14830 Init_Alignment (Acc_Type);
14831 Set_Directly_Designated_Type (Acc_Type, Derived_Type);
14833 Set_Etype (New_Id, Acc_Type);
14834 Set_Scope (New_Id, New_Subp);
14836 -- Create a reference to it
14838 Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
14840 else
14841 Set_Etype (New_Id, Id_Type);
14842 end if;
14843 end;
14845 -- In Ada2012, a formal may have an incomplete type but the type
14846 -- derivation that inherits the primitive follows the full view.
14848 elsif Base_Type (Id_Type) = Base_Type (Parent_Type)
14849 or else
14850 (Ekind (Id_Type) = E_Record_Type_With_Private
14851 and then Present (Full_View (Id_Type))
14852 and then
14853 Base_Type (Full_View (Id_Type)) = Base_Type (Parent_Type))
14854 or else
14855 (Ada_Version >= Ada_2012
14856 and then Ekind (Id_Type) = E_Incomplete_Type
14857 and then Full_View (Id_Type) = Parent_Type)
14858 then
14859 -- Constraint checks on formals are generated during expansion,
14860 -- based on the signature of the original subprogram. The bounds
14861 -- of the derived type are not relevant, and thus we can use
14862 -- the base type for the formals. However, the return type may be
14863 -- used in a context that requires that the proper static bounds
14864 -- be used (a case statement, for example) and for those cases
14865 -- we must use the derived type (first subtype), not its base.
14867 -- If the derived_type_definition has no constraints, we know that
14868 -- the derived type has the same constraints as the first subtype
14869 -- of the parent, and we can also use it rather than its base,
14870 -- which can lead to more efficient code.
14872 if Etype (Id) = Parent_Type then
14873 if Is_Scalar_Type (Parent_Type)
14874 and then
14875 Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
14876 then
14877 Set_Etype (New_Id, Derived_Type);
14879 elsif Nkind (Par) = N_Full_Type_Declaration
14880 and then
14881 Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
14882 and then
14883 Is_Entity_Name
14884 (Subtype_Indication (Type_Definition (Par)))
14885 then
14886 Set_Etype (New_Id, Derived_Type);
14888 else
14889 Set_Etype (New_Id, Base_Type (Derived_Type));
14890 end if;
14892 else
14893 Set_Etype (New_Id, Base_Type (Derived_Type));
14894 end if;
14896 else
14897 Set_Etype (New_Id, Etype (Id));
14898 end if;
14899 end Replace_Type;
14901 ----------------------
14902 -- Set_Derived_Name --
14903 ----------------------
14905 procedure Set_Derived_Name is
14906 Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
14907 begin
14908 if Nm = TSS_Null then
14909 Set_Chars (New_Subp, Chars (Parent_Subp));
14910 else
14911 Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
14912 end if;
14913 end Set_Derived_Name;
14915 -- Start of processing for Derive_Subprogram
14917 begin
14918 New_Subp := New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
14919 Set_Ekind (New_Subp, Ekind (Parent_Subp));
14921 -- Check whether the inherited subprogram is a private operation that
14922 -- should be inherited but not yet made visible. Such subprograms can
14923 -- become visible at a later point (e.g., the private part of a public
14924 -- child unit) via Declare_Inherited_Private_Subprograms. If the
14925 -- following predicate is true, then this is not such a private
14926 -- operation and the subprogram simply inherits the name of the parent
14927 -- subprogram. Note the special check for the names of controlled
14928 -- operations, which are currently exempted from being inherited with
14929 -- a hidden name because they must be findable for generation of
14930 -- implicit run-time calls.
14932 if not Is_Hidden (Parent_Subp)
14933 or else Is_Internal (Parent_Subp)
14934 or else Is_Private_Overriding
14935 or else Is_Internal_Name (Chars (Parent_Subp))
14936 or else (Is_Controlled (Parent_Type)
14937 and then Nam_In (Chars (Parent_Subp), Name_Adjust,
14938 Name_Finalize,
14939 Name_Initialize))
14940 then
14941 Set_Derived_Name;
14943 -- An inherited dispatching equality will be overridden by an internally
14944 -- generated one, or by an explicit one, so preserve its name and thus
14945 -- its entry in the dispatch table. Otherwise, if Parent_Subp is a
14946 -- private operation it may become invisible if the full view has
14947 -- progenitors, and the dispatch table will be malformed.
14948 -- We check that the type is limited to handle the anomalous declaration
14949 -- of Limited_Controlled, which is derived from a non-limited type, and
14950 -- which is handled specially elsewhere as well.
14952 elsif Chars (Parent_Subp) = Name_Op_Eq
14953 and then Is_Dispatching_Operation (Parent_Subp)
14954 and then Etype (Parent_Subp) = Standard_Boolean
14955 and then not Is_Limited_Type (Etype (First_Formal (Parent_Subp)))
14956 and then
14957 Etype (First_Formal (Parent_Subp)) =
14958 Etype (Next_Formal (First_Formal (Parent_Subp)))
14959 then
14960 Set_Derived_Name;
14962 -- If parent is hidden, this can be a regular derivation if the
14963 -- parent is immediately visible in a non-instantiating context,
14964 -- or if we are in the private part of an instance. This test
14965 -- should still be refined ???
14967 -- The test for In_Instance_Not_Visible avoids inheriting the derived
14968 -- operation as a non-visible operation in cases where the parent
14969 -- subprogram might not be visible now, but was visible within the
14970 -- original generic, so it would be wrong to make the inherited
14971 -- subprogram non-visible now. (Not clear if this test is fully
14972 -- correct; are there any cases where we should declare the inherited
14973 -- operation as not visible to avoid it being overridden, e.g., when
14974 -- the parent type is a generic actual with private primitives ???)
14976 -- (they should be treated the same as other private inherited
14977 -- subprograms, but it's not clear how to do this cleanly). ???
14979 elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
14980 and then Is_Immediately_Visible (Parent_Subp)
14981 and then not In_Instance)
14982 or else In_Instance_Not_Visible
14983 then
14984 Set_Derived_Name;
14986 -- Ada 2005 (AI-251): Regular derivation if the parent subprogram
14987 -- overrides an interface primitive because interface primitives
14988 -- must be visible in the partial view of the parent (RM 7.3 (7.3/2))
14990 elsif Ada_Version >= Ada_2005
14991 and then Is_Dispatching_Operation (Parent_Subp)
14992 and then Covers_Some_Interface (Parent_Subp)
14993 then
14994 Set_Derived_Name;
14996 -- Otherwise, the type is inheriting a private operation, so enter it
14997 -- with a special name so it can't be overridden.
14999 else
15000 Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
15001 end if;
15003 Set_Parent (New_Subp, Parent (Derived_Type));
15005 if Present (Actual_Subp) then
15006 Replace_Type (Actual_Subp, New_Subp);
15007 else
15008 Replace_Type (Parent_Subp, New_Subp);
15009 end if;
15011 Conditional_Delay (New_Subp, Parent_Subp);
15013 -- If we are creating a renaming for a primitive operation of an
15014 -- actual of a generic derived type, we must examine the signature
15015 -- of the actual primitive, not that of the generic formal, which for
15016 -- example may be an interface. However the name and initial value
15017 -- of the inherited operation are those of the formal primitive.
15019 Formal := First_Formal (Parent_Subp);
15021 if Present (Actual_Subp) then
15022 Formal_Of_Actual := First_Formal (Actual_Subp);
15023 else
15024 Formal_Of_Actual := Empty;
15025 end if;
15027 while Present (Formal) loop
15028 New_Formal := New_Copy (Formal);
15030 -- Normally we do not go copying parents, but in the case of
15031 -- formals, we need to link up to the declaration (which is the
15032 -- parameter specification), and it is fine to link up to the
15033 -- original formal's parameter specification in this case.
15035 Set_Parent (New_Formal, Parent (Formal));
15036 Append_Entity (New_Formal, New_Subp);
15038 if Present (Formal_Of_Actual) then
15039 Replace_Type (Formal_Of_Actual, New_Formal);
15040 Next_Formal (Formal_Of_Actual);
15041 else
15042 Replace_Type (Formal, New_Formal);
15043 end if;
15045 Next_Formal (Formal);
15046 end loop;
15048 -- If this derivation corresponds to a tagged generic actual, then
15049 -- primitive operations rename those of the actual. Otherwise the
15050 -- primitive operations rename those of the parent type, If the parent
15051 -- renames an intrinsic operator, so does the new subprogram. We except
15052 -- concatenation, which is always properly typed, and does not get
15053 -- expanded as other intrinsic operations.
15055 if No (Actual_Subp) then
15056 if Is_Intrinsic_Subprogram (Parent_Subp) then
15057 Set_Is_Intrinsic_Subprogram (New_Subp);
15059 if Present (Alias (Parent_Subp))
15060 and then Chars (Parent_Subp) /= Name_Op_Concat
15061 then
15062 Set_Alias (New_Subp, Alias (Parent_Subp));
15063 else
15064 Set_Alias (New_Subp, Parent_Subp);
15065 end if;
15067 else
15068 Set_Alias (New_Subp, Parent_Subp);
15069 end if;
15071 else
15072 Set_Alias (New_Subp, Actual_Subp);
15073 end if;
15075 -- Derived subprograms of a tagged type must inherit the convention
15076 -- of the parent subprogram (a requirement of AI-117). Derived
15077 -- subprograms of untagged types simply get convention Ada by default.
15079 -- If the derived type is a tagged generic formal type with unknown
15080 -- discriminants, its convention is intrinsic (RM 6.3.1 (8)).
15082 -- However, if the type is derived from a generic formal, the further
15083 -- inherited subprogram has the convention of the non-generic ancestor.
15084 -- Otherwise there would be no way to override the operation.
15085 -- (This is subject to forthcoming ARG discussions).
15087 if Is_Tagged_Type (Derived_Type) then
15088 if Is_Generic_Type (Derived_Type)
15089 and then Has_Unknown_Discriminants (Derived_Type)
15090 then
15091 Set_Convention (New_Subp, Convention_Intrinsic);
15093 else
15094 if Is_Generic_Type (Parent_Type)
15095 and then Has_Unknown_Discriminants (Parent_Type)
15096 then
15097 Set_Convention (New_Subp, Convention (Alias (Parent_Subp)));
15098 else
15099 Set_Convention (New_Subp, Convention (Parent_Subp));
15100 end if;
15101 end if;
15102 end if;
15104 -- Predefined controlled operations retain their name even if the parent
15105 -- is hidden (see above), but they are not primitive operations if the
15106 -- ancestor is not visible, for example if the parent is a private
15107 -- extension completed with a controlled extension. Note that a full
15108 -- type that is controlled can break privacy: the flag Is_Controlled is
15109 -- set on both views of the type.
15111 if Is_Controlled (Parent_Type)
15112 and then Nam_In (Chars (Parent_Subp), Name_Initialize,
15113 Name_Adjust,
15114 Name_Finalize)
15115 and then Is_Hidden (Parent_Subp)
15116 and then not Is_Visibly_Controlled (Parent_Type)
15117 then
15118 Set_Is_Hidden (New_Subp);
15119 end if;
15121 Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
15122 Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
15124 if Ekind (Parent_Subp) = E_Procedure then
15125 Set_Is_Valued_Procedure
15126 (New_Subp, Is_Valued_Procedure (Parent_Subp));
15127 else
15128 Set_Has_Controlling_Result
15129 (New_Subp, Has_Controlling_Result (Parent_Subp));
15130 end if;
15132 -- No_Return must be inherited properly. If this is overridden in the
15133 -- case of a dispatching operation, then a check is made in Sem_Disp
15134 -- that the overriding operation is also No_Return (no such check is
15135 -- required for the case of non-dispatching operation.
15137 Set_No_Return (New_Subp, No_Return (Parent_Subp));
15139 -- A derived function with a controlling result is abstract. If the
15140 -- Derived_Type is a nonabstract formal generic derived type, then
15141 -- inherited operations are not abstract: the required check is done at
15142 -- instantiation time. If the derivation is for a generic actual, the
15143 -- function is not abstract unless the actual is.
15145 if Is_Generic_Type (Derived_Type)
15146 and then not Is_Abstract_Type (Derived_Type)
15147 then
15148 null;
15150 -- Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
15151 -- properties of the subprogram, as defined in RM-3.9.3(4/2-6/2).
15153 -- A subprogram subject to pragma Extensions_Visible with value False
15154 -- requires overriding if the subprogram has at least one controlling
15155 -- OUT parameter (SPARK RM 6.1.7(6)).
15157 elsif Ada_Version >= Ada_2005
15158 and then (Is_Abstract_Subprogram (Alias (New_Subp))
15159 or else (Is_Tagged_Type (Derived_Type)
15160 and then Etype (New_Subp) = Derived_Type
15161 and then not Is_Null_Extension (Derived_Type))
15162 or else (Is_Tagged_Type (Derived_Type)
15163 and then Ekind (Etype (New_Subp)) =
15164 E_Anonymous_Access_Type
15165 and then Designated_Type (Etype (New_Subp)) =
15166 Derived_Type
15167 and then not Is_Null_Extension (Derived_Type))
15168 or else (Comes_From_Source (Alias (New_Subp))
15169 and then Is_EVF_Procedure (Alias (New_Subp))))
15170 and then No (Actual_Subp)
15171 then
15172 if not Is_Tagged_Type (Derived_Type)
15173 or else Is_Abstract_Type (Derived_Type)
15174 or else Is_Abstract_Subprogram (Alias (New_Subp))
15175 then
15176 Set_Is_Abstract_Subprogram (New_Subp);
15177 else
15178 Set_Requires_Overriding (New_Subp);
15179 end if;
15181 elsif Ada_Version < Ada_2005
15182 and then (Is_Abstract_Subprogram (Alias (New_Subp))
15183 or else (Is_Tagged_Type (Derived_Type)
15184 and then Etype (New_Subp) = Derived_Type
15185 and then No (Actual_Subp)))
15186 then
15187 Set_Is_Abstract_Subprogram (New_Subp);
15189 -- AI05-0097 : an inherited operation that dispatches on result is
15190 -- abstract if the derived type is abstract, even if the parent type
15191 -- is concrete and the derived type is a null extension.
15193 elsif Has_Controlling_Result (Alias (New_Subp))
15194 and then Is_Abstract_Type (Etype (New_Subp))
15195 then
15196 Set_Is_Abstract_Subprogram (New_Subp);
15198 -- Finally, if the parent type is abstract we must verify that all
15199 -- inherited operations are either non-abstract or overridden, or that
15200 -- the derived type itself is abstract (this check is performed at the
15201 -- end of a package declaration, in Check_Abstract_Overriding). A
15202 -- private overriding in the parent type will not be visible in the
15203 -- derivation if we are not in an inner package or in a child unit of
15204 -- the parent type, in which case the abstractness of the inherited
15205 -- operation is carried to the new subprogram.
15207 elsif Is_Abstract_Type (Parent_Type)
15208 and then not In_Open_Scopes (Scope (Parent_Type))
15209 and then Is_Private_Overriding
15210 and then Is_Abstract_Subprogram (Visible_Subp)
15211 then
15212 if No (Actual_Subp) then
15213 Set_Alias (New_Subp, Visible_Subp);
15214 Set_Is_Abstract_Subprogram (New_Subp, True);
15216 else
15217 -- If this is a derivation for an instance of a formal derived
15218 -- type, abstractness comes from the primitive operation of the
15219 -- actual, not from the operation inherited from the ancestor.
15221 Set_Is_Abstract_Subprogram
15222 (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
15223 end if;
15224 end if;
15226 New_Overloaded_Entity (New_Subp, Derived_Type);
15228 -- Check for case of a derived subprogram for the instantiation of a
15229 -- formal derived tagged type, if so mark the subprogram as dispatching
15230 -- and inherit the dispatching attributes of the actual subprogram. The
15231 -- derived subprogram is effectively renaming of the actual subprogram,
15232 -- so it needs to have the same attributes as the actual.
15234 if Present (Actual_Subp)
15235 and then Is_Dispatching_Operation (Actual_Subp)
15236 then
15237 Set_Is_Dispatching_Operation (New_Subp);
15239 if Present (DTC_Entity (Actual_Subp)) then
15240 Set_DTC_Entity (New_Subp, DTC_Entity (Actual_Subp));
15241 Set_DT_Position_Value (New_Subp, DT_Position (Actual_Subp));
15242 end if;
15243 end if;
15245 -- Indicate that a derived subprogram does not require a body and that
15246 -- it does not require processing of default expressions.
15248 Set_Has_Completion (New_Subp);
15249 Set_Default_Expressions_Processed (New_Subp);
15251 if Ekind (New_Subp) = E_Function then
15252 Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
15253 end if;
15254 end Derive_Subprogram;
15256 ------------------------
15257 -- Derive_Subprograms --
15258 ------------------------
15260 procedure Derive_Subprograms
15261 (Parent_Type : Entity_Id;
15262 Derived_Type : Entity_Id;
15263 Generic_Actual : Entity_Id := Empty)
15265 Op_List : constant Elist_Id :=
15266 Collect_Primitive_Operations (Parent_Type);
15268 function Check_Derived_Type return Boolean;
15269 -- Check that all the entities derived from Parent_Type are found in
15270 -- the list of primitives of Derived_Type exactly in the same order.
15272 procedure Derive_Interface_Subprogram
15273 (New_Subp : out Entity_Id;
15274 Subp : Entity_Id;
15275 Actual_Subp : Entity_Id);
15276 -- Derive New_Subp from the ultimate alias of the parent subprogram Subp
15277 -- (which is an interface primitive). If Generic_Actual is present then
15278 -- Actual_Subp is the actual subprogram corresponding with the generic
15279 -- subprogram Subp.
15281 ------------------------
15282 -- Check_Derived_Type --
15283 ------------------------
15285 function Check_Derived_Type return Boolean is
15286 E : Entity_Id;
15287 Elmt : Elmt_Id;
15288 List : Elist_Id;
15289 New_Subp : Entity_Id;
15290 Op_Elmt : Elmt_Id;
15291 Subp : Entity_Id;
15293 begin
15294 -- Traverse list of entities in the current scope searching for
15295 -- an incomplete type whose full-view is derived type.
15297 E := First_Entity (Scope (Derived_Type));
15298 while Present (E) and then E /= Derived_Type loop
15299 if Ekind (E) = E_Incomplete_Type
15300 and then Present (Full_View (E))
15301 and then Full_View (E) = Derived_Type
15302 then
15303 -- Disable this test if Derived_Type completes an incomplete
15304 -- type because in such case more primitives can be added
15305 -- later to the list of primitives of Derived_Type by routine
15306 -- Process_Incomplete_Dependents
15308 return True;
15309 end if;
15311 E := Next_Entity (E);
15312 end loop;
15314 List := Collect_Primitive_Operations (Derived_Type);
15315 Elmt := First_Elmt (List);
15317 Op_Elmt := First_Elmt (Op_List);
15318 while Present (Op_Elmt) loop
15319 Subp := Node (Op_Elmt);
15320 New_Subp := Node (Elmt);
15322 -- At this early stage Derived_Type has no entities with attribute
15323 -- Interface_Alias. In addition, such primitives are always
15324 -- located at the end of the list of primitives of Parent_Type.
15325 -- Therefore, if found we can safely stop processing pending
15326 -- entities.
15328 exit when Present (Interface_Alias (Subp));
15330 -- Handle hidden entities
15332 if not Is_Predefined_Dispatching_Operation (Subp)
15333 and then Is_Hidden (Subp)
15334 then
15335 if Present (New_Subp)
15336 and then Primitive_Names_Match (Subp, New_Subp)
15337 then
15338 Next_Elmt (Elmt);
15339 end if;
15341 else
15342 if not Present (New_Subp)
15343 or else Ekind (Subp) /= Ekind (New_Subp)
15344 or else not Primitive_Names_Match (Subp, New_Subp)
15345 then
15346 return False;
15347 end if;
15349 Next_Elmt (Elmt);
15350 end if;
15352 Next_Elmt (Op_Elmt);
15353 end loop;
15355 return True;
15356 end Check_Derived_Type;
15358 ---------------------------------
15359 -- Derive_Interface_Subprogram --
15360 ---------------------------------
15362 procedure Derive_Interface_Subprogram
15363 (New_Subp : out Entity_Id;
15364 Subp : Entity_Id;
15365 Actual_Subp : Entity_Id)
15367 Iface_Subp : constant Entity_Id := Ultimate_Alias (Subp);
15368 Iface_Type : constant Entity_Id := Find_Dispatching_Type (Iface_Subp);
15370 begin
15371 pragma Assert (Is_Interface (Iface_Type));
15373 Derive_Subprogram
15374 (New_Subp => New_Subp,
15375 Parent_Subp => Iface_Subp,
15376 Derived_Type => Derived_Type,
15377 Parent_Type => Iface_Type,
15378 Actual_Subp => Actual_Subp);
15380 -- Given that this new interface entity corresponds with a primitive
15381 -- of the parent that was not overridden we must leave it associated
15382 -- with its parent primitive to ensure that it will share the same
15383 -- dispatch table slot when overridden. We must set the Alias to Subp
15384 -- (instead of Iface_Subp), and we must fix Is_Abstract_Subprogram
15385 -- (in case we inherited Subp from Iface_Type via a nonabstract
15386 -- generic formal type).
15388 if No (Actual_Subp) then
15389 Set_Alias (New_Subp, Subp);
15391 declare
15392 T : Entity_Id := Find_Dispatching_Type (Subp);
15393 begin
15394 while Etype (T) /= T loop
15395 if Is_Generic_Type (T) and then not Is_Abstract_Type (T) then
15396 Set_Is_Abstract_Subprogram (New_Subp, False);
15397 exit;
15398 end if;
15400 T := Etype (T);
15401 end loop;
15402 end;
15404 -- For instantiations this is not needed since the previous call to
15405 -- Derive_Subprogram leaves the entity well decorated.
15407 else
15408 pragma Assert (Alias (New_Subp) = Actual_Subp);
15409 null;
15410 end if;
15411 end Derive_Interface_Subprogram;
15413 -- Local variables
15415 Alias_Subp : Entity_Id;
15416 Act_List : Elist_Id;
15417 Act_Elmt : Elmt_Id;
15418 Act_Subp : Entity_Id := Empty;
15419 Elmt : Elmt_Id;
15420 Need_Search : Boolean := False;
15421 New_Subp : Entity_Id := Empty;
15422 Parent_Base : Entity_Id;
15423 Subp : Entity_Id;
15425 -- Start of processing for Derive_Subprograms
15427 begin
15428 if Ekind (Parent_Type) = E_Record_Type_With_Private
15429 and then Has_Discriminants (Parent_Type)
15430 and then Present (Full_View (Parent_Type))
15431 then
15432 Parent_Base := Full_View (Parent_Type);
15433 else
15434 Parent_Base := Parent_Type;
15435 end if;
15437 if Present (Generic_Actual) then
15438 Act_List := Collect_Primitive_Operations (Generic_Actual);
15439 Act_Elmt := First_Elmt (Act_List);
15440 else
15441 Act_List := No_Elist;
15442 Act_Elmt := No_Elmt;
15443 end if;
15445 -- Derive primitives inherited from the parent. Note that if the generic
15446 -- actual is present, this is not really a type derivation, it is a
15447 -- completion within an instance.
15449 -- Case 1: Derived_Type does not implement interfaces
15451 if not Is_Tagged_Type (Derived_Type)
15452 or else (not Has_Interfaces (Derived_Type)
15453 and then not (Present (Generic_Actual)
15454 and then Has_Interfaces (Generic_Actual)))
15455 then
15456 Elmt := First_Elmt (Op_List);
15457 while Present (Elmt) loop
15458 Subp := Node (Elmt);
15460 -- Literals are derived earlier in the process of building the
15461 -- derived type, and are skipped here.
15463 if Ekind (Subp) = E_Enumeration_Literal then
15464 null;
15466 -- The actual is a direct descendant and the common primitive
15467 -- operations appear in the same order.
15469 -- If the generic parent type is present, the derived type is an
15470 -- instance of a formal derived type, and within the instance its
15471 -- operations are those of the actual. We derive from the formal
15472 -- type but make the inherited operations aliases of the
15473 -- corresponding operations of the actual.
15475 else
15476 pragma Assert (No (Node (Act_Elmt))
15477 or else (Primitive_Names_Match (Subp, Node (Act_Elmt))
15478 and then
15479 Type_Conformant
15480 (Subp, Node (Act_Elmt),
15481 Skip_Controlling_Formals => True)));
15483 Derive_Subprogram
15484 (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
15486 if Present (Act_Elmt) then
15487 Next_Elmt (Act_Elmt);
15488 end if;
15489 end if;
15491 Next_Elmt (Elmt);
15492 end loop;
15494 -- Case 2: Derived_Type implements interfaces
15496 else
15497 -- If the parent type has no predefined primitives we remove
15498 -- predefined primitives from the list of primitives of generic
15499 -- actual to simplify the complexity of this algorithm.
15501 if Present (Generic_Actual) then
15502 declare
15503 Has_Predefined_Primitives : Boolean := False;
15505 begin
15506 -- Check if the parent type has predefined primitives
15508 Elmt := First_Elmt (Op_List);
15509 while Present (Elmt) loop
15510 Subp := Node (Elmt);
15512 if Is_Predefined_Dispatching_Operation (Subp)
15513 and then not Comes_From_Source (Ultimate_Alias (Subp))
15514 then
15515 Has_Predefined_Primitives := True;
15516 exit;
15517 end if;
15519 Next_Elmt (Elmt);
15520 end loop;
15522 -- Remove predefined primitives of Generic_Actual. We must use
15523 -- an auxiliary list because in case of tagged types the value
15524 -- returned by Collect_Primitive_Operations is the value stored
15525 -- in its Primitive_Operations attribute (and we don't want to
15526 -- modify its current contents).
15528 if not Has_Predefined_Primitives then
15529 declare
15530 Aux_List : constant Elist_Id := New_Elmt_List;
15532 begin
15533 Elmt := First_Elmt (Act_List);
15534 while Present (Elmt) loop
15535 Subp := Node (Elmt);
15537 if not Is_Predefined_Dispatching_Operation (Subp)
15538 or else Comes_From_Source (Subp)
15539 then
15540 Append_Elmt (Subp, Aux_List);
15541 end if;
15543 Next_Elmt (Elmt);
15544 end loop;
15546 Act_List := Aux_List;
15547 end;
15548 end if;
15550 Act_Elmt := First_Elmt (Act_List);
15551 Act_Subp := Node (Act_Elmt);
15552 end;
15553 end if;
15555 -- Stage 1: If the generic actual is not present we derive the
15556 -- primitives inherited from the parent type. If the generic parent
15557 -- type is present, the derived type is an instance of a formal
15558 -- derived type, and within the instance its operations are those of
15559 -- the actual. We derive from the formal type but make the inherited
15560 -- operations aliases of the corresponding operations of the actual.
15562 Elmt := First_Elmt (Op_List);
15563 while Present (Elmt) loop
15564 Subp := Node (Elmt);
15565 Alias_Subp := Ultimate_Alias (Subp);
15567 -- Do not derive internal entities of the parent that link
15568 -- interface primitives with their covering primitive. These
15569 -- entities will be added to this type when frozen.
15571 if Present (Interface_Alias (Subp)) then
15572 goto Continue;
15573 end if;
15575 -- If the generic actual is present find the corresponding
15576 -- operation in the generic actual. If the parent type is a
15577 -- direct ancestor of the derived type then, even if it is an
15578 -- interface, the operations are inherited from the primary
15579 -- dispatch table and are in the proper order. If we detect here
15580 -- that primitives are not in the same order we traverse the list
15581 -- of primitive operations of the actual to find the one that
15582 -- implements the interface primitive.
15584 if Need_Search
15585 or else
15586 (Present (Generic_Actual)
15587 and then Present (Act_Subp)
15588 and then not
15589 (Primitive_Names_Match (Subp, Act_Subp)
15590 and then
15591 Type_Conformant (Subp, Act_Subp,
15592 Skip_Controlling_Formals => True)))
15593 then
15594 pragma Assert (not Is_Ancestor (Parent_Base, Generic_Actual,
15595 Use_Full_View => True));
15597 -- Remember that we need searching for all pending primitives
15599 Need_Search := True;
15601 -- Handle entities associated with interface primitives
15603 if Present (Alias_Subp)
15604 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
15605 and then not Is_Predefined_Dispatching_Operation (Subp)
15606 then
15607 -- Search for the primitive in the homonym chain
15609 Act_Subp :=
15610 Find_Primitive_Covering_Interface
15611 (Tagged_Type => Generic_Actual,
15612 Iface_Prim => Alias_Subp);
15614 -- Previous search may not locate primitives covering
15615 -- interfaces defined in generics units or instantiations.
15616 -- (it fails if the covering primitive has formals whose
15617 -- type is also defined in generics or instantiations).
15618 -- In such case we search in the list of primitives of the
15619 -- generic actual for the internal entity that links the
15620 -- interface primitive and the covering primitive.
15622 if No (Act_Subp)
15623 and then Is_Generic_Type (Parent_Type)
15624 then
15625 -- This code has been designed to handle only generic
15626 -- formals that implement interfaces that are defined
15627 -- in a generic unit or instantiation. If this code is
15628 -- needed for other cases we must review it because
15629 -- (given that it relies on Original_Location to locate
15630 -- the primitive of Generic_Actual that covers the
15631 -- interface) it could leave linked through attribute
15632 -- Alias entities of unrelated instantiations).
15634 pragma Assert
15635 (Is_Generic_Unit
15636 (Scope (Find_Dispatching_Type (Alias_Subp)))
15637 or else
15638 Instantiation_Depth
15639 (Sloc (Find_Dispatching_Type (Alias_Subp))) > 0);
15641 declare
15642 Iface_Prim_Loc : constant Source_Ptr :=
15643 Original_Location (Sloc (Alias_Subp));
15645 Elmt : Elmt_Id;
15646 Prim : Entity_Id;
15648 begin
15649 Elmt :=
15650 First_Elmt (Primitive_Operations (Generic_Actual));
15652 Search : while Present (Elmt) loop
15653 Prim := Node (Elmt);
15655 if Present (Interface_Alias (Prim))
15656 and then Original_Location
15657 (Sloc (Interface_Alias (Prim))) =
15658 Iface_Prim_Loc
15659 then
15660 Act_Subp := Alias (Prim);
15661 exit Search;
15662 end if;
15664 Next_Elmt (Elmt);
15665 end loop Search;
15666 end;
15667 end if;
15669 pragma Assert (Present (Act_Subp)
15670 or else Is_Abstract_Type (Generic_Actual)
15671 or else Serious_Errors_Detected > 0);
15673 -- Handle predefined primitives plus the rest of user-defined
15674 -- primitives
15676 else
15677 Act_Elmt := First_Elmt (Act_List);
15678 while Present (Act_Elmt) loop
15679 Act_Subp := Node (Act_Elmt);
15681 exit when Primitive_Names_Match (Subp, Act_Subp)
15682 and then Type_Conformant
15683 (Subp, Act_Subp,
15684 Skip_Controlling_Formals => True)
15685 and then No (Interface_Alias (Act_Subp));
15687 Next_Elmt (Act_Elmt);
15688 end loop;
15690 if No (Act_Elmt) then
15691 Act_Subp := Empty;
15692 end if;
15693 end if;
15694 end if;
15696 -- Case 1: If the parent is a limited interface then it has the
15697 -- predefined primitives of synchronized interfaces. However, the
15698 -- actual type may be a non-limited type and hence it does not
15699 -- have such primitives.
15701 if Present (Generic_Actual)
15702 and then not Present (Act_Subp)
15703 and then Is_Limited_Interface (Parent_Base)
15704 and then Is_Predefined_Interface_Primitive (Subp)
15705 then
15706 null;
15708 -- Case 2: Inherit entities associated with interfaces that were
15709 -- not covered by the parent type. We exclude here null interface
15710 -- primitives because they do not need special management.
15712 -- We also exclude interface operations that are renamings. If the
15713 -- subprogram is an explicit renaming of an interface primitive,
15714 -- it is a regular primitive operation, and the presence of its
15715 -- alias is not relevant: it has to be derived like any other
15716 -- primitive.
15718 elsif Present (Alias (Subp))
15719 and then Nkind (Unit_Declaration_Node (Subp)) /=
15720 N_Subprogram_Renaming_Declaration
15721 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
15722 and then not
15723 (Nkind (Parent (Alias_Subp)) = N_Procedure_Specification
15724 and then Null_Present (Parent (Alias_Subp)))
15725 then
15726 -- If this is an abstract private type then we transfer the
15727 -- derivation of the interface primitive from the partial view
15728 -- to the full view. This is safe because all the interfaces
15729 -- must be visible in the partial view. Done to avoid adding
15730 -- a new interface derivation to the private part of the
15731 -- enclosing package; otherwise this new derivation would be
15732 -- decorated as hidden when the analysis of the enclosing
15733 -- package completes.
15735 if Is_Abstract_Type (Derived_Type)
15736 and then In_Private_Part (Current_Scope)
15737 and then Has_Private_Declaration (Derived_Type)
15738 then
15739 declare
15740 Partial_View : Entity_Id;
15741 Elmt : Elmt_Id;
15742 Ent : Entity_Id;
15744 begin
15745 Partial_View := First_Entity (Current_Scope);
15746 loop
15747 exit when No (Partial_View)
15748 or else (Has_Private_Declaration (Partial_View)
15749 and then
15750 Full_View (Partial_View) = Derived_Type);
15752 Next_Entity (Partial_View);
15753 end loop;
15755 -- If the partial view was not found then the source code
15756 -- has errors and the derivation is not needed.
15758 if Present (Partial_View) then
15759 Elmt :=
15760 First_Elmt (Primitive_Operations (Partial_View));
15761 while Present (Elmt) loop
15762 Ent := Node (Elmt);
15764 if Present (Alias (Ent))
15765 and then Ultimate_Alias (Ent) = Alias (Subp)
15766 then
15767 Append_Elmt
15768 (Ent, Primitive_Operations (Derived_Type));
15769 exit;
15770 end if;
15772 Next_Elmt (Elmt);
15773 end loop;
15775 -- If the interface primitive was not found in the
15776 -- partial view then this interface primitive was
15777 -- overridden. We add a derivation to activate in
15778 -- Derive_Progenitor_Subprograms the machinery to
15779 -- search for it.
15781 if No (Elmt) then
15782 Derive_Interface_Subprogram
15783 (New_Subp => New_Subp,
15784 Subp => Subp,
15785 Actual_Subp => Act_Subp);
15786 end if;
15787 end if;
15788 end;
15789 else
15790 Derive_Interface_Subprogram
15791 (New_Subp => New_Subp,
15792 Subp => Subp,
15793 Actual_Subp => Act_Subp);
15794 end if;
15796 -- Case 3: Common derivation
15798 else
15799 Derive_Subprogram
15800 (New_Subp => New_Subp,
15801 Parent_Subp => Subp,
15802 Derived_Type => Derived_Type,
15803 Parent_Type => Parent_Base,
15804 Actual_Subp => Act_Subp);
15805 end if;
15807 -- No need to update Act_Elm if we must search for the
15808 -- corresponding operation in the generic actual
15810 if not Need_Search
15811 and then Present (Act_Elmt)
15812 then
15813 Next_Elmt (Act_Elmt);
15814 Act_Subp := Node (Act_Elmt);
15815 end if;
15817 <<Continue>>
15818 Next_Elmt (Elmt);
15819 end loop;
15821 -- Inherit additional operations from progenitors. If the derived
15822 -- type is a generic actual, there are not new primitive operations
15823 -- for the type because it has those of the actual, and therefore
15824 -- nothing needs to be done. The renamings generated above are not
15825 -- primitive operations, and their purpose is simply to make the
15826 -- proper operations visible within an instantiation.
15828 if No (Generic_Actual) then
15829 Derive_Progenitor_Subprograms (Parent_Base, Derived_Type);
15830 end if;
15831 end if;
15833 -- Final check: Direct descendants must have their primitives in the
15834 -- same order. We exclude from this test untagged types and instances
15835 -- of formal derived types. We skip this test if we have already
15836 -- reported serious errors in the sources.
15838 pragma Assert (not Is_Tagged_Type (Derived_Type)
15839 or else Present (Generic_Actual)
15840 or else Serious_Errors_Detected > 0
15841 or else Check_Derived_Type);
15842 end Derive_Subprograms;
15844 --------------------------------
15845 -- Derived_Standard_Character --
15846 --------------------------------
15848 procedure Derived_Standard_Character
15849 (N : Node_Id;
15850 Parent_Type : Entity_Id;
15851 Derived_Type : Entity_Id)
15853 Loc : constant Source_Ptr := Sloc (N);
15854 Def : constant Node_Id := Type_Definition (N);
15855 Indic : constant Node_Id := Subtype_Indication (Def);
15856 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
15857 Implicit_Base : constant Entity_Id :=
15858 Create_Itype
15859 (E_Enumeration_Type, N, Derived_Type, 'B');
15861 Lo : Node_Id;
15862 Hi : Node_Id;
15864 begin
15865 Discard_Node (Process_Subtype (Indic, N));
15867 Set_Etype (Implicit_Base, Parent_Base);
15868 Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
15869 Set_RM_Size (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
15871 Set_Is_Character_Type (Implicit_Base, True);
15872 Set_Has_Delayed_Freeze (Implicit_Base);
15874 -- The bounds of the implicit base are the bounds of the parent base.
15875 -- Note that their type is the parent base.
15877 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
15878 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
15880 Set_Scalar_Range (Implicit_Base,
15881 Make_Range (Loc,
15882 Low_Bound => Lo,
15883 High_Bound => Hi));
15885 Conditional_Delay (Derived_Type, Parent_Type);
15887 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
15888 Set_Etype (Derived_Type, Implicit_Base);
15889 Set_Size_Info (Derived_Type, Parent_Type);
15891 if Unknown_RM_Size (Derived_Type) then
15892 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
15893 end if;
15895 Set_Is_Character_Type (Derived_Type, True);
15897 if Nkind (Indic) /= N_Subtype_Indication then
15899 -- If no explicit constraint, the bounds are those
15900 -- of the parent type.
15902 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Type));
15903 Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
15904 Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
15905 end if;
15907 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
15909 -- Because the implicit base is used in the conversion of the bounds, we
15910 -- have to freeze it now. This is similar to what is done for numeric
15911 -- types, and it equally suspicious, but otherwise a non-static bound
15912 -- will have a reference to an unfrozen type, which is rejected by Gigi
15913 -- (???). This requires specific care for definition of stream
15914 -- attributes. For details, see comments at the end of
15915 -- Build_Derived_Numeric_Type.
15917 Freeze_Before (N, Implicit_Base);
15918 end Derived_Standard_Character;
15920 ------------------------------
15921 -- Derived_Type_Declaration --
15922 ------------------------------
15924 procedure Derived_Type_Declaration
15925 (T : Entity_Id;
15926 N : Node_Id;
15927 Is_Completion : Boolean)
15929 Parent_Type : Entity_Id;
15931 function Comes_From_Generic (Typ : Entity_Id) return Boolean;
15932 -- Check whether the parent type is a generic formal, or derives
15933 -- directly or indirectly from one.
15935 ------------------------
15936 -- Comes_From_Generic --
15937 ------------------------
15939 function Comes_From_Generic (Typ : Entity_Id) return Boolean is
15940 begin
15941 if Is_Generic_Type (Typ) then
15942 return True;
15944 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
15945 return True;
15947 elsif Is_Private_Type (Typ)
15948 and then Present (Full_View (Typ))
15949 and then Is_Generic_Type (Root_Type (Full_View (Typ)))
15950 then
15951 return True;
15953 elsif Is_Generic_Actual_Type (Typ) then
15954 return True;
15956 else
15957 return False;
15958 end if;
15959 end Comes_From_Generic;
15961 -- Local variables
15963 Def : constant Node_Id := Type_Definition (N);
15964 Iface_Def : Node_Id;
15965 Indic : constant Node_Id := Subtype_Indication (Def);
15966 Extension : constant Node_Id := Record_Extension_Part (Def);
15967 Parent_Node : Node_Id;
15968 Taggd : Boolean;
15970 -- Start of processing for Derived_Type_Declaration
15972 begin
15973 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
15975 -- Ada 2005 (AI-251): In case of interface derivation check that the
15976 -- parent is also an interface.
15978 if Interface_Present (Def) then
15979 Check_SPARK_05_Restriction ("interface is not allowed", Def);
15981 if not Is_Interface (Parent_Type) then
15982 Diagnose_Interface (Indic, Parent_Type);
15984 else
15985 Parent_Node := Parent (Base_Type (Parent_Type));
15986 Iface_Def := Type_Definition (Parent_Node);
15988 -- Ada 2005 (AI-251): Limited interfaces can only inherit from
15989 -- other limited interfaces.
15991 if Limited_Present (Def) then
15992 if Limited_Present (Iface_Def) then
15993 null;
15995 elsif Protected_Present (Iface_Def) then
15996 Error_Msg_NE
15997 ("descendant of & must be declared as a protected "
15998 & "interface", N, Parent_Type);
16000 elsif Synchronized_Present (Iface_Def) then
16001 Error_Msg_NE
16002 ("descendant of & must be declared as a synchronized "
16003 & "interface", N, Parent_Type);
16005 elsif Task_Present (Iface_Def) then
16006 Error_Msg_NE
16007 ("descendant of & must be declared as a task interface",
16008 N, Parent_Type);
16010 else
16011 Error_Msg_N
16012 ("(Ada 2005) limited interface cannot inherit from "
16013 & "non-limited interface", Indic);
16014 end if;
16016 -- Ada 2005 (AI-345): Non-limited interfaces can only inherit
16017 -- from non-limited or limited interfaces.
16019 elsif not Protected_Present (Def)
16020 and then not Synchronized_Present (Def)
16021 and then not Task_Present (Def)
16022 then
16023 if Limited_Present (Iface_Def) then
16024 null;
16026 elsif Protected_Present (Iface_Def) then
16027 Error_Msg_NE
16028 ("descendant of & must be declared as a protected "
16029 & "interface", N, Parent_Type);
16031 elsif Synchronized_Present (Iface_Def) then
16032 Error_Msg_NE
16033 ("descendant of & must be declared as a synchronized "
16034 & "interface", N, Parent_Type);
16036 elsif Task_Present (Iface_Def) then
16037 Error_Msg_NE
16038 ("descendant of & must be declared as a task interface",
16039 N, Parent_Type);
16040 else
16041 null;
16042 end if;
16043 end if;
16044 end if;
16045 end if;
16047 if Is_Tagged_Type (Parent_Type)
16048 and then Is_Concurrent_Type (Parent_Type)
16049 and then not Is_Interface (Parent_Type)
16050 then
16051 Error_Msg_N
16052 ("parent type of a record extension cannot be a synchronized "
16053 & "tagged type (RM 3.9.1 (3/1))", N);
16054 Set_Etype (T, Any_Type);
16055 return;
16056 end if;
16058 -- Ada 2005 (AI-251): Decorate all the names in the list of ancestor
16059 -- interfaces
16061 if Is_Tagged_Type (Parent_Type)
16062 and then Is_Non_Empty_List (Interface_List (Def))
16063 then
16064 declare
16065 Intf : Node_Id;
16066 T : Entity_Id;
16068 begin
16069 Intf := First (Interface_List (Def));
16070 while Present (Intf) loop
16071 T := Find_Type_Of_Subtype_Indic (Intf);
16073 if not Is_Interface (T) then
16074 Diagnose_Interface (Intf, T);
16076 -- Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
16077 -- a limited type from having a nonlimited progenitor.
16079 elsif (Limited_Present (Def)
16080 or else (not Is_Interface (Parent_Type)
16081 and then Is_Limited_Type (Parent_Type)))
16082 and then not Is_Limited_Interface (T)
16083 then
16084 Error_Msg_NE
16085 ("progenitor interface& of limited type must be limited",
16086 N, T);
16087 end if;
16089 Next (Intf);
16090 end loop;
16091 end;
16092 end if;
16094 if Parent_Type = Any_Type
16095 or else Etype (Parent_Type) = Any_Type
16096 or else (Is_Class_Wide_Type (Parent_Type)
16097 and then Etype (Parent_Type) = T)
16098 then
16099 -- If Parent_Type is undefined or illegal, make new type into a
16100 -- subtype of Any_Type, and set a few attributes to prevent cascaded
16101 -- errors. If this is a self-definition, emit error now.
16103 if T = Parent_Type or else T = Etype (Parent_Type) then
16104 Error_Msg_N ("type cannot be used in its own definition", Indic);
16105 end if;
16107 Set_Ekind (T, Ekind (Parent_Type));
16108 Set_Etype (T, Any_Type);
16109 Set_Scalar_Range (T, Scalar_Range (Any_Type));
16111 if Is_Tagged_Type (T)
16112 and then Is_Record_Type (T)
16113 then
16114 Set_Direct_Primitive_Operations (T, New_Elmt_List);
16115 end if;
16117 return;
16118 end if;
16120 -- Ada 2005 (AI-251): The case in which the parent of the full-view is
16121 -- an interface is special because the list of interfaces in the full
16122 -- view can be given in any order. For example:
16124 -- type A is interface;
16125 -- type B is interface and A;
16126 -- type D is new B with private;
16127 -- private
16128 -- type D is new A and B with null record; -- 1 --
16130 -- In this case we perform the following transformation of -1-:
16132 -- type D is new B and A with null record;
16134 -- If the parent of the full-view covers the parent of the partial-view
16135 -- we have two possible cases:
16137 -- 1) They have the same parent
16138 -- 2) The parent of the full-view implements some further interfaces
16140 -- In both cases we do not need to perform the transformation. In the
16141 -- first case the source program is correct and the transformation is
16142 -- not needed; in the second case the source program does not fulfill
16143 -- the no-hidden interfaces rule (AI-396) and the error will be reported
16144 -- later.
16146 -- This transformation not only simplifies the rest of the analysis of
16147 -- this type declaration but also simplifies the correct generation of
16148 -- the object layout to the expander.
16150 if In_Private_Part (Current_Scope)
16151 and then Is_Interface (Parent_Type)
16152 then
16153 declare
16154 Iface : Node_Id;
16155 Partial_View : Entity_Id;
16156 Partial_View_Parent : Entity_Id;
16157 New_Iface : Node_Id;
16159 begin
16160 -- Look for the associated private type declaration
16162 Partial_View := First_Entity (Current_Scope);
16163 loop
16164 exit when No (Partial_View)
16165 or else (Has_Private_Declaration (Partial_View)
16166 and then Full_View (Partial_View) = T);
16168 Next_Entity (Partial_View);
16169 end loop;
16171 -- If the partial view was not found then the source code has
16172 -- errors and the transformation is not needed.
16174 if Present (Partial_View) then
16175 Partial_View_Parent := Etype (Partial_View);
16177 -- If the parent of the full-view covers the parent of the
16178 -- partial-view we have nothing else to do.
16180 if Interface_Present_In_Ancestor
16181 (Parent_Type, Partial_View_Parent)
16182 then
16183 null;
16185 -- Traverse the list of interfaces of the full-view to look
16186 -- for the parent of the partial-view and perform the tree
16187 -- transformation.
16189 else
16190 Iface := First (Interface_List (Def));
16191 while Present (Iface) loop
16192 if Etype (Iface) = Etype (Partial_View) then
16193 Rewrite (Subtype_Indication (Def),
16194 New_Copy (Subtype_Indication
16195 (Parent (Partial_View))));
16197 New_Iface :=
16198 Make_Identifier (Sloc (N), Chars (Parent_Type));
16199 Append (New_Iface, Interface_List (Def));
16201 -- Analyze the transformed code
16203 Derived_Type_Declaration (T, N, Is_Completion);
16204 return;
16205 end if;
16207 Next (Iface);
16208 end loop;
16209 end if;
16210 end if;
16211 end;
16212 end if;
16214 -- Only composite types other than array types are allowed to have
16215 -- discriminants.
16217 if Present (Discriminant_Specifications (N)) then
16218 if (Is_Elementary_Type (Parent_Type)
16219 or else
16220 Is_Array_Type (Parent_Type))
16221 and then not Error_Posted (N)
16222 then
16223 Error_Msg_N
16224 ("elementary or array type cannot have discriminants",
16225 Defining_Identifier (First (Discriminant_Specifications (N))));
16226 Set_Has_Discriminants (T, False);
16228 -- The type is allowed to have discriminants
16230 else
16231 Check_SPARK_05_Restriction ("discriminant type is not allowed", N);
16232 end if;
16233 end if;
16235 -- In Ada 83, a derived type defined in a package specification cannot
16236 -- be used for further derivation until the end of its visible part.
16237 -- Note that derivation in the private part of the package is allowed.
16239 if Ada_Version = Ada_83
16240 and then Is_Derived_Type (Parent_Type)
16241 and then In_Visible_Part (Scope (Parent_Type))
16242 then
16243 if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
16244 Error_Msg_N
16245 ("(Ada 83): premature use of type for derivation", Indic);
16246 end if;
16247 end if;
16249 -- Check for early use of incomplete or private type
16251 if Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
16252 Error_Msg_N ("premature derivation of incomplete type", Indic);
16253 return;
16255 elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
16256 and then not Comes_From_Generic (Parent_Type))
16257 or else Has_Private_Component (Parent_Type)
16258 then
16259 -- The ancestor type of a formal type can be incomplete, in which
16260 -- case only the operations of the partial view are available in the
16261 -- generic. Subsequent checks may be required when the full view is
16262 -- analyzed to verify that a derivation from a tagged type has an
16263 -- extension.
16265 if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
16266 null;
16268 elsif No (Underlying_Type (Parent_Type))
16269 or else Has_Private_Component (Parent_Type)
16270 then
16271 Error_Msg_N
16272 ("premature derivation of derived or private type", Indic);
16274 -- Flag the type itself as being in error, this prevents some
16275 -- nasty problems with subsequent uses of the malformed type.
16277 Set_Error_Posted (T);
16279 -- Check that within the immediate scope of an untagged partial
16280 -- view it's illegal to derive from the partial view if the
16281 -- full view is tagged. (7.3(7))
16283 -- We verify that the Parent_Type is a partial view by checking
16284 -- that it is not a Full_Type_Declaration (i.e. a private type or
16285 -- private extension declaration), to distinguish a partial view
16286 -- from a derivation from a private type which also appears as
16287 -- E_Private_Type. If the parent base type is not declared in an
16288 -- enclosing scope there is no need to check.
16290 elsif Present (Full_View (Parent_Type))
16291 and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
16292 and then not Is_Tagged_Type (Parent_Type)
16293 and then Is_Tagged_Type (Full_View (Parent_Type))
16294 and then In_Open_Scopes (Scope (Base_Type (Parent_Type)))
16295 then
16296 Error_Msg_N
16297 ("premature derivation from type with tagged full view",
16298 Indic);
16299 end if;
16300 end if;
16302 -- Check that form of derivation is appropriate
16304 Taggd := Is_Tagged_Type (Parent_Type);
16306 -- Set the parent type to the class-wide type's specific type in this
16307 -- case to prevent cascading errors
16309 if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
16310 Error_Msg_N ("parent type must not be a class-wide type", Indic);
16311 Set_Etype (T, Etype (Parent_Type));
16312 return;
16313 end if;
16315 if Present (Extension) and then not Taggd then
16316 Error_Msg_N
16317 ("type derived from untagged type cannot have extension", Indic);
16319 elsif No (Extension) and then Taggd then
16321 -- If this declaration is within a private part (or body) of a
16322 -- generic instantiation then the derivation is allowed (the parent
16323 -- type can only appear tagged in this case if it's a generic actual
16324 -- type, since it would otherwise have been rejected in the analysis
16325 -- of the generic template).
16327 if not Is_Generic_Actual_Type (Parent_Type)
16328 or else In_Visible_Part (Scope (Parent_Type))
16329 then
16330 if Is_Class_Wide_Type (Parent_Type) then
16331 Error_Msg_N
16332 ("parent type must not be a class-wide type", Indic);
16334 -- Use specific type to prevent cascaded errors.
16336 Parent_Type := Etype (Parent_Type);
16338 else
16339 Error_Msg_N
16340 ("type derived from tagged type must have extension", Indic);
16341 end if;
16342 end if;
16343 end if;
16345 -- AI-443: Synchronized formal derived types require a private
16346 -- extension. There is no point in checking the ancestor type or
16347 -- the progenitors since the construct is wrong to begin with.
16349 if Ada_Version >= Ada_2005
16350 and then Is_Generic_Type (T)
16351 and then Present (Original_Node (N))
16352 then
16353 declare
16354 Decl : constant Node_Id := Original_Node (N);
16356 begin
16357 if Nkind (Decl) = N_Formal_Type_Declaration
16358 and then Nkind (Formal_Type_Definition (Decl)) =
16359 N_Formal_Derived_Type_Definition
16360 and then Synchronized_Present (Formal_Type_Definition (Decl))
16361 and then No (Extension)
16363 -- Avoid emitting a duplicate error message
16365 and then not Error_Posted (Indic)
16366 then
16367 Error_Msg_N
16368 ("synchronized derived type must have extension", N);
16369 end if;
16370 end;
16371 end if;
16373 if Null_Exclusion_Present (Def)
16374 and then not Is_Access_Type (Parent_Type)
16375 then
16376 Error_Msg_N ("null exclusion can only apply to an access type", N);
16377 end if;
16379 -- Avoid deriving parent primitives of underlying record views
16381 Build_Derived_Type (N, Parent_Type, T, Is_Completion,
16382 Derive_Subps => not Is_Underlying_Record_View (T));
16384 -- AI-419: The parent type of an explicitly limited derived type must
16385 -- be a limited type or a limited interface.
16387 if Limited_Present (Def) then
16388 Set_Is_Limited_Record (T);
16390 if Is_Interface (T) then
16391 Set_Is_Limited_Interface (T);
16392 end if;
16394 if not Is_Limited_Type (Parent_Type)
16395 and then
16396 (not Is_Interface (Parent_Type)
16397 or else not Is_Limited_Interface (Parent_Type))
16398 then
16399 -- AI05-0096: a derivation in the private part of an instance is
16400 -- legal if the generic formal is untagged limited, and the actual
16401 -- is non-limited.
16403 if Is_Generic_Actual_Type (Parent_Type)
16404 and then In_Private_Part (Current_Scope)
16405 and then
16406 not Is_Tagged_Type
16407 (Generic_Parent_Type (Parent (Parent_Type)))
16408 then
16409 null;
16411 else
16412 Error_Msg_NE
16413 ("parent type& of limited type must be limited",
16414 N, Parent_Type);
16415 end if;
16416 end if;
16417 end if;
16419 -- In SPARK, there are no derived type definitions other than type
16420 -- extensions of tagged record types.
16422 if No (Extension) then
16423 Check_SPARK_05_Restriction
16424 ("derived type is not allowed", Original_Node (N));
16425 end if;
16426 end Derived_Type_Declaration;
16428 ------------------------
16429 -- Diagnose_Interface --
16430 ------------------------
16432 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id) is
16433 begin
16434 if not Is_Interface (E) and then E /= Any_Type then
16435 Error_Msg_NE ("(Ada 2005) & must be an interface", N, E);
16436 end if;
16437 end Diagnose_Interface;
16439 ----------------------------------
16440 -- Enumeration_Type_Declaration --
16441 ----------------------------------
16443 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
16444 Ev : Uint;
16445 L : Node_Id;
16446 R_Node : Node_Id;
16447 B_Node : Node_Id;
16449 begin
16450 -- Create identifier node representing lower bound
16452 B_Node := New_Node (N_Identifier, Sloc (Def));
16453 L := First (Literals (Def));
16454 Set_Chars (B_Node, Chars (L));
16455 Set_Entity (B_Node, L);
16456 Set_Etype (B_Node, T);
16457 Set_Is_Static_Expression (B_Node, True);
16459 R_Node := New_Node (N_Range, Sloc (Def));
16460 Set_Low_Bound (R_Node, B_Node);
16462 Set_Ekind (T, E_Enumeration_Type);
16463 Set_First_Literal (T, L);
16464 Set_Etype (T, T);
16465 Set_Is_Constrained (T);
16467 Ev := Uint_0;
16469 -- Loop through literals of enumeration type setting pos and rep values
16470 -- except that if the Ekind is already set, then it means the literal
16471 -- was already constructed (case of a derived type declaration and we
16472 -- should not disturb the Pos and Rep values.
16474 while Present (L) loop
16475 if Ekind (L) /= E_Enumeration_Literal then
16476 Set_Ekind (L, E_Enumeration_Literal);
16477 Set_Enumeration_Pos (L, Ev);
16478 Set_Enumeration_Rep (L, Ev);
16479 Set_Is_Known_Valid (L, True);
16480 end if;
16482 Set_Etype (L, T);
16483 New_Overloaded_Entity (L);
16484 Generate_Definition (L);
16485 Set_Convention (L, Convention_Intrinsic);
16487 -- Case of character literal
16489 if Nkind (L) = N_Defining_Character_Literal then
16490 Set_Is_Character_Type (T, True);
16492 -- Check violation of No_Wide_Characters
16494 if Restriction_Check_Required (No_Wide_Characters) then
16495 Get_Name_String (Chars (L));
16497 if Name_Len >= 3 and then Name_Buffer (1 .. 2) = "QW" then
16498 Check_Restriction (No_Wide_Characters, L);
16499 end if;
16500 end if;
16501 end if;
16503 Ev := Ev + 1;
16504 Next (L);
16505 end loop;
16507 -- Now create a node representing upper bound
16509 B_Node := New_Node (N_Identifier, Sloc (Def));
16510 Set_Chars (B_Node, Chars (Last (Literals (Def))));
16511 Set_Entity (B_Node, Last (Literals (Def)));
16512 Set_Etype (B_Node, T);
16513 Set_Is_Static_Expression (B_Node, True);
16515 Set_High_Bound (R_Node, B_Node);
16517 -- Initialize various fields of the type. Some of this information
16518 -- may be overwritten later through rep.clauses.
16520 Set_Scalar_Range (T, R_Node);
16521 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
16522 Set_Enum_Esize (T);
16523 Set_Enum_Pos_To_Rep (T, Empty);
16525 -- Set Discard_Names if configuration pragma set, or if there is
16526 -- a parameterless pragma in the current declarative region
16528 if Global_Discard_Names or else Discard_Names (Scope (T)) then
16529 Set_Discard_Names (T);
16530 end if;
16532 -- Process end label if there is one
16534 if Present (Def) then
16535 Process_End_Label (Def, 'e', T);
16536 end if;
16537 end Enumeration_Type_Declaration;
16539 ---------------------------------
16540 -- Expand_To_Stored_Constraint --
16541 ---------------------------------
16543 function Expand_To_Stored_Constraint
16544 (Typ : Entity_Id;
16545 Constraint : Elist_Id) return Elist_Id
16547 Explicitly_Discriminated_Type : Entity_Id;
16548 Expansion : Elist_Id;
16549 Discriminant : Entity_Id;
16551 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
16552 -- Find the nearest type that actually specifies discriminants
16554 ---------------------------------
16555 -- Type_With_Explicit_Discrims --
16556 ---------------------------------
16558 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
16559 Typ : constant E := Base_Type (Id);
16561 begin
16562 if Ekind (Typ) in Incomplete_Or_Private_Kind then
16563 if Present (Full_View (Typ)) then
16564 return Type_With_Explicit_Discrims (Full_View (Typ));
16565 end if;
16567 else
16568 if Has_Discriminants (Typ) then
16569 return Typ;
16570 end if;
16571 end if;
16573 if Etype (Typ) = Typ then
16574 return Empty;
16575 elsif Has_Discriminants (Typ) then
16576 return Typ;
16577 else
16578 return Type_With_Explicit_Discrims (Etype (Typ));
16579 end if;
16581 end Type_With_Explicit_Discrims;
16583 -- Start of processing for Expand_To_Stored_Constraint
16585 begin
16586 if No (Constraint) or else Is_Empty_Elmt_List (Constraint) then
16587 return No_Elist;
16588 end if;
16590 Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
16592 if No (Explicitly_Discriminated_Type) then
16593 return No_Elist;
16594 end if;
16596 Expansion := New_Elmt_List;
16598 Discriminant :=
16599 First_Stored_Discriminant (Explicitly_Discriminated_Type);
16600 while Present (Discriminant) loop
16601 Append_Elmt
16602 (Get_Discriminant_Value
16603 (Discriminant, Explicitly_Discriminated_Type, Constraint),
16604 To => Expansion);
16605 Next_Stored_Discriminant (Discriminant);
16606 end loop;
16608 return Expansion;
16609 end Expand_To_Stored_Constraint;
16611 ---------------------------
16612 -- Find_Hidden_Interface --
16613 ---------------------------
16615 function Find_Hidden_Interface
16616 (Src : Elist_Id;
16617 Dest : Elist_Id) return Entity_Id
16619 Iface : Entity_Id;
16620 Iface_Elmt : Elmt_Id;
16622 begin
16623 if Present (Src) and then Present (Dest) then
16624 Iface_Elmt := First_Elmt (Src);
16625 while Present (Iface_Elmt) loop
16626 Iface := Node (Iface_Elmt);
16628 if Is_Interface (Iface)
16629 and then not Contain_Interface (Iface, Dest)
16630 then
16631 return Iface;
16632 end if;
16634 Next_Elmt (Iface_Elmt);
16635 end loop;
16636 end if;
16638 return Empty;
16639 end Find_Hidden_Interface;
16641 --------------------
16642 -- Find_Type_Name --
16643 --------------------
16645 function Find_Type_Name (N : Node_Id) return Entity_Id is
16646 Id : constant Entity_Id := Defining_Identifier (N);
16647 New_Id : Entity_Id;
16648 Prev : Entity_Id;
16649 Prev_Par : Node_Id;
16651 procedure Check_Duplicate_Aspects;
16652 -- Check that aspects specified in a completion have not been specified
16653 -- already in the partial view.
16655 procedure Tag_Mismatch;
16656 -- Diagnose a tagged partial view whose full view is untagged. We post
16657 -- the message on the full view, with a reference to the previous
16658 -- partial view. The partial view can be private or incomplete, and
16659 -- these are handled in a different manner, so we determine the position
16660 -- of the error message from the respective slocs of both.
16662 -----------------------------
16663 -- Check_Duplicate_Aspects --
16664 -----------------------------
16666 procedure Check_Duplicate_Aspects is
16667 function Get_Partial_View_Aspect (Asp : Node_Id) return Node_Id;
16668 -- Return the corresponding aspect of the partial view which matches
16669 -- the aspect id of Asp. Return Empty is no such aspect exists.
16671 -----------------------------
16672 -- Get_Partial_View_Aspect --
16673 -----------------------------
16675 function Get_Partial_View_Aspect (Asp : Node_Id) return Node_Id is
16676 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp);
16677 Prev_Asps : constant List_Id := Aspect_Specifications (Prev_Par);
16678 Prev_Asp : Node_Id;
16680 begin
16681 if Present (Prev_Asps) then
16682 Prev_Asp := First (Prev_Asps);
16683 while Present (Prev_Asp) loop
16684 if Get_Aspect_Id (Prev_Asp) = Asp_Id then
16685 return Prev_Asp;
16686 end if;
16688 Next (Prev_Asp);
16689 end loop;
16690 end if;
16692 return Empty;
16693 end Get_Partial_View_Aspect;
16695 -- Local variables
16697 Full_Asps : constant List_Id := Aspect_Specifications (N);
16698 Full_Asp : Node_Id;
16699 Part_Asp : Node_Id;
16701 -- Start of processing for Check_Duplicate_Aspects
16703 begin
16704 if Present (Full_Asps) then
16705 Full_Asp := First (Full_Asps);
16706 while Present (Full_Asp) loop
16707 Part_Asp := Get_Partial_View_Aspect (Full_Asp);
16709 -- An aspect and its class-wide counterpart are two distinct
16710 -- aspects and may apply to both views of an entity.
16712 if Present (Part_Asp)
16713 and then Class_Present (Part_Asp) = Class_Present (Full_Asp)
16714 then
16715 Error_Msg_N
16716 ("aspect already specified in private declaration",
16717 Full_Asp);
16719 Remove (Full_Asp);
16720 return;
16721 end if;
16723 if Has_Discriminants (Prev)
16724 and then not Has_Unknown_Discriminants (Prev)
16725 and then Get_Aspect_Id (Full_Asp) =
16726 Aspect_Implicit_Dereference
16727 then
16728 Error_Msg_N
16729 ("cannot specify aspect if partial view has known "
16730 & "discriminants", Full_Asp);
16731 end if;
16733 Next (Full_Asp);
16734 end loop;
16735 end if;
16736 end Check_Duplicate_Aspects;
16738 ------------------
16739 -- Tag_Mismatch --
16740 ------------------
16742 procedure Tag_Mismatch is
16743 begin
16744 if Sloc (Prev) < Sloc (Id) then
16745 if Ada_Version >= Ada_2012
16746 and then Nkind (N) = N_Private_Type_Declaration
16747 then
16748 Error_Msg_NE
16749 ("declaration of private } must be a tagged type ", Id, Prev);
16750 else
16751 Error_Msg_NE
16752 ("full declaration of } must be a tagged type ", Id, Prev);
16753 end if;
16755 else
16756 if Ada_Version >= Ada_2012
16757 and then Nkind (N) = N_Private_Type_Declaration
16758 then
16759 Error_Msg_NE
16760 ("declaration of private } must be a tagged type ", Prev, Id);
16761 else
16762 Error_Msg_NE
16763 ("full declaration of } must be a tagged type ", Prev, Id);
16764 end if;
16765 end if;
16766 end Tag_Mismatch;
16768 -- Start of processing for Find_Type_Name
16770 begin
16771 -- Find incomplete declaration, if one was given
16773 Prev := Current_Entity_In_Scope (Id);
16775 -- New type declaration
16777 if No (Prev) then
16778 Enter_Name (Id);
16779 return Id;
16781 -- Previous declaration exists
16783 else
16784 Prev_Par := Parent (Prev);
16786 -- Error if not incomplete/private case except if previous
16787 -- declaration is implicit, etc. Enter_Name will emit error if
16788 -- appropriate.
16790 if not Is_Incomplete_Or_Private_Type (Prev) then
16791 Enter_Name (Id);
16792 New_Id := Id;
16794 -- Check invalid completion of private or incomplete type
16796 elsif not Nkind_In (N, N_Full_Type_Declaration,
16797 N_Task_Type_Declaration,
16798 N_Protected_Type_Declaration)
16799 and then
16800 (Ada_Version < Ada_2012
16801 or else not Is_Incomplete_Type (Prev)
16802 or else not Nkind_In (N, N_Private_Type_Declaration,
16803 N_Private_Extension_Declaration))
16804 then
16805 -- Completion must be a full type declarations (RM 7.3(4))
16807 Error_Msg_Sloc := Sloc (Prev);
16808 Error_Msg_NE ("invalid completion of }", Id, Prev);
16810 -- Set scope of Id to avoid cascaded errors. Entity is never
16811 -- examined again, except when saving globals in generics.
16813 Set_Scope (Id, Current_Scope);
16814 New_Id := Id;
16816 -- If this is a repeated incomplete declaration, no further
16817 -- checks are possible.
16819 if Nkind (N) = N_Incomplete_Type_Declaration then
16820 return Prev;
16821 end if;
16823 -- Case of full declaration of incomplete type
16825 elsif Ekind (Prev) = E_Incomplete_Type
16826 and then (Ada_Version < Ada_2012
16827 or else No (Full_View (Prev))
16828 or else not Is_Private_Type (Full_View (Prev)))
16829 then
16830 -- Indicate that the incomplete declaration has a matching full
16831 -- declaration. The defining occurrence of the incomplete
16832 -- declaration remains the visible one, and the procedure
16833 -- Get_Full_View dereferences it whenever the type is used.
16835 if Present (Full_View (Prev)) then
16836 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
16837 end if;
16839 Set_Full_View (Prev, Id);
16840 Append_Entity (Id, Current_Scope);
16841 Set_Is_Public (Id, Is_Public (Prev));
16842 Set_Is_Internal (Id);
16843 New_Id := Prev;
16845 -- If the incomplete view is tagged, a class_wide type has been
16846 -- created already. Use it for the private type as well, in order
16847 -- to prevent multiple incompatible class-wide types that may be
16848 -- created for self-referential anonymous access components.
16850 if Is_Tagged_Type (Prev)
16851 and then Present (Class_Wide_Type (Prev))
16852 then
16853 Set_Ekind (Id, Ekind (Prev)); -- will be reset later
16854 Set_Class_Wide_Type (Id, Class_Wide_Type (Prev));
16856 -- Type of the class-wide type is the current Id. Previously
16857 -- this was not done for private declarations because of order-
16858 -- of-elaboration issues in the back end, but gigi now handles
16859 -- this properly.
16861 Set_Etype (Class_Wide_Type (Id), Id);
16862 end if;
16864 -- Case of full declaration of private type
16866 else
16867 -- If the private type was a completion of an incomplete type then
16868 -- update Prev to reference the private type
16870 if Ada_Version >= Ada_2012
16871 and then Ekind (Prev) = E_Incomplete_Type
16872 and then Present (Full_View (Prev))
16873 and then Is_Private_Type (Full_View (Prev))
16874 then
16875 Prev := Full_View (Prev);
16876 Prev_Par := Parent (Prev);
16877 end if;
16879 if Nkind (N) = N_Full_Type_Declaration
16880 and then Nkind_In
16881 (Type_Definition (N), N_Record_Definition,
16882 N_Derived_Type_Definition)
16883 and then Interface_Present (Type_Definition (N))
16884 then
16885 Error_Msg_N
16886 ("completion of private type cannot be an interface", N);
16887 end if;
16889 if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
16890 if Etype (Prev) /= Prev then
16892 -- Prev is a private subtype or a derived type, and needs
16893 -- no completion.
16895 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
16896 New_Id := Id;
16898 elsif Ekind (Prev) = E_Private_Type
16899 and then Nkind_In (N, N_Task_Type_Declaration,
16900 N_Protected_Type_Declaration)
16901 then
16902 Error_Msg_N
16903 ("completion of nonlimited type cannot be limited", N);
16905 elsif Ekind (Prev) = E_Record_Type_With_Private
16906 and then Nkind_In (N, N_Task_Type_Declaration,
16907 N_Protected_Type_Declaration)
16908 then
16909 if not Is_Limited_Record (Prev) then
16910 Error_Msg_N
16911 ("completion of nonlimited type cannot be limited", N);
16913 elsif No (Interface_List (N)) then
16914 Error_Msg_N
16915 ("completion of tagged private type must be tagged",
16917 end if;
16918 end if;
16920 -- Ada 2005 (AI-251): Private extension declaration of a task
16921 -- type or a protected type. This case arises when covering
16922 -- interface types.
16924 elsif Nkind_In (N, N_Task_Type_Declaration,
16925 N_Protected_Type_Declaration)
16926 then
16927 null;
16929 elsif Nkind (N) /= N_Full_Type_Declaration
16930 or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
16931 then
16932 Error_Msg_N
16933 ("full view of private extension must be an extension", N);
16935 elsif not (Abstract_Present (Parent (Prev)))
16936 and then Abstract_Present (Type_Definition (N))
16937 then
16938 Error_Msg_N
16939 ("full view of non-abstract extension cannot be abstract", N);
16940 end if;
16942 if not In_Private_Part (Current_Scope) then
16943 Error_Msg_N
16944 ("declaration of full view must appear in private part", N);
16945 end if;
16947 if Ada_Version >= Ada_2012 then
16948 Check_Duplicate_Aspects;
16949 end if;
16951 Copy_And_Swap (Prev, Id);
16952 Set_Has_Private_Declaration (Prev);
16953 Set_Has_Private_Declaration (Id);
16955 -- AI12-0133: Indicate whether we have a partial view with
16956 -- unknown discriminants, in which case initialization of objects
16957 -- of the type do not receive an invariant check.
16959 Set_Partial_View_Has_Unknown_Discr
16960 (Prev, Has_Unknown_Discriminants (Id));
16962 -- Preserve aspect and iterator flags that may have been set on
16963 -- the partial view.
16965 Set_Has_Delayed_Aspects (Prev, Has_Delayed_Aspects (Id));
16966 Set_Has_Implicit_Dereference (Prev, Has_Implicit_Dereference (Id));
16968 -- If no error, propagate freeze_node from private to full view.
16969 -- It may have been generated for an early operational item.
16971 if Present (Freeze_Node (Id))
16972 and then Serious_Errors_Detected = 0
16973 and then No (Full_View (Id))
16974 then
16975 Set_Freeze_Node (Prev, Freeze_Node (Id));
16976 Set_Freeze_Node (Id, Empty);
16977 Set_First_Rep_Item (Prev, First_Rep_Item (Id));
16978 end if;
16980 Set_Full_View (Id, Prev);
16981 New_Id := Prev;
16982 end if;
16984 -- Verify that full declaration conforms to partial one
16986 if Is_Incomplete_Or_Private_Type (Prev)
16987 and then Present (Discriminant_Specifications (Prev_Par))
16988 then
16989 if Present (Discriminant_Specifications (N)) then
16990 if Ekind (Prev) = E_Incomplete_Type then
16991 Check_Discriminant_Conformance (N, Prev, Prev);
16992 else
16993 Check_Discriminant_Conformance (N, Prev, Id);
16994 end if;
16996 else
16997 Error_Msg_N
16998 ("missing discriminants in full type declaration", N);
17000 -- To avoid cascaded errors on subsequent use, share the
17001 -- discriminants of the partial view.
17003 Set_Discriminant_Specifications (N,
17004 Discriminant_Specifications (Prev_Par));
17005 end if;
17006 end if;
17008 -- A prior untagged partial view can have an associated class-wide
17009 -- type due to use of the class attribute, and in this case the full
17010 -- type must also be tagged. This Ada 95 usage is deprecated in favor
17011 -- of incomplete tagged declarations, but we check for it.
17013 if Is_Type (Prev)
17014 and then (Is_Tagged_Type (Prev)
17015 or else Present (Class_Wide_Type (Prev)))
17016 then
17017 -- Ada 2012 (AI05-0162): A private type may be the completion of
17018 -- an incomplete type.
17020 if Ada_Version >= Ada_2012
17021 and then Is_Incomplete_Type (Prev)
17022 and then Nkind_In (N, N_Private_Type_Declaration,
17023 N_Private_Extension_Declaration)
17024 then
17025 -- No need to check private extensions since they are tagged
17027 if Nkind (N) = N_Private_Type_Declaration
17028 and then not Tagged_Present (N)
17029 then
17030 Tag_Mismatch;
17031 end if;
17033 -- The full declaration is either a tagged type (including
17034 -- a synchronized type that implements interfaces) or a
17035 -- type extension, otherwise this is an error.
17037 elsif Nkind_In (N, N_Task_Type_Declaration,
17038 N_Protected_Type_Declaration)
17039 then
17040 if No (Interface_List (N)) and then not Error_Posted (N) then
17041 Tag_Mismatch;
17042 end if;
17044 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
17046 -- Indicate that the previous declaration (tagged incomplete
17047 -- or private declaration) requires the same on the full one.
17049 if not Tagged_Present (Type_Definition (N)) then
17050 Tag_Mismatch;
17051 Set_Is_Tagged_Type (Id);
17052 end if;
17054 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
17055 if No (Record_Extension_Part (Type_Definition (N))) then
17056 Error_Msg_NE
17057 ("full declaration of } must be a record extension",
17058 Prev, Id);
17060 -- Set some attributes to produce a usable full view
17062 Set_Is_Tagged_Type (Id);
17063 end if;
17065 else
17066 Tag_Mismatch;
17067 end if;
17068 end if;
17070 if Present (Prev)
17071 and then Nkind (Parent (Prev)) = N_Incomplete_Type_Declaration
17072 and then Present (Premature_Use (Parent (Prev)))
17073 then
17074 Error_Msg_Sloc := Sloc (N);
17075 Error_Msg_N
17076 ("\full declaration #", Premature_Use (Parent (Prev)));
17077 end if;
17079 return New_Id;
17080 end if;
17081 end Find_Type_Name;
17083 -------------------------
17084 -- Find_Type_Of_Object --
17085 -------------------------
17087 function Find_Type_Of_Object
17088 (Obj_Def : Node_Id;
17089 Related_Nod : Node_Id) return Entity_Id
17091 Def_Kind : constant Node_Kind := Nkind (Obj_Def);
17092 P : Node_Id := Parent (Obj_Def);
17093 T : Entity_Id;
17094 Nam : Name_Id;
17096 begin
17097 -- If the parent is a component_definition node we climb to the
17098 -- component_declaration node
17100 if Nkind (P) = N_Component_Definition then
17101 P := Parent (P);
17102 end if;
17104 -- Case of an anonymous array subtype
17106 if Nkind_In (Def_Kind, N_Constrained_Array_Definition,
17107 N_Unconstrained_Array_Definition)
17108 then
17109 T := Empty;
17110 Array_Type_Declaration (T, Obj_Def);
17112 -- Create an explicit subtype whenever possible
17114 elsif Nkind (P) /= N_Component_Declaration
17115 and then Def_Kind = N_Subtype_Indication
17116 then
17117 -- Base name of subtype on object name, which will be unique in
17118 -- the current scope.
17120 -- If this is a duplicate declaration, return base type, to avoid
17121 -- generating duplicate anonymous types.
17123 if Error_Posted (P) then
17124 Analyze (Subtype_Mark (Obj_Def));
17125 return Entity (Subtype_Mark (Obj_Def));
17126 end if;
17128 Nam :=
17129 New_External_Name
17130 (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
17132 T := Make_Defining_Identifier (Sloc (P), Nam);
17134 Insert_Action (Obj_Def,
17135 Make_Subtype_Declaration (Sloc (P),
17136 Defining_Identifier => T,
17137 Subtype_Indication => Relocate_Node (Obj_Def)));
17139 -- This subtype may need freezing, and this will not be done
17140 -- automatically if the object declaration is not in declarative
17141 -- part. Since this is an object declaration, the type cannot always
17142 -- be frozen here. Deferred constants do not freeze their type
17143 -- (which often enough will be private).
17145 if Nkind (P) = N_Object_Declaration
17146 and then Constant_Present (P)
17147 and then No (Expression (P))
17148 then
17149 null;
17151 -- Here we freeze the base type of object type to catch premature use
17152 -- of discriminated private type without a full view.
17154 else
17155 Insert_Actions (Obj_Def, Freeze_Entity (Base_Type (T), P));
17156 end if;
17158 -- Ada 2005 AI-406: the object definition in an object declaration
17159 -- can be an access definition.
17161 elsif Def_Kind = N_Access_Definition then
17162 T := Access_Definition (Related_Nod, Obj_Def);
17164 Set_Is_Local_Anonymous_Access
17166 V => (Ada_Version < Ada_2012)
17167 or else (Nkind (P) /= N_Object_Declaration)
17168 or else Is_Library_Level_Entity (Defining_Identifier (P)));
17170 -- Otherwise, the object definition is just a subtype_mark
17172 else
17173 T := Process_Subtype (Obj_Def, Related_Nod);
17175 -- If expansion is disabled an object definition that is an aggregate
17176 -- will not get expanded and may lead to scoping problems in the back
17177 -- end, if the object is referenced in an inner scope. In that case
17178 -- create an itype reference for the object definition now. This
17179 -- may be redundant in some cases, but harmless.
17181 if Is_Itype (T)
17182 and then Nkind (Related_Nod) = N_Object_Declaration
17183 and then ASIS_Mode
17184 then
17185 Build_Itype_Reference (T, Related_Nod);
17186 end if;
17187 end if;
17189 return T;
17190 end Find_Type_Of_Object;
17192 --------------------------------
17193 -- Find_Type_Of_Subtype_Indic --
17194 --------------------------------
17196 function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
17197 Typ : Entity_Id;
17199 begin
17200 -- Case of subtype mark with a constraint
17202 if Nkind (S) = N_Subtype_Indication then
17203 Find_Type (Subtype_Mark (S));
17204 Typ := Entity (Subtype_Mark (S));
17206 if not
17207 Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
17208 then
17209 Error_Msg_N
17210 ("incorrect constraint for this kind of type", Constraint (S));
17211 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
17212 end if;
17214 -- Otherwise we have a subtype mark without a constraint
17216 elsif Error_Posted (S) then
17217 Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
17218 return Any_Type;
17220 else
17221 Find_Type (S);
17222 Typ := Entity (S);
17223 end if;
17225 -- Check No_Wide_Characters restriction
17227 Check_Wide_Character_Restriction (Typ, S);
17229 return Typ;
17230 end Find_Type_Of_Subtype_Indic;
17232 -------------------------------------
17233 -- Floating_Point_Type_Declaration --
17234 -------------------------------------
17236 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
17237 Digs : constant Node_Id := Digits_Expression (Def);
17238 Max_Digs_Val : constant Uint := Digits_Value (Standard_Long_Long_Float);
17239 Digs_Val : Uint;
17240 Base_Typ : Entity_Id;
17241 Implicit_Base : Entity_Id;
17242 Bound : Node_Id;
17244 function Can_Derive_From (E : Entity_Id) return Boolean;
17245 -- Find if given digits value, and possibly a specified range, allows
17246 -- derivation from specified type
17248 function Find_Base_Type return Entity_Id;
17249 -- Find a predefined base type that Def can derive from, or generate
17250 -- an error and substitute Long_Long_Float if none exists.
17252 ---------------------
17253 -- Can_Derive_From --
17254 ---------------------
17256 function Can_Derive_From (E : Entity_Id) return Boolean is
17257 Spec : constant Entity_Id := Real_Range_Specification (Def);
17259 begin
17260 -- Check specified "digits" constraint
17262 if Digs_Val > Digits_Value (E) then
17263 return False;
17264 end if;
17266 -- Check for matching range, if specified
17268 if Present (Spec) then
17269 if Expr_Value_R (Type_Low_Bound (E)) >
17270 Expr_Value_R (Low_Bound (Spec))
17271 then
17272 return False;
17273 end if;
17275 if Expr_Value_R (Type_High_Bound (E)) <
17276 Expr_Value_R (High_Bound (Spec))
17277 then
17278 return False;
17279 end if;
17280 end if;
17282 return True;
17283 end Can_Derive_From;
17285 --------------------
17286 -- Find_Base_Type --
17287 --------------------
17289 function Find_Base_Type return Entity_Id is
17290 Choice : Elmt_Id := First_Elmt (Predefined_Float_Types);
17292 begin
17293 -- Iterate over the predefined types in order, returning the first
17294 -- one that Def can derive from.
17296 while Present (Choice) loop
17297 if Can_Derive_From (Node (Choice)) then
17298 return Node (Choice);
17299 end if;
17301 Next_Elmt (Choice);
17302 end loop;
17304 -- If we can't derive from any existing type, use Long_Long_Float
17305 -- and give appropriate message explaining the problem.
17307 if Digs_Val > Max_Digs_Val then
17308 -- It might be the case that there is a type with the requested
17309 -- range, just not the combination of digits and range.
17311 Error_Msg_N
17312 ("no predefined type has requested range and precision",
17313 Real_Range_Specification (Def));
17315 else
17316 Error_Msg_N
17317 ("range too large for any predefined type",
17318 Real_Range_Specification (Def));
17319 end if;
17321 return Standard_Long_Long_Float;
17322 end Find_Base_Type;
17324 -- Start of processing for Floating_Point_Type_Declaration
17326 begin
17327 Check_Restriction (No_Floating_Point, Def);
17329 -- Create an implicit base type
17331 Implicit_Base :=
17332 Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
17334 -- Analyze and verify digits value
17336 Analyze_And_Resolve (Digs, Any_Integer);
17337 Check_Digits_Expression (Digs);
17338 Digs_Val := Expr_Value (Digs);
17340 -- Process possible range spec and find correct type to derive from
17342 Process_Real_Range_Specification (Def);
17344 -- Check that requested number of digits is not too high.
17346 if Digs_Val > Max_Digs_Val then
17348 -- The check for Max_Base_Digits may be somewhat expensive, as it
17349 -- requires reading System, so only do it when necessary.
17351 declare
17352 Max_Base_Digits : constant Uint :=
17353 Expr_Value
17354 (Expression
17355 (Parent (RTE (RE_Max_Base_Digits))));
17357 begin
17358 if Digs_Val > Max_Base_Digits then
17359 Error_Msg_Uint_1 := Max_Base_Digits;
17360 Error_Msg_N ("digits value out of range, maximum is ^", Digs);
17362 elsif No (Real_Range_Specification (Def)) then
17363 Error_Msg_Uint_1 := Max_Digs_Val;
17364 Error_Msg_N ("types with more than ^ digits need range spec "
17365 & "(RM 3.5.7(6))", Digs);
17366 end if;
17367 end;
17368 end if;
17370 -- Find a suitable type to derive from or complain and use a substitute
17372 Base_Typ := Find_Base_Type;
17374 -- If there are bounds given in the declaration use them as the bounds
17375 -- of the type, otherwise use the bounds of the predefined base type
17376 -- that was chosen based on the Digits value.
17378 if Present (Real_Range_Specification (Def)) then
17379 Set_Scalar_Range (T, Real_Range_Specification (Def));
17380 Set_Is_Constrained (T);
17382 -- The bounds of this range must be converted to machine numbers
17383 -- in accordance with RM 4.9(38).
17385 Bound := Type_Low_Bound (T);
17387 if Nkind (Bound) = N_Real_Literal then
17388 Set_Realval
17389 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
17390 Set_Is_Machine_Number (Bound);
17391 end if;
17393 Bound := Type_High_Bound (T);
17395 if Nkind (Bound) = N_Real_Literal then
17396 Set_Realval
17397 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
17398 Set_Is_Machine_Number (Bound);
17399 end if;
17401 else
17402 Set_Scalar_Range (T, Scalar_Range (Base_Typ));
17403 end if;
17405 -- Complete definition of implicit base and declared first subtype. The
17406 -- inheritance of the rep item chain ensures that SPARK-related pragmas
17407 -- are not clobbered when the floating point type acts as a full view of
17408 -- a private type.
17410 Set_Etype (Implicit_Base, Base_Typ);
17411 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
17412 Set_Size_Info (Implicit_Base, Base_Typ);
17413 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
17414 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
17415 Set_Digits_Value (Implicit_Base, Digits_Value (Base_Typ));
17416 Set_Float_Rep (Implicit_Base, Float_Rep (Base_Typ));
17418 Set_Ekind (T, E_Floating_Point_Subtype);
17419 Set_Etype (T, Implicit_Base);
17420 Set_Size_Info (T, Implicit_Base);
17421 Set_RM_Size (T, RM_Size (Implicit_Base));
17422 Inherit_Rep_Item_Chain (T, Implicit_Base);
17423 Set_Digits_Value (T, Digs_Val);
17424 end Floating_Point_Type_Declaration;
17426 ----------------------------
17427 -- Get_Discriminant_Value --
17428 ----------------------------
17430 -- This is the situation:
17432 -- There is a non-derived type
17434 -- type T0 (Dx, Dy, Dz...)
17436 -- There are zero or more levels of derivation, with each derivation
17437 -- either purely inheriting the discriminants, or defining its own.
17439 -- type Ti is new Ti-1
17440 -- or
17441 -- type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
17442 -- or
17443 -- subtype Ti is ...
17445 -- The subtype issue is avoided by the use of Original_Record_Component,
17446 -- and the fact that derived subtypes also derive the constraints.
17448 -- This chain leads back from
17450 -- Typ_For_Constraint
17452 -- Typ_For_Constraint has discriminants, and the value for each
17453 -- discriminant is given by its corresponding Elmt of Constraints.
17455 -- Discriminant is some discriminant in this hierarchy
17457 -- We need to return its value
17459 -- We do this by recursively searching each level, and looking for
17460 -- Discriminant. Once we get to the bottom, we start backing up
17461 -- returning the value for it which may in turn be a discriminant
17462 -- further up, so on the backup we continue the substitution.
17464 function Get_Discriminant_Value
17465 (Discriminant : Entity_Id;
17466 Typ_For_Constraint : Entity_Id;
17467 Constraint : Elist_Id) return Node_Id
17469 function Root_Corresponding_Discriminant
17470 (Discr : Entity_Id) return Entity_Id;
17471 -- Given a discriminant, traverse the chain of inherited discriminants
17472 -- and return the topmost discriminant.
17474 function Search_Derivation_Levels
17475 (Ti : Entity_Id;
17476 Discrim_Values : Elist_Id;
17477 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
17478 -- This is the routine that performs the recursive search of levels
17479 -- as described above.
17481 -------------------------------------
17482 -- Root_Corresponding_Discriminant --
17483 -------------------------------------
17485 function Root_Corresponding_Discriminant
17486 (Discr : Entity_Id) return Entity_Id
17488 D : Entity_Id;
17490 begin
17491 D := Discr;
17492 while Present (Corresponding_Discriminant (D)) loop
17493 D := Corresponding_Discriminant (D);
17494 end loop;
17496 return D;
17497 end Root_Corresponding_Discriminant;
17499 ------------------------------
17500 -- Search_Derivation_Levels --
17501 ------------------------------
17503 function Search_Derivation_Levels
17504 (Ti : Entity_Id;
17505 Discrim_Values : Elist_Id;
17506 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
17508 Assoc : Elmt_Id;
17509 Disc : Entity_Id;
17510 Result : Node_Or_Entity_Id;
17511 Result_Entity : Node_Id;
17513 begin
17514 -- If inappropriate type, return Error, this happens only in
17515 -- cascaded error situations, and we want to avoid a blow up.
17517 if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
17518 return Error;
17519 end if;
17521 -- Look deeper if possible. Use Stored_Constraints only for
17522 -- untagged types. For tagged types use the given constraint.
17523 -- This asymmetry needs explanation???
17525 if not Stored_Discrim_Values
17526 and then Present (Stored_Constraint (Ti))
17527 and then not Is_Tagged_Type (Ti)
17528 then
17529 Result :=
17530 Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
17531 else
17532 declare
17533 Td : constant Entity_Id := Etype (Ti);
17535 begin
17536 if Td = Ti then
17537 Result := Discriminant;
17539 else
17540 if Present (Stored_Constraint (Ti)) then
17541 Result :=
17542 Search_Derivation_Levels
17543 (Td, Stored_Constraint (Ti), True);
17544 else
17545 Result :=
17546 Search_Derivation_Levels
17547 (Td, Discrim_Values, Stored_Discrim_Values);
17548 end if;
17549 end if;
17550 end;
17551 end if;
17553 -- Extra underlying places to search, if not found above. For
17554 -- concurrent types, the relevant discriminant appears in the
17555 -- corresponding record. For a type derived from a private type
17556 -- without discriminant, the full view inherits the discriminants
17557 -- of the full view of the parent.
17559 if Result = Discriminant then
17560 if Is_Concurrent_Type (Ti)
17561 and then Present (Corresponding_Record_Type (Ti))
17562 then
17563 Result :=
17564 Search_Derivation_Levels (
17565 Corresponding_Record_Type (Ti),
17566 Discrim_Values,
17567 Stored_Discrim_Values);
17569 elsif Is_Private_Type (Ti)
17570 and then not Has_Discriminants (Ti)
17571 and then Present (Full_View (Ti))
17572 and then Etype (Full_View (Ti)) /= Ti
17573 then
17574 Result :=
17575 Search_Derivation_Levels (
17576 Full_View (Ti),
17577 Discrim_Values,
17578 Stored_Discrim_Values);
17579 end if;
17580 end if;
17582 -- If Result is not a (reference to a) discriminant, return it,
17583 -- otherwise set Result_Entity to the discriminant.
17585 if Nkind (Result) = N_Defining_Identifier then
17586 pragma Assert (Result = Discriminant);
17587 Result_Entity := Result;
17589 else
17590 if not Denotes_Discriminant (Result) then
17591 return Result;
17592 end if;
17594 Result_Entity := Entity (Result);
17595 end if;
17597 -- See if this level of derivation actually has discriminants because
17598 -- tagged derivations can add them, hence the lower levels need not
17599 -- have any.
17601 if not Has_Discriminants (Ti) then
17602 return Result;
17603 end if;
17605 -- Scan Ti's discriminants for Result_Entity, and return its
17606 -- corresponding value, if any.
17608 Result_Entity := Original_Record_Component (Result_Entity);
17610 Assoc := First_Elmt (Discrim_Values);
17612 if Stored_Discrim_Values then
17613 Disc := First_Stored_Discriminant (Ti);
17614 else
17615 Disc := First_Discriminant (Ti);
17616 end if;
17618 while Present (Disc) loop
17619 pragma Assert (Present (Assoc));
17621 if Original_Record_Component (Disc) = Result_Entity then
17622 return Node (Assoc);
17623 end if;
17625 Next_Elmt (Assoc);
17627 if Stored_Discrim_Values then
17628 Next_Stored_Discriminant (Disc);
17629 else
17630 Next_Discriminant (Disc);
17631 end if;
17632 end loop;
17634 -- Could not find it
17636 return Result;
17637 end Search_Derivation_Levels;
17639 -- Local Variables
17641 Result : Node_Or_Entity_Id;
17643 -- Start of processing for Get_Discriminant_Value
17645 begin
17646 -- ??? This routine is a gigantic mess and will be deleted. For the
17647 -- time being just test for the trivial case before calling recurse.
17649 if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
17650 declare
17651 D : Entity_Id;
17652 E : Elmt_Id;
17654 begin
17655 D := First_Discriminant (Typ_For_Constraint);
17656 E := First_Elmt (Constraint);
17657 while Present (D) loop
17658 if Chars (D) = Chars (Discriminant) then
17659 return Node (E);
17660 end if;
17662 Next_Discriminant (D);
17663 Next_Elmt (E);
17664 end loop;
17665 end;
17666 end if;
17668 Result := Search_Derivation_Levels
17669 (Typ_For_Constraint, Constraint, False);
17671 -- ??? hack to disappear when this routine is gone
17673 if Nkind (Result) = N_Defining_Identifier then
17674 declare
17675 D : Entity_Id;
17676 E : Elmt_Id;
17678 begin
17679 D := First_Discriminant (Typ_For_Constraint);
17680 E := First_Elmt (Constraint);
17681 while Present (D) loop
17682 if Root_Corresponding_Discriminant (D) = Discriminant then
17683 return Node (E);
17684 end if;
17686 Next_Discriminant (D);
17687 Next_Elmt (E);
17688 end loop;
17689 end;
17690 end if;
17692 pragma Assert (Nkind (Result) /= N_Defining_Identifier);
17693 return Result;
17694 end Get_Discriminant_Value;
17696 --------------------------
17697 -- Has_Range_Constraint --
17698 --------------------------
17700 function Has_Range_Constraint (N : Node_Id) return Boolean is
17701 C : constant Node_Id := Constraint (N);
17703 begin
17704 if Nkind (C) = N_Range_Constraint then
17705 return True;
17707 elsif Nkind (C) = N_Digits_Constraint then
17708 return
17709 Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
17710 or else Present (Range_Constraint (C));
17712 elsif Nkind (C) = N_Delta_Constraint then
17713 return Present (Range_Constraint (C));
17715 else
17716 return False;
17717 end if;
17718 end Has_Range_Constraint;
17720 ------------------------
17721 -- Inherit_Components --
17722 ------------------------
17724 function Inherit_Components
17725 (N : Node_Id;
17726 Parent_Base : Entity_Id;
17727 Derived_Base : Entity_Id;
17728 Is_Tagged : Boolean;
17729 Inherit_Discr : Boolean;
17730 Discs : Elist_Id) return Elist_Id
17732 Assoc_List : constant Elist_Id := New_Elmt_List;
17734 procedure Inherit_Component
17735 (Old_C : Entity_Id;
17736 Plain_Discrim : Boolean := False;
17737 Stored_Discrim : Boolean := False);
17738 -- Inherits component Old_C from Parent_Base to the Derived_Base. If
17739 -- Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
17740 -- True, Old_C is a stored discriminant. If they are both false then
17741 -- Old_C is a regular component.
17743 -----------------------
17744 -- Inherit_Component --
17745 -----------------------
17747 procedure Inherit_Component
17748 (Old_C : Entity_Id;
17749 Plain_Discrim : Boolean := False;
17750 Stored_Discrim : Boolean := False)
17752 procedure Set_Anonymous_Type (Id : Entity_Id);
17753 -- Id denotes the entity of an access discriminant or anonymous
17754 -- access component. Set the type of Id to either the same type of
17755 -- Old_C or create a new one depending on whether the parent and
17756 -- the child types are in the same scope.
17758 ------------------------
17759 -- Set_Anonymous_Type --
17760 ------------------------
17762 procedure Set_Anonymous_Type (Id : Entity_Id) is
17763 Old_Typ : constant Entity_Id := Etype (Old_C);
17765 begin
17766 if Scope (Parent_Base) = Scope (Derived_Base) then
17767 Set_Etype (Id, Old_Typ);
17769 -- The parent and the derived type are in two different scopes.
17770 -- Reuse the type of the original discriminant / component by
17771 -- copying it in order to preserve all attributes.
17773 else
17774 declare
17775 Typ : constant Entity_Id := New_Copy (Old_Typ);
17777 begin
17778 Set_Etype (Id, Typ);
17780 -- Since we do not generate component declarations for
17781 -- inherited components, associate the itype with the
17782 -- derived type.
17784 Set_Associated_Node_For_Itype (Typ, Parent (Derived_Base));
17785 Set_Scope (Typ, Derived_Base);
17786 end;
17787 end if;
17788 end Set_Anonymous_Type;
17790 -- Local variables and constants
17792 New_C : constant Entity_Id := New_Copy (Old_C);
17794 Corr_Discrim : Entity_Id;
17795 Discrim : Entity_Id;
17797 -- Start of processing for Inherit_Component
17799 begin
17800 pragma Assert (not Is_Tagged or not Stored_Discrim);
17802 Set_Parent (New_C, Parent (Old_C));
17804 -- Regular discriminants and components must be inserted in the scope
17805 -- of the Derived_Base. Do it here.
17807 if not Stored_Discrim then
17808 Enter_Name (New_C);
17809 end if;
17811 -- For tagged types the Original_Record_Component must point to
17812 -- whatever this field was pointing to in the parent type. This has
17813 -- already been achieved by the call to New_Copy above.
17815 if not Is_Tagged then
17816 Set_Original_Record_Component (New_C, New_C);
17817 end if;
17819 -- Set the proper type of an access discriminant
17821 if Ekind (New_C) = E_Discriminant
17822 and then Ekind (Etype (New_C)) = E_Anonymous_Access_Type
17823 then
17824 Set_Anonymous_Type (New_C);
17825 end if;
17827 -- If we have inherited a component then see if its Etype contains
17828 -- references to Parent_Base discriminants. In this case, replace
17829 -- these references with the constraints given in Discs. We do not
17830 -- do this for the partial view of private types because this is
17831 -- not needed (only the components of the full view will be used
17832 -- for code generation) and cause problem. We also avoid this
17833 -- transformation in some error situations.
17835 if Ekind (New_C) = E_Component then
17837 -- Set the proper type of an anonymous access component
17839 if Ekind (Etype (New_C)) = E_Anonymous_Access_Type then
17840 Set_Anonymous_Type (New_C);
17842 elsif (Is_Private_Type (Derived_Base)
17843 and then not Is_Generic_Type (Derived_Base))
17844 or else (Is_Empty_Elmt_List (Discs)
17845 and then not Expander_Active)
17846 then
17847 Set_Etype (New_C, Etype (Old_C));
17849 else
17850 -- The current component introduces a circularity of the
17851 -- following kind:
17853 -- limited with Pack_2;
17854 -- package Pack_1 is
17855 -- type T_1 is tagged record
17856 -- Comp : access Pack_2.T_2;
17857 -- ...
17858 -- end record;
17859 -- end Pack_1;
17861 -- with Pack_1;
17862 -- package Pack_2 is
17863 -- type T_2 is new Pack_1.T_1 with ...;
17864 -- end Pack_2;
17866 Set_Etype
17867 (New_C,
17868 Constrain_Component_Type
17869 (Old_C, Derived_Base, N, Parent_Base, Discs));
17870 end if;
17871 end if;
17873 -- In derived tagged types it is illegal to reference a non
17874 -- discriminant component in the parent type. To catch this, mark
17875 -- these components with an Ekind of E_Void. This will be reset in
17876 -- Record_Type_Definition after processing the record extension of
17877 -- the derived type.
17879 -- If the declaration is a private extension, there is no further
17880 -- record extension to process, and the components retain their
17881 -- current kind, because they are visible at this point.
17883 if Is_Tagged and then Ekind (New_C) = E_Component
17884 and then Nkind (N) /= N_Private_Extension_Declaration
17885 then
17886 Set_Ekind (New_C, E_Void);
17887 end if;
17889 if Plain_Discrim then
17890 Set_Corresponding_Discriminant (New_C, Old_C);
17891 Build_Discriminal (New_C);
17893 -- If we are explicitly inheriting a stored discriminant it will be
17894 -- completely hidden.
17896 elsif Stored_Discrim then
17897 Set_Corresponding_Discriminant (New_C, Empty);
17898 Set_Discriminal (New_C, Empty);
17899 Set_Is_Completely_Hidden (New_C);
17901 -- Set the Original_Record_Component of each discriminant in the
17902 -- derived base to point to the corresponding stored that we just
17903 -- created.
17905 Discrim := First_Discriminant (Derived_Base);
17906 while Present (Discrim) loop
17907 Corr_Discrim := Corresponding_Discriminant (Discrim);
17909 -- Corr_Discrim could be missing in an error situation
17911 if Present (Corr_Discrim)
17912 and then Original_Record_Component (Corr_Discrim) = Old_C
17913 then
17914 Set_Original_Record_Component (Discrim, New_C);
17915 end if;
17917 Next_Discriminant (Discrim);
17918 end loop;
17920 Append_Entity (New_C, Derived_Base);
17921 end if;
17923 if not Is_Tagged then
17924 Append_Elmt (Old_C, Assoc_List);
17925 Append_Elmt (New_C, Assoc_List);
17926 end if;
17927 end Inherit_Component;
17929 -- Variables local to Inherit_Component
17931 Loc : constant Source_Ptr := Sloc (N);
17933 Parent_Discrim : Entity_Id;
17934 Stored_Discrim : Entity_Id;
17935 D : Entity_Id;
17936 Component : Entity_Id;
17938 -- Start of processing for Inherit_Components
17940 begin
17941 if not Is_Tagged then
17942 Append_Elmt (Parent_Base, Assoc_List);
17943 Append_Elmt (Derived_Base, Assoc_List);
17944 end if;
17946 -- Inherit parent discriminants if needed
17948 if Inherit_Discr then
17949 Parent_Discrim := First_Discriminant (Parent_Base);
17950 while Present (Parent_Discrim) loop
17951 Inherit_Component (Parent_Discrim, Plain_Discrim => True);
17952 Next_Discriminant (Parent_Discrim);
17953 end loop;
17954 end if;
17956 -- Create explicit stored discrims for untagged types when necessary
17958 if not Has_Unknown_Discriminants (Derived_Base)
17959 and then Has_Discriminants (Parent_Base)
17960 and then not Is_Tagged
17961 and then
17962 (not Inherit_Discr
17963 or else First_Discriminant (Parent_Base) /=
17964 First_Stored_Discriminant (Parent_Base))
17965 then
17966 Stored_Discrim := First_Stored_Discriminant (Parent_Base);
17967 while Present (Stored_Discrim) loop
17968 Inherit_Component (Stored_Discrim, Stored_Discrim => True);
17969 Next_Stored_Discriminant (Stored_Discrim);
17970 end loop;
17971 end if;
17973 -- See if we can apply the second transformation for derived types, as
17974 -- explained in point 6. in the comments above Build_Derived_Record_Type
17975 -- This is achieved by appending Derived_Base discriminants into Discs,
17976 -- which has the side effect of returning a non empty Discs list to the
17977 -- caller of Inherit_Components, which is what we want. This must be
17978 -- done for private derived types if there are explicit stored
17979 -- discriminants, to ensure that we can retrieve the values of the
17980 -- constraints provided in the ancestors.
17982 if Inherit_Discr
17983 and then Is_Empty_Elmt_List (Discs)
17984 and then Present (First_Discriminant (Derived_Base))
17985 and then
17986 (not Is_Private_Type (Derived_Base)
17987 or else Is_Completely_Hidden
17988 (First_Stored_Discriminant (Derived_Base))
17989 or else Is_Generic_Type (Derived_Base))
17990 then
17991 D := First_Discriminant (Derived_Base);
17992 while Present (D) loop
17993 Append_Elmt (New_Occurrence_Of (D, Loc), Discs);
17994 Next_Discriminant (D);
17995 end loop;
17996 end if;
17998 -- Finally, inherit non-discriminant components unless they are not
17999 -- visible because defined or inherited from the full view of the
18000 -- parent. Don't inherit the _parent field of the parent type.
18002 Component := First_Entity (Parent_Base);
18003 while Present (Component) loop
18005 -- Ada 2005 (AI-251): Do not inherit components associated with
18006 -- secondary tags of the parent.
18008 if Ekind (Component) = E_Component
18009 and then Present (Related_Type (Component))
18010 then
18011 null;
18013 elsif Ekind (Component) /= E_Component
18014 or else Chars (Component) = Name_uParent
18015 then
18016 null;
18018 -- If the derived type is within the parent type's declarative
18019 -- region, then the components can still be inherited even though
18020 -- they aren't visible at this point. This can occur for cases
18021 -- such as within public child units where the components must
18022 -- become visible upon entering the child unit's private part.
18024 elsif not Is_Visible_Component (Component)
18025 and then not In_Open_Scopes (Scope (Parent_Base))
18026 then
18027 null;
18029 elsif Ekind_In (Derived_Base, E_Private_Type,
18030 E_Limited_Private_Type)
18031 then
18032 null;
18034 else
18035 Inherit_Component (Component);
18036 end if;
18038 Next_Entity (Component);
18039 end loop;
18041 -- For tagged derived types, inherited discriminants cannot be used in
18042 -- component declarations of the record extension part. To achieve this
18043 -- we mark the inherited discriminants as not visible.
18045 if Is_Tagged and then Inherit_Discr then
18046 D := First_Discriminant (Derived_Base);
18047 while Present (D) loop
18048 Set_Is_Immediately_Visible (D, False);
18049 Next_Discriminant (D);
18050 end loop;
18051 end if;
18053 return Assoc_List;
18054 end Inherit_Components;
18056 -----------------------------
18057 -- Inherit_Predicate_Flags --
18058 -----------------------------
18060 procedure Inherit_Predicate_Flags (Subt, Par : Entity_Id) is
18061 begin
18062 Set_Has_Predicates (Subt, Has_Predicates (Par));
18063 Set_Has_Static_Predicate_Aspect
18064 (Subt, Has_Static_Predicate_Aspect (Par));
18065 Set_Has_Dynamic_Predicate_Aspect
18066 (Subt, Has_Dynamic_Predicate_Aspect (Par));
18067 end Inherit_Predicate_Flags;
18069 ----------------------
18070 -- Is_EVF_Procedure --
18071 ----------------------
18073 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean is
18074 Formal : Entity_Id;
18076 begin
18077 -- Examine the formals of an Extensions_Visible False procedure looking
18078 -- for a controlling OUT parameter.
18080 if Ekind (Subp) = E_Procedure
18081 and then Extensions_Visible_Status (Subp) = Extensions_Visible_False
18082 then
18083 Formal := First_Formal (Subp);
18084 while Present (Formal) loop
18085 if Ekind (Formal) = E_Out_Parameter
18086 and then Is_Controlling_Formal (Formal)
18087 then
18088 return True;
18089 end if;
18091 Next_Formal (Formal);
18092 end loop;
18093 end if;
18095 return False;
18096 end Is_EVF_Procedure;
18098 -----------------------
18099 -- Is_Null_Extension --
18100 -----------------------
18102 function Is_Null_Extension (T : Entity_Id) return Boolean is
18103 Type_Decl : constant Node_Id := Parent (Base_Type (T));
18104 Comp_List : Node_Id;
18105 Comp : Node_Id;
18107 begin
18108 if Nkind (Type_Decl) /= N_Full_Type_Declaration
18109 or else not Is_Tagged_Type (T)
18110 or else Nkind (Type_Definition (Type_Decl)) /=
18111 N_Derived_Type_Definition
18112 or else No (Record_Extension_Part (Type_Definition (Type_Decl)))
18113 then
18114 return False;
18115 end if;
18117 Comp_List :=
18118 Component_List (Record_Extension_Part (Type_Definition (Type_Decl)));
18120 if Present (Discriminant_Specifications (Type_Decl)) then
18121 return False;
18123 elsif Present (Comp_List)
18124 and then Is_Non_Empty_List (Component_Items (Comp_List))
18125 then
18126 Comp := First (Component_Items (Comp_List));
18128 -- Only user-defined components are relevant. The component list
18129 -- may also contain a parent component and internal components
18130 -- corresponding to secondary tags, but these do not determine
18131 -- whether this is a null extension.
18133 while Present (Comp) loop
18134 if Comes_From_Source (Comp) then
18135 return False;
18136 end if;
18138 Next (Comp);
18139 end loop;
18141 return True;
18143 else
18144 return True;
18145 end if;
18146 end Is_Null_Extension;
18148 ------------------------------
18149 -- Is_Valid_Constraint_Kind --
18150 ------------------------------
18152 function Is_Valid_Constraint_Kind
18153 (T_Kind : Type_Kind;
18154 Constraint_Kind : Node_Kind) return Boolean
18156 begin
18157 case T_Kind is
18158 when Enumeration_Kind
18159 | Integer_Kind
18161 return Constraint_Kind = N_Range_Constraint;
18163 when Decimal_Fixed_Point_Kind =>
18164 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
18165 N_Range_Constraint);
18167 when Ordinary_Fixed_Point_Kind =>
18168 return Nkind_In (Constraint_Kind, N_Delta_Constraint,
18169 N_Range_Constraint);
18171 when Float_Kind =>
18172 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
18173 N_Range_Constraint);
18175 when Access_Kind
18176 | Array_Kind
18177 | Class_Wide_Kind
18178 | Concurrent_Kind
18179 | Private_Kind
18180 | E_Incomplete_Type
18181 | E_Record_Subtype
18182 | E_Record_Type
18184 return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
18186 when others =>
18187 return True; -- Error will be detected later
18188 end case;
18189 end Is_Valid_Constraint_Kind;
18191 --------------------------
18192 -- Is_Visible_Component --
18193 --------------------------
18195 function Is_Visible_Component
18196 (C : Entity_Id;
18197 N : Node_Id := Empty) return Boolean
18199 Original_Comp : Entity_Id := Empty;
18200 Original_Type : Entity_Id;
18201 Type_Scope : Entity_Id;
18203 function Is_Local_Type (Typ : Entity_Id) return Boolean;
18204 -- Check whether parent type of inherited component is declared locally,
18205 -- possibly within a nested package or instance. The current scope is
18206 -- the derived record itself.
18208 -------------------
18209 -- Is_Local_Type --
18210 -------------------
18212 function Is_Local_Type (Typ : Entity_Id) return Boolean is
18213 Scop : Entity_Id;
18215 begin
18216 Scop := Scope (Typ);
18217 while Present (Scop)
18218 and then Scop /= Standard_Standard
18219 loop
18220 if Scop = Scope (Current_Scope) then
18221 return True;
18222 end if;
18224 Scop := Scope (Scop);
18225 end loop;
18227 return False;
18228 end Is_Local_Type;
18230 -- Start of processing for Is_Visible_Component
18232 begin
18233 if Ekind_In (C, E_Component, E_Discriminant) then
18234 Original_Comp := Original_Record_Component (C);
18235 end if;
18237 if No (Original_Comp) then
18239 -- Premature usage, or previous error
18241 return False;
18243 else
18244 Original_Type := Scope (Original_Comp);
18245 Type_Scope := Scope (Base_Type (Scope (C)));
18246 end if;
18248 -- This test only concerns tagged types
18250 if not Is_Tagged_Type (Original_Type) then
18251 return True;
18253 -- If it is _Parent or _Tag, there is no visibility issue
18255 elsif not Comes_From_Source (Original_Comp) then
18256 return True;
18258 -- Discriminants are visible unless the (private) type has unknown
18259 -- discriminants. If the discriminant reference is inserted for a
18260 -- discriminant check on a full view it is also visible.
18262 elsif Ekind (Original_Comp) = E_Discriminant
18263 and then
18264 (not Has_Unknown_Discriminants (Original_Type)
18265 or else (Present (N)
18266 and then Nkind (N) = N_Selected_Component
18267 and then Nkind (Prefix (N)) = N_Type_Conversion
18268 and then not Comes_From_Source (Prefix (N))))
18269 then
18270 return True;
18272 -- In the body of an instantiation, check the visibility of a component
18273 -- in case it has a homograph that is a primitive operation of a private
18274 -- type which was not visible in the generic unit.
18276 -- Should Is_Prefixed_Call be propagated from template to instance???
18278 elsif In_Instance_Body then
18279 if not Is_Tagged_Type (Original_Type)
18280 or else not Is_Private_Type (Original_Type)
18281 then
18282 return True;
18284 else
18285 declare
18286 Subp_Elmt : Elmt_Id;
18288 begin
18289 Subp_Elmt := First_Elmt (Primitive_Operations (Original_Type));
18290 while Present (Subp_Elmt) loop
18292 -- The component is hidden by a primitive operation
18294 if Chars (Node (Subp_Elmt)) = Chars (C) then
18295 return False;
18296 end if;
18298 Next_Elmt (Subp_Elmt);
18299 end loop;
18301 return True;
18302 end;
18303 end if;
18305 -- If the component has been declared in an ancestor which is currently
18306 -- a private type, then it is not visible. The same applies if the
18307 -- component's containing type is not in an open scope and the original
18308 -- component's enclosing type is a visible full view of a private type
18309 -- (which can occur in cases where an attempt is being made to reference
18310 -- a component in a sibling package that is inherited from a visible
18311 -- component of a type in an ancestor package; the component in the
18312 -- sibling package should not be visible even though the component it
18313 -- inherited from is visible). This does not apply however in the case
18314 -- where the scope of the type is a private child unit, or when the
18315 -- parent comes from a local package in which the ancestor is currently
18316 -- visible. The latter suppression of visibility is needed for cases
18317 -- that are tested in B730006.
18319 elsif Is_Private_Type (Original_Type)
18320 or else
18321 (not Is_Private_Descendant (Type_Scope)
18322 and then not In_Open_Scopes (Type_Scope)
18323 and then Has_Private_Declaration (Original_Type))
18324 then
18325 -- If the type derives from an entity in a formal package, there
18326 -- are no additional visible components.
18328 if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
18329 N_Formal_Package_Declaration
18330 then
18331 return False;
18333 -- if we are not in the private part of the current package, there
18334 -- are no additional visible components.
18336 elsif Ekind (Scope (Current_Scope)) = E_Package
18337 and then not In_Private_Part (Scope (Current_Scope))
18338 then
18339 return False;
18340 else
18341 return
18342 Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
18343 and then In_Open_Scopes (Scope (Original_Type))
18344 and then Is_Local_Type (Type_Scope);
18345 end if;
18347 -- There is another weird way in which a component may be invisible when
18348 -- the private and the full view are not derived from the same ancestor.
18349 -- Here is an example :
18351 -- type A1 is tagged record F1 : integer; end record;
18352 -- type A2 is new A1 with record F2 : integer; end record;
18353 -- type T is new A1 with private;
18354 -- private
18355 -- type T is new A2 with null record;
18357 -- In this case, the full view of T inherits F1 and F2 but the private
18358 -- view inherits only F1
18360 else
18361 declare
18362 Ancestor : Entity_Id := Scope (C);
18364 begin
18365 loop
18366 if Ancestor = Original_Type then
18367 return True;
18369 -- The ancestor may have a partial view of the original type,
18370 -- but if the full view is in scope, as in a child body, the
18371 -- component is visible.
18373 elsif In_Private_Part (Scope (Original_Type))
18374 and then Full_View (Ancestor) = Original_Type
18375 then
18376 return True;
18378 elsif Ancestor = Etype (Ancestor) then
18380 -- No further ancestors to examine
18382 return False;
18383 end if;
18385 Ancestor := Etype (Ancestor);
18386 end loop;
18387 end;
18388 end if;
18389 end Is_Visible_Component;
18391 --------------------------
18392 -- Make_Class_Wide_Type --
18393 --------------------------
18395 procedure Make_Class_Wide_Type (T : Entity_Id) is
18396 CW_Type : Entity_Id;
18397 CW_Name : Name_Id;
18398 Next_E : Entity_Id;
18400 begin
18401 if Present (Class_Wide_Type (T)) then
18403 -- The class-wide type is a partially decorated entity created for a
18404 -- unanalyzed tagged type referenced through a limited with clause.
18405 -- When the tagged type is analyzed, its class-wide type needs to be
18406 -- redecorated. Note that we reuse the entity created by Decorate_
18407 -- Tagged_Type in order to preserve all links.
18409 if Materialize_Entity (Class_Wide_Type (T)) then
18410 CW_Type := Class_Wide_Type (T);
18411 Set_Materialize_Entity (CW_Type, False);
18413 -- The class wide type can have been defined by the partial view, in
18414 -- which case everything is already done.
18416 else
18417 return;
18418 end if;
18420 -- Default case, we need to create a new class-wide type
18422 else
18423 CW_Type :=
18424 New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
18425 end if;
18427 -- Inherit root type characteristics
18429 CW_Name := Chars (CW_Type);
18430 Next_E := Next_Entity (CW_Type);
18431 Copy_Node (T, CW_Type);
18432 Set_Comes_From_Source (CW_Type, False);
18433 Set_Chars (CW_Type, CW_Name);
18434 Set_Parent (CW_Type, Parent (T));
18435 Set_Next_Entity (CW_Type, Next_E);
18437 -- Ensure we have a new freeze node for the class-wide type. The partial
18438 -- view may have freeze action of its own, requiring a proper freeze
18439 -- node, and the same freeze node cannot be shared between the two
18440 -- types.
18442 Set_Has_Delayed_Freeze (CW_Type);
18443 Set_Freeze_Node (CW_Type, Empty);
18445 -- Customize the class-wide type: It has no prim. op., it cannot be
18446 -- abstract, its Etype points back to the specific root type, and it
18447 -- cannot have any invariants.
18449 Set_Ekind (CW_Type, E_Class_Wide_Type);
18450 Set_Is_Tagged_Type (CW_Type, True);
18451 Set_Direct_Primitive_Operations (CW_Type, New_Elmt_List);
18452 Set_Is_Abstract_Type (CW_Type, False);
18453 Set_Is_Constrained (CW_Type, False);
18454 Set_Is_First_Subtype (CW_Type, Is_First_Subtype (T));
18455 Set_Default_SSO (CW_Type);
18456 Set_Has_Inheritable_Invariants (CW_Type, False);
18457 Set_Has_Inherited_Invariants (CW_Type, False);
18458 Set_Has_Own_Invariants (CW_Type, False);
18460 if Ekind (T) = E_Class_Wide_Subtype then
18461 Set_Etype (CW_Type, Etype (Base_Type (T)));
18462 else
18463 Set_Etype (CW_Type, T);
18464 end if;
18466 Set_No_Tagged_Streams_Pragma (CW_Type, No_Tagged_Streams);
18468 -- If this is the class_wide type of a constrained subtype, it does
18469 -- not have discriminants.
18471 Set_Has_Discriminants (CW_Type,
18472 Has_Discriminants (T) and then not Is_Constrained (T));
18474 Set_Has_Unknown_Discriminants (CW_Type, True);
18475 Set_Class_Wide_Type (T, CW_Type);
18476 Set_Equivalent_Type (CW_Type, Empty);
18478 -- The class-wide type of a class-wide type is itself (RM 3.9(14))
18480 Set_Class_Wide_Type (CW_Type, CW_Type);
18481 end Make_Class_Wide_Type;
18483 ----------------
18484 -- Make_Index --
18485 ----------------
18487 procedure Make_Index
18488 (N : Node_Id;
18489 Related_Nod : Node_Id;
18490 Related_Id : Entity_Id := Empty;
18491 Suffix_Index : Nat := 1;
18492 In_Iter_Schm : Boolean := False)
18494 R : Node_Id;
18495 T : Entity_Id;
18496 Def_Id : Entity_Id := Empty;
18497 Found : Boolean := False;
18499 begin
18500 -- For a discrete range used in a constrained array definition and
18501 -- defined by a range, an implicit conversion to the predefined type
18502 -- INTEGER is assumed if each bound is either a numeric literal, a named
18503 -- number, or an attribute, and the type of both bounds (prior to the
18504 -- implicit conversion) is the type universal_integer. Otherwise, both
18505 -- bounds must be of the same discrete type, other than universal
18506 -- integer; this type must be determinable independently of the
18507 -- context, but using the fact that the type must be discrete and that
18508 -- both bounds must have the same type.
18510 -- Character literals also have a universal type in the absence of
18511 -- of additional context, and are resolved to Standard_Character.
18513 if Nkind (N) = N_Range then
18515 -- The index is given by a range constraint. The bounds are known
18516 -- to be of a consistent type.
18518 if not Is_Overloaded (N) then
18519 T := Etype (N);
18521 -- For universal bounds, choose the specific predefined type
18523 if T = Universal_Integer then
18524 T := Standard_Integer;
18526 elsif T = Any_Character then
18527 Ambiguous_Character (Low_Bound (N));
18529 T := Standard_Character;
18530 end if;
18532 -- The node may be overloaded because some user-defined operators
18533 -- are available, but if a universal interpretation exists it is
18534 -- also the selected one.
18536 elsif Universal_Interpretation (N) = Universal_Integer then
18537 T := Standard_Integer;
18539 else
18540 T := Any_Type;
18542 declare
18543 Ind : Interp_Index;
18544 It : Interp;
18546 begin
18547 Get_First_Interp (N, Ind, It);
18548 while Present (It.Typ) loop
18549 if Is_Discrete_Type (It.Typ) then
18551 if Found
18552 and then not Covers (It.Typ, T)
18553 and then not Covers (T, It.Typ)
18554 then
18555 Error_Msg_N ("ambiguous bounds in discrete range", N);
18556 exit;
18557 else
18558 T := It.Typ;
18559 Found := True;
18560 end if;
18561 end if;
18563 Get_Next_Interp (Ind, It);
18564 end loop;
18566 if T = Any_Type then
18567 Error_Msg_N ("discrete type required for range", N);
18568 Set_Etype (N, Any_Type);
18569 return;
18571 elsif T = Universal_Integer then
18572 T := Standard_Integer;
18573 end if;
18574 end;
18575 end if;
18577 if not Is_Discrete_Type (T) then
18578 Error_Msg_N ("discrete type required for range", N);
18579 Set_Etype (N, Any_Type);
18580 return;
18581 end if;
18583 if Nkind (Low_Bound (N)) = N_Attribute_Reference
18584 and then Attribute_Name (Low_Bound (N)) = Name_First
18585 and then Is_Entity_Name (Prefix (Low_Bound (N)))
18586 and then Is_Type (Entity (Prefix (Low_Bound (N))))
18587 and then Is_Discrete_Type (Entity (Prefix (Low_Bound (N))))
18588 then
18589 -- The type of the index will be the type of the prefix, as long
18590 -- as the upper bound is 'Last of the same type.
18592 Def_Id := Entity (Prefix (Low_Bound (N)));
18594 if Nkind (High_Bound (N)) /= N_Attribute_Reference
18595 or else Attribute_Name (High_Bound (N)) /= Name_Last
18596 or else not Is_Entity_Name (Prefix (High_Bound (N)))
18597 or else Entity (Prefix (High_Bound (N))) /= Def_Id
18598 then
18599 Def_Id := Empty;
18600 end if;
18601 end if;
18603 R := N;
18604 Process_Range_Expr_In_Decl (R, T, In_Iter_Schm => In_Iter_Schm);
18606 elsif Nkind (N) = N_Subtype_Indication then
18608 -- The index is given by a subtype with a range constraint
18610 T := Base_Type (Entity (Subtype_Mark (N)));
18612 if not Is_Discrete_Type (T) then
18613 Error_Msg_N ("discrete type required for range", N);
18614 Set_Etype (N, Any_Type);
18615 return;
18616 end if;
18618 R := Range_Expression (Constraint (N));
18620 Resolve (R, T);
18621 Process_Range_Expr_In_Decl
18622 (R, Entity (Subtype_Mark (N)), In_Iter_Schm => In_Iter_Schm);
18624 elsif Nkind (N) = N_Attribute_Reference then
18626 -- Catch beginner's error (use of attribute other than 'Range)
18628 if Attribute_Name (N) /= Name_Range then
18629 Error_Msg_N ("expect attribute ''Range", N);
18630 Set_Etype (N, Any_Type);
18631 return;
18632 end if;
18634 -- If the node denotes the range of a type mark, that is also the
18635 -- resulting type, and we do not need to create an Itype for it.
18637 if Is_Entity_Name (Prefix (N))
18638 and then Comes_From_Source (N)
18639 and then Is_Type (Entity (Prefix (N)))
18640 and then Is_Discrete_Type (Entity (Prefix (N)))
18641 then
18642 Def_Id := Entity (Prefix (N));
18643 end if;
18645 Analyze_And_Resolve (N);
18646 T := Etype (N);
18647 R := N;
18649 -- If none of the above, must be a subtype. We convert this to a
18650 -- range attribute reference because in the case of declared first
18651 -- named subtypes, the types in the range reference can be different
18652 -- from the type of the entity. A range attribute normalizes the
18653 -- reference and obtains the correct types for the bounds.
18655 -- This transformation is in the nature of an expansion, is only
18656 -- done if expansion is active. In particular, it is not done on
18657 -- formal generic types, because we need to retain the name of the
18658 -- original index for instantiation purposes.
18660 else
18661 if not Is_Entity_Name (N) or else not Is_Type (Entity (N)) then
18662 Error_Msg_N ("invalid subtype mark in discrete range ", N);
18663 Set_Etype (N, Any_Integer);
18664 return;
18666 else
18667 -- The type mark may be that of an incomplete type. It is only
18668 -- now that we can get the full view, previous analysis does
18669 -- not look specifically for a type mark.
18671 Set_Entity (N, Get_Full_View (Entity (N)));
18672 Set_Etype (N, Entity (N));
18673 Def_Id := Entity (N);
18675 if not Is_Discrete_Type (Def_Id) then
18676 Error_Msg_N ("discrete type required for index", N);
18677 Set_Etype (N, Any_Type);
18678 return;
18679 end if;
18680 end if;
18682 if Expander_Active then
18683 Rewrite (N,
18684 Make_Attribute_Reference (Sloc (N),
18685 Attribute_Name => Name_Range,
18686 Prefix => Relocate_Node (N)));
18688 -- The original was a subtype mark that does not freeze. This
18689 -- means that the rewritten version must not freeze either.
18691 Set_Must_Not_Freeze (N);
18692 Set_Must_Not_Freeze (Prefix (N));
18693 Analyze_And_Resolve (N);
18694 T := Etype (N);
18695 R := N;
18697 -- If expander is inactive, type is legal, nothing else to construct
18699 else
18700 return;
18701 end if;
18702 end if;
18704 if not Is_Discrete_Type (T) then
18705 Error_Msg_N ("discrete type required for range", N);
18706 Set_Etype (N, Any_Type);
18707 return;
18709 elsif T = Any_Type then
18710 Set_Etype (N, Any_Type);
18711 return;
18712 end if;
18714 -- We will now create the appropriate Itype to describe the range, but
18715 -- first a check. If we originally had a subtype, then we just label
18716 -- the range with this subtype. Not only is there no need to construct
18717 -- a new subtype, but it is wrong to do so for two reasons:
18719 -- 1. A legality concern, if we have a subtype, it must not freeze,
18720 -- and the Itype would cause freezing incorrectly
18722 -- 2. An efficiency concern, if we created an Itype, it would not be
18723 -- recognized as the same type for the purposes of eliminating
18724 -- checks in some circumstances.
18726 -- We signal this case by setting the subtype entity in Def_Id
18728 if No (Def_Id) then
18729 Def_Id :=
18730 Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
18731 Set_Etype (Def_Id, Base_Type (T));
18733 if Is_Signed_Integer_Type (T) then
18734 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
18736 elsif Is_Modular_Integer_Type (T) then
18737 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
18739 else
18740 Set_Ekind (Def_Id, E_Enumeration_Subtype);
18741 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
18742 Set_First_Literal (Def_Id, First_Literal (T));
18743 end if;
18745 Set_Size_Info (Def_Id, (T));
18746 Set_RM_Size (Def_Id, RM_Size (T));
18747 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
18749 Set_Scalar_Range (Def_Id, R);
18750 Conditional_Delay (Def_Id, T);
18752 if Nkind (N) = N_Subtype_Indication then
18753 Inherit_Predicate_Flags (Def_Id, Entity (Subtype_Mark (N)));
18754 end if;
18756 -- In the subtype indication case, if the immediate parent of the
18757 -- new subtype is non-static, then the subtype we create is non-
18758 -- static, even if its bounds are static.
18760 if Nkind (N) = N_Subtype_Indication
18761 and then not Is_OK_Static_Subtype (Entity (Subtype_Mark (N)))
18762 then
18763 Set_Is_Non_Static_Subtype (Def_Id);
18764 end if;
18765 end if;
18767 -- Final step is to label the index with this constructed type
18769 Set_Etype (N, Def_Id);
18770 end Make_Index;
18772 ------------------------------
18773 -- Modular_Type_Declaration --
18774 ------------------------------
18776 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
18777 Mod_Expr : constant Node_Id := Expression (Def);
18778 M_Val : Uint;
18780 procedure Set_Modular_Size (Bits : Int);
18781 -- Sets RM_Size to Bits, and Esize to normal word size above this
18783 ----------------------
18784 -- Set_Modular_Size --
18785 ----------------------
18787 procedure Set_Modular_Size (Bits : Int) is
18788 begin
18789 Set_RM_Size (T, UI_From_Int (Bits));
18791 if Bits <= 8 then
18792 Init_Esize (T, 8);
18794 elsif Bits <= 16 then
18795 Init_Esize (T, 16);
18797 elsif Bits <= 32 then
18798 Init_Esize (T, 32);
18800 else
18801 Init_Esize (T, System_Max_Binary_Modulus_Power);
18802 end if;
18804 if not Non_Binary_Modulus (T) and then Esize (T) = RM_Size (T) then
18805 Set_Is_Known_Valid (T);
18806 end if;
18807 end Set_Modular_Size;
18809 -- Start of processing for Modular_Type_Declaration
18811 begin
18812 -- If the mod expression is (exactly) 2 * literal, where literal is
18813 -- 64 or less,then almost certainly the * was meant to be **. Warn.
18815 if Warn_On_Suspicious_Modulus_Value
18816 and then Nkind (Mod_Expr) = N_Op_Multiply
18817 and then Nkind (Left_Opnd (Mod_Expr)) = N_Integer_Literal
18818 and then Intval (Left_Opnd (Mod_Expr)) = Uint_2
18819 and then Nkind (Right_Opnd (Mod_Expr)) = N_Integer_Literal
18820 and then Intval (Right_Opnd (Mod_Expr)) <= Uint_64
18821 then
18822 Error_Msg_N
18823 ("suspicious MOD value, was '*'* intended'??M?", Mod_Expr);
18824 end if;
18826 -- Proceed with analysis of mod expression
18828 Analyze_And_Resolve (Mod_Expr, Any_Integer);
18829 Set_Etype (T, T);
18830 Set_Ekind (T, E_Modular_Integer_Type);
18831 Init_Alignment (T);
18832 Set_Is_Constrained (T);
18834 if not Is_OK_Static_Expression (Mod_Expr) then
18835 Flag_Non_Static_Expr
18836 ("non-static expression used for modular type bound!", Mod_Expr);
18837 M_Val := 2 ** System_Max_Binary_Modulus_Power;
18838 else
18839 M_Val := Expr_Value (Mod_Expr);
18840 end if;
18842 if M_Val < 1 then
18843 Error_Msg_N ("modulus value must be positive", Mod_Expr);
18844 M_Val := 2 ** System_Max_Binary_Modulus_Power;
18845 end if;
18847 if M_Val > 2 ** Standard_Long_Integer_Size then
18848 Check_Restriction (No_Long_Long_Integers, Mod_Expr);
18849 end if;
18851 Set_Modulus (T, M_Val);
18853 -- Create bounds for the modular type based on the modulus given in
18854 -- the type declaration and then analyze and resolve those bounds.
18856 Set_Scalar_Range (T,
18857 Make_Range (Sloc (Mod_Expr),
18858 Low_Bound => Make_Integer_Literal (Sloc (Mod_Expr), 0),
18859 High_Bound => Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
18861 -- Properly analyze the literals for the range. We do this manually
18862 -- because we can't go calling Resolve, since we are resolving these
18863 -- bounds with the type, and this type is certainly not complete yet.
18865 Set_Etype (Low_Bound (Scalar_Range (T)), T);
18866 Set_Etype (High_Bound (Scalar_Range (T)), T);
18867 Set_Is_Static_Expression (Low_Bound (Scalar_Range (T)));
18868 Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
18870 -- Loop through powers of two to find number of bits required
18872 for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
18874 -- Binary case
18876 if M_Val = 2 ** Bits then
18877 Set_Modular_Size (Bits);
18878 return;
18880 -- Nonbinary case
18882 elsif M_Val < 2 ** Bits then
18883 Check_SPARK_05_Restriction ("modulus should be a power of 2", T);
18884 Set_Non_Binary_Modulus (T);
18886 if Bits > System_Max_Nonbinary_Modulus_Power then
18887 Error_Msg_Uint_1 :=
18888 UI_From_Int (System_Max_Nonbinary_Modulus_Power);
18889 Error_Msg_F
18890 ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
18891 Set_Modular_Size (System_Max_Binary_Modulus_Power);
18892 return;
18894 else
18895 -- In the nonbinary case, set size as per RM 13.3(55)
18897 Set_Modular_Size (Bits);
18898 return;
18899 end if;
18900 end if;
18902 end loop;
18904 -- If we fall through, then the size exceed System.Max_Binary_Modulus
18905 -- so we just signal an error and set the maximum size.
18907 Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
18908 Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
18910 Set_Modular_Size (System_Max_Binary_Modulus_Power);
18911 Init_Alignment (T);
18913 end Modular_Type_Declaration;
18915 --------------------------
18916 -- New_Concatenation_Op --
18917 --------------------------
18919 procedure New_Concatenation_Op (Typ : Entity_Id) is
18920 Loc : constant Source_Ptr := Sloc (Typ);
18921 Op : Entity_Id;
18923 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
18924 -- Create abbreviated declaration for the formal of a predefined
18925 -- Operator 'Op' of type 'Typ'
18927 --------------------
18928 -- Make_Op_Formal --
18929 --------------------
18931 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
18932 Formal : Entity_Id;
18933 begin
18934 Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
18935 Set_Etype (Formal, Typ);
18936 Set_Mechanism (Formal, Default_Mechanism);
18937 return Formal;
18938 end Make_Op_Formal;
18940 -- Start of processing for New_Concatenation_Op
18942 begin
18943 Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
18945 Set_Ekind (Op, E_Operator);
18946 Set_Scope (Op, Current_Scope);
18947 Set_Etype (Op, Typ);
18948 Set_Homonym (Op, Get_Name_Entity_Id (Name_Op_Concat));
18949 Set_Is_Immediately_Visible (Op);
18950 Set_Is_Intrinsic_Subprogram (Op);
18951 Set_Has_Completion (Op);
18952 Append_Entity (Op, Current_Scope);
18954 Set_Name_Entity_Id (Name_Op_Concat, Op);
18956 Append_Entity (Make_Op_Formal (Typ, Op), Op);
18957 Append_Entity (Make_Op_Formal (Typ, Op), Op);
18958 end New_Concatenation_Op;
18960 -------------------------
18961 -- OK_For_Limited_Init --
18962 -------------------------
18964 -- ???Check all calls of this, and compare the conditions under which it's
18965 -- called.
18967 function OK_For_Limited_Init
18968 (Typ : Entity_Id;
18969 Exp : Node_Id) return Boolean
18971 begin
18972 return Is_CPP_Constructor_Call (Exp)
18973 or else (Ada_Version >= Ada_2005
18974 and then not Debug_Flag_Dot_L
18975 and then OK_For_Limited_Init_In_05 (Typ, Exp));
18976 end OK_For_Limited_Init;
18978 -------------------------------
18979 -- OK_For_Limited_Init_In_05 --
18980 -------------------------------
18982 function OK_For_Limited_Init_In_05
18983 (Typ : Entity_Id;
18984 Exp : Node_Id) return Boolean
18986 begin
18987 -- An object of a limited interface type can be initialized with any
18988 -- expression of a nonlimited descendant type. However this does not
18989 -- apply if this is a view conversion of some other expression. This
18990 -- is checked below.
18992 if Is_Class_Wide_Type (Typ)
18993 and then Is_Limited_Interface (Typ)
18994 and then not Is_Limited_Type (Etype (Exp))
18995 and then Nkind (Exp) /= N_Type_Conversion
18996 then
18997 return True;
18998 end if;
19000 -- Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
19001 -- case of limited aggregates (including extension aggregates), and
19002 -- function calls. The function call may have been given in prefixed
19003 -- notation, in which case the original node is an indexed component.
19004 -- If the function is parameterless, the original node was an explicit
19005 -- dereference. The function may also be parameterless, in which case
19006 -- the source node is just an identifier.
19008 -- A branch of a conditional expression may have been removed if the
19009 -- condition is statically known. This happens during expansion, and
19010 -- thus will not happen if previous errors were encountered. The check
19011 -- will have been performed on the chosen branch, which replaces the
19012 -- original conditional expression.
19014 if No (Exp) then
19015 return True;
19016 end if;
19018 case Nkind (Original_Node (Exp)) is
19019 when N_Aggregate
19020 | N_Extension_Aggregate
19021 | N_Function_Call
19022 | N_Op
19024 return True;
19026 when N_Identifier =>
19027 return Present (Entity (Original_Node (Exp)))
19028 and then Ekind (Entity (Original_Node (Exp))) = E_Function;
19030 when N_Qualified_Expression =>
19031 return
19032 OK_For_Limited_Init_In_05
19033 (Typ, Expression (Original_Node (Exp)));
19035 -- Ada 2005 (AI-251): If a class-wide interface object is initialized
19036 -- with a function call, the expander has rewritten the call into an
19037 -- N_Type_Conversion node to force displacement of the pointer to
19038 -- reference the component containing the secondary dispatch table.
19039 -- Otherwise a type conversion is not a legal context.
19040 -- A return statement for a build-in-place function returning a
19041 -- synchronized type also introduces an unchecked conversion.
19043 when N_Type_Conversion
19044 | N_Unchecked_Type_Conversion
19046 return not Comes_From_Source (Exp)
19047 and then
19048 OK_For_Limited_Init_In_05
19049 (Typ, Expression (Original_Node (Exp)));
19051 when N_Explicit_Dereference
19052 | N_Indexed_Component
19053 | N_Selected_Component
19055 return Nkind (Exp) = N_Function_Call;
19057 -- A use of 'Input is a function call, hence allowed. Normally the
19058 -- attribute will be changed to a call, but the attribute by itself
19059 -- can occur with -gnatc.
19061 when N_Attribute_Reference =>
19062 return Attribute_Name (Original_Node (Exp)) = Name_Input;
19064 -- For a case expression, all dependent expressions must be legal
19066 when N_Case_Expression =>
19067 declare
19068 Alt : Node_Id;
19070 begin
19071 Alt := First (Alternatives (Original_Node (Exp)));
19072 while Present (Alt) loop
19073 if not OK_For_Limited_Init_In_05 (Typ, Expression (Alt)) then
19074 return False;
19075 end if;
19077 Next (Alt);
19078 end loop;
19080 return True;
19081 end;
19083 -- For an if expression, all dependent expressions must be legal
19085 when N_If_Expression =>
19086 declare
19087 Then_Expr : constant Node_Id :=
19088 Next (First (Expressions (Original_Node (Exp))));
19089 Else_Expr : constant Node_Id := Next (Then_Expr);
19090 begin
19091 return OK_For_Limited_Init_In_05 (Typ, Then_Expr)
19092 and then
19093 OK_For_Limited_Init_In_05 (Typ, Else_Expr);
19094 end;
19096 when others =>
19097 return False;
19098 end case;
19099 end OK_For_Limited_Init_In_05;
19101 -------------------------------------------
19102 -- Ordinary_Fixed_Point_Type_Declaration --
19103 -------------------------------------------
19105 procedure Ordinary_Fixed_Point_Type_Declaration
19106 (T : Entity_Id;
19107 Def : Node_Id)
19109 Loc : constant Source_Ptr := Sloc (Def);
19110 Delta_Expr : constant Node_Id := Delta_Expression (Def);
19111 RRS : constant Node_Id := Real_Range_Specification (Def);
19112 Implicit_Base : Entity_Id;
19113 Delta_Val : Ureal;
19114 Small_Val : Ureal;
19115 Low_Val : Ureal;
19116 High_Val : Ureal;
19118 begin
19119 Check_Restriction (No_Fixed_Point, Def);
19121 -- Create implicit base type
19123 Implicit_Base :=
19124 Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
19125 Set_Etype (Implicit_Base, Implicit_Base);
19127 -- Analyze and process delta expression
19129 Analyze_And_Resolve (Delta_Expr, Any_Real);
19131 Check_Delta_Expression (Delta_Expr);
19132 Delta_Val := Expr_Value_R (Delta_Expr);
19134 Set_Delta_Value (Implicit_Base, Delta_Val);
19136 -- Compute default small from given delta, which is the largest power
19137 -- of two that does not exceed the given delta value.
19139 declare
19140 Tmp : Ureal;
19141 Scale : Int;
19143 begin
19144 Tmp := Ureal_1;
19145 Scale := 0;
19147 if Delta_Val < Ureal_1 then
19148 while Delta_Val < Tmp loop
19149 Tmp := Tmp / Ureal_2;
19150 Scale := Scale + 1;
19151 end loop;
19153 else
19154 loop
19155 Tmp := Tmp * Ureal_2;
19156 exit when Tmp > Delta_Val;
19157 Scale := Scale - 1;
19158 end loop;
19159 end if;
19161 Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
19162 end;
19164 Set_Small_Value (Implicit_Base, Small_Val);
19166 -- If no range was given, set a dummy range
19168 if RRS <= Empty_Or_Error then
19169 Low_Val := -Small_Val;
19170 High_Val := Small_Val;
19172 -- Otherwise analyze and process given range
19174 else
19175 declare
19176 Low : constant Node_Id := Low_Bound (RRS);
19177 High : constant Node_Id := High_Bound (RRS);
19179 begin
19180 Analyze_And_Resolve (Low, Any_Real);
19181 Analyze_And_Resolve (High, Any_Real);
19182 Check_Real_Bound (Low);
19183 Check_Real_Bound (High);
19185 -- Obtain and set the range
19187 Low_Val := Expr_Value_R (Low);
19188 High_Val := Expr_Value_R (High);
19190 if Low_Val > High_Val then
19191 Error_Msg_NE ("??fixed point type& has null range", Def, T);
19192 end if;
19193 end;
19194 end if;
19196 -- The range for both the implicit base and the declared first subtype
19197 -- cannot be set yet, so we use the special routine Set_Fixed_Range to
19198 -- set a temporary range in place. Note that the bounds of the base
19199 -- type will be widened to be symmetrical and to fill the available
19200 -- bits when the type is frozen.
19202 -- We could do this with all discrete types, and probably should, but
19203 -- we absolutely have to do it for fixed-point, since the end-points
19204 -- of the range and the size are determined by the small value, which
19205 -- could be reset before the freeze point.
19207 Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
19208 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
19210 -- Complete definition of first subtype. The inheritance of the rep item
19211 -- chain ensures that SPARK-related pragmas are not clobbered when the
19212 -- ordinary fixed point type acts as a full view of a private type.
19214 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
19215 Set_Etype (T, Implicit_Base);
19216 Init_Size_Align (T);
19217 Inherit_Rep_Item_Chain (T, Implicit_Base);
19218 Set_Small_Value (T, Small_Val);
19219 Set_Delta_Value (T, Delta_Val);
19220 Set_Is_Constrained (T);
19221 end Ordinary_Fixed_Point_Type_Declaration;
19223 ----------------------------------
19224 -- Preanalyze_Assert_Expression --
19225 ----------------------------------
19227 procedure Preanalyze_Assert_Expression (N : Node_Id; T : Entity_Id) is
19228 begin
19229 In_Assertion_Expr := In_Assertion_Expr + 1;
19230 Preanalyze_Spec_Expression (N, T);
19231 In_Assertion_Expr := In_Assertion_Expr - 1;
19232 end Preanalyze_Assert_Expression;
19234 -----------------------------------
19235 -- Preanalyze_Default_Expression --
19236 -----------------------------------
19238 procedure Preanalyze_Default_Expression (N : Node_Id; T : Entity_Id) is
19239 Save_In_Default_Expr : constant Boolean := In_Default_Expr;
19240 begin
19241 In_Default_Expr := True;
19242 Preanalyze_Spec_Expression (N, T);
19243 In_Default_Expr := Save_In_Default_Expr;
19244 end Preanalyze_Default_Expression;
19246 --------------------------------
19247 -- Preanalyze_Spec_Expression --
19248 --------------------------------
19250 procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
19251 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
19252 begin
19253 In_Spec_Expression := True;
19254 Preanalyze_And_Resolve (N, T);
19255 In_Spec_Expression := Save_In_Spec_Expression;
19256 end Preanalyze_Spec_Expression;
19258 ----------------------------------------
19259 -- Prepare_Private_Subtype_Completion --
19260 ----------------------------------------
19262 procedure Prepare_Private_Subtype_Completion
19263 (Id : Entity_Id;
19264 Related_Nod : Node_Id)
19266 Id_B : constant Entity_Id := Base_Type (Id);
19267 Full_B : Entity_Id := Full_View (Id_B);
19268 Full : Entity_Id;
19270 begin
19271 if Present (Full_B) then
19273 -- Get to the underlying full view if necessary
19275 if Is_Private_Type (Full_B)
19276 and then Present (Underlying_Full_View (Full_B))
19277 then
19278 Full_B := Underlying_Full_View (Full_B);
19279 end if;
19281 -- The Base_Type is already completed, we can complete the subtype
19282 -- now. We have to create a new entity with the same name, Thus we
19283 -- can't use Create_Itype.
19285 Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
19286 Set_Is_Itype (Full);
19287 Set_Associated_Node_For_Itype (Full, Related_Nod);
19288 Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
19289 end if;
19291 -- The parent subtype may be private, but the base might not, in some
19292 -- nested instances. In that case, the subtype does not need to be
19293 -- exchanged. It would still be nice to make private subtypes and their
19294 -- bases consistent at all times ???
19296 if Is_Private_Type (Id_B) then
19297 Append_Elmt (Id, Private_Dependents (Id_B));
19298 end if;
19299 end Prepare_Private_Subtype_Completion;
19301 ---------------------------
19302 -- Process_Discriminants --
19303 ---------------------------
19305 procedure Process_Discriminants
19306 (N : Node_Id;
19307 Prev : Entity_Id := Empty)
19309 Elist : constant Elist_Id := New_Elmt_List;
19310 Id : Node_Id;
19311 Discr : Node_Id;
19312 Discr_Number : Uint;
19313 Discr_Type : Entity_Id;
19314 Default_Present : Boolean := False;
19315 Default_Not_Present : Boolean := False;
19317 begin
19318 -- A composite type other than an array type can have discriminants.
19319 -- On entry, the current scope is the composite type.
19321 -- The discriminants are initially entered into the scope of the type
19322 -- via Enter_Name with the default Ekind of E_Void to prevent premature
19323 -- use, as explained at the end of this procedure.
19325 Discr := First (Discriminant_Specifications (N));
19326 while Present (Discr) loop
19327 Enter_Name (Defining_Identifier (Discr));
19329 -- For navigation purposes we add a reference to the discriminant
19330 -- in the entity for the type. If the current declaration is a
19331 -- completion, place references on the partial view. Otherwise the
19332 -- type is the current scope.
19334 if Present (Prev) then
19336 -- The references go on the partial view, if present. If the
19337 -- partial view has discriminants, the references have been
19338 -- generated already.
19340 if not Has_Discriminants (Prev) then
19341 Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
19342 end if;
19343 else
19344 Generate_Reference
19345 (Current_Scope, Defining_Identifier (Discr), 'd');
19346 end if;
19348 if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
19349 Discr_Type := Access_Definition (Discr, Discriminant_Type (Discr));
19351 -- Ada 2005 (AI-254)
19353 if Present (Access_To_Subprogram_Definition
19354 (Discriminant_Type (Discr)))
19355 and then Protected_Present (Access_To_Subprogram_Definition
19356 (Discriminant_Type (Discr)))
19357 then
19358 Discr_Type :=
19359 Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
19360 end if;
19362 else
19363 Find_Type (Discriminant_Type (Discr));
19364 Discr_Type := Etype (Discriminant_Type (Discr));
19366 if Error_Posted (Discriminant_Type (Discr)) then
19367 Discr_Type := Any_Type;
19368 end if;
19369 end if;
19371 -- Handling of discriminants that are access types
19373 if Is_Access_Type (Discr_Type) then
19375 -- Ada 2005 (AI-230): Access discriminant allowed in non-
19376 -- limited record types
19378 if Ada_Version < Ada_2005 then
19379 Check_Access_Discriminant_Requires_Limited
19380 (Discr, Discriminant_Type (Discr));
19381 end if;
19383 if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
19384 Error_Msg_N
19385 ("(Ada 83) access discriminant not allowed", Discr);
19386 end if;
19388 -- If not access type, must be a discrete type
19390 elsif not Is_Discrete_Type (Discr_Type) then
19391 Error_Msg_N
19392 ("discriminants must have a discrete or access type",
19393 Discriminant_Type (Discr));
19394 end if;
19396 Set_Etype (Defining_Identifier (Discr), Discr_Type);
19398 -- If a discriminant specification includes the assignment compound
19399 -- delimiter followed by an expression, the expression is the default
19400 -- expression of the discriminant; the default expression must be of
19401 -- the type of the discriminant. (RM 3.7.1) Since this expression is
19402 -- a default expression, we do the special preanalysis, since this
19403 -- expression does not freeze (see section "Handling of Default and
19404 -- Per-Object Expressions" in spec of package Sem).
19406 if Present (Expression (Discr)) then
19407 Preanalyze_Spec_Expression (Expression (Discr), Discr_Type);
19409 -- Legaity checks
19411 if Nkind (N) = N_Formal_Type_Declaration then
19412 Error_Msg_N
19413 ("discriminant defaults not allowed for formal type",
19414 Expression (Discr));
19416 -- Flag an error for a tagged type with defaulted discriminants,
19417 -- excluding limited tagged types when compiling for Ada 2012
19418 -- (see AI05-0214).
19420 elsif Is_Tagged_Type (Current_Scope)
19421 and then (not Is_Limited_Type (Current_Scope)
19422 or else Ada_Version < Ada_2012)
19423 and then Comes_From_Source (N)
19424 then
19425 -- Note: see similar test in Check_Or_Process_Discriminants, to
19426 -- handle the (illegal) case of the completion of an untagged
19427 -- view with discriminants with defaults by a tagged full view.
19428 -- We skip the check if Discr does not come from source, to
19429 -- account for the case of an untagged derived type providing
19430 -- defaults for a renamed discriminant from a private untagged
19431 -- ancestor with a tagged full view (ACATS B460006).
19433 if Ada_Version >= Ada_2012 then
19434 Error_Msg_N
19435 ("discriminants of nonlimited tagged type cannot have"
19436 & " defaults",
19437 Expression (Discr));
19438 else
19439 Error_Msg_N
19440 ("discriminants of tagged type cannot have defaults",
19441 Expression (Discr));
19442 end if;
19444 else
19445 Default_Present := True;
19446 Append_Elmt (Expression (Discr), Elist);
19448 -- Tag the defining identifiers for the discriminants with
19449 -- their corresponding default expressions from the tree.
19451 Set_Discriminant_Default_Value
19452 (Defining_Identifier (Discr), Expression (Discr));
19453 end if;
19455 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag
19456 -- gets set unless we can be sure that no range check is required.
19458 if (GNATprove_Mode or not Expander_Active)
19459 and then not
19460 Is_In_Range
19461 (Expression (Discr), Discr_Type, Assume_Valid => True)
19462 then
19463 Set_Do_Range_Check (Expression (Discr));
19464 end if;
19466 -- No default discriminant value given
19468 else
19469 Default_Not_Present := True;
19470 end if;
19472 -- Ada 2005 (AI-231): Create an Itype that is a duplicate of
19473 -- Discr_Type but with the null-exclusion attribute
19475 if Ada_Version >= Ada_2005 then
19477 -- Ada 2005 (AI-231): Static checks
19479 if Can_Never_Be_Null (Discr_Type) then
19480 Null_Exclusion_Static_Checks (Discr);
19482 elsif Is_Access_Type (Discr_Type)
19483 and then Null_Exclusion_Present (Discr)
19485 -- No need to check itypes because in their case this check
19486 -- was done at their point of creation
19488 and then not Is_Itype (Discr_Type)
19489 then
19490 if Can_Never_Be_Null (Discr_Type) then
19491 Error_Msg_NE
19492 ("`NOT NULL` not allowed (& already excludes null)",
19493 Discr,
19494 Discr_Type);
19495 end if;
19497 Set_Etype (Defining_Identifier (Discr),
19498 Create_Null_Excluding_Itype
19499 (T => Discr_Type,
19500 Related_Nod => Discr));
19502 -- Check for improper null exclusion if the type is otherwise
19503 -- legal for a discriminant.
19505 elsif Null_Exclusion_Present (Discr)
19506 and then Is_Discrete_Type (Discr_Type)
19507 then
19508 Error_Msg_N
19509 ("null exclusion can only apply to an access type", Discr);
19510 end if;
19512 -- Ada 2005 (AI-402): access discriminants of nonlimited types
19513 -- can't have defaults. Synchronized types, or types that are
19514 -- explicitly limited are fine, but special tests apply to derived
19515 -- types in generics: in a generic body we have to assume the
19516 -- worst, and therefore defaults are not allowed if the parent is
19517 -- a generic formal private type (see ACATS B370001).
19519 if Is_Access_Type (Discr_Type) and then Default_Present then
19520 if Ekind (Discr_Type) /= E_Anonymous_Access_Type
19521 or else Is_Limited_Record (Current_Scope)
19522 or else Is_Concurrent_Type (Current_Scope)
19523 or else Is_Concurrent_Record_Type (Current_Scope)
19524 or else Ekind (Current_Scope) = E_Limited_Private_Type
19525 then
19526 if not Is_Derived_Type (Current_Scope)
19527 or else not Is_Generic_Type (Etype (Current_Scope))
19528 or else not In_Package_Body (Scope (Etype (Current_Scope)))
19529 or else Limited_Present
19530 (Type_Definition (Parent (Current_Scope)))
19531 then
19532 null;
19534 else
19535 Error_Msg_N
19536 ("access discriminants of nonlimited types cannot "
19537 & "have defaults", Expression (Discr));
19538 end if;
19540 elsif Present (Expression (Discr)) then
19541 Error_Msg_N
19542 ("(Ada 2005) access discriminants of nonlimited types "
19543 & "cannot have defaults", Expression (Discr));
19544 end if;
19545 end if;
19546 end if;
19548 -- A discriminant cannot be effectively volatile (SPARK RM 7.1.3(6)).
19549 -- This check is relevant only when SPARK_Mode is on as it is not a
19550 -- standard Ada legality rule.
19552 if SPARK_Mode = On
19553 and then Is_Effectively_Volatile (Defining_Identifier (Discr))
19554 then
19555 Error_Msg_N ("discriminant cannot be volatile", Discr);
19556 end if;
19558 Next (Discr);
19559 end loop;
19561 -- An element list consisting of the default expressions of the
19562 -- discriminants is constructed in the above loop and used to set
19563 -- the Discriminant_Constraint attribute for the type. If an object
19564 -- is declared of this (record or task) type without any explicit
19565 -- discriminant constraint given, this element list will form the
19566 -- actual parameters for the corresponding initialization procedure
19567 -- for the type.
19569 Set_Discriminant_Constraint (Current_Scope, Elist);
19570 Set_Stored_Constraint (Current_Scope, No_Elist);
19572 -- Default expressions must be provided either for all or for none
19573 -- of the discriminants of a discriminant part. (RM 3.7.1)
19575 if Default_Present and then Default_Not_Present then
19576 Error_Msg_N
19577 ("incomplete specification of defaults for discriminants", N);
19578 end if;
19580 -- The use of the name of a discriminant is not allowed in default
19581 -- expressions of a discriminant part if the specification of the
19582 -- discriminant is itself given in the discriminant part. (RM 3.7.1)
19584 -- To detect this, the discriminant names are entered initially with an
19585 -- Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
19586 -- attempt to use a void entity (for example in an expression that is
19587 -- type-checked) produces the error message: premature usage. Now after
19588 -- completing the semantic analysis of the discriminant part, we can set
19589 -- the Ekind of all the discriminants appropriately.
19591 Discr := First (Discriminant_Specifications (N));
19592 Discr_Number := Uint_1;
19593 while Present (Discr) loop
19594 Id := Defining_Identifier (Discr);
19595 Set_Ekind (Id, E_Discriminant);
19596 Init_Component_Location (Id);
19597 Init_Esize (Id);
19598 Set_Discriminant_Number (Id, Discr_Number);
19600 -- Make sure this is always set, even in illegal programs
19602 Set_Corresponding_Discriminant (Id, Empty);
19604 -- Initialize the Original_Record_Component to the entity itself.
19605 -- Inherit_Components will propagate the right value to
19606 -- discriminants in derived record types.
19608 Set_Original_Record_Component (Id, Id);
19610 -- Create the discriminal for the discriminant
19612 Build_Discriminal (Id);
19614 Next (Discr);
19615 Discr_Number := Discr_Number + 1;
19616 end loop;
19618 Set_Has_Discriminants (Current_Scope);
19619 end Process_Discriminants;
19621 -----------------------
19622 -- Process_Full_View --
19623 -----------------------
19625 -- WARNING: This routine manages Ghost regions. Return statements must be
19626 -- replaced by gotos which jump to the end of the routine and restore the
19627 -- Ghost mode.
19629 procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
19630 procedure Collect_Implemented_Interfaces
19631 (Typ : Entity_Id;
19632 Ifaces : Elist_Id);
19633 -- Ada 2005: Gather all the interfaces that Typ directly or
19634 -- inherently implements. Duplicate entries are not added to
19635 -- the list Ifaces.
19637 ------------------------------------
19638 -- Collect_Implemented_Interfaces --
19639 ------------------------------------
19641 procedure Collect_Implemented_Interfaces
19642 (Typ : Entity_Id;
19643 Ifaces : Elist_Id)
19645 Iface : Entity_Id;
19646 Iface_Elmt : Elmt_Id;
19648 begin
19649 -- Abstract interfaces are only associated with tagged record types
19651 if not Is_Tagged_Type (Typ) or else not Is_Record_Type (Typ) then
19652 return;
19653 end if;
19655 -- Recursively climb to the ancestors
19657 if Etype (Typ) /= Typ
19659 -- Protect the frontend against wrong cyclic declarations like:
19661 -- type B is new A with private;
19662 -- type C is new A with private;
19663 -- private
19664 -- type B is new C with null record;
19665 -- type C is new B with null record;
19667 and then Etype (Typ) /= Priv_T
19668 and then Etype (Typ) /= Full_T
19669 then
19670 -- Keep separate the management of private type declarations
19672 if Ekind (Typ) = E_Record_Type_With_Private then
19674 -- Handle the following illegal usage:
19675 -- type Private_Type is tagged private;
19676 -- private
19677 -- type Private_Type is new Type_Implementing_Iface;
19679 if Present (Full_View (Typ))
19680 and then Etype (Typ) /= Full_View (Typ)
19681 then
19682 if Is_Interface (Etype (Typ)) then
19683 Append_Unique_Elmt (Etype (Typ), Ifaces);
19684 end if;
19686 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
19687 end if;
19689 -- Non-private types
19691 else
19692 if Is_Interface (Etype (Typ)) then
19693 Append_Unique_Elmt (Etype (Typ), Ifaces);
19694 end if;
19696 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
19697 end if;
19698 end if;
19700 -- Handle entities in the list of abstract interfaces
19702 if Present (Interfaces (Typ)) then
19703 Iface_Elmt := First_Elmt (Interfaces (Typ));
19704 while Present (Iface_Elmt) loop
19705 Iface := Node (Iface_Elmt);
19707 pragma Assert (Is_Interface (Iface));
19709 if not Contain_Interface (Iface, Ifaces) then
19710 Append_Elmt (Iface, Ifaces);
19711 Collect_Implemented_Interfaces (Iface, Ifaces);
19712 end if;
19714 Next_Elmt (Iface_Elmt);
19715 end loop;
19716 end if;
19717 end Collect_Implemented_Interfaces;
19719 -- Local variables
19721 Full_Indic : Node_Id;
19722 Full_Parent : Entity_Id;
19723 Mode : Ghost_Mode_Type;
19724 Priv_Parent : Entity_Id;
19726 -- Start of processing for Process_Full_View
19728 begin
19729 Mark_And_Set_Ghost_Completion (N, Priv_T, Mode);
19731 -- First some sanity checks that must be done after semantic
19732 -- decoration of the full view and thus cannot be placed with other
19733 -- similar checks in Find_Type_Name
19735 if not Is_Limited_Type (Priv_T)
19736 and then (Is_Limited_Type (Full_T)
19737 or else Is_Limited_Composite (Full_T))
19738 then
19739 if In_Instance then
19740 null;
19741 else
19742 Error_Msg_N
19743 ("completion of nonlimited type cannot be limited", Full_T);
19744 Explain_Limited_Type (Full_T, Full_T);
19745 end if;
19747 elsif Is_Abstract_Type (Full_T)
19748 and then not Is_Abstract_Type (Priv_T)
19749 then
19750 Error_Msg_N
19751 ("completion of nonabstract type cannot be abstract", Full_T);
19753 elsif Is_Tagged_Type (Priv_T)
19754 and then Is_Limited_Type (Priv_T)
19755 and then not Is_Limited_Type (Full_T)
19756 then
19757 -- If pragma CPP_Class was applied to the private declaration
19758 -- propagate the limitedness to the full-view
19760 if Is_CPP_Class (Priv_T) then
19761 Set_Is_Limited_Record (Full_T);
19763 -- GNAT allow its own definition of Limited_Controlled to disobey
19764 -- this rule in order in ease the implementation. This test is safe
19765 -- because Root_Controlled is defined in a child of System that
19766 -- normal programs are not supposed to use.
19768 elsif Is_RTE (Etype (Full_T), RE_Root_Controlled) then
19769 Set_Is_Limited_Composite (Full_T);
19770 else
19771 Error_Msg_N
19772 ("completion of limited tagged type must be limited", Full_T);
19773 end if;
19775 elsif Is_Generic_Type (Priv_T) then
19776 Error_Msg_N ("generic type cannot have a completion", Full_T);
19777 end if;
19779 -- Check that ancestor interfaces of private and full views are
19780 -- consistent. We omit this check for synchronized types because
19781 -- they are performed on the corresponding record type when frozen.
19783 if Ada_Version >= Ada_2005
19784 and then Is_Tagged_Type (Priv_T)
19785 and then Is_Tagged_Type (Full_T)
19786 and then not Is_Concurrent_Type (Full_T)
19787 then
19788 declare
19789 Iface : Entity_Id;
19790 Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
19791 Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
19793 begin
19794 Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
19795 Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
19797 -- Ada 2005 (AI-251): The partial view shall be a descendant of
19798 -- an interface type if and only if the full type is descendant
19799 -- of the interface type (AARM 7.3 (7.3/2)).
19801 Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
19803 if Present (Iface) then
19804 Error_Msg_NE
19805 ("interface in partial view& not implemented by full type "
19806 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
19807 end if;
19809 Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
19811 if Present (Iface) then
19812 Error_Msg_NE
19813 ("interface & not implemented by partial view "
19814 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
19815 end if;
19816 end;
19817 end if;
19819 if Is_Tagged_Type (Priv_T)
19820 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
19821 and then Is_Derived_Type (Full_T)
19822 then
19823 Priv_Parent := Etype (Priv_T);
19825 -- The full view of a private extension may have been transformed
19826 -- into an unconstrained derived type declaration and a subtype
19827 -- declaration (see build_derived_record_type for details).
19829 if Nkind (N) = N_Subtype_Declaration then
19830 Full_Indic := Subtype_Indication (N);
19831 Full_Parent := Etype (Base_Type (Full_T));
19832 else
19833 Full_Indic := Subtype_Indication (Type_Definition (N));
19834 Full_Parent := Etype (Full_T);
19835 end if;
19837 -- Check that the parent type of the full type is a descendant of
19838 -- the ancestor subtype given in the private extension. If either
19839 -- entity has an Etype equal to Any_Type then we had some previous
19840 -- error situation [7.3(8)].
19842 if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
19843 goto Leave;
19845 -- Ada 2005 (AI-251): Interfaces in the full type can be given in
19846 -- any order. Therefore we don't have to check that its parent must
19847 -- be a descendant of the parent of the private type declaration.
19849 elsif Is_Interface (Priv_Parent)
19850 and then Is_Interface (Full_Parent)
19851 then
19852 null;
19854 -- Ada 2005 (AI-251): If the parent of the private type declaration
19855 -- is an interface there is no need to check that it is an ancestor
19856 -- of the associated full type declaration. The required tests for
19857 -- this case are performed by Build_Derived_Record_Type.
19859 elsif not Is_Interface (Base_Type (Priv_Parent))
19860 and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
19861 then
19862 Error_Msg_N
19863 ("parent of full type must descend from parent of private "
19864 & "extension", Full_Indic);
19866 -- First check a formal restriction, and then proceed with checking
19867 -- Ada rules. Since the formal restriction is not a serious error, we
19868 -- don't prevent further error detection for this check, hence the
19869 -- ELSE.
19871 else
19872 -- In formal mode, when completing a private extension the type
19873 -- named in the private part must be exactly the same as that
19874 -- named in the visible part.
19876 if Priv_Parent /= Full_Parent then
19877 Error_Msg_Name_1 := Chars (Priv_Parent);
19878 Check_SPARK_05_Restriction ("% expected", Full_Indic);
19879 end if;
19881 -- Check the rules of 7.3(10): if the private extension inherits
19882 -- known discriminants, then the full type must also inherit those
19883 -- discriminants from the same (ancestor) type, and the parent
19884 -- subtype of the full type must be constrained if and only if
19885 -- the ancestor subtype of the private extension is constrained.
19887 if No (Discriminant_Specifications (Parent (Priv_T)))
19888 and then not Has_Unknown_Discriminants (Priv_T)
19889 and then Has_Discriminants (Base_Type (Priv_Parent))
19890 then
19891 declare
19892 Priv_Indic : constant Node_Id :=
19893 Subtype_Indication (Parent (Priv_T));
19895 Priv_Constr : constant Boolean :=
19896 Is_Constrained (Priv_Parent)
19897 or else
19898 Nkind (Priv_Indic) = N_Subtype_Indication
19899 or else
19900 Is_Constrained (Entity (Priv_Indic));
19902 Full_Constr : constant Boolean :=
19903 Is_Constrained (Full_Parent)
19904 or else
19905 Nkind (Full_Indic) = N_Subtype_Indication
19906 or else
19907 Is_Constrained (Entity (Full_Indic));
19909 Priv_Discr : Entity_Id;
19910 Full_Discr : Entity_Id;
19912 begin
19913 Priv_Discr := First_Discriminant (Priv_Parent);
19914 Full_Discr := First_Discriminant (Full_Parent);
19915 while Present (Priv_Discr) and then Present (Full_Discr) loop
19916 if Original_Record_Component (Priv_Discr) =
19917 Original_Record_Component (Full_Discr)
19918 or else
19919 Corresponding_Discriminant (Priv_Discr) =
19920 Corresponding_Discriminant (Full_Discr)
19921 then
19922 null;
19923 else
19924 exit;
19925 end if;
19927 Next_Discriminant (Priv_Discr);
19928 Next_Discriminant (Full_Discr);
19929 end loop;
19931 if Present (Priv_Discr) or else Present (Full_Discr) then
19932 Error_Msg_N
19933 ("full view must inherit discriminants of the parent "
19934 & "type used in the private extension", Full_Indic);
19936 elsif Priv_Constr and then not Full_Constr then
19937 Error_Msg_N
19938 ("parent subtype of full type must be constrained",
19939 Full_Indic);
19941 elsif Full_Constr and then not Priv_Constr then
19942 Error_Msg_N
19943 ("parent subtype of full type must be unconstrained",
19944 Full_Indic);
19945 end if;
19946 end;
19948 -- Check the rules of 7.3(12): if a partial view has neither
19949 -- known or unknown discriminants, then the full type
19950 -- declaration shall define a definite subtype.
19952 elsif not Has_Unknown_Discriminants (Priv_T)
19953 and then not Has_Discriminants (Priv_T)
19954 and then not Is_Constrained (Full_T)
19955 then
19956 Error_Msg_N
19957 ("full view must define a constrained type if partial view "
19958 & "has no discriminants", Full_T);
19959 end if;
19961 -- ??????? Do we implement the following properly ?????
19962 -- If the ancestor subtype of a private extension has constrained
19963 -- discriminants, then the parent subtype of the full view shall
19964 -- impose a statically matching constraint on those discriminants
19965 -- [7.3(13)].
19966 end if;
19968 else
19969 -- For untagged types, verify that a type without discriminants is
19970 -- not completed with an unconstrained type. A separate error message
19971 -- is produced if the full type has defaulted discriminants.
19973 if Is_Definite_Subtype (Priv_T)
19974 and then not Is_Definite_Subtype (Full_T)
19975 then
19976 Error_Msg_Sloc := Sloc (Parent (Priv_T));
19977 Error_Msg_NE
19978 ("full view of& not compatible with declaration#",
19979 Full_T, Priv_T);
19981 if not Is_Tagged_Type (Full_T) then
19982 Error_Msg_N
19983 ("\one is constrained, the other unconstrained", Full_T);
19984 end if;
19985 end if;
19986 end if;
19988 -- AI-419: verify that the use of "limited" is consistent
19990 declare
19991 Orig_Decl : constant Node_Id := Original_Node (N);
19993 begin
19994 if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
19995 and then Nkind (Orig_Decl) = N_Full_Type_Declaration
19996 and then Nkind
19997 (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
19998 then
19999 if not Limited_Present (Parent (Priv_T))
20000 and then not Synchronized_Present (Parent (Priv_T))
20001 and then Limited_Present (Type_Definition (Orig_Decl))
20002 then
20003 Error_Msg_N
20004 ("full view of non-limited extension cannot be limited", N);
20006 -- Conversely, if the partial view carries the limited keyword,
20007 -- the full view must as well, even if it may be redundant.
20009 elsif Limited_Present (Parent (Priv_T))
20010 and then not Limited_Present (Type_Definition (Orig_Decl))
20011 then
20012 Error_Msg_N
20013 ("full view of limited extension must be explicitly limited",
20015 end if;
20016 end if;
20017 end;
20019 -- Ada 2005 (AI-443): A synchronized private extension must be
20020 -- completed by a task or protected type.
20022 if Ada_Version >= Ada_2005
20023 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
20024 and then Synchronized_Present (Parent (Priv_T))
20025 and then not Is_Concurrent_Type (Full_T)
20026 then
20027 Error_Msg_N ("full view of synchronized extension must " &
20028 "be synchronized type", N);
20029 end if;
20031 -- Ada 2005 AI-363: if the full view has discriminants with
20032 -- defaults, it is illegal to declare constrained access subtypes
20033 -- whose designated type is the current type. This allows objects
20034 -- of the type that are declared in the heap to be unconstrained.
20036 if not Has_Unknown_Discriminants (Priv_T)
20037 and then not Has_Discriminants (Priv_T)
20038 and then Has_Discriminants (Full_T)
20039 and then
20040 Present (Discriminant_Default_Value (First_Discriminant (Full_T)))
20041 then
20042 Set_Has_Constrained_Partial_View (Full_T);
20043 Set_Has_Constrained_Partial_View (Priv_T);
20044 end if;
20046 -- Create a full declaration for all its subtypes recorded in
20047 -- Private_Dependents and swap them similarly to the base type. These
20048 -- are subtypes that have been define before the full declaration of
20049 -- the private type. We also swap the entry in Private_Dependents list
20050 -- so we can properly restore the private view on exit from the scope.
20052 declare
20053 Priv_Elmt : Elmt_Id;
20054 Priv_Scop : Entity_Id;
20055 Priv : Entity_Id;
20056 Full : Entity_Id;
20058 begin
20059 Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
20060 while Present (Priv_Elmt) loop
20061 Priv := Node (Priv_Elmt);
20062 Priv_Scop := Scope (Priv);
20064 if Ekind_In (Priv, E_Private_Subtype,
20065 E_Limited_Private_Subtype,
20066 E_Record_Subtype_With_Private)
20067 then
20068 Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
20069 Set_Is_Itype (Full);
20070 Set_Parent (Full, Parent (Priv));
20071 Set_Associated_Node_For_Itype (Full, N);
20073 -- Now we need to complete the private subtype, but since the
20074 -- base type has already been swapped, we must also swap the
20075 -- subtypes (and thus, reverse the arguments in the call to
20076 -- Complete_Private_Subtype). Also note that we may need to
20077 -- re-establish the scope of the private subtype.
20079 Copy_And_Swap (Priv, Full);
20081 if not In_Open_Scopes (Priv_Scop) then
20082 Push_Scope (Priv_Scop);
20084 else
20085 -- Reset Priv_Scop to Empty to indicate no scope was pushed
20087 Priv_Scop := Empty;
20088 end if;
20090 Complete_Private_Subtype (Full, Priv, Full_T, N);
20092 if Present (Priv_Scop) then
20093 Pop_Scope;
20094 end if;
20096 Replace_Elmt (Priv_Elmt, Full);
20097 end if;
20099 Next_Elmt (Priv_Elmt);
20100 end loop;
20101 end;
20103 -- If the private view was tagged, copy the new primitive operations
20104 -- from the private view to the full view.
20106 if Is_Tagged_Type (Full_T) then
20107 declare
20108 Disp_Typ : Entity_Id;
20109 Full_List : Elist_Id;
20110 Prim : Entity_Id;
20111 Prim_Elmt : Elmt_Id;
20112 Priv_List : Elist_Id;
20114 function Contains
20115 (E : Entity_Id;
20116 L : Elist_Id) return Boolean;
20117 -- Determine whether list L contains element E
20119 --------------
20120 -- Contains --
20121 --------------
20123 function Contains
20124 (E : Entity_Id;
20125 L : Elist_Id) return Boolean
20127 List_Elmt : Elmt_Id;
20129 begin
20130 List_Elmt := First_Elmt (L);
20131 while Present (List_Elmt) loop
20132 if Node (List_Elmt) = E then
20133 return True;
20134 end if;
20136 Next_Elmt (List_Elmt);
20137 end loop;
20139 return False;
20140 end Contains;
20142 -- Start of processing
20144 begin
20145 if Is_Tagged_Type (Priv_T) then
20146 Priv_List := Primitive_Operations (Priv_T);
20147 Prim_Elmt := First_Elmt (Priv_List);
20149 -- In the case of a concurrent type completing a private tagged
20150 -- type, primitives may have been declared in between the two
20151 -- views. These subprograms need to be wrapped the same way
20152 -- entries and protected procedures are handled because they
20153 -- cannot be directly shared by the two views.
20155 if Is_Concurrent_Type (Full_T) then
20156 declare
20157 Conc_Typ : constant Entity_Id :=
20158 Corresponding_Record_Type (Full_T);
20159 Curr_Nod : Node_Id := Parent (Conc_Typ);
20160 Wrap_Spec : Node_Id;
20162 begin
20163 while Present (Prim_Elmt) loop
20164 Prim := Node (Prim_Elmt);
20166 if Comes_From_Source (Prim)
20167 and then not Is_Abstract_Subprogram (Prim)
20168 then
20169 Wrap_Spec :=
20170 Make_Subprogram_Declaration (Sloc (Prim),
20171 Specification =>
20172 Build_Wrapper_Spec
20173 (Subp_Id => Prim,
20174 Obj_Typ => Conc_Typ,
20175 Formals =>
20176 Parameter_Specifications
20177 (Parent (Prim))));
20179 Insert_After (Curr_Nod, Wrap_Spec);
20180 Curr_Nod := Wrap_Spec;
20182 Analyze (Wrap_Spec);
20184 -- Remove the wrapper from visibility to avoid
20185 -- spurious conflict with the wrapped entity.
20187 Set_Is_Immediately_Visible
20188 (Defining_Entity (Specification (Wrap_Spec)),
20189 False);
20190 end if;
20192 Next_Elmt (Prim_Elmt);
20193 end loop;
20195 goto Leave;
20196 end;
20198 -- For non-concurrent types, transfer explicit primitives, but
20199 -- omit those inherited from the parent of the private view
20200 -- since they will be re-inherited later on.
20202 else
20203 Full_List := Primitive_Operations (Full_T);
20205 while Present (Prim_Elmt) loop
20206 Prim := Node (Prim_Elmt);
20208 if Comes_From_Source (Prim)
20209 and then not Contains (Prim, Full_List)
20210 then
20211 Append_Elmt (Prim, Full_List);
20212 end if;
20214 Next_Elmt (Prim_Elmt);
20215 end loop;
20216 end if;
20218 -- Untagged private view
20220 else
20221 Full_List := Primitive_Operations (Full_T);
20223 -- In this case the partial view is untagged, so here we locate
20224 -- all of the earlier primitives that need to be treated as
20225 -- dispatching (those that appear between the two views). Note
20226 -- that these additional operations must all be new operations
20227 -- (any earlier operations that override inherited operations
20228 -- of the full view will already have been inserted in the
20229 -- primitives list, marked by Check_Operation_From_Private_View
20230 -- as dispatching. Note that implicit "/=" operators are
20231 -- excluded from being added to the primitives list since they
20232 -- shouldn't be treated as dispatching (tagged "/=" is handled
20233 -- specially).
20235 Prim := Next_Entity (Full_T);
20236 while Present (Prim) and then Prim /= Priv_T loop
20237 if Ekind_In (Prim, E_Procedure, E_Function) then
20238 Disp_Typ := Find_Dispatching_Type (Prim);
20240 if Disp_Typ = Full_T
20241 and then (Chars (Prim) /= Name_Op_Ne
20242 or else Comes_From_Source (Prim))
20243 then
20244 Check_Controlling_Formals (Full_T, Prim);
20246 if not Is_Dispatching_Operation (Prim) then
20247 Append_Elmt (Prim, Full_List);
20248 Set_Is_Dispatching_Operation (Prim, True);
20249 Set_DT_Position_Value (Prim, No_Uint);
20250 end if;
20252 elsif Is_Dispatching_Operation (Prim)
20253 and then Disp_Typ /= Full_T
20254 then
20256 -- Verify that it is not otherwise controlled by a
20257 -- formal or a return value of type T.
20259 Check_Controlling_Formals (Disp_Typ, Prim);
20260 end if;
20261 end if;
20263 Next_Entity (Prim);
20264 end loop;
20265 end if;
20267 -- For the tagged case, the two views can share the same primitive
20268 -- operations list and the same class-wide type. Update attributes
20269 -- of the class-wide type which depend on the full declaration.
20271 if Is_Tagged_Type (Priv_T) then
20272 Set_Direct_Primitive_Operations (Priv_T, Full_List);
20273 Set_Class_Wide_Type
20274 (Base_Type (Full_T), Class_Wide_Type (Priv_T));
20276 Propagate_Concurrent_Flags (Class_Wide_Type (Priv_T), Full_T);
20277 end if;
20278 end;
20279 end if;
20281 -- Ada 2005 AI 161: Check preelaborable initialization consistency
20283 if Known_To_Have_Preelab_Init (Priv_T) then
20285 -- Case where there is a pragma Preelaborable_Initialization. We
20286 -- always allow this in predefined units, which is cheating a bit,
20287 -- but it means we don't have to struggle to meet the requirements in
20288 -- the RM for having Preelaborable Initialization. Otherwise we
20289 -- require that the type meets the RM rules. But we can't check that
20290 -- yet, because of the rule about overriding Initialize, so we simply
20291 -- set a flag that will be checked at freeze time.
20293 if not In_Predefined_Unit (Full_T) then
20294 Set_Must_Have_Preelab_Init (Full_T);
20295 end if;
20296 end if;
20298 -- If pragma CPP_Class was applied to the private type declaration,
20299 -- propagate it now to the full type declaration.
20301 if Is_CPP_Class (Priv_T) then
20302 Set_Is_CPP_Class (Full_T);
20303 Set_Convention (Full_T, Convention_CPP);
20305 -- Check that components of imported CPP types do not have default
20306 -- expressions.
20308 Check_CPP_Type_Has_No_Defaults (Full_T);
20309 end if;
20311 -- If the private view has user specified stream attributes, then so has
20312 -- the full view.
20314 -- Why the test, how could these flags be already set in Full_T ???
20316 if Has_Specified_Stream_Read (Priv_T) then
20317 Set_Has_Specified_Stream_Read (Full_T);
20318 end if;
20320 if Has_Specified_Stream_Write (Priv_T) then
20321 Set_Has_Specified_Stream_Write (Full_T);
20322 end if;
20324 if Has_Specified_Stream_Input (Priv_T) then
20325 Set_Has_Specified_Stream_Input (Full_T);
20326 end if;
20328 if Has_Specified_Stream_Output (Priv_T) then
20329 Set_Has_Specified_Stream_Output (Full_T);
20330 end if;
20332 -- Propagate Default_Initial_Condition-related attributes from the
20333 -- partial view to the full view and its base type.
20335 Propagate_DIC_Attributes (Full_T, From_Typ => Priv_T);
20336 Propagate_DIC_Attributes (Base_Type (Full_T), From_Typ => Priv_T);
20338 -- Propagate invariant-related attributes from the partial view to the
20339 -- full view and its base type.
20341 Propagate_Invariant_Attributes (Full_T, From_Typ => Priv_T);
20342 Propagate_Invariant_Attributes (Base_Type (Full_T), From_Typ => Priv_T);
20344 -- AI12-0041: Detect an attempt to inherit a class-wide type invariant
20345 -- in the full view without advertising the inheritance in the partial
20346 -- view. This can only occur when the partial view has no parent type
20347 -- and the full view has an interface as a parent. Any other scenarios
20348 -- are illegal because implemented interfaces must match between the
20349 -- two views.
20351 if Is_Tagged_Type (Priv_T) and then Is_Tagged_Type (Full_T) then
20352 declare
20353 Full_Par : constant Entity_Id := Etype (Full_T);
20354 Priv_Par : constant Entity_Id := Etype (Priv_T);
20356 begin
20357 if not Is_Interface (Priv_Par)
20358 and then Is_Interface (Full_Par)
20359 and then Has_Inheritable_Invariants (Full_Par)
20360 then
20361 Error_Msg_N
20362 ("hidden inheritance of class-wide type invariants not "
20363 & "allowed", N);
20364 end if;
20365 end;
20366 end if;
20368 -- Propagate predicates to full type, and predicate function if already
20369 -- defined. It is not clear that this can actually happen? the partial
20370 -- view cannot be frozen yet, and the predicate function has not been
20371 -- built. Still it is a cheap check and seems safer to make it.
20373 if Has_Predicates (Priv_T) then
20374 Set_Has_Predicates (Full_T);
20376 if Present (Predicate_Function (Priv_T)) then
20377 Set_Predicate_Function (Full_T, Predicate_Function (Priv_T));
20378 end if;
20379 end if;
20381 <<Leave>>
20382 Restore_Ghost_Mode (Mode);
20383 end Process_Full_View;
20385 -----------------------------------
20386 -- Process_Incomplete_Dependents --
20387 -----------------------------------
20389 procedure Process_Incomplete_Dependents
20390 (N : Node_Id;
20391 Full_T : Entity_Id;
20392 Inc_T : Entity_Id)
20394 Inc_Elmt : Elmt_Id;
20395 Priv_Dep : Entity_Id;
20396 New_Subt : Entity_Id;
20398 Disc_Constraint : Elist_Id;
20400 begin
20401 if No (Private_Dependents (Inc_T)) then
20402 return;
20403 end if;
20405 -- Itypes that may be generated by the completion of an incomplete
20406 -- subtype are not used by the back-end and not attached to the tree.
20407 -- They are created only for constraint-checking purposes.
20409 Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
20410 while Present (Inc_Elmt) loop
20411 Priv_Dep := Node (Inc_Elmt);
20413 if Ekind (Priv_Dep) = E_Subprogram_Type then
20415 -- An Access_To_Subprogram type may have a return type or a
20416 -- parameter type that is incomplete. Replace with the full view.
20418 if Etype (Priv_Dep) = Inc_T then
20419 Set_Etype (Priv_Dep, Full_T);
20420 end if;
20422 declare
20423 Formal : Entity_Id;
20425 begin
20426 Formal := First_Formal (Priv_Dep);
20427 while Present (Formal) loop
20428 if Etype (Formal) = Inc_T then
20429 Set_Etype (Formal, Full_T);
20430 end if;
20432 Next_Formal (Formal);
20433 end loop;
20434 end;
20436 elsif Is_Overloadable (Priv_Dep) then
20438 -- If a subprogram in the incomplete dependents list is primitive
20439 -- for a tagged full type then mark it as a dispatching operation,
20440 -- check whether it overrides an inherited subprogram, and check
20441 -- restrictions on its controlling formals. Note that a protected
20442 -- operation is never dispatching: only its wrapper operation
20443 -- (which has convention Ada) is.
20445 if Is_Tagged_Type (Full_T)
20446 and then Is_Primitive (Priv_Dep)
20447 and then Convention (Priv_Dep) /= Convention_Protected
20448 then
20449 Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
20450 Set_Is_Dispatching_Operation (Priv_Dep);
20451 Check_Controlling_Formals (Full_T, Priv_Dep);
20452 end if;
20454 elsif Ekind (Priv_Dep) = E_Subprogram_Body then
20456 -- Can happen during processing of a body before the completion
20457 -- of a TA type. Ignore, because spec is also on dependent list.
20459 return;
20461 -- Ada 2005 (AI-412): Transform a regular incomplete subtype into a
20462 -- corresponding subtype of the full view.
20464 elsif Ekind (Priv_Dep) = E_Incomplete_Subtype then
20465 Set_Subtype_Indication
20466 (Parent (Priv_Dep), New_Occurrence_Of (Full_T, Sloc (Priv_Dep)));
20467 Set_Etype (Priv_Dep, Full_T);
20468 Set_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
20469 Set_Analyzed (Parent (Priv_Dep), False);
20471 -- Reanalyze the declaration, suppressing the call to
20472 -- Enter_Name to avoid duplicate names.
20474 Analyze_Subtype_Declaration
20475 (N => Parent (Priv_Dep),
20476 Skip => True);
20478 -- Dependent is a subtype
20480 else
20481 -- We build a new subtype indication using the full view of the
20482 -- incomplete parent. The discriminant constraints have been
20483 -- elaborated already at the point of the subtype declaration.
20485 New_Subt := Create_Itype (E_Void, N);
20487 if Has_Discriminants (Full_T) then
20488 Disc_Constraint := Discriminant_Constraint (Priv_Dep);
20489 else
20490 Disc_Constraint := No_Elist;
20491 end if;
20493 Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
20494 Set_Full_View (Priv_Dep, New_Subt);
20495 end if;
20497 Next_Elmt (Inc_Elmt);
20498 end loop;
20499 end Process_Incomplete_Dependents;
20501 --------------------------------
20502 -- Process_Range_Expr_In_Decl --
20503 --------------------------------
20505 procedure Process_Range_Expr_In_Decl
20506 (R : Node_Id;
20507 T : Entity_Id;
20508 Subtyp : Entity_Id := Empty;
20509 Check_List : List_Id := Empty_List;
20510 R_Check_Off : Boolean := False;
20511 In_Iter_Schm : Boolean := False)
20513 Lo, Hi : Node_Id;
20514 R_Checks : Check_Result;
20515 Insert_Node : Node_Id;
20516 Def_Id : Entity_Id;
20518 begin
20519 Analyze_And_Resolve (R, Base_Type (T));
20521 if Nkind (R) = N_Range then
20523 -- In SPARK, all ranges should be static, with the exception of the
20524 -- discrete type definition of a loop parameter specification.
20526 if not In_Iter_Schm
20527 and then not Is_OK_Static_Range (R)
20528 then
20529 Check_SPARK_05_Restriction ("range should be static", R);
20530 end if;
20532 Lo := Low_Bound (R);
20533 Hi := High_Bound (R);
20535 -- Validity checks on the range of a quantified expression are
20536 -- delayed until the construct is transformed into a loop.
20538 if Nkind (Parent (R)) = N_Loop_Parameter_Specification
20539 and then Nkind (Parent (Parent (R))) = N_Quantified_Expression
20540 then
20541 null;
20543 -- We need to ensure validity of the bounds here, because if we
20544 -- go ahead and do the expansion, then the expanded code will get
20545 -- analyzed with range checks suppressed and we miss the check.
20547 -- WARNING: The capture of the range bounds with xxx_FIRST/_LAST and
20548 -- the temporaries generated by routine Remove_Side_Effects by means
20549 -- of validity checks must use the same names. When a range appears
20550 -- in the parent of a generic, the range is processed with checks
20551 -- disabled as part of the generic context and with checks enabled
20552 -- for code generation purposes. This leads to link issues as the
20553 -- generic contains references to xxx_FIRST/_LAST, but the inlined
20554 -- template sees the temporaries generated by Remove_Side_Effects.
20556 else
20557 Validity_Check_Range (R, Subtyp);
20558 end if;
20560 -- If there were errors in the declaration, try and patch up some
20561 -- common mistakes in the bounds. The cases handled are literals
20562 -- which are Integer where the expected type is Real and vice versa.
20563 -- These corrections allow the compilation process to proceed further
20564 -- along since some basic assumptions of the format of the bounds
20565 -- are guaranteed.
20567 if Etype (R) = Any_Type then
20568 if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
20569 Rewrite (Lo,
20570 Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
20572 elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
20573 Rewrite (Hi,
20574 Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
20576 elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
20577 Rewrite (Lo,
20578 Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
20580 elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
20581 Rewrite (Hi,
20582 Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
20583 end if;
20585 Set_Etype (Lo, T);
20586 Set_Etype (Hi, T);
20587 end if;
20589 -- If the bounds of the range have been mistakenly given as string
20590 -- literals (perhaps in place of character literals), then an error
20591 -- has already been reported, but we rewrite the string literal as a
20592 -- bound of the range's type to avoid blowups in later processing
20593 -- that looks at static values.
20595 if Nkind (Lo) = N_String_Literal then
20596 Rewrite (Lo,
20597 Make_Attribute_Reference (Sloc (Lo),
20598 Prefix => New_Occurrence_Of (T, Sloc (Lo)),
20599 Attribute_Name => Name_First));
20600 Analyze_And_Resolve (Lo);
20601 end if;
20603 if Nkind (Hi) = N_String_Literal then
20604 Rewrite (Hi,
20605 Make_Attribute_Reference (Sloc (Hi),
20606 Prefix => New_Occurrence_Of (T, Sloc (Hi)),
20607 Attribute_Name => Name_First));
20608 Analyze_And_Resolve (Hi);
20609 end if;
20611 -- If bounds aren't scalar at this point then exit, avoiding
20612 -- problems with further processing of the range in this procedure.
20614 if not Is_Scalar_Type (Etype (Lo)) then
20615 return;
20616 end if;
20618 -- Resolve (actually Sem_Eval) has checked that the bounds are in
20619 -- then range of the base type. Here we check whether the bounds
20620 -- are in the range of the subtype itself. Note that if the bounds
20621 -- represent the null range the Constraint_Error exception should
20622 -- not be raised.
20624 -- ??? The following code should be cleaned up as follows
20626 -- 1. The Is_Null_Range (Lo, Hi) test should disappear since it
20627 -- is done in the call to Range_Check (R, T); below
20629 -- 2. The use of R_Check_Off should be investigated and possibly
20630 -- removed, this would clean up things a bit.
20632 if Is_Null_Range (Lo, Hi) then
20633 null;
20635 else
20636 -- Capture values of bounds and generate temporaries for them
20637 -- if needed, before applying checks, since checks may cause
20638 -- duplication of the expression without forcing evaluation.
20640 -- The forced evaluation removes side effects from expressions,
20641 -- which should occur also in GNATprove mode. Otherwise, we end up
20642 -- with unexpected insertions of actions at places where this is
20643 -- not supposed to occur, e.g. on default parameters of a call.
20645 if Expander_Active or GNATprove_Mode then
20647 -- Call Force_Evaluation to create declarations as needed to
20648 -- deal with side effects, and also create typ_FIRST/LAST
20649 -- entities for bounds if we have a subtype name.
20651 -- Note: we do this transformation even if expansion is not
20652 -- active if we are in GNATprove_Mode since the transformation
20653 -- is in general required to ensure that the resulting tree has
20654 -- proper Ada semantics.
20656 Force_Evaluation
20657 (Lo, Related_Id => Subtyp, Is_Low_Bound => True);
20658 Force_Evaluation
20659 (Hi, Related_Id => Subtyp, Is_High_Bound => True);
20660 end if;
20662 -- We use a flag here instead of suppressing checks on the type
20663 -- because the type we check against isn't necessarily the place
20664 -- where we put the check.
20666 if not R_Check_Off then
20667 R_Checks := Get_Range_Checks (R, T);
20669 -- Look up tree to find an appropriate insertion point. We
20670 -- can't just use insert_actions because later processing
20671 -- depends on the insertion node. Prior to Ada 2012 the
20672 -- insertion point could only be a declaration or a loop, but
20673 -- quantified expressions can appear within any context in an
20674 -- expression, and the insertion point can be any statement,
20675 -- pragma, or declaration.
20677 Insert_Node := Parent (R);
20678 while Present (Insert_Node) loop
20679 exit when
20680 Nkind (Insert_Node) in N_Declaration
20681 and then
20682 not Nkind_In
20683 (Insert_Node, N_Component_Declaration,
20684 N_Loop_Parameter_Specification,
20685 N_Function_Specification,
20686 N_Procedure_Specification);
20688 exit when Nkind (Insert_Node) in N_Later_Decl_Item
20689 or else Nkind (Insert_Node) in
20690 N_Statement_Other_Than_Procedure_Call
20691 or else Nkind_In (Insert_Node, N_Procedure_Call_Statement,
20692 N_Pragma);
20694 Insert_Node := Parent (Insert_Node);
20695 end loop;
20697 -- Why would Type_Decl not be present??? Without this test,
20698 -- short regression tests fail.
20700 if Present (Insert_Node) then
20702 -- Case of loop statement. Verify that the range is part
20703 -- of the subtype indication of the iteration scheme.
20705 if Nkind (Insert_Node) = N_Loop_Statement then
20706 declare
20707 Indic : Node_Id;
20709 begin
20710 Indic := Parent (R);
20711 while Present (Indic)
20712 and then Nkind (Indic) /= N_Subtype_Indication
20713 loop
20714 Indic := Parent (Indic);
20715 end loop;
20717 if Present (Indic) then
20718 Def_Id := Etype (Subtype_Mark (Indic));
20720 Insert_Range_Checks
20721 (R_Checks,
20722 Insert_Node,
20723 Def_Id,
20724 Sloc (Insert_Node),
20726 Do_Before => True);
20727 end if;
20728 end;
20730 -- Insertion before a declaration. If the declaration
20731 -- includes discriminants, the list of applicable checks
20732 -- is given by the caller.
20734 elsif Nkind (Insert_Node) in N_Declaration then
20735 Def_Id := Defining_Identifier (Insert_Node);
20737 if (Ekind (Def_Id) = E_Record_Type
20738 and then Depends_On_Discriminant (R))
20739 or else
20740 (Ekind (Def_Id) = E_Protected_Type
20741 and then Has_Discriminants (Def_Id))
20742 then
20743 Append_Range_Checks
20744 (R_Checks,
20745 Check_List, Def_Id, Sloc (Insert_Node), R);
20747 else
20748 Insert_Range_Checks
20749 (R_Checks,
20750 Insert_Node, Def_Id, Sloc (Insert_Node), R);
20752 end if;
20754 -- Insertion before a statement. Range appears in the
20755 -- context of a quantified expression. Insertion will
20756 -- take place when expression is expanded.
20758 else
20759 null;
20760 end if;
20761 end if;
20762 end if;
20763 end if;
20765 -- Case of other than an explicit N_Range node
20767 -- The forced evaluation removes side effects from expressions, which
20768 -- should occur also in GNATprove mode. Otherwise, we end up with
20769 -- unexpected insertions of actions at places where this is not
20770 -- supposed to occur, e.g. on default parameters of a call.
20772 elsif Expander_Active or GNATprove_Mode then
20773 Get_Index_Bounds (R, Lo, Hi);
20774 Force_Evaluation (Lo);
20775 Force_Evaluation (Hi);
20776 end if;
20777 end Process_Range_Expr_In_Decl;
20779 --------------------------------------
20780 -- Process_Real_Range_Specification --
20781 --------------------------------------
20783 procedure Process_Real_Range_Specification (Def : Node_Id) is
20784 Spec : constant Node_Id := Real_Range_Specification (Def);
20785 Lo : Node_Id;
20786 Hi : Node_Id;
20787 Err : Boolean := False;
20789 procedure Analyze_Bound (N : Node_Id);
20790 -- Analyze and check one bound
20792 -------------------
20793 -- Analyze_Bound --
20794 -------------------
20796 procedure Analyze_Bound (N : Node_Id) is
20797 begin
20798 Analyze_And_Resolve (N, Any_Real);
20800 if not Is_OK_Static_Expression (N) then
20801 Flag_Non_Static_Expr
20802 ("bound in real type definition is not static!", N);
20803 Err := True;
20804 end if;
20805 end Analyze_Bound;
20807 -- Start of processing for Process_Real_Range_Specification
20809 begin
20810 if Present (Spec) then
20811 Lo := Low_Bound (Spec);
20812 Hi := High_Bound (Spec);
20813 Analyze_Bound (Lo);
20814 Analyze_Bound (Hi);
20816 -- If error, clear away junk range specification
20818 if Err then
20819 Set_Real_Range_Specification (Def, Empty);
20820 end if;
20821 end if;
20822 end Process_Real_Range_Specification;
20824 ---------------------
20825 -- Process_Subtype --
20826 ---------------------
20828 function Process_Subtype
20829 (S : Node_Id;
20830 Related_Nod : Node_Id;
20831 Related_Id : Entity_Id := Empty;
20832 Suffix : Character := ' ') return Entity_Id
20834 P : Node_Id;
20835 Def_Id : Entity_Id;
20836 Error_Node : Node_Id;
20837 Full_View_Id : Entity_Id;
20838 Subtype_Mark_Id : Entity_Id;
20840 May_Have_Null_Exclusion : Boolean;
20842 procedure Check_Incomplete (T : Node_Id);
20843 -- Called to verify that an incomplete type is not used prematurely
20845 ----------------------
20846 -- Check_Incomplete --
20847 ----------------------
20849 procedure Check_Incomplete (T : Node_Id) is
20850 begin
20851 -- Ada 2005 (AI-412): Incomplete subtypes are legal
20853 if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
20854 and then
20855 not (Ada_Version >= Ada_2005
20856 and then
20857 (Nkind (Parent (T)) = N_Subtype_Declaration
20858 or else (Nkind (Parent (T)) = N_Subtype_Indication
20859 and then Nkind (Parent (Parent (T))) =
20860 N_Subtype_Declaration)))
20861 then
20862 Error_Msg_N ("invalid use of type before its full declaration", T);
20863 end if;
20864 end Check_Incomplete;
20866 -- Start of processing for Process_Subtype
20868 begin
20869 -- Case of no constraints present
20871 if Nkind (S) /= N_Subtype_Indication then
20872 Find_Type (S);
20873 Check_Incomplete (S);
20874 P := Parent (S);
20876 -- Ada 2005 (AI-231): Static check
20878 if Ada_Version >= Ada_2005
20879 and then Present (P)
20880 and then Null_Exclusion_Present (P)
20881 and then Nkind (P) /= N_Access_To_Object_Definition
20882 and then not Is_Access_Type (Entity (S))
20883 then
20884 Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
20885 end if;
20887 -- The following is ugly, can't we have a range or even a flag???
20889 May_Have_Null_Exclusion :=
20890 Nkind_In (P, N_Access_Definition,
20891 N_Access_Function_Definition,
20892 N_Access_Procedure_Definition,
20893 N_Access_To_Object_Definition,
20894 N_Allocator,
20895 N_Component_Definition)
20896 or else
20897 Nkind_In (P, N_Derived_Type_Definition,
20898 N_Discriminant_Specification,
20899 N_Formal_Object_Declaration,
20900 N_Object_Declaration,
20901 N_Object_Renaming_Declaration,
20902 N_Parameter_Specification,
20903 N_Subtype_Declaration);
20905 -- Create an Itype that is a duplicate of Entity (S) but with the
20906 -- null-exclusion attribute.
20908 if May_Have_Null_Exclusion
20909 and then Is_Access_Type (Entity (S))
20910 and then Null_Exclusion_Present (P)
20912 -- No need to check the case of an access to object definition.
20913 -- It is correct to define double not-null pointers.
20915 -- Example:
20916 -- type Not_Null_Int_Ptr is not null access Integer;
20917 -- type Acc is not null access Not_Null_Int_Ptr;
20919 and then Nkind (P) /= N_Access_To_Object_Definition
20920 then
20921 if Can_Never_Be_Null (Entity (S)) then
20922 case Nkind (Related_Nod) is
20923 when N_Full_Type_Declaration =>
20924 if Nkind (Type_Definition (Related_Nod))
20925 in N_Array_Type_Definition
20926 then
20927 Error_Node :=
20928 Subtype_Indication
20929 (Component_Definition
20930 (Type_Definition (Related_Nod)));
20931 else
20932 Error_Node :=
20933 Subtype_Indication (Type_Definition (Related_Nod));
20934 end if;
20936 when N_Subtype_Declaration =>
20937 Error_Node := Subtype_Indication (Related_Nod);
20939 when N_Object_Declaration =>
20940 Error_Node := Object_Definition (Related_Nod);
20942 when N_Component_Declaration =>
20943 Error_Node :=
20944 Subtype_Indication (Component_Definition (Related_Nod));
20946 when N_Allocator =>
20947 Error_Node := Expression (Related_Nod);
20949 when others =>
20950 pragma Assert (False);
20951 Error_Node := Related_Nod;
20952 end case;
20954 Error_Msg_NE
20955 ("`NOT NULL` not allowed (& already excludes null)",
20956 Error_Node,
20957 Entity (S));
20958 end if;
20960 Set_Etype (S,
20961 Create_Null_Excluding_Itype
20962 (T => Entity (S),
20963 Related_Nod => P));
20964 Set_Entity (S, Etype (S));
20965 end if;
20967 return Entity (S);
20969 -- Case of constraint present, so that we have an N_Subtype_Indication
20970 -- node (this node is created only if constraints are present).
20972 else
20973 Find_Type (Subtype_Mark (S));
20975 if Nkind (Parent (S)) /= N_Access_To_Object_Definition
20976 and then not
20977 (Nkind (Parent (S)) = N_Subtype_Declaration
20978 and then Is_Itype (Defining_Identifier (Parent (S))))
20979 then
20980 Check_Incomplete (Subtype_Mark (S));
20981 end if;
20983 P := Parent (S);
20984 Subtype_Mark_Id := Entity (Subtype_Mark (S));
20986 -- Explicit subtype declaration case
20988 if Nkind (P) = N_Subtype_Declaration then
20989 Def_Id := Defining_Identifier (P);
20991 -- Explicit derived type definition case
20993 elsif Nkind (P) = N_Derived_Type_Definition then
20994 Def_Id := Defining_Identifier (Parent (P));
20996 -- Implicit case, the Def_Id must be created as an implicit type.
20997 -- The one exception arises in the case of concurrent types, array
20998 -- and access types, where other subsidiary implicit types may be
20999 -- created and must appear before the main implicit type. In these
21000 -- cases we leave Def_Id set to Empty as a signal that Create_Itype
21001 -- has not yet been called to create Def_Id.
21003 else
21004 if Is_Array_Type (Subtype_Mark_Id)
21005 or else Is_Concurrent_Type (Subtype_Mark_Id)
21006 or else Is_Access_Type (Subtype_Mark_Id)
21007 then
21008 Def_Id := Empty;
21010 -- For the other cases, we create a new unattached Itype,
21011 -- and set the indication to ensure it gets attached later.
21013 else
21014 Def_Id :=
21015 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
21016 end if;
21017 end if;
21019 -- If the kind of constraint is invalid for this kind of type,
21020 -- then give an error, and then pretend no constraint was given.
21022 if not Is_Valid_Constraint_Kind
21023 (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
21024 then
21025 Error_Msg_N
21026 ("incorrect constraint for this kind of type", Constraint (S));
21028 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
21030 -- Set Ekind of orphan itype, to prevent cascaded errors
21032 if Present (Def_Id) then
21033 Set_Ekind (Def_Id, Ekind (Any_Type));
21034 end if;
21036 -- Make recursive call, having got rid of the bogus constraint
21038 return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
21039 end if;
21041 -- Remaining processing depends on type. Select on Base_Type kind to
21042 -- ensure getting to the concrete type kind in the case of a private
21043 -- subtype (needed when only doing semantic analysis).
21045 case Ekind (Base_Type (Subtype_Mark_Id)) is
21046 when Access_Kind =>
21048 -- If this is a constraint on a class-wide type, discard it.
21049 -- There is currently no way to express a partial discriminant
21050 -- constraint on a type with unknown discriminants. This is
21051 -- a pathology that the ACATS wisely decides not to test.
21053 if Is_Class_Wide_Type (Designated_Type (Subtype_Mark_Id)) then
21054 if Comes_From_Source (S) then
21055 Error_Msg_N
21056 ("constraint on class-wide type ignored??",
21057 Constraint (S));
21058 end if;
21060 if Nkind (P) = N_Subtype_Declaration then
21061 Set_Subtype_Indication (P,
21062 New_Occurrence_Of (Subtype_Mark_Id, Sloc (S)));
21063 end if;
21065 return Subtype_Mark_Id;
21066 end if;
21068 Constrain_Access (Def_Id, S, Related_Nod);
21070 if Expander_Active
21071 and then Is_Itype (Designated_Type (Def_Id))
21072 and then Nkind (Related_Nod) = N_Subtype_Declaration
21073 and then not Is_Incomplete_Type (Designated_Type (Def_Id))
21074 then
21075 Build_Itype_Reference
21076 (Designated_Type (Def_Id), Related_Nod);
21077 end if;
21079 when Array_Kind =>
21080 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
21082 when Decimal_Fixed_Point_Kind =>
21083 Constrain_Decimal (Def_Id, S);
21085 when Enumeration_Kind =>
21086 Constrain_Enumeration (Def_Id, S);
21087 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
21089 when Ordinary_Fixed_Point_Kind =>
21090 Constrain_Ordinary_Fixed (Def_Id, S);
21092 when Float_Kind =>
21093 Constrain_Float (Def_Id, S);
21095 when Integer_Kind =>
21096 Constrain_Integer (Def_Id, S);
21097 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
21099 when Class_Wide_Kind
21100 | E_Incomplete_Type
21101 | E_Record_Subtype
21102 | E_Record_Type
21104 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
21106 if Ekind (Def_Id) = E_Incomplete_Type then
21107 Set_Private_Dependents (Def_Id, New_Elmt_List);
21108 end if;
21110 when Private_Kind =>
21111 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
21113 -- The base type may be private but Def_Id may be a full view
21114 -- in an instance.
21116 if Is_Private_Type (Def_Id) then
21117 Set_Private_Dependents (Def_Id, New_Elmt_List);
21118 end if;
21120 -- In case of an invalid constraint prevent further processing
21121 -- since the type constructed is missing expected fields.
21123 if Etype (Def_Id) = Any_Type then
21124 return Def_Id;
21125 end if;
21127 -- If the full view is that of a task with discriminants,
21128 -- we must constrain both the concurrent type and its
21129 -- corresponding record type. Otherwise we will just propagate
21130 -- the constraint to the full view, if available.
21132 if Present (Full_View (Subtype_Mark_Id))
21133 and then Has_Discriminants (Subtype_Mark_Id)
21134 and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
21135 then
21136 Full_View_Id :=
21137 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
21139 Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
21140 Constrain_Concurrent (Full_View_Id, S,
21141 Related_Nod, Related_Id, Suffix);
21142 Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
21143 Set_Full_View (Def_Id, Full_View_Id);
21145 -- Introduce an explicit reference to the private subtype,
21146 -- to prevent scope anomalies in gigi if first use appears
21147 -- in a nested context, e.g. a later function body.
21148 -- Should this be generated in other contexts than a full
21149 -- type declaration?
21151 if Is_Itype (Def_Id)
21152 and then
21153 Nkind (Parent (P)) = N_Full_Type_Declaration
21154 then
21155 Build_Itype_Reference (Def_Id, Parent (P));
21156 end if;
21158 else
21159 Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
21160 end if;
21162 when Concurrent_Kind =>
21163 Constrain_Concurrent (Def_Id, S,
21164 Related_Nod, Related_Id, Suffix);
21166 when others =>
21167 Error_Msg_N ("invalid subtype mark in subtype indication", S);
21168 end case;
21170 -- Size and Convention are always inherited from the base type
21172 Set_Size_Info (Def_Id, (Subtype_Mark_Id));
21173 Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
21175 return Def_Id;
21176 end if;
21177 end Process_Subtype;
21179 -----------------------------
21180 -- Record_Type_Declaration --
21181 -----------------------------
21183 procedure Record_Type_Declaration
21184 (T : Entity_Id;
21185 N : Node_Id;
21186 Prev : Entity_Id)
21188 Def : constant Node_Id := Type_Definition (N);
21189 Is_Tagged : Boolean;
21190 Tag_Comp : Entity_Id;
21192 begin
21193 -- These flags must be initialized before calling Process_Discriminants
21194 -- because this routine makes use of them.
21196 Set_Ekind (T, E_Record_Type);
21197 Set_Etype (T, T);
21198 Init_Size_Align (T);
21199 Set_Interfaces (T, No_Elist);
21200 Set_Stored_Constraint (T, No_Elist);
21201 Set_Default_SSO (T);
21203 -- Normal case
21205 if Ada_Version < Ada_2005 or else not Interface_Present (Def) then
21206 if Limited_Present (Def) then
21207 Check_SPARK_05_Restriction ("limited is not allowed", N);
21208 end if;
21210 if Abstract_Present (Def) then
21211 Check_SPARK_05_Restriction ("abstract is not allowed", N);
21212 end if;
21214 -- The flag Is_Tagged_Type might have already been set by
21215 -- Find_Type_Name if it detected an error for declaration T. This
21216 -- arises in the case of private tagged types where the full view
21217 -- omits the word tagged.
21219 Is_Tagged :=
21220 Tagged_Present (Def)
21221 or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
21223 Set_Is_Limited_Record (T, Limited_Present (Def));
21225 if Is_Tagged then
21226 Set_Is_Tagged_Type (T, True);
21227 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
21228 end if;
21230 -- Type is abstract if full declaration carries keyword, or if
21231 -- previous partial view did.
21233 Set_Is_Abstract_Type (T, Is_Abstract_Type (T)
21234 or else Abstract_Present (Def));
21236 else
21237 Check_SPARK_05_Restriction ("interface is not allowed", N);
21239 Is_Tagged := True;
21240 Analyze_Interface_Declaration (T, Def);
21242 if Present (Discriminant_Specifications (N)) then
21243 Error_Msg_N
21244 ("interface types cannot have discriminants",
21245 Defining_Identifier
21246 (First (Discriminant_Specifications (N))));
21247 end if;
21248 end if;
21250 -- First pass: if there are self-referential access components,
21251 -- create the required anonymous access type declarations, and if
21252 -- need be an incomplete type declaration for T itself.
21254 Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
21256 if Ada_Version >= Ada_2005
21257 and then Present (Interface_List (Def))
21258 then
21259 Check_Interfaces (N, Def);
21261 declare
21262 Ifaces_List : Elist_Id;
21264 begin
21265 -- Ada 2005 (AI-251): Collect the list of progenitors that are not
21266 -- already in the parents.
21268 Collect_Interfaces
21269 (T => T,
21270 Ifaces_List => Ifaces_List,
21271 Exclude_Parents => True);
21273 Set_Interfaces (T, Ifaces_List);
21274 end;
21275 end if;
21277 -- Records constitute a scope for the component declarations within.
21278 -- The scope is created prior to the processing of these declarations.
21279 -- Discriminants are processed first, so that they are visible when
21280 -- processing the other components. The Ekind of the record type itself
21281 -- is set to E_Record_Type (subtypes appear as E_Record_Subtype).
21283 -- Enter record scope
21285 Push_Scope (T);
21287 -- If an incomplete or private type declaration was already given for
21288 -- the type, then this scope already exists, and the discriminants have
21289 -- been declared within. We must verify that the full declaration
21290 -- matches the incomplete one.
21292 Check_Or_Process_Discriminants (N, T, Prev);
21294 Set_Is_Constrained (T, not Has_Discriminants (T));
21295 Set_Has_Delayed_Freeze (T, True);
21297 -- For tagged types add a manually analyzed component corresponding
21298 -- to the component _tag, the corresponding piece of tree will be
21299 -- expanded as part of the freezing actions if it is not a CPP_Class.
21301 if Is_Tagged then
21303 -- Do not add the tag unless we are in expansion mode
21305 if Expander_Active then
21306 Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
21307 Enter_Name (Tag_Comp);
21309 Set_Ekind (Tag_Comp, E_Component);
21310 Set_Is_Tag (Tag_Comp);
21311 Set_Is_Aliased (Tag_Comp);
21312 Set_Etype (Tag_Comp, RTE (RE_Tag));
21313 Set_DT_Entry_Count (Tag_Comp, No_Uint);
21314 Set_Original_Record_Component (Tag_Comp, Tag_Comp);
21315 Init_Component_Location (Tag_Comp);
21317 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
21318 -- implemented interfaces.
21320 if Has_Interfaces (T) then
21321 Add_Interface_Tag_Components (N, T);
21322 end if;
21323 end if;
21325 Make_Class_Wide_Type (T);
21326 Set_Direct_Primitive_Operations (T, New_Elmt_List);
21327 end if;
21329 -- We must suppress range checks when processing record components in
21330 -- the presence of discriminants, since we don't want spurious checks to
21331 -- be generated during their analysis, but Suppress_Range_Checks flags
21332 -- must be reset the after processing the record definition.
21334 -- Note: this is the only use of Kill_Range_Checks, and is a bit odd,
21335 -- couldn't we just use the normal range check suppression method here.
21336 -- That would seem cleaner ???
21338 if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
21339 Set_Kill_Range_Checks (T, True);
21340 Record_Type_Definition (Def, Prev);
21341 Set_Kill_Range_Checks (T, False);
21342 else
21343 Record_Type_Definition (Def, Prev);
21344 end if;
21346 -- Exit from record scope
21348 End_Scope;
21350 -- Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
21351 -- the implemented interfaces and associate them an aliased entity.
21353 if Is_Tagged
21354 and then not Is_Empty_List (Interface_List (Def))
21355 then
21356 Derive_Progenitor_Subprograms (T, T);
21357 end if;
21359 Check_Function_Writable_Actuals (N);
21360 end Record_Type_Declaration;
21362 ----------------------------
21363 -- Record_Type_Definition --
21364 ----------------------------
21366 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
21367 Component : Entity_Id;
21368 Ctrl_Components : Boolean := False;
21369 Final_Storage_Only : Boolean;
21370 T : Entity_Id;
21372 begin
21373 if Ekind (Prev_T) = E_Incomplete_Type then
21374 T := Full_View (Prev_T);
21375 else
21376 T := Prev_T;
21377 end if;
21379 -- In SPARK, tagged types and type extensions may only be declared in
21380 -- the specification of library unit packages.
21382 if Present (Def) and then Is_Tagged_Type (T) then
21383 declare
21384 Typ : Node_Id;
21385 Ctxt : Node_Id;
21387 begin
21388 if Nkind (Parent (Def)) = N_Full_Type_Declaration then
21389 Typ := Parent (Def);
21390 else
21391 pragma Assert
21392 (Nkind (Parent (Def)) = N_Derived_Type_Definition);
21393 Typ := Parent (Parent (Def));
21394 end if;
21396 Ctxt := Parent (Typ);
21398 if Nkind (Ctxt) = N_Package_Body
21399 and then Nkind (Parent (Ctxt)) = N_Compilation_Unit
21400 then
21401 Check_SPARK_05_Restriction
21402 ("type should be defined in package specification", Typ);
21404 elsif Nkind (Ctxt) /= N_Package_Specification
21405 or else Nkind (Parent (Parent (Ctxt))) /= N_Compilation_Unit
21406 then
21407 Check_SPARK_05_Restriction
21408 ("type should be defined in library unit package", Typ);
21409 end if;
21410 end;
21411 end if;
21413 Final_Storage_Only := not Is_Controlled_Active (T);
21415 -- Ada 2005: Check whether an explicit Limited is present in a derived
21416 -- type declaration.
21418 if Nkind (Parent (Def)) = N_Derived_Type_Definition
21419 and then Limited_Present (Parent (Def))
21420 then
21421 Set_Is_Limited_Record (T);
21422 end if;
21424 -- If the component list of a record type is defined by the reserved
21425 -- word null and there is no discriminant part, then the record type has
21426 -- no components and all records of the type are null records (RM 3.7)
21427 -- This procedure is also called to process the extension part of a
21428 -- record extension, in which case the current scope may have inherited
21429 -- components.
21431 if No (Def)
21432 or else No (Component_List (Def))
21433 or else Null_Present (Component_List (Def))
21434 then
21435 if not Is_Tagged_Type (T) then
21436 Check_SPARK_05_Restriction ("untagged record cannot be null", Def);
21437 end if;
21439 else
21440 Analyze_Declarations (Component_Items (Component_List (Def)));
21442 if Present (Variant_Part (Component_List (Def))) then
21443 Check_SPARK_05_Restriction ("variant part is not allowed", Def);
21444 Analyze (Variant_Part (Component_List (Def)));
21445 end if;
21446 end if;
21448 -- After completing the semantic analysis of the record definition,
21449 -- record components, both new and inherited, are accessible. Set their
21450 -- kind accordingly. Exclude malformed itypes from illegal declarations,
21451 -- whose Ekind may be void.
21453 Component := First_Entity (Current_Scope);
21454 while Present (Component) loop
21455 if Ekind (Component) = E_Void
21456 and then not Is_Itype (Component)
21457 then
21458 Set_Ekind (Component, E_Component);
21459 Init_Component_Location (Component);
21460 end if;
21462 Propagate_Concurrent_Flags (T, Etype (Component));
21464 if Ekind (Component) /= E_Component then
21465 null;
21467 -- Do not set Has_Controlled_Component on a class-wide equivalent
21468 -- type. See Make_CW_Equivalent_Type.
21470 elsif not Is_Class_Wide_Equivalent_Type (T)
21471 and then (Has_Controlled_Component (Etype (Component))
21472 or else (Chars (Component) /= Name_uParent
21473 and then Is_Controlled_Active
21474 (Etype (Component))))
21475 then
21476 Set_Has_Controlled_Component (T, True);
21477 Final_Storage_Only :=
21478 Final_Storage_Only
21479 and then Finalize_Storage_Only (Etype (Component));
21480 Ctrl_Components := True;
21481 end if;
21483 Next_Entity (Component);
21484 end loop;
21486 -- A Type is Finalize_Storage_Only only if all its controlled components
21487 -- are also.
21489 if Ctrl_Components then
21490 Set_Finalize_Storage_Only (T, Final_Storage_Only);
21491 end if;
21493 -- Place reference to end record on the proper entity, which may
21494 -- be a partial view.
21496 if Present (Def) then
21497 Process_End_Label (Def, 'e', Prev_T);
21498 end if;
21499 end Record_Type_Definition;
21501 ------------------------
21502 -- Replace_Components --
21503 ------------------------
21505 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id) is
21506 function Process (N : Node_Id) return Traverse_Result;
21508 -------------
21509 -- Process --
21510 -------------
21512 function Process (N : Node_Id) return Traverse_Result is
21513 Comp : Entity_Id;
21515 begin
21516 if Nkind (N) = N_Discriminant_Specification then
21517 Comp := First_Discriminant (Typ);
21518 while Present (Comp) loop
21519 if Chars (Comp) = Chars (Defining_Identifier (N)) then
21520 Set_Defining_Identifier (N, Comp);
21521 exit;
21522 end if;
21524 Next_Discriminant (Comp);
21525 end loop;
21527 elsif Nkind (N) = N_Component_Declaration then
21528 Comp := First_Component (Typ);
21529 while Present (Comp) loop
21530 if Chars (Comp) = Chars (Defining_Identifier (N)) then
21531 Set_Defining_Identifier (N, Comp);
21532 exit;
21533 end if;
21535 Next_Component (Comp);
21536 end loop;
21537 end if;
21539 return OK;
21540 end Process;
21542 procedure Replace is new Traverse_Proc (Process);
21544 -- Start of processing for Replace_Components
21546 begin
21547 Replace (Decl);
21548 end Replace_Components;
21550 -------------------------------
21551 -- Set_Completion_Referenced --
21552 -------------------------------
21554 procedure Set_Completion_Referenced (E : Entity_Id) is
21555 begin
21556 -- If in main unit, mark entity that is a completion as referenced,
21557 -- warnings go on the partial view when needed.
21559 if In_Extended_Main_Source_Unit (E) then
21560 Set_Referenced (E);
21561 end if;
21562 end Set_Completion_Referenced;
21564 ---------------------
21565 -- Set_Default_SSO --
21566 ---------------------
21568 procedure Set_Default_SSO (T : Entity_Id) is
21569 begin
21570 case Opt.Default_SSO is
21571 when ' ' =>
21572 null;
21573 when 'L' =>
21574 Set_SSO_Set_Low_By_Default (T, True);
21575 when 'H' =>
21576 Set_SSO_Set_High_By_Default (T, True);
21577 when others =>
21578 raise Program_Error;
21579 end case;
21580 end Set_Default_SSO;
21582 ---------------------
21583 -- Set_Fixed_Range --
21584 ---------------------
21586 -- The range for fixed-point types is complicated by the fact that we
21587 -- do not know the exact end points at the time of the declaration. This
21588 -- is true for three reasons:
21590 -- A size clause may affect the fudging of the end-points.
21591 -- A small clause may affect the values of the end-points.
21592 -- We try to include the end-points if it does not affect the size.
21594 -- This means that the actual end-points must be established at the
21595 -- point when the type is frozen. Meanwhile, we first narrow the range
21596 -- as permitted (so that it will fit if necessary in a small specified
21597 -- size), and then build a range subtree with these narrowed bounds.
21598 -- Set_Fixed_Range constructs the range from real literal values, and
21599 -- sets the range as the Scalar_Range of the given fixed-point type entity.
21601 -- The parent of this range is set to point to the entity so that it is
21602 -- properly hooked into the tree (unlike normal Scalar_Range entries for
21603 -- other scalar types, which are just pointers to the range in the
21604 -- original tree, this would otherwise be an orphan).
21606 -- The tree is left unanalyzed. When the type is frozen, the processing
21607 -- in Freeze.Freeze_Fixed_Point_Type notices that the range is not
21608 -- analyzed, and uses this as an indication that it should complete
21609 -- work on the range (it will know the final small and size values).
21611 procedure Set_Fixed_Range
21612 (E : Entity_Id;
21613 Loc : Source_Ptr;
21614 Lo : Ureal;
21615 Hi : Ureal)
21617 S : constant Node_Id :=
21618 Make_Range (Loc,
21619 Low_Bound => Make_Real_Literal (Loc, Lo),
21620 High_Bound => Make_Real_Literal (Loc, Hi));
21621 begin
21622 Set_Scalar_Range (E, S);
21623 Set_Parent (S, E);
21625 -- Before the freeze point, the bounds of a fixed point are universal
21626 -- and carry the corresponding type.
21628 Set_Etype (Low_Bound (S), Universal_Real);
21629 Set_Etype (High_Bound (S), Universal_Real);
21630 end Set_Fixed_Range;
21632 ----------------------------------
21633 -- Set_Scalar_Range_For_Subtype --
21634 ----------------------------------
21636 procedure Set_Scalar_Range_For_Subtype
21637 (Def_Id : Entity_Id;
21638 R : Node_Id;
21639 Subt : Entity_Id)
21641 Kind : constant Entity_Kind := Ekind (Def_Id);
21643 begin
21644 -- Defend against previous error
21646 if Nkind (R) = N_Error then
21647 return;
21648 end if;
21650 Set_Scalar_Range (Def_Id, R);
21652 -- We need to link the range into the tree before resolving it so
21653 -- that types that are referenced, including importantly the subtype
21654 -- itself, are properly frozen (Freeze_Expression requires that the
21655 -- expression be properly linked into the tree). Of course if it is
21656 -- already linked in, then we do not disturb the current link.
21658 if No (Parent (R)) then
21659 Set_Parent (R, Def_Id);
21660 end if;
21662 -- Reset the kind of the subtype during analysis of the range, to
21663 -- catch possible premature use in the bounds themselves.
21665 Set_Ekind (Def_Id, E_Void);
21666 Process_Range_Expr_In_Decl (R, Subt, Subtyp => Def_Id);
21667 Set_Ekind (Def_Id, Kind);
21668 end Set_Scalar_Range_For_Subtype;
21670 --------------------------------------------------------
21671 -- Set_Stored_Constraint_From_Discriminant_Constraint --
21672 --------------------------------------------------------
21674 procedure Set_Stored_Constraint_From_Discriminant_Constraint
21675 (E : Entity_Id)
21677 begin
21678 -- Make sure set if encountered during Expand_To_Stored_Constraint
21680 Set_Stored_Constraint (E, No_Elist);
21682 -- Give it the right value
21684 if Is_Constrained (E) and then Has_Discriminants (E) then
21685 Set_Stored_Constraint (E,
21686 Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
21687 end if;
21688 end Set_Stored_Constraint_From_Discriminant_Constraint;
21690 -------------------------------------
21691 -- Signed_Integer_Type_Declaration --
21692 -------------------------------------
21694 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
21695 Implicit_Base : Entity_Id;
21696 Base_Typ : Entity_Id;
21697 Lo_Val : Uint;
21698 Hi_Val : Uint;
21699 Errs : Boolean := False;
21700 Lo : Node_Id;
21701 Hi : Node_Id;
21703 function Can_Derive_From (E : Entity_Id) return Boolean;
21704 -- Determine whether given bounds allow derivation from specified type
21706 procedure Check_Bound (Expr : Node_Id);
21707 -- Check bound to make sure it is integral and static. If not, post
21708 -- appropriate error message and set Errs flag
21710 ---------------------
21711 -- Can_Derive_From --
21712 ---------------------
21714 -- Note we check both bounds against both end values, to deal with
21715 -- strange types like ones with a range of 0 .. -12341234.
21717 function Can_Derive_From (E : Entity_Id) return Boolean is
21718 Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
21719 Hi : constant Uint := Expr_Value (Type_High_Bound (E));
21720 begin
21721 return Lo <= Lo_Val and then Lo_Val <= Hi
21722 and then
21723 Lo <= Hi_Val and then Hi_Val <= Hi;
21724 end Can_Derive_From;
21726 -----------------
21727 -- Check_Bound --
21728 -----------------
21730 procedure Check_Bound (Expr : Node_Id) is
21731 begin
21732 -- If a range constraint is used as an integer type definition, each
21733 -- bound of the range must be defined by a static expression of some
21734 -- integer type, but the two bounds need not have the same integer
21735 -- type (Negative bounds are allowed.) (RM 3.5.4)
21737 if not Is_Integer_Type (Etype (Expr)) then
21738 Error_Msg_N
21739 ("integer type definition bounds must be of integer type", Expr);
21740 Errs := True;
21742 elsif not Is_OK_Static_Expression (Expr) then
21743 Flag_Non_Static_Expr
21744 ("non-static expression used for integer type bound!", Expr);
21745 Errs := True;
21747 -- The bounds are folded into literals, and we set their type to be
21748 -- universal, to avoid typing difficulties: we cannot set the type
21749 -- of the literal to the new type, because this would be a forward
21750 -- reference for the back end, and if the original type is user-
21751 -- defined this can lead to spurious semantic errors (e.g. 2928-003).
21753 else
21754 if Is_Entity_Name (Expr) then
21755 Fold_Uint (Expr, Expr_Value (Expr), True);
21756 end if;
21758 Set_Etype (Expr, Universal_Integer);
21759 end if;
21760 end Check_Bound;
21762 -- Start of processing for Signed_Integer_Type_Declaration
21764 begin
21765 -- Create an anonymous base type
21767 Implicit_Base :=
21768 Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
21770 -- Analyze and check the bounds, they can be of any integer type
21772 Lo := Low_Bound (Def);
21773 Hi := High_Bound (Def);
21775 -- Arbitrarily use Integer as the type if either bound had an error
21777 if Hi = Error or else Lo = Error then
21778 Base_Typ := Any_Integer;
21779 Set_Error_Posted (T, True);
21781 -- Here both bounds are OK expressions
21783 else
21784 Analyze_And_Resolve (Lo, Any_Integer);
21785 Analyze_And_Resolve (Hi, Any_Integer);
21787 Check_Bound (Lo);
21788 Check_Bound (Hi);
21790 if Errs then
21791 Hi := Type_High_Bound (Standard_Long_Long_Integer);
21792 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
21793 end if;
21795 -- Find type to derive from
21797 Lo_Val := Expr_Value (Lo);
21798 Hi_Val := Expr_Value (Hi);
21800 if Can_Derive_From (Standard_Short_Short_Integer) then
21801 Base_Typ := Base_Type (Standard_Short_Short_Integer);
21803 elsif Can_Derive_From (Standard_Short_Integer) then
21804 Base_Typ := Base_Type (Standard_Short_Integer);
21806 elsif Can_Derive_From (Standard_Integer) then
21807 Base_Typ := Base_Type (Standard_Integer);
21809 elsif Can_Derive_From (Standard_Long_Integer) then
21810 Base_Typ := Base_Type (Standard_Long_Integer);
21812 elsif Can_Derive_From (Standard_Long_Long_Integer) then
21813 Check_Restriction (No_Long_Long_Integers, Def);
21814 Base_Typ := Base_Type (Standard_Long_Long_Integer);
21816 else
21817 Base_Typ := Base_Type (Standard_Long_Long_Integer);
21818 Error_Msg_N ("integer type definition bounds out of range", Def);
21819 Hi := Type_High_Bound (Standard_Long_Long_Integer);
21820 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
21821 end if;
21822 end if;
21824 -- Complete both implicit base and declared first subtype entities. The
21825 -- inheritance of the rep item chain ensures that SPARK-related pragmas
21826 -- are not clobbered when the signed integer type acts as a full view of
21827 -- a private type.
21829 Set_Etype (Implicit_Base, Base_Typ);
21830 Set_Size_Info (Implicit_Base, Base_Typ);
21831 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
21832 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
21833 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
21835 Set_Ekind (T, E_Signed_Integer_Subtype);
21836 Set_Etype (T, Implicit_Base);
21837 Set_Size_Info (T, Implicit_Base);
21838 Inherit_Rep_Item_Chain (T, Implicit_Base);
21839 Set_Scalar_Range (T, Def);
21840 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
21841 Set_Is_Constrained (T);
21842 end Signed_Integer_Type_Declaration;
21844 end Sem_Ch3;